Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
0003  */
0004 
0005 /*
0006  * Written by Alexander Zarochentcev.
0007  *
0008  * The kernel part of the (on-line) reiserfs resizer.
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/mm.h>
0013 #include <linux/vmalloc.h>
0014 #include <linux/string.h>
0015 #include <linux/errno.h>
0016 #include "reiserfs.h"
0017 #include <linux/buffer_head.h>
0018 
0019 int reiserfs_resize(struct super_block *s, unsigned long block_count_new)
0020 {
0021     int err = 0;
0022     struct reiserfs_super_block *sb;
0023     struct reiserfs_bitmap_info *bitmap;
0024     struct reiserfs_bitmap_info *info;
0025     struct reiserfs_bitmap_info *old_bitmap = SB_AP_BITMAP(s);
0026     struct buffer_head *bh;
0027     struct reiserfs_transaction_handle th;
0028     unsigned int bmap_nr_new, bmap_nr;
0029     unsigned int block_r_new, block_r;
0030 
0031     struct reiserfs_list_bitmap *jb;
0032     struct reiserfs_list_bitmap jbitmap[JOURNAL_NUM_BITMAPS];
0033 
0034     unsigned long int block_count, free_blocks;
0035     int i;
0036     int copy_size;
0037     int depth;
0038 
0039     sb = SB_DISK_SUPER_BLOCK(s);
0040 
0041     if (SB_BLOCK_COUNT(s) >= block_count_new) {
0042         printk("can\'t shrink filesystem on-line\n");
0043         return -EINVAL;
0044     }
0045 
0046     /* check the device size */
0047     depth = reiserfs_write_unlock_nested(s);
0048     bh = sb_bread(s, block_count_new - 1);
0049     reiserfs_write_lock_nested(s, depth);
0050     if (!bh) {
0051         printk("reiserfs_resize: can\'t read last block\n");
0052         return -EINVAL;
0053     }
0054     bforget(bh);
0055 
0056     /*
0057      * old disk layout detection; those partitions can be mounted, but
0058      * cannot be resized
0059      */
0060     if (SB_BUFFER_WITH_SB(s)->b_blocknr * SB_BUFFER_WITH_SB(s)->b_size
0061         != REISERFS_DISK_OFFSET_IN_BYTES) {
0062         printk
0063             ("reiserfs_resize: unable to resize a reiserfs without distributed bitmap (fs version < 3.5.12)\n");
0064         return -ENOTSUPP;
0065     }
0066 
0067     /* count used bits in last bitmap block */
0068     block_r = SB_BLOCK_COUNT(s) -
0069             (reiserfs_bmap_count(s) - 1) * s->s_blocksize * 8;
0070 
0071     /* count bitmap blocks in new fs */
0072     bmap_nr_new = block_count_new / (s->s_blocksize * 8);
0073     block_r_new = block_count_new - bmap_nr_new * s->s_blocksize * 8;
0074     if (block_r_new)
0075         bmap_nr_new++;
0076     else
0077         block_r_new = s->s_blocksize * 8;
0078 
0079     /* save old values */
0080     block_count = SB_BLOCK_COUNT(s);
0081     bmap_nr = reiserfs_bmap_count(s);
0082 
0083     /* resizing of reiserfs bitmaps (journal and real), if needed */
0084     if (bmap_nr_new > bmap_nr) {
0085         /* reallocate journal bitmaps */
0086         if (reiserfs_allocate_list_bitmaps(s, jbitmap, bmap_nr_new) < 0) {
0087             printk
0088                 ("reiserfs_resize: unable to allocate memory for journal bitmaps\n");
0089             return -ENOMEM;
0090         }
0091         /*
0092          * the new journal bitmaps are zero filled, now we copy i
0093          * the bitmap node pointers from the old journal bitmap
0094          * structs, and then transfer the new data structures
0095          * into the journal struct.
0096          *
0097          * using the copy_size var below allows this code to work for
0098          * both shrinking and expanding the FS.
0099          */
0100         copy_size = bmap_nr_new < bmap_nr ? bmap_nr_new : bmap_nr;
0101         copy_size =
0102             copy_size * sizeof(struct reiserfs_list_bitmap_node *);
0103         for (i = 0; i < JOURNAL_NUM_BITMAPS; i++) {
0104             struct reiserfs_bitmap_node **node_tmp;
0105             jb = SB_JOURNAL(s)->j_list_bitmap + i;
0106             memcpy(jbitmap[i].bitmaps, jb->bitmaps, copy_size);
0107 
0108             /*
0109              * just in case vfree schedules on us, copy the new
0110              * pointer into the journal struct before freeing the
0111              * old one
0112              */
0113             node_tmp = jb->bitmaps;
0114             jb->bitmaps = jbitmap[i].bitmaps;
0115             vfree(node_tmp);
0116         }
0117 
0118         /*
0119          * allocate additional bitmap blocks, reallocate
0120          * array of bitmap block pointers
0121          */
0122         bitmap =
0123             vzalloc(array_size(bmap_nr_new,
0124                        sizeof(struct reiserfs_bitmap_info)));
0125         if (!bitmap) {
0126             /*
0127              * Journal bitmaps are still supersized, but the
0128              * memory isn't leaked, so I guess it's ok
0129              */
0130             printk("reiserfs_resize: unable to allocate memory.\n");
0131             return -ENOMEM;
0132         }
0133         for (i = 0; i < bmap_nr; i++)
0134             bitmap[i] = old_bitmap[i];
0135 
0136         /*
0137          * This doesn't go through the journal, but it doesn't have to.
0138          * The changes are still atomic: We're synced up when the
0139          * journal transaction begins, and the new bitmaps don't
0140          * matter if the transaction fails.
0141          */
0142         for (i = bmap_nr; i < bmap_nr_new; i++) {
0143             int depth;
0144             /*
0145              * don't use read_bitmap_block since it will cache
0146              * the uninitialized bitmap
0147              */
0148             depth = reiserfs_write_unlock_nested(s);
0149             bh = sb_bread(s, i * s->s_blocksize * 8);
0150             reiserfs_write_lock_nested(s, depth);
0151             if (!bh) {
0152                 vfree(bitmap);
0153                 return -EIO;
0154             }
0155             memset(bh->b_data, 0, sb_blocksize(sb));
0156             reiserfs_set_le_bit(0, bh->b_data);
0157             reiserfs_cache_bitmap_metadata(s, bh, bitmap + i);
0158 
0159             set_buffer_uptodate(bh);
0160             mark_buffer_dirty(bh);
0161             depth = reiserfs_write_unlock_nested(s);
0162             sync_dirty_buffer(bh);
0163             reiserfs_write_lock_nested(s, depth);
0164             /* update bitmap_info stuff */
0165             bitmap[i].free_count = sb_blocksize(sb) * 8 - 1;
0166             brelse(bh);
0167         }
0168         /* free old bitmap blocks array */
0169         SB_AP_BITMAP(s) = bitmap;
0170         vfree(old_bitmap);
0171     }
0172 
0173     /*
0174      * begin transaction, if there was an error, it's fine. Yes, we have
0175      * incorrect bitmaps now, but none of it is ever going to touch the
0176      * disk anyway.
0177      */
0178     err = journal_begin(&th, s, 10);
0179     if (err)
0180         return err;
0181 
0182     /* Extend old last bitmap block - new blocks have been made available */
0183     info = SB_AP_BITMAP(s) + bmap_nr - 1;
0184     bh = reiserfs_read_bitmap_block(s, bmap_nr - 1);
0185     if (!bh) {
0186         int jerr = journal_end(&th);
0187         if (jerr)
0188             return jerr;
0189         return -EIO;
0190     }
0191 
0192     reiserfs_prepare_for_journal(s, bh, 1);
0193     for (i = block_r; i < s->s_blocksize * 8; i++)
0194         reiserfs_clear_le_bit(i, bh->b_data);
0195     info->free_count += s->s_blocksize * 8 - block_r;
0196 
0197     journal_mark_dirty(&th, bh);
0198     brelse(bh);
0199 
0200     /* Correct new last bitmap block - It may not be full */
0201     info = SB_AP_BITMAP(s) + bmap_nr_new - 1;
0202     bh = reiserfs_read_bitmap_block(s, bmap_nr_new - 1);
0203     if (!bh) {
0204         int jerr = journal_end(&th);
0205         if (jerr)
0206             return jerr;
0207         return -EIO;
0208     }
0209 
0210     reiserfs_prepare_for_journal(s, bh, 1);
0211     for (i = block_r_new; i < s->s_blocksize * 8; i++)
0212         reiserfs_set_le_bit(i, bh->b_data);
0213     journal_mark_dirty(&th, bh);
0214     brelse(bh);
0215 
0216     info->free_count -= s->s_blocksize * 8 - block_r_new;
0217     /* update super */
0218     reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1);
0219     free_blocks = SB_FREE_BLOCKS(s);
0220     PUT_SB_FREE_BLOCKS(s,
0221                free_blocks + (block_count_new - block_count -
0222                       (bmap_nr_new - bmap_nr)));
0223     PUT_SB_BLOCK_COUNT(s, block_count_new);
0224     PUT_SB_BMAP_NR(s, bmap_would_wrap(bmap_nr_new) ? : bmap_nr_new);
0225 
0226     journal_mark_dirty(&th, SB_BUFFER_WITH_SB(s));
0227 
0228     SB_JOURNAL(s)->j_must_wait = 1;
0229     return journal_end(&th);
0230 }