151a7b740SScott Long /*- 251a7b740SScott Long * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org> 351a7b740SScott Long * All rights reserved. 451a7b740SScott Long * 551a7b740SScott Long * Redistribution and use in source and binary forms, with or without 651a7b740SScott Long * modification, are permitted provided that the following conditions 751a7b740SScott Long * are met: 851a7b740SScott Long * 1. Redistributions of source code must retain the above copyright 951a7b740SScott Long * notice, this list of conditions and the following disclaimer. 1051a7b740SScott Long * 2. Redistributions in binary form must reproduce the above copyright 1151a7b740SScott Long * notice, this list of conditions and the following disclaimer in the 1251a7b740SScott Long * documentation and/or other materials provided with the distribution. 1351a7b740SScott Long * 1451a7b740SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1551a7b740SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1651a7b740SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1751a7b740SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1851a7b740SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1951a7b740SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2051a7b740SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2151a7b740SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2251a7b740SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2351a7b740SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2451a7b740SScott Long * SUCH DAMAGE. 2551a7b740SScott Long * 2651a7b740SScott Long * $FreeBSD$ 2751a7b740SScott Long */ 2851a7b740SScott Long 2951a7b740SScott Long /* udf_vnops.c */ 3051a7b740SScott Long /* Take care of the vnode side of things */ 3151a7b740SScott Long 3251a7b740SScott Long #include <sys/param.h> 3351a7b740SScott Long #include <sys/systm.h> 3451a7b740SScott Long #include <sys/namei.h> 3551a7b740SScott Long #include <sys/kernel.h> 3651a7b740SScott Long #include <sys/malloc.h> 3751a7b740SScott Long #include <sys/stat.h> 3851a7b740SScott Long #include <sys/bio.h> 394e94fafcSPoul-Henning Kamp #include <sys/conf.h> 4051a7b740SScott Long #include <sys/buf.h> 416565282cSScott Long #include <sys/iconv.h> 4251a7b740SScott Long #include <sys/mount.h> 4351a7b740SScott Long #include <sys/vnode.h> 4451a7b740SScott Long #include <sys/dirent.h> 4551a7b740SScott Long #include <sys/queue.h> 4651a7b740SScott Long #include <sys/unistd.h> 4789ec2c3cSScott Long #include <sys/endian.h> 4851a7b740SScott Long 4951a7b740SScott Long #include <vm/uma.h> 5051a7b740SScott Long 5151a7b740SScott Long #include <fs/udf/ecma167-udf.h> 5251a7b740SScott Long #include <fs/udf/osta.h> 5351a7b740SScott Long #include <fs/udf/udf.h> 546565282cSScott Long #include <fs/udf/udf_mount.h> 556565282cSScott Long 566565282cSScott Long extern struct iconv_functions *udf_iconv; 5751a7b740SScott Long 586fde64c7SPoul-Henning Kamp static vop_access_t udf_access; 596fde64c7SPoul-Henning Kamp static vop_getattr_t udf_getattr; 606fde64c7SPoul-Henning Kamp static vop_ioctl_t udf_ioctl; 616fde64c7SPoul-Henning Kamp static vop_pathconf_t udf_pathconf; 626fde64c7SPoul-Henning Kamp static vop_read_t udf_read; 636fde64c7SPoul-Henning Kamp static vop_readdir_t udf_readdir; 646fde64c7SPoul-Henning Kamp static vop_readlink_t udf_readlink; 656fde64c7SPoul-Henning Kamp static vop_strategy_t udf_strategy; 666fde64c7SPoul-Henning Kamp static vop_bmap_t udf_bmap; 676fde64c7SPoul-Henning Kamp static vop_cachedlookup_t udf_lookup; 686fde64c7SPoul-Henning Kamp static vop_reclaim_t udf_reclaim; 699d32fde8SScott Long static int udf_readatoffset(struct udf_node *node, int *size, off_t offset, 709d32fde8SScott Long struct buf **bp, uint8_t **data); 719d32fde8SScott Long static int udf_bmap_internal(struct udf_node *node, off_t offset, 729d32fde8SScott Long daddr_t *sector, uint32_t *max_size); 7351a7b740SScott Long 74aec0fb7bSPoul-Henning Kamp static struct vop_vector udf_vnodeops = { 75aec0fb7bSPoul-Henning Kamp .vop_default = &default_vnodeops, 7683c64397SPoul-Henning Kamp 77aec0fb7bSPoul-Henning Kamp .vop_access = udf_access, 78aec0fb7bSPoul-Henning Kamp .vop_bmap = udf_bmap, 79aec0fb7bSPoul-Henning Kamp .vop_cachedlookup = udf_lookup, 80aec0fb7bSPoul-Henning Kamp .vop_getattr = udf_getattr, 81aec0fb7bSPoul-Henning Kamp .vop_ioctl = udf_ioctl, 82aec0fb7bSPoul-Henning Kamp .vop_lookup = vfs_cache_lookup, 83aec0fb7bSPoul-Henning Kamp .vop_pathconf = udf_pathconf, 84aec0fb7bSPoul-Henning Kamp .vop_read = udf_read, 85aec0fb7bSPoul-Henning Kamp .vop_readdir = udf_readdir, 86aec0fb7bSPoul-Henning Kamp .vop_readlink = udf_readlink, 87aec0fb7bSPoul-Henning Kamp .vop_reclaim = udf_reclaim, 88aec0fb7bSPoul-Henning Kamp .vop_strategy = udf_strategy, 8951a7b740SScott Long }; 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 int 9751a7b740SScott Long udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td) 9851a7b740SScott Long { 9951a7b740SScott Long int error; 10051a7b740SScott Long struct vnode *vp; 10151a7b740SScott Long 102aec0fb7bSPoul-Henning Kamp error = getnewvnode("udf", mp, &udf_vnodeops, &vp); 10351a7b740SScott Long if (error) { 10451a7b740SScott Long printf("udf_allocv: failed to allocate new vnode\n"); 10551a7b740SScott Long return (error); 10651a7b740SScott Long } 10751a7b740SScott Long 10851a7b740SScott Long *vpp = vp; 10951a7b740SScott Long return (0); 11051a7b740SScott Long } 11151a7b740SScott Long 11251a7b740SScott Long /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */ 11351a7b740SScott Long static mode_t 11451a7b740SScott Long udf_permtomode(struct udf_node *node) 11551a7b740SScott Long { 116c2d6947dSJeroen Ruigrok van der Werven uint32_t perm; 117bf1c3dddSScott Long uint16_t flags; 11851a7b740SScott Long mode_t mode; 11951a7b740SScott Long 120bf1c3dddSScott Long perm = le32toh(node->fentry->perm); 121bf1c3dddSScott Long flags = le16toh(node->fentry->icbtag.flags); 12251a7b740SScott Long 12351a7b740SScott Long mode = perm & UDF_FENTRY_PERM_USER_MASK; 12451a7b740SScott Long mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2); 12551a7b740SScott Long mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4); 12651a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4); 12751a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6); 12851a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8); 12951a7b740SScott Long 13051a7b740SScott Long return (mode); 13151a7b740SScott Long } 13251a7b740SScott Long 13351a7b740SScott Long static int 13451a7b740SScott Long udf_access(struct vop_access_args *a) 13551a7b740SScott Long { 13651a7b740SScott Long struct vnode *vp; 13751a7b740SScott Long struct udf_node *node; 13851a7b740SScott Long mode_t a_mode, mode; 13951a7b740SScott Long 14051a7b740SScott Long vp = a->a_vp; 14151a7b740SScott Long node = VTON(vp); 14251a7b740SScott Long a_mode = a->a_mode; 14351a7b740SScott Long 14451a7b740SScott Long if (a_mode & VWRITE) { 14551a7b740SScott Long switch (vp->v_type) { 14651a7b740SScott Long case VDIR: 14751a7b740SScott Long case VLNK: 14851a7b740SScott Long case VREG: 14951a7b740SScott Long return (EROFS); 15051a7b740SScott Long /* NOT REACHED */ 15151a7b740SScott Long default: 15251a7b740SScott Long break; 15351a7b740SScott Long } 15451a7b740SScott Long } 15551a7b740SScott Long 15651a7b740SScott Long mode = udf_permtomode(node); 15751a7b740SScott Long 15851a7b740SScott Long return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid, 15951a7b740SScott Long a_mode, a->a_cred, NULL)); 16051a7b740SScott Long } 16151a7b740SScott Long 16251a7b740SScott Long static int mon_lens[2][12] = { 16351a7b740SScott Long {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 16451a7b740SScott Long {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 16551a7b740SScott Long }; 16651a7b740SScott Long 16751a7b740SScott Long static int 16851a7b740SScott Long udf_isaleapyear(int year) 16951a7b740SScott Long { 17051a7b740SScott Long int i; 17151a7b740SScott Long 17251a7b740SScott Long i = (year % 4) ? 0 : 1; 17351a7b740SScott Long i &= (year % 100) ? 1 : 0; 17451a7b740SScott Long i |= (year % 400) ? 0 : 1; 17551a7b740SScott Long 17651a7b740SScott Long return i; 17751a7b740SScott Long } 17851a7b740SScott Long 17951a7b740SScott Long /* 18051a7b740SScott Long * XXX This is just a rough hack. Daylight savings isn't calculated and tv_nsec 18151a7b740SScott Long * is ignored. 18251a7b740SScott Long * Timezone calculation compliments of Julian Elischer <julian@elischer.org>. 18351a7b740SScott Long */ 18451a7b740SScott Long static void 18551a7b740SScott Long udf_timetotimespec(struct timestamp *time, struct timespec *t) 18651a7b740SScott Long { 187bf1c3dddSScott Long int i, lpyear, daysinyear, year; 18851a7b740SScott Long union { 189c2d6947dSJeroen Ruigrok van der Werven uint16_t u_tz_offset; 19051a7b740SScott Long int16_t s_tz_offset; 19151a7b740SScott Long } tz; 19251a7b740SScott Long 19351a7b740SScott Long t->tv_nsec = 0; 19451a7b740SScott Long 19551a7b740SScott Long /* DirectCD seems to like using bogus year values */ 196bf1c3dddSScott Long year = le16toh(time->year); 197bf1c3dddSScott Long if (year < 1970) { 19851a7b740SScott Long t->tv_sec = 0; 19951a7b740SScott Long return; 20051a7b740SScott Long } 20151a7b740SScott Long 20251a7b740SScott Long /* Calculate the time and day */ 20351a7b740SScott Long t->tv_sec = time->second; 20451a7b740SScott Long t->tv_sec += time->minute * 60; 20551a7b740SScott Long t->tv_sec += time->hour * 3600; 20651a7b740SScott Long t->tv_sec += time->day * 3600 * 24; 20751a7b740SScott Long 208befb7f33SChristian Brueffer /* Calculate the month */ 209bf1c3dddSScott Long lpyear = udf_isaleapyear(year); 21051a7b740SScott Long for (i = 1; i < time->month; i++) 21151a7b740SScott Long t->tv_sec += mon_lens[lpyear][i] * 3600 * 24; 21251a7b740SScott Long 21351a7b740SScott Long /* Speed up the calculation */ 214bf1c3dddSScott Long if (year > 1979) 21551a7b740SScott Long t->tv_sec += 315532800; 216bf1c3dddSScott Long if (year > 1989) 21751a7b740SScott Long t->tv_sec += 315619200; 218bf1c3dddSScott Long if (year > 1999) 21951a7b740SScott Long t->tv_sec += 315532800; 220bf1c3dddSScott Long for (i = 2000; i < year; i++) { 22151a7b740SScott Long daysinyear = udf_isaleapyear(i) + 365 ; 22251a7b740SScott Long t->tv_sec += daysinyear * 3600 * 24; 22351a7b740SScott Long } 22451a7b740SScott Long 22551a7b740SScott Long /* 22651a7b740SScott Long * Calculate the time zone. The timezone is 12 bit signed 2's 227befb7f33SChristian Brueffer * complement, so we gotta do some extra magic to handle it right. 22851a7b740SScott Long */ 229bf1c3dddSScott Long tz.u_tz_offset = le16toh(time->type_tz); 23051a7b740SScott Long tz.u_tz_offset &= 0x0fff; 23151a7b740SScott Long if (tz.u_tz_offset & 0x0800) 23251a7b740SScott Long tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */ 23351a7b740SScott Long if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047)) 23451a7b740SScott Long t->tv_sec -= tz.s_tz_offset * 60; 23551a7b740SScott Long 23651a7b740SScott Long return; 23751a7b740SScott Long } 23851a7b740SScott Long 23951a7b740SScott Long static int 24051a7b740SScott Long udf_getattr(struct vop_getattr_args *a) 24151a7b740SScott Long { 24251a7b740SScott Long struct vnode *vp; 24351a7b740SScott Long struct udf_node *node; 24451a7b740SScott Long struct vattr *vap; 24551a7b740SScott Long struct file_entry *fentry; 24651a7b740SScott Long struct timespec ts; 24751a7b740SScott Long 24851a7b740SScott Long ts.tv_sec = 0; 24951a7b740SScott Long 25051a7b740SScott Long vp = a->a_vp; 25151a7b740SScott Long vap = a->a_vap; 25251a7b740SScott Long node = VTON(vp); 25351a7b740SScott Long fentry = node->fentry; 25451a7b740SScott Long 255c049546eSPoul-Henning Kamp vap->va_fsid = dev2udev(node->udfmp->im_dev); 25651a7b740SScott Long vap->va_fileid = node->hash_id; 25751a7b740SScott Long vap->va_mode = udf_permtomode(node); 258bf1c3dddSScott Long vap->va_nlink = le16toh(fentry->link_cnt); 25951a7b740SScott Long /* 26051a7b740SScott Long * XXX The spec says that -1 is valid for uid/gid and indicates an 26151a7b740SScott Long * invalid uid/gid. How should this be represented? 26251a7b740SScott Long */ 263bf1c3dddSScott Long vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid); 264bf1c3dddSScott Long vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid); 26551a7b740SScott Long udf_timetotimespec(&fentry->atime, &vap->va_atime); 26651a7b740SScott Long udf_timetotimespec(&fentry->mtime, &vap->va_mtime); 26751a7b740SScott Long vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */ 26851a7b740SScott Long vap->va_rdev = 0; /* XXX */ 26951a7b740SScott Long if (vp->v_type & VDIR) { 27051a7b740SScott Long /* 27151a7b740SScott Long * Directories that are recorded within their ICB will show 27251a7b740SScott Long * as having 0 blocks recorded. Since tradition dictates 27351a7b740SScott Long * that directories consume at least one logical block, 27451a7b740SScott Long * make it appear so. 27551a7b740SScott Long */ 27651a7b740SScott Long if (fentry->logblks_rec != 0) { 277bf1c3dddSScott Long vap->va_size = 278bf1c3dddSScott Long le64toh(fentry->logblks_rec) * node->udfmp->bsize; 27951a7b740SScott Long } else { 28051a7b740SScott Long vap->va_size = node->udfmp->bsize; 28151a7b740SScott Long } 28251a7b740SScott Long } else { 283bf1c3dddSScott Long vap->va_size = le64toh(fentry->inf_len); 28451a7b740SScott Long } 28551a7b740SScott Long vap->va_flags = 0; 28651a7b740SScott Long vap->va_gen = 1; 28751a7b740SScott Long vap->va_blocksize = node->udfmp->bsize; 288bf1c3dddSScott Long vap->va_bytes = le64toh(fentry->inf_len); 28951a7b740SScott Long vap->va_type = vp->v_type; 29051a7b740SScott Long vap->va_filerev = 0; /* XXX */ 29151a7b740SScott Long return (0); 29251a7b740SScott Long } 29351a7b740SScott Long 29451a7b740SScott Long /* 295bf1c3dddSScott Long * File specific ioctls. 29651a7b740SScott Long */ 29751a7b740SScott Long static int 29851a7b740SScott Long udf_ioctl(struct vop_ioctl_args *a) 29951a7b740SScott Long { 300c80a90c5SScott Long printf("%s called\n", __func__); 3011d02d910SPoul-Henning Kamp return (ENOTTY); 30251a7b740SScott Long } 30351a7b740SScott Long 30451a7b740SScott Long /* 30551a7b740SScott Long * I'm not sure that this has much value in a read-only filesystem, but 30651a7b740SScott Long * cd9660 has it too. 30751a7b740SScott Long */ 30851a7b740SScott Long static int 30951a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a) 31051a7b740SScott Long { 31151a7b740SScott Long 31251a7b740SScott Long switch (a->a_name) { 31351a7b740SScott Long case _PC_LINK_MAX: 31451a7b740SScott Long *a->a_retval = 65535; 31551a7b740SScott Long return (0); 31651a7b740SScott Long case _PC_NAME_MAX: 31751a7b740SScott Long *a->a_retval = NAME_MAX; 31851a7b740SScott Long return (0); 31951a7b740SScott Long case _PC_PATH_MAX: 32051a7b740SScott Long *a->a_retval = PATH_MAX; 32151a7b740SScott Long return (0); 32251a7b740SScott Long case _PC_NO_TRUNC: 32351a7b740SScott Long *a->a_retval = 1; 32451a7b740SScott Long return (0); 32551a7b740SScott Long default: 32651a7b740SScott Long return (EINVAL); 32751a7b740SScott Long } 32851a7b740SScott Long } 32951a7b740SScott Long 33051a7b740SScott Long static int 33151a7b740SScott Long udf_read(struct vop_read_args *a) 33251a7b740SScott Long { 33351a7b740SScott Long struct vnode *vp = a->a_vp; 33451a7b740SScott Long struct uio *uio = a->a_uio; 33551a7b740SScott Long struct udf_node *node = VTON(vp); 33651a7b740SScott Long struct buf *bp; 337c2d6947dSJeroen Ruigrok van der Werven uint8_t *data; 3389d32fde8SScott Long off_t fsize, offset; 33951a7b740SScott Long int error = 0; 3409d32fde8SScott Long int size; 34151a7b740SScott Long 34251a7b740SScott Long if (uio->uio_offset < 0) 34351a7b740SScott Long return (EINVAL); 34451a7b740SScott Long 345bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len); 346d1def83bSScott Long 34751a7b740SScott Long while (uio->uio_offset < fsize && uio->uio_resid > 0) { 34851a7b740SScott Long offset = uio->uio_offset; 34943bc24bfSScott Long if (uio->uio_resid + offset <= fsize) 35043bc24bfSScott Long size = uio->uio_resid; 35143bc24bfSScott Long else 35243bc24bfSScott Long size = fsize - offset; 35351a7b740SScott Long error = udf_readatoffset(node, &size, offset, &bp, &data); 354744bb56dSScott Long if (error == 0) 355c9524588SDag-Erling Smørgrav error = uiomove(data, size, uio); 35651a7b740SScott Long if (bp != NULL) 35751a7b740SScott Long brelse(bp); 35851a7b740SScott Long if (error) 35951a7b740SScott Long break; 36051a7b740SScott Long }; 36151a7b740SScott Long 36251a7b740SScott Long return (error); 36351a7b740SScott Long } 36451a7b740SScott Long 36551a7b740SScott Long /* 36651a7b740SScott Long * Call the OSTA routines to translate the name from a CS0 dstring to a 36751a7b740SScott Long * 16-bit Unicode String. Hooks need to be placed in here to translate from 3686565282cSScott Long * Unicode to the encoding that the kernel/user expects. Return the length 3696565282cSScott Long * of the translated string. 37051a7b740SScott Long */ 37151a7b740SScott Long static int 3726565282cSScott Long udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp) 37351a7b740SScott Long { 37451a7b740SScott Long unicode_t *transname; 3756565282cSScott Long char *unibuf, *unip; 376181fc3c6SR. Imura int i, destlen; 377181fc3c6SR. Imura ssize_t unilen = 0; 3786565282cSScott Long size_t destleft = MAXNAMLEN; 37951a7b740SScott Long 3806565282cSScott Long /* Convert 16-bit Unicode to destname */ 3816565282cSScott Long if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) { 3826565282cSScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */ 3836565282cSScott Long unibuf = uma_zalloc(udf_zone_trans, M_WAITOK); 3846565282cSScott Long unip = unibuf; 385181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) { 3866565282cSScott Long printf("udf: Unicode translation failed\n"); 3876565282cSScott Long uma_zfree(udf_zone_trans, unibuf); 3886565282cSScott Long return 0; 3896565282cSScott Long } 3906565282cSScott Long 3916565282cSScott Long while (unilen > 0 && destleft > 0) { 3926565282cSScott Long udf_iconv->conv(udfmp->im_d2l, (const char **)&unibuf, 3936565282cSScott Long (size_t *)&unilen, (char **)&destname, &destleft); 3946565282cSScott Long /* Unconverted character found */ 3956565282cSScott Long if (unilen > 0 && destleft > 0) { 3966565282cSScott Long *destname++ = '?'; 3976565282cSScott Long destleft--; 3986565282cSScott Long unibuf += 2; 3996565282cSScott Long unilen -= 2; 4006565282cSScott Long } 4016565282cSScott Long } 4026565282cSScott Long uma_zfree(udf_zone_trans, unip); 4036565282cSScott Long *destname = '\0'; 4046565282cSScott Long destlen = MAXNAMLEN - (int)destleft; 4056565282cSScott Long } else { 40651a7b740SScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */ 407a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK); 40851a7b740SScott Long 409181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) { 41051a7b740SScott Long printf("udf: Unicode translation failed\n"); 41151a7b740SScott Long uma_zfree(udf_zone_trans, transname); 41251a7b740SScott Long return 0; 41351a7b740SScott Long } 41451a7b740SScott Long 41551a7b740SScott Long for (i = 0; i < unilen ; i++) { 41651a7b740SScott Long if (transname[i] & 0xff00) { 41751a7b740SScott Long destname[i] = '.'; /* Fudge the 16bit chars */ 41851a7b740SScott Long } else { 41951a7b740SScott Long destname[i] = transname[i] & 0xff; 42051a7b740SScott Long } 42151a7b740SScott Long } 42251a7b740SScott Long uma_zfree(udf_zone_trans, transname); 4236565282cSScott Long destname[unilen] = 0; 424181fc3c6SR. Imura destlen = (int)unilen; 4256565282cSScott Long } 42651a7b740SScott Long 4276565282cSScott Long return (destlen); 42851a7b740SScott Long } 42951a7b740SScott Long 43051a7b740SScott Long /* 43151a7b740SScott Long * Compare a CS0 dstring with a name passed in from the VFS layer. Return 432befb7f33SChristian Brueffer * 0 on a successful match, nonzero otherwise. Unicode work may need to be done 43351a7b740SScott Long * here also. 43451a7b740SScott Long */ 43551a7b740SScott Long static int 4366565282cSScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp) 43751a7b740SScott Long { 43895ec5961SScott Long char *transname; 43995ec5961SScott Long int error = 0; 44051a7b740SScott Long 44195ec5961SScott Long /* This is overkill, but not worth creating a new zone */ 442a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK); 44395ec5961SScott Long 4446565282cSScott Long cs0len = udf_transname(cs0string, transname, cs0len, udfmp); 44551a7b740SScott Long 44651a7b740SScott Long /* Easy check. If they aren't the same length, they aren't equal */ 44795ec5961SScott Long if ((cs0len == 0) || (cs0len != cmplen)) 44895ec5961SScott Long error = -1; 44995ec5961SScott Long else 45095ec5961SScott Long error = bcmp(transname, cmpname, cmplen); 45151a7b740SScott Long 45295ec5961SScott Long uma_zfree(udf_zone_trans, transname); 45395ec5961SScott Long return (error); 45451a7b740SScott Long } 45551a7b740SScott Long 45651a7b740SScott Long struct udf_uiodir { 45751a7b740SScott Long struct dirent *dirent; 45851a7b740SScott Long u_long *cookies; 45951a7b740SScott Long int ncookies; 46051a7b740SScott Long int acookies; 46151a7b740SScott Long int eofflag; 46251a7b740SScott Long }; 46351a7b740SScott Long 46451a7b740SScott Long static int 46551a7b740SScott Long udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie) 46651a7b740SScott Long { 46751a7b740SScott Long if (uiodir->cookies != NULL) { 46851a7b740SScott Long if (++uiodir->acookies > uiodir->ncookies) { 46951a7b740SScott Long uiodir->eofflag = 0; 47051a7b740SScott Long return (-1); 47151a7b740SScott Long } 47251a7b740SScott Long *uiodir->cookies++ = cookie; 47351a7b740SScott Long } 47451a7b740SScott Long 47551a7b740SScott Long if (uio->uio_resid < de_size) { 47651a7b740SScott Long uiodir->eofflag = 0; 47751a7b740SScott Long return (-1); 47851a7b740SScott Long } 47951a7b740SScott Long 480c9524588SDag-Erling Smørgrav return (uiomove(uiodir->dirent, de_size, uio)); 48151a7b740SScott Long } 48251a7b740SScott Long 4831703656aSScott Long static struct udf_dirstream * 4841703656aSScott Long udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp) 4851703656aSScott Long { 4861703656aSScott Long struct udf_dirstream *ds; 4871703656aSScott Long 488a163d034SWarner Losh ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO); 4891703656aSScott Long 4901703656aSScott Long ds->node = node; 4911703656aSScott Long ds->offset = offset; 4921703656aSScott Long ds->udfmp = udfmp; 4931703656aSScott Long ds->fsize = fsize; 4941703656aSScott Long 4951703656aSScott Long return (ds); 4961703656aSScott Long } 4971703656aSScott Long 4981703656aSScott Long static struct fileid_desc * 4991703656aSScott Long udf_getfid(struct udf_dirstream *ds) 5001703656aSScott Long { 5011703656aSScott Long struct fileid_desc *fid; 5021703656aSScott Long int error, frag_size = 0, total_fid_size; 5031703656aSScott Long 5041703656aSScott Long /* End of directory? */ 5051703656aSScott Long if (ds->offset + ds->off >= ds->fsize) { 5061703656aSScott Long ds->error = 0; 5071703656aSScott Long return (NULL); 5081703656aSScott Long } 5091703656aSScott Long 5101703656aSScott Long /* Grab the first extent of the directory */ 5111703656aSScott Long if (ds->off == 0) { 5121703656aSScott Long ds->size = 0; 5131703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset, 5141703656aSScott Long &ds->bp, &ds->data); 5151703656aSScott Long if (error) { 5161703656aSScott Long ds->error = error; 517744bb56dSScott Long if (ds->bp != NULL) 518744bb56dSScott Long brelse(ds->bp); 5191703656aSScott Long return (NULL); 5201703656aSScott Long } 5211703656aSScott Long } 5221703656aSScott Long 5234576293dSScott Long /* 5244576293dSScott Long * Clean up from a previous fragmented FID. 5254576293dSScott Long * XXX Is this the right place for this? 5264576293dSScott Long */ 5271703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) { 5281703656aSScott Long ds->fid_fragment = 0; 5291703656aSScott Long FREE(ds->buf, M_UDFFID); 5301703656aSScott Long } 5311703656aSScott Long 5321703656aSScott Long fid = (struct fileid_desc*)&ds->data[ds->off]; 5331703656aSScott Long 5341703656aSScott Long /* 5351703656aSScott Long * Check to see if the fid is fragmented. The first test 5361703656aSScott Long * ensures that we don't wander off the end of the buffer 5371703656aSScott Long * looking for the l_iu and l_fi fields. 5381703656aSScott Long */ 5391703656aSScott Long if (ds->off + UDF_FID_SIZE > ds->size || 540bf1c3dddSScott Long ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){ 5411703656aSScott Long 5421703656aSScott Long /* Copy what we have of the fid into a buffer */ 5431703656aSScott Long frag_size = ds->size - ds->off; 5441703656aSScott Long if (frag_size >= ds->udfmp->bsize) { 5451703656aSScott Long printf("udf: invalid FID fragment\n"); 5461703656aSScott Long ds->error = EINVAL; 5471703656aSScott Long return (NULL); 5481703656aSScott Long } 5491703656aSScott Long 5501703656aSScott Long /* 5511703656aSScott Long * File ID descriptors can only be at most one 5521703656aSScott Long * logical sector in size. 5531703656aSScott Long */ 5541703656aSScott Long MALLOC(ds->buf, uint8_t*, ds->udfmp->bsize, M_UDFFID, 555a163d034SWarner Losh M_WAITOK | M_ZERO); 5561703656aSScott Long bcopy(fid, ds->buf, frag_size); 5571703656aSScott Long 5581703656aSScott Long /* Reduce all of the casting magic */ 5591703656aSScott Long fid = (struct fileid_desc*)ds->buf; 5601703656aSScott Long 5611703656aSScott Long if (ds->bp != NULL) 5621703656aSScott Long brelse(ds->bp); 5631703656aSScott Long 5641703656aSScott Long /* Fetch the next allocation */ 5651703656aSScott Long ds->offset += ds->size; 5661703656aSScott Long ds->size = 0; 5671703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset, 5681703656aSScott Long &ds->bp, &ds->data); 5691703656aSScott Long if (error) { 5701703656aSScott Long ds->error = error; 5711703656aSScott Long return (NULL); 5721703656aSScott Long } 5731703656aSScott Long 5741703656aSScott Long /* 5751703656aSScott Long * If the fragment was so small that we didn't get 5761703656aSScott Long * the l_iu and l_fi fields, copy those in. 5771703656aSScott Long */ 5781703656aSScott Long if (frag_size < UDF_FID_SIZE) 5791703656aSScott Long bcopy(ds->data, &ds->buf[frag_size], 5801703656aSScott Long UDF_FID_SIZE - frag_size); 5811703656aSScott Long 5821703656aSScott Long /* 5831703656aSScott Long * Now that we have enough of the fid to work with, 5841703656aSScott Long * copy in the rest of the fid from the new 5851703656aSScott Long * allocation. 5861703656aSScott Long */ 587bf1c3dddSScott Long total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi; 5881703656aSScott Long if (total_fid_size > ds->udfmp->bsize) { 5891703656aSScott Long printf("udf: invalid FID\n"); 5901703656aSScott Long ds->error = EIO; 5911703656aSScott Long return (NULL); 5921703656aSScott Long } 5931703656aSScott Long bcopy(ds->data, &ds->buf[frag_size], 5941703656aSScott Long total_fid_size - frag_size); 5951703656aSScott Long 5961703656aSScott Long ds->fid_fragment = 1; 5971703656aSScott Long } else { 598bf1c3dddSScott Long total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE; 5991703656aSScott Long } 6001703656aSScott Long 6011703656aSScott Long /* 6021703656aSScott Long * Update the offset. Align on a 4 byte boundary because the 6034576293dSScott Long * UDF spec says so. 6041703656aSScott Long */ 6051703656aSScott Long ds->this_off = ds->off; 6061703656aSScott Long if (!ds->fid_fragment) { 6071703656aSScott Long ds->off += (total_fid_size + 3) & ~0x03; 6081703656aSScott Long } else { 6091703656aSScott Long ds->off = (total_fid_size - frag_size + 3) & ~0x03; 6101703656aSScott Long } 6111703656aSScott Long 6121703656aSScott Long return (fid); 6131703656aSScott Long } 6141703656aSScott Long 6151703656aSScott Long static void 6161703656aSScott Long udf_closedir(struct udf_dirstream *ds) 6171703656aSScott Long { 6181703656aSScott Long 6191703656aSScott Long if (ds->bp != NULL) 6201703656aSScott Long brelse(ds->bp); 6211703656aSScott Long 6221703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) 6231703656aSScott Long FREE(ds->buf, M_UDFFID); 6241703656aSScott Long 6251703656aSScott Long uma_zfree(udf_zone_ds, ds); 6261703656aSScott Long } 6271703656aSScott Long 62851a7b740SScott Long static int 62951a7b740SScott Long udf_readdir(struct vop_readdir_args *a) 63051a7b740SScott Long { 63151a7b740SScott Long struct vnode *vp; 63251a7b740SScott Long struct uio *uio; 63351a7b740SScott Long struct dirent dir; 63451a7b740SScott Long struct udf_node *node; 6356565282cSScott Long struct udf_mnt *udfmp; 63651a7b740SScott Long struct fileid_desc *fid; 63751a7b740SScott Long struct udf_uiodir uiodir; 6381703656aSScott Long struct udf_dirstream *ds; 63951a7b740SScott Long u_long *cookies = NULL; 64051a7b740SScott Long int ncookies; 641c8eeea2fSScott Long int error = 0; 64251a7b740SScott Long 64351a7b740SScott Long vp = a->a_vp; 64451a7b740SScott Long uio = a->a_uio; 64551a7b740SScott Long node = VTON(vp); 6466565282cSScott Long udfmp = node->udfmp; 64751a7b740SScott Long uiodir.eofflag = 1; 64851a7b740SScott Long 64951a7b740SScott Long if (a->a_ncookies != NULL) { 65051a7b740SScott Long /* 65151a7b740SScott Long * Guess how many entries are needed. If we run out, this 65251a7b740SScott Long * function will be called again and thing will pick up were 65351a7b740SScott Long * it left off. 65451a7b740SScott Long */ 65551a7b740SScott Long ncookies = uio->uio_resid / 8; 65651a7b740SScott Long MALLOC(cookies, u_long *, sizeof(u_long) * ncookies, 657a163d034SWarner Losh M_TEMP, M_WAITOK); 65851a7b740SScott Long if (cookies == NULL) 65951a7b740SScott Long return (ENOMEM); 66051a7b740SScott Long uiodir.ncookies = ncookies; 66151a7b740SScott Long uiodir.cookies = cookies; 66251a7b740SScott Long uiodir.acookies = 0; 66351a7b740SScott Long } else { 66451a7b740SScott Long uiodir.cookies = NULL; 66551a7b740SScott Long } 66651a7b740SScott Long 66751a7b740SScott Long /* 66851a7b740SScott Long * Iterate through the file id descriptors. Give the parent dir 6694576293dSScott Long * entry special attention. 67051a7b740SScott Long */ 671bf1c3dddSScott Long ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len), 6721703656aSScott Long node->udfmp); 67351a7b740SScott Long 6741703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) { 67551a7b740SScott Long 67651a7b740SScott Long /* XXX Should we return an error on a bad fid? */ 67751a7b740SScott Long if (udf_checktag(&fid->tag, TAGID_FID)) { 67851a7b740SScott Long printf("Invalid FID tag\n"); 67977411499SScott Long hexdump(fid, UDF_FID_SIZE, NULL, 0); 6801703656aSScott Long error = EIO; 68151a7b740SScott Long break; 68251a7b740SScott Long } 68351a7b740SScott Long 68451a7b740SScott Long /* Is this a deleted file? */ 6852bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL) 6861703656aSScott Long continue; 68751a7b740SScott Long 6882bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 68951a7b740SScott Long /* Do up the '.' and '..' entries. Dummy values are 69051a7b740SScott Long * used for the cookies since the offset here is 69151a7b740SScott Long * usually zero, and NFS doesn't like that value 69251a7b740SScott Long */ 693c8eeea2fSScott Long dir.d_fileno = node->hash_id; 694c8eeea2fSScott Long dir.d_type = DT_DIR; 695c8eeea2fSScott Long dir.d_name[0] = '.'; 696444acc16SScott Long dir.d_name[1] = '\0'; 697c8eeea2fSScott Long dir.d_namlen = 1; 698c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 699c8eeea2fSScott Long uiodir.dirent = &dir; 700c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1); 70151a7b740SScott Long if (error) 70251a7b740SScott Long break; 70351a7b740SScott Long 704c8eeea2fSScott Long dir.d_fileno = udf_getid(&fid->icb); 705c8eeea2fSScott Long dir.d_type = DT_DIR; 706c8eeea2fSScott Long dir.d_name[0] = '.'; 707c8eeea2fSScott Long dir.d_name[1] = '.'; 708444acc16SScott Long dir.d_name[2] = '\0'; 709c8eeea2fSScott Long dir.d_namlen = 2; 710c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 711c8eeea2fSScott Long uiodir.dirent = &dir; 712c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2); 71351a7b740SScott Long } else { 71451a7b740SScott Long dir.d_namlen = udf_transname(&fid->data[fid->l_iu], 7156565282cSScott Long &dir.d_name[0], fid->l_fi, udfmp); 71651a7b740SScott Long dir.d_fileno = udf_getid(&fid->icb); 7172bbe0d36SScott Long dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ? 7182bbe0d36SScott Long DT_DIR : DT_UNKNOWN; 71951a7b740SScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 72051a7b740SScott Long uiodir.dirent = &dir; 7211703656aSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 7221703656aSScott Long ds->this_off); 72351a7b740SScott Long } 72451a7b740SScott Long if (error) { 72551a7b740SScott Long printf("uiomove returned %d\n", error); 72651a7b740SScott Long break; 72751a7b740SScott Long } 72851a7b740SScott Long 72951a7b740SScott Long } 73051a7b740SScott Long 73151a7b740SScott Long /* tell the calling layer whether we need to be called again */ 73251a7b740SScott Long *a->a_eofflag = uiodir.eofflag; 7331703656aSScott Long uio->uio_offset = ds->offset + ds->off; 73451a7b740SScott Long 7351703656aSScott Long if (!error) 7361703656aSScott Long error = ds->error; 7371703656aSScott Long 7381703656aSScott Long udf_closedir(ds); 73951a7b740SScott Long 74051a7b740SScott Long if (a->a_ncookies != NULL) { 74151a7b740SScott Long if (error) 7421703656aSScott Long FREE(cookies, M_TEMP); 74351a7b740SScott Long else { 74451a7b740SScott Long *a->a_ncookies = uiodir.acookies; 74551a7b740SScott Long *a->a_cookies = cookies; 74651a7b740SScott Long } 74751a7b740SScott Long } 74851a7b740SScott Long 74951a7b740SScott Long return (error); 75051a7b740SScott Long } 75151a7b740SScott Long 75251a7b740SScott Long /* Are there any implementations out there that do soft-links? */ 75351a7b740SScott Long static int 75451a7b740SScott Long udf_readlink(struct vop_readlink_args *ap) 75551a7b740SScott Long { 756c80a90c5SScott Long printf("%s called\n", __func__); 75751a7b740SScott Long return (EOPNOTSUPP); 75851a7b740SScott Long } 75951a7b740SScott Long 76051a7b740SScott Long static int 76151a7b740SScott Long udf_strategy(struct vop_strategy_args *a) 76251a7b740SScott Long { 76351a7b740SScott Long struct buf *bp; 76451a7b740SScott Long struct vnode *vp; 76551a7b740SScott Long struct udf_node *node; 76651a7b740SScott Long int maxsize; 767429c018aSPoul-Henning Kamp struct bufobj *bo; 76851a7b740SScott Long 76951a7b740SScott Long bp = a->a_bp; 770d83b7498SPoul-Henning Kamp vp = a->a_vp; 77151a7b740SScott Long node = VTON(vp); 77251a7b740SScott Long 77351a7b740SScott Long /* cd9660 has this test reversed, but it seems more logical this way */ 77451a7b740SScott Long if (bp->b_blkno != bp->b_lblkno) { 77551a7b740SScott Long /* 77651a7b740SScott Long * Files that are embedded in the fentry don't translate well 77751a7b740SScott Long * to a block number. Reject. 77851a7b740SScott Long */ 77951a7b740SScott Long if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize, 78051a7b740SScott Long &bp->b_lblkno, &maxsize)) { 78151a7b740SScott Long clrbuf(bp); 78251a7b740SScott Long bp->b_blkno = -1; 78351a7b740SScott Long } 78451a7b740SScott Long } 78551a7b740SScott Long if ((long)bp->b_blkno == -1) { 78651a7b740SScott Long bufdone(bp); 78751a7b740SScott Long return (0); 78851a7b740SScott Long } 789429c018aSPoul-Henning Kamp bo = node->udfmp->im_bo; 7902c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 7910391e5a1SPoul-Henning Kamp BO_STRATEGY(bo, bp); 79251a7b740SScott Long return (0); 79351a7b740SScott Long } 79451a7b740SScott Long 79551a7b740SScott Long static int 79651a7b740SScott Long udf_bmap(struct vop_bmap_args *a) 79751a7b740SScott Long { 79851a7b740SScott Long struct udf_node *node; 799c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size; 80098b0c789SPoul-Henning Kamp daddr_t lsector; 80151a7b740SScott Long int error; 80251a7b740SScott Long 80351a7b740SScott Long node = VTON(a->a_vp); 80451a7b740SScott Long 8059c83534dSPoul-Henning Kamp if (a->a_bop != NULL) 806e0251bbbSPoul-Henning Kamp *a->a_bop = &node->udfmp->im_devvp->v_bufobj; 80751a7b740SScott Long if (a->a_bnp == NULL) 80851a7b740SScott Long return (0); 80951a7b740SScott Long if (a->a_runb) 81051a7b740SScott Long *a->a_runb = 0; 81151a7b740SScott Long 812cd1b1a1dSScott Long error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector, 81351a7b740SScott Long &max_size); 8148db4c2f2SScott Long if (error) 81551a7b740SScott Long return (error); 81651a7b740SScott Long 817cd1b1a1dSScott Long /* Translate logical to physical sector number */ 818cd1b1a1dSScott Long *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT); 819cd1b1a1dSScott Long 82051a7b740SScott Long /* Punt on read-ahead for now */ 82151a7b740SScott Long if (a->a_runp) 82251a7b740SScott Long *a->a_runp = 0; 82351a7b740SScott Long 82451a7b740SScott Long return (0); 82551a7b740SScott Long } 82651a7b740SScott Long 82751a7b740SScott Long /* 82851a7b740SScott Long * The all powerful VOP_LOOKUP(). 82951a7b740SScott Long */ 83051a7b740SScott Long static int 83151a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a) 83251a7b740SScott Long { 83351a7b740SScott Long struct vnode *dvp; 83451a7b740SScott Long struct vnode *tdp = NULL; 83551a7b740SScott Long struct vnode **vpp = a->a_vpp; 83651a7b740SScott Long struct udf_node *node; 83751a7b740SScott Long struct udf_mnt *udfmp; 83851a7b740SScott Long struct fileid_desc *fid = NULL; 8391703656aSScott Long struct udf_dirstream *ds; 84051a7b740SScott Long struct thread *td; 84151a7b740SScott Long u_long nameiop; 84251a7b740SScott Long u_long flags; 84351a7b740SScott Long char *nameptr; 84451a7b740SScott Long long namelen; 84551a7b740SScott Long ino_t id = 0; 8461703656aSScott Long int offset, error = 0; 8471703656aSScott Long int numdirpasses, fsize; 84851a7b740SScott Long 84951a7b740SScott Long dvp = a->a_dvp; 85051a7b740SScott Long node = VTON(dvp); 85151a7b740SScott Long udfmp = node->udfmp; 85251a7b740SScott Long nameiop = a->a_cnp->cn_nameiop; 85351a7b740SScott Long flags = a->a_cnp->cn_flags; 85451a7b740SScott Long nameptr = a->a_cnp->cn_nameptr; 85551a7b740SScott Long namelen = a->a_cnp->cn_namelen; 856bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len); 85751a7b740SScott Long td = a->a_cnp->cn_thread; 85851a7b740SScott Long 85951a7b740SScott Long /* 86051a7b740SScott Long * If this is a LOOKUP and we've already partially searched through 86151a7b740SScott Long * the directory, pick up where we left off and flag that the 86251a7b740SScott Long * directory may need to be searched twice. For a full description, 86351a7b740SScott Long * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup() 86451a7b740SScott Long */ 8651703656aSScott Long if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) { 86651a7b740SScott Long offset = 0; 86751a7b740SScott Long numdirpasses = 1; 86851a7b740SScott Long } else { 86951a7b740SScott Long offset = node->diroff; 87051a7b740SScott Long numdirpasses = 2; 87151a7b740SScott Long nchstats.ncs_2passes++; 87251a7b740SScott Long } 87351a7b740SScott Long 87451a7b740SScott Long lookloop: 8751703656aSScott Long ds = udf_opendir(node, offset, fsize, udfmp); 87651a7b740SScott Long 8771703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) { 87851a7b740SScott Long 87951a7b740SScott Long /* XXX Should we return an error on a bad fid? */ 8801703656aSScott Long if (udf_checktag(&fid->tag, TAGID_FID)) { 8811703656aSScott Long printf("udf_lookup: Invalid tag\n"); 8821703656aSScott Long error = EIO; 8831703656aSScott Long break; 8841703656aSScott Long } 885678d5effSScott Long 886678d5effSScott Long /* Is this a deleted file? */ 8872bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL) 8881703656aSScott Long continue; 88951a7b740SScott Long 8902bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 89151a7b740SScott Long if (flags & ISDOTDOT) { 89251a7b740SScott Long id = udf_getid(&fid->icb); 89351a7b740SScott Long break; 89451a7b740SScott Long } 89551a7b740SScott Long } else { 89651a7b740SScott Long if (!(udf_cmpname(&fid->data[fid->l_iu], 8976565282cSScott Long nameptr, fid->l_fi, namelen, udfmp))) { 89851a7b740SScott Long id = udf_getid(&fid->icb); 89951a7b740SScott Long break; 90051a7b740SScott Long } 90151a7b740SScott Long } 90251a7b740SScott Long } 9031703656aSScott Long 9041703656aSScott Long if (!error) 9051703656aSScott Long error = ds->error; 9061703656aSScott Long 9071703656aSScott Long /* XXX Bail out here? */ 9081703656aSScott Long if (error) { 9091703656aSScott Long udf_closedir(ds); 9101703656aSScott Long return (error); 91151a7b740SScott Long } 91251a7b740SScott Long 91351a7b740SScott Long /* Did we have a match? */ 91451a7b740SScott Long if (id) { 91527ad03cbSJeff Roberson if (flags & ISDOTDOT) 91627ad03cbSJeff Roberson VOP_UNLOCK(dvp, 0, a->a_cnp->cn_thread); 91751a7b740SScott Long error = udf_vget(udfmp->im_mountp, id, LK_EXCLUSIVE, &tdp); 9184585e3acSJeff Roberson vn_lock(dvp, LK_EXCLUSIVE|LK_RETRY, a->a_cnp->cn_thread); 9191703656aSScott Long if (!error) { 9201703656aSScott Long /* 9211703656aSScott Long * Remember where this entry was if it's the final 9221703656aSScott Long * component. 9231703656aSScott Long */ 92451a7b740SScott Long if ((flags & ISLASTCN) && nameiop == LOOKUP) 9251703656aSScott Long node->diroff = ds->offset + ds->off; 92651a7b740SScott Long if (numdirpasses == 2) 92751a7b740SScott Long nchstats.ncs_pass2++; 92851a7b740SScott Long *vpp = tdp; 92951a7b740SScott Long /* Put this entry in the cache */ 93051a7b740SScott Long if (flags & MAKEENTRY) 93151a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp); 9324585e3acSJeff Roberson } 9331703656aSScott Long } else { 93451a7b740SScott Long /* Name wasn't found on this pass. Do another pass? */ 93551a7b740SScott Long if (numdirpasses == 2) { 93651a7b740SScott Long numdirpasses--; 93751a7b740SScott Long offset = 0; 9381703656aSScott Long udf_closedir(ds); 93951a7b740SScott Long goto lookloop; 94051a7b740SScott Long } 94151a7b740SScott Long 94251a7b740SScott Long /* Enter name into cache as non-existant */ 94351a7b740SScott Long if (flags & MAKEENTRY) 94451a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp); 94551a7b740SScott Long 9461703656aSScott Long if ((flags & ISLASTCN) && 9471703656aSScott Long (nameiop == CREATE || nameiop == RENAME)) { 9481703656aSScott Long error = EROFS; 9491703656aSScott Long } else { 9501703656aSScott Long error = ENOENT; 9511703656aSScott Long } 9521703656aSScott Long } 95351a7b740SScott Long 9541703656aSScott Long udf_closedir(ds); 9551703656aSScott Long return (error); 95651a7b740SScott Long } 95751a7b740SScott Long 95851a7b740SScott Long static int 95951a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a) 96051a7b740SScott Long { 96151a7b740SScott Long struct vnode *vp; 96251a7b740SScott Long struct udf_node *unode; 96351a7b740SScott Long 96451a7b740SScott Long vp = a->a_vp; 96551a7b740SScott Long unode = VTON(vp); 96651a7b740SScott Long 96751a7b740SScott Long if (unode != NULL) { 9684e94fafcSPoul-Henning Kamp vfs_hash_remove(vp); 96951a7b740SScott Long 97051a7b740SScott Long if (unode->fentry != NULL) 97151a7b740SScott Long FREE(unode->fentry, M_UDFFENTRY); 97251a7b740SScott Long uma_zfree(udf_zone_node, unode); 97351a7b740SScott Long vp->v_data = NULL; 97451a7b740SScott Long } 975a369f34dSPoul-Henning Kamp vnode_destroy_vobject(vp); 97651a7b740SScott Long 97751a7b740SScott Long return (0); 97851a7b740SScott Long } 97951a7b740SScott Long 98051a7b740SScott Long /* 98151a7b740SScott Long * Read the block and then set the data pointer to correspond with the 98251a7b740SScott Long * offset passed in. Only read in at most 'size' bytes, and then set 'size' 98351a7b740SScott Long * to the number of bytes pointed to. If 'size' is zero, try to read in a 98451a7b740SScott Long * whole extent. 985744bb56dSScott Long * 986744bb56dSScott Long * Note that *bp may be assigned error or not. 987744bb56dSScott Long * 98851a7b740SScott Long */ 98951a7b740SScott Long static int 9909d32fde8SScott Long udf_readatoffset(struct udf_node *node, int *size, off_t offset, 9919d32fde8SScott Long struct buf **bp, uint8_t **data) 99251a7b740SScott Long { 99351a7b740SScott Long struct udf_mnt *udfmp; 99451a7b740SScott Long struct file_entry *fentry = NULL; 99551a7b740SScott Long struct buf *bp1; 996c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size; 99798b0c789SPoul-Henning Kamp daddr_t sector; 99851a7b740SScott Long int error; 99951a7b740SScott Long 100051a7b740SScott Long udfmp = node->udfmp; 100151a7b740SScott Long 1002744bb56dSScott Long *bp = NULL; 100351a7b740SScott Long error = udf_bmap_internal(node, offset, §or, &max_size); 10044576293dSScott Long if (error == UDF_INVALID_BMAP) { 100551a7b740SScott Long /* 100651a7b740SScott Long * This error means that the file *data* is stored in the 100751a7b740SScott Long * allocation descriptor field of the file entry. 100851a7b740SScott Long */ 100951a7b740SScott Long fentry = node->fentry; 1010bf1c3dddSScott Long *data = &fentry->data[le32toh(fentry->l_ea)]; 1011bf1c3dddSScott Long *size = le32toh(fentry->l_ad); 101251a7b740SScott Long return (0); 101351a7b740SScott Long } else if (error != 0) { 101451a7b740SScott Long return (error); 101551a7b740SScott Long } 101651a7b740SScott Long 1017d1def83bSScott Long /* Adjust the size so that it is within range */ 101851a7b740SScott Long if (*size == 0 || *size > max_size) 101951a7b740SScott Long *size = max_size; 1020d1def83bSScott Long *size = min(*size, MAXBSIZE); 102151a7b740SScott Long 102251a7b740SScott Long if ((error = udf_readlblks(udfmp, sector, *size, bp))) { 10231830bca1SScott Long printf("warning: udf_readlblks returned error %d\n", error); 1024744bb56dSScott Long /* note: *bp may be non-NULL */ 102551a7b740SScott Long return (error); 102651a7b740SScott Long } 102751a7b740SScott Long 102851a7b740SScott Long bp1 = *bp; 1029c2d6947dSJeroen Ruigrok van der Werven *data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize]; 103051a7b740SScott Long return (0); 103151a7b740SScott Long } 103251a7b740SScott Long 103351a7b740SScott Long /* 103451a7b740SScott Long * Translate a file offset into a logical block and then into a physical 103551a7b740SScott Long * block. 103651a7b740SScott Long */ 103751a7b740SScott Long static int 10389d32fde8SScott Long udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector, 10399d32fde8SScott Long uint32_t *max_size) 104051a7b740SScott Long { 104151a7b740SScott Long struct udf_mnt *udfmp; 104251a7b740SScott Long struct file_entry *fentry; 104351a7b740SScott Long void *icb; 104451a7b740SScott Long struct icb_tag *tag; 1045c2d6947dSJeroen Ruigrok van der Werven uint32_t icblen = 0; 104698b0c789SPoul-Henning Kamp daddr_t lsector; 104751a7b740SScott Long int ad_offset, ad_num = 0; 104851a7b740SScott Long int i, p_offset; 104951a7b740SScott Long 105051a7b740SScott Long udfmp = node->udfmp; 105151a7b740SScott Long fentry = node->fentry; 105251a7b740SScott Long tag = &fentry->icbtag; 105351a7b740SScott Long 1054bf1c3dddSScott Long switch (le16toh(tag->strat_type)) { 105551a7b740SScott Long case 4: 105651a7b740SScott Long break; 105751a7b740SScott Long 105851a7b740SScott Long case 4096: 105951a7b740SScott Long printf("Cannot deal with strategy4096 yet!\n"); 106051a7b740SScott Long return (ENODEV); 106151a7b740SScott Long 106251a7b740SScott Long default: 106351a7b740SScott Long printf("Unknown strategy type %d\n", tag->strat_type); 106451a7b740SScott Long return (ENODEV); 106551a7b740SScott Long } 106651a7b740SScott Long 1067bf1c3dddSScott Long switch (le16toh(tag->flags) & 0x7) { 106851a7b740SScott Long case 0: 106951a7b740SScott Long /* 107051a7b740SScott Long * The allocation descriptor field is filled with short_ad's. 107151a7b740SScott Long * If the offset is beyond the current extent, look for the 107251a7b740SScott Long * next extent. 107351a7b740SScott Long */ 107451a7b740SScott Long do { 107551a7b740SScott Long offset -= icblen; 107651a7b740SScott Long ad_offset = sizeof(struct short_ad) * ad_num; 1077bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) { 107851a7b740SScott Long printf("File offset out of bounds\n"); 107951a7b740SScott Long return (EINVAL); 108051a7b740SScott Long } 1081a4d629e3SScott Long icb = GETICB(short_ad, fentry, 1082bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset); 108351a7b740SScott Long icblen = GETICBLEN(short_ad, icb); 108451a7b740SScott Long ad_num++; 108551a7b740SScott Long } while(offset >= icblen); 108651a7b740SScott Long 108751a7b740SScott Long lsector = (offset >> udfmp->bshift) + 108851a7b740SScott Long ((struct short_ad *)(icb))->pos; 108951a7b740SScott Long 10901830bca1SScott Long *max_size = GETICBLEN(short_ad, icb); 109151a7b740SScott Long 109251a7b740SScott Long break; 109351a7b740SScott Long case 1: 109451a7b740SScott Long /* 109551a7b740SScott Long * The allocation descriptor field is filled with long_ad's 109651a7b740SScott Long * If the offset is beyond the current extent, look for the 109751a7b740SScott Long * next extent. 109851a7b740SScott Long */ 109951a7b740SScott Long do { 110051a7b740SScott Long offset -= icblen; 110151a7b740SScott Long ad_offset = sizeof(struct long_ad) * ad_num; 1102bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) { 110351a7b740SScott Long printf("File offset out of bounds\n"); 110451a7b740SScott Long return (EINVAL); 110551a7b740SScott Long } 1106bf1c3dddSScott Long icb = GETICB(long_ad, fentry, 1107bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset); 110851a7b740SScott Long icblen = GETICBLEN(long_ad, icb); 110951a7b740SScott Long ad_num++; 111051a7b740SScott Long } while(offset >= icblen); 111151a7b740SScott Long 111251a7b740SScott Long lsector = (offset >> udfmp->bshift) + 1113bf1c3dddSScott Long le32toh(((struct long_ad *)(icb))->loc.lb_num); 111451a7b740SScott Long 11151830bca1SScott Long *max_size = GETICBLEN(long_ad, icb); 111651a7b740SScott Long 111751a7b740SScott Long break; 111851a7b740SScott Long case 3: 111951a7b740SScott Long /* 112051a7b740SScott Long * This type means that the file *data* is stored in the 112151a7b740SScott Long * allocation descriptor field of the file entry. 112251a7b740SScott Long */ 112351a7b740SScott Long *max_size = 0; 11248db4c2f2SScott Long *sector = node->hash_id + udfmp->part_start; 112551a7b740SScott Long 11264576293dSScott Long return (UDF_INVALID_BMAP); 112751a7b740SScott Long case 2: 112851a7b740SScott Long /* DirectCD does not use extended_ad's */ 112951a7b740SScott Long default: 113051a7b740SScott Long printf("Unsupported allocation descriptor %d\n", 113151a7b740SScott Long tag->flags & 0x7); 113251a7b740SScott Long return (ENODEV); 113351a7b740SScott Long } 113451a7b740SScott Long 113551a7b740SScott Long *sector = lsector + udfmp->part_start; 113651a7b740SScott Long 113751a7b740SScott Long /* 113851a7b740SScott Long * Check the sparing table. Each entry represents the beginning of 113951a7b740SScott Long * a packet. 114051a7b740SScott Long */ 114151a7b740SScott Long if (udfmp->s_table != NULL) { 114251a7b740SScott Long for (i = 0; i< udfmp->s_table_entries; i++) { 1143bf1c3dddSScott Long p_offset = 1144bf1c3dddSScott Long lsector - le32toh(udfmp->s_table->entries[i].org); 114551a7b740SScott Long if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) { 1146bf1c3dddSScott Long *sector = 1147bf1c3dddSScott Long le32toh(udfmp->s_table->entries[i].map) + 114851a7b740SScott Long p_offset; 114951a7b740SScott Long break; 115051a7b740SScott Long } 115151a7b740SScott Long } 115251a7b740SScott Long } 115351a7b740SScott Long 115451a7b740SScott Long return (0); 115551a7b740SScott Long } 1156