1 /*-
2 * Copyright (c) 2000-2001, Boris Popov
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: releng/8.0/sys/fs/smbfs/smbfs_vfsops.c 191990 2009-05-11 15:33:26Z attilio $
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/bio.h>
39 #include <sys/buf.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/sx.h>
48
49
50 #include <netsmb/smb.h>
51 #include <netsmb/smb_conn.h>
52 #include <netsmb/smb_subr.h>
53 #include <netsmb/smb_dev.h>
54
55 #include <fs/smbfs/smbfs.h>
56 #include <fs/smbfs/smbfs_node.h>
57 #include <fs/smbfs/smbfs_subr.h>
58
59 static int smbfs_debuglevel = 0;
60
61 static int smbfs_version = SMBFS_VERSION;
62
63 #ifdef SMBFS_USEZONE
64 #include <vm/vm.h>
65 #include <vm/vm_extern.h>
66
67 vm_zone_t smbfsmount_zone;
68 #endif
69
70 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem");
71 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
72 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
73
74 static MALLOC_DEFINE(M_SMBFSHASH, "smbfs_hash", "SMBFS hash table");
75
76 static vfs_init_t smbfs_init;
77 static vfs_uninit_t smbfs_uninit;
78 static vfs_cmount_t smbfs_cmount;
79 static vfs_mount_t smbfs_mount;
80 static vfs_root_t smbfs_root;
81 static vfs_quotactl_t smbfs_quotactl;
82 static vfs_statfs_t smbfs_statfs;
83 static vfs_unmount_t smbfs_unmount;
84
85 static struct vfsops smbfs_vfsops = {
86 .vfs_init = smbfs_init,
87 .vfs_cmount = smbfs_cmount,
88 .vfs_mount = smbfs_mount,
89 .vfs_quotactl = smbfs_quotactl,
90 .vfs_root = smbfs_root,
91 .vfs_statfs = smbfs_statfs,
92 .vfs_sync = vfs_stdsync,
93 .vfs_uninit = smbfs_uninit,
94 .vfs_unmount = smbfs_unmount,
95 };
96
97
98 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
99
100 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
101 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
102 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
103
104 int smbfs_pbuf_freecnt = -1; /* start out unlimited */
105
106 static int
107 smbfs_cmount(struct mntarg *ma, void * data, int flags)
108 {
109 struct smbfs_args args;
110 int error;
111
112 error = copyin(data, &args, sizeof(struct smbfs_args));
113 if (error)
114 return error;
115
116 if (args.version != SMBFS_VERSION) {
117 printf("mount version mismatch: kernel=%d, mount=%d\n",
118 SMBFS_VERSION, args.version);
119 return EINVAL;
120 }
121 ma = mount_argf(ma, "dev", "%d", args.dev);
122 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft");
123 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr");
124 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong");
125 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls");
126 ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong");
127 ma = mount_arg(ma, "rootpath", args.root_path, -1);
128 ma = mount_argf(ma, "uid", "%d", args.uid);
129 ma = mount_argf(ma, "gid", "%d", args.gid);
130 ma = mount_argf(ma, "file_mode", "%d", args.file_mode);
131 ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode);
132 ma = mount_argf(ma, "caseopt", "%d", args.caseopt);
133
134 error = kernel_mount(ma, flags);
135
136 return (error);
137 }
138
139 static const char *smbfs_opts[] = {
140 "dev", "soft", "intr", "strong", "have_nls", "long",
141 "mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode",
142 "caseopt", "errmsg", NULL
143 };
144
145 static int
146 smbfs_mount(struct mount *mp)
147 {
148 struct smbmount *smp = NULL;
149 struct smb_vc *vcp;
150 struct smb_share *ssp = NULL;
151 struct vnode *vp;
152 struct thread *td;
153 struct smb_cred scred;
154 int error, v;
155 char *pc, *pe;
156
157 td = curthread;
158 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
159 return EOPNOTSUPP;
160
161 if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) {
162 vfs_mount_error(mp, "%s", "Invalid option");
163 return (EINVAL);
164 }
165
166 smb_makescred(&scred, td, td->td_ucred);
167 if (1 != vfs_scanopt(mp->mnt_optnew, "dev", "%d", &v)) {
168 vfs_mount_error(mp, "No dev option");
169 return (EINVAL);
170 }
171 error = smb_dev2share(v, SMBM_EXEC, &scred, &ssp);
172 if (error) {
173 printf("invalid device handle %d (%d)\n", v, error);
174 vfs_mount_error(mp, "invalid device handle %d (%d)\n", v, error);
175 return error;
176 }
177 vcp = SSTOVC(ssp);
178 smb_share_unlock(ssp, 0);
179 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
180
181 #ifdef SMBFS_USEZONE
182 smp = zalloc(smbfsmount_zone);
183 #else
184 smp = malloc(sizeof(*smp), M_SMBFSDATA,
185 M_WAITOK|M_USE_RESERVE);
186 #endif
187 if (smp == NULL) {
188 printf("could not alloc smbmount\n");
189 vfs_mount_error(mp, "could not alloc smbmount", v, error);
190 error = ENOMEM;
191 goto bad;
192 }
193 bzero(smp, sizeof(*smp));
194 mp->mnt_data = smp;
195 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
196 if (smp->sm_hash == NULL)
197 goto bad;
198 sx_init(&smp->sm_hashlock, "smbfsh");
199 smp->sm_share = ssp;
200 smp->sm_root = NULL;
201 if (1 != vfs_scanopt(mp->mnt_optnew,
202 "caseopt", "%d", &smp->sm_caseopt)) {
203 vfs_mount_error(mp, "Invalid caseopt");
204 error = EINVAL;
205 goto bad;
206 }
207 if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) {
208 vfs_mount_error(mp, "Invalid uid");
209 error = EINVAL;
210 goto bad;
211 }
212 smp->sm_uid = v;
213
214 if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) {
215 vfs_mount_error(mp, "Invalid gid");
216 error = EINVAL;
217 goto bad;
218 }
219 smp->sm_gid = v;
220
221 if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) {
222 vfs_mount_error(mp, "Invalid file_mode");
223 error = EINVAL;
224 goto bad;
225 }
226 smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
227
228 if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) {
229 vfs_mount_error(mp, "Invalid dir_mode");
230 error = EINVAL;
231 goto bad;
232 }
233 smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
234
235 vfs_flagopt(mp->mnt_optnew,
236 "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG);
237
238 /* simple_lock_init(&smp->sm_npslock);*/
239 pc = mp->mnt_stat.f_mntfromname;
240 pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
241 bzero(pc, MNAMELEN);
242 *pc++ = '/';
243 *pc++ = '/';
244 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
245 if (pc < pe-1) {
246 *(pc++) = '@';
247 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
248 if (pc < pe - 1) {
249 *(pc++) = '/';
250 strncpy(pc, ssp->ss_name, pe - pc - 2);
251 }
252 }
253 vfs_getnewfsid(mp);
254 error = smbfs_root(mp, LK_EXCLUSIVE, &vp);
255 if (error) {
256 vfs_mount_error(mp, "smbfs_root error: %d", error);
257 goto bad;
258 }
259 VOP_UNLOCK(vp, 0);
260 SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
261
262 #ifdef DIAGNOSTICS
263 SMBERROR("mp=%p\n", mp);
264 #endif
265 return error;
266 bad:
267 if (smp) {
268 if (smp->sm_hash)
269 free(smp->sm_hash, M_SMBFSHASH);
270 sx_destroy(&smp->sm_hashlock);
271 #ifdef SMBFS_USEZONE
272 zfree(smbfsmount_zone, smp);
273 #else
274 free(smp, M_SMBFSDATA);
275 #endif
276 }
277 if (ssp)
278 smb_share_put(ssp, &scred);
279 return error;
280 }
281
282 /* Unmount the filesystem described by mp. */
283 static int
284 smbfs_unmount(struct mount *mp, int mntflags)
285 {
286 struct thread *td;
287 struct smbmount *smp = VFSTOSMBFS(mp);
288 struct smb_cred scred;
289 int error, flags;
290
291 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
292 td = curthread;
293 flags = 0;
294 if (mntflags & MNT_FORCE)
295 flags |= FORCECLOSE;
296 /*
297 * Keep trying to flush the vnode list for the mount while
298 * some are still busy and we are making progress towards
299 * making them not busy. This is needed because smbfs vnodes
300 * reference their parent directory but may appear after their
301 * parent in the list; one pass over the vnode list is not
302 * sufficient in this case.
303 */
304 do {
305 smp->sm_didrele = 0;
306 /* There is 1 extra root vnode reference from smbfs_mount(). */
307 error = vflush(mp, 1, flags, td);
308 } while (error == EBUSY && smp->sm_didrele != 0);
309 if (error)
310 return error;
311 smb_makescred(&scred, td, td->td_ucred);
312 error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE);
313 if (error)
314 return error;
315 smb_share_put(smp->sm_share, &scred);
316 mp->mnt_data = NULL;
317
318 if (smp->sm_hash)
319 free(smp->sm_hash, M_SMBFSHASH);
320 sx_destroy(&smp->sm_hashlock);
321 #ifdef SMBFS_USEZONE
322 zfree(smbfsmount_zone, smp);
323 #else
324 free(smp, M_SMBFSDATA);
325 #endif
326 MNT_ILOCK(mp);
327 mp->mnt_flag &= ~MNT_LOCAL;
328 MNT_IUNLOCK(mp);
329 return error;
330 }
331
332 /*
333 * Return locked root vnode of a filesystem
334 */
335 static int
336 smbfs_root(struct mount *mp, int flags, struct vnode **vpp)
337 {
338 struct smbmount *smp = VFSTOSMBFS(mp);
339 struct vnode *vp;
340 struct smbnode *np;
341 struct smbfattr fattr;
342 struct thread *td;
343 struct ucred *cred;
344 struct smb_cred scred;
345 int error;
346
347 td = curthread;
348 cred = td->td_ucred;
349
350 if (smp == NULL) {
351 SMBERROR("smp == NULL (bug in umount)\n");
352 vfs_mount_error(mp, "smp == NULL (bug in umount)");
353 return EINVAL;
354 }
355 if (smp->sm_root) {
356 *vpp = SMBTOV(smp->sm_root);
357 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
358 }
359 smb_makescred(&scred, td, cred);
360 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
361 if (error)
362 return error;
363 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
364 if (error)
365 return error;
366 ASSERT_VOP_LOCKED(vp, "smbfs_root");
367 vp->v_vflag |= VV_ROOT;
368 np = VTOSMB(vp);
369 smp->sm_root = np;
370 *vpp = vp;
371 return 0;
372 }
373
374 /*
375 * Do operations associated with quotas, not supported
376 */
377 /* ARGSUSED */
378 static int
379 smbfs_quotactl(mp, cmd, uid, arg)
380 struct mount *mp;
381 int cmd;
382 uid_t uid;
383 void *arg;
384 {
385 SMBVDEBUG("return EOPNOTSUPP\n");
386 return EOPNOTSUPP;
387 }
388
389 /*ARGSUSED*/
390 int
391 smbfs_init(struct vfsconf *vfsp)
392 {
393 #ifdef SMBFS_USEZONE
394 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
395 #endif
396 smbfs_pbuf_freecnt = nswbuf / 2 + 1;
397 SMBVDEBUG("done.\n");
398 return 0;
399 }
400
401 /*ARGSUSED*/
402 int
403 smbfs_uninit(struct vfsconf *vfsp)
404 {
405
406 SMBVDEBUG("done.\n");
407 return 0;
408 }
409
410 /*
411 * smbfs_statfs call
412 */
413 int
414 smbfs_statfs(struct mount *mp, struct statfs *sbp)
415 {
416 struct thread *td = curthread;
417 struct smbmount *smp = VFSTOSMBFS(mp);
418 struct smbnode *np = smp->sm_root;
419 struct smb_share *ssp = smp->sm_share;
420 struct smb_cred scred;
421 int error = 0;
422
423 if (np == NULL) {
424 vfs_mount_error(mp, "np == NULL");
425 return EINVAL;
426 }
427
428 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */
429 smb_makescred(&scred, td, td->td_ucred);
430
431 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
432 error = smbfs_smb_statfs2(ssp, sbp, &scred);
433 else
434 error = smbfs_smb_statfs(ssp, sbp, &scred);
435 if (error)
436 return error;
437 sbp->f_flags = 0; /* copy of mount exported flags */
438 return 0;
439 }
Cache object: 0466e7083d9ec2a6ec873bb201036827
|