The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/udf/udf_vnops.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: e2c774f694df065b7cbe5c71455c9bc0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.