2 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
4 * bitmap_create - sets up the bitmap structure
5 * bitmap_destroy - destroys the bitmap structure
7 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
8 * - added disk storage for bitmap
9 * - changes to allow various bitmap chunk sizes
15 * flush after percent set rather than just time based. (maybe both).
16 * wait if count gets too high, wake when it drops to half.
19 #include <linux/blkdev.h>
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/timer.h>
25 #include <linux/sched.h>
26 #include <linux/list.h>
27 #include <linux/file.h>
28 #include <linux/mount.h>
29 #include <linux/buffer_head.h>
38 /* these are for debugging purposes only! */
40 /* define one and only one of these */
41 #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
42 #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
43 #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
44 #define INJECT_FAULTS_4 0 /* undef */
45 #define INJECT_FAULTS_5 0 /* undef */
46 #define INJECT_FAULTS_6 0
48 /* if these are defined, the driver will fail! debug only */
49 #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
50 #define INJECT_FATAL_FAULT_2 0 /* undef */
51 #define INJECT_FATAL_FAULT_3 0 /* undef */
54 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
55 #define DPRINTK(x...) do { } while(0)
59 # define PRINTK(x...) printk(KERN_DEBUG x)
65 static inline char * bmname(struct bitmap *bitmap)
67 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
72 * just a placeholder - calls kmalloc for bitmap pages
74 static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
78 #ifdef INJECT_FAULTS_1
81 page = kmalloc(PAGE_SIZE, GFP_NOIO);
84 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
86 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
87 bmname(bitmap), page);
92 * for now just a placeholder -- just calls kfree for bitmap pages
94 static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
96 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
101 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
103 * 1) check to see if this page is allocated, if it's not then try to alloc
104 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
105 * page pointer directly as a counter
107 * if we find our page, we increment the page's refcount so that it stays
108 * allocated while we're using it
110 static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
111 __releases(bitmap->lock)
112 __acquires(bitmap->lock)
114 unsigned char *mappage;
116 if (page >= bitmap->pages) {
117 /* This can happen if bitmap_start_sync goes beyond
118 * End-of-device while looking for a whole page.
125 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
128 if (bitmap->bp[page].map) /* page is already allocated, just return */
134 spin_unlock_irq(&bitmap->lock);
136 /* this page has not been allocated yet */
138 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
139 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
141 /* failed - set the hijacked flag so that we can use the
142 * pointer as a counter */
143 spin_lock_irq(&bitmap->lock);
144 if (!bitmap->bp[page].map)
145 bitmap->bp[page].hijacked = 1;
151 spin_lock_irq(&bitmap->lock);
153 /* recheck the page */
155 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
156 /* somebody beat us to getting the page */
157 bitmap_free_page(bitmap, mappage);
161 /* no page was in place and we have one, so install it */
163 memset(mappage, 0, PAGE_SIZE);
164 bitmap->bp[page].map = mappage;
165 bitmap->missing_pages--;
171 /* if page is completely empty, put it back on the free list, or dealloc it */
172 /* if page was hijacked, unmark the flag so it might get alloced next time */
173 /* Note: lock should be held when calling this */
174 static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
178 if (bitmap->bp[page].count) /* page is still busy */
181 /* page is no longer in use, it can be released */
183 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
184 bitmap->bp[page].hijacked = 0;
185 bitmap->bp[page].map = NULL;
189 /* normal case, free the page */
192 /* actually ... let's not. We will probably need the page again exactly when
193 * memory is tight and we are flusing to disk
197 ptr = bitmap->bp[page].map;
198 bitmap->bp[page].map = NULL;
199 bitmap->missing_pages++;
200 bitmap_free_page(bitmap, ptr);
207 * bitmap file handling - read and write the bitmap file and its superblock
211 * basic page I/O operations
214 /* IO operations when bitmap is stored near all superblocks */
215 static struct page *read_sb_page(mddev_t *mddev, long offset,
217 unsigned long index, int size)
219 /* choose a good rdev and read the page from there */
225 page = alloc_page(GFP_KERNEL);
227 return ERR_PTR(-ENOMEM);
229 list_for_each_entry(rdev, &mddev->disks, same_set) {
230 if (! test_bit(In_sync, &rdev->flags)
231 || test_bit(Faulty, &rdev->flags))
234 target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
236 if (sync_page_io(rdev->bdev, target,
237 roundup(size, bdev_logical_block_size(rdev->bdev)),
240 attach_page_buffers(page, NULL); /* so that free_buffer will
245 return ERR_PTR(-EIO);
249 static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
251 /* Iterate the disks of an mddev, using rcu to protect access to the
252 * linked list, and raising the refcount of devices we return to ensure
253 * they don't disappear while in use.
254 * As devices are only added or removed when raid_disk is < 0 and
255 * nr_pending is 0 and In_sync is clear, the entries we return will
256 * still be in the same position on the list when we re-enter
257 * list_for_each_continue_rcu.
259 struct list_head *pos;
262 /* start at the beginning */
265 /* release the previous rdev and start from there. */
266 rdev_dec_pending(rdev, mddev);
267 pos = &rdev->same_set;
269 list_for_each_continue_rcu(pos, &mddev->disks) {
270 rdev = list_entry(pos, mdk_rdev_t, same_set);
271 if (rdev->raid_disk >= 0 &&
272 !test_bit(Faulty, &rdev->flags)) {
273 /* this is a usable devices */
274 atomic_inc(&rdev->nr_pending);
283 static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
285 mdk_rdev_t *rdev = NULL;
286 mddev_t *mddev = bitmap->mddev;
288 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
289 int size = PAGE_SIZE;
290 if (page->index == bitmap->file_pages-1)
291 size = roundup(bitmap->last_page_size,
292 bdev_logical_block_size(rdev->bdev));
293 /* Just make sure we aren't corrupting data or
296 if (bitmap->offset < 0) {
297 /* DATA BITMAP METADATA */
299 + (long)(page->index * (PAGE_SIZE/512))
301 /* bitmap runs in to metadata */
303 if (rdev->data_offset + mddev->dev_sectors
304 > rdev->sb_start + bitmap->offset)
305 /* data runs in to bitmap */
307 } else if (rdev->sb_start < rdev->data_offset) {
308 /* METADATA BITMAP DATA */
311 + page->index*(PAGE_SIZE/512) + size/512
313 /* bitmap runs in to data */
316 /* DATA METADATA BITMAP - no problems */
318 md_super_write(mddev, rdev,
319 rdev->sb_start + bitmap->offset
320 + page->index * (PAGE_SIZE/512),
326 md_super_wait(mddev);
333 static void bitmap_file_kick(struct bitmap *bitmap);
335 * write out a page to a file
337 static void write_page(struct bitmap *bitmap, struct page *page, int wait)
339 struct buffer_head *bh;
341 if (bitmap->file == NULL) {
342 switch (write_sb_page(bitmap, page, wait)) {
344 bitmap->flags |= BITMAP_WRITE_ERROR;
348 bh = page_buffers(page);
350 while (bh && bh->b_blocknr) {
351 atomic_inc(&bitmap->pending_writes);
352 set_buffer_locked(bh);
353 set_buffer_mapped(bh);
354 submit_bh(WRITE, bh);
355 bh = bh->b_this_page;
359 wait_event(bitmap->write_wait,
360 atomic_read(&bitmap->pending_writes)==0);
363 if (bitmap->flags & BITMAP_WRITE_ERROR)
364 bitmap_file_kick(bitmap);
367 static void end_bitmap_write(struct buffer_head *bh, int uptodate)
369 struct bitmap *bitmap = bh->b_private;
373 spin_lock_irqsave(&bitmap->lock, flags);
374 bitmap->flags |= BITMAP_WRITE_ERROR;
375 spin_unlock_irqrestore(&bitmap->lock, flags);
377 if (atomic_dec_and_test(&bitmap->pending_writes))
378 wake_up(&bitmap->write_wait);
381 /* copied from buffer.c */
383 __clear_page_buffers(struct page *page)
385 ClearPagePrivate(page);
386 set_page_private(page, 0);
387 page_cache_release(page);
389 static void free_buffers(struct page *page)
391 struct buffer_head *bh = page_buffers(page);
394 struct buffer_head *next = bh->b_this_page;
395 free_buffer_head(bh);
398 __clear_page_buffers(page);
402 /* read a page from a file.
403 * We both read the page, and attach buffers to the page to record the
404 * address of each block (using bmap). These addresses will be used
405 * to write the block later, completely bypassing the filesystem.
406 * This usage is similar to how swap files are handled, and allows us
407 * to write to a file with no concerns of memory allocation failing.
409 static struct page *read_page(struct file *file, unsigned long index,
410 struct bitmap *bitmap,
413 struct page *page = NULL;
414 struct inode *inode = file->f_path.dentry->d_inode;
415 struct buffer_head *bh;
418 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
419 (unsigned long long)index << PAGE_SHIFT);
421 page = alloc_page(GFP_KERNEL);
423 page = ERR_PTR(-ENOMEM);
427 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
430 page = ERR_PTR(-ENOMEM);
433 attach_page_buffers(page, bh);
434 block = index << (PAGE_SHIFT - inode->i_blkbits);
439 bh->b_blocknr = bmap(inode, block);
440 if (bh->b_blocknr == 0) {
441 /* Cannot use this file! */
443 page = ERR_PTR(-EINVAL);
446 bh->b_bdev = inode->i_sb->s_bdev;
447 if (count < (1<<inode->i_blkbits))
450 count -= (1<<inode->i_blkbits);
452 bh->b_end_io = end_bitmap_write;
453 bh->b_private = bitmap;
454 atomic_inc(&bitmap->pending_writes);
455 set_buffer_locked(bh);
456 set_buffer_mapped(bh);
460 bh = bh->b_this_page;
464 wait_event(bitmap->write_wait,
465 atomic_read(&bitmap->pending_writes)==0);
466 if (bitmap->flags & BITMAP_WRITE_ERROR) {
468 page = ERR_PTR(-EIO);
472 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
474 (unsigned long long)index << PAGE_SHIFT,
480 * bitmap file superblock operations
483 /* update the event counter and sync the superblock to disk */
484 void bitmap_update_sb(struct bitmap *bitmap)
489 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
491 spin_lock_irqsave(&bitmap->lock, flags);
492 if (!bitmap->sb_page) { /* no superblock */
493 spin_unlock_irqrestore(&bitmap->lock, flags);
496 spin_unlock_irqrestore(&bitmap->lock, flags);
497 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
498 sb->events = cpu_to_le64(bitmap->mddev->events);
499 if (bitmap->mddev->events < bitmap->events_cleared) {
500 /* rocking back to read-only */
501 bitmap->events_cleared = bitmap->mddev->events;
502 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
504 kunmap_atomic(sb, KM_USER0);
505 write_page(bitmap, bitmap->sb_page, 1);
508 /* print out the bitmap file superblock */
509 void bitmap_print_sb(struct bitmap *bitmap)
513 if (!bitmap || !bitmap->sb_page)
515 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
516 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
517 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
518 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
519 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
520 *(__u32 *)(sb->uuid+0),
521 *(__u32 *)(sb->uuid+4),
522 *(__u32 *)(sb->uuid+8),
523 *(__u32 *)(sb->uuid+12));
524 printk(KERN_DEBUG " events: %llu\n",
525 (unsigned long long) le64_to_cpu(sb->events));
526 printk(KERN_DEBUG "events cleared: %llu\n",
527 (unsigned long long) le64_to_cpu(sb->events_cleared));
528 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
529 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
530 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
531 printk(KERN_DEBUG " sync size: %llu KB\n",
532 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
533 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
534 kunmap_atomic(sb, KM_USER0);
537 /* read the superblock from the bitmap file and initialize some bitmap fields */
538 static int bitmap_read_sb(struct bitmap *bitmap)
542 unsigned long chunksize, daemon_sleep, write_behind;
543 unsigned long long events;
546 /* page 0 is the superblock, read it... */
548 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
549 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
551 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
553 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset,
555 0, sizeof(bitmap_super_t));
557 if (IS_ERR(bitmap->sb_page)) {
558 err = PTR_ERR(bitmap->sb_page);
559 bitmap->sb_page = NULL;
563 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
565 chunksize = le32_to_cpu(sb->chunksize);
566 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
567 write_behind = le32_to_cpu(sb->write_behind);
569 /* verify that the bitmap-specific fields are valid */
570 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
571 reason = "bad magic";
572 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
573 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
574 reason = "unrecognized superblock version";
575 else if (chunksize < 512)
576 reason = "bitmap chunksize too small";
577 else if ((1 << ffz(~chunksize)) != chunksize)
578 reason = "bitmap chunksize not a power of 2";
579 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
580 reason = "daemon sleep period out of range";
581 else if (write_behind > COUNTER_MAX)
582 reason = "write-behind limit out of range (0 - 16383)";
584 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
585 bmname(bitmap), reason);
589 /* keep the array size field of the bitmap superblock up to date */
590 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
592 if (!bitmap->mddev->persistent)
596 * if we have a persistent array superblock, compare the
597 * bitmap's UUID and event counter to the mddev's
599 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
600 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
604 events = le64_to_cpu(sb->events);
605 if (events < bitmap->mddev->events) {
606 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
607 "-- forcing full recovery\n", bmname(bitmap), events,
608 (unsigned long long) bitmap->mddev->events);
609 sb->state |= cpu_to_le32(BITMAP_STALE);
612 /* assign fields using values from superblock */
613 bitmap->chunksize = chunksize;
614 bitmap->daemon_sleep = daemon_sleep;
615 bitmap->daemon_lastrun = jiffies;
616 bitmap->max_write_behind = write_behind;
617 bitmap->flags |= le32_to_cpu(sb->state);
618 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
619 bitmap->flags |= BITMAP_HOSTENDIAN;
620 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
621 if (sb->state & cpu_to_le32(BITMAP_STALE))
622 bitmap->events_cleared = bitmap->mddev->events;
625 kunmap_atomic(sb, KM_USER0);
627 bitmap_print_sb(bitmap);
631 enum bitmap_mask_op {
636 /* record the state of the bitmap in the superblock. Return the old value */
637 static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
638 enum bitmap_mask_op op)
644 spin_lock_irqsave(&bitmap->lock, flags);
645 if (!bitmap->sb_page) { /* can't set the state */
646 spin_unlock_irqrestore(&bitmap->lock, flags);
649 spin_unlock_irqrestore(&bitmap->lock, flags);
650 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
651 old = le32_to_cpu(sb->state) & bits;
653 case MASK_SET: sb->state |= cpu_to_le32(bits);
655 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
659 kunmap_atomic(sb, KM_USER0);
664 * general bitmap file operations
667 /* calculate the index of the page that contains this bit */
668 static inline unsigned long file_page_index(unsigned long chunk)
670 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
673 /* calculate the (bit) offset of this bit within a page */
674 static inline unsigned long file_page_offset(unsigned long chunk)
676 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
680 * return a pointer to the page in the filemap that contains the given bit
682 * this lookup is complicated by the fact that the bitmap sb might be exactly
683 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
686 static inline struct page *filemap_get_page(struct bitmap *bitmap,
689 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
690 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
694 static void bitmap_file_unmap(struct bitmap *bitmap)
696 struct page **map, *sb_page;
701 spin_lock_irqsave(&bitmap->lock, flags);
702 map = bitmap->filemap;
703 bitmap->filemap = NULL;
704 attr = bitmap->filemap_attr;
705 bitmap->filemap_attr = NULL;
706 pages = bitmap->file_pages;
707 bitmap->file_pages = 0;
708 sb_page = bitmap->sb_page;
709 bitmap->sb_page = NULL;
710 spin_unlock_irqrestore(&bitmap->lock, flags);
713 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
714 free_buffers(map[pages]);
719 free_buffers(sb_page);
722 static void bitmap_file_put(struct bitmap *bitmap)
727 spin_lock_irqsave(&bitmap->lock, flags);
730 spin_unlock_irqrestore(&bitmap->lock, flags);
733 wait_event(bitmap->write_wait,
734 atomic_read(&bitmap->pending_writes)==0);
735 bitmap_file_unmap(bitmap);
738 struct inode *inode = file->f_path.dentry->d_inode;
739 invalidate_mapping_pages(inode->i_mapping, 0, -1);
746 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
747 * then it is no longer reliable, so we stop using it and we mark the file
748 * as failed in the superblock
750 static void bitmap_file_kick(struct bitmap *bitmap)
752 char *path, *ptr = NULL;
754 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
755 bitmap_update_sb(bitmap);
758 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
760 ptr = d_path(&bitmap->file->f_path, path,
765 "%s: kicking failed bitmap file %s from array!\n",
766 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
771 "%s: disabling internal bitmap due to errors\n",
775 bitmap_file_put(bitmap);
780 enum bitmap_page_attr {
781 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
782 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
783 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
786 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
787 enum bitmap_page_attr attr)
789 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
792 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
793 enum bitmap_page_attr attr)
795 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
798 static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
799 enum bitmap_page_attr attr)
801 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
805 * bitmap_file_set_bit -- called before performing a write to the md device
806 * to set (and eventually sync) a particular bit in the bitmap file
808 * we set the bit immediately, then we record the page number so that
809 * when an unplug occurs, we can flush the dirty pages out to disk
811 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
816 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
818 if (!bitmap->filemap) {
822 page = filemap_get_page(bitmap, chunk);
824 bit = file_page_offset(chunk);
827 kaddr = kmap_atomic(page, KM_USER0);
828 if (bitmap->flags & BITMAP_HOSTENDIAN)
831 ext2_set_bit(bit, kaddr);
832 kunmap_atomic(kaddr, KM_USER0);
833 PRINTK("set file bit %lu page %lu\n", bit, page->index);
835 /* record page number so it gets flushed to disk when unplug occurs */
836 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
840 /* this gets called when the md device is ready to unplug its underlying
841 * (slave) device queues -- before we let any writes go down, we need to
842 * sync the dirty pages of the bitmap file to disk */
843 void bitmap_unplug(struct bitmap *bitmap)
845 unsigned long i, flags;
846 int dirty, need_write;
853 /* look at each page to see if there are any set bits that need to be
854 * flushed out to disk */
855 for (i = 0; i < bitmap->file_pages; i++) {
856 spin_lock_irqsave(&bitmap->lock, flags);
857 if (!bitmap->filemap) {
858 spin_unlock_irqrestore(&bitmap->lock, flags);
861 page = bitmap->filemap[i];
862 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
863 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
864 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
865 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
868 spin_unlock_irqrestore(&bitmap->lock, flags);
870 if (dirty | need_write)
871 write_page(bitmap, page, 0);
873 if (wait) { /* if any writes were performed, we need to wait on them */
875 wait_event(bitmap->write_wait,
876 atomic_read(&bitmap->pending_writes)==0);
878 md_super_wait(bitmap->mddev);
880 if (bitmap->flags & BITMAP_WRITE_ERROR)
881 bitmap_file_kick(bitmap);
884 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
885 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
886 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
887 * memory mapping of the bitmap file
889 * if there's no bitmap file, or if the bitmap file had been
890 * previously kicked from the array, we mark all the bits as
891 * 1's in order to cause a full resync.
893 * We ignore all bits for sectors that end earlier than 'start'.
894 * This is used when reading an out-of-date bitmap...
896 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
898 unsigned long i, chunks, index, oldindex, bit;
899 struct page *page = NULL, *oldpage = NULL;
900 unsigned long num_pages, bit_cnt = 0;
902 unsigned long bytes, offset;
907 chunks = bitmap->chunks;
910 BUG_ON(!file && !bitmap->offset);
912 #ifdef INJECT_FAULTS_3
915 outofdate = bitmap->flags & BITMAP_STALE;
918 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
919 "recovery\n", bmname(bitmap));
921 bytes = (chunks + 7) / 8;
923 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
925 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
926 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
928 (unsigned long) i_size_read(file->f_mapping->host),
929 bytes + sizeof(bitmap_super_t));
935 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
936 if (!bitmap->filemap)
939 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
940 bitmap->filemap_attr = kzalloc(
941 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
943 if (!bitmap->filemap_attr)
948 for (i = 0; i < chunks; i++) {
950 index = file_page_index(i);
951 bit = file_page_offset(i);
952 if (index != oldindex) { /* this is a new page, read it in */
954 /* unmap the old page, we're done with it */
955 if (index == num_pages-1)
956 count = bytes + sizeof(bitmap_super_t)
962 * if we're here then the superblock page
963 * contains some bits (PAGE_SIZE != sizeof sb)
964 * we've already read it in, so just use it
966 page = bitmap->sb_page;
967 offset = sizeof(bitmap_super_t);
969 read_sb_page(bitmap->mddev,
974 page = read_page(file, index, bitmap, count);
977 page = read_sb_page(bitmap->mddev, bitmap->offset,
982 if (IS_ERR(page)) { /* read error */
990 bitmap->filemap[bitmap->file_pages++] = page;
991 bitmap->last_page_size = count;
995 * if bitmap is out of date, dirty the
996 * whole page and write it out
998 paddr = kmap_atomic(page, KM_USER0);
999 memset(paddr + offset, 0xff,
1000 PAGE_SIZE - offset);
1001 kunmap_atomic(paddr, KM_USER0);
1002 write_page(bitmap, page, 1);
1005 if (bitmap->flags & BITMAP_WRITE_ERROR)
1009 paddr = kmap_atomic(page, KM_USER0);
1010 if (bitmap->flags & BITMAP_HOSTENDIAN)
1011 b = test_bit(bit, paddr);
1013 b = ext2_test_bit(bit, paddr);
1014 kunmap_atomic(paddr, KM_USER0);
1016 /* if the disk bit is set, set the memory bit */
1017 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1019 bitmap_set_memory_bits(bitmap,
1020 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1023 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1027 /* everything went OK */
1029 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1031 if (bit_cnt) { /* Kick recovery if any bits were set */
1032 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1033 md_wakeup_thread(bitmap->mddev->thread);
1036 printk(KERN_INFO "%s: bitmap initialized from disk: "
1037 "read %lu/%lu pages, set %lu bits\n",
1038 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
1043 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1044 bmname(bitmap), ret);
1048 void bitmap_write_all(struct bitmap *bitmap)
1050 /* We don't actually write all bitmap blocks here,
1051 * just flag them as needing to be written
1055 for (i=0; i < bitmap->file_pages; i++)
1056 set_page_attr(bitmap, bitmap->filemap[i],
1057 BITMAP_PAGE_NEEDWRITE);
1061 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1063 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1064 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1065 bitmap->bp[page].count += inc;
1067 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1068 (unsigned long long)offset, inc, bitmap->bp[page].count);
1070 bitmap_checkfree(bitmap, page);
1072 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1073 sector_t offset, int *blocks,
1077 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1081 void bitmap_daemon_work(mddev_t *mddev)
1083 struct bitmap *bitmap;
1085 unsigned long flags;
1086 struct page *page = NULL, *lastpage = NULL;
1090 /* Use a mutex to guard daemon_work against
1093 mutex_lock(&mddev->bitmap_mutex);
1094 bitmap = mddev->bitmap;
1095 if (bitmap == NULL) {
1096 mutex_unlock(&mddev->bitmap_mutex);
1099 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1102 bitmap->daemon_lastrun = jiffies;
1103 if (bitmap->allclean) {
1104 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1107 bitmap->allclean = 1;
1109 spin_lock_irqsave(&bitmap->lock, flags);
1110 for (j = 0; j < bitmap->chunks; j++) {
1111 bitmap_counter_t *bmc;
1112 if (!bitmap->filemap)
1113 /* error or shutdown */
1116 page = filemap_get_page(bitmap, j);
1118 if (page != lastpage) {
1119 /* skip this page unless it's marked as needing cleaning */
1120 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1121 int need_write = test_page_attr(bitmap, page,
1122 BITMAP_PAGE_NEEDWRITE);
1124 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1126 spin_unlock_irqrestore(&bitmap->lock, flags);
1128 write_page(bitmap, page, 0);
1129 bitmap->allclean = 0;
1131 spin_lock_irqsave(&bitmap->lock, flags);
1132 j |= (PAGE_BITS - 1);
1136 /* grab the new page, sync and release the old */
1137 if (lastpage != NULL) {
1138 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1139 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1140 spin_unlock_irqrestore(&bitmap->lock, flags);
1141 write_page(bitmap, lastpage, 0);
1143 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1144 spin_unlock_irqrestore(&bitmap->lock, flags);
1147 spin_unlock_irqrestore(&bitmap->lock, flags);
1150 /* We are possibly going to clear some bits, so make
1151 * sure that events_cleared is up-to-date.
1153 if (bitmap->need_sync) {
1155 bitmap->need_sync = 0;
1156 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1157 sb->events_cleared =
1158 cpu_to_le64(bitmap->events_cleared);
1159 kunmap_atomic(sb, KM_USER0);
1160 write_page(bitmap, bitmap->sb_page, 1);
1162 spin_lock_irqsave(&bitmap->lock, flags);
1163 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1165 bmc = bitmap_get_counter(bitmap,
1166 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1170 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1173 bitmap->allclean = 0;
1176 *bmc=1; /* maybe clear the bit next time */
1177 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1178 } else if (*bmc == 1) {
1179 /* we can clear the bit */
1181 bitmap_count_page(bitmap,
1182 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1186 paddr = kmap_atomic(page, KM_USER0);
1187 if (bitmap->flags & BITMAP_HOSTENDIAN)
1188 clear_bit(file_page_offset(j), paddr);
1190 ext2_clear_bit(file_page_offset(j), paddr);
1191 kunmap_atomic(paddr, KM_USER0);
1194 j |= PAGE_COUNTER_MASK;
1196 spin_unlock_irqrestore(&bitmap->lock, flags);
1198 /* now sync the final page */
1199 if (lastpage != NULL) {
1200 spin_lock_irqsave(&bitmap->lock, flags);
1201 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1202 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1203 spin_unlock_irqrestore(&bitmap->lock, flags);
1204 write_page(bitmap, lastpage, 0);
1206 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1207 spin_unlock_irqrestore(&bitmap->lock, flags);
1212 if (bitmap->allclean == 0)
1213 bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1214 mutex_unlock(&mddev->bitmap_mutex);
1217 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1218 sector_t offset, int *blocks,
1220 __releases(bitmap->lock)
1221 __acquires(bitmap->lock)
1223 /* If 'create', we might release the lock and reclaim it.
1224 * The lock must have been taken with interrupts enabled.
1225 * If !create, we don't release the lock.
1227 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1228 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1229 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1232 if (bitmap_checkpage(bitmap, page, create) < 0) {
1233 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1234 *blocks = csize - (offset & (csize- 1));
1237 /* now locked ... */
1239 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1240 /* should we use the first or second counter field
1241 * of the hijacked pointer? */
1242 int hi = (pageoff > PAGE_COUNTER_MASK);
1243 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1244 PAGE_COUNTER_SHIFT - 1);
1245 *blocks = csize - (offset & (csize- 1));
1246 return &((bitmap_counter_t *)
1247 &bitmap->bp[page].map)[hi];
1248 } else { /* page is allocated */
1249 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1250 *blocks = csize - (offset & (csize- 1));
1251 return (bitmap_counter_t *)
1252 &(bitmap->bp[page].map[pageoff]);
1256 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1258 if (!bitmap) return 0;
1261 atomic_inc(&bitmap->behind_writes);
1262 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1263 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1268 bitmap_counter_t *bmc;
1270 spin_lock_irq(&bitmap->lock);
1271 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1273 spin_unlock_irq(&bitmap->lock);
1277 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1278 DEFINE_WAIT(__wait);
1279 /* note that it is safe to do the prepare_to_wait
1280 * after the test as long as we do it before dropping
1283 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1284 TASK_UNINTERRUPTIBLE);
1285 spin_unlock_irq(&bitmap->lock);
1286 blk_unplug(bitmap->mddev->queue);
1288 finish_wait(&bitmap->overflow_wait, &__wait);
1294 bitmap_file_set_bit(bitmap, offset);
1295 bitmap_count_page(bitmap,offset, 1);
1296 blk_plug_device_unlocked(bitmap->mddev->queue);
1304 spin_unlock_irq(&bitmap->lock);
1307 if (sectors > blocks)
1311 bitmap->allclean = 0;
1315 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1316 int success, int behind)
1318 if (!bitmap) return;
1320 if (atomic_dec_and_test(&bitmap->behind_writes))
1321 wake_up(&bitmap->behind_wait);
1322 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1323 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1325 if (bitmap->mddev->degraded)
1326 /* Never clear bits or update events_cleared when degraded */
1331 unsigned long flags;
1332 bitmap_counter_t *bmc;
1334 spin_lock_irqsave(&bitmap->lock, flags);
1335 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1337 spin_unlock_irqrestore(&bitmap->lock, flags);
1342 bitmap->events_cleared < bitmap->mddev->events) {
1343 bitmap->events_cleared = bitmap->mddev->events;
1344 bitmap->need_sync = 1;
1347 if (!success && ! (*bmc & NEEDED_MASK))
1348 *bmc |= NEEDED_MASK;
1350 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1351 wake_up(&bitmap->overflow_wait);
1355 set_page_attr(bitmap,
1356 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1359 spin_unlock_irqrestore(&bitmap->lock, flags);
1361 if (sectors > blocks)
1367 static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1370 bitmap_counter_t *bmc;
1372 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1374 return 1; /* always resync if no bitmap */
1376 spin_lock_irq(&bitmap->lock);
1377 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1383 else if (NEEDED(*bmc)) {
1385 if (!degraded) { /* don't set/clear bits if degraded */
1386 *bmc |= RESYNC_MASK;
1387 *bmc &= ~NEEDED_MASK;
1391 spin_unlock_irq(&bitmap->lock);
1392 bitmap->allclean = 0;
1396 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1399 /* bitmap_start_sync must always report on multiples of whole
1400 * pages, otherwise resync (which is very PAGE_SIZE based) will
1402 * So call __bitmap_start_sync repeatedly (if needed) until
1403 * At least PAGE_SIZE>>9 blocks are covered.
1404 * Return the 'or' of the result.
1410 while (*blocks < (PAGE_SIZE>>9)) {
1411 rv |= __bitmap_start_sync(bitmap, offset,
1412 &blocks1, degraded);
1419 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1421 bitmap_counter_t *bmc;
1422 unsigned long flags;
1424 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1425 */ if (bitmap == NULL) {
1429 spin_lock_irqsave(&bitmap->lock, flags);
1430 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1435 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1438 *bmc &= ~RESYNC_MASK;
1440 if (!NEEDED(*bmc) && aborted)
1441 *bmc |= NEEDED_MASK;
1444 set_page_attr(bitmap,
1445 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1451 spin_unlock_irqrestore(&bitmap->lock, flags);
1452 bitmap->allclean = 0;
1455 void bitmap_close_sync(struct bitmap *bitmap)
1457 /* Sync has finished, and any bitmap chunks that weren't synced
1458 * properly have been aborted. It remains to us to clear the
1459 * RESYNC bit wherever it is still on
1461 sector_t sector = 0;
1465 while (sector < bitmap->mddev->resync_max_sectors) {
1466 bitmap_end_sync(bitmap, sector, &blocks, 0);
1471 void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1479 bitmap->last_end_sync = jiffies;
1482 if (time_before(jiffies, (bitmap->last_end_sync
1483 + bitmap->daemon_sleep * HZ)))
1485 wait_event(bitmap->mddev->recovery_wait,
1486 atomic_read(&bitmap->mddev->recovery_active) == 0);
1488 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1489 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
1490 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1492 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1493 bitmap_end_sync(bitmap, s, &blocks, 0);
1496 bitmap->last_end_sync = jiffies;
1497 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
1500 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1502 /* For each chunk covered by any of these sectors, set the
1503 * counter to 1 and set resync_needed. They should all
1504 * be 0 at this point
1508 bitmap_counter_t *bmc;
1509 spin_lock_irq(&bitmap->lock);
1510 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1512 spin_unlock_irq(&bitmap->lock);
1517 *bmc = 1 | (needed?NEEDED_MASK:0);
1518 bitmap_count_page(bitmap, offset, 1);
1519 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1520 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1522 spin_unlock_irq(&bitmap->lock);
1523 bitmap->allclean = 0;
1526 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1527 void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1529 unsigned long chunk;
1531 for (chunk = s; chunk <= e; chunk++) {
1532 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
1533 bitmap_set_memory_bits(bitmap, sec, 1);
1534 bitmap_file_set_bit(bitmap, sec);
1539 * flush out any pending updates
1541 void bitmap_flush(mddev_t *mddev)
1543 struct bitmap *bitmap = mddev->bitmap;
1546 if (!bitmap) /* there was no bitmap */
1549 /* run the daemon_work three time to ensure everything is flushed
1552 sleep = bitmap->daemon_sleep;
1553 bitmap->daemon_sleep = 0;
1554 bitmap_daemon_work(mddev);
1555 bitmap_daemon_work(mddev);
1556 bitmap_daemon_work(mddev);
1557 bitmap->daemon_sleep = sleep;
1558 bitmap_update_sb(bitmap);
1562 * free memory that was allocated
1564 static void bitmap_free(struct bitmap *bitmap)
1566 unsigned long k, pages;
1567 struct bitmap_page *bp;
1569 if (!bitmap) /* there was no bitmap */
1572 /* release the bitmap file and kill the daemon */
1573 bitmap_file_put(bitmap);
1576 pages = bitmap->pages;
1578 /* free all allocated memory */
1580 if (bp) /* deallocate the page memory */
1581 for (k = 0; k < pages; k++)
1582 if (bp[k].map && !bp[k].hijacked)
1588 void bitmap_destroy(mddev_t *mddev)
1590 struct bitmap *bitmap = mddev->bitmap;
1592 if (!bitmap) /* there was no bitmap */
1595 mutex_lock(&mddev->bitmap_mutex);
1596 mddev->bitmap = NULL; /* disconnect from the md device */
1597 mutex_unlock(&mddev->bitmap_mutex);
1599 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1601 bitmap_free(bitmap);
1605 * initialize the bitmap structure
1606 * if this returns an error, bitmap_destroy must be called to do clean up
1608 int bitmap_create(mddev_t *mddev)
1610 struct bitmap *bitmap;
1611 sector_t blocks = mddev->resync_max_sectors;
1612 unsigned long chunks;
1613 unsigned long pages;
1614 struct file *file = mddev->bitmap_file;
1618 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
1620 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
1623 BUG_ON(file && mddev->bitmap_offset);
1625 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1629 spin_lock_init(&bitmap->lock);
1630 atomic_set(&bitmap->pending_writes, 0);
1631 init_waitqueue_head(&bitmap->write_wait);
1632 init_waitqueue_head(&bitmap->overflow_wait);
1633 init_waitqueue_head(&bitmap->behind_wait);
1635 bitmap->mddev = mddev;
1637 bitmap->file = file;
1638 bitmap->offset = mddev->bitmap_offset;
1641 /* As future accesses to this file will use bmap,
1642 * and bypass the page cache, we must sync the file
1645 vfs_fsync(file, file->f_dentry, 1);
1647 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1648 err = bitmap_read_sb(bitmap);
1652 bitmap->chunkshift = ffz(~bitmap->chunksize);
1654 /* now that chunksize and chunkshift are set, we can use these macros */
1655 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1656 CHUNK_BLOCK_SHIFT(bitmap);
1657 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1661 bitmap->chunks = chunks;
1662 bitmap->pages = pages;
1663 bitmap->missing_pages = pages;
1664 bitmap->counter_bits = COUNTER_BITS;
1666 bitmap->syncchunk = ~0UL;
1668 #ifdef INJECT_FATAL_FAULT_1
1671 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1677 /* now that we have some pages available, initialize the in-memory
1678 * bitmap from the on-disk bitmap */
1680 if (mddev->degraded == 0
1681 || bitmap->events_cleared == mddev->events)
1682 /* no need to keep dirty bits to optimise a re-add of a missing device */
1683 start = mddev->recovery_cp;
1684 err = bitmap_init_from_disk(bitmap, start);
1689 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1690 pages, bmname(bitmap));
1692 mddev->bitmap = bitmap;
1694 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1696 bitmap_update_sb(bitmap);
1698 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1701 bitmap_free(bitmap);
1705 /* the bitmap API -- for raid personalities */
1706 EXPORT_SYMBOL(bitmap_startwrite);
1707 EXPORT_SYMBOL(bitmap_endwrite);
1708 EXPORT_SYMBOL(bitmap_start_sync);
1709 EXPORT_SYMBOL(bitmap_end_sync);
1710 EXPORT_SYMBOL(bitmap_unplug);
1711 EXPORT_SYMBOL(bitmap_close_sync);
1712 EXPORT_SYMBOL(bitmap_cond_end_sync);