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