hfs_vfsops.c revision 1.9.6.6 1 1.9.6.6 yamt /* $NetBSD: hfs_vfsops.c,v 1.9.6.6 2008/02/04 09:23:45 yamt Exp $ */
2 1.9.6.2 yamt
3 1.9.6.2 yamt /*-
4 1.9.6.2 yamt * Copyright (c) 2005, 2007 The NetBSD Foundation, Inc.
5 1.9.6.2 yamt * All rights reserved.
6 1.9.6.2 yamt *
7 1.9.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.9.6.2 yamt * by Yevgeny Binder and Dieter Baron.
9 1.9.6.2 yamt *
10 1.9.6.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.9.6.2 yamt * modification, are permitted provided that the following conditions
12 1.9.6.2 yamt * are met:
13 1.9.6.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.9.6.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.9.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.9.6.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.9.6.2 yamt * documentation and/or other materials provided with the distribution.
18 1.9.6.2 yamt *
19 1.9.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.9.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.9.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.9.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.9.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.9.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.9.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.9.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.9.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.9.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.9.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.9.6.2 yamt */
31 1.9.6.2 yamt
32 1.9.6.2 yamt /*
33 1.9.6.2 yamt * Copyright (c) 1991, 1993, 1994
34 1.9.6.2 yamt * The Regents of the University of California. All rights reserved.
35 1.9.6.2 yamt * (c) UNIX System Laboratories, Inc.
36 1.9.6.2 yamt * All or some portions of this file are derived from material licensed
37 1.9.6.2 yamt * to the University of California by American Telephone and Telegraph
38 1.9.6.2 yamt * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 1.9.6.2 yamt * the permission of UNIX System Laboratories, Inc.
40 1.9.6.2 yamt *
41 1.9.6.2 yamt * Redistribution and use in source and binary forms, with or without
42 1.9.6.2 yamt * modification, are permitted provided that the following conditions
43 1.9.6.2 yamt * are met:
44 1.9.6.2 yamt * 1. Redistributions of source code must retain the above copyright
45 1.9.6.2 yamt * notice, this list of conditions and the following disclaimer.
46 1.9.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
47 1.9.6.2 yamt * notice, this list of conditions and the following disclaimer in the
48 1.9.6.2 yamt * documentation and/or other materials provided with the distribution.
49 1.9.6.2 yamt * 3. Neither the name of the University nor the names of its contributors
50 1.9.6.2 yamt * may be used to endorse or promote products derived from this software
51 1.9.6.2 yamt * without specific prior written permission.
52 1.9.6.2 yamt *
53 1.9.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.9.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.9.6.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.9.6.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.9.6.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.9.6.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.9.6.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.9.6.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.9.6.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.9.6.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.9.6.2 yamt * SUCH DAMAGE.
64 1.9.6.2 yamt */
65 1.9.6.2 yamt
66 1.9.6.2 yamt /*
67 1.9.6.2 yamt * Copyright (c) 1989, 1991, 1993, 1994
68 1.9.6.2 yamt * The Regents of the University of California. All rights reserved.
69 1.9.6.2 yamt *
70 1.9.6.2 yamt * Redistribution and use in source and binary forms, with or without
71 1.9.6.2 yamt * modification, are permitted provided that the following conditions
72 1.9.6.2 yamt * are met:
73 1.9.6.2 yamt * 1. Redistributions of source code must retain the above copyright
74 1.9.6.2 yamt * notice, this list of conditions and the following disclaimer.
75 1.9.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
76 1.9.6.2 yamt * notice, this list of conditions and the following disclaimer in the
77 1.9.6.2 yamt * documentation and/or other materials provided with the distribution.
78 1.9.6.2 yamt * 3. Neither the name of the University nor the names of its contributors
79 1.9.6.2 yamt * may be used to endorse or promote products derived from this software
80 1.9.6.2 yamt * without specific prior written permission.
81 1.9.6.2 yamt *
82 1.9.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 1.9.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 1.9.6.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 1.9.6.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 1.9.6.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 1.9.6.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 1.9.6.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 1.9.6.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 1.9.6.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 1.9.6.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 1.9.6.2 yamt * SUCH DAMAGE.
93 1.9.6.2 yamt */
94 1.9.6.2 yamt
95 1.9.6.2 yamt
96 1.9.6.2 yamt
97 1.9.6.2 yamt /*
98 1.9.6.2 yamt * Apple HFS+ filesystem
99 1.9.6.2 yamt */
100 1.9.6.2 yamt
101 1.9.6.2 yamt #include <sys/cdefs.h>
102 1.9.6.6 yamt __KERNEL_RCSID(0, "$NetBSD: hfs_vfsops.c,v 1.9.6.6 2008/02/04 09:23:45 yamt Exp $");
103 1.9.6.2 yamt
104 1.9.6.2 yamt #ifdef _KERNEL_OPT
105 1.9.6.2 yamt #include "opt_compat_netbsd.h"
106 1.9.6.2 yamt #endif
107 1.9.6.2 yamt
108 1.9.6.2 yamt #include <sys/param.h>
109 1.9.6.2 yamt #include <sys/systm.h>
110 1.9.6.2 yamt #include <sys/namei.h>
111 1.9.6.2 yamt #include <sys/proc.h>
112 1.9.6.2 yamt #include <sys/kernel.h>
113 1.9.6.2 yamt #include <sys/vnode.h>
114 1.9.6.2 yamt #include <sys/socket.h>
115 1.9.6.2 yamt #include <sys/mount.h>
116 1.9.6.2 yamt #include <sys/buf.h>
117 1.9.6.2 yamt #include <sys/device.h>
118 1.9.6.2 yamt #include <sys/mbuf.h>
119 1.9.6.2 yamt #include <sys/file.h>
120 1.9.6.2 yamt #include <sys/disklabel.h>
121 1.9.6.2 yamt #include <sys/ioctl.h>
122 1.9.6.2 yamt #include <sys/errno.h>
123 1.9.6.2 yamt #include <sys/malloc.h>
124 1.9.6.2 yamt #include <sys/pool.h>
125 1.9.6.2 yamt #include <sys/lock.h>
126 1.9.6.2 yamt #include <sys/sysctl.h>
127 1.9.6.2 yamt #include <sys/conf.h>
128 1.9.6.2 yamt #include <sys/kauth.h>
129 1.9.6.2 yamt #include <sys/stat.h>
130 1.9.6.2 yamt
131 1.9.6.6 yamt #include <miscfs/genfs/genfs.h>
132 1.9.6.2 yamt #include <miscfs/specfs/specdev.h>
133 1.9.6.2 yamt
134 1.9.6.2 yamt #include <fs/hfs/hfs.h>
135 1.9.6.2 yamt #include <fs/hfs/libhfs.h>
136 1.9.6.2 yamt
137 1.9.6.2 yamt MALLOC_JUSTDEFINE(M_HFSMNT, "hfs mount", "hfs mount structures");
138 1.9.6.2 yamt
139 1.9.6.6 yamt extern kmutex_t hfs_hashlock;
140 1.9.6.2 yamt
141 1.9.6.2 yamt const struct vnodeopv_desc * const hfs_vnodeopv_descs[] = {
142 1.9.6.2 yamt &hfs_vnodeop_opv_desc,
143 1.9.6.2 yamt &hfs_specop_opv_desc,
144 1.9.6.2 yamt &hfs_fifoop_opv_desc,
145 1.9.6.2 yamt NULL,
146 1.9.6.2 yamt };
147 1.9.6.2 yamt
148 1.9.6.2 yamt struct vfsops hfs_vfsops = {
149 1.9.6.2 yamt MOUNT_HFS,
150 1.9.6.2 yamt sizeof (struct hfs_args),
151 1.9.6.2 yamt hfs_mount,
152 1.9.6.2 yamt hfs_start,
153 1.9.6.2 yamt hfs_unmount,
154 1.9.6.2 yamt hfs_root,
155 1.9.6.4 yamt (void *)eopnotsupp, /* vfs_quotactl */
156 1.9.6.2 yamt hfs_statvfs,
157 1.9.6.2 yamt hfs_sync,
158 1.9.6.2 yamt hfs_vget,
159 1.9.6.2 yamt hfs_fhtovp,
160 1.9.6.2 yamt hfs_vptofh,
161 1.9.6.2 yamt hfs_init,
162 1.9.6.2 yamt hfs_reinit,
163 1.9.6.2 yamt hfs_done,
164 1.9.6.2 yamt NULL, /* vfs_mountroot */
165 1.9.6.2 yamt NULL, /* vfs_snapshot */
166 1.9.6.4 yamt vfs_stdextattrctl,
167 1.9.6.2 yamt (void *)eopnotsupp, /* vfs_suspendctl */
168 1.9.6.6 yamt genfs_renamelock_enter,
169 1.9.6.6 yamt genfs_renamelock_exit,
170 1.9.6.2 yamt hfs_vnodeopv_descs,
171 1.9.6.2 yamt 0,
172 1.9.6.2 yamt { NULL, NULL },
173 1.9.6.2 yamt };
174 1.9.6.2 yamt VFS_ATTACH(hfs_vfsops); /* XXX Is this needed? */
175 1.9.6.2 yamt
176 1.9.6.2 yamt static const struct genfs_ops hfs_genfsops = {
177 1.9.6.2 yamt .gop_size = genfs_size,
178 1.9.6.2 yamt };
179 1.9.6.2 yamt
180 1.9.6.2 yamt int
181 1.9.6.4 yamt hfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
182 1.9.6.2 yamt {
183 1.9.6.4 yamt struct lwp *l = curlwp;
184 1.9.6.2 yamt struct nameidata nd;
185 1.9.6.2 yamt struct hfs_args *args = data;
186 1.9.6.2 yamt struct vnode *devvp;
187 1.9.6.2 yamt struct hfsmount *hmp;
188 1.9.6.2 yamt int error;
189 1.9.6.2 yamt int update;
190 1.9.6.2 yamt mode_t accessmode;
191 1.9.6.2 yamt
192 1.9.6.2 yamt if (*data_len < sizeof *args)
193 1.9.6.2 yamt return EINVAL;
194 1.9.6.2 yamt
195 1.9.6.2 yamt #ifdef HFS_DEBUG
196 1.9.6.2 yamt printf("vfsop = hfs_mount()\n");
197 1.9.6.2 yamt #endif /* HFS_DEBUG */
198 1.9.6.2 yamt
199 1.9.6.2 yamt if (mp->mnt_flag & MNT_GETARGS) {
200 1.9.6.2 yamt hmp = VFSTOHFS(mp);
201 1.9.6.2 yamt if (hmp == NULL)
202 1.9.6.2 yamt return EIO;
203 1.9.6.2 yamt args->fspec = NULL;
204 1.9.6.2 yamt *data_len = sizeof *args;
205 1.9.6.2 yamt return 0;
206 1.9.6.2 yamt }
207 1.9.6.2 yamt
208 1.9.6.2 yamt if (data == NULL)
209 1.9.6.2 yamt return EINVAL;
210 1.9.6.2 yamt
211 1.9.6.2 yamt /* FIXME: For development ONLY - disallow remounting for now */
212 1.9.6.2 yamt #if 0
213 1.9.6.2 yamt update = mp->mnt_flag & MNT_UPDATE;
214 1.9.6.2 yamt #else
215 1.9.6.2 yamt update = 0;
216 1.9.6.2 yamt #endif
217 1.9.6.2 yamt
218 1.9.6.2 yamt /* Check arguments */
219 1.9.6.2 yamt if (args->fspec != NULL) {
220 1.9.6.2 yamt /*
221 1.9.6.2 yamt * Look up the name and verify that it's sane.
222 1.9.6.2 yamt */
223 1.9.6.5 yamt NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
224 1.9.6.2 yamt if ((error = namei(&nd)) != 0)
225 1.9.6.2 yamt return error;
226 1.9.6.2 yamt devvp = nd.ni_vp;
227 1.9.6.2 yamt
228 1.9.6.2 yamt if (!update) {
229 1.9.6.2 yamt /*
230 1.9.6.2 yamt * Be sure this is a valid block device
231 1.9.6.2 yamt */
232 1.9.6.2 yamt if (devvp->v_type != VBLK)
233 1.9.6.2 yamt error = ENOTBLK;
234 1.9.6.2 yamt else if (bdevsw_lookup(devvp->v_rdev) == NULL)
235 1.9.6.2 yamt error = ENXIO;
236 1.9.6.2 yamt } else {
237 1.9.6.2 yamt /*
238 1.9.6.2 yamt * Be sure we're still naming the same device
239 1.9.6.2 yamt * used for our initial mount
240 1.9.6.2 yamt */
241 1.9.6.2 yamt hmp = VFSTOHFS(mp);
242 1.9.6.2 yamt if (devvp != hmp->hm_devvp)
243 1.9.6.2 yamt error = EINVAL;
244 1.9.6.2 yamt }
245 1.9.6.2 yamt } else {
246 1.9.6.2 yamt if (update) {
247 1.9.6.2 yamt /* Use the extant mount */
248 1.9.6.2 yamt hmp = VFSTOHFS(mp);
249 1.9.6.2 yamt devvp = hmp->hm_devvp;
250 1.9.6.2 yamt vref(devvp);
251 1.9.6.2 yamt } else {
252 1.9.6.2 yamt /* New mounts must have a filename for the device */
253 1.9.6.2 yamt return EINVAL;
254 1.9.6.2 yamt }
255 1.9.6.2 yamt }
256 1.9.6.2 yamt
257 1.9.6.2 yamt
258 1.9.6.2 yamt /*
259 1.9.6.2 yamt * If mount by non-root, then verify that user has necessary
260 1.9.6.2 yamt * permissions on the device.
261 1.9.6.2 yamt */
262 1.9.6.2 yamt if (error == 0 && kauth_authorize_generic(l->l_cred,
263 1.9.6.2 yamt KAUTH_GENERIC_ISSUSER, NULL) != 0) {
264 1.9.6.2 yamt accessmode = VREAD;
265 1.9.6.2 yamt if (update ?
266 1.9.6.2 yamt (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
267 1.9.6.2 yamt (mp->mnt_flag & MNT_RDONLY) == 0)
268 1.9.6.2 yamt accessmode |= VWRITE;
269 1.9.6.2 yamt vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
270 1.9.6.4 yamt error = VOP_ACCESS(devvp, accessmode, l->l_cred);
271 1.9.6.2 yamt VOP_UNLOCK(devvp, 0);
272 1.9.6.2 yamt }
273 1.9.6.2 yamt
274 1.9.6.2 yamt if (error != 0)
275 1.9.6.2 yamt goto error;
276 1.9.6.2 yamt
277 1.9.6.2 yamt if (update) {
278 1.9.6.2 yamt printf("HFS: live remounting not yet supported!\n");
279 1.9.6.2 yamt error = EINVAL;
280 1.9.6.2 yamt goto error;
281 1.9.6.2 yamt }
282 1.9.6.2 yamt
283 1.9.6.2 yamt if ((error = hfs_mountfs(devvp, mp, l, args->fspec)) != 0)
284 1.9.6.2 yamt goto error;
285 1.9.6.2 yamt
286 1.9.6.2 yamt error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
287 1.9.6.2 yamt mp->mnt_op->vfs_name, mp, l);
288 1.9.6.2 yamt
289 1.9.6.2 yamt #ifdef HFS_DEBUG
290 1.9.6.2 yamt if(!update) {
291 1.9.6.2 yamt char* volname;
292 1.9.6.2 yamt
293 1.9.6.2 yamt hmp = VFSTOHFS(mp);
294 1.9.6.2 yamt volname = malloc(hmp->hm_vol.name.length + 1, M_TEMP, M_WAITOK);
295 1.9.6.2 yamt if (volname == NULL)
296 1.9.6.2 yamt printf("could not allocate volname; ignored\n");
297 1.9.6.2 yamt else {
298 1.9.6.2 yamt if (hfs_unicode_to_ascii(hmp->hm_vol.name.unicode,
299 1.9.6.2 yamt hmp->hm_vol.name.length, volname) == NULL)
300 1.9.6.2 yamt printf("could not convert volume name to ascii; ignored\n");
301 1.9.6.2 yamt else
302 1.9.6.2 yamt printf("mounted volume \"%s\"\n", volname);
303 1.9.6.2 yamt free(volname, M_TEMP);
304 1.9.6.2 yamt }
305 1.9.6.2 yamt }
306 1.9.6.2 yamt #endif /* HFS_DEBUG */
307 1.9.6.2 yamt
308 1.9.6.2 yamt return error;
309 1.9.6.2 yamt
310 1.9.6.2 yamt error:
311 1.9.6.2 yamt vrele(devvp);
312 1.9.6.2 yamt return error;
313 1.9.6.2 yamt }
314 1.9.6.2 yamt
315 1.9.6.2 yamt int
316 1.9.6.4 yamt hfs_start(struct mount *mp, int flags)
317 1.9.6.2 yamt {
318 1.9.6.2 yamt
319 1.9.6.2 yamt #ifdef HFS_DEBUG
320 1.9.6.2 yamt printf("vfsop = hfs_start()\n");
321 1.9.6.2 yamt #endif /* HFS_DEBUG */
322 1.9.6.2 yamt
323 1.9.6.2 yamt return 0;
324 1.9.6.2 yamt }
325 1.9.6.2 yamt
326 1.9.6.2 yamt int
327 1.9.6.2 yamt hfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l,
328 1.9.6.2 yamt const char *devpath)
329 1.9.6.2 yamt {
330 1.9.6.2 yamt hfs_callback_args cbargs;
331 1.9.6.2 yamt hfs_libcb_argsopen argsopen;
332 1.9.6.2 yamt hfs_libcb_argsread argsread;
333 1.9.6.2 yamt struct hfsmount *hmp;
334 1.9.6.2 yamt kauth_cred_t cred;
335 1.9.6.2 yamt int error;
336 1.9.6.2 yamt
337 1.9.6.2 yamt cred = l ? l->l_cred : NOCRED;
338 1.9.6.2 yamt error = 0;
339 1.9.6.2 yamt hmp = NULL;
340 1.9.6.2 yamt
341 1.9.6.2 yamt /* Create mounted volume structure. */
342 1.9.6.2 yamt hmp = (struct hfsmount*)malloc(sizeof(struct hfsmount),
343 1.9.6.2 yamt M_HFSMNT, M_WAITOK);
344 1.9.6.2 yamt if (hmp == NULL) {
345 1.9.6.2 yamt error = ENOMEM;
346 1.9.6.2 yamt goto error;
347 1.9.6.2 yamt }
348 1.9.6.2 yamt memset(hmp, 0, sizeof(struct hfsmount));
349 1.9.6.2 yamt
350 1.9.6.2 yamt mp->mnt_data = hmp;
351 1.9.6.2 yamt mp->mnt_flag |= MNT_LOCAL;
352 1.9.6.2 yamt vfs_getnewfsid(mp);
353 1.9.6.2 yamt
354 1.9.6.2 yamt hmp->hm_mountp = mp;
355 1.9.6.2 yamt hmp->hm_dev = devvp->v_rdev;
356 1.9.6.2 yamt hmp->hm_devvp = devvp;
357 1.9.6.2 yamt
358 1.9.6.2 yamt /*
359 1.9.6.2 yamt * Use libhfs to open the volume and read the volume header and other
360 1.9.6.2 yamt * useful information.
361 1.9.6.2 yamt */
362 1.9.6.2 yamt
363 1.9.6.2 yamt hfslib_init_cbargs(&cbargs);
364 1.9.6.2 yamt argsopen.cred = argsread.cred = cred;
365 1.9.6.2 yamt argsopen.l = argsread.l = l;
366 1.9.6.2 yamt argsopen.devvp = devvp;
367 1.9.6.2 yamt cbargs.read = (void*)&argsread;
368 1.9.6.2 yamt cbargs.openvol = (void*)&argsopen;
369 1.9.6.2 yamt
370 1.9.6.2 yamt if ((error = hfslib_open_volume(devpath, mp->mnt_flag & MNT_RDONLY,
371 1.9.6.2 yamt &hmp->hm_vol, &cbargs)) != 0)
372 1.9.6.2 yamt goto error;
373 1.9.6.2 yamt
374 1.9.6.2 yamt /* Make sure this is not a journaled volume whose journal is dirty. */
375 1.9.6.2 yamt if (!hfslib_is_journal_clean(&hmp->hm_vol)) {
376 1.9.6.2 yamt printf("volume journal is dirty; not mounting\n");
377 1.9.6.2 yamt error = EIO;
378 1.9.6.2 yamt goto error;
379 1.9.6.2 yamt }
380 1.9.6.2 yamt
381 1.9.6.2 yamt mp->mnt_fs_bshift = 0;
382 1.9.6.2 yamt while ((1 << mp->mnt_fs_bshift) < hmp->hm_vol.vh.block_size)
383 1.9.6.2 yamt mp->mnt_fs_bshift++;
384 1.9.6.2 yamt mp->mnt_dev_bshift = DEV_BSHIFT;
385 1.9.6.2 yamt
386 1.9.6.2 yamt return 0;
387 1.9.6.2 yamt
388 1.9.6.2 yamt error:
389 1.9.6.2 yamt if (hmp != NULL)
390 1.9.6.2 yamt free(hmp, M_HFSMNT);
391 1.9.6.2 yamt
392 1.9.6.2 yamt return error;
393 1.9.6.2 yamt }
394 1.9.6.2 yamt
395 1.9.6.2 yamt int
396 1.9.6.4 yamt hfs_unmount(struct mount *mp, int mntflags)
397 1.9.6.2 yamt {
398 1.9.6.2 yamt hfs_callback_args cbargs;
399 1.9.6.2 yamt hfs_libcb_argsread argsclose;
400 1.9.6.2 yamt struct hfsmount* hmp;
401 1.9.6.2 yamt int error;
402 1.9.6.2 yamt int flags;
403 1.9.6.2 yamt
404 1.9.6.2 yamt #ifdef HFS_DEBUG
405 1.9.6.2 yamt printf("vfsop = hfs_unmount()\n");
406 1.9.6.2 yamt #endif /* HFS_DEBUG */
407 1.9.6.2 yamt
408 1.9.6.2 yamt hmp = VFSTOHFS(mp);
409 1.9.6.2 yamt
410 1.9.6.2 yamt flags = 0;
411 1.9.6.2 yamt if (mntflags & MNT_FORCE)
412 1.9.6.2 yamt flags |= FORCECLOSE;
413 1.9.6.2 yamt
414 1.9.6.2 yamt if ((error = vflush(mp, NULLVP, flags)) != 0)
415 1.9.6.2 yamt return error;
416 1.9.6.2 yamt
417 1.9.6.2 yamt hfslib_init_cbargs(&cbargs);
418 1.9.6.4 yamt argsclose.l = curlwp;
419 1.9.6.2 yamt cbargs.closevol = (void*)&argsclose;
420 1.9.6.2 yamt hfslib_close_volume(&hmp->hm_vol, &cbargs);
421 1.9.6.2 yamt
422 1.9.6.2 yamt vput(hmp->hm_devvp);
423 1.9.6.2 yamt
424 1.9.6.2 yamt free(hmp, M_HFSMNT);
425 1.9.6.2 yamt mp->mnt_data = NULL;
426 1.9.6.2 yamt mp->mnt_flag &= ~MNT_LOCAL;
427 1.9.6.2 yamt
428 1.9.6.2 yamt return error;
429 1.9.6.2 yamt }
430 1.9.6.2 yamt
431 1.9.6.2 yamt int
432 1.9.6.2 yamt hfs_root(struct mount *mp, struct vnode **vpp)
433 1.9.6.2 yamt {
434 1.9.6.2 yamt struct vnode *nvp;
435 1.9.6.2 yamt int error;
436 1.9.6.2 yamt
437 1.9.6.2 yamt #ifdef HFS_DEBUG
438 1.9.6.2 yamt printf("vfsop = hfs_root()\n");
439 1.9.6.2 yamt #endif /* HFS_DEBUG */
440 1.9.6.2 yamt
441 1.9.6.2 yamt if ((error = VFS_VGET(mp, HFS_CNID_ROOT_FOLDER, &nvp)) != 0)
442 1.9.6.2 yamt return error;
443 1.9.6.2 yamt *vpp = nvp;
444 1.9.6.2 yamt
445 1.9.6.2 yamt return 0;
446 1.9.6.2 yamt }
447 1.9.6.2 yamt
448 1.9.6.2 yamt int
449 1.9.6.4 yamt hfs_statvfs(struct mount *mp, struct statvfs *sbp)
450 1.9.6.2 yamt {
451 1.9.6.2 yamt hfs_volume_header_t *vh;
452 1.9.6.2 yamt
453 1.9.6.2 yamt #ifdef HFS_DEBUG
454 1.9.6.2 yamt printf("vfsop = hfs_statvfs()\n");
455 1.9.6.2 yamt #endif /* HFS_DEBUG */
456 1.9.6.2 yamt
457 1.9.6.2 yamt vh = &VFSTOHFS(mp)->hm_vol.vh;
458 1.9.6.2 yamt
459 1.9.6.2 yamt sbp->f_bsize = vh->block_size;
460 1.9.6.2 yamt sbp->f_frsize = sbp->f_bsize;
461 1.9.6.2 yamt sbp->f_iosize = 4096;/* mac os x uses a 4 kb io size, so do the same */
462 1.9.6.2 yamt sbp->f_blocks = vh->total_blocks;
463 1.9.6.2 yamt sbp->f_bfree = vh->free_blocks; /* total free blocks */
464 1.9.6.2 yamt sbp->f_bavail = vh->free_blocks; /* blocks free for non superuser */
465 1.9.6.2 yamt sbp->f_bresvd = 0;
466 1.9.6.2 yamt sbp->f_files = vh->file_count; /* total files */
467 1.9.6.2 yamt sbp->f_ffree = (1<<31) - vh->file_count; /* free file nodes */
468 1.9.6.2 yamt copy_statvfs_info(sbp, mp);
469 1.9.6.2 yamt
470 1.9.6.2 yamt return 0;
471 1.9.6.2 yamt }
472 1.9.6.2 yamt
473 1.9.6.2 yamt int
474 1.9.6.4 yamt hfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
475 1.9.6.2 yamt {
476 1.9.6.2 yamt
477 1.9.6.2 yamt #ifdef HFS_DEBUG
478 1.9.6.2 yamt printf("vfsop = hfs_sync()\n");
479 1.9.6.2 yamt #endif /* HFS_DEBUG */
480 1.9.6.2 yamt
481 1.9.6.2 yamt return 0;
482 1.9.6.2 yamt }
483 1.9.6.2 yamt
484 1.9.6.2 yamt /*
485 1.9.6.2 yamt * an ino_t corresponds directly to a CNID in our particular case,
486 1.9.6.2 yamt * since both are conveniently 32-bit numbers
487 1.9.6.2 yamt */
488 1.9.6.2 yamt int
489 1.9.6.2 yamt hfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
490 1.9.6.2 yamt {
491 1.9.6.2 yamt return hfs_vget_internal(mp, ino, HFS_DATAFORK, vpp);
492 1.9.6.2 yamt }
493 1.9.6.2 yamt
494 1.9.6.2 yamt /*
495 1.9.6.2 yamt * internal version with extra arguments to allow accessing resource fork
496 1.9.6.2 yamt */
497 1.9.6.2 yamt int
498 1.9.6.2 yamt hfs_vget_internal(struct mount *mp, ino_t ino, uint8_t fork,
499 1.9.6.2 yamt struct vnode **vpp)
500 1.9.6.2 yamt {
501 1.9.6.2 yamt struct hfsmount *hmp;
502 1.9.6.2 yamt struct hfsnode *hnode;
503 1.9.6.2 yamt struct vnode *vp;
504 1.9.6.2 yamt hfs_callback_args cbargs;
505 1.9.6.2 yamt hfs_cnid_t cnid;
506 1.9.6.2 yamt hfs_catalog_keyed_record_t rec;
507 1.9.6.2 yamt hfs_catalog_key_t key; /* the search key used to find this file on disk */
508 1.9.6.2 yamt dev_t dev;
509 1.9.6.2 yamt int error;
510 1.9.6.2 yamt
511 1.9.6.2 yamt #ifdef HFS_DEBUG
512 1.9.6.2 yamt printf("vfsop = hfs_vget()\n");
513 1.9.6.2 yamt #endif /* HFS_DEBUG */
514 1.9.6.2 yamt
515 1.9.6.2 yamt hnode = NULL;
516 1.9.6.2 yamt vp = NULL;
517 1.9.6.2 yamt hmp = VFSTOHFS(mp);
518 1.9.6.2 yamt dev = hmp->hm_dev;
519 1.9.6.2 yamt cnid = (hfs_cnid_t)ino;
520 1.9.6.2 yamt
521 1.9.6.2 yamt if (fork != HFS_RSRCFORK)
522 1.9.6.2 yamt fork = HFS_DATAFORK;
523 1.9.6.2 yamt
524 1.9.6.6 yamt retry:
525 1.9.6.2 yamt /* Check if this vnode has already been allocated. If so, just return it. */
526 1.9.6.2 yamt if ((*vpp = hfs_nhashget(dev, cnid, fork, LK_EXCLUSIVE)) != NULL)
527 1.9.6.2 yamt return 0;
528 1.9.6.2 yamt
529 1.9.6.2 yamt /* Allocate a new vnode/inode. */
530 1.9.6.2 yamt if ((error = getnewvnode(VT_HFS, mp, hfs_vnodeop_p, &vp)) != 0)
531 1.9.6.2 yamt goto error;
532 1.9.6.6 yamt MALLOC(hnode, struct hfsnode *, sizeof(struct hfsnode), M_TEMP,
533 1.9.6.6 yamt M_WAITOK + M_ZERO);
534 1.9.6.2 yamt
535 1.9.6.2 yamt /*
536 1.9.6.2 yamt * If someone beat us to it while sleeping in getnewvnode(),
537 1.9.6.2 yamt * push back the freshly allocated vnode we don't need, and return.
538 1.9.6.2 yamt */
539 1.9.6.6 yamt mutex_enter(&hfs_hashlock);
540 1.9.6.6 yamt if (hfs_nhashget(dev, cnid, fork, 0) != NULL) {
541 1.9.6.6 yamt mutex_exit(&hfs_hashlock);
542 1.9.6.6 yamt ungetnewvnode(vp);
543 1.9.6.6 yamt FREE(hnode, M_TEMP);
544 1.9.6.6 yamt goto retry;
545 1.9.6.6 yamt }
546 1.9.6.2 yamt
547 1.9.6.6 yamt vp->v_vflag |= VV_LOCKSWORK;
548 1.9.6.2 yamt vp->v_data = hnode;
549 1.9.6.4 yamt genfs_node_init(vp, &hfs_genfsops);
550 1.9.6.2 yamt
551 1.9.6.2 yamt hnode->h_vnode = vp;
552 1.9.6.2 yamt hnode->h_hmp = hmp;
553 1.9.6.2 yamt hnode->dummy = 0x1337BABE;
554 1.9.6.2 yamt
555 1.9.6.2 yamt /*
556 1.9.6.2 yamt * We need to put this vnode into the hash chain and lock it so that other
557 1.9.6.2 yamt * requests for this inode will block if they arrive while we are sleeping
558 1.9.6.2 yamt * waiting for old data structures to be purged or for the contents of the
559 1.9.6.2 yamt * disk portion of this inode to be read. The hash chain requires the node's
560 1.9.6.2 yamt * device and cnid to be known. Since this information was passed in the
561 1.9.6.2 yamt * arguments, fill in the appropriate hfsnode fields without reading having
562 1.9.6.2 yamt * to read the disk.
563 1.9.6.2 yamt */
564 1.9.6.2 yamt hnode->h_dev = dev;
565 1.9.6.2 yamt hnode->h_rec.cnid = cnid;
566 1.9.6.2 yamt hnode->h_fork = fork;
567 1.9.6.2 yamt
568 1.9.6.2 yamt hfs_nhashinsert(hnode);
569 1.9.6.6 yamt mutex_exit(&hfs_hashlock);
570 1.9.6.2 yamt
571 1.9.6.2 yamt
572 1.9.6.2 yamt /*
573 1.9.6.2 yamt * Read catalog record from disk.
574 1.9.6.2 yamt */
575 1.9.6.2 yamt hfslib_init_cbargs(&cbargs);
576 1.9.6.2 yamt
577 1.9.6.2 yamt if (hfslib_find_catalog_record_with_cnid(&hmp->hm_vol, cnid,
578 1.9.6.2 yamt &rec, &key, &cbargs) != 0) {
579 1.9.6.2 yamt vput(vp);
580 1.9.6.2 yamt error = EBADF;
581 1.9.6.2 yamt goto error;
582 1.9.6.2 yamt }
583 1.9.6.2 yamt
584 1.9.6.2 yamt memcpy(&hnode->h_rec, &rec, sizeof(hnode->h_rec));
585 1.9.6.2 yamt hnode->h_parent = key.parent_cnid;
586 1.9.6.2 yamt
587 1.9.6.2 yamt /* XXX Eventually need to add an "ignore permissions" mount option */
588 1.9.6.2 yamt
589 1.9.6.2 yamt /*
590 1.9.6.2 yamt * Now convert some of the catalog record's fields into values that make
591 1.9.6.2 yamt * sense on this system.
592 1.9.6.2 yamt */
593 1.9.6.2 yamt /* DATE AND TIME */
594 1.9.6.2 yamt
595 1.9.6.2 yamt /*
596 1.9.6.2 yamt * Initialize the vnode from the hfsnode, check for aliases.
597 1.9.6.4 yamt * Note that the underlying vnode may change.
598 1.9.6.2 yamt */
599 1.9.6.2 yamt hfs_vinit(mp, hfs_specop_p, hfs_fifoop_p, &vp);
600 1.9.6.2 yamt
601 1.9.6.2 yamt hnode->h_devvp = hmp->hm_devvp;
602 1.9.6.2 yamt VREF(hnode->h_devvp); /* Increment the ref count to the volume's device. */
603 1.9.6.2 yamt
604 1.9.6.2 yamt /* Make sure UVM has allocated enough memory. (?) */
605 1.9.6.2 yamt if (hnode->h_rec.rec_type == HFS_REC_FILE) {
606 1.9.6.2 yamt if (hnode->h_fork == HFS_DATAFORK)
607 1.9.6.2 yamt uvm_vnp_setsize(vp,
608 1.9.6.2 yamt hnode->h_rec.file.data_fork.logical_size);
609 1.9.6.2 yamt else
610 1.9.6.2 yamt uvm_vnp_setsize(vp,
611 1.9.6.2 yamt hnode->h_rec.file.rsrc_fork.logical_size);
612 1.9.6.2 yamt }
613 1.9.6.2 yamt else
614 1.9.6.2 yamt uvm_vnp_setsize(vp, 0); /* no directly reading directories */
615 1.9.6.2 yamt
616 1.9.6.2 yamt *vpp = vp;
617 1.9.6.2 yamt
618 1.9.6.2 yamt return 0;
619 1.9.6.2 yamt
620 1.9.6.2 yamt error:
621 1.9.6.2 yamt *vpp = NULL;
622 1.9.6.2 yamt return error;
623 1.9.6.2 yamt }
624 1.9.6.2 yamt
625 1.9.6.2 yamt int
626 1.9.6.2 yamt hfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
627 1.9.6.2 yamt {
628 1.9.6.2 yamt
629 1.9.6.2 yamt #ifdef HFS_DEBUG
630 1.9.6.2 yamt printf("vfsop = hfs_fhtovp()\n");
631 1.9.6.2 yamt #endif /* HFS_DEBUG */
632 1.9.6.2 yamt
633 1.9.6.2 yamt return EOPNOTSUPP;
634 1.9.6.2 yamt }
635 1.9.6.2 yamt
636 1.9.6.2 yamt int
637 1.9.6.2 yamt hfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
638 1.9.6.2 yamt {
639 1.9.6.2 yamt
640 1.9.6.2 yamt #ifdef HFS_DEBUG
641 1.9.6.2 yamt printf("vfsop = hfs_vptofh()\n");
642 1.9.6.2 yamt #endif /* HFS_DEBUG */
643 1.9.6.2 yamt
644 1.9.6.2 yamt return EOPNOTSUPP;
645 1.9.6.2 yamt }
646 1.9.6.2 yamt
647 1.9.6.2 yamt void
648 1.9.6.2 yamt hfs_init(void)
649 1.9.6.2 yamt {
650 1.9.6.2 yamt hfs_callbacks callbacks;
651 1.9.6.2 yamt
652 1.9.6.2 yamt #ifdef HFS_DEBUG
653 1.9.6.2 yamt printf("vfsop = hfs_init()\n");
654 1.9.6.2 yamt #endif /* HFS_DEBUG */
655 1.9.6.2 yamt
656 1.9.6.2 yamt malloc_type_attach(M_HFSMNT);
657 1.9.6.2 yamt
658 1.9.6.2 yamt callbacks.error = hfs_libcb_error;
659 1.9.6.2 yamt callbacks.allocmem = hfs_libcb_malloc;
660 1.9.6.2 yamt callbacks.reallocmem = hfs_libcb_realloc;
661 1.9.6.2 yamt callbacks.freemem = hfs_libcb_free;
662 1.9.6.2 yamt callbacks.openvol = hfs_libcb_opendev;
663 1.9.6.2 yamt callbacks.closevol = hfs_libcb_closedev;
664 1.9.6.2 yamt callbacks.read = hfs_libcb_read;
665 1.9.6.2 yamt
666 1.9.6.2 yamt hfs_nhashinit();
667 1.9.6.2 yamt hfslib_init(&callbacks);
668 1.9.6.2 yamt }
669 1.9.6.2 yamt
670 1.9.6.2 yamt void
671 1.9.6.2 yamt hfs_reinit(void)
672 1.9.6.2 yamt {
673 1.9.6.2 yamt
674 1.9.6.2 yamt #ifdef HFS_DEBUG
675 1.9.6.2 yamt printf("vfsop = hfs_reinit()\n");
676 1.9.6.2 yamt #endif /* HFS_DEBUG */
677 1.9.6.2 yamt
678 1.9.6.2 yamt return;
679 1.9.6.2 yamt }
680 1.9.6.2 yamt
681 1.9.6.2 yamt void
682 1.9.6.2 yamt hfs_done(void)
683 1.9.6.2 yamt {
684 1.9.6.2 yamt
685 1.9.6.2 yamt #ifdef HFS_DEBUG
686 1.9.6.2 yamt printf("vfsop = hfs_done()\n");
687 1.9.6.2 yamt #endif /* HFS_DEBUG */
688 1.9.6.2 yamt
689 1.9.6.2 yamt malloc_type_detach(M_HFSMNT);
690 1.9.6.2 yamt
691 1.9.6.2 yamt hfslib_done();
692 1.9.6.2 yamt hfs_nhashdone();
693 1.9.6.2 yamt }
694 1.9.6.2 yamt
695 1.9.6.2 yamt int
696 1.9.6.2 yamt hfs_mountroot(void)
697 1.9.6.2 yamt {
698 1.9.6.2 yamt
699 1.9.6.2 yamt #ifdef HFS_DEBUG
700 1.9.6.2 yamt printf("vfsop = hfs_mountroot()\n");
701 1.9.6.2 yamt #endif /* HFS_DEBUG */
702 1.9.6.2 yamt
703 1.9.6.2 yamt return EOPNOTSUPP;
704 1.9.6.2 yamt }
705