resize.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(array_size(bmap_nr_new,
  109. sizeof(struct reiserfs_bitmap_info)));
  110. if (!bitmap) {
  111. /*
  112. * Journal bitmaps are still supersized, but the
  113. * memory isn't leaked, so I guess it's ok
  114. */
  115. printk("reiserfs_resize: unable to allocate memory.\n");
  116. return -ENOMEM;
  117. }
  118. for (i = 0; i < bmap_nr; i++)
  119. bitmap[i] = old_bitmap[i];
  120. /*
  121. * This doesn't go through the journal, but it doesn't have to.
  122. * The changes are still atomic: We're synced up when the
  123. * journal transaction begins, and the new bitmaps don't
  124. * matter if the transaction fails.
  125. */
  126. for (i = bmap_nr; i < bmap_nr_new; i++) {
  127. int depth;
  128. /*
  129. * don't use read_bitmap_block since it will cache
  130. * the uninitialized bitmap
  131. */
  132. depth = reiserfs_write_unlock_nested(s);
  133. bh = sb_bread(s, i * s->s_blocksize * 8);
  134. reiserfs_write_lock_nested(s, depth);
  135. if (!bh) {
  136. vfree(bitmap);
  137. return -EIO;
  138. }
  139. memset(bh->b_data, 0, sb_blocksize(sb));
  140. reiserfs_set_le_bit(0, bh->b_data);
  141. reiserfs_cache_bitmap_metadata(s, bh, bitmap + i);
  142. set_buffer_uptodate(bh);
  143. mark_buffer_dirty(bh);
  144. depth = reiserfs_write_unlock_nested(s);
  145. sync_dirty_buffer(bh);
  146. reiserfs_write_lock_nested(s, depth);
  147. /* update bitmap_info stuff */
  148. bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
  149. brelse(bh);
  150. }
  151. /* free old bitmap blocks array */
  152. SB_AP_BITMAP(s) = bitmap;
  153. vfree(old_bitmap);
  154. }
  155. /*
  156. * begin transaction, if there was an error, it's fine. Yes, we have
  157. * incorrect bitmaps now, but none of it is ever going to touch the
  158. * disk anyway.
  159. */
  160. err = journal_begin(&th, s, 10);
  161. if (err)
  162. return err;
  163. /* Extend old last bitmap block - new blocks have been made available */
  164. info = SB_AP_BITMAP(s) + bmap_nr - 1;
  165. bh = reiserfs_read_bitmap_block(s, bmap_nr - 1);
  166. if (!bh) {
  167. int jerr = journal_end(&th);
  168. if (jerr)
  169. return jerr;
  170. return -EIO;
  171. }
  172. reiserfs_prepare_for_journal(s, bh, 1);
  173. for (i = block_r; i < s->s_blocksize * 8; i++)
  174. reiserfs_clear_le_bit(i, bh->b_data);
  175. info->free_count += s->s_blocksize * 8 - block_r;
  176. journal_mark_dirty(&th, bh);
  177. brelse(bh);
  178. /* Correct new last bitmap block - It may not be full */
  179. info = SB_AP_BITMAP(s) + bmap_nr_new - 1;
  180. bh = reiserfs_read_bitmap_block(s, bmap_nr_new - 1);
  181. if (!bh) {
  182. int jerr = journal_end(&th);
  183. if (jerr)
  184. return jerr;
  185. return -EIO;
  186. }
  187. reiserfs_prepare_for_journal(s, bh, 1);
  188. for (i = block_r_new; i < s->s_blocksize * 8; i++)
  189. reiserfs_set_le_bit(i, bh->b_data);
  190. journal_mark_dirty(&th, bh);
  191. brelse(bh);
  192. info->free_count -= s->s_blocksize * 8 - block_r_new;
  193. /* update super */
  194. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  195. free_blocks = SB_FREE_BLOCKS(s);
  196. PUT_SB_FREE_BLOCKS(s,
  197. free_blocks + (block_count_new - block_count -
  198. (bmap_nr_new - bmap_nr)));
  199. PUT_SB_BLOCK_COUNT(s, block_count_new);
  200. PUT_SB_BMAP_NR(s, bmap_would_wrap(bmap_nr_new) ? : bmap_nr_new);
  201. journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
  202. SB_JOURNAL(s)->j_must_wait = 1;
  203. return journal_end(&th);
  204. }