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                 es->s_valid = 0;
 355                 ext2_commit_super (sb, es);
 356         }
 357         sb->s_dirt = 0;
 358 }
 359 
 360 int ext2_remount (struct super_block * sb, int * flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 361 {
 362         struct ext2_super_block * es;
 363 
 364         es = sb->u.ext2_sb.s_es;
 365         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
 366                 return 0;
 367         if (*flags & MS_RDONLY) {
 368                 if (es->s_valid || !sb->u.ext2_sb.s_was_mounted_valid)
 369                         return 0;
 370                 /* OK, we are remounting a valid rw partition rdonly, so set
 371                    the rdonly flag and then mark the partition as valid
 372                    again. */
 373                 sb->s_flags |= MS_RDONLY;
 374                 es->s_valid = sb->u.ext2_sb.s_was_mounted_valid;
 375                 ext2_commit_super (sb, es);
 376         }
 377         else {
 378                 /* Mounting a RDONLY partition read-write, so reread and
 379                    store the current valid flag.  (It may have been changed 
 380                    by e2fsck since we originally mounted the partition.)  */
 381                 sb->u.ext2_sb.s_was_mounted_valid = es->s_valid;
 382                 if (!es->s_valid)
 383                         printk ("EXT2-fs warning: remounting unchecked fs, "
 384                                 "running e2fsck is recommended\n");
 385         }
 386         return 0;
 387 }
 388 
 389 void ext2_statfs (struct super_block * sb, struct statfs * buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 390 {
 391         long tmp;
 392 
 393         put_fs_long (EXT2_SUPER_MAGIC, &buf->f_type);
 394         put_fs_long (sb->s_blocksize, &buf->f_bsize);
 395         put_fs_long (sb->u.ext2_sb.s_es->s_blocks_count, &buf->f_blocks);
 396         tmp = ext2_count_free_blocks (sb);
 397         put_fs_long (tmp, &buf->f_bfree);
 398         if (tmp >= sb->u.ext2_sb.s_es->s_r_blocks_count)
 399                 put_fs_long (tmp - sb->u.ext2_sb.s_es->s_r_blocks_count,
 400                              &buf->f_bavail);
 401         else
 402                 put_fs_long (0, &buf->f_bavail);
 403         put_fs_long (sb->u.ext2_sb.s_es->s_inodes_count, &buf->f_files);
 404         put_fs_long (ext2_count_free_inodes (sb), &buf->f_ffree);
 405         put_fs_long (EXT2_NAME_LEN, &buf->f_namelen);
 406         /* Don't know what value to put in buf->f_fsid */
 407 }
 408 
 409 #define inode_bmap(inode, nr) ((inode)->u.ext2_i.i_data[(nr)])
 410 
 411 static int block_bmap (struct buffer_head * bh, int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 412 {
 413         int tmp;
 414 
 415         if (!bh)
 416                 return 0;
 417         tmp = ((unsigned long *) bh->b_data)[nr];
 418         brelse (bh);
 419         return tmp;
 420 }
 421 
 422 int ext2_bmap (struct inode * inode, int block)
     /* [previous][next][first][last][top][bottom][index][help] */
 423 {
 424         int i;
 425         int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 426 
 427         if (block < 0) {
 428                 printk("ext2_bmap: block < 0");
 429                 return 0;
 430         }
 431         if (block >= EXT2_NDIR_BLOCKS + addr_per_block +
 432                      addr_per_block * addr_per_block +
 433                      addr_per_block * addr_per_block * addr_per_block) {
 434                 printk ("ext2_bmap: block > big");
 435                 return 0;
 436         }
 437         if (block < EXT2_NDIR_BLOCKS)
 438                 return inode_bmap (inode, block);
 439         block -= EXT2_NDIR_BLOCKS;
 440         if (block < addr_per_block) {
 441                 i = inode_bmap (inode, EXT2_IND_BLOCK);
 442                 if (!i)
 443                         return 0;
 444                 return block_bmap (bread (inode->i_dev, i,
 445                                           inode->i_sb->s_blocksize), block);
 446         }
 447         block -= addr_per_block;
 448         if (block < addr_per_block * addr_per_block) {
 449                 i = inode_bmap (inode, EXT2_DIND_BLOCK);
 450                 if (!i)
 451                         return 0;
 452                 i = block_bmap (bread (inode->i_dev, i,
 453                                        inode->i_sb->s_blocksize),
 454                                 block / addr_per_block);
 455                 if (!i)
 456                         return 0;
 457                 return block_bmap (bread (inode->i_dev, i,
 458                                           inode->i_sb->s_blocksize),
 459                                    block & (addr_per_block - 1));
 460         }
 461         block -= addr_per_block * addr_per_block;
 462         i = inode_bmap (inode, EXT2_TIND_BLOCK);
 463         if (!i)
 464                 return 0;
 465         i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 466                         block / (addr_per_block * addr_per_block));
 467         if (!i)
 468                 return 0;
 469         i = block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 470                         (block / addr_per_block) & (addr_per_block - 1));
 471         if (!i)
 472                 return 0;
 473         return block_bmap (bread (inode->i_dev, i, inode->i_sb->s_blocksize),
 474                            block & (addr_per_block - 1));
 475 }
 476 
 477 static struct buffer_head * inode_getblk (struct inode * inode, int nr,
     /* [previous][next][first][last][top][bottom][index][help] */
 478                                           int create, int new_block, int *err)
 479 {
 480         int tmp, goal = 0;
 481         unsigned long * p;
 482         struct buffer_head * result;
 483         int blocks = inode->i_sb->s_blocksize / 512;
 484 
 485         p = inode->u.ext2_i.i_data + nr;
 486 repeat:
 487         tmp = *p;
 488         if (tmp) {
 489                 result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize);
 490                 if (tmp == *p)
 491                         return result;
 492                 brelse (result);
 493                 goto repeat;
 494         }
 495         if (!create || new_block >= 
 496             (current->rlim[RLIMIT_FSIZE].rlim_cur >>
 497              EXT2_BLOCK_SIZE_BITS(inode->i_sb))) {
 498                 *err = -EFBIG;
 499                 return NULL;
 500         }
 501         if (inode->u.ext2_i.i_next_alloc_block == new_block)
 502                 goal = inode->u.ext2_i.i_next_alloc_goal;
 503 #ifdef EXT2FS_DEBUG
 504         printk ("ext2 inode_getblk: hint = %d,", goal);
 505 #endif
 506         if (!goal) {
 507                 for (tmp = nr - 1; tmp >= 0; tmp--) {
 508                         if (inode->u.ext2_i.i_data[tmp]) {
 509                                 goal = inode->u.ext2_i.i_data[tmp];
 510                                 break;
 511                         }
 512                 }
 513                 if (!goal)
 514                         goal = (inode->u.ext2_i.i_block_group * 
 515                                 EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
 516                                inode->i_sb->u.ext2_sb.s_es->s_first_data_block;
 517         }
 518 
 519 #ifdef EXT2FS_DEBUG
 520         printk (" goal = %d.\n", goal);
 521 #endif
 522         tmp = ext2_new_block (inode->i_sb, goal);
 523         if (!tmp)
 524                 return NULL;
 525         result = getblk (inode->i_dev, tmp, inode->i_sb->s_blocksize);
 526         if (*p) {
 527                 ext2_free_block (inode->i_sb, tmp);
 528                 brelse (result);
 529                 goto repeat;
 530         }
 531         *p = tmp;
 532         inode->u.ext2_i.i_next_alloc_block = new_block;
 533         inode->u.ext2_i.i_next_alloc_goal = tmp;
 534         inode->i_ctime = CURRENT_TIME;
 535         inode->i_blocks += blocks;
 536         inode->i_dirt = 1;
 537         return result;
 538 }
 539 
 540 static struct buffer_head * block_getblk (struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
 541                                           struct buffer_head * bh, int nr,
 542                                           int create, int blocksize, 
 543                                           int new_block, int *err)
 544 {
 545         int tmp, goal = 0;
 546         unsigned long * p;
 547         struct buffer_head * result;
 548         int blocks = inode->i_sb->s_blocksize / 512;
 549 
 550         if (!bh)
 551                 return NULL;
 552         if (!bh->b_uptodate) {
 553                 ll_rw_block (READ, 1, &bh);
 554                 wait_on_buffer (bh);
 555                 if (!bh->b_uptodate) {
 556                         brelse (bh);
 557                         return NULL;
 558                 }
 559         }
 560         p = nr + (unsigned long *) bh->b_data;
 561 repeat:
 562         tmp = *p;
 563         if (tmp) {
 564                 result = getblk (bh->b_dev, tmp, blocksize);
 565                 if (tmp == *p) {
 566                         brelse (bh);
 567                         return result;
 568                 }
 569                 brelse (result);
 570                 goto repeat;
 571         }
 572         if (!create || new_block >= 
 573             (current->rlim[RLIMIT_FSIZE].rlim_cur >> 
 574              EXT2_BLOCK_SIZE_BITS(inode->i_sb))) {
 575                 brelse (bh);
 576                 *err = -EFBIG;
 577                 return NULL;
 578         }
 579         if (inode->u.ext2_i.i_next_alloc_block == new_block)
 580                 goal = inode->u.ext2_i.i_next_alloc_goal;
 581         if (!goal) {
 582                 for (tmp = nr - 1; tmp >= 0; tmp--) {
 583                         if (((unsigned long *) bh->b_data)[tmp]) {
 584                                 goal = ((unsigned long *)bh->b_data)[tmp];
 585                                 break;
 586                         }
 587                 }
 588                 if (!goal)
 589                         goal = bh->b_blocknr+1;
 590         }
 591         tmp = ext2_new_block (inode->i_sb, goal);
 592         if (!tmp) {
 593 #ifdef EXT2FS_DEBUG
 594                 printk ("inode_getblk: ext2_new_block returned 0\n");
 595 #endif
 596                 brelse (bh);
 597                 return NULL;
 598         }
 599         result = getblk (bh->b_dev, tmp, blocksize);
 600         if (*p) {
 601                 ext2_free_block (inode->i_sb, tmp);
 602                 brelse (result);
 603                 goto repeat;
 604         }
 605         *p = tmp;
 606         bh->b_dirt = 1;
 607         inode->i_ctime = CURRENT_TIME;
 608         inode->i_blocks += blocks;
 609         inode->i_dirt = 1;
 610         inode->u.ext2_i.i_next_alloc_block = new_block;
 611         inode->u.ext2_i.i_next_alloc_goal = tmp;
 612         brelse (bh);
 613         return result;
 614 }
 615 
 616 struct buffer_head * ext2_getblk (struct inode * inode, int block,
     /* [previous][next][first][last][top][bottom][index][help] */
 617                                   int create, int *err)
 618 {
 619         struct buffer_head * bh;
 620         int b;
 621         unsigned long addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
 622 
 623         *err = -EIO;
 624         if (block < 0) {
 625                 printk ("ext2_getblk: block < 0\n");
 626                 return NULL;
 627         }
 628         if (block > EXT2_NDIR_BLOCKS + addr_per_block  +
 629                     addr_per_block * addr_per_block +
 630                     addr_per_block * addr_per_block * addr_per_block) {
 631                 printk ("ext2_getblk: block > big\n");
 632                 return NULL;
 633         }
 634         /* If this is a sequential block allocation, set the next_alloc_block
 635            to this block now so that all the indblock and data block
 636            allocations use the same goal zone */
 637 #ifdef EXT2FS_DEBUG
 638         printk ("ext2_getblk: block %d, next %d, goal %d.\n", block, 
 639                 inode->u.ext2_i.i_next_alloc_block,
 640                 inode->u.ext2_i.i_next_alloc_goal);
 641 #endif
 642         if (block == inode->u.ext2_i.i_next_alloc_block + 1) {
 643                 inode->u.ext2_i.i_next_alloc_block++;
 644                 inode->u.ext2_i.i_next_alloc_goal++;
 645         }
 646 
 647         *err = -ENOSPC;
 648         b = block;
 649         if (block < EXT2_NDIR_BLOCKS)
 650                 return inode_getblk (inode, block, create, b, err);
 651         block -= EXT2_NDIR_BLOCKS;
 652         if (block < addr_per_block) {
 653                 bh = inode_getblk (inode, EXT2_IND_BLOCK, create, b, err);
 654                 return block_getblk (inode, bh, block, create,
 655                                      inode->i_sb->s_blocksize, b, err);
 656         }
 657         block -= addr_per_block;
 658         if (block < addr_per_block * addr_per_block) {
 659                 bh = inode_getblk (inode, EXT2_DIND_BLOCK, create, b, err);
 660                 bh = block_getblk (inode, bh, block / addr_per_block, create,
 661                                    inode->i_sb->s_blocksize, b, err);
 662                 return block_getblk (inode, bh, block & (addr_per_block - 1),
 663                                      create, inode->i_sb->s_blocksize, b, err);
 664         }
 665         block -= addr_per_block * addr_per_block;
 666         bh = inode_getblk (inode, EXT2_TIND_BLOCK, create, b, err);
 667         bh = block_getblk (inode, bh, block/(addr_per_block * addr_per_block),
 668                            create, inode->i_sb->s_blocksize, b, err);
 669         bh = block_getblk (inode, bh, (block/addr_per_block) & (addr_per_block - 1),
 670                            create, inode->i_sb->s_blocksize, b, err);
 671         return block_getblk (inode, bh, block & (addr_per_block - 1), create,
 672                              inode->i_sb->s_blocksize, b, err);
 673 }
 674 
 675 struct buffer_head * ext2_bread (struct inode * inode, int block, 
     /* [previous][next][first][last][top][bottom][index][help] */
 676                                  int create, int *err)
 677 {
 678         struct buffer_head * bh;
 679 
 680         bh = ext2_getblk (inode, block, create, err);
 681         if (!bh || bh->b_uptodate)
 682                 return bh;
 683         ll_rw_block (READ, 1, &bh);
 684         wait_on_buffer (bh);
 685         if (bh->b_uptodate)
 686                 return bh;
 687         brelse (bh);
 688         *err = -EIO;
 689         return NULL;
 690 }
 691 
 692 void ext2_read_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 693 {
 694         struct buffer_head * bh;
 695         struct ext2_inode * raw_inode;
 696         unsigned long block_group;
 697         unsigned long group_desc;
 698         unsigned long desc;
 699         unsigned long block;
 700         struct ext2_group_desc * gdp;
 701 
 702         if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino != EXT2_ACL_IDX_INO &&
 703              inode->i_ino != EXT2_ACL_DATA_INO && inode->i_ino < EXT2_FIRST_INO) ||
 704             inode->i_ino > inode->i_sb->u.ext2_sb.s_es->s_inodes_count) {
 705                 printk ("ext2_read_inode: bad inode number on dev %0x04: %d\n",
 706                         inode->i_dev, inode->i_ino);
 707                 return;
 708         }
 709         block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
 710         if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count)
 711                 panic ("ext2_read_inode: group >= groups count");
 712         group_desc = block_group / EXT2_DESC_PER_BLOCK(inode->i_sb);
 713         desc = block_group % EXT2_DESC_PER_BLOCK(inode->i_sb);
 714         bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
 715         if (!bh)
 716                 panic ("ext2_read_inode: Descriptor not loaded");
 717         gdp = (struct ext2_group_desc *) bh->b_data;
 718         block = gdp[desc].bg_inode_table +
 719                 (((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb))
 720                  / EXT2_INODES_PER_BLOCK(inode->i_sb));
 721         if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize)))
 722                 panic ("ext2_read_inode: unable to read i-node block");
 723         raw_inode = ((struct ext2_inode *) bh->b_data) +
 724                 (inode->i_ino - 1) % EXT2_INODES_PER_BLOCK(inode->i_sb);
 725         inode->i_mode = raw_inode->i_mode;
 726         inode->i_uid = raw_inode->i_uid;
 727         inode->i_gid = raw_inode->i_gid;
 728         inode->i_nlink = raw_inode->i_links_count;
 729         inode->i_size = raw_inode->i_size;
 730         inode->i_atime = raw_inode->i_atime;
 731         inode->i_ctime = raw_inode->i_ctime;
 732         inode->i_mtime = raw_inode->i_mtime;
 733         inode->u.ext2_i.i_dtime = raw_inode->i_dtime;
 734         inode->i_blksize = inode->i_sb->s_blocksize;
 735         inode->i_blocks = raw_inode->i_blocks;
 736         inode->u.ext2_i.i_flags = raw_inode->i_flags;
 737         inode->u.ext2_i.i_faddr = raw_inode->i_faddr;
 738         inode->u.ext2_i.i_frag = raw_inode->i_frag;
 739         inode->u.ext2_i.i_fsize = raw_inode->i_fsize;
 740         inode->u.ext2_i.i_file_acl = raw_inode->i_file_acl;
 741         inode->u.ext2_i.i_dir_acl = raw_inode->i_dir_acl;
 742         inode->u.ext2_i.i_version = raw_inode->i_version;
 743         inode->u.ext2_i.i_block_group = block_group;
 744         inode->u.ext2_i.i_next_alloc_block = 0;
 745         inode->u.ext2_i.i_next_alloc_goal = 0;
 746         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 747                 inode->i_rdev = raw_inode->i_block[0];
 748         else for (block = 0; block < EXT2_N_BLOCKS; block++)
 749                 inode->u.ext2_i.i_data[block] = raw_inode->i_block[block];
 750         brelse (bh);
 751         inode->i_op = NULL;
 752         if (inode->i_ino == EXT2_ACL_IDX_INO ||
 753             inode->i_ino == EXT2_ACL_DATA_INO)
 754                 /* Nothing to do */ ;
 755         else if (S_ISREG(inode->i_mode))
 756                 inode->i_op = &ext2_file_inode_operations;
 757         else if (S_ISDIR(inode->i_mode))
 758                 inode->i_op = &ext2_dir_inode_operations;
 759         else if (S_ISLNK(inode->i_mode))
 760                 inode->i_op = &ext2_symlink_inode_operations;
 761         else if (S_ISCHR(inode->i_mode))
 762                 inode->i_op = &chrdev_inode_operations;
 763         else if (S_ISBLK(inode->i_mode))
 764                 inode->i_op = &blkdev_inode_operations;
 765         else if (S_ISFIFO(inode->i_mode))
 766                 init_fifo(inode);
 767 }
 768 
 769 static struct buffer_head * ext2_update_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 770 {
 771         struct buffer_head * bh;
 772         struct ext2_inode * raw_inode;
 773         unsigned long block_group;
 774         unsigned long group_desc;
 775         unsigned long desc;
 776         unsigned long block;
 777         struct ext2_group_desc * gdp;
 778 
 779         if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino < EXT2_FIRST_INO) ||
 780             inode->i_ino > inode->i_sb->u.ext2_sb.s_es->s_inodes_count) {
 781                 printk ("ext2_write_inode: bad inode number of dev %0x04: %d\n",
 782                         inode->i_dev, inode->i_ino);
 783                 return 0;
 784         }
 785         block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
 786         if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count)
 787                 panic ("ext2_write_inode: group >= groups count");
 788         group_desc = block_group / EXT2_DESC_PER_BLOCK(inode->i_sb);
 789         desc = block_group % EXT2_DESC_PER_BLOCK(inode->i_sb);
 790         bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
 791         if (!bh)
 792                 panic ("ext2_write_inode: Descriptor not loaded");
 793         gdp = (struct ext2_group_desc *) bh->b_data;
 794         block = gdp[desc].bg_inode_table +
 795                 (((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb))
 796                  / EXT2_INODES_PER_BLOCK(inode->i_sb));
 797         if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize)))
 798                 panic ("ext2_write_inode: unable to read i-node block");
 799         raw_inode = ((struct ext2_inode *)bh->b_data) +
 800                 (inode->i_ino - 1) % EXT2_INODES_PER_BLOCK(inode->i_sb);
 801         raw_inode->i_mode = inode->i_mode;
 802         raw_inode->i_uid = inode->i_uid;
 803         raw_inode->i_gid = inode->i_gid;
 804         raw_inode->i_links_count = inode->i_nlink;
 805         raw_inode->i_size = inode->i_size;
 806         raw_inode->i_atime = inode->i_atime;
 807         raw_inode->i_ctime = inode->i_ctime;
 808         raw_inode->i_mtime = inode->i_mtime;
 809         raw_inode->i_blocks = inode->i_blocks;
 810         raw_inode->i_dtime = inode->u.ext2_i.i_dtime;
 811         raw_inode->i_flags = inode->u.ext2_i.i_flags;
 812         raw_inode->i_faddr = inode->u.ext2_i.i_faddr;
 813         raw_inode->i_frag = inode->u.ext2_i.i_frag;
 814         raw_inode->i_fsize = inode->u.ext2_i.i_fsize;
 815         raw_inode->i_file_acl = inode->u.ext2_i.i_file_acl;
 816         raw_inode->i_dir_acl = inode->u.ext2_i.i_dir_acl;
 817         raw_inode->i_version = inode->u.ext2_i.i_version;
 818         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
 819                 raw_inode->i_block[0] = inode->i_rdev;
 820         else for (block = 0; block < EXT2_N_BLOCKS; block++)
 821                 raw_inode->i_block[block] = inode->u.ext2_i.i_data[block];
 822         bh->b_dirt = 1;
 823         inode->i_dirt = 0;
 824         return bh;
 825 }
 826 
 827 void ext2_write_inode (struct inode * inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 828 {
 829         struct buffer_head * bh;
 830         bh = ext2_update_inode (inode);
 831         brelse (bh);
 832 }
 833 
 834 int ext2_sync_inode (struct inode *inode)
     /* [previous][next][first][last][top][bottom][index][help] */
 835 {
 836         int err = 0;
 837         struct buffer_head *bh;
 838 
 839         bh = ext2_update_inode (inode);
 840         if (bh && bh->b_dirt)
 841         {
 842                 ll_rw_block (WRITE, 1, &bh);
 843                 wait_on_buffer (bh);
 844                 if (bh->b_req && !bh->b_uptodate)
 845                 {
 846                         printk ("IO error syncing ext2 inode [%04x:%08x]\n",
 847                                 inode->i_dev, inode->i_ino);
 848                         err = -1;
 849                 }
 850         }
 851         else if (!bh)
 852                 err = -1;
 853         brelse (bh);
 854         return err;
 855 }

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