s4:dsdb: Fix stack use after scope in gkdi_create_root_key()
[samba.git] / source3 / modules / vfs_commit.c
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 #include "smbd/smbd.h"
21
22 /* Commit data module.
23  *
24  * The purpose of this module is to flush data to disk at regular intervals,
25  * just like the NFS commit operation. There's two rationales for this. First,
26  * it minimises the data loss in case of a power outage without incurring
27  * the poor performance of synchronous I/O. Second, a steady flush rate
28  * can produce better throughput than suddenly dumping massive amounts of
29  * writes onto a disk.
30  *
31  * Tunables:
32  *
33  *  commit: dthresh         Amount of dirty data that can accumulate
34  *                          before we commit (sync) it.
35  *
36  *  commit: debug           Debug level at which to emit messages.
37  *
38  *  commit: eof mode        String. Tunes how the module tries to guess when
39  *                          the client has written the last bytes of the file.
40  *                          Possible values (default = hinted):
41  *
42  *     (*)  = hinted        Some clients (i.e. Windows Explorer) declare the
43  *                          size of the file before transferring it. With this
44  *                          option, we remember that hint, and commit after
45  *                          writing in that file position. If the client
46  *                          doesn't declare the size of file, commiting on EOF 
47  *                          is not triggered.
48  *
49  *          = growth        Commits after a write operation has made the file
50  *                          size grow. If the client declares a file size, it
51  *                          refrains to commit until the file has reached it.
52  *                          Useful for defeating writeback on NFS shares.
53  *
54  */
55
56 #define MODULE "commit"
57
58 static int module_debug;
59
60 enum eof_mode
61 {
62     EOF_NONE = 0x0000,
63     EOF_HINTED = 0x0001,
64     EOF_GROWTH = 0x0002
65 };
66
67 struct commit_info
68 {
69         /* For chunk-based commits */
70         SMB_OFF_T dbytes;       /* Dirty (uncommitted) bytes */
71         SMB_OFF_T dthresh;      /* Dirty data threshold */
72         /* For commits on EOF */
73         enum eof_mode on_eof;
74         SMB_OFF_T eof;          /* Expected file size */
75 };
76
77 static int commit_do(
78         struct commit_info *            c,
79         int                             fd)
80 {
81         int result;
82
83         DEBUG(module_debug,
84                 ("%s: flushing %lu dirty bytes\n",
85                  MODULE, (unsigned long)c->dbytes));
86
87 #if HAVE_FDATASYNC
88         result = fdatasync(fd);
89 #elif HAVE_FSYNC
90         result = fsync(fd);
91 #else
92         DEBUG(0, ("%s: WARNING: no commit support on this platform\n",
93                 MODULE));
94         result = 0
95 #endif
96         if (result == 0) {
97                 c->dbytes = 0;  /* on success, no dirty bytes */
98         }
99         return result;
100 }
101
102 static int commit_all(
103         struct vfs_handle_struct *      handle,
104         files_struct *                  fsp)
105 {
106         struct commit_info *c;
107
108         if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
109                 if (c->dbytes) {
110                         DEBUG(module_debug,
111                                 ("%s: flushing %lu dirty bytes\n",
112                                  MODULE, (unsigned long)c->dbytes));
113
114                         return commit_do(c, fsp->fh->fd);
115                 }
116         }
117         return 0;
118 }
119
120 static int commit(
121         struct vfs_handle_struct *      handle,
122         files_struct *                  fsp,
123         SMB_OFF_T                       offset,
124         ssize_t                         last_write)
125 {
126         struct commit_info *c;
127
128         if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(handle, fsp))
129             == NULL) {
130                 return 0;
131         }
132
133         c->dbytes += last_write;        /* dirty bytes always counted */
134
135         if (c->dthresh && (c->dbytes > c->dthresh)) {
136                 return commit_do(c, fsp->fh->fd);
137         }
138
139         /* Return if we are not in EOF mode or if we have temporarily opted
140          * out of it.
141          */
142         if (c->on_eof == EOF_NONE || c->eof < 0) {
143                 return 0;
144         }
145
146         /* This write hit or went past our cache the file size. */
147         if ((offset + last_write) >= c->eof) {
148                 if (commit_do(c, fsp->fh->fd) == -1) {
149                         return -1;
150                 }
151
152                 /* Hinted mode only commits the first time we hit EOF. */
153                 if (c->on_eof == EOF_HINTED) {
154                     c->eof = -1;
155                 } else if (c->on_eof == EOF_GROWTH) {
156                     c->eof = offset + last_write;
157                 }
158         }
159
160         return 0;
161 }
162
163 static int commit_connect(
164         struct vfs_handle_struct *  handle,
165         const char *                service,
166         const char *                user)
167 {
168         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
169
170         if (ret < 0) {
171                 return ret;
172         }
173
174         module_debug = lp_parm_int(SNUM(handle->conn), MODULE, "debug", 100);
175         return 0;
176 }
177
178 static int commit_open(
179         vfs_handle_struct * handle,
180         struct smb_filename *smb_fname,
181         files_struct *      fsp,
182         int                 flags,
183         mode_t              mode)
184 {
185         SMB_OFF_T dthresh;
186         const char *eof_mode;
187         struct commit_info *c = NULL;
188         int fd;
189
190         /* Don't bother with read-only files. */
191         if ((flags & O_ACCMODE) == O_RDONLY) {
192                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
193         }
194
195         /* Read and check module configuration */
196         dthresh = conv_str_size(lp_parm_const_string(SNUM(handle->conn),
197                                         MODULE, "dthresh", NULL));
198
199         eof_mode = lp_parm_const_string(SNUM(handle->conn),
200                                         MODULE, "eof mode", "none");
201
202         if (dthresh > 0 || !strequal(eof_mode, "none")) {
203                 c = (struct commit_info *)VFS_ADD_FSP_EXTENSION(
204                         handle, fsp, struct commit_info, NULL);
205                 /* Process main tunables */
206                 if (c) {
207                         c->dthresh = dthresh;
208                         c->dbytes = 0;
209                         c->on_eof = EOF_NONE;
210                         c->eof = 0;
211                 }
212         }
213         /* Process eof_mode tunable */
214         if (c) {
215                 if (strequal(eof_mode, "hinted")) {
216                         c->on_eof = EOF_HINTED;
217                 } else if (strequal(eof_mode, "growth")) {
218                         c->on_eof = EOF_GROWTH;
219                 }
220         }
221
222         fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
223         if (fd == -1) {
224                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
225                 return fd;
226         }
227
228         /* EOF commit modes require us to know the initial file size. */
229         if (c && (c->on_eof != EOF_NONE)) {
230                 SMB_STRUCT_STAT st;
231                 if (SMB_VFS_FSTAT(fsp, &st) == -1) {
232                         return -1;
233                 }
234                 c->eof = st.st_ex_size;
235         }
236
237         return 0;
238 }
239
240 static ssize_t commit_write(
241         vfs_handle_struct * handle,
242         files_struct *      fsp,
243         const void *        data,
244         size_t              count)
245 {
246         ssize_t ret;
247         ret = SMB_VFS_NEXT_WRITE(handle, fsp, data, count);
248
249         if (ret > 0) {
250                 if (commit(handle, fsp, fsp->fh->pos, ret) == -1) {
251                         return -1;
252                 }
253         }
254
255         return ret;
256 }
257
258 static ssize_t commit_pwrite(
259         vfs_handle_struct * handle,
260         files_struct *      fsp,
261         const void *        data,
262         size_t              count,
263         SMB_OFF_T           offset)
264 {
265         ssize_t ret;
266
267         ret = SMB_VFS_NEXT_PWRITE(handle, fsp, data, count, offset);
268         if (ret > 0) {
269                 if (commit(handle, fsp, offset, ret) == -1) {
270                         return -1;
271                 }
272         }
273
274         return ret;
275 }
276
277 static int commit_close(
278         vfs_handle_struct * handle,
279         files_struct *      fsp)
280 {
281         /* Commit errors not checked, close() will find them again */
282         commit_all(handle, fsp);
283         return SMB_VFS_NEXT_CLOSE(handle, fsp);
284 }
285
286 static int commit_ftruncate(
287         vfs_handle_struct * handle,
288         files_struct *      fsp,
289         SMB_OFF_T           len)
290 {
291         int result;
292
293         result = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, len);
294         if (result == 0) {
295                 struct commit_info *c;
296                 if ((c = (struct commit_info *)VFS_FETCH_FSP_EXTENSION(
297                              handle, fsp))) {
298                         commit(handle, fsp, len, 0);
299                         c->eof = len;
300                 }
301         }
302
303         return result;
304 }
305
306 static struct vfs_fn_pointers vfs_commit_fns = {
307         .open_fn = commit_open,
308         .close_fn = commit_close,
309         .write = commit_write,
310         .pwrite = commit_pwrite,
311         .connect_fn = commit_connect,
312         .ftruncate = commit_ftruncate
313 };
314
315 NTSTATUS vfs_commit_init(void);
316 NTSTATUS vfs_commit_init(void)
317 {
318         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, MODULE,
319                                 &vfs_commit_fns);
320 }
321
322