r5197: moved events code to lib/events/ (suggestion from metze)
[nivanova/samba-autobuild/.git] / source4 / ntvfs / cifs / vfs_cifs.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    CIFS-on-CIFS NTVFS filesystem backend
5
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) James J Myers 2003 <myersjj@samba.org>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 /*
24   this implements a CIFS->CIFS NTVFS filesystem backend. 
25   
26 */
27
28 #include "includes.h"
29 #include "lib/events/events.h"
30 #include "libcli/raw/libcliraw.h"
31 #include "libcli/composite/composite.h"
32 #include "smb_server/smb_server.h"
33 #include "smbd/service_stream.h"
34
35 /* this is stored in ntvfs_private */
36 struct cvfs_private {
37         struct smbcli_tree *tree;
38         struct smbcli_transport *transport;
39         struct smbsrv_tcon *tcon;
40         BOOL map_generic;
41 };
42
43
44 /* a structure used to pass information to an async handler */
45 struct async_info {
46         struct smbsrv_request *req;
47         void *parms;
48 };
49
50 #define SETUP_PID private->tree->session->pid = SVAL(req->in.hdr, HDR_PID)
51
52 /*
53   a handler for oplock break events from the server - these need to be passed
54   along to the client
55  */
56 static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *p_private)
57 {
58         struct cvfs_private *private = p_private;
59         
60         DEBUG(5,("vfs_cifs: sending oplock break level %d for fnum %d\n", level, fnum));
61         return req_send_oplock_break(private->tcon, fnum, level);
62 }
63
64  /*
65   a handler for read events on a connection to a backend server
66 */
67 static void cifs_socket_handler(struct event_context *ev, struct fd_event *fde, 
68                                 uint16_t flags, void *private)
69 {
70         struct cvfs_private *cvfs = talloc_get_type(private, struct cvfs_private);
71         struct smbsrv_tcon *tcon = cvfs->tcon;
72         
73         if (!smbcli_transport_process(cvfs->transport)) {
74                 /* the connection to our server is dead */
75                 talloc_free(tcon);
76         }
77 }
78
79 /*
80   connect to a share - used when a tree_connect operation comes in.
81 */
82 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs, 
83                              struct smbsrv_request *req, const char *sharename)
84 {
85         struct smbsrv_tcon *tcon = req->tcon;
86         NTSTATUS status;
87         struct cvfs_private *private;
88         const char *host, *user, *pass, *domain, *remote_share;
89         struct smb_composite_connect io;
90         struct composite_context *creq;
91         struct fd_event *fde;
92
93         /* Here we need to determine which server to connect to.
94          * For now we use parametric options, type cifs.
95          * Later we will use security=server and auth_server.c.
96          */
97         host = lp_parm_string(req->tcon->service, "cifs", "server");
98         user = lp_parm_string(req->tcon->service, "cifs", "user");
99         pass = lp_parm_string(req->tcon->service, "cifs", "password");
100         domain = lp_parm_string(req->tcon->service, "cifs", "domain");
101         remote_share = lp_parm_string(req->tcon->service, "cifs", "share");
102         if (!remote_share) {
103                 remote_share = sharename;
104         }
105
106         if (!host || !user || !pass || !domain) {
107                 DEBUG(1,("CIFS backend: You must supply server, user, password and domain\n"));
108                 return NT_STATUS_INVALID_PARAMETER;
109         }
110         
111         private = talloc(req->tcon, struct cvfs_private);
112         if (!private) {
113                 return NT_STATUS_NO_MEMORY;
114         }
115         ZERO_STRUCTP(private);
116
117         ntvfs->private_data = private;
118
119         /* connect to the server, using the smbd event context */
120         io.in.dest_host = host;
121         io.in.port = 0;
122         io.in.called_name = host;
123         io.in.calling_name = "vfs_cifs";
124         io.in.service = remote_share;
125         io.in.service_type = "?????";
126         io.in.domain = domain;
127         io.in.user = user;
128         io.in.password = pass;
129         
130         creq = smb_composite_connect_send(&io, tcon->smb_conn->connection->event.ctx);
131         status = smb_composite_connect_recv(creq, private);
132         NT_STATUS_NOT_OK_RETURN(status);
133
134         private->tree = io.out.tree;
135
136         private->transport = private->tree->session->transport;
137         SETUP_PID;
138         private->tcon = req->tcon;
139
140         tcon->fs_type = talloc_strdup(tcon, "NTFS");
141         tcon->dev_type = talloc_strdup(tcon, "A:");
142         
143         /* we need to receive oplock break requests from the server */
144         smbcli_oplock_handler(private->transport, oplock_handler, private);
145
146         /* take over event handling for this socket */
147         talloc_free(private->transport->socket->event.fde);
148         fde = event_add_fd(private->transport->socket->event.ctx,
149                            private,
150                            socket_get_fd(private->transport->socket->sock),
151                            EVENT_FD_READ | EVENT_FD_WRITE,
152                            cifs_socket_handler,
153                            private);
154         private->transport->socket->event.fde = fde;
155
156
157         private->map_generic = lp_parm_bool(req->tcon->service, 
158                                             "cifs", "mapgeneric", False);
159
160         return NT_STATUS_OK;
161 }
162
163 /*
164   disconnect from a share
165 */
166 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs, 
167                                 struct smbsrv_tcon *tcon)
168 {
169         struct cvfs_private *private = ntvfs->private_data;
170
171         talloc_free(private);
172
173         return NT_STATUS_OK;
174 }
175
176 /*
177   a handler for simple async replies
178   this handler can only be used for functions that don't return any
179   parameters (those that just return a status code)
180  */
181 static void async_simple(struct smbcli_request *c_req)
182 {
183         struct async_info *async = c_req->async.private;
184         struct smbsrv_request *req = async->req;
185         req->async_states->status = smbcli_request_simple_recv(c_req);
186         req->async_states->send_fn(req);
187 }
188
189
190 /* save some typing for the simple functions */
191 #define ASYNC_RECV_TAIL(io, async_fn) do { \
192         if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
193         { \
194                 struct async_info *async; \
195                 async = talloc(req, struct async_info); \
196                 if (!async) return NT_STATUS_NO_MEMORY; \
197                 async->parms = io; \
198                 async->req = req; \
199                 c_req->async.private = async; \
200         } \
201         c_req->async.fn = async_fn; \
202         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
203         return NT_STATUS_OK; \
204 } while (0)
205
206 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
207
208 /*
209   delete a file - the dirtype specifies the file types to include in the search. 
210   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
211 */
212 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs, 
213                             struct smbsrv_request *req, struct smb_unlink *unl)
214 {
215         struct cvfs_private *private = ntvfs->private_data;
216         struct smbcli_request *c_req;
217
218         SETUP_PID;
219
220         /* see if the front end will allow us to perform this
221            function asynchronously.  */
222         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
223                 return smb_raw_unlink(private->tree, unl);
224         }
225
226         c_req = smb_raw_unlink_send(private->tree, unl);
227
228         SIMPLE_ASYNC_TAIL;
229 }
230
231 /*
232   a handler for async ioctl replies
233  */
234 static void async_ioctl(struct smbcli_request *c_req)
235 {
236         struct async_info *async = c_req->async.private;
237         struct smbsrv_request *req = async->req;
238         req->async_states->status = smb_raw_ioctl_recv(c_req, req, async->parms);
239         req->async_states->send_fn(req);
240 }
241
242 /*
243   ioctl interface
244 */
245 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs, 
246                                 struct smbsrv_request *req, union smb_ioctl *io)
247 {
248         struct cvfs_private *private = ntvfs->private_data;
249         struct smbcli_request *c_req;
250
251         SETUP_PID;
252
253         /* see if the front end will allow us to perform this
254            function asynchronously.  */
255         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
256                 return smb_raw_ioctl(private->tree, req, io);
257         }
258
259         c_req = smb_raw_ioctl_send(private->tree, io);
260
261         ASYNC_RECV_TAIL(io, async_ioctl);
262 }
263
264 /*
265   check if a directory exists
266 */
267 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs, 
268                                 struct smbsrv_request *req, struct smb_chkpath *cp)
269 {
270         struct cvfs_private *private = ntvfs->private_data;
271         struct smbcli_request *c_req;
272
273         SETUP_PID;
274
275         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
276                 return smb_raw_chkpath(private->tree, cp);
277         }
278
279         c_req = smb_raw_chkpath_send(private->tree, cp);
280
281         SIMPLE_ASYNC_TAIL;
282 }
283
284 /*
285   a handler for async qpathinfo replies
286  */
287 static void async_qpathinfo(struct smbcli_request *c_req)
288 {
289         struct async_info *async = c_req->async.private;
290         struct smbsrv_request *req = async->req;
291         req->async_states->status = smb_raw_pathinfo_recv(c_req, req, async->parms);
292         req->async_states->send_fn(req);
293 }
294
295 /*
296   return info on a pathname
297 */
298 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs, 
299                                 struct smbsrv_request *req, union smb_fileinfo *info)
300 {
301         struct cvfs_private *private = ntvfs->private_data;
302         struct smbcli_request *c_req;
303
304         SETUP_PID;
305
306         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
307                 return smb_raw_pathinfo(private->tree, req, info);
308         }
309
310         c_req = smb_raw_pathinfo_send(private->tree, info);
311
312         ASYNC_RECV_TAIL(info, async_qpathinfo);
313 }
314
315 /*
316   a handler for async qfileinfo replies
317  */
318 static void async_qfileinfo(struct smbcli_request *c_req)
319 {
320         struct async_info *async = c_req->async.private;
321         struct smbsrv_request *req = async->req;
322         req->async_states->status = smb_raw_fileinfo_recv(c_req, req, async->parms);
323         req->async_states->send_fn(req);
324 }
325
326 /*
327   query info on a open file
328 */
329 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs, 
330                                 struct smbsrv_request *req, union smb_fileinfo *info)
331 {
332         struct cvfs_private *private = ntvfs->private_data;
333         struct smbcli_request *c_req;
334
335         SETUP_PID;
336
337         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
338                 return smb_raw_fileinfo(private->tree, req, info);
339         }
340
341         c_req = smb_raw_fileinfo_send(private->tree, info);
342
343         ASYNC_RECV_TAIL(info, async_qfileinfo);
344 }
345
346
347 /*
348   set info on a pathname
349 */
350 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs, 
351                                 struct smbsrv_request *req, union smb_setfileinfo *st)
352 {
353         struct cvfs_private *private = ntvfs->private_data;
354         struct smbcli_request *c_req;
355
356         SETUP_PID;
357
358         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
359                 return smb_raw_setpathinfo(private->tree, st);
360         }
361
362         c_req = smb_raw_setpathinfo_send(private->tree, st);
363
364         SIMPLE_ASYNC_TAIL;
365 }
366
367
368 /*
369   a handler for async open replies
370  */
371 static void async_open(struct smbcli_request *c_req)
372 {
373         struct async_info *async = c_req->async.private;
374         struct smbsrv_request *req = async->req;
375         req->async_states->status = smb_raw_open_recv(c_req, req, async->parms);
376         req->async_states->send_fn(req);
377 }
378
379 /*
380   open a file
381 */
382 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs, 
383                                 struct smbsrv_request *req, union smb_open *io)
384 {
385         struct cvfs_private *private = ntvfs->private_data;
386         struct smbcli_request *c_req;
387
388         SETUP_PID;
389
390         if (io->generic.level != RAW_OPEN_GENERIC &&
391             private->map_generic) {
392                 return ntvfs_map_open(req, io, ntvfs);
393         }
394
395         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
396                 return smb_raw_open(private->tree, req, io);
397         }
398
399         c_req = smb_raw_open_send(private->tree, io);
400
401         ASYNC_RECV_TAIL(io, async_open);
402 }
403
404 /*
405   create a directory
406 */
407 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs, 
408                                 struct smbsrv_request *req, union smb_mkdir *md)
409 {
410         struct cvfs_private *private = ntvfs->private_data;
411         struct smbcli_request *c_req;
412
413         SETUP_PID;
414
415         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
416                 return smb_raw_mkdir(private->tree, md);
417         }
418
419         c_req = smb_raw_mkdir_send(private->tree, md);
420
421         SIMPLE_ASYNC_TAIL;
422 }
423
424 /*
425   remove a directory
426 */
427 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs, 
428                                 struct smbsrv_request *req, struct smb_rmdir *rd)
429 {
430         struct cvfs_private *private = ntvfs->private_data;
431         struct smbcli_request *c_req;
432
433         SETUP_PID;
434
435         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
436                 return smb_raw_rmdir(private->tree, rd);
437         }
438         c_req = smb_raw_rmdir_send(private->tree, rd);
439
440         SIMPLE_ASYNC_TAIL;
441 }
442
443 /*
444   rename a set of files
445 */
446 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs, 
447                                 struct smbsrv_request *req, union smb_rename *ren)
448 {
449         struct cvfs_private *private = ntvfs->private_data;
450         struct smbcli_request *c_req;
451
452         SETUP_PID;
453
454         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
455                 return smb_raw_rename(private->tree, ren);
456         }
457
458         c_req = smb_raw_rename_send(private->tree, ren);
459
460         SIMPLE_ASYNC_TAIL;
461 }
462
463 /*
464   copy a set of files
465 */
466 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs, 
467                                 struct smbsrv_request *req, struct smb_copy *cp)
468 {
469         return NT_STATUS_NOT_SUPPORTED;
470 }
471
472 /*
473   a handler for async read replies
474  */
475 static void async_read(struct smbcli_request *c_req)
476 {
477         struct async_info *async = c_req->async.private;
478         struct smbsrv_request *req = async->req;
479         req->async_states->status = smb_raw_read_recv(c_req, async->parms);
480         req->async_states->send_fn(req);
481 }
482
483 /*
484   read from a file
485 */
486 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs, 
487                                 struct smbsrv_request *req, union smb_read *rd)
488 {
489         struct cvfs_private *private = ntvfs->private_data;
490         struct smbcli_request *c_req;
491
492         SETUP_PID;
493
494         if (rd->generic.level != RAW_READ_GENERIC &&
495             private->map_generic) {
496                 return ntvfs_map_read(req, rd, ntvfs);
497         }
498
499         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
500                 return smb_raw_read(private->tree, rd);
501         }
502
503         c_req = smb_raw_read_send(private->tree, rd);
504
505         ASYNC_RECV_TAIL(rd, async_read);
506 }
507
508 /*
509   a handler for async write replies
510  */
511 static void async_write(struct smbcli_request *c_req)
512 {
513         struct async_info *async = c_req->async.private;
514         struct smbsrv_request *req = async->req;
515         req->async_states->status = smb_raw_write_recv(c_req, async->parms);
516         req->async_states->send_fn(req);
517 }
518
519 /*
520   write to a file
521 */
522 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs, 
523                                 struct smbsrv_request *req, union smb_write *wr)
524 {
525         struct cvfs_private *private = ntvfs->private_data;
526         struct smbcli_request *c_req;
527
528         SETUP_PID;
529
530         if (wr->generic.level != RAW_WRITE_GENERIC &&
531             private->map_generic) {
532                 return ntvfs_map_write(req, wr, ntvfs);
533         }
534
535         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
536                 return smb_raw_write(private->tree, wr);
537         }
538
539         c_req = smb_raw_write_send(private->tree, wr);
540
541         ASYNC_RECV_TAIL(wr, async_write);
542 }
543
544 /*
545   seek in a file
546 */
547 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs, 
548                           struct smbsrv_request *req, struct smb_seek *io)
549 {
550         struct cvfs_private *private = ntvfs->private_data;
551         struct smbcli_request *c_req;
552
553         SETUP_PID;
554
555         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
556                 return smb_raw_seek(private->tree, io);
557         }
558
559         c_req = smb_raw_seek_send(private->tree, io);
560
561         SIMPLE_ASYNC_TAIL;
562 }
563
564 /*
565   flush a file
566 */
567 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs, 
568                            struct smbsrv_request *req, struct smb_flush *io)
569 {
570         struct cvfs_private *private = ntvfs->private_data;
571         struct smbcli_request *c_req;
572
573         SETUP_PID;
574
575         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
576                 return smb_raw_flush(private->tree, io);
577         }
578
579         c_req = smb_raw_flush_send(private->tree, io);
580
581         SIMPLE_ASYNC_TAIL;
582 }
583
584 /*
585   close a file
586 */
587 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs, 
588                                 struct smbsrv_request *req, union smb_close *io)
589 {
590         struct cvfs_private *private = ntvfs->private_data;
591         struct smbcli_request *c_req;
592
593         SETUP_PID;
594
595         if (io->generic.level != RAW_CLOSE_GENERIC &&
596             private->map_generic) {
597                 return ntvfs_map_close(req, io, ntvfs);
598         }
599
600         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
601                 return smb_raw_close(private->tree, io);
602         }
603
604         c_req = smb_raw_close_send(private->tree, io);
605
606         SIMPLE_ASYNC_TAIL;
607 }
608
609 /*
610   exit - closing files open by the pid
611 */
612 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs, 
613                                 struct smbsrv_request *req)
614 {
615         struct cvfs_private *private = ntvfs->private_data;
616         struct smbcli_request *c_req;
617
618         SETUP_PID;
619
620         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
621                 return smb_raw_exit(private->tree->session);
622         }
623
624         c_req = smb_raw_exit_send(private->tree->session);
625
626         SIMPLE_ASYNC_TAIL;
627 }
628
629 /*
630   logoff - closing files open by the user
631 */
632 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs, 
633                                 struct smbsrv_request *req)
634 {
635         /* we can't do this right in the cifs backend .... */
636         return NT_STATUS_OK;
637 }
638
639 /*
640   setup for an async call - nothing to do yet
641 */
642 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs, 
643                                  struct smbsrv_request *req, 
644                                  void *private)
645 {
646         return NT_STATUS_OK;
647 }
648
649 /*
650   cancel an async call
651 */
652 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs, 
653                             struct smbsrv_request *req)
654 {
655         return NT_STATUS_NOT_IMPLEMENTED;
656 }
657
658 /*
659   lock a byte range
660 */
661 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs, 
662                                 struct smbsrv_request *req, union smb_lock *lck)
663 {
664         struct cvfs_private *private = ntvfs->private_data;
665         struct smbcli_request *c_req;
666
667         SETUP_PID;
668
669         if (lck->generic.level != RAW_LOCK_GENERIC &&
670             private->map_generic) {
671                 return ntvfs_map_lock(req, lck, ntvfs);
672         }
673
674         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
675                 return smb_raw_lock(private->tree, lck);
676         }
677
678         c_req = smb_raw_lock_send(private->tree, lck);
679         SIMPLE_ASYNC_TAIL;
680 }
681
682 /*
683   set info on a open file
684 */
685 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs, 
686                                  struct smbsrv_request *req, 
687                                  union smb_setfileinfo *info)
688 {
689         struct cvfs_private *private = ntvfs->private_data;
690         struct smbcli_request *c_req;
691
692         SETUP_PID;
693
694         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
695                 return smb_raw_setfileinfo(private->tree, info);
696         }
697         c_req = smb_raw_setfileinfo_send(private->tree, info);
698
699         SIMPLE_ASYNC_TAIL;
700 }
701
702
703 /*
704   a handler for async fsinfo replies
705  */
706 static void async_fsinfo(struct smbcli_request *c_req)
707 {
708         struct async_info *async = c_req->async.private;
709         struct smbsrv_request *req = async->req;
710         req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
711         req->async_states->send_fn(req);
712 }
713
714 /*
715   return filesystem space info
716 */
717 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs, 
718                                 struct smbsrv_request *req, union smb_fsinfo *fs)
719 {
720         struct cvfs_private *private = ntvfs->private_data;
721         struct smbcli_request *c_req;
722
723         SETUP_PID;
724
725         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
726                 return smb_raw_fsinfo(private->tree, req, fs);
727         }
728
729         c_req = smb_raw_fsinfo_send(private->tree, req, fs);
730
731         ASYNC_RECV_TAIL(fs, async_fsinfo);
732 }
733
734 /*
735   return print queue info
736 */
737 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs, 
738                                 struct smbsrv_request *req, union smb_lpq *lpq)
739 {
740         return NT_STATUS_NOT_SUPPORTED;
741 }
742
743 /* 
744    list files in a directory matching a wildcard pattern
745 */
746 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs, 
747                                   struct smbsrv_request *req, union smb_search_first *io, 
748                                   void *search_private, 
749                                   BOOL (*callback)(void *, union smb_search_data *))
750 {
751         struct cvfs_private *private = ntvfs->private_data;
752
753         SETUP_PID;
754
755         return smb_raw_search_first(private->tree, req, io, search_private, callback);
756 }
757
758 /* continue a search */
759 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs, 
760                                  struct smbsrv_request *req, union smb_search_next *io, 
761                                  void *search_private, 
762                                  BOOL (*callback)(void *, union smb_search_data *))
763 {
764         struct cvfs_private *private = ntvfs->private_data;
765
766         SETUP_PID;
767
768         return smb_raw_search_next(private->tree, req, io, search_private, callback);
769 }
770
771 /* close a search */
772 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs, 
773                                   struct smbsrv_request *req, union smb_search_close *io)
774 {
775         struct cvfs_private *private = ntvfs->private_data;
776
777         SETUP_PID;
778
779         return smb_raw_search_close(private->tree, io);
780 }
781
782 /*
783   a handler for async trans2 replies
784  */
785 static void async_trans2(struct smbcli_request *c_req)
786 {
787         struct async_info *async = c_req->async.private;
788         struct smbsrv_request *req = async->req;
789         req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
790         req->async_states->send_fn(req);
791 }
792
793 /* raw trans2 */
794 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs, 
795                                 struct smbsrv_request *req, struct smb_trans2 *trans2)
796 {
797         struct cvfs_private *private = ntvfs->private_data;
798         struct smbcli_request *c_req;
799
800         SETUP_PID;
801
802         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
803                 return smb_raw_trans2(private->tree, req, trans2);
804         }
805
806         c_req = smb_raw_trans2_send(private->tree, trans2);
807
808         ASYNC_RECV_TAIL(trans2, async_trans2);
809 }
810
811
812 /* SMBtrans - not used on file shares */
813 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs, 
814                                 struct smbsrv_request *req, struct smb_trans2 *trans2)
815 {
816         return NT_STATUS_ACCESS_DENIED;
817 }
818
819 /*
820   initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
821  */
822 NTSTATUS ntvfs_cifs_init(void)
823 {
824         NTSTATUS ret;
825         struct ntvfs_ops ops;
826
827         ZERO_STRUCT(ops);
828
829         /* fill in the name and type */
830         ops.name = "cifs";
831         ops.type = NTVFS_DISK;
832         
833         /* fill in all the operations */
834         ops.connect = cvfs_connect;
835         ops.disconnect = cvfs_disconnect;
836         ops.unlink = cvfs_unlink;
837         ops.chkpath = cvfs_chkpath;
838         ops.qpathinfo = cvfs_qpathinfo;
839         ops.setpathinfo = cvfs_setpathinfo;
840         ops.openfile = cvfs_open;
841         ops.mkdir = cvfs_mkdir;
842         ops.rmdir = cvfs_rmdir;
843         ops.rename = cvfs_rename;
844         ops.copy = cvfs_copy;
845         ops.ioctl = cvfs_ioctl;
846         ops.read = cvfs_read;
847         ops.write = cvfs_write;
848         ops.seek = cvfs_seek;
849         ops.flush = cvfs_flush; 
850         ops.close = cvfs_close;
851         ops.exit = cvfs_exit;
852         ops.lock = cvfs_lock;
853         ops.setfileinfo = cvfs_setfileinfo;
854         ops.qfileinfo = cvfs_qfileinfo;
855         ops.fsinfo = cvfs_fsinfo;
856         ops.lpq = cvfs_lpq;
857         ops.search_first = cvfs_search_first;
858         ops.search_next = cvfs_search_next;
859         ops.search_close = cvfs_search_close;
860         ops.trans = cvfs_trans;
861         ops.logoff = cvfs_logoff;
862         ops.async_setup = cvfs_async_setup;
863         ops.cancel = cvfs_cancel;
864
865         if (lp_parm_bool(-1, "cifs", "maptrans2", False)) {
866                 ops.trans2 = cvfs_trans2;
867         }
868
869         /* register ourselves with the NTVFS subsystem. We register
870            under the name 'cifs'. */
871         ret = ntvfs_register(&ops);
872
873         if (!NT_STATUS_IS_OK(ret)) {
874                 DEBUG(0,("Failed to register CIFS backend!\n"));
875         }
876         
877         return ret;
878 }