1 /* $NetBSD: ntfs_vfsops.c,v 1.15 2003/12/04 19:38:23 atatat Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999 Semen Ustimenko 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * Id: ntfs_vfsops.c,v 1.7 1999/05/31 11:28:30 phk Exp 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: ntfs_vfsops.c,v 1.15 2003/12/04 19:38:23 atatat Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/namei.h> 37 #include <sys/proc.h> 38 #include <sys/kernel.h> 39 #include <sys/vnode.h> 40 #include <sys/mount.h> 41 #include <sys/buf.h> 42 #include <sys/fcntl.h> 43 #include <sys/malloc.h> 44 #include <sys/sysctl.h> 45 #include <sys/device.h> 46 #include <sys/conf.h> 47 48 #if defined(__NetBSD__) 49 #include <uvm/uvm_extern.h> 50 #else 51 #include <vm/vm.h> 52 #endif 53 54 #include <miscfs/specfs/specdev.h> 55 56 /*#define NTFS_DEBUG 1*/ 57 #include <fs/ntfs/ntfs.h> 58 #include <fs/ntfs/ntfs_inode.h> 59 #include <fs/ntfs/ntfs_subr.h> 60 #include <fs/ntfs/ntfs_vfsops.h> 61 #include <fs/ntfs/ntfs_ihash.h> 62 #include <fs/ntfs/ntfsmount.h> 63 64 MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure"); 65 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information"); 66 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information"); 67 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer"); 68 69 #if defined(__FreeBSD__) 70 static int ntfs_mount __P((struct mount *, char *, caddr_t, 71 struct nameidata *, struct proc *)); 72 #else 73 static int ntfs_mount __P((struct mount *, const char *, void *, 74 struct nameidata *, struct proc *)); 75 #endif 76 static int ntfs_quotactl __P((struct mount *, int, uid_t, caddr_t, 77 struct proc *)); 78 static int ntfs_root __P((struct mount *, struct vnode **)); 79 static int ntfs_start __P((struct mount *, int, struct proc *)); 80 static int ntfs_statfs __P((struct mount *, struct statfs *, 81 struct proc *)); 82 static int ntfs_sync __P((struct mount *, int, struct ucred *, 83 struct proc *)); 84 static int ntfs_unmount __P((struct mount *, int, struct proc *)); 85 static int ntfs_vget __P((struct mount *mp, ino_t ino, 86 struct vnode **vpp)); 87 static int ntfs_mountfs __P((struct vnode *, struct mount *, 88 struct ntfs_args *, struct proc *)); 89 static int ntfs_vptofh __P((struct vnode *, struct fid *)); 90 91 #if defined(__FreeBSD__) 92 static int ntfs_init __P((struct vfsconf *)); 93 static int ntfs_fhtovp __P((struct mount *, struct fid *, 94 struct sockaddr *, struct vnode **, 95 int *, struct ucred **)); 96 #elif defined(__NetBSD__) 97 static void ntfs_init __P((void)); 98 static void ntfs_reinit __P((void)); 99 static void ntfs_done __P((void)); 100 static int ntfs_fhtovp __P((struct mount *, struct fid *, 101 struct vnode **)); 102 static int ntfs_checkexp __P((struct mount *, struct mbuf *, 103 int *, struct ucred **)); 104 static int ntfs_mountroot __P((void)); 105 #else 106 static int ntfs_init __P((void)); 107 static int ntfs_fhtovp __P((struct mount *, struct fid *, 108 struct mbuf *, struct vnode **, 109 int *, struct ucred **)); 110 #endif 111 112 struct genfs_ops ntfs_genfsops = { 113 NULL, 114 NULL, 115 genfs_compat_gop_write, 116 }; 117 118 #ifdef __NetBSD__ 119 /* 120 * Verify a remote client has export rights and return these rights via. 121 * exflagsp and credanonp. 122 */ 123 static int 124 ntfs_checkexp(mp, nam, exflagsp, credanonp) 125 struct mount *mp; 126 struct mbuf *nam; 127 int *exflagsp; 128 struct ucred **credanonp; 129 { 130 struct netcred *np; 131 struct ntfsmount *ntm = VFSTONTFS(mp); 132 133 /* 134 * Get the export permission structure for this <mp, client> tuple. 135 */ 136 np = vfs_export_lookup(mp, &ntm->ntm_export, nam); 137 if (np == NULL) 138 return (EACCES); 139 140 *exflagsp = np->netc_exflags; 141 *credanonp = &np->netc_anon; 142 return (0); 143 } 144 145 SYSCTL_SETUP(sysctl_vfs_ntfs_setup, "sysctl vfs.ntfs subtree setup") 146 { 147 148 sysctl_createv(SYSCTL_PERMANENT, 149 CTLTYPE_NODE, "vfs", NULL, 150 NULL, 0, NULL, 0, 151 CTL_VFS, CTL_EOL); 152 sysctl_createv(SYSCTL_PERMANENT, 153 CTLTYPE_NODE, "ntfs", NULL, 154 NULL, 0, NULL, 0, 155 CTL_VFS, 20, CTL_EOL); 156 /* 157 * XXX the "20" above could be dynamic, thereby eliminating 158 * one more instance of the "number to vfs" mapping problem, 159 * but "20" is the order as taken from sys/mount.h 160 */ 161 } 162 163 static int 164 ntfs_mountroot() 165 { 166 struct mount *mp; 167 struct proc *p = curproc; /* XXX */ 168 int error; 169 struct ntfs_args args; 170 171 if (root_device->dv_class != DV_DISK) 172 return (ENODEV); 173 174 /* 175 * Get vnodes for rootdev. 176 */ 177 if (bdevvp(rootdev, &rootvp)) 178 panic("ntfs_mountroot: can't setup rootvp"); 179 180 if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) { 181 vrele(rootvp); 182 return (error); 183 } 184 185 args.flag = 0; 186 args.uid = 0; 187 args.gid = 0; 188 args.mode = 0777; 189 190 if ((error = ntfs_mountfs(rootvp, mp, &args, p)) != 0) { 191 mp->mnt_op->vfs_refcount--; 192 vfs_unbusy(mp); 193 free(mp, M_MOUNT); 194 vrele(rootvp); 195 return (error); 196 } 197 198 simple_lock(&mountlist_slock); 199 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 200 simple_unlock(&mountlist_slock); 201 (void)ntfs_statfs(mp, &mp->mnt_stat, p); 202 vfs_unbusy(mp); 203 return (0); 204 } 205 206 static void 207 ntfs_init() 208 { 209 #ifdef _LKM 210 malloc_type_attach(M_NTFSMNT); 211 malloc_type_attach(M_NTFSNTNODE); 212 malloc_type_attach(M_NTFSFNODE); 213 malloc_type_attach(M_NTFSDIR); 214 malloc_type_attach(M_NTFSNTHASH); 215 malloc_type_attach(M_NTFSNTVATTR); 216 malloc_type_attach(M_NTFSRDATA); 217 malloc_type_attach(M_NTFSDECOMP); 218 malloc_type_attach(M_NTFSRUN); 219 #endif 220 ntfs_nthashinit(); 221 ntfs_toupper_init(); 222 } 223 224 static void 225 ntfs_reinit() 226 { 227 ntfs_nthashreinit(); 228 } 229 230 static void 231 ntfs_done() 232 { 233 ntfs_nthashdone(); 234 #ifdef _LKM 235 malloc_type_detach(M_NTFSMNT); 236 malloc_type_detach(M_NTFSNTNODE); 237 malloc_type_detach(M_NTFSFNODE); 238 malloc_type_detach(M_NTFSDIR); 239 malloc_type_detach(M_NTFSNTHASH); 240 malloc_type_detach(M_NTFSNTVATTR); 241 malloc_type_detach(M_NTFSRDATA); 242 malloc_type_detach(M_NTFSDECOMP); 243 malloc_type_detach(M_NTFSRUN); 244 #endif 245 } 246 247 #elif defined(__FreeBSD__) 248 249 static int 250 ntfs_init ( 251 struct vfsconf *vcp ) 252 { 253 ntfs_nthashinit(); 254 ntfs_toupper_init(); 255 return 0; 256 } 257 258 #endif /* NetBSD */ 259 260 static int 261 ntfs_mount ( 262 struct mount *mp, 263 #if defined(__FreeBSD__) 264 char *path, 265 caddr_t data, 266 #else 267 const char *path, 268 void *data, 269 #endif 270 struct nameidata *ndp, 271 struct proc *p ) 272 { 273 int err = 0; 274 struct vnode *devvp; 275 struct ntfs_args args; 276 277 #ifdef __FreeBSD__ 278 /* 279 * Use NULL path to flag a root mount 280 */ 281 if( path == NULL) { 282 /* 283 *** 284 * Mounting root file system 285 *** 286 */ 287 288 /* Get vnode for root device*/ 289 if( bdevvp( rootdev, &rootvp)) 290 panic("ffs_mountroot: can't setup bdevvp for root"); 291 292 /* 293 * FS specific handling 294 */ 295 mp->mnt_flag |= MNT_RDONLY; /* XXX globally applicable?*/ 296 297 /* 298 * Attempt mount 299 */ 300 if( ( err = ntfs_mountfs(rootvp, mp, &args, p)) != 0) { 301 /* fs specific cleanup (if any)*/ 302 goto error_1; 303 } 304 305 goto dostatfs; /* success*/ 306 307 } 308 #endif /* FreeBSD */ 309 310 if (mp->mnt_flag & MNT_GETARGS) { 311 struct ntfsmount *ntmp = VFSTONTFS(mp); 312 if (ntmp == NULL) 313 return EIO; 314 args.fspec = NULL; 315 args.uid = ntmp->ntm_uid; 316 args.gid = ntmp->ntm_gid; 317 args.mode = ntmp->ntm_mode; 318 args.flag = ntmp->ntm_flag; 319 vfs_showexport(mp, &args.export, &ntmp->ntm_export); 320 return copyout(&args, data, sizeof(args)); 321 } 322 /* 323 *** 324 * Mounting non-root file system or updating a file system 325 *** 326 */ 327 328 /* copy in user arguments*/ 329 err = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args)); 330 if (err) 331 goto error_1; /* can't get arguments*/ 332 333 /* 334 * If updating, check whether changing from read-only to 335 * read/write; if there is no device name, that's all we do. 336 */ 337 if (mp->mnt_flag & MNT_UPDATE) { 338 /* if not updating name...*/ 339 if (args.fspec == 0) { 340 /* 341 * Process export requests. Jumping to "success" 342 * will return the vfs_export() error code. 343 */ 344 struct ntfsmount *ntm = VFSTONTFS(mp); 345 err = vfs_export(mp, &ntm->ntm_export, &args.export); 346 goto success; 347 } 348 349 printf("ntfs_mount(): MNT_UPDATE not supported\n"); 350 err = EINVAL; 351 goto error_1; 352 } 353 354 /* 355 * Not an update, or updating the name: look up the name 356 * and verify that it refers to a sensible block device. 357 */ 358 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 359 err = namei(ndp); 360 if (err) { 361 /* can't get devvp!*/ 362 goto error_1; 363 } 364 365 devvp = ndp->ni_vp; 366 367 if (devvp->v_type != VBLK) { 368 err = ENOTBLK; 369 goto error_2; 370 } 371 #ifdef __FreeBSD__ 372 if (bdevsw(devvp->v_rdev) == NULL) { 373 #else 374 if (bdevsw_lookup(devvp->v_rdev) == NULL) { 375 #endif 376 err = ENXIO; 377 goto error_2; 378 } 379 if (mp->mnt_flag & MNT_UPDATE) { 380 #if 0 381 /* 382 ******************** 383 * UPDATE 384 ******************** 385 */ 386 387 if (devvp != ntmp->um_devvp) 388 err = EINVAL; /* needs translation */ 389 else 390 vrele(devvp); 391 /* 392 * Update device name only on success 393 */ 394 if( !err) { 395 err = set_statfs_info(NULL, UIO_USERSPACE, args.fspec, 396 UIO_USERSPACE, mp, p); 397 } 398 #endif 399 } else { 400 /* 401 ******************** 402 * NEW MOUNT 403 ******************** 404 */ 405 406 /* 407 * Since this is a new mount, we want the names for 408 * the device and the mount point copied in. If an 409 * error occurs, the mountpoint is discarded by the 410 * upper level code. 411 */ 412 /* Save "last mounted on" info for mount point (NULL pad)*/ 413 err = set_statfs_info(path, UIO_USERSPACE, args.fspec, 414 UIO_USERSPACE, mp, p); 415 if ( !err) { 416 err = ntfs_mountfs(devvp, mp, &args, p); 417 } 418 } 419 if (err) { 420 goto error_2; 421 } 422 423 #ifdef __FreeBSD__ 424 dostatfs: 425 #endif 426 /* 427 * Initialize FS stat information in mount struct; uses both 428 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname 429 * 430 * This code is common to root and non-root mounts 431 */ 432 (void)VFS_STATFS(mp, &mp->mnt_stat, p); 433 434 goto success; 435 436 437 error_2: /* error with devvp held*/ 438 439 /* release devvp before failing*/ 440 vrele(devvp); 441 442 error_1: /* no state to back out*/ 443 444 success: 445 return(err); 446 } 447 448 /* 449 * Common code for mount and mountroot 450 */ 451 int 452 ntfs_mountfs(devvp, mp, argsp, p) 453 struct vnode *devvp; 454 struct mount *mp; 455 struct ntfs_args *argsp; 456 struct proc *p; 457 { 458 struct buf *bp; 459 struct ntfsmount *ntmp; 460 dev_t dev = devvp->v_rdev; 461 int error, ronly, ncount, i; 462 struct vnode *vp; 463 464 /* 465 * Disallow multiple mounts of the same device. 466 * Disallow mounting of a device that is currently in use 467 * (except for root, which might share swap device for miniroot). 468 * Flush out any old buffers remaining from a previous use. 469 */ 470 error = vfs_mountedon(devvp); 471 if (error) 472 return (error); 473 ncount = vcount(devvp); 474 if (ncount > 1 && devvp != rootvp) 475 return (EBUSY); 476 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0); 477 if (error) 478 return (error); 479 480 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 481 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 482 if (error) 483 return (error); 484 485 bp = NULL; 486 487 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp); 488 if (error) 489 goto out; 490 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK ); 491 bzero( ntmp, sizeof *ntmp ); 492 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) ); 493 brelse( bp ); 494 bp = NULL; 495 496 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) { 497 error = EINVAL; 498 dprintf(("ntfs_mountfs: invalid boot block\n")); 499 goto out; 500 } 501 502 { 503 int8_t cpr = ntmp->ntm_mftrecsz; 504 if( cpr > 0 ) 505 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr; 506 else 507 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps; 508 } 509 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n", 510 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media, 511 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec)); 512 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n", 513 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn)); 514 515 ntmp->ntm_mountp = mp; 516 ntmp->ntm_dev = dev; 517 ntmp->ntm_devvp = devvp; 518 ntmp->ntm_uid = argsp->uid; 519 ntmp->ntm_gid = argsp->gid; 520 ntmp->ntm_mode = argsp->mode; 521 ntmp->ntm_flag = argsp->flag; 522 mp->mnt_data = ntmp; 523 524 /* set file name encode/decode hooks XXX utf-8 only for now */ 525 ntmp->ntm_wget = ntfs_utf8_wget; 526 ntmp->ntm_wput = ntfs_utf8_wput; 527 ntmp->ntm_wcmp = ntfs_utf8_wcmp; 528 529 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n", 530 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.", 531 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"", 532 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode)); 533 534 /* 535 * We read in some system nodes to do not allow 536 * reclaim them and to have everytime access to them. 537 */ 538 { 539 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO }; 540 for (i=0; i<3; i++) { 541 error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]])); 542 if(error) 543 goto out1; 544 ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM; 545 VREF(ntmp->ntm_sysvn[pi[i]]); 546 vput(ntmp->ntm_sysvn[pi[i]]); 547 } 548 } 549 550 /* read the Unicode lowercase --> uppercase translation table, 551 * if necessary */ 552 if ((error = ntfs_toupper_use(mp, ntmp))) 553 goto out1; 554 555 /* 556 * Scan $BitMap and count free clusters 557 */ 558 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree); 559 if(error) 560 goto out1; 561 562 /* 563 * Read and translate to internal format attribute 564 * definition file. 565 */ 566 { 567 int num,j; 568 struct attrdef ad; 569 570 /* Open $AttrDef */ 571 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp ); 572 if(error) 573 goto out1; 574 575 /* Count valid entries */ 576 for(num=0;;num++) { 577 error = ntfs_readattr(ntmp, VTONT(vp), 578 NTFS_A_DATA, NULL, 579 num * sizeof(ad), sizeof(ad), 580 &ad, NULL); 581 if (error) 582 goto out1; 583 if (ad.ad_name[0] == 0) 584 break; 585 } 586 587 /* Alloc memory for attribute definitions */ 588 ntmp->ntm_ad = (struct ntvattrdef *) malloc( 589 num * sizeof(struct ntvattrdef), 590 M_NTFSMNT, M_WAITOK); 591 592 ntmp->ntm_adnum = num; 593 594 /* Read them and translate */ 595 for(i=0;i<num;i++){ 596 error = ntfs_readattr(ntmp, VTONT(vp), 597 NTFS_A_DATA, NULL, 598 i * sizeof(ad), sizeof(ad), 599 &ad, NULL); 600 if (error) 601 goto out1; 602 j = 0; 603 do { 604 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j]; 605 } while(ad.ad_name[j++]); 606 ntmp->ntm_ad[i].ad_namelen = j - 1; 607 ntmp->ntm_ad[i].ad_type = ad.ad_type; 608 } 609 610 vput(vp); 611 } 612 613 #if defined(__FreeBSD__) 614 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 615 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 616 #else 617 mp->mnt_stat.f_fsid.val[0] = dev; 618 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS); 619 #endif 620 mp->mnt_maxsymlinklen = 0; 621 mp->mnt_flag |= MNT_LOCAL; 622 devvp->v_specmountpoint = mp; 623 return (0); 624 625 out1: 626 for(i=0;i<NTFS_SYSNODESNUM;i++) 627 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]); 628 629 if (vflush(mp,NULLVP,0)) 630 dprintf(("ntfs_mountfs: vflush failed\n")); 631 632 out: 633 devvp->v_specmountpoint = NULL; 634 if (bp) 635 brelse(bp); 636 637 /* lock the device vnode before calling VOP_CLOSE() */ 638 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 639 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 640 VOP_UNLOCK(devvp, 0); 641 642 return (error); 643 } 644 645 static int 646 ntfs_start ( 647 struct mount *mp, 648 int flags, 649 struct proc *p ) 650 { 651 return (0); 652 } 653 654 static int 655 ntfs_unmount( 656 struct mount *mp, 657 int mntflags, 658 struct proc *p) 659 { 660 struct ntfsmount *ntmp; 661 int error, ronly = 0, flags, i; 662 663 dprintf(("ntfs_unmount: unmounting...\n")); 664 ntmp = VFSTONTFS(mp); 665 666 flags = 0; 667 if(mntflags & MNT_FORCE) 668 flags |= FORCECLOSE; 669 670 dprintf(("ntfs_unmount: vflushing...\n")); 671 error = vflush(mp,NULLVP,flags | SKIPSYSTEM); 672 if (error) { 673 dprintf(("ntfs_unmount: vflush failed: %d\n",error)); 674 return (error); 675 } 676 677 /* Check if only system vnodes are rest */ 678 for(i=0;i<NTFS_SYSNODESNUM;i++) 679 if((ntmp->ntm_sysvn[i]) && 680 (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY); 681 682 /* Dereference all system vnodes */ 683 for(i=0;i<NTFS_SYSNODESNUM;i++) 684 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]); 685 686 /* vflush system vnodes */ 687 error = vflush(mp,NULLVP,flags); 688 if (error) { 689 /* XXX should this be panic() ? */ 690 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error); 691 } 692 693 /* Check if the type of device node isn't VBAD before 694 * touching v_specinfo. If the device vnode is revoked, the 695 * field is NULL and touching it causes null pointer derefercence. 696 */ 697 if (ntmp->ntm_devvp->v_type != VBAD) 698 ntmp->ntm_devvp->v_specmountpoint = NULL; 699 700 vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0); 701 702 /* lock the device vnode before calling VOP_CLOSE() */ 703 VOP_LOCK(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY); 704 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, 705 NOCRED, p); 706 VOP_UNLOCK(ntmp->ntm_devvp, 0); 707 708 vrele(ntmp->ntm_devvp); 709 710 /* free the toupper table, if this has been last mounted ntfs volume */ 711 ntfs_toupper_unuse(); 712 713 dprintf(("ntfs_umount: freeing memory...\n")); 714 mp->mnt_data = NULL; 715 mp->mnt_flag &= ~MNT_LOCAL; 716 free(ntmp->ntm_ad, M_NTFSMNT); 717 FREE(ntmp, M_NTFSMNT); 718 return (error); 719 } 720 721 static int 722 ntfs_root( 723 struct mount *mp, 724 struct vnode **vpp ) 725 { 726 struct vnode *nvp; 727 int error = 0; 728 729 dprintf(("ntfs_root(): sysvn: %p\n", 730 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO])); 731 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp); 732 if(error) { 733 printf("ntfs_root: VFS_VGET failed: %d\n",error); 734 return (error); 735 } 736 737 *vpp = nvp; 738 return (0); 739 } 740 741 /* 742 * Do operations associated with quotas, not supported 743 */ 744 /* ARGSUSED */ 745 static int 746 ntfs_quotactl ( 747 struct mount *mp, 748 int cmds, 749 uid_t uid, 750 caddr_t arg, 751 struct proc *p) 752 { 753 754 return EOPNOTSUPP; 755 } 756 757 int 758 ntfs_calccfree( 759 struct ntfsmount *ntmp, 760 cn_t *cfreep) 761 { 762 struct vnode *vp; 763 u_int8_t *tmp; 764 int j, error; 765 long cfree = 0; 766 size_t bmsize, i; 767 768 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO]; 769 770 bmsize = VTOF(vp)->f_size; 771 772 tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK); 773 774 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL, 775 0, bmsize, tmp, NULL); 776 if (error) 777 goto out; 778 779 for(i=0;i<bmsize;i++) 780 for(j=0;j<8;j++) 781 if(~tmp[i] & (1 << j)) cfree++; 782 *cfreep = cfree; 783 784 out: 785 free(tmp, M_TEMP); 786 return(error); 787 } 788 789 static int 790 ntfs_statfs( 791 struct mount *mp, 792 struct statfs *sbp, 793 struct proc *p) 794 { 795 struct ntfsmount *ntmp = VFSTONTFS(mp); 796 u_int64_t mftallocated; 797 798 dprintf(("ntfs_statfs():\n")); 799 800 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated; 801 802 #if defined(__FreeBSD__) 803 sbp->f_type = mp->mnt_vfc->vfc_typenum; 804 #elif defined(__NetBSD__) 805 sbp->f_type = 0; 806 #else 807 sbp->f_type = MOUNT_NTFS; 808 #endif 809 sbp->f_bsize = ntmp->ntm_bps; 810 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc; 811 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv; 812 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree); 813 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec; 814 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) + 815 sbp->f_ffree; 816 sbp->f_flags = mp->mnt_flag; 817 copy_statfs_info(sbp, mp); 818 return (0); 819 } 820 821 static int 822 ntfs_sync ( 823 struct mount *mp, 824 int waitfor, 825 struct ucred *cred, 826 struct proc *p) 827 { 828 /*dprintf(("ntfs_sync():\n"));*/ 829 return (0); 830 } 831 832 /*ARGSUSED*/ 833 static int 834 ntfs_fhtovp( 835 #if defined(__FreeBSD__) 836 struct mount *mp, 837 struct fid *fhp, 838 struct sockaddr *nam, 839 struct vnode **vpp, 840 int *exflagsp, 841 struct ucred **credanonp) 842 #elif defined(__NetBSD__) 843 struct mount *mp, 844 struct fid *fhp, 845 struct vnode **vpp) 846 #else 847 struct mount *mp, 848 struct fid *fhp, 849 struct mbuf *nam, 850 struct vnode **vpp, 851 int *exflagsp, 852 struct ucred **credanonp) 853 #endif 854 { 855 struct ntfid *ntfhp = (struct ntfid *)fhp; 856 int error; 857 858 ddprintf(("ntfs_fhtovp(): %s: %d\n", mp->mnt_stat.f_mntonname, 859 ntfhp->ntfid_ino)); 860 861 error = ntfs_vgetex(mp, ntfhp->ntfid_ino, ntfhp->ntfid_attr, NULL, 862 LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */ 863 if (error != 0) { 864 *vpp = NULLVP; 865 return (error); 866 } 867 868 /* XXX as unlink/rmdir/mkdir/creat are not currently possible 869 * with NTFS, we don't need to check anything else for now */ 870 return (0); 871 } 872 873 static int 874 ntfs_vptofh( 875 struct vnode *vp, 876 struct fid *fhp) 877 { 878 struct ntnode *ntp; 879 struct ntfid *ntfhp; 880 struct fnode *fn; 881 882 ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname, 883 vp)); 884 885 fn = VTOF(vp); 886 ntp = VTONT(vp); 887 ntfhp = (struct ntfid *)fhp; 888 ntfhp->ntfid_len = sizeof(struct ntfid); 889 ntfhp->ntfid_ino = ntp->i_number; 890 ntfhp->ntfid_attr = fn->f_attrtype; 891 #ifdef notyet 892 ntfhp->ntfid_gen = ntp->i_gen; 893 #endif 894 return (0); 895 } 896 897 int 898 ntfs_vgetex( 899 struct mount *mp, 900 ino_t ino, 901 u_int32_t attrtype, 902 char *attrname, 903 u_long lkflags, 904 u_long flags, 905 struct proc *p, 906 struct vnode **vpp) 907 { 908 int error; 909 struct ntfsmount *ntmp; 910 struct ntnode *ip; 911 struct fnode *fp; 912 struct vnode *vp; 913 enum vtype f_type = VBAD; 914 915 dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n", 916 ino, attrtype, attrname?attrname:"", (u_long)lkflags, 917 (u_long)flags )); 918 919 ntmp = VFSTONTFS(mp); 920 *vpp = NULL; 921 922 /* Get ntnode */ 923 error = ntfs_ntlookup(ntmp, ino, &ip); 924 if (error) { 925 printf("ntfs_vget: ntfs_ntget failed\n"); 926 return (error); 927 } 928 929 /* It may be not initialized fully, so force load it */ 930 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) { 931 error = ntfs_loadntnode(ntmp, ip); 932 if(error) { 933 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n", 934 ip->i_number); 935 ntfs_ntput(ip); 936 return (error); 937 } 938 } 939 940 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp); 941 if (error) { 942 printf("ntfs_vget: ntfs_fget failed\n"); 943 ntfs_ntput(ip); 944 return (error); 945 } 946 947 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) { 948 if ((ip->i_frflag & NTFS_FRFLAG_DIR) && 949 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) { 950 f_type = VDIR; 951 } else if (flags & VG_EXT) { 952 f_type = VNON; 953 fp->f_size = fp->f_allocated = 0; 954 } else { 955 f_type = VREG; 956 957 error = ntfs_filesize(ntmp, fp, 958 &fp->f_size, &fp->f_allocated); 959 if (error) { 960 ntfs_ntput(ip); 961 return (error); 962 } 963 } 964 965 fp->f_flag |= FN_VALID; 966 } 967 968 /* 969 * We may be calling vget() now. To avoid potential deadlock, we need 970 * to release ntnode lock, since due to locking order vnode 971 * lock has to be acquired first. 972 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled 973 * prematurely. 974 */ 975 ntfs_ntput(ip); 976 977 if (FTOV(fp)) { 978 /* vget() returns error if the vnode has been recycled */ 979 if (vget(FTOV(fp), lkflags) == 0) { 980 *vpp = FTOV(fp); 981 return (0); 982 } 983 } 984 985 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp); 986 if(error) { 987 ntfs_frele(fp); 988 ntfs_ntput(ip); 989 return (error); 990 } 991 dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino)); 992 993 #ifdef __FreeBSD__ 994 lockinit(&fp->f_lock, PINOD, "fnode", 0, 0); 995 #endif 996 fp->f_vp = vp; 997 vp->v_data = fp; 998 if (f_type != VBAD) 999 vp->v_type = f_type; 1000 1001 if (ino == NTFS_ROOTINO) 1002 vp->v_flag |= VROOT; 1003 1004 if (lkflags & LK_TYPE_MASK) { 1005 error = vn_lock(vp, lkflags); 1006 if (error) { 1007 vput(vp); 1008 return (error); 1009 } 1010 } 1011 1012 genfs_node_init(vp, &ntfs_genfsops); 1013 VREF(ip->i_devvp); 1014 *vpp = vp; 1015 return (0); 1016 } 1017 1018 static int 1019 ntfs_vget( 1020 struct mount *mp, 1021 ino_t ino, 1022 struct vnode **vpp) 1023 { 1024 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, 1025 LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */ 1026 } 1027 1028 #if defined(__FreeBSD__) 1029 static struct vfsops ntfs_vfsops = { 1030 ntfs_mount, 1031 ntfs_start, 1032 ntfs_unmount, 1033 ntfs_root, 1034 ntfs_quotactl, 1035 ntfs_statfs, 1036 ntfs_sync, 1037 ntfs_vget, 1038 ntfs_fhtovp, 1039 ntfs_vptofh, 1040 ntfs_init, 1041 NULL 1042 }; 1043 VFS_SET(ntfs_vfsops, ntfs, 0); 1044 #elif defined(__NetBSD__) 1045 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc; 1046 1047 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = { 1048 &ntfs_vnodeop_opv_desc, 1049 NULL, 1050 }; 1051 1052 struct vfsops ntfs_vfsops = { 1053 MOUNT_NTFS, 1054 ntfs_mount, 1055 ntfs_start, 1056 ntfs_unmount, 1057 ntfs_root, 1058 ntfs_quotactl, 1059 ntfs_statfs, 1060 ntfs_sync, 1061 ntfs_vget, 1062 ntfs_fhtovp, 1063 ntfs_vptofh, 1064 ntfs_init, 1065 ntfs_reinit, 1066 ntfs_done, 1067 NULL, 1068 ntfs_mountroot, 1069 ntfs_checkexp, 1070 ntfs_vnodeopv_descs, 1071 }; 1072 #endif 1073