151a7b740SScott Long /*- 251a7b740SScott Long * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org> 351a7b740SScott Long * All rights reserved. 451a7b740SScott Long * 551a7b740SScott Long * Redistribution and use in source and binary forms, with or without 651a7b740SScott Long * modification, are permitted provided that the following conditions 751a7b740SScott Long * are met: 851a7b740SScott Long * 1. Redistributions of source code must retain the above copyright 951a7b740SScott Long * notice, this list of conditions and the following disclaimer. 1051a7b740SScott Long * 2. Redistributions in binary form must reproduce the above copyright 1151a7b740SScott Long * notice, this list of conditions and the following disclaimer in the 1251a7b740SScott Long * documentation and/or other materials provided with the distribution. 1351a7b740SScott Long * 1451a7b740SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1551a7b740SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1651a7b740SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1751a7b740SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1851a7b740SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1951a7b740SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2051a7b740SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2151a7b740SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2251a7b740SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2351a7b740SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2451a7b740SScott Long * SUCH DAMAGE. 2551a7b740SScott Long * 2651a7b740SScott Long * $FreeBSD$ 2751a7b740SScott Long */ 2851a7b740SScott Long 2951a7b740SScott Long /* udf_vnops.c */ 3051a7b740SScott Long /* Take care of the vnode side of things */ 3151a7b740SScott Long 3251a7b740SScott Long #include <sys/param.h> 3351a7b740SScott Long #include <sys/systm.h> 3451a7b740SScott Long #include <sys/namei.h> 3551a7b740SScott Long #include <sys/kernel.h> 3651a7b740SScott Long #include <sys/malloc.h> 3751a7b740SScott Long #include <sys/stat.h> 3851a7b740SScott Long #include <sys/bio.h> 394e94fafcSPoul-Henning Kamp #include <sys/conf.h> 4051a7b740SScott Long #include <sys/buf.h> 416565282cSScott Long #include <sys/iconv.h> 4251a7b740SScott Long #include <sys/mount.h> 4351a7b740SScott Long #include <sys/vnode.h> 4451a7b740SScott Long #include <sys/dirent.h> 4551a7b740SScott Long #include <sys/queue.h> 4651a7b740SScott Long #include <sys/unistd.h> 4789ec2c3cSScott Long #include <sys/endian.h> 4851a7b740SScott Long 4951a7b740SScott Long #include <vm/uma.h> 5051a7b740SScott Long 5151a7b740SScott Long #include <fs/udf/ecma167-udf.h> 5251a7b740SScott Long #include <fs/udf/osta.h> 5351a7b740SScott Long #include <fs/udf/udf.h> 546565282cSScott Long #include <fs/udf/udf_mount.h> 556565282cSScott Long 566565282cSScott Long extern struct iconv_functions *udf_iconv; 5751a7b740SScott Long 586fde64c7SPoul-Henning Kamp static vop_access_t udf_access; 596fde64c7SPoul-Henning Kamp static vop_getattr_t udf_getattr; 6035e06624SPav Lucistnik static vop_open_t udf_open; 616fde64c7SPoul-Henning Kamp static vop_ioctl_t udf_ioctl; 626fde64c7SPoul-Henning Kamp static vop_pathconf_t udf_pathconf; 6361e69c80SJohn Baldwin static vop_print_t udf_print; 646fde64c7SPoul-Henning Kamp static vop_read_t udf_read; 656fde64c7SPoul-Henning Kamp static vop_readdir_t udf_readdir; 666fde64c7SPoul-Henning Kamp static vop_readlink_t udf_readlink; 6761e69c80SJohn Baldwin static vop_setattr_t udf_setattr; 686fde64c7SPoul-Henning Kamp static vop_strategy_t udf_strategy; 696fde64c7SPoul-Henning Kamp static vop_bmap_t udf_bmap; 706fde64c7SPoul-Henning Kamp static vop_cachedlookup_t udf_lookup; 716fde64c7SPoul-Henning Kamp static vop_reclaim_t udf_reclaim; 7210bcafe9SPawel Jakub Dawidek static vop_vptofh_t udf_vptofh; 739d32fde8SScott Long static int udf_readatoffset(struct udf_node *node, int *size, off_t offset, 749d32fde8SScott Long struct buf **bp, uint8_t **data); 759d32fde8SScott Long static int udf_bmap_internal(struct udf_node *node, off_t offset, 769d32fde8SScott Long daddr_t *sector, uint32_t *max_size); 7751a7b740SScott Long 78aec0fb7bSPoul-Henning Kamp static struct vop_vector udf_vnodeops = { 79aec0fb7bSPoul-Henning Kamp .vop_default = &default_vnodeops, 8083c64397SPoul-Henning Kamp 81aec0fb7bSPoul-Henning Kamp .vop_access = udf_access, 82aec0fb7bSPoul-Henning Kamp .vop_bmap = udf_bmap, 83aec0fb7bSPoul-Henning Kamp .vop_cachedlookup = udf_lookup, 84aec0fb7bSPoul-Henning Kamp .vop_getattr = udf_getattr, 85aec0fb7bSPoul-Henning Kamp .vop_ioctl = udf_ioctl, 86aec0fb7bSPoul-Henning Kamp .vop_lookup = vfs_cache_lookup, 8735e06624SPav Lucistnik .vop_open = udf_open, 88aec0fb7bSPoul-Henning Kamp .vop_pathconf = udf_pathconf, 8961e69c80SJohn Baldwin .vop_print = udf_print, 90aec0fb7bSPoul-Henning Kamp .vop_read = udf_read, 91aec0fb7bSPoul-Henning Kamp .vop_readdir = udf_readdir, 92aec0fb7bSPoul-Henning Kamp .vop_readlink = udf_readlink, 93aec0fb7bSPoul-Henning Kamp .vop_reclaim = udf_reclaim, 9461e69c80SJohn Baldwin .vop_setattr = udf_setattr, 95aec0fb7bSPoul-Henning Kamp .vop_strategy = udf_strategy, 9610bcafe9SPawel Jakub Dawidek .vop_vptofh = udf_vptofh, 9751a7b740SScott Long }; 9851a7b740SScott Long 9961e69c80SJohn Baldwin struct vop_vector udf_fifoops = { 10061e69c80SJohn Baldwin .vop_default = &fifo_specops, 10161e69c80SJohn Baldwin .vop_access = udf_access, 10261e69c80SJohn Baldwin .vop_getattr = udf_getattr, 10361e69c80SJohn Baldwin .vop_print = udf_print, 10461e69c80SJohn Baldwin .vop_reclaim = udf_reclaim, 10561e69c80SJohn Baldwin .vop_setattr = udf_setattr, 10661e69c80SJohn Baldwin .vop_vptofh = udf_vptofh, 10761e69c80SJohn Baldwin }; 10861e69c80SJohn Baldwin 109d745c852SEd Schouten static MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure"); 110d745c852SEd Schouten static MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure"); 11151a7b740SScott Long 1124576293dSScott Long #define UDF_INVALID_BMAP -1 1138db4c2f2SScott Long 11451a7b740SScott Long int 11551a7b740SScott Long udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td) 11651a7b740SScott Long { 11751a7b740SScott Long int error; 11851a7b740SScott Long struct vnode *vp; 11951a7b740SScott Long 120aec0fb7bSPoul-Henning Kamp error = getnewvnode("udf", mp, &udf_vnodeops, &vp); 12151a7b740SScott Long if (error) { 12251a7b740SScott Long printf("udf_allocv: failed to allocate new vnode\n"); 12351a7b740SScott Long return (error); 12451a7b740SScott Long } 12551a7b740SScott Long 12651a7b740SScott Long *vpp = vp; 12751a7b740SScott Long return (0); 12851a7b740SScott Long } 12951a7b740SScott Long 13051a7b740SScott Long /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */ 13151a7b740SScott Long static mode_t 13251a7b740SScott Long udf_permtomode(struct udf_node *node) 13351a7b740SScott Long { 134c2d6947dSJeroen Ruigrok van der Werven uint32_t perm; 135bf1c3dddSScott Long uint16_t flags; 13651a7b740SScott Long mode_t mode; 13751a7b740SScott Long 138bf1c3dddSScott Long perm = le32toh(node->fentry->perm); 139bf1c3dddSScott Long flags = le16toh(node->fentry->icbtag.flags); 14051a7b740SScott Long 14151a7b740SScott Long mode = perm & UDF_FENTRY_PERM_USER_MASK; 14251a7b740SScott Long mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2); 14351a7b740SScott Long mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4); 14451a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4); 14551a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6); 14651a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8); 14751a7b740SScott Long 14851a7b740SScott Long return (mode); 14951a7b740SScott Long } 15051a7b740SScott Long 15151a7b740SScott Long static int 15251a7b740SScott Long udf_access(struct vop_access_args *a) 15351a7b740SScott Long { 15451a7b740SScott Long struct vnode *vp; 15551a7b740SScott Long struct udf_node *node; 15615bc6b2bSEdward Tomasz Napierala accmode_t accmode; 15715bc6b2bSEdward Tomasz Napierala mode_t mode; 15851a7b740SScott Long 15951a7b740SScott Long vp = a->a_vp; 16051a7b740SScott Long node = VTON(vp); 16115bc6b2bSEdward Tomasz Napierala accmode = a->a_accmode; 16251a7b740SScott Long 16315bc6b2bSEdward Tomasz Napierala if (accmode & VWRITE) { 16451a7b740SScott Long switch (vp->v_type) { 16551a7b740SScott Long case VDIR: 16651a7b740SScott Long case VLNK: 16751a7b740SScott Long case VREG: 16851a7b740SScott Long return (EROFS); 16951a7b740SScott Long /* NOT REACHED */ 17051a7b740SScott Long default: 17151a7b740SScott Long break; 17251a7b740SScott Long } 17351a7b740SScott Long } 17451a7b740SScott Long 17551a7b740SScott Long mode = udf_permtomode(node); 17651a7b740SScott Long 17751a7b740SScott Long return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid, 17815bc6b2bSEdward Tomasz Napierala accmode, a->a_cred, NULL)); 17951a7b740SScott Long } 18051a7b740SScott Long 18135e06624SPav Lucistnik static int 18235e06624SPav Lucistnik udf_open(struct vop_open_args *ap) { 18335e06624SPav Lucistnik struct udf_node *np = VTON(ap->a_vp); 18435e06624SPav Lucistnik off_t fsize; 18535e06624SPav Lucistnik 18635e06624SPav Lucistnik fsize = le64toh(np->fentry->inf_len); 18735e06624SPav Lucistnik vnode_create_vobject(ap->a_vp, fsize, ap->a_td); 18835e06624SPav Lucistnik return 0; 18935e06624SPav Lucistnik } 19035e06624SPav Lucistnik 1919c2bf69dSMarkus Brueffer static const int mon_lens[2][12] = { 1929c2bf69dSMarkus Brueffer {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, 1939c2bf69dSMarkus Brueffer {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} 19451a7b740SScott Long }; 19551a7b740SScott Long 19651a7b740SScott Long static int 19751a7b740SScott Long udf_isaleapyear(int year) 19851a7b740SScott Long { 19951a7b740SScott Long int i; 20051a7b740SScott Long 20151a7b740SScott Long i = (year % 4) ? 0 : 1; 20251a7b740SScott Long i &= (year % 100) ? 1 : 0; 20351a7b740SScott Long i |= (year % 400) ? 0 : 1; 20451a7b740SScott Long 20551a7b740SScott Long return i; 20651a7b740SScott Long } 20751a7b740SScott Long 20851a7b740SScott Long /* 20951a7b740SScott Long * Timezone calculation compliments of Julian Elischer <julian@elischer.org>. 21051a7b740SScott Long */ 21151a7b740SScott Long static void 21251a7b740SScott Long udf_timetotimespec(struct timestamp *time, struct timespec *t) 21351a7b740SScott Long { 2149c2bf69dSMarkus Brueffer int i, lpyear, daysinyear, year, startyear; 21551a7b740SScott Long union { 216c2d6947dSJeroen Ruigrok van der Werven uint16_t u_tz_offset; 21751a7b740SScott Long int16_t s_tz_offset; 21851a7b740SScott Long } tz; 21951a7b740SScott Long 2209c2bf69dSMarkus Brueffer /* 2219c2bf69dSMarkus Brueffer * DirectCD seems to like using bogus year values. 2229c2bf69dSMarkus Brueffer * Don't trust time->month as it will be used for an array index. 2239c2bf69dSMarkus Brueffer */ 224bf1c3dddSScott Long year = le16toh(time->year); 2259c2bf69dSMarkus Brueffer if (year < 1970 || time->month < 1 || time->month > 12) { 22651a7b740SScott Long t->tv_sec = 0; 2279c2bf69dSMarkus Brueffer t->tv_nsec = 0; 22851a7b740SScott Long return; 22951a7b740SScott Long } 23051a7b740SScott Long 23151a7b740SScott Long /* Calculate the time and day */ 23251a7b740SScott Long t->tv_sec = time->second; 23351a7b740SScott Long t->tv_sec += time->minute * 60; 23451a7b740SScott Long t->tv_sec += time->hour * 3600; 2359c2bf69dSMarkus Brueffer t->tv_sec += (time->day - 1) * 3600 * 24; 23651a7b740SScott Long 237befb7f33SChristian Brueffer /* Calculate the month */ 238bf1c3dddSScott Long lpyear = udf_isaleapyear(year); 2399c2bf69dSMarkus Brueffer t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24; 24051a7b740SScott Long 24151a7b740SScott Long /* Speed up the calculation */ 2429c2bf69dSMarkus Brueffer startyear = 1970; 2439c2bf69dSMarkus Brueffer if (year > 2009) { 2449c2bf69dSMarkus Brueffer t->tv_sec += 1262304000; 2459c2bf69dSMarkus Brueffer startyear += 40; 2469c2bf69dSMarkus Brueffer } else if (year > 1999) { 2479c2bf69dSMarkus Brueffer t->tv_sec += 946684800; 2489c2bf69dSMarkus Brueffer startyear += 30; 2499c2bf69dSMarkus Brueffer } else if (year > 1989) { 2509c2bf69dSMarkus Brueffer t->tv_sec += 631152000; 2519c2bf69dSMarkus Brueffer startyear += 20; 2529c2bf69dSMarkus Brueffer } else if (year > 1979) { 25351a7b740SScott Long t->tv_sec += 315532800; 2549c2bf69dSMarkus Brueffer startyear += 10; 25551a7b740SScott Long } 25651a7b740SScott Long 2579c2bf69dSMarkus Brueffer daysinyear = (year - startyear) * 365; 2589c2bf69dSMarkus Brueffer for (i = startyear; i < year; i++) 2599c2bf69dSMarkus Brueffer daysinyear += udf_isaleapyear(i); 2609c2bf69dSMarkus Brueffer t->tv_sec += daysinyear * 3600 * 24; 2619c2bf69dSMarkus Brueffer 2629c2bf69dSMarkus Brueffer /* Calculate microseconds */ 2639c2bf69dSMarkus Brueffer t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 + 2649c2bf69dSMarkus Brueffer time->usec; 2659c2bf69dSMarkus Brueffer 26651a7b740SScott Long /* 26751a7b740SScott Long * Calculate the time zone. The timezone is 12 bit signed 2's 268befb7f33SChristian Brueffer * complement, so we gotta do some extra magic to handle it right. 26951a7b740SScott Long */ 270bf1c3dddSScott Long tz.u_tz_offset = le16toh(time->type_tz); 27151a7b740SScott Long tz.u_tz_offset &= 0x0fff; 27251a7b740SScott Long if (tz.u_tz_offset & 0x0800) 27351a7b740SScott Long tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */ 2749c2bf69dSMarkus Brueffer if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047)) 27551a7b740SScott Long t->tv_sec -= tz.s_tz_offset * 60; 27651a7b740SScott Long 27751a7b740SScott Long return; 27851a7b740SScott Long } 27951a7b740SScott Long 28051a7b740SScott Long static int 28151a7b740SScott Long udf_getattr(struct vop_getattr_args *a) 28251a7b740SScott Long { 28351a7b740SScott Long struct vnode *vp; 28451a7b740SScott Long struct udf_node *node; 28551a7b740SScott Long struct vattr *vap; 28651a7b740SScott Long struct file_entry *fentry; 28751a7b740SScott Long struct timespec ts; 28851a7b740SScott Long 28951a7b740SScott Long ts.tv_sec = 0; 29051a7b740SScott Long 29151a7b740SScott Long vp = a->a_vp; 29251a7b740SScott Long vap = a->a_vap; 29351a7b740SScott Long node = VTON(vp); 29451a7b740SScott Long fentry = node->fentry; 29551a7b740SScott Long 296c049546eSPoul-Henning Kamp vap->va_fsid = dev2udev(node->udfmp->im_dev); 29751a7b740SScott Long vap->va_fileid = node->hash_id; 29851a7b740SScott Long vap->va_mode = udf_permtomode(node); 299bf1c3dddSScott Long vap->va_nlink = le16toh(fentry->link_cnt); 30051a7b740SScott Long /* 30151a7b740SScott Long * XXX The spec says that -1 is valid for uid/gid and indicates an 30251a7b740SScott Long * invalid uid/gid. How should this be represented? 30351a7b740SScott Long */ 304bf1c3dddSScott Long vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid); 305bf1c3dddSScott Long vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid); 30651a7b740SScott Long udf_timetotimespec(&fentry->atime, &vap->va_atime); 30751a7b740SScott Long udf_timetotimespec(&fentry->mtime, &vap->va_mtime); 30851a7b740SScott Long vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */ 3094c5a20e3SKonstantin Belousov vap->va_rdev = NODEV; 31051a7b740SScott Long if (vp->v_type & VDIR) { 31151a7b740SScott Long /* 31251a7b740SScott Long * Directories that are recorded within their ICB will show 31351a7b740SScott Long * as having 0 blocks recorded. Since tradition dictates 31451a7b740SScott Long * that directories consume at least one logical block, 31551a7b740SScott Long * make it appear so. 31651a7b740SScott Long */ 31751a7b740SScott Long if (fentry->logblks_rec != 0) { 318bf1c3dddSScott Long vap->va_size = 319bf1c3dddSScott Long le64toh(fentry->logblks_rec) * node->udfmp->bsize; 32051a7b740SScott Long } else { 32151a7b740SScott Long vap->va_size = node->udfmp->bsize; 32251a7b740SScott Long } 32351a7b740SScott Long } else { 324bf1c3dddSScott Long vap->va_size = le64toh(fentry->inf_len); 32551a7b740SScott Long } 32651a7b740SScott Long vap->va_flags = 0; 32751a7b740SScott Long vap->va_gen = 1; 32851a7b740SScott Long vap->va_blocksize = node->udfmp->bsize; 329bf1c3dddSScott Long vap->va_bytes = le64toh(fentry->inf_len); 33051a7b740SScott Long vap->va_type = vp->v_type; 33151a7b740SScott Long vap->va_filerev = 0; /* XXX */ 33251a7b740SScott Long return (0); 33351a7b740SScott Long } 33451a7b740SScott Long 33561e69c80SJohn Baldwin static int 33661e69c80SJohn Baldwin udf_setattr(struct vop_setattr_args *a) 33761e69c80SJohn Baldwin { 33861e69c80SJohn Baldwin struct vnode *vp; 33961e69c80SJohn Baldwin struct vattr *vap; 34061e69c80SJohn Baldwin 34161e69c80SJohn Baldwin vp = a->a_vp; 34261e69c80SJohn Baldwin vap = a->a_vap; 34361e69c80SJohn Baldwin if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL || 34461e69c80SJohn Baldwin vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 34561e69c80SJohn Baldwin vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) 34661e69c80SJohn Baldwin return (EROFS); 34761e69c80SJohn Baldwin if (vap->va_size != (u_quad_t)VNOVAL) { 34861e69c80SJohn Baldwin switch (vp->v_type) { 34961e69c80SJohn Baldwin case VDIR: 35061e69c80SJohn Baldwin return (EISDIR); 35161e69c80SJohn Baldwin case VLNK: 35261e69c80SJohn Baldwin case VREG: 35361e69c80SJohn Baldwin return (EROFS); 35461e69c80SJohn Baldwin case VCHR: 35561e69c80SJohn Baldwin case VBLK: 35661e69c80SJohn Baldwin case VSOCK: 35761e69c80SJohn Baldwin case VFIFO: 35861e69c80SJohn Baldwin case VNON: 35961e69c80SJohn Baldwin case VBAD: 36061e69c80SJohn Baldwin case VMARKER: 36161e69c80SJohn Baldwin return (0); 36261e69c80SJohn Baldwin } 36361e69c80SJohn Baldwin } 36461e69c80SJohn Baldwin return (0); 36561e69c80SJohn Baldwin } 36661e69c80SJohn Baldwin 36751a7b740SScott Long /* 368bf1c3dddSScott Long * File specific ioctls. 36951a7b740SScott Long */ 37051a7b740SScott Long static int 37151a7b740SScott Long udf_ioctl(struct vop_ioctl_args *a) 37251a7b740SScott Long { 373c80a90c5SScott Long printf("%s called\n", __func__); 3741d02d910SPoul-Henning Kamp return (ENOTTY); 37551a7b740SScott Long } 37651a7b740SScott Long 37751a7b740SScott Long /* 37851a7b740SScott Long * I'm not sure that this has much value in a read-only filesystem, but 37951a7b740SScott Long * cd9660 has it too. 38051a7b740SScott Long */ 38151a7b740SScott Long static int 38251a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a) 38351a7b740SScott Long { 38451a7b740SScott Long 38551a7b740SScott Long switch (a->a_name) { 38651a7b740SScott Long case _PC_LINK_MAX: 38751a7b740SScott Long *a->a_retval = 65535; 38851a7b740SScott Long return (0); 38951a7b740SScott Long case _PC_NAME_MAX: 39051a7b740SScott Long *a->a_retval = NAME_MAX; 39151a7b740SScott Long return (0); 39251a7b740SScott Long case _PC_PATH_MAX: 39351a7b740SScott Long *a->a_retval = PATH_MAX; 39451a7b740SScott Long return (0); 39551a7b740SScott Long case _PC_NO_TRUNC: 39651a7b740SScott Long *a->a_retval = 1; 39751a7b740SScott Long return (0); 39851a7b740SScott Long default: 39951a7b740SScott Long return (EINVAL); 40051a7b740SScott Long } 40151a7b740SScott Long } 40251a7b740SScott Long 40361e69c80SJohn Baldwin static int 40461e69c80SJohn Baldwin udf_print(struct vop_print_args *ap) 40561e69c80SJohn Baldwin { 40661e69c80SJohn Baldwin struct vnode *vp = ap->a_vp; 40761e69c80SJohn Baldwin struct udf_node *node = VTON(vp); 40861e69c80SJohn Baldwin 40961e69c80SJohn Baldwin printf(" ino %lu, on dev %s", (u_long)node->hash_id, 41061e69c80SJohn Baldwin devtoname(node->udfmp->im_dev)); 41161e69c80SJohn Baldwin if (vp->v_type == VFIFO) 41261e69c80SJohn Baldwin fifo_printinfo(vp); 41361e69c80SJohn Baldwin printf("\n"); 41461e69c80SJohn Baldwin return (0); 41561e69c80SJohn Baldwin } 41661e69c80SJohn Baldwin 4170c09ac0dSPav Lucistnik #define lblkno(udfmp, loc) ((loc) >> (udfmp)->bshift) 4180c09ac0dSPav Lucistnik #define blkoff(udfmp, loc) ((loc) & (udfmp)->bmask) 4195792e04dSAndriy Gapon #define lblktosize(udfmp, blk) ((blk) << (udfmp)->bshift) 42051a7b740SScott Long 421be52a95dSAndriy Gapon static inline int 422be52a95dSAndriy Gapon is_data_in_fentry(const struct udf_node *node) 423be52a95dSAndriy Gapon { 424be52a95dSAndriy Gapon const struct file_entry *fentry = node->fentry; 425be52a95dSAndriy Gapon 426be52a95dSAndriy Gapon return ((le16toh(fentry->icbtag.flags) & 0x7) == 3); 427be52a95dSAndriy Gapon } 428be52a95dSAndriy Gapon 4290c09ac0dSPav Lucistnik static int 4300c09ac0dSPav Lucistnik udf_read(struct vop_read_args *ap) 4310c09ac0dSPav Lucistnik { 4320c09ac0dSPav Lucistnik struct vnode *vp = ap->a_vp; 4330c09ac0dSPav Lucistnik struct uio *uio = ap->a_uio; 4340c09ac0dSPav Lucistnik struct udf_node *node = VTON(vp); 4350c09ac0dSPav Lucistnik struct udf_mnt *udfmp; 436be52a95dSAndriy Gapon struct file_entry *fentry; 4370c09ac0dSPav Lucistnik struct buf *bp; 438be52a95dSAndriy Gapon uint8_t *data; 4390c09ac0dSPav Lucistnik daddr_t lbn, rablock; 4400c09ac0dSPav Lucistnik off_t diff, fsize; 441526d0bd5SKonstantin Belousov ssize_t n; 4420c09ac0dSPav Lucistnik int error = 0; 443526d0bd5SKonstantin Belousov long size, 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) { 481c535690bSKonstantin Belousov error = cluster_read(vp, fsize, lbn, size, 482c535690bSKonstantin Belousov NOCRED, uio->uio_resid, 483c535690bSKonstantin Belousov (ap->a_ioflag >> 16), 0, &bp); 4840c09ac0dSPav Lucistnik } else { 4850c09ac0dSPav Lucistnik error = bread(vp, lbn, size, NOCRED, &bp); 4860c09ac0dSPav Lucistnik } 4870c09ac0dSPav Lucistnik } else { 4880c09ac0dSPav Lucistnik error = bread(vp, lbn, size, NOCRED, &bp); 4890c09ac0dSPav Lucistnik } 490*1fa81dabSKonstantin Belousov if (error != 0) { 49151a7b740SScott Long brelse(bp); 4920c09ac0dSPav Lucistnik return (error); 4930c09ac0dSPav Lucistnik } 494*1fa81dabSKonstantin Belousov n = min(n, size - bp->b_resid); 49551a7b740SScott Long 4960c09ac0dSPav Lucistnik error = uiomove(bp->b_data + on, (int)n, uio); 4970c09ac0dSPav Lucistnik brelse(bp); 4980c09ac0dSPav Lucistnik } while (error == 0 && uio->uio_resid > 0 && n != 0); 49951a7b740SScott Long return (error); 50051a7b740SScott Long } 50151a7b740SScott Long 50251a7b740SScott Long /* 50351a7b740SScott Long * Call the OSTA routines to translate the name from a CS0 dstring to a 50451a7b740SScott Long * 16-bit Unicode String. Hooks need to be placed in here to translate from 5056565282cSScott Long * Unicode to the encoding that the kernel/user expects. Return the length 5066565282cSScott Long * of the translated string. 50751a7b740SScott Long */ 50851a7b740SScott Long static int 5096565282cSScott Long udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp) 51051a7b740SScott Long { 51151a7b740SScott Long unicode_t *transname; 5126565282cSScott Long char *unibuf, *unip; 513181fc3c6SR. Imura int i, destlen; 514181fc3c6SR. Imura ssize_t unilen = 0; 5156565282cSScott Long size_t destleft = MAXNAMLEN; 51651a7b740SScott Long 5176565282cSScott Long /* Convert 16-bit Unicode to destname */ 5186565282cSScott Long if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) { 5196565282cSScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */ 5206565282cSScott Long unibuf = uma_zalloc(udf_zone_trans, M_WAITOK); 5216565282cSScott Long unip = unibuf; 522181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) { 5236565282cSScott Long printf("udf: Unicode translation failed\n"); 5246565282cSScott Long uma_zfree(udf_zone_trans, unibuf); 5256565282cSScott Long return 0; 5266565282cSScott Long } 5276565282cSScott Long 5286565282cSScott Long while (unilen > 0 && destleft > 0) { 52938fc0aa4SDimitry Andric udf_iconv->conv(udfmp->im_d2l, __DECONST(const char **, 53038fc0aa4SDimitry Andric &unibuf), (size_t *)&unilen, (char **)&destname, 53138fc0aa4SDimitry Andric &destleft); 5326565282cSScott Long /* Unconverted character found */ 5336565282cSScott Long if (unilen > 0 && destleft > 0) { 5346565282cSScott Long *destname++ = '?'; 5356565282cSScott Long destleft--; 5366565282cSScott Long unibuf += 2; 5376565282cSScott Long unilen -= 2; 5386565282cSScott Long } 5396565282cSScott Long } 5406565282cSScott Long uma_zfree(udf_zone_trans, unip); 5416565282cSScott Long *destname = '\0'; 5426565282cSScott Long destlen = MAXNAMLEN - (int)destleft; 5436565282cSScott Long } else { 54451a7b740SScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */ 545a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK); 54651a7b740SScott Long 547181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) { 54851a7b740SScott Long printf("udf: Unicode translation failed\n"); 54951a7b740SScott Long uma_zfree(udf_zone_trans, transname); 55051a7b740SScott Long return 0; 55151a7b740SScott Long } 55251a7b740SScott Long 55351a7b740SScott Long for (i = 0; i < unilen ; i++) { 55451a7b740SScott Long if (transname[i] & 0xff00) { 55551a7b740SScott Long destname[i] = '.'; /* Fudge the 16bit chars */ 55651a7b740SScott Long } else { 55751a7b740SScott Long destname[i] = transname[i] & 0xff; 55851a7b740SScott Long } 55951a7b740SScott Long } 56051a7b740SScott Long uma_zfree(udf_zone_trans, transname); 5616565282cSScott Long destname[unilen] = 0; 562181fc3c6SR. Imura destlen = (int)unilen; 5636565282cSScott Long } 56451a7b740SScott Long 5656565282cSScott Long return (destlen); 56651a7b740SScott Long } 56751a7b740SScott Long 56851a7b740SScott Long /* 56951a7b740SScott Long * Compare a CS0 dstring with a name passed in from the VFS layer. Return 570befb7f33SChristian Brueffer * 0 on a successful match, nonzero otherwise. Unicode work may need to be done 57151a7b740SScott Long * here also. 57251a7b740SScott Long */ 57351a7b740SScott Long static int 5746565282cSScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp) 57551a7b740SScott Long { 57695ec5961SScott Long char *transname; 57795ec5961SScott Long int error = 0; 57851a7b740SScott Long 57995ec5961SScott Long /* This is overkill, but not worth creating a new zone */ 580a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK); 58195ec5961SScott Long 5826565282cSScott Long cs0len = udf_transname(cs0string, transname, cs0len, udfmp); 58351a7b740SScott Long 58451a7b740SScott Long /* Easy check. If they aren't the same length, they aren't equal */ 58595ec5961SScott Long if ((cs0len == 0) || (cs0len != cmplen)) 58695ec5961SScott Long error = -1; 58795ec5961SScott Long else 58895ec5961SScott Long error = bcmp(transname, cmpname, cmplen); 58951a7b740SScott Long 59095ec5961SScott Long uma_zfree(udf_zone_trans, transname); 59195ec5961SScott Long return (error); 59251a7b740SScott Long } 59351a7b740SScott Long 59451a7b740SScott Long struct udf_uiodir { 59551a7b740SScott Long struct dirent *dirent; 59651a7b740SScott Long u_long *cookies; 59751a7b740SScott Long int ncookies; 59851a7b740SScott Long int acookies; 59951a7b740SScott Long int eofflag; 60051a7b740SScott Long }; 60151a7b740SScott Long 60251a7b740SScott Long static int 60351a7b740SScott Long udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie) 60451a7b740SScott Long { 60551a7b740SScott Long if (uiodir->cookies != NULL) { 60651a7b740SScott Long if (++uiodir->acookies > uiodir->ncookies) { 60751a7b740SScott Long uiodir->eofflag = 0; 60851a7b740SScott Long return (-1); 60951a7b740SScott Long } 61051a7b740SScott Long *uiodir->cookies++ = cookie; 61151a7b740SScott Long } 61251a7b740SScott Long 61351a7b740SScott Long if (uio->uio_resid < de_size) { 61451a7b740SScott Long uiodir->eofflag = 0; 61551a7b740SScott Long return (-1); 61651a7b740SScott Long } 61751a7b740SScott Long 618c9524588SDag-Erling Smørgrav return (uiomove(uiodir->dirent, de_size, uio)); 61951a7b740SScott Long } 62051a7b740SScott Long 6211703656aSScott Long static struct udf_dirstream * 6221703656aSScott Long udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp) 6231703656aSScott Long { 6241703656aSScott Long struct udf_dirstream *ds; 6251703656aSScott Long 626a163d034SWarner Losh ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO); 6271703656aSScott Long 6281703656aSScott Long ds->node = node; 6291703656aSScott Long ds->offset = offset; 6301703656aSScott Long ds->udfmp = udfmp; 6311703656aSScott Long ds->fsize = fsize; 6321703656aSScott Long 6331703656aSScott Long return (ds); 6341703656aSScott Long } 6351703656aSScott Long 6361703656aSScott Long static struct fileid_desc * 6371703656aSScott Long udf_getfid(struct udf_dirstream *ds) 6381703656aSScott Long { 6391703656aSScott Long struct fileid_desc *fid; 6401703656aSScott Long int error, frag_size = 0, total_fid_size; 6411703656aSScott Long 6421703656aSScott Long /* End of directory? */ 6431703656aSScott Long if (ds->offset + ds->off >= ds->fsize) { 6441703656aSScott Long ds->error = 0; 6451703656aSScott Long return (NULL); 6461703656aSScott Long } 6471703656aSScott Long 6481703656aSScott Long /* Grab the first extent of the directory */ 6491703656aSScott Long if (ds->off == 0) { 6501703656aSScott Long ds->size = 0; 6511703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset, 6521703656aSScott Long &ds->bp, &ds->data); 6531703656aSScott Long if (error) { 6541703656aSScott Long ds->error = error; 655744bb56dSScott Long if (ds->bp != NULL) 656744bb56dSScott Long brelse(ds->bp); 6571703656aSScott Long return (NULL); 6581703656aSScott Long } 6591703656aSScott Long } 6601703656aSScott Long 6614576293dSScott Long /* 6624576293dSScott Long * Clean up from a previous fragmented FID. 6634576293dSScott Long * XXX Is this the right place for this? 6644576293dSScott Long */ 6651703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) { 6661703656aSScott Long ds->fid_fragment = 0; 6671ede983cSDag-Erling Smørgrav free(ds->buf, M_UDFFID); 6681703656aSScott Long } 6691703656aSScott Long 6701703656aSScott Long fid = (struct fileid_desc*)&ds->data[ds->off]; 6711703656aSScott Long 6721703656aSScott Long /* 6731703656aSScott Long * Check to see if the fid is fragmented. The first test 6741703656aSScott Long * ensures that we don't wander off the end of the buffer 6751703656aSScott Long * looking for the l_iu and l_fi fields. 6761703656aSScott Long */ 6771703656aSScott Long if (ds->off + UDF_FID_SIZE > ds->size || 678bf1c3dddSScott Long ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){ 6791703656aSScott Long 6801703656aSScott Long /* Copy what we have of the fid into a buffer */ 6811703656aSScott Long frag_size = ds->size - ds->off; 6821703656aSScott Long if (frag_size >= ds->udfmp->bsize) { 6831703656aSScott Long printf("udf: invalid FID fragment\n"); 6841703656aSScott Long ds->error = EINVAL; 6851703656aSScott Long return (NULL); 6861703656aSScott Long } 6871703656aSScott Long 6881703656aSScott Long /* 6891703656aSScott Long * File ID descriptors can only be at most one 6901703656aSScott Long * logical sector in size. 6911703656aSScott Long */ 6921ede983cSDag-Erling Smørgrav ds->buf = malloc(ds->udfmp->bsize, M_UDFFID, 693a163d034SWarner Losh M_WAITOK | M_ZERO); 6941703656aSScott Long bcopy(fid, ds->buf, frag_size); 6951703656aSScott Long 6961703656aSScott Long /* Reduce all of the casting magic */ 6971703656aSScott Long fid = (struct fileid_desc*)ds->buf; 6981703656aSScott Long 6991703656aSScott Long if (ds->bp != NULL) 7001703656aSScott Long brelse(ds->bp); 7011703656aSScott Long 7021703656aSScott Long /* Fetch the next allocation */ 7031703656aSScott Long ds->offset += ds->size; 7041703656aSScott Long ds->size = 0; 7051703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset, 7061703656aSScott Long &ds->bp, &ds->data); 7071703656aSScott Long if (error) { 7081703656aSScott Long ds->error = error; 7091703656aSScott Long return (NULL); 7101703656aSScott Long } 7111703656aSScott Long 7121703656aSScott Long /* 7131703656aSScott Long * If the fragment was so small that we didn't get 7141703656aSScott Long * the l_iu and l_fi fields, copy those in. 7151703656aSScott Long */ 7161703656aSScott Long if (frag_size < UDF_FID_SIZE) 7171703656aSScott Long bcopy(ds->data, &ds->buf[frag_size], 7181703656aSScott Long UDF_FID_SIZE - frag_size); 7191703656aSScott Long 7201703656aSScott Long /* 7211703656aSScott Long * Now that we have enough of the fid to work with, 7221703656aSScott Long * copy in the rest of the fid from the new 7231703656aSScott Long * allocation. 7241703656aSScott Long */ 725bf1c3dddSScott Long total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi; 7261703656aSScott Long if (total_fid_size > ds->udfmp->bsize) { 7271703656aSScott Long printf("udf: invalid FID\n"); 7281703656aSScott Long ds->error = EIO; 7291703656aSScott Long return (NULL); 7301703656aSScott Long } 7311703656aSScott Long bcopy(ds->data, &ds->buf[frag_size], 7321703656aSScott Long total_fid_size - frag_size); 7331703656aSScott Long 7341703656aSScott Long ds->fid_fragment = 1; 7351703656aSScott Long } else { 736bf1c3dddSScott Long total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE; 7371703656aSScott Long } 7381703656aSScott Long 7391703656aSScott Long /* 7401703656aSScott Long * Update the offset. Align on a 4 byte boundary because the 7414576293dSScott Long * UDF spec says so. 7421703656aSScott Long */ 743159da68bSAndriy Gapon ds->this_off = ds->offset + ds->off; 7441703656aSScott Long if (!ds->fid_fragment) { 7451703656aSScott Long ds->off += (total_fid_size + 3) & ~0x03; 7461703656aSScott Long } else { 7471703656aSScott Long ds->off = (total_fid_size - frag_size + 3) & ~0x03; 7481703656aSScott Long } 7491703656aSScott Long 7501703656aSScott Long return (fid); 7511703656aSScott Long } 7521703656aSScott Long 7531703656aSScott Long static void 7541703656aSScott Long udf_closedir(struct udf_dirstream *ds) 7551703656aSScott Long { 7561703656aSScott Long 7571703656aSScott Long if (ds->bp != NULL) 7581703656aSScott Long brelse(ds->bp); 7591703656aSScott Long 7601703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) 7611ede983cSDag-Erling Smørgrav free(ds->buf, M_UDFFID); 7621703656aSScott Long 7631703656aSScott Long uma_zfree(udf_zone_ds, ds); 7641703656aSScott Long } 7651703656aSScott Long 76651a7b740SScott Long static int 76751a7b740SScott Long udf_readdir(struct vop_readdir_args *a) 76851a7b740SScott Long { 76951a7b740SScott Long struct vnode *vp; 77051a7b740SScott Long struct uio *uio; 77151a7b740SScott Long struct dirent dir; 77251a7b740SScott Long struct udf_node *node; 7736565282cSScott Long struct udf_mnt *udfmp; 77451a7b740SScott Long struct fileid_desc *fid; 77551a7b740SScott Long struct udf_uiodir uiodir; 7761703656aSScott Long struct udf_dirstream *ds; 77751a7b740SScott Long u_long *cookies = NULL; 77851a7b740SScott Long int ncookies; 779c8eeea2fSScott Long int error = 0; 78051a7b740SScott Long 78151a7b740SScott Long vp = a->a_vp; 78251a7b740SScott Long uio = a->a_uio; 78351a7b740SScott Long node = VTON(vp); 7846565282cSScott Long udfmp = node->udfmp; 78551a7b740SScott Long uiodir.eofflag = 1; 78651a7b740SScott Long 78751a7b740SScott Long if (a->a_ncookies != NULL) { 78851a7b740SScott Long /* 78951a7b740SScott Long * Guess how many entries are needed. If we run out, this 79051a7b740SScott Long * function will be called again and thing will pick up were 79151a7b740SScott Long * it left off. 79251a7b740SScott Long */ 79351a7b740SScott Long ncookies = uio->uio_resid / 8; 7941ede983cSDag-Erling Smørgrav cookies = malloc(sizeof(u_long) * ncookies, 795a163d034SWarner Losh M_TEMP, M_WAITOK); 79651a7b740SScott Long if (cookies == NULL) 79751a7b740SScott Long return (ENOMEM); 79851a7b740SScott Long uiodir.ncookies = ncookies; 79951a7b740SScott Long uiodir.cookies = cookies; 80051a7b740SScott Long uiodir.acookies = 0; 80151a7b740SScott Long } else { 80251a7b740SScott Long uiodir.cookies = NULL; 80351a7b740SScott Long } 80451a7b740SScott Long 80551a7b740SScott Long /* 80651a7b740SScott Long * Iterate through the file id descriptors. Give the parent dir 8074576293dSScott Long * entry special attention. 80851a7b740SScott Long */ 809bf1c3dddSScott Long ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len), 8101703656aSScott Long node->udfmp); 81151a7b740SScott Long 8121703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) { 81351a7b740SScott Long 81451a7b740SScott Long /* XXX Should we return an error on a bad fid? */ 81551a7b740SScott Long if (udf_checktag(&fid->tag, TAGID_FID)) { 81651a7b740SScott Long printf("Invalid FID tag\n"); 81777411499SScott Long hexdump(fid, UDF_FID_SIZE, NULL, 0); 8181703656aSScott Long error = EIO; 81951a7b740SScott Long break; 82051a7b740SScott Long } 82151a7b740SScott Long 82251a7b740SScott Long /* Is this a deleted file? */ 8232bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL) 8241703656aSScott Long continue; 82551a7b740SScott Long 8262bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 82751a7b740SScott Long /* Do up the '.' and '..' entries. Dummy values are 82851a7b740SScott Long * used for the cookies since the offset here is 82951a7b740SScott Long * usually zero, and NFS doesn't like that value 83051a7b740SScott Long */ 831c8eeea2fSScott Long dir.d_fileno = node->hash_id; 832c8eeea2fSScott Long dir.d_type = DT_DIR; 833c8eeea2fSScott Long dir.d_name[0] = '.'; 834444acc16SScott Long dir.d_name[1] = '\0'; 835c8eeea2fSScott Long dir.d_namlen = 1; 836c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 837c8eeea2fSScott Long uiodir.dirent = &dir; 838c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1); 83951a7b740SScott Long if (error) 84051a7b740SScott Long break; 84151a7b740SScott Long 842c8eeea2fSScott Long dir.d_fileno = udf_getid(&fid->icb); 843c8eeea2fSScott Long dir.d_type = DT_DIR; 844c8eeea2fSScott Long dir.d_name[0] = '.'; 845c8eeea2fSScott Long dir.d_name[1] = '.'; 846444acc16SScott Long dir.d_name[2] = '\0'; 847c8eeea2fSScott Long dir.d_namlen = 2; 848c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 849c8eeea2fSScott Long uiodir.dirent = &dir; 850c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2); 85151a7b740SScott Long } else { 85251a7b740SScott Long dir.d_namlen = udf_transname(&fid->data[fid->l_iu], 8536565282cSScott Long &dir.d_name[0], fid->l_fi, udfmp); 85451a7b740SScott Long dir.d_fileno = udf_getid(&fid->icb); 8552bbe0d36SScott Long dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ? 8562bbe0d36SScott Long DT_DIR : DT_UNKNOWN; 85751a7b740SScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 85851a7b740SScott Long uiodir.dirent = &dir; 8591703656aSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 8601703656aSScott Long ds->this_off); 86151a7b740SScott Long } 86284206c74SAndriy Gapon if (error) 86351a7b740SScott Long break; 864ff9e355bSAndriy Gapon uio->uio_offset = ds->offset + ds->off; 86551a7b740SScott Long } 86651a7b740SScott Long 86751a7b740SScott Long /* tell the calling layer whether we need to be called again */ 86851a7b740SScott Long *a->a_eofflag = uiodir.eofflag; 86951a7b740SScott Long 87084206c74SAndriy Gapon if (error < 0) 87184206c74SAndriy Gapon error = 0; 8721703656aSScott Long if (!error) 8731703656aSScott Long error = ds->error; 8741703656aSScott Long 8751703656aSScott Long udf_closedir(ds); 87651a7b740SScott Long 87751a7b740SScott Long if (a->a_ncookies != NULL) { 87851a7b740SScott Long if (error) 8791ede983cSDag-Erling Smørgrav free(cookies, M_TEMP); 88051a7b740SScott Long else { 88151a7b740SScott Long *a->a_ncookies = uiodir.acookies; 88251a7b740SScott Long *a->a_cookies = cookies; 88351a7b740SScott Long } 88451a7b740SScott Long } 88551a7b740SScott Long 88651a7b740SScott Long return (error); 88751a7b740SScott Long } 88851a7b740SScott Long 88951a7b740SScott Long static int 89051a7b740SScott Long udf_readlink(struct vop_readlink_args *ap) 89151a7b740SScott Long { 892e3024df2SJohn Baldwin struct path_component *pc, *end; 893e3024df2SJohn Baldwin struct vnode *vp; 894e3024df2SJohn Baldwin struct uio uio; 895e3024df2SJohn Baldwin struct iovec iov[1]; 896e3024df2SJohn Baldwin struct udf_node *node; 897e3024df2SJohn Baldwin void *buf; 898e3024df2SJohn Baldwin char *cp; 899e3024df2SJohn Baldwin int error, len, root; 900e3024df2SJohn Baldwin 901e3024df2SJohn Baldwin /* 902e3024df2SJohn Baldwin * A symbolic link in UDF is a list of variable-length path 903e3024df2SJohn Baldwin * component structures. We build a pathname in the caller's 904e3024df2SJohn Baldwin * uio by traversing this list. 905e3024df2SJohn Baldwin */ 906e3024df2SJohn Baldwin vp = ap->a_vp; 907e3024df2SJohn Baldwin node = VTON(vp); 908e3024df2SJohn Baldwin len = le64toh(node->fentry->inf_len); 90912b3a08dSAndriy Gapon buf = malloc(len, M_DEVBUF, M_WAITOK); 9106b3ee248SAndriy Gapon iov[0].iov_len = len; 911e3024df2SJohn Baldwin iov[0].iov_base = buf; 912e3024df2SJohn Baldwin uio.uio_iov = iov; 913e3024df2SJohn Baldwin uio.uio_iovcnt = 1; 914e3024df2SJohn Baldwin uio.uio_offset = 0; 915e3024df2SJohn Baldwin uio.uio_resid = iov[0].iov_len; 916e3024df2SJohn Baldwin uio.uio_segflg = UIO_SYSSPACE; 917e3024df2SJohn Baldwin uio.uio_rw = UIO_READ; 918e3024df2SJohn Baldwin uio.uio_td = curthread; 919e3024df2SJohn Baldwin error = VOP_READ(vp, &uio, 0, ap->a_cred); 920e3024df2SJohn Baldwin if (error) 921e3024df2SJohn Baldwin goto error; 922e3024df2SJohn Baldwin 923e3024df2SJohn Baldwin pc = buf; 924e3024df2SJohn Baldwin end = (void *)((char *)buf + len); 925e3024df2SJohn Baldwin root = 0; 926e3024df2SJohn Baldwin while (pc < end) { 927e3024df2SJohn Baldwin switch (pc->type) { 928e3024df2SJohn Baldwin case UDF_PATH_ROOT: 929e3024df2SJohn Baldwin /* Only allow this at the beginning of a path. */ 930e3024df2SJohn Baldwin if ((void *)pc != buf) { 931e3024df2SJohn Baldwin error = EINVAL; 932e3024df2SJohn Baldwin goto error; 933e3024df2SJohn Baldwin } 934e3024df2SJohn Baldwin cp = "/"; 935e3024df2SJohn Baldwin len = 1; 936e3024df2SJohn Baldwin root = 1; 937e3024df2SJohn Baldwin break; 938e3024df2SJohn Baldwin case UDF_PATH_DOT: 939e3024df2SJohn Baldwin cp = "."; 940e3024df2SJohn Baldwin len = 1; 941e3024df2SJohn Baldwin break; 942e3024df2SJohn Baldwin case UDF_PATH_DOTDOT: 943e3024df2SJohn Baldwin cp = ".."; 944e3024df2SJohn Baldwin len = 2; 945e3024df2SJohn Baldwin break; 946e3024df2SJohn Baldwin case UDF_PATH_PATH: 947e3024df2SJohn Baldwin if (pc->length == 0) { 948e3024df2SJohn Baldwin error = EINVAL; 949e3024df2SJohn Baldwin goto error; 950e3024df2SJohn Baldwin } 951e3024df2SJohn Baldwin /* 952e3024df2SJohn Baldwin * XXX: We only support CS8 which appears to map 953e3024df2SJohn Baldwin * to ASCII directly. 954e3024df2SJohn Baldwin */ 955e3024df2SJohn Baldwin switch (pc->identifier[0]) { 956e3024df2SJohn Baldwin case 8: 957e3024df2SJohn Baldwin cp = pc->identifier + 1; 958e3024df2SJohn Baldwin len = pc->length - 1; 959e3024df2SJohn Baldwin break; 960e3024df2SJohn Baldwin default: 961e3024df2SJohn Baldwin error = EOPNOTSUPP; 962e3024df2SJohn Baldwin goto error; 963e3024df2SJohn Baldwin } 964e3024df2SJohn Baldwin break; 965e3024df2SJohn Baldwin default: 966e3024df2SJohn Baldwin error = EINVAL; 967e3024df2SJohn Baldwin goto error; 968e3024df2SJohn Baldwin } 969e3024df2SJohn Baldwin 970e3024df2SJohn Baldwin /* 971e3024df2SJohn Baldwin * If this is not the first component, insert a path 972e3024df2SJohn Baldwin * separator. 973e3024df2SJohn Baldwin */ 974e3024df2SJohn Baldwin if (pc != buf) { 975e3024df2SJohn Baldwin /* If we started with root we already have a "/". */ 976e3024df2SJohn Baldwin if (root) 977e3024df2SJohn Baldwin goto skipslash; 978e3024df2SJohn Baldwin root = 0; 979e3024df2SJohn Baldwin if (ap->a_uio->uio_resid < 1) { 980e3024df2SJohn Baldwin error = ENAMETOOLONG; 981e3024df2SJohn Baldwin goto error; 982e3024df2SJohn Baldwin } 983e3024df2SJohn Baldwin error = uiomove("/", 1, ap->a_uio); 984e3024df2SJohn Baldwin if (error) 985e3024df2SJohn Baldwin break; 986e3024df2SJohn Baldwin } 987e3024df2SJohn Baldwin skipslash: 988e3024df2SJohn Baldwin 989e3024df2SJohn Baldwin /* Append string at 'cp' of length 'len' to our path. */ 990e3024df2SJohn Baldwin if (len > ap->a_uio->uio_resid) { 991e3024df2SJohn Baldwin error = ENAMETOOLONG; 992e3024df2SJohn Baldwin goto error; 993e3024df2SJohn Baldwin } 994e3024df2SJohn Baldwin error = uiomove(cp, len, ap->a_uio); 995e3024df2SJohn Baldwin if (error) 996e3024df2SJohn Baldwin break; 997e3024df2SJohn Baldwin 998e3024df2SJohn Baldwin /* Advance to next component. */ 999e3024df2SJohn Baldwin pc = (void *)((char *)pc + 4 + pc->length); 1000e3024df2SJohn Baldwin } 1001e3024df2SJohn Baldwin error: 1002e3024df2SJohn Baldwin free(buf, M_DEVBUF); 1003e3024df2SJohn Baldwin return (error); 100451a7b740SScott Long } 100551a7b740SScott Long 100651a7b740SScott Long static int 100751a7b740SScott Long udf_strategy(struct vop_strategy_args *a) 100851a7b740SScott Long { 100951a7b740SScott Long struct buf *bp; 101051a7b740SScott Long struct vnode *vp; 101151a7b740SScott Long struct udf_node *node; 1012429c018aSPoul-Henning Kamp struct bufobj *bo; 10135792e04dSAndriy Gapon off_t offset; 10145792e04dSAndriy Gapon uint32_t maxsize; 10155792e04dSAndriy Gapon daddr_t sector; 10165792e04dSAndriy Gapon int error; 101751a7b740SScott Long 101851a7b740SScott Long bp = a->a_bp; 1019d83b7498SPoul-Henning Kamp vp = a->a_vp; 102051a7b740SScott Long node = VTON(vp); 102151a7b740SScott Long 10220c09ac0dSPav Lucistnik if (bp->b_blkno == bp->b_lblkno) { 10235792e04dSAndriy Gapon offset = lblktosize(node->udfmp, bp->b_lblkno); 10245792e04dSAndriy Gapon error = udf_bmap_internal(node, offset, §or, &maxsize); 10255792e04dSAndriy Gapon if (error) { 102651a7b740SScott Long clrbuf(bp); 102751a7b740SScott Long bp->b_blkno = -1; 102851a7b740SScott Long bufdone(bp); 102951a7b740SScott Long return (0); 103051a7b740SScott Long } 10315792e04dSAndriy Gapon /* bmap gives sector numbers, bio works with device blocks */ 10325792e04dSAndriy Gapon bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT); 10335792e04dSAndriy Gapon } 1034429c018aSPoul-Henning Kamp bo = node->udfmp->im_bo; 10352c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 10360391e5a1SPoul-Henning Kamp BO_STRATEGY(bo, bp); 103751a7b740SScott Long return (0); 103851a7b740SScott Long } 103951a7b740SScott Long 104051a7b740SScott Long static int 104151a7b740SScott Long udf_bmap(struct vop_bmap_args *a) 104251a7b740SScott Long { 104351a7b740SScott Long struct udf_node *node; 1044c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size; 104598b0c789SPoul-Henning Kamp daddr_t lsector; 1046fb2a76ccSAndriy Gapon int nblk; 104751a7b740SScott Long int error; 104851a7b740SScott Long 104951a7b740SScott Long node = VTON(a->a_vp); 105051a7b740SScott Long 10519c83534dSPoul-Henning Kamp if (a->a_bop != NULL) 1052e0251bbbSPoul-Henning Kamp *a->a_bop = &node->udfmp->im_devvp->v_bufobj; 105351a7b740SScott Long if (a->a_bnp == NULL) 105451a7b740SScott Long return (0); 105551a7b740SScott Long if (a->a_runb) 105651a7b740SScott Long *a->a_runb = 0; 105751a7b740SScott Long 105882467096SAndriy Gapon /* 105982467096SAndriy Gapon * UDF_INVALID_BMAP means data embedded into fentry, this is an internal 106082467096SAndriy Gapon * error that should not be propagated to calling code. 106182467096SAndriy Gapon * Most obvious mapping for this error is EOPNOTSUPP as we can not truly 106282467096SAndriy Gapon * translate block numbers in this case. 106382467096SAndriy Gapon * Incidentally, this return code will make vnode pager to use VOP_READ 106482467096SAndriy Gapon * to get data for mmap-ed pages and udf_read knows how to do the right 106582467096SAndriy Gapon * thing for this kind of files. 106682467096SAndriy Gapon */ 106782467096SAndriy Gapon error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift, 106882467096SAndriy Gapon &lsector, &max_size); 106982467096SAndriy Gapon if (error == UDF_INVALID_BMAP) 107082467096SAndriy Gapon return (EOPNOTSUPP); 10718db4c2f2SScott Long if (error) 107251a7b740SScott Long return (error); 107351a7b740SScott Long 1074cd1b1a1dSScott Long /* Translate logical to physical sector number */ 1075cd1b1a1dSScott Long *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT); 1076cd1b1a1dSScott Long 1077fb2a76ccSAndriy Gapon /* 1078fb2a76ccSAndriy Gapon * Determine maximum number of readahead blocks following the 1079fb2a76ccSAndriy Gapon * requested block. 1080fb2a76ccSAndriy Gapon */ 1081fb2a76ccSAndriy Gapon if (a->a_runp) { 1082fb2a76ccSAndriy Gapon nblk = (max_size >> node->udfmp->bshift) - 1; 1083fb2a76ccSAndriy Gapon if (nblk <= 0) 108451a7b740SScott Long *a->a_runp = 0; 1085fb2a76ccSAndriy Gapon else if (nblk >= (MAXBSIZE >> node->udfmp->bshift)) 1086fb2a76ccSAndriy Gapon *a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1; 1087fb2a76ccSAndriy Gapon else 1088fb2a76ccSAndriy Gapon *a->a_runp = nblk; 1089fb2a76ccSAndriy Gapon } 1090fb2a76ccSAndriy Gapon 1091fb2a76ccSAndriy Gapon if (a->a_runb) { 1092fb2a76ccSAndriy Gapon *a->a_runb = 0; 1093fb2a76ccSAndriy Gapon } 109451a7b740SScott Long 109551a7b740SScott Long return (0); 109651a7b740SScott Long } 109751a7b740SScott Long 109851a7b740SScott Long /* 109951a7b740SScott Long * The all powerful VOP_LOOKUP(). 110051a7b740SScott Long */ 110151a7b740SScott Long static int 110251a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a) 110351a7b740SScott Long { 110451a7b740SScott Long struct vnode *dvp; 110551a7b740SScott Long struct vnode *tdp = NULL; 110651a7b740SScott Long struct vnode **vpp = a->a_vpp; 110751a7b740SScott Long struct udf_node *node; 110851a7b740SScott Long struct udf_mnt *udfmp; 110951a7b740SScott Long struct fileid_desc *fid = NULL; 11101703656aSScott Long struct udf_dirstream *ds; 111151a7b740SScott Long u_long nameiop; 111251a7b740SScott Long u_long flags; 111351a7b740SScott Long char *nameptr; 111451a7b740SScott Long long namelen; 111551a7b740SScott Long ino_t id = 0; 11161703656aSScott Long int offset, error = 0; 11174ad0d60bSJohn Baldwin int fsize, lkflags, ltype, numdirpasses; 111851a7b740SScott Long 111951a7b740SScott Long dvp = a->a_dvp; 112051a7b740SScott Long node = VTON(dvp); 112151a7b740SScott Long udfmp = node->udfmp; 112251a7b740SScott Long nameiop = a->a_cnp->cn_nameiop; 112351a7b740SScott Long flags = a->a_cnp->cn_flags; 11244ad0d60bSJohn Baldwin lkflags = a->a_cnp->cn_lkflags; 112551a7b740SScott Long nameptr = a->a_cnp->cn_nameptr; 112651a7b740SScott Long namelen = a->a_cnp->cn_namelen; 1127bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len); 112851a7b740SScott Long 112951a7b740SScott Long /* 113051a7b740SScott Long * If this is a LOOKUP and we've already partially searched through 113151a7b740SScott Long * the directory, pick up where we left off and flag that the 113251a7b740SScott Long * directory may need to be searched twice. For a full description, 11334b12bb04SMaxim Konovalov * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup() 113451a7b740SScott Long */ 11351703656aSScott Long if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) { 113651a7b740SScott Long offset = 0; 113751a7b740SScott Long numdirpasses = 1; 113851a7b740SScott Long } else { 113951a7b740SScott Long offset = node->diroff; 114051a7b740SScott Long numdirpasses = 2; 114151a7b740SScott Long nchstats.ncs_2passes++; 114251a7b740SScott Long } 114351a7b740SScott Long 114451a7b740SScott Long lookloop: 11451703656aSScott Long ds = udf_opendir(node, offset, fsize, udfmp); 114651a7b740SScott Long 11471703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) { 114851a7b740SScott Long 114951a7b740SScott Long /* XXX Should we return an error on a bad fid? */ 11501703656aSScott Long if (udf_checktag(&fid->tag, TAGID_FID)) { 11511703656aSScott Long printf("udf_lookup: Invalid tag\n"); 11521703656aSScott Long error = EIO; 11531703656aSScott Long break; 11541703656aSScott Long } 1155678d5effSScott Long 1156678d5effSScott Long /* Is this a deleted file? */ 11572bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL) 11581703656aSScott Long continue; 115951a7b740SScott Long 11602bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 116151a7b740SScott Long if (flags & ISDOTDOT) { 116251a7b740SScott Long id = udf_getid(&fid->icb); 116351a7b740SScott Long break; 116451a7b740SScott Long } 116551a7b740SScott Long } else { 116651a7b740SScott Long if (!(udf_cmpname(&fid->data[fid->l_iu], 11676565282cSScott Long nameptr, fid->l_fi, namelen, udfmp))) { 116851a7b740SScott Long id = udf_getid(&fid->icb); 116951a7b740SScott Long break; 117051a7b740SScott Long } 117151a7b740SScott Long } 117251a7b740SScott Long } 11731703656aSScott Long 11741703656aSScott Long if (!error) 11751703656aSScott Long error = ds->error; 11761703656aSScott Long 11771703656aSScott Long /* XXX Bail out here? */ 11781703656aSScott Long if (error) { 11791703656aSScott Long udf_closedir(ds); 11801703656aSScott Long return (error); 118151a7b740SScott Long } 118251a7b740SScott Long 118351a7b740SScott Long /* Did we have a match? */ 118451a7b740SScott Long if (id) { 11851703656aSScott Long /* 11861703656aSScott Long * Remember where this entry was if it's the final 11871703656aSScott Long * component. 11881703656aSScott Long */ 118951a7b740SScott Long if ((flags & ISLASTCN) && nameiop == LOOKUP) 11901703656aSScott Long node->diroff = ds->offset + ds->off; 119151a7b740SScott Long if (numdirpasses == 2) 119251a7b740SScott Long nchstats.ncs_pass2++; 11934ad0d60bSJohn Baldwin udf_closedir(ds); 11944ad0d60bSJohn Baldwin 11954ad0d60bSJohn Baldwin if (flags & ISDOTDOT) { 11964ad0d60bSJohn Baldwin error = vn_vget_ino(dvp, id, lkflags, &tdp); 11974ad0d60bSJohn Baldwin } else if (node->hash_id == id) { 11984ad0d60bSJohn Baldwin VREF(dvp); /* we want ourself, ie "." */ 11994ad0d60bSJohn Baldwin /* 12004ad0d60bSJohn Baldwin * When we lookup "." we still can be asked to lock it 12014ad0d60bSJohn Baldwin * differently. 12024ad0d60bSJohn Baldwin */ 12034ad0d60bSJohn Baldwin ltype = lkflags & LK_TYPE_MASK; 12044ad0d60bSJohn Baldwin if (ltype != VOP_ISLOCKED(dvp)) { 12054ad0d60bSJohn Baldwin if (ltype == LK_EXCLUSIVE) 12064ad0d60bSJohn Baldwin vn_lock(dvp, LK_UPGRADE | LK_RETRY); 12074ad0d60bSJohn Baldwin else /* if (ltype == LK_SHARED) */ 12084ad0d60bSJohn Baldwin vn_lock(dvp, LK_DOWNGRADE | LK_RETRY); 12094ad0d60bSJohn Baldwin } 12104ad0d60bSJohn Baldwin tdp = dvp; 12114ad0d60bSJohn Baldwin } else 12124ad0d60bSJohn Baldwin error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp); 12134ad0d60bSJohn Baldwin if (!error) { 121451a7b740SScott Long *vpp = tdp; 121551a7b740SScott Long /* Put this entry in the cache */ 121651a7b740SScott Long if (flags & MAKEENTRY) 121751a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp); 12184585e3acSJeff Roberson } 12191703656aSScott Long } else { 122051a7b740SScott Long /* Name wasn't found on this pass. Do another pass? */ 122151a7b740SScott Long if (numdirpasses == 2) { 122251a7b740SScott Long numdirpasses--; 122351a7b740SScott Long offset = 0; 12241703656aSScott Long udf_closedir(ds); 122551a7b740SScott Long goto lookloop; 122651a7b740SScott Long } 12274ad0d60bSJohn Baldwin udf_closedir(ds); 122851a7b740SScott Long 122951a7b740SScott Long /* Enter name into cache as non-existant */ 123051a7b740SScott Long if (flags & MAKEENTRY) 123151a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp); 123251a7b740SScott Long 12331703656aSScott Long if ((flags & ISLASTCN) && 12341703656aSScott Long (nameiop == CREATE || nameiop == RENAME)) { 12351703656aSScott Long error = EROFS; 12361703656aSScott Long } else { 12371703656aSScott Long error = ENOENT; 12381703656aSScott Long } 12391703656aSScott Long } 124051a7b740SScott Long 12411703656aSScott Long return (error); 124251a7b740SScott Long } 124351a7b740SScott Long 124451a7b740SScott Long static int 124551a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a) 124651a7b740SScott Long { 124751a7b740SScott Long struct vnode *vp; 124851a7b740SScott Long struct udf_node *unode; 124951a7b740SScott Long 125051a7b740SScott Long vp = a->a_vp; 125151a7b740SScott Long unode = VTON(vp); 125251a7b740SScott Long 125392e73f57SAlfred Perlstein /* 125492e73f57SAlfred Perlstein * Destroy the vm object and flush associated pages. 125592e73f57SAlfred Perlstein */ 125692e73f57SAlfred Perlstein vnode_destroy_vobject(vp); 125792e73f57SAlfred Perlstein 125851a7b740SScott Long if (unode != NULL) { 12594e94fafcSPoul-Henning Kamp vfs_hash_remove(vp); 126051a7b740SScott Long 126151a7b740SScott Long if (unode->fentry != NULL) 12621ede983cSDag-Erling Smørgrav free(unode->fentry, M_UDFFENTRY); 126351a7b740SScott Long uma_zfree(udf_zone_node, unode); 126451a7b740SScott Long vp->v_data = NULL; 126551a7b740SScott Long } 126651a7b740SScott Long 126751a7b740SScott Long return (0); 126851a7b740SScott Long } 126951a7b740SScott Long 127010bcafe9SPawel Jakub Dawidek static int 127110bcafe9SPawel Jakub Dawidek udf_vptofh(struct vop_vptofh_args *a) 127210bcafe9SPawel Jakub Dawidek { 127310bcafe9SPawel Jakub Dawidek struct udf_node *node; 127410bcafe9SPawel Jakub Dawidek struct ifid *ifhp; 127510bcafe9SPawel Jakub Dawidek 127610bcafe9SPawel Jakub Dawidek node = VTON(a->a_vp); 127710bcafe9SPawel Jakub Dawidek ifhp = (struct ifid *)a->a_fhp; 127810bcafe9SPawel Jakub Dawidek ifhp->ifid_len = sizeof(struct ifid); 127910bcafe9SPawel Jakub Dawidek ifhp->ifid_ino = node->hash_id; 128010bcafe9SPawel Jakub Dawidek 128110bcafe9SPawel Jakub Dawidek return (0); 128210bcafe9SPawel Jakub Dawidek } 128310bcafe9SPawel Jakub Dawidek 128451a7b740SScott Long /* 128551a7b740SScott Long * Read the block and then set the data pointer to correspond with the 128651a7b740SScott Long * offset passed in. Only read in at most 'size' bytes, and then set 'size' 128751a7b740SScott Long * to the number of bytes pointed to. If 'size' is zero, try to read in a 128851a7b740SScott Long * whole extent. 1289744bb56dSScott Long * 1290744bb56dSScott Long * Note that *bp may be assigned error or not. 1291744bb56dSScott Long * 129251a7b740SScott Long */ 129351a7b740SScott Long static int 12949d32fde8SScott Long udf_readatoffset(struct udf_node *node, int *size, off_t offset, 12959d32fde8SScott Long struct buf **bp, uint8_t **data) 129651a7b740SScott Long { 1297b0c0fb59SAndriy Gapon struct udf_mnt *udfmp = node->udfmp; 1298b0c0fb59SAndriy Gapon struct vnode *vp = node->i_vnode; 1299b0c0fb59SAndriy Gapon struct file_entry *fentry; 130051a7b740SScott Long struct buf *bp1; 1301c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size; 130298b0c789SPoul-Henning Kamp daddr_t sector; 1303b0c0fb59SAndriy Gapon off_t off; 1304b0c0fb59SAndriy Gapon int adj_size; 130551a7b740SScott Long int error; 130651a7b740SScott Long 1307b0c0fb59SAndriy Gapon /* 1308b0c0fb59SAndriy Gapon * This call is made *not* only to detect UDF_INVALID_BMAP case, 1309b0c0fb59SAndriy Gapon * max_size is used as an ad-hoc read-ahead hint for "normal" case. 1310b0c0fb59SAndriy Gapon */ 131151a7b740SScott Long error = udf_bmap_internal(node, offset, §or, &max_size); 13124576293dSScott Long if (error == UDF_INVALID_BMAP) { 131351a7b740SScott Long /* 131451a7b740SScott Long * This error means that the file *data* is stored in the 131551a7b740SScott Long * allocation descriptor field of the file entry. 131651a7b740SScott Long */ 131751a7b740SScott Long fentry = node->fentry; 1318bf1c3dddSScott Long *data = &fentry->data[le32toh(fentry->l_ea)]; 1319bf1c3dddSScott Long *size = le32toh(fentry->l_ad); 1320b2c91b67SAndriy Gapon if (offset >= *size) 1321b2c91b67SAndriy Gapon *size = 0; 1322b2c91b67SAndriy Gapon else { 1323b2c91b67SAndriy Gapon *data += offset; 1324b2c91b67SAndriy Gapon *size -= offset; 1325b2c91b67SAndriy Gapon } 132651a7b740SScott Long return (0); 132751a7b740SScott Long } else if (error != 0) { 132851a7b740SScott Long return (error); 132951a7b740SScott Long } 133051a7b740SScott Long 1331d1def83bSScott Long /* Adjust the size so that it is within range */ 133251a7b740SScott Long if (*size == 0 || *size > max_size) 133351a7b740SScott Long *size = max_size; 133451a7b740SScott Long 1335b0c0fb59SAndriy Gapon /* 1336b0c0fb59SAndriy Gapon * Because we will read starting at block boundary, we need to adjust 1337b0c0fb59SAndriy Gapon * how much we need to read so that all promised data is in. 1338b0c0fb59SAndriy Gapon * Also, we can't promise to read more than MAXBSIZE bytes starting 1339b0c0fb59SAndriy Gapon * from block boundary, so adjust what we promise too. 1340b0c0fb59SAndriy Gapon */ 1341b0c0fb59SAndriy Gapon off = blkoff(udfmp, offset); 1342b0c0fb59SAndriy Gapon *size = min(*size, MAXBSIZE - off); 1343b0c0fb59SAndriy Gapon adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask; 1344b0c0fb59SAndriy Gapon *bp = NULL; 1345b0c0fb59SAndriy Gapon if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) { 13461830bca1SScott Long printf("warning: udf_readlblks returned error %d\n", error); 1347744bb56dSScott Long /* note: *bp may be non-NULL */ 134851a7b740SScott Long return (error); 134951a7b740SScott Long } 135051a7b740SScott Long 135151a7b740SScott Long bp1 = *bp; 13525df29e0cSRemko Lodder *data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask]; 135351a7b740SScott Long return (0); 135451a7b740SScott Long } 135551a7b740SScott Long 135651a7b740SScott Long /* 135751a7b740SScott Long * Translate a file offset into a logical block and then into a physical 135851a7b740SScott Long * block. 13595df29e0cSRemko Lodder * max_size - maximum number of bytes that can be read starting from given 13605df29e0cSRemko Lodder * offset, rather than beginning of calculated sector number 136151a7b740SScott Long */ 136251a7b740SScott Long static int 13639d32fde8SScott Long udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector, 13649d32fde8SScott Long uint32_t *max_size) 136551a7b740SScott Long { 136651a7b740SScott Long struct udf_mnt *udfmp; 136751a7b740SScott Long struct file_entry *fentry; 136851a7b740SScott Long void *icb; 136951a7b740SScott Long struct icb_tag *tag; 1370c2d6947dSJeroen Ruigrok van der Werven uint32_t icblen = 0; 137198b0c789SPoul-Henning Kamp daddr_t lsector; 137251a7b740SScott Long int ad_offset, ad_num = 0; 137351a7b740SScott Long int i, p_offset; 137451a7b740SScott Long 137551a7b740SScott Long udfmp = node->udfmp; 137651a7b740SScott Long fentry = node->fentry; 137751a7b740SScott Long tag = &fentry->icbtag; 137851a7b740SScott Long 1379bf1c3dddSScott Long switch (le16toh(tag->strat_type)) { 138051a7b740SScott Long case 4: 138151a7b740SScott Long break; 138251a7b740SScott Long 138351a7b740SScott Long case 4096: 138451a7b740SScott Long printf("Cannot deal with strategy4096 yet!\n"); 138551a7b740SScott Long return (ENODEV); 138651a7b740SScott Long 138751a7b740SScott Long default: 138851a7b740SScott Long printf("Unknown strategy type %d\n", tag->strat_type); 138951a7b740SScott Long return (ENODEV); 139051a7b740SScott Long } 139151a7b740SScott Long 1392bf1c3dddSScott Long switch (le16toh(tag->flags) & 0x7) { 139351a7b740SScott Long case 0: 139451a7b740SScott Long /* 139551a7b740SScott Long * The allocation descriptor field is filled with short_ad's. 139651a7b740SScott Long * If the offset is beyond the current extent, look for the 139751a7b740SScott Long * next extent. 139851a7b740SScott Long */ 139951a7b740SScott Long do { 140051a7b740SScott Long offset -= icblen; 140151a7b740SScott Long ad_offset = sizeof(struct short_ad) * ad_num; 1402bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) { 140351a7b740SScott Long printf("File offset out of bounds\n"); 140451a7b740SScott Long return (EINVAL); 140551a7b740SScott Long } 1406a4d629e3SScott Long icb = GETICB(short_ad, fentry, 1407bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset); 140851a7b740SScott Long icblen = GETICBLEN(short_ad, icb); 140951a7b740SScott Long ad_num++; 141051a7b740SScott Long } while(offset >= icblen); 141151a7b740SScott Long 141251a7b740SScott Long lsector = (offset >> udfmp->bshift) + 1413937a2387SWill Andrews le32toh(((struct short_ad *)(icb))->pos); 141451a7b740SScott Long 14155df29e0cSRemko Lodder *max_size = icblen - offset; 141651a7b740SScott Long 141751a7b740SScott Long break; 141851a7b740SScott Long case 1: 141951a7b740SScott Long /* 142051a7b740SScott Long * The allocation descriptor field is filled with long_ad's 142151a7b740SScott Long * If the offset is beyond the current extent, look for the 142251a7b740SScott Long * next extent. 142351a7b740SScott Long */ 142451a7b740SScott Long do { 142551a7b740SScott Long offset -= icblen; 142651a7b740SScott Long ad_offset = sizeof(struct long_ad) * ad_num; 1427bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) { 142851a7b740SScott Long printf("File offset out of bounds\n"); 142951a7b740SScott Long return (EINVAL); 143051a7b740SScott Long } 1431bf1c3dddSScott Long icb = GETICB(long_ad, fentry, 1432bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset); 143351a7b740SScott Long icblen = GETICBLEN(long_ad, icb); 143451a7b740SScott Long ad_num++; 143551a7b740SScott Long } while(offset >= icblen); 143651a7b740SScott Long 143751a7b740SScott Long lsector = (offset >> udfmp->bshift) + 1438bf1c3dddSScott Long le32toh(((struct long_ad *)(icb))->loc.lb_num); 143951a7b740SScott Long 14405df29e0cSRemko Lodder *max_size = icblen - offset; 144151a7b740SScott Long 144251a7b740SScott Long break; 144351a7b740SScott Long case 3: 144451a7b740SScott Long /* 144551a7b740SScott Long * This type means that the file *data* is stored in the 144651a7b740SScott Long * allocation descriptor field of the file entry. 144751a7b740SScott Long */ 144851a7b740SScott Long *max_size = 0; 14498db4c2f2SScott Long *sector = node->hash_id + udfmp->part_start; 145051a7b740SScott Long 14514576293dSScott Long return (UDF_INVALID_BMAP); 145251a7b740SScott Long case 2: 145351a7b740SScott Long /* DirectCD does not use extended_ad's */ 145451a7b740SScott Long default: 145551a7b740SScott Long printf("Unsupported allocation descriptor %d\n", 145651a7b740SScott Long tag->flags & 0x7); 145751a7b740SScott Long return (ENODEV); 145851a7b740SScott Long } 145951a7b740SScott Long 146051a7b740SScott Long *sector = lsector + udfmp->part_start; 146151a7b740SScott Long 146251a7b740SScott Long /* 146351a7b740SScott Long * Check the sparing table. Each entry represents the beginning of 146451a7b740SScott Long * a packet. 146551a7b740SScott Long */ 146651a7b740SScott Long if (udfmp->s_table != NULL) { 146751a7b740SScott Long for (i = 0; i< udfmp->s_table_entries; i++) { 1468bf1c3dddSScott Long p_offset = 1469bf1c3dddSScott Long lsector - le32toh(udfmp->s_table->entries[i].org); 147051a7b740SScott Long if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) { 1471bf1c3dddSScott Long *sector = 1472bf1c3dddSScott Long le32toh(udfmp->s_table->entries[i].map) + 147351a7b740SScott Long p_offset; 147451a7b740SScott Long break; 147551a7b740SScott Long } 147651a7b740SScott Long } 147751a7b740SScott Long } 147851a7b740SScott Long 147951a7b740SScott Long return (0); 148051a7b740SScott Long } 1481