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