Home | History | Annotate | Line # | Download | only in ntfs
ntfs_vfsops.c revision 1.38
      1 /*	$NetBSD: ntfs_vfsops.c,v 1.38 2006/04/11 14:12:53 christos 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.38 2006/04/11 14:12:53 christos 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 	if (error) {
    577 		if (ntmp->ntm_ad)
    578 			free(ntmp->ntm_ad, M_NTFSMNT);
    579 		free(ntmp, M_NTFSMNT);
    580 	}
    581 
    582 	return (error);
    583 }
    584 
    585 static int
    586 ntfs_start (
    587 	struct mount *mp,
    588 	int flags,
    589 	struct lwp *l )
    590 {
    591 	return (0);
    592 }
    593 
    594 static int
    595 ntfs_unmount(
    596 	struct mount *mp,
    597 	int mntflags,
    598 	struct lwp *l)
    599 {
    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 		/* XXX should this be panic() ? */
    630 		printf("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
    631 	}
    632 
    633 	/* Check if the type of device node isn't VBAD before
    634 	 * touching v_specinfo.  If the device vnode is revoked, the
    635 	 * field is NULL and touching it causes null pointer derefercence.
    636 	 */
    637 	if (ntmp->ntm_devvp->v_type != VBAD)
    638 		ntmp->ntm_devvp->v_specmountpoint = NULL;
    639 
    640 	vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0);
    641 
    642 	/* lock the device vnode before calling VOP_CLOSE() */
    643 	vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
    644 	error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
    645 		NOCRED, l);
    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 (error);
    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 /*
    682  * Do operations associated with quotas, not supported
    683  */
    684 /* ARGSUSED */
    685 static int
    686 ntfs_quotactl (
    687 	struct mount *mp,
    688 	int cmds,
    689 	uid_t uid,
    690 	void *arg,
    691 	struct lwp *l)
    692 {
    693 
    694 	return EOPNOTSUPP;
    695 }
    696 
    697 int
    698 ntfs_calccfree(
    699 	struct ntfsmount *ntmp,
    700 	cn_t *cfreep)
    701 {
    702 	struct vnode *vp;
    703 	u_int8_t *tmp;
    704 	int j, error;
    705 	cn_t cfree = 0;
    706 	size_t bmsize, i;
    707 
    708 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
    709 
    710 	bmsize = VTOF(vp)->f_size;
    711 
    712 	tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
    713 
    714 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
    715 			       0, bmsize, tmp, NULL);
    716 	if (error)
    717 		goto out;
    718 
    719 	for(i=0;i<bmsize;i++)
    720 		for(j=0;j<8;j++)
    721 			if(~tmp[i] & (1 << j)) cfree++;
    722 	*cfreep = cfree;
    723 
    724     out:
    725 	free(tmp, M_TEMP);
    726 	return(error);
    727 }
    728 
    729 static int
    730 ntfs_statvfs(
    731 	struct mount *mp,
    732 	struct statvfs *sbp,
    733 	struct lwp *l)
    734 {
    735 	struct ntfsmount *ntmp = VFSTONTFS(mp);
    736 	u_int64_t mftallocated;
    737 
    738 	dprintf(("ntfs_statvfs():\n"));
    739 
    740 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
    741 
    742 #if defined(__FreeBSD__)
    743 	sbp->f_type = mp->mnt_vfc->vfc_typenum;
    744 #endif
    745 	sbp->f_bsize = ntmp->ntm_bps;
    746 	sbp->f_frsize = sbp->f_bsize; /* XXX */
    747 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
    748 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
    749 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
    750 	sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
    751 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
    752 	    sbp->f_ffree;
    753 	sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */
    754 	sbp->f_flag = mp->mnt_flag;
    755 	copy_statvfs_info(sbp, mp);
    756 	return (0);
    757 }
    758 
    759 static int
    760 ntfs_sync (
    761 	struct mount *mp,
    762 	int waitfor,
    763 	struct ucred *cred,
    764 	struct lwp *l)
    765 {
    766 	/*dprintf(("ntfs_sync():\n"));*/
    767 	return (0);
    768 }
    769 
    770 /*ARGSUSED*/
    771 static int
    772 ntfs_fhtovp(
    773 #if defined(__FreeBSD__)
    774 	struct mount *mp,
    775 	struct fid *fhp,
    776 	struct sockaddr *nam,
    777 	struct vnode **vpp,
    778 	int *exflagsp,
    779 	struct ucred **credanonp)
    780 #elif defined(__NetBSD__)
    781 	struct mount *mp,
    782 	struct fid *fhp,
    783 	struct vnode **vpp)
    784 #else
    785 	struct mount *mp,
    786 	struct fid *fhp,
    787 	struct mbuf *nam,
    788 	struct vnode **vpp,
    789 	int *exflagsp,
    790 	struct ucred **credanonp)
    791 #endif
    792 {
    793 	struct ntfid *ntfhp = (struct ntfid *)fhp;
    794 	int error;
    795 
    796 	ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname,
    797 	    (unsigned long long)ntfhp->ntfid_ino));
    798 
    799 	error = ntfs_vgetex(mp, ntfhp->ntfid_ino, ntfhp->ntfid_attr, NULL,
    800 			LK_EXCLUSIVE | LK_RETRY, 0, vpp);
    801 	if (error != 0) {
    802 		*vpp = NULLVP;
    803 		return (error);
    804 	}
    805 
    806 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
    807 	 * with NTFS, we don't need to check anything else for now */
    808 	return (0);
    809 }
    810 
    811 static int
    812 ntfs_vptofh(
    813 	struct vnode *vp,
    814 	struct fid *fhp)
    815 {
    816 	struct ntnode *ntp;
    817 	struct ntfid *ntfhp;
    818 	struct fnode *fn;
    819 
    820 	ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
    821 		vp));
    822 
    823 	fn = VTOF(vp);
    824 	ntp = VTONT(vp);
    825 	ntfhp = (struct ntfid *)fhp;
    826 	ntfhp->ntfid_len = sizeof(struct ntfid);
    827 	ntfhp->ntfid_ino = ntp->i_number;
    828 	ntfhp->ntfid_attr = fn->f_attrtype;
    829 #ifdef notyet
    830 	ntfhp->ntfid_gen = ntp->i_gen;
    831 #endif
    832 	return (0);
    833 }
    834 
    835 int
    836 ntfs_vgetex(
    837 	struct mount *mp,
    838 	ino_t ino,
    839 	u_int32_t attrtype,
    840 	char *attrname,
    841 	u_long lkflags,
    842 	u_long flags,
    843 	struct vnode **vpp)
    844 {
    845 	int error;
    846 	struct ntfsmount *ntmp;
    847 	struct ntnode *ip;
    848 	struct fnode *fp;
    849 	struct vnode *vp;
    850 	enum vtype f_type = VBAD;
    851 
    852 	dprintf(("ntfs_vgetex: ino: %llu, attr: 0x%x:%s, lkf: 0x%lx, f:"
    853 	    " 0x%lx\n", (unsigned long long)ino, attrtype,
    854 	    attrname ? attrname : "", (u_long)lkflags, (u_long)flags));
    855 
    856 	ntmp = VFSTONTFS(mp);
    857 	*vpp = NULL;
    858 
    859 	/* Get ntnode */
    860 	error = ntfs_ntlookup(ntmp, ino, &ip);
    861 	if (error) {
    862 		printf("ntfs_vget: ntfs_ntget failed\n");
    863 		return (error);
    864 	}
    865 
    866 	/* It may be not initialized fully, so force load it */
    867 	if (!(flags & VG_DONTLOADIN) && !(ip->i_flag & IN_LOADED)) {
    868 		error = ntfs_loadntnode(ntmp, ip);
    869 		if(error) {
    870 			printf("ntfs_vget: CAN'T LOAD ATTRIBUTES FOR INO:"
    871 			    " %llu\n", (unsigned long long)ip->i_number);
    872 			ntfs_ntput(ip);
    873 			return (error);
    874 		}
    875 	}
    876 
    877 	error = ntfs_fget(ntmp, ip, attrtype, attrname, &fp);
    878 	if (error) {
    879 		printf("ntfs_vget: ntfs_fget failed\n");
    880 		ntfs_ntput(ip);
    881 		return (error);
    882 	}
    883 
    884 	if (!(flags & VG_DONTVALIDFN) && !(fp->f_flag & FN_VALID)) {
    885 		if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
    886 		    (fp->f_attrtype == NTFS_A_DATA && fp->f_attrname == NULL)) {
    887 			f_type = VDIR;
    888 		} else if (flags & VG_EXT) {
    889 			f_type = VNON;
    890 			fp->f_size = fp->f_allocated = 0;
    891 		} else {
    892 			f_type = VREG;
    893 
    894 			error = ntfs_filesize(ntmp, fp,
    895 					      &fp->f_size, &fp->f_allocated);
    896 			if (error) {
    897 				ntfs_ntput(ip);
    898 				return (error);
    899 			}
    900 		}
    901 
    902 		fp->f_flag |= FN_VALID;
    903 	}
    904 
    905 	/*
    906 	 * We may be calling vget() now. To avoid potential deadlock, we need
    907 	 * to release ntnode lock, since due to locking order vnode
    908 	 * lock has to be acquired first.
    909 	 * ntfs_fget() bumped ntnode usecount, so ntnode won't be recycled
    910 	 * prematurely.
    911 	 */
    912 	ntfs_ntput(ip);
    913 
    914 	if (FTOV(fp)) {
    915 		/* vget() returns error if the vnode has been recycled */
    916 		if (vget(FTOV(fp), lkflags) == 0) {
    917 			*vpp = FTOV(fp);
    918 			return (0);
    919 		}
    920 	}
    921 
    922 	error = getnewvnode(VT_NTFS, ntmp->ntm_mountp, ntfs_vnodeop_p, &vp);
    923 	if(error) {
    924 		ntfs_frele(fp);
    925 		ntfs_ntput(ip);
    926 		return (error);
    927 	}
    928 	dprintf(("ntfs_vget: vnode: %p for ntnode: %llu\n", vp,
    929 	    (unsigned long long)ino));
    930 
    931 #ifdef __FreeBSD__
    932 	lockinit(&fp->f_lock, PINOD, "fnode", 0, 0);
    933 #endif
    934 	fp->f_vp = vp;
    935 	vp->v_data = fp;
    936 	if (f_type != VBAD)
    937 		vp->v_type = f_type;
    938 
    939 	if (ino == NTFS_ROOTINO)
    940 		vp->v_flag |= VROOT;
    941 
    942 	if (lkflags & LK_TYPE_MASK) {
    943 		error = vn_lock(vp, lkflags);
    944 		if (error) {
    945 			vput(vp);
    946 			return (error);
    947 		}
    948 	}
    949 
    950 	genfs_node_init(vp, &ntfs_genfsops);
    951 	VREF(ip->i_devvp);
    952 	*vpp = vp;
    953 	return (0);
    954 }
    955 
    956 static int
    957 ntfs_vget(
    958 	struct mount *mp,
    959 	ino_t ino,
    960 	struct vnode **vpp)
    961 {
    962 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, NULL,
    963 			LK_EXCLUSIVE | LK_RETRY, 0, vpp);
    964 }
    965 
    966 #if defined(__FreeBSD__)
    967 static struct vfsops ntfs_vfsops = {
    968 	ntfs_mount,
    969 	ntfs_start,
    970 	ntfs_unmount,
    971 	ntfs_root,
    972 	ntfs_quotactl,
    973 	ntfs_statvfs,
    974 	ntfs_sync,
    975 	ntfs_vget,
    976 	ntfs_fhtovp,
    977 	ntfs_vptofh,
    978 	ntfs_init,
    979 	NULL
    980 };
    981 VFS_SET(ntfs_vfsops, ntfs, 0);
    982 #elif defined(__NetBSD__)
    983 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
    984 
    985 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
    986 	&ntfs_vnodeop_opv_desc,
    987 	NULL,
    988 };
    989 
    990 struct vfsops ntfs_vfsops = {
    991 	MOUNT_NTFS,
    992 	ntfs_mount,
    993 	ntfs_start,
    994 	ntfs_unmount,
    995 	ntfs_root,
    996 	ntfs_quotactl,
    997 	ntfs_statvfs,
    998 	ntfs_sync,
    999 	ntfs_vget,
   1000 	ntfs_fhtovp,
   1001 	ntfs_vptofh,
   1002 	ntfs_init,
   1003 	ntfs_reinit,
   1004 	ntfs_done,
   1005 	ntfs_mountroot,
   1006 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
   1007 	vfs_stdextattrctl,
   1008 	ntfs_vnodeopv_descs,
   1009 };
   1010 VFS_ATTACH(ntfs_vfsops);
   1011 #endif
   1012