home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / EXT2FS / ALLOC.C next >
C/C++ Source or Header  |  1995-05-10  |  3KB  |  134 lines

  1. /*
  2.  * alloc.c --- allocate new inodes, blocks for ext2fs
  3.  *
  4.  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
  5.  * under the terms of the GNU Public License.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <errno.h>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15.  
  16. #include <linux/ext2_fs.h>
  17.  
  18. #include "ext2fs.h"
  19.  
  20. /*
  21.  * Right now, just search forward from the parent directory's block
  22.  * group to find the next free inode.
  23.  *
  24.  * Should have a special policy for directories.
  25.  */
  26. errcode_t ext2fs_new_inode(ext2_filsys fs, ino_t dir, int mode,
  27.                ext2fs_inode_bitmap map, ino_t *ret)
  28. {
  29.     int    dir_group = 0;
  30.     ino_t    i;
  31.     ino_t    start_inode;
  32.  
  33.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  34.     
  35.     if (!map)
  36.         map = fs->inode_map;
  37.     if (!map)
  38.         return EXT2_ET_NO_INODE_BITMAP;
  39.     
  40.     if (dir > 0) 
  41.         dir_group = (dir - 1) / EXT2_INODES_PER_GROUP(fs->super);
  42.  
  43.     start_inode = (dir_group * EXT2_INODES_PER_GROUP(fs->super)) + 1;
  44.     i = start_inode;
  45.     if (i < EXT2_FIRST_INO)
  46.         i = EXT2_FIRST_INO;
  47.  
  48.     do {
  49.         if (!ext2fs_test_inode_bitmap(map, i))
  50.             break;
  51.         i++;
  52.         if (i > fs->super->s_inodes_count)
  53.             i = EXT2_FIRST_INO;
  54.     } while (i != start_inode);
  55.     
  56.     if (ext2fs_test_inode_bitmap(map, i))
  57.         return ENOSPC;
  58.     *ret = i;
  59.     return 0;
  60. }
  61.  
  62. /*
  63.  * Stupid algorithm --- we now just search forward starting from the
  64.  * goal.  Should put in a smarter one someday....
  65.  */
  66. errcode_t ext2fs_new_block(ext2_filsys fs, blk_t goal,
  67.                ext2fs_block_bitmap map, blk_t *ret)
  68. {
  69.     blk_t    i = goal;
  70.  
  71.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  72.  
  73.     if (!map)
  74.         map = fs->block_map;
  75.     if (!map)
  76.         return EXT2_ET_NO_BLOCK_BITMAP;
  77.     if (!i)
  78.         i = fs->super->s_first_data_block;
  79.     do {
  80.         if (!ext2fs_test_block_bitmap(map, i)) {
  81.             *ret = i;
  82.             return 0;
  83.         }
  84.         i++;
  85.         if (i > fs->super->s_blocks_count)
  86.             i = fs->super->s_first_data_block;
  87.     } while (i != goal);
  88.     return ENOSPC;
  89. }
  90.  
  91. static int check_blocks_free(ext2_filsys fs, ext2fs_block_bitmap map,
  92.                  blk_t blk, int num)
  93. {
  94.     int    i;
  95.  
  96.     for (i=0; i < num; i++) {
  97.         if ((blk+i) > fs->super->s_blocks_count)
  98.             return 0;
  99.         if (ext2fs_test_block_bitmap(map, blk+i))
  100.             return 0;
  101.     }
  102.     return 1;
  103. }
  104.  
  105. errcode_t ext2fs_get_free_blocks(ext2_filsys fs, blk_t start, blk_t finish,
  106.                  int num, ext2fs_block_bitmap map, blk_t *ret)
  107. {
  108.     blk_t    b = start;
  109.  
  110.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  111.  
  112.     if (!map)
  113.         map = fs->block_map;
  114.     if (!map)
  115.         return EXT2_ET_NO_BLOCK_BITMAP;
  116.     if (!b)
  117.         b = fs->super->s_first_data_block;
  118.     if (!finish)
  119.         finish = start;
  120.     if (!num)
  121.         num = 1;
  122.     do {
  123.         if (check_blocks_free(fs, map, b, num)) {
  124.             *ret = b;
  125.             return 0;
  126.         }
  127.         b++;
  128.         if (b > fs->super->s_blocks_count)
  129.             b = fs->super->s_first_data_block;
  130.     } while (b != finish);
  131.     return ENOSPC;
  132. }
  133.  
  134.