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