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.13 2005/02/17 14:00:10 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 <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_lookup_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 struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
68 { &vop_default_desc, vop_defaultop },
69 { &vop_access_desc, (vnodeopv_entry_t) udf_access },
70 { &vop_bmap_desc, (vnodeopv_entry_t) udf_bmap },
71 { &vop_lookup_desc, (vnodeopv_entry_t) udf_lookup },
72 { &vop_getattr_desc, (vnodeopv_entry_t) udf_getattr },
73 { &vop_ioctl_desc, (vnodeopv_entry_t) udf_ioctl },
74 { &vop_pathconf_desc, (vnodeopv_entry_t) udf_pathconf },
75 { &vop_read_desc, (vnodeopv_entry_t) udf_read },
76 { &vop_readdir_desc, (vnodeopv_entry_t) udf_readdir },
77 { &vop_readlink_desc, (vnodeopv_entry_t) udf_readlink },
78 { &vop_reclaim_desc, (vnodeopv_entry_t) udf_reclaim },
79 { &vop_strategy_desc, (vnodeopv_entry_t) udf_strategy },
83 MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure");
84 MALLOC_DEFINE(M_UDFDS, "UDF DS", "UDF Dirstream structure");
86 #define UDF_INVALID_BMAP -1
88 /* Look up a udf_node based on the ino_t passed in and return it's vnode */
90 udf_hashlookup(struct udf_mnt *udfmp, ino_t id, struct vnode **vpp)
92 struct udf_node *node;
93 struct udf_hash_lh *lh;
99 lwkt_gettoken(&hashlock, &udfmp->hash_token);
101 lh = &udfmp->hashtbl[id % udfmp->hashsz];
103 lwkt_reltoken(&hashlock);
106 LIST_FOREACH(node, lh, le) {
107 if (node->hash_id != id)
110 if (vget(vp, LK_EXCLUSIVE, curthread))
113 * We must check to see if the inode has been ripped
114 * out from under us after blocking.
116 lh = &udfmp->hashtbl[id % udfmp->hashsz];
117 LIST_FOREACH(node, lh, le) {
118 if (node->hash_id == id)
121 if (node == NULL || vp != node->i_vnode) {
125 lwkt_reltoken(&hashlock);
130 lwkt_reltoken(&hashlock);
135 udf_hashins(struct udf_node *node)
137 struct udf_mnt *udfmp;
138 struct udf_hash_lh *lh;
139 lwkt_tokref hashlock;
143 lwkt_gettoken(&hashlock, &udfmp->hash_token);
144 lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
147 LIST_INSERT_HEAD(lh, node, le);
148 lwkt_reltoken(&hashlock);
154 udf_hashrem(struct udf_node *node)
156 struct udf_mnt *udfmp;
157 struct udf_hash_lh *lh;
158 lwkt_tokref hashlock;
162 lwkt_gettoken(&hashlock, &udfmp->hash_token);
163 lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
165 panic("hash entry is NULL, node->hash_id= %d\n", node->hash_id);
166 LIST_REMOVE(node, le);
167 lwkt_reltoken(&hashlock);
173 udf_allocv(struct mount *mp, struct vnode **vpp)
178 error = getnewvnode(VT_UDF, mp, &vp, 0, 0);
180 printf("udf_allocv: failed to allocate new vnode\n");
188 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
190 udf_permtomode(struct udf_node *node)
196 perm = node->fentry->perm;
197 flags = node->fentry->icbtag.flags;
199 mode = perm & UDF_FENTRY_PERM_USER_MASK;
200 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
201 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
202 mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
203 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
204 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
210 udf_access(struct vop_access_args *a)
213 struct udf_node *node;
214 mode_t a_mode, mode, mask;
215 struct ucred *cred = a->a_cred;
223 if (a_mode & VWRITE) {
224 switch (vp->v_type) {
235 mode = udf_permtomode(node);
237 if (cred->cr_uid == 0)
242 /* Otherwise, check the owner. */
243 if (cred->cr_uid == node->fentry->uid) {
250 return((mode & mask) == mask ? 0 : EACCES);
253 /* Otherwise, check the groups. */
254 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
255 if (node->fentry->gid == *gp) {
262 return((mode & mask) == mask ? 0 : EACCES);
265 /* Otherwise, check everyone else. */
272 return((mode & mask) == mask ? 0 : EACCES);
275 static int mon_lens[2][12] = {
276 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
277 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
281 udf_isaleapyear(int year)
285 i = (year % 4) ? 0 : 1;
286 i &= (year % 100) ? 1 : 0;
287 i |= (year % 400) ? 0 : 1;
293 * XXX This is just a rough hack. Daylight savings isn't calculated and tv_nsec
295 * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
298 udf_timetotimespec(struct timestamp *time, struct timespec *t)
300 int i, lpyear, daysinyear;
302 uint16_t u_tz_offset;
308 /* DirectCD seems to like using bogus year values */
309 if (time->year < 1970) {
314 /* Calculate the time and day */
315 t->tv_sec = time->second;
316 t->tv_sec += time->minute * 60;
317 t->tv_sec += time->hour * 3600;
318 t->tv_sec += time->day * 3600 * 24;
320 /* Calclulate the month */
321 lpyear = udf_isaleapyear(time->year);
322 for (i = 1; i < time->month; i++)
323 t->tv_sec += mon_lens[lpyear][i] * 3600 * 24;
325 /* Speed up the calculation */
326 if (time->year > 1979)
327 t->tv_sec += 315532800;
328 if (time->year > 1989)
329 t->tv_sec += 315619200;
330 if (time->year > 1999)
331 t->tv_sec += 315532800;
332 for (i = 2000; i < time->year; i++) {
333 daysinyear = udf_isaleapyear(i) + 365 ;
334 t->tv_sec += daysinyear * 3600 * 24;
338 * Calculate the time zone. The timezone is 12 bit signed 2's
339 * compliment, so we gotta do some extra magic to handle it right.
341 tz.u_tz_offset = time->type_tz;
342 tz.u_tz_offset &= 0x0fff;
343 if (tz.u_tz_offset & 0x0800)
344 tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */
345 if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047))
346 t->tv_sec -= tz.s_tz_offset * 60;
352 udf_getattr(struct vop_getattr_args *a)
355 struct udf_node *node;
357 struct file_entry *fentry;
365 fentry = node->fentry;
367 vap->va_fsid = dev2udev(node->i_dev);
368 vap->va_fileid = node->hash_id;
369 vap->va_mode = udf_permtomode(node);
370 vap->va_nlink = fentry->link_cnt;
372 * XXX The spec says that -1 is valid for uid/gid and indicates an
373 * invalid uid/gid. How should this be represented?
375 vap->va_uid = (fentry->uid == 0xffffffff) ? 0 : fentry->uid;
376 vap->va_gid = (fentry->gid == 0xffffffff) ? 0 : fentry->gid;
377 udf_timetotimespec(&fentry->atime, &vap->va_atime);
378 udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
379 vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
380 vap->va_rdev = 0; /* XXX */
381 if (vp->v_type & VDIR) {
383 * Directories that are recorded within their ICB will show
384 * as having 0 blocks recorded. Since tradition dictates
385 * that directories consume at least one logical block,
388 if (fentry->logblks_rec != 0)
389 vap->va_size = fentry->logblks_rec * node->udfmp->bsize;
391 vap->va_size = node->udfmp->bsize;
393 vap->va_size = fentry->inf_len;
396 vap->va_blocksize = node->udfmp->bsize;
397 vap->va_bytes = fentry->inf_len;
398 vap->va_type = vp->v_type;
399 vap->va_filerev = 0; /* XXX */
404 * File specific ioctls. DeCSS candidate?
407 udf_ioctl(struct vop_ioctl_args *a)
409 printf("%s called\n", __func__);
414 * I'm not sure that this has much value in a read-only filesystem, but
418 udf_pathconf(struct vop_pathconf_args *a)
423 *a->a_retval = 65535;
426 *a->a_retval = NAME_MAX;
429 *a->a_retval = PATH_MAX;
440 udf_read(struct vop_read_args *a)
442 struct vnode *vp = a->a_vp;
443 struct uio *uio = a->a_uio;
444 struct udf_node *node = VTON(vp);
448 int size, fsize, offset;
450 if (uio->uio_offset < 0)
453 fsize = node->fentry->inf_len;
455 while (uio->uio_offset < fsize && uio->uio_resid > 0) {
456 offset = uio->uio_offset;
457 size = uio->uio_resid;
458 error = udf_readatoffset(node, &size, offset, &bp, &data);
460 error = uiomove(data, size, uio);
471 * Call the OSTA routines to translate the name from a CS0 dstring to a
472 * 16-bit Unicode String. Hooks need to be placed in here to translate from
473 * Unicode to the encoding that the kernel/user expects. Return the length
474 * of the translated string.
477 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
479 unicode_t *transname;
480 int i, unilen = 0, destlen;
482 /* Convert 16-bit Unicode to destname */
483 /* allocate a buffer big enough to hold an 8->16 bit expansion */
484 transname = malloc(MAXNAMLEN * sizeof(unicode_t), M_TEMP, M_WAITOK | M_ZERO);
486 if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) {
487 printf("udf: Unicode translation failed\n");
488 free(transname, M_TEMP);
492 for (i = 0; i < unilen ; i++)
493 if (transname[i] & 0xff00)
494 destname[i] = '.'; /* Fudge the 16bit chars */
496 destname[i] = transname[i] & 0xff;
497 free(transname, M_TEMP);
498 destname[unilen] = 0;
505 * Compare a CS0 dstring with a name passed in from the VFS layer. Return
506 * 0 on a successful match, nonzero therwise. Unicode work may need to be done
510 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
515 /* This is overkill, but not worth creating a new zone */
517 transname = malloc(MAXNAMLEN * sizeof(unicode_t), M_TEMP,
520 cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
522 /* Easy check. If they aren't the same length, they aren't equal */
523 if ((cs0len == 0) || (cs0len != cmplen))
526 error = bcmp(transname, cmpname, cmplen);
528 free(transname, M_TEMP);
533 struct dirent *dirent;
541 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
543 if (uiodir->cookies != NULL) {
544 if (++uiodir->acookies > uiodir->ncookies) {
548 *uiodir->cookies++ = cookie;
551 if (uio->uio_resid < de_size) {
556 return(uiomove((caddr_t)uiodir->dirent, de_size, uio));
559 static struct udf_dirstream *
560 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
562 struct udf_dirstream *ds;
564 ds = malloc(sizeof(*ds), M_UDFDS, M_WAITOK | M_ZERO);
574 static struct fileid_desc *
575 udf_getfid(struct udf_dirstream *ds)
577 struct fileid_desc *fid;
578 int error, frag_size = 0, total_fid_size;
580 /* End of directory? */
581 if (ds->offset + ds->off >= ds->fsize) {
586 /* Grab the first extent of the directory */
591 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
600 * Clean up from a previous fragmented FID.
601 * XXX Is this the right place for this?
603 if (ds->fid_fragment && ds->buf != NULL) {
604 ds->fid_fragment = 0;
605 free(ds->buf, M_UDFFID);
608 fid = (struct fileid_desc*)&ds->data[ds->off];
611 * Check to see if the fid is fragmented. The first test
612 * ensures that we don't wander off the end of the buffer
613 * looking for the l_iu and l_fi fields.
615 if (ds->off + UDF_FID_SIZE > ds->size ||
616 ds->off + fid->l_iu + fid->l_fi + UDF_FID_SIZE > ds->size) {
618 /* Copy what we have of the fid into a buffer */
619 frag_size = ds->size - ds->off;
620 if (frag_size >= ds->udfmp->bsize) {
621 printf("udf: invalid FID fragment\n");
627 * File ID descriptors can only be at most one
628 * logical sector in size.
630 ds->buf = malloc(ds->udfmp->bsize, M_UDFFID, M_WAITOK | M_ZERO);
631 bcopy(fid, ds->buf, frag_size);
633 /* Reduce all of the casting magic */
634 fid = (struct fileid_desc*)ds->buf;
639 /* Fetch the next allocation */
640 ds->offset += ds->size;
642 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
650 * If the fragment was so small that we didn't get
651 * the l_iu and l_fi fields, copy those in.
653 if (frag_size < UDF_FID_SIZE)
654 bcopy(ds->data, &ds->buf[frag_size],
655 UDF_FID_SIZE - frag_size);
658 * Now that we have enough of the fid to work with,
659 * copy in the rest of the fid from the new
662 total_fid_size = UDF_FID_SIZE + fid->l_iu + fid->l_fi;
663 if (total_fid_size > ds->udfmp->bsize) {
664 printf("udf: invalid FID\n");
668 bcopy(ds->data, &ds->buf[frag_size],
669 total_fid_size - frag_size);
671 ds->fid_fragment = 1;
673 total_fid_size = fid->l_iu + fid->l_fi + UDF_FID_SIZE;
676 * Update the offset. Align on a 4 byte boundary because the
679 ds->this_off = ds->off;
680 if (!ds->fid_fragment)
681 ds->off += (total_fid_size + 3) & ~0x03;
683 ds->off = (total_fid_size - frag_size + 3) & ~0x03;
689 udf_closedir(struct udf_dirstream *ds)
695 if (ds->fid_fragment && ds->buf != NULL)
696 free(ds->buf, M_UDFFID);
702 udf_readdir(struct vop_readdir_args *a)
707 struct udf_node *node;
708 struct udf_mnt *udfmp;
709 struct fileid_desc *fid;
710 struct udf_uiodir uiodir;
711 struct udf_dirstream *ds;
712 u_long *cookies = NULL;
722 if (a->a_ncookies != NULL) {
724 * Guess how many entries are needed. If we run out, this
725 * function will be called again and thing will pick up were
728 ncookies = uio->uio_resid / 8;
729 cookies = malloc(sizeof(u_long) * ncookies, M_TEMP, M_WAITOK);
730 uiodir.ncookies = ncookies;
731 uiodir.cookies = cookies;
734 uiodir.cookies = NULL;
737 * Iterate through the file id descriptors. Give the parent dir
738 * entry special attention.
740 ds = udf_opendir(node, uio->uio_offset, node->fentry->inf_len,
743 while ((fid = udf_getfid(ds)) != NULL) {
745 /* XXX Should we return an error on a bad fid? */
746 if (udf_checktag(&fid->tag, TAGID_FID)) {
747 printf("Invalid FID tag\n");
752 /* Is this a deleted file? */
753 if (fid->file_char & UDF_FILE_CHAR_DEL)
756 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
757 /* Do up the '.' and '..' entries. Dummy values are
758 * used for the cookies since the offset here is
759 * usually zero, and NFS doesn't like that value
761 dir.d_fileno = node->hash_id;
765 dir.d_reclen = GENERIC_DIRSIZ(&dir);
766 uiodir.dirent = &dir;
767 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
771 dir.d_fileno = udf_getid(&fid->icb);
776 dir.d_reclen = GENERIC_DIRSIZ(&dir);
777 uiodir.dirent = &dir;
778 error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
780 dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
781 &dir.d_name[0], fid->l_fi, udfmp);
782 dir.d_fileno = udf_getid(&fid->icb);
783 dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
785 dir.d_reclen = GENERIC_DIRSIZ(&dir);
786 uiodir.dirent = &dir;
787 error = udf_uiodir(&uiodir, dir.d_reclen, uio,
791 printf("uiomove returned %d\n", error);
797 /* tell the calling layer whether we need to be called again */
798 *a->a_eofflag = uiodir.eofflag;
799 uio->uio_offset = ds->offset + ds->off;
806 if (a->a_ncookies != NULL) {
808 free(cookies, M_TEMP);
810 *a->a_ncookies = uiodir.acookies;
811 *a->a_cookies = cookies;
818 /* Are there any implementations out there that do soft-links? */
820 udf_readlink(struct vop_readlink_args *ap)
822 printf("%s called\n", __func__);
827 udf_strategy(struct vop_strategy_args *a)
831 struct udf_node *node;
838 KASSERT(a->a_vp == a->a_bp->b_vp, ("%s(%p != %p)",
839 __func__, a->a_vp, a->a_bp->b_vp));
840 /* cd9660 has this test reversed, but it seems more logical this way */
841 if (bp->b_blkno != bp->b_lblkno) {
843 * Files that are embedded in the fentry don't translate well
844 * to a block number. Reject.
846 if (udf_bmap_internal(node, bp->b_lblkno * node->udfmp->bsize,
847 &bp->b_lblkno, &maxsize)) {
852 if ((long)bp->b_blkno == -1) {
857 bp->b_dev = vp->v_rdev;
858 bp->b_offset = dbtob(bp->b_blkno);
859 VOP_STRATEGY(vp, bp);
864 udf_bmap(struct vop_bmap_args *a)
866 struct udf_node *node;
871 node = VTON(a->a_vp);
873 if (a->a_vpp != NULL)
874 *a->a_vpp = node->i_devvp;
875 if (a->a_bnp == NULL)
880 error = udf_bmap_internal(node, a->a_bn * node->udfmp->bsize, &lsector,
885 /* Translate logical to physical sector number */
886 *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
888 /* Punt on read-ahead for now */
896 * The all powerful VOP_LOOKUP().
899 udf_lookup(struct vop_lookup_args *a)
902 struct vnode *tdp = NULL;
903 struct vnode **vpp = a->a_vpp;
904 struct udf_node *node;
905 struct udf_mnt *udfmp;
906 struct fileid_desc *fid = NULL;
907 struct udf_dirstream *ds;
909 globaldata_t gd = mycpu;
915 int offset, error = 0;
916 int numdirpasses, fsize;
921 nameiop = a->a_cnp->cn_nameiop;
922 flags = a->a_cnp->cn_flags;
923 nameptr = a->a_cnp->cn_nameptr;
924 namelen = a->a_cnp->cn_namelen;
925 fsize = node->fentry->inf_len;
926 td = a->a_cnp->cn_td;
929 * If this is a LOOKUP and we've already partially searched through
930 * the directory, pick up where we left off and flag that the
931 * directory may need to be searched twice. For a full description,
932 * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup()
934 if (nameiop != NAMEI_LOOKUP || node->diroff == 0 ||
935 node->diroff > fsize) {
939 offset = node->diroff;
941 gd->gd_nchstats->ncs_2passes++;
945 ds = udf_opendir(node, offset, fsize, udfmp);
947 while ((fid = udf_getfid(ds)) != NULL) {
948 /* XXX Should we return an error on a bad fid? */
949 if (udf_checktag(&fid->tag, TAGID_FID)) {
950 printf("udf_lookup: Invalid tag\n");
955 /* Is this a deleted file? */
956 if (fid->file_char & UDF_FILE_CHAR_DEL)
959 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
960 if (flags & CNP_ISDOTDOT) {
961 id = udf_getid(&fid->icb);
965 if (!(udf_cmpname(&fid->data[fid->l_iu],
966 nameptr, fid->l_fi, namelen, udfmp))) {
967 id = udf_getid(&fid->icb);
976 /* XXX Bail out here? */
982 /* Did we have a match? */
984 error = udf_vget(udfmp->im_mountp, id, &tdp);
987 * Remember where this entry was if it's the final
990 if (nameiop == NAMEI_LOOKUP)
991 node->diroff = ds->offset + ds->off;
992 if (numdirpasses == 2)
993 gd->gd_nchstats->ncs_pass2++;
994 if ((flags & CNP_LOCKPARENT) == 0) {
995 a->a_cnp->cn_flags |= CNP_PDIRUNLOCK;
996 VOP_UNLOCK(dvp, 0, td);
1002 /* Name wasn't found on this pass. Do another pass? */
1003 if (numdirpasses == 2) {
1009 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) {
1021 udf_reclaim(struct vop_reclaim_args *a)
1024 struct udf_node *unode;
1029 if (unode != NULL) {
1031 if (unode->i_devvp) {
1032 vrele(unode->i_devvp);
1036 if (unode->fentry != NULL)
1037 free(unode->fentry, M_UDFFENTRY);
1038 free(unode, M_UDFNODE);
1046 * Read the block and then set the data pointer to correspond with the
1047 * offset passed in. Only read in at most 'size' bytes, and then set 'size'
1048 * to the number of bytes pointed to. If 'size' is zero, try to read in a
1051 * Note that *bp may be assigned error or not.
1053 * XXX 'size' is limited to the logical block size for now due to problems
1057 udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp,
1060 struct udf_mnt *udfmp;
1061 struct file_entry *fentry = NULL;
1067 udfmp = node->udfmp;
1070 error = udf_bmap_internal(node, offset, §or, &max_size);
1071 if (error == UDF_INVALID_BMAP) {
1073 * This error means that the file *data* is stored in the
1074 * allocation descriptor field of the file entry.
1076 fentry = node->fentry;
1077 *data = &fentry->data[fentry->l_ea];
1078 *size = fentry->l_ad;
1080 } else if (error != 0) {
1084 /* Adjust the size so that it is within range */
1085 if (*size == 0 || *size > max_size)
1087 *size = min(*size, MAXBSIZE);
1089 if ((error = udf_readlblks(udfmp, sector, *size, bp))) {
1090 printf("warning: udf_readlblks returned error %d\n", error);
1091 /* note: *bp may be non-NULL */
1096 *data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize];
1101 * Translate a file offset into a logical block and then into a physical
1105 udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size)
1107 struct udf_mnt *udfmp;
1108 struct file_entry *fentry;
1110 struct icb_tag *tag;
1111 uint32_t icblen = 0;
1113 int ad_offset, ad_num = 0;
1116 udfmp = node->udfmp;
1117 fentry = node->fentry;
1118 tag = &fentry->icbtag;
1120 switch (tag->strat_type) {
1125 printf("Cannot deal with strategy4096 yet!\n");
1129 printf("Unknown strategy type %d\n", tag->strat_type);
1133 switch (tag->flags & 0x7) {
1136 * The allocation descriptor field is filled with short_ad's.
1137 * If the offset is beyond the current extent, look for the
1142 ad_offset = sizeof(struct short_ad) * ad_num;
1143 if (ad_offset > fentry->l_ad) {
1144 printf("File offset out of bounds\n");
1147 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1148 icblen = GETICBLEN(short_ad, icb);
1150 } while(offset >= icblen);
1152 lsector = (offset >> udfmp->bshift) +
1153 ((struct short_ad *)(icb))->pos;
1155 *max_size = GETICBLEN(short_ad, icb);
1160 * The allocation descriptor field is filled with long_ad's
1161 * If the offset is beyond the current extent, look for the
1166 ad_offset = sizeof(struct long_ad) * ad_num;
1167 if (ad_offset > fentry->l_ad) {
1168 printf("File offset out of bounds\n");
1171 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1172 icblen = GETICBLEN(long_ad, icb);
1174 } while(offset >= icblen);
1176 lsector = (offset >> udfmp->bshift) +
1177 ((struct long_ad *)(icb))->loc.lb_num;
1179 *max_size = GETICBLEN(long_ad, icb);
1184 * This type means that the file *data* is stored in the
1185 * allocation descriptor field of the file entry.
1188 *sector = node->hash_id + udfmp->part_start;
1190 return(UDF_INVALID_BMAP);
1192 /* DirectCD does not use extended_ad's */
1194 printf("Unsupported allocation descriptor %d\n",
1199 *sector = lsector + udfmp->part_start;
1202 * Check the sparing table. Each entry represents the beginning of
1205 if (udfmp->s_table != NULL) {
1206 for (i = 0; i< udfmp->s_table_entries; i++) {
1207 p_offset = lsector - udfmp->s_table->entries[i].org;
1208 if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1209 *sector = udfmp->s_table->entries[i].map +