2 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
4 * bitmap_create - sets up the bitmap structure
5 * bitmap_destroy - destroys the bitmap structure
7 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
8 * - added disk storage for bitmap
9 * - changes to allow various bitmap chunk sizes
15 * flush after percent set rather than just time based. (maybe both).
16 * wait if count gets too high, wake when it drops to half.
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>
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 */
53 //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
54 #define DPRINTK(x...) do { } while(0)
58 # define PRINTK(x...) printk(KERN_DEBUG x)
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
)
77 #ifdef INJECT_FAULTS_1
80 page
= kmalloc(PAGE_SIZE
, GFP_NOIO
);
83 printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap
));
85 PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
86 bmname(bitmap
), 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
);
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
) {
115 "%s: invalid bitmap page request: %lu (> %lu)\n",
116 bmname(bitmap
), page
, bitmap
->pages
-1);
121 if (bitmap
->bp
[page
].hijacked
) /* it's hijacked, don't try to alloc */
124 if (bitmap
->bp
[page
].map
) /* page is already allocated, just return */
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",
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;
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
);
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
--;
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
)
174 if (bitmap
->bp
[page
].count
) /* page is still busy */
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
;
185 /* normal case, free the page */
188 /* actually ... let's not. We will probably need the page again exactly when
189 * memory is tight and we are flusing to disk
193 ptr
= bitmap
->bp
[page
].map
;
194 bitmap
->bp
[page
].map
= NULL
;
195 bitmap
->missing_pages
++;
196 bitmap_free_page(bitmap
, ptr
);
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
,
213 unsigned long index
, int size
)
215 /* choose a good rdev and read the page from there */
218 struct list_head
*tmp
;
222 page
= alloc_page(GFP_KERNEL
);
224 return ERR_PTR(-ENOMEM
);
226 rdev_for_each(rdev
, tmp
, mddev
) {
227 if (! test_bit(In_sync
, &rdev
->flags
)
228 || test_bit(Faulty
, &rdev
->flags
))
231 target
= rdev
->sb_start
+ offset
+ index
* (PAGE_SIZE
/512);
233 if (sync_page_io(rdev
->bdev
, target
,
234 roundup(size
, bdev_hardsect_size(rdev
->bdev
)),
237 attach_page_buffers(page
, NULL
); /* so that free_buffer will
242 return ERR_PTR(-EIO
);
246 static mdk_rdev_t
*next_active_rdev(mdk_rdev_t
*rdev
, mddev_t
*mddev
)
248 /* Iterate the disks of an mddev, using rcu to protect access to the
249 * linked list, and raising the refcount of devices we return to ensure
250 * they don't disappear while in use.
251 * As devices are only added or removed when raid_disk is < 0 and
252 * nr_pending is 0 and In_sync is clear, the entries we return will
253 * still be in the same position on the list when we re-enter
254 * list_for_each_continue_rcu.
256 struct list_head
*pos
;
259 /* start at the beginning */
262 /* release the previous rdev and start from there. */
263 rdev_dec_pending(rdev
, mddev
);
264 pos
= &rdev
->same_set
;
266 list_for_each_continue_rcu(pos
, &mddev
->disks
) {
267 rdev
= list_entry(pos
, mdk_rdev_t
, same_set
);
268 if (rdev
->raid_disk
>= 0 &&
269 test_bit(In_sync
, &rdev
->flags
) &&
270 !test_bit(Faulty
, &rdev
->flags
)) {
271 /* this is a usable devices */
272 atomic_inc(&rdev
->nr_pending
);
281 static int write_sb_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
283 mdk_rdev_t
*rdev
= NULL
;
284 mddev_t
*mddev
= bitmap
->mddev
;
286 while ((rdev
= next_active_rdev(rdev
, mddev
)) != NULL
) {
287 int size
= PAGE_SIZE
;
288 if (page
->index
== bitmap
->file_pages
-1)
289 size
= roundup(bitmap
->last_page_size
,
290 bdev_hardsect_size(rdev
->bdev
));
291 /* Just make sure we aren't corrupting data or
294 if (bitmap
->offset
< 0) {
295 /* DATA BITMAP METADATA */
297 + (long)(page
->index
* (PAGE_SIZE
/512))
299 /* bitmap runs in to metadata */
301 if (rdev
->data_offset
+ mddev
->size
*2
302 > rdev
->sb_start
+ bitmap
->offset
)
303 /* data runs in to bitmap */
305 } else if (rdev
->sb_start
< rdev
->data_offset
) {
306 /* METADATA BITMAP DATA */
309 + page
->index
*(PAGE_SIZE
/512) + size
/512
311 /* bitmap runs in to data */
314 /* DATA METADATA BITMAP - no problems */
316 md_super_write(mddev
, rdev
,
317 rdev
->sb_start
+ bitmap
->offset
318 + page
->index
* (PAGE_SIZE
/512),
324 md_super_wait(mddev
);
332 static void bitmap_file_kick(struct bitmap
*bitmap
);
334 * write out a page to a file
336 static void write_page(struct bitmap
*bitmap
, struct page
*page
, int wait
)
338 struct buffer_head
*bh
;
340 if (bitmap
->file
== NULL
) {
341 switch (write_sb_page(bitmap
, page
, wait
)) {
343 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
347 bh
= page_buffers(page
);
349 while (bh
&& bh
->b_blocknr
) {
350 atomic_inc(&bitmap
->pending_writes
);
351 set_buffer_locked(bh
);
352 set_buffer_mapped(bh
);
353 submit_bh(WRITE
, bh
);
354 bh
= bh
->b_this_page
;
358 wait_event(bitmap
->write_wait
,
359 atomic_read(&bitmap
->pending_writes
)==0);
362 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
363 bitmap_file_kick(bitmap
);
366 static void end_bitmap_write(struct buffer_head
*bh
, int uptodate
)
368 struct bitmap
*bitmap
= bh
->b_private
;
372 spin_lock_irqsave(&bitmap
->lock
, flags
);
373 bitmap
->flags
|= BITMAP_WRITE_ERROR
;
374 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
376 if (atomic_dec_and_test(&bitmap
->pending_writes
))
377 wake_up(&bitmap
->write_wait
);
380 /* copied from buffer.c */
382 __clear_page_buffers(struct page
*page
)
384 ClearPagePrivate(page
);
385 set_page_private(page
, 0);
386 page_cache_release(page
);
388 static void free_buffers(struct page
*page
)
390 struct buffer_head
*bh
= page_buffers(page
);
393 struct buffer_head
*next
= bh
->b_this_page
;
394 free_buffer_head(bh
);
397 __clear_page_buffers(page
);
401 /* read a page from a file.
402 * We both read the page, and attach buffers to the page to record the
403 * address of each block (using bmap). These addresses will be used
404 * to write the block later, completely bypassing the filesystem.
405 * This usage is similar to how swap files are handled, and allows us
406 * to write to a file with no concerns of memory allocation failing.
408 static struct page
*read_page(struct file
*file
, unsigned long index
,
409 struct bitmap
*bitmap
,
412 struct page
*page
= NULL
;
413 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
414 struct buffer_head
*bh
;
417 PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE
,
418 (unsigned long long)index
<< PAGE_SHIFT
);
420 page
= alloc_page(GFP_KERNEL
);
422 page
= ERR_PTR(-ENOMEM
);
426 bh
= alloc_page_buffers(page
, 1<<inode
->i_blkbits
, 0);
429 page
= ERR_PTR(-ENOMEM
);
432 attach_page_buffers(page
, bh
);
433 block
= index
<< (PAGE_SHIFT
- inode
->i_blkbits
);
438 bh
->b_blocknr
= bmap(inode
, block
);
439 if (bh
->b_blocknr
== 0) {
440 /* Cannot use this file! */
442 page
= ERR_PTR(-EINVAL
);
445 bh
->b_bdev
= inode
->i_sb
->s_bdev
;
446 if (count
< (1<<inode
->i_blkbits
))
449 count
-= (1<<inode
->i_blkbits
);
451 bh
->b_end_io
= end_bitmap_write
;
452 bh
->b_private
= bitmap
;
453 atomic_inc(&bitmap
->pending_writes
);
454 set_buffer_locked(bh
);
455 set_buffer_mapped(bh
);
459 bh
= bh
->b_this_page
;
463 wait_event(bitmap
->write_wait
,
464 atomic_read(&bitmap
->pending_writes
)==0);
465 if (bitmap
->flags
& BITMAP_WRITE_ERROR
) {
467 page
= ERR_PTR(-EIO
);
471 printk(KERN_ALERT
"md: bitmap read error: (%dB @ %Lu): %ld\n",
473 (unsigned long long)index
<< PAGE_SHIFT
,
479 * bitmap file superblock operations
482 /* update the event counter and sync the superblock to disk */
483 void bitmap_update_sb(struct bitmap
*bitmap
)
488 if (!bitmap
|| !bitmap
->mddev
) /* no bitmap for this array */
490 spin_lock_irqsave(&bitmap
->lock
, flags
);
491 if (!bitmap
->sb_page
) { /* no superblock */
492 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
495 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
496 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
497 sb
->events
= cpu_to_le64(bitmap
->mddev
->events
);
498 if (bitmap
->mddev
->events
< bitmap
->events_cleared
) {
499 /* rocking back to read-only */
500 bitmap
->events_cleared
= bitmap
->mddev
->events
;
501 sb
->events_cleared
= cpu_to_le64(bitmap
->events_cleared
);
503 kunmap_atomic(sb
, KM_USER0
);
504 write_page(bitmap
, bitmap
->sb_page
, 1);
507 /* print out the bitmap file superblock */
508 void bitmap_print_sb(struct bitmap
*bitmap
)
512 if (!bitmap
|| !bitmap
->sb_page
)
514 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
515 printk(KERN_DEBUG
"%s: bitmap file superblock:\n", bmname(bitmap
));
516 printk(KERN_DEBUG
" magic: %08x\n", le32_to_cpu(sb
->magic
));
517 printk(KERN_DEBUG
" version: %d\n", le32_to_cpu(sb
->version
));
518 printk(KERN_DEBUG
" uuid: %08x.%08x.%08x.%08x\n",
519 *(__u32
*)(sb
->uuid
+0),
520 *(__u32
*)(sb
->uuid
+4),
521 *(__u32
*)(sb
->uuid
+8),
522 *(__u32
*)(sb
->uuid
+12));
523 printk(KERN_DEBUG
" events: %llu\n",
524 (unsigned long long) le64_to_cpu(sb
->events
));
525 printk(KERN_DEBUG
"events cleared: %llu\n",
526 (unsigned long long) le64_to_cpu(sb
->events_cleared
));
527 printk(KERN_DEBUG
" state: %08x\n", le32_to_cpu(sb
->state
));
528 printk(KERN_DEBUG
" chunksize: %d B\n", le32_to_cpu(sb
->chunksize
));
529 printk(KERN_DEBUG
" daemon sleep: %ds\n", le32_to_cpu(sb
->daemon_sleep
));
530 printk(KERN_DEBUG
" sync size: %llu KB\n",
531 (unsigned long long)le64_to_cpu(sb
->sync_size
)/2);
532 printk(KERN_DEBUG
"max write behind: %d\n", le32_to_cpu(sb
->write_behind
));
533 kunmap_atomic(sb
, KM_USER0
);
536 /* read the superblock from the bitmap file and initialize some bitmap fields */
537 static int bitmap_read_sb(struct bitmap
*bitmap
)
541 unsigned long chunksize
, daemon_sleep
, write_behind
;
542 unsigned long long events
;
545 /* page 0 is the superblock, read it... */
547 loff_t isize
= i_size_read(bitmap
->file
->f_mapping
->host
);
548 int bytes
= isize
> PAGE_SIZE
? PAGE_SIZE
: isize
;
550 bitmap
->sb_page
= read_page(bitmap
->file
, 0, bitmap
, bytes
);
552 bitmap
->sb_page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
,
554 0, sizeof(bitmap_super_t
));
556 if (IS_ERR(bitmap
->sb_page
)) {
557 err
= PTR_ERR(bitmap
->sb_page
);
558 bitmap
->sb_page
= NULL
;
562 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
564 chunksize
= le32_to_cpu(sb
->chunksize
);
565 daemon_sleep
= le32_to_cpu(sb
->daemon_sleep
);
566 write_behind
= le32_to_cpu(sb
->write_behind
);
568 /* verify that the bitmap-specific fields are valid */
569 if (sb
->magic
!= cpu_to_le32(BITMAP_MAGIC
))
570 reason
= "bad magic";
571 else if (le32_to_cpu(sb
->version
) < BITMAP_MAJOR_LO
||
572 le32_to_cpu(sb
->version
) > BITMAP_MAJOR_HI
)
573 reason
= "unrecognized superblock version";
574 else if (chunksize
< PAGE_SIZE
)
575 reason
= "bitmap chunksize too small";
576 else if ((1 << ffz(~chunksize
)) != chunksize
)
577 reason
= "bitmap chunksize not a power of 2";
578 else if (daemon_sleep
< 1 || daemon_sleep
> MAX_SCHEDULE_TIMEOUT
/ HZ
)
579 reason
= "daemon sleep period out of range";
580 else if (write_behind
> COUNTER_MAX
)
581 reason
= "write-behind limit out of range (0 - 16383)";
583 printk(KERN_INFO
"%s: invalid bitmap file superblock: %s\n",
584 bmname(bitmap
), reason
);
588 /* keep the array size field of the bitmap superblock up to date */
589 sb
->sync_size
= cpu_to_le64(bitmap
->mddev
->resync_max_sectors
);
591 if (!bitmap
->mddev
->persistent
)
595 * if we have a persistent array superblock, compare the
596 * bitmap's UUID and event counter to the mddev's
598 if (memcmp(sb
->uuid
, bitmap
->mddev
->uuid
, 16)) {
599 printk(KERN_INFO
"%s: bitmap superblock UUID mismatch\n",
603 events
= le64_to_cpu(sb
->events
);
604 if (events
< bitmap
->mddev
->events
) {
605 printk(KERN_INFO
"%s: bitmap file is out of date (%llu < %llu) "
606 "-- forcing full recovery\n", bmname(bitmap
), events
,
607 (unsigned long long) bitmap
->mddev
->events
);
608 sb
->state
|= cpu_to_le32(BITMAP_STALE
);
611 /* assign fields using values from superblock */
612 bitmap
->chunksize
= chunksize
;
613 bitmap
->daemon_sleep
= daemon_sleep
;
614 bitmap
->daemon_lastrun
= jiffies
;
615 bitmap
->max_write_behind
= write_behind
;
616 bitmap
->flags
|= le32_to_cpu(sb
->state
);
617 if (le32_to_cpu(sb
->version
) == BITMAP_MAJOR_HOSTENDIAN
)
618 bitmap
->flags
|= BITMAP_HOSTENDIAN
;
619 bitmap
->events_cleared
= le64_to_cpu(sb
->events_cleared
);
620 if (sb
->state
& cpu_to_le32(BITMAP_STALE
))
621 bitmap
->events_cleared
= bitmap
->mddev
->events
;
624 kunmap_atomic(sb
, KM_USER0
);
626 bitmap_print_sb(bitmap
);
630 enum bitmap_mask_op
{
635 /* record the state of the bitmap in the superblock. Return the old value */
636 static int bitmap_mask_state(struct bitmap
*bitmap
, enum bitmap_state bits
,
637 enum bitmap_mask_op op
)
643 spin_lock_irqsave(&bitmap
->lock
, flags
);
644 if (!bitmap
->sb_page
) { /* can't set the state */
645 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
648 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
649 sb
= (bitmap_super_t
*)kmap_atomic(bitmap
->sb_page
, KM_USER0
);
650 old
= le32_to_cpu(sb
->state
) & bits
;
652 case MASK_SET
: sb
->state
|= cpu_to_le32(bits
);
654 case MASK_UNSET
: sb
->state
&= cpu_to_le32(~bits
);
658 kunmap_atomic(sb
, KM_USER0
);
663 * general bitmap file operations
666 /* calculate the index of the page that contains this bit */
667 static inline unsigned long file_page_index(unsigned long chunk
)
669 return CHUNK_BIT_OFFSET(chunk
) >> PAGE_BIT_SHIFT
;
672 /* calculate the (bit) offset of this bit within a page */
673 static inline unsigned long file_page_offset(unsigned long chunk
)
675 return CHUNK_BIT_OFFSET(chunk
) & (PAGE_BITS
- 1);
679 * return a pointer to the page in the filemap that contains the given bit
681 * this lookup is complicated by the fact that the bitmap sb might be exactly
682 * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
685 static inline struct page
*filemap_get_page(struct bitmap
*bitmap
,
688 if (file_page_index(chunk
) >= bitmap
->file_pages
) return NULL
;
689 return bitmap
->filemap
[file_page_index(chunk
) - file_page_index(0)];
693 static void bitmap_file_unmap(struct bitmap
*bitmap
)
695 struct page
**map
, *sb_page
;
700 spin_lock_irqsave(&bitmap
->lock
, flags
);
701 map
= bitmap
->filemap
;
702 bitmap
->filemap
= NULL
;
703 attr
= bitmap
->filemap_attr
;
704 bitmap
->filemap_attr
= NULL
;
705 pages
= bitmap
->file_pages
;
706 bitmap
->file_pages
= 0;
707 sb_page
= bitmap
->sb_page
;
708 bitmap
->sb_page
= NULL
;
709 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
712 if (map
[pages
]->index
!= 0) /* 0 is sb_page, release it below */
713 free_buffers(map
[pages
]);
718 free_buffers(sb_page
);
721 static void bitmap_file_put(struct bitmap
*bitmap
)
726 spin_lock_irqsave(&bitmap
->lock
, flags
);
729 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
732 wait_event(bitmap
->write_wait
,
733 atomic_read(&bitmap
->pending_writes
)==0);
734 bitmap_file_unmap(bitmap
);
737 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
738 invalidate_mapping_pages(inode
->i_mapping
, 0, -1);
745 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
746 * then it is no longer reliable, so we stop using it and we mark the file
747 * as failed in the superblock
749 static void bitmap_file_kick(struct bitmap
*bitmap
)
751 char *path
, *ptr
= NULL
;
753 if (bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_SET
) == 0) {
754 bitmap_update_sb(bitmap
);
757 path
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
759 ptr
= d_path(&bitmap
->file
->f_path
, path
,
764 "%s: kicking failed bitmap file %s from array!\n",
765 bmname(bitmap
), IS_ERR(ptr
) ? "" : ptr
);
770 "%s: disabling internal bitmap due to errors\n",
774 bitmap_file_put(bitmap
);
779 enum bitmap_page_attr
{
780 BITMAP_PAGE_DIRTY
= 0, // there are set bits that need to be synced
781 BITMAP_PAGE_CLEAN
= 1, // there are bits that might need to be cleared
782 BITMAP_PAGE_NEEDWRITE
=2, // there are cleared bits that need to be synced
785 static inline void set_page_attr(struct bitmap
*bitmap
, struct page
*page
,
786 enum bitmap_page_attr attr
)
788 __set_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
791 static inline void clear_page_attr(struct bitmap
*bitmap
, struct page
*page
,
792 enum bitmap_page_attr attr
)
794 __clear_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
797 static inline unsigned long test_page_attr(struct bitmap
*bitmap
, struct page
*page
,
798 enum bitmap_page_attr attr
)
800 return test_bit((page
->index
<<2) + attr
, bitmap
->filemap_attr
);
804 * bitmap_file_set_bit -- called before performing a write to the md device
805 * to set (and eventually sync) a particular bit in the bitmap file
807 * we set the bit immediately, then we record the page number so that
808 * when an unplug occurs, we can flush the dirty pages out to disk
810 static void bitmap_file_set_bit(struct bitmap
*bitmap
, sector_t block
)
815 unsigned long chunk
= block
>> CHUNK_BLOCK_SHIFT(bitmap
);
817 if (!bitmap
->filemap
) {
821 page
= filemap_get_page(bitmap
, chunk
);
823 bit
= file_page_offset(chunk
);
826 kaddr
= kmap_atomic(page
, KM_USER0
);
827 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
830 ext2_set_bit(bit
, kaddr
);
831 kunmap_atomic(kaddr
, KM_USER0
);
832 PRINTK("set file bit %lu page %lu\n", bit
, page
->index
);
834 /* record page number so it gets flushed to disk when unplug occurs */
835 set_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
839 /* this gets called when the md device is ready to unplug its underlying
840 * (slave) device queues -- before we let any writes go down, we need to
841 * sync the dirty pages of the bitmap file to disk */
842 void bitmap_unplug(struct bitmap
*bitmap
)
844 unsigned long i
, flags
;
845 int dirty
, need_write
;
852 /* look at each page to see if there are any set bits that need to be
853 * flushed out to disk */
854 for (i
= 0; i
< bitmap
->file_pages
; i
++) {
855 spin_lock_irqsave(&bitmap
->lock
, flags
);
856 if (!bitmap
->filemap
) {
857 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
860 page
= bitmap
->filemap
[i
];
861 dirty
= test_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
862 need_write
= test_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
863 clear_page_attr(bitmap
, page
, BITMAP_PAGE_DIRTY
);
864 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
867 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
869 if (dirty
| need_write
)
870 write_page(bitmap
, page
, 0);
872 if (wait
) { /* if any writes were performed, we need to wait on them */
874 wait_event(bitmap
->write_wait
,
875 atomic_read(&bitmap
->pending_writes
)==0);
877 md_super_wait(bitmap
->mddev
);
879 if (bitmap
->flags
& BITMAP_WRITE_ERROR
)
880 bitmap_file_kick(bitmap
);
883 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
);
884 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
885 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
886 * memory mapping of the bitmap file
888 * if there's no bitmap file, or if the bitmap file had been
889 * previously kicked from the array, we mark all the bits as
890 * 1's in order to cause a full resync.
892 * We ignore all bits for sectors that end earlier than 'start'.
893 * This is used when reading an out-of-date bitmap...
895 static int bitmap_init_from_disk(struct bitmap
*bitmap
, sector_t start
)
897 unsigned long i
, chunks
, index
, oldindex
, bit
;
898 struct page
*page
= NULL
, *oldpage
= NULL
;
899 unsigned long num_pages
, bit_cnt
= 0;
901 unsigned long bytes
, offset
;
906 chunks
= bitmap
->chunks
;
909 BUG_ON(!file
&& !bitmap
->offset
);
911 #ifdef INJECT_FAULTS_3
914 outofdate
= bitmap
->flags
& BITMAP_STALE
;
917 printk(KERN_INFO
"%s: bitmap file is out of date, doing full "
918 "recovery\n", bmname(bitmap
));
920 bytes
= (chunks
+ 7) / 8;
922 num_pages
= (bytes
+ sizeof(bitmap_super_t
) + PAGE_SIZE
- 1) / PAGE_SIZE
;
924 if (file
&& i_size_read(file
->f_mapping
->host
) < bytes
+ sizeof(bitmap_super_t
)) {
925 printk(KERN_INFO
"%s: bitmap file too short %lu < %lu\n",
927 (unsigned long) i_size_read(file
->f_mapping
->host
),
928 bytes
+ sizeof(bitmap_super_t
));
934 bitmap
->filemap
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
935 if (!bitmap
->filemap
)
938 /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
939 bitmap
->filemap_attr
= kzalloc(
940 roundup( DIV_ROUND_UP(num_pages
*4, 8), sizeof(unsigned long)),
942 if (!bitmap
->filemap_attr
)
947 for (i
= 0; i
< chunks
; i
++) {
949 index
= file_page_index(i
);
950 bit
= file_page_offset(i
);
951 if (index
!= oldindex
) { /* this is a new page, read it in */
953 /* unmap the old page, we're done with it */
954 if (index
== num_pages
-1)
955 count
= bytes
+ sizeof(bitmap_super_t
)
961 * if we're here then the superblock page
962 * contains some bits (PAGE_SIZE != sizeof sb)
963 * we've already read it in, so just use it
965 page
= bitmap
->sb_page
;
966 offset
= sizeof(bitmap_super_t
);
967 read_sb_page(bitmap
->mddev
, bitmap
->offset
,
971 page
= read_page(file
, index
, bitmap
, count
);
974 page
= read_sb_page(bitmap
->mddev
, bitmap
->offset
,
979 if (IS_ERR(page
)) { /* read error */
989 * if bitmap is out of date, dirty the
990 * whole page and write it out
992 paddr
= kmap_atomic(page
, KM_USER0
);
993 memset(paddr
+ offset
, 0xff,
995 kunmap_atomic(paddr
, KM_USER0
);
996 write_page(bitmap
, page
, 1);
999 if (bitmap
->flags
& BITMAP_WRITE_ERROR
) {
1000 /* release, page not in filemap yet */
1006 bitmap
->filemap
[bitmap
->file_pages
++] = page
;
1007 bitmap
->last_page_size
= count
;
1009 paddr
= kmap_atomic(page
, KM_USER0
);
1010 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1011 b
= test_bit(bit
, paddr
);
1013 b
= ext2_test_bit(bit
, paddr
);
1014 kunmap_atomic(paddr
, KM_USER0
);
1016 /* if the disk bit is set, set the memory bit */
1017 bitmap_set_memory_bits(bitmap
, i
<< CHUNK_BLOCK_SHIFT(bitmap
),
1018 ((i
+1) << (CHUNK_BLOCK_SHIFT(bitmap
)) >= start
)
1021 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1025 /* everything went OK */
1027 bitmap_mask_state(bitmap
, BITMAP_STALE
, MASK_UNSET
);
1029 if (bit_cnt
) { /* Kick recovery if any bits were set */
1030 set_bit(MD_RECOVERY_NEEDED
, &bitmap
->mddev
->recovery
);
1031 md_wakeup_thread(bitmap
->mddev
->thread
);
1034 printk(KERN_INFO
"%s: bitmap initialized from disk: "
1035 "read %lu/%lu pages, set %lu bits\n",
1036 bmname(bitmap
), bitmap
->file_pages
, num_pages
, bit_cnt
);
1041 printk(KERN_INFO
"%s: bitmap initialisation failed: %d\n",
1042 bmname(bitmap
), ret
);
1046 void bitmap_write_all(struct bitmap
*bitmap
)
1048 /* We don't actually write all bitmap blocks here,
1049 * just flag them as needing to be written
1053 for (i
=0; i
< bitmap
->file_pages
; i
++)
1054 set_page_attr(bitmap
, bitmap
->filemap
[i
],
1055 BITMAP_PAGE_NEEDWRITE
);
1059 static void bitmap_count_page(struct bitmap
*bitmap
, sector_t offset
, int inc
)
1061 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1062 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1063 bitmap
->bp
[page
].count
+= inc
;
1065 if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
1066 (unsigned long long)offset, inc, bitmap->bp[page].count);
1068 bitmap_checkfree(bitmap
, page
);
1070 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1071 sector_t offset
, int *blocks
,
1075 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1079 void bitmap_daemon_work(struct bitmap
*bitmap
)
1082 unsigned long flags
;
1083 struct page
*page
= NULL
, *lastpage
= NULL
;
1089 if (time_before(jiffies
, bitmap
->daemon_lastrun
+ bitmap
->daemon_sleep
*HZ
))
1092 bitmap
->daemon_lastrun
= jiffies
;
1093 if (bitmap
->allclean
) {
1094 bitmap
->mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1097 bitmap
->allclean
= 1;
1099 for (j
= 0; j
< bitmap
->chunks
; j
++) {
1100 bitmap_counter_t
*bmc
;
1101 spin_lock_irqsave(&bitmap
->lock
, flags
);
1102 if (!bitmap
->filemap
) {
1103 /* error or shutdown */
1104 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1108 page
= filemap_get_page(bitmap
, j
);
1110 if (page
!= lastpage
) {
1111 /* skip this page unless it's marked as needing cleaning */
1112 if (!test_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
)) {
1113 int need_write
= test_page_attr(bitmap
, page
,
1114 BITMAP_PAGE_NEEDWRITE
);
1116 clear_page_attr(bitmap
, page
, BITMAP_PAGE_NEEDWRITE
);
1118 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1120 write_page(bitmap
, page
, 0);
1121 bitmap
->allclean
= 0;
1126 /* grab the new page, sync and release the old */
1127 if (lastpage
!= NULL
) {
1128 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1129 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1130 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1131 write_page(bitmap
, lastpage
, 0);
1133 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1134 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1137 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1140 /* We are possibly going to clear some bits, so make
1141 * sure that events_cleared is up-to-date.
1143 if (bitmap
->need_sync
) {
1145 bitmap
->need_sync
= 0;
1146 sb
= kmap_atomic(bitmap
->sb_page
, KM_USER0
);
1147 sb
->events_cleared
=
1148 cpu_to_le64(bitmap
->events_cleared
);
1149 kunmap_atomic(sb
, KM_USER0
);
1150 write_page(bitmap
, bitmap
->sb_page
, 1);
1152 spin_lock_irqsave(&bitmap
->lock
, flags
);
1153 clear_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1155 bmc
= bitmap_get_counter(bitmap
, j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1159 if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
1162 bitmap
->allclean
= 0;
1165 *bmc
=1; /* maybe clear the bit next time */
1166 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1167 } else if (*bmc
== 1) {
1168 /* we can clear the bit */
1170 bitmap_count_page(bitmap
, j
<< CHUNK_BLOCK_SHIFT(bitmap
),
1174 paddr
= kmap_atomic(page
, KM_USER0
);
1175 if (bitmap
->flags
& BITMAP_HOSTENDIAN
)
1176 clear_bit(file_page_offset(j
), paddr
);
1178 ext2_clear_bit(file_page_offset(j
), paddr
);
1179 kunmap_atomic(paddr
, KM_USER0
);
1182 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1185 /* now sync the final page */
1186 if (lastpage
!= NULL
) {
1187 spin_lock_irqsave(&bitmap
->lock
, flags
);
1188 if (test_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
)) {
1189 clear_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1190 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1191 write_page(bitmap
, lastpage
, 0);
1193 set_page_attr(bitmap
, lastpage
, BITMAP_PAGE_NEEDWRITE
);
1194 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1199 if (bitmap
->allclean
== 0)
1200 bitmap
->mddev
->thread
->timeout
= bitmap
->daemon_sleep
* HZ
;
1203 static bitmap_counter_t
*bitmap_get_counter(struct bitmap
*bitmap
,
1204 sector_t offset
, int *blocks
,
1207 /* If 'create', we might release the lock and reclaim it.
1208 * The lock must have been taken with interrupts enabled.
1209 * If !create, we don't release the lock.
1211 sector_t chunk
= offset
>> CHUNK_BLOCK_SHIFT(bitmap
);
1212 unsigned long page
= chunk
>> PAGE_COUNTER_SHIFT
;
1213 unsigned long pageoff
= (chunk
& PAGE_COUNTER_MASK
) << COUNTER_BYTE_SHIFT
;
1216 if (bitmap_checkpage(bitmap
, page
, create
) < 0) {
1217 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1218 *blocks
= csize
- (offset
& (csize
- 1));
1221 /* now locked ... */
1223 if (bitmap
->bp
[page
].hijacked
) { /* hijacked pointer */
1224 /* should we use the first or second counter field
1225 * of the hijacked pointer? */
1226 int hi
= (pageoff
> PAGE_COUNTER_MASK
);
1227 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
) +
1228 PAGE_COUNTER_SHIFT
- 1);
1229 *blocks
= csize
- (offset
& (csize
- 1));
1230 return &((bitmap_counter_t
*)
1231 &bitmap
->bp
[page
].map
)[hi
];
1232 } else { /* page is allocated */
1233 csize
= ((sector_t
)1) << (CHUNK_BLOCK_SHIFT(bitmap
));
1234 *blocks
= csize
- (offset
& (csize
- 1));
1235 return (bitmap_counter_t
*)
1236 &(bitmap
->bp
[page
].map
[pageoff
]);
1240 int bitmap_startwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
, int behind
)
1242 if (!bitmap
) return 0;
1245 atomic_inc(&bitmap
->behind_writes
);
1246 PRINTK(KERN_DEBUG
"inc write-behind count %d/%d\n",
1247 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1252 bitmap_counter_t
*bmc
;
1254 spin_lock_irq(&bitmap
->lock
);
1255 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 1);
1257 spin_unlock_irq(&bitmap
->lock
);
1261 if (unlikely((*bmc
& COUNTER_MAX
) == COUNTER_MAX
)) {
1262 DEFINE_WAIT(__wait
);
1263 /* note that it is safe to do the prepare_to_wait
1264 * after the test as long as we do it before dropping
1267 prepare_to_wait(&bitmap
->overflow_wait
, &__wait
,
1268 TASK_UNINTERRUPTIBLE
);
1269 spin_unlock_irq(&bitmap
->lock
);
1270 blk_unplug(bitmap
->mddev
->queue
);
1272 finish_wait(&bitmap
->overflow_wait
, &__wait
);
1278 bitmap_file_set_bit(bitmap
, offset
);
1279 bitmap_count_page(bitmap
,offset
, 1);
1280 blk_plug_device_unlocked(bitmap
->mddev
->queue
);
1288 spin_unlock_irq(&bitmap
->lock
);
1291 if (sectors
> blocks
)
1295 bitmap
->allclean
= 0;
1299 void bitmap_endwrite(struct bitmap
*bitmap
, sector_t offset
, unsigned long sectors
,
1300 int success
, int behind
)
1302 if (!bitmap
) return;
1304 atomic_dec(&bitmap
->behind_writes
);
1305 PRINTK(KERN_DEBUG
"dec write-behind count %d/%d\n",
1306 atomic_read(&bitmap
->behind_writes
), bitmap
->max_write_behind
);
1311 unsigned long flags
;
1312 bitmap_counter_t
*bmc
;
1314 spin_lock_irqsave(&bitmap
->lock
, flags
);
1315 bmc
= bitmap_get_counter(bitmap
, offset
, &blocks
, 0);
1317 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1322 bitmap
->events_cleared
< bitmap
->mddev
->events
) {
1323 bitmap
->events_cleared
= bitmap
->mddev
->events
;
1324 bitmap
->need_sync
= 1;
1327 if (!success
&& ! (*bmc
& NEEDED_MASK
))
1328 *bmc
|= NEEDED_MASK
;
1330 if ((*bmc
& COUNTER_MAX
) == COUNTER_MAX
)
1331 wake_up(&bitmap
->overflow_wait
);
1335 set_page_attr(bitmap
,
1336 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1339 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1341 if (sectors
> blocks
)
1347 int bitmap_start_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
,
1350 bitmap_counter_t
*bmc
;
1352 if (bitmap
== NULL
) {/* FIXME or bitmap set as 'failed' */
1354 return 1; /* always resync if no bitmap */
1356 spin_lock_irq(&bitmap
->lock
);
1357 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1363 else if (NEEDED(*bmc
)) {
1365 if (!degraded
) { /* don't set/clear bits if degraded */
1366 *bmc
|= RESYNC_MASK
;
1367 *bmc
&= ~NEEDED_MASK
;
1371 spin_unlock_irq(&bitmap
->lock
);
1372 bitmap
->allclean
= 0;
1376 void bitmap_end_sync(struct bitmap
*bitmap
, sector_t offset
, int *blocks
, int aborted
)
1378 bitmap_counter_t
*bmc
;
1379 unsigned long flags
;
1381 if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
1382 */ if (bitmap
== NULL
) {
1386 spin_lock_irqsave(&bitmap
->lock
, flags
);
1387 bmc
= bitmap_get_counter(bitmap
, offset
, blocks
, 0);
1392 if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
1395 *bmc
&= ~RESYNC_MASK
;
1397 if (!NEEDED(*bmc
) && aborted
)
1398 *bmc
|= NEEDED_MASK
;
1401 set_page_attr(bitmap
,
1402 filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
)),
1408 spin_unlock_irqrestore(&bitmap
->lock
, flags
);
1409 bitmap
->allclean
= 0;
1412 void bitmap_close_sync(struct bitmap
*bitmap
)
1414 /* Sync has finished, and any bitmap chunks that weren't synced
1415 * properly have been aborted. It remains to us to clear the
1416 * RESYNC bit wherever it is still on
1418 sector_t sector
= 0;
1422 while (sector
< bitmap
->mddev
->resync_max_sectors
) {
1423 bitmap_end_sync(bitmap
, sector
, &blocks
, 0);
1428 void bitmap_cond_end_sync(struct bitmap
*bitmap
, sector_t sector
)
1436 bitmap
->last_end_sync
= jiffies
;
1439 if (time_before(jiffies
, (bitmap
->last_end_sync
1440 + bitmap
->daemon_sleep
* HZ
)))
1442 wait_event(bitmap
->mddev
->recovery_wait
,
1443 atomic_read(&bitmap
->mddev
->recovery_active
) == 0);
1445 sector
&= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap
)) - 1);
1447 while (s
< sector
&& s
< bitmap
->mddev
->resync_max_sectors
) {
1448 bitmap_end_sync(bitmap
, s
, &blocks
, 0);
1451 bitmap
->last_end_sync
= jiffies
;
1454 static void bitmap_set_memory_bits(struct bitmap
*bitmap
, sector_t offset
, int needed
)
1456 /* For each chunk covered by any of these sectors, set the
1457 * counter to 1 and set resync_needed. They should all
1458 * be 0 at this point
1462 bitmap_counter_t
*bmc
;
1463 spin_lock_irq(&bitmap
->lock
);
1464 bmc
= bitmap_get_counter(bitmap
, offset
, &secs
, 1);
1466 spin_unlock_irq(&bitmap
->lock
);
1471 *bmc
= 1 | (needed
?NEEDED_MASK
:0);
1472 bitmap_count_page(bitmap
, offset
, 1);
1473 page
= filemap_get_page(bitmap
, offset
>> CHUNK_BLOCK_SHIFT(bitmap
));
1474 set_page_attr(bitmap
, page
, BITMAP_PAGE_CLEAN
);
1476 spin_unlock_irq(&bitmap
->lock
);
1477 bitmap
->allclean
= 0;
1480 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1481 void bitmap_dirty_bits(struct bitmap
*bitmap
, unsigned long s
, unsigned long e
)
1483 unsigned long chunk
;
1485 for (chunk
= s
; chunk
<= e
; chunk
++) {
1486 sector_t sec
= chunk
<< CHUNK_BLOCK_SHIFT(bitmap
);
1487 bitmap_set_memory_bits(bitmap
, sec
, 1);
1488 bitmap_file_set_bit(bitmap
, sec
);
1493 * flush out any pending updates
1495 void bitmap_flush(mddev_t
*mddev
)
1497 struct bitmap
*bitmap
= mddev
->bitmap
;
1500 if (!bitmap
) /* there was no bitmap */
1503 /* run the daemon_work three time to ensure everything is flushed
1506 sleep
= bitmap
->daemon_sleep
;
1507 bitmap
->daemon_sleep
= 0;
1508 bitmap_daemon_work(bitmap
);
1509 bitmap_daemon_work(bitmap
);
1510 bitmap_daemon_work(bitmap
);
1511 bitmap
->daemon_sleep
= sleep
;
1512 bitmap_update_sb(bitmap
);
1516 * free memory that was allocated
1518 static void bitmap_free(struct bitmap
*bitmap
)
1520 unsigned long k
, pages
;
1521 struct bitmap_page
*bp
;
1523 if (!bitmap
) /* there was no bitmap */
1526 /* release the bitmap file and kill the daemon */
1527 bitmap_file_put(bitmap
);
1530 pages
= bitmap
->pages
;
1532 /* free all allocated memory */
1534 if (bp
) /* deallocate the page memory */
1535 for (k
= 0; k
< pages
; k
++)
1536 if (bp
[k
].map
&& !bp
[k
].hijacked
)
1541 void bitmap_destroy(mddev_t
*mddev
)
1543 struct bitmap
*bitmap
= mddev
->bitmap
;
1545 if (!bitmap
) /* there was no bitmap */
1548 mddev
->bitmap
= NULL
; /* disconnect from the md device */
1550 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
1552 bitmap_free(bitmap
);
1556 * initialize the bitmap structure
1557 * if this returns an error, bitmap_destroy must be called to do clean up
1559 int bitmap_create(mddev_t
*mddev
)
1561 struct bitmap
*bitmap
;
1562 unsigned long blocks
= mddev
->resync_max_sectors
;
1563 unsigned long chunks
;
1564 unsigned long pages
;
1565 struct file
*file
= mddev
->bitmap_file
;
1569 BUILD_BUG_ON(sizeof(bitmap_super_t
) != 256);
1571 if (!file
&& !mddev
->bitmap_offset
) /* bitmap disabled, nothing to do */
1574 BUG_ON(file
&& mddev
->bitmap_offset
);
1576 bitmap
= kzalloc(sizeof(*bitmap
), GFP_KERNEL
);
1580 spin_lock_init(&bitmap
->lock
);
1581 atomic_set(&bitmap
->pending_writes
, 0);
1582 init_waitqueue_head(&bitmap
->write_wait
);
1583 init_waitqueue_head(&bitmap
->overflow_wait
);
1585 bitmap
->mddev
= mddev
;
1587 bitmap
->file
= file
;
1588 bitmap
->offset
= mddev
->bitmap_offset
;
1591 do_sync_mapping_range(file
->f_mapping
, 0, LLONG_MAX
,
1592 SYNC_FILE_RANGE_WAIT_BEFORE
|
1593 SYNC_FILE_RANGE_WRITE
|
1594 SYNC_FILE_RANGE_WAIT_AFTER
);
1596 /* read superblock from bitmap file (this sets bitmap->chunksize) */
1597 err
= bitmap_read_sb(bitmap
);
1601 bitmap
->chunkshift
= ffz(~bitmap
->chunksize
);
1603 /* now that chunksize and chunkshift are set, we can use these macros */
1604 chunks
= (blocks
+ CHUNK_BLOCK_RATIO(bitmap
) - 1) /
1605 CHUNK_BLOCK_RATIO(bitmap
);
1606 pages
= (chunks
+ PAGE_COUNTER_RATIO
- 1) / PAGE_COUNTER_RATIO
;
1610 bitmap
->chunks
= chunks
;
1611 bitmap
->pages
= pages
;
1612 bitmap
->missing_pages
= pages
;
1613 bitmap
->counter_bits
= COUNTER_BITS
;
1615 bitmap
->syncchunk
= ~0UL;
1617 #ifdef INJECT_FATAL_FAULT_1
1620 bitmap
->bp
= kzalloc(pages
* sizeof(*bitmap
->bp
), GFP_KERNEL
);
1626 /* now that we have some pages available, initialize the in-memory
1627 * bitmap from the on-disk bitmap */
1629 if (mddev
->degraded
== 0
1630 || bitmap
->events_cleared
== mddev
->events
)
1631 /* no need to keep dirty bits to optimise a re-add of a missing device */
1632 start
= mddev
->recovery_cp
;
1633 err
= bitmap_init_from_disk(bitmap
, start
);
1638 printk(KERN_INFO
"created bitmap (%lu pages) for device %s\n",
1639 pages
, bmname(bitmap
));
1641 mddev
->bitmap
= bitmap
;
1643 mddev
->thread
->timeout
= bitmap
->daemon_sleep
* HZ
;
1645 bitmap_update_sb(bitmap
);
1647 return (bitmap
->flags
& BITMAP_WRITE_ERROR
) ? -EIO
: 0;
1650 bitmap_free(bitmap
);
1654 /* the bitmap API -- for raid personalities */
1655 EXPORT_SYMBOL(bitmap_startwrite
);
1656 EXPORT_SYMBOL(bitmap_endwrite
);
1657 EXPORT_SYMBOL(bitmap_start_sync
);
1658 EXPORT_SYMBOL(bitmap_end_sync
);
1659 EXPORT_SYMBOL(bitmap_unplug
);
1660 EXPORT_SYMBOL(bitmap_close_sync
);
1661 EXPORT_SYMBOL(bitmap_cond_end_sync
);