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

Cache object: eadced9efc2c3237f76f6d44d5933704


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