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 MALLOC_DEFINE(M_UDFFID, "udf_fid", "UDF FileId structure");
  111 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         mode_t a_mode, mode;
  158 
  159         vp = a->a_vp;
  160         node = VTON(vp);
  161         a_mode = a->a_mode;
  162 
  163         if (a_mode & 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             a_mode, 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_LINK_MAX:
  387                 *a->a_retval = 65535;
  388                 return (0);
  389         case _PC_NAME_MAX:
  390                 *a->a_retval = NAME_MAX;
  391                 return (0);
  392         case _PC_PATH_MAX:
  393                 *a->a_retval = PATH_MAX;
  394                 return (0);
  395         case _PC_NO_TRUNC:
  396                 *a->a_retval = 1;
  397                 return (0);
  398         default:
  399                 return (EINVAL);
  400         }
  401 }
  402 
  403 static int
  404 udf_print(struct vop_print_args *ap)
  405 {
  406         struct vnode *vp = ap->a_vp;
  407         struct udf_node *node = VTON(vp);
  408 
  409         printf("    ino %lu, on dev %s", (u_long)node->hash_id,
  410             devtoname(node->udfmp->im_dev));
  411         if (vp->v_type == VFIFO)
  412                 fifo_printinfo(vp);
  413         printf("\n");
  414         return (0);
  415 }
  416 
  417 #define lblkno(udfmp, loc)      ((loc) >> (udfmp)->bshift)
  418 #define blkoff(udfmp, loc)      ((loc) & (udfmp)->bmask)
  419 #define lblktosize(udfmp, blk)  ((blk) << (udfmp)->bshift)
  420 
  421 static inline int
  422 is_data_in_fentry(const struct udf_node *node)
  423 {
  424         const struct file_entry *fentry = node->fentry;
  425 
  426         return ((le16toh(fentry->icbtag.flags) & 0x7) == 3);
  427 }
  428 
  429 static int
  430 udf_read(struct vop_read_args *ap)
  431 {
  432         struct vnode *vp = ap->a_vp;
  433         struct uio *uio = ap->a_uio;
  434         struct udf_node *node = VTON(vp);
  435         struct udf_mnt *udfmp;
  436         struct file_entry *fentry;
  437         struct buf *bp;
  438         uint8_t *data;
  439         daddr_t lbn, rablock;
  440         off_t diff, fsize;
  441         int error = 0;
  442         long size, n, on;
  443 
  444         if (uio->uio_resid == 0)
  445                 return (0);
  446         if (uio->uio_offset < 0)
  447                 return (EINVAL);
  448 
  449         if (is_data_in_fentry(node)) {
  450                 fentry = node->fentry;
  451                 data = &fentry->data[le32toh(fentry->l_ea)];
  452                 fsize = le32toh(fentry->l_ad);
  453 
  454                 n = uio->uio_resid;
  455                 diff = fsize - uio->uio_offset;
  456                 if (diff <= 0)
  457                         return (0);
  458                 if (diff < n)
  459                         n = diff;
  460                 error = uiomove(data + uio->uio_offset, (int)n, uio);
  461                 return (error);
  462         }
  463 
  464         fsize = le64toh(node->fentry->inf_len);
  465         udfmp = node->udfmp;
  466         do {
  467                 lbn = lblkno(udfmp, uio->uio_offset);
  468                 on = blkoff(udfmp, uio->uio_offset);
  469                 n = min((u_int)(udfmp->bsize - on),
  470                         uio->uio_resid);
  471                 diff = fsize - uio->uio_offset;
  472                 if (diff <= 0)
  473                         return (0);
  474                 if (diff < n)
  475                         n = diff;
  476                 size = udfmp->bsize;
  477                 rablock = lbn + 1;
  478                 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
  479                         if (lblktosize(udfmp, rablock) < fsize) {
  480                                 error = cluster_read(vp, fsize, lbn, size, NOCRED,
  481                                         uio->uio_resid, (ap->a_ioflag >> 16), &bp);
  482                         } else {
  483                                 error = bread(vp, lbn, size, NOCRED, &bp);
  484                         }
  485                 } else {
  486                         error = bread(vp, lbn, size, NOCRED, &bp);
  487                 }
  488                 n = min(n, size - bp->b_resid);
  489                 if (error) {
  490                         brelse(bp);
  491                         return (error);
  492                 }
  493 
  494                 error = uiomove(bp->b_data + on, (int)n, uio);
  495                 brelse(bp);
  496         } while (error == 0 && uio->uio_resid > 0 && n != 0);
  497         return (error);
  498 }
  499 
  500 /*
  501  * Call the OSTA routines to translate the name from a CS0 dstring to a
  502  * 16-bit Unicode String.  Hooks need to be placed in here to translate from
  503  * Unicode to the encoding that the kernel/user expects.  Return the length
  504  * of the translated string.
  505  */
  506 static int
  507 udf_transname(char *cs0string, char *destname, int len, struct udf_mnt *udfmp)
  508 {
  509         unicode_t *transname;
  510         char *unibuf, *unip;
  511         int i, destlen;
  512         ssize_t unilen = 0;
  513         size_t destleft = MAXNAMLEN;
  514 
  515         /* Convert 16-bit Unicode to destname */
  516         if (udfmp->im_flags & UDFMNT_KICONV && udf_iconv) {
  517                 /* allocate a buffer big enough to hold an 8->16 bit expansion */
  518                 unibuf = uma_zalloc(udf_zone_trans, M_WAITOK);
  519                 unip = unibuf;
  520                 if ((unilen = (ssize_t)udf_UncompressUnicodeByte(len, cs0string, unibuf)) == -1) {
  521                         printf("udf: Unicode translation failed\n");
  522                         uma_zfree(udf_zone_trans, unibuf);
  523                         return 0;
  524                 }
  525 
  526                 while (unilen > 0 && destleft > 0) {
  527                         udf_iconv->conv(udfmp->im_d2l, (const char **)&unibuf,
  528                                 (size_t *)&unilen, (char **)&destname, &destleft);
  529                         /* Unconverted character found */
  530                         if (unilen > 0 && destleft > 0) {
  531                                 *destname++ = '?';
  532                                 destleft--;
  533                                 unibuf += 2;
  534                                 unilen -= 2;
  535                         }
  536                 }
  537                 uma_zfree(udf_zone_trans, unip);
  538                 *destname = '\0';
  539                 destlen = MAXNAMLEN - (int)destleft;
  540         } else {
  541                 /* allocate a buffer big enough to hold an 8->16 bit expansion */
  542                 transname = uma_zalloc(udf_zone_trans, M_WAITOK);
  543 
  544                 if ((unilen = (ssize_t)udf_UncompressUnicode(len, cs0string, transname)) == -1) {
  545                         printf("udf: Unicode translation failed\n");
  546                         uma_zfree(udf_zone_trans, transname);
  547                         return 0;
  548                 }
  549 
  550                 for (i = 0; i < unilen ; i++) {
  551                         if (transname[i] & 0xff00) {
  552                                 destname[i] = '.';      /* Fudge the 16bit chars */
  553                         } else {
  554                                 destname[i] = transname[i] & 0xff;
  555                         }
  556                 }
  557                 uma_zfree(udf_zone_trans, transname);
  558                 destname[unilen] = 0;
  559                 destlen = (int)unilen;
  560         }
  561 
  562         return (destlen);
  563 }
  564 
  565 /*
  566  * Compare a CS0 dstring with a name passed in from the VFS layer.  Return
  567  * 0 on a successful match, nonzero otherwise.  Unicode work may need to be done
  568  * here also.
  569  */
  570 static int
  571 udf_cmpname(char *cs0string, char *cmpname, int cs0len, int cmplen, struct udf_mnt *udfmp)
  572 {
  573         char *transname;
  574         int error = 0;
  575 
  576         /* This is overkill, but not worth creating a new zone */
  577         transname = uma_zalloc(udf_zone_trans, M_WAITOK);
  578 
  579         cs0len = udf_transname(cs0string, transname, cs0len, udfmp);
  580 
  581         /* Easy check.  If they aren't the same length, they aren't equal */
  582         if ((cs0len == 0) || (cs0len != cmplen))
  583                 error = -1;
  584         else
  585                 error = bcmp(transname, cmpname, cmplen);
  586 
  587         uma_zfree(udf_zone_trans, transname);
  588         return (error);
  589 }
  590 
  591 struct udf_uiodir {
  592         struct dirent *dirent;
  593         u_long *cookies;
  594         int ncookies;
  595         int acookies;
  596         int eofflag;
  597 };
  598 
  599 static int
  600 udf_uiodir(struct udf_uiodir *uiodir, int de_size, struct uio *uio, long cookie)
  601 {
  602         if (uiodir->cookies != NULL) {
  603                 if (++uiodir->acookies > uiodir->ncookies) {
  604                         uiodir->eofflag = 0;
  605                         return (-1);
  606                 }
  607                 *uiodir->cookies++ = cookie;
  608         }
  609 
  610         if (uio->uio_resid < de_size) {
  611                 uiodir->eofflag = 0;
  612                 return (-1);
  613         }
  614 
  615         return (uiomove(uiodir->dirent, de_size, uio));
  616 }
  617 
  618 static struct udf_dirstream *
  619 udf_opendir(struct udf_node *node, int offset, int fsize, struct udf_mnt *udfmp)
  620 {
  621         struct udf_dirstream *ds;
  622 
  623         ds = uma_zalloc(udf_zone_ds, M_WAITOK | M_ZERO);
  624 
  625         ds->node = node;
  626         ds->offset = offset;
  627         ds->udfmp = udfmp;
  628         ds->fsize = fsize;
  629 
  630         return (ds);
  631 }
  632 
  633 static struct fileid_desc *
  634 udf_getfid(struct udf_dirstream *ds)
  635 {
  636         struct fileid_desc *fid;
  637         int error, frag_size = 0, total_fid_size;
  638 
  639         /* End of directory? */
  640         if (ds->offset + ds->off >= ds->fsize) {
  641                 ds->error = 0;
  642                 return (NULL);
  643         }
  644 
  645         /* Grab the first extent of the directory */
  646         if (ds->off == 0) {
  647                 ds->size = 0;
  648                 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
  649                     &ds->bp, &ds->data);
  650                 if (error) {
  651                         ds->error = error;
  652                         if (ds->bp != NULL)
  653                                 brelse(ds->bp);
  654                         return (NULL);
  655                 }
  656         }
  657 
  658         /*
  659          * Clean up from a previous fragmented FID.
  660          * XXX Is this the right place for this?
  661          */
  662         if (ds->fid_fragment && ds->buf != NULL) {
  663                 ds->fid_fragment = 0;
  664                 FREE(ds->buf, M_UDFFID);
  665         }
  666 
  667         fid = (struct fileid_desc*)&ds->data[ds->off];
  668 
  669         /*
  670          * Check to see if the fid is fragmented. The first test
  671          * ensures that we don't wander off the end of the buffer
  672          * looking for the l_iu and l_fi fields.
  673          */
  674         if (ds->off + UDF_FID_SIZE > ds->size ||
  675             ds->off + le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE > ds->size){
  676 
  677                 /* Copy what we have of the fid into a buffer */
  678                 frag_size = ds->size - ds->off;
  679                 if (frag_size >= ds->udfmp->bsize) {
  680                         printf("udf: invalid FID fragment\n");
  681                         ds->error = EINVAL;
  682                         return (NULL);
  683                 }
  684 
  685                 /*
  686                  * File ID descriptors can only be at most one
  687                  * logical sector in size.
  688                  */
  689                 MALLOC(ds->buf, uint8_t*, ds->udfmp->bsize, M_UDFFID,
  690                      M_WAITOK | M_ZERO);
  691                 bcopy(fid, ds->buf, frag_size);
  692 
  693                 /* Reduce all of the casting magic */
  694                 fid = (struct fileid_desc*)ds->buf;
  695 
  696                 if (ds->bp != NULL)
  697                         brelse(ds->bp);
  698 
  699                 /* Fetch the next allocation */
  700                 ds->offset += ds->size;
  701                 ds->size = 0;
  702                 error = udf_readatoffset(ds->node, &ds->size, ds->offset,
  703                     &ds->bp, &ds->data);
  704                 if (error) {
  705                         ds->error = error;
  706                         return (NULL);
  707                 }
  708 
  709                 /*
  710                  * If the fragment was so small that we didn't get
  711                  * the l_iu and l_fi fields, copy those in.
  712                  */
  713                 if (frag_size < UDF_FID_SIZE)
  714                         bcopy(ds->data, &ds->buf[frag_size],
  715                             UDF_FID_SIZE - frag_size);
  716 
  717                 /*
  718                  * Now that we have enough of the fid to work with,
  719                  * copy in the rest of the fid from the new
  720                  * allocation.
  721                  */
  722                 total_fid_size = UDF_FID_SIZE + le16toh(fid->l_iu) + fid->l_fi;
  723                 if (total_fid_size > ds->udfmp->bsize) {
  724                         printf("udf: invalid FID\n");
  725                         ds->error = EIO;
  726                         return (NULL);
  727                 }
  728                 bcopy(ds->data, &ds->buf[frag_size],
  729                     total_fid_size - frag_size);
  730 
  731                 ds->fid_fragment = 1;
  732         } else {
  733                 total_fid_size = le16toh(fid->l_iu) + fid->l_fi + UDF_FID_SIZE;
  734         }
  735 
  736         /*
  737          * Update the offset. Align on a 4 byte boundary because the
  738          * UDF spec says so.
  739          */
  740         ds->this_off = ds->offset + ds->off;
  741         if (!ds->fid_fragment) {
  742                 ds->off += (total_fid_size + 3) & ~0x03;
  743         } else {
  744                 ds->off = (total_fid_size - frag_size + 3) & ~0x03;
  745         }
  746 
  747         return (fid);
  748 }
  749 
  750 static void
  751 udf_closedir(struct udf_dirstream *ds)
  752 {
  753 
  754         if (ds->bp != NULL)
  755                 brelse(ds->bp);
  756 
  757         if (ds->fid_fragment && ds->buf != NULL)
  758                 FREE(ds->buf, M_UDFFID);
  759 
  760         uma_zfree(udf_zone_ds, ds);
  761 }
  762 
  763 static int
  764 udf_readdir(struct vop_readdir_args *a)
  765 {
  766         struct vnode *vp;
  767         struct uio *uio;
  768         struct dirent dir;
  769         struct udf_node *node;
  770         struct udf_mnt *udfmp;
  771         struct fileid_desc *fid;
  772         struct udf_uiodir uiodir;
  773         struct udf_dirstream *ds;
  774         u_long *cookies = NULL;
  775         int ncookies;
  776         int error = 0;
  777 
  778         vp = a->a_vp;
  779         uio = a->a_uio;
  780         node = VTON(vp);
  781         udfmp = node->udfmp;
  782         uiodir.eofflag = 1;
  783 
  784         if (a->a_ncookies != NULL) {
  785                 /*
  786                  * Guess how many entries are needed.  If we run out, this
  787                  * function will be called again and thing will pick up were
  788                  * it left off.
  789                  */
  790                 ncookies = uio->uio_resid / 8;
  791                 MALLOC(cookies, u_long *, sizeof(u_long) * ncookies,
  792                     M_TEMP, M_WAITOK);
  793                 if (cookies == NULL)
  794                         return (ENOMEM);
  795                 uiodir.ncookies = ncookies;
  796                 uiodir.cookies = cookies;
  797                 uiodir.acookies = 0;
  798         } else {
  799                 uiodir.cookies = NULL;
  800         }
  801 
  802         /*
  803          * Iterate through the file id descriptors.  Give the parent dir
  804          * entry special attention.
  805          */
  806         ds = udf_opendir(node, uio->uio_offset, le64toh(node->fentry->inf_len),
  807             node->udfmp);
  808 
  809         while ((fid = udf_getfid(ds)) != NULL) {
  810 
  811                 /* XXX Should we return an error on a bad fid? */
  812                 if (udf_checktag(&fid->tag, TAGID_FID)) {
  813                         printf("Invalid FID tag\n");
  814                         hexdump(fid, UDF_FID_SIZE, NULL, 0);
  815                         error = EIO;
  816                         break;
  817                 }
  818 
  819                 /* Is this a deleted file? */
  820                 if (fid->file_char & UDF_FILE_CHAR_DEL)
  821                         continue;
  822 
  823                 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
  824                         /* Do up the '.' and '..' entries.  Dummy values are
  825                          * used for the cookies since the offset here is
  826                          * usually zero, and NFS doesn't like that value
  827                          */
  828                         dir.d_fileno = node->hash_id;
  829                         dir.d_type = DT_DIR;
  830                         dir.d_name[0] = '.';
  831                         dir.d_name[1] = '\0';
  832                         dir.d_namlen = 1;
  833                         dir.d_reclen = GENERIC_DIRSIZ(&dir);
  834                         uiodir.dirent = &dir;
  835                         error = udf_uiodir(&uiodir, dir.d_reclen, uio, 1);
  836                         if (error)
  837                                 break;
  838 
  839                         dir.d_fileno = udf_getid(&fid->icb);
  840                         dir.d_type = DT_DIR;
  841                         dir.d_name[0] = '.';
  842                         dir.d_name[1] = '.';
  843                         dir.d_name[2] = '\0';
  844                         dir.d_namlen = 2;
  845                         dir.d_reclen = GENERIC_DIRSIZ(&dir);
  846                         uiodir.dirent = &dir;
  847                         error = udf_uiodir(&uiodir, dir.d_reclen, uio, 2);
  848                 } else {
  849                         dir.d_namlen = udf_transname(&fid->data[fid->l_iu],
  850                             &dir.d_name[0], fid->l_fi, udfmp);
  851                         dir.d_fileno = udf_getid(&fid->icb);
  852                         dir.d_type = (fid->file_char & UDF_FILE_CHAR_DIR) ?
  853                             DT_DIR : DT_UNKNOWN;
  854                         dir.d_reclen = GENERIC_DIRSIZ(&dir);
  855                         uiodir.dirent = &dir;
  856                         error = udf_uiodir(&uiodir, dir.d_reclen, uio,
  857                             ds->this_off);
  858                 }
  859                 if (error)
  860                         break;
  861                 uio->uio_offset = ds->offset + ds->off;
  862         }
  863 
  864         /* tell the calling layer whether we need to be called again */
  865         *a->a_eofflag = uiodir.eofflag;
  866 
  867         if (error < 0)
  868                 error = 0;
  869         if (!error)
  870                 error = ds->error;
  871 
  872         udf_closedir(ds);
  873 
  874         if (a->a_ncookies != NULL) {
  875                 if (error)
  876                         FREE(cookies, M_TEMP);
  877                 else {
  878                         *a->a_ncookies = uiodir.acookies;
  879                         *a->a_cookies = cookies;
  880                 }
  881         }
  882 
  883         return (error);
  884 }
  885 
  886 static int
  887 udf_readlink(struct vop_readlink_args *ap)
  888 {
  889         struct path_component *pc, *end;
  890         struct vnode *vp;
  891         struct uio uio;
  892         struct iovec iov[1];
  893         struct udf_node *node;
  894         void *buf;
  895         char *cp;
  896         int error, len, root;
  897 
  898         /*
  899          * A symbolic link in UDF is a list of variable-length path
  900          * component structures.  We build a pathname in the caller's
  901          * uio by traversing this list.
  902          */
  903         vp = ap->a_vp;
  904         node = VTON(vp);
  905         len = le64toh(node->fentry->inf_len);
  906         buf = malloc(len, M_DEVBUF, M_WAITOK);
  907         iov[0].iov_len = len;
  908         iov[0].iov_base = buf;
  909         uio.uio_iov = iov;
  910         uio.uio_iovcnt = 1;
  911         uio.uio_offset = 0;
  912         uio.uio_resid = iov[0].iov_len;
  913         uio.uio_segflg = UIO_SYSSPACE;
  914         uio.uio_rw = UIO_READ;
  915         uio.uio_td = curthread;
  916         error = VOP_READ(vp, &uio, 0, ap->a_cred);
  917         if (error)
  918                 goto error;
  919 
  920         pc = buf;
  921         end = (void *)((char *)buf + len);
  922         root = 0;
  923         while (pc < end) {
  924                 switch (pc->type) {
  925                 case UDF_PATH_ROOT:
  926                         /* Only allow this at the beginning of a path. */
  927                         if ((void *)pc != buf) {
  928                                 error = EINVAL;
  929                                 goto error;
  930                         }
  931                         cp = "/";
  932                         len = 1;
  933                         root = 1;
  934                         break;
  935                 case UDF_PATH_DOT:
  936                         cp = ".";
  937                         len = 1;
  938                         break;
  939                 case UDF_PATH_DOTDOT:
  940                         cp = "..";
  941                         len = 2;
  942                         break;
  943                 case UDF_PATH_PATH:
  944                         if (pc->length == 0) {
  945                                 error = EINVAL;
  946                                 goto error;
  947                         }
  948                         /*
  949                          * XXX: We only support CS8 which appears to map
  950                          * to ASCII directly.
  951                          */
  952                         switch (pc->identifier[0]) {
  953                         case 8:
  954                                 cp = pc->identifier + 1;
  955                                 len = pc->length - 1;
  956                                 break;
  957                         default:
  958                                 error = EOPNOTSUPP;
  959                                 goto error;
  960                         }
  961                         break;
  962                 default:
  963                         error = EINVAL;
  964                         goto error;
  965                 }
  966 
  967                 /*
  968                  * If this is not the first component, insert a path
  969                  * separator.
  970                  */
  971                 if (pc != buf) {
  972                         /* If we started with root we already have a "/". */
  973                         if (root)
  974                                 goto skipslash;
  975                         root = 0;
  976                         if (ap->a_uio->uio_resid < 1) {
  977                                 error = ENAMETOOLONG;
  978                                 goto error;
  979                         }
  980                         error = uiomove("/", 1, ap->a_uio);
  981                         if (error)
  982                                 break;
  983                 }
  984         skipslash:
  985 
  986                 /* Append string at 'cp' of length 'len' to our path. */
  987                 if (len > ap->a_uio->uio_resid) {
  988                         error = ENAMETOOLONG;
  989                         goto error;
  990                 }
  991                 error = uiomove(cp, len, ap->a_uio);
  992                 if (error)
  993                         break;
  994 
  995                 /* Advance to next component. */
  996                 pc = (void *)((char *)pc + 4 + pc->length);
  997         }
  998 error:
  999         free(buf, M_DEVBUF);
 1000         return (error);
 1001 }
 1002 
 1003 static int
 1004 udf_strategy(struct vop_strategy_args *a)
 1005 {
 1006         struct buf *bp;
 1007         struct vnode *vp;
 1008         struct udf_node *node;
 1009         struct bufobj *bo;
 1010         off_t offset;
 1011         uint32_t maxsize;
 1012         daddr_t sector;
 1013         int error;
 1014 
 1015         bp = a->a_bp;
 1016         vp = a->a_vp;
 1017         node = VTON(vp);
 1018 
 1019         if (bp->b_blkno == bp->b_lblkno) {
 1020                 offset = lblktosize(node->udfmp, bp->b_lblkno);
 1021                 error = udf_bmap_internal(node, offset, &sector, &maxsize);
 1022                 if (error) {
 1023                         clrbuf(bp);
 1024                         bp->b_blkno = -1;
 1025                         bufdone(bp);
 1026                         return (0);
 1027                 }
 1028                 /* bmap gives sector numbers, bio works with device blocks */
 1029                 bp->b_blkno = sector << (node->udfmp->bshift - DEV_BSHIFT);
 1030         }
 1031         bo = node->udfmp->im_bo;
 1032         bp->b_iooffset = dbtob(bp->b_blkno);
 1033         BO_STRATEGY(bo, bp);
 1034         return (0);
 1035 }
 1036 
 1037 static int
 1038 udf_bmap(struct vop_bmap_args *a)
 1039 {
 1040         struct udf_node *node;
 1041         uint32_t max_size;
 1042         daddr_t lsector;
 1043         int nblk;
 1044         int error;
 1045 
 1046         node = VTON(a->a_vp);
 1047 
 1048         if (a->a_bop != NULL)
 1049                 *a->a_bop = &node->udfmp->im_devvp->v_bufobj;
 1050         if (a->a_bnp == NULL)
 1051                 return (0);
 1052         if (a->a_runb)
 1053                 *a->a_runb = 0;
 1054 
 1055         /*
 1056          * UDF_INVALID_BMAP means data embedded into fentry, this is an internal
 1057          * error that should not be propagated to calling code.
 1058          * Most obvious mapping for this error is EOPNOTSUPP as we can not truly
 1059          * translate block numbers in this case.
 1060          * Incidentally, this return code will make vnode pager to use VOP_READ
 1061          * to get data for mmap-ed pages and udf_read knows how to do the right
 1062          * thing for this kind of files.
 1063          */
 1064         error = udf_bmap_internal(node, a->a_bn << node->udfmp->bshift,
 1065             &lsector, &max_size);
 1066         if (error == UDF_INVALID_BMAP)
 1067                 return (EOPNOTSUPP);
 1068         if (error)
 1069                 return (error);
 1070 
 1071         /* Translate logical to physical sector number */
 1072         *a->a_bnp = lsector << (node->udfmp->bshift - DEV_BSHIFT);
 1073 
 1074         /*
 1075          * Determine maximum number of readahead blocks following the
 1076          * requested block.
 1077          */
 1078         if (a->a_runp) {
 1079                 nblk = (max_size >> node->udfmp->bshift) - 1;
 1080                 if (nblk <= 0)
 1081                         *a->a_runp = 0;
 1082                 else if (nblk >= (MAXBSIZE >> node->udfmp->bshift))
 1083                         *a->a_runp = (MAXBSIZE >> node->udfmp->bshift) - 1;
 1084                 else
 1085                         *a->a_runp = nblk;
 1086         }
 1087 
 1088         if (a->a_runb) {
 1089                 *a->a_runb = 0;
 1090         }
 1091 
 1092         return (0);
 1093 }
 1094 
 1095 /*
 1096  * The all powerful VOP_LOOKUP().
 1097  */
 1098 static int
 1099 udf_lookup(struct vop_cachedlookup_args *a)
 1100 {
 1101         struct vnode *dvp;
 1102         struct vnode *tdp = NULL;
 1103         struct vnode **vpp = a->a_vpp;
 1104         struct udf_node *node;
 1105         struct udf_mnt *udfmp;
 1106         struct fileid_desc *fid = NULL;
 1107         struct udf_dirstream *ds;
 1108         struct thread *td;
 1109         u_long nameiop;
 1110         u_long flags;
 1111         char *nameptr;
 1112         long namelen;
 1113         ino_t id = 0;
 1114         int offset, error = 0;
 1115         int fsize, lkflags, ltype, numdirpasses;
 1116 
 1117         dvp = a->a_dvp;
 1118         node = VTON(dvp);
 1119         udfmp = node->udfmp;
 1120         nameiop = a->a_cnp->cn_nameiop;
 1121         flags = a->a_cnp->cn_flags;
 1122         lkflags = a->a_cnp->cn_lkflags;
 1123         nameptr = a->a_cnp->cn_nameptr;
 1124         namelen = a->a_cnp->cn_namelen;
 1125         fsize = le64toh(node->fentry->inf_len);
 1126         td = a->a_cnp->cn_thread;
 1127 
 1128         /*
 1129          * If this is a LOOKUP and we've already partially searched through
 1130          * the directory, pick up where we left off and flag that the
 1131          * directory may need to be searched twice.  For a full description,
 1132          * see /sys/fs/cd9660/cd9660_lookup.c:cd9660_lookup()
 1133          */
 1134         if (nameiop != LOOKUP || node->diroff == 0 || node->diroff > fsize) {
 1135                 offset = 0;
 1136                 numdirpasses = 1;
 1137         } else {
 1138                 offset = node->diroff;
 1139                 numdirpasses = 2;
 1140                 nchstats.ncs_2passes++;
 1141         }
 1142 
 1143 lookloop:
 1144         ds = udf_opendir(node, offset, fsize, udfmp);
 1145 
 1146         while ((fid = udf_getfid(ds)) != NULL) {
 1147 
 1148                 /* XXX Should we return an error on a bad fid? */
 1149                 if (udf_checktag(&fid->tag, TAGID_FID)) {
 1150                         printf("udf_lookup: Invalid tag\n");
 1151                         error = EIO;
 1152                         break;
 1153                 }
 1154 
 1155                 /* Is this a deleted file? */
 1156                 if (fid->file_char & UDF_FILE_CHAR_DEL)
 1157                         continue;
 1158 
 1159                 if ((fid->l_fi == 0) && (fid->file_char & UDF_FILE_CHAR_PAR)) {
 1160                         if (flags & ISDOTDOT) {
 1161                                 id = udf_getid(&fid->icb);
 1162                                 break;
 1163                         }
 1164                 } else {
 1165                         if (!(udf_cmpname(&fid->data[fid->l_iu],
 1166                             nameptr, fid->l_fi, namelen, udfmp))) {
 1167                                 id = udf_getid(&fid->icb);
 1168                                 break;
 1169                         }
 1170                 }
 1171         }
 1172 
 1173         if (!error)
 1174                 error = ds->error;
 1175 
 1176         /* XXX Bail out here? */
 1177         if (error) {
 1178                 udf_closedir(ds);
 1179                 return (error);
 1180         }
 1181 
 1182         /* Did we have a match? */
 1183         if (id) {
 1184                 /*
 1185                  * Remember where this entry was if it's the final
 1186                  * component.
 1187                  */
 1188                 if ((flags & ISLASTCN) && nameiop == LOOKUP)
 1189                         node->diroff = ds->offset + ds->off;
 1190                 if (numdirpasses == 2)
 1191                         nchstats.ncs_pass2++;
 1192                 udf_closedir(ds);
 1193 
 1194                 if (flags & ISDOTDOT) {
 1195                         error = vn_vget_ino(dvp, id, lkflags, &tdp);
 1196                 } else if (node->hash_id == id) {
 1197                         VREF(dvp);      /* we want ourself, ie "." */
 1198                         /*
 1199                          * When we lookup "." we still can be asked to lock it
 1200                          * differently.
 1201                          */
 1202                         ltype = lkflags & LK_TYPE_MASK;
 1203                         if (ltype != VOP_ISLOCKED(dvp, td)) {
 1204                                 if (ltype == LK_EXCLUSIVE)
 1205                                         vn_lock(dvp, LK_UPGRADE | LK_RETRY, td);
 1206                                 else /* if (ltype == LK_SHARED) */
 1207                                         vn_lock(dvp, LK_DOWNGRADE | LK_RETRY,
 1208                                             td);
 1209                         }
 1210                         tdp = dvp;
 1211                 } else
 1212                         error = udf_vget(udfmp->im_mountp, id, lkflags, &tdp);
 1213                 if (!error) {
 1214                         *vpp = tdp;
 1215                         /* Put this entry in the cache */
 1216                         if (flags & MAKEENTRY)
 1217                                 cache_enter(dvp, *vpp, a->a_cnp);
 1218                 }
 1219         } else {
 1220                 /* Name wasn't found on this pass.  Do another pass? */
 1221                 if (numdirpasses == 2) {
 1222                         numdirpasses--;
 1223                         offset = 0;
 1224                         udf_closedir(ds);
 1225                         goto lookloop;
 1226                 }
 1227                 udf_closedir(ds);
 1228 
 1229                 /* Enter name into cache as non-existant */
 1230                 if (flags & MAKEENTRY)
 1231                         cache_enter(dvp, *vpp, a->a_cnp);
 1232 
 1233                 if ((flags & ISLASTCN) &&
 1234                     (nameiop == CREATE || nameiop == RENAME)) {
 1235                         error = EROFS;
 1236                 } else {
 1237                         error = ENOENT;
 1238                 }
 1239         }
 1240 
 1241         return (error);
 1242 }
 1243 
 1244 static int
 1245 udf_reclaim(struct vop_reclaim_args *a)
 1246 {
 1247         struct vnode *vp;
 1248         struct udf_node *unode;
 1249 
 1250         vp = a->a_vp;
 1251         unode = VTON(vp);
 1252 
 1253         /*
 1254          * Destroy the vm object and flush associated pages.
 1255          */
 1256         vnode_destroy_vobject(vp);
 1257 
 1258         if (unode != NULL) {
 1259                 vfs_hash_remove(vp);
 1260 
 1261                 if (unode->fentry != NULL)
 1262                         FREE(unode->fentry, M_UDFFENTRY);
 1263                 uma_zfree(udf_zone_node, unode);
 1264                 vp->v_data = NULL;
 1265         }
 1266 
 1267         return (0);
 1268 }
 1269 
 1270 static int
 1271 udf_vptofh(struct vop_vptofh_args *a)
 1272 {
 1273         struct udf_node *node;
 1274         struct ifid *ifhp;
 1275 
 1276         node = VTON(a->a_vp);
 1277         ifhp = (struct ifid *)a->a_fhp;
 1278         ifhp->ifid_len = sizeof(struct ifid);
 1279         ifhp->ifid_ino = node->hash_id;
 1280 
 1281         return (0);
 1282 }
 1283 
 1284 /*
 1285  * Read the block and then set the data pointer to correspond with the
 1286  * offset passed in.  Only read in at most 'size' bytes, and then set 'size'
 1287  * to the number of bytes pointed to.  If 'size' is zero, try to read in a
 1288  * whole extent.
 1289  *
 1290  * Note that *bp may be assigned error or not.
 1291  *
 1292  */
 1293 static int
 1294 udf_readatoffset(struct udf_node *node, int *size, off_t offset,
 1295     struct buf **bp, uint8_t **data)
 1296 {
 1297         struct udf_mnt *udfmp = node->udfmp;
 1298         struct vnode *vp = node->i_vnode;
 1299         struct file_entry *fentry;
 1300         struct buf *bp1;
 1301         uint32_t max_size;
 1302         daddr_t sector;
 1303         off_t off;
 1304         int adj_size;
 1305         int error;
 1306 
 1307         /*
 1308          * This call is made *not* only to detect UDF_INVALID_BMAP case,
 1309          * max_size is used as an ad-hoc read-ahead hint for "normal" case.
 1310          */
 1311         error = udf_bmap_internal(node, offset, &sector, &max_size);
 1312         if (error == UDF_INVALID_BMAP) {
 1313                 /*
 1314                  * This error means that the file *data* is stored in the
 1315                  * allocation descriptor field of the file entry.
 1316                  */
 1317                 fentry = node->fentry;
 1318                 *data = &fentry->data[le32toh(fentry->l_ea)];
 1319                 *size = le32toh(fentry->l_ad);
 1320                 if (offset >= *size)
 1321                         *size = 0;
 1322                 else {
 1323                         *data += offset;
 1324                         *size -= offset;
 1325                 }
 1326                 return (0);
 1327         } else if (error != 0) {
 1328                 return (error);
 1329         }
 1330 
 1331         /* Adjust the size so that it is within range */
 1332         if (*size == 0 || *size > max_size)
 1333                 *size = max_size;
 1334 
 1335         /*
 1336          * Because we will read starting at block boundary, we need to adjust
 1337          * how much we need to read so that all promised data is in.
 1338          * Also, we can't promise to read more than MAXBSIZE bytes starting
 1339          * from block boundary, so adjust what we promise too.
 1340          */
 1341         off = blkoff(udfmp, offset);
 1342         *size = min(*size, MAXBSIZE - off);
 1343         adj_size = (*size + off + udfmp->bmask) & ~udfmp->bmask;
 1344         *bp = NULL;
 1345         if ((error = bread(vp, lblkno(udfmp, offset), adj_size, NOCRED, bp))) {
 1346                 printf("warning: udf_readlblks returned error %d\n", error);
 1347                 /* note: *bp may be non-NULL */
 1348                 return (error);
 1349         }
 1350 
 1351         bp1 = *bp;
 1352         *data = (uint8_t *)&bp1->b_data[offset & udfmp->bmask];
 1353         return (0);
 1354 }
 1355 
 1356 /*
 1357  * Translate a file offset into a logical block and then into a physical
 1358  * block.
 1359  * max_size - maximum number of bytes that can be read starting from given
 1360  * offset, rather than beginning of calculated sector number
 1361  */
 1362 static int
 1363 udf_bmap_internal(struct udf_node *node, off_t offset, daddr_t *sector,
 1364     uint32_t *max_size)
 1365 {
 1366         struct udf_mnt *udfmp;
 1367         struct file_entry *fentry;
 1368         void *icb;
 1369         struct icb_tag *tag;
 1370         uint32_t icblen = 0;
 1371         daddr_t lsector;
 1372         int ad_offset, ad_num = 0;
 1373         int i, p_offset;
 1374 
 1375         udfmp = node->udfmp;
 1376         fentry = node->fentry;
 1377         tag = &fentry->icbtag;
 1378 
 1379         switch (le16toh(tag->strat_type)) {
 1380         case 4:
 1381                 break;
 1382 
 1383         case 4096:
 1384                 printf("Cannot deal with strategy4096 yet!\n");
 1385                 return (ENODEV);
 1386 
 1387         default:
 1388                 printf("Unknown strategy type %d\n", tag->strat_type);
 1389                 return (ENODEV);
 1390         }
 1391 
 1392         switch (le16toh(tag->flags) & 0x7) {
 1393         case 0:
 1394                 /*
 1395                  * The allocation descriptor field is filled with short_ad's.
 1396                  * If the offset is beyond the current extent, look for the
 1397                  * next extent.
 1398                  */
 1399                 do {
 1400                         offset -= icblen;
 1401                         ad_offset = sizeof(struct short_ad) * ad_num;
 1402                         if (ad_offset > le32toh(fentry->l_ad)) {
 1403                                 printf("File offset out of bounds\n");
 1404                                 return (EINVAL);
 1405                         }
 1406                         icb = GETICB(short_ad, fentry,
 1407                             le32toh(fentry->l_ea) + ad_offset);
 1408                         icblen = GETICBLEN(short_ad, icb);
 1409                         ad_num++;
 1410                 } while(offset >= icblen);
 1411 
 1412                 lsector = (offset  >> udfmp->bshift) +
 1413                     le32toh(((struct short_ad *)(icb))->pos);
 1414 
 1415                 *max_size = icblen - offset;
 1416 
 1417                 break;
 1418         case 1:
 1419                 /*
 1420                  * The allocation descriptor field is filled with long_ad's
 1421                  * If the offset is beyond the current extent, look for the
 1422                  * next extent.
 1423                  */
 1424                 do {
 1425                         offset -= icblen;
 1426                         ad_offset = sizeof(struct long_ad) * ad_num;
 1427                         if (ad_offset > le32toh(fentry->l_ad)) {
 1428                                 printf("File offset out of bounds\n");
 1429                                 return (EINVAL);
 1430                         }
 1431                         icb = GETICB(long_ad, fentry,
 1432                             le32toh(fentry->l_ea) + ad_offset);
 1433                         icblen = GETICBLEN(long_ad, icb);
 1434                         ad_num++;
 1435                 } while(offset >= icblen);
 1436 
 1437                 lsector = (offset >> udfmp->bshift) +
 1438                     le32toh(((struct long_ad *)(icb))->loc.lb_num);
 1439 
 1440                 *max_size = icblen - offset;
 1441 
 1442                 break;
 1443         case 3:
 1444                 /*
 1445                  * This type means that the file *data* is stored in the
 1446                  * allocation descriptor field of the file entry.
 1447                  */
 1448                 *max_size = 0;
 1449                 *sector = node->hash_id + udfmp->part_start;
 1450 
 1451                 return (UDF_INVALID_BMAP);
 1452         case 2:
 1453                 /* DirectCD does not use extended_ad's */
 1454         default:
 1455                 printf("Unsupported allocation descriptor %d\n",
 1456                        tag->flags & 0x7);
 1457                 return (ENODEV);
 1458         }
 1459 
 1460         *sector = lsector + udfmp->part_start;
 1461 
 1462         /*
 1463          * Check the sparing table.  Each entry represents the beginning of
 1464          * a packet.
 1465          */
 1466         if (udfmp->s_table != NULL) {
 1467                 for (i = 0; i< udfmp->s_table_entries; i++) {
 1468                         p_offset =
 1469                             lsector - le32toh(udfmp->s_table->entries[i].org);
 1470                         if ((p_offset < udfmp->p_sectors) && (p_offset >= 0)) {
 1471                                 *sector =
 1472                                    le32toh(udfmp->s_table->entries[i].map) +
 1473                                     p_offset;
 1474                                 break;
 1475                         }
 1476                 }
 1477         }
 1478 
 1479         return (0);
 1480 }

Cache object: 3ad6a7df28c85da3bd8cd2efc91a4964


[ 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.