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