1 /* $NetBSD: ntfs_vfsops.c,v 1.23 1999/11/15 19:38:14 jdolecek 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>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
45 #include <sys/systm.h>
47 #include <geom/geom.h>
48 #include <geom/geom_vfs.h>
51 #include <vm/vm_param.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_extern.h>
56 /*#define NTFS_DEBUG 1*/
57 #include <fs/ntfs/ntfs.h>
58 #include <fs/ntfs/ntfs_inode.h>
59 #include <fs/ntfs/ntfs_subr.h>
60 #include <fs/ntfs/ntfs_vfsops.h>
61 #include <fs/ntfs/ntfs_ihash.h>
62 #include <fs/ntfs/ntfsmount.h>
64 static MALLOC_DEFINE(M_NTFSMNT, "ntfs_mount", "NTFS mount structure");
65 MALLOC_DEFINE(M_NTFSNTNODE,"ntfs_ntnode", "NTFS ntnode information");
66 MALLOC_DEFINE(M_NTFSFNODE,"ntfs_fnode", "NTFS fnode information");
67 MALLOC_DEFINE(M_NTFSDIR,"ntfs_dir", "NTFS dir buffer");
71 static int ntfs_mountfs(register struct vnode *, struct mount *,
73 static int ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep);
75 static vfs_init_t ntfs_init;
76 static vfs_uninit_t ntfs_uninit;
77 static vfs_vget_t ntfs_vget;
78 static vfs_fhtovp_t ntfs_fhtovp;
79 static vfs_cmount_t ntfs_cmount;
80 static vfs_mount_t ntfs_mount;
81 static vfs_root_t ntfs_root;
82 static vfs_statfs_t ntfs_statfs;
83 static vfs_unmount_t ntfs_unmount;
85 static b_strategy_t ntfs_bufstrategy;
88 * Buffer operations for NTFS vnodes.
89 * We punt on VOP_BMAP, so we need to do
90 * strategy on the file's vnode rather
91 * than the underlying device's
93 static struct buf_ops ntfs_vnbufops = {
95 .bop_strategy = ntfs_bufstrategy,
100 struct vfsconf *vcp )
109 struct vfsconf *vcp )
111 ntfs_toupper_destroy();
112 ntfs_nthashdestroy();
122 struct ntfs_args args;
123 struct export_args exp;
126 error = copyin(data, &args, sizeof(args));
129 vfs_oexport_conv(&args.export, &exp);
130 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
131 ma = mount_arg(ma, "export", &exp, sizeof(exp));
132 ma = mount_argf(ma, "uid", "%d", args.uid);
133 ma = mount_argf(ma, "gid", "%d", args.gid);
134 ma = mount_argf(ma, "mode", "%d", args.mode);
135 ma = mount_argb(ma, args.flag & NTFS_MFLAG_CASEINS, "nocaseins");
136 ma = mount_argb(ma, args.flag & NTFS_MFLAG_ALLNAMES, "noallnames");
137 if (args.flag & NTFS_MFLAG_KICONV) {
138 ma = mount_argsu(ma, "cs_ntfs", args.cs_ntfs, 64);
139 ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
142 error = kernel_mount(ma, flags);
147 static const char *ntfs_opts[] = {
148 "from", "export", "uid", "gid", "mode", "caseins", "allnames",
149 "kiconv", "cs_ntfs", "cs_local", NULL
153 ntfs_mount (struct mount *mp)
157 struct nameidata ndp;
160 if (vfs_filteropt(mp->mnt_optnew, ntfs_opts))
163 from = vfs_getopts(mp->mnt_optnew, "from", &error);
168 * If updating, check whether changing from read-only to
171 if (mp->mnt_flag & MNT_UPDATE) {
172 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
173 /* Process export requests in vfs_mount.c */
176 printf("ntfs_mount(): MNT_UPDATE not supported\n");
183 * Not an update, or updating the name: look up the name
184 * and verify that it refers to a sensible block device.
186 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, curthread);
189 /* can't get devvp!*/
192 NDFREE(&ndp, NDF_ONLY_PNBUF);
195 if (!vn_isdisk(devvp, &err)) {
200 if (mp->mnt_flag & MNT_UPDATE) {
208 if (devvp != ntmp->um_devvp)
209 err = EINVAL; /* needs translation */
222 * Since this is a new mount, we want the names for
223 * the device and the mount point copied in. If an
224 * error occurs, the mountpoint is discarded by the
225 * upper level code. Note that vfs_mount() handles
226 * copying the mountpoint f_mntonname for us, so we
227 * don't have to do it here unless we want to set it
228 * to something other than "path" for some rason.
230 /* Save "mounted from" info for mount point (NULL pad)*/
231 vfs_mountedfrom(mp, from);
233 err = ntfs_mountfs(devvp, mp, curthread);
242 error_1: /* no state to back out*/
243 /* XXX: missing NDFREE(&ndp, ...) */
250 * Common code for mount and mountroot
253 ntfs_mountfs(devvp, mp, td)
254 register struct vnode *devvp;
259 struct ntfsmount *ntmp;
260 struct cdev *dev = devvp->v_rdev;
261 int error, ronly, i, v;
263 struct g_consumer *cp;
264 struct g_provider *pp;
265 char *cs_ntfs, *cs_local;
267 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
272 * XXX: Do not allow more than one consumer to open a device
273 * associated with a particular GEOM provider.
274 * This disables multiple read-only mounts of a device,
275 * but it gets rid of panics in vget() when you try to
276 * mount the same device more than once.
278 pp = g_dev_getprovider(devvp->v_rdev);
279 if ((pp != NULL) && ((pp->acr | pp->acw | pp->ace ) != 0))
282 error = g_vfs_open(devvp, &cp, "ntfs", ronly ? 0 : 1);
286 VOP_UNLOCK(devvp, 0);
292 error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
295 ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK | M_ZERO);
296 bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
298 * We must not cache the boot block if its size is not exactly
299 * one cluster in order to avoid confusing the buffer cache when
300 * the boot file is read later by ntfs_readntvattr_plain(), which
301 * reads a cluster at a time.
303 if (ntfs_cntob(1) != BBSIZE)
304 bp->b_flags |= B_NOCACHE;
308 if (strncmp((const char *)ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
310 dprintf(("ntfs_mountfs: invalid boot block\n"));
315 int8_t cpr = ntmp->ntm_mftrecsz;
317 ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
319 ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
321 ntmp->ntm_multiplier = ntmp->ntm_bps / DEV_BSIZE;
323 dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
324 ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
325 ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
326 dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
327 (u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
329 ntmp->ntm_mountp = mp;
330 ntmp->ntm_devvp = devvp;
331 if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
333 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
335 if (1 == vfs_scanopt(mp->mnt_optnew, "mode", "%d", &v))
336 ntmp->ntm_mode = v & ACCESSPERMS;
337 vfs_flagopt(mp->mnt_optnew,
338 "caseins", &ntmp->ntm_flag, NTFS_MFLAG_CASEINS);
339 vfs_flagopt(mp->mnt_optnew,
340 "allnames", &ntmp->ntm_flag, NTFS_MFLAG_ALLNAMES);
342 ntmp->ntm_bo = &devvp->v_bufobj;
344 cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
345 if (error && error != ENOENT)
347 cs_ntfs = vfs_getopts(mp->mnt_optnew, "cs_ntfs", &error);
348 if (error && error != ENOENT)
350 /* Copy in the 8-bit to Unicode conversion table */
351 /* Initialize Unicode to 8-bit table from 8toU table */
352 ntfs_82u_init(ntmp, cs_local, cs_ntfs);
353 if (cs_local != NULL && cs_ntfs != NULL)
354 ntfs_u28_init(ntmp, NULL, cs_local, cs_ntfs);
356 ntfs_u28_init(ntmp, ntmp->ntm_82u, cs_local, cs_ntfs);
360 dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
361 (ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
362 (ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
363 ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
366 * We read in some system nodes to do not allow
367 * reclaim them and to have everytime access to them.
370 int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
371 for (i=0; i<3; i++) {
372 error = VFS_VGET(mp, pi[i], LK_EXCLUSIVE,
373 &(ntmp->ntm_sysvn[pi[i]]));
376 ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
377 VREF(ntmp->ntm_sysvn[pi[i]]);
378 vput(ntmp->ntm_sysvn[pi[i]]);
382 /* read the Unicode lowercase --> uppercase translation table,
384 if ((error = ntfs_toupper_use(mp, ntmp)))
388 * Scan $BitMap and count free clusters
390 error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
395 * Read and translate to internal format attribute
403 error = VFS_VGET(mp, NTFS_ATTRDEFINO, LK_EXCLUSIVE, &vp );
407 /* Count valid entries */
409 error = ntfs_readattr(ntmp, VTONT(vp),
411 num * sizeof(ad), sizeof(ad),
415 if (ad.ad_name[0] == 0)
419 /* Alloc memory for attribute definitions */
420 ntmp->ntm_ad = malloc(num * sizeof(struct ntvattrdef),
421 M_NTFSMNT, M_WAITOK);
423 ntmp->ntm_adnum = num;
425 /* Read them and translate */
427 error = ntfs_readattr(ntmp, VTONT(vp),
429 i * sizeof(ad), sizeof(ad),
435 ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
436 } while(ad.ad_name[j++]);
437 ntmp->ntm_ad[i].ad_namelen = j - 1;
438 ntmp->ntm_ad[i].ad_type = ad.ad_type;
444 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
445 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
446 mp->mnt_maxsymlinklen = 0;
448 mp->mnt_flag |= MNT_LOCAL;
453 for(i=0;i<NTFS_SYSNODESNUM;i++)
454 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
456 if (vflush(mp, 0, 0, td))
457 dprintf(("ntfs_mountfs: vflush failed\n"));
478 struct ntfsmount *ntmp;
481 dprintf(("ntfs_unmount: unmounting...\n"));
483 ntmp = VFSTONTFS(mp);
486 if(mntflags & MNT_FORCE)
489 dprintf(("ntfs_unmount: vflushing...\n"));
490 error = vflush(mp, 0, flags | SKIPSYSTEM, td);
492 printf("ntfs_unmount: vflush failed: %d\n",error);
496 /* Check if only system vnodes are rest */
497 for(i=0;i<NTFS_SYSNODESNUM;i++)
498 if((ntmp->ntm_sysvn[i]) &&
499 (vrefcnt(ntmp->ntm_sysvn[i]) > 1)) return (EBUSY);
501 /* Dereference all system vnodes */
502 for(i=0;i<NTFS_SYSNODESNUM;i++)
503 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
505 /* vflush system vnodes */
506 error = vflush(mp, 0, flags, td);
508 printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
510 vinvalbuf(ntmp->ntm_devvp, V_SAVE, 0, 0);
514 g_vfs_close(ntmp->ntm_cp);
518 vrele(ntmp->ntm_devvp);
520 /* free the toupper table, if this has been last mounted ntfs volume */
521 ntfs_toupper_unuse();
523 dprintf(("ntfs_umount: freeing memory...\n"));
524 ntfs_u28_uninit(ntmp);
525 ntfs_82u_uninit(ntmp);
528 mp->mnt_flag &= ~MNT_LOCAL;
530 free(ntmp->ntm_ad, M_NTFSMNT);
531 free(ntmp, M_NTFSMNT);
544 dprintf(("ntfs_root(): sysvn: %p\n",
545 VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
546 error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, LK_EXCLUSIVE, &nvp);
548 printf("ntfs_root: VFS_VGET failed: %d\n",error);
558 struct ntfsmount *ntmp,
567 vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
569 bmsize = VTOF(vp)->f_size;
571 tmp = malloc(bmsize, M_TEMP, M_WAITOK);
573 error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
574 0, bmsize, tmp, NULL);
578 for(i=0;i<bmsize;i++)
580 if(~tmp[i] & (1 << j)) cfree++;
593 struct ntfsmount *ntmp = VFSTONTFS(mp);
594 u_int64_t mftsize,mftallocated;
596 dprintf(("ntfs_statfs():\n"));
598 mftsize = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_size;
599 mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
601 sbp->f_type = mp->mnt_vfc->vfc_typenum;
602 sbp->f_bsize = ntmp->ntm_bps;
603 sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
604 sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
605 sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
606 sbp->f_ffree = sbp->f_bfree / ntmp->ntm_bpmftrec;
607 sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
609 sbp->f_flags = mp->mnt_flag;
623 struct ntfid *ntfhp = (struct ntfid *)fhp;
626 ddprintf(("ntfs_fhtovp(): %d\n", ntfhp->ntfid_ino));
628 if ((error = VFS_VGET(mp, ntfhp->ntfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
632 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
633 * with NTFS, we don't need to check anything else for now */
635 vnode_create_vobject(nvp, VTOF(nvp)->f_size, curthread);
651 register struct ntfsmount *ntmp;
657 dprintf(("ntfs_vgetex: ino: %d, attr: 0x%x:%s, lkf: 0x%lx, f: 0x%lx\n",
658 ino, attrtype, attrname?attrname:"", (u_long)lkflags,
661 ntmp = VFSTONTFS(mp);
665 error = ntfs_ntlookup(ntmp, ino, &ip);
667 printf("ntfs_vget: ntfs_ntget failed\n");
671 /* It may be not initialized fully, so force load it */
672 if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
673 error = ntfs_loadntnode(ntmp, ip);
675 printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO: %d\n",
682 error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
684 printf("ntfs_vget: ntfs_fget failed\n");
690 if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
691 if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
692 (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
694 } else if (flags & VG_EXT) {
696 fp->f_size = fp->f_allocated = 0;
700 error = ntfs_filesize(ntmp, fp,
701 &fp->f_size, &fp->f_allocated);
708 fp->f_flag |= FN_VALID;
712 vget(FTOV(fp), lkflags, td);
718 error = getnewvnode("ntfs", ntmp->ntm_mountp, &ntfs_vnodeops, &vp);
724 /* XXX: Too early for mpsafe fs, lacks vnode lock */
725 error = insmntque(vp, ntmp->ntm_mountp);
731 dprintf(("ntfs_vget: vnode: %p for ntnode: %d\n", vp,ino));
737 vp->v_bufobj.bo_ops = &ntfs_vnbufops;
738 vp->v_bufobj.bo_private = vp;
740 if (ino == NTFS_ROOTINO)
741 vp->v_vflag |= VV_ROOT;
745 if (lkflags & LK_TYPE_MASK) {
746 error = vn_lock(vp, lkflags);
765 return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL, lkflags, 0,
770 ntfs_bufstrategy(struct bufobj *bo, struct buf *bp)
776 KASSERT(bo == &vp->v_bufobj, ("BO/VP mismatch: vp %p bo %p != %p",
777 vp, &vp->v_bufobj, bo));
778 rc = VOP_STRATEGY(vp, bp);
779 KASSERT(rc == 0, ("NTFS VOP_STRATEGY failed: bp=%p, "
780 "vp=%p, rc=%d", bp, vp, rc));
783 static struct vfsops ntfs_vfsops = {
784 .vfs_fhtovp = ntfs_fhtovp,
785 .vfs_init = ntfs_init,
786 .vfs_cmount = ntfs_cmount,
787 .vfs_mount = ntfs_mount,
788 .vfs_root = ntfs_root,
789 .vfs_statfs = ntfs_statfs,
790 .vfs_uninit = ntfs_uninit,
791 .vfs_unmount = ntfs_unmount,
792 .vfs_vget = ntfs_vget,
794 VFS_SET(ntfs_vfsops, ntfs, 0);
795 MODULE_VERSION(ntfs, 1);