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).
18 #include <linux/blkdev.h>
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>
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 static inline char *bmname(struct bitmap
*bitmap
)
55 return bitmap
->mddev
? mdname(bitmap
->mddev
) : "mdX";
59 * just a placeholder - calls kmalloc for bitmap pages
61 static unsigned char *bitmap_alloc_page(struct bitmap
*bitmap
)
65 #ifdef INJECT_FAULTS_1
68 page
= kzalloc(PAGE_SIZE
, GFP_NOIO
);
71 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap
));
73 pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
74 bmname(bitmap
), page
);
79 * for now just a placeholder -- just calls kfree for bitmap pages
81 static void bitmap_free_page(struct bitmap
*bitmap
, unsigned char *page
)
83 pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap
), page
);
88 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
90 * 1) check to see if this page is allocated, if it's not then try to alloc
91 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
92 * page pointer directly as a counter
94 * if we find our page, we increment the page's refcount so that it stays
95 * allocated while we're using it
97 static int bitmap_checkpage(struct bitmap
*bitmap
,
98 unsigned long page
, int create
)
99 __releases(bitmap
->lock
)
100 __acquires(bitmap
->lock
)
102 unsigned char *mappage
;
104 if (page
>= bitmap
->pages
) {
105 /* This can happen if bitmap_start_sync goes beyond
106 * End-of-device while looking for a whole page.
112 if (bitmap
->bp
[page
].hijacked
) /* it's hijacked, don't try to alloc */
115 if (bitmap
->bp
[page
].map
) /* page is already allocated, just return */
121 /* this page has not been allocated yet */
123 spin_unlock_irq(&bitmap
->lock
);
124 mappage
= bitmap_alloc_page(bitmap
);
125 spin_lock_irq(&bitmap
->lock
);
127 if (mappage
== NULL
) {
128 pr_debug("%s: bitmap map page allocation failed, hijacking\n",
130 /* failed - set the hijacked flag so that we can use the
131 * pointer as a counter */
132 if (!bitmap
->bp
[page
].map
)
133 bitmap
->bp
[page
].hijacked
= 1;
134 } else if (bitmap
->bp
[page
].map
||
135 bitmap
->bp
[page
].hijacked
) {
136 /* somebody beat us to getting the page */
137 bitmap_free_page(bitmap
, mappage
);
141 /* no page was in place and we have one, so install it */
143 bitmap
->bp
[page
].map
= mappage
;
144 bitmap
->missing_pages
--;
149 /* if page is completely empty, put it back on the free list, or dealloc it */
150 /* if page was hijacked, unmark the flag so it might get alloced next time */
151 /* Note: lock should be held when calling this */
152 static void bitmap_checkfree(struct bitmap
*bitmap
, unsigned long page
)
156 if (bitmap
->bp
[page
].count
) /* page is still busy */
159 /* page is no longer in use, it can be released */
161 if (bitmap
->bp
[page
].hijacked
) { /* page was hijacked, undo this now */
162 bitmap
->bp
[page
].hijacked
= 0;
163 bitmap
->bp
[page
].map
= NULL
;
165 /* normal case, free the page */
166 ptr
= bitmap
->bp
[page
].map
;
167 bitmap
->bp
[page
].map
= NULL
;
168 bitmap
->missing_pages
++;
169 bitmap_free_page(bitmap
, ptr
);
174 * bitmap file handling - read and write the bitmap file and its superblock
178 * basic page I/O operations
181 /* IO operations when bitmap is stored near all superblocks */
182 static struct page
*read_sb_page(struct mddev
*mddev
, loff_t offset
,
184 unsigned long index
, int size
)
186 /* choose a good rdev and read the page from there */
188 struct md_rdev
*rdev
;
193 page
= alloc_page(GFP_KERNEL
);
195 return ERR_PTR(-ENOMEM
);
199 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
200 if (! test_bit(In_sync
, &rdev
->flags
)
201 || test_bit(Faulty
, &rdev
->flags
))
204 target
= offset
+ index
* (PAGE_SIZE
/512);
206 if (sync_page_io(rdev
, target
,
207 roundup(size
, bdev_logical_block_size(rdev
->bdev
)),
210 attach_page_buffers(page
, NULL
); /* so that free_buffer will
217 return ERR_PTR(-EIO
);
221 static struct md_rdev
*next_active_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
223 /* Iterate the disks of an mddev, using rcu to protect access to the
224 * linked list, and raising the refcount of devices we return to ensure
225 * they don't disappear while in use.
226 * As devices are only added or removed when raid_disk is < 0 and
227 * nr_pending is 0 and In_sync is clear, the entries we return will
228 * still be in the same position on the list when we re-enter
229 * list_for_each_continue_rcu.
231 struct list_head
*pos
;
234 /* start at the beginning */
237 /* release the previous rdev and start from there. */
238 rdev_dec_pending(rdev
, mddev
);
239 pos
= &rdev
->same_set
;
241 list_for_each_continue_rcu(pos
, &mddev
->disks
) {
242 rdev
= list_entry(pos
, struct md_rdev
, same_set
);
243 if (rdev
->raid_disk
>= 0 &&
244 !test_bit(Faulty
, &rdev
->flags
)) {
245 /* this is a usable devices */
246 atomic_inc(&rdev
->nr_pending
);
255 static int write_sb_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
257 struct md_rdev
*rdev
= NULL
;
258 struct block_device
*bdev
;
259 struct mddev
*mddev
= bitmap
->mddev
;
261 while ((rdev
= next_active_rdev(rdev
, mddev
)) != NULL
) {
262 int size
= PAGE_SIZE
;
263 loff_t offset
= mddev
->bitmap_info
.offset
;
265 bdev
= (rdev
->meta_bdev
) ? rdev
->meta_bdev
: rdev
->bdev
;
267 if (page
->index
== bitmap
->file_pages
-1)
268 size
= roundup(bitmap
->last_page_size
,
269 bdev_logical_block_size(bdev
));
270 /* Just make sure we aren't corrupting data or
273 if (mddev
->external
) {
274 /* Bitmap could be anywhere. */
275 if (rdev
->sb_start
+ offset
+ (page
->index
279 rdev
->sb_start
+ offset
280 < (rdev
->data_offset
+ mddev
->dev_sectors
283 } else if (offset
< 0) {
284 /* DATA BITMAP METADATA */
286 + (long)(page
->index
* (PAGE_SIZE
/512))
288 /* bitmap runs in to metadata */
290 if (rdev
->data_offset
+ mddev
->dev_sectors
291 > rdev
->sb_start
+ offset
)
292 /* data runs in to bitmap */
294 } else if (rdev
->sb_start
< rdev
->data_offset
) {
295 /* METADATA BITMAP DATA */
298 + page
->index
*(PAGE_SIZE
/512) + size
/512
300 /* bitmap runs in to data */
303 /* DATA METADATA BITMAP - no problems */
305 md_super_write(mddev
, rdev
,
306 rdev
->sb_start
+ offset
307 + page
->index
* (PAGE_SIZE
/512),
313 md_super_wait(mddev
);
320 static void bitmap_file_kick(struct bitmap
*bitmap
);
322 * write out a page to a file
324 static void write_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
326 struct buffer_head
*bh
;
328 if (bitmap
->file
== NULL
) {
329 switch (write_sb_page(bitmap
, page
, wait
)) {
331 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
335 bh
= page_buffers(page
);
337 while (bh
&& bh
->b_blocknr
) {
338 atomic_inc(&bitmap
->pending_writes
);
339 set_buffer_locked(bh
);
340 set_buffer_mapped(bh
);
341 submit_bh(WRITE
| REQ_SYNC
, bh
);
342 bh
= bh
->b_this_page
;
346 wait_event(bitmap
->write_wait
,
347 atomic_read(&bitmap
->pending_writes
)==0);
349 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
350 bitmap_file_kick(bitmap
);
353 static void end_bitmap_write(struct buffer_head
*bh
, int uptodate
)
355 struct bitmap
*bitmap
= bh
->b_private
;
359 spin_lock_irqsave(&bitmap
->lock
, flags
);
360 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
361 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
363 if (atomic_dec_and_test(&bitmap
->pending_writes
))
364 wake_up(&bitmap
->write_wait
);
367 /* copied from buffer.c */
369 __clear_page_buffers(struct page
*page
)
371 ClearPagePrivate(page
);
372 set_page_private(page
, 0);
373 page_cache_release(page
);
375 static void free_buffers(struct page
*page
)
377 struct buffer_head
*bh
= page_buffers(page
);
380 struct buffer_head
*next
= bh
->b_this_page
;
381 free_buffer_head(bh
);
384 __clear_page_buffers(page
);
388 /* read a page from a file.
389 * We both read the page, and attach buffers to the page to record the
390 * address of each block (using bmap). These addresses will be used
391 * to write the block later, completely bypassing the filesystem.
392 * This usage is similar to how swap files are handled, and allows us
393 * to write to a file with no concerns of memory allocation failing.
395 static struct page
*read_page(struct file
*file
, unsigned long index
,
396 struct bitmap
*bitmap
,
399 struct page
*page
= NULL
;
400 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
401 struct buffer_head
*bh
;
404 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE
,
405 (unsigned long long)index
<< PAGE_SHIFT
);
407 page
= alloc_page(GFP_KERNEL
);
409 page
= ERR_PTR(-ENOMEM
);
413 bh
= alloc_page_buffers(page
, 1<<inode
->i_blkbits
, 0);
416 page
= ERR_PTR(-ENOMEM
);
419 attach_page_buffers(page
, bh
);
420 block
= index
<< (PAGE_SHIFT
- inode
->i_blkbits
);
425 bh
->b_blocknr
= bmap(inode
, block
);
426 if (bh
->b_blocknr
== 0) {
427 /* Cannot use this file! */
429 page
= ERR_PTR(-EINVAL
);
432 bh
->b_bdev
= inode
->i_sb
->s_bdev
;
433 if (count
< (1<<inode
->i_blkbits
))
436 count
-= (1<<inode
->i_blkbits
);
438 bh
->b_end_io
= end_bitmap_write
;
439 bh
->b_private
= bitmap
;
440 atomic_inc(&bitmap
->pending_writes
);
441 set_buffer_locked(bh
);
442 set_buffer_mapped(bh
);
446 bh
= bh
->b_this_page
;
450 wait_event(bitmap
->write_wait
,
451 atomic_read(&bitmap
->pending_writes
)==0);
452 if (bitmap
->flags
& BITMAP_WRITE_ERROR
) {
454 page
= ERR_PTR(-EIO
);
458 printk(KERN_ALERT
"md: bitmap read error: (%dB @ %llu): %ld\n",
460 (unsigned long long)index
<< PAGE_SHIFT
,
466 * bitmap file superblock operations
469 /* update the event counter and sync the superblock to disk */
470 void bitmap_update_sb(struct bitmap
*bitmap
)
475 if (!bitmap
|| !bitmap
->mddev
) /* no bitmap for this array */
477 if (bitmap
->mddev
->bitmap_info
.external
)
479 spin_lock_irqsave(&bitmap
->lock
, flags
);
480 if (!bitmap
->sb_page
) { /* no superblock */
481 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
484 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
485 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
486 sb
->events
= cpu_to_le64(bitmap
->mddev
->events
);
487 if (bitmap
->mddev
->events
< bitmap
->events_cleared
)
488 /* rocking back to read-only */
489 bitmap
->events_cleared
= bitmap
->mddev
->events
;
490 sb
->events_cleared
= cpu_to_le64(bitmap
->events_cleared
);
491 sb
->state
= cpu_to_le32(bitmap
->flags
);
492 /* Just in case these have been changed via sysfs: */
493 sb
->daemon_sleep
= cpu_to_le32(bitmap
->mddev
->bitmap_info
.daemon_sleep
/HZ
);
494 sb
->write_behind
= cpu_to_le32(bitmap
->mddev
->bitmap_info
.max_write_behind
);
495 kunmap_atomic(sb
, KM_USER0
);
496 write_page(bitmap
, bitmap
->sb_page
, 1);
499 /* print out the bitmap file superblock */
500 void bitmap_print_sb(struct bitmap
*bitmap
)
504 if (!bitmap
|| !bitmap
->sb_page
)
506 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
507 printk(KERN_DEBUG
"%s: bitmap file superblock:\n", bmname(bitmap
));
508 printk(KERN_DEBUG
" magic: %08x\n", le32_to_cpu(sb
->magic
));
509 printk(KERN_DEBUG
" version: %d\n", le32_to_cpu(sb
->version
));
510 printk(KERN_DEBUG
" uuid: %08x.%08x.%08x.%08x\n",
511 *(__u32
*)(sb
->uuid
+0),
512 *(__u32
*)(sb
->uuid
+4),
513 *(__u32
*)(sb
->uuid
+8),
514 *(__u32
*)(sb
->uuid
+12));
515 printk(KERN_DEBUG
" events: %llu\n",
516 (unsigned long long) le64_to_cpu(sb
->events
));
517 printk(KERN_DEBUG
"events cleared: %llu\n",
518 (unsigned long long) le64_to_cpu(sb
->events_cleared
));
519 printk(KERN_DEBUG
" state: %08x\n", le32_to_cpu(sb
->state
));
520 printk(KERN_DEBUG
" chunksize: %d B\n", le32_to_cpu(sb
->chunksize
));
521 printk(KERN_DEBUG
" daemon sleep: %ds\n", le32_to_cpu(sb
->daemon_sleep
));
522 printk(KERN_DEBUG
" sync size: %llu KB\n",
523 (unsigned long long)le64_to_cpu(sb
->sync_size
)/2);
524 printk(KERN_DEBUG
"max write behind: %d\n", le32_to_cpu(sb
->write_behind
));
525 kunmap_atomic(sb
, KM_USER0
);
532 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
533 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
534 * This function verifies 'bitmap_info' and populates the on-disk bitmap
535 * structure, which is to be written to disk.
537 * Returns: 0 on success, -Exxx on error
539 static int bitmap_new_disk_sb(struct bitmap
*bitmap
)
542 unsigned long chunksize
, daemon_sleep
, write_behind
;
545 bitmap
->sb_page
= alloc_page(GFP_KERNEL
);
546 if (IS_ERR(bitmap
->sb_page
)) {
547 err
= PTR_ERR(bitmap
->sb_page
);
548 bitmap
->sb_page
= NULL
;
551 bitmap
->sb_page
->index
= 0;
553 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
555 sb
->magic
= cpu_to_le32(BITMAP_MAGIC
);
556 sb
->version
= cpu_to_le32(BITMAP_MAJOR_HI
);
558 chunksize
= bitmap
->mddev
->bitmap_info
.chunksize
;
560 if (!is_power_of_2(chunksize
)) {
561 kunmap_atomic(sb
, KM_USER0
);
562 printk(KERN_ERR
"bitmap chunksize not a power of 2\n");
565 sb
->chunksize
= cpu_to_le32(chunksize
);
567 daemon_sleep
= bitmap
->mddev
->bitmap_info
.daemon_sleep
;
569 (daemon_sleep
< 1) || (daemon_sleep
> MAX_SCHEDULE_TIMEOUT
)) {
570 printk(KERN_INFO
"Choosing daemon_sleep default (5 sec)\n");
571 daemon_sleep
= 5 * HZ
;
573 sb
->daemon_sleep
= cpu_to_le32(daemon_sleep
);
574 bitmap
->mddev
->bitmap_info
.daemon_sleep
= daemon_sleep
;
577 * FIXME: write_behind for RAID1. If not specified, what
578 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
580 write_behind
= bitmap
->mddev
->bitmap_info
.max_write_behind
;
581 if (write_behind
> COUNTER_MAX
)
582 write_behind
= COUNTER_MAX
/ 2;
583 sb
->write_behind
= cpu_to_le32(write_behind
);
584 bitmap
->mddev
->bitmap_info
.max_write_behind
= write_behind
;
586 /* keep the array size field of the bitmap superblock up to date */
587 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
589 memcpy(sb
->uuid
, bitmap
->mddev
->uuid
, 16);
591 bitmap
->flags
|= BITMAP_STALE
;
592 sb
->state
|= cpu_to_le32(BITMAP_STALE
);
593 bitmap
->events_cleared
= bitmap
->mddev
->events
;
594 sb
->events_cleared
= cpu_to_le64(bitmap
->mddev
->events
);
596 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
597 sb
->version
= cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN
);
599 kunmap_atomic(sb
, KM_USER0
);
604 /* read the superblock from the bitmap file and initialize some bitmap fields */
605 static int bitmap_read_sb(struct bitmap
*bitmap
)
609 unsigned long chunksize
, daemon_sleep
, write_behind
;
610 unsigned long long events
;
613 /* page 0 is the superblock, read it... */
615 loff_t isize
= i_size_read(bitmap
->file
->f_mapping
->host
);
616 int bytes
= isize
> PAGE_SIZE
? PAGE_SIZE
: isize
;
618 bitmap
->sb_page
= read_page(bitmap
->file
, 0, bitmap
, bytes
);
620 bitmap
->sb_page
= read_sb_page(bitmap
->mddev
,
621 bitmap
->mddev
->bitmap_info
.offset
,
623 0, sizeof(bitmap_super_t
));
625 if (IS_ERR(bitmap
->sb_page
)) {
626 err
= PTR_ERR(bitmap
->sb_page
);
627 bitmap
->sb_page
= NULL
;
631 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
633 chunksize
= le32_to_cpu(sb
->chunksize
);
634 daemon_sleep
= le32_to_cpu(sb
->daemon_sleep
) * HZ
;
635 write_behind
= le32_to_cpu(sb
->write_behind
);
637 /* verify that the bitmap-specific fields are valid */
638 if (sb
->magic
!= cpu_to_le32(BITMAP_MAGIC
))
639 reason
= "bad magic";
640 else if (le32_to_cpu(sb
->version
) < BITMAP_MAJOR_LO
||
641 le32_to_cpu(sb
->version
) > BITMAP_MAJOR_HI
)
642 reason
= "unrecognized superblock version";
643 else if (chunksize
< 512)
644 reason
= "bitmap chunksize too small";
645 else if (!is_power_of_2(chunksize
))
646 reason
= "bitmap chunksize not a power of 2";
647 else if (daemon_sleep
< 1 || daemon_sleep
> MAX_SCHEDULE_TIMEOUT
)
648 reason
= "daemon sleep period out of range";
649 else if (write_behind
> COUNTER_MAX
)
650 reason
= "write-behind limit out of range (0 - 16383)";
652 printk(KERN_INFO
"%s: invalid bitmap file superblock: %s\n",
653 bmname(bitmap
), reason
);
657 /* keep the array size field of the bitmap superblock up to date */
658 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
660 if (!bitmap
->mddev
->persistent
)
664 * if we have a persistent array superblock, compare the
665 * bitmap's UUID and event counter to the mddev's
667 if (memcmp(sb
->uuid
, bitmap
->mddev
->uuid
, 16)) {
668 printk(KERN_INFO
"%s: bitmap superblock UUID mismatch\n",
672 events
= le64_to_cpu(sb
->events
);
673 if (events
< bitmap
->mddev
->events
) {
674 printk(KERN_INFO
"%s: bitmap file is out of date (%llu < %llu) "
675 "-- forcing full recovery\n", bmname(bitmap
), events
,
676 (unsigned long long) bitmap
->mddev
->events
);
677 sb
->state
|= cpu_to_le32(BITMAP_STALE
);
680 /* assign fields using values from superblock */
681 bitmap
->mddev
->bitmap_info
.chunksize
= chunksize
;
682 bitmap
->mddev
->bitmap_info
.daemon_sleep
= daemon_sleep
;
683 bitmap
->mddev
->bitmap_info
.max_write_behind
= write_behind
;
684 bitmap
->flags
|= le32_to_cpu(sb
->state
);
685 if (le32_to_cpu(sb
->version
) == BITMAP_MAJOR_HOSTENDIAN
)
686 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
687 bitmap
->events_cleared
= le64_to_cpu(sb
->events_cleared
);
688 if (bitmap
->flags
& BITMAP_STALE
)
689 bitmap
->events_cleared
= bitmap
->mddev
->events
;
692 kunmap_atomic(sb
, KM_USER0
);
694 bitmap_print_sb(bitmap
);
698 enum bitmap_mask_op
{
703 /* record the state of the bitmap in the superblock. Return the old value */
704 static int bitmap_mask_state(struct bitmap
*bitmap
, enum bitmap_state bits
,
705 enum bitmap_mask_op op
)
711 spin_lock_irqsave(&bitmap
->lock
, flags
);
712 if (!bitmap
->sb_page
) { /* can't set the state */
713 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
716 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
717 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
718 old
= le32_to_cpu(sb
->state
) & bits
;
721 sb
->state
|= cpu_to_le32(bits
);
722 bitmap
->flags
|= bits
;
725 sb
->state
&= cpu_to_le32(~bits
);
726 bitmap
->flags
&= ~bits
;
731 kunmap_atomic(sb
, KM_USER0
);
736 * general bitmap file operations
742 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
743 * file a page at a time. There's a superblock at the start of the file.
745 /* calculate the index of the page that contains this bit */
746 static inline unsigned long file_page_index(struct bitmap
*bitmap
, unsigned long chunk
)
748 if (!bitmap
->mddev
->bitmap_info
.external
)
749 chunk
+= sizeof(bitmap_super_t
) << 3;
750 return chunk
>> PAGE_BIT_SHIFT
;
753 /* calculate the (bit) offset of this bit within a page */
754 static inline unsigned long file_page_offset(struct bitmap
*bitmap
, unsigned long chunk
)
756 if (!bitmap
->mddev
->bitmap_info
.external
)
757 chunk
+= sizeof(bitmap_super_t
) << 3;
758 return chunk
& (PAGE_BITS
- 1);
762 * return a pointer to the page in the filemap that contains the given bit
764 * this lookup is complicated by the fact that the bitmap sb might be exactly
765 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
768 static inline struct page
*filemap_get_page(struct bitmap
*bitmap
,
771 if (file_page_index(bitmap
, chunk
) >= bitmap
->file_pages
)
773 return bitmap
->filemap
[file_page_index(bitmap
, chunk
)
774 - file_page_index(bitmap
, 0)];
777 static void bitmap_file_unmap(struct bitmap
*bitmap
)
779 struct page
**map
, *sb_page
;
784 spin_lock_irqsave(&bitmap
->lock
, flags
);
785 map
= bitmap
->filemap
;
786 bitmap
->filemap
= NULL
;
787 attr
= bitmap
->filemap_attr
;
788 bitmap
->filemap_attr
= NULL
;
789 pages
= bitmap
->file_pages
;
790 bitmap
->file_pages
= 0;
791 sb_page
= bitmap
->sb_page
;
792 bitmap
->sb_page
= NULL
;
793 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
796 if (map
[pages
] != sb_page
) /* 0 is sb_page, release it below */
797 free_buffers(map
[pages
]);
802 free_buffers(sb_page
);
805 static void bitmap_file_put(struct bitmap
*bitmap
)
810 spin_lock_irqsave(&bitmap
->lock
, flags
);
813 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
816 wait_event(bitmap
->write_wait
,
817 atomic_read(&bitmap
->pending_writes
)==0);
818 bitmap_file_unmap(bitmap
);
821 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
822 invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
828 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
829 * then it is no longer reliable, so we stop using it and we mark the file
830 * as failed in the superblock
832 static void bitmap_file_kick(struct bitmap
*bitmap
)
834 char *path
, *ptr
= NULL
;
836 if (bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_SET
) == 0) {
837 bitmap_update_sb(bitmap
);
840 path
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
842 ptr
= d_path(&bitmap
->file
->f_path
, path
,
846 "%s: kicking failed bitmap file %s from array!\n",
847 bmname(bitmap
), IS_ERR(ptr
) ? "" : ptr
);
852 "%s: disabling internal bitmap due to errors\n",
856 bitmap_file_put(bitmap
);
861 enum bitmap_page_attr
{
862 BITMAP_PAGE_DIRTY
= 0, /* there are set bits that need to be synced */
863 BITMAP_PAGE_PENDING
= 1, /* there are bits that are being cleaned.
864 * i.e. counter is 1 or 2. */
865 BITMAP_PAGE_NEEDWRITE
= 2, /* there are cleared bits that need to be synced */
868 static inline void set_page_attr(struct bitmap
*bitmap
, struct page
*page
,
869 enum bitmap_page_attr attr
)
871 __set_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
874 static inline void clear_page_attr(struct bitmap
*bitmap
, struct page
*page
,
875 enum bitmap_page_attr attr
)
877 __clear_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
880 static inline unsigned long test_page_attr(struct bitmap
*bitmap
, struct page
*page
,
881 enum bitmap_page_attr attr
)
883 return test_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
887 * bitmap_file_set_bit -- called before performing a write to the md device
888 * to set (and eventually sync) a particular bit in the bitmap file
890 * we set the bit immediately, then we record the page number so that
891 * when an unplug occurs, we can flush the dirty pages out to disk
893 static void bitmap_file_set_bit(struct bitmap
*bitmap
, sector_t block
)
898 unsigned long chunk
= block
>> CHUNK_BLOCK_SHIFT(bitmap
);
900 if (!bitmap
->filemap
)
903 page
= filemap_get_page(bitmap
, chunk
);
906 bit
= file_page_offset(bitmap
, chunk
);
909 kaddr
= kmap_atomic(page
, KM_USER0
);
910 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
913 __set_bit_le(bit
, kaddr
);
914 kunmap_atomic(kaddr
, KM_USER0
);
915 pr_debug("set file bit %lu page %lu\n", bit
, page
->index
);
916 /* record page number so it gets flushed to disk when unplug occurs */
917 set_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
920 /* this gets called when the md device is ready to unplug its underlying
921 * (slave) device queues -- before we let any writes go down, we need to
922 * sync the dirty pages of the bitmap file to disk */
923 void bitmap_unplug(struct bitmap
*bitmap
)
925 unsigned long i
, flags
;
926 int dirty
, need_write
;
933 /* look at each page to see if there are any set bits that need to be
934 * flushed out to disk */
935 for (i
= 0; i
< bitmap
->file_pages
; i
++) {
936 spin_lock_irqsave(&bitmap
->lock
, flags
);
937 if (!bitmap
->filemap
) {
938 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
941 page
= bitmap
->filemap
[i
];
942 dirty
= test_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
943 need_write
= test_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
944 clear_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
945 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
948 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
950 if (dirty
|| need_write
)
951 write_page(bitmap
, page
, 0);
953 if (wait
) { /* if any writes were performed, we need to wait on them */
955 wait_event(bitmap
->write_wait
,
956 atomic_read(&bitmap
->pending_writes
)==0);
958 md_super_wait(bitmap
->mddev
);
960 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
961 bitmap_file_kick(bitmap
);
963 EXPORT_SYMBOL(bitmap_unplug
);
965 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
);
966 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
967 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
968 * memory mapping of the bitmap file
970 * if there's no bitmap file, or if the bitmap file had been
971 * previously kicked from the array, we mark all the bits as
972 * 1's in order to cause a full resync.
974 * We ignore all bits for sectors that end earlier than 'start'.
975 * This is used when reading an out-of-date bitmap...
977 static int bitmap_init_from_disk(struct bitmap
*bitmap
, sector_t start
)
979 unsigned long i
, chunks
, index
, oldindex
, bit
;
980 struct page
*page
= NULL
, *oldpage
= NULL
;
981 unsigned long num_pages
, bit_cnt
= 0;
983 unsigned long bytes
, offset
;
988 chunks
= bitmap
->chunks
;
991 BUG_ON(!file
&& !bitmap
->mddev
->bitmap_info
.offset
);
993 #ifdef INJECT_FAULTS_3
996 outofdate
= bitmap
->flags
& BITMAP_STALE
;
999 printk(KERN_INFO
"%s: bitmap file is out of date, doing full "
1000 "recovery\n", bmname(bitmap
));
1002 bytes
= DIV_ROUND_UP(bitmap
->chunks
, 8);
1003 if (!bitmap
->mddev
->bitmap_info
.external
)
1004 bytes
+= sizeof(bitmap_super_t
);
1006 num_pages
= DIV_ROUND_UP(bytes
, PAGE_SIZE
);
1008 if (file
&& i_size_read(file
->f_mapping
->host
) < bytes
) {
1009 printk(KERN_INFO
"%s: bitmap file too short %lu < %lu\n",
1011 (unsigned long) i_size_read(file
->f_mapping
->host
),
1018 bitmap
->filemap
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
1019 if (!bitmap
->filemap
)
1022 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
1023 bitmap
->filemap_attr
= kzalloc(
1024 roundup(DIV_ROUND_UP(num_pages
*4, 8), sizeof(unsigned long)),
1026 if (!bitmap
->filemap_attr
)
1031 for (i
= 0; i
< chunks
; i
++) {
1033 index
= file_page_index(bitmap
, i
);
1034 bit
= file_page_offset(bitmap
, i
);
1035 if (index
!= oldindex
) { /* this is a new page, read it in */
1037 /* unmap the old page, we're done with it */
1038 if (index
== num_pages
-1)
1039 count
= bytes
- index
* PAGE_SIZE
;
1042 if (index
== 0 && bitmap
->sb_page
) {
1044 * if we're here then the superblock page
1045 * contains some bits (PAGE_SIZE != sizeof sb)
1046 * we've already read it in, so just use it
1048 page
= bitmap
->sb_page
;
1049 offset
= sizeof(bitmap_super_t
);
1051 page
= read_sb_page(
1053 bitmap
->mddev
->bitmap_info
.offset
,
1057 page
= read_page(file
, index
, bitmap
, count
);
1060 page
= read_sb_page(bitmap
->mddev
,
1061 bitmap
->mddev
->bitmap_info
.offset
,
1066 if (IS_ERR(page
)) { /* read error */
1067 ret
= PTR_ERR(page
);
1074 bitmap
->filemap
[bitmap
->file_pages
++] = page
;
1075 bitmap
->last_page_size
= count
;
1079 * if bitmap is out of date, dirty the
1080 * whole page and write it out
1082 paddr
= kmap_atomic(page
, KM_USER0
);
1083 memset(paddr
+ offset
, 0xff,
1084 PAGE_SIZE
- offset
);
1085 kunmap_atomic(paddr
, KM_USER0
);
1086 write_page(bitmap
, page
, 1);
1089 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
1093 paddr
= kmap_atomic(page
, KM_USER0
);
1094 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1095 b
= test_bit(bit
, paddr
);
1097 b
= test_bit_le(bit
, paddr
);
1098 kunmap_atomic(paddr
, KM_USER0
);
1100 /* if the disk bit is set, set the memory bit */
1101 int needed
= ((sector_t
)(i
+1) << (CHUNK_BLOCK_SHIFT(bitmap
))
1103 bitmap_set_memory_bits(bitmap
,
1104 (sector_t
)i
<< CHUNK_BLOCK_SHIFT(bitmap
),
1110 /* everything went OK */
1112 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_UNSET
);
1114 if (bit_cnt
) { /* Kick recovery if any bits were set */
1115 set_bit(MD_RECOVERY_NEEDED
, &bitmap
->mddev
->recovery
);
1116 md_wakeup_thread(bitmap
->mddev
->thread
);
1119 printk(KERN_INFO
"%s: bitmap initialized from disk: "
1120 "read %lu/%lu pages, set %lu of %lu bits\n",
1121 bmname(bitmap
), bitmap
->file_pages
, num_pages
, bit_cnt
, chunks
);
1126 printk(KERN_INFO
"%s: bitmap initialisation failed: %d\n",
1127 bmname(bitmap
), ret
);
1131 void bitmap_write_all(struct bitmap
*bitmap
)
1133 /* We don't actually write all bitmap blocks here,
1134 * just flag them as needing to be written
1138 for (i
= 0; i
< bitmap
->file_pages
; i
++)
1139 set_page_attr(bitmap
, bitmap
->filemap
[i
],
1140 BITMAP_PAGE_NEEDWRITE
);
1141 bitmap
->allclean
= 0;
1144 static void bitmap_count_page(struct bitmap
*bitmap
, sector_t offset
, int inc
)
1146 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1147 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1148 bitmap
->bp
[page
].count
+= inc
;
1149 bitmap_checkfree(bitmap
, page
);
1151 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1152 sector_t offset
, sector_t
*blocks
,
1156 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1160 void bitmap_daemon_work(struct mddev
*mddev
)
1162 struct bitmap
*bitmap
;
1164 unsigned long flags
;
1165 struct page
*page
= NULL
, *lastpage
= NULL
;
1169 /* Use a mutex to guard daemon_work against
1172 mutex_lock(&mddev
->bitmap_info
.mutex
);
1173 bitmap
= mddev
->bitmap
;
1174 if (bitmap
== NULL
) {
1175 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1178 if (time_before(jiffies
, bitmap
->daemon_lastrun
1179 + bitmap
->mddev
->bitmap_info
.daemon_sleep
))
1182 bitmap
->daemon_lastrun
= jiffies
;
1183 if (bitmap
->allclean
) {
1184 bitmap
->mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1187 bitmap
->allclean
= 1;
1189 spin_lock_irqsave(&bitmap
->lock
, flags
);
1190 for (j
= 0; j
< bitmap
->chunks
; j
++) {
1191 bitmap_counter_t
*bmc
;
1192 if (!bitmap
->filemap
)
1193 /* error or shutdown */
1196 page
= filemap_get_page(bitmap
, j
);
1198 if (page
!= lastpage
) {
1199 /* skip this page unless it's marked as needing cleaning */
1200 if (!test_page_attr(bitmap
, page
, BITMAP_PAGE_PENDING
)) {
1201 int need_write
= test_page_attr(bitmap
, page
,
1202 BITMAP_PAGE_NEEDWRITE
);
1204 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1206 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1208 write_page(bitmap
, page
, 0);
1209 spin_lock_irqsave(&bitmap
->lock
, flags
);
1210 j
|= (PAGE_BITS
- 1);
1214 /* grab the new page, sync and release the old */
1215 if (lastpage
!= NULL
) {
1216 if (test_page_attr(bitmap
, lastpage
,
1217 BITMAP_PAGE_NEEDWRITE
)) {
1218 clear_page_attr(bitmap
, lastpage
,
1219 BITMAP_PAGE_NEEDWRITE
);
1220 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1221 write_page(bitmap
, lastpage
, 0);
1223 set_page_attr(bitmap
, lastpage
,
1224 BITMAP_PAGE_NEEDWRITE
);
1225 bitmap
->allclean
= 0;
1226 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1229 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1232 /* We are possibly going to clear some bits, so make
1233 * sure that events_cleared is up-to-date.
1235 if (bitmap
->need_sync
&&
1236 bitmap
->mddev
->bitmap_info
.external
== 0) {
1238 bitmap
->need_sync
= 0;
1239 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
1240 sb
->events_cleared
=
1241 cpu_to_le64(bitmap
->events_cleared
);
1242 kunmap_atomic(sb
, KM_USER0
);
1243 write_page(bitmap
, bitmap
->sb_page
, 1);
1245 spin_lock_irqsave(&bitmap
->lock
, flags
);
1246 if (!bitmap
->need_sync
)
1247 clear_page_attr(bitmap
, page
, BITMAP_PAGE_PENDING
);
1249 bitmap
->allclean
= 0;
1251 bmc
= bitmap_get_counter(bitmap
,
1252 (sector_t
)j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1255 j
|= PAGE_COUNTER_MASK
;
1257 if (*bmc
== 1 && !bitmap
->need_sync
) {
1258 /* we can clear the bit */
1260 bitmap_count_page(bitmap
,
1261 (sector_t
)j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1265 paddr
= kmap_atomic(page
, KM_USER0
);
1266 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1267 clear_bit(file_page_offset(bitmap
, j
),
1271 file_page_offset(bitmap
,
1274 kunmap_atomic(paddr
, KM_USER0
);
1275 } else if (*bmc
<= 2) {
1276 *bmc
= 1; /* maybe clear the bit next time */
1277 set_page_attr(bitmap
, page
, BITMAP_PAGE_PENDING
);
1278 bitmap
->allclean
= 0;
1282 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1284 /* now sync the final page */
1285 if (lastpage
!= NULL
) {
1286 spin_lock_irqsave(&bitmap
->lock
, flags
);
1287 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1288 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1289 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1290 write_page(bitmap
, lastpage
, 0);
1292 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1293 bitmap
->allclean
= 0;
1294 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1299 if (bitmap
->allclean
== 0)
1300 bitmap
->mddev
->thread
->timeout
=
1301 bitmap
->mddev
->bitmap_info
.daemon_sleep
;
1302 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1305 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1306 sector_t offset
, sector_t
*blocks
,
1308 __releases(bitmap
->lock
)
1309 __acquires(bitmap
->lock
)
1311 /* If 'create', we might release the lock and reclaim it.
1312 * The lock must have been taken with interrupts enabled.
1313 * If !create, we don't release the lock.
1315 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1316 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1317 unsigned long pageoff
= (chunk
& PAGE_COUNTER_MASK
) << COUNTER_BYTE_SHIFT
;
1321 err
= bitmap_checkpage(bitmap
, page
, create
);
1323 if (bitmap
->bp
[page
].hijacked
||
1324 bitmap
->bp
[page
].map
== NULL
)
1325 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
) +
1326 PAGE_COUNTER_SHIFT
- 1);
1328 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1329 *blocks
= csize
- (offset
& (csize
- 1));
1334 /* now locked ... */
1336 if (bitmap
->bp
[page
].hijacked
) { /* hijacked pointer */
1337 /* should we use the first or second counter field
1338 * of the hijacked pointer? */
1339 int hi
= (pageoff
> PAGE_COUNTER_MASK
);
1340 return &((bitmap_counter_t
*)
1341 &bitmap
->bp
[page
].map
)[hi
];
1342 } else /* page is allocated */
1343 return (bitmap_counter_t
*)
1344 &(bitmap
->bp
[page
].map
[pageoff
]);
1347 int bitmap_startwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
, int behind
)
1354 atomic_inc(&bitmap
->behind_writes
);
1355 bw
= atomic_read(&bitmap
->behind_writes
);
1356 if (bw
> bitmap
->behind_writes_used
)
1357 bitmap
->behind_writes_used
= bw
;
1359 pr_debug("inc write-behind count %d/%lu\n",
1360 bw
, bitmap
->mddev
->bitmap_info
.max_write_behind
);
1365 bitmap_counter_t
*bmc
;
1367 spin_lock_irq(&bitmap
->lock
);
1368 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 1);
1370 spin_unlock_irq(&bitmap
->lock
);
1374 if (unlikely(COUNTER(*bmc
) == COUNTER_MAX
)) {
1375 DEFINE_WAIT(__wait
);
1376 /* note that it is safe to do the prepare_to_wait
1377 * after the test as long as we do it before dropping
1380 prepare_to_wait(&bitmap
->overflow_wait
, &__wait
,
1381 TASK_UNINTERRUPTIBLE
);
1382 spin_unlock_irq(&bitmap
->lock
);
1384 finish_wait(&bitmap
->overflow_wait
, &__wait
);
1390 bitmap_file_set_bit(bitmap
, offset
);
1391 bitmap_count_page(bitmap
, offset
, 1);
1399 spin_unlock_irq(&bitmap
->lock
);
1402 if (sectors
> blocks
)
1409 EXPORT_SYMBOL(bitmap_startwrite
);
1411 void bitmap_endwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
,
1412 int success
, int behind
)
1417 if (atomic_dec_and_test(&bitmap
->behind_writes
))
1418 wake_up(&bitmap
->behind_wait
);
1419 pr_debug("dec write-behind count %d/%lu\n",
1420 atomic_read(&bitmap
->behind_writes
),
1421 bitmap
->mddev
->bitmap_info
.max_write_behind
);
1423 if (bitmap
->mddev
->degraded
)
1424 /* Never clear bits or update events_cleared when degraded */
1429 unsigned long flags
;
1430 bitmap_counter_t
*bmc
;
1432 spin_lock_irqsave(&bitmap
->lock
, flags
);
1433 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 0);
1435 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1440 bitmap
->events_cleared
< bitmap
->mddev
->events
) {
1441 bitmap
->events_cleared
= bitmap
->mddev
->events
;
1442 bitmap
->need_sync
= 1;
1443 sysfs_notify_dirent_safe(bitmap
->sysfs_can_clear
);
1446 if (!success
&& !NEEDED(*bmc
))
1447 *bmc
|= NEEDED_MASK
;
1449 if (COUNTER(*bmc
) == COUNTER_MAX
)
1450 wake_up(&bitmap
->overflow_wait
);
1454 set_page_attr(bitmap
,
1457 offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1458 BITMAP_PAGE_PENDING
);
1459 bitmap
->allclean
= 0;
1461 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1463 if (sectors
> blocks
)
1469 EXPORT_SYMBOL(bitmap_endwrite
);
1471 static int __bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, sector_t
*blocks
,
1474 bitmap_counter_t
*bmc
;
1476 if (bitmap
== NULL
) {/* FIXME or bitmap set as 'failed' */
1478 return 1; /* always resync if no bitmap */
1480 spin_lock_irq(&bitmap
->lock
);
1481 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1487 else if (NEEDED(*bmc
)) {
1489 if (!degraded
) { /* don't set/clear bits if degraded */
1490 *bmc
|= RESYNC_MASK
;
1491 *bmc
&= ~NEEDED_MASK
;
1495 spin_unlock_irq(&bitmap
->lock
);
1499 int bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, sector_t
*blocks
,
1502 /* bitmap_start_sync must always report on multiples of whole
1503 * pages, otherwise resync (which is very PAGE_SIZE based) will
1505 * So call __bitmap_start_sync repeatedly (if needed) until
1506 * At least PAGE_SIZE>>9 blocks are covered.
1507 * Return the 'or' of the result.
1513 while (*blocks
< (PAGE_SIZE
>>9)) {
1514 rv
|= __bitmap_start_sync(bitmap
, offset
,
1515 &blocks1
, degraded
);
1521 EXPORT_SYMBOL(bitmap_start_sync
);
1523 void bitmap_end_sync(struct bitmap
*bitmap
, sector_t offset
, sector_t
*blocks
, int aborted
)
1525 bitmap_counter_t
*bmc
;
1526 unsigned long flags
;
1528 if (bitmap
== NULL
) {
1532 spin_lock_irqsave(&bitmap
->lock
, flags
);
1533 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1538 *bmc
&= ~RESYNC_MASK
;
1540 if (!NEEDED(*bmc
) && aborted
)
1541 *bmc
|= NEEDED_MASK
;
1544 set_page_attr(bitmap
,
1545 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1546 BITMAP_PAGE_PENDING
);
1547 bitmap
->allclean
= 0;
1552 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1554 EXPORT_SYMBOL(bitmap_end_sync
);
1556 void bitmap_close_sync(struct bitmap
*bitmap
)
1558 /* Sync has finished, and any bitmap chunks that weren't synced
1559 * properly have been aborted. It remains to us to clear the
1560 * RESYNC bit wherever it is still on
1562 sector_t sector
= 0;
1566 while (sector
< bitmap
->mddev
->resync_max_sectors
) {
1567 bitmap_end_sync(bitmap
, sector
, &blocks
, 0);
1571 EXPORT_SYMBOL(bitmap_close_sync
);
1573 void bitmap_cond_end_sync(struct bitmap
*bitmap
, sector_t sector
)
1581 bitmap
->last_end_sync
= jiffies
;
1584 if (time_before(jiffies
, (bitmap
->last_end_sync
1585 + bitmap
->mddev
->bitmap_info
.daemon_sleep
)))
1587 wait_event(bitmap
->mddev
->recovery_wait
,
1588 atomic_read(&bitmap
->mddev
->recovery_active
) == 0);
1590 bitmap
->mddev
->curr_resync_completed
= sector
;
1591 set_bit(MD_CHANGE_CLEAN
, &bitmap
->mddev
->flags
);
1592 sector
&= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap
)) - 1);
1594 while (s
< sector
&& s
< bitmap
->mddev
->resync_max_sectors
) {
1595 bitmap_end_sync(bitmap
, s
, &blocks
, 0);
1598 bitmap
->last_end_sync
= jiffies
;
1599 sysfs_notify(&bitmap
->mddev
->kobj
, NULL
, "sync_completed");
1601 EXPORT_SYMBOL(bitmap_cond_end_sync
);
1603 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
)
1605 /* For each chunk covered by any of these sectors, set the
1606 * counter to 1 and set resync_needed. They should all
1607 * be 0 at this point
1611 bitmap_counter_t
*bmc
;
1612 spin_lock_irq(&bitmap
->lock
);
1613 bmc
= bitmap_get_counter(bitmap
, offset
, &secs
, 1);
1615 spin_unlock_irq(&bitmap
->lock
);
1620 *bmc
= 1 | (needed
? NEEDED_MASK
: 0);
1621 bitmap_count_page(bitmap
, offset
, 1);
1622 page
= filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
));
1623 set_page_attr(bitmap
, page
, BITMAP_PAGE_PENDING
);
1624 bitmap
->allclean
= 0;
1626 spin_unlock_irq(&bitmap
->lock
);
1629 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1630 void bitmap_dirty_bits(struct bitmap
*bitmap
, unsigned long s
, unsigned long e
)
1632 unsigned long chunk
;
1634 for (chunk
= s
; chunk
<= e
; chunk
++) {
1635 sector_t sec
= (sector_t
)chunk
<< CHUNK_BLOCK_SHIFT(bitmap
);
1636 bitmap_set_memory_bits(bitmap
, sec
, 1);
1637 bitmap_file_set_bit(bitmap
, sec
);
1638 if (sec
< bitmap
->mddev
->recovery_cp
)
1639 /* We are asserting that the array is dirty,
1640 * so move the recovery_cp address back so
1641 * that it is obvious that it is dirty
1643 bitmap
->mddev
->recovery_cp
= sec
;
1648 * flush out any pending updates
1650 void bitmap_flush(struct mddev
*mddev
)
1652 struct bitmap
*bitmap
= mddev
->bitmap
;
1655 if (!bitmap
) /* there was no bitmap */
1658 /* run the daemon_work three time to ensure everything is flushed
1661 sleep
= mddev
->bitmap_info
.daemon_sleep
* 2;
1662 bitmap
->daemon_lastrun
-= sleep
;
1663 bitmap_daemon_work(mddev
);
1664 bitmap
->daemon_lastrun
-= sleep
;
1665 bitmap_daemon_work(mddev
);
1666 bitmap
->daemon_lastrun
-= sleep
;
1667 bitmap_daemon_work(mddev
);
1668 bitmap_update_sb(bitmap
);
1672 * free memory that was allocated
1674 static void bitmap_free(struct bitmap
*bitmap
)
1676 unsigned long k
, pages
;
1677 struct bitmap_page
*bp
;
1679 if (!bitmap
) /* there was no bitmap */
1682 /* release the bitmap file and kill the daemon */
1683 bitmap_file_put(bitmap
);
1686 pages
= bitmap
->pages
;
1688 /* free all allocated memory */
1690 if (bp
) /* deallocate the page memory */
1691 for (k
= 0; k
< pages
; k
++)
1692 if (bp
[k
].map
&& !bp
[k
].hijacked
)
1698 void bitmap_destroy(struct mddev
*mddev
)
1700 struct bitmap
*bitmap
= mddev
->bitmap
;
1702 if (!bitmap
) /* there was no bitmap */
1705 mutex_lock(&mddev
->bitmap_info
.mutex
);
1706 mddev
->bitmap
= NULL
; /* disconnect from the md device */
1707 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1709 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1711 if (bitmap
->sysfs_can_clear
)
1712 sysfs_put(bitmap
->sysfs_can_clear
);
1714 bitmap_free(bitmap
);
1718 * initialize the bitmap structure
1719 * if this returns an error, bitmap_destroy must be called to do clean up
1721 int bitmap_create(struct mddev
*mddev
)
1723 struct bitmap
*bitmap
;
1724 sector_t blocks
= mddev
->resync_max_sectors
;
1725 unsigned long chunks
;
1726 unsigned long pages
;
1727 struct file
*file
= mddev
->bitmap_info
.file
;
1729 struct sysfs_dirent
*bm
= NULL
;
1731 BUILD_BUG_ON(sizeof(bitmap_super_t
) != 256);
1734 && !mddev
->bitmap_info
.offset
) /* bitmap disabled, nothing to do */
1737 BUG_ON(file
&& mddev
->bitmap_info
.offset
);
1739 bitmap
= kzalloc(sizeof(*bitmap
), GFP_KERNEL
);
1743 spin_lock_init(&bitmap
->lock
);
1744 atomic_set(&bitmap
->pending_writes
, 0);
1745 init_waitqueue_head(&bitmap
->write_wait
);
1746 init_waitqueue_head(&bitmap
->overflow_wait
);
1747 init_waitqueue_head(&bitmap
->behind_wait
);
1749 bitmap
->mddev
= mddev
;
1752 bm
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "bitmap");
1754 bitmap
->sysfs_can_clear
= sysfs_get_dirent(bm
, NULL
, "can_clear");
1757 bitmap
->sysfs_can_clear
= NULL
;
1759 bitmap
->file
= file
;
1762 /* As future accesses to this file will use bmap,
1763 * and bypass the page cache, we must sync the file
1768 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
1769 if (!mddev
->bitmap_info
.external
) {
1771 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1772 * instructing us to create a new on-disk bitmap instance.
1774 if (test_and_clear_bit(MD_ARRAY_FIRST_USE
, &mddev
->flags
))
1775 err
= bitmap_new_disk_sb(bitmap
);
1777 err
= bitmap_read_sb(bitmap
);
1780 if (mddev
->bitmap_info
.chunksize
== 0 ||
1781 mddev
->bitmap_info
.daemon_sleep
== 0)
1782 /* chunksize and time_base need to be
1789 bitmap
->daemon_lastrun
= jiffies
;
1790 bitmap
->chunkshift
= ffz(~mddev
->bitmap_info
.chunksize
);
1792 /* now that chunksize and chunkshift are set, we can use these macros */
1793 chunks
= (blocks
+ CHUNK_BLOCK_RATIO(bitmap
) - 1) >>
1794 CHUNK_BLOCK_SHIFT(bitmap
);
1795 pages
= (chunks
+ PAGE_COUNTER_RATIO
- 1) / PAGE_COUNTER_RATIO
;
1799 bitmap
->chunks
= chunks
;
1800 bitmap
->pages
= pages
;
1801 bitmap
->missing_pages
= pages
;
1803 #ifdef INJECT_FATAL_FAULT_1
1806 bitmap
->bp
= kzalloc(pages
* sizeof(*bitmap
->bp
), GFP_KERNEL
);
1812 printk(KERN_INFO
"created bitmap (%lu pages) for device %s\n",
1813 pages
, bmname(bitmap
));
1815 mddev
->bitmap
= bitmap
;
1818 return (bitmap
->flags
& BITMAP_WRITE_ERROR
) ? -EIO
: 0;
1821 bitmap_free(bitmap
);
1825 int bitmap_load(struct mddev
*mddev
)
1829 sector_t sector
= 0;
1830 struct bitmap
*bitmap
= mddev
->bitmap
;
1835 /* Clear out old bitmap info first: Either there is none, or we
1836 * are resuming after someone else has possibly changed things,
1837 * so we should forget old cached info.
1838 * All chunks should be clean, but some might need_sync.
1840 while (sector
< mddev
->resync_max_sectors
) {
1842 bitmap_start_sync(bitmap
, sector
, &blocks
, 0);
1845 bitmap_close_sync(bitmap
);
1847 if (mddev
->degraded
== 0
1848 || bitmap
->events_cleared
== mddev
->events
)
1849 /* no need to keep dirty bits to optimise a
1850 * re-add of a missing device */
1851 start
= mddev
->recovery_cp
;
1853 err
= bitmap_init_from_disk(bitmap
, start
);
1858 mddev
->thread
->timeout
= mddev
->bitmap_info
.daemon_sleep
;
1859 md_wakeup_thread(mddev
->thread
);
1861 bitmap_update_sb(bitmap
);
1863 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
1868 EXPORT_SYMBOL_GPL(bitmap_load
);
1871 location_show(struct mddev
*mddev
, char *page
)
1874 if (mddev
->bitmap_info
.file
)
1875 len
= sprintf(page
, "file");
1876 else if (mddev
->bitmap_info
.offset
)
1877 len
= sprintf(page
, "%+lld", (long long)mddev
->bitmap_info
.offset
);
1879 len
= sprintf(page
, "none");
1880 len
+= sprintf(page
+len
, "\n");
1885 location_store(struct mddev
*mddev
, const char *buf
, size_t len
)
1889 if (!mddev
->pers
->quiesce
)
1891 if (mddev
->recovery
|| mddev
->sync_thread
)
1895 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
||
1896 mddev
->bitmap_info
.offset
) {
1897 /* bitmap already configured. Only option is to clear it */
1898 if (strncmp(buf
, "none", 4) != 0)
1901 mddev
->pers
->quiesce(mddev
, 1);
1902 bitmap_destroy(mddev
);
1903 mddev
->pers
->quiesce(mddev
, 0);
1905 mddev
->bitmap_info
.offset
= 0;
1906 if (mddev
->bitmap_info
.file
) {
1907 struct file
*f
= mddev
->bitmap_info
.file
;
1908 mddev
->bitmap_info
.file
= NULL
;
1909 restore_bitmap_write_access(f
);
1913 /* No bitmap, OK to set a location */
1915 if (strncmp(buf
, "none", 4) == 0)
1916 /* nothing to be done */;
1917 else if (strncmp(buf
, "file:", 5) == 0) {
1918 /* Not supported yet */
1923 rv
= strict_strtoll(buf
+1, 10, &offset
);
1925 rv
= strict_strtoll(buf
, 10, &offset
);
1930 if (mddev
->bitmap_info
.external
== 0 &&
1931 mddev
->major_version
== 0 &&
1932 offset
!= mddev
->bitmap_info
.default_offset
)
1934 mddev
->bitmap_info
.offset
= offset
;
1936 mddev
->pers
->quiesce(mddev
, 1);
1937 rv
= bitmap_create(mddev
);
1939 bitmap_destroy(mddev
);
1940 mddev
->bitmap_info
.offset
= 0;
1942 mddev
->pers
->quiesce(mddev
, 0);
1948 if (!mddev
->external
) {
1949 /* Ensure new bitmap info is stored in
1950 * metadata promptly.
1952 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1953 md_wakeup_thread(mddev
->thread
);
1958 static struct md_sysfs_entry bitmap_location
=
1959 __ATTR(location
, S_IRUGO
|S_IWUSR
, location_show
, location_store
);
1962 timeout_show(struct mddev
*mddev
, char *page
)
1965 unsigned long secs
= mddev
->bitmap_info
.daemon_sleep
/ HZ
;
1966 unsigned long jifs
= mddev
->bitmap_info
.daemon_sleep
% HZ
;
1968 len
= sprintf(page
, "%lu", secs
);
1970 len
+= sprintf(page
+len
, ".%03u", jiffies_to_msecs(jifs
));
1971 len
+= sprintf(page
+len
, "\n");
1976 timeout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
1978 /* timeout can be set at any time */
1979 unsigned long timeout
;
1980 int rv
= strict_strtoul_scaled(buf
, &timeout
, 4);
1984 /* just to make sure we don't overflow... */
1985 if (timeout
>= LONG_MAX
/ HZ
)
1988 timeout
= timeout
* HZ
/ 10000;
1990 if (timeout
>= MAX_SCHEDULE_TIMEOUT
)
1991 timeout
= MAX_SCHEDULE_TIMEOUT
-1;
1994 mddev
->bitmap_info
.daemon_sleep
= timeout
;
1995 if (mddev
->thread
) {
1996 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1997 * the bitmap is all clean and we don't need to
1998 * adjust the timeout right now
2000 if (mddev
->thread
->timeout
< MAX_SCHEDULE_TIMEOUT
) {
2001 mddev
->thread
->timeout
= timeout
;
2002 md_wakeup_thread(mddev
->thread
);
2008 static struct md_sysfs_entry bitmap_timeout
=
2009 __ATTR(time_base
, S_IRUGO
|S_IWUSR
, timeout_show
, timeout_store
);
2012 backlog_show(struct mddev
*mddev
, char *page
)
2014 return sprintf(page
, "%lu\n", mddev
->bitmap_info
.max_write_behind
);
2018 backlog_store(struct mddev
*mddev
, const char *buf
, size_t len
)
2020 unsigned long backlog
;
2021 int rv
= strict_strtoul(buf
, 10, &backlog
);
2024 if (backlog
> COUNTER_MAX
)
2026 mddev
->bitmap_info
.max_write_behind
= backlog
;
2030 static struct md_sysfs_entry bitmap_backlog
=
2031 __ATTR(backlog
, S_IRUGO
|S_IWUSR
, backlog_show
, backlog_store
);
2034 chunksize_show(struct mddev
*mddev
, char *page
)
2036 return sprintf(page
, "%lu\n", mddev
->bitmap_info
.chunksize
);
2040 chunksize_store(struct mddev
*mddev
, const char *buf
, size_t len
)
2042 /* Can only be changed when no bitmap is active */
2044 unsigned long csize
;
2047 rv
= strict_strtoul(buf
, 10, &csize
);
2051 !is_power_of_2(csize
))
2053 mddev
->bitmap_info
.chunksize
= csize
;
2057 static struct md_sysfs_entry bitmap_chunksize
=
2058 __ATTR(chunksize
, S_IRUGO
|S_IWUSR
, chunksize_show
, chunksize_store
);
2060 static ssize_t
metadata_show(struct mddev
*mddev
, char *page
)
2062 return sprintf(page
, "%s\n", (mddev
->bitmap_info
.external
2063 ? "external" : "internal"));
2066 static ssize_t
metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
2068 if (mddev
->bitmap
||
2069 mddev
->bitmap_info
.file
||
2070 mddev
->bitmap_info
.offset
)
2072 if (strncmp(buf
, "external", 8) == 0)
2073 mddev
->bitmap_info
.external
= 1;
2074 else if (strncmp(buf
, "internal", 8) == 0)
2075 mddev
->bitmap_info
.external
= 0;
2081 static struct md_sysfs_entry bitmap_metadata
=
2082 __ATTR(metadata
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
2084 static ssize_t
can_clear_show(struct mddev
*mddev
, char *page
)
2088 len
= sprintf(page
, "%s\n", (mddev
->bitmap
->need_sync
?
2091 len
= sprintf(page
, "\n");
2095 static ssize_t
can_clear_store(struct mddev
*mddev
, const char *buf
, size_t len
)
2097 if (mddev
->bitmap
== NULL
)
2099 if (strncmp(buf
, "false", 5) == 0)
2100 mddev
->bitmap
->need_sync
= 1;
2101 else if (strncmp(buf
, "true", 4) == 0) {
2102 if (mddev
->degraded
)
2104 mddev
->bitmap
->need_sync
= 0;
2110 static struct md_sysfs_entry bitmap_can_clear
=
2111 __ATTR(can_clear
, S_IRUGO
|S_IWUSR
, can_clear_show
, can_clear_store
);
2114 behind_writes_used_show(struct mddev
*mddev
, char *page
)
2116 if (mddev
->bitmap
== NULL
)
2117 return sprintf(page
, "0\n");
2118 return sprintf(page
, "%lu\n",
2119 mddev
->bitmap
->behind_writes_used
);
2123 behind_writes_used_reset(struct mddev
*mddev
, const char *buf
, size_t len
)
2126 mddev
->bitmap
->behind_writes_used
= 0;
2130 static struct md_sysfs_entry max_backlog_used
=
2131 __ATTR(max_backlog_used
, S_IRUGO
| S_IWUSR
,
2132 behind_writes_used_show
, behind_writes_used_reset
);
2134 static struct attribute
*md_bitmap_attrs
[] = {
2135 &bitmap_location
.attr
,
2136 &bitmap_timeout
.attr
,
2137 &bitmap_backlog
.attr
,
2138 &bitmap_chunksize
.attr
,
2139 &bitmap_metadata
.attr
,
2140 &bitmap_can_clear
.attr
,
2141 &max_backlog_used
.attr
,
2144 struct attribute_group md_bitmap_group
= {
2146 .attrs
= md_bitmap_attrs
,