[POWERPC] Fix timekeeping on PowerPC 601
[safe/jmp/linux-2.6] / drivers / md / bitmap.c
index e40d3e4..927cb34 100644 (file)
@@ -20,7 +20,6 @@
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/init.h>
-#include <linux/config.h>
 #include <linux/timer.h>
 #include <linux/sched.h>
 #include <linux/list.h>
@@ -67,7 +66,6 @@ static inline char * bmname(struct bitmap *bitmap)
        return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
 }
 
-#define WRITE_POOL_SIZE 256
 
 /*
  * just a placeholder - calls kmalloc for bitmap pages
@@ -214,8 +212,8 @@ char *file_path(struct file *file, char *buf, int count)
        if (!buf)
                return NULL;
 
-       d = file->f_dentry;
-       v = file->f_vfsmnt;
+       d = file->f_path.dentry;
+       v = file->f_path.mnt;
 
        buf = d_path(d, v, buf, count);
 
@@ -248,6 +246,8 @@ static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long inde
 
                if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
                        page->index = index;
+                       attach_page_buffers(page, NULL); /* so that free_buffer will
+                                                         * quietly no-op */
                        return page;
                }
        }
@@ -255,99 +255,193 @@ static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long inde
 
 }
 
-static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
+static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
 {
        mdk_rdev_t *rdev;
        struct list_head *tmp;
+       mddev_t *mddev = bitmap->mddev;
 
        ITERATE_RDEV(mddev, rdev, tmp)
                if (test_bit(In_sync, &rdev->flags)
-                   && !test_bit(Faulty, &rdev->flags))
+                   && !test_bit(Faulty, &rdev->flags)) {
+                       int size = PAGE_SIZE;
+                       if (page->index == bitmap->file_pages-1)
+                               size = roundup(bitmap->last_page_size,
+                                              bdev_hardsect_size(rdev->bdev));
+                       /* Just make sure we aren't corrupting data or
+                        * metadata
+                        */
+                       if (bitmap->offset < 0) {
+                               /* DATA  BITMAP METADATA  */
+                               if (bitmap->offset
+                                   + page->index * (PAGE_SIZE/512)
+                                   + size/512 > 0)
+                                       /* bitmap runs in to metadata */
+                                       return -EINVAL;
+                               if (rdev->data_offset + mddev->size*2
+                                   > rdev->sb_offset*2 + bitmap->offset)
+                                       /* data runs in to bitmap */
+                                       return -EINVAL;
+                       } else if (rdev->sb_offset*2 < rdev->data_offset) {
+                               /* METADATA BITMAP DATA */
+                               if (rdev->sb_offset*2
+                                   + bitmap->offset
+                                   + page->index*(PAGE_SIZE/512) + size/512
+                                   > rdev->data_offset)
+                                       /* bitmap runs in to data */
+                                       return -EINVAL;
+                       } else {
+                               /* DATA METADATA BITMAP - no problems */
+                       }
                        md_super_write(mddev, rdev,
-                                      (rdev->sb_offset<<1) + offset
+                                      (rdev->sb_offset<<1) + bitmap->offset
                                       + page->index * (PAGE_SIZE/512),
-                                      PAGE_SIZE,
+                                      size,
                                       page);
+               }
 
        if (wait)
                md_super_wait(mddev);
        return 0;
 }
 
+static void bitmap_file_kick(struct bitmap *bitmap);
 /*
  * write out a page to a file
  */
-static int write_page(struct bitmap *bitmap, struct page *page, int wait)
+static void write_page(struct bitmap *bitmap, struct page *page, int wait)
 {
-       int ret = -ENOMEM;
+       struct buffer_head *bh;
 
-       if (bitmap->file == NULL)
-               return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
+       if (bitmap->file == NULL) {
+               switch (write_sb_page(bitmap, page, wait)) {
+               case -EINVAL:
+                       bitmap->flags |= BITMAP_WRITE_ERROR;
+               }
+       } else {
 
-       flush_dcache_page(page); /* make sure visible to anyone reading the file */
+               bh = page_buffers(page);
 
-       if (wait)
-               lock_page(page);
-       else {
-               if (TestSetPageLocked(page))
-                       return -EAGAIN; /* already locked */
-               if (PageWriteback(page)) {
-                       unlock_page(page);
-                       return -EAGAIN;
+               while (bh && bh->b_blocknr) {
+                       atomic_inc(&bitmap->pending_writes);
+                       set_buffer_locked(bh);
+                       set_buffer_mapped(bh);
+                       submit_bh(WRITE, bh);
+                       bh = bh->b_this_page;
+               }
+
+               if (wait) {
+                       wait_event(bitmap->write_wait,
+                                  atomic_read(&bitmap->pending_writes)==0);
                }
        }
+       if (bitmap->flags & BITMAP_WRITE_ERROR)
+               bitmap_file_kick(bitmap);
+}
+
+static void end_bitmap_write(struct buffer_head *bh, int uptodate)
+{
+       struct bitmap *bitmap = bh->b_private;
+       unsigned long flags;
 
-       ret = page->mapping->a_ops->prepare_write(bitmap->file, page, 0, PAGE_SIZE);
-       if (!ret)
-               ret = page->mapping->a_ops->commit_write(bitmap->file, page, 0,
-                       PAGE_SIZE);
-       if (ret) {
-               unlock_page(page);
-               return ret;
+       if (!uptodate) {
+               spin_lock_irqsave(&bitmap->lock, flags);
+               bitmap->flags |= BITMAP_WRITE_ERROR;
+               spin_unlock_irqrestore(&bitmap->lock, flags);
        }
+       if (atomic_dec_and_test(&bitmap->pending_writes))
+               wake_up(&bitmap->write_wait);
+}
 
-       set_page_dirty(page); /* force it to be written out */
+/* copied from buffer.c */
+static void
+__clear_page_buffers(struct page *page)
+{
+       ClearPagePrivate(page);
+       set_page_private(page, 0);
+       page_cache_release(page);
+}
+static void free_buffers(struct page *page)
+{
+       struct buffer_head *bh = page_buffers(page);
 
-       if (!wait) {
-               /* add to list to be waited for */
-               struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
-               item->page = page;
-               spin_lock(&bitmap->write_lock);
-               list_add(&item->list, &bitmap->complete_pages);
-               spin_unlock(&bitmap->write_lock);
+       while (bh) {
+               struct buffer_head *next = bh->b_this_page;
+               free_buffer_head(bh);
+               bh = next;
        }
-       return write_one_page(page, wait);
+       __clear_page_buffers(page);
+       put_page(page);
 }
 
-/* read a page from a file, pinning it into cache, and return bytes_read */
+/* read a page from a file.
+ * We both read the page, and attach buffers to the page to record the
+ * address of each block (using bmap).  These addresses will be used
+ * to write the block later, completely bypassing the filesystem.
+ * This usage is similar to how swap files are handled, and allows us
+ * to write to a file with no concerns of memory allocation failing.
+ */
 static struct page *read_page(struct file *file, unsigned long index,
-                                       unsigned long *bytes_read)
+                             struct bitmap *bitmap,
+                             unsigned long count)
 {
-       struct inode *inode = file->f_mapping->host;
        struct page *page = NULL;
-       loff_t isize = i_size_read(inode);
-       unsigned long end_index = isize >> PAGE_SHIFT;
+       struct inode *inode = file->f_path.dentry->d_inode;
+       struct buffer_head *bh;
+       sector_t block;
 
        PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
                        (unsigned long long)index << PAGE_SHIFT);
 
-       page = read_cache_page(inode->i_mapping, index,
-                       (filler_t *)inode->i_mapping->a_ops->readpage, file);
+       page = alloc_page(GFP_KERNEL);
+       if (!page)
+               page = ERR_PTR(-ENOMEM);
        if (IS_ERR(page))
                goto out;
-       wait_on_page_locked(page);
-       if (!PageUptodate(page) || PageError(page)) {
+
+       bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
+       if (!bh) {
                put_page(page);
-               page = ERR_PTR(-EIO);
+               page = ERR_PTR(-ENOMEM);
                goto out;
        }
+       attach_page_buffers(page, bh);
+       block = index << (PAGE_SHIFT - inode->i_blkbits);
+       while (bh) {
+               if (count == 0)
+                       bh->b_blocknr = 0;
+               else {
+                       bh->b_blocknr = bmap(inode, block);
+                       if (bh->b_blocknr == 0) {
+                               /* Cannot use this file! */
+                               free_buffers(page);
+                               page = ERR_PTR(-EINVAL);
+                               goto out;
+                       }
+                       bh->b_bdev = inode->i_sb->s_bdev;
+                       if (count < (1<<inode->i_blkbits))
+                               count = 0;
+                       else
+                               count -= (1<<inode->i_blkbits);
+
+                       bh->b_end_io = end_bitmap_write;
+                       bh->b_private = bitmap;
+                       atomic_inc(&bitmap->pending_writes);
+                       set_buffer_locked(bh);
+                       set_buffer_mapped(bh);
+                       submit_bh(READ, bh);
+               }
+               block++;
+               bh = bh->b_this_page;
+       }
+       page->index = index;
 
-       if (index > end_index) /* we have read beyond EOF */
-               *bytes_read = 0;
-       else if (index == end_index) /* possible short read */
-               *bytes_read = isize & ~PAGE_MASK;
-       else
-               *bytes_read = PAGE_SIZE; /* got a full page */
+       wait_event(bitmap->write_wait,
+                  atomic_read(&bitmap->pending_writes)==0);
+       if (bitmap->flags & BITMAP_WRITE_ERROR) {
+               free_buffers(page);
+               page = ERR_PTR(-EIO);
+       }
 out:
        if (IS_ERR(page))
                printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
@@ -362,17 +456,17 @@ out:
  */
 
 /* update the event counter and sync the superblock to disk */
-int bitmap_update_sb(struct bitmap *bitmap)
+void bitmap_update_sb(struct bitmap *bitmap)
 {
        bitmap_super_t *sb;
        unsigned long flags;
 
        if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
-               return 0;
+               return;
        spin_lock_irqsave(&bitmap->lock, flags);
        if (!bitmap->sb_page) { /* no superblock */
                spin_unlock_irqrestore(&bitmap->lock, flags);
-               return 0;
+               return;
        }
        spin_unlock_irqrestore(&bitmap->lock, flags);
        sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
@@ -380,7 +474,7 @@ int bitmap_update_sb(struct bitmap *bitmap)
        if (!bitmap->mddev->degraded)
                sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
        kunmap_atomic(sb, KM_USER0);
-       return write_page(bitmap, bitmap->sb_page, 1);
+       write_page(bitmap, bitmap->sb_page, 1);
 }
 
 /* print out the bitmap file superblock */
@@ -418,16 +512,17 @@ static int bitmap_read_sb(struct bitmap *bitmap)
        char *reason = NULL;
        bitmap_super_t *sb;
        unsigned long chunksize, daemon_sleep, write_behind;
-       unsigned long bytes_read;
        unsigned long long events;
        int err = -EINVAL;
 
        /* page 0 is the superblock, read it... */
-       if (bitmap->file)
-               bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
-       else {
+       if (bitmap->file) {
+               loff_t isize = i_size_read(bitmap->file->f_mapping->host);
+               int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
+
+               bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
+       } else {
                bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
-               bytes_read = PAGE_SIZE;
        }
        if (IS_ERR(bitmap->sb_page)) {
                err = PTR_ERR(bitmap->sb_page);
@@ -437,13 +532,6 @@ static int bitmap_read_sb(struct bitmap *bitmap)
 
        sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
 
-       if (bytes_read < sizeof(*sb)) { /* short read */
-               printk(KERN_INFO "%s: bitmap file superblock truncated\n",
-                       bmname(bitmap));
-               err = -ENOSPC;
-               goto out;
-       }
-
        chunksize = le32_to_cpu(sb->chunksize);
        daemon_sleep = le32_to_cpu(sb->daemon_sleep);
        write_behind = le32_to_cpu(sb->write_behind);
@@ -488,7 +576,7 @@ static int bitmap_read_sb(struct bitmap *bitmap)
                printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
                        "-- forcing full recovery\n", bmname(bitmap), events,
                        (unsigned long long) bitmap->mddev->events);
-               sb->state |= BITMAP_STALE;
+               sb->state |= cpu_to_le32(BITMAP_STALE);
        }
 success:
        /* assign fields using values from superblock */
@@ -496,11 +584,11 @@ success:
        bitmap->daemon_sleep = daemon_sleep;
        bitmap->daemon_lastrun = jiffies;
        bitmap->max_write_behind = write_behind;
-       bitmap->flags |= sb->state;
+       bitmap->flags |= le32_to_cpu(sb->state);
        if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
                bitmap->flags |= BITMAP_HOSTENDIAN;
        bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
-       if (sb->state & BITMAP_STALE)
+       if (sb->state & cpu_to_le32(BITMAP_STALE))
                bitmap->events_cleared = bitmap->mddev->events;
        err = 0;
 out:
@@ -515,28 +603,31 @@ enum bitmap_mask_op {
        MASK_UNSET
 };
 
-/* record the state of the bitmap in the superblock */
-static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
-                               enum bitmap_mask_op op)
+/* record the state of the bitmap in the superblock.  Return the old value */
+static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
+                            enum bitmap_mask_op op)
 {
        bitmap_super_t *sb;
        unsigned long flags;
+       int old;
 
        spin_lock_irqsave(&bitmap->lock, flags);
        if (!bitmap->sb_page) { /* can't set the state */
                spin_unlock_irqrestore(&bitmap->lock, flags);
-               return;
+               return 0;
        }
        spin_unlock_irqrestore(&bitmap->lock, flags);
        sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
+       old = le32_to_cpu(sb->state) & bits;
        switch (op) {
-               case MASK_SET: sb->state |= bits;
+               case MASK_SET: sb->state |= cpu_to_le32(bits);
                                break;
-               case MASK_UNSET: sb->state &= ~bits;
+               case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
                                break;
                default: BUG();
        }
        kunmap_atomic(sb, KM_USER0);
+       return old;
 }
 
 /*
@@ -565,6 +656,7 @@ static inline unsigned long file_page_offset(unsigned long chunk)
 static inline struct page *filemap_get_page(struct bitmap *bitmap,
                                        unsigned long chunk)
 {
+       if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
        return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
 }
 
@@ -589,37 +681,12 @@ static void bitmap_file_unmap(struct bitmap *bitmap)
 
        while (pages--)
                if (map[pages]->index != 0) /* 0 is sb_page, release it below */
-                       put_page(map[pages]);
+                       free_buffers(map[pages]);
        kfree(map);
        kfree(attr);
 
-       safe_put_page(sb_page);
-}
-
-/* dequeue the next item in a page list -- don't call from irq context */
-static struct page_list *dequeue_page(struct bitmap *bitmap)
-{
-       struct page_list *item = NULL;
-       struct list_head *head = &bitmap->complete_pages;
-
-       spin_lock(&bitmap->write_lock);
-       if (list_empty(head))
-               goto out;
-       item = list_entry(head->prev, struct page_list, list);
-       list_del(head->prev);
-out:
-       spin_unlock(&bitmap->write_lock);
-       return item;
-}
-
-static void drain_write_queues(struct bitmap *bitmap)
-{
-       struct page_list *item;
-
-       while ((item = dequeue_page(bitmap))) {
-               /* don't bother to wait */
-               mempool_free(item, bitmap->write_pool);
-       }
+       if (sb_page)
+               free_buffers(sb_page);
 }
 
 static void bitmap_file_put(struct bitmap *bitmap)
@@ -632,12 +699,16 @@ static void bitmap_file_put(struct bitmap *bitmap)
        bitmap->file = NULL;
        spin_unlock_irqrestore(&bitmap->lock, flags);
 
-       drain_write_queues(bitmap);
-
+       if (file)
+               wait_event(bitmap->write_wait,
+                          atomic_read(&bitmap->pending_writes)==0);
        bitmap_file_unmap(bitmap);
 
-       if (file)
+       if (file) {
+               struct inode *inode = file->f_path.dentry->d_inode;
+               invalidate_mapping_pages(inode->i_mapping, 0, -1);
                fput(file);
+       }
 }
 
 
@@ -650,18 +721,23 @@ static void bitmap_file_kick(struct bitmap *bitmap)
 {
        char *path, *ptr = NULL;
 
-       bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
-       bitmap_update_sb(bitmap);
+       if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
+               bitmap_update_sb(bitmap);
 
-       if (bitmap->file) {
-               path = kmalloc(PAGE_SIZE, GFP_KERNEL);
-               if (path)
-                       ptr = file_path(bitmap->file, path, PAGE_SIZE);
+               if (bitmap->file) {
+                       path = kmalloc(PAGE_SIZE, GFP_KERNEL);
+                       if (path)
+                               ptr = file_path(bitmap->file, path, PAGE_SIZE);
 
-               printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
-                      bmname(bitmap), ptr ? ptr : "");
+                       printk(KERN_ALERT
+                             "%s: kicking failed bitmap file %s from array!\n",
+                             bmname(bitmap), ptr ? ptr : "");
 
-               kfree(path);
+                       kfree(path);
+               } else
+                       printk(KERN_ALERT
+                              "%s: disabling internal bitmap due to errors\n",
+                              bmname(bitmap));
        }
 
        bitmap_file_put(bitmap);
@@ -712,6 +788,7 @@ static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
        }
 
        page = filemap_get_page(bitmap, chunk);
+       if (!page) return;
        bit = file_page_offset(chunk);
 
        /* set the bit */
@@ -728,21 +805,18 @@ static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
 
 }
 
-static void bitmap_writeback(struct bitmap *bitmap);
-
 /* this gets called when the md device is ready to unplug its underlying
  * (slave) device queues -- before we let any writes go down, we need to
  * sync the dirty pages of the bitmap file to disk */
-int bitmap_unplug(struct bitmap *bitmap)
+void bitmap_unplug(struct bitmap *bitmap)
 {
        unsigned long i, flags;
        int dirty, need_write;
        struct page *page;
        int wait = 0;
-       int err;
 
        if (!bitmap)
-               return 0;
+               return;
 
        /* look at each page to see if there are any set bits that need to be
         * flushed out to disk */
@@ -750,7 +824,7 @@ int bitmap_unplug(struct bitmap *bitmap)
                spin_lock_irqsave(&bitmap->lock, flags);
                if (!bitmap->filemap) {
                        spin_unlock_irqrestore(&bitmap->lock, flags);
-                       return 0;
+                       return;
                }
                page = bitmap->filemap[i];
                dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
@@ -761,25 +835,18 @@ int bitmap_unplug(struct bitmap *bitmap)
                        wait = 1;
                spin_unlock_irqrestore(&bitmap->lock, flags);
 
-               if (dirty | need_write) {
-                       err = write_page(bitmap, page, 0);
-                       if (err == -EAGAIN) {
-                               if (dirty)
-                                       err = write_page(bitmap, page, 1);
-                               else
-                                       err = 0;
-                       }
-                       if (err)
-                               return 1;
-               }
+               if (dirty | need_write)
+                       write_page(bitmap, page, 0);
        }
        if (wait) { /* if any writes were performed, we need to wait on them */
                if (bitmap->file)
-                       bitmap_writeback(bitmap);
+                       wait_event(bitmap->write_wait,
+                                  atomic_read(&bitmap->pending_writes)==0);
                else
                        md_super_wait(bitmap->mddev);
        }
-       return 0;
+       if (bitmap->flags & BITMAP_WRITE_ERROR)
+               bitmap_file_kick(bitmap);
 }
 
 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
@@ -800,7 +867,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
        struct page *page = NULL, *oldpage = NULL;
        unsigned long num_pages, bit_cnt = 0;
        struct file *file;
-       unsigned long bytes, offset, dummy;
+       unsigned long bytes, offset;
        int outofdate;
        int ret = -ENOSPC;
        void *paddr;
@@ -828,23 +895,21 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
                        bmname(bitmap),
                        (unsigned long) i_size_read(file->f_mapping->host),
                        bytes + sizeof(bitmap_super_t));
-               goto out;
+               goto err;
        }
 
        ret = -ENOMEM;
 
        bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
        if (!bitmap->filemap)
-               goto out;
+               goto err;
 
        /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
        bitmap->filemap_attr = kzalloc(
-               (((num_pages*4/8)+sizeof(unsigned long)-1)
-                /sizeof(unsigned long))
-               *sizeof(unsigned long),
+               roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
                GFP_KERNEL);
        if (!bitmap->filemap_attr)
-               goto out;
+               goto err;
 
        oldindex = ~0L;
 
@@ -853,7 +918,13 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
                index = file_page_index(i);
                bit = file_page_offset(i);
                if (index != oldindex) { /* this is a new page, read it in */
+                       int count;
                        /* unmap the old page, we're done with it */
+                       if (index == num_pages-1)
+                               count = bytes + sizeof(bitmap_super_t)
+                                       - index * PAGE_SIZE;
+                       else
+                               count = PAGE_SIZE;
                        if (index == 0) {
                                /*
                                 * if we're here then the superblock page
@@ -863,7 +934,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
                                page = bitmap->sb_page;
                                offset = sizeof(bitmap_super_t);
                        } else if (file) {
-                               page = read_page(file, index, &dummy);
+                               page = read_page(file, index, bitmap, count);
                                offset = 0;
                        } else {
                                page = read_sb_page(bitmap->mddev, bitmap->offset, index);
@@ -871,7 +942,7 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
                        }
                        if (IS_ERR(page)) { /* read error */
                                ret = PTR_ERR(page);
-                               goto out;
+                               goto err;
                        }
 
                        oldindex = index;
@@ -886,15 +957,18 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
                                memset(paddr + offset, 0xff,
                                       PAGE_SIZE - offset);
                                kunmap_atomic(paddr, KM_USER0);
-                               ret = write_page(bitmap, page, 1);
-                               if (ret) {
+                               write_page(bitmap, page, 1);
+
+                               ret = -EIO;
+                               if (bitmap->flags & BITMAP_WRITE_ERROR) {
                                        /* release, page not in filemap yet */
                                        put_page(page);
-                                       goto out;
+                                       goto err;
                                }
                        }
 
                        bitmap->filemap[bitmap->file_pages++] = page;
+                       bitmap->last_page_size = count;
                }
                paddr = kmap_atomic(page, KM_USER0);
                if (bitmap->flags & BITMAP_HOSTENDIAN)
@@ -921,11 +995,15 @@ static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
                md_wakeup_thread(bitmap->mddev->thread);
        }
 
-out:
        printk(KERN_INFO "%s: bitmap initialized from disk: "
-               "read %lu/%lu pages, set %lu bits, status: %d\n",
-               bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
+               "read %lu/%lu pages, set %lu bits\n",
+               bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
+
+       return 0;
 
+ err:
+       printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
+              bmname(bitmap), ret);
        return ret;
 }
 
@@ -962,19 +1040,18 @@ static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  *                     out to disk
  */
 
-int bitmap_daemon_work(struct bitmap *bitmap)
+void bitmap_daemon_work(struct bitmap *bitmap)
 {
        unsigned long j;
        unsigned long flags;
        struct page *page = NULL, *lastpage = NULL;
-       int err = 0;
        int blocks;
        void *paddr;
 
        if (bitmap == NULL)
-               return 0;
+               return;
        if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
-               return 0;
+               return;
        bitmap->daemon_lastrun = jiffies;
 
        for (j = 0; j < bitmap->chunks; j++) {
@@ -997,17 +1074,8 @@ int bitmap_daemon_work(struct bitmap *bitmap)
                                        clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
 
                                spin_unlock_irqrestore(&bitmap->lock, flags);
-                               if (need_write) {
-                                       switch (write_page(bitmap, page, 0)) {
-                                       case -EAGAIN:
-                                               set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
-                                               break;
-                                       case 0:
-                                               break;
-                                       default:
-                                               bitmap_file_kick(bitmap);
-                                       }
-                               }
+                               if (need_write)
+                                       write_page(bitmap, page, 0);
                                continue;
                        }
 
@@ -1016,17 +1084,11 @@ int bitmap_daemon_work(struct bitmap *bitmap)
                                if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
                                        clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
                                        spin_unlock_irqrestore(&bitmap->lock, flags);
-                                       err = write_page(bitmap, lastpage, 0);
-                                       if (err == -EAGAIN) {
-                                               err = 0;
-                                               set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
-                                       }
+                                       write_page(bitmap, lastpage, 0);
                                } else {
                                        set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
                                        spin_unlock_irqrestore(&bitmap->lock, flags);
                                }
-                               if (err)
-                                       bitmap_file_kick(bitmap);
                        } else
                                spin_unlock_irqrestore(&bitmap->lock, flags);
                        lastpage = page;
@@ -1069,44 +1131,13 @@ int bitmap_daemon_work(struct bitmap *bitmap)
                if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
                        clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
                        spin_unlock_irqrestore(&bitmap->lock, flags);
-                       err = write_page(bitmap, lastpage, 0);
-                       if (err == -EAGAIN) {
-                               set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
-                               err = 0;
-                       }
+                       write_page(bitmap, lastpage, 0);
                } else {
                        set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
                        spin_unlock_irqrestore(&bitmap->lock, flags);
                }
        }
 
-       return err;
-}
-
-static void bitmap_writeback(struct bitmap *bitmap)
-{
-       struct page *page;
-       struct page_list *item;
-       int err = 0;
-
-       PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
-       /* wait on bitmap page writebacks */
-       while ((item = dequeue_page(bitmap))) {
-               page = item->page;
-               mempool_free(item, bitmap->write_pool);
-               PRINTK("wait on page writeback: %p\n", page);
-               wait_on_page_writeback(page);
-               PRINTK("finished page writeback: %p\n", page);
-
-               err = PageError(page);
-               if (err) {
-                       printk(KERN_WARNING "%s: bitmap file writeback "
-                              "failed (page %lu): %d\n",
-                              bmname(bitmap), page->index, err);
-                       bitmap_file_kick(bitmap);
-                       break;
-               }
-       }
 }
 
 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
@@ -1167,6 +1198,22 @@ int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sect
                        return 0;
                }
 
+               if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
+                       DEFINE_WAIT(__wait);
+                       /* note that it is safe to do the prepare_to_wait
+                        * after the test as long as we do it before dropping
+                        * the spinlock.
+                        */
+                       prepare_to_wait(&bitmap->overflow_wait, &__wait,
+                                       TASK_UNINTERRUPTIBLE);
+                       spin_unlock_irq(&bitmap->lock);
+                       bitmap->mddev->queue
+                               ->unplug_fn(bitmap->mddev->queue);
+                       schedule();
+                       finish_wait(&bitmap->overflow_wait, &__wait);
+                       continue;
+               }
+
                switch(*bmc) {
                case 0:
                        bitmap_file_set_bit(bitmap, offset);
@@ -1176,7 +1223,7 @@ int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sect
                case 1:
                        *bmc = 2;
                }
-               BUG_ON((*bmc & COUNTER_MAX) == COUNTER_MAX);
+
                (*bmc)++;
 
                spin_unlock_irq(&bitmap->lock);
@@ -1214,6 +1261,9 @@ void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long secto
                if (!success && ! (*bmc & NEEDED_MASK))
                        *bmc |= NEEDED_MASK;
 
+               if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
+                       wake_up(&bitmap->overflow_wait);
+
                (*bmc)--;
                if (*bmc <= 2) {
                        set_page_attr(bitmap,
@@ -1335,6 +1385,18 @@ static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int n
 
 }
 
+/* dirty the memory and file bits for bitmap chunks "s" to "e" */
+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 = chunk << CHUNK_BLOCK_SHIFT(bitmap);
+               bitmap_set_memory_bits(bitmap, sec, 1);
+               bitmap_file_set_bit(bitmap, sec);
+       }
+}
+
 /*
  * flush out any pending updates
  */
@@ -1377,8 +1439,6 @@ static void bitmap_free(struct bitmap *bitmap)
 
        /* free all allocated memory */
 
-       mempool_destroy(bitmap->write_pool);
-
        if (bp) /* deallocate the page memory */
                for (k = 0; k < pages; k++)
                        if (bp[k].map && !bp[k].hijacked)
@@ -1414,7 +1474,7 @@ int bitmap_create(mddev_t *mddev)
        int err;
        sector_t start;
 
-       BUG_ON(sizeof(bitmap_super_t) != 256);
+       BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
 
        if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
                return 0;
@@ -1426,26 +1486,27 @@ int bitmap_create(mddev_t *mddev)
                return -ENOMEM;
 
        spin_lock_init(&bitmap->lock);
-       bitmap->mddev = mddev;
+       atomic_set(&bitmap->pending_writes, 0);
+       init_waitqueue_head(&bitmap->write_wait);
+       init_waitqueue_head(&bitmap->overflow_wait);
 
-       spin_lock_init(&bitmap->write_lock);
-       INIT_LIST_HEAD(&bitmap->complete_pages);
-       bitmap->write_pool = mempool_create_kmalloc_pool(WRITE_POOL_SIZE,
-                                               sizeof(struct page_list));
-       err = -ENOMEM;
-       if (!bitmap->write_pool)
-               goto error;
+       bitmap->mddev = mddev;
 
        bitmap->file = file;
        bitmap->offset = mddev->bitmap_offset;
-       if (file) get_file(file);
+       if (file) {
+               get_file(file);
+               do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
+                                     SYNC_FILE_RANGE_WAIT_BEFORE |
+                                     SYNC_FILE_RANGE_WRITE |
+                                     SYNC_FILE_RANGE_WAIT_AFTER);
+       }
        /* read superblock from bitmap file (this sets bitmap->chunksize) */
        err = bitmap_read_sb(bitmap);
        if (err)
                goto error;
 
-       bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
-                                       sizeof(bitmap->chunksize));
+       bitmap->chunkshift = ffz(~bitmap->chunksize);
 
        /* now that chunksize and chunkshift are set, we can use these macros */
        chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
@@ -1489,7 +1550,9 @@ int bitmap_create(mddev_t *mddev)
 
        mddev->thread->timeout = bitmap->daemon_sleep * HZ;
 
-       return bitmap_update_sb(bitmap);
+       bitmap_update_sb(bitmap);
+
+       return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
 
  error:
        bitmap_free(bitmap);
@@ -1503,4 +1566,3 @@ EXPORT_SYMBOL(bitmap_start_sync);
 EXPORT_SYMBOL(bitmap_end_sync);
 EXPORT_SYMBOL(bitmap_unplug);
 EXPORT_SYMBOL(bitmap_close_sync);
-EXPORT_SYMBOL(bitmap_daemon_work);