root/fs/ext2/inode.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ext2_put_inode
  2. block_bmap
  3. ext2_discard_prealloc
  4. ext2_alloc_block
  5. ext2_bmap
  6. inode_getblk
  7. block_getblk
  8. block_getcluster
  9. ext2_getblk
  10. ext2_getcluster
  11. ext2_bread
  12. ext2_read_inode
  13. ext2_update_inode
  14. ext2_write_inode
  15. ext2_sync_inode

   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 (sct@dcs.ed.ac.uk), 1993
  16  */
  17 
  18 #include <asm/segment.h>
  19 #include <asm/system.h>
  20 
  21 #include <linux/errno.h>
  22 #include <linux/fs.h>
  23 #include <linux/ext2_fs.h>
  24 #include <linux/sched.h>
  25 #include <linux/stat.h>
  26 #include <linux/string.h>
  27 #include <linux/locks.h>
  28 #include <linux/mm.h>
  29 
  30 void ext2_put_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  31 {
  32         ext2_discard_prealloc (inode);
  33         if (inode->i_nlink || inode->i_ino == EXT2_ACL_IDX_INO ||
  34             inode->i_ino == EXT2_ACL_DATA_INO)
  35                 return;
  36         inode->i_size = 0;
  37         if (inode->i_blocks)
  38                 ext2_truncate (inode);
  39         ext2_free_inode (inode);
  40 }
  41 
  42 #define inode_bmap(inode, nr) ((inode)->u.ext2_i.i_data[(nr)])
  43 
  44 static int block_bmap (struct buffer_head * bh, int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
  45 {
  46         int tmp;
  47 
  48         if (!bh)
  49                 return 0;
  50         tmp = ((u32 *) bh->b_data)[nr];
  51         brelse (bh);
  52         return tmp;
  53 }
  54 
  55 /* 
  56  * ext2_discard_prealloc and ext2_alloc_block are atomic wrt. the
  57  * superblock in the same manner as are ext2_free_blocks and
  58  * ext2_new_block.  We just wait on the super rather than locking it
  59  * here, since ext2_new_block will do the necessary locking and we
  60  * can't block until then.
  61  */
  62 void ext2_discard_prealloc (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  63 {
  64 #ifdef EXT2_PREALLOCATE
  65         if (inode->u.ext2_i.i_prealloc_count) {
  66                 int i = inode->u.ext2_i.i_prealloc_count;
  67                 inode->u.ext2_i.i_prealloc_count = 0;
  68                 ext2_free_blocks (inode->i_sb,
  69                                   inode->u.ext2_i.i_prealloc_block,
  70                                   i);
  71         }
  72 #endif
  73 }
  74 
  75 static int ext2_alloc_block (struct inode * inode, unsigned long goal)
     /* [previous][next][first][last][top][bottom][index][help] */
  76 {
  77 #ifdef EXT2FS_DEBUG
  78         static unsigned long alloc_hits = 0, alloc_attempts = 0;
  79 #endif
  80         unsigned long result;
  81         struct buffer_head * bh;
  82 
  83         wait_on_super (inode->i_sb);
  84 
  85 #ifdef EXT2_PREALLOCATE
  86         if (inode->u.ext2_i.i_prealloc_count &&
  87             (goal == inode->u.ext2_i.i_prealloc_block ||
  88              goal + 1 == inode->u.ext2_i.i_prealloc_block))
  89         {               
  90                 result = inode->u.ext2_i.i_prealloc_block++;
  91                 inode->u.ext2_i.i_prealloc_count--;
  92                 ext2_debug ("preallocation hit (%lu/%lu).\n",
  93                             ++alloc_hits, ++alloc_attempts);
  94 
  95                 /* It doesn't matter if we block in getblk() since
  96                    we have already atomically allocated the block, and
  97                    are only clearing it now. */
  98                 if (!(bh = getblk (inode->i_sb->s_dev, result,
  99                                    inode->i_sb->s_blocksize))) {
 100                         ext2_error (inode->i_sb, "ext2_alloc_block",
 101                                     "cannot get block %lu", result);
 102                         return 0;
 103                 }
 104                 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
 105                 bh->b_uptodate = 1;
 106                 mark_buffer_dirty(bh, 1);
 107                 brelse (bh);
 108         } else {
 109                 ext2_discard_prealloc (inode);
 110                 ext2_debug ("preallocation miss (%lu/%lu).\n",
 111                             alloc_hits, ++alloc_attempts);
 112                 if (S_ISREG(inode->i_mode))
 113                         result = ext2_new_block
 114                                 (inode->i_sb, goal,
 115                                  &inode->u.ext2_i.i_prealloc_count,
 116                                  &inode->u.ext2_i.i_prealloc_block);
 117                 else
 118                         result = ext2_new_block (inode->i_sb, goal, 0, 0);
 119         }
 120 #else
 121         result = ext2_new_block (inode->i_sb, goal, 0, 0);
 122 #endif
 123 
 124         return result;
 125 }
 126 
 127 
 128 int ext2_bmap (struct inode * inode, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 129 {
 130         int i;
 131         int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 132         int addr_per_block_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
 133 
 134         if (block < 0) {
 135                 ext2_warning (inode->i_sb, "ext2_bmap", "block < 0");
 136                 return 0;
 137         }
 138         if (block >= EXT2_NDIR_BLOCKS + addr_per_block +
 139                 (1 << (addr_per_block_bits * 2)) +
 140                 ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
 141                 ext2_warning (inode->i_sb, "ext2_bmap", "block > big");
 142                 return 0;
 143         }
 144         if (block < EXT2_NDIR_BLOCKS)
 145                 return inode_bmap (inode, block);
 146         block -= EXT2_NDIR_BLOCKS;
 147         if (block < addr_per_block) {
 148                 i = inode_bmap (inode, EXT2_IND_BLOCK);
 149                 if (!i)
 150                         return 0;
 151                 return block_bmap (bread (inode->i_dev, i,
 152                                           inode->i_sb->s_blocksize), block);
 153         }
 154         block -= addr_per_block;
 155         if (block < (1 << (addr_per_block_bits * 2))) {
 156                 i = inode_bmap (inode, EXT2_DIND_BLOCK);
 157                 if (!i)
 158                         return 0;
 159                 i = block_bmap (bread (inode->i_dev, i,
 160                                        inode->i_sb->s_blocksize),
 161                                 block >> addr_per_block_bits);
 162                 if (!i)
 163                         return 0;
 164                 return block_bmap (bread (inode->i_dev, i,
 165                                           inode->i_sb->s_blocksize),
 166                                    block & (addr_per_block - 1));
 167         }
 168         block -= (1 << (addr_per_block_bits * 2));
 169         i = inode_bmap (inode, EXT2_TIND_BLOCK);
 170         if (!i)
 171                 return 0;
 172         i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 173                         block >> (addr_per_block_bits * 2));
 174         if (!i)
 175                 return 0;
 176         i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 177                         (block >> addr_per_block_bits) & (addr_per_block - 1));
 178         if (!i)
 179                 return 0;
 180         return block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 181                            block & (addr_per_block - 1));
 182 }
 183 
 184 static struct buffer_head * inode_getblk (struct inode * inode, int nr,
     /* [previous][next][first][last][top][bottom][index][help] */
 185                                           int create, int new_block, int * err)
 186 {
 187         u32 * p;
 188         int tmp, goal = 0;
 189         struct buffer_head * result;
 190         int blocks = inode->i_sb->s_blocksize / 512;
 191 
 192         p = inode->u.ext2_i.i_data + nr;
 193 repeat:
 194         tmp = *p;
 195         if (tmp) {
 196                 result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize);
 197                 if (tmp == *p)
 198                         return result;
 199                 brelse (result);
 200                 goto repeat;
 201         }
 202         if (!create || new_block >= 
 203             (current->rlim[RLIMIT_FSIZE].rlim_cur >>
 204              EXT2_BLOCK_SIZE_BITS(inode->i_sb))) {
 205                 *err = -EFBIG;
 206                 return NULL;
 207         }
 208         if (inode->u.ext2_i.i_next_alloc_block == new_block)
 209                 goal = inode->u.ext2_i.i_next_alloc_goal;
 210 
 211         ext2_debug ("hint = %d,", goal);
 212 
 213         if (!goal) {
 214                 for (tmp = nr - 1; tmp >= 0; tmp--) {
 215                         if (inode->u.ext2_i.i_data[tmp]) {
 216                                 goal = inode->u.ext2_i.i_data[tmp];
 217                                 break;
 218                         }
 219                 }
 220                 if (!goal)
 221                         goal = (inode->u.ext2_i.i_block_group * 
 222                                 EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
 223                                inode->i_sb->u.ext2_sb.s_es->s_first_data_block;
 224         }
 225 
 226         ext2_debug ("goal = %d.\n", goal);
 227 
 228         tmp = ext2_alloc_block (inode, goal);
 229         if (!tmp)
 230                 return NULL;
 231         result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize);
 232         if (*p) {
 233                 ext2_free_blocks (inode->i_sb, tmp, 1);
 234                 brelse (result);
 235                 goto repeat;
 236         }
 237         *p = tmp;
 238         inode->u.ext2_i.i_next_alloc_block = new_block;
 239         inode->u.ext2_i.i_next_alloc_goal = tmp;
 240         inode->i_ctime = CURRENT_TIME;
 241         inode->i_blocks += blocks;
 242         if (IS_SYNC(inode) || inode->u.ext2_i.i_osync)
 243                 ext2_sync_inode (inode);
 244         else
 245                 inode->i_dirt = 1;
 246         return result;
 247 }
 248 
 249 static struct buffer_head * block_getblk (struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
 250                                           struct buffer_head * bh, int nr,
 251                                           int create, int blocksize, 
 252                                           int new_block, int * err)
 253 {
 254         int tmp, goal = 0;
 255         u32 * p;
 256         struct buffer_head * result;
 257         int blocks = inode->i_sb->s_blocksize / 512;
 258 
 259         if (!bh)
 260                 return NULL;
 261         if (!bh->b_uptodate) {
 262                 ll_rw_block (READ, 1, &bh);
 263                 wait_on_buffer (bh);
 264                 if (!bh->b_uptodate) {
 265                         brelse (bh);
 266                         return NULL;
 267                 }
 268         }
 269         p = (u32 *) bh->b_data + nr;
 270 repeat:
 271         tmp = *p;
 272         if (tmp) {
 273                 result = getblk (bh->b_dev, tmp, blocksize);
 274                 if (tmp == *p) {
 275                         brelse (bh);
 276                         return result;
 277                 }
 278                 brelse (result);
 279                 goto repeat;
 280         }
 281         if (!create || new_block >= 
 282             (current->rlim[RLIMIT_FSIZE].rlim_cur >> 
 283              EXT2_BLOCK_SIZE_BITS(inode->i_sb))) {
 284                 brelse (bh);
 285                 *err = -EFBIG;
 286                 return NULL;
 287         }
 288         if (inode->u.ext2_i.i_next_alloc_block == new_block)
 289                 goal = inode->u.ext2_i.i_next_alloc_goal;
 290         if (!goal) {
 291                 for (tmp = nr - 1; tmp >= 0; tmp--) {
 292                         if (((u32 *) bh->b_data)[tmp]) {
 293                                 goal = ((u32 *)bh->b_data)[tmp];
 294                                 break;
 295                         }
 296                 }
 297                 if (!goal)
 298                         goal = bh->b_blocknr;
 299         }
 300         tmp = ext2_alloc_block (inode, goal);
 301         if (!tmp) {
 302                 brelse (bh);
 303                 return NULL;
 304         }
 305         result = getblk (bh->b_dev, tmp, blocksize);
 306         if (*p) {
 307                 ext2_free_blocks (inode->i_sb, tmp, 1);
 308                 brelse (result);
 309                 goto repeat;
 310         }
 311         *p = tmp;
 312         mark_buffer_dirty(bh, 1);
 313         if (IS_SYNC(inode) || inode->u.ext2_i.i_osync) {
 314                 ll_rw_block (WRITE, 1, &bh);
 315                 wait_on_buffer (bh);
 316         }
 317         inode->i_ctime = CURRENT_TIME;
 318         inode->i_blocks += blocks;
 319         inode->i_dirt = 1;
 320         inode->u.ext2_i.i_next_alloc_block = new_block;
 321         inode->u.ext2_i.i_next_alloc_goal = tmp;
 322         brelse (bh);
 323         return result;
 324 }
 325 
 326 static int block_getcluster (struct inode * inode, struct buffer_head * bh,
     /* [previous][next][first][last][top][bottom][index][help] */
 327                                           int nr,
 328                                           int blocksize)
 329 {
 330         u32 * p;
 331         int firstblock = 0;
 332         int result = 0;
 333         int i;
 334 
 335         /* Check to see if clustering possible here. */
 336 
 337         if(!bh) return 0;
 338 
 339         if((nr & ((PAGE_SIZE >> EXT2_BLOCK_SIZE_BITS(inode->i_sb)) - 1)) != 0)
 340                 goto out;
 341         if(nr + 3 > EXT2_ADDR_PER_BLOCK(inode->i_sb)) goto out;
 342 
 343         for(i=0; i< (PAGE_SIZE >> EXT2_BLOCK_SIZE_BITS(inode->i_sb)); i++) {
 344           p = (u32 *) bh->b_data + nr + i;
 345           
 346           /* All blocks in cluster must already be allocated */
 347           if(*p == 0) goto out;
 348           
 349           /* See if aligned correctly */
 350           if(i==0) firstblock = *p;
 351           else if(*p != firstblock + i) goto out;
 352         }
 353         
 354         p = (u32 *) bh->b_data + nr;
 355         result = generate_cluster(bh->b_dev, (int *) p, blocksize);
 356 
 357       out:
 358         brelse(bh);
 359         return result;
 360 }
 361 
 362 struct buffer_head * ext2_getblk (struct inode * inode, long block,
     /* [previous][next][first][last][top][bottom][index][help] */
 363                                   int create, int * err)
 364 {
 365         struct buffer_head * bh;
 366         unsigned long b;
 367         unsigned long addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 368         int addr_per_block_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
 369 
 370         *err = -EIO;
 371         if (block < 0) {
 372                 ext2_warning (inode->i_sb, "ext2_getblk", "block < 0");
 373                 return NULL;
 374         }
 375         if (block > EXT2_NDIR_BLOCKS + addr_per_block +
 376                 (1 << (addr_per_block_bits * 2)) +
 377                 ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
 378                 ext2_warning (inode->i_sb, "ext2_getblk", "block > big");
 379                 return NULL;
 380         }
 381         /*
 382          * If this is a sequential block allocation, set the next_alloc_block
 383          * to this block now so that all the indblock and data block
 384          * allocations use the same goal zone
 385          */
 386 
 387         ext2_debug ("block %lu, next %lu, goal %lu.\n", block, 
 388                     inode->u.ext2_i.i_next_alloc_block,
 389                     inode->u.ext2_i.i_next_alloc_goal);
 390 
 391         if (block == inode->u.ext2_i.i_next_alloc_block + 1) {
 392                 inode->u.ext2_i.i_next_alloc_block++;
 393                 inode->u.ext2_i.i_next_alloc_goal++;
 394         }
 395 
 396         *err = -ENOSPC;
 397         b = block;
 398         if (block < EXT2_NDIR_BLOCKS)
 399                 return inode_getblk (inode, block, create, b, err);
 400         block -= EXT2_NDIR_BLOCKS;
 401         if (block < addr_per_block) {
 402                 bh = inode_getblk (inode, EXT2_IND_BLOCK, create, b, err);
 403                 return block_getblk (inode, bh, block, create,
 404                                      inode->i_sb->s_blocksize, b, err);
 405         }
 406         block -= addr_per_block;
 407         if (block < (1 << (addr_per_block_bits * 2))) {
 408                 bh = inode_getblk (inode, EXT2_DIND_BLOCK, create, b, err);
 409                 bh = block_getblk (inode, bh, block >> addr_per_block_bits,
 410                                    create, inode->i_sb->s_blocksize, b, err);
 411                 return block_getblk (inode, bh, block & (addr_per_block - 1),
 412                                      create, inode->i_sb->s_blocksize, b, err);
 413         }
 414         block -= (1 << (addr_per_block_bits * 2));
 415         bh = inode_getblk (inode, EXT2_TIND_BLOCK, create, b, err);
 416         bh = block_getblk (inode, bh, block >> (addr_per_block_bits * 2),
 417                            create, inode->i_sb->s_blocksize, b, err);
 418         bh = block_getblk (inode, bh, (block >> addr_per_block_bits) & (addr_per_block - 1),
 419                            create, inode->i_sb->s_blocksize, b, err);
 420         return block_getblk (inode, bh, block & (addr_per_block - 1), create,
 421                              inode->i_sb->s_blocksize, b, err);
 422 }
 423 
 424 int ext2_getcluster (struct inode * inode, long block)
     /* [previous][next][first][last][top][bottom][index][help] */
 425 {
 426         struct buffer_head * bh;
 427         int err, create;
 428         unsigned long b;
 429         unsigned long addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 430         int addr_per_block_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
 431 
 432         create = 0;
 433         err = -EIO;
 434         if (block < 0) {
 435                 ext2_warning (inode->i_sb, "ext2_getblk", "block < 0");
 436                 return 0;
 437         }
 438         if (block > EXT2_NDIR_BLOCKS + addr_per_block +
 439                 (1 << (addr_per_block_bits * 2)) +
 440                 ((1 << (addr_per_block_bits * 2)) << addr_per_block_bits)) {
 441                 ext2_warning (inode->i_sb, "ext2_getblk", "block > big");
 442                 return 0;
 443         }
 444 
 445         err = -ENOSPC;
 446         b = block;
 447         if (block < EXT2_NDIR_BLOCKS) return 0;
 448 
 449         block -= EXT2_NDIR_BLOCKS;
 450 
 451         if (block < addr_per_block) {
 452                 bh = inode_getblk (inode, EXT2_IND_BLOCK, create, b, &err);
 453                 return block_getcluster (inode, bh, block, 
 454                                          inode->i_sb->s_blocksize);
 455         }
 456         block -= addr_per_block;
 457         if (block < (1 << (addr_per_block_bits * 2))) {
 458                 bh = inode_getblk (inode, EXT2_DIND_BLOCK, create, b, &err);
 459                 bh = block_getblk (inode, bh, block >> addr_per_block_bits,
 460                                    create, inode->i_sb->s_blocksize, b, &err);
 461                 return block_getcluster (inode, bh, block & (addr_per_block - 1),
 462                                      inode->i_sb->s_blocksize);
 463         }
 464         block -= (1 << (addr_per_block_bits * 2));
 465         bh = inode_getblk (inode, EXT2_TIND_BLOCK, create, b, &err);
 466         bh = block_getblk (inode, bh, block >> (addr_per_block_bits * 2),
 467                            create, inode->i_sb->s_blocksize, b, &err);
 468         bh = block_getblk (inode, bh, (block >> addr_per_block_bits) & (addr_per_block - 1),
 469                            create, inode->i_sb->s_blocksize, b, &err);
 470         return block_getcluster (inode, bh, block & (addr_per_block - 1),
 471                                  inode->i_sb->s_blocksize);
 472 }
 473 
 474 struct buffer_head * ext2_bread (struct inode * inode, int block, 
     /* [previous][next][first][last][top][bottom][index][help] */
 475                                  int create, int *err)
 476 {
 477         struct buffer_head * bh;
 478 
 479         bh = ext2_getblk (inode, block, create, err);
 480         if (!bh || bh->b_uptodate)
 481                 return bh;
 482         ll_rw_block (READ, 1, &bh);
 483         wait_on_buffer (bh);
 484         if (bh->b_uptodate)
 485                 return bh;
 486         brelse (bh);
 487         *err = -EIO;
 488         return NULL;
 489 }
 490 
 491 void ext2_read_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 492 {
 493         struct buffer_head * bh;
 494         struct ext2_inode * raw_inode;
 495         unsigned long block_group;
 496         unsigned long group_desc;
 497         unsigned long desc;
 498         unsigned long block;
 499         struct ext2_group_desc * gdp;
 500 
 501         if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino != EXT2_ACL_IDX_INO &&
 502              inode->i_ino != EXT2_ACL_DATA_INO && inode->i_ino < EXT2_FIRST_INO) ||
 503             inode->i_ino > inode->i_sb->u.ext2_sb.s_es->s_inodes_count) {
 504                 ext2_error (inode->i_sb, "ext2_read_inode",
 505                             "bad inode number: %lu", inode->i_ino);
 506                 return;
 507         }
 508         block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
 509         if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count)
 510                 ext2_panic (inode->i_sb, "ext2_read_inode",
 511                             "group >= groups count");
 512         group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
 513         desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
 514         bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
 515         if (!bh)
 516                 ext2_panic (inode->i_sb, "ext2_read_inode",
 517                             "Descriptor not loaded");
 518         gdp = (struct ext2_group_desc *) bh->b_data;
 519         block = gdp[desc].bg_inode_table +
 520                 (((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb))
 521                  >> EXT2_INODES_PER_BLOCK_BITS(inode->i_sb));
 522         if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize)))
 523                 ext2_panic (inode->i_sb, "ext2_read_inode",
 524                             "unable to read i-node block - "
 525                             "inode=%lu, block=%lu", inode->i_ino, block);
 526         raw_inode = ((struct ext2_inode *) bh->b_data) +
 527                 ((inode->i_ino - 1) & (EXT2_INODES_PER_BLOCK(inode->i_sb) - 1));
 528         inode->i_mode = raw_inode->i_mode;
 529         inode->i_uid = raw_inode->i_uid;
 530         inode->i_gid = raw_inode->i_gid;
 531         inode->i_nlink = raw_inode->i_links_count;
 532         inode->i_size = raw_inode->i_size;
 533         inode->i_atime = raw_inode->i_atime;
 534         inode->i_ctime = raw_inode->i_ctime;
 535         inode->i_mtime = raw_inode->i_mtime;
 536         inode->u.ext2_i.i_dtime = raw_inode->i_dtime;
 537         inode->i_blksize = inode->i_sb->s_blocksize;
 538         inode->i_blocks = raw_inode->i_blocks;
 539         inode->i_version = ++event;
 540         inode->u.ext2_i.i_flags = raw_inode->i_flags;
 541         inode->u.ext2_i.i_faddr = raw_inode->i_faddr;
 542         inode->u.ext2_i.i_frag_no = raw_inode->i_frag;
 543         inode->u.ext2_i.i_frag_size = raw_inode->i_fsize;
 544         inode->u.ext2_i.i_osync = 0;
 545         inode->u.ext2_i.i_file_acl = raw_inode->i_file_acl;
 546         inode->u.ext2_i.i_dir_acl = raw_inode->i_dir_acl;
 547         inode->u.ext2_i.i_version = raw_inode->i_version;
 548         inode->u.ext2_i.i_block_group = block_group;
 549         inode->u.ext2_i.i_next_alloc_block = 0;
 550         inode->u.ext2_i.i_next_alloc_goal = 0;
 551         if (inode->u.ext2_i.i_prealloc_count)
 552                 ext2_error (inode->i_sb, "ext2_read_inode",
 553                             "New inode has non-zero prealloc count!");
 554         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 555                 inode->i_rdev = raw_inode->i_block[0];
 556         else for (block = 0; block < EXT2_N_BLOCKS; block++)
 557                 inode->u.ext2_i.i_data[block] = raw_inode->i_block[block];
 558         brelse (bh);
 559         inode->i_op = NULL;
 560         if (inode->i_ino == EXT2_ACL_IDX_INO ||
 561             inode->i_ino == EXT2_ACL_DATA_INO)
 562                 /* Nothing to do */ ;
 563         else if (S_ISREG(inode->i_mode))
 564                 inode->i_op = &ext2_file_inode_operations;
 565         else if (S_ISDIR(inode->i_mode))
 566                 inode->i_op = &ext2_dir_inode_operations;
 567         else if (S_ISLNK(inode->i_mode))
 568                 inode->i_op = &ext2_symlink_inode_operations;
 569         else if (S_ISCHR(inode->i_mode))
 570                 inode->i_op = &chrdev_inode_operations;
 571         else if (S_ISBLK(inode->i_mode))
 572                 inode->i_op = &blkdev_inode_operations;
 573         else if (S_ISFIFO(inode->i_mode))
 574                 init_fifo(inode);
 575         if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL)
 576                 inode->i_flags |= MS_SYNCHRONOUS;
 577         if (inode->u.ext2_i.i_flags & EXT2_APPEND_FL)
 578                 inode->i_flags |= S_APPEND;
 579         if (inode->u.ext2_i.i_flags & EXT2_IMMUTABLE_FL)
 580                 inode->i_flags |= S_IMMUTABLE;
 581 }
 582 
 583 static struct buffer_head * ext2_update_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 584 {
 585         struct buffer_head * bh;
 586         struct ext2_inode * raw_inode;
 587         unsigned long block_group;
 588         unsigned long group_desc;
 589         unsigned long desc;
 590         unsigned long block;
 591         struct ext2_group_desc * gdp;
 592 
 593         if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino < EXT2_FIRST_INO) ||
 594             inode->i_ino > inode->i_sb->u.ext2_sb.s_es->s_inodes_count) {
 595                 ext2_error (inode->i_sb, "ext2_write_inode",
 596                             "bad inode number: %lu", inode->i_ino);
 597                 return 0;
 598         }
 599         block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
 600         if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count)
 601                 ext2_panic (inode->i_sb, "ext2_write_inode",
 602                             "group >= groups count");
 603         group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
 604         desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
 605         bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
 606         if (!bh)
 607                 ext2_panic (inode->i_sb, "ext2_write_inode",
 608                             "Descriptor not loaded");
 609         gdp = (struct ext2_group_desc *) bh->b_data;
 610         block = gdp[desc].bg_inode_table +
 611                 (((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb))
 612                  >> EXT2_INODES_PER_BLOCK_BITS(inode->i_sb));
 613         if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize)))
 614                 ext2_panic (inode->i_sb, "ext2_write_inode",
 615                             "unable to read i-node block - "
 616                             "inode=%lu, block=%lu", inode->i_ino, block);
 617         raw_inode = ((struct ext2_inode *)bh->b_data) +
 618                 (inode->i_ino - 1) % EXT2_INODES_PER_BLOCK(inode->i_sb);
 619         raw_inode->i_mode = inode->i_mode;
 620         raw_inode->i_uid = inode->i_uid;
 621         raw_inode->i_gid = inode->i_gid;
 622         raw_inode->i_links_count = inode->i_nlink;
 623         raw_inode->i_size = inode->i_size;
 624         raw_inode->i_atime = inode->i_atime;
 625         raw_inode->i_ctime = inode->i_ctime;
 626         raw_inode->i_mtime = inode->i_mtime;
 627         raw_inode->i_blocks = inode->i_blocks;
 628         raw_inode->i_dtime = inode->u.ext2_i.i_dtime;
 629         raw_inode->i_flags = inode->u.ext2_i.i_flags;
 630         raw_inode->i_faddr = inode->u.ext2_i.i_faddr;
 631         raw_inode->i_frag = inode->u.ext2_i.i_frag_no;
 632         raw_inode->i_fsize = inode->u.ext2_i.i_frag_size;
 633         raw_inode->i_file_acl = inode->u.ext2_i.i_file_acl;
 634         raw_inode->i_dir_acl = inode->u.ext2_i.i_dir_acl;
 635         raw_inode->i_version = inode->u.ext2_i.i_version;
 636         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 637                 raw_inode->i_block[0] = inode->i_rdev;
 638         else for (block = 0; block < EXT2_N_BLOCKS; block++)
 639                 raw_inode->i_block[block] = inode->u.ext2_i.i_data[block];
 640         mark_buffer_dirty(bh, 1);
 641         inode->i_dirt = 0;
 642         return bh;
 643 }
 644 
 645 void ext2_write_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 646 {
 647         struct buffer_head * bh;
 648         bh = ext2_update_inode (inode);
 649         brelse (bh);
 650 }
 651 
 652 int ext2_sync_inode (struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 653 {
 654         int err = 0;
 655         struct buffer_head *bh;
 656 
 657         bh = ext2_update_inode (inode);
 658         if (bh && bh->b_dirt)
 659         {
 660                 ll_rw_block (WRITE, 1, &bh);
 661                 wait_on_buffer (bh);
 662                 if (bh->b_req && !bh->b_uptodate)
 663                 {
 664                         printk ("IO error syncing ext2 inode [%04x:%08lx]\n",
 665                                 inode->i_dev, inode->i_ino);
 666                         err = -1;
 667                 }
 668         }
 669         else if (!bh)
 670                 err = -1;
 671         brelse (bh);
 672         return err;
 673 }

/* [previous][next][first][last][top][bottom][index][help] */