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