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