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