dm writecache: correct uncommitted_block when discarding uncommitted entry
[linux/fpc-iii.git] / drivers / md / md-bitmap.c
blobb952bd45bd6a321861278842b203e3eb4e22dcc9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
5 * bitmap_create - sets up the bitmap structure
6 * bitmap_destroy - destroys the bitmap structure
8 * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
9 * - added disk storage for bitmap
10 * - changes to allow various bitmap chunk sizes
14 * Still to do:
16 * flush after percent set rather than just time based. (maybe both).
19 #include <linux/blkdev.h>
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/timer.h>
25 #include <linux/sched.h>
26 #include <linux/list.h>
27 #include <linux/file.h>
28 #include <linux/mount.h>
29 #include <linux/buffer_head.h>
30 #include <linux/seq_file.h>
31 #include <trace/events/block.h>
32 #include "md.h"
33 #include "md-bitmap.h"
35 static inline char *bmname(struct bitmap *bitmap)
37 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
41 * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
43 * 1) check to see if this page is allocated, if it's not then try to alloc
44 * 2) if the alloc fails, set the page's hijacked flag so we'll use the
45 * page pointer directly as a counter
47 * if we find our page, we increment the page's refcount so that it stays
48 * allocated while we're using it
50 static int md_bitmap_checkpage(struct bitmap_counts *bitmap,
51 unsigned long page, int create, int no_hijack)
52 __releases(bitmap->lock)
53 __acquires(bitmap->lock)
55 unsigned char *mappage;
57 if (page >= bitmap->pages) {
58 /* This can happen if bitmap_start_sync goes beyond
59 * End-of-device while looking for a whole page.
60 * It is harmless.
62 return -EINVAL;
65 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
66 return 0;
68 if (bitmap->bp[page].map) /* page is already allocated, just return */
69 return 0;
71 if (!create)
72 return -ENOENT;
74 /* this page has not been allocated yet */
76 spin_unlock_irq(&bitmap->lock);
77 /* It is possible that this is being called inside a
78 * prepare_to_wait/finish_wait loop from raid5c:make_request().
79 * In general it is not permitted to sleep in that context as it
80 * can cause the loop to spin freely.
81 * That doesn't apply here as we can only reach this point
82 * once with any loop.
83 * When this function completes, either bp[page].map or
84 * bp[page].hijacked. In either case, this function will
85 * abort before getting to this point again. So there is
86 * no risk of a free-spin, and so it is safe to assert
87 * that sleeping here is allowed.
89 sched_annotate_sleep();
90 mappage = kzalloc(PAGE_SIZE, GFP_NOIO);
91 spin_lock_irq(&bitmap->lock);
93 if (mappage == NULL) {
94 pr_debug("md/bitmap: map page allocation failed, hijacking\n");
95 /* We don't support hijack for cluster raid */
96 if (no_hijack)
97 return -ENOMEM;
98 /* failed - set the hijacked flag so that we can use the
99 * pointer as a counter */
100 if (!bitmap->bp[page].map)
101 bitmap->bp[page].hijacked = 1;
102 } else if (bitmap->bp[page].map ||
103 bitmap->bp[page].hijacked) {
104 /* somebody beat us to getting the page */
105 kfree(mappage);
106 } else {
108 /* no page was in place and we have one, so install it */
110 bitmap->bp[page].map = mappage;
111 bitmap->missing_pages--;
113 return 0;
116 /* if page is completely empty, put it back on the free list, or dealloc it */
117 /* if page was hijacked, unmark the flag so it might get alloced next time */
118 /* Note: lock should be held when calling this */
119 static void md_bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page)
121 char *ptr;
123 if (bitmap->bp[page].count) /* page is still busy */
124 return;
126 /* page is no longer in use, it can be released */
128 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
129 bitmap->bp[page].hijacked = 0;
130 bitmap->bp[page].map = NULL;
131 } else {
132 /* normal case, free the page */
133 ptr = bitmap->bp[page].map;
134 bitmap->bp[page].map = NULL;
135 bitmap->missing_pages++;
136 kfree(ptr);
141 * bitmap file handling - read and write the bitmap file and its superblock
145 * basic page I/O operations
148 /* IO operations when bitmap is stored near all superblocks */
149 static int read_sb_page(struct mddev *mddev, loff_t offset,
150 struct page *page,
151 unsigned long index, int size)
153 /* choose a good rdev and read the page from there */
155 struct md_rdev *rdev;
156 sector_t target;
158 rdev_for_each(rdev, mddev) {
159 if (! test_bit(In_sync, &rdev->flags)
160 || test_bit(Faulty, &rdev->flags)
161 || test_bit(Bitmap_sync, &rdev->flags))
162 continue;
164 target = offset + index * (PAGE_SIZE/512);
166 if (sync_page_io(rdev, target,
167 roundup(size, bdev_logical_block_size(rdev->bdev)),
168 page, REQ_OP_READ, 0, true)) {
169 page->index = index;
170 return 0;
173 return -EIO;
176 static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
178 /* Iterate the disks of an mddev, using rcu to protect access to the
179 * linked list, and raising the refcount of devices we return to ensure
180 * they don't disappear while in use.
181 * As devices are only added or removed when raid_disk is < 0 and
182 * nr_pending is 0 and In_sync is clear, the entries we return will
183 * still be in the same position on the list when we re-enter
184 * list_for_each_entry_continue_rcu.
186 * Note that if entered with 'rdev == NULL' to start at the
187 * beginning, we temporarily assign 'rdev' to an address which
188 * isn't really an rdev, but which can be used by
189 * list_for_each_entry_continue_rcu() to find the first entry.
191 rcu_read_lock();
192 if (rdev == NULL)
193 /* start at the beginning */
194 rdev = list_entry(&mddev->disks, struct md_rdev, same_set);
195 else {
196 /* release the previous rdev and start from there. */
197 rdev_dec_pending(rdev, mddev);
199 list_for_each_entry_continue_rcu(rdev, &mddev->disks, same_set) {
200 if (rdev->raid_disk >= 0 &&
201 !test_bit(Faulty, &rdev->flags)) {
202 /* this is a usable devices */
203 atomic_inc(&rdev->nr_pending);
204 rcu_read_unlock();
205 return rdev;
208 rcu_read_unlock();
209 return NULL;
212 static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
214 struct md_rdev *rdev;
215 struct block_device *bdev;
216 struct mddev *mddev = bitmap->mddev;
217 struct bitmap_storage *store = &bitmap->storage;
219 restart:
220 rdev = NULL;
221 while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
222 int size = PAGE_SIZE;
223 loff_t offset = mddev->bitmap_info.offset;
225 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
227 if (page->index == store->file_pages-1) {
228 int last_page_size = store->bytes & (PAGE_SIZE-1);
229 if (last_page_size == 0)
230 last_page_size = PAGE_SIZE;
231 size = roundup(last_page_size,
232 bdev_logical_block_size(bdev));
234 /* Just make sure we aren't corrupting data or
235 * metadata
237 if (mddev->external) {
238 /* Bitmap could be anywhere. */
239 if (rdev->sb_start + offset + (page->index
240 * (PAGE_SIZE/512))
241 > rdev->data_offset
243 rdev->sb_start + offset
244 < (rdev->data_offset + mddev->dev_sectors
245 + (PAGE_SIZE/512)))
246 goto bad_alignment;
247 } else if (offset < 0) {
248 /* DATA BITMAP METADATA */
249 if (offset
250 + (long)(page->index * (PAGE_SIZE/512))
251 + size/512 > 0)
252 /* bitmap runs in to metadata */
253 goto bad_alignment;
254 if (rdev->data_offset + mddev->dev_sectors
255 > rdev->sb_start + offset)
256 /* data runs in to bitmap */
257 goto bad_alignment;
258 } else if (rdev->sb_start < rdev->data_offset) {
259 /* METADATA BITMAP DATA */
260 if (rdev->sb_start
261 + offset
262 + page->index*(PAGE_SIZE/512) + size/512
263 > rdev->data_offset)
264 /* bitmap runs in to data */
265 goto bad_alignment;
266 } else {
267 /* DATA METADATA BITMAP - no problems */
269 md_super_write(mddev, rdev,
270 rdev->sb_start + offset
271 + page->index * (PAGE_SIZE/512),
272 size,
273 page);
276 if (wait && md_super_wait(mddev) < 0)
277 goto restart;
278 return 0;
280 bad_alignment:
281 return -EINVAL;
284 static void md_bitmap_file_kick(struct bitmap *bitmap);
286 * write out a page to a file
288 static void write_page(struct bitmap *bitmap, struct page *page, int wait)
290 struct buffer_head *bh;
292 if (bitmap->storage.file == NULL) {
293 switch (write_sb_page(bitmap, page, wait)) {
294 case -EINVAL:
295 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
297 } else {
299 bh = page_buffers(page);
301 while (bh && bh->b_blocknr) {
302 atomic_inc(&bitmap->pending_writes);
303 set_buffer_locked(bh);
304 set_buffer_mapped(bh);
305 submit_bh(REQ_OP_WRITE, REQ_SYNC, bh);
306 bh = bh->b_this_page;
309 if (wait)
310 wait_event(bitmap->write_wait,
311 atomic_read(&bitmap->pending_writes)==0);
313 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
314 md_bitmap_file_kick(bitmap);
317 static void end_bitmap_write(struct buffer_head *bh, int uptodate)
319 struct bitmap *bitmap = bh->b_private;
321 if (!uptodate)
322 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
323 if (atomic_dec_and_test(&bitmap->pending_writes))
324 wake_up(&bitmap->write_wait);
327 /* copied from buffer.c */
328 static void
329 __clear_page_buffers(struct page *page)
331 ClearPagePrivate(page);
332 set_page_private(page, 0);
333 put_page(page);
335 static void free_buffers(struct page *page)
337 struct buffer_head *bh;
339 if (!PagePrivate(page))
340 return;
342 bh = page_buffers(page);
343 while (bh) {
344 struct buffer_head *next = bh->b_this_page;
345 free_buffer_head(bh);
346 bh = next;
348 __clear_page_buffers(page);
349 put_page(page);
352 /* read a page from a file.
353 * We both read the page, and attach buffers to the page to record the
354 * address of each block (using bmap). These addresses will be used
355 * to write the block later, completely bypassing the filesystem.
356 * This usage is similar to how swap files are handled, and allows us
357 * to write to a file with no concerns of memory allocation failing.
359 static int read_page(struct file *file, unsigned long index,
360 struct bitmap *bitmap,
361 unsigned long count,
362 struct page *page)
364 int ret = 0;
365 struct inode *inode = file_inode(file);
366 struct buffer_head *bh;
367 sector_t block, blk_cur;
369 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
370 (unsigned long long)index << PAGE_SHIFT);
372 bh = alloc_page_buffers(page, 1<<inode->i_blkbits, false);
373 if (!bh) {
374 ret = -ENOMEM;
375 goto out;
377 attach_page_buffers(page, bh);
378 blk_cur = index << (PAGE_SHIFT - inode->i_blkbits);
379 while (bh) {
380 block = blk_cur;
382 if (count == 0)
383 bh->b_blocknr = 0;
384 else {
385 ret = bmap(inode, &block);
386 if (ret || !block) {
387 ret = -EINVAL;
388 bh->b_blocknr = 0;
389 goto out;
392 bh->b_blocknr = block;
393 bh->b_bdev = inode->i_sb->s_bdev;
394 if (count < (1<<inode->i_blkbits))
395 count = 0;
396 else
397 count -= (1<<inode->i_blkbits);
399 bh->b_end_io = end_bitmap_write;
400 bh->b_private = bitmap;
401 atomic_inc(&bitmap->pending_writes);
402 set_buffer_locked(bh);
403 set_buffer_mapped(bh);
404 submit_bh(REQ_OP_READ, 0, bh);
406 blk_cur++;
407 bh = bh->b_this_page;
409 page->index = index;
411 wait_event(bitmap->write_wait,
412 atomic_read(&bitmap->pending_writes)==0);
413 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
414 ret = -EIO;
415 out:
416 if (ret)
417 pr_err("md: bitmap read error: (%dB @ %llu): %d\n",
418 (int)PAGE_SIZE,
419 (unsigned long long)index << PAGE_SHIFT,
420 ret);
421 return ret;
425 * bitmap file superblock operations
429 * md_bitmap_wait_writes() should be called before writing any bitmap
430 * blocks, to ensure previous writes, particularly from
431 * md_bitmap_daemon_work(), have completed.
433 static void md_bitmap_wait_writes(struct bitmap *bitmap)
435 if (bitmap->storage.file)
436 wait_event(bitmap->write_wait,
437 atomic_read(&bitmap->pending_writes)==0);
438 else
439 /* Note that we ignore the return value. The writes
440 * might have failed, but that would just mean that
441 * some bits which should be cleared haven't been,
442 * which is safe. The relevant bitmap blocks will
443 * probably get written again, but there is no great
444 * loss if they aren't.
446 md_super_wait(bitmap->mddev);
450 /* update the event counter and sync the superblock to disk */
451 void md_bitmap_update_sb(struct bitmap *bitmap)
453 bitmap_super_t *sb;
455 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
456 return;
457 if (bitmap->mddev->bitmap_info.external)
458 return;
459 if (!bitmap->storage.sb_page) /* no superblock */
460 return;
461 sb = kmap_atomic(bitmap->storage.sb_page);
462 sb->events = cpu_to_le64(bitmap->mddev->events);
463 if (bitmap->mddev->events < bitmap->events_cleared)
464 /* rocking back to read-only */
465 bitmap->events_cleared = bitmap->mddev->events;
466 sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
468 * clear BITMAP_WRITE_ERROR bit to protect against the case that
469 * a bitmap write error occurred but the later writes succeeded.
471 sb->state = cpu_to_le32(bitmap->flags & ~BIT(BITMAP_WRITE_ERROR));
472 /* Just in case these have been changed via sysfs: */
473 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
474 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
475 /* This might have been changed by a reshape */
476 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
477 sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize);
478 sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes);
479 sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
480 bitmap_info.space);
481 kunmap_atomic(sb);
482 write_page(bitmap, bitmap->storage.sb_page, 1);
484 EXPORT_SYMBOL(md_bitmap_update_sb);
486 /* print out the bitmap file superblock */
487 void md_bitmap_print_sb(struct bitmap *bitmap)
489 bitmap_super_t *sb;
491 if (!bitmap || !bitmap->storage.sb_page)
492 return;
493 sb = kmap_atomic(bitmap->storage.sb_page);
494 pr_debug("%s: bitmap file superblock:\n", bmname(bitmap));
495 pr_debug(" magic: %08x\n", le32_to_cpu(sb->magic));
496 pr_debug(" version: %d\n", le32_to_cpu(sb->version));
497 pr_debug(" uuid: %08x.%08x.%08x.%08x\n",
498 le32_to_cpu(*(__le32 *)(sb->uuid+0)),
499 le32_to_cpu(*(__le32 *)(sb->uuid+4)),
500 le32_to_cpu(*(__le32 *)(sb->uuid+8)),
501 le32_to_cpu(*(__le32 *)(sb->uuid+12)));
502 pr_debug(" events: %llu\n",
503 (unsigned long long) le64_to_cpu(sb->events));
504 pr_debug("events cleared: %llu\n",
505 (unsigned long long) le64_to_cpu(sb->events_cleared));
506 pr_debug(" state: %08x\n", le32_to_cpu(sb->state));
507 pr_debug(" chunksize: %d B\n", le32_to_cpu(sb->chunksize));
508 pr_debug(" daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
509 pr_debug(" sync size: %llu KB\n",
510 (unsigned long long)le64_to_cpu(sb->sync_size)/2);
511 pr_debug("max write behind: %d\n", le32_to_cpu(sb->write_behind));
512 kunmap_atomic(sb);
516 * bitmap_new_disk_sb
517 * @bitmap
519 * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
520 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
521 * This function verifies 'bitmap_info' and populates the on-disk bitmap
522 * structure, which is to be written to disk.
524 * Returns: 0 on success, -Exxx on error
526 static int md_bitmap_new_disk_sb(struct bitmap *bitmap)
528 bitmap_super_t *sb;
529 unsigned long chunksize, daemon_sleep, write_behind;
531 bitmap->storage.sb_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
532 if (bitmap->storage.sb_page == NULL)
533 return -ENOMEM;
534 bitmap->storage.sb_page->index = 0;
536 sb = kmap_atomic(bitmap->storage.sb_page);
538 sb->magic = cpu_to_le32(BITMAP_MAGIC);
539 sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
541 chunksize = bitmap->mddev->bitmap_info.chunksize;
542 BUG_ON(!chunksize);
543 if (!is_power_of_2(chunksize)) {
544 kunmap_atomic(sb);
545 pr_warn("bitmap chunksize not a power of 2\n");
546 return -EINVAL;
548 sb->chunksize = cpu_to_le32(chunksize);
550 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
551 if (!daemon_sleep || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
552 pr_debug("Choosing daemon_sleep default (5 sec)\n");
553 daemon_sleep = 5 * HZ;
555 sb->daemon_sleep = cpu_to_le32(daemon_sleep);
556 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
559 * FIXME: write_behind for RAID1. If not specified, what
560 * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
562 write_behind = bitmap->mddev->bitmap_info.max_write_behind;
563 if (write_behind > COUNTER_MAX)
564 write_behind = COUNTER_MAX / 2;
565 sb->write_behind = cpu_to_le32(write_behind);
566 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
568 /* keep the array size field of the bitmap superblock up to date */
569 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
571 memcpy(sb->uuid, bitmap->mddev->uuid, 16);
573 set_bit(BITMAP_STALE, &bitmap->flags);
574 sb->state = cpu_to_le32(bitmap->flags);
575 bitmap->events_cleared = bitmap->mddev->events;
576 sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
577 bitmap->mddev->bitmap_info.nodes = 0;
579 kunmap_atomic(sb);
581 return 0;
584 /* read the superblock from the bitmap file and initialize some bitmap fields */
585 static int md_bitmap_read_sb(struct bitmap *bitmap)
587 char *reason = NULL;
588 bitmap_super_t *sb;
589 unsigned long chunksize, daemon_sleep, write_behind;
590 unsigned long long events;
591 int nodes = 0;
592 unsigned long sectors_reserved = 0;
593 int err = -EINVAL;
594 struct page *sb_page;
595 loff_t offset = bitmap->mddev->bitmap_info.offset;
597 if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) {
598 chunksize = 128 * 1024 * 1024;
599 daemon_sleep = 5 * HZ;
600 write_behind = 0;
601 set_bit(BITMAP_STALE, &bitmap->flags);
602 err = 0;
603 goto out_no_sb;
605 /* page 0 is the superblock, read it... */
606 sb_page = alloc_page(GFP_KERNEL);
607 if (!sb_page)
608 return -ENOMEM;
609 bitmap->storage.sb_page = sb_page;
611 re_read:
612 /* If cluster_slot is set, the cluster is setup */
613 if (bitmap->cluster_slot >= 0) {
614 sector_t bm_blocks = bitmap->mddev->resync_max_sectors;
616 sector_div(bm_blocks,
617 bitmap->mddev->bitmap_info.chunksize >> 9);
618 /* bits to bytes */
619 bm_blocks = ((bm_blocks+7) >> 3) + sizeof(bitmap_super_t);
620 /* to 4k blocks */
621 bm_blocks = DIV_ROUND_UP_SECTOR_T(bm_blocks, 4096);
622 offset = bitmap->mddev->bitmap_info.offset + (bitmap->cluster_slot * (bm_blocks << 3));
623 pr_debug("%s:%d bm slot: %d offset: %llu\n", __func__, __LINE__,
624 bitmap->cluster_slot, offset);
627 if (bitmap->storage.file) {
628 loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host);
629 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
631 err = read_page(bitmap->storage.file, 0,
632 bitmap, bytes, sb_page);
633 } else {
634 err = read_sb_page(bitmap->mddev,
635 offset,
636 sb_page,
637 0, sizeof(bitmap_super_t));
639 if (err)
640 return err;
642 err = -EINVAL;
643 sb = kmap_atomic(sb_page);
645 chunksize = le32_to_cpu(sb->chunksize);
646 daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
647 write_behind = le32_to_cpu(sb->write_behind);
648 sectors_reserved = le32_to_cpu(sb->sectors_reserved);
649 /* Setup nodes/clustername only if bitmap version is
650 * cluster-compatible
652 if (sb->version == cpu_to_le32(BITMAP_MAJOR_CLUSTERED)) {
653 nodes = le32_to_cpu(sb->nodes);
654 strlcpy(bitmap->mddev->bitmap_info.cluster_name,
655 sb->cluster_name, 64);
658 /* verify that the bitmap-specific fields are valid */
659 if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
660 reason = "bad magic";
661 else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
662 le32_to_cpu(sb->version) > BITMAP_MAJOR_CLUSTERED)
663 reason = "unrecognized superblock version";
664 else if (chunksize < 512)
665 reason = "bitmap chunksize too small";
666 else if (!is_power_of_2(chunksize))
667 reason = "bitmap chunksize not a power of 2";
668 else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
669 reason = "daemon sleep period out of range";
670 else if (write_behind > COUNTER_MAX)
671 reason = "write-behind limit out of range (0 - 16383)";
672 if (reason) {
673 pr_warn("%s: invalid bitmap file superblock: %s\n",
674 bmname(bitmap), reason);
675 goto out;
678 /* keep the array size field of the bitmap superblock up to date */
679 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
681 if (bitmap->mddev->persistent) {
683 * We have a persistent array superblock, so compare the
684 * bitmap's UUID and event counter to the mddev's
686 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
687 pr_warn("%s: bitmap superblock UUID mismatch\n",
688 bmname(bitmap));
689 goto out;
691 events = le64_to_cpu(sb->events);
692 if (!nodes && (events < bitmap->mddev->events)) {
693 pr_warn("%s: bitmap file is out of date (%llu < %llu) -- forcing full recovery\n",
694 bmname(bitmap), events,
695 (unsigned long long) bitmap->mddev->events);
696 set_bit(BITMAP_STALE, &bitmap->flags);
700 /* assign fields using values from superblock */
701 bitmap->flags |= le32_to_cpu(sb->state);
702 if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
703 set_bit(BITMAP_HOSTENDIAN, &bitmap->flags);
704 bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
705 strlcpy(bitmap->mddev->bitmap_info.cluster_name, sb->cluster_name, 64);
706 err = 0;
708 out:
709 kunmap_atomic(sb);
710 /* Assigning chunksize is required for "re_read" */
711 bitmap->mddev->bitmap_info.chunksize = chunksize;
712 if (err == 0 && nodes && (bitmap->cluster_slot < 0)) {
713 err = md_setup_cluster(bitmap->mddev, nodes);
714 if (err) {
715 pr_warn("%s: Could not setup cluster service (%d)\n",
716 bmname(bitmap), err);
717 goto out_no_sb;
719 bitmap->cluster_slot = md_cluster_ops->slot_number(bitmap->mddev);
720 goto re_read;
724 out_no_sb:
725 if (test_bit(BITMAP_STALE, &bitmap->flags))
726 bitmap->events_cleared = bitmap->mddev->events;
727 bitmap->mddev->bitmap_info.chunksize = chunksize;
728 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
729 bitmap->mddev->bitmap_info.max_write_behind = write_behind;
730 bitmap->mddev->bitmap_info.nodes = nodes;
731 if (bitmap->mddev->bitmap_info.space == 0 ||
732 bitmap->mddev->bitmap_info.space > sectors_reserved)
733 bitmap->mddev->bitmap_info.space = sectors_reserved;
734 if (err) {
735 md_bitmap_print_sb(bitmap);
736 if (bitmap->cluster_slot < 0)
737 md_cluster_stop(bitmap->mddev);
739 return err;
743 * general bitmap file operations
747 * on-disk bitmap:
749 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
750 * file a page at a time. There's a superblock at the start of the file.
752 /* calculate the index of the page that contains this bit */
753 static inline unsigned long file_page_index(struct bitmap_storage *store,
754 unsigned long chunk)
756 if (store->sb_page)
757 chunk += sizeof(bitmap_super_t) << 3;
758 return chunk >> PAGE_BIT_SHIFT;
761 /* calculate the (bit) offset of this bit within a page */
762 static inline unsigned long file_page_offset(struct bitmap_storage *store,
763 unsigned long chunk)
765 if (store->sb_page)
766 chunk += sizeof(bitmap_super_t) << 3;
767 return chunk & (PAGE_BITS - 1);
771 * return a pointer to the page in the filemap that contains the given bit
774 static inline struct page *filemap_get_page(struct bitmap_storage *store,
775 unsigned long chunk)
777 if (file_page_index(store, chunk) >= store->file_pages)
778 return NULL;
779 return store->filemap[file_page_index(store, chunk)];
782 static int md_bitmap_storage_alloc(struct bitmap_storage *store,
783 unsigned long chunks, int with_super,
784 int slot_number)
786 int pnum, offset = 0;
787 unsigned long num_pages;
788 unsigned long bytes;
790 bytes = DIV_ROUND_UP(chunks, 8);
791 if (with_super)
792 bytes += sizeof(bitmap_super_t);
794 num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
795 offset = slot_number * num_pages;
797 store->filemap = kmalloc_array(num_pages, sizeof(struct page *),
798 GFP_KERNEL);
799 if (!store->filemap)
800 return -ENOMEM;
802 if (with_super && !store->sb_page) {
803 store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
804 if (store->sb_page == NULL)
805 return -ENOMEM;
808 pnum = 0;
809 if (store->sb_page) {
810 store->filemap[0] = store->sb_page;
811 pnum = 1;
812 store->sb_page->index = offset;
815 for ( ; pnum < num_pages; pnum++) {
816 store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
817 if (!store->filemap[pnum]) {
818 store->file_pages = pnum;
819 return -ENOMEM;
821 store->filemap[pnum]->index = pnum + offset;
823 store->file_pages = pnum;
825 /* We need 4 bits per page, rounded up to a multiple
826 * of sizeof(unsigned long) */
827 store->filemap_attr = kzalloc(
828 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
829 GFP_KERNEL);
830 if (!store->filemap_attr)
831 return -ENOMEM;
833 store->bytes = bytes;
835 return 0;
838 static void md_bitmap_file_unmap(struct bitmap_storage *store)
840 struct page **map, *sb_page;
841 int pages;
842 struct file *file;
844 file = store->file;
845 map = store->filemap;
846 pages = store->file_pages;
847 sb_page = store->sb_page;
849 while (pages--)
850 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
851 free_buffers(map[pages]);
852 kfree(map);
853 kfree(store->filemap_attr);
855 if (sb_page)
856 free_buffers(sb_page);
858 if (file) {
859 struct inode *inode = file_inode(file);
860 invalidate_mapping_pages(inode->i_mapping, 0, -1);
861 fput(file);
866 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
867 * then it is no longer reliable, so we stop using it and we mark the file
868 * as failed in the superblock
870 static void md_bitmap_file_kick(struct bitmap *bitmap)
872 char *path, *ptr = NULL;
874 if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) {
875 md_bitmap_update_sb(bitmap);
877 if (bitmap->storage.file) {
878 path = kmalloc(PAGE_SIZE, GFP_KERNEL);
879 if (path)
880 ptr = file_path(bitmap->storage.file,
881 path, PAGE_SIZE);
883 pr_warn("%s: kicking failed bitmap file %s from array!\n",
884 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
886 kfree(path);
887 } else
888 pr_warn("%s: disabling internal bitmap due to errors\n",
889 bmname(bitmap));
893 enum bitmap_page_attr {
894 BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
895 BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
896 * i.e. counter is 1 or 2. */
897 BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
900 static inline void set_page_attr(struct bitmap *bitmap, int pnum,
901 enum bitmap_page_attr attr)
903 set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
906 static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
907 enum bitmap_page_attr attr)
909 clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
912 static inline int test_page_attr(struct bitmap *bitmap, int pnum,
913 enum bitmap_page_attr attr)
915 return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
918 static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
919 enum bitmap_page_attr attr)
921 return test_and_clear_bit((pnum<<2) + attr,
922 bitmap->storage.filemap_attr);
925 * bitmap_file_set_bit -- called before performing a write to the md device
926 * to set (and eventually sync) a particular bit in the bitmap file
928 * we set the bit immediately, then we record the page number so that
929 * when an unplug occurs, we can flush the dirty pages out to disk
931 static void md_bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
933 unsigned long bit;
934 struct page *page;
935 void *kaddr;
936 unsigned long chunk = block >> bitmap->counts.chunkshift;
937 struct bitmap_storage *store = &bitmap->storage;
938 unsigned long node_offset = 0;
940 if (mddev_is_clustered(bitmap->mddev))
941 node_offset = bitmap->cluster_slot * store->file_pages;
943 page = filemap_get_page(&bitmap->storage, chunk);
944 if (!page)
945 return;
946 bit = file_page_offset(&bitmap->storage, chunk);
948 /* set the bit */
949 kaddr = kmap_atomic(page);
950 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
951 set_bit(bit, kaddr);
952 else
953 set_bit_le(bit, kaddr);
954 kunmap_atomic(kaddr);
955 pr_debug("set file bit %lu page %lu\n", bit, page->index);
956 /* record page number so it gets flushed to disk when unplug occurs */
957 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_DIRTY);
960 static void md_bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
962 unsigned long bit;
963 struct page *page;
964 void *paddr;
965 unsigned long chunk = block >> bitmap->counts.chunkshift;
966 struct bitmap_storage *store = &bitmap->storage;
967 unsigned long node_offset = 0;
969 if (mddev_is_clustered(bitmap->mddev))
970 node_offset = bitmap->cluster_slot * store->file_pages;
972 page = filemap_get_page(&bitmap->storage, chunk);
973 if (!page)
974 return;
975 bit = file_page_offset(&bitmap->storage, chunk);
976 paddr = kmap_atomic(page);
977 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
978 clear_bit(bit, paddr);
979 else
980 clear_bit_le(bit, paddr);
981 kunmap_atomic(paddr);
982 if (!test_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_NEEDWRITE)) {
983 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_PENDING);
984 bitmap->allclean = 0;
988 static int md_bitmap_file_test_bit(struct bitmap *bitmap, sector_t block)
990 unsigned long bit;
991 struct page *page;
992 void *paddr;
993 unsigned long chunk = block >> bitmap->counts.chunkshift;
994 int set = 0;
996 page = filemap_get_page(&bitmap->storage, chunk);
997 if (!page)
998 return -EINVAL;
999 bit = file_page_offset(&bitmap->storage, chunk);
1000 paddr = kmap_atomic(page);
1001 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
1002 set = test_bit(bit, paddr);
1003 else
1004 set = test_bit_le(bit, paddr);
1005 kunmap_atomic(paddr);
1006 return set;
1010 /* this gets called when the md device is ready to unplug its underlying
1011 * (slave) device queues -- before we let any writes go down, we need to
1012 * sync the dirty pages of the bitmap file to disk */
1013 void md_bitmap_unplug(struct bitmap *bitmap)
1015 unsigned long i;
1016 int dirty, need_write;
1017 int writing = 0;
1019 if (!bitmap || !bitmap->storage.filemap ||
1020 test_bit(BITMAP_STALE, &bitmap->flags))
1021 return;
1023 /* look at each page to see if there are any set bits that need to be
1024 * flushed out to disk */
1025 for (i = 0; i < bitmap->storage.file_pages; i++) {
1026 dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
1027 need_write = test_and_clear_page_attr(bitmap, i,
1028 BITMAP_PAGE_NEEDWRITE);
1029 if (dirty || need_write) {
1030 if (!writing) {
1031 md_bitmap_wait_writes(bitmap);
1032 if (bitmap->mddev->queue)
1033 blk_add_trace_msg(bitmap->mddev->queue,
1034 "md bitmap_unplug");
1036 clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
1037 write_page(bitmap, bitmap->storage.filemap[i], 0);
1038 writing = 1;
1041 if (writing)
1042 md_bitmap_wait_writes(bitmap);
1044 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
1045 md_bitmap_file_kick(bitmap);
1047 EXPORT_SYMBOL(md_bitmap_unplug);
1049 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
1050 /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
1051 * the in-memory bitmap from the on-disk bitmap -- also, sets up the
1052 * memory mapping of the bitmap file
1053 * Special cases:
1054 * if there's no bitmap file, or if the bitmap file had been
1055 * previously kicked from the array, we mark all the bits as
1056 * 1's in order to cause a full resync.
1058 * We ignore all bits for sectors that end earlier than 'start'.
1059 * This is used when reading an out-of-date bitmap...
1061 static int md_bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
1063 unsigned long i, chunks, index, oldindex, bit, node_offset = 0;
1064 struct page *page = NULL;
1065 unsigned long bit_cnt = 0;
1066 struct file *file;
1067 unsigned long offset;
1068 int outofdate;
1069 int ret = -ENOSPC;
1070 void *paddr;
1071 struct bitmap_storage *store = &bitmap->storage;
1073 chunks = bitmap->counts.chunks;
1074 file = store->file;
1076 if (!file && !bitmap->mddev->bitmap_info.offset) {
1077 /* No permanent bitmap - fill with '1s'. */
1078 store->filemap = NULL;
1079 store->file_pages = 0;
1080 for (i = 0; i < chunks ; i++) {
1081 /* if the disk bit is set, set the memory bit */
1082 int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift)
1083 >= start);
1084 md_bitmap_set_memory_bits(bitmap,
1085 (sector_t)i << bitmap->counts.chunkshift,
1086 needed);
1088 return 0;
1091 outofdate = test_bit(BITMAP_STALE, &bitmap->flags);
1092 if (outofdate)
1093 pr_warn("%s: bitmap file is out of date, doing full recovery\n", bmname(bitmap));
1095 if (file && i_size_read(file->f_mapping->host) < store->bytes) {
1096 pr_warn("%s: bitmap file too short %lu < %lu\n",
1097 bmname(bitmap),
1098 (unsigned long) i_size_read(file->f_mapping->host),
1099 store->bytes);
1100 goto err;
1103 oldindex = ~0L;
1104 offset = 0;
1105 if (!bitmap->mddev->bitmap_info.external)
1106 offset = sizeof(bitmap_super_t);
1108 if (mddev_is_clustered(bitmap->mddev))
1109 node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE));
1111 for (i = 0; i < chunks; i++) {
1112 int b;
1113 index = file_page_index(&bitmap->storage, i);
1114 bit = file_page_offset(&bitmap->storage, i);
1115 if (index != oldindex) { /* this is a new page, read it in */
1116 int count;
1117 /* unmap the old page, we're done with it */
1118 if (index == store->file_pages-1)
1119 count = store->bytes - index * PAGE_SIZE;
1120 else
1121 count = PAGE_SIZE;
1122 page = store->filemap[index];
1123 if (file)
1124 ret = read_page(file, index, bitmap,
1125 count, page);
1126 else
1127 ret = read_sb_page(
1128 bitmap->mddev,
1129 bitmap->mddev->bitmap_info.offset,
1130 page,
1131 index + node_offset, count);
1133 if (ret)
1134 goto err;
1136 oldindex = index;
1138 if (outofdate) {
1140 * if bitmap is out of date, dirty the
1141 * whole page and write it out
1143 paddr = kmap_atomic(page);
1144 memset(paddr + offset, 0xff,
1145 PAGE_SIZE - offset);
1146 kunmap_atomic(paddr);
1147 write_page(bitmap, page, 1);
1149 ret = -EIO;
1150 if (test_bit(BITMAP_WRITE_ERROR,
1151 &bitmap->flags))
1152 goto err;
1155 paddr = kmap_atomic(page);
1156 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
1157 b = test_bit(bit, paddr);
1158 else
1159 b = test_bit_le(bit, paddr);
1160 kunmap_atomic(paddr);
1161 if (b) {
1162 /* if the disk bit is set, set the memory bit */
1163 int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift
1164 >= start);
1165 md_bitmap_set_memory_bits(bitmap,
1166 (sector_t)i << bitmap->counts.chunkshift,
1167 needed);
1168 bit_cnt++;
1170 offset = 0;
1173 pr_debug("%s: bitmap initialized from disk: read %lu pages, set %lu of %lu bits\n",
1174 bmname(bitmap), store->file_pages,
1175 bit_cnt, chunks);
1177 return 0;
1179 err:
1180 pr_warn("%s: bitmap initialisation failed: %d\n",
1181 bmname(bitmap), ret);
1182 return ret;
1185 void md_bitmap_write_all(struct bitmap *bitmap)
1187 /* We don't actually write all bitmap blocks here,
1188 * just flag them as needing to be written
1190 int i;
1192 if (!bitmap || !bitmap->storage.filemap)
1193 return;
1194 if (bitmap->storage.file)
1195 /* Only one copy, so nothing needed */
1196 return;
1198 for (i = 0; i < bitmap->storage.file_pages; i++)
1199 set_page_attr(bitmap, i,
1200 BITMAP_PAGE_NEEDWRITE);
1201 bitmap->allclean = 0;
1204 static void md_bitmap_count_page(struct bitmap_counts *bitmap,
1205 sector_t offset, int inc)
1207 sector_t chunk = offset >> bitmap->chunkshift;
1208 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1209 bitmap->bp[page].count += inc;
1210 md_bitmap_checkfree(bitmap, page);
1213 static void md_bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset)
1215 sector_t chunk = offset >> bitmap->chunkshift;
1216 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1217 struct bitmap_page *bp = &bitmap->bp[page];
1219 if (!bp->pending)
1220 bp->pending = 1;
1223 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
1224 sector_t offset, sector_t *blocks,
1225 int create);
1228 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1229 * out to disk
1232 void md_bitmap_daemon_work(struct mddev *mddev)
1234 struct bitmap *bitmap;
1235 unsigned long j;
1236 unsigned long nextpage;
1237 sector_t blocks;
1238 struct bitmap_counts *counts;
1240 /* Use a mutex to guard daemon_work against
1241 * bitmap_destroy.
1243 mutex_lock(&mddev->bitmap_info.mutex);
1244 bitmap = mddev->bitmap;
1245 if (bitmap == NULL) {
1246 mutex_unlock(&mddev->bitmap_info.mutex);
1247 return;
1249 if (time_before(jiffies, bitmap->daemon_lastrun
1250 + mddev->bitmap_info.daemon_sleep))
1251 goto done;
1253 bitmap->daemon_lastrun = jiffies;
1254 if (bitmap->allclean) {
1255 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1256 goto done;
1258 bitmap->allclean = 1;
1260 if (bitmap->mddev->queue)
1261 blk_add_trace_msg(bitmap->mddev->queue,
1262 "md bitmap_daemon_work");
1264 /* Any file-page which is PENDING now needs to be written.
1265 * So set NEEDWRITE now, then after we make any last-minute changes
1266 * we will write it.
1268 for (j = 0; j < bitmap->storage.file_pages; j++)
1269 if (test_and_clear_page_attr(bitmap, j,
1270 BITMAP_PAGE_PENDING))
1271 set_page_attr(bitmap, j,
1272 BITMAP_PAGE_NEEDWRITE);
1274 if (bitmap->need_sync &&
1275 mddev->bitmap_info.external == 0) {
1276 /* Arrange for superblock update as well as
1277 * other changes */
1278 bitmap_super_t *sb;
1279 bitmap->need_sync = 0;
1280 if (bitmap->storage.filemap) {
1281 sb = kmap_atomic(bitmap->storage.sb_page);
1282 sb->events_cleared =
1283 cpu_to_le64(bitmap->events_cleared);
1284 kunmap_atomic(sb);
1285 set_page_attr(bitmap, 0,
1286 BITMAP_PAGE_NEEDWRITE);
1289 /* Now look at the bitmap counters and if any are '2' or '1',
1290 * decrement and handle accordingly.
1292 counts = &bitmap->counts;
1293 spin_lock_irq(&counts->lock);
1294 nextpage = 0;
1295 for (j = 0; j < counts->chunks; j++) {
1296 bitmap_counter_t *bmc;
1297 sector_t block = (sector_t)j << counts->chunkshift;
1299 if (j == nextpage) {
1300 nextpage += PAGE_COUNTER_RATIO;
1301 if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) {
1302 j |= PAGE_COUNTER_MASK;
1303 continue;
1305 counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
1308 bmc = md_bitmap_get_counter(counts, block, &blocks, 0);
1309 if (!bmc) {
1310 j |= PAGE_COUNTER_MASK;
1311 continue;
1313 if (*bmc == 1 && !bitmap->need_sync) {
1314 /* We can clear the bit */
1315 *bmc = 0;
1316 md_bitmap_count_page(counts, block, -1);
1317 md_bitmap_file_clear_bit(bitmap, block);
1318 } else if (*bmc && *bmc <= 2) {
1319 *bmc = 1;
1320 md_bitmap_set_pending(counts, block);
1321 bitmap->allclean = 0;
1324 spin_unlock_irq(&counts->lock);
1326 md_bitmap_wait_writes(bitmap);
1327 /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
1328 * DIRTY pages need to be written by bitmap_unplug so it can wait
1329 * for them.
1330 * If we find any DIRTY page we stop there and let bitmap_unplug
1331 * handle all the rest. This is important in the case where
1332 * the first blocking holds the superblock and it has been updated.
1333 * We mustn't write any other blocks before the superblock.
1335 for (j = 0;
1336 j < bitmap->storage.file_pages
1337 && !test_bit(BITMAP_STALE, &bitmap->flags);
1338 j++) {
1339 if (test_page_attr(bitmap, j,
1340 BITMAP_PAGE_DIRTY))
1341 /* bitmap_unplug will handle the rest */
1342 break;
1343 if (bitmap->storage.filemap &&
1344 test_and_clear_page_attr(bitmap, j,
1345 BITMAP_PAGE_NEEDWRITE)) {
1346 write_page(bitmap, bitmap->storage.filemap[j], 0);
1350 done:
1351 if (bitmap->allclean == 0)
1352 mddev->thread->timeout =
1353 mddev->bitmap_info.daemon_sleep;
1354 mutex_unlock(&mddev->bitmap_info.mutex);
1357 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
1358 sector_t offset, sector_t *blocks,
1359 int create)
1360 __releases(bitmap->lock)
1361 __acquires(bitmap->lock)
1363 /* If 'create', we might release the lock and reclaim it.
1364 * The lock must have been taken with interrupts enabled.
1365 * If !create, we don't release the lock.
1367 sector_t chunk = offset >> bitmap->chunkshift;
1368 unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
1369 unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
1370 sector_t csize;
1371 int err;
1373 err = md_bitmap_checkpage(bitmap, page, create, 0);
1375 if (bitmap->bp[page].hijacked ||
1376 bitmap->bp[page].map == NULL)
1377 csize = ((sector_t)1) << (bitmap->chunkshift +
1378 PAGE_COUNTER_SHIFT - 1);
1379 else
1380 csize = ((sector_t)1) << bitmap->chunkshift;
1381 *blocks = csize - (offset & (csize - 1));
1383 if (err < 0)
1384 return NULL;
1386 /* now locked ... */
1388 if (bitmap->bp[page].hijacked) { /* hijacked pointer */
1389 /* should we use the first or second counter field
1390 * of the hijacked pointer? */
1391 int hi = (pageoff > PAGE_COUNTER_MASK);
1392 return &((bitmap_counter_t *)
1393 &bitmap->bp[page].map)[hi];
1394 } else /* page is allocated */
1395 return (bitmap_counter_t *)
1396 &(bitmap->bp[page].map[pageoff]);
1399 int md_bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
1401 if (!bitmap)
1402 return 0;
1404 if (behind) {
1405 int bw;
1406 atomic_inc(&bitmap->behind_writes);
1407 bw = atomic_read(&bitmap->behind_writes);
1408 if (bw > bitmap->behind_writes_used)
1409 bitmap->behind_writes_used = bw;
1411 pr_debug("inc write-behind count %d/%lu\n",
1412 bw, bitmap->mddev->bitmap_info.max_write_behind);
1415 while (sectors) {
1416 sector_t blocks;
1417 bitmap_counter_t *bmc;
1419 spin_lock_irq(&bitmap->counts.lock);
1420 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 1);
1421 if (!bmc) {
1422 spin_unlock_irq(&bitmap->counts.lock);
1423 return 0;
1426 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
1427 DEFINE_WAIT(__wait);
1428 /* note that it is safe to do the prepare_to_wait
1429 * after the test as long as we do it before dropping
1430 * the spinlock.
1432 prepare_to_wait(&bitmap->overflow_wait, &__wait,
1433 TASK_UNINTERRUPTIBLE);
1434 spin_unlock_irq(&bitmap->counts.lock);
1435 schedule();
1436 finish_wait(&bitmap->overflow_wait, &__wait);
1437 continue;
1440 switch (*bmc) {
1441 case 0:
1442 md_bitmap_file_set_bit(bitmap, offset);
1443 md_bitmap_count_page(&bitmap->counts, offset, 1);
1444 /* fall through */
1445 case 1:
1446 *bmc = 2;
1449 (*bmc)++;
1451 spin_unlock_irq(&bitmap->counts.lock);
1453 offset += blocks;
1454 if (sectors > blocks)
1455 sectors -= blocks;
1456 else
1457 sectors = 0;
1459 return 0;
1461 EXPORT_SYMBOL(md_bitmap_startwrite);
1463 void md_bitmap_endwrite(struct bitmap *bitmap, sector_t offset,
1464 unsigned long sectors, int success, int behind)
1466 if (!bitmap)
1467 return;
1468 if (behind) {
1469 if (atomic_dec_and_test(&bitmap->behind_writes))
1470 wake_up(&bitmap->behind_wait);
1471 pr_debug("dec write-behind count %d/%lu\n",
1472 atomic_read(&bitmap->behind_writes),
1473 bitmap->mddev->bitmap_info.max_write_behind);
1476 while (sectors) {
1477 sector_t blocks;
1478 unsigned long flags;
1479 bitmap_counter_t *bmc;
1481 spin_lock_irqsave(&bitmap->counts.lock, flags);
1482 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 0);
1483 if (!bmc) {
1484 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
1485 return;
1488 if (success && !bitmap->mddev->degraded &&
1489 bitmap->events_cleared < bitmap->mddev->events) {
1490 bitmap->events_cleared = bitmap->mddev->events;
1491 bitmap->need_sync = 1;
1492 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
1495 if (!success && !NEEDED(*bmc))
1496 *bmc |= NEEDED_MASK;
1498 if (COUNTER(*bmc) == COUNTER_MAX)
1499 wake_up(&bitmap->overflow_wait);
1501 (*bmc)--;
1502 if (*bmc <= 2) {
1503 md_bitmap_set_pending(&bitmap->counts, offset);
1504 bitmap->allclean = 0;
1506 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
1507 offset += blocks;
1508 if (sectors > blocks)
1509 sectors -= blocks;
1510 else
1511 sectors = 0;
1514 EXPORT_SYMBOL(md_bitmap_endwrite);
1516 static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
1517 int degraded)
1519 bitmap_counter_t *bmc;
1520 int rv;
1521 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
1522 *blocks = 1024;
1523 return 1; /* always resync if no bitmap */
1525 spin_lock_irq(&bitmap->counts.lock);
1526 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
1527 rv = 0;
1528 if (bmc) {
1529 /* locked */
1530 if (RESYNC(*bmc))
1531 rv = 1;
1532 else if (NEEDED(*bmc)) {
1533 rv = 1;
1534 if (!degraded) { /* don't set/clear bits if degraded */
1535 *bmc |= RESYNC_MASK;
1536 *bmc &= ~NEEDED_MASK;
1540 spin_unlock_irq(&bitmap->counts.lock);
1541 return rv;
1544 int md_bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
1545 int degraded)
1547 /* bitmap_start_sync must always report on multiples of whole
1548 * pages, otherwise resync (which is very PAGE_SIZE based) will
1549 * get confused.
1550 * So call __bitmap_start_sync repeatedly (if needed) until
1551 * At least PAGE_SIZE>>9 blocks are covered.
1552 * Return the 'or' of the result.
1554 int rv = 0;
1555 sector_t blocks1;
1557 *blocks = 0;
1558 while (*blocks < (PAGE_SIZE>>9)) {
1559 rv |= __bitmap_start_sync(bitmap, offset,
1560 &blocks1, degraded);
1561 offset += blocks1;
1562 *blocks += blocks1;
1564 return rv;
1566 EXPORT_SYMBOL(md_bitmap_start_sync);
1568 void md_bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
1570 bitmap_counter_t *bmc;
1571 unsigned long flags;
1573 if (bitmap == NULL) {
1574 *blocks = 1024;
1575 return;
1577 spin_lock_irqsave(&bitmap->counts.lock, flags);
1578 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
1579 if (bmc == NULL)
1580 goto unlock;
1581 /* locked */
1582 if (RESYNC(*bmc)) {
1583 *bmc &= ~RESYNC_MASK;
1585 if (!NEEDED(*bmc) && aborted)
1586 *bmc |= NEEDED_MASK;
1587 else {
1588 if (*bmc <= 2) {
1589 md_bitmap_set_pending(&bitmap->counts, offset);
1590 bitmap->allclean = 0;
1594 unlock:
1595 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
1597 EXPORT_SYMBOL(md_bitmap_end_sync);
1599 void md_bitmap_close_sync(struct bitmap *bitmap)
1601 /* Sync has finished, and any bitmap chunks that weren't synced
1602 * properly have been aborted. It remains to us to clear the
1603 * RESYNC bit wherever it is still on
1605 sector_t sector = 0;
1606 sector_t blocks;
1607 if (!bitmap)
1608 return;
1609 while (sector < bitmap->mddev->resync_max_sectors) {
1610 md_bitmap_end_sync(bitmap, sector, &blocks, 0);
1611 sector += blocks;
1614 EXPORT_SYMBOL(md_bitmap_close_sync);
1616 void md_bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector, bool force)
1618 sector_t s = 0;
1619 sector_t blocks;
1621 if (!bitmap)
1622 return;
1623 if (sector == 0) {
1624 bitmap->last_end_sync = jiffies;
1625 return;
1627 if (!force && time_before(jiffies, (bitmap->last_end_sync
1628 + bitmap->mddev->bitmap_info.daemon_sleep)))
1629 return;
1630 wait_event(bitmap->mddev->recovery_wait,
1631 atomic_read(&bitmap->mddev->recovery_active) == 0);
1633 bitmap->mddev->curr_resync_completed = sector;
1634 set_bit(MD_SB_CHANGE_CLEAN, &bitmap->mddev->sb_flags);
1635 sector &= ~((1ULL << bitmap->counts.chunkshift) - 1);
1636 s = 0;
1637 while (s < sector && s < bitmap->mddev->resync_max_sectors) {
1638 md_bitmap_end_sync(bitmap, s, &blocks, 0);
1639 s += blocks;
1641 bitmap->last_end_sync = jiffies;
1642 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
1644 EXPORT_SYMBOL(md_bitmap_cond_end_sync);
1646 void md_bitmap_sync_with_cluster(struct mddev *mddev,
1647 sector_t old_lo, sector_t old_hi,
1648 sector_t new_lo, sector_t new_hi)
1650 struct bitmap *bitmap = mddev->bitmap;
1651 sector_t sector, blocks = 0;
1653 for (sector = old_lo; sector < new_lo; ) {
1654 md_bitmap_end_sync(bitmap, sector, &blocks, 0);
1655 sector += blocks;
1657 WARN((blocks > new_lo) && old_lo, "alignment is not correct for lo\n");
1659 for (sector = old_hi; sector < new_hi; ) {
1660 md_bitmap_start_sync(bitmap, sector, &blocks, 0);
1661 sector += blocks;
1663 WARN((blocks > new_hi) && old_hi, "alignment is not correct for hi\n");
1665 EXPORT_SYMBOL(md_bitmap_sync_with_cluster);
1667 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
1669 /* For each chunk covered by any of these sectors, set the
1670 * counter to 2 and possibly set resync_needed. They should all
1671 * be 0 at this point
1674 sector_t secs;
1675 bitmap_counter_t *bmc;
1676 spin_lock_irq(&bitmap->counts.lock);
1677 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &secs, 1);
1678 if (!bmc) {
1679 spin_unlock_irq(&bitmap->counts.lock);
1680 return;
1682 if (!*bmc) {
1683 *bmc = 2;
1684 md_bitmap_count_page(&bitmap->counts, offset, 1);
1685 md_bitmap_set_pending(&bitmap->counts, offset);
1686 bitmap->allclean = 0;
1688 if (needed)
1689 *bmc |= NEEDED_MASK;
1690 spin_unlock_irq(&bitmap->counts.lock);
1693 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1694 void md_bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
1696 unsigned long chunk;
1698 for (chunk = s; chunk <= e; chunk++) {
1699 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift;
1700 md_bitmap_set_memory_bits(bitmap, sec, 1);
1701 md_bitmap_file_set_bit(bitmap, sec);
1702 if (sec < bitmap->mddev->recovery_cp)
1703 /* We are asserting that the array is dirty,
1704 * so move the recovery_cp address back so
1705 * that it is obvious that it is dirty
1707 bitmap->mddev->recovery_cp = sec;
1712 * flush out any pending updates
1714 void md_bitmap_flush(struct mddev *mddev)
1716 struct bitmap *bitmap = mddev->bitmap;
1717 long sleep;
1719 if (!bitmap) /* there was no bitmap */
1720 return;
1722 /* run the daemon_work three time to ensure everything is flushed
1723 * that can be
1725 sleep = mddev->bitmap_info.daemon_sleep * 2;
1726 bitmap->daemon_lastrun -= sleep;
1727 md_bitmap_daemon_work(mddev);
1728 bitmap->daemon_lastrun -= sleep;
1729 md_bitmap_daemon_work(mddev);
1730 bitmap->daemon_lastrun -= sleep;
1731 md_bitmap_daemon_work(mddev);
1732 md_bitmap_update_sb(bitmap);
1736 * free memory that was allocated
1738 void md_bitmap_free(struct bitmap *bitmap)
1740 unsigned long k, pages;
1741 struct bitmap_page *bp;
1743 if (!bitmap) /* there was no bitmap */
1744 return;
1746 if (bitmap->sysfs_can_clear)
1747 sysfs_put(bitmap->sysfs_can_clear);
1749 if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info &&
1750 bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev))
1751 md_cluster_stop(bitmap->mddev);
1753 /* Shouldn't be needed - but just in case.... */
1754 wait_event(bitmap->write_wait,
1755 atomic_read(&bitmap->pending_writes) == 0);
1757 /* release the bitmap file */
1758 md_bitmap_file_unmap(&bitmap->storage);
1760 bp = bitmap->counts.bp;
1761 pages = bitmap->counts.pages;
1763 /* free all allocated memory */
1765 if (bp) /* deallocate the page memory */
1766 for (k = 0; k < pages; k++)
1767 if (bp[k].map && !bp[k].hijacked)
1768 kfree(bp[k].map);
1769 kfree(bp);
1770 kfree(bitmap);
1772 EXPORT_SYMBOL(md_bitmap_free);
1774 void md_bitmap_wait_behind_writes(struct mddev *mddev)
1776 struct bitmap *bitmap = mddev->bitmap;
1778 /* wait for behind writes to complete */
1779 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
1780 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
1781 mdname(mddev));
1782 /* need to kick something here to make sure I/O goes? */
1783 wait_event(bitmap->behind_wait,
1784 atomic_read(&bitmap->behind_writes) == 0);
1788 void md_bitmap_destroy(struct mddev *mddev)
1790 struct bitmap *bitmap = mddev->bitmap;
1792 if (!bitmap) /* there was no bitmap */
1793 return;
1795 md_bitmap_wait_behind_writes(mddev);
1796 if (!mddev->serialize_policy)
1797 mddev_destroy_serial_pool(mddev, NULL, true);
1799 mutex_lock(&mddev->bitmap_info.mutex);
1800 spin_lock(&mddev->lock);
1801 mddev->bitmap = NULL; /* disconnect from the md device */
1802 spin_unlock(&mddev->lock);
1803 mutex_unlock(&mddev->bitmap_info.mutex);
1804 if (mddev->thread)
1805 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
1807 md_bitmap_free(bitmap);
1811 * initialize the bitmap structure
1812 * if this returns an error, bitmap_destroy must be called to do clean up
1813 * once mddev->bitmap is set
1815 struct bitmap *md_bitmap_create(struct mddev *mddev, int slot)
1817 struct bitmap *bitmap;
1818 sector_t blocks = mddev->resync_max_sectors;
1819 struct file *file = mddev->bitmap_info.file;
1820 int err;
1821 struct kernfs_node *bm = NULL;
1823 BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
1825 BUG_ON(file && mddev->bitmap_info.offset);
1827 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
1828 pr_notice("md/raid:%s: array with journal cannot have bitmap\n",
1829 mdname(mddev));
1830 return ERR_PTR(-EBUSY);
1833 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
1834 if (!bitmap)
1835 return ERR_PTR(-ENOMEM);
1837 spin_lock_init(&bitmap->counts.lock);
1838 atomic_set(&bitmap->pending_writes, 0);
1839 init_waitqueue_head(&bitmap->write_wait);
1840 init_waitqueue_head(&bitmap->overflow_wait);
1841 init_waitqueue_head(&bitmap->behind_wait);
1843 bitmap->mddev = mddev;
1844 bitmap->cluster_slot = slot;
1846 if (mddev->kobj.sd)
1847 bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap");
1848 if (bm) {
1849 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear");
1850 sysfs_put(bm);
1851 } else
1852 bitmap->sysfs_can_clear = NULL;
1854 bitmap->storage.file = file;
1855 if (file) {
1856 get_file(file);
1857 /* As future accesses to this file will use bmap,
1858 * and bypass the page cache, we must sync the file
1859 * first.
1861 vfs_fsync(file, 1);
1863 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
1864 if (!mddev->bitmap_info.external) {
1866 * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
1867 * instructing us to create a new on-disk bitmap instance.
1869 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
1870 err = md_bitmap_new_disk_sb(bitmap);
1871 else
1872 err = md_bitmap_read_sb(bitmap);
1873 } else {
1874 err = 0;
1875 if (mddev->bitmap_info.chunksize == 0 ||
1876 mddev->bitmap_info.daemon_sleep == 0)
1877 /* chunksize and time_base need to be
1878 * set first. */
1879 err = -EINVAL;
1881 if (err)
1882 goto error;
1884 bitmap->daemon_lastrun = jiffies;
1885 err = md_bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
1886 if (err)
1887 goto error;
1889 pr_debug("created bitmap (%lu pages) for device %s\n",
1890 bitmap->counts.pages, bmname(bitmap));
1892 err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
1893 if (err)
1894 goto error;
1896 return bitmap;
1897 error:
1898 md_bitmap_free(bitmap);
1899 return ERR_PTR(err);
1902 int md_bitmap_load(struct mddev *mddev)
1904 int err = 0;
1905 sector_t start = 0;
1906 sector_t sector = 0;
1907 struct bitmap *bitmap = mddev->bitmap;
1908 struct md_rdev *rdev;
1910 if (!bitmap)
1911 goto out;
1913 rdev_for_each(rdev, mddev)
1914 mddev_create_serial_pool(mddev, rdev, true);
1916 if (mddev_is_clustered(mddev))
1917 md_cluster_ops->load_bitmaps(mddev, mddev->bitmap_info.nodes);
1919 /* Clear out old bitmap info first: Either there is none, or we
1920 * are resuming after someone else has possibly changed things,
1921 * so we should forget old cached info.
1922 * All chunks should be clean, but some might need_sync.
1924 while (sector < mddev->resync_max_sectors) {
1925 sector_t blocks;
1926 md_bitmap_start_sync(bitmap, sector, &blocks, 0);
1927 sector += blocks;
1929 md_bitmap_close_sync(bitmap);
1931 if (mddev->degraded == 0
1932 || bitmap->events_cleared == mddev->events)
1933 /* no need to keep dirty bits to optimise a
1934 * re-add of a missing device */
1935 start = mddev->recovery_cp;
1937 mutex_lock(&mddev->bitmap_info.mutex);
1938 err = md_bitmap_init_from_disk(bitmap, start);
1939 mutex_unlock(&mddev->bitmap_info.mutex);
1941 if (err)
1942 goto out;
1943 clear_bit(BITMAP_STALE, &bitmap->flags);
1945 /* Kick recovery in case any bits were set */
1946 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
1948 mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
1949 md_wakeup_thread(mddev->thread);
1951 md_bitmap_update_sb(bitmap);
1953 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
1954 err = -EIO;
1955 out:
1956 return err;
1958 EXPORT_SYMBOL_GPL(md_bitmap_load);
1960 struct bitmap *get_bitmap_from_slot(struct mddev *mddev, int slot)
1962 int rv = 0;
1963 struct bitmap *bitmap;
1965 bitmap = md_bitmap_create(mddev, slot);
1966 if (IS_ERR(bitmap)) {
1967 rv = PTR_ERR(bitmap);
1968 return ERR_PTR(rv);
1971 rv = md_bitmap_init_from_disk(bitmap, 0);
1972 if (rv) {
1973 md_bitmap_free(bitmap);
1974 return ERR_PTR(rv);
1977 return bitmap;
1979 EXPORT_SYMBOL(get_bitmap_from_slot);
1981 /* Loads the bitmap associated with slot and copies the resync information
1982 * to our bitmap
1984 int md_bitmap_copy_from_slot(struct mddev *mddev, int slot,
1985 sector_t *low, sector_t *high, bool clear_bits)
1987 int rv = 0, i, j;
1988 sector_t block, lo = 0, hi = 0;
1989 struct bitmap_counts *counts;
1990 struct bitmap *bitmap;
1992 bitmap = get_bitmap_from_slot(mddev, slot);
1993 if (IS_ERR(bitmap)) {
1994 pr_err("%s can't get bitmap from slot %d\n", __func__, slot);
1995 return -1;
1998 counts = &bitmap->counts;
1999 for (j = 0; j < counts->chunks; j++) {
2000 block = (sector_t)j << counts->chunkshift;
2001 if (md_bitmap_file_test_bit(bitmap, block)) {
2002 if (!lo)
2003 lo = block;
2004 hi = block;
2005 md_bitmap_file_clear_bit(bitmap, block);
2006 md_bitmap_set_memory_bits(mddev->bitmap, block, 1);
2007 md_bitmap_file_set_bit(mddev->bitmap, block);
2011 if (clear_bits) {
2012 md_bitmap_update_sb(bitmap);
2013 /* BITMAP_PAGE_PENDING is set, but bitmap_unplug needs
2014 * BITMAP_PAGE_DIRTY or _NEEDWRITE to write ... */
2015 for (i = 0; i < bitmap->storage.file_pages; i++)
2016 if (test_page_attr(bitmap, i, BITMAP_PAGE_PENDING))
2017 set_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE);
2018 md_bitmap_unplug(bitmap);
2020 md_bitmap_unplug(mddev->bitmap);
2021 *low = lo;
2022 *high = hi;
2024 return rv;
2026 EXPORT_SYMBOL_GPL(md_bitmap_copy_from_slot);
2029 void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
2031 unsigned long chunk_kb;
2032 struct bitmap_counts *counts;
2034 if (!bitmap)
2035 return;
2037 counts = &bitmap->counts;
2039 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
2040 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
2041 "%lu%s chunk",
2042 counts->pages - counts->missing_pages,
2043 counts->pages,
2044 (counts->pages - counts->missing_pages)
2045 << (PAGE_SHIFT - 10),
2046 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
2047 chunk_kb ? "KB" : "B");
2048 if (bitmap->storage.file) {
2049 seq_printf(seq, ", file: ");
2050 seq_file_path(seq, bitmap->storage.file, " \t\n");
2053 seq_printf(seq, "\n");
2056 int md_bitmap_resize(struct bitmap *bitmap, sector_t blocks,
2057 int chunksize, int init)
2059 /* If chunk_size is 0, choose an appropriate chunk size.
2060 * Then possibly allocate new storage space.
2061 * Then quiesce, copy bits, replace bitmap, and re-start
2063 * This function is called both to set up the initial bitmap
2064 * and to resize the bitmap while the array is active.
2065 * If this happens as a result of the array being resized,
2066 * chunksize will be zero, and we need to choose a suitable
2067 * chunksize, otherwise we use what we are given.
2069 struct bitmap_storage store;
2070 struct bitmap_counts old_counts;
2071 unsigned long chunks;
2072 sector_t block;
2073 sector_t old_blocks, new_blocks;
2074 int chunkshift;
2075 int ret = 0;
2076 long pages;
2077 struct bitmap_page *new_bp;
2079 if (bitmap->storage.file && !init) {
2080 pr_info("md: cannot resize file-based bitmap\n");
2081 return -EINVAL;
2084 if (chunksize == 0) {
2085 /* If there is enough space, leave the chunk size unchanged,
2086 * else increase by factor of two until there is enough space.
2088 long bytes;
2089 long space = bitmap->mddev->bitmap_info.space;
2091 if (space == 0) {
2092 /* We don't know how much space there is, so limit
2093 * to current size - in sectors.
2095 bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
2096 if (!bitmap->mddev->bitmap_info.external)
2097 bytes += sizeof(bitmap_super_t);
2098 space = DIV_ROUND_UP(bytes, 512);
2099 bitmap->mddev->bitmap_info.space = space;
2101 chunkshift = bitmap->counts.chunkshift;
2102 chunkshift--;
2103 do {
2104 /* 'chunkshift' is shift from block size to chunk size */
2105 chunkshift++;
2106 chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
2107 bytes = DIV_ROUND_UP(chunks, 8);
2108 if (!bitmap->mddev->bitmap_info.external)
2109 bytes += sizeof(bitmap_super_t);
2110 } while (bytes > (space << 9));
2111 } else
2112 chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
2114 chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
2115 memset(&store, 0, sizeof(store));
2116 if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
2117 ret = md_bitmap_storage_alloc(&store, chunks,
2118 !bitmap->mddev->bitmap_info.external,
2119 mddev_is_clustered(bitmap->mddev)
2120 ? bitmap->cluster_slot : 0);
2121 if (ret) {
2122 md_bitmap_file_unmap(&store);
2123 goto err;
2126 pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
2128 new_bp = kcalloc(pages, sizeof(*new_bp), GFP_KERNEL);
2129 ret = -ENOMEM;
2130 if (!new_bp) {
2131 md_bitmap_file_unmap(&store);
2132 goto err;
2135 if (!init)
2136 bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
2138 store.file = bitmap->storage.file;
2139 bitmap->storage.file = NULL;
2141 if (store.sb_page && bitmap->storage.sb_page)
2142 memcpy(page_address(store.sb_page),
2143 page_address(bitmap->storage.sb_page),
2144 sizeof(bitmap_super_t));
2145 spin_lock_irq(&bitmap->counts.lock);
2146 md_bitmap_file_unmap(&bitmap->storage);
2147 bitmap->storage = store;
2149 old_counts = bitmap->counts;
2150 bitmap->counts.bp = new_bp;
2151 bitmap->counts.pages = pages;
2152 bitmap->counts.missing_pages = pages;
2153 bitmap->counts.chunkshift = chunkshift;
2154 bitmap->counts.chunks = chunks;
2155 bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
2156 BITMAP_BLOCK_SHIFT);
2158 blocks = min(old_counts.chunks << old_counts.chunkshift,
2159 chunks << chunkshift);
2161 /* For cluster raid, need to pre-allocate bitmap */
2162 if (mddev_is_clustered(bitmap->mddev)) {
2163 unsigned long page;
2164 for (page = 0; page < pages; page++) {
2165 ret = md_bitmap_checkpage(&bitmap->counts, page, 1, 1);
2166 if (ret) {
2167 unsigned long k;
2169 /* deallocate the page memory */
2170 for (k = 0; k < page; k++) {
2171 kfree(new_bp[k].map);
2173 kfree(new_bp);
2175 /* restore some fields from old_counts */
2176 bitmap->counts.bp = old_counts.bp;
2177 bitmap->counts.pages = old_counts.pages;
2178 bitmap->counts.missing_pages = old_counts.pages;
2179 bitmap->counts.chunkshift = old_counts.chunkshift;
2180 bitmap->counts.chunks = old_counts.chunks;
2181 bitmap->mddev->bitmap_info.chunksize = 1 << (old_counts.chunkshift +
2182 BITMAP_BLOCK_SHIFT);
2183 blocks = old_counts.chunks << old_counts.chunkshift;
2184 pr_warn("Could not pre-allocate in-memory bitmap for cluster raid\n");
2185 break;
2186 } else
2187 bitmap->counts.bp[page].count += 1;
2191 for (block = 0; block < blocks; ) {
2192 bitmap_counter_t *bmc_old, *bmc_new;
2193 int set;
2195 bmc_old = md_bitmap_get_counter(&old_counts, block, &old_blocks, 0);
2196 set = bmc_old && NEEDED(*bmc_old);
2198 if (set) {
2199 bmc_new = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1);
2200 if (*bmc_new == 0) {
2201 /* need to set on-disk bits too. */
2202 sector_t end = block + new_blocks;
2203 sector_t start = block >> chunkshift;
2204 start <<= chunkshift;
2205 while (start < end) {
2206 md_bitmap_file_set_bit(bitmap, block);
2207 start += 1 << chunkshift;
2209 *bmc_new = 2;
2210 md_bitmap_count_page(&bitmap->counts, block, 1);
2211 md_bitmap_set_pending(&bitmap->counts, block);
2213 *bmc_new |= NEEDED_MASK;
2214 if (new_blocks < old_blocks)
2215 old_blocks = new_blocks;
2217 block += old_blocks;
2220 if (bitmap->counts.bp != old_counts.bp) {
2221 unsigned long k;
2222 for (k = 0; k < old_counts.pages; k++)
2223 if (!old_counts.bp[k].hijacked)
2224 kfree(old_counts.bp[k].map);
2225 kfree(old_counts.bp);
2228 if (!init) {
2229 int i;
2230 while (block < (chunks << chunkshift)) {
2231 bitmap_counter_t *bmc;
2232 bmc = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1);
2233 if (bmc) {
2234 /* new space. It needs to be resynced, so
2235 * we set NEEDED_MASK.
2237 if (*bmc == 0) {
2238 *bmc = NEEDED_MASK | 2;
2239 md_bitmap_count_page(&bitmap->counts, block, 1);
2240 md_bitmap_set_pending(&bitmap->counts, block);
2243 block += new_blocks;
2245 for (i = 0; i < bitmap->storage.file_pages; i++)
2246 set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
2248 spin_unlock_irq(&bitmap->counts.lock);
2250 if (!init) {
2251 md_bitmap_unplug(bitmap);
2252 bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
2254 ret = 0;
2255 err:
2256 return ret;
2258 EXPORT_SYMBOL_GPL(md_bitmap_resize);
2260 static ssize_t
2261 location_show(struct mddev *mddev, char *page)
2263 ssize_t len;
2264 if (mddev->bitmap_info.file)
2265 len = sprintf(page, "file");
2266 else if (mddev->bitmap_info.offset)
2267 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
2268 else
2269 len = sprintf(page, "none");
2270 len += sprintf(page+len, "\n");
2271 return len;
2274 static ssize_t
2275 location_store(struct mddev *mddev, const char *buf, size_t len)
2277 int rv;
2279 rv = mddev_lock(mddev);
2280 if (rv)
2281 return rv;
2282 if (mddev->pers) {
2283 if (!mddev->pers->quiesce) {
2284 rv = -EBUSY;
2285 goto out;
2287 if (mddev->recovery || mddev->sync_thread) {
2288 rv = -EBUSY;
2289 goto out;
2293 if (mddev->bitmap || mddev->bitmap_info.file ||
2294 mddev->bitmap_info.offset) {
2295 /* bitmap already configured. Only option is to clear it */
2296 if (strncmp(buf, "none", 4) != 0) {
2297 rv = -EBUSY;
2298 goto out;
2300 if (mddev->pers) {
2301 mddev_suspend(mddev);
2302 md_bitmap_destroy(mddev);
2303 mddev_resume(mddev);
2305 mddev->bitmap_info.offset = 0;
2306 if (mddev->bitmap_info.file) {
2307 struct file *f = mddev->bitmap_info.file;
2308 mddev->bitmap_info.file = NULL;
2309 fput(f);
2311 } else {
2312 /* No bitmap, OK to set a location */
2313 long long offset;
2314 if (strncmp(buf, "none", 4) == 0)
2315 /* nothing to be done */;
2316 else if (strncmp(buf, "file:", 5) == 0) {
2317 /* Not supported yet */
2318 rv = -EINVAL;
2319 goto out;
2320 } else {
2321 if (buf[0] == '+')
2322 rv = kstrtoll(buf+1, 10, &offset);
2323 else
2324 rv = kstrtoll(buf, 10, &offset);
2325 if (rv)
2326 goto out;
2327 if (offset == 0) {
2328 rv = -EINVAL;
2329 goto out;
2331 if (mddev->bitmap_info.external == 0 &&
2332 mddev->major_version == 0 &&
2333 offset != mddev->bitmap_info.default_offset) {
2334 rv = -EINVAL;
2335 goto out;
2337 mddev->bitmap_info.offset = offset;
2338 if (mddev->pers) {
2339 struct bitmap *bitmap;
2340 bitmap = md_bitmap_create(mddev, -1);
2341 mddev_suspend(mddev);
2342 if (IS_ERR(bitmap))
2343 rv = PTR_ERR(bitmap);
2344 else {
2345 mddev->bitmap = bitmap;
2346 rv = md_bitmap_load(mddev);
2347 if (rv)
2348 mddev->bitmap_info.offset = 0;
2350 if (rv) {
2351 md_bitmap_destroy(mddev);
2352 mddev_resume(mddev);
2353 goto out;
2355 mddev_resume(mddev);
2359 if (!mddev->external) {
2360 /* Ensure new bitmap info is stored in
2361 * metadata promptly.
2363 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2364 md_wakeup_thread(mddev->thread);
2366 rv = 0;
2367 out:
2368 mddev_unlock(mddev);
2369 if (rv)
2370 return rv;
2371 return len;
2374 static struct md_sysfs_entry bitmap_location =
2375 __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
2377 /* 'bitmap/space' is the space available at 'location' for the
2378 * bitmap. This allows the kernel to know when it is safe to
2379 * resize the bitmap to match a resized array.
2381 static ssize_t
2382 space_show(struct mddev *mddev, char *page)
2384 return sprintf(page, "%lu\n", mddev->bitmap_info.space);
2387 static ssize_t
2388 space_store(struct mddev *mddev, const char *buf, size_t len)
2390 unsigned long sectors;
2391 int rv;
2393 rv = kstrtoul(buf, 10, &sectors);
2394 if (rv)
2395 return rv;
2397 if (sectors == 0)
2398 return -EINVAL;
2400 if (mddev->bitmap &&
2401 sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
2402 return -EFBIG; /* Bitmap is too big for this small space */
2404 /* could make sure it isn't too big, but that isn't really
2405 * needed - user-space should be careful.
2407 mddev->bitmap_info.space = sectors;
2408 return len;
2411 static struct md_sysfs_entry bitmap_space =
2412 __ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
2414 static ssize_t
2415 timeout_show(struct mddev *mddev, char *page)
2417 ssize_t len;
2418 unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
2419 unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
2421 len = sprintf(page, "%lu", secs);
2422 if (jifs)
2423 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
2424 len += sprintf(page+len, "\n");
2425 return len;
2428 static ssize_t
2429 timeout_store(struct mddev *mddev, const char *buf, size_t len)
2431 /* timeout can be set at any time */
2432 unsigned long timeout;
2433 int rv = strict_strtoul_scaled(buf, &timeout, 4);
2434 if (rv)
2435 return rv;
2437 /* just to make sure we don't overflow... */
2438 if (timeout >= LONG_MAX / HZ)
2439 return -EINVAL;
2441 timeout = timeout * HZ / 10000;
2443 if (timeout >= MAX_SCHEDULE_TIMEOUT)
2444 timeout = MAX_SCHEDULE_TIMEOUT-1;
2445 if (timeout < 1)
2446 timeout = 1;
2447 mddev->bitmap_info.daemon_sleep = timeout;
2448 if (mddev->thread) {
2449 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
2450 * the bitmap is all clean and we don't need to
2451 * adjust the timeout right now
2453 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
2454 mddev->thread->timeout = timeout;
2455 md_wakeup_thread(mddev->thread);
2458 return len;
2461 static struct md_sysfs_entry bitmap_timeout =
2462 __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
2464 static ssize_t
2465 backlog_show(struct mddev *mddev, char *page)
2467 return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
2470 static ssize_t
2471 backlog_store(struct mddev *mddev, const char *buf, size_t len)
2473 unsigned long backlog;
2474 unsigned long old_mwb = mddev->bitmap_info.max_write_behind;
2475 int rv = kstrtoul(buf, 10, &backlog);
2476 if (rv)
2477 return rv;
2478 if (backlog > COUNTER_MAX)
2479 return -EINVAL;
2480 mddev->bitmap_info.max_write_behind = backlog;
2481 if (!backlog && mddev->serial_info_pool) {
2482 /* serial_info_pool is not needed if backlog is zero */
2483 if (!mddev->serialize_policy)
2484 mddev_destroy_serial_pool(mddev, NULL, false);
2485 } else if (backlog && !mddev->serial_info_pool) {
2486 /* serial_info_pool is needed since backlog is not zero */
2487 struct md_rdev *rdev;
2489 rdev_for_each(rdev, mddev)
2490 mddev_create_serial_pool(mddev, rdev, false);
2492 if (old_mwb != backlog)
2493 md_bitmap_update_sb(mddev->bitmap);
2494 return len;
2497 static struct md_sysfs_entry bitmap_backlog =
2498 __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
2500 static ssize_t
2501 chunksize_show(struct mddev *mddev, char *page)
2503 return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
2506 static ssize_t
2507 chunksize_store(struct mddev *mddev, const char *buf, size_t len)
2509 /* Can only be changed when no bitmap is active */
2510 int rv;
2511 unsigned long csize;
2512 if (mddev->bitmap)
2513 return -EBUSY;
2514 rv = kstrtoul(buf, 10, &csize);
2515 if (rv)
2516 return rv;
2517 if (csize < 512 ||
2518 !is_power_of_2(csize))
2519 return -EINVAL;
2520 mddev->bitmap_info.chunksize = csize;
2521 return len;
2524 static struct md_sysfs_entry bitmap_chunksize =
2525 __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
2527 static ssize_t metadata_show(struct mddev *mddev, char *page)
2529 if (mddev_is_clustered(mddev))
2530 return sprintf(page, "clustered\n");
2531 return sprintf(page, "%s\n", (mddev->bitmap_info.external
2532 ? "external" : "internal"));
2535 static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
2537 if (mddev->bitmap ||
2538 mddev->bitmap_info.file ||
2539 mddev->bitmap_info.offset)
2540 return -EBUSY;
2541 if (strncmp(buf, "external", 8) == 0)
2542 mddev->bitmap_info.external = 1;
2543 else if ((strncmp(buf, "internal", 8) == 0) ||
2544 (strncmp(buf, "clustered", 9) == 0))
2545 mddev->bitmap_info.external = 0;
2546 else
2547 return -EINVAL;
2548 return len;
2551 static struct md_sysfs_entry bitmap_metadata =
2552 __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2554 static ssize_t can_clear_show(struct mddev *mddev, char *page)
2556 int len;
2557 spin_lock(&mddev->lock);
2558 if (mddev->bitmap)
2559 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
2560 "false" : "true"));
2561 else
2562 len = sprintf(page, "\n");
2563 spin_unlock(&mddev->lock);
2564 return len;
2567 static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
2569 if (mddev->bitmap == NULL)
2570 return -ENOENT;
2571 if (strncmp(buf, "false", 5) == 0)
2572 mddev->bitmap->need_sync = 1;
2573 else if (strncmp(buf, "true", 4) == 0) {
2574 if (mddev->degraded)
2575 return -EBUSY;
2576 mddev->bitmap->need_sync = 0;
2577 } else
2578 return -EINVAL;
2579 return len;
2582 static struct md_sysfs_entry bitmap_can_clear =
2583 __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
2585 static ssize_t
2586 behind_writes_used_show(struct mddev *mddev, char *page)
2588 ssize_t ret;
2589 spin_lock(&mddev->lock);
2590 if (mddev->bitmap == NULL)
2591 ret = sprintf(page, "0\n");
2592 else
2593 ret = sprintf(page, "%lu\n",
2594 mddev->bitmap->behind_writes_used);
2595 spin_unlock(&mddev->lock);
2596 return ret;
2599 static ssize_t
2600 behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
2602 if (mddev->bitmap)
2603 mddev->bitmap->behind_writes_used = 0;
2604 return len;
2607 static struct md_sysfs_entry max_backlog_used =
2608 __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
2609 behind_writes_used_show, behind_writes_used_reset);
2611 static struct attribute *md_bitmap_attrs[] = {
2612 &bitmap_location.attr,
2613 &bitmap_space.attr,
2614 &bitmap_timeout.attr,
2615 &bitmap_backlog.attr,
2616 &bitmap_chunksize.attr,
2617 &bitmap_metadata.attr,
2618 &bitmap_can_clear.attr,
2619 &max_backlog_used.attr,
2620 NULL
2622 struct attribute_group md_bitmap_group = {
2623 .name = "bitmap",
2624 .attrs = md_bitmap_attrs,