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