c363388408864e7e39b0a2a242023c11576becfb
[metze/samba/wip.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 "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         struct timed_event *te;
37         int msg_type;
38         struct messaging_context *msg_ctx;
39         struct event_context *ev;
40         struct smbsrv_request *req;
41         enum pvfs_wait_notice reason;
42 };
43
44 /*
45   called from the ntvfs layer when we have requested setup of an async
46   call.  this ensures that async calls runs with the right state of
47   previous ntvfs handlers in the chain (such as security context)
48 */
49 NTSTATUS pvfs_async_setup(struct ntvfs_module_context *ntvfs,
50                           struct smbsrv_request *req, void *private)
51 {
52         struct pvfs_wait *pwait = private;
53         pwait->handler(pwait->private, pwait->reason);
54         return NT_STATUS_OK;
55 }
56
57 /*
58   receive a completion message for a wait
59 */
60 static void pvfs_wait_dispatch(struct messaging_context *msg, void *private, uint32_t msg_type, 
61                                uint32_t src, DATA_BLOB *data)
62 {
63         struct pvfs_wait *pwait = private;
64         struct smbsrv_request *req;
65
66         /* we need to check that this one is for us. See
67            messaging_send_ptr() for the other side of this.
68          */
69         if (data->length != sizeof(void *) ||
70             *(void **)data->data != pwait->private) {
71                 return;
72         }
73         pwait->reason = PVFS_WAIT_EVENT;
74         req = pwait->req;
75
76         /* the extra reference here is to ensure that the req
77            structure is not destroyed when the async request reply is
78            sent, which would cause problems with the other ntvfs
79            modules above us */
80         talloc_increase_ref_count(req);
81         ntvfs_async_setup(pwait->req, pwait);
82         talloc_free(req);
83 }
84
85
86 /*
87   receive a timeout on a message wait
88 */
89 static void pvfs_wait_timeout(struct event_context *ev, 
90                               struct timed_event *te, struct timeval t, void *private)
91 {
92         struct pvfs_wait *pwait = talloc_get_type(private, struct pvfs_wait);
93         struct smbsrv_request *req = pwait->req;
94
95         pwait->reason = PVFS_WAIT_TIMEOUT;
96
97         talloc_increase_ref_count(req);
98         ntvfs_async_setup(pwait->req, pwait);
99         talloc_free(req);
100 }
101
102
103 /*
104   destroy a pending wait
105  */
106 static int pvfs_wait_destructor(void *ptr)
107 {
108         struct pvfs_wait *pwait = ptr;
109         messaging_deregister(pwait->msg_ctx, pwait->msg_type, pwait);
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  void *pvfs_wait_message(struct pvfs_state *pvfs, 
122                         struct smbsrv_request *req, 
123                         int msg_type, 
124                         struct timeval end_time,
125                         void (*fn)(void *, enum pvfs_wait_notice),
126                         void *private)
127 {
128         struct pvfs_wait *pwait;
129
130         pwait = talloc(pvfs, struct pvfs_wait);
131         if (pwait == NULL) {
132                 return NULL;
133         }
134
135         pwait->private = private;
136         pwait->handler = fn;
137         pwait->msg_ctx = pvfs->tcon->smb_conn->connection->msg_ctx;
138         pwait->ev = req->tcon->smb_conn->connection->event.ctx;
139         pwait->msg_type = msg_type;
140         pwait->req = talloc_reference(pwait, req);
141         pwait->pvfs = pvfs;
142
143         /* setup a timer */
144         pwait->te = event_add_timed(pwait->ev, pwait, end_time, 
145                                     pvfs_wait_timeout, pwait);
146
147         /* register with the messaging subsystem for this message
148            type */
149         messaging_register(pwait->msg_ctx,
150                            pwait,
151                            msg_type,
152                            pvfs_wait_dispatch);
153
154         /* tell the main smb server layer that we will be replying 
155            asynchronously */
156         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC;
157
158         DLIST_ADD(pvfs->wait_list, pwait);
159
160         /* make sure we cleanup the timer and message handler */
161         talloc_set_destructor(pwait, pvfs_wait_destructor);
162
163         return pwait;
164 }
165
166
167 /*
168   cancel an outstanding async request
169 */
170 NTSTATUS pvfs_cancel(struct ntvfs_module_context *ntvfs, struct smbsrv_request *req)
171 {
172         struct pvfs_state *pvfs = ntvfs->private_data;
173         struct pvfs_wait *pwait;
174         for (pwait=pvfs->wait_list;pwait;pwait=pwait->next) {
175                 if (SVAL(req->in.hdr, HDR_MID) == SVAL(pwait->req->in.hdr, HDR_MID) &&
176                     req->smbpid == pwait->req->smbpid) {
177                         /* trigger a cancel on the request */
178                         pwait->reason = PVFS_WAIT_CANCEL;
179                         ntvfs_async_setup(pwait->req, pwait);
180                         return NT_STATUS_OK;
181                 }
182         }
183
184         return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
185 }