r16853: New module to flush dirty file data at regular intervals.
authorJames Peach <jpeach@samba.org>
Fri, 7 Jul 2006 03:45:02 +0000 (03:45 +0000)
committerJames Peach <jpeach@samba.org>
Fri, 7 Jul 2006 03:45:02 +0000 (03:45 +0000)
source/Makefile.in
source/configure.in
source/modules/vfs_commit.c [new file with mode: 0644]

index 185f52da5d54d5b9c88d7ee43df82fc57e10f43d..3f586180f048669359004358d1a7184b94d2b5af 100644 (file)
@@ -384,6 +384,7 @@ VFS_AFSACL_OBJ = modules/vfs_afsacl.o
 VFS_CATIA_OBJ = modules/vfs_catia.o
 VFS_CACHEPRIME_OBJ = modules/vfs_cacheprime.o
 VFS_PREALLOC_OBJ = modules/vfs_prealloc.o
+VFS_COMMIT_OBJ = modules/vfs_commit.o
 
 PLAINTEXT_AUTH_OBJ = auth/pampass.o auth/pass_check.o
 
@@ -1419,6 +1420,11 @@ bin/prealloc.@SHLIBEXT@: $(VFS_PREALLOC_OBJ:.o=.@PICSUFFIX@)
        @$(SHLD) $(LDSHFLAGS) -o $@ $(VFS_PREALLOC_OBJ:.o=.@PICSUFFIX@) \
                @SONAMEFLAG@`basename $@`
 
+bin/commit.@SHLIBEXT@: $(VFS_COMMIT_OBJ:.o=.@PICSUFFIX@)
+       @echo "Building plugin $@"
+       @$(SHLD) $(LDSHFLAGS) -o $@ $(VFS_COMMIT_OBJ:.o=.@PICSUFFIX@) \
+               @SONAMEFLAG@`basename $@`
+
 bin/wbinfo@EXEEXT@: $(WBINFO_OBJ) @BUILD_POPT@ bin/.dummy
        @echo Linking $@
        @$(CC) $(FLAGS) @PIE_LDFLAGS@ -o $@ $(LDFLAGS) $(WBINFO_OBJ) $(DYNEXP) $(LIBS) @POPTLIBS@
index 78f6c0157d540da2a0d232b729a883d244005108..dbf35bed4df236d2e3e413edf032ea2859b02efb 100644 (file)
@@ -5606,6 +5606,7 @@ SMB_MODULE(vfs_afsacl, \$(VFS_AFSACL_OBJ), "bin/afsacl.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_catia, \$(VFS_CATIA_OBJ), "bin/catia.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_cacheprime, \$(VFS_CACHEPRIME_OBJ), "bin/cacheprime.$SHLIBEXT", VFS)
 SMB_MODULE(vfs_prealloc, \$(VFS_PREALLOC_OBJ), "bin/prealloc.$SHLIBEXT", VFS)
+SMB_MODULE(vfs_commit, \$(VFS_COMMIT_OBJ), "bin/commit.$SHLIBEXT", VFS)
 
 SMB_SUBSYSTEM(VFS,smbd/vfs.o)
 
diff --git a/source/modules/vfs_commit.c b/source/modules/vfs_commit.c
new file mode 100644 (file)
index 0000000..1512f0d
--- /dev/null
@@ -0,0 +1,227 @@
+/*
+ * Copyright (c) James Peach 2006
+ *
+ * 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 2 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, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include "includes.h"
+
+/* Commit data module.
+ *
+ * The purpose of this module is to flush data to disk at regular intervals,
+ * just like the NFS commit operation. There's two rationales for this. First,
+ * it minimises the data loss in case of a power outage without incurring
+ * the poor performance of synchronous I/O. Second, a steady flush rate
+ * can produce better throughput than suddenly dumping massive amounts of
+ * writes onto a disk.
+ *
+ * Tunables:
+ *
+ *  commit: dthresh         Amount of dirty data that can accumulate
+ *                                     before we commit (sync) it.
+ *
+ *  commit: debug           Debug level at which to emit messages.
+ *
+ */
+
+#define MODULE "commit"
+
+static int module_debug;
+
+struct commit_info
+{
+        SMB_OFF_T dbytes;      /* Dirty (uncommitted) bytes */
+        SMB_OFF_T dthresh;     /* Dirty data threshold */
+};
+
+static SMB_OFF_T conv_str_size(const char * str)
+{
+        SMB_OFF_T lval;
+        char * end;
+
+        if (str == NULL || *str == '\0') {
+                return 0;
+        }
+
+        if (sizeof(SMB_OFF_T) == 8) {
+                lval = strtoull(str, &end, 10 /* base */);
+        } else {
+                lval = strtoul(str, &end, 10 /* base */);
+        }
+
+        if (end == NULL || end == str) {
+                return 0;
+        }
+
+        if (*end) {
+                if (strwicmp(end, "K") == 0) {
+                        lval *= 1024ULL;
+                } else if (strwicmp(end, "M") == 0) {
+                        lval *= (1024ULL * 1024ULL);
+                } else if (strwicmp(end, "G") == 0) {
+                        lval *= (1024ULL * 1024ULL * 1024ULL);
+                } else if (strwicmp(end, "T") == 0) {
+                        lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
+                } else if (strwicmp(end, "P") == 0) {
+                        lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
+                } else {
+                        return 0;
+                }
+        }
+
+        return lval;
+}
+
+static void commit_all(
+        struct vfs_handle_struct *     handle,
+        files_struct *                 fsp)
+{
+        struct commit_info *c;
+
+        if ((c = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
+                if (c->dbytes) {
+                        DEBUG(module_debug,
+                                ("%s: flushing %lu dirty bytes\n",
+                                 MODULE, (unsigned long)c->dbytes));
+
+                        fdatasync(fsp->fh->fd);
+                        c->dbytes = 0;
+                }
+        }
+}
+
+static void commit(
+        struct vfs_handle_struct *     handle,
+        files_struct *                 fsp,
+        ssize_t                                last_write)
+{
+        struct commit_info *c;
+
+        if ((c = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
+
+                if (last_write > 0) {
+                        c->dbytes += last_write;
+                }
+
+                if (c->dbytes > c->dthresh) {
+                        DEBUG(module_debug,
+                                ("%s: flushing %lu dirty bytes\n",
+                                 MODULE, (unsigned long)c->dbytes));
+
+                        fdatasync(fsp->fh->fd);
+                        c->dbytes = 0;
+                }
+        }
+}
+
+static int commit_connect(
+        struct vfs_handle_struct *  handle,
+        const char *                service,
+        const char *                user)
+{
+        module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100);
+        return SMB_VFS_NEXT_CONNECT(handle, service, user);
+}
+
+static int commit_open(
+       vfs_handle_struct * handle,
+       const char *        fname,
+       files_struct *      fsp,
+       int                 flags,
+       mode_t              mode)
+{
+        SMB_OFF_T dthresh;
+
+        /* Don't bother with read-only files. */
+        if ((flags & O_ACCMODE) == O_RDONLY) {
+                return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
+        }
+
+        dthresh = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
+                                        MODULE, "dthresh", NULL));
+
+        if (dthresh > 0) {
+                struct commit_info * c;
+                c = VFS_ADD_FSP_EXTENSION(handle, fsp, struct commit_info);
+                if (c) {
+                        c->dthresh = dthresh;
+                        c->dbytes = 0;
+                }
+        }
+
+        return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
+}
+
+static ssize_t commit_write(
+        vfs_handle_struct * handle,
+        files_struct *      fsp,
+        int                 fd,
+        void *              data,
+        size_t              count)
+{
+        ssize_t ret;
+
+        ret = SMB_VFS_NEXT_WRITE(handle, fsp, fd, data, count);
+        commit(handle, fsp, ret);
+
+        return ret;
+}
+
+static ssize_t commit_pwrite(
+        vfs_handle_struct * handle,
+        files_struct *      fsp,
+        int                 fd,
+        void *              data,
+        size_t              count,
+       SMB_OFF_T           offset)
+{
+        ssize_t ret;
+
+        ret = SMB_VFS_NEXT_PWRITE(handle, fsp, fd, data, count, offset);
+        commit(handle, fsp, ret);
+
+        return ret;
+}
+
+static ssize_t commit_close(
+        vfs_handle_struct * handle,
+        files_struct *      fsp,
+        int                 fd)
+{
+        commit_all(handle, fsp);
+        return SMB_VFS_NEXT_CLOSE(handle, fsp, fd);
+}
+
+static vfs_op_tuple commit_ops [] =
+{
+        {SMB_VFS_OP(commit_open),
+                SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
+        {SMB_VFS_OP(commit_close),
+                SMB_VFS_OP_CLOSE, SMB_VFS_LAYER_TRANSPARENT},
+        {SMB_VFS_OP(commit_write),
+                SMB_VFS_OP_WRITE, SMB_VFS_LAYER_TRANSPARENT},
+        {SMB_VFS_OP(commit_pwrite),
+                SMB_VFS_OP_PWRITE, SMB_VFS_LAYER_TRANSPARENT},
+        {SMB_VFS_OP(commit_connect),
+                SMB_VFS_OP_CONNECT,  SMB_VFS_LAYER_TRANSPARENT},
+
+        {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
+};
+
+NTSTATUS vfs_commit_init(void)
+{
+       return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE, commit_ops);
+}
+