2 * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* Take care of the vnode side of things */
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
41 #include <sys/iconv.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/dirent.h>
45 #include <sys/queue.h>
46 #include <sys/unistd.h>
47 #include <sys/endian.h>
51 #include <fs/fifofs/fifo.h>
52 #include <fs/udf/ecma167-udf.h>
53 #include <fs/udf/osta.h>
54 #include <fs/udf/udf.h>
55 #include <fs/udf/udf_mount.h>
57 extern struct iconv_functions *udf_iconv;
59 static vop_access_t udf_access;
60 static vop_getattr_t udf_getattr;
61 static vop_open_t udf_open;
62 static vop_ioctl_t udf_ioctl;
63 static vop_pathconf_t udf_pathconf;
64 static vop_print_t udf_print;
65 static vop_read_t udf_read;
66 static vop_readdir_t udf_readdir;
67 static vop_readlink_t udf_readlink;
68 static vop_setattr_t udf_setattr;
69 static vop_strategy_t udf_strategy;
70 static vop_bmap_t udf_bmap;
71 static vop_cachedlookup_t udf_lookup;
72 static vop_reclaim_t udf_reclaim;
73 static vop_vptofh_t udf_vptofh;
74 static int udf_readatoffset(struct udf_node *node, int *size, off_t offset,
75 struct buf **bp, uint8_t **data);
76 static int udf_bmap_internal(struct udf_node *node, off_t offset,
77 daddr_t *sector, uint32_t *max_size);
79 static struct vop_vector udf_vnodeops = {
80 .vop_default = &default_vnodeops,
82 .vop_access = udf_access,
84 .vop_cachedlookup = udf_lookup,
85 .vop_getattr = udf_getattr,
86 .vop_ioctl = udf_ioctl,
87 .vop_lookup = vfs_cache_lookup,
89 .vop_pathconf = udf_pathconf,
90 .vop_print = udf_print,
92 .vop_readdir = udf_readdir,
93 .vop_readlink = udf_readlink,
94 .vop_reclaim = udf_reclaim,
95 .vop_setattr = udf_setattr,
96 .vop_strategy = udf_strategy,
97 .vop_vptofh = udf_vptofh,
100 struct vop_vector udf_fifoops = {
101 .vop_default = &fifo_specops,
102 .vop_access = udf_access,
103 .vop_getattr = udf_getattr,
104 .vop_print = udf_print,
105 .vop_reclaim = udf_reclaim,
106 .vop_setattr = udf_setattr,
107 .vop_vptofh = udf_vptofh,
110 static MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure");
111 static MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure");
113 #define UDF_INVALID_BMAP -1
116 udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td)
121 error = getnewvnode("udf", mp, &udf_vnodeops, &vp);
123 printf("udf_allocv: failed to allocate new vnode\n");
131 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
133 udf_permtomode(struct udf_node *node)
139 perm = le32toh(node->fentry->perm);
140 flags = le16toh(node->fentry->icbtag.flags);
142 mode = perm & UDF_FENTRY_PERM_USER_MASK;
143 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
144 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
145 mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
146 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
147 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
153 udf_access(struct vop_access_args *a)
156 struct udf_node *node;
162 accmode = a->a_accmode;
164 if (accmode & VWRITE) {
165 switch (vp->v_type) {
176 mode = udf_permtomode(node);
178 return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid,
179 accmode, a->a_cred, NULL));
183 udf_open(struct vop_open_args *ap) {
184 struct udf_node *np = VTON(ap->a_vp);
187 fsize = le64toh(np->fentry->inf_len);
188 vnode_create_vobject(ap->a_vp, fsize, ap->a_td);
192 static const int mon_lens[2][12] = {
193 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
194 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
198 udf_isaleapyear(int year)
202 i = (year % 4) ? 0 : 1;
203 i &= (year % 100) ? 1 : 0;
204 i |= (year % 400) ? 0 : 1;
210 * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
213 udf_timetotimespec(struct timestamp *time, struct timespec *t)
215 int i, lpyear, daysinyear, year, startyear;
217 uint16_t u_tz_offset;
222 * DirectCD seems to like using bogus year values.
223 * Don't trust time->month as it will be used for an array index.
225 year = le16toh(time->year);
226 if (year < 1970 || time->month < 1 || time->month > 12) {
232 /* Calculate the time and day */
233 t->tv_sec = time->second;
234 t->tv_sec += time->minute * 60;
235 t->tv_sec += time->hour * 3600;
236 t->tv_sec += (time->day - 1) * 3600 * 24;
238 /* Calculate the month */
239 lpyear = udf_isaleapyear(year);
240 t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24;
242 /* Speed up the calculation */
245 t->tv_sec += 1262304000;
247 } else if (year > 1999) {
248 t->tv_sec += 946684800;
250 } else if (year > 1989) {
251 t->tv_sec += 631152000;
253 } else if (year > 1979) {
254 t->tv_sec += 315532800;
258 daysinyear = (year - startyear) * 365;
259 for (i = startyear; i < year; i++)
260 daysinyear += udf_isaleapyear(i);
261 t->tv_sec += daysinyear * 3600 * 24;
263 /* Calculate microseconds */
264 t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 +
268 * Calculate the time zone. The timezone is 12 bit signed 2's
269 * complement, so we gotta do some extra magic to handle it right.
271 tz.u_tz_offset = le16toh(time->type_tz);
272 tz.u_tz_offset &= 0x0fff;
273 if (tz.u_tz_offset & 0x0800)
274 tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */
275 if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047))
276 t->tv_sec -= tz.s_tz_offset * 60;
282 udf_getattr(struct vop_getattr_args *a)
285 struct udf_node *node;
287 struct file_entry *fentry;
295 fentry = node->fentry;
297 vap->va_fsid = dev2udev(node->udfmp->im_dev);
298 vap->va_fileid = node->hash_id;
299 vap->va_mode = udf_permtomode(node);
300 vap->va_nlink = le16toh(fentry->link_cnt);
302 * XXX The spec says that -1 is valid for uid/gid and indicates an
303 * invalid uid/gid. How should this be represented?
305 vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid);
306 vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid);
307 udf_timetotimespec(&fentry->atime, &vap->va_atime);
308 udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
309 vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
310 vap->va_rdev = NODEV;
311 if (vp->v_type & VDIR) {
313 * Directories that are recorded within their ICB will show
314 * as having 0 blocks recorded. Since tradition dictates
315 * that directories consume at least one logical block,
318 if (fentry->logblks_rec != 0) {
320 le64toh(fentry->logblks_rec) * node->udfmp->bsize;
322 vap->va_size = node->udfmp->bsize;
325 vap->va_size = le64toh(fentry->inf_len);
329 vap->va_blocksize = node->udfmp->bsize;
330 vap->va_bytes = le64toh(fentry->inf_len);
331 vap->va_type = vp->v_type;
332 vap->va_filerev = 0; /* XXX */
337 udf_setattr(struct vop_setattr_args *a)
344 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
345 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
346 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
348 if (vap->va_size != (u_quad_t)VNOVAL) {
349 switch (vp->v_type) {
369 * File specific ioctls.
372 udf_ioctl(struct vop_ioctl_args *a)
374 printf("%s called\n", __func__);
379 * I'm not sure that this has much value in a read-only filesystem, but
383 udf_pathconf(struct vop_pathconf_args *a)
388 *a->a_retval = 65535;
391 *a->a_retval = NAME_MAX;
394 *a->a_retval = PATH_MAX;
405 udf_print(struct vop_print_args *ap)
407 struct vnode *vp = ap->a_vp;
408 struct udf_node *node = VTON(vp);
410 printf(" ino %lu, on dev %s", (u_long)node->hash_id,
411 devtoname(node->udfmp->im_dev));
412 if (vp->v_type == VFIFO)
418 #define lblkno(udfmp, loc) ((loc) >> (udfmp)->bshift)
419 #define blkoff(udfmp, loc) ((loc) & (udfmp)->bmask)
420 #define lblktosize(udfmp, blk) ((blk) << (udfmp)->bshift)
423 is_data_in_fentry(const struct udf_node *node)
425 const struct file_entry *fentry = node->fentry;
427 return ((le16toh(fentry->icbtag.flags) & 0x7) == 3);
431 udf_read(struct vop_read_args *ap)
433 struct vnode *vp = ap->a_vp;
434 struct uio *uio = ap->a_uio;
435 struct udf_node *node = VTON(vp);
436 struct udf_mnt *udfmp;
437 struct file_entry *fentry;
440 daddr_t lbn, rablock;
446 if (uio->uio_resid == 0)
448 if (uio->uio_offset < 0)
451 if (is_data_in_fentry(node)) {
452 fentry = node->fentry;
453 data = &fentry->data[le32toh(fentry->l_ea)];
454 fsize = le32toh(fentry->l_ad);
457 diff = fsize - uio->uio_offset;
462 error = uiomove(data + uio->uio_offset, (int)n, uio);
466 fsize = le64toh(node->fentry->inf_len);
469 lbn = lblkno(udfmp, uio->uio_offset);
470 on = blkoff(udfmp, uio->uio_offset);
471 n = min((u_int)(udfmp->bsize - on),
473 diff = fsize - uio->uio_offset;
480 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
481 if (lblktosize(udfmp, rablock) < fsize) {
482 error = cluster_read(vp, fsize, lbn, size,
483 NOCRED, uio->uio_resid,
484 (ap->a_ioflag >> 16), &bp);
486 error = bread(vp, lbn, size, NOCRED, &bp);
489 error = bread(vp, lbn, size, NOCRED, &bp);
491 n = min(n, size - bp->b_resid);
497 error = uiomove(bp->b_data + on, (int)n, uio);
499 } while (error == 0 && uio->uio_resid > 0 && n != 0);
504 * Call the OSTA routines to translate the name from a CS0 dstring to a
505 * 16-bit Unicode String. Hooks need to be placed in here to translate from
506 * Unicode to the encoding that the kernel/user expects. Return the length
507 * of the translated string.
510 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
512 unicode_t *transname;
516 size_t destleft = MAXNAMLEN;
518 /* Convert 16-bit Unicode to destname */
519 if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
520 /* allocate a buffer big enough to hold an 8->16 bit expansion */
521 unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
523 if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
524 printf("udf: Unicode translation failed\n");
525 uma_zfree(udf_zone_trans, unibuf);
529 while (unilen > 0 && destleft > 0) {
530 udf_iconv->conv(udfmp->im_d2l, (const char **)&unibuf,
531 (size_t *)&unilen, (char **)&destname, &destleft);
532 /* Unconverted character found */
533 if (unilen > 0 && destleft > 0) {
540 uma_zfree(udf_zone_trans, unip);
542 destlen = MAXNAMLEN - (int)destleft;
544 /* allocate a buffer big enough to hold an 8->16 bit expansion */
545 transname = uma_zalloc(udf_zone_trans, M_WAITOK);
547 if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) {
548 printf("udf: Unicode translation failed\n");
549 uma_zfree(udf_zone_trans, transname);
553 for (i = 0; i < unilen ; i++) {
554 if (transname[i] & 0xff00) {
555 destname[i] = '.'; /* Fudge the 16bit chars */
557 destname[i] = transname[i] & 0xff;
560 uma_zfree(udf_zone_trans, transname);
561 destname[unilen] = 0;
562 destlen = (int)unilen;
569 * Compare a CS0 dstring with a name passed in from the VFS layer. Return
570 * 0 on a successful match, nonzero otherwise. Unicode work may need to be done
574 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
579 /* This is overkill, but not worth creating a new zone */
580 transname = uma_zalloc(udf_zone_trans, M_WAITOK);
582 cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
584 /* Easy check. If they aren't the same length, they aren't equal */
585 if ((cs0len == 0) || (cs0len != cmplen))
588 error = bcmp(transname, cmpname, cmplen);
590 uma_zfree(udf_zone_trans, transname);
595 struct dirent *dirent;
603 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
605 if (uiodir->cookies != NULL) {
606 if (++uiodir->acookies > uiodir->ncookies) {
610 *uiodir->cookies++ = cookie;
613 if (uio->uio_resid < de_size) {
618 return (uiomove(uiodir->dirent, de_size, uio));
621 static struct udf_dirstream *
622 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
624 struct udf_dirstream *ds;
626 ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
636 static struct fileid_desc *
637 udf_getfid(struct udf_dirstream *ds)
639 struct fileid_desc *fid;
640 int error, frag_size = 0, total_fid_size;
642 /* End of directory? */
643 if (ds->offset + ds->off >= ds->fsize) {
648 /* Grab the first extent of the directory */
651 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
662 * Clean up from a previous fragmented FID.
663 * XXX Is this the right place for this?
665 if (ds->fid_fragment && ds->buf != NULL) {
666 ds->fid_fragment = 0;
667 free(ds->buf, M_UDFFID);
670 fid = (struct fileid_desc*)&ds->data[ds->off];
673 * Check to see if the fid is fragmented. The first test
674 * ensures that we don't wander off the end of the buffer
675 * looking for the l_iu and l_fi fields.
677 if (ds->off + UDF_FID_SIZE > ds->size ||
678 ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){
680 /* Copy what we have of the fid into a buffer */
681 frag_size = ds->size - ds->off;
682 if (frag_size >= ds->udfmp->bsize) {
683 printf("udf: invalid FID fragment\n");
689 * File ID descriptors can only be at most one
690 * logical sector in size.
692 ds->buf = malloc(ds->udfmp->bsize, M_UDFFID,
694 bcopy(fid, ds->buf, frag_size);
696 /* Reduce all of the casting magic */
697 fid = (struct fileid_desc*)ds->buf;
702 /* Fetch the next allocation */
703 ds->offset += ds->size;
705 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
713 * If the fragment was so small that we didn't get
714 * the l_iu and l_fi fields, copy those in.
716 if (frag_size < UDF_FID_SIZE)
717 bcopy(ds->data, &ds->buf[frag_size],
718 UDF_FID_SIZE - frag_size);
721 * Now that we have enough of the fid to work with,
722 * copy in the rest of the fid from the new
725 total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi;
726 if (total_fid_size > ds->udfmp->bsize) {
727 printf("udf: invalid FID\n");
731 bcopy(ds->data, &ds->buf[frag_size],
732 total_fid_size - frag_size);
734 ds->fid_fragment = 1;
736 total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE;
740 * Update the offset. Align on a 4 byte boundary because the
743 ds->this_off = ds->offset + ds->off;
744 if (!ds->fid_fragment) {
745 ds->off += (total_fid_size + 3) & ~0x03;
747 ds->off = (total_fid_size - frag_size + 3) & ~0x03;
754 udf_closedir(struct udf_dirstream *ds)
760 if (ds->fid_fragment && ds->buf != NULL)
761 free(ds->buf, M_UDFFID);
763 uma_zfree(udf_zone_ds, ds);
767 udf_readdir(struct vop_readdir_args *a)
772 struct udf_node *node;
773 struct udf_mnt *udfmp;
774 struct fileid_desc *fid;
775 struct udf_uiodir uiodir;
776 struct udf_dirstream *ds;
777 u_long *cookies = NULL;
787 if (a->a_ncookies != NULL) {
789 * Guess how many entries are needed. If we run out, this
790 * function will be called again and thing will pick up were
793 ncookies = uio->uio_resid / 8;
794 cookies = malloc(sizeof(u_long) * ncookies,
798 uiodir.ncookies = ncookies;
799 uiodir.cookies = cookies;
802 uiodir.cookies = NULL;
806 * Iterate through the file id descriptors. Give the parent dir
807 * entry special attention.
809 ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
812 while ((fid = udf_getfid(ds)) != NULL) {
814 /* XXX Should we return an error on a bad fid? */
815 if (udf_checktag(&fid->tag, TAGID_FID)) {
816 printf("Invalid FID tag\n");
817 hexdump(fid, UDF_FID_SIZE, NULL, 0);
822 /* Is this a deleted file? */
823 if (fid->file_char & UDF_FILE_CHAR_DEL)
826 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
827 /* Do up the '.' and '..' entries. Dummy values are
828 * used for the cookies since the offset here is
829 * usually zero, and NFS doesn't like that value
831 dir.d_fileno = node->hash_id;
834 dir.d_name[1] = '\0';
836 dir.d_reclen = GENERIC_DIRSIZ(&dir);
837 uiodir.dirent = &dir;
838 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
842 dir.d_fileno = udf_getid(&fid->icb);
846 dir.d_name[2] = '\0';
848 dir.d_reclen = GENERIC_DIRSIZ(&dir);
849 uiodir.dirent = &dir;
850 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
852 dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
853 &dir.d_name[0], fid->l_fi, udfmp);
854 dir.d_fileno = udf_getid(&fid->icb);
855 dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
857 dir.d_reclen = GENERIC_DIRSIZ(&dir);
858 uiodir.dirent = &dir;
859 error = udf_uiodir(&uiodir, dir.d_reclen, uio,
864 uio->uio_offset = ds->offset + ds->off;
867 /* tell the calling layer whether we need to be called again */
868 *a->a_eofflag = uiodir.eofflag;
877 if (a->a_ncookies != NULL) {
879 free(cookies, M_TEMP);
881 *a->a_ncookies = uiodir.acookies;
882 *a->a_cookies = cookies;
890 udf_readlink(struct vop_readlink_args *ap)
892 struct path_component *pc, *end;
896 struct udf_node *node;
899 int error, len, root;
902 * A symbolic link in UDF is a list of variable-length path
903 * component structures. We build a pathname in the caller's
904 * uio by traversing this list.
908 len = le64toh(node->fentry->inf_len);
909 buf = malloc(len, M_DEVBUF, M_WAITOK);
910 iov[0].iov_len = len;
911 iov[0].iov_base = buf;
915 uio.uio_resid = iov[0].iov_len;
916 uio.uio_segflg = UIO_SYSSPACE;
917 uio.uio_rw = UIO_READ;
918 uio.uio_td = curthread;
919 error = VOP_READ(vp, &uio, 0, ap->a_cred);
924 end = (void *)((char *)buf + len);
929 /* Only allow this at the beginning of a path. */
930 if ((void *)pc != buf) {
942 case UDF_PATH_DOTDOT:
947 if (pc->length == 0) {
952 * XXX: We only support CS8 which appears to map
955 switch (pc->identifier[0]) {
957 cp = pc->identifier + 1;
958 len = pc->length - 1;
971 * If this is not the first component, insert a path
975 /* If we started with root we already have a "/". */
979 if (ap->a_uio->uio_resid < 1) {
980 error = ENAMETOOLONG;
983 error = uiomove("/", 1, ap->a_uio);
989 /* Append string at 'cp' of length 'len' to our path. */
990 if (len > ap->a_uio->uio_resid) {
991 error = ENAMETOOLONG;
994 error = uiomove(cp, len, ap->a_uio);
998 /* Advance to next component. */
999 pc = (void *)((char *)pc + 4 + pc->length);
1002 free(buf, M_DEVBUF);
1007 udf_strategy(struct vop_strategy_args *a)
1011 struct udf_node *node;
1022 if (bp->b_blkno == bp->b_lblkno) {
1023 offset = lblktosize(node->udfmp, bp->b_lblkno);
1024 error = udf_bmap_internal(node, offset, §or, &maxsize);
1031 /* bmap gives sector numbers, bio works with device blocks */
1032 bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT);
1034 bo = node->udfmp->im_bo;
1035 bp->b_iooffset = dbtob(bp->b_blkno);
1036 BO_STRATEGY(bo, bp);
1041 udf_bmap(struct vop_bmap_args *a)
1043 struct udf_node *node;
1049 node = VTON(a->a_vp);
1051 if (a->a_bop != NULL)
1052 *a->a_bop = &node->udfmp->im_devvp->v_bufobj;
1053 if (a->a_bnp == NULL)
1059 * UDF_INVALID_BMAP means data embedded into fentry, this is an internal
1060 * error that should not be propagated to calling code.
1061 * Most obvious mapping for this error is EOPNOTSUPP as we can not truly
1062 * translate block numbers in this case.
1063 * Incidentally, this return code will make vnode pager to use VOP_READ
1064 * to get data for mmap-ed pages and udf_read knows how to do the right
1065 * thing for this kind of files.
1067 error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift,
1068 &lsector, &max_size);
1069 if (error == UDF_INVALID_BMAP)
1070 return (EOPNOTSUPP);
1074 /* Translate logical to physical sector number */
1075 *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
1078 * Determine maximum number of readahead blocks following the
1082 nblk = (max_size >> node->udfmp->bshift) - 1;
1085 else if (nblk >= (MAXBSIZE >> node->udfmp->bshift))
1086 *a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1;
1099 * The all powerful VOP_LOOKUP().
1102 udf_lookup(struct vop_cachedlookup_args *a)
1105 struct vnode *tdp = NULL;
1106 struct vnode **vpp = a->a_vpp;
1107 struct udf_node *node;
1108 struct udf_mnt *udfmp;
1109 struct fileid_desc *fid = NULL;
1110 struct udf_dirstream *ds;
1116 int offset, error = 0;
1117 int fsize, lkflags, ltype, numdirpasses;
1121 udfmp = node->udfmp;
1122 nameiop = a->a_cnp->cn_nameiop;
1123 flags = a->a_cnp->cn_flags;
1124 lkflags = a->a_cnp->cn_lkflags;
1125 nameptr = a->a_cnp->cn_nameptr;
1126 namelen = a->a_cnp->cn_namelen;
1127 fsize = le64toh(node->fentry->inf_len);
1130 * If this is a LOOKUP and we've already partially searched through
1131 * the directory, pick up where we left off and flag that the
1132 * directory may need to be searched twice. For a full description,
1133 * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
1135 if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
1139 offset = node->diroff;
1141 nchstats.ncs_2passes++;
1145 ds = udf_opendir(node, offset, fsize, udfmp);
1147 while ((fid = udf_getfid(ds)) != NULL) {
1149 /* XXX Should we return an error on a bad fid? */
1150 if (udf_checktag(&fid->tag, TAGID_FID)) {
1151 printf("udf_lookup: Invalid tag\n");
1156 /* Is this a deleted file? */
1157 if (fid->file_char & UDF_FILE_CHAR_DEL)
1160 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
1161 if (flags & ISDOTDOT) {
1162 id = udf_getid(&fid->icb);
1166 if (!(udf_cmpname(&fid->data[fid->l_iu],
1167 nameptr, fid->l_fi, namelen, udfmp))) {
1168 id = udf_getid(&fid->icb);
1177 /* XXX Bail out here? */
1183 /* Did we have a match? */
1186 * Remember where this entry was if it's the final
1189 if ((flags & ISLASTCN) && nameiop == LOOKUP)
1190 node->diroff = ds->offset + ds->off;
1191 if (numdirpasses == 2)
1192 nchstats.ncs_pass2++;
1195 if (flags & ISDOTDOT) {
1196 error = vn_vget_ino(dvp, id, lkflags, &tdp);
1197 } else if (node->hash_id == id) {
1198 VREF(dvp); /* we want ourself, ie "." */
1200 * When we lookup "." we still can be asked to lock it
1203 ltype = lkflags & LK_TYPE_MASK;
1204 if (ltype != VOP_ISLOCKED(dvp)) {
1205 if (ltype == LK_EXCLUSIVE)
1206 vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1207 else /* if (ltype == LK_SHARED) */
1208 vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1212 error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp);
1215 /* Put this entry in the cache */
1216 if (flags & MAKEENTRY)
1217 cache_enter(dvp, *vpp, a->a_cnp);
1220 /* Name wasn't found on this pass. Do another pass? */
1221 if (numdirpasses == 2) {
1229 /* Enter name into cache as non-existant */
1230 if (flags & MAKEENTRY)
1231 cache_enter(dvp, *vpp, a->a_cnp);
1233 if ((flags & ISLASTCN) &&
1234 (nameiop == CREATE || nameiop == RENAME)) {
1245 udf_reclaim(struct vop_reclaim_args *a)
1248 struct udf_node *unode;
1254 * Destroy the vm object and flush associated pages.
1256 vnode_destroy_vobject(vp);
1258 if (unode != NULL) {
1259 vfs_hash_remove(vp);
1261 if (unode->fentry != NULL)
1262 free(unode->fentry, M_UDFFENTRY);
1263 uma_zfree(udf_zone_node, unode);
1271 udf_vptofh(struct vop_vptofh_args *a)
1273 struct udf_node *node;
1276 node = VTON(a->a_vp);
1277 ifhp = (struct ifid *)a->a_fhp;
1278 ifhp->ifid_len = sizeof(struct ifid);
1279 ifhp->ifid_ino = node->hash_id;
1285 * Read the block and then set the data pointer to correspond with the
1286 * offset passed in. Only read in at most 'size' bytes, and then set 'size'
1287 * to the number of bytes pointed to. If 'size' is zero, try to read in a
1290 * Note that *bp may be assigned error or not.
1294 udf_readatoffset(struct udf_node *node, int *size, off_t offset,
1295 struct buf **bp, uint8_t **data)
1297 struct udf_mnt *udfmp = node->udfmp;
1298 struct vnode *vp = node->i_vnode;
1299 struct file_entry *fentry;
1308 * This call is made *not* only to detect UDF_INVALID_BMAP case,
1309 * max_size is used as an ad-hoc read-ahead hint for "normal" case.
1311 error = udf_bmap_internal(node, offset, §or, &max_size);
1312 if (error == UDF_INVALID_BMAP) {
1314 * This error means that the file *data* is stored in the
1315 * allocation descriptor field of the file entry.
1317 fentry = node->fentry;
1318 *data = &fentry->data[le32toh(fentry->l_ea)];
1319 *size = le32toh(fentry->l_ad);
1320 if (offset >= *size)
1327 } else if (error != 0) {
1331 /* Adjust the size so that it is within range */
1332 if (*size == 0 || *size > max_size)
1336 * Because we will read starting at block boundary, we need to adjust
1337 * how much we need to read so that all promised data is in.
1338 * Also, we can't promise to read more than MAXBSIZE bytes starting
1339 * from block boundary, so adjust what we promise too.
1341 off = blkoff(udfmp, offset);
1342 *size = min(*size, MAXBSIZE - off);
1343 adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask;
1345 if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) {
1346 printf("warning: udf_readlblks returned error %d\n", error);
1347 /* note: *bp may be non-NULL */
1352 *data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
1357 * Translate a file offset into a logical block and then into a physical
1359 * max_size - maximum number of bytes that can be read starting from given
1360 * offset, rather than beginning of calculated sector number
1363 udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
1366 struct udf_mnt *udfmp;
1367 struct file_entry *fentry;
1369 struct icb_tag *tag;
1370 uint32_t icblen = 0;
1372 int ad_offset, ad_num = 0;
1375 udfmp = node->udfmp;
1376 fentry = node->fentry;
1377 tag = &fentry->icbtag;
1379 switch (le16toh(tag->strat_type)) {
1384 printf("Cannot deal with strategy4096 yet!\n");
1388 printf("Unknown strategy type %d\n", tag->strat_type);
1392 switch (le16toh(tag->flags) & 0x7) {
1395 * The allocation descriptor field is filled with short_ad's.
1396 * If the offset is beyond the current extent, look for the
1401 ad_offset = sizeof(struct short_ad) * ad_num;
1402 if (ad_offset > le32toh(fentry->l_ad)) {
1403 printf("File offset out of bounds\n");
1406 icb = GETICB(short_ad, fentry,
1407 le32toh(fentry->l_ea) + ad_offset);
1408 icblen = GETICBLEN(short_ad, icb);
1410 } while(offset >= icblen);
1412 lsector = (offset >> udfmp->bshift) +
1413 le32toh(((struct short_ad *)(icb))->pos);
1415 *max_size = icblen - offset;
1420 * The allocation descriptor field is filled with long_ad's
1421 * If the offset is beyond the current extent, look for the
1426 ad_offset = sizeof(struct long_ad) * ad_num;
1427 if (ad_offset > le32toh(fentry->l_ad)) {
1428 printf("File offset out of bounds\n");
1431 icb = GETICB(long_ad, fentry,
1432 le32toh(fentry->l_ea) + ad_offset);
1433 icblen = GETICBLEN(long_ad, icb);
1435 } while(offset >= icblen);
1437 lsector = (offset >> udfmp->bshift) +
1438 le32toh(((struct long_ad *)(icb))->loc.lb_num);
1440 *max_size = icblen - offset;
1445 * This type means that the file *data* is stored in the
1446 * allocation descriptor field of the file entry.
1449 *sector = node->hash_id + udfmp->part_start;
1451 return (UDF_INVALID_BMAP);
1453 /* DirectCD does not use extended_ad's */
1455 printf("Unsupported allocation descriptor %d\n",
1460 *sector = lsector + udfmp->part_start;
1463 * Check the sparing table. Each entry represents the beginning of
1466 if (udfmp->s_table != NULL) {
1467 for (i = 0; i< udfmp->s_table_entries; i++) {
1469 lsector - le32toh(udfmp->s_table->entries[i].org);
1470 if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1472 le32toh(udfmp->s_table->entries[i].map) +