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>
45 #include <netsmb/smb.h>
46 #include <netsmb/smb_conn.h>
47 #include <netsmb/smb_subr.h>
48 #include <netsmb/smb_dev.h>
50 #include <fs/smbfs/smbfs.h>
51 #include <fs/smbfs/smbfs_node.h>
52 #include <fs/smbfs/smbfs_subr.h>
54 static int smbfs_debuglevel = 0;
56 static int smbfs_version = SMBFS_VERSION;
58 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
59 "SMB/CIFS filesystem");
60 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
61 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
63 static vfs_init_t smbfs_init;
64 static vfs_uninit_t smbfs_uninit;
65 static vfs_cmount_t smbfs_cmount;
66 static vfs_mount_t smbfs_mount;
67 static vfs_root_t smbfs_root;
68 static vfs_quotactl_t smbfs_quotactl;
69 static vfs_statfs_t smbfs_statfs;
70 static vfs_unmount_t smbfs_unmount;
72 static struct vfsops smbfs_vfsops = {
73 .vfs_init = smbfs_init,
74 .vfs_cmount = smbfs_cmount,
75 .vfs_mount = smbfs_mount,
76 .vfs_quotactl = smbfs_quotactl,
77 .vfs_root = smbfs_root,
78 .vfs_statfs = smbfs_statfs,
79 .vfs_sync = vfs_stdsync,
80 .vfs_uninit = smbfs_uninit,
81 .vfs_unmount = smbfs_unmount,
84 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
86 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
87 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
88 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
90 uma_zone_t smbfs_pbuf_zone;
93 smbfs_cmount(struct mntarg *ma, void * data, uint64_t flags)
95 struct smbfs_args args;
98 error = copyin(data, &args, sizeof(struct smbfs_args));
102 if (args.version != SMBFS_VERSION) {
103 printf("mount version mismatch: kernel=%d, mount=%d\n",
104 SMBFS_VERSION, args.version);
107 ma = mount_argf(ma, "dev", "%d", args.dev);
108 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft");
109 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr");
110 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong");
111 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls");
112 ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong");
113 ma = mount_arg(ma, "rootpath", args.root_path, -1);
114 ma = mount_argf(ma, "uid", "%d", args.uid);
115 ma = mount_argf(ma, "gid", "%d", args.gid);
116 ma = mount_argf(ma, "file_mode", "%d", args.file_mode);
117 ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode);
118 ma = mount_argf(ma, "caseopt", "%d", args.caseopt);
120 error = kernel_mount(ma, flags);
125 static const char *smbfs_opts[] = {
126 "fd", "soft", "intr", "strong", "have_nls", "long",
127 "mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode",
128 "caseopt", "errmsg", NULL
132 smbfs_mount(struct mount *mp)
134 struct smbmount *smp = NULL;
136 struct smb_share *ssp = NULL;
140 struct smb_cred *scred;
146 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
149 if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) {
150 vfs_mount_error(mp, "%s", "Invalid option");
154 scred = smbfs_malloc_scred();
155 smb_makescred(scred, td, td->td_ucred);
157 /* Ask userspace of `fd`, the file descriptor of this session */
158 if (1 != vfs_scanopt(mp->mnt_optnew, "fd", "%d", &v)) {
159 vfs_mount_error(mp, "No fd option");
160 smbfs_free_scred(scred);
163 error = smb_dev2share(v, SMBM_EXEC, scred, &ssp, &dev);
164 smp = malloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK | M_ZERO);
166 printf("invalid device handle %d (%d)\n", v, error);
167 vfs_mount_error(mp, "invalid device handle %d %d\n", v, error);
168 smbfs_free_scred(scred);
169 free(smp, M_SMBFSDATA);
173 smb_share_unlock(ssp);
174 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
179 if (1 != vfs_scanopt(mp->mnt_optnew,
180 "caseopt", "%d", &smp->sm_caseopt)) {
181 vfs_mount_error(mp, "Invalid caseopt");
185 if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) {
186 vfs_mount_error(mp, "Invalid uid");
192 if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) {
193 vfs_mount_error(mp, "Invalid gid");
199 if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) {
200 vfs_mount_error(mp, "Invalid file_mode");
204 smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
206 if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) {
207 vfs_mount_error(mp, "Invalid dir_mode");
211 smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
213 vfs_flagopt(mp->mnt_optnew,
214 "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG);
216 pc = mp->mnt_stat.f_mntfromname;
217 pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
221 pc = strchr(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
224 pc = strchr(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
227 strncpy(pc, ssp->ss_name, pe - pc - 2);
231 error = smbfs_root(mp, LK_EXCLUSIVE, &vp);
233 vfs_mount_error(mp, "smbfs_root error: %d", error);
237 SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
240 SMBERROR("mp=%p\n", mp);
242 smbfs_free_scred(scred);
246 smb_share_put(ssp, scred);
247 smbfs_free_scred(scred);
249 if (error && smp->sm_dev == dev) {
254 free(smp, M_SMBFSDATA);
258 /* Unmount the filesystem described by mp. */
260 smbfs_unmount(struct mount *mp, int mntflags)
263 struct smbmount *smp = VFSTOSMBFS(mp);
264 struct smb_cred *scred;
268 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
271 if (mntflags & MNT_FORCE)
274 * Keep trying to flush the vnode list for the mount while
275 * some are still busy and we are making progress towards
276 * making them not busy. This is needed because smbfs vnodes
277 * reference their parent directory but may appear after their
278 * parent in the list; one pass over the vnode list is not
279 * sufficient in this case.
283 /* There is 1 extra root vnode reference from smbfs_mount(). */
284 error = vflush(mp, 1, flags, td);
285 } while (error == EBUSY && smp->sm_didrele != 0);
288 scred = smbfs_malloc_scred();
289 smb_makescred(scred, td, td->td_ucred);
290 error = smb_share_lock(smp->sm_share);
293 smb_share_put(smp->sm_share, scred);
297 panic("No private data for mount point");
301 free(smp, M_SMBFSDATA);
303 mp->mnt_flag &= ~MNT_LOCAL;
306 smbfs_free_scred(scred);
311 * Return locked root vnode of a filesystem
314 smbfs_root(struct mount *mp, int flags, struct vnode **vpp)
316 struct smbmount *smp = VFSTOSMBFS(mp);
319 struct smbfattr fattr;
322 struct smb_cred *scred;
329 *vpp = SMBTOV(smp->sm_root);
330 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY);
332 scred = smbfs_malloc_scred();
333 smb_makescred(scred, td, cred);
334 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, scred);
337 error = smbfs_nget(mp, NULL, NULL, 0, &fattr, &vp);
340 ASSERT_VOP_LOCKED(vp, "smbfs_root");
341 vp->v_vflag |= VV_ROOT;
346 smbfs_free_scred(scred);
351 * Do operations associated with quotas, not supported
355 smbfs_quotactl(mp, cmd, uid, arg, mp_busy)
362 SMBVDEBUG("return EOPNOTSUPP\n");
368 smbfs_init(struct vfsconf *vfsp)
371 smbfs_pbuf_zone = pbuf_zsecond_create("smbpbuf", nswbuf / 2);
372 SMBVDEBUG("done.\n");
378 smbfs_uninit(struct vfsconf *vfsp)
381 uma_zdestroy(smbfs_pbuf_zone);
382 SMBVDEBUG("done.\n");
390 smbfs_statfs(struct mount *mp, struct statfs *sbp)
392 struct thread *td = curthread;
393 struct smbmount *smp = VFSTOSMBFS(mp);
394 struct smbnode *np = smp->sm_root;
395 struct smb_share *ssp = smp->sm_share;
396 struct smb_cred *scred;
400 vfs_mount_error(mp, "np == NULL");
404 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */
405 scred = smbfs_malloc_scred();
406 smb_makescred(scred, td, td->td_ucred);
407 error = smbfs_smb_statfs(ssp, sbp, scred);
408 smbfs_free_scred(scred);