smbfs_vfsops.c revision 137479
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 137479 2004-11-09 22:21:10Z phk $ 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 70vm_zone_t smbfsmount_zone; 71#endif 72 73SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem"); 74SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 75SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 76 77static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table"); 78 79static vfs_init_t smbfs_init; 80static vfs_uninit_t smbfs_uninit; 81static vfs_omount_t smbfs_omount; 82static vfs_start_t smbfs_start; 83static vfs_root_t smbfs_root; 84static vfs_quotactl_t smbfs_quotactl; 85static vfs_statfs_t smbfs_statfs; 86static vfs_unmount_t smbfs_unmount; 87 88static struct vfsops smbfs_vfsops = { 89 .vfs_init = smbfs_init, 90 .vfs_omount = smbfs_omount, 91 .vfs_quotactl = smbfs_quotactl, 92 .vfs_root = smbfs_root, 93 .vfs_start = smbfs_start, 94 .vfs_statfs = smbfs_statfs, 95 .vfs_sync = vfs_stdsync, 96 .vfs_uninit = smbfs_uninit, 97 .vfs_unmount = smbfs_unmount, 98}; 99 100 101VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 102 103MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 104MODULE_DEPEND(smbfs, libiconv, 1, 1, 2); 105MODULE_DEPEND(smbfs, libmchain, 1, 1, 1); 106 107int smbfs_pbuf_freecnt = -1; /* start out unlimited */ 108 109static int 110smbfs_omount(struct mount *mp, char *path, caddr_t data, struct thread *td) 111{ 112 struct smbfs_args args; /* will hold data from mount request */ 113 struct smbmount *smp = NULL; 114 struct smb_vc *vcp; 115 struct smb_share *ssp = NULL; 116 struct vnode *vp; 117 struct smb_cred scred; 118 int error; 119 char *pc, *pe; 120 121 if (data == NULL) { 122 printf("missing data argument\n"); 123 return EINVAL; 124 } 125 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS)) 126 return EOPNOTSUPP; 127 128 error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args)); 129 if (error) 130 return error; 131 if (args.version != SMBFS_VERSION) { 132 printf("mount version mismatch: kernel=%d, mount=%d\n", 133 SMBFS_VERSION, args.version); 134 return EINVAL; 135 } 136 smb_makescred(&scred, td, td->td_ucred); 137 error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp); 138 if (error) { 139 printf("invalid device handle %d (%d)\n", args.dev, error); 140 return error; 141 } 142 vcp = SSTOVC(ssp); 143 smb_share_unlock(ssp, 0, td); 144 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 145 146#ifdef SMBFS_USEZONE 147 smp = zalloc(smbfsmount_zone); 148#else 149 MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, 150 M_WAITOK|M_USE_RESERVE); 151#endif 152 if (smp == NULL) { 153 printf("could not alloc smbmount\n"); 154 error = ENOMEM; 155 goto bad; 156 } 157 bzero(smp, sizeof(*smp)); 158 mp->mnt_data = (qaddr_t)smp; 159 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 160 if (smp->sm_hash == NULL) 161 goto bad; 162 lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0); 163 smp->sm_share = ssp; 164 smp->sm_root = NULL; 165 smp->sm_args = args; 166 smp->sm_caseopt = args.caseopt; 167 smp->sm_args.file_mode = (smp->sm_args.file_mode & 168 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 169 smp->sm_args.dir_mode = (smp->sm_args.dir_mode & 170 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 171 172/* simple_lock_init(&smp->sm_npslock);*/ 173 pc = mp->mnt_stat.f_mntfromname; 174 pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 175 bzero(pc, MNAMELEN); 176 *pc++ = '/'; 177 *pc++ = '/'; 178 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 179 if (pc < pe-1) { 180 *(pc++) = '@'; 181 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 182 if (pc < pe - 1) { 183 *(pc++) = '/'; 184 strncpy(pc, ssp->ss_name, pe - pc - 2); 185 } 186 } 187 /* protect against invalid mount points */ 188 smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0'; 189 vfs_getnewfsid(mp); 190 error = smbfs_root(mp, &vp, td); 191 if (error) 192 goto bad; 193 VOP_UNLOCK(vp, 0, td); 194 SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp)); 195 196#ifdef DIAGNOSTICS 197 SMBERROR("mp=%p\n", mp); 198#endif 199 return error; 200bad: 201 if (smp) { 202 if (smp->sm_hash) 203 free(smp->sm_hash, M_SMBFSHASH); 204 lockdestroy(&smp->sm_hashlock); 205#ifdef SMBFS_USEZONE 206 zfree(smbfsmount_zone, smp); 207#else 208 free(smp, M_SMBFSDATA); 209#endif 210 } 211 if (ssp) 212 smb_share_put(ssp, &scred); 213 return error; 214} 215 216/* Unmount the filesystem described by mp. */ 217static int 218smbfs_unmount(struct mount *mp, int mntflags, struct thread *td) 219{ 220 struct smbmount *smp = VFSTOSMBFS(mp); 221 struct smb_cred scred; 222 int error, flags; 223 224 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 225 flags = 0; 226 if (mntflags & MNT_FORCE) 227 flags |= FORCECLOSE; 228 /* 229 * Keep trying to flush the vnode list for the mount while 230 * some are still busy and we are making progress towards 231 * making them not busy. This is needed because smbfs vnodes 232 * reference their parent directory but may appear after their 233 * parent in the list; one pass over the vnode list is not 234 * sufficient in this case. 235 */ 236 do { 237 smp->sm_didrele = 0; 238 /* There is 1 extra root vnode reference from smbfs_mount(). */ 239 error = vflush(mp, 1, flags, td); 240 } while (error == EBUSY && smp->sm_didrele != 0); 241 if (error) 242 return error; 243 smb_makescred(&scred, td, td->td_ucred); 244 smb_share_put(smp->sm_share, &scred); 245 mp->mnt_data = (qaddr_t)0; 246 247 if (smp->sm_hash) 248 free(smp->sm_hash, M_SMBFSHASH); 249 lockdestroy(&smp->sm_hashlock); 250#ifdef SMBFS_USEZONE 251 zfree(smbfsmount_zone, smp); 252#else 253 free(smp, M_SMBFSDATA); 254#endif 255 mp->mnt_flag &= ~MNT_LOCAL; 256 return error; 257} 258 259/* 260 * Return locked root vnode of a filesystem 261 */ 262static int 263smbfs_root(struct mount *mp, struct vnode **vpp, struct thread *td) 264{ 265 struct smbmount *smp = VFSTOSMBFS(mp); 266 struct vnode *vp; 267 struct smbnode *np; 268 struct smbfattr fattr; 269 struct ucred *cred = td->td_ucred; 270 struct smb_cred scred; 271 int error; 272 273 if (smp == NULL) { 274 SMBERROR("smp == NULL (bug in umount)\n"); 275 return EINVAL; 276 } 277 if (smp->sm_root) { 278 *vpp = SMBTOV(smp->sm_root); 279 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td); 280 } 281 smb_makescred(&scred, td, cred); 282 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 283 if (error) 284 return error; 285 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 286 if (error) 287 return error; 288 ASSERT_VOP_LOCKED(vp, "smbfs_root"); 289 vp->v_vflag |= VV_ROOT; 290 np = VTOSMB(vp); 291 smp->sm_root = np; 292 *vpp = vp; 293 return 0; 294} 295 296/* 297 * Vfs start routine, a no-op. 298 */ 299/* ARGSUSED */ 300static int 301smbfs_start(mp, flags, td) 302 struct mount *mp; 303 int flags; 304 struct thread *td; 305{ 306 SMBVDEBUG("flags=%04x\n", flags); 307 return 0; 308} 309 310/* 311 * Do operations associated with quotas, not supported 312 */ 313/* ARGSUSED */ 314static int 315smbfs_quotactl(mp, cmd, uid, arg, td) 316 struct mount *mp; 317 int cmd; 318 uid_t uid; 319 caddr_t arg; 320 struct thread *td; 321{ 322 SMBVDEBUG("return EOPNOTSUPP\n"); 323 return EOPNOTSUPP; 324} 325 326/*ARGSUSED*/ 327int 328smbfs_init(struct vfsconf *vfsp) 329{ 330#ifdef SMBFS_USEZONE 331 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 332#endif 333 smbfs_pbuf_freecnt = nswbuf / 2 + 1; 334 SMBVDEBUG("done.\n"); 335 return 0; 336} 337 338/*ARGSUSED*/ 339int 340smbfs_uninit(struct vfsconf *vfsp) 341{ 342 343 SMBVDEBUG("done.\n"); 344 return 0; 345} 346 347/* 348 * smbfs_statfs call 349 */ 350int 351smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 352{ 353 struct smbmount *smp = VFSTOSMBFS(mp); 354 struct smbnode *np = smp->sm_root; 355 struct smb_share *ssp = smp->sm_share; 356 struct smb_cred scred; 357 int error = 0; 358 359 if (np == NULL) 360 return EINVAL; 361 362 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 363 smb_makescred(&scred, td, td->td_ucred); 364 365 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 366 error = smbfs_smb_statfs2(ssp, sbp, &scred); 367 else 368 error = smbfs_smb_statfs(ssp, sbp, &scred); 369 if (error) 370 return error; 371 sbp->f_flags = 0; /* copy of mount exported flags */ 372 if (sbp != &mp->mnt_stat) { 373 sbp->f_fsid = mp->mnt_stat.f_fsid; /* filesystem id */ 374 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */ 375 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */ 376 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 377 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 378 } 379 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 380 return 0; 381} 382