Lines Matching refs:bitmap

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()
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()
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()
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
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()
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
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()
447 write_file_page(bitmap, page, wait); in filemap_write_page()
449 write_sb_page(bitmap, pg_index, page, wait); in filemap_write_page()
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()
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()
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()
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()
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()
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()
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()
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()
693 bmname(bitmap), reason); in md_bitmap_read_sb()
703 strscpy(bitmap->mddev->bitmap_info.cluster_name, 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()
715 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) { 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()
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()
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()
900 bmname(bitmap), bitmap->storage.file); 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()
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()
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 mddev_add_trace_msg(bitmap->mddev, in md_bitmap_unplug()
1052 clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING); in md_bitmap_unplug()
1053 filemap_write_page(bitmap, i, false); in md_bitmap_unplug()
1058 md_bitmap_wait_writes(bitmap); in md_bitmap_unplug()
1060 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in md_bitmap_unplug()
1061 md_bitmap_file_kick(bitmap); in md_bitmap_unplug()
1067 struct bitmap *bitmap; member
1076 md_bitmap_unplug(unplug_work->bitmap); in md_bitmap_unplug_fn()
1080 void md_bitmap_unplug_async(struct bitmap *bitmap) in md_bitmap_unplug_async() argument
1086 unplug_work.bitmap = bitmap; in md_bitmap_unplug_async()
1094 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
1107 static int md_bitmap_init_from_disk(struct bitmap *bitmap, sector_t start) in md_bitmap_init_from_disk() argument
1109 bool outofdate = test_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_init_from_disk()
1110 struct mddev *mddev = bitmap->mddev; in md_bitmap_init_from_disk()
1111 unsigned long chunks = bitmap->counts.chunks; in md_bitmap_init_from_disk()
1112 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_init_from_disk()
1125 int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift) in md_bitmap_init_from_disk()
1127 md_bitmap_set_memory_bits(bitmap, in md_bitmap_init_from_disk()
1128 (sector_t)i << bitmap->counts.chunkshift, in md_bitmap_init_from_disk()
1136 bmname(bitmap), in md_bitmap_init_from_disk()
1144 node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE)); in md_bitmap_init_from_disk()
1157 ret = read_file_page(file, i, bitmap, count, page); in md_bitmap_init_from_disk()
1167 bmname(bitmap)); in md_bitmap_init_from_disk()
1185 filemap_write_page(bitmap, i, true); in md_bitmap_init_from_disk()
1186 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) { in md_bitmap_init_from_disk()
1194 struct page *page = filemap_get_page(&bitmap->storage, i); in md_bitmap_init_from_disk()
1195 unsigned long bit = file_page_offset(&bitmap->storage, i); in md_bitmap_init_from_disk()
1200 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_init_from_disk()
1208 int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift in md_bitmap_init_from_disk()
1210 md_bitmap_set_memory_bits(bitmap, in md_bitmap_init_from_disk()
1211 (sector_t)i << bitmap->counts.chunkshift, in md_bitmap_init_from_disk()
1218 bmname(bitmap), store->file_pages, in md_bitmap_init_from_disk()
1225 bmname(bitmap), ret); in md_bitmap_init_from_disk()
1229 void md_bitmap_write_all(struct bitmap *bitmap) in md_bitmap_write_all() argument
1236 if (!bitmap || !bitmap->storage.filemap) in md_bitmap_write_all()
1238 if (bitmap->storage.file) in md_bitmap_write_all()
1242 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_write_all()
1243 set_page_attr(bitmap, i, in md_bitmap_write_all()
1245 bitmap->allclean = 0; in md_bitmap_write_all()
1248 static void md_bitmap_count_page(struct bitmap_counts *bitmap, in md_bitmap_count_page() argument
1251 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_count_page()
1253 bitmap->bp[page].count += inc; in md_bitmap_count_page()
1254 md_bitmap_checkfree(bitmap, page); in md_bitmap_count_page()
1257 static void md_bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset) in md_bitmap_set_pending() argument
1259 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_set_pending()
1261 struct bitmap_page *bp = &bitmap->bp[page]; in md_bitmap_set_pending()
1267 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
1295 struct bitmap *bitmap; in md_bitmap_daemon_work() local
1305 bitmap = mddev->bitmap; in md_bitmap_daemon_work()
1306 if (bitmap == NULL) { in md_bitmap_daemon_work()
1310 if (time_before(jiffies, bitmap->daemon_lastrun in md_bitmap_daemon_work()
1314 bitmap->daemon_lastrun = jiffies; in md_bitmap_daemon_work()
1315 if (bitmap->allclean) { in md_bitmap_daemon_work()
1319 bitmap->allclean = 1; in md_bitmap_daemon_work()
1321 mddev_add_trace_msg(bitmap->mddev, "md bitmap_daemon_work"); in md_bitmap_daemon_work()
1327 for (j = 0; j < bitmap->storage.file_pages; j++) in md_bitmap_daemon_work()
1328 if (test_and_clear_page_attr(bitmap, j, in md_bitmap_daemon_work()
1330 set_page_attr(bitmap, j, in md_bitmap_daemon_work()
1333 if (bitmap->need_sync && in md_bitmap_daemon_work()
1338 bitmap->need_sync = 0; in md_bitmap_daemon_work()
1339 if (bitmap->storage.filemap) { in md_bitmap_daemon_work()
1340 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_daemon_work()
1342 cpu_to_le64(bitmap->events_cleared); in md_bitmap_daemon_work()
1344 set_page_attr(bitmap, 0, in md_bitmap_daemon_work()
1351 counts = &bitmap->counts; in md_bitmap_daemon_work()
1372 if (*bmc == 1 && !bitmap->need_sync) { in md_bitmap_daemon_work()
1376 md_bitmap_file_clear_bit(bitmap, block); in md_bitmap_daemon_work()
1380 bitmap->allclean = 0; in md_bitmap_daemon_work()
1385 md_bitmap_wait_writes(bitmap); in md_bitmap_daemon_work()
1395 j < bitmap->storage.file_pages in md_bitmap_daemon_work()
1396 && !test_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_daemon_work()
1398 if (test_page_attr(bitmap, j, in md_bitmap_daemon_work()
1402 if (bitmap->storage.filemap && in md_bitmap_daemon_work()
1403 test_and_clear_page_attr(bitmap, j, in md_bitmap_daemon_work()
1405 filemap_write_page(bitmap, j, false); in md_bitmap_daemon_work()
1409 if (bitmap->allclean == 0) in md_bitmap_daemon_work()
1414 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap, in md_bitmap_get_counter() argument
1417 __releases(bitmap->lock) in md_bitmap_get_counter()
1418 __acquires(bitmap->lock) in md_bitmap_get_counter()
1424 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_get_counter()
1427 sector_t csize = ((sector_t)1) << bitmap->chunkshift; in md_bitmap_get_counter()
1430 if (page >= bitmap->pages) { in md_bitmap_get_counter()
1439 err = md_bitmap_checkpage(bitmap, page, create, 0); in md_bitmap_get_counter()
1441 if (bitmap->bp[page].hijacked || in md_bitmap_get_counter()
1442 bitmap->bp[page].map == NULL) in md_bitmap_get_counter()
1443 csize = ((sector_t)1) << (bitmap->chunkshift + in md_bitmap_get_counter()
1453 if (bitmap->bp[page].hijacked) { /* hijacked pointer */ in md_bitmap_get_counter()
1458 &bitmap->bp[page].map)[hi]; in md_bitmap_get_counter()
1461 &(bitmap->bp[page].map[pageoff]); in md_bitmap_get_counter()
1464 int md_bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind) in md_bitmap_startwrite() argument
1466 if (!bitmap) in md_bitmap_startwrite()
1471 atomic_inc(&bitmap->behind_writes); in md_bitmap_startwrite()
1472 bw = atomic_read(&bitmap->behind_writes); in md_bitmap_startwrite()
1473 if (bw > bitmap->behind_writes_used) in md_bitmap_startwrite()
1474 bitmap->behind_writes_used = bw; in md_bitmap_startwrite()
1477 bw, bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_startwrite()
1484 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1485 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 1); in md_bitmap_startwrite()
1487 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1497 prepare_to_wait(&bitmap->overflow_wait, &__wait, in md_bitmap_startwrite()
1499 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1501 finish_wait(&bitmap->overflow_wait, &__wait); in md_bitmap_startwrite()
1507 md_bitmap_file_set_bit(bitmap, offset); in md_bitmap_startwrite()
1508 md_bitmap_count_page(&bitmap->counts, offset, 1); in md_bitmap_startwrite()
1516 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1528 void md_bitmap_endwrite(struct bitmap *bitmap, sector_t offset, in md_bitmap_endwrite() argument
1531 if (!bitmap) in md_bitmap_endwrite()
1534 if (atomic_dec_and_test(&bitmap->behind_writes)) in md_bitmap_endwrite()
1535 wake_up(&bitmap->behind_wait); in md_bitmap_endwrite()
1537 atomic_read(&bitmap->behind_writes), in md_bitmap_endwrite()
1538 bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_endwrite()
1546 spin_lock_irqsave(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1547 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 0); in md_bitmap_endwrite()
1549 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1553 if (success && !bitmap->mddev->degraded && in md_bitmap_endwrite()
1554 bitmap->events_cleared < bitmap->mddev->events) { in md_bitmap_endwrite()
1555 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_endwrite()
1556 bitmap->need_sync = 1; in md_bitmap_endwrite()
1557 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear); in md_bitmap_endwrite()
1564 wake_up(&bitmap->overflow_wait); in md_bitmap_endwrite()
1568 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_endwrite()
1569 bitmap->allclean = 0; in md_bitmap_endwrite()
1571 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1581 static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, in __bitmap_start_sync() argument
1586 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */ in __bitmap_start_sync()
1590 spin_lock_irq(&bitmap->counts.lock); in __bitmap_start_sync()
1591 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); in __bitmap_start_sync()
1605 spin_unlock_irq(&bitmap->counts.lock); in __bitmap_start_sync()
1609 int md_bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, in md_bitmap_start_sync() argument
1624 rv |= __bitmap_start_sync(bitmap, offset, in md_bitmap_start_sync()
1633 void md_bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted) in md_bitmap_end_sync() argument
1638 if (bitmap == NULL) { in md_bitmap_end_sync()
1642 spin_lock_irqsave(&bitmap->counts.lock, flags); in md_bitmap_end_sync()
1643 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); in md_bitmap_end_sync()
1654 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_end_sync()
1655 bitmap->allclean = 0; in md_bitmap_end_sync()
1660 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_end_sync()
1664 void md_bitmap_close_sync(struct bitmap *bitmap) in md_bitmap_close_sync() argument
1672 if (!bitmap) in md_bitmap_close_sync()
1674 while (sector < bitmap->mddev->resync_max_sectors) { in md_bitmap_close_sync()
1675 md_bitmap_end_sync(bitmap, sector, &blocks, 0); in md_bitmap_close_sync()
1681 void md_bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector, bool force) in md_bitmap_cond_end_sync() argument
1686 if (!bitmap) in md_bitmap_cond_end_sync()
1689 bitmap->last_end_sync = jiffies; in md_bitmap_cond_end_sync()
1692 if (!force && time_before(jiffies, (bitmap->last_end_sync in md_bitmap_cond_end_sync()
1693 + bitmap->mddev->bitmap_info.daemon_sleep))) in md_bitmap_cond_end_sync()
1695 wait_event(bitmap->mddev->recovery_wait, in md_bitmap_cond_end_sync()
1696 atomic_read(&bitmap->mddev->recovery_active) == 0); in md_bitmap_cond_end_sync()
1698 bitmap->mddev->curr_resync_completed = sector; in md_bitmap_cond_end_sync()
1699 set_bit(MD_SB_CHANGE_CLEAN, &bitmap->mddev->sb_flags); in md_bitmap_cond_end_sync()
1700 sector &= ~((1ULL << bitmap->counts.chunkshift) - 1); in md_bitmap_cond_end_sync()
1702 while (s < sector && s < bitmap->mddev->resync_max_sectors) { in md_bitmap_cond_end_sync()
1703 md_bitmap_end_sync(bitmap, s, &blocks, 0); in md_bitmap_cond_end_sync()
1706 bitmap->last_end_sync = jiffies; in md_bitmap_cond_end_sync()
1707 sysfs_notify_dirent_safe(bitmap->mddev->sysfs_completed); in md_bitmap_cond_end_sync()
1715 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_sync_with_cluster() local
1719 md_bitmap_end_sync(bitmap, sector, &blocks, 0); in md_bitmap_sync_with_cluster()
1725 md_bitmap_start_sync(bitmap, sector, &blocks, 0); in md_bitmap_sync_with_cluster()
1732 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed) in md_bitmap_set_memory_bits() argument
1741 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1742 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &secs, 1); in md_bitmap_set_memory_bits()
1744 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1749 md_bitmap_count_page(&bitmap->counts, offset, 1); in md_bitmap_set_memory_bits()
1750 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_set_memory_bits()
1751 bitmap->allclean = 0; in md_bitmap_set_memory_bits()
1755 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1759 void md_bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e) in md_bitmap_dirty_bits() argument
1764 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift; in md_bitmap_dirty_bits()
1765 md_bitmap_set_memory_bits(bitmap, sec, 1); in md_bitmap_dirty_bits()
1766 md_bitmap_file_set_bit(bitmap, sec); in md_bitmap_dirty_bits()
1767 if (sec < bitmap->mddev->recovery_cp) in md_bitmap_dirty_bits()
1772 bitmap->mddev->recovery_cp = sec; in md_bitmap_dirty_bits()
1781 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_flush() local
1784 if (!bitmap) /* there was no bitmap */ in md_bitmap_flush()
1791 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1793 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1795 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1799 md_bitmap_update_sb(bitmap); in md_bitmap_flush()
1805 void md_bitmap_free(struct bitmap *bitmap) in md_bitmap_free() argument
1810 if (!bitmap) /* there was no bitmap */ in md_bitmap_free()
1813 if (bitmap->sysfs_can_clear) in md_bitmap_free()
1814 sysfs_put(bitmap->sysfs_can_clear); in md_bitmap_free()
1816 if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info && in md_bitmap_free()
1817 bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev)) in md_bitmap_free()
1818 md_cluster_stop(bitmap->mddev); in md_bitmap_free()
1821 wait_event(bitmap->write_wait, in md_bitmap_free()
1822 atomic_read(&bitmap->pending_writes) == 0); in md_bitmap_free()
1825 md_bitmap_file_unmap(&bitmap->storage); in md_bitmap_free()
1827 bp = bitmap->counts.bp; in md_bitmap_free()
1828 pages = bitmap->counts.pages; in md_bitmap_free()
1837 kfree(bitmap); in md_bitmap_free()
1843 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_wait_behind_writes() local
1846 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { in md_bitmap_wait_behind_writes()
1850 wait_event(bitmap->behind_wait, in md_bitmap_wait_behind_writes()
1851 atomic_read(&bitmap->behind_writes) == 0); in md_bitmap_wait_behind_writes()
1857 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_destroy() local
1859 if (!bitmap) /* there was no bitmap */ in md_bitmap_destroy()
1868 mddev->bitmap = NULL; /* disconnect from the md device */ in md_bitmap_destroy()
1873 md_bitmap_free(bitmap); in md_bitmap_destroy()
1881 struct bitmap *md_bitmap_create(struct mddev *mddev, int slot) in md_bitmap_create()
1883 struct bitmap *bitmap; in md_bitmap_create() local
1899 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); in md_bitmap_create()
1900 if (!bitmap) in md_bitmap_create()
1903 spin_lock_init(&bitmap->counts.lock); in md_bitmap_create()
1904 atomic_set(&bitmap->pending_writes, 0); in md_bitmap_create()
1905 init_waitqueue_head(&bitmap->write_wait); in md_bitmap_create()
1906 init_waitqueue_head(&bitmap->overflow_wait); in md_bitmap_create()
1907 init_waitqueue_head(&bitmap->behind_wait); in md_bitmap_create()
1909 bitmap->mddev = mddev; in md_bitmap_create()
1910 bitmap->cluster_slot = slot; in md_bitmap_create()
1915 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear"); in md_bitmap_create()
1918 bitmap->sysfs_can_clear = NULL; in md_bitmap_create()
1920 bitmap->storage.file = file; in md_bitmap_create()
1936 err = md_bitmap_new_disk_sb(bitmap); in md_bitmap_create()
1938 err = md_bitmap_read_sb(bitmap); in md_bitmap_create()
1950 bitmap->daemon_lastrun = jiffies; in md_bitmap_create()
1951 err = md_bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1); in md_bitmap_create()
1956 bitmap->counts.pages, bmname(bitmap)); in md_bitmap_create()
1958 err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0; in md_bitmap_create()
1962 return bitmap; in md_bitmap_create()
1964 md_bitmap_free(bitmap); in md_bitmap_create()
1973 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_load() local
1976 if (!bitmap) in md_bitmap_load()
1992 md_bitmap_start_sync(bitmap, sector, &blocks, 0); in md_bitmap_load()
1995 md_bitmap_close_sync(bitmap); in md_bitmap_load()
1998 || bitmap->events_cleared == mddev->events) in md_bitmap_load()
2004 err = md_bitmap_init_from_disk(bitmap, start); in md_bitmap_load()
2009 clear_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_load()
2012 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery); in md_bitmap_load()
2017 md_bitmap_update_sb(bitmap); in md_bitmap_load()
2019 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in md_bitmap_load()
2027 struct bitmap *get_bitmap_from_slot(struct mddev *mddev, int slot) in get_bitmap_from_slot()
2030 struct bitmap *bitmap; in get_bitmap_from_slot() local
2032 bitmap = md_bitmap_create(mddev, slot); in get_bitmap_from_slot()
2033 if (IS_ERR(bitmap)) { in get_bitmap_from_slot()
2034 rv = PTR_ERR(bitmap); in get_bitmap_from_slot()
2038 rv = md_bitmap_init_from_disk(bitmap, 0); in get_bitmap_from_slot()
2040 md_bitmap_free(bitmap); in get_bitmap_from_slot()
2044 return bitmap; in get_bitmap_from_slot()
2057 struct bitmap *bitmap; in md_bitmap_copy_from_slot() local
2059 bitmap = get_bitmap_from_slot(mddev, slot); in md_bitmap_copy_from_slot()
2060 if (IS_ERR(bitmap)) { in md_bitmap_copy_from_slot()
2065 counts = &bitmap->counts; in md_bitmap_copy_from_slot()
2068 if (md_bitmap_file_test_bit(bitmap, block)) { in md_bitmap_copy_from_slot()
2072 md_bitmap_file_clear_bit(bitmap, block); in md_bitmap_copy_from_slot()
2073 md_bitmap_set_memory_bits(mddev->bitmap, block, 1); in md_bitmap_copy_from_slot()
2074 md_bitmap_file_set_bit(mddev->bitmap, block); in md_bitmap_copy_from_slot()
2079 md_bitmap_update_sb(bitmap); in md_bitmap_copy_from_slot()
2082 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_copy_from_slot()
2083 if (test_page_attr(bitmap, i, BITMAP_PAGE_PENDING)) in md_bitmap_copy_from_slot()
2084 set_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE); in md_bitmap_copy_from_slot()
2085 md_bitmap_unplug(bitmap); in md_bitmap_copy_from_slot()
2087 md_bitmap_unplug(mddev->bitmap); in md_bitmap_copy_from_slot()
2090 md_bitmap_free(bitmap); in md_bitmap_copy_from_slot()
2097 void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap) in md_bitmap_status() argument
2102 if (!bitmap) in md_bitmap_status()
2105 counts = &bitmap->counts; in md_bitmap_status()
2107 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10; in md_bitmap_status()
2114 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize, in md_bitmap_status()
2116 if (bitmap->storage.file) { in md_bitmap_status()
2118 seq_file_path(seq, bitmap->storage.file, " \t\n"); in md_bitmap_status()
2124 int md_bitmap_resize(struct bitmap *bitmap, sector_t blocks, in md_bitmap_resize() argument
2147 if (bitmap->storage.file && !init) { in md_bitmap_resize()
2157 long space = bitmap->mddev->bitmap_info.space; in md_bitmap_resize()
2163 bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8); in md_bitmap_resize()
2164 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_resize()
2167 bitmap->mddev->bitmap_info.space = space; in md_bitmap_resize()
2169 chunkshift = bitmap->counts.chunkshift; in md_bitmap_resize()
2176 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_resize()
2185 if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file) in md_bitmap_resize()
2187 !bitmap->mddev->bitmap_info.external, in md_bitmap_resize()
2188 mddev_is_clustered(bitmap->mddev) in md_bitmap_resize()
2189 ? bitmap->cluster_slot : 0); in md_bitmap_resize()
2205 bitmap->mddev->pers->quiesce(bitmap->mddev, 1); in md_bitmap_resize()
2207 store.file = bitmap->storage.file; in md_bitmap_resize()
2208 bitmap->storage.file = NULL; in md_bitmap_resize()
2210 if (store.sb_page && bitmap->storage.sb_page) in md_bitmap_resize()
2212 page_address(bitmap->storage.sb_page), in md_bitmap_resize()
2214 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_resize()
2215 md_bitmap_file_unmap(&bitmap->storage); in md_bitmap_resize()
2216 bitmap->storage = store; in md_bitmap_resize()
2218 old_counts = bitmap->counts; in md_bitmap_resize()
2219 bitmap->counts.bp = new_bp; in md_bitmap_resize()
2220 bitmap->counts.pages = pages; in md_bitmap_resize()
2221 bitmap->counts.missing_pages = pages; in md_bitmap_resize()
2222 bitmap->counts.chunkshift = chunkshift; in md_bitmap_resize()
2223 bitmap->counts.chunks = chunks; in md_bitmap_resize()
2224 bitmap->mddev->bitmap_info.chunksize = 1UL << (chunkshift + in md_bitmap_resize()
2231 if (mddev_is_clustered(bitmap->mddev)) { in md_bitmap_resize()
2234 ret = md_bitmap_checkpage(&bitmap->counts, page, 1, 1); in md_bitmap_resize()
2245 bitmap->counts.bp = old_counts.bp; in md_bitmap_resize()
2246 bitmap->counts.pages = old_counts.pages; in md_bitmap_resize()
2247 bitmap->counts.missing_pages = old_counts.pages; in md_bitmap_resize()
2248 bitmap->counts.chunkshift = old_counts.chunkshift; in md_bitmap_resize()
2249 bitmap->counts.chunks = old_counts.chunks; in md_bitmap_resize()
2250 bitmap->mddev->bitmap_info.chunksize = in md_bitmap_resize()
2256 bitmap->counts.bp[page].count += 1; in md_bitmap_resize()
2268 bmc_new = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); in md_bitmap_resize()
2277 md_bitmap_file_set_bit(bitmap, block); in md_bitmap_resize()
2281 md_bitmap_count_page(&bitmap->counts, block, 1); in md_bitmap_resize()
2282 md_bitmap_set_pending(&bitmap->counts, block); in md_bitmap_resize()
2292 if (bitmap->counts.bp != old_counts.bp) { in md_bitmap_resize()
2304 bmc = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); in md_bitmap_resize()
2311 md_bitmap_count_page(&bitmap->counts, block, 1); in md_bitmap_resize()
2312 md_bitmap_set_pending(&bitmap->counts, block); in md_bitmap_resize()
2317 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_resize()
2318 set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY); in md_bitmap_resize()
2320 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_resize()
2323 md_bitmap_unplug(bitmap); in md_bitmap_resize()
2324 bitmap->mddev->pers->quiesce(bitmap->mddev, 0); in md_bitmap_resize()
2362 if (mddev->bitmap || mddev->bitmap_info.file || in location_store()
2380 struct bitmap *bitmap; in location_store() local
2407 bitmap = md_bitmap_create(mddev, -1); in location_store()
2408 if (IS_ERR(bitmap)) { in location_store()
2409 rv = PTR_ERR(bitmap); in location_store()
2413 mddev->bitmap = bitmap; in location_store()
2463 if (mddev->bitmap && in space_store()
2464 sectors < (mddev->bitmap->storage.bytes + 511) >> 9) in space_store()
2572 md_bitmap_update_sb(mddev->bitmap); in backlog_store()
2593 if (mddev->bitmap) in chunksize_store()
2621 if (mddev->bitmap || in metadata_store()
2642 if (mddev->bitmap) in can_clear_show()
2643 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ? in can_clear_show()
2653 if (mddev->bitmap == NULL) in can_clear_store()
2656 mddev->bitmap->need_sync = 1; in can_clear_store()
2660 mddev->bitmap->need_sync = 0; in can_clear_store()
2674 if (mddev->bitmap == NULL) in behind_writes_used_show()
2678 mddev->bitmap->behind_writes_used); in behind_writes_used_show()
2686 if (mddev->bitmap) in behind_writes_used_reset()
2687 mddev->bitmap->behind_writes_used = 0; in behind_writes_used_reset()