ff3d3448f296464c20253e904393ccaad363284e
[samba.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
30
31 /*
32   setup config options for a posix share
33 */
34 static void pvfs_setup_options(struct pvfs_state *pvfs)
35 {
36         int snum = pvfs->tcon->service;
37         int delay;
38
39         if (lp_map_hidden(snum))     pvfs->flags |= PVFS_FLAG_MAP_HIDDEN;
40         if (lp_map_archive(snum))    pvfs->flags |= PVFS_FLAG_MAP_ARCHIVE;
41         if (lp_map_system(snum))     pvfs->flags |= PVFS_FLAG_MAP_SYSTEM;
42         if (lp_readonly(snum))       pvfs->flags |= PVFS_FLAG_READONLY;
43         if (lp_strict_sync(snum))    pvfs->flags |= PVFS_FLAG_STRICT_SYNC;
44         if (lp_strict_locking(snum)) pvfs->flags |= PVFS_FLAG_STRICT_LOCKING;
45         if (lp_ci_filesystem(snum))  pvfs->flags |= PVFS_FLAG_CI_FILESYSTEM;
46
47         if (lp_parm_bool(snum, "posix", "fakeoplocks", False)) {
48                 pvfs->flags |= PVFS_FLAG_FAKE_OPLOCKS;
49         }
50
51 #if HAVE_XATTR_SUPPORT
52         if (lp_parm_bool(snum, "posix", "xattr", True)) pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
53 #endif
54
55         pvfs->sharing_violation_delay = 1000000;
56         delay = lp_parm_int(snum, "posix", "sharedelay");
57         if (delay != -1) {
58                 pvfs->sharing_violation_delay = delay;
59         }
60
61         pvfs->share_name = talloc_strdup(pvfs, lp_servicename(snum));
62
63         pvfs->fs_attribs = 
64                 FS_ATTR_CASE_SENSITIVE_SEARCH | 
65                 FS_ATTR_CASE_PRESERVED_NAMES |
66                 FS_ATTR_UNICODE_ON_DISK |
67                 FS_ATTR_SPARSE_FILES;
68
69         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
70                 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
71         }
72         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
73                 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
74         }
75 }
76
77
78 /*
79   connect to a share - used when a tree_connect operation comes
80   in. For a disk based backend we needs to ensure that the base
81   directory exists (tho it doesn't need to be accessible by the user,
82   that comes later)
83 */
84 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
85                              struct smbsrv_request *req, const char *sharename)
86 {
87         struct smbsrv_tcon *tcon = req->tcon;
88         struct pvfs_state *pvfs;
89         struct stat st;
90         char *base_directory;
91         NTSTATUS status;
92
93         pvfs = talloc_zero_p(tcon, struct pvfs_state);
94         if (pvfs == NULL) {
95                 return NT_STATUS_NO_MEMORY;
96         }
97
98         /* for simplicity of path construction, remove any trailing slash now */
99         base_directory = talloc_strdup(pvfs, lp_pathname(tcon->service));
100         trim_string(base_directory, NULL, "/");
101
102         pvfs->tcon = tcon;
103         pvfs->base_directory = base_directory;
104
105         /* the directory must exist. Note that we deliberately don't
106            check that it is readable */
107         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
108                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
109                          pvfs->base_directory, sharename));
110                 return NT_STATUS_BAD_NETWORK_NAME;
111         }
112
113         tcon->fs_type = talloc_strdup(tcon, "NTFS");
114         tcon->dev_type = talloc_strdup(tcon, "A:");
115
116         ntvfs->private_data = pvfs;
117
118         pvfs->brl_context = brl_init(pvfs, 
119                                      pvfs->tcon->smb_conn->connection->server_id,  
120                                      pvfs->tcon->service,
121                                      pvfs->tcon->smb_conn->connection->messaging_ctx);
122         if (pvfs->brl_context == NULL) {
123                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
124         }
125
126         pvfs->odb_context = odb_init(pvfs, 
127                                      pvfs->tcon->smb_conn->connection->server_id,  
128                                      pvfs->tcon->smb_conn->connection->messaging_ctx);
129         if (pvfs->odb_context == NULL) {
130                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
131         }
132
133         pvfs->sidmap = sidmap_open(pvfs);
134         if (pvfs->sidmap == NULL) {
135                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
136         }
137
138         /* allocate the fnum id -> ptr tree */
139         pvfs->idtree_fnum = idr_init(pvfs);
140         if (pvfs->idtree_fnum == NULL) {
141                 return NT_STATUS_NO_MEMORY;
142         }
143
144         /* allocate the search handle -> ptr tree */
145         pvfs->idtree_search = idr_init(pvfs);
146         if (pvfs->idtree_search == NULL) {
147                 return NT_STATUS_NO_MEMORY;
148         }
149
150         status = pvfs_mangle_init(pvfs);
151         if (!NT_STATUS_IS_OK(status)) {
152                 return status;
153         }
154
155         pvfs_setup_options(pvfs);
156
157 #ifdef SIGXFSZ
158         /* who had the stupid idea to generate a signal on a large
159            file write instead of just failing it!? */
160         BlockSignals(True, SIGXFSZ);
161 #endif
162
163         return NT_STATUS_OK;
164 }
165
166 /*
167   disconnect from a share
168 */
169 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs,
170                                 struct smbsrv_tcon *tcon)
171 {
172         return NT_STATUS_OK;
173 }
174
175 /*
176   check if a directory exists
177 */
178 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
179                              struct smbsrv_request *req, struct smb_chkpath *cp)
180 {
181         struct pvfs_state *pvfs = ntvfs->private_data;
182         struct pvfs_filename *name;
183         NTSTATUS status;
184
185         /* resolve the cifs name to a posix name */
186         status = pvfs_resolve_name(pvfs, req, cp->in.path, 0, &name);
187         if (!NT_STATUS_IS_OK(status)) {
188                 return status;
189         }
190
191         if (!name->exists) {
192                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
193         }
194
195         if (!S_ISDIR(name->st.st_mode)) {
196                 return NT_STATUS_NOT_A_DIRECTORY;
197         }
198
199         return NT_STATUS_OK;
200 }
201
202 /*
203   copy a set of files
204 */
205 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
206                           struct smbsrv_request *req, struct smb_copy *cp)
207 {
208         DEBUG(0,("pvfs_copy not implemented\n"));
209         return NT_STATUS_NOT_SUPPORTED;
210 }
211
212 /*
213   return print queue info
214 */
215 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
216                          struct smbsrv_request *req, union smb_lpq *lpq)
217 {
218         return NT_STATUS_NOT_SUPPORTED;
219 }
220
221 /* SMBtrans - not used on file shares */
222 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
223                            struct smbsrv_request *req, struct smb_trans2 *trans2)
224 {
225         return NT_STATUS_ACCESS_DENIED;
226 }
227
228 /*
229   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
230  */
231 NTSTATUS ntvfs_posix_init(void)
232 {
233         NTSTATUS ret;
234         struct ntvfs_ops ops;
235
236         ZERO_STRUCT(ops);
237
238         ops.type = NTVFS_DISK;
239         
240         /* fill in all the operations */
241         ops.connect = pvfs_connect;
242         ops.disconnect = pvfs_disconnect;
243         ops.unlink = pvfs_unlink;
244         ops.chkpath = pvfs_chkpath;
245         ops.qpathinfo = pvfs_qpathinfo;
246         ops.setpathinfo = pvfs_setpathinfo;
247         ops.openfile = pvfs_open;
248         ops.mkdir = pvfs_mkdir;
249         ops.rmdir = pvfs_rmdir;
250         ops.rename = pvfs_rename;
251         ops.copy = pvfs_copy;
252         ops.ioctl = pvfs_ioctl;
253         ops.read = pvfs_read;
254         ops.write = pvfs_write;
255         ops.seek = pvfs_seek;
256         ops.flush = pvfs_flush; 
257         ops.close = pvfs_close;
258         ops.exit = pvfs_exit;
259         ops.lock = pvfs_lock;
260         ops.setfileinfo = pvfs_setfileinfo;
261         ops.qfileinfo = pvfs_qfileinfo;
262         ops.fsinfo = pvfs_fsinfo;
263         ops.lpq = pvfs_lpq;
264         ops.search_first = pvfs_search_first;
265         ops.search_next = pvfs_search_next;
266         ops.search_close = pvfs_search_close;
267         ops.trans = pvfs_trans;
268         ops.logoff = pvfs_logoff;
269         ops.async_setup = pvfs_async_setup;
270         ops.cancel = pvfs_cancel;
271
272         /* register ourselves with the NTVFS subsystem. We register
273            under the name 'default' as we wish to be the default
274            backend, and also register as 'posix' */
275         ops.name = "default";
276         ret = ntvfs_register(&ops);
277
278         ops.name = "posix";
279         ret = ntvfs_register(&ops);
280
281         if (!NT_STATUS_IS_OK(ret)) {
282                 DEBUG(0,("Failed to register POSIX backend!\n"));
283         }
284
285         return ret;
286 }