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