pvfs: add posix:oplocktimeout=30 option
[kai/samba-autobuild/.git] / source4 / ntvfs / posix / vfs_posix.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 /*
22   this implements most of the POSIX NTVFS backend
23   This is the default backend
24 */
25
26 #include "includes.h"
27 #include "vfs_posix.h"
28 #include "librpc/gen_ndr/security.h"
29 #include "lib/tdb/include/tdb.h"
30 #include "tdb_wrap.h"
31 #include "util/util_ldb.h"
32 #include "libcli/security/security.h"
33 #include "lib/events/events.h"
34 #include "param/param.h"
35
36 /*
37   setup config options for a posix share
38 */
39 static void pvfs_setup_options(struct pvfs_state *pvfs)
40 {
41         struct share_config *scfg = pvfs->ntvfs->ctx->config;
42         const char *eadb;
43
44         if (share_bool_option(scfg, SHARE_MAP_HIDDEN, SHARE_MAP_HIDDEN_DEFAULT))
45                 pvfs->flags |= PVFS_FLAG_MAP_HIDDEN;
46         if (share_bool_option(scfg, SHARE_MAP_ARCHIVE, SHARE_MAP_ARCHIVE_DEFAULT))
47                 pvfs->flags |= PVFS_FLAG_MAP_ARCHIVE;
48         if (share_bool_option(scfg, SHARE_MAP_SYSTEM, SHARE_MAP_SYSTEM_DEFAULT))
49                 pvfs->flags |= PVFS_FLAG_MAP_SYSTEM;
50         if (share_bool_option(scfg, SHARE_READONLY, SHARE_READONLY_DEFAULT))
51                 pvfs->flags |= PVFS_FLAG_READONLY;
52         if (share_bool_option(scfg, SHARE_STRICT_SYNC, SHARE_STRICT_SYNC_DEFAULT))
53                 pvfs->flags |= PVFS_FLAG_STRICT_SYNC;
54         if (share_bool_option(scfg, SHARE_STRICT_LOCKING, SHARE_STRICT_LOCKING_DEFAULT))
55                 pvfs->flags |= PVFS_FLAG_STRICT_LOCKING;
56         if (share_bool_option(scfg, SHARE_CI_FILESYSTEM, SHARE_CI_FILESYSTEM_DEFAULT))
57                 pvfs->flags |= PVFS_FLAG_CI_FILESYSTEM;
58         if (share_bool_option(scfg, PVFS_FAKE_OPLOCKS, PVFS_FAKE_OPLOCKS_DEFAULT))
59                 pvfs->flags |= PVFS_FLAG_FAKE_OPLOCKS;
60         if (share_bool_option(scfg, PVFS_AIO, false))
61                 pvfs->flags |= PVFS_FLAG_LINUX_AIO;
62
63         /* file perm options */
64         pvfs->options.create_mask       = share_int_option(scfg,
65                                                            SHARE_CREATE_MASK,
66                                                            SHARE_CREATE_MASK_DEFAULT);
67         pvfs->options.dir_mask          = share_int_option(scfg,
68                                                            SHARE_DIR_MASK,
69                                                            SHARE_DIR_MASK_DEFAULT);
70         pvfs->options.force_dir_mode    = share_int_option(scfg,
71                                                            SHARE_FORCE_DIR_MODE,
72                                                            SHARE_FORCE_DIR_MODE_DEFAULT);
73         pvfs->options.force_create_mode = share_int_option(scfg,
74                                                            SHARE_FORCE_CREATE_MODE,
75                                                            SHARE_FORCE_CREATE_MODE_DEFAULT);
76         /* this must be a power of 2 */
77         pvfs->alloc_size_rounding = share_int_option(scfg,
78                                                         PVFS_ALLOCATION_ROUNDING,
79                                                         PVFS_ALLOCATION_ROUNDING_DEFAULT);
80
81         pvfs->search.inactivity_time = share_int_option(scfg,
82                                                         PVFS_SEARCH_INACTIVITY,
83                                                         PVFS_SEARCH_INACTIVITY_DEFAULT);
84
85 #if HAVE_XATTR_SUPPORT
86         if (share_bool_option(scfg, PVFS_XATTR, PVFS_XATTR_DEFAULT))
87                 pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
88 #endif
89
90         pvfs->sharing_violation_delay = share_int_option(scfg,
91                                                         PVFS_SHARE_DELAY,
92                                                         PVFS_SHARE_DELAY_DEFAULT);
93
94         pvfs->oplock_break_timeout = share_int_option(scfg,
95                                                       PVFS_OPLOCK_TIMEOUT,
96                                                       PVFS_OPLOCK_TIMEOUT_DEFAULT);
97
98         pvfs->share_name = talloc_strdup(pvfs, scfg->name);
99
100         pvfs->fs_attribs = 
101                 FS_ATTR_CASE_SENSITIVE_SEARCH | 
102                 FS_ATTR_CASE_PRESERVED_NAMES |
103                 FS_ATTR_UNICODE_ON_DISK |
104                 FS_ATTR_SPARSE_FILES;
105
106         /* allow xattrs to be stored in a external tdb */
107         eadb = share_string_option(scfg, PVFS_EADB, NULL);
108         if (eadb != NULL) {
109                 pvfs->ea_db = tdb_wrap_open(pvfs, eadb, 50000,  
110                                             TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
111                 if (pvfs->ea_db != NULL) {
112                         pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
113                 } else {
114                         DEBUG(0,("Failed to open eadb '%s' - %s\n",
115                                  eadb, strerror(errno)));
116                         pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
117                 }
118         }
119
120         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
121                 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
122         }
123         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
124                 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
125         }
126
127         pvfs->sid_cache.creator_owner = dom_sid_parse_talloc(pvfs, SID_CREATOR_OWNER);
128         pvfs->sid_cache.creator_group = dom_sid_parse_talloc(pvfs, SID_CREATOR_GROUP);
129
130         /* check if the system really supports xattrs */
131         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
132                 pvfs_xattr_probe(pvfs);
133         }
134
135         /* enable an ACL backend */
136         pvfs->acl_ops = pvfs_acl_backend_byname(share_string_option(scfg, PVFS_ACL, "xattr"));
137 }
138
139 static int pvfs_state_destructor(struct pvfs_state *pvfs)
140 {
141         struct pvfs_file *f, *fn;
142         struct pvfs_search_state *s, *sn;
143
144         /* 
145          * make sure we cleanup files and searches before anything else
146          * because there destructors need to acess the pvfs_state struct
147          */
148         for (f=pvfs->files.list; f; f=fn) {
149                 fn = f->next;
150                 talloc_free(f);
151         }
152
153         for (s=pvfs->search.list; s; s=sn) {
154                 sn = s->next;
155                 talloc_free(s);
156         }
157
158         return 0;
159 }
160
161 /*
162   connect to a share - used when a tree_connect operation comes
163   in. For a disk based backend we needs to ensure that the base
164   directory exists (tho it doesn't need to be accessible by the user,
165   that comes later)
166 */
167 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
168                              struct ntvfs_request *req, const char *sharename)
169 {
170         struct pvfs_state *pvfs;
171         struct stat st;
172         char *base_directory;
173         NTSTATUS status;
174
175         pvfs = talloc_zero(ntvfs, struct pvfs_state);
176         NT_STATUS_HAVE_NO_MEMORY(pvfs);
177
178         /* for simplicity of path construction, remove any trailing slash now */
179         base_directory = talloc_strdup(pvfs, share_string_option(ntvfs->ctx->config, SHARE_PATH, ""));
180         NT_STATUS_HAVE_NO_MEMORY(base_directory);
181         if (strcmp(base_directory, "/") != 0) {
182                 trim_string(base_directory, NULL, "/");
183         }
184
185         pvfs->ntvfs = ntvfs;
186         pvfs->base_directory = base_directory;
187
188         /* the directory must exist. Note that we deliberately don't
189            check that it is readable */
190         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
191                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
192                          pvfs->base_directory, sharename));
193                 return NT_STATUS_BAD_NETWORK_NAME;
194         }
195
196         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
197         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
198
199         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
200         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
201
202         ntvfs->private_data = pvfs;
203
204         pvfs->brl_context = brl_init(pvfs, 
205                                      pvfs->ntvfs->ctx->server_id,
206                                      pvfs->ntvfs->ctx->lp_ctx,
207                                      pvfs->ntvfs->ctx->msg_ctx);
208         if (pvfs->brl_context == NULL) {
209                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
210         }
211
212         pvfs->odb_context = odb_init(pvfs, pvfs->ntvfs->ctx);
213         if (pvfs->odb_context == NULL) {
214                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
215         }
216
217         /* allow this to be NULL - we just disable change notify */
218         pvfs->notify_context = notify_init(pvfs, 
219                                            pvfs->ntvfs->ctx->server_id,  
220                                            pvfs->ntvfs->ctx->msg_ctx, 
221                                            pvfs->ntvfs->ctx->lp_ctx, 
222                                            event_context_find(pvfs),
223                                            pvfs->ntvfs->ctx->config);
224
225         pvfs->sidmap = sidmap_open(pvfs, pvfs->ntvfs->ctx->lp_ctx);
226         if (pvfs->sidmap == NULL) {
227                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
228         }
229
230         /* allocate the search handle -> ptr tree */
231         pvfs->search.idtree = idr_init(pvfs);
232         NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
233
234         status = pvfs_mangle_init(pvfs);
235         NT_STATUS_NOT_OK_RETURN(status);
236
237         pvfs_setup_options(pvfs);
238
239         talloc_set_destructor(pvfs, pvfs_state_destructor);
240
241 #ifdef SIGXFSZ
242         /* who had the stupid idea to generate a signal on a large
243            file write instead of just failing it!? */
244         BlockSignals(true, SIGXFSZ);
245 #endif
246
247         return NT_STATUS_OK;
248 }
249
250 /*
251   disconnect from a share
252 */
253 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
254 {
255         return NT_STATUS_OK;
256 }
257
258 /*
259   check if a directory exists
260 */
261 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
262                              struct ntvfs_request *req,
263                              union smb_chkpath *cp)
264 {
265         struct pvfs_state *pvfs = ntvfs->private_data;
266         struct pvfs_filename *name;
267         NTSTATUS status;
268
269         /* resolve the cifs name to a posix name */
270         status = pvfs_resolve_name(pvfs, req, cp->chkpath.in.path, 0, &name);
271         NT_STATUS_NOT_OK_RETURN(status);
272
273         if (!name->exists) {
274                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
275         }
276
277         if (!S_ISDIR(name->st.st_mode)) {
278                 return NT_STATUS_NOT_A_DIRECTORY;
279         }
280
281         return NT_STATUS_OK;
282 }
283
284 /*
285   copy a set of files
286 */
287 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
288                           struct ntvfs_request *req, struct smb_copy *cp)
289 {
290         DEBUG(0,("pvfs_copy not implemented\n"));
291         return NT_STATUS_NOT_SUPPORTED;
292 }
293
294 /*
295   return print queue info
296 */
297 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
298                          struct ntvfs_request *req, union smb_lpq *lpq)
299 {
300         return NT_STATUS_NOT_SUPPORTED;
301 }
302
303 /* SMBtrans - not used on file shares */
304 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
305                            struct ntvfs_request *req, struct smb_trans2 *trans2)
306 {
307         return NT_STATUS_ACCESS_DENIED;
308 }
309
310 /*
311   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
312  */
313 NTSTATUS ntvfs_posix_init(void)
314 {
315         NTSTATUS ret;
316         struct ntvfs_ops ops;
317         NTVFS_CURRENT_CRITICAL_SIZES(vers);
318
319         ZERO_STRUCT(ops);
320
321         ops.type = NTVFS_DISK;
322         
323         /* fill in all the operations */
324         ops.connect = pvfs_connect;
325         ops.disconnect = pvfs_disconnect;
326         ops.unlink = pvfs_unlink;
327         ops.chkpath = pvfs_chkpath;
328         ops.qpathinfo = pvfs_qpathinfo;
329         ops.setpathinfo = pvfs_setpathinfo;
330         ops.open = pvfs_open;
331         ops.mkdir = pvfs_mkdir;
332         ops.rmdir = pvfs_rmdir;
333         ops.rename = pvfs_rename;
334         ops.copy = pvfs_copy;
335         ops.ioctl = pvfs_ioctl;
336         ops.read = pvfs_read;
337         ops.write = pvfs_write;
338         ops.seek = pvfs_seek;
339         ops.flush = pvfs_flush; 
340         ops.close = pvfs_close;
341         ops.exit = pvfs_exit;
342         ops.lock = pvfs_lock;
343         ops.setfileinfo = pvfs_setfileinfo;
344         ops.qfileinfo = pvfs_qfileinfo;
345         ops.fsinfo = pvfs_fsinfo;
346         ops.lpq = pvfs_lpq;
347         ops.search_first = pvfs_search_first;
348         ops.search_next = pvfs_search_next;
349         ops.search_close = pvfs_search_close;
350         ops.trans = pvfs_trans;
351         ops.logoff = pvfs_logoff;
352         ops.async_setup = pvfs_async_setup;
353         ops.cancel = pvfs_cancel;
354         ops.notify = pvfs_notify;
355
356         /* register ourselves with the NTVFS subsystem. We register
357            under the name 'default' as we wish to be the default
358            backend, and also register as 'posix' */
359         ops.name = "default";
360         ret = ntvfs_register(&ops, &vers);
361
362         if (!NT_STATUS_IS_OK(ret)) {
363                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
364         }
365
366         ops.name = "posix";
367         ret = ntvfs_register(&ops, &vers);
368
369         if (!NT_STATUS_IS_OK(ret)) {
370                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
371         }
372
373         if (NT_STATUS_IS_OK(ret)) {
374                 ret = ntvfs_common_init();
375         }
376
377         return ret;
378 }