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

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