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