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