root/source3/modules/vfs_commit.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. commit_do
  2. commit_all
  3. commit
  4. commit_connect
  5. commit_open
  6. commit_write
  7. commit_pwrite
  8. commit_close
  9. commit_ftruncate
  10. vfs_commit_init

   1 /*
   2  * Copyright (c) James Peach 2006, 2007
   3  * Copyright (c) David Losada Carballo 2007
   4  *
   5  * This program is free software; you can redistribute it and/or modify
   6  * it under the terms of the GNU General Public License as published by
   7  * the Free Software Foundation; either version 3 of the License, or
   8  * (at your option) any later version.
   9  *
  10  * This program is distributed in the hope that it will be useful,
  11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  * GNU General Public License for more details.
  14  *
  15  * You should have received a copy of the GNU General Public License
  16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17  */
  18 
  19 #include "includes.h"
  20 
  21 /* Commit data module.
  22  *
  23  * The purpose of this module is to flush data to disk at regular intervals,
  24  * just like the NFS commit operation. There's two rationales for this. First,
  25  * it minimises the data loss in case of a power outage without incurring
  26  * the poor performance of synchronous I/O. Second, a steady flush rate
  27  * can produce better throughput than suddenly dumping massive amounts of
  28  * writes onto a disk.
  29  *
  30  * Tunables:
  31  *
  32  *  commit: dthresh         Amount of dirty data that can accumulate
  33  *                          before we commit (sync) it.
  34  *
  35  *  commit: debug           Debug level at which to emit messages.
  36  *
  37  *  commit: eof mode        String. Tunes how the module tries to guess when
  38  *                          the client has written the last bytes of the file.
  39  *                          Possible values (default = hinted):
  40  *
  41  *     (*)  = hinted        Some clients (i.e. Windows Explorer) declare the
  42  *                          size of the file before transferring it. With this
  43  *                          option, we remember that hint, and commit after
  44  *                          writing in that file position. If the client
  45  *                          doesn't declare the size of file, commiting on EOF 
  46  *                          is not triggered.
  47  *
  48  *          = growth        Commits after a write operation has made the file
  49  *                          size grow. If the client declares a file size, it
  50  *                          refrains to commit until the file has reached it.
  51  *                          Useful for defeating writeback on NFS shares.
  52  *
  53  */
  54 
  55 #define MODULE "commit"
  56 
  57 static int module_debug;
  58 
  59 enum eof_mode
  60 {
  61     EOF_NONE = 0x0000,
  62     EOF_HINTED = 0x0001,
  63     EOF_GROWTH = 0x0002
  64 };
  65 
  66 struct commit_info
  67 {
  68         /* For chunk-based commits */
  69         SMB_OFF_T dbytes;       /* Dirty (uncommitted) bytes */
  70         SMB_OFF_T dthresh;      /* Dirty data threshold */
  71         /* For commits on EOF */
  72         enum eof_mode on_eof;
  73         SMB_OFF_T eof;          /* Expected file size */
  74 };
  75 
  76 static int commit_do(
     /* [<][>][^][v][top][bottom][index][help] */
  77         struct commit_info *            c,
  78         int                             fd)
  79 {
  80         int result;
  81 
  82         DEBUG(module_debug,
  83                 ("%s: flushing %lu dirty bytes\n",
  84                  MODULE, (unsigned long)c->dbytes));
  85 
  86 #if HAVE_FDATASYNC
  87         result = fdatasync(fd);
  88 #elif HAVE_FSYNC
  89         result = fsync(fd);
  90 #else
  91         result = 0
  92 #endif
  93         if (result == 0) {
  94                 c->dbytes = 0;  /* on success, no dirty bytes */
  95         }
  96         return result;
  97 }
  98 
  99 static int commit_all(
     /* [<][>][^][v][top][bottom][index][help] */
 100         struct vfs_handle_struct *      handle,
 101         files_struct *                  fsp)
 102 {
 103         struct commit_info *c;
 104 
 105         if ((c = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
 106                 if (c->dbytes) {
 107                         DEBUG(module_debug,
 108                                 ("%s: flushing %lu dirty bytes\n",
 109                                  MODULE, (unsigned long)c->dbytes));
 110 
 111                         return commit_do(c, fsp->fh->fd);
 112                 }
 113         }
 114         return 0;
 115 }
 116 
 117 static int commit(
     /* [<][>][^][v][top][bottom][index][help] */
 118         struct vfs_handle_struct *      handle,
 119         files_struct *                  fsp,
 120         SMB_OFF_T                       offset,
 121         ssize_t                         last_write)
 122 {
 123         struct commit_info *c;
 124 
 125         if ((c = VFS_FETCH_FSP_EXTENSION(handle, fsp)) == NULL) {
 126                 return 0;
 127         }
 128 
 129         c->dbytes += last_write;        /* dirty bytes always counted */
 130 
 131         if (c->dthresh && (c->dbytes > c->dthresh)) {
 132                 return commit_do(c, fsp->fh->fd);
 133         }
 134 
 135         /* Return if we are not in EOF mode or if we have temporarily opted
 136          * out of it.
 137          */
 138         if (c->on_eof == EOF_NONE || c->eof < 0) {
 139                 return 0;
 140         }
 141 
 142         /* This write hit or went past our cache the file size. */
 143         if ((offset + last_write) >= c->eof) {
 144                 if (commit_do(c, fsp->fh->fd) == -1) {
 145                         return -1;
 146                 }
 147 
 148                 /* Hinted mode only commits the first time we hit EOF. */
 149                 if (c->on_eof == EOF_HINTED) {
 150                     c->eof = -1;
 151                 } else if (c->on_eof == EOF_GROWTH) {
 152                     c->eof = offset + last_write;
 153                 }
 154         }
 155 
 156         return 0;
 157 }
 158 
 159 static int commit_connect(
     /* [<][>][^][v][top][bottom][index][help] */
 160         struct vfs_handle_struct *  handle,
 161         const char *                service,
 162         const char *                user)
 163 {
 164         module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100);
 165         return SMB_VFS_NEXT_CONNECT(handle, service, user);
 166 }
 167 
 168 static int commit_open(
     /* [<][>][^][v][top][bottom][index][help] */
 169         vfs_handle_struct * handle,
 170         const char *        fname,
 171         files_struct *      fsp,
 172         int                 flags,
 173         mode_t              mode)
 174 {
 175         SMB_OFF_T dthresh;
 176         const char *eof_mode;
 177         struct commit_info *c = NULL;
 178         int fd;
 179 
 180         /* Don't bother with read-only files. */
 181         if ((flags & O_ACCMODE) == O_RDONLY) {
 182                 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
 183         }
 184 
 185         /* Read and check module configuration */
 186         dthresh = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
 187                                         MODULE, "dthresh", NULL));
 188 
 189         eof_mode = lp_parm_const_string(SNUM(handle->conn),
 190                                         MODULE, "eof mode", "none");
 191 
 192         if (dthresh > 0 || !strequal(eof_mode, "none")) {
 193                 c = VFS_ADD_FSP_EXTENSION(handle, fsp, struct commit_info, NULL);
 194                 /* Process main tunables */
 195                 if (c) {
 196                         c->dthresh = dthresh;
 197                         c->dbytes = 0;
 198                         c->on_eof = EOF_NONE;
 199                         c->eof = 0;
 200                 }
 201         }
 202         /* Process eof_mode tunable */
 203         if (c) {
 204                 if (strequal(eof_mode, "hinted")) {
 205                         c->on_eof = EOF_HINTED;
 206                 } else if (strequal(eof_mode, "growth")) {
 207                         c->on_eof = EOF_GROWTH;
 208                 }
 209         }
 210 
 211         fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
 212         if (fd == -1) {
 213                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
 214                 return fd;
 215         }
 216 
 217         /* EOF commit modes require us to know the initial file size. */
 218         if (c && (c->on_eof != EOF_NONE)) {
 219                 SMB_STRUCT_STAT st;
 220                 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
 221                         return -1;
 222                 }
 223                 c->eof = st.st_size;
 224         }
 225 
 226         return 0;
 227 }
 228 
 229 static ssize_t commit_write(
     /* [<][>][^][v][top][bottom][index][help] */
 230         vfs_handle_struct * handle,
 231         files_struct *      fsp,
 232         void *              data,
 233         size_t              count)
 234 {
 235         ssize_t ret;
 236         ret = SMB_VFS_NEXT_WRITE(handle, fsp, data, count);
 237 
 238         if (ret > 0) {
 239                 if (commit(handle, fsp, fsp->fh->pos, ret) == -1) {
 240                         return -1;
 241                 }
 242         }
 243 
 244         return ret;
 245 }
 246 
 247 static ssize_t commit_pwrite(
     /* [<][>][^][v][top][bottom][index][help] */
 248         vfs_handle_struct * handle,
 249         files_struct *      fsp,
 250         void *              data,
 251         size_t              count,
 252         SMB_OFF_T           offset)
 253 {
 254         ssize_t ret;
 255 
 256         ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, count, offset);
 257         if (ret > 0) {
 258                 if (commit(handle, fsp, offset, ret) == -1) {
 259                         return -1;
 260                 }
 261         }
 262 
 263         return ret;
 264 }
 265 
 266 static int commit_close(
     /* [<][>][^][v][top][bottom][index][help] */
 267         vfs_handle_struct * handle,
 268         files_struct *      fsp)
 269 {
 270         /* Commit errors not checked, close() will find them again */
 271         commit_all(handle, fsp);
 272         return SMB_VFS_NEXT_CLOSE(handle, fsp);
 273 }
 274 
 275 static int commit_ftruncate(
     /* [<][>][^][v][top][bottom][index][help] */
 276         vfs_handle_struct * handle,
 277         files_struct *      fsp,
 278         SMB_OFF_T           len)
 279 {
 280         int result;
 281 
 282         result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
 283         if (result == 0) {
 284                 struct commit_info *c;
 285                 if ((c = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
 286                         commit(handle, fsp, len, 0);
 287                         c->eof = len;
 288                 }
 289         }
 290 
 291         return result;
 292 }
 293 
 294 static vfs_op_tuple commit_ops [] =
 295 {
 296         {SMB_VFS_OP(commit_open),
 297                 SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
 298         {SMB_VFS_OP(commit_close),
 299                 SMB_VFS_OP_CLOSE, SMB_VFS_LAYER_TRANSPARENT},
 300         {SMB_VFS_OP(commit_write),
 301                 SMB_VFS_OP_WRITE, SMB_VFS_LAYER_TRANSPARENT},
 302         {SMB_VFS_OP(commit_pwrite),
 303                 SMB_VFS_OP_PWRITE, SMB_VFS_LAYER_TRANSPARENT},
 304         {SMB_VFS_OP(commit_connect),
 305                 SMB_VFS_OP_CONNECT,  SMB_VFS_LAYER_TRANSPARENT},
 306         {SMB_VFS_OP(commit_ftruncate),
 307                 SMB_VFS_OP_FTRUNCATE,  SMB_VFS_LAYER_TRANSPARENT},
 308 
 309         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
 310 };
 311 
 312 NTSTATUS vfs_commit_init(void);
 313 NTSTATUS vfs_commit_init(void)
     /* [<][>][^][v][top][bottom][index][help] */
 314 {
 315         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE, commit_ops);
 316 }
 317 
 318 

/* [<][>][^][v][top][bottom][index][help] */