1 /* $NetBSD: ntfs_vfsops.c,v 1.85 2010/07/25 09:54:37 hannken 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.85 2010/07/25 09:54:37 hannken 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 mutex_enter(&mountlist_lock); 122 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list); 123 mutex_exit(&mountlist_lock); 124 (void)ntfs_statvfs(mp, &mp->mnt_stat); 125 vfs_unbusy(mp, false, NULL); 126 return (0); 127 } 128 129 static void 130 ntfs_init(void) 131 { 132 133 malloc_type_attach(M_NTFSMNT); 134 malloc_type_attach(M_NTFSNTNODE); 135 malloc_type_attach(M_NTFSFNODE); 136 malloc_type_attach(M_NTFSDIR); 137 malloc_type_attach(M_NTFSNTVATTR); 138 malloc_type_attach(M_NTFSRDATA); 139 malloc_type_attach(M_NTFSDECOMP); 140 malloc_type_attach(M_NTFSRUN); 141 ntfs_nthashinit(); 142 ntfs_toupper_init(); 143 } 144 145 static void 146 ntfs_reinit(void) 147 { 148 ntfs_nthashreinit(); 149 } 150 151 static void 152 ntfs_done(void) 153 { 154 ntfs_nthashdone(); 155 malloc_type_detach(M_NTFSMNT); 156 malloc_type_detach(M_NTFSNTNODE); 157 malloc_type_detach(M_NTFSFNODE); 158 malloc_type_detach(M_NTFSDIR); 159 malloc_type_detach(M_NTFSNTVATTR); 160 malloc_type_detach(M_NTFSRDATA); 161 malloc_type_detach(M_NTFSDECOMP); 162 malloc_type_detach(M_NTFSRUN); 163 } 164 165 static int 166 ntfs_mount ( 167 struct mount *mp, 168 const char *path, 169 void *data, 170 size_t *data_len) 171 { 172 struct lwp *l = curlwp; 173 int err = 0, flags; 174 struct vnode *devvp; 175 struct ntfs_args *args = data; 176 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 err = VOP_OPEN(devvp, flags, FSCRED); 274 if (err) 275 goto fail; 276 err = ntfs_mountfs(devvp, mp, args, l); 277 if (err) { 278 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 279 (void)VOP_CLOSE(devvp, flags, NOCRED); 280 VOP_UNLOCK(devvp); 281 goto fail; 282 } 283 } 284 285 /* 286 * Initialize FS stat information in mount struct; uses both 287 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname 288 * 289 * This code is common to root and non-root mounts 290 */ 291 (void)VFS_STATVFS(mp, &mp->mnt_stat); 292 return (err); 293 294 fail: 295 vrele(devvp); 296 return (err); 297 } 298 299 /* 300 * Common code for mount and mountroot 301 */ 302 int 303 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp, struct lwp *l) 304 { 305 struct buf *bp; 306 struct ntfsmount *ntmp; 307 dev_t dev = devvp->v_rdev; 308 int error, ronly, i; 309 struct vnode *vp; 310 311 ntmp = NULL; 312 313 /* 314 * Flush out any old buffers remaining from a previous use. 315 */ 316 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 317 error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0); 318 VOP_UNLOCK(devvp); 319 if (error) 320 return (error); 321 322 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 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 devvp->v_specmountpoint = 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 devvp->v_specmountpoint = 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 ntmp->ntm_devvp->v_specmountpoint = NULL; 534 535 vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0); 536 537 /* lock the device vnode before calling VOP_CLOSE() */ 538 vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY); 539 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, 540 NOCRED); 541 KASSERT(error == 0); 542 VOP_UNLOCK(ntmp->ntm_devvp); 543 544 vrele(ntmp->ntm_devvp); 545 546 /* free the toupper table, if this has been last mounted ntfs volume */ 547 ntfs_toupper_unuse(); 548 549 dprintf(("ntfs_umount: freeing memory...\n")); 550 mp->mnt_data = NULL; 551 mp->mnt_flag &= ~MNT_LOCAL; 552 free(ntmp->ntm_ad, M_NTFSMNT); 553 free(ntmp, M_NTFSMNT); 554 return (0); 555 } 556 557 static int 558 ntfs_root( 559 struct mount *mp, 560 struct vnode **vpp) 561 { 562 struct vnode *nvp; 563 int error = 0; 564 565 dprintf(("ntfs_root(): sysvn: %p\n", 566 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO])); 567 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp); 568 if(error) { 569 printf("ntfs_root: VFS_VGET failed: %d\n",error); 570 return (error); 571 } 572 573 *vpp = nvp; 574 return (0); 575 } 576 577 int 578 ntfs_calccfree( 579 struct ntfsmount *ntmp, 580 cn_t *cfreep) 581 { 582 struct vnode *vp; 583 u_int8_t *tmp; 584 int j, error; 585 cn_t cfree = 0; 586 size_t bmsize, i; 587 588 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO]; 589 590 bmsize = VTOF(vp)->f_size; 591 592 tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK); 593 594 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL, 595 0, bmsize, tmp, NULL); 596 if (error) 597 goto out; 598 599 for(i=0;i<bmsize;i++) 600 for(j=0;j<8;j++) 601 if(~tmp[i] & (1 << j)) cfree++; 602 *cfreep = cfree; 603 604 out: 605 free(tmp, M_TEMP); 606 return(error); 607 } 608 609 static int 610 ntfs_statvfs( 611 struct mount *mp, 612 struct statvfs *sbp) 613 { 614 struct ntfsmount *ntmp = VFSTONTFS(mp); 615 u_int64_t mftallocated; 616 617 dprintf(("ntfs_statvfs():\n")); 618 619 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated; 620 621 sbp->f_bsize = ntmp->ntm_bps; 622 sbp->f_frsize = sbp->f_bsize; /* XXX */ 623 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc; 624 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv; 625 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree); 626 sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec; 627 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) + 628 sbp->f_ffree; 629 sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */ 630 sbp->f_flag = mp->mnt_flag; 631 copy_statvfs_info(sbp, mp); 632 return (0); 633 } 634 635 static int 636 ntfs_sync ( 637 struct mount *mp, 638 int waitfor, 639 kauth_cred_t cred) 640 { 641 /*dprintf(("ntfs_sync():\n"));*/ 642 return (0); 643 } 644 645 /*ARGSUSED*/ 646 static int 647 ntfs_fhtovp( 648 struct mount *mp, 649 struct fid *fhp, 650 struct vnode **vpp) 651 { 652 struct ntfid ntfh; 653 int error; 654 655 if (fhp->fid_len != sizeof(struct ntfid)) 656 return EINVAL; 657 memcpy(&ntfh, fhp, sizeof(ntfh)); 658 ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname, 659 (unsigned long long)ntfh.ntfid_ino)); 660 661 error = ntfs_vgetex(mp, ntfh.ntfid_ino, ntfh.ntfid_attr, NULL, 662 LK_EXCLUSIVE, 0, vpp); 663 if (error != 0) { 664 *vpp = NULLVP; 665 return (error); 666 } 667 668 /* XXX as unlink/rmdir/mkdir/creat are not currently possible 669 * with NTFS, we don't need to check anything else for now */ 670 return (0); 671 } 672 673 static int 674 ntfs_vptofh( 675 struct vnode *vp, 676 struct fid *fhp, 677 size_t *fh_size) 678 { 679 struct ntnode *ntp; 680 struct ntfid ntfh; 681 struct fnode *fn; 682 683 if (*fh_size < sizeof(struct ntfid)) { 684 *fh_size = sizeof(struct ntfid); 685 return E2BIG; 686 } 687 *fh_size = sizeof(struct ntfid); 688 689 ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname, 690 vp)); 691 692 fn = VTOF(vp); 693 ntp = VTONT(vp); 694 memset(&ntfh, 0, sizeof(ntfh)); 695 ntfh.ntfid_len = sizeof(struct ntfid); 696 ntfh.ntfid_ino = ntp->i_number; 697 ntfh.ntfid_attr = fn->f_attrtype; 698 #ifdef notyet 699 ntfh.ntfid_gen = ntp->i_gen; 700 #endif 701 memcpy(fhp, &ntfh, sizeof(ntfh)); 702 return (0); 703 } 704 705 int 706 ntfs_vgetex( 707 struct mount *mp, 708 ino_t ino, 709 u_int32_t attrtype, 710 char *attrname, 711 u_long lkflags, 712 u_long flags, 713 struct vnode **vpp) 714 { 715 int error; 716 struct ntfsmount *ntmp; 717 struct ntnode *ip; 718 struct fnode *fp; 719 struct vnode *vp; 720 enum vtype f_type = VBAD; 721 722 dprintf(("ntfs_vgetex: ino: %llu, attr: 0x%x:%s, lkf: 0x%lx, f:" 723 " 0x%lx\n", (unsigned long long)ino, attrtype, 724 attrname ? attrname : "", (u_long)lkflags, (u_long)flags)); 725 726 ntmp = VFSTONTFS(mp); 727 *vpp = NULL; 728 729 loop: 730 /* Get ntnode */ 731 error = ntfs_ntlookup(ntmp, ino, &ip); 732 if (error) { 733 printf("ntfs_vget: ntfs_ntget failed\n"); 734 return (error); 735 } 736 737 /* It may be not initialized fully, so force load it */ 738 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) { 739 error = ntfs_loadntnode(ntmp, ip); 740 if(error) { 741 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO:" 742 " %llu\n", (unsigned long long)ip->i_number); 743 ntfs_ntput(ip); 744 return (error); 745 } 746 } 747 748 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp); 749 if (error) { 750 printf("ntfs_vget: ntfs_fget failed\n"); 751 ntfs_ntput(ip); 752 return (error); 753 } 754 755 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) { 756 if ((ip->i_frflag & NTFS_FRFLAG_DIR) && 757 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) { 758 f_type = VDIR; 759 } else if (flags & VG_EXT) { 760 f_type = VNON; 761 fp->f_size = fp->f_allocated = 0; 762 } else { 763 f_type = VREG; 764 765 error = ntfs_filesize(ntmp, fp, 766 &fp->f_size, &fp->f_allocated); 767 if (error) { 768 ntfs_ntput(ip); 769 return (error); 770 } 771 } 772 773 fp->f_flag |= FN_VALID; 774 } 775 776 /* 777 * We may be calling vget() now. To avoid potential deadlock, we need 778 * to release ntnode lock, since due to locking order vnode 779 * lock has to be acquired first. 780 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled 781 * prematurely. 782 * Take v_interlock before releasing ntnode lock to avoid races. 783 */ 784 vp = FTOV(fp); 785 if (vp) { 786 mutex_enter(&vp->v_interlock); 787 ntfs_ntput(ip); 788 if (vget(vp, lkflags) != 0) 789 goto loop; 790 *vpp = vp; 791 return 0; 792 } 793 ntfs_ntput(ip); 794 795 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp); 796 ntfs_ntget(ip); 797 if(error) { 798 ntfs_frele(fp); 799 ntfs_ntput(ip); 800 return (error); 801 } 802 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp); 803 if (error) { 804 printf("ntfs_vget: ntfs_fget failed\n"); 805 ntfs_ntput(ip); 806 return (error); 807 } 808 if (FTOV(fp)) { 809 /* 810 * Another thread beat us, put back freshly allocated 811 * vnode and retry. 812 */ 813 ntfs_ntput(ip); 814 ungetnewvnode(vp); 815 goto loop; 816 } 817 dprintf(("ntfs_vget: vnode: %p for ntnode: %llu\n", vp, 818 (unsigned long long)ino)); 819 820 fp->f_vp = vp; 821 vp->v_data = fp; 822 if (f_type != VBAD) 823 vp->v_type = f_type; 824 genfs_node_init(vp, &ntfs_genfsops); 825 826 if (ino == NTFS_ROOTINO) 827 vp->v_vflag |= VV_ROOT; 828 829 ntfs_ntput(ip); 830 831 if (lkflags & (LK_EXCLUSIVE | LK_SHARED)) { 832 error = vn_lock(vp, lkflags); 833 if (error) { 834 vput(vp); 835 return (error); 836 } 837 } 838 839 uvm_vnp_setsize(vp, fp->f_size); /* XXX: mess, cf. ntfs_lookupfile() */ 840 vref(ip->i_devvp); 841 *vpp = vp; 842 return (0); 843 } 844 845 static int 846 ntfs_vget( 847 struct mount *mp, 848 ino_t ino, 849 struct vnode **vpp) 850 { 851 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, LK_EXCLUSIVE, 0, vpp); 852 } 853 854 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc; 855 856 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = { 857 &ntfs_vnodeop_opv_desc, 858 NULL, 859 }; 860 861 struct vfsops ntfs_vfsops = { 862 MOUNT_NTFS, 863 sizeof (struct ntfs_args), 864 ntfs_mount, 865 ntfs_start, 866 ntfs_unmount, 867 ntfs_root, 868 (void *)eopnotsupp, /* vfs_quotactl */ 869 ntfs_statvfs, 870 ntfs_sync, 871 ntfs_vget, 872 ntfs_fhtovp, 873 ntfs_vptofh, 874 ntfs_init, 875 ntfs_reinit, 876 ntfs_done, 877 ntfs_mountroot, 878 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp, 879 vfs_stdextattrctl, 880 (void *)eopnotsupp, /* vfs_suspendctl */ 881 genfs_renamelock_enter, 882 genfs_renamelock_exit, 883 (void *)eopnotsupp, 884 ntfs_vnodeopv_descs, 885 0, 886 { NULL, NULL }, 887 }; 888 889 static int 890 ntfs_modcmd(modcmd_t cmd, void *arg) 891 { 892 int error; 893 894 switch (cmd) { 895 case MODULE_CMD_INIT: 896 error = vfs_attach(&ntfs_vfsops); 897 if (error != 0) 898 break; 899 sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL, 900 CTLFLAG_PERMANENT, 901 CTLTYPE_NODE, "vfs", NULL, 902 NULL, 0, NULL, 0, 903 CTL_VFS, CTL_EOL); 904 sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL, 905 CTLFLAG_PERMANENT, 906 CTLTYPE_NODE, "ntfs", 907 SYSCTL_DESCR("NTFS file system"), 908 NULL, 0, NULL, 0, 909 CTL_VFS, 20, CTL_EOL); 910 /* 911 * XXX the "20" above could be dynamic, thereby eliminating 912 * one more instance of the "number to vfs" mapping problem, 913 * but "20" is the order as taken from sys/mount.h 914 */ 915 break; 916 case MODULE_CMD_FINI: 917 error = vfs_detach(&ntfs_vfsops); 918 if (error != 0) 919 break; 920 sysctl_teardown(&ntfs_sysctl_log); 921 break; 922 default: 923 error = ENOTTY; 924 break; 925 } 926 927 return (error); 928 } 929