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()
360 struct bitmap *bitmap, in read_page() argument
396 bh->b_private = bitmap; in read_page()
397 atomic_inc(&bitmap->pending_writes); in read_page()
407 wait_event(bitmap->write_wait, in read_page()
408 atomic_read(&bitmap->pending_writes)==0); in read_page()
409 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in read_page()
429 static void md_bitmap_wait_writes(struct bitmap *bitmap) in md_bitmap_wait_writes() argument
431 if (bitmap->storage.file) in md_bitmap_wait_writes()
432 wait_event(bitmap->write_wait, in md_bitmap_wait_writes()
433 atomic_read(&bitmap->pending_writes)==0); in md_bitmap_wait_writes()
442 md_super_wait(bitmap->mddev); in md_bitmap_wait_writes()
447 void md_bitmap_update_sb(struct bitmap *bitmap) in md_bitmap_update_sb() argument
451 if (!bitmap || !bitmap->mddev) /* no bitmap for this array */ in md_bitmap_update_sb()
453 if (bitmap->mddev->bitmap_info.external) in md_bitmap_update_sb()
455 if (!bitmap->storage.sb_page) /* no superblock */ in md_bitmap_update_sb()
457 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_update_sb()
458 sb->events = cpu_to_le64(bitmap->mddev->events); in md_bitmap_update_sb()
459 if (bitmap->mddev->events < bitmap->events_cleared) in md_bitmap_update_sb()
461 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_update_sb()
462 sb->events_cleared = cpu_to_le64(bitmap->events_cleared); in md_bitmap_update_sb()
467 sb->state = cpu_to_le32(bitmap->flags & ~BIT(BITMAP_WRITE_ERROR)); in md_bitmap_update_sb()
469 sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ); in md_bitmap_update_sb()
470 sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_update_sb()
472 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_update_sb()
473 sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize); in md_bitmap_update_sb()
474 sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes); in md_bitmap_update_sb()
475 sb->sectors_reserved = cpu_to_le32(bitmap->mddev-> in md_bitmap_update_sb()
478 write_page(bitmap, bitmap->storage.sb_page, 1); in md_bitmap_update_sb()
483 void md_bitmap_print_sb(struct bitmap *bitmap) in md_bitmap_print_sb() argument
487 if (!bitmap || !bitmap->storage.sb_page) in md_bitmap_print_sb()
489 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_print_sb()
490 pr_debug("%s: bitmap file superblock:\n", bmname(bitmap)); in md_bitmap_print_sb()
522 static int md_bitmap_new_disk_sb(struct bitmap *bitmap) in md_bitmap_new_disk_sb() argument
527 bitmap->storage.sb_page = alloc_page(GFP_KERNEL | __GFP_ZERO); in md_bitmap_new_disk_sb()
528 if (bitmap->storage.sb_page == NULL) in md_bitmap_new_disk_sb()
530 bitmap->storage.sb_page->index = 0; in md_bitmap_new_disk_sb()
532 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_new_disk_sb()
537 chunksize = bitmap->mddev->bitmap_info.chunksize; in md_bitmap_new_disk_sb()
546 daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep; in md_bitmap_new_disk_sb()
552 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; in md_bitmap_new_disk_sb()
558 write_behind = bitmap->mddev->bitmap_info.max_write_behind; in md_bitmap_new_disk_sb()
562 bitmap->mddev->bitmap_info.max_write_behind = write_behind; in md_bitmap_new_disk_sb()
565 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_new_disk_sb()
567 memcpy(sb->uuid, bitmap->mddev->uuid, 16); in md_bitmap_new_disk_sb()
569 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_new_disk_sb()
570 sb->state = cpu_to_le32(bitmap->flags); in md_bitmap_new_disk_sb()
571 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_new_disk_sb()
572 sb->events_cleared = cpu_to_le64(bitmap->mddev->events); in md_bitmap_new_disk_sb()
573 bitmap->mddev->bitmap_info.nodes = 0; in md_bitmap_new_disk_sb()
581 static int md_bitmap_read_sb(struct bitmap *bitmap) in md_bitmap_read_sb() argument
591 loff_t offset = bitmap->mddev->bitmap_info.offset; in md_bitmap_read_sb()
593 if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) { in md_bitmap_read_sb()
597 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_read_sb()
605 bitmap->storage.sb_page = sb_page; in md_bitmap_read_sb()
609 if (bitmap->cluster_slot >= 0) { in md_bitmap_read_sb()
610 sector_t bm_blocks = bitmap->mddev->resync_max_sectors; in md_bitmap_read_sb()
613 bitmap->mddev->bitmap_info.chunksize >> 9); in md_bitmap_read_sb()
618 offset = bitmap->mddev->bitmap_info.offset + (bitmap->cluster_slot * (bm_blocks << 3)); in md_bitmap_read_sb()
620 bitmap->cluster_slot, offset); in md_bitmap_read_sb()
623 if (bitmap->storage.file) { in md_bitmap_read_sb()
624 loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host); in md_bitmap_read_sb()
627 err = read_page(bitmap->storage.file, 0, in md_bitmap_read_sb()
628 bitmap, bytes, sb_page); in md_bitmap_read_sb()
630 err = read_sb_page(bitmap->mddev, in md_bitmap_read_sb()
650 strlcpy(bitmap->mddev->bitmap_info.cluster_name, in md_bitmap_read_sb()
670 bmname(bitmap), reason); in md_bitmap_read_sb()
675 sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors); in md_bitmap_read_sb()
677 if (bitmap->mddev->persistent) { in md_bitmap_read_sb()
682 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) { in md_bitmap_read_sb()
684 bmname(bitmap)); in md_bitmap_read_sb()
688 if (!nodes && (events < bitmap->mddev->events)) { in md_bitmap_read_sb()
690 bmname(bitmap), events, in md_bitmap_read_sb()
691 (unsigned long long) bitmap->mddev->events); in md_bitmap_read_sb()
692 set_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_read_sb()
697 bitmap->flags |= le32_to_cpu(sb->state); in md_bitmap_read_sb()
699 set_bit(BITMAP_HOSTENDIAN, &bitmap->flags); in md_bitmap_read_sb()
700 bitmap->events_cleared = le64_to_cpu(sb->events_cleared); in md_bitmap_read_sb()
701 strlcpy(bitmap->mddev->bitmap_info.cluster_name, sb->cluster_name, 64); in md_bitmap_read_sb()
707 bitmap->mddev->bitmap_info.chunksize = chunksize; in md_bitmap_read_sb()
708 if (err == 0 && nodes && (bitmap->cluster_slot < 0)) { in md_bitmap_read_sb()
709 err = md_setup_cluster(bitmap->mddev, nodes); in md_bitmap_read_sb()
712 bmname(bitmap), err); in md_bitmap_read_sb()
715 bitmap->cluster_slot = md_cluster_ops->slot_number(bitmap->mddev); in md_bitmap_read_sb()
721 if (test_bit(BITMAP_STALE, &bitmap->flags)) in md_bitmap_read_sb()
722 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_read_sb()
723 bitmap->mddev->bitmap_info.chunksize = chunksize; in md_bitmap_read_sb()
724 bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep; in md_bitmap_read_sb()
725 bitmap->mddev->bitmap_info.max_write_behind = write_behind; in md_bitmap_read_sb()
726 bitmap->mddev->bitmap_info.nodes = nodes; in md_bitmap_read_sb()
727 if (bitmap->mddev->bitmap_info.space == 0 || in md_bitmap_read_sb()
728 bitmap->mddev->bitmap_info.space > sectors_reserved) in md_bitmap_read_sb()
729 bitmap->mddev->bitmap_info.space = sectors_reserved; in md_bitmap_read_sb()
731 md_bitmap_print_sb(bitmap); in md_bitmap_read_sb()
732 if (bitmap->cluster_slot < 0) in md_bitmap_read_sb()
733 md_cluster_stop(bitmap->mddev); in md_bitmap_read_sb()
866 static void md_bitmap_file_kick(struct bitmap *bitmap) in md_bitmap_file_kick() argument
870 if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) { in md_bitmap_file_kick()
871 md_bitmap_update_sb(bitmap); in md_bitmap_file_kick()
873 if (bitmap->storage.file) { in md_bitmap_file_kick()
876 ptr = file_path(bitmap->storage.file, in md_bitmap_file_kick()
880 bmname(bitmap), IS_ERR(ptr) ? "" : ptr); in md_bitmap_file_kick()
885 bmname(bitmap)); in md_bitmap_file_kick()
896 static inline void set_page_attr(struct bitmap *bitmap, int pnum, in set_page_attr() argument
899 set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in set_page_attr()
902 static inline void clear_page_attr(struct bitmap *bitmap, int pnum, in clear_page_attr() argument
905 clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in clear_page_attr()
908 static inline int test_page_attr(struct bitmap *bitmap, int pnum, in test_page_attr() argument
911 return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr); in test_page_attr()
914 static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum, in test_and_clear_page_attr() argument
918 bitmap->storage.filemap_attr); in test_and_clear_page_attr()
927 static void md_bitmap_file_set_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_set_bit() argument
932 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_set_bit()
933 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_file_set_bit()
936 if (mddev_is_clustered(bitmap->mddev)) in md_bitmap_file_set_bit()
937 node_offset = bitmap->cluster_slot * store->file_pages; in md_bitmap_file_set_bit()
939 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_set_bit()
942 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_set_bit()
946 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_set_bit()
953 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_DIRTY); in md_bitmap_file_set_bit()
956 static void md_bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_clear_bit() argument
961 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_clear_bit()
962 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_file_clear_bit()
965 if (mddev_is_clustered(bitmap->mddev)) in md_bitmap_file_clear_bit()
966 node_offset = bitmap->cluster_slot * store->file_pages; in md_bitmap_file_clear_bit()
968 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_clear_bit()
971 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_clear_bit()
973 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_clear_bit()
978 if (!test_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_NEEDWRITE)) { in md_bitmap_file_clear_bit()
979 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_PENDING); in md_bitmap_file_clear_bit()
980 bitmap->allclean = 0; in md_bitmap_file_clear_bit()
984 static int md_bitmap_file_test_bit(struct bitmap *bitmap, sector_t block) in md_bitmap_file_test_bit() argument
989 unsigned long chunk = block >> bitmap->counts.chunkshift; in md_bitmap_file_test_bit()
992 page = filemap_get_page(&bitmap->storage, chunk); in md_bitmap_file_test_bit()
995 bit = file_page_offset(&bitmap->storage, chunk); in md_bitmap_file_test_bit()
997 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_file_test_bit()
1009 void md_bitmap_unplug(struct bitmap *bitmap) in md_bitmap_unplug() argument
1015 if (!bitmap || !bitmap->storage.filemap || in md_bitmap_unplug()
1016 test_bit(BITMAP_STALE, &bitmap->flags)) in md_bitmap_unplug()
1021 for (i = 0; i < bitmap->storage.file_pages; i++) { in md_bitmap_unplug()
1022 if (!bitmap->storage.filemap) in md_bitmap_unplug()
1024 dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY); in md_bitmap_unplug()
1025 need_write = test_and_clear_page_attr(bitmap, i, in md_bitmap_unplug()
1029 md_bitmap_wait_writes(bitmap); in md_bitmap_unplug()
1030 if (bitmap->mddev->queue) in md_bitmap_unplug()
1031 blk_add_trace_msg(bitmap->mddev->queue, in md_bitmap_unplug()
1034 clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING); in md_bitmap_unplug()
1035 write_page(bitmap, bitmap->storage.filemap[i], 0); in md_bitmap_unplug()
1040 md_bitmap_wait_writes(bitmap); in md_bitmap_unplug()
1042 if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags)) in md_bitmap_unplug()
1043 md_bitmap_file_kick(bitmap); in md_bitmap_unplug()
1047 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
1059 static int md_bitmap_init_from_disk(struct bitmap *bitmap, sector_t start) in md_bitmap_init_from_disk() argument
1069 struct bitmap_storage *store = &bitmap->storage; in md_bitmap_init_from_disk()
1071 chunks = bitmap->counts.chunks; in md_bitmap_init_from_disk()
1074 if (!file && !bitmap->mddev->bitmap_info.offset) { in md_bitmap_init_from_disk()
1080 int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift) in md_bitmap_init_from_disk()
1082 md_bitmap_set_memory_bits(bitmap, in md_bitmap_init_from_disk()
1083 (sector_t)i << bitmap->counts.chunkshift, in md_bitmap_init_from_disk()
1089 outofdate = test_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_init_from_disk()
1091 pr_warn("%s: bitmap file is out of date, doing full recovery\n", bmname(bitmap)); in md_bitmap_init_from_disk()
1095 bmname(bitmap), in md_bitmap_init_from_disk()
1103 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_init_from_disk()
1106 if (mddev_is_clustered(bitmap->mddev)) in md_bitmap_init_from_disk()
1107 node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE)); in md_bitmap_init_from_disk()
1111 index = file_page_index(&bitmap->storage, i); in md_bitmap_init_from_disk()
1112 bit = file_page_offset(&bitmap->storage, i); in md_bitmap_init_from_disk()
1122 ret = read_page(file, index, bitmap, in md_bitmap_init_from_disk()
1126 bitmap->mddev, in md_bitmap_init_from_disk()
1127 bitmap->mddev->bitmap_info.offset, in md_bitmap_init_from_disk()
1145 write_page(bitmap, page, 1); in md_bitmap_init_from_disk()
1149 &bitmap->flags)) in md_bitmap_init_from_disk()
1154 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags)) in md_bitmap_init_from_disk()
1161 int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift in md_bitmap_init_from_disk()
1163 md_bitmap_set_memory_bits(bitmap, in md_bitmap_init_from_disk()
1164 (sector_t)i << bitmap->counts.chunkshift, in md_bitmap_init_from_disk()
1172 bmname(bitmap), store->file_pages, in md_bitmap_init_from_disk()
1179 bmname(bitmap), ret); in md_bitmap_init_from_disk()
1183 void md_bitmap_write_all(struct bitmap *bitmap) in md_bitmap_write_all() argument
1190 if (!bitmap || !bitmap->storage.filemap) in md_bitmap_write_all()
1192 if (bitmap->storage.file) in md_bitmap_write_all()
1196 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_write_all()
1197 set_page_attr(bitmap, i, in md_bitmap_write_all()
1199 bitmap->allclean = 0; in md_bitmap_write_all()
1202 static void md_bitmap_count_page(struct bitmap_counts *bitmap, in md_bitmap_count_page() argument
1205 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_count_page()
1207 bitmap->bp[page].count += inc; in md_bitmap_count_page()
1208 md_bitmap_checkfree(bitmap, page); in md_bitmap_count_page()
1211 static void md_bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset) in md_bitmap_set_pending() argument
1213 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_set_pending()
1215 struct bitmap_page *bp = &bitmap->bp[page]; in md_bitmap_set_pending()
1221 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
1232 struct bitmap *bitmap; in md_bitmap_daemon_work() local
1242 bitmap = mddev->bitmap; in md_bitmap_daemon_work()
1243 if (bitmap == NULL) { in md_bitmap_daemon_work()
1247 if (time_before(jiffies, bitmap->daemon_lastrun in md_bitmap_daemon_work()
1251 bitmap->daemon_lastrun = jiffies; in md_bitmap_daemon_work()
1252 if (bitmap->allclean) { in md_bitmap_daemon_work()
1256 bitmap->allclean = 1; in md_bitmap_daemon_work()
1258 if (bitmap->mddev->queue) in md_bitmap_daemon_work()
1259 blk_add_trace_msg(bitmap->mddev->queue, in md_bitmap_daemon_work()
1266 for (j = 0; j < bitmap->storage.file_pages; j++) in md_bitmap_daemon_work()
1267 if (test_and_clear_page_attr(bitmap, j, in md_bitmap_daemon_work()
1269 set_page_attr(bitmap, j, in md_bitmap_daemon_work()
1272 if (bitmap->need_sync && in md_bitmap_daemon_work()
1277 bitmap->need_sync = 0; in md_bitmap_daemon_work()
1278 if (bitmap->storage.filemap) { in md_bitmap_daemon_work()
1279 sb = kmap_atomic(bitmap->storage.sb_page); in md_bitmap_daemon_work()
1281 cpu_to_le64(bitmap->events_cleared); in md_bitmap_daemon_work()
1283 set_page_attr(bitmap, 0, in md_bitmap_daemon_work()
1290 counts = &bitmap->counts; in md_bitmap_daemon_work()
1311 if (*bmc == 1 && !bitmap->need_sync) { in md_bitmap_daemon_work()
1315 md_bitmap_file_clear_bit(bitmap, block); in md_bitmap_daemon_work()
1319 bitmap->allclean = 0; in md_bitmap_daemon_work()
1324 md_bitmap_wait_writes(bitmap); in md_bitmap_daemon_work()
1334 j < bitmap->storage.file_pages in md_bitmap_daemon_work()
1335 && !test_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_daemon_work()
1337 if (test_page_attr(bitmap, j, in md_bitmap_daemon_work()
1341 if (test_and_clear_page_attr(bitmap, j, in md_bitmap_daemon_work()
1343 write_page(bitmap, bitmap->storage.filemap[j], 0); in md_bitmap_daemon_work()
1348 if (bitmap->allclean == 0) in md_bitmap_daemon_work()
1354 static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap, in md_bitmap_get_counter() argument
1357 __releases(bitmap->lock) in md_bitmap_get_counter()
1358 __acquires(bitmap->lock) in md_bitmap_get_counter()
1364 sector_t chunk = offset >> bitmap->chunkshift; in md_bitmap_get_counter()
1370 err = md_bitmap_checkpage(bitmap, page, create, 0); in md_bitmap_get_counter()
1372 if (bitmap->bp[page].hijacked || in md_bitmap_get_counter()
1373 bitmap->bp[page].map == NULL) in md_bitmap_get_counter()
1374 csize = ((sector_t)1) << (bitmap->chunkshift + in md_bitmap_get_counter()
1377 csize = ((sector_t)1) << bitmap->chunkshift; in md_bitmap_get_counter()
1385 if (bitmap->bp[page].hijacked) { /* hijacked pointer */ in md_bitmap_get_counter()
1390 &bitmap->bp[page].map)[hi]; in md_bitmap_get_counter()
1393 &(bitmap->bp[page].map[pageoff]); in md_bitmap_get_counter()
1396 int md_bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind) in md_bitmap_startwrite() argument
1398 if (!bitmap) in md_bitmap_startwrite()
1403 atomic_inc(&bitmap->behind_writes); in md_bitmap_startwrite()
1404 bw = atomic_read(&bitmap->behind_writes); in md_bitmap_startwrite()
1405 if (bw > bitmap->behind_writes_used) in md_bitmap_startwrite()
1406 bitmap->behind_writes_used = bw; in md_bitmap_startwrite()
1409 bw, bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_startwrite()
1416 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1417 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 1); in md_bitmap_startwrite()
1419 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1429 prepare_to_wait(&bitmap->overflow_wait, &__wait, in md_bitmap_startwrite()
1431 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1433 finish_wait(&bitmap->overflow_wait, &__wait); in md_bitmap_startwrite()
1439 md_bitmap_file_set_bit(bitmap, offset); in md_bitmap_startwrite()
1440 md_bitmap_count_page(&bitmap->counts, offset, 1); in md_bitmap_startwrite()
1448 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_startwrite()
1460 void md_bitmap_endwrite(struct bitmap *bitmap, sector_t offset, in md_bitmap_endwrite() argument
1463 if (!bitmap) in md_bitmap_endwrite()
1466 if (atomic_dec_and_test(&bitmap->behind_writes)) in md_bitmap_endwrite()
1467 wake_up(&bitmap->behind_wait); in md_bitmap_endwrite()
1469 atomic_read(&bitmap->behind_writes), in md_bitmap_endwrite()
1470 bitmap->mddev->bitmap_info.max_write_behind); in md_bitmap_endwrite()
1478 spin_lock_irqsave(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1479 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 0); in md_bitmap_endwrite()
1481 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1485 if (success && !bitmap->mddev->degraded && in md_bitmap_endwrite()
1486 bitmap->events_cleared < bitmap->mddev->events) { in md_bitmap_endwrite()
1487 bitmap->events_cleared = bitmap->mddev->events; in md_bitmap_endwrite()
1488 bitmap->need_sync = 1; in md_bitmap_endwrite()
1489 sysfs_notify_dirent_safe(bitmap->sysfs_can_clear); in md_bitmap_endwrite()
1496 wake_up(&bitmap->overflow_wait); in md_bitmap_endwrite()
1500 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_endwrite()
1501 bitmap->allclean = 0; in md_bitmap_endwrite()
1503 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_endwrite()
1513 static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, in __bitmap_start_sync() argument
1518 if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */ in __bitmap_start_sync()
1522 spin_lock_irq(&bitmap->counts.lock); in __bitmap_start_sync()
1523 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); in __bitmap_start_sync()
1537 spin_unlock_irq(&bitmap->counts.lock); in __bitmap_start_sync()
1541 int md_bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, in md_bitmap_start_sync() argument
1556 rv |= __bitmap_start_sync(bitmap, offset, in md_bitmap_start_sync()
1565 void md_bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted) in md_bitmap_end_sync() argument
1570 if (bitmap == NULL) { in md_bitmap_end_sync()
1574 spin_lock_irqsave(&bitmap->counts.lock, flags); in md_bitmap_end_sync()
1575 bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0); in md_bitmap_end_sync()
1586 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_end_sync()
1587 bitmap->allclean = 0; in md_bitmap_end_sync()
1592 spin_unlock_irqrestore(&bitmap->counts.lock, flags); in md_bitmap_end_sync()
1596 void md_bitmap_close_sync(struct bitmap *bitmap) in md_bitmap_close_sync() argument
1604 if (!bitmap) in md_bitmap_close_sync()
1606 while (sector < bitmap->mddev->resync_max_sectors) { in md_bitmap_close_sync()
1607 md_bitmap_end_sync(bitmap, sector, &blocks, 0); in md_bitmap_close_sync()
1613 void md_bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector, bool force) in md_bitmap_cond_end_sync() argument
1618 if (!bitmap) in md_bitmap_cond_end_sync()
1621 bitmap->last_end_sync = jiffies; in md_bitmap_cond_end_sync()
1624 if (!force && time_before(jiffies, (bitmap->last_end_sync in md_bitmap_cond_end_sync()
1625 + bitmap->mddev->bitmap_info.daemon_sleep))) in md_bitmap_cond_end_sync()
1627 wait_event(bitmap->mddev->recovery_wait, in md_bitmap_cond_end_sync()
1628 atomic_read(&bitmap->mddev->recovery_active) == 0); in md_bitmap_cond_end_sync()
1630 bitmap->mddev->curr_resync_completed = sector; in md_bitmap_cond_end_sync()
1631 set_bit(MD_SB_CHANGE_CLEAN, &bitmap->mddev->sb_flags); in md_bitmap_cond_end_sync()
1632 sector &= ~((1ULL << bitmap->counts.chunkshift) - 1); in md_bitmap_cond_end_sync()
1634 while (s < sector && s < bitmap->mddev->resync_max_sectors) { in md_bitmap_cond_end_sync()
1635 md_bitmap_end_sync(bitmap, s, &blocks, 0); in md_bitmap_cond_end_sync()
1638 bitmap->last_end_sync = jiffies; in md_bitmap_cond_end_sync()
1639 sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed"); in md_bitmap_cond_end_sync()
1647 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_sync_with_cluster() local
1651 md_bitmap_end_sync(bitmap, sector, &blocks, 0); in md_bitmap_sync_with_cluster()
1657 md_bitmap_start_sync(bitmap, sector, &blocks, 0); in md_bitmap_sync_with_cluster()
1664 static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed) in md_bitmap_set_memory_bits() argument
1673 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1674 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &secs, 1); in md_bitmap_set_memory_bits()
1676 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1681 md_bitmap_count_page(&bitmap->counts, offset, 1); in md_bitmap_set_memory_bits()
1682 md_bitmap_set_pending(&bitmap->counts, offset); in md_bitmap_set_memory_bits()
1683 bitmap->allclean = 0; in md_bitmap_set_memory_bits()
1687 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_set_memory_bits()
1691 void md_bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e) in md_bitmap_dirty_bits() argument
1696 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift; in md_bitmap_dirty_bits()
1697 md_bitmap_set_memory_bits(bitmap, sec, 1); in md_bitmap_dirty_bits()
1698 md_bitmap_file_set_bit(bitmap, sec); in md_bitmap_dirty_bits()
1699 if (sec < bitmap->mddev->recovery_cp) in md_bitmap_dirty_bits()
1704 bitmap->mddev->recovery_cp = sec; in md_bitmap_dirty_bits()
1713 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_flush() local
1716 if (!bitmap) /* there was no bitmap */ in md_bitmap_flush()
1723 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1725 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1727 bitmap->daemon_lastrun -= sleep; in md_bitmap_flush()
1729 md_bitmap_update_sb(bitmap); in md_bitmap_flush()
1735 void md_bitmap_free(struct bitmap *bitmap) in md_bitmap_free() argument
1740 if (!bitmap) /* there was no bitmap */ in md_bitmap_free()
1743 if (bitmap->sysfs_can_clear) in md_bitmap_free()
1744 sysfs_put(bitmap->sysfs_can_clear); in md_bitmap_free()
1746 if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info && in md_bitmap_free()
1747 bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev)) in md_bitmap_free()
1748 md_cluster_stop(bitmap->mddev); in md_bitmap_free()
1751 wait_event(bitmap->write_wait, in md_bitmap_free()
1752 atomic_read(&bitmap->pending_writes) == 0); in md_bitmap_free()
1755 md_bitmap_file_unmap(&bitmap->storage); in md_bitmap_free()
1757 bp = bitmap->counts.bp; in md_bitmap_free()
1758 pages = bitmap->counts.pages; in md_bitmap_free()
1767 kfree(bitmap); in md_bitmap_free()
1773 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_wait_behind_writes() local
1776 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { in md_bitmap_wait_behind_writes()
1780 wait_event(bitmap->behind_wait, in md_bitmap_wait_behind_writes()
1781 atomic_read(&bitmap->behind_writes) == 0); in md_bitmap_wait_behind_writes()
1787 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_destroy() local
1789 if (!bitmap) /* there was no bitmap */ in md_bitmap_destroy()
1798 mddev->bitmap = NULL; /* disconnect from the md device */ in md_bitmap_destroy()
1804 md_bitmap_free(bitmap); in md_bitmap_destroy()
1812 struct bitmap *md_bitmap_create(struct mddev *mddev, int slot) in md_bitmap_create()
1814 struct bitmap *bitmap; in md_bitmap_create() local
1830 bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL); in md_bitmap_create()
1831 if (!bitmap) in md_bitmap_create()
1834 spin_lock_init(&bitmap->counts.lock); in md_bitmap_create()
1835 atomic_set(&bitmap->pending_writes, 0); in md_bitmap_create()
1836 init_waitqueue_head(&bitmap->write_wait); in md_bitmap_create()
1837 init_waitqueue_head(&bitmap->overflow_wait); in md_bitmap_create()
1838 init_waitqueue_head(&bitmap->behind_wait); in md_bitmap_create()
1840 bitmap->mddev = mddev; in md_bitmap_create()
1841 bitmap->cluster_slot = slot; in md_bitmap_create()
1846 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear"); in md_bitmap_create()
1849 bitmap->sysfs_can_clear = NULL; in md_bitmap_create()
1851 bitmap->storage.file = file; in md_bitmap_create()
1867 err = md_bitmap_new_disk_sb(bitmap); in md_bitmap_create()
1869 err = md_bitmap_read_sb(bitmap); in md_bitmap_create()
1881 bitmap->daemon_lastrun = jiffies; in md_bitmap_create()
1882 err = md_bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1); in md_bitmap_create()
1887 bitmap->counts.pages, bmname(bitmap)); in md_bitmap_create()
1889 err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0; in md_bitmap_create()
1893 return bitmap; in md_bitmap_create()
1895 md_bitmap_free(bitmap); in md_bitmap_create()
1904 struct bitmap *bitmap = mddev->bitmap; in md_bitmap_load() local
1907 if (!bitmap) in md_bitmap_load()
1923 md_bitmap_start_sync(bitmap, sector, &blocks, 0); in md_bitmap_load()
1926 md_bitmap_close_sync(bitmap); in md_bitmap_load()
1929 || bitmap->events_cleared == mddev->events) in md_bitmap_load()
1935 err = md_bitmap_init_from_disk(bitmap, start); in md_bitmap_load()
1940 clear_bit(BITMAP_STALE, &bitmap->flags); in md_bitmap_load()
1943 set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery); in md_bitmap_load()
1948 md_bitmap_update_sb(bitmap); in md_bitmap_load()
1950 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()
2026 void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap) in md_bitmap_status() argument
2031 if (!bitmap) in md_bitmap_status()
2034 counts = &bitmap->counts; in md_bitmap_status()
2036 chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10; in md_bitmap_status()
2043 chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize, in md_bitmap_status()
2045 if (bitmap->storage.file) { in md_bitmap_status()
2047 seq_file_path(seq, bitmap->storage.file, " \t\n"); in md_bitmap_status()
2053 int md_bitmap_resize(struct bitmap *bitmap, sector_t blocks, in md_bitmap_resize() argument
2076 if (bitmap->storage.file && !init) { in md_bitmap_resize()
2086 long space = bitmap->mddev->bitmap_info.space; in md_bitmap_resize()
2092 bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8); in md_bitmap_resize()
2093 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_resize()
2096 bitmap->mddev->bitmap_info.space = space; in md_bitmap_resize()
2098 chunkshift = bitmap->counts.chunkshift; in md_bitmap_resize()
2105 if (!bitmap->mddev->bitmap_info.external) in md_bitmap_resize()
2113 if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file) in md_bitmap_resize()
2115 !bitmap->mddev->bitmap_info.external, in md_bitmap_resize()
2116 mddev_is_clustered(bitmap->mddev) in md_bitmap_resize()
2117 ? bitmap->cluster_slot : 0); in md_bitmap_resize()
2133 bitmap->mddev->pers->quiesce(bitmap->mddev, 1); in md_bitmap_resize()
2135 store.file = bitmap->storage.file; in md_bitmap_resize()
2136 bitmap->storage.file = NULL; in md_bitmap_resize()
2138 if (store.sb_page && bitmap->storage.sb_page) in md_bitmap_resize()
2140 page_address(bitmap->storage.sb_page), in md_bitmap_resize()
2142 md_bitmap_file_unmap(&bitmap->storage); in md_bitmap_resize()
2143 bitmap->storage = store; in md_bitmap_resize()
2145 old_counts = bitmap->counts; in md_bitmap_resize()
2146 bitmap->counts.bp = new_bp; in md_bitmap_resize()
2147 bitmap->counts.pages = pages; in md_bitmap_resize()
2148 bitmap->counts.missing_pages = pages; in md_bitmap_resize()
2149 bitmap->counts.chunkshift = chunkshift; in md_bitmap_resize()
2150 bitmap->counts.chunks = chunks; in md_bitmap_resize()
2151 bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift + in md_bitmap_resize()
2157 spin_lock_irq(&bitmap->counts.lock); in md_bitmap_resize()
2159 if (mddev_is_clustered(bitmap->mddev)) { in md_bitmap_resize()
2162 ret = md_bitmap_checkpage(&bitmap->counts, page, 1, 1); in md_bitmap_resize()
2173 bitmap->counts.bp = old_counts.bp; in md_bitmap_resize()
2174 bitmap->counts.pages = old_counts.pages; in md_bitmap_resize()
2175 bitmap->counts.missing_pages = old_counts.pages; in md_bitmap_resize()
2176 bitmap->counts.chunkshift = old_counts.chunkshift; in md_bitmap_resize()
2177 bitmap->counts.chunks = old_counts.chunks; in md_bitmap_resize()
2178 bitmap->mddev->bitmap_info.chunksize = 1 << (old_counts.chunkshift + in md_bitmap_resize()
2184 bitmap->counts.bp[page].count += 1; in md_bitmap_resize()
2196 bmc_new = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); in md_bitmap_resize()
2203 md_bitmap_file_set_bit(bitmap, block); in md_bitmap_resize()
2207 md_bitmap_count_page(&bitmap->counts, block, 1); in md_bitmap_resize()
2208 md_bitmap_set_pending(&bitmap->counts, block); in md_bitmap_resize()
2217 if (bitmap->counts.bp != old_counts.bp) { in md_bitmap_resize()
2229 bmc = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1); in md_bitmap_resize()
2236 md_bitmap_count_page(&bitmap->counts, block, 1); in md_bitmap_resize()
2237 md_bitmap_set_pending(&bitmap->counts, block); in md_bitmap_resize()
2242 for (i = 0; i < bitmap->storage.file_pages; i++) in md_bitmap_resize()
2243 set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY); in md_bitmap_resize()
2245 spin_unlock_irq(&bitmap->counts.lock); in md_bitmap_resize()
2248 md_bitmap_unplug(bitmap); in md_bitmap_resize()
2249 bitmap->mddev->pers->quiesce(bitmap->mddev, 0); in md_bitmap_resize()
2290 if (mddev->bitmap || mddev->bitmap_info.file || in location_store()
2336 struct bitmap *bitmap; in location_store() local
2337 bitmap = md_bitmap_create(mddev, -1); in location_store()
2339 if (IS_ERR(bitmap)) in location_store()
2340 rv = PTR_ERR(bitmap); in location_store()
2342 mddev->bitmap = bitmap; in location_store()
2397 if (mddev->bitmap && in space_store()
2398 sectors < (mddev->bitmap->storage.bytes + 511) >> 9) in space_store()
2490 md_bitmap_update_sb(mddev->bitmap); in backlog_store()
2509 if (mddev->bitmap) in chunksize_store()
2534 if (mddev->bitmap || in metadata_store()
2555 if (mddev->bitmap) in can_clear_show()
2556 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ? in can_clear_show()
2566 if (mddev->bitmap == NULL) in can_clear_store()
2569 mddev->bitmap->need_sync = 1; in can_clear_store()
2573 mddev->bitmap->need_sync = 0; in can_clear_store()
2587 if (mddev->bitmap == NULL) in behind_writes_used_show()
2591 mddev->bitmap->behind_writes_used); in behind_writes_used_show()
2599 if (mddev->bitmap) in behind_writes_used_reset()
2600 mddev->bitmap->behind_writes_used = 0; in behind_writes_used_reset()