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/>.
20 #include "system/filesys.h"
21 #include "smbd/smbd.h"
22 #include "lib/util/tevent_unix.h"
24 /* Commit data module.
26 * The purpose of this module is to flush data to disk at regular intervals,
27 * just like the NFS commit operation. There's two rationales for this. First,
28 * it minimises the data loss in case of a power outage without incurring
29 * the poor performance of synchronous I/O. Second, a steady flush rate
30 * can produce better throughput than suddenly dumping massive amounts of
35 * commit: dthresh Amount of dirty data that can accumulate
36 * before we commit (sync) it.
38 * commit: debug Debug level at which to emit messages.
40 * commit: eof mode String. Tunes how the module tries to guess when
41 * the client has written the last bytes of the file.
42 * Possible values (default = hinted):
44 * (*) = hinted Some clients (i.e. Windows Explorer) declare the
45 * size of the file before transferring it. With this
46 * option, we remember that hint, and commit after
47 * writing in that file position. If the client
48 * doesn't declare the size of file, committing on EOF
51 * = growth Commits after a write operation has made the file
52 * size grow. If the client declares a file size, it
53 * refrains to commit until the file has reached it.
54 * Useful for defeating writeback on NFS shares.
58 #define MODULE "commit"
60 static int module_debug
;
71 /* For chunk-based commits */
72 off_t dbytes
; /* Dirty (uncommitted) bytes */
73 off_t dthresh
; /* Dirty data threshold */
74 /* For commits on EOF */
76 off_t eof
; /* Expected file size */
80 struct commit_info
* c
,
86 ("%s: flushing %lu dirty bytes\n",
87 MODULE
, (unsigned long)c
->dbytes
));
89 #if defined(HAVE_FDATASYNC)
90 result
= fdatasync(fd
);
91 #elif defined(HAVE_FSYNC)
94 DEBUG(0, ("%s: WARNING: no commit support on this platform\n",
99 c
->dbytes
= 0; /* on success, no dirty bytes */
104 static int commit_all(
105 struct vfs_handle_struct
* handle
,
108 struct commit_info
*c
;
110 if ((c
= (struct commit_info
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
))) {
113 ("%s: flushing %lu dirty bytes\n",
114 MODULE
, (unsigned long)c
->dbytes
));
116 return commit_do(c
, fsp_get_io_fd(fsp
));
123 struct vfs_handle_struct
* handle
,
128 struct commit_info
*c
;
130 if ((c
= (struct commit_info
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
))
135 c
->dbytes
+= last_write
; /* dirty bytes always counted */
137 if (c
->dthresh
&& (c
->dbytes
> c
->dthresh
)) {
138 return commit_do(c
, fsp_get_io_fd(fsp
));
141 /* Return if we are not in EOF mode or if we have temporarily opted
144 if (c
->on_eof
== EOF_NONE
|| c
->eof
< 0) {
148 /* This write hit or went past our cache the file size. */
149 if ((offset
+ last_write
) >= c
->eof
) {
150 if (commit_do(c
, fsp_get_io_fd(fsp
)) == -1) {
154 /* Hinted mode only commits the first time we hit EOF. */
155 if (c
->on_eof
== EOF_HINTED
) {
157 } else if (c
->on_eof
== EOF_GROWTH
) {
158 c
->eof
= offset
+ last_write
;
165 static int commit_connect(
166 struct vfs_handle_struct
* handle
,
167 const char * service
,
170 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
176 module_debug
= lp_parm_int(SNUM(handle
->conn
), MODULE
, "debug", 100);
180 static int commit_openat(struct vfs_handle_struct
*handle
,
181 const struct files_struct
*dirfsp
,
182 const struct smb_filename
*smb_fname
,
184 const struct vfs_open_how
*how
)
187 const char *eof_mode
;
188 struct commit_info
*c
= NULL
;
191 /* Don't bother with read-only files. */
192 if ((how
->flags
& O_ACCMODE
) == O_RDONLY
) {
193 return SMB_VFS_NEXT_OPENAT(handle
,
200 /* Read and check module configuration */
201 dthresh
= conv_str_size(lp_parm_const_string(SNUM(handle
->conn
),
202 MODULE
, "dthresh", NULL
));
204 eof_mode
= lp_parm_const_string(SNUM(handle
->conn
),
205 MODULE
, "eof mode", "none");
207 if (dthresh
> 0 || !strequal(eof_mode
, "none")) {
208 c
= VFS_ADD_FSP_EXTENSION(
209 handle
, fsp
, struct commit_info
, NULL
);
210 /* Process main tunables */
212 c
->dthresh
= dthresh
;
214 c
->on_eof
= EOF_NONE
;
218 /* Process eof_mode tunable */
220 if (strequal(eof_mode
, "hinted")) {
221 c
->on_eof
= EOF_HINTED
;
222 } else if (strequal(eof_mode
, "growth")) {
223 c
->on_eof
= EOF_GROWTH
;
227 fd
= SMB_VFS_NEXT_OPENAT(handle
, dirfsp
, smb_fname
, fsp
, how
);
229 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
233 /* EOF commit modes require us to know the initial file size. */
234 if (c
&& (c
->on_eof
!= EOF_NONE
)) {
237 * Setting the fd of the FSP is a hack
238 * but also practiced elsewhere -
239 * needed for calling the VFS.
242 if (SMB_VFS_FSTAT(fsp
, &st
) == -1) {
243 int saved_errno
= errno
;
249 c
->eof
= st
.st_ex_size
;
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 struct commit_pwrite_state
{
275 struct vfs_handle_struct
*handle
;
276 struct files_struct
*fsp
;
278 struct vfs_aio_state vfs_aio_state
;
281 static void commit_pwrite_written(struct tevent_req
*subreq
);
283 static struct tevent_req
*commit_pwrite_send(struct vfs_handle_struct
*handle
,
285 struct tevent_context
*ev
,
286 struct files_struct
*fsp
,
288 size_t n
, off_t offset
)
290 struct tevent_req
*req
, *subreq
;
291 struct commit_pwrite_state
*state
;
293 req
= tevent_req_create(mem_ctx
, &state
, struct commit_pwrite_state
);
297 state
->handle
= handle
;
300 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
, data
,
302 if (tevent_req_nomem(subreq
, req
)) {
303 return tevent_req_post(req
, ev
);
305 tevent_req_set_callback(subreq
, commit_pwrite_written
, req
);
309 static void commit_pwrite_written(struct tevent_req
*subreq
)
311 struct tevent_req
*req
= tevent_req_callback_data(
312 subreq
, struct tevent_req
);
313 struct commit_pwrite_state
*state
= tevent_req_data(
314 req
, struct commit_pwrite_state
);
317 state
->ret
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
320 if (state
->ret
<= 0) {
321 tevent_req_done(req
);
326 * Ok, this is a sync fake. We should make the sync async as well, but
327 * I'm too lazy for that right now -- vl
329 commit_ret
= commit(state
->handle
,
331 fh_get_pos(state
->fsp
->fh
),
334 if (commit_ret
== -1) {
338 tevent_req_done(req
);
341 static ssize_t
commit_pwrite_recv(struct tevent_req
*req
,
342 struct vfs_aio_state
*vfs_aio_state
)
344 struct commit_pwrite_state
*state
=
345 tevent_req_data(req
, struct commit_pwrite_state
);
347 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
350 *vfs_aio_state
= state
->vfs_aio_state
;
354 static int commit_close(
355 vfs_handle_struct
* handle
,
358 /* Commit errors not checked, close() will find them again */
359 commit_all(handle
, fsp
);
360 return SMB_VFS_NEXT_CLOSE(handle
, fsp
);
363 static int commit_ftruncate(
364 vfs_handle_struct
* handle
,
370 result
= SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, len
);
372 struct commit_info
*c
;
373 if ((c
= (struct commit_info
*)VFS_FETCH_FSP_EXTENSION(
375 commit(handle
, fsp
, len
, 0);
383 static struct vfs_fn_pointers vfs_commit_fns
= {
384 .openat_fn
= commit_openat
,
385 .close_fn
= commit_close
,
386 .pwrite_fn
= commit_pwrite
,
387 .pwrite_send_fn
= commit_pwrite_send
,
388 .pwrite_recv_fn
= commit_pwrite_recv
,
389 .connect_fn
= commit_connect
,
390 .ftruncate_fn
= commit_ftruncate
394 NTSTATUS
vfs_commit_init(TALLOC_CTX
*ctx
)
396 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, MODULE
,