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