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