3
0

res_gdt.c 5.7 KB

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