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