hfs_vnops.c revision 1.37 1 /* $NetBSD: hfs_vnops.c,v 1.37 2021/06/29 22:34:06 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Yevgeny Binder and Dieter Baron.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1992, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software donated to Berkeley by
37 * Jan-Simon Pendry.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 /*
65 * Copyright (c) 1982, 1986, 1989, 1993, 1995
66 * The Regents of the University of California. All rights reserved.
67 * (c) UNIX System Laboratories, Inc.
68 * All or some portions of this file are derived from material licensed
69 * to the University of California by American Telephone and Telegraph
70 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
71 * the permission of UNIX System Laboratories, Inc.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
84 *
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * SUCH DAMAGE.
96 */
97
98
99 /*
100 * Apple HFS+ filesystem
101 */
102
103 #include <sys/cdefs.h>
104 __KERNEL_RCSID(0, "$NetBSD: hfs_vnops.c,v 1.37 2021/06/29 22:34:06 dholland Exp $");
105
106 #ifdef _KERNEL_OPT
107 #include "opt_ipsec.h"
108 #endif
109
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/kernel.h>
113 #include <sys/vmmeter.h>
114 #include <sys/time.h>
115 #include <sys/proc.h>
116 #include <sys/vnode.h>
117 #include <sys/malloc.h>
118 #include <sys/pool.h>
119 #include <sys/file.h>
120 #include <sys/stat.h>
121 #include <sys/mount.h>
122 #include <sys/namei.h>
123 #include <sys/buf.h>
124 #include <sys/dirent.h>
125 #include <sys/msgbuf.h>
126
127 #include <miscfs/fifofs/fifo.h>
128 #include <miscfs/specfs/specdev.h>
129
130 #include <fs/hfs/hfs.h>
131 #include <fs/hfs/unicode.h>
132
133 #include <miscfs/genfs/genfs.h>
134
135 int hfs_vop_lookup(void *);
136 int hfs_vop_open(void *);
137 int hfs_vop_close(void *);
138 int hfs_vop_access(void *);
139 int hfs_vop_getattr(void *);
140 int hfs_vop_setattr(void *);
141 int hfs_vop_bmap(void *);
142 int hfs_vop_read(void *);
143 int hfs_vop_readdir(void *);
144 int hfs_vop_readlink(void *);
145 int hfs_vop_reclaim(void *);
146 int hfs_vop_print(void *);
147
148 #ifdef HFS_DEBUG
149 #define DPRINTF(a) printf a
150 #else
151 #define DPRINTF(a)
152 #endif
153
154
155 int (**hfs_vnodeop_p) (void *);
156 const struct vnodeopv_entry_desc hfs_vnodeop_entries[] = {
157 { &vop_default_desc, vn_default_error },
158 { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
159 { &vop_lookup_desc, hfs_vop_lookup }, /* lookup */
160 { &vop_create_desc, genfs_eopnotsupp }, /* create */
161 { &vop_whiteout_desc, genfs_eopnotsupp }, /* whiteout */
162 { &vop_mknod_desc, genfs_eopnotsupp }, /* mknod */
163 { &vop_open_desc, hfs_vop_open }, /* open */
164 { &vop_close_desc, hfs_vop_close }, /* close */
165 { &vop_access_desc, hfs_vop_access }, /* access */
166 { &vop_accessx_desc, genfs_accessx }, /* accessx */
167 { &vop_getattr_desc, hfs_vop_getattr }, /* getattr */
168 { &vop_setattr_desc, hfs_vop_setattr }, /* setattr */
169 { &vop_read_desc, hfs_vop_read }, /* read */
170 { &vop_write_desc, genfs_eopnotsupp }, /* write */
171 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
172 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
173 { &vop_ioctl_desc, genfs_eopnotsupp }, /* ioctl */
174 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
175 { &vop_poll_desc, genfs_eopnotsupp }, /* poll */
176 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
177 { &vop_revoke_desc, genfs_eopnotsupp }, /* revoke */
178 { &vop_mmap_desc, genfs_mmap }, /* mmap */
179 { &vop_fsync_desc, genfs_nullop }, /* fsync */
180 { &vop_seek_desc, genfs_seek }, /* seek */
181 { &vop_remove_desc, genfs_eopnotsupp }, /* remove */
182 { &vop_link_desc, genfs_eopnotsupp }, /* link */
183 { &vop_rename_desc, genfs_eopnotsupp }, /* rename */
184 { &vop_mkdir_desc, genfs_eopnotsupp }, /* mkdir */
185 { &vop_rmdir_desc, genfs_eopnotsupp }, /* rmdir */
186 { &vop_symlink_desc, genfs_eopnotsupp }, /* symlink */
187 { &vop_readdir_desc, hfs_vop_readdir }, /* readdir */
188 { &vop_readlink_desc, hfs_vop_readlink }, /* readlink */
189 { &vop_abortop_desc, genfs_abortop }, /* abortop */
190 { &vop_inactive_desc, genfs_eopnotsupp }, /* inactive */
191 { &vop_reclaim_desc, hfs_vop_reclaim }, /* reclaim */
192 { &vop_lock_desc, genfs_lock }, /* lock */
193 { &vop_unlock_desc, genfs_unlock }, /* unlock */
194 { &vop_bmap_desc, hfs_vop_bmap }, /* bmap */
195 { &vop_strategy_desc, genfs_eopnotsupp }, /* strategy */
196 { &vop_print_desc, hfs_vop_print }, /* print */
197 { &vop_islocked_desc, genfs_islocked }, /* islocked */
198 { &vop_pathconf_desc, genfs_eopnotsupp }, /* pathconf */
199 { &vop_advlock_desc, genfs_eopnotsupp }, /* advlock */
200 { &vop_bwrite_desc, genfs_eopnotsupp }, /* bwrite */
201 { &vop_getpages_desc, genfs_getpages }, /* getpages */
202 { &vop_putpages_desc, genfs_putpages }, /* putpages */
203 { &vop_openextattr_desc, genfs_eopnotsupp }, /* openextattr */
204 { &vop_closeextattr_desc, genfs_eopnotsupp }, /* closeextattr */
205 { &vop_getextattr_desc, genfs_eopnotsupp }, /* getextattr */
206 { &vop_setextattr_desc, genfs_eopnotsupp }, /* setextattr */
207 { &vop_listextattr_desc, genfs_eopnotsupp }, /* listextattr */
208 { &vop_deleteextattr_desc, genfs_eopnotsupp }, /* deleteextattr */
209 { NULL, NULL }
210 };
211 const struct vnodeopv_desc hfs_vnodeop_opv_desc =
212 { &hfs_vnodeop_p, hfs_vnodeop_entries };
213
214 int (**hfs_specop_p) (void *);
215 const struct vnodeopv_entry_desc hfs_specop_entries[] = {
216 { &vop_default_desc, vn_default_error },
217 { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
218 { &vop_lookup_desc, spec_lookup }, /* lookup */
219 { &vop_create_desc, spec_create }, /* create */
220 { &vop_mknod_desc, spec_mknod }, /* mknod */
221 { &vop_open_desc, spec_open }, /* open */
222 { &vop_close_desc, spec_close }, /* close */
223 { &vop_access_desc, hfs_vop_access }, /* access */
224 { &vop_accessx_desc, genfs_accessx }, /* accessx */
225 { &vop_getattr_desc, hfs_vop_getattr }, /* getattr */
226 { &vop_setattr_desc, hfs_vop_setattr }, /* setattr */
227 { &vop_read_desc, spec_read }, /* read */
228 { &vop_write_desc, spec_write }, /* write */
229 { &vop_fallocate_desc, spec_fallocate }, /* fallocate */
230 { &vop_fdiscard_desc, spec_fdiscard }, /* fdiscard */
231 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
232 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
233 { &vop_poll_desc, spec_poll }, /* poll */
234 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
235 { &vop_revoke_desc, spec_revoke }, /* revoke */
236 { &vop_mmap_desc, spec_mmap }, /* mmap */
237 { &vop_fsync_desc, spec_fsync }, /* fsync */
238 { &vop_seek_desc, spec_seek }, /* seek */
239 { &vop_remove_desc, spec_remove }, /* remove */
240 { &vop_link_desc, spec_link }, /* link */
241 { &vop_rename_desc, spec_rename }, /* rename */
242 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
243 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
244 { &vop_symlink_desc, spec_symlink }, /* symlink */
245 { &vop_readdir_desc, spec_readdir }, /* readdir */
246 { &vop_readlink_desc, spec_readlink }, /* readlink */
247 { &vop_abortop_desc, spec_abortop }, /* abortop */
248 { &vop_inactive_desc, genfs_eopnotsupp }, /* inactive */
249 { &vop_reclaim_desc, hfs_vop_reclaim }, /* reclaim */
250 { &vop_lock_desc, genfs_lock }, /* lock */
251 { &vop_unlock_desc, genfs_unlock }, /* unlock */
252 { &vop_bmap_desc, spec_bmap }, /* bmap */
253 { &vop_strategy_desc, spec_strategy }, /* strategy */
254 { &vop_print_desc, hfs_vop_print }, /* print */
255 { &vop_islocked_desc, genfs_islocked }, /* islocked */
256 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
257 { &vop_advlock_desc, spec_advlock }, /* advlock */
258 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
259 { &vop_getpages_desc, spec_getpages }, /* getpages */
260 { &vop_putpages_desc, spec_putpages }, /* putpages */
261 #if 0
262 { &vop_openextattr_desc, ffs_openextattr }, /* openextattr */
263 { &vop_closeextattr_desc, ffs_closeextattr }, /* closeextattr */
264 { &vop_getextattr_desc, ffs_getextattr }, /* getextattr */
265 { &vop_setextattr_desc, ffs_setextattr }, /* setextattr */
266 { &vop_listextattr_desc, ffs_listextattr }, /* listextattr */
267 { &vop_deleteextattr_desc, ffs_deleteextattr }, /* deleteextattr */
268 #endif
269 { NULL, NULL }
270 };
271 const struct vnodeopv_desc hfs_specop_opv_desc =
272 { &hfs_specop_p, hfs_specop_entries };
273
274 int (**hfs_fifoop_p) (void *);
275 const struct vnodeopv_entry_desc hfs_fifoop_entries[] = {
276 { &vop_default_desc, vn_default_error },
277 { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
278 { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
279 { &vop_create_desc, vn_fifo_bypass }, /* create */
280 { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
281 { &vop_open_desc, vn_fifo_bypass }, /* open */
282 { &vop_close_desc, vn_fifo_bypass }, /* close */
283 { &vop_access_desc, hfs_vop_access }, /* access */
284 { &vop_accessx_desc, genfs_accessx }, /* accessx */
285 { &vop_getattr_desc, hfs_vop_getattr }, /* getattr */
286 { &vop_setattr_desc, hfs_vop_setattr }, /* setattr */
287 { &vop_read_desc, vn_fifo_bypass }, /* read */
288 { &vop_write_desc, vn_fifo_bypass }, /* write */
289 { &vop_fallocate_desc, vn_fifo_bypass }, /* fallocate */
290 { &vop_fdiscard_desc, vn_fifo_bypass }, /* fdiscard */
291 { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
292 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
293 { &vop_poll_desc, vn_fifo_bypass }, /* poll */
294 { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
295 { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
296 { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
297 { &vop_fsync_desc, vn_fifo_bypass }, /* fsync */
298 { &vop_seek_desc, vn_fifo_bypass }, /* seek */
299 { &vop_remove_desc, vn_fifo_bypass }, /* remove */
300 { &vop_link_desc, vn_fifo_bypass }, /* link */
301 { &vop_rename_desc, vn_fifo_bypass }, /* rename */
302 { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
303 { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
304 { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
305 { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
306 { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
307 { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
308 { &vop_inactive_desc, genfs_eopnotsupp }, /* inactive */
309 { &vop_reclaim_desc, hfs_vop_reclaim }, /* reclaim */
310 { &vop_lock_desc, genfs_lock }, /* lock */
311 { &vop_unlock_desc, genfs_unlock }, /* unlock */
312 { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
313 { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
314 { &vop_print_desc, hfs_vop_print }, /* print */
315 { &vop_islocked_desc, genfs_islocked }, /* islocked */
316 { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
317 { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
318 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
319 { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
320 #if 0
321 { &vop_openextattr_desc, ffs_openextattr }, /* openextattr */
322 { &vop_closeextattr_desc, ffs_closeextattr }, /* closeextattr */
323 { &vop_getextattr_desc, ffs_getextattr }, /* getextattr */
324 { &vop_setextattr_desc, ffs_setextattr }, /* setextattr */
325 { &vop_listextattr_desc, ffs_listextattr }, /* listextattr */
326 { &vop_deleteextattr_desc, ffs_deleteextattr }, /* deleteextattr */
327 #endif
328 { NULL, NULL }
329 };
330 const struct vnodeopv_desc hfs_fifoop_opv_desc =
331 { &hfs_fifoop_p, hfs_fifoop_entries };
332
333 int
334 hfs_vop_lookup(void *v)
335 {
336 struct vop_lookup_v2_args /* {
337 struct vnode * a_dvp;
338 struct vnode ** a_vpp;
339 struct componentname * a_cnp;
340 } */ *ap = v;
341 struct componentname *cnp;
342 struct hfsnode *dp; /* hfsnode for directory being searched */
343 kauth_cred_t cred;
344 struct vnode **vpp; /* resultant vnode */
345 struct vnode *tdp; /* returned by VFS_VGET */
346 struct vnode *vdp; /* vnode for directory being searched */
347 hfs_catalog_key_t key; /* hfs+ catalog search key for requested child */
348 hfs_catalog_keyed_record_t rec; /* catalog record of requested child */
349 unichar_t* unicn; /* name of component, in Unicode */
350 const char *pname;
351 int error;
352 int flags;
353 int result; /* result of libhfs operations */
354
355 DPRINTF(("VOP = hfs_vop_lookup()\n"));
356
357 cnp = ap->a_cnp;
358 cred = cnp->cn_cred;
359 vdp = ap->a_dvp;
360 dp = VTOH(vdp);
361 error = 0;
362 pname = cnp->cn_nameptr;
363 result = 0;
364 unicn = NULL;
365 vpp = ap->a_vpp;
366 *vpp = NULL;
367
368 flags = cnp->cn_flags;
369
370
371 /*
372 * Check accessiblity of directory.
373 */
374 if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
375 return error;
376
377 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
378 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
379 return EROFS;
380
381 /*
382 * We now have a segment name to search for, and a directory to search.
383 *
384 * Before tediously performing a linear scan of the directory,
385 * check the name cache to see if the directory/name pair
386 * we are looking for is known already.
387 */
388 /* XXX Cache disabled until we can make sure it works. */
389 #if 0
390 if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
391 return error;
392 #endif
393
394
395 #if 0
396 if (cnp->cn_namelen == 1 && *pname == '.') {
397 *vpp = vdp;
398 vref(vdp);
399 return 0;
400 }
401 #endif
402
403 if (flags & ISDOTDOT) {
404 DPRINTF(("DOTDOT "));
405 error = hfs_vget_internal(vdp->v_mount, dp->h_parent,
406 HFS_RSRCFORK, &tdp);
407 if (error != 0)
408 goto error;
409 *vpp = tdp;
410 #if 0
411 } else if (dp->h_rec.u.cnid == rec.file.u.cnid) {
412 #endif
413 } else if (cnp->cn_namelen == 1 && pname[0] == '.') {
414 DPRINTF(("DOT "));
415 vref(vdp); /* we want ourself, ie "." */
416 *vpp = vdp;
417 } else {
418 hfs_callback_args cbargs;
419 uint8_t len, ni;
420
421 hfslib_init_cbargs(&cbargs);
422
423 /* XXX: when decomposing, string could grow
424 and we have to handle overflow */
425 unicn = malloc(cnp->cn_namelen * sizeof(unicn[0]),
426 M_TEMP, M_WAITOK);
427 len = utf8_to_utf16(unicn, cnp->cn_namelen,
428 cnp->cn_nameptr, cnp->cn_namelen, 0, NULL);
429 for (ni = 0; ni < len; ni++)
430 if (unicn[ni] == (unichar_t)':')
431 unicn[ni] = (unichar_t)'/';
432 /* XXX: check conversion errors? */
433 if (hfslib_make_catalog_key(VTOH(vdp)->h_rec.u.cnid, len, unicn,
434 &key) == 0) {
435 DPRINTF(("ERROR in hfslib_make_catalog_key\n"));
436 error = EINVAL;
437 goto error;
438 }
439
440 result = hfslib_find_catalog_record_with_key(&dp->h_hmp->hm_vol,
441 &key, &rec, &cbargs);
442 if (result > 0) {
443 error = EINVAL;
444 goto error;
445 }
446 if (result < 0) {
447 if (cnp->cn_nameiop == CREATE)
448 error = EROFS;
449 else
450 error = ENOENT;
451 goto error;
452 }
453
454 if (rec.file.user_info.file_type == HFS_HARD_LINK_FILE_TYPE
455 && rec.file.user_info.file_creator == HFS_HFSLUS_CREATOR) {
456 if (hfslib_get_hardlink(&dp->h_hmp->hm_vol,
457 rec.file.bsd.special.inode_num,
458 &rec, &cbargs) != 0) {
459 error = EINVAL;
460 goto error;
461 }
462 }
463
464 if (rec.type == HFS_REC_FILE
465 && strcmp(cnp->cn_nameptr+cnp->cn_namelen, "/rsrc") == 0
466 && rec.file.rsrc_fork.logical_size > 0) {
467 /* advance namei next pointer to end of stirng */
468 cnp->cn_consume = 5;
469 cnp->cn_flags &= ~REQUIREDIR; /* XXX: needed? */
470 error = hfs_vget_internal(vdp->v_mount, rec.file.cnid,
471 HFS_RSRCFORK, &tdp);
472 } else
473 error = hfs_vget_internal(vdp->v_mount, rec.file.cnid,
474 HFS_DATAFORK, &tdp);
475 if (error != 0)
476 goto error;
477 *vpp = tdp;
478 }
479 DPRINTF(("\n"));
480 /*
481 * Insert name into cache if appropriate.
482 */
483 /* XXX Cache disabled until we can make sure it works. */
484 #if 0
485 cache_enter(vdp, *vpp, cnp);
486 #endif
487
488 error = 0;
489
490 /* FALLTHROUGH */
491 error:
492 if (unicn != NULL)
493 free(unicn, M_TEMP);
494
495 return error;
496 }
497
498 int
499 hfs_vop_open(void *v)
500 {
501 #if 0
502 struct vop_open_args /* {
503 struct vnode *a_vp;
504 int a_mode;
505 kauth_cred_t a_cred;
506 } */ *ap = v;
507 struct hfsnode *hn = VTOH(ap->a_vp);
508 #endif
509 DPRINTF(("VOP = hfs_vop_open()\n"));
510
511 /*
512 * XXX This is a good place to read and cache the file's extents to
513 * XXX avoid doing it upon every read/write. Must however keep the
514 * XXX cache in sync when the file grows/shrinks. (So would that go
515 * XXX in vop_truncate?)
516 */
517
518 return 0;
519 }
520
521 int
522 hfs_vop_close(void *v)
523 {
524 #if 0
525 struct vop_close_args /* {
526 struct vnode *a_vp;
527 int a_fflag;
528 kauth_cred_t a_cred;
529 } */ *ap = v;
530 struct hfsnode *hn = VTOH(ap->a_vp);
531 #endif
532 DPRINTF(("VOP = hfs_vop_close()\n"));
533
534 /* Release extents cache here. */
535
536 return 0;
537 }
538
539 static int
540 hfs_check_possible(struct vnode *vp, accmode_t accmode)
541 {
542
543 /*
544 * Disallow writes on files, directories, and symlinks
545 * since we have no write support yet.
546 */
547
548 if (accmode & VWRITE) {
549 switch (vp->v_type) {
550 case VDIR:
551 case VLNK:
552 case VREG:
553 return EROFS;
554 default:
555 break;
556 }
557 }
558
559 return 0;
560 }
561
562 static int
563 hfs_check_permitted(vnode_t *vp, struct vattr *va, accmode_t accmode,
564 kauth_cred_t cred)
565 {
566
567 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
568 va->va_type, va->va_mode), vp, NULL, genfs_can_access(vp, cred,
569 va->va_uid, va->va_gid, va->va_mode, NULL, accmode));
570 }
571
572 int
573 hfs_vop_access(void *v)
574 {
575 struct vop_access_args /* {
576 struct vnode *a_vp;
577 int a_accmode;
578 kauth_cred_t a_cred;
579 } */ *ap = v;
580 struct vattr va;
581 int error;
582
583 DPRINTF(("VOP = hfs_vop_access()\n"));
584
585 error = hfs_check_possible(ap->a_vp, ap->a_accmode);
586 if (error)
587 return error;
588
589 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
590 return error;
591
592 error = hfs_check_permitted(ap->a_vp, &va, ap->a_accmode, ap->a_cred);
593
594 return error;
595 }
596
597 int
598 hfs_vop_getattr(void *v)
599 {
600 struct vop_getattr_args /* {
601 struct vnode *a_vp;
602 struct vattr *a_vap;
603 struct ucred *a_cred;
604 } */ *ap = v;
605 struct vnode *vp;
606 struct hfsnode *hp;
607 struct vattr *vap;
608 hfs_bsd_data_t *bsd;
609 hfs_fork_t *fork;
610
611 DPRINTF(("VOP = hfs_vop_getattr()\n"));
612
613 vp = ap->a_vp;
614 hp = VTOH(vp);
615 vap = ap->a_vap;
616
617 vattr_null(vap);
618
619 /*
620 * XXX Cannot trust permissions/modes/flags stored in an HFS+ catalog
621 * XXX record those values are not set on files created under Mac OS 9.
622 */
623 vap->va_type = ap->a_vp->v_type;
624 if (hp->h_rec.u.rec_type == HFS_REC_FILE) {
625 hfs_file_record_t *f = &hp->h_rec.file;
626 if (hp->h_fork == HFS_RSRCFORK)
627 fork = &f->rsrc_fork;
628 else
629 fork = &f->data_fork;
630 vap->va_fileid = f->cnid;
631 bsd = &f->bsd;
632 vap->va_bytes = fork->total_blocks * HFS_BLOCKSIZE(vp);
633 vap->va_size = fork->logical_size;
634 hfs_time_to_timespec(f->date_created, &vap->va_ctime);
635 hfs_time_to_timespec(f->date_content_mod, &vap->va_mtime);
636 hfs_time_to_timespec(f->date_accessed, &vap->va_atime);
637 vap->va_nlink = 1;
638 } else if (hp->h_rec.u.rec_type == HFS_REC_FLDR) {
639 hfs_folder_record_t *f = &hp->h_rec.folder;
640 vap->va_fileid = hp->h_rec.folder.cnid;
641 bsd = &f->bsd;
642 vap->va_size = 512; /* XXX Temporary */
643 vap->va_bytes = 512; /* XXX Temporary */
644 hfs_time_to_timespec(f->date_created, &vap->va_ctime);
645 hfs_time_to_timespec(f->date_content_mod,&vap->va_mtime);
646 hfs_time_to_timespec(f->date_accessed, &vap->va_atime);
647 vap->va_nlink = 2; /* XXX */
648 } else {
649 DPRINTF(("hfs+: hfs_vop_getattr(): invalid record type %i",
650 hp->h_rec.u.rec_type));
651 return EINVAL;
652 }
653
654 if ((bsd->file_mode & S_IFMT) == 0) {
655 /* no bsd permissions recorded, use default values */
656 if (hp->h_rec.u.rec_type == HFS_REC_FILE)
657 vap->va_mode = (S_IFREG | HFS_DEFAULT_FILE_MODE);
658 else
659 vap->va_mode = (S_IFDIR | HFS_DEFAULT_DIR_MODE);
660 vap->va_uid = HFS_DEFAULT_UID;
661 vap->va_gid = HFS_DEFAULT_GID;
662 } else {
663 vap->va_mode = bsd->file_mode;
664 vap->va_uid = bsd->owner_id;
665 vap->va_gid = bsd->group_id;
666 if ((vap->va_mode & S_IFMT) == S_IFCHR
667 || (vap->va_mode & S_IFMT) == S_IFBLK) {
668 vap->va_rdev
669 = HFS_CONVERT_RDEV(bsd->special.raw_device);
670 }
671 else if (bsd->special.link_count != 0) {
672 /* XXX: only if in metadata directory */
673 vap->va_nlink = bsd->special.link_count;
674 }
675 }
676
677 vap->va_fsid = hp->h_dev;
678 vap->va_blocksize = hp->h_hmp->hm_vol.vh.block_size;
679 vap->va_gen = 1;
680 vap->va_flags = 0;
681
682 return 0;
683 }
684
685 int
686 hfs_vop_setattr(void *v)
687 {
688 struct vop_setattr_args /* {
689 struct vnode *a_vp;
690 struct vattr *a_vap;
691 kauth_cred_t a_cred;
692 } */ *ap = v;
693 struct vattr *vap;
694 struct vnode *vp;
695
696 vap = ap->a_vap;
697 vp = ap->a_vp;
698
699 /*
700 * Check for unsettable attributes.
701 */
702 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
703 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
704 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
705 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
706 return EINVAL;
707 }
708
709 /* XXX: needs revisiting for write support */
710 if (vap->va_flags != VNOVAL
711 || vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL
712 || vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL
713 || vap->va_birthtime.tv_sec != VNOVAL) {
714 return EROFS;
715 }
716
717 if (vap->va_size != VNOVAL) {
718 /*
719 * Disallow write attempts on read-only file systems;
720 * unless the file is a socket, fifo, or a block or
721 * character device resident on the file system.
722 */
723 switch (vp->v_type) {
724 case VDIR:
725 return EISDIR;
726 case VCHR:
727 case VBLK:
728 case VFIFO:
729 break;
730 case VREG:
731 return EROFS;
732 default:
733 return EOPNOTSUPP;
734 }
735 }
736
737 return 0;
738 }
739
740 int
741 hfs_vop_bmap(void *v)
742 {
743 struct vop_bmap_args /* {
744 struct vnode *a_vp;
745 daddr_t a_bn;
746 struct vnode **a_vpp;
747 daddr_t *a_bnp;
748 int *a_runp;
749 } */ *ap = v;
750 struct vnode *vp;
751 struct hfsnode *hp;
752 daddr_t lblkno;
753 hfs_callback_args cbargs;
754 hfs_libcb_argsread argsread;
755 hfs_extent_descriptor_t *extents;
756 uint16_t numextents, i;
757 int bshift;
758
759 vp = ap->a_vp;
760 hp = VTOH(vp);
761 lblkno = ap->a_bn;
762 bshift = vp->v_mount->mnt_fs_bshift;
763
764 /*
765 * Check for underlying vnode requests and ensure that logical
766 * to physical mapping is requested.
767 */
768 if (ap->a_vpp != NULL)
769 *ap->a_vpp = hp->h_devvp;
770 if (ap->a_bnp == NULL)
771 return 0;
772
773 hfslib_init_cbargs(&cbargs);
774 argsread.cred = NULL;
775 argsread.l = NULL;
776 cbargs.read = &argsread;
777
778 numextents = hfslib_get_file_extents(&hp->h_hmp->hm_vol,
779 hp->h_rec.u.cnid, hp->h_fork, &extents, &cbargs);
780
781 /* XXX: is this correct for 0-length files? */
782 if (numextents == 0)
783 return EBADF;
784
785 for (i = 0; i < numextents; i++) {
786 if (lblkno < extents[i].block_count)
787 break;
788 lblkno -= extents[i].block_count;
789 }
790
791 if (i == numextents) {
792 /* XXX: block number past EOF */
793 i--;
794 lblkno += extents[i].block_count;
795 }
796
797 *ap->a_bnp = ((extents[i].start_block + lblkno) << (bshift-DEV_BSHIFT))
798 + (hp->h_hmp->hm_vol.offset >> DEV_BSHIFT);
799
800 if (ap->a_runp) {
801 int nblk;
802
803 nblk = extents[i].block_count - lblkno - 1;
804 if (nblk <= 0)
805 *ap->a_runp = 0;
806 else if (nblk > MAXBSIZE >> bshift)
807 *ap->a_runp = (MAXBSIZE >> bshift) - 1;
808 else
809 *ap->a_runp = nblk;
810 }
811
812 free(extents, M_TEMP);
813
814 return 0;
815 }
816
817 int
818 hfs_vop_read(void *v)
819 {
820 struct vop_read_args /* {
821 struct vnode *a_vp;
822 struct uio *a_uio;
823 int a_ioflag;
824 kauth_cred_t a_cred;
825 } */ *ap = v;
826 struct vnode *vp;
827 struct hfsnode *hp;
828 struct uio *uio;
829 uint64_t fsize; /* logical size of file */
830 int advice;
831 int error;
832
833 vp = ap->a_vp;
834 hp = VTOH(vp);
835 uio = ap->a_uio;
836 if (hp->h_fork == HFS_RSRCFORK)
837 fsize = hp->h_rec.file.rsrc_fork.logical_size;
838 else
839 fsize = hp->h_rec.file.data_fork.logical_size;
840 error = 0;
841 advice = IO_ADV_DECODE(ap->a_ioflag);
842
843 if (uio->uio_offset < 0)
844 return EINVAL;
845
846 if (uio->uio_resid == 0 || uio->uio_offset >= fsize)
847 return 0;
848
849 if (vp->v_type != VREG && vp->v_type != VLNK)
850 return EINVAL;
851
852 error = 0;
853 while (uio->uio_resid > 0 && error == 0) {
854 vsize_t len;
855
856 len = MIN(uio->uio_resid, fsize - uio->uio_offset);
857 if (len == 0)
858 break;
859
860 error = ubc_uiomove(&vp->v_uobj, uio, len, advice,
861 UBC_READ | UBC_PARTIALOK | UBC_VNODE_FLAGS(vp));
862 }
863
864 return error;
865 }
866
867 int
868 hfs_vop_readdir(void *v)
869 {
870 struct vop_readdir_args /* {
871 struct vnode *a_vp;
872 struct uio *a_uio;
873 kauth_cred_t a_cred;
874 int *a_eofflag;
875 off_t **a_cookies;
876 int a_*ncookies;
877 } */ *ap = v;
878
879 DPRINTF(("VOP = hfs_vop_readdir()\n"));
880
881 struct dirent curent; /* the dirent entry we are constructing */
882 struct hfsnode *hp;
883 hfs_catalog_keyed_record_t *children;
884 hfs_unistr255_t *childnames;
885 hfs_callback_args cbargs;
886 hfs_libcb_argsread argsread;
887 struct uio *uio;
888 off_t bufoff; /* offset in buffer relative to start of dirents */
889 uint32_t numchildren;
890 uint32_t curchild; /* index of child we are stuffing into dirent */
891 size_t namlen, ni;
892 int error;
893 int i; /* dummy variable */
894
895 bufoff = 0;
896 children = NULL;
897 error = 0;
898 numchildren = 0;
899 hp = VTOH(ap->a_vp);
900 uio = ap->a_uio;
901
902 if (uio->uio_offset < 0)
903 return EINVAL;
904 if (ap->a_eofflag != NULL)
905 *ap->a_eofflag = 0;
906
907 /* XXX Inform that we don't support NFS, for now. */
908 #if 0
909 if(ap->a_eofflag != NULL || ap->a_cookies != NULL ||
910 ap->a_ncookies != NULL)
911 return EOPNOTSUPP;
912 #endif
913 DPRINTF(("READDIR uio: offset=%td, resid=%zu\n",
914 uio->uio_offset, uio->uio_resid));
915 hfslib_init_cbargs(&cbargs);
916 argsread.cred = ap->a_cred;
917 argsread.l = NULL;
918 cbargs.read = &argsread;
919
920 /* XXX Should we cache this? */
921 if (hfslib_get_directory_contents(&hp->h_hmp->hm_vol, hp->h_rec.u.cnid,
922 &children, &childnames, &numchildren, &cbargs) != 0) {
923 DPRINTF(("ENOENT\n"));
924 error = ENOENT;
925 goto error;
926 }
927
928 DPRINTF(("numchildren = %u\n", numchildren));
929 for (curchild = 0; curchild < numchildren && uio->uio_resid > 0;
930 curchild++) {
931 namlen = utf16_to_utf8(curent.d_name, NAME_MAX,
932 childnames[curchild].unicode, childnames[curchild].length,
933 0, NULL);
934 /* XXX: check conversion errors? */
935 if (namlen > NAME_MAX) {
936 /* XXX: how to handle name too long? */
937 continue;
938 }
939 for (ni = 0; ni < namlen; ni++)
940 if (curent.d_name[ni] == '/')
941 curent.d_name[ni] = ':';
942 curent.d_namlen = namlen;
943 curent.d_reclen = _DIRENT_SIZE(&curent);
944
945 /* Skip to desired dirent. */
946 bufoff += curent.d_reclen;
947 if (bufoff - curent.d_reclen < uio->uio_offset)
948 continue;
949
950 /* Make sure we don't return partial entries. */
951 if (uio->uio_resid < curent.d_reclen) {
952 DPRINTF(("PARTIAL ENTRY\n"));
953 if (ap->a_eofflag != NULL)
954 *ap->a_eofflag = 1;
955 break;
956 }
957
958 curent.d_fileno = children[curchild].file.cnid;
959 switch (hfs_catalog_keyed_record_vtype(children+curchild)) {
960 case VREG:
961 curent.d_type = DT_REG;
962 break;
963 case VDIR:
964 curent.d_type = DT_DIR;
965 break;
966 case VBLK:
967 curent.d_type = DT_BLK;
968 break;
969 case VCHR:
970 curent.d_type = DT_CHR;
971 break;
972 case VLNK:
973 curent.d_type = DT_LNK;
974 break;
975 case VSOCK:
976 curent.d_type = DT_SOCK;
977 break;
978 case VFIFO:
979 curent.d_type = DT_FIFO;
980 break;
981 default:
982 curent.d_type = DT_UNKNOWN;
983 break;
984 }
985 DPRINTF(("curchildname = %s\t\t", curchildname));
986 /* pad curent.d_name to aligned byte boundary */
987 for (i = curent.d_namlen;
988 i < curent.d_reclen - _DIRENT_NAMEOFF(&curent); i++)
989 curent.d_name[i] = 0;
990
991 DPRINTF(("curent.d_name = %s\n", curent.d_name));
992
993 if ((error = uiomove(&curent, curent.d_reclen, uio)) != 0)
994 goto error;
995 }
996
997 /* FALLTHROUGH */
998
999 error:
1000 if (numchildren > 0) {
1001 if (children != NULL)
1002 free(children, M_TEMP);
1003 if (childnames != NULL)
1004 free(childnames, M_TEMP);
1005 }
1006
1007 if (error) {
1008 DPRINTF(("ERROR = %i\n", error));
1009 }
1010
1011 return error;
1012 }
1013
1014 int
1015 hfs_vop_readlink(void *v) {
1016 struct vop_readlink_args /* {
1017 struct vnode *a_vp;
1018 struct uio *a_uio;
1019 kauth_cred_t a_cred;
1020 } */ *ap = v;
1021
1022 return VOP_READ(ap->a_vp, ap->a_uio, 0, ap->a_cred);
1023 }
1024
1025 int
1026 hfs_vop_reclaim(void *v)
1027 {
1028 struct vop_reclaim_v2_args /* {
1029 struct vnode *a_vp;
1030 } */ *ap = v;
1031 struct vnode *vp;
1032 struct hfsnode *hp;
1033
1034 VOP_UNLOCK(ap->a_vp);
1035
1036 DPRINTF(("VOP = hfs_vop_reclaim()\n"));
1037
1038 vp = ap->a_vp;
1039 hp = VTOH(vp);
1040
1041 /* Decrement the reference count to the volume's device. */
1042 if (hp->h_devvp) {
1043 vrele(hp->h_devvp);
1044 hp->h_devvp = 0;
1045 }
1046
1047 genfs_node_destroy(vp);
1048 pool_put(&hfs_node_pool, hp);
1049 vp->v_data = NULL;
1050
1051 return 0;
1052 }
1053
1054 int
1055 hfs_vop_print(void *v)
1056 {
1057 struct vop_print_args /* {
1058 struct vnode *a_vp;
1059 } */ *ap = v;
1060 struct vnode *vp;
1061 struct hfsnode *hp;
1062
1063 DPRINTF(("VOP = hfs_vop_print()\n"));
1064
1065 vp = ap->a_vp;
1066 hp = VTOH(vp);
1067
1068 printf("dummy = %X\n", (unsigned)hp->dummy);
1069 printf("\n");
1070
1071 return 0;
1072 }
1073