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/>.
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
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
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
;
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 */
73 SMB_OFF_T eof
; /* Expected file size */
77 struct commit_info
* c
,
83 ("%s: flushing %lu dirty bytes\n",
84 MODULE
, (unsigned long)c
->dbytes
));
87 result
= fdatasync(fd
);
94 c
->dbytes
= 0; /* on success, no dirty bytes */
99 static int commit_all(
100 struct vfs_handle_struct
* handle
,
103 struct commit_info
*c
;
105 if ((c
= (struct commit_info
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
))) {
108 ("%s: flushing %lu dirty bytes\n",
109 MODULE
, (unsigned long)c
->dbytes
));
111 return commit_do(c
, fsp
->fh
->fd
);
118 struct vfs_handle_struct
* handle
,
123 struct commit_info
*c
;
125 if ((c
= (struct commit_info
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
))
130 c
->dbytes
+= last_write
; /* dirty bytes always counted */
132 if (c
->dthresh
&& (c
->dbytes
> c
->dthresh
)) {
133 return commit_do(c
, fsp
->fh
->fd
);
136 /* Return if we are not in EOF mode or if we have temporarily opted
139 if (c
->on_eof
== EOF_NONE
|| c
->eof
< 0) {
143 /* This write hit or went past our cache the file size. */
144 if ((offset
+ last_write
) >= c
->eof
) {
145 if (commit_do(c
, fsp
->fh
->fd
) == -1) {
149 /* Hinted mode only commits the first time we hit EOF. */
150 if (c
->on_eof
== EOF_HINTED
) {
152 } else if (c
->on_eof
== EOF_GROWTH
) {
153 c
->eof
= offset
+ last_write
;
160 static int commit_connect(
161 struct vfs_handle_struct
* handle
,
162 const char * service
,
165 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
171 module_debug
= lp_parm_int(SNUM(handle
->conn
), MODULE
, "debug", 100);
175 static int commit_open(
176 vfs_handle_struct
* handle
,
177 struct smb_filename
*smb_fname
,
183 const char *eof_mode
;
184 struct commit_info
*c
= NULL
;
187 /* Don't bother with read-only files. */
188 if ((flags
& O_ACCMODE
) == O_RDONLY
) {
189 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
192 /* Read and check module configuration */
193 dthresh
= conv_str_size(lp_parm_const_string(SNUM(handle
->conn
),
194 MODULE
, "dthresh", NULL
));
196 eof_mode
= lp_parm_const_string(SNUM(handle
->conn
),
197 MODULE
, "eof mode", "none");
199 if (dthresh
> 0 || !strequal(eof_mode
, "none")) {
200 c
= (struct commit_info
*)VFS_ADD_FSP_EXTENSION(
201 handle
, fsp
, struct commit_info
, NULL
);
202 /* Process main tunables */
204 c
->dthresh
= dthresh
;
206 c
->on_eof
= EOF_NONE
;
210 /* Process eof_mode tunable */
212 if (strequal(eof_mode
, "hinted")) {
213 c
->on_eof
= EOF_HINTED
;
214 } else if (strequal(eof_mode
, "growth")) {
215 c
->on_eof
= EOF_GROWTH
;
219 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
221 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
225 /* EOF commit modes require us to know the initial file size. */
226 if (c
&& (c
->on_eof
!= EOF_NONE
)) {
228 if (SMB_VFS_FSTAT(fsp
, &st
) == -1) {
231 c
->eof
= st
.st_ex_size
;
237 static ssize_t
commit_write(
238 vfs_handle_struct
* handle
,
244 ret
= SMB_VFS_NEXT_WRITE(handle
, fsp
, data
, count
);
247 if (commit(handle
, fsp
, fsp
->fh
->pos
, ret
) == -1) {
255 static ssize_t
commit_pwrite(
256 vfs_handle_struct
* handle
,
264 ret
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, count
, offset
);
266 if (commit(handle
, fsp
, offset
, ret
) == -1) {
274 static int commit_close(
275 vfs_handle_struct
* handle
,
278 /* Commit errors not checked, close() will find them again */
279 commit_all(handle
, fsp
);
280 return SMB_VFS_NEXT_CLOSE(handle
, fsp
);
283 static int commit_ftruncate(
284 vfs_handle_struct
* handle
,
290 result
= SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, len
);
292 struct commit_info
*c
;
293 if ((c
= (struct commit_info
*)VFS_FETCH_FSP_EXTENSION(
295 commit(handle
, fsp
, len
, 0);
303 static struct vfs_fn_pointers vfs_commit_fns
= {
305 .close_fn
= commit_close
,
306 .write
= commit_write
,
307 .pwrite
= commit_pwrite
,
308 .connect_fn
= commit_connect
,
309 .ftruncate
= commit_ftruncate
312 NTSTATUS
vfs_commit_init(void);
313 NTSTATUS
vfs_commit_init(void)
315 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, MODULE
,