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>
29 #include <linux/seq_file.h>
33 static inline char *bmname(struct bitmap *bitmap)
35 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
39 * just a placeholder - calls kmalloc for bitmap pages
41 static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
45 page = kzalloc(PAGE_SIZE, GFP_NOIO);
47 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
49 pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
50 bmname(bitmap), page);
55 * for now just a placeholder -- just calls kfree for bitmap pages
57 static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
59 pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
64 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
66 * 1) check to see if this page is allocated, if it's not then try to alloc
67 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
68 * page pointer directly as a counter
70 * if we find our page, we increment the page's refcount so that it stays
71 * allocated while we're using it
73 static int bitmap_checkpage(struct bitmap *bitmap,
74 unsigned long page, int create)
75 __releases(bitmap->lock)
76 __acquires(bitmap->lock)
78 unsigned char *mappage;
80 if (page >= bitmap->pages) {
81 /* This can happen if bitmap_start_sync goes beyond
82 * End-of-device while looking for a whole page.
88 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
91 if (bitmap->bp[page].map) /* page is already allocated, just return */
97 /* this page has not been allocated yet */
99 spin_unlock_irq(&bitmap->lock);
100 mappage = bitmap_alloc_page(bitmap);
101 spin_lock_irq(&bitmap->lock);
103 if (mappage == NULL) {
104 pr_debug("%s: bitmap map page allocation failed, hijacking\n",
106 /* failed - set the hijacked flag so that we can use the
107 * pointer as a counter */
108 if (!bitmap->bp[page].map)
109 bitmap->bp[page].hijacked = 1;
110 } else if (bitmap->bp[page].map ||
111 bitmap->bp[page].hijacked) {
112 /* somebody beat us to getting the page */
113 bitmap_free_page(bitmap, mappage);
117 /* no page was in place and we have one, so install it */
119 bitmap->bp[page].map = mappage;
120 bitmap->missing_pages--;
125 /* if page is completely empty, put it back on the free list, or dealloc it */
126 /* if page was hijacked, unmark the flag so it might get alloced next time */
127 /* Note: lock should be held when calling this */
128 static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
132 if (bitmap->bp[page].count) /* page is still busy */
135 /* page is no longer in use, it can be released */
137 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
138 bitmap->bp[page].hijacked = 0;
139 bitmap->bp[page].map = NULL;
141 /* normal case, free the page */
142 ptr = bitmap->bp[page].map;
143 bitmap->bp[page].map = NULL;
144 bitmap->missing_pages++;
145 bitmap_free_page(bitmap, ptr);
150 * bitmap file handling - read and write the bitmap file and its superblock
154 * basic page I/O operations
157 /* IO operations when bitmap is stored near all superblocks */
158 static struct page *read_sb_page(struct mddev *mddev, loff_t offset,
160 unsigned long index, int size)
162 /* choose a good rdev and read the page from there */
164 struct md_rdev *rdev;
169 page = alloc_page(GFP_KERNEL);
171 return ERR_PTR(-ENOMEM);
175 rdev_for_each(rdev, mddev) {
176 if (! test_bit(In_sync, &rdev->flags)
177 || test_bit(Faulty, &rdev->flags))
180 target = offset + index * (PAGE_SIZE/512);
182 if (sync_page_io(rdev, target,
183 roundup(size, bdev_logical_block_size(rdev->bdev)),
186 attach_page_buffers(page, NULL); /* so that free_buffer will
193 return ERR_PTR(-EIO);
197 static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
199 /* Iterate the disks of an mddev, using rcu to protect access to the
200 * linked list, and raising the refcount of devices we return to ensure
201 * they don't disappear while in use.
202 * As devices are only added or removed when raid_disk is < 0 and
203 * nr_pending is 0 and In_sync is clear, the entries we return will
204 * still be in the same position on the list when we re-enter
205 * list_for_each_continue_rcu.
207 struct list_head *pos;
210 /* start at the beginning */
213 /* release the previous rdev and start from there. */
214 rdev_dec_pending(rdev, mddev);
215 pos = &rdev->same_set;
217 list_for_each_continue_rcu(pos, &mddev->disks) {
218 rdev = list_entry(pos, struct md_rdev, same_set);
219 if (rdev->raid_disk >= 0 &&
220 !test_bit(Faulty, &rdev->flags)) {
221 /* this is a usable devices */
222 atomic_inc(&rdev->nr_pending);
231 static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
233 struct md_rdev *rdev = NULL;
234 struct block_device *bdev;
235 struct mddev *mddev = bitmap->mddev;
237 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
238 int size = PAGE_SIZE;
239 loff_t offset = mddev->bitmap_info.offset;
241 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
243 if (page->index == bitmap->file_pages-1)
244 size = roundup(bitmap->last_page_size,
245 bdev_logical_block_size(bdev));
246 /* Just make sure we aren't corrupting data or
249 if (mddev->external) {
250 /* Bitmap could be anywhere. */
251 if (rdev->sb_start + offset + (page->index
255 rdev->sb_start + offset
256 < (rdev->data_offset + mddev->dev_sectors
259 } else if (offset < 0) {
260 /* DATA BITMAP METADATA */
262 + (long)(page->index * (PAGE_SIZE/512))
264 /* bitmap runs in to metadata */
266 if (rdev->data_offset + mddev->dev_sectors
267 > rdev->sb_start + offset)
268 /* data runs in to bitmap */
270 } else if (rdev->sb_start < rdev->data_offset) {
271 /* METADATA BITMAP DATA */
274 + page->index*(PAGE_SIZE/512) + size/512
276 /* bitmap runs in to data */
279 /* DATA METADATA BITMAP - no problems */
281 md_super_write(mddev, rdev,
282 rdev->sb_start + offset
283 + page->index * (PAGE_SIZE/512),
289 md_super_wait(mddev);
296 static void bitmap_file_kick(struct bitmap *bitmap);
298 * write out a page to a file
300 static void write_page(struct bitmap *bitmap, struct page *page, int wait)
302 struct buffer_head *bh;
304 if (bitmap->file == NULL) {
305 switch (write_sb_page(bitmap, page, wait)) {
307 bitmap->flags |= BITMAP_WRITE_ERROR;
311 bh = page_buffers(page);
313 while (bh && bh->b_blocknr) {
314 atomic_inc(&bitmap->pending_writes);
315 set_buffer_locked(bh);
316 set_buffer_mapped(bh);
317 submit_bh(WRITE | REQ_SYNC, bh);
318 bh = bh->b_this_page;
322 wait_event(bitmap->write_wait,
323 atomic_read(&bitmap->pending_writes)==0);
325 if (bitmap->flags & BITMAP_WRITE_ERROR)
326 bitmap_file_kick(bitmap);
329 static void end_bitmap_write(struct buffer_head *bh, int uptodate)
331 struct bitmap *bitmap = bh->b_private;
335 spin_lock_irqsave(&bitmap->lock, flags);
336 bitmap->flags |= BITMAP_WRITE_ERROR;
337 spin_unlock_irqrestore(&bitmap->lock, flags);
339 if (atomic_dec_and_test(&bitmap->pending_writes))
340 wake_up(&bitmap->write_wait);
343 /* copied from buffer.c */
345 __clear_page_buffers(struct page *page)
347 ClearPagePrivate(page);
348 set_page_private(page, 0);
349 page_cache_release(page);
351 static void free_buffers(struct page *page)
353 struct buffer_head *bh = page_buffers(page);
356 struct buffer_head *next = bh->b_this_page;
357 free_buffer_head(bh);
360 __clear_page_buffers(page);
364 /* read a page from a file.
365 * We both read the page, and attach buffers to the page to record the
366 * address of each block (using bmap). These addresses will be used
367 * to write the block later, completely bypassing the filesystem.
368 * This usage is similar to how swap files are handled, and allows us
369 * to write to a file with no concerns of memory allocation failing.
371 static struct page *read_page(struct file *file, unsigned long index,
372 struct bitmap *bitmap,
375 struct page *page = NULL;
376 struct inode *inode = file->f_path.dentry->d_inode;
377 struct buffer_head *bh;
380 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
381 (unsigned long long)index << PAGE_SHIFT);
383 page = alloc_page(GFP_KERNEL);
385 page = ERR_PTR(-ENOMEM);
389 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
392 page = ERR_PTR(-ENOMEM);
395 attach_page_buffers(page, bh);
396 block = index << (PAGE_SHIFT - inode->i_blkbits);
401 bh->b_blocknr = bmap(inode, block);
402 if (bh->b_blocknr == 0) {
403 /* Cannot use this file! */
405 page = ERR_PTR(-EINVAL);
408 bh->b_bdev = inode->i_sb->s_bdev;
409 if (count < (1<<inode->i_blkbits))
412 count -= (1<<inode->i_blkbits);
414 bh->b_end_io = end_bitmap_write;
415 bh->b_private = bitmap;
416 atomic_inc(&bitmap->pending_writes);
417 set_buffer_locked(bh);
418 set_buffer_mapped(bh);
422 bh = bh->b_this_page;
426 wait_event(bitmap->write_wait,
427 atomic_read(&bitmap->pending_writes)==0);
428 if (bitmap->flags & BITMAP_WRITE_ERROR) {
430 page = ERR_PTR(-EIO);
434 printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
436 (unsigned long long)index << PAGE_SHIFT,
442 * bitmap file superblock operations
445 /* update the event counter and sync the superblock to disk */
446 void bitmap_update_sb(struct bitmap *bitmap)
451 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
453 if (bitmap->mddev->bitmap_info.external)
455 spin_lock_irqsave(&bitmap->lock, flags);
456 if (!bitmap->sb_page) { /* no superblock */
457 spin_unlock_irqrestore(&bitmap->lock, flags);
460 spin_unlock_irqrestore(&bitmap->lock, flags);
461 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
462 sb->events = cpu_to_le64(bitmap->mddev->events);
463 if (bitmap->mddev->events < bitmap->events_cleared)
464 /* rocking back to read-only */
465 bitmap->events_cleared = bitmap->mddev->events;
466 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
467 sb->state = cpu_to_le32(bitmap->flags);
468 /* Just in case these have been changed via sysfs: */
469 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
470 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
471 kunmap_atomic(sb, KM_USER0);
472 write_page(bitmap, bitmap->sb_page, 1);
475 /* print out the bitmap file superblock */
476 void bitmap_print_sb(struct bitmap *bitmap)
480 if (!bitmap || !bitmap->sb_page)
482 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
483 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
484 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
485 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
486 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
487 *(__u32 *)(sb->uuid+0),
488 *(__u32 *)(sb->uuid+4),
489 *(__u32 *)(sb->uuid+8),
490 *(__u32 *)(sb->uuid+12));
491 printk(KERN_DEBUG " events: %llu\n",
492 (unsigned long long) le64_to_cpu(sb->events));
493 printk(KERN_DEBUG "events cleared: %llu\n",
494 (unsigned long long) le64_to_cpu(sb->events_cleared));
495 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
496 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
497 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
498 printk(KERN_DEBUG " sync size: %llu KB\n",
499 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
500 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
501 kunmap_atomic(sb, KM_USER0);
508 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
509 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
510 * This function verifies 'bitmap_info' and populates the on-disk bitmap
511 * structure, which is to be written to disk.
513 * Returns: 0 on success, -Exxx on error
515 static int bitmap_new_disk_sb(struct bitmap *bitmap)
518 unsigned long chunksize, daemon_sleep, write_behind;
521 bitmap->sb_page = alloc_page(GFP_KERNEL);
522 if (IS_ERR(bitmap->sb_page)) {
523 err = PTR_ERR(bitmap->sb_page);
524 bitmap->sb_page = NULL;
527 bitmap->sb_page->index = 0;
529 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
531 sb->magic = cpu_to_le32(BITMAP_MAGIC);
532 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
534 chunksize = bitmap->mddev->bitmap_info.chunksize;
536 if (!is_power_of_2(chunksize)) {
537 kunmap_atomic(sb, KM_USER0);
538 printk(KERN_ERR "bitmap chunksize not a power of 2\n");
541 sb->chunksize = cpu_to_le32(chunksize);
543 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
545 (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
546 printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
547 daemon_sleep = 5 * HZ;
549 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
550 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
553 * FIXME: write_behind for RAID1. If not specified, what
554 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
556 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
557 if (write_behind > COUNTER_MAX)
558 write_behind = COUNTER_MAX / 2;
559 sb->write_behind = cpu_to_le32(write_behind);
560 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
562 /* keep the array size field of the bitmap superblock up to date */
563 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
565 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
567 bitmap->flags |= BITMAP_STALE;
568 sb->state |= cpu_to_le32(BITMAP_STALE);
569 bitmap->events_cleared = bitmap->mddev->events;
570 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
572 bitmap->flags |= BITMAP_HOSTENDIAN;
573 sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
575 kunmap_atomic(sb, KM_USER0);
580 /* read the superblock from the bitmap file and initialize some bitmap fields */
581 static int bitmap_read_sb(struct bitmap *bitmap)
585 unsigned long chunksize, daemon_sleep, write_behind;
586 unsigned long long events;
589 /* page 0 is the superblock, read it... */
591 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
592 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
594 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
596 bitmap->sb_page = read_sb_page(bitmap->mddev,
597 bitmap->mddev->bitmap_info.offset,
599 0, sizeof(bitmap_super_t));
601 if (IS_ERR(bitmap->sb_page)) {
602 err = PTR_ERR(bitmap->sb_page);
603 bitmap->sb_page = NULL;
607 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
609 chunksize = le32_to_cpu(sb->chunksize);
610 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
611 write_behind = le32_to_cpu(sb->write_behind);
613 /* verify that the bitmap-specific fields are valid */
614 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
615 reason = "bad magic";
616 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
617 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
618 reason = "unrecognized superblock version";
619 else if (chunksize < 512)
620 reason = "bitmap chunksize too small";
621 else if (!is_power_of_2(chunksize))
622 reason = "bitmap chunksize not a power of 2";
623 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
624 reason = "daemon sleep period out of range";
625 else if (write_behind > COUNTER_MAX)
626 reason = "write-behind limit out of range (0 - 16383)";
628 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
629 bmname(bitmap), reason);
633 /* keep the array size field of the bitmap superblock up to date */
634 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
636 if (!bitmap->mddev->persistent)
640 * if we have a persistent array superblock, compare the
641 * bitmap's UUID and event counter to the mddev's
643 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
644 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
648 events = le64_to_cpu(sb->events);
649 if (events < bitmap->mddev->events) {
650 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
651 "-- forcing full recovery\n", bmname(bitmap), events,
652 (unsigned long long) bitmap->mddev->events);
653 sb->state |= cpu_to_le32(BITMAP_STALE);
656 /* assign fields using values from superblock */
657 bitmap->mddev->bitmap_info.chunksize = chunksize;
658 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
659 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
660 bitmap->flags |= le32_to_cpu(sb->state);
661 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
662 bitmap->flags |= BITMAP_HOSTENDIAN;
663 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
664 if (bitmap->flags & BITMAP_STALE)
665 bitmap->events_cleared = bitmap->mddev->events;
668 kunmap_atomic(sb, KM_USER0);
670 bitmap_print_sb(bitmap);
674 enum bitmap_mask_op {
679 /* record the state of the bitmap in the superblock. Return the old value */
680 static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
681 enum bitmap_mask_op op)
687 spin_lock_irqsave(&bitmap->lock, flags);
688 if (!bitmap->sb_page) { /* can't set the state */
689 spin_unlock_irqrestore(&bitmap->lock, flags);
692 spin_unlock_irqrestore(&bitmap->lock, flags);
693 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
694 old = le32_to_cpu(sb->state) & bits;
697 sb->state |= cpu_to_le32(bits);
698 bitmap->flags |= bits;
701 sb->state &= cpu_to_le32(~bits);
702 bitmap->flags &= ~bits;
707 kunmap_atomic(sb, KM_USER0);
712 * general bitmap file operations
718 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
719 * file a page at a time. There's a superblock at the start of the file.
721 /* calculate the index of the page that contains this bit */
722 static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
724 if (!bitmap->mddev->bitmap_info.external)
725 chunk += sizeof(bitmap_super_t) << 3;
726 return chunk >> PAGE_BIT_SHIFT;
729 /* calculate the (bit) offset of this bit within a page */
730 static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
732 if (!bitmap->mddev->bitmap_info.external)
733 chunk += sizeof(bitmap_super_t) << 3;
734 return chunk & (PAGE_BITS - 1);
738 * return a pointer to the page in the filemap that contains the given bit
740 * this lookup is complicated by the fact that the bitmap sb might be exactly
741 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
744 static inline struct page *filemap_get_page(struct bitmap *bitmap,
747 if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
749 return bitmap->filemap[file_page_index(bitmap, chunk)
750 - file_page_index(bitmap, 0)];
753 static void bitmap_file_unmap(struct bitmap *bitmap)
755 struct page **map, *sb_page;
760 spin_lock_irqsave(&bitmap->lock, flags);
761 map = bitmap->filemap;
762 bitmap->filemap = NULL;
763 attr = bitmap->filemap_attr;
764 bitmap->filemap_attr = NULL;
765 pages = bitmap->file_pages;
766 bitmap->file_pages = 0;
767 sb_page = bitmap->sb_page;
768 bitmap->sb_page = NULL;
769 spin_unlock_irqrestore(&bitmap->lock, flags);
772 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
773 free_buffers(map[pages]);
778 free_buffers(sb_page);
781 static void bitmap_file_put(struct bitmap *bitmap)
786 spin_lock_irqsave(&bitmap->lock, flags);
789 spin_unlock_irqrestore(&bitmap->lock, flags);
792 wait_event(bitmap->write_wait,
793 atomic_read(&bitmap->pending_writes)==0);
794 bitmap_file_unmap(bitmap);
797 struct inode *inode = file->f_path.dentry->d_inode;
798 invalidate_mapping_pages(inode->i_mapping, 0, -1);
804 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
805 * then it is no longer reliable, so we stop using it and we mark the file
806 * as failed in the superblock
808 static void bitmap_file_kick(struct bitmap *bitmap)
810 char *path, *ptr = NULL;
812 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
813 bitmap_update_sb(bitmap);
816 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
818 ptr = d_path(&bitmap->file->f_path, path,
822 "%s: kicking failed bitmap file %s from array!\n",
823 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
828 "%s: disabling internal bitmap due to errors\n",
832 bitmap_file_put(bitmap);
837 enum bitmap_page_attr {
838 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
839 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
840 * i.e. counter is 1 or 2. */
841 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
844 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
845 enum bitmap_page_attr attr)
847 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
850 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
851 enum bitmap_page_attr attr)
853 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
856 static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
857 enum bitmap_page_attr attr)
859 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
863 * bitmap_file_set_bit -- called before performing a write to the md device
864 * to set (and eventually sync) a particular bit in the bitmap file
866 * we set the bit immediately, then we record the page number so that
867 * when an unplug occurs, we can flush the dirty pages out to disk
869 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
874 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
876 if (!bitmap->filemap)
879 page = filemap_get_page(bitmap, chunk);
882 bit = file_page_offset(bitmap, chunk);
885 kaddr = kmap_atomic(page, KM_USER0);
886 if (bitmap->flags & BITMAP_HOSTENDIAN)
889 __set_bit_le(bit, kaddr);
890 kunmap_atomic(kaddr, KM_USER0);
891 pr_debug("set file bit %lu page %lu\n", bit, page->index);
892 /* record page number so it gets flushed to disk when unplug occurs */
893 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
896 /* this gets called when the md device is ready to unplug its underlying
897 * (slave) device queues -- before we let any writes go down, we need to
898 * sync the dirty pages of the bitmap file to disk */
899 void bitmap_unplug(struct bitmap *bitmap)
901 unsigned long i, flags;
902 int dirty, need_write;
909 /* look at each page to see if there are any set bits that need to be
910 * flushed out to disk */
911 for (i = 0; i < bitmap->file_pages; i++) {
912 spin_lock_irqsave(&bitmap->lock, flags);
913 if (!bitmap->filemap) {
914 spin_unlock_irqrestore(&bitmap->lock, flags);
917 page = bitmap->filemap[i];
918 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
919 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
920 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
921 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
924 spin_unlock_irqrestore(&bitmap->lock, flags);
926 if (dirty || need_write)
927 write_page(bitmap, page, 0);
929 if (wait) { /* if any writes were performed, we need to wait on them */
931 wait_event(bitmap->write_wait,
932 atomic_read(&bitmap->pending_writes)==0);
934 md_super_wait(bitmap->mddev);
936 if (bitmap->flags & BITMAP_WRITE_ERROR)
937 bitmap_file_kick(bitmap);
939 EXPORT_SYMBOL(bitmap_unplug);
941 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
942 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
943 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
944 * memory mapping of the bitmap file
946 * if there's no bitmap file, or if the bitmap file had been
947 * previously kicked from the array, we mark all the bits as
948 * 1's in order to cause a full resync.
950 * We ignore all bits for sectors that end earlier than 'start'.
951 * This is used when reading an out-of-date bitmap...
953 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
955 unsigned long i, chunks, index, oldindex, bit;
956 struct page *page = NULL, *oldpage = NULL;
957 unsigned long num_pages, bit_cnt = 0;
959 unsigned long bytes, offset;
964 chunks = bitmap->chunks;
967 BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
969 outofdate = bitmap->flags & BITMAP_STALE;
971 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
972 "recovery\n", bmname(bitmap));
974 bytes = DIV_ROUND_UP(bitmap->chunks, 8);
975 if (!bitmap->mddev->bitmap_info.external)
976 bytes += sizeof(bitmap_super_t);
978 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
980 if (file && i_size_read(file->f_mapping->host) < bytes) {
981 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
983 (unsigned long) i_size_read(file->f_mapping->host),
990 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
991 if (!bitmap->filemap)
994 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
995 bitmap->filemap_attr = kzalloc(
996 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
998 if (!bitmap->filemap_attr)
1003 for (i = 0; i < chunks; i++) {
1005 index = file_page_index(bitmap, i);
1006 bit = file_page_offset(bitmap, i);
1007 if (index != oldindex) { /* this is a new page, read it in */
1009 /* unmap the old page, we're done with it */
1010 if (index == num_pages-1)
1011 count = bytes - index * PAGE_SIZE;
1014 if (index == 0 && bitmap->sb_page) {
1016 * if we're here then the superblock page
1017 * contains some bits (PAGE_SIZE != sizeof sb)
1018 * we've already read it in, so just use it
1020 page = bitmap->sb_page;
1021 offset = sizeof(bitmap_super_t);
1023 page = read_sb_page(
1025 bitmap->mddev->bitmap_info.offset,
1029 page = read_page(file, index, bitmap, count);
1032 page = read_sb_page(bitmap->mddev,
1033 bitmap->mddev->bitmap_info.offset,
1038 if (IS_ERR(page)) { /* read error */
1039 ret = PTR_ERR(page);
1046 bitmap->filemap[bitmap->file_pages++] = page;
1047 bitmap->last_page_size = count;
1051 * if bitmap is out of date, dirty the
1052 * whole page and write it out
1054 paddr = kmap_atomic(page, KM_USER0);
1055 memset(paddr + offset, 0xff,
1056 PAGE_SIZE - offset);
1057 kunmap_atomic(paddr, KM_USER0);
1058 write_page(bitmap, page, 1);
1061 if (bitmap->flags & BITMAP_WRITE_ERROR)
1065 paddr = kmap_atomic(page, KM_USER0);
1066 if (bitmap->flags & BITMAP_HOSTENDIAN)
1067 b = test_bit(bit, paddr);
1069 b = test_bit_le(bit, paddr);
1070 kunmap_atomic(paddr, KM_USER0);
1072 /* if the disk bit is set, set the memory bit */
1073 int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
1075 bitmap_set_memory_bits(bitmap,
1076 (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
1082 /* everything went OK */
1084 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1086 if (bit_cnt) { /* Kick recovery if any bits were set */
1087 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1088 md_wakeup_thread(bitmap->mddev->thread);
1091 printk(KERN_INFO "%s: bitmap initialized from disk: "
1092 "read %lu/%lu pages, set %lu of %lu bits\n",
1093 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
1098 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1099 bmname(bitmap), ret);
1103 void bitmap_write_all(struct bitmap *bitmap)
1105 /* We don't actually write all bitmap blocks here,
1106 * just flag them as needing to be written
1110 spin_lock_irq(&bitmap->lock);
1111 for (i = 0; i < bitmap->file_pages; i++)
1112 set_page_attr(bitmap, bitmap->filemap[i],
1113 BITMAP_PAGE_NEEDWRITE);
1114 bitmap->allclean = 0;
1115 spin_unlock_irq(&bitmap->lock);
1118 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1120 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1121 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1122 bitmap->bp[page].count += inc;
1123 bitmap_checkfree(bitmap, page);
1125 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1126 sector_t offset, sector_t *blocks,
1130 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1134 void bitmap_daemon_work(struct mddev *mddev)
1136 struct bitmap *bitmap;
1138 unsigned long flags;
1139 struct page *page = NULL, *lastpage = NULL;
1143 /* Use a mutex to guard daemon_work against
1146 mutex_lock(&mddev->bitmap_info.mutex);
1147 bitmap = mddev->bitmap;
1148 if (bitmap == NULL) {
1149 mutex_unlock(&mddev->bitmap_info.mutex);
1152 if (time_before(jiffies, bitmap->daemon_lastrun
1153 + mddev->bitmap_info.daemon_sleep))
1156 bitmap->daemon_lastrun = jiffies;
1157 if (bitmap->allclean) {
1158 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1161 bitmap->allclean = 1;
1163 spin_lock_irqsave(&bitmap->lock, flags);
1164 for (j = 0; j < bitmap->chunks; j++) {
1165 bitmap_counter_t *bmc;
1166 if (!bitmap->filemap)
1167 /* error or shutdown */
1170 page = filemap_get_page(bitmap, j);
1172 if (page != lastpage) {
1173 /* skip this page unless it's marked as needing cleaning */
1174 if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) {
1175 int need_write = test_page_attr(bitmap, page,
1176 BITMAP_PAGE_NEEDWRITE);
1178 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1180 spin_unlock_irqrestore(&bitmap->lock, flags);
1182 write_page(bitmap, page, 0);
1183 spin_lock_irqsave(&bitmap->lock, flags);
1184 j |= (PAGE_BITS - 1);
1188 /* grab the new page, sync and release the old */
1189 if (lastpage != NULL) {
1190 if (test_page_attr(bitmap, lastpage,
1191 BITMAP_PAGE_NEEDWRITE)) {
1192 clear_page_attr(bitmap, lastpage,
1193 BITMAP_PAGE_NEEDWRITE);
1194 spin_unlock_irqrestore(&bitmap->lock, flags);
1195 write_page(bitmap, lastpage, 0);
1197 set_page_attr(bitmap, lastpage,
1198 BITMAP_PAGE_NEEDWRITE);
1199 bitmap->allclean = 0;
1200 spin_unlock_irqrestore(&bitmap->lock, flags);
1203 spin_unlock_irqrestore(&bitmap->lock, flags);
1206 /* We are possibly going to clear some bits, so make
1207 * sure that events_cleared is up-to-date.
1209 if (bitmap->need_sync &&
1210 mddev->bitmap_info.external == 0) {
1212 bitmap->need_sync = 0;
1213 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1214 sb->events_cleared =
1215 cpu_to_le64(bitmap->events_cleared);
1216 kunmap_atomic(sb, KM_USER0);
1217 write_page(bitmap, bitmap->sb_page, 1);
1219 spin_lock_irqsave(&bitmap->lock, flags);
1220 if (!bitmap->need_sync)
1221 clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
1223 bitmap->allclean = 0;
1225 bmc = bitmap_get_counter(bitmap,
1226 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1229 j |= PAGE_COUNTER_MASK;
1231 if (*bmc == 1 && !bitmap->need_sync) {
1232 /* we can clear the bit */
1234 bitmap_count_page(bitmap,
1235 (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
1239 paddr = kmap_atomic(page, KM_USER0);
1240 if (bitmap->flags & BITMAP_HOSTENDIAN)
1241 clear_bit(file_page_offset(bitmap, j),
1245 file_page_offset(bitmap,
1248 kunmap_atomic(paddr, KM_USER0);
1249 } else if (*bmc <= 2) {
1250 *bmc = 1; /* maybe clear the bit next time */
1251 set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
1252 bitmap->allclean = 0;
1256 spin_unlock_irqrestore(&bitmap->lock, flags);
1258 /* now sync the final page */
1259 if (lastpage != NULL) {
1260 spin_lock_irqsave(&bitmap->lock, flags);
1261 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1262 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1263 spin_unlock_irqrestore(&bitmap->lock, flags);
1264 write_page(bitmap, lastpage, 0);
1266 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1267 bitmap->allclean = 0;
1268 spin_unlock_irqrestore(&bitmap->lock, flags);
1273 if (bitmap->allclean == 0)
1274 mddev->thread->timeout =
1275 mddev->bitmap_info.daemon_sleep;
1276 mutex_unlock(&mddev->bitmap_info.mutex);
1279 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1280 sector_t offset, sector_t *blocks,
1282 __releases(bitmap->lock)
1283 __acquires(bitmap->lock)
1285 /* If 'create', we might release the lock and reclaim it.
1286 * The lock must have been taken with interrupts enabled.
1287 * If !create, we don't release the lock.
1289 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1290 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1291 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1295 err = bitmap_checkpage(bitmap, page, create);
1297 if (bitmap->bp[page].hijacked ||
1298 bitmap->bp[page].map == NULL)
1299 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1300 PAGE_COUNTER_SHIFT - 1);
1302 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1303 *blocks = csize - (offset & (csize - 1));
1308 /* now locked ... */
1310 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1311 /* should we use the first or second counter field
1312 * of the hijacked pointer? */
1313 int hi = (pageoff > PAGE_COUNTER_MASK);
1314 return &((bitmap_counter_t *)
1315 &bitmap->bp[page].map)[hi];
1316 } else /* page is allocated */
1317 return (bitmap_counter_t *)
1318 &(bitmap->bp[page].map[pageoff]);
1321 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1328 atomic_inc(&bitmap->behind_writes);
1329 bw = atomic_read(&bitmap->behind_writes);
1330 if (bw > bitmap->behind_writes_used)
1331 bitmap->behind_writes_used = bw;
1333 pr_debug("inc write-behind count %d/%lu\n",
1334 bw, bitmap->mddev->bitmap_info.max_write_behind);
1339 bitmap_counter_t *bmc;
1341 spin_lock_irq(&bitmap->lock);
1342 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1344 spin_unlock_irq(&bitmap->lock);
1348 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
1349 DEFINE_WAIT(__wait);
1350 /* note that it is safe to do the prepare_to_wait
1351 * after the test as long as we do it before dropping
1354 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1355 TASK_UNINTERRUPTIBLE);
1356 spin_unlock_irq(&bitmap->lock);
1358 finish_wait(&bitmap->overflow_wait, &__wait);
1364 bitmap_file_set_bit(bitmap, offset);
1365 bitmap_count_page(bitmap, offset, 1);
1373 spin_unlock_irq(&bitmap->lock);
1376 if (sectors > blocks)
1383 EXPORT_SYMBOL(bitmap_startwrite);
1385 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1386 int success, int behind)
1391 if (atomic_dec_and_test(&bitmap->behind_writes))
1392 wake_up(&bitmap->behind_wait);
1393 pr_debug("dec write-behind count %d/%lu\n",
1394 atomic_read(&bitmap->behind_writes),
1395 bitmap->mddev->bitmap_info.max_write_behind);
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);
1410 if (success && !bitmap->mddev->degraded &&
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 = 2 | (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 spin_lock_irq(&bitmap->lock);
1609 bitmap_file_set_bit(bitmap, sec);
1610 spin_unlock_irq(&bitmap->lock);
1611 if (sec < bitmap->mddev->recovery_cp)
1612 /* We are asserting that the array is dirty,
1613 * so move the recovery_cp address back so
1614 * that it is obvious that it is dirty
1616 bitmap->mddev->recovery_cp = sec;
1621 * flush out any pending updates
1623 void bitmap_flush(struct mddev *mddev)
1625 struct bitmap *bitmap = mddev->bitmap;
1628 if (!bitmap) /* there was no bitmap */
1631 /* run the daemon_work three time to ensure everything is flushed
1634 sleep = mddev->bitmap_info.daemon_sleep * 2;
1635 bitmap->daemon_lastrun -= sleep;
1636 bitmap_daemon_work(mddev);
1637 bitmap->daemon_lastrun -= sleep;
1638 bitmap_daemon_work(mddev);
1639 bitmap->daemon_lastrun -= sleep;
1640 bitmap_daemon_work(mddev);
1641 bitmap_update_sb(bitmap);
1645 * free memory that was allocated
1647 static void bitmap_free(struct bitmap *bitmap)
1649 unsigned long k, pages;
1650 struct bitmap_page *bp;
1652 if (!bitmap) /* there was no bitmap */
1655 /* release the bitmap file and kill the daemon */
1656 bitmap_file_put(bitmap);
1659 pages = bitmap->pages;
1661 /* free all allocated memory */
1663 if (bp) /* deallocate the page memory */
1664 for (k = 0; k < pages; k++)
1665 if (bp[k].map && !bp[k].hijacked)
1671 void bitmap_destroy(struct mddev *mddev)
1673 struct bitmap *bitmap = mddev->bitmap;
1675 if (!bitmap) /* there was no bitmap */
1678 mutex_lock(&mddev->bitmap_info.mutex);
1679 mddev->bitmap = NULL; /* disconnect from the md device */
1680 mutex_unlock(&mddev->bitmap_info.mutex);
1682 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1684 if (bitmap->sysfs_can_clear)
1685 sysfs_put(bitmap->sysfs_can_clear);
1687 bitmap_free(bitmap);
1691 * initialize the bitmap structure
1692 * if this returns an error, bitmap_destroy must be called to do clean up
1694 int bitmap_create(struct mddev *mddev)
1696 struct bitmap *bitmap;
1697 sector_t blocks = mddev->resync_max_sectors;
1698 unsigned long chunks;
1699 unsigned long pages;
1700 struct file *file = mddev->bitmap_info.file;
1702 struct sysfs_dirent *bm = NULL;
1704 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
1707 && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
1710 BUG_ON(file && mddev->bitmap_info.offset);
1712 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1716 spin_lock_init(&bitmap->lock);
1717 atomic_set(&bitmap->pending_writes, 0);
1718 init_waitqueue_head(&bitmap->write_wait);
1719 init_waitqueue_head(&bitmap->overflow_wait);
1720 init_waitqueue_head(&bitmap->behind_wait);
1722 bitmap->mddev = mddev;
1725 bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
1727 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
1730 bitmap->sysfs_can_clear = NULL;
1732 bitmap->file = file;
1735 /* As future accesses to this file will use bmap,
1736 * and bypass the page cache, we must sync the file
1741 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
1742 if (!mddev->bitmap_info.external) {
1744 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1745 * instructing us to create a new on-disk bitmap instance.
1747 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1748 err = bitmap_new_disk_sb(bitmap);
1750 err = bitmap_read_sb(bitmap);
1753 if (mddev->bitmap_info.chunksize == 0 ||
1754 mddev->bitmap_info.daemon_sleep == 0)
1755 /* chunksize and time_base need to be
1762 bitmap->daemon_lastrun = jiffies;
1763 bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
1765 /* now that chunksize and chunkshift are set, we can use these macros */
1766 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
1767 CHUNK_BLOCK_SHIFT(bitmap);
1768 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1772 bitmap->chunks = chunks;
1773 bitmap->pages = pages;
1774 bitmap->missing_pages = pages;
1776 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1782 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1783 pages, bmname(bitmap));
1785 mddev->bitmap = bitmap;
1788 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1791 bitmap_free(bitmap);
1795 int bitmap_load(struct mddev *mddev)
1799 sector_t sector = 0;
1800 struct bitmap *bitmap = mddev->bitmap;
1805 /* Clear out old bitmap info first: Either there is none, or we
1806 * are resuming after someone else has possibly changed things,
1807 * so we should forget old cached info.
1808 * All chunks should be clean, but some might need_sync.
1810 while (sector < mddev->resync_max_sectors) {
1812 bitmap_start_sync(bitmap, sector, &blocks, 0);
1815 bitmap_close_sync(bitmap);
1817 if (mddev->degraded == 0
1818 || bitmap->events_cleared == mddev->events)
1819 /* no need to keep dirty bits to optimise a
1820 * re-add of a missing device */
1821 start = mddev->recovery_cp;
1823 err = bitmap_init_from_disk(bitmap, start);
1828 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
1829 md_wakeup_thread(mddev->thread);
1831 bitmap_update_sb(bitmap);
1833 if (bitmap->flags & BITMAP_WRITE_ERROR)
1838 EXPORT_SYMBOL_GPL(bitmap_load);
1840 void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
1842 unsigned long chunk_kb;
1843 unsigned long flags;
1848 spin_lock_irqsave(&bitmap->lock, flags);
1849 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
1850 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
1852 bitmap->pages - bitmap->missing_pages,
1854 (bitmap->pages - bitmap->missing_pages)
1855 << (PAGE_SHIFT - 10),
1856 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
1857 chunk_kb ? "KB" : "B");
1859 seq_printf(seq, ", file: ");
1860 seq_path(seq, &bitmap->file->f_path, " \t\n");
1863 seq_printf(seq, "\n");
1864 spin_unlock_irqrestore(&bitmap->lock, flags);
1868 location_show(struct mddev *mddev, char *page)
1871 if (mddev->bitmap_info.file)
1872 len = sprintf(page, "file");
1873 else if (mddev->bitmap_info.offset)
1874 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
1876 len = sprintf(page, "none");
1877 len += sprintf(page+len, "\n");
1882 location_store(struct mddev *mddev, const char *buf, size_t len)
1886 if (!mddev->pers->quiesce)
1888 if (mddev->recovery || mddev->sync_thread)
1892 if (mddev->bitmap || mddev->bitmap_info.file ||
1893 mddev->bitmap_info.offset) {
1894 /* bitmap already configured. Only option is to clear it */
1895 if (strncmp(buf, "none", 4) != 0)
1898 mddev->pers->quiesce(mddev, 1);
1899 bitmap_destroy(mddev);
1900 mddev->pers->quiesce(mddev, 0);
1902 mddev->bitmap_info.offset = 0;
1903 if (mddev->bitmap_info.file) {
1904 struct file *f = mddev->bitmap_info.file;
1905 mddev->bitmap_info.file = NULL;
1906 restore_bitmap_write_access(f);
1910 /* No bitmap, OK to set a location */
1912 if (strncmp(buf, "none", 4) == 0)
1913 /* nothing to be done */;
1914 else if (strncmp(buf, "file:", 5) == 0) {
1915 /* Not supported yet */
1920 rv = strict_strtoll(buf+1, 10, &offset);
1922 rv = strict_strtoll(buf, 10, &offset);
1927 if (mddev->bitmap_info.external == 0 &&
1928 mddev->major_version == 0 &&
1929 offset != mddev->bitmap_info.default_offset)
1931 mddev->bitmap_info.offset = offset;
1933 mddev->pers->quiesce(mddev, 1);
1934 rv = bitmap_create(mddev);
1936 rv = bitmap_load(mddev);
1938 bitmap_destroy(mddev);
1939 mddev->bitmap_info.offset = 0;
1941 mddev->pers->quiesce(mddev, 0);
1947 if (!mddev->external) {
1948 /* Ensure new bitmap info is stored in
1949 * metadata promptly.
1951 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1952 md_wakeup_thread(mddev->thread);
1957 static struct md_sysfs_entry bitmap_location =
1958 __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
1961 timeout_show(struct mddev *mddev, char *page)
1964 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
1965 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
1967 len = sprintf(page, "%lu", secs);
1969 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
1970 len += sprintf(page+len, "\n");
1975 timeout_store(struct mddev *mddev, const char *buf, size_t len)
1977 /* timeout can be set at any time */
1978 unsigned long timeout;
1979 int rv = strict_strtoul_scaled(buf, &timeout, 4);
1983 /* just to make sure we don't overflow... */
1984 if (timeout >= LONG_MAX / HZ)
1987 timeout = timeout * HZ / 10000;
1989 if (timeout >= MAX_SCHEDULE_TIMEOUT)
1990 timeout = MAX_SCHEDULE_TIMEOUT-1;
1993 mddev->bitmap_info.daemon_sleep = timeout;
1994 if (mddev->thread) {
1995 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
1996 * the bitmap is all clean and we don't need to
1997 * adjust the timeout right now
1999 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2000 mddev->thread->timeout = timeout;
2001 md_wakeup_thread(mddev->thread);
2007 static struct md_sysfs_entry bitmap_timeout =
2008 __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2011 backlog_show(struct mddev *mddev, char *page)
2013 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2017 backlog_store(struct mddev *mddev, const char *buf, size_t len)
2019 unsigned long backlog;
2020 int rv = strict_strtoul(buf, 10, &backlog);
2023 if (backlog > COUNTER_MAX)
2025 mddev->bitmap_info.max_write_behind = backlog;
2029 static struct md_sysfs_entry bitmap_backlog =
2030 __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2033 chunksize_show(struct mddev *mddev, char *page)
2035 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2039 chunksize_store(struct mddev *mddev, const char *buf, size_t len)
2041 /* Can only be changed when no bitmap is active */
2043 unsigned long csize;
2046 rv = strict_strtoul(buf, 10, &csize);
2050 !is_power_of_2(csize))
2052 mddev->bitmap_info.chunksize = csize;
2056 static struct md_sysfs_entry bitmap_chunksize =
2057 __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2059 static ssize_t metadata_show(struct mddev *mddev, char *page)
2061 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2062 ? "external" : "internal"));
2065 static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
2067 if (mddev->bitmap ||
2068 mddev->bitmap_info.file ||
2069 mddev->bitmap_info.offset)
2071 if (strncmp(buf, "external", 8) == 0)
2072 mddev->bitmap_info.external = 1;
2073 else if (strncmp(buf, "internal", 8) == 0)
2074 mddev->bitmap_info.external = 0;
2080 static struct md_sysfs_entry bitmap_metadata =
2081 __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2083 static ssize_t can_clear_show(struct mddev *mddev, char *page)
2087 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2090 len = sprintf(page, "\n");
2094 static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
2096 if (mddev->bitmap == NULL)
2098 if (strncmp(buf, "false", 5) == 0)
2099 mddev->bitmap->need_sync = 1;
2100 else if (strncmp(buf, "true", 4) == 0) {
2101 if (mddev->degraded)
2103 mddev->bitmap->need_sync = 0;
2109 static struct md_sysfs_entry bitmap_can_clear =
2110 __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2113 behind_writes_used_show(struct mddev *mddev, char *page)
2115 if (mddev->bitmap == NULL)
2116 return sprintf(page, "0\n");
2117 return sprintf(page, "%lu\n",
2118 mddev->bitmap->behind_writes_used);
2122 behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
2125 mddev->bitmap->behind_writes_used = 0;
2129 static struct md_sysfs_entry max_backlog_used =
2130 __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2131 behind_writes_used_show, behind_writes_used_reset);
2133 static struct attribute *md_bitmap_attrs[] = {
2134 &bitmap_location.attr,
2135 &bitmap_timeout.attr,
2136 &bitmap_backlog.attr,
2137 &bitmap_chunksize.attr,
2138 &bitmap_metadata.attr,
2139 &bitmap_can_clear.attr,
2140 &max_backlog_used.attr,
2143 struct attribute_group md_bitmap_group = {
2145 .attrs = md_bitmap_attrs,