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