Home | History | Annotate | Line # | Download | only in ntfs
ntfs_vfsops.c revision 1.72.6.1.2.1
      1 /*	$NetBSD: ntfs_vfsops.c,v 1.72.6.1.2.1 2014/04/28 16:03:15 sborrill 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.72.6.1.2.1 2014/04/28 16:03:15 sborrill 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()
     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 	mutex_enter(&mountlist_lock);
    122 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    123 	mutex_exit(&mountlist_lock);
    124 	(void)ntfs_statvfs(mp, &mp->mnt_stat);
    125 	vfs_unbusy(mp, false, NULL);
    126 	return (0);
    127 }
    128 
    129 static void
    130 ntfs_init()
    131 {
    132 
    133 	malloc_type_attach(M_NTFSMNT);
    134 	malloc_type_attach(M_NTFSNTNODE);
    135 	malloc_type_attach(M_NTFSFNODE);
    136 	malloc_type_attach(M_NTFSDIR);
    137 	malloc_type_attach(M_NTFSNTVATTR);
    138 	malloc_type_attach(M_NTFSRDATA);
    139 	malloc_type_attach(M_NTFSDECOMP);
    140 	malloc_type_attach(M_NTFSRUN);
    141 	ntfs_nthashinit();
    142 	ntfs_toupper_init();
    143 }
    144 
    145 static void
    146 ntfs_reinit()
    147 {
    148 	ntfs_nthashreinit();
    149 }
    150 
    151 static void
    152 ntfs_done()
    153 {
    154 	ntfs_nthashdone();
    155 	malloc_type_detach(M_NTFSMNT);
    156 	malloc_type_detach(M_NTFSNTNODE);
    157 	malloc_type_detach(M_NTFSFNODE);
    158 	malloc_type_detach(M_NTFSDIR);
    159 	malloc_type_detach(M_NTFSNTVATTR);
    160 	malloc_type_detach(M_NTFSRDATA);
    161 	malloc_type_detach(M_NTFSDECOMP);
    162 	malloc_type_detach(M_NTFSRUN);
    163 }
    164 
    165 static int
    166 ntfs_mount (
    167 	struct mount *mp,
    168 	const char *path,
    169 	void *data,
    170 	size_t *data_len)
    171 {
    172 	struct nameidata nd;
    173 	struct lwp *l = curlwp;
    174 	struct nameidata *ndp = &nd;
    175 	int		err = 0, flags;
    176 	struct vnode	*devvp;
    177 	struct ntfs_args *args = data;
    178 
    179 	if (args == NULL)
    180 		return EINVAL;
    181 	if (*data_len < sizeof *args)
    182 		return EINVAL;
    183 
    184 	if (mp->mnt_flag & MNT_GETARGS) {
    185 		struct ntfsmount *ntmp = VFSTONTFS(mp);
    186 		if (ntmp == NULL)
    187 			return EIO;
    188 		args->fspec = NULL;
    189 		args->uid = ntmp->ntm_uid;
    190 		args->gid = ntmp->ntm_gid;
    191 		args->mode = ntmp->ntm_mode;
    192 		args->flag = ntmp->ntm_flag;
    193 		*data_len = sizeof *args;
    194 		return 0;
    195 	}
    196 	/*
    197 	 ***
    198 	 * Mounting non-root file system or updating a file system
    199 	 ***
    200 	 */
    201 
    202 	/*
    203 	 * If updating, check whether changing from read-only to
    204 	 * read/write; if there is no device name, that's all we do.
    205 	 */
    206 	if (mp->mnt_flag & MNT_UPDATE) {
    207 		printf("ntfs_mount(): MNT_UPDATE not supported\n");
    208 		return (EINVAL);
    209 	}
    210 
    211 	/*
    212 	 * Not an update, or updating the name: look up the name
    213 	 * and verify that it refers to a sensible block device.
    214 	 */
    215 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
    216 	err = namei(ndp);
    217 	if (err) {
    218 		/* can't get devvp!*/
    219 		return (err);
    220 	}
    221 
    222 	devvp = ndp->ni_vp;
    223 
    224 	if (devvp->v_type != VBLK) {
    225 		err = ENOTBLK;
    226 		goto fail;
    227 	}
    228 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    229 		err = ENXIO;
    230 		goto fail;
    231 	}
    232 	if (mp->mnt_flag & MNT_UPDATE) {
    233 #if 0
    234 		/*
    235 		 ********************
    236 		 * UPDATE
    237 		 ********************
    238 		 */
    239 
    240 		if (devvp != ntmp->um_devvp) {
    241 			err = EINVAL;	/* needs translation */
    242 			goto fail;
    243 		}
    244 
    245 		/*
    246 		 * Update device name only on success
    247 		 */
    248 		err = set_statvfs_info(NULL, UIO_USERSPACE, args->fspec,
    249 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, p);
    250 		if (err)
    251 			goto fail;
    252 
    253 		vrele(devvp);
    254 #endif
    255 	} else {
    256 		/*
    257 		 ********************
    258 		 * NEW MOUNT
    259 		 ********************
    260 		 */
    261 
    262 		/*
    263 		 * Since this is a new mount, we want the names for
    264 		 * the device and the mount point copied in.  If an
    265 		 * error occurs,  the mountpoint is discarded by the
    266 		 * upper level code.
    267 		 */
    268 
    269 		/* Save "last mounted on" info for mount point (NULL pad)*/
    270 		err = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    271 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    272 		if (err)
    273 			goto fail;
    274 
    275 		if (mp->mnt_flag & MNT_RDONLY)
    276 			flags = FREAD;
    277 		else
    278 			flags = FREAD|FWRITE;
    279 		err = VOP_OPEN(devvp, flags, FSCRED);
    280 		if (err)
    281 			goto fail;
    282 		err = ntfs_mountfs(devvp, mp, args, l);
    283 		if (err) {
    284 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    285 			(void)VOP_CLOSE(devvp, flags, NOCRED);
    286 			VOP_UNLOCK(devvp, 0);
    287 			goto fail;
    288 		}
    289 	}
    290 
    291 	/*
    292 	 * Initialize FS stat information in mount struct; uses both
    293 	 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
    294 	 *
    295 	 * This code is common to root and non-root mounts
    296 	 */
    297 	(void)VFS_STATVFS(mp, &mp->mnt_stat);
    298 	return (err);
    299 
    300 fail:
    301 	vrele(devvp);
    302 	return (err);
    303 }
    304 
    305 /*
    306  * Common code for mount and mountroot
    307  */
    308 int
    309 ntfs_mountfs(devvp, mp, argsp, l)
    310 	struct vnode *devvp;
    311 	struct mount *mp;
    312 	struct ntfs_args *argsp;
    313 	struct lwp *l;
    314 {
    315 	struct buf *bp;
    316 	struct ntfsmount *ntmp;
    317 	dev_t dev = devvp->v_rdev;
    318 	int error, ronly, i;
    319 	struct vnode *vp;
    320 
    321 	ntmp = NULL;
    322 
    323 	/*
    324 	 * Flush out any old buffers remaining from a previous use.
    325 	 */
    326 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    327 	error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
    328 	VOP_UNLOCK(devvp, 0);
    329 	if (error)
    330 		return (error);
    331 
    332 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    333 
    334 	bp = NULL;
    335 
    336 	error = bread(devvp, BBLOCK, BBSIZE, NOCRED, 0, &bp);
    337 	if (error)
    338 		goto out;
    339 	ntmp = malloc( sizeof *ntmp, M_NTFSMNT, M_WAITOK );
    340 	bzero( ntmp, sizeof *ntmp );
    341 	bcopy( bp->b_data, &ntmp->ntm_bootfile, sizeof(struct bootfile) );
    342 	brelse( bp , 0 );
    343 	bp = NULL;
    344 
    345 	if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
    346 		error = EINVAL;
    347 		dprintf(("ntfs_mountfs: invalid boot block\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 	devvp->v_specmountpoint = 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 	devvp->v_specmountpoint = 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 		ntmp->ntm_devvp->v_specmountpoint = NULL;
    545 
    546 	vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0);
    547 
    548 	/* lock the device vnode before calling VOP_CLOSE() */
    549 	vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
    550 	error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
    551 		NOCRED);
    552 	KASSERT(error == 0);
    553 	VOP_UNLOCK(ntmp->ntm_devvp, 0);
    554 
    555 	vrele(ntmp->ntm_devvp);
    556 
    557 	/* free the toupper table, if this has been last mounted ntfs volume */
    558 	ntfs_toupper_unuse();
    559 
    560 	dprintf(("ntfs_umount: freeing memory...\n"));
    561 	mp->mnt_data = NULL;
    562 	mp->mnt_flag &= ~MNT_LOCAL;
    563 	free(ntmp->ntm_ad, M_NTFSMNT);
    564 	FREE(ntmp, M_NTFSMNT);
    565 	return (0);
    566 }
    567 
    568 static int
    569 ntfs_root(
    570 	struct mount *mp,
    571 	struct vnode **vpp)
    572 {
    573 	struct vnode *nvp;
    574 	int error = 0;
    575 
    576 	dprintf(("ntfs_root(): sysvn: %p\n",
    577 		VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
    578 	error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
    579 	if(error) {
    580 		printf("ntfs_root: VFS_VGET failed: %d\n",error);
    581 		return (error);
    582 	}
    583 
    584 	*vpp = nvp;
    585 	return (0);
    586 }
    587 
    588 int
    589 ntfs_calccfree(
    590 	struct ntfsmount *ntmp,
    591 	cn_t *cfreep)
    592 {
    593 	struct vnode *vp;
    594 	u_int8_t *tmp;
    595 	int j, error;
    596 	cn_t cfree = 0;
    597 	size_t bmsize, i;
    598 
    599 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
    600 
    601 	bmsize = VTOF(vp)->f_size;
    602 
    603 	tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
    604 
    605 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
    606 			       0, bmsize, tmp, NULL);
    607 	if (error)
    608 		goto out;
    609 
    610 	for(i=0;i<bmsize;i++)
    611 		for(j=0;j<8;j++)
    612 			if(~tmp[i] & (1 << j)) cfree++;
    613 	*cfreep = cfree;
    614 
    615     out:
    616 	free(tmp, M_TEMP);
    617 	return(error);
    618 }
    619 
    620 static int
    621 ntfs_statvfs(
    622 	struct mount *mp,
    623 	struct statvfs *sbp)
    624 {
    625 	struct ntfsmount *ntmp = VFSTONTFS(mp);
    626 	u_int64_t mftallocated;
    627 
    628 	dprintf(("ntfs_statvfs():\n"));
    629 
    630 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
    631 
    632 	sbp->f_bsize = ntmp->ntm_bps;
    633 	sbp->f_frsize = sbp->f_bsize; /* XXX */
    634 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
    635 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
    636 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
    637 	sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
    638 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
    639 	    sbp->f_ffree;
    640 	sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */
    641 	sbp->f_flag = mp->mnt_flag;
    642 	copy_statvfs_info(sbp, mp);
    643 	return (0);
    644 }
    645 
    646 static int
    647 ntfs_sync (
    648 	struct mount *mp,
    649 	int waitfor,
    650 	kauth_cred_t cred)
    651 {
    652 	/*dprintf(("ntfs_sync():\n"));*/
    653 	return (0);
    654 }
    655 
    656 /*ARGSUSED*/
    657 static int
    658 ntfs_fhtovp(
    659 	struct mount *mp,
    660 	struct fid *fhp,
    661 	struct vnode **vpp)
    662 {
    663 	struct ntfid ntfh;
    664 	int error;
    665 
    666 	if (fhp->fid_len != sizeof(struct ntfid))
    667 		return EINVAL;
    668 	memcpy(&ntfh, fhp, sizeof(ntfh));
    669 	ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname,
    670 	    (unsigned long long)ntfh.ntfid_ino));
    671 
    672 	error = ntfs_vgetex(mp, ntfh.ntfid_ino, ntfh.ntfid_attr, NULL,
    673 			LK_EXCLUSIVE | LK_RETRY, 0, vpp);
    674 	if (error != 0) {
    675 		*vpp = NULLVP;
    676 		return (error);
    677 	}
    678 
    679 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
    680 	 * with NTFS, we don't need to check anything else for now */
    681 	return (0);
    682 }
    683 
    684 static int
    685 ntfs_vptofh(
    686 	struct vnode *vp,
    687 	struct fid *fhp,
    688 	size_t *fh_size)
    689 {
    690 	struct ntnode *ntp;
    691 	struct ntfid ntfh;
    692 	struct fnode *fn;
    693 
    694 	if (*fh_size < sizeof(struct ntfid)) {
    695 		*fh_size = sizeof(struct ntfid);
    696 		return E2BIG;
    697 	}
    698 	*fh_size = sizeof(struct ntfid);
    699 
    700 	ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
    701 		vp));
    702 
    703 	fn = VTOF(vp);
    704 	ntp = VTONT(vp);
    705 	memset(&ntfh, 0, sizeof(ntfh));
    706 	ntfh.ntfid_len = sizeof(struct ntfid);
    707 	ntfh.ntfid_ino = ntp->i_number;
    708 	ntfh.ntfid_attr = fn->f_attrtype;
    709 #ifdef notyet
    710 	ntfh.ntfid_gen = ntp->i_gen;
    711 #endif
    712 	memcpy(fhp, &ntfh, sizeof(ntfh));
    713 	return (0);
    714 }
    715 
    716 int
    717 ntfs_vgetex(
    718 	struct mount *mp,
    719 	ino_t ino,
    720 	u_int32_t attrtype,
    721 	char *attrname,
    722 	u_long lkflags,
    723 	u_long flags,
    724 	struct vnode **vpp)
    725 {
    726 	int error;
    727 	struct ntfsmount *ntmp;
    728 	struct ntnode *ip;
    729 	struct fnode *fp;
    730 	struct vnode *vp;
    731 	enum vtype f_type = VBAD;
    732 
    733 	dprintf(("ntfs_vgetex: ino: %llu, attr: 0x%x:%s, lkf: 0x%lx, f:"
    734 	    " 0x%lx\n", (unsigned long long)ino, attrtype,
    735 	    attrname ? attrname : "", (u_long)lkflags, (u_long)flags));
    736 
    737 	ntmp = VFSTONTFS(mp);
    738 	*vpp = NULL;
    739 
    740 	/* Get ntnode */
    741 	error = ntfs_ntlookup(ntmp, ino, &ip);
    742 	if (error) {
    743 		printf("ntfs_vget: ntfs_ntget failed\n");
    744 		return (error);
    745 	}
    746 
    747 	/* It may be not initialized fully, so force load it */
    748 	if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
    749 		error = ntfs_loadntnode(ntmp, ip);
    750 		if(error) {
    751 			printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO:"
    752 			    " %llu\n", (unsigned long long)ip->i_number);
    753 			ntfs_ntput(ip);
    754 			return (error);
    755 		}
    756 	}
    757 
    758 	error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
    759 	if (error) {
    760 		printf("ntfs_vget: ntfs_fget failed\n");
    761 		ntfs_ntput(ip);
    762 		return (error);
    763 	}
    764 
    765 	if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
    766 		if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
    767 		    (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
    768 			f_type = VDIR;
    769 		} else if (flags & VG_EXT) {
    770 			f_type = VNON;
    771 			fp->f_size = fp->f_allocated = 0;
    772 		} else {
    773 			f_type = VREG;
    774 
    775 			error = ntfs_filesize(ntmp, fp,
    776 					      &fp->f_size, &fp->f_allocated);
    777 			if (error) {
    778 				ntfs_ntput(ip);
    779 				return (error);
    780 			}
    781 		}
    782 
    783 		fp->f_flag |= FN_VALID;
    784 	}
    785 
    786 	/*
    787 	 * We may be calling vget() now. To avoid potential deadlock, we need
    788 	 * to release ntnode lock, since due to locking order vnode
    789 	 * lock has to be acquired first.
    790 	 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled
    791 	 * prematurely.
    792 	 */
    793 	ntfs_ntput(ip);
    794 
    795 	if (FTOV(fp)) {
    796 		/* vget() returns error if the vnode has been recycled */
    797 		if (vget(FTOV(fp), lkflags) == 0) {
    798 			*vpp = FTOV(fp);
    799 			return (0);
    800 		}
    801 	}
    802 
    803 	error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
    804 	if(error) {
    805 		ntfs_frele(fp);
    806 		ntfs_ntput(ip);
    807 		return (error);
    808 	}
    809 	dprintf(("ntfs_vget: vnode: %p for ntnode: %llu\n", vp,
    810 	    (unsigned long long)ino));
    811 
    812 	fp->f_vp = vp;
    813 	vp->v_data = fp;
    814 	if (f_type != VBAD)
    815 		vp->v_type = f_type;
    816 	genfs_node_init(vp, &ntfs_genfsops);
    817 
    818 	if (ino == NTFS_ROOTINO)
    819 		vp->v_vflag |= VV_ROOT;
    820 
    821 	if (lkflags & LK_TYPE_MASK) {
    822 		error = vn_lock(vp, lkflags);
    823 		if (error) {
    824 			vput(vp);
    825 			return (error);
    826 		}
    827 	}
    828 
    829 	uvm_vnp_setsize(vp, fp->f_size); /* XXX: mess, cf. ntfs_lookupfile() */
    830 	VREF(ip->i_devvp);
    831 	*vpp = vp;
    832 	return (0);
    833 }
    834 
    835 static int
    836 ntfs_vget(
    837 	struct mount *mp,
    838 	ino_t ino,
    839 	struct vnode **vpp)
    840 {
    841 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
    842 			LK_EXCLUSIVE | LK_RETRY, 0, vpp);
    843 }
    844 
    845 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
    846 
    847 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
    848 	&ntfs_vnodeop_opv_desc,
    849 	NULL,
    850 };
    851 
    852 struct vfsops ntfs_vfsops = {
    853 	MOUNT_NTFS,
    854 	sizeof (struct ntfs_args),
    855 	ntfs_mount,
    856 	ntfs_start,
    857 	ntfs_unmount,
    858 	ntfs_root,
    859 	(void *)eopnotsupp,	/* vfs_quotactl */
    860 	ntfs_statvfs,
    861 	ntfs_sync,
    862 	ntfs_vget,
    863 	ntfs_fhtovp,
    864 	ntfs_vptofh,
    865 	ntfs_init,
    866 	ntfs_reinit,
    867 	ntfs_done,
    868 	ntfs_mountroot,
    869 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    870 	vfs_stdextattrctl,
    871 	(void *)eopnotsupp,		/* vfs_suspendctl */
    872 	genfs_renamelock_enter,
    873 	genfs_renamelock_exit,
    874 	(void *)eopnotsupp,
    875 	ntfs_vnodeopv_descs,
    876 	0,
    877 	{ NULL, NULL },
    878 };
    879 
    880 static int
    881 ntfs_modcmd(modcmd_t cmd, void *arg)
    882 {
    883 	int error;
    884 
    885 	switch (cmd) {
    886 	case MODULE_CMD_INIT:
    887 		error = vfs_attach(&ntfs_vfsops);
    888 		if (error != 0)
    889 			break;
    890 		sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL,
    891 			       CTLFLAG_PERMANENT,
    892 			       CTLTYPE_NODE, "vfs", NULL,
    893 			       NULL, 0, NULL, 0,
    894 			       CTL_VFS, CTL_EOL);
    895 		sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL,
    896 			       CTLFLAG_PERMANENT,
    897 			       CTLTYPE_NODE, "ntfs",
    898 			       SYSCTL_DESCR("NTFS file system"),
    899 			       NULL, 0, NULL, 0,
    900 			       CTL_VFS, 20, CTL_EOL);
    901 		/*
    902 		 * XXX the "20" above could be dynamic, thereby eliminating
    903 		 * one more instance of the "number to vfs" mapping problem,
    904 		 * but "20" is the order as taken from sys/mount.h
    905 		 */
    906 		break;
    907 	case MODULE_CMD_FINI:
    908 		error = vfs_detach(&ntfs_vfsops);
    909 		if (error != 0)
    910 			break;
    911 		sysctl_teardown(&ntfs_sysctl_log);
    912 		break;
    913 	default:
    914 		error = ENOTTY;
    915 		break;
    916 	}
    917 
    918 	return (error);
    919 }
    920