2 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
4 * bitmap_create - sets up the bitmap structure
5 * bitmap_destroy - destroys the bitmap structure
7 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
8 * - added disk storage for bitmap
9 * - changes to allow various bitmap chunk sizes
15 * flush after percent set rather than just time based. (maybe both).
16 * wait if count gets too high, wake when it drops to half.
19 #include <linux/blkdev.h>
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/timer.h>
25 #include <linux/sched.h>
26 #include <linux/list.h>
27 #include <linux/file.h>
28 #include <linux/mount.h>
29 #include <linux/buffer_head.h>
38 /* these are for debugging purposes only! */
40 /* define one and only one of these */
41 #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
42 #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
43 #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
44 #define INJECT_FAULTS_4 0 /* undef */
45 #define INJECT_FAULTS_5 0 /* undef */
46 #define INJECT_FAULTS_6 0
48 /* if these are defined, the driver will fail! debug only */
49 #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
50 #define INJECT_FATAL_FAULT_2 0 /* undef */
51 #define INJECT_FATAL_FAULT_3 0 /* undef */
54 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
55 #define DPRINTK(x...) do { } while(0)
59 # define PRINTK(x...) printk(KERN_DEBUG x)
65 static inline char * bmname(struct bitmap
*bitmap
)
67 return bitmap
->mddev
? mdname(bitmap
->mddev
) : "mdX";
72 * just a placeholder - calls kmalloc for bitmap pages
74 static unsigned char *bitmap_alloc_page(struct bitmap
*bitmap
)
78 #ifdef INJECT_FAULTS_1
81 page
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
84 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap
));
86 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
87 bmname(bitmap
), page
);
92 * for now just a placeholder -- just calls kfree for bitmap pages
94 static void bitmap_free_page(struct bitmap
*bitmap
, unsigned char *page
)
96 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap
), page
);
101 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
103 * 1) check to see if this page is allocated, if it's not then try to alloc
104 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
105 * page pointer directly as a counter
107 * if we find our page, we increment the page's refcount so that it stays
108 * allocated while we're using it
110 static int bitmap_checkpage(struct bitmap
*bitmap
, unsigned long page
, int create
)
111 __releases(bitmap
->lock
)
112 __acquires(bitmap
->lock
)
114 unsigned char *mappage
;
116 if (page
>= bitmap
->pages
) {
117 /* This can happen if bitmap_start_sync goes beyond
118 * End-of-device while looking for a whole page.
125 if (bitmap
->bp
[page
].hijacked
) /* it's hijacked, don't try to alloc */
128 if (bitmap
->bp
[page
].map
) /* page is already allocated, just return */
134 spin_unlock_irq(&bitmap
->lock
);
136 /* this page has not been allocated yet */
138 if ((mappage
= bitmap_alloc_page(bitmap
)) == NULL
) {
139 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
141 /* failed - set the hijacked flag so that we can use the
142 * pointer as a counter */
143 spin_lock_irq(&bitmap
->lock
);
144 if (!bitmap
->bp
[page
].map
)
145 bitmap
->bp
[page
].hijacked
= 1;
151 spin_lock_irq(&bitmap
->lock
);
153 /* recheck the page */
155 if (bitmap
->bp
[page
].map
|| bitmap
->bp
[page
].hijacked
) {
156 /* somebody beat us to getting the page */
157 bitmap_free_page(bitmap
, mappage
);
161 /* no page was in place and we have one, so install it */
163 memset(mappage
, 0, PAGE_SIZE
);
164 bitmap
->bp
[page
].map
= mappage
;
165 bitmap
->missing_pages
--;
171 /* if page is completely empty, put it back on the free list, or dealloc it */
172 /* if page was hijacked, unmark the flag so it might get alloced next time */
173 /* Note: lock should be held when calling this */
174 static void bitmap_checkfree(struct bitmap
*bitmap
, unsigned long page
)
178 if (bitmap
->bp
[page
].count
) /* page is still busy */
181 /* page is no longer in use, it can be released */
183 if (bitmap
->bp
[page
].hijacked
) { /* page was hijacked, undo this now */
184 bitmap
->bp
[page
].hijacked
= 0;
185 bitmap
->bp
[page
].map
= NULL
;
189 /* normal case, free the page */
192 /* actually ... let's not. We will probably need the page again exactly when
193 * memory is tight and we are flusing to disk
197 ptr
= bitmap
->bp
[page
].map
;
198 bitmap
->bp
[page
].map
= NULL
;
199 bitmap
->missing_pages
++;
200 bitmap_free_page(bitmap
, ptr
);
207 * bitmap file handling - read and write the bitmap file and its superblock
211 * basic page I/O operations
214 /* IO operations when bitmap is stored near all superblocks */
215 static struct page
*read_sb_page(mddev_t
*mddev
, loff_t offset
,
217 unsigned long index
, int size
)
219 /* choose a good rdev and read the page from there */
225 page
= alloc_page(GFP_KERNEL
);
227 return ERR_PTR(-ENOMEM
);
229 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
230 if (! test_bit(In_sync
, &rdev
->flags
)
231 || test_bit(Faulty
, &rdev
->flags
))
234 target
= rdev
->sb_start
+ offset
+ index
* (PAGE_SIZE
/512);
236 if (sync_page_io(rdev
->bdev
, target
,
237 roundup(size
, bdev_logical_block_size(rdev
->bdev
)),
240 attach_page_buffers(page
, NULL
); /* so that free_buffer will
245 return ERR_PTR(-EIO
);
249 static mdk_rdev_t
*next_active_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
251 /* Iterate the disks of an mddev, using rcu to protect access to the
252 * linked list, and raising the refcount of devices we return to ensure
253 * they don't disappear while in use.
254 * As devices are only added or removed when raid_disk is < 0 and
255 * nr_pending is 0 and In_sync is clear, the entries we return will
256 * still be in the same position on the list when we re-enter
257 * list_for_each_continue_rcu.
259 struct list_head
*pos
;
262 /* start at the beginning */
265 /* release the previous rdev and start from there. */
266 rdev_dec_pending(rdev
, mddev
);
267 pos
= &rdev
->same_set
;
269 list_for_each_continue_rcu(pos
, &mddev
->disks
) {
270 rdev
= list_entry(pos
, mdk_rdev_t
, same_set
);
271 if (rdev
->raid_disk
>= 0 &&
272 !test_bit(Faulty
, &rdev
->flags
)) {
273 /* this is a usable devices */
274 atomic_inc(&rdev
->nr_pending
);
283 static int write_sb_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
285 mdk_rdev_t
*rdev
= NULL
;
286 mddev_t
*mddev
= bitmap
->mddev
;
288 while ((rdev
= next_active_rdev(rdev
, mddev
)) != NULL
) {
289 int size
= PAGE_SIZE
;
290 loff_t offset
= mddev
->bitmap_info
.offset
;
291 if (page
->index
== bitmap
->file_pages
-1)
292 size
= roundup(bitmap
->last_page_size
,
293 bdev_logical_block_size(rdev
->bdev
));
294 /* Just make sure we aren't corrupting data or
297 if (mddev
->external
) {
298 /* Bitmap could be anywhere. */
299 if (rdev
->sb_start
+ offset
+ (page
->index
*(PAGE_SIZE
/512)) >
301 rdev
->sb_start
+ offset
<
302 rdev
->data_offset
+ mddev
->dev_sectors
+
305 } else if (offset
< 0) {
306 /* DATA BITMAP METADATA */
308 + (long)(page
->index
* (PAGE_SIZE
/512))
310 /* bitmap runs in to metadata */
312 if (rdev
->data_offset
+ mddev
->dev_sectors
313 > rdev
->sb_start
+ offset
)
314 /* data runs in to bitmap */
316 } else if (rdev
->sb_start
< rdev
->data_offset
) {
317 /* METADATA BITMAP DATA */
320 + page
->index
*(PAGE_SIZE
/512) + size
/512
322 /* bitmap runs in to data */
325 /* DATA METADATA BITMAP - no problems */
327 md_super_write(mddev
, rdev
,
328 rdev
->sb_start
+ offset
329 + page
->index
* (PAGE_SIZE
/512),
335 md_super_wait(mddev
);
342 static void bitmap_file_kick(struct bitmap
*bitmap
);
344 * write out a page to a file
346 static void write_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
348 struct buffer_head
*bh
;
350 if (bitmap
->file
== NULL
) {
351 switch (write_sb_page(bitmap
, page
, wait
)) {
353 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
357 bh
= page_buffers(page
);
359 while (bh
&& bh
->b_blocknr
) {
360 atomic_inc(&bitmap
->pending_writes
);
361 set_buffer_locked(bh
);
362 set_buffer_mapped(bh
);
363 submit_bh(WRITE
, bh
);
364 bh
= bh
->b_this_page
;
368 wait_event(bitmap
->write_wait
,
369 atomic_read(&bitmap
->pending_writes
)==0);
372 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
373 bitmap_file_kick(bitmap
);
376 static void end_bitmap_write(struct buffer_head
*bh
, int uptodate
)
378 struct bitmap
*bitmap
= bh
->b_private
;
382 spin_lock_irqsave(&bitmap
->lock
, flags
);
383 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
384 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
386 if (atomic_dec_and_test(&bitmap
->pending_writes
))
387 wake_up(&bitmap
->write_wait
);
390 /* copied from buffer.c */
392 __clear_page_buffers(struct page
*page
)
394 ClearPagePrivate(page
);
395 set_page_private(page
, 0);
396 page_cache_release(page
);
398 static void free_buffers(struct page
*page
)
400 struct buffer_head
*bh
= page_buffers(page
);
403 struct buffer_head
*next
= bh
->b_this_page
;
404 free_buffer_head(bh
);
407 __clear_page_buffers(page
);
411 /* read a page from a file.
412 * We both read the page, and attach buffers to the page to record the
413 * address of each block (using bmap). These addresses will be used
414 * to write the block later, completely bypassing the filesystem.
415 * This usage is similar to how swap files are handled, and allows us
416 * to write to a file with no concerns of memory allocation failing.
418 static struct page
*read_page(struct file
*file
, unsigned long index
,
419 struct bitmap
*bitmap
,
422 struct page
*page
= NULL
;
423 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
424 struct buffer_head
*bh
;
427 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE
,
428 (unsigned long long)index
<< PAGE_SHIFT
);
430 page
= alloc_page(GFP_KERNEL
);
432 page
= ERR_PTR(-ENOMEM
);
436 bh
= alloc_page_buffers(page
, 1<<inode
->i_blkbits
, 0);
439 page
= ERR_PTR(-ENOMEM
);
442 attach_page_buffers(page
, bh
);
443 block
= index
<< (PAGE_SHIFT
- inode
->i_blkbits
);
448 bh
->b_blocknr
= bmap(inode
, block
);
449 if (bh
->b_blocknr
== 0) {
450 /* Cannot use this file! */
452 page
= ERR_PTR(-EINVAL
);
455 bh
->b_bdev
= inode
->i_sb
->s_bdev
;
456 if (count
< (1<<inode
->i_blkbits
))
459 count
-= (1<<inode
->i_blkbits
);
461 bh
->b_end_io
= end_bitmap_write
;
462 bh
->b_private
= bitmap
;
463 atomic_inc(&bitmap
->pending_writes
);
464 set_buffer_locked(bh
);
465 set_buffer_mapped(bh
);
469 bh
= bh
->b_this_page
;
473 wait_event(bitmap
->write_wait
,
474 atomic_read(&bitmap
->pending_writes
)==0);
475 if (bitmap
->flags
& BITMAP_WRITE_ERROR
) {
477 page
= ERR_PTR(-EIO
);
481 printk(KERN_ALERT
"md: bitmap read error: (%dB @ %Lu): %ld\n",
483 (unsigned long long)index
<< PAGE_SHIFT
,
489 * bitmap file superblock operations
492 /* update the event counter and sync the superblock to disk */
493 void bitmap_update_sb(struct bitmap
*bitmap
)
498 if (!bitmap
|| !bitmap
->mddev
) /* no bitmap for this array */
500 if (bitmap
->mddev
->bitmap_info
.external
)
502 spin_lock_irqsave(&bitmap
->lock
, flags
);
503 if (!bitmap
->sb_page
) { /* no superblock */
504 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
507 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
508 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
509 sb
->events
= cpu_to_le64(bitmap
->mddev
->events
);
510 if (bitmap
->mddev
->events
< bitmap
->events_cleared
) {
511 /* rocking back to read-only */
512 bitmap
->events_cleared
= bitmap
->mddev
->events
;
513 sb
->events_cleared
= cpu_to_le64(bitmap
->events_cleared
);
515 /* Just in case these have been changed via sysfs: */
516 sb
->daemon_sleep
= cpu_to_le32(bitmap
->mddev
->bitmap_info
.daemon_sleep
/HZ
);
517 sb
->write_behind
= cpu_to_le32(bitmap
->mddev
->bitmap_info
.max_write_behind
);
518 kunmap_atomic(sb
, KM_USER0
);
519 write_page(bitmap
, bitmap
->sb_page
, 1);
522 /* print out the bitmap file superblock */
523 void bitmap_print_sb(struct bitmap
*bitmap
)
527 if (!bitmap
|| !bitmap
->sb_page
)
529 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
530 printk(KERN_DEBUG
"%s: bitmap file superblock:\n", bmname(bitmap
));
531 printk(KERN_DEBUG
" magic: %08x\n", le32_to_cpu(sb
->magic
));
532 printk(KERN_DEBUG
" version: %d\n", le32_to_cpu(sb
->version
));
533 printk(KERN_DEBUG
" uuid: %08x.%08x.%08x.%08x\n",
534 *(__u32
*)(sb
->uuid
+0),
535 *(__u32
*)(sb
->uuid
+4),
536 *(__u32
*)(sb
->uuid
+8),
537 *(__u32
*)(sb
->uuid
+12));
538 printk(KERN_DEBUG
" events: %llu\n",
539 (unsigned long long) le64_to_cpu(sb
->events
));
540 printk(KERN_DEBUG
"events cleared: %llu\n",
541 (unsigned long long) le64_to_cpu(sb
->events_cleared
));
542 printk(KERN_DEBUG
" state: %08x\n", le32_to_cpu(sb
->state
));
543 printk(KERN_DEBUG
" chunksize: %d B\n", le32_to_cpu(sb
->chunksize
));
544 printk(KERN_DEBUG
" daemon sleep: %ds\n", le32_to_cpu(sb
->daemon_sleep
));
545 printk(KERN_DEBUG
" sync size: %llu KB\n",
546 (unsigned long long)le64_to_cpu(sb
->sync_size
)/2);
547 printk(KERN_DEBUG
"max write behind: %d\n", le32_to_cpu(sb
->write_behind
));
548 kunmap_atomic(sb
, KM_USER0
);
551 /* read the superblock from the bitmap file and initialize some bitmap fields */
552 static int bitmap_read_sb(struct bitmap
*bitmap
)
556 unsigned long chunksize
, daemon_sleep
, write_behind
;
557 unsigned long long events
;
560 /* page 0 is the superblock, read it... */
562 loff_t isize
= i_size_read(bitmap
->file
->f_mapping
->host
);
563 int bytes
= isize
> PAGE_SIZE
? PAGE_SIZE
: isize
;
565 bitmap
->sb_page
= read_page(bitmap
->file
, 0, bitmap
, bytes
);
567 bitmap
->sb_page
= read_sb_page(bitmap
->mddev
,
568 bitmap
->mddev
->bitmap_info
.offset
,
570 0, sizeof(bitmap_super_t
));
572 if (IS_ERR(bitmap
->sb_page
)) {
573 err
= PTR_ERR(bitmap
->sb_page
);
574 bitmap
->sb_page
= NULL
;
578 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
580 chunksize
= le32_to_cpu(sb
->chunksize
);
581 daemon_sleep
= le32_to_cpu(sb
->daemon_sleep
) * HZ
;
582 write_behind
= le32_to_cpu(sb
->write_behind
);
584 /* verify that the bitmap-specific fields are valid */
585 if (sb
->magic
!= cpu_to_le32(BITMAP_MAGIC
))
586 reason
= "bad magic";
587 else if (le32_to_cpu(sb
->version
) < BITMAP_MAJOR_LO
||
588 le32_to_cpu(sb
->version
) > BITMAP_MAJOR_HI
)
589 reason
= "unrecognized superblock version";
590 else if (chunksize
< 512)
591 reason
= "bitmap chunksize too small";
592 else if ((1 << ffz(~chunksize
)) != chunksize
)
593 reason
= "bitmap chunksize not a power of 2";
594 else if (daemon_sleep
< 1 || daemon_sleep
> MAX_SCHEDULE_TIMEOUT
)
595 reason
= "daemon sleep period out of range";
596 else if (write_behind
> COUNTER_MAX
)
597 reason
= "write-behind limit out of range (0 - 16383)";
599 printk(KERN_INFO
"%s: invalid bitmap file superblock: %s\n",
600 bmname(bitmap
), reason
);
604 /* keep the array size field of the bitmap superblock up to date */
605 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
607 if (!bitmap
->mddev
->persistent
)
611 * if we have a persistent array superblock, compare the
612 * bitmap's UUID and event counter to the mddev's
614 if (memcmp(sb
->uuid
, bitmap
->mddev
->uuid
, 16)) {
615 printk(KERN_INFO
"%s: bitmap superblock UUID mismatch\n",
619 events
= le64_to_cpu(sb
->events
);
620 if (events
< bitmap
->mddev
->events
) {
621 printk(KERN_INFO
"%s: bitmap file is out of date (%llu < %llu) "
622 "-- forcing full recovery\n", bmname(bitmap
), events
,
623 (unsigned long long) bitmap
->mddev
->events
);
624 sb
->state
|= cpu_to_le32(BITMAP_STALE
);
627 /* assign fields using values from superblock */
628 bitmap
->mddev
->bitmap_info
.chunksize
= chunksize
;
629 bitmap
->mddev
->bitmap_info
.daemon_sleep
= daemon_sleep
;
630 bitmap
->mddev
->bitmap_info
.max_write_behind
= write_behind
;
631 bitmap
->flags
|= le32_to_cpu(sb
->state
);
632 if (le32_to_cpu(sb
->version
) == BITMAP_MAJOR_HOSTENDIAN
)
633 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
634 bitmap
->events_cleared
= le64_to_cpu(sb
->events_cleared
);
635 if (sb
->state
& cpu_to_le32(BITMAP_STALE
))
636 bitmap
->events_cleared
= bitmap
->mddev
->events
;
639 kunmap_atomic(sb
, KM_USER0
);
641 bitmap_print_sb(bitmap
);
645 enum bitmap_mask_op
{
650 /* record the state of the bitmap in the superblock. Return the old value */
651 static int bitmap_mask_state(struct bitmap
*bitmap
, enum bitmap_state bits
,
652 enum bitmap_mask_op op
)
658 spin_lock_irqsave(&bitmap
->lock
, flags
);
659 if (!bitmap
->sb_page
) { /* can't set the state */
660 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
663 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
664 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
665 old
= le32_to_cpu(sb
->state
) & bits
;
667 case MASK_SET
: sb
->state
|= cpu_to_le32(bits
);
669 case MASK_UNSET
: sb
->state
&= cpu_to_le32(~bits
);
673 kunmap_atomic(sb
, KM_USER0
);
678 * general bitmap file operations
684 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
685 * file a page at a time. There's a superblock at the start of the file.
687 /* calculate the index of the page that contains this bit */
688 static inline unsigned long file_page_index(struct bitmap
*bitmap
, unsigned long chunk
)
690 if (!bitmap
->mddev
->bitmap_info
.external
)
691 chunk
+= sizeof(bitmap_super_t
) << 3;
692 return chunk
>> PAGE_BIT_SHIFT
;
695 /* calculate the (bit) offset of this bit within a page */
696 static inline unsigned long file_page_offset(struct bitmap
*bitmap
, unsigned long chunk
)
698 if (!bitmap
->mddev
->bitmap_info
.external
)
699 chunk
+= sizeof(bitmap_super_t
) << 3;
700 return chunk
& (PAGE_BITS
- 1);
704 * return a pointer to the page in the filemap that contains the given bit
706 * this lookup is complicated by the fact that the bitmap sb might be exactly
707 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
710 static inline struct page
*filemap_get_page(struct bitmap
*bitmap
,
713 if (file_page_index(bitmap
, chunk
) >= bitmap
->file_pages
) return NULL
;
714 return bitmap
->filemap
[file_page_index(bitmap
, chunk
)
715 - file_page_index(bitmap
, 0)];
719 static void bitmap_file_unmap(struct bitmap
*bitmap
)
721 struct page
**map
, *sb_page
;
726 spin_lock_irqsave(&bitmap
->lock
, flags
);
727 map
= bitmap
->filemap
;
728 bitmap
->filemap
= NULL
;
729 attr
= bitmap
->filemap_attr
;
730 bitmap
->filemap_attr
= NULL
;
731 pages
= bitmap
->file_pages
;
732 bitmap
->file_pages
= 0;
733 sb_page
= bitmap
->sb_page
;
734 bitmap
->sb_page
= NULL
;
735 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
738 if (map
[pages
] != sb_page
) /* 0 is sb_page, release it below */
739 free_buffers(map
[pages
]);
744 free_buffers(sb_page
);
747 static void bitmap_file_put(struct bitmap
*bitmap
)
752 spin_lock_irqsave(&bitmap
->lock
, flags
);
755 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
758 wait_event(bitmap
->write_wait
,
759 atomic_read(&bitmap
->pending_writes
)==0);
760 bitmap_file_unmap(bitmap
);
763 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
764 invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
771 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
772 * then it is no longer reliable, so we stop using it and we mark the file
773 * as failed in the superblock
775 static void bitmap_file_kick(struct bitmap
*bitmap
)
777 char *path
, *ptr
= NULL
;
779 if (bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_SET
) == 0) {
780 bitmap_update_sb(bitmap
);
783 path
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
785 ptr
= d_path(&bitmap
->file
->f_path
, path
,
790 "%s: kicking failed bitmap file %s from array!\n",
791 bmname(bitmap
), IS_ERR(ptr
) ? "" : ptr
);
796 "%s: disabling internal bitmap due to errors\n",
800 bitmap_file_put(bitmap
);
805 enum bitmap_page_attr
{
806 BITMAP_PAGE_DIRTY
= 0, // there are set bits that need to be synced
807 BITMAP_PAGE_CLEAN
= 1, // there are bits that might need to be cleared
808 BITMAP_PAGE_NEEDWRITE
=2, // there are cleared bits that need to be synced
811 static inline void set_page_attr(struct bitmap
*bitmap
, struct page
*page
,
812 enum bitmap_page_attr attr
)
814 __set_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
817 static inline void clear_page_attr(struct bitmap
*bitmap
, struct page
*page
,
818 enum bitmap_page_attr attr
)
820 __clear_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
823 static inline unsigned long test_page_attr(struct bitmap
*bitmap
, struct page
*page
,
824 enum bitmap_page_attr attr
)
826 return test_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
830 * bitmap_file_set_bit -- called before performing a write to the md device
831 * to set (and eventually sync) a particular bit in the bitmap file
833 * we set the bit immediately, then we record the page number so that
834 * when an unplug occurs, we can flush the dirty pages out to disk
836 static void bitmap_file_set_bit(struct bitmap
*bitmap
, sector_t block
)
841 unsigned long chunk
= block
>> CHUNK_BLOCK_SHIFT(bitmap
);
843 if (!bitmap
->filemap
) {
847 page
= filemap_get_page(bitmap
, chunk
);
849 bit
= file_page_offset(bitmap
, chunk
);
852 kaddr
= kmap_atomic(page
, KM_USER0
);
853 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
856 ext2_set_bit(bit
, kaddr
);
857 kunmap_atomic(kaddr
, KM_USER0
);
858 PRINTK("set file bit %lu page %lu\n", bit
, page
->index
);
860 /* record page number so it gets flushed to disk when unplug occurs */
861 set_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
865 /* this gets called when the md device is ready to unplug its underlying
866 * (slave) device queues -- before we let any writes go down, we need to
867 * sync the dirty pages of the bitmap file to disk */
868 void bitmap_unplug(struct bitmap
*bitmap
)
870 unsigned long i
, flags
;
871 int dirty
, need_write
;
878 /* look at each page to see if there are any set bits that need to be
879 * flushed out to disk */
880 for (i
= 0; i
< bitmap
->file_pages
; i
++) {
881 spin_lock_irqsave(&bitmap
->lock
, flags
);
882 if (!bitmap
->filemap
) {
883 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
886 page
= bitmap
->filemap
[i
];
887 dirty
= test_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
888 need_write
= test_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
889 clear_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
890 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
893 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
895 if (dirty
| need_write
)
896 write_page(bitmap
, page
, 0);
898 if (wait
) { /* if any writes were performed, we need to wait on them */
900 wait_event(bitmap
->write_wait
,
901 atomic_read(&bitmap
->pending_writes
)==0);
903 md_super_wait(bitmap
->mddev
);
905 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
906 bitmap_file_kick(bitmap
);
909 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
);
910 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
911 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
912 * memory mapping of the bitmap file
914 * if there's no bitmap file, or if the bitmap file had been
915 * previously kicked from the array, we mark all the bits as
916 * 1's in order to cause a full resync.
918 * We ignore all bits for sectors that end earlier than 'start'.
919 * This is used when reading an out-of-date bitmap...
921 static int bitmap_init_from_disk(struct bitmap
*bitmap
, sector_t start
)
923 unsigned long i
, chunks
, index
, oldindex
, bit
;
924 struct page
*page
= NULL
, *oldpage
= NULL
;
925 unsigned long num_pages
, bit_cnt
= 0;
927 unsigned long bytes
, offset
;
932 chunks
= bitmap
->chunks
;
935 BUG_ON(!file
&& !bitmap
->mddev
->bitmap_info
.offset
);
937 #ifdef INJECT_FAULTS_3
940 outofdate
= bitmap
->flags
& BITMAP_STALE
;
943 printk(KERN_INFO
"%s: bitmap file is out of date, doing full "
944 "recovery\n", bmname(bitmap
));
946 bytes
= (chunks
+ 7) / 8;
947 if (!bitmap
->mddev
->bitmap_info
.external
)
948 bytes
+= sizeof(bitmap_super_t
);
951 num_pages
= (bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
953 if (file
&& i_size_read(file
->f_mapping
->host
) < bytes
) {
954 printk(KERN_INFO
"%s: bitmap file too short %lu < %lu\n",
956 (unsigned long) i_size_read(file
->f_mapping
->host
),
963 bitmap
->filemap
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
964 if (!bitmap
->filemap
)
967 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
968 bitmap
->filemap_attr
= kzalloc(
969 roundup( DIV_ROUND_UP(num_pages
*4, 8), sizeof(unsigned long)),
971 if (!bitmap
->filemap_attr
)
976 for (i
= 0; i
< chunks
; i
++) {
978 index
= file_page_index(bitmap
, i
);
979 bit
= file_page_offset(bitmap
, i
);
980 if (index
!= oldindex
) { /* this is a new page, read it in */
982 /* unmap the old page, we're done with it */
983 if (index
== num_pages
-1)
984 count
= bytes
- index
* PAGE_SIZE
;
987 if (index
== 0 && bitmap
->sb_page
) {
989 * if we're here then the superblock page
990 * contains some bits (PAGE_SIZE != sizeof sb)
991 * we've already read it in, so just use it
993 page
= bitmap
->sb_page
;
994 offset
= sizeof(bitmap_super_t
);
996 read_sb_page(bitmap
->mddev
,
997 bitmap
->mddev
->bitmap_info
.offset
,
1001 page
= read_page(file
, index
, bitmap
, count
);
1004 page
= read_sb_page(bitmap
->mddev
,
1005 bitmap
->mddev
->bitmap_info
.offset
,
1010 if (IS_ERR(page
)) { /* read error */
1011 ret
= PTR_ERR(page
);
1018 bitmap
->filemap
[bitmap
->file_pages
++] = page
;
1019 bitmap
->last_page_size
= count
;
1023 * if bitmap is out of date, dirty the
1024 * whole page and write it out
1026 paddr
= kmap_atomic(page
, KM_USER0
);
1027 memset(paddr
+ offset
, 0xff,
1028 PAGE_SIZE
- offset
);
1029 kunmap_atomic(paddr
, KM_USER0
);
1030 write_page(bitmap
, page
, 1);
1033 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
1037 paddr
= kmap_atomic(page
, KM_USER0
);
1038 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1039 b
= test_bit(bit
, paddr
);
1041 b
= ext2_test_bit(bit
, paddr
);
1042 kunmap_atomic(paddr
, KM_USER0
);
1044 /* if the disk bit is set, set the memory bit */
1045 int needed
= ((sector_t
)(i
+1) << (CHUNK_BLOCK_SHIFT(bitmap
))
1047 bitmap_set_memory_bits(bitmap
,
1048 (sector_t
)i
<< CHUNK_BLOCK_SHIFT(bitmap
),
1051 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1055 /* everything went OK */
1057 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_UNSET
);
1059 if (bit_cnt
) { /* Kick recovery if any bits were set */
1060 set_bit(MD_RECOVERY_NEEDED
, &bitmap
->mddev
->recovery
);
1061 md_wakeup_thread(bitmap
->mddev
->thread
);
1064 printk(KERN_INFO
"%s: bitmap initialized from disk: "
1065 "read %lu/%lu pages, set %lu bits\n",
1066 bmname(bitmap
), bitmap
->file_pages
, num_pages
, bit_cnt
);
1071 printk(KERN_INFO
"%s: bitmap initialisation failed: %d\n",
1072 bmname(bitmap
), ret
);
1076 void bitmap_write_all(struct bitmap
*bitmap
)
1078 /* We don't actually write all bitmap blocks here,
1079 * just flag them as needing to be written
1083 for (i
=0; i
< bitmap
->file_pages
; i
++)
1084 set_page_attr(bitmap
, bitmap
->filemap
[i
],
1085 BITMAP_PAGE_NEEDWRITE
);
1089 static void bitmap_count_page(struct bitmap
*bitmap
, sector_t offset
, int inc
)
1091 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1092 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1093 bitmap
->bp
[page
].count
+= inc
;
1095 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1096 (unsigned long long)offset, inc, bitmap->bp[page].count);
1098 bitmap_checkfree(bitmap
, page
);
1100 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1101 sector_t offset
, int *blocks
,
1105 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1109 void bitmap_daemon_work(mddev_t
*mddev
)
1111 struct bitmap
*bitmap
;
1113 unsigned long flags
;
1114 struct page
*page
= NULL
, *lastpage
= NULL
;
1118 /* Use a mutex to guard daemon_work against
1121 mutex_lock(&mddev
->bitmap_info
.mutex
);
1122 bitmap
= mddev
->bitmap
;
1123 if (bitmap
== NULL
) {
1124 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1127 if (time_before(jiffies
, bitmap
->daemon_lastrun
1128 + bitmap
->mddev
->bitmap_info
.daemon_sleep
))
1131 bitmap
->daemon_lastrun
= jiffies
;
1132 if (bitmap
->allclean
) {
1133 bitmap
->mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1136 bitmap
->allclean
= 1;
1138 spin_lock_irqsave(&bitmap
->lock
, flags
);
1139 for (j
= 0; j
< bitmap
->chunks
; j
++) {
1140 bitmap_counter_t
*bmc
;
1141 if (!bitmap
->filemap
)
1142 /* error or shutdown */
1145 page
= filemap_get_page(bitmap
, j
);
1147 if (page
!= lastpage
) {
1148 /* skip this page unless it's marked as needing cleaning */
1149 if (!test_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
)) {
1150 int need_write
= test_page_attr(bitmap
, page
,
1151 BITMAP_PAGE_NEEDWRITE
);
1153 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1155 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1157 write_page(bitmap
, page
, 0);
1158 bitmap
->allclean
= 0;
1160 spin_lock_irqsave(&bitmap
->lock
, flags
);
1161 j
|= (PAGE_BITS
- 1);
1165 /* grab the new page, sync and release the old */
1166 if (lastpage
!= NULL
) {
1167 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1168 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1169 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1170 write_page(bitmap
, lastpage
, 0);
1172 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1173 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1176 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1179 /* We are possibly going to clear some bits, so make
1180 * sure that events_cleared is up-to-date.
1182 if (bitmap
->need_sync
&&
1183 bitmap
->mddev
->bitmap_info
.external
== 0) {
1185 bitmap
->need_sync
= 0;
1186 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
1187 sb
->events_cleared
=
1188 cpu_to_le64(bitmap
->events_cleared
);
1189 kunmap_atomic(sb
, KM_USER0
);
1190 write_page(bitmap
, bitmap
->sb_page
, 1);
1192 spin_lock_irqsave(&bitmap
->lock
, flags
);
1193 if (!bitmap
->need_sync
)
1194 clear_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1196 bmc
= bitmap_get_counter(bitmap
,
1197 (sector_t
)j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1201 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1204 bitmap
->allclean
= 0;
1207 *bmc
=1; /* maybe clear the bit next time */
1208 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1209 } else if (*bmc
== 1 && !bitmap
->need_sync
) {
1210 /* we can clear the bit */
1212 bitmap_count_page(bitmap
,
1213 (sector_t
)j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1217 paddr
= kmap_atomic(page
, KM_USER0
);
1218 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1219 clear_bit(file_page_offset(bitmap
, j
),
1222 ext2_clear_bit(file_page_offset(bitmap
, j
),
1224 kunmap_atomic(paddr
, KM_USER0
);
1227 j
|= PAGE_COUNTER_MASK
;
1229 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1231 /* now sync the final page */
1232 if (lastpage
!= NULL
) {
1233 spin_lock_irqsave(&bitmap
->lock
, flags
);
1234 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1235 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1236 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1237 write_page(bitmap
, lastpage
, 0);
1239 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1240 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1245 if (bitmap
->allclean
== 0)
1246 bitmap
->mddev
->thread
->timeout
=
1247 bitmap
->mddev
->bitmap_info
.daemon_sleep
;
1248 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1251 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1252 sector_t offset
, int *blocks
,
1254 __releases(bitmap
->lock
)
1255 __acquires(bitmap
->lock
)
1257 /* If 'create', we might release the lock and reclaim it.
1258 * The lock must have been taken with interrupts enabled.
1259 * If !create, we don't release the lock.
1261 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1262 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1263 unsigned long pageoff
= (chunk
& PAGE_COUNTER_MASK
) << COUNTER_BYTE_SHIFT
;
1266 if (bitmap_checkpage(bitmap
, page
, create
) < 0) {
1267 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1268 *blocks
= csize
- (offset
& (csize
- 1));
1271 /* now locked ... */
1273 if (bitmap
->bp
[page
].hijacked
) { /* hijacked pointer */
1274 /* should we use the first or second counter field
1275 * of the hijacked pointer? */
1276 int hi
= (pageoff
> PAGE_COUNTER_MASK
);
1277 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
) +
1278 PAGE_COUNTER_SHIFT
- 1);
1279 *blocks
= csize
- (offset
& (csize
- 1));
1280 return &((bitmap_counter_t
*)
1281 &bitmap
->bp
[page
].map
)[hi
];
1282 } else { /* page is allocated */
1283 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1284 *blocks
= csize
- (offset
& (csize
- 1));
1285 return (bitmap_counter_t
*)
1286 &(bitmap
->bp
[page
].map
[pageoff
]);
1290 int bitmap_startwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
, int behind
)
1292 if (!bitmap
) return 0;
1295 atomic_inc(&bitmap
->behind_writes
);
1296 PRINTK(KERN_DEBUG
"inc write-behind count %d/%d\n",
1297 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1302 bitmap_counter_t
*bmc
;
1304 spin_lock_irq(&bitmap
->lock
);
1305 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 1);
1307 spin_unlock_irq(&bitmap
->lock
);
1311 if (unlikely((*bmc
& COUNTER_MAX
) == COUNTER_MAX
)) {
1312 DEFINE_WAIT(__wait
);
1313 /* note that it is safe to do the prepare_to_wait
1314 * after the test as long as we do it before dropping
1317 prepare_to_wait(&bitmap
->overflow_wait
, &__wait
,
1318 TASK_UNINTERRUPTIBLE
);
1319 spin_unlock_irq(&bitmap
->lock
);
1320 blk_unplug(bitmap
->mddev
->queue
);
1322 finish_wait(&bitmap
->overflow_wait
, &__wait
);
1328 bitmap_file_set_bit(bitmap
, offset
);
1329 bitmap_count_page(bitmap
,offset
, 1);
1330 blk_plug_device_unlocked(bitmap
->mddev
->queue
);
1338 spin_unlock_irq(&bitmap
->lock
);
1341 if (sectors
> blocks
)
1345 bitmap
->allclean
= 0;
1349 void bitmap_endwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
,
1350 int success
, int behind
)
1352 if (!bitmap
) return;
1354 atomic_dec(&bitmap
->behind_writes
);
1355 PRINTK(KERN_DEBUG
"dec write-behind count %d/%d\n",
1356 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1358 if (bitmap
->mddev
->degraded
)
1359 /* Never clear bits or update events_cleared when degraded */
1364 unsigned long flags
;
1365 bitmap_counter_t
*bmc
;
1367 spin_lock_irqsave(&bitmap
->lock
, flags
);
1368 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 0);
1370 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1375 bitmap
->events_cleared
< bitmap
->mddev
->events
) {
1376 bitmap
->events_cleared
= bitmap
->mddev
->events
;
1377 bitmap
->need_sync
= 1;
1378 sysfs_notify_dirent(bitmap
->sysfs_can_clear
);
1381 if (!success
&& ! (*bmc
& NEEDED_MASK
))
1382 *bmc
|= NEEDED_MASK
;
1384 if ((*bmc
& COUNTER_MAX
) == COUNTER_MAX
)
1385 wake_up(&bitmap
->overflow_wait
);
1389 set_page_attr(bitmap
,
1390 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1393 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1395 if (sectors
> blocks
)
1401 static int __bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
,
1404 bitmap_counter_t
*bmc
;
1406 if (bitmap
== NULL
) {/* FIXME or bitmap set as 'failed' */
1408 return 1; /* always resync if no bitmap */
1410 spin_lock_irq(&bitmap
->lock
);
1411 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1417 else if (NEEDED(*bmc
)) {
1419 if (!degraded
) { /* don't set/clear bits if degraded */
1420 *bmc
|= RESYNC_MASK
;
1421 *bmc
&= ~NEEDED_MASK
;
1425 spin_unlock_irq(&bitmap
->lock
);
1426 bitmap
->allclean
= 0;
1430 int bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
,
1433 /* bitmap_start_sync must always report on multiples of whole
1434 * pages, otherwise resync (which is very PAGE_SIZE based) will
1436 * So call __bitmap_start_sync repeatedly (if needed) until
1437 * At least PAGE_SIZE>>9 blocks are covered.
1438 * Return the 'or' of the result.
1444 while (*blocks
< (PAGE_SIZE
>>9)) {
1445 rv
|= __bitmap_start_sync(bitmap
, offset
,
1446 &blocks1
, degraded
);
1453 void bitmap_end_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
, int aborted
)
1455 bitmap_counter_t
*bmc
;
1456 unsigned long flags
;
1458 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1459 */ if (bitmap
== NULL
) {
1463 spin_lock_irqsave(&bitmap
->lock
, flags
);
1464 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1469 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1472 *bmc
&= ~RESYNC_MASK
;
1474 if (!NEEDED(*bmc
) && aborted
)
1475 *bmc
|= NEEDED_MASK
;
1478 set_page_attr(bitmap
,
1479 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1485 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1486 bitmap
->allclean
= 0;
1489 void bitmap_close_sync(struct bitmap
*bitmap
)
1491 /* Sync has finished, and any bitmap chunks that weren't synced
1492 * properly have been aborted. It remains to us to clear the
1493 * RESYNC bit wherever it is still on
1495 sector_t sector
= 0;
1499 while (sector
< bitmap
->mddev
->resync_max_sectors
) {
1500 bitmap_end_sync(bitmap
, sector
, &blocks
, 0);
1505 void bitmap_cond_end_sync(struct bitmap
*bitmap
, sector_t sector
)
1513 bitmap
->last_end_sync
= jiffies
;
1516 if (time_before(jiffies
, (bitmap
->last_end_sync
1517 + bitmap
->mddev
->bitmap_info
.daemon_sleep
)))
1519 wait_event(bitmap
->mddev
->recovery_wait
,
1520 atomic_read(&bitmap
->mddev
->recovery_active
) == 0);
1522 bitmap
->mddev
->curr_resync_completed
= bitmap
->mddev
->curr_resync
;
1523 set_bit(MD_CHANGE_CLEAN
, &bitmap
->mddev
->flags
);
1524 sector
&= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap
)) - 1);
1526 while (s
< sector
&& s
< bitmap
->mddev
->resync_max_sectors
) {
1527 bitmap_end_sync(bitmap
, s
, &blocks
, 0);
1530 bitmap
->last_end_sync
= jiffies
;
1531 sysfs_notify(&bitmap
->mddev
->kobj
, NULL
, "sync_completed");
1534 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
)
1536 /* For each chunk covered by any of these sectors, set the
1537 * counter to 1 and set resync_needed. They should all
1538 * be 0 at this point
1542 bitmap_counter_t
*bmc
;
1543 spin_lock_irq(&bitmap
->lock
);
1544 bmc
= bitmap_get_counter(bitmap
, offset
, &secs
, 1);
1546 spin_unlock_irq(&bitmap
->lock
);
1551 *bmc
= 1 | (needed
?NEEDED_MASK
:0);
1552 bitmap_count_page(bitmap
, offset
, 1);
1553 page
= filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
));
1554 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1556 spin_unlock_irq(&bitmap
->lock
);
1557 bitmap
->allclean
= 0;
1560 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1561 void bitmap_dirty_bits(struct bitmap
*bitmap
, unsigned long s
, unsigned long e
)
1563 unsigned long chunk
;
1565 for (chunk
= s
; chunk
<= e
; chunk
++) {
1566 sector_t sec
= (sector_t
)chunk
<< CHUNK_BLOCK_SHIFT(bitmap
);
1567 bitmap_set_memory_bits(bitmap
, sec
, 1);
1568 bitmap_file_set_bit(bitmap
, sec
);
1569 if (sec
< bitmap
->mddev
->recovery_cp
)
1570 /* We are asserting that the array is dirty,
1571 * so move the recovery_cp address back so
1572 * that it is obvious that it is dirty
1574 bitmap
->mddev
->recovery_cp
= sec
;
1579 * flush out any pending updates
1581 void bitmap_flush(mddev_t
*mddev
)
1583 struct bitmap
*bitmap
= mddev
->bitmap
;
1586 if (!bitmap
) /* there was no bitmap */
1589 /* run the daemon_work three time to ensure everything is flushed
1592 sleep
= mddev
->bitmap_info
.daemon_sleep
* 2;
1593 bitmap
->daemon_lastrun
-= sleep
;
1594 bitmap_daemon_work(mddev
);
1595 bitmap
->daemon_lastrun
-= sleep
;
1596 bitmap_daemon_work(mddev
);
1597 bitmap
->daemon_lastrun
-= sleep
;
1598 bitmap_daemon_work(mddev
);
1599 bitmap_update_sb(bitmap
);
1603 * free memory that was allocated
1605 static void bitmap_free(struct bitmap
*bitmap
)
1607 unsigned long k
, pages
;
1608 struct bitmap_page
*bp
;
1610 if (!bitmap
) /* there was no bitmap */
1613 /* release the bitmap file and kill the daemon */
1614 bitmap_file_put(bitmap
);
1617 pages
= bitmap
->pages
;
1619 /* free all allocated memory */
1621 if (bp
) /* deallocate the page memory */
1622 for (k
= 0; k
< pages
; k
++)
1623 if (bp
[k
].map
&& !bp
[k
].hijacked
)
1629 void bitmap_destroy(mddev_t
*mddev
)
1631 struct bitmap
*bitmap
= mddev
->bitmap
;
1633 if (!bitmap
) /* there was no bitmap */
1636 mutex_lock(&mddev
->bitmap_info
.mutex
);
1637 mddev
->bitmap
= NULL
; /* disconnect from the md device */
1638 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1640 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1642 if (bitmap
->sysfs_can_clear
)
1643 sysfs_put(bitmap
->sysfs_can_clear
);
1645 bitmap_free(bitmap
);
1649 * initialize the bitmap structure
1650 * if this returns an error, bitmap_destroy must be called to do clean up
1652 int bitmap_create(mddev_t
*mddev
)
1654 struct bitmap
*bitmap
;
1655 sector_t blocks
= mddev
->resync_max_sectors
;
1656 unsigned long chunks
;
1657 unsigned long pages
;
1658 struct file
*file
= mddev
->bitmap_info
.file
;
1661 struct sysfs_dirent
*bm
;
1663 BUILD_BUG_ON(sizeof(bitmap_super_t
) != 256);
1665 if (!file
&& !mddev
->bitmap_info
.offset
) /* bitmap disabled, nothing to do */
1668 BUG_ON(file
&& mddev
->bitmap_info
.offset
);
1670 bitmap
= kzalloc(sizeof(*bitmap
), GFP_KERNEL
);
1674 spin_lock_init(&bitmap
->lock
);
1675 atomic_set(&bitmap
->pending_writes
, 0);
1676 init_waitqueue_head(&bitmap
->write_wait
);
1677 init_waitqueue_head(&bitmap
->overflow_wait
);
1679 bitmap
->mddev
= mddev
;
1681 bm
= sysfs_get_dirent(mddev
->kobj
.sd
, "bitmap");
1683 bitmap
->sysfs_can_clear
= sysfs_get_dirent(bm
, "can_clear");
1686 bitmap
->sysfs_can_clear
= NULL
;
1688 bitmap
->file
= file
;
1691 /* As future accesses to this file will use bmap,
1692 * and bypass the page cache, we must sync the file
1695 vfs_fsync(file
, file
->f_dentry
, 1);
1697 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
1698 if (!mddev
->bitmap_info
.external
)
1699 err
= bitmap_read_sb(bitmap
);
1702 if (mddev
->bitmap_info
.chunksize
== 0 ||
1703 mddev
->bitmap_info
.daemon_sleep
== 0)
1704 /* chunksize and time_base need to be
1711 bitmap
->daemon_lastrun
= jiffies
;
1712 bitmap
->chunkshift
= ffz(~mddev
->bitmap_info
.chunksize
);
1714 /* now that chunksize and chunkshift are set, we can use these macros */
1715 chunks
= (blocks
+ CHUNK_BLOCK_RATIO(bitmap
) - 1) >>
1716 CHUNK_BLOCK_SHIFT(bitmap
);
1717 pages
= (chunks
+ PAGE_COUNTER_RATIO
- 1) / PAGE_COUNTER_RATIO
;
1721 bitmap
->chunks
= chunks
;
1722 bitmap
->pages
= pages
;
1723 bitmap
->missing_pages
= pages
;
1724 bitmap
->counter_bits
= COUNTER_BITS
;
1726 bitmap
->syncchunk
= ~0UL;
1728 #ifdef INJECT_FATAL_FAULT_1
1731 bitmap
->bp
= kzalloc(pages
* sizeof(*bitmap
->bp
), GFP_KERNEL
);
1737 /* now that we have some pages available, initialize the in-memory
1738 * bitmap from the on-disk bitmap */
1740 if (mddev
->degraded
== 0
1741 || bitmap
->events_cleared
== mddev
->events
)
1742 /* no need to keep dirty bits to optimise a re-add of a missing device */
1743 start
= mddev
->recovery_cp
;
1744 err
= bitmap_init_from_disk(bitmap
, start
);
1749 printk(KERN_INFO
"created bitmap (%lu pages) for device %s\n",
1750 pages
, bmname(bitmap
));
1752 mddev
->bitmap
= bitmap
;
1754 mddev
->thread
->timeout
= mddev
->bitmap_info
.daemon_sleep
;
1755 md_wakeup_thread(mddev
->thread
);
1757 bitmap_update_sb(bitmap
);
1759 return (bitmap
->flags
& BITMAP_WRITE_ERROR
) ? -EIO
: 0;
1762 bitmap_free(bitmap
);
1767 location_show(mddev_t
*mddev
, char *page
)
1770 if (mddev
->bitmap_info
.file
) {
1771 len
= sprintf(page
, "file");
1772 } else if (mddev
->bitmap_info
.offset
) {
1773 len
= sprintf(page
, "%+lld", (long long)mddev
->bitmap_info
.offset
);
1775 len
= sprintf(page
, "none");
1776 len
+= sprintf(page
+len
, "\n");
1781 location_store(mddev_t
*mddev
, const char *buf
, size_t len
)
1785 if (!mddev
->pers
->quiesce
)
1787 if (mddev
->recovery
|| mddev
->sync_thread
)
1791 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
||
1792 mddev
->bitmap_info
.offset
) {
1793 /* bitmap already configured. Only option is to clear it */
1794 if (strncmp(buf
, "none", 4) != 0)
1797 mddev
->pers
->quiesce(mddev
, 1);
1798 bitmap_destroy(mddev
);
1799 mddev
->pers
->quiesce(mddev
, 0);
1801 mddev
->bitmap_info
.offset
= 0;
1802 if (mddev
->bitmap_info
.file
) {
1803 struct file
*f
= mddev
->bitmap_info
.file
;
1804 mddev
->bitmap_info
.file
= NULL
;
1805 restore_bitmap_write_access(f
);
1809 /* No bitmap, OK to set a location */
1811 if (strncmp(buf
, "none", 4) == 0)
1812 /* nothing to be done */;
1813 else if (strncmp(buf
, "file:", 5) == 0) {
1814 /* Not supported yet */
1819 rv
= strict_strtoll(buf
+1, 10, &offset
);
1821 rv
= strict_strtoll(buf
, 10, &offset
);
1826 if (mddev
->bitmap_info
.external
== 0 &&
1827 mddev
->major_version
== 0 &&
1828 offset
!= mddev
->bitmap_info
.default_offset
)
1830 mddev
->bitmap_info
.offset
= offset
;
1832 mddev
->pers
->quiesce(mddev
, 1);
1833 rv
= bitmap_create(mddev
);
1835 bitmap_destroy(mddev
);
1836 mddev
->bitmap_info
.offset
= 0;
1838 mddev
->pers
->quiesce(mddev
, 0);
1844 if (!mddev
->external
) {
1845 /* Ensure new bitmap info is stored in
1846 * metadata promptly.
1848 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1849 md_wakeup_thread(mddev
->thread
);
1854 static struct md_sysfs_entry bitmap_location
=
1855 __ATTR(location
, S_IRUGO
|S_IWUSR
, location_show
, location_store
);
1858 timeout_show(mddev_t
*mddev
, char *page
)
1861 unsigned long secs
= mddev
->bitmap_info
.daemon_sleep
/ HZ
;
1862 unsigned long jifs
= mddev
->bitmap_info
.daemon_sleep
% HZ
;
1864 len
= sprintf(page
, "%lu", secs
);
1866 len
+= sprintf(page
+len
, ".%03u", jiffies_to_msecs(jifs
));
1867 len
+= sprintf(page
+len
, "\n");
1872 timeout_store(mddev_t
*mddev
, const char *buf
, size_t len
)
1874 /* timeout can be set at any time */
1875 unsigned long timeout
;
1876 int rv
= strict_strtoul_scaled(buf
, &timeout
, 4);
1880 /* just to make sure we don't overflow... */
1881 if (timeout
>= LONG_MAX
/ HZ
)
1884 timeout
= timeout
* HZ
/ 10000;
1886 if (timeout
>= MAX_SCHEDULE_TIMEOUT
)
1887 timeout
= MAX_SCHEDULE_TIMEOUT
-1;
1890 mddev
->bitmap_info
.daemon_sleep
= timeout
;
1891 if (mddev
->thread
) {
1892 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1893 * the bitmap is all clean and we don't need to
1894 * adjust the timeout right now
1896 if (mddev
->thread
->timeout
< MAX_SCHEDULE_TIMEOUT
) {
1897 mddev
->thread
->timeout
= timeout
;
1898 md_wakeup_thread(mddev
->thread
);
1904 static struct md_sysfs_entry bitmap_timeout
=
1905 __ATTR(time_base
, S_IRUGO
|S_IWUSR
, timeout_show
, timeout_store
);
1908 backlog_show(mddev_t
*mddev
, char *page
)
1910 return sprintf(page
, "%lu\n", mddev
->bitmap_info
.max_write_behind
);
1914 backlog_store(mddev_t
*mddev
, const char *buf
, size_t len
)
1916 unsigned long backlog
;
1917 int rv
= strict_strtoul(buf
, 10, &backlog
);
1920 if (backlog
> COUNTER_MAX
)
1922 mddev
->bitmap_info
.max_write_behind
= backlog
;
1926 static struct md_sysfs_entry bitmap_backlog
=
1927 __ATTR(backlog
, S_IRUGO
|S_IWUSR
, backlog_show
, backlog_store
);
1930 chunksize_show(mddev_t
*mddev
, char *page
)
1932 return sprintf(page
, "%lu\n", mddev
->bitmap_info
.chunksize
);
1936 chunksize_store(mddev_t
*mddev
, const char *buf
, size_t len
)
1938 /* Can only be changed when no bitmap is active */
1940 unsigned long csize
;
1943 rv
= strict_strtoul(buf
, 10, &csize
);
1947 !is_power_of_2(csize
))
1949 mddev
->bitmap_info
.chunksize
= csize
;
1953 static struct md_sysfs_entry bitmap_chunksize
=
1954 __ATTR(chunksize
, S_IRUGO
|S_IWUSR
, chunksize_show
, chunksize_store
);
1956 static ssize_t
metadata_show(mddev_t
*mddev
, char *page
)
1958 return sprintf(page
, "%s\n", (mddev
->bitmap_info
.external
1959 ? "external" : "internal"));
1962 static ssize_t
metadata_store(mddev_t
*mddev
, const char *buf
, size_t len
)
1964 if (mddev
->bitmap
||
1965 mddev
->bitmap_info
.file
||
1966 mddev
->bitmap_info
.offset
)
1968 if (strncmp(buf
, "external", 8) == 0)
1969 mddev
->bitmap_info
.external
= 1;
1970 else if (strncmp(buf
, "internal", 8) == 0)
1971 mddev
->bitmap_info
.external
= 0;
1977 static struct md_sysfs_entry bitmap_metadata
=
1978 __ATTR(metadata
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
1980 static ssize_t
can_clear_show(mddev_t
*mddev
, char *page
)
1984 len
= sprintf(page
, "%s\n", (mddev
->bitmap
->need_sync
?
1987 len
= sprintf(page
, "\n");
1991 static ssize_t
can_clear_store(mddev_t
*mddev
, const char *buf
, size_t len
)
1993 if (mddev
->bitmap
== NULL
)
1995 if (strncmp(buf
, "false", 5) == 0)
1996 mddev
->bitmap
->need_sync
= 1;
1997 else if (strncmp(buf
, "true", 4) == 0) {
1998 if (mddev
->degraded
)
2000 mddev
->bitmap
->need_sync
= 0;
2006 static struct md_sysfs_entry bitmap_can_clear
=
2007 __ATTR(can_clear
, S_IRUGO
|S_IWUSR
, can_clear_show
, can_clear_store
);
2009 static struct attribute
*md_bitmap_attrs
[] = {
2010 &bitmap_location
.attr
,
2011 &bitmap_timeout
.attr
,
2012 &bitmap_backlog
.attr
,
2013 &bitmap_chunksize
.attr
,
2014 &bitmap_metadata
.attr
,
2015 &bitmap_can_clear
.attr
,
2018 struct attribute_group md_bitmap_group
= {
2020 .attrs
= md_bitmap_attrs
,
2024 /* the bitmap API -- for raid personalities */
2025 EXPORT_SYMBOL(bitmap_startwrite
);
2026 EXPORT_SYMBOL(bitmap_endwrite
);
2027 EXPORT_SYMBOL(bitmap_start_sync
);
2028 EXPORT_SYMBOL(bitmap_end_sync
);
2029 EXPORT_SYMBOL(bitmap_unplug
);
2030 EXPORT_SYMBOL(bitmap_close_sync
);
2031 EXPORT_SYMBOL(bitmap_cond_end_sync
);