hfs_vfsops.c revision 1.9.6.3 1 1.9.6.3 yamt /* $NetBSD: hfs_vfsops.c,v 1.9.6.3 2007/10/27 11:35:04 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.3 yamt __KERNEL_RCSID(0, "$NetBSD: hfs_vfsops.c,v 1.9.6.3 2007/10/27 11:35:04 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.2 yamt #include <miscfs/specfs/specdev.h>
132 1.9.6.2 yamt
133 1.9.6.2 yamt #include <fs/hfs/hfs.h>
134 1.9.6.2 yamt #include <fs/hfs/libhfs.h>
135 1.9.6.2 yamt
136 1.9.6.2 yamt MALLOC_JUSTDEFINE(M_HFSMNT, "hfs mount", "hfs mount structures");
137 1.9.6.2 yamt
138 1.9.6.2 yamt extern struct lock hfs_hashlock;
139 1.9.6.2 yamt
140 1.9.6.2 yamt const struct vnodeopv_desc * const hfs_vnodeopv_descs[] = {
141 1.9.6.2 yamt &hfs_vnodeop_opv_desc,
142 1.9.6.2 yamt &hfs_specop_opv_desc,
143 1.9.6.2 yamt &hfs_fifoop_opv_desc,
144 1.9.6.2 yamt NULL,
145 1.9.6.2 yamt };
146 1.9.6.2 yamt
147 1.9.6.2 yamt struct vfsops hfs_vfsops = {
148 1.9.6.2 yamt MOUNT_HFS,
149 1.9.6.2 yamt sizeof (struct hfs_args),
150 1.9.6.2 yamt hfs_mount,
151 1.9.6.2 yamt hfs_start,
152 1.9.6.2 yamt hfs_unmount,
153 1.9.6.2 yamt hfs_root,
154 1.9.6.2 yamt hfs_quotactl,
155 1.9.6.2 yamt hfs_statvfs,
156 1.9.6.2 yamt hfs_sync,
157 1.9.6.2 yamt hfs_vget,
158 1.9.6.2 yamt hfs_fhtovp,
159 1.9.6.2 yamt hfs_vptofh,
160 1.9.6.2 yamt hfs_init,
161 1.9.6.2 yamt hfs_reinit,
162 1.9.6.2 yamt hfs_done,
163 1.9.6.2 yamt NULL, /* vfs_mountroot */
164 1.9.6.2 yamt NULL, /* vfs_snapshot */
165 1.9.6.2 yamt hfs_extattrctl,
166 1.9.6.2 yamt (void *)eopnotsupp, /* vfs_suspendctl */
167 1.9.6.2 yamt hfs_vnodeopv_descs,
168 1.9.6.2 yamt 0,
169 1.9.6.2 yamt { NULL, NULL },
170 1.9.6.2 yamt };
171 1.9.6.2 yamt VFS_ATTACH(hfs_vfsops); /* XXX Is this needed? */
172 1.9.6.2 yamt
173 1.9.6.2 yamt static const struct genfs_ops hfs_genfsops = {
174 1.9.6.2 yamt .gop_size = genfs_size,
175 1.9.6.2 yamt };
176 1.9.6.2 yamt
177 1.9.6.2 yamt int
178 1.9.6.2 yamt hfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len,
179 1.9.6.2 yamt struct lwp *l)
180 1.9.6.2 yamt {
181 1.9.6.2 yamt struct nameidata nd;
182 1.9.6.2 yamt struct hfs_args *args = data;
183 1.9.6.2 yamt struct vnode *devvp;
184 1.9.6.2 yamt struct hfsmount *hmp;
185 1.9.6.2 yamt int error;
186 1.9.6.2 yamt int update;
187 1.9.6.2 yamt mode_t accessmode;
188 1.9.6.2 yamt
189 1.9.6.2 yamt if (*data_len < sizeof *args)
190 1.9.6.2 yamt return EINVAL;
191 1.9.6.2 yamt
192 1.9.6.2 yamt #ifdef HFS_DEBUG
193 1.9.6.2 yamt printf("vfsop = hfs_mount()\n");
194 1.9.6.2 yamt #endif /* HFS_DEBUG */
195 1.9.6.2 yamt
196 1.9.6.2 yamt if (mp->mnt_flag & MNT_GETARGS) {
197 1.9.6.2 yamt hmp = VFSTOHFS(mp);
198 1.9.6.2 yamt if (hmp == NULL)
199 1.9.6.2 yamt return EIO;
200 1.9.6.2 yamt args->fspec = NULL;
201 1.9.6.2 yamt *data_len = sizeof *args;
202 1.9.6.2 yamt return 0;
203 1.9.6.2 yamt }
204 1.9.6.2 yamt
205 1.9.6.2 yamt if (data == NULL)
206 1.9.6.2 yamt return EINVAL;
207 1.9.6.2 yamt
208 1.9.6.2 yamt /* FIXME: For development ONLY - disallow remounting for now */
209 1.9.6.2 yamt #if 0
210 1.9.6.2 yamt update = mp->mnt_flag & MNT_UPDATE;
211 1.9.6.2 yamt #else
212 1.9.6.2 yamt update = 0;
213 1.9.6.2 yamt #endif
214 1.9.6.2 yamt
215 1.9.6.2 yamt /* Check arguments */
216 1.9.6.2 yamt if (args->fspec != NULL) {
217 1.9.6.2 yamt /*
218 1.9.6.2 yamt * Look up the name and verify that it's sane.
219 1.9.6.2 yamt */
220 1.9.6.2 yamt NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec, l);
221 1.9.6.2 yamt if ((error = namei(&nd)) != 0)
222 1.9.6.2 yamt return error;
223 1.9.6.2 yamt devvp = nd.ni_vp;
224 1.9.6.2 yamt
225 1.9.6.2 yamt if (!update) {
226 1.9.6.2 yamt /*
227 1.9.6.2 yamt * Be sure this is a valid block device
228 1.9.6.2 yamt */
229 1.9.6.2 yamt if (devvp->v_type != VBLK)
230 1.9.6.2 yamt error = ENOTBLK;
231 1.9.6.2 yamt else if (bdevsw_lookup(devvp->v_rdev) == NULL)
232 1.9.6.2 yamt error = ENXIO;
233 1.9.6.2 yamt } else {
234 1.9.6.2 yamt /*
235 1.9.6.2 yamt * Be sure we're still naming the same device
236 1.9.6.2 yamt * used for our initial mount
237 1.9.6.2 yamt */
238 1.9.6.2 yamt hmp = VFSTOHFS(mp);
239 1.9.6.2 yamt if (devvp != hmp->hm_devvp)
240 1.9.6.2 yamt error = EINVAL;
241 1.9.6.2 yamt }
242 1.9.6.2 yamt } else {
243 1.9.6.2 yamt if (update) {
244 1.9.6.2 yamt /* Use the extant mount */
245 1.9.6.2 yamt hmp = VFSTOHFS(mp);
246 1.9.6.2 yamt devvp = hmp->hm_devvp;
247 1.9.6.2 yamt vref(devvp);
248 1.9.6.2 yamt } else {
249 1.9.6.2 yamt /* New mounts must have a filename for the device */
250 1.9.6.2 yamt return EINVAL;
251 1.9.6.2 yamt }
252 1.9.6.2 yamt }
253 1.9.6.2 yamt
254 1.9.6.2 yamt
255 1.9.6.2 yamt /*
256 1.9.6.2 yamt * If mount by non-root, then verify that user has necessary
257 1.9.6.2 yamt * permissions on the device.
258 1.9.6.2 yamt */
259 1.9.6.2 yamt if (error == 0 && kauth_authorize_generic(l->l_cred,
260 1.9.6.2 yamt KAUTH_GENERIC_ISSUSER, NULL) != 0) {
261 1.9.6.2 yamt accessmode = VREAD;
262 1.9.6.2 yamt if (update ?
263 1.9.6.2 yamt (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
264 1.9.6.2 yamt (mp->mnt_flag & MNT_RDONLY) == 0)
265 1.9.6.2 yamt accessmode |= VWRITE;
266 1.9.6.2 yamt vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
267 1.9.6.2 yamt error = VOP_ACCESS(devvp, accessmode, l->l_cred, l);
268 1.9.6.2 yamt VOP_UNLOCK(devvp, 0);
269 1.9.6.2 yamt }
270 1.9.6.2 yamt
271 1.9.6.2 yamt if (error != 0)
272 1.9.6.2 yamt goto error;
273 1.9.6.2 yamt
274 1.9.6.2 yamt if (update) {
275 1.9.6.2 yamt printf("HFS: live remounting not yet supported!\n");
276 1.9.6.2 yamt error = EINVAL;
277 1.9.6.2 yamt goto error;
278 1.9.6.2 yamt }
279 1.9.6.2 yamt
280 1.9.6.2 yamt /*
281 1.9.6.2 yamt * Disallow multiple mounts of the same device.
282 1.9.6.2 yamt * Disallow mounting of a device that is currently in use
283 1.9.6.2 yamt * (except for root, which might share swap device for miniroot).
284 1.9.6.2 yamt * Flush out any old buffers remaining from a previous use.
285 1.9.6.2 yamt */
286 1.9.6.2 yamt if ((error = vfs_mountedon(devvp)) != 0)
287 1.9.6.2 yamt goto error;
288 1.9.6.2 yamt if (vcount(devvp) > 1 && devvp != rootvp) {
289 1.9.6.2 yamt error = EBUSY;
290 1.9.6.2 yamt goto error;
291 1.9.6.2 yamt }
292 1.9.6.2 yamt
293 1.9.6.2 yamt if ((error = hfs_mountfs(devvp, mp, l, args->fspec)) != 0)
294 1.9.6.2 yamt goto error;
295 1.9.6.2 yamt
296 1.9.6.2 yamt error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
297 1.9.6.2 yamt mp->mnt_op->vfs_name, mp, l);
298 1.9.6.2 yamt
299 1.9.6.2 yamt #ifdef HFS_DEBUG
300 1.9.6.2 yamt if(!update) {
301 1.9.6.2 yamt char* volname;
302 1.9.6.2 yamt
303 1.9.6.2 yamt hmp = VFSTOHFS(mp);
304 1.9.6.2 yamt volname = malloc(hmp->hm_vol.name.length + 1, M_TEMP, M_WAITOK);
305 1.9.6.2 yamt if (volname == NULL)
306 1.9.6.2 yamt printf("could not allocate volname; ignored\n");
307 1.9.6.2 yamt else {
308 1.9.6.2 yamt if (hfs_unicode_to_ascii(hmp->hm_vol.name.unicode,
309 1.9.6.2 yamt hmp->hm_vol.name.length, volname) == NULL)
310 1.9.6.2 yamt printf("could not convert volume name to ascii; ignored\n");
311 1.9.6.2 yamt else
312 1.9.6.2 yamt printf("mounted volume \"%s\"\n", volname);
313 1.9.6.2 yamt free(volname, M_TEMP);
314 1.9.6.2 yamt }
315 1.9.6.2 yamt }
316 1.9.6.2 yamt #endif /* HFS_DEBUG */
317 1.9.6.2 yamt
318 1.9.6.2 yamt return error;
319 1.9.6.2 yamt
320 1.9.6.2 yamt error:
321 1.9.6.2 yamt vrele(devvp);
322 1.9.6.2 yamt return error;
323 1.9.6.2 yamt }
324 1.9.6.2 yamt
325 1.9.6.2 yamt int
326 1.9.6.2 yamt hfs_start(struct mount *mp, int flags, struct lwp *l)
327 1.9.6.2 yamt {
328 1.9.6.2 yamt
329 1.9.6.2 yamt #ifdef HFS_DEBUG
330 1.9.6.2 yamt printf("vfsop = hfs_start()\n");
331 1.9.6.2 yamt #endif /* HFS_DEBUG */
332 1.9.6.2 yamt
333 1.9.6.2 yamt return 0;
334 1.9.6.2 yamt }
335 1.9.6.2 yamt
336 1.9.6.2 yamt int
337 1.9.6.2 yamt hfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l,
338 1.9.6.2 yamt const char *devpath)
339 1.9.6.2 yamt {
340 1.9.6.2 yamt hfs_callback_args cbargs;
341 1.9.6.2 yamt hfs_libcb_argsopen argsopen;
342 1.9.6.2 yamt hfs_libcb_argsread argsread;
343 1.9.6.2 yamt struct hfsmount *hmp;
344 1.9.6.2 yamt kauth_cred_t cred;
345 1.9.6.2 yamt int error;
346 1.9.6.2 yamt
347 1.9.6.2 yamt cred = l ? l->l_cred : NOCRED;
348 1.9.6.2 yamt error = 0;
349 1.9.6.2 yamt hmp = NULL;
350 1.9.6.2 yamt
351 1.9.6.2 yamt /* Create mounted volume structure. */
352 1.9.6.2 yamt hmp = (struct hfsmount*)malloc(sizeof(struct hfsmount),
353 1.9.6.2 yamt M_HFSMNT, M_WAITOK);
354 1.9.6.2 yamt if (hmp == NULL) {
355 1.9.6.2 yamt error = ENOMEM;
356 1.9.6.2 yamt goto error;
357 1.9.6.2 yamt }
358 1.9.6.2 yamt memset(hmp, 0, sizeof(struct hfsmount));
359 1.9.6.2 yamt
360 1.9.6.2 yamt mp->mnt_data = hmp;
361 1.9.6.2 yamt mp->mnt_flag |= MNT_LOCAL;
362 1.9.6.2 yamt vfs_getnewfsid(mp);
363 1.9.6.2 yamt
364 1.9.6.2 yamt hmp->hm_mountp = mp;
365 1.9.6.2 yamt hmp->hm_dev = devvp->v_rdev;
366 1.9.6.2 yamt hmp->hm_devvp = devvp;
367 1.9.6.2 yamt
368 1.9.6.2 yamt /*
369 1.9.6.2 yamt * Use libhfs to open the volume and read the volume header and other
370 1.9.6.2 yamt * useful information.
371 1.9.6.2 yamt */
372 1.9.6.2 yamt
373 1.9.6.2 yamt hfslib_init_cbargs(&cbargs);
374 1.9.6.2 yamt argsopen.cred = argsread.cred = cred;
375 1.9.6.2 yamt argsopen.l = argsread.l = l;
376 1.9.6.2 yamt argsopen.devvp = devvp;
377 1.9.6.2 yamt cbargs.read = (void*)&argsread;
378 1.9.6.2 yamt cbargs.openvol = (void*)&argsopen;
379 1.9.6.2 yamt
380 1.9.6.2 yamt if ((error = hfslib_open_volume(devpath, mp->mnt_flag & MNT_RDONLY,
381 1.9.6.2 yamt &hmp->hm_vol, &cbargs)) != 0)
382 1.9.6.2 yamt goto error;
383 1.9.6.2 yamt
384 1.9.6.2 yamt /* Make sure this is not a journaled volume whose journal is dirty. */
385 1.9.6.2 yamt if (!hfslib_is_journal_clean(&hmp->hm_vol)) {
386 1.9.6.2 yamt printf("volume journal is dirty; not mounting\n");
387 1.9.6.2 yamt error = EIO;
388 1.9.6.2 yamt goto error;
389 1.9.6.2 yamt }
390 1.9.6.2 yamt
391 1.9.6.2 yamt mp->mnt_fs_bshift = 0;
392 1.9.6.2 yamt while ((1 << mp->mnt_fs_bshift) < hmp->hm_vol.vh.block_size)
393 1.9.6.2 yamt mp->mnt_fs_bshift++;
394 1.9.6.2 yamt mp->mnt_dev_bshift = DEV_BSHIFT;
395 1.9.6.2 yamt
396 1.9.6.2 yamt return 0;
397 1.9.6.2 yamt
398 1.9.6.2 yamt error:
399 1.9.6.2 yamt if (hmp != NULL)
400 1.9.6.2 yamt free(hmp, M_HFSMNT);
401 1.9.6.2 yamt
402 1.9.6.2 yamt return error;
403 1.9.6.2 yamt }
404 1.9.6.2 yamt
405 1.9.6.2 yamt int
406 1.9.6.2 yamt hfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
407 1.9.6.2 yamt {
408 1.9.6.2 yamt hfs_callback_args cbargs;
409 1.9.6.2 yamt hfs_libcb_argsread argsclose;
410 1.9.6.2 yamt struct hfsmount* hmp;
411 1.9.6.2 yamt int error;
412 1.9.6.2 yamt int flags;
413 1.9.6.2 yamt
414 1.9.6.2 yamt #ifdef HFS_DEBUG
415 1.9.6.2 yamt printf("vfsop = hfs_unmount()\n");
416 1.9.6.2 yamt #endif /* HFS_DEBUG */
417 1.9.6.2 yamt
418 1.9.6.2 yamt hmp = VFSTOHFS(mp);
419 1.9.6.2 yamt
420 1.9.6.2 yamt flags = 0;
421 1.9.6.2 yamt if (mntflags & MNT_FORCE)
422 1.9.6.2 yamt flags |= FORCECLOSE;
423 1.9.6.2 yamt
424 1.9.6.2 yamt if ((error = vflush(mp, NULLVP, flags)) != 0)
425 1.9.6.2 yamt return error;
426 1.9.6.2 yamt
427 1.9.6.2 yamt hfslib_init_cbargs(&cbargs);
428 1.9.6.2 yamt argsclose.l = l;
429 1.9.6.2 yamt cbargs.closevol = (void*)&argsclose;
430 1.9.6.2 yamt hfslib_close_volume(&hmp->hm_vol, &cbargs);
431 1.9.6.2 yamt
432 1.9.6.2 yamt vput(hmp->hm_devvp);
433 1.9.6.2 yamt
434 1.9.6.2 yamt free(hmp, M_HFSMNT);
435 1.9.6.2 yamt mp->mnt_data = NULL;
436 1.9.6.2 yamt mp->mnt_flag &= ~MNT_LOCAL;
437 1.9.6.2 yamt
438 1.9.6.2 yamt return error;
439 1.9.6.2 yamt }
440 1.9.6.2 yamt
441 1.9.6.2 yamt int
442 1.9.6.2 yamt hfs_root(struct mount *mp, struct vnode **vpp)
443 1.9.6.2 yamt {
444 1.9.6.2 yamt struct vnode *nvp;
445 1.9.6.2 yamt int error;
446 1.9.6.2 yamt
447 1.9.6.2 yamt #ifdef HFS_DEBUG
448 1.9.6.2 yamt printf("vfsop = hfs_root()\n");
449 1.9.6.2 yamt #endif /* HFS_DEBUG */
450 1.9.6.2 yamt
451 1.9.6.2 yamt if ((error = VFS_VGET(mp, HFS_CNID_ROOT_FOLDER, &nvp)) != 0)
452 1.9.6.2 yamt return error;
453 1.9.6.2 yamt *vpp = nvp;
454 1.9.6.2 yamt
455 1.9.6.2 yamt return 0;
456 1.9.6.2 yamt }
457 1.9.6.2 yamt
458 1.9.6.2 yamt int
459 1.9.6.2 yamt hfs_quotactl(struct mount *mp, int cmds, uid_t uid, void *arg,
460 1.9.6.2 yamt struct lwp *l)
461 1.9.6.2 yamt {
462 1.9.6.2 yamt
463 1.9.6.2 yamt #ifdef HFS_DEBUG
464 1.9.6.2 yamt printf("vfsop = hfs_quotactl()\n");
465 1.9.6.2 yamt #endif /* HFS_DEBUG */
466 1.9.6.2 yamt
467 1.9.6.2 yamt return EOPNOTSUPP;
468 1.9.6.2 yamt }
469 1.9.6.2 yamt
470 1.9.6.2 yamt int
471 1.9.6.2 yamt hfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
472 1.9.6.2 yamt {
473 1.9.6.2 yamt hfs_volume_header_t *vh;
474 1.9.6.2 yamt
475 1.9.6.2 yamt #ifdef HFS_DEBUG
476 1.9.6.2 yamt printf("vfsop = hfs_statvfs()\n");
477 1.9.6.2 yamt #endif /* HFS_DEBUG */
478 1.9.6.2 yamt
479 1.9.6.2 yamt vh = &VFSTOHFS(mp)->hm_vol.vh;
480 1.9.6.2 yamt
481 1.9.6.2 yamt sbp->f_bsize = vh->block_size;
482 1.9.6.2 yamt sbp->f_frsize = sbp->f_bsize;
483 1.9.6.2 yamt sbp->f_iosize = 4096;/* mac os x uses a 4 kb io size, so do the same */
484 1.9.6.2 yamt sbp->f_blocks = vh->total_blocks;
485 1.9.6.2 yamt sbp->f_bfree = vh->free_blocks; /* total free blocks */
486 1.9.6.2 yamt sbp->f_bavail = vh->free_blocks; /* blocks free for non superuser */
487 1.9.6.2 yamt sbp->f_bresvd = 0;
488 1.9.6.2 yamt sbp->f_files = vh->file_count; /* total files */
489 1.9.6.2 yamt sbp->f_ffree = (1<<31) - vh->file_count; /* free file nodes */
490 1.9.6.2 yamt copy_statvfs_info(sbp, mp);
491 1.9.6.2 yamt
492 1.9.6.2 yamt return 0;
493 1.9.6.2 yamt }
494 1.9.6.2 yamt
495 1.9.6.2 yamt int
496 1.9.6.2 yamt hfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *l)
497 1.9.6.2 yamt {
498 1.9.6.2 yamt
499 1.9.6.2 yamt #ifdef HFS_DEBUG
500 1.9.6.2 yamt printf("vfsop = hfs_sync()\n");
501 1.9.6.2 yamt #endif /* HFS_DEBUG */
502 1.9.6.2 yamt
503 1.9.6.2 yamt return 0;
504 1.9.6.2 yamt }
505 1.9.6.2 yamt
506 1.9.6.2 yamt /*
507 1.9.6.2 yamt * an ino_t corresponds directly to a CNID in our particular case,
508 1.9.6.2 yamt * since both are conveniently 32-bit numbers
509 1.9.6.2 yamt */
510 1.9.6.2 yamt int
511 1.9.6.2 yamt hfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
512 1.9.6.2 yamt {
513 1.9.6.2 yamt return hfs_vget_internal(mp, ino, HFS_DATAFORK, vpp);
514 1.9.6.2 yamt }
515 1.9.6.2 yamt
516 1.9.6.2 yamt /*
517 1.9.6.2 yamt * internal version with extra arguments to allow accessing resource fork
518 1.9.6.2 yamt */
519 1.9.6.2 yamt int
520 1.9.6.2 yamt hfs_vget_internal(struct mount *mp, ino_t ino, uint8_t fork,
521 1.9.6.2 yamt struct vnode **vpp)
522 1.9.6.2 yamt {
523 1.9.6.2 yamt struct hfsmount *hmp;
524 1.9.6.2 yamt struct hfsnode *hnode;
525 1.9.6.2 yamt struct vnode *vp;
526 1.9.6.2 yamt hfs_callback_args cbargs;
527 1.9.6.2 yamt hfs_cnid_t cnid;
528 1.9.6.2 yamt hfs_catalog_keyed_record_t rec;
529 1.9.6.2 yamt hfs_catalog_key_t key; /* the search key used to find this file on disk */
530 1.9.6.2 yamt dev_t dev;
531 1.9.6.2 yamt int error;
532 1.9.6.2 yamt
533 1.9.6.2 yamt #ifdef HFS_DEBUG
534 1.9.6.2 yamt printf("vfsop = hfs_vget()\n");
535 1.9.6.2 yamt #endif /* HFS_DEBUG */
536 1.9.6.2 yamt
537 1.9.6.2 yamt hnode = NULL;
538 1.9.6.2 yamt vp = NULL;
539 1.9.6.2 yamt hmp = VFSTOHFS(mp);
540 1.9.6.2 yamt dev = hmp->hm_dev;
541 1.9.6.2 yamt cnid = (hfs_cnid_t)ino;
542 1.9.6.2 yamt
543 1.9.6.2 yamt if (fork != HFS_RSRCFORK)
544 1.9.6.2 yamt fork = HFS_DATAFORK;
545 1.9.6.2 yamt
546 1.9.6.2 yamt /* Check if this vnode has already been allocated. If so, just return it. */
547 1.9.6.2 yamt if ((*vpp = hfs_nhashget(dev, cnid, fork, LK_EXCLUSIVE)) != NULL)
548 1.9.6.2 yamt return 0;
549 1.9.6.2 yamt
550 1.9.6.2 yamt /* Allocate a new vnode/inode. */
551 1.9.6.2 yamt if ((error = getnewvnode(VT_HFS, mp, hfs_vnodeop_p, &vp)) != 0)
552 1.9.6.2 yamt goto error;
553 1.9.6.2 yamt
554 1.9.6.2 yamt /*
555 1.9.6.2 yamt * If someone beat us to it while sleeping in getnewvnode(),
556 1.9.6.2 yamt * push back the freshly allocated vnode we don't need, and return.
557 1.9.6.2 yamt */
558 1.9.6.2 yamt
559 1.9.6.2 yamt do {
560 1.9.6.2 yamt if ((*vpp = hfs_nhashget(dev, cnid, fork, LK_EXCLUSIVE))
561 1.9.6.2 yamt != NULL) {
562 1.9.6.2 yamt ungetnewvnode(vp);
563 1.9.6.2 yamt return 0;
564 1.9.6.2 yamt }
565 1.9.6.2 yamt } while (lockmgr(&hfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
566 1.9.6.2 yamt
567 1.9.6.3 yamt vp->v_vflag |= VV_LOCKSWORK;
568 1.9.6.2 yamt
569 1.9.6.2 yamt MALLOC(hnode, struct hfsnode *, sizeof(struct hfsnode), M_TEMP,
570 1.9.6.2 yamt M_WAITOK + M_ZERO);
571 1.9.6.2 yamt vp->v_data = hnode;
572 1.9.6.2 yamt
573 1.9.6.2 yamt hnode->h_vnode = vp;
574 1.9.6.2 yamt hnode->h_hmp = hmp;
575 1.9.6.2 yamt hnode->dummy = 0x1337BABE;
576 1.9.6.2 yamt
577 1.9.6.2 yamt /*
578 1.9.6.2 yamt * We need to put this vnode into the hash chain and lock it so that other
579 1.9.6.2 yamt * requests for this inode will block if they arrive while we are sleeping
580 1.9.6.2 yamt * waiting for old data structures to be purged or for the contents of the
581 1.9.6.2 yamt * disk portion of this inode to be read. The hash chain requires the node's
582 1.9.6.2 yamt * device and cnid to be known. Since this information was passed in the
583 1.9.6.2 yamt * arguments, fill in the appropriate hfsnode fields without reading having
584 1.9.6.2 yamt * to read the disk.
585 1.9.6.2 yamt */
586 1.9.6.2 yamt hnode->h_dev = dev;
587 1.9.6.2 yamt hnode->h_rec.cnid = cnid;
588 1.9.6.2 yamt hnode->h_fork = fork;
589 1.9.6.2 yamt
590 1.9.6.2 yamt hfs_nhashinsert(hnode);
591 1.9.6.2 yamt lockmgr(&hfs_hashlock, LK_RELEASE, 0);
592 1.9.6.2 yamt
593 1.9.6.2 yamt
594 1.9.6.2 yamt /*
595 1.9.6.2 yamt * Read catalog record from disk.
596 1.9.6.2 yamt */
597 1.9.6.2 yamt hfslib_init_cbargs(&cbargs);
598 1.9.6.2 yamt
599 1.9.6.2 yamt if (hfslib_find_catalog_record_with_cnid(&hmp->hm_vol, cnid,
600 1.9.6.2 yamt &rec, &key, &cbargs) != 0) {
601 1.9.6.2 yamt vput(vp);
602 1.9.6.2 yamt error = EBADF;
603 1.9.6.2 yamt goto error;
604 1.9.6.2 yamt }
605 1.9.6.2 yamt
606 1.9.6.2 yamt memcpy(&hnode->h_rec, &rec, sizeof(hnode->h_rec));
607 1.9.6.2 yamt hnode->h_parent = key.parent_cnid;
608 1.9.6.2 yamt
609 1.9.6.2 yamt /* XXX Eventually need to add an "ignore permissions" mount option */
610 1.9.6.2 yamt
611 1.9.6.2 yamt /*
612 1.9.6.2 yamt * Now convert some of the catalog record's fields into values that make
613 1.9.6.2 yamt * sense on this system.
614 1.9.6.2 yamt */
615 1.9.6.2 yamt /* DATE AND TIME */
616 1.9.6.2 yamt
617 1.9.6.2 yamt /*
618 1.9.6.2 yamt * Initialize the vnode from the hfsnode, check for aliases.
619 1.9.6.2 yamt * Note that the underlying vnode may have changed.
620 1.9.6.2 yamt */
621 1.9.6.2 yamt
622 1.9.6.2 yamt hfs_vinit(mp, hfs_specop_p, hfs_fifoop_p, &vp);
623 1.9.6.2 yamt
624 1.9.6.2 yamt genfs_node_init(vp, &hfs_genfsops);
625 1.9.6.2 yamt hnode->h_devvp = hmp->hm_devvp;
626 1.9.6.2 yamt VREF(hnode->h_devvp); /* Increment the ref count to the volume's device. */
627 1.9.6.2 yamt
628 1.9.6.2 yamt /* Make sure UVM has allocated enough memory. (?) */
629 1.9.6.2 yamt if (hnode->h_rec.rec_type == HFS_REC_FILE) {
630 1.9.6.2 yamt if (hnode->h_fork == HFS_DATAFORK)
631 1.9.6.2 yamt uvm_vnp_setsize(vp,
632 1.9.6.2 yamt hnode->h_rec.file.data_fork.logical_size);
633 1.9.6.2 yamt else
634 1.9.6.2 yamt uvm_vnp_setsize(vp,
635 1.9.6.2 yamt hnode->h_rec.file.rsrc_fork.logical_size);
636 1.9.6.2 yamt }
637 1.9.6.2 yamt else
638 1.9.6.2 yamt uvm_vnp_setsize(vp, 0); /* no directly reading directories */
639 1.9.6.2 yamt
640 1.9.6.2 yamt *vpp = vp;
641 1.9.6.2 yamt
642 1.9.6.2 yamt return 0;
643 1.9.6.2 yamt
644 1.9.6.2 yamt error:
645 1.9.6.2 yamt *vpp = NULL;
646 1.9.6.2 yamt return error;
647 1.9.6.2 yamt }
648 1.9.6.2 yamt
649 1.9.6.2 yamt int
650 1.9.6.2 yamt hfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
651 1.9.6.2 yamt {
652 1.9.6.2 yamt
653 1.9.6.2 yamt #ifdef HFS_DEBUG
654 1.9.6.2 yamt printf("vfsop = hfs_fhtovp()\n");
655 1.9.6.2 yamt #endif /* HFS_DEBUG */
656 1.9.6.2 yamt
657 1.9.6.2 yamt return EOPNOTSUPP;
658 1.9.6.2 yamt }
659 1.9.6.2 yamt
660 1.9.6.2 yamt int
661 1.9.6.2 yamt hfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
662 1.9.6.2 yamt {
663 1.9.6.2 yamt
664 1.9.6.2 yamt #ifdef HFS_DEBUG
665 1.9.6.2 yamt printf("vfsop = hfs_vptofh()\n");
666 1.9.6.2 yamt #endif /* HFS_DEBUG */
667 1.9.6.2 yamt
668 1.9.6.2 yamt return EOPNOTSUPP;
669 1.9.6.2 yamt }
670 1.9.6.2 yamt
671 1.9.6.2 yamt void
672 1.9.6.2 yamt hfs_init(void)
673 1.9.6.2 yamt {
674 1.9.6.2 yamt hfs_callbacks callbacks;
675 1.9.6.2 yamt
676 1.9.6.2 yamt #ifdef HFS_DEBUG
677 1.9.6.2 yamt printf("vfsop = hfs_init()\n");
678 1.9.6.2 yamt #endif /* HFS_DEBUG */
679 1.9.6.2 yamt
680 1.9.6.2 yamt malloc_type_attach(M_HFSMNT);
681 1.9.6.2 yamt
682 1.9.6.2 yamt callbacks.error = hfs_libcb_error;
683 1.9.6.2 yamt callbacks.allocmem = hfs_libcb_malloc;
684 1.9.6.2 yamt callbacks.reallocmem = hfs_libcb_realloc;
685 1.9.6.2 yamt callbacks.freemem = hfs_libcb_free;
686 1.9.6.2 yamt callbacks.openvol = hfs_libcb_opendev;
687 1.9.6.2 yamt callbacks.closevol = hfs_libcb_closedev;
688 1.9.6.2 yamt callbacks.read = hfs_libcb_read;
689 1.9.6.2 yamt
690 1.9.6.2 yamt hfs_nhashinit();
691 1.9.6.2 yamt hfslib_init(&callbacks);
692 1.9.6.2 yamt }
693 1.9.6.2 yamt
694 1.9.6.2 yamt void
695 1.9.6.2 yamt hfs_reinit(void)
696 1.9.6.2 yamt {
697 1.9.6.2 yamt
698 1.9.6.2 yamt #ifdef HFS_DEBUG
699 1.9.6.2 yamt printf("vfsop = hfs_reinit()\n");
700 1.9.6.2 yamt #endif /* HFS_DEBUG */
701 1.9.6.2 yamt
702 1.9.6.2 yamt return;
703 1.9.6.2 yamt }
704 1.9.6.2 yamt
705 1.9.6.2 yamt void
706 1.9.6.2 yamt hfs_done(void)
707 1.9.6.2 yamt {
708 1.9.6.2 yamt
709 1.9.6.2 yamt #ifdef HFS_DEBUG
710 1.9.6.2 yamt printf("vfsop = hfs_done()\n");
711 1.9.6.2 yamt #endif /* HFS_DEBUG */
712 1.9.6.2 yamt
713 1.9.6.2 yamt malloc_type_detach(M_HFSMNT);
714 1.9.6.2 yamt
715 1.9.6.2 yamt hfslib_done();
716 1.9.6.2 yamt hfs_nhashdone();
717 1.9.6.2 yamt }
718 1.9.6.2 yamt
719 1.9.6.2 yamt int
720 1.9.6.2 yamt hfs_mountroot(void)
721 1.9.6.2 yamt {
722 1.9.6.2 yamt
723 1.9.6.2 yamt #ifdef HFS_DEBUG
724 1.9.6.2 yamt printf("vfsop = hfs_mountroot()\n");
725 1.9.6.2 yamt #endif /* HFS_DEBUG */
726 1.9.6.2 yamt
727 1.9.6.2 yamt return EOPNOTSUPP;
728 1.9.6.2 yamt }
729 1.9.6.2 yamt
730 1.9.6.2 yamt int hfs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
731 1.9.6.2 yamt int attrnamespace, const char *attrname, struct lwp *l)
732 1.9.6.2 yamt {
733 1.9.6.2 yamt
734 1.9.6.2 yamt #ifdef HFS_DEBUG
735 1.9.6.2 yamt printf("vfsop = hfs_checkexp()\n");
736 1.9.6.2 yamt #endif /* HFS_DEBUG */
737 1.9.6.2 yamt
738 1.9.6.2 yamt return EOPNOTSUPP;
739 1.9.6.2 yamt }
740