2 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
4 * bitmap_create - sets up the bitmap structure
5 * bitmap_destroy - destroys the bitmap structure
7 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
8 * - added disk storage for bitmap
9 * - changes to allow various bitmap chunk sizes
15 * flush after percent set rather than just time based. (maybe both).
16 * wait if count gets too high, wake when it drops to half.
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/timer.h>
24 #include <linux/sched.h>
25 #include <linux/list.h>
26 #include <linux/file.h>
27 #include <linux/mount.h>
28 #include <linux/buffer_head.h>
29 #include <linux/raid/md.h>
30 #include <linux/raid/bitmap.h>
37 /* these are for debugging purposes only! */
39 /* define one and only one of these */
40 #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
41 #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
42 #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
43 #define INJECT_FAULTS_4 0 /* undef */
44 #define INJECT_FAULTS_5 0 /* undef */
45 #define INJECT_FAULTS_6 0
47 /* if these are defined, the driver will fail! debug only */
48 #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
49 #define INJECT_FATAL_FAULT_2 0 /* undef */
50 #define INJECT_FATAL_FAULT_3 0 /* undef */
53 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
54 #define DPRINTK(x...) do { } while(0)
58 # define PRINTK(x...) printk(KERN_DEBUG x)
64 static inline char * bmname(struct bitmap
*bitmap
)
66 return bitmap
->mddev
? mdname(bitmap
->mddev
) : "mdX";
71 * just a placeholder - calls kmalloc for bitmap pages
73 static unsigned char *bitmap_alloc_page(struct bitmap
*bitmap
)
77 #ifdef INJECT_FAULTS_1
80 page
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
83 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap
));
85 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
86 bmname(bitmap
), page
);
91 * for now just a placeholder -- just calls kfree for bitmap pages
93 static void bitmap_free_page(struct bitmap
*bitmap
, unsigned char *page
)
95 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap
), page
);
100 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
102 * 1) check to see if this page is allocated, if it's not then try to alloc
103 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
104 * page pointer directly as a counter
106 * if we find our page, we increment the page's refcount so that it stays
107 * allocated while we're using it
109 static int bitmap_checkpage(struct bitmap
*bitmap
, unsigned long page
, int create
)
111 unsigned char *mappage
;
113 if (page
>= bitmap
->pages
) {
115 "%s: invalid bitmap page request: %lu (> %lu)\n",
116 bmname(bitmap
), page
, bitmap
->pages
-1);
121 if (bitmap
->bp
[page
].hijacked
) /* it's hijacked, don't try to alloc */
124 if (bitmap
->bp
[page
].map
) /* page is already allocated, just return */
130 spin_unlock_irq(&bitmap
->lock
);
132 /* this page has not been allocated yet */
134 if ((mappage
= bitmap_alloc_page(bitmap
)) == NULL
) {
135 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
137 /* failed - set the hijacked flag so that we can use the
138 * pointer as a counter */
139 spin_lock_irq(&bitmap
->lock
);
140 if (!bitmap
->bp
[page
].map
)
141 bitmap
->bp
[page
].hijacked
= 1;
147 spin_lock_irq(&bitmap
->lock
);
149 /* recheck the page */
151 if (bitmap
->bp
[page
].map
|| bitmap
->bp
[page
].hijacked
) {
152 /* somebody beat us to getting the page */
153 bitmap_free_page(bitmap
, mappage
);
157 /* no page was in place and we have one, so install it */
159 memset(mappage
, 0, PAGE_SIZE
);
160 bitmap
->bp
[page
].map
= mappage
;
161 bitmap
->missing_pages
--;
167 /* if page is completely empty, put it back on the free list, or dealloc it */
168 /* if page was hijacked, unmark the flag so it might get alloced next time */
169 /* Note: lock should be held when calling this */
170 static void bitmap_checkfree(struct bitmap
*bitmap
, unsigned long page
)
174 if (bitmap
->bp
[page
].count
) /* page is still busy */
177 /* page is no longer in use, it can be released */
179 if (bitmap
->bp
[page
].hijacked
) { /* page was hijacked, undo this now */
180 bitmap
->bp
[page
].hijacked
= 0;
181 bitmap
->bp
[page
].map
= NULL
;
185 /* normal case, free the page */
188 /* actually ... let's not. We will probably need the page again exactly when
189 * memory is tight and we are flusing to disk
193 ptr
= bitmap
->bp
[page
].map
;
194 bitmap
->bp
[page
].map
= NULL
;
195 bitmap
->missing_pages
++;
196 bitmap_free_page(bitmap
, ptr
);
203 * bitmap file handling - read and write the bitmap file and its superblock
207 * basic page I/O operations
210 /* IO operations when bitmap is stored near all superblocks */
211 static struct page
*read_sb_page(mddev_t
*mddev
, long offset
,
213 unsigned long index
, int size
)
215 /* choose a good rdev and read the page from there */
221 page
= alloc_page(GFP_KERNEL
);
223 return ERR_PTR(-ENOMEM
);
225 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
226 if (! test_bit(In_sync
, &rdev
->flags
)
227 || test_bit(Faulty
, &rdev
->flags
))
230 target
= rdev
->sb_start
+ offset
+ index
* (PAGE_SIZE
/512);
232 if (sync_page_io(rdev
->bdev
, target
,
233 roundup(size
, bdev_hardsect_size(rdev
->bdev
)),
236 attach_page_buffers(page
, NULL
); /* so that free_buffer will
241 return ERR_PTR(-EIO
);
245 static mdk_rdev_t
*next_active_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
247 /* Iterate the disks of an mddev, using rcu to protect access to the
248 * linked list, and raising the refcount of devices we return to ensure
249 * they don't disappear while in use.
250 * As devices are only added or removed when raid_disk is < 0 and
251 * nr_pending is 0 and In_sync is clear, the entries we return will
252 * still be in the same position on the list when we re-enter
253 * list_for_each_continue_rcu.
255 struct list_head
*pos
;
258 /* start at the beginning */
261 /* release the previous rdev and start from there. */
262 rdev_dec_pending(rdev
, mddev
);
263 pos
= &rdev
->same_set
;
265 list_for_each_continue_rcu(pos
, &mddev
->disks
) {
266 rdev
= list_entry(pos
, mdk_rdev_t
, same_set
);
267 if (rdev
->raid_disk
>= 0 &&
268 test_bit(In_sync
, &rdev
->flags
) &&
269 !test_bit(Faulty
, &rdev
->flags
)) {
270 /* this is a usable devices */
271 atomic_inc(&rdev
->nr_pending
);
280 static int write_sb_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
282 mdk_rdev_t
*rdev
= NULL
;
283 mddev_t
*mddev
= bitmap
->mddev
;
285 while ((rdev
= next_active_rdev(rdev
, mddev
)) != NULL
) {
286 int size
= PAGE_SIZE
;
287 if (page
->index
== bitmap
->file_pages
-1)
288 size
= roundup(bitmap
->last_page_size
,
289 bdev_hardsect_size(rdev
->bdev
));
290 /* Just make sure we aren't corrupting data or
293 if (bitmap
->offset
< 0) {
294 /* DATA BITMAP METADATA */
296 + (long)(page
->index
* (PAGE_SIZE
/512))
298 /* bitmap runs in to metadata */
300 if (rdev
->data_offset
+ mddev
->size
*2
301 > rdev
->sb_start
+ bitmap
->offset
)
302 /* data runs in to bitmap */
304 } else if (rdev
->sb_start
< rdev
->data_offset
) {
305 /* METADATA BITMAP DATA */
308 + page
->index
*(PAGE_SIZE
/512) + size
/512
310 /* bitmap runs in to data */
313 /* DATA METADATA BITMAP - no problems */
315 md_super_write(mddev
, rdev
,
316 rdev
->sb_start
+ bitmap
->offset
317 + page
->index
* (PAGE_SIZE
/512),
323 md_super_wait(mddev
);
331 static void bitmap_file_kick(struct bitmap
*bitmap
);
333 * write out a page to a file
335 static void write_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
337 struct buffer_head
*bh
;
339 if (bitmap
->file
== NULL
) {
340 switch (write_sb_page(bitmap
, page
, wait
)) {
342 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
346 bh
= page_buffers(page
);
348 while (bh
&& bh
->b_blocknr
) {
349 atomic_inc(&bitmap
->pending_writes
);
350 set_buffer_locked(bh
);
351 set_buffer_mapped(bh
);
352 submit_bh(WRITE
, bh
);
353 bh
= bh
->b_this_page
;
357 wait_event(bitmap
->write_wait
,
358 atomic_read(&bitmap
->pending_writes
)==0);
361 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
362 bitmap_file_kick(bitmap
);
365 static void end_bitmap_write(struct buffer_head
*bh
, int uptodate
)
367 struct bitmap
*bitmap
= bh
->b_private
;
371 spin_lock_irqsave(&bitmap
->lock
, flags
);
372 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
373 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
375 if (atomic_dec_and_test(&bitmap
->pending_writes
))
376 wake_up(&bitmap
->write_wait
);
379 /* copied from buffer.c */
381 __clear_page_buffers(struct page
*page
)
383 ClearPagePrivate(page
);
384 set_page_private(page
, 0);
385 page_cache_release(page
);
387 static void free_buffers(struct page
*page
)
389 struct buffer_head
*bh
= page_buffers(page
);
392 struct buffer_head
*next
= bh
->b_this_page
;
393 free_buffer_head(bh
);
396 __clear_page_buffers(page
);
400 /* read a page from a file.
401 * We both read the page, and attach buffers to the page to record the
402 * address of each block (using bmap). These addresses will be used
403 * to write the block later, completely bypassing the filesystem.
404 * This usage is similar to how swap files are handled, and allows us
405 * to write to a file with no concerns of memory allocation failing.
407 static struct page
*read_page(struct file
*file
, unsigned long index
,
408 struct bitmap
*bitmap
,
411 struct page
*page
= NULL
;
412 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
413 struct buffer_head
*bh
;
416 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE
,
417 (unsigned long long)index
<< PAGE_SHIFT
);
419 page
= alloc_page(GFP_KERNEL
);
421 page
= ERR_PTR(-ENOMEM
);
425 bh
= alloc_page_buffers(page
, 1<<inode
->i_blkbits
, 0);
428 page
= ERR_PTR(-ENOMEM
);
431 attach_page_buffers(page
, bh
);
432 block
= index
<< (PAGE_SHIFT
- inode
->i_blkbits
);
437 bh
->b_blocknr
= bmap(inode
, block
);
438 if (bh
->b_blocknr
== 0) {
439 /* Cannot use this file! */
441 page
= ERR_PTR(-EINVAL
);
444 bh
->b_bdev
= inode
->i_sb
->s_bdev
;
445 if (count
< (1<<inode
->i_blkbits
))
448 count
-= (1<<inode
->i_blkbits
);
450 bh
->b_end_io
= end_bitmap_write
;
451 bh
->b_private
= bitmap
;
452 atomic_inc(&bitmap
->pending_writes
);
453 set_buffer_locked(bh
);
454 set_buffer_mapped(bh
);
458 bh
= bh
->b_this_page
;
462 wait_event(bitmap
->write_wait
,
463 atomic_read(&bitmap
->pending_writes
)==0);
464 if (bitmap
->flags
& BITMAP_WRITE_ERROR
) {
466 page
= ERR_PTR(-EIO
);
470 printk(KERN_ALERT
"md: bitmap read error: (%dB @ %Lu): %ld\n",
472 (unsigned long long)index
<< PAGE_SHIFT
,
478 * bitmap file superblock operations
481 /* update the event counter and sync the superblock to disk */
482 void bitmap_update_sb(struct bitmap
*bitmap
)
487 if (!bitmap
|| !bitmap
->mddev
) /* no bitmap for this array */
489 spin_lock_irqsave(&bitmap
->lock
, flags
);
490 if (!bitmap
->sb_page
) { /* no superblock */
491 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
494 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
495 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
496 sb
->events
= cpu_to_le64(bitmap
->mddev
->events
);
497 if (bitmap
->mddev
->events
< bitmap
->events_cleared
) {
498 /* rocking back to read-only */
499 bitmap
->events_cleared
= bitmap
->mddev
->events
;
500 sb
->events_cleared
= cpu_to_le64(bitmap
->events_cleared
);
502 kunmap_atomic(sb
, KM_USER0
);
503 write_page(bitmap
, bitmap
->sb_page
, 1);
506 /* print out the bitmap file superblock */
507 void bitmap_print_sb(struct bitmap
*bitmap
)
511 if (!bitmap
|| !bitmap
->sb_page
)
513 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
514 printk(KERN_DEBUG
"%s: bitmap file superblock:\n", bmname(bitmap
));
515 printk(KERN_DEBUG
" magic: %08x\n", le32_to_cpu(sb
->magic
));
516 printk(KERN_DEBUG
" version: %d\n", le32_to_cpu(sb
->version
));
517 printk(KERN_DEBUG
" uuid: %08x.%08x.%08x.%08x\n",
518 *(__u32
*)(sb
->uuid
+0),
519 *(__u32
*)(sb
->uuid
+4),
520 *(__u32
*)(sb
->uuid
+8),
521 *(__u32
*)(sb
->uuid
+12));
522 printk(KERN_DEBUG
" events: %llu\n",
523 (unsigned long long) le64_to_cpu(sb
->events
));
524 printk(KERN_DEBUG
"events cleared: %llu\n",
525 (unsigned long long) le64_to_cpu(sb
->events_cleared
));
526 printk(KERN_DEBUG
" state: %08x\n", le32_to_cpu(sb
->state
));
527 printk(KERN_DEBUG
" chunksize: %d B\n", le32_to_cpu(sb
->chunksize
));
528 printk(KERN_DEBUG
" daemon sleep: %ds\n", le32_to_cpu(sb
->daemon_sleep
));
529 printk(KERN_DEBUG
" sync size: %llu KB\n",
530 (unsigned long long)le64_to_cpu(sb
->sync_size
)/2);
531 printk(KERN_DEBUG
"max write behind: %d\n", le32_to_cpu(sb
->write_behind
));
532 kunmap_atomic(sb
, KM_USER0
);
535 /* read the superblock from the bitmap file and initialize some bitmap fields */
536 static int bitmap_read_sb(struct bitmap
*bitmap
)
540 unsigned long chunksize
, daemon_sleep
, write_behind
;
541 unsigned long long events
;
544 /* page 0 is the superblock, read it... */
546 loff_t isize
= i_size_read(bitmap
->file
->f_mapping
->host
);
547 int bytes
= isize
> PAGE_SIZE
? PAGE_SIZE
: isize
;
549 bitmap
->sb_page
= read_page(bitmap
->file
, 0, bitmap
, bytes
);
551 bitmap
->sb_page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
,
553 0, sizeof(bitmap_super_t
));
555 if (IS_ERR(bitmap
->sb_page
)) {
556 err
= PTR_ERR(bitmap
->sb_page
);
557 bitmap
->sb_page
= NULL
;
561 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
563 chunksize
= le32_to_cpu(sb
->chunksize
);
564 daemon_sleep
= le32_to_cpu(sb
->daemon_sleep
);
565 write_behind
= le32_to_cpu(sb
->write_behind
);
567 /* verify that the bitmap-specific fields are valid */
568 if (sb
->magic
!= cpu_to_le32(BITMAP_MAGIC
))
569 reason
= "bad magic";
570 else if (le32_to_cpu(sb
->version
) < BITMAP_MAJOR_LO
||
571 le32_to_cpu(sb
->version
) > BITMAP_MAJOR_HI
)
572 reason
= "unrecognized superblock version";
573 else if (chunksize
< PAGE_SIZE
)
574 reason
= "bitmap chunksize too small";
575 else if ((1 << ffz(~chunksize
)) != chunksize
)
576 reason
= "bitmap chunksize not a power of 2";
577 else if (daemon_sleep
< 1 || daemon_sleep
> MAX_SCHEDULE_TIMEOUT
/ HZ
)
578 reason
= "daemon sleep period out of range";
579 else if (write_behind
> COUNTER_MAX
)
580 reason
= "write-behind limit out of range (0 - 16383)";
582 printk(KERN_INFO
"%s: invalid bitmap file superblock: %s\n",
583 bmname(bitmap
), reason
);
587 /* keep the array size field of the bitmap superblock up to date */
588 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
590 if (!bitmap
->mddev
->persistent
)
594 * if we have a persistent array superblock, compare the
595 * bitmap's UUID and event counter to the mddev's
597 if (memcmp(sb
->uuid
, bitmap
->mddev
->uuid
, 16)) {
598 printk(KERN_INFO
"%s: bitmap superblock UUID mismatch\n",
602 events
= le64_to_cpu(sb
->events
);
603 if (events
< bitmap
->mddev
->events
) {
604 printk(KERN_INFO
"%s: bitmap file is out of date (%llu < %llu) "
605 "-- forcing full recovery\n", bmname(bitmap
), events
,
606 (unsigned long long) bitmap
->mddev
->events
);
607 sb
->state
|= cpu_to_le32(BITMAP_STALE
);
610 /* assign fields using values from superblock */
611 bitmap
->chunksize
= chunksize
;
612 bitmap
->daemon_sleep
= daemon_sleep
;
613 bitmap
->daemon_lastrun
= jiffies
;
614 bitmap
->max_write_behind
= write_behind
;
615 bitmap
->flags
|= le32_to_cpu(sb
->state
);
616 if (le32_to_cpu(sb
->version
) == BITMAP_MAJOR_HOSTENDIAN
)
617 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
618 bitmap
->events_cleared
= le64_to_cpu(sb
->events_cleared
);
619 if (sb
->state
& cpu_to_le32(BITMAP_STALE
))
620 bitmap
->events_cleared
= bitmap
->mddev
->events
;
623 kunmap_atomic(sb
, KM_USER0
);
625 bitmap_print_sb(bitmap
);
629 enum bitmap_mask_op
{
634 /* record the state of the bitmap in the superblock. Return the old value */
635 static int bitmap_mask_state(struct bitmap
*bitmap
, enum bitmap_state bits
,
636 enum bitmap_mask_op op
)
642 spin_lock_irqsave(&bitmap
->lock
, flags
);
643 if (!bitmap
->sb_page
) { /* can't set the state */
644 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
647 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
648 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
649 old
= le32_to_cpu(sb
->state
) & bits
;
651 case MASK_SET
: sb
->state
|= cpu_to_le32(bits
);
653 case MASK_UNSET
: sb
->state
&= cpu_to_le32(~bits
);
657 kunmap_atomic(sb
, KM_USER0
);
662 * general bitmap file operations
665 /* calculate the index of the page that contains this bit */
666 static inline unsigned long file_page_index(unsigned long chunk
)
668 return CHUNK_BIT_OFFSET(chunk
) >> PAGE_BIT_SHIFT
;
671 /* calculate the (bit) offset of this bit within a page */
672 static inline unsigned long file_page_offset(unsigned long chunk
)
674 return CHUNK_BIT_OFFSET(chunk
) & (PAGE_BITS
- 1);
678 * return a pointer to the page in the filemap that contains the given bit
680 * this lookup is complicated by the fact that the bitmap sb might be exactly
681 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
684 static inline struct page
*filemap_get_page(struct bitmap
*bitmap
,
687 if (file_page_index(chunk
) >= bitmap
->file_pages
) return NULL
;
688 return bitmap
->filemap
[file_page_index(chunk
) - file_page_index(0)];
692 static void bitmap_file_unmap(struct bitmap
*bitmap
)
694 struct page
**map
, *sb_page
;
699 spin_lock_irqsave(&bitmap
->lock
, flags
);
700 map
= bitmap
->filemap
;
701 bitmap
->filemap
= NULL
;
702 attr
= bitmap
->filemap_attr
;
703 bitmap
->filemap_attr
= NULL
;
704 pages
= bitmap
->file_pages
;
705 bitmap
->file_pages
= 0;
706 sb_page
= bitmap
->sb_page
;
707 bitmap
->sb_page
= NULL
;
708 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
711 if (map
[pages
]->index
!= 0) /* 0 is sb_page, release it below */
712 free_buffers(map
[pages
]);
717 free_buffers(sb_page
);
720 static void bitmap_file_put(struct bitmap
*bitmap
)
725 spin_lock_irqsave(&bitmap
->lock
, flags
);
728 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
731 wait_event(bitmap
->write_wait
,
732 atomic_read(&bitmap
->pending_writes
)==0);
733 bitmap_file_unmap(bitmap
);
736 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
737 invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
744 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
745 * then it is no longer reliable, so we stop using it and we mark the file
746 * as failed in the superblock
748 static void bitmap_file_kick(struct bitmap
*bitmap
)
750 char *path
, *ptr
= NULL
;
752 if (bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_SET
) == 0) {
753 bitmap_update_sb(bitmap
);
756 path
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
758 ptr
= d_path(&bitmap
->file
->f_path
, path
,
763 "%s: kicking failed bitmap file %s from array!\n",
764 bmname(bitmap
), IS_ERR(ptr
) ? "" : ptr
);
769 "%s: disabling internal bitmap due to errors\n",
773 bitmap_file_put(bitmap
);
778 enum bitmap_page_attr
{
779 BITMAP_PAGE_DIRTY
= 0, // there are set bits that need to be synced
780 BITMAP_PAGE_CLEAN
= 1, // there are bits that might need to be cleared
781 BITMAP_PAGE_NEEDWRITE
=2, // there are cleared bits that need to be synced
784 static inline void set_page_attr(struct bitmap
*bitmap
, struct page
*page
,
785 enum bitmap_page_attr attr
)
787 __set_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
790 static inline void clear_page_attr(struct bitmap
*bitmap
, struct page
*page
,
791 enum bitmap_page_attr attr
)
793 __clear_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
796 static inline unsigned long test_page_attr(struct bitmap
*bitmap
, struct page
*page
,
797 enum bitmap_page_attr attr
)
799 return test_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
803 * bitmap_file_set_bit -- called before performing a write to the md device
804 * to set (and eventually sync) a particular bit in the bitmap file
806 * we set the bit immediately, then we record the page number so that
807 * when an unplug occurs, we can flush the dirty pages out to disk
809 static void bitmap_file_set_bit(struct bitmap
*bitmap
, sector_t block
)
814 unsigned long chunk
= block
>> CHUNK_BLOCK_SHIFT(bitmap
);
816 if (!bitmap
->filemap
) {
820 page
= filemap_get_page(bitmap
, chunk
);
822 bit
= file_page_offset(chunk
);
825 kaddr
= kmap_atomic(page
, KM_USER0
);
826 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
829 ext2_set_bit(bit
, kaddr
);
830 kunmap_atomic(kaddr
, KM_USER0
);
831 PRINTK("set file bit %lu page %lu\n", bit
, page
->index
);
833 /* record page number so it gets flushed to disk when unplug occurs */
834 set_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
838 /* this gets called when the md device is ready to unplug its underlying
839 * (slave) device queues -- before we let any writes go down, we need to
840 * sync the dirty pages of the bitmap file to disk */
841 void bitmap_unplug(struct bitmap
*bitmap
)
843 unsigned long i
, flags
;
844 int dirty
, need_write
;
851 /* look at each page to see if there are any set bits that need to be
852 * flushed out to disk */
853 for (i
= 0; i
< bitmap
->file_pages
; i
++) {
854 spin_lock_irqsave(&bitmap
->lock
, flags
);
855 if (!bitmap
->filemap
) {
856 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
859 page
= bitmap
->filemap
[i
];
860 dirty
= test_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
861 need_write
= test_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
862 clear_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
863 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
866 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
868 if (dirty
| need_write
)
869 write_page(bitmap
, page
, 0);
871 if (wait
) { /* if any writes were performed, we need to wait on them */
873 wait_event(bitmap
->write_wait
,
874 atomic_read(&bitmap
->pending_writes
)==0);
876 md_super_wait(bitmap
->mddev
);
878 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
879 bitmap_file_kick(bitmap
);
882 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
);
883 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
884 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
885 * memory mapping of the bitmap file
887 * if there's no bitmap file, or if the bitmap file had been
888 * previously kicked from the array, we mark all the bits as
889 * 1's in order to cause a full resync.
891 * We ignore all bits for sectors that end earlier than 'start'.
892 * This is used when reading an out-of-date bitmap...
894 static int bitmap_init_from_disk(struct bitmap
*bitmap
, sector_t start
)
896 unsigned long i
, chunks
, index
, oldindex
, bit
;
897 struct page
*page
= NULL
, *oldpage
= NULL
;
898 unsigned long num_pages
, bit_cnt
= 0;
900 unsigned long bytes
, offset
;
905 chunks
= bitmap
->chunks
;
908 BUG_ON(!file
&& !bitmap
->offset
);
910 #ifdef INJECT_FAULTS_3
913 outofdate
= bitmap
->flags
& BITMAP_STALE
;
916 printk(KERN_INFO
"%s: bitmap file is out of date, doing full "
917 "recovery\n", bmname(bitmap
));
919 bytes
= (chunks
+ 7) / 8;
921 num_pages
= (bytes
+ sizeof(bitmap_super_t
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
923 if (file
&& i_size_read(file
->f_mapping
->host
) < bytes
+ sizeof(bitmap_super_t
)) {
924 printk(KERN_INFO
"%s: bitmap file too short %lu < %lu\n",
926 (unsigned long) i_size_read(file
->f_mapping
->host
),
927 bytes
+ sizeof(bitmap_super_t
));
933 bitmap
->filemap
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
934 if (!bitmap
->filemap
)
937 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
938 bitmap
->filemap_attr
= kzalloc(
939 roundup( DIV_ROUND_UP(num_pages
*4, 8), sizeof(unsigned long)),
941 if (!bitmap
->filemap_attr
)
946 for (i
= 0; i
< chunks
; i
++) {
948 index
= file_page_index(i
);
949 bit
= file_page_offset(i
);
950 if (index
!= oldindex
) { /* this is a new page, read it in */
952 /* unmap the old page, we're done with it */
953 if (index
== num_pages
-1)
954 count
= bytes
+ sizeof(bitmap_super_t
)
960 * if we're here then the superblock page
961 * contains some bits (PAGE_SIZE != sizeof sb)
962 * we've already read it in, so just use it
964 page
= bitmap
->sb_page
;
965 offset
= sizeof(bitmap_super_t
);
967 read_sb_page(bitmap
->mddev
,
972 page
= read_page(file
, index
, bitmap
, count
);
975 page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
,
980 if (IS_ERR(page
)) { /* read error */
988 bitmap
->filemap
[bitmap
->file_pages
++] = page
;
989 bitmap
->last_page_size
= count
;
993 * if bitmap is out of date, dirty the
994 * whole page and write it out
996 paddr
= kmap_atomic(page
, KM_USER0
);
997 memset(paddr
+ offset
, 0xff,
999 kunmap_atomic(paddr
, KM_USER0
);
1000 write_page(bitmap
, page
, 1);
1003 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
1007 paddr
= kmap_atomic(page
, KM_USER0
);
1008 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1009 b
= test_bit(bit
, paddr
);
1011 b
= ext2_test_bit(bit
, paddr
);
1012 kunmap_atomic(paddr
, KM_USER0
);
1014 /* if the disk bit is set, set the memory bit */
1015 int needed
= ((sector_t
)(i
+1) << (CHUNK_BLOCK_SHIFT(bitmap
))
1017 bitmap_set_memory_bits(bitmap
,
1018 (sector_t
)i
<< CHUNK_BLOCK_SHIFT(bitmap
),
1021 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1025 /* everything went OK */
1027 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_UNSET
);
1029 if (bit_cnt
) { /* Kick recovery if any bits were set */
1030 set_bit(MD_RECOVERY_NEEDED
, &bitmap
->mddev
->recovery
);
1031 md_wakeup_thread(bitmap
->mddev
->thread
);
1034 printk(KERN_INFO
"%s: bitmap initialized from disk: "
1035 "read %lu/%lu pages, set %lu bits\n",
1036 bmname(bitmap
), bitmap
->file_pages
, num_pages
, bit_cnt
);
1041 printk(KERN_INFO
"%s: bitmap initialisation failed: %d\n",
1042 bmname(bitmap
), ret
);
1046 void bitmap_write_all(struct bitmap
*bitmap
)
1048 /* We don't actually write all bitmap blocks here,
1049 * just flag them as needing to be written
1053 for (i
=0; i
< bitmap
->file_pages
; i
++)
1054 set_page_attr(bitmap
, bitmap
->filemap
[i
],
1055 BITMAP_PAGE_NEEDWRITE
);
1059 static void bitmap_count_page(struct bitmap
*bitmap
, sector_t offset
, int inc
)
1061 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1062 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1063 bitmap
->bp
[page
].count
+= inc
;
1065 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1066 (unsigned long long)offset, inc, bitmap->bp[page].count);
1068 bitmap_checkfree(bitmap
, page
);
1070 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1071 sector_t offset
, int *blocks
,
1075 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1079 void bitmap_daemon_work(struct bitmap
*bitmap
)
1082 unsigned long flags
;
1083 struct page
*page
= NULL
, *lastpage
= NULL
;
1089 if (time_before(jiffies
, bitmap
->daemon_lastrun
+ bitmap
->daemon_sleep
*HZ
))
1092 bitmap
->daemon_lastrun
= jiffies
;
1093 if (bitmap
->allclean
) {
1094 bitmap
->mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1097 bitmap
->allclean
= 1;
1099 for (j
= 0; j
< bitmap
->chunks
; j
++) {
1100 bitmap_counter_t
*bmc
;
1101 spin_lock_irqsave(&bitmap
->lock
, flags
);
1102 if (!bitmap
->filemap
) {
1103 /* error or shutdown */
1104 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1108 page
= filemap_get_page(bitmap
, j
);
1110 if (page
!= lastpage
) {
1111 /* skip this page unless it's marked as needing cleaning */
1112 if (!test_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
)) {
1113 int need_write
= test_page_attr(bitmap
, page
,
1114 BITMAP_PAGE_NEEDWRITE
);
1116 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1118 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1120 write_page(bitmap
, page
, 0);
1121 bitmap
->allclean
= 0;
1126 /* grab the new page, sync and release the old */
1127 if (lastpage
!= NULL
) {
1128 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1129 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1130 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1131 write_page(bitmap
, lastpage
, 0);
1133 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1134 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1137 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1140 /* We are possibly going to clear some bits, so make
1141 * sure that events_cleared is up-to-date.
1143 if (bitmap
->need_sync
) {
1145 bitmap
->need_sync
= 0;
1146 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
1147 sb
->events_cleared
=
1148 cpu_to_le64(bitmap
->events_cleared
);
1149 kunmap_atomic(sb
, KM_USER0
);
1150 write_page(bitmap
, bitmap
->sb_page
, 1);
1152 spin_lock_irqsave(&bitmap
->lock
, flags
);
1153 clear_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1155 bmc
= bitmap_get_counter(bitmap
,
1156 (sector_t
)j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1160 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1163 bitmap
->allclean
= 0;
1166 *bmc
=1; /* maybe clear the bit next time */
1167 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1168 } else if (*bmc
== 1) {
1169 /* we can clear the bit */
1171 bitmap_count_page(bitmap
,
1172 (sector_t
)j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1176 paddr
= kmap_atomic(page
, KM_USER0
);
1177 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1178 clear_bit(file_page_offset(j
), paddr
);
1180 ext2_clear_bit(file_page_offset(j
), paddr
);
1181 kunmap_atomic(paddr
, KM_USER0
);
1184 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1187 /* now sync the final page */
1188 if (lastpage
!= NULL
) {
1189 spin_lock_irqsave(&bitmap
->lock
, flags
);
1190 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1191 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1192 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1193 write_page(bitmap
, lastpage
, 0);
1195 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1196 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1201 if (bitmap
->allclean
== 0)
1202 bitmap
->mddev
->thread
->timeout
= bitmap
->daemon_sleep
* HZ
;
1205 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1206 sector_t offset
, int *blocks
,
1209 /* If 'create', we might release the lock and reclaim it.
1210 * The lock must have been taken with interrupts enabled.
1211 * If !create, we don't release the lock.
1213 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1214 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1215 unsigned long pageoff
= (chunk
& PAGE_COUNTER_MASK
) << COUNTER_BYTE_SHIFT
;
1218 if (bitmap_checkpage(bitmap
, page
, create
) < 0) {
1219 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1220 *blocks
= csize
- (offset
& (csize
- 1));
1223 /* now locked ... */
1225 if (bitmap
->bp
[page
].hijacked
) { /* hijacked pointer */
1226 /* should we use the first or second counter field
1227 * of the hijacked pointer? */
1228 int hi
= (pageoff
> PAGE_COUNTER_MASK
);
1229 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
) +
1230 PAGE_COUNTER_SHIFT
- 1);
1231 *blocks
= csize
- (offset
& (csize
- 1));
1232 return &((bitmap_counter_t
*)
1233 &bitmap
->bp
[page
].map
)[hi
];
1234 } else { /* page is allocated */
1235 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1236 *blocks
= csize
- (offset
& (csize
- 1));
1237 return (bitmap_counter_t
*)
1238 &(bitmap
->bp
[page
].map
[pageoff
]);
1242 int bitmap_startwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
, int behind
)
1244 if (!bitmap
) return 0;
1247 atomic_inc(&bitmap
->behind_writes
);
1248 PRINTK(KERN_DEBUG
"inc write-behind count %d/%d\n",
1249 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1254 bitmap_counter_t
*bmc
;
1256 spin_lock_irq(&bitmap
->lock
);
1257 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 1);
1259 spin_unlock_irq(&bitmap
->lock
);
1263 if (unlikely((*bmc
& COUNTER_MAX
) == COUNTER_MAX
)) {
1264 DEFINE_WAIT(__wait
);
1265 /* note that it is safe to do the prepare_to_wait
1266 * after the test as long as we do it before dropping
1269 prepare_to_wait(&bitmap
->overflow_wait
, &__wait
,
1270 TASK_UNINTERRUPTIBLE
);
1271 spin_unlock_irq(&bitmap
->lock
);
1272 blk_unplug(bitmap
->mddev
->queue
);
1274 finish_wait(&bitmap
->overflow_wait
, &__wait
);
1280 bitmap_file_set_bit(bitmap
, offset
);
1281 bitmap_count_page(bitmap
,offset
, 1);
1282 blk_plug_device_unlocked(bitmap
->mddev
->queue
);
1290 spin_unlock_irq(&bitmap
->lock
);
1293 if (sectors
> blocks
)
1297 bitmap
->allclean
= 0;
1301 void bitmap_endwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
,
1302 int success
, int behind
)
1304 if (!bitmap
) return;
1306 atomic_dec(&bitmap
->behind_writes
);
1307 PRINTK(KERN_DEBUG
"dec write-behind count %d/%d\n",
1308 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1313 unsigned long flags
;
1314 bitmap_counter_t
*bmc
;
1316 spin_lock_irqsave(&bitmap
->lock
, flags
);
1317 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 0);
1319 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1324 bitmap
->events_cleared
< bitmap
->mddev
->events
) {
1325 bitmap
->events_cleared
= bitmap
->mddev
->events
;
1326 bitmap
->need_sync
= 1;
1329 if (!success
&& ! (*bmc
& NEEDED_MASK
))
1330 *bmc
|= NEEDED_MASK
;
1332 if ((*bmc
& COUNTER_MAX
) == COUNTER_MAX
)
1333 wake_up(&bitmap
->overflow_wait
);
1337 set_page_attr(bitmap
,
1338 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1341 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1343 if (sectors
> blocks
)
1349 int bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
,
1352 bitmap_counter_t
*bmc
;
1354 if (bitmap
== NULL
) {/* FIXME or bitmap set as 'failed' */
1356 return 1; /* always resync if no bitmap */
1358 spin_lock_irq(&bitmap
->lock
);
1359 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1365 else if (NEEDED(*bmc
)) {
1367 if (!degraded
) { /* don't set/clear bits if degraded */
1368 *bmc
|= RESYNC_MASK
;
1369 *bmc
&= ~NEEDED_MASK
;
1373 spin_unlock_irq(&bitmap
->lock
);
1374 bitmap
->allclean
= 0;
1378 void bitmap_end_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
, int aborted
)
1380 bitmap_counter_t
*bmc
;
1381 unsigned long flags
;
1383 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1384 */ if (bitmap
== NULL
) {
1388 spin_lock_irqsave(&bitmap
->lock
, flags
);
1389 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1394 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1397 *bmc
&= ~RESYNC_MASK
;
1399 if (!NEEDED(*bmc
) && aborted
)
1400 *bmc
|= NEEDED_MASK
;
1403 set_page_attr(bitmap
,
1404 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1410 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1411 bitmap
->allclean
= 0;
1414 void bitmap_close_sync(struct bitmap
*bitmap
)
1416 /* Sync has finished, and any bitmap chunks that weren't synced
1417 * properly have been aborted. It remains to us to clear the
1418 * RESYNC bit wherever it is still on
1420 sector_t sector
= 0;
1424 while (sector
< bitmap
->mddev
->resync_max_sectors
) {
1425 bitmap_end_sync(bitmap
, sector
, &blocks
, 0);
1430 void bitmap_cond_end_sync(struct bitmap
*bitmap
, sector_t sector
)
1438 bitmap
->last_end_sync
= jiffies
;
1441 if (time_before(jiffies
, (bitmap
->last_end_sync
1442 + bitmap
->daemon_sleep
* HZ
)))
1444 wait_event(bitmap
->mddev
->recovery_wait
,
1445 atomic_read(&bitmap
->mddev
->recovery_active
) == 0);
1447 sector
&= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap
)) - 1);
1449 while (s
< sector
&& s
< bitmap
->mddev
->resync_max_sectors
) {
1450 bitmap_end_sync(bitmap
, s
, &blocks
, 0);
1453 bitmap
->last_end_sync
= jiffies
;
1456 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
)
1458 /* For each chunk covered by any of these sectors, set the
1459 * counter to 1 and set resync_needed. They should all
1460 * be 0 at this point
1464 bitmap_counter_t
*bmc
;
1465 spin_lock_irq(&bitmap
->lock
);
1466 bmc
= bitmap_get_counter(bitmap
, offset
, &secs
, 1);
1468 spin_unlock_irq(&bitmap
->lock
);
1473 *bmc
= 1 | (needed
?NEEDED_MASK
:0);
1474 bitmap_count_page(bitmap
, offset
, 1);
1475 page
= filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
));
1476 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1478 spin_unlock_irq(&bitmap
->lock
);
1479 bitmap
->allclean
= 0;
1482 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1483 void bitmap_dirty_bits(struct bitmap
*bitmap
, unsigned long s
, unsigned long e
)
1485 unsigned long chunk
;
1487 for (chunk
= s
; chunk
<= e
; chunk
++) {
1488 sector_t sec
= (sector_t
)chunk
<< CHUNK_BLOCK_SHIFT(bitmap
);
1489 bitmap_set_memory_bits(bitmap
, sec
, 1);
1490 bitmap_file_set_bit(bitmap
, sec
);
1495 * flush out any pending updates
1497 void bitmap_flush(mddev_t
*mddev
)
1499 struct bitmap
*bitmap
= mddev
->bitmap
;
1502 if (!bitmap
) /* there was no bitmap */
1505 /* run the daemon_work three time to ensure everything is flushed
1508 sleep
= bitmap
->daemon_sleep
;
1509 bitmap
->daemon_sleep
= 0;
1510 bitmap_daemon_work(bitmap
);
1511 bitmap_daemon_work(bitmap
);
1512 bitmap_daemon_work(bitmap
);
1513 bitmap
->daemon_sleep
= sleep
;
1514 bitmap_update_sb(bitmap
);
1518 * free memory that was allocated
1520 static void bitmap_free(struct bitmap
*bitmap
)
1522 unsigned long k
, pages
;
1523 struct bitmap_page
*bp
;
1525 if (!bitmap
) /* there was no bitmap */
1528 /* release the bitmap file and kill the daemon */
1529 bitmap_file_put(bitmap
);
1532 pages
= bitmap
->pages
;
1534 /* free all allocated memory */
1536 if (bp
) /* deallocate the page memory */
1537 for (k
= 0; k
< pages
; k
++)
1538 if (bp
[k
].map
&& !bp
[k
].hijacked
)
1543 void bitmap_destroy(mddev_t
*mddev
)
1545 struct bitmap
*bitmap
= mddev
->bitmap
;
1547 if (!bitmap
) /* there was no bitmap */
1550 mddev
->bitmap
= NULL
; /* disconnect from the md device */
1552 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1554 bitmap_free(bitmap
);
1558 * initialize the bitmap structure
1559 * if this returns an error, bitmap_destroy must be called to do clean up
1561 int bitmap_create(mddev_t
*mddev
)
1563 struct bitmap
*bitmap
;
1564 unsigned long blocks
= mddev
->resync_max_sectors
;
1565 unsigned long chunks
;
1566 unsigned long pages
;
1567 struct file
*file
= mddev
->bitmap_file
;
1571 BUILD_BUG_ON(sizeof(bitmap_super_t
) != 256);
1573 if (!file
&& !mddev
->bitmap_offset
) /* bitmap disabled, nothing to do */
1576 BUG_ON(file
&& mddev
->bitmap_offset
);
1578 bitmap
= kzalloc(sizeof(*bitmap
), GFP_KERNEL
);
1582 spin_lock_init(&bitmap
->lock
);
1583 atomic_set(&bitmap
->pending_writes
, 0);
1584 init_waitqueue_head(&bitmap
->write_wait
);
1585 init_waitqueue_head(&bitmap
->overflow_wait
);
1587 bitmap
->mddev
= mddev
;
1589 bitmap
->file
= file
;
1590 bitmap
->offset
= mddev
->bitmap_offset
;
1593 do_sync_mapping_range(file
->f_mapping
, 0, LLONG_MAX
,
1594 SYNC_FILE_RANGE_WAIT_BEFORE
|
1595 SYNC_FILE_RANGE_WRITE
|
1596 SYNC_FILE_RANGE_WAIT_AFTER
);
1598 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1599 err
= bitmap_read_sb(bitmap
);
1603 bitmap
->chunkshift
= ffz(~bitmap
->chunksize
);
1605 /* now that chunksize and chunkshift are set, we can use these macros */
1606 chunks
= (blocks
+ CHUNK_BLOCK_RATIO(bitmap
) - 1) /
1607 CHUNK_BLOCK_RATIO(bitmap
);
1608 pages
= (chunks
+ PAGE_COUNTER_RATIO
- 1) / PAGE_COUNTER_RATIO
;
1612 bitmap
->chunks
= chunks
;
1613 bitmap
->pages
= pages
;
1614 bitmap
->missing_pages
= pages
;
1615 bitmap
->counter_bits
= COUNTER_BITS
;
1617 bitmap
->syncchunk
= ~0UL;
1619 #ifdef INJECT_FATAL_FAULT_1
1622 bitmap
->bp
= kzalloc(pages
* sizeof(*bitmap
->bp
), GFP_KERNEL
);
1628 /* now that we have some pages available, initialize the in-memory
1629 * bitmap from the on-disk bitmap */
1631 if (mddev
->degraded
== 0
1632 || bitmap
->events_cleared
== mddev
->events
)
1633 /* no need to keep dirty bits to optimise a re-add of a missing device */
1634 start
= mddev
->recovery_cp
;
1635 err
= bitmap_init_from_disk(bitmap
, start
);
1640 printk(KERN_INFO
"created bitmap (%lu pages) for device %s\n",
1641 pages
, bmname(bitmap
));
1643 mddev
->bitmap
= bitmap
;
1645 mddev
->thread
->timeout
= bitmap
->daemon_sleep
* HZ
;
1647 bitmap_update_sb(bitmap
);
1649 return (bitmap
->flags
& BITMAP_WRITE_ERROR
) ? -EIO
: 0;
1652 bitmap_free(bitmap
);
1656 /* the bitmap API -- for raid personalities */
1657 EXPORT_SYMBOL(bitmap_startwrite
);
1658 EXPORT_SYMBOL(bitmap_endwrite
);
1659 EXPORT_SYMBOL(bitmap_start_sync
);
1660 EXPORT_SYMBOL(bitmap_end_sync
);
1661 EXPORT_SYMBOL(bitmap_unplug
);
1662 EXPORT_SYMBOL(bitmap_close_sync
);
1663 EXPORT_SYMBOL(bitmap_cond_end_sync
);