uwb: add symlinks in sysfs between radio controllers and PALs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / md / bitmap.c
blobac89a5deaca2e12fa62ccb78f1d62b088039675c
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
13 * Still to do:
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.
19 #include <linux/module.h>
20 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/timer.h>
24 #include <linux/sched.h>
25 #include <linux/list.h>
26 #include <linux/file.h>
27 #include <linux/mount.h>
28 #include <linux/buffer_head.h>
29 #include <linux/raid/md.h>
30 #include <linux/raid/bitmap.h>
32 /* debug macros */
34 #define DEBUG 0
36 #if DEBUG
37 /* these are for debugging purposes only! */
39 /* define one and only one of these */
40 #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
41 #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
42 #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
43 #define INJECT_FAULTS_4 0 /* undef */
44 #define INJECT_FAULTS_5 0 /* undef */
45 #define INJECT_FAULTS_6 0
47 /* if these are defined, the driver will fail! debug only */
48 #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
49 #define INJECT_FATAL_FAULT_2 0 /* undef */
50 #define INJECT_FATAL_FAULT_3 0 /* undef */
51 #endif
53 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
54 #define DPRINTK(x...) do { } while(0)
56 #ifndef PRINTK
57 # if DEBUG > 0
58 # define PRINTK(x...) printk(KERN_DEBUG x)
59 # else
60 # define PRINTK(x...)
61 # endif
62 #endif
64 static inline char * bmname(struct bitmap *bitmap)
66 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
71 * just a placeholder - calls kmalloc for bitmap pages
73 static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
75 unsigned char *page;
77 #ifdef INJECT_FAULTS_1
78 page = NULL;
79 #else
80 page = kmalloc(PAGE_SIZE, GFP_NOIO);
81 #endif
82 if (!page)
83 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
84 else
85 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
86 bmname(bitmap), page);
87 return page;
91 * for now just a placeholder -- just calls kfree for bitmap pages
93 static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
95 PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
96 kfree(page);
100 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
102 * 1) check to see if this page is allocated, if it's not then try to alloc
103 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
104 * page pointer directly as a counter
106 * if we find our page, we increment the page's refcount so that it stays
107 * allocated while we're using it
109 static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
111 unsigned char *mappage;
113 if (page >= bitmap->pages) {
114 printk(KERN_ALERT
115 "%s: invalid bitmap page request: %lu (> %lu)\n",
116 bmname(bitmap), page, bitmap->pages-1);
117 return -EINVAL;
121 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
122 return 0;
124 if (bitmap->bp[page].map) /* page is already allocated, just return */
125 return 0;
127 if (!create)
128 return -ENOENT;
130 spin_unlock_irq(&bitmap->lock);
132 /* this page has not been allocated yet */
134 if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
135 PRINTK("%s: bitmap map page allocation failed, hijacking\n",
136 bmname(bitmap));
137 /* failed - set the hijacked flag so that we can use the
138 * pointer as a counter */
139 spin_lock_irq(&bitmap->lock);
140 if (!bitmap->bp[page].map)
141 bitmap->bp[page].hijacked = 1;
142 goto out;
145 /* got a page */
147 spin_lock_irq(&bitmap->lock);
149 /* recheck the page */
151 if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
152 /* somebody beat us to getting the page */
153 bitmap_free_page(bitmap, mappage);
154 return 0;
157 /* no page was in place and we have one, so install it */
159 memset(mappage, 0, PAGE_SIZE);
160 bitmap->bp[page].map = mappage;
161 bitmap->missing_pages--;
162 out:
163 return 0;
167 /* if page is completely empty, put it back on the free list, or dealloc it */
168 /* if page was hijacked, unmark the flag so it might get alloced next time */
169 /* Note: lock should be held when calling this */
170 static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
172 char *ptr;
174 if (bitmap->bp[page].count) /* page is still busy */
175 return;
177 /* page is no longer in use, it can be released */
179 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
180 bitmap->bp[page].hijacked = 0;
181 bitmap->bp[page].map = NULL;
182 return;
185 /* normal case, free the page */
187 #if 0
188 /* actually ... let's not. We will probably need the page again exactly when
189 * memory is tight and we are flusing to disk
191 return;
192 #else
193 ptr = bitmap->bp[page].map;
194 bitmap->bp[page].map = NULL;
195 bitmap->missing_pages++;
196 bitmap_free_page(bitmap, ptr);
197 return;
198 #endif
203 * bitmap file handling - read and write the bitmap file and its superblock
207 * basic page I/O operations
210 /* IO operations when bitmap is stored near all superblocks */
211 static struct page *read_sb_page(mddev_t *mddev, long offset, unsigned long index)
213 /* choose a good rdev and read the page from there */
215 mdk_rdev_t *rdev;
216 struct list_head *tmp;
217 struct page *page = alloc_page(GFP_KERNEL);
218 sector_t target;
220 if (!page)
221 return ERR_PTR(-ENOMEM);
223 rdev_for_each(rdev, tmp, mddev) {
224 if (! test_bit(In_sync, &rdev->flags)
225 || test_bit(Faulty, &rdev->flags))
226 continue;
228 target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
230 if (sync_page_io(rdev->bdev, target, PAGE_SIZE, page, READ)) {
231 page->index = index;
232 attach_page_buffers(page, NULL); /* so that free_buffer will
233 * quietly no-op */
234 return page;
237 return ERR_PTR(-EIO);
241 static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
243 /* Iterate the disks of an mddev, using rcu to protect access to the
244 * linked list, and raising the refcount of devices we return to ensure
245 * they don't disappear while in use.
246 * As devices are only added or removed when raid_disk is < 0 and
247 * nr_pending is 0 and In_sync is clear, the entries we return will
248 * still be in the same position on the list when we re-enter
249 * list_for_each_continue_rcu.
251 struct list_head *pos;
252 rcu_read_lock();
253 if (rdev == NULL)
254 /* start at the beginning */
255 pos = &mddev->disks;
256 else {
257 /* release the previous rdev and start from there. */
258 rdev_dec_pending(rdev, mddev);
259 pos = &rdev->same_set;
261 list_for_each_continue_rcu(pos, &mddev->disks) {
262 rdev = list_entry(pos, mdk_rdev_t, same_set);
263 if (rdev->raid_disk >= 0 &&
264 test_bit(In_sync, &rdev->flags) &&
265 !test_bit(Faulty, &rdev->flags)) {
266 /* this is a usable devices */
267 atomic_inc(&rdev->nr_pending);
268 rcu_read_unlock();
269 return rdev;
272 rcu_read_unlock();
273 return NULL;
276 static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
278 mdk_rdev_t *rdev = NULL;
279 mddev_t *mddev = bitmap->mddev;
281 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
282 int size = PAGE_SIZE;
283 if (page->index == bitmap->file_pages-1)
284 size = roundup(bitmap->last_page_size,
285 bdev_hardsect_size(rdev->bdev));
286 /* Just make sure we aren't corrupting data or
287 * metadata
289 if (bitmap->offset < 0) {
290 /* DATA BITMAP METADATA */
291 if (bitmap->offset
292 + (long)(page->index * (PAGE_SIZE/512))
293 + size/512 > 0)
294 /* bitmap runs in to metadata */
295 goto bad_alignment;
296 if (rdev->data_offset + mddev->size*2
297 > rdev->sb_start + bitmap->offset)
298 /* data runs in to bitmap */
299 goto bad_alignment;
300 } else if (rdev->sb_start < rdev->data_offset) {
301 /* METADATA BITMAP DATA */
302 if (rdev->sb_start
303 + bitmap->offset
304 + page->index*(PAGE_SIZE/512) + size/512
305 > rdev->data_offset)
306 /* bitmap runs in to data */
307 goto bad_alignment;
308 } else {
309 /* DATA METADATA BITMAP - no problems */
311 md_super_write(mddev, rdev,
312 rdev->sb_start + bitmap->offset
313 + page->index * (PAGE_SIZE/512),
314 size,
315 page);
318 if (wait)
319 md_super_wait(mddev);
320 return 0;
322 bad_alignment:
323 rcu_read_unlock();
324 return -EINVAL;
327 static void bitmap_file_kick(struct bitmap *bitmap);
329 * write out a page to a file
331 static void write_page(struct bitmap *bitmap, struct page *page, int wait)
333 struct buffer_head *bh;
335 if (bitmap->file == NULL) {
336 switch (write_sb_page(bitmap, page, wait)) {
337 case -EINVAL:
338 bitmap->flags |= BITMAP_WRITE_ERROR;
340 } else {
342 bh = page_buffers(page);
344 while (bh && bh->b_blocknr) {
345 atomic_inc(&bitmap->pending_writes);
346 set_buffer_locked(bh);
347 set_buffer_mapped(bh);
348 submit_bh(WRITE, bh);
349 bh = bh->b_this_page;
352 if (wait) {
353 wait_event(bitmap->write_wait,
354 atomic_read(&bitmap->pending_writes)==0);
357 if (bitmap->flags & BITMAP_WRITE_ERROR)
358 bitmap_file_kick(bitmap);
361 static void end_bitmap_write(struct buffer_head *bh, int uptodate)
363 struct bitmap *bitmap = bh->b_private;
364 unsigned long flags;
366 if (!uptodate) {
367 spin_lock_irqsave(&bitmap->lock, flags);
368 bitmap->flags |= BITMAP_WRITE_ERROR;
369 spin_unlock_irqrestore(&bitmap->lock, flags);
371 if (atomic_dec_and_test(&bitmap->pending_writes))
372 wake_up(&bitmap->write_wait);
375 /* copied from buffer.c */
376 static void
377 __clear_page_buffers(struct page *page)
379 ClearPagePrivate(page);
380 set_page_private(page, 0);
381 page_cache_release(page);
383 static void free_buffers(struct page *page)
385 struct buffer_head *bh = page_buffers(page);
387 while (bh) {
388 struct buffer_head *next = bh->b_this_page;
389 free_buffer_head(bh);
390 bh = next;
392 __clear_page_buffers(page);
393 put_page(page);
396 /* read a page from a file.
397 * We both read the page, and attach buffers to the page to record the
398 * address of each block (using bmap). These addresses will be used
399 * to write the block later, completely bypassing the filesystem.
400 * This usage is similar to how swap files are handled, and allows us
401 * to write to a file with no concerns of memory allocation failing.
403 static struct page *read_page(struct file *file, unsigned long index,
404 struct bitmap *bitmap,
405 unsigned long count)
407 struct page *page = NULL;
408 struct inode *inode = file->f_path.dentry->d_inode;
409 struct buffer_head *bh;
410 sector_t block;
412 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
413 (unsigned long long)index << PAGE_SHIFT);
415 page = alloc_page(GFP_KERNEL);
416 if (!page)
417 page = ERR_PTR(-ENOMEM);
418 if (IS_ERR(page))
419 goto out;
421 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
422 if (!bh) {
423 put_page(page);
424 page = ERR_PTR(-ENOMEM);
425 goto out;
427 attach_page_buffers(page, bh);
428 block = index << (PAGE_SHIFT - inode->i_blkbits);
429 while (bh) {
430 if (count == 0)
431 bh->b_blocknr = 0;
432 else {
433 bh->b_blocknr = bmap(inode, block);
434 if (bh->b_blocknr == 0) {
435 /* Cannot use this file! */
436 free_buffers(page);
437 page = ERR_PTR(-EINVAL);
438 goto out;
440 bh->b_bdev = inode->i_sb->s_bdev;
441 if (count < (1<<inode->i_blkbits))
442 count = 0;
443 else
444 count -= (1<<inode->i_blkbits);
446 bh->b_end_io = end_bitmap_write;
447 bh->b_private = bitmap;
448 atomic_inc(&bitmap->pending_writes);
449 set_buffer_locked(bh);
450 set_buffer_mapped(bh);
451 submit_bh(READ, bh);
453 block++;
454 bh = bh->b_this_page;
456 page->index = index;
458 wait_event(bitmap->write_wait,
459 atomic_read(&bitmap->pending_writes)==0);
460 if (bitmap->flags & BITMAP_WRITE_ERROR) {
461 free_buffers(page);
462 page = ERR_PTR(-EIO);
464 out:
465 if (IS_ERR(page))
466 printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
467 (int)PAGE_SIZE,
468 (unsigned long long)index << PAGE_SHIFT,
469 PTR_ERR(page));
470 return page;
474 * bitmap file superblock operations
477 /* update the event counter and sync the superblock to disk */
478 void bitmap_update_sb(struct bitmap *bitmap)
480 bitmap_super_t *sb;
481 unsigned long flags;
483 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
484 return;
485 spin_lock_irqsave(&bitmap->lock, flags);
486 if (!bitmap->sb_page) { /* no superblock */
487 spin_unlock_irqrestore(&bitmap->lock, flags);
488 return;
490 spin_unlock_irqrestore(&bitmap->lock, flags);
491 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
492 sb->events = cpu_to_le64(bitmap->mddev->events);
493 if (bitmap->mddev->events < bitmap->events_cleared) {
494 /* rocking back to read-only */
495 bitmap->events_cleared = bitmap->mddev->events;
496 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
498 kunmap_atomic(sb, KM_USER0);
499 write_page(bitmap, bitmap->sb_page, 1);
502 /* print out the bitmap file superblock */
503 void bitmap_print_sb(struct bitmap *bitmap)
505 bitmap_super_t *sb;
507 if (!bitmap || !bitmap->sb_page)
508 return;
509 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
510 printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
511 printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
512 printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
513 printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
514 *(__u32 *)(sb->uuid+0),
515 *(__u32 *)(sb->uuid+4),
516 *(__u32 *)(sb->uuid+8),
517 *(__u32 *)(sb->uuid+12));
518 printk(KERN_DEBUG " events: %llu\n",
519 (unsigned long long) le64_to_cpu(sb->events));
520 printk(KERN_DEBUG "events cleared: %llu\n",
521 (unsigned long long) le64_to_cpu(sb->events_cleared));
522 printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
523 printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
524 printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
525 printk(KERN_DEBUG " sync size: %llu KB\n",
526 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
527 printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
528 kunmap_atomic(sb, KM_USER0);
531 /* read the superblock from the bitmap file and initialize some bitmap fields */
532 static int bitmap_read_sb(struct bitmap *bitmap)
534 char *reason = NULL;
535 bitmap_super_t *sb;
536 unsigned long chunksize, daemon_sleep, write_behind;
537 unsigned long long events;
538 int err = -EINVAL;
540 /* page 0 is the superblock, read it... */
541 if (bitmap->file) {
542 loff_t isize = i_size_read(bitmap->file->f_mapping->host);
543 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
545 bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
546 } else {
547 bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset, 0);
549 if (IS_ERR(bitmap->sb_page)) {
550 err = PTR_ERR(bitmap->sb_page);
551 bitmap->sb_page = NULL;
552 return err;
555 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
557 chunksize = le32_to_cpu(sb->chunksize);
558 daemon_sleep = le32_to_cpu(sb->daemon_sleep);
559 write_behind = le32_to_cpu(sb->write_behind);
561 /* verify that the bitmap-specific fields are valid */
562 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
563 reason = "bad magic";
564 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
565 le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
566 reason = "unrecognized superblock version";
567 else if (chunksize < PAGE_SIZE)
568 reason = "bitmap chunksize too small";
569 else if ((1 << ffz(~chunksize)) != chunksize)
570 reason = "bitmap chunksize not a power of 2";
571 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
572 reason = "daemon sleep period out of range";
573 else if (write_behind > COUNTER_MAX)
574 reason = "write-behind limit out of range (0 - 16383)";
575 if (reason) {
576 printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
577 bmname(bitmap), reason);
578 goto out;
581 /* keep the array size field of the bitmap superblock up to date */
582 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
584 if (!bitmap->mddev->persistent)
585 goto success;
588 * if we have a persistent array superblock, compare the
589 * bitmap's UUID and event counter to the mddev's
591 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
592 printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
593 bmname(bitmap));
594 goto out;
596 events = le64_to_cpu(sb->events);
597 if (events < bitmap->mddev->events) {
598 printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
599 "-- forcing full recovery\n", bmname(bitmap), events,
600 (unsigned long long) bitmap->mddev->events);
601 sb->state |= cpu_to_le32(BITMAP_STALE);
603 success:
604 /* assign fields using values from superblock */
605 bitmap->chunksize = chunksize;
606 bitmap->daemon_sleep = daemon_sleep;
607 bitmap->daemon_lastrun = jiffies;
608 bitmap->max_write_behind = write_behind;
609 bitmap->flags |= le32_to_cpu(sb->state);
610 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
611 bitmap->flags |= BITMAP_HOSTENDIAN;
612 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
613 if (sb->state & cpu_to_le32(BITMAP_STALE))
614 bitmap->events_cleared = bitmap->mddev->events;
615 err = 0;
616 out:
617 kunmap_atomic(sb, KM_USER0);
618 if (err)
619 bitmap_print_sb(bitmap);
620 return err;
623 enum bitmap_mask_op {
624 MASK_SET,
625 MASK_UNSET
628 /* record the state of the bitmap in the superblock. Return the old value */
629 static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
630 enum bitmap_mask_op op)
632 bitmap_super_t *sb;
633 unsigned long flags;
634 int old;
636 spin_lock_irqsave(&bitmap->lock, flags);
637 if (!bitmap->sb_page) { /* can't set the state */
638 spin_unlock_irqrestore(&bitmap->lock, flags);
639 return 0;
641 spin_unlock_irqrestore(&bitmap->lock, flags);
642 sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
643 old = le32_to_cpu(sb->state) & bits;
644 switch (op) {
645 case MASK_SET: sb->state |= cpu_to_le32(bits);
646 break;
647 case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
648 break;
649 default: BUG();
651 kunmap_atomic(sb, KM_USER0);
652 return old;
656 * general bitmap file operations
659 /* calculate the index of the page that contains this bit */
660 static inline unsigned long file_page_index(unsigned long chunk)
662 return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
665 /* calculate the (bit) offset of this bit within a page */
666 static inline unsigned long file_page_offset(unsigned long chunk)
668 return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
672 * return a pointer to the page in the filemap that contains the given bit
674 * this lookup is complicated by the fact that the bitmap sb might be exactly
675 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
676 * 0 or page 1
678 static inline struct page *filemap_get_page(struct bitmap *bitmap,
679 unsigned long chunk)
681 if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
682 return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
686 static void bitmap_file_unmap(struct bitmap *bitmap)
688 struct page **map, *sb_page;
689 unsigned long *attr;
690 int pages;
691 unsigned long flags;
693 spin_lock_irqsave(&bitmap->lock, flags);
694 map = bitmap->filemap;
695 bitmap->filemap = NULL;
696 attr = bitmap->filemap_attr;
697 bitmap->filemap_attr = NULL;
698 pages = bitmap->file_pages;
699 bitmap->file_pages = 0;
700 sb_page = bitmap->sb_page;
701 bitmap->sb_page = NULL;
702 spin_unlock_irqrestore(&bitmap->lock, flags);
704 while (pages--)
705 if (map[pages]->index != 0) /* 0 is sb_page, release it below */
706 free_buffers(map[pages]);
707 kfree(map);
708 kfree(attr);
710 if (sb_page)
711 free_buffers(sb_page);
714 static void bitmap_file_put(struct bitmap *bitmap)
716 struct file *file;
717 unsigned long flags;
719 spin_lock_irqsave(&bitmap->lock, flags);
720 file = bitmap->file;
721 bitmap->file = NULL;
722 spin_unlock_irqrestore(&bitmap->lock, flags);
724 if (file)
725 wait_event(bitmap->write_wait,
726 atomic_read(&bitmap->pending_writes)==0);
727 bitmap_file_unmap(bitmap);
729 if (file) {
730 struct inode *inode = file->f_path.dentry->d_inode;
731 invalidate_mapping_pages(inode->i_mapping, 0, -1);
732 fput(file);
738 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
739 * then it is no longer reliable, so we stop using it and we mark the file
740 * as failed in the superblock
742 static void bitmap_file_kick(struct bitmap *bitmap)
744 char *path, *ptr = NULL;
746 if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
747 bitmap_update_sb(bitmap);
749 if (bitmap->file) {
750 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
751 if (path)
752 ptr = d_path(&bitmap->file->f_path, path,
753 PAGE_SIZE);
756 printk(KERN_ALERT
757 "%s: kicking failed bitmap file %s from array!\n",
758 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
760 kfree(path);
761 } else
762 printk(KERN_ALERT
763 "%s: disabling internal bitmap due to errors\n",
764 bmname(bitmap));
767 bitmap_file_put(bitmap);
769 return;
772 enum bitmap_page_attr {
773 BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
774 BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
775 BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
778 static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
779 enum bitmap_page_attr attr)
781 __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
784 static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
785 enum bitmap_page_attr attr)
787 __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
790 static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
791 enum bitmap_page_attr attr)
793 return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
797 * bitmap_file_set_bit -- called before performing a write to the md device
798 * to set (and eventually sync) a particular bit in the bitmap file
800 * we set the bit immediately, then we record the page number so that
801 * when an unplug occurs, we can flush the dirty pages out to disk
803 static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
805 unsigned long bit;
806 struct page *page;
807 void *kaddr;
808 unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
810 if (!bitmap->filemap) {
811 return;
814 page = filemap_get_page(bitmap, chunk);
815 if (!page) return;
816 bit = file_page_offset(chunk);
818 /* set the bit */
819 kaddr = kmap_atomic(page, KM_USER0);
820 if (bitmap->flags & BITMAP_HOSTENDIAN)
821 set_bit(bit, kaddr);
822 else
823 ext2_set_bit(bit, kaddr);
824 kunmap_atomic(kaddr, KM_USER0);
825 PRINTK("set file bit %lu page %lu\n", bit, page->index);
827 /* record page number so it gets flushed to disk when unplug occurs */
828 set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
832 /* this gets called when the md device is ready to unplug its underlying
833 * (slave) device queues -- before we let any writes go down, we need to
834 * sync the dirty pages of the bitmap file to disk */
835 void bitmap_unplug(struct bitmap *bitmap)
837 unsigned long i, flags;
838 int dirty, need_write;
839 struct page *page;
840 int wait = 0;
842 if (!bitmap)
843 return;
845 /* look at each page to see if there are any set bits that need to be
846 * flushed out to disk */
847 for (i = 0; i < bitmap->file_pages; i++) {
848 spin_lock_irqsave(&bitmap->lock, flags);
849 if (!bitmap->filemap) {
850 spin_unlock_irqrestore(&bitmap->lock, flags);
851 return;
853 page = bitmap->filemap[i];
854 dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
855 need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
856 clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
857 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
858 if (dirty)
859 wait = 1;
860 spin_unlock_irqrestore(&bitmap->lock, flags);
862 if (dirty | need_write)
863 write_page(bitmap, page, 0);
865 if (wait) { /* if any writes were performed, we need to wait on them */
866 if (bitmap->file)
867 wait_event(bitmap->write_wait,
868 atomic_read(&bitmap->pending_writes)==0);
869 else
870 md_super_wait(bitmap->mddev);
872 if (bitmap->flags & BITMAP_WRITE_ERROR)
873 bitmap_file_kick(bitmap);
876 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
877 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
878 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
879 * memory mapping of the bitmap file
880 * Special cases:
881 * if there's no bitmap file, or if the bitmap file had been
882 * previously kicked from the array, we mark all the bits as
883 * 1's in order to cause a full resync.
885 * We ignore all bits for sectors that end earlier than 'start'.
886 * This is used when reading an out-of-date bitmap...
888 static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
890 unsigned long i, chunks, index, oldindex, bit;
891 struct page *page = NULL, *oldpage = NULL;
892 unsigned long num_pages, bit_cnt = 0;
893 struct file *file;
894 unsigned long bytes, offset;
895 int outofdate;
896 int ret = -ENOSPC;
897 void *paddr;
899 chunks = bitmap->chunks;
900 file = bitmap->file;
902 BUG_ON(!file && !bitmap->offset);
904 #ifdef INJECT_FAULTS_3
905 outofdate = 1;
906 #else
907 outofdate = bitmap->flags & BITMAP_STALE;
908 #endif
909 if (outofdate)
910 printk(KERN_INFO "%s: bitmap file is out of date, doing full "
911 "recovery\n", bmname(bitmap));
913 bytes = (chunks + 7) / 8;
915 num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
917 if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
918 printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
919 bmname(bitmap),
920 (unsigned long) i_size_read(file->f_mapping->host),
921 bytes + sizeof(bitmap_super_t));
922 goto err;
925 ret = -ENOMEM;
927 bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
928 if (!bitmap->filemap)
929 goto err;
931 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
932 bitmap->filemap_attr = kzalloc(
933 roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
934 GFP_KERNEL);
935 if (!bitmap->filemap_attr)
936 goto err;
938 oldindex = ~0L;
940 for (i = 0; i < chunks; i++) {
941 int b;
942 index = file_page_index(i);
943 bit = file_page_offset(i);
944 if (index != oldindex) { /* this is a new page, read it in */
945 int count;
946 /* unmap the old page, we're done with it */
947 if (index == num_pages-1)
948 count = bytes + sizeof(bitmap_super_t)
949 - index * PAGE_SIZE;
950 else
951 count = PAGE_SIZE;
952 if (index == 0) {
954 * if we're here then the superblock page
955 * contains some bits (PAGE_SIZE != sizeof sb)
956 * we've already read it in, so just use it
958 page = bitmap->sb_page;
959 offset = sizeof(bitmap_super_t);
960 } else if (file) {
961 page = read_page(file, index, bitmap, count);
962 offset = 0;
963 } else {
964 page = read_sb_page(bitmap->mddev, bitmap->offset, index);
965 offset = 0;
967 if (IS_ERR(page)) { /* read error */
968 ret = PTR_ERR(page);
969 goto err;
972 oldindex = index;
973 oldpage = page;
975 if (outofdate) {
977 * if bitmap is out of date, dirty the
978 * whole page and write it out
980 paddr = kmap_atomic(page, KM_USER0);
981 memset(paddr + offset, 0xff,
982 PAGE_SIZE - offset);
983 kunmap_atomic(paddr, KM_USER0);
984 write_page(bitmap, page, 1);
986 ret = -EIO;
987 if (bitmap->flags & BITMAP_WRITE_ERROR) {
988 /* release, page not in filemap yet */
989 put_page(page);
990 goto err;
994 bitmap->filemap[bitmap->file_pages++] = page;
995 bitmap->last_page_size = count;
997 paddr = kmap_atomic(page, KM_USER0);
998 if (bitmap->flags & BITMAP_HOSTENDIAN)
999 b = test_bit(bit, paddr);
1000 else
1001 b = ext2_test_bit(bit, paddr);
1002 kunmap_atomic(paddr, KM_USER0);
1003 if (b) {
1004 /* if the disk bit is set, set the memory bit */
1005 bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
1006 ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
1008 bit_cnt++;
1009 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1013 /* everything went OK */
1014 ret = 0;
1015 bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
1017 if (bit_cnt) { /* Kick recovery if any bits were set */
1018 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1019 md_wakeup_thread(bitmap->mddev->thread);
1022 printk(KERN_INFO "%s: bitmap initialized from disk: "
1023 "read %lu/%lu pages, set %lu bits\n",
1024 bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
1026 return 0;
1028 err:
1029 printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
1030 bmname(bitmap), ret);
1031 return ret;
1034 void bitmap_write_all(struct bitmap *bitmap)
1036 /* We don't actually write all bitmap blocks here,
1037 * just flag them as needing to be written
1039 int i;
1041 for (i=0; i < bitmap->file_pages; i++)
1042 set_page_attr(bitmap, bitmap->filemap[i],
1043 BITMAP_PAGE_NEEDWRITE);
1047 static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
1049 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1050 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1051 bitmap->bp[page].count += inc;
1053 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1054 (unsigned long long)offset, inc, bitmap->bp[page].count);
1056 bitmap_checkfree(bitmap, page);
1058 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1059 sector_t offset, int *blocks,
1060 int create);
1063 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1064 * out to disk
1067 void bitmap_daemon_work(struct bitmap *bitmap)
1069 unsigned long j;
1070 unsigned long flags;
1071 struct page *page = NULL, *lastpage = NULL;
1072 int blocks;
1073 void *paddr;
1075 if (bitmap == NULL)
1076 return;
1077 if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
1078 goto done;
1080 bitmap->daemon_lastrun = jiffies;
1081 if (bitmap->allclean) {
1082 bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1083 return;
1085 bitmap->allclean = 1;
1087 for (j = 0; j < bitmap->chunks; j++) {
1088 bitmap_counter_t *bmc;
1089 spin_lock_irqsave(&bitmap->lock, flags);
1090 if (!bitmap->filemap) {
1091 /* error or shutdown */
1092 spin_unlock_irqrestore(&bitmap->lock, flags);
1093 break;
1096 page = filemap_get_page(bitmap, j);
1098 if (page != lastpage) {
1099 /* skip this page unless it's marked as needing cleaning */
1100 if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
1101 int need_write = test_page_attr(bitmap, page,
1102 BITMAP_PAGE_NEEDWRITE);
1103 if (need_write)
1104 clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
1106 spin_unlock_irqrestore(&bitmap->lock, flags);
1107 if (need_write) {
1108 write_page(bitmap, page, 0);
1109 bitmap->allclean = 0;
1111 continue;
1114 /* grab the new page, sync and release the old */
1115 if (lastpage != NULL) {
1116 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1117 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1118 spin_unlock_irqrestore(&bitmap->lock, flags);
1119 write_page(bitmap, lastpage, 0);
1120 } else {
1121 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1122 spin_unlock_irqrestore(&bitmap->lock, flags);
1124 } else
1125 spin_unlock_irqrestore(&bitmap->lock, flags);
1126 lastpage = page;
1128 /* We are possibly going to clear some bits, so make
1129 * sure that events_cleared is up-to-date.
1131 if (bitmap->need_sync) {
1132 bitmap_super_t *sb;
1133 bitmap->need_sync = 0;
1134 sb = kmap_atomic(bitmap->sb_page, KM_USER0);
1135 sb->events_cleared =
1136 cpu_to_le64(bitmap->events_cleared);
1137 kunmap_atomic(sb, KM_USER0);
1138 write_page(bitmap, bitmap->sb_page, 1);
1140 spin_lock_irqsave(&bitmap->lock, flags);
1141 clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1143 bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1144 &blocks, 0);
1145 if (bmc) {
1147 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1149 if (*bmc)
1150 bitmap->allclean = 0;
1152 if (*bmc == 2) {
1153 *bmc=1; /* maybe clear the bit next time */
1154 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1155 } else if (*bmc == 1) {
1156 /* we can clear the bit */
1157 *bmc = 0;
1158 bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
1159 -1);
1161 /* clear the bit */
1162 paddr = kmap_atomic(page, KM_USER0);
1163 if (bitmap->flags & BITMAP_HOSTENDIAN)
1164 clear_bit(file_page_offset(j), paddr);
1165 else
1166 ext2_clear_bit(file_page_offset(j), paddr);
1167 kunmap_atomic(paddr, KM_USER0);
1170 spin_unlock_irqrestore(&bitmap->lock, flags);
1173 /* now sync the final page */
1174 if (lastpage != NULL) {
1175 spin_lock_irqsave(&bitmap->lock, flags);
1176 if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
1177 clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1178 spin_unlock_irqrestore(&bitmap->lock, flags);
1179 write_page(bitmap, lastpage, 0);
1180 } else {
1181 set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
1182 spin_unlock_irqrestore(&bitmap->lock, flags);
1186 done:
1187 if (bitmap->allclean == 0)
1188 bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1191 static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
1192 sector_t offset, int *blocks,
1193 int create)
1195 /* If 'create', we might release the lock and reclaim it.
1196 * The lock must have been taken with interrupts enabled.
1197 * If !create, we don't release the lock.
1199 sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
1200 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1201 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1202 sector_t csize;
1204 if (bitmap_checkpage(bitmap, page, create) < 0) {
1205 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1206 *blocks = csize - (offset & (csize- 1));
1207 return NULL;
1209 /* now locked ... */
1211 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1212 /* should we use the first or second counter field
1213 * of the hijacked pointer? */
1214 int hi = (pageoff > PAGE_COUNTER_MASK);
1215 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
1216 PAGE_COUNTER_SHIFT - 1);
1217 *blocks = csize - (offset & (csize- 1));
1218 return &((bitmap_counter_t *)
1219 &bitmap->bp[page].map)[hi];
1220 } else { /* page is allocated */
1221 csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
1222 *blocks = csize - (offset & (csize- 1));
1223 return (bitmap_counter_t *)
1224 &(bitmap->bp[page].map[pageoff]);
1228 int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1230 if (!bitmap) return 0;
1232 if (behind) {
1233 atomic_inc(&bitmap->behind_writes);
1234 PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
1235 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1238 while (sectors) {
1239 int blocks;
1240 bitmap_counter_t *bmc;
1242 spin_lock_irq(&bitmap->lock);
1243 bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
1244 if (!bmc) {
1245 spin_unlock_irq(&bitmap->lock);
1246 return 0;
1249 if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
1250 DEFINE_WAIT(__wait);
1251 /* note that it is safe to do the prepare_to_wait
1252 * after the test as long as we do it before dropping
1253 * the spinlock.
1255 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1256 TASK_UNINTERRUPTIBLE);
1257 spin_unlock_irq(&bitmap->lock);
1258 blk_unplug(bitmap->mddev->queue);
1259 schedule();
1260 finish_wait(&bitmap->overflow_wait, &__wait);
1261 continue;
1264 switch(*bmc) {
1265 case 0:
1266 bitmap_file_set_bit(bitmap, offset);
1267 bitmap_count_page(bitmap,offset, 1);
1268 blk_plug_device_unlocked(bitmap->mddev->queue);
1269 /* fall through */
1270 case 1:
1271 *bmc = 2;
1274 (*bmc)++;
1276 spin_unlock_irq(&bitmap->lock);
1278 offset += blocks;
1279 if (sectors > blocks)
1280 sectors -= blocks;
1281 else sectors = 0;
1283 bitmap->allclean = 0;
1284 return 0;
1287 void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
1288 int success, int behind)
1290 if (!bitmap) return;
1291 if (behind) {
1292 atomic_dec(&bitmap->behind_writes);
1293 PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
1294 atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
1297 while (sectors) {
1298 int blocks;
1299 unsigned long flags;
1300 bitmap_counter_t *bmc;
1302 spin_lock_irqsave(&bitmap->lock, flags);
1303 bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
1304 if (!bmc) {
1305 spin_unlock_irqrestore(&bitmap->lock, flags);
1306 return;
1309 if (success &&
1310 bitmap->events_cleared < bitmap->mddev->events) {
1311 bitmap->events_cleared = bitmap->mddev->events;
1312 bitmap->need_sync = 1;
1315 if (!success && ! (*bmc & NEEDED_MASK))
1316 *bmc |= NEEDED_MASK;
1318 if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
1319 wake_up(&bitmap->overflow_wait);
1321 (*bmc)--;
1322 if (*bmc <= 2) {
1323 set_page_attr(bitmap,
1324 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1325 BITMAP_PAGE_CLEAN);
1327 spin_unlock_irqrestore(&bitmap->lock, flags);
1328 offset += blocks;
1329 if (sectors > blocks)
1330 sectors -= blocks;
1331 else sectors = 0;
1335 int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
1336 int degraded)
1338 bitmap_counter_t *bmc;
1339 int rv;
1340 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1341 *blocks = 1024;
1342 return 1; /* always resync if no bitmap */
1344 spin_lock_irq(&bitmap->lock);
1345 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1346 rv = 0;
1347 if (bmc) {
1348 /* locked */
1349 if (RESYNC(*bmc))
1350 rv = 1;
1351 else if (NEEDED(*bmc)) {
1352 rv = 1;
1353 if (!degraded) { /* don't set/clear bits if degraded */
1354 *bmc |= RESYNC_MASK;
1355 *bmc &= ~NEEDED_MASK;
1359 spin_unlock_irq(&bitmap->lock);
1360 bitmap->allclean = 0;
1361 return rv;
1364 void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
1366 bitmap_counter_t *bmc;
1367 unsigned long flags;
1369 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1370 */ if (bitmap == NULL) {
1371 *blocks = 1024;
1372 return;
1374 spin_lock_irqsave(&bitmap->lock, flags);
1375 bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
1376 if (bmc == NULL)
1377 goto unlock;
1378 /* locked */
1380 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1382 if (RESYNC(*bmc)) {
1383 *bmc &= ~RESYNC_MASK;
1385 if (!NEEDED(*bmc) && aborted)
1386 *bmc |= NEEDED_MASK;
1387 else {
1388 if (*bmc <= 2) {
1389 set_page_attr(bitmap,
1390 filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
1391 BITMAP_PAGE_CLEAN);
1395 unlock:
1396 spin_unlock_irqrestore(&bitmap->lock, flags);
1397 bitmap->allclean = 0;
1400 void bitmap_close_sync(struct bitmap *bitmap)
1402 /* Sync has finished, and any bitmap chunks that weren't synced
1403 * properly have been aborted. It remains to us to clear the
1404 * RESYNC bit wherever it is still on
1406 sector_t sector = 0;
1407 int blocks;
1408 if (!bitmap)
1409 return;
1410 while (sector < bitmap->mddev->resync_max_sectors) {
1411 bitmap_end_sync(bitmap, sector, &blocks, 0);
1412 sector += blocks;
1416 void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
1418 sector_t s = 0;
1419 int blocks;
1421 if (!bitmap)
1422 return;
1423 if (sector == 0) {
1424 bitmap->last_end_sync = jiffies;
1425 return;
1427 if (time_before(jiffies, (bitmap->last_end_sync
1428 + bitmap->daemon_sleep * HZ)))
1429 return;
1430 wait_event(bitmap->mddev->recovery_wait,
1431 atomic_read(&bitmap->mddev->recovery_active) == 0);
1433 sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
1434 s = 0;
1435 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1436 bitmap_end_sync(bitmap, s, &blocks, 0);
1437 s += blocks;
1439 bitmap->last_end_sync = jiffies;
1442 static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1444 /* For each chunk covered by any of these sectors, set the
1445 * counter to 1 and set resync_needed. They should all
1446 * be 0 at this point
1449 int secs;
1450 bitmap_counter_t *bmc;
1451 spin_lock_irq(&bitmap->lock);
1452 bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
1453 if (!bmc) {
1454 spin_unlock_irq(&bitmap->lock);
1455 return;
1457 if (! *bmc) {
1458 struct page *page;
1459 *bmc = 1 | (needed?NEEDED_MASK:0);
1460 bitmap_count_page(bitmap, offset, 1);
1461 page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
1462 set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
1464 spin_unlock_irq(&bitmap->lock);
1465 bitmap->allclean = 0;
1468 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1469 void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1471 unsigned long chunk;
1473 for (chunk = s; chunk <= e; chunk++) {
1474 sector_t sec = chunk << CHUNK_BLOCK_SHIFT(bitmap);
1475 bitmap_set_memory_bits(bitmap, sec, 1);
1476 bitmap_file_set_bit(bitmap, sec);
1481 * flush out any pending updates
1483 void bitmap_flush(mddev_t *mddev)
1485 struct bitmap *bitmap = mddev->bitmap;
1486 int sleep;
1488 if (!bitmap) /* there was no bitmap */
1489 return;
1491 /* run the daemon_work three time to ensure everything is flushed
1492 * that can be
1494 sleep = bitmap->daemon_sleep;
1495 bitmap->daemon_sleep = 0;
1496 bitmap_daemon_work(bitmap);
1497 bitmap_daemon_work(bitmap);
1498 bitmap_daemon_work(bitmap);
1499 bitmap->daemon_sleep = sleep;
1500 bitmap_update_sb(bitmap);
1504 * free memory that was allocated
1506 static void bitmap_free(struct bitmap *bitmap)
1508 unsigned long k, pages;
1509 struct bitmap_page *bp;
1511 if (!bitmap) /* there was no bitmap */
1512 return;
1514 /* release the bitmap file and kill the daemon */
1515 bitmap_file_put(bitmap);
1517 bp = bitmap->bp;
1518 pages = bitmap->pages;
1520 /* free all allocated memory */
1522 if (bp) /* deallocate the page memory */
1523 for (k = 0; k < pages; k++)
1524 if (bp[k].map && !bp[k].hijacked)
1525 kfree(bp[k].map);
1526 kfree(bp);
1527 kfree(bitmap);
1529 void bitmap_destroy(mddev_t *mddev)
1531 struct bitmap *bitmap = mddev->bitmap;
1533 if (!bitmap) /* there was no bitmap */
1534 return;
1536 mddev->bitmap = NULL; /* disconnect from the md device */
1537 if (mddev->thread)
1538 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1540 bitmap_free(bitmap);
1544 * initialize the bitmap structure
1545 * if this returns an error, bitmap_destroy must be called to do clean up
1547 int bitmap_create(mddev_t *mddev)
1549 struct bitmap *bitmap;
1550 unsigned long blocks = mddev->resync_max_sectors;
1551 unsigned long chunks;
1552 unsigned long pages;
1553 struct file *file = mddev->bitmap_file;
1554 int err;
1555 sector_t start;
1557 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
1559 if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
1560 return 0;
1562 BUG_ON(file && mddev->bitmap_offset);
1564 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1565 if (!bitmap)
1566 return -ENOMEM;
1568 spin_lock_init(&bitmap->lock);
1569 atomic_set(&bitmap->pending_writes, 0);
1570 init_waitqueue_head(&bitmap->write_wait);
1571 init_waitqueue_head(&bitmap->overflow_wait);
1573 bitmap->mddev = mddev;
1575 bitmap->file = file;
1576 bitmap->offset = mddev->bitmap_offset;
1577 if (file) {
1578 get_file(file);
1579 do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
1580 SYNC_FILE_RANGE_WAIT_BEFORE |
1581 SYNC_FILE_RANGE_WRITE |
1582 SYNC_FILE_RANGE_WAIT_AFTER);
1584 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1585 err = bitmap_read_sb(bitmap);
1586 if (err)
1587 goto error;
1589 bitmap->chunkshift = ffz(~bitmap->chunksize);
1591 /* now that chunksize and chunkshift are set, we can use these macros */
1592 chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
1593 CHUNK_BLOCK_RATIO(bitmap);
1594 pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
1596 BUG_ON(!pages);
1598 bitmap->chunks = chunks;
1599 bitmap->pages = pages;
1600 bitmap->missing_pages = pages;
1601 bitmap->counter_bits = COUNTER_BITS;
1603 bitmap->syncchunk = ~0UL;
1605 #ifdef INJECT_FATAL_FAULT_1
1606 bitmap->bp = NULL;
1607 #else
1608 bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
1609 #endif
1610 err = -ENOMEM;
1611 if (!bitmap->bp)
1612 goto error;
1614 /* now that we have some pages available, initialize the in-memory
1615 * bitmap from the on-disk bitmap */
1616 start = 0;
1617 if (mddev->degraded == 0
1618 || bitmap->events_cleared == mddev->events)
1619 /* no need to keep dirty bits to optimise a re-add of a missing device */
1620 start = mddev->recovery_cp;
1621 err = bitmap_init_from_disk(bitmap, start);
1623 if (err)
1624 goto error;
1626 printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
1627 pages, bmname(bitmap));
1629 mddev->bitmap = bitmap;
1631 mddev->thread->timeout = bitmap->daemon_sleep * HZ;
1633 bitmap_update_sb(bitmap);
1635 return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
1637 error:
1638 bitmap_free(bitmap);
1639 return err;
1642 /* the bitmap API -- for raid personalities */
1643 EXPORT_SYMBOL(bitmap_startwrite);
1644 EXPORT_SYMBOL(bitmap_endwrite);
1645 EXPORT_SYMBOL(bitmap_start_sync);
1646 EXPORT_SYMBOL(bitmap_end_sync);
1647 EXPORT_SYMBOL(bitmap_unplug);
1648 EXPORT_SYMBOL(bitmap_close_sync);
1649 EXPORT_SYMBOL(bitmap_cond_end_sync);