Blame view
fs/reiserfs/resize.c
6.07 KB
0222e6571 reiserfs: strip t... |
1 |
/* |
1da177e4c Linux-2.6.12-rc2 |
2 3 |
* Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README */ |
bd4c625c0 reiserfs: run scr... |
4 |
|
0222e6571 reiserfs: strip t... |
5 |
/* |
1da177e4c Linux-2.6.12-rc2 |
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> |
f466c6fdb move private bits... |
16 |
#include "reiserfs.h" |
1da177e4c Linux-2.6.12-rc2 |
17 |
#include <linux/buffer_head.h> |
bd4c625c0 reiserfs: run scr... |
18 |
int reiserfs_resize(struct super_block *s, unsigned long block_count_new) |
1da177e4c Linux-2.6.12-rc2 |
19 |
{ |
bd4c625c0 reiserfs: run scr... |
20 21 22 |
int err = 0; struct reiserfs_super_block *sb; struct reiserfs_bitmap_info *bitmap; |
0b3dc17bc [PATCH] reiserfs:... |
23 |
struct reiserfs_bitmap_info *info; |
1da177e4c Linux-2.6.12-rc2 |
24 |
struct reiserfs_bitmap_info *old_bitmap = SB_AP_BITMAP(s); |
bd4c625c0 reiserfs: run scr... |
25 |
struct buffer_head *bh; |
1da177e4c Linux-2.6.12-rc2 |
26 27 28 |
struct reiserfs_transaction_handle th; unsigned int bmap_nr_new, bmap_nr; unsigned int block_r_new, block_r; |
bd4c625c0 reiserfs: run scr... |
29 30 |
struct reiserfs_list_bitmap *jb; |
1da177e4c Linux-2.6.12-rc2 |
31 |
struct reiserfs_list_bitmap jbitmap[JOURNAL_NUM_BITMAPS]; |
bd4c625c0 reiserfs: run scr... |
32 |
|
1da177e4c Linux-2.6.12-rc2 |
33 34 |
unsigned long int block_count, free_blocks; int i; |
bd4c625c0 reiserfs: run scr... |
35 |
int copy_size; |
1da177e4c Linux-2.6.12-rc2 |
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
sb = SB_DISK_SUPER_BLOCK(s); if (SB_BLOCK_COUNT(s) >= block_count_new) { printk("can\'t shrink filesystem on-line "); return -EINVAL; } /* check the device size */ bh = sb_bread(s, block_count_new - 1); if (!bh) { printk("reiserfs_resize: can\'t read last block "); return -EINVAL; |
bd4c625c0 reiserfs: run scr... |
51 |
} |
1da177e4c Linux-2.6.12-rc2 |
52 53 54 55 |
bforget(bh); /* old disk layout detection; those partitions can be mounted, but * cannot be resized */ |
bd4c625c0 reiserfs: run scr... |
56 57 58 59 60 |
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) "); |
1da177e4c Linux-2.6.12-rc2 |
61 62 |
return -ENOTSUPP; } |
bd4c625c0 reiserfs: run scr... |
63 |
|
1da177e4c Linux-2.6.12-rc2 |
64 |
/* count used bits in last bitmap block */ |
cb680c1be reiserfs: ignore ... |
65 66 |
block_r = SB_BLOCK_COUNT(s) - (reiserfs_bmap_count(s) - 1) * s->s_blocksize * 8; |
bd4c625c0 reiserfs: run scr... |
67 |
|
1da177e4c Linux-2.6.12-rc2 |
68 |
/* count bitmap blocks in new fs */ |
bd4c625c0 reiserfs: run scr... |
69 |
bmap_nr_new = block_count_new / (s->s_blocksize * 8); |
1da177e4c Linux-2.6.12-rc2 |
70 |
block_r_new = block_count_new - bmap_nr_new * s->s_blocksize * 8; |
bd4c625c0 reiserfs: run scr... |
71 |
if (block_r_new) |
1da177e4c Linux-2.6.12-rc2 |
72 73 74 75 76 77 |
bmap_nr_new++; else block_r_new = s->s_blocksize * 8; /* save old values */ block_count = SB_BLOCK_COUNT(s); |
cb680c1be reiserfs: ignore ... |
78 |
bmap_nr = reiserfs_bmap_count(s); |
1da177e4c Linux-2.6.12-rc2 |
79 80 |
/* resizing of reiserfs bitmaps (journal and real), if needed */ |
bd4c625c0 reiserfs: run scr... |
81 82 83 84 85 86 |
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 "); |
bd4c625c0 reiserfs: run scr... |
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
return -ENOMEM; } /* the new journal bitmaps are zero filled, now we copy in 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. */ 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); /* just in case vfree schedules on us, copy the new |
0222e6571 reiserfs: strip t... |
105 |
** pointer into the journal struct before freeing the |
bd4c625c0 reiserfs: run scr... |
106 107 108 109 110 111 112 113 114 115 |
** old one */ node_tmp = jb->bitmaps; jb->bitmaps = jbitmap[i].bitmaps; vfree(node_tmp); } /* allocate additional bitmap blocks, reallocate array of bitmap * block pointers */ bitmap = |
558feb081 fs: Convert vmall... |
116 |
vzalloc(sizeof(struct reiserfs_bitmap_info) * bmap_nr_new); |
bd4c625c0 reiserfs: run scr... |
117 118 119 120 121 122 123 |
if (!bitmap) { /* Journal bitmaps are still supersized, but the memory isn't * leaked, so I guess it's ok */ printk("reiserfs_resize: unable to allocate memory. "); return -ENOMEM; } |
bd4c625c0 reiserfs: run scr... |
124 125 126 127 128 129 130 131 |
for (i = 0; i < bmap_nr; i++) bitmap[i] = old_bitmap[i]; /* 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. */ for (i = bmap_nr; i < bmap_nr_new; i++) { |
5065227b4 [PATCH] reiserfs:... |
132 133 134 |
/* don't use read_bitmap_block since it will cache * the uninitialized bitmap */ bh = sb_bread(s, i * s->s_blocksize * 8); |
2d3466a34 reiserfs: possibl... |
135 136 137 138 |
if (!bh) { vfree(bitmap); return -EIO; } |
0b3dc17bc [PATCH] reiserfs:... |
139 |
memset(bh->b_data, 0, sb_blocksize(sb)); |
0c2fd1bfb reiserfs: use pro... |
140 |
reiserfs_set_le_bit(0, bh->b_data); |
6f01046b3 [PATCH] reiserfs:... |
141 |
reiserfs_cache_bitmap_metadata(s, bh, bitmap + i); |
0b3dc17bc [PATCH] reiserfs:... |
142 143 144 |
set_buffer_uptodate(bh); mark_buffer_dirty(bh); |
8ebc42323 reiserfs: kill-th... |
145 |
reiserfs_write_unlock(s); |
0b3dc17bc [PATCH] reiserfs:... |
146 |
sync_dirty_buffer(bh); |
8ebc42323 reiserfs: kill-th... |
147 |
reiserfs_write_lock(s); |
bd4c625c0 reiserfs: run scr... |
148 |
// update bitmap_info stuff |
bd4c625c0 reiserfs: run scr... |
149 |
bitmap[i].free_count = sb_blocksize(sb) * 8 - 1; |
0b3dc17bc [PATCH] reiserfs:... |
150 |
brelse(bh); |
bd4c625c0 reiserfs: run scr... |
151 152 153 154 |
} /* free old bitmap blocks array */ SB_AP_BITMAP(s) = bitmap; vfree(old_bitmap); |
1da177e4c Linux-2.6.12-rc2 |
155 |
} |
bd4c625c0 reiserfs: run scr... |
156 |
|
1da177e4c Linux-2.6.12-rc2 |
157 158 159 160 161 |
/* begin transaction, if there was an error, it's fine. Yes, we have * incorrect bitmaps now, but none of it is ever going to touch the * disk anyway. */ err = journal_begin(&th, s, 10); if (err) |
bd4c625c0 reiserfs: run scr... |
162 |
return err; |
1da177e4c Linux-2.6.12-rc2 |
163 |
|
0b3dc17bc [PATCH] reiserfs:... |
164 165 |
/* Extend old last bitmap block - new blocks have been made available */ info = SB_AP_BITMAP(s) + bmap_nr - 1; |
5065227b4 [PATCH] reiserfs:... |
166 167 168 169 170 171 172 |
bh = reiserfs_read_bitmap_block(s, bmap_nr - 1); if (!bh) { int jerr = journal_end(&th, s, 10); if (jerr) return jerr; return -EIO; } |
0b3dc17bc [PATCH] reiserfs:... |
173 174 |
reiserfs_prepare_for_journal(s, bh, 1); |
1da177e4c Linux-2.6.12-rc2 |
175 |
for (i = block_r; i < s->s_blocksize * 8; i++) |
0c2fd1bfb reiserfs: use pro... |
176 |
reiserfs_clear_le_bit(i, bh->b_data); |
0b3dc17bc [PATCH] reiserfs:... |
177 |
info->free_count += s->s_blocksize * 8 - block_r; |
0b3dc17bc [PATCH] reiserfs:... |
178 179 180 |
journal_mark_dirty(&th, s, bh); brelse(bh); |
1da177e4c Linux-2.6.12-rc2 |
181 |
|
0b3dc17bc [PATCH] reiserfs:... |
182 183 |
/* Correct new last bitmap block - It may not be full */ info = SB_AP_BITMAP(s) + bmap_nr_new - 1; |
5065227b4 [PATCH] reiserfs:... |
184 185 186 187 188 189 190 |
bh = reiserfs_read_bitmap_block(s, bmap_nr_new - 1); if (!bh) { int jerr = journal_end(&th, s, 10); if (jerr) return jerr; return -EIO; } |
1da177e4c Linux-2.6.12-rc2 |
191 |
|
0b3dc17bc [PATCH] reiserfs:... |
192 |
reiserfs_prepare_for_journal(s, bh, 1); |
1da177e4c Linux-2.6.12-rc2 |
193 |
for (i = block_r_new; i < s->s_blocksize * 8; i++) |
0c2fd1bfb reiserfs: use pro... |
194 |
reiserfs_set_le_bit(i, bh->b_data); |
0b3dc17bc [PATCH] reiserfs:... |
195 196 |
journal_mark_dirty(&th, s, bh); brelse(bh); |
bd4c625c0 reiserfs: run scr... |
197 |
|
0b3dc17bc [PATCH] reiserfs:... |
198 |
info->free_count -= s->s_blocksize * 8 - block_r_new; |
bd4c625c0 reiserfs: run scr... |
199 200 |
/* update super */ reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1); |
1da177e4c Linux-2.6.12-rc2 |
201 |
free_blocks = SB_FREE_BLOCKS(s); |
bd4c625c0 reiserfs: run scr... |
202 203 204 |
PUT_SB_FREE_BLOCKS(s, free_blocks + (block_count_new - block_count - (bmap_nr_new - bmap_nr))); |
1da177e4c Linux-2.6.12-rc2 |
205 |
PUT_SB_BLOCK_COUNT(s, block_count_new); |
cb680c1be reiserfs: ignore ... |
206 |
PUT_SB_BMAP_NR(s, bmap_would_wrap(bmap_nr_new) ? : bmap_nr_new); |
1da177e4c Linux-2.6.12-rc2 |
207 208 |
journal_mark_dirty(&th, s, SB_BUFFER_WITH_SB(s)); |
bd4c625c0 reiserfs: run scr... |
209 |
|
1da177e4c Linux-2.6.12-rc2 |
210 211 212 |
SB_JOURNAL(s)->j_must_wait = 1; return journal_end(&th, s, 10); } |