xref: /netbsd-src/sys/fs/hfs/hfs_vfsops.c (revision 9ea3b23dafd978027b28369eda07bf00fb169b23)
1*9ea3b23dShannken /*	$NetBSD: hfs_vfsops.c,v 1.38 2022/03/19 13:53:32 hannken Exp $	*/
225e99827Sdillo 
325e99827Sdillo /*-
425e99827Sdillo  * Copyright (c) 2005, 2007 The NetBSD Foundation, Inc.
525e99827Sdillo  * All rights reserved.
625e99827Sdillo  *
725e99827Sdillo  * This code is derived from software contributed to The NetBSD Foundation
825e99827Sdillo  * by Yevgeny Binder and Dieter Baron.
925e99827Sdillo  *
1025e99827Sdillo  * Redistribution and use in source and binary forms, with or without
1125e99827Sdillo  * modification, are permitted provided that the following conditions
1225e99827Sdillo  * are met:
1325e99827Sdillo  * 1. Redistributions of source code must retain the above copyright
1425e99827Sdillo  *    notice, this list of conditions and the following disclaimer.
1525e99827Sdillo  * 2. Redistributions in binary form must reproduce the above copyright
1625e99827Sdillo  *    notice, this list of conditions and the following disclaimer in the
1725e99827Sdillo  *    documentation and/or other materials provided with the distribution.
1825e99827Sdillo  *
1925e99827Sdillo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2025e99827Sdillo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2125e99827Sdillo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2225e99827Sdillo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2325e99827Sdillo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2425e99827Sdillo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2525e99827Sdillo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2625e99827Sdillo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2725e99827Sdillo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2825e99827Sdillo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2925e99827Sdillo  * POSSIBILITY OF SUCH DAMAGE.
3025e99827Sdillo  */
3125e99827Sdillo 
3225e99827Sdillo /*
3325e99827Sdillo  * Copyright (c) 1991, 1993, 1994
3425e99827Sdillo  *	The Regents of the University of California.  All rights reserved.
3525e99827Sdillo  * (c) UNIX System Laboratories, Inc.
3625e99827Sdillo  * All or some portions of this file are derived from material licensed
3725e99827Sdillo  * to the University of California by American Telephone and Telegraph
3825e99827Sdillo  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
3925e99827Sdillo  * the permission of UNIX System Laboratories, Inc.
4025e99827Sdillo  *
4125e99827Sdillo  * Redistribution and use in source and binary forms, with or without
4225e99827Sdillo  * modification, are permitted provided that the following conditions
4325e99827Sdillo  * are met:
4425e99827Sdillo  * 1. Redistributions of source code must retain the above copyright
4525e99827Sdillo  *    notice, this list of conditions and the following disclaimer.
4625e99827Sdillo  * 2. Redistributions in binary form must reproduce the above copyright
4725e99827Sdillo  *    notice, this list of conditions and the following disclaimer in the
4825e99827Sdillo  *    documentation and/or other materials provided with the distribution.
4925e99827Sdillo  * 3. Neither the name of the University nor the names of its contributors
5025e99827Sdillo  *    may be used to endorse or promote products derived from this software
5125e99827Sdillo  *    without specific prior written permission.
5225e99827Sdillo  *
5325e99827Sdillo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5425e99827Sdillo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5525e99827Sdillo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5625e99827Sdillo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5725e99827Sdillo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5825e99827Sdillo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5925e99827Sdillo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6025e99827Sdillo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6125e99827Sdillo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6225e99827Sdillo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6325e99827Sdillo  * SUCH DAMAGE.
6425e99827Sdillo  */
6525e99827Sdillo 
6625e99827Sdillo  /*
6725e99827Sdillo  * Copyright (c) 1989, 1991, 1993, 1994
6825e99827Sdillo  *	The Regents of the University of California.  All rights reserved.
6925e99827Sdillo  *
7025e99827Sdillo  * Redistribution and use in source and binary forms, with or without
7125e99827Sdillo  * modification, are permitted provided that the following conditions
7225e99827Sdillo  * are met:
7325e99827Sdillo  * 1. Redistributions of source code must retain the above copyright
7425e99827Sdillo  *    notice, this list of conditions and the following disclaimer.
7525e99827Sdillo  * 2. Redistributions in binary form must reproduce the above copyright
7625e99827Sdillo  *    notice, this list of conditions and the following disclaimer in the
7725e99827Sdillo  *    documentation and/or other materials provided with the distribution.
7825e99827Sdillo  * 3. Neither the name of the University nor the names of its contributors
7925e99827Sdillo  *    may be used to endorse or promote products derived from this software
8025e99827Sdillo  *    without specific prior written permission.
8125e99827Sdillo  *
8225e99827Sdillo  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
8325e99827Sdillo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8425e99827Sdillo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8525e99827Sdillo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
8625e99827Sdillo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8725e99827Sdillo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
8825e99827Sdillo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8925e99827Sdillo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9025e99827Sdillo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9125e99827Sdillo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9225e99827Sdillo  * SUCH DAMAGE.
9325e99827Sdillo  */
9425e99827Sdillo 
9525e99827Sdillo 
9625e99827Sdillo 
9725e99827Sdillo /*
9825e99827Sdillo  * Apple HFS+ filesystem
9925e99827Sdillo  */
10025e99827Sdillo 
10125e99827Sdillo #include <sys/cdefs.h>
102*9ea3b23dShannken __KERNEL_RCSID(0, "$NetBSD: hfs_vfsops.c,v 1.38 2022/03/19 13:53:32 hannken Exp $");
10325e99827Sdillo 
10425e99827Sdillo #ifdef _KERNEL_OPT
10525e99827Sdillo #include "opt_compat_netbsd.h"
10625e99827Sdillo #endif
10725e99827Sdillo 
10825e99827Sdillo #include <sys/param.h>
10925e99827Sdillo #include <sys/systm.h>
11025e99827Sdillo #include <sys/namei.h>
11125e99827Sdillo #include <sys/proc.h>
11225e99827Sdillo #include <sys/kernel.h>
11325e99827Sdillo #include <sys/vnode.h>
11425e99827Sdillo #include <sys/socket.h>
11525e99827Sdillo #include <sys/mount.h>
11625e99827Sdillo #include <sys/buf.h>
11725e99827Sdillo #include <sys/device.h>
11825e99827Sdillo #include <sys/file.h>
11925e99827Sdillo #include <sys/disklabel.h>
12025e99827Sdillo #include <sys/ioctl.h>
12125e99827Sdillo #include <sys/errno.h>
12225e99827Sdillo #include <sys/malloc.h>
12325e99827Sdillo #include <sys/pool.h>
12425e99827Sdillo #include <sys/lock.h>
12525e99827Sdillo #include <sys/sysctl.h>
12625e99827Sdillo #include <sys/conf.h>
12725e99827Sdillo #include <sys/kauth.h>
12825e99827Sdillo #include <sys/stat.h>
129a1221b6dSrumble #include <sys/module.h>
13025e99827Sdillo 
131717e1785Sdholland #include <miscfs/genfs/genfs.h>
13225e99827Sdillo #include <miscfs/specfs/specdev.h>
13325e99827Sdillo 
13456c3e412Sdillo #include <fs/hfs/hfs.h>
13556c3e412Sdillo #include <fs/hfs/libhfs.h>
13625e99827Sdillo 
137a1221b6dSrumble MODULE(MODULE_CLASS_VFS, hfs, NULL);
138a1221b6dSrumble 
139835b0326Spooka MALLOC_JUSTDEFINE(M_HFSMNT, "hfs mount", "hfs mount structures");
14025e99827Sdillo 
141265f1d8cShannken struct pool hfs_node_pool;
14225e99827Sdillo 
14356c3e412Sdillo const struct vnodeopv_desc * const hfs_vnodeopv_descs[] = {
14456c3e412Sdillo 	&hfs_vnodeop_opv_desc,
14556c3e412Sdillo 	&hfs_specop_opv_desc,
14656c3e412Sdillo 	&hfs_fifoop_opv_desc,
14725e99827Sdillo 	NULL,
14825e99827Sdillo };
14925e99827Sdillo 
15056c3e412Sdillo struct vfsops hfs_vfsops = {
1516d285189Shannken 	.vfs_name = MOUNT_HFS,
1526d285189Shannken 	.vfs_min_mount_data = sizeof (struct hfs_args),
1536d285189Shannken 	.vfs_mount = hfs_mount,
1546d285189Shannken 	.vfs_start = hfs_start,
1556d285189Shannken 	.vfs_unmount = hfs_unmount,
1566d285189Shannken 	.vfs_root = hfs_root,
1576d285189Shannken 	.vfs_quotactl = (void *)eopnotsupp,
1586d285189Shannken 	.vfs_statvfs = hfs_statvfs,
1596d285189Shannken 	.vfs_sync = hfs_sync,
1606d285189Shannken 	.vfs_vget = hfs_vget,
161265f1d8cShannken 	.vfs_loadvnode = hfs_loadvnode,
1626d285189Shannken 	.vfs_fhtovp = hfs_fhtovp,
1636d285189Shannken 	.vfs_vptofh = hfs_vptofh,
1646d285189Shannken 	.vfs_init = hfs_init,
1656d285189Shannken 	.vfs_reinit = hfs_reinit,
1666d285189Shannken 	.vfs_done = hfs_done,
1676d285189Shannken 	.vfs_extattrctl = vfs_stdextattrctl,
168326db3aaShannken 	.vfs_suspendctl = genfs_suspendctl,
1696d285189Shannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
1706d285189Shannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
1716d285189Shannken 	.vfs_fsync = (void *)eopnotsupp,
1726d285189Shannken 	.vfs_opv_descs = hfs_vnodeopv_descs
17325e99827Sdillo };
17425e99827Sdillo 
17556c3e412Sdillo static const struct genfs_ops hfs_genfsops = {
17625e99827Sdillo 	.gop_size = genfs_size,
17725e99827Sdillo };
17825e99827Sdillo 
179a1221b6dSrumble static int
hfs_modcmd(modcmd_t cmd,void * arg)180a1221b6dSrumble hfs_modcmd(modcmd_t cmd, void *arg)
181a1221b6dSrumble {
182a1221b6dSrumble 	switch (cmd) {
183a1221b6dSrumble 	case MODULE_CMD_INIT:
184a1221b6dSrumble 		return vfs_attach(&hfs_vfsops);
185a1221b6dSrumble 	case MODULE_CMD_FINI:
186a1221b6dSrumble 		return vfs_detach(&hfs_vfsops);
187a1221b6dSrumble 	default:
188a1221b6dSrumble 		return ENOTTY;
189a1221b6dSrumble 	}
190a1221b6dSrumble }
191a1221b6dSrumble 
19225e99827Sdillo int
hfs_mount(struct mount * mp,const char * path,void * data,size_t * data_len)19361e8303eSpooka hfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
19425e99827Sdillo {
19561e8303eSpooka 	struct lwp *l = curlwp;
1962721ab6cSdsl 	struct hfs_args *args = data;
19725e99827Sdillo 	struct vnode *devvp;
19856c3e412Sdillo 	struct hfsmount *hmp;
19915a60c04Sjoerg 	int error = 0;
20025e99827Sdillo 	int update;
20125e99827Sdillo 	mode_t accessmode;
20225e99827Sdillo 
20323f76b6dSmaxv 	if (args == NULL)
20423f76b6dSmaxv 		return EINVAL;
2052721ab6cSdsl 	if (*data_len < sizeof *args)
2062721ab6cSdsl 		return EINVAL;
2072721ab6cSdsl 
20856c3e412Sdillo #ifdef HFS_DEBUG
20956c3e412Sdillo 	printf("vfsop = hfs_mount()\n");
21056c3e412Sdillo #endif /* HFS_DEBUG */
21125e99827Sdillo 
21225e99827Sdillo 	if (mp->mnt_flag & MNT_GETARGS) {
21356c3e412Sdillo 		hmp = VFSTOHFS(mp);
21425e99827Sdillo 		if (hmp == NULL)
21525e99827Sdillo 			return EIO;
2162721ab6cSdsl 		args->fspec = NULL;
2172721ab6cSdsl 		*data_len = sizeof *args;
2182721ab6cSdsl 		return 0;
21925e99827Sdillo 	}
22025e99827Sdillo 
22125e99827Sdillo /* FIXME: For development ONLY - disallow remounting for now */
22225e99827Sdillo #if 0
22325e99827Sdillo 	update = mp->mnt_flag & MNT_UPDATE;
22425e99827Sdillo #else
22525e99827Sdillo 	update = 0;
22625e99827Sdillo #endif
22725e99827Sdillo 
22825e99827Sdillo 	/* Check arguments */
2292721ab6cSdsl 	if (args->fspec != NULL) {
23025e99827Sdillo 		/*
23125e99827Sdillo 		 * Look up the name and verify that it's sane.
23225e99827Sdillo 		 */
233effcf1afSdholland 		error = namei_simple_user(args->fspec,
234effcf1afSdholland 					NSM_FOLLOW_NOEMULROOT, &devvp);
235effcf1afSdholland 		if (error != 0)
23625e99827Sdillo 			return error;
23725e99827Sdillo 
23825e99827Sdillo 		if (!update) {
23925e99827Sdillo 			/*
24025e99827Sdillo 			 * Be sure this is a valid block device
24125e99827Sdillo 			 */
24225e99827Sdillo 			if (devvp->v_type != VBLK)
24325e99827Sdillo 				error = ENOTBLK;
24425e99827Sdillo 			else if (bdevsw_lookup(devvp->v_rdev) == NULL)
24525e99827Sdillo 				error = ENXIO;
24625e99827Sdillo 		} else {
24725e99827Sdillo 			/*
24825e99827Sdillo 			 * Be sure we're still naming the same device
24925e99827Sdillo 			 * used for our initial mount
25025e99827Sdillo 			 */
25156c3e412Sdillo 			hmp = VFSTOHFS(mp);
25225e99827Sdillo 			if (devvp != hmp->hm_devvp)
25325e99827Sdillo 				error = EINVAL;
25425e99827Sdillo 		}
25525e99827Sdillo 	} else {
25625e99827Sdillo 		if (update) {
25725e99827Sdillo 			/* Use the extant mount */
25856c3e412Sdillo 			hmp = VFSTOHFS(mp);
25925e99827Sdillo 			devvp = hmp->hm_devvp;
26025e99827Sdillo 			vref(devvp);
26125e99827Sdillo 		} else {
26225e99827Sdillo 			/* New mounts must have a filename for the device */
26325e99827Sdillo 			return EINVAL;
26425e99827Sdillo 		}
26525e99827Sdillo 	}
26625e99827Sdillo 
26725e99827Sdillo 	/*
26825e99827Sdillo 	 * If mount by non-root, then verify that user has necessary
26925e99827Sdillo 	 * permissions on the device.
2709670d2e4Selad 	 *
2719670d2e4Selad 	 * Permission to update a mount is checked higher, so here we presume
2729670d2e4Selad 	 * updating the mount is okay (for example, as far as securelevel goes)
2739670d2e4Selad 	 * which leaves us with the normal check.
27425e99827Sdillo 	 */
2758b53d2f9Spooka 	if (error == 0) {
27625e99827Sdillo 		accessmode = VREAD;
27725e99827Sdillo 		if (update ?
27825e99827Sdillo 			(mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
27925e99827Sdillo 			(mp->mnt_flag & MNT_RDONLY) == 0)
28025e99827Sdillo 			accessmode |= VWRITE;
28125e99827Sdillo 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2820c9d8d15Selad 		error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
2830c9d8d15Selad 		    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
2840c9d8d15Selad 		    KAUTH_ARG(accessmode));
2851423e65bShannken 		VOP_UNLOCK(devvp);
2868b53d2f9Spooka 	}
28725e99827Sdillo 
28825e99827Sdillo 	if (error != 0)
28925e99827Sdillo 		goto error;
29025e99827Sdillo 
29125e99827Sdillo 	if (update) {
29256c3e412Sdillo 		printf("HFS: live remounting not yet supported!\n");
29325e99827Sdillo 		error = EINVAL;
29425e99827Sdillo 		goto error;
29525e99827Sdillo 	}
29625e99827Sdillo 
2972721ab6cSdsl 	if ((error = hfs_mountfs(devvp, mp, l, args->fspec)) != 0)
29825e99827Sdillo 		goto error;
29925e99827Sdillo 
3002721ab6cSdsl 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
301e24b0872Spooka 		mp->mnt_op->vfs_name, mp, l);
30225e99827Sdillo 
30356c3e412Sdillo #ifdef HFS_DEBUG
30425e99827Sdillo 	if(!update) {
30525e99827Sdillo 		char* volname;
30625e99827Sdillo 
30756c3e412Sdillo 		hmp = VFSTOHFS(mp);
30825e99827Sdillo 		volname = malloc(hmp->hm_vol.name.length + 1, M_TEMP, M_WAITOK);
30925e99827Sdillo 		if (volname == NULL)
31025e99827Sdillo 			printf("could not allocate volname; ignored\n");
31125e99827Sdillo 		else {
31256c3e412Sdillo 			if (hfs_unicode_to_ascii(hmp->hm_vol.name.unicode,
31325e99827Sdillo 				hmp->hm_vol.name.length, volname) == NULL)
31425e99827Sdillo 				printf("could not convert volume name to ascii; ignored\n");
31525e99827Sdillo 			else
31625e99827Sdillo 				printf("mounted volume \"%s\"\n", volname);
31725e99827Sdillo 			free(volname, M_TEMP);
31825e99827Sdillo 		}
31925e99827Sdillo 	}
32056c3e412Sdillo #endif /* HFS_DEBUG */
32125e99827Sdillo 
32225e99827Sdillo 	return error;
32325e99827Sdillo 
32425e99827Sdillo error:
32525e99827Sdillo 	vrele(devvp);
32625e99827Sdillo 	return error;
32725e99827Sdillo }
32825e99827Sdillo 
32925e99827Sdillo int
hfs_start(struct mount * mp,int flags)33061e8303eSpooka hfs_start(struct mount *mp, int flags)
33125e99827Sdillo {
33256c3e412Sdillo #ifdef HFS_DEBUG
33356c3e412Sdillo 	printf("vfsop = hfs_start()\n");
33456c3e412Sdillo #endif /* HFS_DEBUG */
33525e99827Sdillo 
33625e99827Sdillo 	return 0;
33725e99827Sdillo }
33825e99827Sdillo 
33925e99827Sdillo int
hfs_mountfs(struct vnode * devvp,struct mount * mp,struct lwp * l,const char * devpath)34056c3e412Sdillo hfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l,
34141627b2cSdillo     const char *devpath)
34225e99827Sdillo {
34356c3e412Sdillo 	hfs_callback_args cbargs;
34456c3e412Sdillo 	hfs_libcb_argsopen argsopen;
34556c3e412Sdillo 	hfs_libcb_argsread argsread;
34656c3e412Sdillo 	struct hfsmount *hmp;
34725e99827Sdillo 	kauth_cred_t cred;
34825e99827Sdillo 	int error;
34925e99827Sdillo 
35025e99827Sdillo 	cred = l ? l->l_cred : NOCRED;
35125e99827Sdillo 	error = 0;
35225e99827Sdillo 	hmp = NULL;
35325e99827Sdillo 
35425e99827Sdillo 	/* Create mounted volume structure. */
3558dfa44bcSmaxv 	hmp = malloc(sizeof(struct hfsmount), M_HFSMNT, M_WAITOK|M_ZERO);
35625e99827Sdillo 	if (hmp == NULL) {
35725e99827Sdillo 		error = ENOMEM;
35825e99827Sdillo 		goto error;
35925e99827Sdillo 	}
36025e99827Sdillo 
36125e99827Sdillo 	mp->mnt_data = hmp;
36225e99827Sdillo 	mp->mnt_flag |= MNT_LOCAL;
36325e99827Sdillo 	vfs_getnewfsid(mp);
36425e99827Sdillo 
36525e99827Sdillo 	hmp->hm_mountp = mp;
36625e99827Sdillo 	hmp->hm_dev = devvp->v_rdev;
36725e99827Sdillo 	hmp->hm_devvp = devvp;
36825e99827Sdillo 
36925e99827Sdillo 	/*
37056c3e412Sdillo 	 * Use libhfs to open the volume and read the volume header and other
37125e99827Sdillo 	 * useful information.
37225e99827Sdillo 	 */
37325e99827Sdillo 
37456c3e412Sdillo 	hfslib_init_cbargs(&cbargs);
37525e99827Sdillo 	argsopen.cred = argsread.cred = cred;
37625e99827Sdillo 	argsopen.l = argsread.l = l;
37725e99827Sdillo 	argsopen.devvp = devvp;
37825e99827Sdillo 	cbargs.read = (void*)&argsread;
37925e99827Sdillo 	cbargs.openvol = (void*)&argsopen;
38025e99827Sdillo 
38141627b2cSdillo 	if ((error = hfslib_open_volume(devpath, mp->mnt_flag & MNT_RDONLY,
38225e99827Sdillo 		&hmp->hm_vol, &cbargs)) != 0)
38325e99827Sdillo 		goto error;
38425e99827Sdillo 
38525e99827Sdillo 	/* Make sure this is not a journaled volume whose journal is dirty. */
38656c3e412Sdillo 	if (!hfslib_is_journal_clean(&hmp->hm_vol)) {
38725e99827Sdillo 		printf("volume journal is dirty; not mounting\n");
38825e99827Sdillo 		error = EIO;
38925e99827Sdillo 		goto error;
39025e99827Sdillo 	}
39125e99827Sdillo 
39225e99827Sdillo 	mp->mnt_fs_bshift = 0;
39325e99827Sdillo 	while ((1 << mp->mnt_fs_bshift) < hmp->hm_vol.vh.block_size)
39425e99827Sdillo 		mp->mnt_fs_bshift++;
39525e99827Sdillo 	mp->mnt_dev_bshift = DEV_BSHIFT;
39625e99827Sdillo 
39725e99827Sdillo 	return 0;
39825e99827Sdillo 
39925e99827Sdillo error:
40025e99827Sdillo 	if (hmp != NULL)
40156c3e412Sdillo 		free(hmp, M_HFSMNT);
40225e99827Sdillo 	return error;
40325e99827Sdillo }
40425e99827Sdillo 
40525e99827Sdillo int
hfs_unmount(struct mount * mp,int mntflags)40661e8303eSpooka hfs_unmount(struct mount *mp, int mntflags)
40725e99827Sdillo {
40856c3e412Sdillo 	hfs_callback_args cbargs;
40956c3e412Sdillo 	hfs_libcb_argsread argsclose;
41056c3e412Sdillo 	struct hfsmount* hmp;
41125e99827Sdillo 	int error;
41225e99827Sdillo 	int flags;
41325e99827Sdillo 
41456c3e412Sdillo #ifdef HFS_DEBUG
41556c3e412Sdillo 	printf("vfsop = hfs_unmount()\n");
41656c3e412Sdillo #endif /* HFS_DEBUG */
41725e99827Sdillo 
41856c3e412Sdillo 	hmp = VFSTOHFS(mp);
41925e99827Sdillo 
42025e99827Sdillo 	flags = 0;
42125e99827Sdillo 	if (mntflags & MNT_FORCE)
42225e99827Sdillo 		flags |= FORCECLOSE;
42325e99827Sdillo 
42425e99827Sdillo 	if ((error = vflush(mp, NULLVP, flags)) != 0)
42525e99827Sdillo 		return error;
42625e99827Sdillo 
42756c3e412Sdillo 	hfslib_init_cbargs(&cbargs);
42861e8303eSpooka 	argsclose.l = curlwp;
42925e99827Sdillo 	cbargs.closevol = (void*)&argsclose;
43056c3e412Sdillo 	hfslib_close_volume(&hmp->hm_vol, &cbargs);
43125e99827Sdillo 
432d630c255Spooka 	vrele(hmp->hm_devvp);
43325e99827Sdillo 
43456c3e412Sdillo 	free(hmp, M_HFSMNT);
43525e99827Sdillo 	mp->mnt_data = NULL;
43625e99827Sdillo 	mp->mnt_flag &= ~MNT_LOCAL;
43725e99827Sdillo 
43825e99827Sdillo 	return error;
43925e99827Sdillo }
44025e99827Sdillo 
44125e99827Sdillo int
hfs_root(struct mount * mp,int lktype,struct vnode ** vpp)442c2e9cb94Sad hfs_root(struct mount *mp, int lktype, struct vnode **vpp)
44325e99827Sdillo {
44425e99827Sdillo 	struct vnode *nvp;
44525e99827Sdillo 	int error;
44625e99827Sdillo 
44756c3e412Sdillo #ifdef HFS_DEBUG
44856c3e412Sdillo 	printf("vfsop = hfs_root()\n");
44956c3e412Sdillo #endif /* HFS_DEBUG */
45025e99827Sdillo 
451c2e9cb94Sad 	if ((error = VFS_VGET(mp, HFS_CNID_ROOT_FOLDER, lktype, &nvp)) != 0)
45225e99827Sdillo 		return error;
45325e99827Sdillo 	*vpp = nvp;
45425e99827Sdillo 
45525e99827Sdillo 	return 0;
45625e99827Sdillo }
45725e99827Sdillo 
45825e99827Sdillo int
hfs_statvfs(struct mount * mp,struct statvfs * sbp)45961e8303eSpooka hfs_statvfs(struct mount *mp, struct statvfs *sbp)
46025e99827Sdillo {
46156c3e412Sdillo 	hfs_volume_header_t *vh;
46225e99827Sdillo 
46356c3e412Sdillo #ifdef HFS_DEBUG
46456c3e412Sdillo 	printf("vfsop = hfs_statvfs()\n");
46556c3e412Sdillo #endif /* HFS_DEBUG */
46625e99827Sdillo 
46756c3e412Sdillo 	vh = &VFSTOHFS(mp)->hm_vol.vh;
46825e99827Sdillo 
46925e99827Sdillo 	sbp->f_bsize = vh->block_size;
47025e99827Sdillo 	sbp->f_frsize = sbp->f_bsize;
47125e99827Sdillo 	sbp->f_iosize = 4096;/* mac os x uses a 4 kb io size, so do the same */
47225e99827Sdillo 	sbp->f_blocks = vh->total_blocks;
47325e99827Sdillo 	sbp->f_bfree = vh->free_blocks; /* total free blocks */
47425e99827Sdillo 	sbp->f_bavail = vh->free_blocks; /* blocks free for non superuser */
47525e99827Sdillo 	sbp->f_bresvd = 0;
47625e99827Sdillo 	sbp->f_files =  vh->file_count; /* total files */
4773fcf6843Skamil 	sbp->f_ffree = (1U<<31) - vh->file_count; /* free file nodes */
47825e99827Sdillo 	copy_statvfs_info(sbp, mp);
47925e99827Sdillo 
48025e99827Sdillo 	return 0;
48125e99827Sdillo }
48225e99827Sdillo 
48325e99827Sdillo int
hfs_sync(struct mount * mp,int waitfor,kauth_cred_t cred)48461e8303eSpooka hfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
48525e99827Sdillo {
48656c3e412Sdillo #ifdef HFS_DEBUG
48756c3e412Sdillo 	printf("vfsop = hfs_sync()\n");
48856c3e412Sdillo #endif /* HFS_DEBUG */
48925e99827Sdillo 
49025e99827Sdillo 	return 0;
49125e99827Sdillo }
49225e99827Sdillo 
49325e99827Sdillo /*
49425e99827Sdillo  * an ino_t corresponds directly to a CNID in our particular case,
49525e99827Sdillo  * since both are conveniently 32-bit numbers
49625e99827Sdillo  */
49725e99827Sdillo int
hfs_vget(struct mount * mp,ino_t ino,int lktype,struct vnode ** vpp)498c2e9cb94Sad hfs_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
49925e99827Sdillo {
500265f1d8cShannken 	int error;
501265f1d8cShannken 
502265f1d8cShannken 	error = hfs_vget_internal(mp, ino, HFS_DATAFORK, vpp);
503265f1d8cShannken 	if (error)
504265f1d8cShannken 		return error;
505c2e9cb94Sad 	error = vn_lock(*vpp, lktype);
506265f1d8cShannken 	if (error) {
507265f1d8cShannken 		vrele(*vpp);
508265f1d8cShannken 		*vpp = NULL;
509265f1d8cShannken 		return error;
510265f1d8cShannken 	}
511265f1d8cShannken 	return 0;
51225e99827Sdillo }
51325e99827Sdillo 
51425e99827Sdillo /*
51525e99827Sdillo  * internal version with extra arguments to allow accessing resource fork
51625e99827Sdillo  */
51725e99827Sdillo int
hfs_vget_internal(struct mount * mp,ino_t ino,uint8_t fork,struct vnode ** vpp)51856c3e412Sdillo hfs_vget_internal(struct mount *mp, ino_t ino, uint8_t fork,
51925e99827Sdillo     struct vnode **vpp)
52025e99827Sdillo {
521265f1d8cShannken 	struct hfsnode_key key;
522265f1d8cShannken 
523265f1d8cShannken 	memset(&key, 0, sizeof(key));
524265f1d8cShannken 	key.hnk_cnid = (hfs_cnid_t)ino;
525265f1d8cShannken 	key.hnk_fork = (fork != HFS_RSRCFORK ? HFS_DATAFORK : HFS_RSRCFORK);
526265f1d8cShannken 
527265f1d8cShannken 	return vcache_get(mp, &key, sizeof(key), vpp);
528265f1d8cShannken }
529265f1d8cShannken 
530265f1d8cShannken int
hfs_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)531265f1d8cShannken hfs_loadvnode(struct mount *mp, struct vnode *vp,
532265f1d8cShannken     const void *key, size_t key_len, const void **new_key)
533265f1d8cShannken {
53456c3e412Sdillo 	struct hfsmount *hmp;
53556c3e412Sdillo 	struct hfsnode *hnode;
536265f1d8cShannken 	struct hfsnode_key hfskey;
53756c3e412Sdillo 	hfs_callback_args cbargs;
53856c3e412Sdillo 	hfs_catalog_keyed_record_t rec;
539265f1d8cShannken 	hfs_catalog_key_t cat_key; /* the search key used to find this file on disk */
54025e99827Sdillo 	dev_t dev;
54125e99827Sdillo 
54256c3e412Sdillo #ifdef HFS_DEBUG
543265f1d8cShannken 	printf("vfsop = hfs_loadvnode()\n");
54456c3e412Sdillo #endif /* HFS_DEBUG */
54525e99827Sdillo 
546265f1d8cShannken 	KASSERT(key_len == sizeof(hfskey));
547265f1d8cShannken 	memcpy(&hfskey, key, key_len);
548265f1d8cShannken 
54956c3e412Sdillo 	hmp = VFSTOHFS(mp);
55025e99827Sdillo 	dev = hmp->hm_dev;
55125e99827Sdillo 
552265f1d8cShannken 	hnode = pool_get(&hfs_node_pool, PR_WAITOK);
553265f1d8cShannken 	memset(hnode, 0, sizeof(*hnode));
55425e99827Sdillo 	hnode->h_vnode = vp;
55525e99827Sdillo 	hnode->h_hmp = hmp;
55625e99827Sdillo 	hnode->dummy = 0x1337BABE;
55725e99827Sdillo 	hnode->h_dev = dev;
558265f1d8cShannken 	hnode->h_rec.u.cnid = hfskey.hnk_cnid;
559265f1d8cShannken 	hnode->h_fork = hfskey.hnk_fork;
560265f1d8cShannken 	hnode->h_key = hfskey;
56125e99827Sdillo 
56225e99827Sdillo 	/*
56325e99827Sdillo 	 * Read catalog record from disk.
56425e99827Sdillo 	 */
56556c3e412Sdillo 	hfslib_init_cbargs(&cbargs);
56625e99827Sdillo 
567265f1d8cShannken 	if (hfslib_find_catalog_record_with_cnid(&hmp->hm_vol, hfskey.hnk_cnid,
568265f1d8cShannken 		&rec, &cat_key, &cbargs) != 0) {
569265f1d8cShannken 		pool_put(&hfs_node_pool, hnode);
570265f1d8cShannken 		return EBADF;
57125e99827Sdillo 	}
57225e99827Sdillo 
57325e99827Sdillo 	memcpy(&hnode->h_rec, &rec, sizeof(hnode->h_rec));
574265f1d8cShannken 	hnode->h_parent = cat_key.parent_cnid;
57525e99827Sdillo 
57625e99827Sdillo 	/* XXX Eventually need to add an "ignore permissions" mount option */
57725e99827Sdillo 
57825e99827Sdillo 	/*
57925e99827Sdillo 	 * Now convert some of the catalog record's fields into values that make
58025e99827Sdillo 	 * sense on this system.
58125e99827Sdillo 	 */
58225e99827Sdillo 	/* DATE AND TIME */
58325e99827Sdillo 
584265f1d8cShannken 	vp->v_tag = VT_HFS;
585265f1d8cShannken 	vp->v_op = hfs_vnodeop_p;
586265f1d8cShannken 	vp->v_data = hnode;
587265f1d8cShannken 	genfs_node_init(vp, &hfs_genfsops);
588265f1d8cShannken 
58925e99827Sdillo 	/*
59056c3e412Sdillo 	 * Initialize the vnode from the hfsnode, check for aliases.
59125e99827Sdillo 	 */
59256c3e412Sdillo 	hfs_vinit(mp, hfs_specop_p, hfs_fifoop_p, &vp);
59325e99827Sdillo 
59425e99827Sdillo 	hnode->h_devvp = hmp->hm_devvp;
595c3183f32Spooka 	vref(hnode->h_devvp);  /* Increment the ref count to the volume's device. */
59625e99827Sdillo 
59725e99827Sdillo 	/* Make sure UVM has allocated enough memory. (?) */
59836a81a3bSgmcgarry 	if (hnode->h_rec.u.rec_type == HFS_REC_FILE) {
59956c3e412Sdillo 		if (hnode->h_fork == HFS_DATAFORK)
60025e99827Sdillo 			uvm_vnp_setsize(vp,
60125e99827Sdillo 			    hnode->h_rec.file.data_fork.logical_size);
60225e99827Sdillo 		else
60325e99827Sdillo 			uvm_vnp_setsize(vp,
60425e99827Sdillo 			    hnode->h_rec.file.rsrc_fork.logical_size);
60525e99827Sdillo 	}
60625e99827Sdillo 	else
60725e99827Sdillo 		uvm_vnp_setsize(vp, 0); /* no directly reading directories */
60825e99827Sdillo 
609265f1d8cShannken 	*new_key = &hnode->h_key;
61025e99827Sdillo 	return 0;
61125e99827Sdillo }
61225e99827Sdillo 
61325e99827Sdillo int
hfs_fhtovp(struct mount * mp,struct fid * fhp,int lktype,struct vnode ** vpp)614c2e9cb94Sad hfs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
61525e99827Sdillo {
61625e99827Sdillo 
61756c3e412Sdillo #ifdef HFS_DEBUG
61856c3e412Sdillo 	printf("vfsop = hfs_fhtovp()\n");
61956c3e412Sdillo #endif /* HFS_DEBUG */
62025e99827Sdillo 
62125e99827Sdillo 	return EOPNOTSUPP;
62225e99827Sdillo }
62325e99827Sdillo 
62425e99827Sdillo int
hfs_vptofh(struct vnode * vp,struct fid * fhp,size_t * fh_size)62556c3e412Sdillo hfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
62625e99827Sdillo {
62756c3e412Sdillo #ifdef HFS_DEBUG
62856c3e412Sdillo 	printf("vfsop = hfs_vptofh()\n");
62956c3e412Sdillo #endif /* HFS_DEBUG */
63025e99827Sdillo 
63125e99827Sdillo 	return EOPNOTSUPP;
63225e99827Sdillo }
63325e99827Sdillo 
63425e99827Sdillo void
hfs_init(void)63556c3e412Sdillo hfs_init(void)
63625e99827Sdillo {
63756c3e412Sdillo 	hfs_callbacks	callbacks;
63825e99827Sdillo 
63956c3e412Sdillo #ifdef HFS_DEBUG
64056c3e412Sdillo 	printf("vfsop = hfs_init()\n");
64156c3e412Sdillo #endif /* HFS_DEBUG */
64225e99827Sdillo 
64356c3e412Sdillo 	malloc_type_attach(M_HFSMNT);
644265f1d8cShannken 	pool_init(&hfs_node_pool, sizeof(struct hfsnode), 0, 0, 0, "hfsndpl",
645265f1d8cShannken 	    &pool_allocator_nointr, IPL_NONE);
64625e99827Sdillo 
64756c3e412Sdillo 	callbacks.error = hfs_libcb_error;
64856c3e412Sdillo 	callbacks.allocmem = hfs_libcb_malloc;
64956c3e412Sdillo 	callbacks.reallocmem = hfs_libcb_realloc;
65056c3e412Sdillo 	callbacks.freemem = hfs_libcb_free;
65156c3e412Sdillo 	callbacks.openvol = hfs_libcb_opendev;
65256c3e412Sdillo 	callbacks.closevol = hfs_libcb_closedev;
65356c3e412Sdillo 	callbacks.read = hfs_libcb_read;
65425e99827Sdillo 
65556c3e412Sdillo 	hfslib_init(&callbacks);
65625e99827Sdillo }
65725e99827Sdillo 
65825e99827Sdillo void
hfs_reinit(void)65956c3e412Sdillo hfs_reinit(void)
66025e99827Sdillo {
66156c3e412Sdillo #ifdef HFS_DEBUG
66256c3e412Sdillo 	printf("vfsop = hfs_reinit()\n");
66356c3e412Sdillo #endif /* HFS_DEBUG */
66425e99827Sdillo 
66525e99827Sdillo 	return;
66625e99827Sdillo }
66725e99827Sdillo 
66825e99827Sdillo void
hfs_done(void)66956c3e412Sdillo hfs_done(void)
67025e99827Sdillo {
67156c3e412Sdillo #ifdef HFS_DEBUG
67256c3e412Sdillo 	printf("vfsop = hfs_done()\n");
67356c3e412Sdillo #endif /* HFS_DEBUG */
67425e99827Sdillo 
67556c3e412Sdillo 	malloc_type_detach(M_HFSMNT);
676265f1d8cShannken 	pool_destroy(&hfs_node_pool);
67756c3e412Sdillo 	hfslib_done();
67825e99827Sdillo }
67925e99827Sdillo 
68025e99827Sdillo int
hfs_mountroot(void)68156c3e412Sdillo hfs_mountroot(void)
68225e99827Sdillo {
68356c3e412Sdillo #ifdef HFS_DEBUG
68456c3e412Sdillo 	printf("vfsop = hfs_mountroot()\n");
68556c3e412Sdillo #endif /* HFS_DEBUG */
68625e99827Sdillo 
68725e99827Sdillo 	return EOPNOTSUPP;
68825e99827Sdillo }
689