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
32 * $FreeBSD: src/sys/fs/smbfs/smbfs_vfsops.c,v 1.2.2.5 2003/01/17 08:20:26 tjr Exp $
33 * $DragonFly: src/sys/vfs/smbfs/smbfs_vfsops.c,v 1.13 2004/08/17 18:57:35 dillon Exp $
35 #include "opt_netsmb.h"
37 #error "SMBFS requires option NETSMB"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
52 #include <netproto/smb/smb.h>
53 #include <netproto/smb/smb_conn.h>
54 #include <netproto/smb/smb_subr.h>
55 #include <netproto/smb/smb_dev.h>
58 #include "smbfs_node.h"
59 #include "smbfs_subr.h"
63 extern struct vnodeopv_entry_desc smbfs_vnodeop_entries[];
65 int smbfs_debuglevel = 0;
67 static int smbfs_version = SMBFS_VERSION;
71 #include <vm/vm_extern.h>
72 #include <vm/vm_zone.h>
74 vm_zone_t smbfsmount_zone;
77 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system");
78 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
79 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
81 static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
84 static int smbfs_mount(struct mount *, char *, caddr_t,
85 struct nameidata *, struct thread *);
86 static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *);
87 static int smbfs_root(struct mount *, struct vnode **);
88 static int smbfs_start(struct mount *, int, struct thread *);
89 static int smbfs_statfs(struct mount *, struct statfs *, struct thread *);
90 static int smbfs_sync(struct mount *, int, struct thread *);
91 static int smbfs_unmount(struct mount *, int, struct thread *);
92 static int smbfs_init(struct vfsconf *vfsp);
93 static int smbfs_uninit(struct vfsconf *vfsp);
95 #if defined(__FreeBSD__) && __FreeBSD_version < 400009
96 static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp);
97 static int smbfs_fhtovp(struct mount *, struct fid *,
98 struct sockaddr *, struct vnode **, int *,
100 static int smbfs_vptofh(struct vnode *, struct fid *);
103 static struct vfsops smbfs_vfsops = {
111 #if defined(__DragonFly__) || __FreeBSD_version > 400008
113 vfs_stdfhtovp, /* shouldn't happen */
115 vfs_stdvptofh, /* shouldn't happen */
127 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
129 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
130 MODULE_DEPEND(smbfs, libiconv, 1, 1, 1);
131 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
133 int smbfs_pbuf_freecnt = -1; /* start out unlimited */
136 smbfs_mount(struct mount *mp, char *path, caddr_t data,
137 struct nameidata *ndp, struct thread *td)
139 struct smbfs_args args; /* will hold data from mount request */
140 struct smbmount *smp = NULL;
142 struct smb_share *ssp = NULL;
144 struct smb_cred scred;
150 KKASSERT(td->td_proc);
151 cred = td->td_proc->p_ucred;
154 printf("missing data argument\n");
157 if (mp->mnt_flag & MNT_UPDATE) {
158 printf("MNT_UPDATE not implemented");
161 error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
164 if (args.version != SMBFS_VERSION) {
165 printf("mount version mismatch: kernel=%d, mount=%d\n",
166 SMBFS_VERSION, args.version);
169 smb_makescred(&scred, td, cred);
170 error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
172 printf("invalid device handle %d (%d)\n", args.dev, 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, M_WAITOK|M_USE_RESERVE);
185 printf("could not alloc smbmount\n");
189 bzero(smp, sizeof(*smp));
190 mp->mnt_data = (qaddr_t)smp;
191 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
192 if (smp->sm_hash == NULL)
194 lockinit(&smp->sm_hashlock, 0, "smbfsh", 0, 0);
198 smp->sm_caseopt = args.caseopt;
199 smp->sm_args.file_mode = (smp->sm_args.file_mode &
200 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
201 smp->sm_args.dir_mode = (smp->sm_args.dir_mode &
202 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
204 /* simple_lock_init(&smp->sm_npslock);*/
205 error = copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
208 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
209 pc = mp->mnt_stat.f_mntfromname;
210 pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
214 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
217 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
220 strncpy(pc, ssp->ss_name, pe - pc - 2);
223 /* protect against invalid mount points */
224 smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
227 vfs_add_vnodeops(&mp->mnt_vn_ops, smbfs_vnodeop_entries);
229 error = smbfs_root(mp, &vp);
232 VOP_UNLOCK(vp, NULL, 0, td);
233 SMBVDEBUG("root.v_usecount = %d\n", vp->v_usecount);
236 SMBERROR("mp=%p\n", mp);
242 free(smp->sm_hash, M_SMBFSHASH);
243 lockdestroy(&smp->sm_hashlock);
245 zfree(smbfsmount_zone, smp);
247 free(smp, M_SMBFSDATA);
251 smb_share_put(ssp, &scred);
255 /* Unmount the filesystem described by mp. */
257 smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
259 struct smbmount *smp = VFSTOSMBFS(mp);
260 struct smb_cred scred;
264 KKASSERT(td->td_proc);
265 cred = td->td_proc->p_ucred;
267 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
269 if (mntflags & MNT_FORCE)
272 * Keep trying to flush the vnode list for the mount while
273 * some are still busy and we are making progress towards
274 * making them not busy. This is needed because smbfs vnodes
275 * reference their parent directory but may appear after their
276 * parent in the list; one pass over the vnode list is not
277 * sufficient in this case.
281 /* There is 1 extra root vnode reference from smbfs_mount(). */
282 error = vflush(mp, 1, flags);
283 } while (error == EBUSY && smp->sm_didrele != 0);
286 smb_makescred(&scred, td, cred);
287 smb_share_put(smp->sm_share, &scred);
288 mp->mnt_data = (qaddr_t)0;
291 free(smp->sm_hash, M_SMBFSHASH);
292 lockdestroy(&smp->sm_hashlock);
294 zfree(smbfsmount_zone, smp);
296 free(smp, M_SMBFSDATA);
298 mp->mnt_flag &= ~MNT_LOCAL;
303 * Return locked root vnode of a filesystem
306 smbfs_root(struct mount *mp, struct vnode **vpp)
308 struct thread *td = curthread; /* XXX */
309 struct smbmount *smp = VFSTOSMBFS(mp);
312 struct smbfattr fattr;
314 struct smb_cred scred;
317 KKASSERT(td->td_proc);
318 cred = td->td_proc->p_ucred;
321 SMBERROR("smp == NULL (bug in umount)\n");
325 *vpp = SMBTOV(smp->sm_root);
326 return vget(*vpp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
328 smb_makescred(&scred, td, cred);
329 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
332 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
343 * Vfs start routine, a no-op.
347 smbfs_start(struct mount *mp, int flags, struct thread *td)
349 SMBVDEBUG("flags=%04x\n", flags);
354 * Do operations associated with quotas, not supported
358 smbfs_quotactl(struct mount *mp, int cmd, uid_t uid, caddr_t arg,
361 SMBVDEBUG("return EOPNOTSUPP\n");
367 smbfs_init(struct vfsconf *vfsp)
370 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
372 smbfs_pbuf_freecnt = nswbuf / 2 + 1;
373 SMBVDEBUG("done.\n");
379 smbfs_uninit(struct vfsconf *vfsp)
381 SMBVDEBUG("done.\n");
389 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
391 struct smbmount *smp = VFSTOSMBFS(mp);
392 struct smbnode *np = smp->sm_root;
393 struct smb_share *ssp = smp->sm_share;
394 struct smb_cred scred;
398 KKASSERT(td->td_proc);
399 cred = td->td_proc->p_ucred;
404 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */
405 sbp->f_spare2 = 0; /* placeholder */
406 smb_makescred(&scred, td, cred);
408 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
409 error = smbfs_smb_statfs2(ssp, sbp, &scred);
411 error = smbfs_smb_statfs(ssp, sbp, &scred);
414 sbp->f_flags = 0; /* copy of mount exported flags */
415 if (sbp != &mp->mnt_stat) {
416 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */
417 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */
418 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */
419 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
420 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
422 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
427 * Flush out the buffer cache
431 smbfs_sync(struct mount *mp, int waitfor, struct thread *td)
434 int error, allerror = 0;
436 * Force stale buffer cache information to be flushed.
439 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
441 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
442 if (vp->v_flag & VPLACEMARKER) /* ZZZ */
445 * If the vnode that we are about to sync is no longer
446 * associated with this mount point, start over.
448 if (vp->v_mount != mp)
450 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
453 if (vget(vp, NULL, LK_EXCLUSIVE, td))
455 error = VOP_FSYNC(vp, waitfor, td);
463 #if defined(__FreeBSD__) && __FreeBSD_version < 400009
465 * smbfs flat namespace lookup. Unsupported.
468 static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
474 static int smbfs_fhtovp(struct mount *mp, struct fid *fhp,
475 struct sockaddr *nam, struct vnode **vpp,
476 int *exflagsp, struct ucred **credanonp)
482 * Vnode pointer to File handle, should never happen either
486 smbfs_vptofh(struct vnode *vp, struct fid *fhp)
491 #endif /* __FreeBSD_version < 400009 */