262b856aa971b748321b699bf51b9f74a2d8d331
[ambi/samba-autobuild/.git] / source4 / ntvfs / posix / pvfs_aio.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - Linux AIO calls
5
6    Copyright (C) Andrew Tridgell 2006
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "vfs_posix.h"
25 #include "lib/events/events.h"
26 #include "system/aio.h"
27
28 struct pvfs_aio_read_state {
29         struct ntvfs_request *req;
30         union smb_read *rd;
31         struct pvfs_file *f;
32         struct aio_event *ae;
33 };
34
35 /*
36   called when an aio read has finished
37 */
38 static void pvfs_aio_handler(struct event_context *ev, struct aio_event *ae, 
39                              int ret, void *private)
40 {
41         struct pvfs_aio_read_state *state = talloc_get_type(private, 
42                                                             struct pvfs_aio_read_state);
43         struct pvfs_file *f = state->f;
44         union smb_read *rd = state->rd;
45
46         if (ret < 0) {
47                 /* errno is -ret on error */
48                 state->req->async_states->status = pvfs_map_errno(f->pvfs, -ret);
49                 state->req->async_states->send_fn(state->req);
50                 return;
51         }
52
53         f->handle->position = f->handle->seek_offset = rd->readx.in.offset + ret;
54
55         rd->readx.out.nread = ret;
56         rd->readx.out.remaining = 0xFFFF;
57         rd->readx.out.compaction_mode = 0; 
58
59         talloc_steal(ev, state->ae);
60
61         state->req->async_states->status = NT_STATUS_OK;
62         state->req->async_states->send_fn(state->req);
63 }
64
65
66 /*
67   read from a file
68 */
69 NTSTATUS pvfs_aio_pread(struct ntvfs_request *req, union smb_read *rd,
70                         struct pvfs_file *f, uint32_t maxcnt)
71 {
72         struct iocb iocb;
73         struct pvfs_aio_read_state *state;
74
75         state = talloc(req, struct pvfs_aio_read_state);
76         NT_STATUS_HAVE_NO_MEMORY(state);
77
78         io_prep_pread(&iocb, f->handle->fd, rd->readx.out.data,
79                       maxcnt, rd->readx.in.offset);
80         state->ae = event_add_aio(req->ctx->event_ctx, req->ctx->event_ctx, &iocb, 
81                                   pvfs_aio_handler, state);
82         if (state->ae == NULL) {
83                 DEBUG(0,("Failed event_add_aio\n"));
84                 talloc_free(state);
85                 return NT_STATUS_NOT_IMPLEMENTED;
86         }
87
88         state->req  = req;
89         state->rd   = rd;
90         state->f    = f;
91
92         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
93
94         return NT_STATUS_OK;
95 }
96