| 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(
|
|---|
| 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
|
|---|
|
|---|