]>
Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/resize.c |
ac27a0ec | 3 | * |
617ba13b | 4 | * Support for resizing an ext4 filesystem while it is mounted. |
ac27a0ec DK |
5 | * |
6 | * Copyright (C) 2001, 2002 Andreas Dilger <adilger@clusterfs.com> | |
7 | * | |
8 | * This could probably be made into a module, because it is not often in use. | |
9 | */ | |
10 | ||
11 | ||
617ba13b | 12 | #define EXT4FS_DEBUG |
ac27a0ec | 13 | |
dab291af | 14 | #include <linux/ext4_jbd2.h> |
ac27a0ec DK |
15 | |
16 | #include <linux/errno.h> | |
17 | #include <linux/slab.h> | |
18 | ||
19 | ||
20 | #define outside(b, first, last) ((b) < (first) || (b) >= (last)) | |
21 | #define inside(b, first, last) ((b) >= (first) && (b) < (last)) | |
22 | ||
23 | static int verify_group_input(struct super_block *sb, | |
617ba13b | 24 | struct ext4_new_group_data *input) |
ac27a0ec | 25 | { |
617ba13b MC |
26 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
27 | struct ext4_super_block *es = sbi->s_es; | |
bd81d8ee | 28 | ext4_fsblk_t start = ext4_blocks_count(es); |
617ba13b | 29 | ext4_fsblk_t end = start + input->blocks_count; |
ac27a0ec | 30 | unsigned group = input->group; |
617ba13b MC |
31 | ext4_fsblk_t itend = input->inode_table + sbi->s_itb_per_group; |
32 | unsigned overhead = ext4_bg_has_super(sb, group) ? | |
33 | (1 + ext4_bg_num_gdb(sb, group) + | |
ac27a0ec | 34 | le16_to_cpu(es->s_reserved_gdt_blocks)) : 0; |
617ba13b | 35 | ext4_fsblk_t metaend = start + overhead; |
ac27a0ec | 36 | struct buffer_head *bh = NULL; |
3a5b2ecd | 37 | ext4_grpblk_t free_blocks_count, offset; |
ac27a0ec DK |
38 | int err = -EINVAL; |
39 | ||
40 | input->free_blocks_count = free_blocks_count = | |
41 | input->blocks_count - 2 - overhead - sbi->s_itb_per_group; | |
42 | ||
43 | if (test_opt(sb, DEBUG)) | |
617ba13b | 44 | printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks " |
ac27a0ec | 45 | "(%d free, %u reserved)\n", |
617ba13b | 46 | ext4_bg_has_super(sb, input->group) ? "normal" : |
ac27a0ec DK |
47 | "no-super", input->group, input->blocks_count, |
48 | free_blocks_count, input->reserved_blocks); | |
49 | ||
3a5b2ecd | 50 | ext4_get_group_no_and_offset(sb, start, NULL, &offset); |
ac27a0ec | 51 | if (group != sbi->s_groups_count) |
617ba13b | 52 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
53 | "Cannot add at group %u (only %lu groups)", |
54 | input->group, sbi->s_groups_count); | |
3a5b2ecd MC |
55 | else if (offset != 0) |
56 | ext4_warning(sb, __FUNCTION__, "Last group not full"); | |
ac27a0ec | 57 | else if (input->reserved_blocks > input->blocks_count / 5) |
617ba13b | 58 | ext4_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)", |
ac27a0ec DK |
59 | input->reserved_blocks); |
60 | else if (free_blocks_count < 0) | |
617ba13b | 61 | ext4_warning(sb, __FUNCTION__, "Bad blocks count %u", |
ac27a0ec DK |
62 | input->blocks_count); |
63 | else if (!(bh = sb_bread(sb, end - 1))) | |
617ba13b | 64 | ext4_warning(sb, __FUNCTION__, |
2ae02107 | 65 | "Cannot read last block (%llu)", |
ac27a0ec DK |
66 | end - 1); |
67 | else if (outside(input->block_bitmap, start, end)) | |
617ba13b | 68 | ext4_warning(sb, __FUNCTION__, |
bd81d8ee | 69 | "Block bitmap not in group (block %llu)", |
1939e49a | 70 | (unsigned long long)input->block_bitmap); |
ac27a0ec | 71 | else if (outside(input->inode_bitmap, start, end)) |
617ba13b | 72 | ext4_warning(sb, __FUNCTION__, |
bd81d8ee | 73 | "Inode bitmap not in group (block %llu)", |
1939e49a | 74 | (unsigned long long)input->inode_bitmap); |
ac27a0ec DK |
75 | else if (outside(input->inode_table, start, end) || |
76 | outside(itend - 1, start, end)) | |
617ba13b | 77 | ext4_warning(sb, __FUNCTION__, |
bd81d8ee | 78 | "Inode table not in group (blocks %llu-%llu)", |
1939e49a | 79 | (unsigned long long)input->inode_table, itend - 1); |
ac27a0ec | 80 | else if (input->inode_bitmap == input->block_bitmap) |
617ba13b | 81 | ext4_warning(sb, __FUNCTION__, |
bd81d8ee | 82 | "Block bitmap same as inode bitmap (%llu)", |
1939e49a | 83 | (unsigned long long)input->block_bitmap); |
ac27a0ec | 84 | else if (inside(input->block_bitmap, input->inode_table, itend)) |
617ba13b | 85 | ext4_warning(sb, __FUNCTION__, |
bd81d8ee | 86 | "Block bitmap (%llu) in inode table (%llu-%llu)", |
1939e49a RD |
87 | (unsigned long long)input->block_bitmap, |
88 | (unsigned long long)input->inode_table, itend - 1); | |
ac27a0ec | 89 | else if (inside(input->inode_bitmap, input->inode_table, itend)) |
617ba13b | 90 | ext4_warning(sb, __FUNCTION__, |
bd81d8ee | 91 | "Inode bitmap (%llu) in inode table (%llu-%llu)", |
1939e49a RD |
92 | (unsigned long long)input->inode_bitmap, |
93 | (unsigned long long)input->inode_table, itend - 1); | |
ac27a0ec | 94 | else if (inside(input->block_bitmap, start, metaend)) |
617ba13b | 95 | ext4_warning(sb, __FUNCTION__, |
bd81d8ee | 96 | "Block bitmap (%llu) in GDT table" |
2ae02107 | 97 | " (%llu-%llu)", |
1939e49a RD |
98 | (unsigned long long)input->block_bitmap, |
99 | start, metaend - 1); | |
ac27a0ec | 100 | else if (inside(input->inode_bitmap, start, metaend)) |
617ba13b | 101 | ext4_warning(sb, __FUNCTION__, |
bd81d8ee | 102 | "Inode bitmap (%llu) in GDT table" |
2ae02107 | 103 | " (%llu-%llu)", |
1939e49a RD |
104 | (unsigned long long)input->inode_bitmap, |
105 | start, metaend - 1); | |
ac27a0ec DK |
106 | else if (inside(input->inode_table, start, metaend) || |
107 | inside(itend - 1, start, metaend)) | |
617ba13b | 108 | ext4_warning(sb, __FUNCTION__, |
2ae02107 MC |
109 | "Inode table (%llu-%llu) overlaps" |
110 | "GDT table (%llu-%llu)", | |
1939e49a RD |
111 | (unsigned long long)input->inode_table, |
112 | itend - 1, start, metaend - 1); | |
ac27a0ec DK |
113 | else |
114 | err = 0; | |
115 | brelse(bh); | |
116 | ||
117 | return err; | |
118 | } | |
119 | ||
120 | static struct buffer_head *bclean(handle_t *handle, struct super_block *sb, | |
617ba13b | 121 | ext4_fsblk_t blk) |
ac27a0ec DK |
122 | { |
123 | struct buffer_head *bh; | |
124 | int err; | |
125 | ||
126 | bh = sb_getblk(sb, blk); | |
127 | if (!bh) | |
128 | return ERR_PTR(-EIO); | |
617ba13b | 129 | if ((err = ext4_journal_get_write_access(handle, bh))) { |
ac27a0ec DK |
130 | brelse(bh); |
131 | bh = ERR_PTR(err); | |
132 | } else { | |
133 | lock_buffer(bh); | |
134 | memset(bh->b_data, 0, sb->s_blocksize); | |
135 | set_buffer_uptodate(bh); | |
136 | unlock_buffer(bh); | |
137 | } | |
138 | ||
139 | return bh; | |
140 | } | |
141 | ||
142 | /* | |
143 | * To avoid calling the atomic setbit hundreds or thousands of times, we only | |
144 | * need to use it within a single byte (to ensure we get endianness right). | |
145 | * We can use memset for the rest of the bitmap as there are no other users. | |
146 | */ | |
147 | static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap) | |
148 | { | |
149 | int i; | |
150 | ||
151 | if (start_bit >= end_bit) | |
152 | return; | |
153 | ||
617ba13b | 154 | ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit); |
ac27a0ec | 155 | for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) |
617ba13b | 156 | ext4_set_bit(i, bitmap); |
ac27a0ec DK |
157 | if (i < end_bit) |
158 | memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); | |
159 | } | |
160 | ||
161 | /* | |
162 | * Set up the block and inode bitmaps, and the inode table for the new group. | |
163 | * This doesn't need to be part of the main transaction, since we are only | |
164 | * changing blocks outside the actual filesystem. We still do journaling to | |
165 | * ensure the recovery is correct in case of a failure just after resize. | |
166 | * If any part of this fails, we simply abort the resize. | |
167 | */ | |
168 | static int setup_new_group_blocks(struct super_block *sb, | |
617ba13b | 169 | struct ext4_new_group_data *input) |
ac27a0ec | 170 | { |
617ba13b MC |
171 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
172 | ext4_fsblk_t start = ext4_group_first_block_no(sb, input->group); | |
173 | int reserved_gdb = ext4_bg_has_super(sb, input->group) ? | |
ac27a0ec | 174 | le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0; |
617ba13b | 175 | unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group); |
ac27a0ec DK |
176 | struct buffer_head *bh; |
177 | handle_t *handle; | |
617ba13b MC |
178 | ext4_fsblk_t block; |
179 | ext4_grpblk_t bit; | |
ac27a0ec DK |
180 | int i; |
181 | int err = 0, err2; | |
182 | ||
617ba13b | 183 | handle = ext4_journal_start_sb(sb, reserved_gdb + gdblocks + |
ac27a0ec DK |
184 | 2 + sbi->s_itb_per_group); |
185 | if (IS_ERR(handle)) | |
186 | return PTR_ERR(handle); | |
187 | ||
188 | lock_super(sb); | |
189 | if (input->group != sbi->s_groups_count) { | |
190 | err = -EBUSY; | |
191 | goto exit_journal; | |
192 | } | |
193 | ||
194 | if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) { | |
195 | err = PTR_ERR(bh); | |
196 | goto exit_journal; | |
197 | } | |
198 | ||
617ba13b MC |
199 | if (ext4_bg_has_super(sb, input->group)) { |
200 | ext4_debug("mark backup superblock %#04lx (+0)\n", start); | |
201 | ext4_set_bit(0, bh->b_data); | |
ac27a0ec DK |
202 | } |
203 | ||
204 | /* Copy all of the GDT blocks into the backup in this group */ | |
205 | for (i = 0, bit = 1, block = start + 1; | |
206 | i < gdblocks; i++, block++, bit++) { | |
207 | struct buffer_head *gdb; | |
208 | ||
617ba13b | 209 | ext4_debug("update backup group %#04lx (+%d)\n", block, bit); |
ac27a0ec DK |
210 | |
211 | gdb = sb_getblk(sb, block); | |
212 | if (!gdb) { | |
213 | err = -EIO; | |
214 | goto exit_bh; | |
215 | } | |
617ba13b | 216 | if ((err = ext4_journal_get_write_access(handle, gdb))) { |
ac27a0ec DK |
217 | brelse(gdb); |
218 | goto exit_bh; | |
219 | } | |
220 | lock_buffer(bh); | |
221 | memcpy(gdb->b_data, sbi->s_group_desc[i]->b_data, bh->b_size); | |
222 | set_buffer_uptodate(gdb); | |
223 | unlock_buffer(bh); | |
617ba13b MC |
224 | ext4_journal_dirty_metadata(handle, gdb); |
225 | ext4_set_bit(bit, bh->b_data); | |
ac27a0ec DK |
226 | brelse(gdb); |
227 | } | |
228 | ||
229 | /* Zero out all of the reserved backup group descriptor table blocks */ | |
230 | for (i = 0, bit = gdblocks + 1, block = start + bit; | |
231 | i < reserved_gdb; i++, block++, bit++) { | |
232 | struct buffer_head *gdb; | |
233 | ||
617ba13b | 234 | ext4_debug("clear reserved block %#04lx (+%d)\n", block, bit); |
ac27a0ec DK |
235 | |
236 | if (IS_ERR(gdb = bclean(handle, sb, block))) { | |
237 | err = PTR_ERR(bh); | |
238 | goto exit_bh; | |
239 | } | |
617ba13b MC |
240 | ext4_journal_dirty_metadata(handle, gdb); |
241 | ext4_set_bit(bit, bh->b_data); | |
ac27a0ec DK |
242 | brelse(gdb); |
243 | } | |
617ba13b | 244 | ext4_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap, |
ac27a0ec | 245 | input->block_bitmap - start); |
617ba13b MC |
246 | ext4_set_bit(input->block_bitmap - start, bh->b_data); |
247 | ext4_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap, | |
ac27a0ec | 248 | input->inode_bitmap - start); |
617ba13b | 249 | ext4_set_bit(input->inode_bitmap - start, bh->b_data); |
ac27a0ec DK |
250 | |
251 | /* Zero out all of the inode table blocks */ | |
252 | for (i = 0, block = input->inode_table, bit = block - start; | |
253 | i < sbi->s_itb_per_group; i++, bit++, block++) { | |
254 | struct buffer_head *it; | |
255 | ||
617ba13b | 256 | ext4_debug("clear inode block %#04lx (+%d)\n", block, bit); |
ac27a0ec DK |
257 | if (IS_ERR(it = bclean(handle, sb, block))) { |
258 | err = PTR_ERR(it); | |
259 | goto exit_bh; | |
260 | } | |
617ba13b | 261 | ext4_journal_dirty_metadata(handle, it); |
ac27a0ec | 262 | brelse(it); |
617ba13b | 263 | ext4_set_bit(bit, bh->b_data); |
ac27a0ec | 264 | } |
617ba13b | 265 | mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb), |
ac27a0ec | 266 | bh->b_data); |
617ba13b | 267 | ext4_journal_dirty_metadata(handle, bh); |
ac27a0ec DK |
268 | brelse(bh); |
269 | ||
270 | /* Mark unused entries in inode bitmap used */ | |
617ba13b | 271 | ext4_debug("clear inode bitmap %#04x (+%ld)\n", |
ac27a0ec DK |
272 | input->inode_bitmap, input->inode_bitmap - start); |
273 | if (IS_ERR(bh = bclean(handle, sb, input->inode_bitmap))) { | |
274 | err = PTR_ERR(bh); | |
275 | goto exit_journal; | |
276 | } | |
277 | ||
617ba13b | 278 | mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb), |
ac27a0ec | 279 | bh->b_data); |
617ba13b | 280 | ext4_journal_dirty_metadata(handle, bh); |
ac27a0ec DK |
281 | exit_bh: |
282 | brelse(bh); | |
283 | ||
284 | exit_journal: | |
285 | unlock_super(sb); | |
617ba13b | 286 | if ((err2 = ext4_journal_stop(handle)) && !err) |
ac27a0ec DK |
287 | err = err2; |
288 | ||
289 | return err; | |
290 | } | |
291 | ||
bd81d8ee | 292 | |
ac27a0ec DK |
293 | /* |
294 | * Iterate through the groups which hold BACKUP superblock/GDT copies in an | |
617ba13b | 295 | * ext4 filesystem. The counters should be initialized to 1, 5, and 7 before |
ac27a0ec DK |
296 | * calling this for the first time. In a sparse filesystem it will be the |
297 | * sequence of powers of 3, 5, and 7: 1, 3, 5, 7, 9, 25, 27, 49, 81, ... | |
298 | * For a non-sparse filesystem it will be every group: 1, 2, 3, 4, ... | |
299 | */ | |
617ba13b | 300 | static unsigned ext4_list_backups(struct super_block *sb, unsigned *three, |
ac27a0ec DK |
301 | unsigned *five, unsigned *seven) |
302 | { | |
303 | unsigned *min = three; | |
304 | int mult = 3; | |
305 | unsigned ret; | |
306 | ||
617ba13b MC |
307 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, |
308 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { | |
ac27a0ec DK |
309 | ret = *min; |
310 | *min += 1; | |
311 | return ret; | |
312 | } | |
313 | ||
314 | if (*five < *min) { | |
315 | min = five; | |
316 | mult = 5; | |
317 | } | |
318 | if (*seven < *min) { | |
319 | min = seven; | |
320 | mult = 7; | |
321 | } | |
322 | ||
323 | ret = *min; | |
324 | *min *= mult; | |
325 | ||
326 | return ret; | |
327 | } | |
328 | ||
329 | /* | |
330 | * Check that all of the backup GDT blocks are held in the primary GDT block. | |
331 | * It is assumed that they are stored in group order. Returns the number of | |
332 | * groups in current filesystem that have BACKUPS, or -ve error code. | |
333 | */ | |
334 | static int verify_reserved_gdb(struct super_block *sb, | |
335 | struct buffer_head *primary) | |
336 | { | |
617ba13b MC |
337 | const ext4_fsblk_t blk = primary->b_blocknr; |
338 | const unsigned long end = EXT4_SB(sb)->s_groups_count; | |
ac27a0ec DK |
339 | unsigned three = 1; |
340 | unsigned five = 5; | |
341 | unsigned seven = 7; | |
342 | unsigned grp; | |
343 | __le32 *p = (__le32 *)primary->b_data; | |
344 | int gdbackups = 0; | |
345 | ||
617ba13b | 346 | while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) { |
bd81d8ee LV |
347 | if (le32_to_cpu(*p++) != |
348 | grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){ | |
617ba13b | 349 | ext4_warning(sb, __FUNCTION__, |
2ae02107 MC |
350 | "reserved GDT %llu" |
351 | " missing grp %d (%llu)", | |
ac27a0ec | 352 | blk, grp, |
bd81d8ee LV |
353 | grp * |
354 | (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) + | |
355 | blk); | |
ac27a0ec DK |
356 | return -EINVAL; |
357 | } | |
617ba13b | 358 | if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb)) |
ac27a0ec DK |
359 | return -EFBIG; |
360 | } | |
361 | ||
362 | return gdbackups; | |
363 | } | |
364 | ||
365 | /* | |
366 | * Called when we need to bring a reserved group descriptor table block into | |
367 | * use from the resize inode. The primary copy of the new GDT block currently | |
368 | * is an indirect block (under the double indirect block in the resize inode). | |
369 | * The new backup GDT blocks will be stored as leaf blocks in this indirect | |
370 | * block, in group order. Even though we know all the block numbers we need, | |
371 | * we check to ensure that the resize inode has actually reserved these blocks. | |
372 | * | |
373 | * Don't need to update the block bitmaps because the blocks are still in use. | |
374 | * | |
375 | * We get all of the error cases out of the way, so that we are sure to not | |
376 | * fail once we start modifying the data on disk, because JBD has no rollback. | |
377 | */ | |
378 | static int add_new_gdb(handle_t *handle, struct inode *inode, | |
617ba13b | 379 | struct ext4_new_group_data *input, |
ac27a0ec DK |
380 | struct buffer_head **primary) |
381 | { | |
382 | struct super_block *sb = inode->i_sb; | |
617ba13b MC |
383 | struct ext4_super_block *es = EXT4_SB(sb)->s_es; |
384 | unsigned long gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); | |
385 | ext4_fsblk_t gdblock = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + gdb_num; | |
ac27a0ec DK |
386 | struct buffer_head **o_group_desc, **n_group_desc; |
387 | struct buffer_head *dind; | |
388 | int gdbackups; | |
617ba13b | 389 | struct ext4_iloc iloc; |
ac27a0ec DK |
390 | __le32 *data; |
391 | int err; | |
392 | ||
393 | if (test_opt(sb, DEBUG)) | |
394 | printk(KERN_DEBUG | |
617ba13b | 395 | "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n", |
ac27a0ec DK |
396 | gdb_num); |
397 | ||
398 | /* | |
399 | * If we are not using the primary superblock/GDT copy don't resize, | |
400 | * because the user tools have no way of handling this. Probably a | |
401 | * bad time to do it anyways. | |
402 | */ | |
617ba13b MC |
403 | if (EXT4_SB(sb)->s_sbh->b_blocknr != |
404 | le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { | |
405 | ext4_warning(sb, __FUNCTION__, | |
ac27a0ec | 406 | "won't resize using backup superblock at %llu", |
617ba13b | 407 | (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr); |
ac27a0ec DK |
408 | return -EPERM; |
409 | } | |
410 | ||
411 | *primary = sb_bread(sb, gdblock); | |
412 | if (!*primary) | |
413 | return -EIO; | |
414 | ||
415 | if ((gdbackups = verify_reserved_gdb(sb, *primary)) < 0) { | |
416 | err = gdbackups; | |
417 | goto exit_bh; | |
418 | } | |
419 | ||
617ba13b | 420 | data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; |
ac27a0ec DK |
421 | dind = sb_bread(sb, le32_to_cpu(*data)); |
422 | if (!dind) { | |
423 | err = -EIO; | |
424 | goto exit_bh; | |
425 | } | |
426 | ||
427 | data = (__le32 *)dind->b_data; | |
617ba13b MC |
428 | if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) { |
429 | ext4_warning(sb, __FUNCTION__, | |
2ae02107 | 430 | "new group %u GDT block %llu not reserved", |
ac27a0ec DK |
431 | input->group, gdblock); |
432 | err = -EINVAL; | |
433 | goto exit_dind; | |
434 | } | |
435 | ||
617ba13b | 436 | if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh))) |
ac27a0ec DK |
437 | goto exit_dind; |
438 | ||
617ba13b | 439 | if ((err = ext4_journal_get_write_access(handle, *primary))) |
ac27a0ec DK |
440 | goto exit_sbh; |
441 | ||
617ba13b | 442 | if ((err = ext4_journal_get_write_access(handle, dind))) |
ac27a0ec DK |
443 | goto exit_primary; |
444 | ||
617ba13b MC |
445 | /* ext4_reserve_inode_write() gets a reference on the iloc */ |
446 | if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) | |
ac27a0ec DK |
447 | goto exit_dindj; |
448 | ||
449 | n_group_desc = kmalloc((gdb_num + 1) * sizeof(struct buffer_head *), | |
450 | GFP_KERNEL); | |
451 | if (!n_group_desc) { | |
452 | err = -ENOMEM; | |
617ba13b | 453 | ext4_warning (sb, __FUNCTION__, |
ac27a0ec DK |
454 | "not enough memory for %lu groups", gdb_num + 1); |
455 | goto exit_inode; | |
456 | } | |
457 | ||
458 | /* | |
459 | * Finally, we have all of the possible failures behind us... | |
460 | * | |
461 | * Remove new GDT block from inode double-indirect block and clear out | |
462 | * the new GDT block for use (which also "frees" the backup GDT blocks | |
463 | * from the reserved inode). We don't need to change the bitmaps for | |
464 | * these blocks, because they are marked as in-use from being in the | |
465 | * reserved inode, and will become GDT blocks (primary and backup). | |
466 | */ | |
617ba13b MC |
467 | data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0; |
468 | ext4_journal_dirty_metadata(handle, dind); | |
ac27a0ec DK |
469 | brelse(dind); |
470 | inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9; | |
617ba13b | 471 | ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 472 | memset((*primary)->b_data, 0, sb->s_blocksize); |
617ba13b | 473 | ext4_journal_dirty_metadata(handle, *primary); |
ac27a0ec | 474 | |
617ba13b | 475 | o_group_desc = EXT4_SB(sb)->s_group_desc; |
ac27a0ec | 476 | memcpy(n_group_desc, o_group_desc, |
617ba13b | 477 | EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *)); |
ac27a0ec | 478 | n_group_desc[gdb_num] = *primary; |
617ba13b MC |
479 | EXT4_SB(sb)->s_group_desc = n_group_desc; |
480 | EXT4_SB(sb)->s_gdb_count++; | |
ac27a0ec DK |
481 | kfree(o_group_desc); |
482 | ||
483 | es->s_reserved_gdt_blocks = | |
484 | cpu_to_le16(le16_to_cpu(es->s_reserved_gdt_blocks) - 1); | |
617ba13b | 485 | ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); |
ac27a0ec DK |
486 | |
487 | return 0; | |
488 | ||
489 | exit_inode: | |
617ba13b | 490 | //ext4_journal_release_buffer(handle, iloc.bh); |
ac27a0ec DK |
491 | brelse(iloc.bh); |
492 | exit_dindj: | |
617ba13b | 493 | //ext4_journal_release_buffer(handle, dind); |
ac27a0ec | 494 | exit_primary: |
617ba13b | 495 | //ext4_journal_release_buffer(handle, *primary); |
ac27a0ec | 496 | exit_sbh: |
617ba13b | 497 | //ext4_journal_release_buffer(handle, *primary); |
ac27a0ec DK |
498 | exit_dind: |
499 | brelse(dind); | |
500 | exit_bh: | |
501 | brelse(*primary); | |
502 | ||
617ba13b | 503 | ext4_debug("leaving with error %d\n", err); |
ac27a0ec DK |
504 | return err; |
505 | } | |
506 | ||
507 | /* | |
508 | * Called when we are adding a new group which has a backup copy of each of | |
509 | * the GDT blocks (i.e. sparse group) and there are reserved GDT blocks. | |
510 | * We need to add these reserved backup GDT blocks to the resize inode, so | |
511 | * that they are kept for future resizing and not allocated to files. | |
512 | * | |
513 | * Each reserved backup GDT block will go into a different indirect block. | |
514 | * The indirect blocks are actually the primary reserved GDT blocks, | |
515 | * so we know in advance what their block numbers are. We only get the | |
516 | * double-indirect block to verify it is pointing to the primary reserved | |
517 | * GDT blocks so we don't overwrite a data block by accident. The reserved | |
518 | * backup GDT blocks are stored in their reserved primary GDT block. | |
519 | */ | |
520 | static int reserve_backup_gdb(handle_t *handle, struct inode *inode, | |
617ba13b | 521 | struct ext4_new_group_data *input) |
ac27a0ec DK |
522 | { |
523 | struct super_block *sb = inode->i_sb; | |
617ba13b | 524 | int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks); |
ac27a0ec DK |
525 | struct buffer_head **primary; |
526 | struct buffer_head *dind; | |
617ba13b MC |
527 | struct ext4_iloc iloc; |
528 | ext4_fsblk_t blk; | |
ac27a0ec DK |
529 | __le32 *data, *end; |
530 | int gdbackups = 0; | |
531 | int res, i; | |
532 | int err; | |
533 | ||
534 | primary = kmalloc(reserved_gdb * sizeof(*primary), GFP_KERNEL); | |
535 | if (!primary) | |
536 | return -ENOMEM; | |
537 | ||
617ba13b | 538 | data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK; |
ac27a0ec DK |
539 | dind = sb_bread(sb, le32_to_cpu(*data)); |
540 | if (!dind) { | |
541 | err = -EIO; | |
542 | goto exit_free; | |
543 | } | |
544 | ||
617ba13b MC |
545 | blk = EXT4_SB(sb)->s_sbh->b_blocknr + 1 + EXT4_SB(sb)->s_gdb_count; |
546 | data = (__le32 *)dind->b_data + EXT4_SB(sb)->s_gdb_count; | |
547 | end = (__le32 *)dind->b_data + EXT4_ADDR_PER_BLOCK(sb); | |
ac27a0ec DK |
548 | |
549 | /* Get each reserved primary GDT block and verify it holds backups */ | |
550 | for (res = 0; res < reserved_gdb; res++, blk++) { | |
551 | if (le32_to_cpu(*data) != blk) { | |
617ba13b | 552 | ext4_warning(sb, __FUNCTION__, |
2ae02107 | 553 | "reserved block %llu" |
ac27a0ec DK |
554 | " not at offset %ld", |
555 | blk, | |
556 | (long)(data - (__le32 *)dind->b_data)); | |
557 | err = -EINVAL; | |
558 | goto exit_bh; | |
559 | } | |
560 | primary[res] = sb_bread(sb, blk); | |
561 | if (!primary[res]) { | |
562 | err = -EIO; | |
563 | goto exit_bh; | |
564 | } | |
565 | if ((gdbackups = verify_reserved_gdb(sb, primary[res])) < 0) { | |
566 | brelse(primary[res]); | |
567 | err = gdbackups; | |
568 | goto exit_bh; | |
569 | } | |
570 | if (++data >= end) | |
571 | data = (__le32 *)dind->b_data; | |
572 | } | |
573 | ||
574 | for (i = 0; i < reserved_gdb; i++) { | |
617ba13b | 575 | if ((err = ext4_journal_get_write_access(handle, primary[i]))) { |
ac27a0ec DK |
576 | /* |
577 | int j; | |
578 | for (j = 0; j < i; j++) | |
617ba13b | 579 | ext4_journal_release_buffer(handle, primary[j]); |
ac27a0ec DK |
580 | */ |
581 | goto exit_bh; | |
582 | } | |
583 | } | |
584 | ||
617ba13b | 585 | if ((err = ext4_reserve_inode_write(handle, inode, &iloc))) |
ac27a0ec DK |
586 | goto exit_bh; |
587 | ||
588 | /* | |
589 | * Finally we can add each of the reserved backup GDT blocks from | |
590 | * the new group to its reserved primary GDT block. | |
591 | */ | |
617ba13b | 592 | blk = input->group * EXT4_BLOCKS_PER_GROUP(sb); |
ac27a0ec DK |
593 | for (i = 0; i < reserved_gdb; i++) { |
594 | int err2; | |
595 | data = (__le32 *)primary[i]->b_data; | |
596 | /* printk("reserving backup %lu[%u] = %lu\n", | |
597 | primary[i]->b_blocknr, gdbackups, | |
598 | blk + primary[i]->b_blocknr); */ | |
599 | data[gdbackups] = cpu_to_le32(blk + primary[i]->b_blocknr); | |
617ba13b | 600 | err2 = ext4_journal_dirty_metadata(handle, primary[i]); |
ac27a0ec DK |
601 | if (!err) |
602 | err = err2; | |
603 | } | |
604 | inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9; | |
617ba13b | 605 | ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec DK |
606 | |
607 | exit_bh: | |
608 | while (--res >= 0) | |
609 | brelse(primary[res]); | |
610 | brelse(dind); | |
611 | ||
612 | exit_free: | |
613 | kfree(primary); | |
614 | ||
615 | return err; | |
616 | } | |
617 | ||
618 | /* | |
617ba13b | 619 | * Update the backup copies of the ext4 metadata. These don't need to be part |
ac27a0ec DK |
620 | * of the main resize transaction, because e2fsck will re-write them if there |
621 | * is a problem (basically only OOM will cause a problem). However, we | |
622 | * _should_ update the backups if possible, in case the primary gets trashed | |
623 | * for some reason and we need to run e2fsck from a backup superblock. The | |
624 | * important part is that the new block and inode counts are in the backup | |
625 | * superblocks, and the location of the new group metadata in the GDT backups. | |
626 | * | |
627 | * We do not need lock_super() for this, because these blocks are not | |
628 | * otherwise touched by the filesystem code when it is mounted. We don't | |
629 | * need to worry about last changing from sbi->s_groups_count, because the | |
630 | * worst that can happen is that we do not copy the full number of backups | |
631 | * at this time. The resize which changed s_groups_count will backup again. | |
632 | */ | |
633 | static void update_backups(struct super_block *sb, | |
634 | int blk_off, char *data, int size) | |
635 | { | |
617ba13b | 636 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
ac27a0ec | 637 | const unsigned long last = sbi->s_groups_count; |
617ba13b | 638 | const int bpg = EXT4_BLOCKS_PER_GROUP(sb); |
ac27a0ec DK |
639 | unsigned three = 1; |
640 | unsigned five = 5; | |
641 | unsigned seven = 7; | |
642 | unsigned group; | |
643 | int rest = sb->s_blocksize - size; | |
644 | handle_t *handle; | |
645 | int err = 0, err2; | |
646 | ||
617ba13b | 647 | handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA); |
ac27a0ec DK |
648 | if (IS_ERR(handle)) { |
649 | group = 1; | |
650 | err = PTR_ERR(handle); | |
651 | goto exit_err; | |
652 | } | |
653 | ||
617ba13b | 654 | while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) { |
ac27a0ec DK |
655 | struct buffer_head *bh; |
656 | ||
657 | /* Out of journal space, and can't get more - abort - so sad */ | |
658 | if (handle->h_buffer_credits == 0 && | |
617ba13b MC |
659 | ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) && |
660 | (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA))) | |
ac27a0ec DK |
661 | break; |
662 | ||
663 | bh = sb_getblk(sb, group * bpg + blk_off); | |
664 | if (!bh) { | |
665 | err = -EIO; | |
666 | break; | |
667 | } | |
617ba13b | 668 | ext4_debug("update metadata backup %#04lx\n", |
ac27a0ec | 669 | (unsigned long)bh->b_blocknr); |
617ba13b | 670 | if ((err = ext4_journal_get_write_access(handle, bh))) |
ac27a0ec DK |
671 | break; |
672 | lock_buffer(bh); | |
673 | memcpy(bh->b_data, data, size); | |
674 | if (rest) | |
675 | memset(bh->b_data + size, 0, rest); | |
676 | set_buffer_uptodate(bh); | |
677 | unlock_buffer(bh); | |
617ba13b | 678 | ext4_journal_dirty_metadata(handle, bh); |
ac27a0ec DK |
679 | brelse(bh); |
680 | } | |
617ba13b | 681 | if ((err2 = ext4_journal_stop(handle)) && !err) |
ac27a0ec DK |
682 | err = err2; |
683 | ||
684 | /* | |
685 | * Ugh! Need to have e2fsck write the backup copies. It is too | |
686 | * late to revert the resize, we shouldn't fail just because of | |
687 | * the backup copies (they are only needed in case of corruption). | |
688 | * | |
689 | * However, if we got here we have a journal problem too, so we | |
690 | * can't really start a transaction to mark the superblock. | |
691 | * Chicken out and just set the flag on the hope it will be written | |
692 | * to disk, and if not - we will simply wait until next fsck. | |
693 | */ | |
694 | exit_err: | |
695 | if (err) { | |
617ba13b | 696 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
697 | "can't update backup for group %d (err %d), " |
698 | "forcing fsck on next reboot", group, err); | |
617ba13b MC |
699 | sbi->s_mount_state &= ~EXT4_VALID_FS; |
700 | sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS); | |
ac27a0ec DK |
701 | mark_buffer_dirty(sbi->s_sbh); |
702 | } | |
703 | } | |
704 | ||
705 | /* Add group descriptor data to an existing or new group descriptor block. | |
706 | * Ensure we handle all possible error conditions _before_ we start modifying | |
707 | * the filesystem, because we cannot abort the transaction and not have it | |
708 | * write the data to disk. | |
709 | * | |
710 | * If we are on a GDT block boundary, we need to get the reserved GDT block. | |
711 | * Otherwise, we may need to add backup GDT blocks for a sparse group. | |
712 | * | |
713 | * We only need to hold the superblock lock while we are actually adding | |
714 | * in the new group's counts to the superblock. Prior to that we have | |
715 | * not really "added" the group at all. We re-check that we are still | |
716 | * adding in the last group in case things have changed since verifying. | |
717 | */ | |
617ba13b | 718 | int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input) |
ac27a0ec | 719 | { |
617ba13b MC |
720 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
721 | struct ext4_super_block *es = sbi->s_es; | |
722 | int reserved_gdb = ext4_bg_has_super(sb, input->group) ? | |
ac27a0ec DK |
723 | le16_to_cpu(es->s_reserved_gdt_blocks) : 0; |
724 | struct buffer_head *primary = NULL; | |
617ba13b | 725 | struct ext4_group_desc *gdp; |
ac27a0ec DK |
726 | struct inode *inode = NULL; |
727 | handle_t *handle; | |
728 | int gdb_off, gdb_num; | |
729 | int err, err2; | |
730 | ||
617ba13b MC |
731 | gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb); |
732 | gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb); | |
ac27a0ec | 733 | |
617ba13b MC |
734 | if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb, |
735 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) { | |
736 | ext4_warning(sb, __FUNCTION__, | |
ac27a0ec DK |
737 | "Can't resize non-sparse filesystem further"); |
738 | return -EPERM; | |
739 | } | |
740 | ||
bd81d8ee LV |
741 | if (ext4_blocks_count(es) + input->blocks_count < |
742 | ext4_blocks_count(es)) { | |
617ba13b | 743 | ext4_warning(sb, __FUNCTION__, "blocks_count overflow\n"); |
ac27a0ec DK |
744 | return -EINVAL; |
745 | } | |
746 | ||
617ba13b | 747 | if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) < |
ac27a0ec | 748 | le32_to_cpu(es->s_inodes_count)) { |
617ba13b | 749 | ext4_warning(sb, __FUNCTION__, "inodes_count overflow\n"); |
ac27a0ec DK |
750 | return -EINVAL; |
751 | } | |
752 | ||
753 | if (reserved_gdb || gdb_off == 0) { | |
617ba13b MC |
754 | if (!EXT4_HAS_COMPAT_FEATURE(sb, |
755 | EXT4_FEATURE_COMPAT_RESIZE_INODE)){ | |
756 | ext4_warning(sb, __FUNCTION__, | |
ac27a0ec DK |
757 | "No reserved GDT blocks, can't resize"); |
758 | return -EPERM; | |
759 | } | |
617ba13b | 760 | inode = iget(sb, EXT4_RESIZE_INO); |
ac27a0ec | 761 | if (!inode || is_bad_inode(inode)) { |
617ba13b | 762 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
763 | "Error opening resize inode"); |
764 | iput(inode); | |
765 | return -ENOENT; | |
766 | } | |
767 | } | |
768 | ||
769 | if ((err = verify_group_input(sb, input))) | |
770 | goto exit_put; | |
771 | ||
772 | if ((err = setup_new_group_blocks(sb, input))) | |
773 | goto exit_put; | |
774 | ||
775 | /* | |
776 | * We will always be modifying at least the superblock and a GDT | |
777 | * block. If we are adding a group past the last current GDT block, | |
778 | * we will also modify the inode and the dindirect block. If we | |
779 | * are adding a group with superblock/GDT backups we will also | |
780 | * modify each of the reserved GDT dindirect blocks. | |
781 | */ | |
617ba13b MC |
782 | handle = ext4_journal_start_sb(sb, |
783 | ext4_bg_has_super(sb, input->group) ? | |
ac27a0ec DK |
784 | 3 + reserved_gdb : 4); |
785 | if (IS_ERR(handle)) { | |
786 | err = PTR_ERR(handle); | |
787 | goto exit_put; | |
788 | } | |
789 | ||
790 | lock_super(sb); | |
791 | if (input->group != sbi->s_groups_count) { | |
617ba13b | 792 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
793 | "multiple resizers run on filesystem!"); |
794 | err = -EBUSY; | |
795 | goto exit_journal; | |
796 | } | |
797 | ||
617ba13b | 798 | if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh))) |
ac27a0ec DK |
799 | goto exit_journal; |
800 | ||
801 | /* | |
802 | * We will only either add reserved group blocks to a backup group | |
803 | * or remove reserved blocks for the first group in a new group block. | |
804 | * Doing both would be mean more complex code, and sane people don't | |
805 | * use non-sparse filesystems anymore. This is already checked above. | |
806 | */ | |
807 | if (gdb_off) { | |
808 | primary = sbi->s_group_desc[gdb_num]; | |
617ba13b | 809 | if ((err = ext4_journal_get_write_access(handle, primary))) |
ac27a0ec DK |
810 | goto exit_journal; |
811 | ||
617ba13b | 812 | if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) && |
ac27a0ec DK |
813 | (err = reserve_backup_gdb(handle, inode, input))) |
814 | goto exit_journal; | |
815 | } else if ((err = add_new_gdb(handle, inode, input, &primary))) | |
816 | goto exit_journal; | |
817 | ||
818 | /* | |
819 | * OK, now we've set up the new group. Time to make it active. | |
820 | * | |
821 | * Current kernels don't lock all allocations via lock_super(), | |
822 | * so we have to be safe wrt. concurrent accesses the group | |
823 | * data. So we need to be careful to set all of the relevant | |
824 | * group descriptor data etc. *before* we enable the group. | |
825 | * | |
826 | * The key field here is sbi->s_groups_count: as long as | |
827 | * that retains its old value, nobody is going to access the new | |
828 | * group. | |
829 | * | |
830 | * So first we update all the descriptor metadata for the new | |
831 | * group; then we update the total disk blocks count; then we | |
832 | * update the groups count to enable the group; then finally we | |
833 | * update the free space counts so that the system can start | |
834 | * using the new disk blocks. | |
835 | */ | |
836 | ||
837 | /* Update group descriptor block for new group */ | |
617ba13b | 838 | gdp = (struct ext4_group_desc *)primary->b_data + gdb_off; |
ac27a0ec | 839 | |
8fadc143 AR |
840 | ext4_block_bitmap_set(sb, gdp, input->block_bitmap); /* LV FIXME */ |
841 | ext4_inode_bitmap_set(sb, gdp, input->inode_bitmap); /* LV FIXME */ | |
842 | ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */ | |
ac27a0ec | 843 | gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count); |
617ba13b | 844 | gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb)); |
ac27a0ec DK |
845 | |
846 | /* | |
847 | * Make the new blocks and inodes valid next. We do this before | |
848 | * increasing the group count so that once the group is enabled, | |
849 | * all of its blocks and inodes are already valid. | |
850 | * | |
851 | * We always allocate group-by-group, then block-by-block or | |
852 | * inode-by-inode within a group, so enabling these | |
853 | * blocks/inodes before the group is live won't actually let us | |
854 | * allocate the new space yet. | |
855 | */ | |
bd81d8ee | 856 | ext4_blocks_count_set(es, ext4_blocks_count(es) + |
ac27a0ec DK |
857 | input->blocks_count); |
858 | es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) + | |
617ba13b | 859 | EXT4_INODES_PER_GROUP(sb)); |
ac27a0ec DK |
860 | |
861 | /* | |
862 | * We need to protect s_groups_count against other CPUs seeing | |
863 | * inconsistent state in the superblock. | |
864 | * | |
865 | * The precise rules we use are: | |
866 | * | |
867 | * * Writers of s_groups_count *must* hold lock_super | |
868 | * AND | |
869 | * * Writers must perform a smp_wmb() after updating all dependent | |
870 | * data and before modifying the groups count | |
871 | * | |
872 | * * Readers must hold lock_super() over the access | |
873 | * OR | |
874 | * * Readers must perform an smp_rmb() after reading the groups count | |
875 | * and before reading any dependent data. | |
876 | * | |
877 | * NB. These rules can be relaxed when checking the group count | |
878 | * while freeing data, as we can only allocate from a block | |
879 | * group after serialising against the group count, and we can | |
880 | * only then free after serialising in turn against that | |
881 | * allocation. | |
882 | */ | |
883 | smp_wmb(); | |
884 | ||
885 | /* Update the global fs size fields */ | |
886 | sbi->s_groups_count++; | |
887 | ||
617ba13b | 888 | ext4_journal_dirty_metadata(handle, primary); |
ac27a0ec DK |
889 | |
890 | /* Update the reserved block counts only once the new group is | |
891 | * active. */ | |
bd81d8ee | 892 | ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) + |
ac27a0ec DK |
893 | input->reserved_blocks); |
894 | ||
895 | /* Update the free space counts */ | |
896 | percpu_counter_mod(&sbi->s_freeblocks_counter, | |
897 | input->free_blocks_count); | |
898 | percpu_counter_mod(&sbi->s_freeinodes_counter, | |
617ba13b | 899 | EXT4_INODES_PER_GROUP(sb)); |
ac27a0ec | 900 | |
617ba13b | 901 | ext4_journal_dirty_metadata(handle, sbi->s_sbh); |
ac27a0ec DK |
902 | sb->s_dirt = 1; |
903 | ||
904 | exit_journal: | |
905 | unlock_super(sb); | |
617ba13b | 906 | if ((err2 = ext4_journal_stop(handle)) && !err) |
ac27a0ec DK |
907 | err = err2; |
908 | if (!err) { | |
909 | update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es, | |
617ba13b | 910 | sizeof(struct ext4_super_block)); |
ac27a0ec DK |
911 | update_backups(sb, primary->b_blocknr, primary->b_data, |
912 | primary->b_size); | |
913 | } | |
914 | exit_put: | |
915 | iput(inode); | |
916 | return err; | |
617ba13b | 917 | } /* ext4_group_add */ |
ac27a0ec DK |
918 | |
919 | /* Extend the filesystem to the new number of blocks specified. This entry | |
920 | * point is only used to extend the current filesystem to the end of the last | |
921 | * existing group. It can be accessed via ioctl, or by "remount,resize=<size>" | |
922 | * for emergencies (because it has no dependencies on reserved blocks). | |
923 | * | |
617ba13b | 924 | * If we _really_ wanted, we could use default values to call ext4_group_add() |
ac27a0ec DK |
925 | * allow the "remount" trick to work for arbitrary resizing, assuming enough |
926 | * GDT blocks are reserved to grow to the desired size. | |
927 | */ | |
617ba13b MC |
928 | int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es, |
929 | ext4_fsblk_t n_blocks_count) | |
ac27a0ec | 930 | { |
617ba13b | 931 | ext4_fsblk_t o_blocks_count; |
ac27a0ec | 932 | unsigned long o_groups_count; |
617ba13b MC |
933 | ext4_grpblk_t last; |
934 | ext4_grpblk_t add; | |
ac27a0ec DK |
935 | struct buffer_head * bh; |
936 | handle_t *handle; | |
937 | int err; | |
938 | unsigned long freed_blocks; | |
939 | ||
940 | /* We don't need to worry about locking wrt other resizers just | |
941 | * yet: we're going to revalidate es->s_blocks_count after | |
942 | * taking lock_super() below. */ | |
bd81d8ee | 943 | o_blocks_count = ext4_blocks_count(es); |
617ba13b | 944 | o_groups_count = EXT4_SB(sb)->s_groups_count; |
ac27a0ec DK |
945 | |
946 | if (test_opt(sb, DEBUG)) | |
2ae02107 | 947 | printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n", |
ac27a0ec DK |
948 | o_blocks_count, n_blocks_count); |
949 | ||
950 | if (n_blocks_count == 0 || n_blocks_count == o_blocks_count) | |
951 | return 0; | |
952 | ||
953 | if (n_blocks_count > (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) { | |
617ba13b | 954 | printk(KERN_ERR "EXT4-fs: filesystem on %s:" |
2ae02107 | 955 | " too large to resize to %llu blocks safely\n", |
ac27a0ec DK |
956 | sb->s_id, n_blocks_count); |
957 | if (sizeof(sector_t) < 8) | |
617ba13b | 958 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
959 | "CONFIG_LBD not enabled\n"); |
960 | return -EINVAL; | |
961 | } | |
962 | ||
963 | if (n_blocks_count < o_blocks_count) { | |
617ba13b | 964 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
965 | "can't shrink FS - resize aborted"); |
966 | return -EBUSY; | |
967 | } | |
968 | ||
969 | /* Handle the remaining blocks in the last group only. */ | |
3a5b2ecd | 970 | ext4_get_group_no_and_offset(sb, o_blocks_count, NULL, &last); |
ac27a0ec DK |
971 | |
972 | if (last == 0) { | |
617ba13b | 973 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
974 | "need to use ext2online to resize further"); |
975 | return -EPERM; | |
976 | } | |
977 | ||
617ba13b | 978 | add = EXT4_BLOCKS_PER_GROUP(sb) - last; |
ac27a0ec DK |
979 | |
980 | if (o_blocks_count + add < o_blocks_count) { | |
617ba13b | 981 | ext4_warning(sb, __FUNCTION__, "blocks_count overflow"); |
ac27a0ec DK |
982 | return -EINVAL; |
983 | } | |
984 | ||
985 | if (o_blocks_count + add > n_blocks_count) | |
986 | add = n_blocks_count - o_blocks_count; | |
987 | ||
988 | if (o_blocks_count + add < n_blocks_count) | |
617ba13b | 989 | ext4_warning(sb, __FUNCTION__, |
2ae02107 | 990 | "will only finish group (%llu" |
ac27a0ec DK |
991 | " blocks, %u new)", |
992 | o_blocks_count + add, add); | |
993 | ||
994 | /* See if the device is actually as big as what was requested */ | |
995 | bh = sb_bread(sb, o_blocks_count + add -1); | |
996 | if (!bh) { | |
617ba13b | 997 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
998 | "can't read last block, resize aborted"); |
999 | return -ENOSPC; | |
1000 | } | |
1001 | brelse(bh); | |
1002 | ||
1003 | /* We will update the superblock, one block bitmap, and | |
617ba13b | 1004 | * one group descriptor via ext4_free_blocks(). |
ac27a0ec | 1005 | */ |
617ba13b | 1006 | handle = ext4_journal_start_sb(sb, 3); |
ac27a0ec DK |
1007 | if (IS_ERR(handle)) { |
1008 | err = PTR_ERR(handle); | |
617ba13b | 1009 | ext4_warning(sb, __FUNCTION__, "error %d on journal start",err); |
ac27a0ec DK |
1010 | goto exit_put; |
1011 | } | |
1012 | ||
1013 | lock_super(sb); | |
bd81d8ee | 1014 | if (o_blocks_count != ext4_blocks_count(es)) { |
617ba13b | 1015 | ext4_warning(sb, __FUNCTION__, |
ac27a0ec DK |
1016 | "multiple resizers run on filesystem!"); |
1017 | unlock_super(sb); | |
1018 | err = -EBUSY; | |
1019 | goto exit_put; | |
1020 | } | |
1021 | ||
617ba13b MC |
1022 | if ((err = ext4_journal_get_write_access(handle, |
1023 | EXT4_SB(sb)->s_sbh))) { | |
1024 | ext4_warning(sb, __FUNCTION__, | |
ac27a0ec DK |
1025 | "error %d on journal write access", err); |
1026 | unlock_super(sb); | |
617ba13b | 1027 | ext4_journal_stop(handle); |
ac27a0ec DK |
1028 | goto exit_put; |
1029 | } | |
bd81d8ee | 1030 | ext4_blocks_count_set(es, o_blocks_count + add); |
617ba13b | 1031 | ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); |
ac27a0ec DK |
1032 | sb->s_dirt = 1; |
1033 | unlock_super(sb); | |
2ae02107 | 1034 | ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count, |
ac27a0ec | 1035 | o_blocks_count + add); |
617ba13b | 1036 | ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks); |
2ae02107 | 1037 | ext4_debug("freed blocks %llu through %llu\n", o_blocks_count, |
ac27a0ec | 1038 | o_blocks_count + add); |
617ba13b | 1039 | if ((err = ext4_journal_stop(handle))) |
ac27a0ec DK |
1040 | goto exit_put; |
1041 | if (test_opt(sb, DEBUG)) | |
bd81d8ee LV |
1042 | printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n", |
1043 | ext4_blocks_count(es)); | |
617ba13b MC |
1044 | update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es, |
1045 | sizeof(struct ext4_super_block)); | |
ac27a0ec DK |
1046 | exit_put: |
1047 | return err; | |
617ba13b | 1048 | } /* ext4_group_extend */ |