2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2001 Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
46 #include <netsmb/smb.h>
47 #include <netsmb/smb_conn.h>
48 #include <netsmb/smb_subr.h>
49 #include <netsmb/smb_dev.h>
51 #include <fs/smbfs/smbfs.h>
52 #include <fs/smbfs/smbfs_node.h>
53 #include <fs/smbfs/smbfs_subr.h>
55 static int smbfs_debuglevel = 0;
57 static int smbfs_version = SMBFS_VERSION;
59 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
60 "SMB/CIFS filesystem");
61 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
62 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
64 static vfs_init_t smbfs_init;
65 static vfs_uninit_t smbfs_uninit;
66 static vfs_cmount_t smbfs_cmount;
67 static vfs_mount_t smbfs_mount;
68 static vfs_root_t smbfs_root;
69 static vfs_quotactl_t smbfs_quotactl;
70 static vfs_statfs_t smbfs_statfs;
71 static vfs_unmount_t smbfs_unmount;
73 static struct vfsops smbfs_vfsops = {
74 .vfs_init = smbfs_init,
75 .vfs_cmount = smbfs_cmount,
76 .vfs_mount = smbfs_mount,
77 .vfs_quotactl = smbfs_quotactl,
78 .vfs_root = smbfs_root,
79 .vfs_statfs = smbfs_statfs,
80 .vfs_sync = vfs_stdsync,
81 .vfs_uninit = smbfs_uninit,
82 .vfs_unmount = smbfs_unmount,
86 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
88 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
89 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
90 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
92 uma_zone_t smbfs_pbuf_zone;
95 smbfs_cmount(struct mntarg *ma, void * data, uint64_t flags)
97 struct smbfs_args args;
100 error = copyin(data, &args, sizeof(struct smbfs_args));
104 if (args.version != SMBFS_VERSION) {
105 printf("mount version mismatch: kernel=%d, mount=%d\n",
106 SMBFS_VERSION, args.version);
109 ma = mount_argf(ma, "dev", "%d", args.dev);
110 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft");
111 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr");
112 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong");
113 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls");
114 ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong");
115 ma = mount_arg(ma, "rootpath", args.root_path, -1);
116 ma = mount_argf(ma, "uid", "%d", args.uid);
117 ma = mount_argf(ma, "gid", "%d", args.gid);
118 ma = mount_argf(ma, "file_mode", "%d", args.file_mode);
119 ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode);
120 ma = mount_argf(ma, "caseopt", "%d", args.caseopt);
122 error = kernel_mount(ma, flags);
127 static const char *smbfs_opts[] = {
128 "fd", "soft", "intr", "strong", "have_nls", "long",
129 "mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode",
130 "caseopt", "errmsg", NULL
134 smbfs_mount(struct mount *mp)
136 struct smbmount *smp = NULL;
138 struct smb_share *ssp = NULL;
142 struct smb_cred *scred;
148 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
151 if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) {
152 vfs_mount_error(mp, "%s", "Invalid option");
156 scred = smbfs_malloc_scred();
157 smb_makescred(scred, td, td->td_ucred);
159 /* Ask userspace of `fd`, the file descriptor of this session */
160 if (1 != vfs_scanopt(mp->mnt_optnew, "fd", "%d", &v)) {
161 vfs_mount_error(mp, "No fd option");
162 smbfs_free_scred(scred);
165 error = smb_dev2share(v, SMBM_EXEC, scred, &ssp, &dev);
166 smp = malloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK | M_ZERO);
168 printf("invalid device handle %d (%d)\n", v, error);
169 vfs_mount_error(mp, "invalid device handle %d %d\n", v, error);
170 smbfs_free_scred(scred);
171 free(smp, M_SMBFSDATA);
175 smb_share_unlock(ssp);
176 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
181 if (1 != vfs_scanopt(mp->mnt_optnew,
182 "caseopt", "%d", &smp->sm_caseopt)) {
183 vfs_mount_error(mp, "Invalid caseopt");
187 if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) {
188 vfs_mount_error(mp, "Invalid uid");
194 if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) {
195 vfs_mount_error(mp, "Invalid gid");
201 if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) {
202 vfs_mount_error(mp, "Invalid file_mode");
206 smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
208 if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) {
209 vfs_mount_error(mp, "Invalid dir_mode");
213 smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
215 vfs_flagopt(mp->mnt_optnew,
216 "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG);
218 pc = mp->mnt_stat.f_mntfromname;
219 pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
223 pc = strchr(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
226 pc = strchr(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
229 strncpy(pc, ssp->ss_name, pe - pc - 2);
233 error = smbfs_root(mp, LK_EXCLUSIVE, &vp);
235 vfs_mount_error(mp, "smbfs_root error: %d", error);
239 SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
242 SMBERROR("mp=%p\n", mp);
244 smbfs_free_scred(scred);
248 smb_share_put(ssp, scred);
249 smbfs_free_scred(scred);
251 if (error && smp->sm_dev == dev) {
256 free(smp, M_SMBFSDATA);
260 /* Unmount the filesystem described by mp. */
262 smbfs_unmount(struct mount *mp, int mntflags)
265 struct smbmount *smp = VFSTOSMBFS(mp);
266 struct smb_cred *scred;
270 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
273 if (mntflags & MNT_FORCE)
276 * Keep trying to flush the vnode list for the mount while
277 * some are still busy and we are making progress towards
278 * making them not busy. This is needed because smbfs vnodes
279 * reference their parent directory but may appear after their
280 * parent in the list; one pass over the vnode list is not
281 * sufficient in this case.
285 /* There is 1 extra root vnode reference from smbfs_mount(). */
286 error = vflush(mp, 1, flags, td);
287 } while (error == EBUSY && smp->sm_didrele != 0);
290 scred = smbfs_malloc_scred();
291 smb_makescred(scred, td, td->td_ucred);
292 error = smb_share_lock(smp->sm_share);
295 smb_share_put(smp->sm_share, scred);
299 panic("No private data for mount point");
303 free(smp, M_SMBFSDATA);
305 mp->mnt_flag &= ~MNT_LOCAL;
308 smbfs_free_scred(scred);
313 * Return locked root vnode of a filesystem
316 smbfs_root(struct mount *mp, int flags, struct vnode **vpp)
318 struct smbmount *smp = VFSTOSMBFS(mp);
321 struct smbfattr fattr;
324 struct smb_cred *scred;
331 *vpp = SMBTOV(smp->sm_root);
332 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
334 scred = smbfs_malloc_scred();
335 smb_makescred(scred, td, cred);
336 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, scred);
339 error = smbfs_nget(mp, NULL, NULL, 0, &fattr, &vp);
342 ASSERT_VOP_LOCKED(vp, "smbfs_root");
343 vp->v_vflag |= VV_ROOT;
348 smbfs_free_scred(scred);
353 * Do operations associated with quotas, not supported
357 smbfs_quotactl(mp, cmd, uid, arg)
363 SMBVDEBUG("return EOPNOTSUPP\n");
369 smbfs_init(struct vfsconf *vfsp)
372 smbfs_pbuf_zone = pbuf_zsecond_create("smbpbuf", nswbuf / 2);
373 SMBVDEBUG("done.\n");
379 smbfs_uninit(struct vfsconf *vfsp)
382 uma_zdestroy(smbfs_pbuf_zone);
383 SMBVDEBUG("done.\n");
391 smbfs_statfs(struct mount *mp, struct statfs *sbp)
393 struct thread *td = curthread;
394 struct smbmount *smp = VFSTOSMBFS(mp);
395 struct smbnode *np = smp->sm_root;
396 struct smb_share *ssp = smp->sm_share;
397 struct smb_cred *scred;
401 vfs_mount_error(mp, "np == NULL");
405 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */
406 scred = smbfs_malloc_scred();
407 smb_makescred(scred, td, td->td_ucred);
408 error = smbfs_smb_statfs(ssp, sbp, scred);
409 smbfs_free_scred(scred);