r17930: Merge noinclude branch:
[metze/samba-autobuild/.git] / source4 / ntvfs / posix / pvfs_wait.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - async request wait routines
5
6    Copyright (C) Andrew Tridgell 2004
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 "lib/events/events.h"
25 #include "lib/util/dlinklist.h"
26 #include "vfs_posix.h"
27 #include "smbd/service_stream.h"
28 #include "lib/messaging/irpc.h"
29
30 /* the context for a single wait instance */
31 struct pvfs_wait {
32         struct pvfs_wait *next, *prev;
33         struct pvfs_state *pvfs;
34         void (*handler)(void *, enum pvfs_wait_notice);
35         void *private;
36         int msg_type;
37         struct messaging_context *msg_ctx;
38         struct event_context *ev;
39         struct ntvfs_request *req;
40         enum pvfs_wait_notice reason;
41 };
42
43 /*
44   called from the ntvfs layer when we have requested setup of an async
45   call.  this ensures that async calls runs with the right state of
46   previous ntvfs handlers in the chain (such as security context)
47 */
48 NTSTATUS pvfs_async_setup(struct ntvfs_module_context *ntvfs,
49                           struct ntvfs_request *req, void *private)
50 {
51         struct pvfs_wait *pwait = private;
52         pwait->handler(pwait->private, pwait->reason);
53         return NT_STATUS_OK;
54 }
55
56 /*
57   receive a completion message for a wait
58 */
59 static void pvfs_wait_dispatch(struct messaging_context *msg, void *private, uint32_t msg_type, 
60                                uint32_t src, DATA_BLOB *data)
61 {
62         struct pvfs_wait *pwait = private;
63         struct ntvfs_request *req;
64
65         /* we need to check that this one is for us. See
66            messaging_send_ptr() for the other side of this.
67          */
68         if (data->length != sizeof(void *) ||
69             *(void **)data->data != pwait->private) {
70                 return;
71         }
72         pwait->reason = PVFS_WAIT_EVENT;
73         req = pwait->req;
74
75         /* the extra reference here is to ensure that the req
76            structure is not destroyed when the async request reply is
77            sent, which would cause problems with the other ntvfs
78            modules above us */
79         talloc_increase_ref_count(req);
80         ntvfs_async_setup(pwait->req, pwait);
81         talloc_free(req);
82 }
83
84
85 /*
86   receive a timeout on a message wait
87 */
88 static void pvfs_wait_timeout(struct event_context *ev, 
89                               struct timed_event *te, struct timeval t, void *private)
90 {
91         struct pvfs_wait *pwait = talloc_get_type(private, struct pvfs_wait);
92         struct ntvfs_request *req = pwait->req;
93
94         pwait->reason = PVFS_WAIT_TIMEOUT;
95
96         talloc_increase_ref_count(req);
97         ntvfs_async_setup(pwait->req, pwait);
98         talloc_free(req);
99 }
100
101
102 /*
103   destroy a pending wait
104  */
105 static int pvfs_wait_destructor(struct pvfs_wait *pwait)
106 {
107         if (pwait->msg_type != -1) {
108                 messaging_deregister(pwait->msg_ctx, pwait->msg_type, pwait);
109         }
110         DLIST_REMOVE(pwait->pvfs->wait_list, pwait);
111         return 0;
112 }
113
114 /*
115   setup a request to wait on a message of type msg_type, with a
116   timeout (given as an expiry time)
117
118   the return value is a handle. To stop waiting talloc_free this
119   handle.
120
121   if msg_type == -1 then no message is registered, and it is assumed
122   that the caller handles any messaging setup needed
123 */
124 void *pvfs_wait_message(struct pvfs_state *pvfs, 
125                         struct ntvfs_request *req, 
126                         int msg_type, 
127                         struct timeval end_time,
128                         void (*fn)(void *, enum pvfs_wait_notice),
129                         void *private)
130 {
131         struct pvfs_wait *pwait;
132
133         pwait = talloc(pvfs, struct pvfs_wait);
134         if (pwait == NULL) {
135                 return NULL;
136         }
137
138         pwait->private = private;
139         pwait->handler = fn;
140         pwait->msg_ctx = pvfs->ntvfs->ctx->msg_ctx;
141         pwait->ev = pvfs->ntvfs->ctx->event_ctx;
142         pwait->msg_type = msg_type;
143         pwait->req = talloc_reference(pwait, req);
144         pwait->pvfs = pvfs;
145
146         if (!timeval_is_zero(&end_time)) {
147                 /* setup a timer */
148                 event_add_timed(pwait->ev, pwait, end_time, pvfs_wait_timeout, pwait);
149         }
150
151         /* register with the messaging subsystem for this message
152            type */
153         if (msg_type != -1) {
154                 messaging_register(pwait->msg_ctx,
155                                    pwait,
156                                    msg_type,
157                                    pvfs_wait_dispatch);
158         }
159
160         /* tell the main smb server layer that we will be replying 
161            asynchronously */
162         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
163
164         DLIST_ADD(pvfs->wait_list, pwait);
165
166         /* make sure we cleanup the timer and message handler */
167         talloc_set_destructor(pwait, pvfs_wait_destructor);
168
169         return pwait;
170 }
171
172
173 /*
174   cancel an outstanding async request
175 */
176 NTSTATUS pvfs_cancel(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req)
177 {
178         struct pvfs_state *pvfs = ntvfs->private_data;
179         struct pvfs_wait *pwait;
180
181         for (pwait=pvfs->wait_list;pwait;pwait=pwait->next) {
182                 if (pwait->req == req) {
183                         /* trigger a cancel on the request */
184                         pwait->reason = PVFS_WAIT_CANCEL;
185                         ntvfs_async_setup(pwait->req, pwait);
186                         return NT_STATUS_OK;
187                 }
188         }
189
190         return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
191 }