0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/kernel.h>
0012 #include <linux/mm.h>
0013 #include <linux/vmalloc.h>
0014 #include <linux/string.h>
0015 #include <linux/errno.h>
0016 #include "reiserfs.h"
0017 #include <linux/buffer_head.h>
0018
0019 int reiserfs_resize(struct super_block *s, unsigned long block_count_new)
0020 {
0021 int err = 0;
0022 struct reiserfs_super_block *sb;
0023 struct reiserfs_bitmap_info *bitmap;
0024 struct reiserfs_bitmap_info *info;
0025 struct reiserfs_bitmap_info *old_bitmap = SB_AP_BITMAP(s);
0026 struct buffer_head *bh;
0027 struct reiserfs_transaction_handle th;
0028 unsigned int bmap_nr_new, bmap_nr;
0029 unsigned int block_r_new, block_r;
0030
0031 struct reiserfs_list_bitmap *jb;
0032 struct reiserfs_list_bitmap jbitmap[JOURNAL_NUM_BITMAPS];
0033
0034 unsigned long int block_count, free_blocks;
0035 int i;
0036 int copy_size;
0037 int depth;
0038
0039 sb = SB_DISK_SUPER_BLOCK(s);
0040
0041 if (SB_BLOCK_COUNT(s) >= block_count_new) {
0042 printk("can\'t shrink filesystem on-line\n");
0043 return -EINVAL;
0044 }
0045
0046
0047 depth = reiserfs_write_unlock_nested(s);
0048 bh = sb_bread(s, block_count_new - 1);
0049 reiserfs_write_lock_nested(s, depth);
0050 if (!bh) {
0051 printk("reiserfs_resize: can\'t read last block\n");
0052 return -EINVAL;
0053 }
0054 bforget(bh);
0055
0056
0057
0058
0059
0060 if (SB_BUFFER_WITH_SB(s)->b_blocknr * SB_BUFFER_WITH_SB(s)->b_size
0061 != REISERFS_DISK_OFFSET_IN_BYTES) {
0062 printk
0063 ("reiserfs_resize: unable to resize a reiserfs without distributed bitmap (fs version < 3.5.12)\n");
0064 return -ENOTSUPP;
0065 }
0066
0067
0068 block_r = SB_BLOCK_COUNT(s) -
0069 (reiserfs_bmap_count(s) - 1) * s->s_blocksize * 8;
0070
0071
0072 bmap_nr_new = block_count_new / (s->s_blocksize * 8);
0073 block_r_new = block_count_new - bmap_nr_new * s->s_blocksize * 8;
0074 if (block_r_new)
0075 bmap_nr_new++;
0076 else
0077 block_r_new = s->s_blocksize * 8;
0078
0079
0080 block_count = SB_BLOCK_COUNT(s);
0081 bmap_nr = reiserfs_bmap_count(s);
0082
0083
0084 if (bmap_nr_new > bmap_nr) {
0085
0086 if (reiserfs_allocate_list_bitmaps(s, jbitmap, bmap_nr_new) < 0) {
0087 printk
0088 ("reiserfs_resize: unable to allocate memory for journal bitmaps\n");
0089 return -ENOMEM;
0090 }
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 copy_size = bmap_nr_new < bmap_nr ? bmap_nr_new : bmap_nr;
0101 copy_size =
0102 copy_size * sizeof(struct reiserfs_list_bitmap_node *);
0103 for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
0104 struct reiserfs_bitmap_node **node_tmp;
0105 jb = SB_JOURNAL(s)->j_list_bitmap + i;
0106 memcpy(jbitmap[i].bitmaps, jb->bitmaps, copy_size);
0107
0108
0109
0110
0111
0112
0113 node_tmp = jb->bitmaps;
0114 jb->bitmaps = jbitmap[i].bitmaps;
0115 vfree(node_tmp);
0116 }
0117
0118
0119
0120
0121
0122 bitmap =
0123 vzalloc(array_size(bmap_nr_new,
0124 sizeof(struct reiserfs_bitmap_info)));
0125 if (!bitmap) {
0126
0127
0128
0129
0130 printk("reiserfs_resize: unable to allocate memory.\n");
0131 return -ENOMEM;
0132 }
0133 for (i = 0; i < bmap_nr; i++)
0134 bitmap[i] = old_bitmap[i];
0135
0136
0137
0138
0139
0140
0141
0142 for (i = bmap_nr; i < bmap_nr_new; i++) {
0143 int depth;
0144
0145
0146
0147
0148 depth = reiserfs_write_unlock_nested(s);
0149 bh = sb_bread(s, i * s->s_blocksize * 8);
0150 reiserfs_write_lock_nested(s, depth);
0151 if (!bh) {
0152 vfree(bitmap);
0153 return -EIO;
0154 }
0155 memset(bh->b_data, 0, sb_blocksize(sb));
0156 reiserfs_set_le_bit(0, bh->b_data);
0157 reiserfs_cache_bitmap_metadata(s, bh, bitmap + i);
0158
0159 set_buffer_uptodate(bh);
0160 mark_buffer_dirty(bh);
0161 depth = reiserfs_write_unlock_nested(s);
0162 sync_dirty_buffer(bh);
0163 reiserfs_write_lock_nested(s, depth);
0164
0165 bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
0166 brelse(bh);
0167 }
0168
0169 SB_AP_BITMAP(s) = bitmap;
0170 vfree(old_bitmap);
0171 }
0172
0173
0174
0175
0176
0177
0178 err = journal_begin(&th, s, 10);
0179 if (err)
0180 return err;
0181
0182
0183 info = SB_AP_BITMAP(s) + bmap_nr - 1;
0184 bh = reiserfs_read_bitmap_block(s, bmap_nr - 1);
0185 if (!bh) {
0186 int jerr = journal_end(&th);
0187 if (jerr)
0188 return jerr;
0189 return -EIO;
0190 }
0191
0192 reiserfs_prepare_for_journal(s, bh, 1);
0193 for (i = block_r; i < s->s_blocksize * 8; i++)
0194 reiserfs_clear_le_bit(i, bh->b_data);
0195 info->free_count += s->s_blocksize * 8 - block_r;
0196
0197 journal_mark_dirty(&th, bh);
0198 brelse(bh);
0199
0200
0201 info = SB_AP_BITMAP(s) + bmap_nr_new - 1;
0202 bh = reiserfs_read_bitmap_block(s, bmap_nr_new - 1);
0203 if (!bh) {
0204 int jerr = journal_end(&th);
0205 if (jerr)
0206 return jerr;
0207 return -EIO;
0208 }
0209
0210 reiserfs_prepare_for_journal(s, bh, 1);
0211 for (i = block_r_new; i < s->s_blocksize * 8; i++)
0212 reiserfs_set_le_bit(i, bh->b_data);
0213 journal_mark_dirty(&th, bh);
0214 brelse(bh);
0215
0216 info->free_count -= s->s_blocksize * 8 - block_r_new;
0217
0218 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
0219 free_blocks = SB_FREE_BLOCKS(s);
0220 PUT_SB_FREE_BLOCKS(s,
0221 free_blocks + (block_count_new - block_count -
0222 (bmap_nr_new - bmap_nr)));
0223 PUT_SB_BLOCK_COUNT(s, block_count_new);
0224 PUT_SB_BMAP_NR(s, bmap_would_wrap(bmap_nr_new) ? : bmap_nr_new);
0225
0226 journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
0227
0228 SB_JOURNAL(s)->j_must_wait = 1;
0229 return journal_end(&th);
0230 }