Merge branch 'hwmon-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[linux-2.6] / fs / ext4 / resize.c
1 /*
2  *  linux/fs/ext4/resize.c
3  *
4  * Support for resizing an ext4 filesystem while it is mounted.
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
12 #define EXT4FS_DEBUG
13
14 #include <linux/smp_lock.h>
15 #include <linux/ext4_jbd2.h>
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,
25                               struct ext4_new_group_data *input)
26 {
27         struct ext4_sb_info *sbi = EXT4_SB(sb);
28         struct ext4_super_block *es = sbi->s_es;
29         ext4_fsblk_t start = ext4_blocks_count(es);
30         ext4_fsblk_t end = start + input->blocks_count;
31         unsigned group = input->group;
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) +
35                  le16_to_cpu(es->s_reserved_gdt_blocks)) : 0;
36         ext4_fsblk_t metaend = start + overhead;
37         struct buffer_head *bh = NULL;
38         ext4_grpblk_t free_blocks_count, offset;
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))
45                 printk(KERN_DEBUG "EXT4-fs: adding %s group %u: %u blocks "
46                        "(%d free, %u reserved)\n",
47                        ext4_bg_has_super(sb, input->group) ? "normal" :
48                        "no-super", input->group, input->blocks_count,
49                        free_blocks_count, input->reserved_blocks);
50
51         ext4_get_group_no_and_offset(sb, start, NULL, &offset);
52         if (group != sbi->s_groups_count)
53                 ext4_warning(sb, __FUNCTION__,
54                              "Cannot add at group %u (only %lu groups)",
55                              input->group, sbi->s_groups_count);
56         else if (offset != 0)
57                         ext4_warning(sb, __FUNCTION__, "Last group not full");
58         else if (input->reserved_blocks > input->blocks_count / 5)
59                 ext4_warning(sb, __FUNCTION__, "Reserved blocks too high (%u)",
60                              input->reserved_blocks);
61         else if (free_blocks_count < 0)
62                 ext4_warning(sb, __FUNCTION__, "Bad blocks count %u",
63                              input->blocks_count);
64         else if (!(bh = sb_bread(sb, end - 1)))
65                 ext4_warning(sb, __FUNCTION__,
66                              "Cannot read last block (%llu)",
67                              end - 1);
68         else if (outside(input->block_bitmap, start, end))
69                 ext4_warning(sb, __FUNCTION__,
70                              "Block bitmap not in group (block %llu)",
71                              (unsigned long long)input->block_bitmap);
72         else if (outside(input->inode_bitmap, start, end))
73                 ext4_warning(sb, __FUNCTION__,
74                              "Inode bitmap not in group (block %llu)",
75                              (unsigned long long)input->inode_bitmap);
76         else if (outside(input->inode_table, start, end) ||
77                  outside(itend - 1, start, end))
78                 ext4_warning(sb, __FUNCTION__,
79                              "Inode table not in group (blocks %llu-%llu)",
80                              (unsigned long long)input->inode_table, itend - 1);
81         else if (input->inode_bitmap == input->block_bitmap)
82                 ext4_warning(sb, __FUNCTION__,
83                              "Block bitmap same as inode bitmap (%llu)",
84                              (unsigned long long)input->block_bitmap);
85         else if (inside(input->block_bitmap, input->inode_table, itend))
86                 ext4_warning(sb, __FUNCTION__,
87                              "Block bitmap (%llu) in inode table (%llu-%llu)",
88                              (unsigned long long)input->block_bitmap,
89                              (unsigned long long)input->inode_table, itend - 1);
90         else if (inside(input->inode_bitmap, input->inode_table, itend))
91                 ext4_warning(sb, __FUNCTION__,
92                              "Inode bitmap (%llu) in inode table (%llu-%llu)",
93                              (unsigned long long)input->inode_bitmap,
94                              (unsigned long long)input->inode_table, itend - 1);
95         else if (inside(input->block_bitmap, start, metaend))
96                 ext4_warning(sb, __FUNCTION__,
97                              "Block bitmap (%llu) in GDT table"
98                              " (%llu-%llu)",
99                              (unsigned long long)input->block_bitmap,
100                              start, metaend - 1);
101         else if (inside(input->inode_bitmap, start, metaend))
102                 ext4_warning(sb, __FUNCTION__,
103                              "Inode bitmap (%llu) in GDT table"
104                              " (%llu-%llu)",
105                              (unsigned long long)input->inode_bitmap,
106                              start, metaend - 1);
107         else if (inside(input->inode_table, start, metaend) ||
108                  inside(itend - 1, start, metaend))
109                 ext4_warning(sb, __FUNCTION__,
110                              "Inode table (%llu-%llu) overlaps"
111                              "GDT table (%llu-%llu)",
112                              (unsigned long long)input->inode_table,
113                              itend - 1, start, metaend - 1);
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,
122                                   ext4_fsblk_t blk)
123 {
124         struct buffer_head *bh;
125         int err;
126
127         bh = sb_getblk(sb, blk);
128         if (!bh)
129                 return ERR_PTR(-EIO);
130         if ((err = ext4_journal_get_write_access(handle, bh))) {
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
155         ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
156         for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
157                 ext4_set_bit(i, bitmap);
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,
170                                   struct ext4_new_group_data *input)
171 {
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) ?
175                 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0;
176         unsigned long gdblocks = ext4_bg_num_gdb(sb, input->group);
177         struct buffer_head *bh;
178         handle_t *handle;
179         ext4_fsblk_t block;
180         ext4_grpblk_t bit;
181         int i;
182         int err = 0, err2;
183
184         handle = ext4_journal_start_sb(sb, reserved_gdb + gdblocks +
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
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);
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
210                 ext4_debug("update backup group %#04lx (+%d)\n", block, bit);
211
212                 gdb = sb_getblk(sb, block);
213                 if (!gdb) {
214                         err = -EIO;
215                         goto exit_bh;
216                 }
217                 if ((err = ext4_journal_get_write_access(handle, gdb))) {
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);
225                 ext4_journal_dirty_metadata(handle, gdb);
226                 ext4_set_bit(bit, bh->b_data);
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
235                 ext4_debug("clear reserved block %#04lx (+%d)\n", block, bit);
236
237                 if (IS_ERR(gdb = bclean(handle, sb, block))) {
238                         err = PTR_ERR(bh);
239                         goto exit_bh;
240                 }
241                 ext4_journal_dirty_metadata(handle, gdb);
242                 ext4_set_bit(bit, bh->b_data);
243                 brelse(gdb);
244         }
245         ext4_debug("mark block bitmap %#04x (+%ld)\n", input->block_bitmap,
246                    input->block_bitmap - start);
247         ext4_set_bit(input->block_bitmap - start, bh->b_data);
248         ext4_debug("mark inode bitmap %#04x (+%ld)\n", input->inode_bitmap,
249                    input->inode_bitmap - start);
250         ext4_set_bit(input->inode_bitmap - start, bh->b_data);
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
257                 ext4_debug("clear inode block %#04lx (+%d)\n", block, bit);
258                 if (IS_ERR(it = bclean(handle, sb, block))) {
259                         err = PTR_ERR(it);
260                         goto exit_bh;
261                 }
262                 ext4_journal_dirty_metadata(handle, it);
263                 brelse(it);
264                 ext4_set_bit(bit, bh->b_data);
265         }
266         mark_bitmap_end(input->blocks_count, EXT4_BLOCKS_PER_GROUP(sb),
267                         bh->b_data);
268         ext4_journal_dirty_metadata(handle, bh);
269         brelse(bh);
270
271         /* Mark unused entries in inode bitmap used */
272         ext4_debug("clear inode bitmap %#04x (+%ld)\n",
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
279         mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
280                         bh->b_data);
281         ext4_journal_dirty_metadata(handle, bh);
282 exit_bh:
283         brelse(bh);
284
285 exit_journal:
286         unlock_super(sb);
287         if ((err2 = ext4_journal_stop(handle)) && !err)
288                 err = err2;
289
290         return err;
291 }
292
293
294 /*
295  * Iterate through the groups which hold BACKUP superblock/GDT copies in an
296  * ext4 filesystem.  The counters should be initialized to 1, 5, and 7 before
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  */
301 static unsigned ext4_list_backups(struct super_block *sb, unsigned *three,
302                                   unsigned *five, unsigned *seven)
303 {
304         unsigned *min = three;
305         int mult = 3;
306         unsigned ret;
307
308         if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
309                                         EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
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 {
338         const ext4_fsblk_t blk = primary->b_blocknr;
339         const unsigned long end = EXT4_SB(sb)->s_groups_count;
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
347         while ((grp = ext4_list_backups(sb, &three, &five, &seven)) < end) {
348                 if (le32_to_cpu(*p++) !=
349                     grp * EXT4_BLOCKS_PER_GROUP(sb) + blk){
350                         ext4_warning(sb, __FUNCTION__,
351                                      "reserved GDT %llu"
352                                      " missing grp %d (%llu)",
353                                      blk, grp,
354                                      grp *
355                                      (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
356                                      blk);
357                         return -EINVAL;
358                 }
359                 if (++gdbackups > EXT4_ADDR_PER_BLOCK(sb))
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,
380                        struct ext4_new_group_data *input,
381                        struct buffer_head **primary)
382 {
383         struct super_block *sb = inode->i_sb;
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;
387         struct buffer_head **o_group_desc, **n_group_desc;
388         struct buffer_head *dind;
389         int gdbackups;
390         struct ext4_iloc iloc;
391         __le32 *data;
392         int err;
393
394         if (test_opt(sb, DEBUG))
395                 printk(KERN_DEBUG
396                        "EXT4-fs: ext4_add_new_gdb: adding group block %lu\n",
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          */
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__,
407                         "won't resize using backup superblock at %llu",
408                         (unsigned long long)EXT4_SB(sb)->s_sbh->b_blocknr);
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
421         data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
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;
429         if (le32_to_cpu(data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)]) != gdblock) {
430                 ext4_warning(sb, __FUNCTION__,
431                              "new group %u GDT block %llu not reserved",
432                              input->group, gdblock);
433                 err = -EINVAL;
434                 goto exit_dind;
435         }
436
437         if ((err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh)))
438                 goto exit_dind;
439
440         if ((err = ext4_journal_get_write_access(handle, *primary)))
441                 goto exit_sbh;
442
443         if ((err = ext4_journal_get_write_access(handle, dind)))
444                 goto exit_primary;
445
446         /* ext4_reserve_inode_write() gets a reference on the iloc */
447         if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
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;
454                 ext4_warning (sb, __FUNCTION__,
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          */
468         data[gdb_num % EXT4_ADDR_PER_BLOCK(sb)] = 0;
469         ext4_journal_dirty_metadata(handle, dind);
470         brelse(dind);
471         inode->i_blocks -= (gdbackups + 1) * sb->s_blocksize >> 9;
472         ext4_mark_iloc_dirty(handle, inode, &iloc);
473         memset((*primary)->b_data, 0, sb->s_blocksize);
474         ext4_journal_dirty_metadata(handle, *primary);
475
476         o_group_desc = EXT4_SB(sb)->s_group_desc;
477         memcpy(n_group_desc, o_group_desc,
478                EXT4_SB(sb)->s_gdb_count * sizeof(struct buffer_head *));
479         n_group_desc[gdb_num] = *primary;
480         EXT4_SB(sb)->s_group_desc = n_group_desc;
481         EXT4_SB(sb)->s_gdb_count++;
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);
486         ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
487
488         return 0;
489
490 exit_inode:
491         //ext4_journal_release_buffer(handle, iloc.bh);
492         brelse(iloc.bh);
493 exit_dindj:
494         //ext4_journal_release_buffer(handle, dind);
495 exit_primary:
496         //ext4_journal_release_buffer(handle, *primary);
497 exit_sbh:
498         //ext4_journal_release_buffer(handle, *primary);
499 exit_dind:
500         brelse(dind);
501 exit_bh:
502         brelse(*primary);
503
504         ext4_debug("leaving with error %d\n", err);
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,
522                               struct ext4_new_group_data *input)
523 {
524         struct super_block *sb = inode->i_sb;
525         int reserved_gdb =le16_to_cpu(EXT4_SB(sb)->s_es->s_reserved_gdt_blocks);
526         struct buffer_head **primary;
527         struct buffer_head *dind;
528         struct ext4_iloc iloc;
529         ext4_fsblk_t blk;
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
539         data = EXT4_I(inode)->i_data + EXT4_DIND_BLOCK;
540         dind = sb_bread(sb, le32_to_cpu(*data));
541         if (!dind) {
542                 err = -EIO;
543                 goto exit_free;
544         }
545
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);
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) {
553                         ext4_warning(sb, __FUNCTION__,
554                                      "reserved block %llu"
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++) {
576                 if ((err = ext4_journal_get_write_access(handle, primary[i]))) {
577                         /*
578                         int j;
579                         for (j = 0; j < i; j++)
580                                 ext4_journal_release_buffer(handle, primary[j]);
581                          */
582                         goto exit_bh;
583                 }
584         }
585
586         if ((err = ext4_reserve_inode_write(handle, inode, &iloc)))
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          */
593         blk = input->group * EXT4_BLOCKS_PER_GROUP(sb);
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);
601                 err2 = ext4_journal_dirty_metadata(handle, primary[i]);
602                 if (!err)
603                         err = err2;
604         }
605         inode->i_blocks += reserved_gdb * sb->s_blocksize >> 9;
606         ext4_mark_iloc_dirty(handle, inode, &iloc);
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 /*
620  * Update the backup copies of the ext4 metadata.  These don't need to be part
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 {
637         struct ext4_sb_info *sbi = EXT4_SB(sb);
638         const unsigned long last = sbi->s_groups_count;
639         const int bpg = EXT4_BLOCKS_PER_GROUP(sb);
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
648         handle = ext4_journal_start_sb(sb, EXT4_MAX_TRANS_DATA);
649         if (IS_ERR(handle)) {
650                 group = 1;
651                 err = PTR_ERR(handle);
652                 goto exit_err;
653         }
654
655         while ((group = ext4_list_backups(sb, &three, &five, &seven)) < last) {
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 &&
660                     ext4_journal_extend(handle, EXT4_MAX_TRANS_DATA) &&
661                     (err = ext4_journal_restart(handle, EXT4_MAX_TRANS_DATA)))
662                         break;
663
664                 bh = sb_getblk(sb, group * bpg + blk_off);
665                 if (!bh) {
666                         err = -EIO;
667                         break;
668                 }
669                 ext4_debug("update metadata backup %#04lx\n",
670                           (unsigned long)bh->b_blocknr);
671                 if ((err = ext4_journal_get_write_access(handle, bh)))
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);
679                 ext4_journal_dirty_metadata(handle, bh);
680                 brelse(bh);
681         }
682         if ((err2 = ext4_journal_stop(handle)) && !err)
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) {
697                 ext4_warning(sb, __FUNCTION__,
698                              "can't update backup for group %d (err %d), "
699                              "forcing fsck on next reboot", group, err);
700                 sbi->s_mount_state &= ~EXT4_VALID_FS;
701                 sbi->s_es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
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  */
719 int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
720 {
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) ?
724                 le16_to_cpu(es->s_reserved_gdt_blocks) : 0;
725         struct buffer_head *primary = NULL;
726         struct ext4_group_desc *gdp;
727         struct inode *inode = NULL;
728         handle_t *handle;
729         int gdb_off, gdb_num;
730         int err, err2;
731
732         gdb_num = input->group / EXT4_DESC_PER_BLOCK(sb);
733         gdb_off = input->group % EXT4_DESC_PER_BLOCK(sb);
734
735         if (gdb_off == 0 && !EXT4_HAS_RO_COMPAT_FEATURE(sb,
736                                         EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
737                 ext4_warning(sb, __FUNCTION__,
738                              "Can't resize non-sparse filesystem further");
739                 return -EPERM;
740         }
741
742         if (ext4_blocks_count(es) + input->blocks_count <
743             ext4_blocks_count(es)) {
744                 ext4_warning(sb, __FUNCTION__, "blocks_count overflow\n");
745                 return -EINVAL;
746         }
747
748         if (le32_to_cpu(es->s_inodes_count) + EXT4_INODES_PER_GROUP(sb) <
749             le32_to_cpu(es->s_inodes_count)) {
750                 ext4_warning(sb, __FUNCTION__, "inodes_count overflow\n");
751                 return -EINVAL;
752         }
753
754         if (reserved_gdb || gdb_off == 0) {
755                 if (!EXT4_HAS_COMPAT_FEATURE(sb,
756                                              EXT4_FEATURE_COMPAT_RESIZE_INODE)){
757                         ext4_warning(sb, __FUNCTION__,
758                                      "No reserved GDT blocks, can't resize");
759                         return -EPERM;
760                 }
761                 inode = iget(sb, EXT4_RESIZE_INO);
762                 if (!inode || is_bad_inode(inode)) {
763                         ext4_warning(sb, __FUNCTION__,
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          */
783         handle = ext4_journal_start_sb(sb,
784                                        ext4_bg_has_super(sb, input->group) ?
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) {
793                 ext4_warning(sb, __FUNCTION__,
794                              "multiple resizers run on filesystem!");
795                 err = -EBUSY;
796                 goto exit_journal;
797         }
798
799         if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
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];
810                 if ((err = ext4_journal_get_write_access(handle, primary)))
811                         goto exit_journal;
812
813                 if (reserved_gdb && ext4_bg_num_gdb(sb, input->group) &&
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 */
839         gdp = (struct ext4_group_desc *)primary->b_data + gdb_off;
840
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 */
844         gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
845         gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb));
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          */
857         ext4_blocks_count_set(es, ext4_blocks_count(es) +
858                 input->blocks_count);
859         es->s_inodes_count = cpu_to_le32(le32_to_cpu(es->s_inodes_count) +
860                 EXT4_INODES_PER_GROUP(sb));
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
889         ext4_journal_dirty_metadata(handle, primary);
890
891         /* Update the reserved block counts only once the new group is
892          * active. */
893         ext4_r_blocks_count_set(es, ext4_r_blocks_count(es) +
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,
900                            EXT4_INODES_PER_GROUP(sb));
901
902         ext4_journal_dirty_metadata(handle, sbi->s_sbh);
903         sb->s_dirt = 1;
904
905 exit_journal:
906         unlock_super(sb);
907         if ((err2 = ext4_journal_stop(handle)) && !err)
908                 err = err2;
909         if (!err) {
910                 update_backups(sb, sbi->s_sbh->b_blocknr, (char *)es,
911                                sizeof(struct ext4_super_block));
912                 update_backups(sb, primary->b_blocknr, primary->b_data,
913                                primary->b_size);
914         }
915 exit_put:
916         iput(inode);
917         return err;
918 } /* ext4_group_add */
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  *
925  * If we _really_ wanted, we could use default values to call ext4_group_add()
926  * allow the "remount" trick to work for arbitrary resizing, assuming enough
927  * GDT blocks are reserved to grow to the desired size.
928  */
929 int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
930                       ext4_fsblk_t n_blocks_count)
931 {
932         ext4_fsblk_t o_blocks_count;
933         unsigned long o_groups_count;
934         ext4_grpblk_t last;
935         ext4_grpblk_t add;
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. */
944         o_blocks_count = ext4_blocks_count(es);
945         o_groups_count = EXT4_SB(sb)->s_groups_count;
946
947         if (test_opt(sb, DEBUG))
948                 printk(KERN_DEBUG "EXT4-fs: extending last group from %llu uto %llu blocks\n",
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)) {
955                 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
956                         " too large to resize to %llu blocks safely\n",
957                         sb->s_id, n_blocks_count);
958                 if (sizeof(sector_t) < 8)
959                         ext4_warning(sb, __FUNCTION__,
960                         "CONFIG_LBD not enabled\n");
961                 return -EINVAL;
962         }
963
964         if (n_blocks_count < o_blocks_count) {
965                 ext4_warning(sb, __FUNCTION__,
966                              "can't shrink FS - resize aborted");
967                 return -EBUSY;
968         }
969
970         /* Handle the remaining blocks in the last group only. */
971         ext4_get_group_no_and_offset(sb, o_blocks_count, NULL, &last);
972
973         if (last == 0) {
974                 ext4_warning(sb, __FUNCTION__,
975                              "need to use ext2online to resize further");
976                 return -EPERM;
977         }
978
979         add = EXT4_BLOCKS_PER_GROUP(sb) - last;
980
981         if (o_blocks_count + add < o_blocks_count) {
982                 ext4_warning(sb, __FUNCTION__, "blocks_count overflow");
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)
990                 ext4_warning(sb, __FUNCTION__,
991                              "will only finish group (%llu"
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) {
998                 ext4_warning(sb, __FUNCTION__,
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
1005          * one group descriptor via ext4_free_blocks().
1006          */
1007         handle = ext4_journal_start_sb(sb, 3);
1008         if (IS_ERR(handle)) {
1009                 err = PTR_ERR(handle);
1010                 ext4_warning(sb, __FUNCTION__, "error %d on journal start",err);
1011                 goto exit_put;
1012         }
1013
1014         lock_super(sb);
1015         if (o_blocks_count != ext4_blocks_count(es)) {
1016                 ext4_warning(sb, __FUNCTION__,
1017                              "multiple resizers run on filesystem!");
1018                 unlock_super(sb);
1019                 err = -EBUSY;
1020                 goto exit_put;
1021         }
1022
1023         if ((err = ext4_journal_get_write_access(handle,
1024                                                  EXT4_SB(sb)->s_sbh))) {
1025                 ext4_warning(sb, __FUNCTION__,
1026                              "error %d on journal write access", err);
1027                 unlock_super(sb);
1028                 ext4_journal_stop(handle);
1029                 goto exit_put;
1030         }
1031         ext4_blocks_count_set(es, o_blocks_count + add);
1032         ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh);
1033         sb->s_dirt = 1;
1034         unlock_super(sb);
1035         ext4_debug("freeing blocks %lu through %llu\n", o_blocks_count,
1036                    o_blocks_count + add);
1037         ext4_free_blocks_sb(handle, sb, o_blocks_count, add, &freed_blocks);
1038         ext4_debug("freed blocks %llu through %llu\n", o_blocks_count,
1039                    o_blocks_count + add);
1040         if ((err = ext4_journal_stop(handle)))
1041                 goto exit_put;
1042         if (test_opt(sb, DEBUG))
1043                 printk(KERN_DEBUG "EXT4-fs: extended group to %llu blocks\n",
1044                        ext4_blocks_count(es));
1045         update_backups(sb, EXT4_SB(sb)->s_sbh->b_blocknr, (char *)es,
1046                        sizeof(struct ext4_super_block));
1047 exit_put:
1048         return err;
1049 } /* ext4_group_extend */