Home | History | Annotate | Line # | Download | only in ntfs
ntfs_vfsops.c revision 1.104.2.1
      1 /*	$NetBSD: ntfs_vfsops.c,v 1.104.2.1 2016/07/20 23:47:56 pgoyette 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.104.2.1 2016/07/20 23:47:56 pgoyette 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 #include <sys/module.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #include <miscfs/genfs/genfs.h>
     53 #include <miscfs/specfs/specdev.h>
     54 
     55 #include <fs/ntfs/ntfs.h>
     56 #include <fs/ntfs/ntfs_inode.h>
     57 #include <fs/ntfs/ntfs_subr.h>
     58 #include <fs/ntfs/ntfs_vfsops.h>
     59 #include <fs/ntfs/ntfs_ihash.h>
     60 #include <fs/ntfs/ntfsmount.h>
     61 
     62 MODULE(MODULE_CLASS_VFS, ntfs, NULL);
     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_NTFSDIR,"NTFS dir",  "NTFS dir buffer");
     67 
     68 static int	ntfs_superblock_validate(struct ntfsmount *);
     69 static int	ntfs_mount(struct mount *, const char *, void *, size_t *);
     70 static int	ntfs_root(struct mount *, struct vnode **);
     71 static int	ntfs_start(struct mount *, int);
     72 static int	ntfs_statvfs(struct mount *, struct statvfs *);
     73 static int	ntfs_sync(struct mount *, int, kauth_cred_t);
     74 static int	ntfs_unmount(struct mount *, int);
     75 static int	ntfs_vget(struct mount *mp, ino_t ino,
     76 			       struct vnode **vpp);
     77 static int	ntfs_loadvnode(struct mount *, struct vnode *,
     78 		                    const void *, size_t, const void **);
     79 static int	ntfs_mountfs(struct vnode *, struct mount *,
     80 				  struct ntfs_args *, struct lwp *);
     81 static int	ntfs_vptofh(struct vnode *, struct fid *, size_t *);
     82 
     83 static void	ntfs_init(void);
     84 static void	ntfs_reinit(void);
     85 static void	ntfs_done(void);
     86 static int	ntfs_fhtovp(struct mount *, struct fid *,
     87 				struct vnode **);
     88 static int	ntfs_mountroot(void);
     89 
     90 static const struct genfs_ops ntfs_genfsops = {
     91 	.gop_write = genfs_compat_gop_write,
     92 };
     93 
     94 static struct sysctllog *ntfs_sysctl_log;
     95 
     96 static int
     97 ntfs_mountroot(void)
     98 {
     99 	struct mount *mp;
    100 	struct lwp *l = curlwp;	/* XXX */
    101 	int error;
    102 	struct ntfs_args args;
    103 
    104 	if (device_class(root_device) != DV_DISK)
    105 		return (ENODEV);
    106 
    107 	if ((error = vfs_rootmountalloc(MOUNT_NTFS, "root_device", &mp))) {
    108 		vrele(rootvp);
    109 		return (error);
    110 	}
    111 
    112 	args.flag = 0;
    113 	args.uid = 0;
    114 	args.gid = 0;
    115 	args.mode = 0777;
    116 
    117 	if ((error = ntfs_mountfs(rootvp, mp, &args, l)) != 0) {
    118 		vfs_unbusy(mp, false, NULL);
    119 		vfs_destroy(mp);
    120 		return (error);
    121 	}
    122 
    123 	mountlist_append(mp);
    124 	(void)ntfs_statvfs(mp, &mp->mnt_stat);
    125 	vfs_unbusy(mp, false, NULL);
    126 	return (0);
    127 }
    128 
    129 static void
    130 ntfs_init(void)
    131 {
    132 
    133 	malloc_type_attach(M_NTFSMNT);
    134 	malloc_type_attach(M_NTFSNTNODE);
    135 	malloc_type_attach(M_NTFSDIR);
    136 	malloc_type_attach(M_NTFSNTVATTR);
    137 	malloc_type_attach(M_NTFSRDATA);
    138 	malloc_type_attach(M_NTFSDECOMP);
    139 	malloc_type_attach(M_NTFSRUN);
    140 	ntfs_nthashinit();
    141 	ntfs_toupper_init();
    142 }
    143 
    144 static void
    145 ntfs_reinit(void)
    146 {
    147 	ntfs_nthashreinit();
    148 }
    149 
    150 static void
    151 ntfs_done(void)
    152 {
    153 	ntfs_nthashdone();
    154 	malloc_type_detach(M_NTFSMNT);
    155 	malloc_type_detach(M_NTFSNTNODE);
    156 	malloc_type_detach(M_NTFSDIR);
    157 	malloc_type_detach(M_NTFSNTVATTR);
    158 	malloc_type_detach(M_NTFSRDATA);
    159 	malloc_type_detach(M_NTFSDECOMP);
    160 	malloc_type_detach(M_NTFSRUN);
    161 }
    162 
    163 static int
    164 ntfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    165 {
    166 	struct lwp *l = curlwp;
    167 	int		err = 0, flags;
    168 	struct vnode	*devvp;
    169 	struct ntfs_args *args = data;
    170 	const struct bdevsw *bdev = NULL;
    171 
    172 	if (args == NULL)
    173 		return EINVAL;
    174 	if (*data_len < sizeof *args)
    175 		return EINVAL;
    176 
    177 	if (mp->mnt_flag & MNT_GETARGS) {
    178 		struct ntfsmount *ntmp = VFSTONTFS(mp);
    179 		if (ntmp == NULL)
    180 			return EIO;
    181 		args->fspec = NULL;
    182 		args->uid = ntmp->ntm_uid;
    183 		args->gid = ntmp->ntm_gid;
    184 		args->mode = ntmp->ntm_mode;
    185 		args->flag = ntmp->ntm_flag;
    186 		*data_len = sizeof *args;
    187 		return 0;
    188 	}
    189 	/*
    190 	 ***
    191 	 * Mounting non-root file system or updating a file system
    192 	 ***
    193 	 */
    194 
    195 	/*
    196 	 * If updating, check whether changing from read-only to
    197 	 * read/write; if there is no device name, that's all we do.
    198 	 */
    199 	if (mp->mnt_flag & MNT_UPDATE) {
    200 		printf("ntfs_mount(): MNT_UPDATE not supported\n");
    201 		return (EINVAL);
    202 	}
    203 
    204 	/*
    205 	 * Not an update, or updating the name: look up the name
    206 	 * and verify that it refers to a sensible block device.
    207 	 */
    208 	err = namei_simple_user(args->fspec,
    209 				NSM_FOLLOW_NOEMULROOT, &devvp);
    210 	if (err)
    211 		return (err);
    212 
    213 	if (devvp->v_type != VBLK) {
    214 		err = ENOTBLK;
    215 		goto fail;
    216 	}
    217 	if ((bdev = bdevsw_lookup_acquire(devvp->v_rdev)) == NULL) {
    218 		err = ENXIO;
    219 		goto fail;
    220 	}
    221 	if (mp->mnt_flag & MNT_UPDATE) {
    222 #if 0
    223 		/*
    224 		 ********************
    225 		 * UPDATE
    226 		 ********************
    227 		 */
    228 
    229 		if (devvp != ntmp->um_devvp) {
    230 			err = EINVAL;	/* needs translation */
    231 			goto fail;
    232 		}
    233 
    234 		/*
    235 		 * Update device name only on success
    236 		 */
    237 		err = set_statvfs_info(NULL, UIO_USERSPACE, args->fspec,
    238 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, p);
    239 		if (err)
    240 			goto fail;
    241 
    242 		vrele(devvp);
    243 #endif
    244 	} else {
    245 		/*
    246 		 ********************
    247 		 * NEW MOUNT
    248 		 ********************
    249 		 */
    250 
    251 		/*
    252 		 * Since this is a new mount, we want the names for
    253 		 * the device and the mount point copied in.  If an
    254 		 * error occurs,  the mountpoint is discarded by the
    255 		 * upper level code.
    256 		 */
    257 
    258 		/* Save "last mounted on" info for mount point (NULL pad)*/
    259 		err = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
    260 		    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    261 		if (err)
    262 			goto fail;
    263 
    264 		if (mp->mnt_flag & MNT_RDONLY)
    265 			flags = FREAD;
    266 		else
    267 			flags = FREAD|FWRITE;
    268 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    269 		err = VOP_OPEN(devvp, flags, FSCRED);
    270 		VOP_UNLOCK(devvp);
    271 		if (err)
    272 			goto fail;
    273 		err = ntfs_mountfs(devvp, mp, args, l);
    274 		if (err) {
    275 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    276 			(void)VOP_CLOSE(devvp, flags, NOCRED);
    277 			VOP_UNLOCK(devvp);
    278 			goto fail;
    279 		}
    280 	}
    281 
    282 	/*
    283 	 * Initialize FS stat information in mount struct; uses both
    284 	 * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
    285 	 *
    286 	 * This code is common to root and non-root mounts
    287 	 */
    288 	(void)VFS_STATVFS(mp, &mp->mnt_stat);
    289 	if (bdev != NULL)
    290 		bdevsw_release(bdev);
    291 	return (err);
    292 
    293 fail:
    294 	vrele(devvp);
    295 	if (bdev != NULL)
    296 		bdevsw_release(bdev);
    297 	return (err);
    298 }
    299 
    300 static int
    301 ntfs_superblock_validate(struct ntfsmount *ntmp)
    302 {
    303 	/* Sanity checks. XXX: More checks are probably needed. */
    304 	if (strncmp(ntmp->ntm_bootfile.bf_sysid, NTFS_BBID, NTFS_BBIDLEN)) {
    305 		dprintf(("ntfs_superblock_validate: invalid boot block\n"));
    306 		return EINVAL;
    307 	}
    308 	if (ntmp->ntm_bps == 0) {
    309 		dprintf(("ntfs_superblock_validate: invalid bytes per sector\n"));
    310 		return EINVAL;
    311 	}
    312 	if (ntmp->ntm_spc == 0) {
    313 		dprintf(("ntfs_superblock_validate: invalid sectors per cluster\n"));
    314 		return EINVAL;
    315 	}
    316 	return 0;
    317 }
    318 
    319 /*
    320  * Common code for mount and mountroot
    321  */
    322 int
    323 ntfs_mountfs(struct vnode *devvp, struct mount *mp, struct ntfs_args *argsp, struct lwp *l)
    324 {
    325 	struct buf *bp;
    326 	struct ntfsmount *ntmp;
    327 	dev_t dev = devvp->v_rdev;
    328 	int error, i;
    329 	struct vnode *vp;
    330 
    331 	ntmp = NULL;
    332 
    333 	/*
    334 	 * Flush out any old buffers remaining from a previous use.
    335 	 */
    336 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    337 	error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
    338 	VOP_UNLOCK(devvp);
    339 	if (error)
    340 		return (error);
    341 
    342 	bp = NULL;
    343 
    344 	error = bread(devvp, BBLOCK, BBSIZE, 0, &bp);
    345 	if (error)
    346 		goto out;
    347 	ntmp = malloc(sizeof(*ntmp), M_NTFSMNT, M_WAITOK|M_ZERO);
    348 	memcpy(&ntmp->ntm_bootfile, bp->b_data, sizeof(struct bootfile));
    349 	brelse(bp, 0);
    350 	bp = NULL;
    351 
    352 	if ((error = ntfs_superblock_validate(ntmp)))
    353 		goto out;
    354 
    355 	{
    356 		int8_t cpr = ntmp->ntm_mftrecsz;
    357 		if (cpr > 0)
    358 			ntmp->ntm_bpmftrec = ntmp->ntm_spc * cpr;
    359 		else
    360 			ntmp->ntm_bpmftrec = (1 << (-cpr)) / ntmp->ntm_bps;
    361 	}
    362 	dprintf(("ntfs_mountfs(): bps: %d, spc: %d, media: %x, mftrecsz: %d (%d sects)\n",
    363 		ntmp->ntm_bps, ntmp->ntm_spc, ntmp->ntm_bootfile.bf_media,
    364 		ntmp->ntm_mftrecsz, ntmp->ntm_bpmftrec));
    365 	dprintf(("ntfs_mountfs(): mftcn: 0x%x|0x%x\n",
    366 		(u_int32_t)ntmp->ntm_mftcn, (u_int32_t)ntmp->ntm_mftmirrcn));
    367 
    368 	ntmp->ntm_mountp = mp;
    369 	ntmp->ntm_dev = dev;
    370 	ntmp->ntm_devvp = devvp;
    371 	ntmp->ntm_uid = argsp->uid;
    372 	ntmp->ntm_gid = argsp->gid;
    373 	ntmp->ntm_mode = argsp->mode;
    374 	ntmp->ntm_flag = argsp->flag;
    375 	mp->mnt_data = ntmp;
    376 
    377 	/* set file name encode/decode hooks XXX utf-8 only for now */
    378 	ntmp->ntm_wget = ntfs_utf8_wget;
    379 	ntmp->ntm_wput = ntfs_utf8_wput;
    380 	ntmp->ntm_wcmp = ntfs_utf8_wcmp;
    381 
    382 	dprintf(("ntfs_mountfs(): case-%s,%s uid: %d, gid: %d, mode: %o\n",
    383 		(ntmp->ntm_flag & NTFS_MFLAG_CASEINS)?"insens.":"sens.",
    384 		(ntmp->ntm_flag & NTFS_MFLAG_ALLNAMES)?" allnames,":"",
    385 		ntmp->ntm_uid, ntmp->ntm_gid, ntmp->ntm_mode));
    386 
    387 	/*
    388 	 * We read in some system nodes to do not allow
    389 	 * reclaim them and to have everytime access to them.
    390 	 */
    391 	{
    392 		int pi[3] = { NTFS_MFTINO, NTFS_ROOTINO, NTFS_BITMAPINO };
    393 		for (i = 0; i < 3; i++) {
    394 			error = VFS_VGET(mp, pi[i], &(ntmp->ntm_sysvn[pi[i]]));
    395 			if (error)
    396 				goto out1;
    397 			ntmp->ntm_sysvn[pi[i]]->v_vflag |= VV_SYSTEM;
    398 			vref(ntmp->ntm_sysvn[pi[i]]);
    399 			vput(ntmp->ntm_sysvn[pi[i]]);
    400 		}
    401 	}
    402 
    403 	/* read the Unicode lowercase --> uppercase translation table,
    404 	 * if necessary */
    405 	if ((error = ntfs_toupper_use(mp, ntmp)))
    406 		goto out1;
    407 
    408 	/*
    409 	 * Scan $BitMap and count free clusters
    410 	 */
    411 	error = ntfs_calccfree(ntmp, &ntmp->ntm_cfree);
    412 	if (error)
    413 		goto out1;
    414 
    415 	/*
    416 	 * Read and translate to internal format attribute
    417 	 * definition file.
    418 	 */
    419 	{
    420 		int num,j;
    421 		struct attrdef ad;
    422 
    423 		/* Open $AttrDef */
    424 		error = VFS_VGET(mp, NTFS_ATTRDEFINO, &vp);
    425 		if (error)
    426 			goto out1;
    427 
    428 		/* Count valid entries */
    429 		for (num = 0; ; num++) {
    430 			error = ntfs_readattr(ntmp, VTONT(vp),
    431 					NTFS_A_DATA, NULL,
    432 					num * sizeof(ad), sizeof(ad),
    433 					&ad, NULL);
    434 			if (error)
    435 				goto out1;
    436 			if (ad.ad_name[0] == 0)
    437 				break;
    438 		}
    439 
    440 		/* Alloc memory for attribute definitions */
    441 		ntmp->ntm_ad = (struct ntvattrdef *) malloc(
    442 			num * sizeof(struct ntvattrdef),
    443 			M_NTFSMNT, M_WAITOK);
    444 
    445 		ntmp->ntm_adnum = num;
    446 
    447 		/* Read them and translate */
    448 		for (i = 0; i < num; i++) {
    449 			error = ntfs_readattr(ntmp, VTONT(vp),
    450 					NTFS_A_DATA, NULL,
    451 					i * sizeof(ad), sizeof(ad),
    452 					&ad, NULL);
    453 			if (error)
    454 				goto out1;
    455 			j = 0;
    456 			do {
    457 				ntmp->ntm_ad[i].ad_name[j] = ad.ad_name[j];
    458 			} while(ad.ad_name[j++]);
    459 			ntmp->ntm_ad[i].ad_namelen = j - 1;
    460 			ntmp->ntm_ad[i].ad_type = ad.ad_type;
    461 		}
    462 
    463 		vput(vp);
    464 	}
    465 
    466 	mp->mnt_stat.f_fsidx.__fsid_val[0] = dev;
    467 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NTFS);
    468 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    469 	mp->mnt_stat.f_namemax = NTFS_MAXFILENAME;
    470 	mp->mnt_flag |= MNT_LOCAL;
    471 	spec_node_setmountedfs(devvp, mp);
    472 	return (0);
    473 
    474 out1:
    475 	for (i = 0; i < NTFS_SYSNODESNUM; i++)
    476 		if (ntmp->ntm_sysvn[i])
    477 			vrele(ntmp->ntm_sysvn[i]);
    478 
    479 	if (vflush(mp, NULLVP, 0)) {
    480 		dprintf(("ntfs_mountfs: vflush failed\n"));
    481 	}
    482 out:
    483 	spec_node_setmountedfs(devvp, NULL);
    484 	if (bp)
    485 		brelse(bp, 0);
    486 
    487 	if (error) {
    488 		if (ntmp) {
    489 			if (ntmp->ntm_ad)
    490 				free(ntmp->ntm_ad, M_NTFSMNT);
    491 			free(ntmp, M_NTFSMNT);
    492 		}
    493 	}
    494 
    495 	return (error);
    496 }
    497 
    498 static int
    499 ntfs_start(struct mount *mp, int flags)
    500 {
    501 	return (0);
    502 }
    503 
    504 static int
    505 ntfs_unmount(struct mount *mp, int mntflags)
    506 {
    507 	struct lwp *l = curlwp;
    508 	struct ntfsmount *ntmp;
    509 	int error, ronly = 0, flags, i;
    510 
    511 	dprintf(("ntfs_unmount: unmounting...\n"));
    512 	ntmp = VFSTONTFS(mp);
    513 
    514 	flags = 0;
    515 	if (mntflags & MNT_FORCE)
    516 		flags |= FORCECLOSE;
    517 
    518 	dprintf(("ntfs_unmount: vflushing...\n"));
    519 	error = vflush(mp, NULLVP, flags | SKIPSYSTEM);
    520 	if (error) {
    521 		dprintf(("ntfs_unmount: vflush failed: %d\n",error));
    522 		return (error);
    523 	}
    524 
    525 	/* Check if only system vnodes are rest */
    526 	for (i = 0; i < NTFS_SYSNODESNUM; i++)
    527 		if ((ntmp->ntm_sysvn[i]) &&
    528 		    (ntmp->ntm_sysvn[i]->v_usecount > 1))
    529 			return (EBUSY);
    530 
    531 	/* Dereference all system vnodes */
    532 	for (i = 0; i < NTFS_SYSNODESNUM; i++)
    533 		if (ntmp->ntm_sysvn[i])
    534 			vrele(ntmp->ntm_sysvn[i]);
    535 
    536 	/* vflush system vnodes */
    537 	error = vflush(mp, NULLVP, flags);
    538 	if (error) {
    539 		panic("ntfs_unmount: vflush failed(sysnodes): %d\n",error);
    540 	}
    541 
    542 	/* Check if the type of device node isn't VBAD before
    543 	 * touching v_specinfo.  If the device vnode is revoked, the
    544 	 * field is NULL and touching it causes null pointer derefercence.
    545 	 */
    546 	if (ntmp->ntm_devvp->v_type != VBAD)
    547 		spec_node_setmountedfs(ntmp->ntm_devvp, NULL);
    548 
    549 	error = vinvalbuf(ntmp->ntm_devvp, V_SAVE, NOCRED, l, 0, 0);
    550 	KASSERT(error == 0);
    551 
    552 	/* lock the device vnode before calling VOP_CLOSE() */
    553 	vn_lock(ntmp->ntm_devvp, LK_EXCLUSIVE | LK_RETRY);
    554 	error = VOP_CLOSE(ntmp->ntm_devvp, ronly ? FREAD : FREAD|FWRITE,
    555 		NOCRED);
    556 	KASSERT(error == 0);
    557 	VOP_UNLOCK(ntmp->ntm_devvp);
    558 
    559 	vrele(ntmp->ntm_devvp);
    560 
    561 	/* free the toupper table, if this has been last mounted ntfs volume */
    562 	ntfs_toupper_unuse();
    563 
    564 	dprintf(("ntfs_umount: freeing memory...\n"));
    565 	mp->mnt_data = NULL;
    566 	mp->mnt_flag &= ~MNT_LOCAL;
    567 	free(ntmp->ntm_ad, M_NTFSMNT);
    568 	free(ntmp, M_NTFSMNT);
    569 	return (0);
    570 }
    571 
    572 static int
    573 ntfs_root(struct mount *mp, struct vnode **vpp)
    574 {
    575 	struct vnode *nvp;
    576 	int error = 0;
    577 
    578 	dprintf(("ntfs_root(): sysvn: %p\n",
    579 		VFSTONTFS(mp)->ntm_sysvn[NTFS_ROOTINO]));
    580 	error = VFS_VGET(mp, (ino_t)NTFS_ROOTINO, &nvp);
    581 	if (error) {
    582 		printf("ntfs_root: VFS_VGET failed: %d\n", error);
    583 		return (error);
    584 	}
    585 
    586 	*vpp = nvp;
    587 	return (0);
    588 }
    589 
    590 int
    591 ntfs_calccfree(struct ntfsmount *ntmp, cn_t *cfreep)
    592 {
    593 	struct vnode *vp;
    594 	u_int8_t *tmp;
    595 	int j, error;
    596 	cn_t cfree = 0;
    597 	size_t bmsize, i;
    598 
    599 	vp = ntmp->ntm_sysvn[NTFS_BITMAPINO];
    600 	bmsize = VTOF(vp)->f_size;
    601 	tmp = (u_int8_t *) malloc(bmsize, M_TEMP, M_WAITOK);
    602 
    603 	error = ntfs_readattr(ntmp, VTONT(vp), NTFS_A_DATA, NULL,
    604 	    0, bmsize, tmp, NULL);
    605 	if (error)
    606 		goto out;
    607 
    608 	for (i = 0; i < bmsize; i++)
    609 		for (j = 0; j < 8; j++)
    610 			if (~tmp[i] & (1 << j))
    611 				cfree++;
    612 	*cfreep = cfree;
    613 
    614 out:
    615 	free(tmp, M_TEMP);
    616 	return(error);
    617 }
    618 
    619 static int
    620 ntfs_statvfs(struct mount *mp, struct statvfs *sbp)
    621 {
    622 	struct ntfsmount *ntmp = VFSTONTFS(mp);
    623 	u_int64_t mftallocated;
    624 
    625 	dprintf(("ntfs_statvfs():\n"));
    626 
    627 	mftallocated = VTOF(ntmp->ntm_sysvn[NTFS_MFTINO])->f_allocated;
    628 
    629 	sbp->f_bsize = ntmp->ntm_bps;
    630 	sbp->f_frsize = sbp->f_bsize; /* XXX */
    631 	sbp->f_iosize = ntmp->ntm_bps * ntmp->ntm_spc;
    632 	sbp->f_blocks = ntmp->ntm_bootfile.bf_spv;
    633 	sbp->f_bfree = sbp->f_bavail = ntfs_cntobn(ntmp->ntm_cfree);
    634 	sbp->f_ffree = sbp->f_favail = sbp->f_bfree / ntmp->ntm_bpmftrec;
    635 	sbp->f_files = mftallocated / ntfs_bntob(ntmp->ntm_bpmftrec) +
    636 	    sbp->f_ffree;
    637 	sbp->f_fresvd = sbp->f_bresvd = 0; /* XXX */
    638 	sbp->f_flag = mp->mnt_flag;
    639 	copy_statvfs_info(sbp, mp);
    640 	return (0);
    641 }
    642 
    643 static int
    644 ntfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    645 {
    646 	/*dprintf(("ntfs_sync():\n"));*/
    647 	return (0);
    648 }
    649 
    650 /*ARGSUSED*/
    651 static int
    652 ntfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    653 {
    654 	struct ntfid ntfh;
    655 	int error;
    656 
    657 	if (fhp->fid_len != sizeof(struct ntfid))
    658 		return EINVAL;
    659 	memcpy(&ntfh, fhp, sizeof(ntfh));
    660 	ddprintf(("ntfs_fhtovp(): %s: %llu\n", mp->mnt_stat.f_mntonname,
    661 	    (unsigned long long)ntfh.ntfid_ino));
    662 
    663 	error = ntfs_vgetex(mp, ntfh.ntfid_ino, ntfh.ntfid_attr, "",
    664 			LK_EXCLUSIVE, vpp);
    665 	if (error != 0) {
    666 		*vpp = NULLVP;
    667 		return (error);
    668 	}
    669 
    670 	/* XXX as unlink/rmdir/mkdir/creat are not currently possible
    671 	 * with NTFS, we don't need to check anything else for now */
    672 	return (0);
    673 }
    674 
    675 static int
    676 ntfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    677 {
    678 	struct ntnode *ntp;
    679 	struct ntfid ntfh;
    680 	struct fnode *fn;
    681 
    682 	if (*fh_size < sizeof(struct ntfid)) {
    683 		*fh_size = sizeof(struct ntfid);
    684 		return E2BIG;
    685 	}
    686 	*fh_size = sizeof(struct ntfid);
    687 
    688 	ddprintf(("ntfs_fhtovp(): %s: %p\n", vp->v_mount->mnt_stat.f_mntonname,
    689 		vp));
    690 
    691 	fn = VTOF(vp);
    692 	ntp = VTONT(vp);
    693 	memset(&ntfh, 0, sizeof(ntfh));
    694 	ntfh.ntfid_len = sizeof(struct ntfid);
    695 	ntfh.ntfid_ino = ntp->i_number;
    696 	ntfh.ntfid_attr = fn->f_attrtype;
    697 #ifdef notyet
    698 	ntfh.ntfid_gen = ntp->i_gen;
    699 #endif
    700 	memcpy(fhp, &ntfh, sizeof(ntfh));
    701 	return (0);
    702 }
    703 
    704 static int
    705 ntfs_loadvnode(struct mount *mp, struct vnode *vp,
    706     const void *key, size_t key_len, const void **new_key)
    707 {
    708 	int error;
    709 	struct ntvattr *vap;
    710 	struct ntkey small_key, *ntkey;
    711 	struct ntfsmount *ntmp;
    712 	struct ntnode *ip;
    713 	struct fnode *fp = NULL;
    714 	enum vtype f_type = VBAD;
    715 
    716 	if (key_len <= sizeof(small_key))
    717 		ntkey = &small_key;
    718 	else
    719 		ntkey = kmem_alloc(key_len, KM_SLEEP);
    720 	memcpy(ntkey, key, key_len);
    721 
    722 	dprintf(("ntfs_loadvnode: ino: %llu, attr: 0x%x:%s",
    723 	    (unsigned long long)ntkey->k_ino,
    724 	    ntkey->k_attrtype, ntkey->k_attrname));
    725 
    726 	ntmp = VFSTONTFS(mp);
    727 
    728 	/* Get ntnode */
    729 	error = ntfs_ntlookup(ntmp, ntkey->k_ino, &ip);
    730 	if (error) {
    731 		printf("ntfs_loadvnode: ntfs_ntget failed\n");
    732 		goto out;
    733 	}
    734 	/* It may be not initialized fully, so force load it */
    735 	if (!(ip->i_flag & IN_LOADED)) {
    736 		error = ntfs_loadntnode(ntmp, ip);
    737 		if (error) {
    738 			printf("ntfs_loadvnode: CAN'T LOAD ATTRIBUTES FOR INO:"
    739 			    " %llu\n", (unsigned long long)ip->i_number);
    740 			ntfs_ntput(ip);
    741 			goto out;
    742 		}
    743 	}
    744 
    745 	/* Setup fnode */
    746 	fp = kmem_zalloc(sizeof(*fp), KM_SLEEP);
    747 	dprintf(("%s: allocating fnode: %p\n", __func__, fp));
    748 
    749 	error = ntfs_ntvattrget(ntmp, ip, NTFS_A_NAME, NULL, 0, &vap);
    750 	if (error) {
    751 		printf("%s: attr %x for ino %" PRId64 ": error %d\n",
    752 		    __func__, NTFS_A_NAME, ip->i_number, error);
    753 		ntfs_ntput(ip);
    754 		goto out;
    755 	}
    756 	fp->f_fflag = vap->va_a_name->n_flag;
    757 	fp->f_pnumber = vap->va_a_name->n_pnumber;
    758 	fp->f_times = vap->va_a_name->n_times;
    759 	ntfs_ntvattrrele(vap);
    760 
    761 	if ((ip->i_frflag & NTFS_FRFLAG_DIR) &&
    762 	    (ntkey->k_attrtype == NTFS_A_DATA &&
    763 	    strcmp(ntkey->k_attrname, "") == 0)) {
    764 		f_type = VDIR;
    765 	} else {
    766 		f_type = VREG;
    767 		error = ntfs_ntvattrget(ntmp, ip,
    768 		    ntkey->k_attrtype, ntkey->k_attrname, 0, &vap);
    769 		if (error == 0) {
    770 			fp->f_size = vap->va_datalen;
    771 			fp->f_allocated = vap->va_allocated;
    772 			ntfs_ntvattrrele(vap);
    773 		} else if (ntkey->k_attrtype == NTFS_A_DATA &&
    774 		    strcmp(ntkey->k_attrname, "") == 0 &&
    775 		    error == ENOENT) {
    776 			fp->f_size = 0;
    777 			fp->f_allocated = 0;
    778 			error = 0;
    779 		} else {
    780 			printf("%s: attr %x for ino %" PRId64 ": error %d\n",
    781 			    __func__, ntkey->k_attrtype, ip->i_number, error);
    782 			ntfs_ntput(ip);
    783 			goto out;
    784 		}
    785 	}
    786 
    787 	if (key_len <= sizeof(fp->f_smallkey))
    788 		fp->f_key = &fp->f_smallkey;
    789 	else
    790 		fp->f_key = kmem_alloc(key_len, KM_SLEEP);
    791 	fp->f_ip = ip;
    792 	fp->f_ino = ip->i_number;
    793 	strcpy(fp->f_attrname, ntkey->k_attrname);
    794 	fp->f_attrtype = ntkey->k_attrtype;
    795 	fp->f_vp = vp;
    796 	vp->v_data = fp;
    797 
    798 	vp->v_tag = VT_NTFS;
    799 	vp->v_type = f_type;
    800 	vp->v_op = ntfs_vnodeop_p;
    801 	ntfs_ntref(ip);
    802 	vref(ip->i_devvp);
    803 	genfs_node_init(vp, &ntfs_genfsops);
    804 
    805 	if (ip->i_number == NTFS_ROOTINO)
    806 		vp->v_vflag |= VV_ROOT;
    807 
    808 	uvm_vnp_setsize(vp, fp->f_size);
    809 	ntfs_ntput(ip);
    810 
    811 	*new_key = fp->f_key;
    812 
    813 	fp = NULL;
    814 
    815 out:
    816 	if (ntkey != &small_key)
    817 		kmem_free(ntkey, key_len);
    818 	if (fp)
    819 		kmem_free(fp, sizeof(*fp));
    820 
    821 	return error;
    822 }
    823 
    824 static int
    825 ntfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    826 {
    827 	return ntfs_vgetex(mp, ino, NTFS_A_DATA, "", LK_EXCLUSIVE, vpp);
    828 }
    829 
    830 int
    831 ntfs_vgetex(struct mount *mp, ino_t ino, u_int32_t attrtype,
    832     const char *attrname, u_long lkflags, struct vnode **vpp)
    833 {
    834 	const int attrlen = strlen(attrname);
    835 	int error;
    836 	struct ntkey small_key, *ntkey;
    837 
    838 	if (NTKEY_SIZE(attrlen) <= sizeof(small_key))
    839 		ntkey = &small_key;
    840 	else
    841 		ntkey = malloc(NTKEY_SIZE(attrlen), M_TEMP, M_WAITOK);
    842 	ntkey->k_ino = ino;
    843 	ntkey->k_attrtype = attrtype;
    844 	strcpy(ntkey->k_attrname, attrname);
    845 
    846 	error = vcache_get(mp, ntkey, NTKEY_SIZE(attrlen), vpp);
    847 	if (error)
    848 		goto out;
    849 
    850 	if ((lkflags & (LK_SHARED | LK_EXCLUSIVE)) != 0) {
    851 		error = vn_lock(*vpp, lkflags);
    852 		if (error) {
    853 			vrele(*vpp);
    854 			*vpp = NULL;
    855 		}
    856 	}
    857 
    858 out:
    859 	if (ntkey != &small_key)
    860 		free(ntkey, M_TEMP);
    861 	return error;
    862 }
    863 
    864 extern const struct vnodeopv_desc ntfs_vnodeop_opv_desc;
    865 
    866 const struct vnodeopv_desc * const ntfs_vnodeopv_descs[] = {
    867 	&ntfs_vnodeop_opv_desc,
    868 	NULL,
    869 };
    870 
    871 struct vfsops ntfs_vfsops = {
    872 	.vfs_name = MOUNT_NTFS,
    873 	.vfs_min_mount_data = sizeof (struct ntfs_args),
    874 	.vfs_mount = ntfs_mount,
    875 	.vfs_start = ntfs_start,
    876 	.vfs_unmount = ntfs_unmount,
    877 	.vfs_root = ntfs_root,
    878 	.vfs_quotactl = (void *)eopnotsupp,
    879 	.vfs_statvfs = ntfs_statvfs,
    880 	.vfs_sync = ntfs_sync,
    881 	.vfs_vget = ntfs_vget,
    882 	.vfs_loadvnode = ntfs_loadvnode,
    883 	.vfs_fhtovp = ntfs_fhtovp,
    884 	.vfs_vptofh = ntfs_vptofh,
    885 	.vfs_init = ntfs_init,
    886 	.vfs_reinit = ntfs_reinit,
    887 	.vfs_done = ntfs_done,
    888 	.vfs_mountroot = ntfs_mountroot,
    889 	.vfs_snapshot = (void *)eopnotsupp,
    890 	.vfs_extattrctl = vfs_stdextattrctl,
    891 	.vfs_suspendctl = (void *)eopnotsupp,
    892 	.vfs_renamelock_enter = genfs_renamelock_enter,
    893 	.vfs_renamelock_exit = genfs_renamelock_exit,
    894 	.vfs_fsync = (void *)eopnotsupp,
    895 	.vfs_opv_descs = ntfs_vnodeopv_descs
    896 };
    897 
    898 static int
    899 ntfs_modcmd(modcmd_t cmd, void *arg)
    900 {
    901 	int error;
    902 
    903 	switch (cmd) {
    904 	case MODULE_CMD_INIT:
    905 		error = vfs_attach(&ntfs_vfsops);
    906 		if (error != 0)
    907 			break;
    908 		sysctl_createv(&ntfs_sysctl_log, 0, NULL, NULL,
    909 			       CTLFLAG_PERMANENT,
    910 			       CTLTYPE_NODE, "ntfs",
    911 			       SYSCTL_DESCR("NTFS file system"),
    912 			       NULL, 0, NULL, 0,
    913 			       CTL_VFS, 20, CTL_EOL);
    914 		/*
    915 		 * XXX the "20" above could be dynamic, thereby eliminating
    916 		 * one more instance of the "number to vfs" mapping problem,
    917 		 * but "20" is the order as taken from sys/mount.h
    918 		 */
    919 		break;
    920 	case MODULE_CMD_FINI:
    921 		error = vfs_detach(&ntfs_vfsops);
    922 		if (error != 0)
    923 			break;
    924 		sysctl_teardown(&ntfs_sysctl_log);
    925 		break;
    926 	default:
    927 		error = ENOTTY;
    928 		break;
    929 	}
    930 
    931 	return (error);
    932 }
    933