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)
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
264 static int write_page(struct bitmap *bitmap, struct page *page, int wait)
270 ret = page->mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
272 ret = page->mapping->a_ops->commit_write(NULL, page, 0,
279 set_page_dirty(page); /* force it to be written out */
282 /* add to list to be waited for by daemon */
283 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
285 page_cache_get(page);
286 spin_lock(&bitmap->write_lock);
287 list_add(&item->list, &bitmap->complete_pages);
288 spin_unlock(&bitmap->write_lock);
289 md_wakeup_thread(bitmap->writeback_daemon);
291 return write_one_page(page, wait);
294 /* read a page from a file, pinning it into cache, and return bytes_read */
295 static struct page *read_page(struct file *file, unsigned long index,
296 unsigned long *bytes_read)
298 struct inode *inode = file->f_mapping->host;
299 struct page *page = NULL;
300 loff_t isize = i_size_read(inode);
301 unsigned long end_index = isize >> PAGE_CACHE_SHIFT;
303 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_CACHE_SIZE,
304 (unsigned long long)index << PAGE_CACHE_SHIFT);
306 page = read_cache_page(inode->i_mapping, index,
307 (filler_t *)inode->i_mapping->a_ops->readpage, file);
310 wait_on_page_locked(page);
311 if (!PageUptodate(page) || PageError(page)) {
312 page_cache_release(page);
313 page = ERR_PTR(-EIO);
317 if (index > end_index) /* we have read beyond EOF */
319 else if (index == end_index) /* possible short read */
320 *bytes_read = isize & ~PAGE_CACHE_MASK;
322 *bytes_read = PAGE_CACHE_SIZE; /* got a full page */
325 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
326 (int)PAGE_CACHE_SIZE,
327 (unsigned long long)index << PAGE_CACHE_SHIFT,
333 * bitmap file superblock operations
336 /* update the event counter and sync the superblock to disk */
337 int bitmap_update_sb(struct bitmap *bitmap)
342 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
344 spin_lock_irqsave(&bitmap->lock, flags);
345 if (!bitmap->sb_page) { /* no superblock */
346 spin_unlock_irqrestore(&bitmap->lock, flags);
349 spin_unlock_irqrestore(&bitmap->lock, flags);
350 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
351 sb->events = cpu_to_le64(bitmap->mddev->events);
352 if (!bitmap->mddev->degraded)
353 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
354 kunmap(bitmap->sb_page);
355 return write_page(bitmap, bitmap->sb_page, 0);
358 /* print out the bitmap file superblock */
359 void bitmap_print_sb(struct bitmap *bitmap)
363 if (!bitmap || !bitmap->sb_page)
365 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
366 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
367 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
368 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
369 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
370 *(__u32 *)(sb->uuid+0),
371 *(__u32 *)(sb->uuid+4),
372 *(__u32 *)(sb->uuid+8),
373 *(__u32 *)(sb->uuid+12));
374 printk(KERN_DEBUG " events: %llu\n",
375 (unsigned long long) le64_to_cpu(sb->events));
376 printk(KERN_DEBUG "events cleared: %llu\n",
377 (unsigned long long) le64_to_cpu(sb->events_cleared));
378 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
379 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
380 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
381 printk(KERN_DEBUG " sync size: %llu KB\n",
382 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
383 kunmap(bitmap->sb_page);
386 /* read the superblock from the bitmap file and initialize some bitmap fields */
387 static int bitmap_read_sb(struct bitmap *bitmap)
391 unsigned long chunksize, daemon_sleep;
392 unsigned long bytes_read;
393 unsigned long long events;
396 /* page 0 is the superblock, read it... */
397 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
398 if (IS_ERR(bitmap->sb_page)) {
399 err = PTR_ERR(bitmap->sb_page);
400 bitmap->sb_page = NULL;
404 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
406 if (bytes_read < sizeof(*sb)) { /* short read */
407 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
413 chunksize = le32_to_cpu(sb->chunksize);
414 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
416 /* verify that the bitmap-specific fields are valid */
417 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
418 reason = "bad magic";
419 else if (sb->version != cpu_to_le32(BITMAP_MAJOR))
420 reason = "unrecognized superblock version";
421 else if (chunksize < 512 || chunksize > (1024 * 1024 * 4))
422 reason = "bitmap chunksize out of range (512B - 4MB)";
423 else if ((1 << ffz(~chunksize)) != chunksize)
424 reason = "bitmap chunksize not a power of 2";
425 else if (daemon_sleep < 1 || daemon_sleep > 15)
426 reason = "daemon sleep period out of range";
428 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
429 bmname(bitmap), reason);
433 /* keep the array size field of the bitmap superblock up to date */
434 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
436 if (!bitmap->mddev->persistent)
440 * if we have a persistent array superblock, compare the
441 * bitmap's UUID and event counter to the mddev's
443 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
444 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
448 events = le64_to_cpu(sb->events);
449 if (events < bitmap->mddev->events) {
450 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
451 "-- forcing full recovery\n", bmname(bitmap), events,
452 (unsigned long long) bitmap->mddev->events);
453 sb->state |= BITMAP_STALE;
456 /* assign fields using values from superblock */
457 bitmap->chunksize = chunksize;
458 bitmap->daemon_sleep = daemon_sleep;
459 bitmap->flags |= sb->state;
460 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
463 kunmap(bitmap->sb_page);
465 bitmap_print_sb(bitmap);
469 enum bitmap_mask_op {
474 /* record the state of the bitmap in the superblock */
475 static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
476 enum bitmap_mask_op op)
481 spin_lock_irqsave(&bitmap->lock, flags);
482 if (!bitmap || !bitmap->sb_page) { /* can't set the state */
483 spin_unlock_irqrestore(&bitmap->lock, flags);
486 page_cache_get(bitmap->sb_page);
487 spin_unlock_irqrestore(&bitmap->lock, flags);
488 sb = (bitmap_super_t *)kmap(bitmap->sb_page);
490 case MASK_SET: sb->state |= bits;
492 case MASK_UNSET: sb->state &= ~bits;
496 kunmap(bitmap->sb_page);
497 page_cache_release(bitmap->sb_page);
501 * general bitmap file operations
504 /* calculate the index of the page that contains this bit */
505 static inline unsigned long file_page_index(unsigned long chunk)
507 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
510 /* calculate the (bit) offset of this bit within a page */
511 static inline unsigned long file_page_offset(unsigned long chunk)
513 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
517 * return a pointer to the page in the filemap that contains the given bit
519 * this lookup is complicated by the fact that the bitmap sb might be exactly
520 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
523 static inline struct page *filemap_get_page(struct bitmap *bitmap,
526 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
530 static void bitmap_file_unmap(struct bitmap *bitmap)
532 struct page **map, *sb_page;
537 spin_lock_irqsave(&bitmap->lock, flags);
538 map = bitmap->filemap;
539 bitmap->filemap = NULL;
540 attr = bitmap->filemap_attr;
541 bitmap->filemap_attr = NULL;
542 pages = bitmap->file_pages;
543 bitmap->file_pages = 0;
544 sb_page = bitmap->sb_page;
545 bitmap->sb_page = NULL;
546 spin_unlock_irqrestore(&bitmap->lock, flags);
549 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
550 page_cache_release(map[pages]);
555 page_cache_release(sb_page);
558 static void bitmap_stop_daemons(struct bitmap *bitmap);
560 /* dequeue the next item in a page list -- don't call from irq context */
561 static struct page_list *dequeue_page(struct bitmap *bitmap)
563 struct page_list *item = NULL;
564 struct list_head *head = &bitmap->complete_pages;
566 spin_lock(&bitmap->write_lock);
567 if (list_empty(head))
569 item = list_entry(head->prev, struct page_list, list);
570 list_del(head->prev);
572 spin_unlock(&bitmap->write_lock);
576 static void drain_write_queues(struct bitmap *bitmap)
578 struct page_list *item;
580 while ((item = dequeue_page(bitmap))) {
581 /* don't bother to wait */
582 page_cache_release(item->page);
583 mempool_free(item, bitmap->write_pool);
586 wake_up(&bitmap->write_wait);
589 static void bitmap_file_put(struct bitmap *bitmap)
595 spin_lock_irqsave(&bitmap->lock, flags);
598 spin_unlock_irqrestore(&bitmap->lock, flags);
600 bitmap_stop_daemons(bitmap);
602 drain_write_queues(bitmap);
604 bitmap_file_unmap(bitmap);
607 inode = file->f_mapping->host;
608 spin_lock(&inode->i_lock);
609 atomic_set(&inode->i_writecount, 1); /* allow writes again */
610 spin_unlock(&inode->i_lock);
617 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
618 * then it is no longer reliable, so we stop using it and we mark the file
619 * as failed in the superblock
621 static void bitmap_file_kick(struct bitmap *bitmap)
623 char *path, *ptr = NULL;
625 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
626 bitmap_update_sb(bitmap);
628 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
630 ptr = file_path(bitmap->file, path, PAGE_SIZE);
632 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
633 bmname(bitmap), ptr ? ptr : "");
637 bitmap_file_put(bitmap);
642 enum bitmap_page_attr {
643 BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
644 BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
645 BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
648 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
649 enum bitmap_page_attr attr)
651 bitmap->filemap_attr[page->index] |= attr;
654 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
655 enum bitmap_page_attr attr)
657 bitmap->filemap_attr[page->index] &= ~attr;
660 static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
662 return bitmap->filemap_attr[page->index];
666 * bitmap_file_set_bit -- called before performing a write to the md device
667 * to set (and eventually sync) a particular bit in the bitmap file
669 * we set the bit immediately, then we record the page number so that
670 * when an unplug occurs, we can flush the dirty pages out to disk
672 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
677 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
679 if (!bitmap->file || !bitmap->filemap) {
683 page = filemap_get_page(bitmap, chunk);
684 bit = file_page_offset(chunk);
687 /* make sure the page stays cached until it gets written out */
688 if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
689 page_cache_get(page);
692 kaddr = kmap_atomic(page, KM_USER0);
694 kunmap_atomic(kaddr, KM_USER0);
695 PRINTK("set file bit %lu page %lu\n", bit, page->index);
697 /* record page number so it gets flushed to disk when unplug occurs */
698 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
702 /* this gets called when the md device is ready to unplug its underlying
703 * (slave) device queues -- before we let any writes go down, we need to
704 * sync the dirty pages of the bitmap file to disk */
705 int bitmap_unplug(struct bitmap *bitmap)
707 unsigned long i, attr, flags;
714 /* look at each page to see if there are any set bits that need to be
715 * flushed out to disk */
716 for (i = 0; i < bitmap->file_pages; i++) {
717 spin_lock_irqsave(&bitmap->lock, flags);
718 if (!bitmap->file || !bitmap->filemap) {
719 spin_unlock_irqrestore(&bitmap->lock, flags);
722 page = bitmap->filemap[i];
723 attr = get_page_attr(bitmap, page);
724 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
725 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
726 if ((attr & BITMAP_PAGE_DIRTY))
728 spin_unlock_irqrestore(&bitmap->lock, flags);
730 if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE))
731 if (write_page(bitmap, page, 0))
734 if (wait) { /* if any writes were performed, we need to wait on them */
735 spin_lock_irq(&bitmap->write_lock);
736 wait_event_lock_irq(bitmap->write_wait,
737 list_empty(&bitmap->complete_pages), bitmap->write_lock,
738 wake_up_process(bitmap->writeback_daemon->tsk));
739 spin_unlock_irq(&bitmap->write_lock);
744 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset,
745 unsigned long sectors, int in_sync);
746 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
747 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
748 * memory mapping of the bitmap file
750 * if there's no bitmap file, or if the bitmap file had been
751 * previously kicked from the array, we mark all the bits as
752 * 1's in order to cause a full resync.
754 static int bitmap_init_from_disk(struct bitmap *bitmap, int in_sync)
756 unsigned long i, chunks, index, oldindex, bit;
757 struct page *page = NULL, *oldpage = NULL;
758 unsigned long num_pages, bit_cnt = 0;
760 unsigned long bytes, offset, dummy;
764 chunks = bitmap->chunks;
772 outofdate = bitmap->flags & BITMAP_STALE;
775 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
776 "recovery\n", bmname(bitmap));
778 bytes = (chunks + 7) / 8;
780 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
782 if (i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
783 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
785 (unsigned long) i_size_read(file->f_mapping->host),
786 bytes + sizeof(bitmap_super_t));
792 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
793 if (!bitmap->filemap)
796 bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL);
797 if (!bitmap->filemap_attr)
800 memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages);
804 for (i = 0; i < chunks; i++) {
805 index = file_page_index(i);
806 bit = file_page_offset(i);
807 if (index != oldindex) { /* this is a new page, read it in */
808 /* unmap the old page, we're done with it */
813 * if we're here then the superblock page
814 * contains some bits (PAGE_SIZE != sizeof sb)
815 * we've already read it in, so just use it
817 page = bitmap->sb_page;
818 offset = sizeof(bitmap_super_t);
820 page = read_page(file, index, &dummy);
821 if (IS_ERR(page)) { /* read error */
833 * if bitmap is out of date, dirty the
834 * whole page and write it out
836 memset(page_address(page) + offset, 0xff,
838 ret = write_page(bitmap, page, 1);
841 /* release, page not in filemap yet */
842 page_cache_release(page);
847 bitmap->filemap[bitmap->file_pages++] = page;
849 if (test_bit(bit, page_address(page))) {
850 /* if the disk bit is set, set the memory bit */
851 bitmap_set_memory_bits(bitmap,
852 i << CHUNK_BLOCK_SHIFT(bitmap), 1, in_sync);
857 /* everything went OK */
859 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
861 if (page) /* unmap the last page */
864 if (bit_cnt) { /* Kick recovery if any bits were set */
865 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
866 md_wakeup_thread(bitmap->mddev->thread);
870 printk(KERN_INFO "%s: bitmap initialized from disk: "
871 "read %lu/%lu pages, set %lu bits, status: %d\n",
872 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
878 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
880 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
881 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
882 bitmap->bp[page].count += inc;
884 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
885 (unsigned long long)offset, inc, bitmap->bp[page].count);
887 bitmap_checkfree(bitmap, page);
889 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
890 sector_t offset, int *blocks,
894 * bitmap daemon -- periodically wakes up to clean bits and flush pages
898 int bitmap_daemon_work(struct bitmap *bitmap)
902 struct page *page = NULL, *lastpage = NULL;
909 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
911 bitmap->daemon_lastrun = jiffies;
913 for (j = 0; j < bitmap->chunks; j++) {
914 bitmap_counter_t *bmc;
915 spin_lock_irqsave(&bitmap->lock, flags);
916 if (!bitmap->file || !bitmap->filemap) {
917 /* error or shutdown */
918 spin_unlock_irqrestore(&bitmap->lock, flags);
922 page = filemap_get_page(bitmap, j);
924 if (page != lastpage) {
925 /* skip this page unless it's marked as needing cleaning */
926 if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
927 if (attr & BITMAP_PAGE_NEEDWRITE) {
928 page_cache_get(page);
929 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
931 spin_unlock_irqrestore(&bitmap->lock, flags);
932 if (attr & BITMAP_PAGE_NEEDWRITE) {
933 if (write_page(bitmap, page, 0))
934 bitmap_file_kick(bitmap);
935 page_cache_release(page);
940 /* grab the new page, sync and release the old */
941 page_cache_get(page);
942 if (lastpage != NULL) {
943 if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
944 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
945 spin_unlock_irqrestore(&bitmap->lock, flags);
946 err = write_page(bitmap, lastpage, 0);
948 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
949 spin_unlock_irqrestore(&bitmap->lock, flags);
952 page_cache_release(lastpage);
954 bitmap_file_kick(bitmap);
956 spin_unlock_irqrestore(&bitmap->lock, flags);
960 printk("bitmap clean at page %lu\n", j);
962 spin_lock_irqsave(&bitmap->lock, flags);
963 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
965 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
969 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
972 *bmc=1; /* maybe clear the bit next time */
973 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
974 } else if (*bmc == 1) {
975 /* we can clear the bit */
977 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
981 clear_bit(file_page_offset(j), page_address(page));
984 spin_unlock_irqrestore(&bitmap->lock, flags);
987 /* now sync the final page */
988 if (lastpage != NULL) {
990 spin_lock_irqsave(&bitmap->lock, flags);
991 if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
992 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
993 spin_unlock_irqrestore(&bitmap->lock, flags);
994 err = write_page(bitmap, lastpage, 0);
996 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
997 spin_unlock_irqrestore(&bitmap->lock, flags);
1000 page_cache_release(lastpage);
1006 static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
1009 unsigned long flags;
1011 /* if no one is waiting on us, we'll free the md thread struct
1012 * and exit, otherwise we let the waiter clean things up */
1013 spin_lock_irqsave(&bitmap->lock, flags);
1014 if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
1016 spin_unlock_irqrestore(&bitmap->lock, flags);
1018 complete_and_exit(NULL, 0); /* do_exit not exported */
1020 spin_unlock_irqrestore(&bitmap->lock, flags);
1023 static void bitmap_writeback_daemon(mddev_t *mddev)
1025 struct bitmap *bitmap = mddev->bitmap;
1027 struct page_list *item;
1030 if (signal_pending(current)) {
1032 "%s: bitmap writeback daemon got signal, exiting...\n",
1038 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1039 /* wait on bitmap page writebacks */
1040 while ((item = dequeue_page(bitmap))) {
1042 mempool_free(item, bitmap->write_pool);
1043 PRINTK("wait on page writeback: %p\n", page);
1044 wait_on_page_writeback(page);
1045 PRINTK("finished page writeback: %p\n", page);
1047 err = PageError(page);
1048 page_cache_release(page);
1050 printk(KERN_WARNING "%s: bitmap file writeback "
1051 "failed (page %lu): %d\n",
1052 bmname(bitmap), page->index, err);
1053 bitmap_file_kick(bitmap);
1058 wake_up(&bitmap->write_wait);
1060 printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
1061 bmname(bitmap), err);
1062 daemon_exit(bitmap, &bitmap->writeback_daemon);
1066 static int bitmap_start_daemon(struct bitmap *bitmap, mdk_thread_t **ptr,
1067 void (*func)(mddev_t *), char *name)
1069 mdk_thread_t *daemon;
1070 unsigned long flags;
1073 spin_lock_irqsave(&bitmap->lock, flags);
1075 if (!bitmap->file) /* no need for daemon if there's no backing file */
1078 spin_unlock_irqrestore(&bitmap->lock, flags);
1080 #if INJECT_FATAL_FAULT_2
1083 sprintf(namebuf, "%%s_%s", name);
1084 daemon = md_register_thread(func, bitmap->mddev, namebuf);
1087 printk(KERN_ERR "%s: failed to start bitmap daemon\n",
1092 spin_lock_irqsave(&bitmap->lock, flags);
1095 md_wakeup_thread(daemon); /* start it running */
1097 PRINTK("%s: %s daemon (pid %d) started...\n",
1098 bmname(bitmap), name, daemon->tsk->pid);
1100 spin_unlock_irqrestore(&bitmap->lock, flags);
1104 static int bitmap_start_daemons(struct bitmap *bitmap)
1106 int err = bitmap_start_daemon(bitmap, &bitmap->writeback_daemon,
1107 bitmap_writeback_daemon, "bitmap_wb");
1111 static void bitmap_stop_daemon(struct bitmap *bitmap, mdk_thread_t **ptr)
1113 mdk_thread_t *daemon;
1114 unsigned long flags;
1116 spin_lock_irqsave(&bitmap->lock, flags);
1119 spin_unlock_irqrestore(&bitmap->lock, flags);
1121 md_unregister_thread(daemon); /* destroy the thread */
1124 static void bitmap_stop_daemons(struct bitmap *bitmap)
1126 /* the daemons can't stop themselves... they'll just exit instead... */
1127 if (bitmap->writeback_daemon &&
1128 current->pid != bitmap->writeback_daemon->tsk->pid)
1129 bitmap_stop_daemon(bitmap, &bitmap->writeback_daemon);
1132 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1133 sector_t offset, int *blocks,
1136 /* If 'create', we might release the lock and reclaim it.
1137 * The lock must have been taken with interrupts enabled.
1138 * If !create, we don't release the lock.
1140 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1141 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1142 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1145 if (bitmap_checkpage(bitmap, page, create) < 0) {
1146 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1147 *blocks = csize - (offset & (csize- 1));
1150 /* now locked ... */
1152 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1153 /* should we use the first or second counter field
1154 * of the hijacked pointer? */
1155 int hi = (pageoff > PAGE_COUNTER_MASK);
1156 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1157 PAGE_COUNTER_SHIFT - 1);
1158 *blocks = csize - (offset & (csize- 1));
1159 return &((bitmap_counter_t *)
1160 &bitmap->bp[page].map)[hi];
1161 } else { /* page is allocated */
1162 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1163 *blocks = csize - (offset & (csize- 1));
1164 return (bitmap_counter_t *)
1165 &(bitmap->bp[page].map[pageoff]);
1169 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors)
1171 if (!bitmap) return 0;
1174 bitmap_counter_t *bmc;
1176 spin_lock_irq(&bitmap->lock);
1177 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1179 spin_unlock_irq(&bitmap->lock);
1185 bitmap_file_set_bit(bitmap, offset);
1186 bitmap_count_page(bitmap,offset, 1);
1187 blk_plug_device(bitmap->mddev->queue);
1192 if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
1195 spin_unlock_irq(&bitmap->lock);
1198 if (sectors > blocks)
1205 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1208 if (!bitmap) return;
1211 unsigned long flags;
1212 bitmap_counter_t *bmc;
1214 spin_lock_irqsave(&bitmap->lock, flags);
1215 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1217 spin_unlock_irqrestore(&bitmap->lock, flags);
1221 if (!success && ! (*bmc & NEEDED_MASK))
1222 *bmc |= NEEDED_MASK;
1226 set_page_attr(bitmap,
1227 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1230 spin_unlock_irqrestore(&bitmap->lock, flags);
1232 if (sectors > blocks)
1238 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks)
1240 bitmap_counter_t *bmc;
1242 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1244 return 1; /* always resync if no bitmap */
1246 spin_lock_irq(&bitmap->lock);
1247 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1253 else if (NEEDED(*bmc)) {
1255 *bmc |= RESYNC_MASK;
1256 *bmc &= ~NEEDED_MASK;
1259 spin_unlock_irq(&bitmap->lock);
1263 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1265 bitmap_counter_t *bmc;
1266 unsigned long flags;
1268 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1269 */ if (bitmap == NULL) {
1273 spin_lock_irqsave(&bitmap->lock, flags);
1274 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1279 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1282 *bmc &= ~RESYNC_MASK;
1284 if (!NEEDED(*bmc) && aborted)
1285 *bmc |= NEEDED_MASK;
1288 set_page_attr(bitmap,
1289 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1295 spin_unlock_irqrestore(&bitmap->lock, flags);
1298 void bitmap_close_sync(struct bitmap *bitmap)
1300 /* Sync has finished, and any bitmap chunks that weren't synced
1301 * properly have been aborted. It remains to us to clear the
1302 * RESYNC bit wherever it is still on
1304 sector_t sector = 0;
1306 if (!bitmap) return;
1307 while (sector < bitmap->mddev->resync_max_sectors) {
1308 bitmap_end_sync(bitmap, sector, &blocks, 0);
1310 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1311 (unsigned long long)sector, blocks);
1312 */ sector += blocks;
1316 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset,
1317 unsigned long sectors, int in_sync)
1319 /* For each chunk covered by any of these sectors, set the
1320 * counter to 1 and set resync_needed unless in_sync. They should all
1321 * be 0 at this point
1325 bitmap_counter_t *bmc;
1326 spin_lock_irq(&bitmap->lock);
1327 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1329 spin_unlock_irq(&bitmap->lock);
1334 *bmc = 1 | (in_sync? 0 : NEEDED_MASK);
1335 bitmap_count_page(bitmap, offset, 1);
1336 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1337 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1339 spin_unlock_irq(&bitmap->lock);
1347 /* dirty the entire bitmap */
1348 int bitmap_setallbits(struct bitmap *bitmap)
1350 unsigned long flags;
1353 /* dirty the in-memory bitmap */
1354 bitmap_set_memory_bits(bitmap, 0, bitmap->chunks << CHUNK_BLOCK_SHIFT(bitmap), 1);
1356 /* dirty the bitmap file */
1357 for (j = 0; j < bitmap->file_pages; j++) {
1358 struct page *page = bitmap->filemap[j];
1360 spin_lock_irqsave(&bitmap->lock, flags);
1361 page_cache_get(page);
1362 spin_unlock_irqrestore(&bitmap->lock, flags);
1363 memset(kmap(page), 0xff, PAGE_SIZE);
1365 if (write_page(bitmap, page, 0))
1373 * free memory that was allocated
1375 void bitmap_destroy(mddev_t *mddev)
1377 unsigned long k, pages;
1378 struct bitmap_page *bp;
1379 struct bitmap *bitmap = mddev->bitmap;
1381 if (!bitmap) /* there was no bitmap */
1384 mddev->bitmap = NULL; /* disconnect from the md device */
1386 /* release the bitmap file and kill the daemon */
1387 bitmap_file_put(bitmap);
1390 pages = bitmap->pages;
1392 /* free all allocated memory */
1394 mempool_destroy(bitmap->write_pool);
1396 if (bp) /* deallocate the page memory */
1397 for (k = 0; k < pages; k++)
1398 if (bp[k].map && !bp[k].hijacked)
1405 * initialize the bitmap structure
1406 * if this returns an error, bitmap_destroy must be called to do clean up
1408 int bitmap_create(mddev_t *mddev)
1410 struct bitmap *bitmap;
1411 unsigned long blocks = mddev->resync_max_sectors;
1412 unsigned long chunks;
1413 unsigned long pages;
1414 struct file *file = mddev->bitmap_file;
1417 BUG_ON(sizeof(bitmap_super_t) != 256);
1419 if (!file) /* bitmap disabled, nothing to do */
1422 bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
1426 memset(bitmap, 0, sizeof(*bitmap));
1428 spin_lock_init(&bitmap->lock);
1429 bitmap->mddev = mddev;
1430 mddev->bitmap = bitmap;
1432 spin_lock_init(&bitmap->write_lock);
1433 INIT_LIST_HEAD(&bitmap->complete_pages);
1434 init_waitqueue_head(&bitmap->write_wait);
1435 bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
1436 write_pool_free, NULL);
1437 if (!bitmap->write_pool)
1440 bitmap->file = file;
1442 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1443 err = bitmap_read_sb(bitmap);
1447 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1448 sizeof(bitmap->chunksize));
1450 /* now that chunksize and chunkshift are set, we can use these macros */
1451 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1452 CHUNK_BLOCK_RATIO(bitmap);
1453 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1457 bitmap->chunks = chunks;
1458 bitmap->pages = pages;
1459 bitmap->missing_pages = pages;
1460 bitmap->counter_bits = COUNTER_BITS;
1462 bitmap->syncchunk = ~0UL;
1464 #if INJECT_FATAL_FAULT_1
1467 bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1471 memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp));
1473 bitmap->flags |= BITMAP_ACTIVE;
1475 /* now that we have some pages available, initialize the in-memory
1476 * bitmap from the on-disk bitmap */
1477 err = bitmap_init_from_disk(bitmap, mddev->recovery_cp == MaxSector);
1481 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1482 pages, bmname(bitmap));
1484 /* kick off the bitmap daemons */
1485 err = bitmap_start_daemons(bitmap);
1488 return bitmap_update_sb(bitmap);
1491 /* the bitmap API -- for raid personalities */
1492 EXPORT_SYMBOL(bitmap_startwrite);
1493 EXPORT_SYMBOL(bitmap_endwrite);
1494 EXPORT_SYMBOL(bitmap_start_sync);
1495 EXPORT_SYMBOL(bitmap_end_sync);
1496 EXPORT_SYMBOL(bitmap_unplug);
1497 EXPORT_SYMBOL(bitmap_close_sync);
1498 EXPORT_SYMBOL(bitmap_daemon_work);