resize.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /* old disk layout detection; those partitions can be mounted, but
  48. * cannot be resized */
  49. if (SB_BUFFER_WITH_SB(s)->b_blocknr * SB_BUFFER_WITH_SB(s)->b_size
  50. != REISERFS_DISK_OFFSET_IN_BYTES) {
  51. printk
  52. ("reiserfs_resize: unable to resize a reiserfs without distributed bitmap (fs version < 3.5.12)\n");
  53. return -ENOTSUPP;
  54. }
  55. /* count used bits in last bitmap block */
  56. block_r = SB_BLOCK_COUNT(s) -
  57. (reiserfs_bmap_count(s) - 1) * s->s_blocksize * 8;
  58. /* count bitmap blocks in new fs */
  59. bmap_nr_new = block_count_new / (s->s_blocksize * 8);
  60. block_r_new = block_count_new - bmap_nr_new * s->s_blocksize * 8;
  61. if (block_r_new)
  62. bmap_nr_new++;
  63. else
  64. block_r_new = s->s_blocksize * 8;
  65. /* save old values */
  66. block_count = SB_BLOCK_COUNT(s);
  67. bmap_nr = reiserfs_bmap_count(s);
  68. /* resizing of reiserfs bitmaps (journal and real), if needed */
  69. if (bmap_nr_new > bmap_nr) {
  70. /* reallocate journal bitmaps */
  71. if (reiserfs_allocate_list_bitmaps(s, jbitmap, bmap_nr_new) < 0) {
  72. printk
  73. ("reiserfs_resize: unable to allocate memory for journal bitmaps\n");
  74. return -ENOMEM;
  75. }
  76. /* the new journal bitmaps are zero filled, now we copy in the bitmap
  77. ** node pointers from the old journal bitmap structs, and then
  78. ** transfer the new data structures into the journal struct.
  79. **
  80. ** using the copy_size var below allows this code to work for
  81. ** both shrinking and expanding the FS.
  82. */
  83. copy_size = bmap_nr_new < bmap_nr ? bmap_nr_new : bmap_nr;
  84. copy_size =
  85. copy_size * sizeof(struct reiserfs_list_bitmap_node *);
  86. for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
  87. struct reiserfs_bitmap_node **node_tmp;
  88. jb = SB_JOURNAL(s)->j_list_bitmap + i;
  89. memcpy(jbitmap[i].bitmaps, jb->bitmaps, copy_size);
  90. /* just in case vfree schedules on us, copy the new
  91. ** pointer into the journal struct before freeing the
  92. ** old one
  93. */
  94. node_tmp = jb->bitmaps;
  95. jb->bitmaps = jbitmap[i].bitmaps;
  96. vfree(node_tmp);
  97. }
  98. /* allocate additional bitmap blocks, reallocate array of bitmap
  99. * block pointers */
  100. bitmap =
  101. vzalloc(sizeof(struct reiserfs_bitmap_info) * bmap_nr_new);
  102. if (!bitmap) {
  103. /* Journal bitmaps are still supersized, but the memory isn't
  104. * leaked, so I guess it's ok */
  105. printk("reiserfs_resize: unable to allocate memory.\n");
  106. return -ENOMEM;
  107. }
  108. for (i = 0; i < bmap_nr; i++)
  109. bitmap[i] = old_bitmap[i];
  110. /* This doesn't go through the journal, but it doesn't have to.
  111. * The changes are still atomic: We're synced up when the journal
  112. * transaction begins, and the new bitmaps don't matter if the
  113. * transaction fails. */
  114. for (i = bmap_nr; i < bmap_nr_new; i++) {
  115. int depth;
  116. /* don't use read_bitmap_block since it will cache
  117. * the uninitialized bitmap */
  118. depth = reiserfs_write_unlock_nested(s);
  119. bh = sb_bread(s, i * s->s_blocksize * 8);
  120. reiserfs_write_lock_nested(s, depth);
  121. if (!bh) {
  122. vfree(bitmap);
  123. return -EIO;
  124. }
  125. memset(bh->b_data, 0, sb_blocksize(sb));
  126. reiserfs_set_le_bit(0, bh->b_data);
  127. reiserfs_cache_bitmap_metadata(s, bh, bitmap + i);
  128. set_buffer_uptodate(bh);
  129. mark_buffer_dirty(bh);
  130. depth = reiserfs_write_unlock_nested(s);
  131. sync_dirty_buffer(bh);
  132. reiserfs_write_lock_nested(s, depth);
  133. // update bitmap_info stuff
  134. bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
  135. brelse(bh);
  136. }
  137. /* free old bitmap blocks array */
  138. SB_AP_BITMAP(s) = bitmap;
  139. vfree(old_bitmap);
  140. }
  141. /* begin transaction, if there was an error, it's fine. Yes, we have
  142. * incorrect bitmaps now, but none of it is ever going to touch the
  143. * disk anyway. */
  144. err = journal_begin(&th, s, 10);
  145. if (err)
  146. return err;
  147. /* Extend old last bitmap block - new blocks have been made available */
  148. info = SB_AP_BITMAP(s) + bmap_nr - 1;
  149. bh = reiserfs_read_bitmap_block(s, bmap_nr - 1);
  150. if (!bh) {
  151. int jerr = journal_end(&th, s, 10);
  152. if (jerr)
  153. return jerr;
  154. return -EIO;
  155. }
  156. reiserfs_prepare_for_journal(s, bh, 1);
  157. for (i = block_r; i < s->s_blocksize * 8; i++)
  158. reiserfs_clear_le_bit(i, bh->b_data);
  159. info->free_count += s->s_blocksize * 8 - block_r;
  160. journal_mark_dirty(&th, s, bh);
  161. brelse(bh);
  162. /* Correct new last bitmap block - It may not be full */
  163. info = SB_AP_BITMAP(s) + bmap_nr_new - 1;
  164. bh = reiserfs_read_bitmap_block(s, bmap_nr_new - 1);
  165. if (!bh) {
  166. int jerr = journal_end(&th, s, 10);
  167. if (jerr)
  168. return jerr;
  169. return -EIO;
  170. }
  171. reiserfs_prepare_for_journal(s, bh, 1);
  172. for (i = block_r_new; i < s->s_blocksize * 8; i++)
  173. reiserfs_set_le_bit(i, bh->b_data);
  174. journal_mark_dirty(&th, s, bh);
  175. brelse(bh);
  176. info->free_count -= s->s_blocksize * 8 - block_r_new;
  177. /* update super */
  178. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  179. free_blocks = SB_FREE_BLOCKS(s);
  180. PUT_SB_FREE_BLOCKS(s,
  181. free_blocks + (block_count_new - block_count -
  182. (bmap_nr_new - bmap_nr)));
  183. PUT_SB_BLOCK_COUNT(s, block_count_new);
  184. PUT_SB_BMAP_NR(s, bmap_would_wrap(bmap_nr_new) ? : bmap_nr_new);
  185. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  186. SB_JOURNAL(s)->j_must_wait = 1;
  187. return journal_end(&th, s, 10);
  188. }