1 /* $OpenBSD: ntfs_vfsops.c,v 1.52 2016/06/19 11:54:33 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_stat.f_namemax = NTFS_MAXFILENAME; 440 mp->mnt_flag |= MNT_LOCAL; 441 devvp->v_specmountpoint = mp; 442 return (0); 443 444 out1: 445 for (i = 0; i < NTFS_SYSNODESNUM; i++) 446 if (ntmp->ntm_sysvn[i]) 447 vrele(ntmp->ntm_sysvn[i]); 448 449 if (vflush(mp,NULLVP,0)) 450 DPRINTF("ntfs_mountfs: vflush failed\n"); 451 452 out: 453 if (devvp->v_specinfo) 454 devvp->v_specmountpoint = NULL; 455 if (bp) 456 brelse(bp); 457 458 if (ntmp != NULL) { 459 if (ntmp->ntm_ad != NULL) 460 free(ntmp->ntm_ad, M_NTFSMNT, 0); 461 free(ntmp, M_NTFSMNT, 0); 462 mp->mnt_data = NULL; 463 } 464 465 /* lock the device vnode before calling VOP_CLOSE() */ 466 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 467 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 468 VOP_UNLOCK(devvp, p); 469 470 return (error); 471 } 472 473 int 474 ntfs_start(struct mount *mp, int flags, struct proc *p) 475 { 476 return (0); 477 } 478 479 int 480 ntfs_unmount(struct mount *mp, int mntflags, struct proc *p) 481 { 482 struct ntfsmount *ntmp; 483 int error, ronly = 0, flags, i; 484 485 DPRINTF("ntfs_unmount: unmounting...\n"); 486 ntmp = VFSTONTFS(mp); 487 488 flags = 0; 489 if(mntflags & MNT_FORCE) 490 flags |= FORCECLOSE; 491 492 DPRINTF("ntfs_unmount: vflushing...\n"); 493 error = vflush(mp,NULLVP,flags | SKIPSYSTEM); 494 if (error) { 495 DPRINTF("ntfs_unmount: vflush failed: %d\n", error); 496 return (error); 497 } 498 499 /* Check if system vnodes are still referenced */ 500 for(i=0;i<NTFS_SYSNODESNUM;i++) { 501 if(((mntflags & MNT_FORCE) == 0) && (ntmp->ntm_sysvn[i] && 502 ntmp->ntm_sysvn[i]->v_usecount > 1)) 503 return (EBUSY); 504 } 505 506 /* Dereference all system vnodes */ 507 for(i=0;i<NTFS_SYSNODESNUM;i++) 508 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]); 509 510 /* vflush system vnodes */ 511 error = vflush(mp,NULLVP,flags); 512 if (error) { 513 /* XXX should this be panic() ? */ 514 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error); 515 } 516 517 /* Check if the type of device node isn't VBAD before 518 * touching v_specinfo. If the device vnode is revoked, the 519 * field is NULL and touching it causes null pointer derefercence. 520 */ 521 if (ntmp->ntm_devvp->v_type != VBAD) 522 ntmp->ntm_devvp->v_specmountpoint = NULL; 523 524 /* lock the device vnode before calling VOP_CLOSE() */ 525 vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY, p); 526 vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0); 527 (void)VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE, 528 NOCRED, p); 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 (0); 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_favail = sbp->f_bfree / ntmp->ntm_bpmftrec; 615 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) + 616 sbp->f_ffree; 617 copy_statfs_info(sbp, mp); 618 619 return (0); 620 } 621 622 int 623 ntfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p) 624 { 625 /*DPRINTF("ntfs_sync():\n");*/ 626 return (0); 627 } 628 629 int 630 ntfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 631 { 632 struct ntfid *ntfhp = (struct ntfid *)fhp; 633 int error; 634 635 DDPRINTF("ntfs_fhtovp(): %s: %u\n", 636 mp->mnt_stat.f_mntonname, ntfhp->ntfid_ino); 637 638 error = ntfs_vgetex(mp, ntfhp->ntfid_ino, ntfhp->ntfid_attr, NULL, 639 LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */ 640 if (error != 0) { 641 *vpp = NULLVP; 642 return (error); 643 } 644 645 /* XXX as unlink/rmdir/mkdir/creat are not currently possible 646 * with NTFS, we don't need to check anything else for now */ 647 return (0); 648 } 649 650 int 651 ntfs_vptofh(struct vnode *vp, struct fid *fhp) 652 { 653 struct ntnode *ntp; 654 struct ntfid *ntfhp; 655 struct fnode *fn; 656 657 DDPRINTF("ntfs_fhtovp(): %s: %p\n", 658 vp->v_mount->mnt_stat.f_mntonname, vp); 659 660 fn = VTOF(vp); 661 ntp = VTONT(vp); 662 ntfhp = (struct ntfid *)fhp; 663 ntfhp->ntfid_len = sizeof(struct ntfid); 664 ntfhp->ntfid_ino = ntp->i_number; 665 ntfhp->ntfid_attr = fn->f_attrtype; 666 #ifdef notyet 667 ntfhp->ntfid_gen = ntp->i_gen; 668 #endif 669 return (0); 670 } 671 672 int 673 ntfs_vgetex(struct mount *mp, ntfsino_t ino, u_int32_t attrtype, char *attrname, 674 u_long lkflags, u_long flags, struct proc *p, struct vnode **vpp) 675 { 676 int error; 677 struct ntfsmount *ntmp; 678 struct ntnode *ip; 679 struct fnode *fp; 680 struct vnode *vp; 681 enum vtype f_type; 682 683 DPRINTF("ntfs_vgetex: ino: %u, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n", 684 ino, attrtype, attrname ? attrname : "", lkflags, flags); 685 686 ntmp = VFSTONTFS(mp); 687 *vpp = NULL; 688 689 /* Get ntnode */ 690 error = ntfs_ntlookup(ntmp, ino, &ip, p); 691 if (error) { 692 printf("ntfs_vget: ntfs_ntget failed\n"); 693 return (error); 694 } 695 696 /* It may be not initialized fully, so force load it */ 697 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) { 698 error = ntfs_loadntnode(ntmp, ip); 699 if(error) { 700 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n", 701 ip->i_number); 702 ntfs_ntput(ip, p); 703 704 return (error); 705 } 706 } 707 708 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp); 709 if (error) { 710 printf("ntfs_vget: ntfs_fget failed\n"); 711 ntfs_ntput(ip, p); 712 713 return (error); 714 } 715 716 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) { 717 if ((ip->i_frflag & NTFS_FRFLAG_DIR) && 718 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) { 719 f_type = VDIR; 720 } else if (flags & VG_EXT) { 721 f_type = VNON; 722 fp->f_size = fp->f_allocated = 0; 723 } else { 724 f_type = VREG; 725 726 error = ntfs_filesize(ntmp, fp, 727 &fp->f_size, &fp->f_allocated); 728 if (error) { 729 ntfs_ntput(ip, p); 730 731 return (error); 732 } 733 } 734 735 fp->f_flag |= FN_VALID; 736 } 737 738 /* 739 * We may be calling vget() now. To avoid potential deadlock, we need 740 * to release ntnode lock, since due to locking order vnode 741 * lock has to be acquired first. 742 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled 743 * prematurely. 744 */ 745 ntfs_ntput(ip, p); 746 747 if (FTOV(fp)) { 748 /* vget() returns error if the vnode has been recycled */ 749 if (vget(FTOV(fp), lkflags, p) == 0) { 750 *vpp = FTOV(fp); 751 return (0); 752 } 753 } 754 755 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &ntfs_vops, &vp); 756 if(error) { 757 ntfs_frele(fp); 758 ntfs_ntput(ip, p); 759 760 return (error); 761 } 762 DPRINTF("ntfs_vget: vnode: %p for ntnode: %u\n", vp, ino); 763 764 fp->f_vp = vp; 765 vp->v_data = fp; 766 vp->v_type = f_type; 767 768 if (ino == NTFS_ROOTINO) 769 vp->v_flag |= VROOT; 770 771 if (lkflags & LK_TYPE_MASK) { 772 error = vn_lock(vp, lkflags, p); 773 if (error) { 774 vput(vp); 775 return (error); 776 } 777 } 778 779 *vpp = vp; 780 return (0); 781 } 782 783 int 784 ntfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 785 { 786 if (ino > (ntfsino_t)-1) 787 panic("ntfs_vget: alien ino_t %llu", (unsigned long long)ino); 788 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, 789 LK_EXCLUSIVE | LK_RETRY, 0, curproc, vpp); /* XXX */ 790 } 791 792 const struct vfsops ntfs_vfsops = { 793 ntfs_mount, 794 ntfs_start, 795 ntfs_unmount, 796 ntfs_root, 797 ntfs_quotactl, 798 ntfs_statfs, 799 ntfs_sync, 800 ntfs_vget, 801 ntfs_fhtovp, 802 ntfs_vptofh, 803 ntfs_init, 804 ntfs_sysctl, 805 ntfs_checkexp, 806 }; 807