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