xref: /freebsd/sys/fs/udf/udf_vnops.c (revision 8db4c2f20cc5faa979226e8d4c8adb19db6af06f)
151a7b740SScott Long /*-
251a7b740SScott Long  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
351a7b740SScott Long  * All rights reserved.
451a7b740SScott Long  *
551a7b740SScott Long  * Redistribution and use in source and binary forms, with or without
651a7b740SScott Long  * modification, are permitted provided that the following conditions
751a7b740SScott Long  * are met:
851a7b740SScott Long  * 1. Redistributions of source code must retain the above copyright
951a7b740SScott Long  *    notice, this list of conditions and the following disclaimer.
1051a7b740SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
1151a7b740SScott Long  *    notice, this list of conditions and the following disclaimer in the
1251a7b740SScott Long  *    documentation and/or other materials provided with the distribution.
1351a7b740SScott Long  *
1451a7b740SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1551a7b740SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1651a7b740SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1751a7b740SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1851a7b740SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1951a7b740SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2051a7b740SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2151a7b740SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2251a7b740SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2351a7b740SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2451a7b740SScott Long  * SUCH DAMAGE.
2551a7b740SScott Long  *
2651a7b740SScott Long  * $FreeBSD$
2751a7b740SScott Long  */
2851a7b740SScott Long 
2951a7b740SScott Long /* udf_vnops.c */
3051a7b740SScott Long /* Take care of the vnode side of things */
3151a7b740SScott Long 
3251a7b740SScott Long #include <sys/param.h>
3351a7b740SScott Long #include <sys/systm.h>
3451a7b740SScott Long #include <sys/namei.h>
3551a7b740SScott Long #include <sys/kernel.h>
3651a7b740SScott Long #include <sys/malloc.h>
3751a7b740SScott Long #include <sys/stat.h>
3851a7b740SScott Long #include <sys/bio.h>
3951a7b740SScott Long #include <sys/buf.h>
4051a7b740SScott Long #include <sys/mount.h>
4151a7b740SScott Long #include <sys/vnode.h>
4251a7b740SScott Long #include <sys/dirent.h>
4351a7b740SScott Long #include <sys/queue.h>
4451a7b740SScott Long #include <sys/unistd.h>
4551a7b740SScott Long 
4651a7b740SScott Long #include <vm/uma.h>
4751a7b740SScott Long 
4851a7b740SScott Long #include <fs/udf/ecma167-udf.h>
4951a7b740SScott Long #include <fs/udf/osta.h>
5051a7b740SScott Long #include <fs/udf/udf.h>
5151a7b740SScott Long 
5251a7b740SScott Long static int udf_access(struct vop_access_args *);
5351a7b740SScott Long static int udf_getattr(struct vop_getattr_args *);
5451a7b740SScott Long static int udf_ioctl(struct vop_ioctl_args *);
5551a7b740SScott Long static int udf_pathconf(struct vop_pathconf_args *);
5651a7b740SScott Long static int udf_read(struct vop_read_args *);
5751a7b740SScott Long static int udf_readdir(struct vop_readdir_args *);
5851a7b740SScott Long static int udf_readlink(struct vop_readlink_args *ap);
5951a7b740SScott Long static int udf_strategy(struct vop_strategy_args *);
6051a7b740SScott Long static int udf_print(struct vop_print_args *);
6151a7b740SScott Long static int udf_bmap(struct vop_bmap_args *);
6251a7b740SScott Long static int udf_lookup(struct vop_cachedlookup_args *);
6351a7b740SScott Long static int udf_reclaim(struct vop_reclaim_args *);
6451a7b740SScott Long static void udf_dumpblock(void *, int) __unused;
65c2d6947dSJeroen Ruigrok van der Werven static int udf_readatoffset(struct udf_node *, int *, int, struct buf **, uint8_t **);
6698b0c789SPoul-Henning Kamp static int udf_bmap_internal(struct udf_node *, uint32_t, daddr_t *, uint32_t *);
6751a7b740SScott Long 
6851a7b740SScott Long vop_t **udf_vnodeop_p;
6951a7b740SScott Long static struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
7051a7b740SScott Long 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
7151a7b740SScott Long 	{ &vop_access_desc,		(vop_t *) udf_access },
7251a7b740SScott Long 	{ &vop_bmap_desc,		(vop_t *) udf_bmap },
7351a7b740SScott Long 	{ &vop_cachedlookup_desc,	(vop_t *) udf_lookup },
7451a7b740SScott Long 	{ &vop_getattr_desc,		(vop_t *) udf_getattr },
7551a7b740SScott Long 	{ &vop_ioctl_desc,		(vop_t *) udf_ioctl },
7651a7b740SScott Long 	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
7751a7b740SScott Long 	{ &vop_lock_desc,		(vop_t *) vop_stdlock },
7851a7b740SScott Long 	{ &vop_lookup_desc,		(vop_t *) vfs_cache_lookup },
7951a7b740SScott Long 	{ &vop_pathconf_desc,		(vop_t *) udf_pathconf },
8051a7b740SScott Long 	{ &vop_print_desc,		(vop_t *) udf_print },
8151a7b740SScott Long 	{ &vop_read_desc,		(vop_t *) udf_read },
8251a7b740SScott Long 	{ &vop_readdir_desc,		(vop_t *) udf_readdir },
8351a7b740SScott Long 	{ &vop_readlink_desc,		(vop_t *) udf_readlink },
8451a7b740SScott Long 	{ &vop_reclaim_desc,		(vop_t *) udf_reclaim },
8551a7b740SScott Long 	{ &vop_strategy_desc,		(vop_t *) udf_strategy },
8651a7b740SScott Long 	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
8751a7b740SScott Long 	{ NULL, NULL }
8851a7b740SScott Long };
8951a7b740SScott Long static struct vnodeopv_desc udf_vnodeop_opv_desc =
9051a7b740SScott Long 	{ &udf_vnodeop_p, udf_vnodeop_entries };
9151a7b740SScott Long VNODEOP_SET(udf_vnodeop_opv_desc);
9251a7b740SScott Long 
9351a7b740SScott Long MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure");
9451a7b740SScott Long 
958db4c2f2SScott Long #define INVALID_BMAP	-1
968db4c2f2SScott Long 
9751a7b740SScott Long /* Look up a udf_node based on the ino_t passed in and return it's vnode */
9851a7b740SScott Long int
9951a7b740SScott Long udf_hashlookup(struct udf_mnt *udfmp, ino_t id, int flags, struct vnode **vpp)
10051a7b740SScott Long {
10151a7b740SScott Long 	struct udf_node *node;
10251a7b740SScott Long 	int error;
10351a7b740SScott Long 
10451a7b740SScott Long 	*vpp = NULL;
10551a7b740SScott Long 
10651a7b740SScott Long loop:
10751a7b740SScott Long 	mtx_lock(&udfmp->hash_mtx);
10851a7b740SScott Long 	TAILQ_FOREACH(node, &udfmp->udf_tqh, tq) {
10951a7b740SScott Long 		if (node->hash_id == id) {
11051a7b740SScott Long 			VI_LOCK(node->i_vnode);
11151a7b740SScott Long 			mtx_unlock(&udfmp->hash_mtx);
11251a7b740SScott Long 			error = vget(node->i_vnode, flags | LK_INTERLOCK,
11351a7b740SScott Long 			    curthread);
11451a7b740SScott Long 			if (error == ENOENT)
11551a7b740SScott Long 				goto loop;
11651a7b740SScott Long 			if (error)
11751a7b740SScott Long 				return (error);
11851a7b740SScott Long 			*vpp = node->i_vnode;
11951a7b740SScott Long 			return (0);
12051a7b740SScott Long 		}
12151a7b740SScott Long 	}
12251a7b740SScott Long 
12351a7b740SScott Long 	mtx_unlock(&udfmp->hash_mtx);
12451a7b740SScott Long 	return (0);
12551a7b740SScott Long }
12651a7b740SScott Long 
12751a7b740SScott Long int
12851a7b740SScott Long udf_hashins(struct udf_node *node)
12951a7b740SScott Long {
13051a7b740SScott Long 	struct udf_mnt *udfmp;
13151a7b740SScott Long 
13251a7b740SScott Long 	udfmp = node->udfmp;
13351a7b740SScott Long 
13451a7b740SScott Long 	mtx_lock(&udfmp->hash_mtx);
13551a7b740SScott Long 	TAILQ_INSERT_TAIL(&udfmp->udf_tqh, node, tq);
13651a7b740SScott Long 	mtx_unlock(&udfmp->hash_mtx);
13751a7b740SScott Long 	lockmgr(&node->i_vnode->v_lock, LK_EXCLUSIVE, (struct mtx *)0,
13851a7b740SScott Long 		curthread);
13951a7b740SScott Long 
14051a7b740SScott Long 	return (0);
14151a7b740SScott Long }
14251a7b740SScott Long 
14351a7b740SScott Long int
14451a7b740SScott Long udf_hashrem(struct udf_node *node)
14551a7b740SScott Long {
14651a7b740SScott Long 	struct udf_mnt *udfmp;
14751a7b740SScott Long 
14851a7b740SScott Long 	udfmp = node->udfmp;
14951a7b740SScott Long 
15051a7b740SScott Long 	mtx_lock(&udfmp->hash_mtx);
15151a7b740SScott Long 	TAILQ_REMOVE(&udfmp->udf_tqh, node, tq);
15251a7b740SScott Long 	mtx_unlock(&udfmp->hash_mtx);
15351a7b740SScott Long 
15451a7b740SScott Long 	return (0);
15551a7b740SScott Long }
15651a7b740SScott Long 
15751a7b740SScott Long int
15851a7b740SScott Long udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td)
15951a7b740SScott Long {
16051a7b740SScott Long 	int error;
16151a7b740SScott Long 	struct vnode *vp;
16251a7b740SScott Long 
16351a7b740SScott Long 	error = getnewvnode(VT_UDF, mp, udf_vnodeop_p, &vp);
16451a7b740SScott Long 	if (error) {
16551a7b740SScott Long 		printf("udf_allocv: failed to allocate new vnode\n");
16651a7b740SScott Long 		return (error);
16751a7b740SScott Long 	}
16851a7b740SScott Long 
16951a7b740SScott Long 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
17051a7b740SScott Long 	*vpp = vp;
17151a7b740SScott Long 	return (0);
17251a7b740SScott Long }
17351a7b740SScott Long 
17451a7b740SScott Long /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
17551a7b740SScott Long static mode_t
17651a7b740SScott Long udf_permtomode(struct udf_node *node)
17751a7b740SScott Long {
178c2d6947dSJeroen Ruigrok van der Werven 	uint32_t perm;
179c2d6947dSJeroen Ruigrok van der Werven 	uint32_t flags;
18051a7b740SScott Long 	mode_t mode;
18151a7b740SScott Long 
18251a7b740SScott Long 	perm = node->fentry->perm;
18351a7b740SScott Long 	flags = node->fentry->icbtag.flags;
18451a7b740SScott Long 
18551a7b740SScott Long 	mode = perm & UDF_FENTRY_PERM_USER_MASK;
18651a7b740SScott Long 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
18751a7b740SScott Long 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
18851a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
18951a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
19051a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
19151a7b740SScott Long 
19251a7b740SScott Long 	return (mode);
19351a7b740SScott Long }
19451a7b740SScott Long 
19551a7b740SScott Long static int
19651a7b740SScott Long udf_access(struct vop_access_args *a)
19751a7b740SScott Long {
19851a7b740SScott Long 	struct vnode *vp;
19951a7b740SScott Long 	struct udf_node *node;
20051a7b740SScott Long 	mode_t a_mode, mode;
20151a7b740SScott Long 
20251a7b740SScott Long 	vp = a->a_vp;
20351a7b740SScott Long 	node = VTON(vp);
20451a7b740SScott Long 	a_mode = a->a_mode;
20551a7b740SScott Long 
20651a7b740SScott Long 	if (a_mode & VWRITE) {
20751a7b740SScott Long 		switch (vp->v_type) {
20851a7b740SScott Long 		case VDIR:
20951a7b740SScott Long 		case VLNK:
21051a7b740SScott Long 		case VREG:
21151a7b740SScott Long 			return (EROFS);
21251a7b740SScott Long 			/* NOT REACHED */
21351a7b740SScott Long 		default:
21451a7b740SScott Long 			break;
21551a7b740SScott Long 		}
21651a7b740SScott Long 	}
21751a7b740SScott Long 
21851a7b740SScott Long 	mode = udf_permtomode(node);
21951a7b740SScott Long 
22051a7b740SScott Long 	return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid,
22151a7b740SScott Long 	    a_mode, a->a_cred, NULL));
22251a7b740SScott Long }
22351a7b740SScott Long 
22451a7b740SScott Long static int mon_lens[2][12] = {
22551a7b740SScott Long 	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
22651a7b740SScott Long 	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
22751a7b740SScott Long };
22851a7b740SScott Long 
22951a7b740SScott Long static int
23051a7b740SScott Long udf_isaleapyear(int year)
23151a7b740SScott Long {
23251a7b740SScott Long 	int i;
23351a7b740SScott Long 
23451a7b740SScott Long 	i = (year % 4) ? 0 : 1;
23551a7b740SScott Long 	i &= (year % 100) ? 1 : 0;
23651a7b740SScott Long 	i |= (year % 400) ? 0 : 1;
23751a7b740SScott Long 
23851a7b740SScott Long 	return i;
23951a7b740SScott Long }
24051a7b740SScott Long 
24151a7b740SScott Long /*
24251a7b740SScott Long  * XXX This is just a rough hack.  Daylight savings isn't calculated and tv_nsec
24351a7b740SScott Long  * is ignored.
24451a7b740SScott Long  * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
24551a7b740SScott Long  */
24651a7b740SScott Long static void
24751a7b740SScott Long udf_timetotimespec(struct timestamp *time, struct timespec *t)
24851a7b740SScott Long {
24951a7b740SScott Long 	int i, lpyear, daysinyear;
25051a7b740SScott Long 	union {
251c2d6947dSJeroen Ruigrok van der Werven 		uint16_t	u_tz_offset;
25251a7b740SScott Long 		int16_t		s_tz_offset;
25351a7b740SScott Long 	} tz;
25451a7b740SScott Long 
25551a7b740SScott Long 	t->tv_nsec = 0;
25651a7b740SScott Long 
25751a7b740SScott Long 	/* DirectCD seems to like using bogus year values */
25851a7b740SScott Long 	if (time->year < 1970) {
25951a7b740SScott Long 		t->tv_sec = 0;
26051a7b740SScott Long 		return;
26151a7b740SScott Long 	}
26251a7b740SScott Long 
26351a7b740SScott Long 	/* Calculate the time and day */
26451a7b740SScott Long 	t->tv_sec = time->second;
26551a7b740SScott Long 	t->tv_sec += time->minute * 60;
26651a7b740SScott Long 	t->tv_sec += time->hour * 3600;
26751a7b740SScott Long 	t->tv_sec += time->day * 3600 * 24;
26851a7b740SScott Long 
26951a7b740SScott Long 	/* Calclulate the month */
27051a7b740SScott Long 	lpyear = udf_isaleapyear(time->year);
27151a7b740SScott Long 	for (i = 1; i < time->month; i++)
27251a7b740SScott Long 		t->tv_sec += mon_lens[lpyear][i] * 3600 * 24;
27351a7b740SScott Long 
27451a7b740SScott Long 	/* Speed up the calculation */
27551a7b740SScott Long 	if (time->year > 1979)
27651a7b740SScott Long 		t->tv_sec += 315532800;
27751a7b740SScott Long 	if (time->year > 1989)
27851a7b740SScott Long 		t->tv_sec += 315619200;
27951a7b740SScott Long 	if (time->year > 1999)
28051a7b740SScott Long 		t->tv_sec += 315532800;
28151a7b740SScott Long 	for (i = 2000; i < time->year; i++) {
28251a7b740SScott Long 		daysinyear = udf_isaleapyear(i) + 365 ;
28351a7b740SScott Long 		t->tv_sec += daysinyear * 3600 * 24;
28451a7b740SScott Long 	}
28551a7b740SScott Long 
28651a7b740SScott Long 	/*
28751a7b740SScott Long 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
28851a7b740SScott Long 	 * compliment, so we gotta do some extra magic to handle it right.
28951a7b740SScott Long 	 */
29051a7b740SScott Long 	tz.u_tz_offset = time->type_tz;
29151a7b740SScott Long 	tz.u_tz_offset &= 0x0fff;
29251a7b740SScott Long 	if (tz.u_tz_offset & 0x0800)
29351a7b740SScott Long 		tz.u_tz_offset |= 0xf000;	/* extend the sign to 16 bits */
29451a7b740SScott Long 	if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047))
29551a7b740SScott Long 		t->tv_sec -= tz.s_tz_offset * 60;
29651a7b740SScott Long 
29751a7b740SScott Long 	return;
29851a7b740SScott Long }
29951a7b740SScott Long 
30051a7b740SScott Long static int
30151a7b740SScott Long udf_getattr(struct vop_getattr_args *a)
30251a7b740SScott Long {
30351a7b740SScott Long 	struct vnode *vp;
30451a7b740SScott Long 	struct udf_node *node;
30551a7b740SScott Long 	struct vattr *vap;
30651a7b740SScott Long 	struct file_entry *fentry;
30751a7b740SScott Long 	struct timespec ts;
30851a7b740SScott Long 
30951a7b740SScott Long 	ts.tv_sec = 0;
31051a7b740SScott Long 
31151a7b740SScott Long 	vp = a->a_vp;
31251a7b740SScott Long 	vap = a->a_vap;
31351a7b740SScott Long 	node = VTON(vp);
31451a7b740SScott Long 	fentry = node->fentry;
31551a7b740SScott Long 
31651a7b740SScott Long 	vap->va_fsid = dev2udev(node->i_dev);
31751a7b740SScott Long 	vap->va_fileid = node->hash_id;
31851a7b740SScott Long 	vap->va_mode = udf_permtomode(node);
31951a7b740SScott Long 	vap->va_nlink = fentry->link_cnt;
32051a7b740SScott Long 	/*
32151a7b740SScott Long 	 * XXX The spec says that -1 is valid for uid/gid and indicates an
32251a7b740SScott Long 	 * invalid uid/gid.  How should this be represented?
32351a7b740SScott Long 	 */
32451a7b740SScott Long 	vap->va_uid = (fentry->uid == -1) ? 0 : fentry->uid;
32551a7b740SScott Long 	vap->va_gid = (fentry->gid == -1) ? 0 : fentry->gid;
32651a7b740SScott Long 	udf_timetotimespec(&fentry->atime, &vap->va_atime);
32751a7b740SScott Long 	udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
32851a7b740SScott Long 	vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
32951a7b740SScott Long 	vap->va_rdev = 0; /* XXX */
33051a7b740SScott Long 	if (vp->v_type & VDIR) {
33151a7b740SScott Long 		/*
33251a7b740SScott Long 		 * Directories that are recorded within their ICB will show
33351a7b740SScott Long 		 * as having 0 blocks recorded.  Since tradition dictates
33451a7b740SScott Long 		 * that directories consume at least one logical block,
33551a7b740SScott Long 		 * make it appear so.
33651a7b740SScott Long 		 */
33751a7b740SScott Long 		if (fentry->logblks_rec != 0) {
33851a7b740SScott Long 			vap->va_size = fentry->logblks_rec * node->udfmp->bsize;
33951a7b740SScott Long 		} else {
34051a7b740SScott Long 			vap->va_size = node->udfmp->bsize;
34151a7b740SScott Long 		}
34251a7b740SScott Long 	} else {
34351a7b740SScott Long 		vap->va_size = fentry->inf_len;
34451a7b740SScott Long 	}
34551a7b740SScott Long 	vap->va_flags = 0;
34651a7b740SScott Long 	vap->va_gen = 1;
34751a7b740SScott Long 	vap->va_blocksize = node->udfmp->bsize;
34851a7b740SScott Long 	vap->va_bytes = fentry->inf_len;
34951a7b740SScott Long 	vap->va_type = vp->v_type;
35051a7b740SScott Long 	vap->va_filerev = 0; /* XXX */
35151a7b740SScott Long 	return (0);
35251a7b740SScott Long }
35351a7b740SScott Long 
35451a7b740SScott Long /*
35551a7b740SScott Long  * File specific ioctls.  DeCSS candidate?
35651a7b740SScott Long  */
35751a7b740SScott Long static int
35851a7b740SScott Long udf_ioctl(struct vop_ioctl_args *a)
35951a7b740SScott Long {
36051a7b740SScott Long 	printf("%s called\n", __FUNCTION__);
36151a7b740SScott Long 	return (EOPNOTSUPP);
36251a7b740SScott Long }
36351a7b740SScott Long 
36451a7b740SScott Long /*
36551a7b740SScott Long  * I'm not sure that this has much value in a read-only filesystem, but
36651a7b740SScott Long  * cd9660 has it too.
36751a7b740SScott Long  */
36851a7b740SScott Long static int
36951a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a)
37051a7b740SScott Long {
37151a7b740SScott Long 
37251a7b740SScott Long 	switch (a->a_name) {
37351a7b740SScott Long 	case _PC_LINK_MAX:
37451a7b740SScott Long 		*a->a_retval = 65535;
37551a7b740SScott Long 		return (0);
37651a7b740SScott Long 	case _PC_NAME_MAX:
37751a7b740SScott Long 		*a->a_retval = NAME_MAX;
37851a7b740SScott Long 		return (0);
37951a7b740SScott Long 	case _PC_PATH_MAX:
38051a7b740SScott Long 		*a->a_retval = PATH_MAX;
38151a7b740SScott Long 		return (0);
38251a7b740SScott Long 	case _PC_NO_TRUNC:
38351a7b740SScott Long 		*a->a_retval = 1;
38451a7b740SScott Long 		return (0);
38551a7b740SScott Long 	default:
38651a7b740SScott Long 		return (EINVAL);
38751a7b740SScott Long 	}
38851a7b740SScott Long }
38951a7b740SScott Long 
39051a7b740SScott Long static int
39151a7b740SScott Long udf_read(struct vop_read_args *a)
39251a7b740SScott Long {
39351a7b740SScott Long 	struct vnode *vp = a->a_vp;
39451a7b740SScott Long 	struct uio *uio = a->a_uio;
39551a7b740SScott Long 	struct udf_node *node = VTON(vp);
39651a7b740SScott Long 	struct buf *bp;
397c2d6947dSJeroen Ruigrok van der Werven 	uint8_t *data;
39851a7b740SScott Long 	int error = 0;
399d1def83bSScott Long 	int size, fsize, offset;
40051a7b740SScott Long 
40151a7b740SScott Long 	if (uio->uio_offset < 0)
40251a7b740SScott Long 		return (EINVAL);
40351a7b740SScott Long 
40451a7b740SScott Long 	fsize = node->fentry->inf_len;
405d1def83bSScott Long 
40651a7b740SScott Long 	while (uio->uio_offset < fsize && uio->uio_resid > 0) {
40751a7b740SScott Long 		offset = uio->uio_offset;
408d1def83bSScott Long 		size = uio->uio_resid;
40951a7b740SScott Long 		error = udf_readatoffset(node, &size, offset, &bp, &data);
41051a7b740SScott Long 		if (error)
41151a7b740SScott Long 			return (error);
412d1def83bSScott Long 		error = uiomove((caddr_t)data, size, uio);
41351a7b740SScott Long 		if (bp != NULL)
41451a7b740SScott Long 			brelse(bp);
41551a7b740SScott Long 		if (error)
41651a7b740SScott Long 			break;
41751a7b740SScott Long 	};
41851a7b740SScott Long 
41951a7b740SScott Long 	return (error);
42051a7b740SScott Long }
42151a7b740SScott Long 
42251a7b740SScott Long /* Convienience routine to dump a block in hex */
42351a7b740SScott Long static void
42451a7b740SScott Long udf_dumpblock(void *data, int len)
42551a7b740SScott Long {
42651a7b740SScott Long 	int i, j;
42751a7b740SScott Long 
42851a7b740SScott Long 	for (i = 0; i < len; i++) {
42951a7b740SScott Long 		printf("\noffset= %d: ", i);
43051a7b740SScott Long 		for (j = 0; j < 8; j++) {
43151a7b740SScott Long 			if (i + j == len)
43251a7b740SScott Long 				break;
433c2d6947dSJeroen Ruigrok van der Werven 			printf("0x%02x ", (uint8_t)((uint8_t*)(data))[i + j]);
43451a7b740SScott Long 		}
43551a7b740SScott Long 		i += j - 1;
43651a7b740SScott Long 	}
43751a7b740SScott Long 	printf("\n");
43851a7b740SScott Long }
43951a7b740SScott Long 
44051a7b740SScott Long /*
44151a7b740SScott Long  * Call the OSTA routines to translate the name from a CS0 dstring to a
44251a7b740SScott Long  * 16-bit Unicode String.  Hooks need to be placed in here to translate from
44351a7b740SScott Long  * Unicode to the encoding that the kernel/user expects.  For now, compact
44451a7b740SScott Long  * the encoding to 8 bits if possible.  Return the length of the translated
44551a7b740SScott Long  * string.
44651a7b740SScott Long  * XXX This horribly pessimizes the 8bit case
44751a7b740SScott Long  */
44851a7b740SScott Long static int
44951a7b740SScott Long udf_transname(char *cs0string, char *destname, int len)
45051a7b740SScott Long {
45151a7b740SScott Long 	unicode_t *transname;
45251a7b740SScott Long 	int i, unilen = 0;
45351a7b740SScott Long 
45451a7b740SScott Long 	/* allocate a buffer big enough to hold an 8->16 bit expansion */
45551a7b740SScott Long 	transname = uma_zalloc(udf_zone_trans, M_WAITOK);
45651a7b740SScott Long 	if (transname == NULL) {
45751a7b740SScott Long 		printf("udf: out of memory?\n");
45851a7b740SScott Long 		return 0;
45951a7b740SScott Long 	}
46051a7b740SScott Long 
46151a7b740SScott Long 	if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) {
46251a7b740SScott Long 		printf("udf: Unicode translation failed\n");
46351a7b740SScott Long 		uma_zfree(udf_zone_trans, transname);
46451a7b740SScott Long 		return 0;
46551a7b740SScott Long 	}
46651a7b740SScott Long 
46751a7b740SScott Long 	/* At this point, the name is in 16-bit Unicode.  Compact it down
46851a7b740SScott Long  	 * to 8-bit
46951a7b740SScott Long 	 */
47051a7b740SScott Long 	for (i = 0; i < unilen ; i++) {
47151a7b740SScott Long 		if (transname[i] & 0xff00) {
47251a7b740SScott Long 			destname[i] = '.';	/* Fudge the 16bit chars */
47351a7b740SScott Long 		} else {
47451a7b740SScott Long 			destname[i] = transname[i] & 0xff;
47551a7b740SScott Long 		}
47651a7b740SScott Long 	}
47751a7b740SScott Long 
47851a7b740SScott Long 	destname[unilen] = 0;
47951a7b740SScott Long 	uma_zfree(udf_zone_trans, transname);
48051a7b740SScott Long 
48151a7b740SScott Long 	return unilen;
48251a7b740SScott Long }
48351a7b740SScott Long 
48451a7b740SScott Long /*
48551a7b740SScott Long  * Compare a CS0 dstring with a name passed in from the VFS layer.  Return
48651a7b740SScott Long  * 0 on a successful match, nonzero therwise.  Unicode work may need to be done
48751a7b740SScott Long  * here also.
48851a7b740SScott Long  */
48951a7b740SScott Long static int
49051a7b740SScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen)
49151a7b740SScott Long {
49251a7b740SScott Long 	char transname[MAXNAMLEN+1]; /* XXX stack */
49351a7b740SScott Long 
49451a7b740SScott Long 	if ((cs0len = udf_transname(cs0string, &transname[0], cs0len)) == 0)
49551a7b740SScott Long 		return -1;
49651a7b740SScott Long 
49751a7b740SScott Long 	/* Easy check.  If they aren't the same length, they aren't equal */
49851a7b740SScott Long 	if (cs0len != cmplen)
49951a7b740SScott Long 		return -1;
50051a7b740SScott Long 
50151a7b740SScott Long 	return (bcmp(transname, cmpname, cmplen));
50251a7b740SScott Long }
50351a7b740SScott Long 
50451a7b740SScott Long struct udf_uiodir {
50551a7b740SScott Long 	struct dirent *dirent;
50651a7b740SScott Long 	u_long *cookies;
50751a7b740SScott Long 	int ncookies;
50851a7b740SScott Long 	int acookies;
50951a7b740SScott Long 	int eofflag;
51051a7b740SScott Long };
51151a7b740SScott Long 
51251a7b740SScott Long static int
51351a7b740SScott Long udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
51451a7b740SScott Long {
51551a7b740SScott Long 	if (uiodir->cookies != NULL) {
51651a7b740SScott Long 		if (++uiodir->acookies > uiodir->ncookies) {
51751a7b740SScott Long 			uiodir->eofflag = 0;
51851a7b740SScott Long 			return (-1);
51951a7b740SScott Long 		}
52051a7b740SScott Long 		*uiodir->cookies++ = cookie;
52151a7b740SScott Long 	}
52251a7b740SScott Long 
52351a7b740SScott Long 	if (uio->uio_resid < de_size) {
52451a7b740SScott Long 		uiodir->eofflag = 0;
52551a7b740SScott Long 		return (-1);
52651a7b740SScott Long 	}
52751a7b740SScott Long 
52851a7b740SScott Long 	return (uiomove((caddr_t)uiodir->dirent, de_size, uio));
52951a7b740SScott Long }
53051a7b740SScott Long 
53151a7b740SScott Long /* Prebuild the . and .. dirents.  d_fileno will need to be filled in */
53251a7b740SScott Long static struct dirent udf_de_dot =
53351a7b740SScott Long 	{ 0, sizeof(struct dirent), DT_DIR, 1, "." };
53451a7b740SScott Long static struct dirent udf_de_dotdot =
53551a7b740SScott Long 	{ 0, sizeof(struct dirent), DT_DIR, 2, ".." };
53651a7b740SScott Long 
53751a7b740SScott Long static int
53851a7b740SScott Long udf_readdir(struct vop_readdir_args *a)
53951a7b740SScott Long {
54051a7b740SScott Long 	struct vnode *vp;
54151a7b740SScott Long 	struct buf *bp;
54251a7b740SScott Long 	struct uio *uio;
54351a7b740SScott Long 	struct dirent dir;
54451a7b740SScott Long 	struct udf_node *node;
54551a7b740SScott Long 	struct udf_mnt *udfmp;
54651a7b740SScott Long 	struct fileid_desc *fid;
54751a7b740SScott Long 	struct udf_uiodir uiodir;
54851a7b740SScott Long 	u_long *cookies = NULL;
549c2d6947dSJeroen Ruigrok van der Werven 	uint8_t *data;
55051a7b740SScott Long 	int ncookies;
55151a7b740SScott Long 	int error = 0, offset, off, size, de_size, fid_size, fsize;
55251a7b740SScott Long 	int total_fid_size = 0, frag_size = 0, fid_fragment = 0;
55351a7b740SScott Long 
55451a7b740SScott Long 	vp = a->a_vp;
55551a7b740SScott Long 	uio = a->a_uio;
55651a7b740SScott Long 	node = VTON(vp);
55751a7b740SScott Long 	udfmp = node->udfmp;
55851a7b740SScott Long 	de_size = sizeof(struct dirent);
55951a7b740SScott Long 	fid_size = UDF_FID_SIZE;
56051a7b740SScott Long 	fsize = node->fentry->inf_len;
56151a7b740SScott Long 	uiodir.eofflag = 1;
56251a7b740SScott Long 
56351a7b740SScott Long 	if (a->a_ncookies != NULL) {
56451a7b740SScott Long 		/*
56551a7b740SScott Long 		 * Guess how many entries are needed.  If we run out, this
56651a7b740SScott Long 		 * function will be called again and thing will pick up were
56751a7b740SScott Long 		 * it left off.
56851a7b740SScott Long 		 */
56951a7b740SScott Long 		ncookies = uio->uio_resid / 8;
57051a7b740SScott Long 		MALLOC(cookies, u_long *, sizeof(u_long) * ncookies,
57151a7b740SScott Long 		    M_TEMP, M_WAITOK);
57251a7b740SScott Long 		if (cookies == NULL)
57351a7b740SScott Long 			return (ENOMEM);
57451a7b740SScott Long 		uiodir.ncookies = ncookies;
57551a7b740SScott Long 		uiodir.cookies = cookies;
57651a7b740SScott Long 		uiodir.acookies = 0;
57751a7b740SScott Long 	} else {
57851a7b740SScott Long 		uiodir.cookies = NULL;
57951a7b740SScott Long 	}
58051a7b740SScott Long 
58151a7b740SScott Long 	/*
58251a7b740SScott Long 	 * offset is the absolute offset into the file data. off is the offset
58351a7b740SScott Long 	 * into the data, minus the blocks that weren't read because they fell
58451a7b740SScott Long 	 * before offset.
58551a7b740SScott Long 	 */
58651a7b740SScott Long 	offset = uio->uio_offset;
58751a7b740SScott Long 	off = 0;
58851a7b740SScott Long 
58951a7b740SScott Long 	/*
59051a7b740SScott Long 	 * Iterate through the file id descriptors.  Give the parent dir
59151a7b740SScott Long 	 * entry special attention.  size will be the size of the extent
59251a7b740SScott Long 	 * returned in data.  If there is more than one extent, things get
59351a7b740SScott Long 	 * ugly.
59451a7b740SScott Long 	 */
59551a7b740SScott Long 	size = 0;
59651a7b740SScott Long 	error = udf_readatoffset(node, &size, offset, &bp, &data);
59751a7b740SScott Long 	if (error) {
59851a7b740SScott Long 		if (a->a_ncookies != NULL)
59951a7b740SScott Long 			FREE(cookies, M_TEMP);
60051a7b740SScott Long 		return (error);
60151a7b740SScott Long 	}
60251a7b740SScott Long 
60351a7b740SScott Long 	while (offset + off < fsize) {
60451a7b740SScott Long 
60551a7b740SScott Long 		fid = (struct fileid_desc*)&data[off];
60651a7b740SScott Long 
607d1def83bSScott Long 		/*
608d1def83bSScott Long 		 * Check to see if the fid is fragmented. The first test
609d1def83bSScott Long 		 * ensures that we don't wander off the end of the buffer
610d1def83bSScott Long 		 * looking for the l_iu and l_fi fields.
611d1def83bSScott Long 		 */
612d1def83bSScott Long 		if (off + fid_size > size ||
61351a7b740SScott Long 		    off + fid->l_iu + fid->l_fi + fid_size > size) {
61451a7b740SScott Long 			struct fileid_desc *fid_buf;
615c2d6947dSJeroen Ruigrok van der Werven 			uint8_t *buf;
61651a7b740SScott Long 
61751a7b740SScott Long 			/* Copy what we have of the fid into a buffer */
61851a7b740SScott Long 			frag_size = size - off;
619c2d6947dSJeroen Ruigrok van der Werven 			MALLOC(buf, uint8_t*, max(frag_size, fid_size),
62051a7b740SScott Long 			    M_UDFFID, M_NOWAIT | M_ZERO);
62151a7b740SScott Long 			if (buf == NULL)
62251a7b740SScott Long 				panic("No memory?");
62351a7b740SScott Long 			bcopy(fid, buf, frag_size);
62451a7b740SScott Long 
62551a7b740SScott Long 			/* Reduce all of the casting magic */
62651a7b740SScott Long 			fid_buf = (struct fileid_desc*)buf;
62751a7b740SScott Long 
62851a7b740SScott Long 			if (bp != NULL)
62951a7b740SScott Long 				brelse(bp);
63051a7b740SScott Long 
63151a7b740SScott Long 			/* Fetch the next allocation */
63251a7b740SScott Long 			offset += size;
63351a7b740SScott Long 			size = 0;
63451a7b740SScott Long 			error = udf_readatoffset(node, &size, offset, &bp,
63551a7b740SScott Long 			    &data);
63651a7b740SScott Long 			if (error)
63751a7b740SScott Long 				break;
63851a7b740SScott Long 
63951a7b740SScott Long 			/*
64051a7b740SScott Long 			 * If the fragment was so small that we didn't get
64151a7b740SScott Long 			 * the l_iu and l_fi fields, copy those in.
64251a7b740SScott Long 			 */
64351a7b740SScott Long 			if (fid_size > frag_size)
64451a7b740SScott Long 				bcopy(data, &buf[frag_size],
64551a7b740SScott Long 				    fid_size - frag_size);
64651a7b740SScott Long 
64751a7b740SScott Long 			/*
64851a7b740SScott Long 			 * Now that we have enough of the fid to work with,
64951a7b740SScott Long 			 * allocate a new fid, copy the fragment into it,
65051a7b740SScott Long 			 * and copy the rest of the fid from the new
65151a7b740SScott Long 			 * allocation.
65251a7b740SScott Long 			 */
65351a7b740SScott Long 			total_fid_size = fid_size + fid_buf->l_iu +
65451a7b740SScott Long 			    fid_buf->l_fi;
65551a7b740SScott Long 			MALLOC(fid, struct fileid_desc *, total_fid_size,
65651a7b740SScott Long 			    M_UDFFID, M_NOWAIT | M_ZERO);
65751a7b740SScott Long 			if (fid == NULL) {
658d1def83bSScott Long 				if (bp != NULL)
65951a7b740SScott Long 					brelse(bp);
66051a7b740SScott Long 				error = ENOMEM;
66151a7b740SScott Long 				break;
66251a7b740SScott Long 			}
66351a7b740SScott Long 			bcopy(fid_buf, fid, frag_size);
664c2d6947dSJeroen Ruigrok van der Werven 			bcopy(data, &((uint8_t*)(fid))[frag_size],
66551a7b740SScott Long 			    total_fid_size - frag_size);
66651a7b740SScott Long 
66751a7b740SScott Long 			fid_fragment = 1;
66851a7b740SScott Long 			FREE(buf, M_UDFFID);
66951a7b740SScott Long 		} else {
67051a7b740SScott Long 			total_fid_size = fid->l_iu + fid->l_fi + fid_size;
67151a7b740SScott Long 		}
67251a7b740SScott Long 
67351a7b740SScott Long 		/* XXX Should we return an error on a bad fid? */
67451a7b740SScott Long 		if (udf_checktag(&fid->tag, TAGID_FID)) {
67551a7b740SScott Long 			printf("Invalid FID tag\n");
67651a7b740SScott Long 			break;
67751a7b740SScott Long 		}
67851a7b740SScott Long 
67951a7b740SScott Long 		/* Is this a deleted file? */
68051a7b740SScott Long 		if (fid->file_char & 0x4)
68151a7b740SScott Long 			goto update_offset;
68251a7b740SScott Long 
68351a7b740SScott Long 		if (fid->l_iu != 0) {
68451a7b740SScott Long 			printf("Possibly invalid fid found.\n");
68551a7b740SScott Long 			goto update_offset;
68651a7b740SScott Long 		}
68751a7b740SScott Long 
68851a7b740SScott Long 		if ((fid->l_fi == 0) && (fid->file_char & 0x08)) {
68951a7b740SScott Long 			/* Do up the '.' and '..' entries.  Dummy values are
69051a7b740SScott Long 			 * used for the cookies since the offset here is
69151a7b740SScott Long 			 * usually zero, and NFS doesn't like that value
69251a7b740SScott Long 			 * XXX Should the magic dirents be locked?
69351a7b740SScott Long 			 */
69451a7b740SScott Long 			udf_de_dot.d_fileno = node->hash_id;
69551a7b740SScott Long 			uiodir.dirent = &udf_de_dot;
69651a7b740SScott Long 			error = udf_uiodir(&uiodir, de_size, uio, 1);
69751a7b740SScott Long 			if (error)
69851a7b740SScott Long 				break;
69951a7b740SScott Long 
70051a7b740SScott Long 			udf_de_dotdot.d_fileno = udf_getid(&fid->icb);
70151a7b740SScott Long 			uiodir.dirent = &udf_de_dotdot;
70251a7b740SScott Long 			error = udf_uiodir(&uiodir, de_size, uio, 2);
70351a7b740SScott Long 		} else {
70451a7b740SScott Long 			dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
70551a7b740SScott Long 			    &dir.d_name[0], fid->l_fi);
70651a7b740SScott Long 			dir.d_fileno = udf_getid(&fid->icb);
70751a7b740SScott Long 			dir.d_type = (fid->file_char & 0x02) ? DT_DIR :
70851a7b740SScott Long 			    DT_UNKNOWN;
70951a7b740SScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
71051a7b740SScott Long 			uiodir.dirent = &dir;
71151a7b740SScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, off);
71251a7b740SScott Long 		}
71351a7b740SScott Long 		if (error) {
71451a7b740SScott Long 			printf("uiomove returned %d\n", error);
71551a7b740SScott Long 			break;
71651a7b740SScott Long 		}
71751a7b740SScott Long 
71851a7b740SScott Long update_offset:	/*
71951a7b740SScott Long 		 * Update the offset. Align on a 4 byte boundary because the
72051a7b740SScott Long 		 * UDF spec says so.  If it was a fragmented entry, clean up.
72151a7b740SScott Long 		 */
72251a7b740SScott Long 		if (fid_fragment) {
72351a7b740SScott Long 			off = (total_fid_size - frag_size + 3) & ~0x03;
72451a7b740SScott Long 			FREE(fid, M_UDFFID);
72551a7b740SScott Long 			fid_fragment = 0;
72651a7b740SScott Long 		} else {
72751a7b740SScott Long 			off += (total_fid_size + 3) & ~0x03;
72851a7b740SScott Long 		}
72951a7b740SScott Long 	}
73051a7b740SScott Long 
73151a7b740SScott Long 	/* tell the calling layer whether we need to be called again */
73251a7b740SScott Long 	*a->a_eofflag = uiodir.eofflag;
73351a7b740SScott Long 	uio->uio_offset = offset + off;
73451a7b740SScott Long 
73551a7b740SScott Long 	if (bp != NULL)
73651a7b740SScott Long 		brelse(bp);
73751a7b740SScott Long 
73851a7b740SScott Long 	if (a->a_ncookies != NULL) {
73951a7b740SScott Long 		if (error)
74051a7b740SScott Long 			free(cookies, M_TEMP);
74151a7b740SScott Long 		else {
74251a7b740SScott Long 			*a->a_ncookies = uiodir.acookies;
74351a7b740SScott Long 			*a->a_cookies = cookies;
74451a7b740SScott Long 		}
74551a7b740SScott Long 	}
74651a7b740SScott Long 
74751a7b740SScott Long 	return (error);
74851a7b740SScott Long }
74951a7b740SScott Long 
75051a7b740SScott Long /* Are there any implementations out there that do soft-links? */
75151a7b740SScott Long static int
75251a7b740SScott Long udf_readlink(struct vop_readlink_args *ap)
75351a7b740SScott Long {
75451a7b740SScott Long 	printf("%s called\n", __FUNCTION__);
75551a7b740SScott Long 	return (EOPNOTSUPP);
75651a7b740SScott Long }
75751a7b740SScott Long 
75851a7b740SScott Long static int
75951a7b740SScott Long udf_strategy(struct vop_strategy_args *a)
76051a7b740SScott Long {
76151a7b740SScott Long 	struct buf *bp;
76251a7b740SScott Long 	struct vnode *vp;
76351a7b740SScott Long 	struct udf_node *node;
76451a7b740SScott Long 	int maxsize;
76551a7b740SScott Long 
76651a7b740SScott Long 	bp = a->a_bp;
76751a7b740SScott Long 	vp = bp->b_vp;
76851a7b740SScott Long 	node = VTON(vp);
76951a7b740SScott Long 
77051a7b740SScott Long 	/* cd9660 has this test reversed, but it seems more logical this way */
77151a7b740SScott Long 	if (bp->b_blkno != bp->b_lblkno) {
77251a7b740SScott Long 		/*
77351a7b740SScott Long 		 * Files that are embedded in the fentry don't translate well
77451a7b740SScott Long 		 * to a block number.  Reject.
77551a7b740SScott Long 		 */
77651a7b740SScott Long 		if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize,
77751a7b740SScott Long 		    &bp->b_lblkno, &maxsize)) {
77851a7b740SScott Long 			clrbuf(bp);
77951a7b740SScott Long 			bp->b_blkno = -1;
78051a7b740SScott Long 		}
78151a7b740SScott Long 	}
78251a7b740SScott Long 	if ((long)bp->b_blkno == -1) {
78351a7b740SScott Long 		bufdone(bp);
78451a7b740SScott Long 		return (0);
78551a7b740SScott Long 	}
78651a7b740SScott Long 	vp = node->i_devvp;
78751a7b740SScott Long 	bp->b_dev = vp->v_rdev;
78851a7b740SScott Long 	VOP_STRATEGY(vp, bp);
78951a7b740SScott Long 	return (0);
79051a7b740SScott Long }
79151a7b740SScott Long 
79251a7b740SScott Long static int
79351a7b740SScott Long udf_print(struct vop_print_args *a)
79451a7b740SScott Long {
79551a7b740SScott Long 	printf("%s called\n", __FUNCTION__);
79651a7b740SScott Long 	return (EOPNOTSUPP);
79751a7b740SScott Long }
79851a7b740SScott Long 
79951a7b740SScott Long static int
80051a7b740SScott Long udf_bmap(struct vop_bmap_args *a)
80151a7b740SScott Long {
80251a7b740SScott Long 	struct udf_node *node;
803c2d6947dSJeroen Ruigrok van der Werven 	uint32_t max_size;
80498b0c789SPoul-Henning Kamp 	daddr_t lsector;
80551a7b740SScott Long 	int error;
80651a7b740SScott Long 
80751a7b740SScott Long 	node = VTON(a->a_vp);
80851a7b740SScott Long 
80951a7b740SScott Long 	if (a->a_vpp != NULL)
81051a7b740SScott Long 		*a->a_vpp = node->i_devvp;
81151a7b740SScott Long 	if (a->a_bnp == NULL)
81251a7b740SScott Long 		return (0);
81351a7b740SScott Long 	if (a->a_runb)
81451a7b740SScott Long 		*a->a_runb = 0;
81551a7b740SScott Long 
816cd1b1a1dSScott Long 	error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector,
81751a7b740SScott Long 	    &max_size);
8188db4c2f2SScott Long 	if (error)
81951a7b740SScott Long 		return (error);
82051a7b740SScott Long 
821cd1b1a1dSScott Long 	/* Translate logical to physical sector number */
822cd1b1a1dSScott Long 	*a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
823cd1b1a1dSScott Long 
82451a7b740SScott Long 	/* Punt on read-ahead for now */
82551a7b740SScott Long 	if (a->a_runp)
82651a7b740SScott Long 		*a->a_runp = 0;
82751a7b740SScott Long 
82851a7b740SScott Long 	return (0);
82951a7b740SScott Long }
83051a7b740SScott Long 
83151a7b740SScott Long /*
83251a7b740SScott Long  * The all powerful VOP_LOOKUP().
83351a7b740SScott Long  */
83451a7b740SScott Long static int
83551a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a)
83651a7b740SScott Long {
83751a7b740SScott Long 	struct vnode *dvp;
83851a7b740SScott Long 	struct vnode *tdp = NULL;
83951a7b740SScott Long 	struct vnode **vpp = a->a_vpp;
84051a7b740SScott Long 	struct buf *bp = NULL;
84151a7b740SScott Long 	struct udf_node *node;
84251a7b740SScott Long 	struct udf_mnt *udfmp;
84351a7b740SScott Long 	struct fileid_desc *fid = NULL;
84451a7b740SScott Long 	struct thread *td;
84551a7b740SScott Long 	u_long nameiop;
84651a7b740SScott Long 	u_long flags;
84751a7b740SScott Long 	char *nameptr;
84851a7b740SScott Long 	long namelen;
84951a7b740SScott Long 	ino_t id = 0;
850c2d6947dSJeroen Ruigrok van der Werven 	uint8_t *data;
85151a7b740SScott Long 	int offset, off, error, size;
85251a7b740SScott Long 	int numdirpasses, fid_size, fsize, icb_len;
85351a7b740SScott Long 	int total_fid_size = 0, fid_fragment = 0;
85451a7b740SScott Long 
85551a7b740SScott Long 	dvp = a->a_dvp;
85651a7b740SScott Long 	node = VTON(dvp);
85751a7b740SScott Long 	udfmp = node->udfmp;
85851a7b740SScott Long 	nameiop = a->a_cnp->cn_nameiop;
85951a7b740SScott Long 	flags = a->a_cnp->cn_flags;
86051a7b740SScott Long 	nameptr = a->a_cnp->cn_nameptr;
86151a7b740SScott Long 	namelen = a->a_cnp->cn_namelen;
86251a7b740SScott Long 	fid_size = UDF_FID_SIZE;
86351a7b740SScott Long 	fsize = node->fentry->inf_len;
86451a7b740SScott Long 	icb_len = sizeof(struct long_ad);
86551a7b740SScott Long 	td = a->a_cnp->cn_thread;
86651a7b740SScott Long 
86751a7b740SScott Long 	/*
86851a7b740SScott Long 	 * If this is a LOOKUP and we've already partially searched through
86951a7b740SScott Long 	 * the directory, pick up where we left off and flag that the
87051a7b740SScott Long 	 * directory may need to be searched twice.  For a full description,
87151a7b740SScott Long 	 * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup()
87251a7b740SScott Long 	 */
87351a7b740SScott Long 	if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > size) {
87451a7b740SScott Long 		offset = 0;
87551a7b740SScott Long 		numdirpasses = 1;
87651a7b740SScott Long 	} else {
87751a7b740SScott Long 		offset = node->diroff;
87851a7b740SScott Long 		numdirpasses = 2;
87951a7b740SScott Long 		nchstats.ncs_2passes++;
88051a7b740SScott Long 	}
88151a7b740SScott Long 
88251a7b740SScott Long 	/*
88351a7b740SScott Long 	 * The name lookup algorithm is quite similar to what is in readdir.
88451a7b740SScott Long 	 * Can this be broken out and shared?
88551a7b740SScott Long 	 */
88651a7b740SScott Long lookloop:
88751a7b740SScott Long 	size = 0;
88851a7b740SScott Long 	off = 0;
88951a7b740SScott Long 	error = udf_readatoffset(node, &size, offset, &bp, &data);
89051a7b740SScott Long 	if (error)
89151a7b740SScott Long 		return (error);
89251a7b740SScott Long 
89351a7b740SScott Long 	while (offset + off < fsize) {
89451a7b740SScott Long 		fid = (struct fileid_desc*)&data[off];
89551a7b740SScott Long 
896d1def83bSScott Long 		/*
897d1def83bSScott Long 		 * Check to see if the fid is fragmented. The first test
898d1def83bSScott Long 		 * ensures that we don't wander off the end of the buffer
899d1def83bSScott Long 		 * looking for the l_iu and l_fi fields.
900d1def83bSScott Long 		 */
901d1def83bSScott Long 		if (off + fid_size > size ||
90251a7b740SScott Long 		    off + fid_size + fid->l_iu + fid->l_fi > size) {
90351a7b740SScott Long 			struct fileid_desc *fid_buf;
904c2d6947dSJeroen Ruigrok van der Werven 			uint8_t *buf;
90551a7b740SScott Long 			int frag_size = 0;
90651a7b740SScott Long 
90751a7b740SScott Long 			/* Copy what we have of the fid into a buffer */
90851a7b740SScott Long 			frag_size = size - off;
909c2d6947dSJeroen Ruigrok van der Werven 			MALLOC(buf, uint8_t*, max(frag_size, fid_size),
91051a7b740SScott Long 			    M_UDFFID, M_NOWAIT | M_ZERO);
91151a7b740SScott Long 			if (buf == NULL)
91251a7b740SScott Long 				panic("No memory?");
91351a7b740SScott Long 			bcopy(fid, buf, frag_size);
91451a7b740SScott Long 
91551a7b740SScott Long 			/* Reduce all of the casting magic */
91651a7b740SScott Long 			fid_buf = (struct fileid_desc*)buf;
91751a7b740SScott Long 
91851a7b740SScott Long 			if (bp != NULL)
91951a7b740SScott Long 				brelse(bp);
92051a7b740SScott Long 
92151a7b740SScott Long 			/* Fetch the next allocation */
92251a7b740SScott Long 			offset += size;
92351a7b740SScott Long 			size = 0;
92451a7b740SScott Long 			error = udf_readatoffset(node, &size, offset, &bp,
92551a7b740SScott Long 			    &data);
92651a7b740SScott Long 			if (error)
92751a7b740SScott Long 				return (error);
92851a7b740SScott Long 
92951a7b740SScott Long 			/*
93051a7b740SScott Long 			 * If the fragment was so small that we didn't get
93151a7b740SScott Long 			 * the l_iu and l_fi fields, copy those in.
93251a7b740SScott Long 			 */
93351a7b740SScott Long 			if (fid_size > frag_size)
93451a7b740SScott Long 				bcopy(data, &buf[frag_size],
93551a7b740SScott Long 				    fid_size - frag_size);
93651a7b740SScott Long 
93751a7b740SScott Long 			/*
93851a7b740SScott Long 			 * Now that we have enough of the fid to work with,
93951a7b740SScott Long 			 * allocate a new fid, copy the fragment into it,
94051a7b740SScott Long 			 * and copy the rest of the fid from the new
94151a7b740SScott Long 			 * allocation.
94251a7b740SScott Long 			 */
94351a7b740SScott Long 			total_fid_size = fid_size + fid_buf->l_iu +
94451a7b740SScott Long 			    fid_buf->l_fi;
94551a7b740SScott Long 			MALLOC(fid, struct fileid_desc *, total_fid_size,
94651a7b740SScott Long 			    M_UDFFID, M_NOWAIT | M_ZERO);
94751a7b740SScott Long 			if (fid == NULL) {
948d1def83bSScott Long 				if (bp != NULL)
94951a7b740SScott Long 					brelse(bp);
95051a7b740SScott Long 				return (ENOMEM);
95151a7b740SScott Long 			}
95251a7b740SScott Long 			bcopy(fid_buf, fid, frag_size);
953c2d6947dSJeroen Ruigrok van der Werven 			bcopy(data, &((uint8_t*)(fid))[frag_size],
95451a7b740SScott Long 			    total_fid_size - frag_size);
95551a7b740SScott Long 
95651a7b740SScott Long 			off = (total_fid_size - frag_size + 3) & ~0x03;
95751a7b740SScott Long 			fid_fragment = 1;
95851a7b740SScott Long 			FREE(buf, M_UDFFID);
95951a7b740SScott Long 		} else {
96051a7b740SScott Long 			/*
96151a7b740SScott Long 			 * Update the offset here to avoid looking at this fid
96251a7b740SScott Long 			 * again on a subsequent lookup.
96351a7b740SScott Long 			 */
96451a7b740SScott Long 			total_fid_size = fid->l_iu + fid->l_fi + fid_size;
96551a7b740SScott Long 			off += (total_fid_size + 3) & ~0x03;
96651a7b740SScott Long 		}
96751a7b740SScott Long 
96851a7b740SScott Long 		/* XXX Should we return an error on a bad fid? */
96951a7b740SScott Long 		if (udf_checktag(&fid->tag, TAGID_FID))
970678d5effSScott Long 			goto continue_lookup;
971678d5effSScott Long 
972678d5effSScott Long 		/* Is this a deleted file? */
973678d5effSScott Long 		if (fid->file_char & 0x4)
974678d5effSScott Long 			goto continue_lookup;
97551a7b740SScott Long 
97651a7b740SScott Long 		if ((fid->l_fi == 0) && (fid->file_char & 0x08)) {
97751a7b740SScott Long 			if (flags & ISDOTDOT) {
97851a7b740SScott Long 				id = udf_getid(&fid->icb);
97951a7b740SScott Long 				break;
98051a7b740SScott Long 			}
98151a7b740SScott Long 		} else {
98251a7b740SScott Long 			if (!(udf_cmpname(&fid->data[fid->l_iu],
98351a7b740SScott Long 			    nameptr, fid->l_fi, namelen))) {
98451a7b740SScott Long 				id = udf_getid(&fid->icb);
98551a7b740SScott Long 				break;
98651a7b740SScott Long 			}
98751a7b740SScott Long 		}
98851a7b740SScott Long 
98951a7b740SScott Long 		/*
99051a7b740SScott Long 		 * If we got this far then this fid isn't what we were
99151a7b740SScott Long 		 * looking for.  It's therefore safe to clean up from a
99251a7b740SScott Long 		 * fragmented fid.
99351a7b740SScott Long 		 */
994678d5effSScott Long continue_lookup:
99551a7b740SScott Long 		if (fid_fragment) {
99651a7b740SScott Long 			FREE(fid, M_UDFFID);
99751a7b740SScott Long 			fid_fragment = 0;
99851a7b740SScott Long 		}
99951a7b740SScott Long 	}
100051a7b740SScott Long 
100151a7b740SScott Long 	/* Did we have a match? */
100251a7b740SScott Long 	if (id) {
100351a7b740SScott Long 		error = udf_vget(udfmp->im_mountp, id, LK_EXCLUSIVE, &tdp);
100451a7b740SScott Long 		if (bp != NULL)
100551a7b740SScott Long 			brelse(bp);
100651a7b740SScott Long 		if (error)
100751a7b740SScott Long 			return (error);
100851a7b740SScott Long 
100951a7b740SScott Long 		/* Remember where this entry was if it's the final component */
101051a7b740SScott Long 		if ((flags & ISLASTCN) && nameiop == LOOKUP)
101151a7b740SScott Long 			node->diroff = offset + off;
101251a7b740SScott Long 		if (numdirpasses == 2)
101351a7b740SScott Long 			nchstats.ncs_pass2++;
101451a7b740SScott Long 		if (!(flags & LOCKPARENT) || !(flags & ISLASTCN)) {
101551a7b740SScott Long 			a->a_cnp->cn_flags |= PDIRUNLOCK;
101651a7b740SScott Long 			VOP_UNLOCK(dvp, 0, td);
101751a7b740SScott Long 		}
101851a7b740SScott Long 
101951a7b740SScott Long 		*vpp = tdp;
102051a7b740SScott Long 
102151a7b740SScott Long 		/* Put this entry in the cache */
102251a7b740SScott Long 		if (flags & MAKEENTRY)
102351a7b740SScott Long 			cache_enter(dvp, *vpp, a->a_cnp);
102451a7b740SScott Long 
102551a7b740SScott Long 		if (fid_fragment)
102651a7b740SScott Long 			FREE(fid, M_UDFFID);
102751a7b740SScott Long 
102851a7b740SScott Long 		return (0);
102951a7b740SScott Long 	}
103051a7b740SScott Long 
103151a7b740SScott Long 	/* Name wasn't found on this pass.  Do another pass? */
103251a7b740SScott Long 	if (numdirpasses == 2) {
103351a7b740SScott Long 		numdirpasses--;
103451a7b740SScott Long 		offset = 0;
103551a7b740SScott Long 		goto lookloop;
103651a7b740SScott Long 	}
103751a7b740SScott Long 
103851a7b740SScott Long 	if (bp != NULL)
103951a7b740SScott Long 		brelse(bp);
104051a7b740SScott Long 
104151a7b740SScott Long 	/* Enter name into cache as non-existant */
104251a7b740SScott Long 	if (flags & MAKEENTRY)
104351a7b740SScott Long 		cache_enter(dvp, *vpp, a->a_cnp);
104451a7b740SScott Long 
1045fc6f338fSMaxime Henrion 	if ((flags & ISLASTCN) && (nameiop == CREATE || nameiop == RENAME))
104651a7b740SScott Long 		return (EROFS);
104751a7b740SScott Long 	return (ENOENT);
104851a7b740SScott Long 
104951a7b740SScott Long }
105051a7b740SScott Long 
105151a7b740SScott Long static int
105251a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a)
105351a7b740SScott Long {
105451a7b740SScott Long 	struct vnode *vp;
105551a7b740SScott Long 	struct udf_node *unode;
105651a7b740SScott Long 
105751a7b740SScott Long 	vp = a->a_vp;
105851a7b740SScott Long 	unode = VTON(vp);
105951a7b740SScott Long 
106051a7b740SScott Long 	cache_purge(vp);
106151a7b740SScott Long 	if (unode != NULL) {
106251a7b740SScott Long 		udf_hashrem(unode);
106351a7b740SScott Long 		if (unode->i_devvp) {
106451a7b740SScott Long 			vrele(unode->i_devvp);
106551a7b740SScott Long 			unode->i_devvp = 0;
106651a7b740SScott Long 		}
106751a7b740SScott Long 
106851a7b740SScott Long 		if (unode->fentry != NULL)
106951a7b740SScott Long 			FREE(unode->fentry, M_UDFFENTRY);
107051a7b740SScott Long 		lockdestroy(&unode->i_vnode->v_lock);
107151a7b740SScott Long 		uma_zfree(udf_zone_node, unode);
107251a7b740SScott Long 		vp->v_data = NULL;
107351a7b740SScott Long 	}
107451a7b740SScott Long 
107551a7b740SScott Long 	return (0);
107651a7b740SScott Long }
107751a7b740SScott Long 
107851a7b740SScott Long /*
107951a7b740SScott Long  * Read the block and then set the data pointer to correspond with the
108051a7b740SScott Long  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
108151a7b740SScott Long  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
108251a7b740SScott Long  * whole extent.
108351a7b740SScott Long  * XXX 'size' is limited to the logical block size for now due to problems
108451a7b740SScott Long  * with udf_read()
108551a7b740SScott Long  */
108651a7b740SScott Long static int
1087c2d6947dSJeroen Ruigrok van der Werven udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp, uint8_t **data)
108851a7b740SScott Long {
108951a7b740SScott Long 	struct udf_mnt *udfmp;
109051a7b740SScott Long 	struct file_entry *fentry = NULL;
109151a7b740SScott Long 	struct buf *bp1;
1092c2d6947dSJeroen Ruigrok van der Werven 	uint32_t max_size;
109398b0c789SPoul-Henning Kamp 	daddr_t sector;
109451a7b740SScott Long 	int error;
109551a7b740SScott Long 
109651a7b740SScott Long 	udfmp = node->udfmp;
109751a7b740SScott Long 
109851a7b740SScott Long 	error = udf_bmap_internal(node, offset, &sector, &max_size);
10998db4c2f2SScott Long 	if (error == INVALID_BMAP) {
110051a7b740SScott Long 		/*
110151a7b740SScott Long 		 * This error means that the file *data* is stored in the
110251a7b740SScott Long 		 * allocation descriptor field of the file entry.
110351a7b740SScott Long 		 */
110451a7b740SScott Long 		fentry = node->fentry;
110551a7b740SScott Long 		*data = &fentry->data[fentry->l_ea];
110651a7b740SScott Long 		*size = fentry->l_ad;
110751a7b740SScott Long 		*bp = NULL;
110851a7b740SScott Long 		return (0);
110951a7b740SScott Long 	} else if (error != 0) {
111051a7b740SScott Long 		return (error);
111151a7b740SScott Long 	}
111251a7b740SScott Long 
1113d1def83bSScott Long 	/* Adjust the size so that it is within range */
111451a7b740SScott Long 	if (*size == 0 || *size > max_size)
111551a7b740SScott Long 		*size = max_size;
1116d1def83bSScott Long 	*size = min(*size, MAXBSIZE);
111751a7b740SScott Long 
111851a7b740SScott Long 	if ((error = udf_readlblks(udfmp, sector, *size, bp))) {
111951a7b740SScott Long 		printf("udf_readlblks returned %d\n", error);
112051a7b740SScott Long 		return (error);
112151a7b740SScott Long 	}
112251a7b740SScott Long 
112351a7b740SScott Long 	bp1 = *bp;
1124c2d6947dSJeroen Ruigrok van der Werven 	*data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize];
112551a7b740SScott Long 	return (0);
112651a7b740SScott Long }
112751a7b740SScott Long 
112851a7b740SScott Long /*
112951a7b740SScott Long  * Translate a file offset into a logical block and then into a physical
113051a7b740SScott Long  * block.
113151a7b740SScott Long  */
113251a7b740SScott Long static int
113398b0c789SPoul-Henning Kamp udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size)
113451a7b740SScott Long {
113551a7b740SScott Long 	struct udf_mnt *udfmp;
113651a7b740SScott Long 	struct file_entry *fentry;
113751a7b740SScott Long 	void *icb;
113851a7b740SScott Long 	struct icb_tag *tag;
1139c2d6947dSJeroen Ruigrok van der Werven 	uint32_t icblen = 0;
114098b0c789SPoul-Henning Kamp 	daddr_t lsector;
114151a7b740SScott Long 	int ad_offset, ad_num = 0;
114251a7b740SScott Long 	int i, p_offset;
114351a7b740SScott Long 
114451a7b740SScott Long 	udfmp = node->udfmp;
114551a7b740SScott Long 	fentry = node->fentry;
114651a7b740SScott Long 	tag = &fentry->icbtag;
114751a7b740SScott Long 
114851a7b740SScott Long 	switch (tag->strat_type) {
114951a7b740SScott Long 	case 4:
115051a7b740SScott Long 		break;
115151a7b740SScott Long 
115251a7b740SScott Long 	case 4096:
115351a7b740SScott Long 		printf("Cannot deal with strategy4096 yet!\n");
115451a7b740SScott Long 		return (ENODEV);
115551a7b740SScott Long 
115651a7b740SScott Long 	default:
115751a7b740SScott Long 		printf("Unknown strategy type %d\n", tag->strat_type);
115851a7b740SScott Long 		return (ENODEV);
115951a7b740SScott Long 	}
116051a7b740SScott Long 
116151a7b740SScott Long 	switch (tag->flags & 0x7) {
116251a7b740SScott Long 	case 0:
116351a7b740SScott Long 		/*
116451a7b740SScott Long 		 * The allocation descriptor field is filled with short_ad's.
116551a7b740SScott Long 		 * If the offset is beyond the current extent, look for the
116651a7b740SScott Long 		 * next extent.
116751a7b740SScott Long 		 */
116851a7b740SScott Long 		do {
116951a7b740SScott Long 			offset -= icblen;
117051a7b740SScott Long 			ad_offset = sizeof(struct short_ad) * ad_num;
117151a7b740SScott Long 			if (ad_offset > fentry->l_ad) {
117251a7b740SScott Long 				printf("File offset out of bounds\n");
117351a7b740SScott Long 				return (EINVAL);
117451a7b740SScott Long 			}
117551a7b740SScott Long 			icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
117651a7b740SScott Long 			icblen = GETICBLEN(short_ad, icb);
117751a7b740SScott Long 			ad_num++;
117851a7b740SScott Long 		} while(offset >= icblen);
117951a7b740SScott Long 
118051a7b740SScott Long 		lsector = (offset  >> udfmp->bshift) +
118151a7b740SScott Long 		    ((struct short_ad *)(icb))->pos;
118251a7b740SScott Long 
118351a7b740SScott Long 		*max_size = GETICBLEN(short_ad, icb) - offset;
118451a7b740SScott Long 
118551a7b740SScott Long 		break;
118651a7b740SScott Long 	case 1:
118751a7b740SScott Long 		/*
118851a7b740SScott Long 		 * The allocation descriptor field is filled with long_ad's
118951a7b740SScott Long 		 * If the offset is beyond the current extent, look for the
119051a7b740SScott Long 		 * next extent.
119151a7b740SScott Long 		 */
119251a7b740SScott Long 		do {
119351a7b740SScott Long 			offset -= icblen;
119451a7b740SScott Long 			ad_offset = sizeof(struct long_ad) * ad_num;
119551a7b740SScott Long 			if (ad_offset > fentry->l_ad) {
119651a7b740SScott Long 				printf("File offset out of bounds\n");
119751a7b740SScott Long 				return (EINVAL);
119851a7b740SScott Long 			}
119951a7b740SScott Long 			icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
120051a7b740SScott Long 			icblen = GETICBLEN(long_ad, icb);
120151a7b740SScott Long 			ad_num++;
120251a7b740SScott Long 		} while(offset >= icblen);
120351a7b740SScott Long 
120451a7b740SScott Long 		lsector = (offset >> udfmp->bshift) +
120551a7b740SScott Long 		    ((struct long_ad *)(icb))->loc.lb_num;
120651a7b740SScott Long 
120751a7b740SScott Long 		*max_size = GETICBLEN(long_ad, icb) - offset;
120851a7b740SScott Long 
120951a7b740SScott Long 		break;
121051a7b740SScott Long 	case 3:
121151a7b740SScott Long 		/*
121251a7b740SScott Long 		 * This type means that the file *data* is stored in the
121351a7b740SScott Long 		 * allocation descriptor field of the file entry.
121451a7b740SScott Long 		 */
121551a7b740SScott Long 		*max_size = 0;
12168db4c2f2SScott Long 		*sector = node->hash_id + udfmp->part_start;
121751a7b740SScott Long 
12188db4c2f2SScott Long 		return (INVALID_BMAP);
121951a7b740SScott Long 	case 2:
122051a7b740SScott Long 		/* DirectCD does not use extended_ad's */
122151a7b740SScott Long 	default:
122251a7b740SScott Long 		printf("Unsupported allocation descriptor %d\n",
122351a7b740SScott Long 		       tag->flags & 0x7);
122451a7b740SScott Long 		return (ENODEV);
122551a7b740SScott Long 	}
122651a7b740SScott Long 
122751a7b740SScott Long 	*sector = lsector + udfmp->part_start;
122851a7b740SScott Long 
122951a7b740SScott Long 	/*
123051a7b740SScott Long 	 * Check the sparing table.  Each entry represents the beginning of
123151a7b740SScott Long 	 * a packet.
123251a7b740SScott Long 	 */
123351a7b740SScott Long 	if (udfmp->s_table != NULL) {
123451a7b740SScott Long 		for (i = 0; i< udfmp->s_table_entries; i++) {
123551a7b740SScott Long 			p_offset = lsector - udfmp->s_table->entries[i].org;
123651a7b740SScott Long 			if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
123751a7b740SScott Long 				*sector = udfmp->s_table->entries[i].map +
123851a7b740SScott Long 				    p_offset;
123951a7b740SScott Long 				break;
124051a7b740SScott Long 			}
124151a7b740SScott Long 		}
124251a7b740SScott Long 	}
124351a7b740SScott Long 
124451a7b740SScott Long 	return (0);
124551a7b740SScott Long }
1246