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 if (page >= bitmap->pages) { in md_bitmap_checkpage()
65 if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */ in md_bitmap_checkpage()
68 if (bitmap->bp[page].map) /* page is already allocated, just return */ in md_bitmap_checkpage()
76 spin_unlock_irq(&bitmap->lock); in md_bitmap_checkpage()
91 spin_lock_irq(&bitmap->lock); in md_bitmap_checkpage()
100 if (!bitmap->bp[page].map) in md_bitmap_checkpage()
101 bitmap->bp[page].hijacked = 1; in md_bitmap_checkpage()
102 } else if (bitmap->bp[page].map || in md_bitmap_checkpage()
103 bitmap->bp[page].hijacked) { in md_bitmap_checkpage()
110 bitmap->bp[page].map = mappage; in md_bitmap_checkpage()
111 bitmap->missing_pages--; in md_bitmap_checkpage()
119 static void md_bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page) in md_bitmap_checkfree() argument
123 if (bitmap->bp[page].count) /* page is still busy */ in md_bitmap_checkfree()
128 if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */ in md_bitmap_checkfree()
129 bitmap->bp[page].hijacked = 0; in md_bitmap_checkfree()
130 bitmap->bp[page].map = NULL; in md_bitmap_checkfree()
133 ptr = bitmap->bp[page].map; in md_bitmap_checkfree()
134 bitmap->bp[page].map = NULL; in md_bitmap_checkfree()
135 bitmap->missing_pages++; in md_bitmap_checkfree()
212 static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait) in write_sb_page() argument
216 struct mddev *mddev = bitmap->mddev; in write_sb_page()
217 struct bitmap_storage *store = &bitmap->storage; in write_sb_page()
284 static void md_bitmap_file_kick(struct bitmap *bitmap);
288 static void write_page(struct bitmap *bitmap, struct page *page, int wait) in write_page() argument
292 if (bitmap->storage.file == NULL) { in write_page()
293 switch (write_sb_page(bitmap, page, wait)) { in write_page()
295 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags); in write_page()
302 atomic_inc(&bitmap->pending_writes); in write_page()
310 wait_event(bitmap->write_wait, in write_page()
311 atomic_read(&bitmap->pending_writes)==0); in write_page()
313 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in write_page()
314 md_bitmap_file_kick(bitmap); in write_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, in read_page() argument
393 bh->b_private = bitmap; in read_page()
394 atomic_inc(&bitmap->pending_writes); in read_page()
404 wait_event(bitmap->write_wait, in read_page()
405 atomic_read(&bitmap->pending_writes)==0); in read_page()
406 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in read_page()
426 static void md_bitmap_wait_writes(struct bitmap *bitmap) in md_bitmap_wait_writes() argument
428 if (bitmap->storage.file) in md_bitmap_wait_writes()
429 wait_event(bitmap->write_wait, in md_bitmap_wait_writes()
430 atomic_read(&bitmap->pending_writes)==0); in md_bitmap_wait_writes()
439 md_super_wait(bitmap->mddev); in md_bitmap_wait_writes()
444 void md_bitmap_update_sb(struct bitmap *bitmap) in md_bitmap_update_sb() argument
448 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */ in md_bitmap_update_sb()
450 if (bitmap->mddev->bitmap_info.external) in md_bitmap_update_sb()
452 if (!bitmap->storage.sb_page) /* no superblock */ in md_bitmap_update_sb()
454 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_update_sb()
455 sb->events = cpu_to_le64(bitmap->mddev->events); in md_bitmap_update_sb()
456 if (bitmap->mddev->events < bitmap->events_cleared) in md_bitmap_update_sb()
458 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_update_sb()
459 sb->events_cleared = cpu_to_le64(bitmap->events_cleared); in md_bitmap_update_sb()
464 sb->state = cpu_to_le32(bitmap->flags & ~BIT(BITMAP_WRITE_ERROR)); in md_bitmap_update_sb()
466 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ); in md_bitmap_update_sb()
467 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_update_sb()
469 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_update_sb()
470 sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize); in md_bitmap_update_sb()
471 sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes); in md_bitmap_update_sb()
472 sb->sectors_reserved = cpu_to_le32(bitmap->mddev-> in md_bitmap_update_sb()
475 write_page(bitmap, bitmap->storage.sb_page, 1); in md_bitmap_update_sb()
480 void md_bitmap_print_sb(struct bitmap *bitmap) in md_bitmap_print_sb() argument
484 if (!bitmap || !bitmap->storage.sb_page) in md_bitmap_print_sb()
486 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_print_sb()
487 pr_debug("%s: bitmap file superblock:\n", bmname(bitmap)); in md_bitmap_print_sb()
519 static int md_bitmap_new_disk_sb(struct bitmap *bitmap) in md_bitmap_new_disk_sb() argument
524 bitmap->storage.sb_page = alloc_page(GFP_KERNEL | __GFP_ZERO); in md_bitmap_new_disk_sb()
525 if (bitmap->storage.sb_page == NULL) in md_bitmap_new_disk_sb()
527 bitmap->storage.sb_page->index = 0; in md_bitmap_new_disk_sb()
529 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_new_disk_sb()
534 chunksize = bitmap->mddev->bitmap_info.chunksize; in md_bitmap_new_disk_sb()
543 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep; in md_bitmap_new_disk_sb()
549 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; in md_bitmap_new_disk_sb()
555 write_behind = bitmap->mddev->bitmap_info.max_write_behind; in md_bitmap_new_disk_sb()
559 bitmap->mddev->bitmap_info.max_write_behind = write_behind; in md_bitmap_new_disk_sb()
562 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_new_disk_sb()
564 memcpy(sb->uuid, bitmap->mddev->uuid, 16); in md_bitmap_new_disk_sb()
566 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_new_disk_sb()
567 sb->state = cpu_to_le32(bitmap->flags); in md_bitmap_new_disk_sb()
568 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_new_disk_sb()
569 sb->events_cleared = cpu_to_le64(bitmap->mddev->events); in md_bitmap_new_disk_sb()
570 bitmap->mddev->bitmap_info.nodes = 0; in md_bitmap_new_disk_sb()
578 static int md_bitmap_read_sb(struct bitmap *bitmap) in md_bitmap_read_sb() argument
588 loff_t offset = bitmap->mddev->bitmap_info.offset; in md_bitmap_read_sb()
590 if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) { in md_bitmap_read_sb()
594 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_read_sb()
602 bitmap->storage.sb_page = sb_page; in md_bitmap_read_sb()
606 if (bitmap->cluster_slot >= 0) { in md_bitmap_read_sb()
607 sector_t bm_blocks = bitmap->mddev->resync_max_sectors; in md_bitmap_read_sb()
610 (bitmap->mddev->bitmap_info.chunksize >> 9)); in md_bitmap_read_sb()
615 offset = bitmap->mddev->bitmap_info.offset + (bitmap->cluster_slot * (bm_blocks << 3)); in md_bitmap_read_sb()
617 bitmap->cluster_slot, offset); in md_bitmap_read_sb()
620 if (bitmap->storage.file) { in md_bitmap_read_sb()
621 loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host); in md_bitmap_read_sb()
624 err = read_page(bitmap->storage.file, 0, in md_bitmap_read_sb()
625 bitmap, bytes, sb_page); in md_bitmap_read_sb()
627 err = read_sb_page(bitmap->mddev, in md_bitmap_read_sb()
659 bmname(bitmap), reason); in md_bitmap_read_sb()
669 strscpy(bitmap->mddev->bitmap_info.cluster_name, in md_bitmap_read_sb()
674 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_read_sb()
676 if (bitmap->mddev->persistent) { in md_bitmap_read_sb()
681 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) { in md_bitmap_read_sb()
683 bmname(bitmap)); in md_bitmap_read_sb()
687 if (!nodes && (events < bitmap->mddev->events)) { in md_bitmap_read_sb()
689 bmname(bitmap), events, in md_bitmap_read_sb()
690 (unsigned long long) bitmap->mddev->events); in md_bitmap_read_sb()
691 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_read_sb()
696 bitmap->flags |= le32_to_cpu(sb->state); in md_bitmap_read_sb()
698 set_bit(BITMAP_HOSTENDIAN, &bitmap->flags); in md_bitmap_read_sb()
699 bitmap->events_cleared = le64_to_cpu(sb->events_cleared); in md_bitmap_read_sb()
704 if (err == 0 && nodes && (bitmap->cluster_slot < 0)) { in md_bitmap_read_sb()
706 bitmap->mddev->bitmap_info.chunksize = chunksize; in md_bitmap_read_sb()
707 err = md_setup_cluster(bitmap->mddev, nodes); in md_bitmap_read_sb()
710 bmname(bitmap), err); in md_bitmap_read_sb()
713 bitmap->cluster_slot = md_cluster_ops->slot_number(bitmap->mddev); in md_bitmap_read_sb()
719 if (test_bit(BITMAP_STALE, &bitmap->flags)) in md_bitmap_read_sb()
720 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_read_sb()
721 bitmap->mddev->bitmap_info.chunksize = chunksize; in md_bitmap_read_sb()
722 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; in md_bitmap_read_sb()
723 bitmap->mddev->bitmap_info.max_write_behind = write_behind; in md_bitmap_read_sb()
724 bitmap->mddev->bitmap_info.nodes = nodes; in md_bitmap_read_sb()
725 if (bitmap->mddev->bitmap_info.space == 0 || in md_bitmap_read_sb()
726 bitmap->mddev->bitmap_info.space > sectors_reserved) in md_bitmap_read_sb()
727 bitmap->mddev->bitmap_info.space = sectors_reserved; in md_bitmap_read_sb()
729 md_bitmap_print_sb(bitmap); in md_bitmap_read_sb()
730 if (bitmap->cluster_slot < 0) in md_bitmap_read_sb()
731 md_cluster_stop(bitmap->mddev); in md_bitmap_read_sb()
864 static void md_bitmap_file_kick(struct bitmap *bitmap) in md_bitmap_file_kick() argument
868 if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) { in md_bitmap_file_kick()
869 md_bitmap_update_sb(bitmap); in md_bitmap_file_kick()
871 if (bitmap->storage.file) { in md_bitmap_file_kick()
874 ptr = file_path(bitmap->storage.file, in md_bitmap_file_kick()
878 bmname(bitmap), IS_ERR(ptr) ? "" : ptr); in md_bitmap_file_kick()
883 bmname(bitmap)); in md_bitmap_file_kick()
894 static inline void set_page_attr(struct bitmap *bitmap, int pnum, in set_page_attr() argument
897 set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in set_page_attr()
900 static inline void clear_page_attr(struct bitmap *bitmap, int pnum, in clear_page_attr() argument
903 clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in clear_page_attr()
906 static inline int test_page_attr(struct bitmap *bitmap, int pnum, in test_page_attr() argument
909 return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in test_page_attr()
912 static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum, in test_and_clear_page_attr() argument
916 bitmap->storage.filemap_attr); in test_and_clear_page_attr()
925 static void md_bitmap_file_set_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_set_bit() argument
930 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_set_bit()
931 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_file_set_bit()
934 if (mddev_is_clustered(bitmap->mddev)) in md_bitmap_file_set_bit()
935 node_offset = bitmap->cluster_slot * store->file_pages; in md_bitmap_file_set_bit()
937 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_set_bit()
940 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_set_bit()
944 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_set_bit()
951 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_DIRTY); in md_bitmap_file_set_bit()
954 static void md_bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_clear_bit() argument
959 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_clear_bit()
960 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_file_clear_bit()
963 if (mddev_is_clustered(bitmap->mddev)) in md_bitmap_file_clear_bit()
964 node_offset = bitmap->cluster_slot * store->file_pages; in md_bitmap_file_clear_bit()
966 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_clear_bit()
969 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_clear_bit()
971 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_clear_bit()
976 if (!test_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_NEEDWRITE)) { in md_bitmap_file_clear_bit()
977 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_PENDING); in md_bitmap_file_clear_bit()
978 bitmap->allclean = 0; in md_bitmap_file_clear_bit()
982 static int md_bitmap_file_test_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_test_bit() argument
987 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_test_bit()
990 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_test_bit()
993 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_test_bit()
995 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_test_bit()
1007 void md_bitmap_unplug(struct bitmap *bitmap) in md_bitmap_unplug() argument
1013 if (!bitmap || !bitmap->storage.filemap || in md_bitmap_unplug()
1014 test_bit(BITMAP_STALE, &bitmap->flags)) in md_bitmap_unplug()
1019 for (i = 0; i < bitmap->storage.file_pages; i++) { in md_bitmap_unplug()
1020 dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY); in md_bitmap_unplug()
1021 need_write = test_and_clear_page_attr(bitmap, i, in md_bitmap_unplug()
1025 md_bitmap_wait_writes(bitmap); in md_bitmap_unplug()
1026 if (bitmap->mddev->queue) in md_bitmap_unplug()
1027 blk_add_trace_msg(bitmap->mddev->queue, in md_bitmap_unplug()
1030 clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING); in md_bitmap_unplug()
1031 write_page(bitmap, bitmap->storage.filemap[i], 0); in md_bitmap_unplug()
1036 md_bitmap_wait_writes(bitmap); in md_bitmap_unplug()
1038 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in md_bitmap_unplug()
1039 md_bitmap_file_kick(bitmap); in md_bitmap_unplug()
1043 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
1055 static int md_bitmap_init_from_disk(struct bitmap *bitmap, sector_t start) in md_bitmap_init_from_disk() argument
1065 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_init_from_disk()
1067 chunks = bitmap->counts.chunks; in md_bitmap_init_from_disk()
1070 if (!file && !bitmap->mddev->bitmap_info.offset) { in md_bitmap_init_from_disk()
1076 int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift) in md_bitmap_init_from_disk()
1078 md_bitmap_set_memory_bits(bitmap, in md_bitmap_init_from_disk()
1079 (sector_t)i << bitmap->counts.chunkshift, in md_bitmap_init_from_disk()
1085 outofdate = test_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_init_from_disk()
1087 pr_warn("%s: bitmap file is out of date, doing full recovery\n", bmname(bitmap)); in md_bitmap_init_from_disk()
1091 bmname(bitmap), in md_bitmap_init_from_disk()
1099 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_init_from_disk()
1102 if (mddev_is_clustered(bitmap->mddev)) in md_bitmap_init_from_disk()
1103 node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE)); in md_bitmap_init_from_disk()
1107 index = file_page_index(&bitmap->storage, i); in md_bitmap_init_from_disk()
1108 bit = file_page_offset(&bitmap->storage, i); in md_bitmap_init_from_disk()
1118 ret = read_page(file, index, bitmap, in md_bitmap_init_from_disk()
1122 bitmap->mddev, in md_bitmap_init_from_disk()
1123 bitmap->mddev->bitmap_info.offset, in md_bitmap_init_from_disk()
1141 write_page(bitmap, page, 1); in md_bitmap_init_from_disk()
1145 &bitmap->flags)) in md_bitmap_init_from_disk()
1150 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_init_from_disk()
1157 int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift in md_bitmap_init_from_disk()
1159 md_bitmap_set_memory_bits(bitmap, in md_bitmap_init_from_disk()
1160 (sector_t)i << bitmap->counts.chunkshift, in md_bitmap_init_from_disk()
1168 bmname(bitmap), store->file_pages, in md_bitmap_init_from_disk()
1175 bmname(bitmap), ret); in md_bitmap_init_from_disk()
1179 void md_bitmap_write_all(struct bitmap *bitmap) in md_bitmap_write_all() argument
1186 if (!bitmap || !bitmap->storage.filemap) in md_bitmap_write_all()
1188 if (bitmap->storage.file) in md_bitmap_write_all()
1192 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_write_all()
1193 set_page_attr(bitmap, i, in md_bitmap_write_all()
1195 bitmap->allclean = 0; in md_bitmap_write_all()
1198 static void md_bitmap_count_page(struct bitmap_counts *bitmap, in md_bitmap_count_page() argument
1201 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_count_page()
1203 bitmap->bp[page].count += inc; in md_bitmap_count_page()
1204 md_bitmap_checkfree(bitmap, page); in md_bitmap_count_page()
1207 static void md_bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset) in md_bitmap_set_pending() argument
1209 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_set_pending()
1211 struct bitmap_page *bp = &bitmap->bp[page]; in md_bitmap_set_pending()
1217 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
1228 struct bitmap *bitmap; in md_bitmap_daemon_work() local
1238 bitmap = mddev->bitmap; in md_bitmap_daemon_work()
1239 if (bitmap == NULL) { in md_bitmap_daemon_work()
1243 if (time_before(jiffies, bitmap->daemon_lastrun in md_bitmap_daemon_work()
1247 bitmap->daemon_lastrun = jiffies; in md_bitmap_daemon_work()
1248 if (bitmap->allclean) { in md_bitmap_daemon_work()
1252 bitmap->allclean = 1; in md_bitmap_daemon_work()
1254 if (bitmap->mddev->queue) in md_bitmap_daemon_work()
1255 blk_add_trace_msg(bitmap->mddev->queue, in md_bitmap_daemon_work()
1262 for (j = 0; j < bitmap->storage.file_pages; j++) in md_bitmap_daemon_work()
1263 if (test_and_clear_page_attr(bitmap, j, in md_bitmap_daemon_work()
1265 set_page_attr(bitmap, j, in md_bitmap_daemon_work()
1268 if (bitmap->need_sync && in md_bitmap_daemon_work()
1273 bitmap->need_sync = 0; in md_bitmap_daemon_work()
1274 if (bitmap->storage.filemap) { in md_bitmap_daemon_work()
1275 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_daemon_work()
1277 cpu_to_le64(bitmap->events_cleared); in md_bitmap_daemon_work()
1279 set_page_attr(bitmap, 0, in md_bitmap_daemon_work()
1286 counts = &bitmap->counts; in md_bitmap_daemon_work()
1307 if (*bmc == 1 && !bitmap->need_sync) { in md_bitmap_daemon_work()
1311 md_bitmap_file_clear_bit(bitmap, block); in md_bitmap_daemon_work()
1315 bitmap->allclean = 0; in md_bitmap_daemon_work()
1320 md_bitmap_wait_writes(bitmap); in md_bitmap_daemon_work()
1330 j < bitmap->storage.file_pages in md_bitmap_daemon_work()
1331 && !test_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_daemon_work()
1333 if (test_page_attr(bitmap, j, in md_bitmap_daemon_work()
1337 if (bitmap->storage.filemap && in md_bitmap_daemon_work()
1338 test_and_clear_page_attr(bitmap, j, in md_bitmap_daemon_work()
1340 write_page(bitmap, bitmap->storage.filemap[j], 0); in md_bitmap_daemon_work()
1345 if (bitmap->allclean == 0) in md_bitmap_daemon_work()
1351 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap, in md_bitmap_get_counter() argument
1354 __releases(bitmap->lock) in md_bitmap_get_counter()
1355 __acquires(bitmap->lock) in md_bitmap_get_counter()
1361 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_get_counter()
1367 err = md_bitmap_checkpage(bitmap, page, create, 0); in md_bitmap_get_counter()
1369 if (bitmap->bp[page].hijacked || in md_bitmap_get_counter()
1370 bitmap->bp[page].map == NULL) in md_bitmap_get_counter()
1371 csize = ((sector_t)1) << (bitmap->chunkshift + in md_bitmap_get_counter()
1374 csize = ((sector_t)1) << bitmap->chunkshift; in md_bitmap_get_counter()
1382 if (bitmap->bp[page].hijacked) { /* hijacked pointer */ in md_bitmap_get_counter()
1387 &bitmap->bp[page].map)[hi]; in md_bitmap_get_counter()
1390 &(bitmap->bp[page].map[pageoff]); in md_bitmap_get_counter()
1393 int md_bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind) in md_bitmap_startwrite() argument
1395 if (!bitmap) in md_bitmap_startwrite()
1400 atomic_inc(&bitmap->behind_writes); in md_bitmap_startwrite()
1401 bw = atomic_read(&bitmap->behind_writes); in md_bitmap_startwrite()
1402 if (bw > bitmap->behind_writes_used) in md_bitmap_startwrite()
1403 bitmap->behind_writes_used = bw; in md_bitmap_startwrite()
1406 bw, bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_startwrite()
1413 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1414 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 1); in md_bitmap_startwrite()
1416 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1426 prepare_to_wait(&bitmap->overflow_wait, &__wait, in md_bitmap_startwrite()
1428 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1430 finish_wait(&bitmap->overflow_wait, &__wait); in md_bitmap_startwrite()
1436 md_bitmap_file_set_bit(bitmap, offset); in md_bitmap_startwrite()
1437 md_bitmap_count_page(&bitmap->counts, offset, 1); in md_bitmap_startwrite()
1445 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1457 void md_bitmap_endwrite(struct bitmap *bitmap, sector_t offset, in md_bitmap_endwrite() argument
1460 if (!bitmap) in md_bitmap_endwrite()
1463 if (atomic_dec_and_test(&bitmap->behind_writes)) in md_bitmap_endwrite()
1464 wake_up(&bitmap->behind_wait); in md_bitmap_endwrite()
1466 atomic_read(&bitmap->behind_writes), in md_bitmap_endwrite()
1467 bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_endwrite()
1475 spin_lock_irqsave(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1476 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 0); in md_bitmap_endwrite()
1478 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1482 if (success && !bitmap->mddev->degraded && in md_bitmap_endwrite()
1483 bitmap->events_cleared < bitmap->mddev->events) { in md_bitmap_endwrite()
1484 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_endwrite()
1485 bitmap->need_sync = 1; in md_bitmap_endwrite()
1486 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear); in md_bitmap_endwrite()
1493 wake_up(&bitmap->overflow_wait); in md_bitmap_endwrite()
1497 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_endwrite()
1498 bitmap->allclean = 0; in md_bitmap_endwrite()
1500 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1510 static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, in __bitmap_start_sync() argument
1515 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */ in __bitmap_start_sync()
1519 spin_lock_irq(&bitmap->counts.lock); in __bitmap_start_sync()
1520 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); in __bitmap_start_sync()
1534 spin_unlock_irq(&bitmap->counts.lock); in __bitmap_start_sync()
1538 int md_bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, in md_bitmap_start_sync() argument
1553 rv |= __bitmap_start_sync(bitmap, offset, in md_bitmap_start_sync()
1562 void md_bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted) in md_bitmap_end_sync() argument
1567 if (bitmap == NULL) { in md_bitmap_end_sync()
1571 spin_lock_irqsave(&bitmap->counts.lock, flags); in md_bitmap_end_sync()
1572 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); in md_bitmap_end_sync()
1583 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_end_sync()
1584 bitmap->allclean = 0; in md_bitmap_end_sync()
1589 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_end_sync()
1593 void md_bitmap_close_sync(struct bitmap *bitmap) in md_bitmap_close_sync() argument
1601 if (!bitmap) in md_bitmap_close_sync()
1603 while (sector < bitmap->mddev->resync_max_sectors) { in md_bitmap_close_sync()
1604 md_bitmap_end_sync(bitmap, sector, &blocks, 0); in md_bitmap_close_sync()
1610 void md_bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector, bool force) in md_bitmap_cond_end_sync() argument
1615 if (!bitmap) in md_bitmap_cond_end_sync()
1618 bitmap->last_end_sync = jiffies; in md_bitmap_cond_end_sync()
1621 if (!force && time_before(jiffies, (bitmap->last_end_sync in md_bitmap_cond_end_sync()
1622 + bitmap->mddev->bitmap_info.daemon_sleep))) in md_bitmap_cond_end_sync()
1624 wait_event(bitmap->mddev->recovery_wait, in md_bitmap_cond_end_sync()
1625 atomic_read(&bitmap->mddev->recovery_active) == 0); in md_bitmap_cond_end_sync()
1627 bitmap->mddev->curr_resync_completed = sector; in md_bitmap_cond_end_sync()
1628 set_bit(MD_SB_CHANGE_CLEAN, &bitmap->mddev->sb_flags); in md_bitmap_cond_end_sync()
1629 sector &= ~((1ULL << bitmap->counts.chunkshift) - 1); in md_bitmap_cond_end_sync()
1631 while (s < sector && s < bitmap->mddev->resync_max_sectors) { in md_bitmap_cond_end_sync()
1632 md_bitmap_end_sync(bitmap, s, &blocks, 0); in md_bitmap_cond_end_sync()
1635 bitmap->last_end_sync = jiffies; in md_bitmap_cond_end_sync()
1636 sysfs_notify_dirent_safe(bitmap->mddev->sysfs_completed); in md_bitmap_cond_end_sync()
1644 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_sync_with_cluster() local
1648 md_bitmap_end_sync(bitmap, sector, &blocks, 0); in md_bitmap_sync_with_cluster()
1654 md_bitmap_start_sync(bitmap, sector, &blocks, 0); in md_bitmap_sync_with_cluster()
1661 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed) in md_bitmap_set_memory_bits() argument
1670 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1671 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &secs, 1); in md_bitmap_set_memory_bits()
1673 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1678 md_bitmap_count_page(&bitmap->counts, offset, 1); in md_bitmap_set_memory_bits()
1679 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_set_memory_bits()
1680 bitmap->allclean = 0; in md_bitmap_set_memory_bits()
1684 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1688 void md_bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e) in md_bitmap_dirty_bits() argument
1693 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift; in md_bitmap_dirty_bits()
1694 md_bitmap_set_memory_bits(bitmap, sec, 1); in md_bitmap_dirty_bits()
1695 md_bitmap_file_set_bit(bitmap, sec); in md_bitmap_dirty_bits()
1696 if (sec < bitmap->mddev->recovery_cp) in md_bitmap_dirty_bits()
1701 bitmap->mddev->recovery_cp = sec; in md_bitmap_dirty_bits()
1710 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_flush() local
1713 if (!bitmap) /* there was no bitmap */ in md_bitmap_flush()
1720 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1722 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1724 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1728 md_bitmap_update_sb(bitmap); in md_bitmap_flush()
1734 void md_bitmap_free(struct bitmap *bitmap) in md_bitmap_free() argument
1739 if (!bitmap) /* there was no bitmap */ in md_bitmap_free()
1742 if (bitmap->sysfs_can_clear) in md_bitmap_free()
1743 sysfs_put(bitmap->sysfs_can_clear); in md_bitmap_free()
1745 if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info && in md_bitmap_free()
1746 bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev)) in md_bitmap_free()
1747 md_cluster_stop(bitmap->mddev); in md_bitmap_free()
1750 wait_event(bitmap->write_wait, in md_bitmap_free()
1751 atomic_read(&bitmap->pending_writes) == 0); in md_bitmap_free()
1754 md_bitmap_file_unmap(&bitmap->storage); in md_bitmap_free()
1756 bp = bitmap->counts.bp; in md_bitmap_free()
1757 pages = bitmap->counts.pages; in md_bitmap_free()
1766 kfree(bitmap); in md_bitmap_free()
1772 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_wait_behind_writes() local
1775 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { in md_bitmap_wait_behind_writes()
1779 wait_event(bitmap->behind_wait, in md_bitmap_wait_behind_writes()
1780 atomic_read(&bitmap->behind_writes) == 0); in md_bitmap_wait_behind_writes()
1786 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_destroy() local
1788 if (!bitmap) /* there was no bitmap */ in md_bitmap_destroy()
1797 mddev->bitmap = NULL; /* disconnect from the md device */ in md_bitmap_destroy()
1803 md_bitmap_free(bitmap); in md_bitmap_destroy()
1811 struct bitmap *md_bitmap_create(struct mddev *mddev, int slot) in md_bitmap_create()
1813 struct bitmap *bitmap; in md_bitmap_create() local
1829 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); in md_bitmap_create()
1830 if (!bitmap) in md_bitmap_create()
1833 spin_lock_init(&bitmap->counts.lock); in md_bitmap_create()
1834 atomic_set(&bitmap->pending_writes, 0); in md_bitmap_create()
1835 init_waitqueue_head(&bitmap->write_wait); in md_bitmap_create()
1836 init_waitqueue_head(&bitmap->overflow_wait); in md_bitmap_create()
1837 init_waitqueue_head(&bitmap->behind_wait); in md_bitmap_create()
1839 bitmap->mddev = mddev; in md_bitmap_create()
1840 bitmap->cluster_slot = slot; in md_bitmap_create()
1845 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear"); in md_bitmap_create()
1848 bitmap->sysfs_can_clear = NULL; in md_bitmap_create()
1850 bitmap->storage.file = file; in md_bitmap_create()
1866 err = md_bitmap_new_disk_sb(bitmap); in md_bitmap_create()
1868 err = md_bitmap_read_sb(bitmap); in md_bitmap_create()
1880 bitmap->daemon_lastrun = jiffies; in md_bitmap_create()
1881 err = md_bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1); in md_bitmap_create()
1886 bitmap->counts.pages, bmname(bitmap)); in md_bitmap_create()
1888 err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0; in md_bitmap_create()
1892 return bitmap; in md_bitmap_create()
1894 md_bitmap_free(bitmap); in md_bitmap_create()
1903 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_load() local
1906 if (!bitmap) in md_bitmap_load()
1922 md_bitmap_start_sync(bitmap, sector, &blocks, 0); in md_bitmap_load()
1925 md_bitmap_close_sync(bitmap); in md_bitmap_load()
1928 || bitmap->events_cleared == mddev->events) in md_bitmap_load()
1934 err = md_bitmap_init_from_disk(bitmap, start); in md_bitmap_load()
1939 clear_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_load()
1942 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery); in md_bitmap_load()
1947 md_bitmap_update_sb(bitmap); in md_bitmap_load()
1949 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in md_bitmap_load()
1957 struct bitmap *get_bitmap_from_slot(struct mddev *mddev, int slot) in get_bitmap_from_slot()
1960 struct bitmap *bitmap; in get_bitmap_from_slot() local
1962 bitmap = md_bitmap_create(mddev, slot); in get_bitmap_from_slot()
1963 if (IS_ERR(bitmap)) { in get_bitmap_from_slot()
1964 rv = PTR_ERR(bitmap); in get_bitmap_from_slot()
1968 rv = md_bitmap_init_from_disk(bitmap, 0); in get_bitmap_from_slot()
1970 md_bitmap_free(bitmap); in get_bitmap_from_slot()
1974 return bitmap; in get_bitmap_from_slot()
1987 struct bitmap *bitmap; in md_bitmap_copy_from_slot() local
1989 bitmap = get_bitmap_from_slot(mddev, slot); in md_bitmap_copy_from_slot()
1990 if (IS_ERR(bitmap)) { in md_bitmap_copy_from_slot()
1995 counts = &bitmap->counts; in md_bitmap_copy_from_slot()
1998 if (md_bitmap_file_test_bit(bitmap, block)) { in md_bitmap_copy_from_slot()
2002 md_bitmap_file_clear_bit(bitmap, block); in md_bitmap_copy_from_slot()
2003 md_bitmap_set_memory_bits(mddev->bitmap, block, 1); in md_bitmap_copy_from_slot()
2004 md_bitmap_file_set_bit(mddev->bitmap, block); in md_bitmap_copy_from_slot()
2009 md_bitmap_update_sb(bitmap); in md_bitmap_copy_from_slot()
2012 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_copy_from_slot()
2013 if (test_page_attr(bitmap, i, BITMAP_PAGE_PENDING)) in md_bitmap_copy_from_slot()
2014 set_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE); in md_bitmap_copy_from_slot()
2015 md_bitmap_unplug(bitmap); in md_bitmap_copy_from_slot()
2017 md_bitmap_unplug(mddev->bitmap); in md_bitmap_copy_from_slot()
2020 md_bitmap_free(bitmap); in md_bitmap_copy_from_slot()
2027 void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap) in md_bitmap_status() argument
2032 if (!bitmap) in md_bitmap_status()
2035 counts = &bitmap->counts; in md_bitmap_status()
2037 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10; in md_bitmap_status()
2044 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize, in md_bitmap_status()
2046 if (bitmap->storage.file) { in md_bitmap_status()
2048 seq_file_path(seq, bitmap->storage.file, " \t\n"); in md_bitmap_status()
2054 int md_bitmap_resize(struct bitmap *bitmap, sector_t blocks, in md_bitmap_resize() argument
2077 if (bitmap->storage.file && !init) { in md_bitmap_resize()
2087 long space = bitmap->mddev->bitmap_info.space; in md_bitmap_resize()
2093 bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8); in md_bitmap_resize()
2094 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_resize()
2097 bitmap->mddev->bitmap_info.space = space; in md_bitmap_resize()
2099 chunkshift = bitmap->counts.chunkshift; in md_bitmap_resize()
2106 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_resize()
2115 if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file) in md_bitmap_resize()
2117 !bitmap->mddev->bitmap_info.external, in md_bitmap_resize()
2118 mddev_is_clustered(bitmap->mddev) in md_bitmap_resize()
2119 ? bitmap->cluster_slot : 0); in md_bitmap_resize()
2135 bitmap->mddev->pers->quiesce(bitmap->mddev, 1); in md_bitmap_resize()
2137 store.file = bitmap->storage.file; in md_bitmap_resize()
2138 bitmap->storage.file = NULL; in md_bitmap_resize()
2140 if (store.sb_page && bitmap->storage.sb_page) in md_bitmap_resize()
2142 page_address(bitmap->storage.sb_page), in md_bitmap_resize()
2144 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_resize()
2145 md_bitmap_file_unmap(&bitmap->storage); in md_bitmap_resize()
2146 bitmap->storage = store; in md_bitmap_resize()
2148 old_counts = bitmap->counts; in md_bitmap_resize()
2149 bitmap->counts.bp = new_bp; in md_bitmap_resize()
2150 bitmap->counts.pages = pages; in md_bitmap_resize()
2151 bitmap->counts.missing_pages = pages; in md_bitmap_resize()
2152 bitmap->counts.chunkshift = chunkshift; in md_bitmap_resize()
2153 bitmap->counts.chunks = chunks; in md_bitmap_resize()
2154 bitmap->mddev->bitmap_info.chunksize = 1UL << (chunkshift + in md_bitmap_resize()
2161 if (mddev_is_clustered(bitmap->mddev)) { in md_bitmap_resize()
2164 ret = md_bitmap_checkpage(&bitmap->counts, page, 1, 1); in md_bitmap_resize()
2175 bitmap->counts.bp = old_counts.bp; in md_bitmap_resize()
2176 bitmap->counts.pages = old_counts.pages; in md_bitmap_resize()
2177 bitmap->counts.missing_pages = old_counts.pages; in md_bitmap_resize()
2178 bitmap->counts.chunkshift = old_counts.chunkshift; in md_bitmap_resize()
2179 bitmap->counts.chunks = old_counts.chunks; in md_bitmap_resize()
2180 bitmap->mddev->bitmap_info.chunksize = in md_bitmap_resize()
2186 bitmap->counts.bp[page].count += 1; in md_bitmap_resize()
2198 bmc_new = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); in md_bitmap_resize()
2207 md_bitmap_file_set_bit(bitmap, block); in md_bitmap_resize()
2211 md_bitmap_count_page(&bitmap->counts, block, 1); in md_bitmap_resize()
2212 md_bitmap_set_pending(&bitmap->counts, block); in md_bitmap_resize()
2222 if (bitmap->counts.bp != old_counts.bp) { in md_bitmap_resize()
2234 bmc = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); in md_bitmap_resize()
2241 md_bitmap_count_page(&bitmap->counts, block, 1); in md_bitmap_resize()
2242 md_bitmap_set_pending(&bitmap->counts, block); in md_bitmap_resize()
2247 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_resize()
2248 set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY); in md_bitmap_resize()
2250 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_resize()
2253 md_bitmap_unplug(bitmap); in md_bitmap_resize()
2254 bitmap->mddev->pers->quiesce(bitmap->mddev, 0); in md_bitmap_resize()
2295 if (mddev->bitmap || mddev->bitmap_info.file || in location_store()
2341 struct bitmap *bitmap; in location_store() local
2342 bitmap = md_bitmap_create(mddev, -1); in location_store()
2344 if (IS_ERR(bitmap)) in location_store()
2345 rv = PTR_ERR(bitmap); in location_store()
2347 mddev->bitmap = bitmap; in location_store()
2402 if (mddev->bitmap && in space_store()
2403 sectors < (mddev->bitmap->storage.bytes + 511) >> 9) in space_store()
2514 md_bitmap_update_sb(mddev->bitmap); in backlog_store()
2533 if (mddev->bitmap) in chunksize_store()
2561 if (mddev->bitmap || in metadata_store()
2582 if (mddev->bitmap) in can_clear_show()
2583 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ? in can_clear_show()
2593 if (mddev->bitmap == NULL) in can_clear_store()
2596 mddev->bitmap->need_sync = 1; in can_clear_store()
2600 mddev->bitmap->need_sync = 0; in can_clear_store()
2614 if (mddev->bitmap == NULL) in behind_writes_used_show()
2618 mddev->bitmap->behind_writes_used); in behind_writes_used_show()
2626 if (mddev->bitmap) in behind_writes_used_reset()
2627 mddev->bitmap->behind_writes_used = 0; in behind_writes_used_reset()