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.14 2005/08/02 13:03:55 joerg 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 <machine/inttypes.h>
50 #include <vfs/udf/ecma167-udf.h>
51 #include <vfs/udf/osta.h>
52 #include <vfs/udf/udf.h>
53 #include <vfs/udf/udf_mount.h>
55 static int udf_access(struct vop_access_args *);
56 static int udf_getattr(struct vop_getattr_args *);
57 static int udf_ioctl(struct vop_ioctl_args *);
58 static int udf_pathconf(struct vop_pathconf_args *);
59 static int udf_read(struct vop_read_args *);
60 static int udf_readdir(struct vop_readdir_args *);
61 static int udf_readlink(struct vop_readlink_args *ap);
62 static int udf_strategy(struct vop_strategy_args *);
63 static int udf_bmap(struct vop_bmap_args *);
64 static int udf_lookup(struct vop_lookup_args *);
65 static int udf_reclaim(struct vop_reclaim_args *);
66 static int udf_readatoffset(struct udf_node *, int *, int, struct buf **, uint8_t **);
67 static int udf_bmap_internal(struct udf_node *, uint32_t, daddr_t *, uint32_t *);
69 struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
70 { &vop_default_desc, vop_defaultop },
71 { &vop_access_desc, (vnodeopv_entry_t) udf_access },
72 { &vop_bmap_desc, (vnodeopv_entry_t) udf_bmap },
73 { &vop_lookup_desc, (vnodeopv_entry_t) udf_lookup },
74 { &vop_getattr_desc, (vnodeopv_entry_t) udf_getattr },
75 { &vop_ioctl_desc, (vnodeopv_entry_t) udf_ioctl },
76 { &vop_pathconf_desc, (vnodeopv_entry_t) udf_pathconf },
77 { &vop_read_desc, (vnodeopv_entry_t) udf_read },
78 { &vop_readdir_desc, (vnodeopv_entry_t) udf_readdir },
79 { &vop_readlink_desc, (vnodeopv_entry_t) udf_readlink },
80 { &vop_reclaim_desc, (vnodeopv_entry_t) udf_reclaim },
81 { &vop_strategy_desc, (vnodeopv_entry_t) udf_strategy },
85 MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure");
86 MALLOC_DEFINE(M_UDFDS, "UDF DS", "UDF Dirstream structure");
88 #define UDF_INVALID_BMAP -1
90 /* Look up a udf_node based on the ino_t passed in and return it's vnode */
92 udf_hashlookup(struct udf_mnt *udfmp, ino_t id, struct vnode **vpp)
94 struct udf_node *node;
95 struct udf_hash_lh *lh;
101 lwkt_gettoken(&hashlock, &udfmp->hash_token);
103 lh = &udfmp->hashtbl[id % udfmp->hashsz];
105 lwkt_reltoken(&hashlock);
108 LIST_FOREACH(node, lh, le) {
109 if (node->hash_id != id)
112 if (vget(vp, LK_EXCLUSIVE, curthread))
115 * We must check to see if the inode has been ripped
116 * out from under us after blocking.
118 lh = &udfmp->hashtbl[id % udfmp->hashsz];
119 LIST_FOREACH(node, lh, le) {
120 if (node->hash_id == id)
123 if (node == NULL || vp != node->i_vnode) {
127 lwkt_reltoken(&hashlock);
132 lwkt_reltoken(&hashlock);
137 udf_hashins(struct udf_node *node)
139 struct udf_mnt *udfmp;
140 struct udf_hash_lh *lh;
141 lwkt_tokref hashlock;
145 lwkt_gettoken(&hashlock, &udfmp->hash_token);
146 lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
149 LIST_INSERT_HEAD(lh, node, le);
150 lwkt_reltoken(&hashlock);
156 udf_hashrem(struct udf_node *node)
158 struct udf_mnt *udfmp;
159 struct udf_hash_lh *lh;
160 lwkt_tokref hashlock;
164 lwkt_gettoken(&hashlock, &udfmp->hash_token);
165 lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
167 panic("hash entry is NULL, node->hash_id= %"PRId64"\n", node->hash_id);
168 LIST_REMOVE(node, le);
169 lwkt_reltoken(&hashlock);
175 udf_allocv(struct mount *mp, struct vnode **vpp)
180 error = getnewvnode(VT_UDF, mp, &vp, 0, 0);
182 printf("udf_allocv: failed to allocate new vnode\n");
190 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
192 udf_permtomode(struct udf_node *node)
198 perm = node->fentry->perm;
199 flags = node->fentry->icbtag.flags;
201 mode = perm & UDF_FENTRY_PERM_USER_MASK;
202 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
203 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
204 mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
205 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
206 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
212 udf_access(struct vop_access_args *a)
215 struct udf_node *node;
216 mode_t a_mode, mode, mask;
217 struct ucred *cred = a->a_cred;
225 if (a_mode & VWRITE) {
226 switch (vp->v_type) {
237 mode = udf_permtomode(node);
239 if (cred->cr_uid == 0)
244 /* Otherwise, check the owner. */
245 if (cred->cr_uid == node->fentry->uid) {
252 return((mode & mask) == mask ? 0 : EACCES);
255 /* Otherwise, check the groups. */
256 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
257 if (node->fentry->gid == *gp) {
264 return((mode & mask) == mask ? 0 : EACCES);
267 /* Otherwise, check everyone else. */
274 return((mode & mask) == mask ? 0 : EACCES);
277 static int mon_lens[2][12] = {
278 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
279 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
283 udf_isaleapyear(int year)
287 i = (year % 4) ? 0 : 1;
288 i &= (year % 100) ? 1 : 0;
289 i |= (year % 400) ? 0 : 1;
295 * XXX This is just a rough hack. Daylight savings isn't calculated and tv_nsec
297 * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
300 udf_timetotimespec(struct timestamp *time, struct timespec *t)
302 int i, lpyear, daysinyear;
304 uint16_t u_tz_offset;
310 /* DirectCD seems to like using bogus year values */
311 if (time->year < 1970) {
316 /* Calculate the time and day */
317 t->tv_sec = time->second;
318 t->tv_sec += time->minute * 60;
319 t->tv_sec += time->hour * 3600;
320 t->tv_sec += time->day * 3600 * 24;
322 /* Calclulate the month */
323 lpyear = udf_isaleapyear(time->year);
324 for (i = 1; i < time->month; i++)
325 t->tv_sec += mon_lens[lpyear][i] * 3600 * 24;
327 /* Speed up the calculation */
328 if (time->year > 1979)
329 t->tv_sec += 315532800;
330 if (time->year > 1989)
331 t->tv_sec += 315619200;
332 if (time->year > 1999)
333 t->tv_sec += 315532800;
334 for (i = 2000; i < time->year; i++) {
335 daysinyear = udf_isaleapyear(i) + 365 ;
336 t->tv_sec += daysinyear * 3600 * 24;
340 * Calculate the time zone. The timezone is 12 bit signed 2's
341 * compliment, so we gotta do some extra magic to handle it right.
343 tz.u_tz_offset = time->type_tz;
344 tz.u_tz_offset &= 0x0fff;
345 if (tz.u_tz_offset & 0x0800)
346 tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */
347 if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047))
348 t->tv_sec -= tz.s_tz_offset * 60;
354 udf_getattr(struct vop_getattr_args *a)
357 struct udf_node *node;
359 struct file_entry *fentry;
367 fentry = node->fentry;
369 vap->va_fsid = dev2udev(node->i_dev);
370 vap->va_fileid = node->hash_id;
371 vap->va_mode = udf_permtomode(node);
372 vap->va_nlink = fentry->link_cnt;
374 * XXX The spec says that -1 is valid for uid/gid and indicates an
375 * invalid uid/gid. How should this be represented?
377 vap->va_uid = (fentry->uid == 0xffffffff) ? 0 : fentry->uid;
378 vap->va_gid = (fentry->gid == 0xffffffff) ? 0 : fentry->gid;
379 udf_timetotimespec(&fentry->atime, &vap->va_atime);
380 udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
381 vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
382 vap->va_rdev = 0; /* XXX */
383 if (vp->v_type & VDIR) {
385 * Directories that are recorded within their ICB will show
386 * as having 0 blocks recorded. Since tradition dictates
387 * that directories consume at least one logical block,
390 if (fentry->logblks_rec != 0)
391 vap->va_size = fentry->logblks_rec * node->udfmp->bsize;
393 vap->va_size = node->udfmp->bsize;
395 vap->va_size = fentry->inf_len;
398 vap->va_blocksize = node->udfmp->bsize;
399 vap->va_bytes = fentry->inf_len;
400 vap->va_type = vp->v_type;
401 vap->va_filerev = 0; /* XXX */
406 * File specific ioctls. DeCSS candidate?
409 udf_ioctl(struct vop_ioctl_args *a)
411 printf("%s called\n", __func__);
416 * I'm not sure that this has much value in a read-only filesystem, but
420 udf_pathconf(struct vop_pathconf_args *a)
425 *a->a_retval = 65535;
428 *a->a_retval = NAME_MAX;
431 *a->a_retval = PATH_MAX;
442 udf_read(struct vop_read_args *a)
444 struct vnode *vp = a->a_vp;
445 struct uio *uio = a->a_uio;
446 struct udf_node *node = VTON(vp);
450 int size, fsize, offset;
452 if (uio->uio_offset < 0)
455 fsize = node->fentry->inf_len;
457 while (uio->uio_offset < fsize && uio->uio_resid > 0) {
458 offset = uio->uio_offset;
459 size = uio->uio_resid;
460 error = udf_readatoffset(node, &size, offset, &bp, &data);
462 error = uiomove(data, size, uio);
473 * Call the OSTA routines to translate the name from a CS0 dstring to a
474 * 16-bit Unicode String. Hooks need to be placed in here to translate from
475 * Unicode to the encoding that the kernel/user expects. Return the length
476 * of the translated string.
479 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
481 unicode_t *transname;
482 int i, unilen = 0, destlen;
484 /* Convert 16-bit Unicode to destname */
485 /* allocate a buffer big enough to hold an 8->16 bit expansion */
486 transname = malloc(MAXNAMLEN * sizeof(unicode_t), M_TEMP, M_WAITOK | M_ZERO);
488 if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) {
489 printf("udf: Unicode translation failed\n");
490 free(transname, M_TEMP);
494 for (i = 0; i < unilen ; i++)
495 if (transname[i] & 0xff00)
496 destname[i] = '.'; /* Fudge the 16bit chars */
498 destname[i] = transname[i] & 0xff;
499 free(transname, M_TEMP);
500 destname[unilen] = 0;
507 * Compare a CS0 dstring with a name passed in from the VFS layer. Return
508 * 0 on a successful match, nonzero therwise. Unicode work may need to be done
512 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
517 /* This is overkill, but not worth creating a new zone */
519 transname = malloc(MAXNAMLEN * sizeof(unicode_t), M_TEMP,
522 cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
524 /* Easy check. If they aren't the same length, they aren't equal */
525 if ((cs0len == 0) || (cs0len != cmplen))
528 error = bcmp(transname, cmpname, cmplen);
530 free(transname, M_TEMP);
535 struct dirent *dirent;
543 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
545 if (uiodir->cookies != NULL) {
546 if (++uiodir->acookies > uiodir->ncookies) {
550 *uiodir->cookies++ = cookie;
553 if (uio->uio_resid < de_size) {
558 return(uiomove((caddr_t)uiodir->dirent, de_size, uio));
561 static struct udf_dirstream *
562 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
564 struct udf_dirstream *ds;
566 ds = malloc(sizeof(*ds), M_UDFDS, M_WAITOK | M_ZERO);
576 static struct fileid_desc *
577 udf_getfid(struct udf_dirstream *ds)
579 struct fileid_desc *fid;
580 int error, frag_size = 0, total_fid_size;
582 /* End of directory? */
583 if (ds->offset + ds->off >= ds->fsize) {
588 /* Grab the first extent of the directory */
593 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
602 * Clean up from a previous fragmented FID.
603 * XXX Is this the right place for this?
605 if (ds->fid_fragment && ds->buf != NULL) {
606 ds->fid_fragment = 0;
607 free(ds->buf, M_UDFFID);
610 fid = (struct fileid_desc*)&ds->data[ds->off];
613 * Check to see if the fid is fragmented. The first test
614 * ensures that we don't wander off the end of the buffer
615 * looking for the l_iu and l_fi fields.
617 if (ds->off + UDF_FID_SIZE > ds->size ||
618 ds->off + fid->l_iu + fid->l_fi + UDF_FID_SIZE > ds->size) {
620 /* Copy what we have of the fid into a buffer */
621 frag_size = ds->size - ds->off;
622 if (frag_size >= ds->udfmp->bsize) {
623 printf("udf: invalid FID fragment\n");
629 * File ID descriptors can only be at most one
630 * logical sector in size.
632 ds->buf = malloc(ds->udfmp->bsize, M_UDFFID, M_WAITOK | M_ZERO);
633 bcopy(fid, ds->buf, frag_size);
635 /* Reduce all of the casting magic */
636 fid = (struct fileid_desc*)ds->buf;
641 /* Fetch the next allocation */
642 ds->offset += ds->size;
644 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
652 * If the fragment was so small that we didn't get
653 * the l_iu and l_fi fields, copy those in.
655 if (frag_size < UDF_FID_SIZE)
656 bcopy(ds->data, &ds->buf[frag_size],
657 UDF_FID_SIZE - frag_size);
660 * Now that we have enough of the fid to work with,
661 * copy in the rest of the fid from the new
664 total_fid_size = UDF_FID_SIZE + fid->l_iu + fid->l_fi;
665 if (total_fid_size > ds->udfmp->bsize) {
666 printf("udf: invalid FID\n");
670 bcopy(ds->data, &ds->buf[frag_size],
671 total_fid_size - frag_size);
673 ds->fid_fragment = 1;
675 total_fid_size = fid->l_iu + fid->l_fi + UDF_FID_SIZE;
678 * Update the offset. Align on a 4 byte boundary because the
681 ds->this_off = ds->off;
682 if (!ds->fid_fragment)
683 ds->off += (total_fid_size + 3) & ~0x03;
685 ds->off = (total_fid_size - frag_size + 3) & ~0x03;
691 udf_closedir(struct udf_dirstream *ds)
697 if (ds->fid_fragment && ds->buf != NULL)
698 free(ds->buf, M_UDFFID);
704 udf_readdir(struct vop_readdir_args *a)
709 struct udf_node *node;
710 struct udf_mnt *udfmp;
711 struct fileid_desc *fid;
712 struct udf_uiodir uiodir;
713 struct udf_dirstream *ds;
714 u_long *cookies = NULL;
724 if (a->a_ncookies != NULL) {
726 * Guess how many entries are needed. If we run out, this
727 * function will be called again and thing will pick up were
730 ncookies = uio->uio_resid / 8;
731 cookies = malloc(sizeof(u_long) * ncookies, M_TEMP, M_WAITOK);
732 uiodir.ncookies = ncookies;
733 uiodir.cookies = cookies;
736 uiodir.cookies = NULL;
739 * Iterate through the file id descriptors. Give the parent dir
740 * entry special attention.
742 ds = udf_opendir(node, uio->uio_offset, node->fentry->inf_len,
745 while ((fid = udf_getfid(ds)) != NULL) {
747 /* XXX Should we return an error on a bad fid? */
748 if (udf_checktag(&fid->tag, TAGID_FID)) {
749 printf("Invalid FID tag\n");
754 /* Is this a deleted file? */
755 if (fid->file_char & UDF_FILE_CHAR_DEL)
758 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
759 /* Do up the '.' and '..' entries. Dummy values are
760 * used for the cookies since the offset here is
761 * usually zero, and NFS doesn't like that value
763 dir.d_fileno = node->hash_id;
767 dir.d_reclen = GENERIC_DIRSIZ(&dir);
768 uiodir.dirent = &dir;
769 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
773 dir.d_fileno = udf_getid(&fid->icb);
778 dir.d_reclen = GENERIC_DIRSIZ(&dir);
779 uiodir.dirent = &dir;
780 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
782 dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
783 &dir.d_name[0], fid->l_fi, udfmp);
784 dir.d_fileno = udf_getid(&fid->icb);
785 dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
787 dir.d_reclen = GENERIC_DIRSIZ(&dir);
788 uiodir.dirent = &dir;
789 error = udf_uiodir(&uiodir, dir.d_reclen, uio,
793 printf("uiomove returned %d\n", error);
799 /* tell the calling layer whether we need to be called again */
800 *a->a_eofflag = uiodir.eofflag;
801 uio->uio_offset = ds->offset + ds->off;
808 if (a->a_ncookies != NULL) {
810 free(cookies, M_TEMP);
812 *a->a_ncookies = uiodir.acookies;
813 *a->a_cookies = cookies;
820 /* Are there any implementations out there that do soft-links? */
822 udf_readlink(struct vop_readlink_args *ap)
824 printf("%s called\n", __func__);
829 udf_strategy(struct vop_strategy_args *a)
833 struct udf_node *node;
840 KASSERT(a->a_vp == a->a_bp->b_vp, ("%s(%p != %p)",
841 __func__, a->a_vp, a->a_bp->b_vp));
842 /* cd9660 has this test reversed, but it seems more logical this way */
843 if (bp->b_blkno != bp->b_lblkno) {
845 * Files that are embedded in the fentry don't translate well
846 * to a block number. Reject.
848 if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize,
849 &bp->b_lblkno, &maxsize)) {
854 if ((long)bp->b_blkno == -1) {
859 bp->b_dev = vp->v_rdev;
860 bp->b_offset = dbtob(bp->b_blkno);
861 VOP_STRATEGY(vp, bp);
866 udf_bmap(struct vop_bmap_args *a)
868 struct udf_node *node;
873 node = VTON(a->a_vp);
875 if (a->a_vpp != NULL)
876 *a->a_vpp = node->i_devvp;
877 if (a->a_bnp == NULL)
882 error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector,
887 /* Translate logical to physical sector number */
888 *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
890 /* Punt on read-ahead for now */
898 * The all powerful VOP_LOOKUP().
901 udf_lookup(struct vop_lookup_args *a)
904 struct vnode *tdp = NULL;
905 struct vnode **vpp = a->a_vpp;
906 struct udf_node *node;
907 struct udf_mnt *udfmp;
908 struct fileid_desc *fid = NULL;
909 struct udf_dirstream *ds;
911 globaldata_t gd = mycpu;
917 int offset, error = 0;
918 int numdirpasses, fsize;
923 nameiop = a->a_cnp->cn_nameiop;
924 flags = a->a_cnp->cn_flags;
925 nameptr = a->a_cnp->cn_nameptr;
926 namelen = a->a_cnp->cn_namelen;
927 fsize = node->fentry->inf_len;
928 td = a->a_cnp->cn_td;
931 * If this is a LOOKUP and we've already partially searched through
932 * the directory, pick up where we left off and flag that the
933 * directory may need to be searched twice. For a full description,
934 * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup()
936 if (nameiop != NAMEI_LOOKUP || node->diroff == 0 ||
937 node->diroff > fsize) {
941 offset = node->diroff;
943 gd->gd_nchstats->ncs_2passes++;
947 ds = udf_opendir(node, offset, fsize, udfmp);
949 while ((fid = udf_getfid(ds)) != NULL) {
950 /* XXX Should we return an error on a bad fid? */
951 if (udf_checktag(&fid->tag, TAGID_FID)) {
952 printf("udf_lookup: Invalid tag\n");
957 /* Is this a deleted file? */
958 if (fid->file_char & UDF_FILE_CHAR_DEL)
961 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
962 if (flags & CNP_ISDOTDOT) {
963 id = udf_getid(&fid->icb);
967 if (!(udf_cmpname(&fid->data[fid->l_iu],
968 nameptr, fid->l_fi, namelen, udfmp))) {
969 id = udf_getid(&fid->icb);
978 /* XXX Bail out here? */
984 /* Did we have a match? */
986 error = udf_vget(udfmp->im_mountp, id, &tdp);
989 * Remember where this entry was if it's the final
992 if (nameiop == NAMEI_LOOKUP)
993 node->diroff = ds->offset + ds->off;
994 if (numdirpasses == 2)
995 gd->gd_nchstats->ncs_pass2++;
996 if ((flags & CNP_LOCKPARENT) == 0) {
997 a->a_cnp->cn_flags |= CNP_PDIRUNLOCK;
998 VOP_UNLOCK(dvp, 0, td);
1004 /* Name wasn't found on this pass. Do another pass? */
1005 if (numdirpasses == 2) {
1011 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) {
1023 udf_reclaim(struct vop_reclaim_args *a)
1026 struct udf_node *unode;
1031 if (unode != NULL) {
1033 if (unode->i_devvp) {
1034 vrele(unode->i_devvp);
1038 if (unode->fentry != NULL)
1039 free(unode->fentry, M_UDFFENTRY);
1040 free(unode, M_UDFNODE);
1048 * Read the block and then set the data pointer to correspond with the
1049 * offset passed in. Only read in at most 'size' bytes, and then set 'size'
1050 * to the number of bytes pointed to. If 'size' is zero, try to read in a
1053 * Note that *bp may be assigned error or not.
1055 * XXX 'size' is limited to the logical block size for now due to problems
1059 udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp,
1062 struct udf_mnt *udfmp;
1063 struct file_entry *fentry = NULL;
1069 udfmp = node->udfmp;
1072 error = udf_bmap_internal(node, offset, §or, &max_size);
1073 if (error == UDF_INVALID_BMAP) {
1075 * This error means that the file *data* is stored in the
1076 * allocation descriptor field of the file entry.
1078 fentry = node->fentry;
1079 *data = &fentry->data[fentry->l_ea];
1080 *size = fentry->l_ad;
1082 } else if (error != 0) {
1086 /* Adjust the size so that it is within range */
1087 if (*size == 0 || *size > max_size)
1089 *size = min(*size, MAXBSIZE);
1091 if ((error = udf_readlblks(udfmp, sector, *size, bp))) {
1092 printf("warning: udf_readlblks returned error %d\n", error);
1093 /* note: *bp may be non-NULL */
1098 *data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize];
1103 * Translate a file offset into a logical block and then into a physical
1107 udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size)
1109 struct udf_mnt *udfmp;
1110 struct file_entry *fentry;
1112 struct icb_tag *tag;
1113 uint32_t icblen = 0;
1115 int ad_offset, ad_num = 0;
1118 udfmp = node->udfmp;
1119 fentry = node->fentry;
1120 tag = &fentry->icbtag;
1122 switch (tag->strat_type) {
1127 printf("Cannot deal with strategy4096 yet!\n");
1131 printf("Unknown strategy type %d\n", tag->strat_type);
1135 switch (tag->flags & 0x7) {
1138 * The allocation descriptor field is filled with short_ad's.
1139 * If the offset is beyond the current extent, look for the
1144 ad_offset = sizeof(struct short_ad) * ad_num;
1145 if (ad_offset > fentry->l_ad) {
1146 printf("File offset out of bounds\n");
1149 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1150 icblen = GETICBLEN(short_ad, icb);
1152 } while(offset >= icblen);
1154 lsector = (offset >> udfmp->bshift) +
1155 ((struct short_ad *)(icb))->pos;
1157 *max_size = GETICBLEN(short_ad, icb);
1162 * The allocation descriptor field is filled with long_ad's
1163 * If the offset is beyond the current extent, look for the
1168 ad_offset = sizeof(struct long_ad) * ad_num;
1169 if (ad_offset > fentry->l_ad) {
1170 printf("File offset out of bounds\n");
1173 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1174 icblen = GETICBLEN(long_ad, icb);
1176 } while(offset >= icblen);
1178 lsector = (offset >> udfmp->bshift) +
1179 ((struct long_ad *)(icb))->loc.lb_num;
1181 *max_size = GETICBLEN(long_ad, icb);
1186 * This type means that the file *data* is stored in the
1187 * allocation descriptor field of the file entry.
1190 *sector = node->hash_id + udfmp->part_start;
1192 return(UDF_INVALID_BMAP);
1194 /* DirectCD does not use extended_ad's */
1196 printf("Unsupported allocation descriptor %d\n",
1201 *sector = lsector + udfmp->part_start;
1204 * Check the sparing table. Each entry represents the beginning of
1207 if (udfmp->s_table != NULL) {
1208 for (i = 0; i< udfmp->s_table_entries; i++) {
1209 p_offset = lsector - udfmp->s_table->entries[i].org;
1210 if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1211 *sector = udfmp->s_table->entries[i].map +