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.25 2006/08/19 17:27:25 dillon 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_old_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 vop_ops udf_vnode_vops = {
70 .vop_default = vop_defaultop,
71 .vop_access = udf_access,
73 .vop_old_lookup = udf_lookup,
74 .vop_getattr = udf_getattr,
75 .vop_ioctl = udf_ioctl,
76 .vop_pathconf = udf_pathconf,
78 .vop_readdir = udf_readdir,
79 .vop_readlink = udf_readlink,
80 .vop_reclaim = udf_reclaim,
81 .vop_strategy = udf_strategy
84 MALLOC_DEFINE(M_UDFFID, "UDF FID", "UDF FileId structure");
85 MALLOC_DEFINE(M_UDFDS, "UDF DS", "UDF Dirstream structure");
87 #define UDF_INVALID_BMAP -1
89 /* Look up a udf_node based on the ino_t passed in and return it's vnode */
91 udf_hashlookup(struct udf_mnt *udfmp, ino_t id, struct vnode **vpp)
93 struct udf_node *node;
94 struct udf_hash_lh *lh;
100 lwkt_gettoken(&hashlock, &udfmp->hash_token);
102 lh = &udfmp->hashtbl[id % udfmp->hashsz];
104 lwkt_reltoken(&hashlock);
107 LIST_FOREACH(node, lh, le) {
108 if (node->hash_id != id)
111 if (vget(vp, LK_EXCLUSIVE))
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 if (node == NULL || vp != node->i_vnode) {
126 lwkt_reltoken(&hashlock);
131 lwkt_reltoken(&hashlock);
136 udf_hashins(struct udf_node *node)
138 struct udf_mnt *udfmp;
139 struct udf_hash_lh *lh;
140 lwkt_tokref hashlock;
144 lwkt_gettoken(&hashlock, &udfmp->hash_token);
145 lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
148 LIST_INSERT_HEAD(lh, node, le);
149 lwkt_reltoken(&hashlock);
155 udf_hashrem(struct udf_node *node)
157 struct udf_mnt *udfmp;
158 struct udf_hash_lh *lh;
159 lwkt_tokref hashlock;
163 lwkt_gettoken(&hashlock, &udfmp->hash_token);
164 lh = &udfmp->hashtbl[node->hash_id % udfmp->hashsz];
166 panic("hash entry is NULL, node->hash_id= %"PRId64"\n", node->hash_id);
167 LIST_REMOVE(node, le);
168 lwkt_reltoken(&hashlock);
174 udf_allocv(struct mount *mp, struct vnode **vpp)
179 error = getnewvnode(VT_UDF, mp, &vp, 0, 0);
181 printf("udf_allocv: failed to allocate new vnode\n");
189 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
191 udf_permtomode(struct udf_node *node)
197 perm = node->fentry->perm;
198 flags = node->fentry->icbtag.flags;
200 mode = perm & UDF_FENTRY_PERM_USER_MASK;
201 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
202 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
203 mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
204 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
205 mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
211 udf_access(struct vop_access_args *a)
214 struct udf_node *node;
215 mode_t a_mode, mode, mask;
216 struct ucred *cred = a->a_cred;
224 if (a_mode & VWRITE) {
225 switch (vp->v_type) {
236 mode = udf_permtomode(node);
238 if (cred->cr_uid == 0)
243 /* Otherwise, check the owner. */
244 if (cred->cr_uid == node->fentry->uid) {
251 return((mode & mask) == mask ? 0 : EACCES);
254 /* Otherwise, check the groups. */
255 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
256 if (node->fentry->gid == *gp) {
263 return((mode & mask) == mask ? 0 : EACCES);
266 /* Otherwise, check everyone else. */
273 return((mode & mask) == mask ? 0 : EACCES);
276 static int mon_lens[2][12] = {
277 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
278 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
282 udf_isaleapyear(int year)
286 i = (year % 4) ? 0 : 1;
287 i &= (year % 100) ? 1 : 0;
288 i |= (year % 400) ? 0 : 1;
294 * XXX This is just a rough hack. Daylight savings isn't calculated and tv_nsec
296 * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
299 udf_timetotimespec(struct timestamp *time, struct timespec *t)
301 int i, lpyear, daysinyear;
303 uint16_t u_tz_offset;
309 /* DirectCD seems to like using bogus year values */
310 if (time->year < 1970) {
315 /* Calculate the time and day */
316 t->tv_sec = time->second;
317 t->tv_sec += time->minute * 60;
318 t->tv_sec += time->hour * 3600;
319 t->tv_sec += time->day * 3600 * 24;
321 /* Calclulate the month */
322 lpyear = udf_isaleapyear(time->year);
323 for (i = 1; i < time->month; i++)
324 t->tv_sec += mon_lens[lpyear][i] * 3600 * 24;
326 /* Speed up the calculation */
327 if (time->year > 1979)
328 t->tv_sec += 315532800;
329 if (time->year > 1989)
330 t->tv_sec += 315619200;
331 if (time->year > 1999)
332 t->tv_sec += 315532800;
333 for (i = 2000; i < time->year; i++) {
334 daysinyear = udf_isaleapyear(i) + 365 ;
335 t->tv_sec += daysinyear * 3600 * 24;
339 * Calculate the time zone. The timezone is 12 bit signed 2's
340 * compliment, so we gotta do some extra magic to handle it right.
342 tz.u_tz_offset = time->type_tz;
343 tz.u_tz_offset &= 0x0fff;
344 if (tz.u_tz_offset & 0x0800)
345 tz.u_tz_offset |= 0xf000; /* extend the sign to 16 bits */
346 if ((time->type_tz & 0x1000) && (tz.s_tz_offset != -2047))
347 t->tv_sec -= tz.s_tz_offset * 60;
353 udf_getattr(struct vop_getattr_args *a)
356 struct udf_node *node;
358 struct file_entry *fentry;
366 fentry = node->fentry;
368 vap->va_fsid = dev2udev(node->i_dev);
369 vap->va_fileid = node->hash_id;
370 vap->va_mode = udf_permtomode(node);
371 vap->va_nlink = fentry->link_cnt;
373 * XXX The spec says that -1 is valid for uid/gid and indicates an
374 * invalid uid/gid. How should this be represented?
376 vap->va_uid = (fentry->uid == 0xffffffff) ? 0 : fentry->uid;
377 vap->va_gid = (fentry->gid == 0xffffffff) ? 0 : fentry->gid;
378 udf_timetotimespec(&fentry->atime, &vap->va_atime);
379 udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
380 vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
381 vap->va_rdev = 0; /* XXX */
382 if (vp->v_type & VDIR) {
384 * Directories that are recorded within their ICB will show
385 * as having 0 blocks recorded. Since tradition dictates
386 * that directories consume at least one logical block,
389 if (fentry->logblks_rec != 0)
390 vap->va_size = fentry->logblks_rec * node->udfmp->bsize;
392 vap->va_size = node->udfmp->bsize;
394 vap->va_size = fentry->inf_len;
397 vap->va_blocksize = node->udfmp->bsize;
398 vap->va_bytes = fentry->inf_len;
399 vap->va_type = vp->v_type;
400 vap->va_filerev = 0; /* XXX */
405 * File specific ioctls. DeCSS candidate?
408 udf_ioctl(struct vop_ioctl_args *a)
410 printf("%s called\n", __func__);
415 * I'm not sure that this has much value in a read-only filesystem, but
419 udf_pathconf(struct vop_pathconf_args *a)
424 *a->a_retval = 65535;
427 *a->a_retval = NAME_MAX;
430 *a->a_retval = PATH_MAX;
441 udf_read(struct vop_read_args *a)
443 struct vnode *vp = a->a_vp;
444 struct uio *uio = a->a_uio;
445 struct udf_node *node = VTON(vp);
449 int size, fsize, offset;
451 if (uio->uio_offset < 0)
454 fsize = node->fentry->inf_len;
456 while (uio->uio_offset < fsize && uio->uio_resid > 0) {
457 offset = uio->uio_offset;
458 size = uio->uio_resid;
459 error = udf_readatoffset(node, &size, offset, &bp, &data);
461 error = uiomove(data, size, uio);
472 * Call the OSTA routines to translate the name from a CS0 dstring to a
473 * 16-bit Unicode String. Hooks need to be placed in here to translate from
474 * Unicode to the encoding that the kernel/user expects. Return the length
475 * of the translated string.
478 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
480 unicode_t *transname;
481 int i, unilen = 0, destlen;
483 /* Convert 16-bit Unicode to destname */
484 /* allocate a buffer big enough to hold an 8->16 bit expansion */
485 transname = malloc(NAME_MAX * sizeof(unicode_t), M_TEMP, M_WAITOK | M_ZERO);
487 if ((unilen = udf_UncompressUnicode(len, cs0string, transname)) == -1) {
488 printf("udf: Unicode translation failed\n");
489 free(transname, M_TEMP);
493 for (i = 0; i < unilen ; i++)
494 if (transname[i] & 0xff00)
495 destname[i] = '.'; /* Fudge the 16bit chars */
497 destname[i] = transname[i] & 0xff;
498 free(transname, M_TEMP);
499 destname[unilen] = 0;
506 * Compare a CS0 dstring with a name passed in from the VFS layer. Return
507 * 0 on a successful match, nonzero therwise. Unicode work may need to be done
511 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
516 /* This is overkill, but not worth creating a new zone */
518 transname = malloc(NAME_MAX * sizeof(unicode_t), M_TEMP,
521 cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
523 /* Easy check. If they aren't the same length, they aren't equal */
524 if ((cs0len == 0) || (cs0len != cmplen))
527 error = bcmp(transname, cmpname, cmplen);
529 free(transname, M_TEMP);
534 struct dirent *dirent;
541 static struct udf_dirstream *
542 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
544 struct udf_dirstream *ds;
546 ds = malloc(sizeof(*ds), M_UDFDS, M_WAITOK | M_ZERO);
556 static struct fileid_desc *
557 udf_getfid(struct udf_dirstream *ds)
559 struct fileid_desc *fid;
560 int error, frag_size = 0, total_fid_size;
562 /* End of directory? */
563 if (ds->offset + ds->off >= ds->fsize) {
568 /* Grab the first extent of the directory */
573 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
582 * Clean up from a previous fragmented FID.
583 * XXX Is this the right place for this?
585 if (ds->fid_fragment && ds->buf != NULL) {
586 ds->fid_fragment = 0;
587 free(ds->buf, M_UDFFID);
590 fid = (struct fileid_desc*)&ds->data[ds->off];
593 * Check to see if the fid is fragmented. The first test
594 * ensures that we don't wander off the end of the buffer
595 * looking for the l_iu and l_fi fields.
597 if (ds->off + UDF_FID_SIZE > ds->size ||
598 ds->off + fid->l_iu + fid->l_fi + UDF_FID_SIZE > ds->size) {
600 /* Copy what we have of the fid into a buffer */
601 frag_size = ds->size - ds->off;
602 if (frag_size >= ds->udfmp->bsize) {
603 printf("udf: invalid FID fragment\n");
609 * File ID descriptors can only be at most one
610 * logical sector in size.
612 ds->buf = malloc(ds->udfmp->bsize, M_UDFFID, M_WAITOK | M_ZERO);
613 bcopy(fid, ds->buf, frag_size);
615 /* Reduce all of the casting magic */
616 fid = (struct fileid_desc*)ds->buf;
621 /* Fetch the next allocation */
622 ds->offset += ds->size;
624 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
632 * If the fragment was so small that we didn't get
633 * the l_iu and l_fi fields, copy those in.
635 if (frag_size < UDF_FID_SIZE)
636 bcopy(ds->data, &ds->buf[frag_size],
637 UDF_FID_SIZE - frag_size);
640 * Now that we have enough of the fid to work with,
641 * copy in the rest of the fid from the new
644 total_fid_size = UDF_FID_SIZE + fid->l_iu + fid->l_fi;
645 if (total_fid_size > ds->udfmp->bsize) {
646 printf("udf: invalid FID\n");
650 bcopy(ds->data, &ds->buf[frag_size],
651 total_fid_size - frag_size);
653 ds->fid_fragment = 1;
655 total_fid_size = fid->l_iu + fid->l_fi + UDF_FID_SIZE;
658 * Update the offset. Align on a 4 byte boundary because the
661 ds->this_off = ds->off;
662 if (!ds->fid_fragment)
663 ds->off += (total_fid_size + 3) & ~0x03;
665 ds->off = (total_fid_size - frag_size + 3) & ~0x03;
671 udf_closedir(struct udf_dirstream *ds)
677 if (ds->fid_fragment && ds->buf != NULL)
678 free(ds->buf, M_UDFFID);
684 udf_readdir(struct vop_readdir_args *a)
688 struct udf_node *node;
689 struct udf_mnt *udfmp;
690 struct fileid_desc *fid;
691 struct udf_uiodir uiodir;
692 struct udf_dirstream *ds;
693 u_long *cookies = NULL;
700 if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
708 if (a->a_ncookies != NULL) {
710 * Guess how many entries are needed. If we run out, this
711 * function will be called again and thing will pick up were
714 ncookies = uio->uio_resid / 8 + 1;
717 cookies = malloc(sizeof(u_long) * ncookies, M_TEMP, M_WAITOK);
718 uiodir.ncookies = ncookies;
719 uiodir.cookies = cookies;
722 uiodir.cookies = NULL;
725 * Iterate through the file id descriptors. Give the parent dir
726 * entry special attention.
728 ds = udf_opendir(node, uio->uio_offset, node->fentry->inf_len,
731 name = malloc(NAME_MAX, M_TEMP, M_WAITOK);
733 while ((fid = udf_getfid(ds)) != NULL) {
735 /* XXX Should we return an error on a bad fid? */
736 if (udf_checktag(&fid->tag, TAGID_FID)) {
737 printf("Invalid FID tag\n");
742 /* Is this a deleted file? */
743 if (fid->file_char & UDF_FILE_CHAR_DEL)
746 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
747 /* Do up the '.' and '..' entries. Dummy values are
748 * used for the cookies since the offset here is
749 * usually zero, and NFS doesn't like that value
751 if (uiodir.cookies != NULL) {
752 if (++uiodir.acookies > uiodir.ncookies) {
756 *uiodir.cookies++ = 1;
758 if (vop_write_dirent(&error, uio, node->hash_id, DT_DIR,
767 if (uiodir.cookies != NULL) {
768 if (++uiodir.acookies > uiodir.ncookies) {
772 *uiodir.cookies++ = 2;
774 if (vop_write_dirent(&error, uio, udf_getid(&fid->icb),
784 uint8_t d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
786 uint16_t namelen = udf_transname(&fid->data[fid->l_iu],
787 name, fid->l_fi, udfmp);
789 if (uiodir.cookies != NULL) {
790 if (++uiodir.acookies > uiodir.ncookies) {
794 *uiodir.cookies++ = ds->this_off;
796 if (vop_write_dirent(&error, uio, udf_getid(&fid->icb),
797 d_type, namelen, name)) {
807 printf("uiomove returned %d\n", error);
815 /* tell the calling layer whether we need to be called again */
816 *a->a_eofflag = uiodir.eofflag;
817 uio->uio_offset = ds->offset + ds->off;
824 if (a->a_ncookies != NULL) {
826 free(cookies, M_TEMP);
828 *a->a_ncookies = uiodir.acookies;
829 *a->a_cookies = cookies;
837 /* Are there any implementations out there that do soft-links? */
839 udf_readlink(struct vop_readlink_args *ap)
841 printf("%s called\n", __func__);
846 udf_strategy(struct vop_strategy_args *ap)
852 struct udf_node *node;
861 nbio = push_bio(bio);
862 if (nbio->bio_offset == NOOFFSET) {
864 * Files that are embedded in the fentry don't translate well
865 * to a block number. Reject.
867 if (udf_bmap_internal(node,
869 &dblkno, &maxsize)) {
871 nbio->bio_offset = NOOFFSET;
873 nbio->bio_offset = dbtob(dblkno);
876 if (nbio->bio_offset == NOOFFSET) {
877 /* I/O was never started on nbio, must biodone(bio) */
881 vn_strategy(node->i_devvp, nbio);
886 udf_bmap(struct vop_bmap_args *a)
888 struct udf_node *node;
893 node = VTON(a->a_vp);
895 if (a->a_vpp != NULL)
896 *a->a_vpp = node->i_devvp;
897 if (a->a_doffsetp == NULL)
900 KKASSERT(a->a_loffset % node->udfmp->bsize == 0);
902 error = udf_bmap_internal(node, a->a_loffset, &lsector, &max_size);
906 /* Translate logical to physical sector number */
907 *a->a_doffsetp = (off_t)lsector << node->udfmp->bshift;
909 /* Punt on read-ahead for now */
918 * The all powerful VOP_LOOKUP().
921 udf_lookup(struct vop_old_lookup_args *a)
924 struct vnode *tdp = NULL;
925 struct vnode **vpp = a->a_vpp;
926 struct udf_node *node;
927 struct udf_mnt *udfmp;
928 struct fileid_desc *fid = NULL;
929 struct udf_dirstream *ds;
931 globaldata_t gd = mycpu;
937 int offset, error = 0;
938 int numdirpasses, fsize;
943 nameiop = a->a_cnp->cn_nameiop;
944 flags = a->a_cnp->cn_flags;
945 nameptr = a->a_cnp->cn_nameptr;
946 namelen = a->a_cnp->cn_namelen;
947 fsize = node->fentry->inf_len;
948 td = a->a_cnp->cn_td;
951 * If this is a LOOKUP and we've already partially searched through
952 * the directory, pick up where we left off and flag that the
953 * directory may need to be searched twice. For a full description,
954 * see /sys/isofs/cd9660/cd9660_lookup.c:cd9660_lookup()
956 if (nameiop != NAMEI_LOOKUP || node->diroff == 0 ||
957 node->diroff > fsize) {
961 offset = node->diroff;
963 gd->gd_nchstats->ncs_2passes++;
967 ds = udf_opendir(node, offset, fsize, udfmp);
969 while ((fid = udf_getfid(ds)) != NULL) {
970 /* XXX Should we return an error on a bad fid? */
971 if (udf_checktag(&fid->tag, TAGID_FID)) {
972 printf("udf_lookup: Invalid tag\n");
977 /* Is this a deleted file? */
978 if (fid->file_char & UDF_FILE_CHAR_DEL)
981 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
982 if (flags & CNP_ISDOTDOT) {
983 id = udf_getid(&fid->icb);
987 if (!(udf_cmpname(&fid->data[fid->l_iu],
988 nameptr, fid->l_fi, namelen, udfmp))) {
989 id = udf_getid(&fid->icb);
998 /* XXX Bail out here? */
1004 /* Did we have a match? */
1006 error = udf_vget(udfmp->im_mountp, id, &tdp);
1009 * Remember where this entry was if it's the final
1012 if (nameiop == NAMEI_LOOKUP)
1013 node->diroff = ds->offset + ds->off;
1014 if (numdirpasses == 2)
1015 gd->gd_nchstats->ncs_pass2++;
1016 if ((flags & CNP_LOCKPARENT) == 0) {
1017 a->a_cnp->cn_flags |= CNP_PDIRUNLOCK;
1024 /* Name wasn't found on this pass. Do another pass? */
1025 if (numdirpasses == 2) {
1031 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) {
1043 udf_reclaim(struct vop_reclaim_args *a)
1046 struct udf_node *unode;
1051 if (unode != NULL) {
1053 if (unode->i_devvp) {
1054 vrele(unode->i_devvp);
1058 if (unode->fentry != NULL)
1059 free(unode->fentry, M_UDFFENTRY);
1060 free(unode, M_UDFNODE);
1068 * Read the block and then set the data pointer to correspond with the
1069 * offset passed in. Only read in at most 'size' bytes, and then set 'size'
1070 * to the number of bytes pointed to. If 'size' is zero, try to read in a
1073 * Note that *bp may be assigned error or not.
1075 * XXX 'size' is limited to the logical block size for now due to problems
1079 udf_readatoffset(struct udf_node *node, int *size, int offset, struct buf **bp,
1082 struct udf_mnt *udfmp;
1083 struct file_entry *fentry = NULL;
1089 udfmp = node->udfmp;
1092 error = udf_bmap_internal(node, offset, §or, &max_size);
1093 if (error == UDF_INVALID_BMAP) {
1095 * This error means that the file *data* is stored in the
1096 * allocation descriptor field of the file entry.
1098 fentry = node->fentry;
1099 *data = &fentry->data[fentry->l_ea];
1100 *size = fentry->l_ad;
1102 } else if (error != 0) {
1106 /* Adjust the size so that it is within range */
1107 if (*size == 0 || *size > max_size)
1109 *size = min(*size, MAXBSIZE);
1111 if ((error = udf_readlblks(udfmp, sector, *size, bp))) {
1112 printf("warning: udf_readlblks returned error %d\n", error);
1113 /* note: *bp may be non-NULL */
1118 *data = (uint8_t *)&bp1->b_data[offset % udfmp->bsize];
1123 * Translate a file offset into a logical block and then into a physical
1127 udf_bmap_internal(struct udf_node *node, uint32_t offset, daddr_t *sector, uint32_t *max_size)
1129 struct udf_mnt *udfmp;
1130 struct file_entry *fentry;
1132 struct icb_tag *tag;
1133 uint32_t icblen = 0;
1135 int ad_offset, ad_num = 0;
1138 udfmp = node->udfmp;
1139 fentry = node->fentry;
1140 tag = &fentry->icbtag;
1142 switch (tag->strat_type) {
1147 printf("Cannot deal with strategy4096 yet!\n");
1151 printf("Unknown strategy type %d\n", tag->strat_type);
1155 switch (tag->flags & 0x7) {
1158 * The allocation descriptor field is filled with short_ad's.
1159 * If the offset is beyond the current extent, look for the
1164 ad_offset = sizeof(struct short_ad) * ad_num;
1165 if (ad_offset > fentry->l_ad) {
1166 printf("File offset out of bounds\n");
1169 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1170 icblen = GETICBLEN(short_ad, icb);
1172 } while(offset >= icblen);
1174 lsector = (offset >> udfmp->bshift) +
1175 ((struct short_ad *)(icb))->pos;
1177 *max_size = GETICBLEN(short_ad, icb);
1182 * The allocation descriptor field is filled with long_ad's
1183 * If the offset is beyond the current extent, look for the
1188 ad_offset = sizeof(struct long_ad) * ad_num;
1189 if (ad_offset > fentry->l_ad) {
1190 printf("File offset out of bounds\n");
1193 icb = GETICB(long_ad, fentry, fentry->l_ea + ad_offset);
1194 icblen = GETICBLEN(long_ad, icb);
1196 } while(offset >= icblen);
1198 lsector = (offset >> udfmp->bshift) +
1199 ((struct long_ad *)(icb))->loc.lb_num;
1201 *max_size = GETICBLEN(long_ad, icb);
1206 * This type means that the file *data* is stored in the
1207 * allocation descriptor field of the file entry.
1210 *sector = node->hash_id + udfmp->part_start;
1212 return(UDF_INVALID_BMAP);
1214 /* DirectCD does not use extended_ad's */
1216 printf("Unsupported allocation descriptor %d\n",
1221 *sector = lsector + udfmp->part_start;
1224 * Check the sparing table. Each entry represents the beginning of
1227 if (udfmp->s_table != NULL) {
1228 for (i = 0; i< udfmp->s_table_entries; i++) {
1229 p_offset = lsector - udfmp->s_table->entries[i].org;
1230 if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1231 *sector = udfmp->s_table->entries[i].map +