smbfs_vfsops.c revision 229272
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 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/fs/smbfs/smbfs_vfsops.c 229272 2012-01-02 12:12:10Z ed $ 27 */ 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/proc.h> 32#include <sys/bio.h> 33#include <sys/buf.h> 34#include <sys/kernel.h> 35#include <sys/sysctl.h> 36#include <sys/vnode.h> 37#include <sys/mount.h> 38#include <sys/stat.h> 39#include <sys/malloc.h> 40#include <sys/module.h> 41#include <sys/sx.h> 42 43 44#include <netsmb/smb.h> 45#include <netsmb/smb_conn.h> 46#include <netsmb/smb_subr.h> 47#include <netsmb/smb_dev.h> 48 49#include <fs/smbfs/smbfs.h> 50#include <fs/smbfs/smbfs_node.h> 51#include <fs/smbfs/smbfs_subr.h> 52 53static int smbfs_debuglevel = 0; 54 55static int smbfs_version = SMBFS_VERSION; 56 57#ifdef SMBFS_USEZONE 58#include <vm/vm.h> 59#include <vm/vm_extern.h> 60 61vm_zone_t smbfsmount_zone; 62#endif 63 64SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem"); 65SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 66SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 67 68static MALLOC_DEFINE(M_SMBFSHASH, "smbfs_hash", "SMBFS hash table"); 69 70static vfs_init_t smbfs_init; 71static vfs_uninit_t smbfs_uninit; 72static vfs_cmount_t smbfs_cmount; 73static vfs_mount_t smbfs_mount; 74static vfs_root_t smbfs_root; 75static vfs_quotactl_t smbfs_quotactl; 76static vfs_statfs_t smbfs_statfs; 77static vfs_unmount_t smbfs_unmount; 78 79static struct vfsops smbfs_vfsops = { 80 .vfs_init = smbfs_init, 81 .vfs_cmount = smbfs_cmount, 82 .vfs_mount = smbfs_mount, 83 .vfs_quotactl = smbfs_quotactl, 84 .vfs_root = smbfs_root, 85 .vfs_statfs = smbfs_statfs, 86 .vfs_sync = vfs_stdsync, 87 .vfs_uninit = smbfs_uninit, 88 .vfs_unmount = smbfs_unmount, 89}; 90 91 92VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 93 94MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 95MODULE_DEPEND(smbfs, libiconv, 1, 1, 2); 96MODULE_DEPEND(smbfs, libmchain, 1, 1, 1); 97 98int smbfs_pbuf_freecnt = -1; /* start out unlimited */ 99 100static int 101smbfs_cmount(struct mntarg *ma, void * data, int flags) 102{ 103 struct smbfs_args args; 104 int error; 105 106 error = copyin(data, &args, sizeof(struct smbfs_args)); 107 if (error) 108 return error; 109 110 if (args.version != SMBFS_VERSION) { 111 printf("mount version mismatch: kernel=%d, mount=%d\n", 112 SMBFS_VERSION, args.version); 113 return EINVAL; 114 } 115 ma = mount_argf(ma, "dev", "%d", args.dev); 116 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft"); 117 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr"); 118 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong"); 119 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls"); 120 ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong"); 121 ma = mount_arg(ma, "rootpath", args.root_path, -1); 122 ma = mount_argf(ma, "uid", "%d", args.uid); 123 ma = mount_argf(ma, "gid", "%d", args.gid); 124 ma = mount_argf(ma, "file_mode", "%d", args.file_mode); 125 ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode); 126 ma = mount_argf(ma, "caseopt", "%d", args.caseopt); 127 128 error = kernel_mount(ma, flags); 129 130 return (error); 131} 132 133static const char *smbfs_opts[] = { 134 "dev", "soft", "intr", "strong", "have_nls", "long", 135 "mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode", 136 "caseopt", "errmsg", NULL 137}; 138 139static int 140smbfs_mount(struct mount *mp) 141{ 142 struct smbmount *smp = NULL; 143 struct smb_vc *vcp; 144 struct smb_share *ssp = NULL; 145 struct vnode *vp; 146 struct thread *td; 147 struct smb_cred scred; 148 int error, v; 149 char *pc, *pe; 150 151 td = curthread; 152 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS)) 153 return EOPNOTSUPP; 154 155 if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) { 156 vfs_mount_error(mp, "%s", "Invalid option"); 157 return (EINVAL); 158 } 159 160 smb_makescred(&scred, td, td->td_ucred); 161 if (1 != vfs_scanopt(mp->mnt_optnew, "dev", "%d", &v)) { 162 vfs_mount_error(mp, "No dev option"); 163 return (EINVAL); 164 } 165 error = smb_dev2share(v, SMBM_EXEC, &scred, &ssp); 166 if (error) { 167 printf("invalid device handle %d (%d)\n", v, error); 168 vfs_mount_error(mp, "invalid device handle %d (%d)\n", v, error); 169 return error; 170 } 171 vcp = SSTOVC(ssp); 172 smb_share_unlock(ssp, 0); 173 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 174 175#ifdef SMBFS_USEZONE 176 smp = zalloc(smbfsmount_zone); 177#else 178 smp = malloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK); 179#endif 180 if (smp == NULL) { 181 printf("could not alloc smbmount\n"); 182 vfs_mount_error(mp, "could not alloc smbmount", v, error); 183 error = ENOMEM; 184 goto bad; 185 } 186 bzero(smp, sizeof(*smp)); 187 mp->mnt_data = smp; 188 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 189 if (smp->sm_hash == NULL) 190 goto bad; 191 sx_init(&smp->sm_hashlock, "smbfsh"); 192 smp->sm_share = ssp; 193 smp->sm_root = NULL; 194 if (1 != vfs_scanopt(mp->mnt_optnew, 195 "caseopt", "%d", &smp->sm_caseopt)) { 196 vfs_mount_error(mp, "Invalid caseopt"); 197 error = EINVAL; 198 goto bad; 199 } 200 if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) { 201 vfs_mount_error(mp, "Invalid uid"); 202 error = EINVAL; 203 goto bad; 204 } 205 smp->sm_uid = v; 206 207 if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) { 208 vfs_mount_error(mp, "Invalid gid"); 209 error = EINVAL; 210 goto bad; 211 } 212 smp->sm_gid = v; 213 214 if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) { 215 vfs_mount_error(mp, "Invalid file_mode"); 216 error = EINVAL; 217 goto bad; 218 } 219 smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 220 221 if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) { 222 vfs_mount_error(mp, "Invalid dir_mode"); 223 error = EINVAL; 224 goto bad; 225 } 226 smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 227 228 vfs_flagopt(mp->mnt_optnew, 229 "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG); 230 231/* simple_lock_init(&smp->sm_npslock);*/ 232 pc = mp->mnt_stat.f_mntfromname; 233 pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 234 bzero(pc, MNAMELEN); 235 *pc++ = '/'; 236 *pc++ = '/'; 237 pc = strchr(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 238 if (pc < pe-1) { 239 *(pc++) = '@'; 240 pc = strchr(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 241 if (pc < pe - 1) { 242 *(pc++) = '/'; 243 strncpy(pc, ssp->ss_name, pe - pc - 2); 244 } 245 } 246 vfs_getnewfsid(mp); 247 error = smbfs_root(mp, LK_EXCLUSIVE, &vp); 248 if (error) { 249 vfs_mount_error(mp, "smbfs_root error: %d", error); 250 goto bad; 251 } 252 VOP_UNLOCK(vp, 0); 253 SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp)); 254 255#ifdef DIAGNOSTIC 256 SMBERROR("mp=%p\n", mp); 257#endif 258 return error; 259bad: 260 if (smp) { 261 if (smp->sm_hash) 262 free(smp->sm_hash, M_SMBFSHASH); 263 sx_destroy(&smp->sm_hashlock); 264#ifdef SMBFS_USEZONE 265 zfree(smbfsmount_zone, smp); 266#else 267 free(smp, M_SMBFSDATA); 268#endif 269 } 270 if (ssp) 271 smb_share_put(ssp, &scred); 272 return error; 273} 274 275/* Unmount the filesystem described by mp. */ 276static int 277smbfs_unmount(struct mount *mp, int mntflags) 278{ 279 struct thread *td; 280 struct smbmount *smp = VFSTOSMBFS(mp); 281 struct smb_cred scred; 282 int error, flags; 283 284 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 285 td = curthread; 286 flags = 0; 287 if (mntflags & MNT_FORCE) 288 flags |= FORCECLOSE; 289 /* 290 * Keep trying to flush the vnode list for the mount while 291 * some are still busy and we are making progress towards 292 * making them not busy. This is needed because smbfs vnodes 293 * reference their parent directory but may appear after their 294 * parent in the list; one pass over the vnode list is not 295 * sufficient in this case. 296 */ 297 do { 298 smp->sm_didrele = 0; 299 /* There is 1 extra root vnode reference from smbfs_mount(). */ 300 error = vflush(mp, 1, flags, td); 301 } while (error == EBUSY && smp->sm_didrele != 0); 302 if (error) 303 return error; 304 smb_makescred(&scred, td, td->td_ucred); 305 error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE); 306 if (error) 307 return error; 308 smb_share_put(smp->sm_share, &scred); 309 mp->mnt_data = NULL; 310 311 if (smp->sm_hash) 312 free(smp->sm_hash, M_SMBFSHASH); 313 sx_destroy(&smp->sm_hashlock); 314#ifdef SMBFS_USEZONE 315 zfree(smbfsmount_zone, smp); 316#else 317 free(smp, M_SMBFSDATA); 318#endif 319 MNT_ILOCK(mp); 320 mp->mnt_flag &= ~MNT_LOCAL; 321 MNT_IUNLOCK(mp); 322 return error; 323} 324 325/* 326 * Return locked root vnode of a filesystem 327 */ 328static int 329smbfs_root(struct mount *mp, int flags, struct vnode **vpp) 330{ 331 struct smbmount *smp = VFSTOSMBFS(mp); 332 struct vnode *vp; 333 struct smbnode *np; 334 struct smbfattr fattr; 335 struct thread *td; 336 struct ucred *cred; 337 struct smb_cred scred; 338 int error; 339 340 td = curthread; 341 cred = td->td_ucred; 342 343 if (smp == NULL) { 344 SMBERROR("smp == NULL (bug in umount)\n"); 345 vfs_mount_error(mp, "smp == NULL (bug in umount)"); 346 return EINVAL; 347 } 348 if (smp->sm_root) { 349 *vpp = SMBTOV(smp->sm_root); 350 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td); 351 } 352 smb_makescred(&scred, td, cred); 353 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 354 if (error) 355 return error; 356 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 357 if (error) 358 return error; 359 ASSERT_VOP_LOCKED(vp, "smbfs_root"); 360 vp->v_vflag |= VV_ROOT; 361 np = VTOSMB(vp); 362 smp->sm_root = np; 363 *vpp = vp; 364 return 0; 365} 366 367/* 368 * Do operations associated with quotas, not supported 369 */ 370/* ARGSUSED */ 371static int 372smbfs_quotactl(mp, cmd, uid, arg) 373 struct mount *mp; 374 int cmd; 375 uid_t uid; 376 void *arg; 377{ 378 SMBVDEBUG("return EOPNOTSUPP\n"); 379 return EOPNOTSUPP; 380} 381 382/*ARGSUSED*/ 383int 384smbfs_init(struct vfsconf *vfsp) 385{ 386#ifdef SMBFS_USEZONE 387 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 388#endif 389 smbfs_pbuf_freecnt = nswbuf / 2 + 1; 390 SMBVDEBUG("done.\n"); 391 return 0; 392} 393 394/*ARGSUSED*/ 395int 396smbfs_uninit(struct vfsconf *vfsp) 397{ 398 399 SMBVDEBUG("done.\n"); 400 return 0; 401} 402 403/* 404 * smbfs_statfs call 405 */ 406int 407smbfs_statfs(struct mount *mp, struct statfs *sbp) 408{ 409 struct thread *td = curthread; 410 struct smbmount *smp = VFSTOSMBFS(mp); 411 struct smbnode *np = smp->sm_root; 412 struct smb_share *ssp = smp->sm_share; 413 struct smb_cred scred; 414 int error = 0; 415 416 if (np == NULL) { 417 vfs_mount_error(mp, "np == NULL"); 418 return EINVAL; 419 } 420 421 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 422 smb_makescred(&scred, td, td->td_ucred); 423 424 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 425 error = smbfs_smb_statfs2(ssp, sbp, &scred); 426 else 427 error = smbfs_smb_statfs(ssp, sbp, &scred); 428 if (error) 429 return error; 430 sbp->f_flags = 0; /* copy of mount exported flags */ 431 return 0; 432} 433