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