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 *,
96 struct sockaddr *, struct vnode **,
97 int *, struct ucred **));
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 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
431 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
434 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
435 bzero( ntmp, sizeof *ntmp );
436 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
440 if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
442 printf("ntfs_mountfs: invalid boot block\n");
447 int8_t cpr = ntmp->ntm_mftrecsz;
449 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
451 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
453 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
454 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
455 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
456 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
457 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
459 ntmp->ntm_mountp = mp;
461 ntmp->ntm_devvp = devvp;
462 ntmp->ntm_uid = argsp->uid;
463 ntmp->ntm_gid = argsp->gid;
464 ntmp->ntm_mode = argsp->mode;
465 ntmp->ntm_flag = argsp->flag;
466 mp->mnt_data = (qaddr_t)ntmp;
468 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
469 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
470 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
471 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
474 * We read in some system nodes to do not allow
475 * reclaim them and to have everytime access to them.
478 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
479 for (i=0; i<3; i++) {
480 error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
483 ntmp->ntm_sysvn[pi[i]]->v_flag |= VSYSTEM;
484 VREF(ntmp->ntm_sysvn[pi[i]]);
485 vput(ntmp->ntm_sysvn[pi[i]]);
490 * Read in WHOLE lowcase -> upcase translation
493 MALLOC(ntmp->ntm_upcase, wchar *, 65536 * sizeof(wchar),
494 M_NTFSMNT, M_WAITOK);
496 error = VFS_VGET(mp, NTFS_UPCASEINO, &vp);
499 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
500 0, 65536*sizeof(wchar), ntmp->ntm_upcase);
506 * Scan $BitMap and count free clusters
508 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
513 * Read and translate to internal format attribute
521 error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
525 /* Count valid entries */
527 error = ntfs_readattr(ntmp, VTONT(vp),
529 num * sizeof(ad), sizeof(ad),
533 if (ad.ad_name[0] == 0)
537 /* Alloc memory for attribute definitions */
538 MALLOC(ntmp->ntm_ad, struct ntvattrdef *,
539 num * sizeof(struct ntvattrdef),
540 M_NTFSMNT, M_WAITOK);
542 ntmp->ntm_adnum = num;
544 /* Read them and translate */
546 error = ntfs_readattr(ntmp, VTONT(vp),
548 i * sizeof(ad), sizeof(ad),
554 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
555 } while(ad.ad_name[j++]);
556 ntmp->ntm_ad[i].ad_namelen = j - 1;
557 ntmp->ntm_ad[i].ad_type = ad.ad_type;
563 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
564 #if defined(__FreeBSD__)
565 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
567 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_NTFS);
569 mp->mnt_maxsymlinklen = 0;
570 mp->mnt_flag |= MNT_LOCAL;
571 #if defined(__FreeBSD__)
572 devvp->v_specmountpoint = mp;
574 devvp->v_specflags |= SI_MOUNTEDON;
579 for(i=0;i<NTFS_SYSNODESNUM;i++)
580 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
582 if (vflush(mp,NULLVP,0))
583 printf("ntfs_mountfs: vflush failed\n");
586 #if defined(__FreeBSD__)
587 devvp->v_specmountpoint = NULL;
589 devvp->v_specflags &= ~SI_MOUNTEDON;
593 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
597 #if !defined(__FreeBSD__)
606 #endif /* !defined(__FreeBSD__) */
614 register struct ntfsmount *ntmp;
615 int error, ronly = 0, flags, i;
617 dprintf(("ntfs_unmount: unmounting...\n"));
618 ntmp = VFSTONTFS(mp);
621 if(mntflags & MNT_FORCE)
624 dprintf(("ntfs_unmount: vflushing...\n"));
625 error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
627 printf("ntfs_unmount: vflush failed: %d\n",error);
631 /* Check if only system vnodes are rest */
632 for(i=0;i<NTFS_SYSNODESNUM;i++)
633 if((ntmp->ntm_sysvn[i]) &&
634 (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
636 /* Derefernce all system vnodes */
637 for(i=0;i<NTFS_SYSNODESNUM;i++)
638 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
640 /* vflush system vnodes */
641 error = vflush(mp,NULLVP,flags);
643 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
645 #if defined(__FreeBSD__)
646 ntmp->ntm_devvp->v_specmountpoint = NULL;
648 ntmp->ntm_devvp->v_specflags &= ~SI_MOUNTEDON;
651 vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, p, 0, 0);
652 error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
655 vrele(ntmp->ntm_devvp);
657 dprintf(("ntfs_umount: freeing memory...\n"));
658 mp->mnt_data = (qaddr_t)0;
659 mp->mnt_flag &= ~MNT_LOCAL;
660 FREE(ntmp->ntm_ad, M_NTFSMNT);
661 FREE(ntmp->ntm_upcase, M_NTFSMNT);
662 FREE(ntmp, M_NTFSMNT);
674 dprintf(("ntfs_root(): sysvn: %p\n",
675 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
676 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
678 printf("ntfs_root: VFS_VGET failed: %d\n",error);
686 #if !defined(__FreeBSD__)
695 printf("\nntfs_quotactl():\n");
698 #endif /* !defined(__FreeBSD__) */
702 struct ntfsmount *ntmp,
711 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
713 bmsize = VTOF(vp)->f_size;
715 MALLOC(tmp, u_int8_t *, bmsize, M_TEMP, M_WAITOK);
717 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
724 for(i=0;i<bmsize;i++)
726 if(~tmp[i] & (1 << j)) cfree++;
741 struct ntfsmount *ntmp = VFSTONTFS(mp);
742 u_int64_t mftsize,mftallocated;
744 dprintf(("ntfs_statfs():\n"));
746 mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
747 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
749 #if defined(__FreeBSD__)
750 sbp->f_type = mp->mnt_vfc->vfc_typenum;
751 #elif defined(__NetBSD__)
754 sbp->f_type = MOUNT_NTFS;
756 sbp->f_bsize = ntmp->ntm_bps;
757 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
758 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
759 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
760 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
761 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
763 if (sbp != &mp->mnt_stat) {
764 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
765 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
766 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
767 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
769 sbp->f_flags = mp->mnt_flag;
774 #if !defined(__FreeBSD__)
782 /*dprintf(("ntfs_sync():\n"));*/
785 #endif /* !defined(__FreeBSD__) */
787 #if !defined(__FreeBSD__)
791 #if defined(__FreeBSD__)
794 struct sockaddr *nam,
797 struct ucred **credanonp)
798 #elif defined(__NetBSD__)
808 struct ucred **credanonp)
811 printf("\ntfs_fhtovp():\n");
820 printf("ntfs_vptofh():\n");
823 #endif /* !defined(__FreeBSD__) */
837 register struct ntfsmount *ntmp;
842 dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%x, f: 0x%x\n",
843 ino, attrtype, attrname?attrname:"", lkflags, flags ));
845 ntmp = VFSTONTFS(mp);
849 error = ntfs_ntlookup(ntmp, ino, &ip);
851 printf("ntfs_vget: ntfs_ntget failed\n");
855 /* It may be not initialized fully, so force load it */
856 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
857 error = ntfs_loadntnode(ntmp, ip);
859 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
866 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
868 printf("ntfs_vget: ntfs_fget failed\n");
873 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
874 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
875 (fp->f_attrtype == 0x80 && fp->f_attrname == NULL)) {
877 } else if(flags & VG_EXT) {
880 fp->f_size =fp->f_allocated = 0;
884 error = ntfs_filesize(ntmp, fp,
885 &fp->f_size, &fp->f_allocated);
892 fp->f_flag |= FN_VALID;
896 VGET(FTOV(fp), lkflags, p);
902 error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
908 dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
910 lockinit(&fp->f_lock, PINOD, "fnode", 0, 0);
913 vp->v_type = fp->f_type;
915 if (ino == NTFS_ROOTINO)
920 if (lkflags & LK_TYPE_MASK) {
921 error = VN_LOCK(vp, lkflags, p);
940 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
941 LK_EXCLUSIVE, 0, curproc, vpp);
944 #if defined(__FreeBSD__)
945 static struct vfsops ntfs_vfsops = {
959 VFS_SET(ntfs_vfsops, ntfs, 0);
960 #elif defined(__NetBSD__)
961 extern struct vnodeopv_desc ntfs_vnodeop_opv_desc;
963 struct vnodeopv_desc *ntfs_vnodeopv_descs[] = {
964 &ntfs_vnodeop_opv_desc,
968 struct vfsops ntfs_vfsops = {
987 static struct vfsops ntfs_vfsops = {
1000 VFS_SET(ntfs_vfsops, ntfs, MOUNT_NTFS, 0);