md/bitmap: remove unnecessary page reference manipulations from md/bitmap code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / bitmap.c
blobfa53579b544fd143e9be93a6b7d97d42d642a108
1 /*
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)
14 * Still to do:
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/errno.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/config.h>
28 #include <linux/timer.h>
29 #include <linux/sched.h>
30 #include <linux/list.h>
31 #include <linux/file.h>
32 #include <linux/mount.h>
33 #include <linux/buffer_head.h>
34 #include <linux/raid/md.h>
35 #include <linux/raid/bitmap.h>
37 /* debug macros */
39 #define DEBUG 0
41 #if DEBUG
42 /* these are for debugging purposes only! */
44 /* define one and only one of these */
45 #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
46 #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
47 #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
48 #define INJECT_FAULTS_4 0 /* undef */
49 #define INJECT_FAULTS_5 0 /* undef */
50 #define INJECT_FAULTS_6 0
52 /* if these are defined, the driver will fail! debug only */
53 #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
54 #define INJECT_FATAL_FAULT_2 0 /* undef */
55 #define INJECT_FATAL_FAULT_3 0 /* undef */
56 #endif
58 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
59 #define DPRINTK(x...) do { } while(0)
61 #ifndef PRINTK
62 # if DEBUG > 0
63 # define PRINTK(x...) printk(KERN_DEBUG x)
64 # else
65 # define PRINTK(x...)
66 # endif
67 #endif
69 static inline char * bmname(struct bitmap *bitmap)
71 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
76 * test if the bitmap is active
78 int bitmap_active(struct bitmap *bitmap)
80 unsigned long flags;
81 int res = 0;
83 if (!bitmap)
84 return res;
85 spin_lock_irqsave(&bitmap->lock, flags);
86 res = bitmap->flags & BITMAP_ACTIVE;
87 spin_unlock_irqrestore(&bitmap->lock, flags);
88 return res;
91 #define WRITE_POOL_SIZE 256
92 /* mempool for queueing pending writes on the bitmap file */
93 static void *write_pool_alloc(gfp_t gfp_flags, void *data)
95 return kmalloc(sizeof(struct page_list), gfp_flags);
98 static void write_pool_free(void *ptr, void *data)
100 kfree(ptr);
104 * just a placeholder - calls kmalloc for bitmap pages
106 static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
108 unsigned char *page;
110 #ifdef INJECT_FAULTS_1
111 page = NULL;
112 #else
113 page = kmalloc(PAGE_SIZE, GFP_NOIO);
114 #endif
115 if (!page)
116 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
117 else
118 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
119 bmname(bitmap), page);
120 return page;
124 * for now just a placeholder -- just calls kfree for bitmap pages
126 static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
128 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
129 kfree(page);
133 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
135 * 1) check to see if this page is allocated, if it's not then try to alloc
136 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
137 * page pointer directly as a counter
139 * if we find our page, we increment the page's refcount so that it stays
140 * allocated while we're using it
142 static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
144 unsigned char *mappage;
146 if (page >= bitmap->pages) {
147 printk(KERN_ALERT
148 "%s: invalid bitmap page request: %lu (> %lu)\n",
149 bmname(bitmap), page, bitmap->pages-1);
150 return -EINVAL;
154 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
155 return 0;
157 if (bitmap->bp[page].map) /* page is already allocated, just return */
158 return 0;
160 if (!create)
161 return -ENOENT;
163 spin_unlock_irq(&bitmap->lock);
165 /* this page has not been allocated yet */
167 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
168 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
169 bmname(bitmap));
170 /* failed - set the hijacked flag so that we can use the
171 * pointer as a counter */
172 spin_lock_irq(&bitmap->lock);
173 if (!bitmap->bp[page].map)
174 bitmap->bp[page].hijacked = 1;
175 goto out;
178 /* got a page */
180 spin_lock_irq(&bitmap->lock);
182 /* recheck the page */
184 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
185 /* somebody beat us to getting the page */
186 bitmap_free_page(bitmap, mappage);
187 return 0;
190 /* no page was in place and we have one, so install it */
192 memset(mappage, 0, PAGE_SIZE);
193 bitmap->bp[page].map = mappage;
194 bitmap->missing_pages--;
195 out:
196 return 0;
200 /* if page is completely empty, put it back on the free list, or dealloc it */
201 /* if page was hijacked, unmark the flag so it might get alloced next time */
202 /* Note: lock should be held when calling this */
203 static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
205 char *ptr;
207 if (bitmap->bp[page].count) /* page is still busy */
208 return;
210 /* page is no longer in use, it can be released */
212 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
213 bitmap->bp[page].hijacked = 0;
214 bitmap->bp[page].map = NULL;
215 return;
218 /* normal case, free the page */
220 #if 0
221 /* actually ... let's not. We will probably need the page again exactly when
222 * memory is tight and we are flusing to disk
224 return;
225 #else
226 ptr = bitmap->bp[page].map;
227 bitmap->bp[page].map = NULL;
228 bitmap->missing_pages++;
229 bitmap_free_page(bitmap, ptr);
230 return;
231 #endif
236 * bitmap file handling - read and write the bitmap file and its superblock
239 /* copy the pathname of a file to a buffer */
240 char *file_path(struct file *file, char *buf, int count)
242 struct dentry *d;
243 struct vfsmount *v;
245 if (!buf)
246 return NULL;
248 d = file->f_dentry;
249 v = file->f_vfsmnt;
251 buf = d_path(d, v, buf, count);
253 return IS_ERR(buf) ? NULL : buf;
257 * basic page I/O operations
260 /* IO operations when bitmap is stored near all superblocks */
261 static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
263 /* choose a good rdev and read the page from there */
265 mdk_rdev_t *rdev;
266 struct list_head *tmp;
267 struct page *page = alloc_page(GFP_KERNEL);
268 sector_t target;
270 if (!page)
271 return ERR_PTR(-ENOMEM);
273 ITERATE_RDEV(mddev, rdev, tmp) {
274 if (! test_bit(In_sync, &rdev->flags)
275 || test_bit(Faulty, &rdev->flags))
276 continue;
278 target = (rdev->sb_offset << 1) + offset + index * (PAGE_SIZE/512);
280 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
281 page->index = index;
282 return page;
285 return ERR_PTR(-EIO);
289 static int write_sb_page(mddev_t *mddev, long offset, struct page *page, int wait)
291 mdk_rdev_t *rdev;
292 struct list_head *tmp;
294 ITERATE_RDEV(mddev, rdev, tmp)
295 if (test_bit(In_sync, &rdev->flags)
296 && !test_bit(Faulty, &rdev->flags))
297 md_super_write(mddev, rdev,
298 (rdev->sb_offset<<1) + offset
299 + page->index * (PAGE_SIZE/512),
300 PAGE_SIZE,
301 page);
303 if (wait)
304 md_super_wait(mddev);
305 return 0;
309 * write out a page to a file
311 static int write_page(struct bitmap *bitmap, struct page *page, int wait)
313 int ret = -ENOMEM;
315 if (bitmap->file == NULL)
316 return write_sb_page(bitmap->mddev, bitmap->offset, page, wait);
318 flush_dcache_page(page); /* make sure visible to anyone reading the file */
320 if (wait)
321 lock_page(page);
322 else {
323 if (TestSetPageLocked(page))
324 return -EAGAIN; /* already locked */
325 if (PageWriteback(page)) {
326 unlock_page(page);
327 return -EAGAIN;
331 ret = page->mapping->a_ops->prepare_write(bitmap->file, page, 0, PAGE_SIZE);
332 if (!ret)
333 ret = page->mapping->a_ops->commit_write(bitmap->file, page, 0,
334 PAGE_SIZE);
335 if (ret) {
336 unlock_page(page);
337 return ret;
340 set_page_dirty(page); /* force it to be written out */
342 if (!wait) {
343 /* add to list to be waited for by daemon */
344 struct page_list *item = mempool_alloc(bitmap->write_pool, GFP_NOIO);
345 item->page = page;
346 spin_lock(&bitmap->write_lock);
347 list_add(&item->list, &bitmap->complete_pages);
348 spin_unlock(&bitmap->write_lock);
349 md_wakeup_thread(bitmap->writeback_daemon);
351 return write_one_page(page, wait);
354 /* read a page from a file, pinning it into cache, and return bytes_read */
355 static struct page *read_page(struct file *file, unsigned long index,
356 unsigned long *bytes_read)
358 struct inode *inode = file->f_mapping->host;
359 struct page *page = NULL;
360 loff_t isize = i_size_read(inode);
361 unsigned long end_index = isize >> PAGE_SHIFT;
363 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
364 (unsigned long long)index << PAGE_SHIFT);
366 page = read_cache_page(inode->i_mapping, index,
367 (filler_t *)inode->i_mapping->a_ops->readpage, file);
368 if (IS_ERR(page))
369 goto out;
370 wait_on_page_locked(page);
371 if (!PageUptodate(page) || PageError(page)) {
372 put_page(page);
373 page = ERR_PTR(-EIO);
374 goto out;
377 if (index > end_index) /* we have read beyond EOF */
378 *bytes_read = 0;
379 else if (index == end_index) /* possible short read */
380 *bytes_read = isize & ~PAGE_MASK;
381 else
382 *bytes_read = PAGE_SIZE; /* got a full page */
383 out:
384 if (IS_ERR(page))
385 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
386 (int)PAGE_SIZE,
387 (unsigned long long)index << PAGE_SHIFT,
388 PTR_ERR(page));
389 return page;
393 * bitmap file superblock operations
396 /* update the event counter and sync the superblock to disk */
397 int bitmap_update_sb(struct bitmap *bitmap)
399 bitmap_super_t *sb;
400 unsigned long flags;
402 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
403 return 0;
404 spin_lock_irqsave(&bitmap->lock, flags);
405 if (!bitmap->sb_page) { /* no superblock */
406 spin_unlock_irqrestore(&bitmap->lock, flags);
407 return 0;
409 spin_unlock_irqrestore(&bitmap->lock, flags);
410 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
411 sb->events = cpu_to_le64(bitmap->mddev->events);
412 if (!bitmap->mddev->degraded)
413 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
414 kunmap_atomic(sb, KM_USER0);
415 return write_page(bitmap, bitmap->sb_page, 1);
418 /* print out the bitmap file superblock */
419 void bitmap_print_sb(struct bitmap *bitmap)
421 bitmap_super_t *sb;
423 if (!bitmap || !bitmap->sb_page)
424 return;
425 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
426 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
427 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
428 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
429 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
430 *(__u32 *)(sb->uuid+0),
431 *(__u32 *)(sb->uuid+4),
432 *(__u32 *)(sb->uuid+8),
433 *(__u32 *)(sb->uuid+12));
434 printk(KERN_DEBUG " events: %llu\n",
435 (unsigned long long) le64_to_cpu(sb->events));
436 printk(KERN_DEBUG "events cleared: %llu\n",
437 (unsigned long long) le64_to_cpu(sb->events_cleared));
438 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
439 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
440 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
441 printk(KERN_DEBUG " sync size: %llu KB\n",
442 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
443 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
444 kunmap_atomic(sb, KM_USER0);
447 /* read the superblock from the bitmap file and initialize some bitmap fields */
448 static int bitmap_read_sb(struct bitmap *bitmap)
450 char *reason = NULL;
451 bitmap_super_t *sb;
452 unsigned long chunksize, daemon_sleep, write_behind;
453 unsigned long bytes_read;
454 unsigned long long events;
455 int err = -EINVAL;
457 /* page 0 is the superblock, read it... */
458 if (bitmap->file)
459 bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
460 else {
461 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
462 bytes_read = PAGE_SIZE;
464 if (IS_ERR(bitmap->sb_page)) {
465 err = PTR_ERR(bitmap->sb_page);
466 bitmap->sb_page = NULL;
467 return err;
470 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
472 if (bytes_read < sizeof(*sb)) { /* short read */
473 printk(KERN_INFO "%s: bitmap file superblock truncated\n",
474 bmname(bitmap));
475 err = -ENOSPC;
476 goto out;
479 chunksize = le32_to_cpu(sb->chunksize);
480 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
481 write_behind = le32_to_cpu(sb->write_behind);
483 /* verify that the bitmap-specific fields are valid */
484 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
485 reason = "bad magic";
486 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
487 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
488 reason = "unrecognized superblock version";
489 else if (chunksize < PAGE_SIZE)
490 reason = "bitmap chunksize too small";
491 else if ((1 << ffz(~chunksize)) != chunksize)
492 reason = "bitmap chunksize not a power of 2";
493 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
494 reason = "daemon sleep period out of range";
495 else if (write_behind > COUNTER_MAX)
496 reason = "write-behind limit out of range (0 - 16383)";
497 if (reason) {
498 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
499 bmname(bitmap), reason);
500 goto out;
503 /* keep the array size field of the bitmap superblock up to date */
504 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
506 if (!bitmap->mddev->persistent)
507 goto success;
510 * if we have a persistent array superblock, compare the
511 * bitmap's UUID and event counter to the mddev's
513 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
514 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
515 bmname(bitmap));
516 goto out;
518 events = le64_to_cpu(sb->events);
519 if (events < bitmap->mddev->events) {
520 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
521 "-- forcing full recovery\n", bmname(bitmap), events,
522 (unsigned long long) bitmap->mddev->events);
523 sb->state |= BITMAP_STALE;
525 success:
526 /* assign fields using values from superblock */
527 bitmap->chunksize = chunksize;
528 bitmap->daemon_sleep = daemon_sleep;
529 bitmap->daemon_lastrun = jiffies;
530 bitmap->max_write_behind = write_behind;
531 bitmap->flags |= sb->state;
532 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
533 bitmap->flags |= BITMAP_HOSTENDIAN;
534 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
535 if (sb->state & BITMAP_STALE)
536 bitmap->events_cleared = bitmap->mddev->events;
537 err = 0;
538 out:
539 kunmap_atomic(sb, KM_USER0);
540 if (err)
541 bitmap_print_sb(bitmap);
542 return err;
545 enum bitmap_mask_op {
546 MASK_SET,
547 MASK_UNSET
550 /* record the state of the bitmap in the superblock */
551 static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
552 enum bitmap_mask_op op)
554 bitmap_super_t *sb;
555 unsigned long flags;
557 spin_lock_irqsave(&bitmap->lock, flags);
558 if (!bitmap || !bitmap->sb_page) { /* can't set the state */
559 spin_unlock_irqrestore(&bitmap->lock, flags);
560 return;
562 spin_unlock_irqrestore(&bitmap->lock, flags);
563 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
564 switch (op) {
565 case MASK_SET: sb->state |= bits;
566 break;
567 case MASK_UNSET: sb->state &= ~bits;
568 break;
569 default: BUG();
571 kunmap_atomic(sb, KM_USER0);
575 * general bitmap file operations
578 /* calculate the index of the page that contains this bit */
579 static inline unsigned long file_page_index(unsigned long chunk)
581 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
584 /* calculate the (bit) offset of this bit within a page */
585 static inline unsigned long file_page_offset(unsigned long chunk)
587 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
591 * return a pointer to the page in the filemap that contains the given bit
593 * this lookup is complicated by the fact that the bitmap sb might be exactly
594 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
595 * 0 or page 1
597 static inline struct page *filemap_get_page(struct bitmap *bitmap,
598 unsigned long chunk)
600 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
604 static void bitmap_file_unmap(struct bitmap *bitmap)
606 struct page **map, *sb_page;
607 unsigned long *attr;
608 int pages;
609 unsigned long flags;
611 spin_lock_irqsave(&bitmap->lock, flags);
612 map = bitmap->filemap;
613 bitmap->filemap = NULL;
614 attr = bitmap->filemap_attr;
615 bitmap->filemap_attr = NULL;
616 pages = bitmap->file_pages;
617 bitmap->file_pages = 0;
618 sb_page = bitmap->sb_page;
619 bitmap->sb_page = NULL;
620 spin_unlock_irqrestore(&bitmap->lock, flags);
622 while (pages--)
623 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
624 put_page(map[pages]);
625 kfree(map);
626 kfree(attr);
628 safe_put_page(sb_page);
631 static void bitmap_stop_daemon(struct bitmap *bitmap);
633 /* dequeue the next item in a page list -- don't call from irq context */
634 static struct page_list *dequeue_page(struct bitmap *bitmap)
636 struct page_list *item = NULL;
637 struct list_head *head = &bitmap->complete_pages;
639 spin_lock(&bitmap->write_lock);
640 if (list_empty(head))
641 goto out;
642 item = list_entry(head->prev, struct page_list, list);
643 list_del(head->prev);
644 out:
645 spin_unlock(&bitmap->write_lock);
646 return item;
649 static void drain_write_queues(struct bitmap *bitmap)
651 struct page_list *item;
653 while ((item = dequeue_page(bitmap))) {
654 /* don't bother to wait */
655 mempool_free(item, bitmap->write_pool);
658 wake_up(&bitmap->write_wait);
661 static void bitmap_file_put(struct bitmap *bitmap)
663 struct file *file;
664 struct inode *inode;
665 unsigned long flags;
667 spin_lock_irqsave(&bitmap->lock, flags);
668 file = bitmap->file;
669 bitmap->file = NULL;
670 spin_unlock_irqrestore(&bitmap->lock, flags);
672 bitmap_stop_daemon(bitmap);
674 drain_write_queues(bitmap);
676 bitmap_file_unmap(bitmap);
678 if (file) {
679 inode = file->f_mapping->host;
680 spin_lock(&inode->i_lock);
681 atomic_set(&inode->i_writecount, 1); /* allow writes again */
682 spin_unlock(&inode->i_lock);
683 fput(file);
689 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
690 * then it is no longer reliable, so we stop using it and we mark the file
691 * as failed in the superblock
693 static void bitmap_file_kick(struct bitmap *bitmap)
695 char *path, *ptr = NULL;
697 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
698 bitmap_update_sb(bitmap);
700 if (bitmap->file) {
701 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
702 if (path)
703 ptr = file_path(bitmap->file, path, PAGE_SIZE);
705 printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
706 bmname(bitmap), ptr ? ptr : "");
708 kfree(path);
711 bitmap_file_put(bitmap);
713 return;
716 enum bitmap_page_attr {
717 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
718 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
719 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
722 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
723 enum bitmap_page_attr attr)
725 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
728 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
729 enum bitmap_page_attr attr)
731 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
734 static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
735 enum bitmap_page_attr attr)
737 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
741 * bitmap_file_set_bit -- called before performing a write to the md device
742 * to set (and eventually sync) a particular bit in the bitmap file
744 * we set the bit immediately, then we record the page number so that
745 * when an unplug occurs, we can flush the dirty pages out to disk
747 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
749 unsigned long bit;
750 struct page *page;
751 void *kaddr;
752 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
754 if (!bitmap->filemap) {
755 return;
758 page = filemap_get_page(bitmap, chunk);
759 bit = file_page_offset(chunk);
761 /* set the bit */
762 kaddr = kmap_atomic(page, KM_USER0);
763 if (bitmap->flags & BITMAP_HOSTENDIAN)
764 set_bit(bit, kaddr);
765 else
766 ext2_set_bit(bit, kaddr);
767 kunmap_atomic(kaddr, KM_USER0);
768 PRINTK("set file bit %lu page %lu\n", bit, page->index);
770 /* record page number so it gets flushed to disk when unplug occurs */
771 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
775 /* this gets called when the md device is ready to unplug its underlying
776 * (slave) device queues -- before we let any writes go down, we need to
777 * sync the dirty pages of the bitmap file to disk */
778 int bitmap_unplug(struct bitmap *bitmap)
780 unsigned long i, flags;
781 int dirty, need_write;
782 struct page *page;
783 int wait = 0;
784 int err;
786 if (!bitmap)
787 return 0;
789 /* look at each page to see if there are any set bits that need to be
790 * flushed out to disk */
791 for (i = 0; i < bitmap->file_pages; i++) {
792 spin_lock_irqsave(&bitmap->lock, flags);
793 if (!bitmap->filemap) {
794 spin_unlock_irqrestore(&bitmap->lock, flags);
795 return 0;
797 page = bitmap->filemap[i];
798 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
799 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
800 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
801 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
802 if (dirty)
803 wait = 1;
804 spin_unlock_irqrestore(&bitmap->lock, flags);
806 if (dirty | need_write) {
807 err = write_page(bitmap, page, 0);
808 if (err == -EAGAIN) {
809 if (dirty)
810 err = write_page(bitmap, page, 1);
811 else
812 err = 0;
814 if (err)
815 return 1;
818 if (wait) { /* if any writes were performed, we need to wait on them */
819 if (bitmap->file) {
820 spin_lock_irq(&bitmap->write_lock);
821 wait_event_lock_irq(bitmap->write_wait,
822 list_empty(&bitmap->complete_pages), bitmap->write_lock,
823 wake_up_process(bitmap->writeback_daemon->tsk));
824 spin_unlock_irq(&bitmap->write_lock);
825 } else
826 md_super_wait(bitmap->mddev);
828 return 0;
831 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
832 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
833 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
834 * memory mapping of the bitmap file
835 * Special cases:
836 * if there's no bitmap file, or if the bitmap file had been
837 * previously kicked from the array, we mark all the bits as
838 * 1's in order to cause a full resync.
840 * We ignore all bits for sectors that end earlier than 'start'.
841 * This is used when reading an out-of-date bitmap...
843 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
845 unsigned long i, chunks, index, oldindex, bit;
846 struct page *page = NULL, *oldpage = NULL;
847 unsigned long num_pages, bit_cnt = 0;
848 struct file *file;
849 unsigned long bytes, offset, dummy;
850 int outofdate;
851 int ret = -ENOSPC;
852 void *paddr;
854 chunks = bitmap->chunks;
855 file = bitmap->file;
857 BUG_ON(!file && !bitmap->offset);
859 #ifdef INJECT_FAULTS_3
860 outofdate = 1;
861 #else
862 outofdate = bitmap->flags & BITMAP_STALE;
863 #endif
864 if (outofdate)
865 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
866 "recovery\n", bmname(bitmap));
868 bytes = (chunks + 7) / 8;
870 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
872 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
873 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
874 bmname(bitmap),
875 (unsigned long) i_size_read(file->f_mapping->host),
876 bytes + sizeof(bitmap_super_t));
877 goto out;
880 ret = -ENOMEM;
882 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
883 if (!bitmap->filemap)
884 goto out;
886 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
887 bitmap->filemap_attr = kzalloc(
888 (((num_pages*4/8)+sizeof(unsigned long))
889 /sizeof(unsigned long))
890 *sizeof(unsigned long),
891 GFP_KERNEL);
892 if (!bitmap->filemap_attr)
893 goto out;
895 oldindex = ~0L;
897 for (i = 0; i < chunks; i++) {
898 int b;
899 index = file_page_index(i);
900 bit = file_page_offset(i);
901 if (index != oldindex) { /* this is a new page, read it in */
902 /* unmap the old page, we're done with it */
903 if (index == 0) {
905 * if we're here then the superblock page
906 * contains some bits (PAGE_SIZE != sizeof sb)
907 * we've already read it in, so just use it
909 page = bitmap->sb_page;
910 offset = sizeof(bitmap_super_t);
911 } else if (file) {
912 page = read_page(file, index, &dummy);
913 offset = 0;
914 } else {
915 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
916 offset = 0;
918 if (IS_ERR(page)) { /* read error */
919 ret = PTR_ERR(page);
920 goto out;
923 oldindex = index;
924 oldpage = page;
926 if (outofdate) {
928 * if bitmap is out of date, dirty the
929 * whole page and write it out
931 paddr = kmap_atomic(page, KM_USER0);
932 memset(paddr + offset, 0xff,
933 PAGE_SIZE - offset);
934 kunmap_atomic(paddr, KM_USER0);
935 ret = write_page(bitmap, page, 1);
936 if (ret) {
937 /* release, page not in filemap yet */
938 put_page(page);
939 goto out;
943 bitmap->filemap[bitmap->file_pages++] = page;
945 paddr = kmap_atomic(page, KM_USER0);
946 if (bitmap->flags & BITMAP_HOSTENDIAN)
947 b = test_bit(bit, paddr);
948 else
949 b = ext2_test_bit(bit, paddr);
950 kunmap_atomic(paddr, KM_USER0);
951 if (b) {
952 /* if the disk bit is set, set the memory bit */
953 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
954 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
956 bit_cnt++;
957 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
961 /* everything went OK */
962 ret = 0;
963 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
965 if (bit_cnt) { /* Kick recovery if any bits were set */
966 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
967 md_wakeup_thread(bitmap->mddev->thread);
970 out:
971 printk(KERN_INFO "%s: bitmap initialized from disk: "
972 "read %lu/%lu pages, set %lu bits, status: %d\n",
973 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
975 return ret;
978 void bitmap_write_all(struct bitmap *bitmap)
980 /* We don't actually write all bitmap blocks here,
981 * just flag them as needing to be written
983 int i;
985 for (i=0; i < bitmap->file_pages; i++)
986 set_page_attr(bitmap, bitmap->filemap[i],
987 BITMAP_PAGE_NEEDWRITE);
991 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
993 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
994 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
995 bitmap->bp[page].count += inc;
997 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
998 (unsigned long long)offset, inc, bitmap->bp[page].count);
1000 bitmap_checkfree(bitmap, page);
1002 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1003 sector_t offset, int *blocks,
1004 int create);
1007 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1008 * out to disk
1011 int bitmap_daemon_work(struct bitmap *bitmap)
1013 unsigned long j;
1014 unsigned long flags;
1015 struct page *page = NULL, *lastpage = NULL;
1016 int err = 0;
1017 int blocks;
1018 void *paddr;
1020 if (bitmap == NULL)
1021 return 0;
1022 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1023 return 0;
1024 bitmap->daemon_lastrun = jiffies;
1026 for (j = 0; j < bitmap->chunks; j++) {
1027 bitmap_counter_t *bmc;
1028 spin_lock_irqsave(&bitmap->lock, flags);
1029 if (!bitmap->filemap) {
1030 /* error or shutdown */
1031 spin_unlock_irqrestore(&bitmap->lock, flags);
1032 break;
1035 page = filemap_get_page(bitmap, j);
1037 if (page != lastpage) {
1038 /* skip this page unless it's marked as needing cleaning */
1039 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1040 int need_write = test_page_attr(bitmap, page,
1041 BITMAP_PAGE_NEEDWRITE);
1042 if (need_write)
1043 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1045 spin_unlock_irqrestore(&bitmap->lock, flags);
1046 if (need_write) {
1047 switch (write_page(bitmap, page, 0)) {
1048 case -EAGAIN:
1049 set_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1050 break;
1051 case 0:
1052 break;
1053 default:
1054 bitmap_file_kick(bitmap);
1057 continue;
1060 /* grab the new page, sync and release the old */
1061 if (lastpage != NULL) {
1062 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1063 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1064 spin_unlock_irqrestore(&bitmap->lock, flags);
1065 err = write_page(bitmap, lastpage, 0);
1066 if (err == -EAGAIN) {
1067 err = 0;
1068 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1070 } else {
1071 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1072 spin_unlock_irqrestore(&bitmap->lock, flags);
1074 if (err)
1075 bitmap_file_kick(bitmap);
1076 } else
1077 spin_unlock_irqrestore(&bitmap->lock, flags);
1078 lastpage = page;
1080 printk("bitmap clean at page %lu\n", j);
1082 spin_lock_irqsave(&bitmap->lock, flags);
1083 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1085 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1086 &blocks, 0);
1087 if (bmc) {
1089 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1091 if (*bmc == 2) {
1092 *bmc=1; /* maybe clear the bit next time */
1093 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1094 } else if (*bmc == 1) {
1095 /* we can clear the bit */
1096 *bmc = 0;
1097 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1098 -1);
1100 /* clear the bit */
1101 paddr = kmap_atomic(page, KM_USER0);
1102 if (bitmap->flags & BITMAP_HOSTENDIAN)
1103 clear_bit(file_page_offset(j), paddr);
1104 else
1105 ext2_clear_bit(file_page_offset(j), paddr);
1106 kunmap_atomic(paddr, KM_USER0);
1109 spin_unlock_irqrestore(&bitmap->lock, flags);
1112 /* now sync the final page */
1113 if (lastpage != NULL) {
1114 spin_lock_irqsave(&bitmap->lock, flags);
1115 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1116 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1117 spin_unlock_irqrestore(&bitmap->lock, flags);
1118 err = write_page(bitmap, lastpage, 0);
1119 if (err == -EAGAIN) {
1120 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1121 err = 0;
1123 } else {
1124 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1125 spin_unlock_irqrestore(&bitmap->lock, flags);
1129 return err;
1132 static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
1134 mdk_thread_t *dmn;
1135 unsigned long flags;
1137 /* if no one is waiting on us, we'll free the md thread struct
1138 * and exit, otherwise we let the waiter clean things up */
1139 spin_lock_irqsave(&bitmap->lock, flags);
1140 if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
1141 *daemon = NULL;
1142 spin_unlock_irqrestore(&bitmap->lock, flags);
1143 kfree(dmn);
1144 complete_and_exit(NULL, 0); /* do_exit not exported */
1146 spin_unlock_irqrestore(&bitmap->lock, flags);
1149 static void bitmap_writeback_daemon(mddev_t *mddev)
1151 struct bitmap *bitmap = mddev->bitmap;
1152 struct page *page;
1153 struct page_list *item;
1154 int err = 0;
1156 if (signal_pending(current)) {
1157 printk(KERN_INFO
1158 "%s: bitmap writeback daemon got signal, exiting...\n",
1159 bmname(bitmap));
1160 err = -EINTR;
1161 goto out;
1163 if (bitmap == NULL)
1164 /* about to be stopped. */
1165 return;
1167 PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
1168 /* wait on bitmap page writebacks */
1169 while ((item = dequeue_page(bitmap))) {
1170 page = item->page;
1171 mempool_free(item, bitmap->write_pool);
1172 PRINTK("wait on page writeback: %p\n", page);
1173 wait_on_page_writeback(page);
1174 PRINTK("finished page writeback: %p\n", page);
1176 err = PageError(page);
1177 if (err) {
1178 printk(KERN_WARNING "%s: bitmap file writeback "
1179 "failed (page %lu): %d\n",
1180 bmname(bitmap), page->index, err);
1181 bitmap_file_kick(bitmap);
1182 goto out;
1185 out:
1186 wake_up(&bitmap->write_wait);
1187 if (err) {
1188 printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
1189 bmname(bitmap), err);
1190 daemon_exit(bitmap, &bitmap->writeback_daemon);
1194 static mdk_thread_t *bitmap_start_daemon(struct bitmap *bitmap,
1195 void (*func)(mddev_t *), char *name)
1197 mdk_thread_t *daemon;
1198 char namebuf[32];
1200 #ifdef INJECT_FATAL_FAULT_2
1201 daemon = NULL;
1202 #else
1203 sprintf(namebuf, "%%s_%s", name);
1204 daemon = md_register_thread(func, bitmap->mddev, namebuf);
1205 #endif
1206 if (!daemon) {
1207 printk(KERN_ERR "%s: failed to start bitmap daemon\n",
1208 bmname(bitmap));
1209 return ERR_PTR(-ECHILD);
1212 md_wakeup_thread(daemon); /* start it running */
1214 PRINTK("%s: %s daemon (pid %d) started...\n",
1215 bmname(bitmap), name, daemon->tsk->pid);
1217 return daemon;
1220 static void bitmap_stop_daemon(struct bitmap *bitmap)
1222 /* the daemon can't stop itself... it'll just exit instead... */
1223 if (bitmap->writeback_daemon && ! IS_ERR(bitmap->writeback_daemon) &&
1224 current->pid != bitmap->writeback_daemon->tsk->pid) {
1225 mdk_thread_t *daemon;
1226 unsigned long flags;
1228 spin_lock_irqsave(&bitmap->lock, flags);
1229 daemon = bitmap->writeback_daemon;
1230 bitmap->writeback_daemon = NULL;
1231 spin_unlock_irqrestore(&bitmap->lock, flags);
1232 if (daemon && ! IS_ERR(daemon))
1233 md_unregister_thread(daemon); /* destroy the thread */
1237 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1238 sector_t offset, int *blocks,
1239 int create)
1241 /* If 'create', we might release the lock and reclaim it.
1242 * The lock must have been taken with interrupts enabled.
1243 * If !create, we don't release the lock.
1245 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1246 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1247 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1248 sector_t csize;
1250 if (bitmap_checkpage(bitmap, page, create) < 0) {
1251 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1252 *blocks = csize - (offset & (csize- 1));
1253 return NULL;
1255 /* now locked ... */
1257 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1258 /* should we use the first or second counter field
1259 * of the hijacked pointer? */
1260 int hi = (pageoff > PAGE_COUNTER_MASK);
1261 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1262 PAGE_COUNTER_SHIFT - 1);
1263 *blocks = csize - (offset & (csize- 1));
1264 return &((bitmap_counter_t *)
1265 &bitmap->bp[page].map)[hi];
1266 } else { /* page is allocated */
1267 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1268 *blocks = csize - (offset & (csize- 1));
1269 return (bitmap_counter_t *)
1270 &(bitmap->bp[page].map[pageoff]);
1274 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1276 if (!bitmap) return 0;
1278 if (behind) {
1279 atomic_inc(&bitmap->behind_writes);
1280 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1281 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1284 while (sectors) {
1285 int blocks;
1286 bitmap_counter_t *bmc;
1288 spin_lock_irq(&bitmap->lock);
1289 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1290 if (!bmc) {
1291 spin_unlock_irq(&bitmap->lock);
1292 return 0;
1295 switch(*bmc) {
1296 case 0:
1297 bitmap_file_set_bit(bitmap, offset);
1298 bitmap_count_page(bitmap,offset, 1);
1299 blk_plug_device(bitmap->mddev->queue);
1300 /* fall through */
1301 case 1:
1302 *bmc = 2;
1304 if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
1305 (*bmc)++;
1307 spin_unlock_irq(&bitmap->lock);
1309 offset += blocks;
1310 if (sectors > blocks)
1311 sectors -= blocks;
1312 else sectors = 0;
1314 return 0;
1317 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1318 int success, int behind)
1320 if (!bitmap) return;
1321 if (behind) {
1322 atomic_dec(&bitmap->behind_writes);
1323 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1324 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1327 while (sectors) {
1328 int blocks;
1329 unsigned long flags;
1330 bitmap_counter_t *bmc;
1332 spin_lock_irqsave(&bitmap->lock, flags);
1333 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1334 if (!bmc) {
1335 spin_unlock_irqrestore(&bitmap->lock, flags);
1336 return;
1339 if (!success && ! (*bmc & NEEDED_MASK))
1340 *bmc |= NEEDED_MASK;
1342 (*bmc)--;
1343 if (*bmc <= 2) {
1344 set_page_attr(bitmap,
1345 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1346 BITMAP_PAGE_CLEAN);
1348 spin_unlock_irqrestore(&bitmap->lock, flags);
1349 offset += blocks;
1350 if (sectors > blocks)
1351 sectors -= blocks;
1352 else sectors = 0;
1356 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1357 int degraded)
1359 bitmap_counter_t *bmc;
1360 int rv;
1361 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1362 *blocks = 1024;
1363 return 1; /* always resync if no bitmap */
1365 spin_lock_irq(&bitmap->lock);
1366 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1367 rv = 0;
1368 if (bmc) {
1369 /* locked */
1370 if (RESYNC(*bmc))
1371 rv = 1;
1372 else if (NEEDED(*bmc)) {
1373 rv = 1;
1374 if (!degraded) { /* don't set/clear bits if degraded */
1375 *bmc |= RESYNC_MASK;
1376 *bmc &= ~NEEDED_MASK;
1380 spin_unlock_irq(&bitmap->lock);
1381 return rv;
1384 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1386 bitmap_counter_t *bmc;
1387 unsigned long flags;
1389 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1390 */ if (bitmap == NULL) {
1391 *blocks = 1024;
1392 return;
1394 spin_lock_irqsave(&bitmap->lock, flags);
1395 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1396 if (bmc == NULL)
1397 goto unlock;
1398 /* locked */
1400 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1402 if (RESYNC(*bmc)) {
1403 *bmc &= ~RESYNC_MASK;
1405 if (!NEEDED(*bmc) && aborted)
1406 *bmc |= NEEDED_MASK;
1407 else {
1408 if (*bmc <= 2) {
1409 set_page_attr(bitmap,
1410 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1411 BITMAP_PAGE_CLEAN);
1415 unlock:
1416 spin_unlock_irqrestore(&bitmap->lock, flags);
1419 void bitmap_close_sync(struct bitmap *bitmap)
1421 /* Sync has finished, and any bitmap chunks that weren't synced
1422 * properly have been aborted. It remains to us to clear the
1423 * RESYNC bit wherever it is still on
1425 sector_t sector = 0;
1426 int blocks;
1427 if (!bitmap) return;
1428 while (sector < bitmap->mddev->resync_max_sectors) {
1429 bitmap_end_sync(bitmap, sector, &blocks, 0);
1431 if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
1432 (unsigned long long)sector, blocks);
1433 */ sector += blocks;
1437 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1439 /* For each chunk covered by any of these sectors, set the
1440 * counter to 1 and set resync_needed. They should all
1441 * be 0 at this point
1444 int secs;
1445 bitmap_counter_t *bmc;
1446 spin_lock_irq(&bitmap->lock);
1447 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1448 if (!bmc) {
1449 spin_unlock_irq(&bitmap->lock);
1450 return;
1452 if (! *bmc) {
1453 struct page *page;
1454 *bmc = 1 | (needed?NEEDED_MASK:0);
1455 bitmap_count_page(bitmap, offset, 1);
1456 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1457 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1459 spin_unlock_irq(&bitmap->lock);
1464 * flush out any pending updates
1466 void bitmap_flush(mddev_t *mddev)
1468 struct bitmap *bitmap = mddev->bitmap;
1469 int sleep;
1471 if (!bitmap) /* there was no bitmap */
1472 return;
1474 /* run the daemon_work three time to ensure everything is flushed
1475 * that can be
1477 sleep = bitmap->daemon_sleep;
1478 bitmap->daemon_sleep = 0;
1479 bitmap_daemon_work(bitmap);
1480 bitmap_daemon_work(bitmap);
1481 bitmap_daemon_work(bitmap);
1482 bitmap->daemon_sleep = sleep;
1483 bitmap_update_sb(bitmap);
1487 * free memory that was allocated
1489 static void bitmap_free(struct bitmap *bitmap)
1491 unsigned long k, pages;
1492 struct bitmap_page *bp;
1494 if (!bitmap) /* there was no bitmap */
1495 return;
1497 /* release the bitmap file and kill the daemon */
1498 bitmap_file_put(bitmap);
1500 bp = bitmap->bp;
1501 pages = bitmap->pages;
1503 /* free all allocated memory */
1505 mempool_destroy(bitmap->write_pool);
1507 if (bp) /* deallocate the page memory */
1508 for (k = 0; k < pages; k++)
1509 if (bp[k].map && !bp[k].hijacked)
1510 kfree(bp[k].map);
1511 kfree(bp);
1512 kfree(bitmap);
1514 void bitmap_destroy(mddev_t *mddev)
1516 struct bitmap *bitmap = mddev->bitmap;
1518 if (!bitmap) /* there was no bitmap */
1519 return;
1521 mddev->bitmap = NULL; /* disconnect from the md device */
1522 if (mddev->thread)
1523 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1525 bitmap_free(bitmap);
1529 * initialize the bitmap structure
1530 * if this returns an error, bitmap_destroy must be called to do clean up
1532 int bitmap_create(mddev_t *mddev)
1534 struct bitmap *bitmap;
1535 unsigned long blocks = mddev->resync_max_sectors;
1536 unsigned long chunks;
1537 unsigned long pages;
1538 struct file *file = mddev->bitmap_file;
1539 int err;
1540 sector_t start;
1542 BUG_ON(sizeof(bitmap_super_t) != 256);
1544 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
1545 return 0;
1547 BUG_ON(file && mddev->bitmap_offset);
1549 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1550 if (!bitmap)
1551 return -ENOMEM;
1553 spin_lock_init(&bitmap->lock);
1554 bitmap->mddev = mddev;
1556 spin_lock_init(&bitmap->write_lock);
1557 INIT_LIST_HEAD(&bitmap->complete_pages);
1558 init_waitqueue_head(&bitmap->write_wait);
1559 bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
1560 write_pool_free, NULL);
1561 err = -ENOMEM;
1562 if (!bitmap->write_pool)
1563 goto error;
1565 bitmap->file = file;
1566 bitmap->offset = mddev->bitmap_offset;
1567 if (file) get_file(file);
1568 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1569 err = bitmap_read_sb(bitmap);
1570 if (err)
1571 goto error;
1573 bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
1574 sizeof(bitmap->chunksize));
1576 /* now that chunksize and chunkshift are set, we can use these macros */
1577 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1578 CHUNK_BLOCK_RATIO(bitmap);
1579 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1581 BUG_ON(!pages);
1583 bitmap->chunks = chunks;
1584 bitmap->pages = pages;
1585 bitmap->missing_pages = pages;
1586 bitmap->counter_bits = COUNTER_BITS;
1588 bitmap->syncchunk = ~0UL;
1590 #ifdef INJECT_FATAL_FAULT_1
1591 bitmap->bp = NULL;
1592 #else
1593 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1594 #endif
1595 err = -ENOMEM;
1596 if (!bitmap->bp)
1597 goto error;
1599 bitmap->flags |= BITMAP_ACTIVE;
1601 /* now that we have some pages available, initialize the in-memory
1602 * bitmap from the on-disk bitmap */
1603 start = 0;
1604 if (mddev->degraded == 0
1605 || bitmap->events_cleared == mddev->events)
1606 /* no need to keep dirty bits to optimise a re-add of a missing device */
1607 start = mddev->recovery_cp;
1608 err = bitmap_init_from_disk(bitmap, start);
1610 if (err)
1611 goto error;
1613 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1614 pages, bmname(bitmap));
1616 mddev->bitmap = bitmap;
1618 if (file)
1619 /* kick off the bitmap writeback daemon */
1620 bitmap->writeback_daemon =
1621 bitmap_start_daemon(bitmap,
1622 bitmap_writeback_daemon,
1623 "bitmap_wb");
1625 if (IS_ERR(bitmap->writeback_daemon))
1626 return PTR_ERR(bitmap->writeback_daemon);
1627 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1629 return bitmap_update_sb(bitmap);
1631 error:
1632 bitmap_free(bitmap);
1633 return err;
1636 /* the bitmap API -- for raid personalities */
1637 EXPORT_SYMBOL(bitmap_startwrite);
1638 EXPORT_SYMBOL(bitmap_endwrite);
1639 EXPORT_SYMBOL(bitmap_start_sync);
1640 EXPORT_SYMBOL(bitmap_end_sync);
1641 EXPORT_SYMBOL(bitmap_unplug);
1642 EXPORT_SYMBOL(bitmap_close_sync);
1643 EXPORT_SYMBOL(bitmap_daemon_work);