rw_bitmaps.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * rw_bitmaps.c --- routines to read and write the inode and block bitmaps.
  3. *
  4. * Copyright (C) 1993, 1994, 1994, 1996 Theodore Ts'o.
  5. *
  6. * %Begin-Header%
  7. * This file may be redistributed under the terms of the GNU Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <fcntl.h>
  17. #include <time.h>
  18. #ifdef HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #ifdef HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #include "ext2_fs.h"
  25. #include "ext2fs.h"
  26. #include "e2image.h"
  27. #if defined(__powerpc__) && defined(EXT2FS_ENABLE_SWAPFS)
  28. /*
  29. * On the PowerPC, the big-endian variant of the ext2 filesystem
  30. * has its bitmaps stored as 32-bit words with bit 0 as the LSB
  31. * of each word. Thus a bitmap with only bit 0 set would be, as
  32. * a string of bytes, 00 00 00 01 00 ...
  33. * To cope with this, we byte-reverse each word of a bitmap if
  34. * we have a big-endian filesystem, that is, if we are *not*
  35. * byte-swapping other word-sized numbers.
  36. */
  37. #define EXT2_BIG_ENDIAN_BITMAPS
  38. #endif
  39. #ifdef EXT2_BIG_ENDIAN_BITMAPS
  40. static void ext2fs_swap_bitmap(ext2_filsys fs, char *bitmap, int nbytes)
  41. {
  42. __u32 *p = (__u32 *) bitmap;
  43. int n;
  44. for (n = nbytes / sizeof(__u32); n > 0; --n, ++p)
  45. *p = ext2fs_swab32(*p);
  46. }
  47. #endif
  48. errcode_t ext2fs_write_inode_bitmap(ext2_filsys fs)
  49. {
  50. dgrp_t i;
  51. size_t nbytes;
  52. errcode_t retval;
  53. char * inode_bitmap = fs->inode_map->bitmap;
  54. char * bitmap_block = NULL;
  55. blk_t blk;
  56. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  57. if (!(fs->flags & EXT2_FLAG_RW))
  58. return EXT2_ET_RO_FILSYS;
  59. if (!inode_bitmap)
  60. return 0;
  61. nbytes = (size_t) ((EXT2_INODES_PER_GROUP(fs->super)+7) / 8);
  62. retval = ext2fs_get_mem(fs->blocksize, &bitmap_block);
  63. if (retval)
  64. return retval;
  65. memset(bitmap_block, 0xff, fs->blocksize);
  66. for (i = 0; i < fs->group_desc_count; i++) {
  67. memcpy(bitmap_block, inode_bitmap, nbytes);
  68. blk = fs->group_desc[i].bg_inode_bitmap;
  69. if (blk) {
  70. #ifdef EXT2_BIG_ENDIAN_BITMAPS
  71. if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  72. (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
  73. ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
  74. #endif
  75. retval = io_channel_write_blk(fs->io, blk, 1,
  76. bitmap_block);
  77. if (retval)
  78. return EXT2_ET_INODE_BITMAP_WRITE;
  79. }
  80. inode_bitmap += nbytes;
  81. }
  82. fs->flags &= ~EXT2_FLAG_IB_DIRTY;
  83. ext2fs_free_mem(&bitmap_block);
  84. return 0;
  85. }
  86. errcode_t ext2fs_write_block_bitmap (ext2_filsys fs)
  87. {
  88. dgrp_t i;
  89. unsigned int j;
  90. int nbytes;
  91. unsigned int nbits;
  92. errcode_t retval;
  93. char * block_bitmap = fs->block_map->bitmap;
  94. char * bitmap_block = NULL;
  95. blk_t blk;
  96. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  97. if (!(fs->flags & EXT2_FLAG_RW))
  98. return EXT2_ET_RO_FILSYS;
  99. if (!block_bitmap)
  100. return 0;
  101. nbytes = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
  102. retval = ext2fs_get_mem(fs->blocksize, &bitmap_block);
  103. if (retval)
  104. return retval;
  105. memset(bitmap_block, 0xff, fs->blocksize);
  106. for (i = 0; i < fs->group_desc_count; i++) {
  107. memcpy(bitmap_block, block_bitmap, nbytes);
  108. if (i == fs->group_desc_count - 1) {
  109. /* Force bitmap padding for the last group */
  110. nbits = ((fs->super->s_blocks_count
  111. - fs->super->s_first_data_block)
  112. % EXT2_BLOCKS_PER_GROUP(fs->super));
  113. if (nbits)
  114. for (j = nbits; j < fs->blocksize * 8; j++)
  115. ext2fs_set_bit(j, bitmap_block);
  116. }
  117. blk = fs->group_desc[i].bg_block_bitmap;
  118. if (blk) {
  119. #ifdef EXT2_BIG_ENDIAN_BITMAPS
  120. if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  121. (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)))
  122. ext2fs_swap_bitmap(fs, bitmap_block, nbytes);
  123. #endif
  124. retval = io_channel_write_blk(fs->io, blk, 1,
  125. bitmap_block);
  126. if (retval)
  127. return EXT2_ET_BLOCK_BITMAP_WRITE;
  128. }
  129. block_bitmap += nbytes;
  130. }
  131. fs->flags &= ~EXT2_FLAG_BB_DIRTY;
  132. ext2fs_free_mem(&bitmap_block);
  133. return 0;
  134. }
  135. static errcode_t read_bitmaps(ext2_filsys fs, int do_inode, int do_block)
  136. {
  137. dgrp_t i;
  138. char *block_bitmap = 0, *inode_bitmap = 0;
  139. char *buf;
  140. errcode_t retval;
  141. int block_nbytes = (int) EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
  142. int inode_nbytes = (int) EXT2_INODES_PER_GROUP(fs->super) / 8;
  143. blk_t blk;
  144. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  145. fs->write_bitmaps = ext2fs_write_bitmaps;
  146. retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
  147. if (retval)
  148. return retval;
  149. if (do_block) {
  150. if (fs->block_map)
  151. ext2fs_free_block_bitmap(fs->block_map);
  152. sprintf(buf, "block bitmap for %s", fs->device_name);
  153. retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
  154. if (retval)
  155. goto cleanup;
  156. block_bitmap = fs->block_map->bitmap;
  157. }
  158. if (do_inode) {
  159. if (fs->inode_map)
  160. ext2fs_free_inode_bitmap(fs->inode_map);
  161. sprintf(buf, "inode bitmap for %s", fs->device_name);
  162. retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
  163. if (retval)
  164. goto cleanup;
  165. inode_bitmap = fs->inode_map->bitmap;
  166. }
  167. ext2fs_free_mem(&buf);
  168. if (fs->flags & EXT2_FLAG_IMAGE_FILE) {
  169. if (inode_bitmap) {
  170. blk = (fs->image_header->offset_inodemap /
  171. fs->blocksize);
  172. retval = io_channel_read_blk(fs->image_io, blk,
  173. -(inode_nbytes * fs->group_desc_count),
  174. inode_bitmap);
  175. if (retval)
  176. goto cleanup;
  177. }
  178. if (block_bitmap) {
  179. blk = (fs->image_header->offset_blockmap /
  180. fs->blocksize);
  181. retval = io_channel_read_blk(fs->image_io, blk,
  182. -(block_nbytes * fs->group_desc_count),
  183. block_bitmap);
  184. if (retval)
  185. goto cleanup;
  186. }
  187. return 0;
  188. }
  189. for (i = 0; i < fs->group_desc_count; i++) {
  190. if (block_bitmap) {
  191. blk = fs->group_desc[i].bg_block_bitmap;
  192. if (blk) {
  193. retval = io_channel_read_blk(fs->io, blk,
  194. -block_nbytes, block_bitmap);
  195. if (retval) {
  196. retval = EXT2_ET_BLOCK_BITMAP_READ;
  197. goto cleanup;
  198. }
  199. #ifdef EXT2_BIG_ENDIAN_BITMAPS
  200. if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  201. (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
  202. ext2fs_swap_bitmap(fs, block_bitmap, block_nbytes);
  203. #endif
  204. } else
  205. memset(block_bitmap, 0, block_nbytes);
  206. block_bitmap += block_nbytes;
  207. }
  208. if (inode_bitmap) {
  209. blk = fs->group_desc[i].bg_inode_bitmap;
  210. if (blk) {
  211. retval = io_channel_read_blk(fs->io, blk,
  212. -inode_nbytes, inode_bitmap);
  213. if (retval) {
  214. retval = EXT2_ET_INODE_BITMAP_READ;
  215. goto cleanup;
  216. }
  217. #ifdef EXT2_BIG_ENDIAN_BITMAPS
  218. if (!((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
  219. (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)))
  220. ext2fs_swap_bitmap(fs, inode_bitmap, inode_nbytes);
  221. #endif
  222. } else
  223. memset(inode_bitmap, 0, inode_nbytes);
  224. inode_bitmap += inode_nbytes;
  225. }
  226. }
  227. return 0;
  228. cleanup:
  229. if (do_block) {
  230. ext2fs_free_mem(&fs->block_map);
  231. fs->block_map = 0;
  232. }
  233. if (do_inode) {
  234. ext2fs_free_mem(&fs->inode_map);
  235. fs->inode_map = 0;
  236. }
  237. if (buf)
  238. ext2fs_free_mem(&buf);
  239. return retval;
  240. }
  241. errcode_t ext2fs_read_inode_bitmap (ext2_filsys fs)
  242. {
  243. return read_bitmaps(fs, 1, 0);
  244. }
  245. errcode_t ext2fs_read_block_bitmap(ext2_filsys fs)
  246. {
  247. return read_bitmaps(fs, 0, 1);
  248. }
  249. errcode_t ext2fs_read_bitmaps(ext2_filsys fs)
  250. {
  251. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  252. if (fs->inode_map && fs->block_map)
  253. return 0;
  254. return read_bitmaps(fs, !fs->inode_map, !fs->block_map);
  255. }
  256. errcode_t ext2fs_write_bitmaps(ext2_filsys fs)
  257. {
  258. errcode_t retval;
  259. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  260. if (fs->block_map && ext2fs_test_bb_dirty(fs)) {
  261. retval = ext2fs_write_block_bitmap(fs);
  262. if (retval)
  263. return retval;
  264. }
  265. if (fs->inode_map && ext2fs_test_ib_dirty(fs)) {
  266. retval = ext2fs_write_inode_bitmap(fs);
  267. if (retval)
  268. return retval;
  269. }
  270. return 0;
  271. }