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