linux-bk/fs/ext2/inode.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/ext2/inode.c
   3 *
   4 * Copyright (C) 1992, 1993, 1994, 1995
   5 * Remy Card (card@masi.ibp.fr)
   6 * Laboratoire MASI - Institut Blaise Pascal
   7 * Universite Pierre et Marie Curie (Paris VI)
   8 *
   9 *  from
  10 *
  11 *  linux/fs/minix/inode.c
  12 *
  13 *  Copyright (C) 1991, 1992  Linus Torvalds
  14 *
  15 *  Goal-directed block allocation by Stephen Tweedie
  16 *      (sct@dcs.ed.ac.uk), 1993, 1998
  17 *  Big-endian to little-endian byte-swapping/bitmaps by
  18 *        David S. Miller (davem@caip.rutgers.edu), 1995
  19 *  64-bit file support on 64-bit platforms by Jakub Jelinek
  20 *      (jj@sunsite.ms.mff.cuni.cz)
  21 *
  22 *  Assorted race fixes, rewrite of ext2_get_block() by Al Viro, 2000
  23 */
  24
  25#include <linux/smp_lock.h>
  26#include <linux/time.h>
  27#include <linux/highuid.h>
  28#include <linux/pagemap.h>
  29#include <linux/quotaops.h>
  30#include <linux/module.h>
  31#include <linux/writeback.h>
  32#include <linux/buffer_head.h>
  33#include <linux/mpage.h>
  34#include "ext2.h"
  35#include "acl.h"
  36
  37MODULE_AUTHOR("Remy Card and others");
  38MODULE_DESCRIPTION("Second Extended Filesystem");
  39MODULE_LICENSE("GPL");
  40
  41static int ext2_update_inode(struct inode * inode, int do_sync);
  42
  43/*
  44 * Test whether an inode is a fast symlink.
  45 */
  46static inline int ext2_inode_is_fast_symlink(struct inode *inode)
  47{
  48        int ea_blocks = EXT2_I(inode)->i_file_acl ?
  49                (inode->i_sb->s_blocksize >> 9) : 0;
  50
  51        return (S_ISLNK(inode->i_mode) &&
  52                inode->i_blocks - ea_blocks == 0);
  53}
  54
  55/*
  56 * Called at the last iput() if i_nlink is zero.
  57 */
  58void ext2_delete_inode (struct inode * inode)
  59{
  60        if (is_bad_inode(inode))
  61                goto no_delete;
  62        EXT2_I(inode)->i_dtime  = get_seconds();
  63        mark_inode_dirty(inode);
  64        ext2_update_inode(inode, inode_needs_sync(inode));
  65
  66        inode->i_size = 0;
  67        if (inode->i_blocks)
  68                ext2_truncate (inode);
  69        ext2_free_inode (inode);
  70
  71        return;
  72no_delete:
  73        clear_inode(inode);     /* We must guarantee clearing of inode... */
  74}
  75
  76void ext2_discard_prealloc (struct inode * inode)
  77{
  78#ifdef EXT2_PREALLOCATE
  79        struct ext2_inode_info *ei = EXT2_I(inode);
  80        write_lock(&ei->i_meta_lock);
  81        if (ei->i_prealloc_count) {
  82                unsigned short total = ei->i_prealloc_count;
  83                unsigned long block = ei->i_prealloc_block;
  84                ei->i_prealloc_count = 0;
  85                ei->i_prealloc_block = 0;
  86                write_unlock(&ei->i_meta_lock);
  87                ext2_free_blocks (inode, block, total);
  88                return;
  89        } else
  90                write_unlock(&ei->i_meta_lock);
  91#endif
  92}
  93
  94static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err)
  95{
  96#ifdef EXT2FS_DEBUG
  97        static unsigned long alloc_hits, alloc_attempts;
  98#endif
  99        unsigned long result;
 100
 101
 102#ifdef EXT2_PREALLOCATE
 103        struct ext2_inode_info *ei = EXT2_I(inode);
 104        write_lock(&ei->i_meta_lock);
 105        if (ei->i_prealloc_count &&
 106            (goal == ei->i_prealloc_block || goal + 1 == ei->i_prealloc_block))
 107        {
 108                result = ei->i_prealloc_block++;
 109                ei->i_prealloc_count--;
 110                write_unlock(&ei->i_meta_lock);
 111                ext2_debug ("preallocation hit (%lu/%lu).\n",
 112                            ++alloc_hits, ++alloc_attempts);
 113        } else {
 114                write_unlock(&ei->i_meta_lock);
 115                ext2_discard_prealloc (inode);
 116                ext2_debug ("preallocation miss (%lu/%lu).\n",
 117                            alloc_hits, ++alloc_attempts);
 118                if (S_ISREG(inode->i_mode))
 119                        result = ext2_new_block (inode, goal, 
 120                                 &ei->i_prealloc_count,
 121                                 &ei->i_prealloc_block, err);
 122                else
 123                        result = ext2_new_block(inode, goal, NULL, NULL, err);
 124        }
 125#else
 126        result = ext2_new_block (inode, goal, 0, 0, err);
 127#endif
 128        return result;
 129}
 130
 131typedef struct {
 132        __le32  *p;
 133        __le32  key;
 134        struct buffer_head *bh;
 135} Indirect;
 136
 137static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
 138{
 139        p->key = *(p->p = v);
 140        p->bh = bh;
 141}
 142
 143static inline int verify_chain(Indirect *from, Indirect *to)
 144{
 145        while (from <= to && from->key == *from->p)
 146                from++;
 147        return (from > to);
 148}
 149
 150/**
 151 *      ext2_block_to_path - parse the block number into array of offsets
 152 *      @inode: inode in question (we are only interested in its superblock)
 153 *      @i_block: block number to be parsed
 154 *      @offsets: array to store the offsets in
 155 *      @boundary: set this non-zero if the referred-to block is likely to be
 156 *             followed (on disk) by an indirect block.
 157 *      To store the locations of file's data ext2 uses a data structure common
 158 *      for UNIX filesystems - tree of pointers anchored in the inode, with
 159 *      data blocks at leaves and indirect blocks in intermediate nodes.
 160 *      This function translates the block number into path in that tree -
 161 *      return value is the path length and @offsets[n] is the offset of
 162 *      pointer to (n+1)th node in the nth one. If @block is out of range
 163 *      (negative or too large) warning is printed and zero returned.
 164 *
 165 *      Note: function doesn't find node addresses, so no IO is needed. All
 166 *      we need to know is the capacity of indirect blocks (taken from the
 167 *      inode->i_sb).
 168 */
 169
 170/*
 171 * Portability note: the last comparison (check that we fit into triple
 172 * indirect block) is spelled differently, because otherwise on an
 173 * architecture with 32-bit longs and 8Kb pages we might get into trouble
 174 * if our filesystem had 8Kb blocks. We might use long long, but that would
 175 * kill us on x86. Oh, well, at least the sign propagation does not matter -
 176 * i_block would have to be negative in the very beginning, so we would not
 177 * get there at all.
 178 */
 179
 180static int ext2_block_to_path(struct inode *inode,
 181                        long i_block, int offsets[4], int *boundary)
 182{
 183        int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 184        int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
 185        const long direct_blocks = EXT2_NDIR_BLOCKS,
 186                indirect_blocks = ptrs,
 187                double_blocks = (1 << (ptrs_bits * 2));
 188        int n = 0;
 189        int final = 0;
 190
 191        if (i_block < 0) {
 192                ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0");
 193        } else if (i_block < direct_blocks) {
 194                offsets[n++] = i_block;
 195                final = direct_blocks;
 196        } else if ( (i_block -= direct_blocks) < indirect_blocks) {
 197                offsets[n++] = EXT2_IND_BLOCK;
 198                offsets[n++] = i_block;
 199                final = ptrs;
 200        } else if ((i_block -= indirect_blocks) < double_blocks) {
 201                offsets[n++] = EXT2_DIND_BLOCK;
 202                offsets[n++] = i_block >> ptrs_bits;
 203                offsets[n++] = i_block & (ptrs - 1);
 204                final = ptrs;
 205        } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
 206                offsets[n++] = EXT2_TIND_BLOCK;
 207                offsets[n++] = i_block >> (ptrs_bits * 2);
 208                offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
 209                offsets[n++] = i_block & (ptrs - 1);
 210                final = ptrs;
 211        } else {
 212                ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
 213        }
 214        if (boundary)
 215                *boundary = (i_block & (ptrs - 1)) == (final - 1);
 216        return n;
 217}
 218
 219/**
 220 *      ext2_get_branch - read the chain of indirect blocks leading to data
 221 *      @inode: inode in question
 222 *      @depth: depth of the chain (1 - direct pointer, etc.)
 223 *      @offsets: offsets of pointers in inode/indirect blocks
 224 *      @chain: place to store the result
 225 *      @err: here we store the error value
 226 *
 227 *      Function fills the array of triples <key, p, bh> and returns %NULL
 228 *      if everything went OK or the pointer to the last filled triple
 229 *      (incomplete one) otherwise. Upon the return chain[i].key contains
 230 *      the number of (i+1)-th block in the chain (as it is stored in memory,
 231 *      i.e. little-endian 32-bit), chain[i].p contains the address of that
 232 *      number (it points into struct inode for i==0 and into the bh->b_data
 233 *      for i>0) and chain[i].bh points to the buffer_head of i-th indirect
 234 *      block for i>0 and NULL for i==0. In other words, it holds the block
 235 *      numbers of the chain, addresses they were taken from (and where we can
 236 *      verify that chain did not change) and buffer_heads hosting these
 237 *      numbers.
 238 *
 239 *      Function stops when it stumbles upon zero pointer (absent block)
 240 *              (pointer to last triple returned, *@err == 0)
 241 *      or when it gets an IO error reading an indirect block
 242 *              (ditto, *@err == -EIO)
 243 *      or when it notices that chain had been changed while it was reading
 244 *              (ditto, *@err == -EAGAIN)
 245 *      or when it reads all @depth-1 indirect blocks successfully and finds
 246 *      the whole chain, all way to the data (returns %NULL, *err == 0).
 247 */
 248static Indirect *ext2_get_branch(struct inode *inode,
 249                                 int depth,
 250                                 int *offsets,
 251                                 Indirect chain[4],
 252                                 int *err)
 253{
 254        struct super_block *sb = inode->i_sb;
 255        Indirect *p = chain;
 256        struct buffer_head *bh;
 257
 258        *err = 0;
 259        /* i_data is not going away, no lock needed */
 260        add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets);
 261        if (!p->key)
 262                goto no_block;
 263        while (--depth) {
 264                bh = sb_bread(sb, le32_to_cpu(p->key));
 265                if (!bh)
 266                        goto failure;
 267                read_lock(&EXT2_I(inode)->i_meta_lock);
 268                if (!verify_chain(chain, p))
 269                        goto changed;
 270                add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
 271                read_unlock(&EXT2_I(inode)->i_meta_lock);
 272                if (!p->key)
 273                        goto no_block;
 274        }
 275        return NULL;
 276
 277changed:
 278        read_unlock(&EXT2_I(inode)->i_meta_lock);
 279        brelse(bh);
 280        *err = -EAGAIN;
 281        goto no_block;
 282failure:
 283        *err = -EIO;
 284no_block:
 285        return p;
 286}
 287
 288/**
 289 *      ext2_find_near - find a place for allocation with sufficient locality
 290 *      @inode: owner
 291 *      @ind: descriptor of indirect block.
 292 *
 293 *      This function returns the prefered place for block allocation.
 294 *      It is used when heuristic for sequential allocation fails.
 295 *      Rules are:
 296 *        + if there is a block to the left of our position - allocate near it.
 297 *        + if pointer will live in indirect block - allocate near that block.
 298 *        + if pointer will live in inode - allocate in the same cylinder group.
 299 *
 300 * In the latter case we colour the starting block by the callers PID to
 301 * prevent it from clashing with concurrent allocations for a different inode
 302 * in the same block group.   The PID is used here so that functionally related
 303 * files will be close-by on-disk.
 304 *
 305 *      Caller must make sure that @ind is valid and will stay that way.
 306 */
 307
 308static unsigned long ext2_find_near(struct inode *inode, Indirect *ind)
 309{
 310        struct ext2_inode_info *ei = EXT2_I(inode);
 311        __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
 312        __le32 *p;
 313        unsigned long bg_start;
 314        unsigned long colour;
 315
 316        /* Try to find previous block */
 317        for (p = ind->p - 1; p >= start; p--)
 318                if (*p)
 319                        return le32_to_cpu(*p);
 320
 321        /* No such thing, so let's try location of indirect block */
 322        if (ind->bh)
 323                return ind->bh->b_blocknr;
 324
 325        /*
 326         * It is going to be refered from inode itself? OK, just put it into
 327         * the same cylinder group then.
 328         */
 329        bg_start = (ei->i_block_group * EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
 330                le32_to_cpu(EXT2_SB(inode->i_sb)->s_es->s_first_data_block);
 331        colour = (current->pid % 16) *
 332                        (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16);
 333        return bg_start + colour;
 334}
 335
 336/**
 337 *      ext2_find_goal - find a prefered place for allocation.
 338 *      @inode: owner
 339 *      @block:  block we want
 340 *      @chain:  chain of indirect blocks
 341 *      @partial: pointer to the last triple within a chain
 342 *      @goal:  place to store the result.
 343 *
 344 *      Normally this function find the prefered place for block allocation,
 345 *      stores it in *@goal and returns zero. If the branch had been changed
 346 *      under us we return -EAGAIN.
 347 */
 348
 349static inline int ext2_find_goal(struct inode *inode,
 350                                 long block,
 351                                 Indirect chain[4],
 352                                 Indirect *partial,
 353                                 unsigned long *goal)
 354{
 355        struct ext2_inode_info *ei = EXT2_I(inode);
 356        write_lock(&ei->i_meta_lock);
 357        if ((block == ei->i_next_alloc_block + 1) && ei->i_next_alloc_goal) {
 358                ei->i_next_alloc_block++;
 359                ei->i_next_alloc_goal++;
 360        } 
 361        if (verify_chain(chain, partial)) {
 362                /*
 363                 * try the heuristic for sequential allocation,
 364                 * failing that at least try to get decent locality.
 365                 */
 366                if (block == ei->i_next_alloc_block)
 367                        *goal = ei->i_next_alloc_goal;
 368                if (!*goal)
 369                        *goal = ext2_find_near(inode, partial);
 370                write_unlock(&ei->i_meta_lock);
 371                return 0;
 372        }
 373        write_unlock(&ei->i_meta_lock);
 374        return -EAGAIN;
 375}
 376
 377/**
 378 *      ext2_alloc_branch - allocate and set up a chain of blocks.
 379 *      @inode: owner
 380 *      @num: depth of the chain (number of blocks to allocate)
 381 *      @offsets: offsets (in the blocks) to store the pointers to next.
 382 *      @branch: place to store the chain in.
 383 *
 384 *      This function allocates @num blocks, zeroes out all but the last one,
 385 *      links them into chain and (if we are synchronous) writes them to disk.
 386 *      In other words, it prepares a branch that can be spliced onto the
 387 *      inode. It stores the information about that chain in the branch[], in
 388 *      the same format as ext2_get_branch() would do. We are calling it after
 389 *      we had read the existing part of chain and partial points to the last
 390 *      triple of that (one with zero ->key). Upon the exit we have the same
 391 *      picture as after the successful ext2_get_block(), excpet that in one
 392 *      place chain is disconnected - *branch->p is still zero (we did not
 393 *      set the last link), but branch->key contains the number that should
 394 *      be placed into *branch->p to fill that gap.
 395 *
 396 *      If allocation fails we free all blocks we've allocated (and forget
 397 *      their buffer_heads) and return the error value the from failed
 398 *      ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
 399 *      as described above and return 0.
 400 */
 401
 402static int ext2_alloc_branch(struct inode *inode,
 403                             int num,
 404                             unsigned long goal,
 405                             int *offsets,
 406                             Indirect *branch)
 407{
 408        int blocksize = inode->i_sb->s_blocksize;
 409        int n = 0;
 410        int err;
 411        int i;
 412        int parent = ext2_alloc_block(inode, goal, &err);
 413
 414        branch[0].key = cpu_to_le32(parent);
 415        if (parent) for (n = 1; n < num; n++) {
 416                struct buffer_head *bh;
 417                /* Allocate the next block */
 418                int nr = ext2_alloc_block(inode, parent, &err);
 419                if (!nr)
 420                        break;
 421                branch[n].key = cpu_to_le32(nr);
 422                /*
 423                 * Get buffer_head for parent block, zero it out and set 
 424                 * the pointer to new one, then send parent to disk.
 425                 */
 426                bh = sb_getblk(inode->i_sb, parent);
 427                lock_buffer(bh);
 428                memset(bh->b_data, 0, blocksize);
 429                branch[n].bh = bh;
 430                branch[n].p = (__le32 *) bh->b_data + offsets[n];
 431                *branch[n].p = branch[n].key;
 432                set_buffer_uptodate(bh);
 433                unlock_buffer(bh);
 434                mark_buffer_dirty_inode(bh, inode);
 435                /* We used to sync bh here if IS_SYNC(inode).
 436                 * But we now rely upon generic_osync_inode()
 437                 * and b_inode_buffers.  But not for directories.
 438                 */
 439                if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
 440                        sync_dirty_buffer(bh);
 441                parent = nr;
 442        }
 443        if (n == num)
 444                return 0;
 445
 446        /* Allocation failed, free what we already allocated */
 447        for (i = 1; i < n; i++)
 448                bforget(branch[i].bh);
 449        for (i = 0; i < n; i++)
 450                ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1);
 451        return err;
 452}
 453
 454/**
 455 *      ext2_splice_branch - splice the allocated branch onto inode.
 456 *      @inode: owner
 457 *      @block: (logical) number of block we are adding
 458 *      @chain: chain of indirect blocks (with a missing link - see
 459 *              ext2_alloc_branch)
 460 *      @where: location of missing link
 461 *      @num:   number of blocks we are adding
 462 *
 463 *      This function verifies that chain (up to the missing link) had not
 464 *      changed, fills the missing link and does all housekeeping needed in
 465 *      inode (->i_blocks, etc.). In case of success we end up with the full
 466 *      chain to new block and return 0. Otherwise (== chain had been changed)
 467 *      we free the new blocks (forgetting their buffer_heads, indeed) and
 468 *      return -EAGAIN.
 469 */
 470
 471static inline int ext2_splice_branch(struct inode *inode,
 472                                     long block,
 473                                     Indirect chain[4],
 474                                     Indirect *where,
 475                                     int num)
 476{
 477        struct ext2_inode_info *ei = EXT2_I(inode);
 478        int i;
 479
 480        /* Verify that place we are splicing to is still there and vacant */
 481
 482        write_lock(&ei->i_meta_lock);
 483        if (!verify_chain(chain, where-1) || *where->p)
 484                goto changed;
 485
 486        /* That's it */
 487
 488        *where->p = where->key;
 489        ei->i_next_alloc_block = block;
 490        ei->i_next_alloc_goal = le32_to_cpu(where[num-1].key);
 491
 492        write_unlock(&ei->i_meta_lock);
 493
 494        /* We are done with atomic stuff, now do the rest of housekeeping */
 495
 496        inode->i_ctime = CURRENT_TIME_SEC;
 497
 498        /* had we spliced it onto indirect block? */
 499        if (where->bh)
 500                mark_buffer_dirty_inode(where->bh, inode);
 501
 502        mark_inode_dirty(inode);
 503        return 0;
 504
 505changed:
 506        write_unlock(&ei->i_meta_lock);
 507        for (i = 1; i < num; i++)
 508                bforget(where[i].bh);
 509        for (i = 0; i < num; i++)
 510                ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1);
 511        return -EAGAIN;
 512}
 513
 514/*
 515 * Allocation strategy is simple: if we have to allocate something, we will
 516 * have to go the whole way to leaf. So let's do it before attaching anything
 517 * to tree, set linkage between the newborn blocks, write them if sync is
 518 * required, recheck the path, free and repeat if check fails, otherwise
 519 * set the last missing link (that will protect us from any truncate-generated
 520 * removals - all blocks on the path are immune now) and possibly force the
 521 * write on the parent block.
 522 * That has a nice additional property: no special recovery from the failed
 523 * allocations is needed - we simply release blocks and do not touch anything
 524 * reachable from inode.
 525 */
 526
 527int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
 528{
 529        int err = -EIO;
 530        int offsets[4];
 531        Indirect chain[4];
 532        Indirect *partial;
 533        unsigned long goal;
 534        int left;
 535        int boundary = 0;
 536        int depth = ext2_block_to_path(inode, iblock, offsets, &boundary);
 537
 538        if (depth == 0)
 539                goto out;
 540
 541reread:
 542        partial = ext2_get_branch(inode, depth, offsets, chain, &err);
 543
 544        /* Simplest case - block found, no allocation needed */
 545        if (!partial) {
 546got_it:
 547                map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
 548                if (boundary)
 549                        set_buffer_boundary(bh_result);
 550                /* Clean up and exit */
 551                partial = chain+depth-1; /* the whole chain */
 552                goto cleanup;
 553        }
 554
 555        /* Next simple case - plain lookup or failed read of indirect block */
 556        if (!create || err == -EIO) {
 557cleanup:
 558                while (partial > chain) {
 559                        brelse(partial->bh);
 560                        partial--;
 561                }
 562out:
 563                return err;
 564        }
 565
 566        /*
 567         * Indirect block might be removed by truncate while we were
 568         * reading it. Handling of that case (forget what we've got and
 569         * reread) is taken out of the main path.
 570         */
 571        if (err == -EAGAIN)
 572                goto changed;
 573
 574        goal = 0;
 575        if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0)
 576                goto changed;
 577
 578        left = (chain + depth) - partial;
 579        err = ext2_alloc_branch(inode, left, goal,
 580                                        offsets+(partial-chain), partial);
 581        if (err)
 582                goto cleanup;
 583
 584        if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0)
 585                goto changed;
 586
 587        set_buffer_new(bh_result);
 588        goto got_it;
 589
 590changed:
 591        while (partial > chain) {
 592                brelse(partial->bh);
 593                partial--;
 594        }
 595        goto reread;
 596}
 597
 598static int ext2_writepage(struct page *page, struct writeback_control *wbc)
 599{
 600        return block_write_full_page(page, ext2_get_block, wbc);
 601}
 602
 603static int ext2_readpage(struct file *file, struct page *page)
 604{
 605        return mpage_readpage(page, ext2_get_block);
 606}
 607
 608static int
 609ext2_readpages(struct file *file, struct address_space *mapping,
 610                struct list_head *pages, unsigned nr_pages)
 611{
 612        return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
 613}
 614
 615static int
 616ext2_prepare_write(struct file *file, struct page *page,
 617                        unsigned from, unsigned to)
 618{
 619        return block_prepare_write(page,from,to,ext2_get_block);
 620}
 621
 622static int
 623ext2_nobh_prepare_write(struct file *file, struct page *page,
 624                        unsigned from, unsigned to)
 625{
 626        return nobh_prepare_write(page,from,to,ext2_get_block);
 627}
 628
 629static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
 630{
 631        return generic_block_bmap(mapping,block,ext2_get_block);
 632}
 633
 634static int
 635ext2_get_blocks(struct inode *inode, sector_t iblock, unsigned long max_blocks,
 636                        struct buffer_head *bh_result, int create)
 637{
 638        int ret;
 639
 640        ret = ext2_get_block(inode, iblock, bh_result, create);
 641        if (ret == 0)
 642                bh_result->b_size = (1 << inode->i_blkbits);
 643        return ret;
 644}
 645
 646static ssize_t
 647ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
 648                        loff_t offset, unsigned long nr_segs)
 649{
 650        struct file *file = iocb->ki_filp;
 651        struct inode *inode = file->f_mapping->host;
 652
 653        return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
 654                                offset, nr_segs, ext2_get_blocks, NULL);
 655}
 656
 657static int
 658ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
 659{
 660        return mpage_writepages(mapping, wbc, ext2_get_block);
 661}
 662
 663struct address_space_operations ext2_aops = {
 664        .readpage               = ext2_readpage,
 665        .readpages              = ext2_readpages,
 666        .writepage              = ext2_writepage,
 667        .sync_page              = block_sync_page,
 668        .prepare_write          = ext2_prepare_write,
 669        .commit_write           = generic_commit_write,
 670        .bmap                   = ext2_bmap,
 671        .direct_IO              = ext2_direct_IO,
 672        .writepages             = ext2_writepages,
 673};
 674
 675struct address_space_operations ext2_nobh_aops = {
 676        .readpage               = ext2_readpage,
 677        .readpages              = ext2_readpages,
 678        .writepage              = ext2_writepage,
 679        .sync_page              = block_sync_page,
 680        .prepare_write          = ext2_nobh_prepare_write,
 681        .commit_write           = nobh_commit_write,
 682        .bmap                   = ext2_bmap,
 683        .direct_IO              = ext2_direct_IO,
 684        .writepages             = ext2_writepages,
 685};
 686
 687/*
 688 * Probably it should be a library function... search for first non-zero word
 689 * or memcmp with zero_page, whatever is better for particular architecture.
 690 * Linus?
 691 */
 692static inline int all_zeroes(__le32 *p, __le32 *q)
 693{
 694        while (p < q)
 695                if (*p++)
 696                        return 0;
 697        return 1;
 698}
 699
 700/**
 701 *      ext2_find_shared - find the indirect blocks for partial truncation.
 702 *      @inode:   inode in question
 703 *      @depth:   depth of the affected branch
 704 *      @offsets: offsets of pointers in that branch (see ext2_block_to_path)
 705 *      @chain:   place to store the pointers to partial indirect blocks
 706 *      @top:     place to the (detached) top of branch
 707 *
 708 *      This is a helper function used by ext2_truncate().
 709 *
 710 *      When we do truncate() we may have to clean the ends of several indirect
 711 *      blocks but leave the blocks themselves alive. Block is partially
 712 *      truncated if some data below the new i_size is refered from it (and
 713 *      it is on the path to the first completely truncated data block, indeed).
 714 *      We have to free the top of that path along with everything to the right
 715 *      of the path. Since no allocation past the truncation point is possible
 716 *      until ext2_truncate() finishes, we may safely do the latter, but top
 717 *      of branch may require special attention - pageout below the truncation
 718 *      point might try to populate it.
 719 *
 720 *      We atomically detach the top of branch from the tree, store the block
 721 *      number of its root in *@top, pointers to buffer_heads of partially
 722 *      truncated blocks - in @chain[].bh and pointers to their last elements
 723 *      that should not be removed - in @chain[].p. Return value is the pointer
 724 *      to last filled element of @chain.
 725 *
 726 *      The work left to caller to do the actual freeing of subtrees:
 727 *              a) free the subtree starting from *@top
 728 *              b) free the subtrees whose roots are stored in
 729 *                      (@chain[i].p+1 .. end of @chain[i].bh->b_data)
 730 *              c) free the subtrees growing from the inode past the @chain[0].p
 731 *                      (no partially truncated stuff there).
 732 */
 733
 734static Indirect *ext2_find_shared(struct inode *inode,
 735                                int depth,
 736                                int offsets[4],
 737                                Indirect chain[4],
 738                                __le32 *top)
 739{
 740        Indirect *partial, *p;
 741        int k, err;
 742
 743        *top = 0;
 744        for (k = depth; k > 1 && !offsets[k-1]; k--)
 745                ;
 746        partial = ext2_get_branch(inode, k, offsets, chain, &err);
 747        if (!partial)
 748                partial = chain + k-1;
 749        /*
 750         * If the branch acquired continuation since we've looked at it -
 751         * fine, it should all survive and (new) top doesn't belong to us.
 752         */
 753        write_lock(&EXT2_I(inode)->i_meta_lock);
 754        if (!partial->key && *partial->p) {
 755                write_unlock(&EXT2_I(inode)->i_meta_lock);
 756                goto no_top;
 757        }
 758        for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
 759                ;
 760        /*
 761         * OK, we've found the last block that must survive. The rest of our
 762         * branch should be detached before unlocking. However, if that rest
 763         * of branch is all ours and does not grow immediately from the inode
 764         * it's easier to cheat and just decrement partial->p.
 765         */
 766        if (p == chain + k - 1 && p > chain) {
 767                p->p--;
 768        } else {
 769                *top = *p->p;
 770                *p->p = 0;
 771        }
 772        write_unlock(&EXT2_I(inode)->i_meta_lock);
 773
 774        while(partial > p)
 775        {
 776                brelse(partial->bh);
 777                partial--;
 778        }
 779no_top:
 780        return partial;
 781}
 782
 783/**
 784 *      ext2_free_data - free a list of data blocks
 785 *      @inode: inode we are dealing with
 786 *      @p:     array of block numbers
 787 *      @q:     points immediately past the end of array
 788 *
 789 *      We are freeing all blocks refered from that array (numbers are
 790 *      stored as little-endian 32-bit) and updating @inode->i_blocks
 791 *      appropriately.
 792 */
 793static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
 794{
 795        unsigned long block_to_free = 0, count = 0;
 796        unsigned long nr;
 797
 798        for ( ; p < q ; p++) {
 799                nr = le32_to_cpu(*p);
 800                if (nr) {
 801                        *p = 0;
 802                        /* accumulate blocks to free if they're contiguous */
 803                        if (count == 0)
 804                                goto free_this;
 805                        else if (block_to_free == nr - count)
 806                                count++;
 807                        else {
 808                                mark_inode_dirty(inode);
 809                                ext2_free_blocks (inode, block_to_free, count);
 810                        free_this:
 811                                block_to_free = nr;
 812                                count = 1;
 813                        }
 814                }
 815        }
 816        if (count > 0) {
 817                mark_inode_dirty(inode);
 818                ext2_free_blocks (inode, block_to_free, count);
 819        }
 820}
 821
 822/**
 823 *      ext2_free_branches - free an array of branches
 824 *      @inode: inode we are dealing with
 825 *      @p:     array of block numbers
 826 *      @q:     pointer immediately past the end of array
 827 *      @depth: depth of the branches to free
 828 *
 829 *      We are freeing all blocks refered from these branches (numbers are
 830 *      stored as little-endian 32-bit) and updating @inode->i_blocks
 831 *      appropriately.
 832 */
 833static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
 834{
 835        struct buffer_head * bh;
 836        unsigned long nr;
 837
 838        if (depth--) {
 839                int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 840                for ( ; p < q ; p++) {
 841                        nr = le32_to_cpu(*p);
 842                        if (!nr)
 843                                continue;
 844                        *p = 0;
 845                        bh = sb_bread(inode->i_sb, nr);
 846                        /*
 847                         * A read failure? Report error and clear slot
 848                         * (should be rare).
 849                         */ 
 850                        if (!bh) {
 851                                ext2_error(inode->i_sb, "ext2_free_branches",
 852                                        "Read failure, inode=%ld, block=%ld",
 853                                        inode->i_ino, nr);
 854                                continue;
 855                        }
 856                        ext2_free_branches(inode,
 857                                           (__le32*)bh->b_data,
 858                                           (__le32*)bh->b_data + addr_per_block,
 859                                           depth);
 860                        bforget(bh);
 861                        ext2_free_blocks(inode, nr, 1);
 862                        mark_inode_dirty(inode);
 863                }
 864        } else
 865                ext2_free_data(inode, p, q);
 866}
 867
 868void ext2_truncate (struct inode * inode)
 869{
 870        __le32 *i_data = EXT2_I(inode)->i_data;
 871        int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 872        int offsets[4];
 873        Indirect chain[4];
 874        Indirect *partial;
 875        __le32 nr = 0;
 876        int n;
 877        long iblock;
 878        unsigned blocksize;
 879
 880        if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
 881            S_ISLNK(inode->i_mode)))
 882                return;
 883        if (ext2_inode_is_fast_symlink(inode))
 884                return;
 885        if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
 886                return;
 887
 888        ext2_discard_prealloc(inode);
 889
 890        blocksize = inode->i_sb->s_blocksize;
 891        iblock = (inode->i_size + blocksize-1)
 892                                        >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
 893
 894        if (test_opt(inode->i_sb, NOBH))
 895                nobh_truncate_page(inode->i_mapping, inode->i_size);
 896        else
 897                block_truncate_page(inode->i_mapping,
 898                                inode->i_size, ext2_get_block);
 899
 900        n = ext2_block_to_path(inode, iblock, offsets, NULL);
 901        if (n == 0)
 902                return;
 903
 904        if (n == 1) {
 905                ext2_free_data(inode, i_data+offsets[0],
 906                                        i_data + EXT2_NDIR_BLOCKS);
 907                goto do_indirects;
 908        }
 909
 910        partial = ext2_find_shared(inode, n, offsets, chain, &nr);
 911        /* Kill the top of shared branch (already detached) */
 912        if (nr) {
 913                if (partial == chain)
 914                        mark_inode_dirty(inode);
 915                else
 916                        mark_buffer_dirty_inode(partial->bh, inode);
 917                ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
 918        }
 919        /* Clear the ends of indirect blocks on the shared branch */
 920        while (partial > chain) {
 921                ext2_free_branches(inode,
 922                                   partial->p + 1,
 923                                   (__le32*)partial->bh->b_data+addr_per_block,
 924                                   (chain+n-1) - partial);
 925                mark_buffer_dirty_inode(partial->bh, inode);
 926                brelse (partial->bh);
 927                partial--;
 928        }
 929do_indirects:
 930        /* Kill the remaining (whole) subtrees */
 931        switch (offsets[0]) {
 932                default:
 933                        nr = i_data[EXT2_IND_BLOCK];
 934                        if (nr) {
 935                                i_data[EXT2_IND_BLOCK] = 0;
 936                                mark_inode_dirty(inode);
 937                                ext2_free_branches(inode, &nr, &nr+1, 1);
 938                        }
 939                case EXT2_IND_BLOCK:
 940                        nr = i_data[EXT2_DIND_BLOCK];
 941                        if (nr) {
 942                                i_data[EXT2_DIND_BLOCK] = 0;
 943                                mark_inode_dirty(inode);
 944                                ext2_free_branches(inode, &nr, &nr+1, 2);
 945                        }
 946                case EXT2_DIND_BLOCK:
 947                        nr = i_data[EXT2_TIND_BLOCK];
 948                        if (nr) {
 949                                i_data[EXT2_TIND_BLOCK] = 0;
 950                                mark_inode_dirty(inode);
 951                                ext2_free_branches(inode, &nr, &nr+1, 3);
 952                        }
 953                case EXT2_TIND_BLOCK:
 954                        ;
 955        }
 956        inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
 957        if (inode_needs_sync(inode)) {
 958                sync_mapping_buffers(inode->i_mapping);
 959                ext2_sync_inode (inode);
 960        } else {
 961                mark_inode_dirty(inode);
 962        }
 963}
 964
 965static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
 966                                        struct buffer_head **p)
 967{
 968        struct buffer_head * bh;
 969        unsigned long block_group;
 970        unsigned long block;
 971        unsigned long offset;
 972        struct ext2_group_desc * gdp;
 973
 974        *p = NULL;
 975        if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
 976            ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
 977                goto Einval;
 978
 979        block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
 980        gdp = ext2_get_group_desc(sb, block_group, &bh);
 981        if (!gdp)
 982                goto Egdp;
 983        /*
 984         * Figure out the offset within the block group inode table
 985         */
 986        offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
 987        block = le32_to_cpu(gdp->bg_inode_table) +
 988                (offset >> EXT2_BLOCK_SIZE_BITS(sb));
 989        if (!(bh = sb_bread(sb, block)))
 990                goto Eio;
 991
 992        *p = bh;
 993        offset &= (EXT2_BLOCK_SIZE(sb) - 1);
 994        return (struct ext2_inode *) (bh->b_data + offset);
 995
 996Einval:
 997        ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
 998                   (unsigned long) ino);
 999        return ERR_PTR(-EINVAL);
1000Eio:
1001        ext2_error(sb, "ext2_get_inode",
1002                   "unable to read inode block - inode=%lu, block=%lu",
1003                   (unsigned long) ino, block);
1004Egdp:
1005        return ERR_PTR(-EIO);
1006}
1007
1008void ext2_set_inode_flags(struct inode *inode)
1009{
1010        unsigned int flags = EXT2_I(inode)->i_flags;
1011
1012        inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1013        if (flags & EXT2_SYNC_FL)
1014                inode->i_flags |= S_SYNC;
1015        if (flags & EXT2_APPEND_FL)
1016                inode->i_flags |= S_APPEND;
1017        if (flags & EXT2_IMMUTABLE_FL)
1018                inode->i_flags |= S_IMMUTABLE;
1019        if (flags & EXT2_NOATIME_FL)
1020                inode->i_flags |= S_NOATIME;
1021        if (flags & EXT2_DIRSYNC_FL)
1022                inode->i_flags |= S_DIRSYNC;
1023}
1024
1025void ext2_read_inode (struct inode * inode)
1026{
1027        struct ext2_inode_info *ei = EXT2_I(inode);
1028        ino_t ino = inode->i_ino;
1029        struct buffer_head * bh;
1030        struct ext2_inode * raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
1031        int n;
1032
1033#ifdef CONFIG_EXT2_FS_POSIX_ACL
1034        ei->i_acl = EXT2_ACL_NOT_CACHED;
1035        ei->i_default_acl = EXT2_ACL_NOT_CACHED;
1036#endif
1037        if (IS_ERR(raw_inode))
1038                goto bad_inode;
1039
1040        inode->i_mode = le16_to_cpu(raw_inode->i_mode);
1041        inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
1042        inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
1043        if (!(test_opt (inode->i_sb, NO_UID32))) {
1044                inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
1045                inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
1046        }
1047        inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
1048        inode->i_size = le32_to_cpu(raw_inode->i_size);
1049        inode->i_atime.tv_sec = le32_to_cpu(raw_inode->i_atime);
1050        inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->i_ctime);
1051        inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->i_mtime);
1052        inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1053        ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
1054        /* We now have enough fields to check if the inode was active or not.
1055         * This is needed because nfsd might try to access dead inodes
1056         * the test is that same one that e2fsck uses
1057         * NeilBrown 1999oct15
1058         */
1059        if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
1060                /* this inode is deleted */
1061                brelse (bh);
1062                goto bad_inode;
1063        }
1064        inode->i_blksize = PAGE_SIZE;   /* This is the optimal IO size (for stat), not the fs block size */
1065        inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
1066        ei->i_flags = le32_to_cpu(raw_inode->i_flags);
1067        ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
1068        ei->i_frag_no = raw_inode->i_frag;
1069        ei->i_frag_size = raw_inode->i_fsize;
1070        ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
1071        ei->i_dir_acl = 0;
1072        if (S_ISREG(inode->i_mode))
1073                inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
1074        else
1075                ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
1076        ei->i_dtime = 0;
1077        inode->i_generation = le32_to_cpu(raw_inode->i_generation);
1078        ei->i_state = 0;
1079        ei->i_next_alloc_block = 0;
1080        ei->i_next_alloc_goal = 0;
1081        ei->i_prealloc_count = 0;
1082        ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
1083        ei->i_dir_start_lookup = 0;
1084
1085        /*
1086         * NOTE! The in-memory inode i_data array is in little-endian order
1087         * even on big-endian machines: we do NOT byteswap the block numbers!
1088         */
1089        for (n = 0; n < EXT2_N_BLOCKS; n++)
1090                ei->i_data[n] = raw_inode->i_block[n];
1091
1092        if (S_ISREG(inode->i_mode)) {
1093                inode->i_op = &ext2_file_inode_operations;
1094                inode->i_fop = &ext2_file_operations;
1095                if (test_opt(inode->i_sb, NOBH))
1096                        inode->i_mapping->a_ops = &ext2_nobh_aops;
1097                else
1098                        inode->i_mapping->a_ops = &ext2_aops;
1099        } else if (S_ISDIR(inode->i_mode)) {
1100                inode->i_op = &ext2_dir_inode_operations;
1101                inode->i_fop = &ext2_dir_operations;
1102                if (test_opt(inode->i_sb, NOBH))
1103                        inode->i_mapping->a_ops = &ext2_nobh_aops;
1104                else
1105                        inode->i_mapping->a_ops = &ext2_aops;
1106        } else if (S_ISLNK(inode->i_mode)) {
1107                if (ext2_inode_is_fast_symlink(inode))
1108                        inode->i_op = &ext2_fast_symlink_inode_operations;
1109                else {
1110                        inode->i_op = &ext2_symlink_inode_operations;
1111                        if (test_opt(inode->i_sb, NOBH))
1112                                inode->i_mapping->a_ops = &ext2_nobh_aops;
1113                        else
1114                                inode->i_mapping->a_ops = &ext2_aops;
1115                }
1116        } else {
1117                inode->i_op = &ext2_special_inode_operations;
1118                if (raw_inode->i_block[0])
1119                        init_special_inode(inode, inode->i_mode,
1120                           old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
1121                else 
1122                        init_special_inode(inode, inode->i_mode,
1123                           new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
1124        }
1125        brelse (bh);
1126        ext2_set_inode_flags(inode);
1127        return;
1128        
1129bad_inode:
1130        make_bad_inode(inode);
1131        return;
1132}
1133
1134static int ext2_update_inode(struct inode * inode, int do_sync)
1135{
1136        struct ext2_inode_info *ei = EXT2_I(inode);
1137        struct super_block *sb = inode->i_sb;
1138        ino_t ino = inode->i_ino;
1139        uid_t uid = inode->i_uid;
1140        gid_t gid = inode->i_gid;
1141        struct buffer_head * bh;
1142        struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
1143        int n;
1144        int err = 0;
1145
1146        if (IS_ERR(raw_inode))
1147                return -EIO;
1148
1149        /* For fields not not tracking in the in-memory inode,
1150         * initialise them to zero for new inodes. */
1151        if (ei->i_state & EXT2_STATE_NEW)
1152                memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
1153
1154        raw_inode->i_mode = cpu_to_le16(inode->i_mode);
1155        if (!(test_opt(sb, NO_UID32))) {
1156                raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
1157                raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
1158/*
1159 * Fix up interoperability with old kernels. Otherwise, old inodes get
1160 * re-used with the upper 16 bits of the uid/gid intact
1161 */
1162                if (!ei->i_dtime) {
1163                        raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
1164                        raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
1165                } else {
1166                        raw_inode->i_uid_high = 0;
1167                        raw_inode->i_gid_high = 0;
1168                }
1169        } else {
1170                raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
1171                raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
1172                raw_inode->i_uid_high = 0;
1173                raw_inode->i_gid_high = 0;
1174        }
1175        raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
1176        raw_inode->i_size = cpu_to_le32(inode->i_size);
1177        raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
1178        raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
1179        raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
1180
1181        raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
1182        raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
1183        raw_inode->i_flags = cpu_to_le32(ei->i_flags);
1184        raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
1185        raw_inode->i_frag = ei->i_frag_no;
1186        raw_inode->i_fsize = ei->i_frag_size;
1187        raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
1188        if (!S_ISREG(inode->i_mode))
1189                raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
1190        else {
1191                raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
1192                if (inode->i_size > 0x7fffffffULL) {
1193                        if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
1194                                        EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
1195                            EXT2_SB(sb)->s_es->s_rev_level ==
1196                                        cpu_to_le32(EXT2_GOOD_OLD_REV)) {
1197                               /* If this is the first large file
1198                                * created, add a flag to the superblock.
1199                                */
1200                                lock_kernel();
1201                                ext2_update_dynamic_rev(sb);
1202                                EXT2_SET_RO_COMPAT_FEATURE(sb,
1203                                        EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
1204                                unlock_kernel();
1205                                ext2_write_super(sb);
1206                        }
1207                }
1208        }
1209        
1210        raw_inode->i_generation = cpu_to_le32(inode->i_generation);
1211        if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1212                if (old_valid_dev(inode->i_rdev)) {
1213                        raw_inode->i_block[0] =
1214                                cpu_to_le32(old_encode_dev(inode->i_rdev));
1215                        raw_inode->i_block[1] = 0;
1216                } else {
1217                        raw_inode->i_block[0] = 0;
1218                        raw_inode->i_block[1] =
1219                                cpu_to_le32(new_encode_dev(inode->i_rdev));
1220                        raw_inode->i_block[2] = 0;
1221                }
1222        } else for (n = 0; n < EXT2_N_BLOCKS; n++)
1223                raw_inode->i_block[n] = ei->i_data[n];
1224        mark_buffer_dirty(bh);
1225        if (do_sync) {
1226                sync_dirty_buffer(bh);
1227                if (buffer_req(bh) && !buffer_uptodate(bh)) {
1228                        printk ("IO error syncing ext2 inode [%s:%08lx]\n",
1229                                sb->s_id, (unsigned long) ino);
1230                        err = -EIO;
1231                }
1232        }
1233        ei->i_state &= ~EXT2_STATE_NEW;
1234        brelse (bh);
1235        return err;
1236}
1237
1238int ext2_write_inode(struct inode *inode, int wait)
1239{
1240        return ext2_update_inode(inode, wait);
1241}
1242
1243int ext2_sync_inode(struct inode *inode)
1244{
1245        struct writeback_control wbc = {
1246                .sync_mode = WB_SYNC_ALL,
1247                .nr_to_write = 0,       /* sys_fsync did this */
1248        };
1249        return sync_inode(inode, &wbc);
1250}
1251
1252int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
1253{
1254        struct inode *inode = dentry->d_inode;
1255        int error;
1256
1257        error = inode_change_ok(inode, iattr);
1258        if (error)
1259                return error;
1260        if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
1261            (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
1262                error = DQUOT_TRANSFER(inode, iattr) ? -EDQUOT : 0;
1263                if (error)
1264                        return error;
1265        }
1266        error = inode_setattr(inode, iattr);
1267        if (!error && (iattr->ia_valid & ATTR_MODE))
1268                error = ext2_acl_chmod(inode);
1269        return error;
1270}
1271
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.