r20698: added AIO writing support
[kai/samba.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 struct pvfs_aio_write_state {
36         struct ntvfs_request *req;
37         union smb_write *wr;
38         struct pvfs_file *f;
39         struct aio_event *ae;
40 };
41
42 /*
43   called when an aio read has finished
44 */
45 static void pvfs_aio_read_handler(struct event_context *ev, struct aio_event *ae, 
46                              int ret, void *private)
47 {
48         struct pvfs_aio_read_state *state = talloc_get_type(private, 
49                                                             struct pvfs_aio_read_state);
50         struct pvfs_file *f = state->f;
51         union smb_read *rd = state->rd;
52
53         if (ret < 0) {
54                 /* errno is -ret on error */
55                 state->req->async_states->status = pvfs_map_errno(f->pvfs, -ret);
56                 state->req->async_states->send_fn(state->req);
57                 return;
58         }
59
60         f->handle->position = f->handle->seek_offset = rd->readx.in.offset + ret;
61
62         rd->readx.out.nread = ret;
63         rd->readx.out.remaining = 0xFFFF;
64         rd->readx.out.compaction_mode = 0; 
65
66         talloc_steal(ev, state->ae);
67
68         state->req->async_states->status = NT_STATUS_OK;
69         state->req->async_states->send_fn(state->req);
70 }
71
72
73 /*
74   read from a file
75 */
76 NTSTATUS pvfs_aio_pread(struct ntvfs_request *req, union smb_read *rd,
77                         struct pvfs_file *f, uint32_t maxcnt)
78 {
79         struct iocb iocb;
80         struct pvfs_aio_read_state *state;
81
82         state = talloc(req, struct pvfs_aio_read_state);
83         NT_STATUS_HAVE_NO_MEMORY(state);
84
85         io_prep_pread(&iocb, f->handle->fd, rd->readx.out.data,
86                       maxcnt, rd->readx.in.offset);
87         state->ae = event_add_aio(req->ctx->event_ctx, req->ctx->event_ctx, &iocb, 
88                                   pvfs_aio_read_handler, state);
89         if (state->ae == NULL) {
90                 DEBUG(0,("Failed event_add_aio\n"));
91                 talloc_free(state);
92                 return NT_STATUS_NOT_IMPLEMENTED;
93         }
94
95         state->req  = req;
96         state->rd   = rd;
97         state->f    = f;
98
99         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
100
101         return NT_STATUS_OK;
102 }
103
104
105
106
107 /*
108   called when an aio write has finished
109 */
110 static void pvfs_aio_write_handler(struct event_context *ev, struct aio_event *ae, 
111                              int ret, void *private)
112 {
113         struct pvfs_aio_write_state *state = talloc_get_type(private, 
114                                                             struct pvfs_aio_write_state);
115         struct pvfs_file *f = state->f;
116         union smb_write *wr = state->wr;
117
118         if (ret < 0) {
119                 /* errno is -ret on error */
120                 state->req->async_states->status = pvfs_map_errno(f->pvfs, -ret);
121                 state->req->async_states->send_fn(state->req);
122                 return;
123         }
124
125         f->handle->seek_offset = wr->writex.in.offset + ret;
126
127         wr->writex.out.nwritten = ret;
128         wr->writex.out.remaining = 0;
129
130         talloc_steal(ev, state->ae);
131
132         state->req->async_states->status = NT_STATUS_OK;
133         state->req->async_states->send_fn(state->req);
134 }
135
136
137 /*
138   write to a file
139 */
140 NTSTATUS pvfs_aio_pwrite(struct ntvfs_request *req, union smb_write *wr,
141                          struct pvfs_file *f)
142 {
143         struct iocb iocb;
144         struct pvfs_aio_write_state *state;
145
146         state = talloc(req, struct pvfs_aio_write_state);
147         NT_STATUS_HAVE_NO_MEMORY(state);
148
149         io_prep_pwrite(&iocb, f->handle->fd, wr->writex.in.data,
150                        wr->writex.in.count, wr->writex.in.offset);
151         state->ae = event_add_aio(req->ctx->event_ctx, req->ctx->event_ctx, &iocb, 
152                                   pvfs_aio_write_handler, state);
153         if (state->ae == NULL) {
154                 DEBUG(0,("Failed event_add_aio\n"));
155                 talloc_free(state);
156                 return NT_STATUS_NOT_IMPLEMENTED;
157         }
158
159         state->req  = req;
160         state->wr   = wr;
161         state->f    = f;
162
163         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
164
165         return NT_STATUS_OK;
166 }
167