Home | History | Annotate | Line # | Download | only in ntfs
ntfs_vfsops.c revision 1.68
      1 /*	$NetBSD: ntfs_vfsops.c,v 1.68 2008/05/05 17:11:16 ad 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  *	Id: ntfs_vfsops.c,v 1.7 1999/05/31 11:28:30 phk Exp
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: ntfs_vfsops.c,v 1.68 2008/05/05 17:11:16 ad Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/namei.h>
     37 #include <sys/proc.h>
     38 #include <sys/kernel.h>
     39 #include <sys/vnode.h>
     40 #include <sys/mount.h>
     41 #include <sys/buf.h>
     42 #include <sys/fcntl.h>
     43 #include <sys/malloc.h>
     44 #include <sys/sysctl.h>
     45 #include <sys/device.h>
     46 #include <sys/conf.h>
     47 #include <sys/kauth.h>
     48 
     49 #include <uvm/uvm_extern.h>
     50 
     51 #include <miscfs/genfs/genfs.h>
     52 #include <miscfs/specfs/specdev.h>
     53 
     54 #include <fs/ntfs/ntfs.h>
     55 #include <fs/ntfs/ntfs_inode.h>
     56 #include <fs/ntfs/ntfs_subr.h>
     57 #include <fs/ntfs/ntfs_vfsops.h>
     58 #include <fs/ntfs/ntfs_ihash.h>
     59 #include <fs/ntfs/ntfsmount.h>
     60 
     61 MALLOC_JUSTDEFINE(M_NTFSMNT, "NTFS mount", "NTFS mount structure");
     62 MALLOC_JUSTDEFINE(M_NTFSNTNODE,"NTFS ntnode",  "NTFS ntnode information");
     63 MALLOC_JUSTDEFINE(M_NTFSFNODE,"NTFS fnode",  "NTFS fnode information");
     64 MALLOC_JUSTDEFINE(M_NTFSDIR,"NTFS dir",  "NTFS dir buffer");
     65 
     66 static int	ntfs_mount(struct mount *, const char *, void *, size_t *);
     67 static int	ntfs_root(struct mount *, struct vnode **);
     68 static int	ntfs_start(struct mount *, int);
     69 static int	ntfs_statvfs(struct mount *, struct statvfs *);
     70 static int	ntfs_sync(struct mount *, int, kauth_cred_t);
     71 static int	ntfs_unmount(struct mount *, int);
     72 static int	ntfs_vget(struct mount *mp, ino_t ino,
     73 			       struct vnode **vpp);
     74 static int	ntfs_mountfs(struct vnode *, struct mount *,
     75 				  struct ntfs_args *, struct lwp *);
     76 static int	ntfs_vptofh(struct vnode *, struct fid *, size_t *);
     77 
     78 static void     ntfs_init(void);
     79 static void     ntfs_reinit(void);
     80 static void     ntfs_done(void);
     81 static int      ntfs_fhtovp(struct mount *, struct fid *,
     82 				struct vnode **);
     83 static int      ntfs_mountroot(void);
     84 
     85 static const struct genfs_ops ntfs_genfsops = {
     86 	.gop_write = genfs_compat_gop_write,
     87 };
     88 
     89 SYSCTL_SETUP(sysctl_vfs_ntfs_setup, "sysctl vfs.ntfs subtree setup")
     90 {
     91 
     92 	sysctl_createv(clog, 0, NULL, NULL,
     93 		       CTLFLAG_PERMANENT,
     94 		       CTLTYPE_NODE, "vfs", NULL,
     95 		       NULL, 0, NULL, 0,
     96 		       CTL_VFS, CTL_EOL);
     97 	sysctl_createv(clog, 0, NULL, NULL,
     98 		       CTLFLAG_PERMANENT,
     99 		       CTLTYPE_NODE, "ntfs",
    100 		       SYSCTL_DESCR("NTFS file system"),
    101 		       NULL, 0, NULL, 0,
    102 		       CTL_VFS, 20, CTL_EOL);
    103 	/*
    104 	 * XXX the "20" above could be dynamic, thereby eliminating
    105 	 * one more instance of the "number to vfs" mapping problem,
    106 	 * but "20" is the order as taken from sys/mount.h
    107 	 */
    108 }
    109 
    110 static int
    111 ntfs_mountroot()
    112 {
    113 	struct mount *mp;
    114 	struct lwp *l = curlwp;	/* XXX */
    115 	int error;
    116 	struct ntfs_args args;
    117 
    118 	if (device_class(root_device) != DV_DISK)
    119 		return (ENODEV);
    120 
    121 	if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
    122 		vrele(rootvp);
    123 		return (error);
    124 	}
    125 
    126 	args.flag = 0;
    127 	args.uid = 0;
    128 	args.gid = 0;
    129 	args.mode = 0777;
    130 
    131 	if ((error = ntfs_mountfs(rootvp, mp, &args, l)) != 0) {
    132 		vfs_unbusy(mp, false, NULL);
    133 		vfs_destroy(mp, false);
    134 		return (error);
    135 	}
    136 
    137 	mutex_enter(&mountlist_lock);
    138 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    139 	mp->mnt_iflag |= IMNT_ONLIST;
    140 	mutex_exit(&mountlist_lock);
    141 	(void)ntfs_statvfs(mp, &mp->mnt_stat);
    142 	vfs_unbusy(mp, false, NULL);
    143 	return (0);
    144 }
    145 
    146 static void
    147 ntfs_init()
    148 {
    149 
    150 	malloc_type_attach(M_NTFSMNT);
    151 	malloc_type_attach(M_NTFSNTNODE);
    152 	malloc_type_attach(M_NTFSFNODE);
    153 	malloc_type_attach(M_NTFSDIR);
    154 	malloc_type_attach(M_NTFSNTVATTR);
    155 	malloc_type_attach(M_NTFSRDATA);
    156 	malloc_type_attach(M_NTFSDECOMP);
    157 	malloc_type_attach(M_NTFSRUN);
    158 	ntfs_nthashinit();
    159 	ntfs_toupper_init();
    160 }
    161 
    162 static void
    163 ntfs_reinit()
    164 {
    165 	ntfs_nthashreinit();
    166 }
    167 
    168 static void
    169 ntfs_done()
    170 {
    171 	ntfs_nthashdone();
    172 	malloc_type_detach(M_NTFSMNT);
    173 	malloc_type_detach(M_NTFSNTNODE);
    174 	malloc_type_detach(M_NTFSFNODE);
    175 	malloc_type_detach(M_NTFSDIR);
    176 	malloc_type_detach(M_NTFSNTVATTR);
    177 	malloc_type_detach(M_NTFSRDATA);
    178 	malloc_type_detach(M_NTFSDECOMP);
    179 	malloc_type_detach(M_NTFSRUN);
    180 }
    181 
    182 static int
    183 ntfs_mount (
    184 	struct mount *mp,
    185 	const char *path,
    186 	void *data,
    187 	size_t *data_len)
    188 {
    189 	struct nameidata nd;
    190 	struct lwp *l = curlwp;
    191 	struct nameidata *ndp = &nd;
    192 	int		err = 0, flags;
    193 	struct vnode	*devvp;
    194 	struct ntfs_args *args = data;
    195 
    196 	if (*data_len < sizeof *args)
    197 		return EINVAL;
    198 
    199 	if (mp->mnt_flag & MNT_GETARGS) {
    200 		struct ntfsmount *ntmp = VFSTONTFS(mp);
    201 		if (ntmp == NULL)
    202 			return EIO;
    203 		args->fspec = NULL;
    204 		args->uid = ntmp->ntm_uid;
    205 		args->gid = ntmp->ntm_gid;
    206 		args->mode = ntmp->ntm_mode;
    207 		args->flag = ntmp->ntm_flag;
    208 		*data_len = sizeof *args;
    209 		return 0;
    210 	}
    211 	/*
    212 	 ***
    213 	 * Mounting non-root file system or updating a file system
    214 	 ***
    215 	 */
    216 
    217 	/*
    218 	 * If updating, check whether changing from read-only to
    219 	 * read/write; if there is no device name, that's all we do.
    220 	 */
    221 	if (mp->mnt_flag & MNT_UPDATE) {
    222 		printf("ntfs_mount(): MNT_UPDATE not supported\n");
    223 		return (EINVAL);
    224 	}
    225 
    226 	/*
    227 	 * Not an update, or updating the name: look up the name
    228 	 * and verify that it refers to a sensible block device.
    229 	 */
    230 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
    231 	err = namei(ndp);
    232 	if (err) {
    233 		/* can't get devvp!*/
    234 		return (err);
    235 	}
    236 
    237 	devvp = ndp->ni_vp;
    238 
    239 	if (devvp->v_type != VBLK) {
    240 		err = ENOTBLK;
    241 		goto fail;
    242 	}
    243 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    244 		err = ENXIO;
    245 		goto fail;
    246 	}
    247 	if (mp->mnt_flag & MNT_UPDATE) {
    248 #if 0
    249 		/*
    250 		 ********************
    251 		 * UPDATE
    252 		 ********************
    253 		 */
    254 
    255 		if (devvp != ntmp->um_devvp) {
    256 			err = EINVAL;	/* needs translation */
    257 			goto fail;
    258 		}
    259 
    260 		/*
    261 		 * Update device name only on success
    262 		 */
    263 		err = set_statvfs_info(NULL, UIO_USERSPACE, args->fspec,
    264 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, p);
    265 		if (err)
    266 			goto fail;
    267 
    268 		vrele(devvp);
    269 #endif
    270 	} else {
    271 		/*
    272 		 ********************
    273 		 * NEW MOUNT
    274 		 ********************
    275 		 */
    276 
    277 		/*
    278 		 * Since this is a new mount, we want the names for
    279 		 * the device and the mount point copied in.  If an
    280 		 * error occurs,  the mountpoint is discarded by the
    281 		 * upper level code.
    282 		 */
    283 
    284 		/* Save "last mounted on" info for mount point (NULL pad)*/
    285 		err = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    286 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    287 		if (err)
    288 			goto fail;
    289 
    290 		if (mp->mnt_flag & MNT_RDONLY)
    291 			flags = FREAD;
    292 		else
    293 			flags = FREAD|FWRITE;
    294 		err = VOP_OPEN(devvp, flags, FSCRED);
    295 		if (err)
    296 			goto fail;
    297 		err = ntfs_mountfs(devvp, mp, args, l);
    298 		if (err) {
    299 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    300 			(void)VOP_CLOSE(devvp, flags, NOCRED);
    301 			VOP_UNLOCK(devvp, 0);
    302 			goto fail;
    303 		}
    304 	}
    305 
    306 	/*
    307 	 * Initialize FS stat information in mount struct; uses both
    308 	 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
    309 	 *
    310 	 * This code is common to root and non-root mounts
    311 	 */
    312 	(void)VFS_STATVFS(mp, &mp->mnt_stat);
    313 	return (err);
    314 
    315 fail:
    316 	vrele(devvp);
    317 	return (err);
    318 }
    319 
    320 /*
    321  * Common code for mount and mountroot
    322  */
    323 int
    324 ntfs_mountfs(devvp, mp, argsp, l)
    325 	struct vnode *devvp;
    326 	struct mount *mp;
    327 	struct ntfs_args *argsp;
    328 	struct lwp *l;
    329 {
    330 	struct buf *bp;
    331 	struct ntfsmount *ntmp;
    332 	dev_t dev = devvp->v_rdev;
    333 	int error, ronly, i;
    334 	struct vnode *vp;
    335 
    336 	ntmp = NULL;
    337 
    338 	/*
    339 	 * Flush out any old buffers remaining from a previous use.
    340 	 */
    341 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    342 	error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
    343 	VOP_UNLOCK(devvp, 0);
    344 	if (error)
    345 		return (error);
    346 
    347 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    348 
    349 	bp = NULL;
    350 
    351 	error = bread(devvp, BBLOCK, BBSIZE, NOCRED, &bp);
    352 	if (error)
    353 		goto out;
    354 	ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
    355 	bzero( ntmp, sizeof *ntmp );
    356 	bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
    357 	brelse( bp , 0 );
    358 	bp = NULL;
    359 
    360 	if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
    361 		error = EINVAL;
    362 		dprintf(("ntfs_mountfs: invalid boot block\n"));
    363 		goto out;
    364 	}
    365 
    366 	{
    367 		int8_t cpr = ntmp->ntm_mftrecsz;
    368 		if( cpr > 0 )
    369 			ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
    370 		else
    371 			ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
    372 	}
    373 	dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
    374 		ntmp->ntm_bps,ntmp->ntm_spc,ntmp->ntm_bootfile.bf_media,
    375 		ntmp->ntm_mftrecsz,ntmp->ntm_bpmftrec));
    376 	dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
    377 		(u_int32_t)ntmp->ntm_mftcn,(u_int32_t)ntmp->ntm_mftmirrcn));
    378 
    379 	ntmp->ntm_mountp = mp;
    380 	ntmp->ntm_dev = dev;
    381 	ntmp->ntm_devvp = devvp;
    382 	ntmp->ntm_uid = argsp->uid;
    383 	ntmp->ntm_gid = argsp->gid;
    384 	ntmp->ntm_mode = argsp->mode;
    385 	ntmp->ntm_flag = argsp->flag;
    386 	mp->mnt_data = ntmp;
    387 
    388 	/* set file name encode/decode hooks XXX utf-8 only for now */
    389 	ntmp->ntm_wget = ntfs_utf8_wget;
    390 	ntmp->ntm_wput = ntfs_utf8_wput;
    391 	ntmp->ntm_wcmp = ntfs_utf8_wcmp;
    392 
    393 	dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
    394 		(ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
    395 		(ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
    396 		ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
    397 
    398 	/*
    399 	 * We read in some system nodes to do not allow
    400 	 * reclaim them and to have everytime access to them.
    401 	 */
    402 	{
    403 		int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
    404 		for (i=0; i<3; i++) {
    405 			error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
    406 			if(error)
    407 				goto out1;
    408 			ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
    409 			VREF(ntmp->ntm_sysvn[pi[i]]);
    410 			vput(ntmp->ntm_sysvn[pi[i]]);
    411 		}
    412 	}
    413 
    414 	/* read the Unicode lowercase --> uppercase translation table,
    415 	 * if necessary */
    416 	if ((error = ntfs_toupper_use(mp, ntmp)))
    417 		goto out1;
    418 
    419 	/*
    420 	 * Scan $BitMap and count free clusters
    421 	 */
    422 	error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
    423 	if(error)
    424 		goto out1;
    425 
    426 	/*
    427 	 * Read and translate to internal format attribute
    428 	 * definition file.
    429 	 */
    430 	{
    431 		int num,j;
    432 		struct attrdef ad;
    433 
    434 		/* Open $AttrDef */
    435 		error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp );
    436 		if(error)
    437 			goto out1;
    438 
    439 		/* Count valid entries */
    440 		for(num=0;;num++) {
    441 			error = ntfs_readattr(ntmp, VTONT(vp),
    442 					NTFS_A_DATA, NULL,
    443 					num * sizeof(ad), sizeof(ad),
    444 					&ad, NULL);
    445 			if (error)
    446 				goto out1;
    447 			if (ad.ad_name[0] == 0)
    448 				break;
    449 		}
    450 
    451 		/* Alloc memory for attribute definitions */
    452 		ntmp->ntm_ad = (struct ntvattrdef *) malloc(
    453 			num * sizeof(struct ntvattrdef),
    454 			M_NTFSMNT, M_WAITOK);
    455 
    456 		ntmp->ntm_adnum = num;
    457 
    458 		/* Read them and translate */
    459 		for(i=0;i<num;i++){
    460 			error = ntfs_readattr(ntmp, VTONT(vp),
    461 					NTFS_A_DATA, NULL,
    462 					i * sizeof(ad), sizeof(ad),
    463 					&ad, NULL);
    464 			if (error)
    465 				goto out1;
    466 			j = 0;
    467 			do {
    468 				ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
    469 			} while(ad.ad_name[j++]);
    470 			ntmp->ntm_ad[i].ad_namelen = j - 1;
    471 			ntmp->ntm_ad[i].ad_type = ad.ad_type;
    472 		}
    473 
    474 		vput(vp);
    475 	}
    476 
    477 	mp->mnt_stat.f_fsidx.__fsid_val[0] = dev;
    478 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NTFS);
    479 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    480 	mp->mnt_stat.f_namemax = NTFS_MAXFILENAME;
    481 	mp->mnt_flag |= MNT_LOCAL;
    482 	devvp->v_specmountpoint = mp;
    483 	return (0);
    484 
    485 out1:
    486 	for(i=0;i<NTFS_SYSNODESNUM;i++)
    487 		if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
    488 
    489 	if (vflush(mp,NULLVP,0)) {
    490 		dprintf(("ntfs_mountfs: vflush failed\n"));
    491 	}
    492 out:
    493 	devvp->v_specmountpoint = NULL;
    494 	if (bp)
    495 		brelse(bp, 0);
    496 
    497 	if (error) {
    498 		if (ntmp) {
    499 			if (ntmp->ntm_ad)
    500 				free(ntmp->ntm_ad, M_NTFSMNT);
    501 			free(ntmp, M_NTFSMNT);
    502 		}
    503 	}
    504 
    505 	return (error);
    506 }
    507 
    508 static int
    509 ntfs_start (
    510 	struct mount *mp,
    511 	int flags)
    512 {
    513 	return (0);
    514 }
    515 
    516 static int
    517 ntfs_unmount(
    518 	struct mount *mp,
    519 	int mntflags)
    520 {
    521 	struct lwp *l = curlwp;
    522 	struct ntfsmount *ntmp;
    523 	int error, ronly = 0, flags, i;
    524 
    525 	dprintf(("ntfs_unmount: unmounting...\n"));
    526 	ntmp = VFSTONTFS(mp);
    527 
    528 	flags = 0;
    529 	if(mntflags & MNT_FORCE)
    530 		flags |= FORCECLOSE;
    531 
    532 	dprintf(("ntfs_unmount: vflushing...\n"));
    533 	error = vflush(mp,NULLVP,flags | SKIPSYSTEM);
    534 	if (error) {
    535 		dprintf(("ntfs_unmount: vflush failed: %d\n",error));
    536 		return (error);
    537 	}
    538 
    539 	/* Check if only system vnodes are rest */
    540 	for(i=0;i<NTFS_SYSNODESNUM;i++)
    541 		 if((ntmp->ntm_sysvn[i]) &&
    542 		    (ntmp->ntm_sysvn[i]->v_usecount > 1)) return (EBUSY);
    543 
    544 	/* Dereference all system vnodes */
    545 	for(i=0;i<NTFS_SYSNODESNUM;i++)
    546 		 if(ntmp->ntm_sysvn[i]) vrele(ntmp->ntm_sysvn[i]);
    547 
    548 	/* vflush system vnodes */
    549 	error = vflush(mp,NULLVP,flags);
    550 	if (error) {
    551 		panic("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
    552 	}
    553 
    554 	/* Check if the type of device node isn't VBAD before
    555 	 * touching v_specinfo.  If the device vnode is revoked, the
    556 	 * field is NULL and touching it causes null pointer derefercence.
    557 	 */
    558 	if (ntmp->ntm_devvp->v_type != VBAD)
    559 		ntmp->ntm_devvp->v_specmountpoint = NULL;
    560 
    561 	vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0);
    562 
    563 	/* lock the device vnode before calling VOP_CLOSE() */
    564 	vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
    565 	error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
    566 		NOCRED);
    567 	KASSERT(error == 0);
    568 	VOP_UNLOCK(ntmp->ntm_devvp, 0);
    569 
    570 	vrele(ntmp->ntm_devvp);
    571 
    572 	/* free the toupper table, if this has been last mounted ntfs volume */
    573 	ntfs_toupper_unuse();
    574 
    575 	dprintf(("ntfs_umount: freeing memory...\n"));
    576 	mp->mnt_data = NULL;
    577 	mp->mnt_flag &= ~MNT_LOCAL;
    578 	free(ntmp->ntm_ad, M_NTFSMNT);
    579 	FREE(ntmp, M_NTFSMNT);
    580 	return (0);
    581 }
    582 
    583 static int
    584 ntfs_root(
    585 	struct mount *mp,
    586 	struct vnode **vpp)
    587 {
    588 	struct vnode *nvp;
    589 	int error = 0;
    590 
    591 	dprintf(("ntfs_root(): sysvn: %p\n",
    592 		VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
    593 	error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
    594 	if(error) {
    595 		printf("ntfs_root: VFS_VGET failed: %d\n",error);
    596 		return (error);
    597 	}
    598 
    599 	*vpp = nvp;
    600 	return (0);
    601 }
    602 
    603 int
    604 ntfs_calccfree(
    605 	struct ntfsmount *ntmp,
    606 	cn_t *cfreep)
    607 {
    608 	struct vnode *vp;
    609 	u_int8_t *tmp;
    610 	int j, error;
    611 	cn_t cfree = 0;
    612 	size_t bmsize, i;
    613 
    614 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
    615 
    616 	bmsize = VTOF(vp)->f_size;
    617 
    618 	tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
    619 
    620 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
    621 			       0, bmsize, tmp, NULL);
    622 	if (error)
    623 		goto out;
    624 
    625 	for(i=0;i<bmsize;i++)
    626 		for(j=0;j<8;j++)
    627 			if(~tmp[i] & (1 << j)) cfree++;
    628 	*cfreep = cfree;
    629 
    630     out:
    631 	free(tmp, M_TEMP);
    632 	return(error);
    633 }
    634 
    635 static int
    636 ntfs_statvfs(
    637 	struct mount *mp,
    638 	struct statvfs *sbp)
    639 {
    640 	struct ntfsmount *ntmp = VFSTONTFS(mp);
    641 	u_int64_t mftallocated;
    642 
    643 	dprintf(("ntfs_statvfs():\n"));
    644 
    645 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
    646 
    647 	sbp->f_bsize = ntmp->ntm_bps;
    648 	sbp->f_frsize = sbp->f_bsize; /* XXX */
    649 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
    650 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
    651 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
    652 	sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
    653 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
    654 	    sbp->f_ffree;
    655 	sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */
    656 	sbp->f_flag = mp->mnt_flag;
    657 	copy_statvfs_info(sbp, mp);
    658 	return (0);
    659 }
    660 
    661 static int
    662 ntfs_sync (
    663 	struct mount *mp,
    664 	int waitfor,
    665 	kauth_cred_t cred)
    666 {
    667 	/*dprintf(("ntfs_sync():\n"));*/
    668 	return (0);
    669 }
    670 
    671 /*ARGSUSED*/
    672 static int
    673 ntfs_fhtovp(
    674 	struct mount *mp,
    675 	struct fid *fhp,
    676 	struct vnode **vpp)
    677 {
    678 	struct ntfid ntfh;
    679 	int error;
    680 
    681 	if (fhp->fid_len != sizeof(struct ntfid))
    682 		return EINVAL;
    683 	memcpy(&ntfh, fhp, sizeof(ntfh));
    684 	ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname,
    685 	    (unsigned long long)ntfh.ntfid_ino));
    686 
    687 	error = ntfs_vgetex(mp, ntfh.ntfid_ino, ntfh.ntfid_attr, NULL,
    688 			LK_EXCLUSIVE | LK_RETRY, 0, vpp);
    689 	if (error != 0) {
    690 		*vpp = NULLVP;
    691 		return (error);
    692 	}
    693 
    694 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
    695 	 * with NTFS, we don't need to check anything else for now */
    696 	return (0);
    697 }
    698 
    699 static int
    700 ntfs_vptofh(
    701 	struct vnode *vp,
    702 	struct fid *fhp,
    703 	size_t *fh_size)
    704 {
    705 	struct ntnode *ntp;
    706 	struct ntfid ntfh;
    707 	struct fnode *fn;
    708 
    709 	if (*fh_size < sizeof(struct ntfid)) {
    710 		*fh_size = sizeof(struct ntfid);
    711 		return E2BIG;
    712 	}
    713 	*fh_size = sizeof(struct ntfid);
    714 
    715 	ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
    716 		vp));
    717 
    718 	fn = VTOF(vp);
    719 	ntp = VTONT(vp);
    720 	memset(&ntfh, 0, sizeof(ntfh));
    721 	ntfh.ntfid_len = sizeof(struct ntfid);
    722 	ntfh.ntfid_ino = ntp->i_number;
    723 	ntfh.ntfid_attr = fn->f_attrtype;
    724 #ifdef notyet
    725 	ntfh.ntfid_gen = ntp->i_gen;
    726 #endif
    727 	memcpy(fhp, &ntfh, sizeof(ntfh));
    728 	return (0);
    729 }
    730 
    731 int
    732 ntfs_vgetex(
    733 	struct mount *mp,
    734 	ino_t ino,
    735 	u_int32_t attrtype,
    736 	char *attrname,
    737 	u_long lkflags,
    738 	u_long flags,
    739 	struct vnode **vpp)
    740 {
    741 	int error;
    742 	struct ntfsmount *ntmp;
    743 	struct ntnode *ip;
    744 	struct fnode *fp;
    745 	struct vnode *vp;
    746 	enum vtype f_type = VBAD;
    747 
    748 	dprintf(("ntfs_vgetex: ino: %llu, attr: 0x%x:%s, lkf: 0x%lx, f:"
    749 	    " 0x%lx\n", (unsigned long long)ino, attrtype,
    750 	    attrname ? attrname : "", (u_long)lkflags, (u_long)flags));
    751 
    752 	ntmp = VFSTONTFS(mp);
    753 	*vpp = NULL;
    754 
    755 	/* Get ntnode */
    756 	error = ntfs_ntlookup(ntmp, ino, &ip);
    757 	if (error) {
    758 		printf("ntfs_vget: ntfs_ntget failed\n");
    759 		return (error);
    760 	}
    761 
    762 	/* It may be not initialized fully, so force load it */
    763 	if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
    764 		error = ntfs_loadntnode(ntmp, ip);
    765 		if(error) {
    766 			printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO:"
    767 			    " %llu\n", (unsigned long long)ip->i_number);
    768 			ntfs_ntput(ip);
    769 			return (error);
    770 		}
    771 	}
    772 
    773 	error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
    774 	if (error) {
    775 		printf("ntfs_vget: ntfs_fget failed\n");
    776 		ntfs_ntput(ip);
    777 		return (error);
    778 	}
    779 
    780 	if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
    781 		if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
    782 		    (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
    783 			f_type = VDIR;
    784 		} else if (flags & VG_EXT) {
    785 			f_type = VNON;
    786 			fp->f_size = fp->f_allocated = 0;
    787 		} else {
    788 			f_type = VREG;
    789 
    790 			error = ntfs_filesize(ntmp, fp,
    791 					      &fp->f_size, &fp->f_allocated);
    792 			if (error) {
    793 				ntfs_ntput(ip);
    794 				return (error);
    795 			}
    796 		}
    797 
    798 		fp->f_flag |= FN_VALID;
    799 	}
    800 
    801 	/*
    802 	 * We may be calling vget() now. To avoid potential deadlock, we need
    803 	 * to release ntnode lock, since due to locking order vnode
    804 	 * lock has to be acquired first.
    805 	 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled
    806 	 * prematurely.
    807 	 */
    808 	ntfs_ntput(ip);
    809 
    810 	if (FTOV(fp)) {
    811 		/* vget() returns error if the vnode has been recycled */
    812 		if (vget(FTOV(fp), lkflags) == 0) {
    813 			*vpp = FTOV(fp);
    814 			return (0);
    815 		}
    816 	}
    817 
    818 	error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
    819 	if(error) {
    820 		ntfs_frele(fp);
    821 		ntfs_ntput(ip);
    822 		return (error);
    823 	}
    824 	dprintf(("ntfs_vget: vnode: %p for ntnode: %llu\n", vp,
    825 	    (unsigned long long)ino));
    826 
    827 	fp->f_vp = vp;
    828 	vp->v_data = fp;
    829 	if (f_type != VBAD)
    830 		vp->v_type = f_type;
    831 	genfs_node_init(vp, &ntfs_genfsops);
    832 
    833 	if (ino == NTFS_ROOTINO)
    834 		vp->v_vflag |= VV_ROOT;
    835 
    836 	if (lkflags & LK_TYPE_MASK) {
    837 		error = vn_lock(vp, lkflags);
    838 		if (error) {
    839 			vput(vp);
    840 			return (error);
    841 		}
    842 	}
    843 
    844 	uvm_vnp_setsize(vp, 0); /* XXX notused */
    845 	VREF(ip->i_devvp);
    846 	*vpp = vp;
    847 	return (0);
    848 }
    849 
    850 static int
    851 ntfs_vget(
    852 	struct mount *mp,
    853 	ino_t ino,
    854 	struct vnode **vpp)
    855 {
    856 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
    857 			LK_EXCLUSIVE | LK_RETRY, 0, vpp);
    858 }
    859 
    860 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
    861 
    862 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
    863 	&ntfs_vnodeop_opv_desc,
    864 	NULL,
    865 };
    866 
    867 struct vfsops ntfs_vfsops = {
    868 	MOUNT_NTFS,
    869 	sizeof (struct ntfs_args),
    870 	ntfs_mount,
    871 	ntfs_start,
    872 	ntfs_unmount,
    873 	ntfs_root,
    874 	(void *)eopnotsupp,	/* vfs_quotactl */
    875 	ntfs_statvfs,
    876 	ntfs_sync,
    877 	ntfs_vget,
    878 	ntfs_fhtovp,
    879 	ntfs_vptofh,
    880 	ntfs_init,
    881 	ntfs_reinit,
    882 	ntfs_done,
    883 	ntfs_mountroot,
    884 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    885 	vfs_stdextattrctl,
    886 	(void *)eopnotsupp,		/* vfs_suspendctl */
    887 	genfs_renamelock_enter,
    888 	genfs_renamelock_exit,
    889 	(void *)eopnotsupp,
    890 	ntfs_vnodeopv_descs,
    891 	0,
    892 	{ NULL, NULL },
    893 };
    894 VFS_ATTACH(ntfs_vfsops);
    895