1 /* $NetBSD: ntfs_vfsops.c,v 1.2 1999/05/06 15:43:20 christos Exp $ */
4 * Copyright (c) 1998, 1999 Semen Ustimenko
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
37 #include <sys/kernel.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
45 #include <vm/vm_param.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_extern.h>
50 /*#define NTFS_DEBUG 1*/
51 #include <ntfs/ntfs.h>
52 #include <ntfs/ntfs_inode.h>
53 #include <ntfs/ntfs_subr.h>
54 #include <ntfs/ntfs_vfsops.h>
55 #include <ntfs/ntfs_ihash.h>
56 #include <ntfs/ntfs_extern.h>
57 #include <ntfs/ntfsmount.h>
59 #if defined(__FreeBSD__)
60 MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
61 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information");
62 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information");
63 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
66 #if defined(__FreeBSD__)
67 static int ntfs_mount __P((struct mount *, char *, caddr_t,
68 struct nameidata *, struct proc *));
70 static int ntfs_mount __P((struct mount *, const char *, void *,
71 struct nameidata *, struct proc *));
73 static int ntfs_root __P((struct mount *, struct vnode **));
74 static int ntfs_statfs __P((struct mount *, struct statfs *,
76 static int ntfs_unmount __P((struct mount *, int, struct proc *));
77 static int ntfs_vget __P((struct mount *mp, ino_t ino,
79 static int ntfs_mountfs __P((register struct vnode *, struct mount *,
80 struct ntfs_args *, struct proc *));
82 #if !defined(__FreeBSD__)
83 static int ntfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
85 static int ntfs_start __P((struct mount *, int, struct proc *));
86 static int ntfs_sync __P((struct mount *, int, struct ucred *,
88 static int ntfs_vptofh __P((struct vnode *, struct fid *));
89 #endif /* !defined(__FreeBSD__) */
91 #if defined(__FreeBSD__)
92 static int ntfs_init __P((struct vfsconf *));
93 #if 0 /* may be implemented at a later date */
94 static int ntfs_fhtovp __P((struct mount *, struct fid *,
96 static int ntfs_checkexp __P((struct vnode *, struct mbuf *,
97 int *, struct ucred **));
98 #endif /* 0, default ops in FreeBSD */
99 #elif defined(__NetBSD__)
100 static void ntfs_init __P((void));
101 static int ntfs_fhtovp __P((struct mount *, struct fid *,
103 static int ntfs_checkexp __P((struct mount *, struct mbuf *,
104 int *, struct ucred **));
105 static int ntfs_mountroot __P((void));
106 static int ntfs_sysctl __P((int *, u_int, void *, size_t *, void *,
107 size_t, struct proc *));
109 static int ntfs_init __P((void));
110 static int ntfs_fhtovp __P((struct mount *, struct fid *,
111 struct mbuf *, struct vnode **,
112 int *, struct ucred **));
118 ntfs_checkexp(mp, nam, exflagsp, credanonp)
119 register struct mount *mp;
122 struct ucred **credanonp;
130 ntfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
149 #if defined(__FreeBSD__)
152 struct vfsconf *vcp )
153 #elif defined(__NetBSD__)
162 #if !defined(__NetBSD__)
170 #if defined(__FreeBSD__)
177 struct nameidata *ndp,
183 struct ntfs_args args;
186 * Use NULL path to flag a root mount
191 * Mounting root file system
195 /* Get vnode for root device*/
196 if( bdevvp( rootdev, &rootvp))
197 panic("ffs_mountroot: can't setup bdevvp for root");
200 * FS specific handling
202 mp->mnt_flag |= MNT_RDONLY; /* XXX globally applicable?*/
207 if( ( err = ntfs_mountfs(rootvp, mp, &args, p)) != 0) {
208 /* fs specific cleanup (if any)*/
212 goto dostatfs; /* success*/
218 * Mounting non-root file system or updating a file system
222 /* copy in user arguments*/
223 err = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
225 goto error_1; /* can't get arguments*/
228 * If updating, check whether changing from read-only to
229 * read/write; if there is no device name, that's all we do.
231 if (mp->mnt_flag & MNT_UPDATE) {
232 printf("ntfs_mount(): MNT_UPDATE not supported\n");
240 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
242 if (mp->mnt_flag & MNT_FORCE)
248 err = ffs_flushfiles(mp, flags, p);
251 if (!err && (mp->mnt_flag & MNT_RELOAD))
252 err = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
256 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
258 if (mp->mnt_flag & MNT_FORCE) {
259 printf("WARNING: %s was not properly dismounted.\n",fs->fs_fsmnt);
261 printf("WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck.\n",
269 if (fs->fs_ronly == 0) {
271 ffs_sbupdate(ump, MNT_WAIT);
273 /* if not updating name...*/
274 if (args.fspec == 0) {
276 * Process export requests. Jumping to "success"
277 * will return the vfs_export() error code.
279 err = vfs_export(mp, &ump->um_export, &args.export);
286 * Not an update, or updating the name: look up the name
287 * and verify that it refers to a sensible block device.
289 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
292 /* can't get devvp!*/
298 if (!vn_isdisk(devvp)) {
302 if (mp->mnt_flag & MNT_UPDATE) {
310 if (devvp != ntmp->um_devvp)
311 err = EINVAL; /* needs translation */
315 * Update device name only on success
318 /* Save "mounted from" info for mount point (NULL pad)*/
319 copyinstr( args.fspec,
320 mp->mnt_stat.f_mntfromname,
323 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
334 * Since this is a new mount, we want the names for
335 * the device and the mount point copied in. If an
336 * error occurs, the mountpoint is discarded by the
339 /* Save "last mounted on" info for mount point (NULL pad)*/
340 copyinstr( path, /* mount point*/
341 mp->mnt_stat.f_mntonname, /* save area*/
342 MNAMELEN - 1, /* max size*/
343 &size); /* real size*/
344 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
346 /* Save "mounted from" info for mount point (NULL pad)*/
347 copyinstr( args.fspec, /* device name*/
348 mp->mnt_stat.f_mntfromname, /* save area*/
349 MNAMELEN - 1, /* max size*/
350 &size); /* real size*/
351 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
353 err = ntfs_mountfs(devvp, mp, &args, p);
361 * Initialize FS stat information in mount struct; uses both
362 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
364 * This code is common to root and non-root mounts
366 (void)VFS_STATFS(mp, &mp->mnt_stat, p);
371 error_2: /* error with devvp held*/
373 /* release devvp before failing*/
376 error_1: /* no state to back out*/
383 * Common code for mount and mountroot
386 ntfs_mountfs(devvp, mp, argsp, p)
387 register struct vnode *devvp;
389 struct ntfs_args *argsp;
393 struct ntfsmount *ntmp;
394 dev_t dev = devvp->v_rdev;
395 int error, ronly, ncount, i;
399 * Disallow multiple mounts of the same device.
400 * Disallow mounting of a device that is currently in use
401 * (except for root, which might share swap device for miniroot).
402 * Flush out any old buffers remaining from a previous use.
404 error = vfs_mountedon(devvp);
407 ncount = vcount(devvp);
408 #if defined(__FreeBSD__)
412 if (ncount > 1 && devvp != rootvp)
414 #if defined(__FreeBSD__)
415 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
416 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
417 VOP_UNLOCK(devvp, 0, p);
419 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
424 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
425 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
426 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
427 VOP_UNLOCK(devvp, 0, p);
433 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
436 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
437 bzero( ntmp, sizeof *ntmp );
438 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
442 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
444 printf("ntfs_mountfs: invalid boot block\n");
449 int8_t cpr = ntmp->ntm_mftrecsz;
451 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
453 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
455 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
456 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
457 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
458 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
459 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
461 ntmp->ntm_mountp = mp;
463 ntmp->ntm_devvp = devvp;
464 ntmp->ntm_uid = argsp->uid;
465 ntmp->ntm_gid = argsp->gid;
466 ntmp->ntm_mode = argsp->mode;
467 ntmp->ntm_flag = argsp->flag;
468 mp->mnt_data = (qaddr_t)ntmp;
470 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
471 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
472 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
473 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
476 * We read in some system nodes to do not allow
477 * reclaim them and to have everytime access to them.
480 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
481 for (i=0; i<3; i++) {
482 error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
485 ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
486 VREF(ntmp->ntm_sysvn[pi[i]]);
487 vput(ntmp->ntm_sysvn[pi[i]]);
492 * Read in WHOLE lowcase -> upcase translation
495 MALLOC(ntmp->ntm_upcase, wchar *, 65536 * sizeof(wchar),
496 M_NTFSMNT, M_WAITOK);
498 error = VFS_VGET(mp, NTFS_UPCASEINO, &vp);
501 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
502 0, 65536*sizeof(wchar), ntmp->ntm_upcase);
508 * Scan $BitMap and count free clusters
510 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
515 * Read and translate to internal format attribute
523 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
527 /* Count valid entries */
529 error = ntfs_readattr(ntmp, VTONT(vp),
531 num * sizeof(ad), sizeof(ad),
535 if (ad.ad_name[0] == 0)
539 /* Alloc memory for attribute definitions */
540 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
541 num * sizeof(struct ntvattrdef),
542 M_NTFSMNT, M_WAITOK);
544 ntmp->ntm_adnum = num;
546 /* Read them and translate */
548 error = ntfs_readattr(ntmp, VTONT(vp),
550 i * sizeof(ad), sizeof(ad),
556 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
557 } while(ad.ad_name[j++]);
558 ntmp->ntm_ad[i].ad_namelen = j - 1;
559 ntmp->ntm_ad[i].ad_type = ad.ad_type;
565 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
566 #if defined(__FreeBSD__)
567 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
569 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
571 mp->mnt_maxsymlinklen = 0;
572 mp->mnt_flag |= MNT_LOCAL;
573 #if defined(__FreeBSD__)
574 devvp->v_specmountpoint = mp;
576 devvp->v_specflags |= SI_MOUNTEDON;
581 for(i=0;i<NTFS_SYSNODESNUM;i++)
582 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
584 if (vflush(mp,NULLVP,0))
585 printf("ntfs_mountfs: vflush failed\n");
588 #if defined(__FreeBSD__)
589 devvp->v_specmountpoint = NULL;
591 devvp->v_specflags &= ~SI_MOUNTEDON;
595 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
599 #if !defined(__FreeBSD__)
608 #endif /* !defined(__FreeBSD__) */
616 register struct ntfsmount *ntmp;
617 int error, ronly = 0, flags, i;
619 dprintf(("ntfs_unmount: unmounting...\n"));
620 ntmp = VFSTONTFS(mp);
623 if(mntflags & MNT_FORCE)
626 dprintf(("ntfs_unmount: vflushing...\n"));
627 error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
629 printf("ntfs_unmount: vflush failed: %d\n",error);
633 /* Check if only system vnodes are rest */
634 for(i=0;i<NTFS_SYSNODESNUM;i++)
635 if((ntmp->ntm_sysvn[i]) &&
636 (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
638 /* Derefernce all system vnodes */
639 for(i=0;i<NTFS_SYSNODESNUM;i++)
640 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
642 /* vflush system vnodes */
643 error = vflush(mp,NULLVP,flags);
645 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
647 #if defined(__FreeBSD__)
648 ntmp->ntm_devvp->v_specmountpoint = NULL;
650 ntmp->ntm_devvp->v_specflags &= ~SI_MOUNTEDON;
653 vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0);
654 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
657 vrele(ntmp->ntm_devvp);
659 dprintf(("ntfs_umount: freeing memory...\n"));
660 mp->mnt_data = (qaddr_t)0;
661 mp->mnt_flag &= ~MNT_LOCAL;
662 FREE(ntmp->ntm_ad, M_NTFSMNT);
663 FREE(ntmp->ntm_upcase, M_NTFSMNT);
664 FREE(ntmp, M_NTFSMNT);
676 dprintf(("ntfs_root(): sysvn: %p\n",
677 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
678 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
680 printf("ntfs_root: VFS_VGET failed: %d\n",error);
688 #if !defined(__FreeBSD__)
697 printf("\nntfs_quotactl():\n");
700 #endif /* !defined(__FreeBSD__) */
704 struct ntfsmount *ntmp,
713 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
715 bmsize = VTOF(vp)->f_size;
717 MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
719 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
726 for(i=0;i<bmsize;i++)
728 if(~tmp[i] & (1 << j)) cfree++;
743 struct ntfsmount *ntmp = VFSTONTFS(mp);
744 u_int64_t mftsize,mftallocated;
746 dprintf(("ntfs_statfs():\n"));
748 mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
749 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
751 #if defined(__FreeBSD__)
752 sbp->f_type = mp->mnt_vfc->vfc_typenum;
753 #elif defined(__NetBSD__)
756 sbp->f_type = MOUNT_NTFS;
758 sbp->f_bsize = ntmp->ntm_bps;
759 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
760 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
761 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
762 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
763 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
765 if (sbp != &mp->mnt_stat) {
766 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
767 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
768 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
769 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
771 sbp->f_flags = mp->mnt_flag;
776 #if !defined(__FreeBSD__)
784 /*dprintf(("ntfs_sync():\n"));*/
787 #endif /* !defined(__FreeBSD__) */
789 #if !defined(__FreeBSD__)
793 #if defined(__FreeBSD__)
796 struct sockaddr *nam,
799 struct ucred **credanonp)
800 #elif defined(__NetBSD__)
810 struct ucred **credanonp)
813 printf("\ntfs_fhtovp():\n");
822 printf("ntfs_vptofh():\n");
825 #endif /* !defined(__FreeBSD__) */
839 register struct ntfsmount *ntmp;
844 dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%x, f: 0x%x\n",
845 ino, attrtype, attrname?attrname:"", lkflags, flags ));
847 ntmp = VFSTONTFS(mp);
851 error = ntfs_ntlookup(ntmp, ino, &ip);
853 printf("ntfs_vget: ntfs_ntget failed\n");
857 /* It may be not initialized fully, so force load it */
858 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
859 error = ntfs_loadntnode(ntmp, ip);
861 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
868 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
870 printf("ntfs_vget: ntfs_fget failed\n");
875 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
876 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
877 (fp->f_attrtype == 0x80 && fp->f_attrname == NULL)) {
879 } else if(flags & VG_EXT) {
882 fp->f_size =fp->f_allocated = 0;
886 error = ntfs_filesize(ntmp, fp,
887 &fp->f_size, &fp->f_allocated);
894 fp->f_flag |= FN_VALID;
898 VGET(FTOV(fp), lkflags, p);
904 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
910 dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
912 lockinit(&fp->f_lock, PINOD, "fnode", 0, 0);
915 vp->v_type = fp->f_type;
917 if (ino == NTFS_ROOTINO)
922 if (lkflags & LK_TYPE_MASK) {
923 error = VN_LOCK(vp, lkflags, p);
942 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
943 LK_EXCLUSIVE, 0, curproc, vpp);
946 #if defined(__FreeBSD__)
947 static struct vfsops ntfs_vfsops = {
962 VFS_SET(ntfs_vfsops, ntfs, 0);
963 #elif defined(__NetBSD__)
964 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
966 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
967 &ntfs_vnodeop_opv_desc,
971 struct vfsops ntfs_vfsops = {
990 static struct vfsops ntfs_vfsops = {
1003 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);