2 * Copyright (c) 2000-2001, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/param.h>
36 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
50 #include <netsmb/smb.h>
51 #include <netsmb/smb_conn.h>
52 #include <netsmb/smb_subr.h>
53 #include <netsmb/smb_dev.h>
55 #include <fs/smbfs/smbfs.h>
56 #include <fs/smbfs/smbfs_node.h>
57 #include <fs/smbfs/smbfs_subr.h>
59 static int smbfs_debuglevel = 0;
61 static int smbfs_version = SMBFS_VERSION;
65 #include <vm/vm_extern.h>
67 vm_zone_t smbfsmount_zone;
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, "");
74 static MALLOC_DEFINE(M_SMBFSHASH, "smbfs_hash", "SMBFS hash table");
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;
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,
98 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
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);
104 int smbfs_pbuf_freecnt = -1; /* start out unlimited */
107 smbfs_cmount(struct mntarg *ma, void * data, int flags, struct thread *td)
109 struct smbfs_args args;
112 error = copyin(data, &args, sizeof(struct smbfs_args));
116 if (args.version != SMBFS_VERSION) {
117 printf("mount version mismatch: kernel=%d, mount=%d\n",
118 SMBFS_VERSION, args.version);
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);
134 error = kernel_mount(ma, flags);
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
146 smbfs_mount(struct mount *mp, struct thread *td)
148 struct smbmount *smp = NULL;
150 struct smb_share *ssp = NULL;
152 struct smb_cred scred;
156 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS))
159 if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) {
160 vfs_mount_error(mp, "%s", "Invalid option");
164 smb_makescred(&scred, td, td->td_ucred);
165 if (1 != vfs_scanopt(mp->mnt_optnew, "dev", "%d", &v)) {
166 vfs_mount_error(mp, "No dev option");
169 error = smb_dev2share(v, SMBM_EXEC, &scred, &ssp);
171 printf("invalid device handle %d (%d)\n", v, error);
172 vfs_mount_error(mp, "invalid device handle %d (%d)\n", v, error);
176 smb_share_unlock(ssp, 0, td);
177 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
180 smp = zalloc(smbfsmount_zone);
182 MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA,
183 M_WAITOK|M_USE_RESERVE);
186 printf("could not alloc smbmount\n");
187 vfs_mount_error(mp, "could not alloc smbmount", v, error);
191 bzero(smp, sizeof(*smp));
193 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
194 if (smp->sm_hash == NULL)
196 sx_init(&smp->sm_hashlock, "smbfsh");
199 if (1 != vfs_scanopt(mp->mnt_optnew,
200 "caseopt", "%d", &smp->sm_caseopt)) {
201 vfs_mount_error(mp, "Invalid caseopt");
205 if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) {
206 vfs_mount_error(mp, "Invalid uid");
212 if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) {
213 vfs_mount_error(mp, "Invalid gid");
219 if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) {
220 vfs_mount_error(mp, "Invalid file_mode");
224 smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
226 if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) {
227 vfs_mount_error(mp, "Invalid dir_mode");
231 smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
233 vfs_flagopt(mp->mnt_optnew,
234 "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG);
236 /* simple_lock_init(&smp->sm_npslock);*/
237 pc = mp->mnt_stat.f_mntfromname;
238 pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
242 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
245 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
248 strncpy(pc, ssp->ss_name, pe - pc - 2);
252 error = smbfs_root(mp, LK_EXCLUSIVE, &vp, td);
254 vfs_mount_error(mp, "smbfs_root error: %d", error);
258 SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
261 SMBERROR("mp=%p\n", mp);
267 free(smp->sm_hash, M_SMBFSHASH);
268 sx_destroy(&smp->sm_hashlock);
270 zfree(smbfsmount_zone, smp);
272 free(smp, M_SMBFSDATA);
276 smb_share_put(ssp, &scred);
280 /* Unmount the filesystem described by mp. */
282 smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
284 struct smbmount *smp = VFSTOSMBFS(mp);
285 struct smb_cred scred;
288 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
290 if (mntflags & MNT_FORCE)
293 * Keep trying to flush the vnode list for the mount while
294 * some are still busy and we are making progress towards
295 * making them not busy. This is needed because smbfs vnodes
296 * reference their parent directory but may appear after their
297 * parent in the list; one pass over the vnode list is not
298 * sufficient in this case.
302 /* There is 1 extra root vnode reference from smbfs_mount(). */
303 error = vflush(mp, 1, flags, td);
304 } while (error == EBUSY && smp->sm_didrele != 0);
307 smb_makescred(&scred, td, td->td_ucred);
308 error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE, td);
311 smb_share_put(smp->sm_share, &scred);
315 free(smp->sm_hash, M_SMBFSHASH);
316 sx_destroy(&smp->sm_hashlock);
318 zfree(smbfsmount_zone, smp);
320 free(smp, M_SMBFSDATA);
323 mp->mnt_flag &= ~MNT_LOCAL;
329 * Return locked root vnode of a filesystem
332 smbfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
334 struct smbmount *smp = VFSTOSMBFS(mp);
337 struct smbfattr fattr;
338 struct ucred *cred = td->td_ucred;
339 struct smb_cred scred;
343 SMBERROR("smp == NULL (bug in umount)\n");
344 vfs_mount_error(mp, "smp == NULL (bug in umount)");
348 *vpp = SMBTOV(smp->sm_root);
349 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
351 smb_makescred(&scred, td, cred);
352 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
355 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
358 ASSERT_VOP_LOCKED(vp, "smbfs_root");
359 vp->v_vflag |= VV_ROOT;
367 * Do operations associated with quotas, not supported
371 smbfs_quotactl(mp, cmd, uid, arg, td)
378 SMBVDEBUG("return EOPNOTSUPP\n");
384 smbfs_init(struct vfsconf *vfsp)
387 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
389 smbfs_pbuf_freecnt = nswbuf / 2 + 1;
390 SMBVDEBUG("done.\n");
396 smbfs_uninit(struct vfsconf *vfsp)
399 SMBVDEBUG("done.\n");
407 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
409 struct smbmount *smp = VFSTOSMBFS(mp);
410 struct smbnode *np = smp->sm_root;
411 struct smb_share *ssp = smp->sm_share;
412 struct smb_cred scred;
416 vfs_mount_error(mp, "np == NULL");
420 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */
421 smb_makescred(&scred, td, td->td_ucred);
423 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
424 error = smbfs_smb_statfs2(ssp, sbp, &scred);
426 error = smbfs_smb_statfs(ssp, sbp, &scred);
429 sbp->f_flags = 0; /* copy of mount exported flags */