s3-winbindd: Fix typo in comment.
[Samba/vl.git] / source3 / modules / vfs_commit.c
blobcade1170bab445801c31959937710f6e385821c6
1 /*
2 * Copyright (c) James Peach 2006, 2007
3 * Copyright (c) David Losada Carballo 2007
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.
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.
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/>.
19 #include "includes.h"
21 /* Commit data module.
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.
30 * Tunables:
32 * commit: dthresh Amount of dirty data that can accumulate
33 * before we commit (sync) it.
35 * commit: debug Debug level at which to emit messages.
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):
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.
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.
55 #define MODULE "commit"
57 static int module_debug;
59 enum eof_mode
61 EOF_NONE = 0x0000,
62 EOF_HINTED = 0x0001,
63 EOF_GROWTH = 0x0002
66 struct commit_info
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 */
76 static int commit_do(
77 struct commit_info * c,
78 int fd)
80 int result;
82 DEBUG(module_debug,
83 ("%s: flushing %lu dirty bytes\n",
84 MODULE, (unsigned long)c->dbytes));
86 #if HAVE_FDATASYNC
87 result = fdatasync(fd);
88 #elif HAVE_FSYNC
89 result = fsync(fd);
90 #else
91 DEBUG(0, ("%s: WARNING: no commit support on this platform\n",
92 MODULE));
93 result = 0
94 #endif
95 if (result == 0) {
96 c->dbytes = 0; /* on success, no dirty bytes */
98 return result;
101 static int commit_all(
102 struct vfs_handle_struct * handle,
103 files_struct * fsp)
105 struct commit_info *c;
107 if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
108 if (c->dbytes) {
109 DEBUG(module_debug,
110 ("%s: flushing %lu dirty bytes\n",
111 MODULE, (unsigned long)c->dbytes));
113 return commit_do(c, fsp->fh->fd);
116 return 0;
119 static int commit(
120 struct vfs_handle_struct * handle,
121 files_struct * fsp,
122 SMB_OFF_T offset,
123 ssize_t last_write)
125 struct commit_info *c;
127 if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(handle, fsp))
128 == NULL) {
129 return 0;
132 c->dbytes += last_write; /* dirty bytes always counted */
134 if (c->dthresh && (c->dbytes > c->dthresh)) {
135 return commit_do(c, fsp->fh->fd);
138 /* Return if we are not in EOF mode or if we have temporarily opted
139 * out of it.
141 if (c->on_eof == EOF_NONE || c->eof < 0) {
142 return 0;
145 /* This write hit or went past our cache the file size. */
146 if ((offset + last_write) >= c->eof) {
147 if (commit_do(c, fsp->fh->fd) == -1) {
148 return -1;
151 /* Hinted mode only commits the first time we hit EOF. */
152 if (c->on_eof == EOF_HINTED) {
153 c->eof = -1;
154 } else if (c->on_eof == EOF_GROWTH) {
155 c->eof = offset + last_write;
159 return 0;
162 static int commit_connect(
163 struct vfs_handle_struct * handle,
164 const char * service,
165 const char * user)
167 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
169 if (ret < 0) {
170 return ret;
173 module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100);
174 return 0;
177 static int commit_open(
178 vfs_handle_struct * handle,
179 struct smb_filename *smb_fname,
180 files_struct * fsp,
181 int flags,
182 mode_t mode)
184 SMB_OFF_T dthresh;
185 const char *eof_mode;
186 struct commit_info *c = NULL;
187 int fd;
189 /* Don't bother with read-only files. */
190 if ((flags & O_ACCMODE) == O_RDONLY) {
191 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
194 /* Read and check module configuration */
195 dthresh = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
196 MODULE, "dthresh", NULL));
198 eof_mode = lp_parm_const_string(SNUM(handle->conn),
199 MODULE, "eof mode", "none");
201 if (dthresh > 0 || !strequal(eof_mode, "none")) {
202 c = (struct commit_info *)VFS_ADD_FSP_EXTENSION(
203 handle, fsp, struct commit_info, NULL);
204 /* Process main tunables */
205 if (c) {
206 c->dthresh = dthresh;
207 c->dbytes = 0;
208 c->on_eof = EOF_NONE;
209 c->eof = 0;
212 /* Process eof_mode tunable */
213 if (c) {
214 if (strequal(eof_mode, "hinted")) {
215 c->on_eof = EOF_HINTED;
216 } else if (strequal(eof_mode, "growth")) {
217 c->on_eof = EOF_GROWTH;
221 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
222 if (fd == -1) {
223 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
224 return fd;
227 /* EOF commit modes require us to know the initial file size. */
228 if (c && (c->on_eof != EOF_NONE)) {
229 SMB_STRUCT_STAT st;
230 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
231 return -1;
233 c->eof = st.st_ex_size;
236 return 0;
239 static ssize_t commit_write(
240 vfs_handle_struct * handle,
241 files_struct * fsp,
242 const void * data,
243 size_t count)
245 ssize_t ret;
246 ret = SMB_VFS_NEXT_WRITE(handle, fsp, data, count);
248 if (ret > 0) {
249 if (commit(handle, fsp, fsp->fh->pos, ret) == -1) {
250 return -1;
254 return ret;
257 static ssize_t commit_pwrite(
258 vfs_handle_struct * handle,
259 files_struct * fsp,
260 const void * data,
261 size_t count,
262 SMB_OFF_T offset)
264 ssize_t ret;
266 ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, count, offset);
267 if (ret > 0) {
268 if (commit(handle, fsp, offset, ret) == -1) {
269 return -1;
273 return ret;
276 static int commit_close(
277 vfs_handle_struct * handle,
278 files_struct * fsp)
280 /* Commit errors not checked, close() will find them again */
281 commit_all(handle, fsp);
282 return SMB_VFS_NEXT_CLOSE(handle, fsp);
285 static int commit_ftruncate(
286 vfs_handle_struct * handle,
287 files_struct * fsp,
288 SMB_OFF_T len)
290 int result;
292 result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
293 if (result == 0) {
294 struct commit_info *c;
295 if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(
296 handle, fsp))) {
297 commit(handle, fsp, len, 0);
298 c->eof = len;
302 return result;
305 static struct vfs_fn_pointers vfs_commit_fns = {
306 .open = commit_open,
307 .close_fn = commit_close,
308 .write = commit_write,
309 .pwrite = commit_pwrite,
310 .connect_fn = commit_connect,
311 .ftruncate = commit_ftruncate
314 NTSTATUS vfs_commit_init(void);
315 NTSTATUS vfs_commit_init(void)
317 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
318 &vfs_commit_fns);