1 /* $NetBSD: ntfs_vfsops.c,v 1.23 1999/11/15 19:38:14 jdolecek 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 * $FreeBSD: src/sys/ntfs/ntfs_vfsops.c,v 1.20.2.5 2001/12/25 01:44:45 dillon Exp $ 29 */ 30 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/conf.h> 35 #include <sys/proc.h> 36 #include <sys/nlookup.h> 37 #include <sys/kernel.h> 38 #include <sys/vnode.h> 39 #include <sys/mount.h> 40 #include <sys/buf.h> 41 #include <sys/fcntl.h> 42 #include <sys/malloc.h> 43 #include <sys/systm.h> 44 #if defined(__NetBSD__) 45 #include <sys/device.h> 46 #endif 47 48 #include <machine/inttypes.h> 49 50 #include <vm/vm.h> 51 #include <vm/vm_param.h> 52 #if defined(__NetBSD__) 53 #include <vm/vm_prot.h> 54 #endif 55 #include <vm/vm_page.h> 56 #include <vm/vm_object.h> 57 #include <vm/vm_extern.h> 58 #include <vm/vm_zone.h> 59 60 #if defined(__NetBSD__) 61 #include <miscfs/specfs/specdev.h> 62 #endif 63 64 #include <sys/buf2.h> 65 66 #include "ntfs.h" 67 #include "ntfs_inode.h" 68 #include "ntfs_subr.h" 69 #include "ntfs_vfsops.h" 70 #include "ntfs_ihash.h" 71 #include "ntfsmount.h" 72 73 extern struct vop_ops ntfs_vnode_vops; 74 75 #if defined(__DragonFly__) 76 MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure"); 77 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information"); 78 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information"); 79 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer"); 80 #endif 81 82 struct iconv_functions *ntfs_iconv = NULL; 83 84 static int ntfs_root (struct mount *, struct vnode **); 85 static int ntfs_statfs (struct mount *, struct statfs *, 86 struct ucred *cred); 87 static int ntfs_statvfs (struct mount *, struct statvfs *, 88 struct ucred *); 89 static int ntfs_unmount (struct mount *, int); 90 static int ntfs_vget (struct mount *mp, struct vnode *dvp, 91 ino_t ino, struct vnode **vpp); 92 static int ntfs_mountfs (struct vnode *, struct mount *, 93 struct ntfs_args *, struct ucred *); 94 static int ntfs_vptofh (struct vnode *, struct fid *); 95 static int ntfs_fhtovp (struct mount *, struct vnode *rootvp, 96 struct fid *, struct vnode **); 97 98 #if !defined (__DragonFly__) 99 static int ntfs_quotactl (struct mount *, int, uid_t, caddr_t, 100 struct thread *); 101 static int ntfs_start (struct mount *, int, struct thread *); 102 static int ntfs_sync (struct mount *, int, struct ucred *, 103 struct thread *); 104 #endif 105 106 #if defined(__DragonFly__) 107 struct sockaddr; 108 static int ntfs_mount (struct mount *, char *, caddr_t, struct ucred *); 109 static int ntfs_init (struct vfsconf *); 110 static int ntfs_checkexp (struct mount *, struct sockaddr *, 111 int *, struct ucred **); 112 #elif defined(__NetBSD__) 113 static int ntfs_mount (struct mount *, const char *, void *, 114 struct nameidata *, struct thread *); 115 static void ntfs_init (void); 116 static int ntfs_mountroot (void); 117 static int ntfs_sysctl (int *, u_int, void *, size_t *, void *, 118 size_t, struct thread *); 119 static int ntfs_checkexp (struct mount *, struct mbuf *, 120 int *, struct ucred **); 121 #endif 122 123 /* 124 * Verify a remote client has export rights and return these rights via. 125 * exflagsp and credanonp. 126 */ 127 static int 128 ntfs_checkexp(struct mount *mp, 129 #if defined(__DragonFly__) 130 struct sockaddr *nam, 131 #else /* defined(__NetBSD__) */ 132 struct mbuf *nam, 133 #endif 134 int *exflagsp, struct ucred **credanonp) 135 { 136 struct netcred *np; 137 struct ntfsmount *ntm = VFSTONTFS(mp); 138 139 /* 140 * Get the export permission structure for this <mp, client> tuple. 141 */ 142 np = vfs_export_lookup(mp, &ntm->ntm_export, nam); 143 if (np == NULL) 144 return (EACCES); 145 146 *exflagsp = np->netc_exflags; 147 *credanonp = &np->netc_anon; 148 return (0); 149 } 150 151 #if defined(__NetBSD__) 152 /*ARGSUSED*/ 153 static int 154 ntfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 155 size_t newlen, struct thread *td) 156 { 157 return (EINVAL); 158 } 159 160 static int 161 ntfs_mountroot(void) 162 { 163 struct mount *mp; 164 struct vnode *rootvp; 165 struct thread *td = curthread; /* XXX */ 166 struct ntfs_args args; 167 int error; 168 169 if (root_device->dv_class != DV_DISK) 170 return (ENODEV); 171 172 /* 173 * Get vnodes for rootdev. 174 */ 175 if (bdevvp(rootdev, &rootvp)) 176 panic("ntfs_mountroot: can't setup rootvp"); 177 178 if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) { 179 vrele(rootvp); 180 return (error); 181 } 182 183 args.flag = 0; 184 args.uid = 0; 185 args.gid = 0; 186 args.mode = 0777; 187 188 if ((error = ntfs_mountfs(rootvp, mp, &args, proc0.p_ucred)) != 0) { 189 mp->mnt_op->vfs_refcount--; 190 vfs_unbusy(mp); 191 kfree(mp, M_MOUNT); 192 vrele(rootvp); 193 return (error); 194 } 195 mountlist_insert(mp, MNTINS_LAST); 196 (void)ntfs_statfs(mp, &mp->mnt_stat, proc0.p_ucred); 197 vfs_unbusy(mp); 198 return (0); 199 } 200 201 static void 202 ntfs_init(void) 203 { 204 ntfs_nthashinit(); 205 ntfs_toupper_init(); 206 } 207 208 #elif defined(__DragonFly__) 209 210 static int 211 ntfs_init(struct vfsconf *vcp) 212 { 213 ntfs_nthashinit(); 214 ntfs_toupper_init(); 215 return 0; 216 } 217 218 #endif /* NetBSD */ 219 220 static int 221 ntfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) 222 { 223 size_t size; 224 int error; 225 struct vnode *devvp; 226 struct ntfs_args args; 227 struct nlookupdata nd; 228 struct vnode *rootvp; 229 230 error = 0; 231 #ifdef __DragonFly__ 232 /* 233 * Use NULL path to flag a root mount 234 */ 235 if( path == NULL) { 236 /* 237 *** 238 * Mounting root file system 239 *** 240 */ 241 242 /* Get vnode for root device*/ 243 if( bdevvp( rootdev, &rootvp)) 244 panic("ffs_mountroot: can't setup bdevvp for root"); 245 246 /* 247 * FS specific handling 248 */ 249 mp->mnt_flag |= MNT_RDONLY; /* XXX globally applicable?*/ 250 251 /* 252 * Attempt mount 253 */ 254 if( ( error = ntfs_mountfs(rootvp, mp, &args, cred)) != 0) { 255 /* fs specific cleanup (if any)*/ 256 goto error_1; 257 } 258 259 goto dostatfs; /* success*/ 260 261 } 262 #endif /* FreeBSD */ 263 264 /* 265 *** 266 * Mounting non-root file system or updating a file system 267 *** 268 */ 269 270 /* copy in user arguments*/ 271 error = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args)); 272 if (error) 273 goto error_1; /* can't get arguments*/ 274 275 /* 276 * If updating, check whether changing from read-only to 277 * read/write; if there is no device name, that's all we do. 278 */ 279 if (mp->mnt_flag & MNT_UPDATE) { 280 /* if not updating name...*/ 281 if (args.fspec == NULL) { 282 /* 283 * Process export requests. Jumping to "success" 284 * will return the vfs_export() error code. 285 */ 286 struct ntfsmount *ntm = VFSTONTFS(mp); 287 error = vfs_export(mp, &ntm->ntm_export, &args.export); 288 goto success; 289 } 290 291 kprintf("ntfs_mount(): MNT_UPDATE not supported\n"); 292 error = EINVAL; 293 goto error_1; 294 } 295 296 /* 297 * Not an update, or updating the name: look up the name 298 * and verify that it refers to a sensible block device. 299 */ 300 devvp = NULL; 301 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW); 302 if (error == 0) 303 error = nlookup(&nd); 304 if (error == 0) 305 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp); 306 nlookup_done(&nd); 307 if (error) 308 goto error_1; 309 310 #if defined(__DragonFly__) 311 if (!vn_isdisk(devvp, &error)) 312 goto error_2; 313 #else 314 if (devvp->v_type != VBLK) { 315 error = ENOTBLK; 316 goto error_2; 317 } 318 if (devvp->v_umajor >= nblkdev) { 319 error = ENXIO; 320 goto error_2; 321 } 322 #endif 323 if (mp->mnt_flag & MNT_UPDATE) { 324 #if 0 325 /* 326 ******************** 327 * UPDATE 328 ******************** 329 */ 330 331 if (devvp != ntmp->um_devvp) 332 error = EINVAL; /* needs translation */ 333 else 334 vrele(devvp); 335 /* 336 * Update device name only on success 337 */ 338 if( !error) { 339 /* Save "mounted from" info for mount point (NULL pad)*/ 340 copyinstr( args.fspec, 341 mp->mnt_stat.f_mntfromname, 342 MNAMELEN - 1, 343 &size); 344 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 345 } 346 #endif 347 } else { 348 /* 349 ******************** 350 * NEW MOUNT 351 ******************** 352 */ 353 354 /* Save "mounted from" info for mount point (NULL pad)*/ 355 copyinstr( args.fspec, /* device name*/ 356 mp->mnt_stat.f_mntfromname, /* save area*/ 357 MNAMELEN - 1, /* max size*/ 358 &size); /* real size*/ 359 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 360 361 error = ntfs_mountfs(devvp, mp, &args, cred); 362 } 363 if (error) { 364 goto error_2; 365 } 366 367 #ifdef __DragonFly__ 368 dostatfs: 369 #endif 370 /* 371 * Initialize FS stat information in mount struct; uses 372 * mp->mnt_stat.f_mntfromname. 373 * 374 * This code is common to root and non-root mounts 375 */ 376 (void)VFS_STATFS(mp, &mp->mnt_stat, cred); 377 378 goto success; 379 380 381 error_2: /* error with devvp held*/ 382 383 /* release devvp before failing*/ 384 vrele(devvp); 385 386 error_1: /* no state to back out*/ 387 388 success: 389 return(error); 390 } 391 392 /* 393 * Common code for mount and mountroot 394 */ 395 int 396 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp, 397 struct ucred *cred) 398 { 399 struct buf *bp; 400 struct ntfsmount *ntmp; 401 cdev_t dev; 402 int error, ronly, ncount, i; 403 struct vnode *vp; 404 char cs_local[ICONV_CSNMAXLEN]; 405 char cs_ntfs[ICONV_CSNMAXLEN]; 406 407 /* 408 * Disallow multiple mounts of the same device. 409 * Disallow mounting of a device that is currently in use 410 * (except for root, which might share swap device for miniroot). 411 * Flush out any old buffers remaining from a previous use. 412 */ 413 error = vfs_mountedon(devvp); 414 if (error) 415 return (error); 416 ncount = vcount(devvp); 417 #if defined(__DragonFly__) 418 if (devvp->v_object) 419 ncount -= 1; 420 #endif 421 if (ncount > 1) 422 return (EBUSY); 423 #if defined(__DragonFly__) 424 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY); 425 error = vinvalbuf(devvp, V_SAVE, 0, 0); 426 VOP__UNLOCK(devvp, 0); 427 #else 428 error = vinvalbuf(devvp, V_SAVE, 0, 0); 429 #endif 430 if (error) 431 return (error); 432 433 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 434 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY); 435 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL); 436 VOP__UNLOCK(devvp, 0); 437 if (error) 438 return (error); 439 dev = devvp->v_rdev; 440 441 bp = NULL; 442 443 error = bread(devvp, BBLOCK, BBSIZE, &bp); 444 if (error) 445 goto out; 446 ntmp = kmalloc(sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO); 447 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) ); 448 brelse( bp ); 449 bp = NULL; 450 451 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) { 452 error = EINVAL; 453 dprintf(("ntfs_mountfs: invalid boot block\n")); 454 goto out; 455 } 456 457 { 458 int8_t cpr = ntmp->ntm_mftrecsz; 459 if( cpr > 0 ) 460 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr; 461 else 462 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps; 463 } 464 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n", 465 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media, 466 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec)); 467 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n", 468 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn)); 469 470 ntmp->ntm_mountp = mp; 471 ntmp->ntm_dev = dev; 472 ntmp->ntm_devvp = devvp; 473 ntmp->ntm_uid = argsp->uid; 474 ntmp->ntm_gid = argsp->gid; 475 ntmp->ntm_mode = argsp->mode; 476 ntmp->ntm_flag = argsp->flag; 477 478 if (argsp->flag & NTFS_MFLAG_KICONV && ntfs_iconv) { 479 bcopy(argsp->cs_local, cs_local, sizeof(cs_local)); 480 bcopy(argsp->cs_ntfs, cs_ntfs, sizeof(cs_ntfs)); 481 ntfs_82u_init(ntmp, cs_local, cs_ntfs); 482 ntfs_u28_init(ntmp, NULL, cs_local, cs_ntfs); 483 } else { 484 ntfs_82u_init(ntmp, NULL, NULL); 485 ntfs_u28_init(ntmp, ntmp->ntm_82u, NULL, NULL); 486 } 487 488 mp->mnt_data = (qaddr_t)ntmp; 489 490 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n", 491 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.", 492 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"", 493 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode)); 494 495 vfs_add_vnodeops(mp, &ntfs_vnode_vops, &mp->mnt_vn_norm_ops); 496 497 /* 498 * We read in some system nodes to do not allow 499 * reclaim them and to have everytime access to them. 500 */ 501 { 502 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO }; 503 for (i=0; i<3; i++) { 504 error = VFS_VGET(mp, NULL, 505 pi[i], &(ntmp->ntm_sysvn[pi[i]])); 506 if(error) 507 goto out1; 508 vsetflags(ntmp->ntm_sysvn[pi[i]], VSYSTEM); 509 vref(ntmp->ntm_sysvn[pi[i]]); 510 vput(ntmp->ntm_sysvn[pi[i]]); 511 } 512 } 513 514 /* read the Unicode lowercase --> uppercase translation table, 515 * if necessary */ 516 if ((error = ntfs_toupper_use(mp, ntmp))) 517 goto out1; 518 519 /* 520 * Scan $BitMap and count free clusters 521 */ 522 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree); 523 if(error) 524 goto out1; 525 526 /* 527 * Read and translate to internal format attribute 528 * definition file. 529 */ 530 { 531 int num,j; 532 struct attrdef ad; 533 534 /* Open $AttrDef */ 535 error = VFS_VGET(mp, NULL, NTFS_ATTRDEFINO, &vp); 536 if(error) 537 goto out1; 538 539 /* Count valid entries */ 540 for(num=0;;num++) { 541 error = ntfs_readattr(ntmp, VTONT(vp), 542 NTFS_A_DATA, NULL, 543 num * sizeof(ad), sizeof(ad), 544 &ad, NULL); 545 if (error) 546 goto out1; 547 if (ad.ad_name[0] == 0) 548 break; 549 } 550 551 /* Alloc memory for attribute definitions */ 552 ntmp->ntm_ad = kmalloc(num * sizeof(struct ntvattrdef), 553 M_NTFSMNT, M_WAITOK); 554 555 ntmp->ntm_adnum = num; 556 557 /* Read them and translate */ 558 for(i=0;i<num;i++){ 559 error = ntfs_readattr(ntmp, VTONT(vp), 560 NTFS_A_DATA, NULL, 561 i * sizeof(ad), sizeof(ad), 562 &ad, NULL); 563 if (error) 564 goto out1; 565 j = 0; 566 do { 567 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j]; 568 } while(ad.ad_name[j++]); 569 ntmp->ntm_ad[i].ad_namelen = j - 1; 570 ntmp->ntm_ad[i].ad_type = ad.ad_type; 571 } 572 573 vput(vp); 574 } 575 576 #if defined(__DragonFly__) 577 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 578 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 579 #else 580 mp->mnt_stat.f_fsid.val[0] = dev; 581 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS); 582 #endif 583 mp->mnt_maxsymlinklen = 0; 584 mp->mnt_flag |= MNT_LOCAL; 585 dev->si_mountpoint = mp; 586 587 return (0); 588 589 out1: 590 for(i=0;i<NTFS_SYSNODESNUM;i++) 591 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]); 592 593 if (vflush(mp, 0, 0)) 594 dprintf(("ntfs_mountfs: vflush failed\n")); 595 596 out: 597 dev->si_mountpoint = NULL; 598 if (bp) 599 brelse(bp); 600 601 #if defined __NetBSD__ 602 /* lock the device vnode before calling VOP_CLOSE() */ 603 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY); 604 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE); 605 VOP__UNLOCK(devvp, 0); 606 #else 607 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE); 608 #endif 609 610 return (error); 611 } 612 613 #if !defined(__DragonFly__) 614 static int 615 ntfs_start(struct mount *mp, int flags, struct thread *td) 616 { 617 return (0); 618 } 619 #endif 620 621 static int 622 ntfs_unmount(struct mount *mp, int mntflags) 623 { 624 struct ntfsmount *ntmp; 625 int error, ronly, flags, i; 626 627 dprintf(("ntfs_unmount: unmounting...\n")); 628 ntmp = VFSTONTFS(mp); 629 630 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 631 flags = 0; 632 if(mntflags & MNT_FORCE) 633 flags |= FORCECLOSE; 634 635 dprintf(("ntfs_unmount: vflushing...\n")); 636 error = vflush(mp, 0, flags | SKIPSYSTEM); 637 if (error) { 638 kprintf("ntfs_unmount: vflush failed: %d\n",error); 639 return (error); 640 } 641 642 /* Check if only system vnodes are left */ 643 for(i=0;i<NTFS_SYSNODESNUM;i++) 644 if((ntmp->ntm_sysvn[i]) && 645 (ntmp->ntm_sysvn[i]->v_sysref.refcnt > 1)) return (EBUSY); 646 647 /* Dereference all system vnodes */ 648 for(i=0;i<NTFS_SYSNODESNUM;i++) 649 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]); 650 651 /* vflush system vnodes */ 652 error = vflush(mp, 0, flags); 653 if (error) 654 kprintf("ntfs_unmount: vflush failed(sysnodes): %d\n",error); 655 656 /* Check if the type of device node isn't VBAD before 657 * touching v_cdevinfo. If the device vnode is revoked, the 658 * field is NULL and touching it causes null pointer derefercence. 659 */ 660 if (ntmp->ntm_devvp->v_type != VBAD) 661 ntmp->ntm_devvp->v_rdev->si_mountpoint = NULL; 662 663 vinvalbuf(ntmp->ntm_devvp, V_SAVE, 0, 0); 664 665 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE); 666 667 vrele(ntmp->ntm_devvp); 668 669 /* free the toupper table, if this has been last mounted ntfs volume */ 670 ntfs_toupper_unuse(); 671 672 dprintf(("ntfs_umount: freeing memory...\n")); 673 ntfs_u28_uninit(ntmp); 674 ntfs_82u_uninit(ntmp); 675 mp->mnt_data = (qaddr_t)0; 676 mp->mnt_flag &= ~MNT_LOCAL; 677 kfree(ntmp->ntm_ad, M_NTFSMNT); 678 kfree(ntmp, M_NTFSMNT); 679 return (error); 680 } 681 682 static int 683 ntfs_root(struct mount *mp, struct vnode **vpp) 684 { 685 struct vnode *nvp; 686 int error = 0; 687 688 dprintf(("ntfs_root(): sysvn: %p\n", 689 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO])); 690 error = VFS_VGET(mp, NULL, (ino_t)NTFS_ROOTINO, &nvp); 691 if(error) { 692 kprintf("ntfs_root: VFS_VGET failed: %d\n",error); 693 return (error); 694 } 695 696 *vpp = nvp; 697 return (0); 698 } 699 700 #if !defined(__DragonFly__) 701 static int 702 ntfs_quotactl(struct mount *mp, int cmds, uid_t uid, caddr_t arg, 703 struct thread *td) 704 { 705 kprintf("\nntfs_quotactl():\n"); 706 return EOPNOTSUPP; 707 } 708 #endif 709 710 int 711 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep) 712 { 713 struct vnode *vp; 714 u_int8_t *tmp; 715 int j, error; 716 long cfree = 0; 717 size_t bmsize, i; 718 719 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO]; 720 721 bmsize = VTOF(vp)->f_size; 722 723 tmp = kmalloc(bmsize, M_TEMP, M_WAITOK); 724 725 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL, 726 0, bmsize, tmp, NULL); 727 if (error) 728 goto out; 729 730 for(i=0;i<bmsize;i++) 731 for(j=0;j<8;j++) 732 if(~tmp[i] & (1 << j)) cfree++; 733 *cfreep = cfree; 734 735 out: 736 kfree(tmp, M_TEMP); 737 return(error); 738 } 739 740 static int 741 ntfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 742 { 743 struct ntfsmount *ntmp = VFSTONTFS(mp); 744 u_int64_t mftallocated; 745 746 dprintf(("ntfs_statfs():\n")); 747 748 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated; 749 750 #if defined(__DragonFly__) 751 sbp->f_type = mp->mnt_vfc->vfc_typenum; 752 #elif defined(__NetBSD__) 753 sbp->f_type = 0; 754 #else 755 sbp->f_type = MOUNT_NTFS; 756 #endif 757 sbp->f_bsize = ntmp->ntm_bps; 758 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc; 759 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv; 760 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree); 761 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec; 762 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) + 763 sbp->f_ffree; 764 if (sbp != &mp->mnt_stat) { 765 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 766 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 767 } 768 sbp->f_flags = mp->mnt_flag; 769 #ifdef __NetBSD__ 770 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN); 771 #endif 772 773 return (0); 774 } 775 776 static int 777 ntfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred) 778 { 779 struct ntfsmount *ntmp = VFSTONTFS(mp); 780 u_int64_t mftallocated; 781 782 dprintf(("ntfs_statvfs():\n")); 783 784 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated; 785 786 sbp->f_type = mp->mnt_vfc->vfc_typenum; 787 sbp->f_bsize = ntmp->ntm_bps; 788 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv; 789 sbp->f_bfree = sbp->f_bavail = ntmp->ntm_cfree * ntmp->ntm_spc; 790 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec; 791 sbp->f_files = mftallocated / (ntmp->ntm_bpmftrec * ntmp->ntm_bps) + 792 sbp->f_ffree; 793 794 return (0); 795 } 796 797 #if !defined(__DragonFly__) 798 static int 799 ntfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct thread *td) 800 { 801 /*dprintf(("ntfs_sync():\n"));*/ 802 return (0); 803 } 804 #endif 805 806 /*ARGSUSED*/ 807 static int 808 ntfs_fhtovp(struct mount *mp, struct vnode *rootvp, 809 struct fid *fhp, struct vnode **vpp) 810 { 811 struct vnode *nvp; 812 struct ntfid *ntfhp = (struct ntfid *)fhp; 813 int error; 814 815 ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino)); 816 817 if ((error = VFS_VGET(mp, NULL, ntfhp->ntfid_ino, &nvp)) != 0) { 818 *vpp = NULLVP; 819 return (error); 820 } 821 /* XXX as unlink/rmdir/mkdir/creat are not currently possible 822 * with NTFS, we don't need to check anything else for now */ 823 *vpp = nvp; 824 825 return (0); 826 } 827 828 static int 829 ntfs_vptofh(struct vnode *vp, struct fid *fhp) 830 { 831 struct ntnode *ntp; 832 struct ntfid *ntfhp; 833 834 ddprintf(("ntfs_fhtovp(): %p\n", vp)); 835 836 ntp = VTONT(vp); 837 ntfhp = (struct ntfid *)fhp; 838 ntfhp->ntfid_len = sizeof(struct ntfid); 839 ntfhp->ntfid_ino = ntp->i_number; 840 /* ntfhp->ntfid_gen = ntp->i_gen; */ 841 return (0); 842 } 843 844 int 845 ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype, char *attrname, 846 u_long lkflags, u_long flags, struct thread *td, 847 struct vnode **vpp) 848 { 849 int error; 850 struct ntfsmount *ntmp; 851 struct ntnode *ip; 852 struct fnode *fp; 853 struct vnode *vp; 854 enum vtype f_type; 855 856 dprintf(("ntfs_vgetex: ino: %ju, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n", 857 (uintmax_t) ino, attrtype, attrname?attrname:"", lkflags, flags)); 858 859 ntmp = VFSTONTFS(mp); 860 *vpp = NULL; 861 862 /* Get ntnode */ 863 error = ntfs_ntlookup(ntmp, ino, &ip); 864 if (error) { 865 kprintf("ntfs_vget: ntfs_ntget failed\n"); 866 return (error); 867 } 868 869 /* It may be not initialized fully, so force load it */ 870 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) { 871 error = ntfs_loadntnode(ntmp, ip); 872 if(error) { 873 kprintf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %"PRId64"\n", 874 ip->i_number); 875 ntfs_ntput(ip); 876 return (error); 877 } 878 } 879 880 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp); 881 if (error) { 882 kprintf("ntfs_vget: ntfs_fget failed\n"); 883 ntfs_ntput(ip); 884 return (error); 885 } 886 887 f_type = VINT; 888 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) { 889 if ((ip->i_frflag & NTFS_FRFLAG_DIR) && 890 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) { 891 f_type = VDIR; 892 } else if (flags & VG_EXT) { 893 f_type = VINT; 894 fp->f_size = fp->f_allocated = 0; 895 } else { 896 f_type = VREG; 897 898 error = ntfs_filesize(ntmp, fp, 899 &fp->f_size, &fp->f_allocated); 900 if (error) { 901 ntfs_ntput(ip); 902 return (error); 903 } 904 } 905 906 fp->f_flag |= FN_VALID; 907 } 908 909 if (FTOV(fp)) { 910 VGET(FTOV(fp), lkflags); 911 *vpp = FTOV(fp); 912 ntfs_ntput(ip); 913 return (0); 914 } 915 916 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, &vp, VLKTIMEOUT, 0); 917 if(error) { 918 ntfs_frele(fp); 919 ntfs_ntput(ip); 920 return (error); 921 } 922 dprintf(("ntfs_vget: vnode: %p for ntnode: %ju\n", vp, (uintmax_t)ino)); 923 924 fp->f_vp = vp; 925 vp->v_data = fp; 926 vp->v_type = f_type; 927 928 if (ino == NTFS_ROOTINO) 929 vsetflags(vp, VROOT); 930 931 /* 932 * Normal files use the buffer cache 933 */ 934 if (f_type == VREG) 935 vinitvmio(vp, fp->f_size, PAGE_SIZE, -1); 936 937 ntfs_ntput(ip); 938 939 KKASSERT(lkflags & LK_TYPE_MASK); 940 /* XXX leave vnode locked exclusively from getnewvnode */ 941 *vpp = vp; 942 return (0); 943 944 } 945 946 static int 947 ntfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp) 948 { 949 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, 950 LK_EXCLUSIVE | LK_RETRY, 0, curthread, vpp); 951 } 952 953 #if defined(__DragonFly__) 954 static struct vfsops ntfs_vfsops = { 955 .vfs_mount = ntfs_mount, 956 .vfs_unmount = ntfs_unmount, 957 .vfs_root = ntfs_root, 958 .vfs_statfs = ntfs_statfs, 959 .vfs_statvfs = ntfs_statvfs, 960 .vfs_sync = vfs_stdsync, 961 .vfs_vget = ntfs_vget, 962 .vfs_fhtovp = ntfs_fhtovp, 963 .vfs_checkexp = ntfs_checkexp, 964 .vfs_vptofh = ntfs_vptofh, 965 .vfs_init = ntfs_init, 966 .vfs_uninit = ntfs_nthash_uninit /* see ntfs_ihash.c */ 967 }; 968 VFS_SET(ntfs_vfsops, ntfs, 0); 969 MODULE_VERSION(ntfs, 1); 970 #elif defined(__NetBSD__) 971 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc; 972 973 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = { 974 &ntfs_vnodeop_opv_desc, 975 NULL, 976 }; 977 978 struct vfsops ntfs_vfsops = { 979 MOUNT_NTFS, 980 ntfs_mount, 981 ntfs_start, 982 ntfs_unmount, 983 ntfs_root, 984 ntfs_quotactl, 985 ntfs_statfs, 986 ntfs_sync, 987 ntfs_vget, 988 ntfs_fhtovp, 989 ntfs_vptofh, 990 ntfs_init, 991 ntfs_sysctl, 992 ntfs_mountroot, 993 ntfs_checkexp, 994 ntfs_vnodeopv_descs, 995 }; 996 #else /* !NetBSD && !FreeBSD */ 997 static struct vfsops ntfs_vfsops = { 998 ntfs_mount, 999 ntfs_start, 1000 ntfs_unmount, 1001 ntfs_root, 1002 ntfs_quotactl, 1003 ntfs_statfs, 1004 ntfs_sync, 1005 ntfs_vget, 1006 ntfs_fhtovp, 1007 ntfs_vptofh, 1008 ntfs_init, 1009 }; 1010 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0); 1011 #endif 1012 1013 1014