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