res_gdt.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * res_gdt.c --- reserve blocks for growing the group descriptor table
  4. * during online resizing.
  5. *
  6. * Copyright (C) 2002 Andreas Dilger
  7. *
  8. * %Begin-Header%
  9. * This file may be redistributed under the terms of the GNU Public
  10. * License.
  11. * %End-Header%
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include "ext2_fs.h"
  17. #include "ext2fs.h"
  18. /*
  19. * Iterate through the groups which hold BACKUP superblock/GDT copies in an
  20. * ext3 filesystem. The counters should be initialized to 1, 5, and 7 before
  21. * calling this for the first time. In a sparse filesystem it will be the
  22. * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ...
  23. * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ...
  24. */
  25. static unsigned int list_backups(ext2_filsys fs, unsigned int *three,
  26. unsigned int *five, unsigned int *seven)
  27. {
  28. unsigned int *min = three;
  29. int mult = 3;
  30. unsigned int ret;
  31. if (!(fs->super->s_feature_ro_compat &
  32. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
  33. ret = *min;
  34. *min += 1;
  35. return ret;
  36. }
  37. if (*five < *min) {
  38. min = five;
  39. mult = 5;
  40. }
  41. if (*seven < *min) {
  42. min = seven;
  43. mult = 7;
  44. }
  45. ret = *min;
  46. *min *= mult;
  47. return ret;
  48. }
  49. /*
  50. * This code assumes that the reserved blocks have already been marked in-use
  51. * during ext2fs_initialize(), so that they are not allocated for other
  52. * uses before we can add them to the resize inode (which has to come
  53. * after the creation of the inode table).
  54. */
  55. errcode_t ext2fs_create_resize_inode(ext2_filsys fs)
  56. {
  57. errcode_t retval, retval2;
  58. struct ext2_super_block *sb;
  59. struct ext2_inode inode;
  60. __u32 *dindir_buf, *gdt_buf;
  61. int rsv_add;
  62. unsigned long long apb, inode_size;
  63. blk_t dindir_blk, rsv_off, gdt_off, gdt_blk;
  64. int dindir_dirty = 0, inode_dirty = 0;
  65. EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
  66. sb = fs->super;
  67. retval = ext2fs_get_mem(2 * fs->blocksize, (void *)&dindir_buf);
  68. if (retval)
  69. goto out_free;
  70. gdt_buf = (__u32 *)((char *)dindir_buf + fs->blocksize);
  71. retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
  72. if (retval)
  73. goto out_free;
  74. /* Maximum possible file size (we donly use the dindirect blocks) */
  75. apb = EXT2_ADDR_PER_BLOCK(sb);
  76. rsv_add = fs->blocksize / 512;
  77. if ((dindir_blk = inode.i_block[EXT2_DIND_BLOCK])) {
  78. #ifdef RES_GDT_DEBUG
  79. printf("reading GDT dindir %u\n", dindir_blk);
  80. #endif
  81. retval = ext2fs_read_ind_block(fs, dindir_blk, dindir_buf);
  82. if (retval)
  83. goto out_inode;
  84. } else {
  85. blk_t goal = 3 + sb->s_reserved_gdt_blocks +
  86. fs->desc_blocks + fs->inode_blocks_per_group;
  87. retval = ext2fs_alloc_block(fs, goal, 0, &dindir_blk);
  88. if (retval)
  89. goto out_free;
  90. inode.i_mode = LINUX_S_IFREG | 0600;
  91. inode.i_links_count = 1;
  92. inode.i_block[EXT2_DIND_BLOCK] = dindir_blk;
  93. inode.i_blocks = rsv_add;
  94. memset(dindir_buf, 0, fs->blocksize);
  95. #ifdef RES_GDT_DEBUG
  96. printf("allocated GDT dindir %u\n", dindir_blk);
  97. #endif
  98. dindir_dirty = inode_dirty = 1;
  99. inode_size = apb*apb + apb + EXT2_NDIR_BLOCKS;
  100. inode_size *= fs->blocksize;
  101. inode.i_size = inode_size & 0xFFFFFFFF;
  102. inode.i_size_high = (inode_size >> 32) & 0xFFFFFFFF;
  103. if (inode.i_size_high) {
  104. sb->s_feature_ro_compat |=
  105. EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
  106. }
  107. inode.i_ctime = time(NULL);
  108. }
  109. for (rsv_off = 0, gdt_off = fs->desc_blocks,
  110. gdt_blk = sb->s_first_data_block + 1 + fs->desc_blocks;
  111. rsv_off < sb->s_reserved_gdt_blocks;
  112. rsv_off++, gdt_off++, gdt_blk++) {
  113. unsigned int three = 1, five = 5, seven = 7;
  114. unsigned int grp, last = 0;
  115. int gdt_dirty = 0;
  116. gdt_off %= apb;
  117. if (!dindir_buf[gdt_off]) {
  118. /* FIXME XXX XXX
  119. blk_t new_blk;
  120. retval = ext2fs_new_block(fs, gdt_blk, 0, &new_blk);
  121. if (retval)
  122. goto out_free;
  123. if (new_blk != gdt_blk) {
  124. // XXX free block
  125. retval = -1; // XXX
  126. }
  127. */
  128. gdt_dirty = dindir_dirty = inode_dirty = 1;
  129. memset(gdt_buf, 0, fs->blocksize);
  130. dindir_buf[gdt_off] = gdt_blk;
  131. inode.i_blocks += rsv_add;
  132. #ifdef RES_GDT_DEBUG
  133. printf("added primary GDT block %u at %u[%u]\n",
  134. gdt_blk, dindir_blk, gdt_off);
  135. #endif
  136. } else if (dindir_buf[gdt_off] == gdt_blk) {
  137. #ifdef RES_GDT_DEBUG
  138. printf("reading primary GDT block %u\n", gdt_blk);
  139. #endif
  140. retval = ext2fs_read_ind_block(fs, gdt_blk, gdt_buf);
  141. if (retval)
  142. goto out_dindir;
  143. } else {
  144. #ifdef RES_GDT_DEBUG
  145. printf("bad primary GDT %u != %u at %u[%u]\n",
  146. dindir_buf[gdt_off], gdt_blk,dindir_blk,gdt_off);
  147. #endif
  148. retval = EXT2_ET_RESIZE_INODE_CORRUPT;
  149. goto out_dindir;
  150. }
  151. while ((grp = list_backups(fs, &three, &five, &seven)) <
  152. fs->group_desc_count) {
  153. blk_t expect = gdt_blk + grp * sb->s_blocks_per_group;
  154. if (!gdt_buf[last]) {
  155. #ifdef RES_GDT_DEBUG
  156. printf("added backup GDT %u grp %u@%u[%u]\n",
  157. expect, grp, gdt_blk, last);
  158. #endif
  159. gdt_buf[last] = expect;
  160. inode.i_blocks += rsv_add;
  161. gdt_dirty = inode_dirty = 1;
  162. } else if (gdt_buf[last] != expect) {
  163. #ifdef RES_GDT_DEBUG
  164. printf("bad backup GDT %u != %u at %u[%u]\n",
  165. gdt_buf[last], expect, gdt_blk, last);
  166. #endif
  167. retval = EXT2_ET_RESIZE_INODE_CORRUPT;
  168. goto out_dindir;
  169. }
  170. last++;
  171. }
  172. if (gdt_dirty) {
  173. #ifdef RES_GDT_DEBUG
  174. printf("writing primary GDT block %u\n", gdt_blk);
  175. #endif
  176. retval = ext2fs_write_ind_block(fs, gdt_blk, gdt_buf);
  177. if (retval)
  178. goto out_dindir;
  179. }
  180. }
  181. out_dindir:
  182. if (dindir_dirty) {
  183. retval2 = ext2fs_write_ind_block(fs, dindir_blk, dindir_buf);
  184. if (!retval)
  185. retval = retval2;
  186. }
  187. out_inode:
  188. #ifdef RES_GDT_DEBUG
  189. printf("inode.i_blocks = %u, i_size = %u\n", inode.i_blocks,
  190. inode.i_size);
  191. #endif
  192. if (inode_dirty) {
  193. inode.i_atime = inode.i_mtime = time(NULL);
  194. retval2 = ext2fs_write_inode(fs, EXT2_RESIZE_INO, &inode);
  195. if (!retval)
  196. retval = retval2;
  197. }
  198. out_free:
  199. ext2fs_free_mem((void *)&dindir_buf);
  200. return retval;
  201. }