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