151a7b740SScott Long /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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
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 };
986fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(udf_vnodeops);
9951a7b740SScott Long
10061e69c80SJohn Baldwin struct vop_vector udf_fifoops = {
10161e69c80SJohn Baldwin .vop_default = &fifo_specops,
10261e69c80SJohn Baldwin .vop_access = udf_access,
10361e69c80SJohn Baldwin .vop_getattr = udf_getattr,
104b501cc5dSJohn Baldwin .vop_pathconf = udf_pathconf,
10561e69c80SJohn Baldwin .vop_print = udf_print,
10661e69c80SJohn Baldwin .vop_reclaim = udf_reclaim,
10761e69c80SJohn Baldwin .vop_setattr = udf_setattr,
10861e69c80SJohn Baldwin .vop_vptofh = udf_vptofh,
10961e69c80SJohn Baldwin };
1106fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(udf_fifoops);
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
udf_allocv(struct mount * mp,struct vnode ** vpp,struct thread * td)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
udf_permtomode(struct udf_node * node)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
udf_access(struct vop_access_args * a)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,
181d292b194SMateusz Guzik accmode, a->a_cred));
18251a7b740SScott Long }
18351a7b740SScott Long
18435e06624SPav Lucistnik static int
udf_open(struct vop_open_args * ap)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
udf_isaleapyear(int year)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
udf_timetotimespec(struct timestamp * time,struct timespec * t)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
udf_getattr(struct vop_getattr_args * a)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
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
udf_setattr(struct vop_setattr_args * a)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
udf_ioctl(struct vop_ioctl_args * a)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
udf_pathconf(struct vop_pathconf_args * a)38251a7b740SScott Long udf_pathconf(struct vop_pathconf_args *a)
38351a7b740SScott Long {
38451a7b740SScott Long
38551a7b740SScott Long switch (a->a_name) {
38661d53d8fSJohn Baldwin case _PC_FILESIZEBITS:
38761d53d8fSJohn Baldwin *a->a_retval = 64;
38861d53d8fSJohn Baldwin return (0);
38951a7b740SScott Long case _PC_LINK_MAX:
39051a7b740SScott Long *a->a_retval = 65535;
39151a7b740SScott Long return (0);
39251a7b740SScott Long case _PC_NAME_MAX:
39351a7b740SScott Long *a->a_retval = NAME_MAX;
39451a7b740SScott Long return (0);
39561d53d8fSJohn Baldwin case _PC_SYMLINK_MAX:
39661d53d8fSJohn Baldwin *a->a_retval = MAXPATHLEN;
39751a7b740SScott Long return (0);
39851a7b740SScott Long case _PC_NO_TRUNC:
39951a7b740SScott Long *a->a_retval = 1;
40051a7b740SScott Long return (0);
401b501cc5dSJohn Baldwin case _PC_PIPE_BUF:
402b501cc5dSJohn Baldwin if (a->a_vp->v_type == VDIR || a->a_vp->v_type == VFIFO) {
403b501cc5dSJohn Baldwin *a->a_retval = PIPE_BUF;
404b501cc5dSJohn Baldwin return (0);
405b501cc5dSJohn Baldwin }
406b501cc5dSJohn Baldwin return (EINVAL);
40751a7b740SScott Long default:
40861d53d8fSJohn Baldwin return (vop_stdpathconf(a));
40951a7b740SScott Long }
41051a7b740SScott Long }
41151a7b740SScott Long
41261e69c80SJohn Baldwin static int
udf_print(struct vop_print_args * ap)41361e69c80SJohn Baldwin udf_print(struct vop_print_args *ap)
41461e69c80SJohn Baldwin {
41561e69c80SJohn Baldwin struct vnode *vp = ap->a_vp;
41661e69c80SJohn Baldwin struct udf_node *node = VTON(vp);
41761e69c80SJohn Baldwin
41861e69c80SJohn Baldwin printf(" ino %lu, on dev %s", (u_long)node->hash_id,
41961e69c80SJohn Baldwin devtoname(node->udfmp->im_dev));
42061e69c80SJohn Baldwin if (vp->v_type == VFIFO)
42161e69c80SJohn Baldwin fifo_printinfo(vp);
42261e69c80SJohn Baldwin printf("\n");
42361e69c80SJohn Baldwin return (0);
42461e69c80SJohn Baldwin }
42561e69c80SJohn Baldwin
4260c09ac0dSPav Lucistnik #define lblkno(udfmp, loc) ((loc) >> (udfmp)->bshift)
4270c09ac0dSPav Lucistnik #define blkoff(udfmp, loc) ((loc) & (udfmp)->bmask)
4285792e04dSAndriy Gapon #define lblktosize(udfmp, blk) ((blk) << (udfmp)->bshift)
42951a7b740SScott Long
430be52a95dSAndriy Gapon static inline int
is_data_in_fentry(const struct udf_node * node)431be52a95dSAndriy Gapon is_data_in_fentry(const struct udf_node *node)
432be52a95dSAndriy Gapon {
433be52a95dSAndriy Gapon const struct file_entry *fentry = node->fentry;
434be52a95dSAndriy Gapon
435be52a95dSAndriy Gapon return ((le16toh(fentry->icbtag.flags) & 0x7) == 3);
436be52a95dSAndriy Gapon }
437be52a95dSAndriy Gapon
4380c09ac0dSPav Lucistnik static int
udf_read(struct vop_read_args * ap)4390c09ac0dSPav Lucistnik udf_read(struct vop_read_args *ap)
4400c09ac0dSPav Lucistnik {
4410c09ac0dSPav Lucistnik struct vnode *vp = ap->a_vp;
4420c09ac0dSPav Lucistnik struct uio *uio = ap->a_uio;
4430c09ac0dSPav Lucistnik struct udf_node *node = VTON(vp);
4440c09ac0dSPav Lucistnik struct udf_mnt *udfmp;
445be52a95dSAndriy Gapon struct file_entry *fentry;
4460c09ac0dSPav Lucistnik struct buf *bp;
447be52a95dSAndriy Gapon uint8_t *data;
4480c09ac0dSPav Lucistnik daddr_t lbn, rablock;
4490c09ac0dSPav Lucistnik off_t diff, fsize;
450526d0bd5SKonstantin Belousov ssize_t n;
4510c09ac0dSPav Lucistnik int error = 0;
452526d0bd5SKonstantin Belousov long size, on;
4530c09ac0dSPav Lucistnik
4540c09ac0dSPav Lucistnik if (uio->uio_resid == 0)
4550c09ac0dSPav Lucistnik return (0);
45651a7b740SScott Long if (uio->uio_offset < 0)
45751a7b740SScott Long return (EINVAL);
458be52a95dSAndriy Gapon
459be52a95dSAndriy Gapon if (is_data_in_fentry(node)) {
460be52a95dSAndriy Gapon fentry = node->fentry;
461be52a95dSAndriy Gapon data = &fentry->data[le32toh(fentry->l_ea)];
462be52a95dSAndriy Gapon fsize = le32toh(fentry->l_ad);
463be52a95dSAndriy Gapon
464be52a95dSAndriy Gapon n = uio->uio_resid;
465be52a95dSAndriy Gapon diff = fsize - uio->uio_offset;
466be52a95dSAndriy Gapon if (diff <= 0)
467be52a95dSAndriy Gapon return (0);
468be52a95dSAndriy Gapon if (diff < n)
469be52a95dSAndriy Gapon n = diff;
470be52a95dSAndriy Gapon error = uiomove(data + uio->uio_offset, (int)n, uio);
471be52a95dSAndriy Gapon return (error);
472be52a95dSAndriy Gapon }
473be52a95dSAndriy Gapon
474bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len);
4750c09ac0dSPav Lucistnik udfmp = node->udfmp;
4760c09ac0dSPav Lucistnik do {
4770c09ac0dSPav Lucistnik lbn = lblkno(udfmp, uio->uio_offset);
4780c09ac0dSPav Lucistnik on = blkoff(udfmp, uio->uio_offset);
4790c09ac0dSPav Lucistnik n = min((u_int)(udfmp->bsize - on),
4800c09ac0dSPav Lucistnik uio->uio_resid);
4810c09ac0dSPav Lucistnik diff = fsize - uio->uio_offset;
4820c09ac0dSPav Lucistnik if (diff <= 0)
4830c09ac0dSPav Lucistnik return (0);
4840c09ac0dSPav Lucistnik if (diff < n)
4850c09ac0dSPav Lucistnik n = diff;
4860c09ac0dSPav Lucistnik size = udfmp->bsize;
4870c09ac0dSPav Lucistnik rablock = lbn + 1;
4880c09ac0dSPav Lucistnik if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
4890c09ac0dSPav Lucistnik if (lblktosize(udfmp, rablock) < fsize) {
490c535690bSKonstantin Belousov error = cluster_read(vp, fsize, lbn, size,
491c535690bSKonstantin Belousov NOCRED, uio->uio_resid,
492c535690bSKonstantin Belousov (ap->a_ioflag >> 16), 0, &bp);
4930c09ac0dSPav Lucistnik } else {
4940c09ac0dSPav Lucistnik error = bread(vp, lbn, size, NOCRED, &bp);
4950c09ac0dSPav Lucistnik }
4960c09ac0dSPav Lucistnik } else {
4970c09ac0dSPav Lucistnik error = bread(vp, lbn, size, NOCRED, &bp);
4980c09ac0dSPav Lucistnik }
4991fa81dabSKonstantin Belousov if (error != 0) {
50051a7b740SScott Long brelse(bp);
5010c09ac0dSPav Lucistnik return (error);
5020c09ac0dSPav Lucistnik }
5031fa81dabSKonstantin Belousov n = min(n, size - bp->b_resid);
50451a7b740SScott Long
5050c09ac0dSPav Lucistnik error = uiomove(bp->b_data + on, (int)n, uio);
5060c09ac0dSPav Lucistnik brelse(bp);
5070c09ac0dSPav Lucistnik } while (error == 0 && uio->uio_resid > 0 && n != 0);
50851a7b740SScott Long return (error);
50951a7b740SScott Long }
51051a7b740SScott Long
51151a7b740SScott Long /*
51251a7b740SScott Long * Call the OSTA routines to translate the name from a CS0 dstring to a
51351a7b740SScott Long * 16-bit Unicode String. Hooks need to be placed in here to translate from
5146565282cSScott Long * Unicode to the encoding that the kernel/user expects. Return the length
5156565282cSScott Long * of the translated string.
51651a7b740SScott Long */
51751a7b740SScott Long static int
udf_transname(char * cs0string,char * destname,int len,struct udf_mnt * udfmp)5186565282cSScott Long udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
51951a7b740SScott Long {
52051a7b740SScott Long unicode_t *transname;
5216565282cSScott Long char *unibuf, *unip;
522181fc3c6SR. Imura int i, destlen;
523181fc3c6SR. Imura ssize_t unilen = 0;
5246565282cSScott Long size_t destleft = MAXNAMLEN;
52551a7b740SScott Long
5266565282cSScott Long /* Convert 16-bit Unicode to destname */
5276565282cSScott Long if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
5286565282cSScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */
5296565282cSScott Long unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
5306565282cSScott Long unip = unibuf;
531181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
5326565282cSScott Long printf("udf: Unicode translation failed\n");
5336565282cSScott Long uma_zfree(udf_zone_trans, unibuf);
5346565282cSScott Long return 0;
5356565282cSScott Long }
5366565282cSScott Long
5376565282cSScott Long while (unilen > 0 && destleft > 0) {
53838fc0aa4SDimitry Andric udf_iconv->conv(udfmp->im_d2l, __DECONST(const char **,
53938fc0aa4SDimitry Andric &unibuf), (size_t *)&unilen, (char **)&destname,
54038fc0aa4SDimitry Andric &destleft);
5416565282cSScott Long /* Unconverted character found */
5426565282cSScott Long if (unilen > 0 && destleft > 0) {
5436565282cSScott Long *destname++ = '?';
5446565282cSScott Long destleft--;
5456565282cSScott Long unibuf += 2;
5466565282cSScott Long unilen -= 2;
5476565282cSScott Long }
5486565282cSScott Long }
5496565282cSScott Long uma_zfree(udf_zone_trans, unip);
5506565282cSScott Long *destname = '\0';
5516565282cSScott Long destlen = MAXNAMLEN - (int)destleft;
5526565282cSScott Long } else {
55351a7b740SScott Long /* allocate a buffer big enough to hold an 8->16 bit expansion */
554a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK);
55551a7b740SScott Long
556181fc3c6SR. Imura if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) {
55751a7b740SScott Long printf("udf: Unicode translation failed\n");
55851a7b740SScott Long uma_zfree(udf_zone_trans, transname);
55951a7b740SScott Long return 0;
56051a7b740SScott Long }
56151a7b740SScott Long
56251a7b740SScott Long for (i = 0; i < unilen ; i++) {
56351a7b740SScott Long if (transname[i] & 0xff00) {
56451a7b740SScott Long destname[i] = '.'; /* Fudge the 16bit chars */
56551a7b740SScott Long } else {
56651a7b740SScott Long destname[i] = transname[i] & 0xff;
56751a7b740SScott Long }
56851a7b740SScott Long }
56951a7b740SScott Long uma_zfree(udf_zone_trans, transname);
5706565282cSScott Long destname[unilen] = 0;
571181fc3c6SR. Imura destlen = (int)unilen;
5726565282cSScott Long }
57351a7b740SScott Long
5746565282cSScott Long return (destlen);
57551a7b740SScott Long }
57651a7b740SScott Long
57751a7b740SScott Long /*
57851a7b740SScott Long * Compare a CS0 dstring with a name passed in from the VFS layer. Return
579befb7f33SChristian Brueffer * 0 on a successful match, nonzero otherwise. Unicode work may need to be done
58051a7b740SScott Long * here also.
58151a7b740SScott Long */
58251a7b740SScott Long static int
udf_cmpname(char * cs0string,char * cmpname,int cs0len,int cmplen,struct udf_mnt * udfmp)5836565282cSScott Long udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
58451a7b740SScott Long {
58595ec5961SScott Long char *transname;
58695ec5961SScott Long int error = 0;
58751a7b740SScott Long
58895ec5961SScott Long /* This is overkill, but not worth creating a new zone */
589a163d034SWarner Losh transname = uma_zalloc(udf_zone_trans, M_WAITOK);
59095ec5961SScott Long
5916565282cSScott Long cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
59251a7b740SScott Long
59351a7b740SScott Long /* Easy check. If they aren't the same length, they aren't equal */
59495ec5961SScott Long if ((cs0len == 0) || (cs0len != cmplen))
59595ec5961SScott Long error = -1;
59695ec5961SScott Long else
59795ec5961SScott Long error = bcmp(transname, cmpname, cmplen);
59851a7b740SScott Long
59995ec5961SScott Long uma_zfree(udf_zone_trans, transname);
60095ec5961SScott Long return (error);
60151a7b740SScott Long }
60251a7b740SScott Long
60351a7b740SScott Long struct udf_uiodir {
60451a7b740SScott Long struct dirent *dirent;
605b214fcceSAlan Somers uint64_t *cookies;
60651a7b740SScott Long int ncookies;
60751a7b740SScott Long int acookies;
60851a7b740SScott Long int eofflag;
60951a7b740SScott Long };
61051a7b740SScott Long
61151a7b740SScott Long static int
udf_uiodir(struct udf_uiodir * uiodir,int de_size,struct uio * uio,long cookie)61251a7b740SScott Long udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
61351a7b740SScott Long {
61451a7b740SScott Long if (uiodir->cookies != NULL) {
61551a7b740SScott Long if (++uiodir->acookies > uiodir->ncookies) {
61651a7b740SScott Long uiodir->eofflag = 0;
61751a7b740SScott Long return (-1);
61851a7b740SScott Long }
61951a7b740SScott Long *uiodir->cookies++ = cookie;
62051a7b740SScott Long }
62151a7b740SScott Long
62251a7b740SScott Long if (uio->uio_resid < de_size) {
62351a7b740SScott Long uiodir->eofflag = 0;
62451a7b740SScott Long return (-1);
62551a7b740SScott Long }
62651a7b740SScott Long
627c9524588SDag-Erling Smørgrav return (uiomove(uiodir->dirent, de_size, uio));
62851a7b740SScott Long }
62951a7b740SScott Long
6301703656aSScott Long static struct udf_dirstream *
udf_opendir(struct udf_node * node,int offset,int fsize,struct udf_mnt * udfmp)6311703656aSScott Long udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
6321703656aSScott Long {
6331703656aSScott Long struct udf_dirstream *ds;
6341703656aSScott Long
635a163d034SWarner Losh ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
6361703656aSScott Long
6371703656aSScott Long ds->node = node;
6381703656aSScott Long ds->offset = offset;
6391703656aSScott Long ds->udfmp = udfmp;
6401703656aSScott Long ds->fsize = fsize;
6411703656aSScott Long
6421703656aSScott Long return (ds);
6431703656aSScott Long }
6441703656aSScott Long
6451703656aSScott Long static struct fileid_desc *
udf_getfid(struct udf_dirstream * ds)6461703656aSScott Long udf_getfid(struct udf_dirstream *ds)
6471703656aSScott Long {
6481703656aSScott Long struct fileid_desc *fid;
6491703656aSScott Long int error, frag_size = 0, total_fid_size;
6501703656aSScott Long
6511703656aSScott Long /* End of directory? */
6521703656aSScott Long if (ds->offset + ds->off >= ds->fsize) {
6531703656aSScott Long ds->error = 0;
6541703656aSScott Long return (NULL);
6551703656aSScott Long }
6561703656aSScott Long
6571703656aSScott Long /* Grab the first extent of the directory */
6581703656aSScott Long if (ds->off == 0) {
6591703656aSScott Long ds->size = 0;
6601703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset,
6611703656aSScott Long &ds->bp, &ds->data);
6621703656aSScott Long if (error) {
6631703656aSScott Long ds->error = error;
664744bb56dSScott Long if (ds->bp != NULL)
665744bb56dSScott Long brelse(ds->bp);
6661703656aSScott Long return (NULL);
6671703656aSScott Long }
6681703656aSScott Long }
6691703656aSScott Long
6704576293dSScott Long /*
6714576293dSScott Long * Clean up from a previous fragmented FID.
6724576293dSScott Long * XXX Is this the right place for this?
6734576293dSScott Long */
6741703656aSScott Long if (ds->fid_fragment && ds->buf != NULL) {
6751703656aSScott Long ds->fid_fragment = 0;
6761ede983cSDag-Erling Smørgrav free(ds->buf, M_UDFFID);
6771703656aSScott Long }
6781703656aSScott Long
6791703656aSScott Long fid = (struct fileid_desc*)&ds->data[ds->off];
6801703656aSScott Long
6811703656aSScott Long /*
6821703656aSScott Long * Check to see if the fid is fragmented. The first test
6831703656aSScott Long * ensures that we don't wander off the end of the buffer
6841703656aSScott Long * looking for the l_iu and l_fi fields.
6851703656aSScott Long */
6861703656aSScott Long if (ds->off + UDF_FID_SIZE > ds->size ||
687bf1c3dddSScott Long ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){
6881703656aSScott Long /* Copy what we have of the fid into a buffer */
6891703656aSScott Long frag_size = ds->size - ds->off;
6901703656aSScott Long if (frag_size >= ds->udfmp->bsize) {
6911703656aSScott Long printf("udf: invalid FID fragment\n");
6921703656aSScott Long ds->error = EINVAL;
6931703656aSScott Long return (NULL);
6941703656aSScott Long }
6951703656aSScott Long
6961703656aSScott Long /*
6971703656aSScott Long * File ID descriptors can only be at most one
6981703656aSScott Long * logical sector in size.
6991703656aSScott Long */
7001ede983cSDag-Erling Smørgrav ds->buf = malloc(ds->udfmp->bsize, M_UDFFID,
701a163d034SWarner Losh M_WAITOK | M_ZERO);
7021703656aSScott Long bcopy(fid, ds->buf, frag_size);
7031703656aSScott Long
7041703656aSScott Long /* Reduce all of the casting magic */
7051703656aSScott Long fid = (struct fileid_desc*)ds->buf;
7061703656aSScott Long
7071703656aSScott Long if (ds->bp != NULL)
7081703656aSScott Long brelse(ds->bp);
7091703656aSScott Long
7101703656aSScott Long /* Fetch the next allocation */
7111703656aSScott Long ds->offset += ds->size;
7121703656aSScott Long ds->size = 0;
7131703656aSScott Long error = udf_readatoffset(ds->node, &ds->size, ds->offset,
7141703656aSScott Long &ds->bp, &ds->data);
7151703656aSScott Long if (error) {
7161703656aSScott Long ds->error = error;
7171703656aSScott Long return (NULL);
7181703656aSScott Long }
7191703656aSScott Long
7201703656aSScott Long /*
7211703656aSScott Long * If the fragment was so small that we didn't get
7221703656aSScott Long * the l_iu and l_fi fields, copy those in.
7231703656aSScott Long */
7241703656aSScott Long if (frag_size < UDF_FID_SIZE)
7251703656aSScott Long bcopy(ds->data, &ds->buf[frag_size],
7261703656aSScott Long UDF_FID_SIZE - frag_size);
7271703656aSScott Long
7281703656aSScott Long /*
7291703656aSScott Long * Now that we have enough of the fid to work with,
7301703656aSScott Long * copy in the rest of the fid from the new
7311703656aSScott Long * allocation.
7321703656aSScott Long */
733bf1c3dddSScott Long total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi;
7341703656aSScott Long if (total_fid_size > ds->udfmp->bsize) {
7351703656aSScott Long printf("udf: invalid FID\n");
7361703656aSScott Long ds->error = EIO;
7371703656aSScott Long return (NULL);
7381703656aSScott Long }
7391703656aSScott Long bcopy(ds->data, &ds->buf[frag_size],
7401703656aSScott Long total_fid_size - frag_size);
7411703656aSScott Long
7421703656aSScott Long ds->fid_fragment = 1;
7431703656aSScott Long } else {
744bf1c3dddSScott Long total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE;
7451703656aSScott Long }
7461703656aSScott Long
7471703656aSScott Long /*
7481703656aSScott Long * Update the offset. Align on a 4 byte boundary because the
7494576293dSScott Long * UDF spec says so.
7501703656aSScott Long */
751159da68bSAndriy Gapon ds->this_off = ds->offset + ds->off;
7521703656aSScott Long if (!ds->fid_fragment) {
7531703656aSScott Long ds->off += (total_fid_size + 3) & ~0x03;
7541703656aSScott Long } else {
7551703656aSScott Long ds->off = (total_fid_size - frag_size + 3) & ~0x03;
7561703656aSScott Long }
7571703656aSScott Long
7581703656aSScott Long return (fid);
7591703656aSScott Long }
7601703656aSScott Long
7611703656aSScott Long static void
udf_closedir(struct udf_dirstream * ds)7621703656aSScott Long udf_closedir(struct udf_dirstream *ds)
7631703656aSScott Long {
7641703656aSScott Long
7651703656aSScott Long if (ds->bp != NULL)
7661703656aSScott Long brelse(ds->bp);
7671703656aSScott Long
7681703656aSScott Long if (ds->fid_fragment && ds->buf != NULL)
7691ede983cSDag-Erling Smørgrav free(ds->buf, M_UDFFID);
7701703656aSScott Long
7711703656aSScott Long uma_zfree(udf_zone_ds, ds);
7721703656aSScott Long }
7731703656aSScott Long
77451a7b740SScott Long static int
udf_readdir(struct vop_readdir_args * a)77551a7b740SScott Long udf_readdir(struct vop_readdir_args *a)
77651a7b740SScott Long {
77751a7b740SScott Long struct vnode *vp;
77851a7b740SScott Long struct uio *uio;
77951a7b740SScott Long struct dirent dir;
78051a7b740SScott Long struct udf_node *node;
7816565282cSScott Long struct udf_mnt *udfmp;
78251a7b740SScott Long struct fileid_desc *fid;
78351a7b740SScott Long struct udf_uiodir uiodir;
7841703656aSScott Long struct udf_dirstream *ds;
785b214fcceSAlan Somers uint64_t *cookies = NULL;
78651a7b740SScott Long int ncookies;
787c8eeea2fSScott Long int error = 0;
78851a7b740SScott Long
78951a7b740SScott Long vp = a->a_vp;
79051a7b740SScott Long uio = a->a_uio;
79151a7b740SScott Long node = VTON(vp);
7926565282cSScott Long udfmp = node->udfmp;
79351a7b740SScott Long uiodir.eofflag = 1;
79451a7b740SScott Long
79551a7b740SScott Long if (a->a_ncookies != NULL) {
79651a7b740SScott Long /*
79751a7b740SScott Long * Guess how many entries are needed. If we run out, this
79851a7b740SScott Long * function will be called again and thing will pick up were
79951a7b740SScott Long * it left off.
80051a7b740SScott Long */
80151a7b740SScott Long ncookies = uio->uio_resid / 8;
802b214fcceSAlan Somers cookies = malloc(sizeof(*cookies) * ncookies, M_TEMP, M_WAITOK);
80351a7b740SScott Long uiodir.ncookies = ncookies;
80451a7b740SScott Long uiodir.cookies = cookies;
80551a7b740SScott Long uiodir.acookies = 0;
80651a7b740SScott Long } else {
80751a7b740SScott Long uiodir.cookies = NULL;
80851a7b740SScott Long }
80951a7b740SScott Long
81051a7b740SScott Long /*
81151a7b740SScott Long * Iterate through the file id descriptors. Give the parent dir
8124576293dSScott Long * entry special attention.
81351a7b740SScott Long */
814bf1c3dddSScott Long ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
8151703656aSScott Long node->udfmp);
81651a7b740SScott Long
8171703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) {
81851a7b740SScott Long /* XXX Should we return an error on a bad fid? */
81951a7b740SScott Long if (udf_checktag(&fid->tag, TAGID_FID)) {
82051a7b740SScott Long printf("Invalid FID tag\n");
82177411499SScott Long hexdump(fid, UDF_FID_SIZE, NULL, 0);
8221703656aSScott Long error = EIO;
82351a7b740SScott Long break;
82451a7b740SScott Long }
82551a7b740SScott Long
82651a7b740SScott Long /* Is this a deleted file? */
8272bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL)
8281703656aSScott Long continue;
82951a7b740SScott Long
8302bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
83151a7b740SScott Long /* Do up the '.' and '..' entries. Dummy values are
83251a7b740SScott Long * used for the cookies since the offset here is
83351a7b740SScott Long * usually zero, and NFS doesn't like that value
83451a7b740SScott Long */
835c8eeea2fSScott Long dir.d_fileno = node->hash_id;
836c8eeea2fSScott Long dir.d_type = DT_DIR;
837c8eeea2fSScott Long dir.d_name[0] = '.';
838c8eeea2fSScott Long dir.d_namlen = 1;
839c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir);
8401c4ca778SKonstantin Belousov dir.d_off = 1;
8416d2e2df7SMark Johnston dirent_terminate(&dir);
842c8eeea2fSScott Long uiodir.dirent = &dir;
843c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
84451a7b740SScott Long if (error)
84551a7b740SScott Long break;
84651a7b740SScott Long
847c8eeea2fSScott Long dir.d_fileno = udf_getid(&fid->icb);
848c8eeea2fSScott Long dir.d_type = DT_DIR;
849c8eeea2fSScott Long dir.d_name[0] = '.';
850c8eeea2fSScott Long dir.d_name[1] = '.';
851c8eeea2fSScott Long dir.d_namlen = 2;
852c8eeea2fSScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir);
8531c4ca778SKonstantin Belousov dir.d_off = 2;
8546d2e2df7SMark Johnston dirent_terminate(&dir);
855c8eeea2fSScott Long uiodir.dirent = &dir;
856c8eeea2fSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
85751a7b740SScott Long } else {
85851a7b740SScott Long dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
8596565282cSScott Long &dir.d_name[0], fid->l_fi, udfmp);
86051a7b740SScott Long dir.d_fileno = udf_getid(&fid->icb);
8612bbe0d36SScott Long dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
8622bbe0d36SScott Long DT_DIR : DT_UNKNOWN;
86351a7b740SScott Long dir.d_reclen = GENERIC_DIRSIZ(&dir);
8641c4ca778SKonstantin Belousov dir.d_off = ds->this_off;
8656d2e2df7SMark Johnston dirent_terminate(&dir);
86651a7b740SScott Long uiodir.dirent = &dir;
8671703656aSScott Long error = udf_uiodir(&uiodir, dir.d_reclen, uio,
8681703656aSScott Long ds->this_off);
86951a7b740SScott Long }
87084206c74SAndriy Gapon if (error)
87151a7b740SScott Long break;
872ff9e355bSAndriy Gapon uio->uio_offset = ds->offset + ds->off;
87351a7b740SScott Long }
87451a7b740SScott Long
87551a7b740SScott Long /* tell the calling layer whether we need to be called again */
87651a7b740SScott Long *a->a_eofflag = uiodir.eofflag;
87751a7b740SScott Long
87884206c74SAndriy Gapon if (error < 0)
87984206c74SAndriy Gapon error = 0;
8801703656aSScott Long if (!error)
8811703656aSScott Long error = ds->error;
8821703656aSScott Long
8831703656aSScott Long udf_closedir(ds);
88451a7b740SScott Long
88551a7b740SScott Long if (a->a_ncookies != NULL) {
88651a7b740SScott Long if (error)
8871ede983cSDag-Erling Smørgrav free(cookies, M_TEMP);
88851a7b740SScott Long else {
88951a7b740SScott Long *a->a_ncookies = uiodir.acookies;
89051a7b740SScott Long *a->a_cookies = cookies;
89151a7b740SScott Long }
89251a7b740SScott Long }
89351a7b740SScott Long
89451a7b740SScott Long return (error);
89551a7b740SScott Long }
89651a7b740SScott Long
89751a7b740SScott Long static int
udf_readlink(struct vop_readlink_args * ap)89851a7b740SScott Long udf_readlink(struct vop_readlink_args *ap)
89951a7b740SScott Long {
900e3024df2SJohn Baldwin struct path_component *pc, *end;
901e3024df2SJohn Baldwin struct vnode *vp;
902e3024df2SJohn Baldwin struct uio uio;
903e3024df2SJohn Baldwin struct iovec iov[1];
904e3024df2SJohn Baldwin struct udf_node *node;
905e3024df2SJohn Baldwin void *buf;
906e3024df2SJohn Baldwin char *cp;
907e3024df2SJohn Baldwin int error, len, root;
908e3024df2SJohn Baldwin
909e3024df2SJohn Baldwin /*
910e3024df2SJohn Baldwin * A symbolic link in UDF is a list of variable-length path
911e3024df2SJohn Baldwin * component structures. We build a pathname in the caller's
912e3024df2SJohn Baldwin * uio by traversing this list.
913e3024df2SJohn Baldwin */
914e3024df2SJohn Baldwin vp = ap->a_vp;
915e3024df2SJohn Baldwin node = VTON(vp);
916e3024df2SJohn Baldwin len = le64toh(node->fentry->inf_len);
91712b3a08dSAndriy Gapon buf = malloc(len, M_DEVBUF, M_WAITOK);
9186b3ee248SAndriy Gapon iov[0].iov_len = len;
919e3024df2SJohn Baldwin iov[0].iov_base = buf;
920e3024df2SJohn Baldwin uio.uio_iov = iov;
921e3024df2SJohn Baldwin uio.uio_iovcnt = 1;
922e3024df2SJohn Baldwin uio.uio_offset = 0;
923e3024df2SJohn Baldwin uio.uio_resid = iov[0].iov_len;
924e3024df2SJohn Baldwin uio.uio_segflg = UIO_SYSSPACE;
925e3024df2SJohn Baldwin uio.uio_rw = UIO_READ;
926e3024df2SJohn Baldwin uio.uio_td = curthread;
927e3024df2SJohn Baldwin error = VOP_READ(vp, &uio, 0, ap->a_cred);
928e3024df2SJohn Baldwin if (error)
929e3024df2SJohn Baldwin goto error;
930e3024df2SJohn Baldwin
931e3024df2SJohn Baldwin pc = buf;
932e3024df2SJohn Baldwin end = (void *)((char *)buf + len);
933e3024df2SJohn Baldwin root = 0;
934e3024df2SJohn Baldwin while (pc < end) {
935e3024df2SJohn Baldwin switch (pc->type) {
936e3024df2SJohn Baldwin case UDF_PATH_ROOT:
937e3024df2SJohn Baldwin /* Only allow this at the beginning of a path. */
938e3024df2SJohn Baldwin if ((void *)pc != buf) {
939e3024df2SJohn Baldwin error = EINVAL;
940e3024df2SJohn Baldwin goto error;
941e3024df2SJohn Baldwin }
942e3024df2SJohn Baldwin cp = "/";
943e3024df2SJohn Baldwin len = 1;
944e3024df2SJohn Baldwin root = 1;
945e3024df2SJohn Baldwin break;
946e3024df2SJohn Baldwin case UDF_PATH_DOT:
947e3024df2SJohn Baldwin cp = ".";
948e3024df2SJohn Baldwin len = 1;
949e3024df2SJohn Baldwin break;
950e3024df2SJohn Baldwin case UDF_PATH_DOTDOT:
951e3024df2SJohn Baldwin cp = "..";
952e3024df2SJohn Baldwin len = 2;
953e3024df2SJohn Baldwin break;
954e3024df2SJohn Baldwin case UDF_PATH_PATH:
955e3024df2SJohn Baldwin if (pc->length == 0) {
956e3024df2SJohn Baldwin error = EINVAL;
957e3024df2SJohn Baldwin goto error;
958e3024df2SJohn Baldwin }
959e3024df2SJohn Baldwin /*
960e3024df2SJohn Baldwin * XXX: We only support CS8 which appears to map
961e3024df2SJohn Baldwin * to ASCII directly.
962e3024df2SJohn Baldwin */
963e3024df2SJohn Baldwin switch (pc->identifier[0]) {
964e3024df2SJohn Baldwin case 8:
965e3024df2SJohn Baldwin cp = pc->identifier + 1;
966e3024df2SJohn Baldwin len = pc->length - 1;
967e3024df2SJohn Baldwin break;
968e3024df2SJohn Baldwin default:
969e3024df2SJohn Baldwin error = EOPNOTSUPP;
970e3024df2SJohn Baldwin goto error;
971e3024df2SJohn Baldwin }
972e3024df2SJohn Baldwin break;
973e3024df2SJohn Baldwin default:
974e3024df2SJohn Baldwin error = EINVAL;
975e3024df2SJohn Baldwin goto error;
976e3024df2SJohn Baldwin }
977e3024df2SJohn Baldwin
978e3024df2SJohn Baldwin /*
979e3024df2SJohn Baldwin * If this is not the first component, insert a path
980e3024df2SJohn Baldwin * separator.
981e3024df2SJohn Baldwin */
982e3024df2SJohn Baldwin if (pc != buf) {
983e3024df2SJohn Baldwin /* If we started with root we already have a "/". */
984e3024df2SJohn Baldwin if (root)
985e3024df2SJohn Baldwin goto skipslash;
986e3024df2SJohn Baldwin root = 0;
987e3024df2SJohn Baldwin if (ap->a_uio->uio_resid < 1) {
988e3024df2SJohn Baldwin error = ENAMETOOLONG;
989e3024df2SJohn Baldwin goto error;
990e3024df2SJohn Baldwin }
991e3024df2SJohn Baldwin error = uiomove("/", 1, ap->a_uio);
992e3024df2SJohn Baldwin if (error)
993e3024df2SJohn Baldwin break;
994e3024df2SJohn Baldwin }
995e3024df2SJohn Baldwin skipslash:
996e3024df2SJohn Baldwin
997e3024df2SJohn Baldwin /* Append string at 'cp' of length 'len' to our path. */
998e3024df2SJohn Baldwin if (len > ap->a_uio->uio_resid) {
999e3024df2SJohn Baldwin error = ENAMETOOLONG;
1000e3024df2SJohn Baldwin goto error;
1001e3024df2SJohn Baldwin }
1002e3024df2SJohn Baldwin error = uiomove(cp, len, ap->a_uio);
1003e3024df2SJohn Baldwin if (error)
1004e3024df2SJohn Baldwin break;
1005e3024df2SJohn Baldwin
1006e3024df2SJohn Baldwin /* Advance to next component. */
1007e3024df2SJohn Baldwin pc = (void *)((char *)pc + 4 + pc->length);
1008e3024df2SJohn Baldwin }
1009e3024df2SJohn Baldwin error:
1010e3024df2SJohn Baldwin free(buf, M_DEVBUF);
1011e3024df2SJohn Baldwin return (error);
101251a7b740SScott Long }
101351a7b740SScott Long
101451a7b740SScott Long static int
udf_strategy(struct vop_strategy_args * a)101551a7b740SScott Long udf_strategy(struct vop_strategy_args *a)
101651a7b740SScott Long {
101751a7b740SScott Long struct buf *bp;
101851a7b740SScott Long struct vnode *vp;
101951a7b740SScott Long struct udf_node *node;
1020429c018aSPoul-Henning Kamp struct bufobj *bo;
10215792e04dSAndriy Gapon off_t offset;
10225792e04dSAndriy Gapon uint32_t maxsize;
10235792e04dSAndriy Gapon daddr_t sector;
10245792e04dSAndriy Gapon int error;
102551a7b740SScott Long
102651a7b740SScott Long bp = a->a_bp;
1027d83b7498SPoul-Henning Kamp vp = a->a_vp;
102851a7b740SScott Long node = VTON(vp);
102951a7b740SScott Long
10300c09ac0dSPav Lucistnik if (bp->b_blkno == bp->b_lblkno) {
10315792e04dSAndriy Gapon offset = lblktosize(node->udfmp, bp->b_lblkno);
10325792e04dSAndriy Gapon error = udf_bmap_internal(node, offset, §or, &maxsize);
10335792e04dSAndriy Gapon if (error) {
103451a7b740SScott Long clrbuf(bp);
103551a7b740SScott Long bp->b_blkno = -1;
103651a7b740SScott Long bufdone(bp);
103751a7b740SScott Long return (0);
103851a7b740SScott Long }
10395792e04dSAndriy Gapon /* bmap gives sector numbers, bio works with device blocks */
10405792e04dSAndriy Gapon bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT);
10415792e04dSAndriy Gapon }
1042429c018aSPoul-Henning Kamp bo = node->udfmp->im_bo;
10432c18019fSPoul-Henning Kamp bp->b_iooffset = dbtob(bp->b_blkno);
10440391e5a1SPoul-Henning Kamp BO_STRATEGY(bo, bp);
104551a7b740SScott Long return (0);
104651a7b740SScott Long }
104751a7b740SScott Long
104851a7b740SScott Long static int
udf_bmap(struct vop_bmap_args * a)104951a7b740SScott Long udf_bmap(struct vop_bmap_args *a)
105051a7b740SScott Long {
105151a7b740SScott Long struct udf_node *node;
1052c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size;
105398b0c789SPoul-Henning Kamp daddr_t lsector;
1054fb2a76ccSAndriy Gapon int nblk;
105551a7b740SScott Long int error;
105651a7b740SScott Long
105751a7b740SScott Long node = VTON(a->a_vp);
105851a7b740SScott Long
10599c83534dSPoul-Henning Kamp if (a->a_bop != NULL)
1060e0251bbbSPoul-Henning Kamp *a->a_bop = &node->udfmp->im_devvp->v_bufobj;
106151a7b740SScott Long if (a->a_bnp == NULL)
106251a7b740SScott Long return (0);
106351a7b740SScott Long if (a->a_runb)
106451a7b740SScott Long *a->a_runb = 0;
106551a7b740SScott Long
106682467096SAndriy Gapon /*
106782467096SAndriy Gapon * UDF_INVALID_BMAP means data embedded into fentry, this is an internal
106882467096SAndriy Gapon * error that should not be propagated to calling code.
106982467096SAndriy Gapon * Most obvious mapping for this error is EOPNOTSUPP as we can not truly
107082467096SAndriy Gapon * translate block numbers in this case.
107182467096SAndriy Gapon * Incidentally, this return code will make vnode pager to use VOP_READ
107282467096SAndriy Gapon * to get data for mmap-ed pages and udf_read knows how to do the right
107382467096SAndriy Gapon * thing for this kind of files.
107482467096SAndriy Gapon */
107582467096SAndriy Gapon error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift,
107682467096SAndriy Gapon &lsector, &max_size);
107782467096SAndriy Gapon if (error == UDF_INVALID_BMAP)
107882467096SAndriy Gapon return (EOPNOTSUPP);
10798db4c2f2SScott Long if (error)
108051a7b740SScott Long return (error);
108151a7b740SScott Long
1082cd1b1a1dSScott Long /* Translate logical to physical sector number */
1083cd1b1a1dSScott Long *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
1084cd1b1a1dSScott Long
1085fb2a76ccSAndriy Gapon /*
1086fb2a76ccSAndriy Gapon * Determine maximum number of readahead blocks following the
1087fb2a76ccSAndriy Gapon * requested block.
1088fb2a76ccSAndriy Gapon */
1089fb2a76ccSAndriy Gapon if (a->a_runp) {
1090fb2a76ccSAndriy Gapon nblk = (max_size >> node->udfmp->bshift) - 1;
1091fb2a76ccSAndriy Gapon if (nblk <= 0)
109251a7b740SScott Long *a->a_runp = 0;
1093fb2a76ccSAndriy Gapon else if (nblk >= (MAXBSIZE >> node->udfmp->bshift))
1094fb2a76ccSAndriy Gapon *a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1;
1095fb2a76ccSAndriy Gapon else
1096fb2a76ccSAndriy Gapon *a->a_runp = nblk;
1097fb2a76ccSAndriy Gapon }
1098fb2a76ccSAndriy Gapon
1099fb2a76ccSAndriy Gapon if (a->a_runb) {
1100fb2a76ccSAndriy Gapon *a->a_runb = 0;
1101fb2a76ccSAndriy Gapon }
110251a7b740SScott Long
110351a7b740SScott Long return (0);
110451a7b740SScott Long }
110551a7b740SScott Long
110651a7b740SScott Long /*
110751a7b740SScott Long * The all powerful VOP_LOOKUP().
110851a7b740SScott Long */
110951a7b740SScott Long static int
udf_lookup(struct vop_cachedlookup_args * a)111051a7b740SScott Long udf_lookup(struct vop_cachedlookup_args *a)
111151a7b740SScott Long {
111251a7b740SScott Long struct vnode *dvp;
111351a7b740SScott Long struct vnode *tdp = NULL;
111451a7b740SScott Long struct vnode **vpp = a->a_vpp;
111551a7b740SScott Long struct udf_node *node;
111651a7b740SScott Long struct udf_mnt *udfmp;
111751a7b740SScott Long struct fileid_desc *fid = NULL;
11181703656aSScott Long struct udf_dirstream *ds;
111951a7b740SScott Long u_long nameiop;
112051a7b740SScott Long u_long flags;
112151a7b740SScott Long char *nameptr;
112251a7b740SScott Long long namelen;
112351a7b740SScott Long ino_t id = 0;
11241703656aSScott Long int offset, error = 0;
11254ad0d60bSJohn Baldwin int fsize, lkflags, ltype, numdirpasses;
112651a7b740SScott Long
112751a7b740SScott Long dvp = a->a_dvp;
112851a7b740SScott Long node = VTON(dvp);
112951a7b740SScott Long udfmp = node->udfmp;
113051a7b740SScott Long nameiop = a->a_cnp->cn_nameiop;
113151a7b740SScott Long flags = a->a_cnp->cn_flags;
11324ad0d60bSJohn Baldwin lkflags = a->a_cnp->cn_lkflags;
113351a7b740SScott Long nameptr = a->a_cnp->cn_nameptr;
113451a7b740SScott Long namelen = a->a_cnp->cn_namelen;
1135bf1c3dddSScott Long fsize = le64toh(node->fentry->inf_len);
113651a7b740SScott Long
113751a7b740SScott Long /*
113851a7b740SScott Long * If this is a LOOKUP and we've already partially searched through
113951a7b740SScott Long * the directory, pick up where we left off and flag that the
114051a7b740SScott Long * directory may need to be searched twice. For a full description,
11414b12bb04SMaxim Konovalov * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
114251a7b740SScott Long */
11431703656aSScott Long if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
114451a7b740SScott Long offset = 0;
114551a7b740SScott Long numdirpasses = 1;
114651a7b740SScott Long } else {
114751a7b740SScott Long offset = node->diroff;
114851a7b740SScott Long numdirpasses = 2;
114951a7b740SScott Long nchstats.ncs_2passes++;
115051a7b740SScott Long }
115151a7b740SScott Long
115251a7b740SScott Long lookloop:
11531703656aSScott Long ds = udf_opendir(node, offset, fsize, udfmp);
115451a7b740SScott Long
11551703656aSScott Long while ((fid = udf_getfid(ds)) != NULL) {
115651a7b740SScott Long /* XXX Should we return an error on a bad fid? */
11571703656aSScott Long if (udf_checktag(&fid->tag, TAGID_FID)) {
11581703656aSScott Long printf("udf_lookup: Invalid tag\n");
11591703656aSScott Long error = EIO;
11601703656aSScott Long break;
11611703656aSScott Long }
1162678d5effSScott Long
1163678d5effSScott Long /* Is this a deleted file? */
11642bbe0d36SScott Long if (fid->file_char & UDF_FILE_CHAR_DEL)
11651703656aSScott Long continue;
116651a7b740SScott Long
11672bbe0d36SScott Long if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
116851a7b740SScott Long if (flags & ISDOTDOT) {
116951a7b740SScott Long id = udf_getid(&fid->icb);
117051a7b740SScott Long break;
117151a7b740SScott Long }
117251a7b740SScott Long } else {
117351a7b740SScott Long if (!(udf_cmpname(&fid->data[fid->l_iu],
11746565282cSScott Long nameptr, fid->l_fi, namelen, udfmp))) {
117551a7b740SScott Long id = udf_getid(&fid->icb);
117651a7b740SScott Long break;
117751a7b740SScott Long }
117851a7b740SScott Long }
117951a7b740SScott Long }
11801703656aSScott Long
11811703656aSScott Long if (!error)
11821703656aSScott Long error = ds->error;
11831703656aSScott Long
11841703656aSScott Long /* XXX Bail out here? */
11851703656aSScott Long if (error) {
11861703656aSScott Long udf_closedir(ds);
11871703656aSScott Long return (error);
118851a7b740SScott Long }
118951a7b740SScott Long
119051a7b740SScott Long /* Did we have a match? */
119151a7b740SScott Long if (id) {
11921703656aSScott Long /*
11931703656aSScott Long * Remember where this entry was if it's the final
11941703656aSScott Long * component.
11951703656aSScott Long */
119651a7b740SScott Long if ((flags & ISLASTCN) && nameiop == LOOKUP)
11971703656aSScott Long node->diroff = ds->offset + ds->off;
119851a7b740SScott Long if (numdirpasses == 2)
119951a7b740SScott Long nchstats.ncs_pass2++;
12004ad0d60bSJohn Baldwin udf_closedir(ds);
12014ad0d60bSJohn Baldwin
12024ad0d60bSJohn Baldwin if (flags & ISDOTDOT) {
12034ad0d60bSJohn Baldwin error = vn_vget_ino(dvp, id, lkflags, &tdp);
12044ad0d60bSJohn Baldwin } else if (node->hash_id == id) {
12054ad0d60bSJohn Baldwin VREF(dvp); /* we want ourself, ie "." */
12064ad0d60bSJohn Baldwin /*
12074ad0d60bSJohn Baldwin * When we lookup "." we still can be asked to lock it
12084ad0d60bSJohn Baldwin * differently.
12094ad0d60bSJohn Baldwin */
12104ad0d60bSJohn Baldwin ltype = lkflags & LK_TYPE_MASK;
12114ad0d60bSJohn Baldwin if (ltype != VOP_ISLOCKED(dvp)) {
12124ad0d60bSJohn Baldwin if (ltype == LK_EXCLUSIVE)
12134ad0d60bSJohn Baldwin vn_lock(dvp, LK_UPGRADE | LK_RETRY);
12144ad0d60bSJohn Baldwin else /* if (ltype == LK_SHARED) */
12154ad0d60bSJohn Baldwin vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
12164ad0d60bSJohn Baldwin }
12174ad0d60bSJohn Baldwin tdp = dvp;
12184ad0d60bSJohn Baldwin } else
12194ad0d60bSJohn Baldwin error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp);
12204ad0d60bSJohn Baldwin if (!error) {
122151a7b740SScott Long *vpp = tdp;
122251a7b740SScott Long /* Put this entry in the cache */
122351a7b740SScott Long if (flags & MAKEENTRY)
122451a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp);
12254585e3acSJeff Roberson }
12261703656aSScott Long } else {
122751a7b740SScott Long /* Name wasn't found on this pass. Do another pass? */
122851a7b740SScott Long if (numdirpasses == 2) {
122951a7b740SScott Long numdirpasses--;
123051a7b740SScott Long offset = 0;
12311703656aSScott Long udf_closedir(ds);
123251a7b740SScott Long goto lookloop;
123351a7b740SScott Long }
12344ad0d60bSJohn Baldwin udf_closedir(ds);
123551a7b740SScott Long
123651a7b740SScott Long /* Enter name into cache as non-existant */
123751a7b740SScott Long if (flags & MAKEENTRY)
123851a7b740SScott Long cache_enter(dvp, *vpp, a->a_cnp);
123951a7b740SScott Long
12401703656aSScott Long if ((flags & ISLASTCN) &&
12411703656aSScott Long (nameiop == CREATE || nameiop == RENAME)) {
12421703656aSScott Long error = EROFS;
12431703656aSScott Long } else {
12441703656aSScott Long error = ENOENT;
12451703656aSScott Long }
12461703656aSScott Long }
124751a7b740SScott Long
12481703656aSScott Long return (error);
124951a7b740SScott Long }
125051a7b740SScott Long
125151a7b740SScott Long static int
udf_reclaim(struct vop_reclaim_args * a)125251a7b740SScott Long udf_reclaim(struct vop_reclaim_args *a)
125351a7b740SScott Long {
125451a7b740SScott Long struct vnode *vp;
125551a7b740SScott Long struct udf_node *unode;
125651a7b740SScott Long
125751a7b740SScott Long vp = a->a_vp;
125851a7b740SScott Long unode = VTON(vp);
125951a7b740SScott Long
126051a7b740SScott Long if (unode != NULL) {
12614e94fafcSPoul-Henning Kamp vfs_hash_remove(vp);
126251a7b740SScott Long
126351a7b740SScott Long if (unode->fentry != NULL)
12641ede983cSDag-Erling Smørgrav free(unode->fentry, M_UDFFENTRY);
126551a7b740SScott Long uma_zfree(udf_zone_node, unode);
126651a7b740SScott Long vp->v_data = NULL;
126751a7b740SScott Long }
126851a7b740SScott Long
126951a7b740SScott Long return (0);
127051a7b740SScott Long }
127151a7b740SScott Long
127210bcafe9SPawel Jakub Dawidek static int
udf_vptofh(struct vop_vptofh_args * a)127310bcafe9SPawel Jakub Dawidek udf_vptofh(struct vop_vptofh_args *a)
127410bcafe9SPawel Jakub Dawidek {
127510bcafe9SPawel Jakub Dawidek struct udf_node *node;
127610bcafe9SPawel Jakub Dawidek struct ifid *ifhp;
1277*91b5592aSRick Macklem _Static_assert(sizeof(struct ifid) <= sizeof(struct fid),
1278*91b5592aSRick Macklem "struct ifid cannot be larger than struct fid");
127910bcafe9SPawel Jakub Dawidek
128010bcafe9SPawel Jakub Dawidek node = VTON(a->a_vp);
128110bcafe9SPawel Jakub Dawidek ifhp = (struct ifid *)a->a_fhp;
128210bcafe9SPawel Jakub Dawidek ifhp->ifid_len = sizeof(struct ifid);
128310bcafe9SPawel Jakub Dawidek ifhp->ifid_ino = node->hash_id;
128410bcafe9SPawel Jakub Dawidek
128510bcafe9SPawel Jakub Dawidek return (0);
128610bcafe9SPawel Jakub Dawidek }
128710bcafe9SPawel Jakub Dawidek
128851a7b740SScott Long /*
128951a7b740SScott Long * Read the block and then set the data pointer to correspond with the
129051a7b740SScott Long * offset passed in. Only read in at most 'size' bytes, and then set 'size'
129151a7b740SScott Long * to the number of bytes pointed to. If 'size' is zero, try to read in a
129251a7b740SScott Long * whole extent.
1293744bb56dSScott Long *
1294744bb56dSScott Long * Note that *bp may be assigned error or not.
1295744bb56dSScott Long *
129651a7b740SScott Long */
129751a7b740SScott Long static int
udf_readatoffset(struct udf_node * node,int * size,off_t offset,struct buf ** bp,uint8_t ** data)12989d32fde8SScott Long udf_readatoffset(struct udf_node *node, int *size, off_t offset,
12999d32fde8SScott Long struct buf **bp, uint8_t **data)
130051a7b740SScott Long {
1301b0c0fb59SAndriy Gapon struct udf_mnt *udfmp = node->udfmp;
1302b0c0fb59SAndriy Gapon struct vnode *vp = node->i_vnode;
1303b0c0fb59SAndriy Gapon struct file_entry *fentry;
130451a7b740SScott Long struct buf *bp1;
1305c2d6947dSJeroen Ruigrok van der Werven uint32_t max_size;
130698b0c789SPoul-Henning Kamp daddr_t sector;
1307b0c0fb59SAndriy Gapon off_t off;
1308b0c0fb59SAndriy Gapon int adj_size;
130951a7b740SScott Long int error;
131051a7b740SScott Long
1311b0c0fb59SAndriy Gapon /*
1312b0c0fb59SAndriy Gapon * This call is made *not* only to detect UDF_INVALID_BMAP case,
1313b0c0fb59SAndriy Gapon * max_size is used as an ad-hoc read-ahead hint for "normal" case.
1314b0c0fb59SAndriy Gapon */
131551a7b740SScott Long error = udf_bmap_internal(node, offset, §or, &max_size);
13164576293dSScott Long if (error == UDF_INVALID_BMAP) {
131751a7b740SScott Long /*
131851a7b740SScott Long * This error means that the file *data* is stored in the
131951a7b740SScott Long * allocation descriptor field of the file entry.
132051a7b740SScott Long */
132151a7b740SScott Long fentry = node->fentry;
1322bf1c3dddSScott Long *data = &fentry->data[le32toh(fentry->l_ea)];
1323bf1c3dddSScott Long *size = le32toh(fentry->l_ad);
1324b2c91b67SAndriy Gapon if (offset >= *size)
1325b2c91b67SAndriy Gapon *size = 0;
1326b2c91b67SAndriy Gapon else {
1327b2c91b67SAndriy Gapon *data += offset;
1328b2c91b67SAndriy Gapon *size -= offset;
1329b2c91b67SAndriy Gapon }
133051a7b740SScott Long return (0);
133151a7b740SScott Long } else if (error != 0) {
133251a7b740SScott Long return (error);
133351a7b740SScott Long }
133451a7b740SScott Long
1335d1def83bSScott Long /* Adjust the size so that it is within range */
133651a7b740SScott Long if (*size == 0 || *size > max_size)
133751a7b740SScott Long *size = max_size;
133851a7b740SScott Long
1339b0c0fb59SAndriy Gapon /*
1340b0c0fb59SAndriy Gapon * Because we will read starting at block boundary, we need to adjust
1341b0c0fb59SAndriy Gapon * how much we need to read so that all promised data is in.
1342b0c0fb59SAndriy Gapon * Also, we can't promise to read more than MAXBSIZE bytes starting
1343b0c0fb59SAndriy Gapon * from block boundary, so adjust what we promise too.
1344b0c0fb59SAndriy Gapon */
1345b0c0fb59SAndriy Gapon off = blkoff(udfmp, offset);
1346b0c0fb59SAndriy Gapon *size = min(*size, MAXBSIZE - off);
1347b0c0fb59SAndriy Gapon adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask;
1348b0c0fb59SAndriy Gapon *bp = NULL;
1349b0c0fb59SAndriy Gapon if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) {
13501830bca1SScott Long printf("warning: udf_readlblks returned error %d\n", error);
1351744bb56dSScott Long /* note: *bp may be non-NULL */
135251a7b740SScott Long return (error);
135351a7b740SScott Long }
135451a7b740SScott Long
135551a7b740SScott Long bp1 = *bp;
13565df29e0cSRemko Lodder *data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
135751a7b740SScott Long return (0);
135851a7b740SScott Long }
135951a7b740SScott Long
136051a7b740SScott Long /*
136151a7b740SScott Long * Translate a file offset into a logical block and then into a physical
136251a7b740SScott Long * block.
13635df29e0cSRemko Lodder * max_size - maximum number of bytes that can be read starting from given
13645df29e0cSRemko Lodder * offset, rather than beginning of calculated sector number
136551a7b740SScott Long */
136651a7b740SScott Long static int
udf_bmap_internal(struct udf_node * node,off_t offset,daddr_t * sector,uint32_t * max_size)13679d32fde8SScott Long udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
13689d32fde8SScott Long uint32_t *max_size)
136951a7b740SScott Long {
137051a7b740SScott Long struct udf_mnt *udfmp;
137151a7b740SScott Long struct file_entry *fentry;
137251a7b740SScott Long void *icb;
137351a7b740SScott Long struct icb_tag *tag;
1374c2d6947dSJeroen Ruigrok van der Werven uint32_t icblen = 0;
137598b0c789SPoul-Henning Kamp daddr_t lsector;
137651a7b740SScott Long int ad_offset, ad_num = 0;
137751a7b740SScott Long int i, p_offset;
137851a7b740SScott Long
137951a7b740SScott Long udfmp = node->udfmp;
138051a7b740SScott Long fentry = node->fentry;
138151a7b740SScott Long tag = &fentry->icbtag;
138251a7b740SScott Long
1383bf1c3dddSScott Long switch (le16toh(tag->strat_type)) {
138451a7b740SScott Long case 4:
138551a7b740SScott Long break;
138651a7b740SScott Long
138751a7b740SScott Long case 4096:
138851a7b740SScott Long printf("Cannot deal with strategy4096 yet!\n");
138951a7b740SScott Long return (ENODEV);
139051a7b740SScott Long
139151a7b740SScott Long default:
139251a7b740SScott Long printf("Unknown strategy type %d\n", tag->strat_type);
139351a7b740SScott Long return (ENODEV);
139451a7b740SScott Long }
139551a7b740SScott Long
1396bf1c3dddSScott Long switch (le16toh(tag->flags) & 0x7) {
139751a7b740SScott Long case 0:
139851a7b740SScott Long /*
139951a7b740SScott Long * The allocation descriptor field is filled with short_ad's.
140051a7b740SScott Long * If the offset is beyond the current extent, look for the
140151a7b740SScott Long * next extent.
140251a7b740SScott Long */
140351a7b740SScott Long do {
140451a7b740SScott Long offset -= icblen;
140551a7b740SScott Long ad_offset = sizeof(struct short_ad) * ad_num;
1406bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) {
140751a7b740SScott Long printf("File offset out of bounds\n");
140851a7b740SScott Long return (EINVAL);
140951a7b740SScott Long }
1410a4d629e3SScott Long icb = GETICB(short_ad, fentry,
1411bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset);
141251a7b740SScott Long icblen = GETICBLEN(short_ad, icb);
141351a7b740SScott Long ad_num++;
141451a7b740SScott Long } while(offset >= icblen);
141551a7b740SScott Long
141651a7b740SScott Long lsector = (offset >> udfmp->bshift) +
1417937a2387SWill Andrews le32toh(((struct short_ad *)(icb))->pos);
141851a7b740SScott Long
14195df29e0cSRemko Lodder *max_size = icblen - offset;
142051a7b740SScott Long
142151a7b740SScott Long break;
142251a7b740SScott Long case 1:
142351a7b740SScott Long /*
142451a7b740SScott Long * The allocation descriptor field is filled with long_ad's
142551a7b740SScott Long * If the offset is beyond the current extent, look for the
142651a7b740SScott Long * next extent.
142751a7b740SScott Long */
142851a7b740SScott Long do {
142951a7b740SScott Long offset -= icblen;
143051a7b740SScott Long ad_offset = sizeof(struct long_ad) * ad_num;
1431bf1c3dddSScott Long if (ad_offset > le32toh(fentry->l_ad)) {
143251a7b740SScott Long printf("File offset out of bounds\n");
143351a7b740SScott Long return (EINVAL);
143451a7b740SScott Long }
1435bf1c3dddSScott Long icb = GETICB(long_ad, fentry,
1436bf1c3dddSScott Long le32toh(fentry->l_ea) + ad_offset);
143751a7b740SScott Long icblen = GETICBLEN(long_ad, icb);
143851a7b740SScott Long ad_num++;
143951a7b740SScott Long } while(offset >= icblen);
144051a7b740SScott Long
144151a7b740SScott Long lsector = (offset >> udfmp->bshift) +
1442bf1c3dddSScott Long le32toh(((struct long_ad *)(icb))->loc.lb_num);
144351a7b740SScott Long
14445df29e0cSRemko Lodder *max_size = icblen - offset;
144551a7b740SScott Long
144651a7b740SScott Long break;
144751a7b740SScott Long case 3:
144851a7b740SScott Long /*
144951a7b740SScott Long * This type means that the file *data* is stored in the
145051a7b740SScott Long * allocation descriptor field of the file entry.
145151a7b740SScott Long */
145251a7b740SScott Long *max_size = 0;
14538db4c2f2SScott Long *sector = node->hash_id + udfmp->part_start;
145451a7b740SScott Long
14554576293dSScott Long return (UDF_INVALID_BMAP);
145651a7b740SScott Long case 2:
145751a7b740SScott Long /* DirectCD does not use extended_ad's */
145851a7b740SScott Long default:
145951a7b740SScott Long printf("Unsupported allocation descriptor %d\n",
146051a7b740SScott Long tag->flags & 0x7);
146151a7b740SScott Long return (ENODEV);
146251a7b740SScott Long }
146351a7b740SScott Long
146451a7b740SScott Long *sector = lsector + udfmp->part_start;
146551a7b740SScott Long
146651a7b740SScott Long /*
146751a7b740SScott Long * Check the sparing table. Each entry represents the beginning of
146851a7b740SScott Long * a packet.
146951a7b740SScott Long */
147051a7b740SScott Long if (udfmp->s_table != NULL) {
147151a7b740SScott Long for (i = 0; i< udfmp->s_table_entries; i++) {
1472bf1c3dddSScott Long p_offset =
1473bf1c3dddSScott Long lsector - le32toh(udfmp->s_table->entries[i].org);
147451a7b740SScott Long if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1475bf1c3dddSScott Long *sector =
1476bf1c3dddSScott Long le32toh(udfmp->s_table->entries[i].map) +
147751a7b740SScott Long p_offset;
147851a7b740SScott Long break;
147951a7b740SScott Long }
148051a7b740SScott Long }
148151a7b740SScott Long }
148251a7b740SScott Long
148351a7b740SScott Long return (0);
148451a7b740SScott Long }
1485