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