resize.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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("reiserfs_resize: unable to resize a reiserfs without distributed bitmap (fs version < 3.5.12)\n");
  49. return -ENOTSUPP;
  50. }
  51. /* count used bits in last bitmap block */
  52. block_r = SB_BLOCK_COUNT(s) -
  53. (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("reiserfs_resize: unable to allocate memory for journal bitmaps\n");
  69. unlock_super(s) ;
  70. return -ENOMEM ;
  71. }
  72. /* the new journal bitmaps are zero filled, now we copy in the bitmap
  73. ** node pointers from the old journal bitmap structs, and then
  74. ** transfer the new data structures into the journal struct.
  75. **
  76. ** using the copy_size var below allows this code to work for
  77. ** both shrinking and expanding the FS.
  78. */
  79. copy_size = bmap_nr_new < bmap_nr ? bmap_nr_new : bmap_nr ;
  80. copy_size = copy_size * sizeof(struct reiserfs_list_bitmap_node *) ;
  81. for (i = 0 ; i < JOURNAL_NUM_BITMAPS ; i++) {
  82. struct reiserfs_bitmap_node **node_tmp ;
  83. jb = SB_JOURNAL(s)->j_list_bitmap + i ;
  84. memcpy(jbitmap[i].bitmaps, jb->bitmaps, copy_size) ;
  85. /* just in case vfree schedules on us, copy the new
  86. ** pointer into the journal struct before freeing the
  87. ** old one
  88. */
  89. node_tmp = jb->bitmaps ;
  90. jb->bitmaps = jbitmap[i].bitmaps ;
  91. vfree(node_tmp) ;
  92. }
  93. /* allocate additional bitmap blocks, reallocate array of bitmap
  94. * block pointers */
  95. bitmap = vmalloc(sizeof(struct reiserfs_bitmap_info) * bmap_nr_new);
  96. if (!bitmap) {
  97. /* Journal bitmaps are still supersized, but the memory isn't
  98. * leaked, so I guess it's ok */
  99. printk("reiserfs_resize: unable to allocate memory.\n");
  100. return -ENOMEM;
  101. }
  102. memset (bitmap, 0, sizeof (struct reiserfs_bitmap_info) * SB_BMAP_NR(s));
  103. for (i = 0; i < bmap_nr; i++)
  104. bitmap[i] = old_bitmap[i];
  105. /* This doesn't go through the journal, but it doesn't have to.
  106. * The changes are still atomic: We're synced up when the journal
  107. * transaction begins, and the new bitmaps don't matter if the
  108. * transaction fails. */
  109. for (i = bmap_nr; i < bmap_nr_new; i++) {
  110. bitmap[i].bh = sb_getblk(s, i * s->s_blocksize * 8);
  111. memset(bitmap[i].bh->b_data, 0, sb_blocksize(sb));
  112. reiserfs_test_and_set_le_bit(0, bitmap[i].bh->b_data);
  113. set_buffer_uptodate(bitmap[i].bh);
  114. mark_buffer_dirty(bitmap[i].bh) ;
  115. sync_dirty_buffer(bitmap[i].bh);
  116. // update bitmap_info stuff
  117. bitmap[i].first_zero_hint=1;
  118. bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
  119. }
  120. /* free old bitmap blocks array */
  121. SB_AP_BITMAP(s) = bitmap;
  122. vfree (old_bitmap);
  123. }
  124. /* begin transaction, if there was an error, it's fine. Yes, we have
  125. * incorrect bitmaps now, but none of it is ever going to touch the
  126. * disk anyway. */
  127. err = journal_begin(&th, s, 10);
  128. if (err)
  129. return err;
  130. /* correct last bitmap blocks in old and new disk layout */
  131. reiserfs_prepare_for_journal(s, SB_AP_BITMAP(s)[bmap_nr - 1].bh, 1);
  132. for (i = block_r; i < s->s_blocksize * 8; i++)
  133. reiserfs_test_and_clear_le_bit(i,
  134. SB_AP_BITMAP(s)[bmap_nr - 1].bh->b_data);
  135. SB_AP_BITMAP(s)[bmap_nr - 1].free_count += s->s_blocksize * 8 - block_r;
  136. if ( !SB_AP_BITMAP(s)[bmap_nr - 1].first_zero_hint)
  137. SB_AP_BITMAP(s)[bmap_nr - 1].first_zero_hint = block_r;
  138. journal_mark_dirty(&th, s, SB_AP_BITMAP(s)[bmap_nr - 1].bh);
  139. reiserfs_prepare_for_journal(s, SB_AP_BITMAP(s)[bmap_nr_new - 1].bh, 1);
  140. for (i = block_r_new; i < s->s_blocksize * 8; i++)
  141. reiserfs_test_and_set_le_bit(i,
  142. SB_AP_BITMAP(s)[bmap_nr_new - 1].bh->b_data);
  143. journal_mark_dirty(&th, s, SB_AP_BITMAP(s)[bmap_nr_new - 1].bh);
  144. SB_AP_BITMAP(s)[bmap_nr_new - 1].free_count -= s->s_blocksize * 8 - block_r_new;
  145. /* Extreme case where last bitmap is the only valid block in itself. */
  146. if ( !SB_AP_BITMAP(s)[bmap_nr_new - 1].free_count )
  147. SB_AP_BITMAP(s)[bmap_nr_new - 1].first_zero_hint = 0;
  148. /* update super */
  149. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
  150. free_blocks = SB_FREE_BLOCKS(s);
  151. PUT_SB_FREE_BLOCKS(s, free_blocks + (block_count_new - block_count - (bmap_nr_new - bmap_nr)));
  152. PUT_SB_BLOCK_COUNT(s, block_count_new);
  153. PUT_SB_BMAP_NR(s, bmap_nr_new);
  154. s->s_dirt = 1;
  155. journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s));
  156. SB_JOURNAL(s)->j_must_wait = 1;
  157. return journal_end(&th, s, 10);
  158. }