xref: /freebsd/sys/fs/udf/udf_vnops.c (revision e3024df2e01dfaaa1925c5a1049c9af79b2c561e)
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 
5161e69c80SJohn Baldwin #include <fs/fifofs/fifo.h>
5251a7b740SScott Long #include <fs/udf/ecma167-udf.h>
5351a7b740SScott Long #include <fs/udf/osta.h>
5451a7b740SScott Long #include <fs/udf/udf.h>
556565282cSScott Long #include <fs/udf/udf_mount.h>
566565282cSScott Long 
576565282cSScott Long extern struct iconv_functions *udf_iconv;
5851a7b740SScott Long 
596fde64c7SPoul-Henning Kamp static vop_access_t	udf_access;
606fde64c7SPoul-Henning Kamp static vop_getattr_t	udf_getattr;
6135e06624SPav Lucistnik static vop_open_t	udf_open;
626fde64c7SPoul-Henning Kamp static vop_ioctl_t	udf_ioctl;
636fde64c7SPoul-Henning Kamp static vop_pathconf_t	udf_pathconf;
6461e69c80SJohn Baldwin static vop_print_t	udf_print;
656fde64c7SPoul-Henning Kamp static vop_read_t	udf_read;
666fde64c7SPoul-Henning Kamp static vop_readdir_t	udf_readdir;
676fde64c7SPoul-Henning Kamp static vop_readlink_t	udf_readlink;
6861e69c80SJohn Baldwin static vop_setattr_t	udf_setattr;
696fde64c7SPoul-Henning Kamp static vop_strategy_t	udf_strategy;
706fde64c7SPoul-Henning Kamp static vop_bmap_t	udf_bmap;
716fde64c7SPoul-Henning Kamp static vop_cachedlookup_t	udf_lookup;
726fde64c7SPoul-Henning Kamp static vop_reclaim_t	udf_reclaim;
7310bcafe9SPawel Jakub Dawidek static vop_vptofh_t	udf_vptofh;
749d32fde8SScott Long static int udf_readatoffset(struct udf_node *node, int *size, off_t offset,
759d32fde8SScott Long     struct buf **bp, uint8_t **data);
769d32fde8SScott Long static int udf_bmap_internal(struct udf_node *node, off_t offset,
779d32fde8SScott Long     daddr_t *sector, uint32_t *max_size);
7851a7b740SScott Long 
79aec0fb7bSPoul-Henning Kamp static struct vop_vector udf_vnodeops = {
80aec0fb7bSPoul-Henning Kamp 	.vop_default =		&default_vnodeops,
8183c64397SPoul-Henning Kamp 
82aec0fb7bSPoul-Henning Kamp 	.vop_access =		udf_access,
83aec0fb7bSPoul-Henning Kamp 	.vop_bmap =		udf_bmap,
84aec0fb7bSPoul-Henning Kamp 	.vop_cachedlookup =	udf_lookup,
85aec0fb7bSPoul-Henning Kamp 	.vop_getattr =		udf_getattr,
86aec0fb7bSPoul-Henning Kamp 	.vop_ioctl =		udf_ioctl,
87aec0fb7bSPoul-Henning Kamp 	.vop_lookup =		vfs_cache_lookup,
8835e06624SPav Lucistnik 	.vop_open =		udf_open,
89aec0fb7bSPoul-Henning Kamp 	.vop_pathconf =		udf_pathconf,
9061e69c80SJohn Baldwin 	.vop_print =		udf_print,
91aec0fb7bSPoul-Henning Kamp 	.vop_read =		udf_read,
92aec0fb7bSPoul-Henning Kamp 	.vop_readdir =		udf_readdir,
93aec0fb7bSPoul-Henning Kamp 	.vop_readlink =		udf_readlink,
94aec0fb7bSPoul-Henning Kamp 	.vop_reclaim =		udf_reclaim,
9561e69c80SJohn Baldwin 	.vop_setattr =		udf_setattr,
96aec0fb7bSPoul-Henning Kamp 	.vop_strategy =		udf_strategy,
9710bcafe9SPawel Jakub Dawidek 	.vop_vptofh =		udf_vptofh,
9851a7b740SScott Long };
9951a7b740SScott Long 
10061e69c80SJohn Baldwin struct vop_vector udf_fifoops = {
10161e69c80SJohn Baldwin 	.vop_default =		&fifo_specops,
10261e69c80SJohn Baldwin 	.vop_access =		udf_access,
10361e69c80SJohn Baldwin 	.vop_getattr =		udf_getattr,
10461e69c80SJohn Baldwin 	.vop_print =		udf_print,
10561e69c80SJohn Baldwin 	.vop_reclaim =		udf_reclaim,
10661e69c80SJohn Baldwin 	.vop_setattr =		udf_setattr,
10761e69c80SJohn Baldwin 	.vop_vptofh =		udf_vptofh,
10861e69c80SJohn Baldwin };
10961e69c80SJohn Baldwin 
1105bb84bc8SRobert Watson MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure");
1115bb84bc8SRobert Watson MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure");
11251a7b740SScott Long 
1134576293dSScott Long #define UDF_INVALID_BMAP	-1
1148db4c2f2SScott Long 
11551a7b740SScott Long int
11651a7b740SScott Long udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td)
11751a7b740SScott Long {
11851a7b740SScott Long 	int error;
11951a7b740SScott Long 	struct vnode *vp;
12051a7b740SScott Long 
121aec0fb7bSPoul-Henning Kamp 	error = getnewvnode("udf", mp, &udf_vnodeops, &vp);
12251a7b740SScott Long 	if (error) {
12351a7b740SScott Long 		printf("udf_allocv: failed to allocate new vnode\n");
12451a7b740SScott Long 		return (error);
12551a7b740SScott Long 	}
12651a7b740SScott Long 
12751a7b740SScott Long 	*vpp = vp;
12851a7b740SScott Long 	return (0);
12951a7b740SScott Long }
13051a7b740SScott Long 
13151a7b740SScott Long /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
13251a7b740SScott Long static mode_t
13351a7b740SScott Long udf_permtomode(struct udf_node *node)
13451a7b740SScott Long {
135c2d6947dSJeroen Ruigrok van der Werven 	uint32_t perm;
136bf1c3dddSScott Long 	uint16_t flags;
13751a7b740SScott Long 	mode_t mode;
13851a7b740SScott Long 
139bf1c3dddSScott Long 	perm = le32toh(node->fentry->perm);
140bf1c3dddSScott Long 	flags = le16toh(node->fentry->icbtag.flags);
14151a7b740SScott Long 
14251a7b740SScott Long 	mode = perm & UDF_FENTRY_PERM_USER_MASK;
14351a7b740SScott Long 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
14451a7b740SScott Long 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
14551a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
14651a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
14751a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
14851a7b740SScott Long 
14951a7b740SScott Long 	return (mode);
15051a7b740SScott Long }
15151a7b740SScott Long 
15251a7b740SScott Long static int
15351a7b740SScott Long udf_access(struct vop_access_args *a)
15451a7b740SScott Long {
15551a7b740SScott Long 	struct vnode *vp;
15651a7b740SScott Long 	struct udf_node *node;
15715bc6b2bSEdward Tomasz Napierala 	accmode_t accmode;
15815bc6b2bSEdward Tomasz Napierala 	mode_t mode;
15951a7b740SScott Long 
16051a7b740SScott Long 	vp = a->a_vp;
16151a7b740SScott Long 	node = VTON(vp);
16215bc6b2bSEdward Tomasz Napierala 	accmode = a->a_accmode;
16351a7b740SScott Long 
16415bc6b2bSEdward Tomasz Napierala 	if (accmode & VWRITE) {
16551a7b740SScott Long 		switch (vp->v_type) {
16651a7b740SScott Long 		case VDIR:
16751a7b740SScott Long 		case VLNK:
16851a7b740SScott Long 		case VREG:
16951a7b740SScott Long 			return (EROFS);
17051a7b740SScott Long 			/* NOT REACHED */
17151a7b740SScott Long 		default:
17251a7b740SScott Long 			break;
17351a7b740SScott Long 		}
17451a7b740SScott Long 	}
17551a7b740SScott Long 
17651a7b740SScott Long 	mode = udf_permtomode(node);
17751a7b740SScott Long 
17851a7b740SScott Long 	return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid,
17915bc6b2bSEdward Tomasz Napierala 	    accmode, a->a_cred, NULL));
18051a7b740SScott Long }
18151a7b740SScott Long 
18235e06624SPav Lucistnik static int
18335e06624SPav Lucistnik udf_open(struct vop_open_args *ap) {
18435e06624SPav Lucistnik 	struct udf_node *np = VTON(ap->a_vp);
18535e06624SPav Lucistnik 	off_t fsize;
18635e06624SPav Lucistnik 
18735e06624SPav Lucistnik 	fsize = le64toh(np->fentry->inf_len);
18835e06624SPav Lucistnik 	vnode_create_vobject(ap->a_vp, fsize, ap->a_td);
18935e06624SPav Lucistnik 	return 0;
19035e06624SPav Lucistnik }
19135e06624SPav Lucistnik 
1929c2bf69dSMarkus Brueffer static const int mon_lens[2][12] = {
1939c2bf69dSMarkus Brueffer 	{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
1949c2bf69dSMarkus Brueffer 	{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
19551a7b740SScott Long };
19651a7b740SScott Long 
19751a7b740SScott Long static int
19851a7b740SScott Long udf_isaleapyear(int year)
19951a7b740SScott Long {
20051a7b740SScott Long 	int i;
20151a7b740SScott Long 
20251a7b740SScott Long 	i = (year % 4) ? 0 : 1;
20351a7b740SScott Long 	i &= (year % 100) ? 1 : 0;
20451a7b740SScott Long 	i |= (year % 400) ? 0 : 1;
20551a7b740SScott Long 
20651a7b740SScott Long 	return i;
20751a7b740SScott Long }
20851a7b740SScott Long 
20951a7b740SScott Long /*
21051a7b740SScott Long  * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
21151a7b740SScott Long  */
21251a7b740SScott Long static void
21351a7b740SScott Long udf_timetotimespec(struct timestamp *time, struct timespec *t)
21451a7b740SScott Long {
2159c2bf69dSMarkus Brueffer 	int i, lpyear, daysinyear, year, startyear;
21651a7b740SScott Long 	union {
217c2d6947dSJeroen Ruigrok van der Werven 		uint16_t	u_tz_offset;
21851a7b740SScott Long 		int16_t		s_tz_offset;
21951a7b740SScott Long 	} tz;
22051a7b740SScott Long 
2219c2bf69dSMarkus Brueffer 	/*
2229c2bf69dSMarkus Brueffer 	 * DirectCD seems to like using bogus year values.
2239c2bf69dSMarkus Brueffer 	 * Don't trust time->month as it will be used for an array index.
2249c2bf69dSMarkus Brueffer 	 */
225bf1c3dddSScott Long 	year = le16toh(time->year);
2269c2bf69dSMarkus Brueffer 	if (year < 1970 || time->month < 1 || time->month > 12) {
22751a7b740SScott Long 		t->tv_sec = 0;
2289c2bf69dSMarkus Brueffer 		t->tv_nsec = 0;
22951a7b740SScott Long 		return;
23051a7b740SScott Long 	}
23151a7b740SScott Long 
23251a7b740SScott Long 	/* Calculate the time and day */
23351a7b740SScott Long 	t->tv_sec = time->second;
23451a7b740SScott Long 	t->tv_sec += time->minute * 60;
23551a7b740SScott Long 	t->tv_sec += time->hour * 3600;
2369c2bf69dSMarkus Brueffer 	t->tv_sec += (time->day - 1) * 3600 * 24;
23751a7b740SScott Long 
238befb7f33SChristian Brueffer 	/* Calculate the month */
239bf1c3dddSScott Long 	lpyear = udf_isaleapyear(year);
2409c2bf69dSMarkus Brueffer 	t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24;
24151a7b740SScott Long 
24251a7b740SScott Long 	/* Speed up the calculation */
2439c2bf69dSMarkus Brueffer 	startyear = 1970;
2449c2bf69dSMarkus Brueffer 	if (year > 2009) {
2459c2bf69dSMarkus Brueffer 		t->tv_sec += 1262304000;
2469c2bf69dSMarkus Brueffer 		startyear += 40;
2479c2bf69dSMarkus Brueffer 	} else if (year > 1999) {
2489c2bf69dSMarkus Brueffer 		t->tv_sec += 946684800;
2499c2bf69dSMarkus Brueffer 		startyear += 30;
2509c2bf69dSMarkus Brueffer 	} else if (year > 1989) {
2519c2bf69dSMarkus Brueffer 		t->tv_sec += 631152000;
2529c2bf69dSMarkus Brueffer 		startyear += 20;
2539c2bf69dSMarkus Brueffer 	} else if (year > 1979) {
25451a7b740SScott Long 		t->tv_sec += 315532800;
2559c2bf69dSMarkus Brueffer 		startyear += 10;
25651a7b740SScott Long 	}
25751a7b740SScott Long 
2589c2bf69dSMarkus Brueffer 	daysinyear = (year - startyear) * 365;
2599c2bf69dSMarkus Brueffer 	for (i = startyear; i < year; i++)
2609c2bf69dSMarkus Brueffer 		daysinyear += udf_isaleapyear(i);
2619c2bf69dSMarkus Brueffer 	t->tv_sec += daysinyear * 3600 * 24;
2629c2bf69dSMarkus Brueffer 
2639c2bf69dSMarkus Brueffer 	/* Calculate microseconds */
2649c2bf69dSMarkus Brueffer 	t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 +
2659c2bf69dSMarkus Brueffer 	    time->usec;
2669c2bf69dSMarkus Brueffer 
26751a7b740SScott Long 	/*
26851a7b740SScott Long 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
269befb7f33SChristian Brueffer 	 * complement, so we gotta do some extra magic to handle it right.
27051a7b740SScott Long 	 */
271bf1c3dddSScott Long 	tz.u_tz_offset = le16toh(time->type_tz);
27251a7b740SScott Long 	tz.u_tz_offset &= 0x0fff;
27351a7b740SScott Long 	if (tz.u_tz_offset & 0x0800)
27451a7b740SScott Long 		tz.u_tz_offset |= 0xf000;	/* extend the sign to 16 bits */
2759c2bf69dSMarkus Brueffer 	if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047))
27651a7b740SScott Long 		t->tv_sec -= tz.s_tz_offset * 60;
27751a7b740SScott Long 
27851a7b740SScott Long 	return;
27951a7b740SScott Long }
28051a7b740SScott Long 
28151a7b740SScott Long static int
28251a7b740SScott Long udf_getattr(struct vop_getattr_args *a)
28351a7b740SScott Long {
28451a7b740SScott Long 	struct vnode *vp;
28551a7b740SScott Long 	struct udf_node *node;
28651a7b740SScott Long 	struct vattr *vap;
28751a7b740SScott Long 	struct file_entry *fentry;
28851a7b740SScott Long 	struct timespec ts;
28951a7b740SScott Long 
29051a7b740SScott Long 	ts.tv_sec = 0;
29151a7b740SScott Long 
29251a7b740SScott Long 	vp = a->a_vp;
29351a7b740SScott Long 	vap = a->a_vap;
29451a7b740SScott Long 	node = VTON(vp);
29551a7b740SScott Long 	fentry = node->fentry;
29651a7b740SScott Long 
297c049546eSPoul-Henning Kamp 	vap->va_fsid = dev2udev(node->udfmp->im_dev);
29851a7b740SScott Long 	vap->va_fileid = node->hash_id;
29951a7b740SScott Long 	vap->va_mode = udf_permtomode(node);
300bf1c3dddSScott Long 	vap->va_nlink = le16toh(fentry->link_cnt);
30151a7b740SScott Long 	/*
30251a7b740SScott Long 	 * XXX The spec says that -1 is valid for uid/gid and indicates an
30351a7b740SScott Long 	 * invalid uid/gid.  How should this be represented?
30451a7b740SScott Long 	 */
305bf1c3dddSScott Long 	vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid);
306bf1c3dddSScott Long 	vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid);
30751a7b740SScott Long 	udf_timetotimespec(&fentry->atime, &vap->va_atime);
30851a7b740SScott Long 	udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
30951a7b740SScott Long 	vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
3104c5a20e3SKonstantin Belousov 	vap->va_rdev = NODEV;
31151a7b740SScott Long 	if (vp->v_type & VDIR) {
31251a7b740SScott Long 		/*
31351a7b740SScott Long 		 * Directories that are recorded within their ICB will show
31451a7b740SScott Long 		 * as having 0 blocks recorded.  Since tradition dictates
31551a7b740SScott Long 		 * that directories consume at least one logical block,
31651a7b740SScott Long 		 * make it appear so.
31751a7b740SScott Long 		 */
31851a7b740SScott Long 		if (fentry->logblks_rec != 0) {
319bf1c3dddSScott Long 			vap->va_size =
320bf1c3dddSScott Long 			    le64toh(fentry->logblks_rec) * node->udfmp->bsize;
32151a7b740SScott Long 		} else {
32251a7b740SScott Long 			vap->va_size = node->udfmp->bsize;
32351a7b740SScott Long 		}
32451a7b740SScott Long 	} else {
325bf1c3dddSScott Long 		vap->va_size = le64toh(fentry->inf_len);
32651a7b740SScott Long 	}
32751a7b740SScott Long 	vap->va_flags = 0;
32851a7b740SScott Long 	vap->va_gen = 1;
32951a7b740SScott Long 	vap->va_blocksize = node->udfmp->bsize;
330bf1c3dddSScott Long 	vap->va_bytes = le64toh(fentry->inf_len);
33151a7b740SScott Long 	vap->va_type = vp->v_type;
33251a7b740SScott Long 	vap->va_filerev = 0; /* XXX */
33351a7b740SScott Long 	return (0);
33451a7b740SScott Long }
33551a7b740SScott Long 
33661e69c80SJohn Baldwin static int
33761e69c80SJohn Baldwin udf_setattr(struct vop_setattr_args *a)
33861e69c80SJohn Baldwin {
33961e69c80SJohn Baldwin 	struct vnode *vp;
34061e69c80SJohn Baldwin 	struct vattr *vap;
34161e69c80SJohn Baldwin 
34261e69c80SJohn Baldwin 	vp = a->a_vp;
34361e69c80SJohn Baldwin 	vap = a->a_vap;
34461e69c80SJohn Baldwin 	if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
34561e69c80SJohn Baldwin 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
34661e69c80SJohn Baldwin 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
34761e69c80SJohn Baldwin 		return (EROFS);
34861e69c80SJohn Baldwin 	if (vap->va_size != (u_quad_t)VNOVAL) {
34961e69c80SJohn Baldwin 		switch (vp->v_type) {
35061e69c80SJohn Baldwin 		case VDIR:
35161e69c80SJohn Baldwin 			return (EISDIR);
35261e69c80SJohn Baldwin 		case VLNK:
35361e69c80SJohn Baldwin 		case VREG:
35461e69c80SJohn Baldwin 			return (EROFS);
35561e69c80SJohn Baldwin 		case VCHR:
35661e69c80SJohn Baldwin 		case VBLK:
35761e69c80SJohn Baldwin 		case VSOCK:
35861e69c80SJohn Baldwin 		case VFIFO:
35961e69c80SJohn Baldwin 		case VNON:
36061e69c80SJohn Baldwin 		case VBAD:
36161e69c80SJohn Baldwin 		case VMARKER:
36261e69c80SJohn Baldwin 			return (0);
36361e69c80SJohn Baldwin 		}
36461e69c80SJohn Baldwin 	}
36561e69c80SJohn Baldwin 	return (0);
36661e69c80SJohn Baldwin }
36761e69c80SJohn Baldwin 
36851a7b740SScott Long /*
369bf1c3dddSScott Long  * File specific ioctls.
37051a7b740SScott Long  */
37151a7b740SScott Long static int
37251a7b740SScott Long udf_ioctl(struct vop_ioctl_args *a)
37351a7b740SScott Long {
374c80a90c5SScott Long 	printf("%s called\n", __func__);
3751d02d910SPoul-Henning Kamp 	return (ENOTTY);
37651a7b740SScott Long }
37751a7b740SScott Long 
37851a7b740SScott Long /*
37951a7b740SScott Long  * I'm not sure that this has much value in a read-only filesystem, but
38051a7b740SScott Long  * cd9660 has it too.
38151a7b740SScott Long  */
38251a7b740SScott Long static int
38351a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a)
38451a7b740SScott Long {
38551a7b740SScott Long 
38651a7b740SScott Long 	switch (a->a_name) {
38751a7b740SScott Long 	case _PC_LINK_MAX:
38851a7b740SScott Long 		*a->a_retval = 65535;
38951a7b740SScott Long 		return (0);
39051a7b740SScott Long 	case _PC_NAME_MAX:
39151a7b740SScott Long 		*a->a_retval = NAME_MAX;
39251a7b740SScott Long 		return (0);
39351a7b740SScott Long 	case _PC_PATH_MAX:
39451a7b740SScott Long 		*a->a_retval = PATH_MAX;
39551a7b740SScott Long 		return (0);
39651a7b740SScott Long 	case _PC_NO_TRUNC:
39751a7b740SScott Long 		*a->a_retval = 1;
39851a7b740SScott Long 		return (0);
39951a7b740SScott Long 	default:
40051a7b740SScott Long 		return (EINVAL);
40151a7b740SScott Long 	}
40251a7b740SScott Long }
40351a7b740SScott Long 
40461e69c80SJohn Baldwin static int
40561e69c80SJohn Baldwin udf_print(struct vop_print_args *ap)
40661e69c80SJohn Baldwin {
40761e69c80SJohn Baldwin 	struct vnode *vp = ap->a_vp;
40861e69c80SJohn Baldwin 	struct udf_node *node = VTON(vp);
40961e69c80SJohn Baldwin 
41061e69c80SJohn Baldwin 	printf("    ino %lu, on dev %s", (u_long)node->hash_id,
41161e69c80SJohn Baldwin 	    devtoname(node->udfmp->im_dev));
41261e69c80SJohn Baldwin 	if (vp->v_type == VFIFO)
41361e69c80SJohn Baldwin 		fifo_printinfo(vp);
41461e69c80SJohn Baldwin 	printf("\n");
41561e69c80SJohn Baldwin 	return (0);
41661e69c80SJohn Baldwin }
41761e69c80SJohn Baldwin 
4180c09ac0dSPav Lucistnik #define lblkno(udfmp, loc)	((loc) >> (udfmp)->bshift)
4190c09ac0dSPav Lucistnik #define blkoff(udfmp, loc)	((loc) & (udfmp)->bmask)
4200c09ac0dSPav Lucistnik #define lblktosize(imp, blk)	((blk) << (udfmp)->bshift)
42151a7b740SScott Long 
4220c09ac0dSPav Lucistnik static int
4230c09ac0dSPav Lucistnik udf_read(struct vop_read_args *ap)
4240c09ac0dSPav Lucistnik {
4250c09ac0dSPav Lucistnik 	struct vnode *vp = ap->a_vp;
4260c09ac0dSPav Lucistnik 	struct uio *uio = ap->a_uio;
4270c09ac0dSPav Lucistnik 	struct udf_node *node = VTON(vp);
4280c09ac0dSPav Lucistnik 	struct udf_mnt *udfmp;
4290c09ac0dSPav Lucistnik 	struct buf *bp;
4300c09ac0dSPav Lucistnik 	daddr_t lbn, rablock;
4310c09ac0dSPav Lucistnik 	off_t diff, fsize;
4320c09ac0dSPav Lucistnik 	int error = 0;
4330c09ac0dSPav Lucistnik 	long size, n, on;
4340c09ac0dSPav Lucistnik 
4350c09ac0dSPav Lucistnik 	if (uio->uio_resid == 0)
4360c09ac0dSPav Lucistnik 		return (0);
43751a7b740SScott Long 	if (uio->uio_offset < 0)
43851a7b740SScott Long 		return (EINVAL);
439bf1c3dddSScott Long 	fsize = le64toh(node->fentry->inf_len);
4400c09ac0dSPav Lucistnik 	udfmp = node->udfmp;
4410c09ac0dSPav Lucistnik 	do {
4420c09ac0dSPav Lucistnik 		lbn = lblkno(udfmp, uio->uio_offset);
4430c09ac0dSPav Lucistnik 		on = blkoff(udfmp, uio->uio_offset);
4440c09ac0dSPav Lucistnik 		n = min((u_int)(udfmp->bsize - on),
4450c09ac0dSPav Lucistnik 			uio->uio_resid);
4460c09ac0dSPav Lucistnik 		diff = fsize - uio->uio_offset;
4470c09ac0dSPav Lucistnik 		if (diff <= 0)
4480c09ac0dSPav Lucistnik 			return (0);
4490c09ac0dSPav Lucistnik 		if (diff < n)
4500c09ac0dSPav Lucistnik 			n = diff;
4510c09ac0dSPav Lucistnik 		size = udfmp->bsize;
4520c09ac0dSPav Lucistnik 		rablock = lbn + 1;
4530c09ac0dSPav Lucistnik 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
4540c09ac0dSPav Lucistnik 			if (lblktosize(udfmp, rablock) < fsize) {
4550c09ac0dSPav Lucistnik 				error = cluster_read(vp, fsize, lbn, size, NOCRED,
4560c09ac0dSPav Lucistnik 					uio->uio_resid, (ap->a_ioflag >> 16), &bp);
4570c09ac0dSPav Lucistnik 			} else {
4580c09ac0dSPav Lucistnik 				error = bread(vp, lbn, size, NOCRED, &bp);
4590c09ac0dSPav Lucistnik 			}
4600c09ac0dSPav Lucistnik 		} else {
4610c09ac0dSPav Lucistnik 			error = bread(vp, lbn, size, NOCRED, &bp);
4620c09ac0dSPav Lucistnik 		}
4630c09ac0dSPav Lucistnik 		n = min(n, size - bp->b_resid);
4640c09ac0dSPav Lucistnik 		if (error) {
46551a7b740SScott Long 			brelse(bp);
4660c09ac0dSPav Lucistnik 			return (error);
4670c09ac0dSPav Lucistnik 		}
46851a7b740SScott Long 
4690c09ac0dSPav Lucistnik 		error = uiomove(bp->b_data + on, (int)n, uio);
4700c09ac0dSPav Lucistnik 		brelse(bp);
4710c09ac0dSPav Lucistnik 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
47251a7b740SScott Long 	return (error);
47351a7b740SScott Long }
47451a7b740SScott Long 
47551a7b740SScott Long /*
47651a7b740SScott Long  * Call the OSTA routines to translate the name from a CS0 dstring to a
47751a7b740SScott Long  * 16-bit Unicode String.  Hooks need to be placed in here to translate from
4786565282cSScott Long  * Unicode to the encoding that the kernel/user expects.  Return the length
4796565282cSScott Long  * of the translated string.
48051a7b740SScott Long  */
48151a7b740SScott Long static int
4826565282cSScott Long udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
48351a7b740SScott Long {
48451a7b740SScott Long 	unicode_t *transname;
4856565282cSScott Long 	char *unibuf, *unip;
486181fc3c6SR. Imura 	int i, destlen;
487181fc3c6SR. Imura 	ssize_t unilen = 0;
4886565282cSScott Long 	size_t destleft = MAXNAMLEN;
48951a7b740SScott Long 
4906565282cSScott Long 	/* Convert 16-bit Unicode to destname */
4916565282cSScott Long 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
4926565282cSScott Long 		/* allocate a buffer big enough to hold an 8->16 bit expansion */
4936565282cSScott Long 		unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
4946565282cSScott Long 		unip = unibuf;
495181fc3c6SR. Imura 		if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
4966565282cSScott Long 			printf("udf: Unicode translation failed\n");
4976565282cSScott Long 			uma_zfree(udf_zone_trans, unibuf);
4986565282cSScott Long 			return 0;
4996565282cSScott Long 		}
5006565282cSScott Long 
5016565282cSScott Long 		while (unilen > 0 && destleft > 0) {
5026565282cSScott Long 			udf_iconv->conv(udfmp->im_d2l, (const char **)&unibuf,
5036565282cSScott Long 				(size_t *)&unilen, (char **)&destname, &destleft);
5046565282cSScott Long 			/* Unconverted character found */
5056565282cSScott Long 			if (unilen > 0 && destleft > 0) {
5066565282cSScott Long 				*destname++ = '?';
5076565282cSScott Long 				destleft--;
5086565282cSScott Long 				unibuf += 2;
5096565282cSScott Long 				unilen -= 2;
5106565282cSScott Long 			}
5116565282cSScott Long 		}
5126565282cSScott Long 		uma_zfree(udf_zone_trans, unip);
5136565282cSScott Long 		*destname = '\0';
5146565282cSScott Long 		destlen = MAXNAMLEN - (int)destleft;
5156565282cSScott Long 	} else {
51651a7b740SScott Long 		/* allocate a buffer big enough to hold an 8->16 bit expansion */
517a163d034SWarner Losh 		transname = uma_zalloc(udf_zone_trans, M_WAITOK);
51851a7b740SScott Long 
519181fc3c6SR. Imura 		if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) {
52051a7b740SScott Long 			printf("udf: Unicode translation failed\n");
52151a7b740SScott Long 			uma_zfree(udf_zone_trans, transname);
52251a7b740SScott Long 			return 0;
52351a7b740SScott Long 		}
52451a7b740SScott Long 
52551a7b740SScott Long 		for (i = 0; i < unilen ; i++) {
52651a7b740SScott Long 			if (transname[i] & 0xff00) {
52751a7b740SScott Long 				destname[i] = '.';	/* Fudge the 16bit chars */
52851a7b740SScott Long 			} else {
52951a7b740SScott Long 				destname[i] = transname[i] & 0xff;
53051a7b740SScott Long 			}
53151a7b740SScott Long 		}
53251a7b740SScott Long 		uma_zfree(udf_zone_trans, transname);
5336565282cSScott Long 		destname[unilen] = 0;
534181fc3c6SR. Imura 		destlen = (int)unilen;
5356565282cSScott Long 	}
53651a7b740SScott Long 
5376565282cSScott Long 	return (destlen);
53851a7b740SScott Long }
53951a7b740SScott Long 
54051a7b740SScott Long /*
54151a7b740SScott Long  * Compare a CS0 dstring with a name passed in from the VFS layer.  Return
542befb7f33SChristian Brueffer  * 0 on a successful match, nonzero otherwise.  Unicode work may need to be done
54351a7b740SScott Long  * here also.
54451a7b740SScott Long  */
54551a7b740SScott Long static int
5466565282cSScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
54751a7b740SScott Long {
54895ec5961SScott Long 	char *transname;
54995ec5961SScott Long 	int error = 0;
55051a7b740SScott Long 
55195ec5961SScott Long 	/* This is overkill, but not worth creating a new zone */
552a163d034SWarner Losh 	transname = uma_zalloc(udf_zone_trans, M_WAITOK);
55395ec5961SScott Long 
5546565282cSScott Long 	cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
55551a7b740SScott Long 
55651a7b740SScott Long 	/* Easy check.  If they aren't the same length, they aren't equal */
55795ec5961SScott Long 	if ((cs0len == 0) || (cs0len != cmplen))
55895ec5961SScott Long 		error = -1;
55995ec5961SScott Long 	else
56095ec5961SScott Long 		error = bcmp(transname, cmpname, cmplen);
56151a7b740SScott Long 
56295ec5961SScott Long 	uma_zfree(udf_zone_trans, transname);
56395ec5961SScott Long 	return (error);
56451a7b740SScott Long }
56551a7b740SScott Long 
56651a7b740SScott Long struct udf_uiodir {
56751a7b740SScott Long 	struct dirent *dirent;
56851a7b740SScott Long 	u_long *cookies;
56951a7b740SScott Long 	int ncookies;
57051a7b740SScott Long 	int acookies;
57151a7b740SScott Long 	int eofflag;
57251a7b740SScott Long };
57351a7b740SScott Long 
57451a7b740SScott Long static int
57551a7b740SScott Long udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
57651a7b740SScott Long {
57751a7b740SScott Long 	if (uiodir->cookies != NULL) {
57851a7b740SScott Long 		if (++uiodir->acookies > uiodir->ncookies) {
57951a7b740SScott Long 			uiodir->eofflag = 0;
58051a7b740SScott Long 			return (-1);
58151a7b740SScott Long 		}
58251a7b740SScott Long 		*uiodir->cookies++ = cookie;
58351a7b740SScott Long 	}
58451a7b740SScott Long 
58551a7b740SScott Long 	if (uio->uio_resid < de_size) {
58651a7b740SScott Long 		uiodir->eofflag = 0;
58751a7b740SScott Long 		return (-1);
58851a7b740SScott Long 	}
58951a7b740SScott Long 
590c9524588SDag-Erling Smørgrav 	return (uiomove(uiodir->dirent, de_size, uio));
59151a7b740SScott Long }
59251a7b740SScott Long 
5931703656aSScott Long static struct udf_dirstream *
5941703656aSScott Long udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
5951703656aSScott Long {
5961703656aSScott Long 	struct udf_dirstream *ds;
5971703656aSScott Long 
598a163d034SWarner Losh 	ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
5991703656aSScott Long 
6001703656aSScott Long 	ds->node = node;
6011703656aSScott Long 	ds->offset = offset;
6021703656aSScott Long 	ds->udfmp = udfmp;
6031703656aSScott Long 	ds->fsize = fsize;
6041703656aSScott Long 
6051703656aSScott Long 	return (ds);
6061703656aSScott Long }
6071703656aSScott Long 
6081703656aSScott Long static struct fileid_desc *
6091703656aSScott Long udf_getfid(struct udf_dirstream *ds)
6101703656aSScott Long {
6111703656aSScott Long 	struct fileid_desc *fid;
6121703656aSScott Long 	int error, frag_size = 0, total_fid_size;
6131703656aSScott Long 
6141703656aSScott Long 	/* End of directory? */
6151703656aSScott Long 	if (ds->offset + ds->off >= ds->fsize) {
6161703656aSScott Long 		ds->error = 0;
6171703656aSScott Long 		return (NULL);
6181703656aSScott Long 	}
6191703656aSScott Long 
6201703656aSScott Long 	/* Grab the first extent of the directory */
6211703656aSScott Long 	if (ds->off == 0) {
6221703656aSScott Long 		ds->size = 0;
6231703656aSScott Long 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
6241703656aSScott Long 		    &ds->bp, &ds->data);
6251703656aSScott Long 		if (error) {
6261703656aSScott Long 			ds->error = error;
627744bb56dSScott Long 			if (ds->bp != NULL)
628744bb56dSScott Long 				brelse(ds->bp);
6291703656aSScott Long 			return (NULL);
6301703656aSScott Long 		}
6311703656aSScott Long 	}
6321703656aSScott Long 
6334576293dSScott Long 	/*
6344576293dSScott Long 	 * Clean up from a previous fragmented FID.
6354576293dSScott Long 	 * XXX Is this the right place for this?
6364576293dSScott Long 	 */
6371703656aSScott Long 	if (ds->fid_fragment && ds->buf != NULL) {
6381703656aSScott Long 		ds->fid_fragment = 0;
6391ede983cSDag-Erling Smørgrav 		free(ds->buf, M_UDFFID);
6401703656aSScott Long 	}
6411703656aSScott Long 
6421703656aSScott Long 	fid = (struct fileid_desc*)&ds->data[ds->off];
6431703656aSScott Long 
6441703656aSScott Long 	/*
6451703656aSScott Long 	 * Check to see if the fid is fragmented. The first test
6461703656aSScott Long 	 * ensures that we don't wander off the end of the buffer
6471703656aSScott Long 	 * looking for the l_iu and l_fi fields.
6481703656aSScott Long 	 */
6491703656aSScott Long 	if (ds->off + UDF_FID_SIZE > ds->size ||
650bf1c3dddSScott Long 	    ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){
6511703656aSScott Long 
6521703656aSScott Long 		/* Copy what we have of the fid into a buffer */
6531703656aSScott Long 		frag_size = ds->size - ds->off;
6541703656aSScott Long 		if (frag_size >= ds->udfmp->bsize) {
6551703656aSScott Long 			printf("udf: invalid FID fragment\n");
6561703656aSScott Long 			ds->error = EINVAL;
6571703656aSScott Long 			return (NULL);
6581703656aSScott Long 		}
6591703656aSScott Long 
6601703656aSScott Long 		/*
6611703656aSScott Long 		 * File ID descriptors can only be at most one
6621703656aSScott Long 		 * logical sector in size.
6631703656aSScott Long 		 */
6641ede983cSDag-Erling Smørgrav 		ds->buf = malloc(ds->udfmp->bsize, M_UDFFID,
665a163d034SWarner Losh 		     M_WAITOK | M_ZERO);
6661703656aSScott Long 		bcopy(fid, ds->buf, frag_size);
6671703656aSScott Long 
6681703656aSScott Long 		/* Reduce all of the casting magic */
6691703656aSScott Long 		fid = (struct fileid_desc*)ds->buf;
6701703656aSScott Long 
6711703656aSScott Long 		if (ds->bp != NULL)
6721703656aSScott Long 			brelse(ds->bp);
6731703656aSScott Long 
6741703656aSScott Long 		/* Fetch the next allocation */
6751703656aSScott Long 		ds->offset += ds->size;
6761703656aSScott Long 		ds->size = 0;
6771703656aSScott Long 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
6781703656aSScott Long 		    &ds->bp, &ds->data);
6791703656aSScott Long 		if (error) {
6801703656aSScott Long 			ds->error = error;
6811703656aSScott Long 			return (NULL);
6821703656aSScott Long 		}
6831703656aSScott Long 
6841703656aSScott Long 		/*
6851703656aSScott Long 		 * If the fragment was so small that we didn't get
6861703656aSScott Long 		 * the l_iu and l_fi fields, copy those in.
6871703656aSScott Long 		 */
6881703656aSScott Long 		if (frag_size < UDF_FID_SIZE)
6891703656aSScott Long 			bcopy(ds->data, &ds->buf[frag_size],
6901703656aSScott Long 			    UDF_FID_SIZE - frag_size);
6911703656aSScott Long 
6921703656aSScott Long 		/*
6931703656aSScott Long 		 * Now that we have enough of the fid to work with,
6941703656aSScott Long 		 * copy in the rest of the fid from the new
6951703656aSScott Long 		 * allocation.
6961703656aSScott Long 		 */
697bf1c3dddSScott Long 		total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi;
6981703656aSScott Long 		if (total_fid_size > ds->udfmp->bsize) {
6991703656aSScott Long 			printf("udf: invalid FID\n");
7001703656aSScott Long 			ds->error = EIO;
7011703656aSScott Long 			return (NULL);
7021703656aSScott Long 		}
7031703656aSScott Long 		bcopy(ds->data, &ds->buf[frag_size],
7041703656aSScott Long 		    total_fid_size - frag_size);
7051703656aSScott Long 
7061703656aSScott Long 		ds->fid_fragment = 1;
7071703656aSScott Long 	} else {
708bf1c3dddSScott Long 		total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE;
7091703656aSScott Long 	}
7101703656aSScott Long 
7111703656aSScott Long 	/*
7121703656aSScott Long 	 * Update the offset. Align on a 4 byte boundary because the
7134576293dSScott Long 	 * UDF spec says so.
7141703656aSScott Long 	 */
7151703656aSScott Long 	ds->this_off = ds->off;
7161703656aSScott Long 	if (!ds->fid_fragment) {
7171703656aSScott Long 		ds->off += (total_fid_size + 3) & ~0x03;
7181703656aSScott Long 	} else {
7191703656aSScott Long 		ds->off = (total_fid_size - frag_size + 3) & ~0x03;
7201703656aSScott Long 	}
7211703656aSScott Long 
7221703656aSScott Long 	return (fid);
7231703656aSScott Long }
7241703656aSScott Long 
7251703656aSScott Long static void
7261703656aSScott Long udf_closedir(struct udf_dirstream *ds)
7271703656aSScott Long {
7281703656aSScott Long 
7291703656aSScott Long 	if (ds->bp != NULL)
7301703656aSScott Long 		brelse(ds->bp);
7311703656aSScott Long 
7321703656aSScott Long 	if (ds->fid_fragment && ds->buf != NULL)
7331ede983cSDag-Erling Smørgrav 		free(ds->buf, M_UDFFID);
7341703656aSScott Long 
7351703656aSScott Long 	uma_zfree(udf_zone_ds, ds);
7361703656aSScott Long }
7371703656aSScott Long 
73851a7b740SScott Long static int
73951a7b740SScott Long udf_readdir(struct vop_readdir_args *a)
74051a7b740SScott Long {
74151a7b740SScott Long 	struct vnode *vp;
74251a7b740SScott Long 	struct uio *uio;
74351a7b740SScott Long 	struct dirent dir;
74451a7b740SScott Long 	struct udf_node *node;
7456565282cSScott Long 	struct udf_mnt *udfmp;
74651a7b740SScott Long 	struct fileid_desc *fid;
74751a7b740SScott Long 	struct udf_uiodir uiodir;
7481703656aSScott Long 	struct udf_dirstream *ds;
74951a7b740SScott Long 	u_long *cookies = NULL;
75051a7b740SScott Long 	int ncookies;
751c8eeea2fSScott Long 	int error = 0;
75251a7b740SScott Long 
75351a7b740SScott Long 	vp = a->a_vp;
75451a7b740SScott Long 	uio = a->a_uio;
75551a7b740SScott Long 	node = VTON(vp);
7566565282cSScott Long 	udfmp = node->udfmp;
75751a7b740SScott Long 	uiodir.eofflag = 1;
75851a7b740SScott Long 
75951a7b740SScott Long 	if (a->a_ncookies != NULL) {
76051a7b740SScott Long 		/*
76151a7b740SScott Long 		 * Guess how many entries are needed.  If we run out, this
76251a7b740SScott Long 		 * function will be called again and thing will pick up were
76351a7b740SScott Long 		 * it left off.
76451a7b740SScott Long 		 */
76551a7b740SScott Long 		ncookies = uio->uio_resid / 8;
7661ede983cSDag-Erling Smørgrav 		cookies = malloc(sizeof(u_long) * ncookies,
767a163d034SWarner Losh 		    M_TEMP, M_WAITOK);
76851a7b740SScott Long 		if (cookies == NULL)
76951a7b740SScott Long 			return (ENOMEM);
77051a7b740SScott Long 		uiodir.ncookies = ncookies;
77151a7b740SScott Long 		uiodir.cookies = cookies;
77251a7b740SScott Long 		uiodir.acookies = 0;
77351a7b740SScott Long 	} else {
77451a7b740SScott Long 		uiodir.cookies = NULL;
77551a7b740SScott Long 	}
77651a7b740SScott Long 
77751a7b740SScott Long 	/*
77851a7b740SScott Long 	 * Iterate through the file id descriptors.  Give the parent dir
7794576293dSScott Long 	 * entry special attention.
78051a7b740SScott Long 	 */
781bf1c3dddSScott Long 	ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
7821703656aSScott Long 	    node->udfmp);
78351a7b740SScott Long 
7841703656aSScott Long 	while ((fid = udf_getfid(ds)) != NULL) {
78551a7b740SScott Long 
78651a7b740SScott Long 		/* XXX Should we return an error on a bad fid? */
78751a7b740SScott Long 		if (udf_checktag(&fid->tag, TAGID_FID)) {
78851a7b740SScott Long 			printf("Invalid FID tag\n");
78977411499SScott Long 			hexdump(fid, UDF_FID_SIZE, NULL, 0);
7901703656aSScott Long 			error = EIO;
79151a7b740SScott Long 			break;
79251a7b740SScott Long 		}
79351a7b740SScott Long 
79451a7b740SScott Long 		/* Is this a deleted file? */
7952bbe0d36SScott Long 		if (fid->file_char & UDF_FILE_CHAR_DEL)
7961703656aSScott Long 			continue;
79751a7b740SScott Long 
7982bbe0d36SScott Long 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
79951a7b740SScott Long 			/* Do up the '.' and '..' entries.  Dummy values are
80051a7b740SScott Long 			 * used for the cookies since the offset here is
80151a7b740SScott Long 			 * usually zero, and NFS doesn't like that value
80251a7b740SScott Long 			 */
803c8eeea2fSScott Long 			dir.d_fileno = node->hash_id;
804c8eeea2fSScott Long 			dir.d_type = DT_DIR;
805c8eeea2fSScott Long 			dir.d_name[0] = '.';
806444acc16SScott Long 			dir.d_name[1] = '\0';
807c8eeea2fSScott Long 			dir.d_namlen = 1;
808c8eeea2fSScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
809c8eeea2fSScott Long 			uiodir.dirent = &dir;
810c8eeea2fSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
81151a7b740SScott Long 			if (error)
81251a7b740SScott Long 				break;
81351a7b740SScott Long 
814c8eeea2fSScott Long 			dir.d_fileno = udf_getid(&fid->icb);
815c8eeea2fSScott Long 			dir.d_type = DT_DIR;
816c8eeea2fSScott Long 			dir.d_name[0] = '.';
817c8eeea2fSScott Long 			dir.d_name[1] = '.';
818444acc16SScott Long 			dir.d_name[2] = '\0';
819c8eeea2fSScott Long 			dir.d_namlen = 2;
820c8eeea2fSScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
821c8eeea2fSScott Long 			uiodir.dirent = &dir;
822c8eeea2fSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
82351a7b740SScott Long 		} else {
82451a7b740SScott Long 			dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
8256565282cSScott Long 			    &dir.d_name[0], fid->l_fi, udfmp);
82651a7b740SScott Long 			dir.d_fileno = udf_getid(&fid->icb);
8272bbe0d36SScott Long 			dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
8282bbe0d36SScott Long 			    DT_DIR : DT_UNKNOWN;
82951a7b740SScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
83051a7b740SScott Long 			uiodir.dirent = &dir;
8311703656aSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio,
8321703656aSScott Long 			    ds->this_off);
83351a7b740SScott Long 		}
83451a7b740SScott Long 		if (error) {
83551a7b740SScott Long 			printf("uiomove returned %d\n", error);
83651a7b740SScott Long 			break;
83751a7b740SScott Long 		}
83851a7b740SScott Long 
83951a7b740SScott Long 	}
84051a7b740SScott Long 
84151a7b740SScott Long 	/* tell the calling layer whether we need to be called again */
84251a7b740SScott Long 	*a->a_eofflag = uiodir.eofflag;
8431703656aSScott Long 	uio->uio_offset = ds->offset + ds->off;
84451a7b740SScott Long 
8451703656aSScott Long 	if (!error)
8461703656aSScott Long 		error = ds->error;
8471703656aSScott Long 
8481703656aSScott Long 	udf_closedir(ds);
84951a7b740SScott Long 
85051a7b740SScott Long 	if (a->a_ncookies != NULL) {
85151a7b740SScott Long 		if (error)
8521ede983cSDag-Erling Smørgrav 			free(cookies, M_TEMP);
85351a7b740SScott Long 		else {
85451a7b740SScott Long 			*a->a_ncookies = uiodir.acookies;
85551a7b740SScott Long 			*a->a_cookies = cookies;
85651a7b740SScott Long 		}
85751a7b740SScott Long 	}
85851a7b740SScott Long 
85951a7b740SScott Long 	return (error);
86051a7b740SScott Long }
86151a7b740SScott Long 
86251a7b740SScott Long static int
86351a7b740SScott Long udf_readlink(struct vop_readlink_args *ap)
86451a7b740SScott Long {
865e3024df2SJohn Baldwin 	struct path_component *pc, *end;
866e3024df2SJohn Baldwin 	struct vnode *vp;
867e3024df2SJohn Baldwin 	struct uio uio;
868e3024df2SJohn Baldwin 	struct iovec iov[1];
869e3024df2SJohn Baldwin 	struct udf_node *node;
870e3024df2SJohn Baldwin 	void *buf;
871e3024df2SJohn Baldwin 	char *cp;
872e3024df2SJohn Baldwin 	int error, len, root;
873e3024df2SJohn Baldwin 
874e3024df2SJohn Baldwin 	/*
875e3024df2SJohn Baldwin 	 * A symbolic link in UDF is a list of variable-length path
876e3024df2SJohn Baldwin 	 * component structures.  We build a pathname in the caller's
877e3024df2SJohn Baldwin 	 * uio by traversing this list.
878e3024df2SJohn Baldwin 	 */
879e3024df2SJohn Baldwin 	vp = ap->a_vp;
880e3024df2SJohn Baldwin 	node = VTON(vp);
881e3024df2SJohn Baldwin 	len = le64toh(node->fentry->inf_len);
882e3024df2SJohn Baldwin 	buf = malloc(iov[0].iov_len, M_DEVBUF, M_WAITOK);
883e3024df2SJohn Baldwin 	iov[0].iov_base = buf;
884e3024df2SJohn Baldwin 	iov[0].iov_len = len;
885e3024df2SJohn Baldwin 	uio.uio_iov = iov;
886e3024df2SJohn Baldwin 	uio.uio_iovcnt = 1;
887e3024df2SJohn Baldwin 	uio.uio_offset = 0;
888e3024df2SJohn Baldwin 	uio.uio_resid = iov[0].iov_len;
889e3024df2SJohn Baldwin 	uio.uio_segflg = UIO_SYSSPACE;
890e3024df2SJohn Baldwin 	uio.uio_rw = UIO_READ;
891e3024df2SJohn Baldwin 	uio.uio_td = curthread;
892e3024df2SJohn Baldwin 	error = VOP_READ(vp, &uio, 0, ap->a_cred);
893e3024df2SJohn Baldwin 	if (error)
894e3024df2SJohn Baldwin 		goto error;
895e3024df2SJohn Baldwin 
896e3024df2SJohn Baldwin 	pc = buf;
897e3024df2SJohn Baldwin 	end = (void *)((char *)buf + len);
898e3024df2SJohn Baldwin 	root = 0;
899e3024df2SJohn Baldwin 	while (pc < end) {
900e3024df2SJohn Baldwin 		switch (pc->type) {
901e3024df2SJohn Baldwin 		case UDF_PATH_ROOT:
902e3024df2SJohn Baldwin 			/* Only allow this at the beginning of a path. */
903e3024df2SJohn Baldwin 			if ((void *)pc != buf) {
904e3024df2SJohn Baldwin 				error = EINVAL;
905e3024df2SJohn Baldwin 				goto error;
906e3024df2SJohn Baldwin 			}
907e3024df2SJohn Baldwin 			cp = "/";
908e3024df2SJohn Baldwin 			len = 1;
909e3024df2SJohn Baldwin 			root = 1;
910e3024df2SJohn Baldwin 			break;
911e3024df2SJohn Baldwin 		case UDF_PATH_DOT:
912e3024df2SJohn Baldwin 			cp = ".";
913e3024df2SJohn Baldwin 			len = 1;
914e3024df2SJohn Baldwin 			break;
915e3024df2SJohn Baldwin 		case UDF_PATH_DOTDOT:
916e3024df2SJohn Baldwin 			cp = "..";
917e3024df2SJohn Baldwin 			len = 2;
918e3024df2SJohn Baldwin 			break;
919e3024df2SJohn Baldwin 		case UDF_PATH_PATH:
920e3024df2SJohn Baldwin 			if (pc->length == 0) {
921e3024df2SJohn Baldwin 				error = EINVAL;
922e3024df2SJohn Baldwin 				goto error;
923e3024df2SJohn Baldwin 			}
924e3024df2SJohn Baldwin 			/*
925e3024df2SJohn Baldwin 			 * XXX: We only support CS8 which appears to map
926e3024df2SJohn Baldwin 			 * to ASCII directly.
927e3024df2SJohn Baldwin 			 */
928e3024df2SJohn Baldwin 			switch (pc->identifier[0]) {
929e3024df2SJohn Baldwin 			case 8:
930e3024df2SJohn Baldwin 				cp = pc->identifier + 1;
931e3024df2SJohn Baldwin 				len = pc->length - 1;
932e3024df2SJohn Baldwin 				break;
933e3024df2SJohn Baldwin 			default:
934e3024df2SJohn Baldwin 				error = EOPNOTSUPP;
935e3024df2SJohn Baldwin 				goto error;
936e3024df2SJohn Baldwin 			}
937e3024df2SJohn Baldwin 			break;
938e3024df2SJohn Baldwin 		default:
939e3024df2SJohn Baldwin 			error = EINVAL;
940e3024df2SJohn Baldwin 			goto error;
941e3024df2SJohn Baldwin 		}
942e3024df2SJohn Baldwin 
943e3024df2SJohn Baldwin 		/*
944e3024df2SJohn Baldwin 		 * If this is not the first component, insert a path
945e3024df2SJohn Baldwin 		 * separator.
946e3024df2SJohn Baldwin 		 */
947e3024df2SJohn Baldwin 		if (pc != buf) {
948e3024df2SJohn Baldwin 			/* If we started with root we already have a "/". */
949e3024df2SJohn Baldwin 			if (root)
950e3024df2SJohn Baldwin 				goto skipslash;
951e3024df2SJohn Baldwin 			root = 0;
952e3024df2SJohn Baldwin 			if (ap->a_uio->uio_resid < 1) {
953e3024df2SJohn Baldwin 				error = ENAMETOOLONG;
954e3024df2SJohn Baldwin 				goto error;
955e3024df2SJohn Baldwin 			}
956e3024df2SJohn Baldwin 			error = uiomove("/", 1, ap->a_uio);
957e3024df2SJohn Baldwin 			if (error)
958e3024df2SJohn Baldwin 				break;
959e3024df2SJohn Baldwin 		}
960e3024df2SJohn Baldwin 	skipslash:
961e3024df2SJohn Baldwin 
962e3024df2SJohn Baldwin 		/* Append string at 'cp' of length 'len' to our path. */
963e3024df2SJohn Baldwin 		if (len > ap->a_uio->uio_resid) {
964e3024df2SJohn Baldwin 			error = ENAMETOOLONG;
965e3024df2SJohn Baldwin 			goto error;
966e3024df2SJohn Baldwin 		}
967e3024df2SJohn Baldwin 		error = uiomove(cp, len, ap->a_uio);
968e3024df2SJohn Baldwin 		if (error)
969e3024df2SJohn Baldwin 			break;
970e3024df2SJohn Baldwin 
971e3024df2SJohn Baldwin 		/* Advance to next component. */
972e3024df2SJohn Baldwin 		pc = (void *)((char *)pc + 4 + pc->length);
973e3024df2SJohn Baldwin 	}
974e3024df2SJohn Baldwin error:
975e3024df2SJohn Baldwin 	free(buf, M_DEVBUF);
976e3024df2SJohn Baldwin 	return (error);
97751a7b740SScott Long }
97851a7b740SScott Long 
97951a7b740SScott Long static int
98051a7b740SScott Long udf_strategy(struct vop_strategy_args *a)
98151a7b740SScott Long {
98251a7b740SScott Long 	struct buf *bp;
98351a7b740SScott Long 	struct vnode *vp;
98451a7b740SScott Long 	struct udf_node *node;
98551a7b740SScott Long 	int maxsize;
9860c09ac0dSPav Lucistnik 	daddr_t sector;
987429c018aSPoul-Henning Kamp 	struct bufobj *bo;
9880c09ac0dSPav Lucistnik 	int multiplier;
98951a7b740SScott Long 
99051a7b740SScott Long 	bp = a->a_bp;
991d83b7498SPoul-Henning Kamp 	vp = a->a_vp;
99251a7b740SScott Long 	node = VTON(vp);
99351a7b740SScott Long 
9940c09ac0dSPav Lucistnik 	if (bp->b_blkno == bp->b_lblkno) {
99551a7b740SScott Long 		/*
99651a7b740SScott Long 		 * Files that are embedded in the fentry don't translate well
99751a7b740SScott Long 		 * to a block number.  Reject.
99851a7b740SScott Long 		 */
99951a7b740SScott Long 		if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize,
10000c09ac0dSPav Lucistnik 		    &sector, &maxsize)) {
100151a7b740SScott Long 			clrbuf(bp);
100251a7b740SScott Long 			bp->b_blkno = -1;
100351a7b740SScott Long 		}
10040c09ac0dSPav Lucistnik 
10050c09ac0dSPav Lucistnik 		/* bmap gives sector numbers, bio works with device blocks */
10060c09ac0dSPav Lucistnik 		multiplier = node->udfmp->bsize / DEV_BSIZE;
10070c09ac0dSPav Lucistnik 		bp->b_blkno = sector * multiplier;
10080c09ac0dSPav Lucistnik 
100951a7b740SScott Long 	}
101051a7b740SScott Long 	if ((long)bp->b_blkno == -1) {
101151a7b740SScott Long 		bufdone(bp);
101251a7b740SScott Long 		return (0);
101351a7b740SScott Long 	}
1014429c018aSPoul-Henning Kamp 	bo = node->udfmp->im_bo;
10152c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
10160391e5a1SPoul-Henning Kamp 	BO_STRATEGY(bo, bp);
101751a7b740SScott Long 	return (0);
101851a7b740SScott Long }
101951a7b740SScott Long 
102051a7b740SScott Long static int
102151a7b740SScott Long udf_bmap(struct vop_bmap_args *a)
102251a7b740SScott Long {
102351a7b740SScott Long 	struct udf_node *node;
1024c2d6947dSJeroen Ruigrok van der Werven 	uint32_t max_size;
102598b0c789SPoul-Henning Kamp 	daddr_t lsector;
102651a7b740SScott Long 	int error;
102751a7b740SScott Long 
102851a7b740SScott Long 	node = VTON(a->a_vp);
102951a7b740SScott Long 
10309c83534dSPoul-Henning Kamp 	if (a->a_bop != NULL)
1031e0251bbbSPoul-Henning Kamp 		*a->a_bop = &node->udfmp->im_devvp->v_bufobj;
103251a7b740SScott Long 	if (a->a_bnp == NULL)
103351a7b740SScott Long 		return (0);
103451a7b740SScott Long 	if (a->a_runb)
103551a7b740SScott Long 		*a->a_runb = 0;
103651a7b740SScott Long 
1037cd1b1a1dSScott Long 	error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector,
103851a7b740SScott Long 	    &max_size);
10398db4c2f2SScott Long 	if (error)
104051a7b740SScott Long 		return (error);
104151a7b740SScott Long 
1042cd1b1a1dSScott Long 	/* Translate logical to physical sector number */
1043cd1b1a1dSScott Long 	*a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
1044cd1b1a1dSScott Long 
104551a7b740SScott Long 	/* Punt on read-ahead for now */
104651a7b740SScott Long 	if (a->a_runp)
104751a7b740SScott Long 		*a->a_runp = 0;
104851a7b740SScott Long 
104951a7b740SScott Long 	return (0);
105051a7b740SScott Long }
105151a7b740SScott Long 
105251a7b740SScott Long /*
105351a7b740SScott Long  * The all powerful VOP_LOOKUP().
105451a7b740SScott Long  */
105551a7b740SScott Long static int
105651a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a)
105751a7b740SScott Long {
105851a7b740SScott Long 	struct vnode *dvp;
105951a7b740SScott Long 	struct vnode *tdp = NULL;
106051a7b740SScott Long 	struct vnode **vpp = a->a_vpp;
106151a7b740SScott Long 	struct udf_node *node;
106251a7b740SScott Long 	struct udf_mnt *udfmp;
106351a7b740SScott Long 	struct fileid_desc *fid = NULL;
10641703656aSScott Long 	struct udf_dirstream *ds;
106551a7b740SScott Long 	u_long nameiop;
106651a7b740SScott Long 	u_long flags;
106751a7b740SScott Long 	char *nameptr;
106851a7b740SScott Long 	long namelen;
106951a7b740SScott Long 	ino_t id = 0;
10701703656aSScott Long 	int offset, error = 0;
10711703656aSScott Long 	int numdirpasses, fsize;
107251a7b740SScott Long 
107351a7b740SScott Long 	dvp = a->a_dvp;
107451a7b740SScott Long 	node = VTON(dvp);
107551a7b740SScott Long 	udfmp = node->udfmp;
107651a7b740SScott Long 	nameiop = a->a_cnp->cn_nameiop;
107751a7b740SScott Long 	flags = a->a_cnp->cn_flags;
107851a7b740SScott Long 	nameptr = a->a_cnp->cn_nameptr;
107951a7b740SScott Long 	namelen = a->a_cnp->cn_namelen;
1080bf1c3dddSScott Long 	fsize = le64toh(node->fentry->inf_len);
108151a7b740SScott Long 
108251a7b740SScott Long 	/*
108351a7b740SScott Long 	 * If this is a LOOKUP and we've already partially searched through
108451a7b740SScott Long 	 * the directory, pick up where we left off and flag that the
108551a7b740SScott Long 	 * directory may need to be searched twice.  For a full description,
10864b12bb04SMaxim Konovalov 	 * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
108751a7b740SScott Long 	 */
10881703656aSScott Long 	if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
108951a7b740SScott Long 		offset = 0;
109051a7b740SScott Long 		numdirpasses = 1;
109151a7b740SScott Long 	} else {
109251a7b740SScott Long 		offset = node->diroff;
109351a7b740SScott Long 		numdirpasses = 2;
109451a7b740SScott Long 		nchstats.ncs_2passes++;
109551a7b740SScott Long 	}
109651a7b740SScott Long 
109751a7b740SScott Long lookloop:
10981703656aSScott Long 	ds = udf_opendir(node, offset, fsize, udfmp);
109951a7b740SScott Long 
11001703656aSScott Long 	while ((fid = udf_getfid(ds)) != NULL) {
110151a7b740SScott Long 
110251a7b740SScott Long 		/* XXX Should we return an error on a bad fid? */
11031703656aSScott Long 		if (udf_checktag(&fid->tag, TAGID_FID)) {
11041703656aSScott Long 			printf("udf_lookup: Invalid tag\n");
11051703656aSScott Long 			error = EIO;
11061703656aSScott Long 			break;
11071703656aSScott Long 		}
1108678d5effSScott Long 
1109678d5effSScott Long 		/* Is this a deleted file? */
11102bbe0d36SScott Long 		if (fid->file_char & UDF_FILE_CHAR_DEL)
11111703656aSScott Long 			continue;
111251a7b740SScott Long 
11132bbe0d36SScott Long 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
111451a7b740SScott Long 			if (flags & ISDOTDOT) {
111551a7b740SScott Long 				id = udf_getid(&fid->icb);
111651a7b740SScott Long 				break;
111751a7b740SScott Long 			}
111851a7b740SScott Long 		} else {
111951a7b740SScott Long 			if (!(udf_cmpname(&fid->data[fid->l_iu],
11206565282cSScott Long 			    nameptr, fid->l_fi, namelen, udfmp))) {
112151a7b740SScott Long 				id = udf_getid(&fid->icb);
112251a7b740SScott Long 				break;
112351a7b740SScott Long 			}
112451a7b740SScott Long 		}
112551a7b740SScott Long 	}
11261703656aSScott Long 
11271703656aSScott Long 	if (!error)
11281703656aSScott Long 		error = ds->error;
11291703656aSScott Long 
11301703656aSScott Long 	/* XXX Bail out here? */
11311703656aSScott Long 	if (error) {
11321703656aSScott Long 		udf_closedir(ds);
11331703656aSScott Long 		return (error);
113451a7b740SScott Long 	}
113551a7b740SScott Long 
113651a7b740SScott Long 	/* Did we have a match? */
113751a7b740SScott Long 	if (id) {
113827ad03cbSJeff Roberson 		if (flags & ISDOTDOT)
113922db15c0SAttilio Rao 			VOP_UNLOCK(dvp, 0);
114051a7b740SScott Long 		error = udf_vget(udfmp->im_mountp, id, LK_EXCLUSIVE, &tdp);
1141045f25a2SSeigo Tanimura 		if (flags & ISDOTDOT)
1142cb05b60aSAttilio Rao 			vn_lock(dvp, LK_EXCLUSIVE|LK_RETRY);
11431703656aSScott Long 		if (!error) {
11441703656aSScott Long 			/*
11451703656aSScott Long 			 * Remember where this entry was if it's the final
11461703656aSScott Long 			 * component.
11471703656aSScott Long 			 */
114851a7b740SScott Long 			if ((flags & ISLASTCN) && nameiop == LOOKUP)
11491703656aSScott Long 				node->diroff = ds->offset + ds->off;
115051a7b740SScott Long 			if (numdirpasses == 2)
115151a7b740SScott Long 				nchstats.ncs_pass2++;
115251a7b740SScott Long 			*vpp = tdp;
115351a7b740SScott Long 			/* Put this entry in the cache */
115451a7b740SScott Long 			if (flags & MAKEENTRY)
115551a7b740SScott Long 				cache_enter(dvp, *vpp, a->a_cnp);
11564585e3acSJeff Roberson 		}
11571703656aSScott Long 	} else {
115851a7b740SScott Long 		/* Name wasn't found on this pass.  Do another pass? */
115951a7b740SScott Long 		if (numdirpasses == 2) {
116051a7b740SScott Long 			numdirpasses--;
116151a7b740SScott Long 			offset = 0;
11621703656aSScott Long 			udf_closedir(ds);
116351a7b740SScott Long 			goto lookloop;
116451a7b740SScott Long 		}
116551a7b740SScott Long 
116651a7b740SScott Long 		/* Enter name into cache as non-existant */
116751a7b740SScott Long 		if (flags & MAKEENTRY)
116851a7b740SScott Long 			cache_enter(dvp, *vpp, a->a_cnp);
116951a7b740SScott Long 
11701703656aSScott Long 		if ((flags & ISLASTCN) &&
11711703656aSScott Long 		    (nameiop == CREATE || nameiop == RENAME)) {
11721703656aSScott Long 			error = EROFS;
11731703656aSScott Long 		} else {
11741703656aSScott Long 			error = ENOENT;
11751703656aSScott Long 		}
11761703656aSScott Long 	}
117751a7b740SScott Long 
11781703656aSScott Long 	udf_closedir(ds);
11791703656aSScott Long 	return (error);
118051a7b740SScott Long }
118151a7b740SScott Long 
118251a7b740SScott Long static int
118351a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a)
118451a7b740SScott Long {
118551a7b740SScott Long 	struct vnode *vp;
118651a7b740SScott Long 	struct udf_node *unode;
118751a7b740SScott Long 
118851a7b740SScott Long 	vp = a->a_vp;
118951a7b740SScott Long 	unode = VTON(vp);
119051a7b740SScott Long 
119192e73f57SAlfred Perlstein 	/*
119292e73f57SAlfred Perlstein 	 * Destroy the vm object and flush associated pages.
119392e73f57SAlfred Perlstein 	 */
119492e73f57SAlfred Perlstein 	vnode_destroy_vobject(vp);
119592e73f57SAlfred Perlstein 
119651a7b740SScott Long 	if (unode != NULL) {
11974e94fafcSPoul-Henning Kamp 		vfs_hash_remove(vp);
119851a7b740SScott Long 
119951a7b740SScott Long 		if (unode->fentry != NULL)
12001ede983cSDag-Erling Smørgrav 			free(unode->fentry, M_UDFFENTRY);
120151a7b740SScott Long 		uma_zfree(udf_zone_node, unode);
120251a7b740SScott Long 		vp->v_data = NULL;
120351a7b740SScott Long 	}
120451a7b740SScott Long 
120551a7b740SScott Long 	return (0);
120651a7b740SScott Long }
120751a7b740SScott Long 
120810bcafe9SPawel Jakub Dawidek static int
120910bcafe9SPawel Jakub Dawidek udf_vptofh(struct vop_vptofh_args *a)
121010bcafe9SPawel Jakub Dawidek {
121110bcafe9SPawel Jakub Dawidek 	struct udf_node *node;
121210bcafe9SPawel Jakub Dawidek 	struct ifid *ifhp;
121310bcafe9SPawel Jakub Dawidek 
121410bcafe9SPawel Jakub Dawidek 	node = VTON(a->a_vp);
121510bcafe9SPawel Jakub Dawidek 	ifhp = (struct ifid *)a->a_fhp;
121610bcafe9SPawel Jakub Dawidek 	ifhp->ifid_len = sizeof(struct ifid);
121710bcafe9SPawel Jakub Dawidek 	ifhp->ifid_ino = node->hash_id;
121810bcafe9SPawel Jakub Dawidek 
121910bcafe9SPawel Jakub Dawidek 	return (0);
122010bcafe9SPawel Jakub Dawidek }
122110bcafe9SPawel Jakub Dawidek 
122251a7b740SScott Long /*
122351a7b740SScott Long  * Read the block and then set the data pointer to correspond with the
122451a7b740SScott Long  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
122551a7b740SScott Long  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
122651a7b740SScott Long  * whole extent.
1227744bb56dSScott Long  *
1228744bb56dSScott Long  * Note that *bp may be assigned error or not.
1229744bb56dSScott Long  *
123051a7b740SScott Long  */
123151a7b740SScott Long static int
12329d32fde8SScott Long udf_readatoffset(struct udf_node *node, int *size, off_t offset,
12339d32fde8SScott Long     struct buf **bp, uint8_t **data)
123451a7b740SScott Long {
123551a7b740SScott Long 	struct udf_mnt *udfmp;
123651a7b740SScott Long 	struct file_entry *fentry = NULL;
123751a7b740SScott Long 	struct buf *bp1;
1238c2d6947dSJeroen Ruigrok van der Werven 	uint32_t max_size;
123998b0c789SPoul-Henning Kamp 	daddr_t sector;
124051a7b740SScott Long 	int error;
124151a7b740SScott Long 
124251a7b740SScott Long 	udfmp = node->udfmp;
124351a7b740SScott Long 
1244744bb56dSScott Long 	*bp = NULL;
124551a7b740SScott Long 	error = udf_bmap_internal(node, offset, &sector, &max_size);
12464576293dSScott Long 	if (error == UDF_INVALID_BMAP) {
124751a7b740SScott Long 		/*
124851a7b740SScott Long 		 * This error means that the file *data* is stored in the
124951a7b740SScott Long 		 * allocation descriptor field of the file entry.
125051a7b740SScott Long 		 */
125151a7b740SScott Long 		fentry = node->fentry;
1252bf1c3dddSScott Long 		*data = &fentry->data[le32toh(fentry->l_ea)];
1253bf1c3dddSScott Long 		*size = le32toh(fentry->l_ad);
125451a7b740SScott Long 		return (0);
125551a7b740SScott Long 	} else if (error != 0) {
125651a7b740SScott Long 		return (error);
125751a7b740SScott Long 	}
125851a7b740SScott Long 
1259d1def83bSScott Long 	/* Adjust the size so that it is within range */
126051a7b740SScott Long 	if (*size == 0 || *size > max_size)
126151a7b740SScott Long 		*size = max_size;
1262d1def83bSScott Long 	*size = min(*size, MAXBSIZE);
126351a7b740SScott Long 
12645df29e0cSRemko Lodder 	if ((error = udf_readlblks(udfmp, sector, *size + (offset & udfmp->bmask), bp))) {
12651830bca1SScott Long 		printf("warning: udf_readlblks returned error %d\n", error);
1266744bb56dSScott Long 		/* note: *bp may be non-NULL */
126751a7b740SScott Long 		return (error);
126851a7b740SScott Long 	}
126951a7b740SScott Long 
127051a7b740SScott Long 	bp1 = *bp;
12715df29e0cSRemko Lodder 	*data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
127251a7b740SScott Long 	return (0);
127351a7b740SScott Long }
127451a7b740SScott Long 
127551a7b740SScott Long /*
127651a7b740SScott Long  * Translate a file offset into a logical block and then into a physical
127751a7b740SScott Long  * block.
12785df29e0cSRemko Lodder  * max_size - maximum number of bytes that can be read starting from given
12795df29e0cSRemko Lodder  * offset, rather than beginning of calculated sector number
128051a7b740SScott Long  */
128151a7b740SScott Long static int
12829d32fde8SScott Long udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
12839d32fde8SScott Long     uint32_t *max_size)
128451a7b740SScott Long {
128551a7b740SScott Long 	struct udf_mnt *udfmp;
128651a7b740SScott Long 	struct file_entry *fentry;
128751a7b740SScott Long 	void *icb;
128851a7b740SScott Long 	struct icb_tag *tag;
1289c2d6947dSJeroen Ruigrok van der Werven 	uint32_t icblen = 0;
129098b0c789SPoul-Henning Kamp 	daddr_t lsector;
129151a7b740SScott Long 	int ad_offset, ad_num = 0;
129251a7b740SScott Long 	int i, p_offset;
129351a7b740SScott Long 
129451a7b740SScott Long 	udfmp = node->udfmp;
129551a7b740SScott Long 	fentry = node->fentry;
129651a7b740SScott Long 	tag = &fentry->icbtag;
129751a7b740SScott Long 
1298bf1c3dddSScott Long 	switch (le16toh(tag->strat_type)) {
129951a7b740SScott Long 	case 4:
130051a7b740SScott Long 		break;
130151a7b740SScott Long 
130251a7b740SScott Long 	case 4096:
130351a7b740SScott Long 		printf("Cannot deal with strategy4096 yet!\n");
130451a7b740SScott Long 		return (ENODEV);
130551a7b740SScott Long 
130651a7b740SScott Long 	default:
130751a7b740SScott Long 		printf("Unknown strategy type %d\n", tag->strat_type);
130851a7b740SScott Long 		return (ENODEV);
130951a7b740SScott Long 	}
131051a7b740SScott Long 
1311bf1c3dddSScott Long 	switch (le16toh(tag->flags) & 0x7) {
131251a7b740SScott Long 	case 0:
131351a7b740SScott Long 		/*
131451a7b740SScott Long 		 * The allocation descriptor field is filled with short_ad's.
131551a7b740SScott Long 		 * If the offset is beyond the current extent, look for the
131651a7b740SScott Long 		 * next extent.
131751a7b740SScott Long 		 */
131851a7b740SScott Long 		do {
131951a7b740SScott Long 			offset -= icblen;
132051a7b740SScott Long 			ad_offset = sizeof(struct short_ad) * ad_num;
1321bf1c3dddSScott Long 			if (ad_offset > le32toh(fentry->l_ad)) {
132251a7b740SScott Long 				printf("File offset out of bounds\n");
132351a7b740SScott Long 				return (EINVAL);
132451a7b740SScott Long 			}
1325a4d629e3SScott Long 			icb = GETICB(short_ad, fentry,
1326bf1c3dddSScott Long 			    le32toh(fentry->l_ea) + ad_offset);
132751a7b740SScott Long 			icblen = GETICBLEN(short_ad, icb);
132851a7b740SScott Long 			ad_num++;
132951a7b740SScott Long 		} while(offset >= icblen);
133051a7b740SScott Long 
133151a7b740SScott Long 		lsector = (offset  >> udfmp->bshift) +
1332937a2387SWill Andrews 		    le32toh(((struct short_ad *)(icb))->pos);
133351a7b740SScott Long 
13345df29e0cSRemko Lodder 		*max_size = icblen - offset;
133551a7b740SScott Long 
133651a7b740SScott Long 		break;
133751a7b740SScott Long 	case 1:
133851a7b740SScott Long 		/*
133951a7b740SScott Long 		 * The allocation descriptor field is filled with long_ad's
134051a7b740SScott Long 		 * If the offset is beyond the current extent, look for the
134151a7b740SScott Long 		 * next extent.
134251a7b740SScott Long 		 */
134351a7b740SScott Long 		do {
134451a7b740SScott Long 			offset -= icblen;
134551a7b740SScott Long 			ad_offset = sizeof(struct long_ad) * ad_num;
1346bf1c3dddSScott Long 			if (ad_offset > le32toh(fentry->l_ad)) {
134751a7b740SScott Long 				printf("File offset out of bounds\n");
134851a7b740SScott Long 				return (EINVAL);
134951a7b740SScott Long 			}
1350bf1c3dddSScott Long 			icb = GETICB(long_ad, fentry,
1351bf1c3dddSScott Long 			    le32toh(fentry->l_ea) + ad_offset);
135251a7b740SScott Long 			icblen = GETICBLEN(long_ad, icb);
135351a7b740SScott Long 			ad_num++;
135451a7b740SScott Long 		} while(offset >= icblen);
135551a7b740SScott Long 
135651a7b740SScott Long 		lsector = (offset >> udfmp->bshift) +
1357bf1c3dddSScott Long 		    le32toh(((struct long_ad *)(icb))->loc.lb_num);
135851a7b740SScott Long 
13595df29e0cSRemko Lodder 		*max_size = icblen - offset;
136051a7b740SScott Long 
136151a7b740SScott Long 		break;
136251a7b740SScott Long 	case 3:
136351a7b740SScott Long 		/*
136451a7b740SScott Long 		 * This type means that the file *data* is stored in the
136551a7b740SScott Long 		 * allocation descriptor field of the file entry.
136651a7b740SScott Long 		 */
136751a7b740SScott Long 		*max_size = 0;
13688db4c2f2SScott Long 		*sector = node->hash_id + udfmp->part_start;
136951a7b740SScott Long 
13704576293dSScott Long 		return (UDF_INVALID_BMAP);
137151a7b740SScott Long 	case 2:
137251a7b740SScott Long 		/* DirectCD does not use extended_ad's */
137351a7b740SScott Long 	default:
137451a7b740SScott Long 		printf("Unsupported allocation descriptor %d\n",
137551a7b740SScott Long 		       tag->flags & 0x7);
137651a7b740SScott Long 		return (ENODEV);
137751a7b740SScott Long 	}
137851a7b740SScott Long 
137951a7b740SScott Long 	*sector = lsector + udfmp->part_start;
138051a7b740SScott Long 
138151a7b740SScott Long 	/*
138251a7b740SScott Long 	 * Check the sparing table.  Each entry represents the beginning of
138351a7b740SScott Long 	 * a packet.
138451a7b740SScott Long 	 */
138551a7b740SScott Long 	if (udfmp->s_table != NULL) {
138651a7b740SScott Long 		for (i = 0; i< udfmp->s_table_entries; i++) {
1387bf1c3dddSScott Long 			p_offset =
1388bf1c3dddSScott Long 			    lsector - le32toh(udfmp->s_table->entries[i].org);
138951a7b740SScott Long 			if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1390bf1c3dddSScott Long 				*sector =
1391bf1c3dddSScott Long 				   le32toh(udfmp->s_table->entries[i].map) +
139251a7b740SScott Long 				    p_offset;
139351a7b740SScott Long 				break;
139451a7b740SScott Long 			}
139551a7b740SScott Long 		}
139651a7b740SScott Long 	}
139751a7b740SScott Long 
139851a7b740SScott Long 	return (0);
139951a7b740SScott Long }
1400