home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / EXT2FS / RW_BITM.C < prev   
C/C++ Source or Header  |  1995-05-10  |  6KB  |  237 lines

  1. /*
  2.  * rw_bitmaps.c --- routines to read and write the  inode and block bitmaps.
  3.  *
  4.  * Copyright (C) 1993,1994 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 <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <fcntl.h>
  13. #include <time.h>
  14. #include <sys/stat.h>
  15. #include <errno.h>
  16. #include <sys/types.h>
  17.  
  18. #include <linux/ext2_fs.h>
  19.  
  20. #include "ext2fs.h"
  21.  
  22. errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
  23. {
  24.     int         i;
  25.     int        nbytes;
  26.     errcode_t    retval;
  27.     char * inode_bitmap = fs->inode_map->bitmap;
  28.     char * bitmap_block = NULL;
  29.  
  30.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  31.  
  32.     if (!(fs->flags & EXT2_FLAG_RW))
  33.         return EXT2_ET_RO_FILSYS;
  34.     if (!inode_bitmap)
  35.         return 0;
  36.     nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
  37.     bitmap_block = malloc(fs->blocksize);
  38.     if (!bitmap_block)
  39.         return ENOMEM;
  40.     memset(bitmap_block, 0xff, fs->blocksize);
  41.     for (i = 0; i < fs->group_desc_count; i++) {
  42.         memcpy(bitmap_block, inode_bitmap, nbytes);
  43.         retval = io_channel_write_blk(fs->io,
  44.               fs->group_desc[i].bg_inode_bitmap, 1,
  45.                           bitmap_block);
  46.         if (retval)
  47.             return EXT2_ET_INODE_BITMAP_WRITE;
  48.         inode_bitmap += nbytes;
  49.     }
  50.     fs->flags |= EXT2_FLAG_CHANGED;
  51.     fs->flags &= ~EXT2_FLAG_IB_DIRTY;
  52.     free(bitmap_block);
  53.     return 0;
  54. }
  55.  
  56. errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
  57. {
  58.     int         i;
  59.     int        j;
  60.     int        nbytes;
  61.     int        nbits;
  62.     errcode_t    retval;
  63.     char * block_bitmap = fs->block_map->bitmap;
  64.     char * bitmap_block = NULL;
  65.  
  66.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  67.  
  68.     if (!(fs->flags & EXT2_FLAG_RW))
  69.         return EXT2_ET_RO_FILSYS;
  70.     if (!block_bitmap)
  71.         return 0;
  72.     nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
  73.     bitmap_block = malloc(fs->blocksize);
  74.     if (!bitmap_block)
  75.         return ENOMEM;
  76.     memset(bitmap_block, 0xff, fs->blocksize);
  77.     for (i = 0; i < fs->group_desc_count; i++) {
  78.         memcpy(bitmap_block, block_bitmap, nbytes);
  79.         if (i == fs->group_desc_count - 1) {
  80.             /* Force bitmap padding for the last group */
  81.             nbits = (fs->super->s_blocks_count
  82.                  - fs->super->s_first_data_block)
  83.                 % EXT2_BLOCKS_PER_GROUP(fs->super);
  84.             if (nbits)
  85.                 for (j = nbits; j < fs->blocksize * 8; j++)
  86.                     set_bit(j, bitmap_block);
  87.         }
  88.         retval = io_channel_write_blk(fs->io,
  89.               fs->group_desc[i].bg_block_bitmap, 1,
  90.                           bitmap_block);
  91.         if (retval)
  92.             return EXT2_ET_BLOCK_BITMAP_WRITE;
  93.         block_bitmap += nbytes;
  94.     }
  95.     fs->flags |= EXT2_FLAG_CHANGED;
  96.     fs->flags &= ~EXT2_FLAG_BB_DIRTY;
  97.     free(bitmap_block);
  98.     return 0;
  99. }
  100.  
  101. errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
  102. {
  103.     int i;
  104.     char * inode_bitmap;
  105.     char *buf;
  106.     errcode_t    retval;
  107.     int nbytes = EXT2_INODES_PER_GROUP(fs->super) / 8;
  108.  
  109.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  110.  
  111.     fs->write_bitmaps = ext2fs_write_bitmaps;
  112.  
  113.     if (fs->inode_map)
  114.         ext2fs_free_inode_bitmap(fs->inode_map);
  115.     buf = malloc(strlen(fs->device_name) + 80);
  116.     sprintf(buf, "inode bitmap for %s", fs->device_name);
  117.     retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
  118.     free(buf);
  119.     if (retval)
  120.         return retval;
  121.     inode_bitmap = fs->inode_map->bitmap;
  122.  
  123.     buf = malloc(fs->blocksize);
  124.     if (!buf)
  125.         return ENOMEM;
  126.  
  127.     for (i = 0; i < fs->group_desc_count; i++) {
  128.         retval = io_channel_read_blk(fs->io,
  129.                  fs->group_desc[i].bg_inode_bitmap, 1,
  130.                          buf);
  131.         if (retval) {
  132.             retval = EXT2_ET_INODE_BITMAP_READ;
  133.             goto cleanup;
  134.         }
  135.         memcpy(inode_bitmap, buf, nbytes);
  136.         inode_bitmap += nbytes;
  137.     }
  138.     free(buf);
  139.     return 0;
  140.     
  141. cleanup:
  142.     free(fs->inode_map);
  143.     fs->inode_map = 0;
  144.     if (buf)
  145.         free(buf);
  146.     return retval;
  147. }
  148.  
  149. errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
  150. {
  151.     int i;
  152.     char * block_bitmap;
  153.     char *buf;
  154.     errcode_t retval;
  155.     int nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
  156.  
  157.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  158.  
  159.     fs->write_bitmaps = ext2fs_write_bitmaps;
  160.  
  161.     if (fs->block_map)
  162.         ext2fs_free_block_bitmap(fs->block_map);
  163.     buf = malloc(strlen(fs->device_name) + 80);
  164.     sprintf(buf, "block bitmap for %s", fs->device_name);
  165.     retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
  166.     free(buf);
  167.     if (retval)
  168.         return retval;
  169.     block_bitmap = fs->block_map->bitmap;
  170.  
  171.     buf = malloc(fs->blocksize);
  172.     if (!buf)
  173.         return ENOMEM;
  174.  
  175.     for (i = 0; i < fs->group_desc_count; i++) {
  176.         retval = io_channel_read_blk(fs->io,
  177.                  fs->group_desc[i].bg_block_bitmap, 1,
  178.                          buf);
  179.         if (retval) {
  180.             retval = EXT2_ET_BLOCK_BITMAP_READ;
  181.             goto cleanup;
  182.         }
  183.         memcpy(block_bitmap, buf, nbytes);
  184.         block_bitmap += nbytes;
  185.     }
  186.     free(buf);
  187.     return 0;
  188.     
  189. cleanup:
  190.     free(fs->block_map);
  191.     fs->block_map = 0;
  192.     if (buf)
  193.         free(buf);
  194.     return retval;
  195. }
  196.  
  197. errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
  198. {
  199.     errcode_t    retval;
  200.  
  201.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  202.  
  203.     fs->write_bitmaps = ext2fs_write_bitmaps;
  204.  
  205.     if (!fs->inode_map) {
  206.         retval = ext2fs_read_inode_bitmap(fs);
  207.         if (retval)
  208.             return retval;
  209.     }
  210.     if (!fs->block_map) {
  211.         retval = ext2fs_read_block_bitmap(fs);
  212.         if (retval)
  213.             return retval;
  214.     }
  215.     return 0;
  216. }
  217.  
  218. errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
  219. {
  220.     errcode_t    retval;
  221.  
  222.     EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  223.  
  224.     if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
  225.         retval = ext2fs_write_block_bitmap(fs);
  226.         if (retval)
  227.             return retval;
  228.     }
  229.     if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
  230.         retval = ext2fs_write_inode_bitmap(fs);
  231.         if (retval)
  232.             return retval;
  233.     }
  234.     return 0;
  235. }    
  236.  
  237.