2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 /* Take care of the vnode side of things */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
43 #include <sys/iconv.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
46 #include <sys/dirent.h>
47 #include <sys/queue.h>
48 #include <sys/unistd.h>
49 #include <sys/endian.h>
53 #include <fs/udf/ecma167-udf.h>
54 #include <fs/udf/osta.h>
55 #include <fs/udf/udf.h>
56 #include <fs/udf/udf_mount.h>
58 extern struct iconv_functions *udf_iconv;
60 static vop_access_t udf_access;
61 static vop_getattr_t udf_getattr;
62 static vop_open_t udf_open;
63 static vop_ioctl_t udf_ioctl;
64 static vop_pathconf_t udf_pathconf;
65 static vop_print_t udf_print;
66 static vop_read_t udf_read;
67 static vop_readdir_t udf_readdir;
68 static vop_readlink_t udf_readlink;
69 static vop_setattr_t udf_setattr;
70 static vop_strategy_t udf_strategy;
71 static vop_bmap_t udf_bmap;
72 static vop_cachedlookup_t udf_lookup;
73 static vop_reclaim_t udf_reclaim;
74 static vop_vptofh_t udf_vptofh;
75 static int udf_readatoffset(struct udf_node *node, int *size, off_t offset,
76 struct buf **bp, uint8_t **data);
77 static int udf_bmap_internal(struct udf_node *node, off_t offset,
78 daddr_t *sector, uint32_t *max_size);
80 static struct vop_vector udf_vnodeops = {
81 .vop_default = &default_vnodeops,
83 .vop_access = udf_access,
85 .vop_cachedlookup = udf_lookup,
86 .vop_getattr = udf_getattr,
87 .vop_ioctl = udf_ioctl,
88 .vop_lookup = vfs_cache_lookup,
90 .vop_pathconf = udf_pathconf,
91 .vop_print = udf_print,
93 .vop_readdir = udf_readdir,
94 .vop_readlink = udf_readlink,
95 .vop_reclaim = udf_reclaim,
96 .vop_setattr = udf_setattr,
97 .vop_strategy = udf_strategy,
98 .vop_vptofh = udf_vptofh,
100 VFS_VOP_VECTOR_REGISTER(udf_vnodeops);
102 struct vop_vector udf_fifoops = {
103 .vop_default = &fifo_specops,
104 .vop_access = udf_access,
105 .vop_getattr = udf_getattr,
106 .vop_pathconf = udf_pathconf,
107 .vop_print = udf_print,
108 .vop_reclaim = udf_reclaim,
109 .vop_setattr = udf_setattr,
110 .vop_vptofh = udf_vptofh,
112 VFS_VOP_VECTOR_REGISTER(udf_fifoops);
114 static MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure");
115 static MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure");
117 #define UDF_INVALID_BMAP -1
120 udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td)
125 error = getnewvnode("udf", mp, &udf_vnodeops, &vp);
127 printf("udf_allocv: failed to allocate new vnode\n");
135 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
137 udf_permtomode(struct udf_node *node)
143 perm = le32toh(node->fentry->perm);
144 flags = le16toh(node->fentry->icbtag.flags);
146 mode = perm & UDF_FENTRY_PERM_USER_MASK;
147 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
148 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
149 mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
150 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
151 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
157 udf_access(struct vop_access_args *a)
160 struct udf_node *node;
166 accmode = a->a_accmode;
168 if (accmode & VWRITE) {
169 switch (vp->v_type) {
180 mode = udf_permtomode(node);
182 return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid,
183 accmode, a->a_cred, NULL));
187 udf_open(struct vop_open_args *ap) {
188 struct udf_node *np = VTON(ap->a_vp);
191 fsize = le64toh(np->fentry->inf_len);
192 vnode_create_vobject(ap->a_vp, fsize, ap->a_td);
196 static const int mon_lens[2][12] = {
197 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
198 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
202 udf_isaleapyear(int year)
206 i = (year % 4) ? 0 : 1;
207 i &= (year % 100) ? 1 : 0;
208 i |= (year % 400) ? 0 : 1;
214 * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
217 udf_timetotimespec(struct timestamp *time, struct timespec *t)
219 int i, lpyear, daysinyear, year, startyear;
221 uint16_t u_tz_offset;
226 * DirectCD seems to like using bogus year values.
227 * Don't trust time->month as it will be used for an array index.
229 year = le16toh(time->year);
230 if (year < 1970 || time->month < 1 || time->month > 12) {
236 /* Calculate the time and day */
237 t->tv_sec = time->second;
238 t->tv_sec += time->minute * 60;
239 t->tv_sec += time->hour * 3600;
240 t->tv_sec += (time->day - 1) * 3600 * 24;
242 /* Calculate the month */
243 lpyear = udf_isaleapyear(year);
244 t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24;
246 /* Speed up the calculation */
249 t->tv_sec += 1262304000;
251 } else if (year > 1999) {
252 t->tv_sec += 946684800;
254 } else if (year > 1989) {
255 t->tv_sec += 631152000;
257 } else if (year > 1979) {
258 t->tv_sec += 315532800;
262 daysinyear = (year - startyear) * 365;
263 for (i = startyear; i < year; i++)
264 daysinyear += udf_isaleapyear(i);
265 t->tv_sec += daysinyear * 3600 * 24;
267 /* Calculate microseconds */
268 t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 +
272 * Calculate the time zone. The timezone is 12 bit signed 2's
273 * complement, so we gotta do some extra magic to handle it right.
275 tz.u_tz_offset = le16toh(time->type_tz);
276 tz.u_tz_offset &= 0x0fff;
277 if (tz.u_tz_offset & 0x0800)
278 tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */
279 if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047))
280 t->tv_sec -= tz.s_tz_offset * 60;
286 udf_getattr(struct vop_getattr_args *a)
289 struct udf_node *node;
291 struct file_entry *fentry;
299 fentry = node->fentry;
301 vap->va_fsid = dev2udev(node->udfmp->im_dev);
302 vap->va_fileid = node->hash_id;
303 vap->va_mode = udf_permtomode(node);
304 vap->va_nlink = le16toh(fentry->link_cnt);
306 * XXX The spec says that -1 is valid for uid/gid and indicates an
307 * invalid uid/gid. How should this be represented?
309 vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid);
310 vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid);
311 udf_timetotimespec(&fentry->atime, &vap->va_atime);
312 udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
313 vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
314 vap->va_rdev = NODEV;
315 if (vp->v_type & VDIR) {
317 * Directories that are recorded within their ICB will show
318 * as having 0 blocks recorded. Since tradition dictates
319 * that directories consume at least one logical block,
322 if (fentry->logblks_rec != 0) {
324 le64toh(fentry->logblks_rec) * node->udfmp->bsize;
326 vap->va_size = node->udfmp->bsize;
329 vap->va_size = le64toh(fentry->inf_len);
333 vap->va_blocksize = node->udfmp->bsize;
334 vap->va_bytes = le64toh(fentry->inf_len);
335 vap->va_type = vp->v_type;
336 vap->va_filerev = 0; /* XXX */
341 udf_setattr(struct vop_setattr_args *a)
348 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
349 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
350 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
352 if (vap->va_size != (u_quad_t)VNOVAL) {
353 switch (vp->v_type) {
373 * File specific ioctls.
376 udf_ioctl(struct vop_ioctl_args *a)
378 printf("%s called\n", __func__);
383 * I'm not sure that this has much value in a read-only filesystem, but
387 udf_pathconf(struct vop_pathconf_args *a)
391 case _PC_FILESIZEBITS:
395 *a->a_retval = 65535;
398 *a->a_retval = NAME_MAX;
400 case _PC_SYMLINK_MAX:
401 *a->a_retval = MAXPATHLEN;
407 if (a->a_vp->v_type == VDIR || a->a_vp->v_type == VFIFO) {
408 *a->a_retval = PIPE_BUF;
413 return (vop_stdpathconf(a));
418 udf_print(struct vop_print_args *ap)
420 struct vnode *vp = ap->a_vp;
421 struct udf_node *node = VTON(vp);
423 printf(" ino %lu, on dev %s", (u_long)node->hash_id,
424 devtoname(node->udfmp->im_dev));
425 if (vp->v_type == VFIFO)
431 #define lblkno(udfmp, loc) ((loc) >> (udfmp)->bshift)
432 #define blkoff(udfmp, loc) ((loc) & (udfmp)->bmask)
433 #define lblktosize(udfmp, blk) ((blk) << (udfmp)->bshift)
436 is_data_in_fentry(const struct udf_node *node)
438 const struct file_entry *fentry = node->fentry;
440 return ((le16toh(fentry->icbtag.flags) & 0x7) == 3);
444 udf_read(struct vop_read_args *ap)
446 struct vnode *vp = ap->a_vp;
447 struct uio *uio = ap->a_uio;
448 struct udf_node *node = VTON(vp);
449 struct udf_mnt *udfmp;
450 struct file_entry *fentry;
453 daddr_t lbn, rablock;
459 if (uio->uio_resid == 0)
461 if (uio->uio_offset < 0)
464 if (is_data_in_fentry(node)) {
465 fentry = node->fentry;
466 data = &fentry->data[le32toh(fentry->l_ea)];
467 fsize = le32toh(fentry->l_ad);
470 diff = fsize - uio->uio_offset;
475 error = uiomove(data + uio->uio_offset, (int)n, uio);
479 fsize = le64toh(node->fentry->inf_len);
482 lbn = lblkno(udfmp, uio->uio_offset);
483 on = blkoff(udfmp, uio->uio_offset);
484 n = min((u_int)(udfmp->bsize - on),
486 diff = fsize - uio->uio_offset;
493 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
494 if (lblktosize(udfmp, rablock) < fsize) {
495 error = cluster_read(vp, fsize, lbn, size,
496 NOCRED, uio->uio_resid,
497 (ap->a_ioflag >> 16), 0, &bp);
499 error = bread(vp, lbn, size, NOCRED, &bp);
502 error = bread(vp, lbn, size, NOCRED, &bp);
508 n = min(n, size - bp->b_resid);
510 error = uiomove(bp->b_data + on, (int)n, uio);
512 } while (error == 0 && uio->uio_resid > 0 && n != 0);
517 * Call the OSTA routines to translate the name from a CS0 dstring to a
518 * 16-bit Unicode String. Hooks need to be placed in here to translate from
519 * Unicode to the encoding that the kernel/user expects. Return the length
520 * of the translated string.
523 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
525 unicode_t *transname;
529 size_t destleft = MAXNAMLEN;
531 /* Convert 16-bit Unicode to destname */
532 if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
533 /* allocate a buffer big enough to hold an 8->16 bit expansion */
534 unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
536 if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
537 printf("udf: Unicode translation failed\n");
538 uma_zfree(udf_zone_trans, unibuf);
542 while (unilen > 0 && destleft > 0) {
543 udf_iconv->conv(udfmp->im_d2l, __DECONST(const char **,
544 &unibuf), (size_t *)&unilen, (char **)&destname,
546 /* Unconverted character found */
547 if (unilen > 0 && destleft > 0) {
554 uma_zfree(udf_zone_trans, unip);
556 destlen = MAXNAMLEN - (int)destleft;
558 /* allocate a buffer big enough to hold an 8->16 bit expansion */
559 transname = uma_zalloc(udf_zone_trans, M_WAITOK);
561 if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) {
562 printf("udf: Unicode translation failed\n");
563 uma_zfree(udf_zone_trans, transname);
567 for (i = 0; i < unilen ; i++) {
568 if (transname[i] & 0xff00) {
569 destname[i] = '.'; /* Fudge the 16bit chars */
571 destname[i] = transname[i] & 0xff;
574 uma_zfree(udf_zone_trans, transname);
575 destname[unilen] = 0;
576 destlen = (int)unilen;
583 * Compare a CS0 dstring with a name passed in from the VFS layer. Return
584 * 0 on a successful match, nonzero otherwise. Unicode work may need to be done
588 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
593 /* This is overkill, but not worth creating a new zone */
594 transname = uma_zalloc(udf_zone_trans, M_WAITOK);
596 cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
598 /* Easy check. If they aren't the same length, they aren't equal */
599 if ((cs0len == 0) || (cs0len != cmplen))
602 error = bcmp(transname, cmpname, cmplen);
604 uma_zfree(udf_zone_trans, transname);
609 struct dirent *dirent;
617 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
619 if (uiodir->cookies != NULL) {
620 if (++uiodir->acookies > uiodir->ncookies) {
624 *uiodir->cookies++ = cookie;
627 if (uio->uio_resid < de_size) {
632 return (uiomove(uiodir->dirent, de_size, uio));
635 static struct udf_dirstream *
636 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
638 struct udf_dirstream *ds;
640 ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
650 static struct fileid_desc *
651 udf_getfid(struct udf_dirstream *ds)
653 struct fileid_desc *fid;
654 int error, frag_size = 0, total_fid_size;
656 /* End of directory? */
657 if (ds->offset + ds->off >= ds->fsize) {
662 /* Grab the first extent of the directory */
665 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
676 * Clean up from a previous fragmented FID.
677 * XXX Is this the right place for this?
679 if (ds->fid_fragment && ds->buf != NULL) {
680 ds->fid_fragment = 0;
681 free(ds->buf, M_UDFFID);
684 fid = (struct fileid_desc*)&ds->data[ds->off];
687 * Check to see if the fid is fragmented. The first test
688 * ensures that we don't wander off the end of the buffer
689 * looking for the l_iu and l_fi fields.
691 if (ds->off + UDF_FID_SIZE > ds->size ||
692 ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){
694 /* Copy what we have of the fid into a buffer */
695 frag_size = ds->size - ds->off;
696 if (frag_size >= ds->udfmp->bsize) {
697 printf("udf: invalid FID fragment\n");
703 * File ID descriptors can only be at most one
704 * logical sector in size.
706 ds->buf = malloc(ds->udfmp->bsize, M_UDFFID,
708 bcopy(fid, ds->buf, frag_size);
710 /* Reduce all of the casting magic */
711 fid = (struct fileid_desc*)ds->buf;
716 /* Fetch the next allocation */
717 ds->offset += ds->size;
719 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
727 * If the fragment was so small that we didn't get
728 * the l_iu and l_fi fields, copy those in.
730 if (frag_size < UDF_FID_SIZE)
731 bcopy(ds->data, &ds->buf[frag_size],
732 UDF_FID_SIZE - frag_size);
735 * Now that we have enough of the fid to work with,
736 * copy in the rest of the fid from the new
739 total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi;
740 if (total_fid_size > ds->udfmp->bsize) {
741 printf("udf: invalid FID\n");
745 bcopy(ds->data, &ds->buf[frag_size],
746 total_fid_size - frag_size);
748 ds->fid_fragment = 1;
750 total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE;
754 * Update the offset. Align on a 4 byte boundary because the
757 ds->this_off = ds->offset + ds->off;
758 if (!ds->fid_fragment) {
759 ds->off += (total_fid_size + 3) & ~0x03;
761 ds->off = (total_fid_size - frag_size + 3) & ~0x03;
768 udf_closedir(struct udf_dirstream *ds)
774 if (ds->fid_fragment && ds->buf != NULL)
775 free(ds->buf, M_UDFFID);
777 uma_zfree(udf_zone_ds, ds);
781 udf_readdir(struct vop_readdir_args *a)
786 struct udf_node *node;
787 struct udf_mnt *udfmp;
788 struct fileid_desc *fid;
789 struct udf_uiodir uiodir;
790 struct udf_dirstream *ds;
791 u_long *cookies = NULL;
801 if (a->a_ncookies != NULL) {
803 * Guess how many entries are needed. If we run out, this
804 * function will be called again and thing will pick up were
807 ncookies = uio->uio_resid / 8;
808 cookies = malloc(sizeof(u_long) * ncookies,
812 uiodir.ncookies = ncookies;
813 uiodir.cookies = cookies;
816 uiodir.cookies = NULL;
820 * Iterate through the file id descriptors. Give the parent dir
821 * entry special attention.
823 ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
826 while ((fid = udf_getfid(ds)) != NULL) {
828 /* XXX Should we return an error on a bad fid? */
829 if (udf_checktag(&fid->tag, TAGID_FID)) {
830 printf("Invalid FID tag\n");
831 hexdump(fid, UDF_FID_SIZE, NULL, 0);
836 /* Is this a deleted file? */
837 if (fid->file_char & UDF_FILE_CHAR_DEL)
840 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
841 /* Do up the '.' and '..' entries. Dummy values are
842 * used for the cookies since the offset here is
843 * usually zero, and NFS doesn't like that value
845 dir.d_fileno = node->hash_id;
849 dir.d_reclen = GENERIC_DIRSIZ(&dir);
851 dirent_terminate(&dir);
852 uiodir.dirent = &dir;
853 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
857 dir.d_fileno = udf_getid(&fid->icb);
862 dir.d_reclen = GENERIC_DIRSIZ(&dir);
864 dirent_terminate(&dir);
865 uiodir.dirent = &dir;
866 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
868 dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
869 &dir.d_name[0], fid->l_fi, udfmp);
870 dir.d_fileno = udf_getid(&fid->icb);
871 dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
873 dir.d_reclen = GENERIC_DIRSIZ(&dir);
874 dir.d_off = ds->this_off;
875 dirent_terminate(&dir);
876 uiodir.dirent = &dir;
877 error = udf_uiodir(&uiodir, dir.d_reclen, uio,
882 uio->uio_offset = ds->offset + ds->off;
885 /* tell the calling layer whether we need to be called again */
886 *a->a_eofflag = uiodir.eofflag;
895 if (a->a_ncookies != NULL) {
897 free(cookies, M_TEMP);
899 *a->a_ncookies = uiodir.acookies;
900 *a->a_cookies = cookies;
908 udf_readlink(struct vop_readlink_args *ap)
910 struct path_component *pc, *end;
914 struct udf_node *node;
917 int error, len, root;
920 * A symbolic link in UDF is a list of variable-length path
921 * component structures. We build a pathname in the caller's
922 * uio by traversing this list.
926 len = le64toh(node->fentry->inf_len);
927 buf = malloc(len, M_DEVBUF, M_WAITOK);
928 iov[0].iov_len = len;
929 iov[0].iov_base = buf;
933 uio.uio_resid = iov[0].iov_len;
934 uio.uio_segflg = UIO_SYSSPACE;
935 uio.uio_rw = UIO_READ;
936 uio.uio_td = curthread;
937 error = VOP_READ(vp, &uio, 0, ap->a_cred);
942 end = (void *)((char *)buf + len);
947 /* Only allow this at the beginning of a path. */
948 if ((void *)pc != buf) {
960 case UDF_PATH_DOTDOT:
965 if (pc->length == 0) {
970 * XXX: We only support CS8 which appears to map
973 switch (pc->identifier[0]) {
975 cp = pc->identifier + 1;
976 len = pc->length - 1;
989 * If this is not the first component, insert a path
993 /* If we started with root we already have a "/". */
997 if (ap->a_uio->uio_resid < 1) {
998 error = ENAMETOOLONG;
1001 error = uiomove("/", 1, ap->a_uio);
1007 /* Append string at 'cp' of length 'len' to our path. */
1008 if (len > ap->a_uio->uio_resid) {
1009 error = ENAMETOOLONG;
1012 error = uiomove(cp, len, ap->a_uio);
1016 /* Advance to next component. */
1017 pc = (void *)((char *)pc + 4 + pc->length);
1020 free(buf, M_DEVBUF);
1025 udf_strategy(struct vop_strategy_args *a)
1029 struct udf_node *node;
1040 if (bp->b_blkno == bp->b_lblkno) {
1041 offset = lblktosize(node->udfmp, bp->b_lblkno);
1042 error = udf_bmap_internal(node, offset, §or, &maxsize);
1049 /* bmap gives sector numbers, bio works with device blocks */
1050 bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT);
1052 bo = node->udfmp->im_bo;
1053 bp->b_iooffset = dbtob(bp->b_blkno);
1054 BO_STRATEGY(bo, bp);
1059 udf_bmap(struct vop_bmap_args *a)
1061 struct udf_node *node;
1067 node = VTON(a->a_vp);
1069 if (a->a_bop != NULL)
1070 *a->a_bop = &node->udfmp->im_devvp->v_bufobj;
1071 if (a->a_bnp == NULL)
1077 * UDF_INVALID_BMAP means data embedded into fentry, this is an internal
1078 * error that should not be propagated to calling code.
1079 * Most obvious mapping for this error is EOPNOTSUPP as we can not truly
1080 * translate block numbers in this case.
1081 * Incidentally, this return code will make vnode pager to use VOP_READ
1082 * to get data for mmap-ed pages and udf_read knows how to do the right
1083 * thing for this kind of files.
1085 error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift,
1086 &lsector, &max_size);
1087 if (error == UDF_INVALID_BMAP)
1088 return (EOPNOTSUPP);
1092 /* Translate logical to physical sector number */
1093 *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
1096 * Determine maximum number of readahead blocks following the
1100 nblk = (max_size >> node->udfmp->bshift) - 1;
1103 else if (nblk >= (MAXBSIZE >> node->udfmp->bshift))
1104 *a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1;
1117 * The all powerful VOP_LOOKUP().
1120 udf_lookup(struct vop_cachedlookup_args *a)
1123 struct vnode *tdp = NULL;
1124 struct vnode **vpp = a->a_vpp;
1125 struct udf_node *node;
1126 struct udf_mnt *udfmp;
1127 struct fileid_desc *fid = NULL;
1128 struct udf_dirstream *ds;
1134 int offset, error = 0;
1135 int fsize, lkflags, ltype, numdirpasses;
1139 udfmp = node->udfmp;
1140 nameiop = a->a_cnp->cn_nameiop;
1141 flags = a->a_cnp->cn_flags;
1142 lkflags = a->a_cnp->cn_lkflags;
1143 nameptr = a->a_cnp->cn_nameptr;
1144 namelen = a->a_cnp->cn_namelen;
1145 fsize = le64toh(node->fentry->inf_len);
1148 * If this is a LOOKUP and we've already partially searched through
1149 * the directory, pick up where we left off and flag that the
1150 * directory may need to be searched twice. For a full description,
1151 * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
1153 if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
1157 offset = node->diroff;
1159 nchstats.ncs_2passes++;
1163 ds = udf_opendir(node, offset, fsize, udfmp);
1165 while ((fid = udf_getfid(ds)) != NULL) {
1167 /* XXX Should we return an error on a bad fid? */
1168 if (udf_checktag(&fid->tag, TAGID_FID)) {
1169 printf("udf_lookup: Invalid tag\n");
1174 /* Is this a deleted file? */
1175 if (fid->file_char & UDF_FILE_CHAR_DEL)
1178 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
1179 if (flags & ISDOTDOT) {
1180 id = udf_getid(&fid->icb);
1184 if (!(udf_cmpname(&fid->data[fid->l_iu],
1185 nameptr, fid->l_fi, namelen, udfmp))) {
1186 id = udf_getid(&fid->icb);
1195 /* XXX Bail out here? */
1201 /* Did we have a match? */
1204 * Remember where this entry was if it's the final
1207 if ((flags & ISLASTCN) && nameiop == LOOKUP)
1208 node->diroff = ds->offset + ds->off;
1209 if (numdirpasses == 2)
1210 nchstats.ncs_pass2++;
1213 if (flags & ISDOTDOT) {
1214 error = vn_vget_ino(dvp, id, lkflags, &tdp);
1215 } else if (node->hash_id == id) {
1216 VREF(dvp); /* we want ourself, ie "." */
1218 * When we lookup "." we still can be asked to lock it
1221 ltype = lkflags & LK_TYPE_MASK;
1222 if (ltype != VOP_ISLOCKED(dvp)) {
1223 if (ltype == LK_EXCLUSIVE)
1224 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1225 else /* if (ltype == LK_SHARED) */
1226 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1230 error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp);
1233 /* Put this entry in the cache */
1234 if (flags & MAKEENTRY)
1235 cache_enter(dvp, *vpp, a->a_cnp);
1238 /* Name wasn't found on this pass. Do another pass? */
1239 if (numdirpasses == 2) {
1247 /* Enter name into cache as non-existant */
1248 if (flags & MAKEENTRY)
1249 cache_enter(dvp, *vpp, a->a_cnp);
1251 if ((flags & ISLASTCN) &&
1252 (nameiop == CREATE || nameiop == RENAME)) {
1263 udf_reclaim(struct vop_reclaim_args *a)
1266 struct udf_node *unode;
1271 if (unode != NULL) {
1272 vfs_hash_remove(vp);
1274 if (unode->fentry != NULL)
1275 free(unode->fentry, M_UDFFENTRY);
1276 uma_zfree(udf_zone_node, unode);
1284 udf_vptofh(struct vop_vptofh_args *a)
1286 struct udf_node *node;
1289 node = VTON(a->a_vp);
1290 ifhp = (struct ifid *)a->a_fhp;
1291 ifhp->ifid_len = sizeof(struct ifid);
1292 ifhp->ifid_ino = node->hash_id;
1298 * Read the block and then set the data pointer to correspond with the
1299 * offset passed in. Only read in at most 'size' bytes, and then set 'size'
1300 * to the number of bytes pointed to. If 'size' is zero, try to read in a
1303 * Note that *bp may be assigned error or not.
1307 udf_readatoffset(struct udf_node *node, int *size, off_t offset,
1308 struct buf **bp, uint8_t **data)
1310 struct udf_mnt *udfmp = node->udfmp;
1311 struct vnode *vp = node->i_vnode;
1312 struct file_entry *fentry;
1321 * This call is made *not* only to detect UDF_INVALID_BMAP case,
1322 * max_size is used as an ad-hoc read-ahead hint for "normal" case.
1324 error = udf_bmap_internal(node, offset, §or, &max_size);
1325 if (error == UDF_INVALID_BMAP) {
1327 * This error means that the file *data* is stored in the
1328 * allocation descriptor field of the file entry.
1330 fentry = node->fentry;
1331 *data = &fentry->data[le32toh(fentry->l_ea)];
1332 *size = le32toh(fentry->l_ad);
1333 if (offset >= *size)
1340 } else if (error != 0) {
1344 /* Adjust the size so that it is within range */
1345 if (*size == 0 || *size > max_size)
1349 * Because we will read starting at block boundary, we need to adjust
1350 * how much we need to read so that all promised data is in.
1351 * Also, we can't promise to read more than MAXBSIZE bytes starting
1352 * from block boundary, so adjust what we promise too.
1354 off = blkoff(udfmp, offset);
1355 *size = min(*size, MAXBSIZE - off);
1356 adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask;
1358 if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) {
1359 printf("warning: udf_readlblks returned error %d\n", error);
1360 /* note: *bp may be non-NULL */
1365 *data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
1370 * Translate a file offset into a logical block and then into a physical
1372 * max_size - maximum number of bytes that can be read starting from given
1373 * offset, rather than beginning of calculated sector number
1376 udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
1379 struct udf_mnt *udfmp;
1380 struct file_entry *fentry;
1382 struct icb_tag *tag;
1383 uint32_t icblen = 0;
1385 int ad_offset, ad_num = 0;
1388 udfmp = node->udfmp;
1389 fentry = node->fentry;
1390 tag = &fentry->icbtag;
1392 switch (le16toh(tag->strat_type)) {
1397 printf("Cannot deal with strategy4096 yet!\n");
1401 printf("Unknown strategy type %d\n", tag->strat_type);
1405 switch (le16toh(tag->flags) & 0x7) {
1408 * The allocation descriptor field is filled with short_ad's.
1409 * If the offset is beyond the current extent, look for the
1414 ad_offset = sizeof(struct short_ad) * ad_num;
1415 if (ad_offset > le32toh(fentry->l_ad)) {
1416 printf("File offset out of bounds\n");
1419 icb = GETICB(short_ad, fentry,
1420 le32toh(fentry->l_ea) + ad_offset);
1421 icblen = GETICBLEN(short_ad, icb);
1423 } while(offset >= icblen);
1425 lsector = (offset >> udfmp->bshift) +
1426 le32toh(((struct short_ad *)(icb))->pos);
1428 *max_size = icblen - offset;
1433 * The allocation descriptor field is filled with long_ad's
1434 * If the offset is beyond the current extent, look for the
1439 ad_offset = sizeof(struct long_ad) * ad_num;
1440 if (ad_offset > le32toh(fentry->l_ad)) {
1441 printf("File offset out of bounds\n");
1444 icb = GETICB(long_ad, fentry,
1445 le32toh(fentry->l_ea) + ad_offset);
1446 icblen = GETICBLEN(long_ad, icb);
1448 } while(offset >= icblen);
1450 lsector = (offset >> udfmp->bshift) +
1451 le32toh(((struct long_ad *)(icb))->loc.lb_num);
1453 *max_size = icblen - offset;
1458 * This type means that the file *data* is stored in the
1459 * allocation descriptor field of the file entry.
1462 *sector = node->hash_id + udfmp->part_start;
1464 return (UDF_INVALID_BMAP);
1466 /* DirectCD does not use extended_ad's */
1468 printf("Unsupported allocation descriptor %d\n",
1473 *sector = lsector + udfmp->part_start;
1476 * Check the sparing table. Each entry represents the beginning of
1479 if (udfmp->s_table != NULL) {
1480 for (i = 0; i< udfmp->s_table_entries; i++) {
1482 lsector - le32toh(udfmp->s_table->entries[i].org);
1483 if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1485 le32toh(udfmp->s_table->entries[i].map) +