param: tidy up get_parametrics_by_service
[Samba.git] / source3 / modules / vfs_commit.c
bloba6bc2a4a65621eca06284745bd4231e072aca6e4
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"
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
31 * writes onto a disk.
33 * Tunables:
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, commiting on EOF
49 * is not triggered.
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;
62 enum eof_mode
64 EOF_NONE = 0x0000,
65 EOF_HINTED = 0x0001,
66 EOF_GROWTH = 0x0002
69 struct commit_info
71 /* For chunk-based commits */
72 off_t dbytes; /* Dirty (uncommitted) bytes */
73 off_t dthresh; /* Dirty data threshold */
74 /* For commits on EOF */
75 enum eof_mode on_eof;
76 off_t eof; /* Expected file size */
79 static int commit_do(
80 struct commit_info * c,
81 int fd)
83 int result;
85 DEBUG(module_debug,
86 ("%s: flushing %lu dirty bytes\n",
87 MODULE, (unsigned long)c->dbytes));
89 #if HAVE_FDATASYNC
90 result = fdatasync(fd);
91 #elif HAVE_FSYNC
92 result = fsync(fd);
93 #else
94 DEBUG(0, ("%s: WARNING: no commit support on this platform\n",
95 MODULE));
96 result = 0
97 #endif
98 if (result == 0) {
99 c->dbytes = 0; /* on success, no dirty bytes */
101 return result;
104 static int commit_all(
105 struct vfs_handle_struct * handle,
106 files_struct * fsp)
108 struct commit_info *c;
110 if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
111 if (c->dbytes) {
112 DEBUG(module_debug,
113 ("%s: flushing %lu dirty bytes\n",
114 MODULE, (unsigned long)c->dbytes));
116 return commit_do(c, fsp->fh->fd);
119 return 0;
122 static int commit(
123 struct vfs_handle_struct * handle,
124 files_struct * fsp,
125 off_t offset,
126 ssize_t last_write)
128 struct commit_info *c;
130 if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(handle, fsp))
131 == NULL) {
132 return 0;
135 c->dbytes += last_write; /* dirty bytes always counted */
137 if (c->dthresh && (c->dbytes > c->dthresh)) {
138 return commit_do(c, fsp->fh->fd);
141 /* Return if we are not in EOF mode or if we have temporarily opted
142 * out of it.
144 if (c->on_eof == EOF_NONE || c->eof < 0) {
145 return 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->fh->fd) == -1) {
151 return -1;
154 /* Hinted mode only commits the first time we hit EOF. */
155 if (c->on_eof == EOF_HINTED) {
156 c->eof = -1;
157 } else if (c->on_eof == EOF_GROWTH) {
158 c->eof = offset + last_write;
162 return 0;
165 static int commit_connect(
166 struct vfs_handle_struct * handle,
167 const char * service,
168 const char * user)
170 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
172 if (ret < 0) {
173 return ret;
176 module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100);
177 return 0;
180 static int commit_open(
181 vfs_handle_struct * handle,
182 struct smb_filename *smb_fname,
183 files_struct * fsp,
184 int flags,
185 mode_t mode)
187 off_t dthresh;
188 const char *eof_mode;
189 struct commit_info *c = NULL;
190 int fd;
192 /* Don't bother with read-only files. */
193 if ((flags & O_ACCMODE) == O_RDONLY) {
194 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
197 /* Read and check module configuration */
198 dthresh = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
199 MODULE, "dthresh", NULL));
201 eof_mode = lp_parm_const_string(SNUM(handle->conn),
202 MODULE, "eof mode", "none");
204 if (dthresh > 0 || !strequal(eof_mode, "none")) {
205 c = (struct commit_info *)VFS_ADD_FSP_EXTENSION(
206 handle, fsp, struct commit_info, NULL);
207 /* Process main tunables */
208 if (c) {
209 c->dthresh = dthresh;
210 c->dbytes = 0;
211 c->on_eof = EOF_NONE;
212 c->eof = 0;
215 /* Process eof_mode tunable */
216 if (c) {
217 if (strequal(eof_mode, "hinted")) {
218 c->on_eof = EOF_HINTED;
219 } else if (strequal(eof_mode, "growth")) {
220 c->on_eof = EOF_GROWTH;
224 fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
225 if (fd == -1) {
226 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
227 return fd;
230 /* EOF commit modes require us to know the initial file size. */
231 if (c && (c->on_eof != EOF_NONE)) {
232 SMB_STRUCT_STAT st;
233 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
234 return -1;
236 c->eof = st.st_ex_size;
239 return fd;
242 static ssize_t commit_write(
243 vfs_handle_struct * handle,
244 files_struct * fsp,
245 const void * data,
246 size_t count)
248 ssize_t ret;
249 ret = SMB_VFS_NEXT_WRITE(handle, fsp, data, count);
251 if (ret > 0) {
252 if (commit(handle, fsp, fsp->fh->pos, ret) == -1) {
253 return -1;
257 return ret;
260 static ssize_t commit_pwrite(
261 vfs_handle_struct * handle,
262 files_struct * fsp,
263 const void * data,
264 size_t count,
265 off_t offset)
267 ssize_t ret;
269 ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, count, offset);
270 if (ret > 0) {
271 if (commit(handle, fsp, offset, ret) == -1) {
272 return -1;
276 return ret;
279 struct commit_pwrite_state {
280 struct vfs_handle_struct *handle;
281 struct files_struct *fsp;
282 ssize_t ret;
283 int err;
286 static void commit_pwrite_written(struct tevent_req *subreq);
288 static struct tevent_req *commit_pwrite_send(struct vfs_handle_struct *handle,
289 TALLOC_CTX *mem_ctx,
290 struct tevent_context *ev,
291 struct files_struct *fsp,
292 const void *data,
293 size_t n, off_t offset)
295 struct tevent_req *req, *subreq;
296 struct commit_pwrite_state *state;
298 req = tevent_req_create(mem_ctx, &state, struct commit_pwrite_state);
299 if (req == NULL) {
300 return NULL;
302 state->handle = handle;
303 state->fsp = fsp;
305 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
306 n, offset);
307 if (tevent_req_nomem(subreq, req)) {
308 return tevent_req_post(req, ev);
310 tevent_req_set_callback(subreq, commit_pwrite_written, req);
311 return req;
314 static void commit_pwrite_written(struct tevent_req *subreq)
316 struct tevent_req *req = tevent_req_callback_data(
317 subreq, struct tevent_req);
318 struct commit_pwrite_state *state = tevent_req_data(
319 req, struct commit_pwrite_state);
320 int commit_ret;
322 state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->err);
323 TALLOC_FREE(subreq);
325 if (state->ret <= 0) {
326 tevent_req_done(req);
327 return;
331 * Ok, this is a sync fake. We should make the sync async as well, but
332 * I'm too lazy for that right now -- vl
334 commit_ret = commit(state->handle, state->fsp, state->fsp->fh->pos,
335 state->ret);
337 if (commit_ret == -1) {
338 state->ret = -1;
341 tevent_req_done(req);
344 static ssize_t commit_pwrite_recv(struct tevent_req *req, int *err)
346 struct commit_pwrite_state *state =
347 tevent_req_data(req, struct commit_pwrite_state);
349 if (tevent_req_is_unix_error(req, err)) {
350 return -1;
352 *err = state->err;
353 return state->ret;
356 static int commit_close(
357 vfs_handle_struct * handle,
358 files_struct * fsp)
360 /* Commit errors not checked, close() will find them again */
361 commit_all(handle, fsp);
362 return SMB_VFS_NEXT_CLOSE(handle, fsp);
365 static int commit_ftruncate(
366 vfs_handle_struct * handle,
367 files_struct * fsp,
368 off_t len)
370 int result;
372 result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
373 if (result == 0) {
374 struct commit_info *c;
375 if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(
376 handle, fsp))) {
377 commit(handle, fsp, len, 0);
378 c->eof = len;
382 return result;
385 static struct vfs_fn_pointers vfs_commit_fns = {
386 .open_fn = commit_open,
387 .close_fn = commit_close,
388 .write_fn = commit_write,
389 .pwrite_fn = commit_pwrite,
390 .pwrite_send_fn = commit_pwrite_send,
391 .pwrite_recv_fn = commit_pwrite_recv,
392 .connect_fn = commit_connect,
393 .ftruncate_fn = commit_ftruncate
396 NTSTATUS vfs_commit_init(void);
397 NTSTATUS vfs_commit_init(void)
399 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
400 &vfs_commit_fns);