resize.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 *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) - (SB_BMAP_NR(s) - 1) * s->s_blocksize * 8;
  54. /* count bitmap blocks in new fs */
  55. bmap_nr_new = block_count_new / (s->s_blocksize * 8);
  56. block_r_new = block_count_new - bmap_nr_new * s->s_blocksize * 8;
  57. if (block_r_new)
  58. bmap_nr_new++;
  59. else
  60. block_r_new = s->s_blocksize * 8;
  61. /* save old values */
  62. block_count = SB_BLOCK_COUNT(s);
  63. bmap_nr = SB_BMAP_NR(s);
  64. /* resizing of reiserfs bitmaps (journal and real), if needed */
  65. if (bmap_nr_new > bmap_nr) {
  66. /* reallocate journal bitmaps */
  67. if (reiserfs_allocate_list_bitmaps(s, jbitmap, bmap_nr_new) < 0) {
  68. printk
  69. ("reiserfs_resize: unable to allocate memory for journal bitmaps\n");
  70. unlock_super(s);
  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. vmalloc(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. memset(bitmap, 0,
  106. sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
  107. for (i = 0; i < bmap_nr; i++)
  108. bitmap[i] = old_bitmap[i];
  109. /* This doesn't go through the journal, but it doesn't have to.
  110. * The changes are still atomic: We're synced up when the journal
  111. * transaction begins, and the new bitmaps don't matter if the
  112. * transaction fails. */
  113. for (i = bmap_nr; i < bmap_nr_new; i++) {
  114. bitmap[i].bh = sb_getblk(s, i * s->s_blocksize * 8);
  115. memset(bitmap[i].bh->b_data, 0, sb_blocksize(sb));
  116. reiserfs_test_and_set_le_bit(0, bitmap[i].bh->b_data);
  117. set_buffer_uptodate(bitmap[i].bh);
  118. mark_buffer_dirty(bitmap[i].bh);
  119. sync_dirty_buffer(bitmap[i].bh);
  120. // update bitmap_info stuff
  121. bitmap[i].first_zero_hint = 1;
  122. bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
  123. }
  124. /* free old bitmap blocks array */
  125. SB_AP_BITMAP(s) = bitmap;
  126. vfree(old_bitmap);
  127. }
  128. /* begin transaction, if there was an error, it's fine. Yes, we have
  129. * incorrect bitmaps now, but none of it is ever going to touch the
  130. * disk anyway. */
  131. err = journal_begin(&th, s, 10);
  132. if (err)
  133. return err;
  134. /* correct last bitmap blocks in old and new disk layout */
  135. reiserfs_prepare_for_journal(s, SB_AP_BITMAP(s)[bmap_nr - 1].bh, 1);
  136. for (i = block_r; i < s->s_blocksize * 8; i++)
  137. reiserfs_test_and_clear_le_bit(i,
  138. SB_AP_BITMAP(s)[bmap_nr -
  139. 1].bh->b_data);
  140. SB_AP_BITMAP(s)[bmap_nr - 1].free_count += s->s_blocksize * 8 - block_r;
  141. if (!SB_AP_BITMAP(s)[bmap_nr - 1].first_zero_hint)
  142. SB_AP_BITMAP(s)[bmap_nr - 1].first_zero_hint = block_r;
  143. journal_mark_dirty(&th, s, SB_AP_BITMAP(s)[bmap_nr - 1].bh);
  144. reiserfs_prepare_for_journal(s, SB_AP_BITMAP(s)[bmap_nr_new - 1].bh, 1);
  145. for (i = block_r_new; i < s->s_blocksize * 8; i++)
  146. reiserfs_test_and_set_le_bit(i,
  147. SB_AP_BITMAP(s)[bmap_nr_new -
  148. 1].bh->b_data);
  149. journal_mark_dirty(&th, s, SB_AP_BITMAP(s)[bmap_nr_new - 1].bh);
  150. SB_AP_BITMAP(s)[bmap_nr_new - 1].free_count -=
  151. s->s_blocksize * 8 - block_r_new;
  152. /* Extreme case where last bitmap is the only valid block in itself. */
  153. if (!SB_AP_BITMAP(s)[bmap_nr_new - 1].free_count)
  154. SB_AP_BITMAP(s)[bmap_nr_new - 1].first_zero_hint = 0;
  155. /* update super */
  156. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  157. free_blocks = SB_FREE_BLOCKS(s);
  158. PUT_SB_FREE_BLOCKS(s,
  159. free_blocks + (block_count_new - block_count -
  160. (bmap_nr_new - bmap_nr)));
  161. PUT_SB_BLOCK_COUNT(s, block_count_new);
  162. PUT_SB_BMAP_NR(s, bmap_nr_new);
  163. s->s_dirt = 1;
  164. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  165. SB_JOURNAL(s)->j_must_wait = 1;
  166. return journal_end(&th, s, 10);
  167. }