r4757: added the ability of the clisocket level of libcli to handle async
[jelmer/samba4-debian.git] / source / ntvfs / cifs / vfs_cifs.c
index 5b10e7241161d5eb74858ef1149ac085c680ecdf..1a5a5ac042a95cf358bc01eddcd5ca2df85dbe82 100644 (file)
 */
 
 #include "includes.h"
+#include "events.h"
+#include "libcli/raw/libcliraw.h"
+#include "smb_server/smb_server.h"
 
 /* this is stored in ntvfs_private */
 struct cvfs_private {
        struct smbcli_tree *tree;
        struct smbcli_transport *transport;
        struct smbsrv_tcon *tcon;
-       /*const struct ntvfs_ops *ops;*/
+       BOOL map_generic;
 };
 
 
@@ -42,6 +45,8 @@ struct async_info {
        void *parms;
 };
 
+#define SETUP_PID private->tree->session->pid = SVAL(req->in.hdr, HDR_PID)
+
 /*
   an idle function to cope with messages from the smbd client while 
   waiting for a reply from the server
@@ -73,7 +78,8 @@ static BOOL oplock_handler(struct smbcli_transport *transport, uint16_t tid, uin
  /*
   a handler for read events on a connection to a backend server
 */
-static void cifs_socket_handler(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags)
+static void cifs_socket_handler(struct event_context *ev, struct fd_event *fde, 
+                               struct timeval t, uint16_t flags)
 {
        struct cvfs_private *private = fde->private;
        struct smbsrv_tcon *tcon = private->tcon;
@@ -82,7 +88,7 @@ static void cifs_socket_handler(struct event_context *ev, struct fd_event *fde,
        
        if (!smbcli_transport_process(private->transport)) {
                /* the connection to our server is dead */
-               close_cnum(tcon);
+               talloc_free(tcon);
        }
 }
 
@@ -115,7 +121,7 @@ static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs,
                return NT_STATUS_INVALID_PARAMETER;
        }
        
-       private = talloc(req->tcon, sizeof(struct cvfs_private));
+       private = talloc_p(req->tcon, struct cvfs_private);
        if (!private) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -136,7 +142,7 @@ static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs,
        }
 
        private->transport = private->tree->session->transport;
-       private->tree->session->pid = SVAL(req->in.hdr, HDR_PID);
+       SETUP_PID;
        private->tcon = req->tcon;
 
        tcon->fs_type = talloc_strdup(tcon, "NTFS");
@@ -144,14 +150,16 @@ static NTSTATUS cvfs_connect(struct ntvfs_module_context *ntvfs,
        
        /* we need to receive oplock break requests from the server */
        smbcli_oplock_handler(private->transport, oplock_handler, private);
-       smbcli_transport_idle_handler(private->transport, idle_func, 1, private);
+       smbcli_transport_idle_handler(private->transport, idle_func, 50000, private);
 
-       private->transport->event.fde->handler = cifs_socket_handler;
-       private->transport->event.fde->private = private;
+       private->transport->socket->event.fde->handler = cifs_socket_handler;
+       private->transport->socket->event.fde->private = private;
 
-       private->transport->event.ctx = event_context_merge(tcon->smb_conn->connection->event.ctx,
-                                                           private->transport->event.ctx);
-       talloc_reference(private, private->transport->event.ctx);
+       private->transport->socket->event.ctx = event_context_merge(tcon->smb_conn->connection->event.ctx,
+                                                                   private->transport->socket->event.ctx);
+       talloc_reference(private, private->transport->socket->event.ctx);
+       private->map_generic = lp_parm_bool(req->tcon->service, 
+                                           "cifs", "mapgeneric", False);
 
        return NT_STATUS_OK;
 }
@@ -164,8 +172,7 @@ static NTSTATUS cvfs_disconnect(struct ntvfs_module_context *ntvfs,
 {
        struct cvfs_private *private = ntvfs->private_data;
 
-       smb_tree_disconnect(private->tree);
-       talloc_free(private->tree);
+       talloc_free(private);
 
        return NT_STATUS_OK;
 }
@@ -179,8 +186,8 @@ static void async_simple(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smbcli_request_simple_recv(c_req);
-       req->async.send_fn(req);
+       req->async_states->status = smbcli_request_simple_recv(c_req);
+       req->async_states->send_fn(req);
 }
 
 
@@ -196,7 +203,7 @@ static void async_simple(struct smbcli_request *c_req)
                c_req->async.private = async; \
        } \
        c_req->async.fn = async_fn; \
-       req->control_flags |= REQ_CONTROL_ASYNC; \
+       req->async_states->state |= NTVFS_ASYNC_STATE_ASYNC; \
        return NT_STATUS_OK; \
 } while (0)
 
@@ -207,14 +214,16 @@ static void async_simple(struct smbcli_request *c_req)
   The name can contain CIFS wildcards, but rarely does (except with OS/2 clients)
 */
 static NTSTATUS cvfs_unlink(struct ntvfs_module_context *ntvfs, 
-                               struct smbsrv_request *req, struct smb_unlink *unl)
+                           struct smbsrv_request *req, struct smb_unlink *unl)
 {
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
+       SETUP_PID;
+
        /* see if the front end will allow us to perform this
           function asynchronously.  */
-       if (!req->async.send_fn) {
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_unlink(private->tree, unl);
        }
 
@@ -230,8 +239,8 @@ static void async_ioctl(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smb_raw_ioctl_recv(c_req, req, async->parms);
-       req->async.send_fn(req);
+       req->async_states->status = smb_raw_ioctl_recv(c_req, req, async->parms);
+       req->async_states->send_fn(req);
 }
 
 /*
@@ -243,9 +252,11 @@ static NTSTATUS cvfs_ioctl(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
+       SETUP_PID;
+
        /* see if the front end will allow us to perform this
           function asynchronously.  */
-       if (!req->async.send_fn) {
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_ioctl(private->tree, req, io);
        }
 
@@ -263,7 +274,9 @@ static NTSTATUS cvfs_chkpath(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_chkpath(private->tree, cp);
        }
 
@@ -279,8 +292,8 @@ static void async_qpathinfo(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smb_raw_pathinfo_recv(c_req, req, async->parms);
-       req->async.send_fn(req);
+       req->async_states->status = smb_raw_pathinfo_recv(c_req, req, async->parms);
+       req->async_states->send_fn(req);
 }
 
 /*
@@ -292,7 +305,9 @@ static NTSTATUS cvfs_qpathinfo(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_pathinfo(private->tree, req, info);
        }
 
@@ -308,8 +323,8 @@ static void async_qfileinfo(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smb_raw_fileinfo_recv(c_req, req, async->parms);
-       req->async.send_fn(req);
+       req->async_states->status = smb_raw_fileinfo_recv(c_req, req, async->parms);
+       req->async_states->send_fn(req);
 }
 
 /*
@@ -321,7 +336,9 @@ static NTSTATUS cvfs_qfileinfo(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_fileinfo(private->tree, req, info);
        }
 
@@ -340,7 +357,9 @@ static NTSTATUS cvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_setpathinfo(private->tree, st);
        }
 
@@ -357,8 +376,8 @@ static void async_open(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smb_raw_open_recv(c_req, req, async->parms);
-       req->async.send_fn(req);
+       req->async_states->status = smb_raw_open_recv(c_req, req, async->parms);
+       req->async_states->send_fn(req);
 }
 
 /*
@@ -370,7 +389,14 @@ static NTSTATUS cvfs_open(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (io->generic.level != RAW_OPEN_GENERIC &&
+           private->map_generic) {
+               return ntvfs_map_open(req, io, ntvfs);
+       }
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_open(private->tree, req, io);
        }
 
@@ -388,7 +414,9 @@ static NTSTATUS cvfs_mkdir(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_mkdir(private->tree, md);
        }
 
@@ -406,7 +434,9 @@ static NTSTATUS cvfs_rmdir(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_rmdir(private->tree, rd);
        }
        c_req = smb_raw_rmdir_send(private->tree, rd);
@@ -423,7 +453,9 @@ static NTSTATUS cvfs_rename(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_rename(private->tree, ren);
        }
 
@@ -448,8 +480,8 @@ static void async_read(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smb_raw_read_recv(c_req, async->parms);
-       req->async.send_fn(req);
+       req->async_states->status = smb_raw_read_recv(c_req, async->parms);
+       req->async_states->send_fn(req);
 }
 
 /*
@@ -461,7 +493,14 @@ static NTSTATUS cvfs_read(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (rd->generic.level != RAW_READ_GENERIC &&
+           private->map_generic) {
+               return ntvfs_map_read(req, rd, ntvfs);
+       }
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_read(private->tree, rd);
        }
 
@@ -477,8 +516,8 @@ static void async_write(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smb_raw_write_recv(c_req, async->parms);
-       req->async.send_fn(req);
+       req->async_states->status = smb_raw_write_recv(c_req, async->parms);
+       req->async_states->send_fn(req);
 }
 
 /*
@@ -490,7 +529,14 @@ static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (wr->generic.level != RAW_WRITE_GENERIC &&
+           private->map_generic) {
+               return ntvfs_map_write(req, wr, ntvfs);
+       }
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_write(private->tree, wr);
        }
 
@@ -503,18 +549,40 @@ static NTSTATUS cvfs_write(struct ntvfs_module_context *ntvfs,
   seek in a file
 */
 static NTSTATUS cvfs_seek(struct ntvfs_module_context *ntvfs, 
-                               struct smbsrv_request *req, struct smb_seek *io)
+                         struct smbsrv_request *req, struct smb_seek *io)
 {
-       return NT_STATUS_NOT_SUPPORTED;
+       struct cvfs_private *private = ntvfs->private_data;
+       struct smbcli_request *c_req;
+
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
+               return smb_raw_seek(private->tree, io);
+       }
+
+       c_req = smb_raw_seek_send(private->tree, io);
+
+       SIMPLE_ASYNC_TAIL;
 }
 
 /*
   flush a file
 */
 static NTSTATUS cvfs_flush(struct ntvfs_module_context *ntvfs, 
-                               struct smbsrv_request *req, struct smb_flush *io)
+                          struct smbsrv_request *req, struct smb_flush *io)
 {
-       return NT_STATUS_OK;
+       struct cvfs_private *private = ntvfs->private_data;
+       struct smbcli_request *c_req;
+
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
+               return smb_raw_flush(private->tree, io);
+       }
+
+       c_req = smb_raw_flush_send(private->tree, io);
+
+       SIMPLE_ASYNC_TAIL;
 }
 
 /*
@@ -526,7 +594,14 @@ static NTSTATUS cvfs_close(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (io->generic.level != RAW_CLOSE_GENERIC &&
+           private->map_generic) {
+               return ntvfs_map_close(req, io, ntvfs);
+       }
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_close(private->tree, io);
        }
 
@@ -544,7 +619,9 @@ static NTSTATUS cvfs_exit(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_exit(private->tree->session);
        }
 
@@ -573,6 +650,15 @@ static NTSTATUS cvfs_async_setup(struct ntvfs_module_context *ntvfs,
        return NT_STATUS_OK;
 }
 
+/*
+  cancel an async call
+*/
+static NTSTATUS cvfs_cancel(struct ntvfs_module_context *ntvfs, 
+                           struct smbsrv_request *req)
+{
+       return NT_STATUS_NOT_IMPLEMENTED;
+}
+
 /*
   lock a byte range
 */
@@ -582,7 +668,14 @@ static NTSTATUS cvfs_lock(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (lck->generic.level != RAW_LOCK_GENERIC &&
+           private->map_generic) {
+               return ntvfs_map_lock(req, lck, ntvfs);
+       }
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_lock(private->tree, lck);
        }
 
@@ -600,7 +693,9 @@ static NTSTATUS cvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_setfileinfo(private->tree, info);
        }
        c_req = smb_raw_setfileinfo_send(private->tree, info);
@@ -616,8 +711,8 @@ static void async_fsinfo(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smb_raw_fsinfo_recv(c_req, req, async->parms);
-       req->async.send_fn(req);
+       req->async_states->status = smb_raw_fsinfo_recv(c_req, req, async->parms);
+       req->async_states->send_fn(req);
 }
 
 /*
@@ -629,7 +724,9 @@ static NTSTATUS cvfs_fsinfo(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_fsinfo(private->tree, req, fs);
        }
 
@@ -657,6 +754,8 @@ static NTSTATUS cvfs_search_first(struct ntvfs_module_context *ntvfs,
 {
        struct cvfs_private *private = ntvfs->private_data;
 
+       SETUP_PID;
+
        return smb_raw_search_first(private->tree, req, io, search_private, callback);
 }
 
@@ -668,6 +767,8 @@ static NTSTATUS cvfs_search_next(struct ntvfs_module_context *ntvfs,
 {
        struct cvfs_private *private = ntvfs->private_data;
 
+       SETUP_PID;
+
        return smb_raw_search_next(private->tree, req, io, search_private, callback);
 }
 
@@ -677,6 +778,8 @@ static NTSTATUS cvfs_search_close(struct ntvfs_module_context *ntvfs,
 {
        struct cvfs_private *private = ntvfs->private_data;
 
+       SETUP_PID;
+
        return smb_raw_search_close(private->tree, io);
 }
 
@@ -687,8 +790,8 @@ static void async_trans2(struct smbcli_request *c_req)
 {
        struct async_info *async = c_req->async.private;
        struct smbsrv_request *req = async->req;
-       req->async.status = smb_raw_trans2_recv(c_req, req, async->parms);
-       req->async.send_fn(req);
+       req->async_states->status = smb_raw_trans2_recv(c_req, req, async->parms);
+       req->async_states->send_fn(req);
 }
 
 /* raw trans2 */
@@ -698,7 +801,9 @@ static NTSTATUS cvfs_trans2(struct ntvfs_module_context *ntvfs,
        struct cvfs_private *private = ntvfs->private_data;
        struct smbcli_request *c_req;
 
-       if (!req->async.send_fn) {
+       SETUP_PID;
+
+       if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
                return smb_raw_trans2(private->tree, req, trans2);
        }
 
@@ -736,7 +841,7 @@ NTSTATUS ntvfs_cifs_init(void)
        ops.chkpath = cvfs_chkpath;
        ops.qpathinfo = cvfs_qpathinfo;
        ops.setpathinfo = cvfs_setpathinfo;
-       ops.open = cvfs_open;
+       ops.openfile = cvfs_open;
        ops.mkdir = cvfs_mkdir;
        ops.rmdir = cvfs_rmdir;
        ops.rename = cvfs_rename;
@@ -759,6 +864,7 @@ NTSTATUS ntvfs_cifs_init(void)
        ops.trans = cvfs_trans;
        ops.logoff = cvfs_logoff;
        ops.async_setup = cvfs_async_setup;
+       ops.cancel = cvfs_cancel;
 
        if (lp_parm_bool(-1, "cifs", "maptrans2", False)) {
                ops.trans2 = cvfs_trans2;
@@ -766,7 +872,7 @@ NTSTATUS ntvfs_cifs_init(void)
 
        /* register ourselves with the NTVFS subsystem. We register
           under the name 'cifs'. */
-       ret = register_backend("ntvfs", &ops);
+       ret = ntvfs_register(&ops);
 
        if (!NT_STATUS_IS_OK(ret)) {
                DEBUG(0,("Failed to register CIFS backend!\n"));