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. ext2_put_super
  3. convert_pre_02b_fs
  4. ext2_read_super
  5. ext2_commit_super
  6. ext2_write_super
  7. ext2_remount
  8. ext2_statfs
  9. block_bmap
  10. ext2_bmap
  11. inode_getblk
  12. block_getblk
  13. ext2_getblk
  14. ext2_bread
  15. ext2_read_inode
  16. ext2_update_inode
  17. ext2_write_inode
  18. ext2_sync_inode

   1 /*
   2  *  linux/fs/ext2/inode.c
   3  *
   4  *  Copyright (C) 1992, 1993  Remy Card (card@masi.ibp.fr)
   5  *
   6  *  from
   7  *
   8  *  linux/fs/minix/inode.c
   9  *
  10  *  Copyright (C) 1991, 1992  Linus Torvalds
  11  *
  12  *  Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
  13  */
  14 
  15 #include <linux/sched.h>
  16 #include <linux/ext2_fs.h>
  17 #include <linux/kernel.h>
  18 #include <linux/mm.h>
  19 #include <linux/string.h>
  20 #include <linux/stat.h>
  21 #include <linux/locks.h>
  22 #include <linux/errno.h>
  23 
  24 #include <asm/system.h>
  25 #include <asm/segment.h>
  26 
  27 void ext2_put_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29         if (inode->i_nlink || inode->i_ino == EXT2_ACL_IDX_INO ||
  30             inode->i_ino == EXT2_ACL_DATA_INO)
  31                 return;
  32         inode->i_size = 0;
  33         if (inode->i_blocks)
  34                 ext2_truncate (inode);
  35         ext2_free_inode (inode);
  36 }
  37 
  38 void ext2_put_super (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40         int i;
  41 
  42         lock_super (sb);
  43         if ((sb->s_flags & MS_RDONLY) == 0) {
  44                 sb->u.ext2_sb.s_es->s_valid = sb->u.ext2_sb.s_was_mounted_valid;
  45                 sb->u.ext2_sb.s_sbh->b_dirt = 1;
  46         }
  47 #ifndef DONT_USE_DCACHE
  48         ext2_dcache_invalidate (sb->s_dev);
  49 #endif
  50         sb->s_dev = 0;
  51         for (i = 0; i < EXT2_MAX_GROUP_DESC; i++)
  52                 if (sb->u.ext2_sb.s_group_desc[i])
  53                         brelse (sb->u.ext2_sb.s_group_desc[i]);
  54         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
  55                 if (sb->u.ext2_sb.s_inode_bitmap[i])
  56                         brelse (sb->u.ext2_sb.s_inode_bitmap[i]);
  57         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++)
  58                 if (sb->u.ext2_sb.s_block_bitmap[i])
  59                         brelse (sb->u.ext2_sb.s_block_bitmap[i]);
  60         brelse (sb->u.ext2_sb.s_sbh);
  61         unlock_super (sb);
  62         return;
  63 }
  64 
  65 static struct super_operations ext2_sops = { 
  66         ext2_read_inode,
  67         NULL,
  68         ext2_write_inode,
  69         ext2_put_inode,
  70         ext2_put_super,
  71         ext2_write_super,
  72         ext2_statfs,
  73         ext2_remount
  74 };
  75 
  76 #ifdef EXT2FS_PRE_02B_COMPAT
  77 
  78 static int convert_pre_02b_fs (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
  79                                struct buffer_head * bh)
  80 {
  81         struct ext2_super_block * es;
  82         struct ext2_old_group_desc old_group_desc [BLOCK_SIZE / sizeof (struct ext2_old_group_desc)];
  83         struct ext2_group_desc * gdp;
  84         struct buffer_head * bh2;
  85         int groups_count;
  86         int i;
  87 
  88         es = (struct ext2_super_block *) bh->b_data;
  89         bh2 = bread (sb->s_dev, 2, BLOCK_SIZE);
  90         if (!bh2) {
  91                 printk ("Cannot read descriptor blocks while converting !\n");
  92                 return 0;
  93         }
  94         memcpy (old_group_desc, bh2->b_data, BLOCK_SIZE);
  95         groups_count = (sb->u.ext2_sb.s_blocks_count - 
  96                         sb->u.ext2_sb.s_first_data_block +
  97                         (EXT2_BLOCK_SIZE(sb) * 8) - 1) /
  98                                 (EXT2_BLOCK_SIZE(sb) * 8);
  99         memset (bh2->b_data, 0, BLOCK_SIZE);
 100         gdp = (struct ext2_group_desc *) bh2->b_data;
 101         for (i = 0; i < groups_count; i++) {
 102                 gdp[i].bg_block_bitmap = old_group_desc[i].bg_block_bitmap;
 103                 gdp[i].bg_inode_bitmap = old_group_desc[i].bg_inode_bitmap;
 104                 gdp[i].bg_inode_table = old_group_desc[i].bg_inode_table;
 105                 gdp[i].bg_free_blocks_count = old_group_desc[i].bg_free_blocks_count;
 106                 gdp[i].bg_free_inodes_count = old_group_desc[i].bg_free_inodes_count;
 107         }
 108         bh2->b_dirt = 1;
 109         brelse (bh2);
 110         es->s_magic = EXT2_SUPER_MAGIC;
 111         bh->b_dirt = 1;
 112         sb->s_magic = EXT2_SUPER_MAGIC;
 113         return 1;
 114 }
 115 
 116 #endif
 117 
 118 struct super_block * ext2_read_super (struct super_block * s, void * data,
     /* [previous][next][first][last][top][bottom][index][help] */
 119                                       int silent)
 120 {
 121         struct buffer_head * bh;
 122         struct ext2_super_block * es;
 123         int dev = s->s_dev;
 124         int bh_count;
 125         int i, j;
 126 #ifdef EXT2FS_PRE_02B_COMPAT
 127         int fs_converted = 0;
 128 #endif
 129 
 130         lock_super (s);
 131         set_blocksize (dev, BLOCK_SIZE);
 132         if (!(bh = bread (dev, 1, BLOCK_SIZE))) {
 133                 s->s_dev = 0;
 134                 unlock_super (s);
 135                 printk ("EXT2-fs: unable to read superblock\n");
 136                 return NULL;
 137         }
 138         es = (struct ext2_super_block *) bh->b_data;
 139         /* Note: s_es must be initialized s_es as soon as possible because
 140            some ext2 macro-instructions depend on its value */
 141         s->u.ext2_sb.s_es = es;
 142         s->s_magic = es->s_magic;
 143         if (s->s_magic != EXT2_SUPER_MAGIC
 144 #ifdef EXT2FS_PRE_02B_COMPAT
 145            && s->s_magic != EXT2_OLD_SUPER_MAGIC
 146 #endif
 147            ) {
 148                 s->s_dev = 0;
 149                 unlock_super (s);
 150                 brelse (bh);
 151                 if (!silent)
 152                         printk ("VFS: Can't find an ext2 filesystem on dev 0x%04x.\n",
 153                                 dev);
 154                 return NULL;
 155         }
 156         s->s_blocksize = EXT2_MIN_BLOCK_SIZE << es->s_log_block_size;
 157         s->s_blocksize_bits = EXT2_BLOCK_SIZE_BITS(s);
 158         if (s->s_blocksize != BLOCK_SIZE && 
 159             (s->s_blocksize == 1024 || s->s_blocksize == 2048 ||  
 160              s->s_blocksize == 4096)) {
 161                 brelse (bh);
 162                 set_blocksize (dev, s->s_blocksize);
 163                 bh = bread (dev, 0, s->s_blocksize);
 164                 if(!bh)
 165                         return NULL;
 166                 es = (struct ext2_super_block *) (((char *)bh->b_data) + BLOCK_SIZE);
 167                 s->u.ext2_sb.s_es = es;
 168                 if (es->s_magic != EXT2_SUPER_MAGIC) {
 169                         s->s_dev = 0;
 170                         unlock_super (s);
 171                         brelse (bh);
 172                         printk ("ext2-FS: Magic mismatch, very weird !\n");
 173                         return NULL;
 174                 }
 175         }
 176         s->u.ext2_sb.s_frag_size = EXT2_MIN_FRAG_SIZE <<
 177                                    es->s_log_frag_size;
 178         if (s->u.ext2_sb.s_frag_size)
 179                 s->u.ext2_sb.s_frags_per_block = s->s_blocksize /
 180                                                    s->u.ext2_sb.s_frag_size;
 181         else
 182                 s->s_magic = 0;
 183         s->u.ext2_sb.s_blocks_per_group = es->s_blocks_per_group;
 184         s->u.ext2_sb.s_frags_per_group = es->s_frags_per_group;
 185         s->u.ext2_sb.s_inodes_per_group = es->s_inodes_per_group;
 186         s->u.ext2_sb.s_inodes_per_block = s->s_blocksize /
 187                                           sizeof (struct ext2_inode);
 188         s->u.ext2_sb.s_desc_per_block = s->s_blocksize /
 189                                         sizeof (struct ext2_group_desc);
 190         s->u.ext2_sb.s_sbh = bh;
 191         s->u.ext2_sb.s_es = es;
 192         s->u.ext2_sb.s_was_mounted_valid = es->s_valid;
 193         s->u.ext2_sb.s_rename_lock = 0;
 194         s->u.ext2_sb.s_rename_wait = NULL;
 195 #ifdef EXT2FS_PRE_02B_COMPAT
 196         if (s->s_magic == EXT2_OLD_SUPER_MAGIC) {
 197                 if (es->s_blocks_count > 262144) {
 198                          /* fs > 256 MB can't be converted */ 
 199                         s->s_dev = 0;
 200                         unlock_super (s);
 201                         brelse (bh);
 202                         printk ("EXT2-fs: trying to mount a pre-0.2b file"
 203                                 "system which cannot be converted\n");
 204                         return NULL;
 205                 }
 206                 printk ("EXT2-fs: mounting a pre 0.2b file system, "
 207                         "will try to convert the structure\n");
 208                 if (s->s_flags & MS_RDONLY == 0) {
 209                         s->s_dev = 0;
 210                         unlock_super (s);
 211                         brelse (bh);
 212                         printk ("EXT2-fs: cannot convert a read-only fs\n");
 213                         return NULL;
 214                 }
 215                 if (!convert_pre_02b_fs (s, bh)) {
 216                         s->s_dev = 0;
 217                         unlock_super (s);
 218                         brelse (bh);
 219                         printk ("EXT2-fs: conversion failed !!!\n");
 220                         return NULL;
 221                 }
 222                 printk ("EXT2-fs: conversion succeeded !!!\n");
 223                 fs_converted = 1;
 224         }
 225 #endif
 226         if (s->s_magic != EXT2_SUPER_MAGIC) {
 227                 s->s_dev = 0;
 228                 unlock_super (s);
 229                 brelse (bh);
 230                 if (!silent)
 231                         printk ("VFS: Can't find an ext2 filesystem on dev 0x%04x.\n",
 232                                 dev);
 233                 return NULL;
 234         }
 235         if (s->s_blocksize != bh->b_size) {
 236                 s->s_dev = 0;
 237                 unlock_super (s);
 238                 brelse (bh);
 239                 if (!silent)
 240                         printk ("VFS: Unsupported blocksize on dev 0x%04x.\n",
 241                                 dev);
 242                 return NULL;
 243         }
 244 
 245         if (s->s_blocksize != s->u.ext2_sb.s_frag_size) {
 246                 s->s_dev = 0;
 247                 unlock_super (s);
 248                 brelse (bh);
 249                 printk ("EXT2-fs: fragsize %d != blocksize %d (not supported yet)\n",
 250                         s->u.ext2_sb.s_frag_size, s->s_blocksize);
 251                 return NULL;
 252         }
 253 
 254         if (!es->s_valid)
 255                 printk ("EXT2-fs warning: mounting unchecked file system, "
 256                         "running e2fsck is recommended\n");
 257         s->u.ext2_sb.s_groups_count = (es->s_blocks_count -
 258                                        es->s_first_data_block +
 259                                        EXT2_BLOCKS_PER_GROUP(s) - 1) /
 260                                        EXT2_BLOCKS_PER_GROUP(s);
 261         for (i = 0; i < EXT2_MAX_GROUP_DESC; i++)
 262                 s->u.ext2_sb.s_group_desc[i] = NULL;
 263         bh_count = (s->u.ext2_sb.s_groups_count + EXT2_DESC_PER_BLOCK(s) - 1) /
 264                    EXT2_DESC_PER_BLOCK(s);
 265         if (bh_count >= EXT2_MAX_GROUP_DESC) {
 266                 s->s_dev = 0;
 267                 unlock_super (s);
 268                 brelse (bh);
 269                 printk ("EXT2-fs: file system too big\n");
 270                 return NULL;
 271         }
 272         for (i = 0; i < bh_count; i++) {
 273                 s->u.ext2_sb.s_group_desc[i] = bread (dev, i + 2,
 274                                                       s->s_blocksize);
 275                 if (!s->u.ext2_sb.s_group_desc[i]) {
 276                         s->s_dev = 0;
 277                         unlock_super (s);
 278                         for (j = 0; j < i; j++)
 279                                 brelse (s->u.ext2_sb.s_group_desc[i]);
 280                         brelse (bh);
 281                         printk ("ext2-FS: unable to read group descriptors\n");
 282                         return NULL;
 283                 }
 284         }
 285         for (i = 0; i < EXT2_MAX_GROUP_LOADED; i++) {
 286                 s->u.ext2_sb.s_inode_bitmap_number[i] = 0;
 287                 s->u.ext2_sb.s_inode_bitmap[i] = NULL;
 288                 s->u.ext2_sb.s_block_bitmap_number[i] = 0;
 289                 s->u.ext2_sb.s_block_bitmap[i] = NULL;
 290         }
 291         s->u.ext2_sb.s_loaded_inode_bitmaps = 0;
 292         s->u.ext2_sb.s_loaded_block_bitmaps = 0;
 293         unlock_super (s);
 294         /* set up enough so that it can read an inode */
 295         s->s_dev = dev;
 296         s->s_op = &ext2_sops;
 297         if (!(s->s_mounted = iget (s, EXT2_ROOT_INO))) {
 298                 s->s_dev = 0;
 299                 for (i = 0; i < EXT2_MAX_GROUP_DESC; i++)
 300                         if (s->u.ext2_sb.s_group_desc[i])
 301                                 brelse (s->u.ext2_sb.s_group_desc[i]);
 302                 brelse (bh);
 303                 printk ("EXT2-fs: get root inode failed\n");
 304                 return NULL;
 305         }
 306         if ((s->s_flags & MS_RDONLY) == 0) {
 307                 es->s_valid = 0;
 308                 es->s_mtime = CURRENT_TIME;
 309                 bh->b_dirt = 1;
 310                 s->s_dirt = 1;
 311         }
 312 #ifdef EXT2FS_PRE_02B_COMPAT
 313         if (fs_converted) {
 314                 for (i = 0; i < bh_count; i++)
 315                         s->u.ext2_sb.s_group_desc[i]->b_dirt = 1;
 316                 s->s_dirt = 1;
 317         }
 318 #endif
 319         printk ("[EXT II FS %s, %s, bs=%d, fs=%d, gc=%d, bpg=%d, ipg=%d]\n",
 320                 EXT2FS_VERSION, EXT2FS_DATE, s->s_blocksize,
 321                 s->u.ext2_sb.s_frag_size, s->u.ext2_sb.s_groups_count,
 322                 EXT2_BLOCKS_PER_GROUP(s), EXT2_INODES_PER_GROUP(s));
 323         return s;
 324 }
 325 
 326 /*
 327  * In the second extended file system, it is not necessary to
 328  * write the super block since we use a mapping of the
 329  * disk super block in a buffer.
 330  *
 331  * However, this function is still used to set the fs valid
 332  * flags to 0.  We need to set this flag to 0 since the fs
 333  * may have been checked while mounted and e2fsck may have
 334  * set s_valid to 1 after some corrections.
 335  */
 336 
 337 static void ext2_commit_super (struct super_block * sb,
     /* [previous][next][first][last][top][bottom][index][help] */
 338                                struct ext2_super_block * es)
 339 {
 340         es->s_wtime = CURRENT_TIME;
 341         sb->u.ext2_sb.s_sbh->b_dirt = 1;
 342         sb->s_dirt = 0;
 343 }
 344 
 345 void ext2_write_super (struct super_block * sb)
     /* [previous][next][first][last][top][bottom][index][help] */
 346 {
 347         struct ext2_super_block * es;
 348 
 349         if ((sb->s_flags & MS_RDONLY) == 0) {
 350                 es = sb->u.ext2_sb.s_es;
 351 #ifdef EXT2FS_DEBUG
 352                 printk ("ext2_write_super: setting valid to 0\n");
 353 #endif
 354                 if (es->s_valid) {
 355                         es->s_valid = 0;
 356                         es->s_mtime = CURRENT_TIME;
 357                 }
 358                 ext2_commit_super (sb, es);
 359         }
 360         sb->s_dirt = 0;
 361 }
 362 
 363 int ext2_remount (struct super_block * sb, int * flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 364 {
 365         struct ext2_super_block * es;
 366 
 367         es = sb->u.ext2_sb.s_es;
 368         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
 369                 return 0;
 370         if (*flags & MS_RDONLY) {
 371                 if (es->s_valid || !sb->u.ext2_sb.s_was_mounted_valid)
 372                         return 0;
 373                 /* OK, we are remounting a valid rw partition rdonly, so set
 374                    the rdonly flag and then mark the partition as valid
 375                    again. */
 376                 es->s_valid = sb->u.ext2_sb.s_was_mounted_valid;
 377                 es->s_mtime = CURRENT_TIME;
 378                 sb->u.ext2_sb.s_sbh->b_dirt = 1;
 379                 sb->s_dirt = 1;
 380                 ext2_commit_super (sb, es);
 381         }
 382         else {
 383                 /* Mounting a RDONLY partition read-write, so reread and
 384                    store the current valid flag.  (It may have been changed 
 385                    by e2fsck since we originally mounted the partition.)  */
 386                 sb->u.ext2_sb.s_was_mounted_valid = es->s_valid;
 387                 if (!es->s_valid)
 388                         printk ("EXT2-fs warning: remounting unchecked fs, "
 389                                 "running e2fsck is recommended\n");
 390                 else {
 391                         es->s_valid = 0;
 392                         es->s_mtime = CURRENT_TIME;
 393                         sb->u.ext2_sb.s_sbh->b_dirt = 1;
 394                         sb->s_dirt = 1;
 395                 }
 396         }
 397         return 0;
 398 }
 399 
 400 void ext2_statfs (struct super_block * sb, struct statfs * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 401 {
 402         long tmp;
 403 
 404         put_fs_long (EXT2_SUPER_MAGIC, &buf->f_type);
 405         put_fs_long (sb->s_blocksize, &buf->f_bsize);
 406         put_fs_long (sb->u.ext2_sb.s_es->s_blocks_count, &buf->f_blocks);
 407         tmp = ext2_count_free_blocks (sb);
 408         put_fs_long (tmp, &buf->f_bfree);
 409         if (tmp >= sb->u.ext2_sb.s_es->s_r_blocks_count)
 410                 put_fs_long (tmp - sb->u.ext2_sb.s_es->s_r_blocks_count,
 411                              &buf->f_bavail);
 412         else
 413                 put_fs_long (0, &buf->f_bavail);
 414         put_fs_long (sb->u.ext2_sb.s_es->s_inodes_count, &buf->f_files);
 415         put_fs_long (ext2_count_free_inodes (sb), &buf->f_ffree);
 416         put_fs_long (EXT2_NAME_LEN, &buf->f_namelen);
 417         /* Don't know what value to put in buf->f_fsid */
 418 }
 419 
 420 #define inode_bmap(inode, nr) ((inode)->u.ext2_i.i_data[(nr)])
 421 
 422 static int block_bmap (struct buffer_head * bh, int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 423 {
 424         int tmp;
 425 
 426         if (!bh)
 427                 return 0;
 428         tmp = ((unsigned long *) bh->b_data)[nr];
 429         brelse (bh);
 430         return tmp;
 431 }
 432 
 433 int ext2_bmap (struct inode * inode, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 434 {
 435         int i;
 436         int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 437 
 438         if (block < 0) {
 439                 printk("ext2_bmap: block < 0");
 440                 return 0;
 441         }
 442         if (block >= EXT2_NDIR_BLOCKS + addr_per_block +
 443                      addr_per_block * addr_per_block +
 444                      addr_per_block * addr_per_block * addr_per_block) {
 445                 printk ("ext2_bmap: block > big");
 446                 return 0;
 447         }
 448         if (block < EXT2_NDIR_BLOCKS)
 449                 return inode_bmap (inode, block);
 450         block -= EXT2_NDIR_BLOCKS;
 451         if (block < addr_per_block) {
 452                 i = inode_bmap (inode, EXT2_IND_BLOCK);
 453                 if (!i)
 454                         return 0;
 455                 return block_bmap (bread (inode->i_dev, i,
 456                                           inode->i_sb->s_blocksize), block);
 457         }
 458         block -= addr_per_block;
 459         if (block < addr_per_block * addr_per_block) {
 460                 i = inode_bmap (inode, EXT2_DIND_BLOCK);
 461                 if (!i)
 462                         return 0;
 463                 i = block_bmap (bread (inode->i_dev, i,
 464                                        inode->i_sb->s_blocksize),
 465                                 block / addr_per_block);
 466                 if (!i)
 467                         return 0;
 468                 return block_bmap (bread (inode->i_dev, i,
 469                                           inode->i_sb->s_blocksize),
 470                                    block & (addr_per_block - 1));
 471         }
 472         block -= addr_per_block * addr_per_block;
 473         i = inode_bmap (inode, EXT2_TIND_BLOCK);
 474         if (!i)
 475                 return 0;
 476         i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 477                         block / (addr_per_block * addr_per_block));
 478         if (!i)
 479                 return 0;
 480         i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 481                         (block / addr_per_block) & (addr_per_block - 1));
 482         if (!i)
 483                 return 0;
 484         return block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 485                            block & (addr_per_block - 1));
 486 }
 487 
 488 static struct buffer_head * inode_getblk (struct inode * inode, int nr,
     /* [previous][next][first][last][top][bottom][index][help] */
 489                                           int create, int new_block, int *err)
 490 {
 491         int tmp, goal = 0;
 492         unsigned long * p;
 493         struct buffer_head * result;
 494         int blocks = inode->i_sb->s_blocksize / 512;
 495 
 496         p = inode->u.ext2_i.i_data + nr;
 497 repeat:
 498         tmp = *p;
 499         if (tmp) {
 500                 result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize);
 501                 if (tmp == *p)
 502                         return result;
 503                 brelse (result);
 504                 goto repeat;
 505         }
 506         if (!create || new_block >= 
 507             (current->rlim[RLIMIT_FSIZE].rlim_cur >>
 508              EXT2_BLOCK_SIZE_BITS(inode->i_sb))) {
 509                 *err = -EFBIG;
 510                 return NULL;
 511         }
 512         if (inode->u.ext2_i.i_next_alloc_block == new_block)
 513                 goal = inode->u.ext2_i.i_next_alloc_goal;
 514 #ifdef EXT2FS_DEBUG
 515         printk ("ext2 inode_getblk: hint = %d,", goal);
 516 #endif
 517         if (!goal) {
 518                 for (tmp = nr - 1; tmp >= 0; tmp--) {
 519                         if (inode->u.ext2_i.i_data[tmp]) {
 520                                 goal = inode->u.ext2_i.i_data[tmp];
 521                                 break;
 522                         }
 523                 }
 524                 if (!goal)
 525                         goal = (inode->u.ext2_i.i_block_group * 
 526                                 EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
 527                                inode->i_sb->u.ext2_sb.s_es->s_first_data_block;
 528         }
 529 
 530 #ifdef EXT2FS_DEBUG
 531         printk (" goal = %d.\n", goal);
 532 #endif
 533         tmp = ext2_new_block (inode->i_sb, goal);
 534         if (!tmp)
 535                 return NULL;
 536         result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize);
 537         if (*p) {
 538                 ext2_free_block (inode->i_sb, tmp);
 539                 brelse (result);
 540                 goto repeat;
 541         }
 542         *p = tmp;
 543         inode->u.ext2_i.i_next_alloc_block = new_block;
 544         inode->u.ext2_i.i_next_alloc_goal = tmp;
 545         inode->i_ctime = CURRENT_TIME;
 546         inode->i_blocks += blocks;
 547         inode->i_dirt = 1;
 548         return result;
 549 }
 550 
 551 static struct buffer_head * block_getblk (struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
 552                                           struct buffer_head * bh, int nr,
 553                                           int create, int blocksize, 
 554                                           int new_block, int *err)
 555 {
 556         int tmp, goal = 0;
 557         unsigned long * p;
 558         struct buffer_head * result;
 559         int blocks = inode->i_sb->s_blocksize / 512;
 560 
 561         if (!bh)
 562                 return NULL;
 563         if (!bh->b_uptodate) {
 564                 ll_rw_block (READ, 1, &bh);
 565                 wait_on_buffer (bh);
 566                 if (!bh->b_uptodate) {
 567                         brelse (bh);
 568                         return NULL;
 569                 }
 570         }
 571         p = nr + (unsigned long *) bh->b_data;
 572 repeat:
 573         tmp = *p;
 574         if (tmp) {
 575                 result = getblk (bh->b_dev, tmp, blocksize);
 576                 if (tmp == *p) {
 577                         brelse (bh);
 578                         return result;
 579                 }
 580                 brelse (result);
 581                 goto repeat;
 582         }
 583         if (!create || new_block >= 
 584             (current->rlim[RLIMIT_FSIZE].rlim_cur >> 
 585              EXT2_BLOCK_SIZE_BITS(inode->i_sb))) {
 586                 brelse (bh);
 587                 *err = -EFBIG;
 588                 return NULL;
 589         }
 590         if (inode->u.ext2_i.i_next_alloc_block == new_block)
 591                 goal = inode->u.ext2_i.i_next_alloc_goal;
 592         if (!goal) {
 593                 for (tmp = nr - 1; tmp >= 0; tmp--) {
 594                         if (((unsigned long *) bh->b_data)[tmp]) {
 595                                 goal = ((unsigned long *)bh->b_data)[tmp];
 596                                 break;
 597                         }
 598                 }
 599                 if (!goal)
 600                         goal = bh->b_blocknr+1;
 601         }
 602         tmp = ext2_new_block (inode->i_sb, goal);
 603         if (!tmp) {
 604 #ifdef EXT2FS_DEBUG
 605                 printk ("inode_getblk: ext2_new_block returned 0\n");
 606 #endif
 607                 brelse (bh);
 608                 return NULL;
 609         }
 610         result = getblk (bh->b_dev, tmp, blocksize);
 611         if (*p) {
 612                 ext2_free_block (inode->i_sb, tmp);
 613                 brelse (result);
 614                 goto repeat;
 615         }
 616         *p = tmp;
 617         bh->b_dirt = 1;
 618         inode->i_ctime = CURRENT_TIME;
 619         inode->i_blocks += blocks;
 620         inode->i_dirt = 1;
 621         inode->u.ext2_i.i_next_alloc_block = new_block;
 622         inode->u.ext2_i.i_next_alloc_goal = tmp;
 623         brelse (bh);
 624         return result;
 625 }
 626 
 627 struct buffer_head * ext2_getblk (struct inode * inode, int block,
     /* [previous][next][first][last][top][bottom][index][help] */
 628                                   int create, int *err)
 629 {
 630         struct buffer_head * bh;
 631         int b;
 632         unsigned long addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 633 
 634         *err = -EIO;
 635         if (block < 0) {
 636                 printk ("ext2_getblk: block < 0\n");
 637                 return NULL;
 638         }
 639         if (block > EXT2_NDIR_BLOCKS + addr_per_block  +
 640                     addr_per_block * addr_per_block +
 641                     addr_per_block * addr_per_block * addr_per_block) {
 642                 printk ("ext2_getblk: block > big\n");
 643                 return NULL;
 644         }
 645         /* If this is a sequential block allocation, set the next_alloc_block
 646            to this block now so that all the indblock and data block
 647            allocations use the same goal zone */
 648 #ifdef EXT2FS_DEBUG
 649         printk ("ext2_getblk: block %d, next %d, goal %d.\n", block, 
 650                 inode->u.ext2_i.i_next_alloc_block,
 651                 inode->u.ext2_i.i_next_alloc_goal);
 652 #endif
 653         if (block == inode->u.ext2_i.i_next_alloc_block + 1) {
 654                 inode->u.ext2_i.i_next_alloc_block++;
 655                 inode->u.ext2_i.i_next_alloc_goal++;
 656         }
 657 
 658         *err = -ENOSPC;
 659         b = block;
 660         if (block < EXT2_NDIR_BLOCKS)
 661                 return inode_getblk (inode, block, create, b, err);
 662         block -= EXT2_NDIR_BLOCKS;
 663         if (block < addr_per_block) {
 664                 bh = inode_getblk (inode, EXT2_IND_BLOCK, create, b, err);
 665                 return block_getblk (inode, bh, block, create,
 666                                      inode->i_sb->s_blocksize, b, err);
 667         }
 668         block -= addr_per_block;
 669         if (block < addr_per_block * addr_per_block) {
 670                 bh = inode_getblk (inode, EXT2_DIND_BLOCK, create, b, err);
 671                 bh = block_getblk (inode, bh, block / addr_per_block, create,
 672                                    inode->i_sb->s_blocksize, b, err);
 673                 return block_getblk (inode, bh, block & (addr_per_block - 1),
 674                                      create, inode->i_sb->s_blocksize, b, err);
 675         }
 676         block -= addr_per_block * addr_per_block;
 677         bh = inode_getblk (inode, EXT2_TIND_BLOCK, create, b, err);
 678         bh = block_getblk (inode, bh, block/(addr_per_block * addr_per_block),
 679                            create, inode->i_sb->s_blocksize, b, err);
 680         bh = block_getblk (inode, bh, (block/addr_per_block) & (addr_per_block - 1),
 681                            create, inode->i_sb->s_blocksize, b, err);
 682         return block_getblk (inode, bh, block & (addr_per_block - 1), create,
 683                              inode->i_sb->s_blocksize, b, err);
 684 }
 685 
 686 struct buffer_head * ext2_bread (struct inode * inode, int block, 
     /* [previous][next][first][last][top][bottom][index][help] */
 687                                  int create, int *err)
 688 {
 689         struct buffer_head * bh;
 690 
 691         bh = ext2_getblk (inode, block, create, err);
 692         if (!bh || bh->b_uptodate)
 693                 return bh;
 694         ll_rw_block (READ, 1, &bh);
 695         wait_on_buffer (bh);
 696         if (bh->b_uptodate)
 697                 return bh;
 698         brelse (bh);
 699         *err = -EIO;
 700         return NULL;
 701 }
 702 
 703 void ext2_read_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 704 {
 705         struct buffer_head * bh;
 706         struct ext2_inode * raw_inode;
 707         unsigned long block_group;
 708         unsigned long group_desc;
 709         unsigned long desc;
 710         unsigned long block;
 711         struct ext2_group_desc * gdp;
 712 
 713         if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino != EXT2_ACL_IDX_INO &&
 714              inode->i_ino != EXT2_ACL_DATA_INO && inode->i_ino < EXT2_FIRST_INO) ||
 715             inode->i_ino > inode->i_sb->u.ext2_sb.s_es->s_inodes_count) {
 716                 printk ("ext2_read_inode: bad inode number on dev %0x04: %d\n",
 717                         inode->i_dev, inode->i_ino);
 718                 return;
 719         }
 720         block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
 721         if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count)
 722                 panic ("ext2_read_inode: group >= groups count");
 723         group_desc = block_group / EXT2_DESC_PER_BLOCK(inode->i_sb);
 724         desc = block_group % EXT2_DESC_PER_BLOCK(inode->i_sb);
 725         bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
 726         if (!bh)
 727                 panic ("ext2_read_inode: Descriptor not loaded");
 728         gdp = (struct ext2_group_desc *) bh->b_data;
 729         block = gdp[desc].bg_inode_table +
 730                 (((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb))
 731                  / EXT2_INODES_PER_BLOCK(inode->i_sb));
 732         if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize)))
 733                 panic ("ext2_read_inode: unable to read i-node block");
 734         raw_inode = ((struct ext2_inode *) bh->b_data) +
 735                 (inode->i_ino - 1) % EXT2_INODES_PER_BLOCK(inode->i_sb);
 736         inode->i_mode = raw_inode->i_mode;
 737         inode->i_uid = raw_inode->i_uid;
 738         inode->i_gid = raw_inode->i_gid;
 739         inode->i_nlink = raw_inode->i_links_count;
 740         inode->i_size = raw_inode->i_size;
 741         inode->i_atime = raw_inode->i_atime;
 742         inode->i_ctime = raw_inode->i_ctime;
 743         inode->i_mtime = raw_inode->i_mtime;
 744         inode->u.ext2_i.i_dtime = raw_inode->i_dtime;
 745         inode->i_blksize = inode->i_sb->s_blocksize;
 746         inode->i_blocks = raw_inode->i_blocks;
 747         inode->u.ext2_i.i_flags = raw_inode->i_flags;
 748         inode->u.ext2_i.i_faddr = raw_inode->i_faddr;
 749         inode->u.ext2_i.i_frag = raw_inode->i_frag;
 750         inode->u.ext2_i.i_fsize = raw_inode->i_fsize;
 751         inode->u.ext2_i.i_file_acl = raw_inode->i_file_acl;
 752         inode->u.ext2_i.i_dir_acl = raw_inode->i_dir_acl;
 753         inode->u.ext2_i.i_version = raw_inode->i_version;
 754         inode->u.ext2_i.i_block_group = block_group;
 755         inode->u.ext2_i.i_next_alloc_block = 0;
 756         inode->u.ext2_i.i_next_alloc_goal = 0;
 757         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 758                 inode->i_rdev = raw_inode->i_block[0];
 759         else for (block = 0; block < EXT2_N_BLOCKS; block++)
 760                 inode->u.ext2_i.i_data[block] = raw_inode->i_block[block];
 761         brelse (bh);
 762         inode->i_op = NULL;
 763         if (inode->i_ino == EXT2_ACL_IDX_INO ||
 764             inode->i_ino == EXT2_ACL_DATA_INO)
 765                 /* Nothing to do */ ;
 766         else if (S_ISREG(inode->i_mode))
 767                 inode->i_op = &ext2_file_inode_operations;
 768         else if (S_ISDIR(inode->i_mode))
 769                 inode->i_op = &ext2_dir_inode_operations;
 770         else if (S_ISLNK(inode->i_mode))
 771                 inode->i_op = &ext2_symlink_inode_operations;
 772         else if (S_ISCHR(inode->i_mode))
 773                 inode->i_op = &chrdev_inode_operations;
 774         else if (S_ISBLK(inode->i_mode))
 775                 inode->i_op = &blkdev_inode_operations;
 776         else if (S_ISFIFO(inode->i_mode))
 777                 init_fifo(inode);
 778 }
 779 
 780 static struct buffer_head * ext2_update_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 781 {
 782         struct buffer_head * bh;
 783         struct ext2_inode * raw_inode;
 784         unsigned long block_group;
 785         unsigned long group_desc;
 786         unsigned long desc;
 787         unsigned long block;
 788         struct ext2_group_desc * gdp;
 789 
 790         if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino < EXT2_FIRST_INO) ||
 791             inode->i_ino > inode->i_sb->u.ext2_sb.s_es->s_inodes_count) {
 792                 printk ("ext2_write_inode: bad inode number of dev %0x04: %d\n",
 793                         inode->i_dev, inode->i_ino);
 794                 return 0;
 795         }
 796         block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
 797         if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count)
 798                 panic ("ext2_write_inode: group >= groups count");
 799         group_desc = block_group / EXT2_DESC_PER_BLOCK(inode->i_sb);
 800         desc = block_group % EXT2_DESC_PER_BLOCK(inode->i_sb);
 801         bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
 802         if (!bh)
 803                 panic ("ext2_write_inode: Descriptor not loaded");
 804         gdp = (struct ext2_group_desc *) bh->b_data;
 805         block = gdp[desc].bg_inode_table +
 806                 (((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb))
 807                  / EXT2_INODES_PER_BLOCK(inode->i_sb));
 808         if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize)))
 809                 panic ("ext2_write_inode: unable to read i-node block");
 810         raw_inode = ((struct ext2_inode *)bh->b_data) +
 811                 (inode->i_ino - 1) % EXT2_INODES_PER_BLOCK(inode->i_sb);
 812         raw_inode->i_mode = inode->i_mode;
 813         raw_inode->i_uid = inode->i_uid;
 814         raw_inode->i_gid = inode->i_gid;
 815         raw_inode->i_links_count = inode->i_nlink;
 816         raw_inode->i_size = inode->i_size;
 817         raw_inode->i_atime = inode->i_atime;
 818         raw_inode->i_ctime = inode->i_ctime;
 819         raw_inode->i_mtime = inode->i_mtime;
 820         raw_inode->i_blocks = inode->i_blocks;
 821         raw_inode->i_dtime = inode->u.ext2_i.i_dtime;
 822         raw_inode->i_flags = inode->u.ext2_i.i_flags;
 823         raw_inode->i_faddr = inode->u.ext2_i.i_faddr;
 824         raw_inode->i_frag = inode->u.ext2_i.i_frag;
 825         raw_inode->i_fsize = inode->u.ext2_i.i_fsize;
 826         raw_inode->i_file_acl = inode->u.ext2_i.i_file_acl;
 827         raw_inode->i_dir_acl = inode->u.ext2_i.i_dir_acl;
 828         raw_inode->i_version = inode->u.ext2_i.i_version;
 829         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 830                 raw_inode->i_block[0] = inode->i_rdev;
 831         else for (block = 0; block < EXT2_N_BLOCKS; block++)
 832                 raw_inode->i_block[block] = inode->u.ext2_i.i_data[block];
 833         bh->b_dirt = 1;
 834         inode->i_dirt = 0;
 835         return bh;
 836 }
 837 
 838 void ext2_write_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 839 {
 840         struct buffer_head * bh;
 841         bh = ext2_update_inode (inode);
 842         brelse (bh);
 843 }
 844 
 845 int ext2_sync_inode (struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 846 {
 847         int err = 0;
 848         struct buffer_head *bh;
 849 
 850         bh = ext2_update_inode (inode);
 851         if (bh && bh->b_dirt)
 852         {
 853                 ll_rw_block (WRITE, 1, &bh);
 854                 wait_on_buffer (bh);
 855                 if (bh->b_req && !bh->b_uptodate)
 856                 {
 857                         printk ("IO error syncing ext2 inode [%04x:%08x]\n",
 858                                 inode->i_dev, inode->i_ino);
 859                         err = -1;
 860                 }
 861         }
 862         else if (!bh)
 863                 err = -1;
 864         brelse (bh);
 865         return err;
 866 }

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