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