smbfs_vfsops.c revision 95314
1126706Swpaul/* 2126706Swpaul * Copyright (c) 2000-2001, Boris Popov 3126706Swpaul * All rights reserved. 4126706Swpaul * 5126706Swpaul * Redistribution and use in source and binary forms, with or without 6126706Swpaul * modification, are permitted provided that the following conditions 7126706Swpaul * are met: 8126706Swpaul * 1. Redistributions of source code must retain the above copyright 9126706Swpaul * notice, this list of conditions and the following disclaimer. 10126706Swpaul * 2. Redistributions in binary form must reproduce the above copyright 11126706Swpaul * notice, this list of conditions and the following disclaimer in the 12126706Swpaul * documentation and/or other materials provided with the distribution. 13126706Swpaul * 3. All advertising materials mentioning features or use of this software 14126706Swpaul * must display the following acknowledgement: 15126706Swpaul * This product includes software developed by Boris Popov. 16126706Swpaul * 4. Neither the name of the author nor the names of any co-contributors 17126706Swpaul * may be used to endorse or promote products derived from this software 18126706Swpaul * without specific prior written permission. 19126706Swpaul * 20126706Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21126706Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22126706Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23126706Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24126706Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25126706Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26126706Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27126706Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28126706Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29126706Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30126706Swpaul * SUCH DAMAGE. 31126706Swpaul * 32126706Swpaul * $FreeBSD: head/sys/fs/smbfs/smbfs_vfsops.c 95314 2002-04-23 13:55:14Z bp $ 33126706Swpaul */ 34126706Swpaul#include "opt_netsmb.h" 35126706Swpaul#ifndef NETSMB 36126706Swpaul#error "SMBFS requires option NETSMB" 37126706Swpaul#endif 38126706Swpaul 39126706Swpaul#include <sys/param.h> 40126706Swpaul#include <sys/systm.h> 41126706Swpaul#include <sys/proc.h> 42126706Swpaul#include <sys/bio.h> 43126706Swpaul#include <sys/buf.h> 44126706Swpaul#include <sys/kernel.h> 45126706Swpaul#include <sys/sysctl.h> 46126706Swpaul#include <sys/vnode.h> 47126706Swpaul#include <sys/mount.h> 48126706Swpaul#include <sys/stat.h> 49126706Swpaul#include <sys/malloc.h> 50126706Swpaul#include <sys/module.h> 51126706Swpaul 52126706Swpaul 53126706Swpaul#include <netsmb/smb.h> 54126706Swpaul#include <netsmb/smb_conn.h> 55126706Swpaul#include <netsmb/smb_subr.h> 56126706Swpaul#include <netsmb/smb_dev.h> 57126706Swpaul 58126706Swpaul#include <fs/smbfs/smbfs.h> 59126706Swpaul#include <fs/smbfs/smbfs_node.h> 60126706Swpaul#include <fs/smbfs/smbfs_subr.h> 61126706Swpaul 62126706Swpaulint smbfs_debuglevel = 0; 63126706Swpaul 64126706Swpaulstatic int smbfs_version = SMBFS_VERSION; 65126706Swpaul 66126706Swpaul#ifdef SMBFS_USEZONE 67126706Swpaul#include <vm/vm.h> 68126706Swpaul#include <vm/vm_extern.h> 69126706Swpaul 70126706Swpaulvm_zone_t smbfsmount_zone; 71126706Swpaul#endif 72126706Swpaul 73126706SwpaulSYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system"); 74126706SwpaulSYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 75126706SwpaulSYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 76126706Swpaul 77126706Swpaulstatic MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table"); 78126706Swpaul 79126706Swpaul 80126706Swpaulstatic int smbfs_mount(struct mount *, char *, caddr_t, 81126706Swpaul struct nameidata *, struct thread *); 82126706Swpaulstatic int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *); 83126706Swpaulstatic int smbfs_root(struct mount *, struct vnode **); 84126706Swpaulstatic int smbfs_start(struct mount *, int, struct thread *); 85126706Swpaulstatic int smbfs_statfs(struct mount *, struct statfs *, struct thread *); 86126706Swpaulstatic int smbfs_sync(struct mount *, int, struct ucred *, struct thread *); 87126706Swpaulstatic int smbfs_unmount(struct mount *, int, struct thread *); 88126706Swpaulstatic int smbfs_init(struct vfsconf *vfsp); 89126706Swpaulstatic int smbfs_uninit(struct vfsconf *vfsp); 90126706Swpaul 91126706Swpaulstatic struct vfsops smbfs_vfsops = { 92126706Swpaul smbfs_mount, 93126706Swpaul smbfs_start, 94126706Swpaul smbfs_unmount, 95126706Swpaul smbfs_root, 96126706Swpaul smbfs_quotactl, 97126706Swpaul smbfs_statfs, 98126706Swpaul smbfs_sync, 99126706Swpaul vfs_stdvget, 100126706Swpaul vfs_stdfhtovp, /* shouldn't happen */ 101126706Swpaul vfs_stdcheckexp, 102126706Swpaul vfs_stdvptofh, /* shouldn't happen */ 103126706Swpaul smbfs_init, 104126706Swpaul smbfs_uninit, 105126706Swpaul vfs_stdextattrctl 106126706Swpaul}; 107126706Swpaul 108126706Swpaul 109126706SwpaulVFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 110126706Swpaul 111126706SwpaulMODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 112126706SwpaulMODULE_DEPEND(smbfs, libiconv, 1, 1, 1); 113126706SwpaulMODULE_DEPEND(smbfs, libmchain, 1, 1, 1); 114126706Swpaul 115126706Swpaulint smbfs_pbuf_freecnt = -1; /* start out unlimited */ 116126706Swpaul 117126706Swpaulstatic int 118126706Swpaulsmbfs_mount(struct mount *mp, char *path, caddr_t data, 119126706Swpaul struct nameidata *ndp, struct thread *td) 120126706Swpaul{ 121126706Swpaul struct smbfs_args args; /* will hold data from mount request */ 122126706Swpaul struct smbmount *smp = NULL; 123126706Swpaul struct smb_vc *vcp; 124126706Swpaul struct smb_share *ssp = NULL; 125126706Swpaul struct vnode *vp; 126126706Swpaul struct smb_cred scred; 127126706Swpaul#ifndef FB_CURRENT 128126706Swpaul size_t size; 129126706Swpaul#endif 130126706Swpaul int error; 131126706Swpaul char *pc, *pe; 132126706Swpaul 133126706Swpaul if (data == NULL) { 134126706Swpaul printf("missing data argument\n"); 135126706Swpaul return EINVAL; 136126706Swpaul } 137126706Swpaul if (mp->mnt_flag & MNT_UPDATE) { 138126706Swpaul printf("MNT_UPDATE not implemented"); 139126706Swpaul return EOPNOTSUPP; 140126706Swpaul } 141126706Swpaul error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args)); 142126706Swpaul if (error) 143126706Swpaul return error; 144126706Swpaul if (args.version != SMBFS_VERSION) { 145126706Swpaul printf("mount version mismatch: kernel=%d, mount=%d\n", 146126706Swpaul SMBFS_VERSION, args.version); 147126706Swpaul return EINVAL; 148126706Swpaul } 149126706Swpaul smb_makescred(&scred, td, td->td_ucred); 150126706Swpaul error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp); 151126706Swpaul if (error) { 152126706Swpaul printf("invalid device handle %d (%d)\n", args.dev, error); 153126706Swpaul return error; 154126706Swpaul } 155126706Swpaul vcp = SSTOVC(ssp); 156126706Swpaul smb_share_unlock(ssp, 0, td); 157126706Swpaul mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 158126706Swpaul 159126706Swpaul#ifdef SMBFS_USEZONE 160126706Swpaul smp = zalloc(smbfsmount_zone); 161126706Swpaul#else 162126706Swpaul MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, M_USE_RESERVE); 163126706Swpaul#endif 164126706Swpaul if (smp == NULL) { 165126706Swpaul printf("could not alloc smbmount\n"); 166126706Swpaul error = ENOMEM; 167126706Swpaul goto bad; 168126706Swpaul } 169126706Swpaul bzero(smp, sizeof(*smp)); 170126706Swpaul mp->mnt_data = (qaddr_t)smp; 171126706Swpaul smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 172126706Swpaul if (smp->sm_hash == NULL) 173126706Swpaul goto bad; 174126706Swpaul lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0); 175126706Swpaul smp->sm_share = ssp; 176126706Swpaul smp->sm_root = NULL; 177126706Swpaul smp->sm_args = args; 178126706Swpaul smp->sm_caseopt = args.caseopt; 179126706Swpaul smp->sm_args.file_mode = (smp->sm_args.file_mode & 180126706Swpaul (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 181126706Swpaul smp->sm_args.dir_mode = (smp->sm_args.dir_mode & 182126706Swpaul (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 183126706Swpaul 184126706Swpaul/* simple_lock_init(&smp->sm_npslock);*/ 185126706Swpaul#ifndef FB_CURRENT 186126706Swpaul error = copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 187126706Swpaul if (error) 188126706Swpaul goto bad; 189126706Swpaul bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 190126706Swpaul#endif 191126706Swpaul pc = mp->mnt_stat.f_mntfromname; 192126706Swpaul pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 193126706Swpaul bzero(pc, MNAMELEN); 194126706Swpaul *pc++ = '/'; 195126706Swpaul *pc++ = '/'; 196126706Swpaul pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 197126706Swpaul if (pc < pe-1) { 198126706Swpaul *(pc++) = '@'; 199126706Swpaul pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 200126706Swpaul if (pc < pe - 1) { 201126706Swpaul *(pc++) = '/'; 202126706Swpaul strncpy(pc, ssp->ss_name, pe - pc - 2); 203126706Swpaul } 204126706Swpaul } 205126706Swpaul /* protect against invalid mount points */ 206126706Swpaul smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0'; 207126706Swpaul vfs_getnewfsid(mp); 208126706Swpaul error = smbfs_root(mp, &vp); 209126706Swpaul if (error) 210126706Swpaul goto bad; 211126706Swpaul VOP_UNLOCK(vp, 0, td); 212126706Swpaul SMBVDEBUG("root.v_usecount = %d\n", vp->v_usecount); 213126706Swpaul 214126706Swpaul#ifdef DIAGNOSTICS 215126706Swpaul SMBERROR("mp=%p\n", mp); 216126706Swpaul#endif 217126706Swpaul return error; 218126706Swpaulbad: 219126706Swpaul if (smp) { 220126706Swpaul if (smp->sm_hash) 221126706Swpaul free(smp->sm_hash, M_SMBFSHASH); 222126706Swpaul lockdestroy(&smp->sm_hashlock); 223126706Swpaul#ifdef SMBFS_USEZONE 224126706Swpaul zfree(smbfsmount_zone, smp); 225126706Swpaul#else 226126706Swpaul free(smp, M_SMBFSDATA); 227126706Swpaul#endif 228126706Swpaul } 229126706Swpaul if (ssp) 230126706Swpaul smb_share_put(ssp, &scred); 231126706Swpaul return error; 232126706Swpaul} 233126706Swpaul 234126706Swpaul/* Unmount the filesystem described by mp. */ 235126706Swpaulstatic int 236126706Swpaulsmbfs_unmount(struct mount *mp, int mntflags, struct thread *td) 237126706Swpaul{ 238126706Swpaul struct smbmount *smp = VFSTOSMBFS(mp); 239126706Swpaul struct smb_cred scred; 240126706Swpaul int error, flags; 241126706Swpaul 242126706Swpaul SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 243126706Swpaul flags = 0; 244126706Swpaul if (mntflags & MNT_FORCE) 245126706Swpaul flags |= FORCECLOSE; 246126706Swpaul /* There is 1 extra root vnode reference from smbfs_mount(). */ 247126706Swpaul error = vflush(mp, 1, flags); 248126706Swpaul if (error) 249126706Swpaul return error; 250126706Swpaul smb_makescred(&scred, td, td->td_ucred); 251126706Swpaul smb_share_put(smp->sm_share, &scred); 252126706Swpaul mp->mnt_data = (qaddr_t)0; 253126706Swpaul 254126706Swpaul if (smp->sm_hash) 255126706Swpaul free(smp->sm_hash, M_SMBFSHASH); 256126706Swpaul lockdestroy(&smp->sm_hashlock); 257126706Swpaul#ifdef SMBFS_USEZONE 258126706Swpaul zfree(smbfsmount_zone, smp); 259126706Swpaul#else 260126706Swpaul free(smp, M_SMBFSDATA); 261126706Swpaul#endif 262126706Swpaul mp->mnt_flag &= ~MNT_LOCAL; 263126706Swpaul return error; 264126706Swpaul} 265126706Swpaul 266126780Swpaul/* 267126780Swpaul * Return locked root vnode of a filesystem 268126780Swpaul */ 269126780Swpaulstatic int 270126780Swpaulsmbfs_root(struct mount *mp, struct vnode **vpp) 271126780Swpaul{ 272126780Swpaul struct smbmount *smp = VFSTOSMBFS(mp); 273126780Swpaul struct vnode *vp; 274126780Swpaul struct smbnode *np; 275126780Swpaul struct smbfattr fattr; 276126780Swpaul struct thread *td = curthread; 277126780Swpaul struct ucred *cred = td->td_ucred; 278126780Swpaul struct smb_cred scred; 279126780Swpaul int error; 280126780Swpaul 281126780Swpaul if (smp == NULL) { 282126780Swpaul SMBERROR("smp == NULL (bug in umount)\n"); 283126780Swpaul return EINVAL; 284126780Swpaul } 285126780Swpaul if (smp->sm_root) { 286126706Swpaul *vpp = SMBTOV(smp->sm_root); 287126706Swpaul return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td); 288126706Swpaul } 289126706Swpaul smb_makescred(&scred, td, cred); 290126706Swpaul error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 291126706Swpaul if (error) 292126706Swpaul return error; 293126706Swpaul error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 294126706Swpaul if (error) 295126706Swpaul return error; 296126706Swpaul vp->v_flag |= VROOT; 297126706Swpaul np = VTOSMB(vp); 298126706Swpaul smp->sm_root = np; 299126706Swpaul *vpp = vp; 300126706Swpaul return 0; 301126706Swpaul} 302126706Swpaul 303126706Swpaul/* 304126706Swpaul * Vfs start routine, a no-op. 305126706Swpaul */ 306126706Swpaul/* ARGSUSED */ 307126706Swpaulstatic int 308126706Swpaulsmbfs_start(mp, flags, td) 309126706Swpaul struct mount *mp; 310126706Swpaul int flags; 311126706Swpaul struct thread *td; 312126706Swpaul{ 313126706Swpaul SMBVDEBUG("flags=%04x\n", flags); 314126706Swpaul return 0; 315126706Swpaul} 316126706Swpaul 317126706Swpaul/* 318126706Swpaul * Do operations associated with quotas, not supported 319126706Swpaul */ 320126706Swpaul/* ARGSUSED */ 321126706Swpaulstatic int 322126706Swpaulsmbfs_quotactl(mp, cmd, uid, arg, td) 323126706Swpaul struct mount *mp; 324126706Swpaul int cmd; 325126706Swpaul uid_t uid; 326126706Swpaul caddr_t arg; 327126706Swpaul struct thread *td; 328126706Swpaul{ 329126706Swpaul SMBVDEBUG("return EOPNOTSUPP\n"); 330126706Swpaul return EOPNOTSUPP; 331126706Swpaul} 332126706Swpaul 333126706Swpaul/*ARGSUSED*/ 334126706Swpaulint 335126706Swpaulsmbfs_init(struct vfsconf *vfsp) 336{ 337#ifndef SMP 338 int name[2]; 339 int olen, ncpu, plen, error; 340 341 name[0] = CTL_HW; 342 name[1] = HW_NCPU; 343 error = kernel_sysctl(curthread, name, 2, &ncpu, &olen, NULL, 0, &plen); 344 if (error == 0 && ncpu > 1) 345 printf("warning: smbfs module compiled without SMP support."); 346#endif 347 348#ifdef SMBFS_USEZONE 349 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 350#endif 351 smbfs_pbuf_freecnt = nswbuf / 2 + 1; 352 SMBVDEBUG("done.\n"); 353 return 0; 354} 355 356/*ARGSUSED*/ 357int 358smbfs_uninit(struct vfsconf *vfsp) 359{ 360 361 SMBVDEBUG("done.\n"); 362 return 0; 363} 364 365/* 366 * smbfs_statfs call 367 */ 368int 369smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 370{ 371 struct smbmount *smp = VFSTOSMBFS(mp); 372 struct smbnode *np = smp->sm_root; 373 struct smb_share *ssp = smp->sm_share; 374 struct smb_cred scred; 375 int error = 0; 376 377 if (np == NULL) 378 return EINVAL; 379 380 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 381 sbp->f_spare2 = 0; /* placeholder */ 382 smb_makescred(&scred, td, td->td_ucred); 383 384 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 385 error = smbfs_smb_statfs2(ssp, sbp, &scred); 386 else 387 error = smbfs_smb_statfs(ssp, sbp, &scred); 388 if (error) 389 return error; 390 sbp->f_flags = 0; /* copy of mount exported flags */ 391 if (sbp != &mp->mnt_stat) { 392 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */ 393 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */ 394 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */ 395 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 396 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 397 } 398 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 399 return 0; 400} 401 402/* 403 * Flush out the buffer cache 404 */ 405/* ARGSUSED */ 406static int 407smbfs_sync(mp, waitfor, cred, td) 408 struct mount *mp; 409 int waitfor; 410 struct ucred *cred; 411 struct thread *td; 412{ 413 struct vnode *vp; 414 int error, allerror = 0; 415 /* 416 * Force stale buffer cache information to be flushed. 417 */ 418loop: 419 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 420 vp != NULL; 421 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 422 /* 423 * If the vnode that we are about to sync is no longer 424 * associated with this mount point, start over. 425 */ 426 if (vp->v_mount != mp) 427 goto loop; 428#ifndef FB_RELENG3 429 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 430#else 431 if (VOP_ISLOCKED(vp) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 432#endif 433 waitfor == MNT_LAZY) 434 continue; 435 if (vget(vp, LK_EXCLUSIVE, td)) 436 goto loop; 437 error = VOP_FSYNC(vp, cred, waitfor, td); 438 if (error) 439 allerror = error; 440 vput(vp); 441 } 442 return (allerror); 443} 444 445