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