xref: /freebsd/sys/fs/udf/udf_vnops.c (revision 315ee00f)
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 		if (cookies == NULL)
804 			return (ENOMEM);
805 		uiodir.ncookies = ncookies;
806 		uiodir.cookies = cookies;
807 		uiodir.acookies = 0;
808 	} else {
809 		uiodir.cookies = NULL;
810 	}
811 
812 	/*
813 	 * Iterate through the file id descriptors.  Give the parent dir
814 	 * entry special attention.
815 	 */
816 	ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
817 	    node->udfmp);
818 
819 	while ((fid = udf_getfid(ds)) != NULL) {
820 		/* XXX Should we return an error on a bad fid? */
821 		if (udf_checktag(&fid->tag, TAGID_FID)) {
822 			printf("Invalid FID tag\n");
823 			hexdump(fid, UDF_FID_SIZE, NULL, 0);
824 			error = EIO;
825 			break;
826 		}
827 
828 		/* Is this a deleted file? */
829 		if (fid->file_char & UDF_FILE_CHAR_DEL)
830 			continue;
831 
832 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
833 			/* Do up the '.' and '..' entries.  Dummy values are
834 			 * used for the cookies since the offset here is
835 			 * usually zero, and NFS doesn't like that value
836 			 */
837 			dir.d_fileno = node->hash_id;
838 			dir.d_type = DT_DIR;
839 			dir.d_name[0] = '.';
840 			dir.d_namlen = 1;
841 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
842 			dir.d_off = 1;
843 			dirent_terminate(&dir);
844 			uiodir.dirent = &dir;
845 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
846 			if (error)
847 				break;
848 
849 			dir.d_fileno = udf_getid(&fid->icb);
850 			dir.d_type = DT_DIR;
851 			dir.d_name[0] = '.';
852 			dir.d_name[1] = '.';
853 			dir.d_namlen = 2;
854 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
855 			dir.d_off = 2;
856 			dirent_terminate(&dir);
857 			uiodir.dirent = &dir;
858 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
859 		} else {
860 			dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
861 			    &dir.d_name[0], fid->l_fi, udfmp);
862 			dir.d_fileno = udf_getid(&fid->icb);
863 			dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
864 			    DT_DIR : DT_UNKNOWN;
865 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
866 			dir.d_off = ds->this_off;
867 			dirent_terminate(&dir);
868 			uiodir.dirent = &dir;
869 			error = udf_uiodir(&uiodir, dir.d_reclen, uio,
870 			    ds->this_off);
871 		}
872 		if (error)
873 			break;
874 		uio->uio_offset = ds->offset + ds->off;
875 	}
876 
877 	/* tell the calling layer whether we need to be called again */
878 	*a->a_eofflag = uiodir.eofflag;
879 
880 	if (error < 0)
881 		error = 0;
882 	if (!error)
883 		error = ds->error;
884 
885 	udf_closedir(ds);
886 
887 	if (a->a_ncookies != NULL) {
888 		if (error)
889 			free(cookies, M_TEMP);
890 		else {
891 			*a->a_ncookies = uiodir.acookies;
892 			*a->a_cookies = cookies;
893 		}
894 	}
895 
896 	return (error);
897 }
898 
899 static int
900 udf_readlink(struct vop_readlink_args *ap)
901 {
902 	struct path_component *pc, *end;
903 	struct vnode *vp;
904 	struct uio uio;
905 	struct iovec iov[1];
906 	struct udf_node *node;
907 	void *buf;
908 	char *cp;
909 	int error, len, root;
910 
911 	/*
912 	 * A symbolic link in UDF is a list of variable-length path
913 	 * component structures.  We build a pathname in the caller's
914 	 * uio by traversing this list.
915 	 */
916 	vp = ap->a_vp;
917 	node = VTON(vp);
918 	len = le64toh(node->fentry->inf_len);
919 	buf = malloc(len, M_DEVBUF, M_WAITOK);
920 	iov[0].iov_len = len;
921 	iov[0].iov_base = buf;
922 	uio.uio_iov = iov;
923 	uio.uio_iovcnt = 1;
924 	uio.uio_offset = 0;
925 	uio.uio_resid = iov[0].iov_len;
926 	uio.uio_segflg = UIO_SYSSPACE;
927 	uio.uio_rw = UIO_READ;
928 	uio.uio_td = curthread;
929 	error = VOP_READ(vp, &uio, 0, ap->a_cred);
930 	if (error)
931 		goto error;
932 
933 	pc = buf;
934 	end = (void *)((char *)buf + len);
935 	root = 0;
936 	while (pc < end) {
937 		switch (pc->type) {
938 		case UDF_PATH_ROOT:
939 			/* Only allow this at the beginning of a path. */
940 			if ((void *)pc != buf) {
941 				error = EINVAL;
942 				goto error;
943 			}
944 			cp = "/";
945 			len = 1;
946 			root = 1;
947 			break;
948 		case UDF_PATH_DOT:
949 			cp = ".";
950 			len = 1;
951 			break;
952 		case UDF_PATH_DOTDOT:
953 			cp = "..";
954 			len = 2;
955 			break;
956 		case UDF_PATH_PATH:
957 			if (pc->length == 0) {
958 				error = EINVAL;
959 				goto error;
960 			}
961 			/*
962 			 * XXX: We only support CS8 which appears to map
963 			 * to ASCII directly.
964 			 */
965 			switch (pc->identifier[0]) {
966 			case 8:
967 				cp = pc->identifier + 1;
968 				len = pc->length - 1;
969 				break;
970 			default:
971 				error = EOPNOTSUPP;
972 				goto error;
973 			}
974 			break;
975 		default:
976 			error = EINVAL;
977 			goto error;
978 		}
979 
980 		/*
981 		 * If this is not the first component, insert a path
982 		 * separator.
983 		 */
984 		if (pc != buf) {
985 			/* If we started with root we already have a "/". */
986 			if (root)
987 				goto skipslash;
988 			root = 0;
989 			if (ap->a_uio->uio_resid < 1) {
990 				error = ENAMETOOLONG;
991 				goto error;
992 			}
993 			error = uiomove("/", 1, ap->a_uio);
994 			if (error)
995 				break;
996 		}
997 	skipslash:
998 
999 		/* Append string at 'cp' of length 'len' to our path. */
1000 		if (len > ap->a_uio->uio_resid) {
1001 			error = ENAMETOOLONG;
1002 			goto error;
1003 		}
1004 		error = uiomove(cp, len, ap->a_uio);
1005 		if (error)
1006 			break;
1007 
1008 		/* Advance to next component. */
1009 		pc = (void *)((char *)pc + 4 + pc->length);
1010 	}
1011 error:
1012 	free(buf, M_DEVBUF);
1013 	return (error);
1014 }
1015 
1016 static int
1017 udf_strategy(struct vop_strategy_args *a)
1018 {
1019 	struct buf *bp;
1020 	struct vnode *vp;
1021 	struct udf_node *node;
1022 	struct bufobj *bo;
1023 	off_t offset;
1024 	uint32_t maxsize;
1025 	daddr_t sector;
1026 	int error;
1027 
1028 	bp = a->a_bp;
1029 	vp = a->a_vp;
1030 	node = VTON(vp);
1031 
1032 	if (bp->b_blkno == bp->b_lblkno) {
1033 		offset = lblktosize(node->udfmp, bp->b_lblkno);
1034 		error = udf_bmap_internal(node, offset, &sector, &maxsize);
1035 		if (error) {
1036 			clrbuf(bp);
1037 			bp->b_blkno = -1;
1038 			bufdone(bp);
1039 			return (0);
1040 		}
1041 		/* bmap gives sector numbers, bio works with device blocks */
1042 		bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT);
1043 	}
1044 	bo = node->udfmp->im_bo;
1045 	bp->b_iooffset = dbtob(bp->b_blkno);
1046 	BO_STRATEGY(bo, bp);
1047 	return (0);
1048 }
1049 
1050 static int
1051 udf_bmap(struct vop_bmap_args *a)
1052 {
1053 	struct udf_node *node;
1054 	uint32_t max_size;
1055 	daddr_t lsector;
1056 	int nblk;
1057 	int error;
1058 
1059 	node = VTON(a->a_vp);
1060 
1061 	if (a->a_bop != NULL)
1062 		*a->a_bop = &node->udfmp->im_devvp->v_bufobj;
1063 	if (a->a_bnp == NULL)
1064 		return (0);
1065 	if (a->a_runb)
1066 		*a->a_runb = 0;
1067 
1068 	/*
1069 	 * UDF_INVALID_BMAP means data embedded into fentry, this is an internal
1070 	 * error that should not be propagated to calling code.
1071 	 * Most obvious mapping for this error is EOPNOTSUPP as we can not truly
1072 	 * translate block numbers in this case.
1073 	 * Incidentally, this return code will make vnode pager to use VOP_READ
1074 	 * to get data for mmap-ed pages and udf_read knows how to do the right
1075 	 * thing for this kind of files.
1076 	 */
1077 	error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift,
1078 	    &lsector, &max_size);
1079 	if (error == UDF_INVALID_BMAP)
1080 		return (EOPNOTSUPP);
1081 	if (error)
1082 		return (error);
1083 
1084 	/* Translate logical to physical sector number */
1085 	*a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
1086 
1087 	/*
1088 	 * Determine maximum number of readahead blocks following the
1089 	 * requested block.
1090 	 */
1091 	if (a->a_runp) {
1092 		nblk = (max_size >> node->udfmp->bshift) - 1;
1093 		if (nblk <= 0)
1094 			*a->a_runp = 0;
1095 		else if (nblk >= (MAXBSIZE >> node->udfmp->bshift))
1096 			*a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1;
1097 		else
1098 			*a->a_runp = nblk;
1099 	}
1100 
1101 	if (a->a_runb) {
1102 		*a->a_runb = 0;
1103 	}
1104 
1105 	return (0);
1106 }
1107 
1108 /*
1109  * The all powerful VOP_LOOKUP().
1110  */
1111 static int
1112 udf_lookup(struct vop_cachedlookup_args *a)
1113 {
1114 	struct vnode *dvp;
1115 	struct vnode *tdp = NULL;
1116 	struct vnode **vpp = a->a_vpp;
1117 	struct udf_node *node;
1118 	struct udf_mnt *udfmp;
1119 	struct fileid_desc *fid = NULL;
1120 	struct udf_dirstream *ds;
1121 	u_long nameiop;
1122 	u_long flags;
1123 	char *nameptr;
1124 	long namelen;
1125 	ino_t id = 0;
1126 	int offset, error = 0;
1127 	int fsize, lkflags, ltype, numdirpasses;
1128 
1129 	dvp = a->a_dvp;
1130 	node = VTON(dvp);
1131 	udfmp = node->udfmp;
1132 	nameiop = a->a_cnp->cn_nameiop;
1133 	flags = a->a_cnp->cn_flags;
1134 	lkflags = a->a_cnp->cn_lkflags;
1135 	nameptr = a->a_cnp->cn_nameptr;
1136 	namelen = a->a_cnp->cn_namelen;
1137 	fsize = le64toh(node->fentry->inf_len);
1138 
1139 	/*
1140 	 * If this is a LOOKUP and we've already partially searched through
1141 	 * the directory, pick up where we left off and flag that the
1142 	 * directory may need to be searched twice.  For a full description,
1143 	 * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
1144 	 */
1145 	if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
1146 		offset = 0;
1147 		numdirpasses = 1;
1148 	} else {
1149 		offset = node->diroff;
1150 		numdirpasses = 2;
1151 		nchstats.ncs_2passes++;
1152 	}
1153 
1154 lookloop:
1155 	ds = udf_opendir(node, offset, fsize, udfmp);
1156 
1157 	while ((fid = udf_getfid(ds)) != NULL) {
1158 		/* XXX Should we return an error on a bad fid? */
1159 		if (udf_checktag(&fid->tag, TAGID_FID)) {
1160 			printf("udf_lookup: Invalid tag\n");
1161 			error = EIO;
1162 			break;
1163 		}
1164 
1165 		/* Is this a deleted file? */
1166 		if (fid->file_char & UDF_FILE_CHAR_DEL)
1167 			continue;
1168 
1169 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
1170 			if (flags & ISDOTDOT) {
1171 				id = udf_getid(&fid->icb);
1172 				break;
1173 			}
1174 		} else {
1175 			if (!(udf_cmpname(&fid->data[fid->l_iu],
1176 			    nameptr, fid->l_fi, namelen, udfmp))) {
1177 				id = udf_getid(&fid->icb);
1178 				break;
1179 			}
1180 		}
1181 	}
1182 
1183 	if (!error)
1184 		error = ds->error;
1185 
1186 	/* XXX Bail out here? */
1187 	if (error) {
1188 		udf_closedir(ds);
1189 		return (error);
1190 	}
1191 
1192 	/* Did we have a match? */
1193 	if (id) {
1194 		/*
1195 		 * Remember where this entry was if it's the final
1196 		 * component.
1197 		 */
1198 		if ((flags & ISLASTCN) && nameiop == LOOKUP)
1199 			node->diroff = ds->offset + ds->off;
1200 		if (numdirpasses == 2)
1201 			nchstats.ncs_pass2++;
1202 		udf_closedir(ds);
1203 
1204 		if (flags & ISDOTDOT) {
1205 			error = vn_vget_ino(dvp, id, lkflags, &tdp);
1206 		} else if (node->hash_id == id) {
1207 			VREF(dvp);	/* we want ourself, ie "." */
1208 			/*
1209 			 * When we lookup "." we still can be asked to lock it
1210 			 * differently.
1211 			 */
1212 			ltype = lkflags & LK_TYPE_MASK;
1213 			if (ltype != VOP_ISLOCKED(dvp)) {
1214 				if (ltype == LK_EXCLUSIVE)
1215 					vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1216 				else /* if (ltype == LK_SHARED) */
1217 					vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1218 			}
1219 			tdp = dvp;
1220 		} else
1221 			error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp);
1222 		if (!error) {
1223 			*vpp = tdp;
1224 			/* Put this entry in the cache */
1225 			if (flags & MAKEENTRY)
1226 				cache_enter(dvp, *vpp, a->a_cnp);
1227 		}
1228 	} else {
1229 		/* Name wasn't found on this pass.  Do another pass? */
1230 		if (numdirpasses == 2) {
1231 			numdirpasses--;
1232 			offset = 0;
1233 			udf_closedir(ds);
1234 			goto lookloop;
1235 		}
1236 		udf_closedir(ds);
1237 
1238 		/* Enter name into cache as non-existant */
1239 		if (flags & MAKEENTRY)
1240 			cache_enter(dvp, *vpp, a->a_cnp);
1241 
1242 		if ((flags & ISLASTCN) &&
1243 		    (nameiop == CREATE || nameiop == RENAME)) {
1244 			error = EROFS;
1245 		} else {
1246 			error = ENOENT;
1247 		}
1248 	}
1249 
1250 	return (error);
1251 }
1252 
1253 static int
1254 udf_reclaim(struct vop_reclaim_args *a)
1255 {
1256 	struct vnode *vp;
1257 	struct udf_node *unode;
1258 
1259 	vp = a->a_vp;
1260 	unode = VTON(vp);
1261 
1262 	if (unode != NULL) {
1263 		vfs_hash_remove(vp);
1264 
1265 		if (unode->fentry != NULL)
1266 			free(unode->fentry, M_UDFFENTRY);
1267 		uma_zfree(udf_zone_node, unode);
1268 		vp->v_data = NULL;
1269 	}
1270 
1271 	return (0);
1272 }
1273 
1274 static int
1275 udf_vptofh(struct vop_vptofh_args *a)
1276 {
1277 	struct udf_node *node;
1278 	struct ifid *ifhp;
1279 
1280 	node = VTON(a->a_vp);
1281 	ifhp = (struct ifid *)a->a_fhp;
1282 	ifhp->ifid_len = sizeof(struct ifid);
1283 	ifhp->ifid_ino = node->hash_id;
1284 
1285 	return (0);
1286 }
1287 
1288 /*
1289  * Read the block and then set the data pointer to correspond with the
1290  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
1291  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
1292  * whole extent.
1293  *
1294  * Note that *bp may be assigned error or not.
1295  *
1296  */
1297 static int
1298 udf_readatoffset(struct udf_node *node, int *size, off_t offset,
1299     struct buf **bp, uint8_t **data)
1300 {
1301 	struct udf_mnt *udfmp = node->udfmp;
1302 	struct vnode *vp = node->i_vnode;
1303 	struct file_entry *fentry;
1304 	struct buf *bp1;
1305 	uint32_t max_size;
1306 	daddr_t sector;
1307 	off_t off;
1308 	int adj_size;
1309 	int error;
1310 
1311 	/*
1312 	 * This call is made *not* only to detect UDF_INVALID_BMAP case,
1313 	 * max_size is used as an ad-hoc read-ahead hint for "normal" case.
1314 	 */
1315 	error = udf_bmap_internal(node, offset, &sector, &max_size);
1316 	if (error == UDF_INVALID_BMAP) {
1317 		/*
1318 		 * This error means that the file *data* is stored in the
1319 		 * allocation descriptor field of the file entry.
1320 		 */
1321 		fentry = node->fentry;
1322 		*data = &fentry->data[le32toh(fentry->l_ea)];
1323 		*size = le32toh(fentry->l_ad);
1324 		if (offset >= *size)
1325 			*size = 0;
1326 		else {
1327 			*data += offset;
1328 			*size -= offset;
1329 		}
1330 		return (0);
1331 	} else if (error != 0) {
1332 		return (error);
1333 	}
1334 
1335 	/* Adjust the size so that it is within range */
1336 	if (*size == 0 || *size > max_size)
1337 		*size = max_size;
1338 
1339 	/*
1340 	 * Because we will read starting at block boundary, we need to adjust
1341 	 * how much we need to read so that all promised data is in.
1342 	 * Also, we can't promise to read more than MAXBSIZE bytes starting
1343 	 * from block boundary, so adjust what we promise too.
1344 	 */
1345 	off = blkoff(udfmp, offset);
1346 	*size = min(*size, MAXBSIZE - off);
1347 	adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask;
1348 	*bp = NULL;
1349 	if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) {
1350 		printf("warning: udf_readlblks returned error %d\n", error);
1351 		/* note: *bp may be non-NULL */
1352 		return (error);
1353 	}
1354 
1355 	bp1 = *bp;
1356 	*data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
1357 	return (0);
1358 }
1359 
1360 /*
1361  * Translate a file offset into a logical block and then into a physical
1362  * block.
1363  * max_size - maximum number of bytes that can be read starting from given
1364  * offset, rather than beginning of calculated sector number
1365  */
1366 static int
1367 udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
1368     uint32_t *max_size)
1369 {
1370 	struct udf_mnt *udfmp;
1371 	struct file_entry *fentry;
1372 	void *icb;
1373 	struct icb_tag *tag;
1374 	uint32_t icblen = 0;
1375 	daddr_t lsector;
1376 	int ad_offset, ad_num = 0;
1377 	int i, p_offset;
1378 
1379 	udfmp = node->udfmp;
1380 	fentry = node->fentry;
1381 	tag = &fentry->icbtag;
1382 
1383 	switch (le16toh(tag->strat_type)) {
1384 	case 4:
1385 		break;
1386 
1387 	case 4096:
1388 		printf("Cannot deal with strategy4096 yet!\n");
1389 		return (ENODEV);
1390 
1391 	default:
1392 		printf("Unknown strategy type %d\n", tag->strat_type);
1393 		return (ENODEV);
1394 	}
1395 
1396 	switch (le16toh(tag->flags) & 0x7) {
1397 	case 0:
1398 		/*
1399 		 * The allocation descriptor field is filled with short_ad's.
1400 		 * If the offset is beyond the current extent, look for the
1401 		 * next extent.
1402 		 */
1403 		do {
1404 			offset -= icblen;
1405 			ad_offset = sizeof(struct short_ad) * ad_num;
1406 			if (ad_offset > le32toh(fentry->l_ad)) {
1407 				printf("File offset out of bounds\n");
1408 				return (EINVAL);
1409 			}
1410 			icb = GETICB(short_ad, fentry,
1411 			    le32toh(fentry->l_ea) + ad_offset);
1412 			icblen = GETICBLEN(short_ad, icb);
1413 			ad_num++;
1414 		} while(offset >= icblen);
1415 
1416 		lsector = (offset  >> udfmp->bshift) +
1417 		    le32toh(((struct short_ad *)(icb))->pos);
1418 
1419 		*max_size = icblen - offset;
1420 
1421 		break;
1422 	case 1:
1423 		/*
1424 		 * The allocation descriptor field is filled with long_ad's
1425 		 * If the offset is beyond the current extent, look for the
1426 		 * next extent.
1427 		 */
1428 		do {
1429 			offset -= icblen;
1430 			ad_offset = sizeof(struct long_ad) * ad_num;
1431 			if (ad_offset > le32toh(fentry->l_ad)) {
1432 				printf("File offset out of bounds\n");
1433 				return (EINVAL);
1434 			}
1435 			icb = GETICB(long_ad, fentry,
1436 			    le32toh(fentry->l_ea) + ad_offset);
1437 			icblen = GETICBLEN(long_ad, icb);
1438 			ad_num++;
1439 		} while(offset >= icblen);
1440 
1441 		lsector = (offset >> udfmp->bshift) +
1442 		    le32toh(((struct long_ad *)(icb))->loc.lb_num);
1443 
1444 		*max_size = icblen - offset;
1445 
1446 		break;
1447 	case 3:
1448 		/*
1449 		 * This type means that the file *data* is stored in the
1450 		 * allocation descriptor field of the file entry.
1451 		 */
1452 		*max_size = 0;
1453 		*sector = node->hash_id + udfmp->part_start;
1454 
1455 		return (UDF_INVALID_BMAP);
1456 	case 2:
1457 		/* DirectCD does not use extended_ad's */
1458 	default:
1459 		printf("Unsupported allocation descriptor %d\n",
1460 		       tag->flags & 0x7);
1461 		return (ENODEV);
1462 	}
1463 
1464 	*sector = lsector + udfmp->part_start;
1465 
1466 	/*
1467 	 * Check the sparing table.  Each entry represents the beginning of
1468 	 * a packet.
1469 	 */
1470 	if (udfmp->s_table != NULL) {
1471 		for (i = 0; i< udfmp->s_table_entries; i++) {
1472 			p_offset =
1473 			    lsector - le32toh(udfmp->s_table->entries[i].org);
1474 			if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1475 				*sector =
1476 				   le32toh(udfmp->s_table->entries[i].map) +
1477 				    p_offset;
1478 				break;
1479 			}
1480 		}
1481 	}
1482 
1483 	return (0);
1484 }
1485