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