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).
16 * wait if count gets too high, wake when it drops to half.
17 * allow bitmap to be mirrored with superblock (before or after...)
18 * allow hot-add to re-instate a current device.
19 * allow hot-add of bitmap after quiessing device
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/config.h>
27 #include <linux/timer.h>
28 #include <linux/sched.h>
29 #include <linux/list.h>
30 #include <linux/file.h>
31 #include <linux/mount.h>
32 #include <linux/buffer_head.h>
33 #include <linux/raid/md.h>
34 #include <linux/raid/bitmap.h>
41 /* these are for debugging purposes only! */
43 /* define one and only one of these */
44 #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
45 #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
46 #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
47 #define INJECT_FAULTS_4 0 /* undef */
48 #define INJECT_FAULTS_5 0 /* undef */
49 #define INJECT_FAULTS_6 0
51 /* if these are defined, the driver will fail! debug only */
52 #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
53 #define INJECT_FATAL_FAULT_2 0 /* undef */
54 #define INJECT_FATAL_FAULT_3 0 /* undef */
57 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
58 #define DPRINTK(x...) do { } while(0)
62 # define PRINTK(x...) printk(KERN_DEBUG x)
68 static inline char * bmname(struct bitmap *bitmap)
70 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
75 * test if the bitmap is active
77 int bitmap_active(struct bitmap *bitmap)
84 spin_lock_irqsave(&bitmap->lock, flags);
85 res = bitmap->flags & BITMAP_ACTIVE;
86 spin_unlock_irqrestore(&bitmap->lock, flags);
90 #define WRITE_POOL_SIZE 256
93 * just a placeholder - calls kmalloc for bitmap pages
95 static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
99 #ifdef INJECT_FAULTS_1
102 page = kmalloc(PAGE_SIZE, GFP_NOIO);
105 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
107 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
108 bmname(bitmap), page);
113 * for now just a placeholder -- just calls kfree for bitmap pages
115 static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
117 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
122 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
124 * 1) check to see if this page is allocated, if it's not then try to alloc
125 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
126 * page pointer directly as a counter
128 * if we find our page, we increment the page's refcount so that it stays
129 * allocated while we're using it
131 static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
133 unsigned char *mappage;
135 if (page >= bitmap->pages) {
137 "%s: invalid bitmap page request: %lu (> %lu)\n",
138 bmname(bitmap), page, bitmap->pages-1);
143 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
146 if (bitmap->bp[page].map) /* page is already allocated, just return */
152 spin_unlock_irq(&bitmap->lock);
154 /* this page has not been allocated yet */
156 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
157 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
159 /* failed - set the hijacked flag so that we can use the
160 * pointer as a counter */
161 spin_lock_irq(&bitmap->lock);
162 if (!bitmap->bp[page].map)
163 bitmap->bp[page].hijacked = 1;
169 spin_lock_irq(&bitmap->lock);
171 /* recheck the page */
173 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
174 /* somebody beat us to getting the page */
175 bitmap_free_page(bitmap, mappage);
179 /* no page was in place and we have one, so install it */
181 memset(mappage, 0, PAGE_SIZE);
182 bitmap->bp[page].map = mappage;
183 bitmap->missing_pages--;
189 /* if page is completely empty, put it back on the free list, or dealloc it */
190 /* if page was hijacked, unmark the flag so it might get alloced next time */
191 /* Note: lock should be held when calling this */
192 static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
196 if (bitmap->bp[page].count) /* page is still busy */
199 /* page is no longer in use, it can be released */
201 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
202 bitmap->bp[page].hijacked = 0;
203 bitmap->bp[page].map = NULL;
207 /* normal case, free the page */
210 /* actually ... let's not. We will probably need the page again exactly when
211 * memory is tight and we are flusing to disk
215 ptr = bitmap->bp[page].map;
216 bitmap->bp[page].map = NULL;
217 bitmap->missing_pages++;
218 bitmap_free_page(bitmap, ptr);
225 * bitmap file handling - read and write the bitmap file and its superblock
228 /* copy the pathname of a file to a buffer */
229 char *file_path(struct file *file, char *buf, int count)
240 buf = d_path(d, v, buf, count);
242 return IS_ERR(buf) ? NULL : buf;
246 * basic page I/O operations
249 /* IO operations when bitmap is stored near all superblocks */
250 static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
252 /* choose a good rdev and read the page from there */
255 struct list_head *tmp;
256 struct page *page = alloc_page(GFP_KERNEL);
260 return ERR_PTR(-ENOMEM);
262 ITERATE_RDEV(mddev, rdev, tmp) {
263 if (! test_bit(In_sync, &rdev->flags)
264 || test_bit(Faulty, &rdev->flags))
267 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
269 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
274 return ERR_PTR(-EIO);
278 static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
281 struct list_head *tmp;
283 ITERATE_RDEV(mddev, rdev, tmp)
284 if (test_bit(In_sync, &rdev->flags)
285 && !test_bit(Faulty, &rdev->flags))
286 md_super_write(mddev, rdev,
287 (rdev->sb_offset<<1) + offset
288 + page->index * (PAGE_SIZE/512),
293 md_super_wait(mddev);
298 * write out a page to a file
300 static int write_page(struct bitmap *bitmap, struct page *page, int wait)
304 if (bitmap->file == NULL)
305 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
307 flush_dcache_page(page); /* make sure visible to anyone reading the file */
312 if (TestSetPageLocked(page))
313 return -EAGAIN; /* already locked */
314 if (PageWriteback(page)) {
320 ret = page->mapping->a_ops->prepare_write(bitmap->file, page, 0, PAGE_SIZE);
322 ret = page->mapping->a_ops->commit_write(bitmap->file, page, 0,
329 set_page_dirty(page); /* force it to be written out */
332 /* add to list to be waited for */
333 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
335 spin_lock(&bitmap->write_lock);
336 list_add(&item->list, &bitmap->complete_pages);
337 spin_unlock(&bitmap->write_lock);
339 return write_one_page(page, wait);
342 /* read a page from a file, pinning it into cache, and return bytes_read */
343 static struct page *read_page(struct file *file, unsigned long index,
344 unsigned long *bytes_read)
346 struct inode *inode = file->f_mapping->host;
347 struct page *page = NULL;
348 loff_t isize = i_size_read(inode);
349 unsigned long end_index = isize >> PAGE_SHIFT;
351 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
352 (unsigned long long)index << PAGE_SHIFT);
354 page = read_cache_page(inode->i_mapping, index,
355 (filler_t *)inode->i_mapping->a_ops->readpage, file);
358 wait_on_page_locked(page);
359 if (!PageUptodate(page) || PageError(page)) {
361 page = ERR_PTR(-EIO);
365 if (index > end_index) /* we have read beyond EOF */
367 else if (index == end_index) /* possible short read */
368 *bytes_read = isize & ~PAGE_MASK;
370 *bytes_read = PAGE_SIZE; /* got a full page */
373 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
375 (unsigned long long)index << PAGE_SHIFT,
381 * bitmap file superblock operations
384 /* update the event counter and sync the superblock to disk */
385 int bitmap_update_sb(struct bitmap *bitmap)
390 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
392 spin_lock_irqsave(&bitmap->lock, flags);
393 if (!bitmap->sb_page) { /* no superblock */
394 spin_unlock_irqrestore(&bitmap->lock, flags);
397 spin_unlock_irqrestore(&bitmap->lock, flags);
398 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
399 sb->events = cpu_to_le64(bitmap->mddev->events);
400 if (!bitmap->mddev->degraded)
401 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
402 kunmap_atomic(sb, KM_USER0);
403 return write_page(bitmap, bitmap->sb_page, 1);
406 /* print out the bitmap file superblock */
407 void bitmap_print_sb(struct bitmap *bitmap)
411 if (!bitmap || !bitmap->sb_page)
413 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
414 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
415 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
416 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
417 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
418 *(__u32 *)(sb->uuid+0),
419 *(__u32 *)(sb->uuid+4),
420 *(__u32 *)(sb->uuid+8),
421 *(__u32 *)(sb->uuid+12));
422 printk(KERN_DEBUG " events: %llu\n",
423 (unsigned long long) le64_to_cpu(sb->events));
424 printk(KERN_DEBUG "events cleared: %llu\n",
425 (unsigned long long) le64_to_cpu(sb->events_cleared));
426 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
427 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
428 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
429 printk(KERN_DEBUG " sync size: %llu KB\n",
430 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
431 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
432 kunmap_atomic(sb, KM_USER0);
435 /* read the superblock from the bitmap file and initialize some bitmap fields */
436 static int bitmap_read_sb(struct bitmap *bitmap)
440 unsigned long chunksize, daemon_sleep, write_behind;
441 unsigned long bytes_read;
442 unsigned long long events;
445 /* page 0 is the superblock, read it... */
447 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
449 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
450 bytes_read = PAGE_SIZE;
452 if (IS_ERR(bitmap->sb_page)) {
453 err = PTR_ERR(bitmap->sb_page);
454 bitmap->sb_page = NULL;
458 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
460 if (bytes_read < sizeof(*sb)) { /* short read */
461 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
467 chunksize = le32_to_cpu(sb->chunksize);
468 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
469 write_behind = le32_to_cpu(sb->write_behind);
471 /* verify that the bitmap-specific fields are valid */
472 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
473 reason = "bad magic";
474 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
475 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
476 reason = "unrecognized superblock version";
477 else if (chunksize < PAGE_SIZE)
478 reason = "bitmap chunksize too small";
479 else if ((1 << ffz(~chunksize)) != chunksize)
480 reason = "bitmap chunksize not a power of 2";
481 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
482 reason = "daemon sleep period out of range";
483 else if (write_behind > COUNTER_MAX)
484 reason = "write-behind limit out of range (0 - 16383)";
486 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
487 bmname(bitmap), reason);
491 /* keep the array size field of the bitmap superblock up to date */
492 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
494 if (!bitmap->mddev->persistent)
498 * if we have a persistent array superblock, compare the
499 * bitmap's UUID and event counter to the mddev's
501 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
502 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
506 events = le64_to_cpu(sb->events);
507 if (events < bitmap->mddev->events) {
508 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
509 "-- forcing full recovery\n", bmname(bitmap), events,
510 (unsigned long long) bitmap->mddev->events);
511 sb->state |= BITMAP_STALE;
514 /* assign fields using values from superblock */
515 bitmap->chunksize = chunksize;
516 bitmap->daemon_sleep = daemon_sleep;
517 bitmap->daemon_lastrun = jiffies;
518 bitmap->max_write_behind = write_behind;
519 bitmap->flags |= sb->state;
520 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
521 bitmap->flags |= BITMAP_HOSTENDIAN;
522 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
523 if (sb->state & BITMAP_STALE)
524 bitmap->events_cleared = bitmap->mddev->events;
527 kunmap_atomic(sb, KM_USER0);
529 bitmap_print_sb(bitmap);
533 enum bitmap_mask_op {
538 /* record the state of the bitmap in the superblock */
539 static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
540 enum bitmap_mask_op op)
545 spin_lock_irqsave(&bitmap->lock, flags);
546 if (!bitmap->sb_page) { /* can't set the state */
547 spin_unlock_irqrestore(&bitmap->lock, flags);
550 spin_unlock_irqrestore(&bitmap->lock, flags);
551 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
553 case MASK_SET: sb->state |= bits;
555 case MASK_UNSET: sb->state &= ~bits;
559 kunmap_atomic(sb, KM_USER0);
563 * general bitmap file operations
566 /* calculate the index of the page that contains this bit */
567 static inline unsigned long file_page_index(unsigned long chunk)
569 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
572 /* calculate the (bit) offset of this bit within a page */
573 static inline unsigned long file_page_offset(unsigned long chunk)
575 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
579 * return a pointer to the page in the filemap that contains the given bit
581 * this lookup is complicated by the fact that the bitmap sb might be exactly
582 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
585 static inline struct page *filemap_get_page(struct bitmap *bitmap,
588 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
592 static void bitmap_file_unmap(struct bitmap *bitmap)
594 struct page **map, *sb_page;
599 spin_lock_irqsave(&bitmap->lock, flags);
600 map = bitmap->filemap;
601 bitmap->filemap = NULL;
602 attr = bitmap->filemap_attr;
603 bitmap->filemap_attr = NULL;
604 pages = bitmap->file_pages;
605 bitmap->file_pages = 0;
606 sb_page = bitmap->sb_page;
607 bitmap->sb_page = NULL;
608 spin_unlock_irqrestore(&bitmap->lock, flags);
611 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
612 put_page(map[pages]);
616 safe_put_page(sb_page);
619 /* dequeue the next item in a page list -- don't call from irq context */
620 static struct page_list *dequeue_page(struct bitmap *bitmap)
622 struct page_list *item = NULL;
623 struct list_head *head = &bitmap->complete_pages;
625 spin_lock(&bitmap->write_lock);
626 if (list_empty(head))
628 item = list_entry(head->prev, struct page_list, list);
629 list_del(head->prev);
631 spin_unlock(&bitmap->write_lock);
635 static void drain_write_queues(struct bitmap *bitmap)
637 struct page_list *item;
639 while ((item = dequeue_page(bitmap))) {
640 /* don't bother to wait */
641 mempool_free(item, bitmap->write_pool);
645 static void bitmap_file_put(struct bitmap *bitmap)
651 spin_lock_irqsave(&bitmap->lock, flags);
654 spin_unlock_irqrestore(&bitmap->lock, flags);
656 drain_write_queues(bitmap);
658 bitmap_file_unmap(bitmap);
661 inode = file->f_mapping->host;
662 spin_lock(&inode->i_lock);
663 atomic_set(&inode->i_writecount, 1); /* allow writes again */
664 spin_unlock(&inode->i_lock);
671 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
672 * then it is no longer reliable, so we stop using it and we mark the file
673 * as failed in the superblock
675 static void bitmap_file_kick(struct bitmap *bitmap)
677 char *path, *ptr = NULL;
679 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
680 bitmap_update_sb(bitmap);
683 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
685 ptr = file_path(bitmap->file, path, PAGE_SIZE);
687 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
688 bmname(bitmap), ptr ? ptr : "");
693 bitmap_file_put(bitmap);
698 enum bitmap_page_attr {
699 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
700 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
701 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
704 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
705 enum bitmap_page_attr attr)
707 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
710 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
711 enum bitmap_page_attr attr)
713 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
716 static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
717 enum bitmap_page_attr attr)
719 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
723 * bitmap_file_set_bit -- called before performing a write to the md device
724 * to set (and eventually sync) a particular bit in the bitmap file
726 * we set the bit immediately, then we record the page number so that
727 * when an unplug occurs, we can flush the dirty pages out to disk
729 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
734 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
736 if (!bitmap->filemap) {
740 page = filemap_get_page(bitmap, chunk);
741 bit = file_page_offset(chunk);
744 kaddr = kmap_atomic(page, KM_USER0);
745 if (bitmap->flags & BITMAP_HOSTENDIAN)
748 ext2_set_bit(bit, kaddr);
749 kunmap_atomic(kaddr, KM_USER0);
750 PRINTK("set file bit %lu page %lu\n", bit, page->index);
752 /* record page number so it gets flushed to disk when unplug occurs */
753 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
757 static void bitmap_writeback(struct bitmap *bitmap);
759 /* this gets called when the md device is ready to unplug its underlying
760 * (slave) device queues -- before we let any writes go down, we need to
761 * sync the dirty pages of the bitmap file to disk */
762 int bitmap_unplug(struct bitmap *bitmap)
764 unsigned long i, flags;
765 int dirty, need_write;
773 /* look at each page to see if there are any set bits that need to be
774 * flushed out to disk */
775 for (i = 0; i < bitmap->file_pages; i++) {
776 spin_lock_irqsave(&bitmap->lock, flags);
777 if (!bitmap->filemap) {
778 spin_unlock_irqrestore(&bitmap->lock, flags);
781 page = bitmap->filemap[i];
782 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
783 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
784 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
785 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
788 spin_unlock_irqrestore(&bitmap->lock, flags);
790 if (dirty | need_write) {
791 err = write_page(bitmap, page, 0);
792 if (err == -EAGAIN) {
794 err = write_page(bitmap, page, 1);
802 if (wait) { /* if any writes were performed, we need to wait on them */
804 bitmap_writeback(bitmap);
806 md_super_wait(bitmap->mddev);
811 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
812 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
813 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
814 * memory mapping of the bitmap file
816 * if there's no bitmap file, or if the bitmap file had been
817 * previously kicked from the array, we mark all the bits as
818 * 1's in order to cause a full resync.
820 * We ignore all bits for sectors that end earlier than 'start'.
821 * This is used when reading an out-of-date bitmap...
823 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
825 unsigned long i, chunks, index, oldindex, bit;
826 struct page *page = NULL, *oldpage = NULL;
827 unsigned long num_pages, bit_cnt = 0;
829 unsigned long bytes, offset, dummy;
834 chunks = bitmap->chunks;
837 BUG_ON(!file && !bitmap->offset);
839 #ifdef INJECT_FAULTS_3
842 outofdate = bitmap->flags & BITMAP_STALE;
845 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
846 "recovery\n", bmname(bitmap));
848 bytes = (chunks + 7) / 8;
850 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
852 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
853 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
855 (unsigned long) i_size_read(file->f_mapping->host),
856 bytes + sizeof(bitmap_super_t));
862 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
863 if (!bitmap->filemap)
866 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
867 bitmap->filemap_attr = kzalloc(
868 (((num_pages*4/8)+sizeof(unsigned long)-1)
869 /sizeof(unsigned long))
870 *sizeof(unsigned long),
872 if (!bitmap->filemap_attr)
877 for (i = 0; i < chunks; i++) {
879 index = file_page_index(i);
880 bit = file_page_offset(i);
881 if (index != oldindex) { /* this is a new page, read it in */
882 /* unmap the old page, we're done with it */
885 * if we're here then the superblock page
886 * contains some bits (PAGE_SIZE != sizeof sb)
887 * we've already read it in, so just use it
889 page = bitmap->sb_page;
890 offset = sizeof(bitmap_super_t);
892 page = read_page(file, index, &dummy);
895 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
898 if (IS_ERR(page)) { /* read error */
908 * if bitmap is out of date, dirty the
909 * whole page and write it out
911 paddr = kmap_atomic(page, KM_USER0);
912 memset(paddr + offset, 0xff,
914 kunmap_atomic(paddr, KM_USER0);
915 ret = write_page(bitmap, page, 1);
917 /* release, page not in filemap yet */
923 bitmap->filemap[bitmap->file_pages++] = page;
925 paddr = kmap_atomic(page, KM_USER0);
926 if (bitmap->flags & BITMAP_HOSTENDIAN)
927 b = test_bit(bit, paddr);
929 b = ext2_test_bit(bit, paddr);
930 kunmap_atomic(paddr, KM_USER0);
932 /* if the disk bit is set, set the memory bit */
933 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
934 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
937 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
941 /* everything went OK */
943 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
945 if (bit_cnt) { /* Kick recovery if any bits were set */
946 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
947 md_wakeup_thread(bitmap->mddev->thread);
951 printk(KERN_INFO "%s: bitmap initialized from disk: "
952 "read %lu/%lu pages, set %lu bits, status: %d\n",
953 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
958 void bitmap_write_all(struct bitmap *bitmap)
960 /* We don't actually write all bitmap blocks here,
961 * just flag them as needing to be written
965 for (i=0; i < bitmap->file_pages; i++)
966 set_page_attr(bitmap, bitmap->filemap[i],
967 BITMAP_PAGE_NEEDWRITE);
971 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
973 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
974 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
975 bitmap->bp[page].count += inc;
977 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
978 (unsigned long long)offset, inc, bitmap->bp[page].count);
980 bitmap_checkfree(bitmap, page);
982 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
983 sector_t offset, int *blocks,
987 * bitmap daemon -- periodically wakes up to clean bits and flush pages
991 int bitmap_daemon_work(struct bitmap *bitmap)
995 struct page *page = NULL, *lastpage = NULL;
1002 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1004 bitmap->daemon_lastrun = jiffies;
1006 for (j = 0; j < bitmap->chunks; j++) {
1007 bitmap_counter_t *bmc;
1008 spin_lock_irqsave(&bitmap->lock, flags);
1009 if (!bitmap->filemap) {
1010 /* error or shutdown */
1011 spin_unlock_irqrestore(&bitmap->lock, flags);
1015 page = filemap_get_page(bitmap, j);
1017 if (page != lastpage) {
1018 /* skip this page unless it's marked as needing cleaning */
1019 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1020 int need_write = test_page_attr(bitmap, page,
1021 BITMAP_PAGE_NEEDWRITE);
1023 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1025 spin_unlock_irqrestore(&bitmap->lock, flags);
1027 switch (write_page(bitmap, page, 0)) {
1029 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1034 bitmap_file_kick(bitmap);
1040 /* grab the new page, sync and release the old */
1041 if (lastpage != NULL) {
1042 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1043 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1044 spin_unlock_irqrestore(&bitmap->lock, flags);
1045 err = write_page(bitmap, lastpage, 0);
1046 if (err == -EAGAIN) {
1048 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1051 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1052 spin_unlock_irqrestore(&bitmap->lock, flags);
1055 bitmap_file_kick(bitmap);
1057 spin_unlock_irqrestore(&bitmap->lock, flags);
1060 printk("bitmap clean at page %lu\n", j);
1062 spin_lock_irqsave(&bitmap->lock, flags);
1063 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1065 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1069 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1072 *bmc=1; /* maybe clear the bit next time */
1073 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1074 } else if (*bmc == 1) {
1075 /* we can clear the bit */
1077 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1081 paddr = kmap_atomic(page, KM_USER0);
1082 if (bitmap->flags & BITMAP_HOSTENDIAN)
1083 clear_bit(file_page_offset(j), paddr);
1085 ext2_clear_bit(file_page_offset(j), paddr);
1086 kunmap_atomic(paddr, KM_USER0);
1089 spin_unlock_irqrestore(&bitmap->lock, flags);
1092 /* now sync the final page */
1093 if (lastpage != NULL) {
1094 spin_lock_irqsave(&bitmap->lock, flags);
1095 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1096 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1097 spin_unlock_irqrestore(&bitmap->lock, flags);
1098 err = write_page(bitmap, lastpage, 0);
1099 if (err == -EAGAIN) {
1100 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1104 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1105 spin_unlock_irqrestore(&bitmap->lock, flags);
1112 static void bitmap_writeback(struct bitmap *bitmap)
1115 struct page_list *item;
1118 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1119 /* wait on bitmap page writebacks */
1120 while ((item = dequeue_page(bitmap))) {
1122 mempool_free(item, bitmap->write_pool);
1123 PRINTK("wait on page writeback: %p\n", page);
1124 wait_on_page_writeback(page);
1125 PRINTK("finished page writeback: %p\n", page);
1127 err = PageError(page);
1129 printk(KERN_WARNING "%s: bitmap file writeback "
1130 "failed (page %lu): %d\n",
1131 bmname(bitmap), page->index, err);
1132 bitmap_file_kick(bitmap);
1138 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1139 sector_t offset, int *blocks,
1142 /* If 'create', we might release the lock and reclaim it.
1143 * The lock must have been taken with interrupts enabled.
1144 * If !create, we don't release the lock.
1146 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1147 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1148 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1151 if (bitmap_checkpage(bitmap, page, create) < 0) {
1152 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1153 *blocks = csize - (offset & (csize- 1));
1156 /* now locked ... */
1158 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1159 /* should we use the first or second counter field
1160 * of the hijacked pointer? */
1161 int hi = (pageoff > PAGE_COUNTER_MASK);
1162 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1163 PAGE_COUNTER_SHIFT - 1);
1164 *blocks = csize - (offset & (csize- 1));
1165 return &((bitmap_counter_t *)
1166 &bitmap->bp[page].map)[hi];
1167 } else { /* page is allocated */
1168 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1169 *blocks = csize - (offset & (csize- 1));
1170 return (bitmap_counter_t *)
1171 &(bitmap->bp[page].map[pageoff]);
1175 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1177 if (!bitmap) return 0;
1180 atomic_inc(&bitmap->behind_writes);
1181 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1182 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1187 bitmap_counter_t *bmc;
1189 spin_lock_irq(&bitmap->lock);
1190 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1192 spin_unlock_irq(&bitmap->lock);
1198 bitmap_file_set_bit(bitmap, offset);
1199 bitmap_count_page(bitmap,offset, 1);
1200 blk_plug_device(bitmap->mddev->queue);
1205 BUG_ON((*bmc & COUNTER_MAX) == COUNTER_MAX);
1208 spin_unlock_irq(&bitmap->lock);
1211 if (sectors > blocks)
1218 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1219 int success, int behind)
1221 if (!bitmap) return;
1223 atomic_dec(&bitmap->behind_writes);
1224 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1225 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1230 unsigned long flags;
1231 bitmap_counter_t *bmc;
1233 spin_lock_irqsave(&bitmap->lock, flags);
1234 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1236 spin_unlock_irqrestore(&bitmap->lock, flags);
1240 if (!success && ! (*bmc & NEEDED_MASK))
1241 *bmc |= NEEDED_MASK;
1245 set_page_attr(bitmap,
1246 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1249 spin_unlock_irqrestore(&bitmap->lock, flags);
1251 if (sectors > blocks)
1257 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1260 bitmap_counter_t *bmc;
1262 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1264 return 1; /* always resync if no bitmap */
1266 spin_lock_irq(&bitmap->lock);
1267 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1273 else if (NEEDED(*bmc)) {
1275 if (!degraded) { /* don't set/clear bits if degraded */
1276 *bmc |= RESYNC_MASK;
1277 *bmc &= ~NEEDED_MASK;
1281 spin_unlock_irq(&bitmap->lock);
1285 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1287 bitmap_counter_t *bmc;
1288 unsigned long flags;
1290 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1291 */ if (bitmap == NULL) {
1295 spin_lock_irqsave(&bitmap->lock, flags);
1296 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1301 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1304 *bmc &= ~RESYNC_MASK;
1306 if (!NEEDED(*bmc) && aborted)
1307 *bmc |= NEEDED_MASK;
1310 set_page_attr(bitmap,
1311 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1317 spin_unlock_irqrestore(&bitmap->lock, flags);
1320 void bitmap_close_sync(struct bitmap *bitmap)
1322 /* Sync has finished, and any bitmap chunks that weren't synced
1323 * properly have been aborted. It remains to us to clear the
1324 * RESYNC bit wherever it is still on
1326 sector_t sector = 0;
1328 if (!bitmap) return;
1329 while (sector < bitmap->mddev->resync_max_sectors) {
1330 bitmap_end_sync(bitmap, sector, &blocks, 0);
1332 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1333 (unsigned long long)sector, blocks);
1334 */ sector += blocks;
1338 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1340 /* For each chunk covered by any of these sectors, set the
1341 * counter to 1 and set resync_needed. They should all
1342 * be 0 at this point
1346 bitmap_counter_t *bmc;
1347 spin_lock_irq(&bitmap->lock);
1348 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1350 spin_unlock_irq(&bitmap->lock);
1355 *bmc = 1 | (needed?NEEDED_MASK:0);
1356 bitmap_count_page(bitmap, offset, 1);
1357 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1358 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1360 spin_unlock_irq(&bitmap->lock);
1365 * flush out any pending updates
1367 void bitmap_flush(mddev_t *mddev)
1369 struct bitmap *bitmap = mddev->bitmap;
1372 if (!bitmap) /* there was no bitmap */
1375 /* run the daemon_work three time to ensure everything is flushed
1378 sleep = bitmap->daemon_sleep;
1379 bitmap->daemon_sleep = 0;
1380 bitmap_daemon_work(bitmap);
1381 bitmap_daemon_work(bitmap);
1382 bitmap_daemon_work(bitmap);
1383 bitmap->daemon_sleep = sleep;
1384 bitmap_update_sb(bitmap);
1388 * free memory that was allocated
1390 static void bitmap_free(struct bitmap *bitmap)
1392 unsigned long k, pages;
1393 struct bitmap_page *bp;
1395 if (!bitmap) /* there was no bitmap */
1398 /* release the bitmap file and kill the daemon */
1399 bitmap_file_put(bitmap);
1402 pages = bitmap->pages;
1404 /* free all allocated memory */
1406 mempool_destroy(bitmap->write_pool);
1408 if (bp) /* deallocate the page memory */
1409 for (k = 0; k < pages; k++)
1410 if (bp[k].map && !bp[k].hijacked)
1415 void bitmap_destroy(mddev_t *mddev)
1417 struct bitmap *bitmap = mddev->bitmap;
1419 if (!bitmap) /* there was no bitmap */
1422 mddev->bitmap = NULL; /* disconnect from the md device */
1424 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1426 bitmap_free(bitmap);
1430 * initialize the bitmap structure
1431 * if this returns an error, bitmap_destroy must be called to do clean up
1433 int bitmap_create(mddev_t *mddev)
1435 struct bitmap *bitmap;
1436 unsigned long blocks = mddev->resync_max_sectors;
1437 unsigned long chunks;
1438 unsigned long pages;
1439 struct file *file = mddev->bitmap_file;
1443 BUG_ON(sizeof(bitmap_super_t) != 256);
1445 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
1448 BUG_ON(file && mddev->bitmap_offset);
1450 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1454 spin_lock_init(&bitmap->lock);
1455 bitmap->mddev = mddev;
1457 spin_lock_init(&bitmap->write_lock);
1458 INIT_LIST_HEAD(&bitmap->complete_pages);
1459 bitmap->write_pool = mempool_create_kmalloc_pool(WRITE_POOL_SIZE,
1460 sizeof(struct page_list));
1462 if (!bitmap->write_pool)
1465 bitmap->file = file;
1466 bitmap->offset = mddev->bitmap_offset;
1467 if (file) get_file(file);
1468 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1469 err = bitmap_read_sb(bitmap);
1473 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1474 sizeof(bitmap->chunksize));
1476 /* now that chunksize and chunkshift are set, we can use these macros */
1477 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1478 CHUNK_BLOCK_RATIO(bitmap);
1479 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1483 bitmap->chunks = chunks;
1484 bitmap->pages = pages;
1485 bitmap->missing_pages = pages;
1486 bitmap->counter_bits = COUNTER_BITS;
1488 bitmap->syncchunk = ~0UL;
1490 #ifdef INJECT_FATAL_FAULT_1
1493 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1499 bitmap->flags |= BITMAP_ACTIVE;
1501 /* now that we have some pages available, initialize the in-memory
1502 * bitmap from the on-disk bitmap */
1504 if (mddev->degraded == 0
1505 || bitmap->events_cleared == mddev->events)
1506 /* no need to keep dirty bits to optimise a re-add of a missing device */
1507 start = mddev->recovery_cp;
1508 err = bitmap_init_from_disk(bitmap, start);
1513 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1514 pages, bmname(bitmap));
1516 mddev->bitmap = bitmap;
1518 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1520 return bitmap_update_sb(bitmap);
1523 bitmap_free(bitmap);
1527 /* the bitmap API -- for raid personalities */
1528 EXPORT_SYMBOL(bitmap_startwrite);
1529 EXPORT_SYMBOL(bitmap_endwrite);
1530 EXPORT_SYMBOL(bitmap_start_sync);
1531 EXPORT_SYMBOL(bitmap_end_sync);
1532 EXPORT_SYMBOL(bitmap_unplug);
1533 EXPORT_SYMBOL(bitmap_close_sync);
1534 EXPORT_SYMBOL(bitmap_daemon_work);