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