]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/udf/udf_vnops.c
Flesh out pathconf() on UDF.
[FreeBSD/FreeBSD.git] / sys / fs / udf / udf_vnops.c
1 /*-
2  * Copyright (c) 2001, 2002 Scott Long <scottl@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /* udf_vnops.c */
30 /* Take care of the vnode side of things */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/stat.h>
38 #include <sys/bio.h>
39 #include <sys/conf.h>
40 #include <sys/buf.h>
41 #include <sys/iconv.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/dirent.h>
45 #include <sys/queue.h>
46 #include <sys/unistd.h>
47 #include <sys/endian.h>
48
49 #include <vm/uma.h>
50
51 #include <fs/udf/ecma167-udf.h>
52 #include <fs/udf/osta.h>
53 #include <fs/udf/udf.h>
54 #include <fs/udf/udf_mount.h>
55
56 extern struct iconv_functions *udf_iconv;
57
58 static vop_access_t     udf_access;
59 static vop_getattr_t    udf_getattr;
60 static vop_open_t       udf_open;
61 static vop_ioctl_t      udf_ioctl;
62 static vop_pathconf_t   udf_pathconf;
63 static vop_print_t      udf_print;
64 static vop_read_t       udf_read;
65 static vop_readdir_t    udf_readdir;
66 static vop_readlink_t   udf_readlink;
67 static vop_setattr_t    udf_setattr;
68 static vop_strategy_t   udf_strategy;
69 static vop_bmap_t       udf_bmap;
70 static vop_cachedlookup_t       udf_lookup;
71 static vop_reclaim_t    udf_reclaim;
72 static vop_vptofh_t     udf_vptofh;
73 static int udf_readatoffset(struct udf_node *node, int *size, off_t offset,
74     struct buf **bp, uint8_t **data);
75 static int udf_bmap_internal(struct udf_node *node, off_t offset,
76     daddr_t *sector, uint32_t *max_size);
77
78 static struct vop_vector udf_vnodeops = {
79         .vop_default =          &default_vnodeops,
80
81         .vop_access =           udf_access,
82         .vop_bmap =             udf_bmap,
83         .vop_cachedlookup =     udf_lookup,
84         .vop_getattr =          udf_getattr,
85         .vop_ioctl =            udf_ioctl,
86         .vop_lookup =           vfs_cache_lookup,
87         .vop_open =             udf_open,
88         .vop_pathconf =         udf_pathconf,
89         .vop_print =            udf_print,
90         .vop_read =             udf_read,
91         .vop_readdir =          udf_readdir,
92         .vop_readlink =         udf_readlink,
93         .vop_reclaim =          udf_reclaim,
94         .vop_setattr =          udf_setattr,
95         .vop_strategy =         udf_strategy,
96         .vop_vptofh =           udf_vptofh,
97 };
98
99 struct vop_vector udf_fifoops = {
100         .vop_default =          &fifo_specops,
101         .vop_access =           udf_access,
102         .vop_getattr =          udf_getattr,
103         .vop_print =            udf_print,
104         .vop_reclaim =          udf_reclaim,
105         .vop_setattr =          udf_setattr,
106         .vop_vptofh =           udf_vptofh,
107 };
108
109 static MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure");
110 static MALLOC_DEFINE(M_UDFDS, "udf_ds", "UDF Dirstream structure");
111
112 #define UDF_INVALID_BMAP        -1
113
114 int
115 udf_allocv(struct mount *mp, struct vnode **vpp, struct thread *td)
116 {
117         int error;
118         struct vnode *vp;
119
120         error = getnewvnode("udf", mp, &udf_vnodeops, &vp);
121         if (error) {
122                 printf("udf_allocv: failed to allocate new vnode\n");
123                 return (error);
124         }
125
126         *vpp = vp;
127         return (0);
128 }
129
130 /* Convert file entry permission (5 bits per owner/group/user) to a mode_t */
131 static mode_t
132 udf_permtomode(struct udf_node *node)
133 {
134         uint32_t perm;
135         uint16_t flags;
136         mode_t mode;
137
138         perm = le32toh(node->fentry->perm);
139         flags = le16toh(node->fentry->icbtag.flags);
140
141         mode = perm & UDF_FENTRY_PERM_USER_MASK;
142         mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK) >> 2);
143         mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
144         mode |= ((flags & UDF_ICB_TAG_FLAGS_STICKY) << 4);
145         mode |= ((flags & UDF_ICB_TAG_FLAGS_SETGID) << 6);
146         mode |= ((flags & UDF_ICB_TAG_FLAGS_SETUID) << 8);
147
148         return (mode);
149 }
150
151 static int
152 udf_access(struct vop_access_args *a)
153 {
154         struct vnode *vp;
155         struct udf_node *node;
156         accmode_t accmode;
157         mode_t mode;
158
159         vp = a->a_vp;
160         node = VTON(vp);
161         accmode = a->a_accmode;
162
163         if (accmode & VWRITE) {
164                 switch (vp->v_type) {
165                 case VDIR:
166                 case VLNK:
167                 case VREG:
168                         return (EROFS);
169                         /* NOT REACHED */
170                 default:
171                         break;
172                 }
173         }
174
175         mode = udf_permtomode(node);
176
177         return (vaccess(vp->v_type, mode, node->fentry->uid, node->fentry->gid,
178             accmode, a->a_cred, NULL));
179 }
180
181 static int
182 udf_open(struct vop_open_args *ap) {
183         struct udf_node *np = VTON(ap->a_vp);
184         off_t fsize;
185
186         fsize = le64toh(np->fentry->inf_len);
187         vnode_create_vobject(ap->a_vp, fsize, ap->a_td);
188         return 0;
189 }
190
191 static const int mon_lens[2][12] = {
192         {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
193         {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
194 };
195
196 static int
197 udf_isaleapyear(int year)
198 {
199         int i;
200
201         i = (year % 4) ? 0 : 1;
202         i &= (year % 100) ? 1 : 0;
203         i |= (year % 400) ? 0 : 1;
204
205         return i;
206 }
207
208 /*
209  * Timezone calculation compliments of Julian Elischer <julian@elischer.org>.
210  */
211 static void
212 udf_timetotimespec(struct timestamp *time, struct timespec *t)
213 {
214         int i, lpyear, daysinyear, year, startyear;
215         union {
216                 uint16_t        u_tz_offset;
217                 int16_t         s_tz_offset;
218         } tz;
219
220         /*
221          * DirectCD seems to like using bogus year values.
222          * Don't trust time->month as it will be used for an array index.
223          */
224         year = le16toh(time->year);
225         if (year < 1970 || time->month < 1 || time->month > 12) {
226                 t->tv_sec = 0;
227                 t->tv_nsec = 0;
228                 return;
229         }
230
231         /* Calculate the time and day */
232         t->tv_sec = time->second;
233         t->tv_sec += time->minute * 60;
234         t->tv_sec += time->hour * 3600;
235         t->tv_sec += (time->day - 1) * 3600 * 24;
236
237         /* Calculate the month */
238         lpyear = udf_isaleapyear(year);
239         t->tv_sec += mon_lens[lpyear][time->month - 1] * 3600 * 24;
240
241         /* Speed up the calculation */
242         startyear = 1970;
243         if (year > 2009) {
244                 t->tv_sec += 1262304000;
245                 startyear += 40;
246         } else if (year > 1999) {
247                 t->tv_sec += 946684800;
248                 startyear += 30;
249         } else if (year > 1989) {
250                 t->tv_sec += 631152000;
251                 startyear += 20;
252         } else if (year > 1979) {
253                 t->tv_sec += 315532800;
254                 startyear += 10;
255         }
256
257         daysinyear = (year - startyear) * 365;
258         for (i = startyear; i < year; i++)
259                 daysinyear += udf_isaleapyear(i);
260         t->tv_sec += daysinyear * 3600 * 24;
261
262         /* Calculate microseconds */
263         t->tv_nsec = time->centisec * 10000 + time->hund_usec * 100 +
264             time->usec;
265
266         /*
267          * Calculate the time zone.  The timezone is 12 bit signed 2's
268          * complement, so we gotta do some extra magic to handle it right.
269          */
270         tz.u_tz_offset = le16toh(time->type_tz);
271         tz.u_tz_offset &= 0x0fff;
272         if (tz.u_tz_offset & 0x0800)
273                 tz.u_tz_offset |= 0xf000;       /* extend the sign to 16 bits */
274         if ((le16toh(time->type_tz) & 0x1000) && (tz.s_tz_offset != -2047))
275                 t->tv_sec -= tz.s_tz_offset * 60;
276
277         return;
278 }
279
280 static int
281 udf_getattr(struct vop_getattr_args *a)
282 {
283         struct vnode *vp;
284         struct udf_node *node;
285         struct vattr *vap;
286         struct file_entry *fentry;
287         struct timespec ts;
288
289         ts.tv_sec = 0;
290
291         vp = a->a_vp;
292         vap = a->a_vap;
293         node = VTON(vp);
294         fentry = node->fentry;
295
296         vap->va_fsid = dev2udev(node->udfmp->im_dev);
297         vap->va_fileid = node->hash_id;
298         vap->va_mode = udf_permtomode(node);
299         vap->va_nlink = le16toh(fentry->link_cnt);
300         /*
301          * XXX The spec says that -1 is valid for uid/gid and indicates an
302          * invalid uid/gid.  How should this be represented?
303          */
304         vap->va_uid = (le32toh(fentry->uid) == -1) ? 0 : le32toh(fentry->uid);
305         vap->va_gid = (le32toh(fentry->gid) == -1) ? 0 : le32toh(fentry->gid);
306         udf_timetotimespec(&fentry->atime, &vap->va_atime);
307         udf_timetotimespec(&fentry->mtime, &vap->va_mtime);
308         vap->va_ctime = vap->va_mtime; /* XXX Stored as an Extended Attribute */
309         vap->va_rdev = NODEV;
310         if (vp->v_type & VDIR) {
311                 /*
312                  * Directories that are recorded within their ICB will show
313                  * as having 0 blocks recorded.  Since tradition dictates
314                  * that directories consume at least one logical block,
315                  * make it appear so.
316                  */
317                 if (fentry->logblks_rec != 0) {
318                         vap->va_size =
319                             le64toh(fentry->logblks_rec) * node->udfmp->bsize;
320                 } else {
321                         vap->va_size = node->udfmp->bsize;
322                 }
323         } else {
324                 vap->va_size = le64toh(fentry->inf_len);
325         }
326         vap->va_flags = 0;
327         vap->va_gen = 1;
328         vap->va_blocksize = node->udfmp->bsize;
329         vap->va_bytes = le64toh(fentry->inf_len);
330         vap->va_type = vp->v_type;
331         vap->va_filerev = 0; /* XXX */
332         return (0);
333 }
334
335 static int
336 udf_setattr(struct vop_setattr_args *a)
337 {
338         struct vnode *vp;
339         struct vattr *vap;
340
341         vp = a->a_vp;
342         vap = a->a_vap;
343         if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
344             vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
345             vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
346                 return (EROFS);
347         if (vap->va_size != (u_quad_t)VNOVAL) {
348                 switch (vp->v_type) {
349                 case VDIR:
350                         return (EISDIR);
351                 case VLNK:
352                 case VREG:
353                         return (EROFS);
354                 case VCHR:
355                 case VBLK:
356                 case VSOCK:
357                 case VFIFO:
358                 case VNON:
359                 case VBAD:
360                 case VMARKER:
361                         return (0);
362                 }
363         }
364         return (0);
365 }
366
367 /*
368  * File specific ioctls.
369  */
370 static int
371 udf_ioctl(struct vop_ioctl_args *a)
372 {
373         printf("%s called\n", __func__);
374         return (ENOTTY);
375 }
376
377 /*
378  * I'm not sure that this has much value in a read-only filesystem, but
379  * cd9660 has it too.
380  */
381 static int
382 udf_pathconf(struct vop_pathconf_args *a)
383 {
384
385         switch (a->a_name) {
386         case _PC_FILESIZEBITS:
387                 *a->a_retval = 64;
388                 return (0);
389         case _PC_LINK_MAX:
390                 *a->a_retval = 65535;
391                 return (0);
392         case _PC_NAME_MAX:
393                 *a->a_retval = NAME_MAX;
394                 return (0);
395         case _PC_SYMLINK_MAX:
396                 *a->a_retval = MAXPATHLEN;
397                 return (0);
398         case _PC_NO_TRUNC:
399                 *a->a_retval = 1;
400                 return (0);
401         default:
402                 return (vop_stdpathconf(a));
403         }
404 }
405
406 static int
407 udf_print(struct vop_print_args *ap)
408 {
409         struct vnode *vp = ap->a_vp;
410         struct udf_node *node = VTON(vp);
411
412         printf("    ino %lu, on dev %s", (u_long)node->hash_id,
413             devtoname(node->udfmp->im_dev));
414         if (vp->v_type == VFIFO)
415                 fifo_printinfo(vp);
416         printf("\n");
417         return (0);
418 }
419
420 #define lblkno(udfmp, loc)      ((loc) >> (udfmp)->bshift)
421 #define blkoff(udfmp, loc)      ((loc) & (udfmp)->bmask)
422 #define lblktosize(udfmp, blk)  ((blk) << (udfmp)->bshift)
423
424 static inline int
425 is_data_in_fentry(const struct udf_node *node)
426 {
427         const struct file_entry *fentry = node->fentry;
428
429         return ((le16toh(fentry->icbtag.flags) & 0x7) == 3);
430 }
431
432 static int
433 udf_read(struct vop_read_args *ap)
434 {
435         struct vnode *vp = ap->a_vp;
436         struct uio *uio = ap->a_uio;
437         struct udf_node *node = VTON(vp);
438         struct udf_mnt *udfmp;
439         struct file_entry *fentry;
440         struct buf *bp;
441         uint8_t *data;
442         daddr_t lbn, rablock;
443         off_t diff, fsize;
444         ssize_t n;
445         int error = 0;
446         long size, on;
447
448         if (uio->uio_resid == 0)
449                 return (0);
450         if (uio->uio_offset < 0)
451                 return (EINVAL);
452
453         if (is_data_in_fentry(node)) {
454                 fentry = node->fentry;
455                 data = &fentry->data[le32toh(fentry->l_ea)];
456                 fsize = le32toh(fentry->l_ad);
457
458                 n = uio->uio_resid;
459                 diff = fsize - uio->uio_offset;
460                 if (diff <= 0)
461                         return (0);
462                 if (diff < n)
463                         n = diff;
464                 error = uiomove(data + uio->uio_offset, (int)n, uio);
465                 return (error);
466         }
467
468         fsize = le64toh(node->fentry->inf_len);
469         udfmp = node->udfmp;
470         do {
471                 lbn = lblkno(udfmp, uio->uio_offset);
472                 on = blkoff(udfmp, uio->uio_offset);
473                 n = min((u_int)(udfmp->bsize - on),
474                         uio->uio_resid);
475                 diff = fsize - uio->uio_offset;
476                 if (diff <= 0)
477                         return (0);
478                 if (diff < n)
479                         n = diff;
480                 size = udfmp->bsize;
481                 rablock = lbn + 1;
482                 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
483                         if (lblktosize(udfmp, rablock) < fsize) {
484                                 error = cluster_read(vp, fsize, lbn, size,
485                                     NOCRED, uio->uio_resid,
486                                     (ap->a_ioflag >> 16), 0, &bp);
487                         } else {
488                                 error = bread(vp, lbn, size, NOCRED, &bp);
489                         }
490                 } else {
491                         error = bread(vp, lbn, size, NOCRED, &bp);
492                 }
493                 if (error != 0) {
494                         brelse(bp);
495                         return (error);
496                 }
497                 n = min(n, size - bp->b_resid);
498
499                 error = uiomove(bp->b_data + on, (int)n, uio);
500                 brelse(bp);
501         } while (error == 0 && uio->uio_resid > 0 && n != 0);
502         return (error);
503 }
504
505 /*
506  * Call the OSTA routines to translate the name from a CS0 dstring to a
507  * 16-bit Unicode String.  Hooks need to be placed in here to translate from
508  * Unicode to the encoding that the kernel/user expects.  Return the length
509  * of the translated string.
510  */
511 static int
512 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
513 {
514         unicode_t *transname;
515         char *unibuf, *unip;
516         int i, destlen;
517         ssize_t unilen = 0;
518         size_t destleft = MAXNAMLEN;
519
520         /* Convert 16-bit Unicode to destname */
521         if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
522                 /* allocate a buffer big enough to hold an 8->16 bit expansion */
523                 unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
524                 unip = unibuf;
525                 if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
526                         printf("udf: Unicode translation failed\n");
527                         uma_zfree(udf_zone_trans, unibuf);
528                         return 0;
529                 }
530
531                 while (unilen > 0 && destleft > 0) {
532                         udf_iconv->conv(udfmp->im_d2l, __DECONST(const char **,
533                             &unibuf), (size_t *)&unilen, (char **)&destname,
534                             &destleft);
535                         /* Unconverted character found */
536                         if (unilen > 0 && destleft > 0) {
537                                 *destname++ = '?';
538                                 destleft--;
539                                 unibuf += 2;
540                                 unilen -= 2;
541                         }
542                 }
543                 uma_zfree(udf_zone_trans, unip);
544                 *destname = '\0';
545                 destlen = MAXNAMLEN - (int)destleft;
546         } else {
547                 /* allocate a buffer big enough to hold an 8->16 bit expansion */
548                 transname = uma_zalloc(udf_zone_trans, M_WAITOK);
549
550                 if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) {
551                         printf("udf: Unicode translation failed\n");
552                         uma_zfree(udf_zone_trans, transname);
553                         return 0;
554                 }
555
556                 for (i = 0; i < unilen ; i++) {
557                         if (transname[i] & 0xff00) {
558                                 destname[i] = '.';      /* Fudge the 16bit chars */
559                         } else {
560                                 destname[i] = transname[i] & 0xff;
561                         }
562                 }
563                 uma_zfree(udf_zone_trans, transname);
564                 destname[unilen] = 0;
565                 destlen = (int)unilen;
566         }
567
568         return (destlen);
569 }
570
571 /*
572  * Compare a CS0 dstring with a name passed in from the VFS layer.  Return
573  * 0 on a successful match, nonzero otherwise.  Unicode work may need to be done
574  * here also.
575  */
576 static int
577 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
578 {
579         char *transname;
580         int error = 0;
581
582         /* This is overkill, but not worth creating a new zone */
583         transname = uma_zalloc(udf_zone_trans, M_WAITOK);
584
585         cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
586
587         /* Easy check.  If they aren't the same length, they aren't equal */
588         if ((cs0len == 0) || (cs0len != cmplen))
589                 error = -1;
590         else
591                 error = bcmp(transname, cmpname, cmplen);
592
593         uma_zfree(udf_zone_trans, transname);
594         return (error);
595 }
596
597 struct udf_uiodir {
598         struct dirent *dirent;
599         u_long *cookies;
600         int ncookies;
601         int acookies;
602         int eofflag;
603 };
604
605 static int
606 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
607 {
608         if (uiodir->cookies != NULL) {
609                 if (++uiodir->acookies > uiodir->ncookies) {
610                         uiodir->eofflag = 0;
611                         return (-1);
612                 }
613                 *uiodir->cookies++ = cookie;
614         }
615
616         if (uio->uio_resid < de_size) {
617                 uiodir->eofflag = 0;
618                 return (-1);
619         }
620
621         return (uiomove(uiodir->dirent, de_size, uio));
622 }
623
624 static struct udf_dirstream *
625 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
626 {
627         struct udf_dirstream *ds;
628
629         ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
630
631         ds->node = node;
632         ds->offset = offset;
633         ds->udfmp = udfmp;
634         ds->fsize = fsize;
635
636         return (ds);
637 }
638
639 static struct fileid_desc *
640 udf_getfid(struct udf_dirstream *ds)
641 {
642         struct fileid_desc *fid;
643         int error, frag_size = 0, total_fid_size;
644
645         /* End of directory? */
646         if (ds->offset + ds->off >= ds->fsize) {
647                 ds->error = 0;
648                 return (NULL);
649         }
650
651         /* Grab the first extent of the directory */
652         if (ds->off == 0) {
653                 ds->size = 0;
654                 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
655                     &ds->bp, &ds->data);
656                 if (error) {
657                         ds->error = error;
658                         if (ds->bp != NULL)
659                                 brelse(ds->bp);
660                         return (NULL);
661                 }
662         }
663
664         /*
665          * Clean up from a previous fragmented FID.
666          * XXX Is this the right place for this?
667          */
668         if (ds->fid_fragment && ds->buf != NULL) {
669                 ds->fid_fragment = 0;
670                 free(ds->buf, M_UDFFID);
671         }
672
673         fid = (struct fileid_desc*)&ds->data[ds->off];
674
675         /*
676          * Check to see if the fid is fragmented. The first test
677          * ensures that we don't wander off the end of the buffer
678          * looking for the l_iu and l_fi fields.
679          */
680         if (ds->off + UDF_FID_SIZE > ds->size ||
681             ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){
682
683                 /* Copy what we have of the fid into a buffer */
684                 frag_size = ds->size - ds->off;
685                 if (frag_size >= ds->udfmp->bsize) {
686                         printf("udf: invalid FID fragment\n");
687                         ds->error = EINVAL;
688                         return (NULL);
689                 }
690
691                 /*
692                  * File ID descriptors can only be at most one
693                  * logical sector in size.
694                  */
695                 ds->buf = malloc(ds->udfmp->bsize, M_UDFFID,
696                      M_WAITOK | M_ZERO);
697                 bcopy(fid, ds->buf, frag_size);
698
699                 /* Reduce all of the casting magic */
700                 fid = (struct fileid_desc*)ds->buf;
701
702                 if (ds->bp != NULL)
703                         brelse(ds->bp);
704
705                 /* Fetch the next allocation */
706                 ds->offset += ds->size;
707                 ds->size = 0;
708                 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
709                     &ds->bp, &ds->data);
710                 if (error) {
711                         ds->error = error;
712                         return (NULL);
713                 }
714
715                 /*
716                  * If the fragment was so small that we didn't get
717                  * the l_iu and l_fi fields, copy those in.
718                  */
719                 if (frag_size < UDF_FID_SIZE)
720                         bcopy(ds->data, &ds->buf[frag_size],
721                             UDF_FID_SIZE - frag_size);
722
723                 /*
724                  * Now that we have enough of the fid to work with,
725                  * copy in the rest of the fid from the new
726                  * allocation.
727                  */
728                 total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi;
729                 if (total_fid_size > ds->udfmp->bsize) {
730                         printf("udf: invalid FID\n");
731                         ds->error = EIO;
732                         return (NULL);
733                 }
734                 bcopy(ds->data, &ds->buf[frag_size],
735                     total_fid_size - frag_size);
736
737                 ds->fid_fragment = 1;
738         } else {
739                 total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE;
740         }
741
742         /*
743          * Update the offset. Align on a 4 byte boundary because the
744          * UDF spec says so.
745          */
746         ds->this_off = ds->offset + ds->off;
747         if (!ds->fid_fragment) {
748                 ds->off += (total_fid_size + 3) & ~0x03;
749         } else {
750                 ds->off = (total_fid_size - frag_size + 3) & ~0x03;
751         }
752
753         return (fid);
754 }
755
756 static void
757 udf_closedir(struct udf_dirstream *ds)
758 {
759
760         if (ds->bp != NULL)
761                 brelse(ds->bp);
762
763         if (ds->fid_fragment && ds->buf != NULL)
764                 free(ds->buf, M_UDFFID);
765
766         uma_zfree(udf_zone_ds, ds);
767 }
768
769 static int
770 udf_readdir(struct vop_readdir_args *a)
771 {
772         struct vnode *vp;
773         struct uio *uio;
774         struct dirent dir;
775         struct udf_node *node;
776         struct udf_mnt *udfmp;
777         struct fileid_desc *fid;
778         struct udf_uiodir uiodir;
779         struct udf_dirstream *ds;
780         u_long *cookies = NULL;
781         int ncookies;
782         int error = 0;
783
784         vp = a->a_vp;
785         uio = a->a_uio;
786         node = VTON(vp);
787         udfmp = node->udfmp;
788         uiodir.eofflag = 1;
789
790         if (a->a_ncookies != NULL) {
791                 /*
792                  * Guess how many entries are needed.  If we run out, this
793                  * function will be called again and thing will pick up were
794                  * it left off.
795                  */
796                 ncookies = uio->uio_resid / 8;
797                 cookies = malloc(sizeof(u_long) * ncookies,
798                     M_TEMP, M_WAITOK);
799                 if (cookies == NULL)
800                         return (ENOMEM);
801                 uiodir.ncookies = ncookies;
802                 uiodir.cookies = cookies;
803                 uiodir.acookies = 0;
804         } else {
805                 uiodir.cookies = NULL;
806         }
807
808         /*
809          * Iterate through the file id descriptors.  Give the parent dir
810          * entry special attention.
811          */
812         ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
813             node->udfmp);
814
815         while ((fid = udf_getfid(ds)) != NULL) {
816
817                 /* XXX Should we return an error on a bad fid? */
818                 if (udf_checktag(&fid->tag, TAGID_FID)) {
819                         printf("Invalid FID tag\n");
820                         hexdump(fid, UDF_FID_SIZE, NULL, 0);
821                         error = EIO;
822                         break;
823                 }
824
825                 /* Is this a deleted file? */
826                 if (fid->file_char & UDF_FILE_CHAR_DEL)
827                         continue;
828
829                 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
830                         /* Do up the '.' and '..' entries.  Dummy values are
831                          * used for the cookies since the offset here is
832                          * usually zero, and NFS doesn't like that value
833                          */
834                         dir.d_fileno = node->hash_id;
835                         dir.d_type = DT_DIR;
836                         dir.d_name[0] = '.';
837                         dir.d_name[1] = '\0';
838                         dir.d_namlen = 1;
839                         dir.d_reclen = GENERIC_DIRSIZ(&dir);
840                         uiodir.dirent = &dir;
841                         error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
842                         if (error)
843                                 break;
844
845                         dir.d_fileno = udf_getid(&fid->icb);
846                         dir.d_type = DT_DIR;
847                         dir.d_name[0] = '.';
848                         dir.d_name[1] = '.';
849                         dir.d_name[2] = '\0';
850                         dir.d_namlen = 2;
851                         dir.d_reclen = GENERIC_DIRSIZ(&dir);
852                         uiodir.dirent = &dir;
853                         error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
854                 } else {
855                         dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
856                             &dir.d_name[0], fid->l_fi, udfmp);
857                         dir.d_fileno = udf_getid(&fid->icb);
858                         dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
859                             DT_DIR : DT_UNKNOWN;
860                         dir.d_reclen = GENERIC_DIRSIZ(&dir);
861                         uiodir.dirent = &dir;
862                         error = udf_uiodir(&uiodir, dir.d_reclen, uio,
863                             ds->this_off);
864                 }
865                 if (error)
866                         break;
867                 uio->uio_offset = ds->offset + ds->off;
868         }
869
870         /* tell the calling layer whether we need to be called again */
871         *a->a_eofflag = uiodir.eofflag;
872
873         if (error < 0)
874                 error = 0;
875         if (!error)
876                 error = ds->error;
877
878         udf_closedir(ds);
879
880         if (a->a_ncookies != NULL) {
881                 if (error)
882                         free(cookies, M_TEMP);
883                 else {
884                         *a->a_ncookies = uiodir.acookies;
885                         *a->a_cookies = cookies;
886                 }
887         }
888
889         return (error);
890 }
891
892 static int
893 udf_readlink(struct vop_readlink_args *ap)
894 {
895         struct path_component *pc, *end;
896         struct vnode *vp;
897         struct uio uio;
898         struct iovec iov[1];
899         struct udf_node *node;
900         void *buf;
901         char *cp;
902         int error, len, root;
903
904         /*
905          * A symbolic link in UDF is a list of variable-length path
906          * component structures.  We build a pathname in the caller's
907          * uio by traversing this list.
908          */
909         vp = ap->a_vp;
910         node = VTON(vp);
911         len = le64toh(node->fentry->inf_len);
912         buf = malloc(len, M_DEVBUF, M_WAITOK);
913         iov[0].iov_len = len;
914         iov[0].iov_base = buf;
915         uio.uio_iov = iov;
916         uio.uio_iovcnt = 1;
917         uio.uio_offset = 0;
918         uio.uio_resid = iov[0].iov_len;
919         uio.uio_segflg = UIO_SYSSPACE;
920         uio.uio_rw = UIO_READ;
921         uio.uio_td = curthread;
922         error = VOP_READ(vp, &uio, 0, ap->a_cred);
923         if (error)
924                 goto error;
925
926         pc = buf;
927         end = (void *)((char *)buf + len);
928         root = 0;
929         while (pc < end) {
930                 switch (pc->type) {
931                 case UDF_PATH_ROOT:
932                         /* Only allow this at the beginning of a path. */
933                         if ((void *)pc != buf) {
934                                 error = EINVAL;
935                                 goto error;
936                         }
937                         cp = "/";
938                         len = 1;
939                         root = 1;
940                         break;
941                 case UDF_PATH_DOT:
942                         cp = ".";
943                         len = 1;
944                         break;
945                 case UDF_PATH_DOTDOT:
946                         cp = "..";
947                         len = 2;
948                         break;
949                 case UDF_PATH_PATH:
950                         if (pc->length == 0) {
951                                 error = EINVAL;
952                                 goto error;
953                         }
954                         /*
955                          * XXX: We only support CS8 which appears to map
956                          * to ASCII directly.
957                          */
958                         switch (pc->identifier[0]) {
959                         case 8:
960                                 cp = pc->identifier + 1;
961                                 len = pc->length - 1;
962                                 break;
963                         default:
964                                 error = EOPNOTSUPP;
965                                 goto error;
966                         }
967                         break;
968                 default:
969                         error = EINVAL;
970                         goto error;
971                 }
972
973                 /*
974                  * If this is not the first component, insert a path
975                  * separator.
976                  */
977                 if (pc != buf) {
978                         /* If we started with root we already have a "/". */
979                         if (root)
980                                 goto skipslash;
981                         root = 0;
982                         if (ap->a_uio->uio_resid < 1) {
983                                 error = ENAMETOOLONG;
984                                 goto error;
985                         }
986                         error = uiomove("/", 1, ap->a_uio);
987                         if (error)
988                                 break;
989                 }
990         skipslash:
991
992                 /* Append string at 'cp' of length 'len' to our path. */
993                 if (len > ap->a_uio->uio_resid) {
994                         error = ENAMETOOLONG;
995                         goto error;
996                 }
997                 error = uiomove(cp, len, ap->a_uio);
998                 if (error)
999                         break;
1000
1001                 /* Advance to next component. */
1002                 pc = (void *)((char *)pc + 4 + pc->length);
1003         }
1004 error:
1005         free(buf, M_DEVBUF);
1006         return (error);
1007 }
1008
1009 static int
1010 udf_strategy(struct vop_strategy_args *a)
1011 {
1012         struct buf *bp;
1013         struct vnode *vp;
1014         struct udf_node *node;
1015         struct bufobj *bo;
1016         off_t offset;
1017         uint32_t maxsize;
1018         daddr_t sector;
1019         int error;
1020
1021         bp = a->a_bp;
1022         vp = a->a_vp;
1023         node = VTON(vp);
1024
1025         if (bp->b_blkno == bp->b_lblkno) {
1026                 offset = lblktosize(node->udfmp, bp->b_lblkno);
1027                 error = udf_bmap_internal(node, offset, &sector, &maxsize);
1028                 if (error) {
1029                         clrbuf(bp);
1030                         bp->b_blkno = -1;
1031                         bufdone(bp);
1032                         return (0);
1033                 }
1034                 /* bmap gives sector numbers, bio works with device blocks */
1035                 bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT);
1036         }
1037         bo = node->udfmp->im_bo;
1038         bp->b_iooffset = dbtob(bp->b_blkno);
1039         BO_STRATEGY(bo, bp);
1040         return (0);
1041 }
1042
1043 static int
1044 udf_bmap(struct vop_bmap_args *a)
1045 {
1046         struct udf_node *node;
1047         uint32_t max_size;
1048         daddr_t lsector;
1049         int nblk;
1050         int error;
1051
1052         node = VTON(a->a_vp);
1053
1054         if (a->a_bop != NULL)
1055                 *a->a_bop = &node->udfmp->im_devvp->v_bufobj;
1056         if (a->a_bnp == NULL)
1057                 return (0);
1058         if (a->a_runb)
1059                 *a->a_runb = 0;
1060
1061         /*
1062          * UDF_INVALID_BMAP means data embedded into fentry, this is an internal
1063          * error that should not be propagated to calling code.
1064          * Most obvious mapping for this error is EOPNOTSUPP as we can not truly
1065          * translate block numbers in this case.
1066          * Incidentally, this return code will make vnode pager to use VOP_READ
1067          * to get data for mmap-ed pages and udf_read knows how to do the right
1068          * thing for this kind of files.
1069          */
1070         error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift,
1071             &lsector, &max_size);
1072         if (error == UDF_INVALID_BMAP)
1073                 return (EOPNOTSUPP);
1074         if (error)
1075                 return (error);
1076
1077         /* Translate logical to physical sector number */
1078         *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
1079
1080         /*
1081          * Determine maximum number of readahead blocks following the
1082          * requested block.
1083          */
1084         if (a->a_runp) {
1085                 nblk = (max_size >> node->udfmp->bshift) - 1;
1086                 if (nblk <= 0)
1087                         *a->a_runp = 0;
1088                 else if (nblk >= (MAXBSIZE >> node->udfmp->bshift))
1089                         *a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1;
1090                 else
1091                         *a->a_runp = nblk;
1092         }
1093
1094         if (a->a_runb) {
1095                 *a->a_runb = 0;
1096         }
1097
1098         return (0);
1099 }
1100
1101 /*
1102  * The all powerful VOP_LOOKUP().
1103  */
1104 static int
1105 udf_lookup(struct vop_cachedlookup_args *a)
1106 {
1107         struct vnode *dvp;
1108         struct vnode *tdp = NULL;
1109         struct vnode **vpp = a->a_vpp;
1110         struct udf_node *node;
1111         struct udf_mnt *udfmp;
1112         struct fileid_desc *fid = NULL;
1113         struct udf_dirstream *ds;
1114         u_long nameiop;
1115         u_long flags;
1116         char *nameptr;
1117         long namelen;
1118         ino_t id = 0;
1119         int offset, error = 0;
1120         int fsize, lkflags, ltype, numdirpasses;
1121
1122         dvp = a->a_dvp;
1123         node = VTON(dvp);
1124         udfmp = node->udfmp;
1125         nameiop = a->a_cnp->cn_nameiop;
1126         flags = a->a_cnp->cn_flags;
1127         lkflags = a->a_cnp->cn_lkflags;
1128         nameptr = a->a_cnp->cn_nameptr;
1129         namelen = a->a_cnp->cn_namelen;
1130         fsize = le64toh(node->fentry->inf_len);
1131
1132         /*
1133          * If this is a LOOKUP and we've already partially searched through
1134          * the directory, pick up where we left off and flag that the
1135          * directory may need to be searched twice.  For a full description,
1136          * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
1137          */
1138         if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
1139                 offset = 0;
1140                 numdirpasses = 1;
1141         } else {
1142                 offset = node->diroff;
1143                 numdirpasses = 2;
1144                 nchstats.ncs_2passes++;
1145         }
1146
1147 lookloop:
1148         ds = udf_opendir(node, offset, fsize, udfmp);
1149
1150         while ((fid = udf_getfid(ds)) != NULL) {
1151
1152                 /* XXX Should we return an error on a bad fid? */
1153                 if (udf_checktag(&fid->tag, TAGID_FID)) {
1154                         printf("udf_lookup: Invalid tag\n");
1155                         error = EIO;
1156                         break;
1157                 }
1158
1159                 /* Is this a deleted file? */
1160                 if (fid->file_char & UDF_FILE_CHAR_DEL)
1161                         continue;
1162
1163                 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
1164                         if (flags & ISDOTDOT) {
1165                                 id = udf_getid(&fid->icb);
1166                                 break;
1167                         }
1168                 } else {
1169                         if (!(udf_cmpname(&fid->data[fid->l_iu],
1170                             nameptr, fid->l_fi, namelen, udfmp))) {
1171                                 id = udf_getid(&fid->icb);
1172                                 break;
1173                         }
1174                 }
1175         }
1176
1177         if (!error)
1178                 error = ds->error;
1179
1180         /* XXX Bail out here? */
1181         if (error) {
1182                 udf_closedir(ds);
1183                 return (error);
1184         }
1185
1186         /* Did we have a match? */
1187         if (id) {
1188                 /*
1189                  * Remember where this entry was if it's the final
1190                  * component.
1191                  */
1192                 if ((flags & ISLASTCN) && nameiop == LOOKUP)
1193                         node->diroff = ds->offset + ds->off;
1194                 if (numdirpasses == 2)
1195                         nchstats.ncs_pass2++;
1196                 udf_closedir(ds);
1197
1198                 if (flags & ISDOTDOT) {
1199                         error = vn_vget_ino(dvp, id, lkflags, &tdp);
1200                 } else if (node->hash_id == id) {
1201                         VREF(dvp);      /* we want ourself, ie "." */
1202                         /*
1203                          * When we lookup "." we still can be asked to lock it
1204                          * differently.
1205                          */
1206                         ltype = lkflags & LK_TYPE_MASK;
1207                         if (ltype != VOP_ISLOCKED(dvp)) {
1208                                 if (ltype == LK_EXCLUSIVE)
1209                                         vn_lock(dvp, LK_UPGRADE | LK_RETRY);
1210                                 else /* if (ltype == LK_SHARED) */
1211                                         vn_lock(dvp, LK_DOWNGRADE | LK_RETRY);
1212                         }
1213                         tdp = dvp;
1214                 } else
1215                         error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp);
1216                 if (!error) {
1217                         *vpp = tdp;
1218                         /* Put this entry in the cache */
1219                         if (flags & MAKEENTRY)
1220                                 cache_enter(dvp, *vpp, a->a_cnp);
1221                 }
1222         } else {
1223                 /* Name wasn't found on this pass.  Do another pass? */
1224                 if (numdirpasses == 2) {
1225                         numdirpasses--;
1226                         offset = 0;
1227                         udf_closedir(ds);
1228                         goto lookloop;
1229                 }
1230                 udf_closedir(ds);
1231
1232                 /* Enter name into cache as non-existant */
1233                 if (flags & MAKEENTRY)
1234                         cache_enter(dvp, *vpp, a->a_cnp);
1235
1236                 if ((flags & ISLASTCN) &&
1237                     (nameiop == CREATE || nameiop == RENAME)) {
1238                         error = EROFS;
1239                 } else {
1240                         error = ENOENT;
1241                 }
1242         }
1243
1244         return (error);
1245 }
1246
1247 static int
1248 udf_reclaim(struct vop_reclaim_args *a)
1249 {
1250         struct vnode *vp;
1251         struct udf_node *unode;
1252
1253         vp = a->a_vp;
1254         unode = VTON(vp);
1255
1256         /*
1257          * Destroy the vm object and flush associated pages.
1258          */
1259         vnode_destroy_vobject(vp);
1260
1261         if (unode != NULL) {
1262                 vfs_hash_remove(vp);
1263
1264                 if (unode->fentry != NULL)
1265                         free(unode->fentry, M_UDFFENTRY);
1266                 uma_zfree(udf_zone_node, unode);
1267                 vp->v_data = NULL;
1268         }
1269
1270         return (0);
1271 }
1272
1273 static int
1274 udf_vptofh(struct vop_vptofh_args *a)
1275 {
1276         struct udf_node *node;
1277         struct ifid *ifhp;
1278
1279         node = VTON(a->a_vp);
1280         ifhp = (struct ifid *)a->a_fhp;
1281         ifhp->ifid_len = sizeof(struct ifid);
1282         ifhp->ifid_ino = node->hash_id;
1283
1284         return (0);
1285 }
1286
1287 /*
1288  * Read the block and then set the data pointer to correspond with the
1289  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
1290  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
1291  * whole extent.
1292  *
1293  * Note that *bp may be assigned error or not.
1294  *
1295  */
1296 static int
1297 udf_readatoffset(struct udf_node *node, int *size, off_t offset,
1298     struct buf **bp, uint8_t **data)
1299 {
1300         struct udf_mnt *udfmp = node->udfmp;
1301         struct vnode *vp = node->i_vnode;
1302         struct file_entry *fentry;
1303         struct buf *bp1;
1304         uint32_t max_size;
1305         daddr_t sector;
1306         off_t off;
1307         int adj_size;
1308         int error;
1309
1310         /*
1311          * This call is made *not* only to detect UDF_INVALID_BMAP case,
1312          * max_size is used as an ad-hoc read-ahead hint for "normal" case.
1313          */
1314         error = udf_bmap_internal(node, offset, &sector, &max_size);
1315         if (error == UDF_INVALID_BMAP) {
1316                 /*
1317                  * This error means that the file *data* is stored in the
1318                  * allocation descriptor field of the file entry.
1319                  */
1320                 fentry = node->fentry;
1321                 *data = &fentry->data[le32toh(fentry->l_ea)];
1322                 *size = le32toh(fentry->l_ad);
1323                 if (offset >= *size)
1324                         *size = 0;
1325                 else {
1326                         *data += offset;
1327                         *size -= offset;
1328                 }
1329                 return (0);
1330         } else if (error != 0) {
1331                 return (error);
1332         }
1333
1334         /* Adjust the size so that it is within range */
1335         if (*size == 0 || *size > max_size)
1336                 *size = max_size;
1337
1338         /*
1339          * Because we will read starting at block boundary, we need to adjust
1340          * how much we need to read so that all promised data is in.
1341          * Also, we can't promise to read more than MAXBSIZE bytes starting
1342          * from block boundary, so adjust what we promise too.
1343          */
1344         off = blkoff(udfmp, offset);
1345         *size = min(*size, MAXBSIZE - off);
1346         adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask;
1347         *bp = NULL;
1348         if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) {
1349                 printf("warning: udf_readlblks returned error %d\n", error);
1350                 /* note: *bp may be non-NULL */
1351                 return (error);
1352         }
1353
1354         bp1 = *bp;
1355         *data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
1356         return (0);
1357 }
1358
1359 /*
1360  * Translate a file offset into a logical block and then into a physical
1361  * block.
1362  * max_size - maximum number of bytes that can be read starting from given
1363  * offset, rather than beginning of calculated sector number
1364  */
1365 static int
1366 udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
1367     uint32_t *max_size)
1368 {
1369         struct udf_mnt *udfmp;
1370         struct file_entry *fentry;
1371         void *icb;
1372         struct icb_tag *tag;
1373         uint32_t icblen = 0;
1374         daddr_t lsector;
1375         int ad_offset, ad_num = 0;
1376         int i, p_offset;
1377
1378         udfmp = node->udfmp;
1379         fentry = node->fentry;
1380         tag = &fentry->icbtag;
1381
1382         switch (le16toh(tag->strat_type)) {
1383         case 4:
1384                 break;
1385
1386         case 4096:
1387                 printf("Cannot deal with strategy4096 yet!\n");
1388                 return (ENODEV);
1389
1390         default:
1391                 printf("Unknown strategy type %d\n", tag->strat_type);
1392                 return (ENODEV);
1393         }
1394
1395         switch (le16toh(tag->flags) & 0x7) {
1396         case 0:
1397                 /*
1398                  * The allocation descriptor field is filled with short_ad's.
1399                  * If the offset is beyond the current extent, look for the
1400                  * next extent.
1401                  */
1402                 do {
1403                         offset -= icblen;
1404                         ad_offset = sizeof(struct short_ad) * ad_num;
1405                         if (ad_offset > le32toh(fentry->l_ad)) {
1406                                 printf("File offset out of bounds\n");
1407                                 return (EINVAL);
1408                         }
1409                         icb = GETICB(short_ad, fentry,
1410                             le32toh(fentry->l_ea) + ad_offset);
1411                         icblen = GETICBLEN(short_ad, icb);
1412                         ad_num++;
1413                 } while(offset >= icblen);
1414
1415                 lsector = (offset  >> udfmp->bshift) +
1416                     le32toh(((struct short_ad *)(icb))->pos);
1417
1418                 *max_size = icblen - offset;
1419
1420                 break;
1421         case 1:
1422                 /*
1423                  * The allocation descriptor field is filled with long_ad's
1424                  * If the offset is beyond the current extent, look for the
1425                  * next extent.
1426                  */
1427                 do {
1428                         offset -= icblen;
1429                         ad_offset = sizeof(struct long_ad) * ad_num;
1430                         if (ad_offset > le32toh(fentry->l_ad)) {
1431                                 printf("File offset out of bounds\n");
1432                                 return (EINVAL);
1433                         }
1434                         icb = GETICB(long_ad, fentry,
1435                             le32toh(fentry->l_ea) + ad_offset);
1436                         icblen = GETICBLEN(long_ad, icb);
1437                         ad_num++;
1438                 } while(offset >= icblen);
1439
1440                 lsector = (offset >> udfmp->bshift) +
1441                     le32toh(((struct long_ad *)(icb))->loc.lb_num);
1442
1443                 *max_size = icblen - offset;
1444
1445                 break;
1446         case 3:
1447                 /*
1448                  * This type means that the file *data* is stored in the
1449                  * allocation descriptor field of the file entry.
1450                  */
1451                 *max_size = 0;
1452                 *sector = node->hash_id + udfmp->part_start;
1453
1454                 return (UDF_INVALID_BMAP);
1455         case 2:
1456                 /* DirectCD does not use extended_ad's */
1457         default:
1458                 printf("Unsupported allocation descriptor %d\n",
1459                        tag->flags & 0x7);
1460                 return (ENODEV);
1461         }
1462
1463         *sector = lsector + udfmp->part_start;
1464
1465         /*
1466          * Check the sparing table.  Each entry represents the beginning of
1467          * a packet.
1468          */
1469         if (udfmp->s_table != NULL) {
1470                 for (i = 0; i< udfmp->s_table_entries; i++) {
1471                         p_offset =
1472                             lsector - le32toh(udfmp->s_table->entries[i].org);
1473                         if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
1474                                 *sector =
1475                                    le32toh(udfmp->s_table->entries[i].map) +
1476                                     p_offset;
1477                                 break;
1478                         }
1479                 }
1480         }
1481
1482         return (0);
1483 }