linux/fs/udf/truncate.c
<<
>>
Prefs
   1/*
   2 * truncate.c
   3 *
   4 * PURPOSE
   5 *      Truncate handling routines for the OSTA-UDF(tm) filesystem.
   6 *
   7 * COPYRIGHT
   8 *      This file is distributed under the terms of the GNU General Public
   9 *      License (GPL). Copies of the GPL can be obtained from:
  10 *              ftp://prep.ai.mit.edu/pub/gnu/GPL
  11 *      Each contributing author retains all rights to their own work.
  12 *
  13 *  (C) 1999-2004 Ben Fennema
  14 *  (C) 1999 Stelias Computing Inc
  15 *
  16 * HISTORY
  17 *
  18 *  02/24/99 blf  Created.
  19 *
  20 */
  21
  22#include "udfdecl.h"
  23#include <linux/fs.h>
  24#include <linux/mm.h>
  25#include <linux/buffer_head.h>
  26
  27#include "udf_i.h"
  28#include "udf_sb.h"
  29
  30static void extent_trunc(struct inode *inode, struct extent_position *epos,
  31                         struct kernel_lb_addr *eloc, int8_t etype, uint32_t elen,
  32                         uint32_t nelen)
  33{
  34        struct kernel_lb_addr neloc = {};
  35        int last_block = (elen + inode->i_sb->s_blocksize - 1) >>
  36                inode->i_sb->s_blocksize_bits;
  37        int first_block = (nelen + inode->i_sb->s_blocksize - 1) >>
  38                inode->i_sb->s_blocksize_bits;
  39
  40        if (nelen) {
  41                if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  42                        udf_free_blocks(inode->i_sb, inode, eloc, 0,
  43                                        last_block);
  44                        etype = (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30);
  45                } else
  46                        neloc = *eloc;
  47                nelen = (etype << 30) | nelen;
  48        }
  49
  50        if (elen != nelen) {
  51                udf_write_aext(inode, epos, &neloc, nelen, 0);
  52                if (last_block - first_block > 0) {
  53                        if (etype == (EXT_RECORDED_ALLOCATED >> 30))
  54                                mark_inode_dirty(inode);
  55
  56                        if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  57                                udf_free_blocks(inode->i_sb, inode, eloc,
  58                                                first_block,
  59                                                last_block - first_block);
  60                }
  61        }
  62}
  63
  64/*
  65 * Truncate the last extent to match i_size. This function assumes
  66 * that preallocation extent is already truncated.
  67 */
  68void udf_truncate_tail_extent(struct inode *inode)
  69{
  70        struct extent_position epos = {};
  71        struct kernel_lb_addr eloc;
  72        uint32_t elen, nelen;
  73        uint64_t lbcount = 0;
  74        int8_t etype = -1, netype;
  75        int adsize;
  76        struct udf_inode_info *iinfo = UDF_I(inode);
  77
  78        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
  79            inode->i_size == iinfo->i_lenExtents)
  80                return;
  81        /* Are we going to delete the file anyway? */
  82        if (inode->i_nlink == 0)
  83                return;
  84
  85        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  86                adsize = sizeof(struct short_ad);
  87        else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  88                adsize = sizeof(struct long_ad);
  89        else
  90                BUG();
  91
  92        /* Find the last extent in the file */
  93        while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  94                etype = netype;
  95                lbcount += elen;
  96                if (lbcount > inode->i_size) {
  97                        if (lbcount - inode->i_size >= inode->i_sb->s_blocksize)
  98                                udf_warn(inode->i_sb,
  99                                         "Too long extent after EOF in inode %u: i_size: %lld lbcount: %lld extent %u+%u\n",
 100                                         (unsigned)inode->i_ino,
 101                                         (long long)inode->i_size,
 102                                         (long long)lbcount,
 103                                         (unsigned)eloc.logicalBlockNum,
 104                                         (unsigned)elen);
 105                        nelen = elen - (lbcount - inode->i_size);
 106                        epos.offset -= adsize;
 107                        extent_trunc(inode, &epos, &eloc, etype, elen, nelen);
 108                        epos.offset += adsize;
 109                        if (udf_next_aext(inode, &epos, &eloc, &elen, 1) != -1)
 110                                udf_err(inode->i_sb,
 111                                        "Extent after EOF in inode %u\n",
 112                                        (unsigned)inode->i_ino);
 113                        break;
 114                }
 115        }
 116        /* This inode entry is in-memory only and thus we don't have to mark
 117         * the inode dirty */
 118        iinfo->i_lenExtents = inode->i_size;
 119        brelse(epos.bh);
 120}
 121
 122void udf_discard_prealloc(struct inode *inode)
 123{
 124        struct extent_position epos = { NULL, 0, {0, 0} };
 125        struct kernel_lb_addr eloc;
 126        uint32_t elen;
 127        uint64_t lbcount = 0;
 128        int8_t etype = -1, netype;
 129        int adsize;
 130        struct udf_inode_info *iinfo = UDF_I(inode);
 131
 132        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB ||
 133            inode->i_size == iinfo->i_lenExtents)
 134                return;
 135
 136        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
 137                adsize = sizeof(struct short_ad);
 138        else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
 139                adsize = sizeof(struct long_ad);
 140        else
 141                adsize = 0;
 142
 143        epos.block = iinfo->i_location;
 144
 145        /* Find the last extent in the file */
 146        while ((netype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
 147                etype = netype;
 148                lbcount += elen;
 149        }
 150        if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
 151                epos.offset -= adsize;
 152                lbcount -= elen;
 153                extent_trunc(inode, &epos, &eloc, etype, elen, 0);
 154                if (!epos.bh) {
 155                        iinfo->i_lenAlloc =
 156                                epos.offset -
 157                                udf_file_entry_alloc_offset(inode);
 158                        mark_inode_dirty(inode);
 159                } else {
 160                        struct allocExtDesc *aed =
 161                                (struct allocExtDesc *)(epos.bh->b_data);
 162                        aed->lengthAllocDescs =
 163                                cpu_to_le32(epos.offset -
 164                                            sizeof(struct allocExtDesc));
 165                        if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
 166                            UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
 167                                udf_update_tag(epos.bh->b_data, epos.offset);
 168                        else
 169                                udf_update_tag(epos.bh->b_data,
 170                                               sizeof(struct allocExtDesc));
 171                        mark_buffer_dirty_inode(epos.bh, inode);
 172                }
 173        }
 174        /* This inode entry is in-memory only and thus we don't have to mark
 175         * the inode dirty */
 176        iinfo->i_lenExtents = lbcount;
 177        brelse(epos.bh);
 178}
 179
 180static void udf_update_alloc_ext_desc(struct inode *inode,
 181                                      struct extent_position *epos,
 182                                      u32 lenalloc)
 183{
 184        struct super_block *sb = inode->i_sb;
 185        struct udf_sb_info *sbi = UDF_SB(sb);
 186
 187        struct allocExtDesc *aed = (struct allocExtDesc *) (epos->bh->b_data);
 188        int len = sizeof(struct allocExtDesc);
 189
 190        aed->lengthAllocDescs = cpu_to_le32(lenalloc);
 191        if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT) || sbi->s_udfrev >= 0x0201)
 192                len += lenalloc;
 193
 194        udf_update_tag(epos->bh->b_data, len);
 195        mark_buffer_dirty_inode(epos->bh, inode);
 196}
 CBTAG_Fs13" id="L113" class="line" name                " class="line" name="L110"> 1102;class="sref">eposcate.c#L195" id="L195" class="line" name="f=">);
ru12dclass() has1toL195" id="L195" class="line" name="f2 1       2  href="+code=UDF_QUERY_2  (lo2g longclass="3 don't have tbe usedlass="line" name="L67">  67  68void <2         2                        2  (un2igned)="sref">udf_truncate_tail_extentinode,
 192   2         2                        2  (un2igned)"line" name="L184"> 184   2         2          udf_sb_info *epos = { NULL, 0,"line" name="L196"> 196}
<2         2          udf_inode_info *eloc;
eloc;
  71      2         2          elen, nelen;
te.c#L73" id="L73"ncposectref">te.s="lass= href="fs/udf/truf/truncate.c#L193" id="L193" class="line" name="L193"> 193
 etype = -1, netype 193
 *sb = inode->i_sb;
 185   2         2                  etypesectorla -1, i_size;
inode->cksize)
 185   2 1       2                        2 etypef168la -1,  185   2         2                        2 (uns2gned) 130   2         2          break;
 *iinfo = UDF_I(inode);
 131
 145   2    }
i_alloc_type == ICBTAG_FLAG_AD_LONG)
 137   2    /* This 2node 21sref">adsize = sizeof(struct short_ad);
  87      2n class="2omment">         * the i2ode d21sref">iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  88      2    i21sref">adsize = sizeof(struct long_ad);
  89      2    21d="L90" class="line" name="L90">  90      2 href="fs2udf/truncate.c#L121" id=2L121"22f">BUG();
  91
 122voi2  netype;
eloc, &elen, 1)) != -1) {
 168   2 href="fs2udf/truncate.c#L124" id=2L124"22.EXT_NOT_REruncate.c#L164" id="L164" class=&lL15l" ccode=inode" class="sref">inode->cksize)
 168   2         2 i_sb)->inode->cksize)
 168   2    }
 147   2    adsize = sizry only and thus we don&W46"L1u/udf/trud" id="L14" class="line" name="L82">  82      2    etype =  168   2    22sref">adsize = siclass="line" name="L135"> 135
 150   2    struc2 offset -= adsize;
 152   2href="fs/2df/truncate.c#L132" id="2132" 23class="stringref="fs/udf/tru="+code=inode" class="sref">inode, &epos, &eloc, etype, elen, 0);
nelen;
 168   2    if (<2 href="+code=iinfo" clas2="sre23uint32_t offset += adsize;
 109   2        <2 href="+code=inode" clas2="sre2">inode-> 109   2         2  return;
offset += adsize;"line" name="L109"> 109   2    }
  90      2    if (<2 href="+code=iinfo" clas2="sre23sref">adsize = sizeof(struct offset += adsize;; 109   2     110<2    else 2f (iinfobh) {
 110<2    int <2  adsize = sizeof(struct inode);
 158   2    else
2a href="fs/udf/truncate.2#L1412 id="L141" class="line" name="L141"> 141   2         2  adsize = 0;
allocExtDesc);
 189
 143   2    ep2s. = netype;
incurrode<=ino&epos, &eloc, etype,  143   2         2df/truncate.c#L145" id="2145" 24zeof(struct  147   2    /* Find 2he la2t extent in th>iinfoEXT_NOT_RECORDECORDCORENTcate.cDECS_RECORDED_ALLOCCORDECORDCORENTcate.cDECSass=d="L151" class="line" name="L151"> 151   2    while2((epos.inwriclasinode" class="sref">inwriclasino&epos, &eloc, eloc;
 154   2         2  etype = iinfote.c#L73" id="L73"ncposectref">te.s="ls="line" name="L151"> 151   2    else 2  lbcount +=pos.  82      2    }
  82      2    if (<2 href="+code=etype" clas2="sre25=udf_err" class="sref">udf_err(bh) {
 154   2         2  epos.UDF_FLAG_STRICT) fs/udf/truncate.c#L125" id="Lmp;eloc, href="+code=iinfo" class="sref">iinfo->hline" name="L143"> 143   2         2  lbcount -================================== href="fs/udf/truncposectref">te.c#L73" id="L73"ncposectref">te.s="ls"line" name="L154"> 154   2    extent_trrrrrrrrr}/a>->bh) {
 155   2         2  if (!epos.udf_err(i_lenAlloc =
 193
iinfo->(inode);
 159   2         2                  epos.}ass="line" name="L141"> 141   2         2                  udf_file_entrc_ext_desc(struct inodeepos, &line" name="L141"> 141   2    else 2          mark_________________________mp;eloc,  159   2    }
udf_file_" class="sref">epos.bh);
 178}
<2         2          struct alef="+code=lbcounlass="sref">offset += adsize;;a ref">allocExtDesc);
 189
eloc, href="+code=iinfo" class="sref">iinfo-> href="+code=lbcount.c#L126" id="L126" class="line" name="L126"> 126   2         2          aed->bh);
UDF_Fline" name="L126"> 126   2    cpu_to_le3t;po" class="sref">iinfe" cgetpo" cl&UDF_FL href="+code=etype" class="sref">etype,  168   2         2                        2     2izeof(struct  110<2         2          if (!UDt;te.c#L73" id="L73"ncposectref">te.s="laslass="line" name="L110"> 110<2         2              UDF_SB(inode->cksize)
 110<2         2                  udf_updt;inode->cksize)
 110<2    else 2          else
 169   2         2                  udf_update_tag(te.c#L73" id="L73"ncposectref">te.s="lass1 lass="line" name="L110"> 110<2         2                        2     2  sizeof(struct  160   2         2          mark_buffer_d="+code=inode" class="sref">inode, &epos, &eloc, etype, elen, 0);
 110<2         2  }
mark_buffer_d=lass="sref">offset += adsize;
 109   2    }
 150   2    /* This 2node 2ntry is in-mem"line" name="L150"> 150   2         2omment">         * the i2ode d27ass="line" name="L136"> 136   2    i27>iinfo->te.c#L73" id="L73"ncposectref">te.s="ls="line" name="L151"> 151   2    27ref">etype = bh) {
 154   2 href="fs2udf/truncate.c#L179" id=2L179"27sref">adsize = sizeof(struct UDF_FLAG_STRICT) fs/udf/truncate.c#L125" id="Lmp;eloc, href="+code=iinfo" class="sref">iinfo->h== href="fs/udf/truncposectref">te.c#L73" id="L73"ncposectref">te.s="ls"line" name="L154"> 154   2href="fs/2df/truncate.c#L180" id="2180" 27"L150" class="/a>->bh) {
 155   2ic void <2 href="+code=udf_update_2lloc_28f">BUG();
i_lenAlloc =
 193
inode);
 159   2         2                        2a hre2="+code=u32" c}ass="line" name="L141"> 141   2 href="fs2udf/truncate.c#L184" id=2L184"28lass="sref">extent_trunc((struct inodeepos, &_mp;eloc,  159   2    struc2 sup="fs/udf/truncalenAlloc" class="sref">i_lenAlloc =
lbcount;
i_sb)-> 159   2         2  136   2href="fs/2df/truncate.c#L187" id="2187" 28fo->epos.bh);
 178}
<2    struc2  150   2    int <2 href="+code=len" class=2sref"2len


The origi93" LXR software by" id=ine" namehttp://sourceforge.net/projects/lxr">LXR #39;uni); Lxr.e=lux.no kncply hosted"by"ine" namehttp://www.redpill-e=lpro.no="Redpill L=lpro AS