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