This source file includes following definitions.
- dmz_id
- dmz_start_sect
- dmz_start_block
- dmz_nr_chunks
- dmz_nr_rnd_zones
- dmz_nr_unmap_rnd_zones
- dmz_lock_map
- dmz_unlock_map
- dmz_lock_metadata
- dmz_unlock_metadata
- dmz_lock_flush
- dmz_unlock_flush
- dmz_alloc_mblock
- dmz_free_mblock
- dmz_insert_mblock
- dmz_get_mblock_fast
- dmz_mblock_bio_end_io
- dmz_get_mblock_slow
- dmz_shrink_mblock_cache
- dmz_mblock_shrinker_count
- dmz_mblock_shrinker_scan
- dmz_release_mblock
- dmz_get_mblock
- dmz_dirty_mblock
- dmz_write_mblock
- dmz_rdwr_block
- dmz_write_sb
- dmz_write_dirty_mblocks
- dmz_log_dirty_mblocks
- dmz_flush_metadata
- dmz_check_sb
- dmz_read_sb
- dmz_lookup_secondary_sb
- dmz_get_sb
- dmz_recover_mblocks
- dmz_load_sb
- dmz_init_zone
- dmz_drop_zones
- dmz_init_zones
- dmz_update_zone
- dmz_handle_seq_write_err
- dmz_get
- dmz_reset_zone
- dmz_load_mapping
- dmz_set_chunk_mapping
- __dmz_lru_zone
- dmz_lru_zone
- dmz_wait_for_free_zones
- dmz_lock_zone_reclaim
- dmz_unlock_zone_reclaim
- dmz_wait_for_reclaim
- dmz_get_rnd_zone_for_reclaim
- dmz_get_seq_zone_for_reclaim
- dmz_get_zone_for_reclaim
- dmz_get_chunk_mapping
- dmz_put_chunk_mapping
- dmz_get_chunk_buffer
- dmz_alloc_zone
- dmz_free_zone
- dmz_map_zone
- dmz_unmap_zone
- dmz_set_bits
- dmz_get_bitmap
- dmz_copy_valid_blocks
- dmz_merge_valid_blocks
- dmz_validate_blocks
- dmz_clear_bits
- dmz_invalidate_blocks
- dmz_test_block
- dmz_to_next_set_block
- dmz_block_valid
- dmz_first_valid_block
- dmz_count_bits
- dmz_get_zone_weight
- dmz_cleanup_metadata
- dmz_ctr_metadata
- dmz_dtr_metadata
- dmz_resume_metadata
1
2
3
4
5
6
7
8 #include "dm-zoned.h"
9
10 #include <linux/module.h>
11 #include <linux/crc32.h>
12 #include <linux/sched/mm.h>
13
14 #define DM_MSG_PREFIX "zoned metadata"
15
16
17
18
19 #define DMZ_META_VER 1
20
21
22
23
24 #define DMZ_MAGIC ((((unsigned int)('D')) << 24) | \
25 (((unsigned int)('Z')) << 16) | \
26 (((unsigned int)('B')) << 8) | \
27 ((unsigned int)('D')))
28
29
30
31
32
33
34
35
36
37
38
39
40
41 struct dmz_super {
42
43 __le32 magic;
44
45
46 __le32 version;
47
48
49 __le64 gen;
50
51
52 __le64 sb_block;
53
54
55 __le32 nr_meta_blocks;
56
57
58 __le32 nr_reserved_seq;
59
60
61 __le32 nr_chunks;
62
63
64 __le32 nr_map_blocks;
65
66
67 __le32 nr_bitmap_blocks;
68
69
70 __le32 crc;
71
72
73 u8 reserved[464];
74 };
75
76
77
78
79
80
81
82
83
84 struct dmz_map {
85 __le32 dzone_id;
86 __le32 bzone_id;
87 };
88
89
90
91
92 #define DMZ_MAP_ENTRIES (DMZ_BLOCK_SIZE / sizeof(struct dmz_map))
93 #define DMZ_MAP_ENTRIES_SHIFT (ilog2(DMZ_MAP_ENTRIES))
94 #define DMZ_MAP_ENTRIES_MASK (DMZ_MAP_ENTRIES - 1)
95 #define DMZ_MAP_UNMAPPED UINT_MAX
96
97
98
99
100 struct dmz_mblock {
101 struct rb_node node;
102 struct list_head link;
103 sector_t no;
104 unsigned int ref;
105 unsigned long state;
106 struct page *page;
107 void *data;
108 };
109
110
111
112
113 enum {
114 DMZ_META_DIRTY,
115 DMZ_META_READING,
116 DMZ_META_WRITING,
117 DMZ_META_ERROR,
118 };
119
120
121
122
123 struct dmz_sb {
124 sector_t block;
125 struct dmz_mblock *mblk;
126 struct dmz_super *sb;
127 };
128
129
130
131
132 struct dmz_metadata {
133 struct dmz_dev *dev;
134
135 sector_t zone_bitmap_size;
136 unsigned int zone_nr_bitmap_blocks;
137 unsigned int zone_bits_per_mblk;
138
139 unsigned int nr_bitmap_blocks;
140 unsigned int nr_map_blocks;
141
142 unsigned int nr_useable_zones;
143 unsigned int nr_meta_blocks;
144 unsigned int nr_meta_zones;
145 unsigned int nr_data_zones;
146 unsigned int nr_rnd_zones;
147 unsigned int nr_reserved_seq;
148 unsigned int nr_chunks;
149
150
151 struct dm_zone *zones;
152
153 struct dm_zone *sb_zone;
154 struct dmz_sb sb[2];
155 unsigned int mblk_primary;
156 u64 sb_gen;
157 unsigned int min_nr_mblks;
158 unsigned int max_nr_mblks;
159 atomic_t nr_mblks;
160 struct rw_semaphore mblk_sem;
161 struct mutex mblk_flush_lock;
162 spinlock_t mblk_lock;
163 struct rb_root mblk_rbtree;
164 struct list_head mblk_lru_list;
165 struct list_head mblk_dirty_list;
166 struct shrinker mblk_shrinker;
167
168
169 struct mutex map_lock;
170 struct dmz_mblock **map_mblk;
171 unsigned int nr_rnd;
172 atomic_t unmap_nr_rnd;
173 struct list_head unmap_rnd_list;
174 struct list_head map_rnd_list;
175
176 unsigned int nr_seq;
177 atomic_t unmap_nr_seq;
178 struct list_head unmap_seq_list;
179 struct list_head map_seq_list;
180
181 atomic_t nr_reserved_seq_zones;
182 struct list_head reserved_seq_zones_list;
183
184 wait_queue_head_t free_wq;
185 };
186
187
188
189
190 unsigned int dmz_id(struct dmz_metadata *zmd, struct dm_zone *zone)
191 {
192 return ((unsigned int)(zone - zmd->zones));
193 }
194
195 sector_t dmz_start_sect(struct dmz_metadata *zmd, struct dm_zone *zone)
196 {
197 return (sector_t)dmz_id(zmd, zone) << zmd->dev->zone_nr_sectors_shift;
198 }
199
200 sector_t dmz_start_block(struct dmz_metadata *zmd, struct dm_zone *zone)
201 {
202 return (sector_t)dmz_id(zmd, zone) << zmd->dev->zone_nr_blocks_shift;
203 }
204
205 unsigned int dmz_nr_chunks(struct dmz_metadata *zmd)
206 {
207 return zmd->nr_chunks;
208 }
209
210 unsigned int dmz_nr_rnd_zones(struct dmz_metadata *zmd)
211 {
212 return zmd->nr_rnd;
213 }
214
215 unsigned int dmz_nr_unmap_rnd_zones(struct dmz_metadata *zmd)
216 {
217 return atomic_read(&zmd->unmap_nr_rnd);
218 }
219
220
221
222
223
224 void dmz_lock_map(struct dmz_metadata *zmd)
225 {
226 mutex_lock(&zmd->map_lock);
227 }
228
229 void dmz_unlock_map(struct dmz_metadata *zmd)
230 {
231 mutex_unlock(&zmd->map_lock);
232 }
233
234
235
236
237
238
239
240
241 void dmz_lock_metadata(struct dmz_metadata *zmd)
242 {
243 down_read(&zmd->mblk_sem);
244 }
245
246 void dmz_unlock_metadata(struct dmz_metadata *zmd)
247 {
248 up_read(&zmd->mblk_sem);
249 }
250
251
252
253
254
255
256 void dmz_lock_flush(struct dmz_metadata *zmd)
257 {
258 mutex_lock(&zmd->mblk_flush_lock);
259 }
260
261 void dmz_unlock_flush(struct dmz_metadata *zmd)
262 {
263 mutex_unlock(&zmd->mblk_flush_lock);
264 }
265
266
267
268
269 static struct dmz_mblock *dmz_alloc_mblock(struct dmz_metadata *zmd,
270 sector_t mblk_no)
271 {
272 struct dmz_mblock *mblk = NULL;
273
274
275 if (zmd->max_nr_mblks && atomic_read(&zmd->nr_mblks) > zmd->max_nr_mblks) {
276 spin_lock(&zmd->mblk_lock);
277 mblk = list_first_entry_or_null(&zmd->mblk_lru_list,
278 struct dmz_mblock, link);
279 if (mblk) {
280 list_del_init(&mblk->link);
281 rb_erase(&mblk->node, &zmd->mblk_rbtree);
282 mblk->no = mblk_no;
283 }
284 spin_unlock(&zmd->mblk_lock);
285 if (mblk)
286 return mblk;
287 }
288
289
290 mblk = kmalloc(sizeof(struct dmz_mblock), GFP_NOIO);
291 if (!mblk)
292 return NULL;
293
294 mblk->page = alloc_page(GFP_NOIO);
295 if (!mblk->page) {
296 kfree(mblk);
297 return NULL;
298 }
299
300 RB_CLEAR_NODE(&mblk->node);
301 INIT_LIST_HEAD(&mblk->link);
302 mblk->ref = 0;
303 mblk->state = 0;
304 mblk->no = mblk_no;
305 mblk->data = page_address(mblk->page);
306
307 atomic_inc(&zmd->nr_mblks);
308
309 return mblk;
310 }
311
312
313
314
315 static void dmz_free_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
316 {
317 __free_pages(mblk->page, 0);
318 kfree(mblk);
319
320 atomic_dec(&zmd->nr_mblks);
321 }
322
323
324
325
326 static void dmz_insert_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
327 {
328 struct rb_root *root = &zmd->mblk_rbtree;
329 struct rb_node **new = &(root->rb_node), *parent = NULL;
330 struct dmz_mblock *b;
331
332
333 while (*new) {
334 b = container_of(*new, struct dmz_mblock, node);
335 parent = *new;
336 new = (b->no < mblk->no) ? &((*new)->rb_left) : &((*new)->rb_right);
337 }
338
339
340 rb_link_node(&mblk->node, parent, new);
341 rb_insert_color(&mblk->node, root);
342 }
343
344
345
346
347
348 static struct dmz_mblock *dmz_get_mblock_fast(struct dmz_metadata *zmd,
349 sector_t mblk_no)
350 {
351 struct rb_root *root = &zmd->mblk_rbtree;
352 struct rb_node *node = root->rb_node;
353 struct dmz_mblock *mblk;
354
355 while (node) {
356 mblk = container_of(node, struct dmz_mblock, node);
357 if (mblk->no == mblk_no) {
358
359
360
361
362 mblk->ref++;
363 if (mblk->ref == 1 &&
364 !test_bit(DMZ_META_DIRTY, &mblk->state))
365 list_del_init(&mblk->link);
366 return mblk;
367 }
368 node = (mblk->no < mblk_no) ? node->rb_left : node->rb_right;
369 }
370
371 return NULL;
372 }
373
374
375
376
377 static void dmz_mblock_bio_end_io(struct bio *bio)
378 {
379 struct dmz_mblock *mblk = bio->bi_private;
380 int flag;
381
382 if (bio->bi_status)
383 set_bit(DMZ_META_ERROR, &mblk->state);
384
385 if (bio_op(bio) == REQ_OP_WRITE)
386 flag = DMZ_META_WRITING;
387 else
388 flag = DMZ_META_READING;
389
390 clear_bit_unlock(flag, &mblk->state);
391 smp_mb__after_atomic();
392 wake_up_bit(&mblk->state, flag);
393
394 bio_put(bio);
395 }
396
397
398
399
400 static struct dmz_mblock *dmz_get_mblock_slow(struct dmz_metadata *zmd,
401 sector_t mblk_no)
402 {
403 struct dmz_mblock *mblk, *m;
404 sector_t block = zmd->sb[zmd->mblk_primary].block + mblk_no;
405 struct bio *bio;
406
407 if (dmz_bdev_is_dying(zmd->dev))
408 return ERR_PTR(-EIO);
409
410
411 mblk = dmz_alloc_mblock(zmd, mblk_no);
412 if (!mblk)
413 return ERR_PTR(-ENOMEM);
414
415 bio = bio_alloc(GFP_NOIO, 1);
416 if (!bio) {
417 dmz_free_mblock(zmd, mblk);
418 return ERR_PTR(-ENOMEM);
419 }
420
421 spin_lock(&zmd->mblk_lock);
422
423
424
425
426
427 m = dmz_get_mblock_fast(zmd, mblk_no);
428 if (m) {
429 spin_unlock(&zmd->mblk_lock);
430 dmz_free_mblock(zmd, mblk);
431 bio_put(bio);
432 return m;
433 }
434
435 mblk->ref++;
436 set_bit(DMZ_META_READING, &mblk->state);
437 dmz_insert_mblock(zmd, mblk);
438
439 spin_unlock(&zmd->mblk_lock);
440
441
442 bio->bi_iter.bi_sector = dmz_blk2sect(block);
443 bio_set_dev(bio, zmd->dev->bdev);
444 bio->bi_private = mblk;
445 bio->bi_end_io = dmz_mblock_bio_end_io;
446 bio_set_op_attrs(bio, REQ_OP_READ, REQ_META | REQ_PRIO);
447 bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
448 submit_bio(bio);
449
450 return mblk;
451 }
452
453
454
455
456 static unsigned long dmz_shrink_mblock_cache(struct dmz_metadata *zmd,
457 unsigned long limit)
458 {
459 struct dmz_mblock *mblk;
460 unsigned long count = 0;
461
462 if (!zmd->max_nr_mblks)
463 return 0;
464
465 while (!list_empty(&zmd->mblk_lru_list) &&
466 atomic_read(&zmd->nr_mblks) > zmd->min_nr_mblks &&
467 count < limit) {
468 mblk = list_first_entry(&zmd->mblk_lru_list,
469 struct dmz_mblock, link);
470 list_del_init(&mblk->link);
471 rb_erase(&mblk->node, &zmd->mblk_rbtree);
472 dmz_free_mblock(zmd, mblk);
473 count++;
474 }
475
476 return count;
477 }
478
479
480
481
482 static unsigned long dmz_mblock_shrinker_count(struct shrinker *shrink,
483 struct shrink_control *sc)
484 {
485 struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
486
487 return atomic_read(&zmd->nr_mblks);
488 }
489
490
491
492
493 static unsigned long dmz_mblock_shrinker_scan(struct shrinker *shrink,
494 struct shrink_control *sc)
495 {
496 struct dmz_metadata *zmd = container_of(shrink, struct dmz_metadata, mblk_shrinker);
497 unsigned long count;
498
499 spin_lock(&zmd->mblk_lock);
500 count = dmz_shrink_mblock_cache(zmd, sc->nr_to_scan);
501 spin_unlock(&zmd->mblk_lock);
502
503 return count ? count : SHRINK_STOP;
504 }
505
506
507
508
509 static void dmz_release_mblock(struct dmz_metadata *zmd,
510 struct dmz_mblock *mblk)
511 {
512
513 if (!mblk)
514 return;
515
516 spin_lock(&zmd->mblk_lock);
517
518 mblk->ref--;
519 if (mblk->ref == 0) {
520 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
521 rb_erase(&mblk->node, &zmd->mblk_rbtree);
522 dmz_free_mblock(zmd, mblk);
523 } else if (!test_bit(DMZ_META_DIRTY, &mblk->state)) {
524 list_add_tail(&mblk->link, &zmd->mblk_lru_list);
525 dmz_shrink_mblock_cache(zmd, 1);
526 }
527 }
528
529 spin_unlock(&zmd->mblk_lock);
530 }
531
532
533
534
535
536 static struct dmz_mblock *dmz_get_mblock(struct dmz_metadata *zmd,
537 sector_t mblk_no)
538 {
539 struct dmz_mblock *mblk;
540
541
542 spin_lock(&zmd->mblk_lock);
543 mblk = dmz_get_mblock_fast(zmd, mblk_no);
544 spin_unlock(&zmd->mblk_lock);
545
546 if (!mblk) {
547
548 mblk = dmz_get_mblock_slow(zmd, mblk_no);
549 if (IS_ERR(mblk))
550 return mblk;
551 }
552
553
554 wait_on_bit_io(&mblk->state, DMZ_META_READING,
555 TASK_UNINTERRUPTIBLE);
556 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
557 dmz_release_mblock(zmd, mblk);
558 dmz_check_bdev(zmd->dev);
559 return ERR_PTR(-EIO);
560 }
561
562 return mblk;
563 }
564
565
566
567
568 static void dmz_dirty_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk)
569 {
570 spin_lock(&zmd->mblk_lock);
571 if (!test_and_set_bit(DMZ_META_DIRTY, &mblk->state))
572 list_add_tail(&mblk->link, &zmd->mblk_dirty_list);
573 spin_unlock(&zmd->mblk_lock);
574 }
575
576
577
578
579 static int dmz_write_mblock(struct dmz_metadata *zmd, struct dmz_mblock *mblk,
580 unsigned int set)
581 {
582 sector_t block = zmd->sb[set].block + mblk->no;
583 struct bio *bio;
584
585 if (dmz_bdev_is_dying(zmd->dev))
586 return -EIO;
587
588 bio = bio_alloc(GFP_NOIO, 1);
589 if (!bio) {
590 set_bit(DMZ_META_ERROR, &mblk->state);
591 return -ENOMEM;
592 }
593
594 set_bit(DMZ_META_WRITING, &mblk->state);
595
596 bio->bi_iter.bi_sector = dmz_blk2sect(block);
597 bio_set_dev(bio, zmd->dev->bdev);
598 bio->bi_private = mblk;
599 bio->bi_end_io = dmz_mblock_bio_end_io;
600 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_META | REQ_PRIO);
601 bio_add_page(bio, mblk->page, DMZ_BLOCK_SIZE, 0);
602 submit_bio(bio);
603
604 return 0;
605 }
606
607
608
609
610 static int dmz_rdwr_block(struct dmz_metadata *zmd, int op, sector_t block,
611 struct page *page)
612 {
613 struct bio *bio;
614 int ret;
615
616 if (dmz_bdev_is_dying(zmd->dev))
617 return -EIO;
618
619 bio = bio_alloc(GFP_NOIO, 1);
620 if (!bio)
621 return -ENOMEM;
622
623 bio->bi_iter.bi_sector = dmz_blk2sect(block);
624 bio_set_dev(bio, zmd->dev->bdev);
625 bio_set_op_attrs(bio, op, REQ_SYNC | REQ_META | REQ_PRIO);
626 bio_add_page(bio, page, DMZ_BLOCK_SIZE, 0);
627 ret = submit_bio_wait(bio);
628 bio_put(bio);
629
630 if (ret)
631 dmz_check_bdev(zmd->dev);
632 return ret;
633 }
634
635
636
637
638 static int dmz_write_sb(struct dmz_metadata *zmd, unsigned int set)
639 {
640 sector_t block = zmd->sb[set].block;
641 struct dmz_mblock *mblk = zmd->sb[set].mblk;
642 struct dmz_super *sb = zmd->sb[set].sb;
643 u64 sb_gen = zmd->sb_gen + 1;
644 int ret;
645
646 sb->magic = cpu_to_le32(DMZ_MAGIC);
647 sb->version = cpu_to_le32(DMZ_META_VER);
648
649 sb->gen = cpu_to_le64(sb_gen);
650
651 sb->sb_block = cpu_to_le64(block);
652 sb->nr_meta_blocks = cpu_to_le32(zmd->nr_meta_blocks);
653 sb->nr_reserved_seq = cpu_to_le32(zmd->nr_reserved_seq);
654 sb->nr_chunks = cpu_to_le32(zmd->nr_chunks);
655
656 sb->nr_map_blocks = cpu_to_le32(zmd->nr_map_blocks);
657 sb->nr_bitmap_blocks = cpu_to_le32(zmd->nr_bitmap_blocks);
658
659 sb->crc = 0;
660 sb->crc = cpu_to_le32(crc32_le(sb_gen, (unsigned char *)sb, DMZ_BLOCK_SIZE));
661
662 ret = dmz_rdwr_block(zmd, REQ_OP_WRITE, block, mblk->page);
663 if (ret == 0)
664 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
665
666 return ret;
667 }
668
669
670
671
672 static int dmz_write_dirty_mblocks(struct dmz_metadata *zmd,
673 struct list_head *write_list,
674 unsigned int set)
675 {
676 struct dmz_mblock *mblk;
677 struct blk_plug plug;
678 int ret = 0, nr_mblks_submitted = 0;
679
680
681 blk_start_plug(&plug);
682 list_for_each_entry(mblk, write_list, link) {
683 ret = dmz_write_mblock(zmd, mblk, set);
684 if (ret)
685 break;
686 nr_mblks_submitted++;
687 }
688 blk_finish_plug(&plug);
689
690
691 list_for_each_entry(mblk, write_list, link) {
692 if (!nr_mblks_submitted)
693 break;
694 wait_on_bit_io(&mblk->state, DMZ_META_WRITING,
695 TASK_UNINTERRUPTIBLE);
696 if (test_bit(DMZ_META_ERROR, &mblk->state)) {
697 clear_bit(DMZ_META_ERROR, &mblk->state);
698 dmz_check_bdev(zmd->dev);
699 ret = -EIO;
700 }
701 nr_mblks_submitted--;
702 }
703
704
705 if (ret == 0)
706 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
707
708 return ret;
709 }
710
711
712
713
714 static int dmz_log_dirty_mblocks(struct dmz_metadata *zmd,
715 struct list_head *write_list)
716 {
717 unsigned int log_set = zmd->mblk_primary ^ 0x1;
718 int ret;
719
720
721 ret = dmz_write_dirty_mblocks(zmd, write_list, log_set);
722 if (ret)
723 return ret;
724
725
726
727
728
729 ret = dmz_write_sb(zmd, log_set);
730 if (ret)
731 return ret;
732
733 return 0;
734 }
735
736
737
738
739 int dmz_flush_metadata(struct dmz_metadata *zmd)
740 {
741 struct dmz_mblock *mblk;
742 struct list_head write_list;
743 int ret;
744
745 if (WARN_ON(!zmd))
746 return 0;
747
748 INIT_LIST_HEAD(&write_list);
749
750
751
752
753
754
755 down_write(&zmd->mblk_sem);
756
757
758
759
760
761 dmz_lock_flush(zmd);
762
763 if (dmz_bdev_is_dying(zmd->dev)) {
764 ret = -EIO;
765 goto out;
766 }
767
768
769 spin_lock(&zmd->mblk_lock);
770 list_splice_init(&zmd->mblk_dirty_list, &write_list);
771 spin_unlock(&zmd->mblk_lock);
772
773
774 if (list_empty(&write_list)) {
775 ret = blkdev_issue_flush(zmd->dev->bdev, GFP_NOIO, NULL);
776 goto err;
777 }
778
779
780
781
782
783
784 ret = dmz_log_dirty_mblocks(zmd, &write_list);
785 if (ret)
786 goto err;
787
788
789
790
791
792 ret = dmz_write_dirty_mblocks(zmd, &write_list, zmd->mblk_primary);
793 if (ret)
794 goto err;
795
796 ret = dmz_write_sb(zmd, zmd->mblk_primary);
797 if (ret)
798 goto err;
799
800 while (!list_empty(&write_list)) {
801 mblk = list_first_entry(&write_list, struct dmz_mblock, link);
802 list_del_init(&mblk->link);
803
804 spin_lock(&zmd->mblk_lock);
805 clear_bit(DMZ_META_DIRTY, &mblk->state);
806 if (mblk->ref == 0)
807 list_add_tail(&mblk->link, &zmd->mblk_lru_list);
808 spin_unlock(&zmd->mblk_lock);
809 }
810
811 zmd->sb_gen++;
812 out:
813 dmz_unlock_flush(zmd);
814 up_write(&zmd->mblk_sem);
815
816 return ret;
817
818 err:
819 if (!list_empty(&write_list)) {
820 spin_lock(&zmd->mblk_lock);
821 list_splice(&write_list, &zmd->mblk_dirty_list);
822 spin_unlock(&zmd->mblk_lock);
823 }
824 if (!dmz_check_bdev(zmd->dev))
825 ret = -EIO;
826 goto out;
827 }
828
829
830
831
832 static int dmz_check_sb(struct dmz_metadata *zmd, struct dmz_super *sb)
833 {
834 unsigned int nr_meta_zones, nr_data_zones;
835 struct dmz_dev *dev = zmd->dev;
836 u32 crc, stored_crc;
837 u64 gen;
838
839 gen = le64_to_cpu(sb->gen);
840 stored_crc = le32_to_cpu(sb->crc);
841 sb->crc = 0;
842 crc = crc32_le(gen, (unsigned char *)sb, DMZ_BLOCK_SIZE);
843 if (crc != stored_crc) {
844 dmz_dev_err(dev, "Invalid checksum (needed 0x%08x, got 0x%08x)",
845 crc, stored_crc);
846 return -ENXIO;
847 }
848
849 if (le32_to_cpu(sb->magic) != DMZ_MAGIC) {
850 dmz_dev_err(dev, "Invalid meta magic (needed 0x%08x, got 0x%08x)",
851 DMZ_MAGIC, le32_to_cpu(sb->magic));
852 return -ENXIO;
853 }
854
855 if (le32_to_cpu(sb->version) != DMZ_META_VER) {
856 dmz_dev_err(dev, "Invalid meta version (needed %d, got %d)",
857 DMZ_META_VER, le32_to_cpu(sb->version));
858 return -ENXIO;
859 }
860
861 nr_meta_zones = (le32_to_cpu(sb->nr_meta_blocks) + dev->zone_nr_blocks - 1)
862 >> dev->zone_nr_blocks_shift;
863 if (!nr_meta_zones ||
864 nr_meta_zones >= zmd->nr_rnd_zones) {
865 dmz_dev_err(dev, "Invalid number of metadata blocks");
866 return -ENXIO;
867 }
868
869 if (!le32_to_cpu(sb->nr_reserved_seq) ||
870 le32_to_cpu(sb->nr_reserved_seq) >= (zmd->nr_useable_zones - nr_meta_zones)) {
871 dmz_dev_err(dev, "Invalid number of reserved sequential zones");
872 return -ENXIO;
873 }
874
875 nr_data_zones = zmd->nr_useable_zones -
876 (nr_meta_zones * 2 + le32_to_cpu(sb->nr_reserved_seq));
877 if (le32_to_cpu(sb->nr_chunks) > nr_data_zones) {
878 dmz_dev_err(dev, "Invalid number of chunks %u / %u",
879 le32_to_cpu(sb->nr_chunks), nr_data_zones);
880 return -ENXIO;
881 }
882
883
884 zmd->nr_meta_blocks = le32_to_cpu(sb->nr_meta_blocks);
885 zmd->nr_reserved_seq = le32_to_cpu(sb->nr_reserved_seq);
886 zmd->nr_chunks = le32_to_cpu(sb->nr_chunks);
887 zmd->nr_map_blocks = le32_to_cpu(sb->nr_map_blocks);
888 zmd->nr_bitmap_blocks = le32_to_cpu(sb->nr_bitmap_blocks);
889 zmd->nr_meta_zones = nr_meta_zones;
890 zmd->nr_data_zones = nr_data_zones;
891
892 return 0;
893 }
894
895
896
897
898 static int dmz_read_sb(struct dmz_metadata *zmd, unsigned int set)
899 {
900 return dmz_rdwr_block(zmd, REQ_OP_READ, zmd->sb[set].block,
901 zmd->sb[set].mblk->page);
902 }
903
904
905
906
907
908
909 static int dmz_lookup_secondary_sb(struct dmz_metadata *zmd)
910 {
911 unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
912 struct dmz_mblock *mblk;
913 int i;
914
915
916 mblk = dmz_alloc_mblock(zmd, 0);
917 if (!mblk)
918 return -ENOMEM;
919
920 zmd->sb[1].mblk = mblk;
921 zmd->sb[1].sb = mblk->data;
922
923
924 zmd->sb[1].block = zmd->sb[0].block + zone_nr_blocks;
925 for (i = 0; i < zmd->nr_rnd_zones - 1; i++) {
926 if (dmz_read_sb(zmd, 1) != 0)
927 break;
928 if (le32_to_cpu(zmd->sb[1].sb->magic) == DMZ_MAGIC)
929 return 0;
930 zmd->sb[1].block += zone_nr_blocks;
931 }
932
933 dmz_free_mblock(zmd, mblk);
934 zmd->sb[1].mblk = NULL;
935
936 return -EIO;
937 }
938
939
940
941
942 static int dmz_get_sb(struct dmz_metadata *zmd, unsigned int set)
943 {
944 struct dmz_mblock *mblk;
945 int ret;
946
947
948 mblk = dmz_alloc_mblock(zmd, 0);
949 if (!mblk)
950 return -ENOMEM;
951
952 zmd->sb[set].mblk = mblk;
953 zmd->sb[set].sb = mblk->data;
954
955
956 ret = dmz_read_sb(zmd, set);
957 if (ret) {
958 dmz_free_mblock(zmd, mblk);
959 zmd->sb[set].mblk = NULL;
960 return ret;
961 }
962
963 return 0;
964 }
965
966
967
968
969 static int dmz_recover_mblocks(struct dmz_metadata *zmd, unsigned int dst_set)
970 {
971 unsigned int src_set = dst_set ^ 0x1;
972 struct page *page;
973 int i, ret;
974
975 dmz_dev_warn(zmd->dev, "Metadata set %u invalid: recovering", dst_set);
976
977 if (dst_set == 0)
978 zmd->sb[0].block = dmz_start_block(zmd, zmd->sb_zone);
979 else {
980 zmd->sb[1].block = zmd->sb[0].block +
981 (zmd->nr_meta_zones << zmd->dev->zone_nr_blocks_shift);
982 }
983
984 page = alloc_page(GFP_NOIO);
985 if (!page)
986 return -ENOMEM;
987
988
989 for (i = 1; i < zmd->nr_meta_blocks; i++) {
990 ret = dmz_rdwr_block(zmd, REQ_OP_READ,
991 zmd->sb[src_set].block + i, page);
992 if (ret)
993 goto out;
994 ret = dmz_rdwr_block(zmd, REQ_OP_WRITE,
995 zmd->sb[dst_set].block + i, page);
996 if (ret)
997 goto out;
998 }
999
1000
1001 if (!zmd->sb[dst_set].mblk) {
1002 zmd->sb[dst_set].mblk = dmz_alloc_mblock(zmd, 0);
1003 if (!zmd->sb[dst_set].mblk) {
1004 ret = -ENOMEM;
1005 goto out;
1006 }
1007 zmd->sb[dst_set].sb = zmd->sb[dst_set].mblk->data;
1008 }
1009
1010 ret = dmz_write_sb(zmd, dst_set);
1011 out:
1012 __free_pages(page, 0);
1013
1014 return ret;
1015 }
1016
1017
1018
1019
1020 static int dmz_load_sb(struct dmz_metadata *zmd)
1021 {
1022 bool sb_good[2] = {false, false};
1023 u64 sb_gen[2] = {0, 0};
1024 int ret;
1025
1026
1027 zmd->sb[0].block = dmz_start_block(zmd, zmd->sb_zone);
1028 ret = dmz_get_sb(zmd, 0);
1029 if (ret) {
1030 dmz_dev_err(zmd->dev, "Read primary super block failed");
1031 return ret;
1032 }
1033
1034 ret = dmz_check_sb(zmd, zmd->sb[0].sb);
1035
1036
1037 if (ret == 0) {
1038 sb_good[0] = true;
1039 zmd->sb[1].block = zmd->sb[0].block +
1040 (zmd->nr_meta_zones << zmd->dev->zone_nr_blocks_shift);
1041 ret = dmz_get_sb(zmd, 1);
1042 } else
1043 ret = dmz_lookup_secondary_sb(zmd);
1044
1045 if (ret) {
1046 dmz_dev_err(zmd->dev, "Read secondary super block failed");
1047 return ret;
1048 }
1049
1050 ret = dmz_check_sb(zmd, zmd->sb[1].sb);
1051 if (ret == 0)
1052 sb_good[1] = true;
1053
1054
1055 if (!sb_good[0] && !sb_good[1]) {
1056 dmz_dev_err(zmd->dev, "No valid super block found");
1057 return -EIO;
1058 }
1059
1060 if (sb_good[0])
1061 sb_gen[0] = le64_to_cpu(zmd->sb[0].sb->gen);
1062 else
1063 ret = dmz_recover_mblocks(zmd, 0);
1064
1065 if (sb_good[1])
1066 sb_gen[1] = le64_to_cpu(zmd->sb[1].sb->gen);
1067 else
1068 ret = dmz_recover_mblocks(zmd, 1);
1069
1070 if (ret) {
1071 dmz_dev_err(zmd->dev, "Recovery failed");
1072 return -EIO;
1073 }
1074
1075 if (sb_gen[0] >= sb_gen[1]) {
1076 zmd->sb_gen = sb_gen[0];
1077 zmd->mblk_primary = 0;
1078 } else {
1079 zmd->sb_gen = sb_gen[1];
1080 zmd->mblk_primary = 1;
1081 }
1082
1083 dmz_dev_debug(zmd->dev, "Using super block %u (gen %llu)",
1084 zmd->mblk_primary, zmd->sb_gen);
1085
1086 return 0;
1087 }
1088
1089
1090
1091
1092 static int dmz_init_zone(struct dmz_metadata *zmd, struct dm_zone *zone,
1093 struct blk_zone *blkz)
1094 {
1095 struct dmz_dev *dev = zmd->dev;
1096
1097
1098 if (blkz->len != dev->zone_nr_sectors) {
1099 if (blkz->start + blkz->len == dev->capacity)
1100 return 0;
1101 return -ENXIO;
1102 }
1103
1104 INIT_LIST_HEAD(&zone->link);
1105 atomic_set(&zone->refcount, 0);
1106 zone->chunk = DMZ_MAP_UNMAPPED;
1107
1108 if (blkz->type == BLK_ZONE_TYPE_CONVENTIONAL) {
1109 set_bit(DMZ_RND, &zone->flags);
1110 } else if (blkz->type == BLK_ZONE_TYPE_SEQWRITE_REQ ||
1111 blkz->type == BLK_ZONE_TYPE_SEQWRITE_PREF) {
1112 set_bit(DMZ_SEQ, &zone->flags);
1113 } else
1114 return -ENXIO;
1115
1116 if (blkz->cond == BLK_ZONE_COND_OFFLINE)
1117 set_bit(DMZ_OFFLINE, &zone->flags);
1118 else if (blkz->cond == BLK_ZONE_COND_READONLY)
1119 set_bit(DMZ_READ_ONLY, &zone->flags);
1120
1121 if (dmz_is_rnd(zone))
1122 zone->wp_block = 0;
1123 else
1124 zone->wp_block = dmz_sect2blk(blkz->wp - blkz->start);
1125
1126 if (!dmz_is_offline(zone) && !dmz_is_readonly(zone)) {
1127 zmd->nr_useable_zones++;
1128 if (dmz_is_rnd(zone)) {
1129 zmd->nr_rnd_zones++;
1130 if (!zmd->sb_zone) {
1131
1132 zmd->sb_zone = zone;
1133 }
1134 }
1135 }
1136
1137 return 0;
1138 }
1139
1140
1141
1142
1143 static void dmz_drop_zones(struct dmz_metadata *zmd)
1144 {
1145 kfree(zmd->zones);
1146 zmd->zones = NULL;
1147 }
1148
1149
1150
1151
1152
1153 #define DMZ_REPORT_NR_ZONES 4096
1154
1155
1156
1157
1158
1159 static int dmz_init_zones(struct dmz_metadata *zmd)
1160 {
1161 struct dmz_dev *dev = zmd->dev;
1162 struct dm_zone *zone;
1163 struct blk_zone *blkz;
1164 unsigned int nr_blkz;
1165 sector_t sector = 0;
1166 int i, ret = 0;
1167
1168
1169 zmd->zone_bitmap_size = dev->zone_nr_blocks >> 3;
1170 zmd->zone_nr_bitmap_blocks =
1171 max_t(sector_t, 1, zmd->zone_bitmap_size >> DMZ_BLOCK_SHIFT);
1172 zmd->zone_bits_per_mblk = min_t(sector_t, dev->zone_nr_blocks,
1173 DMZ_BLOCK_SIZE_BITS);
1174
1175
1176 zmd->zones = kcalloc(dev->nr_zones, sizeof(struct dm_zone), GFP_KERNEL);
1177 if (!zmd->zones)
1178 return -ENOMEM;
1179
1180 dmz_dev_info(dev, "Using %zu B for zone information",
1181 sizeof(struct dm_zone) * dev->nr_zones);
1182
1183
1184 nr_blkz = DMZ_REPORT_NR_ZONES;
1185 blkz = kcalloc(nr_blkz, sizeof(struct blk_zone), GFP_KERNEL);
1186 if (!blkz) {
1187 ret = -ENOMEM;
1188 goto out;
1189 }
1190
1191
1192
1193
1194
1195
1196
1197 zone = zmd->zones;
1198 while (sector < dev->capacity) {
1199
1200 nr_blkz = DMZ_REPORT_NR_ZONES;
1201 ret = blkdev_report_zones(dev->bdev, sector, blkz, &nr_blkz);
1202 if (ret) {
1203 dmz_dev_err(dev, "Report zones failed %d", ret);
1204 goto out;
1205 }
1206
1207 if (!nr_blkz)
1208 break;
1209
1210
1211 for (i = 0; i < nr_blkz; i++) {
1212 ret = dmz_init_zone(zmd, zone, &blkz[i]);
1213 if (ret)
1214 goto out;
1215 sector += dev->zone_nr_sectors;
1216 zone++;
1217 }
1218 }
1219
1220
1221 if (sector < dev->capacity) {
1222 dmz_dev_err(dev, "Failed to get correct zone information");
1223 ret = -ENXIO;
1224 }
1225 out:
1226 kfree(blkz);
1227 if (ret)
1228 dmz_drop_zones(zmd);
1229
1230 return ret;
1231 }
1232
1233
1234
1235
1236 static int dmz_update_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1237 {
1238 unsigned int nr_blkz = 1;
1239 unsigned int noio_flag;
1240 struct blk_zone blkz;
1241 int ret;
1242
1243
1244
1245
1246
1247
1248
1249 noio_flag = memalloc_noio_save();
1250 ret = blkdev_report_zones(zmd->dev->bdev, dmz_start_sect(zmd, zone),
1251 &blkz, &nr_blkz);
1252 memalloc_noio_restore(noio_flag);
1253 if (!nr_blkz)
1254 ret = -EIO;
1255 if (ret) {
1256 dmz_dev_err(zmd->dev, "Get zone %u report failed",
1257 dmz_id(zmd, zone));
1258 dmz_check_bdev(zmd->dev);
1259 return ret;
1260 }
1261
1262 clear_bit(DMZ_OFFLINE, &zone->flags);
1263 clear_bit(DMZ_READ_ONLY, &zone->flags);
1264 if (blkz.cond == BLK_ZONE_COND_OFFLINE)
1265 set_bit(DMZ_OFFLINE, &zone->flags);
1266 else if (blkz.cond == BLK_ZONE_COND_READONLY)
1267 set_bit(DMZ_READ_ONLY, &zone->flags);
1268
1269 if (dmz_is_seq(zone))
1270 zone->wp_block = dmz_sect2blk(blkz.wp - blkz.start);
1271 else
1272 zone->wp_block = 0;
1273
1274 return 0;
1275 }
1276
1277
1278
1279
1280
1281 static int dmz_handle_seq_write_err(struct dmz_metadata *zmd,
1282 struct dm_zone *zone)
1283 {
1284 unsigned int wp = 0;
1285 int ret;
1286
1287 wp = zone->wp_block;
1288 ret = dmz_update_zone(zmd, zone);
1289 if (ret)
1290 return ret;
1291
1292 dmz_dev_warn(zmd->dev, "Processing zone %u write error (zone wp %u/%u)",
1293 dmz_id(zmd, zone), zone->wp_block, wp);
1294
1295 if (zone->wp_block < wp) {
1296 dmz_invalidate_blocks(zmd, zone, zone->wp_block,
1297 wp - zone->wp_block);
1298 }
1299
1300 return 0;
1301 }
1302
1303 static struct dm_zone *dmz_get(struct dmz_metadata *zmd, unsigned int zone_id)
1304 {
1305 return &zmd->zones[zone_id];
1306 }
1307
1308
1309
1310
1311 static int dmz_reset_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1312 {
1313 int ret;
1314
1315
1316
1317
1318
1319 if (dmz_is_offline(zone) ||
1320 dmz_is_readonly(zone) ||
1321 dmz_is_rnd(zone))
1322 return 0;
1323
1324 if (!dmz_is_empty(zone) || dmz_seq_write_err(zone)) {
1325 struct dmz_dev *dev = zmd->dev;
1326
1327 ret = blkdev_reset_zones(dev->bdev,
1328 dmz_start_sect(zmd, zone),
1329 dev->zone_nr_sectors, GFP_NOIO);
1330 if (ret) {
1331 dmz_dev_err(dev, "Reset zone %u failed %d",
1332 dmz_id(zmd, zone), ret);
1333 return ret;
1334 }
1335 }
1336
1337
1338 clear_bit(DMZ_SEQ_WRITE_ERR, &zone->flags);
1339 zone->wp_block = 0;
1340
1341 return 0;
1342 }
1343
1344 static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone);
1345
1346
1347
1348
1349 static int dmz_load_mapping(struct dmz_metadata *zmd)
1350 {
1351 struct dmz_dev *dev = zmd->dev;
1352 struct dm_zone *dzone, *bzone;
1353 struct dmz_mblock *dmap_mblk = NULL;
1354 struct dmz_map *dmap;
1355 unsigned int i = 0, e = 0, chunk = 0;
1356 unsigned int dzone_id;
1357 unsigned int bzone_id;
1358
1359
1360 zmd->map_mblk = kcalloc(zmd->nr_map_blocks,
1361 sizeof(struct dmz_mblk *), GFP_KERNEL);
1362 if (!zmd->map_mblk)
1363 return -ENOMEM;
1364
1365
1366 while (chunk < zmd->nr_chunks) {
1367 if (!dmap_mblk) {
1368
1369 dmap_mblk = dmz_get_mblock(zmd, i + 1);
1370 if (IS_ERR(dmap_mblk))
1371 return PTR_ERR(dmap_mblk);
1372 zmd->map_mblk[i] = dmap_mblk;
1373 dmap = (struct dmz_map *) dmap_mblk->data;
1374 i++;
1375 e = 0;
1376 }
1377
1378
1379 dzone_id = le32_to_cpu(dmap[e].dzone_id);
1380 if (dzone_id == DMZ_MAP_UNMAPPED)
1381 goto next;
1382
1383 if (dzone_id >= dev->nr_zones) {
1384 dmz_dev_err(dev, "Chunk %u mapping: invalid data zone ID %u",
1385 chunk, dzone_id);
1386 return -EIO;
1387 }
1388
1389 dzone = dmz_get(zmd, dzone_id);
1390 set_bit(DMZ_DATA, &dzone->flags);
1391 dzone->chunk = chunk;
1392 dmz_get_zone_weight(zmd, dzone);
1393
1394 if (dmz_is_rnd(dzone))
1395 list_add_tail(&dzone->link, &zmd->map_rnd_list);
1396 else
1397 list_add_tail(&dzone->link, &zmd->map_seq_list);
1398
1399
1400 bzone_id = le32_to_cpu(dmap[e].bzone_id);
1401 if (bzone_id == DMZ_MAP_UNMAPPED)
1402 goto next;
1403
1404 if (bzone_id >= dev->nr_zones) {
1405 dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone ID %u",
1406 chunk, bzone_id);
1407 return -EIO;
1408 }
1409
1410 bzone = dmz_get(zmd, bzone_id);
1411 if (!dmz_is_rnd(bzone)) {
1412 dmz_dev_err(dev, "Chunk %u mapping: invalid buffer zone %u",
1413 chunk, bzone_id);
1414 return -EIO;
1415 }
1416
1417 set_bit(DMZ_DATA, &bzone->flags);
1418 set_bit(DMZ_BUF, &bzone->flags);
1419 bzone->chunk = chunk;
1420 bzone->bzone = dzone;
1421 dzone->bzone = bzone;
1422 dmz_get_zone_weight(zmd, bzone);
1423 list_add_tail(&bzone->link, &zmd->map_rnd_list);
1424 next:
1425 chunk++;
1426 e++;
1427 if (e >= DMZ_MAP_ENTRIES)
1428 dmap_mblk = NULL;
1429 }
1430
1431
1432
1433
1434
1435
1436 for (i = 0; i < dev->nr_zones; i++) {
1437 dzone = dmz_get(zmd, i);
1438 if (dmz_is_meta(dzone))
1439 continue;
1440
1441 if (dmz_is_rnd(dzone))
1442 zmd->nr_rnd++;
1443 else
1444 zmd->nr_seq++;
1445
1446 if (dmz_is_data(dzone)) {
1447
1448 continue;
1449 }
1450
1451
1452 set_bit(DMZ_DATA, &dzone->flags);
1453 dzone->chunk = DMZ_MAP_UNMAPPED;
1454 if (dmz_is_rnd(dzone)) {
1455 list_add_tail(&dzone->link, &zmd->unmap_rnd_list);
1456 atomic_inc(&zmd->unmap_nr_rnd);
1457 } else if (atomic_read(&zmd->nr_reserved_seq_zones) < zmd->nr_reserved_seq) {
1458 list_add_tail(&dzone->link, &zmd->reserved_seq_zones_list);
1459 atomic_inc(&zmd->nr_reserved_seq_zones);
1460 zmd->nr_seq--;
1461 } else {
1462 list_add_tail(&dzone->link, &zmd->unmap_seq_list);
1463 atomic_inc(&zmd->unmap_nr_seq);
1464 }
1465 }
1466
1467 return 0;
1468 }
1469
1470
1471
1472
1473 static void dmz_set_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk,
1474 unsigned int dzone_id, unsigned int bzone_id)
1475 {
1476 struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
1477 struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
1478 int map_idx = chunk & DMZ_MAP_ENTRIES_MASK;
1479
1480 dmap[map_idx].dzone_id = cpu_to_le32(dzone_id);
1481 dmap[map_idx].bzone_id = cpu_to_le32(bzone_id);
1482 dmz_dirty_mblock(zmd, dmap_mblk);
1483 }
1484
1485
1486
1487
1488
1489 static void __dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1490 {
1491 if (list_empty(&zone->link))
1492 return;
1493
1494 list_del_init(&zone->link);
1495 if (dmz_is_seq(zone)) {
1496
1497 list_add_tail(&zone->link, &zmd->map_seq_list);
1498 } else {
1499
1500 list_add_tail(&zone->link, &zmd->map_rnd_list);
1501 }
1502 }
1503
1504
1505
1506
1507
1508 static void dmz_lru_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1509 {
1510 __dmz_lru_zone(zmd, zone);
1511 if (zone->bzone)
1512 __dmz_lru_zone(zmd, zone->bzone);
1513 }
1514
1515
1516
1517
1518 static void dmz_wait_for_free_zones(struct dmz_metadata *zmd)
1519 {
1520 DEFINE_WAIT(wait);
1521
1522 prepare_to_wait(&zmd->free_wq, &wait, TASK_UNINTERRUPTIBLE);
1523 dmz_unlock_map(zmd);
1524 dmz_unlock_metadata(zmd);
1525
1526 io_schedule_timeout(HZ);
1527
1528 dmz_lock_metadata(zmd);
1529 dmz_lock_map(zmd);
1530 finish_wait(&zmd->free_wq, &wait);
1531 }
1532
1533
1534
1535
1536
1537
1538 int dmz_lock_zone_reclaim(struct dm_zone *zone)
1539 {
1540
1541 if (dmz_is_active(zone))
1542 return 0;
1543
1544 return !test_and_set_bit(DMZ_RECLAIM, &zone->flags);
1545 }
1546
1547
1548
1549
1550 void dmz_unlock_zone_reclaim(struct dm_zone *zone)
1551 {
1552 WARN_ON(dmz_is_active(zone));
1553 WARN_ON(!dmz_in_reclaim(zone));
1554
1555 clear_bit_unlock(DMZ_RECLAIM, &zone->flags);
1556 smp_mb__after_atomic();
1557 wake_up_bit(&zone->flags, DMZ_RECLAIM);
1558 }
1559
1560
1561
1562
1563 static void dmz_wait_for_reclaim(struct dmz_metadata *zmd, struct dm_zone *zone)
1564 {
1565 dmz_unlock_map(zmd);
1566 dmz_unlock_metadata(zmd);
1567 wait_on_bit_timeout(&zone->flags, DMZ_RECLAIM, TASK_UNINTERRUPTIBLE, HZ);
1568 dmz_lock_metadata(zmd);
1569 dmz_lock_map(zmd);
1570 }
1571
1572
1573
1574
1575 static struct dm_zone *dmz_get_rnd_zone_for_reclaim(struct dmz_metadata *zmd)
1576 {
1577 struct dm_zone *dzone = NULL;
1578 struct dm_zone *zone;
1579
1580 if (list_empty(&zmd->map_rnd_list))
1581 return ERR_PTR(-EBUSY);
1582
1583 list_for_each_entry(zone, &zmd->map_rnd_list, link) {
1584 if (dmz_is_buf(zone))
1585 dzone = zone->bzone;
1586 else
1587 dzone = zone;
1588 if (dmz_lock_zone_reclaim(dzone))
1589 return dzone;
1590 }
1591
1592 return ERR_PTR(-EBUSY);
1593 }
1594
1595
1596
1597
1598 static struct dm_zone *dmz_get_seq_zone_for_reclaim(struct dmz_metadata *zmd)
1599 {
1600 struct dm_zone *zone;
1601
1602 if (list_empty(&zmd->map_seq_list))
1603 return ERR_PTR(-EBUSY);
1604
1605 list_for_each_entry(zone, &zmd->map_seq_list, link) {
1606 if (!zone->bzone)
1607 continue;
1608 if (dmz_lock_zone_reclaim(zone))
1609 return zone;
1610 }
1611
1612 return ERR_PTR(-EBUSY);
1613 }
1614
1615
1616
1617
1618 struct dm_zone *dmz_get_zone_for_reclaim(struct dmz_metadata *zmd)
1619 {
1620 struct dm_zone *zone;
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630 dmz_lock_map(zmd);
1631 if (list_empty(&zmd->reserved_seq_zones_list))
1632 zone = dmz_get_seq_zone_for_reclaim(zmd);
1633 else
1634 zone = dmz_get_rnd_zone_for_reclaim(zmd);
1635 dmz_unlock_map(zmd);
1636
1637 return zone;
1638 }
1639
1640
1641
1642
1643
1644
1645
1646 struct dm_zone *dmz_get_chunk_mapping(struct dmz_metadata *zmd, unsigned int chunk, int op)
1647 {
1648 struct dmz_mblock *dmap_mblk = zmd->map_mblk[chunk >> DMZ_MAP_ENTRIES_SHIFT];
1649 struct dmz_map *dmap = (struct dmz_map *) dmap_mblk->data;
1650 int dmap_idx = chunk & DMZ_MAP_ENTRIES_MASK;
1651 unsigned int dzone_id;
1652 struct dm_zone *dzone = NULL;
1653 int ret = 0;
1654
1655 dmz_lock_map(zmd);
1656 again:
1657
1658 dzone_id = le32_to_cpu(dmap[dmap_idx].dzone_id);
1659 if (dzone_id == DMZ_MAP_UNMAPPED) {
1660
1661
1662
1663
1664 if (op != REQ_OP_WRITE)
1665 goto out;
1666
1667
1668 dzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
1669 if (!dzone) {
1670 if (dmz_bdev_is_dying(zmd->dev)) {
1671 dzone = ERR_PTR(-EIO);
1672 goto out;
1673 }
1674 dmz_wait_for_free_zones(zmd);
1675 goto again;
1676 }
1677
1678 dmz_map_zone(zmd, dzone, chunk);
1679
1680 } else {
1681
1682 dzone = dmz_get(zmd, dzone_id);
1683 if (dzone->chunk != chunk) {
1684 dzone = ERR_PTR(-EIO);
1685 goto out;
1686 }
1687
1688
1689 if (dmz_seq_write_err(dzone)) {
1690 ret = dmz_handle_seq_write_err(zmd, dzone);
1691 if (ret) {
1692 dzone = ERR_PTR(-EIO);
1693 goto out;
1694 }
1695 clear_bit(DMZ_SEQ_WRITE_ERR, &dzone->flags);
1696 }
1697 }
1698
1699
1700
1701
1702
1703
1704 if (dmz_in_reclaim(dzone)) {
1705 dmz_wait_for_reclaim(zmd, dzone);
1706 goto again;
1707 }
1708 dmz_activate_zone(dzone);
1709 dmz_lru_zone(zmd, dzone);
1710 out:
1711 dmz_unlock_map(zmd);
1712
1713 return dzone;
1714 }
1715
1716
1717
1718
1719
1720
1721
1722 void dmz_put_chunk_mapping(struct dmz_metadata *zmd, struct dm_zone *dzone)
1723 {
1724 struct dm_zone *bzone;
1725
1726 dmz_lock_map(zmd);
1727
1728 bzone = dzone->bzone;
1729 if (bzone) {
1730 if (dmz_weight(bzone))
1731 dmz_lru_zone(zmd, bzone);
1732 else {
1733
1734 dmz_unmap_zone(zmd, bzone);
1735 dmz_free_zone(zmd, bzone);
1736 bzone = NULL;
1737 }
1738 }
1739
1740
1741 dmz_deactivate_zone(dzone);
1742 if (dmz_is_active(dzone) || bzone || dmz_weight(dzone))
1743 dmz_lru_zone(zmd, dzone);
1744 else {
1745
1746 dmz_unmap_zone(zmd, dzone);
1747 dmz_free_zone(zmd, dzone);
1748 }
1749
1750 dmz_unlock_map(zmd);
1751 }
1752
1753
1754
1755
1756
1757 struct dm_zone *dmz_get_chunk_buffer(struct dmz_metadata *zmd,
1758 struct dm_zone *dzone)
1759 {
1760 struct dm_zone *bzone;
1761
1762 dmz_lock_map(zmd);
1763 again:
1764 bzone = dzone->bzone;
1765 if (bzone)
1766 goto out;
1767
1768
1769 bzone = dmz_alloc_zone(zmd, DMZ_ALLOC_RND);
1770 if (!bzone) {
1771 if (dmz_bdev_is_dying(zmd->dev)) {
1772 bzone = ERR_PTR(-EIO);
1773 goto out;
1774 }
1775 dmz_wait_for_free_zones(zmd);
1776 goto again;
1777 }
1778
1779
1780 dmz_set_chunk_mapping(zmd, dzone->chunk, dmz_id(zmd, dzone),
1781 dmz_id(zmd, bzone));
1782
1783 set_bit(DMZ_BUF, &bzone->flags);
1784 bzone->chunk = dzone->chunk;
1785 bzone->bzone = dzone;
1786 dzone->bzone = bzone;
1787 list_add_tail(&bzone->link, &zmd->map_rnd_list);
1788 out:
1789 dmz_unlock_map(zmd);
1790
1791 return bzone;
1792 }
1793
1794
1795
1796
1797
1798 struct dm_zone *dmz_alloc_zone(struct dmz_metadata *zmd, unsigned long flags)
1799 {
1800 struct list_head *list;
1801 struct dm_zone *zone;
1802
1803 if (flags & DMZ_ALLOC_RND)
1804 list = &zmd->unmap_rnd_list;
1805 else
1806 list = &zmd->unmap_seq_list;
1807 again:
1808 if (list_empty(list)) {
1809
1810
1811
1812
1813 if (!(flags & DMZ_ALLOC_RECLAIM) ||
1814 list_empty(&zmd->reserved_seq_zones_list))
1815 return NULL;
1816
1817 zone = list_first_entry(&zmd->reserved_seq_zones_list,
1818 struct dm_zone, link);
1819 list_del_init(&zone->link);
1820 atomic_dec(&zmd->nr_reserved_seq_zones);
1821 return zone;
1822 }
1823
1824 zone = list_first_entry(list, struct dm_zone, link);
1825 list_del_init(&zone->link);
1826
1827 if (dmz_is_rnd(zone))
1828 atomic_dec(&zmd->unmap_nr_rnd);
1829 else
1830 atomic_dec(&zmd->unmap_nr_seq);
1831
1832 if (dmz_is_offline(zone)) {
1833 dmz_dev_warn(zmd->dev, "Zone %u is offline", dmz_id(zmd, zone));
1834 zone = NULL;
1835 goto again;
1836 }
1837
1838 return zone;
1839 }
1840
1841
1842
1843
1844
1845 void dmz_free_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1846 {
1847
1848 if (dmz_is_seq(zone))
1849 dmz_reset_zone(zmd, zone);
1850
1851
1852 if (dmz_is_rnd(zone)) {
1853 list_add_tail(&zone->link, &zmd->unmap_rnd_list);
1854 atomic_inc(&zmd->unmap_nr_rnd);
1855 } else if (atomic_read(&zmd->nr_reserved_seq_zones) <
1856 zmd->nr_reserved_seq) {
1857 list_add_tail(&zone->link, &zmd->reserved_seq_zones_list);
1858 atomic_inc(&zmd->nr_reserved_seq_zones);
1859 } else {
1860 list_add_tail(&zone->link, &zmd->unmap_seq_list);
1861 atomic_inc(&zmd->unmap_nr_seq);
1862 }
1863
1864 wake_up_all(&zmd->free_wq);
1865 }
1866
1867
1868
1869
1870
1871 void dmz_map_zone(struct dmz_metadata *zmd, struct dm_zone *dzone,
1872 unsigned int chunk)
1873 {
1874
1875 dmz_set_chunk_mapping(zmd, chunk, dmz_id(zmd, dzone),
1876 DMZ_MAP_UNMAPPED);
1877 dzone->chunk = chunk;
1878 if (dmz_is_rnd(dzone))
1879 list_add_tail(&dzone->link, &zmd->map_rnd_list);
1880 else
1881 list_add_tail(&dzone->link, &zmd->map_seq_list);
1882 }
1883
1884
1885
1886
1887
1888 void dmz_unmap_zone(struct dmz_metadata *zmd, struct dm_zone *zone)
1889 {
1890 unsigned int chunk = zone->chunk;
1891 unsigned int dzone_id;
1892
1893 if (chunk == DMZ_MAP_UNMAPPED) {
1894
1895 return;
1896 }
1897
1898 if (test_and_clear_bit(DMZ_BUF, &zone->flags)) {
1899
1900
1901
1902
1903 dzone_id = dmz_id(zmd, zone->bzone);
1904 zone->bzone->bzone = NULL;
1905 zone->bzone = NULL;
1906
1907 } else {
1908
1909
1910
1911
1912 if (WARN_ON(zone->bzone)) {
1913 zone->bzone->bzone = NULL;
1914 zone->bzone = NULL;
1915 }
1916 dzone_id = DMZ_MAP_UNMAPPED;
1917 }
1918
1919 dmz_set_chunk_mapping(zmd, chunk, dzone_id, DMZ_MAP_UNMAPPED);
1920
1921 zone->chunk = DMZ_MAP_UNMAPPED;
1922 list_del_init(&zone->link);
1923 }
1924
1925
1926
1927
1928
1929 static unsigned int dmz_set_bits(unsigned long *bitmap,
1930 unsigned int bit, unsigned int nr_bits)
1931 {
1932 unsigned long *addr;
1933 unsigned int end = bit + nr_bits;
1934 unsigned int n = 0;
1935
1936 while (bit < end) {
1937 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
1938 ((end - bit) >= BITS_PER_LONG)) {
1939
1940 addr = bitmap + BIT_WORD(bit);
1941 if (*addr == 0) {
1942 *addr = ULONG_MAX;
1943 n += BITS_PER_LONG;
1944 bit += BITS_PER_LONG;
1945 continue;
1946 }
1947 }
1948
1949 if (!test_and_set_bit(bit, bitmap))
1950 n++;
1951 bit++;
1952 }
1953
1954 return n;
1955 }
1956
1957
1958
1959
1960 static struct dmz_mblock *dmz_get_bitmap(struct dmz_metadata *zmd,
1961 struct dm_zone *zone,
1962 sector_t chunk_block)
1963 {
1964 sector_t bitmap_block = 1 + zmd->nr_map_blocks +
1965 (sector_t)(dmz_id(zmd, zone) * zmd->zone_nr_bitmap_blocks) +
1966 (chunk_block >> DMZ_BLOCK_SHIFT_BITS);
1967
1968 return dmz_get_mblock(zmd, bitmap_block);
1969 }
1970
1971
1972
1973
1974 int dmz_copy_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
1975 struct dm_zone *to_zone)
1976 {
1977 struct dmz_mblock *from_mblk, *to_mblk;
1978 sector_t chunk_block = 0;
1979
1980
1981 while (chunk_block < zmd->dev->zone_nr_blocks) {
1982 from_mblk = dmz_get_bitmap(zmd, from_zone, chunk_block);
1983 if (IS_ERR(from_mblk))
1984 return PTR_ERR(from_mblk);
1985 to_mblk = dmz_get_bitmap(zmd, to_zone, chunk_block);
1986 if (IS_ERR(to_mblk)) {
1987 dmz_release_mblock(zmd, from_mblk);
1988 return PTR_ERR(to_mblk);
1989 }
1990
1991 memcpy(to_mblk->data, from_mblk->data, DMZ_BLOCK_SIZE);
1992 dmz_dirty_mblock(zmd, to_mblk);
1993
1994 dmz_release_mblock(zmd, to_mblk);
1995 dmz_release_mblock(zmd, from_mblk);
1996
1997 chunk_block += zmd->zone_bits_per_mblk;
1998 }
1999
2000 to_zone->weight = from_zone->weight;
2001
2002 return 0;
2003 }
2004
2005
2006
2007
2008
2009 int dmz_merge_valid_blocks(struct dmz_metadata *zmd, struct dm_zone *from_zone,
2010 struct dm_zone *to_zone, sector_t chunk_block)
2011 {
2012 unsigned int nr_blocks;
2013 int ret;
2014
2015
2016 while (chunk_block < zmd->dev->zone_nr_blocks) {
2017
2018 ret = dmz_first_valid_block(zmd, from_zone, &chunk_block);
2019 if (ret <= 0)
2020 return ret;
2021
2022 nr_blocks = ret;
2023 ret = dmz_validate_blocks(zmd, to_zone, chunk_block, nr_blocks);
2024 if (ret)
2025 return ret;
2026
2027 chunk_block += nr_blocks;
2028 }
2029
2030 return 0;
2031 }
2032
2033
2034
2035
2036 int dmz_validate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
2037 sector_t chunk_block, unsigned int nr_blocks)
2038 {
2039 unsigned int count, bit, nr_bits;
2040 unsigned int zone_nr_blocks = zmd->dev->zone_nr_blocks;
2041 struct dmz_mblock *mblk;
2042 unsigned int n = 0;
2043
2044 dmz_dev_debug(zmd->dev, "=> VALIDATE zone %u, block %llu, %u blocks",
2045 dmz_id(zmd, zone), (unsigned long long)chunk_block,
2046 nr_blocks);
2047
2048 WARN_ON(chunk_block + nr_blocks > zone_nr_blocks);
2049
2050 while (nr_blocks) {
2051
2052 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2053 if (IS_ERR(mblk))
2054 return PTR_ERR(mblk);
2055
2056
2057 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2058 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2059
2060 count = dmz_set_bits((unsigned long *)mblk->data, bit, nr_bits);
2061 if (count) {
2062 dmz_dirty_mblock(zmd, mblk);
2063 n += count;
2064 }
2065 dmz_release_mblock(zmd, mblk);
2066
2067 nr_blocks -= nr_bits;
2068 chunk_block += nr_bits;
2069 }
2070
2071 if (likely(zone->weight + n <= zone_nr_blocks))
2072 zone->weight += n;
2073 else {
2074 dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be <= %u",
2075 dmz_id(zmd, zone), zone->weight,
2076 zone_nr_blocks - n);
2077 zone->weight = zone_nr_blocks;
2078 }
2079
2080 return 0;
2081 }
2082
2083
2084
2085
2086
2087 static int dmz_clear_bits(unsigned long *bitmap, int bit, int nr_bits)
2088 {
2089 unsigned long *addr;
2090 int end = bit + nr_bits;
2091 int n = 0;
2092
2093 while (bit < end) {
2094 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2095 ((end - bit) >= BITS_PER_LONG)) {
2096
2097 addr = bitmap + BIT_WORD(bit);
2098 if (*addr == ULONG_MAX) {
2099 *addr = 0;
2100 n += BITS_PER_LONG;
2101 bit += BITS_PER_LONG;
2102 continue;
2103 }
2104 }
2105
2106 if (test_and_clear_bit(bit, bitmap))
2107 n++;
2108 bit++;
2109 }
2110
2111 return n;
2112 }
2113
2114
2115
2116
2117 int dmz_invalidate_blocks(struct dmz_metadata *zmd, struct dm_zone *zone,
2118 sector_t chunk_block, unsigned int nr_blocks)
2119 {
2120 unsigned int count, bit, nr_bits;
2121 struct dmz_mblock *mblk;
2122 unsigned int n = 0;
2123
2124 dmz_dev_debug(zmd->dev, "=> INVALIDATE zone %u, block %llu, %u blocks",
2125 dmz_id(zmd, zone), (u64)chunk_block, nr_blocks);
2126
2127 WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
2128
2129 while (nr_blocks) {
2130
2131 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2132 if (IS_ERR(mblk))
2133 return PTR_ERR(mblk);
2134
2135
2136 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2137 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2138
2139 count = dmz_clear_bits((unsigned long *)mblk->data,
2140 bit, nr_bits);
2141 if (count) {
2142 dmz_dirty_mblock(zmd, mblk);
2143 n += count;
2144 }
2145 dmz_release_mblock(zmd, mblk);
2146
2147 nr_blocks -= nr_bits;
2148 chunk_block += nr_bits;
2149 }
2150
2151 if (zone->weight >= n)
2152 zone->weight -= n;
2153 else {
2154 dmz_dev_warn(zmd->dev, "Zone %u: weight %u should be >= %u",
2155 dmz_id(zmd, zone), zone->weight, n);
2156 zone->weight = 0;
2157 }
2158
2159 return 0;
2160 }
2161
2162
2163
2164
2165 static int dmz_test_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2166 sector_t chunk_block)
2167 {
2168 struct dmz_mblock *mblk;
2169 int ret;
2170
2171 WARN_ON(chunk_block >= zmd->dev->zone_nr_blocks);
2172
2173
2174 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2175 if (IS_ERR(mblk))
2176 return PTR_ERR(mblk);
2177
2178
2179 ret = test_bit(chunk_block & DMZ_BLOCK_MASK_BITS,
2180 (unsigned long *) mblk->data) != 0;
2181
2182 dmz_release_mblock(zmd, mblk);
2183
2184 return ret;
2185 }
2186
2187
2188
2189
2190
2191 static int dmz_to_next_set_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2192 sector_t chunk_block, unsigned int nr_blocks,
2193 int set)
2194 {
2195 struct dmz_mblock *mblk;
2196 unsigned int bit, set_bit, nr_bits;
2197 unsigned int zone_bits = zmd->zone_bits_per_mblk;
2198 unsigned long *bitmap;
2199 int n = 0;
2200
2201 WARN_ON(chunk_block + nr_blocks > zmd->dev->zone_nr_blocks);
2202
2203 while (nr_blocks) {
2204
2205 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2206 if (IS_ERR(mblk))
2207 return PTR_ERR(mblk);
2208
2209
2210 bitmap = (unsigned long *) mblk->data;
2211 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2212 nr_bits = min(nr_blocks, zone_bits - bit);
2213 if (set)
2214 set_bit = find_next_bit(bitmap, zone_bits, bit);
2215 else
2216 set_bit = find_next_zero_bit(bitmap, zone_bits, bit);
2217 dmz_release_mblock(zmd, mblk);
2218
2219 n += set_bit - bit;
2220 if (set_bit < zone_bits)
2221 break;
2222
2223 nr_blocks -= nr_bits;
2224 chunk_block += nr_bits;
2225 }
2226
2227 return n;
2228 }
2229
2230
2231
2232
2233
2234 int dmz_block_valid(struct dmz_metadata *zmd, struct dm_zone *zone,
2235 sector_t chunk_block)
2236 {
2237 int valid;
2238
2239 valid = dmz_test_block(zmd, zone, chunk_block);
2240 if (valid <= 0)
2241 return valid;
2242
2243
2244 return dmz_to_next_set_block(zmd, zone, chunk_block,
2245 zmd->dev->zone_nr_blocks - chunk_block, 0);
2246 }
2247
2248
2249
2250
2251
2252
2253
2254 int dmz_first_valid_block(struct dmz_metadata *zmd, struct dm_zone *zone,
2255 sector_t *chunk_block)
2256 {
2257 sector_t start_block = *chunk_block;
2258 int ret;
2259
2260 ret = dmz_to_next_set_block(zmd, zone, start_block,
2261 zmd->dev->zone_nr_blocks - start_block, 1);
2262 if (ret < 0)
2263 return ret;
2264
2265 start_block += ret;
2266 *chunk_block = start_block;
2267
2268 return dmz_to_next_set_block(zmd, zone, start_block,
2269 zmd->dev->zone_nr_blocks - start_block, 0);
2270 }
2271
2272
2273
2274
2275 static int dmz_count_bits(void *bitmap, int bit, int nr_bits)
2276 {
2277 unsigned long *addr;
2278 int end = bit + nr_bits;
2279 int n = 0;
2280
2281 while (bit < end) {
2282 if (((bit & (BITS_PER_LONG - 1)) == 0) &&
2283 ((end - bit) >= BITS_PER_LONG)) {
2284 addr = (unsigned long *)bitmap + BIT_WORD(bit);
2285 if (*addr == ULONG_MAX) {
2286 n += BITS_PER_LONG;
2287 bit += BITS_PER_LONG;
2288 continue;
2289 }
2290 }
2291
2292 if (test_bit(bit, bitmap))
2293 n++;
2294 bit++;
2295 }
2296
2297 return n;
2298 }
2299
2300
2301
2302
2303 static void dmz_get_zone_weight(struct dmz_metadata *zmd, struct dm_zone *zone)
2304 {
2305 struct dmz_mblock *mblk;
2306 sector_t chunk_block = 0;
2307 unsigned int bit, nr_bits;
2308 unsigned int nr_blocks = zmd->dev->zone_nr_blocks;
2309 void *bitmap;
2310 int n = 0;
2311
2312 while (nr_blocks) {
2313
2314 mblk = dmz_get_bitmap(zmd, zone, chunk_block);
2315 if (IS_ERR(mblk)) {
2316 n = 0;
2317 break;
2318 }
2319
2320
2321 bitmap = mblk->data;
2322 bit = chunk_block & DMZ_BLOCK_MASK_BITS;
2323 nr_bits = min(nr_blocks, zmd->zone_bits_per_mblk - bit);
2324 n += dmz_count_bits(bitmap, bit, nr_bits);
2325
2326 dmz_release_mblock(zmd, mblk);
2327
2328 nr_blocks -= nr_bits;
2329 chunk_block += nr_bits;
2330 }
2331
2332 zone->weight = n;
2333 }
2334
2335
2336
2337
2338 static void dmz_cleanup_metadata(struct dmz_metadata *zmd)
2339 {
2340 struct rb_root *root;
2341 struct dmz_mblock *mblk, *next;
2342 int i;
2343
2344
2345 if (zmd->map_mblk) {
2346 for (i = 0; i < zmd->nr_map_blocks; i++)
2347 dmz_release_mblock(zmd, zmd->map_mblk[i]);
2348 kfree(zmd->map_mblk);
2349 zmd->map_mblk = NULL;
2350 }
2351
2352
2353 for (i = 0; i < 2; i++) {
2354 if (zmd->sb[i].mblk) {
2355 dmz_free_mblock(zmd, zmd->sb[i].mblk);
2356 zmd->sb[i].mblk = NULL;
2357 }
2358 }
2359
2360
2361 while (!list_empty(&zmd->mblk_dirty_list)) {
2362 mblk = list_first_entry(&zmd->mblk_dirty_list,
2363 struct dmz_mblock, link);
2364 dmz_dev_warn(zmd->dev, "mblock %llu still in dirty list (ref %u)",
2365 (u64)mblk->no, mblk->ref);
2366 list_del_init(&mblk->link);
2367 rb_erase(&mblk->node, &zmd->mblk_rbtree);
2368 dmz_free_mblock(zmd, mblk);
2369 }
2370
2371 while (!list_empty(&zmd->mblk_lru_list)) {
2372 mblk = list_first_entry(&zmd->mblk_lru_list,
2373 struct dmz_mblock, link);
2374 list_del_init(&mblk->link);
2375 rb_erase(&mblk->node, &zmd->mblk_rbtree);
2376 dmz_free_mblock(zmd, mblk);
2377 }
2378
2379
2380 root = &zmd->mblk_rbtree;
2381 rbtree_postorder_for_each_entry_safe(mblk, next, root, node) {
2382 dmz_dev_warn(zmd->dev, "mblock %llu ref %u still in rbtree",
2383 (u64)mblk->no, mblk->ref);
2384 mblk->ref = 0;
2385 dmz_free_mblock(zmd, mblk);
2386 }
2387
2388
2389 dmz_drop_zones(zmd);
2390
2391 mutex_destroy(&zmd->mblk_flush_lock);
2392 mutex_destroy(&zmd->map_lock);
2393 }
2394
2395
2396
2397
2398 int dmz_ctr_metadata(struct dmz_dev *dev, struct dmz_metadata **metadata)
2399 {
2400 struct dmz_metadata *zmd;
2401 unsigned int i, zid;
2402 struct dm_zone *zone;
2403 int ret;
2404
2405 zmd = kzalloc(sizeof(struct dmz_metadata), GFP_KERNEL);
2406 if (!zmd)
2407 return -ENOMEM;
2408
2409 zmd->dev = dev;
2410 zmd->mblk_rbtree = RB_ROOT;
2411 init_rwsem(&zmd->mblk_sem);
2412 mutex_init(&zmd->mblk_flush_lock);
2413 spin_lock_init(&zmd->mblk_lock);
2414 INIT_LIST_HEAD(&zmd->mblk_lru_list);
2415 INIT_LIST_HEAD(&zmd->mblk_dirty_list);
2416
2417 mutex_init(&zmd->map_lock);
2418 atomic_set(&zmd->unmap_nr_rnd, 0);
2419 INIT_LIST_HEAD(&zmd->unmap_rnd_list);
2420 INIT_LIST_HEAD(&zmd->map_rnd_list);
2421
2422 atomic_set(&zmd->unmap_nr_seq, 0);
2423 INIT_LIST_HEAD(&zmd->unmap_seq_list);
2424 INIT_LIST_HEAD(&zmd->map_seq_list);
2425
2426 atomic_set(&zmd->nr_reserved_seq_zones, 0);
2427 INIT_LIST_HEAD(&zmd->reserved_seq_zones_list);
2428
2429 init_waitqueue_head(&zmd->free_wq);
2430
2431
2432 ret = dmz_init_zones(zmd);
2433 if (ret)
2434 goto err;
2435
2436
2437 ret = dmz_load_sb(zmd);
2438 if (ret)
2439 goto err;
2440
2441
2442 zid = dmz_id(zmd, zmd->sb_zone);
2443 for (i = 0; i < zmd->nr_meta_zones << 1; i++) {
2444 zone = dmz_get(zmd, zid + i);
2445 if (!dmz_is_rnd(zone))
2446 goto err;
2447 set_bit(DMZ_META, &zone->flags);
2448 }
2449
2450
2451 ret = dmz_load_mapping(zmd);
2452 if (ret)
2453 goto err;
2454
2455
2456
2457
2458
2459
2460
2461 zmd->min_nr_mblks = 2 + zmd->nr_map_blocks + zmd->zone_nr_bitmap_blocks * 16;
2462 zmd->max_nr_mblks = zmd->min_nr_mblks + 512;
2463 zmd->mblk_shrinker.count_objects = dmz_mblock_shrinker_count;
2464 zmd->mblk_shrinker.scan_objects = dmz_mblock_shrinker_scan;
2465 zmd->mblk_shrinker.seeks = DEFAULT_SEEKS;
2466
2467
2468 ret = register_shrinker(&zmd->mblk_shrinker);
2469 if (ret) {
2470 dmz_dev_err(dev, "Register metadata cache shrinker failed");
2471 goto err;
2472 }
2473
2474 dmz_dev_info(dev, "Host-%s zoned block device",
2475 bdev_zoned_model(dev->bdev) == BLK_ZONED_HA ?
2476 "aware" : "managed");
2477 dmz_dev_info(dev, " %llu 512-byte logical sectors",
2478 (u64)dev->capacity);
2479 dmz_dev_info(dev, " %u zones of %llu 512-byte logical sectors",
2480 dev->nr_zones, (u64)dev->zone_nr_sectors);
2481 dmz_dev_info(dev, " %u metadata zones",
2482 zmd->nr_meta_zones * 2);
2483 dmz_dev_info(dev, " %u data zones for %u chunks",
2484 zmd->nr_data_zones, zmd->nr_chunks);
2485 dmz_dev_info(dev, " %u random zones (%u unmapped)",
2486 zmd->nr_rnd, atomic_read(&zmd->unmap_nr_rnd));
2487 dmz_dev_info(dev, " %u sequential zones (%u unmapped)",
2488 zmd->nr_seq, atomic_read(&zmd->unmap_nr_seq));
2489 dmz_dev_info(dev, " %u reserved sequential data zones",
2490 zmd->nr_reserved_seq);
2491
2492 dmz_dev_debug(dev, "Format:");
2493 dmz_dev_debug(dev, "%u metadata blocks per set (%u max cache)",
2494 zmd->nr_meta_blocks, zmd->max_nr_mblks);
2495 dmz_dev_debug(dev, " %u data zone mapping blocks",
2496 zmd->nr_map_blocks);
2497 dmz_dev_debug(dev, " %u bitmap blocks",
2498 zmd->nr_bitmap_blocks);
2499
2500 *metadata = zmd;
2501
2502 return 0;
2503 err:
2504 dmz_cleanup_metadata(zmd);
2505 kfree(zmd);
2506 *metadata = NULL;
2507
2508 return ret;
2509 }
2510
2511
2512
2513
2514 void dmz_dtr_metadata(struct dmz_metadata *zmd)
2515 {
2516 unregister_shrinker(&zmd->mblk_shrinker);
2517 dmz_cleanup_metadata(zmd);
2518 kfree(zmd);
2519 }
2520
2521
2522
2523
2524 int dmz_resume_metadata(struct dmz_metadata *zmd)
2525 {
2526 struct dmz_dev *dev = zmd->dev;
2527 struct dm_zone *zone;
2528 sector_t wp_block;
2529 unsigned int i;
2530 int ret;
2531
2532
2533 for (i = 0; i < dev->nr_zones; i++) {
2534 zone = dmz_get(zmd, i);
2535 if (!zone) {
2536 dmz_dev_err(dev, "Unable to get zone %u", i);
2537 return -EIO;
2538 }
2539
2540 wp_block = zone->wp_block;
2541
2542 ret = dmz_update_zone(zmd, zone);
2543 if (ret) {
2544 dmz_dev_err(dev, "Broken zone %u", i);
2545 return ret;
2546 }
2547
2548 if (dmz_is_offline(zone)) {
2549 dmz_dev_warn(dev, "Zone %u is offline", i);
2550 continue;
2551 }
2552
2553
2554 if (!dmz_is_seq(zone))
2555 zone->wp_block = 0;
2556 else if (zone->wp_block != wp_block) {
2557 dmz_dev_err(dev, "Zone %u: Invalid wp (%llu / %llu)",
2558 i, (u64)zone->wp_block, (u64)wp_block);
2559 zone->wp_block = wp_block;
2560 dmz_invalidate_blocks(zmd, zone, zone->wp_block,
2561 dev->zone_nr_blocks - zone->wp_block);
2562 }
2563 }
2564
2565 return 0;
2566 }