Lines Matching full:bitmap
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
9 * - added disk storage for bitmap
10 * - changes to allow various bitmap chunk sizes
33 #include "md-bitmap.h"
35 static inline char *bmname(struct bitmap *bitmap) in bmname() argument
37 return bitmap->mddev ? mdname(bitmap->mddev) : "mdX"; in bmname()
50 static int md_bitmap_checkpage(struct bitmap_counts *bitmap, in md_bitmap_checkpage() argument
52 __releases(bitmap->lock) in md_bitmap_checkpage()
53 __acquires(bitmap->lock) in md_bitmap_checkpage()
57 WARN_ON_ONCE(page >= bitmap->pages); in md_bitmap_checkpage()
58 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */ in md_bitmap_checkpage()
61 if (bitmap->bp[page].map) /* page is already allocated, just return */ in md_bitmap_checkpage()
69 spin_unlock_irq(&bitmap->lock); in md_bitmap_checkpage()
84 spin_lock_irq(&bitmap->lock); in md_bitmap_checkpage()
87 pr_debug("md/bitmap: map page allocation failed, hijacking\n"); in md_bitmap_checkpage()
93 if (!bitmap->bp[page].map) in md_bitmap_checkpage()
94 bitmap->bp[page].hijacked = 1; in md_bitmap_checkpage()
95 } else if (bitmap->bp[page].map || in md_bitmap_checkpage()
96 bitmap->bp[page].hijacked) { in md_bitmap_checkpage()
103 bitmap->bp[page].map = mappage; in md_bitmap_checkpage()
104 bitmap->missing_pages--; in md_bitmap_checkpage()
112 static void md_bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page) in md_bitmap_checkfree() argument
116 if (bitmap->bp[page].count) /* page is still busy */ in md_bitmap_checkfree()
121 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */ in md_bitmap_checkfree()
122 bitmap->bp[page].hijacked = 0; in md_bitmap_checkfree()
123 bitmap->bp[page].map = NULL; in md_bitmap_checkfree()
126 ptr = bitmap->bp[page].map; in md_bitmap_checkfree()
127 bitmap->bp[page].map = NULL; in md_bitmap_checkfree()
128 bitmap->missing_pages++; in md_bitmap_checkfree()
134 * bitmap file handling - read and write the bitmap file and its superblock
141 /* IO operations when bitmap is stored near all superblocks */
224 static int __write_sb_page(struct md_rdev *rdev, struct bitmap *bitmap, in __write_sb_page() argument
228 struct mddev *mddev = bitmap->mddev; in __write_sb_page()
229 struct bitmap_storage *store = &bitmap->storage; in __write_sb_page()
230 unsigned int bitmap_limit = (bitmap->storage.file_pages - pg_index) << in __write_sb_page()
254 /* Bitmap could be anywhere. */ in __write_sb_page()
259 /* DATA BITMAP METADATA */ in __write_sb_page()
262 /* bitmap runs in to metadata */ in __write_sb_page()
266 /* data runs in to bitmap */ in __write_sb_page()
269 /* METADATA BITMAP DATA */ in __write_sb_page()
272 /* bitmap runs in to data */ in __write_sb_page()
280 static void write_sb_page(struct bitmap *bitmap, unsigned long pg_index, in write_sb_page() argument
283 struct mddev *mddev = bitmap->mddev; in write_sb_page()
289 if (__write_sb_page(rdev, bitmap, pg_index, page) < 0) { in write_sb_page()
290 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags); in write_sb_page()
297 static void md_bitmap_file_kick(struct bitmap *bitmap);
300 static void write_file_page(struct bitmap *bitmap, struct page *page, int wait) in write_file_page() argument
305 atomic_inc(&bitmap->pending_writes); in write_file_page()
313 wait_event(bitmap->write_wait, in write_file_page()
314 atomic_read(&bitmap->pending_writes) == 0); in write_file_page()
319 struct bitmap *bitmap = bh->b_private; in end_bitmap_write() local
322 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags); in end_bitmap_write()
323 if (atomic_dec_and_test(&bitmap->pending_writes)) in end_bitmap_write()
324 wake_up(&bitmap->write_wait); in end_bitmap_write()
352 struct bitmap *bitmap, unsigned long count, struct page *page) in read_file_page() argument
360 pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE, in read_file_page()
391 bh->b_private = bitmap; in read_file_page()
392 atomic_inc(&bitmap->pending_writes); in read_file_page()
401 wait_event(bitmap->write_wait, in read_file_page()
402 atomic_read(&bitmap->pending_writes)==0); in read_file_page()
403 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in read_file_page()
407 pr_err("md: bitmap read error: (%dB @ %llu): %d\n", in read_file_page()
414 static void write_file_page(struct bitmap *bitmap, struct page *page, int wait) in write_file_page() argument
418 struct bitmap *bitmap, unsigned long count, struct page *page) in read_file_page() argument
429 * bitmap file superblock operations
435 static void filemap_write_page(struct bitmap *bitmap, unsigned long pg_index, in filemap_write_page() argument
438 struct bitmap_storage *store = &bitmap->storage; in filemap_write_page()
441 if (mddev_is_clustered(bitmap->mddev)) { in filemap_write_page()
442 /* go to node bitmap area starting point */ in filemap_write_page()
447 write_file_page(bitmap, page, wait); in filemap_write_page()
449 write_sb_page(bitmap, pg_index, page, wait); in filemap_write_page()
453 * md_bitmap_wait_writes() should be called before writing any bitmap
457 static void md_bitmap_wait_writes(struct bitmap *bitmap) in md_bitmap_wait_writes() argument
459 if (bitmap->storage.file) in md_bitmap_wait_writes()
460 wait_event(bitmap->write_wait, in md_bitmap_wait_writes()
461 atomic_read(&bitmap->pending_writes)==0); in md_bitmap_wait_writes()
466 * which is safe. The relevant bitmap blocks will in md_bitmap_wait_writes()
470 md_super_wait(bitmap->mddev); in md_bitmap_wait_writes()
475 void md_bitmap_update_sb(struct bitmap *bitmap) in md_bitmap_update_sb() argument
479 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */ in md_bitmap_update_sb()
481 if (bitmap->mddev->bitmap_info.external) in md_bitmap_update_sb()
483 if (!bitmap->storage.sb_page) /* no superblock */ in md_bitmap_update_sb()
485 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_update_sb()
486 sb->events = cpu_to_le64(bitmap->mddev->events); in md_bitmap_update_sb()
487 if (bitmap->mddev->events < bitmap->events_cleared) in md_bitmap_update_sb()
489 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_update_sb()
490 sb->events_cleared = cpu_to_le64(bitmap->events_cleared); in md_bitmap_update_sb()
493 * a bitmap write error occurred but the later writes succeeded. in md_bitmap_update_sb()
495 sb->state = cpu_to_le32(bitmap->flags & ~BIT(BITMAP_WRITE_ERROR)); in md_bitmap_update_sb()
497 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ); in md_bitmap_update_sb()
498 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_update_sb()
500 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_update_sb()
501 sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize); in md_bitmap_update_sb()
502 sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes); in md_bitmap_update_sb()
503 sb->sectors_reserved = cpu_to_le32(bitmap->mddev-> in md_bitmap_update_sb()
507 if (bitmap->storage.file) in md_bitmap_update_sb()
508 write_file_page(bitmap, bitmap->storage.sb_page, 1); in md_bitmap_update_sb()
510 write_sb_page(bitmap, bitmap->storage.sb_index, in md_bitmap_update_sb()
511 bitmap->storage.sb_page, 1); in md_bitmap_update_sb()
515 /* print out the bitmap file superblock */
516 void md_bitmap_print_sb(struct bitmap *bitmap) in md_bitmap_print_sb() argument
520 if (!bitmap || !bitmap->storage.sb_page) in md_bitmap_print_sb()
522 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_print_sb()
523 pr_debug("%s: bitmap file superblock:\n", bmname(bitmap)); in md_bitmap_print_sb()
546 * @bitmap
549 * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
550 * This function verifies 'bitmap_info' and populates the on-disk bitmap
555 static int md_bitmap_new_disk_sb(struct bitmap *bitmap) in md_bitmap_new_disk_sb() argument
560 bitmap->storage.sb_page = alloc_page(GFP_KERNEL | __GFP_ZERO); in md_bitmap_new_disk_sb()
561 if (bitmap->storage.sb_page == NULL) in md_bitmap_new_disk_sb()
563 bitmap->storage.sb_index = 0; in md_bitmap_new_disk_sb()
565 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_new_disk_sb()
570 chunksize = bitmap->mddev->bitmap_info.chunksize; in md_bitmap_new_disk_sb()
574 pr_warn("bitmap chunksize not a power of 2\n"); in md_bitmap_new_disk_sb()
579 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep; in md_bitmap_new_disk_sb()
585 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; in md_bitmap_new_disk_sb()
591 write_behind = bitmap->mddev->bitmap_info.max_write_behind; in md_bitmap_new_disk_sb()
595 bitmap->mddev->bitmap_info.max_write_behind = write_behind; in md_bitmap_new_disk_sb()
597 /* keep the array size field of the bitmap superblock up to date */ in md_bitmap_new_disk_sb()
598 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_new_disk_sb()
600 memcpy(sb->uuid, bitmap->mddev->uuid, 16); in md_bitmap_new_disk_sb()
602 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_new_disk_sb()
603 sb->state = cpu_to_le32(bitmap->flags); in md_bitmap_new_disk_sb()
604 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_new_disk_sb()
605 sb->events_cleared = cpu_to_le64(bitmap->mddev->events); in md_bitmap_new_disk_sb()
606 bitmap->mddev->bitmap_info.nodes = 0; in md_bitmap_new_disk_sb()
613 /* read the superblock from the bitmap file and initialize some bitmap fields */
614 static int md_bitmap_read_sb(struct bitmap *bitmap) in md_bitmap_read_sb() argument
626 if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) { in md_bitmap_read_sb()
630 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_read_sb()
638 bitmap->storage.sb_page = sb_page; in md_bitmap_read_sb()
642 if (bitmap->cluster_slot >= 0) { in md_bitmap_read_sb()
643 sector_t bm_blocks = bitmap->mddev->resync_max_sectors; in md_bitmap_read_sb()
646 (bitmap->mddev->bitmap_info.chunksize >> 9)); in md_bitmap_read_sb()
651 offset = bitmap->cluster_slot * (bm_blocks << 3); in md_bitmap_read_sb()
653 bitmap->cluster_slot, offset); in md_bitmap_read_sb()
656 if (bitmap->storage.file) { in md_bitmap_read_sb()
657 loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host); in md_bitmap_read_sb()
660 err = read_file_page(bitmap->storage.file, 0, in md_bitmap_read_sb()
661 bitmap, bytes, sb_page); in md_bitmap_read_sb()
663 err = read_sb_page(bitmap->mddev, offset, sb_page, 0, in md_bitmap_read_sb()
677 /* verify that the bitmap-specific fields are valid */ in md_bitmap_read_sb()
684 reason = "bitmap chunksize too small"; in md_bitmap_read_sb()
686 reason = "bitmap chunksize not a power of 2"; in md_bitmap_read_sb()
692 pr_warn("%s: invalid bitmap file superblock: %s\n", in md_bitmap_read_sb()
693 bmname(bitmap), reason); in md_bitmap_read_sb()
698 * Setup nodes/clustername only if bitmap version is in md_bitmap_read_sb()
703 strscpy(bitmap->mddev->bitmap_info.cluster_name, in md_bitmap_read_sb()
707 /* keep the array size field of the bitmap superblock up to date */ in md_bitmap_read_sb()
708 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_read_sb()
710 if (bitmap->mddev->persistent) { in md_bitmap_read_sb()
713 * bitmap's UUID and event counter to the mddev's in md_bitmap_read_sb()
715 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) { in md_bitmap_read_sb()
716 pr_warn("%s: bitmap superblock UUID mismatch\n", in md_bitmap_read_sb()
717 bmname(bitmap)); in md_bitmap_read_sb()
721 if (!nodes && (events < bitmap->mddev->events)) { in md_bitmap_read_sb()
722 pr_warn("%s: bitmap file is out of date (%llu < %llu) -- forcing full recovery\n", in md_bitmap_read_sb()
723 bmname(bitmap), events, in md_bitmap_read_sb()
724 (unsigned long long) bitmap->mddev->events); in md_bitmap_read_sb()
725 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_read_sb()
730 bitmap->flags |= le32_to_cpu(sb->state); in md_bitmap_read_sb()
732 set_bit(BITMAP_HOSTENDIAN, &bitmap->flags); in md_bitmap_read_sb()
733 bitmap->events_cleared = le64_to_cpu(sb->events_cleared); in md_bitmap_read_sb()
738 if (err == 0 && nodes && (bitmap->cluster_slot < 0)) { in md_bitmap_read_sb()
740 bitmap->mddev->bitmap_info.chunksize = chunksize; in md_bitmap_read_sb()
741 err = md_setup_cluster(bitmap->mddev, nodes); in md_bitmap_read_sb()
744 bmname(bitmap), err); in md_bitmap_read_sb()
747 bitmap->cluster_slot = md_cluster_ops->slot_number(bitmap->mddev); in md_bitmap_read_sb()
753 if (test_bit(BITMAP_STALE, &bitmap->flags)) in md_bitmap_read_sb()
754 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_read_sb()
755 bitmap->mddev->bitmap_info.chunksize = chunksize; in md_bitmap_read_sb()
756 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; in md_bitmap_read_sb()
757 bitmap->mddev->bitmap_info.max_write_behind = write_behind; in md_bitmap_read_sb()
758 bitmap->mddev->bitmap_info.nodes = nodes; in md_bitmap_read_sb()
759 if (bitmap->mddev->bitmap_info.space == 0 || in md_bitmap_read_sb()
760 bitmap->mddev->bitmap_info.space > sectors_reserved) in md_bitmap_read_sb()
761 bitmap->mddev->bitmap_info.space = sectors_reserved; in md_bitmap_read_sb()
763 md_bitmap_print_sb(bitmap); in md_bitmap_read_sb()
764 if (bitmap->cluster_slot < 0) in md_bitmap_read_sb()
765 md_cluster_stop(bitmap->mddev); in md_bitmap_read_sb()
771 * general bitmap file operations
775 * on-disk bitmap:
777 * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
889 * bitmap_file_kick - if an error occurs while manipulating the bitmap file
893 static void md_bitmap_file_kick(struct bitmap *bitmap) in md_bitmap_file_kick() argument
895 if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) { in md_bitmap_file_kick()
896 md_bitmap_update_sb(bitmap); in md_bitmap_file_kick()
898 if (bitmap->storage.file) { in md_bitmap_file_kick()
899 pr_warn("%s: kicking failed bitmap file %pD4 from array!\n", in md_bitmap_file_kick()
900 bmname(bitmap), bitmap->storage.file); in md_bitmap_file_kick()
903 pr_warn("%s: disabling internal bitmap due to errors\n", in md_bitmap_file_kick()
904 bmname(bitmap)); in md_bitmap_file_kick()
915 static inline void set_page_attr(struct bitmap *bitmap, int pnum, in set_page_attr() argument
918 set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in set_page_attr()
921 static inline void clear_page_attr(struct bitmap *bitmap, int pnum, in clear_page_attr() argument
924 clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in clear_page_attr()
927 static inline int test_page_attr(struct bitmap *bitmap, int pnum, in test_page_attr() argument
930 return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in test_page_attr()
933 static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum, in test_and_clear_page_attr() argument
937 bitmap->storage.filemap_attr); in test_and_clear_page_attr()
941 * to set (and eventually sync) a particular bit in the bitmap file
946 static void md_bitmap_file_set_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_set_bit() argument
951 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_set_bit()
952 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_file_set_bit()
957 if (mddev_is_clustered(bitmap->mddev)) in md_bitmap_file_set_bit()
958 node_offset = bitmap->cluster_slot * store->file_pages; in md_bitmap_file_set_bit()
960 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_set_bit()
963 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_set_bit()
967 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_set_bit()
974 set_page_attr(bitmap, index - node_offset, BITMAP_PAGE_DIRTY); in md_bitmap_file_set_bit()
977 static void md_bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_clear_bit() argument
982 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_clear_bit()
983 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_file_clear_bit()
988 if (mddev_is_clustered(bitmap->mddev)) in md_bitmap_file_clear_bit()
989 node_offset = bitmap->cluster_slot * store->file_pages; in md_bitmap_file_clear_bit()
991 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_clear_bit()
994 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_clear_bit()
996 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_clear_bit()
1001 if (!test_page_attr(bitmap, index - node_offset, BITMAP_PAGE_NEEDWRITE)) { in md_bitmap_file_clear_bit()
1002 set_page_attr(bitmap, index - node_offset, BITMAP_PAGE_PENDING); in md_bitmap_file_clear_bit()
1003 bitmap->allclean = 0; in md_bitmap_file_clear_bit()
1007 static int md_bitmap_file_test_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_test_bit() argument
1012 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_test_bit()
1015 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_test_bit()
1018 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_test_bit()
1020 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_test_bit()
1030 * sync the dirty pages of the bitmap file to disk */
1031 void md_bitmap_unplug(struct bitmap *bitmap) in md_bitmap_unplug() argument
1037 if (!md_bitmap_enabled(bitmap)) in md_bitmap_unplug()
1042 for (i = 0; i < bitmap->storage.file_pages; i++) { in md_bitmap_unplug()
1043 dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY); in md_bitmap_unplug()
1044 need_write = test_and_clear_page_attr(bitmap, i, in md_bitmap_unplug()
1048 md_bitmap_wait_writes(bitmap); in md_bitmap_unplug()
1049 if (bitmap->mddev->queue) in md_bitmap_unplug()
1050 blk_add_trace_msg(bitmap->mddev->queue, in md_bitmap_unplug()
1053 clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING); in md_bitmap_unplug()
1054 filemap_write_page(bitmap, i, false); in md_bitmap_unplug()
1059 md_bitmap_wait_writes(bitmap); in md_bitmap_unplug()
1061 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in md_bitmap_unplug()
1062 md_bitmap_file_kick(bitmap); in md_bitmap_unplug()
1068 struct bitmap *bitmap; member
1077 md_bitmap_unplug(unplug_work->bitmap); in md_bitmap_unplug_fn()
1081 void md_bitmap_unplug_async(struct bitmap *bitmap) in md_bitmap_unplug_async() argument
1087 unplug_work.bitmap = bitmap; in md_bitmap_unplug_async()
1096 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
1099 * Initialize the in-memory bitmap from the on-disk bitmap and set up the memory
1100 * mapping of the bitmap file.
1102 * Special case: If there's no bitmap file, or if the bitmap file had been
1107 * This is used when reading an out-of-date bitmap.
1109 static int md_bitmap_init_from_disk(struct bitmap *bitmap, sector_t start) in md_bitmap_init_from_disk() argument
1111 bool outofdate = test_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_init_from_disk()
1112 struct mddev *mddev = bitmap->mddev; in md_bitmap_init_from_disk()
1113 unsigned long chunks = bitmap->counts.chunks; in md_bitmap_init_from_disk()
1114 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_init_from_disk()
1122 /* No permanent bitmap - fill with '1s'. */ in md_bitmap_init_from_disk()
1127 int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift) in md_bitmap_init_from_disk()
1129 md_bitmap_set_memory_bits(bitmap, in md_bitmap_init_from_disk()
1130 (sector_t)i << bitmap->counts.chunkshift, in md_bitmap_init_from_disk()
1137 pr_warn("%s: bitmap file too short %lu < %lu\n", in md_bitmap_init_from_disk()
1138 bmname(bitmap), in md_bitmap_init_from_disk()
1146 node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE)); in md_bitmap_init_from_disk()
1159 ret = read_file_page(file, i, bitmap, count, page); in md_bitmap_init_from_disk()
1168 pr_warn("%s: bitmap file is out of date, doing full recovery\n", in md_bitmap_init_from_disk()
1169 bmname(bitmap)); in md_bitmap_init_from_disk()
1180 * If the bitmap is out of date, dirty the whole page in md_bitmap_init_from_disk()
1187 filemap_write_page(bitmap, i, true); in md_bitmap_init_from_disk()
1188 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) { in md_bitmap_init_from_disk()
1196 struct page *page = filemap_get_page(&bitmap->storage, i); in md_bitmap_init_from_disk()
1197 unsigned long bit = file_page_offset(&bitmap->storage, i); in md_bitmap_init_from_disk()
1202 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_init_from_disk()
1210 int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift in md_bitmap_init_from_disk()
1212 md_bitmap_set_memory_bits(bitmap, in md_bitmap_init_from_disk()
1213 (sector_t)i << bitmap->counts.chunkshift, in md_bitmap_init_from_disk()
1219 pr_debug("%s: bitmap initialized from disk: read %lu pages, set %lu of %lu bits\n", in md_bitmap_init_from_disk()
1220 bmname(bitmap), store->file_pages, in md_bitmap_init_from_disk()
1226 pr_warn("%s: bitmap initialisation failed: %d\n", in md_bitmap_init_from_disk()
1227 bmname(bitmap), ret); in md_bitmap_init_from_disk()
1231 void md_bitmap_write_all(struct bitmap *bitmap) in md_bitmap_write_all() argument
1233 /* We don't actually write all bitmap blocks here, in md_bitmap_write_all()
1238 if (!bitmap || !bitmap->storage.filemap) in md_bitmap_write_all()
1240 if (bitmap->storage.file) in md_bitmap_write_all()
1244 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_write_all()
1245 set_page_attr(bitmap, i, in md_bitmap_write_all()
1247 bitmap->allclean = 0; in md_bitmap_write_all()
1250 static void md_bitmap_count_page(struct bitmap_counts *bitmap, in md_bitmap_count_page() argument
1253 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_count_page()
1255 bitmap->bp[page].count += inc; in md_bitmap_count_page()
1256 md_bitmap_checkfree(bitmap, page); in md_bitmap_count_page()
1259 static void md_bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset) in md_bitmap_set_pending() argument
1261 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_set_pending()
1263 struct bitmap_page *bp = &bitmap->bp[page]; in md_bitmap_set_pending()
1269 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
1292 * bitmap daemon -- periodically wakes up to clean bits and flush pages
1297 struct bitmap *bitmap; in md_bitmap_daemon_work() local
1307 bitmap = mddev->bitmap; in md_bitmap_daemon_work()
1308 if (bitmap == NULL) { in md_bitmap_daemon_work()
1312 if (time_before(jiffies, bitmap->daemon_lastrun in md_bitmap_daemon_work()
1316 bitmap->daemon_lastrun = jiffies; in md_bitmap_daemon_work()
1317 if (bitmap->allclean) { in md_bitmap_daemon_work()
1321 bitmap->allclean = 1; in md_bitmap_daemon_work()
1323 if (bitmap->mddev->queue) in md_bitmap_daemon_work()
1324 blk_add_trace_msg(bitmap->mddev->queue, in md_bitmap_daemon_work()
1331 for (j = 0; j < bitmap->storage.file_pages; j++) in md_bitmap_daemon_work()
1332 if (test_and_clear_page_attr(bitmap, j, in md_bitmap_daemon_work()
1334 set_page_attr(bitmap, j, in md_bitmap_daemon_work()
1337 if (bitmap->need_sync && in md_bitmap_daemon_work()
1342 bitmap->need_sync = 0; in md_bitmap_daemon_work()
1343 if (bitmap->storage.filemap) { in md_bitmap_daemon_work()
1344 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_daemon_work()
1346 cpu_to_le64(bitmap->events_cleared); in md_bitmap_daemon_work()
1348 set_page_attr(bitmap, 0, in md_bitmap_daemon_work()
1352 /* Now look at the bitmap counters and if any are '2' or '1', in md_bitmap_daemon_work()
1355 counts = &bitmap->counts; in md_bitmap_daemon_work()
1376 if (*bmc == 1 && !bitmap->need_sync) { in md_bitmap_daemon_work()
1380 md_bitmap_file_clear_bit(bitmap, block); in md_bitmap_daemon_work()
1384 bitmap->allclean = 0; in md_bitmap_daemon_work()
1389 md_bitmap_wait_writes(bitmap); in md_bitmap_daemon_work()
1399 j < bitmap->storage.file_pages in md_bitmap_daemon_work()
1400 && !test_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_daemon_work()
1402 if (test_page_attr(bitmap, j, in md_bitmap_daemon_work()
1406 if (bitmap->storage.filemap && in md_bitmap_daemon_work()
1407 test_and_clear_page_attr(bitmap, j, in md_bitmap_daemon_work()
1409 filemap_write_page(bitmap, j, false); in md_bitmap_daemon_work()
1413 if (bitmap->allclean == 0) in md_bitmap_daemon_work()
1418 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap, in md_bitmap_get_counter() argument
1421 __releases(bitmap->lock) in md_bitmap_get_counter()
1422 __acquires(bitmap->lock) in md_bitmap_get_counter()
1428 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_get_counter()
1431 sector_t csize = ((sector_t)1) << bitmap->chunkshift; in md_bitmap_get_counter()
1434 if (page >= bitmap->pages) { in md_bitmap_get_counter()
1443 err = md_bitmap_checkpage(bitmap, page, create, 0); in md_bitmap_get_counter()
1445 if (bitmap->bp[page].hijacked || in md_bitmap_get_counter()
1446 bitmap->bp[page].map == NULL) in md_bitmap_get_counter()
1447 csize = ((sector_t)1) << (bitmap->chunkshift + in md_bitmap_get_counter()
1457 if (bitmap->bp[page].hijacked) { /* hijacked pointer */ in md_bitmap_get_counter()
1462 &bitmap->bp[page].map)[hi]; in md_bitmap_get_counter()
1465 &(bitmap->bp[page].map[pageoff]); in md_bitmap_get_counter()
1468 int md_bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind) in md_bitmap_startwrite() argument
1470 if (!bitmap) in md_bitmap_startwrite()
1475 atomic_inc(&bitmap->behind_writes); in md_bitmap_startwrite()
1476 bw = atomic_read(&bitmap->behind_writes); in md_bitmap_startwrite()
1477 if (bw > bitmap->behind_writes_used) in md_bitmap_startwrite()
1478 bitmap->behind_writes_used = bw; in md_bitmap_startwrite()
1481 bw, bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_startwrite()
1488 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1489 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 1); in md_bitmap_startwrite()
1491 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1501 prepare_to_wait(&bitmap->overflow_wait, &__wait, in md_bitmap_startwrite()
1503 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1505 finish_wait(&bitmap->overflow_wait, &__wait); in md_bitmap_startwrite()
1511 md_bitmap_file_set_bit(bitmap, offset); in md_bitmap_startwrite()
1512 md_bitmap_count_page(&bitmap->counts, offset, 1); in md_bitmap_startwrite()
1520 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1532 void md_bitmap_endwrite(struct bitmap *bitmap, sector_t offset, in md_bitmap_endwrite() argument
1535 if (!bitmap) in md_bitmap_endwrite()
1538 if (atomic_dec_and_test(&bitmap->behind_writes)) in md_bitmap_endwrite()
1539 wake_up(&bitmap->behind_wait); in md_bitmap_endwrite()
1541 atomic_read(&bitmap->behind_writes), in md_bitmap_endwrite()
1542 bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_endwrite()
1550 spin_lock_irqsave(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1551 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 0); in md_bitmap_endwrite()
1553 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1557 if (success && !bitmap->mddev->degraded && in md_bitmap_endwrite()
1558 bitmap->events_cleared < bitmap->mddev->events) { in md_bitmap_endwrite()
1559 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_endwrite()
1560 bitmap->need_sync = 1; in md_bitmap_endwrite()
1561 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear); in md_bitmap_endwrite()
1568 wake_up(&bitmap->overflow_wait); in md_bitmap_endwrite()
1572 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_endwrite()
1573 bitmap->allclean = 0; in md_bitmap_endwrite()
1575 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1585 static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, in __bitmap_start_sync() argument
1590 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */ in __bitmap_start_sync()
1592 return 1; /* always resync if no bitmap */ in __bitmap_start_sync()
1594 spin_lock_irq(&bitmap->counts.lock); in __bitmap_start_sync()
1595 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); in __bitmap_start_sync()
1609 spin_unlock_irq(&bitmap->counts.lock); in __bitmap_start_sync()
1613 int md_bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, in md_bitmap_start_sync() argument
1628 rv |= __bitmap_start_sync(bitmap, offset, in md_bitmap_start_sync()
1637 void md_bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted) in md_bitmap_end_sync() argument
1642 if (bitmap == NULL) { in md_bitmap_end_sync()
1646 spin_lock_irqsave(&bitmap->counts.lock, flags); in md_bitmap_end_sync()
1647 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); in md_bitmap_end_sync()
1658 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_end_sync()
1659 bitmap->allclean = 0; in md_bitmap_end_sync()
1664 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_end_sync()
1668 void md_bitmap_close_sync(struct bitmap *bitmap) in md_bitmap_close_sync() argument
1670 /* Sync has finished, and any bitmap chunks that weren't synced in md_bitmap_close_sync()
1676 if (!bitmap) in md_bitmap_close_sync()
1678 while (sector < bitmap->mddev->resync_max_sectors) { in md_bitmap_close_sync()
1679 md_bitmap_end_sync(bitmap, sector, &blocks, 0); in md_bitmap_close_sync()
1685 void md_bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector, bool force) in md_bitmap_cond_end_sync() argument
1690 if (!bitmap) in md_bitmap_cond_end_sync()
1693 bitmap->last_end_sync = jiffies; in md_bitmap_cond_end_sync()
1696 if (!force && time_before(jiffies, (bitmap->last_end_sync in md_bitmap_cond_end_sync()
1697 + bitmap->mddev->bitmap_info.daemon_sleep))) in md_bitmap_cond_end_sync()
1699 wait_event(bitmap->mddev->recovery_wait, in md_bitmap_cond_end_sync()
1700 atomic_read(&bitmap->mddev->recovery_active) == 0); in md_bitmap_cond_end_sync()
1702 bitmap->mddev->curr_resync_completed = sector; in md_bitmap_cond_end_sync()
1703 set_bit(MD_SB_CHANGE_CLEAN, &bitmap->mddev->sb_flags); in md_bitmap_cond_end_sync()
1704 sector &= ~((1ULL << bitmap->counts.chunkshift) - 1); in md_bitmap_cond_end_sync()
1706 while (s < sector && s < bitmap->mddev->resync_max_sectors) { in md_bitmap_cond_end_sync()
1707 md_bitmap_end_sync(bitmap, s, &blocks, 0); in md_bitmap_cond_end_sync()
1710 bitmap->last_end_sync = jiffies; in md_bitmap_cond_end_sync()
1711 sysfs_notify_dirent_safe(bitmap->mddev->sysfs_completed); in md_bitmap_cond_end_sync()
1719 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_sync_with_cluster() local
1723 md_bitmap_end_sync(bitmap, sector, &blocks, 0); in md_bitmap_sync_with_cluster()
1729 md_bitmap_start_sync(bitmap, sector, &blocks, 0); in md_bitmap_sync_with_cluster()
1736 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed) in md_bitmap_set_memory_bits() argument
1745 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1746 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &secs, 1); in md_bitmap_set_memory_bits()
1748 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1753 md_bitmap_count_page(&bitmap->counts, offset, 1); in md_bitmap_set_memory_bits()
1754 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_set_memory_bits()
1755 bitmap->allclean = 0; in md_bitmap_set_memory_bits()
1759 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1762 /* dirty the memory and file bits for bitmap chunks "s" to "e" */
1763 void md_bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e) in md_bitmap_dirty_bits() argument
1768 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift; in md_bitmap_dirty_bits()
1769 md_bitmap_set_memory_bits(bitmap, sec, 1); in md_bitmap_dirty_bits()
1770 md_bitmap_file_set_bit(bitmap, sec); in md_bitmap_dirty_bits()
1771 if (sec < bitmap->mddev->recovery_cp) in md_bitmap_dirty_bits()
1776 bitmap->mddev->recovery_cp = sec; in md_bitmap_dirty_bits()
1785 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_flush() local
1788 if (!bitmap) /* there was no bitmap */ in md_bitmap_flush()
1795 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1797 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1799 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1803 md_bitmap_update_sb(bitmap); in md_bitmap_flush()
1809 void md_bitmap_free(struct bitmap *bitmap) in md_bitmap_free() argument
1814 if (!bitmap) /* there was no bitmap */ in md_bitmap_free()
1817 if (bitmap->sysfs_can_clear) in md_bitmap_free()
1818 sysfs_put(bitmap->sysfs_can_clear); in md_bitmap_free()
1820 if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info && in md_bitmap_free()
1821 bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev)) in md_bitmap_free()
1822 md_cluster_stop(bitmap->mddev); in md_bitmap_free()
1825 wait_event(bitmap->write_wait, in md_bitmap_free()
1826 atomic_read(&bitmap->pending_writes) == 0); in md_bitmap_free()
1828 /* release the bitmap file */ in md_bitmap_free()
1829 md_bitmap_file_unmap(&bitmap->storage); in md_bitmap_free()
1831 bp = bitmap->counts.bp; in md_bitmap_free()
1832 pages = bitmap->counts.pages; in md_bitmap_free()
1841 kfree(bitmap); in md_bitmap_free()
1847 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_wait_behind_writes() local
1850 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { in md_bitmap_wait_behind_writes()
1854 wait_event(bitmap->behind_wait, in md_bitmap_wait_behind_writes()
1855 atomic_read(&bitmap->behind_writes) == 0); in md_bitmap_wait_behind_writes()
1861 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_destroy() local
1863 if (!bitmap) /* there was no bitmap */ in md_bitmap_destroy()
1872 mddev->bitmap = NULL; /* disconnect from the md device */ in md_bitmap_destroy()
1877 md_bitmap_free(bitmap); in md_bitmap_destroy()
1881 * initialize the bitmap structure
1883 * once mddev->bitmap is set
1885 struct bitmap *md_bitmap_create(struct mddev *mddev, int slot) in md_bitmap_create()
1887 struct bitmap *bitmap; in md_bitmap_create() local
1898 pr_notice("md/raid:%s: array with journal cannot have bitmap\n", in md_bitmap_create()
1903 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); in md_bitmap_create()
1904 if (!bitmap) in md_bitmap_create()
1907 spin_lock_init(&bitmap->counts.lock); in md_bitmap_create()
1908 atomic_set(&bitmap->pending_writes, 0); in md_bitmap_create()
1909 init_waitqueue_head(&bitmap->write_wait); in md_bitmap_create()
1910 init_waitqueue_head(&bitmap->overflow_wait); in md_bitmap_create()
1911 init_waitqueue_head(&bitmap->behind_wait); in md_bitmap_create()
1913 bitmap->mddev = mddev; in md_bitmap_create()
1914 bitmap->cluster_slot = slot; in md_bitmap_create()
1917 bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap"); in md_bitmap_create()
1919 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear"); in md_bitmap_create()
1922 bitmap->sysfs_can_clear = NULL; in md_bitmap_create()
1924 bitmap->storage.file = file; in md_bitmap_create()
1933 /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */ in md_bitmap_create()
1937 * instructing us to create a new on-disk bitmap instance. in md_bitmap_create()
1940 err = md_bitmap_new_disk_sb(bitmap); in md_bitmap_create()
1942 err = md_bitmap_read_sb(bitmap); in md_bitmap_create()
1954 bitmap->daemon_lastrun = jiffies; in md_bitmap_create()
1955 err = md_bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1); in md_bitmap_create()
1959 pr_debug("created bitmap (%lu pages) for device %s\n", in md_bitmap_create()
1960 bitmap->counts.pages, bmname(bitmap)); in md_bitmap_create()
1962 err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0; in md_bitmap_create()
1966 return bitmap; in md_bitmap_create()
1968 md_bitmap_free(bitmap); in md_bitmap_create()
1977 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_load() local
1980 if (!bitmap) in md_bitmap_load()
1989 /* Clear out old bitmap info first: Either there is none, or we in md_bitmap_load()
1996 md_bitmap_start_sync(bitmap, sector, &blocks, 0); in md_bitmap_load()
1999 md_bitmap_close_sync(bitmap); in md_bitmap_load()
2002 || bitmap->events_cleared == mddev->events) in md_bitmap_load()
2008 err = md_bitmap_init_from_disk(bitmap, start); in md_bitmap_load()
2013 clear_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_load()
2016 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery); in md_bitmap_load()
2021 md_bitmap_update_sb(bitmap); in md_bitmap_load()
2023 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in md_bitmap_load()
2030 /* caller need to free returned bitmap with md_bitmap_free() */
2031 struct bitmap *get_bitmap_from_slot(struct mddev *mddev, int slot) in get_bitmap_from_slot()
2034 struct bitmap *bitmap; in get_bitmap_from_slot() local
2036 bitmap = md_bitmap_create(mddev, slot); in get_bitmap_from_slot()
2037 if (IS_ERR(bitmap)) { in get_bitmap_from_slot()
2038 rv = PTR_ERR(bitmap); in get_bitmap_from_slot()
2042 rv = md_bitmap_init_from_disk(bitmap, 0); in get_bitmap_from_slot()
2044 md_bitmap_free(bitmap); in get_bitmap_from_slot()
2048 return bitmap; in get_bitmap_from_slot()
2052 /* Loads the bitmap associated with slot and copies the resync information
2053 * to our bitmap
2061 struct bitmap *bitmap; in md_bitmap_copy_from_slot() local
2063 bitmap = get_bitmap_from_slot(mddev, slot); in md_bitmap_copy_from_slot()
2064 if (IS_ERR(bitmap)) { in md_bitmap_copy_from_slot()
2065 pr_err("%s can't get bitmap from slot %d\n", __func__, slot); in md_bitmap_copy_from_slot()
2069 counts = &bitmap->counts; in md_bitmap_copy_from_slot()
2072 if (md_bitmap_file_test_bit(bitmap, block)) { in md_bitmap_copy_from_slot()
2076 md_bitmap_file_clear_bit(bitmap, block); in md_bitmap_copy_from_slot()
2077 md_bitmap_set_memory_bits(mddev->bitmap, block, 1); in md_bitmap_copy_from_slot()
2078 md_bitmap_file_set_bit(mddev->bitmap, block); in md_bitmap_copy_from_slot()
2083 md_bitmap_update_sb(bitmap); in md_bitmap_copy_from_slot()
2086 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_copy_from_slot()
2087 if (test_page_attr(bitmap, i, BITMAP_PAGE_PENDING)) in md_bitmap_copy_from_slot()
2088 set_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE); in md_bitmap_copy_from_slot()
2089 md_bitmap_unplug(bitmap); in md_bitmap_copy_from_slot()
2091 md_bitmap_unplug(mddev->bitmap); in md_bitmap_copy_from_slot()
2094 md_bitmap_free(bitmap); in md_bitmap_copy_from_slot()
2101 void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap) in md_bitmap_status() argument
2106 if (!bitmap) in md_bitmap_status()
2109 counts = &bitmap->counts; in md_bitmap_status()
2111 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10; in md_bitmap_status()
2112 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " in md_bitmap_status()
2118 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize, in md_bitmap_status()
2120 if (bitmap->storage.file) { in md_bitmap_status()
2122 seq_file_path(seq, bitmap->storage.file, " \t\n"); in md_bitmap_status()
2128 int md_bitmap_resize(struct bitmap *bitmap, sector_t blocks, in md_bitmap_resize() argument
2133 * Then quiesce, copy bits, replace bitmap, and re-start in md_bitmap_resize()
2135 * This function is called both to set up the initial bitmap in md_bitmap_resize()
2136 * and to resize the bitmap while the array is active. in md_bitmap_resize()
2151 if (bitmap->storage.file && !init) { in md_bitmap_resize()
2152 pr_info("md: cannot resize file-based bitmap\n"); in md_bitmap_resize()
2161 long space = bitmap->mddev->bitmap_info.space; in md_bitmap_resize()
2167 bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8); in md_bitmap_resize()
2168 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_resize()
2171 bitmap->mddev->bitmap_info.space = space; in md_bitmap_resize()
2173 chunkshift = bitmap->counts.chunkshift; in md_bitmap_resize()
2180 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_resize()
2189 if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file) in md_bitmap_resize()
2191 !bitmap->mddev->bitmap_info.external, in md_bitmap_resize()
2192 mddev_is_clustered(bitmap->mddev) in md_bitmap_resize()
2193 ? bitmap->cluster_slot : 0); in md_bitmap_resize()
2209 bitmap->mddev->pers->quiesce(bitmap->mddev, 1); in md_bitmap_resize()
2211 store.file = bitmap->storage.file; in md_bitmap_resize()
2212 bitmap->storage.file = NULL; in md_bitmap_resize()
2214 if (store.sb_page && bitmap->storage.sb_page) in md_bitmap_resize()
2216 page_address(bitmap->storage.sb_page), in md_bitmap_resize()
2218 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_resize()
2219 md_bitmap_file_unmap(&bitmap->storage); in md_bitmap_resize()
2220 bitmap->storage = store; in md_bitmap_resize()
2222 old_counts = bitmap->counts; in md_bitmap_resize()
2223 bitmap->counts.bp = new_bp; in md_bitmap_resize()
2224 bitmap->counts.pages = pages; in md_bitmap_resize()
2225 bitmap->counts.missing_pages = pages; in md_bitmap_resize()
2226 bitmap->counts.chunkshift = chunkshift; in md_bitmap_resize()
2227 bitmap->counts.chunks = chunks; in md_bitmap_resize()
2228 bitmap->mddev->bitmap_info.chunksize = 1UL << (chunkshift + in md_bitmap_resize()
2234 /* For cluster raid, need to pre-allocate bitmap */ in md_bitmap_resize()
2235 if (mddev_is_clustered(bitmap->mddev)) { in md_bitmap_resize()
2238 ret = md_bitmap_checkpage(&bitmap->counts, page, 1, 1); in md_bitmap_resize()
2249 bitmap->counts.bp = old_counts.bp; in md_bitmap_resize()
2250 bitmap->counts.pages = old_counts.pages; in md_bitmap_resize()
2251 bitmap->counts.missing_pages = old_counts.pages; in md_bitmap_resize()
2252 bitmap->counts.chunkshift = old_counts.chunkshift; in md_bitmap_resize()
2253 bitmap->counts.chunks = old_counts.chunks; in md_bitmap_resize()
2254 bitmap->mddev->bitmap_info.chunksize = in md_bitmap_resize()
2257 pr_warn("Could not pre-allocate in-memory bitmap for cluster raid\n"); in md_bitmap_resize()
2260 bitmap->counts.bp[page].count += 1; in md_bitmap_resize()
2272 bmc_new = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); in md_bitmap_resize()
2281 md_bitmap_file_set_bit(bitmap, block); in md_bitmap_resize()
2285 md_bitmap_count_page(&bitmap->counts, block, 1); in md_bitmap_resize()
2286 md_bitmap_set_pending(&bitmap->counts, block); in md_bitmap_resize()
2296 if (bitmap->counts.bp != old_counts.bp) { in md_bitmap_resize()
2308 bmc = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); in md_bitmap_resize()
2315 md_bitmap_count_page(&bitmap->counts, block, 1); in md_bitmap_resize()
2316 md_bitmap_set_pending(&bitmap->counts, block); in md_bitmap_resize()
2321 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_resize()
2322 set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY); in md_bitmap_resize()
2324 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_resize()
2327 md_bitmap_unplug(bitmap); in md_bitmap_resize()
2328 bitmap->mddev->pers->quiesce(bitmap->mddev, 0); in md_bitmap_resize()
2369 if (mddev->bitmap || mddev->bitmap_info.file || in location_store()
2371 /* bitmap already configured. Only option is to clear it */ in location_store()
2388 /* No bitmap, OK to set a location */ in location_store()
2415 struct bitmap *bitmap; in location_store() local
2416 bitmap = md_bitmap_create(mddev, -1); in location_store()
2418 if (IS_ERR(bitmap)) in location_store()
2419 rv = PTR_ERR(bitmap); in location_store()
2421 mddev->bitmap = bitmap; in location_store()
2436 /* Ensure new bitmap info is stored in in location_store()
2453 /* 'bitmap/space' is the space available at 'location' for the
2454 * bitmap. This allows the kernel to know when it is safe to
2455 * resize the bitmap to match a resized array.
2476 if (mddev->bitmap && in space_store()
2477 sectors < (mddev->bitmap->storage.bytes + 511) >> 9) in space_store()
2478 return -EFBIG; /* Bitmap is too big for this small space */ in space_store()
2585 md_bitmap_update_sb(mddev->bitmap); in backlog_store()
2603 /* Can only be changed when no bitmap is active */ in chunksize_store()
2606 if (mddev->bitmap) in chunksize_store()
2634 if (mddev->bitmap || in metadata_store()
2655 if (mddev->bitmap) in can_clear_show()
2656 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ? in can_clear_show()
2666 if (mddev->bitmap == NULL) in can_clear_store()
2669 mddev->bitmap->need_sync = 1; in can_clear_store()
2673 mddev->bitmap->need_sync = 0; in can_clear_store()
2687 if (mddev->bitmap == NULL) in behind_writes_used_show()
2691 mddev->bitmap->behind_writes_used); in behind_writes_used_show()
2699 if (mddev->bitmap) in behind_writes_used_reset()
2700 mddev->bitmap->behind_writes_used = 0; in behind_writes_used_reset()
2720 .name = "bitmap",