r20624: added AIO read to pvfs backend
[bbaumbach/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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23   this implements most of the POSIX NTVFS backend
24   This is the default backend
25 */
26
27 #include "includes.h"
28 #include "vfs_posix.h"
29 #include "librpc/gen_ndr/security.h"
30 #include "lib/tdb/include/tdb.h"
31 #include "db_wrap.h"
32 #include "libcli/security/security.h"
33 #include "lib/events/events.h"
34
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         /* this must be a power of 2 */
64         pvfs->alloc_size_rounding = share_int_option(scfg,
65                                                         PVFS_ALLOCATION_ROUNDING,
66                                                         PVFS_ALLOCATION_ROUNDING_DEFAULT);
67
68         pvfs->search.inactivity_time = share_int_option(scfg,
69                                                         PVFS_SEARCH_INACTIVITY,
70                                                         PVFS_SEARCH_INACTIVITY_DEFAULT);
71
72 #if HAVE_XATTR_SUPPORT
73         if (share_bool_option(scfg, PVFS_XATTR, PVFS_XATTR_DEFAULT))
74                 pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
75 #endif
76
77         pvfs->sharing_violation_delay = share_int_option(scfg,
78                                                         PVFS_SHARE_DELAY,
79                                                         PVFS_SHARE_DELAY_DEFAULT);
80
81         pvfs->share_name = talloc_strdup(pvfs, scfg->name);
82
83         pvfs->fs_attribs = 
84                 FS_ATTR_CASE_SENSITIVE_SEARCH | 
85                 FS_ATTR_CASE_PRESERVED_NAMES |
86                 FS_ATTR_UNICODE_ON_DISK |
87                 FS_ATTR_SPARSE_FILES;
88
89         /* allow xattrs to be stored in a external tdb */
90         eadb = share_string_option(scfg, PVFS_EADB, NULL);
91         if (eadb != NULL) {
92                 pvfs->ea_db = tdb_wrap_open(pvfs, eadb, 50000,  
93                                             TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
94                 if (pvfs->ea_db != NULL) {
95                         pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
96                 } else {
97                         DEBUG(0,("Failed to open eadb '%s' - %s\n",
98                                  eadb, strerror(errno)));
99                         pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
100                 }
101         }
102
103         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
104                 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
105         }
106         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
107                 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
108         }
109
110         pvfs->sid_cache.creator_owner = dom_sid_parse_talloc(pvfs, SID_CREATOR_OWNER);
111         pvfs->sid_cache.creator_group = dom_sid_parse_talloc(pvfs, SID_CREATOR_GROUP);
112
113         /* check if the system really supports xattrs */
114         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
115                 pvfs_xattr_probe(pvfs);
116         }
117
118         /* enable an ACL backend */
119         pvfs->acl_ops = pvfs_acl_backend_byname(share_string_option(scfg, PVFS_ACL, "xattr"));
120 }
121
122 static int pvfs_state_destructor(struct pvfs_state *pvfs)
123 {
124         struct pvfs_file *f, *fn;
125         struct pvfs_search_state *s, *sn;
126
127         /* 
128          * make sure we cleanup files and searches before anything else
129          * because there destructors need to acess the pvfs_state struct
130          */
131         for (f=pvfs->files.list; f; f=fn) {
132                 fn = f->next;
133                 talloc_free(f);
134         }
135
136         for (s=pvfs->search.list; s; s=sn) {
137                 sn = s->next;
138                 talloc_free(s);
139         }
140
141         return 0;
142 }
143
144 /*
145   connect to a share - used when a tree_connect operation comes
146   in. For a disk based backend we needs to ensure that the base
147   directory exists (tho it doesn't need to be accessible by the user,
148   that comes later)
149 */
150 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
151                              struct ntvfs_request *req, const char *sharename)
152 {
153         struct pvfs_state *pvfs;
154         struct stat st;
155         char *base_directory;
156         NTSTATUS status;
157
158         pvfs = talloc_zero(ntvfs, struct pvfs_state);
159         NT_STATUS_HAVE_NO_MEMORY(pvfs);
160
161         /* for simplicity of path construction, remove any trailing slash now */
162         base_directory = talloc_strdup(pvfs, share_string_option(ntvfs->ctx->config, SHARE_PATH, ""));
163         NT_STATUS_HAVE_NO_MEMORY(base_directory);
164         if (strcmp(base_directory, "/") != 0) {
165                 trim_string(base_directory, NULL, "/");
166         }
167
168         pvfs->ntvfs = ntvfs;
169         pvfs->base_directory = base_directory;
170
171         /* the directory must exist. Note that we deliberately don't
172            check that it is readable */
173         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
174                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
175                          pvfs->base_directory, sharename));
176                 return NT_STATUS_BAD_NETWORK_NAME;
177         }
178
179         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
180         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
181
182         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
183         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
184
185         ntvfs->private_data = pvfs;
186
187         pvfs->brl_context = brl_init(pvfs, 
188                                      pvfs->ntvfs->ctx->server_id,
189                                      pvfs->ntvfs->ctx->msg_ctx);
190         if (pvfs->brl_context == NULL) {
191                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
192         }
193
194         pvfs->odb_context = odb_init(pvfs, pvfs->ntvfs->ctx);
195         if (pvfs->odb_context == NULL) {
196                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
197         }
198
199         /* allow this to be NULL - we just disable change notify */
200         pvfs->notify_context = notify_init(pvfs, 
201                                            pvfs->ntvfs->ctx->server_id,  
202                                            pvfs->ntvfs->ctx->msg_ctx, 
203                                            event_context_find(pvfs),
204                                            pvfs->ntvfs->ctx->config);
205
206         pvfs->sidmap = sidmap_open(pvfs);
207         if (pvfs->sidmap == NULL) {
208                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
209         }
210
211         /* allocate the search handle -> ptr tree */
212         pvfs->search.idtree = idr_init(pvfs);
213         NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
214
215         status = pvfs_mangle_init(pvfs);
216         NT_STATUS_NOT_OK_RETURN(status);
217
218         pvfs_setup_options(pvfs);
219
220         talloc_set_destructor(pvfs, pvfs_state_destructor);
221
222 #ifdef SIGXFSZ
223         /* who had the stupid idea to generate a signal on a large
224            file write instead of just failing it!? */
225         BlockSignals(True, SIGXFSZ);
226 #endif
227
228         return NT_STATUS_OK;
229 }
230
231 /*
232   disconnect from a share
233 */
234 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
235 {
236         return NT_STATUS_OK;
237 }
238
239 /*
240   check if a directory exists
241 */
242 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
243                              struct ntvfs_request *req,
244                              union smb_chkpath *cp)
245 {
246         struct pvfs_state *pvfs = ntvfs->private_data;
247         struct pvfs_filename *name;
248         NTSTATUS status;
249
250         /* resolve the cifs name to a posix name */
251         status = pvfs_resolve_name(pvfs, req, cp->chkpath.in.path, 0, &name);
252         NT_STATUS_NOT_OK_RETURN(status);
253
254         if (!name->exists) {
255                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
256         }
257
258         if (!S_ISDIR(name->st.st_mode)) {
259                 return NT_STATUS_NOT_A_DIRECTORY;
260         }
261
262         return NT_STATUS_OK;
263 }
264
265 /*
266   copy a set of files
267 */
268 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
269                           struct ntvfs_request *req, struct smb_copy *cp)
270 {
271         DEBUG(0,("pvfs_copy not implemented\n"));
272         return NT_STATUS_NOT_SUPPORTED;
273 }
274
275 /*
276   return print queue info
277 */
278 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
279                          struct ntvfs_request *req, union smb_lpq *lpq)
280 {
281         return NT_STATUS_NOT_SUPPORTED;
282 }
283
284 /* SMBtrans - not used on file shares */
285 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
286                            struct ntvfs_request *req, struct smb_trans2 *trans2)
287 {
288         return NT_STATUS_ACCESS_DENIED;
289 }
290
291 /*
292   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
293  */
294 NTSTATUS ntvfs_posix_init(void)
295 {
296         NTSTATUS ret;
297         struct ntvfs_ops ops;
298         NTVFS_CURRENT_CRITICAL_SIZES(vers);
299
300         ZERO_STRUCT(ops);
301
302         ops.type = NTVFS_DISK;
303         
304         /* fill in all the operations */
305         ops.connect = pvfs_connect;
306         ops.disconnect = pvfs_disconnect;
307         ops.unlink = pvfs_unlink;
308         ops.chkpath = pvfs_chkpath;
309         ops.qpathinfo = pvfs_qpathinfo;
310         ops.setpathinfo = pvfs_setpathinfo;
311         ops.open = pvfs_open;
312         ops.mkdir = pvfs_mkdir;
313         ops.rmdir = pvfs_rmdir;
314         ops.rename = pvfs_rename;
315         ops.copy = pvfs_copy;
316         ops.ioctl = pvfs_ioctl;
317         ops.read = pvfs_read;
318         ops.write = pvfs_write;
319         ops.seek = pvfs_seek;
320         ops.flush = pvfs_flush; 
321         ops.close = pvfs_close;
322         ops.exit = pvfs_exit;
323         ops.lock = pvfs_lock;
324         ops.setfileinfo = pvfs_setfileinfo;
325         ops.qfileinfo = pvfs_qfileinfo;
326         ops.fsinfo = pvfs_fsinfo;
327         ops.lpq = pvfs_lpq;
328         ops.search_first = pvfs_search_first;
329         ops.search_next = pvfs_search_next;
330         ops.search_close = pvfs_search_close;
331         ops.trans = pvfs_trans;
332         ops.logoff = pvfs_logoff;
333         ops.async_setup = pvfs_async_setup;
334         ops.cancel = pvfs_cancel;
335         ops.notify = pvfs_notify;
336
337         /* register ourselves with the NTVFS subsystem. We register
338            under the name 'default' as we wish to be the default
339            backend, and also register as 'posix' */
340         ops.name = "default";
341         ret = ntvfs_register(&ops, &vers);
342
343         if (!NT_STATUS_IS_OK(ret)) {
344                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
345         }
346
347         ops.name = "posix";
348         ret = ntvfs_register(&ops, &vers);
349
350         if (!NT_STATUS_IS_OK(ret)) {
351                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
352         }
353
354         if (NT_STATUS_IS_OK(ret)) {
355                 ret = ntvfs_common_init();
356         }
357
358         return ret;
359 }