resize.c 6.34 KB
Newer Older
1
/*
Linus Torvalds's avatar
Linus Torvalds committed
2 3
 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
 */
4

5
/*
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8 9 10 11 12 13 14 15
 * Written by Alexander Zarochentcev.
 *
 * The kernel part of the (on-line) reiserfs resizer.
 */

#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/errno.h>
16
#include "reiserfs.h"
Linus Torvalds's avatar
Linus Torvalds committed
17 18
#include <linux/buffer_head.h>

19
int reiserfs_resize(struct super_block *s, unsigned long block_count_new)
Linus Torvalds's avatar
Linus Torvalds committed
20
{
21 22 23
	int err = 0;
	struct reiserfs_super_block *sb;
	struct reiserfs_bitmap_info *bitmap;
24
	struct reiserfs_bitmap_info *info;
Linus Torvalds's avatar
Linus Torvalds committed
25
	struct reiserfs_bitmap_info *old_bitmap = SB_AP_BITMAP(s);
26
	struct buffer_head *bh;
Linus Torvalds's avatar
Linus Torvalds committed
27 28 29
	struct reiserfs_transaction_handle th;
	unsigned int bmap_nr_new, bmap_nr;
	unsigned int block_r_new, block_r;
30 31

	struct reiserfs_list_bitmap *jb;
Linus Torvalds's avatar
Linus Torvalds committed
32
	struct reiserfs_list_bitmap jbitmap[JOURNAL_NUM_BITMAPS];
33

Linus Torvalds's avatar
Linus Torvalds committed
34 35
	unsigned long int block_count, free_blocks;
	int i;
36
	int copy_size;
37
	int depth;
Linus Torvalds's avatar
Linus Torvalds committed
38 39 40 41 42 43 44 45 46

	sb = SB_DISK_SUPER_BLOCK(s);

	if (SB_BLOCK_COUNT(s) >= block_count_new) {
		printk("can\'t shrink filesystem on-line\n");
		return -EINVAL;
	}

	/* check the device size */
47
	depth = reiserfs_write_unlock_nested(s);
Linus Torvalds's avatar
Linus Torvalds committed
48
	bh = sb_bread(s, block_count_new - 1);
49
	reiserfs_write_lock_nested(s, depth);
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52
	if (!bh) {
		printk("reiserfs_resize: can\'t read last block\n");
		return -EINVAL;
53
	}
Linus Torvalds's avatar
Linus Torvalds committed
54 55
	bforget(bh);

56 57 58 59
	/*
	 * old disk layout detection; those partitions can be mounted, but
	 * cannot be resized
	 */
60 61 62 63
	if (SB_BUFFER_WITH_SB(s)->b_blocknr * SB_BUFFER_WITH_SB(s)->b_size
	    != REISERFS_DISK_OFFSET_IN_BYTES) {
		printk
		    ("reiserfs_resize: unable to resize a reiserfs without distributed bitmap (fs version < 3.5.12)\n");
Linus Torvalds's avatar
Linus Torvalds committed
64 65
		return -ENOTSUPP;
	}
66

Linus Torvalds's avatar
Linus Torvalds committed
67
	/* count used bits in last bitmap block */
68 69
	block_r = SB_BLOCK_COUNT(s) -
			(reiserfs_bmap_count(s) - 1) * s->s_blocksize * 8;
70

Linus Torvalds's avatar
Linus Torvalds committed
71
	/* count bitmap blocks in new fs */
72
	bmap_nr_new = block_count_new / (s->s_blocksize * 8);
Linus Torvalds's avatar
Linus Torvalds committed
73
	block_r_new = block_count_new - bmap_nr_new * s->s_blocksize * 8;
74
	if (block_r_new)
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78 79 80
		bmap_nr_new++;
	else
		block_r_new = s->s_blocksize * 8;

	/* save old values */
	block_count = SB_BLOCK_COUNT(s);
81
	bmap_nr = reiserfs_bmap_count(s);
Linus Torvalds's avatar
Linus Torvalds committed
82 83

	/* resizing of reiserfs bitmaps (journal and real), if needed */
84 85 86 87 88 89 90
	if (bmap_nr_new > bmap_nr) {
		/* reallocate journal bitmaps */
		if (reiserfs_allocate_list_bitmaps(s, jbitmap, bmap_nr_new) < 0) {
			printk
			    ("reiserfs_resize: unable to allocate memory for journal bitmaps\n");
			return -ENOMEM;
		}
91 92 93 94 95 96 97 98
		/*
		 * the new journal bitmaps are zero filled, now we copy i
		 * the bitmap node pointers from the old journal bitmap
		 * structs, and then transfer the new data structures
		 * into the journal struct.
		 *
		 * using the copy_size var below allows this code to work for
		 * both shrinking and expanding the FS.
99 100 101 102 103 104 105 106 107
		 */
		copy_size = bmap_nr_new < bmap_nr ? bmap_nr_new : bmap_nr;
		copy_size =
		    copy_size * sizeof(struct reiserfs_list_bitmap_node *);
		for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
			struct reiserfs_bitmap_node **node_tmp;
			jb = SB_JOURNAL(s)->j_list_bitmap + i;
			memcpy(jbitmap[i].bitmaps, jb->bitmaps, copy_size);

108 109 110 111
			/*
			 * just in case vfree schedules on us, copy the new
			 * pointer into the journal struct before freeing the
			 * old one
112 113 114 115 116 117
			 */
			node_tmp = jb->bitmaps;
			jb->bitmaps = jbitmap[i].bitmaps;
			vfree(node_tmp);
		}

118 119 120 121
		/*
		 * allocate additional bitmap blocks, reallocate
		 * array of bitmap block pointers
		 */
122
		bitmap =
123 124
		    vzalloc(array_size(bmap_nr_new,
				       sizeof(struct reiserfs_bitmap_info)));
125
		if (!bitmap) {
126 127 128 129
			/*
			 * Journal bitmaps are still supersized, but the
			 * memory isn't leaked, so I guess it's ok
			 */
130 131 132 133 134 135
			printk("reiserfs_resize: unable to allocate memory.\n");
			return -ENOMEM;
		}
		for (i = 0; i < bmap_nr; i++)
			bitmap[i] = old_bitmap[i];

136 137 138 139 140 141
		/*
		 * This doesn't go through the journal, but it doesn't have to.
		 * The changes are still atomic: We're synced up when the
		 * journal transaction begins, and the new bitmaps don't
		 * matter if the transaction fails.
		 */
142
		for (i = bmap_nr; i < bmap_nr_new; i++) {
143
			int depth;
144 145 146 147
			/*
			 * don't use read_bitmap_block since it will cache
			 * the uninitialized bitmap
			 */
148
			depth = reiserfs_write_unlock_nested(s);
149
			bh = sb_bread(s, i * s->s_blocksize * 8);
150
			reiserfs_write_lock_nested(s, depth);
151 152 153 154
			if (!bh) {
				vfree(bitmap);
				return -EIO;
			}
155
			memset(bh->b_data, 0, sb_blocksize(sb));
156
			reiserfs_set_le_bit(0, bh->b_data);
157
			reiserfs_cache_bitmap_metadata(s, bh, bitmap + i);
158 159 160

			set_buffer_uptodate(bh);
			mark_buffer_dirty(bh);
161
			depth = reiserfs_write_unlock_nested(s);
162
			sync_dirty_buffer(bh);
163
			reiserfs_write_lock_nested(s, depth);
164
			/* update bitmap_info stuff */
165
			bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
166
			brelse(bh);
167 168 169 170
		}
		/* free old bitmap blocks array */
		SB_AP_BITMAP(s) = bitmap;
		vfree(old_bitmap);
Linus Torvalds's avatar
Linus Torvalds committed
171
	}
172

173 174
	/*
	 * begin transaction, if there was an error, it's fine. Yes, we have
Linus Torvalds's avatar
Linus Torvalds committed
175
	 * incorrect bitmaps now, but none of it is ever going to touch the
176 177
	 * disk anyway.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
178 179
	err = journal_begin(&th, s, 10);
	if (err)
180
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
181

182 183
	/* Extend old last bitmap block - new blocks have been made available */
	info = SB_AP_BITMAP(s) + bmap_nr - 1;
184 185
	bh = reiserfs_read_bitmap_block(s, bmap_nr - 1);
	if (!bh) {
186
		int jerr = journal_end(&th);
187 188 189 190
		if (jerr)
			return jerr;
		return -EIO;
	}
191 192

	reiserfs_prepare_for_journal(s, bh, 1);
Linus Torvalds's avatar
Linus Torvalds committed
193
	for (i = block_r; i < s->s_blocksize * 8; i++)
194
		reiserfs_clear_le_bit(i, bh->b_data);
195 196
	info->free_count += s->s_blocksize * 8 - block_r;

197
	journal_mark_dirty(&th, bh);
198
	brelse(bh);
Linus Torvalds's avatar
Linus Torvalds committed
199

200 201
	/* Correct new last bitmap block - It may not be full */
	info = SB_AP_BITMAP(s) + bmap_nr_new - 1;
202 203
	bh = reiserfs_read_bitmap_block(s, bmap_nr_new - 1);
	if (!bh) {
204
		int jerr = journal_end(&th);
205 206 207 208
		if (jerr)
			return jerr;
		return -EIO;
	}
Linus Torvalds's avatar
Linus Torvalds committed
209

210
	reiserfs_prepare_for_journal(s, bh, 1);
Linus Torvalds's avatar
Linus Torvalds committed
211
	for (i = block_r_new; i < s->s_blocksize * 8; i++)
212
		reiserfs_set_le_bit(i, bh->b_data);
213
	journal_mark_dirty(&th, bh);
214
	brelse(bh);
215

216
	info->free_count -= s->s_blocksize * 8 - block_r_new;
217 218
	/* update super */
	reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
Linus Torvalds's avatar
Linus Torvalds committed
219
	free_blocks = SB_FREE_BLOCKS(s);
220 221 222
	PUT_SB_FREE_BLOCKS(s,
			   free_blocks + (block_count_new - block_count -
					  (bmap_nr_new - bmap_nr)));
Linus Torvalds's avatar
Linus Torvalds committed
223
	PUT_SB_BLOCK_COUNT(s, block_count_new);
224
	PUT_SB_BMAP_NR(s, bmap_would_wrap(bmap_nr_new) ? : bmap_nr_new);
Linus Torvalds's avatar
Linus Torvalds committed
225

226
	journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
227

Linus Torvalds's avatar
Linus Torvalds committed
228
	SB_JOURNAL(s)->j_must_wait = 1;
229
	return journal_end(&th);
Linus Torvalds's avatar
Linus Torvalds committed
230
}