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
26 * $FreeBSD: src/sys/fs/udf/udf_vnops.c,v 1.33 2003/12/07 05:04:49 scottl Exp $
27 * $DragonFly: src/sys/vfs/udf/udf_vnops.c,v 1.4 2004/04/02 05:46:03 hmp Exp $
31 /* Take care of the vnode side of things */
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/namei.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
39 #include <sys/module.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>
48 #include <vfs/udf/ecma167-udf.h>
49 #include <vfs/udf/osta.h>
50 #include <vfs/udf/udf.h>
51 #include <vfs/udf/udf_mount.h>
53 static int udf_access(struct vop_access_args *);
54 static int udf_getattr(struct vop_getattr_args *);
55 static int udf_ioctl(struct vop_ioctl_args *);
56 static int udf_pathconf(struct vop_pathconf_args *);
57 static int udf_read(struct vop_read_args *);
58 static int udf_readdir(struct vop_readdir_args *);
59 static int udf_readlink(struct vop_readlink_args *ap);
60 static int udf_strategy(struct vop_strategy_args *);
61 static int udf_bmap(struct vop_bmap_args *);
62 static int udf_lookup(struct vop_cachedlookup_args *);
63 static int udf_reclaim(struct vop_reclaim_args *);
64 static int udf_readatoffset(struct udf_node *, int *, int, struct buf **, uint8_t **);
65 static int udf_bmap_internal(struct udf_node *, uint32_t, daddr_t *, uint32_t *);
67 vop_t **udf_vnodeop_p;
68 static struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
69 { &vop_default_desc, (vop_t *) vop_defaultop },
70 { &vop_access_desc, (vop_t *) udf_access },
71 { &vop_bmap_desc, (vop_t *) udf_bmap },
72 { &vop_cachedlookup_desc, (vop_t *) udf_lookup },
73 { &vop_getattr_desc, (vop_t *) udf_getattr },
74 { &vop_ioctl_desc, (vop_t *) udf_ioctl },
75 { &vop_lookup_desc, (vop_t *) vfs_cache_lookup },
76 { &vop_pathconf_desc, (vop_t *) udf_pathconf },
77 { &vop_read_desc, (vop_t *) udf_read },
78 { &vop_readdir_desc, (vop_t *) udf_readdir },
79 { &vop_readlink_desc, (vop_t *) udf_readlink },
80 { &vop_reclaim_desc, (vop_t *) udf_reclaim },
81 { &vop_strategy_desc, (vop_t *) udf_strategy },
84 static struct vnodeopv_desc udf_vnodeop_opv_desc =
85 { &udf_vnodeop_p, udf_vnodeop_entries };
86 VNODEOP_SET(udf_vnodeop_opv_desc);
88 MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure");
89 MALLOC_DEFINE(M_UDFDS, "UDF DS", "UDF Dirstream structure");
91 #define UDF_INVALID_BMAP -1
93 /* Look up a udf_node based on the ino_t passed in and return it's vnode */
95 udf_hashlookup(struct udf_mnt *udfmp, ino_t id, struct vnode **vpp)
97 struct udf_node *node;
98 struct udf_hash_lh *lh;
100 lwkt_tokref vlock, hashlock;
104 lwkt_gettoken(&hashlock, &udfmp->hash_token);
106 lh = &udfmp->hashtbl[id % udfmp->hashsz];
109 LIST_FOREACH(node, lh, le) {
110 if (node->hash_id != id)
112 lwkt_gettoken(&vlock, node->i_vnode->v_interlock);
114 * We must check to see if the inode has been ripped
115 * out from under us after blocking.
117 lh = &udfmp->hashtbl[id % udfmp->hashsz];
118 LIST_FOREACH(node, lh, le)
119 if (node->hash_id == id)
122 lwkt_reltoken(&vlock);
125 error = vget(node->i_vnode, &vlock, LK_EXCLUSIVE | LK_INTERLOCK,
129 lwkt_reltoken(&hashlock);
132 *vpp = node->i_vnode;
136 lwkt_reltoken(&hashlock);
141 udf_hashins(struct udf_node *node)
143 struct udf_mnt *udfmp;
144 struct udf_hash_lh *lh;
145 lwkt_tokref hashlock;
149 vn_lock(node->i_vnode, NULL, LK_EXCLUSIVE | LK_RETRY, curthread);
150 lwkt_gettoken(&hashlock, &udfmp->hash_token);
151 lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
154 LIST_INSERT_HEAD(lh, node, le);
155 lwkt_reltoken(&hashlock);
161 udf_hashrem(struct udf_node *node)
163 struct udf_mnt *udfmp;
164 struct udf_hash_lh *lh;
165 lwkt_tokref hashlock;
169 lwkt_gettoken(&hashlock, &udfmp->hash_token);
170 lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
172 panic("hash entry is NULL, node->hash_id= %d\n", node->hash_id);
173 LIST_REMOVE(node, le);
174 lwkt_reltoken(&hashlock);
180 udf_allocv(struct mount *mp, struct vnode **vpp)
185 error = getnewvnode(VT_UDF, mp, udf_vnodeop_p, &vp);
187 printf("udf_allocv: failed to allocate new vnode\n");
195 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
197 udf_permtomode(struct udf_node *node)
203 perm = node->fentry->perm;
204 flags = node->fentry->icbtag.flags;
206 mode = perm & UDF_FENTRY_PERM_USER_MASK;
207 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
208 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
209 mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
210 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
211 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
217 udf_access(struct vop_access_args *a)
220 struct udf_node *node;
221 mode_t a_mode, mode, mask;
222 struct ucred *cred = a->a_cred;
230 if (a_mode & VWRITE) {
231 switch (vp->v_type) {
242 mode = udf_permtomode(node);
244 if (cred->cr_uid == 0)
249 /* Otherwise, check the owner. */
250 if (cred->cr_uid == node->fentry->uid) {
257 return((mode & mask) == mask ? 0 : EACCES);
260 /* Otherwise, check the groups. */
261 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
262 if (node->fentry->gid == *gp) {
269 return((mode & mask) == mask ? 0 : EACCES);
272 /* Otherwise, check everyone else. */
279 return((mode & mask) == mask ? 0 : EACCES);
282 static int mon_lens[2][12] = {
283 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
284 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
288 udf_isaleapyear(int year)
292 i = (year % 4) ? 0 : 1;
293 i &= (year % 100) ? 1 : 0;
294 i |= (year % 400) ? 0 : 1;
300 * XXX This is just a rough hack. Daylight savings isn't calculated and tv_nsec
302 * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
305 udf_timetotimespec(struct timestamp *time, struct timespec *t)
307 int i, lpyear, daysinyear;
309 uint16_t u_tz_offset;
315 /* DirectCD seems to like using bogus year values */
316 if (time->year < 1970) {
321 /* Calculate the time and day */
322 t->tv_sec = time->second;
323 t->tv_sec += time->minute * 60;
324 t->tv_sec += time->hour * 3600;
325 t->tv_sec += time->day * 3600 * 24;
327 /* Calclulate the month */
328 lpyear = udf_isaleapyear(time->year);
329 for (i = 1; i < time->month; i++)
330 t->tv_sec += mon_lens[lpyear][i] * 3600 * 24;
332 /* Speed up the calculation */
333 if (time->year > 1979)
334 t->tv_sec += 315532800;
335 if (time->year > 1989)
336 t->tv_sec += 315619200;
337 if (time->year > 1999)
338 t->tv_sec += 315532800;
339 for (i = 2000; i < time->year; i++) {
340 daysinyear = udf_isaleapyear(i) + 365 ;
341 t->tv_sec += daysinyear * 3600 * 24;
345 * Calculate the time zone. The timezone is 12 bit signed 2's
346 * compliment, so we gotta do some extra magic to handle it right.
348 tz.u_tz_offset = time->type_tz;
349 tz.u_tz_offset &= 0x0fff;
350 if (tz.u_tz_offset & 0x0800)
351 tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */
352 if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047))
353 t->tv_sec -= tz.s_tz_offset * 60;
359 udf_getattr(struct vop_getattr_args *a)
362 struct udf_node *node;
364 struct file_entry *fentry;
372 fentry = node->fentry;
374 vap->va_fsid = dev2udev(node->i_dev);
375 vap->va_fileid = node->hash_id;
376 vap->va_mode = udf_permtomode(node);
377 vap->va_nlink = fentry->link_cnt;
379 * XXX The spec says that -1 is valid for uid/gid and indicates an
380 * invalid uid/gid. How should this be represented?
382 vap->va_uid = (fentry->uid == 0xffffffff) ? 0 : fentry->uid;
383 vap->va_gid = (fentry->gid == 0xffffffff) ? 0 : fentry->gid;
384 udf_timetotimespec(&fentry->atime, &vap->va_atime);
385 udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
386 vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
387 vap->va_rdev = 0; /* XXX */
388 if (vp->v_type & VDIR) {
390 * Directories that are recorded within their ICB will show
391 * as having 0 blocks recorded. Since tradition dictates
392 * that directories consume at least one logical block,
395 if (fentry->logblks_rec != 0)
396 vap->va_size = fentry->logblks_rec * node->udfmp->bsize;
398 vap->va_size = node->udfmp->bsize;
400 vap->va_size = fentry->inf_len;
403 vap->va_blocksize = node->udfmp->bsize;
404 vap->va_bytes = fentry->inf_len;
405 vap->va_type = vp->v_type;
406 vap->va_filerev = 0; /* XXX */
411 * File specific ioctls. DeCSS candidate?
414 udf_ioctl(struct vop_ioctl_args *a)
416 printf("%s called\n", __FUNCTION__);
421 * I'm not sure that this has much value in a read-only filesystem, but
425 udf_pathconf(struct vop_pathconf_args *a)
430 *a->a_retval = 65535;
433 *a->a_retval = NAME_MAX;
436 *a->a_retval = PATH_MAX;
447 udf_read(struct vop_read_args *a)
449 struct vnode *vp = a->a_vp;
450 struct uio *uio = a->a_uio;
451 struct udf_node *node = VTON(vp);
455 int size, fsize, offset;
457 if (uio->uio_offset < 0)
460 fsize = node->fentry->inf_len;
462 while (uio->uio_offset < fsize && uio->uio_resid > 0) {
463 offset = uio->uio_offset;
464 size = uio->uio_resid;
465 error = udf_readatoffset(node, &size, offset, &bp, &data);
467 error = uiomove(data, size, uio);
478 * Call the OSTA routines to translate the name from a CS0 dstring to a
479 * 16-bit Unicode String. Hooks need to be placed in here to translate from
480 * Unicode to the encoding that the kernel/user expects. Return the length
481 * of the translated string.
484 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
486 unicode_t *transname;
487 int i, unilen = 0, destlen;
489 /* Convert 16-bit Unicode to destname */
490 /* allocate a buffer big enough to hold an 8->16 bit expansion */
491 transname = malloc(MAXNAMLEN * sizeof(unicode_t), M_TEMP, M_WAITOK | M_ZERO);
493 if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) {
494 printf("udf: Unicode translation failed\n");
495 free(transname, M_TEMP);
499 for (i = 0; i < unilen ; i++)
500 if (transname[i] & 0xff00)
501 destname[i] = '.'; /* Fudge the 16bit chars */
503 destname[i] = transname[i] & 0xff;
504 free(transname, M_TEMP);
505 destname[unilen] = 0;
512 * Compare a CS0 dstring with a name passed in from the VFS layer. Return
513 * 0 on a successful match, nonzero therwise. Unicode work may need to be done
517 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
522 /* This is overkill, but not worth creating a new zone */
524 transname = malloc(MAXNAMLEN * sizeof(unicode_t), M_TEMP,
527 cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
529 /* Easy check. If they aren't the same length, they aren't equal */
530 if ((cs0len == 0) || (cs0len != cmplen))
533 error = bcmp(transname, cmpname, cmplen);
535 free(transname, M_TEMP);
540 struct dirent *dirent;
548 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
550 if (uiodir->cookies != NULL) {
551 if (++uiodir->acookies > uiodir->ncookies) {
555 *uiodir->cookies++ = cookie;
558 if (uio->uio_resid < de_size) {
563 return(uiomove((caddr_t)uiodir->dirent, de_size, uio));
566 static struct udf_dirstream *
567 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
569 struct udf_dirstream *ds;
571 ds = malloc(sizeof(*ds), M_UDFDS, M_WAITOK | M_ZERO);
581 static struct fileid_desc *
582 udf_getfid(struct udf_dirstream *ds)
584 struct fileid_desc *fid;
585 int error, frag_size = 0, total_fid_size;
587 /* End of directory? */
588 if (ds->offset + ds->off >= ds->fsize) {
593 /* Grab the first extent of the directory */
598 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
607 * Clean up from a previous fragmented FID.
608 * XXX Is this the right place for this?
610 if (ds->fid_fragment && ds->buf != NULL) {
611 ds->fid_fragment = 0;
612 free(ds->buf, M_UDFFID);
615 fid = (struct fileid_desc*)&ds->data[ds->off];
618 * Check to see if the fid is fragmented. The first test
619 * ensures that we don't wander off the end of the buffer
620 * looking for the l_iu and l_fi fields.
622 if (ds->off + UDF_FID_SIZE > ds->size ||
623 ds->off + fid->l_iu + fid->l_fi + UDF_FID_SIZE > ds->size) {
625 /* Copy what we have of the fid into a buffer */
626 frag_size = ds->size - ds->off;
627 if (frag_size >= ds->udfmp->bsize) {
628 printf("udf: invalid FID fragment\n");
634 * File ID descriptors can only be at most one
635 * logical sector in size.
637 ds->buf = malloc(ds->udfmp->bsize, M_UDFFID, M_WAITOK | M_ZERO);
638 bcopy(fid, ds->buf, frag_size);
640 /* Reduce all of the casting magic */
641 fid = (struct fileid_desc*)ds->buf;
646 /* Fetch the next allocation */
647 ds->offset += ds->size;
649 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
657 * If the fragment was so small that we didn't get
658 * the l_iu and l_fi fields, copy those in.
660 if (frag_size < UDF_FID_SIZE)
661 bcopy(ds->data, &ds->buf[frag_size],
662 UDF_FID_SIZE - frag_size);
665 * Now that we have enough of the fid to work with,
666 * copy in the rest of the fid from the new
669 total_fid_size = UDF_FID_SIZE + fid->l_iu + fid->l_fi;
670 if (total_fid_size > ds->udfmp->bsize) {
671 printf("udf: invalid FID\n");
675 bcopy(ds->data, &ds->buf[frag_size],
676 total_fid_size - frag_size);
678 ds->fid_fragment = 1;
680 total_fid_size = fid->l_iu + fid->l_fi + UDF_FID_SIZE;
683 * Update the offset. Align on a 4 byte boundary because the
686 ds->this_off = ds->off;
687 if (!ds->fid_fragment)
688 ds->off += (total_fid_size + 3) & ~0x03;
690 ds->off = (total_fid_size - frag_size + 3) & ~0x03;
696 udf_closedir(struct udf_dirstream *ds)
702 if (ds->fid_fragment && ds->buf != NULL)
703 free(ds->buf, M_UDFFID);
709 udf_readdir(struct vop_readdir_args *a)
714 struct udf_node *node;
715 struct udf_mnt *udfmp;
716 struct fileid_desc *fid;
717 struct udf_uiodir uiodir;
718 struct udf_dirstream *ds;
719 u_long *cookies = NULL;
729 if (a->a_ncookies != NULL) {
731 * Guess how many entries are needed. If we run out, this
732 * function will be called again and thing will pick up were
735 ncookies = uio->uio_resid / 8;
736 cookies = malloc(sizeof(u_long) * ncookies, M_TEMP, M_WAITOK);
737 uiodir.ncookies = ncookies;
738 uiodir.cookies = cookies;
741 uiodir.cookies = NULL;
744 * Iterate through the file id descriptors. Give the parent dir
745 * entry special attention.
747 ds = udf_opendir(node, uio->uio_offset, node->fentry->inf_len,
750 while ((fid = udf_getfid(ds)) != NULL) {
752 /* XXX Should we return an error on a bad fid? */
753 if (udf_checktag(&fid->tag, TAGID_FID)) {
754 printf("Invalid FID tag\n");
759 /* Is this a deleted file? */
760 if (fid->file_char & UDF_FILE_CHAR_DEL)
763 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
764 /* Do up the '.' and '..' entries. Dummy values are
765 * used for the cookies since the offset here is
766 * usually zero, and NFS doesn't like that value
768 dir.d_fileno = node->hash_id;
772 dir.d_reclen = GENERIC_DIRSIZ(&dir);
773 uiodir.dirent = &dir;
774 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
778 dir.d_fileno = udf_getid(&fid->icb);
783 dir.d_reclen = GENERIC_DIRSIZ(&dir);
784 uiodir.dirent = &dir;
785 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
787 dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
788 &dir.d_name[0], fid->l_fi, udfmp);
789 dir.d_fileno = udf_getid(&fid->icb);
790 dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
792 dir.d_reclen = GENERIC_DIRSIZ(&dir);
793 uiodir.dirent = &dir;
794 error = udf_uiodir(&uiodir, dir.d_reclen, uio,
798 printf("uiomove returned %d\n", error);
804 /* tell the calling layer whether we need to be called again */
805 *a->a_eofflag = uiodir.eofflag;
806 uio->uio_offset = ds->offset + ds->off;
813 if (a->a_ncookies != NULL) {
815 free(cookies, M_TEMP);
817 *a->a_ncookies = uiodir.acookies;
818 *a->a_cookies = cookies;
825 /* Are there any implementations out there that do soft-links? */
827 udf_readlink(struct vop_readlink_args *ap)
829 printf("%s called\n", __FUNCTION__);
834 udf_strategy(struct vop_strategy_args *a)
838 struct udf_node *node;
845 KASSERT(a->a_vp == a->a_bp->b_vp, ("%s(%p != %p)",
846 __func__, a->a_vp, a->a_bp->b_vp));
847 /* cd9660 has this test reversed, but it seems more logical this way */
848 if (bp->b_blkno != bp->b_lblkno) {
850 * Files that are embedded in the fentry don't translate well
851 * to a block number. Reject.
853 if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize,
854 &bp->b_lblkno, &maxsize)) {
859 if ((long)bp->b_blkno == -1) {
864 bp->b_dev = vp->v_rdev;
865 bp->b_offset = dbtob(bp->b_blkno);
866 VOP_STRATEGY(vp, bp);
871 udf_bmap(struct vop_bmap_args *a)
873 struct udf_node *node;
878 node = VTON(a->a_vp);
880 if (a->a_vpp != NULL)
881 *a->a_vpp = node->i_devvp;
882 if (a->a_bnp == NULL)
887 error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector,
892 /* Translate logical to physical sector number */
893 *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
895 /* Punt on read-ahead for now */
903 * The all powerful VOP_LOOKUP().
906 udf_lookup(struct vop_cachedlookup_args *a)
909 struct vnode *tdp = NULL;
910 struct vnode **vpp = a->a_vpp;
911 struct udf_node *node;
912 struct udf_mnt *udfmp;
913 struct fileid_desc *fid = NULL;
914 struct udf_dirstream *ds;
916 globaldata_t gd = mycpu;
922 int offset, error = 0;
923 int numdirpasses, fsize;
928 nameiop = a->a_cnp->cn_nameiop;
929 flags = a->a_cnp->cn_flags;
930 nameptr = a->a_cnp->cn_nameptr;
931 namelen = a->a_cnp->cn_namelen;
932 fsize = node->fentry->inf_len;
933 td = a->a_cnp->cn_td;
936 * If this is a LOOKUP and we've already partially searched through
937 * the directory, pick up where we left off and flag that the
938 * directory may need to be searched twice. For a full description,
939 * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup()
941 if (nameiop != NAMEI_LOOKUP || node->diroff == 0 ||
942 node->diroff > fsize) {
946 offset = node->diroff;
948 gd->gd_nchstats->ncs_2passes++;
952 ds = udf_opendir(node, offset, fsize, udfmp);
954 while ((fid = udf_getfid(ds)) != NULL) {
955 /* XXX Should we return an error on a bad fid? */
956 if (udf_checktag(&fid->tag, TAGID_FID)) {
957 printf("udf_lookup: Invalid tag\n");
962 /* Is this a deleted file? */
963 if (fid->file_char & UDF_FILE_CHAR_DEL)
966 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
967 if (flags & CNP_ISDOTDOT) {
968 id = udf_getid(&fid->icb);
972 if (!(udf_cmpname(&fid->data[fid->l_iu],
973 nameptr, fid->l_fi, namelen, udfmp))) {
974 id = udf_getid(&fid->icb);
983 /* XXX Bail out here? */
989 /* Did we have a match? */
991 error = udf_vget(udfmp->im_mountp, id, &tdp);
994 * Remember where this entry was if it's the final
997 if ((flags & CNP_ISLASTCN) && nameiop == NAMEI_LOOKUP)
998 node->diroff = ds->offset + ds->off;
999 if (numdirpasses == 2)
1000 gd->gd_nchstats->ncs_pass2++;
1001 if (!(flags & CNP_LOCKPARENT) || !(flags & CNP_ISLASTCN)) {
1002 a->a_cnp->cn_flags |= CNP_PDIRUNLOCK;
1003 VOP_UNLOCK(dvp, NULL, 0, td);
1008 /* Put this entry in the cache */
1009 if (flags & CNP_MAKEENTRY)
1010 cache_enter(dvp, NCPNULL, *vpp, a->a_cnp);
1013 /* Name wasn't found on this pass. Do another pass? */
1014 if (numdirpasses == 2) {
1021 /* Enter name into cache as non-existant */
1022 if (flags & CNP_MAKEENTRY)
1023 cache_enter(dvp, NCPNULL, *vpp, a->a_cnp);
1025 if ((flags & CNP_ISLASTCN) &&
1026 (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)) {
1038 udf_reclaim(struct vop_reclaim_args *a)
1041 struct udf_node *unode;
1046 if (unode != NULL) {
1048 if (unode->i_devvp) {
1049 vrele(unode->i_devvp);
1053 if (unode->fentry != NULL)
1054 free(unode->fentry, M_UDFFENTRY);
1055 free(unode, M_UDFNODE);
1063 * Read the block and then set the data pointer to correspond with the
1064 * offset passed in. Only read in at most 'size' bytes, and then set 'size'
1065 * to the number of bytes pointed to. If 'size' is zero, try to read in a
1068 * Note that *bp may be assigned error or not.
1070 * XXX 'size' is limited to the logical block size for now due to problems
1074 udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp,
1077 struct udf_mnt *udfmp;
1078 struct file_entry *fentry = NULL;
1084 udfmp = node->udfmp;
1087 error = udf_bmap_internal(node, offset, §or, &max_size);
1088 if (error == UDF_INVALID_BMAP) {
1090 * This error means that the file *data* is stored in the
1091 * allocation descriptor field of the file entry.
1093 fentry = node->fentry;
1094 *data = &fentry->data[fentry->l_ea];
1095 *size = fentry->l_ad;
1097 } else if (error != 0) {
1101 /* Adjust the size so that it is within range */
1102 if (*size == 0 || *size > max_size)
1104 *size = min(*size, MAXBSIZE);
1106 if ((error = udf_readlblks(udfmp, sector, *size, bp))) {
1107 printf("warning: udf_readlblks returned error %d\n", error);
1108 /* note: *bp may be non-NULL */
1113 *data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize];
1118 * Translate a file offset into a logical block and then into a physical
1122 udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size)
1124 struct udf_mnt *udfmp;
1125 struct file_entry *fentry;
1127 struct icb_tag *tag;
1128 uint32_t icblen = 0;
1130 int ad_offset, ad_num = 0;
1133 udfmp = node->udfmp;
1134 fentry = node->fentry;
1135 tag = &fentry->icbtag;
1137 switch (tag->strat_type) {
1142 printf("Cannot deal with strategy4096 yet!\n");
1146 printf("Unknown strategy type %d\n", tag->strat_type);
1150 switch (tag->flags & 0x7) {
1153 * The allocation descriptor field is filled with short_ad's.
1154 * If the offset is beyond the current extent, look for the
1159 ad_offset = sizeof(struct short_ad) * ad_num;
1160 if (ad_offset > fentry->l_ad) {
1161 printf("File offset out of bounds\n");
1164 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1165 icblen = GETICBLEN(short_ad, icb);
1167 } while(offset >= icblen);
1169 lsector = (offset >> udfmp->bshift) +
1170 ((struct short_ad *)(icb))->pos;
1172 *max_size = GETICBLEN(short_ad, icb);
1177 * The allocation descriptor field is filled with long_ad's
1178 * If the offset is beyond the current extent, look for the
1183 ad_offset = sizeof(struct long_ad) * ad_num;
1184 if (ad_offset > fentry->l_ad) {
1185 printf("File offset out of bounds\n");
1188 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1189 icblen = GETICBLEN(long_ad, icb);
1191 } while(offset >= icblen);
1193 lsector = (offset >> udfmp->bshift) +
1194 ((struct long_ad *)(icb))->loc.lb_num;
1196 *max_size = GETICBLEN(long_ad, icb);
1201 * This type means that the file *data* is stored in the
1202 * allocation descriptor field of the file entry.
1205 *sector = node->hash_id + udfmp->part_start;
1207 return(UDF_INVALID_BMAP);
1209 /* DirectCD does not use extended_ad's */
1211 printf("Unsupported allocation descriptor %d\n",
1216 *sector = lsector + udfmp->part_start;
1219 * Check the sparing table. Each entry represents the beginning of
1222 if (udfmp->s_table != NULL) {
1223 for (i = 0; i< udfmp->s_table_entries; i++) {
1224 p_offset = lsector - udfmp->s_table->entries[i].org;
1225 if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1226 *sector = udfmp->s_table->entries[i].map +