1 /* $OpenBSD: ntfs_vfsops.c,v 1.48 2016/03/19 12:04:16 natano Exp $ */ 2 /* $NetBSD: ntfs_vfsops.c,v 1.7 2003/04/24 07:50:19 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1998, 1999 Semen Ustimenko 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * Id: ntfs_vfsops.c,v 1.7 1999/05/31 11:28:30 phk Exp 30 */ 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/namei.h> 35 #include <sys/proc.h> 36 #include <sys/kernel.h> 37 #include <sys/vnode.h> 38 #include <sys/lock.h> 39 #include <sys/mount.h> 40 #include <sys/buf.h> 41 #include <sys/disk.h> 42 #include <sys/fcntl.h> 43 #include <sys/malloc.h> 44 #include <sys/device.h> 45 #include <sys/conf.h> 46 #include <sys/specdev.h> 47 48 /*#define NTFS_DEBUG 1*/ 49 #include <ntfs/ntfs.h> 50 #include <ntfs/ntfs_inode.h> 51 #include <ntfs/ntfs_subr.h> 52 #include <ntfs/ntfs_vfsops.h> 53 #include <ntfs/ntfs_ihash.h> 54 55 int ntfs_mount(struct mount *, const char *, void *, 56 struct nameidata *, struct proc *); 57 int ntfs_quotactl(struct mount *, int, uid_t, caddr_t, 58 struct proc *); 59 int ntfs_root(struct mount *, struct vnode **); 60 int ntfs_start(struct mount *, int, struct proc *); 61 int ntfs_statfs(struct mount *, struct statfs *, 62 struct proc *); 63 int ntfs_sync(struct mount *, int, struct ucred *, 64 struct proc *); 65 int ntfs_unmount(struct mount *, int, struct proc *); 66 int ntfs_vget(struct mount *mp, ino_t ino, 67 struct vnode **vpp); 68 int ntfs_mountfs(struct vnode *, struct mount *, 69 struct ntfs_args *, struct proc *); 70 int ntfs_vptofh(struct vnode *, struct fid *); 71 72 int ntfs_init(struct vfsconf *); 73 int ntfs_fhtovp(struct mount *, struct fid *, 74 struct vnode **); 75 int ntfs_checkexp(struct mount *, struct mbuf *, 76 int *, struct ucred **); 77 int ntfs_sysctl(int *, u_int, void *, size_t *, void *, 78 size_t, struct proc *); 79 80 /* 81 * Verify a remote client has export rights and return these rights via. 82 * exflagsp and credanonp. 83 */ 84 int 85 ntfs_checkexp(struct mount *mp, struct mbuf *nam, int *exflagsp, 86 struct ucred **credanonp) 87 { 88 struct netcred *np; 89 struct ntfsmount *ntm = VFSTONTFS(mp); 90 91 /* 92 * Get the export permission structure for this <mp, client> tuple. 93 */ 94 np = vfs_export_lookup(mp, &ntm->ntm_export, nam); 95 if (np == NULL) 96 return (EACCES); 97 98 *exflagsp = np->netc_exflags; 99 *credanonp = &np->netc_anon; 100 return (0); 101 } 102 103 int 104 ntfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 105 size_t newlen, struct proc *p) 106 { 107 return (EINVAL); 108 } 109 110 int 111 ntfs_init(struct vfsconf *vcp) 112 { 113 return 0; 114 } 115 116 int 117 ntfs_mount(struct mount *mp, const char *path, void *data, 118 struct nameidata *ndp, struct proc *p) 119 { 120 int err = 0; 121 struct vnode *devvp; 122 struct ntfs_args args; 123 char fname[MNAMELEN]; 124 char fspec[MNAMELEN]; 125 mode_t amode; 126 127 ntfs_nthashinit(); 128 129 /* 130 *** 131 * Mounting non-root file system or updating a file system 132 *** 133 */ 134 135 /* copy in user arguments*/ 136 err = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args)); 137 if (err) 138 goto error_1; /* can't get arguments*/ 139 140 /* 141 * If updating, check whether changing from read-only to 142 * read/write; if there is no device name, that's all we do. 143 */ 144 if (mp->mnt_flag & MNT_UPDATE) { 145 /* if not updating name...*/ 146 if (args.fspec == NULL) { 147 /* 148 * Process export requests. Jumping to "success" 149 * will return the vfs_export() error code. 150 */ 151 struct ntfsmount *ntm = VFSTONTFS(mp); 152 err = vfs_export(mp, &ntm->ntm_export, &args.export_info); 153 goto success; 154 } 155 156 printf("ntfs_mount(): MNT_UPDATE not supported\n"); 157 err = EINVAL; 158 goto error_1; 159 } 160 161 /* 162 * Not an update, or updating the name: look up the name 163 * and verify that it refers to a sensible block device. 164 */ 165 err = copyinstr(args.fspec, fspec, sizeof(fspec), NULL); 166 if (err) 167 goto error_1; 168 169 if (disk_map(fspec, fname, sizeof(fname), DM_OPENBLCK) == -1) 170 bcopy(fspec, fname, sizeof(fname)); 171 172 NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, p); 173 err = namei(ndp); 174 if (err) { 175 /* can't get devvp!*/ 176 goto error_1; 177 } 178 179 devvp = ndp->ni_vp; 180 181 if (devvp->v_type != VBLK) { 182 err = ENOTBLK; 183 goto error_2; 184 } 185 186 if (major(devvp->v_rdev) >= nblkdev) { 187 err = ENXIO; 188 goto error_2; 189 } 190 191 /* 192 * If we are not root, make sure we have permission to access the 193 * requested device. 194 */ 195 if (p->p_ucred->cr_uid) { 196 amode = (mp->mnt_flag & MNT_RDONLY) ? VREAD : (VREAD | VWRITE); 197 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 198 err = VOP_ACCESS(devvp, amode, p->p_ucred, p); 199 VOP_UNLOCK(devvp, p); 200 if (err) 201 goto error_2; 202 } 203 204 if (mp->mnt_flag & MNT_UPDATE) { 205 #if 0 206 /* 207 ******************** 208 * UPDATE 209 ******************** 210 */ 211 212 if (devvp != ntmp->um_devvp) 213 err = EINVAL; /* needs translation */ 214 else 215 vrele(devvp); 216 /* 217 * Update device name only on success 218 */ 219 if( !err) { 220 err = set_statfs_info(NULL, UIO_USERSPACE, args.fspec, 221 UIO_USERSPACE, mp, p); 222 } 223 #endif 224 } else { 225 /* 226 ******************** 227 * NEW MOUNT 228 ******************** 229 */ 230 231 /* 232 * Since this is a new mount, we want the names for 233 * the device and the mount point copied in. If an 234 * error occurs, the mountpoint is discarded by the 235 * upper level code. 236 */ 237 /* Save "last mounted on" info for mount point (NULL pad)*/ 238 bzero(mp->mnt_stat.f_mntonname, MNAMELEN); 239 strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN); 240 bzero(mp->mnt_stat.f_mntfromname, MNAMELEN); 241 strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN); 242 bzero(mp->mnt_stat.f_mntfromspec, MNAMELEN); 243 strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN); 244 bcopy(&args, &mp->mnt_stat.mount_info.ntfs_args, sizeof(args)); 245 if ( !err) { 246 err = ntfs_mountfs(devvp, mp, &args, p); 247 } 248 } 249 if (err) { 250 goto error_2; 251 } 252 253 /* 254 * Initialize FS stat information in mount struct; uses both 255 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname 256 * 257 * This code is common to root and non-root mounts 258 */ 259 (void)VFS_STATFS(mp, &mp->mnt_stat, p); 260 261 goto success; 262 263 264 error_2: /* error with devvp held*/ 265 266 /* release devvp before failing*/ 267 vrele(devvp); 268 269 error_1: /* no state to back out*/ 270 271 success: 272 return(err); 273 } 274 275 /* 276 * Common code for mount and mountroot 277 */ 278 int 279 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp, 280 struct proc *p) 281 { 282 struct buf *bp; 283 struct ntfsmount *ntmp = NULL; 284 dev_t dev = devvp->v_rdev; 285 int error, ronly, ncount, i; 286 struct vnode *vp; 287 288 /* 289 * Disallow multiple mounts of the same device. 290 * Disallow mounting of a device that is currently in use 291 * (except for root, which might share swap device for miniroot). 292 * Flush out any old buffers remaining from a previous use. 293 */ 294 error = vfs_mountedon(devvp); 295 if (error) 296 return (error); 297 ncount = vcount(devvp); 298 if (ncount > 1 && devvp != rootvp) 299 return (EBUSY); 300 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 301 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0); 302 VOP_UNLOCK(devvp, p); 303 if (error) 304 return (error); 305 306 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 307 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 308 if (error) 309 return (error); 310 311 bp = NULL; 312 313 error = bread(devvp, BBLOCK, BBSIZE, &bp); 314 if (error) 315 goto out; 316 ntmp = malloc(sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO); 317 bcopy(bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile)); 318 brelse(bp); 319 bp = NULL; 320 321 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) { 322 error = EINVAL; 323 DPRINTF("ntfs_mountfs: invalid boot block\n"); 324 goto out; 325 } 326 327 { 328 int8_t cpr = ntmp->ntm_mftrecsz; 329 if( cpr > 0 ) 330 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr; 331 else 332 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps; 333 } 334 DPRINTF("ntfs_mountfs(): bps: %u, spc: %u, media: %x, " 335 "mftrecsz: %u (%u sects)\n", ntmp->ntm_bps, ntmp->ntm_spc, 336 ntmp->ntm_bootfile.bf_media, ntmp->ntm_mftrecsz, 337 ntmp->ntm_bpmftrec); 338 DPRINTF("ntfs_mountfs(): mftcn: 0x%llx|0x%llx\n", 339 ntmp->ntm_mftcn, ntmp->ntm_mftmirrcn); 340 341 ntmp->ntm_mountp = mp; 342 ntmp->ntm_dev = dev; 343 ntmp->ntm_devvp = devvp; 344 ntmp->ntm_uid = argsp->uid; 345 ntmp->ntm_gid = argsp->gid; 346 ntmp->ntm_mode = argsp->mode; 347 ntmp->ntm_flag = argsp->flag; 348 mp->mnt_data = (qaddr_t) ntmp; 349 TAILQ_INIT(&ntmp->ntm_ntnodeq); 350 351 /* set file name encode/decode hooks XXX utf-8 only for now */ 352 ntmp->ntm_wget = ntfs_utf8_wget; 353 ntmp->ntm_wput = ntfs_utf8_wput; 354 ntmp->ntm_wcmp = ntfs_utf8_wcmp; 355 356 DPRINTF("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n", 357 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS) ? "insens." : "sens.", 358 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES) ? " allnames," : "", 359 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode); 360 361 /* 362 * We read in some system nodes to do not allow 363 * reclaim them and to have everytime access to them. 364 */ 365 { 366 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO }; 367 for (i=0; i<3; i++) { 368 error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]])); 369 if(error) 370 goto out1; 371 ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM; 372 vref(ntmp->ntm_sysvn[pi[i]]); 373 vput(ntmp->ntm_sysvn[pi[i]]); 374 } 375 } 376 377 /* read the Unicode lowercase --> uppercase translation table, 378 * if necessary */ 379 if ((error = ntfs_toupper_use(mp, ntmp, p))) 380 goto out1; 381 382 /* 383 * Scan $BitMap and count free clusters 384 */ 385 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree); 386 if(error) 387 goto out1; 388 389 /* 390 * Read and translate to internal format attribute 391 * definition file. 392 */ 393 { 394 int num,j; 395 struct attrdef ad; 396 397 /* Open $AttrDef */ 398 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp ); 399 if(error) 400 goto out1; 401 402 /* Count valid entries */ 403 for(num = 0; ; num++) { 404 error = ntfs_readattr(ntmp, VTONT(vp), 405 NTFS_A_DATA, NULL, num * sizeof(ad), sizeof(ad), 406 &ad, NULL); 407 if (error) 408 goto out1; 409 if (ad.ad_name[0] == 0) 410 break; 411 } 412 413 /* Alloc memory for attribute definitions */ 414 ntmp->ntm_ad = mallocarray(num, sizeof(struct ntvattrdef), 415 M_NTFSMNT, M_WAITOK); 416 417 ntmp->ntm_adnum = num; 418 419 /* Read them and translate */ 420 for(i = 0; i < num; i++){ 421 error = ntfs_readattr(ntmp, VTONT(vp), 422 NTFS_A_DATA, NULL, i * sizeof(ad), sizeof(ad), 423 &ad, NULL); 424 if (error) 425 goto out1; 426 j = 0; 427 do { 428 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j]; 429 } while(ad.ad_name[j++]); 430 ntmp->ntm_ad[i].ad_namelen = j - 1; 431 ntmp->ntm_ad[i].ad_type = ad.ad_type; 432 } 433 434 vput(vp); 435 } 436 437 mp->mnt_stat.f_fsid.val[0] = dev; 438 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 439 mp->mnt_flag |= MNT_LOCAL; 440 devvp->v_specmountpoint = mp; 441 return (0); 442 443 out1: 444 for (i = 0; i < NTFS_SYSNODESNUM; i++) 445 if (ntmp->ntm_sysvn[i]) 446 vrele(ntmp->ntm_sysvn[i]); 447 448 if (vflush(mp,NULLVP,0)) 449 DPRINTF("ntfs_mountfs: vflush failed\n"); 450 451 out: 452 devvp->v_specmountpoint = NULL; 453 if (bp) 454 brelse(bp); 455 456 if (ntmp != NULL) { 457 if (ntmp->ntm_ad != NULL) 458 free(ntmp->ntm_ad, M_NTFSMNT, 0); 459 free(ntmp, M_NTFSMNT, 0); 460 mp->mnt_data = NULL; 461 } 462 463 /* lock the device vnode before calling VOP_CLOSE() */ 464 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 465 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 466 VOP_UNLOCK(devvp, p); 467 468 return (error); 469 } 470 471 int 472 ntfs_start(struct mount *mp, int flags, struct proc *p) 473 { 474 return (0); 475 } 476 477 int 478 ntfs_unmount(struct mount *mp, int mntflags, struct proc *p) 479 { 480 struct ntfsmount *ntmp; 481 int error, ronly = 0, flags, i; 482 483 DPRINTF("ntfs_unmount: unmounting...\n"); 484 ntmp = VFSTONTFS(mp); 485 486 flags = 0; 487 if(mntflags & MNT_FORCE) 488 flags |= FORCECLOSE; 489 490 DPRINTF("ntfs_unmount: vflushing...\n"); 491 error = vflush(mp,NULLVP,flags | SKIPSYSTEM); 492 if (error) { 493 DPRINTF("ntfs_unmount: vflush failed: %d\n", error); 494 return (error); 495 } 496 497 /* Check if system vnodes are still referenced */ 498 for(i=0;i<NTFS_SYSNODESNUM;i++) { 499 if(((mntflags & MNT_FORCE) == 0) && (ntmp->ntm_sysvn[i] && 500 ntmp->ntm_sysvn[i]->v_usecount > 1)) 501 return (EBUSY); 502 } 503 504 /* Dereference all system vnodes */ 505 for(i=0;i<NTFS_SYSNODESNUM;i++) 506 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]); 507 508 /* vflush system vnodes */ 509 error = vflush(mp,NULLVP,flags); 510 if (error) { 511 /* XXX should this be panic() ? */ 512 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error); 513 } 514 515 /* Check if the type of device node isn't VBAD before 516 * touching v_specinfo. If the device vnode is revoked, the 517 * field is NULL and touching it causes null pointer derefercence. 518 */ 519 if (ntmp->ntm_devvp->v_type != VBAD) 520 ntmp->ntm_devvp->v_specmountpoint = NULL; 521 522 /* lock the device vnode before calling VOP_CLOSE() */ 523 VOP_LOCK(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY, p); 524 vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0); 525 526 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, 527 NOCRED, p); 528 529 vput(ntmp->ntm_devvp); 530 531 /* free the toupper table, if this has been last mounted ntfs volume */ 532 ntfs_toupper_unuse(p); 533 534 DPRINTF("ntfs_unmount: freeing memory...\n"); 535 free(ntmp->ntm_ad, M_NTFSMNT, 0); 536 free(ntmp, M_NTFSMNT, 0); 537 mp->mnt_data = NULL; 538 mp->mnt_flag &= ~MNT_LOCAL; 539 return (error); 540 } 541 542 int 543 ntfs_root(struct mount *mp, struct vnode **vpp) 544 { 545 struct vnode *nvp; 546 int error = 0; 547 548 DPRINTF("ntfs_root(): sysvn: %p\n", 549 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]); 550 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp); 551 if(error) { 552 printf("ntfs_root: VFS_VGET failed: %d\n",error); 553 return (error); 554 } 555 556 *vpp = nvp; 557 return (0); 558 } 559 560 /* 561 * Do operations associated with quotas, not supported 562 */ 563 int 564 ntfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg, 565 struct proc *p) 566 { 567 return EOPNOTSUPP; 568 } 569 570 int 571 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep) 572 { 573 struct vnode *vp; 574 u_int8_t *tmp; 575 int j, error; 576 cn_t cfree = 0; 577 size_t bmsize, i; 578 579 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO]; 580 581 bmsize = VTOF(vp)->f_size; 582 583 tmp = malloc(bmsize, M_TEMP, M_WAITOK); 584 585 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL, 586 0, bmsize, tmp, NULL); 587 if (error) 588 goto out; 589 590 for(i=0;i<bmsize;i++) 591 for(j=0;j<8;j++) 592 if(~tmp[i] & (1 << j)) cfree++; 593 *cfreep = cfree; 594 595 out: 596 free(tmp, M_TEMP, 0); 597 return(error); 598 } 599 600 int 601 ntfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 602 { 603 struct ntfsmount *ntmp = VFSTONTFS(mp); 604 u_int64_t mftallocated; 605 606 DPRINTF("ntfs_statfs():\n"); 607 608 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated; 609 610 sbp->f_bsize = ntmp->ntm_bps; 611 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc; 612 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv; 613 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree); 614 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec; 615 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) + 616 sbp->f_ffree; 617 sbp->f_flags = mp->mnt_flag; 618 if (sbp != &mp->mnt_stat) { 619 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 620 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 621 bcopy(&mp->mnt_stat.mount_info.ntfs_args, 622 &sbp->mount_info.ntfs_args, sizeof(struct ntfs_args)); 623 } 624 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 625 626 return (0); 627 } 628 629 int 630 ntfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p) 631 { 632 /*DPRINTF("ntfs_sync():\n");*/ 633 return (0); 634 } 635 636 int 637 ntfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 638 { 639 struct ntfid *ntfhp = (struct ntfid *)fhp; 640 int error; 641 642 DDPRINTF("ntfs_fhtovp(): %s: %u\n", 643 mp->mnt_stat.f_mntonname, ntfhp->ntfid_ino); 644 645 error = ntfs_vgetex(mp, ntfhp->ntfid_ino, ntfhp->ntfid_attr, NULL, 646 LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */ 647 if (error != 0) { 648 *vpp = NULLVP; 649 return (error); 650 } 651 652 /* XXX as unlink/rmdir/mkdir/creat are not currently possible 653 * with NTFS, we don't need to check anything else for now */ 654 return (0); 655 } 656 657 int 658 ntfs_vptofh(struct vnode *vp, struct fid *fhp) 659 { 660 struct ntnode *ntp; 661 struct ntfid *ntfhp; 662 struct fnode *fn; 663 664 DDPRINTF("ntfs_fhtovp(): %s: %p\n", 665 vp->v_mount->mnt_stat.f_mntonname, vp); 666 667 fn = VTOF(vp); 668 ntp = VTONT(vp); 669 ntfhp = (struct ntfid *)fhp; 670 ntfhp->ntfid_len = sizeof(struct ntfid); 671 ntfhp->ntfid_ino = ntp->i_number; 672 ntfhp->ntfid_attr = fn->f_attrtype; 673 #ifdef notyet 674 ntfhp->ntfid_gen = ntp->i_gen; 675 #endif 676 return (0); 677 } 678 679 int 680 ntfs_vgetex(struct mount *mp, ntfsino_t ino, u_int32_t attrtype, char *attrname, 681 u_long lkflags, u_long flags, struct proc *p, struct vnode **vpp) 682 { 683 int error; 684 struct ntfsmount *ntmp; 685 struct ntnode *ip; 686 struct fnode *fp; 687 struct vnode *vp; 688 enum vtype f_type; 689 690 DPRINTF("ntfs_vgetex: ino: %u, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n", 691 ino, attrtype, attrname ? attrname : "", lkflags, flags); 692 693 ntmp = VFSTONTFS(mp); 694 *vpp = NULL; 695 696 /* Get ntnode */ 697 error = ntfs_ntlookup(ntmp, ino, &ip, p); 698 if (error) { 699 printf("ntfs_vget: ntfs_ntget failed\n"); 700 return (error); 701 } 702 703 /* It may be not initialized fully, so force load it */ 704 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) { 705 error = ntfs_loadntnode(ntmp, ip); 706 if(error) { 707 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n", 708 ip->i_number); 709 ntfs_ntput(ip, p); 710 711 return (error); 712 } 713 } 714 715 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp); 716 if (error) { 717 printf("ntfs_vget: ntfs_fget failed\n"); 718 ntfs_ntput(ip, p); 719 720 return (error); 721 } 722 723 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) { 724 if ((ip->i_frflag & NTFS_FRFLAG_DIR) && 725 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) { 726 f_type = VDIR; 727 } else if (flags & VG_EXT) { 728 f_type = VNON; 729 fp->f_size = fp->f_allocated = 0; 730 } else { 731 f_type = VREG; 732 733 error = ntfs_filesize(ntmp, fp, 734 &fp->f_size, &fp->f_allocated); 735 if (error) { 736 ntfs_ntput(ip, p); 737 738 return (error); 739 } 740 } 741 742 fp->f_flag |= FN_VALID; 743 } 744 745 /* 746 * We may be calling vget() now. To avoid potential deadlock, we need 747 * to release ntnode lock, since due to locking order vnode 748 * lock has to be acquired first. 749 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled 750 * prematurely. 751 */ 752 ntfs_ntput(ip, p); 753 754 if (FTOV(fp)) { 755 /* vget() returns error if the vnode has been recycled */ 756 if (vget(FTOV(fp), lkflags, p) == 0) { 757 *vpp = FTOV(fp); 758 return (0); 759 } 760 } 761 762 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &ntfs_vops, &vp); 763 if(error) { 764 ntfs_frele(fp); 765 ntfs_ntput(ip, p); 766 767 return (error); 768 } 769 DPRINTF("ntfs_vget: vnode: %p for ntnode: %u\n", vp, ino); 770 771 fp->f_vp = vp; 772 vp->v_data = fp; 773 vp->v_type = f_type; 774 775 if (ino == NTFS_ROOTINO) 776 vp->v_flag |= VROOT; 777 778 if (lkflags & LK_TYPE_MASK) { 779 error = vn_lock(vp, lkflags, p); 780 if (error) { 781 vput(vp); 782 return (error); 783 } 784 } 785 786 *vpp = vp; 787 return (0); 788 } 789 790 int 791 ntfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 792 { 793 if (ino > (ntfsino_t)-1) 794 panic("ntfs_vget: alien ino_t %llu", (unsigned long long)ino); 795 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, 796 LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */ 797 } 798 799 const struct vfsops ntfs_vfsops = { 800 ntfs_mount, 801 ntfs_start, 802 ntfs_unmount, 803 ntfs_root, 804 ntfs_quotactl, 805 ntfs_statfs, 806 ntfs_sync, 807 ntfs_vget, 808 ntfs_fhtovp, 809 ntfs_vptofh, 810 ntfs_init, 811 ntfs_sysctl, 812 ntfs_checkexp, 813 }; 814