vfs_cifs: disable level2 oplocks if the frontend client doesn't support them
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22 /*
23   this implements a CIFS->CIFS NTVFS filesystem backend. 
24   
25 */
26
27 #include "includes.h"
28 #include "libcli/raw/libcliraw.h"
29 #include "libcli/smb_composite/smb_composite.h"
30 #include "auth/auth.h"
31 #include "auth/credentials/credentials.h"
32 #include "ntvfs/ntvfs.h"
33 #include "lib/util/dlinklist.h"
34 #include "param/param.h"
35 #include "libcli/resolve/resolve.h"
36
37 struct cvfs_file {
38         struct cvfs_file *prev, *next;
39         uint16_t fnum;
40         struct ntvfs_handle *h;
41 };
42
43 /* this is stored in ntvfs_private */
44 struct cvfs_private {
45         struct smbcli_tree *tree;
46         struct smbcli_transport *transport;
47         struct ntvfs_module_context *ntvfs;
48         struct async_info *pending;
49         struct cvfs_file *files;
50         bool map_generic;
51         bool map_trans2;
52 };
53
54
55 /* a structure used to pass information to an async handler */
56 struct async_info {
57         struct async_info *next, *prev;
58         struct cvfs_private *cvfs;
59         struct ntvfs_request *req;
60         struct smbcli_request *c_req;
61         struct cvfs_file *f;
62         void *parms;
63 };
64
65 #define SETUP_PID private->tree->session->pid = req->smbpid
66
67 #define SETUP_FILE_HERE(f) do { \
68         f = ntvfs_handle_get_backend_data(io->generic.in.file.ntvfs, ntvfs); \
69         if (!f) return NT_STATUS_INVALID_HANDLE; \
70         io->generic.in.file.fnum = f->fnum; \
71 } while (0)
72
73 #define SETUP_FILE do { \
74         struct cvfs_file *f; \
75         SETUP_FILE_HERE(f); \
76 } while (0)
77
78 #define SETUP_PID_AND_FILE do { \
79         SETUP_PID; \
80         SETUP_FILE; \
81 } while (0)
82
83 #define CIFS_SERVER             "cifs:server"
84 #define CIFS_USER               "cifs:user"
85 #define CIFS_PASSWORD           "cifs:password"
86 #define CIFS_DOMAIN             "cifs:domain"
87 #define CIFS_SHARE              "cifs:share"
88 #define CIFS_USE_MACHINE_ACCT   "cifs:use-machine-account"
89 #define CIFS_MAP_GENERIC        "cifs:map-generic"
90 #define CIFS_MAP_TRANS2         "cifs:map-trans2"
91
92 #define CIFS_USE_MACHINE_ACCT_DEFAULT   false
93 #define CIFS_MAP_GENERIC_DEFAULT        false
94 #define CIFS_MAP_TRANS2_DEFAULT         true
95
96 /*
97   a handler for oplock break events from the server - these need to be passed
98   along to the client
99  */
100 static bool oplock_handler(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *p_private)
101 {
102         struct cvfs_private *private = p_private;
103         NTSTATUS status;
104         struct ntvfs_handle *h = NULL;
105         struct cvfs_file *f;
106
107         for (f=private->files; f; f=f->next) {
108                 if (f->fnum != fnum) continue;
109                 h = f->h;
110                 break;
111         }
112
113         if (!h) {
114                 DEBUG(5,("vfs_cifs: ignoring oplock break level %d for fnum %d\n", level, fnum));
115                 return true;
116         }
117
118         DEBUG(5,("vfs_cifs: sending oplock break level %d for fnum %d\n", level, fnum));
119         status = ntvfs_send_oplock_break(private->ntvfs, h, level);
120         if (!NT_STATUS_IS_OK(status)) return false;
121         return true;
122 }
123
124 /*
125   connect to a share - used when a tree_connect operation comes in.
126 */
127 static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs, 
128                              struct ntvfs_request *req, const char *sharename)
129 {
130         NTSTATUS status;
131         struct cvfs_private *private;
132         const char *host, *user, *pass, *domain, *remote_share;
133         struct smb_composite_connect io;
134         struct composite_context *creq;
135         struct share_config *scfg = ntvfs->ctx->config;
136
137         struct cli_credentials *credentials;
138         bool machine_account;
139
140         /* Here we need to determine which server to connect to.
141          * For now we use parametric options, type cifs.
142          * Later we will use security=server and auth_server.c.
143          */
144         host = share_string_option(scfg, CIFS_SERVER, NULL);
145         user = share_string_option(scfg, CIFS_USER, NULL);
146         pass = share_string_option(scfg, CIFS_PASSWORD, NULL);
147         domain = share_string_option(scfg, CIFS_DOMAIN, NULL);
148         remote_share = share_string_option(scfg, CIFS_SHARE, NULL);
149         if (!remote_share) {
150                 remote_share = sharename;
151         }
152
153         machine_account = share_bool_option(scfg, CIFS_USE_MACHINE_ACCT, CIFS_USE_MACHINE_ACCT_DEFAULT);
154
155         private = talloc_zero(ntvfs, struct cvfs_private);
156         if (!private) {
157                 return NT_STATUS_NO_MEMORY;
158         }
159
160         ntvfs->private_data = private;
161
162         if (!host) {
163                 DEBUG(1,("CIFS backend: You must supply server\n"));
164                 return NT_STATUS_INVALID_PARAMETER;
165         } 
166         
167         if (user && pass) {
168                 DEBUG(5, ("CIFS backend: Using specified password\n"));
169                 credentials = cli_credentials_init(private);
170                 if (!credentials) {
171                         return NT_STATUS_NO_MEMORY;
172                 }
173                 cli_credentials_set_event_context(credentials, ntvfs->ctx->event_ctx);
174                 cli_credentials_set_conf(credentials, ntvfs->ctx->lp_ctx);
175                 cli_credentials_set_username(credentials, user, CRED_SPECIFIED);
176                 if (domain) {
177                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
178                 }
179                 cli_credentials_set_password(credentials, pass, CRED_SPECIFIED);
180         } else if (machine_account) {
181                 DEBUG(5, ("CIFS backend: Using machine account\n"));
182                 credentials = cli_credentials_init(private);
183                 cli_credentials_set_event_context(credentials, ntvfs->ctx->event_ctx);
184                 cli_credentials_set_conf(credentials, ntvfs->ctx->lp_ctx);
185                 if (domain) {
186                         cli_credentials_set_domain(credentials, domain, CRED_SPECIFIED);
187                 }
188                 status = cli_credentials_set_machine_account(credentials, ntvfs->ctx->lp_ctx);
189                 if (!NT_STATUS_IS_OK(status)) {
190                         return status;
191                 }
192         } else if (req->session_info->credentials) {
193                 DEBUG(5, ("CIFS backend: Using delegated credentials\n"));
194                 credentials = req->session_info->credentials;
195         } else {
196                 DEBUG(1,("CIFS backend: NO delegated credentials found: You must supply server, user and password or the client must supply delegated credentials\n"));
197                 return NT_STATUS_INVALID_PARAMETER;
198         }
199
200         /* connect to the server, using the smbd event context */
201         io.in.dest_host = host;
202         io.in.dest_ports = lp_smb_ports(ntvfs->ctx->lp_ctx);
203         io.in.called_name = host;
204         io.in.credentials = credentials;
205         io.in.fallback_to_anonymous = false;
206         io.in.workgroup = lp_workgroup(ntvfs->ctx->lp_ctx);
207         io.in.service = remote_share;
208         io.in.service_type = "?????";
209         lp_smbcli_options(ntvfs->ctx->lp_ctx, &io.in.options);
210
211         if (!(ntvfs->ctx->client_caps & NTVFS_CLIENT_CAP_LEVEL_II_OPLOCKS)) {
212                 io.in.options.use_level2_oplocks = false;
213         }
214
215         creq = smb_composite_connect_send(&io, private, 
216                                           lp_resolve_context(ntvfs->ctx->lp_ctx),
217                                           ntvfs->ctx->event_ctx);
218         status = smb_composite_connect_recv(creq, private);
219         NT_STATUS_NOT_OK_RETURN(status);
220
221         private->tree = io.out.tree;
222
223         private->transport = private->tree->session->transport;
224         SETUP_PID;
225         private->ntvfs = ntvfs;
226
227         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
228         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
229         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
230         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
231
232         /* we need to receive oplock break requests from the server */
233         smbcli_oplock_handler(private->transport, oplock_handler, private);
234
235         private->map_generic = share_bool_option(scfg, CIFS_MAP_GENERIC, CIFS_MAP_GENERIC_DEFAULT);
236
237         private->map_trans2 = share_bool_option(scfg, CIFS_MAP_TRANS2, CIFS_MAP_TRANS2_DEFAULT);
238
239         return NT_STATUS_OK;
240 }
241
242 /*
243   disconnect from a share
244 */
245 static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs)
246 {
247         struct cvfs_private *private = ntvfs->private_data;
248         struct async_info *a, *an;
249
250         /* first cleanup pending requests */
251         for (a=private->pending; a; a = an) {
252                 an = a->next;
253                 smbcli_request_destroy(a->c_req);
254                 talloc_free(a);
255         }
256
257         talloc_free(private);
258         ntvfs->private_data = NULL;
259
260         return NT_STATUS_OK;
261 }
262
263 /*
264   destroy an async info structure
265 */
266 static int async_info_destructor(struct async_info *async)
267 {
268         DLIST_REMOVE(async->cvfs->pending, async);
269         return 0;
270 }
271
272 /*
273   a handler for simple async replies
274   this handler can only be used for functions that don't return any
275   parameters (those that just return a status code)
276  */
277 static void async_simple(struct smbcli_request *c_req)
278 {
279         struct async_info *async = c_req->async.private;
280         struct ntvfs_request *req = async->req;
281         req->async_states->status = smbcli_request_simple_recv(c_req);
282         talloc_free(async);
283         req->async_states->send_fn(req);
284 }
285
286
287 /* save some typing for the simple functions */
288 #define ASYNC_RECV_TAIL_F(io, async_fn, file) do { \
289         if (!c_req) return NT_STATUS_UNSUCCESSFUL; \
290         { \
291                 struct async_info *async; \
292                 async = talloc(req, struct async_info); \
293                 if (!async) return NT_STATUS_NO_MEMORY; \
294                 async->parms = io; \
295                 async->req = req; \
296                 async->f = file; \
297                 async->cvfs = private; \
298                 async->c_req = c_req; \
299                 DLIST_ADD(private->pending, async); \
300                 c_req->async.private = async; \
301                 talloc_set_destructor(async, async_info_destructor); \
302         } \
303         c_req->async.fn = async_fn; \
304         req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
305         return NT_STATUS_OK; \
306 } while (0)
307
308 #define ASYNC_RECV_TAIL(io, async_fn) ASYNC_RECV_TAIL_F(io, async_fn, NULL)
309
310 #define SIMPLE_ASYNC_TAIL ASYNC_RECV_TAIL(NULL, async_simple)
311
312 /*
313   delete a file - the dirtype specifies the file types to include in the search. 
314   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
315 */
316 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs, 
317                             struct ntvfs_request *req, union smb_unlink *unl)
318 {
319         struct cvfs_private *private = ntvfs->private_data;
320         struct smbcli_request *c_req;
321
322         SETUP_PID;
323
324         /* see if the front end will allow us to perform this
325            function asynchronously.  */
326         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
327                 return smb_raw_unlink(private->tree, unl);
328         }
329
330         c_req = smb_raw_unlink_send(private->tree, unl);
331
332         SIMPLE_ASYNC_TAIL;
333 }
334
335 /*
336   a handler for async ioctl replies
337  */
338 static void async_ioctl(struct smbcli_request *c_req)
339 {
340         struct async_info *async = c_req->async.private;
341         struct ntvfs_request *req = async->req;
342         req->async_states->status = smb_raw_ioctl_recv(c_req, req, async->parms);
343         talloc_free(async);
344         req->async_states->send_fn(req);
345 }
346
347 /*
348   ioctl interface
349 */
350 static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs, 
351                            struct ntvfs_request *req, union smb_ioctl *io)
352 {
353         struct cvfs_private *private = ntvfs->private_data;
354         struct smbcli_request *c_req;
355
356         SETUP_PID_AND_FILE;
357
358         /* see if the front end will allow us to perform this
359            function asynchronously.  */
360         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
361                 return smb_raw_ioctl(private->tree, req, io);
362         }
363
364         c_req = smb_raw_ioctl_send(private->tree, io);
365
366         ASYNC_RECV_TAIL(io, async_ioctl);
367 }
368
369 /*
370   check if a directory exists
371 */
372 static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs, 
373                              struct ntvfs_request *req, union smb_chkpath *cp)
374 {
375         struct cvfs_private *private = ntvfs->private_data;
376         struct smbcli_request *c_req;
377
378         SETUP_PID;
379
380         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
381                 return smb_raw_chkpath(private->tree, cp);
382         }
383
384         c_req = smb_raw_chkpath_send(private->tree, cp);
385
386         SIMPLE_ASYNC_TAIL;
387 }
388
389 /*
390   a handler for async qpathinfo replies
391  */
392 static void async_qpathinfo(struct smbcli_request *c_req)
393 {
394         struct async_info *async = c_req->async.private;
395         struct ntvfs_request *req = async->req;
396         req->async_states->status = smb_raw_pathinfo_recv(c_req, req, async->parms);
397         talloc_free(async);
398         req->async_states->send_fn(req);
399 }
400
401 /*
402   return info on a pathname
403 */
404 static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs, 
405                                struct ntvfs_request *req, union smb_fileinfo *info)
406 {
407         struct cvfs_private *private = ntvfs->private_data;
408         struct smbcli_request *c_req;
409
410         SETUP_PID;
411
412         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
413                 return smb_raw_pathinfo(private->tree, req, info);
414         }
415
416         c_req = smb_raw_pathinfo_send(private->tree, info);
417
418         ASYNC_RECV_TAIL(info, async_qpathinfo);
419 }
420
421 /*
422   a handler for async qfileinfo replies
423  */
424 static void async_qfileinfo(struct smbcli_request *c_req)
425 {
426         struct async_info *async = c_req->async.private;
427         struct ntvfs_request *req = async->req;
428         req->async_states->status = smb_raw_fileinfo_recv(c_req, req, async->parms);
429         talloc_free(async);
430         req->async_states->send_fn(req);
431 }
432
433 /*
434   query info on a open file
435 */
436 static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs, 
437                                struct ntvfs_request *req, union smb_fileinfo *io)
438 {
439         struct cvfs_private *private = ntvfs->private_data;
440         struct smbcli_request *c_req;
441
442         SETUP_PID_AND_FILE;
443
444         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
445                 return smb_raw_fileinfo(private->tree, req, io);
446         }
447
448         c_req = smb_raw_fileinfo_send(private->tree, io);
449
450         ASYNC_RECV_TAIL(io, async_qfileinfo);
451 }
452
453
454 /*
455   set info on a pathname
456 */
457 static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs, 
458                                  struct ntvfs_request *req, union smb_setfileinfo *st)
459 {
460         struct cvfs_private *private = ntvfs->private_data;
461         struct smbcli_request *c_req;
462
463         SETUP_PID;
464
465         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
466                 return smb_raw_setpathinfo(private->tree, st);
467         }
468
469         c_req = smb_raw_setpathinfo_send(private->tree, st);
470
471         SIMPLE_ASYNC_TAIL;
472 }
473
474
475 /*
476   a handler for async open replies
477  */
478 static void async_open(struct smbcli_request *c_req)
479 {
480         struct async_info *async = c_req->async.private;
481         struct cvfs_private *cvfs = async->cvfs;
482         struct ntvfs_request *req = async->req;
483         struct cvfs_file *f = async->f;
484         union smb_open *io = async->parms;
485         union smb_handle *file;
486         talloc_free(async);
487         req->async_states->status = smb_raw_open_recv(c_req, req, io);
488         SMB_OPEN_OUT_FILE(io, file);
489         f->fnum = file->fnum;
490         file->ntvfs = NULL;
491         if (!NT_STATUS_IS_OK(req->async_states->status)) goto failed;
492         req->async_states->status = ntvfs_handle_set_backend_data(f->h, cvfs->ntvfs, f);
493         if (!NT_STATUS_IS_OK(req->async_states->status)) goto failed;
494         file->ntvfs = f->h;
495         DLIST_ADD(cvfs->files, f);
496 failed:
497         req->async_states->send_fn(req);
498 }
499
500 /*
501   open a file
502 */
503 static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs, 
504                           struct ntvfs_request *req, union smb_open *io)
505 {
506         struct cvfs_private *private = ntvfs->private_data;
507         struct smbcli_request *c_req;
508         struct ntvfs_handle *h;
509         struct cvfs_file *f;
510         NTSTATUS status;
511
512         SETUP_PID;
513
514         if (io->generic.level != RAW_OPEN_GENERIC &&
515             private->map_generic) {
516                 return ntvfs_map_open(ntvfs, req, io);
517         }
518
519         status = ntvfs_handle_new(ntvfs, req, &h);
520         NT_STATUS_NOT_OK_RETURN(status);
521
522         f = talloc_zero(h, struct cvfs_file);
523         NT_STATUS_HAVE_NO_MEMORY(f);
524         f->h = h;
525
526         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
527                 union smb_handle *file;
528
529                 status = smb_raw_open(private->tree, req, io);
530                 NT_STATUS_NOT_OK_RETURN(status);
531
532                 SMB_OPEN_OUT_FILE(io, file);
533                 f->fnum = file->fnum;
534                 file->ntvfs = NULL;
535                 status = ntvfs_handle_set_backend_data(f->h, private->ntvfs, f);
536                 NT_STATUS_NOT_OK_RETURN(status);
537                 file->ntvfs = f->h;
538                 DLIST_ADD(private->files, f);
539
540                 return NT_STATUS_OK;
541         }
542
543         c_req = smb_raw_open_send(private->tree, io);
544
545         ASYNC_RECV_TAIL_F(io, async_open, f);
546 }
547
548 /*
549   create a directory
550 */
551 static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs, 
552                            struct ntvfs_request *req, union smb_mkdir *md)
553 {
554         struct cvfs_private *private = ntvfs->private_data;
555         struct smbcli_request *c_req;
556
557         SETUP_PID;
558
559         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
560                 return smb_raw_mkdir(private->tree, md);
561         }
562
563         c_req = smb_raw_mkdir_send(private->tree, md);
564
565         SIMPLE_ASYNC_TAIL;
566 }
567
568 /*
569   remove a directory
570 */
571 static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs, 
572                            struct ntvfs_request *req, struct smb_rmdir *rd)
573 {
574         struct cvfs_private *private = ntvfs->private_data;
575         struct smbcli_request *c_req;
576
577         SETUP_PID;
578
579         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
580                 return smb_raw_rmdir(private->tree, rd);
581         }
582         c_req = smb_raw_rmdir_send(private->tree, rd);
583
584         SIMPLE_ASYNC_TAIL;
585 }
586
587 /*
588   rename a set of files
589 */
590 static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs, 
591                             struct ntvfs_request *req, union smb_rename *ren)
592 {
593         struct cvfs_private *private = ntvfs->private_data;
594         struct smbcli_request *c_req;
595
596         SETUP_PID;
597
598         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
599                 return smb_raw_rename(private->tree, ren);
600         }
601
602         c_req = smb_raw_rename_send(private->tree, ren);
603
604         SIMPLE_ASYNC_TAIL;
605 }
606
607 /*
608   copy a set of files
609 */
610 static NTSTATUS cvfs_copy(struct ntvfs_module_context *ntvfs, 
611                           struct ntvfs_request *req, struct smb_copy *cp)
612 {
613         return NT_STATUS_NOT_SUPPORTED;
614 }
615
616 /*
617   a handler for async read replies
618  */
619 static void async_read(struct smbcli_request *c_req)
620 {
621         struct async_info *async = c_req->async.private;
622         struct ntvfs_request *req = async->req;
623         req->async_states->status = smb_raw_read_recv(c_req, async->parms);
624         talloc_free(async);
625         req->async_states->send_fn(req);
626 }
627
628 /*
629   read from a file
630 */
631 static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs, 
632                           struct ntvfs_request *req, union smb_read *io)
633 {
634         struct cvfs_private *private = ntvfs->private_data;
635         struct smbcli_request *c_req;
636
637         SETUP_PID;
638
639         if (io->generic.level != RAW_READ_GENERIC &&
640             private->map_generic) {
641                 return ntvfs_map_read(ntvfs, req, io);
642         }
643
644         SETUP_FILE;
645
646         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
647                 return smb_raw_read(private->tree, io);
648         }
649
650         c_req = smb_raw_read_send(private->tree, io);
651
652         ASYNC_RECV_TAIL(io, async_read);
653 }
654
655 /*
656   a handler for async write replies
657  */
658 static void async_write(struct smbcli_request *c_req)
659 {
660         struct async_info *async = c_req->async.private;
661         struct ntvfs_request *req = async->req;
662         req->async_states->status = smb_raw_write_recv(c_req, async->parms);
663         talloc_free(async);
664         req->async_states->send_fn(req);
665 }
666
667 /*
668   write to a file
669 */
670 static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs, 
671                            struct ntvfs_request *req, union smb_write *io)
672 {
673         struct cvfs_private *private = ntvfs->private_data;
674         struct smbcli_request *c_req;
675
676         SETUP_PID;
677
678         if (io->generic.level != RAW_WRITE_GENERIC &&
679             private->map_generic) {
680                 return ntvfs_map_write(ntvfs, req, io);
681         }
682         SETUP_FILE;
683
684         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
685                 return smb_raw_write(private->tree, io);
686         }
687
688         c_req = smb_raw_write_send(private->tree, io);
689
690         ASYNC_RECV_TAIL(io, async_write);
691 }
692
693 /*
694   a handler for async seek replies
695  */
696 static void async_seek(struct smbcli_request *c_req)
697 {
698         struct async_info *async = c_req->async.private;
699         struct ntvfs_request *req = async->req;
700         req->async_states->status = smb_raw_seek_recv(c_req, async->parms);
701         talloc_free(async);
702         req->async_states->send_fn(req);
703 }
704
705 /*
706   seek in a file
707 */
708 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs, 
709                           struct ntvfs_request *req,
710                           union smb_seek *io)
711 {
712         struct cvfs_private *private = ntvfs->private_data;
713         struct smbcli_request *c_req;
714
715         SETUP_PID_AND_FILE;
716
717         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
718                 return smb_raw_seek(private->tree, io);
719         }
720
721         c_req = smb_raw_seek_send(private->tree, io);
722
723         ASYNC_RECV_TAIL(io, async_seek);
724 }
725
726 /*
727   flush a file
728 */
729 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs, 
730                            struct ntvfs_request *req,
731                            union smb_flush *io)
732 {
733         struct cvfs_private *private = ntvfs->private_data;
734         struct smbcli_request *c_req;
735
736         SETUP_PID;
737         switch (io->generic.level) {
738         case RAW_FLUSH_FLUSH:
739                 SETUP_FILE;
740                 break;
741         case RAW_FLUSH_ALL:
742                 io->generic.in.file.fnum = 0xFFFF;
743                 break;
744         case RAW_FLUSH_SMB2:
745                 return NT_STATUS_INVALID_LEVEL;
746         }
747
748         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
749                 return smb_raw_flush(private->tree, io);
750         }
751
752         c_req = smb_raw_flush_send(private->tree, io);
753
754         SIMPLE_ASYNC_TAIL;
755 }
756
757 /*
758   close a file
759 */
760 static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs, 
761                            struct ntvfs_request *req, union smb_close *io)
762 {
763         struct cvfs_private *private = ntvfs->private_data;
764         struct smbcli_request *c_req;
765         struct cvfs_file *f;
766
767         SETUP_PID;
768
769         if (io->generic.level != RAW_CLOSE_GENERIC &&
770             private->map_generic) {
771                 return ntvfs_map_close(ntvfs, req, io);
772         }
773         SETUP_FILE_HERE(f);
774         /* Note, we aren't free-ing f, or it's h here. Should we?
775            even if file-close fails, we'll remove it from the list,
776            what else would we do? Maybe we should not remove until
777            after the proxied call completes? */
778         DLIST_REMOVE(private->files, f);
779
780         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
781                 return smb_raw_close(private->tree, io);
782         }
783
784         c_req = smb_raw_close_send(private->tree, io);
785
786         SIMPLE_ASYNC_TAIL;
787 }
788
789 /*
790   exit - closing files open by the pid
791 */
792 static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs, 
793                           struct ntvfs_request *req)
794 {
795         struct cvfs_private *private = ntvfs->private_data;
796         struct smbcli_request *c_req;
797
798         SETUP_PID;
799
800         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
801                 return smb_raw_exit(private->tree->session);
802         }
803
804         c_req = smb_raw_exit_send(private->tree->session);
805
806         SIMPLE_ASYNC_TAIL;
807 }
808
809 /*
810   logoff - closing files open by the user
811 */
812 static NTSTATUS cvfs_logoff(struct ntvfs_module_context *ntvfs, 
813                             struct ntvfs_request *req)
814 {
815         /* we can't do this right in the cifs backend .... */
816         return NT_STATUS_OK;
817 }
818
819 /*
820   setup for an async call - nothing to do yet
821 */
822 static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs, 
823                                  struct ntvfs_request *req, 
824                                  void *private)
825 {
826         return NT_STATUS_OK;
827 }
828
829 /*
830   cancel an async call
831 */
832 static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs, 
833                             struct ntvfs_request *req)
834 {
835         struct cvfs_private *private = ntvfs->private_data;
836         struct async_info *a;
837
838         /* find the matching request */
839         for (a=private->pending;a;a=a->next) {
840                 if (a->req == req) {
841                         break;
842                 }
843         }
844
845         if (a == NULL) {
846                 return NT_STATUS_INVALID_PARAMETER;
847         }
848
849         return smb_raw_ntcancel(a->c_req);
850 }
851
852 /*
853   lock a byte range
854 */
855 static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs, 
856                           struct ntvfs_request *req, union smb_lock *io)
857 {
858         struct cvfs_private *private = ntvfs->private_data;
859         struct smbcli_request *c_req;
860
861         SETUP_PID;
862
863         if (io->generic.level != RAW_LOCK_GENERIC &&
864             private->map_generic) {
865                 return ntvfs_map_lock(ntvfs, req, io);
866         }
867         SETUP_FILE;
868
869         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
870                 return smb_raw_lock(private->tree, io);
871         }
872
873         c_req = smb_raw_lock_send(private->tree, io);
874         SIMPLE_ASYNC_TAIL;
875 }
876
877 /*
878   set info on a open file
879 */
880 static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs, 
881                                  struct ntvfs_request *req, 
882                                  union smb_setfileinfo *io)
883 {
884         struct cvfs_private *private = ntvfs->private_data;
885         struct smbcli_request *c_req;
886
887         SETUP_PID_AND_FILE;
888
889         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
890                 return smb_raw_setfileinfo(private->tree, io);
891         }
892         c_req = smb_raw_setfileinfo_send(private->tree, io);
893
894         SIMPLE_ASYNC_TAIL;
895 }
896
897
898 /*
899   a handler for async fsinfo replies
900  */
901 static void async_fsinfo(struct smbcli_request *c_req)
902 {
903         struct async_info *async = c_req->async.private;
904         struct ntvfs_request *req = async->req;
905         req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
906         talloc_free(async);
907         req->async_states->send_fn(req);
908 }
909
910 /*
911   return filesystem space info
912 */
913 static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs, 
914                             struct ntvfs_request *req, union smb_fsinfo *fs)
915 {
916         struct cvfs_private *private = ntvfs->private_data;
917         struct smbcli_request *c_req;
918
919         SETUP_PID;
920
921         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
922                 return smb_raw_fsinfo(private->tree, req, fs);
923         }
924
925         c_req = smb_raw_fsinfo_send(private->tree, req, fs);
926
927         ASYNC_RECV_TAIL(fs, async_fsinfo);
928 }
929
930 /*
931   return print queue info
932 */
933 static NTSTATUS cvfs_lpq(struct ntvfs_module_context *ntvfs, 
934                          struct ntvfs_request *req, union smb_lpq *lpq)
935 {
936         return NT_STATUS_NOT_SUPPORTED;
937 }
938
939 /* 
940    list files in a directory matching a wildcard pattern
941 */
942 static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs, 
943                                   struct ntvfs_request *req, union smb_search_first *io, 
944                                   void *search_private, 
945                                   bool (*callback)(void *, const union smb_search_data *))
946 {
947         struct cvfs_private *private = ntvfs->private_data;
948
949         SETUP_PID;
950
951         return smb_raw_search_first(private->tree, req, io, search_private, callback);
952 }
953
954 /* continue a search */
955 static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs, 
956                                  struct ntvfs_request *req, union smb_search_next *io, 
957                                  void *search_private, 
958                                  bool (*callback)(void *, const union smb_search_data *))
959 {
960         struct cvfs_private *private = ntvfs->private_data;
961
962         SETUP_PID;
963
964         return smb_raw_search_next(private->tree, req, io, search_private, callback);
965 }
966
967 /* close a search */
968 static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs, 
969                                   struct ntvfs_request *req, union smb_search_close *io)
970 {
971         struct cvfs_private *private = ntvfs->private_data;
972
973         SETUP_PID;
974
975         return smb_raw_search_close(private->tree, io);
976 }
977
978 /*
979   a handler for async trans2 replies
980  */
981 static void async_trans2(struct smbcli_request *c_req)
982 {
983         struct async_info *async = c_req->async.private;
984         struct ntvfs_request *req = async->req;
985         req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
986         talloc_free(async);
987         req->async_states->send_fn(req);
988 }
989
990 /* raw trans2 */
991 static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs, 
992                             struct ntvfs_request *req,
993                             struct smb_trans2 *trans2)
994 {
995         struct cvfs_private *private = ntvfs->private_data;
996         struct smbcli_request *c_req;
997
998         if (private->map_trans2) {
999                 return NT_STATUS_NOT_IMPLEMENTED;
1000         }
1001
1002         SETUP_PID;
1003
1004         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1005                 return smb_raw_trans2(private->tree, req, trans2);
1006         }
1007
1008         c_req = smb_raw_trans2_send(private->tree, trans2);
1009
1010         ASYNC_RECV_TAIL(trans2, async_trans2);
1011 }
1012
1013
1014 /* SMBtrans - not used on file shares */
1015 static NTSTATUS cvfs_trans(struct ntvfs_module_context *ntvfs, 
1016                            struct ntvfs_request *req,
1017                            struct smb_trans2 *trans2)
1018 {
1019         return NT_STATUS_ACCESS_DENIED;
1020 }
1021
1022 /*
1023   a handler for async change notify replies
1024  */
1025 static void async_changenotify(struct smbcli_request *c_req)
1026 {
1027         struct async_info *async = c_req->async.private;
1028         struct ntvfs_request *req = async->req;
1029         req->async_states->status = smb_raw_changenotify_recv(c_req, req, async->parms);
1030         talloc_free(async);
1031         req->async_states->send_fn(req);
1032 }
1033
1034 /* change notify request - always async */
1035 static NTSTATUS cvfs_notify(struct ntvfs_module_context *ntvfs, 
1036                             struct ntvfs_request *req,
1037                             union smb_notify *io)
1038 {
1039         struct cvfs_private *private = ntvfs->private_data;
1040         struct smbcli_request *c_req;
1041         int saved_timeout = private->transport->options.request_timeout;
1042         struct cvfs_file *f;
1043
1044         if (io->nttrans.level != RAW_NOTIFY_NTTRANS) {
1045                 return NT_STATUS_NOT_IMPLEMENTED;
1046         }
1047
1048         SETUP_PID;
1049
1050         f = ntvfs_handle_get_backend_data(io->nttrans.in.file.ntvfs, ntvfs);
1051         if (!f) return NT_STATUS_INVALID_HANDLE;
1052         io->nttrans.in.file.fnum = f->fnum;
1053
1054         /* this request doesn't make sense unless its async */
1055         if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
1056                 return NT_STATUS_INVALID_PARAMETER;
1057         }
1058
1059         /* we must not timeout on notify requests - they wait
1060            forever */
1061         private->transport->options.request_timeout = 0;
1062
1063         c_req = smb_raw_changenotify_send(private->tree, io);
1064
1065         private->transport->options.request_timeout = saved_timeout;
1066
1067         ASYNC_RECV_TAIL(io, async_changenotify);
1068 }
1069
1070 /*
1071   initialise the CIFS->CIFS backend, registering ourselves with the ntvfs subsystem
1072  */
1073 NTSTATUS ntvfs_cifs_init(void)
1074 {
1075         NTSTATUS ret;
1076         struct ntvfs_ops ops;
1077         NTVFS_CURRENT_CRITICAL_SIZES(vers);
1078
1079         ZERO_STRUCT(ops);
1080
1081         /* fill in the name and type */
1082         ops.name = "cifs";
1083         ops.type = NTVFS_DISK;
1084         
1085         /* fill in all the operations */
1086         ops.connect = cvfs_connect;
1087         ops.disconnect = cvfs_disconnect;
1088         ops.unlink = cvfs_unlink;
1089         ops.chkpath = cvfs_chkpath;
1090         ops.qpathinfo = cvfs_qpathinfo;
1091         ops.setpathinfo = cvfs_setpathinfo;
1092         ops.open = cvfs_open;
1093         ops.mkdir = cvfs_mkdir;
1094         ops.rmdir = cvfs_rmdir;
1095         ops.rename = cvfs_rename;
1096         ops.copy = cvfs_copy;
1097         ops.ioctl = cvfs_ioctl;
1098         ops.read = cvfs_read;
1099         ops.write = cvfs_write;
1100         ops.seek = cvfs_seek;
1101         ops.flush = cvfs_flush; 
1102         ops.close = cvfs_close;
1103         ops.exit = cvfs_exit;
1104         ops.lock = cvfs_lock;
1105         ops.setfileinfo = cvfs_setfileinfo;
1106         ops.qfileinfo = cvfs_qfileinfo;
1107         ops.fsinfo = cvfs_fsinfo;
1108         ops.lpq = cvfs_lpq;
1109         ops.search_first = cvfs_search_first;
1110         ops.search_next = cvfs_search_next;
1111         ops.search_close = cvfs_search_close;
1112         ops.trans = cvfs_trans;
1113         ops.logoff = cvfs_logoff;
1114         ops.async_setup = cvfs_async_setup;
1115         ops.cancel = cvfs_cancel;
1116         ops.notify = cvfs_notify;
1117         ops.trans2 = cvfs_trans2;
1118
1119         /* register ourselves with the NTVFS subsystem. We register
1120            under the name 'cifs'. */
1121         ret = ntvfs_register(&ops, &vers);
1122
1123         if (!NT_STATUS_IS_OK(ret)) {
1124                 DEBUG(0,("Failed to register CIFS backend!\n"));
1125         }
1126         
1127         return ret;
1128 }