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 long offset = mddev->bitmap_info.offset;
291 if (page->index == bitmap->file_pages-1)
292 size = roundup(bitmap->last_page_size,
293 bdev_logical_block_size(rdev->bdev));
294 /* Just make sure we aren't corrupting data or
298 /* DATA BITMAP METADATA */
300 + (long)(page->index * (PAGE_SIZE/512))
302 /* bitmap runs in to metadata */
304 if (rdev->data_offset + mddev->dev_sectors
305 > rdev->sb_start + offset)
306 /* data runs in to bitmap */
308 } else if (rdev->sb_start < rdev->data_offset) {
309 /* METADATA BITMAP DATA */
312 + page->index*(PAGE_SIZE/512) + size/512
314 /* bitmap runs in to data */
317 /* DATA METADATA BITMAP - no problems */
319 md_super_write(mddev, rdev,
320 rdev->sb_start + offset
321 + page->index * (PAGE_SIZE/512),
327 md_super_wait(mddev);
334 static void bitmap_file_kick(struct bitmap *bitmap);
336 * write out a page to a file
338 static void write_page(struct bitmap *bitmap, struct page *page, int wait)
340 struct buffer_head *bh;
342 if (bitmap->file == NULL) {
343 switch (write_sb_page(bitmap, page, wait)) {
345 bitmap->flags |= BITMAP_WRITE_ERROR;
349 bh = page_buffers(page);
351 while (bh && bh->b_blocknr) {
352 atomic_inc(&bitmap->pending_writes);
353 set_buffer_locked(bh);
354 set_buffer_mapped(bh);
355 submit_bh(WRITE, bh);
356 bh = bh->b_this_page;
360 wait_event(bitmap->write_wait,
361 atomic_read(&bitmap->pending_writes)==0);
364 if (bitmap->flags & BITMAP_WRITE_ERROR)
365 bitmap_file_kick(bitmap);
368 static void end_bitmap_write(struct buffer_head *bh, int uptodate)
370 struct bitmap *bitmap = bh->b_private;
374 spin_lock_irqsave(&bitmap->lock, flags);
375 bitmap->flags |= BITMAP_WRITE_ERROR;
376 spin_unlock_irqrestore(&bitmap->lock, flags);
378 if (atomic_dec_and_test(&bitmap->pending_writes))
379 wake_up(&bitmap->write_wait);
382 /* copied from buffer.c */
384 __clear_page_buffers(struct page *page)
386 ClearPagePrivate(page);
387 set_page_private(page, 0);
388 page_cache_release(page);
390 static void free_buffers(struct page *page)
392 struct buffer_head *bh = page_buffers(page);
395 struct buffer_head *next = bh->b_this_page;
396 free_buffer_head(bh);
399 __clear_page_buffers(page);
403 /* read a page from a file.
404 * We both read the page, and attach buffers to the page to record the
405 * address of each block (using bmap). These addresses will be used
406 * to write the block later, completely bypassing the filesystem.
407 * This usage is similar to how swap files are handled, and allows us
408 * to write to a file with no concerns of memory allocation failing.
410 static struct page *read_page(struct file *file, unsigned long index,
411 struct bitmap *bitmap,
414 struct page *page = NULL;
415 struct inode *inode = file->f_path.dentry->d_inode;
416 struct buffer_head *bh;
419 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
420 (unsigned long long)index << PAGE_SHIFT);
422 page = alloc_page(GFP_KERNEL);
424 page = ERR_PTR(-ENOMEM);
428 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
431 page = ERR_PTR(-ENOMEM);
434 attach_page_buffers(page, bh);
435 block = index << (PAGE_SHIFT - inode->i_blkbits);
440 bh->b_blocknr = bmap(inode, block);
441 if (bh->b_blocknr == 0) {
442 /* Cannot use this file! */
444 page = ERR_PTR(-EINVAL);
447 bh->b_bdev = inode->i_sb->s_bdev;
448 if (count < (1<<inode->i_blkbits))
451 count -= (1<<inode->i_blkbits);
453 bh->b_end_io = end_bitmap_write;
454 bh->b_private = bitmap;
455 atomic_inc(&bitmap->pending_writes);
456 set_buffer_locked(bh);
457 set_buffer_mapped(bh);
461 bh = bh->b_this_page;
465 wait_event(bitmap->write_wait,
466 atomic_read(&bitmap->pending_writes)==0);
467 if (bitmap->flags & BITMAP_WRITE_ERROR) {
469 page = ERR_PTR(-EIO);
473 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
475 (unsigned long long)index << PAGE_SHIFT,
481 * bitmap file superblock operations
484 /* update the event counter and sync the superblock to disk */
485 void bitmap_update_sb(struct bitmap *bitmap)
490 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
492 spin_lock_irqsave(&bitmap->lock, flags);
493 if (!bitmap->sb_page) { /* no superblock */
494 spin_unlock_irqrestore(&bitmap->lock, flags);
497 spin_unlock_irqrestore(&bitmap->lock, flags);
498 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
499 sb->events = cpu_to_le64(bitmap->mddev->events);
500 if (bitmap->mddev->events < bitmap->events_cleared) {
501 /* rocking back to read-only */
502 bitmap->events_cleared = bitmap->mddev->events;
503 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
505 kunmap_atomic(sb, KM_USER0);
506 write_page(bitmap, bitmap->sb_page, 1);
509 /* print out the bitmap file superblock */
510 void bitmap_print_sb(struct bitmap *bitmap)
514 if (!bitmap || !bitmap->sb_page)
516 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
517 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
518 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
519 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
520 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
521 *(__u32 *)(sb->uuid+0),
522 *(__u32 *)(sb->uuid+4),
523 *(__u32 *)(sb->uuid+8),
524 *(__u32 *)(sb->uuid+12));
525 printk(KERN_DEBUG " events: %llu\n",
526 (unsigned long long) le64_to_cpu(sb->events));
527 printk(KERN_DEBUG "events cleared: %llu\n",
528 (unsigned long long) le64_to_cpu(sb->events_cleared));
529 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
530 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
531 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
532 printk(KERN_DEBUG " sync size: %llu KB\n",
533 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
534 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
535 kunmap_atomic(sb, KM_USER0);
538 /* read the superblock from the bitmap file and initialize some bitmap fields */
539 static int bitmap_read_sb(struct bitmap *bitmap)
543 unsigned long chunksize, daemon_sleep, write_behind;
544 unsigned long long events;
547 /* page 0 is the superblock, read it... */
549 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
550 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
552 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
554 bitmap->sb_page = read_sb_page(bitmap->mddev,
555 bitmap->mddev->bitmap_info.offset,
557 0, sizeof(bitmap_super_t));
559 if (IS_ERR(bitmap->sb_page)) {
560 err = PTR_ERR(bitmap->sb_page);
561 bitmap->sb_page = NULL;
565 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
567 chunksize = le32_to_cpu(sb->chunksize);
568 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
569 write_behind = le32_to_cpu(sb->write_behind);
571 /* verify that the bitmap-specific fields are valid */
572 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
573 reason = "bad magic";
574 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
575 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
576 reason = "unrecognized superblock version";
577 else if (chunksize < 512)
578 reason = "bitmap chunksize too small";
579 else if ((1 << ffz(~chunksize)) != chunksize)
580 reason = "bitmap chunksize not a power of 2";
581 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
582 reason = "daemon sleep period out of range";
583 else if (write_behind > COUNTER_MAX)
584 reason = "write-behind limit out of range (0 - 16383)";
586 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
587 bmname(bitmap), reason);
591 /* keep the array size field of the bitmap superblock up to date */
592 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
594 if (!bitmap->mddev->persistent)
598 * if we have a persistent array superblock, compare the
599 * bitmap's UUID and event counter to the mddev's
601 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
602 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
606 events = le64_to_cpu(sb->events);
607 if (events < bitmap->mddev->events) {
608 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
609 "-- forcing full recovery\n", bmname(bitmap), events,
610 (unsigned long long) bitmap->mddev->events);
611 sb->state |= cpu_to_le32(BITMAP_STALE);
614 /* assign fields using values from superblock */
615 bitmap->mddev->bitmap_info.chunksize = chunksize;
616 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
617 bitmap->daemon_lastrun = jiffies;
618 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
619 bitmap->flags |= le32_to_cpu(sb->state);
620 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
621 bitmap->flags |= BITMAP_HOSTENDIAN;
622 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
623 if (sb->state & cpu_to_le32(BITMAP_STALE))
624 bitmap->events_cleared = bitmap->mddev->events;
627 kunmap_atomic(sb, KM_USER0);
629 bitmap_print_sb(bitmap);
633 enum bitmap_mask_op {
638 /* record the state of the bitmap in the superblock. Return the old value */
639 static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
640 enum bitmap_mask_op op)
646 spin_lock_irqsave(&bitmap->lock, flags);
647 if (!bitmap->sb_page) { /* can't set the state */
648 spin_unlock_irqrestore(&bitmap->lock, flags);
651 spin_unlock_irqrestore(&bitmap->lock, flags);
652 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
653 old = le32_to_cpu(sb->state) & bits;
655 case MASK_SET: sb->state |= cpu_to_le32(bits);
657 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
661 kunmap_atomic(sb, KM_USER0);
666 * general bitmap file operations
669 /* calculate the index of the page that contains this bit */
670 static inline unsigned long file_page_index(unsigned long chunk)
672 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
675 /* calculate the (bit) offset of this bit within a page */
676 static inline unsigned long file_page_offset(unsigned long chunk)
678 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
682 * return a pointer to the page in the filemap that contains the given bit
684 * this lookup is complicated by the fact that the bitmap sb might be exactly
685 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
688 static inline struct page *filemap_get_page(struct bitmap *bitmap,
691 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
692 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
696 static void bitmap_file_unmap(struct bitmap *bitmap)
698 struct page **map, *sb_page;
703 spin_lock_irqsave(&bitmap->lock, flags);
704 map = bitmap->filemap;
705 bitmap->filemap = NULL;
706 attr = bitmap->filemap_attr;
707 bitmap->filemap_attr = NULL;
708 pages = bitmap->file_pages;
709 bitmap->file_pages = 0;
710 sb_page = bitmap->sb_page;
711 bitmap->sb_page = NULL;
712 spin_unlock_irqrestore(&bitmap->lock, flags);
715 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
716 free_buffers(map[pages]);
721 free_buffers(sb_page);
724 static void bitmap_file_put(struct bitmap *bitmap)
729 spin_lock_irqsave(&bitmap->lock, flags);
732 spin_unlock_irqrestore(&bitmap->lock, flags);
735 wait_event(bitmap->write_wait,
736 atomic_read(&bitmap->pending_writes)==0);
737 bitmap_file_unmap(bitmap);
740 struct inode *inode = file->f_path.dentry->d_inode;
741 invalidate_mapping_pages(inode->i_mapping, 0, -1);
748 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
749 * then it is no longer reliable, so we stop using it and we mark the file
750 * as failed in the superblock
752 static void bitmap_file_kick(struct bitmap *bitmap)
754 char *path, *ptr = NULL;
756 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
757 bitmap_update_sb(bitmap);
760 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
762 ptr = d_path(&bitmap->file->f_path, path,
767 "%s: kicking failed bitmap file %s from array!\n",
768 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
773 "%s: disabling internal bitmap due to errors\n",
777 bitmap_file_put(bitmap);
782 enum bitmap_page_attr {
783 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
784 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
785 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
788 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
789 enum bitmap_page_attr attr)
791 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
794 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
795 enum bitmap_page_attr attr)
797 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
800 static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
801 enum bitmap_page_attr attr)
803 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
807 * bitmap_file_set_bit -- called before performing a write to the md device
808 * to set (and eventually sync) a particular bit in the bitmap file
810 * we set the bit immediately, then we record the page number so that
811 * when an unplug occurs, we can flush the dirty pages out to disk
813 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
818 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
820 if (!bitmap->filemap) {
824 page = filemap_get_page(bitmap, chunk);
826 bit = file_page_offset(chunk);
829 kaddr = kmap_atomic(page, KM_USER0);
830 if (bitmap->flags & BITMAP_HOSTENDIAN)
833 ext2_set_bit(bit, kaddr);
834 kunmap_atomic(kaddr, KM_USER0);
835 PRINTK("set file bit %lu page %lu\n", bit, page->index);
837 /* record page number so it gets flushed to disk when unplug occurs */
838 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
842 /* this gets called when the md device is ready to unplug its underlying
843 * (slave) device queues -- before we let any writes go down, we need to
844 * sync the dirty pages of the bitmap file to disk */
845 void bitmap_unplug(struct bitmap *bitmap)
847 unsigned long i, flags;
848 int dirty, need_write;
855 /* look at each page to see if there are any set bits that need to be
856 * flushed out to disk */
857 for (i = 0; i < bitmap->file_pages; i++) {
858 spin_lock_irqsave(&bitmap->lock, flags);
859 if (!bitmap->filemap) {
860 spin_unlock_irqrestore(&bitmap->lock, flags);
863 page = bitmap->filemap[i];
864 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
865 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
866 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
867 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
870 spin_unlock_irqrestore(&bitmap->lock, flags);
872 if (dirty | need_write)
873 write_page(bitmap, page, 0);
875 if (wait) { /* if any writes were performed, we need to wait on them */
877 wait_event(bitmap->write_wait,
878 atomic_read(&bitmap->pending_writes)==0);
880 md_super_wait(bitmap->mddev);
882 if (bitmap->flags & BITMAP_WRITE_ERROR)
883 bitmap_file_kick(bitmap);
886 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
887 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
888 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
889 * memory mapping of the bitmap file
891 * if there's no bitmap file, or if the bitmap file had been
892 * previously kicked from the array, we mark all the bits as
893 * 1's in order to cause a full resync.
895 * We ignore all bits for sectors that end earlier than 'start'.
896 * This is used when reading an out-of-date bitmap...
898 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
900 unsigned long i, chunks, index, oldindex, bit;
901 struct page *page = NULL, *oldpage = NULL;
902 unsigned long num_pages, bit_cnt = 0;
904 unsigned long bytes, offset;
909 chunks = bitmap->chunks;
912 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
914 #ifdef INJECT_FAULTS_3
917 outofdate = bitmap->flags & BITMAP_STALE;
920 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
921 "recovery\n", bmname(bitmap));
923 bytes = (chunks + 7) / 8;
925 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
927 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
928 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
930 (unsigned long) i_size_read(file->f_mapping->host),
931 bytes + sizeof(bitmap_super_t));
937 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
938 if (!bitmap->filemap)
941 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
942 bitmap->filemap_attr = kzalloc(
943 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
945 if (!bitmap->filemap_attr)
950 for (i = 0; i < chunks; i++) {
952 index = file_page_index(i);
953 bit = file_page_offset(i);
954 if (index != oldindex) { /* this is a new page, read it in */
956 /* unmap the old page, we're done with it */
957 if (index == num_pages-1)
958 count = bytes + sizeof(bitmap_super_t)
964 * if we're here then the superblock page
965 * contains some bits (PAGE_SIZE != sizeof sb)
966 * we've already read it in, so just use it
968 page = bitmap->sb_page;
969 offset = sizeof(bitmap_super_t);
971 read_sb_page(bitmap->mddev,
972 bitmap->mddev->bitmap_info.offset,
976 page = read_page(file, index, bitmap, count);
979 page = read_sb_page(bitmap->mddev,
980 bitmap->mddev->bitmap_info.offset,
985 if (IS_ERR(page)) { /* read error */
993 bitmap->filemap[bitmap->file_pages++] = page;
994 bitmap->last_page_size = count;
998 * if bitmap is out of date, dirty the
999 * whole page and write it out
1001 paddr = kmap_atomic(page, KM_USER0);
1002 memset(paddr + offset, 0xff,
1003 PAGE_SIZE - offset);
1004 kunmap_atomic(paddr, KM_USER0);
1005 write_page(bitmap, page, 1);
1008 if (bitmap->flags & BITMAP_WRITE_ERROR)
1012 paddr = kmap_atomic(page, KM_USER0);
1013 if (bitmap->flags & BITMAP_HOSTENDIAN)
1014 b = test_bit(bit, paddr);
1016 b = ext2_test_bit(bit, paddr);
1017 kunmap_atomic(paddr, KM_USER0);
1019 /* if the disk bit is set, set the memory bit */
1020 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1022 bitmap_set_memory_bits(bitmap,
1023 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1026 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1030 /* everything went OK */
1032 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1034 if (bit_cnt) { /* Kick recovery if any bits were set */
1035 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1036 md_wakeup_thread(bitmap->mddev->thread);
1039 printk(KERN_INFO "%s: bitmap initialized from disk: "
1040 "read %lu/%lu pages, set %lu bits\n",
1041 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
1046 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1047 bmname(bitmap), ret);
1051 void bitmap_write_all(struct bitmap *bitmap)
1053 /* We don't actually write all bitmap blocks here,
1054 * just flag them as needing to be written
1058 for (i=0; i < bitmap->file_pages; i++)
1059 set_page_attr(bitmap, bitmap->filemap[i],
1060 BITMAP_PAGE_NEEDWRITE);
1064 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1066 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1067 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1068 bitmap->bp[page].count += inc;
1070 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1071 (unsigned long long)offset, inc, bitmap->bp[page].count);
1073 bitmap_checkfree(bitmap, page);
1075 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1076 sector_t offset, int *blocks,
1080 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1084 void bitmap_daemon_work(mddev_t *mddev)
1086 struct bitmap *bitmap;
1088 unsigned long flags;
1089 struct page *page = NULL, *lastpage = NULL;
1093 /* Use a mutex to guard daemon_work against
1096 mutex_lock(&mddev->bitmap_info.mutex);
1097 bitmap = mddev->bitmap;
1098 if (bitmap == NULL) {
1099 mutex_unlock(&mddev->bitmap_info.mutex);
1102 if (time_before(jiffies, bitmap->daemon_lastrun
1103 + bitmap->mddev->bitmap_info.daemon_sleep))
1106 bitmap->daemon_lastrun = jiffies;
1107 if (bitmap->allclean) {
1108 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1111 bitmap->allclean = 1;
1113 spin_lock_irqsave(&bitmap->lock, flags);
1114 for (j = 0; j < bitmap->chunks; j++) {
1115 bitmap_counter_t *bmc;
1116 if (!bitmap->filemap)
1117 /* error or shutdown */
1120 page = filemap_get_page(bitmap, j);
1122 if (page != lastpage) {
1123 /* skip this page unless it's marked as needing cleaning */
1124 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1125 int need_write = test_page_attr(bitmap, page,
1126 BITMAP_PAGE_NEEDWRITE);
1128 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1130 spin_unlock_irqrestore(&bitmap->lock, flags);
1132 write_page(bitmap, page, 0);
1133 bitmap->allclean = 0;
1135 spin_lock_irqsave(&bitmap->lock, flags);
1136 j |= (PAGE_BITS - 1);
1140 /* grab the new page, sync and release the old */
1141 if (lastpage != NULL) {
1142 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1143 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1144 spin_unlock_irqrestore(&bitmap->lock, flags);
1145 write_page(bitmap, lastpage, 0);
1147 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1148 spin_unlock_irqrestore(&bitmap->lock, flags);
1151 spin_unlock_irqrestore(&bitmap->lock, flags);
1154 /* We are possibly going to clear some bits, so make
1155 * sure that events_cleared is up-to-date.
1157 if (bitmap->need_sync) {
1159 bitmap->need_sync = 0;
1160 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1161 sb->events_cleared =
1162 cpu_to_le64(bitmap->events_cleared);
1163 kunmap_atomic(sb, KM_USER0);
1164 write_page(bitmap, bitmap->sb_page, 1);
1166 spin_lock_irqsave(&bitmap->lock, flags);
1167 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1169 bmc = bitmap_get_counter(bitmap,
1170 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1174 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1177 bitmap->allclean = 0;
1180 *bmc=1; /* maybe clear the bit next time */
1181 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1182 } else if (*bmc == 1) {
1183 /* we can clear the bit */
1185 bitmap_count_page(bitmap,
1186 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1190 paddr = kmap_atomic(page, KM_USER0);
1191 if (bitmap->flags & BITMAP_HOSTENDIAN)
1192 clear_bit(file_page_offset(j), paddr);
1194 ext2_clear_bit(file_page_offset(j), paddr);
1195 kunmap_atomic(paddr, KM_USER0);
1198 j |= PAGE_COUNTER_MASK;
1200 spin_unlock_irqrestore(&bitmap->lock, flags);
1202 /* now sync the final page */
1203 if (lastpage != NULL) {
1204 spin_lock_irqsave(&bitmap->lock, flags);
1205 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1206 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1207 spin_unlock_irqrestore(&bitmap->lock, flags);
1208 write_page(bitmap, lastpage, 0);
1210 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1211 spin_unlock_irqrestore(&bitmap->lock, flags);
1216 if (bitmap->allclean == 0)
1217 bitmap->mddev->thread->timeout =
1218 bitmap->mddev->bitmap_info.daemon_sleep;
1219 mutex_unlock(&mddev->bitmap_info.mutex);
1222 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1223 sector_t offset, int *blocks,
1225 __releases(bitmap->lock)
1226 __acquires(bitmap->lock)
1228 /* If 'create', we might release the lock and reclaim it.
1229 * The lock must have been taken with interrupts enabled.
1230 * If !create, we don't release the lock.
1232 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1233 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1234 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1237 if (bitmap_checkpage(bitmap, page, create) < 0) {
1238 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1239 *blocks = csize - (offset & (csize- 1));
1242 /* now locked ... */
1244 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1245 /* should we use the first or second counter field
1246 * of the hijacked pointer? */
1247 int hi = (pageoff > PAGE_COUNTER_MASK);
1248 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1249 PAGE_COUNTER_SHIFT - 1);
1250 *blocks = csize - (offset & (csize- 1));
1251 return &((bitmap_counter_t *)
1252 &bitmap->bp[page].map)[hi];
1253 } else { /* page is allocated */
1254 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1255 *blocks = csize - (offset & (csize- 1));
1256 return (bitmap_counter_t *)
1257 &(bitmap->bp[page].map[pageoff]);
1261 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1263 if (!bitmap) return 0;
1266 atomic_inc(&bitmap->behind_writes);
1267 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1268 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1273 bitmap_counter_t *bmc;
1275 spin_lock_irq(&bitmap->lock);
1276 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1278 spin_unlock_irq(&bitmap->lock);
1282 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1283 DEFINE_WAIT(__wait);
1284 /* note that it is safe to do the prepare_to_wait
1285 * after the test as long as we do it before dropping
1288 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1289 TASK_UNINTERRUPTIBLE);
1290 spin_unlock_irq(&bitmap->lock);
1291 blk_unplug(bitmap->mddev->queue);
1293 finish_wait(&bitmap->overflow_wait, &__wait);
1299 bitmap_file_set_bit(bitmap, offset);
1300 bitmap_count_page(bitmap,offset, 1);
1301 blk_plug_device_unlocked(bitmap->mddev->queue);
1309 spin_unlock_irq(&bitmap->lock);
1312 if (sectors > blocks)
1316 bitmap->allclean = 0;
1320 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1321 int success, int behind)
1323 if (!bitmap) return;
1325 atomic_dec(&bitmap->behind_writes);
1326 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1327 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1329 if (bitmap->mddev->degraded)
1330 /* Never clear bits or update events_cleared when degraded */
1335 unsigned long flags;
1336 bitmap_counter_t *bmc;
1338 spin_lock_irqsave(&bitmap->lock, flags);
1339 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1341 spin_unlock_irqrestore(&bitmap->lock, flags);
1346 bitmap->events_cleared < bitmap->mddev->events) {
1347 bitmap->events_cleared = bitmap->mddev->events;
1348 bitmap->need_sync = 1;
1351 if (!success && ! (*bmc & NEEDED_MASK))
1352 *bmc |= NEEDED_MASK;
1354 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1355 wake_up(&bitmap->overflow_wait);
1359 set_page_attr(bitmap,
1360 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1363 spin_unlock_irqrestore(&bitmap->lock, flags);
1365 if (sectors > blocks)
1371 static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1374 bitmap_counter_t *bmc;
1376 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1378 return 1; /* always resync if no bitmap */
1380 spin_lock_irq(&bitmap->lock);
1381 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1387 else if (NEEDED(*bmc)) {
1389 if (!degraded) { /* don't set/clear bits if degraded */
1390 *bmc |= RESYNC_MASK;
1391 *bmc &= ~NEEDED_MASK;
1395 spin_unlock_irq(&bitmap->lock);
1396 bitmap->allclean = 0;
1400 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1403 /* bitmap_start_sync must always report on multiples of whole
1404 * pages, otherwise resync (which is very PAGE_SIZE based) will
1406 * So call __bitmap_start_sync repeatedly (if needed) until
1407 * At least PAGE_SIZE>>9 blocks are covered.
1408 * Return the 'or' of the result.
1414 while (*blocks < (PAGE_SIZE>>9)) {
1415 rv |= __bitmap_start_sync(bitmap, offset,
1416 &blocks1, degraded);
1423 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1425 bitmap_counter_t *bmc;
1426 unsigned long flags;
1428 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1429 */ if (bitmap == NULL) {
1433 spin_lock_irqsave(&bitmap->lock, flags);
1434 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1439 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1442 *bmc &= ~RESYNC_MASK;
1444 if (!NEEDED(*bmc) && aborted)
1445 *bmc |= NEEDED_MASK;
1448 set_page_attr(bitmap,
1449 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1455 spin_unlock_irqrestore(&bitmap->lock, flags);
1456 bitmap->allclean = 0;
1459 void bitmap_close_sync(struct bitmap *bitmap)
1461 /* Sync has finished, and any bitmap chunks that weren't synced
1462 * properly have been aborted. It remains to us to clear the
1463 * RESYNC bit wherever it is still on
1465 sector_t sector = 0;
1469 while (sector < bitmap->mddev->resync_max_sectors) {
1470 bitmap_end_sync(bitmap, sector, &blocks, 0);
1475 void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1483 bitmap->last_end_sync = jiffies;
1486 if (time_before(jiffies, (bitmap->last_end_sync
1487 + bitmap->mddev->bitmap_info.daemon_sleep)))
1489 wait_event(bitmap->mddev->recovery_wait,
1490 atomic_read(&bitmap->mddev->recovery_active) == 0);
1492 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1493 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
1494 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1496 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1497 bitmap_end_sync(bitmap, s, &blocks, 0);
1500 bitmap->last_end_sync = jiffies;
1501 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
1504 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1506 /* For each chunk covered by any of these sectors, set the
1507 * counter to 1 and set resync_needed. They should all
1508 * be 0 at this point
1512 bitmap_counter_t *bmc;
1513 spin_lock_irq(&bitmap->lock);
1514 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1516 spin_unlock_irq(&bitmap->lock);
1521 *bmc = 1 | (needed?NEEDED_MASK:0);
1522 bitmap_count_page(bitmap, offset, 1);
1523 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1524 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1526 spin_unlock_irq(&bitmap->lock);
1527 bitmap->allclean = 0;
1530 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1531 void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1533 unsigned long chunk;
1535 for (chunk = s; chunk <= e; chunk++) {
1536 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
1537 bitmap_set_memory_bits(bitmap, sec, 1);
1538 bitmap_file_set_bit(bitmap, sec);
1543 * flush out any pending updates
1545 void bitmap_flush(mddev_t *mddev)
1547 struct bitmap *bitmap = mddev->bitmap;
1550 if (!bitmap) /* there was no bitmap */
1553 /* run the daemon_work three time to ensure everything is flushed
1556 sleep = mddev->bitmap_info.daemon_sleep * 2;
1557 bitmap->daemon_lastrun -= sleep;
1558 bitmap_daemon_work(mddev);
1559 bitmap->daemon_lastrun -= sleep;
1560 bitmap_daemon_work(mddev);
1561 bitmap->daemon_lastrun -= sleep;
1562 bitmap_daemon_work(mddev);
1563 bitmap_update_sb(bitmap);
1567 * free memory that was allocated
1569 static void bitmap_free(struct bitmap *bitmap)
1571 unsigned long k, pages;
1572 struct bitmap_page *bp;
1574 if (!bitmap) /* there was no bitmap */
1577 /* release the bitmap file and kill the daemon */
1578 bitmap_file_put(bitmap);
1581 pages = bitmap->pages;
1583 /* free all allocated memory */
1585 if (bp) /* deallocate the page memory */
1586 for (k = 0; k < pages; k++)
1587 if (bp[k].map && !bp[k].hijacked)
1593 void bitmap_destroy(mddev_t *mddev)
1595 struct bitmap *bitmap = mddev->bitmap;
1597 if (!bitmap) /* there was no bitmap */
1600 mutex_lock(&mddev->bitmap_info.mutex);
1601 mddev->bitmap = NULL; /* disconnect from the md device */
1602 mutex_unlock(&mddev->bitmap_info.mutex);
1604 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1606 bitmap_free(bitmap);
1610 * initialize the bitmap structure
1611 * if this returns an error, bitmap_destroy must be called to do clean up
1613 int bitmap_create(mddev_t *mddev)
1615 struct bitmap *bitmap;
1616 sector_t blocks = mddev->resync_max_sectors;
1617 unsigned long chunks;
1618 unsigned long pages;
1619 struct file *file = mddev->bitmap_info.file;
1623 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
1625 if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
1628 BUG_ON(file && mddev->bitmap_info.offset);
1630 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1634 spin_lock_init(&bitmap->lock);
1635 atomic_set(&bitmap->pending_writes, 0);
1636 init_waitqueue_head(&bitmap->write_wait);
1637 init_waitqueue_head(&bitmap->overflow_wait);
1639 bitmap->mddev = mddev;
1641 bitmap->file = file;
1644 /* As future accesses to this file will use bmap,
1645 * and bypass the page cache, we must sync the file
1648 vfs_fsync(file, file->f_dentry, 1);
1650 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
1651 err = bitmap_read_sb(bitmap);
1655 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
1657 /* now that chunksize and chunkshift are set, we can use these macros */
1658 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1659 CHUNK_BLOCK_SHIFT(bitmap);
1660 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1664 bitmap->chunks = chunks;
1665 bitmap->pages = pages;
1666 bitmap->missing_pages = pages;
1667 bitmap->counter_bits = COUNTER_BITS;
1669 bitmap->syncchunk = ~0UL;
1671 #ifdef INJECT_FATAL_FAULT_1
1674 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1680 /* now that we have some pages available, initialize the in-memory
1681 * bitmap from the on-disk bitmap */
1683 if (mddev->degraded == 0
1684 || bitmap->events_cleared == mddev->events)
1685 /* no need to keep dirty bits to optimise a re-add of a missing device */
1686 start = mddev->recovery_cp;
1687 err = bitmap_init_from_disk(bitmap, start);
1692 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1693 pages, bmname(bitmap));
1695 mddev->bitmap = bitmap;
1697 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
1698 md_wakeup_thread(mddev->thread);
1700 bitmap_update_sb(bitmap);
1702 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1705 bitmap_free(bitmap);
1709 /* the bitmap API -- for raid personalities */
1710 EXPORT_SYMBOL(bitmap_startwrite);
1711 EXPORT_SYMBOL(bitmap_endwrite);
1712 EXPORT_SYMBOL(bitmap_start_sync);
1713 EXPORT_SYMBOL(bitmap_end_sync);
1714 EXPORT_SYMBOL(bitmap_unplug);
1715 EXPORT_SYMBOL(bitmap_close_sync);
1716 EXPORT_SYMBOL(bitmap_cond_end_sync);