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