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