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
94 * just a placeholder - calls kmalloc for bitmap pages
96 static unsigned char *bitmap_alloc_page(struct bitmap
*bitmap
)
100 #ifdef INJECT_FAULTS_1
103 page
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
106 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap
));
108 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
109 bmname(bitmap
), page
);
114 * for now just a placeholder -- just calls kfree for bitmap pages
116 static void bitmap_free_page(struct bitmap
*bitmap
, unsigned char *page
)
118 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap
), page
);
123 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
125 * 1) check to see if this page is allocated, if it's not then try to alloc
126 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
127 * page pointer directly as a counter
129 * if we find our page, we increment the page's refcount so that it stays
130 * allocated while we're using it
132 static int bitmap_checkpage(struct bitmap
*bitmap
, unsigned long page
, int create
)
134 unsigned char *mappage
;
136 if (page
>= bitmap
->pages
) {
138 "%s: invalid bitmap page request: %lu (> %lu)\n",
139 bmname(bitmap
), page
, bitmap
->pages
-1);
144 if (bitmap
->bp
[page
].hijacked
) /* it's hijacked, don't try to alloc */
147 if (bitmap
->bp
[page
].map
) /* page is already allocated, just return */
153 spin_unlock_irq(&bitmap
->lock
);
155 /* this page has not been allocated yet */
157 if ((mappage
= bitmap_alloc_page(bitmap
)) == NULL
) {
158 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
160 /* failed - set the hijacked flag so that we can use the
161 * pointer as a counter */
162 spin_lock_irq(&bitmap
->lock
);
163 if (!bitmap
->bp
[page
].map
)
164 bitmap
->bp
[page
].hijacked
= 1;
170 spin_lock_irq(&bitmap
->lock
);
172 /* recheck the page */
174 if (bitmap
->bp
[page
].map
|| bitmap
->bp
[page
].hijacked
) {
175 /* somebody beat us to getting the page */
176 bitmap_free_page(bitmap
, mappage
);
180 /* no page was in place and we have one, so install it */
182 memset(mappage
, 0, PAGE_SIZE
);
183 bitmap
->bp
[page
].map
= mappage
;
184 bitmap
->missing_pages
--;
190 /* if page is completely empty, put it back on the free list, or dealloc it */
191 /* if page was hijacked, unmark the flag so it might get alloced next time */
192 /* Note: lock should be held when calling this */
193 static void bitmap_checkfree(struct bitmap
*bitmap
, unsigned long page
)
197 if (bitmap
->bp
[page
].count
) /* page is still busy */
200 /* page is no longer in use, it can be released */
202 if (bitmap
->bp
[page
].hijacked
) { /* page was hijacked, undo this now */
203 bitmap
->bp
[page
].hijacked
= 0;
204 bitmap
->bp
[page
].map
= NULL
;
208 /* normal case, free the page */
211 /* actually ... let's not. We will probably need the page again exactly when
212 * memory is tight and we are flusing to disk
216 ptr
= bitmap
->bp
[page
].map
;
217 bitmap
->bp
[page
].map
= NULL
;
218 bitmap
->missing_pages
++;
219 bitmap_free_page(bitmap
, ptr
);
226 * bitmap file handling - read and write the bitmap file and its superblock
229 /* copy the pathname of a file to a buffer */
230 char *file_path(struct file
*file
, char *buf
, int count
)
241 buf
= d_path(d
, v
, buf
, count
);
243 return IS_ERR(buf
) ? NULL
: buf
;
247 * basic page I/O operations
250 /* IO operations when bitmap is stored near all superblocks */
251 static struct page
*read_sb_page(mddev_t
*mddev
, long offset
, unsigned long index
)
253 /* choose a good rdev and read the page from there */
256 struct list_head
*tmp
;
257 struct page
*page
= alloc_page(GFP_KERNEL
);
261 return ERR_PTR(-ENOMEM
);
263 ITERATE_RDEV(mddev
, rdev
, tmp
) {
264 if (! test_bit(In_sync
, &rdev
->flags
)
265 || test_bit(Faulty
, &rdev
->flags
))
268 target
= (rdev
->sb_offset
<< 1) + offset
+ index
* (PAGE_SIZE
/512);
270 if (sync_page_io(rdev
->bdev
, target
, PAGE_SIZE
, page
, READ
)) {
275 return ERR_PTR(-EIO
);
279 static int write_sb_page(mddev_t
*mddev
, long offset
, struct page
*page
, int wait
)
282 struct list_head
*tmp
;
284 ITERATE_RDEV(mddev
, rdev
, tmp
)
285 if (test_bit(In_sync
, &rdev
->flags
)
286 && !test_bit(Faulty
, &rdev
->flags
))
287 md_super_write(mddev
, rdev
,
288 (rdev
->sb_offset
<<1) + offset
289 + page
->index
* (PAGE_SIZE
/512),
294 md_super_wait(mddev
);
299 * write out a page to a file
301 static int write_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
305 if (bitmap
->file
== NULL
)
306 return write_sb_page(bitmap
->mddev
, bitmap
->offset
, page
, wait
);
308 flush_dcache_page(page
); /* make sure visible to anyone reading the file */
313 if (TestSetPageLocked(page
))
314 return -EAGAIN
; /* already locked */
315 if (PageWriteback(page
)) {
321 ret
= page
->mapping
->a_ops
->prepare_write(bitmap
->file
, page
, 0, PAGE_SIZE
);
323 ret
= page
->mapping
->a_ops
->commit_write(bitmap
->file
, page
, 0,
330 set_page_dirty(page
); /* force it to be written out */
333 /* add to list to be waited for by daemon */
334 struct page_list
*item
= mempool_alloc(bitmap
->write_pool
, GFP_NOIO
);
337 spin_lock(&bitmap
->write_lock
);
338 list_add(&item
->list
, &bitmap
->complete_pages
);
339 spin_unlock(&bitmap
->write_lock
);
340 md_wakeup_thread(bitmap
->writeback_daemon
);
342 return write_one_page(page
, wait
);
345 /* read a page from a file, pinning it into cache, and return bytes_read */
346 static struct page
*read_page(struct file
*file
, unsigned long index
,
347 unsigned long *bytes_read
)
349 struct inode
*inode
= file
->f_mapping
->host
;
350 struct page
*page
= NULL
;
351 loff_t isize
= i_size_read(inode
);
352 unsigned long end_index
= isize
>> PAGE_SHIFT
;
354 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE
,
355 (unsigned long long)index
<< PAGE_SHIFT
);
357 page
= read_cache_page(inode
->i_mapping
, index
,
358 (filler_t
*)inode
->i_mapping
->a_ops
->readpage
, file
);
361 wait_on_page_locked(page
);
362 if (!PageUptodate(page
) || PageError(page
)) {
364 page
= ERR_PTR(-EIO
);
368 if (index
> end_index
) /* we have read beyond EOF */
370 else if (index
== end_index
) /* possible short read */
371 *bytes_read
= isize
& ~PAGE_MASK
;
373 *bytes_read
= PAGE_SIZE
; /* got a full page */
376 printk(KERN_ALERT
"md: bitmap read error: (%dB @ %Lu): %ld\n",
378 (unsigned long long)index
<< PAGE_SHIFT
,
384 * bitmap file superblock operations
387 /* update the event counter and sync the superblock to disk */
388 int bitmap_update_sb(struct bitmap
*bitmap
)
393 if (!bitmap
|| !bitmap
->mddev
) /* no bitmap for this array */
395 spin_lock_irqsave(&bitmap
->lock
, flags
);
396 if (!bitmap
->sb_page
) { /* no superblock */
397 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
400 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
401 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
402 sb
->events
= cpu_to_le64(bitmap
->mddev
->events
);
403 if (!bitmap
->mddev
->degraded
)
404 sb
->events_cleared
= cpu_to_le64(bitmap
->mddev
->events
);
405 kunmap_atomic(sb
, KM_USER0
);
406 return write_page(bitmap
, bitmap
->sb_page
, 1);
409 /* print out the bitmap file superblock */
410 void bitmap_print_sb(struct bitmap
*bitmap
)
414 if (!bitmap
|| !bitmap
->sb_page
)
416 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
417 printk(KERN_DEBUG
"%s: bitmap file superblock:\n", bmname(bitmap
));
418 printk(KERN_DEBUG
" magic: %08x\n", le32_to_cpu(sb
->magic
));
419 printk(KERN_DEBUG
" version: %d\n", le32_to_cpu(sb
->version
));
420 printk(KERN_DEBUG
" uuid: %08x.%08x.%08x.%08x\n",
421 *(__u32
*)(sb
->uuid
+0),
422 *(__u32
*)(sb
->uuid
+4),
423 *(__u32
*)(sb
->uuid
+8),
424 *(__u32
*)(sb
->uuid
+12));
425 printk(KERN_DEBUG
" events: %llu\n",
426 (unsigned long long) le64_to_cpu(sb
->events
));
427 printk(KERN_DEBUG
"events cleared: %llu\n",
428 (unsigned long long) le64_to_cpu(sb
->events_cleared
));
429 printk(KERN_DEBUG
" state: %08x\n", le32_to_cpu(sb
->state
));
430 printk(KERN_DEBUG
" chunksize: %d B\n", le32_to_cpu(sb
->chunksize
));
431 printk(KERN_DEBUG
" daemon sleep: %ds\n", le32_to_cpu(sb
->daemon_sleep
));
432 printk(KERN_DEBUG
" sync size: %llu KB\n",
433 (unsigned long long)le64_to_cpu(sb
->sync_size
)/2);
434 printk(KERN_DEBUG
"max write behind: %d\n", le32_to_cpu(sb
->write_behind
));
435 kunmap_atomic(sb
, KM_USER0
);
438 /* read the superblock from the bitmap file and initialize some bitmap fields */
439 static int bitmap_read_sb(struct bitmap
*bitmap
)
443 unsigned long chunksize
, daemon_sleep
, write_behind
;
444 unsigned long bytes_read
;
445 unsigned long long events
;
448 /* page 0 is the superblock, read it... */
450 bitmap
->sb_page
= read_page(bitmap
->file
, 0, &bytes_read
);
452 bitmap
->sb_page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
, 0);
453 bytes_read
= PAGE_SIZE
;
455 if (IS_ERR(bitmap
->sb_page
)) {
456 err
= PTR_ERR(bitmap
->sb_page
);
457 bitmap
->sb_page
= NULL
;
461 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
463 if (bytes_read
< sizeof(*sb
)) { /* short read */
464 printk(KERN_INFO
"%s: bitmap file superblock truncated\n",
470 chunksize
= le32_to_cpu(sb
->chunksize
);
471 daemon_sleep
= le32_to_cpu(sb
->daemon_sleep
);
472 write_behind
= le32_to_cpu(sb
->write_behind
);
474 /* verify that the bitmap-specific fields are valid */
475 if (sb
->magic
!= cpu_to_le32(BITMAP_MAGIC
))
476 reason
= "bad magic";
477 else if (le32_to_cpu(sb
->version
) < BITMAP_MAJOR_LO
||
478 le32_to_cpu(sb
->version
) > BITMAP_MAJOR_HI
)
479 reason
= "unrecognized superblock version";
480 else if (chunksize
< PAGE_SIZE
)
481 reason
= "bitmap chunksize too small";
482 else if ((1 << ffz(~chunksize
)) != chunksize
)
483 reason
= "bitmap chunksize not a power of 2";
484 else if (daemon_sleep
< 1 || daemon_sleep
> MAX_SCHEDULE_TIMEOUT
/ HZ
)
485 reason
= "daemon sleep period out of range";
486 else if (write_behind
> COUNTER_MAX
)
487 reason
= "write-behind limit out of range (0 - 16383)";
489 printk(KERN_INFO
"%s: invalid bitmap file superblock: %s\n",
490 bmname(bitmap
), reason
);
494 /* keep the array size field of the bitmap superblock up to date */
495 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
497 if (!bitmap
->mddev
->persistent
)
501 * if we have a persistent array superblock, compare the
502 * bitmap's UUID and event counter to the mddev's
504 if (memcmp(sb
->uuid
, bitmap
->mddev
->uuid
, 16)) {
505 printk(KERN_INFO
"%s: bitmap superblock UUID mismatch\n",
509 events
= le64_to_cpu(sb
->events
);
510 if (events
< bitmap
->mddev
->events
) {
511 printk(KERN_INFO
"%s: bitmap file is out of date (%llu < %llu) "
512 "-- forcing full recovery\n", bmname(bitmap
), events
,
513 (unsigned long long) bitmap
->mddev
->events
);
514 sb
->state
|= BITMAP_STALE
;
517 /* assign fields using values from superblock */
518 bitmap
->chunksize
= chunksize
;
519 bitmap
->daemon_sleep
= daemon_sleep
;
520 bitmap
->daemon_lastrun
= jiffies
;
521 bitmap
->max_write_behind
= write_behind
;
522 bitmap
->flags
|= sb
->state
;
523 if (le32_to_cpu(sb
->version
) == BITMAP_MAJOR_HOSTENDIAN
)
524 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
525 bitmap
->events_cleared
= le64_to_cpu(sb
->events_cleared
);
526 if (sb
->state
& BITMAP_STALE
)
527 bitmap
->events_cleared
= bitmap
->mddev
->events
;
530 kunmap_atomic(sb
, KM_USER0
);
532 bitmap_print_sb(bitmap
);
536 enum bitmap_mask_op
{
541 /* record the state of the bitmap in the superblock */
542 static void bitmap_mask_state(struct bitmap
*bitmap
, enum bitmap_state bits
,
543 enum bitmap_mask_op op
)
548 spin_lock_irqsave(&bitmap
->lock
, flags
);
549 if (!bitmap
->sb_page
) { /* can't set the state */
550 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
553 get_page(bitmap
->sb_page
);
554 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
555 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
557 case MASK_SET
: sb
->state
|= bits
;
559 case MASK_UNSET
: sb
->state
&= ~bits
;
563 kunmap_atomic(sb
, KM_USER0
);
564 put_page(bitmap
->sb_page
);
568 * general bitmap file operations
571 /* calculate the index of the page that contains this bit */
572 static inline unsigned long file_page_index(unsigned long chunk
)
574 return CHUNK_BIT_OFFSET(chunk
) >> PAGE_BIT_SHIFT
;
577 /* calculate the (bit) offset of this bit within a page */
578 static inline unsigned long file_page_offset(unsigned long chunk
)
580 return CHUNK_BIT_OFFSET(chunk
) & (PAGE_BITS
- 1);
584 * return a pointer to the page in the filemap that contains the given bit
586 * this lookup is complicated by the fact that the bitmap sb might be exactly
587 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
590 static inline struct page
*filemap_get_page(struct bitmap
*bitmap
,
593 return bitmap
->filemap
[file_page_index(chunk
) - file_page_index(0)];
597 static void bitmap_file_unmap(struct bitmap
*bitmap
)
599 struct page
**map
, *sb_page
;
604 spin_lock_irqsave(&bitmap
->lock
, flags
);
605 map
= bitmap
->filemap
;
606 bitmap
->filemap
= NULL
;
607 attr
= bitmap
->filemap_attr
;
608 bitmap
->filemap_attr
= NULL
;
609 pages
= bitmap
->file_pages
;
610 bitmap
->file_pages
= 0;
611 sb_page
= bitmap
->sb_page
;
612 bitmap
->sb_page
= NULL
;
613 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
616 if (map
[pages
]->index
!= 0) /* 0 is sb_page, release it below */
617 put_page(map
[pages
]);
621 safe_put_page(sb_page
);
624 static void bitmap_stop_daemon(struct bitmap
*bitmap
);
626 /* dequeue the next item in a page list -- don't call from irq context */
627 static struct page_list
*dequeue_page(struct bitmap
*bitmap
)
629 struct page_list
*item
= NULL
;
630 struct list_head
*head
= &bitmap
->complete_pages
;
632 spin_lock(&bitmap
->write_lock
);
633 if (list_empty(head
))
635 item
= list_entry(head
->prev
, struct page_list
, list
);
636 list_del(head
->prev
);
638 spin_unlock(&bitmap
->write_lock
);
642 static void drain_write_queues(struct bitmap
*bitmap
)
644 struct page_list
*item
;
646 while ((item
= dequeue_page(bitmap
))) {
647 /* don't bother to wait */
648 put_page(item
->page
);
649 mempool_free(item
, bitmap
->write_pool
);
652 wake_up(&bitmap
->write_wait
);
655 static void bitmap_file_put(struct bitmap
*bitmap
)
661 spin_lock_irqsave(&bitmap
->lock
, flags
);
664 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
666 bitmap_stop_daemon(bitmap
);
668 drain_write_queues(bitmap
);
670 bitmap_file_unmap(bitmap
);
673 inode
= file
->f_mapping
->host
;
674 spin_lock(&inode
->i_lock
);
675 atomic_set(&inode
->i_writecount
, 1); /* allow writes again */
676 spin_unlock(&inode
->i_lock
);
683 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
684 * then it is no longer reliable, so we stop using it and we mark the file
685 * as failed in the superblock
687 static void bitmap_file_kick(struct bitmap
*bitmap
)
689 char *path
, *ptr
= NULL
;
691 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_SET
);
692 bitmap_update_sb(bitmap
);
695 path
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
697 ptr
= file_path(bitmap
->file
, path
, PAGE_SIZE
);
699 printk(KERN_ALERT
"%s: kicking failed bitmap file %s from array!\n",
700 bmname(bitmap
), ptr
? ptr
: "");
705 bitmap_file_put(bitmap
);
710 enum bitmap_page_attr
{
711 BITMAP_PAGE_DIRTY
= 1, // there are set bits that need to be synced
712 BITMAP_PAGE_CLEAN
= 2, // there are bits that might need to be cleared
713 BITMAP_PAGE_NEEDWRITE
=4, // there are cleared bits that need to be synced
716 static inline void set_page_attr(struct bitmap
*bitmap
, struct page
*page
,
717 enum bitmap_page_attr attr
)
719 bitmap
->filemap_attr
[page
->index
] |= attr
;
722 static inline void clear_page_attr(struct bitmap
*bitmap
, struct page
*page
,
723 enum bitmap_page_attr attr
)
725 bitmap
->filemap_attr
[page
->index
] &= ~attr
;
728 static inline unsigned long get_page_attr(struct bitmap
*bitmap
, struct page
*page
)
730 return bitmap
->filemap_attr
[page
->index
];
734 * bitmap_file_set_bit -- called before performing a write to the md device
735 * to set (and eventually sync) a particular bit in the bitmap file
737 * we set the bit immediately, then we record the page number so that
738 * when an unplug occurs, we can flush the dirty pages out to disk
740 static void bitmap_file_set_bit(struct bitmap
*bitmap
, sector_t block
)
745 unsigned long chunk
= block
>> CHUNK_BLOCK_SHIFT(bitmap
);
747 if (!bitmap
->filemap
) {
751 page
= filemap_get_page(bitmap
, chunk
);
752 bit
= file_page_offset(chunk
);
755 /* make sure the page stays cached until it gets written out */
756 if (! (get_page_attr(bitmap
, page
) & BITMAP_PAGE_DIRTY
))
760 kaddr
= kmap_atomic(page
, KM_USER0
);
761 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
764 ext2_set_bit(bit
, kaddr
);
765 kunmap_atomic(kaddr
, KM_USER0
);
766 PRINTK("set file bit %lu page %lu\n", bit
, page
->index
);
768 /* record page number so it gets flushed to disk when unplug occurs */
769 set_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
773 /* this gets called when the md device is ready to unplug its underlying
774 * (slave) device queues -- before we let any writes go down, we need to
775 * sync the dirty pages of the bitmap file to disk */
776 int bitmap_unplug(struct bitmap
*bitmap
)
778 unsigned long i
, attr
, flags
;
786 /* look at each page to see if there are any set bits that need to be
787 * flushed out to disk */
788 for (i
= 0; i
< bitmap
->file_pages
; i
++) {
789 spin_lock_irqsave(&bitmap
->lock
, flags
);
790 if (!bitmap
->filemap
) {
791 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
794 page
= bitmap
->filemap
[i
];
795 attr
= get_page_attr(bitmap
, page
);
796 clear_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
797 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
798 if ((attr
& BITMAP_PAGE_DIRTY
))
800 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
802 if (attr
& (BITMAP_PAGE_DIRTY
| BITMAP_PAGE_NEEDWRITE
)) {
803 err
= write_page(bitmap
, page
, 0);
804 if (err
== -EAGAIN
) {
805 if (attr
& BITMAP_PAGE_DIRTY
)
806 err
= write_page(bitmap
, page
, 1);
814 if (wait
) { /* if any writes were performed, we need to wait on them */
816 spin_lock_irq(&bitmap
->write_lock
);
817 wait_event_lock_irq(bitmap
->write_wait
,
818 list_empty(&bitmap
->complete_pages
), bitmap
->write_lock
,
819 wake_up_process(bitmap
->writeback_daemon
->tsk
));
820 spin_unlock_irq(&bitmap
->write_lock
);
822 md_super_wait(bitmap
->mddev
);
827 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
);
828 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
829 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
830 * memory mapping of the bitmap file
832 * if there's no bitmap file, or if the bitmap file had been
833 * previously kicked from the array, we mark all the bits as
834 * 1's in order to cause a full resync.
836 * We ignore all bits for sectors that end earlier than 'start'.
837 * This is used when reading an out-of-date bitmap...
839 static int bitmap_init_from_disk(struct bitmap
*bitmap
, sector_t start
)
841 unsigned long i
, chunks
, index
, oldindex
, bit
;
842 struct page
*page
= NULL
, *oldpage
= NULL
;
843 unsigned long num_pages
, bit_cnt
= 0;
845 unsigned long bytes
, offset
, dummy
;
850 chunks
= bitmap
->chunks
;
853 BUG_ON(!file
&& !bitmap
->offset
);
855 #ifdef INJECT_FAULTS_3
858 outofdate
= bitmap
->flags
& BITMAP_STALE
;
861 printk(KERN_INFO
"%s: bitmap file is out of date, doing full "
862 "recovery\n", bmname(bitmap
));
864 bytes
= (chunks
+ 7) / 8;
866 num_pages
= (bytes
+ sizeof(bitmap_super_t
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
868 if (file
&& i_size_read(file
->f_mapping
->host
) < bytes
+ sizeof(bitmap_super_t
)) {
869 printk(KERN_INFO
"%s: bitmap file too short %lu < %lu\n",
871 (unsigned long) i_size_read(file
->f_mapping
->host
),
872 bytes
+ sizeof(bitmap_super_t
));
878 bitmap
->filemap
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
879 if (!bitmap
->filemap
)
882 bitmap
->filemap_attr
= kzalloc(sizeof(long) * num_pages
, GFP_KERNEL
);
883 if (!bitmap
->filemap_attr
)
888 for (i
= 0; i
< chunks
; i
++) {
890 index
= file_page_index(i
);
891 bit
= file_page_offset(i
);
892 if (index
!= oldindex
) { /* this is a new page, read it in */
893 /* unmap the old page, we're done with it */
896 * if we're here then the superblock page
897 * contains some bits (PAGE_SIZE != sizeof sb)
898 * we've already read it in, so just use it
900 page
= bitmap
->sb_page
;
901 offset
= sizeof(bitmap_super_t
);
903 page
= read_page(file
, index
, &dummy
);
906 page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
, index
);
909 if (IS_ERR(page
)) { /* read error */
919 * if bitmap is out of date, dirty the
920 * whole page and write it out
922 paddr
= kmap_atomic(page
, KM_USER0
);
923 memset(paddr
+ offset
, 0xff,
925 kunmap_atomic(paddr
, KM_USER0
);
926 ret
= write_page(bitmap
, page
, 1);
928 /* release, page not in filemap yet */
934 bitmap
->filemap
[bitmap
->file_pages
++] = page
;
936 paddr
= kmap_atomic(page
, KM_USER0
);
937 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
938 b
= test_bit(bit
, paddr
);
940 b
= ext2_test_bit(bit
, paddr
);
941 kunmap_atomic(paddr
, KM_USER0
);
943 /* if the disk bit is set, set the memory bit */
944 bitmap_set_memory_bits(bitmap
, i
<< CHUNK_BLOCK_SHIFT(bitmap
),
945 ((i
+1) << (CHUNK_BLOCK_SHIFT(bitmap
)) >= start
)
948 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
952 /* everything went OK */
954 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_UNSET
);
956 if (bit_cnt
) { /* Kick recovery if any bits were set */
957 set_bit(MD_RECOVERY_NEEDED
, &bitmap
->mddev
->recovery
);
958 md_wakeup_thread(bitmap
->mddev
->thread
);
962 printk(KERN_INFO
"%s: bitmap initialized from disk: "
963 "read %lu/%lu pages, set %lu bits, status: %d\n",
964 bmname(bitmap
), bitmap
->file_pages
, num_pages
, bit_cnt
, ret
);
969 void bitmap_write_all(struct bitmap
*bitmap
)
971 /* We don't actually write all bitmap blocks here,
972 * just flag them as needing to be written
975 unsigned long chunks
= bitmap
->chunks
;
976 unsigned long bytes
= (chunks
+7)/8 + sizeof(bitmap_super_t
);
977 unsigned long num_pages
= (bytes
+ PAGE_SIZE
-1) / PAGE_SIZE
;
979 bitmap
->filemap_attr
[num_pages
] |= BITMAP_PAGE_NEEDWRITE
;
983 static void bitmap_count_page(struct bitmap
*bitmap
, sector_t offset
, int inc
)
985 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
986 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
987 bitmap
->bp
[page
].count
+= inc
;
989 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
990 (unsigned long long)offset, inc, bitmap->bp[page].count);
992 bitmap_checkfree(bitmap
, page
);
994 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
995 sector_t offset
, int *blocks
,
999 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1003 int bitmap_daemon_work(struct bitmap
*bitmap
)
1006 unsigned long flags
;
1007 struct page
*page
= NULL
, *lastpage
= NULL
;
1015 if (time_before(jiffies
, bitmap
->daemon_lastrun
+ bitmap
->daemon_sleep
*HZ
))
1017 bitmap
->daemon_lastrun
= jiffies
;
1019 for (j
= 0; j
< bitmap
->chunks
; j
++) {
1020 bitmap_counter_t
*bmc
;
1021 spin_lock_irqsave(&bitmap
->lock
, flags
);
1022 if (!bitmap
->filemap
) {
1023 /* error or shutdown */
1024 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1028 page
= filemap_get_page(bitmap
, j
);
1030 if (page
!= lastpage
) {
1031 /* skip this page unless it's marked as needing cleaning */
1032 if (!((attr
=get_page_attr(bitmap
, page
)) & BITMAP_PAGE_CLEAN
)) {
1033 if (attr
& BITMAP_PAGE_NEEDWRITE
) {
1035 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1037 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1038 if (attr
& BITMAP_PAGE_NEEDWRITE
) {
1039 switch (write_page(bitmap
, page
, 0)) {
1041 set_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1046 bitmap_file_kick(bitmap
);
1053 /* grab the new page, sync and release the old */
1055 if (lastpage
!= NULL
) {
1056 if (get_page_attr(bitmap
, lastpage
) & BITMAP_PAGE_NEEDWRITE
) {
1057 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1058 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1059 err
= write_page(bitmap
, lastpage
, 0);
1060 if (err
== -EAGAIN
) {
1062 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1065 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1066 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1070 bitmap_file_kick(bitmap
);
1072 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1075 printk("bitmap clean at page %lu\n", j);
1077 spin_lock_irqsave(&bitmap
->lock
, flags
);
1078 clear_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1080 bmc
= bitmap_get_counter(bitmap
, j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1084 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1087 *bmc
=1; /* maybe clear the bit next time */
1088 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1089 } else if (*bmc
== 1) {
1090 /* we can clear the bit */
1092 bitmap_count_page(bitmap
, j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1096 paddr
= kmap_atomic(page
, KM_USER0
);
1097 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1098 clear_bit(file_page_offset(j
), paddr
);
1100 ext2_clear_bit(file_page_offset(j
), paddr
);
1101 kunmap_atomic(paddr
, KM_USER0
);
1104 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1107 /* now sync the final page */
1108 if (lastpage
!= NULL
) {
1109 spin_lock_irqsave(&bitmap
->lock
, flags
);
1110 if (get_page_attr(bitmap
, lastpage
) &BITMAP_PAGE_NEEDWRITE
) {
1111 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1112 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1113 err
= write_page(bitmap
, lastpage
, 0);
1114 if (err
== -EAGAIN
) {
1115 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1119 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1120 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1129 static void daemon_exit(struct bitmap
*bitmap
, mdk_thread_t
**daemon
)
1132 unsigned long flags
;
1134 /* if no one is waiting on us, we'll free the md thread struct
1135 * and exit, otherwise we let the waiter clean things up */
1136 spin_lock_irqsave(&bitmap
->lock
, flags
);
1137 if ((dmn
= *daemon
)) { /* no one is waiting, cleanup and exit */
1139 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1141 complete_and_exit(NULL
, 0); /* do_exit not exported */
1143 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1146 static void bitmap_writeback_daemon(mddev_t
*mddev
)
1148 struct bitmap
*bitmap
= mddev
->bitmap
;
1150 struct page_list
*item
;
1153 if (signal_pending(current
)) {
1155 "%s: bitmap writeback daemon got signal, exiting...\n",
1161 /* about to be stopped. */
1164 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap
));
1165 /* wait on bitmap page writebacks */
1166 while ((item
= dequeue_page(bitmap
))) {
1168 mempool_free(item
, bitmap
->write_pool
);
1169 PRINTK("wait on page writeback: %p\n", page
);
1170 wait_on_page_writeback(page
);
1171 PRINTK("finished page writeback: %p\n", page
);
1173 err
= PageError(page
);
1176 printk(KERN_WARNING
"%s: bitmap file writeback "
1177 "failed (page %lu): %d\n",
1178 bmname(bitmap
), page
->index
, err
);
1179 bitmap_file_kick(bitmap
);
1184 wake_up(&bitmap
->write_wait
);
1186 printk(KERN_INFO
"%s: bitmap writeback daemon exiting (%d)\n",
1187 bmname(bitmap
), err
);
1188 daemon_exit(bitmap
, &bitmap
->writeback_daemon
);
1192 static mdk_thread_t
*bitmap_start_daemon(struct bitmap
*bitmap
,
1193 void (*func
)(mddev_t
*), char *name
)
1195 mdk_thread_t
*daemon
;
1198 #ifdef INJECT_FATAL_FAULT_2
1201 sprintf(namebuf
, "%%s_%s", name
);
1202 daemon
= md_register_thread(func
, bitmap
->mddev
, namebuf
);
1205 printk(KERN_ERR
"%s: failed to start bitmap daemon\n",
1207 return ERR_PTR(-ECHILD
);
1210 md_wakeup_thread(daemon
); /* start it running */
1212 PRINTK("%s: %s daemon (pid %d) started...\n",
1213 bmname(bitmap
), name
, daemon
->tsk
->pid
);
1218 static void bitmap_stop_daemon(struct bitmap
*bitmap
)
1220 /* the daemon can't stop itself... it'll just exit instead... */
1221 if (bitmap
->writeback_daemon
&& ! IS_ERR(bitmap
->writeback_daemon
) &&
1222 current
->pid
!= bitmap
->writeback_daemon
->tsk
->pid
) {
1223 mdk_thread_t
*daemon
;
1224 unsigned long flags
;
1226 spin_lock_irqsave(&bitmap
->lock
, flags
);
1227 daemon
= bitmap
->writeback_daemon
;
1228 bitmap
->writeback_daemon
= NULL
;
1229 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1230 if (daemon
&& ! IS_ERR(daemon
))
1231 md_unregister_thread(daemon
); /* destroy the thread */
1235 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1236 sector_t offset
, int *blocks
,
1239 /* If 'create', we might release the lock and reclaim it.
1240 * The lock must have been taken with interrupts enabled.
1241 * If !create, we don't release the lock.
1243 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1244 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1245 unsigned long pageoff
= (chunk
& PAGE_COUNTER_MASK
) << COUNTER_BYTE_SHIFT
;
1248 if (bitmap_checkpage(bitmap
, page
, create
) < 0) {
1249 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1250 *blocks
= csize
- (offset
& (csize
- 1));
1253 /* now locked ... */
1255 if (bitmap
->bp
[page
].hijacked
) { /* hijacked pointer */
1256 /* should we use the first or second counter field
1257 * of the hijacked pointer? */
1258 int hi
= (pageoff
> PAGE_COUNTER_MASK
);
1259 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
) +
1260 PAGE_COUNTER_SHIFT
- 1);
1261 *blocks
= csize
- (offset
& (csize
- 1));
1262 return &((bitmap_counter_t
*)
1263 &bitmap
->bp
[page
].map
)[hi
];
1264 } else { /* page is allocated */
1265 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1266 *blocks
= csize
- (offset
& (csize
- 1));
1267 return (bitmap_counter_t
*)
1268 &(bitmap
->bp
[page
].map
[pageoff
]);
1272 int bitmap_startwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
, int behind
)
1274 if (!bitmap
) return 0;
1277 atomic_inc(&bitmap
->behind_writes
);
1278 PRINTK(KERN_DEBUG
"inc write-behind count %d/%d\n",
1279 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1284 bitmap_counter_t
*bmc
;
1286 spin_lock_irq(&bitmap
->lock
);
1287 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 1);
1289 spin_unlock_irq(&bitmap
->lock
);
1295 bitmap_file_set_bit(bitmap
, offset
);
1296 bitmap_count_page(bitmap
,offset
, 1);
1297 blk_plug_device(bitmap
->mddev
->queue
);
1302 BUG_ON((*bmc
& COUNTER_MAX
) == COUNTER_MAX
);
1305 spin_unlock_irq(&bitmap
->lock
);
1308 if (sectors
> blocks
)
1315 void bitmap_endwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
,
1316 int success
, int behind
)
1318 if (!bitmap
) return;
1320 atomic_dec(&bitmap
->behind_writes
);
1321 PRINTK(KERN_DEBUG
"dec write-behind count %d/%d\n",
1322 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1327 unsigned long flags
;
1328 bitmap_counter_t
*bmc
;
1330 spin_lock_irqsave(&bitmap
->lock
, flags
);
1331 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 0);
1333 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1337 if (!success
&& ! (*bmc
& NEEDED_MASK
))
1338 *bmc
|= NEEDED_MASK
;
1342 set_page_attr(bitmap
,
1343 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1346 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1348 if (sectors
> blocks
)
1354 int bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
,
1357 bitmap_counter_t
*bmc
;
1359 if (bitmap
== NULL
) {/* FIXME or bitmap set as 'failed' */
1361 return 1; /* always resync if no bitmap */
1363 spin_lock_irq(&bitmap
->lock
);
1364 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1370 else if (NEEDED(*bmc
)) {
1372 if (!degraded
) { /* don't set/clear bits if degraded */
1373 *bmc
|= RESYNC_MASK
;
1374 *bmc
&= ~NEEDED_MASK
;
1378 spin_unlock_irq(&bitmap
->lock
);
1382 void bitmap_end_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
, int aborted
)
1384 bitmap_counter_t
*bmc
;
1385 unsigned long flags
;
1387 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1388 */ if (bitmap
== NULL
) {
1392 spin_lock_irqsave(&bitmap
->lock
, flags
);
1393 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1398 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1401 *bmc
&= ~RESYNC_MASK
;
1403 if (!NEEDED(*bmc
) && aborted
)
1404 *bmc
|= NEEDED_MASK
;
1407 set_page_attr(bitmap
,
1408 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1414 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1417 void bitmap_close_sync(struct bitmap
*bitmap
)
1419 /* Sync has finished, and any bitmap chunks that weren't synced
1420 * properly have been aborted. It remains to us to clear the
1421 * RESYNC bit wherever it is still on
1423 sector_t sector
= 0;
1425 if (!bitmap
) return;
1426 while (sector
< bitmap
->mddev
->resync_max_sectors
) {
1427 bitmap_end_sync(bitmap
, sector
, &blocks
, 0);
1429 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1430 (unsigned long long)sector, blocks);
1431 */ sector
+= blocks
;
1435 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
)
1437 /* For each chunk covered by any of these sectors, set the
1438 * counter to 1 and set resync_needed. They should all
1439 * be 0 at this point
1443 bitmap_counter_t
*bmc
;
1444 spin_lock_irq(&bitmap
->lock
);
1445 bmc
= bitmap_get_counter(bitmap
, offset
, &secs
, 1);
1447 spin_unlock_irq(&bitmap
->lock
);
1452 *bmc
= 1 | (needed
?NEEDED_MASK
:0);
1453 bitmap_count_page(bitmap
, offset
, 1);
1454 page
= filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
));
1455 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1457 spin_unlock_irq(&bitmap
->lock
);
1462 * flush out any pending updates
1464 void bitmap_flush(mddev_t
*mddev
)
1466 struct bitmap
*bitmap
= mddev
->bitmap
;
1469 if (!bitmap
) /* there was no bitmap */
1472 /* run the daemon_work three time to ensure everything is flushed
1475 sleep
= bitmap
->daemon_sleep
;
1476 bitmap
->daemon_sleep
= 0;
1477 bitmap_daemon_work(bitmap
);
1478 bitmap_daemon_work(bitmap
);
1479 bitmap_daemon_work(bitmap
);
1480 bitmap
->daemon_sleep
= sleep
;
1481 bitmap_update_sb(bitmap
);
1485 * free memory that was allocated
1487 static void bitmap_free(struct bitmap
*bitmap
)
1489 unsigned long k
, pages
;
1490 struct bitmap_page
*bp
;
1492 if (!bitmap
) /* there was no bitmap */
1495 /* release the bitmap file and kill the daemon */
1496 bitmap_file_put(bitmap
);
1499 pages
= bitmap
->pages
;
1501 /* free all allocated memory */
1503 mempool_destroy(bitmap
->write_pool
);
1505 if (bp
) /* deallocate the page memory */
1506 for (k
= 0; k
< pages
; k
++)
1507 if (bp
[k
].map
&& !bp
[k
].hijacked
)
1512 void bitmap_destroy(mddev_t
*mddev
)
1514 struct bitmap
*bitmap
= mddev
->bitmap
;
1516 if (!bitmap
) /* there was no bitmap */
1519 mddev
->bitmap
= NULL
; /* disconnect from the md device */
1521 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1523 bitmap_free(bitmap
);
1527 * initialize the bitmap structure
1528 * if this returns an error, bitmap_destroy must be called to do clean up
1530 int bitmap_create(mddev_t
*mddev
)
1532 struct bitmap
*bitmap
;
1533 unsigned long blocks
= mddev
->resync_max_sectors
;
1534 unsigned long chunks
;
1535 unsigned long pages
;
1536 struct file
*file
= mddev
->bitmap_file
;
1540 BUG_ON(sizeof(bitmap_super_t
) != 256);
1542 if (!file
&& !mddev
->bitmap_offset
) /* bitmap disabled, nothing to do */
1545 BUG_ON(file
&& mddev
->bitmap_offset
);
1547 bitmap
= kzalloc(sizeof(*bitmap
), GFP_KERNEL
);
1551 spin_lock_init(&bitmap
->lock
);
1552 bitmap
->mddev
= mddev
;
1554 spin_lock_init(&bitmap
->write_lock
);
1555 INIT_LIST_HEAD(&bitmap
->complete_pages
);
1556 init_waitqueue_head(&bitmap
->write_wait
);
1557 bitmap
->write_pool
= mempool_create_kmalloc_pool(WRITE_POOL_SIZE
,
1558 sizeof(struct page_list
));
1560 if (!bitmap
->write_pool
)
1563 bitmap
->file
= file
;
1564 bitmap
->offset
= mddev
->bitmap_offset
;
1565 if (file
) get_file(file
);
1566 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1567 err
= bitmap_read_sb(bitmap
);
1571 bitmap
->chunkshift
= find_first_bit(&bitmap
->chunksize
,
1572 sizeof(bitmap
->chunksize
));
1574 /* now that chunksize and chunkshift are set, we can use these macros */
1575 chunks
= (blocks
+ CHUNK_BLOCK_RATIO(bitmap
) - 1) /
1576 CHUNK_BLOCK_RATIO(bitmap
);
1577 pages
= (chunks
+ PAGE_COUNTER_RATIO
- 1) / PAGE_COUNTER_RATIO
;
1581 bitmap
->chunks
= chunks
;
1582 bitmap
->pages
= pages
;
1583 bitmap
->missing_pages
= pages
;
1584 bitmap
->counter_bits
= COUNTER_BITS
;
1586 bitmap
->syncchunk
= ~0UL;
1588 #ifdef INJECT_FATAL_FAULT_1
1591 bitmap
->bp
= kzalloc(pages
* sizeof(*bitmap
->bp
), GFP_KERNEL
);
1597 bitmap
->flags
|= BITMAP_ACTIVE
;
1599 /* now that we have some pages available, initialize the in-memory
1600 * bitmap from the on-disk bitmap */
1602 if (mddev
->degraded
== 0
1603 || bitmap
->events_cleared
== mddev
->events
)
1604 /* no need to keep dirty bits to optimise a re-add of a missing device */
1605 start
= mddev
->recovery_cp
;
1606 err
= bitmap_init_from_disk(bitmap
, start
);
1611 printk(KERN_INFO
"created bitmap (%lu pages) for device %s\n",
1612 pages
, bmname(bitmap
));
1614 mddev
->bitmap
= bitmap
;
1617 /* kick off the bitmap writeback daemon */
1618 bitmap
->writeback_daemon
=
1619 bitmap_start_daemon(bitmap
,
1620 bitmap_writeback_daemon
,
1623 if (IS_ERR(bitmap
->writeback_daemon
))
1624 return PTR_ERR(bitmap
->writeback_daemon
);
1625 mddev
->thread
->timeout
= bitmap
->daemon_sleep
* HZ
;
1627 return bitmap_update_sb(bitmap
);
1630 bitmap_free(bitmap
);
1634 /* the bitmap API -- for raid personalities */
1635 EXPORT_SYMBOL(bitmap_startwrite
);
1636 EXPORT_SYMBOL(bitmap_endwrite
);
1637 EXPORT_SYMBOL(bitmap_start_sync
);
1638 EXPORT_SYMBOL(bitmap_end_sync
);
1639 EXPORT_SYMBOL(bitmap_unplug
);
1640 EXPORT_SYMBOL(bitmap_close_sync
);
1641 EXPORT_SYMBOL(bitmap_daemon_work
);