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>
32 static inline char *bmname(struct bitmap
*bitmap
)
34 return bitmap
->mddev
? mdname(bitmap
->mddev
) : "mdX";
38 * just a placeholder - calls kmalloc for bitmap pages
40 static unsigned char *bitmap_alloc_page(struct bitmap
*bitmap
)
44 page
= kzalloc(PAGE_SIZE
, GFP_NOIO
);
46 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap
));
48 pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
49 bmname(bitmap
), page
);
54 * for now just a placeholder -- just calls kfree for bitmap pages
56 static void bitmap_free_page(struct bitmap
*bitmap
, unsigned char *page
)
58 pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap
), page
);
63 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
65 * 1) check to see if this page is allocated, if it's not then try to alloc
66 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
67 * page pointer directly as a counter
69 * if we find our page, we increment the page's refcount so that it stays
70 * allocated while we're using it
72 static int bitmap_checkpage(struct bitmap
*bitmap
,
73 unsigned long page
, int create
)
74 __releases(bitmap
->lock
)
75 __acquires(bitmap
->lock
)
77 unsigned char *mappage
;
79 if (page
>= bitmap
->pages
) {
80 /* This can happen if bitmap_start_sync goes beyond
81 * End-of-device while looking for a whole page.
87 if (bitmap
->bp
[page
].hijacked
) /* it's hijacked, don't try to alloc */
90 if (bitmap
->bp
[page
].map
) /* page is already allocated, just return */
96 /* this page has not been allocated yet */
98 spin_unlock_irq(&bitmap
->lock
);
99 mappage
= bitmap_alloc_page(bitmap
);
100 spin_lock_irq(&bitmap
->lock
);
102 if (mappage
== NULL
) {
103 pr_debug("%s: bitmap map page allocation failed, hijacking\n",
105 /* failed - set the hijacked flag so that we can use the
106 * pointer as a counter */
107 if (!bitmap
->bp
[page
].map
)
108 bitmap
->bp
[page
].hijacked
= 1;
109 } else if (bitmap
->bp
[page
].map
||
110 bitmap
->bp
[page
].hijacked
) {
111 /* somebody beat us to getting the page */
112 bitmap_free_page(bitmap
, mappage
);
116 /* no page was in place and we have one, so install it */
118 bitmap
->bp
[page
].map
= mappage
;
119 bitmap
->missing_pages
--;
124 /* if page is completely empty, put it back on the free list, or dealloc it */
125 /* if page was hijacked, unmark the flag so it might get alloced next time */
126 /* Note: lock should be held when calling this */
127 static void bitmap_checkfree(struct bitmap
*bitmap
, unsigned long page
)
131 if (bitmap
->bp
[page
].count
) /* page is still busy */
134 /* page is no longer in use, it can be released */
136 if (bitmap
->bp
[page
].hijacked
) { /* page was hijacked, undo this now */
137 bitmap
->bp
[page
].hijacked
= 0;
138 bitmap
->bp
[page
].map
= NULL
;
140 /* normal case, free the page */
141 ptr
= bitmap
->bp
[page
].map
;
142 bitmap
->bp
[page
].map
= NULL
;
143 bitmap
->missing_pages
++;
144 bitmap_free_page(bitmap
, ptr
);
149 * bitmap file handling - read and write the bitmap file and its superblock
153 * basic page I/O operations
156 /* IO operations when bitmap is stored near all superblocks */
157 static struct page
*read_sb_page(struct mddev
*mddev
, loff_t offset
,
159 unsigned long index
, int size
)
161 /* choose a good rdev and read the page from there */
163 struct md_rdev
*rdev
;
168 page
= alloc_page(GFP_KERNEL
);
170 return ERR_PTR(-ENOMEM
);
174 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
175 if (! test_bit(In_sync
, &rdev
->flags
)
176 || test_bit(Faulty
, &rdev
->flags
))
179 target
= offset
+ index
* (PAGE_SIZE
/512);
181 if (sync_page_io(rdev
, target
,
182 roundup(size
, bdev_logical_block_size(rdev
->bdev
)),
185 attach_page_buffers(page
, NULL
); /* so that free_buffer will
192 return ERR_PTR(-EIO
);
196 static struct md_rdev
*next_active_rdev(struct md_rdev
*rdev
, struct mddev
*mddev
)
198 /* Iterate the disks of an mddev, using rcu to protect access to the
199 * linked list, and raising the refcount of devices we return to ensure
200 * they don't disappear while in use.
201 * As devices are only added or removed when raid_disk is < 0 and
202 * nr_pending is 0 and In_sync is clear, the entries we return will
203 * still be in the same position on the list when we re-enter
204 * list_for_each_continue_rcu.
206 struct list_head
*pos
;
209 /* start at the beginning */
212 /* release the previous rdev and start from there. */
213 rdev_dec_pending(rdev
, mddev
);
214 pos
= &rdev
->same_set
;
216 list_for_each_continue_rcu(pos
, &mddev
->disks
) {
217 rdev
= list_entry(pos
, struct md_rdev
, same_set
);
218 if (rdev
->raid_disk
>= 0 &&
219 !test_bit(Faulty
, &rdev
->flags
)) {
220 /* this is a usable devices */
221 atomic_inc(&rdev
->nr_pending
);
230 static int write_sb_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
232 struct md_rdev
*rdev
= NULL
;
233 struct block_device
*bdev
;
234 struct mddev
*mddev
= bitmap
->mddev
;
236 while ((rdev
= next_active_rdev(rdev
, mddev
)) != NULL
) {
237 int size
= PAGE_SIZE
;
238 loff_t offset
= mddev
->bitmap_info
.offset
;
240 bdev
= (rdev
->meta_bdev
) ? rdev
->meta_bdev
: rdev
->bdev
;
242 if (page
->index
== bitmap
->file_pages
-1)
243 size
= roundup(bitmap
->last_page_size
,
244 bdev_logical_block_size(bdev
));
245 /* Just make sure we aren't corrupting data or
248 if (mddev
->external
) {
249 /* Bitmap could be anywhere. */
250 if (rdev
->sb_start
+ offset
+ (page
->index
254 rdev
->sb_start
+ offset
255 < (rdev
->data_offset
+ mddev
->dev_sectors
258 } else if (offset
< 0) {
259 /* DATA BITMAP METADATA */
261 + (long)(page
->index
* (PAGE_SIZE
/512))
263 /* bitmap runs in to metadata */
265 if (rdev
->data_offset
+ mddev
->dev_sectors
266 > rdev
->sb_start
+ offset
)
267 /* data runs in to bitmap */
269 } else if (rdev
->sb_start
< rdev
->data_offset
) {
270 /* METADATA BITMAP DATA */
273 + page
->index
*(PAGE_SIZE
/512) + size
/512
275 /* bitmap runs in to data */
278 /* DATA METADATA BITMAP - no problems */
280 md_super_write(mddev
, rdev
,
281 rdev
->sb_start
+ offset
282 + page
->index
* (PAGE_SIZE
/512),
288 md_super_wait(mddev
);
295 static void bitmap_file_kick(struct bitmap
*bitmap
);
297 * write out a page to a file
299 static void write_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
301 struct buffer_head
*bh
;
303 if (bitmap
->file
== NULL
) {
304 switch (write_sb_page(bitmap
, page
, wait
)) {
306 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
310 bh
= page_buffers(page
);
312 while (bh
&& bh
->b_blocknr
) {
313 atomic_inc(&bitmap
->pending_writes
);
314 set_buffer_locked(bh
);
315 set_buffer_mapped(bh
);
316 submit_bh(WRITE
| REQ_SYNC
, bh
);
317 bh
= bh
->b_this_page
;
321 wait_event(bitmap
->write_wait
,
322 atomic_read(&bitmap
->pending_writes
)==0);
324 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
325 bitmap_file_kick(bitmap
);
328 static void end_bitmap_write(struct buffer_head
*bh
, int uptodate
)
330 struct bitmap
*bitmap
= bh
->b_private
;
334 spin_lock_irqsave(&bitmap
->lock
, flags
);
335 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
336 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
338 if (atomic_dec_and_test(&bitmap
->pending_writes
))
339 wake_up(&bitmap
->write_wait
);
342 /* copied from buffer.c */
344 __clear_page_buffers(struct page
*page
)
346 ClearPagePrivate(page
);
347 set_page_private(page
, 0);
348 page_cache_release(page
);
350 static void free_buffers(struct page
*page
)
352 struct buffer_head
*bh
= page_buffers(page
);
355 struct buffer_head
*next
= bh
->b_this_page
;
356 free_buffer_head(bh
);
359 __clear_page_buffers(page
);
363 /* read a page from a file.
364 * We both read the page, and attach buffers to the page to record the
365 * address of each block (using bmap). These addresses will be used
366 * to write the block later, completely bypassing the filesystem.
367 * This usage is similar to how swap files are handled, and allows us
368 * to write to a file with no concerns of memory allocation failing.
370 static struct page
*read_page(struct file
*file
, unsigned long index
,
371 struct bitmap
*bitmap
,
374 struct page
*page
= NULL
;
375 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
376 struct buffer_head
*bh
;
379 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE
,
380 (unsigned long long)index
<< PAGE_SHIFT
);
382 page
= alloc_page(GFP_KERNEL
);
384 page
= ERR_PTR(-ENOMEM
);
388 bh
= alloc_page_buffers(page
, 1<<inode
->i_blkbits
, 0);
391 page
= ERR_PTR(-ENOMEM
);
394 attach_page_buffers(page
, bh
);
395 block
= index
<< (PAGE_SHIFT
- inode
->i_blkbits
);
400 bh
->b_blocknr
= bmap(inode
, block
);
401 if (bh
->b_blocknr
== 0) {
402 /* Cannot use this file! */
404 page
= ERR_PTR(-EINVAL
);
407 bh
->b_bdev
= inode
->i_sb
->s_bdev
;
408 if (count
< (1<<inode
->i_blkbits
))
411 count
-= (1<<inode
->i_blkbits
);
413 bh
->b_end_io
= end_bitmap_write
;
414 bh
->b_private
= bitmap
;
415 atomic_inc(&bitmap
->pending_writes
);
416 set_buffer_locked(bh
);
417 set_buffer_mapped(bh
);
421 bh
= bh
->b_this_page
;
425 wait_event(bitmap
->write_wait
,
426 atomic_read(&bitmap
->pending_writes
)==0);
427 if (bitmap
->flags
& BITMAP_WRITE_ERROR
) {
429 page
= ERR_PTR(-EIO
);
433 printk(KERN_ALERT
"md: bitmap read error: (%dB @ %llu): %ld\n",
435 (unsigned long long)index
<< PAGE_SHIFT
,
441 * bitmap file superblock operations
444 /* update the event counter and sync the superblock to disk */
445 void bitmap_update_sb(struct bitmap
*bitmap
)
450 if (!bitmap
|| !bitmap
->mddev
) /* no bitmap for this array */
452 if (bitmap
->mddev
->bitmap_info
.external
)
454 spin_lock_irqsave(&bitmap
->lock
, flags
);
455 if (!bitmap
->sb_page
) { /* no superblock */
456 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
459 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
460 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
461 sb
->events
= cpu_to_le64(bitmap
->mddev
->events
);
462 if (bitmap
->mddev
->events
< bitmap
->events_cleared
)
463 /* rocking back to read-only */
464 bitmap
->events_cleared
= bitmap
->mddev
->events
;
465 sb
->events_cleared
= cpu_to_le64(bitmap
->events_cleared
);
466 sb
->state
= cpu_to_le32(bitmap
->flags
);
467 /* Just in case these have been changed via sysfs: */
468 sb
->daemon_sleep
= cpu_to_le32(bitmap
->mddev
->bitmap_info
.daemon_sleep
/HZ
);
469 sb
->write_behind
= cpu_to_le32(bitmap
->mddev
->bitmap_info
.max_write_behind
);
470 kunmap_atomic(sb
, KM_USER0
);
471 write_page(bitmap
, bitmap
->sb_page
, 1);
474 /* print out the bitmap file superblock */
475 void bitmap_print_sb(struct bitmap
*bitmap
)
479 if (!bitmap
|| !bitmap
->sb_page
)
481 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
482 printk(KERN_DEBUG
"%s: bitmap file superblock:\n", bmname(bitmap
));
483 printk(KERN_DEBUG
" magic: %08x\n", le32_to_cpu(sb
->magic
));
484 printk(KERN_DEBUG
" version: %d\n", le32_to_cpu(sb
->version
));
485 printk(KERN_DEBUG
" uuid: %08x.%08x.%08x.%08x\n",
486 *(__u32
*)(sb
->uuid
+0),
487 *(__u32
*)(sb
->uuid
+4),
488 *(__u32
*)(sb
->uuid
+8),
489 *(__u32
*)(sb
->uuid
+12));
490 printk(KERN_DEBUG
" events: %llu\n",
491 (unsigned long long) le64_to_cpu(sb
->events
));
492 printk(KERN_DEBUG
"events cleared: %llu\n",
493 (unsigned long long) le64_to_cpu(sb
->events_cleared
));
494 printk(KERN_DEBUG
" state: %08x\n", le32_to_cpu(sb
->state
));
495 printk(KERN_DEBUG
" chunksize: %d B\n", le32_to_cpu(sb
->chunksize
));
496 printk(KERN_DEBUG
" daemon sleep: %ds\n", le32_to_cpu(sb
->daemon_sleep
));
497 printk(KERN_DEBUG
" sync size: %llu KB\n",
498 (unsigned long long)le64_to_cpu(sb
->sync_size
)/2);
499 printk(KERN_DEBUG
"max write behind: %d\n", le32_to_cpu(sb
->write_behind
));
500 kunmap_atomic(sb
, KM_USER0
);
507 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
508 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
509 * This function verifies 'bitmap_info' and populates the on-disk bitmap
510 * structure, which is to be written to disk.
512 * Returns: 0 on success, -Exxx on error
514 static int bitmap_new_disk_sb(struct bitmap
*bitmap
)
517 unsigned long chunksize
, daemon_sleep
, write_behind
;
520 bitmap
->sb_page
= alloc_page(GFP_KERNEL
);
521 if (IS_ERR(bitmap
->sb_page
)) {
522 err
= PTR_ERR(bitmap
->sb_page
);
523 bitmap
->sb_page
= NULL
;
526 bitmap
->sb_page
->index
= 0;
528 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
530 sb
->magic
= cpu_to_le32(BITMAP_MAGIC
);
531 sb
->version
= cpu_to_le32(BITMAP_MAJOR_HI
);
533 chunksize
= bitmap
->mddev
->bitmap_info
.chunksize
;
535 if (!is_power_of_2(chunksize
)) {
536 kunmap_atomic(sb
, KM_USER0
);
537 printk(KERN_ERR
"bitmap chunksize not a power of 2\n");
540 sb
->chunksize
= cpu_to_le32(chunksize
);
542 daemon_sleep
= bitmap
->mddev
->bitmap_info
.daemon_sleep
;
544 (daemon_sleep
< 1) || (daemon_sleep
> MAX_SCHEDULE_TIMEOUT
)) {
545 printk(KERN_INFO
"Choosing daemon_sleep default (5 sec)\n");
546 daemon_sleep
= 5 * HZ
;
548 sb
->daemon_sleep
= cpu_to_le32(daemon_sleep
);
549 bitmap
->mddev
->bitmap_info
.daemon_sleep
= daemon_sleep
;
552 * FIXME: write_behind for RAID1. If not specified, what
553 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
555 write_behind
= bitmap
->mddev
->bitmap_info
.max_write_behind
;
556 if (write_behind
> COUNTER_MAX
)
557 write_behind
= COUNTER_MAX
/ 2;
558 sb
->write_behind
= cpu_to_le32(write_behind
);
559 bitmap
->mddev
->bitmap_info
.max_write_behind
= write_behind
;
561 /* keep the array size field of the bitmap superblock up to date */
562 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
564 memcpy(sb
->uuid
, bitmap
->mddev
->uuid
, 16);
566 bitmap
->flags
|= BITMAP_STALE
;
567 sb
->state
|= cpu_to_le32(BITMAP_STALE
);
568 bitmap
->events_cleared
= bitmap
->mddev
->events
;
569 sb
->events_cleared
= cpu_to_le64(bitmap
->mddev
->events
);
571 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
572 sb
->version
= cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN
);
574 kunmap_atomic(sb
, KM_USER0
);
579 /* read the superblock from the bitmap file and initialize some bitmap fields */
580 static int bitmap_read_sb(struct bitmap
*bitmap
)
584 unsigned long chunksize
, daemon_sleep
, write_behind
;
585 unsigned long long events
;
588 /* page 0 is the superblock, read it... */
590 loff_t isize
= i_size_read(bitmap
->file
->f_mapping
->host
);
591 int bytes
= isize
> PAGE_SIZE
? PAGE_SIZE
: isize
;
593 bitmap
->sb_page
= read_page(bitmap
->file
, 0, bitmap
, bytes
);
595 bitmap
->sb_page
= read_sb_page(bitmap
->mddev
,
596 bitmap
->mddev
->bitmap_info
.offset
,
598 0, sizeof(bitmap_super_t
));
600 if (IS_ERR(bitmap
->sb_page
)) {
601 err
= PTR_ERR(bitmap
->sb_page
);
602 bitmap
->sb_page
= NULL
;
606 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
608 chunksize
= le32_to_cpu(sb
->chunksize
);
609 daemon_sleep
= le32_to_cpu(sb
->daemon_sleep
) * HZ
;
610 write_behind
= le32_to_cpu(sb
->write_behind
);
612 /* verify that the bitmap-specific fields are valid */
613 if (sb
->magic
!= cpu_to_le32(BITMAP_MAGIC
))
614 reason
= "bad magic";
615 else if (le32_to_cpu(sb
->version
) < BITMAP_MAJOR_LO
||
616 le32_to_cpu(sb
->version
) > BITMAP_MAJOR_HI
)
617 reason
= "unrecognized superblock version";
618 else if (chunksize
< 512)
619 reason
= "bitmap chunksize too small";
620 else if (!is_power_of_2(chunksize
))
621 reason
= "bitmap chunksize not a power of 2";
622 else if (daemon_sleep
< 1 || daemon_sleep
> MAX_SCHEDULE_TIMEOUT
)
623 reason
= "daemon sleep period out of range";
624 else if (write_behind
> COUNTER_MAX
)
625 reason
= "write-behind limit out of range (0 - 16383)";
627 printk(KERN_INFO
"%s: invalid bitmap file superblock: %s\n",
628 bmname(bitmap
), reason
);
632 /* keep the array size field of the bitmap superblock up to date */
633 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
635 if (!bitmap
->mddev
->persistent
)
639 * if we have a persistent array superblock, compare the
640 * bitmap's UUID and event counter to the mddev's
642 if (memcmp(sb
->uuid
, bitmap
->mddev
->uuid
, 16)) {
643 printk(KERN_INFO
"%s: bitmap superblock UUID mismatch\n",
647 events
= le64_to_cpu(sb
->events
);
648 if (events
< bitmap
->mddev
->events
) {
649 printk(KERN_INFO
"%s: bitmap file is out of date (%llu < %llu) "
650 "-- forcing full recovery\n", bmname(bitmap
), events
,
651 (unsigned long long) bitmap
->mddev
->events
);
652 sb
->state
|= cpu_to_le32(BITMAP_STALE
);
655 /* assign fields using values from superblock */
656 bitmap
->mddev
->bitmap_info
.chunksize
= chunksize
;
657 bitmap
->mddev
->bitmap_info
.daemon_sleep
= daemon_sleep
;
658 bitmap
->mddev
->bitmap_info
.max_write_behind
= write_behind
;
659 bitmap
->flags
|= le32_to_cpu(sb
->state
);
660 if (le32_to_cpu(sb
->version
) == BITMAP_MAJOR_HOSTENDIAN
)
661 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
662 bitmap
->events_cleared
= le64_to_cpu(sb
->events_cleared
);
663 if (bitmap
->flags
& BITMAP_STALE
)
664 bitmap
->events_cleared
= bitmap
->mddev
->events
;
667 kunmap_atomic(sb
, KM_USER0
);
669 bitmap_print_sb(bitmap
);
673 enum bitmap_mask_op
{
678 /* record the state of the bitmap in the superblock. Return the old value */
679 static int bitmap_mask_state(struct bitmap
*bitmap
, enum bitmap_state bits
,
680 enum bitmap_mask_op op
)
686 spin_lock_irqsave(&bitmap
->lock
, flags
);
687 if (!bitmap
->sb_page
) { /* can't set the state */
688 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
691 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
692 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
693 old
= le32_to_cpu(sb
->state
) & bits
;
696 sb
->state
|= cpu_to_le32(bits
);
697 bitmap
->flags
|= bits
;
700 sb
->state
&= cpu_to_le32(~bits
);
701 bitmap
->flags
&= ~bits
;
706 kunmap_atomic(sb
, KM_USER0
);
711 * general bitmap file operations
717 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
718 * file a page at a time. There's a superblock at the start of the file.
720 /* calculate the index of the page that contains this bit */
721 static inline unsigned long file_page_index(struct bitmap
*bitmap
, unsigned long chunk
)
723 if (!bitmap
->mddev
->bitmap_info
.external
)
724 chunk
+= sizeof(bitmap_super_t
) << 3;
725 return chunk
>> PAGE_BIT_SHIFT
;
728 /* calculate the (bit) offset of this bit within a page */
729 static inline unsigned long file_page_offset(struct bitmap
*bitmap
, unsigned long chunk
)
731 if (!bitmap
->mddev
->bitmap_info
.external
)
732 chunk
+= sizeof(bitmap_super_t
) << 3;
733 return chunk
& (PAGE_BITS
- 1);
737 * return a pointer to the page in the filemap that contains the given bit
739 * this lookup is complicated by the fact that the bitmap sb might be exactly
740 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
743 static inline struct page
*filemap_get_page(struct bitmap
*bitmap
,
746 if (file_page_index(bitmap
, chunk
) >= bitmap
->file_pages
)
748 return bitmap
->filemap
[file_page_index(bitmap
, chunk
)
749 - file_page_index(bitmap
, 0)];
752 static void bitmap_file_unmap(struct bitmap
*bitmap
)
754 struct page
**map
, *sb_page
;
759 spin_lock_irqsave(&bitmap
->lock
, flags
);
760 map
= bitmap
->filemap
;
761 bitmap
->filemap
= NULL
;
762 attr
= bitmap
->filemap_attr
;
763 bitmap
->filemap_attr
= NULL
;
764 pages
= bitmap
->file_pages
;
765 bitmap
->file_pages
= 0;
766 sb_page
= bitmap
->sb_page
;
767 bitmap
->sb_page
= NULL
;
768 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
771 if (map
[pages
] != sb_page
) /* 0 is sb_page, release it below */
772 free_buffers(map
[pages
]);
777 free_buffers(sb_page
);
780 static void bitmap_file_put(struct bitmap
*bitmap
)
785 spin_lock_irqsave(&bitmap
->lock
, flags
);
788 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
791 wait_event(bitmap
->write_wait
,
792 atomic_read(&bitmap
->pending_writes
)==0);
793 bitmap_file_unmap(bitmap
);
796 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
797 invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
803 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
804 * then it is no longer reliable, so we stop using it and we mark the file
805 * as failed in the superblock
807 static void bitmap_file_kick(struct bitmap
*bitmap
)
809 char *path
, *ptr
= NULL
;
811 if (bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_SET
) == 0) {
812 bitmap_update_sb(bitmap
);
815 path
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
817 ptr
= d_path(&bitmap
->file
->f_path
, path
,
821 "%s: kicking failed bitmap file %s from array!\n",
822 bmname(bitmap
), IS_ERR(ptr
) ? "" : ptr
);
827 "%s: disabling internal bitmap due to errors\n",
831 bitmap_file_put(bitmap
);
836 enum bitmap_page_attr
{
837 BITMAP_PAGE_DIRTY
= 0, /* there are set bits that need to be synced */
838 BITMAP_PAGE_PENDING
= 1, /* there are bits that are being cleaned.
839 * i.e. counter is 1 or 2. */
840 BITMAP_PAGE_NEEDWRITE
= 2, /* there are cleared bits that need to be synced */
843 static inline void set_page_attr(struct bitmap
*bitmap
, struct page
*page
,
844 enum bitmap_page_attr attr
)
846 __set_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
849 static inline void clear_page_attr(struct bitmap
*bitmap
, struct page
*page
,
850 enum bitmap_page_attr attr
)
852 __clear_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
855 static inline unsigned long test_page_attr(struct bitmap
*bitmap
, struct page
*page
,
856 enum bitmap_page_attr attr
)
858 return test_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
862 * bitmap_file_set_bit -- called before performing a write to the md device
863 * to set (and eventually sync) a particular bit in the bitmap file
865 * we set the bit immediately, then we record the page number so that
866 * when an unplug occurs, we can flush the dirty pages out to disk
868 static void bitmap_file_set_bit(struct bitmap
*bitmap
, sector_t block
)
873 unsigned long chunk
= block
>> CHUNK_BLOCK_SHIFT(bitmap
);
875 if (!bitmap
->filemap
)
878 page
= filemap_get_page(bitmap
, chunk
);
881 bit
= file_page_offset(bitmap
, chunk
);
884 kaddr
= kmap_atomic(page
, KM_USER0
);
885 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
888 __set_bit_le(bit
, kaddr
);
889 kunmap_atomic(kaddr
, KM_USER0
);
890 pr_debug("set file bit %lu page %lu\n", bit
, page
->index
);
891 /* record page number so it gets flushed to disk when unplug occurs */
892 set_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
895 /* this gets called when the md device is ready to unplug its underlying
896 * (slave) device queues -- before we let any writes go down, we need to
897 * sync the dirty pages of the bitmap file to disk */
898 void bitmap_unplug(struct bitmap
*bitmap
)
900 unsigned long i
, flags
;
901 int dirty
, need_write
;
908 /* look at each page to see if there are any set bits that need to be
909 * flushed out to disk */
910 for (i
= 0; i
< bitmap
->file_pages
; i
++) {
911 spin_lock_irqsave(&bitmap
->lock
, flags
);
912 if (!bitmap
->filemap
) {
913 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
916 page
= bitmap
->filemap
[i
];
917 dirty
= test_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
918 need_write
= test_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
919 clear_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
920 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
923 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
925 if (dirty
|| need_write
)
926 write_page(bitmap
, page
, 0);
928 if (wait
) { /* if any writes were performed, we need to wait on them */
930 wait_event(bitmap
->write_wait
,
931 atomic_read(&bitmap
->pending_writes
)==0);
933 md_super_wait(bitmap
->mddev
);
935 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
936 bitmap_file_kick(bitmap
);
938 EXPORT_SYMBOL(bitmap_unplug
);
940 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
);
941 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
942 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
943 * memory mapping of the bitmap file
945 * if there's no bitmap file, or if the bitmap file had been
946 * previously kicked from the array, we mark all the bits as
947 * 1's in order to cause a full resync.
949 * We ignore all bits for sectors that end earlier than 'start'.
950 * This is used when reading an out-of-date bitmap...
952 static int bitmap_init_from_disk(struct bitmap
*bitmap
, sector_t start
)
954 unsigned long i
, chunks
, index
, oldindex
, bit
;
955 struct page
*page
= NULL
, *oldpage
= NULL
;
956 unsigned long num_pages
, bit_cnt
= 0;
958 unsigned long bytes
, offset
;
963 chunks
= bitmap
->chunks
;
966 BUG_ON(!file
&& !bitmap
->mddev
->bitmap_info
.offset
);
968 outofdate
= bitmap
->flags
& BITMAP_STALE
;
970 printk(KERN_INFO
"%s: bitmap file is out of date, doing full "
971 "recovery\n", bmname(bitmap
));
973 bytes
= DIV_ROUND_UP(bitmap
->chunks
, 8);
974 if (!bitmap
->mddev
->bitmap_info
.external
)
975 bytes
+= sizeof(bitmap_super_t
);
977 num_pages
= DIV_ROUND_UP(bytes
, PAGE_SIZE
);
979 if (file
&& i_size_read(file
->f_mapping
->host
) < bytes
) {
980 printk(KERN_INFO
"%s: bitmap file too short %lu < %lu\n",
982 (unsigned long) i_size_read(file
->f_mapping
->host
),
989 bitmap
->filemap
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
990 if (!bitmap
->filemap
)
993 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
994 bitmap
->filemap_attr
= kzalloc(
995 roundup(DIV_ROUND_UP(num_pages
*4, 8), sizeof(unsigned long)),
997 if (!bitmap
->filemap_attr
)
1002 for (i
= 0; i
< chunks
; i
++) {
1004 index
= file_page_index(bitmap
, i
);
1005 bit
= file_page_offset(bitmap
, i
);
1006 if (index
!= oldindex
) { /* this is a new page, read it in */
1008 /* unmap the old page, we're done with it */
1009 if (index
== num_pages
-1)
1010 count
= bytes
- index
* PAGE_SIZE
;
1013 if (index
== 0 && bitmap
->sb_page
) {
1015 * if we're here then the superblock page
1016 * contains some bits (PAGE_SIZE != sizeof sb)
1017 * we've already read it in, so just use it
1019 page
= bitmap
->sb_page
;
1020 offset
= sizeof(bitmap_super_t
);
1022 page
= read_sb_page(
1024 bitmap
->mddev
->bitmap_info
.offset
,
1028 page
= read_page(file
, index
, bitmap
, count
);
1031 page
= read_sb_page(bitmap
->mddev
,
1032 bitmap
->mddev
->bitmap_info
.offset
,
1037 if (IS_ERR(page
)) { /* read error */
1038 ret
= PTR_ERR(page
);
1045 bitmap
->filemap
[bitmap
->file_pages
++] = page
;
1046 bitmap
->last_page_size
= count
;
1050 * if bitmap is out of date, dirty the
1051 * whole page and write it out
1053 paddr
= kmap_atomic(page
, KM_USER0
);
1054 memset(paddr
+ offset
, 0xff,
1055 PAGE_SIZE
- offset
);
1056 kunmap_atomic(paddr
, KM_USER0
);
1057 write_page(bitmap
, page
, 1);
1060 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
1064 paddr
= kmap_atomic(page
, KM_USER0
);
1065 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1066 b
= test_bit(bit
, paddr
);
1068 b
= test_bit_le(bit
, paddr
);
1069 kunmap_atomic(paddr
, KM_USER0
);
1071 /* if the disk bit is set, set the memory bit */
1072 int needed
= ((sector_t
)(i
+1) << (CHUNK_BLOCK_SHIFT(bitmap
))
1074 bitmap_set_memory_bits(bitmap
,
1075 (sector_t
)i
<< CHUNK_BLOCK_SHIFT(bitmap
),
1081 /* everything went OK */
1083 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_UNSET
);
1085 if (bit_cnt
) { /* Kick recovery if any bits were set */
1086 set_bit(MD_RECOVERY_NEEDED
, &bitmap
->mddev
->recovery
);
1087 md_wakeup_thread(bitmap
->mddev
->thread
);
1090 printk(KERN_INFO
"%s: bitmap initialized from disk: "
1091 "read %lu/%lu pages, set %lu of %lu bits\n",
1092 bmname(bitmap
), bitmap
->file_pages
, num_pages
, bit_cnt
, chunks
);
1097 printk(KERN_INFO
"%s: bitmap initialisation failed: %d\n",
1098 bmname(bitmap
), ret
);
1102 void bitmap_write_all(struct bitmap
*bitmap
)
1104 /* We don't actually write all bitmap blocks here,
1105 * just flag them as needing to be written
1109 for (i
= 0; i
< bitmap
->file_pages
; i
++)
1110 set_page_attr(bitmap
, bitmap
->filemap
[i
],
1111 BITMAP_PAGE_NEEDWRITE
);
1112 bitmap
->allclean
= 0;
1115 static void bitmap_count_page(struct bitmap
*bitmap
, sector_t offset
, int inc
)
1117 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1118 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1119 bitmap
->bp
[page
].count
+= inc
;
1120 bitmap_checkfree(bitmap
, page
);
1122 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1123 sector_t offset
, sector_t
*blocks
,
1127 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1131 void bitmap_daemon_work(struct mddev
*mddev
)
1133 struct bitmap
*bitmap
;
1135 unsigned long flags
;
1136 struct page
*page
= NULL
, *lastpage
= NULL
;
1140 /* Use a mutex to guard daemon_work against
1143 mutex_lock(&mddev
->bitmap_info
.mutex
);
1144 bitmap
= mddev
->bitmap
;
1145 if (bitmap
== NULL
) {
1146 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1149 if (time_before(jiffies
, bitmap
->daemon_lastrun
1150 + bitmap
->mddev
->bitmap_info
.daemon_sleep
))
1153 bitmap
->daemon_lastrun
= jiffies
;
1154 if (bitmap
->allclean
) {
1155 bitmap
->mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1158 bitmap
->allclean
= 1;
1160 spin_lock_irqsave(&bitmap
->lock
, flags
);
1161 for (j
= 0; j
< bitmap
->chunks
; j
++) {
1162 bitmap_counter_t
*bmc
;
1163 if (!bitmap
->filemap
)
1164 /* error or shutdown */
1167 page
= filemap_get_page(bitmap
, j
);
1169 if (page
!= lastpage
) {
1170 /* skip this page unless it's marked as needing cleaning */
1171 if (!test_page_attr(bitmap
, page
, BITMAP_PAGE_PENDING
)) {
1172 int need_write
= test_page_attr(bitmap
, page
,
1173 BITMAP_PAGE_NEEDWRITE
);
1175 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1177 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1179 write_page(bitmap
, page
, 0);
1180 spin_lock_irqsave(&bitmap
->lock
, flags
);
1181 j
|= (PAGE_BITS
- 1);
1185 /* grab the new page, sync and release the old */
1186 if (lastpage
!= NULL
) {
1187 if (test_page_attr(bitmap
, lastpage
,
1188 BITMAP_PAGE_NEEDWRITE
)) {
1189 clear_page_attr(bitmap
, lastpage
,
1190 BITMAP_PAGE_NEEDWRITE
);
1191 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1192 write_page(bitmap
, lastpage
, 0);
1194 set_page_attr(bitmap
, lastpage
,
1195 BITMAP_PAGE_NEEDWRITE
);
1196 bitmap
->allclean
= 0;
1197 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1200 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1203 /* We are possibly going to clear some bits, so make
1204 * sure that events_cleared is up-to-date.
1206 if (bitmap
->need_sync
&&
1207 bitmap
->mddev
->bitmap_info
.external
== 0) {
1209 bitmap
->need_sync
= 0;
1210 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
1211 sb
->events_cleared
=
1212 cpu_to_le64(bitmap
->events_cleared
);
1213 kunmap_atomic(sb
, KM_USER0
);
1214 write_page(bitmap
, bitmap
->sb_page
, 1);
1216 spin_lock_irqsave(&bitmap
->lock
, flags
);
1217 if (!bitmap
->need_sync
)
1218 clear_page_attr(bitmap
, page
, BITMAP_PAGE_PENDING
);
1220 bitmap
->allclean
= 0;
1222 bmc
= bitmap_get_counter(bitmap
,
1223 (sector_t
)j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1226 j
|= PAGE_COUNTER_MASK
;
1228 if (*bmc
== 1 && !bitmap
->need_sync
) {
1229 /* we can clear the bit */
1231 bitmap_count_page(bitmap
,
1232 (sector_t
)j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1236 paddr
= kmap_atomic(page
, KM_USER0
);
1237 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1238 clear_bit(file_page_offset(bitmap
, j
),
1242 file_page_offset(bitmap
,
1245 kunmap_atomic(paddr
, KM_USER0
);
1246 } else if (*bmc
<= 2) {
1247 *bmc
= 1; /* maybe clear the bit next time */
1248 set_page_attr(bitmap
, page
, BITMAP_PAGE_PENDING
);
1249 bitmap
->allclean
= 0;
1253 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1255 /* now sync the final page */
1256 if (lastpage
!= NULL
) {
1257 spin_lock_irqsave(&bitmap
->lock
, flags
);
1258 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1259 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1260 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1261 write_page(bitmap
, lastpage
, 0);
1263 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1264 bitmap
->allclean
= 0;
1265 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1270 if (bitmap
->allclean
== 0)
1271 bitmap
->mddev
->thread
->timeout
=
1272 bitmap
->mddev
->bitmap_info
.daemon_sleep
;
1273 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1276 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1277 sector_t offset
, sector_t
*blocks
,
1279 __releases(bitmap
->lock
)
1280 __acquires(bitmap
->lock
)
1282 /* If 'create', we might release the lock and reclaim it.
1283 * The lock must have been taken with interrupts enabled.
1284 * If !create, we don't release the lock.
1286 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1287 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1288 unsigned long pageoff
= (chunk
& PAGE_COUNTER_MASK
) << COUNTER_BYTE_SHIFT
;
1292 err
= bitmap_checkpage(bitmap
, page
, create
);
1294 if (bitmap
->bp
[page
].hijacked
||
1295 bitmap
->bp
[page
].map
== NULL
)
1296 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
) +
1297 PAGE_COUNTER_SHIFT
- 1);
1299 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1300 *blocks
= csize
- (offset
& (csize
- 1));
1305 /* now locked ... */
1307 if (bitmap
->bp
[page
].hijacked
) { /* hijacked pointer */
1308 /* should we use the first or second counter field
1309 * of the hijacked pointer? */
1310 int hi
= (pageoff
> PAGE_COUNTER_MASK
);
1311 return &((bitmap_counter_t
*)
1312 &bitmap
->bp
[page
].map
)[hi
];
1313 } else /* page is allocated */
1314 return (bitmap_counter_t
*)
1315 &(bitmap
->bp
[page
].map
[pageoff
]);
1318 int bitmap_startwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
, int behind
)
1325 atomic_inc(&bitmap
->behind_writes
);
1326 bw
= atomic_read(&bitmap
->behind_writes
);
1327 if (bw
> bitmap
->behind_writes_used
)
1328 bitmap
->behind_writes_used
= bw
;
1330 pr_debug("inc write-behind count %d/%lu\n",
1331 bw
, bitmap
->mddev
->bitmap_info
.max_write_behind
);
1336 bitmap_counter_t
*bmc
;
1338 spin_lock_irq(&bitmap
->lock
);
1339 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 1);
1341 spin_unlock_irq(&bitmap
->lock
);
1345 if (unlikely(COUNTER(*bmc
) == COUNTER_MAX
)) {
1346 DEFINE_WAIT(__wait
);
1347 /* note that it is safe to do the prepare_to_wait
1348 * after the test as long as we do it before dropping
1351 prepare_to_wait(&bitmap
->overflow_wait
, &__wait
,
1352 TASK_UNINTERRUPTIBLE
);
1353 spin_unlock_irq(&bitmap
->lock
);
1355 finish_wait(&bitmap
->overflow_wait
, &__wait
);
1361 bitmap_file_set_bit(bitmap
, offset
);
1362 bitmap_count_page(bitmap
, offset
, 1);
1370 spin_unlock_irq(&bitmap
->lock
);
1373 if (sectors
> blocks
)
1380 EXPORT_SYMBOL(bitmap_startwrite
);
1382 void bitmap_endwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
,
1383 int success
, int behind
)
1388 if (atomic_dec_and_test(&bitmap
->behind_writes
))
1389 wake_up(&bitmap
->behind_wait
);
1390 pr_debug("dec write-behind count %d/%lu\n",
1391 atomic_read(&bitmap
->behind_writes
),
1392 bitmap
->mddev
->bitmap_info
.max_write_behind
);
1394 if (bitmap
->mddev
->degraded
)
1395 /* Never clear bits or update events_cleared when degraded */
1400 unsigned long flags
;
1401 bitmap_counter_t
*bmc
;
1403 spin_lock_irqsave(&bitmap
->lock
, flags
);
1404 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 0);
1406 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1411 bitmap
->events_cleared
< bitmap
->mddev
->events
) {
1412 bitmap
->events_cleared
= bitmap
->mddev
->events
;
1413 bitmap
->need_sync
= 1;
1414 sysfs_notify_dirent_safe(bitmap
->sysfs_can_clear
);
1417 if (!success
&& !NEEDED(*bmc
))
1418 *bmc
|= NEEDED_MASK
;
1420 if (COUNTER(*bmc
) == COUNTER_MAX
)
1421 wake_up(&bitmap
->overflow_wait
);
1425 set_page_attr(bitmap
,
1428 offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1429 BITMAP_PAGE_PENDING
);
1430 bitmap
->allclean
= 0;
1432 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1434 if (sectors
> blocks
)
1440 EXPORT_SYMBOL(bitmap_endwrite
);
1442 static int __bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, sector_t
*blocks
,
1445 bitmap_counter_t
*bmc
;
1447 if (bitmap
== NULL
) {/* FIXME or bitmap set as 'failed' */
1449 return 1; /* always resync if no bitmap */
1451 spin_lock_irq(&bitmap
->lock
);
1452 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1458 else if (NEEDED(*bmc
)) {
1460 if (!degraded
) { /* don't set/clear bits if degraded */
1461 *bmc
|= RESYNC_MASK
;
1462 *bmc
&= ~NEEDED_MASK
;
1466 spin_unlock_irq(&bitmap
->lock
);
1470 int bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, sector_t
*blocks
,
1473 /* bitmap_start_sync must always report on multiples of whole
1474 * pages, otherwise resync (which is very PAGE_SIZE based) will
1476 * So call __bitmap_start_sync repeatedly (if needed) until
1477 * At least PAGE_SIZE>>9 blocks are covered.
1478 * Return the 'or' of the result.
1484 while (*blocks
< (PAGE_SIZE
>>9)) {
1485 rv
|= __bitmap_start_sync(bitmap
, offset
,
1486 &blocks1
, degraded
);
1492 EXPORT_SYMBOL(bitmap_start_sync
);
1494 void bitmap_end_sync(struct bitmap
*bitmap
, sector_t offset
, sector_t
*blocks
, int aborted
)
1496 bitmap_counter_t
*bmc
;
1497 unsigned long flags
;
1499 if (bitmap
== NULL
) {
1503 spin_lock_irqsave(&bitmap
->lock
, flags
);
1504 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1509 *bmc
&= ~RESYNC_MASK
;
1511 if (!NEEDED(*bmc
) && aborted
)
1512 *bmc
|= NEEDED_MASK
;
1515 set_page_attr(bitmap
,
1516 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1517 BITMAP_PAGE_PENDING
);
1518 bitmap
->allclean
= 0;
1523 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1525 EXPORT_SYMBOL(bitmap_end_sync
);
1527 void bitmap_close_sync(struct bitmap
*bitmap
)
1529 /* Sync has finished, and any bitmap chunks that weren't synced
1530 * properly have been aborted. It remains to us to clear the
1531 * RESYNC bit wherever it is still on
1533 sector_t sector
= 0;
1537 while (sector
< bitmap
->mddev
->resync_max_sectors
) {
1538 bitmap_end_sync(bitmap
, sector
, &blocks
, 0);
1542 EXPORT_SYMBOL(bitmap_close_sync
);
1544 void bitmap_cond_end_sync(struct bitmap
*bitmap
, sector_t sector
)
1552 bitmap
->last_end_sync
= jiffies
;
1555 if (time_before(jiffies
, (bitmap
->last_end_sync
1556 + bitmap
->mddev
->bitmap_info
.daemon_sleep
)))
1558 wait_event(bitmap
->mddev
->recovery_wait
,
1559 atomic_read(&bitmap
->mddev
->recovery_active
) == 0);
1561 bitmap
->mddev
->curr_resync_completed
= sector
;
1562 set_bit(MD_CHANGE_CLEAN
, &bitmap
->mddev
->flags
);
1563 sector
&= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap
)) - 1);
1565 while (s
< sector
&& s
< bitmap
->mddev
->resync_max_sectors
) {
1566 bitmap_end_sync(bitmap
, s
, &blocks
, 0);
1569 bitmap
->last_end_sync
= jiffies
;
1570 sysfs_notify(&bitmap
->mddev
->kobj
, NULL
, "sync_completed");
1572 EXPORT_SYMBOL(bitmap_cond_end_sync
);
1574 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
)
1576 /* For each chunk covered by any of these sectors, set the
1577 * counter to 1 and set resync_needed. They should all
1578 * be 0 at this point
1582 bitmap_counter_t
*bmc
;
1583 spin_lock_irq(&bitmap
->lock
);
1584 bmc
= bitmap_get_counter(bitmap
, offset
, &secs
, 1);
1586 spin_unlock_irq(&bitmap
->lock
);
1591 *bmc
= 1 | (needed
? NEEDED_MASK
: 0);
1592 bitmap_count_page(bitmap
, offset
, 1);
1593 page
= filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
));
1594 set_page_attr(bitmap
, page
, BITMAP_PAGE_PENDING
);
1595 bitmap
->allclean
= 0;
1597 spin_unlock_irq(&bitmap
->lock
);
1600 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1601 void bitmap_dirty_bits(struct bitmap
*bitmap
, unsigned long s
, unsigned long e
)
1603 unsigned long chunk
;
1605 for (chunk
= s
; chunk
<= e
; chunk
++) {
1606 sector_t sec
= (sector_t
)chunk
<< CHUNK_BLOCK_SHIFT(bitmap
);
1607 bitmap_set_memory_bits(bitmap
, sec
, 1);
1608 bitmap_file_set_bit(bitmap
, sec
);
1609 if (sec
< bitmap
->mddev
->recovery_cp
)
1610 /* We are asserting that the array is dirty,
1611 * so move the recovery_cp address back so
1612 * that it is obvious that it is dirty
1614 bitmap
->mddev
->recovery_cp
= sec
;
1619 * flush out any pending updates
1621 void bitmap_flush(struct mddev
*mddev
)
1623 struct bitmap
*bitmap
= mddev
->bitmap
;
1626 if (!bitmap
) /* there was no bitmap */
1629 /* run the daemon_work three time to ensure everything is flushed
1632 sleep
= mddev
->bitmap_info
.daemon_sleep
* 2;
1633 bitmap
->daemon_lastrun
-= sleep
;
1634 bitmap_daemon_work(mddev
);
1635 bitmap
->daemon_lastrun
-= sleep
;
1636 bitmap_daemon_work(mddev
);
1637 bitmap
->daemon_lastrun
-= sleep
;
1638 bitmap_daemon_work(mddev
);
1639 bitmap_update_sb(bitmap
);
1643 * free memory that was allocated
1645 static void bitmap_free(struct bitmap
*bitmap
)
1647 unsigned long k
, pages
;
1648 struct bitmap_page
*bp
;
1650 if (!bitmap
) /* there was no bitmap */
1653 /* release the bitmap file and kill the daemon */
1654 bitmap_file_put(bitmap
);
1657 pages
= bitmap
->pages
;
1659 /* free all allocated memory */
1661 if (bp
) /* deallocate the page memory */
1662 for (k
= 0; k
< pages
; k
++)
1663 if (bp
[k
].map
&& !bp
[k
].hijacked
)
1669 void bitmap_destroy(struct mddev
*mddev
)
1671 struct bitmap
*bitmap
= mddev
->bitmap
;
1673 if (!bitmap
) /* there was no bitmap */
1676 mutex_lock(&mddev
->bitmap_info
.mutex
);
1677 mddev
->bitmap
= NULL
; /* disconnect from the md device */
1678 mutex_unlock(&mddev
->bitmap_info
.mutex
);
1680 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1682 if (bitmap
->sysfs_can_clear
)
1683 sysfs_put(bitmap
->sysfs_can_clear
);
1685 bitmap_free(bitmap
);
1689 * initialize the bitmap structure
1690 * if this returns an error, bitmap_destroy must be called to do clean up
1692 int bitmap_create(struct mddev
*mddev
)
1694 struct bitmap
*bitmap
;
1695 sector_t blocks
= mddev
->resync_max_sectors
;
1696 unsigned long chunks
;
1697 unsigned long pages
;
1698 struct file
*file
= mddev
->bitmap_info
.file
;
1700 struct sysfs_dirent
*bm
= NULL
;
1702 BUILD_BUG_ON(sizeof(bitmap_super_t
) != 256);
1705 && !mddev
->bitmap_info
.offset
) /* bitmap disabled, nothing to do */
1708 BUG_ON(file
&& mddev
->bitmap_info
.offset
);
1710 bitmap
= kzalloc(sizeof(*bitmap
), GFP_KERNEL
);
1714 spin_lock_init(&bitmap
->lock
);
1715 atomic_set(&bitmap
->pending_writes
, 0);
1716 init_waitqueue_head(&bitmap
->write_wait
);
1717 init_waitqueue_head(&bitmap
->overflow_wait
);
1718 init_waitqueue_head(&bitmap
->behind_wait
);
1720 bitmap
->mddev
= mddev
;
1723 bm
= sysfs_get_dirent(mddev
->kobj
.sd
, NULL
, "bitmap");
1725 bitmap
->sysfs_can_clear
= sysfs_get_dirent(bm
, NULL
, "can_clear");
1728 bitmap
->sysfs_can_clear
= NULL
;
1730 bitmap
->file
= file
;
1733 /* As future accesses to this file will use bmap,
1734 * and bypass the page cache, we must sync the file
1739 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
1740 if (!mddev
->bitmap_info
.external
) {
1742 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1743 * instructing us to create a new on-disk bitmap instance.
1745 if (test_and_clear_bit(MD_ARRAY_FIRST_USE
, &mddev
->flags
))
1746 err
= bitmap_new_disk_sb(bitmap
);
1748 err
= bitmap_read_sb(bitmap
);
1751 if (mddev
->bitmap_info
.chunksize
== 0 ||
1752 mddev
->bitmap_info
.daemon_sleep
== 0)
1753 /* chunksize and time_base need to be
1760 bitmap
->daemon_lastrun
= jiffies
;
1761 bitmap
->chunkshift
= ffz(~mddev
->bitmap_info
.chunksize
);
1763 /* now that chunksize and chunkshift are set, we can use these macros */
1764 chunks
= (blocks
+ CHUNK_BLOCK_RATIO(bitmap
) - 1) >>
1765 CHUNK_BLOCK_SHIFT(bitmap
);
1766 pages
= (chunks
+ PAGE_COUNTER_RATIO
- 1) / PAGE_COUNTER_RATIO
;
1770 bitmap
->chunks
= chunks
;
1771 bitmap
->pages
= pages
;
1772 bitmap
->missing_pages
= pages
;
1774 bitmap
->bp
= kzalloc(pages
* sizeof(*bitmap
->bp
), GFP_KERNEL
);
1780 printk(KERN_INFO
"created bitmap (%lu pages) for device %s\n",
1781 pages
, bmname(bitmap
));
1783 mddev
->bitmap
= bitmap
;
1786 return (bitmap
->flags
& BITMAP_WRITE_ERROR
) ? -EIO
: 0;
1789 bitmap_free(bitmap
);
1793 int bitmap_load(struct mddev
*mddev
)
1797 sector_t sector
= 0;
1798 struct bitmap
*bitmap
= mddev
->bitmap
;
1803 /* Clear out old bitmap info first: Either there is none, or we
1804 * are resuming after someone else has possibly changed things,
1805 * so we should forget old cached info.
1806 * All chunks should be clean, but some might need_sync.
1808 while (sector
< mddev
->resync_max_sectors
) {
1810 bitmap_start_sync(bitmap
, sector
, &blocks
, 0);
1813 bitmap_close_sync(bitmap
);
1815 if (mddev
->degraded
== 0
1816 || bitmap
->events_cleared
== mddev
->events
)
1817 /* no need to keep dirty bits to optimise a
1818 * re-add of a missing device */
1819 start
= mddev
->recovery_cp
;
1821 err
= bitmap_init_from_disk(bitmap
, start
);
1826 mddev
->thread
->timeout
= mddev
->bitmap_info
.daemon_sleep
;
1827 md_wakeup_thread(mddev
->thread
);
1829 bitmap_update_sb(bitmap
);
1831 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
1836 EXPORT_SYMBOL_GPL(bitmap_load
);
1839 location_show(struct mddev
*mddev
, char *page
)
1842 if (mddev
->bitmap_info
.file
)
1843 len
= sprintf(page
, "file");
1844 else if (mddev
->bitmap_info
.offset
)
1845 len
= sprintf(page
, "%+lld", (long long)mddev
->bitmap_info
.offset
);
1847 len
= sprintf(page
, "none");
1848 len
+= sprintf(page
+len
, "\n");
1853 location_store(struct mddev
*mddev
, const char *buf
, size_t len
)
1857 if (!mddev
->pers
->quiesce
)
1859 if (mddev
->recovery
|| mddev
->sync_thread
)
1863 if (mddev
->bitmap
|| mddev
->bitmap_info
.file
||
1864 mddev
->bitmap_info
.offset
) {
1865 /* bitmap already configured. Only option is to clear it */
1866 if (strncmp(buf
, "none", 4) != 0)
1869 mddev
->pers
->quiesce(mddev
, 1);
1870 bitmap_destroy(mddev
);
1871 mddev
->pers
->quiesce(mddev
, 0);
1873 mddev
->bitmap_info
.offset
= 0;
1874 if (mddev
->bitmap_info
.file
) {
1875 struct file
*f
= mddev
->bitmap_info
.file
;
1876 mddev
->bitmap_info
.file
= NULL
;
1877 restore_bitmap_write_access(f
);
1881 /* No bitmap, OK to set a location */
1883 if (strncmp(buf
, "none", 4) == 0)
1884 /* nothing to be done */;
1885 else if (strncmp(buf
, "file:", 5) == 0) {
1886 /* Not supported yet */
1891 rv
= strict_strtoll(buf
+1, 10, &offset
);
1893 rv
= strict_strtoll(buf
, 10, &offset
);
1898 if (mddev
->bitmap_info
.external
== 0 &&
1899 mddev
->major_version
== 0 &&
1900 offset
!= mddev
->bitmap_info
.default_offset
)
1902 mddev
->bitmap_info
.offset
= offset
;
1904 mddev
->pers
->quiesce(mddev
, 1);
1905 rv
= bitmap_create(mddev
);
1907 bitmap_destroy(mddev
);
1908 mddev
->bitmap_info
.offset
= 0;
1910 mddev
->pers
->quiesce(mddev
, 0);
1916 if (!mddev
->external
) {
1917 /* Ensure new bitmap info is stored in
1918 * metadata promptly.
1920 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1921 md_wakeup_thread(mddev
->thread
);
1926 static struct md_sysfs_entry bitmap_location
=
1927 __ATTR(location
, S_IRUGO
|S_IWUSR
, location_show
, location_store
);
1930 timeout_show(struct mddev
*mddev
, char *page
)
1933 unsigned long secs
= mddev
->bitmap_info
.daemon_sleep
/ HZ
;
1934 unsigned long jifs
= mddev
->bitmap_info
.daemon_sleep
% HZ
;
1936 len
= sprintf(page
, "%lu", secs
);
1938 len
+= sprintf(page
+len
, ".%03u", jiffies_to_msecs(jifs
));
1939 len
+= sprintf(page
+len
, "\n");
1944 timeout_store(struct mddev
*mddev
, const char *buf
, size_t len
)
1946 /* timeout can be set at any time */
1947 unsigned long timeout
;
1948 int rv
= strict_strtoul_scaled(buf
, &timeout
, 4);
1952 /* just to make sure we don't overflow... */
1953 if (timeout
>= LONG_MAX
/ HZ
)
1956 timeout
= timeout
* HZ
/ 10000;
1958 if (timeout
>= MAX_SCHEDULE_TIMEOUT
)
1959 timeout
= MAX_SCHEDULE_TIMEOUT
-1;
1962 mddev
->bitmap_info
.daemon_sleep
= timeout
;
1963 if (mddev
->thread
) {
1964 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1965 * the bitmap is all clean and we don't need to
1966 * adjust the timeout right now
1968 if (mddev
->thread
->timeout
< MAX_SCHEDULE_TIMEOUT
) {
1969 mddev
->thread
->timeout
= timeout
;
1970 md_wakeup_thread(mddev
->thread
);
1976 static struct md_sysfs_entry bitmap_timeout
=
1977 __ATTR(time_base
, S_IRUGO
|S_IWUSR
, timeout_show
, timeout_store
);
1980 backlog_show(struct mddev
*mddev
, char *page
)
1982 return sprintf(page
, "%lu\n", mddev
->bitmap_info
.max_write_behind
);
1986 backlog_store(struct mddev
*mddev
, const char *buf
, size_t len
)
1988 unsigned long backlog
;
1989 int rv
= strict_strtoul(buf
, 10, &backlog
);
1992 if (backlog
> COUNTER_MAX
)
1994 mddev
->bitmap_info
.max_write_behind
= backlog
;
1998 static struct md_sysfs_entry bitmap_backlog
=
1999 __ATTR(backlog
, S_IRUGO
|S_IWUSR
, backlog_show
, backlog_store
);
2002 chunksize_show(struct mddev
*mddev
, char *page
)
2004 return sprintf(page
, "%lu\n", mddev
->bitmap_info
.chunksize
);
2008 chunksize_store(struct mddev
*mddev
, const char *buf
, size_t len
)
2010 /* Can only be changed when no bitmap is active */
2012 unsigned long csize
;
2015 rv
= strict_strtoul(buf
, 10, &csize
);
2019 !is_power_of_2(csize
))
2021 mddev
->bitmap_info
.chunksize
= csize
;
2025 static struct md_sysfs_entry bitmap_chunksize
=
2026 __ATTR(chunksize
, S_IRUGO
|S_IWUSR
, chunksize_show
, chunksize_store
);
2028 static ssize_t
metadata_show(struct mddev
*mddev
, char *page
)
2030 return sprintf(page
, "%s\n", (mddev
->bitmap_info
.external
2031 ? "external" : "internal"));
2034 static ssize_t
metadata_store(struct mddev
*mddev
, const char *buf
, size_t len
)
2036 if (mddev
->bitmap
||
2037 mddev
->bitmap_info
.file
||
2038 mddev
->bitmap_info
.offset
)
2040 if (strncmp(buf
, "external", 8) == 0)
2041 mddev
->bitmap_info
.external
= 1;
2042 else if (strncmp(buf
, "internal", 8) == 0)
2043 mddev
->bitmap_info
.external
= 0;
2049 static struct md_sysfs_entry bitmap_metadata
=
2050 __ATTR(metadata
, S_IRUGO
|S_IWUSR
, metadata_show
, metadata_store
);
2052 static ssize_t
can_clear_show(struct mddev
*mddev
, char *page
)
2056 len
= sprintf(page
, "%s\n", (mddev
->bitmap
->need_sync
?
2059 len
= sprintf(page
, "\n");
2063 static ssize_t
can_clear_store(struct mddev
*mddev
, const char *buf
, size_t len
)
2065 if (mddev
->bitmap
== NULL
)
2067 if (strncmp(buf
, "false", 5) == 0)
2068 mddev
->bitmap
->need_sync
= 1;
2069 else if (strncmp(buf
, "true", 4) == 0) {
2070 if (mddev
->degraded
)
2072 mddev
->bitmap
->need_sync
= 0;
2078 static struct md_sysfs_entry bitmap_can_clear
=
2079 __ATTR(can_clear
, S_IRUGO
|S_IWUSR
, can_clear_show
, can_clear_store
);
2082 behind_writes_used_show(struct mddev
*mddev
, char *page
)
2084 if (mddev
->bitmap
== NULL
)
2085 return sprintf(page
, "0\n");
2086 return sprintf(page
, "%lu\n",
2087 mddev
->bitmap
->behind_writes_used
);
2091 behind_writes_used_reset(struct mddev
*mddev
, const char *buf
, size_t len
)
2094 mddev
->bitmap
->behind_writes_used
= 0;
2098 static struct md_sysfs_entry max_backlog_used
=
2099 __ATTR(max_backlog_used
, S_IRUGO
| S_IWUSR
,
2100 behind_writes_used_show
, behind_writes_used_reset
);
2102 static struct attribute
*md_bitmap_attrs
[] = {
2103 &bitmap_location
.attr
,
2104 &bitmap_timeout
.attr
,
2105 &bitmap_backlog
.attr
,
2106 &bitmap_chunksize
.attr
,
2107 &bitmap_metadata
.attr
,
2108 &bitmap_can_clear
.attr
,
2109 &max_backlog_used
.attr
,
2112 struct attribute_group md_bitmap_group
= {
2114 .attrs
= md_bitmap_attrs
,