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
10 * - added bitmap daemon (to asynchronously clear bitmap bits from disk)
16 * flush after percent set rather than just time based. (maybe both).
17 * wait if count gets too high, wake when it drops to half.
18 * allow bitmap to be mirrored with superblock (before or after...)
19 * allow hot-add to re-instate a current device.
20 * allow hot-add of bitmap after quiessing device
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/config.h>
28 #include <linux/timer.h>
29 #include <linux/sched.h>
30 #include <linux/list.h>
31 #include <linux/file.h>
32 #include <linux/mount.h>
33 #include <linux/buffer_head.h>
34 #include <linux/raid/md.h>
35 #include <linux/raid/bitmap.h>
42 /* these are for debugging purposes only! */
44 /* define one and only one of these */
45 #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
46 #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
47 #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
48 #define INJECT_FAULTS_4 0 /* undef */
49 #define INJECT_FAULTS_5 0 /* undef */
50 #define INJECT_FAULTS_6 0
52 /* if these are defined, the driver will fail! debug only */
53 #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
54 #define INJECT_FATAL_FAULT_2 0 /* undef */
55 #define INJECT_FATAL_FAULT_3 0 /* undef */
58 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
59 #define DPRINTK(x...) do { } while(0)
63 # define PRINTK(x...) printk(KERN_DEBUG x)
69 static inline char * bmname(struct bitmap
*bitmap
)
71 return bitmap
->mddev
? mdname(bitmap
->mddev
) : "mdX";
76 * test if the bitmap is active
78 int bitmap_active(struct bitmap
*bitmap
)
85 spin_lock_irqsave(&bitmap
->lock
, flags
);
86 res
= bitmap
->flags
& BITMAP_ACTIVE
;
87 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
91 #define WRITE_POOL_SIZE 256
92 /* mempool for queueing pending writes on the bitmap file */
93 static void *write_pool_alloc(gfp_t gfp_flags
, void *data
)
95 return kmalloc(sizeof(struct page_list
), gfp_flags
);
98 static void write_pool_free(void *ptr
, void *data
)
104 * just a placeholder - calls kmalloc for bitmap pages
106 static unsigned char *bitmap_alloc_page(struct bitmap
*bitmap
)
110 #ifdef INJECT_FAULTS_1
113 page
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
116 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap
));
118 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
119 bmname(bitmap
), page
);
124 * for now just a placeholder -- just calls kfree for bitmap pages
126 static void bitmap_free_page(struct bitmap
*bitmap
, unsigned char *page
)
128 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap
), page
);
133 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
135 * 1) check to see if this page is allocated, if it's not then try to alloc
136 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
137 * page pointer directly as a counter
139 * if we find our page, we increment the page's refcount so that it stays
140 * allocated while we're using it
142 static int bitmap_checkpage(struct bitmap
*bitmap
, unsigned long page
, int create
)
144 unsigned char *mappage
;
146 if (page
>= bitmap
->pages
) {
148 "%s: invalid bitmap page request: %lu (> %lu)\n",
149 bmname(bitmap
), page
, bitmap
->pages
-1);
154 if (bitmap
->bp
[page
].hijacked
) /* it's hijacked, don't try to alloc */
157 if (bitmap
->bp
[page
].map
) /* page is already allocated, just return */
163 spin_unlock_irq(&bitmap
->lock
);
165 /* this page has not been allocated yet */
167 if ((mappage
= bitmap_alloc_page(bitmap
)) == NULL
) {
168 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
170 /* failed - set the hijacked flag so that we can use the
171 * pointer as a counter */
172 spin_lock_irq(&bitmap
->lock
);
173 if (!bitmap
->bp
[page
].map
)
174 bitmap
->bp
[page
].hijacked
= 1;
180 spin_lock_irq(&bitmap
->lock
);
182 /* recheck the page */
184 if (bitmap
->bp
[page
].map
|| bitmap
->bp
[page
].hijacked
) {
185 /* somebody beat us to getting the page */
186 bitmap_free_page(bitmap
, mappage
);
190 /* no page was in place and we have one, so install it */
192 memset(mappage
, 0, PAGE_SIZE
);
193 bitmap
->bp
[page
].map
= mappage
;
194 bitmap
->missing_pages
--;
200 /* if page is completely empty, put it back on the free list, or dealloc it */
201 /* if page was hijacked, unmark the flag so it might get alloced next time */
202 /* Note: lock should be held when calling this */
203 static inline void bitmap_checkfree(struct bitmap
*bitmap
, unsigned long page
)
207 if (bitmap
->bp
[page
].count
) /* page is still busy */
210 /* page is no longer in use, it can be released */
212 if (bitmap
->bp
[page
].hijacked
) { /* page was hijacked, undo this now */
213 bitmap
->bp
[page
].hijacked
= 0;
214 bitmap
->bp
[page
].map
= NULL
;
218 /* normal case, free the page */
221 /* actually ... let's not. We will probably need the page again exactly when
222 * memory is tight and we are flusing to disk
226 ptr
= bitmap
->bp
[page
].map
;
227 bitmap
->bp
[page
].map
= NULL
;
228 bitmap
->missing_pages
++;
229 bitmap_free_page(bitmap
, ptr
);
236 * bitmap file handling - read and write the bitmap file and its superblock
239 /* copy the pathname of a file to a buffer */
240 char *file_path(struct file
*file
, char *buf
, int count
)
251 buf
= d_path(d
, v
, buf
, count
);
253 return IS_ERR(buf
) ? NULL
: buf
;
257 * basic page I/O operations
260 /* IO operations when bitmap is stored near all superblocks */
261 static struct page
*read_sb_page(mddev_t
*mddev
, long offset
, unsigned long index
)
263 /* choose a good rdev and read the page from there */
266 struct list_head
*tmp
;
267 struct page
*page
= alloc_page(GFP_KERNEL
);
271 return ERR_PTR(-ENOMEM
);
273 ITERATE_RDEV(mddev
, rdev
, tmp
) {
274 if (! test_bit(In_sync
, &rdev
->flags
)
275 || test_bit(Faulty
, &rdev
->flags
))
278 target
= (rdev
->sb_offset
<< 1) + offset
+ index
* (PAGE_SIZE
/512);
280 if (sync_page_io(rdev
->bdev
, target
, PAGE_SIZE
, page
, READ
)) {
285 return ERR_PTR(-EIO
);
289 static int write_sb_page(mddev_t
*mddev
, long offset
, struct page
*page
, int wait
)
292 struct list_head
*tmp
;
294 ITERATE_RDEV(mddev
, rdev
, tmp
)
295 if (test_bit(In_sync
, &rdev
->flags
)
296 && !test_bit(Faulty
, &rdev
->flags
))
297 md_super_write(mddev
, rdev
,
298 (rdev
->sb_offset
<<1) + offset
299 + page
->index
* (PAGE_SIZE
/512),
304 md_super_wait(mddev
);
309 * write out a page to a file
311 static int write_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
315 if (bitmap
->file
== NULL
)
316 return write_sb_page(bitmap
->mddev
, bitmap
->offset
, page
, wait
);
321 if (TestSetPageLocked(page
))
322 return -EAGAIN
; /* already locked */
323 if (PageWriteback(page
)) {
329 ret
= page
->mapping
->a_ops
->prepare_write(bitmap
->file
, page
, 0, PAGE_SIZE
);
331 ret
= page
->mapping
->a_ops
->commit_write(bitmap
->file
, page
, 0,
338 set_page_dirty(page
); /* force it to be written out */
341 /* add to list to be waited for by daemon */
342 struct page_list
*item
= mempool_alloc(bitmap
->write_pool
, GFP_NOIO
);
345 spin_lock(&bitmap
->write_lock
);
346 list_add(&item
->list
, &bitmap
->complete_pages
);
347 spin_unlock(&bitmap
->write_lock
);
348 md_wakeup_thread(bitmap
->writeback_daemon
);
350 return write_one_page(page
, wait
);
353 /* read a page from a file, pinning it into cache, and return bytes_read */
354 static struct page
*read_page(struct file
*file
, unsigned long index
,
355 unsigned long *bytes_read
)
357 struct inode
*inode
= file
->f_mapping
->host
;
358 struct page
*page
= NULL
;
359 loff_t isize
= i_size_read(inode
);
360 unsigned long end_index
= isize
>> PAGE_SHIFT
;
362 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE
,
363 (unsigned long long)index
<< PAGE_SHIFT
);
365 page
= read_cache_page(inode
->i_mapping
, index
,
366 (filler_t
*)inode
->i_mapping
->a_ops
->readpage
, file
);
369 wait_on_page_locked(page
);
370 if (!PageUptodate(page
) || PageError(page
)) {
372 page
= ERR_PTR(-EIO
);
376 if (index
> end_index
) /* we have read beyond EOF */
378 else if (index
== end_index
) /* possible short read */
379 *bytes_read
= isize
& ~PAGE_MASK
;
381 *bytes_read
= PAGE_SIZE
; /* got a full page */
384 printk(KERN_ALERT
"md: bitmap read error: (%dB @ %Lu): %ld\n",
386 (unsigned long long)index
<< PAGE_SHIFT
,
392 * bitmap file superblock operations
395 /* update the event counter and sync the superblock to disk */
396 int bitmap_update_sb(struct bitmap
*bitmap
)
401 if (!bitmap
|| !bitmap
->mddev
) /* no bitmap for this array */
403 spin_lock_irqsave(&bitmap
->lock
, flags
);
404 if (!bitmap
->sb_page
) { /* no superblock */
405 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
408 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
409 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
410 sb
->events
= cpu_to_le64(bitmap
->mddev
->events
);
411 if (!bitmap
->mddev
->degraded
)
412 sb
->events_cleared
= cpu_to_le64(bitmap
->mddev
->events
);
413 kunmap_atomic(sb
, KM_USER0
);
414 return write_page(bitmap
, bitmap
->sb_page
, 1);
417 /* print out the bitmap file superblock */
418 void bitmap_print_sb(struct bitmap
*bitmap
)
422 if (!bitmap
|| !bitmap
->sb_page
)
424 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
425 printk(KERN_DEBUG
"%s: bitmap file superblock:\n", bmname(bitmap
));
426 printk(KERN_DEBUG
" magic: %08x\n", le32_to_cpu(sb
->magic
));
427 printk(KERN_DEBUG
" version: %d\n", le32_to_cpu(sb
->version
));
428 printk(KERN_DEBUG
" uuid: %08x.%08x.%08x.%08x\n",
429 *(__u32
*)(sb
->uuid
+0),
430 *(__u32
*)(sb
->uuid
+4),
431 *(__u32
*)(sb
->uuid
+8),
432 *(__u32
*)(sb
->uuid
+12));
433 printk(KERN_DEBUG
" events: %llu\n",
434 (unsigned long long) le64_to_cpu(sb
->events
));
435 printk(KERN_DEBUG
"events cleared: %llu\n",
436 (unsigned long long) le64_to_cpu(sb
->events_cleared
));
437 printk(KERN_DEBUG
" state: %08x\n", le32_to_cpu(sb
->state
));
438 printk(KERN_DEBUG
" chunksize: %d B\n", le32_to_cpu(sb
->chunksize
));
439 printk(KERN_DEBUG
" daemon sleep: %ds\n", le32_to_cpu(sb
->daemon_sleep
));
440 printk(KERN_DEBUG
" sync size: %llu KB\n",
441 (unsigned long long)le64_to_cpu(sb
->sync_size
)/2);
442 printk(KERN_DEBUG
"max write behind: %d\n", le32_to_cpu(sb
->write_behind
));
443 kunmap_atomic(sb
, KM_USER0
);
446 /* read the superblock from the bitmap file and initialize some bitmap fields */
447 static int bitmap_read_sb(struct bitmap
*bitmap
)
451 unsigned long chunksize
, daemon_sleep
, write_behind
;
452 unsigned long bytes_read
;
453 unsigned long long events
;
456 /* page 0 is the superblock, read it... */
458 bitmap
->sb_page
= read_page(bitmap
->file
, 0, &bytes_read
);
460 bitmap
->sb_page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
, 0);
461 bytes_read
= PAGE_SIZE
;
463 if (IS_ERR(bitmap
->sb_page
)) {
464 err
= PTR_ERR(bitmap
->sb_page
);
465 bitmap
->sb_page
= NULL
;
469 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
471 if (bytes_read
< sizeof(*sb
)) { /* short read */
472 printk(KERN_INFO
"%s: bitmap file superblock truncated\n",
478 chunksize
= le32_to_cpu(sb
->chunksize
);
479 daemon_sleep
= le32_to_cpu(sb
->daemon_sleep
);
480 write_behind
= le32_to_cpu(sb
->write_behind
);
482 /* verify that the bitmap-specific fields are valid */
483 if (sb
->magic
!= cpu_to_le32(BITMAP_MAGIC
))
484 reason
= "bad magic";
485 else if (le32_to_cpu(sb
->version
) < BITMAP_MAJOR_LO
||
486 le32_to_cpu(sb
->version
) > BITMAP_MAJOR_HI
)
487 reason
= "unrecognized superblock version";
488 else if (chunksize
< PAGE_SIZE
)
489 reason
= "bitmap chunksize too small";
490 else if ((1 << ffz(~chunksize
)) != chunksize
)
491 reason
= "bitmap chunksize not a power of 2";
492 else if (daemon_sleep
< 1 || daemon_sleep
> MAX_SCHEDULE_TIMEOUT
/ HZ
)
493 reason
= "daemon sleep period out of range";
494 else if (write_behind
> COUNTER_MAX
)
495 reason
= "write-behind limit out of range (0 - 16383)";
497 printk(KERN_INFO
"%s: invalid bitmap file superblock: %s\n",
498 bmname(bitmap
), reason
);
502 /* keep the array size field of the bitmap superblock up to date */
503 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
505 if (!bitmap
->mddev
->persistent
)
509 * if we have a persistent array superblock, compare the
510 * bitmap's UUID and event counter to the mddev's
512 if (memcmp(sb
->uuid
, bitmap
->mddev
->uuid
, 16)) {
513 printk(KERN_INFO
"%s: bitmap superblock UUID mismatch\n",
517 events
= le64_to_cpu(sb
->events
);
518 if (events
< bitmap
->mddev
->events
) {
519 printk(KERN_INFO
"%s: bitmap file is out of date (%llu < %llu) "
520 "-- forcing full recovery\n", bmname(bitmap
), events
,
521 (unsigned long long) bitmap
->mddev
->events
);
522 sb
->state
|= BITMAP_STALE
;
525 /* assign fields using values from superblock */
526 bitmap
->chunksize
= chunksize
;
527 bitmap
->daemon_sleep
= daemon_sleep
;
528 bitmap
->daemon_lastrun
= jiffies
;
529 bitmap
->max_write_behind
= write_behind
;
530 bitmap
->flags
|= sb
->state
;
531 if (le32_to_cpu(sb
->version
) == BITMAP_MAJOR_HOSTENDIAN
)
532 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
533 bitmap
->events_cleared
= le64_to_cpu(sb
->events_cleared
);
534 if (sb
->state
& BITMAP_STALE
)
535 bitmap
->events_cleared
= bitmap
->mddev
->events
;
538 kunmap_atomic(sb
, KM_USER0
);
540 bitmap_print_sb(bitmap
);
544 enum bitmap_mask_op
{
549 /* record the state of the bitmap in the superblock */
550 static void bitmap_mask_state(struct bitmap
*bitmap
, enum bitmap_state bits
,
551 enum bitmap_mask_op op
)
556 spin_lock_irqsave(&bitmap
->lock
, flags
);
557 if (!bitmap
|| !bitmap
->sb_page
) { /* can't set the state */
558 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
561 get_page(bitmap
->sb_page
);
562 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
563 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
565 case MASK_SET
: sb
->state
|= bits
;
567 case MASK_UNSET
: sb
->state
&= ~bits
;
571 kunmap_atomic(sb
, KM_USER0
);
572 put_page(bitmap
->sb_page
);
576 * general bitmap file operations
579 /* calculate the index of the page that contains this bit */
580 static inline unsigned long file_page_index(unsigned long chunk
)
582 return CHUNK_BIT_OFFSET(chunk
) >> PAGE_BIT_SHIFT
;
585 /* calculate the (bit) offset of this bit within a page */
586 static inline unsigned long file_page_offset(unsigned long chunk
)
588 return CHUNK_BIT_OFFSET(chunk
) & (PAGE_BITS
- 1);
592 * return a pointer to the page in the filemap that contains the given bit
594 * this lookup is complicated by the fact that the bitmap sb might be exactly
595 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
598 static inline struct page
*filemap_get_page(struct bitmap
*bitmap
,
601 return bitmap
->filemap
[file_page_index(chunk
) - file_page_index(0)];
605 static void bitmap_file_unmap(struct bitmap
*bitmap
)
607 struct page
**map
, *sb_page
;
612 spin_lock_irqsave(&bitmap
->lock
, flags
);
613 map
= bitmap
->filemap
;
614 bitmap
->filemap
= NULL
;
615 attr
= bitmap
->filemap_attr
;
616 bitmap
->filemap_attr
= NULL
;
617 pages
= bitmap
->file_pages
;
618 bitmap
->file_pages
= 0;
619 sb_page
= bitmap
->sb_page
;
620 bitmap
->sb_page
= NULL
;
621 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
624 if (map
[pages
]->index
!= 0) /* 0 is sb_page, release it below */
625 put_page(map
[pages
]);
629 safe_put_page(sb_page
);
632 static void bitmap_stop_daemon(struct bitmap
*bitmap
);
634 /* dequeue the next item in a page list -- don't call from irq context */
635 static struct page_list
*dequeue_page(struct bitmap
*bitmap
)
637 struct page_list
*item
= NULL
;
638 struct list_head
*head
= &bitmap
->complete_pages
;
640 spin_lock(&bitmap
->write_lock
);
641 if (list_empty(head
))
643 item
= list_entry(head
->prev
, struct page_list
, list
);
644 list_del(head
->prev
);
646 spin_unlock(&bitmap
->write_lock
);
650 static void drain_write_queues(struct bitmap
*bitmap
)
652 struct page_list
*item
;
654 while ((item
= dequeue_page(bitmap
))) {
655 /* don't bother to wait */
656 put_page(item
->page
);
657 mempool_free(item
, bitmap
->write_pool
);
660 wake_up(&bitmap
->write_wait
);
663 static void bitmap_file_put(struct bitmap
*bitmap
)
669 spin_lock_irqsave(&bitmap
->lock
, flags
);
672 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
674 bitmap_stop_daemon(bitmap
);
676 drain_write_queues(bitmap
);
678 bitmap_file_unmap(bitmap
);
681 inode
= file
->f_mapping
->host
;
682 spin_lock(&inode
->i_lock
);
683 atomic_set(&inode
->i_writecount
, 1); /* allow writes again */
684 spin_unlock(&inode
->i_lock
);
691 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
692 * then it is no longer reliable, so we stop using it and we mark the file
693 * as failed in the superblock
695 static void bitmap_file_kick(struct bitmap
*bitmap
)
697 char *path
, *ptr
= NULL
;
699 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_SET
);
700 bitmap_update_sb(bitmap
);
703 path
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
705 ptr
= file_path(bitmap
->file
, path
, PAGE_SIZE
);
707 printk(KERN_ALERT
"%s: kicking failed bitmap file %s from array!\n",
708 bmname(bitmap
), ptr
? ptr
: "");
713 bitmap_file_put(bitmap
);
718 enum bitmap_page_attr
{
719 BITMAP_PAGE_DIRTY
= 1, // there are set bits that need to be synced
720 BITMAP_PAGE_CLEAN
= 2, // there are bits that might need to be cleared
721 BITMAP_PAGE_NEEDWRITE
=4, // there are cleared bits that need to be synced
724 static inline void set_page_attr(struct bitmap
*bitmap
, struct page
*page
,
725 enum bitmap_page_attr attr
)
727 bitmap
->filemap_attr
[page
->index
] |= attr
;
730 static inline void clear_page_attr(struct bitmap
*bitmap
, struct page
*page
,
731 enum bitmap_page_attr attr
)
733 bitmap
->filemap_attr
[page
->index
] &= ~attr
;
736 static inline unsigned long get_page_attr(struct bitmap
*bitmap
, struct page
*page
)
738 return bitmap
->filemap_attr
[page
->index
];
742 * bitmap_file_set_bit -- called before performing a write to the md device
743 * to set (and eventually sync) a particular bit in the bitmap file
745 * we set the bit immediately, then we record the page number so that
746 * when an unplug occurs, we can flush the dirty pages out to disk
748 static void bitmap_file_set_bit(struct bitmap
*bitmap
, sector_t block
)
753 unsigned long chunk
= block
>> CHUNK_BLOCK_SHIFT(bitmap
);
755 if (!bitmap
->filemap
) {
759 page
= filemap_get_page(bitmap
, chunk
);
760 bit
= file_page_offset(chunk
);
763 /* make sure the page stays cached until it gets written out */
764 if (! (get_page_attr(bitmap
, page
) & BITMAP_PAGE_DIRTY
))
768 kaddr
= kmap_atomic(page
, KM_USER0
);
769 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
772 ext2_set_bit(bit
, kaddr
);
773 kunmap_atomic(kaddr
, KM_USER0
);
774 PRINTK("set file bit %lu page %lu\n", bit
, page
->index
);
776 /* record page number so it gets flushed to disk when unplug occurs */
777 set_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
781 /* this gets called when the md device is ready to unplug its underlying
782 * (slave) device queues -- before we let any writes go down, we need to
783 * sync the dirty pages of the bitmap file to disk */
784 int bitmap_unplug(struct bitmap
*bitmap
)
786 unsigned long i
, attr
, flags
;
794 /* look at each page to see if there are any set bits that need to be
795 * flushed out to disk */
796 for (i
= 0; i
< bitmap
->file_pages
; i
++) {
797 spin_lock_irqsave(&bitmap
->lock
, flags
);
798 if (!bitmap
->filemap
) {
799 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
802 page
= bitmap
->filemap
[i
];
803 attr
= get_page_attr(bitmap
, page
);
804 clear_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
805 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
806 if ((attr
& BITMAP_PAGE_DIRTY
))
808 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
810 if (attr
& (BITMAP_PAGE_DIRTY
| BITMAP_PAGE_NEEDWRITE
)) {
811 err
= write_page(bitmap
, page
, 0);
812 if (err
== -EAGAIN
) {
813 if (attr
& BITMAP_PAGE_DIRTY
)
814 err
= write_page(bitmap
, page
, 1);
822 if (wait
) { /* if any writes were performed, we need to wait on them */
824 spin_lock_irq(&bitmap
->write_lock
);
825 wait_event_lock_irq(bitmap
->write_wait
,
826 list_empty(&bitmap
->complete_pages
), bitmap
->write_lock
,
827 wake_up_process(bitmap
->writeback_daemon
->tsk
));
828 spin_unlock_irq(&bitmap
->write_lock
);
830 md_super_wait(bitmap
->mddev
);
835 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
);
836 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
837 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
838 * memory mapping of the bitmap file
840 * if there's no bitmap file, or if the bitmap file had been
841 * previously kicked from the array, we mark all the bits as
842 * 1's in order to cause a full resync.
844 * We ignore all bits for sectors that end earlier than 'start'.
845 * This is used when reading an out-of-date bitmap...
847 static int bitmap_init_from_disk(struct bitmap
*bitmap
, sector_t start
)
849 unsigned long i
, chunks
, index
, oldindex
, bit
;
850 struct page
*page
= NULL
, *oldpage
= NULL
;
851 unsigned long num_pages
, bit_cnt
= 0;
853 unsigned long bytes
, offset
, dummy
;
858 chunks
= bitmap
->chunks
;
861 BUG_ON(!file
&& !bitmap
->offset
);
863 #ifdef INJECT_FAULTS_3
866 outofdate
= bitmap
->flags
& BITMAP_STALE
;
869 printk(KERN_INFO
"%s: bitmap file is out of date, doing full "
870 "recovery\n", bmname(bitmap
));
872 bytes
= (chunks
+ 7) / 8;
874 num_pages
= (bytes
+ sizeof(bitmap_super_t
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
876 if (file
&& i_size_read(file
->f_mapping
->host
) < bytes
+ sizeof(bitmap_super_t
)) {
877 printk(KERN_INFO
"%s: bitmap file too short %lu < %lu\n",
879 (unsigned long) i_size_read(file
->f_mapping
->host
),
880 bytes
+ sizeof(bitmap_super_t
));
886 bitmap
->filemap
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
887 if (!bitmap
->filemap
)
890 bitmap
->filemap_attr
= kzalloc(sizeof(long) * num_pages
, GFP_KERNEL
);
891 if (!bitmap
->filemap_attr
)
896 for (i
= 0; i
< chunks
; i
++) {
898 index
= file_page_index(i
);
899 bit
= file_page_offset(i
);
900 if (index
!= oldindex
) { /* this is a new page, read it in */
901 /* unmap the old page, we're done with it */
904 * if we're here then the superblock page
905 * contains some bits (PAGE_SIZE != sizeof sb)
906 * we've already read it in, so just use it
908 page
= bitmap
->sb_page
;
909 offset
= sizeof(bitmap_super_t
);
911 page
= read_page(file
, index
, &dummy
);
914 page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
, index
);
917 if (IS_ERR(page
)) { /* read error */
927 * if bitmap is out of date, dirty the
928 * whole page and write it out
930 paddr
= kmap_atomic(page
, KM_USER0
);
931 memset(paddr
+ offset
, 0xff,
933 kunmap_atomic(paddr
, KM_USER0
);
934 ret
= write_page(bitmap
, page
, 1);
936 /* release, page not in filemap yet */
942 bitmap
->filemap
[bitmap
->file_pages
++] = page
;
944 paddr
= kmap_atomic(page
, KM_USER0
);
945 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
946 b
= test_bit(bit
, paddr
);
948 b
= ext2_test_bit(bit
, paddr
);
949 kunmap_atomic(paddr
, KM_USER0
);
951 /* if the disk bit is set, set the memory bit */
952 bitmap_set_memory_bits(bitmap
, i
<< CHUNK_BLOCK_SHIFT(bitmap
),
953 ((i
+1) << (CHUNK_BLOCK_SHIFT(bitmap
)) >= start
)
956 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
960 /* everything went OK */
962 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_UNSET
);
964 if (bit_cnt
) { /* Kick recovery if any bits were set */
965 set_bit(MD_RECOVERY_NEEDED
, &bitmap
->mddev
->recovery
);
966 md_wakeup_thread(bitmap
->mddev
->thread
);
970 printk(KERN_INFO
"%s: bitmap initialized from disk: "
971 "read %lu/%lu pages, set %lu bits, status: %d\n",
972 bmname(bitmap
), bitmap
->file_pages
, num_pages
, bit_cnt
, ret
);
977 void bitmap_write_all(struct bitmap
*bitmap
)
979 /* We don't actually write all bitmap blocks here,
980 * just flag them as needing to be written
983 unsigned long chunks
= bitmap
->chunks
;
984 unsigned long bytes
= (chunks
+7)/8 + sizeof(bitmap_super_t
);
985 unsigned long num_pages
= (bytes
+ PAGE_SIZE
-1) / PAGE_SIZE
;
987 bitmap
->filemap_attr
[num_pages
] |= BITMAP_PAGE_NEEDWRITE
;
991 static void bitmap_count_page(struct bitmap
*bitmap
, sector_t offset
, int inc
)
993 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
994 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
995 bitmap
->bp
[page
].count
+= inc
;
997 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
998 (unsigned long long)offset, inc, bitmap->bp[page].count);
1000 bitmap_checkfree(bitmap
, page
);
1002 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1003 sector_t offset
, int *blocks
,
1007 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1011 int bitmap_daemon_work(struct bitmap
*bitmap
)
1014 unsigned long flags
;
1015 struct page
*page
= NULL
, *lastpage
= NULL
;
1023 if (time_before(jiffies
, bitmap
->daemon_lastrun
+ bitmap
->daemon_sleep
*HZ
))
1025 bitmap
->daemon_lastrun
= jiffies
;
1027 for (j
= 0; j
< bitmap
->chunks
; j
++) {
1028 bitmap_counter_t
*bmc
;
1029 spin_lock_irqsave(&bitmap
->lock
, flags
);
1030 if (!bitmap
->filemap
) {
1031 /* error or shutdown */
1032 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1036 page
= filemap_get_page(bitmap
, j
);
1038 if (page
!= lastpage
) {
1039 /* skip this page unless it's marked as needing cleaning */
1040 if (!((attr
=get_page_attr(bitmap
, page
)) & BITMAP_PAGE_CLEAN
)) {
1041 if (attr
& BITMAP_PAGE_NEEDWRITE
) {
1043 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1045 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1046 if (attr
& BITMAP_PAGE_NEEDWRITE
) {
1047 switch (write_page(bitmap
, page
, 0)) {
1049 set_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1054 bitmap_file_kick(bitmap
);
1061 /* grab the new page, sync and release the old */
1063 if (lastpage
!= NULL
) {
1064 if (get_page_attr(bitmap
, lastpage
) & BITMAP_PAGE_NEEDWRITE
) {
1065 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1066 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1067 err
= write_page(bitmap
, lastpage
, 0);
1068 if (err
== -EAGAIN
) {
1070 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1073 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1074 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1078 bitmap_file_kick(bitmap
);
1080 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1083 printk("bitmap clean at page %lu\n", j);
1085 spin_lock_irqsave(&bitmap
->lock
, flags
);
1086 clear_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1088 bmc
= bitmap_get_counter(bitmap
, j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1092 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1095 *bmc
=1; /* maybe clear the bit next time */
1096 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1097 } else if (*bmc
== 1) {
1098 /* we can clear the bit */
1100 bitmap_count_page(bitmap
, j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1104 paddr
= kmap_atomic(page
, KM_USER0
);
1105 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1106 clear_bit(file_page_offset(j
), paddr
);
1108 ext2_clear_bit(file_page_offset(j
), paddr
);
1109 kunmap_atomic(paddr
, KM_USER0
);
1112 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1115 /* now sync the final page */
1116 if (lastpage
!= NULL
) {
1117 spin_lock_irqsave(&bitmap
->lock
, flags
);
1118 if (get_page_attr(bitmap
, lastpage
) &BITMAP_PAGE_NEEDWRITE
) {
1119 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1120 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1121 err
= write_page(bitmap
, lastpage
, 0);
1122 if (err
== -EAGAIN
) {
1123 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1127 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1128 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1137 static void daemon_exit(struct bitmap
*bitmap
, mdk_thread_t
**daemon
)
1140 unsigned long flags
;
1142 /* if no one is waiting on us, we'll free the md thread struct
1143 * and exit, otherwise we let the waiter clean things up */
1144 spin_lock_irqsave(&bitmap
->lock
, flags
);
1145 if ((dmn
= *daemon
)) { /* no one is waiting, cleanup and exit */
1147 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1149 complete_and_exit(NULL
, 0); /* do_exit not exported */
1151 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1154 static void bitmap_writeback_daemon(mddev_t
*mddev
)
1156 struct bitmap
*bitmap
= mddev
->bitmap
;
1158 struct page_list
*item
;
1161 if (signal_pending(current
)) {
1163 "%s: bitmap writeback daemon got signal, exiting...\n",
1169 /* about to be stopped. */
1172 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap
));
1173 /* wait on bitmap page writebacks */
1174 while ((item
= dequeue_page(bitmap
))) {
1176 mempool_free(item
, bitmap
->write_pool
);
1177 PRINTK("wait on page writeback: %p\n", page
);
1178 wait_on_page_writeback(page
);
1179 PRINTK("finished page writeback: %p\n", page
);
1181 err
= PageError(page
);
1184 printk(KERN_WARNING
"%s: bitmap file writeback "
1185 "failed (page %lu): %d\n",
1186 bmname(bitmap
), page
->index
, err
);
1187 bitmap_file_kick(bitmap
);
1192 wake_up(&bitmap
->write_wait
);
1194 printk(KERN_INFO
"%s: bitmap writeback daemon exiting (%d)\n",
1195 bmname(bitmap
), err
);
1196 daemon_exit(bitmap
, &bitmap
->writeback_daemon
);
1200 static mdk_thread_t
*bitmap_start_daemon(struct bitmap
*bitmap
,
1201 void (*func
)(mddev_t
*), char *name
)
1203 mdk_thread_t
*daemon
;
1206 #ifdef INJECT_FATAL_FAULT_2
1209 sprintf(namebuf
, "%%s_%s", name
);
1210 daemon
= md_register_thread(func
, bitmap
->mddev
, namebuf
);
1213 printk(KERN_ERR
"%s: failed to start bitmap daemon\n",
1215 return ERR_PTR(-ECHILD
);
1218 md_wakeup_thread(daemon
); /* start it running */
1220 PRINTK("%s: %s daemon (pid %d) started...\n",
1221 bmname(bitmap
), name
, daemon
->tsk
->pid
);
1226 static void bitmap_stop_daemon(struct bitmap
*bitmap
)
1228 /* the daemon can't stop itself... it'll just exit instead... */
1229 if (bitmap
->writeback_daemon
&& ! IS_ERR(bitmap
->writeback_daemon
) &&
1230 current
->pid
!= bitmap
->writeback_daemon
->tsk
->pid
) {
1231 mdk_thread_t
*daemon
;
1232 unsigned long flags
;
1234 spin_lock_irqsave(&bitmap
->lock
, flags
);
1235 daemon
= bitmap
->writeback_daemon
;
1236 bitmap
->writeback_daemon
= NULL
;
1237 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1238 if (daemon
&& ! IS_ERR(daemon
))
1239 md_unregister_thread(daemon
); /* destroy the thread */
1243 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1244 sector_t offset
, int *blocks
,
1247 /* If 'create', we might release the lock and reclaim it.
1248 * The lock must have been taken with interrupts enabled.
1249 * If !create, we don't release the lock.
1251 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1252 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1253 unsigned long pageoff
= (chunk
& PAGE_COUNTER_MASK
) << COUNTER_BYTE_SHIFT
;
1256 if (bitmap_checkpage(bitmap
, page
, create
) < 0) {
1257 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1258 *blocks
= csize
- (offset
& (csize
- 1));
1261 /* now locked ... */
1263 if (bitmap
->bp
[page
].hijacked
) { /* hijacked pointer */
1264 /* should we use the first or second counter field
1265 * of the hijacked pointer? */
1266 int hi
= (pageoff
> PAGE_COUNTER_MASK
);
1267 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
) +
1268 PAGE_COUNTER_SHIFT
- 1);
1269 *blocks
= csize
- (offset
& (csize
- 1));
1270 return &((bitmap_counter_t
*)
1271 &bitmap
->bp
[page
].map
)[hi
];
1272 } else { /* page is allocated */
1273 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1274 *blocks
= csize
- (offset
& (csize
- 1));
1275 return (bitmap_counter_t
*)
1276 &(bitmap
->bp
[page
].map
[pageoff
]);
1280 int bitmap_startwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
, int behind
)
1282 if (!bitmap
) return 0;
1285 atomic_inc(&bitmap
->behind_writes
);
1286 PRINTK(KERN_DEBUG
"inc write-behind count %d/%d\n",
1287 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1292 bitmap_counter_t
*bmc
;
1294 spin_lock_irq(&bitmap
->lock
);
1295 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 1);
1297 spin_unlock_irq(&bitmap
->lock
);
1303 bitmap_file_set_bit(bitmap
, offset
);
1304 bitmap_count_page(bitmap
,offset
, 1);
1305 blk_plug_device(bitmap
->mddev
->queue
);
1310 if ((*bmc
& COUNTER_MAX
) == COUNTER_MAX
) BUG();
1313 spin_unlock_irq(&bitmap
->lock
);
1316 if (sectors
> blocks
)
1323 void bitmap_endwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
,
1324 int success
, int behind
)
1326 if (!bitmap
) return;
1328 atomic_dec(&bitmap
->behind_writes
);
1329 PRINTK(KERN_DEBUG
"dec write-behind count %d/%d\n",
1330 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
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
);
1345 if (!success
&& ! (*bmc
& NEEDED_MASK
))
1346 *bmc
|= NEEDED_MASK
;
1350 set_page_attr(bitmap
,
1351 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1354 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1356 if (sectors
> blocks
)
1362 int bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
,
1365 bitmap_counter_t
*bmc
;
1367 if (bitmap
== NULL
) {/* FIXME or bitmap set as 'failed' */
1369 return 1; /* always resync if no bitmap */
1371 spin_lock_irq(&bitmap
->lock
);
1372 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1378 else if (NEEDED(*bmc
)) {
1380 if (!degraded
) { /* don't set/clear bits if degraded */
1381 *bmc
|= RESYNC_MASK
;
1382 *bmc
&= ~NEEDED_MASK
;
1386 spin_unlock_irq(&bitmap
->lock
);
1390 void bitmap_end_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
, int aborted
)
1392 bitmap_counter_t
*bmc
;
1393 unsigned long flags
;
1395 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1396 */ if (bitmap
== NULL
) {
1400 spin_lock_irqsave(&bitmap
->lock
, flags
);
1401 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1406 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1409 *bmc
&= ~RESYNC_MASK
;
1411 if (!NEEDED(*bmc
) && aborted
)
1412 *bmc
|= NEEDED_MASK
;
1415 set_page_attr(bitmap
,
1416 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1422 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1425 void bitmap_close_sync(struct bitmap
*bitmap
)
1427 /* Sync has finished, and any bitmap chunks that weren't synced
1428 * properly have been aborted. It remains to us to clear the
1429 * RESYNC bit wherever it is still on
1431 sector_t sector
= 0;
1433 if (!bitmap
) return;
1434 while (sector
< bitmap
->mddev
->resync_max_sectors
) {
1435 bitmap_end_sync(bitmap
, sector
, &blocks
, 0);
1437 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1438 (unsigned long long)sector, blocks);
1439 */ sector
+= blocks
;
1443 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
)
1445 /* For each chunk covered by any of these sectors, set the
1446 * counter to 1 and set resync_needed. They should all
1447 * be 0 at this point
1451 bitmap_counter_t
*bmc
;
1452 spin_lock_irq(&bitmap
->lock
);
1453 bmc
= bitmap_get_counter(bitmap
, offset
, &secs
, 1);
1455 spin_unlock_irq(&bitmap
->lock
);
1460 *bmc
= 1 | (needed
?NEEDED_MASK
:0);
1461 bitmap_count_page(bitmap
, offset
, 1);
1462 page
= filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
));
1463 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1465 spin_unlock_irq(&bitmap
->lock
);
1470 * flush out any pending updates
1472 void bitmap_flush(mddev_t
*mddev
)
1474 struct bitmap
*bitmap
= mddev
->bitmap
;
1477 if (!bitmap
) /* there was no bitmap */
1480 /* run the daemon_work three time to ensure everything is flushed
1483 sleep
= bitmap
->daemon_sleep
;
1484 bitmap
->daemon_sleep
= 0;
1485 bitmap_daemon_work(bitmap
);
1486 bitmap_daemon_work(bitmap
);
1487 bitmap_daemon_work(bitmap
);
1488 bitmap
->daemon_sleep
= sleep
;
1489 bitmap_update_sb(bitmap
);
1493 * free memory that was allocated
1495 static void bitmap_free(struct bitmap
*bitmap
)
1497 unsigned long k
, pages
;
1498 struct bitmap_page
*bp
;
1500 if (!bitmap
) /* there was no bitmap */
1503 /* release the bitmap file and kill the daemon */
1504 bitmap_file_put(bitmap
);
1507 pages
= bitmap
->pages
;
1509 /* free all allocated memory */
1511 mempool_destroy(bitmap
->write_pool
);
1513 if (bp
) /* deallocate the page memory */
1514 for (k
= 0; k
< pages
; k
++)
1515 if (bp
[k
].map
&& !bp
[k
].hijacked
)
1520 void bitmap_destroy(mddev_t
*mddev
)
1522 struct bitmap
*bitmap
= mddev
->bitmap
;
1524 if (!bitmap
) /* there was no bitmap */
1527 mddev
->bitmap
= NULL
; /* disconnect from the md device */
1529 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1531 bitmap_free(bitmap
);
1535 * initialize the bitmap structure
1536 * if this returns an error, bitmap_destroy must be called to do clean up
1538 int bitmap_create(mddev_t
*mddev
)
1540 struct bitmap
*bitmap
;
1541 unsigned long blocks
= mddev
->resync_max_sectors
;
1542 unsigned long chunks
;
1543 unsigned long pages
;
1544 struct file
*file
= mddev
->bitmap_file
;
1548 BUG_ON(sizeof(bitmap_super_t
) != 256);
1550 if (!file
&& !mddev
->bitmap_offset
) /* bitmap disabled, nothing to do */
1553 BUG_ON(file
&& mddev
->bitmap_offset
);
1555 bitmap
= kzalloc(sizeof(*bitmap
), GFP_KERNEL
);
1559 spin_lock_init(&bitmap
->lock
);
1560 bitmap
->mddev
= mddev
;
1562 spin_lock_init(&bitmap
->write_lock
);
1563 INIT_LIST_HEAD(&bitmap
->complete_pages
);
1564 init_waitqueue_head(&bitmap
->write_wait
);
1565 bitmap
->write_pool
= mempool_create(WRITE_POOL_SIZE
, write_pool_alloc
,
1566 write_pool_free
, NULL
);
1568 if (!bitmap
->write_pool
)
1571 bitmap
->file
= file
;
1572 bitmap
->offset
= mddev
->bitmap_offset
;
1573 if (file
) get_file(file
);
1574 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1575 err
= bitmap_read_sb(bitmap
);
1579 bitmap
->chunkshift
= find_first_bit(&bitmap
->chunksize
,
1580 sizeof(bitmap
->chunksize
));
1582 /* now that chunksize and chunkshift are set, we can use these macros */
1583 chunks
= (blocks
+ CHUNK_BLOCK_RATIO(bitmap
) - 1) /
1584 CHUNK_BLOCK_RATIO(bitmap
);
1585 pages
= (chunks
+ PAGE_COUNTER_RATIO
- 1) / PAGE_COUNTER_RATIO
;
1589 bitmap
->chunks
= chunks
;
1590 bitmap
->pages
= pages
;
1591 bitmap
->missing_pages
= pages
;
1592 bitmap
->counter_bits
= COUNTER_BITS
;
1594 bitmap
->syncchunk
= ~0UL;
1596 #ifdef INJECT_FATAL_FAULT_1
1599 bitmap
->bp
= kzalloc(pages
* sizeof(*bitmap
->bp
), GFP_KERNEL
);
1605 bitmap
->flags
|= BITMAP_ACTIVE
;
1607 /* now that we have some pages available, initialize the in-memory
1608 * bitmap from the on-disk bitmap */
1610 if (mddev
->degraded
== 0
1611 || bitmap
->events_cleared
== mddev
->events
)
1612 /* no need to keep dirty bits to optimise a re-add of a missing device */
1613 start
= mddev
->recovery_cp
;
1614 err
= bitmap_init_from_disk(bitmap
, start
);
1619 printk(KERN_INFO
"created bitmap (%lu pages) for device %s\n",
1620 pages
, bmname(bitmap
));
1622 mddev
->bitmap
= bitmap
;
1625 /* kick off the bitmap writeback daemon */
1626 bitmap
->writeback_daemon
=
1627 bitmap_start_daemon(bitmap
,
1628 bitmap_writeback_daemon
,
1631 if (IS_ERR(bitmap
->writeback_daemon
))
1632 return PTR_ERR(bitmap
->writeback_daemon
);
1633 mddev
->thread
->timeout
= bitmap
->daemon_sleep
* HZ
;
1635 return bitmap_update_sb(bitmap
);
1638 bitmap_free(bitmap
);
1642 /* the bitmap API -- for raid personalities */
1643 EXPORT_SYMBOL(bitmap_startwrite
);
1644 EXPORT_SYMBOL(bitmap_endwrite
);
1645 EXPORT_SYMBOL(bitmap_start_sync
);
1646 EXPORT_SYMBOL(bitmap_end_sync
);
1647 EXPORT_SYMBOL(bitmap_unplug
);
1648 EXPORT_SYMBOL(bitmap_close_sync
);
1649 EXPORT_SYMBOL(bitmap_daemon_work
);