closefs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * closefs.c --- close an ext2 filesystem
  4. *
  5. * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <time.h>
  17. #include <string.h>
  18. #include "ext2_fs.h"
  19. #include "ext2fsP.h"
  20. static int test_root(int a, int b)
  21. {
  22. if (a == 0)
  23. return 1;
  24. while (1) {
  25. if (a == 1)
  26. return 1;
  27. if (a % b)
  28. return 0;
  29. a = a / b;
  30. }
  31. }
  32. int ext2fs_bg_has_super(ext2_filsys fs, int group_block)
  33. {
  34. if (!(fs->super->s_feature_ro_compat &
  35. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
  36. return 1;
  37. if (test_root(group_block, 3) || (test_root(group_block, 5)) ||
  38. test_root(group_block, 7))
  39. return 1;
  40. return 0;
  41. }
  42. int ext2fs_super_and_bgd_loc(ext2_filsys fs,
  43. dgrp_t group,
  44. blk_t *ret_super_blk,
  45. blk_t *ret_old_desc_blk,
  46. blk_t *ret_new_desc_blk,
  47. int *ret_meta_bg)
  48. {
  49. blk_t group_block, super_blk = 0, old_desc_blk = 0, new_desc_blk = 0;
  50. unsigned int meta_bg, meta_bg_size;
  51. int numblocks, has_super;
  52. int old_desc_blocks;
  53. group_block = fs->super->s_first_data_block +
  54. (group * fs->super->s_blocks_per_group);
  55. if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
  56. old_desc_blocks = fs->super->s_first_meta_bg;
  57. else
  58. old_desc_blocks =
  59. fs->desc_blocks + fs->super->s_reserved_gdt_blocks;
  60. if (group == fs->group_desc_count-1) {
  61. numblocks = (fs->super->s_blocks_count -
  62. fs->super->s_first_data_block) %
  63. fs->super->s_blocks_per_group;
  64. if (!numblocks)
  65. numblocks = fs->super->s_blocks_per_group;
  66. } else
  67. numblocks = fs->super->s_blocks_per_group;
  68. has_super = ext2fs_bg_has_super(fs, group);
  69. if (has_super) {
  70. super_blk = group_block;
  71. numblocks--;
  72. }
  73. meta_bg_size = (fs->blocksize / sizeof (struct ext2_group_desc));
  74. meta_bg = group / meta_bg_size;
  75. if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
  76. (meta_bg < fs->super->s_first_meta_bg)) {
  77. if (has_super) {
  78. old_desc_blk = group_block + 1;
  79. numblocks -= old_desc_blocks;
  80. }
  81. } else {
  82. if (((group % meta_bg_size) == 0) ||
  83. ((group % meta_bg_size) == 1) ||
  84. ((group % meta_bg_size) == (meta_bg_size-1))) {
  85. if (has_super)
  86. has_super = 1;
  87. new_desc_blk = group_block + has_super;
  88. numblocks--;
  89. }
  90. }
  91. numblocks -= 2 + fs->inode_blocks_per_group;
  92. if (ret_super_blk)
  93. *ret_super_blk = super_blk;
  94. if (ret_old_desc_blk)
  95. *ret_old_desc_blk = old_desc_blk;
  96. if (ret_new_desc_blk)
  97. *ret_new_desc_blk = new_desc_blk;
  98. if (ret_meta_bg)
  99. *ret_meta_bg = meta_bg;
  100. return numblocks;
  101. }
  102. /*
  103. * This function forces out the primary superblock. We need to only
  104. * write out those fields which we have changed, since if the
  105. * filesystem is mounted, it may have changed some of the other
  106. * fields.
  107. *
  108. * It takes as input a superblock which has already been byte swapped
  109. * (if necessary).
  110. *
  111. */
  112. static errcode_t write_primary_superblock(ext2_filsys fs,
  113. struct ext2_super_block *super)
  114. {
  115. __u16 *old_super, *new_super;
  116. int check_idx, write_idx, size;
  117. errcode_t retval;
  118. if (!fs->io->manager->write_byte || !fs->orig_super) {
  119. io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
  120. retval = io_channel_write_blk(fs->io, 1, -SUPERBLOCK_SIZE,
  121. super);
  122. io_channel_set_blksize(fs->io, fs->blocksize);
  123. return retval;
  124. }
  125. old_super = (__u16 *) fs->orig_super;
  126. new_super = (__u16 *) super;
  127. for (check_idx = 0; check_idx < SUPERBLOCK_SIZE/2; check_idx++) {
  128. if (old_super[check_idx] == new_super[check_idx])
  129. continue;
  130. write_idx = check_idx;
  131. for (check_idx++; check_idx < SUPERBLOCK_SIZE/2; check_idx++)
  132. if (old_super[check_idx] == new_super[check_idx])
  133. break;
  134. size = 2 * (check_idx - write_idx);
  135. retval = io_channel_write_byte(fs->io,
  136. SUPERBLOCK_OFFSET + (2 * write_idx), size,
  137. new_super + write_idx);
  138. if (retval)
  139. return retval;
  140. }
  141. memcpy(fs->orig_super, super, SUPERBLOCK_SIZE);
  142. return 0;
  143. }
  144. /*
  145. * Updates the revision to EXT2_DYNAMIC_REV
  146. */
  147. void ext2fs_update_dynamic_rev(ext2_filsys fs)
  148. {
  149. struct ext2_super_block *sb = fs->super;
  150. if (sb->s_rev_level > EXT2_GOOD_OLD_REV)
  151. return;
  152. sb->s_rev_level = EXT2_DYNAMIC_REV;
  153. sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
  154. sb->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
  155. /* s_uuid is handled by e2fsck already */
  156. /* other fields should be left alone */
  157. }
  158. static errcode_t write_backup_super(ext2_filsys fs, dgrp_t group,
  159. blk_t group_block,
  160. struct ext2_super_block *super_shadow)
  161. {
  162. dgrp_t sgrp = group;
  163. if (sgrp > ((1 << 16) - 1))
  164. sgrp = (1 << 16) - 1;
  165. #if BB_BIG_ENDIAN
  166. if (fs->flags & EXT2_FLAG_SWAP_BYTES)
  167. super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
  168. else
  169. #endif
  170. fs->super->s_block_group_nr = sgrp;
  171. return io_channel_write_blk(fs->io, group_block, -SUPERBLOCK_SIZE,
  172. super_shadow);
  173. }
  174. errcode_t ext2fs_flush(ext2_filsys fs)
  175. {
  176. dgrp_t i;
  177. blk_t group_block;
  178. errcode_t retval;
  179. unsigned long fs_state;
  180. struct ext2_super_block *super_shadow = 0;
  181. struct ext2_group_desc *group_shadow = 0;
  182. char *group_ptr;
  183. int old_desc_blocks;
  184. #if BB_BIG_ENDIAN
  185. dgrp_t j;
  186. struct ext2_group_desc *s, *t;
  187. #endif
  188. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  189. fs_state = fs->super->s_state;
  190. fs->super->s_wtime = time(NULL);
  191. fs->super->s_block_group_nr = 0;
  192. #if BB_BIG_ENDIAN
  193. if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
  194. retval = EXT2_ET_NO_MEMORY;
  195. retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super_shadow);
  196. if (retval)
  197. goto errout;
  198. retval = ext2fs_get_mem((size_t)(fs->blocksize *
  199. fs->desc_blocks),
  200. &group_shadow);
  201. if (retval)
  202. goto errout;
  203. memset(group_shadow, 0, (size_t) fs->blocksize *
  204. fs->desc_blocks);
  205. /* swap the group descriptors */
  206. for (j=0, s=fs->group_desc, t=group_shadow;
  207. j < fs->group_desc_count; j++, t++, s++) {
  208. *t = *s;
  209. ext2fs_swap_group_desc(t);
  210. }
  211. } else {
  212. super_shadow = fs->super;
  213. group_shadow = fs->group_desc;
  214. }
  215. #else
  216. super_shadow = fs->super;
  217. group_shadow = fs->group_desc;
  218. #endif
  219. /*
  220. * If this is an external journal device, don't write out the
  221. * block group descriptors or any of the backup superblocks
  222. */
  223. if (fs->super->s_feature_incompat &
  224. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)
  225. goto write_primary_superblock_only;
  226. /*
  227. * Set the state of the FS to be non-valid. (The state has
  228. * already been backed up earlier, and will be restored after
  229. * we write out the backup superblocks.)
  230. */
  231. fs->super->s_state &= ~EXT2_VALID_FS;
  232. #if BB_BIG_ENDIAN
  233. if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
  234. *super_shadow = *fs->super;
  235. ext2fs_swap_super(super_shadow);
  236. }
  237. #endif
  238. /*
  239. * Write out the master group descriptors, and the backup
  240. * superblocks and group descriptors.
  241. */
  242. group_block = fs->super->s_first_data_block;
  243. group_ptr = (char *) group_shadow;
  244. if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG)
  245. old_desc_blocks = fs->super->s_first_meta_bg;
  246. else
  247. old_desc_blocks = fs->desc_blocks;
  248. for (i = 0; i < fs->group_desc_count; i++) {
  249. blk_t super_blk, old_desc_blk, new_desc_blk;
  250. int meta_bg;
  251. ext2fs_super_and_bgd_loc(fs, i, &super_blk, &old_desc_blk,
  252. &new_desc_blk, &meta_bg);
  253. if (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) &&i && super_blk) {
  254. retval = write_backup_super(fs, i, super_blk,
  255. super_shadow);
  256. if (retval)
  257. goto errout;
  258. }
  259. if (fs->flags & EXT2_FLAG_SUPER_ONLY)
  260. continue;
  261. if ((old_desc_blk) &&
  262. (!(fs->flags & EXT2_FLAG_MASTER_SB_ONLY) || (i == 0))) {
  263. retval = io_channel_write_blk(fs->io,
  264. old_desc_blk, old_desc_blocks, group_ptr);
  265. if (retval)
  266. goto errout;
  267. }
  268. if (new_desc_blk) {
  269. retval = io_channel_write_blk(fs->io, new_desc_blk,
  270. 1, group_ptr + (meta_bg*fs->blocksize));
  271. if (retval)
  272. goto errout;
  273. }
  274. }
  275. fs->super->s_block_group_nr = 0;
  276. fs->super->s_state = fs_state;
  277. #if BB_BIG_ENDIAN
  278. if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
  279. *super_shadow = *fs->super;
  280. ext2fs_swap_super(super_shadow);
  281. }
  282. #endif
  283. /*
  284. * If the write_bitmaps() function is present, call it to
  285. * flush the bitmaps. This is done this way so that a simple
  286. * program that doesn't mess with the bitmaps doesn't need to
  287. * drag in the bitmaps.c code.
  288. */
  289. if (fs->write_bitmaps) {
  290. retval = fs->write_bitmaps(fs);
  291. if (retval)
  292. goto errout;
  293. }
  294. write_primary_superblock_only:
  295. /*
  296. * Write out master superblock. This has to be done
  297. * separately, since it is located at a fixed location
  298. * (SUPERBLOCK_OFFSET). We flush all other pending changes
  299. * out to disk first, just to avoid a race condition with an
  300. * insy-tinsy window....
  301. */
  302. retval = io_channel_flush(fs->io);
  303. retval = write_primary_superblock(fs, super_shadow);
  304. if (retval)
  305. goto errout;
  306. fs->flags &= ~EXT2_FLAG_DIRTY;
  307. retval = io_channel_flush(fs->io);
  308. errout:
  309. fs->super->s_state = fs_state;
  310. if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
  311. if (super_shadow)
  312. ext2fs_free_mem(&super_shadow);
  313. if (group_shadow)
  314. ext2fs_free_mem(&group_shadow);
  315. }
  316. return retval;
  317. }
  318. errcode_t ext2fs_close(ext2_filsys fs)
  319. {
  320. errcode_t retval;
  321. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  322. if (fs->flags & EXT2_FLAG_DIRTY) {
  323. retval = ext2fs_flush(fs);
  324. if (retval)
  325. return retval;
  326. }
  327. if (fs->write_bitmaps) {
  328. retval = fs->write_bitmaps(fs);
  329. if (retval)
  330. return retval;
  331. }
  332. ext2fs_free(fs);
  333. return 0;
  334. }