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

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