linux/fs/udf/inode.c
<<
>>
Prefs
   1/*
   2 * inode.c
   3 *
   4 * PURPOSE
   5 *  Inode handling routines for the OSTA-UDF(tm) filesystem.
   6 *
   7 * COPYRIGHT
   8 *  This file is distributed under the terms of the GNU General Public
   9 *  License (GPL). Copies of the GPL can be obtained from:
  10 *    ftp://prep.ai.mit.edu/pub/gnu/GPL
  11 *  Each contributing author retains all rights to their own work.
  12 *
  13 *  (C) 1998 Dave Boynton
  14 *  (C) 1998-2004 Ben Fennema
  15 *  (C) 1999-2000 Stelias Computing Inc
  16 *
  17 * HISTORY
  18 *
  19 *  10/04/98 dgb  Added rudimentary directory functions
  20 *  10/07/98      Fully working udf_block_map! It works!
  21 *  11/25/98      bmap altered to better support extents
  22 *  12/06/98 blf  partition support in udf_iget, udf_block_map
  23 *                and udf_read_inode
  24 *  12/12/98      rewrote udf_block_map to handle next extents and descs across
  25 *                block boundaries (which is not actually allowed)
  26 *  12/20/98      added support for strategy 4096
  27 *  03/07/99      rewrote udf_block_map (again)
  28 *                New funcs, inode_bmap, udf_next_aext
  29 *  04/19/99      Support for writing device EA's for major/minor #
  30 */
  31
  32#include "udfdecl.h"
  33#include <linux/mm.h>
  34#include <linux/module.h>
  35#include <linux/pagemap.h>
  36#include <linux/buffer_head.h>
  37#include <linux/writeback.h>
  38#include <linux/slab.h>
  39#include <linux/crc-itu-t.h>
  40#include <linux/mpage.h>
  41
  42#include "udf_i.h"
  43#include "udf_sb.h"
  44
  45MODULE_AUTHOR("Ben Fennema");
  46MODULE_DESCRIPTION("Universal Disk Format Filesystem");
  47MODULE_LICENSE("GPL");
  48
  49#define EXTENT_MERGE_SIZE 5
  50
  51static umode_t udf_convert_permissions(struct fileEntry *);
  52static int udf_update_inode(struct inode *, int);
  53static void udf_fill_inode(struct inode *, struct buffer_head *);
  54static int udf_sync_inode(struct inode *inode);
  55static int udf_alloc_i_data(struct inode *inode, size_t size);
  56static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
  57static int8_t udf_insert_aext(struct inode *, struct extent_position,
  58                              struct kernel_lb_addr, uint32_t);
  59static void udf_split_extents(struct inode *, int *, int, int,
  60                              struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  61static void udf_prealloc_extents(struct inode *, int, int,
  62                                 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  63static void udf_merge_extents(struct inode *,
  64                              struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  65static void udf_update_extents(struct inode *,
  66                               struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
  67                               struct extent_position *);
  68static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
  69
  70
  71void udf_evict_inode(struct inode *inode)
  72{
  73        struct udf_inode_info *iinfo = UDF_I(inode);
  74        int want_delete = 0;
  75
  76        if (!inode->i_nlink && !is_bad_inode(inode)) {
  77                want_delete = 1;
  78                udf_setsize(inode, 0);
  79                udf_update_inode(inode, IS_SYNC(inode));
  80        } else
  81                truncate_inode_pages(&inode->i_data, 0);
  82        invalidate_inode_buffers(inode);
  83        clear_inode(inode);
  84        if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
  85            inode->i_size != iinfo->i_lenExtents) {
  86                udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
  87                         inode->i_ino, inode->i_mode,
  88                         (unsigned long long)inode->i_size,
  89                         (unsigned long long)iinfo->i_lenExtents);
  90        }
  91        kfree(iinfo->i_ext.i_data);
  92        iinfo->i_ext.i_data = NULL;
  93        if (want_delete) {
  94                udf_free_inode(inode);
  95        }
  96}
  97
  98static void udf_write_failed(struct address_space *mapping, loff_t to)
  99{
 100        struct inode *inode = mapping->host;
 101        struct udf_inode_info *iinfo = UDF_I(inode);
 102        loff_t isize = inode->i_size;
 103
 104        if (to > isize) {
 105                truncate_pagecache(inode, to, isize);
 106                if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
 107                        down_write(&iinfo->i_data_sem);
 108                        udf_truncate_extents(inode);
 109                        up_write(&iinfo->i_data_sem);
 110                }
 111        }
 112}
 113
 114static int udf_writepage(struct page *page, struct writeback_control *wbc)
 115{
 116        return block_write_full_page(page, udf_get_block, wbc);
 117}
 118
 119static int udf_writepages(struct address_space *mapping,
 120                        struct writeback_control *wbc)
 121{
 122        return mpage_writepages(mapping, wbc, udf_get_block);
 123}
 124
 125static int udf_readpage(struct file *file, struct page *page)
 126{
 127        return mpage_readpage(page, udf_get_block);
 128}
 129
 130static int udf_readpages(struct file *file, struct address_space *mapping,
 131                        struct list_head *pages, unsigned nr_pages)
 132{
 133        return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
 134}
 135
 136static int udf_write_begin(struct file *file, struct address_space *mapping,
 137                        loff_t pos, unsigned len, unsigned flags,
 138                        struct page **pagep, void **fsdata)
 139{
 140        int ret;
 141
 142        ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
 143        if (unlikely(ret))
 144                udf_write_failed(mapping, pos + len);
 145        return ret;
 146}
 147
 148static ssize_t udf_direct_IO(int rw, struct kiocb *iocb,
 149                             const struct iovec *iov,
 150                             loff_t offset, unsigned long nr_segs)
 151{
 152        struct file *file = iocb->ki_filp;
 153        struct address_space *mapping = file->f_mapping;
 154        struct inode *inode = mapping->host;
 155        ssize_t ret;
 156
 157        ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
 158                                  udf_get_block);
 159        if (unlikely(ret < 0 && (rw & WRITE)))
 160                udf_write_failed(mapping, offset + iov_length(iov, nr_segs));
 161        return ret;
 162}
 163
 164static sector_t udf_bmap(struct address_space *mapping, sector_t block)
 165{
 166        return generic_block_bmap(mapping, block, udf_get_block);
 167}
 168
 169const struct address_space_operations udf_aops = {
 170        .readpage       = udf_readpage,
 171        .readpages      = udf_readpages,
 172        .writepage      = udf_writepage,
 173        .writepages     = udf_writepages,
 174        .write_begin    = udf_write_begin,
 175        .write_end      = generic_write_end,
 176        .direct_IO      = udf_direct_IO,
 177        .bmap           = udf_bmap,
 178};
 179
 180/*
 181 * Expand file stored in ICB to a normal one-block-file
 182 *
 183 * This function requires i_data_sem for writing and releases it.
 184 * This function requires i_mutex held
 185 */
 186int udf_expand_file_adinicb(struct inode *inode)
 187{
 188        struct page *page;
 189        char *kaddr;
 190        struct udf_inode_info *iinfo = UDF_I(inode);
 191        int err;
 192        struct writeback_control udf_wbc = {
 193                .sync_mode = WB_SYNC_NONE,
 194                .nr_to_write = 1,
 195        };
 196
 197        if (!iinfo->i_lenAlloc) {
 198                if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
 199                        iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
 200                else
 201                        iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
 202                /* from now on we have normal address_space methods */
 203                inode->i_data.a_ops = &udf_aops;
 204                up_write(&iinfo->i_data_sem);
 205                mark_inode_dirty(inode);
 206                return 0;
 207        }
 208        /*
 209         * Release i_data_sem so that we can lock a page - page lock ranks
 210         * above i_data_sem. i_mutex still protects us against file changes.
 211         */
 212        up_write(&iinfo->i_data_sem);
 213
 214        page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
 215        if (!page)
 216                return -ENOMEM;
 217
 218        if (!PageUptodate(page)) {
 219                kaddr = kmap(page);
 220                memset(kaddr + iinfo->i_lenAlloc, 0x00,
 221                       PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
 222                memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
 223                        iinfo->i_lenAlloc);
 224                flush_dcache_page(page);
 225                SetPageUptodate(page);
 226                kunmap(page);
 227        }
 228        down_write(&iinfo->i_data_sem);
 229        memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
 230               iinfo->i_lenAlloc);
 231        iinfo->i_lenAlloc = 0;
 232        if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
 233                iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
 234        else
 235                iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
 236        /* from now on we have normal address_space methods */
 237        inode->i_data.a_ops = &udf_aops;
 238        up_write(&iinfo->i_data_sem);
 239        err = inode->i_data.a_ops->writepage(page, &udf_wbc);
 240        if (err) {
 241                /* Restore everything back so that we don't lose data... */
 242                lock_page(page);
 243                kaddr = kmap(page);
 244                down_write(&iinfo->i_data_sem);
 245                memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
 246                       inode->i_size);
 247                kunmap(page);
 248                unlock_page(page);
 249                iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
 250                inode->i_data.a_ops = &udf_adinicb_aops;
 251                up_write(&iinfo->i_data_sem);
 252        }
 253        page_cache_release(page);
 254        mark_inode_dirty(inode);
 255
 256        return err;
 257}
 258
 259struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
 260                                           int *err)
 261{
 262        int newblock;
 263        struct buffer_head *dbh = NULL;
 264        struct kernel_lb_addr eloc;
 265        uint8_t alloctype;
 266        struct extent_position epos;
 267
 268        struct udf_fileident_bh sfibh, dfibh;
 269        loff_t f_pos = udf_ext0_offset(inode);
 270        int size = udf_ext0_offset(inode) + inode->i_size;
 271        struct fileIdentDesc cfi, *sfi, *dfi;
 272        struct udf_inode_info *iinfo = UDF_I(inode);
 273
 274        if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
 275                alloctype = ICBTAG_FLAG_AD_SHORT;
 276        else
 277                alloctype = ICBTAG_FLAG_AD_LONG;
 278
 279        if (!inode->i_size) {
 280                iinfo->i_alloc_type = alloctype;
 281                mark_inode_dirty(inode);
 282                return NULL;
 283        }
 284
 285        /* alloc block, and copy data to it */
 286        *block = udf_new_block(inode->i_sb, inode,
 287                               iinfo->i_location.partitionReferenceNum,
 288                               iinfo->i_location.logicalBlockNum, err);
 289        if (!(*block))
 290                return NULL;
 291        newblock = udf_get_pblock(inode->i_sb, *block,
 292                                  iinfo->i_location.partitionReferenceNum,
 293                                0);
 294        if (!newblock)
 295                return NULL;
 296        dbh = udf_tgetblk(inode->i_sb, newblock);
 297        if (!dbh)
 298                return NULL;
 299        lock_buffer(dbh);
 300        memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
 301        set_buffer_uptodate(dbh);
 302        unlock_buffer(dbh);
 303        mark_buffer_dirty_inode(dbh, inode);
 304
 305        sfibh.soffset = sfibh.eoffset =
 306                        f_pos & (inode->i_sb->s_blocksize - 1);
 307        sfibh.sbh = sfibh.ebh = NULL;
 308        dfibh.soffset = dfibh.eoffset = 0;
 309        dfibh.sbh = dfibh.ebh = dbh;
 310        while (f_pos < size) {
 311                iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
 312                sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
 313                                         NULL, NULL, NULL);
 314                if (!sfi) {
 315                        brelse(dbh);
 316                        return NULL;
 317                }
 318                iinfo->i_alloc_type = alloctype;
 319                sfi->descTag.tagLocation = cpu_to_le32(*block);
 320                dfibh.soffset = dfibh.eoffset;
 321                dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
 322                dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
 323                if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
 324                                 sfi->fileIdent +
 325                                        le16_to_cpu(sfi->lengthOfImpUse))) {
 326                        iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
 327                        brelse(dbh);
 328                        return NULL;
 329                }
 330        }
 331        mark_buffer_dirty_inode(dbh, inode);
 332
 333        memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
 334                iinfo->i_lenAlloc);
 335        iinfo->i_lenAlloc = 0;
 336        eloc.logicalBlockNum = *block;
 337        eloc.partitionReferenceNum =
 338                                iinfo->i_location.partitionReferenceNum;
 339        iinfo->i_lenExtents = inode->i_size;
 340        epos.bh = NULL;
 341        epos.block = iinfo->i_location;
 342        epos.offset = udf_file_entry_alloc_offset(inode);
 343        udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
 344        /* UniqueID stuff */
 345
 346        brelse(epos.bh);
 347        mark_inode_dirty(inode);
 348        return dbh;
 349}
 350
 351static int udf_get_block(struct inode *inode, sector_t block,
 352                         struct buffer_head *bh_result, int create)
 353{
 354        int err, new;
 355        sector_t phys = 0;
 356        struct udf_inode_info *iinfo;
 357
 358        if (!create) {
 359                phys = udf_block_map(inode, block);
 360                if (phys)
 361                        map_bh(bh_result, inode->i_sb, phys);
 362                return 0;
 363        }
 364
 365        err = -EIO;
 366        new = 0;
 367        iinfo = UDF_I(inode);
 368
 369        down_write(&iinfo->i_data_sem);
 370        if (block == iinfo->i_next_alloc_block + 1) {
 371                iinfo->i_next_alloc_block++;
 372                iinfo->i_next_alloc_goal++;
 373        }
 374
 375
 376        phys = inode_getblk(inode, block, &err, &new);
 377        if (!phys)
 378                goto abort;
 379
 380        if (new)
 381                set_buffer_new(bh_result);
 382        map_bh(bh_result, inode->i_sb, phys);
 383
 384abort:
 385        up_write(&iinfo->i_data_sem);
 386        return err;
 387}
 388
 389static struct buffer_head *udf_getblk(struct inode *inode, long block,
 390                                      int create, int *err)
 391{
 392        struct buffer_head *bh;
 393        struct buffer_head dummy;
 394
 395        dummy.b_state = 0;
 396        dummy.b_blocknr = -1000;
 397        *err = udf_get_block(inode, block, &dummy, create);
 398        if (!*err && buffer_mapped(&dummy)) {
 399                bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
 400                if (buffer_new(&dummy)) {
 401                        lock_buffer(bh);
 402                        memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
 403                        set_buffer_uptodate(bh);
 404                        unlock_buffer(bh);
 405                        mark_buffer_dirty_inode(bh, inode);
 406                }
 407                return bh;
 408        }
 409
 410        return NULL;
 411}
 412
 413/* Extend the file by 'blocks' blocks, return the number of extents added */
 414static int udf_do_extend_file(struct inode *inode,
 415                              struct extent_position *last_pos,
 416                              struct kernel_long_ad *last_ext,
 417                              sector_t blocks)
 418{
 419        sector_t add;
 420        int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
 421        struct super_block *sb = inode->i_sb;
 422        struct kernel_lb_addr prealloc_loc = {};
 423        int prealloc_len = 0;
 424        struct udf_inode_info *iinfo;
 425        int err;
 426
 427        /* The previous extent is fake and we should not extend by anything
 428         * - there's nothing to do... */
 429        if (!blocks && fake)
 430                return 0;
 431
 432        iinfo = UDF_I(inode);
 433        /* Round the last extent up to a multiple of block size */
 434        if (last_ext->extLength & (sb->s_blocksize - 1)) {
 435                last_ext->extLength =
 436                        (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
 437                        (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
 438                          sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
 439                iinfo->i_lenExtents =
 440                        (iinfo->i_lenExtents + sb->s_blocksize - 1) &
 441                        ~(sb->s_blocksize - 1);
 442        }
 443
 444        /* Last extent are just preallocated blocks? */
 445        if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
 446                                                EXT_NOT_RECORDED_ALLOCATED) {
 447                /* Save the extent so that we can reattach it to the end */
 448                prealloc_loc = last_ext->extLocation;
 449                prealloc_len = last_ext->extLength;
 450                /* Mark the extent as a hole */
 451                last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
 452                        (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
 453                last_ext->extLocation.logicalBlockNum = 0;
 454                last_ext->extLocation.partitionReferenceNum = 0;
 455        }
 456
 457        /* Can we merge with the previous extent? */
 458        if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
 459                                        EXT_NOT_RECORDED_NOT_ALLOCATED) {
 460                add = ((1 << 30) - sb->s_blocksize -
 461                        (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
 462                        sb->s_blocksize_bits;
 463                if (add > blocks)
 464                        add = blocks;
 465                blocks -= add;
 466                last_ext->extLength += add << sb->s_blocksize_bits;
 467        }
 468
 469        if (fake) {
 470                udf_add_aext(inode, last_pos, &last_ext->extLocation,
 471                             last_ext->extLength, 1);
 472                count++;
 473        } else
 474                udf_write_aext(inode, last_pos, &last_ext->extLocation,
 475                                last_ext->extLength, 1);
 476
 477        /* Managed to do everything necessary? */
 478        if (!blocks)
 479                goto out;
 480
 481        /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
 482        last_ext->extLocation.logicalBlockNum = 0;
 483        last_ext->extLocation.partitionReferenceNum = 0;
 484        add = (1 << (30-sb->s_blocksize_bits)) - 1;
 485        last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
 486                                (add << sb->s_blocksize_bits);
 487
 488        /* Create enough extents to cover the whole hole */
 489        while (blocks > add) {
 490                blocks -= add;
 491                err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
 492                                   last_ext->extLength, 1);
 493                if (err)
 494                        return err;
 495                count++;
 496        }
 497        if (blocks) {
 498                last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
 499                        (blocks << sb->s_blocksize_bits);
 500                err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
 501                                   last_ext->extLength, 1);
 502                if (err)
 503                        return err;
 504                count++;
 505        }
 506
 507out:
 508        /* Do we have some preallocated blocks saved? */
 509        if (prealloc_len) {
 510                err = udf_add_aext(inode, last_pos, &prealloc_loc,
 511                                   prealloc_len, 1);
 512                if (err)
 513                        return err;
 514                last_ext->extLocation = prealloc_loc;
 515                last_ext->extLength = prealloc_len;
 516                count++;
 517        }
 518
 519        /* last_pos should point to the last written extent... */
 520        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
 521                last_pos->offset -= sizeof(struct short_ad);
 522        else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
 523                last_pos->offset -= sizeof(struct long_ad);
 524        else
 525                return -EIO;
 526
 527        return count;
 528}
 529
 530static int udf_extend_file(struct inode *inode, loff_t newsize)
 531{
 532
 533        struct extent_position epos;
 534        struct kernel_lb_addr eloc;
 535        uint32_t elen;
 536        int8_t etype;
 537        struct super_block *sb = inode->i_sb;
 538        sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
 539        int adsize;
 540        struct udf_inode_info *iinfo = UDF_I(inode);
 541        struct kernel_long_ad extent;
 542        int err;
 543
 544        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
 545                adsize = sizeof(struct short_ad);
 546        else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
 547                adsize = sizeof(struct long_ad);
 548        else
 549                BUG();
 550
 551        etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
 552
 553        /* File has extent covering the new size (could happen when extending
 554         * inside a block)? */
 555        if (etype != -1)
 556                return 0;
 557        if (newsize & (sb->s_blocksize - 1))
 558                offset++;
 559        /* Extended file just to the boundary of the last file block? */
 560        if (offset == 0)
 561                return 0;
 562
 563        /* Truncate is extending the file by 'offset' blocks */
 564        if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
 565            (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
 566                /* File has no extents at all or has empty last
 567                 * indirect extent! Create a fake extent... */
 568                extent.extLocation.logicalBlockNum = 0;
 569                extent.extLocation.partitionReferenceNum = 0;
 570                extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
 571        } else {
 572                epos.offset -= adsize;
 573                etype = udf_next_aext(inode, &epos, &extent.extLocation,
 574                                      &extent.extLength, 0);
 575                extent.extLength |= etype << 30;
 576        }
 577        err = udf_do_extend_file(inode, &epos, &extent, offset);
 578        if (err < 0)
 579                goto out;
 580        err = 0;
 581        iinfo->i_lenExtents = newsize;
 582out:
 583        brelse(epos.bh);
 584        return err;
 585}
 586
 587static sector_t inode_getblk(struct inode *inode, sector_t block,
 588                             int *err, int *new)
 589{
 590        static sector_t last_block;
 591        struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
 592        struct extent_position prev_epos, cur_epos, next_epos;
 593        int count = 0, startnum = 0, endnum = 0;
 594        uint32_t elen = 0, tmpelen;
 595        struct kernel_lb_addr eloc, tmpeloc;
 596        int c = 1;
 597        loff_t lbcount = 0, b_off = 0;
 598        uint32_t newblocknum, newblock;
 599        sector_t offset = 0;
 600        int8_t etype;
 601        struct udf_inode_info *iinfo = UDF_I(inode);
 602        int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
 603        int lastblock = 0;
 604        bool isBeyondEOF;
 605
 606        *err = 0;
 607        *new = 0;
 608        prev_epos.offset = udf_file_entry_alloc_offset(inode);
 609        prev_epos.block = iinfo->i_location;
 610        prev_epos.bh = NULL;
 611        cur_epos = next_epos = prev_epos;
 612        b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
 613
 614        /* find the extent which contains the block we are looking for.
 615           alternate between laarr[0] and laarr[1] for locations of the
 616           current extent, and the previous extent */
 617        do {
 618                if (prev_epos.bh != cur_epos.bh) {
 619                        brelse(prev_epos.bh);
 620                        get_bh(cur_epos.bh);
 621                        prev_epos.bh = cur_epos.bh;
 622                }
 623                if (cur_epos.bh != next_epos.bh) {
 624                        brelse(cur_epos.bh);
 625                        get_bh(next_epos.bh);
 626                        cur_epos.bh = next_epos.bh;
 627                }
 628
 629                lbcount += elen;
 630
 631                prev_epos.block = cur_epos.block;
 632                cur_epos.block = next_epos.block;
 633
 634                prev_epos.offset = cur_epos.offset;
 635                cur_epos.offset = next_epos.offset;
 636
 637                etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
 638                if (etype == -1)
 639                        break;
 640
 641                c = !c;
 642
 643                laarr[c].extLength = (etype << 30) | elen;
 644                laarr[c].extLocation = eloc;
 645
 646                if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
 647                        pgoal = eloc.logicalBlockNum +
 648                                ((elen + inode->i_sb->s_blocksize - 1) >>
 649                                 inode->i_sb->s_blocksize_bits);
 650
 651                count++;
 652        } while (lbcount + elen <= b_off);
 653
 654        b_off -= lbcount;
 655        offset = b_off >> inode->i_sb->s_blocksize_bits;
 656        /*
 657         * Move prev_epos and cur_epos into indirect extent if we are at
 658         * the pointer to it
 659         */
 660        udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
 661        udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
 662
 663        /* if the extent is allocated and recorded, return the block
 664           if the extent is not a multiple of the blocksize, round up */
 665
 666        if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
 667                if (elen & (inode->i_sb->s_blocksize - 1)) {
 668                        elen = EXT_RECORDED_ALLOCATED |
 669                                ((elen + inode->i_sb->s_blocksize - 1) &
 670                                 ~(inode->i_sb->s_blocksize - 1));
 671                        udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
 672                }
 673                brelse(prev_epos.bh);
 674                brelse(cur_epos.bh);
 675                brelse(next_epos.bh);
 676                newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
 677                return newblock;
 678        }
 679
 680        last_block = block;
 681        /* Are we beyond EOF? */
 682        if (etype == -1) {
 683                int ret;
 684                isBeyondEOF = 1;
 685                if (count) {
 686                        if (c)
 687                                laarr[0] = laarr[1];
 688                        startnum = 1;
 689                } else {
 690                        /* Create a fake extent when there's not one */
 691                        memset(&laarr[0].extLocation, 0x00,
 692                                sizeof(struct kernel_lb_addr));
 693                        laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
 694                        /* Will udf_do_extend_file() create real extent from
 695                           a fake one? */
 696                        startnum = (offset > 0);
 697                }
 698                /* Create extents for the hole between EOF and offset */
 699                ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
 700                if (ret < 0) {
 701                        brelse(prev_epos.bh);
 702                        brelse(cur_epos.bh);
 703                        brelse(next_epos.bh);
 704                        *err = ret;
 705                        return 0;
 706                }
 707                c = 0;
 708                offset = 0;
 709                count += ret;
 710                /* We are not covered by a preallocated extent? */
 711                if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
 712                                                EXT_NOT_RECORDED_ALLOCATED) {
 713                        /* Is there any real extent? - otherwise we overwrite
 714                         * the fake one... */
 715                        if (count)
 716                                c = !c;
 717                        laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
 718                                inode->i_sb->s_blocksize;
 719                        memset(&laarr[c].extLocation, 0x00,
 720                                sizeof(struct kernel_lb_addr));
 721                        count++;
 722                        endnum++;
 723                }
 724                endnum = c + 1;
 725                lastblock = 1;
 726        } else {
 727                isBeyondEOF = 0;
 728                endnum = startnum = ((count > 2) ? 2 : count);
 729
 730                /* if the current extent is in position 0,
 731                   swap it with the previous */
 732                if (!c && count != 1) {
 733                        laarr[2] = laarr[0];
 734                        laarr[0] = laarr[1];
 735                        laarr[1] = laarr[2];
 736                        c = 1;
 737                }
 738
 739                /* if the current block is located in an extent,
 740                   read the next extent */
 741                etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
 742                if (etype != -1) {
 743                        laarr[c + 1].extLength = (etype << 30) | elen;
 744                        laarr[c + 1].extLocation = eloc;
 745                        count++;
 746                        startnum++;
 747                        endnum++;
 748                } else
 749                        lastblock = 1;
 750        }
 751
 752        /* if the current extent is not recorded but allocated, get the
 753         * block in the extent corresponding to the requested block */
 754        if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
 755                newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
 756        else { /* otherwise, allocate a new block */
 757                if (iinfo->i_next_alloc_block == block)
 758                        goal = iinfo->i_next_alloc_goal;
 759
 760                if (!goal) {
 761                        if (!(goal = pgoal)) /* XXX: what was intended here? */
 762                                goal = iinfo->i_location.logicalBlockNum + 1;
 763                }
 764
 765                newblocknum = udf_new_block(inode->i_sb, inode,
 766                                iinfo->i_location.partitionReferenceNum,
 767                                goal, err);
 768                if (!newblocknum) {
 769                        brelse(prev_epos.bh);
 770                        brelse(cur_epos.bh);
 771                        brelse(next_epos.bh);
 772                        *err = -ENOSPC;
 773                        return 0;
 774                }
 775                if (isBeyondEOF)
 776                        iinfo->i_lenExtents += inode->i_sb->s_blocksize;
 777        }
 778
 779        /* if the extent the requsted block is located in contains multiple
 780         * blocks, split the extent into at most three extents. blocks prior
 781         * to requested block, requested block, and blocks after requested
 782         * block */
 783        udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
 784
 785#ifdef UDF_PREALLOCATE
 786        /* We preallocate blocks only for regular files. It also makes sense
 787         * for directories but there's a problem when to drop the
 788         * preallocation. We might use some delayed work for that but I feel
 789         * it's overengineering for a filesystem like UDF. */
 790        if (S_ISREG(inode->i_mode))
 791                udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
 792#endif
 793
 794        /* merge any continuous blocks in laarr */
 795        udf_merge_extents(inode, laarr, &endnum);
 796
 797        /* write back the new extents, inserting new extents if the new number
 798         * of extents is greater than the old number, and deleting extents if
 799         * the new number of extents is less than the old number */
 800        udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
 801
 802        brelse(prev_epos.bh);
 803        brelse(cur_epos.bh);
 804        brelse(next_epos.bh);
 805
 806        newblock = udf_get_pblock(inode->i_sb, newblocknum,
 807                                iinfo->i_location.partitionReferenceNum, 0);
 808        if (!newblock) {
 809                *err = -EIO;
 810                return 0;
 811        }
 812        *new = 1;
 813        iinfo->i_next_alloc_block = block;
 814        iinfo->i_next_alloc_goal = newblocknum;
 815        inode->i_ctime = current_fs_time(inode->i_sb);
 816
 817        if (IS_SYNC(inode))
 818                udf_sync_inode(inode);
 819        else
 820                mark_inode_dirty(inode);
 821
 822        return newblock;
 823}
 824
 825static void udf_split_extents(struct inode *inode, int *c, int offset,
 826                              int newblocknum,
 827                              struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
 828                              int *endnum)
 829{
 830        unsigned long blocksize = inode->i_sb->s_blocksize;
 831        unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
 832
 833        if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
 834            (laarr[*c].extLength >> 30) ==
 835                                (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
 836                int curr = *c;
 837                int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
 838                            blocksize - 1) >> blocksize_bits;
 839                int8_t etype = (laarr[curr].extLength >> 30);
 840
 841                if (blen == 1)
 842                        ;
 843                else if (!offset || blen == offset + 1) {
 844                        laarr[curr + 2] = laarr[curr + 1];
 845                        laarr[curr + 1] = laarr[curr];
 846                } else {
 847                        laarr[curr + 3] = laarr[curr + 1];
 848                        laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
 849                }
 850
 851                if (offset) {
 852                        if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
 853                                udf_free_blocks(inode->i_sb, inode,
 854                                                &laarr[curr].extLocation,
 855                                                0, offset);
 856                                laarr[curr].extLength =
 857                                        EXT_NOT_RECORDED_NOT_ALLOCATED |
 858                                        (offset << blocksize_bits);
 859                                laarr[curr].extLocation.logicalBlockNum = 0;
 860                                laarr[curr].extLocation.
 861                                                partitionReferenceNum = 0;
 862                        } else
 863                                laarr[curr].extLength = (etype << 30) |
 864                                        (offset << blocksize_bits);
 865                        curr++;
 866                        (*c)++;
 867                        (*endnum)++;
 868                }
 869
 870                laarr[curr].extLocation.logicalBlockNum = newblocknum;
 871                if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
 872                        laarr[curr].extLocation.partitionReferenceNum =
 873                                UDF_I(inode)->i_location.partitionReferenceNum;
 874                laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
 875                        blocksize;
 876                curr++;
 877
 878                if (blen != offset + 1) {
 879                        if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
 880                                laarr[curr].extLocation.logicalBlockNum +=
 881                                                                offset + 1;
 882                        laarr[curr].extLength = (etype << 30) |
 883                                ((blen - (offset + 1)) << blocksize_bits);
 884                        curr++;
 885                        (*endnum)++;
 886                }
 887        }
 888}
 889
 890static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
 891                                 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
 892                                 int *endnum)
 893{
 894        int start, length = 0, currlength = 0, i;
 895
 896        if (*endnum >= (c + 1)) {
 897                if (!lastblock)
 898                        return;
 899                else
 900                        start = c;
 901        } else {
 902                if ((laarr[c + 1].extLength >> 30) ==
 903                                        (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
 904                        start = c + 1;
 905                        length = currlength =
 906                                (((laarr[c + 1].extLength &
 907                                        UDF_EXTENT_LENGTH_MASK) +
 908                                inode->i_sb->s_blocksize - 1) >>
 909                                inode->i_sb->s_blocksize_bits);
 910                } else
 911                        start = c;
 912        }
 913
 914        for (i = start + 1; i <= *endnum; i++) {
 915                if (i == *endnum) {
 916                        if (lastblock)
 917                                length += UDF_DEFAULT_PREALLOC_BLOCKS;
 918                } else if ((laarr[i].extLength >> 30) ==
 919                                (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
 920                        length += (((laarr[i].extLength &
 921                                                UDF_EXTENT_LENGTH_MASK) +
 922                                    inode->i_sb->s_blocksize - 1) >>
 923                                    inode->i_sb->s_blocksize_bits);
 924                } else
 925                        break;
 926        }
 927
 928        if (length) {
 929                int next = laarr[start].extLocation.logicalBlockNum +
 930                        (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
 931                          inode->i_sb->s_blocksize - 1) >>
 932                          inode->i_sb->s_blocksize_bits);
 933                int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
 934                                laarr[start].extLocation.partitionReferenceNum,
 935                                next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
 936                                length : UDF_DEFAULT_PREALLOC_BLOCKS) -
 937                                currlength);
 938                if (numalloc)   {
 939                        if (start == (c + 1))
 940                                laarr[start].extLength +=
 941                                        (numalloc <<
 942                                         inode->i_sb->s_blocksize_bits);
 943                        else {
 944                                memmove(&laarr[c + 2], &laarr[c + 1],
 945                                        sizeof(struct long_ad) * (*endnum - (c + 1)));
 946                                (*endnum)++;
 947                                laarr[c + 1].extLocation.logicalBlockNum = next;
 948                                laarr[c + 1].extLocation.partitionReferenceNum =
 949                                        laarr[c].extLocation.
 950                                                        partitionReferenceNum;
 951                                laarr[c + 1].extLength =
 952                                        EXT_NOT_RECORDED_ALLOCATED |
 953                                        (numalloc <<
 954                                         inode->i_sb->s_blocksize_bits);
 955                                start = c + 1;
 956                        }
 957
 958                        for (i = start + 1; numalloc && i < *endnum; i++) {
 959                                int elen = ((laarr[i].extLength &
 960                                                UDF_EXTENT_LENGTH_MASK) +
 961                                            inode->i_sb->s_blocksize - 1) >>
 962                                            inode->i_sb->s_blocksize_bits;
 963
 964                                if (elen > numalloc) {
 965                                        laarr[i].extLength -=
 966                                                (numalloc <<
 967                                                 inode->i_sb->s_blocksize_bits);
 968                                        numalloc = 0;
 969                                } else {
 970                                        numalloc -= elen;
 971                                        if (*endnum > (i + 1))
 972                                                memmove(&laarr[i],
 973                                                        &laarr[i + 1],
 974                                                        sizeof(struct long_ad) *
 975                                                        (*endnum - (i + 1)));
 976                                        i--;
 977                                        (*endnum)--;
 978                                }
 979                        }
 980                        UDF_I(inode)->i_lenExtents +=
 981                                numalloc << inode->i_sb->s_blocksize_bits;
 982                }
 983        }
 984}
 985
 986static void udf_merge_extents(struct inode *inode,
 987                              struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
 988                              int *endnum)
 989{
 990        int i;
 991        unsigned long blocksize = inode->i_sb->s_blocksize;
 992        unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
 993
 994        for (i = 0; i < (*endnum - 1); i++) {
 995                struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
 996                struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
 997
 998                if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
 999                        (((li->extLength >> 30) ==
1000                                (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1001                        ((lip1->extLocation.logicalBlockNum -
1002                          li->extLocation.logicalBlockNum) ==
1003                        (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1004                        blocksize - 1) >> blocksize_bits)))) {
1005
1006                        if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1007                                (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1008                                blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1009                                lip1->extLength = (lip1->extLength -
1010                                                  (li->extLength &
1011                                                   UDF_EXTENT_LENGTH_MASK) +
1012                                                   UDF_EXTENT_LENGTH_MASK) &
1013                                                        ~(blocksize - 1);
1014                                li->extLength = (li->extLength &
1015                                                 UDF_EXTENT_FLAG_MASK) +
1016                                                (UDF_EXTENT_LENGTH_MASK + 1) -
1017                                                blocksize;
1018                                lip1->extLocation.logicalBlockNum =
1019                                        li->extLocation.logicalBlockNum +
1020                                        ((li->extLength &
1021                                                UDF_EXTENT_LENGTH_MASK) >>
1022                                                blocksize_bits);
1023                        } else {
1024                                li->extLength = lip1->extLength +
1025                                        (((li->extLength &
1026                                                UDF_EXTENT_LENGTH_MASK) +
1027                                         blocksize - 1) & ~(blocksize - 1));
1028                                if (*endnum > (i + 2))
1029                                        memmove(&laarr[i + 1], &laarr[i + 2],
1030                                                sizeof(struct long_ad) *
1031                                                (*endnum - (i + 2)));
1032                                i--;
1033                                (*endnum)--;
1034                        }
1035                } else if (((li->extLength >> 30) ==
1036                                (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1037                           ((lip1->extLength >> 30) ==
1038                                (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1039                        udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1040                                        ((li->extLength &
1041                                          UDF_EXTENT_LENGTH_MASK) +
1042                                         blocksize - 1) >> blocksize_bits);
1043                        li->extLocation.logicalBlockNum = 0;
1044                        li->extLocation.partitionReferenceNum = 0;
1045
1046                        if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1047                             (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1048                             blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1049                                lip1->extLength = (lip1->extLength -
1050                                                   (li->extLength &
1051                                                   UDF_EXTENT_LENGTH_MASK) +
1052                                                   UDF_EXTENT_LENGTH_MASK) &
1053                                                   ~(blocksize - 1);
1054                                li->extLength = (li->extLength &
1055                                                 UDF_EXTENT_FLAG_MASK) +
1056                                                (UDF_EXTENT_LENGTH_MASK + 1) -
1057                                                blocksize;
1058                        } else {
1059                                li->extLength = lip1->extLength +
1060                                        (((li->extLength &
1061                                                UDF_EXTENT_LENGTH_MASK) +
1062                                          blocksize - 1) & ~(blocksize - 1));
1063                                if (*endnum > (i + 2))
1064                                        memmove(&laarr[i + 1], &laarr[i + 2],
1065                                                sizeof(struct long_ad) *
1066                                                (*endnum - (i + 2)));
1067                                i--;
1068                                (*endnum)--;
1069                        }
1070                } else if ((li->extLength >> 30) ==
1071                                        (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1072                        udf_free_blocks(inode->i_sb, inode,
1073                                        &li->extLocation, 0,
1074                                        ((li->extLength &
1075                                                UDF_EXTENT_LENGTH_MASK) +
1076                                         blocksize - 1) >> blocksize_bits);
1077                        li->extLocation.logicalBlockNum = 0;
1078                        li->extLocation.partitionReferenceNum = 0;
1079                        li->extLength = (li->extLength &
1080                                                UDF_EXTENT_LENGTH_MASK) |
1081                                                EXT_NOT_RECORDED_NOT_ALLOCATED;
1082                }
1083        }
1084}
1085
1086static void udf_update_extents(struct inode *inode,
1087                               struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1088                               int startnum, int endnum,
1089                               struct extent_position *epos)
1090{
1091        int start = 0, i;
1092        struct kernel_lb_addr tmploc;
1093        uint32_t tmplen;
1094
1095        if (startnum > endnum) {
1096                for (i = 0; i < (startnum - endnum); i++)
1097                        udf_delete_aext(inode, *epos, laarr[i].extLocation,
1098                                        laarr[i].extLength);
1099        } else if (startnum < endnum) {
1100                for (i = 0; i < (endnum - startnum); i++) {
1101                        udf_insert_aext(inode, *epos, laarr[i].extLocation,
1102                                        laarr[i].extLength);
1103                        udf_next_aext(inode, epos, &laarr[i].extLocation,
1104                                      &laarr[i].extLength, 1);
1105                        start++;
1106                }
1107        }
1108
1109        for (i = start; i < endnum; i++) {
1110                udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1111                udf_write_aext(inode, epos, &laarr[i].extLocation,
1112                               laarr[i].extLength, 1);
1113        }
1114}
1115
1116struct buffer_head *udf_bread(struct inode *inode, int block,
1117                              int create, int *err)
1118{
1119        struct buffer_head *bh = NULL;
1120
1121        bh = udf_getblk(inode, block, create, err);
1122        if (!bh)
1123                return NULL;
1124
1125        if (buffer_uptodate(bh))
1126                return bh;
1127
1128        ll_rw_block(READ, 1, &bh);
1129
1130        wait_on_buffer(bh);
1131        if (buffer_uptodate(bh))
1132                return bh;
1133
1134        brelse(bh);
1135        *err = -EIO;
1136        return NULL;
1137}
1138
1139int udf_setsize(struct inode *inode, loff_t newsize)
1140{
1141        int err;
1142        struct udf_inode_info *iinfo;
1143        int bsize = 1 << inode->i_blkbits;
1144
1145        if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1146              S_ISLNK(inode->i_mode)))
1147                return -EINVAL;
1148        if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1149                return -EPERM;
1150
1151        iinfo = UDF_I(inode);
1152        if (newsize > inode->i_size) {
1153                down_write(&iinfo->i_data_sem);
1154                if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1155                        if (bsize <
1156                            (udf_file_entry_alloc_offset(inode) + newsize)) {
1157                                err = udf_expand_file_adinicb(inode);
1158                                if (err)
1159                                        return err;
1160                                down_write(&iinfo->i_data_sem);
1161                        } else {
1162                                iinfo->i_lenAlloc = newsize;
1163                                goto set_size;
1164                        }
1165                }
1166                err = udf_extend_file(inode, newsize);
1167                if (err) {
1168                        up_write(&iinfo->i_data_sem);
1169                        return err;
1170                }
1171set_size:
1172                truncate_setsize(inode, newsize);
1173                up_write(&iinfo->i_data_sem);
1174        } else {
1175                if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1176                        down_write(&iinfo->i_data_sem);
1177                        memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1178                               0x00, bsize - newsize -
1179                               udf_file_entry_alloc_offset(inode));
1180                        iinfo->i_lenAlloc = newsize;
1181                        truncate_setsize(inode, newsize);
1182                        up_write(&iinfo->i_data_sem);
1183                        goto update_time;
1184                }
1185                err = block_truncate_page(inode->i_mapping, newsize,
1186                                          udf_get_block);
1187                if (err)
1188                        return err;
1189                down_write(&iinfo->i_data_sem);
1190                truncate_setsize(inode, newsize);
1191                udf_truncate_extents(inode);
1192                up_write(&iinfo->i_data_sem);
1193        }
1194update_time:
1195        inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1196        if (IS_SYNC(inode))
1197                udf_sync_inode(inode);
1198        else
1199                mark_inode_dirty(inode);
1200        return 0;
1201}
1202
1203static void __udf_read_inode(struct inode *inode)
1204{
1205        struct buffer_head *bh = NULL;
1206        struct fileEntry *fe;
1207        uint16_t ident;
1208        struct udf_inode_info *iinfo = UDF_I(inode);
1209
1210        /*
1211         * Set defaults, but the inode is still incomplete!
1212         * Note: get_new_inode() sets the following on a new inode:
1213         *      i_sb = sb
1214         *      i_no = ino
1215         *      i_flags = sb->s_flags
1216         *      i_state = 0
1217         * clean_inode(): zero fills and sets
1218         *      i_count = 1
1219         *      i_nlink = 1
1220         *      i_op = NULL;
1221         */
1222        bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
1223        if (!bh) {
1224                udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1225                make_bad_inode(inode);
1226                return;
1227        }
1228
1229        if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1230            ident != TAG_IDENT_USE) {
1231                udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1232                        inode->i_ino, ident);
1233                brelse(bh);
1234                make_bad_inode(inode);
1235                return;
1236        }
1237
1238        fe = (struct fileEntry *)bh->b_data;
1239
1240        if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1241                struct buffer_head *ibh;
1242
1243                ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
1244                                        &ident);
1245                if (ident == TAG_IDENT_IE && ibh) {
1246                        struct buffer_head *nbh = NULL;
1247                        struct kernel_lb_addr loc;
1248                        struct indirectEntry *ie;
1249
1250                        ie = (struct indirectEntry *)ibh->b_data;
1251                        loc = lelb_to_cpu(ie->indirectICB.extLocation);
1252
1253                        if (ie->indirectICB.extLength &&
1254                                (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
1255                                                        &ident))) {
1256                                if (ident == TAG_IDENT_FE ||
1257                                        ident == TAG_IDENT_EFE) {
1258                                        memcpy(&iinfo->i_location,
1259                                                &loc,
1260                                                sizeof(struct kernel_lb_addr));
1261                                        brelse(bh);
1262                                        brelse(ibh);
1263                                        brelse(nbh);
1264                                        __udf_read_inode(inode);
1265                                        return;
1266                                }
1267                                brelse(nbh);
1268                        }
1269                }
1270                brelse(ibh);
1271        } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1272                udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1273                        le16_to_cpu(fe->icbTag.strategyType));
1274                brelse(bh);
1275                make_bad_inode(inode);
1276                return;
1277        }
1278        udf_fill_inode(inode, bh);
1279
1280        brelse(bh);
1281}
1282
1283static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1284{
1285        struct fileEntry *fe;
1286        struct extendedFileEntry *efe;
1287        struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1288        struct udf_inode_info *iinfo = UDF_I(inode);
1289        unsigned int link_count;
1290
1291        fe = (struct fileEntry *)bh->b_data;
1292        efe = (struct extendedFileEntry *)bh->b_data;
1293
1294        if (fe->icbTag.strategyType == cpu_to_le16(4))
1295                iinfo->i_strat4096 = 0;
1296        else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1297                iinfo->i_strat4096 = 1;
1298
1299        iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1300                                                        ICBTAG_FLAG_AD_MASK;
1301        iinfo->i_unique = 0;
1302        iinfo->i_lenEAttr = 0;
1303        iinfo->i_lenExtents = 0;
1304        iinfo->i_lenAlloc = 0;
1305        iinfo->i_next_alloc_block = 0;
1306        iinfo->i_next_alloc_goal = 0;
1307        if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1308                iinfo->i_efe = 1;
1309                iinfo->i_use = 0;
1310                if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1311                                        sizeof(struct extendedFileEntry))) {
1312                        make_bad_inode(inode);
1313                        return;
1314                }
1315                memcpy(iinfo->i_ext.i_data,
1316                       bh->b_data + sizeof(struct extendedFileEntry),
1317                       inode->i_sb->s_blocksize -
1318                                        sizeof(struct extendedFileEntry));
1319        } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1320                iinfo->i_efe = 0;
1321                iinfo->i_use = 0;
1322                if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1323                                                sizeof(struct fileEntry))) {
1324                        make_bad_inode(inode);
1325                        return;
1326                }
1327                memcpy(iinfo->i_ext.i_data,
1328                       bh->b_data + sizeof(struct fileEntry),
1329                       inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1330        } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1331                iinfo->i_efe = 0;
1332                iinfo->i_use = 1;
1333                iinfo->i_lenAlloc = le32_to_cpu(
1334                                ((struct unallocSpaceEntry *)bh->b_data)->
1335                                 lengthAllocDescs);
1336                if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1337                                        sizeof(struct unallocSpaceEntry))) {
1338                        make_bad_inode(inode);
1339                        return;
1340                }
1341                memcpy(iinfo->i_ext.i_data,
1342                       bh->b_data + sizeof(struct unallocSpaceEntry),
1343                       inode->i_sb->s_blocksize -
1344                                        sizeof(struct unallocSpaceEntry));
1345                return;
1346        }
1347
1348        read_lock(&sbi->s_cred_lock);
1349        i_uid_write(inode, le32_to_cpu(fe->uid));
1350        if (!uid_valid(inode->i_uid) ||
1351            UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1352            UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1353                inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1354
1355        i_gid_write(inode, le32_to_cpu(fe->gid));
1356        if (!gid_valid(inode->i_gid) ||
1357            UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1358            UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1359                inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1360
1361        if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1362                        sbi->s_fmode != UDF_INVALID_MODE)
1363                inode->i_mode = sbi->s_fmode;
1364        else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1365                        sbi->s_dmode != UDF_INVALID_MODE)
1366                inode->i_mode = sbi->s_dmode;
1367        else
1368                inode->i_mode = udf_convert_permissions(fe);
1369        inode->i_mode &= ~sbi->s_umask;
1370        read_unlock(&sbi->s_cred_lock);
1371
1372        link_count = le16_to_cpu(fe->fileLinkCount);
1373        if (!link_count)
1374                link_count = 1;
1375        set_nlink(inode, link_count);
1376
1377        inode->i_size = le64_to_cpu(fe->informationLength);
1378        iinfo->i_lenExtents = inode->i_size;
1379
1380        if (iinfo->i_efe == 0) {
1381                inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1382                        (inode->i_sb->s_blocksize_bits - 9);
1383
1384                if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1385                        inode->i_atime = sbi->s_record_time;
1386
1387                if (!udf_disk_stamp_to_time(&inode->i_mtime,
1388                                            fe->modificationTime))
1389                        inode->i_mtime = sbi->s_record_time;
1390
1391                if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1392                        inode->i_ctime = sbi->s_record_time;
1393
1394                iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1395                iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1396                iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1397                iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1398        } else {
1399                inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1400                    (inode->i_sb->s_blocksize_bits - 9);
1401
1402                if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1403                        inode->i_atime = sbi->s_record_time;
1404
1405                if (!udf_disk_stamp_to_time(&inode->i_mtime,
1406                                            efe->modificationTime))
1407                        inode->i_mtime = sbi->s_record_time;
1408
1409                if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1410                        iinfo->i_crtime = sbi->s_record_time;
1411
1412                if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1413                        inode->i_ctime = sbi->s_record_time;
1414
1415                iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1416                iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1417                iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1418                iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1419        }
1420
1421        switch (fe->icbTag.fileType) {
1422        case ICBTAG_FILE_TYPE_DIRECTORY:
1423                inode->i_op = &udf_dir_inode_operations;
1424                inode->i_fop = &udf_dir_operations;
1425                inode->i_mode |= S_IFDIR;
1426                inc_nlink(inode);
1427                break;
1428        case ICBTAG_FILE_TYPE_REALTIME:
1429        case ICBTAG_FILE_TYPE_REGULAR:
1430        case ICBTAG_FILE_TYPE_UNDEF:
1431        case ICBTAG_FILE_TYPE_VAT20:
1432                if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1433                        inode->i_data.a_ops = &udf_adinicb_aops;
1434                else
1435                        inode->i_data.a_ops = &udf_aops;
1436                inode->i_op = &udf_file_inode_operations;
1437                inode->i_fop = &udf_file_operations;
1438                inode->i_mode |= S_IFREG;
1439                break;
1440        case ICBTAG_FILE_TYPE_BLOCK:
1441                inode->i_mode |= S_IFBLK;
1442                break;
1443        case ICBTAG_FILE_TYPE_CHAR:
1444                inode->i_mode |= S_IFCHR;
1445                break;
1446        case ICBTAG_FILE_TYPE_FIFO:
1447                init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1448                break;
1449        case ICBTAG_FILE_TYPE_SOCKET:
1450                init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1451                break;
1452        case ICBTAG_FILE_TYPE_SYMLINK:
1453                inode->i_data.a_ops = &udf_symlink_aops;
1454                inode->i_op = &udf_symlink_inode_operations;
1455                inode->i_mode = S_IFLNK | S_IRWXUGO;
1456                break;
1457        case ICBTAG_FILE_TYPE_MAIN:
1458                udf_debug("METADATA FILE-----\n");
1459                break;
1460        case ICBTAG_FILE_TYPE_MIRROR:
1461                udf_debug("METADATA MIRROR FILE-----\n");
1462                break;
1463        case ICBTAG_FILE_TYPE_BITMAP:
1464                udf_debug("METADATA BITMAP FILE-----\n");
1465                break;
1466        default:
1467                udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1468                        inode->i_ino, fe->icbTag.fileType);
1469                make_bad_inode(inode);
1470                return;
1471        }
1472        if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1473                struct deviceSpec *dsea =
1474                        (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1475                if (dsea) {
1476                        init_special_inode(inode, inode->i_mode,
1477                                MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1478                                      le32_to_cpu(dsea->minorDeviceIdent)));
1479                        /* Developer ID ??? */
1480                } else
1481                        make_bad_inode(inode);
1482        }
1483}
1484
1485static int udf_alloc_i_data(struct inode *inode, size_t size)
1486{
1487        struct udf_inode_info *iinfo = UDF_I(inode);
1488        iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1489
1490        if (!iinfo->i_ext.i_data) {
1491                udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1492                        inode->i_ino);
1493                return -ENOMEM;
1494        }
1495
1496        return 0;
1497}
1498
1499static umode_t udf_convert_permissions(struct fileEntry *fe)
1500{
1501        umode_t mode;
1502        uint32_t permissions;
1503        uint32_t flags;
1504
1505        permissions = le32_to_cpu(fe->permissions);
1506        flags = le16_to_cpu(fe->icbTag.flags);
1507
1508        mode =  ((permissions) & S_IRWXO) |
1509                ((permissions >> 2) & S_IRWXG) |
1510                ((permissions >> 4) & S_IRWXU) |
1511                ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1512                ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1513                ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1514
1515        return mode;
1516}
1517
1518int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1519{
1520        return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1521}
1522
1523static int udf_sync_inode(struct inode *inode)
1524{
1525        return udf_update_inode(inode, 1);
1526}
1527
1528static int udf_update_inode(struct inode *inode, int do_sync)
1529{
1530        struct buffer_head *bh = NULL;
1531        struct fileEntry *fe;
1532        struct extendedFileEntry *efe;
1533        uint64_t lb_recorded;
1534        uint32_t udfperms;
1535        uint16_t icbflags;
1536        uint16_t crclen;
1537        int err = 0;
1538        struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1539        unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1540        struct udf_inode_info *iinfo = UDF_I(inode);
1541
1542        bh = udf_tgetblk(inode->i_sb,
1543                        udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1544        if (!bh) {
1545                udf_debug("getblk failure\n");
1546                return -ENOMEM;
1547        }
1548
1549        lock_buffer(bh);
1550        memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1551        fe = (struct fileEntry *)bh->b_data;
1552        efe = (struct extendedFileEntry *)bh->b_data;
1553
1554        if (iinfo->i_use) {
1555                struct unallocSpaceEntry *use =
1556                        (struct unallocSpaceEntry *)bh->b_data;
1557
1558                use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1559                memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1560                       iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1561                                        sizeof(struct unallocSpaceEntry));
1562                use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1563                use->descTag.tagLocation =
1564                                cpu_to_le32(iinfo->i_location.logicalBlockNum);
1565                crclen = sizeof(struct unallocSpaceEntry) +
1566                                iinfo->i_lenAlloc - sizeof(struct tag);
1567                use->descTag.descCRCLength = cpu_to_le16(crclen);
1568                use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
1569                                                           sizeof(struct tag),
1570                                                           crclen));
1571                use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
1572
1573                goto out;
1574        }
1575
1576        if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1577                fe->uid = cpu_to_le32(-1);
1578        else
1579                fe->uid = cpu_to_le32(i_uid_read(inode));
1580
1581        if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1582                fe->gid = cpu_to_le32(-1);
1583        else
1584                fe->gid = cpu_to_le32(i_gid_read(inode));
1585
1586        udfperms = ((inode->i_mode & S_IRWXO)) |
1587                   ((inode->i_mode & S_IRWXG) << 2) |
1588                   ((inode->i_mode & S_IRWXU) << 4);
1589
1590        udfperms |= (le32_to_cpu(fe->permissions) &
1591                    (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1592                     FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1593                     FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1594        fe->permissions = cpu_to_le32(udfperms);
1595
1596        if (S_ISDIR(inode->i_mode))
1597                fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1598        else
1599                fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1600
1601        fe->informationLength = cpu_to_le64(inode->i_size);
1602
1603        if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1604                struct regid *eid;
1605                struct deviceSpec *dsea =
1606                        (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1607                if (!dsea) {
1608                        dsea = (struct deviceSpec *)
1609                                udf_add_extendedattr(inode,
1610                                                     sizeof(struct deviceSpec) +
1611                                                     sizeof(struct regid), 12, 0x3);
1612                        dsea->attrType = cpu_to_le32(12);
1613                        dsea->attrSubtype = 1;
1614                        dsea->attrLength = cpu_to_le32(
1615                                                sizeof(struct deviceSpec) +
1616                                                sizeof(struct regid));
1617                        dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1618                }
1619                eid = (struct regid *)dsea->impUse;
1620                memset(eid, 0, sizeof(struct regid));
1621                strcpy(eid->ident, UDF_ID_DEVELOPER);
1622                eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1623                eid->identSuffix[1] = UDF_OS_ID_LINUX;
1624                dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1625                dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1626        }
1627
1628        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1629                lb_recorded = 0; /* No extents => no blocks! */
1630        else
1631                lb_recorded =
1632                        (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1633                        (blocksize_bits - 9);
1634
1635        if (iinfo->i_efe == 0) {
1636                memcpy(bh->b_data + sizeof(struct fileEntry),
1637                       iinfo->i_ext.i_data,
1638                       inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1639                fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1640
1641                udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1642                udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1643                udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1644                memset(&(fe->impIdent), 0, sizeof(struct regid));
1645                strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1646                fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1647                fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1648                fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1649                fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1650                fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1651                fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1652                fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1653                crclen = sizeof(struct fileEntry);
1654        } else {
1655                memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1656                       iinfo->i_ext.i_data,
1657                       inode->i_sb->s_blocksize -
1658                                        sizeof(struct extendedFileEntry));
1659                efe->objectSize = cpu_to_le64(inode->i_size);
1660                efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1661
1662                if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1663                    (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1664                     iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1665                        iinfo->i_crtime = inode->i_atime;
1666
1667                if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1668                    (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1669                     iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1670                        iinfo->i_crtime = inode->i_mtime;
1671
1672                if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1673                    (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1674                     iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1675                        iinfo->i_crtime = inode->i_ctime;
1676
1677                udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1678                udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1679                udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1680                udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1681
1682                memset(&(efe->impIdent), 0, sizeof(struct regid));
1683                strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1684                efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1685                efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1686                efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1687                efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1688                efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1689                efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1690                efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1691                crclen = sizeof(struct extendedFileEntry);
1692        }
1693        if (iinfo->i_strat4096) {
1694                fe->icbTag.strategyType = cpu_to_le16(4096);
1695                fe->icbTag.strategyParameter = cpu_to_le16(1);
1696                fe->icbTag.numEntries = cpu_to_le16(2);
1697        } else {
1698                fe->icbTag.strategyType = cpu_to_le16(4);
1699                fe->icbTag.numEntries = cpu_to_le16(1);
1700        }
1701
1702        if (S_ISDIR(inode->i_mode))
1703                fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1704        else if (S_ISREG(inode->i_mode))
1705                fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1706        else if (S_ISLNK(inode->i_mode))
1707                fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1708        else if (S_ISBLK(inode->i_mode))
1709                fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1710        else if (S_ISCHR(inode->i_mode))
1711                fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1712        else if (S_ISFIFO(inode->i_mode))
1713                fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1714        else if (S_ISSOCK(inode->i_mode))
1715                fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1716
1717        icbflags =      iinfo->i_alloc_type |
1718                        ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1719                        ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1720                        ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1721                        (le16_to_cpu(fe->icbTag.flags) &
1722                                ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1723                                ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1724
1725        fe->icbTag.flags = cpu_to_le16(icbflags);
1726        if (sbi->s_udfrev >= 0x0200)
1727                fe->descTag.descVersion = cpu_to_le16(3);
1728        else
1729                fe->descTag.descVersion = cpu_to_le16(2);
1730        fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1731        fe->descTag.tagLocation = cpu_to_le32(
1732                                        iinfo->i_location.logicalBlockNum);
1733        crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1734        fe->descTag.descCRCLength = cpu_to_le16(crclen);
1735        fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1736                                                  crclen));
1737        fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1738
1739out:
1740        set_buffer_uptodate(bh);
1741        unlock_buffer(bh);
1742
1743        /* write the data blocks */
1744        mark_buffer_dirty(bh);
1745        if (do_sync) {
1746                sync_dirty_buffer(bh);
1747                if (buffer_write_io_error(bh)) {
1748                        udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1749                                 inode->i_ino);
1750                        err = -EIO;
1751                }
1752        }
1753        brelse(bh);
1754
1755        return err;
1756}
1757
1758struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
1759{
1760        unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1761        struct inode *inode = iget_locked(sb, block);
1762
1763        if (!inode)
1764                return NULL;
1765
1766        if (inode->i_state & I_NEW) {
1767                memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1768                __udf_read_inode(inode);
1769                unlock_new_inode(inode);
1770        }
1771
1772        if (is_bad_inode(inode))
1773                goto out_iput;
1774
1775        if (ino->logicalBlockNum >= UDF_SB(sb)->
1776                        s_partmaps[ino->partitionReferenceNum].s_partition_len) {
1777                udf_debug("block=%d, partition=%d out of range\n",
1778                          ino->logicalBlockNum, ino->partitionReferenceNum);
1779                make_bad_inode(inode);
1780                goto out_iput;
1781        }
1782
1783        return inode;
1784
1785 out_iput:
1786        iput(inode);
1787        return NULL;
1788}
1789
1790int udf_add_aext(struct inode *inode, struct extent_position *epos,
1791                 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1792{
1793        int adsize;
1794        struct short_ad *sad = NULL;
1795        struct long_ad *lad = NULL;
1796        struct allocExtDesc *aed;
1797        uint8_t *ptr;
1798        struct udf_inode_info *iinfo = UDF_I(inode);
1799
1800        if (!epos->bh)
1801                ptr = iinfo->i_ext.i_data + epos->offset -
1802                        udf_file_entry_alloc_offset(inode) +
1803                        iinfo->i_lenEAttr;
1804        else
1805                ptr = epos->bh->b_data + epos->offset;
1806
1807        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1808                adsize = sizeof(struct short_ad);
1809        else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1810                adsize = sizeof(struct long_ad);
1811        else
1812                return -EIO;
1813
1814        if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1815                unsigned char *sptr, *dptr;
1816                struct buffer_head *nbh;
1817                int err, loffset;
1818                struct kernel_lb_addr obloc = epos->block;
1819
1820                epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1821                                                obloc.partitionReferenceNum,
1822                                                obloc.logicalBlockNum, &err);
1823                if (!epos->block.logicalBlockNum)
1824                        return -ENOSPC;
1825                nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1826                                                                 &epos->block,
1827                                                                 0));
1828                if (!nbh)
1829                        return -EIO;
1830                lock_buffer(nbh);
1831                memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1832                set_buffer_uptodate(nbh);
1833                unlock_buffer(nbh);
1834                mark_buffer_dirty_inode(nbh, inode);
1835
1836                aed = (struct allocExtDesc *)(nbh->b_data);
1837                if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1838                        aed->previousAllocExtLocation =
1839                                        cpu_to_le32(obloc.logicalBlockNum);
1840                if (epos->offset + adsize > inode->i_sb->s_blocksize) {
1841                        loffset = epos->offset;
1842                        aed->lengthAllocDescs = cpu_to_le32(adsize);
1843                        sptr = ptr - adsize;
1844                        dptr = nbh->b_data + sizeof(struct allocExtDesc);
1845                        memcpy(dptr, sptr, adsize);
1846                        epos->offset = sizeof(struct allocExtDesc) + adsize;
1847                } else {
1848                        loffset = epos->offset + adsize;
1849                        aed->lengthAllocDescs = cpu_to_le32(0);
1850                        sptr = ptr;
1851                        epos->offset = sizeof(struct allocExtDesc);
1852
1853                        if (epos->bh) {
1854                                aed = (struct allocExtDesc *)epos->bh->b_data;
1855                                le32_add_cpu(&aed->lengthAllocDescs, adsize);
1856                        } else {
1857                                iinfo->i_lenAlloc += adsize;
1858                                mark_inode_dirty(inode);
1859                        }
1860                }
1861                if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1862                        udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1863                                    epos->block.logicalBlockNum, sizeof(struct tag));
1864                else
1865                        udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1866                                    epos->block.logicalBlockNum, sizeof(struct tag));
1867                switch (iinfo->i_alloc_type) {
1868                case ICBTAG_FLAG_AD_SHORT:
1869                        sad = (struct short_ad *)sptr;
1870                        sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1871                                                     inode->i_sb->s_blocksize);
1872                        sad->extPosition =
1873                                cpu_to_le32(epos->block.logicalBlockNum);
1874                        break;
1875                case ICBTAG_FLAG_AD_LONG:
1876                        lad = (struct long_ad *)sptr;
1877                        lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1878                                                     inode->i_sb->s_blocksize);
1879                        lad->extLocation = cpu_to_lelb(epos->block);
1880                        memset(lad->impUse, 0x00, sizeof(lad->impUse));
1881                        break;
1882                }
1883                if (epos->bh) {
1884                        if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1885                            UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1886                                udf_update_tag(epos->bh->b_data, loffset);
1887                        else
1888                                udf_update_tag(epos->bh->b_data,
1889                                                sizeof(struct allocExtDesc));
1890                        mark_buffer_dirty_inode(epos->bh, inode);
1891                        brelse(epos->bh);
1892                } else {
1893                        mark_inode_dirty(inode);
1894                }
1895                epos->bh = nbh;
1896        }
1897
1898        udf_write_aext(inode, epos, eloc, elen, inc);
1899
1900        if (!epos->bh) {
1901                iinfo->i_lenAlloc += adsize;
1902                mark_inode_dirty(inode);
1903        } else {
1904                aed = (struct allocExtDesc *)epos->bh->b_data;
1905                le32_add_cpu(&aed->lengthAllocDescs, adsize);
1906                if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1907                                UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1908                        udf_update_tag(epos->bh->b_data,
1909                                        epos->offset + (inc ? 0 : adsize));
1910                else
1911                        udf_update_tag(epos->bh->b_data,
1912                                        sizeof(struct allocExtDesc));
1913                mark_buffer_dirty_inode(epos->bh, inode);
1914        }
1915
1916        return 0;
1917}
1918
1919void udf_write_aext(struct inode *inode, struct extent_position *epos,
1920                    struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1921{
1922        int adsize;
1923        uint8_t *ptr;
1924        struct short_ad *sad;
1925        struct long_ad *lad;
1926        struct udf_inode_info *iinfo = UDF_I(inode);
1927
1928        if (!epos->bh)
1929                ptr = iinfo->i_ext.i_data + epos->offset -
1930                        udf_file_entry_alloc_offset(inode) +
1931                        iinfo->i_lenEAttr;
1932        else
1933                ptr = epos->bh->b_data + epos->offset;
1934
1935        switch (iinfo->i_alloc_type) {
1936        case ICBTAG_FLAG_AD_SHORT:
1937                sad = (struct short_ad *)ptr;
1938                sad->extLength = cpu_to_le32(elen);
1939                sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
1940                adsize = sizeof(struct short_ad);
1941                break;
1942        case ICBTAG_FLAG_AD_LONG:
1943                lad = (struct long_ad *)ptr;
1944                lad->extLength = cpu_to_le32(elen);
1945                lad->extLocation = cpu_to_lelb(*eloc);
1946                memset(lad->impUse, 0x00, sizeof(lad->impUse));
1947                adsize = sizeof(struct long_ad);
1948                break;
1949        default:
1950                return;
1951        }
1952
1953        if (epos->bh) {
1954                if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1955                    UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
1956                        struct allocExtDesc *aed =
1957                                (struct allocExtDesc *)epos->bh->b_data;
1958                        udf_update_tag(epos->bh->b_data,
1959                                       le32_to_cpu(aed->lengthAllocDescs) +
1960                                       sizeof(struct allocExtDesc));
1961                }
1962                mark_buffer_dirty_inode(epos->bh, inode);
1963        } else {
1964                mark_inode_dirty(inode);
1965        }
1966
1967        if (inc)
1968                epos->offset += adsize;
1969}
1970
1971int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1972                     struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1973{
1974        int8_t etype;
1975
1976        while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
1977               (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
1978                int block;
1979                epos->block = *eloc;
1980                epos->offset = sizeof(struct allocExtDesc);
1981                brelse(epos->bh);
1982                block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
1983                epos->bh = udf_tread(inode->i_sb, block);
1984                if (!epos->bh) {
1985                        udf_debug("reading block %d failed!\n", block);
1986                        return -1;
1987                }
1988        }
1989
1990        return etype;
1991}
1992
1993int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1994                        struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1995{
1996        int alen;
1997        int8_t etype;
1998        uint8_t *ptr;
1999        struct short_ad *sad;
2000        struct long_ad *lad;
2001        struct udf_inode_info *iinfo = UDF_I(inode);
2002
2003        if (!epos->bh) {
2004                if (!epos->offset)
2005                        epos->offset = udf_file_entry_alloc_offset(inode);
2006                ptr = iinfo->i_ext.i_data + epos->offset -
2007                        udf_file_entry_alloc_offset(inode) +
2008                        iinfo->i_lenEAttr;
2009                alen = udf_file_entry_alloc_offset(inode) +
2010                                                        iinfo->i_lenAlloc;
2011        } else {
2012                if (!epos->offset)
2013                        epos->offset = sizeof(struct allocExtDesc);
2014                ptr = epos->bh->b_data + epos->offset;
2015                alen = sizeof(struct allocExtDesc) +
2016                        le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2017                                                        lengthAllocDescs);
2018        }
2019
2020        switch (iinfo->i_alloc_type) {
2021        case ICBTAG_FLAG_AD_SHORT:
2022                sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2023                if (!sad)
2024                        return -1;
2025                etype = le32_to_cpu(sad->extLength) >> 30;
2026                eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2027                eloc->partitionReferenceNum =
2028                                iinfo->i_location.partitionReferenceNum;
2029                *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2030                break;
2031        case ICBTAG_FLAG_AD_LONG:
2032                lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2033                if (!lad)
2034                        return -1;
2035                etype = le32_to_cpu(lad->extLength) >> 30;
2036                *eloc = lelb_to_cpu(lad->extLocation);
2037                *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2038                break;
2039        default:
2040                udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2041                return -1;
2042        }
2043
2044        return etype;
2045}
2046
2047static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2048                              struct kernel_lb_addr neloc, uint32_t nelen)
2049{
2050        struct kernel_lb_addr oeloc;
2051        uint32_t oelen;
2052        int8_t etype;
2053
2054        if (epos.bh)
2055                get_bh(epos.bh);
2056
2057        while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2058                udf_write_aext(inode, &epos, &neloc, nelen, 1);
2059                neloc = oeloc;
2060                nelen = (etype << 30) | oelen;
2061        }
2062        udf_add_aext(inode, &epos, &neloc, nelen, 1);
2063        brelse(epos.bh);
2064
2065        return (nelen >> 30);
2066}
2067
2068int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2069                       struct kernel_lb_addr eloc, uint32_t elen)
2070{
2071        struct extent_position oepos;
2072        int adsize;
2073        int8_t etype;
2074        struct allocExtDesc *aed;
2075        struct udf_inode_info *iinfo;
2076
2077        if (epos.bh) {
2078                get_bh(epos.bh);
2079                get_bh(epos.bh);
2080        }
2081
2082        iinfo = UDF_I(inode);
2083        if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2084                adsize = sizeof(struct short_ad);
2085        else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2086                adsize = sizeof(struct long_ad);
2087        else
2088                adsize = 0;
2089
2090        oepos = epos;
2091        if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2092                return -1;
2093
2094        while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2095                udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2096                if (oepos.bh != epos.bh) {
2097                        oepos.block = epos.block;
2098                        brelse(oepos.bh);
2099                        get_bh(epos.bh);
2100                        oepos.bh = epos.bh;
2101                        oepos.offset = epos.offset - adsize;
2102                }
2103        }
2104        memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2105        elen = 0;
2106
2107        if (epos.bh != oepos.bh) {
2108                udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2109                udf_write_aext(inode, &oepos, &eloc, elen, 1);
2110                udf_write_aext(inode, &oepos, &eloc, elen, 1);
2111                if (!oepos.bh) {
2112                        iinfo->i_lenAlloc -= (adsize * 2);
2113                        mark_inode_dirty(inode);
2114                } else {
2115                        aed = (struct allocExtDesc *)oepos.bh->b_data;
2116                        le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2117                        if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2118                            UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2119                                udf_update_tag(oepos.bh->b_data,
2120                                                oepos.offset - (2 * adsize));
2121                        else
2122                                udf_update_tag(oepos.bh->b_data,
2123                                                sizeof(struct allocExtDesc));
2124                        mark_buffer_dirty_inode(oepos.bh, inode);
2125                }
2126        } else {
2127                udf_write_aext(inode, &oepos, &eloc, elen, 1);
2128                if (!oepos.bh) {
2129                        iinfo->i_lenAlloc -= adsize;
2130                        mark_inode_dirty(inode);
2131                } else {
2132                        aed = (struct allocExtDesc *)oepos.bh->b_data;
2133                        le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2134                        if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2135                            UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2136                                udf_update_tag(oepos.bh->b_data,
2137                                                epos.offset - adsize);
2138                        else
2139                                udf_update_tag(oepos.bh->b_data,
2140                                                sizeof(struct allocExtDesc));
2141                        mark_buffer_dirty_inode(oepos.bh, inode);
2142                }
2143        }
2144
2145        brelse(epos.bh);
2146        brelse(oepos.bh);
2147
2148        return (elen >> 30);
2149}
2150
2151int8_t inode_bmap(struct inode *inode, sector_t block,
2152                  struct extent_position *pos, struct kernel_lb_addr *eloc,
2153                  uint32_t *elen, sector_t *offset)
2154{
2155        unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2156        loff_t lbcount = 0, bcount =
2157            (loff_t) block << blocksize_bits;
2158        int8_t etype;
2159        struct udf_inode_info *iinfo;
2160
2161        iinfo = UDF_I(inode);
2162        pos->offset = 0;
2163        pos->block = iinfo->i_location;
2164        pos->bh = NULL;
2165        *elen = 0;
2166
2167        do {
2168                etype = udf_next_aext(inode, pos, eloc, elen, 1);
2169                if (etype == -1) {
2170                        *offset = (bcount - lbcount) >> blocksize_bits;
2171                        iinfo->i_lenExtents = lbcount;
2172                        return -1;
2173                }
2174                lbcount += *elen;
2175        } while (lbcount <= bcount);
2176
2177        *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2178
2179        return etype;
2180}
2181
2182long udf_block_map(struct inode *inode, sector_t block)
2183{
2184        struct kernel_lb_addr eloc;
2185        uint32_t elen;
2186        sector_t offset;
2187        struct extent_position epos = {};
2188        int ret;
2189
2190        down_read(&UDF_I(inode)->i_data_sem);
2191
2192        if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2193                                                (EXT_RECORDED_ALLOCATED >> 30))
2194                ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2195        else
2196                ret = 0;
2197
2198        up_read(&UDF_I(inode)->i_data_sem);
2199        brelse(epos.bh);
2200
2201        if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2202                return udf_fixed_to_variable(ret);
2203        else
2204                return ret;
2205}
2206
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.