Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * inode.c
0003  *
0004  * PURPOSE
0005  *  Inode handling routines for the OSTA-UDF(tm) filesystem.
0006  *
0007  * COPYRIGHT
0008  *  This file is distributed under the terms of the GNU General Public
0009  *  License (GPL). Copies of the GPL can be obtained from:
0010  *    ftp://prep.ai.mit.edu/pub/gnu/GPL
0011  *  Each contributing author retains all rights to their own work.
0012  *
0013  *  (C) 1998 Dave Boynton
0014  *  (C) 1998-2004 Ben Fennema
0015  *  (C) 1999-2000 Stelias Computing Inc
0016  *
0017  * HISTORY
0018  *
0019  *  10/04/98 dgb  Added rudimentary directory functions
0020  *  10/07/98      Fully working udf_block_map! It works!
0021  *  11/25/98      bmap altered to better support extents
0022  *  12/06/98 blf  partition support in udf_iget, udf_block_map
0023  *                and udf_read_inode
0024  *  12/12/98      rewrote udf_block_map to handle next extents and descs across
0025  *                block boundaries (which is not actually allowed)
0026  *  12/20/98      added support for strategy 4096
0027  *  03/07/99      rewrote udf_block_map (again)
0028  *                New funcs, inode_bmap, udf_next_aext
0029  *  04/19/99      Support for writing device EA's for major/minor #
0030  */
0031 
0032 #include "udfdecl.h"
0033 #include <linux/mm.h>
0034 #include <linux/module.h>
0035 #include <linux/pagemap.h>
0036 #include <linux/writeback.h>
0037 #include <linux/slab.h>
0038 #include <linux/crc-itu-t.h>
0039 #include <linux/mpage.h>
0040 #include <linux/uio.h>
0041 #include <linux/bio.h>
0042 
0043 #include "udf_i.h"
0044 #include "udf_sb.h"
0045 
0046 #define EXTENT_MERGE_SIZE 5
0047 
0048 #define FE_MAPPED_PERMS (FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \
0049              FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \
0050              FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC)
0051 
0052 #define FE_DELETE_PERMS (FE_PERM_U_DELETE | FE_PERM_G_DELETE | \
0053              FE_PERM_O_DELETE)
0054 
0055 static umode_t udf_convert_permissions(struct fileEntry *);
0056 static int udf_update_inode(struct inode *, int);
0057 static int udf_sync_inode(struct inode *inode);
0058 static int udf_alloc_i_data(struct inode *inode, size_t size);
0059 static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
0060 static int8_t udf_insert_aext(struct inode *, struct extent_position,
0061                   struct kernel_lb_addr, uint32_t);
0062 static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
0063                   struct kernel_long_ad *, int *);
0064 static void udf_prealloc_extents(struct inode *, int, int,
0065                  struct kernel_long_ad *, int *);
0066 static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
0067 static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
0068                    int, struct extent_position *);
0069 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
0070 
0071 static void __udf_clear_extent_cache(struct inode *inode)
0072 {
0073     struct udf_inode_info *iinfo = UDF_I(inode);
0074 
0075     if (iinfo->cached_extent.lstart != -1) {
0076         brelse(iinfo->cached_extent.epos.bh);
0077         iinfo->cached_extent.lstart = -1;
0078     }
0079 }
0080 
0081 /* Invalidate extent cache */
0082 static void udf_clear_extent_cache(struct inode *inode)
0083 {
0084     struct udf_inode_info *iinfo = UDF_I(inode);
0085 
0086     spin_lock(&iinfo->i_extent_cache_lock);
0087     __udf_clear_extent_cache(inode);
0088     spin_unlock(&iinfo->i_extent_cache_lock);
0089 }
0090 
0091 /* Return contents of extent cache */
0092 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
0093                  loff_t *lbcount, struct extent_position *pos)
0094 {
0095     struct udf_inode_info *iinfo = UDF_I(inode);
0096     int ret = 0;
0097 
0098     spin_lock(&iinfo->i_extent_cache_lock);
0099     if ((iinfo->cached_extent.lstart <= bcount) &&
0100         (iinfo->cached_extent.lstart != -1)) {
0101         /* Cache hit */
0102         *lbcount = iinfo->cached_extent.lstart;
0103         memcpy(pos, &iinfo->cached_extent.epos,
0104                sizeof(struct extent_position));
0105         if (pos->bh)
0106             get_bh(pos->bh);
0107         ret = 1;
0108     }
0109     spin_unlock(&iinfo->i_extent_cache_lock);
0110     return ret;
0111 }
0112 
0113 /* Add extent to extent cache */
0114 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
0115                     struct extent_position *pos)
0116 {
0117     struct udf_inode_info *iinfo = UDF_I(inode);
0118 
0119     spin_lock(&iinfo->i_extent_cache_lock);
0120     /* Invalidate previously cached extent */
0121     __udf_clear_extent_cache(inode);
0122     if (pos->bh)
0123         get_bh(pos->bh);
0124     memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
0125     iinfo->cached_extent.lstart = estart;
0126     switch (iinfo->i_alloc_type) {
0127     case ICBTAG_FLAG_AD_SHORT:
0128         iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
0129         break;
0130     case ICBTAG_FLAG_AD_LONG:
0131         iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
0132         break;
0133     }
0134     spin_unlock(&iinfo->i_extent_cache_lock);
0135 }
0136 
0137 void udf_evict_inode(struct inode *inode)
0138 {
0139     struct udf_inode_info *iinfo = UDF_I(inode);
0140     int want_delete = 0;
0141 
0142     if (!is_bad_inode(inode)) {
0143         if (!inode->i_nlink) {
0144             want_delete = 1;
0145             udf_setsize(inode, 0);
0146             udf_update_inode(inode, IS_SYNC(inode));
0147         }
0148         if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
0149             inode->i_size != iinfo->i_lenExtents) {
0150             udf_warn(inode->i_sb,
0151                  "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
0152                  inode->i_ino, inode->i_mode,
0153                  (unsigned long long)inode->i_size,
0154                  (unsigned long long)iinfo->i_lenExtents);
0155         }
0156     }
0157     truncate_inode_pages_final(&inode->i_data);
0158     invalidate_inode_buffers(inode);
0159     clear_inode(inode);
0160     kfree(iinfo->i_data);
0161     iinfo->i_data = NULL;
0162     udf_clear_extent_cache(inode);
0163     if (want_delete) {
0164         udf_free_inode(inode);
0165     }
0166 }
0167 
0168 static void udf_write_failed(struct address_space *mapping, loff_t to)
0169 {
0170     struct inode *inode = mapping->host;
0171     struct udf_inode_info *iinfo = UDF_I(inode);
0172     loff_t isize = inode->i_size;
0173 
0174     if (to > isize) {
0175         truncate_pagecache(inode, isize);
0176         if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
0177             down_write(&iinfo->i_data_sem);
0178             udf_clear_extent_cache(inode);
0179             udf_truncate_extents(inode);
0180             up_write(&iinfo->i_data_sem);
0181         }
0182     }
0183 }
0184 
0185 static int udf_writepage(struct page *page, struct writeback_control *wbc)
0186 {
0187     return block_write_full_page(page, udf_get_block, wbc);
0188 }
0189 
0190 static int udf_writepages(struct address_space *mapping,
0191             struct writeback_control *wbc)
0192 {
0193     return mpage_writepages(mapping, wbc, udf_get_block);
0194 }
0195 
0196 static int udf_read_folio(struct file *file, struct folio *folio)
0197 {
0198     return mpage_read_folio(folio, udf_get_block);
0199 }
0200 
0201 static void udf_readahead(struct readahead_control *rac)
0202 {
0203     mpage_readahead(rac, udf_get_block);
0204 }
0205 
0206 static int udf_write_begin(struct file *file, struct address_space *mapping,
0207             loff_t pos, unsigned len,
0208             struct page **pagep, void **fsdata)
0209 {
0210     int ret;
0211 
0212     ret = block_write_begin(mapping, pos, len, pagep, udf_get_block);
0213     if (unlikely(ret))
0214         udf_write_failed(mapping, pos + len);
0215     return ret;
0216 }
0217 
0218 static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
0219 {
0220     struct file *file = iocb->ki_filp;
0221     struct address_space *mapping = file->f_mapping;
0222     struct inode *inode = mapping->host;
0223     size_t count = iov_iter_count(iter);
0224     ssize_t ret;
0225 
0226     ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
0227     if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
0228         udf_write_failed(mapping, iocb->ki_pos + count);
0229     return ret;
0230 }
0231 
0232 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
0233 {
0234     return generic_block_bmap(mapping, block, udf_get_block);
0235 }
0236 
0237 const struct address_space_operations udf_aops = {
0238     .dirty_folio    = block_dirty_folio,
0239     .invalidate_folio = block_invalidate_folio,
0240     .read_folio = udf_read_folio,
0241     .readahead  = udf_readahead,
0242     .writepage  = udf_writepage,
0243     .writepages = udf_writepages,
0244     .write_begin    = udf_write_begin,
0245     .write_end  = generic_write_end,
0246     .direct_IO  = udf_direct_IO,
0247     .bmap       = udf_bmap,
0248 };
0249 
0250 /*
0251  * Expand file stored in ICB to a normal one-block-file
0252  *
0253  * This function requires i_data_sem for writing and releases it.
0254  * This function requires i_mutex held
0255  */
0256 int udf_expand_file_adinicb(struct inode *inode)
0257 {
0258     struct page *page;
0259     char *kaddr;
0260     struct udf_inode_info *iinfo = UDF_I(inode);
0261     int err;
0262 
0263     WARN_ON_ONCE(!inode_is_locked(inode));
0264     if (!iinfo->i_lenAlloc) {
0265         if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
0266             iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
0267         else
0268             iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
0269         /* from now on we have normal address_space methods */
0270         inode->i_data.a_ops = &udf_aops;
0271         up_write(&iinfo->i_data_sem);
0272         mark_inode_dirty(inode);
0273         return 0;
0274     }
0275     /*
0276      * Release i_data_sem so that we can lock a page - page lock ranks
0277      * above i_data_sem. i_mutex still protects us against file changes.
0278      */
0279     up_write(&iinfo->i_data_sem);
0280 
0281     page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
0282     if (!page)
0283         return -ENOMEM;
0284 
0285     if (!PageUptodate(page)) {
0286         kaddr = kmap_atomic(page);
0287         memset(kaddr + iinfo->i_lenAlloc, 0x00,
0288                PAGE_SIZE - iinfo->i_lenAlloc);
0289         memcpy(kaddr, iinfo->i_data + iinfo->i_lenEAttr,
0290             iinfo->i_lenAlloc);
0291         flush_dcache_page(page);
0292         SetPageUptodate(page);
0293         kunmap_atomic(kaddr);
0294     }
0295     down_write(&iinfo->i_data_sem);
0296     memset(iinfo->i_data + iinfo->i_lenEAttr, 0x00,
0297            iinfo->i_lenAlloc);
0298     iinfo->i_lenAlloc = 0;
0299     if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
0300         iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
0301     else
0302         iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
0303     /* from now on we have normal address_space methods */
0304     inode->i_data.a_ops = &udf_aops;
0305     set_page_dirty(page);
0306     unlock_page(page);
0307     up_write(&iinfo->i_data_sem);
0308     err = filemap_fdatawrite(inode->i_mapping);
0309     if (err) {
0310         /* Restore everything back so that we don't lose data... */
0311         lock_page(page);
0312         down_write(&iinfo->i_data_sem);
0313         kaddr = kmap_atomic(page);
0314         memcpy(iinfo->i_data + iinfo->i_lenEAttr, kaddr, inode->i_size);
0315         kunmap_atomic(kaddr);
0316         unlock_page(page);
0317         iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
0318         inode->i_data.a_ops = &udf_adinicb_aops;
0319         iinfo->i_lenAlloc = inode->i_size;
0320         up_write(&iinfo->i_data_sem);
0321     }
0322     put_page(page);
0323     mark_inode_dirty(inode);
0324 
0325     return err;
0326 }
0327 
0328 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode,
0329                         udf_pblk_t *block, int *err)
0330 {
0331     udf_pblk_t newblock;
0332     struct buffer_head *dbh = NULL;
0333     struct kernel_lb_addr eloc;
0334     uint8_t alloctype;
0335     struct extent_position epos;
0336 
0337     struct udf_fileident_bh sfibh, dfibh;
0338     loff_t f_pos = udf_ext0_offset(inode);
0339     int size = udf_ext0_offset(inode) + inode->i_size;
0340     struct fileIdentDesc cfi, *sfi, *dfi;
0341     struct udf_inode_info *iinfo = UDF_I(inode);
0342 
0343     if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
0344         alloctype = ICBTAG_FLAG_AD_SHORT;
0345     else
0346         alloctype = ICBTAG_FLAG_AD_LONG;
0347 
0348     if (!inode->i_size) {
0349         iinfo->i_alloc_type = alloctype;
0350         mark_inode_dirty(inode);
0351         return NULL;
0352     }
0353 
0354     /* alloc block, and copy data to it */
0355     *block = udf_new_block(inode->i_sb, inode,
0356                    iinfo->i_location.partitionReferenceNum,
0357                    iinfo->i_location.logicalBlockNum, err);
0358     if (!(*block))
0359         return NULL;
0360     newblock = udf_get_pblock(inode->i_sb, *block,
0361                   iinfo->i_location.partitionReferenceNum,
0362                 0);
0363     if (!newblock)
0364         return NULL;
0365     dbh = udf_tgetblk(inode->i_sb, newblock);
0366     if (!dbh)
0367         return NULL;
0368     lock_buffer(dbh);
0369     memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
0370     set_buffer_uptodate(dbh);
0371     unlock_buffer(dbh);
0372     mark_buffer_dirty_inode(dbh, inode);
0373 
0374     sfibh.soffset = sfibh.eoffset =
0375             f_pos & (inode->i_sb->s_blocksize - 1);
0376     sfibh.sbh = sfibh.ebh = NULL;
0377     dfibh.soffset = dfibh.eoffset = 0;
0378     dfibh.sbh = dfibh.ebh = dbh;
0379     while (f_pos < size) {
0380         iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
0381         sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
0382                      NULL, NULL, NULL);
0383         if (!sfi) {
0384             brelse(dbh);
0385             return NULL;
0386         }
0387         iinfo->i_alloc_type = alloctype;
0388         sfi->descTag.tagLocation = cpu_to_le32(*block);
0389         dfibh.soffset = dfibh.eoffset;
0390         dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
0391         dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
0392         if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
0393                  udf_get_fi_ident(sfi))) {
0394             iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
0395             brelse(dbh);
0396             return NULL;
0397         }
0398     }
0399     mark_buffer_dirty_inode(dbh, inode);
0400 
0401     memset(iinfo->i_data + iinfo->i_lenEAttr, 0, iinfo->i_lenAlloc);
0402     iinfo->i_lenAlloc = 0;
0403     eloc.logicalBlockNum = *block;
0404     eloc.partitionReferenceNum =
0405                 iinfo->i_location.partitionReferenceNum;
0406     iinfo->i_lenExtents = inode->i_size;
0407     epos.bh = NULL;
0408     epos.block = iinfo->i_location;
0409     epos.offset = udf_file_entry_alloc_offset(inode);
0410     udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
0411     /* UniqueID stuff */
0412 
0413     brelse(epos.bh);
0414     mark_inode_dirty(inode);
0415     return dbh;
0416 }
0417 
0418 static int udf_get_block(struct inode *inode, sector_t block,
0419              struct buffer_head *bh_result, int create)
0420 {
0421     int err, new;
0422     sector_t phys = 0;
0423     struct udf_inode_info *iinfo;
0424 
0425     if (!create) {
0426         phys = udf_block_map(inode, block);
0427         if (phys)
0428             map_bh(bh_result, inode->i_sb, phys);
0429         return 0;
0430     }
0431 
0432     err = -EIO;
0433     new = 0;
0434     iinfo = UDF_I(inode);
0435 
0436     down_write(&iinfo->i_data_sem);
0437     if (block == iinfo->i_next_alloc_block + 1) {
0438         iinfo->i_next_alloc_block++;
0439         iinfo->i_next_alloc_goal++;
0440     }
0441 
0442     udf_clear_extent_cache(inode);
0443     phys = inode_getblk(inode, block, &err, &new);
0444     if (!phys)
0445         goto abort;
0446 
0447     if (new)
0448         set_buffer_new(bh_result);
0449     map_bh(bh_result, inode->i_sb, phys);
0450 
0451 abort:
0452     up_write(&iinfo->i_data_sem);
0453     return err;
0454 }
0455 
0456 static struct buffer_head *udf_getblk(struct inode *inode, udf_pblk_t block,
0457                       int create, int *err)
0458 {
0459     struct buffer_head *bh;
0460     struct buffer_head dummy;
0461 
0462     dummy.b_state = 0;
0463     dummy.b_blocknr = -1000;
0464     *err = udf_get_block(inode, block, &dummy, create);
0465     if (!*err && buffer_mapped(&dummy)) {
0466         bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
0467         if (buffer_new(&dummy)) {
0468             lock_buffer(bh);
0469             memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
0470             set_buffer_uptodate(bh);
0471             unlock_buffer(bh);
0472             mark_buffer_dirty_inode(bh, inode);
0473         }
0474         return bh;
0475     }
0476 
0477     return NULL;
0478 }
0479 
0480 /* Extend the file with new blocks totaling 'new_block_bytes',
0481  * return the number of extents added
0482  */
0483 static int udf_do_extend_file(struct inode *inode,
0484                   struct extent_position *last_pos,
0485                   struct kernel_long_ad *last_ext,
0486                   loff_t new_block_bytes)
0487 {
0488     uint32_t add;
0489     int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
0490     struct super_block *sb = inode->i_sb;
0491     struct kernel_lb_addr prealloc_loc = {};
0492     uint32_t prealloc_len = 0;
0493     struct udf_inode_info *iinfo;
0494     int err;
0495 
0496     /* The previous extent is fake and we should not extend by anything
0497      * - there's nothing to do... */
0498     if (!new_block_bytes && fake)
0499         return 0;
0500 
0501     iinfo = UDF_I(inode);
0502     /* Round the last extent up to a multiple of block size */
0503     if (last_ext->extLength & (sb->s_blocksize - 1)) {
0504         last_ext->extLength =
0505             (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
0506             (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
0507               sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
0508         iinfo->i_lenExtents =
0509             (iinfo->i_lenExtents + sb->s_blocksize - 1) &
0510             ~(sb->s_blocksize - 1);
0511     }
0512 
0513     /* Last extent are just preallocated blocks? */
0514     if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
0515                         EXT_NOT_RECORDED_ALLOCATED) {
0516         /* Save the extent so that we can reattach it to the end */
0517         prealloc_loc = last_ext->extLocation;
0518         prealloc_len = last_ext->extLength;
0519         /* Mark the extent as a hole */
0520         last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
0521             (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
0522         last_ext->extLocation.logicalBlockNum = 0;
0523         last_ext->extLocation.partitionReferenceNum = 0;
0524     }
0525 
0526     /* Can we merge with the previous extent? */
0527     if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
0528                     EXT_NOT_RECORDED_NOT_ALLOCATED) {
0529         add = (1 << 30) - sb->s_blocksize -
0530             (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
0531         if (add > new_block_bytes)
0532             add = new_block_bytes;
0533         new_block_bytes -= add;
0534         last_ext->extLength += add;
0535     }
0536 
0537     if (fake) {
0538         udf_add_aext(inode, last_pos, &last_ext->extLocation,
0539                  last_ext->extLength, 1);
0540         count++;
0541     } else {
0542         struct kernel_lb_addr tmploc;
0543         uint32_t tmplen;
0544 
0545         udf_write_aext(inode, last_pos, &last_ext->extLocation,
0546                 last_ext->extLength, 1);
0547 
0548         /*
0549          * We've rewritten the last extent. If we are going to add
0550          * more extents, we may need to enter possible following
0551          * empty indirect extent.
0552          */
0553         if (new_block_bytes || prealloc_len)
0554             udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
0555     }
0556 
0557     /* Managed to do everything necessary? */
0558     if (!new_block_bytes)
0559         goto out;
0560 
0561     /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
0562     last_ext->extLocation.logicalBlockNum = 0;
0563     last_ext->extLocation.partitionReferenceNum = 0;
0564     add = (1 << 30) - sb->s_blocksize;
0565     last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
0566 
0567     /* Create enough extents to cover the whole hole */
0568     while (new_block_bytes > add) {
0569         new_block_bytes -= add;
0570         err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
0571                    last_ext->extLength, 1);
0572         if (err)
0573             return err;
0574         count++;
0575     }
0576     if (new_block_bytes) {
0577         last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
0578             new_block_bytes;
0579         err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
0580                    last_ext->extLength, 1);
0581         if (err)
0582             return err;
0583         count++;
0584     }
0585 
0586 out:
0587     /* Do we have some preallocated blocks saved? */
0588     if (prealloc_len) {
0589         err = udf_add_aext(inode, last_pos, &prealloc_loc,
0590                    prealloc_len, 1);
0591         if (err)
0592             return err;
0593         last_ext->extLocation = prealloc_loc;
0594         last_ext->extLength = prealloc_len;
0595         count++;
0596     }
0597 
0598     /* last_pos should point to the last written extent... */
0599     if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
0600         last_pos->offset -= sizeof(struct short_ad);
0601     else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
0602         last_pos->offset -= sizeof(struct long_ad);
0603     else
0604         return -EIO;
0605 
0606     return count;
0607 }
0608 
0609 /* Extend the final block of the file to final_block_len bytes */
0610 static void udf_do_extend_final_block(struct inode *inode,
0611                       struct extent_position *last_pos,
0612                       struct kernel_long_ad *last_ext,
0613                       uint32_t final_block_len)
0614 {
0615     struct super_block *sb = inode->i_sb;
0616     uint32_t added_bytes;
0617 
0618     added_bytes = final_block_len -
0619               (last_ext->extLength & (sb->s_blocksize - 1));
0620     last_ext->extLength += added_bytes;
0621     UDF_I(inode)->i_lenExtents += added_bytes;
0622 
0623     udf_write_aext(inode, last_pos, &last_ext->extLocation,
0624             last_ext->extLength, 1);
0625 }
0626 
0627 static int udf_extend_file(struct inode *inode, loff_t newsize)
0628 {
0629 
0630     struct extent_position epos;
0631     struct kernel_lb_addr eloc;
0632     uint32_t elen;
0633     int8_t etype;
0634     struct super_block *sb = inode->i_sb;
0635     sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
0636     unsigned long partial_final_block;
0637     int adsize;
0638     struct udf_inode_info *iinfo = UDF_I(inode);
0639     struct kernel_long_ad extent;
0640     int err = 0;
0641     int within_final_block;
0642 
0643     if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
0644         adsize = sizeof(struct short_ad);
0645     else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
0646         adsize = sizeof(struct long_ad);
0647     else
0648         BUG();
0649 
0650     etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
0651     within_final_block = (etype != -1);
0652 
0653     if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
0654         (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
0655         /* File has no extents at all or has empty last
0656          * indirect extent! Create a fake extent... */
0657         extent.extLocation.logicalBlockNum = 0;
0658         extent.extLocation.partitionReferenceNum = 0;
0659         extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
0660     } else {
0661         epos.offset -= adsize;
0662         etype = udf_next_aext(inode, &epos, &extent.extLocation,
0663                       &extent.extLength, 0);
0664         extent.extLength |= etype << 30;
0665     }
0666 
0667     partial_final_block = newsize & (sb->s_blocksize - 1);
0668 
0669     /* File has extent covering the new size (could happen when extending
0670      * inside a block)?
0671      */
0672     if (within_final_block) {
0673         /* Extending file within the last file block */
0674         udf_do_extend_final_block(inode, &epos, &extent,
0675                       partial_final_block);
0676     } else {
0677         loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
0678                  partial_final_block;
0679         err = udf_do_extend_file(inode, &epos, &extent, add);
0680     }
0681 
0682     if (err < 0)
0683         goto out;
0684     err = 0;
0685     iinfo->i_lenExtents = newsize;
0686 out:
0687     brelse(epos.bh);
0688     return err;
0689 }
0690 
0691 static sector_t inode_getblk(struct inode *inode, sector_t block,
0692                  int *err, int *new)
0693 {
0694     struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
0695     struct extent_position prev_epos, cur_epos, next_epos;
0696     int count = 0, startnum = 0, endnum = 0;
0697     uint32_t elen = 0, tmpelen;
0698     struct kernel_lb_addr eloc, tmpeloc;
0699     int c = 1;
0700     loff_t lbcount = 0, b_off = 0;
0701     udf_pblk_t newblocknum, newblock;
0702     sector_t offset = 0;
0703     int8_t etype;
0704     struct udf_inode_info *iinfo = UDF_I(inode);
0705     udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
0706     int lastblock = 0;
0707     bool isBeyondEOF;
0708 
0709     *err = 0;
0710     *new = 0;
0711     prev_epos.offset = udf_file_entry_alloc_offset(inode);
0712     prev_epos.block = iinfo->i_location;
0713     prev_epos.bh = NULL;
0714     cur_epos = next_epos = prev_epos;
0715     b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
0716 
0717     /* find the extent which contains the block we are looking for.
0718        alternate between laarr[0] and laarr[1] for locations of the
0719        current extent, and the previous extent */
0720     do {
0721         if (prev_epos.bh != cur_epos.bh) {
0722             brelse(prev_epos.bh);
0723             get_bh(cur_epos.bh);
0724             prev_epos.bh = cur_epos.bh;
0725         }
0726         if (cur_epos.bh != next_epos.bh) {
0727             brelse(cur_epos.bh);
0728             get_bh(next_epos.bh);
0729             cur_epos.bh = next_epos.bh;
0730         }
0731 
0732         lbcount += elen;
0733 
0734         prev_epos.block = cur_epos.block;
0735         cur_epos.block = next_epos.block;
0736 
0737         prev_epos.offset = cur_epos.offset;
0738         cur_epos.offset = next_epos.offset;
0739 
0740         etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
0741         if (etype == -1)
0742             break;
0743 
0744         c = !c;
0745 
0746         laarr[c].extLength = (etype << 30) | elen;
0747         laarr[c].extLocation = eloc;
0748 
0749         if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
0750             pgoal = eloc.logicalBlockNum +
0751                 ((elen + inode->i_sb->s_blocksize - 1) >>
0752                  inode->i_sb->s_blocksize_bits);
0753 
0754         count++;
0755     } while (lbcount + elen <= b_off);
0756 
0757     b_off -= lbcount;
0758     offset = b_off >> inode->i_sb->s_blocksize_bits;
0759     /*
0760      * Move prev_epos and cur_epos into indirect extent if we are at
0761      * the pointer to it
0762      */
0763     udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
0764     udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
0765 
0766     /* if the extent is allocated and recorded, return the block
0767        if the extent is not a multiple of the blocksize, round up */
0768 
0769     if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
0770         if (elen & (inode->i_sb->s_blocksize - 1)) {
0771             elen = EXT_RECORDED_ALLOCATED |
0772                 ((elen + inode->i_sb->s_blocksize - 1) &
0773                  ~(inode->i_sb->s_blocksize - 1));
0774             udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
0775         }
0776         newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
0777         goto out_free;
0778     }
0779 
0780     /* Are we beyond EOF? */
0781     if (etype == -1) {
0782         int ret;
0783         loff_t hole_len;
0784         isBeyondEOF = true;
0785         if (count) {
0786             if (c)
0787                 laarr[0] = laarr[1];
0788             startnum = 1;
0789         } else {
0790             /* Create a fake extent when there's not one */
0791             memset(&laarr[0].extLocation, 0x00,
0792                 sizeof(struct kernel_lb_addr));
0793             laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
0794             /* Will udf_do_extend_file() create real extent from
0795                a fake one? */
0796             startnum = (offset > 0);
0797         }
0798         /* Create extents for the hole between EOF and offset */
0799         hole_len = (loff_t)offset << inode->i_blkbits;
0800         ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
0801         if (ret < 0) {
0802             *err = ret;
0803             newblock = 0;
0804             goto out_free;
0805         }
0806         c = 0;
0807         offset = 0;
0808         count += ret;
0809         /* We are not covered by a preallocated extent? */
0810         if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
0811                         EXT_NOT_RECORDED_ALLOCATED) {
0812             /* Is there any real extent? - otherwise we overwrite
0813              * the fake one... */
0814             if (count)
0815                 c = !c;
0816             laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
0817                 inode->i_sb->s_blocksize;
0818             memset(&laarr[c].extLocation, 0x00,
0819                 sizeof(struct kernel_lb_addr));
0820             count++;
0821         }
0822         endnum = c + 1;
0823         lastblock = 1;
0824     } else {
0825         isBeyondEOF = false;
0826         endnum = startnum = ((count > 2) ? 2 : count);
0827 
0828         /* if the current extent is in position 0,
0829            swap it with the previous */
0830         if (!c && count != 1) {
0831             laarr[2] = laarr[0];
0832             laarr[0] = laarr[1];
0833             laarr[1] = laarr[2];
0834             c = 1;
0835         }
0836 
0837         /* if the current block is located in an extent,
0838            read the next extent */
0839         etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
0840         if (etype != -1) {
0841             laarr[c + 1].extLength = (etype << 30) | elen;
0842             laarr[c + 1].extLocation = eloc;
0843             count++;
0844             startnum++;
0845             endnum++;
0846         } else
0847             lastblock = 1;
0848     }
0849 
0850     /* if the current extent is not recorded but allocated, get the
0851      * block in the extent corresponding to the requested block */
0852     if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
0853         newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
0854     else { /* otherwise, allocate a new block */
0855         if (iinfo->i_next_alloc_block == block)
0856             goal = iinfo->i_next_alloc_goal;
0857 
0858         if (!goal) {
0859             if (!(goal = pgoal)) /* XXX: what was intended here? */
0860                 goal = iinfo->i_location.logicalBlockNum + 1;
0861         }
0862 
0863         newblocknum = udf_new_block(inode->i_sb, inode,
0864                 iinfo->i_location.partitionReferenceNum,
0865                 goal, err);
0866         if (!newblocknum) {
0867             *err = -ENOSPC;
0868             newblock = 0;
0869             goto out_free;
0870         }
0871         if (isBeyondEOF)
0872             iinfo->i_lenExtents += inode->i_sb->s_blocksize;
0873     }
0874 
0875     /* if the extent the requsted block is located in contains multiple
0876      * blocks, split the extent into at most three extents. blocks prior
0877      * to requested block, requested block, and blocks after requested
0878      * block */
0879     udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
0880 
0881     /* We preallocate blocks only for regular files. It also makes sense
0882      * for directories but there's a problem when to drop the
0883      * preallocation. We might use some delayed work for that but I feel
0884      * it's overengineering for a filesystem like UDF. */
0885     if (S_ISREG(inode->i_mode))
0886         udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
0887 
0888     /* merge any continuous blocks in laarr */
0889     udf_merge_extents(inode, laarr, &endnum);
0890 
0891     /* write back the new extents, inserting new extents if the new number
0892      * of extents is greater than the old number, and deleting extents if
0893      * the new number of extents is less than the old number */
0894     udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
0895 
0896     newblock = udf_get_pblock(inode->i_sb, newblocknum,
0897                 iinfo->i_location.partitionReferenceNum, 0);
0898     if (!newblock) {
0899         *err = -EIO;
0900         goto out_free;
0901     }
0902     *new = 1;
0903     iinfo->i_next_alloc_block = block;
0904     iinfo->i_next_alloc_goal = newblocknum;
0905     inode->i_ctime = current_time(inode);
0906 
0907     if (IS_SYNC(inode))
0908         udf_sync_inode(inode);
0909     else
0910         mark_inode_dirty(inode);
0911 out_free:
0912     brelse(prev_epos.bh);
0913     brelse(cur_epos.bh);
0914     brelse(next_epos.bh);
0915     return newblock;
0916 }
0917 
0918 static void udf_split_extents(struct inode *inode, int *c, int offset,
0919                    udf_pblk_t newblocknum,
0920                    struct kernel_long_ad *laarr, int *endnum)
0921 {
0922     unsigned long blocksize = inode->i_sb->s_blocksize;
0923     unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
0924 
0925     if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
0926         (laarr[*c].extLength >> 30) ==
0927                 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
0928         int curr = *c;
0929         int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
0930                 blocksize - 1) >> blocksize_bits;
0931         int8_t etype = (laarr[curr].extLength >> 30);
0932 
0933         if (blen == 1)
0934             ;
0935         else if (!offset || blen == offset + 1) {
0936             laarr[curr + 2] = laarr[curr + 1];
0937             laarr[curr + 1] = laarr[curr];
0938         } else {
0939             laarr[curr + 3] = laarr[curr + 1];
0940             laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
0941         }
0942 
0943         if (offset) {
0944             if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
0945                 udf_free_blocks(inode->i_sb, inode,
0946                         &laarr[curr].extLocation,
0947                         0, offset);
0948                 laarr[curr].extLength =
0949                     EXT_NOT_RECORDED_NOT_ALLOCATED |
0950                     (offset << blocksize_bits);
0951                 laarr[curr].extLocation.logicalBlockNum = 0;
0952                 laarr[curr].extLocation.
0953                         partitionReferenceNum = 0;
0954             } else
0955                 laarr[curr].extLength = (etype << 30) |
0956                     (offset << blocksize_bits);
0957             curr++;
0958             (*c)++;
0959             (*endnum)++;
0960         }
0961 
0962         laarr[curr].extLocation.logicalBlockNum = newblocknum;
0963         if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
0964             laarr[curr].extLocation.partitionReferenceNum =
0965                 UDF_I(inode)->i_location.partitionReferenceNum;
0966         laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
0967             blocksize;
0968         curr++;
0969 
0970         if (blen != offset + 1) {
0971             if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
0972                 laarr[curr].extLocation.logicalBlockNum +=
0973                                 offset + 1;
0974             laarr[curr].extLength = (etype << 30) |
0975                 ((blen - (offset + 1)) << blocksize_bits);
0976             curr++;
0977             (*endnum)++;
0978         }
0979     }
0980 }
0981 
0982 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
0983                  struct kernel_long_ad *laarr,
0984                  int *endnum)
0985 {
0986     int start, length = 0, currlength = 0, i;
0987 
0988     if (*endnum >= (c + 1)) {
0989         if (!lastblock)
0990             return;
0991         else
0992             start = c;
0993     } else {
0994         if ((laarr[c + 1].extLength >> 30) ==
0995                     (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
0996             start = c + 1;
0997             length = currlength =
0998                 (((laarr[c + 1].extLength &
0999                     UDF_EXTENT_LENGTH_MASK) +
1000                 inode->i_sb->s_blocksize - 1) >>
1001                 inode->i_sb->s_blocksize_bits);
1002         } else
1003             start = c;
1004     }
1005 
1006     for (i = start + 1; i <= *endnum; i++) {
1007         if (i == *endnum) {
1008             if (lastblock)
1009                 length += UDF_DEFAULT_PREALLOC_BLOCKS;
1010         } else if ((laarr[i].extLength >> 30) ==
1011                 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1012             length += (((laarr[i].extLength &
1013                         UDF_EXTENT_LENGTH_MASK) +
1014                     inode->i_sb->s_blocksize - 1) >>
1015                     inode->i_sb->s_blocksize_bits);
1016         } else
1017             break;
1018     }
1019 
1020     if (length) {
1021         int next = laarr[start].extLocation.logicalBlockNum +
1022             (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1023               inode->i_sb->s_blocksize - 1) >>
1024               inode->i_sb->s_blocksize_bits);
1025         int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1026                 laarr[start].extLocation.partitionReferenceNum,
1027                 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1028                 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1029                 currlength);
1030         if (numalloc)   {
1031             if (start == (c + 1))
1032                 laarr[start].extLength +=
1033                     (numalloc <<
1034                      inode->i_sb->s_blocksize_bits);
1035             else {
1036                 memmove(&laarr[c + 2], &laarr[c + 1],
1037                     sizeof(struct long_ad) * (*endnum - (c + 1)));
1038                 (*endnum)++;
1039                 laarr[c + 1].extLocation.logicalBlockNum = next;
1040                 laarr[c + 1].extLocation.partitionReferenceNum =
1041                     laarr[c].extLocation.
1042                             partitionReferenceNum;
1043                 laarr[c + 1].extLength =
1044                     EXT_NOT_RECORDED_ALLOCATED |
1045                     (numalloc <<
1046                      inode->i_sb->s_blocksize_bits);
1047                 start = c + 1;
1048             }
1049 
1050             for (i = start + 1; numalloc && i < *endnum; i++) {
1051                 int elen = ((laarr[i].extLength &
1052                         UDF_EXTENT_LENGTH_MASK) +
1053                         inode->i_sb->s_blocksize - 1) >>
1054                         inode->i_sb->s_blocksize_bits;
1055 
1056                 if (elen > numalloc) {
1057                     laarr[i].extLength -=
1058                         (numalloc <<
1059                          inode->i_sb->s_blocksize_bits);
1060                     numalloc = 0;
1061                 } else {
1062                     numalloc -= elen;
1063                     if (*endnum > (i + 1))
1064                         memmove(&laarr[i],
1065                             &laarr[i + 1],
1066                             sizeof(struct long_ad) *
1067                             (*endnum - (i + 1)));
1068                     i--;
1069                     (*endnum)--;
1070                 }
1071             }
1072             UDF_I(inode)->i_lenExtents +=
1073                 numalloc << inode->i_sb->s_blocksize_bits;
1074         }
1075     }
1076 }
1077 
1078 static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
1079                   int *endnum)
1080 {
1081     int i;
1082     unsigned long blocksize = inode->i_sb->s_blocksize;
1083     unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1084 
1085     for (i = 0; i < (*endnum - 1); i++) {
1086         struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1087         struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1088 
1089         if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1090             (((li->extLength >> 30) ==
1091                 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1092             ((lip1->extLocation.logicalBlockNum -
1093               li->extLocation.logicalBlockNum) ==
1094             (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1095             blocksize - 1) >> blocksize_bits)))) {
1096 
1097             if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1098                 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1099                 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1100                 lip1->extLength = (lip1->extLength -
1101                           (li->extLength &
1102                            UDF_EXTENT_LENGTH_MASK) +
1103                            UDF_EXTENT_LENGTH_MASK) &
1104                             ~(blocksize - 1);
1105                 li->extLength = (li->extLength &
1106                          UDF_EXTENT_FLAG_MASK) +
1107                         (UDF_EXTENT_LENGTH_MASK + 1) -
1108                         blocksize;
1109                 lip1->extLocation.logicalBlockNum =
1110                     li->extLocation.logicalBlockNum +
1111                     ((li->extLength &
1112                         UDF_EXTENT_LENGTH_MASK) >>
1113                         blocksize_bits);
1114             } else {
1115                 li->extLength = lip1->extLength +
1116                     (((li->extLength &
1117                         UDF_EXTENT_LENGTH_MASK) +
1118                      blocksize - 1) & ~(blocksize - 1));
1119                 if (*endnum > (i + 2))
1120                     memmove(&laarr[i + 1], &laarr[i + 2],
1121                         sizeof(struct long_ad) *
1122                         (*endnum - (i + 2)));
1123                 i--;
1124                 (*endnum)--;
1125             }
1126         } else if (((li->extLength >> 30) ==
1127                 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1128                ((lip1->extLength >> 30) ==
1129                 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1130             udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1131                     ((li->extLength &
1132                       UDF_EXTENT_LENGTH_MASK) +
1133                      blocksize - 1) >> blocksize_bits);
1134             li->extLocation.logicalBlockNum = 0;
1135             li->extLocation.partitionReferenceNum = 0;
1136 
1137             if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1138                  (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1139                  blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1140                 lip1->extLength = (lip1->extLength -
1141                            (li->extLength &
1142                            UDF_EXTENT_LENGTH_MASK) +
1143                            UDF_EXTENT_LENGTH_MASK) &
1144                            ~(blocksize - 1);
1145                 li->extLength = (li->extLength &
1146                          UDF_EXTENT_FLAG_MASK) +
1147                         (UDF_EXTENT_LENGTH_MASK + 1) -
1148                         blocksize;
1149             } else {
1150                 li->extLength = lip1->extLength +
1151                     (((li->extLength &
1152                         UDF_EXTENT_LENGTH_MASK) +
1153                       blocksize - 1) & ~(blocksize - 1));
1154                 if (*endnum > (i + 2))
1155                     memmove(&laarr[i + 1], &laarr[i + 2],
1156                         sizeof(struct long_ad) *
1157                         (*endnum - (i + 2)));
1158                 i--;
1159                 (*endnum)--;
1160             }
1161         } else if ((li->extLength >> 30) ==
1162                     (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1163             udf_free_blocks(inode->i_sb, inode,
1164                     &li->extLocation, 0,
1165                     ((li->extLength &
1166                         UDF_EXTENT_LENGTH_MASK) +
1167                      blocksize - 1) >> blocksize_bits);
1168             li->extLocation.logicalBlockNum = 0;
1169             li->extLocation.partitionReferenceNum = 0;
1170             li->extLength = (li->extLength &
1171                         UDF_EXTENT_LENGTH_MASK) |
1172                         EXT_NOT_RECORDED_NOT_ALLOCATED;
1173         }
1174     }
1175 }
1176 
1177 static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1178                    int startnum, int endnum,
1179                    struct extent_position *epos)
1180 {
1181     int start = 0, i;
1182     struct kernel_lb_addr tmploc;
1183     uint32_t tmplen;
1184 
1185     if (startnum > endnum) {
1186         for (i = 0; i < (startnum - endnum); i++)
1187             udf_delete_aext(inode, *epos);
1188     } else if (startnum < endnum) {
1189         for (i = 0; i < (endnum - startnum); i++) {
1190             udf_insert_aext(inode, *epos, laarr[i].extLocation,
1191                     laarr[i].extLength);
1192             udf_next_aext(inode, epos, &laarr[i].extLocation,
1193                       &laarr[i].extLength, 1);
1194             start++;
1195         }
1196     }
1197 
1198     for (i = start; i < endnum; i++) {
1199         udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1200         udf_write_aext(inode, epos, &laarr[i].extLocation,
1201                    laarr[i].extLength, 1);
1202     }
1203 }
1204 
1205 struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
1206                   int create, int *err)
1207 {
1208     struct buffer_head *bh = NULL;
1209 
1210     bh = udf_getblk(inode, block, create, err);
1211     if (!bh)
1212         return NULL;
1213 
1214     if (buffer_uptodate(bh))
1215         return bh;
1216 
1217     ll_rw_block(REQ_OP_READ, 1, &bh);
1218 
1219     wait_on_buffer(bh);
1220     if (buffer_uptodate(bh))
1221         return bh;
1222 
1223     brelse(bh);
1224     *err = -EIO;
1225     return NULL;
1226 }
1227 
1228 int udf_setsize(struct inode *inode, loff_t newsize)
1229 {
1230     int err;
1231     struct udf_inode_info *iinfo;
1232     unsigned int bsize = i_blocksize(inode);
1233 
1234     if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1235           S_ISLNK(inode->i_mode)))
1236         return -EINVAL;
1237     if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1238         return -EPERM;
1239 
1240     iinfo = UDF_I(inode);
1241     if (newsize > inode->i_size) {
1242         down_write(&iinfo->i_data_sem);
1243         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1244             if (bsize <
1245                 (udf_file_entry_alloc_offset(inode) + newsize)) {
1246                 err = udf_expand_file_adinicb(inode);
1247                 if (err)
1248                     return err;
1249                 down_write(&iinfo->i_data_sem);
1250             } else {
1251                 iinfo->i_lenAlloc = newsize;
1252                 goto set_size;
1253             }
1254         }
1255         err = udf_extend_file(inode, newsize);
1256         if (err) {
1257             up_write(&iinfo->i_data_sem);
1258             return err;
1259         }
1260 set_size:
1261         up_write(&iinfo->i_data_sem);
1262         truncate_setsize(inode, newsize);
1263     } else {
1264         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1265             down_write(&iinfo->i_data_sem);
1266             udf_clear_extent_cache(inode);
1267             memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
1268                    0x00, bsize - newsize -
1269                    udf_file_entry_alloc_offset(inode));
1270             iinfo->i_lenAlloc = newsize;
1271             truncate_setsize(inode, newsize);
1272             up_write(&iinfo->i_data_sem);
1273             goto update_time;
1274         }
1275         err = block_truncate_page(inode->i_mapping, newsize,
1276                       udf_get_block);
1277         if (err)
1278             return err;
1279         truncate_setsize(inode, newsize);
1280         down_write(&iinfo->i_data_sem);
1281         udf_clear_extent_cache(inode);
1282         err = udf_truncate_extents(inode);
1283         up_write(&iinfo->i_data_sem);
1284         if (err)
1285             return err;
1286     }
1287 update_time:
1288     inode->i_mtime = inode->i_ctime = current_time(inode);
1289     if (IS_SYNC(inode))
1290         udf_sync_inode(inode);
1291     else
1292         mark_inode_dirty(inode);
1293     return 0;
1294 }
1295 
1296 /*
1297  * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1298  * arbitrary - just that we hopefully don't limit any real use of rewritten
1299  * inode on write-once media but avoid looping for too long on corrupted media.
1300  */
1301 #define UDF_MAX_ICB_NESTING 1024
1302 
1303 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1304 {
1305     struct buffer_head *bh = NULL;
1306     struct fileEntry *fe;
1307     struct extendedFileEntry *efe;
1308     uint16_t ident;
1309     struct udf_inode_info *iinfo = UDF_I(inode);
1310     struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1311     struct kernel_lb_addr *iloc = &iinfo->i_location;
1312     unsigned int link_count;
1313     unsigned int indirections = 0;
1314     int bs = inode->i_sb->s_blocksize;
1315     int ret = -EIO;
1316     uint32_t uid, gid;
1317 
1318 reread:
1319     if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1320         udf_debug("partition reference: %u > logical volume partitions: %u\n",
1321               iloc->partitionReferenceNum, sbi->s_partitions);
1322         return -EIO;
1323     }
1324 
1325     if (iloc->logicalBlockNum >=
1326         sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1327         udf_debug("block=%u, partition=%u out of range\n",
1328               iloc->logicalBlockNum, iloc->partitionReferenceNum);
1329         return -EIO;
1330     }
1331 
1332     /*
1333      * Set defaults, but the inode is still incomplete!
1334      * Note: get_new_inode() sets the following on a new inode:
1335      *      i_sb = sb
1336      *      i_no = ino
1337      *      i_flags = sb->s_flags
1338      *      i_state = 0
1339      * clean_inode(): zero fills and sets
1340      *      i_count = 1
1341      *      i_nlink = 1
1342      *      i_op = NULL;
1343      */
1344     bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1345     if (!bh) {
1346         udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
1347         return -EIO;
1348     }
1349 
1350     if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1351         ident != TAG_IDENT_USE) {
1352         udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
1353             inode->i_ino, ident);
1354         goto out;
1355     }
1356 
1357     fe = (struct fileEntry *)bh->b_data;
1358     efe = (struct extendedFileEntry *)bh->b_data;
1359 
1360     if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1361         struct buffer_head *ibh;
1362 
1363         ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1364         if (ident == TAG_IDENT_IE && ibh) {
1365             struct kernel_lb_addr loc;
1366             struct indirectEntry *ie;
1367 
1368             ie = (struct indirectEntry *)ibh->b_data;
1369             loc = lelb_to_cpu(ie->indirectICB.extLocation);
1370 
1371             if (ie->indirectICB.extLength) {
1372                 brelse(ibh);
1373                 memcpy(&iinfo->i_location, &loc,
1374                        sizeof(struct kernel_lb_addr));
1375                 if (++indirections > UDF_MAX_ICB_NESTING) {
1376                     udf_err(inode->i_sb,
1377                         "too many ICBs in ICB hierarchy"
1378                         " (max %d supported)\n",
1379                         UDF_MAX_ICB_NESTING);
1380                     goto out;
1381                 }
1382                 brelse(bh);
1383                 goto reread;
1384             }
1385         }
1386         brelse(ibh);
1387     } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1388         udf_err(inode->i_sb, "unsupported strategy type: %u\n",
1389             le16_to_cpu(fe->icbTag.strategyType));
1390         goto out;
1391     }
1392     if (fe->icbTag.strategyType == cpu_to_le16(4))
1393         iinfo->i_strat4096 = 0;
1394     else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1395         iinfo->i_strat4096 = 1;
1396 
1397     iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1398                             ICBTAG_FLAG_AD_MASK;
1399     if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1400         iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1401         iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1402         ret = -EIO;
1403         goto out;
1404     }
1405     iinfo->i_unique = 0;
1406     iinfo->i_lenEAttr = 0;
1407     iinfo->i_lenExtents = 0;
1408     iinfo->i_lenAlloc = 0;
1409     iinfo->i_next_alloc_block = 0;
1410     iinfo->i_next_alloc_goal = 0;
1411     if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1412         iinfo->i_efe = 1;
1413         iinfo->i_use = 0;
1414         ret = udf_alloc_i_data(inode, bs -
1415                     sizeof(struct extendedFileEntry));
1416         if (ret)
1417             goto out;
1418         memcpy(iinfo->i_data,
1419                bh->b_data + sizeof(struct extendedFileEntry),
1420                bs - sizeof(struct extendedFileEntry));
1421     } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1422         iinfo->i_efe = 0;
1423         iinfo->i_use = 0;
1424         ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1425         if (ret)
1426             goto out;
1427         memcpy(iinfo->i_data,
1428                bh->b_data + sizeof(struct fileEntry),
1429                bs - sizeof(struct fileEntry));
1430     } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1431         iinfo->i_efe = 0;
1432         iinfo->i_use = 1;
1433         iinfo->i_lenAlloc = le32_to_cpu(
1434                 ((struct unallocSpaceEntry *)bh->b_data)->
1435                  lengthAllocDescs);
1436         ret = udf_alloc_i_data(inode, bs -
1437                     sizeof(struct unallocSpaceEntry));
1438         if (ret)
1439             goto out;
1440         memcpy(iinfo->i_data,
1441                bh->b_data + sizeof(struct unallocSpaceEntry),
1442                bs - sizeof(struct unallocSpaceEntry));
1443         return 0;
1444     }
1445 
1446     ret = -EIO;
1447     read_lock(&sbi->s_cred_lock);
1448     uid = le32_to_cpu(fe->uid);
1449     if (uid == UDF_INVALID_ID ||
1450         UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1451         inode->i_uid = sbi->s_uid;
1452     else
1453         i_uid_write(inode, uid);
1454 
1455     gid = le32_to_cpu(fe->gid);
1456     if (gid == UDF_INVALID_ID ||
1457         UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1458         inode->i_gid = sbi->s_gid;
1459     else
1460         i_gid_write(inode, gid);
1461 
1462     if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1463             sbi->s_fmode != UDF_INVALID_MODE)
1464         inode->i_mode = sbi->s_fmode;
1465     else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1466             sbi->s_dmode != UDF_INVALID_MODE)
1467         inode->i_mode = sbi->s_dmode;
1468     else
1469         inode->i_mode = udf_convert_permissions(fe);
1470     inode->i_mode &= ~sbi->s_umask;
1471     iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
1472 
1473     read_unlock(&sbi->s_cred_lock);
1474 
1475     link_count = le16_to_cpu(fe->fileLinkCount);
1476     if (!link_count) {
1477         if (!hidden_inode) {
1478             ret = -ESTALE;
1479             goto out;
1480         }
1481         link_count = 1;
1482     }
1483     set_nlink(inode, link_count);
1484 
1485     inode->i_size = le64_to_cpu(fe->informationLength);
1486     iinfo->i_lenExtents = inode->i_size;
1487 
1488     if (iinfo->i_efe == 0) {
1489         inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1490             (inode->i_sb->s_blocksize_bits - 9);
1491 
1492         udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1493         udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
1494         udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
1495 
1496         iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1497         iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1498         iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1499         iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1500         iinfo->i_streamdir = 0;
1501         iinfo->i_lenStreams = 0;
1502     } else {
1503         inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1504             (inode->i_sb->s_blocksize_bits - 9);
1505 
1506         udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
1507         udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
1508         udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
1509         udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
1510 
1511         iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1512         iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1513         iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1514         iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1515 
1516         /* Named streams */
1517         iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
1518         iinfo->i_locStreamdir =
1519             lelb_to_cpu(efe->streamDirectoryICB.extLocation);
1520         iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
1521         if (iinfo->i_lenStreams >= inode->i_size)
1522             iinfo->i_lenStreams -= inode->i_size;
1523         else
1524             iinfo->i_lenStreams = 0;
1525     }
1526     inode->i_generation = iinfo->i_unique;
1527 
1528     /*
1529      * Sanity check length of allocation descriptors and extended attrs to
1530      * avoid integer overflows
1531      */
1532     if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1533         goto out;
1534     /* Now do exact checks */
1535     if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1536         goto out;
1537     /* Sanity checks for files in ICB so that we don't get confused later */
1538     if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1539         /*
1540          * For file in ICB data is stored in allocation descriptor
1541          * so sizes should match
1542          */
1543         if (iinfo->i_lenAlloc != inode->i_size)
1544             goto out;
1545         /* File in ICB has to fit in there... */
1546         if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1547             goto out;
1548     }
1549 
1550     switch (fe->icbTag.fileType) {
1551     case ICBTAG_FILE_TYPE_DIRECTORY:
1552         inode->i_op = &udf_dir_inode_operations;
1553         inode->i_fop = &udf_dir_operations;
1554         inode->i_mode |= S_IFDIR;
1555         inc_nlink(inode);
1556         break;
1557     case ICBTAG_FILE_TYPE_REALTIME:
1558     case ICBTAG_FILE_TYPE_REGULAR:
1559     case ICBTAG_FILE_TYPE_UNDEF:
1560     case ICBTAG_FILE_TYPE_VAT20:
1561         if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1562             inode->i_data.a_ops = &udf_adinicb_aops;
1563         else
1564             inode->i_data.a_ops = &udf_aops;
1565         inode->i_op = &udf_file_inode_operations;
1566         inode->i_fop = &udf_file_operations;
1567         inode->i_mode |= S_IFREG;
1568         break;
1569     case ICBTAG_FILE_TYPE_BLOCK:
1570         inode->i_mode |= S_IFBLK;
1571         break;
1572     case ICBTAG_FILE_TYPE_CHAR:
1573         inode->i_mode |= S_IFCHR;
1574         break;
1575     case ICBTAG_FILE_TYPE_FIFO:
1576         init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1577         break;
1578     case ICBTAG_FILE_TYPE_SOCKET:
1579         init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1580         break;
1581     case ICBTAG_FILE_TYPE_SYMLINK:
1582         inode->i_data.a_ops = &udf_symlink_aops;
1583         inode->i_op = &udf_symlink_inode_operations;
1584         inode_nohighmem(inode);
1585         inode->i_mode = S_IFLNK | 0777;
1586         break;
1587     case ICBTAG_FILE_TYPE_MAIN:
1588         udf_debug("METADATA FILE-----\n");
1589         break;
1590     case ICBTAG_FILE_TYPE_MIRROR:
1591         udf_debug("METADATA MIRROR FILE-----\n");
1592         break;
1593     case ICBTAG_FILE_TYPE_BITMAP:
1594         udf_debug("METADATA BITMAP FILE-----\n");
1595         break;
1596     default:
1597         udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
1598             inode->i_ino, fe->icbTag.fileType);
1599         goto out;
1600     }
1601     if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1602         struct deviceSpec *dsea =
1603             (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1604         if (dsea) {
1605             init_special_inode(inode, inode->i_mode,
1606                 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1607                       le32_to_cpu(dsea->minorDeviceIdent)));
1608             /* Developer ID ??? */
1609         } else
1610             goto out;
1611     }
1612     ret = 0;
1613 out:
1614     brelse(bh);
1615     return ret;
1616 }
1617 
1618 static int udf_alloc_i_data(struct inode *inode, size_t size)
1619 {
1620     struct udf_inode_info *iinfo = UDF_I(inode);
1621     iinfo->i_data = kmalloc(size, GFP_KERNEL);
1622     if (!iinfo->i_data)
1623         return -ENOMEM;
1624     return 0;
1625 }
1626 
1627 static umode_t udf_convert_permissions(struct fileEntry *fe)
1628 {
1629     umode_t mode;
1630     uint32_t permissions;
1631     uint32_t flags;
1632 
1633     permissions = le32_to_cpu(fe->permissions);
1634     flags = le16_to_cpu(fe->icbTag.flags);
1635 
1636     mode =  ((permissions) & 0007) |
1637         ((permissions >> 2) & 0070) |
1638         ((permissions >> 4) & 0700) |
1639         ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1640         ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1641         ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1642 
1643     return mode;
1644 }
1645 
1646 void udf_update_extra_perms(struct inode *inode, umode_t mode)
1647 {
1648     struct udf_inode_info *iinfo = UDF_I(inode);
1649 
1650     /*
1651      * UDF 2.01 sec. 3.3.3.3 Note 2:
1652      * In Unix, delete permission tracks write
1653      */
1654     iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
1655     if (mode & 0200)
1656         iinfo->i_extraPerms |= FE_PERM_U_DELETE;
1657     if (mode & 0020)
1658         iinfo->i_extraPerms |= FE_PERM_G_DELETE;
1659     if (mode & 0002)
1660         iinfo->i_extraPerms |= FE_PERM_O_DELETE;
1661 }
1662 
1663 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1664 {
1665     return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1666 }
1667 
1668 static int udf_sync_inode(struct inode *inode)
1669 {
1670     return udf_update_inode(inode, 1);
1671 }
1672 
1673 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
1674 {
1675     if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1676         (iinfo->i_crtime.tv_sec == time.tv_sec &&
1677          iinfo->i_crtime.tv_nsec > time.tv_nsec))
1678         iinfo->i_crtime = time;
1679 }
1680 
1681 static int udf_update_inode(struct inode *inode, int do_sync)
1682 {
1683     struct buffer_head *bh = NULL;
1684     struct fileEntry *fe;
1685     struct extendedFileEntry *efe;
1686     uint64_t lb_recorded;
1687     uint32_t udfperms;
1688     uint16_t icbflags;
1689     uint16_t crclen;
1690     int err = 0;
1691     struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1692     unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1693     struct udf_inode_info *iinfo = UDF_I(inode);
1694 
1695     bh = udf_tgetblk(inode->i_sb,
1696             udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1697     if (!bh) {
1698         udf_debug("getblk failure\n");
1699         return -EIO;
1700     }
1701 
1702     lock_buffer(bh);
1703     memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1704     fe = (struct fileEntry *)bh->b_data;
1705     efe = (struct extendedFileEntry *)bh->b_data;
1706 
1707     if (iinfo->i_use) {
1708         struct unallocSpaceEntry *use =
1709             (struct unallocSpaceEntry *)bh->b_data;
1710 
1711         use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1712         memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1713                iinfo->i_data, inode->i_sb->s_blocksize -
1714                     sizeof(struct unallocSpaceEntry));
1715         use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1716         crclen = sizeof(struct unallocSpaceEntry);
1717 
1718         goto finish;
1719     }
1720 
1721     if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1722         fe->uid = cpu_to_le32(UDF_INVALID_ID);
1723     else
1724         fe->uid = cpu_to_le32(i_uid_read(inode));
1725 
1726     if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1727         fe->gid = cpu_to_le32(UDF_INVALID_ID);
1728     else
1729         fe->gid = cpu_to_le32(i_gid_read(inode));
1730 
1731     udfperms = ((inode->i_mode & 0007)) |
1732            ((inode->i_mode & 0070) << 2) |
1733            ((inode->i_mode & 0700) << 4);
1734 
1735     udfperms |= iinfo->i_extraPerms;
1736     fe->permissions = cpu_to_le32(udfperms);
1737 
1738     if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1739         fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1740     else
1741         fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1742 
1743     fe->informationLength = cpu_to_le64(inode->i_size);
1744 
1745     if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1746         struct regid *eid;
1747         struct deviceSpec *dsea =
1748             (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1749         if (!dsea) {
1750             dsea = (struct deviceSpec *)
1751                 udf_add_extendedattr(inode,
1752                              sizeof(struct deviceSpec) +
1753                              sizeof(struct regid), 12, 0x3);
1754             dsea->attrType = cpu_to_le32(12);
1755             dsea->attrSubtype = 1;
1756             dsea->attrLength = cpu_to_le32(
1757                         sizeof(struct deviceSpec) +
1758                         sizeof(struct regid));
1759             dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1760         }
1761         eid = (struct regid *)dsea->impUse;
1762         memset(eid, 0, sizeof(*eid));
1763         strcpy(eid->ident, UDF_ID_DEVELOPER);
1764         eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1765         eid->identSuffix[1] = UDF_OS_ID_LINUX;
1766         dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1767         dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1768     }
1769 
1770     if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1771         lb_recorded = 0; /* No extents => no blocks! */
1772     else
1773         lb_recorded =
1774             (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1775             (blocksize_bits - 9);
1776 
1777     if (iinfo->i_efe == 0) {
1778         memcpy(bh->b_data + sizeof(struct fileEntry),
1779                iinfo->i_data,
1780                inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1781         fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1782 
1783         udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1784         udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1785         udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1786         memset(&(fe->impIdent), 0, sizeof(struct regid));
1787         strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1788         fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1789         fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1790         fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1791         fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1792         fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1793         fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1794         fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1795         crclen = sizeof(struct fileEntry);
1796     } else {
1797         memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1798                iinfo->i_data,
1799                inode->i_sb->s_blocksize -
1800                     sizeof(struct extendedFileEntry));
1801         efe->objectSize =
1802             cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
1803         efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1804 
1805         if (iinfo->i_streamdir) {
1806             struct long_ad *icb_lad = &efe->streamDirectoryICB;
1807 
1808             icb_lad->extLocation =
1809                 cpu_to_lelb(iinfo->i_locStreamdir);
1810             icb_lad->extLength =
1811                 cpu_to_le32(inode->i_sb->s_blocksize);
1812         }
1813 
1814         udf_adjust_time(iinfo, inode->i_atime);
1815         udf_adjust_time(iinfo, inode->i_mtime);
1816         udf_adjust_time(iinfo, inode->i_ctime);
1817 
1818         udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1819         udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1820         udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1821         udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1822 
1823         memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
1824         strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1825         efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1826         efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1827         efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1828         efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1829         efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1830         efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1831         efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1832         crclen = sizeof(struct extendedFileEntry);
1833     }
1834 
1835 finish:
1836     if (iinfo->i_strat4096) {
1837         fe->icbTag.strategyType = cpu_to_le16(4096);
1838         fe->icbTag.strategyParameter = cpu_to_le16(1);
1839         fe->icbTag.numEntries = cpu_to_le16(2);
1840     } else {
1841         fe->icbTag.strategyType = cpu_to_le16(4);
1842         fe->icbTag.numEntries = cpu_to_le16(1);
1843     }
1844 
1845     if (iinfo->i_use)
1846         fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1847     else if (S_ISDIR(inode->i_mode))
1848         fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1849     else if (S_ISREG(inode->i_mode))
1850         fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1851     else if (S_ISLNK(inode->i_mode))
1852         fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1853     else if (S_ISBLK(inode->i_mode))
1854         fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1855     else if (S_ISCHR(inode->i_mode))
1856         fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1857     else if (S_ISFIFO(inode->i_mode))
1858         fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1859     else if (S_ISSOCK(inode->i_mode))
1860         fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1861 
1862     icbflags =  iinfo->i_alloc_type |
1863             ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1864             ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1865             ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1866             (le16_to_cpu(fe->icbTag.flags) &
1867                 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1868                 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1869 
1870     fe->icbTag.flags = cpu_to_le16(icbflags);
1871     if (sbi->s_udfrev >= 0x0200)
1872         fe->descTag.descVersion = cpu_to_le16(3);
1873     else
1874         fe->descTag.descVersion = cpu_to_le16(2);
1875     fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1876     fe->descTag.tagLocation = cpu_to_le32(
1877                     iinfo->i_location.logicalBlockNum);
1878     crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1879     fe->descTag.descCRCLength = cpu_to_le16(crclen);
1880     fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1881                           crclen));
1882     fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1883 
1884     set_buffer_uptodate(bh);
1885     unlock_buffer(bh);
1886 
1887     /* write the data blocks */
1888     mark_buffer_dirty(bh);
1889     if (do_sync) {
1890         sync_dirty_buffer(bh);
1891         if (buffer_write_io_error(bh)) {
1892             udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1893                  inode->i_ino);
1894             err = -EIO;
1895         }
1896     }
1897     brelse(bh);
1898 
1899     return err;
1900 }
1901 
1902 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1903              bool hidden_inode)
1904 {
1905     unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1906     struct inode *inode = iget_locked(sb, block);
1907     int err;
1908 
1909     if (!inode)
1910         return ERR_PTR(-ENOMEM);
1911 
1912     if (!(inode->i_state & I_NEW))
1913         return inode;
1914 
1915     memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1916     err = udf_read_inode(inode, hidden_inode);
1917     if (err < 0) {
1918         iget_failed(inode);
1919         return ERR_PTR(err);
1920     }
1921     unlock_new_inode(inode);
1922 
1923     return inode;
1924 }
1925 
1926 int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
1927                 struct extent_position *epos)
1928 {
1929     struct super_block *sb = inode->i_sb;
1930     struct buffer_head *bh;
1931     struct allocExtDesc *aed;
1932     struct extent_position nepos;
1933     struct kernel_lb_addr neloc;
1934     int ver, adsize;
1935 
1936     if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1937         adsize = sizeof(struct short_ad);
1938     else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1939         adsize = sizeof(struct long_ad);
1940     else
1941         return -EIO;
1942 
1943     neloc.logicalBlockNum = block;
1944     neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1945 
1946     bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1947     if (!bh)
1948         return -EIO;
1949     lock_buffer(bh);
1950     memset(bh->b_data, 0x00, sb->s_blocksize);
1951     set_buffer_uptodate(bh);
1952     unlock_buffer(bh);
1953     mark_buffer_dirty_inode(bh, inode);
1954 
1955     aed = (struct allocExtDesc *)(bh->b_data);
1956     if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1957         aed->previousAllocExtLocation =
1958                 cpu_to_le32(epos->block.logicalBlockNum);
1959     }
1960     aed->lengthAllocDescs = cpu_to_le32(0);
1961     if (UDF_SB(sb)->s_udfrev >= 0x0200)
1962         ver = 3;
1963     else
1964         ver = 2;
1965     udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1966             sizeof(struct tag));
1967 
1968     nepos.block = neloc;
1969     nepos.offset = sizeof(struct allocExtDesc);
1970     nepos.bh = bh;
1971 
1972     /*
1973      * Do we have to copy current last extent to make space for indirect
1974      * one?
1975      */
1976     if (epos->offset + adsize > sb->s_blocksize) {
1977         struct kernel_lb_addr cp_loc;
1978         uint32_t cp_len;
1979         int cp_type;
1980 
1981         epos->offset -= adsize;
1982         cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1983         cp_len |= ((uint32_t)cp_type) << 30;
1984 
1985         __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1986         udf_write_aext(inode, epos, &nepos.block,
1987                    sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
1988     } else {
1989         __udf_add_aext(inode, epos, &nepos.block,
1990                    sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
1991     }
1992 
1993     brelse(epos->bh);
1994     *epos = nepos;
1995 
1996     return 0;
1997 }
1998 
1999 /*
2000  * Append extent at the given position - should be the first free one in inode
2001  * / indirect extent. This function assumes there is enough space in the inode
2002  * or indirect extent. Use udf_add_aext() if you didn't check for this before.
2003  */
2004 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
2005            struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2006 {
2007     struct udf_inode_info *iinfo = UDF_I(inode);
2008     struct allocExtDesc *aed;
2009     int adsize;
2010 
2011     if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2012         adsize = sizeof(struct short_ad);
2013     else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2014         adsize = sizeof(struct long_ad);
2015     else
2016         return -EIO;
2017 
2018     if (!epos->bh) {
2019         WARN_ON(iinfo->i_lenAlloc !=
2020             epos->offset - udf_file_entry_alloc_offset(inode));
2021     } else {
2022         aed = (struct allocExtDesc *)epos->bh->b_data;
2023         WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
2024             epos->offset - sizeof(struct allocExtDesc));
2025         WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
2026     }
2027 
2028     udf_write_aext(inode, epos, eloc, elen, inc);
2029 
2030     if (!epos->bh) {
2031         iinfo->i_lenAlloc += adsize;
2032         mark_inode_dirty(inode);
2033     } else {
2034         aed = (struct allocExtDesc *)epos->bh->b_data;
2035         le32_add_cpu(&aed->lengthAllocDescs, adsize);
2036         if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2037                 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2038             udf_update_tag(epos->bh->b_data,
2039                     epos->offset + (inc ? 0 : adsize));
2040         else
2041             udf_update_tag(epos->bh->b_data,
2042                     sizeof(struct allocExtDesc));
2043         mark_buffer_dirty_inode(epos->bh, inode);
2044     }
2045 
2046     return 0;
2047 }
2048 
2049 /*
2050  * Append extent at given position - should be the first free one in inode
2051  * / indirect extent. Takes care of allocating and linking indirect blocks.
2052  */
2053 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2054          struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2055 {
2056     int adsize;
2057     struct super_block *sb = inode->i_sb;
2058 
2059     if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2060         adsize = sizeof(struct short_ad);
2061     else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2062         adsize = sizeof(struct long_ad);
2063     else
2064         return -EIO;
2065 
2066     if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2067         int err;
2068         udf_pblk_t new_block;
2069 
2070         new_block = udf_new_block(sb, NULL,
2071                       epos->block.partitionReferenceNum,
2072                       epos->block.logicalBlockNum, &err);
2073         if (!new_block)
2074             return -ENOSPC;
2075 
2076         err = udf_setup_indirect_aext(inode, new_block, epos);
2077         if (err)
2078             return err;
2079     }
2080 
2081     return __udf_add_aext(inode, epos, eloc, elen, inc);
2082 }
2083 
2084 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2085             struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2086 {
2087     int adsize;
2088     uint8_t *ptr;
2089     struct short_ad *sad;
2090     struct long_ad *lad;
2091     struct udf_inode_info *iinfo = UDF_I(inode);
2092 
2093     if (!epos->bh)
2094         ptr = iinfo->i_data + epos->offset -
2095             udf_file_entry_alloc_offset(inode) +
2096             iinfo->i_lenEAttr;
2097     else
2098         ptr = epos->bh->b_data + epos->offset;
2099 
2100     switch (iinfo->i_alloc_type) {
2101     case ICBTAG_FLAG_AD_SHORT:
2102         sad = (struct short_ad *)ptr;
2103         sad->extLength = cpu_to_le32(elen);
2104         sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2105         adsize = sizeof(struct short_ad);
2106         break;
2107     case ICBTAG_FLAG_AD_LONG:
2108         lad = (struct long_ad *)ptr;
2109         lad->extLength = cpu_to_le32(elen);
2110         lad->extLocation = cpu_to_lelb(*eloc);
2111         memset(lad->impUse, 0x00, sizeof(lad->impUse));
2112         adsize = sizeof(struct long_ad);
2113         break;
2114     default:
2115         return;
2116     }
2117 
2118     if (epos->bh) {
2119         if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2120             UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2121             struct allocExtDesc *aed =
2122                 (struct allocExtDesc *)epos->bh->b_data;
2123             udf_update_tag(epos->bh->b_data,
2124                        le32_to_cpu(aed->lengthAllocDescs) +
2125                        sizeof(struct allocExtDesc));
2126         }
2127         mark_buffer_dirty_inode(epos->bh, inode);
2128     } else {
2129         mark_inode_dirty(inode);
2130     }
2131 
2132     if (inc)
2133         epos->offset += adsize;
2134 }
2135 
2136 /*
2137  * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2138  * someone does some weird stuff.
2139  */
2140 #define UDF_MAX_INDIR_EXTS 16
2141 
2142 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2143              struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2144 {
2145     int8_t etype;
2146     unsigned int indirections = 0;
2147 
2148     while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2149            (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
2150         udf_pblk_t block;
2151 
2152         if (++indirections > UDF_MAX_INDIR_EXTS) {
2153             udf_err(inode->i_sb,
2154                 "too many indirect extents in inode %lu\n",
2155                 inode->i_ino);
2156             return -1;
2157         }
2158 
2159         epos->block = *eloc;
2160         epos->offset = sizeof(struct allocExtDesc);
2161         brelse(epos->bh);
2162         block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2163         epos->bh = udf_tread(inode->i_sb, block);
2164         if (!epos->bh) {
2165             udf_debug("reading block %u failed!\n", block);
2166             return -1;
2167         }
2168     }
2169 
2170     return etype;
2171 }
2172 
2173 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2174             struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2175 {
2176     int alen;
2177     int8_t etype;
2178     uint8_t *ptr;
2179     struct short_ad *sad;
2180     struct long_ad *lad;
2181     struct udf_inode_info *iinfo = UDF_I(inode);
2182 
2183     if (!epos->bh) {
2184         if (!epos->offset)
2185             epos->offset = udf_file_entry_alloc_offset(inode);
2186         ptr = iinfo->i_data + epos->offset -
2187             udf_file_entry_alloc_offset(inode) +
2188             iinfo->i_lenEAttr;
2189         alen = udf_file_entry_alloc_offset(inode) +
2190                             iinfo->i_lenAlloc;
2191     } else {
2192         if (!epos->offset)
2193             epos->offset = sizeof(struct allocExtDesc);
2194         ptr = epos->bh->b_data + epos->offset;
2195         alen = sizeof(struct allocExtDesc) +
2196             le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2197                             lengthAllocDescs);
2198     }
2199 
2200     switch (iinfo->i_alloc_type) {
2201     case ICBTAG_FLAG_AD_SHORT:
2202         sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2203         if (!sad)
2204             return -1;
2205         etype = le32_to_cpu(sad->extLength) >> 30;
2206         eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2207         eloc->partitionReferenceNum =
2208                 iinfo->i_location.partitionReferenceNum;
2209         *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2210         break;
2211     case ICBTAG_FLAG_AD_LONG:
2212         lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2213         if (!lad)
2214             return -1;
2215         etype = le32_to_cpu(lad->extLength) >> 30;
2216         *eloc = lelb_to_cpu(lad->extLocation);
2217         *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2218         break;
2219     default:
2220         udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
2221         return -1;
2222     }
2223 
2224     return etype;
2225 }
2226 
2227 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2228                   struct kernel_lb_addr neloc, uint32_t nelen)
2229 {
2230     struct kernel_lb_addr oeloc;
2231     uint32_t oelen;
2232     int8_t etype;
2233 
2234     if (epos.bh)
2235         get_bh(epos.bh);
2236 
2237     while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2238         udf_write_aext(inode, &epos, &neloc, nelen, 1);
2239         neloc = oeloc;
2240         nelen = (etype << 30) | oelen;
2241     }
2242     udf_add_aext(inode, &epos, &neloc, nelen, 1);
2243     brelse(epos.bh);
2244 
2245     return (nelen >> 30);
2246 }
2247 
2248 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
2249 {
2250     struct extent_position oepos;
2251     int adsize;
2252     int8_t etype;
2253     struct allocExtDesc *aed;
2254     struct udf_inode_info *iinfo;
2255     struct kernel_lb_addr eloc;
2256     uint32_t elen;
2257 
2258     if (epos.bh) {
2259         get_bh(epos.bh);
2260         get_bh(epos.bh);
2261     }
2262 
2263     iinfo = UDF_I(inode);
2264     if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2265         adsize = sizeof(struct short_ad);
2266     else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2267         adsize = sizeof(struct long_ad);
2268     else
2269         adsize = 0;
2270 
2271     oepos = epos;
2272     if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2273         return -1;
2274 
2275     while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2276         udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2277         if (oepos.bh != epos.bh) {
2278             oepos.block = epos.block;
2279             brelse(oepos.bh);
2280             get_bh(epos.bh);
2281             oepos.bh = epos.bh;
2282             oepos.offset = epos.offset - adsize;
2283         }
2284     }
2285     memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2286     elen = 0;
2287 
2288     if (epos.bh != oepos.bh) {
2289         udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2290         udf_write_aext(inode, &oepos, &eloc, elen, 1);
2291         udf_write_aext(inode, &oepos, &eloc, elen, 1);
2292         if (!oepos.bh) {
2293             iinfo->i_lenAlloc -= (adsize * 2);
2294             mark_inode_dirty(inode);
2295         } else {
2296             aed = (struct allocExtDesc *)oepos.bh->b_data;
2297             le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2298             if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2299                 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2300                 udf_update_tag(oepos.bh->b_data,
2301                         oepos.offset - (2 * adsize));
2302             else
2303                 udf_update_tag(oepos.bh->b_data,
2304                         sizeof(struct allocExtDesc));
2305             mark_buffer_dirty_inode(oepos.bh, inode);
2306         }
2307     } else {
2308         udf_write_aext(inode, &oepos, &eloc, elen, 1);
2309         if (!oepos.bh) {
2310             iinfo->i_lenAlloc -= adsize;
2311             mark_inode_dirty(inode);
2312         } else {
2313             aed = (struct allocExtDesc *)oepos.bh->b_data;
2314             le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2315             if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2316                 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2317                 udf_update_tag(oepos.bh->b_data,
2318                         epos.offset - adsize);
2319             else
2320                 udf_update_tag(oepos.bh->b_data,
2321                         sizeof(struct allocExtDesc));
2322             mark_buffer_dirty_inode(oepos.bh, inode);
2323         }
2324     }
2325 
2326     brelse(epos.bh);
2327     brelse(oepos.bh);
2328 
2329     return (elen >> 30);
2330 }
2331 
2332 int8_t inode_bmap(struct inode *inode, sector_t block,
2333           struct extent_position *pos, struct kernel_lb_addr *eloc,
2334           uint32_t *elen, sector_t *offset)
2335 {
2336     unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2337     loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2338     int8_t etype;
2339     struct udf_inode_info *iinfo;
2340 
2341     iinfo = UDF_I(inode);
2342     if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2343         pos->offset = 0;
2344         pos->block = iinfo->i_location;
2345         pos->bh = NULL;
2346     }
2347     *elen = 0;
2348     do {
2349         etype = udf_next_aext(inode, pos, eloc, elen, 1);
2350         if (etype == -1) {
2351             *offset = (bcount - lbcount) >> blocksize_bits;
2352             iinfo->i_lenExtents = lbcount;
2353             return -1;
2354         }
2355         lbcount += *elen;
2356     } while (lbcount <= bcount);
2357     /* update extent cache */
2358     udf_update_extent_cache(inode, lbcount - *elen, pos);
2359     *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2360 
2361     return etype;
2362 }
2363 
2364 udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
2365 {
2366     struct kernel_lb_addr eloc;
2367     uint32_t elen;
2368     sector_t offset;
2369     struct extent_position epos = {};
2370     udf_pblk_t ret;
2371 
2372     down_read(&UDF_I(inode)->i_data_sem);
2373 
2374     if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2375                         (EXT_RECORDED_ALLOCATED >> 30))
2376         ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2377     else
2378         ret = 0;
2379 
2380     up_read(&UDF_I(inode)->i_data_sem);
2381     brelse(epos.bh);
2382 
2383     if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2384         return udf_fixed_to_variable(ret);
2385     else
2386         return ret;
2387 }