s3-vfs: add vfs_btrfs module
authorDavid Disseldorp <ddiss@samba.org>
Tue, 4 Sep 2012 13:27:38 +0000 (15:27 +0200)
committerDavid Disseldorp <ddiss@samba.org>
Wed, 19 Sep 2012 03:59:03 +0000 (05:59 +0200)
Currently it only plumbs itself into the copy_chunk call path,
translating such requests into BTRFS_IOC_CLONE_RANGE calls.

source3/Makefile.in
source3/configure.in
source3/modules/vfs_btrfs.c [new file with mode: 0644]
source3/modules/wscript_build
source3/wscript

index 430fc88df7402727155cfa669db87834d046fb72..97d583221005c6148aacf66c847f76cc4265ffe5 100644 (file)
@@ -903,6 +903,7 @@ VFS_CROSSRENAME_OBJ = modules/vfs_crossrename.o
 VFS_LINUX_XFS_SGID_OBJ = modules/vfs_linux_xfs_sgid.o
 VFS_TIME_AUDIT_OBJ = modules/vfs_time_audit.o
 VFS_MEDIA_HARMONY_OBJ = modules/vfs_media_harmony.o
+VFS_BTRFS_OBJ = modules/vfs_btrfs.o
 
 PAM_ERRORS_OBJ = ../libcli/auth/pam_errors.o
 PLAINTEXT_AUTH_OBJ = auth/pampass.o auth/pass_check.o $(PAM_ERRORS_OBJ)
@@ -2947,6 +2948,10 @@ bin/media_harmony.@SHLIBEXT@: $(BINARY_PREREQS) $(VFS_MEDIA_HARMONY_OBJ)
        @echo "Building plugin $@"
        @$(SHLD_MODULE) $(VFS_MEDIA_HARMONY_OBJ)
 
+bin/btrfs.@SHLIBEXT@: $(BINARY_PREREQS) $(VFS_BTRFS_OBJ)
+       @echo "Building plugin $@"
+       @$(SHLD_MODULE) $(VFS_BTRFS_OBJ)
+
 #########################################################
 ## IdMap NSS plugins
 
index 06f6028dfc4d75ddf4095faf6a135430b339afa0..5439416f1d0e5dacba9801184ea1e06897c503f8 100644 (file)
@@ -6306,6 +6306,18 @@ fi
 # End
 # Checks for the vfs_fileid module
 
+# Check for btrfs support
+AC_CACHE_CHECK([for btrfs],samba_cv_have_btrfs, [
+    AC_TRY_COMPILE([#include <asm-generic/int-ll64.h>
+                    #include <btrfs/btrfs_ioctl.h>],
+                   [struct btrfs_ioctl_clone_range_args cr;],
+                   samba_cv_have_btrfs=yes,
+                  samba_cv_have_btrfs=no)])
+if test x"$samba_cv_have_btrfs" = x"yes"; then
+    AC_DEFINE(HAVE_BTRFS, 1, [If btrfs ioctls are available])
+    default_shared_modules="$default_shared_modules vfs_btrfs"
+fi
+# End btrfs
 
 for i in `echo $default_static_modules | sed -e 's/,/ /g'`
 do
@@ -6436,6 +6448,7 @@ SMB_MODULE(vfs_crossrename, \$(VFS_CROSSRENAME_OBJ), "bin/crossrename.$SHLIBEXT"
 SMB_MODULE(vfs_linux_xfs_sgid, \$(VFS_LINUX_XFS_SGID_OBJ), "bin/linux_xfs_sgid.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_time_audit, \$(VFS_TIME_AUDIT_OBJ), "bin/time_audit.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_media_harmony, \$(VFS_MEDIA_HARMONY_OBJ), "bin/media_harmony.$SHLIBEXT", VFS)
+SMB_MODULE(vfs_btrfs, \$(VFS_BTRFS_OBJ), "bin/btrfs.$SHLIBEXT", VFS)
 
 SMB_SUBSYSTEM(VFS,smbd/vfs.o)
 
diff --git a/source3/modules/vfs_btrfs.c b/source3/modules/vfs_btrfs.c
new file mode 100644 (file)
index 0000000..7489813
--- /dev/null
@@ -0,0 +1,137 @@
+/*
+ * Module to make use of awesome btrfs features
+ *
+ * Copyright (C) David Disseldorp 2011
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <asm-generic/int-ll64.h>
+#include <btrfs/btrfs_ioctl.h>
+#include "includes.h"
+#include "system/filesys.h"
+#include "smbd/smbd.h"
+#include "lib/util/tevent_ntstatus.h"
+
+struct btrfs_cc_state {
+       struct vfs_handle_struct *handle;
+       SMB_OFF_T copied;
+       NTSTATUS status;
+       struct tevent_req *subreq;      /* non-null if passed to next VFS fn */
+};
+static void btrfs_copy_chunk_done(struct tevent_req *subreq);
+
+static struct tevent_req *btrfs_copy_chunk_send(struct vfs_handle_struct *handle,
+                                               TALLOC_CTX *mem_ctx,
+                                               struct tevent_context *ev,
+                                               struct files_struct *src_fsp,
+                                               SMB_OFF_T src_off,
+                                               struct files_struct *dest_fsp,
+                                               SMB_OFF_T dest_off,
+                                               SMB_OFF_T num)
+{
+       struct tevent_req *req;
+       struct btrfs_cc_state *cc_state;
+       struct btrfs_ioctl_clone_range_args cr_args;
+       int ret;
+
+       req = tevent_req_create(mem_ctx, &cc_state, struct btrfs_cc_state);
+       if (req == NULL) {
+               return NULL;
+       }
+       cc_state->handle = handle;
+       cc_state->status = NT_STATUS_OK;
+
+       ZERO_STRUCT(cr_args);
+       cr_args.src_fd = src_fsp->fh->fd;
+       cr_args.src_offset = (uint64_t)src_off;
+       cr_args.dest_offset = (uint64_t)dest_off;
+       cr_args.src_length = (uint64_t)num;
+
+       ret = ioctl(dest_fsp->fh->fd, BTRFS_IOC_CLONE_RANGE, &cr_args);
+       if (ret < 0) {
+               /*
+                * BTRFS_IOC_CLONE_RANGE only supports 'sectorsize' aligned
+                * cloning. Which is 4096 by default, therefore fall back to
+                * manual read/write on failure.
+                */
+               DEBUG(5, ("BTRFS_IOC_CLONE_RANGE failed: %s, length %llu, "
+                         "src fd: %lld off: %llu, dest fd: %d off: %llu\n",
+                         strerror(errno), cr_args.src_length,
+                         cr_args.src_fd, cr_args.src_offset,
+                         dest_fsp->fh->fd, cr_args.dest_offset));
+               cc_state->subreq = SMB_VFS_NEXT_COPY_CHUNK_SEND(handle,
+                                                               cc_state, ev,
+                                                               src_fsp,
+                                                               src_off,
+                                                               dest_fsp,
+                                                               dest_off, num);
+               if (tevent_req_nomem(cc_state->subreq, req)) {
+                       return tevent_req_post(req, ev);
+               }
+               /* wait for subreq completion */
+               tevent_req_set_callback(cc_state->subreq,
+                                       btrfs_copy_chunk_done,
+                                       req);
+               return req;
+
+       }
+
+       DEBUG(5, ("BTRFS_IOC_CLONE_RANGE returned %d\n", ret));
+       /* BTRFS_IOC_CLONE_RANGE is all or nothing */
+       cc_state->copied = num;
+       tevent_req_done(req);
+       return tevent_req_post(req, ev);
+}
+
+/* only used if the request is passed through to next VFS module */
+static void btrfs_copy_chunk_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct btrfs_cc_state *cc_state = tevent_req_data(req,
+                                                       struct btrfs_cc_state);
+
+       cc_state->status = SMB_VFS_NEXT_COPY_CHUNK_RECV(cc_state->handle,
+                                       cc_state->subreq, &cc_state->copied);
+       if (tevent_req_nterror(req, cc_state->status)) {
+               return;
+       }
+       tevent_req_done(req);
+}
+
+static NTSTATUS btrfs_copy_chunk_recv(struct vfs_handle_struct *handle,
+                                     struct tevent_req *req,
+                                     SMB_OFF_T *copied)
+{
+       struct btrfs_cc_state *cc_state = tevent_req_data(req,
+                                                       struct btrfs_cc_state);
+
+       DEBUG(10,("server side copy chunk copied %lu\n", cc_state->copied));
+
+       *copied = cc_state->copied;
+       return cc_state->status;
+}
+
+static struct vfs_fn_pointers btrfs_fns = {
+       .copy_chunk_send_fn = btrfs_copy_chunk_send,
+       .copy_chunk_recv_fn = btrfs_copy_chunk_recv,
+};
+
+NTSTATUS vfs_btrfs_init(void);
+NTSTATUS vfs_btrfs_init(void)
+{
+       return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
+                               "btrfs", &btrfs_fns);
+}
index 594b27c5569f6408de97fcd317b6672c4f741418..418e61b858bb9f4d3c5b421b5ee5ef5967941803 100644 (file)
@@ -49,6 +49,7 @@ VFS_SCANNEDONLY_SRC = 'vfs_scannedonly.c'
 VFS_CROSSRENAME_SRC = 'vfs_crossrename.c'
 VFS_LINUX_XFS_SGID_SRC = 'vfs_linux_xfs_sgid.c'
 VFS_TIME_AUDIT_SRC = 'vfs_time_audit.c'
+VFS_BTRFS_SRC = 'vfs_btrfs.c'
 
 
 bld.SAMBA3_SUBSYSTEM('NFS4_ACLS',
@@ -472,6 +473,14 @@ bld.SAMBA3_MODULE('vfs_dfs_samba4',
                  internal_module=bld.SAMBA3_IS_STATIC_MODULE('vfs_dfs_samba4') and bld.AD_DC_BUILD_IS_ENABLED(),
                  enabled=bld.SAMBA3_IS_ENABLED_MODULE('vfs_dfs_samba4') and bld.AD_DC_BUILD_IS_ENABLED())
 
+bld.SAMBA3_MODULE('vfs_btrfs',
+                 subsystem='vfs',
+                 source=VFS_BTRFS_SRC,
+                 deps='samba-util',
+                 init_function='',
+                 internal_module=bld.SAMBA3_IS_STATIC_MODULE('vfs_btrfs'),
+                 enabled=bld.SAMBA3_IS_ENABLED_MODULE('vfs_btrfs'))
+
 PERFCOUNT_TEST_SRC = 'perfcount_test.c'
 
 bld.SAMBA3_SUBSYSTEM('perfcount',
index d0e76f361781294558f640ca16cec25d67908ca0..54270704b022817547a6a134d99c77bcd6128243 100644 (file)
@@ -1501,6 +1501,14 @@ main() {
     if conf.CHECK_HEADERS('gpfs_gpl.h'):
         conf.DEFINE('HAVE_GPFS', '1')
 
+    conf.CHECK_CODE('''
+                   #include <asm-generic/int-ll64.h>
+                   #include <btrfs/btrfs_ioctl.h>
+                   struct btrfs_ioctl_clone_range_args cr;
+                   ''',
+                   'HAVE_BTRFS',
+                   msg='Checking whether btrfs ioctls are available')
+
     default_static_modules.extend(TO_LIST('''pdb_smbpasswd pdb_tdbsam pdb_wbc_sam
                                       auth_sam auth_unix auth_winbind auth_wbc
                                       auth_domain auth_builtin vfs_default
@@ -1561,6 +1569,9 @@ main() {
     if conf.CONFIG_SET('HAVE_GPFS'):
        default_shared_modules.extend(TO_LIST('vfs_gpfs'))
 
+    if conf.CONFIG_SET('HAVE_BTRFS'):
+       default_shared_modules.extend(TO_LIST('vfs_btrfs'))
+
     explicit_shared_modules = TO_LIST(Options.options.shared_modules, delimiter=',')
     explicit_static_modules = TO_LIST(Options.options.static_modules, delimiter=',')