2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
22 this implements most of the POSIX NTVFS backend
23 This is the default backend
27 #include "vfs_posix.h"
28 #include "librpc/gen_ndr/security.h"
29 #include "../tdb/include/tdb.h"
31 #include "libcli/security/security.h"
32 #include "lib/events/events.h"
33 #include "param/param.h"
36 setup config options for a posix share
38 static void pvfs_setup_options(struct pvfs_state *pvfs)
40 struct share_config *scfg = pvfs->ntvfs->ctx->config;
43 if (share_bool_option(scfg, SHARE_MAP_HIDDEN, SHARE_MAP_HIDDEN_DEFAULT))
44 pvfs->flags |= PVFS_FLAG_MAP_HIDDEN;
45 if (share_bool_option(scfg, SHARE_MAP_ARCHIVE, SHARE_MAP_ARCHIVE_DEFAULT))
46 pvfs->flags |= PVFS_FLAG_MAP_ARCHIVE;
47 if (share_bool_option(scfg, SHARE_MAP_SYSTEM, SHARE_MAP_SYSTEM_DEFAULT))
48 pvfs->flags |= PVFS_FLAG_MAP_SYSTEM;
49 if (share_bool_option(scfg, SHARE_READONLY, SHARE_READONLY_DEFAULT))
50 pvfs->flags |= PVFS_FLAG_READONLY;
51 if (share_bool_option(scfg, SHARE_STRICT_SYNC, SHARE_STRICT_SYNC_DEFAULT))
52 pvfs->flags |= PVFS_FLAG_STRICT_SYNC;
53 if (share_bool_option(scfg, SHARE_STRICT_LOCKING, SHARE_STRICT_LOCKING_DEFAULT))
54 pvfs->flags |= PVFS_FLAG_STRICT_LOCKING;
55 if (share_bool_option(scfg, SHARE_CI_FILESYSTEM, SHARE_CI_FILESYSTEM_DEFAULT))
56 pvfs->flags |= PVFS_FLAG_CI_FILESYSTEM;
57 if (share_bool_option(scfg, PVFS_FAKE_OPLOCKS, PVFS_FAKE_OPLOCKS_DEFAULT))
58 pvfs->flags |= PVFS_FLAG_FAKE_OPLOCKS;
59 if (share_bool_option(scfg, PVFS_AIO, false))
60 pvfs->flags |= PVFS_FLAG_LINUX_AIO;
61 if (share_bool_option(scfg, PVFS_PERM_OVERRIDE, true))
62 pvfs->flags |= PVFS_FLAG_PERM_OVERRIDE;
64 /* file perm options */
65 pvfs->options.create_mask = share_int_option(scfg,
67 SHARE_CREATE_MASK_DEFAULT);
68 pvfs->options.dir_mask = share_int_option(scfg,
70 SHARE_DIR_MASK_DEFAULT);
71 pvfs->options.force_dir_mode = share_int_option(scfg,
73 SHARE_FORCE_DIR_MODE_DEFAULT);
74 pvfs->options.force_create_mode = share_int_option(scfg,
75 SHARE_FORCE_CREATE_MODE,
76 SHARE_FORCE_CREATE_MODE_DEFAULT);
77 /* this must be a power of 2 */
78 pvfs->alloc_size_rounding = share_int_option(scfg,
79 PVFS_ALLOCATION_ROUNDING,
80 PVFS_ALLOCATION_ROUNDING_DEFAULT);
82 pvfs->search.inactivity_time = share_int_option(scfg,
83 PVFS_SEARCH_INACTIVITY,
84 PVFS_SEARCH_INACTIVITY_DEFAULT);
86 #if HAVE_XATTR_SUPPORT
87 if (share_bool_option(scfg, PVFS_XATTR, PVFS_XATTR_DEFAULT))
88 pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
91 pvfs->sharing_violation_delay = share_int_option(scfg,
93 PVFS_SHARE_DELAY_DEFAULT);
95 pvfs->oplock_break_timeout = share_int_option(scfg,
97 PVFS_OPLOCK_TIMEOUT_DEFAULT);
99 pvfs->writetime_delay = share_int_option(scfg,
100 PVFS_WRITETIME_DELAY,
101 PVFS_WRITETIME_DELAY_DEFAULT);
103 pvfs->share_name = talloc_strdup(pvfs, scfg->name);
106 FS_ATTR_CASE_SENSITIVE_SEARCH |
107 FS_ATTR_CASE_PRESERVED_NAMES |
108 FS_ATTR_UNICODE_ON_DISK |
109 FS_ATTR_SPARSE_FILES;
111 /* allow xattrs to be stored in a external tdb */
112 eadb = share_string_option(scfg, PVFS_EADB, NULL);
114 pvfs->ea_db = tdb_wrap_open(pvfs, eadb, 50000,
115 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
116 if (pvfs->ea_db != NULL) {
117 pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
119 DEBUG(0,("Failed to open eadb '%s' - %s\n",
120 eadb, strerror(errno)));
121 pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
125 if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
126 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
128 if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
129 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
132 pvfs->sid_cache.creator_owner = dom_sid_parse_talloc(pvfs, SID_CREATOR_OWNER);
133 pvfs->sid_cache.creator_group = dom_sid_parse_talloc(pvfs, SID_CREATOR_GROUP);
135 /* check if the system really supports xattrs */
136 if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
137 pvfs_xattr_probe(pvfs);
140 /* enable an ACL backend */
141 pvfs->acl_ops = pvfs_acl_backend_byname(share_string_option(scfg, PVFS_ACL, "xattr"));
144 static int pvfs_state_destructor(struct pvfs_state *pvfs)
146 struct pvfs_file *f, *fn;
147 struct pvfs_search_state *s, *sn;
150 * make sure we cleanup files and searches before anything else
151 * because there destructors need to acess the pvfs_state struct
153 for (f=pvfs->files.list; f; f=fn) {
158 for (s=pvfs->search.list; s; s=sn) {
167 connect to a share - used when a tree_connect operation comes
168 in. For a disk based backend we needs to ensure that the base
169 directory exists (tho it doesn't need to be accessible by the user,
172 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
173 struct ntvfs_request *req,
174 union smb_tcon* tcon)
176 struct pvfs_state *pvfs;
178 char *base_directory;
180 const char *sharename;
182 switch (tcon->generic.level) {
184 sharename = tcon->tcon.in.service;
187 sharename = tcon->tconx.in.path;
190 sharename = tcon->smb2.in.path;
193 return NT_STATUS_INVALID_LEVEL;
196 if (strncmp(sharename, "\\\\", 2) == 0) {
197 char *p = strchr(sharename+2, '\\');
204 * TODO: call this from ntvfs_posix_init()
205 * but currently we don't have a lp_ctx there
207 status = pvfs_acl_init(ntvfs->ctx->lp_ctx);
208 NT_STATUS_NOT_OK_RETURN(status);
210 pvfs = talloc_zero(ntvfs, struct pvfs_state);
211 NT_STATUS_HAVE_NO_MEMORY(pvfs);
213 /* for simplicity of path construction, remove any trailing slash now */
214 base_directory = talloc_strdup(pvfs, share_string_option(ntvfs->ctx->config, SHARE_PATH, ""));
215 NT_STATUS_HAVE_NO_MEMORY(base_directory);
216 if (strcmp(base_directory, "/") != 0) {
217 trim_string(base_directory, NULL, "/");
221 pvfs->base_directory = base_directory;
223 /* the directory must exist. Note that we deliberately don't
224 check that it is readable */
225 if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
226 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n",
227 pvfs->base_directory, sharename));
228 return NT_STATUS_BAD_NETWORK_NAME;
231 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
232 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
234 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
235 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
237 if (tcon->generic.level == RAW_TCON_TCONX) {
238 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
239 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
242 ntvfs->private_data = pvfs;
244 pvfs->brl_context = brl_init(pvfs,
245 pvfs->ntvfs->ctx->server_id,
246 pvfs->ntvfs->ctx->lp_ctx,
247 pvfs->ntvfs->ctx->msg_ctx);
248 if (pvfs->brl_context == NULL) {
249 return NT_STATUS_INTERNAL_DB_CORRUPTION;
252 pvfs->odb_context = odb_init(pvfs, pvfs->ntvfs->ctx);
253 if (pvfs->odb_context == NULL) {
254 return NT_STATUS_INTERNAL_DB_CORRUPTION;
257 /* allow this to be NULL - we just disable change notify */
258 pvfs->notify_context = notify_init(pvfs,
259 pvfs->ntvfs->ctx->server_id,
260 pvfs->ntvfs->ctx->msg_ctx,
261 pvfs->ntvfs->ctx->lp_ctx,
262 pvfs->ntvfs->ctx->event_ctx,
263 pvfs->ntvfs->ctx->config);
265 pvfs->wbc_ctx = wbc_init(pvfs,
266 pvfs->ntvfs->ctx->msg_ctx,
267 pvfs->ntvfs->ctx->event_ctx);
268 if (pvfs->wbc_ctx == NULL) {
269 return NT_STATUS_INTERNAL_DB_CORRUPTION;
272 /* allocate the search handle -> ptr tree */
273 pvfs->search.idtree = idr_init(pvfs);
274 NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
276 status = pvfs_mangle_init(pvfs);
277 NT_STATUS_NOT_OK_RETURN(status);
279 pvfs_setup_options(pvfs);
281 talloc_set_destructor(pvfs, pvfs_state_destructor);
284 /* who had the stupid idea to generate a signal on a large
285 file write instead of just failing it!? */
286 BlockSignals(true, SIGXFSZ);
293 disconnect from a share
295 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
301 check if a directory exists
303 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
304 struct ntvfs_request *req,
305 union smb_chkpath *cp)
307 struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
309 struct pvfs_filename *name;
312 /* resolve the cifs name to a posix name */
313 status = pvfs_resolve_name(pvfs, req, cp->chkpath.in.path, 0, &name);
314 NT_STATUS_NOT_OK_RETURN(status);
317 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
320 if (!S_ISDIR(name->st.st_mode)) {
321 return NT_STATUS_NOT_A_DIRECTORY;
330 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
331 struct ntvfs_request *req, struct smb_copy *cp)
333 DEBUG(0,("pvfs_copy not implemented\n"));
334 return NT_STATUS_NOT_SUPPORTED;
338 return print queue info
340 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
341 struct ntvfs_request *req, union smb_lpq *lpq)
343 return NT_STATUS_NOT_SUPPORTED;
346 /* SMBtrans - not used on file shares */
347 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
348 struct ntvfs_request *req, struct smb_trans2 *trans2)
350 return NT_STATUS_ACCESS_DENIED;
354 initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
356 NTSTATUS ntvfs_posix_init(void)
359 struct ntvfs_ops ops;
360 NTVFS_CURRENT_CRITICAL_SIZES(vers);
364 ops.type = NTVFS_DISK;
366 /* fill in all the operations */
367 ops.connect = pvfs_connect;
368 ops.disconnect = pvfs_disconnect;
369 ops.unlink = pvfs_unlink;
370 ops.chkpath = pvfs_chkpath;
371 ops.qpathinfo = pvfs_qpathinfo;
372 ops.setpathinfo = pvfs_setpathinfo;
373 ops.open = pvfs_open;
374 ops.mkdir = pvfs_mkdir;
375 ops.rmdir = pvfs_rmdir;
376 ops.rename = pvfs_rename;
377 ops.copy = pvfs_copy;
378 ops.ioctl = pvfs_ioctl;
379 ops.read = pvfs_read;
380 ops.write = pvfs_write;
381 ops.seek = pvfs_seek;
382 ops.flush = pvfs_flush;
383 ops.close = pvfs_close;
384 ops.exit = pvfs_exit;
385 ops.lock = pvfs_lock;
386 ops.setfileinfo = pvfs_setfileinfo;
387 ops.qfileinfo = pvfs_qfileinfo;
388 ops.fsinfo = pvfs_fsinfo;
390 ops.search_first = pvfs_search_first;
391 ops.search_next = pvfs_search_next;
392 ops.search_close = pvfs_search_close;
393 ops.trans = pvfs_trans;
394 ops.logoff = pvfs_logoff;
395 ops.async_setup = pvfs_async_setup;
396 ops.cancel = pvfs_cancel;
397 ops.notify = pvfs_notify;
399 /* register ourselves with the NTVFS subsystem. We register
400 under the name 'default' as we wish to be the default
401 backend, and also register as 'posix' */
402 ops.name = "default";
403 ret = ntvfs_register(&ops, &vers);
405 if (!NT_STATUS_IS_OK(ret)) {
406 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
410 ret = ntvfs_register(&ops, &vers);
412 if (!NT_STATUS_IS_OK(ret)) {
413 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
416 if (NT_STATUS_IS_OK(ret)) {
417 ret = ntvfs_common_init();