resize.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. /*
  5. * Written by Alexander Zarochentcev.
  6. *
  7. * The kernel part of the (on-line) reiserfs resizer.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/string.h>
  13. #include <linux/errno.h>
  14. #include "reiserfs.h"
  15. #include <linux/buffer_head.h>
  16. int reiserfs_resize(struct super_block *s, unsigned long block_count_new)
  17. {
  18. int err = 0;
  19. struct reiserfs_super_block *sb;
  20. struct reiserfs_bitmap_info *bitmap;
  21. struct reiserfs_bitmap_info *info;
  22. struct reiserfs_bitmap_info *old_bitmap = SB_AP_BITMAP(s);
  23. struct buffer_head *bh;
  24. struct reiserfs_transaction_handle th;
  25. unsigned int bmap_nr_new, bmap_nr;
  26. unsigned int block_r_new, block_r;
  27. struct reiserfs_list_bitmap *jb;
  28. struct reiserfs_list_bitmap jbitmap[JOURNAL_NUM_BITMAPS];
  29. unsigned long int block_count, free_blocks;
  30. int i;
  31. int copy_size;
  32. int depth;
  33. sb = SB_DISK_SUPER_BLOCK(s);
  34. if (SB_BLOCK_COUNT(s) >= block_count_new) {
  35. printk("can\'t shrink filesystem on-line\n");
  36. return -EINVAL;
  37. }
  38. /* check the device size */
  39. depth = reiserfs_write_unlock_nested(s);
  40. bh = sb_bread(s, block_count_new - 1);
  41. reiserfs_write_lock_nested(s, depth);
  42. if (!bh) {
  43. printk("reiserfs_resize: can\'t read last block\n");
  44. return -EINVAL;
  45. }
  46. bforget(bh);
  47. /*
  48. * old disk layout detection; those partitions can be mounted, but
  49. * cannot be resized
  50. */
  51. if (SB_BUFFER_WITH_SB(s)->b_blocknr * SB_BUFFER_WITH_SB(s)->b_size
  52. != REISERFS_DISK_OFFSET_IN_BYTES) {
  53. printk
  54. ("reiserfs_resize: unable to resize a reiserfs without distributed bitmap (fs version < 3.5.12)\n");
  55. return -ENOTSUPP;
  56. }
  57. /* count used bits in last bitmap block */
  58. block_r = SB_BLOCK_COUNT(s) -
  59. (reiserfs_bmap_count(s) - 1) * s->s_blocksize * 8;
  60. /* count bitmap blocks in new fs */
  61. bmap_nr_new = block_count_new / (s->s_blocksize * 8);
  62. block_r_new = block_count_new - bmap_nr_new * s->s_blocksize * 8;
  63. if (block_r_new)
  64. bmap_nr_new++;
  65. else
  66. block_r_new = s->s_blocksize * 8;
  67. /* save old values */
  68. block_count = SB_BLOCK_COUNT(s);
  69. bmap_nr = reiserfs_bmap_count(s);
  70. /* resizing of reiserfs bitmaps (journal and real), if needed */
  71. if (bmap_nr_new > bmap_nr) {
  72. /* reallocate journal bitmaps */
  73. if (reiserfs_allocate_list_bitmaps(s, jbitmap, bmap_nr_new) < 0) {
  74. printk
  75. ("reiserfs_resize: unable to allocate memory for journal bitmaps\n");
  76. return -ENOMEM;
  77. }
  78. /*
  79. * the new journal bitmaps are zero filled, now we copy i
  80. * the bitmap node pointers from the old journal bitmap
  81. * structs, and then transfer the new data structures
  82. * into the journal struct.
  83. *
  84. * using the copy_size var below allows this code to work for
  85. * both shrinking and expanding the FS.
  86. */
  87. copy_size = bmap_nr_new < bmap_nr ? bmap_nr_new : bmap_nr;
  88. copy_size =
  89. copy_size * sizeof(struct reiserfs_list_bitmap_node *);
  90. for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
  91. struct reiserfs_bitmap_node **node_tmp;
  92. jb = SB_JOURNAL(s)->j_list_bitmap + i;
  93. memcpy(jbitmap[i].bitmaps, jb->bitmaps, copy_size);
  94. /*
  95. * just in case vfree schedules on us, copy the new
  96. * pointer into the journal struct before freeing the
  97. * old one
  98. */
  99. node_tmp = jb->bitmaps;
  100. jb->bitmaps = jbitmap[i].bitmaps;
  101. vfree(node_tmp);
  102. }
  103. /*
  104. * allocate additional bitmap blocks, reallocate
  105. * array of bitmap block pointers
  106. */
  107. bitmap =
  108. vzalloc(sizeof(struct reiserfs_bitmap_info) * bmap_nr_new);
  109. if (!bitmap) {
  110. /*
  111. * Journal bitmaps are still supersized, but the
  112. * memory isn't leaked, so I guess it's ok
  113. */
  114. printk("reiserfs_resize: unable to allocate memory.\n");
  115. return -ENOMEM;
  116. }
  117. for (i = 0; i < bmap_nr; i++)
  118. bitmap[i] = old_bitmap[i];
  119. /*
  120. * This doesn't go through the journal, but it doesn't have to.
  121. * The changes are still atomic: We're synced up when the
  122. * journal transaction begins, and the new bitmaps don't
  123. * matter if the transaction fails.
  124. */
  125. for (i = bmap_nr; i < bmap_nr_new; i++) {
  126. int depth;
  127. /*
  128. * don't use read_bitmap_block since it will cache
  129. * the uninitialized bitmap
  130. */
  131. depth = reiserfs_write_unlock_nested(s);
  132. bh = sb_bread(s, i * s->s_blocksize * 8);
  133. reiserfs_write_lock_nested(s, depth);
  134. if (!bh) {
  135. vfree(bitmap);
  136. return -EIO;
  137. }
  138. memset(bh->b_data, 0, sb_blocksize(sb));
  139. reiserfs_set_le_bit(0, bh->b_data);
  140. reiserfs_cache_bitmap_metadata(s, bh, bitmap + i);
  141. set_buffer_uptodate(bh);
  142. mark_buffer_dirty(bh);
  143. depth = reiserfs_write_unlock_nested(s);
  144. sync_dirty_buffer(bh);
  145. reiserfs_write_lock_nested(s, depth);
  146. /* update bitmap_info stuff */
  147. bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
  148. brelse(bh);
  149. }
  150. /* free old bitmap blocks array */
  151. SB_AP_BITMAP(s) = bitmap;
  152. vfree(old_bitmap);
  153. }
  154. /*
  155. * begin transaction, if there was an error, it's fine. Yes, we have
  156. * incorrect bitmaps now, but none of it is ever going to touch the
  157. * disk anyway.
  158. */
  159. err = journal_begin(&th, s, 10);
  160. if (err)
  161. return err;
  162. /* Extend old last bitmap block - new blocks have been made available */
  163. info = SB_AP_BITMAP(s) + bmap_nr - 1;
  164. bh = reiserfs_read_bitmap_block(s, bmap_nr - 1);
  165. if (!bh) {
  166. int jerr = journal_end(&th);
  167. if (jerr)
  168. return jerr;
  169. return -EIO;
  170. }
  171. reiserfs_prepare_for_journal(s, bh, 1);
  172. for (i = block_r; i < s->s_blocksize * 8; i++)
  173. reiserfs_clear_le_bit(i, bh->b_data);
  174. info->free_count += s->s_blocksize * 8 - block_r;
  175. journal_mark_dirty(&th, bh);
  176. brelse(bh);
  177. /* Correct new last bitmap block - It may not be full */
  178. info = SB_AP_BITMAP(s) + bmap_nr_new - 1;
  179. bh = reiserfs_read_bitmap_block(s, bmap_nr_new - 1);
  180. if (!bh) {
  181. int jerr = journal_end(&th);
  182. if (jerr)
  183. return jerr;
  184. return -EIO;
  185. }
  186. reiserfs_prepare_for_journal(s, bh, 1);
  187. for (i = block_r_new; i < s->s_blocksize * 8; i++)
  188. reiserfs_set_le_bit(i, bh->b_data);
  189. journal_mark_dirty(&th, bh);
  190. brelse(bh);
  191. info->free_count -= s->s_blocksize * 8 - block_r_new;
  192. /* update super */
  193. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  194. free_blocks = SB_FREE_BLOCKS(s);
  195. PUT_SB_FREE_BLOCKS(s,
  196. free_blocks + (block_count_new - block_count -
  197. (bmap_nr_new - bmap_nr)));
  198. PUT_SB_BLOCK_COUNT(s, block_count_new);
  199. PUT_SB_BMAP_NR(s, bmap_would_wrap(bmap_nr_new) ? : bmap_nr_new);
  200. journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
  201. SB_JOURNAL(s)->j_must_wait = 1;
  202. return journal_end(&th);
  203. }