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_prot.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_extern.h>
51 /*#define NTFS_DEBUG 1*/
52 #include <ntfs/ntfs.h>
53 #include <ntfs/ntfs_inode.h>
54 #include <ntfs/ntfs_subr.h>
55 #include <ntfs/ntfs_vfsops.h>
56 #include <ntfs/ntfs_ihash.h>
57 #include <ntfs/ntfs_extern.h>
58 #include <ntfs/ntfsmount.h>
60 #if defined(__FreeBSD__)
61 MALLOC_DEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
62 MALLOC_DEFINE(M_NTFSNTNODE,"NTFS ntnode", "NTFS ntnode information");
63 MALLOC_DEFINE(M_NTFSFNODE,"NTFS fnode", "NTFS fnode information");
64 MALLOC_DEFINE(M_NTFSDIR,"NTFS dir", "NTFS dir buffer");
67 #if defined(__FreeBSD__)
68 static int ntfs_mount __P((struct mount *, char *, caddr_t,
69 struct nameidata *, struct proc *));
71 static int ntfs_mount __P((struct mount *, const char *, void *,
72 struct nameidata *, struct proc *));
74 static int ntfs_root __P((struct mount *, struct vnode **));
75 static int ntfs_statfs __P((struct mount *, struct statfs *,
77 static int ntfs_unmount __P((struct mount *, int, struct proc *));
78 static int ntfs_vget __P((struct mount *mp, ino_t ino,
80 static int ntfs_mountfs __P((register struct vnode *, struct mount *,
81 struct ntfs_args *, struct proc *));
83 #if !defined(__FreeBSD__)
84 static int ntfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
86 static int ntfs_start __P((struct mount *, int, struct proc *));
87 static int ntfs_sync __P((struct mount *, int, struct ucred *,
89 static int ntfs_vptofh __P((struct vnode *, struct fid *));
90 #endif /* !defined(__FreeBSD__) */
92 #if defined(__FreeBSD__)
93 static int ntfs_init __P((struct vfsconf *));
94 #if 0 /* may be implemented at a later date */
95 static int ntfs_fhtovp __P((struct mount *, struct fid *,
97 static int ntfs_checkexp __P((struct vnode *, struct mbuf *,
98 int *, struct ucred **));
99 #endif /* 0, default ops in FreeBSD */
100 #elif defined(__NetBSD__)
101 static void ntfs_init __P((void));
102 static int ntfs_fhtovp __P((struct mount *, struct fid *,
104 static int ntfs_checkexp __P((struct mount *, struct mbuf *,
105 int *, struct ucred **));
106 static int ntfs_mountroot __P((void));
107 static int ntfs_sysctl __P((int *, u_int, void *, size_t *, void *,
108 size_t, struct proc *));
110 static int ntfs_init __P((void));
111 static int ntfs_fhtovp __P((struct mount *, struct fid *,
112 struct mbuf *, struct vnode **,
113 int *, struct ucred **));
119 ntfs_checkexp(mp, nam, exflagsp, credanonp)
120 register struct mount *mp;
123 struct ucred **credanonp;
131 ntfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
150 #if defined(__FreeBSD__)
153 struct vfsconf *vcp )
154 #elif defined(__NetBSD__)
163 #if !defined(__NetBSD__)
171 #if defined(__FreeBSD__)
178 struct nameidata *ndp,
184 struct ntfs_args args;
187 * Use NULL path to flag a root mount
192 * Mounting root file system
196 /* Get vnode for root device*/
197 if( bdevvp( rootdev, &rootvp))
198 panic("ffs_mountroot: can't setup bdevvp for root");
201 * FS specific handling
203 mp->mnt_flag |= MNT_RDONLY; /* XXX globally applicable?*/
208 if( ( err = ntfs_mountfs(rootvp, mp, &args, p)) != 0) {
209 /* fs specific cleanup (if any)*/
213 goto dostatfs; /* success*/
219 * Mounting non-root file system or updating a file system
223 /* copy in user arguments*/
224 err = copyin(data, (caddr_t)&args, sizeof (struct ntfs_args));
226 goto error_1; /* can't get arguments*/
229 * If updating, check whether changing from read-only to
230 * read/write; if there is no device name, that's all we do.
232 if (mp->mnt_flag & MNT_UPDATE) {
233 printf("ntfs_mount(): MNT_UPDATE not supported\n");
241 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
243 if (mp->mnt_flag & MNT_FORCE)
249 err = ffs_flushfiles(mp, flags, p);
252 if (!err && (mp->mnt_flag & MNT_RELOAD))
253 err = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
257 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
259 if (mp->mnt_flag & MNT_FORCE) {
260 printf("WARNING: %s was not properly dismounted.\n",fs->fs_fsmnt);
262 printf("WARNING: R/W mount of %s denied. Filesystem is not clean - run fsck.\n",
270 if (fs->fs_ronly == 0) {
272 ffs_sbupdate(ump, MNT_WAIT);
274 /* if not updating name...*/
275 if (args.fspec == 0) {
277 * Process export requests. Jumping to "success"
278 * will return the vfs_export() error code.
280 err = vfs_export(mp, &ump->um_export, &args.export);
287 * Not an update, or updating the name: look up the name
288 * and verify that it refers to a sensible block device.
290 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
293 /* can't get devvp!*/
299 if (!vn_isdisk(devvp)) {
303 if (mp->mnt_flag & MNT_UPDATE) {
311 if (devvp != ntmp->um_devvp)
312 err = EINVAL; /* needs translation */
316 * Update device name only on success
319 /* Save "mounted from" info for mount point (NULL pad)*/
320 copyinstr( args.fspec,
321 mp->mnt_stat.f_mntfromname,
324 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
335 * Since this is a new mount, we want the names for
336 * the device and the mount point copied in. If an
337 * error occurs, the mountpoint is discarded by the
340 /* Save "last mounted on" info for mount point (NULL pad)*/
341 copyinstr( path, /* mount point*/
342 mp->mnt_stat.f_mntonname, /* save area*/
343 MNAMELEN - 1, /* max size*/
344 &size); /* real size*/
345 bzero( mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
347 /* Save "mounted from" info for mount point (NULL pad)*/
348 copyinstr( args.fspec, /* device name*/
349 mp->mnt_stat.f_mntfromname, /* save area*/
350 MNAMELEN - 1, /* max size*/
351 &size); /* real size*/
352 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
354 err = ntfs_mountfs(devvp, mp, &args, p);
362 * Initialize FS stat information in mount struct; uses both
363 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
365 * This code is common to root and non-root mounts
367 (void)VFS_STATFS(mp, &mp->mnt_stat, p);
372 error_2: /* error with devvp held*/
374 /* release devvp before failing*/
377 error_1: /* no state to back out*/
384 * Common code for mount and mountroot
387 ntfs_mountfs(devvp, mp, argsp, p)
388 register struct vnode *devvp;
390 struct ntfs_args *argsp;
394 struct ntfsmount *ntmp;
395 dev_t dev = devvp->v_rdev;
396 int error, ronly, ncount, i;
400 * Disallow multiple mounts of the same device.
401 * Disallow mounting of a device that is currently in use
402 * (except for root, which might share swap device for miniroot).
403 * Flush out any old buffers remaining from a previous use.
405 error = vfs_mountedon(devvp);
408 ncount = vcount(devvp);
409 #if defined(__FreeBSD__)
413 if (ncount > 1 && devvp != rootvp)
415 #if defined(__FreeBSD__)
416 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
417 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
418 VOP_UNLOCK(devvp, 0, p);
420 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
425 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
426 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
432 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
435 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
436 bzero( ntmp, sizeof *ntmp );
437 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
441 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
443 printf("ntfs_mountfs: invalid boot block\n");
448 int8_t cpr = ntmp->ntm_mftrecsz;
450 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
452 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
454 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
455 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
456 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
457 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
458 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
460 ntmp->ntm_mountp = mp;
462 ntmp->ntm_devvp = devvp;
463 ntmp->ntm_uid = argsp->uid;
464 ntmp->ntm_gid = argsp->gid;
465 ntmp->ntm_mode = argsp->mode;
466 ntmp->ntm_flag = argsp->flag;
467 mp->mnt_data = (qaddr_t)ntmp;
469 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
470 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
471 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
472 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
475 * We read in some system nodes to do not allow
476 * reclaim them and to have everytime access to them.
479 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
480 for (i=0; i<3; i++) {
481 error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
484 ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
485 VREF(ntmp->ntm_sysvn[pi[i]]);
486 vput(ntmp->ntm_sysvn[pi[i]]);
491 * Read in WHOLE lowcase -> upcase translation
494 MALLOC(ntmp->ntm_upcase, wchar *, 65536 * sizeof(wchar),
495 M_NTFSMNT, M_WAITOK);
497 error = VFS_VGET(mp, NTFS_UPCASEINO, &vp);
500 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
501 0, 65536*sizeof(wchar), ntmp->ntm_upcase);
507 * Scan $BitMap and count free clusters
509 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
514 * Read and translate to internal format attribute
522 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
526 /* Count valid entries */
528 error = ntfs_readattr(ntmp, VTONT(vp),
530 num * sizeof(ad), sizeof(ad),
534 if (ad.ad_name[0] == 0)
538 /* Alloc memory for attribute definitions */
539 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
540 num * sizeof(struct ntvattrdef),
541 M_NTFSMNT, M_WAITOK);
543 ntmp->ntm_adnum = num;
545 /* Read them and translate */
547 error = ntfs_readattr(ntmp, VTONT(vp),
549 i * sizeof(ad), sizeof(ad),
555 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
556 } while(ad.ad_name[j++]);
557 ntmp->ntm_ad[i].ad_namelen = j - 1;
558 ntmp->ntm_ad[i].ad_type = ad.ad_type;
564 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
565 #if defined(__FreeBSD__)
566 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
568 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
570 mp->mnt_maxsymlinklen = 0;
571 mp->mnt_flag |= MNT_LOCAL;
572 #if defined(__FreeBSD__)
573 devvp->v_specmountpoint = mp;
575 devvp->v_specflags |= SI_MOUNTEDON;
580 for(i=0;i<NTFS_SYSNODESNUM;i++)
581 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
583 if (vflush(mp,NULLVP,0))
584 printf("ntfs_mountfs: vflush failed\n");
587 #if defined(__FreeBSD__)
588 devvp->v_specmountpoint = NULL;
590 devvp->v_specflags &= ~SI_MOUNTEDON;
594 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
598 #if !defined(__FreeBSD__)
607 #endif /* !defined(__FreeBSD__) */
615 register struct ntfsmount *ntmp;
616 int error, ronly = 0, flags, i;
618 dprintf(("ntfs_unmount: unmounting...\n"));
619 ntmp = VFSTONTFS(mp);
622 if(mntflags & MNT_FORCE)
625 dprintf(("ntfs_unmount: vflushing...\n"));
626 error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
628 printf("ntfs_unmount: vflush failed: %d\n",error);
632 /* Check if only system vnodes are rest */
633 for(i=0;i<NTFS_SYSNODESNUM;i++)
634 if((ntmp->ntm_sysvn[i]) &&
635 (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
637 /* Derefernce all system vnodes */
638 for(i=0;i<NTFS_SYSNODESNUM;i++)
639 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
641 /* vflush system vnodes */
642 error = vflush(mp,NULLVP,flags);
644 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
646 #if defined(__FreeBSD__)
647 ntmp->ntm_devvp->v_specmountpoint = NULL;
649 ntmp->ntm_devvp->v_specflags &= ~SI_MOUNTEDON;
652 vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0);
653 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
656 vrele(ntmp->ntm_devvp);
658 dprintf(("ntfs_umount: freeing memory...\n"));
659 mp->mnt_data = (qaddr_t)0;
660 mp->mnt_flag &= ~MNT_LOCAL;
661 FREE(ntmp->ntm_ad, M_NTFSMNT);
662 FREE(ntmp->ntm_upcase, M_NTFSMNT);
663 FREE(ntmp, M_NTFSMNT);
675 dprintf(("ntfs_root(): sysvn: %p\n",
676 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
677 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
679 printf("ntfs_root: VFS_VGET failed: %d\n",error);
687 #if !defined(__FreeBSD__)
696 printf("\nntfs_quotactl():\n");
699 #endif /* !defined(__FreeBSD__) */
703 struct ntfsmount *ntmp,
712 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
714 bmsize = VTOF(vp)->f_size;
716 MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
718 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
725 for(i=0;i<bmsize;i++)
727 if(~tmp[i] & (1 << j)) cfree++;
742 struct ntfsmount *ntmp = VFSTONTFS(mp);
743 u_int64_t mftsize,mftallocated;
745 dprintf(("ntfs_statfs():\n"));
747 mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
748 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
750 #if defined(__FreeBSD__)
751 sbp->f_type = mp->mnt_vfc->vfc_typenum;
752 #elif defined(__NetBSD__)
755 sbp->f_type = MOUNT_NTFS;
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) +
764 if (sbp != &mp->mnt_stat) {
765 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
766 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
767 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
768 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
770 sbp->f_flags = mp->mnt_flag;
775 #if !defined(__FreeBSD__)
783 /*dprintf(("ntfs_sync():\n"));*/
786 #endif /* !defined(__FreeBSD__) */
788 #if !defined(__FreeBSD__)
792 #if defined(__FreeBSD__)
795 struct sockaddr *nam,
798 struct ucred **credanonp)
799 #elif defined(__NetBSD__)
809 struct ucred **credanonp)
812 printf("\ntfs_fhtovp():\n");
821 printf("ntfs_vptofh():\n");
824 #endif /* !defined(__FreeBSD__) */
838 register struct ntfsmount *ntmp;
843 dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%x, f: 0x%x\n",
844 ino, attrtype, attrname?attrname:"", lkflags, flags ));
846 ntmp = VFSTONTFS(mp);
850 error = ntfs_ntlookup(ntmp, ino, &ip);
852 printf("ntfs_vget: ntfs_ntget failed\n");
856 /* It may be not initialized fully, so force load it */
857 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
858 error = ntfs_loadntnode(ntmp, ip);
860 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
867 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
869 printf("ntfs_vget: ntfs_fget failed\n");
874 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
875 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
876 (fp->f_attrtype == 0x80 && fp->f_attrname == NULL)) {
878 } else if(flags & VG_EXT) {
881 fp->f_size =fp->f_allocated = 0;
885 error = ntfs_filesize(ntmp, fp,
886 &fp->f_size, &fp->f_allocated);
893 fp->f_flag |= FN_VALID;
897 VGET(FTOV(fp), lkflags, p);
903 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
909 dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
911 lockinit(&fp->f_lock, PINOD, "fnode", 0, 0);
914 vp->v_type = fp->f_type;
916 if (ino == NTFS_ROOTINO)
921 if (lkflags & LK_TYPE_MASK) {
922 error = VN_LOCK(vp, lkflags, p);
941 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
942 LK_EXCLUSIVE, 0, curproc, vpp);
945 #if defined(__FreeBSD__)
946 static struct vfsops ntfs_vfsops = {
961 VFS_SET(ntfs_vfsops, ntfs, 0);
962 #elif defined(__NetBSD__)
963 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
965 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
966 &ntfs_vnodeop_opv_desc,
970 struct vfsops ntfs_vfsops = {
989 static struct vfsops ntfs_vfsops = {
1002 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);