151a7b740SScott Long /*- 2d63027b6SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3d63027b6SPedro F. Giffuni * 451a7b740SScott Long * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org> 551a7b740SScott Long * All rights reserved. 651a7b740SScott Long * 751a7b740SScott Long * Redistribution and use in source and binary forms, with or without 851a7b740SScott Long * modification, are permitted provided that the following conditions 951a7b740SScott Long * are met: 1051a7b740SScott Long * 1. Redistributions of source code must retain the above copyright 1151a7b740SScott Long * notice, this list of conditions and the following disclaimer. 1251a7b740SScott Long * 2. Redistributions in binary form must reproduce the above copyright 1351a7b740SScott Long * notice, this list of conditions and the following disclaimer in the 1451a7b740SScott Long * documentation and/or other materials provided with the distribution. 1551a7b740SScott Long * 1651a7b740SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1751a7b740SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1851a7b740SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1951a7b740SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2051a7b740SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2151a7b740SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2251a7b740SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2351a7b740SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2451a7b740SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2551a7b740SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2651a7b740SScott Long * SUCH DAMAGE. 2751a7b740SScott Long * 2851a7b740SScott Long * $FreeBSD$ 2951a7b740SScott Long */ 3051a7b740SScott Long 3151a7b740SScott Long /* udf_vnops.c */ 3251a7b740SScott Long /* Take care of the vnode side of things */ 3351a7b740SScott Long 3451a7b740SScott Long #include <sys/param.h> 3551a7b740SScott Long #include <sys/systm.h> 3651a7b740SScott Long #include <sys/namei.h> 3751a7b740SScott Long #include <sys/kernel.h> 3851a7b740SScott Long #include <sys/malloc.h> 3951a7b740SScott Long #include <sys/stat.h> 4051a7b740SScott Long #include <sys/bio.h> 414e94fafcSPoul-Henning Kamp #include <sys/conf.h> 4251a7b740SScott Long #include <sys/buf.h> 436565282cSScott Long #include <sys/iconv.h> 4451a7b740SScott Long #include <sys/mount.h> 4551a7b740SScott Long #include <sys/vnode.h> 4651a7b740SScott Long #include <sys/dirent.h> 4751a7b740SScott Long #include <sys/queue.h> 4851a7b740SScott Long #include <sys/unistd.h> 4989ec2c3cSScott Long #include <sys/endian.h> 5051a7b740SScott Long 5151a7b740SScott Long #include <vm/uma.h> 5251a7b740SScott Long 5351a7b740SScott Long #include <fs/udf/ecma167-udf.h> 5451a7b740SScott Long #include <fs/udf/osta.h> 5551a7b740SScott Long #include <fs/udf/udf.h> 566565282cSScott Long #include <fs/udf/udf_mount.h> 576565282cSScott Long 586565282cSScott Long extern struct iconv_functions *udf_iconv; 5951a7b740SScott Long 606fde64c7SPoul-Henning Kamp static vop_access_t udf_access; 616fde64c7SPoul-Henning Kamp static vop_getattr_t udf_getattr; 6235e06624SPav Lucistnik static vop_open_t udf_open; 636fde64c7SPoul-Henning Kamp static vop_ioctl_t udf_ioctl; 646fde64c7SPoul-Henning Kamp static vop_pathconf_t udf_pathconf; 6561e69c80SJohn Baldwin static vop_print_t udf_print; 666fde64c7SPoul-Henning Kamp static vop_read_t udf_read; 676fde64c7SPoul-Henning Kamp static vop_readdir_t udf_readdir; 686fde64c7SPoul-Henning Kamp static vop_readlink_t udf_readlink; 6961e69c80SJohn Baldwin static vop_setattr_t udf_setattr; 706fde64c7SPoul-Henning Kamp static vop_strategy_t udf_strategy; 716fde64c7SPoul-Henning Kamp static vop_bmap_t udf_bmap; 726fde64c7SPoul-Henning Kamp static vop_cachedlookup_t udf_lookup; 736fde64c7SPoul-Henning Kamp static vop_reclaim_t udf_reclaim; 7410bcafe9SPawel Jakub Dawidek static vop_vptofh_t udf_vptofh; 759d32fde8SScott Long static int udf_readatoffset(struct udf_node *node, int *size, off_t offset, 769d32fde8SScott Long struct buf **bp, uint8_t **data); 779d32fde8SScott Long static int udf_bmap_internal(struct udf_node *node, off_t offset, 789d32fde8SScott Long daddr_t *sector, uint32_t *max_size); 7951a7b740SScott Long 80aec0fb7bSPoul-Henning Kamp static struct vop_vector udf_vnodeops = { 81aec0fb7bSPoul-Henning Kamp .vop_default = &default_vnodeops, 8283c64397SPoul-Henning Kamp 83aec0fb7bSPoul-Henning Kamp .vop_access = udf_access, 84aec0fb7bSPoul-Henning Kamp .vop_bmap = udf_bmap, 85aec0fb7bSPoul-Henning Kamp .vop_cachedlookup = udf_lookup, 86aec0fb7bSPoul-Henning Kamp .vop_getattr = udf_getattr, 87aec0fb7bSPoul-Henning Kamp .vop_ioctl = udf_ioctl, 88aec0fb7bSPoul-Henning Kamp .vop_lookup = vfs_cache_lookup, 8935e06624SPav Lucistnik .vop_open = udf_open, 90aec0fb7bSPoul-Henning Kamp .vop_pathconf = udf_pathconf, 9161e69c80SJohn Baldwin .vop_print = udf_print, 92aec0fb7bSPoul-Henning Kamp .vop_read = udf_read, 93aec0fb7bSPoul-Henning Kamp .vop_readdir = udf_readdir, 94aec0fb7bSPoul-Henning Kamp .vop_readlink = udf_readlink, 95aec0fb7bSPoul-Henning Kamp .vop_reclaim = udf_reclaim, 9661e69c80SJohn Baldwin .vop_setattr = udf_setattr, 97aec0fb7bSPoul-Henning Kamp .vop_strategy = udf_strategy, 9810bcafe9SPawel Jakub Dawidek .vop_vptofh = udf_vptofh, 9951a7b740SScott Long }; 10051a7b740SScott Long 10161e69c80SJohn Baldwin struct vop_vector udf_fifoops = { 10261e69c80SJohn Baldwin .vop_default = &fifo_specops, 10361e69c80SJohn Baldwin .vop_access = udf_access, 10461e69c80SJohn Baldwin .vop_getattr = udf_getattr, 105b501cc5dSJohn Baldwin .vop_pathconf = udf_pathconf, 10661e69c80SJohn Baldwin .vop_print = udf_print, 10761e69c80SJohn Baldwin .vop_reclaim = udf_reclaim, 10861e69c80SJohn Baldwin .vop_setattr = udf_setattr, 10961e69c80SJohn Baldwin .vop_vptofh = udf_vptofh, 11061e69c80SJohn Baldwin }; 11161e69c80SJohn Baldwin 112d745c852SEd Schouten static MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure"); 113d745c852SEd Schouten static MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure"); 11451a7b740SScott Long 1154576293dSScott Long #define UDF_INVALID_BMAP -1 1168db4c2f2SScott Long 11751a7b740SScott Long int 11851a7b740SScott Long udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td) 11951a7b740SScott Long { 12051a7b740SScott Long int error; 12151a7b740SScott Long struct vnode *vp; 12251a7b740SScott Long 123aec0fb7bSPoul-Henning Kamp error = getnewvnode("udf", mp, &udf_vnodeops, &vp); 12451a7b740SScott Long if (error) { 12551a7b740SScott Long printf("udf_allocv: failed to allocate new vnode\n"); 12651a7b740SScott Long return (error); 12751a7b740SScott Long } 12851a7b740SScott Long 12951a7b740SScott Long *vpp = vp; 13051a7b740SScott Long return (0); 13151a7b740SScott Long } 13251a7b740SScott Long 13351a7b740SScott Long /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */ 13451a7b740SScott Long static mode_t 13551a7b740SScott Long udf_permtomode(struct udf_node *node) 13651a7b740SScott Long { 137c2d6947dSJeroen Ruigrok van der Werven uint32_t perm; 138bf1c3dddSScott Long uint16_t flags; 13951a7b740SScott Long mode_t mode; 14051a7b740SScott Long 141bf1c3dddSScott Long perm = le32toh(node->fentry->perm); 142bf1c3dddSScott Long flags = le16toh(node->fentry->icbtag.flags); 14351a7b740SScott Long 14451a7b740SScott Long mode = perm & UDF_FENTRY_PERM_USER_MASK; 14551a7b740SScott Long mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2); 14651a7b740SScott Long mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4); 14751a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4); 14851a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6); 14951a7b740SScott Long mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8); 15051a7b740SScott Long 15151a7b740SScott Long return (mode); 15251a7b740SScott Long } 15351a7b740SScott Long 15451a7b740SScott Long static int 15551a7b740SScott Long udf_access(struct vop_access_args *a) 15651a7b740SScott Long { 15751a7b740SScott Long struct vnode *vp; 15851a7b740SScott Long struct udf_node *node; 15915bc6b2bSEdward Tomasz Napierala accmode_t accmode; 16015bc6b2bSEdward Tomasz Napierala mode_t mode; 16151a7b740SScott Long 16251a7b740SScott Long vp = a->a_vp; 16351a7b740SScott Long node = VTON(vp); 16415bc6b2bSEdward Tomasz Napierala accmode = a->a_accmode; 16551a7b740SScott Long 16615bc6b2bSEdward Tomasz Napierala if (accmode & VWRITE) { 16751a7b740SScott Long switch (vp->v_type) { 16851a7b740SScott Long case VDIR: 16951a7b740SScott Long case VLNK: 17051a7b740SScott Long case VREG: 17151a7b740SScott Long return (EROFS); 17251a7b740SScott Long /* NOT REACHED */ 17351a7b740SScott Long default: 17451a7b740SScott Long break; 17551a7b740SScott Long } 17651a7b740SScott Long } 17751a7b740SScott Long 17851a7b740SScott Long mode = udf_permtomode(node); 17951a7b740SScott Long 18051a7b740SScott Long return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid, 18115bc6b2bSEdward Tomasz Napierala accmode, a->a_cred, NULL)); 18251a7b740SScott Long } 18351a7b740SScott Long 18435e06624SPav Lucistnik static int 18535e06624SPav Lucistnik udf_open(struct vop_open_args *ap) { 18635e06624SPav Lucistnik struct udf_node *np = VTON(ap->a_vp); 18735e06624SPav Lucistnik off_t fsize; 18835e06624SPav Lucistnik 18935e06624SPav Lucistnik fsize = le64toh(np->fentry->inf_len); 19035e06624SPav Lucistnik vnode_create_vobject(ap->a_vp, fsize, ap->a_td); 19135e06624SPav Lucistnik return 0; 19235e06624SPav Lucistnik } 19335e06624SPav Lucistnik 1949c2bf69dSMarkus Brueffer static const int mon_lens[2][12] = { 1959c2bf69dSMarkus Brueffer {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, 1969c2bf69dSMarkus Brueffer {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} 19751a7b740SScott Long }; 19851a7b740SScott Long 19951a7b740SScott Long static int 20051a7b740SScott Long udf_isaleapyear(int year) 20151a7b740SScott Long { 20251a7b740SScott Long int i; 20351a7b740SScott Long 20451a7b740SScott Long i = (year % 4) ? 0 : 1; 20551a7b740SScott Long i &= (year % 100) ? 1 : 0; 20651a7b740SScott Long i |= (year % 400) ? 0 : 1; 20751a7b740SScott Long 20851a7b740SScott Long return i; 20951a7b740SScott Long } 21051a7b740SScott Long 21151a7b740SScott Long /* 21251a7b740SScott Long * Timezone calculation compliments of Julian Elischer <julian@elischer.org>. 21351a7b740SScott Long */ 21451a7b740SScott Long static void 21551a7b740SScott Long udf_timetotimespec(struct timestamp *time, struct timespec *t) 21651a7b740SScott Long { 2179c2bf69dSMarkus Brueffer int i, lpyear, daysinyear, year, startyear; 21851a7b740SScott Long union { 219c2d6947dSJeroen Ruigrok van der Werven uint16_t u_tz_offset; 22051a7b740SScott Long int16_t s_tz_offset; 22151a7b740SScott Long } tz; 22251a7b740SScott Long 2239c2bf69dSMarkus Brueffer /* 2249c2bf69dSMarkus Brueffer * DirectCD seems to like using bogus year values. 2259c2bf69dSMarkus Brueffer * Don't trust time->month as it will be used for an array index. 2269c2bf69dSMarkus Brueffer */ 227bf1c3dddSScott Long year = le16toh(time->year); 2289c2bf69dSMarkus Brueffer if (year < 1970 || time->month < 1 || time->month > 12) { 22951a7b740SScott Long t->tv_sec = 0; 2309c2bf69dSMarkus Brueffer t->tv_nsec = 0; 23151a7b740SScott Long return; 23251a7b740SScott Long } 23351a7b740SScott Long 23451a7b740SScott Long /* Calculate the time and day */ 23551a7b740SScott Long t->tv_sec = time->second; 23651a7b740SScott Long t->tv_sec += time->minute * 60; 23751a7b740SScott Long t->tv_sec += time->hour * 3600; 2389c2bf69dSMarkus Brueffer t->tv_sec += (time->day - 1) * 3600 * 24; 23951a7b740SScott Long 240befb7f33SChristian Brueffer /* Calculate the month */ 241bf1c3dddSScott Long lpyear = udf_isaleapyear(year); 2429c2bf69dSMarkus Brueffer t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24; 24351a7b740SScott Long 24451a7b740SScott Long /* Speed up the calculation */ 2459c2bf69dSMarkus Brueffer startyear = 1970; 2469c2bf69dSMarkus Brueffer if (year > 2009) { 2479c2bf69dSMarkus Brueffer t->tv_sec += 1262304000; 2489c2bf69dSMarkus Brueffer startyear += 40; 2499c2bf69dSMarkus Brueffer } else if (year > 1999) { 2509c2bf69dSMarkus Brueffer t->tv_sec += 946684800; 2519c2bf69dSMarkus Brueffer startyear += 30; 2529c2bf69dSMarkus Brueffer } else if (year > 1989) { 2539c2bf69dSMarkus Brueffer t->tv_sec += 631152000; 2549c2bf69dSMarkus Brueffer startyear += 20; 2559c2bf69dSMarkus Brueffer } else if (year > 1979) { 25651a7b740SScott Long t->tv_sec += 315532800; 2579c2bf69dSMarkus Brueffer startyear += 10; 25851a7b740SScott Long } 25951a7b740SScott Long 2609c2bf69dSMarkus Brueffer daysinyear = (year - startyear) * 365; 2619c2bf69dSMarkus Brueffer for (i = startyear; i < year; i++) 2629c2bf69dSMarkus Brueffer daysinyear += udf_isaleapyear(i); 2639c2bf69dSMarkus Brueffer t->tv_sec += daysinyear * 3600 * 24; 2649c2bf69dSMarkus Brueffer 2659c2bf69dSMarkus Brueffer /* Calculate microseconds */ 2669c2bf69dSMarkus Brueffer t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 + 2679c2bf69dSMarkus Brueffer time->usec; 2689c2bf69dSMarkus Brueffer 26951a7b740SScott Long /* 27051a7b740SScott Long * Calculate the time zone. The timezone is 12 bit signed 2's 271befb7f33SChristian Brueffer * complement, so we gotta do some extra magic to handle it right. 27251a7b740SScott Long */ 273bf1c3dddSScott Long tz.u_tz_offset = le16toh(time->type_tz); 27451a7b740SScott Long tz.u_tz_offset &= 0x0fff; 27551a7b740SScott Long if (tz.u_tz_offset & 0x0800) 27651a7b740SScott Long tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */ 2779c2bf69dSMarkus Brueffer if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047)) 27851a7b740SScott Long t->tv_sec -= tz.s_tz_offset * 60; 27951a7b740SScott Long 28051a7b740SScott Long return; 28151a7b740SScott Long } 28251a7b740SScott Long 28351a7b740SScott Long static int 28451a7b740SScott Long udf_getattr(struct vop_getattr_args *a) 28551a7b740SScott Long { 28651a7b740SScott Long struct vnode *vp; 28751a7b740SScott Long struct udf_node *node; 28851a7b740SScott Long struct vattr *vap; 28951a7b740SScott Long struct file_entry *fentry; 29051a7b740SScott Long struct timespec ts; 29151a7b740SScott Long 29251a7b740SScott Long ts.tv_sec = 0; 29351a7b740SScott Long 29451a7b740SScott Long vp = a->a_vp; 29551a7b740SScott Long vap = a->a_vap; 29651a7b740SScott Long node = VTON(vp); 29751a7b740SScott Long fentry = node->fentry; 29851a7b740SScott Long 299c049546eSPoul-Henning Kamp vap->va_fsid = dev2udev(node->udfmp->im_dev); 30051a7b740SScott Long vap->va_fileid = node->hash_id; 30151a7b740SScott Long vap->va_mode = udf_permtomode(node); 302bf1c3dddSScott Long vap->va_nlink = le16toh(fentry->link_cnt); 30351a7b740SScott Long /* 30451a7b740SScott Long * XXX The spec says that -1 is valid for uid/gid and indicates an 30551a7b740SScott Long * invalid uid/gid. How should this be represented? 30651a7b740SScott Long */ 307bf1c3dddSScott Long vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid); 308bf1c3dddSScott Long vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid); 30951a7b740SScott Long udf_timetotimespec(&fentry->atime, &vap->va_atime); 31051a7b740SScott Long udf_timetotimespec(&fentry->mtime, &vap->va_mtime); 31151a7b740SScott Long vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */ 3124c5a20e3SKonstantin Belousov vap->va_rdev = NODEV; 31351a7b740SScott Long if (vp->v_type & VDIR) { 31451a7b740SScott Long /* 31551a7b740SScott Long * Directories that are recorded within their ICB will show 31651a7b740SScott Long * as having 0 blocks recorded. Since tradition dictates 31751a7b740SScott Long * that directories consume at least one logical block, 31851a7b740SScott Long * make it appear so. 31951a7b740SScott Long */ 32051a7b740SScott Long if (fentry->logblks_rec != 0) { 321bf1c3dddSScott Long vap->va_size = 322bf1c3dddSScott Long le64toh(fentry->logblks_rec) * node->udfmp->bsize; 32351a7b740SScott Long } else { 32451a7b740SScott Long vap->va_size = node->udfmp->bsize; 32551a7b740SScott Long } 32651a7b740SScott Long } else { 327bf1c3dddSScott Long vap->va_size = le64toh(fentry->inf_len); 32851a7b740SScott Long } 32951a7b740SScott Long vap->va_flags = 0; 33051a7b740SScott Long vap->va_gen = 1; 33151a7b740SScott Long vap->va_blocksize = node->udfmp->bsize; 332bf1c3dddSScott Long vap->va_bytes = le64toh(fentry->inf_len); 33351a7b740SScott Long vap->va_type = vp->v_type; 33451a7b740SScott Long vap->va_filerev = 0; /* XXX */ 33551a7b740SScott Long return (0); 33651a7b740SScott Long } 33751a7b740SScott Long 33861e69c80SJohn Baldwin static int 33961e69c80SJohn Baldwin udf_setattr(struct vop_setattr_args *a) 34061e69c80SJohn Baldwin { 34161e69c80SJohn Baldwin struct vnode *vp; 34261e69c80SJohn Baldwin struct vattr *vap; 34361e69c80SJohn Baldwin 34461e69c80SJohn Baldwin vp = a->a_vp; 34561e69c80SJohn Baldwin vap = a->a_vap; 34661e69c80SJohn Baldwin if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL || 34761e69c80SJohn Baldwin vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL || 34861e69c80SJohn Baldwin vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) 34961e69c80SJohn Baldwin return (EROFS); 35061e69c80SJohn Baldwin if (vap->va_size != (u_quad_t)VNOVAL) { 35161e69c80SJohn Baldwin switch (vp->v_type) { 35261e69c80SJohn Baldwin case VDIR: 35361e69c80SJohn Baldwin return (EISDIR); 35461e69c80SJohn Baldwin case VLNK: 35561e69c80SJohn Baldwin case VREG: 35661e69c80SJohn Baldwin return (EROFS); 35761e69c80SJohn Baldwin case VCHR: 35861e69c80SJohn Baldwin case VBLK: 35961e69c80SJohn Baldwin case VSOCK: 36061e69c80SJohn Baldwin case VFIFO: 36161e69c80SJohn Baldwin case VNON: 36261e69c80SJohn Baldwin case VBAD: 36361e69c80SJohn Baldwin case VMARKER: 36461e69c80SJohn Baldwin return (0); 36561e69c80SJohn Baldwin } 36661e69c80SJohn Baldwin } 36761e69c80SJohn Baldwin return (0); 36861e69c80SJohn Baldwin } 36961e69c80SJohn Baldwin 37051a7b740SScott Long /* 371bf1c3dddSScott Long * File specific ioctls. 37251a7b740SScott Long */ 37351a7b740SScott Long static int 37451a7b740SScott Long udf_ioctl(struct vop_ioctl_args *a) 37551a7b740SScott Long { 376c80a90c5SScott Long printf("%s called\n", __func__); 3771d02d910SPoul-Henning Kamp return (ENOTTY); 37851a7b740SScott Long } 37951a7b740SScott Long 38051a7b740SScott Long /* 38151a7b740SScott Long * I'm not sure that this has much value in a read-only filesystem, but 38251a7b740SScott Long * cd9660 has it too. 38351a7b740SScott Long */ 38451a7b740SScott Long static int 38551a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a) 38651a7b740SScott Long { 38751a7b740SScott Long 38851a7b740SScott Long switch (a->a_name) { 38961d53d8fSJohn Baldwin case _PC_FILESIZEBITS: 39061d53d8fSJohn Baldwin *a->a_retval = 64; 39161d53d8fSJohn Baldwin return (0); 39251a7b740SScott Long case _PC_LINK_MAX: 39351a7b740SScott Long *a->a_retval = 65535; 39451a7b740SScott Long return (0); 39551a7b740SScott Long case _PC_NAME_MAX: 39651a7b740SScott Long *a->a_retval = NAME_MAX; 39751a7b740SScott Long return (0); 39861d53d8fSJohn Baldwin case _PC_SYMLINK_MAX: 39961d53d8fSJohn Baldwin *a->a_retval = MAXPATHLEN; 40051a7b740SScott Long return (0); 40151a7b740SScott Long case _PC_NO_TRUNC: 40251a7b740SScott Long *a->a_retval = 1; 40351a7b740SScott Long return (0); 404b501cc5dSJohn Baldwin case _PC_PIPE_BUF: 405b501cc5dSJohn Baldwin if (a->a_vp->v_type == VDIR || a->a_vp->v_type == VFIFO) { 406b501cc5dSJohn Baldwin *a->a_retval = PIPE_BUF; 407b501cc5dSJohn Baldwin return (0); 408b501cc5dSJohn Baldwin } 409b501cc5dSJohn Baldwin return (EINVAL); 41051a7b740SScott Long default: 41161d53d8fSJohn Baldwin return (vop_stdpathconf(a)); 41251a7b740SScott Long } 41351a7b740SScott Long } 41451a7b740SScott Long 41561e69c80SJohn Baldwin static int 41661e69c80SJohn Baldwin udf_print(struct vop_print_args *ap) 41761e69c80SJohn Baldwin { 41861e69c80SJohn Baldwin struct vnode *vp = ap->a_vp; 41961e69c80SJohn Baldwin struct udf_node *node = VTON(vp); 42061e69c80SJohn Baldwin 42161e69c80SJohn Baldwin printf(" ino %lu, on dev %s", (u_long)node->hash_id, 42261e69c80SJohn Baldwin devtoname(node->udfmp->im_dev)); 42361e69c80SJohn Baldwin if (vp->v_type == VFIFO) 42461e69c80SJohn Baldwin fifo_printinfo(vp); 42561e69c80SJohn Baldwin printf("\n"); 42661e69c80SJohn Baldwin return (0); 42761e69c80SJohn Baldwin } 42861e69c80SJohn Baldwin 4290c09ac0dSPav Lucistnik #define lblkno(udfmp, loc) ((loc) >> (udfmp)->bshift) 4300c09ac0dSPav Lucistnik #define blkoff(udfmp, loc) ((loc) & (udfmp)->bmask) 4315792e04dSAndriy Gapon #define lblktosize(udfmp, blk) ((blk) << (udfmp)->bshift) 43251a7b740SScott Long 433be52a95dSAndriy Gapon static inline int 434be52a95dSAndriy Gapon is_data_in_fentry(const struct udf_node *node) 435be52a95dSAndriy Gapon { 436be52a95dSAndriy Gapon const struct file_entry *fentry = node->fentry; 437be52a95dSAndriy Gapon 438be52a95dSAndriy Gapon return ((le16toh(fentry->icbtag.flags) & 0x7) == 3); 439be52a95dSAndriy Gapon } 440be52a95dSAndriy Gapon 4410c09ac0dSPav Lucistnik static int 4420c09ac0dSPav Lucistnik udf_read(struct vop_read_args *ap) 4430c09ac0dSPav Lucistnik { 4440c09ac0dSPav Lucistnik struct vnode *vp = ap->a_vp; 4450c09ac0dSPav Lucistnik struct uio *uio = ap->a_uio; 4460c09ac0dSPav Lucistnik struct udf_node *node = VTON(vp); 4470c09ac0dSPav Lucistnik struct udf_mnt *udfmp; 448be52a95dSAndriy Gapon struct file_entry *fentry; 4490c09ac0dSPav Lucistnik struct buf *bp; 450be52a95dSAndriy Gapon uint8_t *data; 4510c09ac0dSPav Lucistnik daddr_t lbn, rablock; 4520c09ac0dSPav Lucistnik off_t diff, fsize; 453526d0bd5SKonstantin Belousov ssize_t n; 4540c09ac0dSPav Lucistnik int error = 0; 455526d0bd5SKonstantin Belousov long size, on; 4560c09ac0dSPav Lucistnik 4570c09ac0dSPav Lucistnik if (uio->uio_resid == 0) 4580c09ac0dSPav Lucistnik return (0); 45951a7b740SScott Long if (uio->uio_offset < 0) 46051a7b740SScott Long return (EINVAL); 461be52a95dSAndriy Gapon 462be52a95dSAndriy Gapon if (is_data_in_fentry(node)) { 463be52a95dSAndriy Gapon fentry = node->fentry; 464be52a95dSAndriy Gapon data = &fentry->data[le32toh(fentry->l_ea)]; 465be52a95dSAndriy Gapon fsize = le32toh(fentry->l_ad); 466be52a95dSAndriy Gapon 467be52a95dSAndriy Gapon n = uio->uio_resid; 468be52a95dSAndriy Gapon diff = fsize - uio->uio_offset; 469be52a95dSAndriy Gapon if (diff <= 0) 470be52a95dSAndriy Gapon return (0); 471be52a95dSAndriy Gapon if (diff < n) 472be52a95dSAndriy Gapon n = diff; 473be52a95dSAndriy Gapon error = uiomove(data + uio->uio_offset, (int)n, uio); 474be52a95dSAndriy Gapon return (error); 475be52a95dSAndriy Gapon } 476be52a95dSAndriy Gapon 477bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len); 4780c09ac0dSPav Lucistnik udfmp = node->udfmp; 4790c09ac0dSPav Lucistnik do { 4800c09ac0dSPav Lucistnik lbn = lblkno(udfmp, uio->uio_offset); 4810c09ac0dSPav Lucistnik on = blkoff(udfmp, uio->uio_offset); 4820c09ac0dSPav Lucistnik n = min((u_int)(udfmp->bsize - on), 4830c09ac0dSPav Lucistnik uio->uio_resid); 4840c09ac0dSPav Lucistnik diff = fsize - uio->uio_offset; 4850c09ac0dSPav Lucistnik if (diff <= 0) 4860c09ac0dSPav Lucistnik return (0); 4870c09ac0dSPav Lucistnik if (diff < n) 4880c09ac0dSPav Lucistnik n = diff; 4890c09ac0dSPav Lucistnik size = udfmp->bsize; 4900c09ac0dSPav Lucistnik rablock = lbn + 1; 4910c09ac0dSPav Lucistnik if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 4920c09ac0dSPav Lucistnik if (lblktosize(udfmp, rablock) < fsize) { 493c535690bSKonstantin Belousov error = cluster_read(vp, fsize, lbn, size, 494c535690bSKonstantin Belousov NOCRED, uio->uio_resid, 495c535690bSKonstantin Belousov (ap->a_ioflag >> 16), 0, &bp); 4960c09ac0dSPav Lucistnik } else { 4970c09ac0dSPav Lucistnik error = bread(vp, lbn, size, NOCRED, &bp); 4980c09ac0dSPav Lucistnik } 4990c09ac0dSPav Lucistnik } else { 5000c09ac0dSPav Lucistnik error = bread(vp, lbn, size, NOCRED, &bp); 5010c09ac0dSPav Lucistnik } 5021fa81dabSKonstantin Belousov if (error != 0) { 50351a7b740SScott Long brelse(bp); 5040c09ac0dSPav Lucistnik return (error); 5050c09ac0dSPav Lucistnik } 5061fa81dabSKonstantin Belousov n = min(n, size - bp->b_resid); 50751a7b740SScott Long 5080c09ac0dSPav Lucistnik error = uiomove(bp->b_data + on, (int)n, uio); 5090c09ac0dSPav Lucistnik brelse(bp); 5100c09ac0dSPav Lucistnik } while (error == 0 && uio->uio_resid > 0 && n != 0); 51151a7b740SScott Long return (error); 51251a7b740SScott Long } 51351a7b740SScott Long 51451a7b740SScott Long /* 51551a7b740SScott Long * Call the OSTA routines to translate the name from a CS0 dstring to a 51651a7b740SScott Long * 16-bit Unicode String. Hooks need to be placed in here to translate from 5176565282cSScott Long * Unicode to the encoding that the kernel/user expects. Return the length 5186565282cSScott Long * of the translated string. 51951a7b740SScott Long */ 52051a7b740SScott Long static int 5216565282cSScott Long udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp) 52251a7b740SScott Long { 52351a7b740SScott Long unicode_t *transname; 5246565282cSScott Long char *unibuf, *unip; 525181fc3c6SR. Imura int i, destlen; 526181fc3c6SR. Imura ssize_t unilen = 0; 5276565282cSScott Long size_t destleft = MAXNAMLEN; 52851a7b740SScott Long 5296565282cSScott Long /* Convert 16-bit Unicode to destname */ 5306565282cSScott Long if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) { 5316565282cSScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */ 5326565282cSScott Long unibuf = uma_zalloc(udf_zone_trans, M_WAITOK); 5336565282cSScott Long unip = unibuf; 534181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) { 5356565282cSScott Long printf("udf: Unicode translation failed\n"); 5366565282cSScott Long uma_zfree(udf_zone_trans, unibuf); 5376565282cSScott Long return 0; 5386565282cSScott Long } 5396565282cSScott Long 5406565282cSScott Long while (unilen > 0 && destleft > 0) { 54138fc0aa4SDimitry Andric udf_iconv->conv(udfmp->im_d2l, __DECONST(const char **, 54238fc0aa4SDimitry Andric &unibuf), (size_t *)&unilen, (char **)&destname, 54338fc0aa4SDimitry Andric &destleft); 5446565282cSScott Long /* Unconverted character found */ 5456565282cSScott Long if (unilen > 0 && destleft > 0) { 5466565282cSScott Long *destname++ = '?'; 5476565282cSScott Long destleft--; 5486565282cSScott Long unibuf += 2; 5496565282cSScott Long unilen -= 2; 5506565282cSScott Long } 5516565282cSScott Long } 5526565282cSScott Long uma_zfree(udf_zone_trans, unip); 5536565282cSScott Long *destname = '\0'; 5546565282cSScott Long destlen = MAXNAMLEN - (int)destleft; 5556565282cSScott Long } else { 55651a7b740SScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */ 557a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK); 55851a7b740SScott Long 559181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) { 56051a7b740SScott Long printf("udf: Unicode translation failed\n"); 56151a7b740SScott Long uma_zfree(udf_zone_trans, transname); 56251a7b740SScott Long return 0; 56351a7b740SScott Long } 56451a7b740SScott Long 56551a7b740SScott Long for (i = 0; i < unilen ; i++) { 56651a7b740SScott Long if (transname[i] & 0xff00) { 56751a7b740SScott Long destname[i] = '.'; /* Fudge the 16bit chars */ 56851a7b740SScott Long } else { 56951a7b740SScott Long destname[i] = transname[i] & 0xff; 57051a7b740SScott Long } 57151a7b740SScott Long } 57251a7b740SScott Long uma_zfree(udf_zone_trans, transname); 5736565282cSScott Long destname[unilen] = 0; 574181fc3c6SR. Imura destlen = (int)unilen; 5756565282cSScott Long } 57651a7b740SScott Long 5776565282cSScott Long return (destlen); 57851a7b740SScott Long } 57951a7b740SScott Long 58051a7b740SScott Long /* 58151a7b740SScott Long * Compare a CS0 dstring with a name passed in from the VFS layer. Return 582befb7f33SChristian Brueffer * 0 on a successful match, nonzero otherwise. Unicode work may need to be done 58351a7b740SScott Long * here also. 58451a7b740SScott Long */ 58551a7b740SScott Long static int 5866565282cSScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp) 58751a7b740SScott Long { 58895ec5961SScott Long char *transname; 58995ec5961SScott Long int error = 0; 59051a7b740SScott Long 59195ec5961SScott Long /* This is overkill, but not worth creating a new zone */ 592a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK); 59395ec5961SScott Long 5946565282cSScott Long cs0len = udf_transname(cs0string, transname, cs0len, udfmp); 59551a7b740SScott Long 59651a7b740SScott Long /* Easy check. If they aren't the same length, they aren't equal */ 59795ec5961SScott Long if ((cs0len == 0) || (cs0len != cmplen)) 59895ec5961SScott Long error = -1; 59995ec5961SScott Long else 60095ec5961SScott Long error = bcmp(transname, cmpname, cmplen); 60151a7b740SScott Long 60295ec5961SScott Long uma_zfree(udf_zone_trans, transname); 60395ec5961SScott Long return (error); 60451a7b740SScott Long } 60551a7b740SScott Long 60651a7b740SScott Long struct udf_uiodir { 60751a7b740SScott Long struct dirent *dirent; 60851a7b740SScott Long u_long *cookies; 60951a7b740SScott Long int ncookies; 61051a7b740SScott Long int acookies; 61151a7b740SScott Long int eofflag; 61251a7b740SScott Long }; 61351a7b740SScott Long 61451a7b740SScott Long static int 61551a7b740SScott Long udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie) 61651a7b740SScott Long { 61751a7b740SScott Long if (uiodir->cookies != NULL) { 61851a7b740SScott Long if (++uiodir->acookies > uiodir->ncookies) { 61951a7b740SScott Long uiodir->eofflag = 0; 62051a7b740SScott Long return (-1); 62151a7b740SScott Long } 62251a7b740SScott Long *uiodir->cookies++ = cookie; 62351a7b740SScott Long } 62451a7b740SScott Long 62551a7b740SScott Long if (uio->uio_resid < de_size) { 62651a7b740SScott Long uiodir->eofflag = 0; 62751a7b740SScott Long return (-1); 62851a7b740SScott Long } 62951a7b740SScott Long 630c9524588SDag-Erling Smørgrav return (uiomove(uiodir->dirent, de_size, uio)); 63151a7b740SScott Long } 63251a7b740SScott Long 6331703656aSScott Long static struct udf_dirstream * 6341703656aSScott Long udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp) 6351703656aSScott Long { 6361703656aSScott Long struct udf_dirstream *ds; 6371703656aSScott Long 638a163d034SWarner Losh ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO); 6391703656aSScott Long 6401703656aSScott Long ds->node = node; 6411703656aSScott Long ds->offset = offset; 6421703656aSScott Long ds->udfmp = udfmp; 6431703656aSScott Long ds->fsize = fsize; 6441703656aSScott Long 6451703656aSScott Long return (ds); 6461703656aSScott Long } 6471703656aSScott Long 6481703656aSScott Long static struct fileid_desc * 6491703656aSScott Long udf_getfid(struct udf_dirstream *ds) 6501703656aSScott Long { 6511703656aSScott Long struct fileid_desc *fid; 6521703656aSScott Long int error, frag_size = 0, total_fid_size; 6531703656aSScott Long 6541703656aSScott Long /* End of directory? */ 6551703656aSScott Long if (ds->offset + ds->off >= ds->fsize) { 6561703656aSScott Long ds->error = 0; 6571703656aSScott Long return (NULL); 6581703656aSScott Long } 6591703656aSScott Long 6601703656aSScott Long /* Grab the first extent of the directory */ 6611703656aSScott Long if (ds->off == 0) { 6621703656aSScott Long ds->size = 0; 6631703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset, 6641703656aSScott Long &ds->bp, &ds->data); 6651703656aSScott Long if (error) { 6661703656aSScott Long ds->error = error; 667744bb56dSScott Long if (ds->bp != NULL) 668744bb56dSScott Long brelse(ds->bp); 6691703656aSScott Long return (NULL); 6701703656aSScott Long } 6711703656aSScott Long } 6721703656aSScott Long 6734576293dSScott Long /* 6744576293dSScott Long * Clean up from a previous fragmented FID. 6754576293dSScott Long * XXX Is this the right place for this? 6764576293dSScott Long */ 6771703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) { 6781703656aSScott Long ds->fid_fragment = 0; 6791ede983cSDag-Erling Smørgrav free(ds->buf, M_UDFFID); 6801703656aSScott Long } 6811703656aSScott Long 6821703656aSScott Long fid = (struct fileid_desc*)&ds->data[ds->off]; 6831703656aSScott Long 6841703656aSScott Long /* 6851703656aSScott Long * Check to see if the fid is fragmented. The first test 6861703656aSScott Long * ensures that we don't wander off the end of the buffer 6871703656aSScott Long * looking for the l_iu and l_fi fields. 6881703656aSScott Long */ 6891703656aSScott Long if (ds->off + UDF_FID_SIZE > ds->size || 690bf1c3dddSScott Long ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){ 6911703656aSScott Long 6921703656aSScott Long /* Copy what we have of the fid into a buffer */ 6931703656aSScott Long frag_size = ds->size - ds->off; 6941703656aSScott Long if (frag_size >= ds->udfmp->bsize) { 6951703656aSScott Long printf("udf: invalid FID fragment\n"); 6961703656aSScott Long ds->error = EINVAL; 6971703656aSScott Long return (NULL); 6981703656aSScott Long } 6991703656aSScott Long 7001703656aSScott Long /* 7011703656aSScott Long * File ID descriptors can only be at most one 7021703656aSScott Long * logical sector in size. 7031703656aSScott Long */ 7041ede983cSDag-Erling Smørgrav ds->buf = malloc(ds->udfmp->bsize, M_UDFFID, 705a163d034SWarner Losh M_WAITOK | M_ZERO); 7061703656aSScott Long bcopy(fid, ds->buf, frag_size); 7071703656aSScott Long 7081703656aSScott Long /* Reduce all of the casting magic */ 7091703656aSScott Long fid = (struct fileid_desc*)ds->buf; 7101703656aSScott Long 7111703656aSScott Long if (ds->bp != NULL) 7121703656aSScott Long brelse(ds->bp); 7131703656aSScott Long 7141703656aSScott Long /* Fetch the next allocation */ 7151703656aSScott Long ds->offset += ds->size; 7161703656aSScott Long ds->size = 0; 7171703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset, 7181703656aSScott Long &ds->bp, &ds->data); 7191703656aSScott Long if (error) { 7201703656aSScott Long ds->error = error; 7211703656aSScott Long return (NULL); 7221703656aSScott Long } 7231703656aSScott Long 7241703656aSScott Long /* 7251703656aSScott Long * If the fragment was so small that we didn't get 7261703656aSScott Long * the l_iu and l_fi fields, copy those in. 7271703656aSScott Long */ 7281703656aSScott Long if (frag_size < UDF_FID_SIZE) 7291703656aSScott Long bcopy(ds->data, &ds->buf[frag_size], 7301703656aSScott Long UDF_FID_SIZE - frag_size); 7311703656aSScott Long 7321703656aSScott Long /* 7331703656aSScott Long * Now that we have enough of the fid to work with, 7341703656aSScott Long * copy in the rest of the fid from the new 7351703656aSScott Long * allocation. 7361703656aSScott Long */ 737bf1c3dddSScott Long total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi; 7381703656aSScott Long if (total_fid_size > ds->udfmp->bsize) { 7391703656aSScott Long printf("udf: invalid FID\n"); 7401703656aSScott Long ds->error = EIO; 7411703656aSScott Long return (NULL); 7421703656aSScott Long } 7431703656aSScott Long bcopy(ds->data, &ds->buf[frag_size], 7441703656aSScott Long total_fid_size - frag_size); 7451703656aSScott Long 7461703656aSScott Long ds->fid_fragment = 1; 7471703656aSScott Long } else { 748bf1c3dddSScott Long total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE; 7491703656aSScott Long } 7501703656aSScott Long 7511703656aSScott Long /* 7521703656aSScott Long * Update the offset. Align on a 4 byte boundary because the 7534576293dSScott Long * UDF spec says so. 7541703656aSScott Long */ 755159da68bSAndriy Gapon ds->this_off = ds->offset + ds->off; 7561703656aSScott Long if (!ds->fid_fragment) { 7571703656aSScott Long ds->off += (total_fid_size + 3) & ~0x03; 7581703656aSScott Long } else { 7591703656aSScott Long ds->off = (total_fid_size - frag_size + 3) & ~0x03; 7601703656aSScott Long } 7611703656aSScott Long 7621703656aSScott Long return (fid); 7631703656aSScott Long } 7641703656aSScott Long 7651703656aSScott Long static void 7661703656aSScott Long udf_closedir(struct udf_dirstream *ds) 7671703656aSScott Long { 7681703656aSScott Long 7691703656aSScott Long if (ds->bp != NULL) 7701703656aSScott Long brelse(ds->bp); 7711703656aSScott Long 7721703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) 7731ede983cSDag-Erling Smørgrav free(ds->buf, M_UDFFID); 7741703656aSScott Long 7751703656aSScott Long uma_zfree(udf_zone_ds, ds); 7761703656aSScott Long } 7771703656aSScott Long 77851a7b740SScott Long static int 77951a7b740SScott Long udf_readdir(struct vop_readdir_args *a) 78051a7b740SScott Long { 78151a7b740SScott Long struct vnode *vp; 78251a7b740SScott Long struct uio *uio; 78351a7b740SScott Long struct dirent dir; 78451a7b740SScott Long struct udf_node *node; 7856565282cSScott Long struct udf_mnt *udfmp; 78651a7b740SScott Long struct fileid_desc *fid; 78751a7b740SScott Long struct udf_uiodir uiodir; 7881703656aSScott Long struct udf_dirstream *ds; 78951a7b740SScott Long u_long *cookies = NULL; 79051a7b740SScott Long int ncookies; 791c8eeea2fSScott Long int error = 0; 79251a7b740SScott Long 79351a7b740SScott Long vp = a->a_vp; 79451a7b740SScott Long uio = a->a_uio; 79551a7b740SScott Long node = VTON(vp); 7966565282cSScott Long udfmp = node->udfmp; 79751a7b740SScott Long uiodir.eofflag = 1; 79851a7b740SScott Long 79951a7b740SScott Long if (a->a_ncookies != NULL) { 80051a7b740SScott Long /* 80151a7b740SScott Long * Guess how many entries are needed. If we run out, this 80251a7b740SScott Long * function will be called again and thing will pick up were 80351a7b740SScott Long * it left off. 80451a7b740SScott Long */ 80551a7b740SScott Long ncookies = uio->uio_resid / 8; 8061ede983cSDag-Erling Smørgrav cookies = malloc(sizeof(u_long) * ncookies, 807a163d034SWarner Losh M_TEMP, M_WAITOK); 80851a7b740SScott Long if (cookies == NULL) 80951a7b740SScott Long return (ENOMEM); 81051a7b740SScott Long uiodir.ncookies = ncookies; 81151a7b740SScott Long uiodir.cookies = cookies; 81251a7b740SScott Long uiodir.acookies = 0; 81351a7b740SScott Long } else { 81451a7b740SScott Long uiodir.cookies = NULL; 81551a7b740SScott Long } 81651a7b740SScott Long 81751a7b740SScott Long /* 81851a7b740SScott Long * Iterate through the file id descriptors. Give the parent dir 8194576293dSScott Long * entry special attention. 82051a7b740SScott Long */ 821bf1c3dddSScott Long ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len), 8221703656aSScott Long node->udfmp); 82351a7b740SScott Long 8241703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) { 82551a7b740SScott Long 82651a7b740SScott Long /* XXX Should we return an error on a bad fid? */ 82751a7b740SScott Long if (udf_checktag(&fid->tag, TAGID_FID)) { 82851a7b740SScott Long printf("Invalid FID tag\n"); 82977411499SScott Long hexdump(fid, UDF_FID_SIZE, NULL, 0); 8301703656aSScott Long error = EIO; 83151a7b740SScott Long break; 83251a7b740SScott Long } 83351a7b740SScott Long 83451a7b740SScott Long /* Is this a deleted file? */ 8352bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL) 8361703656aSScott Long continue; 83751a7b740SScott Long 8382bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 83951a7b740SScott Long /* Do up the '.' and '..' entries. Dummy values are 84051a7b740SScott Long * used for the cookies since the offset here is 84151a7b740SScott Long * usually zero, and NFS doesn't like that value 84251a7b740SScott Long */ 843c8eeea2fSScott Long dir.d_fileno = node->hash_id; 844c8eeea2fSScott Long dir.d_type = DT_DIR; 845c8eeea2fSScott Long dir.d_name[0] = '.'; 846444acc16SScott Long dir.d_name[1] = '\0'; 847c8eeea2fSScott Long dir.d_namlen = 1; 848c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 849*1c4ca778SKonstantin Belousov dir.d_off = 1; 850c8eeea2fSScott Long uiodir.dirent = &dir; 851c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1); 85251a7b740SScott Long if (error) 85351a7b740SScott Long break; 85451a7b740SScott Long 855c8eeea2fSScott Long dir.d_fileno = udf_getid(&fid->icb); 856c8eeea2fSScott Long dir.d_type = DT_DIR; 857c8eeea2fSScott Long dir.d_name[0] = '.'; 858c8eeea2fSScott Long dir.d_name[1] = '.'; 859444acc16SScott Long dir.d_name[2] = '\0'; 860c8eeea2fSScott Long dir.d_namlen = 2; 861c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 862*1c4ca778SKonstantin Belousov dir.d_off = 2; 863c8eeea2fSScott Long uiodir.dirent = &dir; 864c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2); 86551a7b740SScott Long } else { 86651a7b740SScott Long dir.d_namlen = udf_transname(&fid->data[fid->l_iu], 8676565282cSScott Long &dir.d_name[0], fid->l_fi, udfmp); 86851a7b740SScott Long dir.d_fileno = udf_getid(&fid->icb); 8692bbe0d36SScott Long dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ? 8702bbe0d36SScott Long DT_DIR : DT_UNKNOWN; 87151a7b740SScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir); 872*1c4ca778SKonstantin Belousov dir.d_off = ds->this_off; 87351a7b740SScott Long uiodir.dirent = &dir; 8741703656aSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 8751703656aSScott Long ds->this_off); 87651a7b740SScott Long } 87784206c74SAndriy Gapon if (error) 87851a7b740SScott Long break; 879ff9e355bSAndriy Gapon uio->uio_offset = ds->offset + ds->off; 88051a7b740SScott Long } 88151a7b740SScott Long 88251a7b740SScott Long /* tell the calling layer whether we need to be called again */ 88351a7b740SScott Long *a->a_eofflag = uiodir.eofflag; 88451a7b740SScott Long 88584206c74SAndriy Gapon if (error < 0) 88684206c74SAndriy Gapon error = 0; 8871703656aSScott Long if (!error) 8881703656aSScott Long error = ds->error; 8891703656aSScott Long 8901703656aSScott Long udf_closedir(ds); 89151a7b740SScott Long 89251a7b740SScott Long if (a->a_ncookies != NULL) { 89351a7b740SScott Long if (error) 8941ede983cSDag-Erling Smørgrav free(cookies, M_TEMP); 89551a7b740SScott Long else { 89651a7b740SScott Long *a->a_ncookies = uiodir.acookies; 89751a7b740SScott Long *a->a_cookies = cookies; 89851a7b740SScott Long } 89951a7b740SScott Long } 90051a7b740SScott Long 90151a7b740SScott Long return (error); 90251a7b740SScott Long } 90351a7b740SScott Long 90451a7b740SScott Long static int 90551a7b740SScott Long udf_readlink(struct vop_readlink_args *ap) 90651a7b740SScott Long { 907e3024df2SJohn Baldwin struct path_component *pc, *end; 908e3024df2SJohn Baldwin struct vnode *vp; 909e3024df2SJohn Baldwin struct uio uio; 910e3024df2SJohn Baldwin struct iovec iov[1]; 911e3024df2SJohn Baldwin struct udf_node *node; 912e3024df2SJohn Baldwin void *buf; 913e3024df2SJohn Baldwin char *cp; 914e3024df2SJohn Baldwin int error, len, root; 915e3024df2SJohn Baldwin 916e3024df2SJohn Baldwin /* 917e3024df2SJohn Baldwin * A symbolic link in UDF is a list of variable-length path 918e3024df2SJohn Baldwin * component structures. We build a pathname in the caller's 919e3024df2SJohn Baldwin * uio by traversing this list. 920e3024df2SJohn Baldwin */ 921e3024df2SJohn Baldwin vp = ap->a_vp; 922e3024df2SJohn Baldwin node = VTON(vp); 923e3024df2SJohn Baldwin len = le64toh(node->fentry->inf_len); 92412b3a08dSAndriy Gapon buf = malloc(len, M_DEVBUF, M_WAITOK); 9256b3ee248SAndriy Gapon iov[0].iov_len = len; 926e3024df2SJohn Baldwin iov[0].iov_base = buf; 927e3024df2SJohn Baldwin uio.uio_iov = iov; 928e3024df2SJohn Baldwin uio.uio_iovcnt = 1; 929e3024df2SJohn Baldwin uio.uio_offset = 0; 930e3024df2SJohn Baldwin uio.uio_resid = iov[0].iov_len; 931e3024df2SJohn Baldwin uio.uio_segflg = UIO_SYSSPACE; 932e3024df2SJohn Baldwin uio.uio_rw = UIO_READ; 933e3024df2SJohn Baldwin uio.uio_td = curthread; 934e3024df2SJohn Baldwin error = VOP_READ(vp, &uio, 0, ap->a_cred); 935e3024df2SJohn Baldwin if (error) 936e3024df2SJohn Baldwin goto error; 937e3024df2SJohn Baldwin 938e3024df2SJohn Baldwin pc = buf; 939e3024df2SJohn Baldwin end = (void *)((char *)buf + len); 940e3024df2SJohn Baldwin root = 0; 941e3024df2SJohn Baldwin while (pc < end) { 942e3024df2SJohn Baldwin switch (pc->type) { 943e3024df2SJohn Baldwin case UDF_PATH_ROOT: 944e3024df2SJohn Baldwin /* Only allow this at the beginning of a path. */ 945e3024df2SJohn Baldwin if ((void *)pc != buf) { 946e3024df2SJohn Baldwin error = EINVAL; 947e3024df2SJohn Baldwin goto error; 948e3024df2SJohn Baldwin } 949e3024df2SJohn Baldwin cp = "/"; 950e3024df2SJohn Baldwin len = 1; 951e3024df2SJohn Baldwin root = 1; 952e3024df2SJohn Baldwin break; 953e3024df2SJohn Baldwin case UDF_PATH_DOT: 954e3024df2SJohn Baldwin cp = "."; 955e3024df2SJohn Baldwin len = 1; 956e3024df2SJohn Baldwin break; 957e3024df2SJohn Baldwin case UDF_PATH_DOTDOT: 958e3024df2SJohn Baldwin cp = ".."; 959e3024df2SJohn Baldwin len = 2; 960e3024df2SJohn Baldwin break; 961e3024df2SJohn Baldwin case UDF_PATH_PATH: 962e3024df2SJohn Baldwin if (pc->length == 0) { 963e3024df2SJohn Baldwin error = EINVAL; 964e3024df2SJohn Baldwin goto error; 965e3024df2SJohn Baldwin } 966e3024df2SJohn Baldwin /* 967e3024df2SJohn Baldwin * XXX: We only support CS8 which appears to map 968e3024df2SJohn Baldwin * to ASCII directly. 969e3024df2SJohn Baldwin */ 970e3024df2SJohn Baldwin switch (pc->identifier[0]) { 971e3024df2SJohn Baldwin case 8: 972e3024df2SJohn Baldwin cp = pc->identifier + 1; 973e3024df2SJohn Baldwin len = pc->length - 1; 974e3024df2SJohn Baldwin break; 975e3024df2SJohn Baldwin default: 976e3024df2SJohn Baldwin error = EOPNOTSUPP; 977e3024df2SJohn Baldwin goto error; 978e3024df2SJohn Baldwin } 979e3024df2SJohn Baldwin break; 980e3024df2SJohn Baldwin default: 981e3024df2SJohn Baldwin error = EINVAL; 982e3024df2SJohn Baldwin goto error; 983e3024df2SJohn Baldwin } 984e3024df2SJohn Baldwin 985e3024df2SJohn Baldwin /* 986e3024df2SJohn Baldwin * If this is not the first component, insert a path 987e3024df2SJohn Baldwin * separator. 988e3024df2SJohn Baldwin */ 989e3024df2SJohn Baldwin if (pc != buf) { 990e3024df2SJohn Baldwin /* If we started with root we already have a "/". */ 991e3024df2SJohn Baldwin if (root) 992e3024df2SJohn Baldwin goto skipslash; 993e3024df2SJohn Baldwin root = 0; 994e3024df2SJohn Baldwin if (ap->a_uio->uio_resid < 1) { 995e3024df2SJohn Baldwin error = ENAMETOOLONG; 996e3024df2SJohn Baldwin goto error; 997e3024df2SJohn Baldwin } 998e3024df2SJohn Baldwin error = uiomove("/", 1, ap->a_uio); 999e3024df2SJohn Baldwin if (error) 1000e3024df2SJohn Baldwin break; 1001e3024df2SJohn Baldwin } 1002e3024df2SJohn Baldwin skipslash: 1003e3024df2SJohn Baldwin 1004e3024df2SJohn Baldwin /* Append string at 'cp' of length 'len' to our path. */ 1005e3024df2SJohn Baldwin if (len > ap->a_uio->uio_resid) { 1006e3024df2SJohn Baldwin error = ENAMETOOLONG; 1007e3024df2SJohn Baldwin goto error; 1008e3024df2SJohn Baldwin } 1009e3024df2SJohn Baldwin error = uiomove(cp, len, ap->a_uio); 1010e3024df2SJohn Baldwin if (error) 1011e3024df2SJohn Baldwin break; 1012e3024df2SJohn Baldwin 1013e3024df2SJohn Baldwin /* Advance to next component. */ 1014e3024df2SJohn Baldwin pc = (void *)((char *)pc + 4 + pc->length); 1015e3024df2SJohn Baldwin } 1016e3024df2SJohn Baldwin error: 1017e3024df2SJohn Baldwin free(buf, M_DEVBUF); 1018e3024df2SJohn Baldwin return (error); 101951a7b740SScott Long } 102051a7b740SScott Long 102151a7b740SScott Long static int 102251a7b740SScott Long udf_strategy(struct vop_strategy_args *a) 102351a7b740SScott Long { 102451a7b740SScott Long struct buf *bp; 102551a7b740SScott Long struct vnode *vp; 102651a7b740SScott Long struct udf_node *node; 1027429c018aSPoul-Henning Kamp struct bufobj *bo; 10285792e04dSAndriy Gapon off_t offset; 10295792e04dSAndriy Gapon uint32_t maxsize; 10305792e04dSAndriy Gapon daddr_t sector; 10315792e04dSAndriy Gapon int error; 103251a7b740SScott Long 103351a7b740SScott Long bp = a->a_bp; 1034d83b7498SPoul-Henning Kamp vp = a->a_vp; 103551a7b740SScott Long node = VTON(vp); 103651a7b740SScott Long 10370c09ac0dSPav Lucistnik if (bp->b_blkno == bp->b_lblkno) { 10385792e04dSAndriy Gapon offset = lblktosize(node->udfmp, bp->b_lblkno); 10395792e04dSAndriy Gapon error = udf_bmap_internal(node, offset, §or, &maxsize); 10405792e04dSAndriy Gapon if (error) { 104151a7b740SScott Long clrbuf(bp); 104251a7b740SScott Long bp->b_blkno = -1; 104351a7b740SScott Long bufdone(bp); 104451a7b740SScott Long return (0); 104551a7b740SScott Long } 10465792e04dSAndriy Gapon /* bmap gives sector numbers, bio works with device blocks */ 10475792e04dSAndriy Gapon bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT); 10485792e04dSAndriy Gapon } 1049429c018aSPoul-Henning Kamp bo = node->udfmp->im_bo; 10502c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno); 10510391e5a1SPoul-Henning Kamp BO_STRATEGY(bo, bp); 105251a7b740SScott Long return (0); 105351a7b740SScott Long } 105451a7b740SScott Long 105551a7b740SScott Long static int 105651a7b740SScott Long udf_bmap(struct vop_bmap_args *a) 105751a7b740SScott Long { 105851a7b740SScott Long struct udf_node *node; 1059c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size; 106098b0c789SPoul-Henning Kamp daddr_t lsector; 1061fb2a76ccSAndriy Gapon int nblk; 106251a7b740SScott Long int error; 106351a7b740SScott Long 106451a7b740SScott Long node = VTON(a->a_vp); 106551a7b740SScott Long 10669c83534dSPoul-Henning Kamp if (a->a_bop != NULL) 1067e0251bbbSPoul-Henning Kamp *a->a_bop = &node->udfmp->im_devvp->v_bufobj; 106851a7b740SScott Long if (a->a_bnp == NULL) 106951a7b740SScott Long return (0); 107051a7b740SScott Long if (a->a_runb) 107151a7b740SScott Long *a->a_runb = 0; 107251a7b740SScott Long 107382467096SAndriy Gapon /* 107482467096SAndriy Gapon * UDF_INVALID_BMAP means data embedded into fentry, this is an internal 107582467096SAndriy Gapon * error that should not be propagated to calling code. 107682467096SAndriy Gapon * Most obvious mapping for this error is EOPNOTSUPP as we can not truly 107782467096SAndriy Gapon * translate block numbers in this case. 107882467096SAndriy Gapon * Incidentally, this return code will make vnode pager to use VOP_READ 107982467096SAndriy Gapon * to get data for mmap-ed pages and udf_read knows how to do the right 108082467096SAndriy Gapon * thing for this kind of files. 108182467096SAndriy Gapon */ 108282467096SAndriy Gapon error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift, 108382467096SAndriy Gapon &lsector, &max_size); 108482467096SAndriy Gapon if (error == UDF_INVALID_BMAP) 108582467096SAndriy Gapon return (EOPNOTSUPP); 10868db4c2f2SScott Long if (error) 108751a7b740SScott Long return (error); 108851a7b740SScott Long 1089cd1b1a1dSScott Long /* Translate logical to physical sector number */ 1090cd1b1a1dSScott Long *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT); 1091cd1b1a1dSScott Long 1092fb2a76ccSAndriy Gapon /* 1093fb2a76ccSAndriy Gapon * Determine maximum number of readahead blocks following the 1094fb2a76ccSAndriy Gapon * requested block. 1095fb2a76ccSAndriy Gapon */ 1096fb2a76ccSAndriy Gapon if (a->a_runp) { 1097fb2a76ccSAndriy Gapon nblk = (max_size >> node->udfmp->bshift) - 1; 1098fb2a76ccSAndriy Gapon if (nblk <= 0) 109951a7b740SScott Long *a->a_runp = 0; 1100fb2a76ccSAndriy Gapon else if (nblk >= (MAXBSIZE >> node->udfmp->bshift)) 1101fb2a76ccSAndriy Gapon *a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1; 1102fb2a76ccSAndriy Gapon else 1103fb2a76ccSAndriy Gapon *a->a_runp = nblk; 1104fb2a76ccSAndriy Gapon } 1105fb2a76ccSAndriy Gapon 1106fb2a76ccSAndriy Gapon if (a->a_runb) { 1107fb2a76ccSAndriy Gapon *a->a_runb = 0; 1108fb2a76ccSAndriy Gapon } 110951a7b740SScott Long 111051a7b740SScott Long return (0); 111151a7b740SScott Long } 111251a7b740SScott Long 111351a7b740SScott Long /* 111451a7b740SScott Long * The all powerful VOP_LOOKUP(). 111551a7b740SScott Long */ 111651a7b740SScott Long static int 111751a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a) 111851a7b740SScott Long { 111951a7b740SScott Long struct vnode *dvp; 112051a7b740SScott Long struct vnode *tdp = NULL; 112151a7b740SScott Long struct vnode **vpp = a->a_vpp; 112251a7b740SScott Long struct udf_node *node; 112351a7b740SScott Long struct udf_mnt *udfmp; 112451a7b740SScott Long struct fileid_desc *fid = NULL; 11251703656aSScott Long struct udf_dirstream *ds; 112651a7b740SScott Long u_long nameiop; 112751a7b740SScott Long u_long flags; 112851a7b740SScott Long char *nameptr; 112951a7b740SScott Long long namelen; 113051a7b740SScott Long ino_t id = 0; 11311703656aSScott Long int offset, error = 0; 11324ad0d60bSJohn Baldwin int fsize, lkflags, ltype, numdirpasses; 113351a7b740SScott Long 113451a7b740SScott Long dvp = a->a_dvp; 113551a7b740SScott Long node = VTON(dvp); 113651a7b740SScott Long udfmp = node->udfmp; 113751a7b740SScott Long nameiop = a->a_cnp->cn_nameiop; 113851a7b740SScott Long flags = a->a_cnp->cn_flags; 11394ad0d60bSJohn Baldwin lkflags = a->a_cnp->cn_lkflags; 114051a7b740SScott Long nameptr = a->a_cnp->cn_nameptr; 114151a7b740SScott Long namelen = a->a_cnp->cn_namelen; 1142bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len); 114351a7b740SScott Long 114451a7b740SScott Long /* 114551a7b740SScott Long * If this is a LOOKUP and we've already partially searched through 114651a7b740SScott Long * the directory, pick up where we left off and flag that the 114751a7b740SScott Long * directory may need to be searched twice. For a full description, 11484b12bb04SMaxim Konovalov * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup() 114951a7b740SScott Long */ 11501703656aSScott Long if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) { 115151a7b740SScott Long offset = 0; 115251a7b740SScott Long numdirpasses = 1; 115351a7b740SScott Long } else { 115451a7b740SScott Long offset = node->diroff; 115551a7b740SScott Long numdirpasses = 2; 115651a7b740SScott Long nchstats.ncs_2passes++; 115751a7b740SScott Long } 115851a7b740SScott Long 115951a7b740SScott Long lookloop: 11601703656aSScott Long ds = udf_opendir(node, offset, fsize, udfmp); 116151a7b740SScott Long 11621703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) { 116351a7b740SScott Long 116451a7b740SScott Long /* XXX Should we return an error on a bad fid? */ 11651703656aSScott Long if (udf_checktag(&fid->tag, TAGID_FID)) { 11661703656aSScott Long printf("udf_lookup: Invalid tag\n"); 11671703656aSScott Long error = EIO; 11681703656aSScott Long break; 11691703656aSScott Long } 1170678d5effSScott Long 1171678d5effSScott Long /* Is this a deleted file? */ 11722bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL) 11731703656aSScott Long continue; 117451a7b740SScott Long 11752bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) { 117651a7b740SScott Long if (flags & ISDOTDOT) { 117751a7b740SScott Long id = udf_getid(&fid->icb); 117851a7b740SScott Long break; 117951a7b740SScott Long } 118051a7b740SScott Long } else { 118151a7b740SScott Long if (!(udf_cmpname(&fid->data[fid->l_iu], 11826565282cSScott Long nameptr, fid->l_fi, namelen, udfmp))) { 118351a7b740SScott Long id = udf_getid(&fid->icb); 118451a7b740SScott Long break; 118551a7b740SScott Long } 118651a7b740SScott Long } 118751a7b740SScott Long } 11881703656aSScott Long 11891703656aSScott Long if (!error) 11901703656aSScott Long error = ds->error; 11911703656aSScott Long 11921703656aSScott Long /* XXX Bail out here? */ 11931703656aSScott Long if (error) { 11941703656aSScott Long udf_closedir(ds); 11951703656aSScott Long return (error); 119651a7b740SScott Long } 119751a7b740SScott Long 119851a7b740SScott Long /* Did we have a match? */ 119951a7b740SScott Long if (id) { 12001703656aSScott Long /* 12011703656aSScott Long * Remember where this entry was if it's the final 12021703656aSScott Long * component. 12031703656aSScott Long */ 120451a7b740SScott Long if ((flags & ISLASTCN) && nameiop == LOOKUP) 12051703656aSScott Long node->diroff = ds->offset + ds->off; 120651a7b740SScott Long if (numdirpasses == 2) 120751a7b740SScott Long nchstats.ncs_pass2++; 12084ad0d60bSJohn Baldwin udf_closedir(ds); 12094ad0d60bSJohn Baldwin 12104ad0d60bSJohn Baldwin if (flags & ISDOTDOT) { 12114ad0d60bSJohn Baldwin error = vn_vget_ino(dvp, id, lkflags, &tdp); 12124ad0d60bSJohn Baldwin } else if (node->hash_id == id) { 12134ad0d60bSJohn Baldwin VREF(dvp); /* we want ourself, ie "." */ 12144ad0d60bSJohn Baldwin /* 12154ad0d60bSJohn Baldwin * When we lookup "." we still can be asked to lock it 12164ad0d60bSJohn Baldwin * differently. 12174ad0d60bSJohn Baldwin */ 12184ad0d60bSJohn Baldwin ltype = lkflags & LK_TYPE_MASK; 12194ad0d60bSJohn Baldwin if (ltype != VOP_ISLOCKED(dvp)) { 12204ad0d60bSJohn Baldwin if (ltype == LK_EXCLUSIVE) 12214ad0d60bSJohn Baldwin vn_lock(dvp, LK_UPGRADE | LK_RETRY); 12224ad0d60bSJohn Baldwin else /* if (ltype == LK_SHARED) */ 12234ad0d60bSJohn Baldwin vn_lock(dvp, LK_DOWNGRADE | LK_RETRY); 12244ad0d60bSJohn Baldwin } 12254ad0d60bSJohn Baldwin tdp = dvp; 12264ad0d60bSJohn Baldwin } else 12274ad0d60bSJohn Baldwin error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp); 12284ad0d60bSJohn Baldwin if (!error) { 122951a7b740SScott Long *vpp = tdp; 123051a7b740SScott Long /* Put this entry in the cache */ 123151a7b740SScott Long if (flags & MAKEENTRY) 123251a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp); 12334585e3acSJeff Roberson } 12341703656aSScott Long } else { 123551a7b740SScott Long /* Name wasn't found on this pass. Do another pass? */ 123651a7b740SScott Long if (numdirpasses == 2) { 123751a7b740SScott Long numdirpasses--; 123851a7b740SScott Long offset = 0; 12391703656aSScott Long udf_closedir(ds); 124051a7b740SScott Long goto lookloop; 124151a7b740SScott Long } 12424ad0d60bSJohn Baldwin udf_closedir(ds); 124351a7b740SScott Long 124451a7b740SScott Long /* Enter name into cache as non-existant */ 124551a7b740SScott Long if (flags & MAKEENTRY) 124651a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp); 124751a7b740SScott Long 12481703656aSScott Long if ((flags & ISLASTCN) && 12491703656aSScott Long (nameiop == CREATE || nameiop == RENAME)) { 12501703656aSScott Long error = EROFS; 12511703656aSScott Long } else { 12521703656aSScott Long error = ENOENT; 12531703656aSScott Long } 12541703656aSScott Long } 125551a7b740SScott Long 12561703656aSScott Long return (error); 125751a7b740SScott Long } 125851a7b740SScott Long 125951a7b740SScott Long static int 126051a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a) 126151a7b740SScott Long { 126251a7b740SScott Long struct vnode *vp; 126351a7b740SScott Long struct udf_node *unode; 126451a7b740SScott Long 126551a7b740SScott Long vp = a->a_vp; 126651a7b740SScott Long unode = VTON(vp); 126751a7b740SScott Long 126892e73f57SAlfred Perlstein /* 126992e73f57SAlfred Perlstein * Destroy the vm object and flush associated pages. 127092e73f57SAlfred Perlstein */ 127192e73f57SAlfred Perlstein vnode_destroy_vobject(vp); 127292e73f57SAlfred Perlstein 127351a7b740SScott Long if (unode != NULL) { 12744e94fafcSPoul-Henning Kamp vfs_hash_remove(vp); 127551a7b740SScott Long 127651a7b740SScott Long if (unode->fentry != NULL) 12771ede983cSDag-Erling Smørgrav free(unode->fentry, M_UDFFENTRY); 127851a7b740SScott Long uma_zfree(udf_zone_node, unode); 127951a7b740SScott Long vp->v_data = NULL; 128051a7b740SScott Long } 128151a7b740SScott Long 128251a7b740SScott Long return (0); 128351a7b740SScott Long } 128451a7b740SScott Long 128510bcafe9SPawel Jakub Dawidek static int 128610bcafe9SPawel Jakub Dawidek udf_vptofh(struct vop_vptofh_args *a) 128710bcafe9SPawel Jakub Dawidek { 128810bcafe9SPawel Jakub Dawidek struct udf_node *node; 128910bcafe9SPawel Jakub Dawidek struct ifid *ifhp; 129010bcafe9SPawel Jakub Dawidek 129110bcafe9SPawel Jakub Dawidek node = VTON(a->a_vp); 129210bcafe9SPawel Jakub Dawidek ifhp = (struct ifid *)a->a_fhp; 129310bcafe9SPawel Jakub Dawidek ifhp->ifid_len = sizeof(struct ifid); 129410bcafe9SPawel Jakub Dawidek ifhp->ifid_ino = node->hash_id; 129510bcafe9SPawel Jakub Dawidek 129610bcafe9SPawel Jakub Dawidek return (0); 129710bcafe9SPawel Jakub Dawidek } 129810bcafe9SPawel Jakub Dawidek 129951a7b740SScott Long /* 130051a7b740SScott Long * Read the block and then set the data pointer to correspond with the 130151a7b740SScott Long * offset passed in. Only read in at most 'size' bytes, and then set 'size' 130251a7b740SScott Long * to the number of bytes pointed to. If 'size' is zero, try to read in a 130351a7b740SScott Long * whole extent. 1304744bb56dSScott Long * 1305744bb56dSScott Long * Note that *bp may be assigned error or not. 1306744bb56dSScott Long * 130751a7b740SScott Long */ 130851a7b740SScott Long static int 13099d32fde8SScott Long udf_readatoffset(struct udf_node *node, int *size, off_t offset, 13109d32fde8SScott Long struct buf **bp, uint8_t **data) 131151a7b740SScott Long { 1312b0c0fb59SAndriy Gapon struct udf_mnt *udfmp = node->udfmp; 1313b0c0fb59SAndriy Gapon struct vnode *vp = node->i_vnode; 1314b0c0fb59SAndriy Gapon struct file_entry *fentry; 131551a7b740SScott Long struct buf *bp1; 1316c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size; 131798b0c789SPoul-Henning Kamp daddr_t sector; 1318b0c0fb59SAndriy Gapon off_t off; 1319b0c0fb59SAndriy Gapon int adj_size; 132051a7b740SScott Long int error; 132151a7b740SScott Long 1322b0c0fb59SAndriy Gapon /* 1323b0c0fb59SAndriy Gapon * This call is made *not* only to detect UDF_INVALID_BMAP case, 1324b0c0fb59SAndriy Gapon * max_size is used as an ad-hoc read-ahead hint for "normal" case. 1325b0c0fb59SAndriy Gapon */ 132651a7b740SScott Long error = udf_bmap_internal(node, offset, §or, &max_size); 13274576293dSScott Long if (error == UDF_INVALID_BMAP) { 132851a7b740SScott Long /* 132951a7b740SScott Long * This error means that the file *data* is stored in the 133051a7b740SScott Long * allocation descriptor field of the file entry. 133151a7b740SScott Long */ 133251a7b740SScott Long fentry = node->fentry; 1333bf1c3dddSScott Long *data = &fentry->data[le32toh(fentry->l_ea)]; 1334bf1c3dddSScott Long *size = le32toh(fentry->l_ad); 1335b2c91b67SAndriy Gapon if (offset >= *size) 1336b2c91b67SAndriy Gapon *size = 0; 1337b2c91b67SAndriy Gapon else { 1338b2c91b67SAndriy Gapon *data += offset; 1339b2c91b67SAndriy Gapon *size -= offset; 1340b2c91b67SAndriy Gapon } 134151a7b740SScott Long return (0); 134251a7b740SScott Long } else if (error != 0) { 134351a7b740SScott Long return (error); 134451a7b740SScott Long } 134551a7b740SScott Long 1346d1def83bSScott Long /* Adjust the size so that it is within range */ 134751a7b740SScott Long if (*size == 0 || *size > max_size) 134851a7b740SScott Long *size = max_size; 134951a7b740SScott Long 1350b0c0fb59SAndriy Gapon /* 1351b0c0fb59SAndriy Gapon * Because we will read starting at block boundary, we need to adjust 1352b0c0fb59SAndriy Gapon * how much we need to read so that all promised data is in. 1353b0c0fb59SAndriy Gapon * Also, we can't promise to read more than MAXBSIZE bytes starting 1354b0c0fb59SAndriy Gapon * from block boundary, so adjust what we promise too. 1355b0c0fb59SAndriy Gapon */ 1356b0c0fb59SAndriy Gapon off = blkoff(udfmp, offset); 1357b0c0fb59SAndriy Gapon *size = min(*size, MAXBSIZE - off); 1358b0c0fb59SAndriy Gapon adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask; 1359b0c0fb59SAndriy Gapon *bp = NULL; 1360b0c0fb59SAndriy Gapon if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) { 13611830bca1SScott Long printf("warning: udf_readlblks returned error %d\n", error); 1362744bb56dSScott Long /* note: *bp may be non-NULL */ 136351a7b740SScott Long return (error); 136451a7b740SScott Long } 136551a7b740SScott Long 136651a7b740SScott Long bp1 = *bp; 13675df29e0cSRemko Lodder *data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask]; 136851a7b740SScott Long return (0); 136951a7b740SScott Long } 137051a7b740SScott Long 137151a7b740SScott Long /* 137251a7b740SScott Long * Translate a file offset into a logical block and then into a physical 137351a7b740SScott Long * block. 13745df29e0cSRemko Lodder * max_size - maximum number of bytes that can be read starting from given 13755df29e0cSRemko Lodder * offset, rather than beginning of calculated sector number 137651a7b740SScott Long */ 137751a7b740SScott Long static int 13789d32fde8SScott Long udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector, 13799d32fde8SScott Long uint32_t *max_size) 138051a7b740SScott Long { 138151a7b740SScott Long struct udf_mnt *udfmp; 138251a7b740SScott Long struct file_entry *fentry; 138351a7b740SScott Long void *icb; 138451a7b740SScott Long struct icb_tag *tag; 1385c2d6947dSJeroen Ruigrok van der Werven uint32_t icblen = 0; 138698b0c789SPoul-Henning Kamp daddr_t lsector; 138751a7b740SScott Long int ad_offset, ad_num = 0; 138851a7b740SScott Long int i, p_offset; 138951a7b740SScott Long 139051a7b740SScott Long udfmp = node->udfmp; 139151a7b740SScott Long fentry = node->fentry; 139251a7b740SScott Long tag = &fentry->icbtag; 139351a7b740SScott Long 1394bf1c3dddSScott Long switch (le16toh(tag->strat_type)) { 139551a7b740SScott Long case 4: 139651a7b740SScott Long break; 139751a7b740SScott Long 139851a7b740SScott Long case 4096: 139951a7b740SScott Long printf("Cannot deal with strategy4096 yet!\n"); 140051a7b740SScott Long return (ENODEV); 140151a7b740SScott Long 140251a7b740SScott Long default: 140351a7b740SScott Long printf("Unknown strategy type %d\n", tag->strat_type); 140451a7b740SScott Long return (ENODEV); 140551a7b740SScott Long } 140651a7b740SScott Long 1407bf1c3dddSScott Long switch (le16toh(tag->flags) & 0x7) { 140851a7b740SScott Long case 0: 140951a7b740SScott Long /* 141051a7b740SScott Long * The allocation descriptor field is filled with short_ad's. 141151a7b740SScott Long * If the offset is beyond the current extent, look for the 141251a7b740SScott Long * next extent. 141351a7b740SScott Long */ 141451a7b740SScott Long do { 141551a7b740SScott Long offset -= icblen; 141651a7b740SScott Long ad_offset = sizeof(struct short_ad) * ad_num; 1417bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) { 141851a7b740SScott Long printf("File offset out of bounds\n"); 141951a7b740SScott Long return (EINVAL); 142051a7b740SScott Long } 1421a4d629e3SScott Long icb = GETICB(short_ad, fentry, 1422bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset); 142351a7b740SScott Long icblen = GETICBLEN(short_ad, icb); 142451a7b740SScott Long ad_num++; 142551a7b740SScott Long } while(offset >= icblen); 142651a7b740SScott Long 142751a7b740SScott Long lsector = (offset >> udfmp->bshift) + 1428937a2387SWill Andrews le32toh(((struct short_ad *)(icb))->pos); 142951a7b740SScott Long 14305df29e0cSRemko Lodder *max_size = icblen - offset; 143151a7b740SScott Long 143251a7b740SScott Long break; 143351a7b740SScott Long case 1: 143451a7b740SScott Long /* 143551a7b740SScott Long * The allocation descriptor field is filled with long_ad's 143651a7b740SScott Long * If the offset is beyond the current extent, look for the 143751a7b740SScott Long * next extent. 143851a7b740SScott Long */ 143951a7b740SScott Long do { 144051a7b740SScott Long offset -= icblen; 144151a7b740SScott Long ad_offset = sizeof(struct long_ad) * ad_num; 1442bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) { 144351a7b740SScott Long printf("File offset out of bounds\n"); 144451a7b740SScott Long return (EINVAL); 144551a7b740SScott Long } 1446bf1c3dddSScott Long icb = GETICB(long_ad, fentry, 1447bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset); 144851a7b740SScott Long icblen = GETICBLEN(long_ad, icb); 144951a7b740SScott Long ad_num++; 145051a7b740SScott Long } while(offset >= icblen); 145151a7b740SScott Long 145251a7b740SScott Long lsector = (offset >> udfmp->bshift) + 1453bf1c3dddSScott Long le32toh(((struct long_ad *)(icb))->loc.lb_num); 145451a7b740SScott Long 14555df29e0cSRemko Lodder *max_size = icblen - offset; 145651a7b740SScott Long 145751a7b740SScott Long break; 145851a7b740SScott Long case 3: 145951a7b740SScott Long /* 146051a7b740SScott Long * This type means that the file *data* is stored in the 146151a7b740SScott Long * allocation descriptor field of the file entry. 146251a7b740SScott Long */ 146351a7b740SScott Long *max_size = 0; 14648db4c2f2SScott Long *sector = node->hash_id + udfmp->part_start; 146551a7b740SScott Long 14664576293dSScott Long return (UDF_INVALID_BMAP); 146751a7b740SScott Long case 2: 146851a7b740SScott Long /* DirectCD does not use extended_ad's */ 146951a7b740SScott Long default: 147051a7b740SScott Long printf("Unsupported allocation descriptor %d\n", 147151a7b740SScott Long tag->flags & 0x7); 147251a7b740SScott Long return (ENODEV); 147351a7b740SScott Long } 147451a7b740SScott Long 147551a7b740SScott Long *sector = lsector + udfmp->part_start; 147651a7b740SScott Long 147751a7b740SScott Long /* 147851a7b740SScott Long * Check the sparing table. Each entry represents the beginning of 147951a7b740SScott Long * a packet. 148051a7b740SScott Long */ 148151a7b740SScott Long if (udfmp->s_table != NULL) { 148251a7b740SScott Long for (i = 0; i< udfmp->s_table_entries; i++) { 1483bf1c3dddSScott Long p_offset = 1484bf1c3dddSScott Long lsector - le32toh(udfmp->s_table->entries[i].org); 148551a7b740SScott Long if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) { 1486bf1c3dddSScott Long *sector = 1487bf1c3dddSScott Long le32toh(udfmp->s_table->entries[i].map) + 148851a7b740SScott Long p_offset; 148951a7b740SScott Long break; 149051a7b740SScott Long } 149151a7b740SScott Long } 149251a7b740SScott Long } 149351a7b740SScott Long 149451a7b740SScott Long return (0); 149551a7b740SScott Long } 1496