151a7b740SScott Long /*- 251a7b740SScott Long * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org> 351a7b740SScott Long * All rights reserved. 451a7b740SScott Long * 551a7b740SScott Long * Redistribution and use in source and binary forms, with or without 651a7b740SScott Long * modification, are permitted provided that the following conditions 751a7b740SScott Long * are met: 851a7b740SScott Long * 1. Redistributions of source code must retain the above copyright 951a7b740SScott Long * notice, this list of conditions and the following disclaimer. 1051a7b740SScott Long * 2. Redistributions in binary form must reproduce the above copyright 1151a7b740SScott Long * notice, this list of conditions and the following disclaimer in the 1251a7b740SScott Long * documentation and/or other materials provided with the distribution. 1351a7b740SScott Long * 1451a7b740SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1551a7b740SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1651a7b740SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1751a7b740SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1851a7b740SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1951a7b740SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2051a7b740SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2151a7b740SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2251a7b740SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2351a7b740SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2451a7b740SScott Long * SUCH DAMAGE. 2551a7b740SScott Long * 2651a7b740SScott Long * $FreeBSD$ 2751a7b740SScott Long */ 2851a7b740SScott Long 2951a7b740SScott Long /* udf_vnops.c */ 3051a7b740SScott Long /* Take care of the vnode side of things */ 3151a7b740SScott Long 3251a7b740SScott Long #include <sys/param.h> 3351a7b740SScott Long #include <sys/systm.h> 3451a7b740SScott Long #include <sys/namei.h> 3551a7b740SScott Long #include <sys/kernel.h> 3651a7b740SScott Long #include <sys/malloc.h> 3751a7b740SScott Long #include <sys/stat.h> 3851a7b740SScott Long #include <sys/bio.h> 394e94fafcSPoul-Henning Kamp #include <sys/conf.h> 4051a7b740SScott Long #include <sys/buf.h> 416565282cSScott Long #include <sys/iconv.h> 4251a7b740SScott Long #include <sys/mount.h> 4351a7b740SScott Long #include <sys/vnode.h> 4451a7b740SScott Long #include <sys/dirent.h> 4551a7b740SScott Long #include <sys/queue.h> 4651a7b740SScott Long #include <sys/unistd.h> 4789ec2c3cSScott Long #include <sys/endian.h> 4851a7b740SScott Long 4951a7b740SScott Long #include <vm/uma.h> 5051a7b740SScott Long 5161e69c80SJohn Baldwin #include <fs/fifofs/fifo.h> 5251a7b740SScott Long #include <fs/udf/ecma167-udf.h> 5351a7b740SScott Long #include <fs/udf/osta.h> 5451a7b740SScott Long #include <fs/udf/udf.h> 556565282cSScott Long #include <fs/udf/udf_mount.h> 566565282cSScott Long 576565282cSScott Long extern struct iconv_functions *udf_iconv; 5851a7b740SScott Long 596fde64c7SPoul-Henning Kamp static vop_access_t udf_access; 606fde64c7SPoul-Henning Kamp static vop_getattr_t udf_getattr; 6135e06624SPav Lucistnik static vop_open_t udf_open; 626fde64c7SPoul-Henning Kamp static vop_ioctl_t udf_ioctl; 636fde64c7SPoul-Henning Kamp static vop_pathconf_t udf_pathconf; 6461e69c80SJohn Baldwin static vop_print_t udf_print; 656fde64c7SPoul-Henning Kamp static vop_read_t udf_read; 666fde64c7SPoul-Henning Kamp static vop_readdir_t udf_readdir; 676fde64c7SPoul-Henning Kamp static vop_readlink_t udf_readlink; 6861e69c80SJohn Baldwin static vop_setattr_t udf_setattr; 696fde64c7SPoul-Henning Kamp static vop_strategy_t udf_strategy; 706fde64c7SPoul-Henning Kamp static vop_bmap_t udf_bmap; 716fde64c7SPoul-Henning Kamp static vop_cachedlookup_t udf_lookup; 726fde64c7SPoul-Henning Kamp static vop_reclaim_t udf_reclaim; 7310bcafe9SPawel Jakub Dawidek static vop_vptofh_t udf_vptofh; 749d32fde8SScott Long static int udf_readatoffset(struct udf_node *node, int *size, off_t offset, 759d32fde8SScott Long struct buf **bp, uint8_t **data); 769d32fde8SScott Long static int udf_bmap_internal(struct udf_node *node, off_t offset, 779d32fde8SScott Long daddr_t *sector, uint32_t *max_size); 7851a7b740SScott Long 79aec0fb7bSPoul-Henning Kamp static struct vop_vector udf_vnodeops = { 80aec0fb7bSPoul-Henning Kamp .vop_default = &default_vnodeops, 8183c64397SPoul-Henning Kamp 82aec0fb7bSPoul-Henning Kamp .vop_access = udf_access, 83aec0fb7bSPoul-Henning Kamp .vop_bmap = udf_bmap, 84aec0fb7bSPoul-Henning Kamp .vop_cachedlookup = udf_lookup, 85aec0fb7bSPoul-Henning Kamp .vop_getattr = udf_getattr, 86aec0fb7bSPoul-Henning Kamp .vop_ioctl = udf_ioctl, 87aec0fb7bSPoul-Henning Kamp .vop_lookup = vfs_cache_lookup, 8835e06624SPav Lucistnik .vop_open = udf_open, 89aec0fb7bSPoul-Henning Kamp .vop_pathconf = udf_pathconf, 9061e69c80SJohn Baldwin .vop_print = udf_print, 91aec0fb7bSPoul-Henning Kamp .vop_read = udf_read, 92aec0fb7bSPoul-Henning Kamp .vop_readdir = udf_readdir, 93aec0fb7bSPoul-Henning Kamp .vop_readlink = udf_readlink, 94aec0fb7bSPoul-Henning Kamp .vop_reclaim = udf_reclaim, 9561e69c80SJohn Baldwin .vop_setattr = udf_setattr, 96aec0fb7bSPoul-Henning Kamp .vop_strategy = udf_strategy, 9710bcafe9SPawel Jakub Dawidek .vop_vptofh = udf_vptofh, 9851a7b740SScott Long }; 9951a7b740SScott Long 10061e69c80SJohn Baldwin struct vop_vector udf_fifoops = { 10161e69c80SJohn Baldwin .vop_default = &fifo_specops, 10261e69c80SJohn Baldwin .vop_access = udf_access, 10361e69c80SJohn Baldwin .vop_getattr = udf_getattr, 10461e69c80SJohn Baldwin .vop_print = udf_print, 10561e69c80SJohn Baldwin .vop_reclaim = udf_reclaim, 10661e69c80SJohn Baldwin .vop_setattr = udf_setattr, 10761e69c80SJohn Baldwin .vop_vptofh = udf_vptofh, 10861e69c80SJohn Baldwin }; 10961e69c80SJohn Baldwin 1105bb84bc8SRobert Watson MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure"); 1115bb84bc8SRobert Watson MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure"); 11251a7b740SScott Long 1134576293dSScott Long #define UDF_INVALID_BMAP -1 1148db4c2f2SScott Long 11551a7b740SScott Long int 11651a7b740SScott Long udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td) 11751a7b740SScott Long { 11851a7b740SScott Long int error; 11951a7b740SScott Long struct vnode *vp; 12051a7b740SScott Long 121aec0fb7bSPoul-Henning Kamp error = getnewvnode("udf", mp, &udf_vnodeops, &vp); 12251a7b740SScott Long if (error) { 12351a7b740SScott Long printf("udf_allocv: failed to allocate new vnode\n"); 12451a7b740SScott Long return (error); 12551a7b740SScott Long } 12651a7b740SScott Long 12751a7b740SScott Long *vpp = vp; 12851a7b740SScott Long return (0); 12951a7b740SScott Long } 13051a7b740SScott Long 13151a7b740SScott Long /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */ 13251a7b740SScott Long static mode_t 13351a7b740SScott Long udf_permtomode(struct udf_node *node) 13451a7b740SScott Long { 135c2d6947dSJeroen Ruigrok van der Werven uint32_t perm; 136bf1c3dddSScott Long uint16_t flags; 13751a7b740SScott Long mode_t mode; 13851a7b740SScott Long 139bf1c3dddSScott Long perm = le32toh(node->fentry->perm); 140bf1c3dddSScott Long flags = le16toh(node->fentry->icbtag.flags); 14151a7b740SScott Long 14251a7b740SScott Long mode = perm & UDF_FENTRY_PERM_USER_MASK; 14351a7b740SScott Long mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2); 14451a7b740SScott Long mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4); 14551a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4); 14651a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6); 14751a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8); 14851a7b740SScott Long 14951a7b740SScott Long return (mode); 15051a7b740SScott Long } 15151a7b740SScott Long 15251a7b740SScott Long static int 15351a7b740SScott Long udf_access(struct vop_access_args *a) 15451a7b740SScott Long { 15551a7b740SScott Long struct vnode *vp; 15651a7b740SScott Long struct udf_node *node; 15715bc6b2bSEdward Tomasz Napierala accmode_t accmode; 15815bc6b2bSEdward Tomasz Napierala mode_t mode; 15951a7b740SScott Long 16051a7b740SScott Long vp = a->a_vp; 16151a7b740SScott Long node = VTON(vp); 16215bc6b2bSEdward Tomasz Napierala accmode = a->a_accmode; 16351a7b740SScott Long 16415bc6b2bSEdward Tomasz Napierala if (accmode & VWRITE) { 16551a7b740SScott Long switch (vp->v_type) { 16651a7b740SScott Long case VDIR: 16751a7b740SScott Long case VLNK: 16851a7b740SScott Long case VREG: 16951a7b740SScott Long return (EROFS); 17051a7b740SScott Long /* NOT REACHED */ 17151a7b740SScott Long default: 17251a7b740SScott Long break; 17351a7b740SScott Long } 17451a7b740SScott Long } 17551a7b740SScott Long 17651a7b740SScott Long mode = udf_permtomode(node); 17751a7b740SScott Long 17851a7b740SScott Long return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid, 17915bc6b2bSEdward Tomasz Napierala accmode, a->a_cred, NULL)); 18051a7b740SScott Long } 18151a7b740SScott Long 18235e06624SPav Lucistnik static int 18335e06624SPav Lucistnik udf_open(struct vop_open_args *ap) { 18435e06624SPav Lucistnik struct udf_node *np = VTON(ap->a_vp); 18535e06624SPav Lucistnik off_t fsize; 18635e06624SPav Lucistnik 18735e06624SPav Lucistnik fsize = le64toh(np->fentry->inf_len); 18835e06624SPav Lucistnik vnode_create_vobject(ap->a_vp, fsize, ap->a_td); 18935e06624SPav Lucistnik return 0; 19035e06624SPav Lucistnik } 19135e06624SPav Lucistnik 1929c2bf69dSMarkus Brueffer static const int mon_lens[2][12] = { 1939c2bf69dSMarkus Brueffer {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, 1949c2bf69dSMarkus Brueffer {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} 19551a7b740SScott Long }; 19651a7b740SScott Long 19751a7b740SScott Long static int 19851a7b740SScott Long udf_isaleapyear(int year) 19951a7b740SScott Long { 20051a7b740SScott Long int i; 20151a7b740SScott Long 20251a7b740SScott Long i = (year % 4) ? 0 : 1; 20351a7b740SScott Long i &= (year % 100) ? 1 : 0; 20451a7b740SScott Long i |= (year % 400) ? 0 : 1; 20551a7b740SScott Long 20651a7b740SScott Long return i; 20751a7b740SScott Long } 20851a7b740SScott Long 20951a7b740SScott Long /* 21051a7b740SScott Long * Timezone calculation compliments of Julian Elischer <julian@elischer.org>. 21151a7b740SScott Long */ 21251a7b740SScott Long static void 21351a7b740SScott Long udf_timetotimespec(struct timestamp *time, struct timespec *t) 21451a7b740SScott Long { 2159c2bf69dSMarkus Brueffer int i, lpyear, daysinyear, year, startyear; 21651a7b740SScott Long union { 217c2d6947dSJeroen Ruigrok van der Werven uint16_t u_tz_offset; 21851a7b740SScott Long int16_t s_tz_offset; 21951a7b740SScott Long } tz; 22051a7b740SScott Long 2219c2bf69dSMarkus Brueffer /* 2229c2bf69dSMarkus Brueffer * DirectCD seems to like using bogus year values. 2239c2bf69dSMarkus Brueffer * Don't trust time->month as it will be used for an array index. 2249c2bf69dSMarkus Brueffer */ 225bf1c3dddSScott Long year = le16toh(time->year); 2269c2bf69dSMarkus Brueffer if (year < 1970 || time->month < 1 || time->month > 12) { 22751a7b740SScott Long t->tv_sec = 0; 2289c2bf69dSMarkus Brueffer t->tv_nsec = 0; 22951a7b740SScott Long return; 23051a7b740SScott Long } 23151a7b740SScott Long 23251a7b740SScott Long /* Calculate the time and day */ 23351a7b740SScott Long t->tv_sec = time->second; 23451a7b740SScott Long t->tv_sec += time->minute * 60; 23551a7b740SScott Long t->tv_sec += time->hour * 3600; 2369c2bf69dSMarkus Brueffer t->tv_sec += (time->day - 1) * 3600 * 24; 23751a7b740SScott Long 238befb7f33SChristian Brueffer /* Calculate the month */ 239bf1c3dddSScott Long lpyear = udf_isaleapyear(year); 2409c2bf69dSMarkus Brueffer t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24; 24151a7b740SScott Long 24251a7b740SScott Long /* Speed up the calculation */ 2439c2bf69dSMarkus Brueffer startyear = 1970; 2449c2bf69dSMarkus Brueffer if (year > 2009) { 2459c2bf69dSMarkus Brueffer t->tv_sec += 1262304000; 2469c2bf69dSMarkus Brueffer startyear += 40; 2479c2bf69dSMarkus Brueffer } else if (year > 1999) { 2489c2bf69dSMarkus Brueffer t->tv_sec += 946684800; 2499c2bf69dSMarkus Brueffer startyear += 30; 2509c2bf69dSMarkus Brueffer } else if (year > 1989) { 2519c2bf69dSMarkus Brueffer t->tv_sec += 631152000; 2529c2bf69dSMarkus Brueffer startyear += 20; 2539c2bf69dSMarkus Brueffer } else if (year > 1979) { 25451a7b740SScott Long t->tv_sec += 315532800; 2559c2bf69dSMarkus Brueffer startyear += 10; 25651a7b740SScott Long } 25751a7b740SScott Long 2589c2bf69dSMarkus Brueffer daysinyear = (year - startyear) * 365; 2599c2bf69dSMarkus Brueffer for (i = startyear; i < year; i++) 2609c2bf69dSMarkus Brueffer daysinyear += udf_isaleapyear(i); 2619c2bf69dSMarkus Brueffer t->tv_sec += daysinyear * 3600 * 24; 2629c2bf69dSMarkus Brueffer 2639c2bf69dSMarkus Brueffer /* Calculate microseconds */ 2649c2bf69dSMarkus Brueffer t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 + 2659c2bf69dSMarkus Brueffer time->usec; 2669c2bf69dSMarkus Brueffer 26751a7b740SScott Long /* 26851a7b740SScott Long * Calculate the time zone. The timezone is 12 bit signed 2's 269befb7f33SChristian Brueffer * complement, so we gotta do some extra magic to handle it right. 27051a7b740SScott Long */ 271bf1c3dddSScott Long tz.u_tz_offset = le16toh(time->type_tz); 27251a7b740SScott Long tz.u_tz_offset &= 0x0fff; 27351a7b740SScott Long if (tz.u_tz_offset & 0x0800) 27451a7b740SScott Long tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */ 2759c2bf69dSMarkus Brueffer if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047)) 27651a7b740SScott Long t->tv_sec -= tz.s_tz_offset * 60; 27751a7b740SScott Long 27851a7b740SScott Long return; 27951a7b740SScott Long } 28051a7b740SScott Long 28151a7b740SScott Long static int 28251a7b740SScott Long udf_getattr(struct vop_getattr_args *a) 28351a7b740SScott Long { 28451a7b740SScott Long struct vnode *vp; 28551a7b740SScott Long struct udf_node *node; 28651a7b740SScott Long struct vattr *vap; 28751a7b740SScott Long struct file_entry *fentry; 28851a7b740SScott Long struct timespec ts; 28951a7b740SScott Long 29051a7b740SScott Long ts.tv_sec = 0; 29151a7b740SScott Long 29251a7b740SScott Long vp = a->a_vp; 29351a7b740SScott Long vap = a->a_vap; 29451a7b740SScott Long node = VTON(vp); 29551a7b740SScott Long fentry = node->fentry; 29651a7b740SScott Long 297c049546eSPoul-Henning Kamp vap->va_fsid = dev2udev(node->udfmp->im_dev); 29851a7b740SScott Long vap->va_fileid = node->hash_id; 29951a7b740SScott Long vap->va_mode = udf_permtomode(node); 300bf1c3dddSScott Long vap->va_nlink = le16toh(fentry->link_cnt); 30151a7b740SScott Long /* 30251a7b740SScott Long * XXX The spec says that -1 is valid for uid/gid and indicates an 30351a7b740SScott Long * invalid uid/gid. How should this be represented? 30451a7b740SScott Long */ 305bf1c3dddSScott Long vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid); 306bf1c3dddSScott Long vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid); 30751a7b740SScott Long udf_timetotimespec(&fentry->atime, &vap->va_atime); 30851a7b740SScott Long udf_timetotimespec(&fentry->mtime, &vap->va_mtime); 30951a7b740SScott Long vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */ 3104c5a20e3SKonstantin Belousov vap->va_rdev = NODEV; 31151a7b740SScott Long if (vp->v_type & VDIR) { 31251a7b740SScott Long /* 31351a7b740SScott Long * Directories that are recorded within their ICB will show 31451a7b740SScott Long * as having 0 blocks recorded. Since tradition dictates 31551a7b740SScott Long * that directories consume at least one logical block, 31651a7b740SScott Long * make it appear so. 31751a7b740SScott Long */ 31851a7b740SScott Long if (fentry->logblks_rec != 0) { 319bf1c3dddSScott Long vap->va_size = 320bf1c3dddSScott Long le64toh(fentry->logblks_rec) * node->udfmp->bsize; 32151a7b740SScott Long } else { 32251a7b740SScott Long vap->va_size = node->udfmp->bsize; 32351a7b740SScott Long } 32451a7b740SScott Long } else { 325bf1c3dddSScott Long vap->va_size = le64toh(fentry->inf_len); 32651a7b740SScott Long } 32751a7b740SScott Long vap->va_flags = 0; 32851a7b740SScott Long vap->va_gen = 1; 32951a7b740SScott Long vap->va_blocksize = node->udfmp->bsize; 330bf1c3dddSScott Long vap->va_bytes = le64toh(fentry->inf_len); 33151a7b740SScott Long vap->va_type = vp->v_type; 33251a7b740SScott Long vap->va_filerev = 0; /* XXX */ 33351a7b740SScott Long return (0); 33451a7b740SScott Long } 33551a7b740SScott Long 33661e69c80SJohn Baldwin static int 33761e69c80SJohn Baldwin udf_setattr(struct vop_setattr_args *a) 33861e69c80SJohn Baldwin { 33961e69c80SJohn Baldwin struct vnode *vp; 34061e69c80SJohn Baldwin struct vattr *vap; 34161e69c80SJohn Baldwin 34261e69c80SJohn Baldwin vp = a->a_vp; 34361e69c80SJohn Baldwin vap = a->a_vap; 34461e69c80SJohn Baldwin if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL || 34561e69c80SJohn Baldwin vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 34661e69c80SJohn Baldwin vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) 34761e69c80SJohn Baldwin return (EROFS); 34861e69c80SJohn Baldwin if (vap->va_size != (u_quad_t)VNOVAL) { 34961e69c80SJohn Baldwin switch (vp->v_type) { 35061e69c80SJohn Baldwin case VDIR: 35161e69c80SJohn Baldwin return (EISDIR); 35261e69c80SJohn Baldwin case VLNK: 35361e69c80SJohn Baldwin case VREG: 35461e69c80SJohn Baldwin return (EROFS); 35561e69c80SJohn Baldwin case VCHR: 35661e69c80SJohn Baldwin case VBLK: 35761e69c80SJohn Baldwin case VSOCK: 35861e69c80SJohn Baldwin case VFIFO: 35961e69c80SJohn Baldwin case VNON: 36061e69c80SJohn Baldwin case VBAD: 36161e69c80SJohn Baldwin case VMARKER: 36261e69c80SJohn Baldwin return (0); 36361e69c80SJohn Baldwin } 36461e69c80SJohn Baldwin } 36561e69c80SJohn Baldwin return (0); 36661e69c80SJohn Baldwin } 36761e69c80SJohn Baldwin 36851a7b740SScott Long /* 369bf1c3dddSScott Long * File specific ioctls. 37051a7b740SScott Long */ 37151a7b740SScott Long static int 37251a7b740SScott Long udf_ioctl(struct vop_ioctl_args *a) 37351a7b740SScott Long { 374c80a90c5SScott Long printf("%s called\n", __func__); 3751d02d910SPoul-Henning Kamp return (ENOTTY); 37651a7b740SScott Long } 37751a7b740SScott Long 37851a7b740SScott Long /* 37951a7b740SScott Long * I'm not sure that this has much value in a read-only filesystem, but 38051a7b740SScott Long * cd9660 has it too. 38151a7b740SScott Long */ 38251a7b740SScott Long static int 38351a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a) 38451a7b740SScott Long { 38551a7b740SScott Long 38651a7b740SScott Long switch (a->a_name) { 38751a7b740SScott Long case _PC_LINK_MAX: 38851a7b740SScott Long *a->a_retval = 65535; 38951a7b740SScott Long return (0); 39051a7b740SScott Long case _PC_NAME_MAX: 39151a7b740SScott Long *a->a_retval = NAME_MAX; 39251a7b740SScott Long return (0); 39351a7b740SScott Long case _PC_PATH_MAX: 39451a7b740SScott Long *a->a_retval = PATH_MAX; 39551a7b740SScott Long return (0); 39651a7b740SScott Long case _PC_NO_TRUNC: 39751a7b740SScott Long *a->a_retval = 1; 39851a7b740SScott Long return (0); 39951a7b740SScott Long default: 40051a7b740SScott Long return (EINVAL); 40151a7b740SScott Long } 40251a7b740SScott Long } 40351a7b740SScott Long 40461e69c80SJohn Baldwin static int 40561e69c80SJohn Baldwin udf_print(struct vop_print_args *ap) 40661e69c80SJohn Baldwin { 40761e69c80SJohn Baldwin struct vnode *vp = ap->a_vp; 40861e69c80SJohn Baldwin struct udf_node *node = VTON(vp); 40961e69c80SJohn Baldwin 41061e69c80SJohn Baldwin printf(" ino %lu, on dev %s", (u_long)node->hash_id, 41161e69c80SJohn Baldwin devtoname(node->udfmp->im_dev)); 41261e69c80SJohn Baldwin if (vp->v_type == VFIFO) 41361e69c80SJohn Baldwin fifo_printinfo(vp); 41461e69c80SJohn Baldwin printf("\n"); 41561e69c80SJohn Baldwin return (0); 41661e69c80SJohn Baldwin } 41761e69c80SJohn Baldwin 4180c09ac0dSPav Lucistnik #define lblkno(udfmp, loc) ((loc) >> (udfmp)->bshift) 4190c09ac0dSPav Lucistnik #define blkoff(udfmp, loc) ((loc) & (udfmp)->bmask) 4205792e04dSAndriy Gapon #define lblktosize(udfmp, blk) ((blk) << (udfmp)->bshift) 42151a7b740SScott Long 422be52a95dSAndriy Gapon static inline int 423be52a95dSAndriy Gapon is_data_in_fentry(const struct udf_node *node) 424be52a95dSAndriy Gapon { 425be52a95dSAndriy Gapon const struct file_entry *fentry = node->fentry; 426be52a95dSAndriy Gapon 427be52a95dSAndriy Gapon return ((le16toh(fentry->icbtag.flags) & 0x7) == 3); 428be52a95dSAndriy Gapon } 429be52a95dSAndriy Gapon 4300c09ac0dSPav Lucistnik static int 4310c09ac0dSPav Lucistnik udf_read(struct vop_read_args *ap) 4320c09ac0dSPav Lucistnik { 4330c09ac0dSPav Lucistnik struct vnode *vp = ap->a_vp; 4340c09ac0dSPav Lucistnik struct uio *uio = ap->a_uio; 4350c09ac0dSPav Lucistnik struct udf_node *node = VTON(vp); 4360c09ac0dSPav Lucistnik struct udf_mnt *udfmp; 437be52a95dSAndriy Gapon struct file_entry *fentry; 4380c09ac0dSPav Lucistnik struct buf *bp; 439be52a95dSAndriy Gapon uint8_t *data; 4400c09ac0dSPav Lucistnik daddr_t lbn, rablock; 4410c09ac0dSPav Lucistnik off_t diff, fsize; 4420c09ac0dSPav Lucistnik int error = 0; 4430c09ac0dSPav Lucistnik long size, n, on; 4440c09ac0dSPav Lucistnik 4450c09ac0dSPav Lucistnik if (uio->uio_resid == 0) 4460c09ac0dSPav Lucistnik return (0); 44751a7b740SScott Long if (uio->uio_offset < 0) 44851a7b740SScott Long return (EINVAL); 449be52a95dSAndriy Gapon 450be52a95dSAndriy Gapon if (is_data_in_fentry(node)) { 451be52a95dSAndriy Gapon fentry = node->fentry; 452be52a95dSAndriy Gapon data = &fentry->data[le32toh(fentry->l_ea)]; 453be52a95dSAndriy Gapon fsize = le32toh(fentry->l_ad); 454be52a95dSAndriy Gapon 455be52a95dSAndriy Gapon n = uio->uio_resid; 456be52a95dSAndriy Gapon diff = fsize - uio->uio_offset; 457be52a95dSAndriy Gapon if (diff <= 0) 458be52a95dSAndriy Gapon return (0); 459be52a95dSAndriy Gapon if (diff < n) 460be52a95dSAndriy Gapon n = diff; 461be52a95dSAndriy Gapon error = uiomove(data + uio->uio_offset, (int)n, uio); 462be52a95dSAndriy Gapon return (error); 463be52a95dSAndriy Gapon } 464be52a95dSAndriy Gapon 465bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len); 4660c09ac0dSPav Lucistnik udfmp = node->udfmp; 4670c09ac0dSPav Lucistnik do { 4680c09ac0dSPav Lucistnik lbn = lblkno(udfmp, uio->uio_offset); 4690c09ac0dSPav Lucistnik on = blkoff(udfmp, uio->uio_offset); 4700c09ac0dSPav Lucistnik n = min((u_int)(udfmp->bsize - on), 4710c09ac0dSPav Lucistnik uio->uio_resid); 4720c09ac0dSPav Lucistnik diff = fsize - uio->uio_offset; 4730c09ac0dSPav Lucistnik if (diff <= 0) 4740c09ac0dSPav Lucistnik return (0); 4750c09ac0dSPav Lucistnik if (diff < n) 4760c09ac0dSPav Lucistnik n = diff; 4770c09ac0dSPav Lucistnik size = udfmp->bsize; 4780c09ac0dSPav Lucistnik rablock = lbn + 1; 4790c09ac0dSPav Lucistnik if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 4800c09ac0dSPav Lucistnik if (lblktosize(udfmp, rablock) < fsize) { 4810c09ac0dSPav Lucistnik error = cluster_read(vp, fsize, lbn, size, NOCRED, 4820c09ac0dSPav Lucistnik uio->uio_resid, (ap->a_ioflag >> 16), &bp); 4830c09ac0dSPav Lucistnik } else { 4840c09ac0dSPav Lucistnik error = bread(vp, lbn, size, NOCRED, &bp); 4850c09ac0dSPav Lucistnik } 4860c09ac0dSPav Lucistnik } else { 4870c09ac0dSPav Lucistnik error = bread(vp, lbn, size, NOCRED, &bp); 4880c09ac0dSPav Lucistnik } 4890c09ac0dSPav Lucistnik n = min(n, size - bp->b_resid); 4900c09ac0dSPav Lucistnik if (error) { 49151a7b740SScott Long brelse(bp); 4920c09ac0dSPav Lucistnik return (error); 4930c09ac0dSPav Lucistnik } 49451a7b740SScott Long 4950c09ac0dSPav Lucistnik error = uiomove(bp->b_data + on, (int)n, uio); 4960c09ac0dSPav Lucistnik brelse(bp); 4970c09ac0dSPav Lucistnik } while (error == 0 && uio->uio_resid > 0 && n != 0); 49851a7b740SScott Long return (error); 49951a7b740SScott Long } 50051a7b740SScott Long 50151a7b740SScott Long /* 50251a7b740SScott Long * Call the OSTA routines to translate the name from a CS0 dstring to a 50351a7b740SScott Long * 16-bit Unicode String. Hooks need to be placed in here to translate from 5046565282cSScott Long * Unicode to the encoding that the kernel/user expects. Return the length 5056565282cSScott Long * of the translated string. 50651a7b740SScott Long */ 50751a7b740SScott Long static int 5086565282cSScott Long udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp) 50951a7b740SScott Long { 51051a7b740SScott Long unicode_t *transname; 5116565282cSScott Long char *unibuf, *unip; 512181fc3c6SR. Imura int i, destlen; 513181fc3c6SR. Imura ssize_t unilen = 0; 5146565282cSScott Long size_t destleft = MAXNAMLEN; 51551a7b740SScott Long 5166565282cSScott Long /* Convert 16-bit Unicode to destname */ 5176565282cSScott Long if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) { 5186565282cSScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */ 5196565282cSScott Long unibuf = uma_zalloc(udf_zone_trans, M_WAITOK); 5206565282cSScott Long unip = unibuf; 521181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) { 5226565282cSScott Long printf("udf: Unicode translation failed\n"); 5236565282cSScott Long uma_zfree(udf_zone_trans, unibuf); 5246565282cSScott Long return 0; 5256565282cSScott Long } 5266565282cSScott Long 5276565282cSScott Long while (unilen > 0 && destleft > 0) { 5286565282cSScott Long udf_iconv->conv(udfmp->im_d2l, (const char **)&unibuf, 5296565282cSScott Long (size_t *)&unilen, (char **)&destname, &destleft); 5306565282cSScott Long /* Unconverted character found */ 5316565282cSScott Long if (unilen > 0 && destleft > 0) { 5326565282cSScott Long *destname++ = '?'; 5336565282cSScott Long destleft--; 5346565282cSScott Long unibuf += 2; 5356565282cSScott Long unilen -= 2; 5366565282cSScott Long } 5376565282cSScott Long } 5386565282cSScott Long uma_zfree(udf_zone_trans, unip); 5396565282cSScott Long *destname = '\0'; 5406565282cSScott Long destlen = MAXNAMLEN - (int)destleft; 5416565282cSScott Long } else { 54251a7b740SScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */ 543a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK); 54451a7b740SScott Long 545181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) { 54651a7b740SScott Long printf("udf: Unicode translation failed\n"); 54751a7b740SScott Long uma_zfree(udf_zone_trans, transname); 54851a7b740SScott Long return 0; 54951a7b740SScott Long } 55051a7b740SScott Long 55151a7b740SScott Long for (i = 0; i < unilen ; i++) { 55251a7b740SScott Long if (transname[i] & 0xff00) { 55351a7b740SScott Long destname[i] = '.'; /* Fudge the 16bit chars */ 55451a7b740SScott Long } else { 55551a7b740SScott Long destname[i] = transname[i] & 0xff; 55651a7b740SScott Long } 55751a7b740SScott Long } 55851a7b740SScott Long uma_zfree(udf_zone_trans, transname); 5596565282cSScott Long destname[unilen] = 0; 560181fc3c6SR. Imura destlen = (int)unilen; 5616565282cSScott Long } 56251a7b740SScott Long 5636565282cSScott Long return (destlen); 56451a7b740SScott Long } 56551a7b740SScott Long 56651a7b740SScott Long /* 56751a7b740SScott Long * Compare a CS0 dstring with a name passed in from the VFS layer. Return 568befb7f33SChristian Brueffer * 0 on a successful match, nonzero otherwise. Unicode work may need to be done 56951a7b740SScott Long * here also. 57051a7b740SScott Long */ 57151a7b740SScott Long static int 5726565282cSScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp) 57351a7b740SScott Long { 57495ec5961SScott Long char *transname; 57595ec5961SScott Long int error = 0; 57651a7b740SScott Long 57795ec5961SScott Long /* This is overkill, but not worth creating a new zone */ 578a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK); 57995ec5961SScott Long 5806565282cSScott Long cs0len = udf_transname(cs0string, transname, cs0len, udfmp); 58151a7b740SScott Long 58251a7b740SScott Long /* Easy check. If they aren't the same length, they aren't equal */ 58395ec5961SScott Long if ((cs0len == 0) || (cs0len != cmplen)) 58495ec5961SScott Long error = -1; 58595ec5961SScott Long else 58695ec5961SScott Long error = bcmp(transname, cmpname, cmplen); 58751a7b740SScott Long 58895ec5961SScott Long uma_zfree(udf_zone_trans, transname); 58995ec5961SScott Long return (error); 59051a7b740SScott Long } 59151a7b740SScott Long 59251a7b740SScott Long struct udf_uiodir { 59351a7b740SScott Long struct dirent *dirent; 59451a7b740SScott Long u_long *cookies; 59551a7b740SScott Long int ncookies; 59651a7b740SScott Long int acookies; 59751a7b740SScott Long int eofflag; 59851a7b740SScott Long }; 59951a7b740SScott Long 60051a7b740SScott Long static int 60151a7b740SScott Long udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie) 60251a7b740SScott Long { 60351a7b740SScott Long if (uiodir->cookies != NULL) { 60451a7b740SScott Long if (++uiodir->acookies > uiodir->ncookies) { 60551a7b740SScott Long uiodir->eofflag = 0; 60651a7b740SScott Long return (-1); 60751a7b740SScott Long } 60851a7b740SScott Long *uiodir->cookies++ = cookie; 60951a7b740SScott Long } 61051a7b740SScott Long 61151a7b740SScott Long if (uio->uio_resid < de_size) { 61251a7b740SScott Long uiodir->eofflag = 0; 61351a7b740SScott Long return (-1); 61451a7b740SScott Long } 61551a7b740SScott Long 616c9524588SDag-Erling Smørgrav return (uiomove(uiodir->dirent, de_size, uio)); 61751a7b740SScott Long } 61851a7b740SScott Long 6191703656aSScott Long static struct udf_dirstream * 6201703656aSScott Long udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp) 6211703656aSScott Long { 6221703656aSScott Long struct udf_dirstream *ds; 6231703656aSScott Long 624a163d034SWarner Losh ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO); 6251703656aSScott Long 6261703656aSScott Long ds->node = node; 6271703656aSScott Long ds->offset = offset; 6281703656aSScott Long ds->udfmp = udfmp; 6291703656aSScott Long ds->fsize = fsize; 6301703656aSScott Long 6311703656aSScott Long return (ds); 6321703656aSScott Long } 6331703656aSScott Long 6341703656aSScott Long static struct fileid_desc * 6351703656aSScott Long udf_getfid(struct udf_dirstream *ds) 6361703656aSScott Long { 6371703656aSScott Long struct fileid_desc *fid; 6381703656aSScott Long int error, frag_size = 0, total_fid_size; 6391703656aSScott Long 6401703656aSScott Long /* End of directory? */ 6411703656aSScott Long if (ds->offset + ds->off >= ds->fsize) { 6421703656aSScott Long ds->error = 0; 6431703656aSScott Long return (NULL); 6441703656aSScott Long } 6451703656aSScott Long 6461703656aSScott Long /* Grab the first extent of the directory */ 6471703656aSScott Long if (ds->off == 0) { 6481703656aSScott Long ds->size = 0; 6491703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset, 6501703656aSScott Long &ds->bp, &ds->data); 6511703656aSScott Long if (error) { 6521703656aSScott Long ds->error = error; 653744bb56dSScott Long if (ds->bp != NULL) 654744bb56dSScott Long brelse(ds->bp); 6551703656aSScott Long return (NULL); 6561703656aSScott Long } 6571703656aSScott Long } 6581703656aSScott Long 6594576293dSScott Long /* 6604576293dSScott Long * Clean up from a previous fragmented FID. 6614576293dSScott Long * XXX Is this the right place for this? 6624576293dSScott Long */ 6631703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) { 6641703656aSScott Long ds->fid_fragment = 0; 6651ede983cSDag-Erling Smørgrav free(ds->buf, M_UDFFID); 6661703656aSScott Long } 6671703656aSScott Long 6681703656aSScott Long fid = (struct fileid_desc*)&ds->data[ds->off]; 6691703656aSScott Long 6701703656aSScott Long /* 6711703656aSScott Long * Check to see if the fid is fragmented. The first test 6721703656aSScott Long * ensures that we don't wander off the end of the buffer 6731703656aSScott Long * looking for the l_iu and l_fi fields. 6741703656aSScott Long */ 6751703656aSScott Long if (ds->off + UDF_FID_SIZE > ds->size || 676bf1c3dddSScott Long ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){ 6771703656aSScott Long 6781703656aSScott Long /* Copy what we have of the fid into a buffer */ 6791703656aSScott Long frag_size = ds->size - ds->off; 6801703656aSScott Long if (frag_size >= ds->udfmp->bsize) { 6811703656aSScott Long printf("udf: invalid FID fragment\n"); 6821703656aSScott Long ds->error = EINVAL; 6831703656aSScott Long return (NULL); 6841703656aSScott Long } 6851703656aSScott Long 6861703656aSScott Long /* 6871703656aSScott Long * File ID descriptors can only be at most one 6881703656aSScott Long * logical sector in size. 6891703656aSScott Long */ 6901ede983cSDag-Erling Smørgrav ds->buf = malloc(ds->udfmp->bsize, M_UDFFID, 691a163d034SWarner Losh M_WAITOK | M_ZERO); 6921703656aSScott Long bcopy(fid, ds->buf, frag_size); 6931703656aSScott Long 6941703656aSScott Long /* Reduce all of the casting magic */ 6951703656aSScott Long fid = (struct fileid_desc*)ds->buf; 6961703656aSScott Long 6971703656aSScott Long if (ds->bp != NULL) 6981703656aSScott Long brelse(ds->bp); 6991703656aSScott Long 7001703656aSScott Long /* Fetch the next allocation */ 7011703656aSScott Long ds->offset += ds->size; 7021703656aSScott Long ds->size = 0; 7031703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset, 7041703656aSScott Long &ds->bp, &ds->data); 7051703656aSScott Long if (error) { 7061703656aSScott Long ds->error = error; 7071703656aSScott Long return (NULL); 7081703656aSScott Long } 7091703656aSScott Long 7101703656aSScott Long /* 7111703656aSScott Long * If the fragment was so small that we didn't get 7121703656aSScott Long * the l_iu and l_fi fields, copy those in. 7131703656aSScott Long */ 7141703656aSScott Long if (frag_size < UDF_FID_SIZE) 7151703656aSScott Long bcopy(ds->data, &ds->buf[frag_size], 7161703656aSScott Long UDF_FID_SIZE - frag_size); 7171703656aSScott Long 7181703656aSScott Long /* 7191703656aSScott Long * Now that we have enough of the fid to work with, 7201703656aSScott Long * copy in the rest of the fid from the new 7211703656aSScott Long * allocation. 7221703656aSScott Long */ 723bf1c3dddSScott Long total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi; 7241703656aSScott Long if (total_fid_size > ds->udfmp->bsize) { 7251703656aSScott Long printf("udf: invalid FID\n"); 7261703656aSScott Long ds->error = EIO; 7271703656aSScott Long return (NULL); 7281703656aSScott Long } 7291703656aSScott Long bcopy(ds->data, &ds->buf[frag_size], 7301703656aSScott Long total_fid_size - frag_size); 7311703656aSScott Long 7321703656aSScott Long ds->fid_fragment = 1; 7331703656aSScott Long } else { 734bf1c3dddSScott Long total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE; 7351703656aSScott Long } 7361703656aSScott Long 7371703656aSScott Long /* 7381703656aSScott Long * Update the offset. Align on a 4 byte boundary because the 7394576293dSScott Long * UDF spec says so. 7401703656aSScott Long */ 7411703656aSScott Long ds->this_off = ds->off; 7421703656aSScott Long if (!ds->fid_fragment) { 7431703656aSScott Long ds->off += (total_fid_size + 3) & ~0x03; 7441703656aSScott Long } else { 7451703656aSScott Long ds->off = (total_fid_size - frag_size + 3) & ~0x03; 7461703656aSScott Long } 7471703656aSScott Long 7481703656aSScott Long return (fid); 7491703656aSScott Long } 7501703656aSScott Long 7511703656aSScott Long static void 7521703656aSScott Long udf_closedir(struct udf_dirstream *ds) 7531703656aSScott Long { 7541703656aSScott Long 7551703656aSScott Long if (ds->bp != NULL) 7561703656aSScott Long brelse(ds->bp); 7571703656aSScott Long 7581703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) 7591ede983cSDag-Erling Smørgrav free(ds->buf, M_UDFFID); 7601703656aSScott Long 7611703656aSScott Long uma_zfree(udf_zone_ds, ds); 7621703656aSScott Long } 7631703656aSScott Long 76451a7b740SScott Long static int 76551a7b740SScott Long udf_readdir(struct vop_readdir_args *a) 76651a7b740SScott Long { 76751a7b740SScott Long struct vnode *vp; 76851a7b740SScott Long struct uio *uio; 76951a7b740SScott Long struct dirent dir; 77051a7b740SScott Long struct udf_node *node; 7716565282cSScott Long struct udf_mnt *udfmp; 77251a7b740SScott Long struct fileid_desc *fid; 77351a7b740SScott Long struct udf_uiodir uiodir; 7741703656aSScott Long struct udf_dirstream *ds; 77551a7b740SScott Long u_long *cookies = NULL; 77651a7b740SScott Long int ncookies; 777c8eeea2fSScott Long int error = 0; 77851a7b740SScott Long 77951a7b740SScott Long vp = a->a_vp; 78051a7b740SScott Long uio = a->a_uio; 78151a7b740SScott Long node = VTON(vp); 7826565282cSScott Long udfmp = node->udfmp; 78351a7b740SScott Long uiodir.eofflag = 1; 78451a7b740SScott Long 78551a7b740SScott Long if (a->a_ncookies != NULL) { 78651a7b740SScott Long /* 78751a7b740SScott Long * Guess how many entries are needed. If we run out, this 78851a7b740SScott Long * function will be called again and thing will pick up were 78951a7b740SScott Long * it left off. 79051a7b740SScott Long */ 79151a7b740SScott Long ncookies = uio->uio_resid / 8; 7921ede983cSDag-Erling Smørgrav cookies = malloc(sizeof(u_long) * ncookies, 793a163d034SWarner Losh M_TEMP, M_WAITOK); 79451a7b740SScott Long if (cookies == NULL) 79551a7b740SScott Long return (ENOMEM); 79651a7b740SScott Long uiodir.ncookies = ncookies; 79751a7b740SScott Long uiodir.cookies = cookies; 79851a7b740SScott Long uiodir.acookies = 0; 79951a7b740SScott Long } else { 80051a7b740SScott Long uiodir.cookies = NULL; 80151a7b740SScott Long } 80251a7b740SScott Long 80351a7b740SScott Long /* 80451a7b740SScott Long * Iterate through the file id descriptors. Give the parent dir 8054576293dSScott Long * entry special attention. 80651a7b740SScott Long */ 807bf1c3dddSScott Long ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len), 8081703656aSScott Long node->udfmp); 80951a7b740SScott Long 8101703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) { 81151a7b740SScott Long 81251a7b740SScott Long /* XXX Should we return an error on a bad fid? */ 81351a7b740SScott Long if (udf_checktag(&fid->tag, TAGID_FID)) { 81451a7b740SScott Long printf("Invalid FID tag\n"); 81577411499SScott Long hexdump(fid, UDF_FID_SIZE, NULL, 0); 8161703656aSScott Long error = EIO; 81751a7b740SScott Long break; 81851a7b740SScott Long } 81951a7b740SScott Long 82051a7b740SScott Long /* Is this a deleted file? */ 8212bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL) 8221703656aSScott Long continue; 82351a7b740SScott Long 8242bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 82551a7b740SScott Long /* Do up the '.' and '..' entries. Dummy values are 82651a7b740SScott Long * used for the cookies since the offset here is 82751a7b740SScott Long * usually zero, and NFS doesn't like that value 82851a7b740SScott Long */ 829c8eeea2fSScott Long dir.d_fileno = node->hash_id; 830c8eeea2fSScott Long dir.d_type = DT_DIR; 831c8eeea2fSScott Long dir.d_name[0] = '.'; 832444acc16SScott Long dir.d_name[1] = '\0'; 833c8eeea2fSScott Long dir.d_namlen = 1; 834c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 835c8eeea2fSScott Long uiodir.dirent = &dir; 836c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1); 83751a7b740SScott Long if (error) 83851a7b740SScott Long break; 83951a7b740SScott Long 840c8eeea2fSScott Long dir.d_fileno = udf_getid(&fid->icb); 841c8eeea2fSScott Long dir.d_type = DT_DIR; 842c8eeea2fSScott Long dir.d_name[0] = '.'; 843c8eeea2fSScott Long dir.d_name[1] = '.'; 844444acc16SScott Long dir.d_name[2] = '\0'; 845c8eeea2fSScott Long dir.d_namlen = 2; 846c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 847c8eeea2fSScott Long uiodir.dirent = &dir; 848c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2); 84951a7b740SScott Long } else { 85051a7b740SScott Long dir.d_namlen = udf_transname(&fid->data[fid->l_iu], 8516565282cSScott Long &dir.d_name[0], fid->l_fi, udfmp); 85251a7b740SScott Long dir.d_fileno = udf_getid(&fid->icb); 8532bbe0d36SScott Long dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ? 8542bbe0d36SScott Long DT_DIR : DT_UNKNOWN; 85551a7b740SScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 85651a7b740SScott Long uiodir.dirent = &dir; 8571703656aSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 8581703656aSScott Long ds->this_off); 85951a7b740SScott Long } 86084206c74SAndriy Gapon if (error) 86151a7b740SScott Long break; 862ff9e355bSAndriy Gapon uio->uio_offset = ds->offset + ds->off; 86351a7b740SScott Long } 86451a7b740SScott Long 86551a7b740SScott Long /* tell the calling layer whether we need to be called again */ 86651a7b740SScott Long *a->a_eofflag = uiodir.eofflag; 86751a7b740SScott Long 86884206c74SAndriy Gapon if (error < 0) 86984206c74SAndriy Gapon error = 0; 8701703656aSScott Long if (!error) 8711703656aSScott Long error = ds->error; 8721703656aSScott Long 8731703656aSScott Long udf_closedir(ds); 87451a7b740SScott Long 87551a7b740SScott Long if (a->a_ncookies != NULL) { 87651a7b740SScott Long if (error) 8771ede983cSDag-Erling Smørgrav free(cookies, M_TEMP); 87851a7b740SScott Long else { 87951a7b740SScott Long *a->a_ncookies = uiodir.acookies; 88051a7b740SScott Long *a->a_cookies = cookies; 88151a7b740SScott Long } 88251a7b740SScott Long } 88351a7b740SScott Long 88451a7b740SScott Long return (error); 88551a7b740SScott Long } 88651a7b740SScott Long 88751a7b740SScott Long static int 88851a7b740SScott Long udf_readlink(struct vop_readlink_args *ap) 88951a7b740SScott Long { 890e3024df2SJohn Baldwin struct path_component *pc, *end; 891e3024df2SJohn Baldwin struct vnode *vp; 892e3024df2SJohn Baldwin struct uio uio; 893e3024df2SJohn Baldwin struct iovec iov[1]; 894e3024df2SJohn Baldwin struct udf_node *node; 895e3024df2SJohn Baldwin void *buf; 896e3024df2SJohn Baldwin char *cp; 897e3024df2SJohn Baldwin int error, len, root; 898e3024df2SJohn Baldwin 899e3024df2SJohn Baldwin /* 900e3024df2SJohn Baldwin * A symbolic link in UDF is a list of variable-length path 901e3024df2SJohn Baldwin * component structures. We build a pathname in the caller's 902e3024df2SJohn Baldwin * uio by traversing this list. 903e3024df2SJohn Baldwin */ 904e3024df2SJohn Baldwin vp = ap->a_vp; 905e3024df2SJohn Baldwin node = VTON(vp); 906e3024df2SJohn Baldwin len = le64toh(node->fentry->inf_len); 907e3024df2SJohn Baldwin buf = malloc(iov[0].iov_len, M_DEVBUF, M_WAITOK); 908e3024df2SJohn Baldwin iov[0].iov_base = buf; 909e3024df2SJohn Baldwin iov[0].iov_len = len; 910e3024df2SJohn Baldwin uio.uio_iov = iov; 911e3024df2SJohn Baldwin uio.uio_iovcnt = 1; 912e3024df2SJohn Baldwin uio.uio_offset = 0; 913e3024df2SJohn Baldwin uio.uio_resid = iov[0].iov_len; 914e3024df2SJohn Baldwin uio.uio_segflg = UIO_SYSSPACE; 915e3024df2SJohn Baldwin uio.uio_rw = UIO_READ; 916e3024df2SJohn Baldwin uio.uio_td = curthread; 917e3024df2SJohn Baldwin error = VOP_READ(vp, &uio, 0, ap->a_cred); 918e3024df2SJohn Baldwin if (error) 919e3024df2SJohn Baldwin goto error; 920e3024df2SJohn Baldwin 921e3024df2SJohn Baldwin pc = buf; 922e3024df2SJohn Baldwin end = (void *)((char *)buf + len); 923e3024df2SJohn Baldwin root = 0; 924e3024df2SJohn Baldwin while (pc < end) { 925e3024df2SJohn Baldwin switch (pc->type) { 926e3024df2SJohn Baldwin case UDF_PATH_ROOT: 927e3024df2SJohn Baldwin /* Only allow this at the beginning of a path. */ 928e3024df2SJohn Baldwin if ((void *)pc != buf) { 929e3024df2SJohn Baldwin error = EINVAL; 930e3024df2SJohn Baldwin goto error; 931e3024df2SJohn Baldwin } 932e3024df2SJohn Baldwin cp = "/"; 933e3024df2SJohn Baldwin len = 1; 934e3024df2SJohn Baldwin root = 1; 935e3024df2SJohn Baldwin break; 936e3024df2SJohn Baldwin case UDF_PATH_DOT: 937e3024df2SJohn Baldwin cp = "."; 938e3024df2SJohn Baldwin len = 1; 939e3024df2SJohn Baldwin break; 940e3024df2SJohn Baldwin case UDF_PATH_DOTDOT: 941e3024df2SJohn Baldwin cp = ".."; 942e3024df2SJohn Baldwin len = 2; 943e3024df2SJohn Baldwin break; 944e3024df2SJohn Baldwin case UDF_PATH_PATH: 945e3024df2SJohn Baldwin if (pc->length == 0) { 946e3024df2SJohn Baldwin error = EINVAL; 947e3024df2SJohn Baldwin goto error; 948e3024df2SJohn Baldwin } 949e3024df2SJohn Baldwin /* 950e3024df2SJohn Baldwin * XXX: We only support CS8 which appears to map 951e3024df2SJohn Baldwin * to ASCII directly. 952e3024df2SJohn Baldwin */ 953e3024df2SJohn Baldwin switch (pc->identifier[0]) { 954e3024df2SJohn Baldwin case 8: 955e3024df2SJohn Baldwin cp = pc->identifier + 1; 956e3024df2SJohn Baldwin len = pc->length - 1; 957e3024df2SJohn Baldwin break; 958e3024df2SJohn Baldwin default: 959e3024df2SJohn Baldwin error = EOPNOTSUPP; 960e3024df2SJohn Baldwin goto error; 961e3024df2SJohn Baldwin } 962e3024df2SJohn Baldwin break; 963e3024df2SJohn Baldwin default: 964e3024df2SJohn Baldwin error = EINVAL; 965e3024df2SJohn Baldwin goto error; 966e3024df2SJohn Baldwin } 967e3024df2SJohn Baldwin 968e3024df2SJohn Baldwin /* 969e3024df2SJohn Baldwin * If this is not the first component, insert a path 970e3024df2SJohn Baldwin * separator. 971e3024df2SJohn Baldwin */ 972e3024df2SJohn Baldwin if (pc != buf) { 973e3024df2SJohn Baldwin /* If we started with root we already have a "/". */ 974e3024df2SJohn Baldwin if (root) 975e3024df2SJohn Baldwin goto skipslash; 976e3024df2SJohn Baldwin root = 0; 977e3024df2SJohn Baldwin if (ap->a_uio->uio_resid < 1) { 978e3024df2SJohn Baldwin error = ENAMETOOLONG; 979e3024df2SJohn Baldwin goto error; 980e3024df2SJohn Baldwin } 981e3024df2SJohn Baldwin error = uiomove("/", 1, ap->a_uio); 982e3024df2SJohn Baldwin if (error) 983e3024df2SJohn Baldwin break; 984e3024df2SJohn Baldwin } 985e3024df2SJohn Baldwin skipslash: 986e3024df2SJohn Baldwin 987e3024df2SJohn Baldwin /* Append string at 'cp' of length 'len' to our path. */ 988e3024df2SJohn Baldwin if (len > ap->a_uio->uio_resid) { 989e3024df2SJohn Baldwin error = ENAMETOOLONG; 990e3024df2SJohn Baldwin goto error; 991e3024df2SJohn Baldwin } 992e3024df2SJohn Baldwin error = uiomove(cp, len, ap->a_uio); 993e3024df2SJohn Baldwin if (error) 994e3024df2SJohn Baldwin break; 995e3024df2SJohn Baldwin 996e3024df2SJohn Baldwin /* Advance to next component. */ 997e3024df2SJohn Baldwin pc = (void *)((char *)pc + 4 + pc->length); 998e3024df2SJohn Baldwin } 999e3024df2SJohn Baldwin error: 1000e3024df2SJohn Baldwin free(buf, M_DEVBUF); 1001e3024df2SJohn Baldwin return (error); 100251a7b740SScott Long } 100351a7b740SScott Long 100451a7b740SScott Long static int 100551a7b740SScott Long udf_strategy(struct vop_strategy_args *a) 100651a7b740SScott Long { 100751a7b740SScott Long struct buf *bp; 100851a7b740SScott Long struct vnode *vp; 100951a7b740SScott Long struct udf_node *node; 1010429c018aSPoul-Henning Kamp struct bufobj *bo; 10115792e04dSAndriy Gapon off_t offset; 10125792e04dSAndriy Gapon uint32_t maxsize; 10135792e04dSAndriy Gapon daddr_t sector; 10145792e04dSAndriy Gapon int error; 101551a7b740SScott Long 101651a7b740SScott Long bp = a->a_bp; 1017d83b7498SPoul-Henning Kamp vp = a->a_vp; 101851a7b740SScott Long node = VTON(vp); 101951a7b740SScott Long 10200c09ac0dSPav Lucistnik if (bp->b_blkno == bp->b_lblkno) { 102151a7b740SScott Long /* 102251a7b740SScott Long * Files that are embedded in the fentry don't translate well 102351a7b740SScott Long * to a block number. Reject. 102451a7b740SScott Long */ 10255792e04dSAndriy Gapon offset = lblktosize(node->udfmp, bp->b_lblkno); 10265792e04dSAndriy Gapon error = udf_bmap_internal(node, offset, §or, &maxsize); 10275792e04dSAndriy Gapon if (error) { 102851a7b740SScott Long clrbuf(bp); 102951a7b740SScott Long bp->b_blkno = -1; 103051a7b740SScott Long bufdone(bp); 103151a7b740SScott Long return (0); 103251a7b740SScott Long } 10335792e04dSAndriy Gapon /* bmap gives sector numbers, bio works with device blocks */ 10345792e04dSAndriy Gapon bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT); 10355792e04dSAndriy Gapon } 1036429c018aSPoul-Henning Kamp bo = node->udfmp->im_bo; 10372c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 10380391e5a1SPoul-Henning Kamp BO_STRATEGY(bo, bp); 103951a7b740SScott Long return (0); 104051a7b740SScott Long } 104151a7b740SScott Long 104251a7b740SScott Long static int 104351a7b740SScott Long udf_bmap(struct vop_bmap_args *a) 104451a7b740SScott Long { 104551a7b740SScott Long struct udf_node *node; 1046c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size; 104798b0c789SPoul-Henning Kamp daddr_t lsector; 1048fb2a76ccSAndriy Gapon int nblk; 104951a7b740SScott Long int error; 105051a7b740SScott Long 105151a7b740SScott Long node = VTON(a->a_vp); 105251a7b740SScott Long 10539c83534dSPoul-Henning Kamp if (a->a_bop != NULL) 1054e0251bbbSPoul-Henning Kamp *a->a_bop = &node->udfmp->im_devvp->v_bufobj; 105551a7b740SScott Long if (a->a_bnp == NULL) 105651a7b740SScott Long return (0); 105751a7b740SScott Long if (a->a_runb) 105851a7b740SScott Long *a->a_runb = 0; 105951a7b740SScott Long 106082467096SAndriy Gapon /* 106182467096SAndriy Gapon * UDF_INVALID_BMAP means data embedded into fentry, this is an internal 106282467096SAndriy Gapon * error that should not be propagated to calling code. 106382467096SAndriy Gapon * Most obvious mapping for this error is EOPNOTSUPP as we can not truly 106482467096SAndriy Gapon * translate block numbers in this case. 106582467096SAndriy Gapon * Incidentally, this return code will make vnode pager to use VOP_READ 106682467096SAndriy Gapon * to get data for mmap-ed pages and udf_read knows how to do the right 106782467096SAndriy Gapon * thing for this kind of files. 106882467096SAndriy Gapon */ 106982467096SAndriy Gapon error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift, 107082467096SAndriy Gapon &lsector, &max_size); 107182467096SAndriy Gapon if (error == UDF_INVALID_BMAP) 107282467096SAndriy Gapon return (EOPNOTSUPP); 10738db4c2f2SScott Long if (error) 107451a7b740SScott Long return (error); 107551a7b740SScott Long 1076cd1b1a1dSScott Long /* Translate logical to physical sector number */ 1077cd1b1a1dSScott Long *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT); 1078cd1b1a1dSScott Long 1079fb2a76ccSAndriy Gapon /* 1080fb2a76ccSAndriy Gapon * Determine maximum number of readahead blocks following the 1081fb2a76ccSAndriy Gapon * requested block. 1082fb2a76ccSAndriy Gapon */ 1083fb2a76ccSAndriy Gapon if (a->a_runp) { 1084fb2a76ccSAndriy Gapon nblk = (max_size >> node->udfmp->bshift) - 1; 1085fb2a76ccSAndriy Gapon if (nblk <= 0) 108651a7b740SScott Long *a->a_runp = 0; 1087fb2a76ccSAndriy Gapon else if (nblk >= (MAXBSIZE >> node->udfmp->bshift)) 1088fb2a76ccSAndriy Gapon *a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1; 1089fb2a76ccSAndriy Gapon else 1090fb2a76ccSAndriy Gapon *a->a_runp = nblk; 1091fb2a76ccSAndriy Gapon } 1092fb2a76ccSAndriy Gapon 1093fb2a76ccSAndriy Gapon if (a->a_runb) { 1094fb2a76ccSAndriy Gapon *a->a_runb = 0; 1095fb2a76ccSAndriy Gapon } 109651a7b740SScott Long 109751a7b740SScott Long return (0); 109851a7b740SScott Long } 109951a7b740SScott Long 110051a7b740SScott Long /* 110151a7b740SScott Long * The all powerful VOP_LOOKUP(). 110251a7b740SScott Long */ 110351a7b740SScott Long static int 110451a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a) 110551a7b740SScott Long { 110651a7b740SScott Long struct vnode *dvp; 110751a7b740SScott Long struct vnode *tdp = NULL; 110851a7b740SScott Long struct vnode **vpp = a->a_vpp; 110951a7b740SScott Long struct udf_node *node; 111051a7b740SScott Long struct udf_mnt *udfmp; 111151a7b740SScott Long struct fileid_desc *fid = NULL; 11121703656aSScott Long struct udf_dirstream *ds; 111351a7b740SScott Long u_long nameiop; 111451a7b740SScott Long u_long flags; 111551a7b740SScott Long char *nameptr; 111651a7b740SScott Long long namelen; 111751a7b740SScott Long ino_t id = 0; 11181703656aSScott Long int offset, error = 0; 11194ad0d60bSJohn Baldwin int fsize, lkflags, ltype, numdirpasses; 112051a7b740SScott Long 112151a7b740SScott Long dvp = a->a_dvp; 112251a7b740SScott Long node = VTON(dvp); 112351a7b740SScott Long udfmp = node->udfmp; 112451a7b740SScott Long nameiop = a->a_cnp->cn_nameiop; 112551a7b740SScott Long flags = a->a_cnp->cn_flags; 11264ad0d60bSJohn Baldwin lkflags = a->a_cnp->cn_lkflags; 112751a7b740SScott Long nameptr = a->a_cnp->cn_nameptr; 112851a7b740SScott Long namelen = a->a_cnp->cn_namelen; 1129bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len); 113051a7b740SScott Long 113151a7b740SScott Long /* 113251a7b740SScott Long * If this is a LOOKUP and we've already partially searched through 113351a7b740SScott Long * the directory, pick up where we left off and flag that the 113451a7b740SScott Long * directory may need to be searched twice. For a full description, 11354b12bb04SMaxim Konovalov * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup() 113651a7b740SScott Long */ 11371703656aSScott Long if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) { 113851a7b740SScott Long offset = 0; 113951a7b740SScott Long numdirpasses = 1; 114051a7b740SScott Long } else { 114151a7b740SScott Long offset = node->diroff; 114251a7b740SScott Long numdirpasses = 2; 114351a7b740SScott Long nchstats.ncs_2passes++; 114451a7b740SScott Long } 114551a7b740SScott Long 114651a7b740SScott Long lookloop: 11471703656aSScott Long ds = udf_opendir(node, offset, fsize, udfmp); 114851a7b740SScott Long 11491703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) { 115051a7b740SScott Long 115151a7b740SScott Long /* XXX Should we return an error on a bad fid? */ 11521703656aSScott Long if (udf_checktag(&fid->tag, TAGID_FID)) { 11531703656aSScott Long printf("udf_lookup: Invalid tag\n"); 11541703656aSScott Long error = EIO; 11551703656aSScott Long break; 11561703656aSScott Long } 1157678d5effSScott Long 1158678d5effSScott Long /* Is this a deleted file? */ 11592bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL) 11601703656aSScott Long continue; 116151a7b740SScott Long 11622bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 116351a7b740SScott Long if (flags & ISDOTDOT) { 116451a7b740SScott Long id = udf_getid(&fid->icb); 116551a7b740SScott Long break; 116651a7b740SScott Long } 116751a7b740SScott Long } else { 116851a7b740SScott Long if (!(udf_cmpname(&fid->data[fid->l_iu], 11696565282cSScott Long nameptr, fid->l_fi, namelen, udfmp))) { 117051a7b740SScott Long id = udf_getid(&fid->icb); 117151a7b740SScott Long break; 117251a7b740SScott Long } 117351a7b740SScott Long } 117451a7b740SScott Long } 11751703656aSScott Long 11761703656aSScott Long if (!error) 11771703656aSScott Long error = ds->error; 11781703656aSScott Long 11791703656aSScott Long /* XXX Bail out here? */ 11801703656aSScott Long if (error) { 11811703656aSScott Long udf_closedir(ds); 11821703656aSScott Long return (error); 118351a7b740SScott Long } 118451a7b740SScott Long 118551a7b740SScott Long /* Did we have a match? */ 118651a7b740SScott Long if (id) { 11871703656aSScott Long /* 11881703656aSScott Long * Remember where this entry was if it's the final 11891703656aSScott Long * component. 11901703656aSScott Long */ 119151a7b740SScott Long if ((flags & ISLASTCN) && nameiop == LOOKUP) 11921703656aSScott Long node->diroff = ds->offset + ds->off; 119351a7b740SScott Long if (numdirpasses == 2) 119451a7b740SScott Long nchstats.ncs_pass2++; 11954ad0d60bSJohn Baldwin udf_closedir(ds); 11964ad0d60bSJohn Baldwin 11974ad0d60bSJohn Baldwin if (flags & ISDOTDOT) { 11984ad0d60bSJohn Baldwin error = vn_vget_ino(dvp, id, lkflags, &tdp); 11994ad0d60bSJohn Baldwin } else if (node->hash_id == id) { 12004ad0d60bSJohn Baldwin VREF(dvp); /* we want ourself, ie "." */ 12014ad0d60bSJohn Baldwin /* 12024ad0d60bSJohn Baldwin * When we lookup "." we still can be asked to lock it 12034ad0d60bSJohn Baldwin * differently. 12044ad0d60bSJohn Baldwin */ 12054ad0d60bSJohn Baldwin ltype = lkflags & LK_TYPE_MASK; 12064ad0d60bSJohn Baldwin if (ltype != VOP_ISLOCKED(dvp)) { 12074ad0d60bSJohn Baldwin if (ltype == LK_EXCLUSIVE) 12084ad0d60bSJohn Baldwin vn_lock(dvp, LK_UPGRADE | LK_RETRY); 12094ad0d60bSJohn Baldwin else /* if (ltype == LK_SHARED) */ 12104ad0d60bSJohn Baldwin vn_lock(dvp, LK_DOWNGRADE | LK_RETRY); 12114ad0d60bSJohn Baldwin } 12124ad0d60bSJohn Baldwin tdp = dvp; 12134ad0d60bSJohn Baldwin } else 12144ad0d60bSJohn Baldwin error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp); 12154ad0d60bSJohn Baldwin if (!error) { 121651a7b740SScott Long *vpp = tdp; 121751a7b740SScott Long /* Put this entry in the cache */ 121851a7b740SScott Long if (flags & MAKEENTRY) 121951a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp); 12204585e3acSJeff Roberson } 12211703656aSScott Long } else { 122251a7b740SScott Long /* Name wasn't found on this pass. Do another pass? */ 122351a7b740SScott Long if (numdirpasses == 2) { 122451a7b740SScott Long numdirpasses--; 122551a7b740SScott Long offset = 0; 12261703656aSScott Long udf_closedir(ds); 122751a7b740SScott Long goto lookloop; 122851a7b740SScott Long } 12294ad0d60bSJohn Baldwin udf_closedir(ds); 123051a7b740SScott Long 123151a7b740SScott Long /* Enter name into cache as non-existant */ 123251a7b740SScott Long if (flags & MAKEENTRY) 123351a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp); 123451a7b740SScott Long 12351703656aSScott Long if ((flags & ISLASTCN) && 12361703656aSScott Long (nameiop == CREATE || nameiop == RENAME)) { 12371703656aSScott Long error = EROFS; 12381703656aSScott Long } else { 12391703656aSScott Long error = ENOENT; 12401703656aSScott Long } 12411703656aSScott Long } 124251a7b740SScott Long 12431703656aSScott Long return (error); 124451a7b740SScott Long } 124551a7b740SScott Long 124651a7b740SScott Long static int 124751a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a) 124851a7b740SScott Long { 124951a7b740SScott Long struct vnode *vp; 125051a7b740SScott Long struct udf_node *unode; 125151a7b740SScott Long 125251a7b740SScott Long vp = a->a_vp; 125351a7b740SScott Long unode = VTON(vp); 125451a7b740SScott Long 125592e73f57SAlfred Perlstein /* 125692e73f57SAlfred Perlstein * Destroy the vm object and flush associated pages. 125792e73f57SAlfred Perlstein */ 125892e73f57SAlfred Perlstein vnode_destroy_vobject(vp); 125992e73f57SAlfred Perlstein 126051a7b740SScott Long if (unode != NULL) { 12614e94fafcSPoul-Henning Kamp vfs_hash_remove(vp); 126251a7b740SScott Long 126351a7b740SScott Long if (unode->fentry != NULL) 12641ede983cSDag-Erling Smørgrav free(unode->fentry, M_UDFFENTRY); 126551a7b740SScott Long uma_zfree(udf_zone_node, unode); 126651a7b740SScott Long vp->v_data = NULL; 126751a7b740SScott Long } 126851a7b740SScott Long 126951a7b740SScott Long return (0); 127051a7b740SScott Long } 127151a7b740SScott Long 127210bcafe9SPawel Jakub Dawidek static int 127310bcafe9SPawel Jakub Dawidek udf_vptofh(struct vop_vptofh_args *a) 127410bcafe9SPawel Jakub Dawidek { 127510bcafe9SPawel Jakub Dawidek struct udf_node *node; 127610bcafe9SPawel Jakub Dawidek struct ifid *ifhp; 127710bcafe9SPawel Jakub Dawidek 127810bcafe9SPawel Jakub Dawidek node = VTON(a->a_vp); 127910bcafe9SPawel Jakub Dawidek ifhp = (struct ifid *)a->a_fhp; 128010bcafe9SPawel Jakub Dawidek ifhp->ifid_len = sizeof(struct ifid); 128110bcafe9SPawel Jakub Dawidek ifhp->ifid_ino = node->hash_id; 128210bcafe9SPawel Jakub Dawidek 128310bcafe9SPawel Jakub Dawidek return (0); 128410bcafe9SPawel Jakub Dawidek } 128510bcafe9SPawel Jakub Dawidek 128651a7b740SScott Long /* 128751a7b740SScott Long * Read the block and then set the data pointer to correspond with the 128851a7b740SScott Long * offset passed in. Only read in at most 'size' bytes, and then set 'size' 128951a7b740SScott Long * to the number of bytes pointed to. If 'size' is zero, try to read in a 129051a7b740SScott Long * whole extent. 1291744bb56dSScott Long * 1292744bb56dSScott Long * Note that *bp may be assigned error or not. 1293744bb56dSScott Long * 129451a7b740SScott Long */ 129551a7b740SScott Long static int 12969d32fde8SScott Long udf_readatoffset(struct udf_node *node, int *size, off_t offset, 12979d32fde8SScott Long struct buf **bp, uint8_t **data) 129851a7b740SScott Long { 1299b0c0fb59SAndriy Gapon struct udf_mnt *udfmp = node->udfmp; 1300b0c0fb59SAndriy Gapon struct vnode *vp = node->i_vnode; 1301b0c0fb59SAndriy Gapon struct file_entry *fentry; 130251a7b740SScott Long struct buf *bp1; 1303c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size; 130498b0c789SPoul-Henning Kamp daddr_t sector; 1305b0c0fb59SAndriy Gapon off_t off; 1306b0c0fb59SAndriy Gapon int adj_size; 130751a7b740SScott Long int error; 130851a7b740SScott Long 1309b0c0fb59SAndriy Gapon /* 1310b0c0fb59SAndriy Gapon * This call is made *not* only to detect UDF_INVALID_BMAP case, 1311b0c0fb59SAndriy Gapon * max_size is used as an ad-hoc read-ahead hint for "normal" case. 1312b0c0fb59SAndriy Gapon */ 131351a7b740SScott Long error = udf_bmap_internal(node, offset, §or, &max_size); 13144576293dSScott Long if (error == UDF_INVALID_BMAP) { 131551a7b740SScott Long /* 131651a7b740SScott Long * This error means that the file *data* is stored in the 131751a7b740SScott Long * allocation descriptor field of the file entry. 131851a7b740SScott Long */ 131951a7b740SScott Long fentry = node->fentry; 1320bf1c3dddSScott Long *data = &fentry->data[le32toh(fentry->l_ea)]; 1321bf1c3dddSScott Long *size = le32toh(fentry->l_ad); 1322b2c91b67SAndriy Gapon if (offset >= *size) 1323b2c91b67SAndriy Gapon *size = 0; 1324b2c91b67SAndriy Gapon else { 1325b2c91b67SAndriy Gapon *data += offset; 1326b2c91b67SAndriy Gapon *size -= offset; 1327b2c91b67SAndriy Gapon } 132851a7b740SScott Long return (0); 132951a7b740SScott Long } else if (error != 0) { 133051a7b740SScott Long return (error); 133151a7b740SScott Long } 133251a7b740SScott Long 1333d1def83bSScott Long /* Adjust the size so that it is within range */ 133451a7b740SScott Long if (*size == 0 || *size > max_size) 133551a7b740SScott Long *size = max_size; 133651a7b740SScott Long 1337b0c0fb59SAndriy Gapon /* 1338b0c0fb59SAndriy Gapon * Because we will read starting at block boundary, we need to adjust 1339b0c0fb59SAndriy Gapon * how much we need to read so that all promised data is in. 1340b0c0fb59SAndriy Gapon * Also, we can't promise to read more than MAXBSIZE bytes starting 1341b0c0fb59SAndriy Gapon * from block boundary, so adjust what we promise too. 1342b0c0fb59SAndriy Gapon */ 1343b0c0fb59SAndriy Gapon off = blkoff(udfmp, offset); 1344b0c0fb59SAndriy Gapon *size = min(*size, MAXBSIZE - off); 1345b0c0fb59SAndriy Gapon adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask; 1346b0c0fb59SAndriy Gapon *bp = NULL; 1347b0c0fb59SAndriy Gapon if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) { 13481830bca1SScott Long printf("warning: udf_readlblks returned error %d\n", error); 1349744bb56dSScott Long /* note: *bp may be non-NULL */ 135051a7b740SScott Long return (error); 135151a7b740SScott Long } 135251a7b740SScott Long 135351a7b740SScott Long bp1 = *bp; 13545df29e0cSRemko Lodder *data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask]; 135551a7b740SScott Long return (0); 135651a7b740SScott Long } 135751a7b740SScott Long 135851a7b740SScott Long /* 135951a7b740SScott Long * Translate a file offset into a logical block and then into a physical 136051a7b740SScott Long * block. 13615df29e0cSRemko Lodder * max_size - maximum number of bytes that can be read starting from given 13625df29e0cSRemko Lodder * offset, rather than beginning of calculated sector number 136351a7b740SScott Long */ 136451a7b740SScott Long static int 13659d32fde8SScott Long udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector, 13669d32fde8SScott Long uint32_t *max_size) 136751a7b740SScott Long { 136851a7b740SScott Long struct udf_mnt *udfmp; 136951a7b740SScott Long struct file_entry *fentry; 137051a7b740SScott Long void *icb; 137151a7b740SScott Long struct icb_tag *tag; 1372c2d6947dSJeroen Ruigrok van der Werven uint32_t icblen = 0; 137398b0c789SPoul-Henning Kamp daddr_t lsector; 137451a7b740SScott Long int ad_offset, ad_num = 0; 137551a7b740SScott Long int i, p_offset; 137651a7b740SScott Long 137751a7b740SScott Long udfmp = node->udfmp; 137851a7b740SScott Long fentry = node->fentry; 137951a7b740SScott Long tag = &fentry->icbtag; 138051a7b740SScott Long 1381bf1c3dddSScott Long switch (le16toh(tag->strat_type)) { 138251a7b740SScott Long case 4: 138351a7b740SScott Long break; 138451a7b740SScott Long 138551a7b740SScott Long case 4096: 138651a7b740SScott Long printf("Cannot deal with strategy4096 yet!\n"); 138751a7b740SScott Long return (ENODEV); 138851a7b740SScott Long 138951a7b740SScott Long default: 139051a7b740SScott Long printf("Unknown strategy type %d\n", tag->strat_type); 139151a7b740SScott Long return (ENODEV); 139251a7b740SScott Long } 139351a7b740SScott Long 1394bf1c3dddSScott Long switch (le16toh(tag->flags) & 0x7) { 139551a7b740SScott Long case 0: 139651a7b740SScott Long /* 139751a7b740SScott Long * The allocation descriptor field is filled with short_ad's. 139851a7b740SScott Long * If the offset is beyond the current extent, look for the 139951a7b740SScott Long * next extent. 140051a7b740SScott Long */ 140151a7b740SScott Long do { 140251a7b740SScott Long offset -= icblen; 140351a7b740SScott Long ad_offset = sizeof(struct short_ad) * ad_num; 1404bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) { 140551a7b740SScott Long printf("File offset out of bounds\n"); 140651a7b740SScott Long return (EINVAL); 140751a7b740SScott Long } 1408a4d629e3SScott Long icb = GETICB(short_ad, fentry, 1409bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset); 141051a7b740SScott Long icblen = GETICBLEN(short_ad, icb); 141151a7b740SScott Long ad_num++; 141251a7b740SScott Long } while(offset >= icblen); 141351a7b740SScott Long 141451a7b740SScott Long lsector = (offset >> udfmp->bshift) + 1415937a2387SWill Andrews le32toh(((struct short_ad *)(icb))->pos); 141651a7b740SScott Long 14175df29e0cSRemko Lodder *max_size = icblen - offset; 141851a7b740SScott Long 141951a7b740SScott Long break; 142051a7b740SScott Long case 1: 142151a7b740SScott Long /* 142251a7b740SScott Long * The allocation descriptor field is filled with long_ad's 142351a7b740SScott Long * If the offset is beyond the current extent, look for the 142451a7b740SScott Long * next extent. 142551a7b740SScott Long */ 142651a7b740SScott Long do { 142751a7b740SScott Long offset -= icblen; 142851a7b740SScott Long ad_offset = sizeof(struct long_ad) * ad_num; 1429bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) { 143051a7b740SScott Long printf("File offset out of bounds\n"); 143151a7b740SScott Long return (EINVAL); 143251a7b740SScott Long } 1433bf1c3dddSScott Long icb = GETICB(long_ad, fentry, 1434bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset); 143551a7b740SScott Long icblen = GETICBLEN(long_ad, icb); 143651a7b740SScott Long ad_num++; 143751a7b740SScott Long } while(offset >= icblen); 143851a7b740SScott Long 143951a7b740SScott Long lsector = (offset >> udfmp->bshift) + 1440bf1c3dddSScott Long le32toh(((struct long_ad *)(icb))->loc.lb_num); 144151a7b740SScott Long 14425df29e0cSRemko Lodder *max_size = icblen - offset; 144351a7b740SScott Long 144451a7b740SScott Long break; 144551a7b740SScott Long case 3: 144651a7b740SScott Long /* 144751a7b740SScott Long * This type means that the file *data* is stored in the 144851a7b740SScott Long * allocation descriptor field of the file entry. 144951a7b740SScott Long */ 145051a7b740SScott Long *max_size = 0; 14518db4c2f2SScott Long *sector = node->hash_id + udfmp->part_start; 145251a7b740SScott Long 14534576293dSScott Long return (UDF_INVALID_BMAP); 145451a7b740SScott Long case 2: 145551a7b740SScott Long /* DirectCD does not use extended_ad's */ 145651a7b740SScott Long default: 145751a7b740SScott Long printf("Unsupported allocation descriptor %d\n", 145851a7b740SScott Long tag->flags & 0x7); 145951a7b740SScott Long return (ENODEV); 146051a7b740SScott Long } 146151a7b740SScott Long 146251a7b740SScott Long *sector = lsector + udfmp->part_start; 146351a7b740SScott Long 146451a7b740SScott Long /* 146551a7b740SScott Long * Check the sparing table. Each entry represents the beginning of 146651a7b740SScott Long * a packet. 146751a7b740SScott Long */ 146851a7b740SScott Long if (udfmp->s_table != NULL) { 146951a7b740SScott Long for (i = 0; i< udfmp->s_table_entries; i++) { 1470bf1c3dddSScott Long p_offset = 1471bf1c3dddSScott Long lsector - le32toh(udfmp->s_table->entries[i].org); 147251a7b740SScott Long if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) { 1473bf1c3dddSScott Long *sector = 1474bf1c3dddSScott Long le32toh(udfmp->s_table->entries[i].map) + 147551a7b740SScott Long p_offset; 147651a7b740SScott Long break; 147751a7b740SScott Long } 147851a7b740SScott Long } 147951a7b740SScott Long } 148051a7b740SScott Long 148151a7b740SScott Long return (0); 148251a7b740SScott Long } 1483