1 /*- 2 * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org) 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: src/sys/fs/hpfs/hpfs_vfsops.c,v 1.3.2.2 2001/12/25 01:44:45 dillon Exp $ 27 * $DragonFly: src/sys/vfs/hpfs/hpfs_vfsops.c,v 1.43 2008/09/17 21:44:22 dillon Exp $ 28 */ 29 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/nlookup.h> 34 #include <sys/conf.h> 35 #include <sys/proc.h> 36 #include <sys/kernel.h> 37 #include <sys/vnode.h> 38 #include <sys/mount.h> 39 #include <sys/buf.h> 40 #include <sys/fcntl.h> 41 #include <sys/malloc.h> 42 43 #include <machine/inttypes.h> 44 45 #include <vm/vm.h> 46 #include <vm/vm_param.h> 47 #if defined(__NetBSD__) 48 #include <vm/vm_prot.h> 49 #endif 50 #include <vm/vm_page.h> 51 #include <vm/vm_object.h> 52 #include <vm/vm_extern.h> 53 #include <sys/buf2.h> 54 55 #if defined(__NetBSD__) 56 #include <miscfs/specfs/specdev.h> 57 #endif 58 59 #include "hpfs.h" 60 #include "hpfsmount.h" 61 #include "hpfs_subr.h" 62 63 extern struct vop_ops hpfs_vnode_vops; 64 65 MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure"); 66 MALLOC_DEFINE(M_HPFSNO, "HPFS node", "HPFS node structure"); 67 68 static int hpfs_root (struct mount *, struct vnode **); 69 static int hpfs_statfs (struct mount *, struct statfs *, struct ucred *); 70 static int hpfs_unmount (struct mount *, int); 71 static int hpfs_vget (struct mount *mp, ino_t ino, 72 struct vnode **vpp); 73 static int hpfs_mountfs (struct vnode *, struct mount *, 74 struct hpfs_args *); 75 static int hpfs_vptofh (struct vnode *, struct fid *); 76 static int hpfs_fhtovp (struct mount *, struct vnode *, 77 struct fid *, struct vnode **); 78 79 80 struct sockaddr; 81 static int hpfs_mount (struct mount *, char *, caddr_t, struct ucred *); 82 static int hpfs_init (struct vfsconf *); 83 static int hpfs_checkexp (struct mount *, struct sockaddr *, 84 int *, struct ucred **); 85 86 /*ARGSUSED*/ 87 static int 88 hpfs_checkexp(struct mount *mp, 89 struct sockaddr *nam, 90 int *exflagsp, struct ucred **credanonp) 91 { 92 struct netcred *np; 93 struct hpfsmount *hpm = VFSTOHPFS(mp); 94 95 /* 96 * Get the export permission structure for this <mp, client> tuple. 97 */ 98 np = vfs_export_lookup(mp, &hpm->hpm_export, nam); 99 if (np == NULL) 100 return (EACCES); 101 102 *exflagsp = np->netc_exflags; 103 *credanonp = &np->netc_anon; 104 return (0); 105 } 106 107 static int 108 hpfs_init(struct vfsconf *vcp) 109 { 110 dprintf(("hpfs_init():\n")); 111 112 hpfs_hphashinit(); 113 return 0; 114 } 115 116 static int 117 hpfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) 118 { 119 size_t size; 120 int error; 121 struct vnode *devvp; 122 struct hpfs_args args; 123 struct hpfsmount *hpmp = 0; 124 struct nlookupdata nd; 125 126 dprintf(("hpfs_mount():\n")); 127 /* 128 *** 129 * Mounting non-root file system or updating a file system 130 *** 131 */ 132 133 /* copy in user arguments*/ 134 error = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args)); 135 if (error) 136 goto error_1; /* can't get arguments*/ 137 138 /* 139 * If updating, check whether changing from read-only to 140 * read/write; if there is no device name, that's all we do. 141 */ 142 if (mp->mnt_flag & MNT_UPDATE) { 143 dprintf(("hpfs_mount: MNT_UPDATE: ")); 144 145 hpmp = VFSTOHPFS(mp); 146 147 if (args.fspec == 0) { 148 dprintf(("export 0x%x\n",args.export.ex_flags)); 149 error = vfs_export(mp, &hpmp->hpm_export, &args.export); 150 if (error) { 151 kprintf("hpfs_mount: vfs_export failed %d\n", 152 error); 153 } 154 goto success; 155 } else { 156 dprintf(("name [FAILED]\n")); 157 error = EINVAL; 158 goto success; 159 } 160 dprintf(("\n")); 161 } 162 163 /* 164 * Not an update, or updating the name: look up the name 165 * and verify that it refers to a sensible block device. 166 */ 167 devvp = NULL; 168 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW); 169 if (error == 0) 170 error = nlookup(&nd); 171 if (error == 0) 172 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp); 173 nlookup_done(&nd); 174 if (error) 175 goto error_1; 176 177 if (!vn_isdisk(devvp, &error)) 178 goto error_2; 179 180 /* 181 ******************** 182 * NEW MOUNT 183 ******************** 184 */ 185 186 /* Save "mounted from" info for mount point (NULL pad)*/ 187 copyinstr( args.fspec, /* device name*/ 188 mp->mnt_stat.f_mntfromname, /* save area*/ 189 MNAMELEN - 1, /* max size*/ 190 &size); /* real size*/ 191 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 192 193 error = hpfs_mountfs(devvp, mp, &args); 194 if (error) 195 goto error_2; 196 197 /* 198 * Initialize FS stat information in mount struct; uses 199 * mp->mnt_stat.f_mntfromname. 200 * 201 * This code is common to root and non-root mounts 202 */ 203 VFS_STATFS(mp, &mp->mnt_stat, cred); 204 return (error); 205 206 error_2: /* error with devvp held*/ 207 208 /* release devvp before failing*/ 209 vrele(devvp); 210 211 error_1: /* no state to back out*/ 212 213 success: 214 return (error); 215 } 216 217 /* 218 * Common code for mount and mountroot 219 */ 220 int 221 hpfs_mountfs(struct vnode *devvp, struct mount *mp, struct hpfs_args *argsp) 222 { 223 int error, ncount, ronly; 224 struct sublock *sup; 225 struct spblock *spp; 226 struct hpfsmount *hpmp; 227 struct buf *bp = NULL; 228 struct vnode *vp; 229 cdev_t dev; 230 231 dprintf(("hpfs_mountfs():\n")); 232 /* 233 * Disallow multiple mounts of the same device. 234 * Disallow mounting of a device that is currently in use 235 * (except for root, which might share swap device for miniroot). 236 * Flush out any old buffers remaining from a previous use. 237 */ 238 error = vfs_mountedon(devvp); 239 if (error) 240 return (error); 241 ncount = count_udev(devvp->v_umajor, devvp->v_uminor); 242 if (devvp->v_object) 243 ncount -= 1; 244 if (ncount > 0) 245 return (EBUSY); 246 247 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY); 248 error = vinvalbuf(devvp, V_SAVE, 0, 0); 249 VOP__UNLOCK(devvp, 0); 250 if (error) 251 return (error); 252 253 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 254 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY); 255 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL); 256 VOP__UNLOCK(devvp, 0); 257 if (error) 258 return (error); 259 dev = devvp->v_rdev; 260 261 /* 262 * Do actual mount 263 */ 264 hpmp = kmalloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK | M_ZERO); 265 266 /* Read in SuperBlock */ 267 error = bread(devvp, dbtodoff(SUBLOCK), SUSIZE, &bp); 268 if (error) 269 goto failed; 270 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock)); 271 brelse(bp); bp = NULL; 272 273 /* Read in SpareBlock */ 274 error = bread(devvp, dbtodoff(SPBLOCK), SPSIZE, &bp); 275 if (error) 276 goto failed; 277 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock)); 278 brelse(bp); bp = NULL; 279 280 sup = &hpmp->hpm_su; 281 spp = &hpmp->hpm_sp; 282 283 /* Check magic */ 284 if (sup->su_magic != SU_MAGIC) { 285 kprintf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n"); 286 error = EINVAL; 287 goto failed; 288 } 289 if (spp->sp_magic != SP_MAGIC) { 290 kprintf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n"); 291 error = EINVAL; 292 goto failed; 293 } 294 295 mp->mnt_data = (qaddr_t)hpmp; 296 hpmp->hpm_devvp = devvp; 297 hpmp->hpm_dev = dev; 298 hpmp->hpm_mp = mp; 299 hpmp->hpm_uid = argsp->uid; 300 hpmp->hpm_gid = argsp->gid; 301 hpmp->hpm_mode = argsp->mode; 302 303 error = hpfs_bminit(hpmp); 304 if (error) 305 goto failed; 306 307 error = hpfs_cpinit(hpmp, argsp); 308 if (error) { 309 hpfs_bmdeinit(hpmp); 310 goto failed; 311 } 312 vfs_add_vnodeops(mp, &hpfs_vnode_vops, &mp->mnt_vn_norm_ops); 313 314 error = hpfs_root(mp, &vp); 315 if (error) { 316 hpfs_cpdeinit(hpmp); 317 hpfs_bmdeinit(hpmp); 318 goto failed; 319 } 320 321 vput(vp); 322 323 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev); 324 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 325 mp->mnt_maxsymlinklen = 0; 326 mp->mnt_flag |= MNT_LOCAL; 327 dev->si_mountpoint = mp; 328 return (0); 329 330 failed: 331 if (bp) 332 brelse (bp); 333 mp->mnt_data = (qaddr_t)NULL; 334 dev->si_mountpoint = NULL; 335 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE); 336 return (error); 337 } 338 339 340 static int 341 hpfs_unmount(struct mount *mp, int mntflags) 342 { 343 int error, flags, ronly; 344 struct hpfsmount *hpmp = VFSTOHPFS(mp); 345 346 dprintf(("hpfs_unmount():\n")); 347 348 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 349 350 flags = 0; 351 if(mntflags & MNT_FORCE) 352 flags |= FORCECLOSE; 353 354 dprintf(("hpfs_unmount: vflushing...\n")); 355 356 error = vflush(mp, 0, flags); 357 if (error) { 358 kprintf("hpfs_unmount: vflush failed: %d\n",error); 359 return (error); 360 } 361 362 hpmp->hpm_devvp->v_rdev->si_mountpoint = NULL; 363 364 vinvalbuf(hpmp->hpm_devvp, V_SAVE, 0, 0); 365 error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE); 366 367 vrele(hpmp->hpm_devvp); 368 369 dprintf(("hpfs_umount: freeing memory...\n")); 370 hpfs_cpdeinit(hpmp); 371 hpfs_bmdeinit(hpmp); 372 mp->mnt_data = (qaddr_t)0; 373 mp->mnt_flag &= ~MNT_LOCAL; 374 FREE(hpmp, M_HPFSMNT); 375 376 return (0); 377 } 378 379 static int 380 hpfs_root(struct mount *mp, struct vnode **vpp) 381 { 382 int error = 0; 383 struct hpfsmount *hpmp = VFSTOHPFS(mp); 384 385 dprintf(("hpfs_root():\n")); 386 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, vpp); 387 if(error) { 388 kprintf("hpfs_root: VFS_VGET failed: %d\n",error); 389 return (error); 390 } 391 392 return (error); 393 } 394 395 static int 396 hpfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 397 { 398 struct hpfsmount *hpmp = VFSTOHPFS(mp); 399 400 dprintf(("hpfs_statfs(): HPFS%d.%d\n", 401 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver)); 402 403 sbp->f_type = mp->mnt_vfc->vfc_typenum; 404 sbp->f_bsize = DEV_BSIZE; 405 sbp->f_iosize = DEV_BSIZE; 406 sbp->f_blocks = hpmp->hpm_su.su_btotal; 407 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail; 408 sbp->f_ffree = 0; 409 sbp->f_files = 0; 410 if (sbp != &mp->mnt_stat) { 411 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 412 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 413 } 414 sbp->f_flags = mp->mnt_flag; 415 416 return (0); 417 } 418 419 420 /*ARGSUSED*/ 421 static int 422 hpfs_fhtovp(struct mount *mp, struct vnode *rootvp, 423 struct fid *fhp, struct vnode **vpp) 424 { 425 struct vnode *nvp; 426 struct hpfid *hpfhp = (struct hpfid *)fhp; 427 int error; 428 429 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, &nvp)) != 0) { 430 *vpp = NULLVP; 431 return (error); 432 } 433 /* XXX as unlink/rmdir/mkdir/creat are not currently possible 434 * with HPFS, we don't need to check anything else for now */ 435 *vpp = nvp; 436 437 return (0); 438 } 439 440 static int 441 hpfs_vptofh(struct vnode *vp, struct fid *fhp) 442 { 443 struct hpfsnode *hpp; 444 struct hpfid *hpfhp; 445 446 hpp = VTOHP(vp); 447 hpfhp = (struct hpfid *)fhp; 448 hpfhp->hpfid_len = sizeof(struct hpfid); 449 hpfhp->hpfid_ino = hpp->h_no; 450 /* hpfhp->hpfid_gen = hpp->h_gen; */ 451 return (0); 452 } 453 454 static int 455 hpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 456 { 457 struct hpfsmount *hpmp = VFSTOHPFS(mp); 458 struct vnode *vp; 459 struct hpfsnode *hp; 460 struct buf *bp; 461 int error; 462 463 dprintf(("hpfs_vget(0x%x): ",ino)); 464 465 *vpp = NULL; 466 hp = NULL; 467 vp = NULL; 468 469 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino)) != NULL) { 470 dprintf(("hashed\n")); 471 return (0); 472 } 473 474 /* 475 * We have to lock node creation for a while, 476 * but then we have to call getnewvnode(), 477 * this may cause hpfs_reclaim() to be called, 478 * this may need to VOP_VGET() parent dir for 479 * update reasons, and if parent is not in 480 * hash, we have to lock node creation... 481 * To solve this, we MALLOC, getnewvnode and init while 482 * not locked (probability of node appearence 483 * at that time is little, and anyway - we'll 484 * check for it). 485 */ 486 MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode), 487 M_HPFSNO, M_WAITOK); 488 489 error = getnewvnode(VT_HPFS, hpmp->hpm_mp, &vp, VLKTIMEOUT, 0); 490 if (error) { 491 kprintf("hpfs_vget: can't get new vnode\n"); 492 FREE(hp, M_HPFSNO); 493 return (error); 494 } 495 496 dprintf(("prenew ")); 497 498 vp->v_data = hp; 499 500 if (ino == (ino_t)hpmp->hpm_su.su_rootfno) 501 vp->v_flag |= VROOT; 502 503 lwkt_token_init(&hp->h_interlock); 504 505 hp->h_flag = H_INVAL; 506 hp->h_vp = vp; 507 hp->h_hpmp = hpmp; 508 hp->h_no = ino; 509 hp->h_dev = hpmp->hpm_dev; 510 hp->h_uid = hpmp->hpm_uid; 511 hp->h_gid = hpmp->hpm_uid; 512 hp->h_mode = hpmp->hpm_mode; 513 hp->h_devvp = hpmp->hpm_devvp; 514 vref(hp->h_devvp); 515 516 do { 517 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino)) != NULL) { 518 dprintf(("hashed2\n")); 519 vx_put(vp); 520 return (0); 521 } 522 } while (LOCKMGR(&hpfs_hphash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL)); 523 524 hpfs_hphashins(hp); 525 526 LOCKMGR(&hpfs_hphash_lock, LK_RELEASE); 527 528 error = bread(hpmp->hpm_devvp, dbtodoff(ino), FNODESIZE, &bp); 529 if (error) { 530 kprintf("hpfs_vget: can't read ino %"PRId64"\n",ino); 531 vx_put(vp); 532 return (error); 533 } 534 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode)); 535 brelse(bp); 536 537 if (hp->h_fn.fn_magic != FN_MAGIC) { 538 kprintf("hpfs_vget: MAGIC DOESN'T MATCH\n"); 539 vx_put(vp); 540 return (EINVAL); 541 } 542 543 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG; 544 hp->h_flag &= ~H_INVAL; 545 546 /* Return the locked and refd vnode */ 547 *vpp = vp; 548 549 return (0); 550 } 551 552 static struct vfsops hpfs_vfsops = { 553 .vfs_mount = hpfs_mount, 554 .vfs_unmount = hpfs_unmount, 555 .vfs_root = hpfs_root, 556 .vfs_statfs = hpfs_statfs, 557 .vfs_sync = vfs_stdsync, 558 .vfs_vget = hpfs_vget, 559 .vfs_fhtovp = hpfs_fhtovp, 560 .vfs_checkexp = hpfs_checkexp, 561 .vfs_vptofh = hpfs_vptofh, 562 .vfs_init = hpfs_init, 563 .vfs_uninit = hpfs_hphash_uninit 564 }; 565 566 VFS_SET(hpfs_vfsops, hpfs, 0); 567 568