xref: /dragonfly/sys/vfs/udf/udf_vnops.c (revision 029e6489)
1 /*-
2  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/fs/udf/udf_vnops.c,v 1.33 2003/12/07 05:04:49 scottl Exp $
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/uio.h>
35 #include <sys/namei.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/stat.h>
39 #include <sys/module.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 
48 #include <machine/inttypes.h>
49 
50 #include <sys/buf2.h>
51 
52 #include <vfs/udf/ecma167-udf.h>
53 #include <vfs/udf/osta.h>
54 #include <vfs/udf/udf.h>
55 #include <vfs/udf/udf_mount.h>
56 
57 static int udf_access(struct vop_access_args *);
58 static int udf_getattr(struct vop_getattr_args *);
59 static int udf_ioctl(struct vop_ioctl_args *);
60 static int udf_pathconf(struct vop_pathconf_args *);
61 static int udf_read(struct vop_read_args *);
62 static int udf_readdir(struct vop_readdir_args *);
63 static int udf_readlink(struct vop_readlink_args *ap);
64 static int udf_strategy(struct vop_strategy_args *);
65 static int udf_bmap(struct vop_bmap_args *);
66 static int udf_lookup(struct vop_old_lookup_args *);
67 static int udf_reclaim(struct vop_reclaim_args *);
68 static int udf_readatoffset(struct udf_node *, int *, int, struct buf **, uint8_t **);
69 static int udf_bmap_internal(struct udf_node *, uint32_t, daddr_t *, uint32_t *);
70 
71 struct vop_ops udf_vnode_vops = {
72 	.vop_default =		vop_defaultop,
73 	.vop_access =		udf_access,
74 	.vop_bmap =		udf_bmap,
75 	.vop_old_lookup =	udf_lookup,
76 	.vop_getattr =		udf_getattr,
77 	.vop_ioctl =		udf_ioctl,
78 	.vop_pathconf =		udf_pathconf,
79 	.vop_read =		udf_read,
80 	.vop_readdir =		udf_readdir,
81 	.vop_readlink =		udf_readlink,
82 	.vop_reclaim =		udf_reclaim,
83 	.vop_strategy =		udf_strategy
84 };
85 
86 MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure");
87 MALLOC_DEFINE(M_UDFDS, "UDF DS", "UDF Dirstream structure");
88 
89 #define UDF_INVALID_BMAP	-1
90 
91 /* Look up a udf_node based on the ino_t passed in and return it's vnode */
92 int
93 udf_hashlookup(struct udf_mnt *udfmp, ino_t id, struct vnode **vpp)
94 {
95 	struct udf_node *node;
96 	struct udf_hash_lh *lh;
97 	struct vnode *vp;
98 
99 	*vpp = NULL;
100 
101 	lwkt_gettoken(&udfmp->hash_token);
102 loop:
103 	lh = &udfmp->hashtbl[id % udfmp->hashsz];
104 	if (lh == NULL) {
105 		lwkt_reltoken(&udfmp->hash_token);
106 		return(ENOENT);
107 	}
108 	LIST_FOREACH(node, lh, le) {
109 		if (node->hash_id != id)
110 			continue;
111 		vp = node->i_vnode;
112 		if (vget(vp, LK_EXCLUSIVE))
113 			goto loop;
114 		/*
115 		 * We must check to see if the inode has been ripped
116 		 * out from under us after blocking.
117 		 */
118 		lh = &udfmp->hashtbl[id % udfmp->hashsz];
119 		LIST_FOREACH(node, lh, le) {
120 			if (node->hash_id == id)
121 				break;
122 		}
123 		if (node == NULL || vp != node->i_vnode) {
124 			vput(vp);
125 			goto loop;
126 		}
127 		lwkt_reltoken(&udfmp->hash_token);
128 		*vpp = vp;
129 		return(0);
130 	}
131 
132 	lwkt_reltoken(&udfmp->hash_token);
133 	return(0);
134 }
135 
136 int
137 udf_hashins(struct udf_node *node)
138 {
139 	struct udf_mnt *udfmp;
140 	struct udf_hash_lh *lh;
141 
142 	udfmp = node->udfmp;
143 
144 	lwkt_gettoken(&udfmp->hash_token);
145 	lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
146 	LIST_INSERT_HEAD(lh, node, le);
147 	lwkt_reltoken(&udfmp->hash_token);
148 
149 	return(0);
150 }
151 
152 int
153 udf_hashrem(struct udf_node *node)
154 {
155 	struct udf_mnt *udfmp;
156 	struct udf_hash_lh *lh;
157 
158 	udfmp = node->udfmp;
159 
160 	lwkt_gettoken(&udfmp->hash_token);
161 	lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
162 	if (lh == NULL)
163 		panic("hash entry is NULL, node->hash_id= %"PRId64, node->hash_id);
164 	LIST_REMOVE(node, le);
165 	lwkt_reltoken(&udfmp->hash_token);
166 
167 	return(0);
168 }
169 
170 int
171 udf_allocv(struct mount *mp, struct vnode **vpp)
172 {
173 	int error;
174 	struct vnode *vp;
175 
176 	error = getnewvnode(VT_UDF, mp, &vp, 0, 0);
177 	if (error) {
178 		kprintf("udf_allocv: failed to allocate new vnode\n");
179 		return(error);
180 	}
181 	vx_downgrade(vp);
182 	*vpp = vp;
183 	return(0);
184 }
185 
186 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
187 static mode_t
188 udf_permtomode(struct udf_node *node)
189 {
190 	uint32_t perm;
191 	uint32_t flags;
192 	mode_t mode;
193 
194 	perm = node->fentry->perm;
195 	flags = node->fentry->icbtag.flags;
196 
197 	mode = perm & UDF_FENTRY_PERM_USER_MASK;
198 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
199 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
200 	mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
201 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
202 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
203 
204 	return(mode);
205 }
206 
207 static int
208 udf_access(struct vop_access_args *a)
209 {
210 	struct vnode *vp;
211 	struct udf_node *node;
212 
213 	vp = a->a_vp;
214 	node = VTON(vp);
215 	KKASSERT(vp->v_mount->mnt_flag & MNT_RDONLY);
216 	return (vop_helper_access(a, node->fentry->uid, node->fentry->gid,
217 				  udf_permtomode(node), 0));
218 }
219 
220 static int mon_lens[2][12] = {
221 	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
222 	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
223 };
224 
225 static int
226 udf_isaleapyear(int year)
227 {
228 	int i;
229 
230 	i = (year % 4) ? 0 : 1;
231 	i &= (year % 100) ? 1 : 0;
232 	i |= (year % 400) ? 0 : 1;
233 
234 	return(i);
235 }
236 
237 /*
238  * XXX This is just a rough hack.  Daylight savings isn't calculated and tv_nsec
239  * is ignored.
240  * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
241  */
242 static void
243 udf_timetotimespec(struct timestamp *time, struct timespec *t)
244 {
245 	int i, lpyear, daysinyear;
246 	union {
247 		uint16_t	u_tz_offset;
248 		int16_t		s_tz_offset;
249 	} tz;
250 
251 	t->tv_nsec = 0;
252 
253 	/* DirectCD seems to like using bogus year values */
254 	if (time->year < 1970) {
255 		t->tv_sec = 0;
256 		return;
257 	}
258 
259 	/* Calculate the time and day */
260 	t->tv_sec = time->second;
261 	t->tv_sec += time->minute * 60;
262 	t->tv_sec += time->hour * 3600;
263 	t->tv_sec += time->day * 3600 * 24;
264 
265 	/* Calclulate the month */
266 	lpyear = udf_isaleapyear(time->year);
267 	for (i = 1; i < time->month; i++)
268 		t->tv_sec += mon_lens[lpyear][i] * 3600 * 24;
269 
270 	/* Speed up the calculation */
271 	if (time->year > 1979)
272 		t->tv_sec += 315532800;
273 	if (time->year > 1989)
274 		t->tv_sec += 315619200;
275 	if (time->year > 1999)
276 		t->tv_sec += 315532800;
277 	for (i = 2000; i < time->year; i++) {
278 		daysinyear = udf_isaleapyear(i) + 365 ;
279 		t->tv_sec += daysinyear * 3600 * 24;
280 	}
281 
282 	/*
283 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
284 	 * compliment, so we gotta do some extra magic to handle it right.
285 	 */
286 	tz.u_tz_offset = time->type_tz;
287 	tz.u_tz_offset &= 0x0fff;
288 	if (tz.u_tz_offset & 0x0800)
289 		tz.u_tz_offset |= 0xf000;	/* extend the sign to 16 bits */
290 	if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047))
291 		t->tv_sec -= tz.s_tz_offset * 60;
292 
293 	return;
294 }
295 
296 static int
297 udf_getattr(struct vop_getattr_args *a)
298 {
299 	struct vnode *vp;
300 	struct udf_node *node;
301 	struct vattr *vap;
302 	struct file_entry *fentry;
303 
304 	vp = a->a_vp;
305 	vap = a->a_vap;
306 	node = VTON(vp);
307 	fentry = node->fentry;
308 
309 	vap->va_fsid = devid_from_dev(node->i_dev);
310 	vap->va_fileid = node->hash_id;
311 	vap->va_mode = udf_permtomode(node);
312 	vap->va_nlink = fentry->link_cnt;
313 	/*
314 	 * XXX The spec says that -1 is valid for uid/gid and indicates an
315 	 * invalid uid/gid.  How should this be represented?
316 	 */
317 	vap->va_uid = (fentry->uid == 0xffffffff) ? 0 : fentry->uid;
318 	vap->va_gid = (fentry->gid == 0xffffffff) ? 0 : fentry->gid;
319 	udf_timetotimespec(&fentry->atime, &vap->va_atime);
320 	udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
321 	vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
322 	vap->va_rmajor = VNOVAL;
323 	vap->va_rminor = VNOVAL;
324 	if (vp->v_type & VDIR) {
325 		/*
326 		 * Directories that are recorded within their ICB will show
327 		 * as having 0 blocks recorded.  Since tradition dictates
328 		 * that directories consume at least one logical block,
329 		 * make it appear so.
330 		 */
331 		if (fentry->logblks_rec != 0)
332 			vap->va_size = fentry->logblks_rec * node->udfmp->bsize;
333 		else
334 			vap->va_size = node->udfmp->bsize;
335 	} else
336 		vap->va_size = fentry->inf_len;
337 	vap->va_flags = 0;
338 	vap->va_gen = 1;
339 	vap->va_blocksize = node->udfmp->bsize;
340 	vap->va_bytes = fentry->inf_len;
341 	vap->va_type = vp->v_type;
342 	vap->va_filerev = 0; /* XXX */
343 	return(0);
344 }
345 
346 /*
347  * File specific ioctls.  DeCSS candidate?
348  */
349 static int
350 udf_ioctl(struct vop_ioctl_args *a)
351 {
352 	kprintf("%s called\n", __func__);
353 	return(ENOTTY);
354 }
355 
356 /*
357  * I'm not sure that this has much value in a read-only filesystem, but
358  * cd9660 has it too.
359  */
360 static int
361 udf_pathconf(struct vop_pathconf_args *a)
362 {
363 
364 	switch (a->a_name) {
365 	case _PC_LINK_MAX:
366 		*a->a_retval = 65535;
367 		return(0);
368 	case _PC_NAME_MAX:
369 		*a->a_retval = NAME_MAX;
370 		return(0);
371 	case _PC_PATH_MAX:
372 		*a->a_retval = PATH_MAX;
373 		return(0);
374 	case _PC_NO_TRUNC:
375 		*a->a_retval = 1;
376 		return(0);
377 	default:
378 		return(EINVAL);
379 	}
380 }
381 
382 static int
383 udf_read(struct vop_read_args *a)
384 {
385 	struct vnode *vp = a->a_vp;
386 	struct uio *uio = a->a_uio;
387 	struct udf_node *node = VTON(vp);
388 	struct buf *bp;
389 	uint8_t *data;
390 	int error = 0;
391 	int size, fsize, offset;
392 
393 	if (uio->uio_offset < 0)
394 		return(EINVAL);
395 
396 	fsize = node->fentry->inf_len;
397 
398 	while (uio->uio_offset < fsize && uio->uio_resid > 0) {
399 		offset = uio->uio_offset;
400 		size = uio->uio_resid;
401 		error = udf_readatoffset(node, &size, offset, &bp, &data);
402 		if (error == 0)
403 			error = uiomove(data, size, uio);
404 		if (bp != NULL)
405 			brelse(bp);
406 		if (error)
407 			break;
408 	}
409 
410 	return(error);
411 }
412 
413 /*
414  * Call the OSTA routines to translate the name from a CS0 dstring to a
415  * 16-bit Unicode String.  Hooks need to be placed in here to translate from
416  * Unicode to the encoding that the kernel/user expects.  Return the length
417  * of the translated string.
418  */
419 static int
420 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
421 {
422 	unicode_t *transname;
423 	int i, unilen = 0, destlen;
424 
425 	/* Convert 16-bit Unicode to destname */
426 	/* allocate a buffer big enough to hold an 8->16 bit expansion */
427 	transname = kmalloc(NAME_MAX * sizeof(unicode_t), M_TEMP, M_WAITOK | M_ZERO);
428 
429 	if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) {
430 		kprintf("udf: Unicode translation failed\n");
431 		kfree(transname, M_TEMP);
432 		return(0);
433 	}
434 
435 	for (i = 0; i < unilen ; i++)
436 		if (transname[i] & 0xff00)
437 			destname[i] = '.';	/* Fudge the 16bit chars */
438 		else
439 			destname[i] = transname[i] & 0xff;
440 	kfree(transname, M_TEMP);
441 	destname[unilen] = 0;
442 	destlen = unilen;
443 
444 	return(destlen);
445 }
446 
447 /*
448  * Compare a CS0 dstring with a name passed in from the VFS layer.  Return
449  * 0 on a successful match, nonzero therwise.  Unicode work may need to be done
450  * here also.
451  */
452 static int
453 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
454 {
455 	char *transname;
456 	int error = 0;
457 
458 	/* This is overkill, but not worth creating a new zone */
459 
460 	transname = kmalloc(NAME_MAX * sizeof(unicode_t), M_TEMP,
461 			   M_WAITOK | M_ZERO);
462 
463 	cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
464 
465 	/* Easy check.  If they aren't the same length, they aren't equal */
466 	if ((cs0len == 0) || (cs0len != cmplen))
467 		error = -1;
468 	else
469 		error = bcmp(transname, cmpname, cmplen);
470 
471 	kfree(transname, M_TEMP);
472 	return(error);
473 }
474 
475 struct udf_uiodir {
476 	struct dirent *dirent;
477 	off_t *cookies;
478 	int ncookies;
479 	int acookies;
480 	int eofflag;
481 };
482 
483 static struct udf_dirstream *
484 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
485 {
486 	struct udf_dirstream *ds;
487 
488 	ds = kmalloc(sizeof(*ds), M_UDFDS, M_WAITOK | M_ZERO);
489 
490 	ds->node = node;
491 	ds->offset = offset;
492 	ds->udfmp = udfmp;
493 	ds->fsize = fsize;
494 
495 	return(ds);
496 }
497 
498 static struct fileid_desc *
499 udf_getfid(struct udf_dirstream *ds)
500 {
501 	struct fileid_desc *fid;
502 	int error, frag_size = 0, total_fid_size;
503 
504 	/* End of directory? */
505 	if (ds->offset + ds->off >= ds->fsize) {
506 		ds->error = 0;
507 		return(NULL);
508 	}
509 
510 	/* Grab the first extent of the directory */
511 	if (ds->off == 0) {
512 		ds->size = 0;
513 		if (ds->bp != NULL)
514 			brelse(ds->bp);
515 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
516 		    &ds->bp, &ds->data);
517 		if (error) {
518 			ds->error = error;
519 			return(NULL);
520 		}
521 	}
522 
523 	/*
524 	 * Clean up from a previous fragmented FID.
525 	 * XXX Is this the right place for this?
526 	 */
527 	if (ds->fid_fragment && ds->buf != NULL) {
528 		ds->fid_fragment = 0;
529 		kfree(ds->buf, M_UDFFID);
530 	}
531 
532 	fid = (struct fileid_desc*)&ds->data[ds->off];
533 
534 	/*
535 	 * Check to see if the fid is fragmented. The first test
536 	 * ensures that we don't wander off the end of the buffer
537 	 * looking for the l_iu and l_fi fields.
538 	 */
539 	if (ds->off + UDF_FID_SIZE > ds->size ||
540 	    ds->off + fid->l_iu + fid->l_fi + UDF_FID_SIZE > ds->size) {
541 
542 		/* Copy what we have of the fid into a buffer */
543 		frag_size = ds->size - ds->off;
544 		if (frag_size >= ds->udfmp->bsize) {
545 			kprintf("udf: invalid FID fragment\n");
546 			ds->error = EINVAL;
547 			return(NULL);
548 		}
549 
550 		/*
551 		 * File ID descriptors can only be at most one
552 		 * logical sector in size.
553 		 */
554 		ds->buf = kmalloc(ds->udfmp->bsize, M_UDFFID, M_WAITOK | M_ZERO);
555 		bcopy(fid, ds->buf, frag_size);
556 
557 		/* Reduce all of the casting magic */
558 		fid = (struct fileid_desc*)ds->buf;
559 
560 		if (ds->bp != NULL)
561 			brelse(ds->bp);
562 
563 		/* Fetch the next allocation */
564 		ds->offset += ds->size;
565 		ds->size = 0;
566 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
567 		    &ds->bp, &ds->data);
568 		if (error) {
569 			ds->error = error;
570 			return(NULL);
571 		}
572 
573 		/*
574 		 * If the fragment was so small that we didn't get
575 		 * the l_iu and l_fi fields, copy those in.
576 		 */
577 		if (frag_size < UDF_FID_SIZE)
578 			bcopy(ds->data, &ds->buf[frag_size],
579 			    UDF_FID_SIZE - frag_size);
580 
581 		/*
582 		 * Now that we have enough of the fid to work with,
583 		 * copy in the rest of the fid from the new
584 		 * allocation.
585 		 */
586 		total_fid_size = UDF_FID_SIZE + fid->l_iu + fid->l_fi;
587 		if (total_fid_size > ds->udfmp->bsize) {
588 			kprintf("udf: invalid FID\n");
589 			ds->error = EIO;
590 			return(NULL);
591 		}
592 		bcopy(ds->data, &ds->buf[frag_size],
593 		    total_fid_size - frag_size);
594 
595 		ds->fid_fragment = 1;
596 	} else
597 		total_fid_size = fid->l_iu + fid->l_fi + UDF_FID_SIZE;
598 
599 	/*
600 	 * Update the offset. Align on a 4 byte boundary because the
601 	 * UDF spec says so.
602 	 */
603 	ds->this_off = ds->off;
604 	if (!ds->fid_fragment)
605 		ds->off += (total_fid_size + 3) & ~0x03;
606 	else
607 		ds->off = (total_fid_size - frag_size + 3) & ~0x03;
608 
609 	return(fid);
610 }
611 
612 static void
613 udf_closedir(struct udf_dirstream *ds)
614 {
615 
616 	if (ds->bp != NULL)
617 		brelse(ds->bp);
618 
619 	if (ds->fid_fragment && ds->buf != NULL)
620 		kfree(ds->buf, M_UDFFID);
621 
622 	kfree(ds, M_UDFDS);
623 }
624 
625 static int
626 udf_readdir(struct vop_readdir_args *a)
627 {
628 	struct vnode *vp;
629 	struct uio *uio;
630 	struct udf_node *node;
631 	struct udf_mnt *udfmp;
632 	struct fileid_desc *fid;
633 	struct udf_uiodir uiodir;
634 	struct udf_dirstream *ds;
635 	off_t *cookies = NULL;
636 	int ncookies;
637 	int error = 0;
638 	char *name;
639 
640 	vp = a->a_vp;
641 
642 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
643 	if (error)
644 		return (error);
645 
646 	uio = a->a_uio;
647 	node = VTON(vp);
648 	udfmp = node->udfmp;
649 	uiodir.eofflag = 1;
650 
651 	if (a->a_ncookies != NULL) {
652 		/*
653 		 * Guess how many entries are needed.  If we run out, this
654 		 * function will be called again and thing will pick up were
655 		 * it left off.
656 		 */
657 		ncookies = uio->uio_resid / 8 + 1;
658 		if (ncookies > 1024)
659 			ncookies = 1024;
660 		cookies = kmalloc(sizeof(off_t) * ncookies, M_TEMP, M_WAITOK);
661 		uiodir.ncookies = ncookies;
662 		uiodir.cookies = cookies;
663 		uiodir.acookies = 0;
664 	} else {
665 		uiodir.cookies = NULL;
666 		uiodir.ncookies = 0;
667 	}
668 
669 	/*
670 	 * Iterate through the file id descriptors.  Give the parent dir
671 	 * entry special attention.
672 	 */
673 	ds = udf_opendir(node, uio->uio_offset, node->fentry->inf_len,
674 			 node->udfmp);
675 
676 	name = kmalloc(NAME_MAX, M_TEMP, M_WAITOK);
677 
678 	while ((fid = udf_getfid(ds)) != NULL) {
679 
680 		/* XXX Should we return an error on a bad fid? */
681 		if (udf_checktag(&fid->tag, TAGID_FID)) {
682 			kprintf("Invalid FID tag\n");
683 			error = EIO;
684 			break;
685 		}
686 
687 		/* Is this a deleted file? */
688 		if (fid->file_char & UDF_FILE_CHAR_DEL)
689 			continue;
690 
691 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
692 			/* Do up the '.' and '..' entries.  Dummy values are
693 			 * used for the cookies since the offset here is
694 			 * usually zero, and NFS doesn't like that value
695 			 */
696 			if (uiodir.cookies != NULL) {
697 				if (++uiodir.acookies > uiodir.ncookies) {
698 					uiodir.eofflag = 0;
699 					break;
700 				}
701 				*uiodir.cookies++ = 1;
702 			}
703 			if (vop_write_dirent(&error, uio, node->hash_id, DT_DIR,
704 					     1, ".")) {
705 				uiodir.eofflag = 0;
706 				break;
707 			}
708 			if (error) {
709 				uiodir.eofflag = 0;
710 				break;
711 			}
712 			if (uiodir.cookies != NULL) {
713 				if (++uiodir.acookies > uiodir.ncookies) {
714 					uiodir.eofflag = 0;
715 					break;
716 				}
717 				*uiodir.cookies++ = 2;
718 			}
719 			if (vop_write_dirent(&error, uio, udf_getid(&fid->icb),
720 					     DT_DIR, 2, "..")) {
721 				uiodir.eofflag = 0;
722 				break;
723 			}
724 			if (error) {
725 				uiodir.eofflag = 0;
726 				break;
727 			}
728 		} else {
729 			uint8_t d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
730 			    DT_DIR : DT_UNKNOWN;
731 			uint16_t namelen = udf_transname(&fid->data[fid->l_iu],
732 			    name, fid->l_fi, udfmp);
733 
734 			if (uiodir.cookies != NULL) {
735 				if (++uiodir.acookies > uiodir.ncookies) {
736 					uiodir.eofflag = 0;
737 					break;
738 				}
739 				*uiodir.cookies++ = ds->this_off;
740 			}
741 			if (vop_write_dirent(&error, uio, udf_getid(&fid->icb),
742 					 d_type, namelen, name)) {
743 				uiodir.eofflag = 0;
744 				break;
745 			}
746 			if (error) {
747 				uiodir.eofflag = 0;
748 				break;
749 			}
750 		}
751 		if (error) {
752 			kprintf("uiomove returned %d\n", error);
753 			break;
754 		}
755 
756 	}
757 
758 	kfree(name, M_TEMP);
759 
760 	/* tell the calling layer whether we need to be called again */
761 	*a->a_eofflag = uiodir.eofflag;
762 	uio->uio_offset = ds->offset + ds->off;
763 
764 	if (!error)
765 		error = ds->error;
766 
767 	udf_closedir(ds);
768 
769 	if (a->a_ncookies != NULL) {
770 		if (error)
771 			kfree(cookies, M_TEMP);
772 		else {
773 			*a->a_ncookies = uiodir.acookies;
774 			*a->a_cookies = cookies;
775 		}
776 	}
777 
778 	vn_unlock(vp);
779 	return(error);
780 }
781 
782 /* Are there any implementations out there that do soft-links? */
783 static int
784 udf_readlink(struct vop_readlink_args *ap)
785 {
786 	kprintf("%s called\n", __func__);
787 	return(EOPNOTSUPP);
788 }
789 
790 static int
791 udf_strategy(struct vop_strategy_args *ap)
792 {
793 	struct bio *bio;
794 	struct bio *nbio;
795 	struct buf *bp;
796 	struct vnode *vp;
797 	struct udf_node *node;
798 	int maxsize;
799 	daddr_t dblkno;
800 
801 	bio = ap->a_bio;
802 	bp = bio->bio_buf;
803 	vp = ap->a_vp;
804 	node = VTON(vp);
805 
806 	nbio = push_bio(bio);
807 	if (nbio->bio_offset == NOOFFSET) {
808 		/*
809 		 * Files that are embedded in the fentry don't translate well
810 		 * to a block number.  Reject.
811 		 */
812 		if (udf_bmap_internal(node,
813 				     bio->bio_offset,
814 				     &dblkno, &maxsize)) {
815 			clrbuf(bp);
816 			nbio->bio_offset = NOOFFSET;
817 		} else {
818 			nbio->bio_offset = dbtob(dblkno);
819 		}
820 	}
821 	if (nbio->bio_offset == NOOFFSET) {
822 		/* I/O was never started on nbio, must biodone(bio) */
823 		biodone(bio);
824 		return(0);
825 	}
826 	vn_strategy(node->i_devvp, nbio);
827 	return(0);
828 }
829 
830 static int
831 udf_bmap(struct vop_bmap_args *a)
832 {
833 	struct udf_node *node;
834 	uint32_t max_size;
835 	daddr_t lsector;
836 	int error;
837 
838 	node = VTON(a->a_vp);
839 
840 	if (a->a_doffsetp == NULL)
841 		return(0);
842 
843 	KKASSERT(a->a_loffset % node->udfmp->bsize == 0);
844 
845 	error = udf_bmap_internal(node, a->a_loffset, &lsector, &max_size);
846 	if (error)
847 		return(error);
848 
849 	/* Translate logical to physical sector number */
850 	*a->a_doffsetp = (off_t)lsector << node->udfmp->bshift;
851 
852 	/* Punt on read-ahead for now */
853 	if (a->a_runp)
854 		*a->a_runp = 0;
855 	if (a->a_runb)
856 		*a->a_runb = 0;
857 	return(0);
858 }
859 
860 /*
861  * The all powerful VOP_LOOKUP().
862  */
863 static int
864 udf_lookup(struct vop_old_lookup_args *a)
865 {
866 	struct vnode *dvp;
867 	struct vnode *tdp = NULL;
868 	struct vnode **vpp = a->a_vpp;
869 	struct udf_node *node;
870 	struct udf_mnt *udfmp;
871 	struct fileid_desc *fid = NULL;
872 	struct udf_dirstream *ds;
873 	u_long nameiop;
874 	u_long flags;
875 	char *nameptr;
876 	long namelen;
877 	ino_t id = 0;
878 	int offset, error = 0;
879 	int numdirpasses, fsize;
880 
881 	dvp = a->a_dvp;
882 	node = VTON(dvp);
883 	udfmp = node->udfmp;
884 	nameiop = a->a_cnp->cn_nameiop;
885 	flags = a->a_cnp->cn_flags;
886 	nameptr = a->a_cnp->cn_nameptr;
887 	namelen = a->a_cnp->cn_namelen;
888 	fsize = node->fentry->inf_len;
889 
890 	*vpp = NULL;
891 
892 	/*
893 	 * If this is a LOOKUP and we've already partially searched through
894 	 * the directory, pick up where we left off and flag that the
895 	 * directory may need to be searched twice.  For a full description,
896 	 * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup()
897 	 */
898 	if (nameiop != NAMEI_LOOKUP || node->diroff == 0 ||
899 	    node->diroff > fsize) {
900 		offset = 0;
901 		numdirpasses = 1;
902 	} else {
903 		offset = node->diroff;
904 		numdirpasses = 2;
905 	}
906 
907 lookloop:
908 	ds = udf_opendir(node, offset, fsize, udfmp);
909 
910 	while ((fid = udf_getfid(ds)) != NULL) {
911 		/* XXX Should we return an error on a bad fid? */
912 		if (udf_checktag(&fid->tag, TAGID_FID)) {
913 			kprintf("udf_lookup: Invalid tag\n");
914 			error = EIO;
915 			break;
916 		}
917 
918 		/* Is this a deleted file? */
919 		if (fid->file_char & UDF_FILE_CHAR_DEL)
920 			continue;
921 
922 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
923 			if (flags & CNP_ISDOTDOT) {
924 				id = udf_getid(&fid->icb);
925 				break;
926 			}
927 		} else {
928 			if (!(udf_cmpname(&fid->data[fid->l_iu],
929 					  nameptr, fid->l_fi, namelen, udfmp))) {
930 				id = udf_getid(&fid->icb);
931 				break;
932 			}
933 		}
934 	}
935 
936 	if (!error)
937 		error = ds->error;
938 
939 	/* XXX Bail out here? */
940 	if (error) {
941 		udf_closedir(ds);
942 		return (error);
943 	}
944 
945 	/* Did we have a match? */
946 	if (id) {
947 		error = udf_vget(udfmp->im_mountp, NULL, id, &tdp);
948 		if (!error) {
949 			/*
950 			 * Remember where this entry was if it's the final
951 			 * component.
952 			 */
953 			if (nameiop == NAMEI_LOOKUP)
954 				node->diroff = ds->offset + ds->off;
955 			if ((flags & CNP_LOCKPARENT) == 0) {
956 				a->a_cnp->cn_flags |= CNP_PDIRUNLOCK;
957 				vn_unlock(dvp);
958 			}
959 
960 			*vpp = tdp;
961 		}
962 	} else {
963 		/* Name wasn't found on this pass.  Do another pass? */
964 		if (numdirpasses == 2) {
965 			numdirpasses--;
966 			offset = 0;
967 			udf_closedir(ds);
968 			goto lookloop;
969 		}
970 		if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) {
971 			error = EROFS;
972 		} else {
973 			error = ENOENT;
974 		}
975 	}
976 
977 	udf_closedir(ds);
978 	return(error);
979 }
980 
981 static int
982 udf_reclaim(struct vop_reclaim_args *a)
983 {
984 	struct vnode *vp;
985 	struct udf_node *unode;
986 
987 	vp = a->a_vp;
988 	unode = VTON(vp);
989 
990 	if (unode != NULL) {
991 		udf_hashrem(unode);
992 		if (unode->i_devvp) {
993 			vrele(unode->i_devvp);
994 			unode->i_devvp = 0;
995 		}
996 
997 		if (unode->fentry != NULL)
998 			kfree(unode->fentry, M_UDFFENTRY);
999 		kfree(unode, M_UDFNODE);
1000 		vp->v_data = NULL;
1001 	}
1002 
1003 	return(0);
1004 }
1005 
1006 /*
1007  * Read the block and then set the data pointer to correspond with the
1008  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
1009  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
1010  * whole extent.
1011  *
1012  * Note that *bp may be assigned error or not.
1013  *
1014  * XXX 'size' is limited to the logical block size for now due to problems
1015  * with udf_read()
1016  */
1017 static int
1018 udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp,
1019 		 uint8_t **data)
1020 {
1021 	struct udf_mnt *udfmp;
1022 	struct file_entry *fentry = NULL;
1023 	struct buf *bp1;
1024 	uint32_t max_size;
1025 	daddr_t sector;
1026 	int error;
1027 
1028 	udfmp = node->udfmp;
1029 
1030 	*bp = NULL;
1031 	error = udf_bmap_internal(node, offset, &sector, &max_size);
1032 	if (error == UDF_INVALID_BMAP) {
1033 		/*
1034 		 * This error means that the file *data* is stored in the
1035 		 * allocation descriptor field of the file entry.
1036 		 */
1037 		fentry = node->fentry;
1038 		*data = &fentry->data[fentry->l_ea];
1039 		*size = fentry->l_ad;
1040 		return(0);
1041 	} else if (error != 0) {
1042 		return(error);
1043 	}
1044 
1045 	/* Adjust the size so that it is within range */
1046 	if (*size == 0 || *size > max_size)
1047 		*size = max_size;
1048 	*size = min(*size, MAXBSIZE);
1049 
1050 	if ((error = udf_readlblks(udfmp, sector, *size, bp))) {
1051 		kprintf("warning: udf_readlblks returned error %d\n", error);
1052 		/* note: *bp may be non-NULL */
1053 		return(error);
1054 	}
1055 
1056 	bp1 = *bp;
1057 	*data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize];
1058 	return(0);
1059 }
1060 
1061 /*
1062  * Translate a file offset into a logical block and then into a physical
1063  * block.
1064  */
1065 static int
1066 udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size)
1067 {
1068 	struct udf_mnt *udfmp;
1069 	struct file_entry *fentry;
1070 	void *icb;
1071 	struct icb_tag *tag;
1072 	uint32_t icblen = 0;
1073 	daddr_t lsector;
1074 	int ad_offset, ad_num = 0;
1075 	int i, p_offset;
1076 
1077 	udfmp = node->udfmp;
1078 	fentry = node->fentry;
1079 	tag = &fentry->icbtag;
1080 
1081 	switch (tag->strat_type) {
1082 	case 4:
1083 		break;
1084 
1085 	case 4096:
1086 		kprintf("Cannot deal with strategy4096 yet!\n");
1087 		return(ENODEV);
1088 
1089 	default:
1090 		kprintf("Unknown strategy type %d\n", tag->strat_type);
1091 		return(ENODEV);
1092 	}
1093 
1094 	switch (tag->flags & 0x7) {
1095 	case 0:
1096 		/*
1097 		 * The allocation descriptor field is filled with short_ad's.
1098 		 * If the offset is beyond the current extent, look for the
1099 		 * next extent.
1100 		 */
1101 		do {
1102 			offset -= icblen;
1103 			ad_offset = sizeof(struct short_ad) * ad_num;
1104 			if (ad_offset > fentry->l_ad) {
1105 				kprintf("File offset out of bounds\n");
1106 				return(EINVAL);
1107 			}
1108 			icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1109 			icblen = GETICBLEN(short_ad, icb);
1110 			ad_num++;
1111 		} while(offset >= icblen);
1112 
1113 		lsector = (offset  >> udfmp->bshift) +
1114 		    ((struct short_ad *)(icb))->pos;
1115 
1116 		*max_size = GETICBLEN(short_ad, icb);
1117 
1118 		break;
1119 	case 1:
1120 		/*
1121 		 * The allocation descriptor field is filled with long_ad's
1122 		 * If the offset is beyond the current extent, look for the
1123 		 * next extent.
1124 		 */
1125 		do {
1126 			offset -= icblen;
1127 			ad_offset = sizeof(struct long_ad) * ad_num;
1128 			if (ad_offset > fentry->l_ad) {
1129 				kprintf("File offset out of bounds\n");
1130 				return(EINVAL);
1131 			}
1132 			icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1133 			icblen = GETICBLEN(long_ad, icb);
1134 			ad_num++;
1135 		} while(offset >= icblen);
1136 
1137 		lsector = (offset >> udfmp->bshift) +
1138 		    ((struct long_ad *)(icb))->loc.lb_num;
1139 
1140 		*max_size = GETICBLEN(long_ad, icb);
1141 
1142 		break;
1143 	case 3:
1144 		/*
1145 		 * This type means that the file *data* is stored in the
1146 		 * allocation descriptor field of the file entry.
1147 		 */
1148 		*max_size = 0;
1149 		*sector = node->hash_id + udfmp->part_start;
1150 
1151 		return(UDF_INVALID_BMAP);
1152 	case 2:
1153 		/* DirectCD does not use extended_ad's */
1154 	default:
1155 		kprintf("Unsupported allocation descriptor %d\n",
1156 		       tag->flags & 0x7);
1157 		return(ENODEV);
1158 	}
1159 
1160 	*sector = lsector + udfmp->part_start;
1161 
1162 	/*
1163 	 * Check the sparing table.  Each entry represents the beginning of
1164 	 * a packet.
1165 	 */
1166 	if (udfmp->s_table != NULL) {
1167 		for (i = 0; i< udfmp->s_table_entries; i++) {
1168 			p_offset = lsector - udfmp->s_table->entries[i].org;
1169 			if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1170 				*sector = udfmp->s_table->entries[i].map +
1171 				    p_offset;
1172 				break;
1173 			}
1174 		}
1175 	}
1176 
1177 	return(0);
1178 }
1179