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 */
55 # define PRINTK(x...) printk(KERN_DEBUG x)
61 static inline char *bmname(struct bitmap *bitmap)
63 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
67 * just a placeholder - calls kmalloc for bitmap pages
69 static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
73 #ifdef INJECT_FAULTS_1
76 page = kzalloc(PAGE_SIZE, GFP_NOIO);
79 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
81 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
82 bmname(bitmap), page);
87 * for now just a placeholder -- just calls kfree for bitmap pages
89 static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
91 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
96 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
98 * 1) check to see if this page is allocated, if it's not then try to alloc
99 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
100 * page pointer directly as a counter
102 * if we find our page, we increment the page's refcount so that it stays
103 * allocated while we're using it
105 static int bitmap_checkpage(struct bitmap *bitmap,
106 unsigned long page, int create)
107 __releases(bitmap->lock)
108 __acquires(bitmap->lock)
110 unsigned char *mappage;
112 if (page >= bitmap->pages) {
113 /* This can happen if bitmap_start_sync goes beyond
114 * End-of-device while looking for a whole page.
120 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
123 if (bitmap->bp[page].map) /* page is already allocated, just return */
129 /* this page has not been allocated yet */
131 spin_unlock_irq(&bitmap->lock);
132 mappage = bitmap_alloc_page(bitmap);
133 spin_lock_irq(&bitmap->lock);
135 if (mappage == NULL) {
136 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
138 /* failed - set the hijacked flag so that we can use the
139 * pointer as a counter */
140 if (!bitmap->bp[page].map)
141 bitmap->bp[page].hijacked = 1;
142 } else if (bitmap->bp[page].map ||
143 bitmap->bp[page].hijacked) {
144 /* somebody beat us to getting the page */
145 bitmap_free_page(bitmap, mappage);
149 /* no page was in place and we have one, so install it */
151 bitmap->bp[page].map = mappage;
152 bitmap->missing_pages--;
157 /* if page is completely empty, put it back on the free list, or dealloc it */
158 /* if page was hijacked, unmark the flag so it might get alloced next time */
159 /* Note: lock should be held when calling this */
160 static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
164 if (bitmap->bp[page].count) /* page is still busy */
167 /* page is no longer in use, it can be released */
169 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
170 bitmap->bp[page].hijacked = 0;
171 bitmap->bp[page].map = NULL;
173 /* normal case, free the page */
174 ptr = bitmap->bp[page].map;
175 bitmap->bp[page].map = NULL;
176 bitmap->missing_pages++;
177 bitmap_free_page(bitmap, ptr);
182 * bitmap file handling - read and write the bitmap file and its superblock
186 * basic page I/O operations
189 /* IO operations when bitmap is stored near all superblocks */
190 static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
192 unsigned long index, int size)
194 /* choose a good rdev and read the page from there */
201 page = alloc_page(GFP_KERNEL);
203 return ERR_PTR(-ENOMEM);
207 list_for_each_entry(rdev, &mddev->disks, same_set) {
208 if (! test_bit(In_sync, &rdev->flags)
209 || test_bit(Faulty, &rdev->flags))
212 target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
214 if (sync_page_io(rdev->bdev, target,
215 roundup(size, bdev_logical_block_size(rdev->bdev)),
218 attach_page_buffers(page, NULL); /* so that free_buffer will
225 return ERR_PTR(-EIO);
229 static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
231 /* Iterate the disks of an mddev, using rcu to protect access to the
232 * linked list, and raising the refcount of devices we return to ensure
233 * they don't disappear while in use.
234 * As devices are only added or removed when raid_disk is < 0 and
235 * nr_pending is 0 and In_sync is clear, the entries we return will
236 * still be in the same position on the list when we re-enter
237 * list_for_each_continue_rcu.
239 struct list_head *pos;
242 /* start at the beginning */
245 /* release the previous rdev and start from there. */
246 rdev_dec_pending(rdev, mddev);
247 pos = &rdev->same_set;
249 list_for_each_continue_rcu(pos, &mddev->disks) {
250 rdev = list_entry(pos, mdk_rdev_t, same_set);
251 if (rdev->raid_disk >= 0 &&
252 !test_bit(Faulty, &rdev->flags)) {
253 /* this is a usable devices */
254 atomic_inc(&rdev->nr_pending);
263 static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
265 mdk_rdev_t *rdev = NULL;
266 mddev_t *mddev = bitmap->mddev;
268 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
269 int size = PAGE_SIZE;
270 loff_t offset = mddev->bitmap_info.offset;
271 if (page->index == bitmap->file_pages-1)
272 size = roundup(bitmap->last_page_size,
273 bdev_logical_block_size(rdev->bdev));
274 /* Just make sure we aren't corrupting data or
277 if (mddev->external) {
278 /* Bitmap could be anywhere. */
279 if (rdev->sb_start + offset + (page->index
283 rdev->sb_start + offset
284 < (rdev->data_offset + mddev->dev_sectors
287 } else if (offset < 0) {
288 /* DATA BITMAP METADATA */
290 + (long)(page->index * (PAGE_SIZE/512))
292 /* bitmap runs in to metadata */
294 if (rdev->data_offset + mddev->dev_sectors
295 > rdev->sb_start + offset)
296 /* data runs in to bitmap */
298 } else if (rdev->sb_start < rdev->data_offset) {
299 /* METADATA BITMAP DATA */
302 + page->index*(PAGE_SIZE/512) + size/512
304 /* bitmap runs in to data */
307 /* DATA METADATA BITMAP - no problems */
309 md_super_write(mddev, rdev,
310 rdev->sb_start + offset
311 + page->index * (PAGE_SIZE/512),
317 md_super_wait(mddev);
324 static void bitmap_file_kick(struct bitmap *bitmap);
326 * write out a page to a file
328 static void write_page(struct bitmap *bitmap, struct page *page, int wait)
330 struct buffer_head *bh;
332 if (bitmap->file == NULL) {
333 switch (write_sb_page(bitmap, page, wait)) {
335 bitmap->flags |= BITMAP_WRITE_ERROR;
339 bh = page_buffers(page);
341 while (bh && bh->b_blocknr) {
342 atomic_inc(&bitmap->pending_writes);
343 set_buffer_locked(bh);
344 set_buffer_mapped(bh);
345 submit_bh(WRITE, bh);
346 bh = bh->b_this_page;
350 wait_event(bitmap->write_wait,
351 atomic_read(&bitmap->pending_writes)==0);
353 if (bitmap->flags & BITMAP_WRITE_ERROR)
354 bitmap_file_kick(bitmap);
357 static void end_bitmap_write(struct buffer_head *bh, int uptodate)
359 struct bitmap *bitmap = bh->b_private;
363 spin_lock_irqsave(&bitmap->lock, flags);
364 bitmap->flags |= BITMAP_WRITE_ERROR;
365 spin_unlock_irqrestore(&bitmap->lock, flags);
367 if (atomic_dec_and_test(&bitmap->pending_writes))
368 wake_up(&bitmap->write_wait);
371 /* copied from buffer.c */
373 __clear_page_buffers(struct page *page)
375 ClearPagePrivate(page);
376 set_page_private(page, 0);
377 page_cache_release(page);
379 static void free_buffers(struct page *page)
381 struct buffer_head *bh = page_buffers(page);
384 struct buffer_head *next = bh->b_this_page;
385 free_buffer_head(bh);
388 __clear_page_buffers(page);
392 /* read a page from a file.
393 * We both read the page, and attach buffers to the page to record the
394 * address of each block (using bmap). These addresses will be used
395 * to write the block later, completely bypassing the filesystem.
396 * This usage is similar to how swap files are handled, and allows us
397 * to write to a file with no concerns of memory allocation failing.
399 static struct page *read_page(struct file *file, unsigned long index,
400 struct bitmap *bitmap,
403 struct page *page = NULL;
404 struct inode *inode = file->f_path.dentry->d_inode;
405 struct buffer_head *bh;
408 PRINTK("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
409 (unsigned long long)index << PAGE_SHIFT);
411 page = alloc_page(GFP_KERNEL);
413 page = ERR_PTR(-ENOMEM);
417 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
420 page = ERR_PTR(-ENOMEM);
423 attach_page_buffers(page, bh);
424 block = index << (PAGE_SHIFT - inode->i_blkbits);
429 bh->b_blocknr = bmap(inode, block);
430 if (bh->b_blocknr == 0) {
431 /* Cannot use this file! */
433 page = ERR_PTR(-EINVAL);
436 bh->b_bdev = inode->i_sb->s_bdev;
437 if (count < (1<<inode->i_blkbits))
440 count -= (1<<inode->i_blkbits);
442 bh->b_end_io = end_bitmap_write;
443 bh->b_private = bitmap;
444 atomic_inc(&bitmap->pending_writes);
445 set_buffer_locked(bh);
446 set_buffer_mapped(bh);
450 bh = bh->b_this_page;
454 wait_event(bitmap->write_wait,
455 atomic_read(&bitmap->pending_writes)==0);
456 if (bitmap->flags & BITMAP_WRITE_ERROR) {
458 page = ERR_PTR(-EIO);
462 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
464 (unsigned long long)index << PAGE_SHIFT,
470 * bitmap file superblock operations
473 /* update the event counter and sync the superblock to disk */
474 void bitmap_update_sb(struct bitmap *bitmap)
479 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
481 if (bitmap->mddev->bitmap_info.external)
483 spin_lock_irqsave(&bitmap->lock, flags);
484 if (!bitmap->sb_page) { /* no superblock */
485 spin_unlock_irqrestore(&bitmap->lock, flags);
488 spin_unlock_irqrestore(&bitmap->lock, flags);
489 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
490 sb->events = cpu_to_le64(bitmap->mddev->events);
491 if (bitmap->mddev->events < bitmap->events_cleared) {
492 /* rocking back to read-only */
493 bitmap->events_cleared = bitmap->mddev->events;
494 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
496 /* Just in case these have been changed via sysfs: */
497 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
498 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
499 kunmap_atomic(sb, KM_USER0);
500 write_page(bitmap, bitmap->sb_page, 1);
503 /* print out the bitmap file superblock */
504 void bitmap_print_sb(struct bitmap *bitmap)
508 if (!bitmap || !bitmap->sb_page)
510 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
511 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
512 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
513 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
514 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
515 *(__u32 *)(sb->uuid+0),
516 *(__u32 *)(sb->uuid+4),
517 *(__u32 *)(sb->uuid+8),
518 *(__u32 *)(sb->uuid+12));
519 printk(KERN_DEBUG " events: %llu\n",
520 (unsigned long long) le64_to_cpu(sb->events));
521 printk(KERN_DEBUG "events cleared: %llu\n",
522 (unsigned long long) le64_to_cpu(sb->events_cleared));
523 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
524 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
525 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
526 printk(KERN_DEBUG " sync size: %llu KB\n",
527 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
528 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
529 kunmap_atomic(sb, KM_USER0);
532 /* read the superblock from the bitmap file and initialize some bitmap fields */
533 static int bitmap_read_sb(struct bitmap *bitmap)
537 unsigned long chunksize, daemon_sleep, write_behind;
538 unsigned long long events;
541 /* page 0 is the superblock, read it... */
543 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
544 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
546 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
548 bitmap->sb_page = read_sb_page(bitmap->mddev,
549 bitmap->mddev->bitmap_info.offset,
551 0, sizeof(bitmap_super_t));
553 if (IS_ERR(bitmap->sb_page)) {
554 err = PTR_ERR(bitmap->sb_page);
555 bitmap->sb_page = NULL;
559 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
561 chunksize = le32_to_cpu(sb->chunksize);
562 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
563 write_behind = le32_to_cpu(sb->write_behind);
565 /* verify that the bitmap-specific fields are valid */
566 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
567 reason = "bad magic";
568 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
569 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
570 reason = "unrecognized superblock version";
571 else if (chunksize < 512)
572 reason = "bitmap chunksize too small";
573 else if ((1 << ffz(~chunksize)) != chunksize)
574 reason = "bitmap chunksize not a power of 2";
575 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
576 reason = "daemon sleep period out of range";
577 else if (write_behind > COUNTER_MAX)
578 reason = "write-behind limit out of range (0 - 16383)";
580 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
581 bmname(bitmap), reason);
585 /* keep the array size field of the bitmap superblock up to date */
586 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
588 if (!bitmap->mddev->persistent)
592 * if we have a persistent array superblock, compare the
593 * bitmap's UUID and event counter to the mddev's
595 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
596 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
600 events = le64_to_cpu(sb->events);
601 if (events < bitmap->mddev->events) {
602 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
603 "-- forcing full recovery\n", bmname(bitmap), events,
604 (unsigned long long) bitmap->mddev->events);
605 sb->state |= cpu_to_le32(BITMAP_STALE);
608 /* assign fields using values from superblock */
609 bitmap->mddev->bitmap_info.chunksize = chunksize;
610 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
611 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
612 bitmap->flags |= le32_to_cpu(sb->state);
613 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
614 bitmap->flags |= BITMAP_HOSTENDIAN;
615 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
616 if (sb->state & cpu_to_le32(BITMAP_STALE))
617 bitmap->events_cleared = bitmap->mddev->events;
620 kunmap_atomic(sb, KM_USER0);
622 bitmap_print_sb(bitmap);
626 enum bitmap_mask_op {
631 /* record the state of the bitmap in the superblock. Return the old value */
632 static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
633 enum bitmap_mask_op op)
639 spin_lock_irqsave(&bitmap->lock, flags);
640 if (!bitmap->sb_page) { /* can't set the state */
641 spin_unlock_irqrestore(&bitmap->lock, flags);
644 spin_unlock_irqrestore(&bitmap->lock, flags);
645 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
646 old = le32_to_cpu(sb->state) & bits;
649 sb->state |= cpu_to_le32(bits);
652 sb->state &= cpu_to_le32(~bits);
657 kunmap_atomic(sb, KM_USER0);
662 * general bitmap file operations
668 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
669 * file a page at a time. There's a superblock at the start of the file.
671 /* calculate the index of the page that contains this bit */
672 static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
674 if (!bitmap->mddev->bitmap_info.external)
675 chunk += sizeof(bitmap_super_t) << 3;
676 return chunk >> PAGE_BIT_SHIFT;
679 /* calculate the (bit) offset of this bit within a page */
680 static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
682 if (!bitmap->mddev->bitmap_info.external)
683 chunk += sizeof(bitmap_super_t) << 3;
684 return chunk & (PAGE_BITS - 1);
688 * return a pointer to the page in the filemap that contains the given bit
690 * this lookup is complicated by the fact that the bitmap sb might be exactly
691 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
694 static inline struct page *filemap_get_page(struct bitmap *bitmap,
697 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
699 return bitmap->filemap[file_page_index(bitmap, chunk)
700 - file_page_index(bitmap, 0)];
703 static void bitmap_file_unmap(struct bitmap *bitmap)
705 struct page **map, *sb_page;
710 spin_lock_irqsave(&bitmap->lock, flags);
711 map = bitmap->filemap;
712 bitmap->filemap = NULL;
713 attr = bitmap->filemap_attr;
714 bitmap->filemap_attr = NULL;
715 pages = bitmap->file_pages;
716 bitmap->file_pages = 0;
717 sb_page = bitmap->sb_page;
718 bitmap->sb_page = NULL;
719 spin_unlock_irqrestore(&bitmap->lock, flags);
722 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
723 free_buffers(map[pages]);
728 free_buffers(sb_page);
731 static void bitmap_file_put(struct bitmap *bitmap)
736 spin_lock_irqsave(&bitmap->lock, flags);
739 spin_unlock_irqrestore(&bitmap->lock, flags);
742 wait_event(bitmap->write_wait,
743 atomic_read(&bitmap->pending_writes)==0);
744 bitmap_file_unmap(bitmap);
747 struct inode *inode = file->f_path.dentry->d_inode;
748 invalidate_mapping_pages(inode->i_mapping, 0, -1);
754 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
755 * then it is no longer reliable, so we stop using it and we mark the file
756 * as failed in the superblock
758 static void bitmap_file_kick(struct bitmap *bitmap)
760 char *path, *ptr = NULL;
762 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
763 bitmap_update_sb(bitmap);
766 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
768 ptr = d_path(&bitmap->file->f_path, path,
772 "%s: kicking failed bitmap file %s from array!\n",
773 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
778 "%s: disabling internal bitmap due to errors\n",
782 bitmap_file_put(bitmap);
787 enum bitmap_page_attr {
788 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
789 BITMAP_PAGE_CLEAN = 1, /* there are bits that might need to be cleared */
790 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
793 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
794 enum bitmap_page_attr attr)
796 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
799 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
800 enum bitmap_page_attr attr)
802 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
805 static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
806 enum bitmap_page_attr attr)
808 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
812 * bitmap_file_set_bit -- called before performing a write to the md device
813 * to set (and eventually sync) a particular bit in the bitmap file
815 * we set the bit immediately, then we record the page number so that
816 * when an unplug occurs, we can flush the dirty pages out to disk
818 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
823 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
825 if (!bitmap->filemap)
828 page = filemap_get_page(bitmap, chunk);
831 bit = file_page_offset(bitmap, chunk);
834 kaddr = kmap_atomic(page, KM_USER0);
835 if (bitmap->flags & BITMAP_HOSTENDIAN)
838 ext2_set_bit(bit, kaddr);
839 kunmap_atomic(kaddr, KM_USER0);
840 PRINTK("set file bit %lu page %lu\n", bit, page->index);
842 /* record page number so it gets flushed to disk when unplug occurs */
843 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
846 /* this gets called when the md device is ready to unplug its underlying
847 * (slave) device queues -- before we let any writes go down, we need to
848 * sync the dirty pages of the bitmap file to disk */
849 void bitmap_unplug(struct bitmap *bitmap)
851 unsigned long i, flags;
852 int dirty, need_write;
859 /* look at each page to see if there are any set bits that need to be
860 * flushed out to disk */
861 for (i = 0; i < bitmap->file_pages; i++) {
862 spin_lock_irqsave(&bitmap->lock, flags);
863 if (!bitmap->filemap) {
864 spin_unlock_irqrestore(&bitmap->lock, flags);
867 page = bitmap->filemap[i];
868 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
869 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
870 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
871 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
874 spin_unlock_irqrestore(&bitmap->lock, flags);
876 if (dirty || need_write)
877 write_page(bitmap, page, 0);
879 if (wait) { /* if any writes were performed, we need to wait on them */
881 wait_event(bitmap->write_wait,
882 atomic_read(&bitmap->pending_writes)==0);
884 md_super_wait(bitmap->mddev);
886 if (bitmap->flags & BITMAP_WRITE_ERROR)
887 bitmap_file_kick(bitmap);
889 EXPORT_SYMBOL(bitmap_unplug);
891 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
892 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
893 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
894 * memory mapping of the bitmap file
896 * if there's no bitmap file, or if the bitmap file had been
897 * previously kicked from the array, we mark all the bits as
898 * 1's in order to cause a full resync.
900 * We ignore all bits for sectors that end earlier than 'start'.
901 * This is used when reading an out-of-date bitmap...
903 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
905 unsigned long i, chunks, index, oldindex, bit;
906 struct page *page = NULL, *oldpage = NULL;
907 unsigned long num_pages, bit_cnt = 0;
909 unsigned long bytes, offset;
914 chunks = bitmap->chunks;
917 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
919 #ifdef INJECT_FAULTS_3
922 outofdate = bitmap->flags & BITMAP_STALE;
925 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
926 "recovery\n", bmname(bitmap));
928 bytes = (chunks + 7) / 8;
929 if (!bitmap->mddev->bitmap_info.external)
930 bytes += sizeof(bitmap_super_t);
932 num_pages = (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
934 if (file && i_size_read(file->f_mapping->host) < bytes) {
935 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
937 (unsigned long) i_size_read(file->f_mapping->host),
944 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
945 if (!bitmap->filemap)
948 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
949 bitmap->filemap_attr = kzalloc(
950 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
952 if (!bitmap->filemap_attr)
957 for (i = 0; i < chunks; i++) {
959 index = file_page_index(bitmap, i);
960 bit = file_page_offset(bitmap, i);
961 if (index != oldindex) { /* this is a new page, read it in */
963 /* unmap the old page, we're done with it */
964 if (index == num_pages-1)
965 count = bytes - index * PAGE_SIZE;
968 if (index == 0 && bitmap->sb_page) {
970 * if we're here then the superblock page
971 * contains some bits (PAGE_SIZE != sizeof sb)
972 * we've already read it in, so just use it
974 page = bitmap->sb_page;
975 offset = sizeof(bitmap_super_t);
977 read_sb_page(bitmap->mddev,
978 bitmap->mddev->bitmap_info.offset,
982 page = read_page(file, index, bitmap, count);
985 page = read_sb_page(bitmap->mddev,
986 bitmap->mddev->bitmap_info.offset,
991 if (IS_ERR(page)) { /* read error */
999 bitmap->filemap[bitmap->file_pages++] = page;
1000 bitmap->last_page_size = count;
1004 * if bitmap is out of date, dirty the
1005 * whole page and write it out
1007 paddr = kmap_atomic(page, KM_USER0);
1008 memset(paddr + offset, 0xff,
1009 PAGE_SIZE - offset);
1010 kunmap_atomic(paddr, KM_USER0);
1011 write_page(bitmap, page, 1);
1014 if (bitmap->flags & BITMAP_WRITE_ERROR)
1018 paddr = kmap_atomic(page, KM_USER0);
1019 if (bitmap->flags & BITMAP_HOSTENDIAN)
1020 b = test_bit(bit, paddr);
1022 b = ext2_test_bit(bit, paddr);
1023 kunmap_atomic(paddr, KM_USER0);
1025 /* if the disk bit is set, set the memory bit */
1026 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1028 bitmap_set_memory_bits(bitmap,
1029 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1032 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1036 /* everything went OK */
1038 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1040 if (bit_cnt) { /* Kick recovery if any bits were set */
1041 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1042 md_wakeup_thread(bitmap->mddev->thread);
1045 printk(KERN_INFO "%s: bitmap initialized from disk: "
1046 "read %lu/%lu pages, set %lu bits\n",
1047 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
1052 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1053 bmname(bitmap), ret);
1057 void bitmap_write_all(struct bitmap *bitmap)
1059 /* We don't actually write all bitmap blocks here,
1060 * just flag them as needing to be written
1064 for (i = 0; i < bitmap->file_pages; i++)
1065 set_page_attr(bitmap, bitmap->filemap[i],
1066 BITMAP_PAGE_NEEDWRITE);
1069 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1071 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1072 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1073 bitmap->bp[page].count += inc;
1074 bitmap_checkfree(bitmap, page);
1076 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1077 sector_t offset, int *blocks,
1081 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1085 void bitmap_daemon_work(mddev_t *mddev)
1087 struct bitmap *bitmap;
1089 unsigned long flags;
1090 struct page *page = NULL, *lastpage = NULL;
1094 /* Use a mutex to guard daemon_work against
1097 mutex_lock(&mddev->bitmap_info.mutex);
1098 bitmap = mddev->bitmap;
1099 if (bitmap == NULL) {
1100 mutex_unlock(&mddev->bitmap_info.mutex);
1103 if (time_before(jiffies, bitmap->daemon_lastrun
1104 + bitmap->mddev->bitmap_info.daemon_sleep))
1107 bitmap->daemon_lastrun = jiffies;
1108 if (bitmap->allclean) {
1109 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1112 bitmap->allclean = 1;
1114 spin_lock_irqsave(&bitmap->lock, flags);
1115 for (j = 0; j < bitmap->chunks; j++) {
1116 bitmap_counter_t *bmc;
1117 if (!bitmap->filemap)
1118 /* error or shutdown */
1121 page = filemap_get_page(bitmap, j);
1123 if (page != lastpage) {
1124 /* skip this page unless it's marked as needing cleaning */
1125 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1126 int need_write = test_page_attr(bitmap, page,
1127 BITMAP_PAGE_NEEDWRITE);
1129 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1131 spin_unlock_irqrestore(&bitmap->lock, flags);
1133 write_page(bitmap, page, 0);
1134 bitmap->allclean = 0;
1136 spin_lock_irqsave(&bitmap->lock, flags);
1137 j |= (PAGE_BITS - 1);
1141 /* grab the new page, sync and release the old */
1142 if (lastpage != NULL) {
1143 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1144 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1145 spin_unlock_irqrestore(&bitmap->lock, flags);
1146 write_page(bitmap, lastpage, 0);
1148 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1149 spin_unlock_irqrestore(&bitmap->lock, flags);
1152 spin_unlock_irqrestore(&bitmap->lock, flags);
1155 /* We are possibly going to clear some bits, so make
1156 * sure that events_cleared is up-to-date.
1158 if (bitmap->need_sync &&
1159 bitmap->mddev->bitmap_info.external == 0) {
1161 bitmap->need_sync = 0;
1162 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1163 sb->events_cleared =
1164 cpu_to_le64(bitmap->events_cleared);
1165 kunmap_atomic(sb, KM_USER0);
1166 write_page(bitmap, bitmap->sb_page, 1);
1168 spin_lock_irqsave(&bitmap->lock, flags);
1169 if (!bitmap->need_sync)
1170 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1172 bmc = bitmap_get_counter(bitmap,
1173 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1177 bitmap->allclean = 0;
1180 *bmc = 1; /* maybe clear the bit next time */
1181 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1182 } else if (*bmc == 1 && !bitmap->need_sync) {
1183 /* we can clear the bit */
1185 bitmap_count_page(bitmap,
1186 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1190 paddr = kmap_atomic(page, KM_USER0);
1191 if (bitmap->flags & BITMAP_HOSTENDIAN)
1192 clear_bit(file_page_offset(bitmap, j),
1195 ext2_clear_bit(file_page_offset(bitmap, j),
1197 kunmap_atomic(paddr, KM_USER0);
1200 j |= PAGE_COUNTER_MASK;
1202 spin_unlock_irqrestore(&bitmap->lock, flags);
1204 /* now sync the final page */
1205 if (lastpage != NULL) {
1206 spin_lock_irqsave(&bitmap->lock, flags);
1207 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1208 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1209 spin_unlock_irqrestore(&bitmap->lock, flags);
1210 write_page(bitmap, lastpage, 0);
1212 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1213 spin_unlock_irqrestore(&bitmap->lock, flags);
1218 if (bitmap->allclean == 0)
1219 bitmap->mddev->thread->timeout =
1220 bitmap->mddev->bitmap_info.daemon_sleep;
1221 mutex_unlock(&mddev->bitmap_info.mutex);
1224 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1225 sector_t offset, int *blocks,
1227 __releases(bitmap->lock)
1228 __acquires(bitmap->lock)
1230 /* If 'create', we might release the lock and reclaim it.
1231 * The lock must have been taken with interrupts enabled.
1232 * If !create, we don't release the lock.
1234 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1235 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1236 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1240 err = bitmap_checkpage(bitmap, page, create);
1242 if (bitmap->bp[page].hijacked ||
1243 bitmap->bp[page].map == NULL)
1244 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1245 PAGE_COUNTER_SHIFT - 1);
1247 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1248 *blocks = csize - (offset & (csize - 1));
1253 /* now locked ... */
1255 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1256 /* should we use the first or second counter field
1257 * of the hijacked pointer? */
1258 int hi = (pageoff > PAGE_COUNTER_MASK);
1259 return &((bitmap_counter_t *)
1260 &bitmap->bp[page].map)[hi];
1261 } else /* page is allocated */
1262 return (bitmap_counter_t *)
1263 &(bitmap->bp[page].map[pageoff]);
1266 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1273 atomic_inc(&bitmap->behind_writes);
1274 bw = atomic_read(&bitmap->behind_writes);
1275 if (bw > bitmap->behind_writes_used)
1276 bitmap->behind_writes_used = bw;
1278 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1279 bw, bitmap->max_write_behind);
1284 bitmap_counter_t *bmc;
1286 spin_lock_irq(&bitmap->lock);
1287 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1289 spin_unlock_irq(&bitmap->lock);
1293 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1294 DEFINE_WAIT(__wait);
1295 /* note that it is safe to do the prepare_to_wait
1296 * after the test as long as we do it before dropping
1299 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1300 TASK_UNINTERRUPTIBLE);
1301 spin_unlock_irq(&bitmap->lock);
1302 md_unplug(bitmap->mddev);
1304 finish_wait(&bitmap->overflow_wait, &__wait);
1310 bitmap_file_set_bit(bitmap, offset);
1311 bitmap_count_page(bitmap, offset, 1);
1319 spin_unlock_irq(&bitmap->lock);
1322 if (sectors > blocks)
1327 bitmap->allclean = 0;
1330 EXPORT_SYMBOL(bitmap_startwrite);
1332 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1333 int success, int behind)
1338 if (atomic_dec_and_test(&bitmap->behind_writes))
1339 wake_up(&bitmap->behind_wait);
1340 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1341 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1343 if (bitmap->mddev->degraded)
1344 /* Never clear bits or update events_cleared when degraded */
1349 unsigned long flags;
1350 bitmap_counter_t *bmc;
1352 spin_lock_irqsave(&bitmap->lock, flags);
1353 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1355 spin_unlock_irqrestore(&bitmap->lock, flags);
1360 bitmap->events_cleared < bitmap->mddev->events) {
1361 bitmap->events_cleared = bitmap->mddev->events;
1362 bitmap->need_sync = 1;
1363 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
1366 if (!success && ! (*bmc & NEEDED_MASK))
1367 *bmc |= NEEDED_MASK;
1369 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1370 wake_up(&bitmap->overflow_wait);
1374 set_page_attr(bitmap,
1375 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1378 spin_unlock_irqrestore(&bitmap->lock, flags);
1380 if (sectors > blocks)
1386 EXPORT_SYMBOL(bitmap_endwrite);
1388 static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1391 bitmap_counter_t *bmc;
1393 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1395 return 1; /* always resync if no bitmap */
1397 spin_lock_irq(&bitmap->lock);
1398 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1404 else if (NEEDED(*bmc)) {
1406 if (!degraded) { /* don't set/clear bits if degraded */
1407 *bmc |= RESYNC_MASK;
1408 *bmc &= ~NEEDED_MASK;
1412 spin_unlock_irq(&bitmap->lock);
1413 bitmap->allclean = 0;
1417 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1420 /* bitmap_start_sync must always report on multiples of whole
1421 * pages, otherwise resync (which is very PAGE_SIZE based) will
1423 * So call __bitmap_start_sync repeatedly (if needed) until
1424 * At least PAGE_SIZE>>9 blocks are covered.
1425 * Return the 'or' of the result.
1431 while (*blocks < (PAGE_SIZE>>9)) {
1432 rv |= __bitmap_start_sync(bitmap, offset,
1433 &blocks1, degraded);
1439 EXPORT_SYMBOL(bitmap_start_sync);
1441 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1443 bitmap_counter_t *bmc;
1444 unsigned long flags;
1446 if (bitmap == NULL) {
1450 spin_lock_irqsave(&bitmap->lock, flags);
1451 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1456 *bmc &= ~RESYNC_MASK;
1458 if (!NEEDED(*bmc) && aborted)
1459 *bmc |= NEEDED_MASK;
1462 set_page_attr(bitmap,
1463 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1468 spin_unlock_irqrestore(&bitmap->lock, flags);
1469 bitmap->allclean = 0;
1471 EXPORT_SYMBOL(bitmap_end_sync);
1473 void bitmap_close_sync(struct bitmap *bitmap)
1475 /* Sync has finished, and any bitmap chunks that weren't synced
1476 * properly have been aborted. It remains to us to clear the
1477 * RESYNC bit wherever it is still on
1479 sector_t sector = 0;
1483 while (sector < bitmap->mddev->resync_max_sectors) {
1484 bitmap_end_sync(bitmap, sector, &blocks, 0);
1488 EXPORT_SYMBOL(bitmap_close_sync);
1490 void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1498 bitmap->last_end_sync = jiffies;
1501 if (time_before(jiffies, (bitmap->last_end_sync
1502 + bitmap->mddev->bitmap_info.daemon_sleep)))
1504 wait_event(bitmap->mddev->recovery_wait,
1505 atomic_read(&bitmap->mddev->recovery_active) == 0);
1507 bitmap->mddev->curr_resync_completed = bitmap->mddev->curr_resync;
1508 if (bitmap->mddev->persistent)
1509 set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
1510 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1512 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1513 bitmap_end_sync(bitmap, s, &blocks, 0);
1516 bitmap->last_end_sync = jiffies;
1517 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
1519 EXPORT_SYMBOL(bitmap_cond_end_sync);
1521 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1523 /* For each chunk covered by any of these sectors, set the
1524 * counter to 1 and set resync_needed. They should all
1525 * be 0 at this point
1529 bitmap_counter_t *bmc;
1530 spin_lock_irq(&bitmap->lock);
1531 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1533 spin_unlock_irq(&bitmap->lock);
1538 *bmc = 1 | (needed ? NEEDED_MASK : 0);
1539 bitmap_count_page(bitmap, offset, 1);
1540 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1541 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1543 spin_unlock_irq(&bitmap->lock);
1544 bitmap->allclean = 0;
1547 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1548 void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1550 unsigned long chunk;
1552 for (chunk = s; chunk <= e; chunk++) {
1553 sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
1554 bitmap_set_memory_bits(bitmap, sec, 1);
1555 bitmap_file_set_bit(bitmap, sec);
1556 if (sec < bitmap->mddev->recovery_cp)
1557 /* We are asserting that the array is dirty,
1558 * so move the recovery_cp address back so
1559 * that it is obvious that it is dirty
1561 bitmap->mddev->recovery_cp = sec;
1566 * flush out any pending updates
1568 void bitmap_flush(mddev_t *mddev)
1570 struct bitmap *bitmap = mddev->bitmap;
1573 if (!bitmap) /* there was no bitmap */
1576 /* run the daemon_work three time to ensure everything is flushed
1579 sleep = mddev->bitmap_info.daemon_sleep * 2;
1580 bitmap->daemon_lastrun -= sleep;
1581 bitmap_daemon_work(mddev);
1582 bitmap->daemon_lastrun -= sleep;
1583 bitmap_daemon_work(mddev);
1584 bitmap->daemon_lastrun -= sleep;
1585 bitmap_daemon_work(mddev);
1586 bitmap_update_sb(bitmap);
1590 * free memory that was allocated
1592 static void bitmap_free(struct bitmap *bitmap)
1594 unsigned long k, pages;
1595 struct bitmap_page *bp;
1597 if (!bitmap) /* there was no bitmap */
1600 /* release the bitmap file and kill the daemon */
1601 bitmap_file_put(bitmap);
1604 pages = bitmap->pages;
1606 /* free all allocated memory */
1608 if (bp) /* deallocate the page memory */
1609 for (k = 0; k < pages; k++)
1610 if (bp[k].map && !bp[k].hijacked)
1616 void bitmap_destroy(mddev_t *mddev)
1618 struct bitmap *bitmap = mddev->bitmap;
1620 if (!bitmap) /* there was no bitmap */
1623 mutex_lock(&mddev->bitmap_info.mutex);
1624 mddev->bitmap = NULL; /* disconnect from the md device */
1625 mutex_unlock(&mddev->bitmap_info.mutex);
1627 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1629 if (bitmap->sysfs_can_clear)
1630 sysfs_put(bitmap->sysfs_can_clear);
1632 bitmap_free(bitmap);
1636 * initialize the bitmap structure
1637 * if this returns an error, bitmap_destroy must be called to do clean up
1639 int bitmap_create(mddev_t *mddev)
1641 struct bitmap *bitmap;
1642 sector_t blocks = mddev->resync_max_sectors;
1643 unsigned long chunks;
1644 unsigned long pages;
1645 struct file *file = mddev->bitmap_info.file;
1648 struct sysfs_dirent *bm = NULL;
1650 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
1652 if (!file && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
1655 BUG_ON(file && mddev->bitmap_info.offset);
1657 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1661 spin_lock_init(&bitmap->lock);
1662 atomic_set(&bitmap->pending_writes, 0);
1663 init_waitqueue_head(&bitmap->write_wait);
1664 init_waitqueue_head(&bitmap->overflow_wait);
1665 init_waitqueue_head(&bitmap->behind_wait);
1667 bitmap->mddev = mddev;
1670 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
1672 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
1675 bitmap->sysfs_can_clear = NULL;
1677 bitmap->file = file;
1680 /* As future accesses to this file will use bmap,
1681 * and bypass the page cache, we must sync the file
1686 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
1687 if (!mddev->bitmap_info.external)
1688 err = bitmap_read_sb(bitmap);
1691 if (mddev->bitmap_info.chunksize == 0 ||
1692 mddev->bitmap_info.daemon_sleep == 0)
1693 /* chunksize and time_base need to be
1700 bitmap->daemon_lastrun = jiffies;
1701 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
1703 /* now that chunksize and chunkshift are set, we can use these macros */
1704 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1705 CHUNK_BLOCK_SHIFT(bitmap);
1706 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1710 bitmap->chunks = chunks;
1711 bitmap->pages = pages;
1712 bitmap->missing_pages = pages;
1713 bitmap->counter_bits = COUNTER_BITS;
1715 bitmap->syncchunk = ~0UL;
1717 #ifdef INJECT_FATAL_FAULT_1
1720 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1726 /* now that we have some pages available, initialize the in-memory
1727 * bitmap from the on-disk bitmap */
1729 if (mddev->degraded == 0
1730 || bitmap->events_cleared == mddev->events)
1731 /* no need to keep dirty bits to optimise a re-add of a missing device */
1732 start = mddev->recovery_cp;
1733 err = bitmap_init_from_disk(bitmap, start);
1738 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1739 pages, bmname(bitmap));
1741 mddev->bitmap = bitmap;
1743 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
1744 md_wakeup_thread(mddev->thread);
1746 bitmap_update_sb(bitmap);
1748 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1751 bitmap_free(bitmap);
1756 location_show(mddev_t *mddev, char *page)
1759 if (mddev->bitmap_info.file)
1760 len = sprintf(page, "file");
1761 else if (mddev->bitmap_info.offset)
1762 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
1764 len = sprintf(page, "none");
1765 len += sprintf(page+len, "\n");
1770 location_store(mddev_t *mddev, const char *buf, size_t len)
1774 if (!mddev->pers->quiesce)
1776 if (mddev->recovery || mddev->sync_thread)
1780 if (mddev->bitmap || mddev->bitmap_info.file ||
1781 mddev->bitmap_info.offset) {
1782 /* bitmap already configured. Only option is to clear it */
1783 if (strncmp(buf, "none", 4) != 0)
1786 mddev->pers->quiesce(mddev, 1);
1787 bitmap_destroy(mddev);
1788 mddev->pers->quiesce(mddev, 0);
1790 mddev->bitmap_info.offset = 0;
1791 if (mddev->bitmap_info.file) {
1792 struct file *f = mddev->bitmap_info.file;
1793 mddev->bitmap_info.file = NULL;
1794 restore_bitmap_write_access(f);
1798 /* No bitmap, OK to set a location */
1800 if (strncmp(buf, "none", 4) == 0)
1801 /* nothing to be done */;
1802 else if (strncmp(buf, "file:", 5) == 0) {
1803 /* Not supported yet */
1808 rv = strict_strtoll(buf+1, 10, &offset);
1810 rv = strict_strtoll(buf, 10, &offset);
1815 if (mddev->bitmap_info.external == 0 &&
1816 mddev->major_version == 0 &&
1817 offset != mddev->bitmap_info.default_offset)
1819 mddev->bitmap_info.offset = offset;
1821 mddev->pers->quiesce(mddev, 1);
1822 rv = bitmap_create(mddev);
1824 bitmap_destroy(mddev);
1825 mddev->bitmap_info.offset = 0;
1827 mddev->pers->quiesce(mddev, 0);
1833 if (!mddev->external) {
1834 /* Ensure new bitmap info is stored in
1835 * metadata promptly.
1837 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1838 md_wakeup_thread(mddev->thread);
1843 static struct md_sysfs_entry bitmap_location =
1844 __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1847 timeout_show(mddev_t *mddev, char *page)
1850 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1851 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
1853 len = sprintf(page, "%lu", secs);
1855 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1856 len += sprintf(page+len, "\n");
1861 timeout_store(mddev_t *mddev, const char *buf, size_t len)
1863 /* timeout can be set at any time */
1864 unsigned long timeout;
1865 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1869 /* just to make sure we don't overflow... */
1870 if (timeout >= LONG_MAX / HZ)
1873 timeout = timeout * HZ / 10000;
1875 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1876 timeout = MAX_SCHEDULE_TIMEOUT-1;
1879 mddev->bitmap_info.daemon_sleep = timeout;
1880 if (mddev->thread) {
1881 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1882 * the bitmap is all clean and we don't need to
1883 * adjust the timeout right now
1885 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
1886 mddev->thread->timeout = timeout;
1887 md_wakeup_thread(mddev->thread);
1893 static struct md_sysfs_entry bitmap_timeout =
1894 __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
1897 backlog_show(mddev_t *mddev, char *page)
1899 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
1903 backlog_store(mddev_t *mddev, const char *buf, size_t len)
1905 unsigned long backlog;
1906 int rv = strict_strtoul(buf, 10, &backlog);
1909 if (backlog > COUNTER_MAX)
1911 mddev->bitmap_info.max_write_behind = backlog;
1915 static struct md_sysfs_entry bitmap_backlog =
1916 __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
1919 chunksize_show(mddev_t *mddev, char *page)
1921 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
1925 chunksize_store(mddev_t *mddev, const char *buf, size_t len)
1927 /* Can only be changed when no bitmap is active */
1929 unsigned long csize;
1932 rv = strict_strtoul(buf, 10, &csize);
1936 !is_power_of_2(csize))
1938 mddev->bitmap_info.chunksize = csize;
1942 static struct md_sysfs_entry bitmap_chunksize =
1943 __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
1945 static ssize_t metadata_show(mddev_t *mddev, char *page)
1947 return sprintf(page, "%s\n", (mddev->bitmap_info.external
1948 ? "external" : "internal"));
1951 static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
1953 if (mddev->bitmap ||
1954 mddev->bitmap_info.file ||
1955 mddev->bitmap_info.offset)
1957 if (strncmp(buf, "external", 8) == 0)
1958 mddev->bitmap_info.external = 1;
1959 else if (strncmp(buf, "internal", 8) == 0)
1960 mddev->bitmap_info.external = 0;
1966 static struct md_sysfs_entry bitmap_metadata =
1967 __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
1969 static ssize_t can_clear_show(mddev_t *mddev, char *page)
1973 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
1976 len = sprintf(page, "\n");
1980 static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
1982 if (mddev->bitmap == NULL)
1984 if (strncmp(buf, "false", 5) == 0)
1985 mddev->bitmap->need_sync = 1;
1986 else if (strncmp(buf, "true", 4) == 0) {
1987 if (mddev->degraded)
1989 mddev->bitmap->need_sync = 0;
1995 static struct md_sysfs_entry bitmap_can_clear =
1996 __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
1999 behind_writes_used_show(mddev_t *mddev, char *page)
2001 if (mddev->bitmap == NULL)
2002 return sprintf(page, "0\n");
2003 return sprintf(page, "%lu\n",
2004 mddev->bitmap->behind_writes_used);
2008 behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
2011 mddev->bitmap->behind_writes_used = 0;
2015 static struct md_sysfs_entry max_backlog_used =
2016 __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2017 behind_writes_used_show, behind_writes_used_reset);
2019 static struct attribute *md_bitmap_attrs[] = {
2020 &bitmap_location.attr,
2021 &bitmap_timeout.attr,
2022 &bitmap_backlog.attr,
2023 &bitmap_chunksize.attr,
2024 &bitmap_metadata.attr,
2025 &bitmap_can_clear.attr,
2026 &max_backlog_used.attr,
2029 struct attribute_group md_bitmap_group = {
2031 .attrs = md_bitmap_attrs,