resize.c 6.1 KB

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