xref: /freebsd/sys/fs/udf/udf_vnops.c (revision c80a90c51fc1f8c0760c869fe1eb06c0f35aa69b)
151a7b740SScott Long /*-
251a7b740SScott Long  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
351a7b740SScott Long  * All rights reserved.
451a7b740SScott Long  *
551a7b740SScott Long  * Redistribution and use in source and binary forms, with or without
651a7b740SScott Long  * modification, are permitted provided that the following conditions
751a7b740SScott Long  * are met:
851a7b740SScott Long  * 1. Redistributions of source code must retain the above copyright
951a7b740SScott Long  *    notice, this list of conditions and the following disclaimer.
1051a7b740SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
1151a7b740SScott Long  *    notice, this list of conditions and the following disclaimer in the
1251a7b740SScott Long  *    documentation and/or other materials provided with the distribution.
1351a7b740SScott Long  *
1451a7b740SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1551a7b740SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1651a7b740SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1751a7b740SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1851a7b740SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1951a7b740SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2051a7b740SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2151a7b740SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2251a7b740SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2351a7b740SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2451a7b740SScott Long  * SUCH DAMAGE.
2551a7b740SScott Long  *
2651a7b740SScott Long  * $FreeBSD$
2751a7b740SScott Long  */
2851a7b740SScott Long 
2951a7b740SScott Long /* udf_vnops.c */
3051a7b740SScott Long /* Take care of the vnode side of things */
3151a7b740SScott Long 
3251a7b740SScott Long #include <sys/param.h>
3351a7b740SScott Long #include <sys/systm.h>
3451a7b740SScott Long #include <sys/namei.h>
3551a7b740SScott Long #include <sys/kernel.h>
3651a7b740SScott Long #include <sys/malloc.h>
3751a7b740SScott Long #include <sys/stat.h>
3851a7b740SScott Long #include <sys/bio.h>
3951a7b740SScott Long #include <sys/buf.h>
406565282cSScott Long #include <sys/iconv.h>
4151a7b740SScott Long #include <sys/mount.h>
4251a7b740SScott Long #include <sys/vnode.h>
4351a7b740SScott Long #include <sys/dirent.h>
4451a7b740SScott Long #include <sys/queue.h>
4551a7b740SScott Long #include <sys/unistd.h>
4651a7b740SScott Long 
4751a7b740SScott Long #include <vm/uma.h>
4851a7b740SScott Long 
4951a7b740SScott Long #include <fs/udf/ecma167-udf.h>
5051a7b740SScott Long #include <fs/udf/osta.h>
5151a7b740SScott Long #include <fs/udf/udf.h>
526565282cSScott Long #include <fs/udf/udf_mount.h>
536565282cSScott Long 
546565282cSScott Long extern struct iconv_functions *udf_iconv;
5551a7b740SScott Long 
5651a7b740SScott Long static int udf_access(struct vop_access_args *);
5751a7b740SScott Long static int udf_getattr(struct vop_getattr_args *);
5851a7b740SScott Long static int udf_ioctl(struct vop_ioctl_args *);
5951a7b740SScott Long static int udf_pathconf(struct vop_pathconf_args *);
6051a7b740SScott Long static int udf_read(struct vop_read_args *);
6151a7b740SScott Long static int udf_readdir(struct vop_readdir_args *);
6251a7b740SScott Long static int udf_readlink(struct vop_readlink_args *ap);
6351a7b740SScott Long static int udf_strategy(struct vop_strategy_args *);
6451a7b740SScott Long static int udf_bmap(struct vop_bmap_args *);
6551a7b740SScott Long static int udf_lookup(struct vop_cachedlookup_args *);
6651a7b740SScott Long static int udf_reclaim(struct vop_reclaim_args *);
67c2d6947dSJeroen Ruigrok van der Werven static int udf_readatoffset(struct udf_node *, int *, int, struct buf **, uint8_t **);
6898b0c789SPoul-Henning Kamp static int udf_bmap_internal(struct udf_node *, uint32_t, daddr_t *, uint32_t *);
6951a7b740SScott Long 
7051a7b740SScott Long vop_t **udf_vnodeop_p;
7151a7b740SScott Long static struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
7251a7b740SScott Long 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
7351a7b740SScott Long 	{ &vop_access_desc,		(vop_t *) udf_access },
7451a7b740SScott Long 	{ &vop_bmap_desc,		(vop_t *) udf_bmap },
7551a7b740SScott Long 	{ &vop_cachedlookup_desc,	(vop_t *) udf_lookup },
7651a7b740SScott Long 	{ &vop_getattr_desc,		(vop_t *) udf_getattr },
7751a7b740SScott Long 	{ &vop_ioctl_desc,		(vop_t *) udf_ioctl },
7851a7b740SScott Long 	{ &vop_lookup_desc,		(vop_t *) vfs_cache_lookup },
7951a7b740SScott Long 	{ &vop_pathconf_desc,		(vop_t *) udf_pathconf },
8051a7b740SScott Long 	{ &vop_read_desc,		(vop_t *) udf_read },
8151a7b740SScott Long 	{ &vop_readdir_desc,		(vop_t *) udf_readdir },
8251a7b740SScott Long 	{ &vop_readlink_desc,		(vop_t *) udf_readlink },
8351a7b740SScott Long 	{ &vop_reclaim_desc,		(vop_t *) udf_reclaim },
8451a7b740SScott Long 	{ &vop_strategy_desc,		(vop_t *) udf_strategy },
8551a7b740SScott Long 	{ NULL, NULL }
8651a7b740SScott Long };
8751a7b740SScott Long static struct vnodeopv_desc udf_vnodeop_opv_desc =
8851a7b740SScott Long 	{ &udf_vnodeop_p, udf_vnodeop_entries };
8951a7b740SScott Long VNODEOP_SET(udf_vnodeop_opv_desc);
9051a7b740SScott Long 
9151a7b740SScott Long MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure");
921703656aSScott Long MALLOC_DEFINE(M_UDFDS, "UDF DS", "UDF Dirstream structure");
9351a7b740SScott Long 
944576293dSScott Long #define UDF_INVALID_BMAP	-1
958db4c2f2SScott Long 
9651a7b740SScott Long /* Look up a udf_node based on the ino_t passed in and return it's vnode */
9751a7b740SScott Long int
9851a7b740SScott Long udf_hashlookup(struct udf_mnt *udfmp, ino_t id, int flags, struct vnode **vpp)
9951a7b740SScott Long {
10051a7b740SScott Long 	struct udf_node *node;
101c9c0dc5bSScott Long 	struct udf_hash_lh *lh;
10251a7b740SScott Long 	int error;
10351a7b740SScott Long 
10451a7b740SScott Long 	*vpp = NULL;
10551a7b740SScott Long 
10651a7b740SScott Long loop:
10751a7b740SScott Long 	mtx_lock(&udfmp->hash_mtx);
108c9c0dc5bSScott Long 	lh = &udfmp->hashtbl[id % udfmp->hashsz];
109c9c0dc5bSScott Long 	if (lh == NULL)
110c9c0dc5bSScott Long 		return (ENOENT);
111c9c0dc5bSScott Long 	LIST_FOREACH(node, lh, le) {
11251a7b740SScott Long 		if (node->hash_id == id) {
11351a7b740SScott Long 			VI_LOCK(node->i_vnode);
11451a7b740SScott Long 			mtx_unlock(&udfmp->hash_mtx);
11551a7b740SScott Long 			error = vget(node->i_vnode, flags | LK_INTERLOCK,
11651a7b740SScott Long 			    curthread);
11751a7b740SScott Long 			if (error == ENOENT)
11851a7b740SScott Long 				goto loop;
11951a7b740SScott Long 			if (error)
12051a7b740SScott Long 				return (error);
12151a7b740SScott Long 			*vpp = node->i_vnode;
12251a7b740SScott Long 			return (0);
12351a7b740SScott Long 		}
12451a7b740SScott Long 	}
12551a7b740SScott Long 
12651a7b740SScott Long 	mtx_unlock(&udfmp->hash_mtx);
12751a7b740SScott Long 	return (0);
12851a7b740SScott Long }
12951a7b740SScott Long 
13051a7b740SScott Long int
13151a7b740SScott Long udf_hashins(struct udf_node *node)
13251a7b740SScott Long {
13351a7b740SScott Long 	struct udf_mnt *udfmp;
134c9c0dc5bSScott Long 	struct udf_hash_lh *lh;
13551a7b740SScott Long 
13651a7b740SScott Long 	udfmp = node->udfmp;
13751a7b740SScott Long 
138bf6ac110STim J. Robbins 	vn_lock(node->i_vnode, LK_EXCLUSIVE | LK_RETRY, curthread);
13951a7b740SScott Long 	mtx_lock(&udfmp->hash_mtx);
140c9c0dc5bSScott Long 	lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
141c9c0dc5bSScott Long 	if (lh == NULL)
142c9c0dc5bSScott Long 		LIST_INIT(lh);
143c9c0dc5bSScott Long 	LIST_INSERT_HEAD(lh, node, le);
14451a7b740SScott Long 	mtx_unlock(&udfmp->hash_mtx);
14551a7b740SScott Long 
14651a7b740SScott Long 	return (0);
14751a7b740SScott Long }
14851a7b740SScott Long 
14951a7b740SScott Long int
15051a7b740SScott Long udf_hashrem(struct udf_node *node)
15151a7b740SScott Long {
15251a7b740SScott Long 	struct udf_mnt *udfmp;
153c9c0dc5bSScott Long 	struct udf_hash_lh *lh;
15451a7b740SScott Long 
15551a7b740SScott Long 	udfmp = node->udfmp;
15651a7b740SScott Long 
15751a7b740SScott Long 	mtx_lock(&udfmp->hash_mtx);
158c9c0dc5bSScott Long 	lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
159c9c0dc5bSScott Long 	if (lh == NULL)
160c9c0dc5bSScott Long 		panic("hash entry is NULL, node->hash_id= %d\n", node->hash_id);
161c9c0dc5bSScott Long 	LIST_REMOVE(node, le);
16251a7b740SScott Long 	mtx_unlock(&udfmp->hash_mtx);
16351a7b740SScott Long 
16451a7b740SScott Long 	return (0);
16551a7b740SScott Long }
16651a7b740SScott Long 
16751a7b740SScott Long int
16851a7b740SScott Long udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td)
16951a7b740SScott Long {
17051a7b740SScott Long 	int error;
17151a7b740SScott Long 	struct vnode *vp;
17251a7b740SScott Long 
17306be2aaaSNate Lawson 	error = getnewvnode("udf", mp, udf_vnodeop_p, &vp);
17451a7b740SScott Long 	if (error) {
17551a7b740SScott Long 		printf("udf_allocv: failed to allocate new vnode\n");
17651a7b740SScott Long 		return (error);
17751a7b740SScott Long 	}
17851a7b740SScott Long 
17951a7b740SScott Long 	*vpp = vp;
18051a7b740SScott Long 	return (0);
18151a7b740SScott Long }
18251a7b740SScott Long 
18351a7b740SScott Long /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
18451a7b740SScott Long static mode_t
18551a7b740SScott Long udf_permtomode(struct udf_node *node)
18651a7b740SScott Long {
187c2d6947dSJeroen Ruigrok van der Werven 	uint32_t perm;
188c2d6947dSJeroen Ruigrok van der Werven 	uint32_t flags;
18951a7b740SScott Long 	mode_t mode;
19051a7b740SScott Long 
19151a7b740SScott Long 	perm = node->fentry->perm;
19251a7b740SScott Long 	flags = node->fentry->icbtag.flags;
19351a7b740SScott Long 
19451a7b740SScott Long 	mode = perm & UDF_FENTRY_PERM_USER_MASK;
19551a7b740SScott Long 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
19651a7b740SScott Long 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
19751a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
19851a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
19951a7b740SScott Long 	mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
20051a7b740SScott Long 
20151a7b740SScott Long 	return (mode);
20251a7b740SScott Long }
20351a7b740SScott Long 
20451a7b740SScott Long static int
20551a7b740SScott Long udf_access(struct vop_access_args *a)
20651a7b740SScott Long {
20751a7b740SScott Long 	struct vnode *vp;
20851a7b740SScott Long 	struct udf_node *node;
20951a7b740SScott Long 	mode_t a_mode, mode;
21051a7b740SScott Long 
21151a7b740SScott Long 	vp = a->a_vp;
21251a7b740SScott Long 	node = VTON(vp);
21351a7b740SScott Long 	a_mode = a->a_mode;
21451a7b740SScott Long 
21551a7b740SScott Long 	if (a_mode & VWRITE) {
21651a7b740SScott Long 		switch (vp->v_type) {
21751a7b740SScott Long 		case VDIR:
21851a7b740SScott Long 		case VLNK:
21951a7b740SScott Long 		case VREG:
22051a7b740SScott Long 			return (EROFS);
22151a7b740SScott Long 			/* NOT REACHED */
22251a7b740SScott Long 		default:
22351a7b740SScott Long 			break;
22451a7b740SScott Long 		}
22551a7b740SScott Long 	}
22651a7b740SScott Long 
22751a7b740SScott Long 	mode = udf_permtomode(node);
22851a7b740SScott Long 
22951a7b740SScott Long 	return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid,
23051a7b740SScott Long 	    a_mode, a->a_cred, NULL));
23151a7b740SScott Long }
23251a7b740SScott Long 
23351a7b740SScott Long static int mon_lens[2][12] = {
23451a7b740SScott Long 	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
23551a7b740SScott Long 	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
23651a7b740SScott Long };
23751a7b740SScott Long 
23851a7b740SScott Long static int
23951a7b740SScott Long udf_isaleapyear(int year)
24051a7b740SScott Long {
24151a7b740SScott Long 	int i;
24251a7b740SScott Long 
24351a7b740SScott Long 	i = (year % 4) ? 0 : 1;
24451a7b740SScott Long 	i &= (year % 100) ? 1 : 0;
24551a7b740SScott Long 	i |= (year % 400) ? 0 : 1;
24651a7b740SScott Long 
24751a7b740SScott Long 	return i;
24851a7b740SScott Long }
24951a7b740SScott Long 
25051a7b740SScott Long /*
25151a7b740SScott Long  * XXX This is just a rough hack.  Daylight savings isn't calculated and tv_nsec
25251a7b740SScott Long  * is ignored.
25351a7b740SScott Long  * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
25451a7b740SScott Long  */
25551a7b740SScott Long static void
25651a7b740SScott Long udf_timetotimespec(struct timestamp *time, struct timespec *t)
25751a7b740SScott Long {
25851a7b740SScott Long 	int i, lpyear, daysinyear;
25951a7b740SScott Long 	union {
260c2d6947dSJeroen Ruigrok van der Werven 		uint16_t	u_tz_offset;
26151a7b740SScott Long 		int16_t		s_tz_offset;
26251a7b740SScott Long 	} tz;
26351a7b740SScott Long 
26451a7b740SScott Long 	t->tv_nsec = 0;
26551a7b740SScott Long 
26651a7b740SScott Long 	/* DirectCD seems to like using bogus year values */
26751a7b740SScott Long 	if (time->year < 1970) {
26851a7b740SScott Long 		t->tv_sec = 0;
26951a7b740SScott Long 		return;
27051a7b740SScott Long 	}
27151a7b740SScott Long 
27251a7b740SScott Long 	/* Calculate the time and day */
27351a7b740SScott Long 	t->tv_sec = time->second;
27451a7b740SScott Long 	t->tv_sec += time->minute * 60;
27551a7b740SScott Long 	t->tv_sec += time->hour * 3600;
27651a7b740SScott Long 	t->tv_sec += time->day * 3600 * 24;
27751a7b740SScott Long 
27851a7b740SScott Long 	/* Calclulate the month */
27951a7b740SScott Long 	lpyear = udf_isaleapyear(time->year);
28051a7b740SScott Long 	for (i = 1; i < time->month; i++)
28151a7b740SScott Long 		t->tv_sec += mon_lens[lpyear][i] * 3600 * 24;
28251a7b740SScott Long 
28351a7b740SScott Long 	/* Speed up the calculation */
28451a7b740SScott Long 	if (time->year > 1979)
28551a7b740SScott Long 		t->tv_sec += 315532800;
28651a7b740SScott Long 	if (time->year > 1989)
28751a7b740SScott Long 		t->tv_sec += 315619200;
28851a7b740SScott Long 	if (time->year > 1999)
28951a7b740SScott Long 		t->tv_sec += 315532800;
29051a7b740SScott Long 	for (i = 2000; i < time->year; i++) {
29151a7b740SScott Long 		daysinyear = udf_isaleapyear(i) + 365 ;
29251a7b740SScott Long 		t->tv_sec += daysinyear * 3600 * 24;
29351a7b740SScott Long 	}
29451a7b740SScott Long 
29551a7b740SScott Long 	/*
29651a7b740SScott Long 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
29751a7b740SScott Long 	 * compliment, so we gotta do some extra magic to handle it right.
29851a7b740SScott Long 	 */
29951a7b740SScott Long 	tz.u_tz_offset = time->type_tz;
30051a7b740SScott Long 	tz.u_tz_offset &= 0x0fff;
30151a7b740SScott Long 	if (tz.u_tz_offset & 0x0800)
30251a7b740SScott Long 		tz.u_tz_offset |= 0xf000;	/* extend the sign to 16 bits */
30351a7b740SScott Long 	if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047))
30451a7b740SScott Long 		t->tv_sec -= tz.s_tz_offset * 60;
30551a7b740SScott Long 
30651a7b740SScott Long 	return;
30751a7b740SScott Long }
30851a7b740SScott Long 
30951a7b740SScott Long static int
31051a7b740SScott Long udf_getattr(struct vop_getattr_args *a)
31151a7b740SScott Long {
31251a7b740SScott Long 	struct vnode *vp;
31351a7b740SScott Long 	struct udf_node *node;
31451a7b740SScott Long 	struct vattr *vap;
31551a7b740SScott Long 	struct file_entry *fentry;
31651a7b740SScott Long 	struct timespec ts;
31751a7b740SScott Long 
31851a7b740SScott Long 	ts.tv_sec = 0;
31951a7b740SScott Long 
32051a7b740SScott Long 	vp = a->a_vp;
32151a7b740SScott Long 	vap = a->a_vap;
32251a7b740SScott Long 	node = VTON(vp);
32351a7b740SScott Long 	fentry = node->fentry;
32451a7b740SScott Long 
32551a7b740SScott Long 	vap->va_fsid = dev2udev(node->i_dev);
32651a7b740SScott Long 	vap->va_fileid = node->hash_id;
32751a7b740SScott Long 	vap->va_mode = udf_permtomode(node);
32851a7b740SScott Long 	vap->va_nlink = fentry->link_cnt;
32951a7b740SScott Long 	/*
33051a7b740SScott Long 	 * XXX The spec says that -1 is valid for uid/gid and indicates an
33151a7b740SScott Long 	 * invalid uid/gid.  How should this be represented?
33251a7b740SScott Long 	 */
33351a7b740SScott Long 	vap->va_uid = (fentry->uid == -1) ? 0 : fentry->uid;
33451a7b740SScott Long 	vap->va_gid = (fentry->gid == -1) ? 0 : fentry->gid;
33551a7b740SScott Long 	udf_timetotimespec(&fentry->atime, &vap->va_atime);
33651a7b740SScott Long 	udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
33751a7b740SScott Long 	vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
33851a7b740SScott Long 	vap->va_rdev = 0; /* XXX */
33951a7b740SScott Long 	if (vp->v_type & VDIR) {
34051a7b740SScott Long 		/*
34151a7b740SScott Long 		 * Directories that are recorded within their ICB will show
34251a7b740SScott Long 		 * as having 0 blocks recorded.  Since tradition dictates
34351a7b740SScott Long 		 * that directories consume at least one logical block,
34451a7b740SScott Long 		 * make it appear so.
34551a7b740SScott Long 		 */
34651a7b740SScott Long 		if (fentry->logblks_rec != 0) {
34751a7b740SScott Long 			vap->va_size = fentry->logblks_rec * node->udfmp->bsize;
34851a7b740SScott Long 		} else {
34951a7b740SScott Long 			vap->va_size = node->udfmp->bsize;
35051a7b740SScott Long 		}
35151a7b740SScott Long 	} else {
35251a7b740SScott Long 		vap->va_size = fentry->inf_len;
35351a7b740SScott Long 	}
35451a7b740SScott Long 	vap->va_flags = 0;
35551a7b740SScott Long 	vap->va_gen = 1;
35651a7b740SScott Long 	vap->va_blocksize = node->udfmp->bsize;
35751a7b740SScott Long 	vap->va_bytes = fentry->inf_len;
35851a7b740SScott Long 	vap->va_type = vp->v_type;
35951a7b740SScott Long 	vap->va_filerev = 0; /* XXX */
36051a7b740SScott Long 	return (0);
36151a7b740SScott Long }
36251a7b740SScott Long 
36351a7b740SScott Long /*
36451a7b740SScott Long  * File specific ioctls.  DeCSS candidate?
36551a7b740SScott Long  */
36651a7b740SScott Long static int
36751a7b740SScott Long udf_ioctl(struct vop_ioctl_args *a)
36851a7b740SScott Long {
369c80a90c5SScott Long 	printf("%s called\n", __func__);
3701d02d910SPoul-Henning Kamp 	return (ENOTTY);
37151a7b740SScott Long }
37251a7b740SScott Long 
37351a7b740SScott Long /*
37451a7b740SScott Long  * I'm not sure that this has much value in a read-only filesystem, but
37551a7b740SScott Long  * cd9660 has it too.
37651a7b740SScott Long  */
37751a7b740SScott Long static int
37851a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a)
37951a7b740SScott Long {
38051a7b740SScott Long 
38151a7b740SScott Long 	switch (a->a_name) {
38251a7b740SScott Long 	case _PC_LINK_MAX:
38351a7b740SScott Long 		*a->a_retval = 65535;
38451a7b740SScott Long 		return (0);
38551a7b740SScott Long 	case _PC_NAME_MAX:
38651a7b740SScott Long 		*a->a_retval = NAME_MAX;
38751a7b740SScott Long 		return (0);
38851a7b740SScott Long 	case _PC_PATH_MAX:
38951a7b740SScott Long 		*a->a_retval = PATH_MAX;
39051a7b740SScott Long 		return (0);
39151a7b740SScott Long 	case _PC_NO_TRUNC:
39251a7b740SScott Long 		*a->a_retval = 1;
39351a7b740SScott Long 		return (0);
39451a7b740SScott Long 	default:
39551a7b740SScott Long 		return (EINVAL);
39651a7b740SScott Long 	}
39751a7b740SScott Long }
39851a7b740SScott Long 
39951a7b740SScott Long static int
40051a7b740SScott Long udf_read(struct vop_read_args *a)
40151a7b740SScott Long {
40251a7b740SScott Long 	struct vnode *vp = a->a_vp;
40351a7b740SScott Long 	struct uio *uio = a->a_uio;
40451a7b740SScott Long 	struct udf_node *node = VTON(vp);
40551a7b740SScott Long 	struct buf *bp;
406c2d6947dSJeroen Ruigrok van der Werven 	uint8_t *data;
40751a7b740SScott Long 	int error = 0;
408d1def83bSScott Long 	int size, fsize, offset;
40951a7b740SScott Long 
41051a7b740SScott Long 	if (uio->uio_offset < 0)
41151a7b740SScott Long 		return (EINVAL);
41251a7b740SScott Long 
41351a7b740SScott Long 	fsize = node->fentry->inf_len;
414d1def83bSScott Long 
41551a7b740SScott Long 	while (uio->uio_offset < fsize && uio->uio_resid > 0) {
41651a7b740SScott Long 		offset = uio->uio_offset;
417d1def83bSScott Long 		size = uio->uio_resid;
41851a7b740SScott Long 		error = udf_readatoffset(node, &size, offset, &bp, &data);
41951a7b740SScott Long 		if (error)
42051a7b740SScott Long 			return (error);
421c9524588SDag-Erling Smørgrav 		error = uiomove(data, size, uio);
42251a7b740SScott Long 		if (bp != NULL)
42351a7b740SScott Long 			brelse(bp);
42451a7b740SScott Long 		if (error)
42551a7b740SScott Long 			break;
42651a7b740SScott Long 	};
42751a7b740SScott Long 
42851a7b740SScott Long 	return (error);
42951a7b740SScott Long }
43051a7b740SScott Long 
43151a7b740SScott Long /*
43251a7b740SScott Long  * Call the OSTA routines to translate the name from a CS0 dstring to a
43351a7b740SScott Long  * 16-bit Unicode String.  Hooks need to be placed in here to translate from
4346565282cSScott Long  * Unicode to the encoding that the kernel/user expects.  Return the length
4356565282cSScott Long  * of the translated string.
43651a7b740SScott Long  */
43751a7b740SScott Long static int
4386565282cSScott Long udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
43951a7b740SScott Long {
44051a7b740SScott Long 	unicode_t *transname;
4416565282cSScott Long 	char *unibuf, *unip;
4426565282cSScott Long 	int i, unilen = 0, destlen;
4436565282cSScott Long 	size_t destleft = MAXNAMLEN;
44451a7b740SScott Long 
4456565282cSScott Long 	/* Convert 16-bit Unicode to destname */
4466565282cSScott Long 	if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
4476565282cSScott Long 		/* allocate a buffer big enough to hold an 8->16 bit expansion */
4486565282cSScott Long 		unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
4496565282cSScott Long 		unip = unibuf;
4506565282cSScott Long 		if ((unilen = udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
4516565282cSScott Long 			printf("udf: Unicode translation failed\n");
4526565282cSScott Long 			uma_zfree(udf_zone_trans, unibuf);
4536565282cSScott Long 			return 0;
4546565282cSScott Long 		}
4556565282cSScott Long 
4566565282cSScott Long 		while (unilen > 0 && destleft > 0) {
4576565282cSScott Long 			udf_iconv->conv(udfmp->im_d2l, (const char **)&unibuf,
4586565282cSScott Long 				(size_t *)&unilen, (char **)&destname, &destleft);
4596565282cSScott Long 			/* Unconverted character found */
4606565282cSScott Long 			if (unilen > 0 && destleft > 0) {
4616565282cSScott Long 				*destname++ = '?';
4626565282cSScott Long 				destleft--;
4636565282cSScott Long 				unibuf += 2;
4646565282cSScott Long 				unilen -= 2;
4656565282cSScott Long 			}
4666565282cSScott Long 		}
4676565282cSScott Long 		uma_zfree(udf_zone_trans, unip);
4686565282cSScott Long 		*destname = '\0';
4696565282cSScott Long 		destlen = MAXNAMLEN - (int)destleft;
4706565282cSScott Long 	} else {
47151a7b740SScott Long 		/* allocate a buffer big enough to hold an 8->16 bit expansion */
472a163d034SWarner Losh 		transname = uma_zalloc(udf_zone_trans, M_WAITOK);
47351a7b740SScott Long 
47451a7b740SScott Long 		if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) {
47551a7b740SScott Long 			printf("udf: Unicode translation failed\n");
47651a7b740SScott Long 			uma_zfree(udf_zone_trans, transname);
47751a7b740SScott Long 			return 0;
47851a7b740SScott Long 		}
47951a7b740SScott Long 
48051a7b740SScott Long 		for (i = 0; i < unilen ; i++) {
48151a7b740SScott Long 			if (transname[i] & 0xff00) {
48251a7b740SScott Long 				destname[i] = '.';	/* Fudge the 16bit chars */
48351a7b740SScott Long 			} else {
48451a7b740SScott Long 				destname[i] = transname[i] & 0xff;
48551a7b740SScott Long 			}
48651a7b740SScott Long 		}
48751a7b740SScott Long 		uma_zfree(udf_zone_trans, transname);
4886565282cSScott Long 		destname[unilen] = 0;
4896565282cSScott Long 		destlen = unilen;
4906565282cSScott Long 	}
49151a7b740SScott Long 
4926565282cSScott Long 	return (destlen);
49351a7b740SScott Long }
49451a7b740SScott Long 
49551a7b740SScott Long /*
49651a7b740SScott Long  * Compare a CS0 dstring with a name passed in from the VFS layer.  Return
49751a7b740SScott Long  * 0 on a successful match, nonzero therwise.  Unicode work may need to be done
49851a7b740SScott Long  * here also.
49951a7b740SScott Long  */
50051a7b740SScott Long static int
5016565282cSScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
50251a7b740SScott Long {
50395ec5961SScott Long 	char *transname;
50495ec5961SScott Long 	int error = 0;
50551a7b740SScott Long 
50695ec5961SScott Long 	/* This is overkill, but not worth creating a new zone */
507a163d034SWarner Losh 	transname = uma_zalloc(udf_zone_trans, M_WAITOK);
50895ec5961SScott Long 
5096565282cSScott Long 	cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
51051a7b740SScott Long 
51151a7b740SScott Long 	/* Easy check.  If they aren't the same length, they aren't equal */
51295ec5961SScott Long 	if ((cs0len == 0) || (cs0len != cmplen))
51395ec5961SScott Long 		error = -1;
51495ec5961SScott Long 	else
51595ec5961SScott Long 		error = bcmp(transname, cmpname, cmplen);
51651a7b740SScott Long 
51795ec5961SScott Long 	uma_zfree(udf_zone_trans, transname);
51895ec5961SScott Long 	return (error);
51951a7b740SScott Long }
52051a7b740SScott Long 
52151a7b740SScott Long struct udf_uiodir {
52251a7b740SScott Long 	struct dirent *dirent;
52351a7b740SScott Long 	u_long *cookies;
52451a7b740SScott Long 	int ncookies;
52551a7b740SScott Long 	int acookies;
52651a7b740SScott Long 	int eofflag;
52751a7b740SScott Long };
52851a7b740SScott Long 
52951a7b740SScott Long static int
53051a7b740SScott Long udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
53151a7b740SScott Long {
53251a7b740SScott Long 	if (uiodir->cookies != NULL) {
53351a7b740SScott Long 		if (++uiodir->acookies > uiodir->ncookies) {
53451a7b740SScott Long 			uiodir->eofflag = 0;
53551a7b740SScott Long 			return (-1);
53651a7b740SScott Long 		}
53751a7b740SScott Long 		*uiodir->cookies++ = cookie;
53851a7b740SScott Long 	}
53951a7b740SScott Long 
54051a7b740SScott Long 	if (uio->uio_resid < de_size) {
54151a7b740SScott Long 		uiodir->eofflag = 0;
54251a7b740SScott Long 		return (-1);
54351a7b740SScott Long 	}
54451a7b740SScott Long 
545c9524588SDag-Erling Smørgrav 	return (uiomove(uiodir->dirent, de_size, uio));
54651a7b740SScott Long }
54751a7b740SScott Long 
5481703656aSScott Long static struct udf_dirstream *
5491703656aSScott Long udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
5501703656aSScott Long {
5511703656aSScott Long 	struct udf_dirstream *ds;
5521703656aSScott Long 
553a163d034SWarner Losh 	ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
5541703656aSScott Long 
5551703656aSScott Long 	ds->node = node;
5561703656aSScott Long 	ds->offset = offset;
5571703656aSScott Long 	ds->udfmp = udfmp;
5581703656aSScott Long 	ds->fsize = fsize;
5591703656aSScott Long 
5601703656aSScott Long 	return (ds);
5611703656aSScott Long }
5621703656aSScott Long 
5631703656aSScott Long static struct fileid_desc *
5641703656aSScott Long udf_getfid(struct udf_dirstream *ds)
5651703656aSScott Long {
5661703656aSScott Long 	struct fileid_desc *fid;
5671703656aSScott Long 	int error, frag_size = 0, total_fid_size;
5681703656aSScott Long 
5691703656aSScott Long 	/* End of directory? */
5701703656aSScott Long 	if (ds->offset + ds->off >= ds->fsize) {
5711703656aSScott Long 		ds->error = 0;
5721703656aSScott Long 		return (NULL);
5731703656aSScott Long 	}
5741703656aSScott Long 
5751703656aSScott Long 	/* Grab the first extent of the directory */
5761703656aSScott Long 	if (ds->off == 0) {
5771703656aSScott Long 		ds->size = 0;
5781703656aSScott Long 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
5791703656aSScott Long 		    &ds->bp, &ds->data);
5801703656aSScott Long 		if (error) {
5811703656aSScott Long 			ds->error = error;
5821703656aSScott Long 			return (NULL);
5831703656aSScott Long 		}
5841703656aSScott Long 	}
5851703656aSScott Long 
5864576293dSScott Long 	/*
5874576293dSScott Long 	 * Clean up from a previous fragmented FID.
5884576293dSScott Long 	 * XXX Is this the right place for this?
5894576293dSScott Long 	 */
5901703656aSScott Long 	if (ds->fid_fragment && ds->buf != NULL) {
5911703656aSScott Long 		ds->fid_fragment = 0;
5921703656aSScott Long 		FREE(ds->buf, M_UDFFID);
5931703656aSScott Long 	}
5941703656aSScott Long 
5951703656aSScott Long 	fid = (struct fileid_desc*)&ds->data[ds->off];
5961703656aSScott Long 
5971703656aSScott Long 	/*
5981703656aSScott Long 	 * Check to see if the fid is fragmented. The first test
5991703656aSScott Long 	 * ensures that we don't wander off the end of the buffer
6001703656aSScott Long 	 * looking for the l_iu and l_fi fields.
6011703656aSScott Long 	 */
6021703656aSScott Long 	if (ds->off + UDF_FID_SIZE > ds->size ||
6031703656aSScott Long 	    ds->off + fid->l_iu + fid->l_fi + UDF_FID_SIZE > ds->size) {
6041703656aSScott Long 
6051703656aSScott Long 		/* Copy what we have of the fid into a buffer */
6061703656aSScott Long 		frag_size = ds->size - ds->off;
6071703656aSScott Long 		if (frag_size >= ds->udfmp->bsize) {
6081703656aSScott Long 			printf("udf: invalid FID fragment\n");
6091703656aSScott Long 			ds->error = EINVAL;
6101703656aSScott Long 			return (NULL);
6111703656aSScott Long 		}
6121703656aSScott Long 
6131703656aSScott Long 		/*
6141703656aSScott Long 		 * File ID descriptors can only be at most one
6151703656aSScott Long 		 * logical sector in size.
6161703656aSScott Long 		 */
6171703656aSScott Long 		MALLOC(ds->buf, uint8_t*, ds->udfmp->bsize, M_UDFFID,
618a163d034SWarner Losh 		     M_WAITOK | M_ZERO);
6191703656aSScott Long 		bcopy(fid, ds->buf, frag_size);
6201703656aSScott Long 
6211703656aSScott Long 		/* Reduce all of the casting magic */
6221703656aSScott Long 		fid = (struct fileid_desc*)ds->buf;
6231703656aSScott Long 
6241703656aSScott Long 		if (ds->bp != NULL)
6251703656aSScott Long 			brelse(ds->bp);
6261703656aSScott Long 
6271703656aSScott Long 		/* Fetch the next allocation */
6281703656aSScott Long 		ds->offset += ds->size;
6291703656aSScott Long 		ds->size = 0;
6301703656aSScott Long 		error = udf_readatoffset(ds->node, &ds->size, ds->offset,
6311703656aSScott Long 		    &ds->bp, &ds->data);
6321703656aSScott Long 		if (error) {
6331703656aSScott Long 			ds->error = error;
6341703656aSScott Long 			return (NULL);
6351703656aSScott Long 		}
6361703656aSScott Long 
6371703656aSScott Long 		/*
6381703656aSScott Long 		 * If the fragment was so small that we didn't get
6391703656aSScott Long 		 * the l_iu and l_fi fields, copy those in.
6401703656aSScott Long 		 */
6411703656aSScott Long 		if (frag_size < UDF_FID_SIZE)
6421703656aSScott Long 			bcopy(ds->data, &ds->buf[frag_size],
6431703656aSScott Long 			    UDF_FID_SIZE - frag_size);
6441703656aSScott Long 
6451703656aSScott Long 		/*
6461703656aSScott Long 		 * Now that we have enough of the fid to work with,
6471703656aSScott Long 		 * copy in the rest of the fid from the new
6481703656aSScott Long 		 * allocation.
6491703656aSScott Long 		 */
6501703656aSScott Long 		total_fid_size = UDF_FID_SIZE + fid->l_iu + fid->l_fi;
6511703656aSScott Long 		if (total_fid_size > ds->udfmp->bsize) {
6521703656aSScott Long 			printf("udf: invalid FID\n");
6531703656aSScott Long 			ds->error = EIO;
6541703656aSScott Long 			return (NULL);
6551703656aSScott Long 		}
6561703656aSScott Long 		bcopy(ds->data, &ds->buf[frag_size],
6571703656aSScott Long 		    total_fid_size - frag_size);
6581703656aSScott Long 
6591703656aSScott Long 		ds->fid_fragment = 1;
6601703656aSScott Long 	} else {
6611703656aSScott Long 		total_fid_size = fid->l_iu + fid->l_fi + UDF_FID_SIZE;
6621703656aSScott Long 	}
6631703656aSScott Long 
6641703656aSScott Long 	/*
6651703656aSScott Long 	 * Update the offset. Align on a 4 byte boundary because the
6664576293dSScott Long 	 * UDF spec says so.
6671703656aSScott Long 	 */
6681703656aSScott Long 	ds->this_off = ds->off;
6691703656aSScott Long 	if (!ds->fid_fragment) {
6701703656aSScott Long 		ds->off += (total_fid_size + 3) & ~0x03;
6711703656aSScott Long 	} else {
6721703656aSScott Long 		ds->off = (total_fid_size - frag_size + 3) & ~0x03;
6731703656aSScott Long 	}
6741703656aSScott Long 
6751703656aSScott Long 	return (fid);
6761703656aSScott Long }
6771703656aSScott Long 
6781703656aSScott Long static void
6791703656aSScott Long udf_closedir(struct udf_dirstream *ds)
6801703656aSScott Long {
6811703656aSScott Long 
6821703656aSScott Long 	if (ds->bp != NULL)
6831703656aSScott Long 		brelse(ds->bp);
6841703656aSScott Long 
6851703656aSScott Long 	if (ds->fid_fragment && ds->buf != NULL)
6861703656aSScott Long 		FREE(ds->buf, M_UDFFID);
6871703656aSScott Long 
6881703656aSScott Long 	uma_zfree(udf_zone_ds, ds);
6891703656aSScott Long }
6901703656aSScott Long 
69151a7b740SScott Long static int
69251a7b740SScott Long udf_readdir(struct vop_readdir_args *a)
69351a7b740SScott Long {
69451a7b740SScott Long 	struct vnode *vp;
69551a7b740SScott Long 	struct uio *uio;
69651a7b740SScott Long 	struct dirent dir;
69751a7b740SScott Long 	struct udf_node *node;
6986565282cSScott Long 	struct udf_mnt *udfmp;
69951a7b740SScott Long 	struct fileid_desc *fid;
70051a7b740SScott Long 	struct udf_uiodir uiodir;
7011703656aSScott Long 	struct udf_dirstream *ds;
70251a7b740SScott Long 	u_long *cookies = NULL;
70351a7b740SScott Long 	int ncookies;
704c8eeea2fSScott Long 	int error = 0;
70551a7b740SScott Long 
70651a7b740SScott Long 	vp = a->a_vp;
70751a7b740SScott Long 	uio = a->a_uio;
70851a7b740SScott Long 	node = VTON(vp);
7096565282cSScott Long 	udfmp = node->udfmp;
71051a7b740SScott Long 	uiodir.eofflag = 1;
71151a7b740SScott Long 
71251a7b740SScott Long 	if (a->a_ncookies != NULL) {
71351a7b740SScott Long 		/*
71451a7b740SScott Long 		 * Guess how many entries are needed.  If we run out, this
71551a7b740SScott Long 		 * function will be called again and thing will pick up were
71651a7b740SScott Long 		 * it left off.
71751a7b740SScott Long 		 */
71851a7b740SScott Long 		ncookies = uio->uio_resid / 8;
71951a7b740SScott Long 		MALLOC(cookies, u_long *, sizeof(u_long) * ncookies,
720a163d034SWarner Losh 		    M_TEMP, M_WAITOK);
72151a7b740SScott Long 		if (cookies == NULL)
72251a7b740SScott Long 			return (ENOMEM);
72351a7b740SScott Long 		uiodir.ncookies = ncookies;
72451a7b740SScott Long 		uiodir.cookies = cookies;
72551a7b740SScott Long 		uiodir.acookies = 0;
72651a7b740SScott Long 	} else {
72751a7b740SScott Long 		uiodir.cookies = NULL;
72851a7b740SScott Long 	}
72951a7b740SScott Long 
73051a7b740SScott Long 	/*
73151a7b740SScott Long 	 * Iterate through the file id descriptors.  Give the parent dir
7324576293dSScott Long 	 * entry special attention.
73351a7b740SScott Long 	 */
7341703656aSScott Long 	ds = udf_opendir(node, uio->uio_offset, node->fentry->inf_len,
7351703656aSScott Long 	    node->udfmp);
73651a7b740SScott Long 
7371703656aSScott Long 	while ((fid = udf_getfid(ds)) != NULL) {
73851a7b740SScott Long 
73951a7b740SScott Long 		/* XXX Should we return an error on a bad fid? */
74051a7b740SScott Long 		if (udf_checktag(&fid->tag, TAGID_FID)) {
74151a7b740SScott Long 			printf("Invalid FID tag\n");
74277411499SScott Long 			hexdump(fid, UDF_FID_SIZE, NULL, 0);
7431703656aSScott Long 			error = EIO;
74451a7b740SScott Long 			break;
74551a7b740SScott Long 		}
74651a7b740SScott Long 
74751a7b740SScott Long 		/* Is this a deleted file? */
7482bbe0d36SScott Long 		if (fid->file_char & UDF_FILE_CHAR_DEL)
7491703656aSScott Long 			continue;
75051a7b740SScott Long 
7512bbe0d36SScott Long 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
75251a7b740SScott Long 			/* Do up the '.' and '..' entries.  Dummy values are
75351a7b740SScott Long 			 * used for the cookies since the offset here is
75451a7b740SScott Long 			 * usually zero, and NFS doesn't like that value
75551a7b740SScott Long 			 */
756c8eeea2fSScott Long 			dir.d_fileno = node->hash_id;
757c8eeea2fSScott Long 			dir.d_type = DT_DIR;
758c8eeea2fSScott Long 			dir.d_name[0] = '.';
759c8eeea2fSScott Long 			dir.d_namlen = 1;
760c8eeea2fSScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
761c8eeea2fSScott Long 			uiodir.dirent = &dir;
762c8eeea2fSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
76351a7b740SScott Long 			if (error)
76451a7b740SScott Long 				break;
76551a7b740SScott Long 
766c8eeea2fSScott Long 			dir.d_fileno = udf_getid(&fid->icb);
767c8eeea2fSScott Long 			dir.d_type = DT_DIR;
768c8eeea2fSScott Long 			dir.d_name[0] = '.';
769c8eeea2fSScott Long 			dir.d_name[1] = '.';
770c8eeea2fSScott Long 			dir.d_namlen = 2;
771c8eeea2fSScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
772c8eeea2fSScott Long 			uiodir.dirent = &dir;
773c8eeea2fSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
77451a7b740SScott Long 		} else {
77551a7b740SScott Long 			dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
7766565282cSScott Long 			    &dir.d_name[0], fid->l_fi, udfmp);
77751a7b740SScott Long 			dir.d_fileno = udf_getid(&fid->icb);
7782bbe0d36SScott Long 			dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
7792bbe0d36SScott Long 			    DT_DIR : DT_UNKNOWN;
78051a7b740SScott Long 			dir.d_reclen = GENERIC_DIRSIZ(&dir);
78151a7b740SScott Long 			uiodir.dirent = &dir;
7821703656aSScott Long 			error = udf_uiodir(&uiodir, dir.d_reclen, uio,
7831703656aSScott Long 			    ds->this_off);
78451a7b740SScott Long 		}
78551a7b740SScott Long 		if (error) {
78651a7b740SScott Long 			printf("uiomove returned %d\n", error);
78751a7b740SScott Long 			break;
78851a7b740SScott Long 		}
78951a7b740SScott Long 
79051a7b740SScott Long 	}
79151a7b740SScott Long 
79251a7b740SScott Long 	/* tell the calling layer whether we need to be called again */
79351a7b740SScott Long 	*a->a_eofflag = uiodir.eofflag;
7941703656aSScott Long 	uio->uio_offset = ds->offset + ds->off;
79551a7b740SScott Long 
7961703656aSScott Long 	if (!error)
7971703656aSScott Long 		error = ds->error;
7981703656aSScott Long 
7991703656aSScott Long 	udf_closedir(ds);
80051a7b740SScott Long 
80151a7b740SScott Long 	if (a->a_ncookies != NULL) {
80251a7b740SScott Long 		if (error)
8031703656aSScott Long 			FREE(cookies, M_TEMP);
80451a7b740SScott Long 		else {
80551a7b740SScott Long 			*a->a_ncookies = uiodir.acookies;
80651a7b740SScott Long 			*a->a_cookies = cookies;
80751a7b740SScott Long 		}
80851a7b740SScott Long 	}
80951a7b740SScott Long 
81051a7b740SScott Long 	return (error);
81151a7b740SScott Long }
81251a7b740SScott Long 
81351a7b740SScott Long /* Are there any implementations out there that do soft-links? */
81451a7b740SScott Long static int
81551a7b740SScott Long udf_readlink(struct vop_readlink_args *ap)
81651a7b740SScott Long {
817c80a90c5SScott Long 	printf("%s called\n", __func__);
81851a7b740SScott Long 	return (EOPNOTSUPP);
81951a7b740SScott Long }
82051a7b740SScott Long 
82151a7b740SScott Long static int
82251a7b740SScott Long udf_strategy(struct vop_strategy_args *a)
82351a7b740SScott Long {
82451a7b740SScott Long 	struct buf *bp;
82551a7b740SScott Long 	struct vnode *vp;
82651a7b740SScott Long 	struct udf_node *node;
82751a7b740SScott Long 	int maxsize;
82851a7b740SScott Long 
82951a7b740SScott Long 	bp = a->a_bp;
83051a7b740SScott Long 	vp = bp->b_vp;
83151a7b740SScott Long 	node = VTON(vp);
83251a7b740SScott Long 
833cefb5754SPoul-Henning Kamp 	KASSERT(a->a_vp == a->a_bp->b_vp, ("%s(%p != %p)",
834cefb5754SPoul-Henning Kamp 	    __func__, a->a_vp, a->a_bp->b_vp));
83551a7b740SScott Long 	/* cd9660 has this test reversed, but it seems more logical this way */
83651a7b740SScott Long 	if (bp->b_blkno != bp->b_lblkno) {
83751a7b740SScott Long 		/*
83851a7b740SScott Long 		 * Files that are embedded in the fentry don't translate well
83951a7b740SScott Long 		 * to a block number.  Reject.
84051a7b740SScott Long 		 */
84151a7b740SScott Long 		if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize,
84251a7b740SScott Long 		    &bp->b_lblkno, &maxsize)) {
84351a7b740SScott Long 			clrbuf(bp);
84451a7b740SScott Long 			bp->b_blkno = -1;
84551a7b740SScott Long 		}
84651a7b740SScott Long 	}
84751a7b740SScott Long 	if ((long)bp->b_blkno == -1) {
84851a7b740SScott Long 		bufdone(bp);
84951a7b740SScott Long 		return (0);
85051a7b740SScott Long 	}
85151a7b740SScott Long 	vp = node->i_devvp;
85251a7b740SScott Long 	bp->b_dev = vp->v_rdev;
8532c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
854f5b11b6eSPoul-Henning Kamp 	VOP_SPECSTRATEGY(vp, bp);
85551a7b740SScott Long 	return (0);
85651a7b740SScott Long }
85751a7b740SScott Long 
85851a7b740SScott Long static int
85951a7b740SScott Long udf_bmap(struct vop_bmap_args *a)
86051a7b740SScott Long {
86151a7b740SScott Long 	struct udf_node *node;
862c2d6947dSJeroen Ruigrok van der Werven 	uint32_t max_size;
86398b0c789SPoul-Henning Kamp 	daddr_t lsector;
86451a7b740SScott Long 	int error;
86551a7b740SScott Long 
86651a7b740SScott Long 	node = VTON(a->a_vp);
86751a7b740SScott Long 
86851a7b740SScott Long 	if (a->a_vpp != NULL)
86951a7b740SScott Long 		*a->a_vpp = node->i_devvp;
87051a7b740SScott Long 	if (a->a_bnp == NULL)
87151a7b740SScott Long 		return (0);
87251a7b740SScott Long 	if (a->a_runb)
87351a7b740SScott Long 		*a->a_runb = 0;
87451a7b740SScott Long 
875cd1b1a1dSScott Long 	error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector,
87651a7b740SScott Long 	    &max_size);
8778db4c2f2SScott Long 	if (error)
87851a7b740SScott Long 		return (error);
87951a7b740SScott Long 
880cd1b1a1dSScott Long 	/* Translate logical to physical sector number */
881cd1b1a1dSScott Long 	*a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
882cd1b1a1dSScott Long 
88351a7b740SScott Long 	/* Punt on read-ahead for now */
88451a7b740SScott Long 	if (a->a_runp)
88551a7b740SScott Long 		*a->a_runp = 0;
88651a7b740SScott Long 
88751a7b740SScott Long 	return (0);
88851a7b740SScott Long }
88951a7b740SScott Long 
89051a7b740SScott Long /*
89151a7b740SScott Long  * The all powerful VOP_LOOKUP().
89251a7b740SScott Long  */
89351a7b740SScott Long static int
89451a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a)
89551a7b740SScott Long {
89651a7b740SScott Long 	struct vnode *dvp;
89751a7b740SScott Long 	struct vnode *tdp = NULL;
89851a7b740SScott Long 	struct vnode **vpp = a->a_vpp;
89951a7b740SScott Long 	struct udf_node *node;
90051a7b740SScott Long 	struct udf_mnt *udfmp;
90151a7b740SScott Long 	struct fileid_desc *fid = NULL;
9021703656aSScott Long 	struct udf_dirstream *ds;
90351a7b740SScott Long 	struct thread *td;
90451a7b740SScott Long 	u_long nameiop;
90551a7b740SScott Long 	u_long flags;
90651a7b740SScott Long 	char *nameptr;
90751a7b740SScott Long 	long namelen;
90851a7b740SScott Long 	ino_t id = 0;
9091703656aSScott Long 	int offset, error = 0;
9101703656aSScott Long 	int numdirpasses, fsize;
91151a7b740SScott Long 
91251a7b740SScott Long 	dvp = a->a_dvp;
91351a7b740SScott Long 	node = VTON(dvp);
91451a7b740SScott Long 	udfmp = node->udfmp;
91551a7b740SScott Long 	nameiop = a->a_cnp->cn_nameiop;
91651a7b740SScott Long 	flags = a->a_cnp->cn_flags;
91751a7b740SScott Long 	nameptr = a->a_cnp->cn_nameptr;
91851a7b740SScott Long 	namelen = a->a_cnp->cn_namelen;
91951a7b740SScott Long 	fsize = node->fentry->inf_len;
92051a7b740SScott Long 	td = a->a_cnp->cn_thread;
92151a7b740SScott Long 
92251a7b740SScott Long 	/*
92351a7b740SScott Long 	 * If this is a LOOKUP and we've already partially searched through
92451a7b740SScott Long 	 * the directory, pick up where we left off and flag that the
92551a7b740SScott Long 	 * directory may need to be searched twice.  For a full description,
92651a7b740SScott Long 	 * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup()
92751a7b740SScott Long 	 */
9281703656aSScott Long 	if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
92951a7b740SScott Long 		offset = 0;
93051a7b740SScott Long 		numdirpasses = 1;
93151a7b740SScott Long 	} else {
93251a7b740SScott Long 		offset = node->diroff;
93351a7b740SScott Long 		numdirpasses = 2;
93451a7b740SScott Long 		nchstats.ncs_2passes++;
93551a7b740SScott Long 	}
93651a7b740SScott Long 
93751a7b740SScott Long lookloop:
9381703656aSScott Long 	ds = udf_opendir(node, offset, fsize, udfmp);
93951a7b740SScott Long 
9401703656aSScott Long 	while ((fid = udf_getfid(ds)) != NULL) {
94151a7b740SScott Long 
94251a7b740SScott Long 		/* XXX Should we return an error on a bad fid? */
9431703656aSScott Long 		if (udf_checktag(&fid->tag, TAGID_FID)) {
9441703656aSScott Long 			printf("udf_lookup: Invalid tag\n");
9451703656aSScott Long 			error = EIO;
9461703656aSScott Long 			break;
9471703656aSScott Long 		}
948678d5effSScott Long 
949678d5effSScott Long 		/* Is this a deleted file? */
9502bbe0d36SScott Long 		if (fid->file_char & UDF_FILE_CHAR_DEL)
9511703656aSScott Long 			continue;
95251a7b740SScott Long 
9532bbe0d36SScott Long 		if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
95451a7b740SScott Long 			if (flags & ISDOTDOT) {
95551a7b740SScott Long 				id = udf_getid(&fid->icb);
95651a7b740SScott Long 				break;
95751a7b740SScott Long 			}
95851a7b740SScott Long 		} else {
95951a7b740SScott Long 			if (!(udf_cmpname(&fid->data[fid->l_iu],
9606565282cSScott Long 			    nameptr, fid->l_fi, namelen, udfmp))) {
96151a7b740SScott Long 				id = udf_getid(&fid->icb);
96251a7b740SScott Long 				break;
96351a7b740SScott Long 			}
96451a7b740SScott Long 		}
96551a7b740SScott Long 	}
9661703656aSScott Long 
9671703656aSScott Long 	if (!error)
9681703656aSScott Long 		error = ds->error;
9691703656aSScott Long 
9701703656aSScott Long 	/* XXX Bail out here? */
9711703656aSScott Long 	if (error) {
9721703656aSScott Long 		udf_closedir(ds);
9731703656aSScott Long 		return (error);
97451a7b740SScott Long 	}
97551a7b740SScott Long 
97651a7b740SScott Long 	/* Did we have a match? */
97751a7b740SScott Long 	if (id) {
97851a7b740SScott Long 		error = udf_vget(udfmp->im_mountp, id, LK_EXCLUSIVE, &tdp);
9791703656aSScott Long 		if (!error) {
9801703656aSScott Long 			/*
9811703656aSScott Long 			 * Remember where this entry was if it's the final
9821703656aSScott Long 			 * component.
9831703656aSScott Long 			 */
98451a7b740SScott Long 			if ((flags & ISLASTCN) && nameiop == LOOKUP)
9851703656aSScott Long 				node->diroff = ds->offset + ds->off;
98651a7b740SScott Long 			if (numdirpasses == 2)
98751a7b740SScott Long 				nchstats.ncs_pass2++;
98851a7b740SScott Long 			if (!(flags & LOCKPARENT) || !(flags & ISLASTCN)) {
98951a7b740SScott Long 				a->a_cnp->cn_flags |= PDIRUNLOCK;
99051a7b740SScott Long 				VOP_UNLOCK(dvp, 0, td);
99151a7b740SScott Long 			}
99251a7b740SScott Long 
99351a7b740SScott Long 			*vpp = tdp;
99451a7b740SScott Long 
99551a7b740SScott Long 			/* Put this entry in the cache */
99651a7b740SScott Long 			if (flags & MAKEENTRY)
99751a7b740SScott Long 				cache_enter(dvp, *vpp, a->a_cnp);
99851a7b740SScott Long 		}
9991703656aSScott Long 	} else {
100051a7b740SScott Long 		/* Name wasn't found on this pass.  Do another pass? */
100151a7b740SScott Long 		if (numdirpasses == 2) {
100251a7b740SScott Long 			numdirpasses--;
100351a7b740SScott Long 			offset = 0;
10041703656aSScott Long 			udf_closedir(ds);
100551a7b740SScott Long 			goto lookloop;
100651a7b740SScott Long 		}
100751a7b740SScott Long 
100851a7b740SScott Long 		/* Enter name into cache as non-existant */
100951a7b740SScott Long 		if (flags & MAKEENTRY)
101051a7b740SScott Long 			cache_enter(dvp, *vpp, a->a_cnp);
101151a7b740SScott Long 
10121703656aSScott Long 		if ((flags & ISLASTCN) &&
10131703656aSScott Long 		    (nameiop == CREATE || nameiop == RENAME)) {
10141703656aSScott Long 			error = EROFS;
10151703656aSScott Long 		} else {
10161703656aSScott Long 			error = ENOENT;
10171703656aSScott Long 		}
10181703656aSScott Long 	}
101951a7b740SScott Long 
10201703656aSScott Long 	udf_closedir(ds);
10211703656aSScott Long 	return (error);
102251a7b740SScott Long }
102351a7b740SScott Long 
102451a7b740SScott Long static int
102551a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a)
102651a7b740SScott Long {
102751a7b740SScott Long 	struct vnode *vp;
102851a7b740SScott Long 	struct udf_node *unode;
102951a7b740SScott Long 
103051a7b740SScott Long 	vp = a->a_vp;
103151a7b740SScott Long 	unode = VTON(vp);
103251a7b740SScott Long 
103351a7b740SScott Long 	if (unode != NULL) {
103451a7b740SScott Long 		udf_hashrem(unode);
103551a7b740SScott Long 		if (unode->i_devvp) {
103651a7b740SScott Long 			vrele(unode->i_devvp);
103751a7b740SScott Long 			unode->i_devvp = 0;
103851a7b740SScott Long 		}
103951a7b740SScott Long 
104051a7b740SScott Long 		if (unode->fentry != NULL)
104151a7b740SScott Long 			FREE(unode->fentry, M_UDFFENTRY);
104251a7b740SScott Long 		uma_zfree(udf_zone_node, unode);
104351a7b740SScott Long 		vp->v_data = NULL;
104451a7b740SScott Long 	}
104551a7b740SScott Long 
104651a7b740SScott Long 	return (0);
104751a7b740SScott Long }
104851a7b740SScott Long 
104951a7b740SScott Long /*
105051a7b740SScott Long  * Read the block and then set the data pointer to correspond with the
105151a7b740SScott Long  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
105251a7b740SScott Long  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
105351a7b740SScott Long  * whole extent.
105451a7b740SScott Long  * XXX 'size' is limited to the logical block size for now due to problems
105551a7b740SScott Long  * with udf_read()
105651a7b740SScott Long  */
105751a7b740SScott Long static int
1058c2d6947dSJeroen Ruigrok van der Werven udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp, uint8_t **data)
105951a7b740SScott Long {
106051a7b740SScott Long 	struct udf_mnt *udfmp;
106151a7b740SScott Long 	struct file_entry *fentry = NULL;
106251a7b740SScott Long 	struct buf *bp1;
1063c2d6947dSJeroen Ruigrok van der Werven 	uint32_t max_size;
106498b0c789SPoul-Henning Kamp 	daddr_t sector;
106551a7b740SScott Long 	int error;
106651a7b740SScott Long 
106751a7b740SScott Long 	udfmp = node->udfmp;
106851a7b740SScott Long 
106951a7b740SScott Long 	error = udf_bmap_internal(node, offset, &sector, &max_size);
10704576293dSScott Long 	if (error == UDF_INVALID_BMAP) {
107151a7b740SScott Long 		/*
107251a7b740SScott Long 		 * This error means that the file *data* is stored in the
107351a7b740SScott Long 		 * allocation descriptor field of the file entry.
107451a7b740SScott Long 		 */
107551a7b740SScott Long 		fentry = node->fentry;
107651a7b740SScott Long 		*data = &fentry->data[fentry->l_ea];
107751a7b740SScott Long 		*size = fentry->l_ad;
107851a7b740SScott Long 		*bp = NULL;
107951a7b740SScott Long 		return (0);
108051a7b740SScott Long 	} else if (error != 0) {
108151a7b740SScott Long 		return (error);
108251a7b740SScott Long 	}
108351a7b740SScott Long 
1084d1def83bSScott Long 	/* Adjust the size so that it is within range */
108551a7b740SScott Long 	if (*size == 0 || *size > max_size)
108651a7b740SScott Long 		*size = max_size;
1087d1def83bSScott Long 	*size = min(*size, MAXBSIZE);
108851a7b740SScott Long 
108951a7b740SScott Long 	if ((error = udf_readlblks(udfmp, sector, *size, bp))) {
10901830bca1SScott Long 		printf("warning: udf_readlblks returned error %d\n", error);
109151a7b740SScott Long 		return (error);
109251a7b740SScott Long 	}
109351a7b740SScott Long 
109451a7b740SScott Long 	bp1 = *bp;
1095c2d6947dSJeroen Ruigrok van der Werven 	*data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize];
109651a7b740SScott Long 	return (0);
109751a7b740SScott Long }
109851a7b740SScott Long 
109951a7b740SScott Long /*
110051a7b740SScott Long  * Translate a file offset into a logical block and then into a physical
110151a7b740SScott Long  * block.
110251a7b740SScott Long  */
110351a7b740SScott Long static int
110498b0c789SPoul-Henning Kamp udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size)
110551a7b740SScott Long {
110651a7b740SScott Long 	struct udf_mnt *udfmp;
110751a7b740SScott Long 	struct file_entry *fentry;
110851a7b740SScott Long 	void *icb;
110951a7b740SScott Long 	struct icb_tag *tag;
1110c2d6947dSJeroen Ruigrok van der Werven 	uint32_t icblen = 0;
111198b0c789SPoul-Henning Kamp 	daddr_t lsector;
111251a7b740SScott Long 	int ad_offset, ad_num = 0;
111351a7b740SScott Long 	int i, p_offset;
111451a7b740SScott Long 
111551a7b740SScott Long 	udfmp = node->udfmp;
111651a7b740SScott Long 	fentry = node->fentry;
111751a7b740SScott Long 	tag = &fentry->icbtag;
111851a7b740SScott Long 
111951a7b740SScott Long 	switch (tag->strat_type) {
112051a7b740SScott Long 	case 4:
112151a7b740SScott Long 		break;
112251a7b740SScott Long 
112351a7b740SScott Long 	case 4096:
112451a7b740SScott Long 		printf("Cannot deal with strategy4096 yet!\n");
112551a7b740SScott Long 		return (ENODEV);
112651a7b740SScott Long 
112751a7b740SScott Long 	default:
112851a7b740SScott Long 		printf("Unknown strategy type %d\n", tag->strat_type);
112951a7b740SScott Long 		return (ENODEV);
113051a7b740SScott Long 	}
113151a7b740SScott Long 
113251a7b740SScott Long 	switch (tag->flags & 0x7) {
113351a7b740SScott Long 	case 0:
113451a7b740SScott Long 		/*
113551a7b740SScott Long 		 * The allocation descriptor field is filled with short_ad's.
113651a7b740SScott Long 		 * If the offset is beyond the current extent, look for the
113751a7b740SScott Long 		 * next extent.
113851a7b740SScott Long 		 */
113951a7b740SScott Long 		do {
114051a7b740SScott Long 			offset -= icblen;
114151a7b740SScott Long 			ad_offset = sizeof(struct short_ad) * ad_num;
114251a7b740SScott Long 			if (ad_offset > fentry->l_ad) {
114351a7b740SScott Long 				printf("File offset out of bounds\n");
114451a7b740SScott Long 				return (EINVAL);
114551a7b740SScott Long 			}
114651a7b740SScott Long 			icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
114751a7b740SScott Long 			icblen = GETICBLEN(short_ad, icb);
114851a7b740SScott Long 			ad_num++;
114951a7b740SScott Long 		} while(offset >= icblen);
115051a7b740SScott Long 
115151a7b740SScott Long 		lsector = (offset  >> udfmp->bshift) +
115251a7b740SScott Long 		    ((struct short_ad *)(icb))->pos;
115351a7b740SScott Long 
11541830bca1SScott Long 		*max_size = GETICBLEN(short_ad, icb);
115551a7b740SScott Long 
115651a7b740SScott Long 		break;
115751a7b740SScott Long 	case 1:
115851a7b740SScott Long 		/*
115951a7b740SScott Long 		 * The allocation descriptor field is filled with long_ad's
116051a7b740SScott Long 		 * If the offset is beyond the current extent, look for the
116151a7b740SScott Long 		 * next extent.
116251a7b740SScott Long 		 */
116351a7b740SScott Long 		do {
116451a7b740SScott Long 			offset -= icblen;
116551a7b740SScott Long 			ad_offset = sizeof(struct long_ad) * ad_num;
116651a7b740SScott Long 			if (ad_offset > fentry->l_ad) {
116751a7b740SScott Long 				printf("File offset out of bounds\n");
116851a7b740SScott Long 				return (EINVAL);
116951a7b740SScott Long 			}
117051a7b740SScott Long 			icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
117151a7b740SScott Long 			icblen = GETICBLEN(long_ad, icb);
117251a7b740SScott Long 			ad_num++;
117351a7b740SScott Long 		} while(offset >= icblen);
117451a7b740SScott Long 
117551a7b740SScott Long 		lsector = (offset >> udfmp->bshift) +
117651a7b740SScott Long 		    ((struct long_ad *)(icb))->loc.lb_num;
117751a7b740SScott Long 
11781830bca1SScott Long 		*max_size = GETICBLEN(long_ad, icb);
117951a7b740SScott Long 
118051a7b740SScott Long 		break;
118151a7b740SScott Long 	case 3:
118251a7b740SScott Long 		/*
118351a7b740SScott Long 		 * This type means that the file *data* is stored in the
118451a7b740SScott Long 		 * allocation descriptor field of the file entry.
118551a7b740SScott Long 		 */
118651a7b740SScott Long 		*max_size = 0;
11878db4c2f2SScott Long 		*sector = node->hash_id + udfmp->part_start;
118851a7b740SScott Long 
11894576293dSScott Long 		return (UDF_INVALID_BMAP);
119051a7b740SScott Long 	case 2:
119151a7b740SScott Long 		/* DirectCD does not use extended_ad's */
119251a7b740SScott Long 	default:
119351a7b740SScott Long 		printf("Unsupported allocation descriptor %d\n",
119451a7b740SScott Long 		       tag->flags & 0x7);
119551a7b740SScott Long 		return (ENODEV);
119651a7b740SScott Long 	}
119751a7b740SScott Long 
119851a7b740SScott Long 	*sector = lsector + udfmp->part_start;
119951a7b740SScott Long 
120051a7b740SScott Long 	/*
120151a7b740SScott Long 	 * Check the sparing table.  Each entry represents the beginning of
120251a7b740SScott Long 	 * a packet.
120351a7b740SScott Long 	 */
120451a7b740SScott Long 	if (udfmp->s_table != NULL) {
120551a7b740SScott Long 		for (i = 0; i< udfmp->s_table_entries; i++) {
120651a7b740SScott Long 			p_offset = lsector - udfmp->s_table->entries[i].org;
120751a7b740SScott Long 			if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
120851a7b740SScott Long 				*sector = udfmp->s_table->entries[i].map +
120951a7b740SScott Long 				    p_offset;
121051a7b740SScott Long 				break;
121151a7b740SScott Long 			}
121251a7b740SScott Long 		}
121351a7b740SScott Long 	}
121451a7b740SScott Long 
121551a7b740SScott Long 	return (0);
121651a7b740SScott Long }
1217