resize.c 6.2 KB

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