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