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