resize.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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) - (SB_BMAP_NR(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 = SB_BMAP_NR(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. unlock_super(s);
  72. return -ENOMEM;
  73. }
  74. /* the new journal bitmaps are zero filled, now we copy in the bitmap
  75. ** node pointers from the old journal bitmap structs, and then
  76. ** transfer the new data structures into the journal struct.
  77. **
  78. ** using the copy_size var below allows this code to work for
  79. ** both shrinking and expanding the FS.
  80. */
  81. copy_size = bmap_nr_new < bmap_nr ? bmap_nr_new : bmap_nr;
  82. copy_size =
  83. copy_size * sizeof(struct reiserfs_list_bitmap_node *);
  84. for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
  85. struct reiserfs_bitmap_node **node_tmp;
  86. jb = SB_JOURNAL(s)->j_list_bitmap + i;
  87. memcpy(jbitmap[i].bitmaps, jb->bitmaps, copy_size);
  88. /* just in case vfree schedules on us, copy the new
  89. ** pointer into the journal struct before freeing the
  90. ** old one
  91. */
  92. node_tmp = jb->bitmaps;
  93. jb->bitmaps = jbitmap[i].bitmaps;
  94. vfree(node_tmp);
  95. }
  96. /* allocate additional bitmap blocks, reallocate array of bitmap
  97. * block pointers */
  98. bitmap =
  99. vmalloc(sizeof(struct reiserfs_bitmap_info) * bmap_nr_new);
  100. if (!bitmap) {
  101. /* Journal bitmaps are still supersized, but the memory isn't
  102. * leaked, so I guess it's ok */
  103. printk("reiserfs_resize: unable to allocate memory.\n");
  104. return -ENOMEM;
  105. }
  106. memset(bitmap, 0,
  107. sizeof(struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
  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. /* don't use read_bitmap_block since it will cache
  116. * the uninitialized bitmap */
  117. bh = sb_bread(s, i * s->s_blocksize * 8);
  118. memset(bh->b_data, 0, sb_blocksize(sb));
  119. reiserfs_test_and_set_le_bit(0, bh->b_data);
  120. reiserfs_cache_bitmap_metadata(s, bh, bitmap + i);
  121. set_buffer_uptodate(bh);
  122. mark_buffer_dirty(bh);
  123. sync_dirty_buffer(bh);
  124. // update bitmap_info stuff
  125. bitmap[i].first_zero_hint = 1;
  126. bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
  127. brelse(bh);
  128. }
  129. /* free old bitmap blocks array */
  130. SB_AP_BITMAP(s) = bitmap;
  131. vfree(old_bitmap);
  132. }
  133. /* begin transaction, if there was an error, it's fine. Yes, we have
  134. * incorrect bitmaps now, but none of it is ever going to touch the
  135. * disk anyway. */
  136. err = journal_begin(&th, s, 10);
  137. if (err)
  138. return err;
  139. /* Extend old last bitmap block - new blocks have been made available */
  140. info = SB_AP_BITMAP(s) + bmap_nr - 1;
  141. bh = reiserfs_read_bitmap_block(s, bmap_nr - 1);
  142. if (!bh) {
  143. int jerr = journal_end(&th, s, 10);
  144. if (jerr)
  145. return jerr;
  146. return -EIO;
  147. }
  148. reiserfs_prepare_for_journal(s, bh, 1);
  149. for (i = block_r; i < s->s_blocksize * 8; i++)
  150. reiserfs_test_and_clear_le_bit(i, bh->b_data);
  151. info->free_count += s->s_blocksize * 8 - block_r;
  152. if (!info->first_zero_hint)
  153. info->first_zero_hint = 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_test_and_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. /* Extreme case where last bitmap is the only valid block in itself. */
  172. if (!info->free_count)
  173. info->first_zero_hint = 0;
  174. /* update super */
  175. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
  176. free_blocks = SB_FREE_BLOCKS(s);
  177. PUT_SB_FREE_BLOCKS(s,
  178. free_blocks + (block_count_new - block_count -
  179. (bmap_nr_new - bmap_nr)));
  180. PUT_SB_BLOCK_COUNT(s, block_count_new);
  181. PUT_SB_BMAP_NR(s, bmap_nr_new);
  182. s->s_dirt = 1;
  183. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  184. SB_JOURNAL(s)->j_must_wait = 1;
  185. return journal_end(&th, s, 10);
  186. }