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 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
);
318 flush_dcache_page(page
); /* make sure visible to anyone reading the file */
323 if (TestSetPageLocked(page
))
324 return -EAGAIN
; /* already locked */
325 if (PageWriteback(page
)) {
331 ret
= page
->mapping
->a_ops
->prepare_write(bitmap
->file
, page
, 0, PAGE_SIZE
);
333 ret
= page
->mapping
->a_ops
->commit_write(bitmap
->file
, page
, 0,
340 set_page_dirty(page
); /* force it to be written out */
343 /* add to list to be waited for by daemon */
344 struct page_list
*item
= mempool_alloc(bitmap
->write_pool
, GFP_NOIO
);
346 spin_lock(&bitmap
->write_lock
);
347 list_add(&item
->list
, &bitmap
->complete_pages
);
348 spin_unlock(&bitmap
->write_lock
);
349 md_wakeup_thread(bitmap
->writeback_daemon
);
351 return write_one_page(page
, wait
);
354 /* read a page from a file, pinning it into cache, and return bytes_read */
355 static struct page
*read_page(struct file
*file
, unsigned long index
,
356 unsigned long *bytes_read
)
358 struct inode
*inode
= file
->f_mapping
->host
;
359 struct page
*page
= NULL
;
360 loff_t isize
= i_size_read(inode
);
361 unsigned long end_index
= isize
>> PAGE_SHIFT
;
363 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE
,
364 (unsigned long long)index
<< PAGE_SHIFT
);
366 page
= read_cache_page(inode
->i_mapping
, index
,
367 (filler_t
*)inode
->i_mapping
->a_ops
->readpage
, file
);
370 wait_on_page_locked(page
);
371 if (!PageUptodate(page
) || PageError(page
)) {
373 page
= ERR_PTR(-EIO
);
377 if (index
> end_index
) /* we have read beyond EOF */
379 else if (index
== end_index
) /* possible short read */
380 *bytes_read
= isize
& ~PAGE_MASK
;
382 *bytes_read
= PAGE_SIZE
; /* got a full page */
385 printk(KERN_ALERT
"md: bitmap read error: (%dB @ %Lu): %ld\n",
387 (unsigned long long)index
<< PAGE_SHIFT
,
393 * bitmap file superblock operations
396 /* update the event counter and sync the superblock to disk */
397 int bitmap_update_sb(struct bitmap
*bitmap
)
402 if (!bitmap
|| !bitmap
->mddev
) /* no bitmap for this array */
404 spin_lock_irqsave(&bitmap
->lock
, flags
);
405 if (!bitmap
->sb_page
) { /* no superblock */
406 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
409 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
410 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
411 sb
->events
= cpu_to_le64(bitmap
->mddev
->events
);
412 if (!bitmap
->mddev
->degraded
)
413 sb
->events_cleared
= cpu_to_le64(bitmap
->mddev
->events
);
414 kunmap_atomic(sb
, KM_USER0
);
415 return write_page(bitmap
, bitmap
->sb_page
, 1);
418 /* print out the bitmap file superblock */
419 void bitmap_print_sb(struct bitmap
*bitmap
)
423 if (!bitmap
|| !bitmap
->sb_page
)
425 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
426 printk(KERN_DEBUG
"%s: bitmap file superblock:\n", bmname(bitmap
));
427 printk(KERN_DEBUG
" magic: %08x\n", le32_to_cpu(sb
->magic
));
428 printk(KERN_DEBUG
" version: %d\n", le32_to_cpu(sb
->version
));
429 printk(KERN_DEBUG
" uuid: %08x.%08x.%08x.%08x\n",
430 *(__u32
*)(sb
->uuid
+0),
431 *(__u32
*)(sb
->uuid
+4),
432 *(__u32
*)(sb
->uuid
+8),
433 *(__u32
*)(sb
->uuid
+12));
434 printk(KERN_DEBUG
" events: %llu\n",
435 (unsigned long long) le64_to_cpu(sb
->events
));
436 printk(KERN_DEBUG
"events cleared: %llu\n",
437 (unsigned long long) le64_to_cpu(sb
->events_cleared
));
438 printk(KERN_DEBUG
" state: %08x\n", le32_to_cpu(sb
->state
));
439 printk(KERN_DEBUG
" chunksize: %d B\n", le32_to_cpu(sb
->chunksize
));
440 printk(KERN_DEBUG
" daemon sleep: %ds\n", le32_to_cpu(sb
->daemon_sleep
));
441 printk(KERN_DEBUG
" sync size: %llu KB\n",
442 (unsigned long long)le64_to_cpu(sb
->sync_size
)/2);
443 printk(KERN_DEBUG
"max write behind: %d\n", le32_to_cpu(sb
->write_behind
));
444 kunmap_atomic(sb
, KM_USER0
);
447 /* read the superblock from the bitmap file and initialize some bitmap fields */
448 static int bitmap_read_sb(struct bitmap
*bitmap
)
452 unsigned long chunksize
, daemon_sleep
, write_behind
;
453 unsigned long bytes_read
;
454 unsigned long long events
;
457 /* page 0 is the superblock, read it... */
459 bitmap
->sb_page
= read_page(bitmap
->file
, 0, &bytes_read
);
461 bitmap
->sb_page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
, 0);
462 bytes_read
= PAGE_SIZE
;
464 if (IS_ERR(bitmap
->sb_page
)) {
465 err
= PTR_ERR(bitmap
->sb_page
);
466 bitmap
->sb_page
= NULL
;
470 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
472 if (bytes_read
< sizeof(*sb
)) { /* short read */
473 printk(KERN_INFO
"%s: bitmap file superblock truncated\n",
479 chunksize
= le32_to_cpu(sb
->chunksize
);
480 daemon_sleep
= le32_to_cpu(sb
->daemon_sleep
);
481 write_behind
= le32_to_cpu(sb
->write_behind
);
483 /* verify that the bitmap-specific fields are valid */
484 if (sb
->magic
!= cpu_to_le32(BITMAP_MAGIC
))
485 reason
= "bad magic";
486 else if (le32_to_cpu(sb
->version
) < BITMAP_MAJOR_LO
||
487 le32_to_cpu(sb
->version
) > BITMAP_MAJOR_HI
)
488 reason
= "unrecognized superblock version";
489 else if (chunksize
< PAGE_SIZE
)
490 reason
= "bitmap chunksize too small";
491 else if ((1 << ffz(~chunksize
)) != chunksize
)
492 reason
= "bitmap chunksize not a power of 2";
493 else if (daemon_sleep
< 1 || daemon_sleep
> MAX_SCHEDULE_TIMEOUT
/ HZ
)
494 reason
= "daemon sleep period out of range";
495 else if (write_behind
> COUNTER_MAX
)
496 reason
= "write-behind limit out of range (0 - 16383)";
498 printk(KERN_INFO
"%s: invalid bitmap file superblock: %s\n",
499 bmname(bitmap
), reason
);
503 /* keep the array size field of the bitmap superblock up to date */
504 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
506 if (!bitmap
->mddev
->persistent
)
510 * if we have a persistent array superblock, compare the
511 * bitmap's UUID and event counter to the mddev's
513 if (memcmp(sb
->uuid
, bitmap
->mddev
->uuid
, 16)) {
514 printk(KERN_INFO
"%s: bitmap superblock UUID mismatch\n",
518 events
= le64_to_cpu(sb
->events
);
519 if (events
< bitmap
->mddev
->events
) {
520 printk(KERN_INFO
"%s: bitmap file is out of date (%llu < %llu) "
521 "-- forcing full recovery\n", bmname(bitmap
), events
,
522 (unsigned long long) bitmap
->mddev
->events
);
523 sb
->state
|= BITMAP_STALE
;
526 /* assign fields using values from superblock */
527 bitmap
->chunksize
= chunksize
;
528 bitmap
->daemon_sleep
= daemon_sleep
;
529 bitmap
->daemon_lastrun
= jiffies
;
530 bitmap
->max_write_behind
= write_behind
;
531 bitmap
->flags
|= sb
->state
;
532 if (le32_to_cpu(sb
->version
) == BITMAP_MAJOR_HOSTENDIAN
)
533 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
534 bitmap
->events_cleared
= le64_to_cpu(sb
->events_cleared
);
535 if (sb
->state
& BITMAP_STALE
)
536 bitmap
->events_cleared
= bitmap
->mddev
->events
;
539 kunmap_atomic(sb
, KM_USER0
);
541 bitmap_print_sb(bitmap
);
545 enum bitmap_mask_op
{
550 /* record the state of the bitmap in the superblock */
551 static void bitmap_mask_state(struct bitmap
*bitmap
, enum bitmap_state bits
,
552 enum bitmap_mask_op op
)
557 spin_lock_irqsave(&bitmap
->lock
, flags
);
558 if (!bitmap
|| !bitmap
->sb_page
) { /* can't set the state */
559 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
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
);
575 * general bitmap file operations
578 /* calculate the index of the page that contains this bit */
579 static inline unsigned long file_page_index(unsigned long chunk
)
581 return CHUNK_BIT_OFFSET(chunk
) >> PAGE_BIT_SHIFT
;
584 /* calculate the (bit) offset of this bit within a page */
585 static inline unsigned long file_page_offset(unsigned long chunk
)
587 return CHUNK_BIT_OFFSET(chunk
) & (PAGE_BITS
- 1);
591 * return a pointer to the page in the filemap that contains the given bit
593 * this lookup is complicated by the fact that the bitmap sb might be exactly
594 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
597 static inline struct page
*filemap_get_page(struct bitmap
*bitmap
,
600 return bitmap
->filemap
[file_page_index(chunk
) - file_page_index(0)];
604 static void bitmap_file_unmap(struct bitmap
*bitmap
)
606 struct page
**map
, *sb_page
;
611 spin_lock_irqsave(&bitmap
->lock
, flags
);
612 map
= bitmap
->filemap
;
613 bitmap
->filemap
= NULL
;
614 attr
= bitmap
->filemap_attr
;
615 bitmap
->filemap_attr
= NULL
;
616 pages
= bitmap
->file_pages
;
617 bitmap
->file_pages
= 0;
618 sb_page
= bitmap
->sb_page
;
619 bitmap
->sb_page
= NULL
;
620 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
623 if (map
[pages
]->index
!= 0) /* 0 is sb_page, release it below */
624 put_page(map
[pages
]);
628 safe_put_page(sb_page
);
631 static void bitmap_stop_daemon(struct bitmap
*bitmap
);
633 /* dequeue the next item in a page list -- don't call from irq context */
634 static struct page_list
*dequeue_page(struct bitmap
*bitmap
)
636 struct page_list
*item
= NULL
;
637 struct list_head
*head
= &bitmap
->complete_pages
;
639 spin_lock(&bitmap
->write_lock
);
640 if (list_empty(head
))
642 item
= list_entry(head
->prev
, struct page_list
, list
);
643 list_del(head
->prev
);
645 spin_unlock(&bitmap
->write_lock
);
649 static void drain_write_queues(struct bitmap
*bitmap
)
651 struct page_list
*item
;
653 while ((item
= dequeue_page(bitmap
))) {
654 /* don't bother to wait */
655 mempool_free(item
, bitmap
->write_pool
);
658 wake_up(&bitmap
->write_wait
);
661 static void bitmap_file_put(struct bitmap
*bitmap
)
667 spin_lock_irqsave(&bitmap
->lock
, flags
);
670 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
672 bitmap_stop_daemon(bitmap
);
674 drain_write_queues(bitmap
);
676 bitmap_file_unmap(bitmap
);
679 inode
= file
->f_mapping
->host
;
680 spin_lock(&inode
->i_lock
);
681 atomic_set(&inode
->i_writecount
, 1); /* allow writes again */
682 spin_unlock(&inode
->i_lock
);
689 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
690 * then it is no longer reliable, so we stop using it and we mark the file
691 * as failed in the superblock
693 static void bitmap_file_kick(struct bitmap
*bitmap
)
695 char *path
, *ptr
= NULL
;
697 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_SET
);
698 bitmap_update_sb(bitmap
);
701 path
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
703 ptr
= file_path(bitmap
->file
, path
, PAGE_SIZE
);
705 printk(KERN_ALERT
"%s: kicking failed bitmap file %s from array!\n",
706 bmname(bitmap
), ptr
? ptr
: "");
711 bitmap_file_put(bitmap
);
716 enum bitmap_page_attr
{
717 BITMAP_PAGE_DIRTY
= 0, // there are set bits that need to be synced
718 BITMAP_PAGE_CLEAN
= 1, // there are bits that might need to be cleared
719 BITMAP_PAGE_NEEDWRITE
=2, // there are cleared bits that need to be synced
722 static inline void set_page_attr(struct bitmap
*bitmap
, struct page
*page
,
723 enum bitmap_page_attr attr
)
725 __set_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
728 static inline void clear_page_attr(struct bitmap
*bitmap
, struct page
*page
,
729 enum bitmap_page_attr attr
)
731 __clear_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
734 static inline unsigned long test_page_attr(struct bitmap
*bitmap
, struct page
*page
,
735 enum bitmap_page_attr attr
)
737 return test_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
741 * bitmap_file_set_bit -- called before performing a write to the md device
742 * to set (and eventually sync) a particular bit in the bitmap file
744 * we set the bit immediately, then we record the page number so that
745 * when an unplug occurs, we can flush the dirty pages out to disk
747 static void bitmap_file_set_bit(struct bitmap
*bitmap
, sector_t block
)
752 unsigned long chunk
= block
>> CHUNK_BLOCK_SHIFT(bitmap
);
754 if (!bitmap
->filemap
) {
758 page
= filemap_get_page(bitmap
, chunk
);
759 bit
= file_page_offset(chunk
);
762 kaddr
= kmap_atomic(page
, KM_USER0
);
763 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
766 ext2_set_bit(bit
, kaddr
);
767 kunmap_atomic(kaddr
, KM_USER0
);
768 PRINTK("set file bit %lu page %lu\n", bit
, page
->index
);
770 /* record page number so it gets flushed to disk when unplug occurs */
771 set_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
775 /* this gets called when the md device is ready to unplug its underlying
776 * (slave) device queues -- before we let any writes go down, we need to
777 * sync the dirty pages of the bitmap file to disk */
778 int bitmap_unplug(struct bitmap
*bitmap
)
780 unsigned long i
, flags
;
781 int dirty
, need_write
;
789 /* look at each page to see if there are any set bits that need to be
790 * flushed out to disk */
791 for (i
= 0; i
< bitmap
->file_pages
; i
++) {
792 spin_lock_irqsave(&bitmap
->lock
, flags
);
793 if (!bitmap
->filemap
) {
794 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
797 page
= bitmap
->filemap
[i
];
798 dirty
= test_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
799 need_write
= test_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
800 clear_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
801 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
804 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
806 if (dirty
| need_write
) {
807 err
= write_page(bitmap
, page
, 0);
808 if (err
== -EAGAIN
) {
810 err
= write_page(bitmap
, page
, 1);
818 if (wait
) { /* if any writes were performed, we need to wait on them */
820 spin_lock_irq(&bitmap
->write_lock
);
821 wait_event_lock_irq(bitmap
->write_wait
,
822 list_empty(&bitmap
->complete_pages
), bitmap
->write_lock
,
823 wake_up_process(bitmap
->writeback_daemon
->tsk
));
824 spin_unlock_irq(&bitmap
->write_lock
);
826 md_super_wait(bitmap
->mddev
);
831 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
);
832 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
833 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
834 * memory mapping of the bitmap file
836 * if there's no bitmap file, or if the bitmap file had been
837 * previously kicked from the array, we mark all the bits as
838 * 1's in order to cause a full resync.
840 * We ignore all bits for sectors that end earlier than 'start'.
841 * This is used when reading an out-of-date bitmap...
843 static int bitmap_init_from_disk(struct bitmap
*bitmap
, sector_t start
)
845 unsigned long i
, chunks
, index
, oldindex
, bit
;
846 struct page
*page
= NULL
, *oldpage
= NULL
;
847 unsigned long num_pages
, bit_cnt
= 0;
849 unsigned long bytes
, offset
, dummy
;
854 chunks
= bitmap
->chunks
;
857 BUG_ON(!file
&& !bitmap
->offset
);
859 #ifdef INJECT_FAULTS_3
862 outofdate
= bitmap
->flags
& BITMAP_STALE
;
865 printk(KERN_INFO
"%s: bitmap file is out of date, doing full "
866 "recovery\n", bmname(bitmap
));
868 bytes
= (chunks
+ 7) / 8;
870 num_pages
= (bytes
+ sizeof(bitmap_super_t
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
872 if (file
&& i_size_read(file
->f_mapping
->host
) < bytes
+ sizeof(bitmap_super_t
)) {
873 printk(KERN_INFO
"%s: bitmap file too short %lu < %lu\n",
875 (unsigned long) i_size_read(file
->f_mapping
->host
),
876 bytes
+ sizeof(bitmap_super_t
));
882 bitmap
->filemap
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
883 if (!bitmap
->filemap
)
886 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
887 bitmap
->filemap_attr
= kzalloc(
888 (((num_pages
*4/8)+sizeof(unsigned long))
889 /sizeof(unsigned long))
890 *sizeof(unsigned long),
892 if (!bitmap
->filemap_attr
)
897 for (i
= 0; i
< chunks
; i
++) {
899 index
= file_page_index(i
);
900 bit
= file_page_offset(i
);
901 if (index
!= oldindex
) { /* this is a new page, read it in */
902 /* unmap the old page, we're done with it */
905 * if we're here then the superblock page
906 * contains some bits (PAGE_SIZE != sizeof sb)
907 * we've already read it in, so just use it
909 page
= bitmap
->sb_page
;
910 offset
= sizeof(bitmap_super_t
);
912 page
= read_page(file
, index
, &dummy
);
915 page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
, index
);
918 if (IS_ERR(page
)) { /* read error */
928 * if bitmap is out of date, dirty the
929 * whole page and write it out
931 paddr
= kmap_atomic(page
, KM_USER0
);
932 memset(paddr
+ offset
, 0xff,
934 kunmap_atomic(paddr
, KM_USER0
);
935 ret
= write_page(bitmap
, page
, 1);
937 /* release, page not in filemap yet */
943 bitmap
->filemap
[bitmap
->file_pages
++] = page
;
945 paddr
= kmap_atomic(page
, KM_USER0
);
946 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
947 b
= test_bit(bit
, paddr
);
949 b
= ext2_test_bit(bit
, paddr
);
950 kunmap_atomic(paddr
, KM_USER0
);
952 /* if the disk bit is set, set the memory bit */
953 bitmap_set_memory_bits(bitmap
, i
<< CHUNK_BLOCK_SHIFT(bitmap
),
954 ((i
+1) << (CHUNK_BLOCK_SHIFT(bitmap
)) >= start
)
957 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
961 /* everything went OK */
963 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_UNSET
);
965 if (bit_cnt
) { /* Kick recovery if any bits were set */
966 set_bit(MD_RECOVERY_NEEDED
, &bitmap
->mddev
->recovery
);
967 md_wakeup_thread(bitmap
->mddev
->thread
);
971 printk(KERN_INFO
"%s: bitmap initialized from disk: "
972 "read %lu/%lu pages, set %lu bits, status: %d\n",
973 bmname(bitmap
), bitmap
->file_pages
, num_pages
, bit_cnt
, ret
);
978 void bitmap_write_all(struct bitmap
*bitmap
)
980 /* We don't actually write all bitmap blocks here,
981 * just flag them as needing to be written
985 for (i
=0; i
< bitmap
->file_pages
; i
++)
986 set_page_attr(bitmap
, bitmap
->filemap
[i
],
987 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
;
1022 if (time_before(jiffies
, bitmap
->daemon_lastrun
+ bitmap
->daemon_sleep
*HZ
))
1024 bitmap
->daemon_lastrun
= jiffies
;
1026 for (j
= 0; j
< bitmap
->chunks
; j
++) {
1027 bitmap_counter_t
*bmc
;
1028 spin_lock_irqsave(&bitmap
->lock
, flags
);
1029 if (!bitmap
->filemap
) {
1030 /* error or shutdown */
1031 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1035 page
= filemap_get_page(bitmap
, j
);
1037 if (page
!= lastpage
) {
1038 /* skip this page unless it's marked as needing cleaning */
1039 if (!test_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
)) {
1040 int need_write
= test_page_attr(bitmap
, page
,
1041 BITMAP_PAGE_NEEDWRITE
);
1043 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1045 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1047 switch (write_page(bitmap
, page
, 0)) {
1049 set_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1054 bitmap_file_kick(bitmap
);
1060 /* grab the new page, sync and release the old */
1061 if (lastpage
!= NULL
) {
1062 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1063 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1064 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1065 err
= write_page(bitmap
, lastpage
, 0);
1066 if (err
== -EAGAIN
) {
1068 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1071 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1072 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1075 bitmap_file_kick(bitmap
);
1077 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1080 printk("bitmap clean at page %lu\n", j);
1082 spin_lock_irqsave(&bitmap
->lock
, flags
);
1083 clear_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1085 bmc
= bitmap_get_counter(bitmap
, j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1089 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1092 *bmc
=1; /* maybe clear the bit next time */
1093 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1094 } else if (*bmc
== 1) {
1095 /* we can clear the bit */
1097 bitmap_count_page(bitmap
, j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1101 paddr
= kmap_atomic(page
, KM_USER0
);
1102 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1103 clear_bit(file_page_offset(j
), paddr
);
1105 ext2_clear_bit(file_page_offset(j
), paddr
);
1106 kunmap_atomic(paddr
, KM_USER0
);
1109 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1112 /* now sync the final page */
1113 if (lastpage
!= NULL
) {
1114 spin_lock_irqsave(&bitmap
->lock
, flags
);
1115 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1116 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1117 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1118 err
= write_page(bitmap
, lastpage
, 0);
1119 if (err
== -EAGAIN
) {
1120 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1124 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1125 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1132 static void daemon_exit(struct bitmap
*bitmap
, mdk_thread_t
**daemon
)
1135 unsigned long flags
;
1137 /* if no one is waiting on us, we'll free the md thread struct
1138 * and exit, otherwise we let the waiter clean things up */
1139 spin_lock_irqsave(&bitmap
->lock
, flags
);
1140 if ((dmn
= *daemon
)) { /* no one is waiting, cleanup and exit */
1142 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1144 complete_and_exit(NULL
, 0); /* do_exit not exported */
1146 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1149 static void bitmap_writeback_daemon(mddev_t
*mddev
)
1151 struct bitmap
*bitmap
= mddev
->bitmap
;
1153 struct page_list
*item
;
1156 if (signal_pending(current
)) {
1158 "%s: bitmap writeback daemon got signal, exiting...\n",
1164 /* about to be stopped. */
1167 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap
));
1168 /* wait on bitmap page writebacks */
1169 while ((item
= dequeue_page(bitmap
))) {
1171 mempool_free(item
, bitmap
->write_pool
);
1172 PRINTK("wait on page writeback: %p\n", page
);
1173 wait_on_page_writeback(page
);
1174 PRINTK("finished page writeback: %p\n", page
);
1176 err
= PageError(page
);
1178 printk(KERN_WARNING
"%s: bitmap file writeback "
1179 "failed (page %lu): %d\n",
1180 bmname(bitmap
), page
->index
, err
);
1181 bitmap_file_kick(bitmap
);
1186 wake_up(&bitmap
->write_wait
);
1188 printk(KERN_INFO
"%s: bitmap writeback daemon exiting (%d)\n",
1189 bmname(bitmap
), err
);
1190 daemon_exit(bitmap
, &bitmap
->writeback_daemon
);
1194 static mdk_thread_t
*bitmap_start_daemon(struct bitmap
*bitmap
,
1195 void (*func
)(mddev_t
*), char *name
)
1197 mdk_thread_t
*daemon
;
1200 #ifdef INJECT_FATAL_FAULT_2
1203 sprintf(namebuf
, "%%s_%s", name
);
1204 daemon
= md_register_thread(func
, bitmap
->mddev
, namebuf
);
1207 printk(KERN_ERR
"%s: failed to start bitmap daemon\n",
1209 return ERR_PTR(-ECHILD
);
1212 md_wakeup_thread(daemon
); /* start it running */
1214 PRINTK("%s: %s daemon (pid %d) started...\n",
1215 bmname(bitmap
), name
, daemon
->tsk
->pid
);
1220 static void bitmap_stop_daemon(struct bitmap
*bitmap
)
1222 /* the daemon can't stop itself... it'll just exit instead... */
1223 if (bitmap
->writeback_daemon
&& ! IS_ERR(bitmap
->writeback_daemon
) &&
1224 current
->pid
!= bitmap
->writeback_daemon
->tsk
->pid
) {
1225 mdk_thread_t
*daemon
;
1226 unsigned long flags
;
1228 spin_lock_irqsave(&bitmap
->lock
, flags
);
1229 daemon
= bitmap
->writeback_daemon
;
1230 bitmap
->writeback_daemon
= NULL
;
1231 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1232 if (daemon
&& ! IS_ERR(daemon
))
1233 md_unregister_thread(daemon
); /* destroy the thread */
1237 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1238 sector_t offset
, int *blocks
,
1241 /* If 'create', we might release the lock and reclaim it.
1242 * The lock must have been taken with interrupts enabled.
1243 * If !create, we don't release the lock.
1245 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1246 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1247 unsigned long pageoff
= (chunk
& PAGE_COUNTER_MASK
) << COUNTER_BYTE_SHIFT
;
1250 if (bitmap_checkpage(bitmap
, page
, create
) < 0) {
1251 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1252 *blocks
= csize
- (offset
& (csize
- 1));
1255 /* now locked ... */
1257 if (bitmap
->bp
[page
].hijacked
) { /* hijacked pointer */
1258 /* should we use the first or second counter field
1259 * of the hijacked pointer? */
1260 int hi
= (pageoff
> PAGE_COUNTER_MASK
);
1261 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
) +
1262 PAGE_COUNTER_SHIFT
- 1);
1263 *blocks
= csize
- (offset
& (csize
- 1));
1264 return &((bitmap_counter_t
*)
1265 &bitmap
->bp
[page
].map
)[hi
];
1266 } else { /* page is allocated */
1267 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1268 *blocks
= csize
- (offset
& (csize
- 1));
1269 return (bitmap_counter_t
*)
1270 &(bitmap
->bp
[page
].map
[pageoff
]);
1274 int bitmap_startwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
, int behind
)
1276 if (!bitmap
) return 0;
1279 atomic_inc(&bitmap
->behind_writes
);
1280 PRINTK(KERN_DEBUG
"inc write-behind count %d/%d\n",
1281 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1286 bitmap_counter_t
*bmc
;
1288 spin_lock_irq(&bitmap
->lock
);
1289 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 1);
1291 spin_unlock_irq(&bitmap
->lock
);
1297 bitmap_file_set_bit(bitmap
, offset
);
1298 bitmap_count_page(bitmap
,offset
, 1);
1299 blk_plug_device(bitmap
->mddev
->queue
);
1304 if ((*bmc
& COUNTER_MAX
) == COUNTER_MAX
) BUG();
1307 spin_unlock_irq(&bitmap
->lock
);
1310 if (sectors
> blocks
)
1317 void bitmap_endwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
,
1318 int success
, int behind
)
1320 if (!bitmap
) return;
1322 atomic_dec(&bitmap
->behind_writes
);
1323 PRINTK(KERN_DEBUG
"dec write-behind count %d/%d\n",
1324 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1329 unsigned long flags
;
1330 bitmap_counter_t
*bmc
;
1332 spin_lock_irqsave(&bitmap
->lock
, flags
);
1333 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 0);
1335 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1339 if (!success
&& ! (*bmc
& NEEDED_MASK
))
1340 *bmc
|= NEEDED_MASK
;
1344 set_page_attr(bitmap
,
1345 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1348 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1350 if (sectors
> blocks
)
1356 int bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
,
1359 bitmap_counter_t
*bmc
;
1361 if (bitmap
== NULL
) {/* FIXME or bitmap set as 'failed' */
1363 return 1; /* always resync if no bitmap */
1365 spin_lock_irq(&bitmap
->lock
);
1366 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1372 else if (NEEDED(*bmc
)) {
1374 if (!degraded
) { /* don't set/clear bits if degraded */
1375 *bmc
|= RESYNC_MASK
;
1376 *bmc
&= ~NEEDED_MASK
;
1380 spin_unlock_irq(&bitmap
->lock
);
1384 void bitmap_end_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
, int aborted
)
1386 bitmap_counter_t
*bmc
;
1387 unsigned long flags
;
1389 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1390 */ if (bitmap
== NULL
) {
1394 spin_lock_irqsave(&bitmap
->lock
, flags
);
1395 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1400 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1403 *bmc
&= ~RESYNC_MASK
;
1405 if (!NEEDED(*bmc
) && aborted
)
1406 *bmc
|= NEEDED_MASK
;
1409 set_page_attr(bitmap
,
1410 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1416 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1419 void bitmap_close_sync(struct bitmap
*bitmap
)
1421 /* Sync has finished, and any bitmap chunks that weren't synced
1422 * properly have been aborted. It remains to us to clear the
1423 * RESYNC bit wherever it is still on
1425 sector_t sector
= 0;
1427 if (!bitmap
) return;
1428 while (sector
< bitmap
->mddev
->resync_max_sectors
) {
1429 bitmap_end_sync(bitmap
, sector
, &blocks
, 0);
1431 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1432 (unsigned long long)sector, blocks);
1433 */ sector
+= blocks
;
1437 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
)
1439 /* For each chunk covered by any of these sectors, set the
1440 * counter to 1 and set resync_needed. They should all
1441 * be 0 at this point
1445 bitmap_counter_t
*bmc
;
1446 spin_lock_irq(&bitmap
->lock
);
1447 bmc
= bitmap_get_counter(bitmap
, offset
, &secs
, 1);
1449 spin_unlock_irq(&bitmap
->lock
);
1454 *bmc
= 1 | (needed
?NEEDED_MASK
:0);
1455 bitmap_count_page(bitmap
, offset
, 1);
1456 page
= filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
));
1457 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1459 spin_unlock_irq(&bitmap
->lock
);
1464 * flush out any pending updates
1466 void bitmap_flush(mddev_t
*mddev
)
1468 struct bitmap
*bitmap
= mddev
->bitmap
;
1471 if (!bitmap
) /* there was no bitmap */
1474 /* run the daemon_work three time to ensure everything is flushed
1477 sleep
= bitmap
->daemon_sleep
;
1478 bitmap
->daemon_sleep
= 0;
1479 bitmap_daemon_work(bitmap
);
1480 bitmap_daemon_work(bitmap
);
1481 bitmap_daemon_work(bitmap
);
1482 bitmap
->daemon_sleep
= sleep
;
1483 bitmap_update_sb(bitmap
);
1487 * free memory that was allocated
1489 static void bitmap_free(struct bitmap
*bitmap
)
1491 unsigned long k
, pages
;
1492 struct bitmap_page
*bp
;
1494 if (!bitmap
) /* there was no bitmap */
1497 /* release the bitmap file and kill the daemon */
1498 bitmap_file_put(bitmap
);
1501 pages
= bitmap
->pages
;
1503 /* free all allocated memory */
1505 mempool_destroy(bitmap
->write_pool
);
1507 if (bp
) /* deallocate the page memory */
1508 for (k
= 0; k
< pages
; k
++)
1509 if (bp
[k
].map
&& !bp
[k
].hijacked
)
1514 void bitmap_destroy(mddev_t
*mddev
)
1516 struct bitmap
*bitmap
= mddev
->bitmap
;
1518 if (!bitmap
) /* there was no bitmap */
1521 mddev
->bitmap
= NULL
; /* disconnect from the md device */
1523 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1525 bitmap_free(bitmap
);
1529 * initialize the bitmap structure
1530 * if this returns an error, bitmap_destroy must be called to do clean up
1532 int bitmap_create(mddev_t
*mddev
)
1534 struct bitmap
*bitmap
;
1535 unsigned long blocks
= mddev
->resync_max_sectors
;
1536 unsigned long chunks
;
1537 unsigned long pages
;
1538 struct file
*file
= mddev
->bitmap_file
;
1542 BUG_ON(sizeof(bitmap_super_t
) != 256);
1544 if (!file
&& !mddev
->bitmap_offset
) /* bitmap disabled, nothing to do */
1547 BUG_ON(file
&& mddev
->bitmap_offset
);
1549 bitmap
= kzalloc(sizeof(*bitmap
), GFP_KERNEL
);
1553 spin_lock_init(&bitmap
->lock
);
1554 bitmap
->mddev
= mddev
;
1556 spin_lock_init(&bitmap
->write_lock
);
1557 INIT_LIST_HEAD(&bitmap
->complete_pages
);
1558 init_waitqueue_head(&bitmap
->write_wait
);
1559 bitmap
->write_pool
= mempool_create(WRITE_POOL_SIZE
, write_pool_alloc
,
1560 write_pool_free
, NULL
);
1562 if (!bitmap
->write_pool
)
1565 bitmap
->file
= file
;
1566 bitmap
->offset
= mddev
->bitmap_offset
;
1567 if (file
) get_file(file
);
1568 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1569 err
= bitmap_read_sb(bitmap
);
1573 bitmap
->chunkshift
= find_first_bit(&bitmap
->chunksize
,
1574 sizeof(bitmap
->chunksize
));
1576 /* now that chunksize and chunkshift are set, we can use these macros */
1577 chunks
= (blocks
+ CHUNK_BLOCK_RATIO(bitmap
) - 1) /
1578 CHUNK_BLOCK_RATIO(bitmap
);
1579 pages
= (chunks
+ PAGE_COUNTER_RATIO
- 1) / PAGE_COUNTER_RATIO
;
1583 bitmap
->chunks
= chunks
;
1584 bitmap
->pages
= pages
;
1585 bitmap
->missing_pages
= pages
;
1586 bitmap
->counter_bits
= COUNTER_BITS
;
1588 bitmap
->syncchunk
= ~0UL;
1590 #ifdef INJECT_FATAL_FAULT_1
1593 bitmap
->bp
= kzalloc(pages
* sizeof(*bitmap
->bp
), GFP_KERNEL
);
1599 bitmap
->flags
|= BITMAP_ACTIVE
;
1601 /* now that we have some pages available, initialize the in-memory
1602 * bitmap from the on-disk bitmap */
1604 if (mddev
->degraded
== 0
1605 || bitmap
->events_cleared
== mddev
->events
)
1606 /* no need to keep dirty bits to optimise a re-add of a missing device */
1607 start
= mddev
->recovery_cp
;
1608 err
= bitmap_init_from_disk(bitmap
, start
);
1613 printk(KERN_INFO
"created bitmap (%lu pages) for device %s\n",
1614 pages
, bmname(bitmap
));
1616 mddev
->bitmap
= bitmap
;
1619 /* kick off the bitmap writeback daemon */
1620 bitmap
->writeback_daemon
=
1621 bitmap_start_daemon(bitmap
,
1622 bitmap_writeback_daemon
,
1625 if (IS_ERR(bitmap
->writeback_daemon
))
1626 return PTR_ERR(bitmap
->writeback_daemon
);
1627 mddev
->thread
->timeout
= bitmap
->daemon_sleep
* HZ
;
1629 return bitmap_update_sb(bitmap
);
1632 bitmap_free(bitmap
);
1636 /* the bitmap API -- for raid personalities */
1637 EXPORT_SYMBOL(bitmap_startwrite
);
1638 EXPORT_SYMBOL(bitmap_endwrite
);
1639 EXPORT_SYMBOL(bitmap_start_sync
);
1640 EXPORT_SYMBOL(bitmap_end_sync
);
1641 EXPORT_SYMBOL(bitmap_unplug
);
1642 EXPORT_SYMBOL(bitmap_close_sync
);
1643 EXPORT_SYMBOL(bitmap_daemon_work
);