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(unsigned int 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)
277 return ERR_PTR(-EIO);
280 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
282 } while (!sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ));
288 static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
291 struct list_head *tmp;
293 ITERATE_RDEV(mddev, rdev, tmp)
294 if (rdev->in_sync && !rdev->faulty)
295 md_super_write(mddev, rdev,
296 (rdev->sb_offset<<1) + offset
297 + page->index * (PAGE_SIZE/512),
302 wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
307 * write out a page to a file
309 static int write_page(struct bitmap *bitmap, struct page *page, int wait)
313 if (bitmap->file == NULL)
314 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
319 if (TestSetPageLocked(page))
320 return -EAGAIN; /* already locked */
321 if (PageWriteback(page)) {
327 ret = page->mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
329 ret = page->mapping->a_ops->commit_write(NULL, page, 0,
336 set_page_dirty(page); /* force it to be written out */
339 /* add to list to be waited for by daemon */
340 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
342 page_cache_get(page);
343 spin_lock(&bitmap->write_lock);
344 list_add(&item->list, &bitmap->complete_pages);
345 spin_unlock(&bitmap->write_lock);
346 md_wakeup_thread(bitmap->writeback_daemon);
348 return write_one_page(page, wait);
351 /* read a page from a file, pinning it into cache, and return bytes_read */
352 static struct page *read_page(struct file *file, unsigned long index,
353 unsigned long *bytes_read)
355 struct inode *inode = file->f_mapping->host;
356 struct page *page = NULL;
357 loff_t isize = i_size_read(inode);
358 unsigned long end_index = isize >> PAGE_CACHE_SHIFT;
360 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_CACHE_SIZE,
361 (unsigned long long)index << PAGE_CACHE_SHIFT);
363 page = read_cache_page(inode->i_mapping, index,
364 (filler_t *)inode->i_mapping->a_ops->readpage, file);
367 wait_on_page_locked(page);
368 if (!PageUptodate(page) || PageError(page)) {
369 page_cache_release(page);
370 page = ERR_PTR(-EIO);
374 if (index > end_index) /* we have read beyond EOF */
376 else if (index == end_index) /* possible short read */
377 *bytes_read = isize & ~PAGE_CACHE_MASK;
379 *bytes_read = PAGE_CACHE_SIZE; /* got a full page */
382 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
383 (int)PAGE_CACHE_SIZE,
384 (unsigned long long)index << PAGE_CACHE_SHIFT,
390 * bitmap file superblock operations
393 /* update the event counter and sync the superblock to disk */
394 int bitmap_update_sb(struct bitmap *bitmap)
399 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
401 spin_lock_irqsave(&bitmap->lock, flags);
402 if (!bitmap->sb_page) { /* no superblock */
403 spin_unlock_irqrestore(&bitmap->lock, flags);
406 spin_unlock_irqrestore(&bitmap->lock, flags);
407 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
408 sb->events = cpu_to_le64(bitmap->mddev->events);
409 if (!bitmap->mddev->degraded)
410 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
411 kunmap(bitmap->sb_page);
412 return write_page(bitmap, bitmap->sb_page, 1);
415 /* print out the bitmap file superblock */
416 void bitmap_print_sb(struct bitmap *bitmap)
420 if (!bitmap || !bitmap->sb_page)
422 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
423 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
424 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
425 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
426 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
427 *(__u32 *)(sb->uuid+0),
428 *(__u32 *)(sb->uuid+4),
429 *(__u32 *)(sb->uuid+8),
430 *(__u32 *)(sb->uuid+12));
431 printk(KERN_DEBUG " events: %llu\n",
432 (unsigned long long) le64_to_cpu(sb->events));
433 printk(KERN_DEBUG "events cleared: %llu\n",
434 (unsigned long long) le64_to_cpu(sb->events_cleared));
435 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
436 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
437 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
438 printk(KERN_DEBUG " sync size: %llu KB\n",
439 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
440 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
441 kunmap(bitmap->sb_page);
444 /* read the superblock from the bitmap file and initialize some bitmap fields */
445 static int bitmap_read_sb(struct bitmap *bitmap)
449 unsigned long chunksize, daemon_sleep, write_behind;
450 unsigned long bytes_read;
451 unsigned long long events;
454 /* page 0 is the superblock, read it... */
456 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
458 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
459 bytes_read = PAGE_SIZE;
461 if (IS_ERR(bitmap->sb_page)) {
462 err = PTR_ERR(bitmap->sb_page);
463 bitmap->sb_page = NULL;
467 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
469 if (bytes_read < sizeof(*sb)) { /* short read */
470 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
476 chunksize = le32_to_cpu(sb->chunksize);
477 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
478 write_behind = le32_to_cpu(sb->write_behind);
480 /* verify that the bitmap-specific fields are valid */
481 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
482 reason = "bad magic";
483 else if (sb->version != cpu_to_le32(BITMAP_MAJOR))
484 reason = "unrecognized superblock version";
485 else if (chunksize < 512 || chunksize > (1024 * 1024 * 4))
486 reason = "bitmap chunksize out of range (512B - 4MB)";
487 else if ((1 << ffz(~chunksize)) != chunksize)
488 reason = "bitmap chunksize not a power of 2";
489 else if (daemon_sleep < 1 || daemon_sleep > 15)
490 reason = "daemon sleep period out of range (1-15s)";
491 else if (write_behind > COUNTER_MAX)
492 reason = "write-behind limit out of range (0 - 16383)";
494 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
495 bmname(bitmap), reason);
499 /* keep the array size field of the bitmap superblock up to date */
500 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
502 if (!bitmap->mddev->persistent)
506 * if we have a persistent array superblock, compare the
507 * bitmap's UUID and event counter to the mddev's
509 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
510 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
514 events = le64_to_cpu(sb->events);
515 if (events < bitmap->mddev->events) {
516 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
517 "-- forcing full recovery\n", bmname(bitmap), events,
518 (unsigned long long) bitmap->mddev->events);
519 sb->state |= BITMAP_STALE;
522 /* assign fields using values from superblock */
523 bitmap->chunksize = chunksize;
524 bitmap->daemon_sleep = daemon_sleep;
525 bitmap->daemon_lastrun = jiffies;
526 bitmap->max_write_behind = write_behind;
527 bitmap->flags |= sb->state;
528 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
529 if (sb->state & BITMAP_STALE)
530 bitmap->events_cleared = bitmap->mddev->events;
533 kunmap(bitmap->sb_page);
535 bitmap_print_sb(bitmap);
539 enum bitmap_mask_op {
544 /* record the state of the bitmap in the superblock */
545 static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
546 enum bitmap_mask_op op)
551 spin_lock_irqsave(&bitmap->lock, flags);
552 if (!bitmap || !bitmap->sb_page) { /* can't set the state */
553 spin_unlock_irqrestore(&bitmap->lock, flags);
556 page_cache_get(bitmap->sb_page);
557 spin_unlock_irqrestore(&bitmap->lock, flags);
558 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
560 case MASK_SET: sb->state |= bits;
562 case MASK_UNSET: sb->state &= ~bits;
566 kunmap(bitmap->sb_page);
567 page_cache_release(bitmap->sb_page);
571 * general bitmap file operations
574 /* calculate the index of the page that contains this bit */
575 static inline unsigned long file_page_index(unsigned long chunk)
577 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
580 /* calculate the (bit) offset of this bit within a page */
581 static inline unsigned long file_page_offset(unsigned long chunk)
583 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
587 * return a pointer to the page in the filemap that contains the given bit
589 * this lookup is complicated by the fact that the bitmap sb might be exactly
590 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
593 static inline struct page *filemap_get_page(struct bitmap *bitmap,
596 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
600 static void bitmap_file_unmap(struct bitmap *bitmap)
602 struct page **map, *sb_page;
607 spin_lock_irqsave(&bitmap->lock, flags);
608 map = bitmap->filemap;
609 bitmap->filemap = NULL;
610 attr = bitmap->filemap_attr;
611 bitmap->filemap_attr = NULL;
612 pages = bitmap->file_pages;
613 bitmap->file_pages = 0;
614 sb_page = bitmap->sb_page;
615 bitmap->sb_page = NULL;
616 spin_unlock_irqrestore(&bitmap->lock, flags);
619 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
620 page_cache_release(map[pages]);
625 page_cache_release(sb_page);
628 static void bitmap_stop_daemons(struct bitmap *bitmap);
630 /* dequeue the next item in a page list -- don't call from irq context */
631 static struct page_list *dequeue_page(struct bitmap *bitmap)
633 struct page_list *item = NULL;
634 struct list_head *head = &bitmap->complete_pages;
636 spin_lock(&bitmap->write_lock);
637 if (list_empty(head))
639 item = list_entry(head->prev, struct page_list, list);
640 list_del(head->prev);
642 spin_unlock(&bitmap->write_lock);
646 static void drain_write_queues(struct bitmap *bitmap)
648 struct page_list *item;
650 while ((item = dequeue_page(bitmap))) {
651 /* don't bother to wait */
652 page_cache_release(item->page);
653 mempool_free(item, bitmap->write_pool);
656 wake_up(&bitmap->write_wait);
659 static void bitmap_file_put(struct bitmap *bitmap)
665 spin_lock_irqsave(&bitmap->lock, flags);
668 spin_unlock_irqrestore(&bitmap->lock, flags);
670 bitmap_stop_daemons(bitmap);
672 drain_write_queues(bitmap);
674 bitmap_file_unmap(bitmap);
677 inode = file->f_mapping->host;
678 spin_lock(&inode->i_lock);
679 atomic_set(&inode->i_writecount, 1); /* allow writes again */
680 spin_unlock(&inode->i_lock);
687 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
688 * then it is no longer reliable, so we stop using it and we mark the file
689 * as failed in the superblock
691 static void bitmap_file_kick(struct bitmap *bitmap)
693 char *path, *ptr = NULL;
695 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
696 bitmap_update_sb(bitmap);
699 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
701 ptr = file_path(bitmap->file, path, PAGE_SIZE);
703 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
704 bmname(bitmap), ptr ? ptr : "");
709 bitmap_file_put(bitmap);
714 enum bitmap_page_attr {
715 BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
716 BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
717 BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
720 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
721 enum bitmap_page_attr attr)
723 bitmap->filemap_attr[page->index] |= attr;
726 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
727 enum bitmap_page_attr attr)
729 bitmap->filemap_attr[page->index] &= ~attr;
732 static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
734 return bitmap->filemap_attr[page->index];
738 * bitmap_file_set_bit -- called before performing a write to the md device
739 * to set (and eventually sync) a particular bit in the bitmap file
741 * we set the bit immediately, then we record the page number so that
742 * when an unplug occurs, we can flush the dirty pages out to disk
744 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
749 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
751 if (!bitmap->filemap) {
755 page = filemap_get_page(bitmap, chunk);
756 bit = file_page_offset(chunk);
759 /* make sure the page stays cached until it gets written out */
760 if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
761 page_cache_get(page);
764 kaddr = kmap_atomic(page, KM_USER0);
766 kunmap_atomic(kaddr, KM_USER0);
767 PRINTK("set file bit %lu page %lu\n", bit, page->index);
769 /* record page number so it gets flushed to disk when unplug occurs */
770 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
774 /* this gets called when the md device is ready to unplug its underlying
775 * (slave) device queues -- before we let any writes go down, we need to
776 * sync the dirty pages of the bitmap file to disk */
777 int bitmap_unplug(struct bitmap *bitmap)
779 unsigned long i, attr, flags;
787 /* look at each page to see if there are any set bits that need to be
788 * flushed out to disk */
789 for (i = 0; i < bitmap->file_pages; i++) {
790 spin_lock_irqsave(&bitmap->lock, flags);
791 if (!bitmap->filemap) {
792 spin_unlock_irqrestore(&bitmap->lock, flags);
795 page = bitmap->filemap[i];
796 attr = get_page_attr(bitmap, page);
797 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
798 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
799 if ((attr & BITMAP_PAGE_DIRTY))
801 spin_unlock_irqrestore(&bitmap->lock, flags);
803 if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE)) {
804 err = write_page(bitmap, page, 0);
805 if (err == -EAGAIN) {
806 if (attr & BITMAP_PAGE_DIRTY)
807 err = write_page(bitmap, page, 1);
815 if (wait) { /* if any writes were performed, we need to wait on them */
817 spin_lock_irq(&bitmap->write_lock);
818 wait_event_lock_irq(bitmap->write_wait,
819 list_empty(&bitmap->complete_pages), bitmap->write_lock,
820 wake_up_process(bitmap->writeback_daemon->tsk));
821 spin_unlock_irq(&bitmap->write_lock);
823 wait_event(bitmap->mddev->sb_wait,
824 atomic_read(&bitmap->mddev->pending_writes)==0);
829 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
830 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
831 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
832 * memory mapping of the bitmap file
834 * if there's no bitmap file, or if the bitmap file had been
835 * previously kicked from the array, we mark all the bits as
836 * 1's in order to cause a full resync.
838 * We ignore all bits for sectors that end earlier than 'start'.
839 * This is used when reading an out-of-date bitmap...
841 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
843 unsigned long i, chunks, index, oldindex, bit;
844 struct page *page = NULL, *oldpage = NULL;
845 unsigned long num_pages, bit_cnt = 0;
847 unsigned long bytes, offset, dummy;
851 chunks = bitmap->chunks;
854 BUG_ON(!file && !bitmap->offset);
856 #ifdef INJECT_FAULTS_3
859 outofdate = bitmap->flags & BITMAP_STALE;
862 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
863 "recovery\n", bmname(bitmap));
865 bytes = (chunks + 7) / 8;
867 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
869 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
870 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
872 (unsigned long) i_size_read(file->f_mapping->host),
873 bytes + sizeof(bitmap_super_t));
879 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
880 if (!bitmap->filemap)
883 bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL);
884 if (!bitmap->filemap_attr)
887 memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages);
891 for (i = 0; i < chunks; i++) {
892 index = file_page_index(i);
893 bit = file_page_offset(i);
894 if (index != oldindex) { /* this is a new page, read it in */
895 /* unmap the old page, we're done with it */
900 * if we're here then the superblock page
901 * contains some bits (PAGE_SIZE != sizeof sb)
902 * we've already read it in, so just use it
904 page = bitmap->sb_page;
905 offset = sizeof(bitmap_super_t);
907 page = read_page(file, index, &dummy);
910 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
913 if (IS_ERR(page)) { /* read error */
924 * if bitmap is out of date, dirty the
925 * whole page and write it out
927 memset(page_address(page) + offset, 0xff,
929 ret = write_page(bitmap, page, 1);
932 /* release, page not in filemap yet */
933 page_cache_release(page);
938 bitmap->filemap[bitmap->file_pages++] = page;
940 if (test_bit(bit, page_address(page))) {
941 /* if the disk bit is set, set the memory bit */
942 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
943 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
946 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
950 /* everything went OK */
952 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
954 if (page) /* unmap the last page */
957 if (bit_cnt) { /* Kick recovery if any bits were set */
958 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
959 md_wakeup_thread(bitmap->mddev->thread);
963 printk(KERN_INFO "%s: bitmap initialized from disk: "
964 "read %lu/%lu pages, set %lu bits, status: %d\n",
965 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
970 void bitmap_write_all(struct bitmap *bitmap)
972 /* We don't actually write all bitmap blocks here,
973 * just flag them as needing to be written
976 unsigned long chunks = bitmap->chunks;
977 unsigned long bytes = (chunks+7)/8 + sizeof(bitmap_super_t);
978 unsigned long num_pages = (bytes + PAGE_SIZE-1) / PAGE_SIZE;
980 bitmap->filemap_attr[num_pages] |= BITMAP_PAGE_NEEDWRITE;
984 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
986 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
987 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
988 bitmap->bp[page].count += inc;
990 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
991 (unsigned long long)offset, inc, bitmap->bp[page].count);
993 bitmap_checkfree(bitmap, page);
995 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
996 sector_t offset, int *blocks,
1000 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1004 int bitmap_daemon_work(struct bitmap *bitmap)
1007 unsigned long flags;
1008 struct page *page = NULL, *lastpage = NULL;
1015 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1017 bitmap->daemon_lastrun = jiffies;
1019 for (j = 0; j < bitmap->chunks; j++) {
1020 bitmap_counter_t *bmc;
1021 spin_lock_irqsave(&bitmap->lock, flags);
1022 if (!bitmap->filemap) {
1023 /* error or shutdown */
1024 spin_unlock_irqrestore(&bitmap->lock, flags);
1028 page = filemap_get_page(bitmap, j);
1030 if (page != lastpage) {
1031 /* skip this page unless it's marked as needing cleaning */
1032 if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
1033 if (attr & BITMAP_PAGE_NEEDWRITE) {
1034 page_cache_get(page);
1035 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1037 spin_unlock_irqrestore(&bitmap->lock, flags);
1038 if (attr & BITMAP_PAGE_NEEDWRITE) {
1039 switch (write_page(bitmap, page, 0)) {
1041 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1046 bitmap_file_kick(bitmap);
1048 page_cache_release(page);
1053 /* grab the new page, sync and release the old */
1054 page_cache_get(page);
1055 if (lastpage != NULL) {
1056 if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
1057 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1058 spin_unlock_irqrestore(&bitmap->lock, flags);
1059 err = write_page(bitmap, lastpage, 0);
1060 if (err == -EAGAIN) {
1062 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1065 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1066 spin_unlock_irqrestore(&bitmap->lock, flags);
1069 page_cache_release(lastpage);
1071 bitmap_file_kick(bitmap);
1073 spin_unlock_irqrestore(&bitmap->lock, flags);
1077 printk("bitmap clean at page %lu\n", j);
1079 spin_lock_irqsave(&bitmap->lock, flags);
1080 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1082 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1086 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1089 *bmc=1; /* maybe clear the bit next time */
1090 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1091 } else if (*bmc == 1) {
1092 /* we can clear the bit */
1094 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1098 clear_bit(file_page_offset(j), page_address(page));
1101 spin_unlock_irqrestore(&bitmap->lock, flags);
1104 /* now sync the final page */
1105 if (lastpage != NULL) {
1107 spin_lock_irqsave(&bitmap->lock, flags);
1108 if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
1109 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1110 spin_unlock_irqrestore(&bitmap->lock, flags);
1111 err = write_page(bitmap, lastpage, 0);
1112 if (err == -EAGAIN) {
1113 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1117 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1118 spin_unlock_irqrestore(&bitmap->lock, flags);
1121 page_cache_release(lastpage);
1127 static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
1130 unsigned long flags;
1132 /* if no one is waiting on us, we'll free the md thread struct
1133 * and exit, otherwise we let the waiter clean things up */
1134 spin_lock_irqsave(&bitmap->lock, flags);
1135 if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
1137 spin_unlock_irqrestore(&bitmap->lock, flags);
1139 complete_and_exit(NULL, 0); /* do_exit not exported */
1141 spin_unlock_irqrestore(&bitmap->lock, flags);
1144 static void bitmap_writeback_daemon(mddev_t *mddev)
1146 struct bitmap *bitmap = mddev->bitmap;
1148 struct page_list *item;
1151 if (signal_pending(current)) {
1153 "%s: bitmap writeback daemon got signal, exiting...\n",
1159 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1160 /* wait on bitmap page writebacks */
1161 while ((item = dequeue_page(bitmap))) {
1163 mempool_free(item, bitmap->write_pool);
1164 PRINTK("wait on page writeback: %p\n", page);
1165 wait_on_page_writeback(page);
1166 PRINTK("finished page writeback: %p\n", page);
1168 err = PageError(page);
1169 page_cache_release(page);
1171 printk(KERN_WARNING "%s: bitmap file writeback "
1172 "failed (page %lu): %d\n",
1173 bmname(bitmap), page->index, err);
1174 bitmap_file_kick(bitmap);
1179 wake_up(&bitmap->write_wait);
1181 printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
1182 bmname(bitmap), err);
1183 daemon_exit(bitmap, &bitmap->writeback_daemon);
1187 static int bitmap_start_daemon(struct bitmap *bitmap, mdk_thread_t **ptr,
1188 void (*func)(mddev_t *), char *name)
1190 mdk_thread_t *daemon;
1191 unsigned long flags;
1194 spin_lock_irqsave(&bitmap->lock, flags);
1197 if (!bitmap->file) /* no need for daemon if there's no backing file */
1200 spin_unlock_irqrestore(&bitmap->lock, flags);
1202 #ifdef INJECT_FATAL_FAULT_2
1205 sprintf(namebuf, "%%s_%s", name);
1206 daemon = md_register_thread(func, bitmap->mddev, namebuf);
1209 printk(KERN_ERR "%s: failed to start bitmap daemon\n",
1214 spin_lock_irqsave(&bitmap->lock, flags);
1217 md_wakeup_thread(daemon); /* start it running */
1219 PRINTK("%s: %s daemon (pid %d) started...\n",
1220 bmname(bitmap), name, daemon->tsk->pid);
1222 spin_unlock_irqrestore(&bitmap->lock, flags);
1226 static int bitmap_start_daemons(struct bitmap *bitmap)
1228 int err = bitmap_start_daemon(bitmap, &bitmap->writeback_daemon,
1229 bitmap_writeback_daemon, "bitmap_wb");
1233 static void bitmap_stop_daemon(struct bitmap *bitmap, mdk_thread_t **ptr)
1235 mdk_thread_t *daemon;
1236 unsigned long flags;
1238 spin_lock_irqsave(&bitmap->lock, flags);
1241 spin_unlock_irqrestore(&bitmap->lock, flags);
1243 md_unregister_thread(daemon); /* destroy the thread */
1246 static void bitmap_stop_daemons(struct bitmap *bitmap)
1248 /* the daemons can't stop themselves... they'll just exit instead... */
1249 if (bitmap->writeback_daemon &&
1250 current->pid != bitmap->writeback_daemon->tsk->pid)
1251 bitmap_stop_daemon(bitmap, &bitmap->writeback_daemon);
1254 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1255 sector_t offset, int *blocks,
1258 /* If 'create', we might release the lock and reclaim it.
1259 * The lock must have been taken with interrupts enabled.
1260 * If !create, we don't release the lock.
1262 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1263 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1264 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1267 if (bitmap_checkpage(bitmap, page, create) < 0) {
1268 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1269 *blocks = csize - (offset & (csize- 1));
1272 /* now locked ... */
1274 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1275 /* should we use the first or second counter field
1276 * of the hijacked pointer? */
1277 int hi = (pageoff > PAGE_COUNTER_MASK);
1278 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1279 PAGE_COUNTER_SHIFT - 1);
1280 *blocks = csize - (offset & (csize- 1));
1281 return &((bitmap_counter_t *)
1282 &bitmap->bp[page].map)[hi];
1283 } else { /* page is allocated */
1284 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1285 *blocks = csize - (offset & (csize- 1));
1286 return (bitmap_counter_t *)
1287 &(bitmap->bp[page].map[pageoff]);
1291 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1293 if (!bitmap) return 0;
1296 atomic_inc(&bitmap->behind_writes);
1297 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1298 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1303 bitmap_counter_t *bmc;
1305 spin_lock_irq(&bitmap->lock);
1306 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1308 spin_unlock_irq(&bitmap->lock);
1314 bitmap_file_set_bit(bitmap, offset);
1315 bitmap_count_page(bitmap,offset, 1);
1316 blk_plug_device(bitmap->mddev->queue);
1321 if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
1324 spin_unlock_irq(&bitmap->lock);
1327 if (sectors > blocks)
1334 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1335 int success, int behind)
1337 if (!bitmap) return;
1339 atomic_dec(&bitmap->behind_writes);
1340 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1341 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1346 unsigned long flags;
1347 bitmap_counter_t *bmc;
1349 spin_lock_irqsave(&bitmap->lock, flags);
1350 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1352 spin_unlock_irqrestore(&bitmap->lock, flags);
1356 if (!success && ! (*bmc & NEEDED_MASK))
1357 *bmc |= NEEDED_MASK;
1361 set_page_attr(bitmap,
1362 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1365 spin_unlock_irqrestore(&bitmap->lock, flags);
1367 if (sectors > blocks)
1373 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1376 bitmap_counter_t *bmc;
1378 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1380 return 1; /* always resync if no bitmap */
1382 spin_lock_irq(&bitmap->lock);
1383 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1389 else if (NEEDED(*bmc)) {
1391 if (!degraded) { /* don't set/clear bits if degraded */
1392 *bmc |= RESYNC_MASK;
1393 *bmc &= ~NEEDED_MASK;
1397 spin_unlock_irq(&bitmap->lock);
1401 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1403 bitmap_counter_t *bmc;
1404 unsigned long flags;
1406 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1407 */ if (bitmap == NULL) {
1411 spin_lock_irqsave(&bitmap->lock, flags);
1412 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1417 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1420 *bmc &= ~RESYNC_MASK;
1422 if (!NEEDED(*bmc) && aborted)
1423 *bmc |= NEEDED_MASK;
1426 set_page_attr(bitmap,
1427 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1433 spin_unlock_irqrestore(&bitmap->lock, flags);
1436 void bitmap_close_sync(struct bitmap *bitmap)
1438 /* Sync has finished, and any bitmap chunks that weren't synced
1439 * properly have been aborted. It remains to us to clear the
1440 * RESYNC bit wherever it is still on
1442 sector_t sector = 0;
1444 if (!bitmap) return;
1445 while (sector < bitmap->mddev->resync_max_sectors) {
1446 bitmap_end_sync(bitmap, sector, &blocks, 0);
1448 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1449 (unsigned long long)sector, blocks);
1450 */ sector += blocks;
1454 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1456 /* For each chunk covered by any of these sectors, set the
1457 * counter to 1 and set resync_needed. They should all
1458 * be 0 at this point
1462 bitmap_counter_t *bmc;
1463 spin_lock_irq(&bitmap->lock);
1464 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1466 spin_unlock_irq(&bitmap->lock);
1471 *bmc = 1 | (needed?NEEDED_MASK:0);
1472 bitmap_count_page(bitmap, offset, 1);
1473 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1474 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1476 spin_unlock_irq(&bitmap->lock);
1481 * flush out any pending updates
1483 void bitmap_flush(mddev_t *mddev)
1485 struct bitmap *bitmap = mddev->bitmap;
1488 if (!bitmap) /* there was no bitmap */
1491 /* run the daemon_work three time to ensure everything is flushed
1494 sleep = bitmap->daemon_sleep;
1495 bitmap->daemon_sleep = 0;
1496 bitmap_daemon_work(bitmap);
1497 bitmap_daemon_work(bitmap);
1498 bitmap_daemon_work(bitmap);
1499 bitmap->daemon_sleep = sleep;
1500 bitmap_update_sb(bitmap);
1504 * free memory that was allocated
1506 void bitmap_destroy(mddev_t *mddev)
1508 unsigned long k, pages;
1509 struct bitmap_page *bp;
1510 struct bitmap *bitmap = mddev->bitmap;
1512 if (!bitmap) /* there was no bitmap */
1515 mddev->bitmap = NULL; /* disconnect from the md device */
1517 /* release the bitmap file and kill the daemon */
1518 bitmap_file_put(bitmap);
1521 pages = bitmap->pages;
1523 /* free all allocated memory */
1525 mempool_destroy(bitmap->write_pool);
1527 if (bp) /* deallocate the page memory */
1528 for (k = 0; k < pages; k++)
1529 if (bp[k].map && !bp[k].hijacked)
1536 * initialize the bitmap structure
1537 * if this returns an error, bitmap_destroy must be called to do clean up
1539 int bitmap_create(mddev_t *mddev)
1541 struct bitmap *bitmap;
1542 unsigned long blocks = mddev->resync_max_sectors;
1543 unsigned long chunks;
1544 unsigned long pages;
1545 struct file *file = mddev->bitmap_file;
1549 BUG_ON(sizeof(bitmap_super_t) != 256);
1551 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
1554 BUG_ON(file && mddev->bitmap_offset);
1556 bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
1560 memset(bitmap, 0, sizeof(*bitmap));
1562 spin_lock_init(&bitmap->lock);
1563 bitmap->mddev = mddev;
1564 mddev->bitmap = bitmap;
1566 spin_lock_init(&bitmap->write_lock);
1567 INIT_LIST_HEAD(&bitmap->complete_pages);
1568 init_waitqueue_head(&bitmap->write_wait);
1569 bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
1570 write_pool_free, NULL);
1571 if (!bitmap->write_pool)
1574 bitmap->file = file;
1575 bitmap->offset = mddev->bitmap_offset;
1576 if (file) get_file(file);
1577 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1578 err = bitmap_read_sb(bitmap);
1582 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1583 sizeof(bitmap->chunksize));
1585 /* now that chunksize and chunkshift are set, we can use these macros */
1586 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1587 CHUNK_BLOCK_RATIO(bitmap);
1588 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1592 bitmap->chunks = chunks;
1593 bitmap->pages = pages;
1594 bitmap->missing_pages = pages;
1595 bitmap->counter_bits = COUNTER_BITS;
1597 bitmap->syncchunk = ~0UL;
1599 #ifdef INJECT_FATAL_FAULT_1
1602 bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1606 memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp));
1608 bitmap->flags |= BITMAP_ACTIVE;
1610 /* now that we have some pages available, initialize the in-memory
1611 * bitmap from the on-disk bitmap */
1613 if (mddev->degraded == 0
1614 || bitmap->events_cleared == mddev->events)
1615 /* no need to keep dirty bits to optimise a re-add of a missing device */
1616 start = mddev->recovery_cp;
1617 err = bitmap_init_from_disk(bitmap, start);
1622 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1623 pages, bmname(bitmap));
1625 /* kick off the bitmap daemons */
1626 err = bitmap_start_daemons(bitmap);
1629 return bitmap_update_sb(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);