xref: /freebsd/sys/fs/udf/udf_vnops.c (revision 1ede983cc905643549d8cae56a9d0e28fc68375f)
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>
394e94fafcSPoul-Henning Kamp #include <sys/conf.h>
4051a7b740SScott Long #include <sys/buf.h>
416565282cSScott Long #include <sys/iconv.h>
4251a7b740SScott Long #include <sys/mount.h>
4351a7b740SScott Long #include <sys/vnode.h>
4451a7b740SScott Long #include <sys/dirent.h>
4551a7b740SScott Long #include <sys/queue.h>
4651a7b740SScott Long #include <sys/unistd.h>
4789ec2c3cSScott Long #include <sys/endian.h>
4851a7b740SScott Long 
4951a7b740SScott Long #include <vm/uma.h>
5051a7b740SScott Long 
5151a7b740SScott Long #include <fs/udf/ecma167-udf.h>
5251a7b740SScott Long #include <fs/udf/osta.h>
5351a7b740SScott Long #include <fs/udf/udf.h>
546565282cSScott Long #include <fs/udf/udf_mount.h>
556565282cSScott Long 
566565282cSScott Long extern struct iconv_functions *udf_iconv;
5751a7b740SScott Long 
586fde64c7SPoul-Henning Kamp static vop_access_t	udf_access;
596fde64c7SPoul-Henning Kamp static vop_getattr_t	udf_getattr;
6035e06624SPav Lucistnik static vop_open_t	udf_open;
616fde64c7SPoul-Henning Kamp static vop_ioctl_t	udf_ioctl;
626fde64c7SPoul-Henning Kamp static vop_pathconf_t	udf_pathconf;
636fde64c7SPoul-Henning Kamp static vop_read_t	udf_read;
646fde64c7SPoul-Henning Kamp static vop_readdir_t	udf_readdir;
656fde64c7SPoul-Henning Kamp static vop_readlink_t	udf_readlink;
666fde64c7SPoul-Henning Kamp static vop_strategy_t	udf_strategy;
676fde64c7SPoul-Henning Kamp static vop_bmap_t	udf_bmap;
686fde64c7SPoul-Henning Kamp static vop_cachedlookup_t	udf_lookup;
696fde64c7SPoul-Henning Kamp static vop_reclaim_t	udf_reclaim;
7010bcafe9SPawel Jakub Dawidek static vop_vptofh_t	udf_vptofh;
719d32fde8SScott Long static int udf_readatoffset(struct udf_node *node, int *size, off_t offset,
729d32fde8SScott Long     struct buf **bp, uint8_t **data);
739d32fde8SScott Long static int udf_bmap_internal(struct udf_node *node, off_t offset,
749d32fde8SScott Long     daddr_t *sector, uint32_t *max_size);
7551a7b740SScott Long 
76aec0fb7bSPoul-Henning Kamp static struct vop_vector udf_vnodeops = {
77aec0fb7bSPoul-Henning Kamp 	.vop_default =		&default_vnodeops,
7883c64397SPoul-Henning Kamp 
79aec0fb7bSPoul-Henning Kamp 	.vop_access =		udf_access,
80aec0fb7bSPoul-Henning Kamp 	.vop_bmap =		udf_bmap,
81aec0fb7bSPoul-Henning Kamp 	.vop_cachedlookup =	udf_lookup,
82aec0fb7bSPoul-Henning Kamp 	.vop_getattr =		udf_getattr,
83aec0fb7bSPoul-Henning Kamp 	.vop_ioctl =		udf_ioctl,
84aec0fb7bSPoul-Henning Kamp 	.vop_lookup =		vfs_cache_lookup,
8535e06624SPav Lucistnik 	.vop_open =		udf_open,
86aec0fb7bSPoul-Henning Kamp 	.vop_pathconf =		udf_pathconf,
87aec0fb7bSPoul-Henning Kamp 	.vop_read =		udf_read,
88aec0fb7bSPoul-Henning Kamp 	.vop_readdir =		udf_readdir,
89aec0fb7bSPoul-Henning Kamp 	.vop_readlink =		udf_readlink,
90aec0fb7bSPoul-Henning Kamp 	.vop_reclaim =		udf_reclaim,
91aec0fb7bSPoul-Henning Kamp 	.vop_strategy =		udf_strategy,
9210bcafe9SPawel Jakub Dawidek 	.vop_vptofh =		udf_vptofh,
9351a7b740SScott Long };
9451a7b740SScott Long 
955bb84bc8SRobert Watson MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure");
965bb84bc8SRobert Watson MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure");
9751a7b740SScott Long 
984576293dSScott Long #define UDF_INVALID_BMAP	-1
998db4c2f2SScott Long 
10051a7b740SScott Long int
10151a7b740SScott Long udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td)
10251a7b740SScott Long {
10351a7b740SScott Long 	int error;
10451a7b740SScott Long 	struct vnode *vp;
10551a7b740SScott Long 
106aec0fb7bSPoul-Henning Kamp 	error = getnewvnode("udf", mp, &udf_vnodeops, &vp);
10751a7b740SScott Long 	if (error) {
10851a7b740SScott Long 		printf("udf_allocv: failed to allocate new vnode\n");
10951a7b740SScott Long 		return (error);
11051a7b740SScott Long 	}
11151a7b740SScott Long 
11251a7b740SScott Long 	*vpp = vp;
11351a7b740SScott Long 	return (0);
11451a7b740SScott Long }
11551a7b740SScott Long 
11651a7b740SScott Long /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
11751a7b740SScott Long static mode_t
11851a7b740SScott Long udf_permtomode(struct udf_node *node)
11951a7b740SScott Long {
120c2d6947dSJeroen Ruigrok van der Werven 	uint32_t perm;
121bf1c3dddSScott Long 	uint16_t flags;
12251a7b740SScott Long 	mode_t mode;
12351a7b740SScott Long 
124bf1c3dddSScott Long 	perm = le32toh(node->fentry->perm);
125bf1c3dddSScott Long 	flags = le16toh(node->fentry->icbtag.flags);
12651a7b740SScott Long 
12751a7b740SScott Long 	mode = perm & UDF_FENTRY_PERM_USER_MASK;
12851a7b740SScott Long 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
12951a7b740SScott Long 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
13051a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
13151a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
13251a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
13351a7b740SScott Long 
13451a7b740SScott Long 	return (mode);
13551a7b740SScott Long }
13651a7b740SScott Long 
13751a7b740SScott Long static int
13851a7b740SScott Long udf_access(struct vop_access_args *a)
13951a7b740SScott Long {
14051a7b740SScott Long 	struct vnode *vp;
14151a7b740SScott Long 	struct udf_node *node;
14251a7b740SScott Long 	mode_t a_mode, mode;
14351a7b740SScott Long 
14451a7b740SScott Long 	vp = a->a_vp;
14551a7b740SScott Long 	node = VTON(vp);
14651a7b740SScott Long 	a_mode = a->a_mode;
14751a7b740SScott Long 
14851a7b740SScott Long 	if (a_mode & VWRITE) {
14951a7b740SScott Long 		switch (vp->v_type) {
15051a7b740SScott Long 		case VDIR:
15151a7b740SScott Long 		case VLNK:
15251a7b740SScott Long 		case VREG:
15351a7b740SScott Long 			return (EROFS);
15451a7b740SScott Long 			/* NOT REACHED */
15551a7b740SScott Long 		default:
15651a7b740SScott Long 			break;
15751a7b740SScott Long 		}
15851a7b740SScott Long 	}
15951a7b740SScott Long 
16051a7b740SScott Long 	mode = udf_permtomode(node);
16151a7b740SScott Long 
16251a7b740SScott Long 	return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid,
16351a7b740SScott Long 	    a_mode, a->a_cred, NULL));
16451a7b740SScott Long }
16551a7b740SScott Long 
16635e06624SPav Lucistnik static int
16735e06624SPav Lucistnik udf_open(struct vop_open_args *ap) {
16835e06624SPav Lucistnik 	struct udf_node *np = VTON(ap->a_vp);
16935e06624SPav Lucistnik 	off_t fsize;
17035e06624SPav Lucistnik 
17135e06624SPav Lucistnik 	fsize = le64toh(np->fentry->inf_len);
17235e06624SPav Lucistnik 	vnode_create_vobject(ap->a_vp, fsize, ap->a_td);
17335e06624SPav Lucistnik 	return 0;
17435e06624SPav Lucistnik }
17535e06624SPav Lucistnik 
1769c2bf69dSMarkus Brueffer static const int mon_lens[2][12] = {
1779c2bf69dSMarkus Brueffer 	{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
1789c2bf69dSMarkus Brueffer 	{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
17951a7b740SScott Long };
18051a7b740SScott Long 
18151a7b740SScott Long static int
18251a7b740SScott Long udf_isaleapyear(int year)
18351a7b740SScott Long {
18451a7b740SScott Long 	int i;
18551a7b740SScott Long 
18651a7b740SScott Long 	i = (year % 4) ? 0 : 1;
18751a7b740SScott Long 	i &= (year % 100) ? 1 : 0;
18851a7b740SScott Long 	i |= (year % 400) ? 0 : 1;
18951a7b740SScott Long 
19051a7b740SScott Long 	return i;
19151a7b740SScott Long }
19251a7b740SScott Long 
19351a7b740SScott Long /*
19451a7b740SScott Long  * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
19551a7b740SScott Long  */
19651a7b740SScott Long static void
19751a7b740SScott Long udf_timetotimespec(struct timestamp *time, struct timespec *t)
19851a7b740SScott Long {
1999c2bf69dSMarkus Brueffer 	int i, lpyear, daysinyear, year, startyear;
20051a7b740SScott Long 	union {
201c2d6947dSJeroen Ruigrok van der Werven 		uint16_t	u_tz_offset;
20251a7b740SScott Long 		int16_t		s_tz_offset;
20351a7b740SScott Long 	} tz;
20451a7b740SScott Long 
2059c2bf69dSMarkus Brueffer 	/*
2069c2bf69dSMarkus Brueffer 	 * DirectCD seems to like using bogus year values.
2079c2bf69dSMarkus Brueffer 	 * Don't trust time->month as it will be used for an array index.
2089c2bf69dSMarkus Brueffer 	 */
209bf1c3dddSScott Long 	year = le16toh(time->year);
2109c2bf69dSMarkus Brueffer 	if (year < 1970 || time->month < 1 || time->month > 12) {
21151a7b740SScott Long 		t->tv_sec = 0;
2129c2bf69dSMarkus Brueffer 		t->tv_nsec = 0;
21351a7b740SScott Long 		return;
21451a7b740SScott Long 	}
21551a7b740SScott Long 
21651a7b740SScott Long 	/* Calculate the time and day */
21751a7b740SScott Long 	t->tv_sec = time->second;
21851a7b740SScott Long 	t->tv_sec += time->minute * 60;
21951a7b740SScott Long 	t->tv_sec += time->hour * 3600;
2209c2bf69dSMarkus Brueffer 	t->tv_sec += (time->day - 1) * 3600 * 24;
22151a7b740SScott Long 
222befb7f33SChristian Brueffer 	/* Calculate the month */
223bf1c3dddSScott Long 	lpyear = udf_isaleapyear(year);
2249c2bf69dSMarkus Brueffer 	t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24;
22551a7b740SScott Long 
22651a7b740SScott Long 	/* Speed up the calculation */
2279c2bf69dSMarkus Brueffer 	startyear = 1970;
2289c2bf69dSMarkus Brueffer 	if (year > 2009) {
2299c2bf69dSMarkus Brueffer 		t->tv_sec += 1262304000;
2309c2bf69dSMarkus Brueffer 		startyear += 40;
2319c2bf69dSMarkus Brueffer 	} else if (year > 1999) {
2329c2bf69dSMarkus Brueffer 		t->tv_sec += 946684800;
2339c2bf69dSMarkus Brueffer 		startyear += 30;
2349c2bf69dSMarkus Brueffer 	} else if (year > 1989) {
2359c2bf69dSMarkus Brueffer 		t->tv_sec += 631152000;
2369c2bf69dSMarkus Brueffer 		startyear += 20;
2379c2bf69dSMarkus Brueffer 	} else if (year > 1979) {
23851a7b740SScott Long 		t->tv_sec += 315532800;
2399c2bf69dSMarkus Brueffer 		startyear += 10;
24051a7b740SScott Long 	}
24151a7b740SScott Long 
2429c2bf69dSMarkus Brueffer 	daysinyear = (year - startyear) * 365;
2439c2bf69dSMarkus Brueffer 	for (i = startyear; i < year; i++)
2449c2bf69dSMarkus Brueffer 		daysinyear += udf_isaleapyear(i);
2459c2bf69dSMarkus Brueffer 	t->tv_sec += daysinyear * 3600 * 24;
2469c2bf69dSMarkus Brueffer 
2479c2bf69dSMarkus Brueffer 	/* Calculate microseconds */
2489c2bf69dSMarkus Brueffer 	t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 +
2499c2bf69dSMarkus Brueffer 	    time->usec;
2509c2bf69dSMarkus Brueffer 
25151a7b740SScott Long 	/*
25251a7b740SScott Long 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
253befb7f33SChristian Brueffer 	 * complement, so we gotta do some extra magic to handle it right.
25451a7b740SScott Long 	 */
255bf1c3dddSScott Long 	tz.u_tz_offset = le16toh(time->type_tz);
25651a7b740SScott Long 	tz.u_tz_offset &= 0x0fff;
25751a7b740SScott Long 	if (tz.u_tz_offset & 0x0800)
25851a7b740SScott Long 		tz.u_tz_offset |= 0xf000;	/* extend the sign to 16 bits */
2599c2bf69dSMarkus Brueffer 	if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047))
26051a7b740SScott Long 		t->tv_sec -= tz.s_tz_offset * 60;
26151a7b740SScott Long 
26251a7b740SScott Long 	return;
26351a7b740SScott Long }
26451a7b740SScott Long 
26551a7b740SScott Long static int
26651a7b740SScott Long udf_getattr(struct vop_getattr_args *a)
26751a7b740SScott Long {
26851a7b740SScott Long 	struct vnode *vp;
26951a7b740SScott Long 	struct udf_node *node;
27051a7b740SScott Long 	struct vattr *vap;
27151a7b740SScott Long 	struct file_entry *fentry;
27251a7b740SScott Long 	struct timespec ts;
27351a7b740SScott Long 
27451a7b740SScott Long 	ts.tv_sec = 0;
27551a7b740SScott Long 
27651a7b740SScott Long 	vp = a->a_vp;
27751a7b740SScott Long 	vap = a->a_vap;
27851a7b740SScott Long 	node = VTON(vp);
27951a7b740SScott Long 	fentry = node->fentry;
28051a7b740SScott Long 
281c049546eSPoul-Henning Kamp 	vap->va_fsid = dev2udev(node->udfmp->im_dev);
28251a7b740SScott Long 	vap->va_fileid = node->hash_id;
28351a7b740SScott Long 	vap->va_mode = udf_permtomode(node);
284bf1c3dddSScott Long 	vap->va_nlink = le16toh(fentry->link_cnt);
28551a7b740SScott Long 	/*
28651a7b740SScott Long 	 * XXX The spec says that -1 is valid for uid/gid and indicates an
28751a7b740SScott Long 	 * invalid uid/gid.  How should this be represented?
28851a7b740SScott Long 	 */
289bf1c3dddSScott Long 	vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid);
290bf1c3dddSScott Long 	vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid);
29151a7b740SScott Long 	udf_timetotimespec(&fentry->atime, &vap->va_atime);
29251a7b740SScott Long 	udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
29351a7b740SScott Long 	vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
2944c5a20e3SKonstantin Belousov 	vap->va_rdev = NODEV;
29551a7b740SScott Long 	if (vp->v_type & VDIR) {
29651a7b740SScott Long 		/*
29751a7b740SScott Long 		 * Directories that are recorded within their ICB will show
29851a7b740SScott Long 		 * as having 0 blocks recorded.  Since tradition dictates
29951a7b740SScott Long 		 * that directories consume at least one logical block,
30051a7b740SScott Long 		 * make it appear so.
30151a7b740SScott Long 		 */
30251a7b740SScott Long 		if (fentry->logblks_rec != 0) {
303bf1c3dddSScott Long 			vap->va_size =
304bf1c3dddSScott Long 			    le64toh(fentry->logblks_rec) * node->udfmp->bsize;
30551a7b740SScott Long 		} else {
30651a7b740SScott Long 			vap->va_size = node->udfmp->bsize;
30751a7b740SScott Long 		}
30851a7b740SScott Long 	} else {
309bf1c3dddSScott Long 		vap->va_size = le64toh(fentry->inf_len);
31051a7b740SScott Long 	}
31151a7b740SScott Long 	vap->va_flags = 0;
31251a7b740SScott Long 	vap->va_gen = 1;
31351a7b740SScott Long 	vap->va_blocksize = node->udfmp->bsize;
314bf1c3dddSScott Long 	vap->va_bytes = le64toh(fentry->inf_len);
31551a7b740SScott Long 	vap->va_type = vp->v_type;
31651a7b740SScott Long 	vap->va_filerev = 0; /* XXX */
31751a7b740SScott Long 	return (0);
31851a7b740SScott Long }
31951a7b740SScott Long 
32051a7b740SScott Long /*
321bf1c3dddSScott Long  * File specific ioctls.
32251a7b740SScott Long  */
32351a7b740SScott Long static int
32451a7b740SScott Long udf_ioctl(struct vop_ioctl_args *a)
32551a7b740SScott Long {
326c80a90c5SScott Long 	printf("%s called\n", __func__);
3271d02d910SPoul-Henning Kamp 	return (ENOTTY);
32851a7b740SScott Long }
32951a7b740SScott Long 
33051a7b740SScott Long /*
33151a7b740SScott Long  * I'm not sure that this has much value in a read-only filesystem, but
33251a7b740SScott Long  * cd9660 has it too.
33351a7b740SScott Long  */
33451a7b740SScott Long static int
33551a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a)
33651a7b740SScott Long {
33751a7b740SScott Long 
33851a7b740SScott Long 	switch (a->a_name) {
33951a7b740SScott Long 	case _PC_LINK_MAX:
34051a7b740SScott Long 		*a->a_retval = 65535;
34151a7b740SScott Long 		return (0);
34251a7b740SScott Long 	case _PC_NAME_MAX:
34351a7b740SScott Long 		*a->a_retval = NAME_MAX;
34451a7b740SScott Long 		return (0);
34551a7b740SScott Long 	case _PC_PATH_MAX:
34651a7b740SScott Long 		*a->a_retval = PATH_MAX;
34751a7b740SScott Long 		return (0);
34851a7b740SScott Long 	case _PC_NO_TRUNC:
34951a7b740SScott Long 		*a->a_retval = 1;
35051a7b740SScott Long 		return (0);
35151a7b740SScott Long 	default:
35251a7b740SScott Long 		return (EINVAL);
35351a7b740SScott Long 	}
35451a7b740SScott Long }
35551a7b740SScott Long 
3560c09ac0dSPav Lucistnik #define lblkno(udfmp, loc)	((loc) >> (udfmp)->bshift)
3570c09ac0dSPav Lucistnik #define blkoff(udfmp, loc)	((loc) & (udfmp)->bmask)
3580c09ac0dSPav Lucistnik #define lblktosize(imp, blk)	((blk) << (udfmp)->bshift)
35951a7b740SScott Long 
3600c09ac0dSPav Lucistnik static int
3610c09ac0dSPav Lucistnik udf_read(struct vop_read_args *ap)
3620c09ac0dSPav Lucistnik {
3630c09ac0dSPav Lucistnik 	struct vnode *vp = ap->a_vp;
3640c09ac0dSPav Lucistnik 	struct uio *uio = ap->a_uio;
3650c09ac0dSPav Lucistnik 	struct udf_node *node = VTON(vp);
3660c09ac0dSPav Lucistnik 	struct udf_mnt *udfmp;
3670c09ac0dSPav Lucistnik 	struct buf *bp;
3680c09ac0dSPav Lucistnik 	daddr_t lbn, rablock;
3690c09ac0dSPav Lucistnik 	off_t diff, fsize;
3700c09ac0dSPav Lucistnik 	int error = 0;
3710c09ac0dSPav Lucistnik 	long size, n, on;
3720c09ac0dSPav Lucistnik 
3730c09ac0dSPav Lucistnik 	if (uio->uio_resid == 0)
3740c09ac0dSPav Lucistnik 		return (0);
37551a7b740SScott Long 	if (uio->uio_offset < 0)
37651a7b740SScott Long 		return (EINVAL);
377bf1c3dddSScott Long 	fsize = le64toh(node->fentry->inf_len);
3780c09ac0dSPav Lucistnik 	udfmp = node->udfmp;
3790c09ac0dSPav Lucistnik 	do {
3800c09ac0dSPav Lucistnik 		lbn = lblkno(udfmp, uio->uio_offset);
3810c09ac0dSPav Lucistnik 		on = blkoff(udfmp, uio->uio_offset);
3820c09ac0dSPav Lucistnik 		n = min((u_int)(udfmp->bsize - on),
3830c09ac0dSPav Lucistnik 			uio->uio_resid);
3840c09ac0dSPav Lucistnik 		diff = fsize - uio->uio_offset;
3850c09ac0dSPav Lucistnik 		if (diff <= 0)
3860c09ac0dSPav Lucistnik 			return (0);
3870c09ac0dSPav Lucistnik 		if (diff < n)
3880c09ac0dSPav Lucistnik 			n = diff;
3890c09ac0dSPav Lucistnik 		size = udfmp->bsize;
3900c09ac0dSPav Lucistnik 		rablock = lbn + 1;
3910c09ac0dSPav Lucistnik 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
3920c09ac0dSPav Lucistnik 			if (lblktosize(udfmp, rablock) < fsize) {
3930c09ac0dSPav Lucistnik 				error = cluster_read(vp, fsize, lbn, size, NOCRED,
3940c09ac0dSPav Lucistnik 					uio->uio_resid, (ap->a_ioflag >> 16), &bp);
3950c09ac0dSPav Lucistnik 			} else {
3960c09ac0dSPav Lucistnik 				error = bread(vp, lbn, size, NOCRED, &bp);
3970c09ac0dSPav Lucistnik 			}
3980c09ac0dSPav Lucistnik 		} else {
3990c09ac0dSPav Lucistnik 			error = bread(vp, lbn, size, NOCRED, &bp);
4000c09ac0dSPav Lucistnik 		}
4010c09ac0dSPav Lucistnik 		n = min(n, size - bp->b_resid);
4020c09ac0dSPav Lucistnik 		if (error) {
40351a7b740SScott Long 			brelse(bp);
4040c09ac0dSPav Lucistnik 			return (error);
4050c09ac0dSPav Lucistnik 		}
40651a7b740SScott Long 
4070c09ac0dSPav Lucistnik 		error = uiomove(bp->b_data + on, (int)n, uio);
4080c09ac0dSPav Lucistnik 		brelse(bp);
4090c09ac0dSPav Lucistnik 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
41051a7b740SScott Long 	return (error);
41151a7b740SScott Long }
41251a7b740SScott Long 
41351a7b740SScott Long /*
41451a7b740SScott Long  * Call the OSTA routines to translate the name from a CS0 dstring to a
41551a7b740SScott Long  * 16-bit Unicode String.  Hooks need to be placed in here to translate from
4166565282cSScott Long  * Unicode to the encoding that the kernel/user expects.  Return the length
4176565282cSScott Long  * of the translated string.
41851a7b740SScott Long  */
41951a7b740SScott Long static int
4206565282cSScott Long udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
42151a7b740SScott Long {
42251a7b740SScott Long 	unicode_t *transname;
4236565282cSScott Long 	char *unibuf, *unip;
424181fc3c6SR. Imura 	int i, destlen;
425181fc3c6SR. Imura 	ssize_t unilen = 0;
4266565282cSScott Long 	size_t destleft = MAXNAMLEN;
42751a7b740SScott Long 
4286565282cSScott Long 	/* Convert 16-bit Unicode to destname */
4296565282cSScott Long 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
4306565282cSScott Long 		/* allocate a buffer big enough to hold an 8->16 bit expansion */
4316565282cSScott Long 		unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
4326565282cSScott Long 		unip = unibuf;
433181fc3c6SR. Imura 		if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
4346565282cSScott Long 			printf("udf: Unicode translation failed\n");
4356565282cSScott Long 			uma_zfree(udf_zone_trans, unibuf);
4366565282cSScott Long 			return 0;
4376565282cSScott Long 		}
4386565282cSScott Long 
4396565282cSScott Long 		while (unilen > 0 && destleft > 0) {
4406565282cSScott Long 			udf_iconv->conv(udfmp->im_d2l, (const char **)&unibuf,
4416565282cSScott Long 				(size_t *)&unilen, (char **)&destname, &destleft);
4426565282cSScott Long 			/* Unconverted character found */
4436565282cSScott Long 			if (unilen > 0 && destleft > 0) {
4446565282cSScott Long 				*destname++ = '?';
4456565282cSScott Long 				destleft--;
4466565282cSScott Long 				unibuf += 2;
4476565282cSScott Long 				unilen -= 2;
4486565282cSScott Long 			}
4496565282cSScott Long 		}
4506565282cSScott Long 		uma_zfree(udf_zone_trans, unip);
4516565282cSScott Long 		*destname = '\0';
4526565282cSScott Long 		destlen = MAXNAMLEN - (int)destleft;
4536565282cSScott Long 	} else {
45451a7b740SScott Long 		/* allocate a buffer big enough to hold an 8->16 bit expansion */
455a163d034SWarner Losh 		transname = uma_zalloc(udf_zone_trans, M_WAITOK);
45651a7b740SScott Long 
457181fc3c6SR. Imura 		if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) {
45851a7b740SScott Long 			printf("udf: Unicode translation failed\n");
45951a7b740SScott Long 			uma_zfree(udf_zone_trans, transname);
46051a7b740SScott Long 			return 0;
46151a7b740SScott Long 		}
46251a7b740SScott Long 
46351a7b740SScott Long 		for (i = 0; i < unilen ; i++) {
46451a7b740SScott Long 			if (transname[i] & 0xff00) {
46551a7b740SScott Long 				destname[i] = '.';	/* Fudge the 16bit chars */
46651a7b740SScott Long 			} else {
46751a7b740SScott Long 				destname[i] = transname[i] & 0xff;
46851a7b740SScott Long 			}
46951a7b740SScott Long 		}
47051a7b740SScott Long 		uma_zfree(udf_zone_trans, transname);
4716565282cSScott Long 		destname[unilen] = 0;
472181fc3c6SR. Imura 		destlen = (int)unilen;
4736565282cSScott Long 	}
47451a7b740SScott Long 
4756565282cSScott Long 	return (destlen);
47651a7b740SScott Long }
47751a7b740SScott Long 
47851a7b740SScott Long /*
47951a7b740SScott Long  * Compare a CS0 dstring with a name passed in from the VFS layer.  Return
480befb7f33SChristian Brueffer  * 0 on a successful match, nonzero otherwise.  Unicode work may need to be done
48151a7b740SScott Long  * here also.
48251a7b740SScott Long  */
48351a7b740SScott Long static int
4846565282cSScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
48551a7b740SScott Long {
48695ec5961SScott Long 	char *transname;
48795ec5961SScott Long 	int error = 0;
48851a7b740SScott Long 
48995ec5961SScott Long 	/* This is overkill, but not worth creating a new zone */
490a163d034SWarner Losh 	transname = uma_zalloc(udf_zone_trans, M_WAITOK);
49195ec5961SScott Long 
4926565282cSScott Long 	cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
49351a7b740SScott Long 
49451a7b740SScott Long 	/* Easy check.  If they aren't the same length, they aren't equal */
49595ec5961SScott Long 	if ((cs0len == 0) || (cs0len != cmplen))
49695ec5961SScott Long 		error = -1;
49795ec5961SScott Long 	else
49895ec5961SScott Long 		error = bcmp(transname, cmpname, cmplen);
49951a7b740SScott Long 
50095ec5961SScott Long 	uma_zfree(udf_zone_trans, transname);
50195ec5961SScott Long 	return (error);
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 
528c9524588SDag-Erling Smørgrav 	return (uiomove(uiodir->dirent, de_size, uio));
52951a7b740SScott Long }
53051a7b740SScott Long 
5311703656aSScott Long static struct udf_dirstream *
5321703656aSScott Long udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
5331703656aSScott Long {
5341703656aSScott Long 	struct udf_dirstream *ds;
5351703656aSScott Long 
536a163d034SWarner Losh 	ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
5371703656aSScott Long 
5381703656aSScott Long 	ds->node = node;
5391703656aSScott Long 	ds->offset = offset;
5401703656aSScott Long 	ds->udfmp = udfmp;
5411703656aSScott Long 	ds->fsize = fsize;
5421703656aSScott Long 
5431703656aSScott Long 	return (ds);
5441703656aSScott Long }
5451703656aSScott Long 
5461703656aSScott Long static struct fileid_desc *
5471703656aSScott Long udf_getfid(struct udf_dirstream *ds)
5481703656aSScott Long {
5491703656aSScott Long 	struct fileid_desc *fid;
5501703656aSScott Long 	int error, frag_size = 0, total_fid_size;
5511703656aSScott Long 
5521703656aSScott Long 	/* End of directory? */
5531703656aSScott Long 	if (ds->offset + ds->off >= ds->fsize) {
5541703656aSScott Long 		ds->error = 0;
5551703656aSScott Long 		return (NULL);
5561703656aSScott Long 	}
5571703656aSScott Long 
5581703656aSScott Long 	/* Grab the first extent of the directory */
5591703656aSScott Long 	if (ds->off == 0) {
5601703656aSScott Long 		ds->size = 0;
5611703656aSScott Long 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
5621703656aSScott Long 		    &ds->bp, &ds->data);
5631703656aSScott Long 		if (error) {
5641703656aSScott Long 			ds->error = error;
565744bb56dSScott Long 			if (ds->bp != NULL)
566744bb56dSScott Long 				brelse(ds->bp);
5671703656aSScott Long 			return (NULL);
5681703656aSScott Long 		}
5691703656aSScott Long 	}
5701703656aSScott Long 
5714576293dSScott Long 	/*
5724576293dSScott Long 	 * Clean up from a previous fragmented FID.
5734576293dSScott Long 	 * XXX Is this the right place for this?
5744576293dSScott Long 	 */
5751703656aSScott Long 	if (ds->fid_fragment && ds->buf != NULL) {
5761703656aSScott Long 		ds->fid_fragment = 0;
5771ede983cSDag-Erling Smørgrav 		free(ds->buf, M_UDFFID);
5781703656aSScott Long 	}
5791703656aSScott Long 
5801703656aSScott Long 	fid = (struct fileid_desc*)&ds->data[ds->off];
5811703656aSScott Long 
5821703656aSScott Long 	/*
5831703656aSScott Long 	 * Check to see if the fid is fragmented. The first test
5841703656aSScott Long 	 * ensures that we don't wander off the end of the buffer
5851703656aSScott Long 	 * looking for the l_iu and l_fi fields.
5861703656aSScott Long 	 */
5871703656aSScott Long 	if (ds->off + UDF_FID_SIZE > ds->size ||
588bf1c3dddSScott Long 	    ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){
5891703656aSScott Long 
5901703656aSScott Long 		/* Copy what we have of the fid into a buffer */
5911703656aSScott Long 		frag_size = ds->size - ds->off;
5921703656aSScott Long 		if (frag_size >= ds->udfmp->bsize) {
5931703656aSScott Long 			printf("udf: invalid FID fragment\n");
5941703656aSScott Long 			ds->error = EINVAL;
5951703656aSScott Long 			return (NULL);
5961703656aSScott Long 		}
5971703656aSScott Long 
5981703656aSScott Long 		/*
5991703656aSScott Long 		 * File ID descriptors can only be at most one
6001703656aSScott Long 		 * logical sector in size.
6011703656aSScott Long 		 */
6021ede983cSDag-Erling Smørgrav 		ds->buf = malloc(ds->udfmp->bsize, M_UDFFID,
603a163d034SWarner Losh 		     M_WAITOK | M_ZERO);
6041703656aSScott Long 		bcopy(fid, ds->buf, frag_size);
6051703656aSScott Long 
6061703656aSScott Long 		/* Reduce all of the casting magic */
6071703656aSScott Long 		fid = (struct fileid_desc*)ds->buf;
6081703656aSScott Long 
6091703656aSScott Long 		if (ds->bp != NULL)
6101703656aSScott Long 			brelse(ds->bp);
6111703656aSScott Long 
6121703656aSScott Long 		/* Fetch the next allocation */
6131703656aSScott Long 		ds->offset += ds->size;
6141703656aSScott Long 		ds->size = 0;
6151703656aSScott Long 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
6161703656aSScott Long 		    &ds->bp, &ds->data);
6171703656aSScott Long 		if (error) {
6181703656aSScott Long 			ds->error = error;
6191703656aSScott Long 			return (NULL);
6201703656aSScott Long 		}
6211703656aSScott Long 
6221703656aSScott Long 		/*
6231703656aSScott Long 		 * If the fragment was so small that we didn't get
6241703656aSScott Long 		 * the l_iu and l_fi fields, copy those in.
6251703656aSScott Long 		 */
6261703656aSScott Long 		if (frag_size < UDF_FID_SIZE)
6271703656aSScott Long 			bcopy(ds->data, &ds->buf[frag_size],
6281703656aSScott Long 			    UDF_FID_SIZE - frag_size);
6291703656aSScott Long 
6301703656aSScott Long 		/*
6311703656aSScott Long 		 * Now that we have enough of the fid to work with,
6321703656aSScott Long 		 * copy in the rest of the fid from the new
6331703656aSScott Long 		 * allocation.
6341703656aSScott Long 		 */
635bf1c3dddSScott Long 		total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi;
6361703656aSScott Long 		if (total_fid_size > ds->udfmp->bsize) {
6371703656aSScott Long 			printf("udf: invalid FID\n");
6381703656aSScott Long 			ds->error = EIO;
6391703656aSScott Long 			return (NULL);
6401703656aSScott Long 		}
6411703656aSScott Long 		bcopy(ds->data, &ds->buf[frag_size],
6421703656aSScott Long 		    total_fid_size - frag_size);
6431703656aSScott Long 
6441703656aSScott Long 		ds->fid_fragment = 1;
6451703656aSScott Long 	} else {
646bf1c3dddSScott Long 		total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE;
6471703656aSScott Long 	}
6481703656aSScott Long 
6491703656aSScott Long 	/*
6501703656aSScott Long 	 * Update the offset. Align on a 4 byte boundary because the
6514576293dSScott Long 	 * UDF spec says so.
6521703656aSScott Long 	 */
6531703656aSScott Long 	ds->this_off = ds->off;
6541703656aSScott Long 	if (!ds->fid_fragment) {
6551703656aSScott Long 		ds->off += (total_fid_size + 3) & ~0x03;
6561703656aSScott Long 	} else {
6571703656aSScott Long 		ds->off = (total_fid_size - frag_size + 3) & ~0x03;
6581703656aSScott Long 	}
6591703656aSScott Long 
6601703656aSScott Long 	return (fid);
6611703656aSScott Long }
6621703656aSScott Long 
6631703656aSScott Long static void
6641703656aSScott Long udf_closedir(struct udf_dirstream *ds)
6651703656aSScott Long {
6661703656aSScott Long 
6671703656aSScott Long 	if (ds->bp != NULL)
6681703656aSScott Long 		brelse(ds->bp);
6691703656aSScott Long 
6701703656aSScott Long 	if (ds->fid_fragment && ds->buf != NULL)
6711ede983cSDag-Erling Smørgrav 		free(ds->buf, M_UDFFID);
6721703656aSScott Long 
6731703656aSScott Long 	uma_zfree(udf_zone_ds, ds);
6741703656aSScott Long }
6751703656aSScott Long 
67651a7b740SScott Long static int
67751a7b740SScott Long udf_readdir(struct vop_readdir_args *a)
67851a7b740SScott Long {
67951a7b740SScott Long 	struct vnode *vp;
68051a7b740SScott Long 	struct uio *uio;
68151a7b740SScott Long 	struct dirent dir;
68251a7b740SScott Long 	struct udf_node *node;
6836565282cSScott Long 	struct udf_mnt *udfmp;
68451a7b740SScott Long 	struct fileid_desc *fid;
68551a7b740SScott Long 	struct udf_uiodir uiodir;
6861703656aSScott Long 	struct udf_dirstream *ds;
68751a7b740SScott Long 	u_long *cookies = NULL;
68851a7b740SScott Long 	int ncookies;
689c8eeea2fSScott Long 	int error = 0;
69051a7b740SScott Long 
69151a7b740SScott Long 	vp = a->a_vp;
69251a7b740SScott Long 	uio = a->a_uio;
69351a7b740SScott Long 	node = VTON(vp);
6946565282cSScott Long 	udfmp = node->udfmp;
69551a7b740SScott Long 	uiodir.eofflag = 1;
69651a7b740SScott Long 
69751a7b740SScott Long 	if (a->a_ncookies != NULL) {
69851a7b740SScott Long 		/*
69951a7b740SScott Long 		 * Guess how many entries are needed.  If we run out, this
70051a7b740SScott Long 		 * function will be called again and thing will pick up were
70151a7b740SScott Long 		 * it left off.
70251a7b740SScott Long 		 */
70351a7b740SScott Long 		ncookies = uio->uio_resid / 8;
7041ede983cSDag-Erling Smørgrav 		cookies = malloc(sizeof(u_long) * ncookies,
705a163d034SWarner Losh 		    M_TEMP, M_WAITOK);
70651a7b740SScott Long 		if (cookies == NULL)
70751a7b740SScott Long 			return (ENOMEM);
70851a7b740SScott Long 		uiodir.ncookies = ncookies;
70951a7b740SScott Long 		uiodir.cookies = cookies;
71051a7b740SScott Long 		uiodir.acookies = 0;
71151a7b740SScott Long 	} else {
71251a7b740SScott Long 		uiodir.cookies = NULL;
71351a7b740SScott Long 	}
71451a7b740SScott Long 
71551a7b740SScott Long 	/*
71651a7b740SScott Long 	 * Iterate through the file id descriptors.  Give the parent dir
7174576293dSScott Long 	 * entry special attention.
71851a7b740SScott Long 	 */
719bf1c3dddSScott Long 	ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
7201703656aSScott Long 	    node->udfmp);
72151a7b740SScott Long 
7221703656aSScott Long 	while ((fid = udf_getfid(ds)) != NULL) {
72351a7b740SScott Long 
72451a7b740SScott Long 		/* XXX Should we return an error on a bad fid? */
72551a7b740SScott Long 		if (udf_checktag(&fid->tag, TAGID_FID)) {
72651a7b740SScott Long 			printf("Invalid FID tag\n");
72777411499SScott Long 			hexdump(fid, UDF_FID_SIZE, NULL, 0);
7281703656aSScott Long 			error = EIO;
72951a7b740SScott Long 			break;
73051a7b740SScott Long 		}
73151a7b740SScott Long 
73251a7b740SScott Long 		/* Is this a deleted file? */
7332bbe0d36SScott Long 		if (fid->file_char & UDF_FILE_CHAR_DEL)
7341703656aSScott Long 			continue;
73551a7b740SScott Long 
7362bbe0d36SScott Long 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
73751a7b740SScott Long 			/* Do up the '.' and '..' entries.  Dummy values are
73851a7b740SScott Long 			 * used for the cookies since the offset here is
73951a7b740SScott Long 			 * usually zero, and NFS doesn't like that value
74051a7b740SScott Long 			 */
741c8eeea2fSScott Long 			dir.d_fileno = node->hash_id;
742c8eeea2fSScott Long 			dir.d_type = DT_DIR;
743c8eeea2fSScott Long 			dir.d_name[0] = '.';
744444acc16SScott Long 			dir.d_name[1] = '\0';
745c8eeea2fSScott Long 			dir.d_namlen = 1;
746c8eeea2fSScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
747c8eeea2fSScott Long 			uiodir.dirent = &dir;
748c8eeea2fSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
74951a7b740SScott Long 			if (error)
75051a7b740SScott Long 				break;
75151a7b740SScott Long 
752c8eeea2fSScott Long 			dir.d_fileno = udf_getid(&fid->icb);
753c8eeea2fSScott Long 			dir.d_type = DT_DIR;
754c8eeea2fSScott Long 			dir.d_name[0] = '.';
755c8eeea2fSScott Long 			dir.d_name[1] = '.';
756444acc16SScott Long 			dir.d_name[2] = '\0';
757c8eeea2fSScott Long 			dir.d_namlen = 2;
758c8eeea2fSScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
759c8eeea2fSScott Long 			uiodir.dirent = &dir;
760c8eeea2fSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
76151a7b740SScott Long 		} else {
76251a7b740SScott Long 			dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
7636565282cSScott Long 			    &dir.d_name[0], fid->l_fi, udfmp);
76451a7b740SScott Long 			dir.d_fileno = udf_getid(&fid->icb);
7652bbe0d36SScott Long 			dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
7662bbe0d36SScott Long 			    DT_DIR : DT_UNKNOWN;
76751a7b740SScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
76851a7b740SScott Long 			uiodir.dirent = &dir;
7691703656aSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio,
7701703656aSScott Long 			    ds->this_off);
77151a7b740SScott Long 		}
77251a7b740SScott Long 		if (error) {
77351a7b740SScott Long 			printf("uiomove returned %d\n", error);
77451a7b740SScott Long 			break;
77551a7b740SScott Long 		}
77651a7b740SScott Long 
77751a7b740SScott Long 	}
77851a7b740SScott Long 
77951a7b740SScott Long 	/* tell the calling layer whether we need to be called again */
78051a7b740SScott Long 	*a->a_eofflag = uiodir.eofflag;
7811703656aSScott Long 	uio->uio_offset = ds->offset + ds->off;
78251a7b740SScott Long 
7831703656aSScott Long 	if (!error)
7841703656aSScott Long 		error = ds->error;
7851703656aSScott Long 
7861703656aSScott Long 	udf_closedir(ds);
78751a7b740SScott Long 
78851a7b740SScott Long 	if (a->a_ncookies != NULL) {
78951a7b740SScott Long 		if (error)
7901ede983cSDag-Erling Smørgrav 			free(cookies, M_TEMP);
79151a7b740SScott Long 		else {
79251a7b740SScott Long 			*a->a_ncookies = uiodir.acookies;
79351a7b740SScott Long 			*a->a_cookies = cookies;
79451a7b740SScott Long 		}
79551a7b740SScott Long 	}
79651a7b740SScott Long 
79751a7b740SScott Long 	return (error);
79851a7b740SScott Long }
79951a7b740SScott Long 
80051a7b740SScott Long /* Are there any implementations out there that do soft-links? */
80151a7b740SScott Long static int
80251a7b740SScott Long udf_readlink(struct vop_readlink_args *ap)
80351a7b740SScott Long {
804c80a90c5SScott Long 	printf("%s called\n", __func__);
80551a7b740SScott Long 	return (EOPNOTSUPP);
80651a7b740SScott Long }
80751a7b740SScott Long 
80851a7b740SScott Long static int
80951a7b740SScott Long udf_strategy(struct vop_strategy_args *a)
81051a7b740SScott Long {
81151a7b740SScott Long 	struct buf *bp;
81251a7b740SScott Long 	struct vnode *vp;
81351a7b740SScott Long 	struct udf_node *node;
81451a7b740SScott Long 	int maxsize;
8150c09ac0dSPav Lucistnik 	daddr_t sector;
816429c018aSPoul-Henning Kamp 	struct bufobj *bo;
8170c09ac0dSPav Lucistnik 	int multiplier;
81851a7b740SScott Long 
81951a7b740SScott Long 	bp = a->a_bp;
820d83b7498SPoul-Henning Kamp 	vp = a->a_vp;
82151a7b740SScott Long 	node = VTON(vp);
82251a7b740SScott Long 
8230c09ac0dSPav Lucistnik 	if (bp->b_blkno == bp->b_lblkno) {
82451a7b740SScott Long 		/*
82551a7b740SScott Long 		 * Files that are embedded in the fentry don't translate well
82651a7b740SScott Long 		 * to a block number.  Reject.
82751a7b740SScott Long 		 */
82851a7b740SScott Long 		if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize,
8290c09ac0dSPav Lucistnik 		    &sector, &maxsize)) {
83051a7b740SScott Long 			clrbuf(bp);
83151a7b740SScott Long 			bp->b_blkno = -1;
83251a7b740SScott Long 		}
8330c09ac0dSPav Lucistnik 
8340c09ac0dSPav Lucistnik 		/* bmap gives sector numbers, bio works with device blocks */
8350c09ac0dSPav Lucistnik 		multiplier = node->udfmp->bsize / DEV_BSIZE;
8360c09ac0dSPav Lucistnik 		bp->b_blkno = sector * multiplier;
8370c09ac0dSPav Lucistnik 
83851a7b740SScott Long 	}
83951a7b740SScott Long 	if ((long)bp->b_blkno == -1) {
84051a7b740SScott Long 		bufdone(bp);
84151a7b740SScott Long 		return (0);
84251a7b740SScott Long 	}
843429c018aSPoul-Henning Kamp 	bo = node->udfmp->im_bo;
8442c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
8450391e5a1SPoul-Henning Kamp 	BO_STRATEGY(bo, bp);
84651a7b740SScott Long 	return (0);
84751a7b740SScott Long }
84851a7b740SScott Long 
84951a7b740SScott Long static int
85051a7b740SScott Long udf_bmap(struct vop_bmap_args *a)
85151a7b740SScott Long {
85251a7b740SScott Long 	struct udf_node *node;
853c2d6947dSJeroen Ruigrok van der Werven 	uint32_t max_size;
85498b0c789SPoul-Henning Kamp 	daddr_t lsector;
85551a7b740SScott Long 	int error;
85651a7b740SScott Long 
85751a7b740SScott Long 	node = VTON(a->a_vp);
85851a7b740SScott Long 
8599c83534dSPoul-Henning Kamp 	if (a->a_bop != NULL)
860e0251bbbSPoul-Henning Kamp 		*a->a_bop = &node->udfmp->im_devvp->v_bufobj;
86151a7b740SScott Long 	if (a->a_bnp == NULL)
86251a7b740SScott Long 		return (0);
86351a7b740SScott Long 	if (a->a_runb)
86451a7b740SScott Long 		*a->a_runb = 0;
86551a7b740SScott Long 
866cd1b1a1dSScott Long 	error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector,
86751a7b740SScott Long 	    &max_size);
8688db4c2f2SScott Long 	if (error)
86951a7b740SScott Long 		return (error);
87051a7b740SScott Long 
871cd1b1a1dSScott Long 	/* Translate logical to physical sector number */
872cd1b1a1dSScott Long 	*a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
873cd1b1a1dSScott Long 
87451a7b740SScott Long 	/* Punt on read-ahead for now */
87551a7b740SScott Long 	if (a->a_runp)
87651a7b740SScott Long 		*a->a_runp = 0;
87751a7b740SScott Long 
87851a7b740SScott Long 	return (0);
87951a7b740SScott Long }
88051a7b740SScott Long 
88151a7b740SScott Long /*
88251a7b740SScott Long  * The all powerful VOP_LOOKUP().
88351a7b740SScott Long  */
88451a7b740SScott Long static int
88551a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a)
88651a7b740SScott Long {
88751a7b740SScott Long 	struct vnode *dvp;
88851a7b740SScott Long 	struct vnode *tdp = NULL;
88951a7b740SScott Long 	struct vnode **vpp = a->a_vpp;
89051a7b740SScott Long 	struct udf_node *node;
89151a7b740SScott Long 	struct udf_mnt *udfmp;
89251a7b740SScott Long 	struct fileid_desc *fid = NULL;
8931703656aSScott Long 	struct udf_dirstream *ds;
89451a7b740SScott Long 	struct thread *td;
89551a7b740SScott Long 	u_long nameiop;
89651a7b740SScott Long 	u_long flags;
89751a7b740SScott Long 	char *nameptr;
89851a7b740SScott Long 	long namelen;
89951a7b740SScott Long 	ino_t id = 0;
9001703656aSScott Long 	int offset, error = 0;
9011703656aSScott Long 	int numdirpasses, fsize;
90251a7b740SScott Long 
90351a7b740SScott Long 	dvp = a->a_dvp;
90451a7b740SScott Long 	node = VTON(dvp);
90551a7b740SScott Long 	udfmp = node->udfmp;
90651a7b740SScott Long 	nameiop = a->a_cnp->cn_nameiop;
90751a7b740SScott Long 	flags = a->a_cnp->cn_flags;
90851a7b740SScott Long 	nameptr = a->a_cnp->cn_nameptr;
90951a7b740SScott Long 	namelen = a->a_cnp->cn_namelen;
910bf1c3dddSScott Long 	fsize = le64toh(node->fentry->inf_len);
91151a7b740SScott Long 	td = a->a_cnp->cn_thread;
91251a7b740SScott Long 
91351a7b740SScott Long 	/*
91451a7b740SScott Long 	 * If this is a LOOKUP and we've already partially searched through
91551a7b740SScott Long 	 * the directory, pick up where we left off and flag that the
91651a7b740SScott Long 	 * directory may need to be searched twice.  For a full description,
9174b12bb04SMaxim Konovalov 	 * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
91851a7b740SScott Long 	 */
9191703656aSScott Long 	if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
92051a7b740SScott Long 		offset = 0;
92151a7b740SScott Long 		numdirpasses = 1;
92251a7b740SScott Long 	} else {
92351a7b740SScott Long 		offset = node->diroff;
92451a7b740SScott Long 		numdirpasses = 2;
92551a7b740SScott Long 		nchstats.ncs_2passes++;
92651a7b740SScott Long 	}
92751a7b740SScott Long 
92851a7b740SScott Long lookloop:
9291703656aSScott Long 	ds = udf_opendir(node, offset, fsize, udfmp);
93051a7b740SScott Long 
9311703656aSScott Long 	while ((fid = udf_getfid(ds)) != NULL) {
93251a7b740SScott Long 
93351a7b740SScott Long 		/* XXX Should we return an error on a bad fid? */
9341703656aSScott Long 		if (udf_checktag(&fid->tag, TAGID_FID)) {
9351703656aSScott Long 			printf("udf_lookup: Invalid tag\n");
9361703656aSScott Long 			error = EIO;
9371703656aSScott Long 			break;
9381703656aSScott Long 		}
939678d5effSScott Long 
940678d5effSScott Long 		/* Is this a deleted file? */
9412bbe0d36SScott Long 		if (fid->file_char & UDF_FILE_CHAR_DEL)
9421703656aSScott Long 			continue;
94351a7b740SScott Long 
9442bbe0d36SScott Long 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
94551a7b740SScott Long 			if (flags & ISDOTDOT) {
94651a7b740SScott Long 				id = udf_getid(&fid->icb);
94751a7b740SScott Long 				break;
94851a7b740SScott Long 			}
94951a7b740SScott Long 		} else {
95051a7b740SScott Long 			if (!(udf_cmpname(&fid->data[fid->l_iu],
9516565282cSScott Long 			    nameptr, fid->l_fi, namelen, udfmp))) {
95251a7b740SScott Long 				id = udf_getid(&fid->icb);
95351a7b740SScott Long 				break;
95451a7b740SScott Long 			}
95551a7b740SScott Long 		}
95651a7b740SScott Long 	}
9571703656aSScott Long 
9581703656aSScott Long 	if (!error)
9591703656aSScott Long 		error = ds->error;
9601703656aSScott Long 
9611703656aSScott Long 	/* XXX Bail out here? */
9621703656aSScott Long 	if (error) {
9631703656aSScott Long 		udf_closedir(ds);
9641703656aSScott Long 		return (error);
96551a7b740SScott Long 	}
96651a7b740SScott Long 
96751a7b740SScott Long 	/* Did we have a match? */
96851a7b740SScott Long 	if (id) {
96927ad03cbSJeff Roberson 		if (flags & ISDOTDOT)
97022db15c0SAttilio Rao 			VOP_UNLOCK(dvp, 0);
97151a7b740SScott Long 		error = udf_vget(udfmp->im_mountp, id, LK_EXCLUSIVE, &tdp);
972045f25a2SSeigo Tanimura 		if (flags & ISDOTDOT)
973cb05b60aSAttilio Rao 			vn_lock(dvp, LK_EXCLUSIVE|LK_RETRY);
9741703656aSScott Long 		if (!error) {
9751703656aSScott Long 			/*
9761703656aSScott Long 			 * Remember where this entry was if it's the final
9771703656aSScott Long 			 * component.
9781703656aSScott Long 			 */
97951a7b740SScott Long 			if ((flags & ISLASTCN) && nameiop == LOOKUP)
9801703656aSScott Long 				node->diroff = ds->offset + ds->off;
98151a7b740SScott Long 			if (numdirpasses == 2)
98251a7b740SScott Long 				nchstats.ncs_pass2++;
98351a7b740SScott Long 			*vpp = tdp;
98451a7b740SScott Long 			/* Put this entry in the cache */
98551a7b740SScott Long 			if (flags & MAKEENTRY)
98651a7b740SScott Long 				cache_enter(dvp, *vpp, a->a_cnp);
9874585e3acSJeff Roberson 		}
9881703656aSScott Long 	} else {
98951a7b740SScott Long 		/* Name wasn't found on this pass.  Do another pass? */
99051a7b740SScott Long 		if (numdirpasses == 2) {
99151a7b740SScott Long 			numdirpasses--;
99251a7b740SScott Long 			offset = 0;
9931703656aSScott Long 			udf_closedir(ds);
99451a7b740SScott Long 			goto lookloop;
99551a7b740SScott Long 		}
99651a7b740SScott Long 
99751a7b740SScott Long 		/* Enter name into cache as non-existant */
99851a7b740SScott Long 		if (flags & MAKEENTRY)
99951a7b740SScott Long 			cache_enter(dvp, *vpp, a->a_cnp);
100051a7b740SScott Long 
10011703656aSScott Long 		if ((flags & ISLASTCN) &&
10021703656aSScott Long 		    (nameiop == CREATE || nameiop == RENAME)) {
10031703656aSScott Long 			error = EROFS;
10041703656aSScott Long 		} else {
10051703656aSScott Long 			error = ENOENT;
10061703656aSScott Long 		}
10071703656aSScott Long 	}
100851a7b740SScott Long 
10091703656aSScott Long 	udf_closedir(ds);
10101703656aSScott Long 	return (error);
101151a7b740SScott Long }
101251a7b740SScott Long 
101351a7b740SScott Long static int
101451a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a)
101551a7b740SScott Long {
101651a7b740SScott Long 	struct vnode *vp;
101751a7b740SScott Long 	struct udf_node *unode;
101851a7b740SScott Long 
101951a7b740SScott Long 	vp = a->a_vp;
102051a7b740SScott Long 	unode = VTON(vp);
102151a7b740SScott Long 
102292e73f57SAlfred Perlstein 	/*
102392e73f57SAlfred Perlstein 	 * Destroy the vm object and flush associated pages.
102492e73f57SAlfred Perlstein 	 */
102592e73f57SAlfred Perlstein 	vnode_destroy_vobject(vp);
102692e73f57SAlfred Perlstein 
102751a7b740SScott Long 	if (unode != NULL) {
10284e94fafcSPoul-Henning Kamp 		vfs_hash_remove(vp);
102951a7b740SScott Long 
103051a7b740SScott Long 		if (unode->fentry != NULL)
10311ede983cSDag-Erling Smørgrav 			free(unode->fentry, M_UDFFENTRY);
103251a7b740SScott Long 		uma_zfree(udf_zone_node, unode);
103351a7b740SScott Long 		vp->v_data = NULL;
103451a7b740SScott Long 	}
103551a7b740SScott Long 
103651a7b740SScott Long 	return (0);
103751a7b740SScott Long }
103851a7b740SScott Long 
103910bcafe9SPawel Jakub Dawidek static int
104010bcafe9SPawel Jakub Dawidek udf_vptofh(struct vop_vptofh_args *a)
104110bcafe9SPawel Jakub Dawidek {
104210bcafe9SPawel Jakub Dawidek 	struct udf_node *node;
104310bcafe9SPawel Jakub Dawidek 	struct ifid *ifhp;
104410bcafe9SPawel Jakub Dawidek 
104510bcafe9SPawel Jakub Dawidek 	node = VTON(a->a_vp);
104610bcafe9SPawel Jakub Dawidek 	ifhp = (struct ifid *)a->a_fhp;
104710bcafe9SPawel Jakub Dawidek 	ifhp->ifid_len = sizeof(struct ifid);
104810bcafe9SPawel Jakub Dawidek 	ifhp->ifid_ino = node->hash_id;
104910bcafe9SPawel Jakub Dawidek 
105010bcafe9SPawel Jakub Dawidek 	return (0);
105110bcafe9SPawel Jakub Dawidek }
105210bcafe9SPawel Jakub Dawidek 
105351a7b740SScott Long /*
105451a7b740SScott Long  * Read the block and then set the data pointer to correspond with the
105551a7b740SScott Long  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
105651a7b740SScott Long  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
105751a7b740SScott Long  * whole extent.
1058744bb56dSScott Long  *
1059744bb56dSScott Long  * Note that *bp may be assigned error or not.
1060744bb56dSScott Long  *
106151a7b740SScott Long  */
106251a7b740SScott Long static int
10639d32fde8SScott Long udf_readatoffset(struct udf_node *node, int *size, off_t offset,
10649d32fde8SScott Long     struct buf **bp, uint8_t **data)
106551a7b740SScott Long {
106651a7b740SScott Long 	struct udf_mnt *udfmp;
106751a7b740SScott Long 	struct file_entry *fentry = NULL;
106851a7b740SScott Long 	struct buf *bp1;
1069c2d6947dSJeroen Ruigrok van der Werven 	uint32_t max_size;
107098b0c789SPoul-Henning Kamp 	daddr_t sector;
107151a7b740SScott Long 	int error;
107251a7b740SScott Long 
107351a7b740SScott Long 	udfmp = node->udfmp;
107451a7b740SScott Long 
1075744bb56dSScott Long 	*bp = NULL;
107651a7b740SScott Long 	error = udf_bmap_internal(node, offset, &sector, &max_size);
10774576293dSScott Long 	if (error == UDF_INVALID_BMAP) {
107851a7b740SScott Long 		/*
107951a7b740SScott Long 		 * This error means that the file *data* is stored in the
108051a7b740SScott Long 		 * allocation descriptor field of the file entry.
108151a7b740SScott Long 		 */
108251a7b740SScott Long 		fentry = node->fentry;
1083bf1c3dddSScott Long 		*data = &fentry->data[le32toh(fentry->l_ea)];
1084bf1c3dddSScott Long 		*size = le32toh(fentry->l_ad);
108551a7b740SScott Long 		return (0);
108651a7b740SScott Long 	} else if (error != 0) {
108751a7b740SScott Long 		return (error);
108851a7b740SScott Long 	}
108951a7b740SScott Long 
1090d1def83bSScott Long 	/* Adjust the size so that it is within range */
109151a7b740SScott Long 	if (*size == 0 || *size > max_size)
109251a7b740SScott Long 		*size = max_size;
1093d1def83bSScott Long 	*size = min(*size, MAXBSIZE);
109451a7b740SScott Long 
10955df29e0cSRemko Lodder 	if ((error = udf_readlblks(udfmp, sector, *size + (offset & udfmp->bmask), bp))) {
10961830bca1SScott Long 		printf("warning: udf_readlblks returned error %d\n", error);
1097744bb56dSScott Long 		/* note: *bp may be non-NULL */
109851a7b740SScott Long 		return (error);
109951a7b740SScott Long 	}
110051a7b740SScott Long 
110151a7b740SScott Long 	bp1 = *bp;
11025df29e0cSRemko Lodder 	*data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
110351a7b740SScott Long 	return (0);
110451a7b740SScott Long }
110551a7b740SScott Long 
110651a7b740SScott Long /*
110751a7b740SScott Long  * Translate a file offset into a logical block and then into a physical
110851a7b740SScott Long  * block.
11095df29e0cSRemko Lodder  * max_size - maximum number of bytes that can be read starting from given
11105df29e0cSRemko Lodder  * offset, rather than beginning of calculated sector number
111151a7b740SScott Long  */
111251a7b740SScott Long static int
11139d32fde8SScott Long udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
11149d32fde8SScott Long     uint32_t *max_size)
111551a7b740SScott Long {
111651a7b740SScott Long 	struct udf_mnt *udfmp;
111751a7b740SScott Long 	struct file_entry *fentry;
111851a7b740SScott Long 	void *icb;
111951a7b740SScott Long 	struct icb_tag *tag;
1120c2d6947dSJeroen Ruigrok van der Werven 	uint32_t icblen = 0;
112198b0c789SPoul-Henning Kamp 	daddr_t lsector;
112251a7b740SScott Long 	int ad_offset, ad_num = 0;
112351a7b740SScott Long 	int i, p_offset;
112451a7b740SScott Long 
112551a7b740SScott Long 	udfmp = node->udfmp;
112651a7b740SScott Long 	fentry = node->fentry;
112751a7b740SScott Long 	tag = &fentry->icbtag;
112851a7b740SScott Long 
1129bf1c3dddSScott Long 	switch (le16toh(tag->strat_type)) {
113051a7b740SScott Long 	case 4:
113151a7b740SScott Long 		break;
113251a7b740SScott Long 
113351a7b740SScott Long 	case 4096:
113451a7b740SScott Long 		printf("Cannot deal with strategy4096 yet!\n");
113551a7b740SScott Long 		return (ENODEV);
113651a7b740SScott Long 
113751a7b740SScott Long 	default:
113851a7b740SScott Long 		printf("Unknown strategy type %d\n", tag->strat_type);
113951a7b740SScott Long 		return (ENODEV);
114051a7b740SScott Long 	}
114151a7b740SScott Long 
1142bf1c3dddSScott Long 	switch (le16toh(tag->flags) & 0x7) {
114351a7b740SScott Long 	case 0:
114451a7b740SScott Long 		/*
114551a7b740SScott Long 		 * The allocation descriptor field is filled with short_ad's.
114651a7b740SScott Long 		 * If the offset is beyond the current extent, look for the
114751a7b740SScott Long 		 * next extent.
114851a7b740SScott Long 		 */
114951a7b740SScott Long 		do {
115051a7b740SScott Long 			offset -= icblen;
115151a7b740SScott Long 			ad_offset = sizeof(struct short_ad) * ad_num;
1152bf1c3dddSScott Long 			if (ad_offset > le32toh(fentry->l_ad)) {
115351a7b740SScott Long 				printf("File offset out of bounds\n");
115451a7b740SScott Long 				return (EINVAL);
115551a7b740SScott Long 			}
1156a4d629e3SScott Long 			icb = GETICB(short_ad, fentry,
1157bf1c3dddSScott Long 			    le32toh(fentry->l_ea) + ad_offset);
115851a7b740SScott Long 			icblen = GETICBLEN(short_ad, icb);
115951a7b740SScott Long 			ad_num++;
116051a7b740SScott Long 		} while(offset >= icblen);
116151a7b740SScott Long 
116251a7b740SScott Long 		lsector = (offset  >> udfmp->bshift) +
1163937a2387SWill Andrews 		    le32toh(((struct short_ad *)(icb))->pos);
116451a7b740SScott Long 
11655df29e0cSRemko Lodder 		*max_size = icblen - offset;
116651a7b740SScott Long 
116751a7b740SScott Long 		break;
116851a7b740SScott Long 	case 1:
116951a7b740SScott Long 		/*
117051a7b740SScott Long 		 * The allocation descriptor field is filled with long_ad's
117151a7b740SScott Long 		 * If the offset is beyond the current extent, look for the
117251a7b740SScott Long 		 * next extent.
117351a7b740SScott Long 		 */
117451a7b740SScott Long 		do {
117551a7b740SScott Long 			offset -= icblen;
117651a7b740SScott Long 			ad_offset = sizeof(struct long_ad) * ad_num;
1177bf1c3dddSScott Long 			if (ad_offset > le32toh(fentry->l_ad)) {
117851a7b740SScott Long 				printf("File offset out of bounds\n");
117951a7b740SScott Long 				return (EINVAL);
118051a7b740SScott Long 			}
1181bf1c3dddSScott Long 			icb = GETICB(long_ad, fentry,
1182bf1c3dddSScott Long 			    le32toh(fentry->l_ea) + ad_offset);
118351a7b740SScott Long 			icblen = GETICBLEN(long_ad, icb);
118451a7b740SScott Long 			ad_num++;
118551a7b740SScott Long 		} while(offset >= icblen);
118651a7b740SScott Long 
118751a7b740SScott Long 		lsector = (offset >> udfmp->bshift) +
1188bf1c3dddSScott Long 		    le32toh(((struct long_ad *)(icb))->loc.lb_num);
118951a7b740SScott Long 
11905df29e0cSRemko Lodder 		*max_size = icblen - offset;
119151a7b740SScott Long 
119251a7b740SScott Long 		break;
119351a7b740SScott Long 	case 3:
119451a7b740SScott Long 		/*
119551a7b740SScott Long 		 * This type means that the file *data* is stored in the
119651a7b740SScott Long 		 * allocation descriptor field of the file entry.
119751a7b740SScott Long 		 */
119851a7b740SScott Long 		*max_size = 0;
11998db4c2f2SScott Long 		*sector = node->hash_id + udfmp->part_start;
120051a7b740SScott Long 
12014576293dSScott Long 		return (UDF_INVALID_BMAP);
120251a7b740SScott Long 	case 2:
120351a7b740SScott Long 		/* DirectCD does not use extended_ad's */
120451a7b740SScott Long 	default:
120551a7b740SScott Long 		printf("Unsupported allocation descriptor %d\n",
120651a7b740SScott Long 		       tag->flags & 0x7);
120751a7b740SScott Long 		return (ENODEV);
120851a7b740SScott Long 	}
120951a7b740SScott Long 
121051a7b740SScott Long 	*sector = lsector + udfmp->part_start;
121151a7b740SScott Long 
121251a7b740SScott Long 	/*
121351a7b740SScott Long 	 * Check the sparing table.  Each entry represents the beginning of
121451a7b740SScott Long 	 * a packet.
121551a7b740SScott Long 	 */
121651a7b740SScott Long 	if (udfmp->s_table != NULL) {
121751a7b740SScott Long 		for (i = 0; i< udfmp->s_table_entries; i++) {
1218bf1c3dddSScott Long 			p_offset =
1219bf1c3dddSScott Long 			    lsector - le32toh(udfmp->s_table->entries[i].org);
122051a7b740SScott Long 			if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1221bf1c3dddSScott Long 				*sector =
1222bf1c3dddSScott Long 				   le32toh(udfmp->s_table->entries[i].map) +
122351a7b740SScott Long 				    p_offset;
122451a7b740SScott Long 				break;
122551a7b740SScott Long 			}
122651a7b740SScott Long 		}
122751a7b740SScott Long 	}
122851a7b740SScott Long 
122951a7b740SScott Long 	return (0);
123051a7b740SScott Long }
1231