xref: /freebsd/sys/fs/udf/udf_vnops.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /* udf_vnops.c */
30 /* Take care of the vnode side of things */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/stat.h>
38 #include <sys/bio.h>
39 #include <sys/conf.h>
40 #include <sys/buf.h>
41 #include <sys/iconv.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/dirent.h>
45 #include <sys/queue.h>
46 #include <sys/unistd.h>
47 #include <sys/endian.h>
48 
49 #include <vm/uma.h>
50 
51 #include <fs/udf/ecma167-udf.h>
52 #include <fs/udf/osta.h>
53 #include <fs/udf/udf.h>
54 #include <fs/udf/udf_mount.h>
55 
56 extern struct iconv_functions *udf_iconv;
57 
58 static vop_access_t	udf_access;
59 static vop_getattr_t	udf_getattr;
60 static vop_open_t	udf_open;
61 static vop_ioctl_t	udf_ioctl;
62 static vop_pathconf_t	udf_pathconf;
63 static vop_print_t	udf_print;
64 static vop_read_t	udf_read;
65 static vop_readdir_t	udf_readdir;
66 static vop_readlink_t	udf_readlink;
67 static vop_setattr_t	udf_setattr;
68 static vop_strategy_t	udf_strategy;
69 static vop_bmap_t	udf_bmap;
70 static vop_cachedlookup_t	udf_lookup;
71 static vop_reclaim_t	udf_reclaim;
72 static vop_vptofh_t	udf_vptofh;
73 static int udf_readatoffset(struct udf_node *node, int *size, off_t offset,
74     struct buf **bp, uint8_t **data);
75 static int udf_bmap_internal(struct udf_node *node, off_t offset,
76     daddr_t *sector, uint32_t *max_size);
77 
78 static struct vop_vector udf_vnodeops = {
79 	.vop_default =		&default_vnodeops,
80 
81 	.vop_access =		udf_access,
82 	.vop_bmap =		udf_bmap,
83 	.vop_cachedlookup =	udf_lookup,
84 	.vop_getattr =		udf_getattr,
85 	.vop_ioctl =		udf_ioctl,
86 	.vop_lookup =		vfs_cache_lookup,
87 	.vop_open =		udf_open,
88 	.vop_pathconf =		udf_pathconf,
89 	.vop_print =		udf_print,
90 	.vop_read =		udf_read,
91 	.vop_readdir =		udf_readdir,
92 	.vop_readlink =		udf_readlink,
93 	.vop_reclaim =		udf_reclaim,
94 	.vop_setattr =		udf_setattr,
95 	.vop_strategy =		udf_strategy,
96 	.vop_vptofh =		udf_vptofh,
97 };
98 VFS_VOP_VECTOR_REGISTER(udf_vnodeops);
99 
100 struct vop_vector udf_fifoops = {
101 	.vop_default =		&fifo_specops,
102 	.vop_access =		udf_access,
103 	.vop_getattr =		udf_getattr,
104 	.vop_pathconf =		udf_pathconf,
105 	.vop_print =		udf_print,
106 	.vop_reclaim =		udf_reclaim,
107 	.vop_setattr =		udf_setattr,
108 	.vop_vptofh =		udf_vptofh,
109 };
110 VFS_VOP_VECTOR_REGISTER(udf_fifoops);
111 
112 static MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure");
113 static MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure");
114 
115 #define UDF_INVALID_BMAP	-1
116 
117 int
118 udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td)
119 {
120 	int error;
121 	struct vnode *vp;
122 
123 	error = getnewvnode("udf", mp, &udf_vnodeops, &vp);
124 	if (error) {
125 		printf("udf_allocv: failed to allocate new vnode\n");
126 		return (error);
127 	}
128 
129 	*vpp = vp;
130 	return (0);
131 }
132 
133 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
134 static mode_t
135 udf_permtomode(struct udf_node *node)
136 {
137 	uint32_t perm;
138 	uint16_t flags;
139 	mode_t mode;
140 
141 	perm = le32toh(node->fentry->perm);
142 	flags = le16toh(node->fentry->icbtag.flags);
143 
144 	mode = perm & UDF_FENTRY_PERM_USER_MASK;
145 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
146 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
147 	mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
148 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
149 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
150 
151 	return (mode);
152 }
153 
154 static int
155 udf_access(struct vop_access_args *a)
156 {
157 	struct vnode *vp;
158 	struct udf_node *node;
159 	accmode_t accmode;
160 	mode_t mode;
161 
162 	vp = a->a_vp;
163 	node = VTON(vp);
164 	accmode = a->a_accmode;
165 
166 	if (accmode & VWRITE) {
167 		switch (vp->v_type) {
168 		case VDIR:
169 		case VLNK:
170 		case VREG:
171 			return (EROFS);
172 			/* NOT REACHED */
173 		default:
174 			break;
175 		}
176 	}
177 
178 	mode = udf_permtomode(node);
179 
180 	return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid,
181 	    accmode, a->a_cred));
182 }
183 
184 static int
185 udf_open(struct vop_open_args *ap) {
186 	struct udf_node *np = VTON(ap->a_vp);
187 	off_t fsize;
188 
189 	fsize = le64toh(np->fentry->inf_len);
190 	vnode_create_vobject(ap->a_vp, fsize, ap->a_td);
191 	return 0;
192 }
193 
194 static const int mon_lens[2][12] = {
195 	{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
196 	{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
197 };
198 
199 static int
200 udf_isaleapyear(int year)
201 {
202 	int i;
203 
204 	i = (year % 4) ? 0 : 1;
205 	i &= (year % 100) ? 1 : 0;
206 	i |= (year % 400) ? 0 : 1;
207 
208 	return i;
209 }
210 
211 /*
212  * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
213  */
214 static void
215 udf_timetotimespec(struct timestamp *time, struct timespec *t)
216 {
217 	int i, lpyear, daysinyear, year, startyear;
218 	union {
219 		uint16_t	u_tz_offset;
220 		int16_t		s_tz_offset;
221 	} tz;
222 
223 	/*
224 	 * DirectCD seems to like using bogus year values.
225 	 * Don't trust time->month as it will be used for an array index.
226 	 */
227 	year = le16toh(time->year);
228 	if (year < 1970 || time->month < 1 || time->month > 12) {
229 		t->tv_sec = 0;
230 		t->tv_nsec = 0;
231 		return;
232 	}
233 
234 	/* Calculate the time and day */
235 	t->tv_sec = time->second;
236 	t->tv_sec += time->minute * 60;
237 	t->tv_sec += time->hour * 3600;
238 	t->tv_sec += (time->day - 1) * 3600 * 24;
239 
240 	/* Calculate the month */
241 	lpyear = udf_isaleapyear(year);
242 	t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24;
243 
244 	/* Speed up the calculation */
245 	startyear = 1970;
246 	if (year > 2009) {
247 		t->tv_sec += 1262304000;
248 		startyear += 40;
249 	} else if (year > 1999) {
250 		t->tv_sec += 946684800;
251 		startyear += 30;
252 	} else if (year > 1989) {
253 		t->tv_sec += 631152000;
254 		startyear += 20;
255 	} else if (year > 1979) {
256 		t->tv_sec += 315532800;
257 		startyear += 10;
258 	}
259 
260 	daysinyear = (year - startyear) * 365;
261 	for (i = startyear; i < year; i++)
262 		daysinyear += udf_isaleapyear(i);
263 	t->tv_sec += daysinyear * 3600 * 24;
264 
265 	/* Calculate microseconds */
266 	t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 +
267 	    time->usec;
268 
269 	/*
270 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
271 	 * complement, so we gotta do some extra magic to handle it right.
272 	 */
273 	tz.u_tz_offset = le16toh(time->type_tz);
274 	tz.u_tz_offset &= 0x0fff;
275 	if (tz.u_tz_offset & 0x0800)
276 		tz.u_tz_offset |= 0xf000;	/* extend the sign to 16 bits */
277 	if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047))
278 		t->tv_sec -= tz.s_tz_offset * 60;
279 
280 	return;
281 }
282 
283 static int
284 udf_getattr(struct vop_getattr_args *a)
285 {
286 	struct vnode *vp;
287 	struct udf_node *node;
288 	struct vattr *vap;
289 	struct file_entry *fentry;
290 
291 	vp = a->a_vp;
292 	vap = a->a_vap;
293 	node = VTON(vp);
294 	fentry = node->fentry;
295 
296 	vap->va_fsid = dev2udev(node->udfmp->im_dev);
297 	vap->va_fileid = node->hash_id;
298 	vap->va_mode = udf_permtomode(node);
299 	vap->va_nlink = le16toh(fentry->link_cnt);
300 	/*
301 	 * XXX The spec says that -1 is valid for uid/gid and indicates an
302 	 * invalid uid/gid.  How should this be represented?
303 	 */
304 	vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid);
305 	vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid);
306 	udf_timetotimespec(&fentry->atime, &vap->va_atime);
307 	udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
308 	vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
309 	vap->va_rdev = NODEV;
310 	if (vp->v_type & VDIR) {
311 		/*
312 		 * Directories that are recorded within their ICB will show
313 		 * as having 0 blocks recorded.  Since tradition dictates
314 		 * that directories consume at least one logical block,
315 		 * make it appear so.
316 		 */
317 		if (fentry->logblks_rec != 0) {
318 			vap->va_size =
319 			    le64toh(fentry->logblks_rec) * node->udfmp->bsize;
320 		} else {
321 			vap->va_size = node->udfmp->bsize;
322 		}
323 	} else {
324 		vap->va_size = le64toh(fentry->inf_len);
325 	}
326 	vap->va_flags = 0;
327 	vap->va_gen = 1;
328 	vap->va_blocksize = node->udfmp->bsize;
329 	vap->va_bytes = le64toh(fentry->inf_len);
330 	vap->va_type = vp->v_type;
331 	vap->va_filerev = 0; /* XXX */
332 	return (0);
333 }
334 
335 static int
336 udf_setattr(struct vop_setattr_args *a)
337 {
338 	struct vnode *vp;
339 	struct vattr *vap;
340 
341 	vp = a->a_vp;
342 	vap = a->a_vap;
343 	if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
344 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
345 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
346 		return (EROFS);
347 	if (vap->va_size != (u_quad_t)VNOVAL) {
348 		switch (vp->v_type) {
349 		case VDIR:
350 			return (EISDIR);
351 		case VLNK:
352 		case VREG:
353 			return (EROFS);
354 		case VCHR:
355 		case VBLK:
356 		case VSOCK:
357 		case VFIFO:
358 		case VNON:
359 		case VBAD:
360 		case VMARKER:
361 			return (0);
362 		}
363 	}
364 	return (0);
365 }
366 
367 /*
368  * File specific ioctls.
369  */
370 static int
371 udf_ioctl(struct vop_ioctl_args *a)
372 {
373 	printf("%s called\n", __func__);
374 	return (ENOTTY);
375 }
376 
377 /*
378  * I'm not sure that this has much value in a read-only filesystem, but
379  * cd9660 has it too.
380  */
381 static int
382 udf_pathconf(struct vop_pathconf_args *a)
383 {
384 
385 	switch (a->a_name) {
386 	case _PC_FILESIZEBITS:
387 		*a->a_retval = 64;
388 		return (0);
389 	case _PC_LINK_MAX:
390 		*a->a_retval = 65535;
391 		return (0);
392 	case _PC_NAME_MAX:
393 		*a->a_retval = NAME_MAX;
394 		return (0);
395 	case _PC_SYMLINK_MAX:
396 		*a->a_retval = MAXPATHLEN;
397 		return (0);
398 	case _PC_NO_TRUNC:
399 		*a->a_retval = 1;
400 		return (0);
401 	case _PC_PIPE_BUF:
402 		if (a->a_vp->v_type == VDIR || a->a_vp->v_type == VFIFO) {
403 			*a->a_retval = PIPE_BUF;
404 			return (0);
405 		}
406 		return (EINVAL);
407 	default:
408 		return (vop_stdpathconf(a));
409 	}
410 }
411 
412 static int
413 udf_print(struct vop_print_args *ap)
414 {
415 	struct vnode *vp = ap->a_vp;
416 	struct udf_node *node = VTON(vp);
417 
418 	printf("    ino %lu, on dev %s", (u_long)node->hash_id,
419 	    devtoname(node->udfmp->im_dev));
420 	if (vp->v_type == VFIFO)
421 		fifo_printinfo(vp);
422 	printf("\n");
423 	return (0);
424 }
425 
426 #define lblkno(udfmp, loc)	((loc) >> (udfmp)->bshift)
427 #define blkoff(udfmp, loc)	((loc) & (udfmp)->bmask)
428 #define lblktosize(udfmp, blk)	((blk) << (udfmp)->bshift)
429 
430 static inline int
431 is_data_in_fentry(const struct udf_node *node)
432 {
433 	const struct file_entry *fentry = node->fentry;
434 
435 	return ((le16toh(fentry->icbtag.flags) & 0x7) == 3);
436 }
437 
438 static int
439 udf_read(struct vop_read_args *ap)
440 {
441 	struct vnode *vp = ap->a_vp;
442 	struct uio *uio = ap->a_uio;
443 	struct udf_node *node = VTON(vp);
444 	struct udf_mnt *udfmp;
445 	struct file_entry *fentry;
446 	struct buf *bp;
447 	uint8_t *data;
448 	daddr_t lbn, rablock;
449 	off_t diff, fsize;
450 	ssize_t n;
451 	int error = 0;
452 	long size, on;
453 
454 	if (uio->uio_resid == 0)
455 		return (0);
456 	if (uio->uio_offset < 0)
457 		return (EINVAL);
458 
459 	if (is_data_in_fentry(node)) {
460 		fentry = node->fentry;
461 		data = &fentry->data[le32toh(fentry->l_ea)];
462 		fsize = le32toh(fentry->l_ad);
463 
464 		n = uio->uio_resid;
465 		diff = fsize - uio->uio_offset;
466 		if (diff <= 0)
467 			return (0);
468 		if (diff < n)
469 			n = diff;
470 		error = uiomove(data + uio->uio_offset, (int)n, uio);
471 		return (error);
472 	}
473 
474 	fsize = le64toh(node->fentry->inf_len);
475 	udfmp = node->udfmp;
476 	do {
477 		lbn = lblkno(udfmp, uio->uio_offset);
478 		on = blkoff(udfmp, uio->uio_offset);
479 		n = min((u_int)(udfmp->bsize - on),
480 			uio->uio_resid);
481 		diff = fsize - uio->uio_offset;
482 		if (diff <= 0)
483 			return (0);
484 		if (diff < n)
485 			n = diff;
486 		size = udfmp->bsize;
487 		rablock = lbn + 1;
488 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
489 			if (lblktosize(udfmp, rablock) < fsize) {
490 				error = cluster_read(vp, fsize, lbn, size,
491 				    NOCRED, uio->uio_resid,
492 				    (ap->a_ioflag >> 16), 0, &bp);
493 			} else {
494 				error = bread(vp, lbn, size, NOCRED, &bp);
495 			}
496 		} else {
497 			error = bread(vp, lbn, size, NOCRED, &bp);
498 		}
499 		if (error != 0) {
500 			brelse(bp);
501 			return (error);
502 		}
503 		n = min(n, size - bp->b_resid);
504 
505 		error = uiomove(bp->b_data + on, (int)n, uio);
506 		brelse(bp);
507 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
508 	return (error);
509 }
510 
511 /*
512  * Call the OSTA routines to translate the name from a CS0 dstring to a
513  * 16-bit Unicode String.  Hooks need to be placed in here to translate from
514  * Unicode to the encoding that the kernel/user expects.  Return the length
515  * of the translated string.
516  */
517 static int
518 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
519 {
520 	unicode_t *transname;
521 	char *unibuf, *unip;
522 	int i, destlen;
523 	ssize_t unilen = 0;
524 	size_t destleft = MAXNAMLEN;
525 
526 	/* Convert 16-bit Unicode to destname */
527 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
528 		/* allocate a buffer big enough to hold an 8->16 bit expansion */
529 		unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
530 		unip = unibuf;
531 		if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
532 			printf("udf: Unicode translation failed\n");
533 			uma_zfree(udf_zone_trans, unibuf);
534 			return 0;
535 		}
536 
537 		while (unilen > 0 && destleft > 0) {
538 			udf_iconv->conv(udfmp->im_d2l, __DECONST(const char **,
539 			    &unibuf), (size_t *)&unilen, (char **)&destname,
540 			    &destleft);
541 			/* Unconverted character found */
542 			if (unilen > 0 && destleft > 0) {
543 				*destname++ = '?';
544 				destleft--;
545 				unibuf += 2;
546 				unilen -= 2;
547 			}
548 		}
549 		uma_zfree(udf_zone_trans, unip);
550 		*destname = '\0';
551 		destlen = MAXNAMLEN - (int)destleft;
552 	} else {
553 		/* allocate a buffer big enough to hold an 8->16 bit expansion */
554 		transname = uma_zalloc(udf_zone_trans, M_WAITOK);
555 
556 		if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) {
557 			printf("udf: Unicode translation failed\n");
558 			uma_zfree(udf_zone_trans, transname);
559 			return 0;
560 		}
561 
562 		for (i = 0; i < unilen ; i++) {
563 			if (transname[i] & 0xff00) {
564 				destname[i] = '.';	/* Fudge the 16bit chars */
565 			} else {
566 				destname[i] = transname[i] & 0xff;
567 			}
568 		}
569 		uma_zfree(udf_zone_trans, transname);
570 		destname[unilen] = 0;
571 		destlen = (int)unilen;
572 	}
573 
574 	return (destlen);
575 }
576 
577 /*
578  * Compare a CS0 dstring with a name passed in from the VFS layer.  Return
579  * 0 on a successful match, nonzero otherwise.  Unicode work may need to be done
580  * here also.
581  */
582 static int
583 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
584 {
585 	char *transname;
586 	int error = 0;
587 
588 	/* This is overkill, but not worth creating a new zone */
589 	transname = uma_zalloc(udf_zone_trans, M_WAITOK);
590 
591 	cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
592 
593 	/* Easy check.  If they aren't the same length, they aren't equal */
594 	if ((cs0len == 0) || (cs0len != cmplen))
595 		error = -1;
596 	else
597 		error = bcmp(transname, cmpname, cmplen);
598 
599 	uma_zfree(udf_zone_trans, transname);
600 	return (error);
601 }
602 
603 struct udf_uiodir {
604 	struct dirent *dirent;
605 	uint64_t *cookies;
606 	int ncookies;
607 	int acookies;
608 	int eofflag;
609 };
610 
611 static int
612 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
613 {
614 	if (uiodir->cookies != NULL) {
615 		if (++uiodir->acookies > uiodir->ncookies) {
616 			uiodir->eofflag = 0;
617 			return (-1);
618 		}
619 		*uiodir->cookies++ = cookie;
620 	}
621 
622 	if (uio->uio_resid < de_size) {
623 		uiodir->eofflag = 0;
624 		return (-1);
625 	}
626 
627 	return (uiomove(uiodir->dirent, de_size, uio));
628 }
629 
630 static struct udf_dirstream *
631 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
632 {
633 	struct udf_dirstream *ds;
634 
635 	ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
636 
637 	ds->node = node;
638 	ds->offset = offset;
639 	ds->udfmp = udfmp;
640 	ds->fsize = fsize;
641 
642 	return (ds);
643 }
644 
645 static struct fileid_desc *
646 udf_getfid(struct udf_dirstream *ds)
647 {
648 	struct fileid_desc *fid;
649 	int error, frag_size = 0, total_fid_size;
650 
651 	/* End of directory? */
652 	if (ds->offset + ds->off >= ds->fsize) {
653 		ds->error = 0;
654 		return (NULL);
655 	}
656 
657 	/* Grab the first extent of the directory */
658 	if (ds->off == 0) {
659 		ds->size = 0;
660 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
661 		    &ds->bp, &ds->data);
662 		if (error) {
663 			ds->error = error;
664 			if (ds->bp != NULL)
665 				brelse(ds->bp);
666 			return (NULL);
667 		}
668 	}
669 
670 	/*
671 	 * Clean up from a previous fragmented FID.
672 	 * XXX Is this the right place for this?
673 	 */
674 	if (ds->fid_fragment && ds->buf != NULL) {
675 		ds->fid_fragment = 0;
676 		free(ds->buf, M_UDFFID);
677 	}
678 
679 	fid = (struct fileid_desc*)&ds->data[ds->off];
680 
681 	/*
682 	 * Check to see if the fid is fragmented. The first test
683 	 * ensures that we don't wander off the end of the buffer
684 	 * looking for the l_iu and l_fi fields.
685 	 */
686 	if (ds->off + UDF_FID_SIZE > ds->size ||
687 	    ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){
688 		/* Copy what we have of the fid into a buffer */
689 		frag_size = ds->size - ds->off;
690 		if (frag_size >= ds->udfmp->bsize) {
691 			printf("udf: invalid FID fragment\n");
692 			ds->error = EINVAL;
693 			return (NULL);
694 		}
695 
696 		/*
697 		 * File ID descriptors can only be at most one
698 		 * logical sector in size.
699 		 */
700 		ds->buf = malloc(ds->udfmp->bsize, M_UDFFID,
701 		     M_WAITOK | M_ZERO);
702 		bcopy(fid, ds->buf, frag_size);
703 
704 		/* Reduce all of the casting magic */
705 		fid = (struct fileid_desc*)ds->buf;
706 
707 		if (ds->bp != NULL)
708 			brelse(ds->bp);
709 
710 		/* Fetch the next allocation */
711 		ds->offset += ds->size;
712 		ds->size = 0;
713 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
714 		    &ds->bp, &ds->data);
715 		if (error) {
716 			ds->error = error;
717 			return (NULL);
718 		}
719 
720 		/*
721 		 * If the fragment was so small that we didn't get
722 		 * the l_iu and l_fi fields, copy those in.
723 		 */
724 		if (frag_size < UDF_FID_SIZE)
725 			bcopy(ds->data, &ds->buf[frag_size],
726 			    UDF_FID_SIZE - frag_size);
727 
728 		/*
729 		 * Now that we have enough of the fid to work with,
730 		 * copy in the rest of the fid from the new
731 		 * allocation.
732 		 */
733 		total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi;
734 		if (total_fid_size > ds->udfmp->bsize) {
735 			printf("udf: invalid FID\n");
736 			ds->error = EIO;
737 			return (NULL);
738 		}
739 		bcopy(ds->data, &ds->buf[frag_size],
740 		    total_fid_size - frag_size);
741 
742 		ds->fid_fragment = 1;
743 	} else {
744 		total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE;
745 	}
746 
747 	/*
748 	 * Update the offset. Align on a 4 byte boundary because the
749 	 * UDF spec says so.
750 	 */
751 	ds->this_off = ds->offset + ds->off;
752 	if (!ds->fid_fragment) {
753 		ds->off += (total_fid_size + 3) & ~0x03;
754 	} else {
755 		ds->off = (total_fid_size - frag_size + 3) & ~0x03;
756 	}
757 
758 	return (fid);
759 }
760 
761 static void
762 udf_closedir(struct udf_dirstream *ds)
763 {
764 
765 	if (ds->bp != NULL)
766 		brelse(ds->bp);
767 
768 	if (ds->fid_fragment && ds->buf != NULL)
769 		free(ds->buf, M_UDFFID);
770 
771 	uma_zfree(udf_zone_ds, ds);
772 }
773 
774 static int
775 udf_readdir(struct vop_readdir_args *a)
776 {
777 	struct vnode *vp;
778 	struct uio *uio;
779 	struct dirent dir;
780 	struct udf_node *node;
781 	struct udf_mnt *udfmp;
782 	struct fileid_desc *fid;
783 	struct udf_uiodir uiodir;
784 	struct udf_dirstream *ds;
785 	uint64_t *cookies = NULL;
786 	int ncookies;
787 	int error = 0;
788 
789 	vp = a->a_vp;
790 	uio = a->a_uio;
791 	node = VTON(vp);
792 	udfmp = node->udfmp;
793 	uiodir.eofflag = 1;
794 
795 	if (a->a_ncookies != NULL) {
796 		/*
797 		 * Guess how many entries are needed.  If we run out, this
798 		 * function will be called again and thing will pick up were
799 		 * it left off.
800 		 */
801 		ncookies = uio->uio_resid / 8;
802 		cookies = malloc(sizeof(*cookies) * ncookies, M_TEMP, M_WAITOK);
803 		uiodir.ncookies = ncookies;
804 		uiodir.cookies = cookies;
805 		uiodir.acookies = 0;
806 	} else {
807 		uiodir.cookies = NULL;
808 	}
809 
810 	/*
811 	 * Iterate through the file id descriptors.  Give the parent dir
812 	 * entry special attention.
813 	 */
814 	ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
815 	    node->udfmp);
816 
817 	while ((fid = udf_getfid(ds)) != NULL) {
818 		/* XXX Should we return an error on a bad fid? */
819 		if (udf_checktag(&fid->tag, TAGID_FID)) {
820 			printf("Invalid FID tag\n");
821 			hexdump(fid, UDF_FID_SIZE, NULL, 0);
822 			error = EIO;
823 			break;
824 		}
825 
826 		/* Is this a deleted file? */
827 		if (fid->file_char & UDF_FILE_CHAR_DEL)
828 			continue;
829 
830 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
831 			/* Do up the '.' and '..' entries.  Dummy values are
832 			 * used for the cookies since the offset here is
833 			 * usually zero, and NFS doesn't like that value
834 			 */
835 			dir.d_fileno = node->hash_id;
836 			dir.d_type = DT_DIR;
837 			dir.d_name[0] = '.';
838 			dir.d_namlen = 1;
839 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
840 			dir.d_off = 1;
841 			dirent_terminate(&dir);
842 			uiodir.dirent = &dir;
843 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
844 			if (error)
845 				break;
846 
847 			dir.d_fileno = udf_getid(&fid->icb);
848 			dir.d_type = DT_DIR;
849 			dir.d_name[0] = '.';
850 			dir.d_name[1] = '.';
851 			dir.d_namlen = 2;
852 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
853 			dir.d_off = 2;
854 			dirent_terminate(&dir);
855 			uiodir.dirent = &dir;
856 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
857 		} else {
858 			dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
859 			    &dir.d_name[0], fid->l_fi, udfmp);
860 			dir.d_fileno = udf_getid(&fid->icb);
861 			dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
862 			    DT_DIR : DT_UNKNOWN;
863 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
864 			dir.d_off = ds->this_off;
865 			dirent_terminate(&dir);
866 			uiodir.dirent = &dir;
867 			error = udf_uiodir(&uiodir, dir.d_reclen, uio,
868 			    ds->this_off);
869 		}
870 		if (error)
871 			break;
872 		uio->uio_offset = ds->offset + ds->off;
873 	}
874 
875 	/* tell the calling layer whether we need to be called again */
876 	*a->a_eofflag = uiodir.eofflag;
877 
878 	if (error < 0)
879 		error = 0;
880 	if (!error)
881 		error = ds->error;
882 
883 	udf_closedir(ds);
884 
885 	if (a->a_ncookies != NULL) {
886 		if (error)
887 			free(cookies, M_TEMP);
888 		else {
889 			*a->a_ncookies = uiodir.acookies;
890 			*a->a_cookies = cookies;
891 		}
892 	}
893 
894 	return (error);
895 }
896 
897 static int
898 udf_readlink(struct vop_readlink_args *ap)
899 {
900 	struct path_component *pc, *end;
901 	struct vnode *vp;
902 	struct uio uio;
903 	struct iovec iov[1];
904 	struct udf_node *node;
905 	void *buf;
906 	char *cp;
907 	int error, len, root;
908 
909 	/*
910 	 * A symbolic link in UDF is a list of variable-length path
911 	 * component structures.  We build a pathname in the caller's
912 	 * uio by traversing this list.
913 	 */
914 	vp = ap->a_vp;
915 	node = VTON(vp);
916 	len = le64toh(node->fentry->inf_len);
917 	buf = malloc(len, M_DEVBUF, M_WAITOK);
918 	iov[0].iov_len = len;
919 	iov[0].iov_base = buf;
920 	uio.uio_iov = iov;
921 	uio.uio_iovcnt = 1;
922 	uio.uio_offset = 0;
923 	uio.uio_resid = iov[0].iov_len;
924 	uio.uio_segflg = UIO_SYSSPACE;
925 	uio.uio_rw = UIO_READ;
926 	uio.uio_td = curthread;
927 	error = VOP_READ(vp, &uio, 0, ap->a_cred);
928 	if (error)
929 		goto error;
930 
931 	pc = buf;
932 	end = (void *)((char *)buf + len);
933 	root = 0;
934 	while (pc < end) {
935 		switch (pc->type) {
936 		case UDF_PATH_ROOT:
937 			/* Only allow this at the beginning of a path. */
938 			if ((void *)pc != buf) {
939 				error = EINVAL;
940 				goto error;
941 			}
942 			cp = "/";
943 			len = 1;
944 			root = 1;
945 			break;
946 		case UDF_PATH_DOT:
947 			cp = ".";
948 			len = 1;
949 			break;
950 		case UDF_PATH_DOTDOT:
951 			cp = "..";
952 			len = 2;
953 			break;
954 		case UDF_PATH_PATH:
955 			if (pc->length == 0) {
956 				error = EINVAL;
957 				goto error;
958 			}
959 			/*
960 			 * XXX: We only support CS8 which appears to map
961 			 * to ASCII directly.
962 			 */
963 			switch (pc->identifier[0]) {
964 			case 8:
965 				cp = pc->identifier + 1;
966 				len = pc->length - 1;
967 				break;
968 			default:
969 				error = EOPNOTSUPP;
970 				goto error;
971 			}
972 			break;
973 		default:
974 			error = EINVAL;
975 			goto error;
976 		}
977 
978 		/*
979 		 * If this is not the first component, insert a path
980 		 * separator.
981 		 */
982 		if (pc != buf) {
983 			/* If we started with root we already have a "/". */
984 			if (root)
985 				goto skipslash;
986 			root = 0;
987 			if (ap->a_uio->uio_resid < 1) {
988 				error = ENAMETOOLONG;
989 				goto error;
990 			}
991 			error = uiomove("/", 1, ap->a_uio);
992 			if (error)
993 				break;
994 		}
995 	skipslash:
996 
997 		/* Append string at 'cp' of length 'len' to our path. */
998 		if (len > ap->a_uio->uio_resid) {
999 			error = ENAMETOOLONG;
1000 			goto error;
1001 		}
1002 		error = uiomove(cp, len, ap->a_uio);
1003 		if (error)
1004 			break;
1005 
1006 		/* Advance to next component. */
1007 		pc = (void *)((char *)pc + 4 + pc->length);
1008 	}
1009 error:
1010 	free(buf, M_DEVBUF);
1011 	return (error);
1012 }
1013 
1014 static int
1015 udf_strategy(struct vop_strategy_args *a)
1016 {
1017 	struct buf *bp;
1018 	struct vnode *vp;
1019 	struct udf_node *node;
1020 	struct bufobj *bo;
1021 	off_t offset;
1022 	uint32_t maxsize;
1023 	daddr_t sector;
1024 	int error;
1025 
1026 	bp = a->a_bp;
1027 	vp = a->a_vp;
1028 	node = VTON(vp);
1029 
1030 	if (bp->b_blkno == bp->b_lblkno) {
1031 		offset = lblktosize(node->udfmp, bp->b_lblkno);
1032 		error = udf_bmap_internal(node, offset, &sector, &maxsize);
1033 		if (error) {
1034 			clrbuf(bp);
1035 			bp->b_blkno = -1;
1036 			bufdone(bp);
1037 			return (0);
1038 		}
1039 		/* bmap gives sector numbers, bio works with device blocks */
1040 		bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT);
1041 	}
1042 	bo = node->udfmp->im_bo;
1043 	bp->b_iooffset = dbtob(bp->b_blkno);
1044 	BO_STRATEGY(bo, bp);
1045 	return (0);
1046 }
1047 
1048 static int
1049 udf_bmap(struct vop_bmap_args *a)
1050 {
1051 	struct udf_node *node;
1052 	uint32_t max_size;
1053 	daddr_t lsector;
1054 	int nblk;
1055 	int error;
1056 
1057 	node = VTON(a->a_vp);
1058 
1059 	if (a->a_bop != NULL)
1060 		*a->a_bop = &node->udfmp->im_devvp->v_bufobj;
1061 	if (a->a_bnp == NULL)
1062 		return (0);
1063 	if (a->a_runb)
1064 		*a->a_runb = 0;
1065 
1066 	/*
1067 	 * UDF_INVALID_BMAP means data embedded into fentry, this is an internal
1068 	 * error that should not be propagated to calling code.
1069 	 * Most obvious mapping for this error is EOPNOTSUPP as we can not truly
1070 	 * translate block numbers in this case.
1071 	 * Incidentally, this return code will make vnode pager to use VOP_READ
1072 	 * to get data for mmap-ed pages and udf_read knows how to do the right
1073 	 * thing for this kind of files.
1074 	 */
1075 	error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift,
1076 	    &lsector, &max_size);
1077 	if (error == UDF_INVALID_BMAP)
1078 		return (EOPNOTSUPP);
1079 	if (error)
1080 		return (error);
1081 
1082 	/* Translate logical to physical sector number */
1083 	*a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
1084 
1085 	/*
1086 	 * Determine maximum number of readahead blocks following the
1087 	 * requested block.
1088 	 */
1089 	if (a->a_runp) {
1090 		nblk = (max_size >> node->udfmp->bshift) - 1;
1091 		if (nblk <= 0)
1092 			*a->a_runp = 0;
1093 		else if (nblk >= (MAXBSIZE >> node->udfmp->bshift))
1094 			*a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1;
1095 		else
1096 			*a->a_runp = nblk;
1097 	}
1098 
1099 	if (a->a_runb) {
1100 		*a->a_runb = 0;
1101 	}
1102 
1103 	return (0);
1104 }
1105 
1106 /*
1107  * The all powerful VOP_LOOKUP().
1108  */
1109 static int
1110 udf_lookup(struct vop_cachedlookup_args *a)
1111 {
1112 	struct vnode *dvp;
1113 	struct vnode *tdp = NULL;
1114 	struct vnode **vpp = a->a_vpp;
1115 	struct udf_node *node;
1116 	struct udf_mnt *udfmp;
1117 	struct fileid_desc *fid = NULL;
1118 	struct udf_dirstream *ds;
1119 	u_long nameiop;
1120 	u_long flags;
1121 	char *nameptr;
1122 	long namelen;
1123 	ino_t id = 0;
1124 	int offset, error = 0;
1125 	int fsize, lkflags, ltype, numdirpasses;
1126 
1127 	dvp = a->a_dvp;
1128 	node = VTON(dvp);
1129 	udfmp = node->udfmp;
1130 	nameiop = a->a_cnp->cn_nameiop;
1131 	flags = a->a_cnp->cn_flags;
1132 	lkflags = a->a_cnp->cn_lkflags;
1133 	nameptr = a->a_cnp->cn_nameptr;
1134 	namelen = a->a_cnp->cn_namelen;
1135 	fsize = le64toh(node->fentry->inf_len);
1136 
1137 	/*
1138 	 * If this is a LOOKUP and we've already partially searched through
1139 	 * the directory, pick up where we left off and flag that the
1140 	 * directory may need to be searched twice.  For a full description,
1141 	 * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
1142 	 */
1143 	if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
1144 		offset = 0;
1145 		numdirpasses = 1;
1146 	} else {
1147 		offset = node->diroff;
1148 		numdirpasses = 2;
1149 		nchstats.ncs_2passes++;
1150 	}
1151 
1152 lookloop:
1153 	ds = udf_opendir(node, offset, fsize, udfmp);
1154 
1155 	while ((fid = udf_getfid(ds)) != NULL) {
1156 		/* XXX Should we return an error on a bad fid? */
1157 		if (udf_checktag(&fid->tag, TAGID_FID)) {
1158 			printf("udf_lookup: Invalid tag\n");
1159 			error = EIO;
1160 			break;
1161 		}
1162 
1163 		/* Is this a deleted file? */
1164 		if (fid->file_char & UDF_FILE_CHAR_DEL)
1165 			continue;
1166 
1167 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
1168 			if (flags & ISDOTDOT) {
1169 				id = udf_getid(&fid->icb);
1170 				break;
1171 			}
1172 		} else {
1173 			if (!(udf_cmpname(&fid->data[fid->l_iu],
1174 			    nameptr, fid->l_fi, namelen, udfmp))) {
1175 				id = udf_getid(&fid->icb);
1176 				break;
1177 			}
1178 		}
1179 	}
1180 
1181 	if (!error)
1182 		error = ds->error;
1183 
1184 	/* XXX Bail out here? */
1185 	if (error) {
1186 		udf_closedir(ds);
1187 		return (error);
1188 	}
1189 
1190 	/* Did we have a match? */
1191 	if (id) {
1192 		/*
1193 		 * Remember where this entry was if it's the final
1194 		 * component.
1195 		 */
1196 		if ((flags & ISLASTCN) && nameiop == LOOKUP)
1197 			node->diroff = ds->offset + ds->off;
1198 		if (numdirpasses == 2)
1199 			nchstats.ncs_pass2++;
1200 		udf_closedir(ds);
1201 
1202 		if (flags & ISDOTDOT) {
1203 			error = vn_vget_ino(dvp, id, lkflags, &tdp);
1204 		} else if (node->hash_id == id) {
1205 			VREF(dvp);	/* we want ourself, ie "." */
1206 			/*
1207 			 * When we lookup "." we still can be asked to lock it
1208 			 * differently.
1209 			 */
1210 			ltype = lkflags & LK_TYPE_MASK;
1211 			if (ltype != VOP_ISLOCKED(dvp)) {
1212 				if (ltype == LK_EXCLUSIVE)
1213 					vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1214 				else /* if (ltype == LK_SHARED) */
1215 					vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1216 			}
1217 			tdp = dvp;
1218 		} else
1219 			error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp);
1220 		if (!error) {
1221 			*vpp = tdp;
1222 			/* Put this entry in the cache */
1223 			if (flags & MAKEENTRY)
1224 				cache_enter(dvp, *vpp, a->a_cnp);
1225 		}
1226 	} else {
1227 		/* Name wasn't found on this pass.  Do another pass? */
1228 		if (numdirpasses == 2) {
1229 			numdirpasses--;
1230 			offset = 0;
1231 			udf_closedir(ds);
1232 			goto lookloop;
1233 		}
1234 		udf_closedir(ds);
1235 
1236 		/* Enter name into cache as non-existant */
1237 		if (flags & MAKEENTRY)
1238 			cache_enter(dvp, *vpp, a->a_cnp);
1239 
1240 		if ((flags & ISLASTCN) &&
1241 		    (nameiop == CREATE || nameiop == RENAME)) {
1242 			error = EROFS;
1243 		} else {
1244 			error = ENOENT;
1245 		}
1246 	}
1247 
1248 	return (error);
1249 }
1250 
1251 static int
1252 udf_reclaim(struct vop_reclaim_args *a)
1253 {
1254 	struct vnode *vp;
1255 	struct udf_node *unode;
1256 
1257 	vp = a->a_vp;
1258 	unode = VTON(vp);
1259 
1260 	if (unode != NULL) {
1261 		vfs_hash_remove(vp);
1262 
1263 		if (unode->fentry != NULL)
1264 			free(unode->fentry, M_UDFFENTRY);
1265 		uma_zfree(udf_zone_node, unode);
1266 		vp->v_data = NULL;
1267 	}
1268 
1269 	return (0);
1270 }
1271 
1272 static int
1273 udf_vptofh(struct vop_vptofh_args *a)
1274 {
1275 	struct udf_node *node;
1276 	struct ifid *ifhp;
1277 
1278 	node = VTON(a->a_vp);
1279 	ifhp = (struct ifid *)a->a_fhp;
1280 	ifhp->ifid_len = sizeof(struct ifid);
1281 	ifhp->ifid_ino = node->hash_id;
1282 
1283 	return (0);
1284 }
1285 
1286 /*
1287  * Read the block and then set the data pointer to correspond with the
1288  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
1289  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
1290  * whole extent.
1291  *
1292  * Note that *bp may be assigned error or not.
1293  *
1294  */
1295 static int
1296 udf_readatoffset(struct udf_node *node, int *size, off_t offset,
1297     struct buf **bp, uint8_t **data)
1298 {
1299 	struct udf_mnt *udfmp = node->udfmp;
1300 	struct vnode *vp = node->i_vnode;
1301 	struct file_entry *fentry;
1302 	struct buf *bp1;
1303 	uint32_t max_size;
1304 	daddr_t sector;
1305 	off_t off;
1306 	int adj_size;
1307 	int error;
1308 
1309 	/*
1310 	 * This call is made *not* only to detect UDF_INVALID_BMAP case,
1311 	 * max_size is used as an ad-hoc read-ahead hint for "normal" case.
1312 	 */
1313 	error = udf_bmap_internal(node, offset, &sector, &max_size);
1314 	if (error == UDF_INVALID_BMAP) {
1315 		/*
1316 		 * This error means that the file *data* is stored in the
1317 		 * allocation descriptor field of the file entry.
1318 		 */
1319 		fentry = node->fentry;
1320 		*data = &fentry->data[le32toh(fentry->l_ea)];
1321 		*size = le32toh(fentry->l_ad);
1322 		if (offset >= *size)
1323 			*size = 0;
1324 		else {
1325 			*data += offset;
1326 			*size -= offset;
1327 		}
1328 		return (0);
1329 	} else if (error != 0) {
1330 		return (error);
1331 	}
1332 
1333 	/* Adjust the size so that it is within range */
1334 	if (*size == 0 || *size > max_size)
1335 		*size = max_size;
1336 
1337 	/*
1338 	 * Because we will read starting at block boundary, we need to adjust
1339 	 * how much we need to read so that all promised data is in.
1340 	 * Also, we can't promise to read more than MAXBSIZE bytes starting
1341 	 * from block boundary, so adjust what we promise too.
1342 	 */
1343 	off = blkoff(udfmp, offset);
1344 	*size = min(*size, MAXBSIZE - off);
1345 	adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask;
1346 	*bp = NULL;
1347 	if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) {
1348 		printf("warning: udf_readlblks returned error %d\n", error);
1349 		/* note: *bp may be non-NULL */
1350 		return (error);
1351 	}
1352 
1353 	bp1 = *bp;
1354 	*data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
1355 	return (0);
1356 }
1357 
1358 /*
1359  * Translate a file offset into a logical block and then into a physical
1360  * block.
1361  * max_size - maximum number of bytes that can be read starting from given
1362  * offset, rather than beginning of calculated sector number
1363  */
1364 static int
1365 udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
1366     uint32_t *max_size)
1367 {
1368 	struct udf_mnt *udfmp;
1369 	struct file_entry *fentry;
1370 	void *icb;
1371 	struct icb_tag *tag;
1372 	uint32_t icblen = 0;
1373 	daddr_t lsector;
1374 	int ad_offset, ad_num = 0;
1375 	int i, p_offset;
1376 
1377 	udfmp = node->udfmp;
1378 	fentry = node->fentry;
1379 	tag = &fentry->icbtag;
1380 
1381 	switch (le16toh(tag->strat_type)) {
1382 	case 4:
1383 		break;
1384 
1385 	case 4096:
1386 		printf("Cannot deal with strategy4096 yet!\n");
1387 		return (ENODEV);
1388 
1389 	default:
1390 		printf("Unknown strategy type %d\n", tag->strat_type);
1391 		return (ENODEV);
1392 	}
1393 
1394 	switch (le16toh(tag->flags) & 0x7) {
1395 	case 0:
1396 		/*
1397 		 * The allocation descriptor field is filled with short_ad's.
1398 		 * If the offset is beyond the current extent, look for the
1399 		 * next extent.
1400 		 */
1401 		do {
1402 			offset -= icblen;
1403 			ad_offset = sizeof(struct short_ad) * ad_num;
1404 			if (ad_offset > le32toh(fentry->l_ad)) {
1405 				printf("File offset out of bounds\n");
1406 				return (EINVAL);
1407 			}
1408 			icb = GETICB(short_ad, fentry,
1409 			    le32toh(fentry->l_ea) + ad_offset);
1410 			icblen = GETICBLEN(short_ad, icb);
1411 			ad_num++;
1412 		} while(offset >= icblen);
1413 
1414 		lsector = (offset  >> udfmp->bshift) +
1415 		    le32toh(((struct short_ad *)(icb))->pos);
1416 
1417 		*max_size = icblen - offset;
1418 
1419 		break;
1420 	case 1:
1421 		/*
1422 		 * The allocation descriptor field is filled with long_ad's
1423 		 * If the offset is beyond the current extent, look for the
1424 		 * next extent.
1425 		 */
1426 		do {
1427 			offset -= icblen;
1428 			ad_offset = sizeof(struct long_ad) * ad_num;
1429 			if (ad_offset > le32toh(fentry->l_ad)) {
1430 				printf("File offset out of bounds\n");
1431 				return (EINVAL);
1432 			}
1433 			icb = GETICB(long_ad, fentry,
1434 			    le32toh(fentry->l_ea) + ad_offset);
1435 			icblen = GETICBLEN(long_ad, icb);
1436 			ad_num++;
1437 		} while(offset >= icblen);
1438 
1439 		lsector = (offset >> udfmp->bshift) +
1440 		    le32toh(((struct long_ad *)(icb))->loc.lb_num);
1441 
1442 		*max_size = icblen - offset;
1443 
1444 		break;
1445 	case 3:
1446 		/*
1447 		 * This type means that the file *data* is stored in the
1448 		 * allocation descriptor field of the file entry.
1449 		 */
1450 		*max_size = 0;
1451 		*sector = node->hash_id + udfmp->part_start;
1452 
1453 		return (UDF_INVALID_BMAP);
1454 	case 2:
1455 		/* DirectCD does not use extended_ad's */
1456 	default:
1457 		printf("Unsupported allocation descriptor %d\n",
1458 		       tag->flags & 0x7);
1459 		return (ENODEV);
1460 	}
1461 
1462 	*sector = lsector + udfmp->part_start;
1463 
1464 	/*
1465 	 * Check the sparing table.  Each entry represents the beginning of
1466 	 * a packet.
1467 	 */
1468 	if (udfmp->s_table != NULL) {
1469 		for (i = 0; i< udfmp->s_table_entries; i++) {
1470 			p_offset =
1471 			    lsector - le32toh(udfmp->s_table->entries[i].org);
1472 			if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1473 				*sector =
1474 				   le32toh(udfmp->s_table->entries[i].map) +
1475 				    p_offset;
1476 				break;
1477 			}
1478 		}
1479 	}
1480 
1481 	return (0);
1482 }
1483