lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[metze/samba-autobuild/.git] / source3 / modules / vfs_default.c
index 087fb446feb13ff93ad97ad8ffa5ba260aaa7f27..d660120561024b195f622e142456e55f4cb17dc2 100644 (file)
 #include "source3/include/msdfs.h"
 #include "librpc/gen_ndr/ndr_dfsblobs.h"
 #include "lib/util/tevent_unix.h"
-#include "lib/asys/asys.h"
 #include "lib/util/tevent_ntstatus.h"
 #include "lib/util/sys_rw.h"
+#include "lib/pthreadpool/pthreadpool_tevent.h"
+#include "librpc/gen_ndr/ndr_ioctl.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_VFS
@@ -705,65 +706,33 @@ static ssize_t vfswrap_pwrite(vfs_handle_struct *handle, files_struct *fsp, cons
        return result;
 }
 
-static void vfswrap_asys_finished(struct tevent_context *ev,
-                                 struct tevent_fd *fde,
-                                 uint16_t flags, void *p);
-
-static bool vfswrap_init_asys_ctx(struct smbd_server_connection *conn)
+static int vfswrap_init_pool(struct smbd_server_connection *conn)
 {
-       struct asys_context *ctx;
-       struct tevent_fd *fde;
        int ret;
-       int fd;
-
-       if (conn->asys_ctx != NULL) {
-               return true;
-       }
-
-       ret = asys_context_init(&ctx, lp_aio_max_threads());
-       if (ret != 0) {
-               DEBUG(1, ("asys_context_init failed: %s\n", strerror(ret)));
-               return false;
-       }
-
-       fd = asys_signalfd(ctx);
 
-       ret = set_blocking(fd, false);
-       if (ret != 0) {
-               DBG_WARNING("set_blocking failed: %s\n", strerror(errno));
-               goto fail;
-       }
-
-       fde = tevent_add_fd(conn->ev_ctx, conn, fd, TEVENT_FD_READ,
-                           vfswrap_asys_finished, ctx);
-       if (fde == NULL) {
-               DEBUG(1, ("tevent_add_fd failed\n"));
-               goto fail;
+       if (conn->pool != NULL) {
+               return 0;
        }
 
-       conn->asys_ctx = ctx;
-       conn->asys_fde = fde;
-       return true;
-
-fail:
-       asys_context_destroy(ctx);
-       return false;
+       ret = pthreadpool_tevent_init(conn, lp_aio_max_threads(),
+                                     &conn->pool);
+       return ret;
 }
 
-struct vfswrap_asys_state {
-       struct asys_context *asys_ctx;
-       struct tevent_req *req;
+struct vfswrap_pread_state {
        ssize_t ret;
+       int err;
+       int fd;
+       void *buf;
+       size_t count;
+       off_t offset;
+
        struct vfs_aio_state vfs_aio_state;
-       SMBPROFILE_BASIC_ASYNC_STATE(profile_basic);
        SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
 };
 
-static int vfswrap_asys_state_destructor(struct vfswrap_asys_state *s)
-{
-       asys_cancel(s->asys_ctx, s->req);
-       return 0;
-}
+static void vfs_pread_do(void *private_data);
+static void vfs_pread_done(struct tevent_req *subreq);
 
 static struct tevent_req *vfswrap_pread_send(struct vfs_handle_struct *handle,
                                             TALLOC_CTX *mem_ctx,
@@ -772,33 +741,115 @@ static struct tevent_req *vfswrap_pread_send(struct vfs_handle_struct *handle,
                                             void *data,
                                             size_t n, off_t offset)
 {
-       struct tevent_req *req;
-       struct vfswrap_asys_state *state;
+       struct tevent_req *req, *subreq;
+       struct vfswrap_pread_state *state;
        int ret;
 
-       req = tevent_req_create(mem_ctx, &state, struct vfswrap_asys_state);
+       req = tevent_req_create(mem_ctx, &state, struct vfswrap_pread_state);
        if (req == NULL) {
                return NULL;
        }
-       if (!vfswrap_init_asys_ctx(handle->conn->sconn)) {
-               tevent_req_oom(req);
+
+       ret = vfswrap_init_pool(handle->conn->sconn);
+       if (tevent_req_error(req, ret)) {
                return tevent_req_post(req, ev);
        }
-       state->asys_ctx = handle->conn->sconn->asys_ctx;
-       state->req = req;
+
+       state->ret = -1;
+       state->fd = fsp->fh->fd;
+       state->buf = data;
+       state->count = n;
+       state->offset = offset;
 
        SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pread, profile_p,
                                     state->profile_bytes, n);
-       ret = asys_pread(state->asys_ctx, fsp->fh->fd, data, n, offset, req);
-       if (ret != 0) {
-               tevent_req_error(req, ret);
+       SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
+
+       subreq = pthreadpool_tevent_job_send(
+               state, ev, handle->conn->sconn->pool,
+               vfs_pread_do, state);
+       if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       talloc_set_destructor(state, vfswrap_asys_state_destructor);
+       tevent_req_set_callback(subreq, vfs_pread_done, req);
 
        return req;
 }
 
+static void vfs_pread_do(void *private_data)
+{
+       struct vfswrap_pread_state *state = talloc_get_type_abort(
+               private_data, struct vfswrap_pread_state);
+       struct timespec start_time;
+       struct timespec end_time;
+
+       SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
+
+       PROFILE_TIMESTAMP(&start_time);
+
+       do {
+               state->ret = pread(state->fd, state->buf, state->count,
+                                  state->offset);
+       } while ((state->ret == -1) && (errno == EINTR));
+
+       state->err = errno;
+
+       PROFILE_TIMESTAMP(&end_time);
+
+       state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
+
+       SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
+}
+
+static void vfs_pread_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+#ifdef WITH_PROFILE
+       struct vfswrap_pread_state *state = tevent_req_data(
+               req, struct vfswrap_pread_state);
+#endif
+       int ret;
+
+       ret = pthreadpool_tevent_job_recv(subreq);
+       TALLOC_FREE(subreq);
+       SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
+       if (tevent_req_error(req, ret)) {
+               return;
+       }
+
+       tevent_req_done(req);
+}
+
+static ssize_t vfswrap_pread_recv(struct tevent_req *req,
+                                 struct vfs_aio_state *vfs_aio_state)
+{
+       struct vfswrap_pread_state *state = tevent_req_data(
+               req, struct vfswrap_pread_state);
+
+       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
+               return -1;
+       }
+
+       *vfs_aio_state = state->vfs_aio_state;
+       return state->ret;
+}
+
+struct vfswrap_pwrite_state {
+       ssize_t ret;
+       int err;
+       int fd;
+       const void *buf;
+       size_t count;
+       off_t offset;
+
+       struct vfs_aio_state vfs_aio_state;
+       SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
+};
+
+static void vfs_pwrite_do(void *private_data);
+static void vfs_pwrite_done(struct tevent_req *subreq);
+
 static struct tevent_req *vfswrap_pwrite_send(struct vfs_handle_struct *handle,
                                              TALLOC_CTX *mem_ctx,
                                              struct tevent_context *ev,
@@ -806,130 +857,197 @@ static struct tevent_req *vfswrap_pwrite_send(struct vfs_handle_struct *handle,
                                              const void *data,
                                              size_t n, off_t offset)
 {
-       struct tevent_req *req;
-       struct vfswrap_asys_state *state;
+       struct tevent_req *req, *subreq;
+       struct vfswrap_pwrite_state *state;
        int ret;
 
-       req = tevent_req_create(mem_ctx, &state, struct vfswrap_asys_state);
+       req = tevent_req_create(mem_ctx, &state, struct vfswrap_pwrite_state);
        if (req == NULL) {
                return NULL;
        }
-       if (!vfswrap_init_asys_ctx(handle->conn->sconn)) {
-               tevent_req_oom(req);
+
+       ret = vfswrap_init_pool(handle->conn->sconn);
+       if (tevent_req_error(req, ret)) {
                return tevent_req_post(req, ev);
        }
-       state->asys_ctx = handle->conn->sconn->asys_ctx;
-       state->req = req;
+
+       state->ret = -1;
+       state->fd = fsp->fh->fd;
+       state->buf = data;
+       state->count = n;
+       state->offset = offset;
 
        SMBPROFILE_BYTES_ASYNC_START(syscall_asys_pwrite, profile_p,
                                     state->profile_bytes, n);
-       ret = asys_pwrite(state->asys_ctx, fsp->fh->fd, data, n, offset, req);
-       if (ret != 0) {
-               tevent_req_error(req, ret);
+       SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
+
+       subreq = pthreadpool_tevent_job_send(
+               state, ev, handle->conn->sconn->pool,
+               vfs_pwrite_do, state);
+       if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       talloc_set_destructor(state, vfswrap_asys_state_destructor);
+       tevent_req_set_callback(subreq, vfs_pwrite_done, req);
 
        return req;
 }
 
+static void vfs_pwrite_do(void *private_data)
+{
+       struct vfswrap_pwrite_state *state = talloc_get_type_abort(
+               private_data, struct vfswrap_pwrite_state);
+       struct timespec start_time;
+       struct timespec end_time;
+
+       SMBPROFILE_BYTES_ASYNC_SET_BUSY(state->profile_bytes);
+
+       PROFILE_TIMESTAMP(&start_time);
+
+       do {
+               state->ret = pwrite(state->fd, state->buf, state->count,
+                                  state->offset);
+       } while ((state->ret == -1) && (errno == EINTR));
+
+       state->err = errno;
+
+       PROFILE_TIMESTAMP(&end_time);
+
+       state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
+
+       SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
+}
+
+static void vfs_pwrite_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+#ifdef WITH_PROFILE
+       struct vfswrap_pwrite_state *state = tevent_req_data(
+               req, struct vfswrap_pwrite_state);
+#endif
+       int ret;
+
+       ret = pthreadpool_tevent_job_recv(subreq);
+       TALLOC_FREE(subreq);
+       SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
+       if (tevent_req_error(req, ret)) {
+               return;
+       }
+
+       tevent_req_done(req);
+}
+
+static ssize_t vfswrap_pwrite_recv(struct tevent_req *req,
+                                  struct vfs_aio_state *vfs_aio_state)
+{
+       struct vfswrap_pwrite_state *state = tevent_req_data(
+               req, struct vfswrap_pwrite_state);
+
+       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
+               return -1;
+       }
+
+       *vfs_aio_state = state->vfs_aio_state;
+       return state->ret;
+}
+
+struct vfswrap_fsync_state {
+       ssize_t ret;
+       int err;
+       int fd;
+
+       struct vfs_aio_state vfs_aio_state;
+       SMBPROFILE_BASIC_ASYNC_STATE(profile_basic);
+};
+
+static void vfs_fsync_do(void *private_data);
+static void vfs_fsync_done(struct tevent_req *subreq);
+
 static struct tevent_req *vfswrap_fsync_send(struct vfs_handle_struct *handle,
                                             TALLOC_CTX *mem_ctx,
                                             struct tevent_context *ev,
                                             struct files_struct *fsp)
 {
-       struct tevent_req *req;
-       struct vfswrap_asys_state *state;
+       struct tevent_req *req, *subreq;
+       struct vfswrap_fsync_state *state;
        int ret;
 
-       req = tevent_req_create(mem_ctx, &state, struct vfswrap_asys_state);
+       req = tevent_req_create(mem_ctx, &state, struct vfswrap_fsync_state);
        if (req == NULL) {
                return NULL;
        }
-       if (!vfswrap_init_asys_ctx(handle->conn->sconn)) {
-               tevent_req_oom(req);
+
+       ret = vfswrap_init_pool(handle->conn->sconn);
+       if (tevent_req_error(req, ret)) {
                return tevent_req_post(req, ev);
        }
-       state->asys_ctx = handle->conn->sconn->asys_ctx;
-       state->req = req;
+
+       state->ret = -1;
+       state->fd = fsp->fh->fd;
 
        SMBPROFILE_BASIC_ASYNC_START(syscall_asys_fsync, profile_p,
                                     state->profile_basic);
-       ret = asys_fsync(state->asys_ctx, fsp->fh->fd, req);
-       if (ret != 0) {
-               tevent_req_error(req, ret);
+
+       subreq = pthreadpool_tevent_job_send(
+               state, ev, handle->conn->sconn->pool, vfs_fsync_do, state);
+       if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
-       talloc_set_destructor(state, vfswrap_asys_state_destructor);
+       tevent_req_set_callback(subreq, vfs_fsync_done, req);
 
        return req;
 }
 
-static void vfswrap_asys_finished(struct tevent_context *ev,
-                                       struct tevent_fd *fde,
-                                       uint16_t flags, void *p)
+static void vfs_fsync_do(void *private_data)
 {
-       struct asys_context *asys_ctx = (struct asys_context *)p;
-       struct asys_result results[get_outstanding_aio_calls()];
-       int i, ret;
-
-       if ((flags & TEVENT_FD_READ) == 0) {
-               return;
-       }
+       struct vfswrap_fsync_state *state = talloc_get_type_abort(
+               private_data, struct vfswrap_fsync_state);
+       struct timespec start_time;
+       struct timespec end_time;
 
-       ret = asys_results(asys_ctx, results, get_outstanding_aio_calls());
-       if (ret < 0) {
-               DEBUG(1, ("asys_results returned %s\n", strerror(-ret)));
-               return;
-       }
+       PROFILE_TIMESTAMP(&start_time);
 
-       for (i=0; i<ret; i++) {
-               struct asys_result *result = &results[i];
-               struct tevent_req *req;
-               struct vfswrap_asys_state *state;
-
-               if ((result->ret == -1) && (result->err == ECANCELED)) {
-                       continue;
-               }
+       do {
+               state->ret = fsync(state->fd);
+       } while ((state->ret == -1) && (errno == EINTR));
 
-               req = talloc_get_type_abort(result->private_data,
-                                           struct tevent_req);
-               state = tevent_req_data(req, struct vfswrap_asys_state);
+       state->err = errno;
 
-               talloc_set_destructor(state, NULL);
+       PROFILE_TIMESTAMP(&end_time);
 
-               SMBPROFILE_BASIC_ASYNC_END(state->profile_basic);
-               SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
-               state->ret = result->ret;
-               state->vfs_aio_state.error = result->err;
-               state->vfs_aio_state.duration = result->duration;
-               tevent_req_defer_callback(req, ev);
-               tevent_req_done(req);
-       }
+       state->vfs_aio_state.duration = nsec_time_diff(&end_time, &start_time);
 }
 
-static ssize_t vfswrap_asys_ssize_t_recv(struct tevent_req *req,
-                                        struct vfs_aio_state *vfs_aio_state)
+static void vfs_fsync_done(struct tevent_req *subreq)
 {
-       struct vfswrap_asys_state *state = tevent_req_data(
-               req, struct vfswrap_asys_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+#ifdef WITH_PROFILE
+       struct vfswrap_fsync_state *state = tevent_req_data(
+               req, struct vfswrap_fsync_state);
+#endif
+       int ret;
 
-       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
-               return -1;
+       ret = pthreadpool_tevent_job_recv(subreq);
+       TALLOC_FREE(subreq);
+       SMBPROFILE_BASIC_ASYNC_END(state->profile_basic);
+       if (tevent_req_error(req, ret)) {
+               return;
        }
-       *vfs_aio_state = state->vfs_aio_state;
-       return state->ret;
+
+       tevent_req_done(req);
 }
 
-static int vfswrap_asys_int_recv(struct tevent_req *req,
-                                struct vfs_aio_state *vfs_aio_state)
+static int vfswrap_fsync_recv(struct tevent_req *req,
+                             struct vfs_aio_state *vfs_aio_state)
 {
-       struct vfswrap_asys_state *state = tevent_req_data(
-               req, struct vfswrap_asys_state);
+       struct vfswrap_fsync_state *state = tevent_req_data(
+               req, struct vfswrap_fsync_state);
 
        if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
                return -1;
        }
+
        *vfs_aio_state = state->vfs_aio_state;
        return state->ret;
 }
@@ -1427,10 +1545,21 @@ static NTSTATUS vfswrap_fsctl(struct vfs_handle_struct *handle,
        return NT_STATUS_NOT_SUPPORTED;
 }
 
+static bool vfswrap_is_offline(struct vfs_handle_struct *handle,
+                              const struct smb_filename *fname,
+                              SMB_STRUCT_STAT *sbuf);
+
 static NTSTATUS vfswrap_get_dos_attributes(struct vfs_handle_struct *handle,
                                           struct smb_filename *smb_fname,
                                           uint32_t *dosmode)
 {
+       bool offline;
+
+       offline = vfswrap_is_offline(handle, smb_fname, &smb_fname->st);
+       if (offline) {
+               *dosmode |= FILE_ATTRIBUTE_OFFLINE;
+       }
+
        return get_ea_dos_attribute(handle->conn, smb_fname, dosmode);
 }
 
@@ -1438,6 +1567,13 @@ static NTSTATUS vfswrap_fget_dos_attributes(struct vfs_handle_struct *handle,
                                            struct files_struct *fsp,
                                            uint32_t *dosmode)
 {
+       bool offline;
+
+       offline = vfswrap_is_offline(handle, fsp->fsp_name, &fsp->fsp_name->st);
+       if (offline) {
+               *dosmode |= FILE_ATTRIBUTE_OFFLINE;
+       }
+
        return get_ea_dos_attribute(handle->conn, fsp->fsp_name, dosmode);
 }
 
@@ -1456,10 +1592,23 @@ static NTSTATUS vfswrap_fset_dos_attributes(struct vfs_handle_struct *handle,
 }
 
 struct vfs_cc_state {
-       off_t copied;
+       struct tevent_context *ev;
        uint8_t *buf;
+       bool read_lck_locked;
+       struct lock_struct read_lck;
+       bool write_lck_locked;
+       struct lock_struct write_lck;
+       struct files_struct *src_fsp;
+       off_t src_off;
+       struct files_struct *dst_fsp;
+       off_t dst_off;
+       off_t to_copy;
+       off_t remaining;
+       size_t next_io_size;
 };
 
+static NTSTATUS copy_chunk_loop(struct tevent_req *req);
+
 static struct tevent_req *vfswrap_copy_chunk_send(struct vfs_handle_struct *handle,
                                                  TALLOC_CTX *mem_ctx,
                                                  struct tevent_context *ev,
@@ -1467,23 +1616,31 @@ static struct tevent_req *vfswrap_copy_chunk_send(struct vfs_handle_struct *hand
                                                  off_t src_off,
                                                  struct files_struct *dest_fsp,
                                                  off_t dest_off,
-                                                 off_t num)
+                                                 off_t to_copy)
 {
        struct tevent_req *req;
-       struct vfs_cc_state *vfs_cc_state;
+       struct vfs_cc_state *state = NULL;
+       size_t num = MIN(to_copy, COPYCHUNK_MAX_TOTAL_LEN);
        NTSTATUS status;
 
-       DEBUG(10, ("performing server side copy chunk of length %lu\n",
-                  (unsigned long)num));
+       DBG_DEBUG("server side copy chunk of length %" PRIu64 "\n", to_copy);
 
-       req = tevent_req_create(mem_ctx, &vfs_cc_state, struct vfs_cc_state);
+       req = tevent_req_create(mem_ctx, &state, struct vfs_cc_state);
        if (req == NULL) {
                return NULL;
        }
 
-       vfs_cc_state->buf = talloc_array(vfs_cc_state, uint8_t,
-                                        MIN(num, 8*1024*1024));
-       if (tevent_req_nomem(vfs_cc_state->buf, req)) {
+       *state = (struct vfs_cc_state) {
+               .ev = ev,
+               .src_fsp = src_fsp,
+               .src_off = src_off,
+               .dst_fsp = dest_fsp,
+               .dst_off = dest_off,
+               .to_copy = to_copy,
+               .remaining = to_copy,
+       };
+       state->buf = talloc_array(state, uint8_t, num);
+       if (tevent_req_nomem(state->buf, req)) {
                return tevent_req_post(req, ev);
        }
 
@@ -1506,115 +1663,188 @@ static struct tevent_req *vfswrap_copy_chunk_send(struct vfs_handle_struct *hand
                return tevent_req_post(req, ev);
        }
 
-       /* could use 2.6.33+ sendfile here to do this in kernel */
-       while (vfs_cc_state->copied < num) {
-               ssize_t ret;
-               struct lock_struct lck;
-               int saved_errno;
+       if (src_fsp->op == NULL) {
+               tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+               return tevent_req_post(req, ev);
+       }
 
-               off_t this_num = MIN(talloc_array_length(vfs_cc_state->buf),
-                                    num - vfs_cc_state->copied);
+       if (dest_fsp->op == NULL) {
+               tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+               return tevent_req_post(req, ev);
+       }
 
-               if (src_fsp->op == NULL) {
-                       tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
-                       return tevent_req_post(req, ev);
-               }
-               init_strict_lock_struct(src_fsp,
-                                       src_fsp->op->global->open_persistent_id,
-                                       src_off,
-                                       this_num,
-                                       READ_LOCK,
-                                       &lck);
-
-               if (!SMB_VFS_STRICT_LOCK(src_fsp->conn, src_fsp, &lck)) {
-                       tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
-                       return tevent_req_post(req, ev);
-               }
+       status = copy_chunk_loop(req);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
 
-               ret = SMB_VFS_PREAD(src_fsp, vfs_cc_state->buf,
-                                   this_num, src_off);
-               if (ret == -1) {
-                       saved_errno = errno;
-               }
+       return req;
+}
 
-               SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &lck);
+static void vfswrap_copy_chunk_read_done(struct tevent_req *subreq);
 
-               if (ret == -1) {
-                       errno = saved_errno;
-                       tevent_req_nterror(req, map_nt_error_from_unix(errno));
-                       return tevent_req_post(req, ev);
-               }
-               if (ret != this_num) {
-                       /* zero tolerance for short reads */
-                       tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
-                       return tevent_req_post(req, ev);
-               }
+static NTSTATUS copy_chunk_loop(struct tevent_req *req)
+{
+       struct vfs_cc_state *state = tevent_req_data(req, struct vfs_cc_state);
+       struct tevent_req *subreq = NULL;
+       bool ok;
 
-               src_off += ret;
+       state->next_io_size = MIN(state->remaining, talloc_array_length(state->buf));
 
-               if (dest_fsp->op == NULL) {
-                       tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
-                       return tevent_req_post(req, ev);
-               }
+       init_strict_lock_struct(state->src_fsp,
+                               state->src_fsp->op->global->open_persistent_id,
+                               state->src_off,
+                               state->next_io_size,
+                               READ_LOCK,
+                               &state->read_lck);
 
-               init_strict_lock_struct(dest_fsp,
-                                       dest_fsp->op->global->open_persistent_id,
-                                       dest_off,
-                                       this_num,
-                                       WRITE_LOCK,
-                                       &lck);
+       ok = SMB_VFS_STRICT_LOCK(state->src_fsp->conn,
+                                state->src_fsp,
+                                &state->read_lck);
+       if (!ok) {
+               return NT_STATUS_FILE_LOCK_CONFLICT;
+       }
 
-               if (!SMB_VFS_STRICT_LOCK(dest_fsp->conn, dest_fsp, &lck)) {
-                       tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
-                       return tevent_req_post(req, ev);
-               }
+       subreq = SMB_VFS_PREAD_SEND(state,
+                                   state->src_fsp->conn->sconn->ev_ctx,
+                                   state->src_fsp,
+                                   state->buf,
+                                   state->next_io_size,
+                                   state->src_off);
+       if (subreq == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       tevent_req_set_callback(subreq, vfswrap_copy_chunk_read_done, req);
 
-               ret = SMB_VFS_PWRITE(dest_fsp, vfs_cc_state->buf,
-                                    this_num, dest_off);
-               if (ret == -1) {
-                       saved_errno = errno;
-               }
+       return NT_STATUS_OK;
+}
 
-               SMB_VFS_STRICT_UNLOCK(src_fsp->conn, src_fsp, &lck);
+static void vfswrap_copy_chunk_write_done(struct tevent_req *subreq);
 
-               if (ret == -1) {
-                       errno = saved_errno;
-                       tevent_req_nterror(req, map_nt_error_from_unix(errno));
-                       return tevent_req_post(req, ev);
-               }
-               if (ret != this_num) {
-                       /* zero tolerance for short writes */
-                       tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
-                       return tevent_req_post(req, ev);
-               }
-               dest_off += ret;
+static void vfswrap_copy_chunk_read_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct vfs_cc_state *state = tevent_req_data(req, struct vfs_cc_state);
+       struct vfs_aio_state aio_state;
+       ssize_t nread;
+       bool ok;
 
-               vfs_cc_state->copied += this_num;
+       SMB_VFS_STRICT_UNLOCK(state->src_fsp->conn,
+                             state->src_fsp,
+                             &state->read_lck);
+       ZERO_STRUCT(state->read_lck);
+
+       nread = SMB_VFS_PREAD_RECV(subreq, &aio_state);
+       TALLOC_FREE(subreq);
+       if (nread == -1) {
+               DBG_ERR("read failed: %s\n", strerror(errno));
+               tevent_req_nterror(req, map_nt_error_from_unix(aio_state.error));
+               return;
+       }
+       if (nread != state->next_io_size) {
+               DBG_ERR("Short read, only %zd of %zu\n",
+                       nread, state->next_io_size);
+               tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
+               return;
        }
 
-       tevent_req_done(req);
-       return tevent_req_post(req, ev);
+       state->src_off += nread;
+
+       init_strict_lock_struct(state->dst_fsp,
+                               state->dst_fsp->op->global->open_persistent_id,
+                               state->dst_off,
+                               state->next_io_size,
+                               WRITE_LOCK,
+                               &state->write_lck);
+
+       ok = SMB_VFS_STRICT_LOCK(state->dst_fsp->conn,
+                                state->dst_fsp,
+                                &state->write_lck);
+       if (!ok) {
+               tevent_req_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
+               return;
+       }
+
+       subreq = SMB_VFS_PWRITE_SEND(state,
+                                    state->ev,
+                                    state->dst_fsp,
+                                    state->buf,
+                                    state->next_io_size,
+                                    state->dst_off);
+       if (subreq == NULL) {
+               tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
+               return;
+       }
+       tevent_req_set_callback(subreq, vfswrap_copy_chunk_write_done, req);
+}
+
+static void vfswrap_copy_chunk_write_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct vfs_cc_state *state = tevent_req_data(req, struct vfs_cc_state);
+       struct vfs_aio_state aio_state;
+       ssize_t nwritten;
+       NTSTATUS status;
+
+       SMB_VFS_STRICT_UNLOCK(state->dst_fsp->conn,
+                             state->dst_fsp,
+                             &state->write_lck);
+       ZERO_STRUCT(state->write_lck);
+
+       nwritten = SMB_VFS_PWRITE_RECV(subreq, &aio_state);
+       TALLOC_FREE(subreq);
+       if (nwritten == -1) {
+               DBG_ERR("write failed: %s\n", strerror(errno));
+               tevent_req_nterror(req, map_nt_error_from_unix(aio_state.error));
+               return;
+       }
+       if (nwritten != state->next_io_size) {
+               DBG_ERR("Short write, only %zd of %zu\n", nwritten, state->next_io_size);
+               tevent_req_nterror(req, NT_STATUS_IO_DEVICE_ERROR);
+               return;
+       }
+
+       state->dst_off += nwritten;
+
+       if (state->remaining < nwritten) {
+               /* Paranoia check */
+               tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+               return;
+       }
+       state->remaining -= nwritten;
+       if (state->remaining == 0) {
+               tevent_req_done(req);
+               return;
+       }
+
+       status = copy_chunk_loop(req);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
+       }
+
+       return;
 }
 
 static NTSTATUS vfswrap_copy_chunk_recv(struct vfs_handle_struct *handle,
                                        struct tevent_req *req,
                                        off_t *copied)
 {
-       struct vfs_cc_state *vfs_cc_state = tevent_req_data(req,
-                                                       struct vfs_cc_state);
+       struct vfs_cc_state *state = tevent_req_data(req, struct vfs_cc_state);
        NTSTATUS status;
 
        if (tevent_req_is_nterror(req, &status)) {
-               DEBUG(2, ("server side copy chunk failed: %s\n",
-                         nt_errstr(status)));
+               DBG_DEBUG("copy chunk failed: %s\n", nt_errstr(status));
                *copied = 0;
                tevent_req_received(req);
                return status;
        }
 
-       *copied = vfs_cc_state->copied;
-       DEBUG(10, ("server side copy chunk copied %lu\n",
-                  (unsigned long)*copied));
+       *copied = state->to_copy;
+       DBG_DEBUG("copy chunk copied %lu\n", (unsigned long)*copied);
        tevent_req_received(req);
 
        return NT_STATUS_OK;
@@ -1978,9 +2208,8 @@ static int strict_allocate_ftruncate(vfs_handle_struct *handle, files_struct *fs
                "error %d. Falling back to slow manual allocation\n", errno));
 
        /* available disk space is enough or not? */
-       space_avail = get_dfree_info(fsp->conn,
-                                    fsp->fsp_name->base_name,
-                                    &bsize, &dfree, &dsize);
+       space_avail =
+           get_dfree_info(fsp->conn, fsp->fsp_name, &bsize, &dfree, &dsize);
        /* space_avail is 1k blocks */
        if (space_avail == (uint64_t)-1 ||
                        ((uint64_t)space_to_write/1024 > space_avail) ) {
@@ -2102,6 +2331,14 @@ static bool vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int op, o
        bool result;
 
        START_PROFILE(syscall_fcntl_lock);
+
+       if (fsp->use_ofd_locks || !lp_parm_bool(SNUM(fsp->conn),
+                                               "smbd",
+                                               "force process locks",
+                                               false)) {
+               op = map_process_lock_to_ofd_lock(op, &fsp->use_ofd_locks);
+       }
+
        result =  fcntl_lock(fsp->fh->fd, op, offset, count, type);
        END_PROFILE(syscall_fcntl_lock);
        return result;
@@ -2122,6 +2359,14 @@ static bool vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, off_t
        int op = F_GETLK;
 
        START_PROFILE(syscall_fcntl_getlock);
+
+       if (fsp->use_ofd_locks || !lp_parm_bool(SNUM(fsp->conn),
+                                               "smbd",
+                                               "force process locks",
+                                               false)) {
+               op = map_process_lock_to_ofd_lock(op, &fsp->use_ofd_locks);
+       }
+
        result = fcntl_getlock(fsp->fh->fd, op, poffset, pcount, ptype, ppid);
        END_PROFILE(syscall_fcntl_getlock);
        return result;
@@ -2551,16 +2796,6 @@ static bool vfswrap_is_offline(struct vfs_handle_struct *handle,
        return offline;
 }
 
-static int vfswrap_set_offline(struct vfs_handle_struct *handle,
-                              const struct smb_filename *fname)
-{
-       /* We don't know how to set offline bit by default, needs to be overriden in the vfs modules */
-#if defined(ENOTSUP)
-       errno = ENOTSUP;
-#endif
-       return -1;
-}
-
 static NTSTATUS vfswrap_durable_cookie(struct vfs_handle_struct *handle,
                                       struct files_struct *fsp,
                                       TALLOC_CTX *mem_ctx,
@@ -2630,18 +2865,18 @@ static struct vfs_fn_pointers vfs_default_fns = {
        .read_fn = vfswrap_read,
        .pread_fn = vfswrap_pread,
        .pread_send_fn = vfswrap_pread_send,
-       .pread_recv_fn = vfswrap_asys_ssize_t_recv,
+       .pread_recv_fn = vfswrap_pread_recv,
        .write_fn = vfswrap_write,
        .pwrite_fn = vfswrap_pwrite,
        .pwrite_send_fn = vfswrap_pwrite_send,
-       .pwrite_recv_fn = vfswrap_asys_ssize_t_recv,
+       .pwrite_recv_fn = vfswrap_pwrite_recv,
        .lseek_fn = vfswrap_lseek,
        .sendfile_fn = vfswrap_sendfile,
        .recvfile_fn = vfswrap_recvfile,
        .rename_fn = vfswrap_rename,
        .fsync_fn = vfswrap_fsync,
        .fsync_send_fn = vfswrap_fsync_send,
-       .fsync_recv_fn = vfswrap_asys_int_recv,
+       .fsync_recv_fn = vfswrap_fsync_recv,
        .stat_fn = vfswrap_stat,
        .fstat_fn = vfswrap_fstat,
        .lstat_fn = vfswrap_lstat,
@@ -2720,18 +2955,14 @@ static struct vfs_fn_pointers vfs_default_fns = {
        /* aio operations */
        .aio_force_fn = vfswrap_aio_force,
 
-       /* offline operations */
-       .is_offline_fn = vfswrap_is_offline,
-       .set_offline_fn = vfswrap_set_offline,
-
        /* durable handle operations */
        .durable_cookie_fn = vfswrap_durable_cookie,
        .durable_disconnect_fn = vfswrap_durable_disconnect,
        .durable_reconnect_fn = vfswrap_durable_reconnect,
 };
 
-NTSTATUS vfs_default_init(void);
-NTSTATUS vfs_default_init(void)
+NTSTATUS vfs_default_init(TALLOC_CTX *);
+NTSTATUS vfs_default_init(TALLOC_CTX *ctx)
 {
        return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
                                DEFAULT_VFS_MODULE_NAME, &vfs_default_fns);