s3: Change SMB_VFS_OPEN to take an smb_filename struct
[sfrench/samba-autobuild/.git] / source3 / modules / vfs_syncops.c
1 /* 
2  * ensure meta data operations are performed synchronously
3  *
4  * Copyright (C) Andrew Tridgell     2007
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *  
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22
23 /*
24
25   Some filesystems (even some journaled filesystems) require that a
26   fsync() be performed on many meta data operations to ensure that the
27   operation is guaranteed to remain in the filesystem after a power
28   failure. This is particularly important for some cluster filesystems
29   which are participating in a node failover system with clustered
30   Samba
31
32   On those filesystems this module provides a way to perform those
33   operations safely.  
34  */
35
36 /*
37   most of the performance loss with this module is in fsync on close(). 
38   You can disable that with syncops:onclose = no
39  */
40 static bool sync_onclose;
41
42 /*
43   given a filename, find the parent directory
44  */
45 static char *parent_dir(TALLOC_CTX *mem_ctx, const char *name)
46 {
47         const char *p = strrchr(name, '/');
48         if (p == NULL) {
49                 return talloc_strdup(mem_ctx, ".");
50         }
51         return talloc_strndup(mem_ctx, name, (p+1) - name);
52 }
53
54 /*
55   fsync a directory by name
56  */
57 static void syncops_sync_directory(const char *dname)
58 {
59 #ifdef O_DIRECTORY
60         int fd = open(dname, O_DIRECTORY|O_RDONLY);
61         if (fd != -1) {
62                 fsync(fd);
63                 close(fd);
64         }
65 #else
66         DIR *d = opendir(dname);
67         if (d != NULL) {
68                 fsync(dirfd(d));
69                 closedir(d);
70         }
71 #endif
72 }
73
74 /*
75   sync two meta data changes for 2 names
76  */
77 static void syncops_two_names(const char *name1, const char *name2)
78 {
79         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
80         char *parent1, *parent2;
81         parent1 = parent_dir(tmp_ctx, name1);
82         parent2 = parent_dir(tmp_ctx, name2);
83         if (!parent1 || !parent2) {
84                 talloc_free(tmp_ctx);
85                 return;
86         }
87         syncops_sync_directory(parent1);
88         if (strcmp(parent1, parent2) != 0) {
89                 syncops_sync_directory(parent2);                
90         }
91         talloc_free(tmp_ctx);
92 }
93
94 /*
95   sync two meta data changes for 1 names
96  */
97 static void syncops_name(const char *name)
98 {
99         char *parent;
100         parent = parent_dir(NULL, name);
101         if (parent) {
102                 syncops_sync_directory(parent);
103                 talloc_free(parent);
104         }
105 }
106
107 /*
108   sync two meta data changes for 1 names
109  */
110 static void syncops_smb_fname(struct smb_filename *smb_fname)
111 {
112         char *parent;
113         parent = parent_dir(NULL, smb_fname->base_name);
114         if (parent) {
115                 syncops_sync_directory(parent);
116                 talloc_free(parent);
117         }
118 }
119
120
121 /*
122   rename needs special handling, as we may need to fsync two directories
123  */
124 static int syncops_rename(vfs_handle_struct *handle,
125                           const char *oldname, const char *newname)
126 {
127         int ret = SMB_VFS_NEXT_RENAME(handle, oldname, newname);
128         if (ret == 0) {
129                 syncops_two_names(oldname, newname);
130         }
131         return ret;
132 }
133
134 /* handle the rest with a macro */
135 #define SYNCOPS_NEXT(op, fname, args) do {   \
136         int ret = SMB_VFS_NEXT_ ## op args; \
137         if (ret == 0 && fname) syncops_name(fname); \
138         return ret; \
139 } while (0)
140
141 #define SYNCOPS_NEXT_SMB_FNAME(op, fname, args) do {   \
142         int ret = SMB_VFS_NEXT_ ## op args; \
143         if (ret == 0 && fname) syncops_smb_fname(fname); \
144         return ret; \
145 } while (0)
146
147 static int syncops_symlink(vfs_handle_struct *handle,
148                            const char *oldname, const char *newname)
149 {
150         SYNCOPS_NEXT(SYMLINK, newname, (handle, oldname, newname));
151 }
152
153 static int syncops_link(vfs_handle_struct *handle,
154                          const char *oldname, const char *newname)
155 {
156         SYNCOPS_NEXT(LINK, newname, (handle, oldname, newname));
157 }
158
159 static int syncops_open(vfs_handle_struct *handle,
160                         struct smb_filename *smb_fname, files_struct *fsp,
161                         int flags, mode_t mode)
162 {
163         SYNCOPS_NEXT_SMB_FNAME(OPEN, (flags&O_CREAT?smb_fname:NULL),
164                                (handle, smb_fname, fsp, flags, mode));
165 }
166
167 static int syncops_unlink(vfs_handle_struct *handle, const char *fname)
168 {
169         SYNCOPS_NEXT(UNLINK, fname, (handle, fname));
170 }
171
172 static int syncops_mknod(vfs_handle_struct *handle,
173                          const char *fname, mode_t mode, SMB_DEV_T dev)
174 {
175         SYNCOPS_NEXT(MKNOD, fname, (handle, fname, mode, dev));
176 }
177
178 static int syncops_mkdir(vfs_handle_struct *handle,  const char *fname, mode_t mode)
179 {
180         SYNCOPS_NEXT(MKDIR, fname, (handle, fname, mode));
181 }
182
183 static int syncops_rmdir(vfs_handle_struct *handle,  const char *fname)
184 {
185         SYNCOPS_NEXT(RMDIR, fname, (handle, fname));
186 }
187
188 /* close needs to be handled specially */
189 static int syncops_close(vfs_handle_struct *handle, files_struct *fsp)
190 {
191         if (fsp->can_write && sync_onclose) {
192                 /* ideally we'd only do this if we have written some
193                  data, but there is no flag for that in fsp yet. */
194                 fsync(fsp->fh->fd);
195         }
196         return SMB_VFS_NEXT_CLOSE(handle, fsp);
197 }
198
199
200 /* VFS operations structure */
201
202 static vfs_op_tuple syncops_ops[] = {
203         /* directory operations */
204         {SMB_VFS_OP(syncops_mkdir),       SMB_VFS_OP_MKDIR,       SMB_VFS_LAYER_TRANSPARENT},
205         {SMB_VFS_OP(syncops_rmdir),       SMB_VFS_OP_RMDIR,       SMB_VFS_LAYER_TRANSPARENT},
206
207         /* File operations */
208         {SMB_VFS_OP(syncops_open),       SMB_VFS_OP_OPEN,     SMB_VFS_LAYER_TRANSPARENT},
209         {SMB_VFS_OP(syncops_rename),     SMB_VFS_OP_RENAME,   SMB_VFS_LAYER_TRANSPARENT},
210         {SMB_VFS_OP(syncops_unlink),     SMB_VFS_OP_UNLINK,   SMB_VFS_LAYER_TRANSPARENT},
211         {SMB_VFS_OP(syncops_symlink),    SMB_VFS_OP_SYMLINK,  SMB_VFS_LAYER_TRANSPARENT},
212         {SMB_VFS_OP(syncops_link),       SMB_VFS_OP_LINK,     SMB_VFS_LAYER_TRANSPARENT},
213         {SMB_VFS_OP(syncops_mknod),      SMB_VFS_OP_MKNOD,    SMB_VFS_LAYER_TRANSPARENT},
214         {SMB_VFS_OP(syncops_close),      SMB_VFS_OP_CLOSE,    SMB_VFS_LAYER_TRANSPARENT},
215
216         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
217 };
218
219 NTSTATUS vfs_syncops_init(void)
220 {
221         NTSTATUS ret;
222
223         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "syncops", syncops_ops);
224
225         if (!NT_STATUS_IS_OK(ret))
226                 return ret;
227
228         sync_onclose = lp_parm_bool(-1, "syncops", "onclose", true);
229         
230         return ret;
231 }