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