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