md/bitmap: record the space available for the bitmap in the superblock.
[deliverable/linux.git] / drivers / md / bitmap.c
index f3a9dffc3fc8f0a797109d0dc1004ce76dcb5825..15dbe03117e473da9ce6166b6978b7f496b7bc2a 100644 (file)
@@ -45,7 +45,7 @@ static inline char *bmname(struct bitmap *bitmap)
  * if we find our page, we increment the page's refcount so that it stays
  * allocated while we're using it
  */
-static int bitmap_checkpage(struct bitmap *bitmap,
+static int bitmap_checkpage(struct bitmap_counts *bitmap,
                            unsigned long page, int create)
 __releases(bitmap->lock)
 __acquires(bitmap->lock)
@@ -76,8 +76,7 @@ __acquires(bitmap->lock)
        spin_lock_irq(&bitmap->lock);
 
        if (mappage == NULL) {
-               pr_debug("%s: bitmap map page allocation failed, hijacking\n",
-                        bmname(bitmap));
+               pr_debug("md/bitmap: map page allocation failed, hijacking\n");
                /* failed - set the hijacked flag so that we can use the
                 * pointer as a counter */
                if (!bitmap->bp[page].map)
@@ -100,7 +99,7 @@ __acquires(bitmap->lock)
 /* if page is completely empty, put it back on the free list, or dealloc it */
 /* if page was hijacked, unmark the flag so it might get alloced next time */
 /* Note: lock should be held when calling this */
-static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
+static void bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page)
 {
        char *ptr;
 
@@ -421,6 +420,11 @@ void bitmap_update_sb(struct bitmap *bitmap)
        /* Just in case these have been changed via sysfs: */
        sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
        sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
+       /* This might have been changed by a reshape */
+       sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
+       sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize);
+       sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
+                                          bitmap_info.space);
        kunmap_atomic(sb);
        write_page(bitmap, bitmap->storage.sb_page, 1);
 }
@@ -534,6 +538,7 @@ static int bitmap_read_sb(struct bitmap *bitmap)
        bitmap_super_t *sb;
        unsigned long chunksize, daemon_sleep, write_behind;
        unsigned long long events;
+       unsigned long sectors_reserved = 0;
        int err = -EINVAL;
        struct page *sb_page;
 
@@ -571,6 +576,7 @@ static int bitmap_read_sb(struct bitmap *bitmap)
        chunksize = le32_to_cpu(sb->chunksize);
        daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
        write_behind = le32_to_cpu(sb->write_behind);
+       sectors_reserved = le32_to_cpu(sb->sectors_reserved);
 
        /* verify that the bitmap-specific fields are valid */
        if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
@@ -631,6 +637,9 @@ out_no_sb:
        bitmap->mddev->bitmap_info.chunksize = chunksize;
        bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
        bitmap->mddev->bitmap_info.max_write_behind = write_behind;
+       if (bitmap->mddev->bitmap_info.space == 0 ||
+           bitmap->mddev->bitmap_info.space > sectors_reserved)
+               bitmap->mddev->bitmap_info.space = sectors_reserved;
        if (err)
                bitmap_print_sb(bitmap);
        return err;
@@ -699,7 +708,7 @@ static int bitmap_storage_alloc(struct bitmap_storage *store,
                return -ENOMEM;
 
        if (with_super && !store->sb_page) {
-               store->sb_page = alloc_page(GFP_KERNEL);
+               store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
                if (store->sb_page == NULL)
                        return -ENOMEM;
                store->sb_page->index = 0;
@@ -710,7 +719,7 @@ static int bitmap_storage_alloc(struct bitmap_storage *store,
                pnum = 1;
        }
        for ( ; pnum < num_pages; pnum++) {
-               store->filemap[pnum] = alloc_page(GFP_KERNEL);
+               store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
                if (!store->filemap[pnum]) {
                        store->file_pages = pnum;
                        return -ENOMEM;
@@ -732,49 +741,25 @@ static int bitmap_storage_alloc(struct bitmap_storage *store,
        return 0;
 }
 
-static void bitmap_file_unmap(struct bitmap *bitmap)
+static void bitmap_file_unmap(struct bitmap_storage *store)
 {
        struct page **map, *sb_page;
-       unsigned long *attr;
        int pages;
-       unsigned long flags;
-       struct bitmap_storage *store = &bitmap->storage;
+       struct file *file;
 
-       spin_lock_irqsave(&bitmap->lock, flags);
+       file = store->file;
        map = store->filemap;
-       store->filemap = NULL;
-       attr = store->filemap_attr;
-       store->filemap_attr = NULL;
        pages = store->file_pages;
-       store->file_pages = 0;
        sb_page = store->sb_page;
-       store->sb_page = NULL;
-       spin_unlock_irqrestore(&bitmap->lock, flags);
 
        while (pages--)
                if (map[pages] != sb_page) /* 0 is sb_page, release it below */
                        free_buffers(map[pages]);
        kfree(map);
-       kfree(attr);
+       kfree(store->filemap_attr);
 
        if (sb_page)
                free_buffers(sb_page);
-}
-
-static void bitmap_file_put(struct bitmap *bitmap)
-{
-       struct file *file;
-       unsigned long flags;
-
-       spin_lock_irqsave(&bitmap->lock, flags);
-       file = bitmap->storage.file;
-       bitmap->storage.file = NULL;
-       spin_unlock_irqrestore(&bitmap->lock, flags);
-
-       if (file)
-               wait_event(bitmap->write_wait,
-                          atomic_read(&bitmap->pending_writes)==0);
-       bitmap_file_unmap(bitmap);
 
        if (file) {
                struct inode *inode = file->f_path.dentry->d_inode;
@@ -811,10 +796,6 @@ static void bitmap_file_kick(struct bitmap *bitmap)
                               "%s: disabling internal bitmap due to errors\n",
                               bmname(bitmap));
        }
-
-       bitmap_file_put(bitmap);
-
-       return;
 }
 
 enum bitmap_page_attr {
@@ -827,21 +808,27 @@ enum bitmap_page_attr {
 static inline void set_page_attr(struct bitmap *bitmap, int pnum,
                                 enum bitmap_page_attr attr)
 {
-       __set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
+       set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
 }
 
 static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
                                   enum bitmap_page_attr attr)
 {
-       __clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
+       clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
 }
 
-static inline unsigned long test_page_attr(struct bitmap *bitmap, int pnum,
-                                          enum bitmap_page_attr attr)
+static inline int test_page_attr(struct bitmap *bitmap, int pnum,
+                                enum bitmap_page_attr attr)
 {
        return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
 }
 
+static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
+                                          enum bitmap_page_attr attr)
+{
+       return test_and_clear_bit((pnum<<2) + attr,
+                                 bitmap->storage.filemap_attr);
+}
 /*
  * bitmap_file_set_bit -- called before performing a write to the md device
  * to set (and eventually sync) a particular bit in the bitmap file
@@ -854,7 +841,7 @@ static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
        unsigned long bit;
        struct page *page;
        void *kaddr;
-       unsigned long chunk = block >> bitmap->chunkshift;
+       unsigned long chunk = block >> bitmap->counts.chunkshift;
 
        page = filemap_get_page(&bitmap->storage, chunk);
        if (!page)
@@ -866,7 +853,7 @@ static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
        if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
                set_bit(bit, kaddr);
        else
-               __set_bit_le(bit, kaddr);
+               test_and_set_bit_le(bit, kaddr);
        kunmap_atomic(kaddr);
        pr_debug("set file bit %lu page %lu\n", bit, page->index);
        /* record page number so it gets flushed to disk when unplug occurs */
@@ -878,7 +865,7 @@ static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
        unsigned long bit;
        struct page *page;
        void *paddr;
-       unsigned long chunk = block >> bitmap->chunkshift;
+       unsigned long chunk = block >> bitmap->counts.chunkshift;
 
        page = filemap_get_page(&bitmap->storage, chunk);
        if (!page)
@@ -888,7 +875,7 @@ static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
        if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
                clear_bit(bit, paddr);
        else
-               __clear_bit_le(bit, paddr);
+               test_and_clear_bit_le(bit, paddr);
        kunmap_atomic(paddr);
        if (!test_page_attr(bitmap, page->index, BITMAP_PAGE_NEEDWRITE)) {
                set_page_attr(bitmap, page->index, BITMAP_PAGE_PENDING);
@@ -901,33 +888,28 @@ static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
  * sync the dirty pages of the bitmap file to disk */
 void bitmap_unplug(struct bitmap *bitmap)
 {
-       unsigned long i, flags;
+       unsigned long i;
        int dirty, need_write;
        int wait = 0;
 
-       if (!bitmap || !bitmap->storage.filemap)
+       if (!bitmap || !bitmap->storage.filemap ||
+           test_bit(BITMAP_STALE, &bitmap->flags))
                return;
 
        /* look at each page to see if there are any set bits that need to be
         * flushed out to disk */
        for (i = 0; i < bitmap->storage.file_pages; i++) {
-               spin_lock_irqsave(&bitmap->lock, flags);
-               if (!bitmap->storage.filemap) {
-                       spin_unlock_irqrestore(&bitmap->lock, flags);
+               if (!bitmap->storage.filemap)
                        return;
-               }
-               dirty = test_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
-               need_write = test_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE);
-               clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
-               clear_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE);
-               if (dirty || need_write)
+               dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
+               need_write = test_and_clear_page_attr(bitmap, i,
+                                                     BITMAP_PAGE_NEEDWRITE);
+               if (dirty || need_write) {
                        clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
+                       write_page(bitmap, bitmap->storage.filemap[i], 0);
+               }
                if (dirty)
                        wait = 1;
-               spin_unlock_irqrestore(&bitmap->lock, flags);
-
-               if (dirty || need_write)
-                       write_page(bitmap, bitmap->storage.filemap[i], 0);
        }
        if (wait) { /* if any writes were performed, we need to wait on them */
                if (bitmap->storage.file)
@@ -965,7 +947,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
        void *paddr;
        struct bitmap_storage *store = &bitmap->storage;
 
-       chunks = bitmap->chunks;
+       chunks = bitmap->counts.chunks;
        file = store->file;
 
        if (!file && !bitmap->mddev->bitmap_info.offset) {
@@ -974,10 +956,10 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
                store->file_pages = 0;
                for (i = 0; i < chunks ; i++) {
                        /* if the disk bit is set, set the memory bit */
-                       int needed = ((sector_t)(i+1) << (bitmap->chunkshift)
+                       int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift)
                                      >= start);
                        bitmap_set_memory_bits(bitmap,
-                                              (sector_t)i << bitmap->chunkshift,
+                                              (sector_t)i << bitmap->counts.chunkshift,
                                               needed);
                }
                return 0;
@@ -1053,10 +1035,10 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
                kunmap_atomic(paddr);
                if (b) {
                        /* if the disk bit is set, set the memory bit */
-                       int needed = ((sector_t)(i+1) << bitmap->chunkshift
+                       int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift
                                      >= start);
                        bitmap_set_memory_bits(bitmap,
-                                              (sector_t)i << bitmap->chunkshift,
+                                              (sector_t)i << bitmap->counts.chunkshift,
                                               needed);
                        bit_cnt++;
                }
@@ -1089,15 +1071,14 @@ void bitmap_write_all(struct bitmap *bitmap)
                /* Only one copy, so nothing needed */
                return;
 
-       spin_lock_irq(&bitmap->lock);
        for (i = 0; i < bitmap->storage.file_pages; i++)
                set_page_attr(bitmap, i,
                              BITMAP_PAGE_NEEDWRITE);
        bitmap->allclean = 0;
-       spin_unlock_irq(&bitmap->lock);
 }
 
-static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
+static void bitmap_count_page(struct bitmap_counts *bitmap,
+                             sector_t offset, int inc)
 {
        sector_t chunk = offset >> bitmap->chunkshift;
        unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
@@ -1105,7 +1086,7 @@ static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
        bitmap_checkfree(bitmap, page);
 }
 
-static void bitmap_set_pending(struct bitmap *bitmap, sector_t offset)
+static void bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset)
 {
        sector_t chunk = offset >> bitmap->chunkshift;
        unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
@@ -1115,7 +1096,7 @@ static void bitmap_set_pending(struct bitmap *bitmap, sector_t offset)
                bp->pending = 1;
 }
 
-static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
+static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
                                            sector_t offset, sector_t *blocks,
                                            int create);
 
@@ -1129,8 +1110,8 @@ void bitmap_daemon_work(struct mddev *mddev)
        struct bitmap *bitmap;
        unsigned long j;
        unsigned long nextpage;
-       unsigned long flags;
        sector_t blocks;
+       struct bitmap_counts *counts;
 
        /* Use a mutex to guard daemon_work against
         * bitmap_destroy.
@@ -1156,15 +1137,11 @@ void bitmap_daemon_work(struct mddev *mddev)
         * So set NEEDWRITE now, then after we make any last-minute changes
         * we will write it.
         */
-       spin_lock_irqsave(&bitmap->lock, flags);
        for (j = 0; j < bitmap->storage.file_pages; j++)
-               if (test_page_attr(bitmap, j,
-                                  BITMAP_PAGE_PENDING)) {
+               if (test_and_clear_page_attr(bitmap, j,
+                                            BITMAP_PAGE_PENDING))
                        set_page_attr(bitmap, j,
                                      BITMAP_PAGE_NEEDWRITE);
-                       clear_page_attr(bitmap, j,
-                                       BITMAP_PAGE_PENDING);
-               }
 
        if (bitmap->need_sync &&
            mddev->bitmap_info.external == 0) {
@@ -1184,20 +1161,22 @@ void bitmap_daemon_work(struct mddev *mddev)
        /* Now look at the bitmap counters and if any are '2' or '1',
         * decrement and handle accordingly.
         */
+       counts = &bitmap->counts;
+       spin_lock_irq(&counts->lock);
        nextpage = 0;
-       for (j = 0; j < bitmap->chunks; j++) {
+       for (j = 0; j < counts->chunks; j++) {
                bitmap_counter_t *bmc;
-               sector_t  block = (sector_t)j << bitmap->chunkshift;
+               sector_t  block = (sector_t)j << counts->chunkshift;
 
                if (j == nextpage) {
                        nextpage += PAGE_COUNTER_RATIO;
-                       if (!bitmap->bp[j >> PAGE_COUNTER_SHIFT].pending) {
+                       if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) {
                                j |= PAGE_COUNTER_MASK;
                                continue;
                        }
-                       bitmap->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
+                       counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
                }
-               bmc = bitmap_get_counter(bitmap,
+               bmc = bitmap_get_counter(counts,
                                         block,
                                         &blocks, 0);
 
@@ -1208,14 +1187,15 @@ void bitmap_daemon_work(struct mddev *mddev)
                if (*bmc == 1 && !bitmap->need_sync) {
                        /* We can clear the bit */
                        *bmc = 0;
-                       bitmap_count_page(bitmap, block, -1);
+                       bitmap_count_page(counts, block, -1);
                        bitmap_file_clear_bit(bitmap, block);
                } else if (*bmc && *bmc <= 2) {
                        *bmc = 1;
-                       bitmap_set_pending(bitmap, block);
+                       bitmap_set_pending(counts, block);
                        bitmap->allclean = 0;
                }
        }
+       spin_unlock_irq(&counts->lock);
 
        /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
         * DIRTY pages need to be written by bitmap_unplug so it can wait
@@ -1225,24 +1205,20 @@ void bitmap_daemon_work(struct mddev *mddev)
         * the first blocking holds the superblock and it has been updated.
         * We mustn't write any other blocks before the superblock.
         */
-       for (j = 0; j < bitmap->storage.file_pages; j++) {
+       for (j = 0;
+            j < bitmap->storage.file_pages
+                    && !test_bit(BITMAP_STALE, &bitmap->flags);
+            j++) {
 
                if (test_page_attr(bitmap, j,
                                   BITMAP_PAGE_DIRTY))
                        /* bitmap_unplug will handle the rest */
                        break;
-               if (test_page_attr(bitmap, j,
-                                  BITMAP_PAGE_NEEDWRITE)) {
-                       clear_page_attr(bitmap, j,
-                                       BITMAP_PAGE_NEEDWRITE);
-                       spin_unlock_irqrestore(&bitmap->lock, flags);
+               if (test_and_clear_page_attr(bitmap, j,
+                                            BITMAP_PAGE_NEEDWRITE)) {
                        write_page(bitmap, bitmap->storage.filemap[j], 0);
-                       spin_lock_irqsave(&bitmap->lock, flags);
-                       if (!bitmap->storage.filemap)
-                               break;
                }
        }
-       spin_unlock_irqrestore(&bitmap->lock, flags);
 
  done:
        if (bitmap->allclean == 0)
@@ -1251,7 +1227,7 @@ void bitmap_daemon_work(struct mddev *mddev)
        mutex_unlock(&mddev->bitmap_info.mutex);
 }
 
-static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
+static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
                                            sector_t offset, sector_t *blocks,
                                            int create)
 __releases(bitmap->lock)
@@ -1313,10 +1289,10 @@ int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sect
                sector_t blocks;
                bitmap_counter_t *bmc;
 
-               spin_lock_irq(&bitmap->lock);
-               bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
+               spin_lock_irq(&bitmap->counts.lock);
+               bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 1);
                if (!bmc) {
-                       spin_unlock_irq(&bitmap->lock);
+                       spin_unlock_irq(&bitmap->counts.lock);
                        return 0;
                }
 
@@ -1328,7 +1304,7 @@ int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sect
                         */
                        prepare_to_wait(&bitmap->overflow_wait, &__wait,
                                        TASK_UNINTERRUPTIBLE);
-                       spin_unlock_irq(&bitmap->lock);
+                       spin_unlock_irq(&bitmap->counts.lock);
                        io_schedule();
                        finish_wait(&bitmap->overflow_wait, &__wait);
                        continue;
@@ -1337,7 +1313,7 @@ int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sect
                switch (*bmc) {
                case 0:
                        bitmap_file_set_bit(bitmap, offset);
-                       bitmap_count_page(bitmap, offset, 1);
+                       bitmap_count_page(&bitmap->counts, offset, 1);
                        /* fall through */
                case 1:
                        *bmc = 2;
@@ -1345,7 +1321,7 @@ int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sect
 
                (*bmc)++;
 
-               spin_unlock_irq(&bitmap->lock);
+               spin_unlock_irq(&bitmap->counts.lock);
 
                offset += blocks;
                if (sectors > blocks)
@@ -1375,10 +1351,10 @@ void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long secto
                unsigned long flags;
                bitmap_counter_t *bmc;
 
-               spin_lock_irqsave(&bitmap->lock, flags);
-               bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
+               spin_lock_irqsave(&bitmap->counts.lock, flags);
+               bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 0);
                if (!bmc) {
-                       spin_unlock_irqrestore(&bitmap->lock, flags);
+                       spin_unlock_irqrestore(&bitmap->counts.lock, flags);
                        return;
                }
 
@@ -1397,10 +1373,10 @@ void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long secto
 
                (*bmc)--;
                if (*bmc <= 2) {
-                       bitmap_set_pending(bitmap, offset);
+                       bitmap_set_pending(&bitmap->counts, offset);
                        bitmap->allclean = 0;
                }
-               spin_unlock_irqrestore(&bitmap->lock, flags);
+               spin_unlock_irqrestore(&bitmap->counts.lock, flags);
                offset += blocks;
                if (sectors > blocks)
                        sectors -= blocks;
@@ -1419,8 +1395,8 @@ static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t
                *blocks = 1024;
                return 1; /* always resync if no bitmap */
        }
-       spin_lock_irq(&bitmap->lock);
-       bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
+       spin_lock_irq(&bitmap->counts.lock);
+       bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
        rv = 0;
        if (bmc) {
                /* locked */
@@ -1434,7 +1410,7 @@ static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t
                        }
                }
        }
-       spin_unlock_irq(&bitmap->lock);
+       spin_unlock_irq(&bitmap->counts.lock);
        return rv;
 }
 
@@ -1471,8 +1447,8 @@ void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, i
                *blocks = 1024;
                return;
        }
-       spin_lock_irqsave(&bitmap->lock, flags);
-       bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
+       spin_lock_irqsave(&bitmap->counts.lock, flags);
+       bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
        if (bmc == NULL)
                goto unlock;
        /* locked */
@@ -1483,13 +1459,13 @@ void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, i
                        *bmc |= NEEDED_MASK;
                else {
                        if (*bmc <= 2) {
-                               bitmap_set_pending(bitmap, offset);
+                               bitmap_set_pending(&bitmap->counts, offset);
                                bitmap->allclean = 0;
                        }
                }
        }
  unlock:
-       spin_unlock_irqrestore(&bitmap->lock, flags);
+       spin_unlock_irqrestore(&bitmap->counts.lock, flags);
 }
 EXPORT_SYMBOL(bitmap_end_sync);
 
@@ -1529,7 +1505,7 @@ void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
 
        bitmap->mddev->curr_resync_completed = sector;
        set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
-       sector &= ~((1ULL << bitmap->chunkshift) - 1);
+       sector &= ~((1ULL << bitmap->counts.chunkshift) - 1);
        s = 0;
        while (s < sector && s < bitmap->mddev->resync_max_sectors) {
                bitmap_end_sync(bitmap, s, &blocks, 0);
@@ -1549,19 +1525,19 @@ static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int n
 
        sector_t secs;
        bitmap_counter_t *bmc;
-       spin_lock_irq(&bitmap->lock);
-       bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
+       spin_lock_irq(&bitmap->counts.lock);
+       bmc = bitmap_get_counter(&bitmap->counts, offset, &secs, 1);
        if (!bmc) {
-               spin_unlock_irq(&bitmap->lock);
+               spin_unlock_irq(&bitmap->counts.lock);
                return;
        }
        if (!*bmc) {
                *bmc = 2 | (needed ? NEEDED_MASK : 0);
-               bitmap_count_page(bitmap, offset, 1);
-               bitmap_set_pending(bitmap, offset);
+               bitmap_count_page(&bitmap->counts, offset, 1);
+               bitmap_set_pending(&bitmap->counts, offset);
                bitmap->allclean = 0;
        }
-       spin_unlock_irq(&bitmap->lock);
+       spin_unlock_irq(&bitmap->counts.lock);
 }
 
 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
@@ -1570,11 +1546,9 @@ void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
        unsigned long chunk;
 
        for (chunk = s; chunk <= e; chunk++) {
-               sector_t sec = (sector_t)chunk << bitmap->chunkshift;
+               sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift;
                bitmap_set_memory_bits(bitmap, sec, 1);
-               spin_lock_irq(&bitmap->lock);
                bitmap_file_set_bit(bitmap, sec);
-               spin_unlock_irq(&bitmap->lock);
                if (sec < bitmap->mddev->recovery_cp)
                        /* We are asserting that the array is dirty,
                         * so move the recovery_cp address back so
@@ -1619,11 +1593,15 @@ static void bitmap_free(struct bitmap *bitmap)
        if (!bitmap) /* there was no bitmap */
                return;
 
-       /* release the bitmap file and kill the daemon */
-       bitmap_file_put(bitmap);
+       /* Shouldn't be needed - but just in case.... */
+       wait_event(bitmap->write_wait,
+                  atomic_read(&bitmap->pending_writes) == 0);
+
+       /* release the bitmap file  */
+       bitmap_file_unmap(&bitmap->storage);
 
-       bp = bitmap->bp;
-       pages = bitmap->pages;
+       bp = bitmap->counts.bp;
+       pages = bitmap->counts.pages;
 
        /* free all allocated memory */
 
@@ -1662,8 +1640,6 @@ int bitmap_create(struct mddev *mddev)
 {
        struct bitmap *bitmap;
        sector_t blocks = mddev->resync_max_sectors;
-       unsigned long chunks;
-       unsigned long pages;
        struct file *file = mddev->bitmap_info.file;
        int err;
        struct sysfs_dirent *bm = NULL;
@@ -1676,7 +1652,7 @@ int bitmap_create(struct mddev *mddev)
        if (!bitmap)
                return -ENOMEM;
 
-       spin_lock_init(&bitmap->lock);
+       spin_lock_init(&bitmap->counts.lock);
        atomic_set(&bitmap->pending_writes, 0);
        init_waitqueue_head(&bitmap->write_wait);
        init_waitqueue_head(&bitmap->overflow_wait);
@@ -1723,37 +1699,14 @@ int bitmap_create(struct mddev *mddev)
                goto error;
 
        bitmap->daemon_lastrun = jiffies;
-       bitmap->chunkshift = (ffz(~mddev->bitmap_info.chunksize)
-                             - BITMAP_BLOCK_SHIFT);
-
-       chunks = (blocks + (1 << bitmap->chunkshift) - 1) >>
-                       bitmap->chunkshift;
-       pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
-
-       BUG_ON(!pages);
-
-       bitmap->chunks = chunks;
-       bitmap->pages = pages;
-       bitmap->missing_pages = pages;
-
-       bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
-
-       err = -ENOMEM;
-       if (!bitmap->bp)
+       err = bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
+       if (err)
                goto error;
 
-       if (file || mddev->bitmap_info.offset) {
-               err = bitmap_storage_alloc(&bitmap->storage, bitmap->chunks,
-                                          !mddev->bitmap_info.external);
-               if (err)
-                       goto error;
-       }
        printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
-               pages, bmname(bitmap));
+              bitmap->counts.pages, bmname(bitmap));
 
        mddev->bitmap = bitmap;
-
-
        return test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
 
  error:
@@ -1815,18 +1768,19 @@ EXPORT_SYMBOL_GPL(bitmap_load);
 void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
 {
        unsigned long chunk_kb;
-       unsigned long flags;
+       struct bitmap_counts *counts;
 
        if (!bitmap)
                return;
 
-       spin_lock_irqsave(&bitmap->lock, flags);
+       counts = &bitmap->counts;
+
        chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
        seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
                   "%lu%s chunk",
-                  bitmap->pages - bitmap->missing_pages,
-                  bitmap->pages,
-                  (bitmap->pages - bitmap->missing_pages)
+                  counts->pages - counts->missing_pages,
+                  counts->pages,
+                  (counts->pages - counts->missing_pages)
                   << (PAGE_SHIFT - 10),
                   chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
                   chunk_kb ? "KB" : "B");
@@ -1836,9 +1790,172 @@ void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
        }
 
        seq_printf(seq, "\n");
-       spin_unlock_irqrestore(&bitmap->lock, flags);
 }
 
+int bitmap_resize(struct bitmap *bitmap, sector_t blocks,
+                 int chunksize, int init)
+{
+       /* If chunk_size is 0, choose an appropriate chunk size.
+        * Then possibly allocate new storage space.
+        * Then quiesce, copy bits, replace bitmap, and re-start
+        *
+        * This function is called both to set up the initial bitmap
+        * and to resize the bitmap while the array is active.
+        * If this happens as a result of the array being resized,
+        * chunksize will be zero, and we need to choose a suitable
+        * chunksize, otherwise we use what we are given.
+        */
+       struct bitmap_storage store;
+       struct bitmap_counts old_counts;
+       unsigned long chunks;
+       sector_t block;
+       sector_t old_blocks, new_blocks;
+       int chunkshift;
+       int ret = 0;
+       long pages;
+       struct bitmap_page *new_bp;
+
+       if (chunksize == 0) {
+               /* If there is enough space, leave the chunk size unchanged,
+                * else increase by factor of two until there is enough space.
+                */
+               long bytes;
+               long space = bitmap->mddev->bitmap_info.space;
+
+               if (space == 0) {
+                       /* We don't know how much space there is, so limit
+                        * to current size - in sectors.
+                        */
+                       bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
+                       if (!bitmap->mddev->bitmap_info.external)
+                               bytes += sizeof(bitmap_super_t);
+                       space = DIV_ROUND_UP(bytes, 512);
+                       bitmap->mddev->bitmap_info.space = space;
+               }
+               chunkshift = bitmap->counts.chunkshift;
+               chunkshift--;
+               do {
+                       /* 'chunkshift' is shift from block size to chunk size */
+                       chunkshift++;
+                       chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
+                       bytes = DIV_ROUND_UP(chunks, 8);
+                       if (!bitmap->mddev->bitmap_info.external)
+                               bytes += sizeof(bitmap_super_t);
+               } while (bytes > (space << 9));
+       } else
+               chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
+
+       chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
+       memset(&store, 0, sizeof(store));
+       if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
+               ret = bitmap_storage_alloc(&store, chunks,
+                                          !bitmap->mddev->bitmap_info.external);
+       if (ret)
+               goto err;
+
+       pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
+
+       new_bp = kzalloc(pages * sizeof(*new_bp), GFP_KERNEL);
+       ret = -ENOMEM;
+       if (!new_bp) {
+               bitmap_file_unmap(&store);
+               goto err;
+       }
+
+       if (!init)
+               bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
+
+       store.file = bitmap->storage.file;
+       bitmap->storage.file = NULL;
+
+       if (store.sb_page && bitmap->storage.sb_page)
+               memcpy(page_address(store.sb_page),
+                      page_address(bitmap->storage.sb_page),
+                      sizeof(bitmap_super_t));
+       bitmap_file_unmap(&bitmap->storage);
+       bitmap->storage = store;
+
+       old_counts = bitmap->counts;
+       bitmap->counts.bp = new_bp;
+       bitmap->counts.pages = pages;
+       bitmap->counts.missing_pages = pages;
+       bitmap->counts.chunkshift = chunkshift;
+       bitmap->counts.chunks = chunks;
+       bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
+                                                    BITMAP_BLOCK_SHIFT);
+
+       blocks = min(old_counts.chunks << old_counts.chunkshift,
+                    chunks << chunkshift);
+
+       spin_lock_irq(&bitmap->counts.lock);
+       for (block = 0; block < blocks; ) {
+               bitmap_counter_t *bmc_old, *bmc_new;
+               int set;
+
+               bmc_old = bitmap_get_counter(&old_counts, block,
+                                            &old_blocks, 0);
+               set = bmc_old && NEEDED(*bmc_old);
+
+               if (set) {
+                       bmc_new = bitmap_get_counter(&bitmap->counts, block,
+                                                    &new_blocks, 1);
+                       if (*bmc_new == 0) {
+                               /* need to set on-disk bits too. */
+                               sector_t end = block + new_blocks;
+                               sector_t start = block >> chunkshift;
+                               start <<= chunkshift;
+                               while (start < end) {
+                                       bitmap_file_set_bit(bitmap, block);
+                                       start += 1 << chunkshift;
+                               }
+                               *bmc_new = 2;
+                               bitmap_count_page(&bitmap->counts,
+                                                 block, 1);
+                               bitmap_set_pending(&bitmap->counts,
+                                                  block);
+                       }
+                       *bmc_new |= NEEDED_MASK;
+                       if (new_blocks < old_blocks)
+                               old_blocks = new_blocks;
+               }
+               block += old_blocks;
+       }
+
+       if (!init) {
+               int i;
+               while (block < (chunks << chunkshift)) {
+                       bitmap_counter_t *bmc;
+                       bmc = bitmap_get_counter(&bitmap->counts, block,
+                                                &new_blocks, 1);
+                       if (bmc) {
+                               /* new space.  It needs to be resynced, so
+                                * we set NEEDED_MASK.
+                                */
+                               if (*bmc == 0) {
+                                       *bmc = NEEDED_MASK | 2;
+                                       bitmap_count_page(&bitmap->counts,
+                                                         block, 1);
+                                       bitmap_set_pending(&bitmap->counts,
+                                                          block);
+                               }
+                       }
+                       block += new_blocks;
+               }
+               for (i = 0; i < bitmap->storage.file_pages; i++)
+                       set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
+       }
+       spin_unlock_irq(&bitmap->counts.lock);
+
+       if (!init) {
+               bitmap_unplug(bitmap);
+               bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
+       }
+       ret = 0;
+err:
+       return ret;
+}
+EXPORT_SYMBOL_GPL(bitmap_resize);
+
 static ssize_t
 location_show(struct mddev *mddev, char *page)
 {
This page took 0.037106 seconds and 5 git commands to generate.