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
10 * - added bitmap daemon (to asynchronously clear bitmap bits from disk)
16 * flush after percent set rather than just time based. (maybe both).
17 * wait if count gets too high, wake when it drops to half.
18 * allow bitmap to be mirrored with superblock (before or after...)
19 * allow hot-add to re-instate a current device.
20 * allow hot-add of bitmap after quiessing device
23 #include <linux/module.h>
24 #include <linux/version.h>
25 #include <linux/errno.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/config.h>
29 #include <linux/timer.h>
30 #include <linux/sched.h>
31 #include <linux/list.h>
32 #include <linux/file.h>
33 #include <linux/mount.h>
34 #include <linux/buffer_head.h>
35 #include <linux/raid/md.h>
36 #include <linux/raid/bitmap.h>
43 /* these are for debugging purposes only! */
45 /* define one and only one of these */
46 #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
47 #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
48 #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
49 #define INJECT_FAULTS_4 0 /* undef */
50 #define INJECT_FAULTS_5 0 /* undef */
51 #define INJECT_FAULTS_6 0
53 /* if these are defined, the driver will fail! debug only */
54 #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
55 #define INJECT_FATAL_FAULT_2 0 /* undef */
56 #define INJECT_FATAL_FAULT_3 0 /* undef */
59 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
60 #define DPRINTK(x...) do { } while(0)
64 # define PRINTK(x...) printk(KERN_DEBUG x)
70 static inline char * bmname(struct bitmap *bitmap)
72 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
77 * test if the bitmap is active
79 int bitmap_active(struct bitmap *bitmap)
86 spin_lock_irqsave(&bitmap->lock, flags);
87 res = bitmap->flags & BITMAP_ACTIVE;
88 spin_unlock_irqrestore(&bitmap->lock, flags);
92 #define WRITE_POOL_SIZE 256
93 /* mempool for queueing pending writes on the bitmap file */
94 static void *write_pool_alloc(gfp_t gfp_flags, void *data)
96 return kmalloc(sizeof(struct page_list), gfp_flags);
99 static void write_pool_free(void *ptr, void *data)
105 * just a placeholder - calls kmalloc for bitmap pages
107 static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
111 #ifdef INJECT_FAULTS_1
114 page = kmalloc(PAGE_SIZE, GFP_NOIO);
117 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
119 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
120 bmname(bitmap), page);
125 * for now just a placeholder -- just calls kfree for bitmap pages
127 static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
129 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
134 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
136 * 1) check to see if this page is allocated, if it's not then try to alloc
137 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
138 * page pointer directly as a counter
140 * if we find our page, we increment the page's refcount so that it stays
141 * allocated while we're using it
143 static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
145 unsigned char *mappage;
147 if (page >= bitmap->pages) {
149 "%s: invalid bitmap page request: %lu (> %lu)\n",
150 bmname(bitmap), page, bitmap->pages-1);
155 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
158 if (bitmap->bp[page].map) /* page is already allocated, just return */
164 spin_unlock_irq(&bitmap->lock);
166 /* this page has not been allocated yet */
168 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
169 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
171 /* failed - set the hijacked flag so that we can use the
172 * pointer as a counter */
173 spin_lock_irq(&bitmap->lock);
174 if (!bitmap->bp[page].map)
175 bitmap->bp[page].hijacked = 1;
181 spin_lock_irq(&bitmap->lock);
183 /* recheck the page */
185 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
186 /* somebody beat us to getting the page */
187 bitmap_free_page(bitmap, mappage);
191 /* no page was in place and we have one, so install it */
193 memset(mappage, 0, PAGE_SIZE);
194 bitmap->bp[page].map = mappage;
195 bitmap->missing_pages--;
201 /* if page is completely empty, put it back on the free list, or dealloc it */
202 /* if page was hijacked, unmark the flag so it might get alloced next time */
203 /* Note: lock should be held when calling this */
204 static inline void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
208 if (bitmap->bp[page].count) /* page is still busy */
211 /* page is no longer in use, it can be released */
213 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
214 bitmap->bp[page].hijacked = 0;
215 bitmap->bp[page].map = NULL;
219 /* normal case, free the page */
222 /* actually ... let's not. We will probably need the page again exactly when
223 * memory is tight and we are flusing to disk
227 ptr = bitmap->bp[page].map;
228 bitmap->bp[page].map = NULL;
229 bitmap->missing_pages++;
230 bitmap_free_page(bitmap, ptr);
237 * bitmap file handling - read and write the bitmap file and its superblock
240 /* copy the pathname of a file to a buffer */
241 char *file_path(struct file *file, char *buf, int count)
252 buf = d_path(d, v, buf, count);
254 return IS_ERR(buf) ? NULL : buf;
258 * basic page I/O operations
261 /* IO operations when bitmap is stored near all superblocks */
262 static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
264 /* choose a good rdev and read the page from there */
267 struct list_head *tmp;
268 struct page *page = alloc_page(GFP_KERNEL);
272 return ERR_PTR(-ENOMEM);
274 ITERATE_RDEV(mddev, rdev, tmp) {
275 if (! rdev->in_sync || rdev->faulty)
278 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
280 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
285 return ERR_PTR(-EIO);
289 static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
292 struct list_head *tmp;
294 ITERATE_RDEV(mddev, rdev, tmp)
295 if (rdev->in_sync && !rdev->faulty)
296 md_super_write(mddev, rdev,
297 (rdev->sb_offset<<1) + offset
298 + page->index * (PAGE_SIZE/512),
303 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
308 * write out a page to a file
310 static int write_page(struct bitmap *bitmap, struct page *page, int wait)
314 if (bitmap->file == NULL)
315 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
320 if (TestSetPageLocked(page))
321 return -EAGAIN; /* already locked */
322 if (PageWriteback(page)) {
328 ret = page->mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
330 ret = page->mapping->a_ops->commit_write(NULL, page, 0,
337 set_page_dirty(page); /* force it to be written out */
340 /* add to list to be waited for by daemon */
341 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
343 page_cache_get(page);
344 spin_lock(&bitmap->write_lock);
345 list_add(&item->list, &bitmap->complete_pages);
346 spin_unlock(&bitmap->write_lock);
347 md_wakeup_thread(bitmap->writeback_daemon);
349 return write_one_page(page, wait);
352 /* read a page from a file, pinning it into cache, and return bytes_read */
353 static struct page *read_page(struct file *file, unsigned long index,
354 unsigned long *bytes_read)
356 struct inode *inode = file->f_mapping->host;
357 struct page *page = NULL;
358 loff_t isize = i_size_read(inode);
359 unsigned long end_index = isize >> PAGE_CACHE_SHIFT;
361 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_CACHE_SIZE,
362 (unsigned long long)index << PAGE_CACHE_SHIFT);
364 page = read_cache_page(inode->i_mapping, index,
365 (filler_t *)inode->i_mapping->a_ops->readpage, file);
368 wait_on_page_locked(page);
369 if (!PageUptodate(page) || PageError(page)) {
370 page_cache_release(page);
371 page = ERR_PTR(-EIO);
375 if (index > end_index) /* we have read beyond EOF */
377 else if (index == end_index) /* possible short read */
378 *bytes_read = isize & ~PAGE_CACHE_MASK;
380 *bytes_read = PAGE_CACHE_SIZE; /* got a full page */
383 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
384 (int)PAGE_CACHE_SIZE,
385 (unsigned long long)index << PAGE_CACHE_SHIFT,
391 * bitmap file superblock operations
394 /* update the event counter and sync the superblock to disk */
395 int bitmap_update_sb(struct bitmap *bitmap)
400 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
402 spin_lock_irqsave(&bitmap->lock, flags);
403 if (!bitmap->sb_page) { /* no superblock */
404 spin_unlock_irqrestore(&bitmap->lock, flags);
407 spin_unlock_irqrestore(&bitmap->lock, flags);
408 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
409 sb->events = cpu_to_le64(bitmap->mddev->events);
410 if (!bitmap->mddev->degraded)
411 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
412 kunmap(bitmap->sb_page);
413 return write_page(bitmap, bitmap->sb_page, 1);
416 /* print out the bitmap file superblock */
417 void bitmap_print_sb(struct bitmap *bitmap)
421 if (!bitmap || !bitmap->sb_page)
423 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
424 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
425 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
426 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
427 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
428 *(__u32 *)(sb->uuid+0),
429 *(__u32 *)(sb->uuid+4),
430 *(__u32 *)(sb->uuid+8),
431 *(__u32 *)(sb->uuid+12));
432 printk(KERN_DEBUG " events: %llu\n",
433 (unsigned long long) le64_to_cpu(sb->events));
434 printk(KERN_DEBUG "events cleared: %llu\n",
435 (unsigned long long) le64_to_cpu(sb->events_cleared));
436 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
437 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
438 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
439 printk(KERN_DEBUG " sync size: %llu KB\n",
440 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
441 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
442 kunmap(bitmap->sb_page);
445 /* read the superblock from the bitmap file and initialize some bitmap fields */
446 static int bitmap_read_sb(struct bitmap *bitmap)
450 unsigned long chunksize, daemon_sleep, write_behind;
451 unsigned long bytes_read;
452 unsigned long long events;
455 /* page 0 is the superblock, read it... */
457 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
459 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
460 bytes_read = PAGE_SIZE;
462 if (IS_ERR(bitmap->sb_page)) {
463 err = PTR_ERR(bitmap->sb_page);
464 bitmap->sb_page = NULL;
468 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
470 if (bytes_read < sizeof(*sb)) { /* short read */
471 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
477 chunksize = le32_to_cpu(sb->chunksize);
478 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
479 write_behind = le32_to_cpu(sb->write_behind);
481 /* verify that the bitmap-specific fields are valid */
482 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
483 reason = "bad magic";
484 else if (sb->version != cpu_to_le32(BITMAP_MAJOR))
485 reason = "unrecognized superblock version";
486 else if (chunksize < 512 || chunksize > (1024 * 1024 * 4))
487 reason = "bitmap chunksize out of range (512B - 4MB)";
488 else if ((1 << ffz(~chunksize)) != chunksize)
489 reason = "bitmap chunksize not a power of 2";
490 else if (daemon_sleep < 1 || daemon_sleep > 15)
491 reason = "daemon sleep period out of range (1-15s)";
492 else if (write_behind > COUNTER_MAX)
493 reason = "write-behind limit out of range (0 - 16383)";
495 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
496 bmname(bitmap), reason);
500 /* keep the array size field of the bitmap superblock up to date */
501 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
503 if (!bitmap->mddev->persistent)
507 * if we have a persistent array superblock, compare the
508 * bitmap's UUID and event counter to the mddev's
510 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
511 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
515 events = le64_to_cpu(sb->events);
516 if (events < bitmap->mddev->events) {
517 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
518 "-- forcing full recovery\n", bmname(bitmap), events,
519 (unsigned long long) bitmap->mddev->events);
520 sb->state |= BITMAP_STALE;
523 /* assign fields using values from superblock */
524 bitmap->chunksize = chunksize;
525 bitmap->daemon_sleep = daemon_sleep;
526 bitmap->daemon_lastrun = jiffies;
527 bitmap->max_write_behind = write_behind;
528 bitmap->flags |= sb->state;
529 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
530 if (sb->state & BITMAP_STALE)
531 bitmap->events_cleared = bitmap->mddev->events;
534 kunmap(bitmap->sb_page);
536 bitmap_print_sb(bitmap);
540 enum bitmap_mask_op {
545 /* record the state of the bitmap in the superblock */
546 static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
547 enum bitmap_mask_op op)
552 spin_lock_irqsave(&bitmap->lock, flags);
553 if (!bitmap || !bitmap->sb_page) { /* can't set the state */
554 spin_unlock_irqrestore(&bitmap->lock, flags);
557 page_cache_get(bitmap->sb_page);
558 spin_unlock_irqrestore(&bitmap->lock, flags);
559 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
561 case MASK_SET: sb->state |= bits;
563 case MASK_UNSET: sb->state &= ~bits;
567 kunmap(bitmap->sb_page);
568 page_cache_release(bitmap->sb_page);
572 * general bitmap file operations
575 /* calculate the index of the page that contains this bit */
576 static inline unsigned long file_page_index(unsigned long chunk)
578 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
581 /* calculate the (bit) offset of this bit within a page */
582 static inline unsigned long file_page_offset(unsigned long chunk)
584 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
588 * return a pointer to the page in the filemap that contains the given bit
590 * this lookup is complicated by the fact that the bitmap sb might be exactly
591 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
594 static inline struct page *filemap_get_page(struct bitmap *bitmap,
597 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
601 static void bitmap_file_unmap(struct bitmap *bitmap)
603 struct page **map, *sb_page;
608 spin_lock_irqsave(&bitmap->lock, flags);
609 map = bitmap->filemap;
610 bitmap->filemap = NULL;
611 attr = bitmap->filemap_attr;
612 bitmap->filemap_attr = NULL;
613 pages = bitmap->file_pages;
614 bitmap->file_pages = 0;
615 sb_page = bitmap->sb_page;
616 bitmap->sb_page = NULL;
617 spin_unlock_irqrestore(&bitmap->lock, flags);
620 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
621 page_cache_release(map[pages]);
626 page_cache_release(sb_page);
629 static void bitmap_stop_daemon(struct bitmap *bitmap);
631 /* dequeue the next item in a page list -- don't call from irq context */
632 static struct page_list *dequeue_page(struct bitmap *bitmap)
634 struct page_list *item = NULL;
635 struct list_head *head = &bitmap->complete_pages;
637 spin_lock(&bitmap->write_lock);
638 if (list_empty(head))
640 item = list_entry(head->prev, struct page_list, list);
641 list_del(head->prev);
643 spin_unlock(&bitmap->write_lock);
647 static void drain_write_queues(struct bitmap *bitmap)
649 struct page_list *item;
651 while ((item = dequeue_page(bitmap))) {
652 /* don't bother to wait */
653 page_cache_release(item->page);
654 mempool_free(item, bitmap->write_pool);
657 wake_up(&bitmap->write_wait);
660 static void bitmap_file_put(struct bitmap *bitmap)
666 spin_lock_irqsave(&bitmap->lock, flags);
669 spin_unlock_irqrestore(&bitmap->lock, flags);
671 bitmap_stop_daemon(bitmap);
673 drain_write_queues(bitmap);
675 bitmap_file_unmap(bitmap);
678 inode = file->f_mapping->host;
679 spin_lock(&inode->i_lock);
680 atomic_set(&inode->i_writecount, 1); /* allow writes again */
681 spin_unlock(&inode->i_lock);
688 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
689 * then it is no longer reliable, so we stop using it and we mark the file
690 * as failed in the superblock
692 static void bitmap_file_kick(struct bitmap *bitmap)
694 char *path, *ptr = NULL;
696 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
697 bitmap_update_sb(bitmap);
700 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
702 ptr = file_path(bitmap->file, path, PAGE_SIZE);
704 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
705 bmname(bitmap), ptr ? ptr : "");
710 bitmap_file_put(bitmap);
715 enum bitmap_page_attr {
716 BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
717 BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
718 BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
721 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
722 enum bitmap_page_attr attr)
724 bitmap->filemap_attr[page->index] |= attr;
727 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
728 enum bitmap_page_attr attr)
730 bitmap->filemap_attr[page->index] &= ~attr;
733 static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
735 return bitmap->filemap_attr[page->index];
739 * bitmap_file_set_bit -- called before performing a write to the md device
740 * to set (and eventually sync) a particular bit in the bitmap file
742 * we set the bit immediately, then we record the page number so that
743 * when an unplug occurs, we can flush the dirty pages out to disk
745 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
750 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
752 if (!bitmap->filemap) {
756 page = filemap_get_page(bitmap, chunk);
757 bit = file_page_offset(chunk);
760 /* make sure the page stays cached until it gets written out */
761 if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
762 page_cache_get(page);
765 kaddr = kmap_atomic(page, KM_USER0);
767 kunmap_atomic(kaddr, KM_USER0);
768 PRINTK("set file bit %lu page %lu\n", bit, page->index);
770 /* record page number so it gets flushed to disk when unplug occurs */
771 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
775 /* this gets called when the md device is ready to unplug its underlying
776 * (slave) device queues -- before we let any writes go down, we need to
777 * sync the dirty pages of the bitmap file to disk */
778 int bitmap_unplug(struct bitmap *bitmap)
780 unsigned long i, attr, flags;
788 /* look at each page to see if there are any set bits that need to be
789 * flushed out to disk */
790 for (i = 0; i < bitmap->file_pages; i++) {
791 spin_lock_irqsave(&bitmap->lock, flags);
792 if (!bitmap->filemap) {
793 spin_unlock_irqrestore(&bitmap->lock, flags);
796 page = bitmap->filemap[i];
797 attr = get_page_attr(bitmap, page);
798 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
799 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
800 if ((attr & BITMAP_PAGE_DIRTY))
802 spin_unlock_irqrestore(&bitmap->lock, flags);
804 if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE)) {
805 err = write_page(bitmap, page, 0);
806 if (err == -EAGAIN) {
807 if (attr & BITMAP_PAGE_DIRTY)
808 err = write_page(bitmap, page, 1);
816 if (wait) { /* if any writes were performed, we need to wait on them */
818 spin_lock_irq(&bitmap->write_lock);
819 wait_event_lock_irq(bitmap->write_wait,
820 list_empty(&bitmap->complete_pages), bitmap->write_lock,
821 wake_up_process(bitmap->writeback_daemon->tsk));
822 spin_unlock_irq(&bitmap->write_lock);
824 wait_event(bitmap->mddev->sb_wait,
825 atomic_read(&bitmap->mddev->pending_writes)==0);
830 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
831 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
832 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
833 * memory mapping of the bitmap file
835 * if there's no bitmap file, or if the bitmap file had been
836 * previously kicked from the array, we mark all the bits as
837 * 1's in order to cause a full resync.
839 * We ignore all bits for sectors that end earlier than 'start'.
840 * This is used when reading an out-of-date bitmap...
842 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
844 unsigned long i, chunks, index, oldindex, bit;
845 struct page *page = NULL, *oldpage = NULL;
846 unsigned long num_pages, bit_cnt = 0;
848 unsigned long bytes, offset, dummy;
852 chunks = bitmap->chunks;
855 BUG_ON(!file && !bitmap->offset);
857 #ifdef INJECT_FAULTS_3
860 outofdate = bitmap->flags & BITMAP_STALE;
863 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
864 "recovery\n", bmname(bitmap));
866 bytes = (chunks + 7) / 8;
868 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
870 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
871 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
873 (unsigned long) i_size_read(file->f_mapping->host),
874 bytes + sizeof(bitmap_super_t));
880 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
881 if (!bitmap->filemap)
884 bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL);
885 if (!bitmap->filemap_attr)
888 memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages);
892 for (i = 0; i < chunks; i++) {
893 index = file_page_index(i);
894 bit = file_page_offset(i);
895 if (index != oldindex) { /* this is a new page, read it in */
896 /* unmap the old page, we're done with it */
901 * if we're here then the superblock page
902 * contains some bits (PAGE_SIZE != sizeof sb)
903 * we've already read it in, so just use it
905 page = bitmap->sb_page;
906 offset = sizeof(bitmap_super_t);
908 page = read_page(file, index, &dummy);
911 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
914 if (IS_ERR(page)) { /* read error */
925 * if bitmap is out of date, dirty the
926 * whole page and write it out
928 memset(page_address(page) + offset, 0xff,
930 ret = write_page(bitmap, page, 1);
933 /* release, page not in filemap yet */
934 page_cache_release(page);
939 bitmap->filemap[bitmap->file_pages++] = page;
941 if (test_bit(bit, page_address(page))) {
942 /* if the disk bit is set, set the memory bit */
943 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
944 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
947 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
951 /* everything went OK */
953 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
955 if (page) /* unmap the last page */
958 if (bit_cnt) { /* Kick recovery if any bits were set */
959 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
960 md_wakeup_thread(bitmap->mddev->thread);
964 printk(KERN_INFO "%s: bitmap initialized from disk: "
965 "read %lu/%lu pages, set %lu bits, status: %d\n",
966 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
971 void bitmap_write_all(struct bitmap *bitmap)
973 /* We don't actually write all bitmap blocks here,
974 * just flag them as needing to be written
977 unsigned long chunks = bitmap->chunks;
978 unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t);
979 unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE;
981 bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE;
985 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
987 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
988 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
989 bitmap->bp[page].count += inc;
991 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
992 (unsigned long long)offset, inc, bitmap->bp[page].count);
994 bitmap_checkfree(bitmap, page);
996 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
997 sector_t offset, int *blocks,
1001 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1005 int bitmap_daemon_work(struct bitmap *bitmap)
1008 unsigned long flags;
1009 struct page *page = NULL, *lastpage = NULL;
1016 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1018 bitmap->daemon_lastrun = jiffies;
1020 for (j = 0; j < bitmap->chunks; j++) {
1021 bitmap_counter_t *bmc;
1022 spin_lock_irqsave(&bitmap->lock, flags);
1023 if (!bitmap->filemap) {
1024 /* error or shutdown */
1025 spin_unlock_irqrestore(&bitmap->lock, flags);
1029 page = filemap_get_page(bitmap, j);
1031 if (page != lastpage) {
1032 /* skip this page unless it's marked as needing cleaning */
1033 if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
1034 if (attr & BITMAP_PAGE_NEEDWRITE) {
1035 page_cache_get(page);
1036 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1038 spin_unlock_irqrestore(&bitmap->lock, flags);
1039 if (attr & BITMAP_PAGE_NEEDWRITE) {
1040 switch (write_page(bitmap, page, 0)) {
1042 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1047 bitmap_file_kick(bitmap);
1049 page_cache_release(page);
1054 /* grab the new page, sync and release the old */
1055 page_cache_get(page);
1056 if (lastpage != NULL) {
1057 if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
1058 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1059 spin_unlock_irqrestore(&bitmap->lock, flags);
1060 err = write_page(bitmap, lastpage, 0);
1061 if (err == -EAGAIN) {
1063 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1066 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1067 spin_unlock_irqrestore(&bitmap->lock, flags);
1070 page_cache_release(lastpage);
1072 bitmap_file_kick(bitmap);
1074 spin_unlock_irqrestore(&bitmap->lock, flags);
1078 printk("bitmap clean at page %lu\n", j);
1080 spin_lock_irqsave(&bitmap->lock, flags);
1081 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1083 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1087 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1090 *bmc=1; /* maybe clear the bit next time */
1091 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1092 } else if (*bmc == 1) {
1093 /* we can clear the bit */
1095 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1099 clear_bit(file_page_offset(j), page_address(page));
1102 spin_unlock_irqrestore(&bitmap->lock, flags);
1105 /* now sync the final page */
1106 if (lastpage != NULL) {
1108 spin_lock_irqsave(&bitmap->lock, flags);
1109 if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
1110 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1111 spin_unlock_irqrestore(&bitmap->lock, flags);
1112 err = write_page(bitmap, lastpage, 0);
1113 if (err == -EAGAIN) {
1114 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1118 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1119 spin_unlock_irqrestore(&bitmap->lock, flags);
1122 page_cache_release(lastpage);
1128 static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
1131 unsigned long flags;
1133 /* if no one is waiting on us, we'll free the md thread struct
1134 * and exit, otherwise we let the waiter clean things up */
1135 spin_lock_irqsave(&bitmap->lock, flags);
1136 if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
1138 spin_unlock_irqrestore(&bitmap->lock, flags);
1140 complete_and_exit(NULL, 0); /* do_exit not exported */
1142 spin_unlock_irqrestore(&bitmap->lock, flags);
1145 static void bitmap_writeback_daemon(mddev_t *mddev)
1147 struct bitmap *bitmap = mddev->bitmap;
1149 struct page_list *item;
1152 if (signal_pending(current)) {
1154 "%s: bitmap writeback daemon got signal, exiting...\n",
1160 /* about to be stopped. */
1163 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1164 /* wait on bitmap page writebacks */
1165 while ((item = dequeue_page(bitmap))) {
1167 mempool_free(item, bitmap->write_pool);
1168 PRINTK("wait on page writeback: %p\n", page);
1169 wait_on_page_writeback(page);
1170 PRINTK("finished page writeback: %p\n", page);
1172 err = PageError(page);
1173 page_cache_release(page);
1175 printk(KERN_WARNING "%s: bitmap file writeback "
1176 "failed (page %lu): %d\n",
1177 bmname(bitmap), page->index, err);
1178 bitmap_file_kick(bitmap);
1183 wake_up(&bitmap->write_wait);
1185 printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
1186 bmname(bitmap), err);
1187 daemon_exit(bitmap, &bitmap->writeback_daemon);
1191 static mdk_thread_t *bitmap_start_daemon(struct bitmap *bitmap,
1192 void (*func)(mddev_t *), char *name)
1194 mdk_thread_t *daemon;
1197 #ifdef INJECT_FATAL_FAULT_2
1200 sprintf(namebuf, "%%s_%s", name);
1201 daemon = md_register_thread(func, bitmap->mddev, namebuf);
1204 printk(KERN_ERR "%s: failed to start bitmap daemon\n",
1206 return ERR_PTR(-ECHILD);
1209 md_wakeup_thread(daemon); /* start it running */
1211 PRINTK("%s: %s daemon (pid %d) started...\n",
1212 bmname(bitmap), name, daemon->tsk->pid);
1217 static void bitmap_stop_daemon(struct bitmap *bitmap)
1219 /* the daemon can't stop itself... it'll just exit instead... */
1220 if (bitmap->writeback_daemon && ! IS_ERR(bitmap->writeback_daemon) &&
1221 current->pid != bitmap->writeback_daemon->tsk->pid) {
1222 mdk_thread_t *daemon;
1223 unsigned long flags;
1225 spin_lock_irqsave(&bitmap->lock, flags);
1226 daemon = bitmap->writeback_daemon;
1227 bitmap->writeback_daemon = NULL;
1228 spin_unlock_irqrestore(&bitmap->lock, flags);
1229 if (daemon && ! IS_ERR(daemon))
1230 md_unregister_thread(daemon); /* destroy the thread */
1234 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1235 sector_t offset, int *blocks,
1238 /* If 'create', we might release the lock and reclaim it.
1239 * The lock must have been taken with interrupts enabled.
1240 * If !create, we don't release the lock.
1242 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1243 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1244 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1247 if (bitmap_checkpage(bitmap, page, create) < 0) {
1248 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1249 *blocks = csize - (offset & (csize- 1));
1252 /* now locked ... */
1254 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1255 /* should we use the first or second counter field
1256 * of the hijacked pointer? */
1257 int hi = (pageoff > PAGE_COUNTER_MASK);
1258 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1259 PAGE_COUNTER_SHIFT - 1);
1260 *blocks = csize - (offset & (csize- 1));
1261 return &((bitmap_counter_t *)
1262 &bitmap->bp[page].map)[hi];
1263 } else { /* page is allocated */
1264 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1265 *blocks = csize - (offset & (csize- 1));
1266 return (bitmap_counter_t *)
1267 &(bitmap->bp[page].map[pageoff]);
1271 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1273 if (!bitmap) return 0;
1276 atomic_inc(&bitmap->behind_writes);
1277 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1278 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1283 bitmap_counter_t *bmc;
1285 spin_lock_irq(&bitmap->lock);
1286 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1288 spin_unlock_irq(&bitmap->lock);
1294 bitmap_file_set_bit(bitmap, offset);
1295 bitmap_count_page(bitmap,offset, 1);
1296 blk_plug_device(bitmap->mddev->queue);
1301 if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
1304 spin_unlock_irq(&bitmap->lock);
1307 if (sectors > blocks)
1314 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1315 int success, int behind)
1317 if (!bitmap) return;
1319 atomic_dec(&bitmap->behind_writes);
1320 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1321 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1326 unsigned long flags;
1327 bitmap_counter_t *bmc;
1329 spin_lock_irqsave(&bitmap->lock, flags);
1330 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1332 spin_unlock_irqrestore(&bitmap->lock, flags);
1336 if (!success && ! (*bmc & NEEDED_MASK))
1337 *bmc |= NEEDED_MASK;
1341 set_page_attr(bitmap,
1342 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1345 spin_unlock_irqrestore(&bitmap->lock, flags);
1347 if (sectors > blocks)
1353 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1356 bitmap_counter_t *bmc;
1358 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1360 return 1; /* always resync if no bitmap */
1362 spin_lock_irq(&bitmap->lock);
1363 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1369 else if (NEEDED(*bmc)) {
1371 if (!degraded) { /* don't set/clear bits if degraded */
1372 *bmc |= RESYNC_MASK;
1373 *bmc &= ~NEEDED_MASK;
1377 spin_unlock_irq(&bitmap->lock);
1381 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1383 bitmap_counter_t *bmc;
1384 unsigned long flags;
1386 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1387 */ if (bitmap == NULL) {
1391 spin_lock_irqsave(&bitmap->lock, flags);
1392 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1397 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1400 *bmc &= ~RESYNC_MASK;
1402 if (!NEEDED(*bmc) && aborted)
1403 *bmc |= NEEDED_MASK;
1406 set_page_attr(bitmap,
1407 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1413 spin_unlock_irqrestore(&bitmap->lock, flags);
1416 void bitmap_close_sync(struct bitmap *bitmap)
1418 /* Sync has finished, and any bitmap chunks that weren't synced
1419 * properly have been aborted. It remains to us to clear the
1420 * RESYNC bit wherever it is still on
1422 sector_t sector = 0;
1424 if (!bitmap) return;
1425 while (sector < bitmap->mddev->resync_max_sectors) {
1426 bitmap_end_sync(bitmap, sector, &blocks, 0);
1428 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1429 (unsigned long long)sector, blocks);
1430 */ sector += blocks;
1434 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1436 /* For each chunk covered by any of these sectors, set the
1437 * counter to 1 and set resync_needed. They should all
1438 * be 0 at this point
1442 bitmap_counter_t *bmc;
1443 spin_lock_irq(&bitmap->lock);
1444 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1446 spin_unlock_irq(&bitmap->lock);
1451 *bmc = 1 | (needed?NEEDED_MASK:0);
1452 bitmap_count_page(bitmap, offset, 1);
1453 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1454 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1456 spin_unlock_irq(&bitmap->lock);
1461 * flush out any pending updates
1463 void bitmap_flush(mddev_t *mddev)
1465 struct bitmap *bitmap = mddev->bitmap;
1468 if (!bitmap) /* there was no bitmap */
1471 /* run the daemon_work three time to ensure everything is flushed
1474 sleep = bitmap->daemon_sleep;
1475 bitmap->daemon_sleep = 0;
1476 bitmap_daemon_work(bitmap);
1477 bitmap_daemon_work(bitmap);
1478 bitmap_daemon_work(bitmap);
1479 bitmap->daemon_sleep = sleep;
1480 bitmap_update_sb(bitmap);
1484 * free memory that was allocated
1486 static void bitmap_free(struct bitmap *bitmap)
1488 unsigned long k, pages;
1489 struct bitmap_page *bp;
1491 if (!bitmap) /* there was no bitmap */
1494 /* release the bitmap file and kill the daemon */
1495 bitmap_file_put(bitmap);
1498 pages = bitmap->pages;
1500 /* free all allocated memory */
1502 mempool_destroy(bitmap->write_pool);
1504 if (bp) /* deallocate the page memory */
1505 for (k = 0; k < pages; k++)
1506 if (bp[k].map && !bp[k].hijacked)
1511 void bitmap_destroy(mddev_t *mddev)
1513 struct bitmap *bitmap = mddev->bitmap;
1515 if (!bitmap) /* there was no bitmap */
1518 mddev->bitmap = NULL; /* disconnect from the md device */
1520 bitmap_free(bitmap);
1524 * initialize the bitmap structure
1525 * if this returns an error, bitmap_destroy must be called to do clean up
1527 int bitmap_create(mddev_t *mddev)
1529 struct bitmap *bitmap;
1530 unsigned long blocks = mddev->resync_max_sectors;
1531 unsigned long chunks;
1532 unsigned long pages;
1533 struct file *file = mddev->bitmap_file;
1537 BUG_ON(sizeof(bitmap_super_t) != 256);
1539 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
1542 BUG_ON(file && mddev->bitmap_offset);
1544 bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
1548 memset(bitmap, 0, sizeof(*bitmap));
1550 spin_lock_init(&bitmap->lock);
1551 bitmap->mddev = mddev;
1553 spin_lock_init(&bitmap->write_lock);
1554 INIT_LIST_HEAD(&bitmap->complete_pages);
1555 init_waitqueue_head(&bitmap->write_wait);
1556 bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
1557 write_pool_free, NULL);
1559 if (!bitmap->write_pool)
1562 bitmap->file = file;
1563 bitmap->offset = mddev->bitmap_offset;
1564 if (file) get_file(file);
1565 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1566 err = bitmap_read_sb(bitmap);
1570 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1571 sizeof(bitmap->chunksize));
1573 /* now that chunksize and chunkshift are set, we can use these macros */
1574 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1575 CHUNK_BLOCK_RATIO(bitmap);
1576 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1580 bitmap->chunks = chunks;
1581 bitmap->pages = pages;
1582 bitmap->missing_pages = pages;
1583 bitmap->counter_bits = COUNTER_BITS;
1585 bitmap->syncchunk = ~0UL;
1587 #ifdef INJECT_FATAL_FAULT_1
1590 bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1595 memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp));
1597 bitmap->flags |= BITMAP_ACTIVE;
1599 /* now that we have some pages available, initialize the in-memory
1600 * bitmap from the on-disk bitmap */
1602 if (mddev->degraded == 0
1603 || bitmap->events_cleared == mddev->events)
1604 /* no need to keep dirty bits to optimise a re-add of a missing device */
1605 start = mddev->recovery_cp;
1606 err = bitmap_init_from_disk(bitmap, start);
1611 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1612 pages, bmname(bitmap));
1614 mddev->bitmap = bitmap;
1617 /* kick off the bitmap writeback daemon */
1618 bitmap->writeback_daemon =
1619 bitmap_start_daemon(bitmap,
1620 bitmap_writeback_daemon,
1623 if (IS_ERR(bitmap->writeback_daemon))
1624 return PTR_ERR(bitmap->writeback_daemon);
1625 return bitmap_update_sb(bitmap);
1628 bitmap_free(bitmap);
1632 /* the bitmap API -- for raid personalities */
1633 EXPORT_SYMBOL(bitmap_startwrite);
1634 EXPORT_SYMBOL(bitmap_endwrite);
1635 EXPORT_SYMBOL(bitmap_start_sync);
1636 EXPORT_SYMBOL(bitmap_end_sync);
1637 EXPORT_SYMBOL(bitmap_unplug);
1638 EXPORT_SYMBOL(bitmap_close_sync);
1639 EXPORT_SYMBOL(bitmap_daemon_work);