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