smbfs_vfsops.c revision 92462
1/* 2 * Copyright (c) 2000-2001, Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD: head/sys/fs/smbfs/smbfs_vfsops.c 92462 2002-03-17 01:25:47Z mckusick $ 33 */ 34#include "opt_netsmb.h" 35#ifndef NETSMB 36#error "SMBFS requires option NETSMB" 37#endif 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/proc.h> 42#include <sys/bio.h> 43#include <sys/buf.h> 44#include <sys/kernel.h> 45#include <sys/sysctl.h> 46#include <sys/vnode.h> 47#include <sys/mount.h> 48#include <sys/stat.h> 49#include <sys/malloc.h> 50#include <sys/module.h> 51 52 53#include <netsmb/smb.h> 54#include <netsmb/smb_conn.h> 55#include <netsmb/smb_subr.h> 56#include <netsmb/smb_dev.h> 57 58#include <fs/smbfs/smbfs.h> 59#include <fs/smbfs/smbfs_node.h> 60#include <fs/smbfs/smbfs_subr.h> 61 62int smbfs_debuglevel = 0; 63 64static int smbfs_version = SMBFS_VERSION; 65 66#ifdef SMBFS_USEZONE 67#include <vm/vm.h> 68#include <vm/vm_extern.h> 69#include <vm/vm_zone.h> 70 71vm_zone_t smbfsmount_zone; 72#endif 73 74SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system"); 75SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 76SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 77 78static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table"); 79 80 81static int smbfs_mount(struct mount *, char *, caddr_t, 82 struct nameidata *, struct thread *); 83static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *); 84static int smbfs_root(struct mount *, struct vnode **); 85static int smbfs_start(struct mount *, int, struct thread *); 86static int smbfs_statfs(struct mount *, struct statfs *, struct thread *); 87static int smbfs_sync(struct mount *, int, struct ucred *, struct thread *); 88static int smbfs_unmount(struct mount *, int, struct thread *); 89static int smbfs_init(struct vfsconf *vfsp); 90static int smbfs_uninit(struct vfsconf *vfsp); 91 92#if __FreeBSD_version < 400009 93static int smbfs_vget(struct mount *mp, ino_t ino, int flags, 94 struct vnode **vpp); 95static int smbfs_fhtovp(struct mount *, struct fid *, 96 struct sockaddr *, struct vnode **, int *, 97 struct ucred **); 98static int smbfs_vptofh(struct vnode *, struct fid *); 99#endif 100 101static struct vfsops smbfs_vfsops = { 102 smbfs_mount, 103 smbfs_start, 104 smbfs_unmount, 105 smbfs_root, 106 smbfs_quotactl, 107 smbfs_statfs, 108 smbfs_sync, 109#if __FreeBSD_version > 400008 110 vfs_stdvget, 111 vfs_stdfhtovp, /* shouldn't happen */ 112 vfs_stdcheckexp, 113 vfs_stdvptofh, /* shouldn't happen */ 114#else 115 smbfs_vget, 116 smbfs_fhtovp, 117 smbfs_vptofh, 118#endif 119 smbfs_init, 120 smbfs_uninit, 121#ifndef FB_RELENG3 122 vfs_stdextattrctl, 123#else 124#define M_USE_RESERVE M_KERNEL 125 &sysctl___vfs_smbfs 126#endif 127}; 128 129 130VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 131 132MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 133MODULE_DEPEND(smbfs, libiconv, 1, 1, 1); 134MODULE_DEPEND(smbfs, libmchain, 1, 1, 1); 135 136int smbfs_pbuf_freecnt = -1; /* start out unlimited */ 137 138static int 139smbfs_mount(struct mount *mp, char *path, caddr_t data, 140 struct nameidata *ndp, struct thread *td) 141{ 142 struct smbfs_args args; /* will hold data from mount request */ 143 struct smbmount *smp = NULL; 144 struct smb_vc *vcp; 145 struct smb_share *ssp = NULL; 146 struct vnode *vp; 147 struct smb_cred scred; 148#ifndef FB_CURRENT 149 size_t size; 150#endif 151 int error; 152 char *pc, *pe; 153 154 if (data == NULL) { 155 printf("missing data argument\n"); 156 return EINVAL; 157 } 158 if (mp->mnt_flag & MNT_UPDATE) { 159 printf("MNT_UPDATE not implemented"); 160 return EOPNOTSUPP; 161 } 162 error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args)); 163 if (error) 164 return error; 165 if (args.version != SMBFS_VERSION) { 166 printf("mount version mismatch: kernel=%d, mount=%d\n", 167 SMBFS_VERSION, args.version); 168 return EINVAL; 169 } 170 smb_makescred(&scred, td, td->td_ucred); 171 error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp); 172 if (error) { 173 printf("invalid device handle %d (%d)\n", args.dev, error); 174 return error; 175 } 176 vcp = SSTOVC(ssp); 177 smb_share_unlock(ssp, 0, td); 178 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 179 180#ifdef SMBFS_USEZONE 181 smp = zalloc(smbfsmount_zone); 182#else 183 MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, M_USE_RESERVE); 184#endif 185 if (smp == NULL) { 186 printf("could not alloc smbmount\n"); 187 error = ENOMEM; 188 goto bad; 189 } 190 bzero(smp, sizeof(*smp)); 191 mp->mnt_data = (qaddr_t)smp; 192 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 193 if (smp->sm_hash == NULL) 194 goto bad; 195 lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0); 196 smp->sm_share = ssp; 197 smp->sm_root = NULL; 198 smp->sm_args = args; 199 smp->sm_caseopt = args.caseopt; 200 smp->sm_args.file_mode = (smp->sm_args.file_mode & 201 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 202 smp->sm_args.dir_mode = (smp->sm_args.dir_mode & 203 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 204 205/* simple_lock_init(&smp->sm_npslock);*/ 206#ifndef FB_CURRENT 207 error = copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 208 if (error) 209 goto bad; 210 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 211#endif 212 pc = mp->mnt_stat.f_mntfromname; 213 pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 214 bzero(pc, MNAMELEN); 215 *pc++ = '/'; 216 *pc++ = '/'; 217 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 218 if (pc < pe-1) { 219 *(pc++) = '@'; 220 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 221 if (pc < pe - 1) { 222 *(pc++) = '/'; 223 strncpy(pc, ssp->ss_name, pe - pc - 2); 224 } 225 } 226 /* protect against invalid mount points */ 227 smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0'; 228 vfs_getnewfsid(mp); 229 error = smbfs_root(mp, &vp); 230 if (error) 231 goto bad; 232 VOP_UNLOCK(vp, 0, td); 233 SMBVDEBUG("root.v_usecount = %d\n", vp->v_usecount); 234 235#ifdef DIAGNOSTICS 236 SMBERROR("mp=%p\n", mp); 237#endif 238 return error; 239bad: 240 if (smp) { 241 if (smp->sm_hash) 242 free(smp->sm_hash, M_SMBFSHASH); 243 lockdestroy(&smp->sm_hashlock); 244#ifdef SMBFS_USEZONE 245 zfree(smbfsmount_zone, smp); 246#else 247 free(smp, M_SMBFSDATA); 248#endif 249 } 250 if (ssp) 251 smb_share_put(ssp, &scred); 252 return error; 253} 254 255/* Unmount the filesystem described by mp. */ 256static int 257smbfs_unmount(struct mount *mp, int mntflags, struct thread *td) 258{ 259 struct smbmount *smp = VFSTOSMBFS(mp); 260 struct smb_cred scred; 261 int error, flags; 262 263 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 264 flags = 0; 265 if (mntflags & MNT_FORCE) 266 flags |= FORCECLOSE; 267 /* There is 1 extra root vnode reference from smbfs_mount(). */ 268 error = vflush(mp, 1, flags); 269 if (error) 270 return error; 271 smb_makescred(&scred, td, td->td_ucred); 272 smb_share_put(smp->sm_share, &scred); 273 mp->mnt_data = (qaddr_t)0; 274 275 if (smp->sm_hash) 276 free(smp->sm_hash, M_SMBFSHASH); 277 lockdestroy(&smp->sm_hashlock); 278#ifdef SMBFS_USEZONE 279 zfree(smbfsmount_zone, smp); 280#else 281 free(smp, M_SMBFSDATA); 282#endif 283 mp->mnt_flag &= ~MNT_LOCAL; 284 return error; 285} 286 287/* 288 * Return locked root vnode of a filesystem 289 */ 290static int 291smbfs_root(struct mount *mp, struct vnode **vpp) 292{ 293 struct smbmount *smp = VFSTOSMBFS(mp); 294 struct vnode *vp; 295 struct smbnode *np; 296 struct smbfattr fattr; 297 struct thread *td = curthread; 298 struct ucred *cred = td->td_ucred; 299 struct smb_cred scred; 300 int error; 301 302 if (smp == NULL) { 303 SMBERROR("smp == NULL (bug in umount)\n"); 304 return EINVAL; 305 } 306 if (smp->sm_root) { 307 *vpp = SMBTOV(smp->sm_root); 308 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td); 309 } 310 smb_makescred(&scred, td, cred); 311 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 312 if (error) 313 return error; 314 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 315 if (error) 316 return error; 317 vp->v_flag |= VROOT; 318 np = VTOSMB(vp); 319 smp->sm_root = np; 320 *vpp = vp; 321 return 0; 322} 323 324/* 325 * Vfs start routine, a no-op. 326 */ 327/* ARGSUSED */ 328static int 329smbfs_start(mp, flags, td) 330 struct mount *mp; 331 int flags; 332 struct thread *td; 333{ 334 SMBVDEBUG("flags=%04x\n", flags); 335 return 0; 336} 337 338/* 339 * Do operations associated with quotas, not supported 340 */ 341/* ARGSUSED */ 342static int 343smbfs_quotactl(mp, cmd, uid, arg, td) 344 struct mount *mp; 345 int cmd; 346 uid_t uid; 347 caddr_t arg; 348 struct thread *td; 349{ 350 SMBVDEBUG("return EOPNOTSUPP\n"); 351 return EOPNOTSUPP; 352} 353 354/*ARGSUSED*/ 355int 356smbfs_init(struct vfsconf *vfsp) 357{ 358#ifndef SMP 359 int name[2]; 360 int olen, ncpu, plen, error; 361 362 name[0] = CTL_HW; 363 name[1] = HW_NCPU; 364 error = kernel_sysctl(curthread, name, 2, &ncpu, &olen, NULL, 0, &plen); 365 if (error == 0 && ncpu > 1) 366 printf("warning: smbfs module compiled without SMP support."); 367#endif 368 369#ifdef SMBFS_USEZONE 370 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 371#endif 372 smbfs_pbuf_freecnt = nswbuf / 2 + 1; 373 SMBVDEBUG("done.\n"); 374 return 0; 375} 376 377/*ARGSUSED*/ 378int 379smbfs_uninit(struct vfsconf *vfsp) 380{ 381 382 SMBVDEBUG("done.\n"); 383 return 0; 384} 385 386/* 387 * smbfs_statfs call 388 */ 389int 390smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 391{ 392 struct smbmount *smp = VFSTOSMBFS(mp); 393 struct smbnode *np = smp->sm_root; 394 struct smb_share *ssp = smp->sm_share; 395 struct smb_cred scred; 396 int error = 0; 397 398 if (np == NULL) 399 return EINVAL; 400 401 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 402 sbp->f_spare2 = 0; /* placeholder */ 403 smb_makescred(&scred, td, td->td_ucred); 404 405 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 406 error = smbfs_smb_statfs2(ssp, sbp, &scred); 407 else 408 error = smbfs_smb_statfs(ssp, sbp, &scred); 409 if (error) 410 return error; 411 sbp->f_flags = 0; /* copy of mount exported flags */ 412 if (sbp != &mp->mnt_stat) { 413 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */ 414 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */ 415 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */ 416 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 417 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 418 } 419 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 420 return 0; 421} 422 423/* 424 * Flush out the buffer cache 425 */ 426/* ARGSUSED */ 427static int 428smbfs_sync(mp, waitfor, cred, td) 429 struct mount *mp; 430 int waitfor; 431 struct ucred *cred; 432 struct thread *td; 433{ 434 struct vnode *vp; 435 int error, allerror = 0; 436 /* 437 * Force stale buffer cache information to be flushed. 438 */ 439loop: 440 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 441 vp != NULL; 442 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 443 /* 444 * If the vnode that we are about to sync is no longer 445 * associated with this mount point, start over. 446 */ 447 if (vp->v_mount != mp) 448 goto loop; 449#ifndef FB_RELENG3 450 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 451#else 452 if (VOP_ISLOCKED(vp) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 453#endif 454 waitfor == MNT_LAZY) 455 continue; 456 if (vget(vp, LK_EXCLUSIVE, td)) 457 goto loop; 458 error = VOP_FSYNC(vp, cred, waitfor, td); 459 if (error) 460 allerror = error; 461 vput(vp); 462 } 463 return (allerror); 464} 465 466#if __FreeBSD_version < 400009 467/* 468 * smbfs flat namespace lookup. Unsupported. 469 */ 470/* ARGSUSED */ 471static int smbfs_vget(mp, ino, flags, vpp) 472 struct mount *mp; 473 ino_t ino; 474 int flags; 475 struct vnode **vpp; 476{ 477 return (EOPNOTSUPP); 478} 479 480/* ARGSUSED */ 481static int smbfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 482 struct mount *mp; 483 struct fid *fhp; 484 struct sockaddr *nam; 485 struct vnode **vpp; 486 int *exflagsp; 487 struct ucred **credanonp; 488{ 489 return (EINVAL); 490} 491 492/* 493 * Vnode pointer to File handle, should never happen either 494 */ 495/* ARGSUSED */ 496static int 497smbfs_vptofh(vp, fhp) 498 struct vnode *vp; 499 struct fid *fhp; 500{ 501 return (EINVAL); 502} 503 504#endif /* __FreeBSD_version < 400009 */ 505