File: ddt_log.c

package info (click to toggle)
zfs-linux 2.3.4~git20250812.3b64a96-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 70,688 kB
  • sloc: ansic: 393,668; sh: 68,068; asm: 47,734; python: 8,160; makefile: 5,125; perl: 859; sed: 41
file content (779 lines) | stat: -rw-r--r-- 21,850 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// SPDX-License-Identifier: CDDL-1.0
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or https://opensource.org/licenses/CDDL-1.0.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2023, Klara Inc.
 */

#include <sys/zfs_context.h>
#include <sys/spa.h>
#include <sys/ddt.h>
#include <sys/dmu_tx.h>
#include <sys/dmu.h>
#include <sys/ddt_impl.h>
#include <sys/dnode.h>
#include <sys/dbuf.h>
#include <sys/zap.h>
#include <sys/zio_checksum.h>

/*
 * No more than this many txgs before swapping logs.
 */
uint_t zfs_dedup_log_txg_max = 8;

/*
 * Max memory for the log AVL trees. If zfs_dedup_log_mem_max is zero at module
 * load, it will be set to zfs_dedup_log_mem_max_percent% of total memory.
 */
uint64_t zfs_dedup_log_mem_max = 0;
uint_t zfs_dedup_log_mem_max_percent = 1;


static kmem_cache_t *ddt_log_entry_flat_cache;
static kmem_cache_t *ddt_log_entry_trad_cache;

#define	DDT_LOG_ENTRY_FLAT_SIZE	\
	(sizeof (ddt_log_entry_t) + DDT_FLAT_PHYS_SIZE)
#define	DDT_LOG_ENTRY_TRAD_SIZE	\
	(sizeof (ddt_log_entry_t) + DDT_TRAD_PHYS_SIZE)

#define	DDT_LOG_ENTRY_SIZE(ddt)	\
	_DDT_PHYS_SWITCH(ddt, DDT_LOG_ENTRY_FLAT_SIZE, DDT_LOG_ENTRY_TRAD_SIZE)

void
ddt_log_init(void)
{
	ddt_log_entry_flat_cache = kmem_cache_create("ddt_log_entry_flat_cache",
	    DDT_LOG_ENTRY_FLAT_SIZE, 0, NULL, NULL, NULL, NULL, NULL, 0);
	ddt_log_entry_trad_cache = kmem_cache_create("ddt_log_entry_trad_cache",
	    DDT_LOG_ENTRY_TRAD_SIZE, 0, NULL, NULL, NULL, NULL, NULL, 0);

	/*
	 * Max memory for log AVL entries. At least 1M, because we need
	 * something (that's ~3800 entries per tree). They can say 100% if they
	 * want; it just means they're at the mercy of the the txg flush limit.
	 */
	if (zfs_dedup_log_mem_max == 0) {
		zfs_dedup_log_mem_max_percent =
		    MIN(zfs_dedup_log_mem_max_percent, 100);
		zfs_dedup_log_mem_max = (physmem * PAGESIZE) *
		    zfs_dedup_log_mem_max_percent / 100;
	}
	zfs_dedup_log_mem_max = MAX(zfs_dedup_log_mem_max, 1*1024*1024);
}

void
ddt_log_fini(void)
{
	kmem_cache_destroy(ddt_log_entry_trad_cache);
	kmem_cache_destroy(ddt_log_entry_flat_cache);
}

static void
ddt_log_name(ddt_t *ddt, char *name, uint_t n)
{
	snprintf(name, DDT_NAMELEN, DMU_POOL_DDT_LOG,
	    zio_checksum_table[ddt->ddt_checksum].ci_name, n);
}

static void
ddt_log_update_header(ddt_t *ddt, ddt_log_t *ddl, dmu_tx_t *tx)
{
	dmu_buf_t *db;
	VERIFY0(dmu_bonus_hold(ddt->ddt_os, ddl->ddl_object, FTAG, &db));
	dmu_buf_will_dirty(db, tx);

	ddt_log_header_t *hdr = (ddt_log_header_t *)db->db_data;
	DLH_SET_VERSION(hdr, 1);
	DLH_SET_FLAGS(hdr, ddl->ddl_flags);
	hdr->dlh_length = ddl->ddl_length;
	hdr->dlh_first_txg = ddl->ddl_first_txg;
	hdr->dlh_checkpoint = ddl->ddl_checkpoint;

	dmu_buf_rele(db, FTAG);
}

static void
ddt_log_create_one(ddt_t *ddt, ddt_log_t *ddl, uint_t n, dmu_tx_t *tx)
{
	ASSERT3U(ddt->ddt_dir_object, >, 0);
	ASSERT3U(ddl->ddl_object, ==, 0);

	char name[DDT_NAMELEN];
	ddt_log_name(ddt, name, n);

	ddl->ddl_object = dmu_object_alloc(ddt->ddt_os,
	    DMU_OTN_UINT64_METADATA, SPA_OLD_MAXBLOCKSIZE,
	    DMU_OTN_UINT64_METADATA, sizeof (ddt_log_header_t), tx);
	VERIFY0(zap_add(ddt->ddt_os, ddt->ddt_dir_object, name,
	    sizeof (uint64_t), 1, &ddl->ddl_object, tx));
	ddl->ddl_length = 0;
	ddl->ddl_first_txg = tx->tx_txg;
	ddt_log_update_header(ddt, ddl, tx);
}

static void
ddt_log_create(ddt_t *ddt, dmu_tx_t *tx)
{
	ddt_log_create_one(ddt, ddt->ddt_log_active, 0, tx);
	ddt_log_create_one(ddt, ddt->ddt_log_flushing, 1, tx);
}

static void
ddt_log_destroy_one(ddt_t *ddt, ddt_log_t *ddl, uint_t n, dmu_tx_t *tx)
{
	ASSERT3U(ddt->ddt_dir_object, >, 0);

	if (ddl->ddl_object == 0)
		return;

	ASSERT0(ddl->ddl_length);

	char name[DDT_NAMELEN];
	ddt_log_name(ddt, name, n);

	VERIFY0(zap_remove(ddt->ddt_os, ddt->ddt_dir_object, name, tx));
	VERIFY0(dmu_object_free(ddt->ddt_os, ddl->ddl_object, tx));

	ddl->ddl_object = 0;
}

void
ddt_log_destroy(ddt_t *ddt, dmu_tx_t *tx)
{
	ddt_log_destroy_one(ddt, ddt->ddt_log_active, 0, tx);
	ddt_log_destroy_one(ddt, ddt->ddt_log_flushing, 1, tx);
}

static void
ddt_log_update_stats(ddt_t *ddt)
{
	/*
	 * Log object stats. We count the number of live entries in the log
	 * tree, even if there are more than on disk, and even if the same
	 * entry is on both append and flush trees, because that's more what
	 * the user expects to see. This does mean the on-disk size is not
	 * really correlated with the number of entries, but I don't think
	 * that's reasonable to expect anyway.
	 */
	dmu_object_info_t doi;
	uint64_t nblocks;
	dmu_object_info(ddt->ddt_os, ddt->ddt_log_active->ddl_object, &doi);
	nblocks = doi.doi_physical_blocks_512;
	dmu_object_info(ddt->ddt_os, ddt->ddt_log_flushing->ddl_object, &doi);
	nblocks += doi.doi_physical_blocks_512;

	ddt_object_t *ddo = &ddt->ddt_log_stats;
	ddo->ddo_count =
	    avl_numnodes(&ddt->ddt_log_active->ddl_tree) +
	    avl_numnodes(&ddt->ddt_log_flushing->ddl_tree);
	ddo->ddo_mspace = ddo->ddo_count * DDT_LOG_ENTRY_SIZE(ddt);
	ddo->ddo_dspace = nblocks << 9;
}

void
ddt_log_begin(ddt_t *ddt, size_t nentries, dmu_tx_t *tx, ddt_log_update_t *dlu)
{
	ASSERT3U(nentries, >, 0);
	ASSERT3P(dlu->dlu_dbp, ==, NULL);

	if (ddt->ddt_log_active->ddl_object == 0)
		ddt_log_create(ddt, tx);

	/*
	 * We want to store as many entries as we can in a block, but never
	 * split an entry across block boundaries.
	 */
	size_t reclen = P2ALIGN_TYPED(
	    sizeof (ddt_log_record_t) + sizeof (ddt_log_record_entry_t) +
	    DDT_PHYS_SIZE(ddt), sizeof (uint64_t), size_t);
	ASSERT3U(reclen, <=, UINT16_MAX);
	dlu->dlu_reclen = reclen;

	VERIFY0(dnode_hold(ddt->ddt_os, ddt->ddt_log_active->ddl_object, FTAG,
	    &dlu->dlu_dn));
	dnode_set_storage_type(dlu->dlu_dn, DMU_OT_DDT_ZAP);

	uint64_t nblocks = howmany(nentries,
	    dlu->dlu_dn->dn_datablksz / dlu->dlu_reclen);
	uint64_t offset = ddt->ddt_log_active->ddl_length;
	uint64_t length = nblocks * dlu->dlu_dn->dn_datablksz;

	VERIFY0(dmu_buf_hold_array_by_dnode(dlu->dlu_dn, offset, length,
	    B_FALSE, FTAG, &dlu->dlu_ndbp, &dlu->dlu_dbp,
	    DMU_READ_NO_PREFETCH));

	dlu->dlu_tx = tx;
	dlu->dlu_block = dlu->dlu_offset = 0;
}

static ddt_log_entry_t *
ddt_log_alloc_entry(ddt_t *ddt)
{
	ddt_log_entry_t *ddle;

	if (ddt->ddt_flags & DDT_FLAG_FLAT) {
		ddle = kmem_cache_alloc(ddt_log_entry_flat_cache, KM_SLEEP);
		memset(ddle, 0, DDT_LOG_ENTRY_FLAT_SIZE);
	} else {
		ddle = kmem_cache_alloc(ddt_log_entry_trad_cache, KM_SLEEP);
		memset(ddle, 0, DDT_LOG_ENTRY_TRAD_SIZE);
	}

	return (ddle);
}

static void
ddt_log_update_entry(ddt_t *ddt, ddt_log_t *ddl, ddt_lightweight_entry_t *ddlwe)
{
	/* Create the log tree entry from a live or stored entry */
	avl_index_t where;
	ddt_log_entry_t *ddle =
	    avl_find(&ddl->ddl_tree, &ddlwe->ddlwe_key, &where);
	if (ddle == NULL) {
		ddle = ddt_log_alloc_entry(ddt);
		ddle->ddle_key = ddlwe->ddlwe_key;
		avl_insert(&ddl->ddl_tree, ddle, where);
	}
	ddle->ddle_type = ddlwe->ddlwe_type;
	ddle->ddle_class = ddlwe->ddlwe_class;
	memcpy(ddle->ddle_phys, &ddlwe->ddlwe_phys, DDT_PHYS_SIZE(ddt));
}

void
ddt_log_entry(ddt_t *ddt, ddt_lightweight_entry_t *ddlwe, ddt_log_update_t *dlu)
{
	ASSERT3U(dlu->dlu_dbp, !=, NULL);

	ddt_log_update_entry(ddt, ddt->ddt_log_active, ddlwe);
	ddt_histogram_add_entry(ddt, &ddt->ddt_log_histogram, ddlwe);

	/* Get our block */
	ASSERT3U(dlu->dlu_block, <, dlu->dlu_ndbp);
	dmu_buf_t *db = dlu->dlu_dbp[dlu->dlu_block];

	/*
	 * If this would take us past the end of the block, finish it and
	 * move to the next one.
	 */
	if (db->db_size < (dlu->dlu_offset + dlu->dlu_reclen)) {
		ASSERT3U(dlu->dlu_offset, >, 0);
		dmu_buf_fill_done(db, dlu->dlu_tx, B_FALSE);
		dlu->dlu_block++;
		dlu->dlu_offset = 0;
		ASSERT3U(dlu->dlu_block, <, dlu->dlu_ndbp);
		db = dlu->dlu_dbp[dlu->dlu_block];
	}

	/*
	 * If this is the first time touching the block, inform the DMU that
	 * we will fill it, and zero it out.
	 */
	if (dlu->dlu_offset == 0) {
		dmu_buf_will_fill(db, dlu->dlu_tx, B_FALSE);
		memset(db->db_data, 0, db->db_size);
	}

	/* Create the log record directly in the buffer */
	ddt_log_record_t *dlr = (db->db_data + dlu->dlu_offset);
	DLR_SET_TYPE(dlr, DLR_ENTRY);
	DLR_SET_RECLEN(dlr, dlu->dlu_reclen);
	DLR_SET_ENTRY_TYPE(dlr, ddlwe->ddlwe_type);
	DLR_SET_ENTRY_CLASS(dlr, ddlwe->ddlwe_class);

	ddt_log_record_entry_t *dlre =
	    (ddt_log_record_entry_t *)&dlr->dlr_payload;
	dlre->dlre_key = ddlwe->ddlwe_key;
	memcpy(dlre->dlre_phys, &ddlwe->ddlwe_phys, DDT_PHYS_SIZE(ddt));

	/* Advance offset for next record. */
	dlu->dlu_offset += dlu->dlu_reclen;
}

void
ddt_log_commit(ddt_t *ddt, ddt_log_update_t *dlu)
{
	ASSERT3U(dlu->dlu_dbp, !=, NULL);
	ASSERT3U(dlu->dlu_block+1, ==, dlu->dlu_ndbp);
	ASSERT3U(dlu->dlu_offset, >, 0);

	/*
	 * Close out the last block. Whatever we haven't used will be zeroed,
	 * which matches DLR_INVALID, so we can detect this during load.
	 */
	dmu_buf_fill_done(dlu->dlu_dbp[dlu->dlu_block], dlu->dlu_tx, B_FALSE);

	dmu_buf_rele_array(dlu->dlu_dbp, dlu->dlu_ndbp, FTAG);

	ddt->ddt_log_active->ddl_length +=
	    dlu->dlu_ndbp * (uint64_t)dlu->dlu_dn->dn_datablksz;
	dnode_rele(dlu->dlu_dn, FTAG);

	ddt_log_update_header(ddt, ddt->ddt_log_active, dlu->dlu_tx);

	memset(dlu, 0, sizeof (ddt_log_update_t));

	ddt_log_update_stats(ddt);
}

boolean_t
ddt_log_take_first(ddt_t *ddt, ddt_log_t *ddl, ddt_lightweight_entry_t *ddlwe)
{
	ddt_log_entry_t *ddle = avl_first(&ddl->ddl_tree);
	if (ddle == NULL)
		return (B_FALSE);

	DDT_LOG_ENTRY_TO_LIGHTWEIGHT(ddt, ddle, ddlwe);

	ddt_histogram_sub_entry(ddt, &ddt->ddt_log_histogram, ddlwe);

	avl_remove(&ddl->ddl_tree, ddle);
	kmem_cache_free(ddt->ddt_flags & DDT_FLAG_FLAT ?
	    ddt_log_entry_flat_cache : ddt_log_entry_trad_cache, ddle);

	return (B_TRUE);
}

boolean_t
ddt_log_remove_key(ddt_t *ddt, ddt_log_t *ddl, const ddt_key_t *ddk)
{
	ddt_log_entry_t *ddle = avl_find(&ddl->ddl_tree, ddk, NULL);
	if (ddle == NULL)
		return (B_FALSE);

	ddt_lightweight_entry_t ddlwe;
	DDT_LOG_ENTRY_TO_LIGHTWEIGHT(ddt, ddle, &ddlwe);
	ddt_histogram_sub_entry(ddt, &ddt->ddt_log_histogram, &ddlwe);

	avl_remove(&ddl->ddl_tree, ddle);
	kmem_cache_free(ddt->ddt_flags & DDT_FLAG_FLAT ?
	    ddt_log_entry_flat_cache : ddt_log_entry_trad_cache, ddle);

	return (B_TRUE);
}

boolean_t
ddt_log_find_key(ddt_t *ddt, const ddt_key_t *ddk,
    ddt_lightweight_entry_t *ddlwe)
{
	ddt_log_entry_t *ddle =
	    avl_find(&ddt->ddt_log_active->ddl_tree, ddk, NULL);
	if (!ddle)
		ddle = avl_find(&ddt->ddt_log_flushing->ddl_tree, ddk, NULL);
	if (!ddle)
		return (B_FALSE);
	if (ddlwe)
		DDT_LOG_ENTRY_TO_LIGHTWEIGHT(ddt, ddle, ddlwe);
	return (B_TRUE);
}

void
ddt_log_checkpoint(ddt_t *ddt, ddt_lightweight_entry_t *ddlwe, dmu_tx_t *tx)
{
	ddt_log_t *ddl = ddt->ddt_log_flushing;

	ASSERT3U(ddl->ddl_object, !=, 0);

#ifdef ZFS_DEBUG
	/*
	 * There should not be any entries on the log tree before the given
	 * checkpoint. Assert that this is the case.
	 */
	ddt_log_entry_t *ddle = avl_first(&ddl->ddl_tree);
	if (ddle != NULL)
		VERIFY3U(ddt_key_compare(&ddle->ddle_key, &ddlwe->ddlwe_key),
		    >, 0);
#endif

	ddl->ddl_flags |= DDL_FLAG_CHECKPOINT;
	ddl->ddl_checkpoint = ddlwe->ddlwe_key;
	ddt_log_update_header(ddt, ddl, tx);

	ddt_log_update_stats(ddt);
}

void
ddt_log_truncate(ddt_t *ddt, dmu_tx_t *tx)
{
	ddt_log_t *ddl = ddt->ddt_log_flushing;

	if (ddl->ddl_object == 0)
		return;

	ASSERT(avl_is_empty(&ddl->ddl_tree));

	/* Eject the entire object */
	dmu_free_range(ddt->ddt_os, ddl->ddl_object, 0, DMU_OBJECT_END, tx);

	ddl->ddl_length = 0;
	ddl->ddl_flags &= ~DDL_FLAG_CHECKPOINT;
	memset(&ddl->ddl_checkpoint, 0, sizeof (ddt_key_t));
	ddt_log_update_header(ddt, ddl, tx);

	ddt_log_update_stats(ddt);
}

boolean_t
ddt_log_swap(ddt_t *ddt, dmu_tx_t *tx)
{
	/* Swap the logs. The old flushing one must be empty */
	VERIFY(avl_is_empty(&ddt->ddt_log_flushing->ddl_tree));

	/*
	 * If there are still blocks on the flushing log, truncate it first.
	 * This can happen if there were entries on the flushing log that were
	 * removed in memory via ddt_lookup(); their vestigal remains are
	 * on disk.
	 */
	if (ddt->ddt_log_flushing->ddl_length > 0)
		ddt_log_truncate(ddt, tx);

	/*
	 * Swap policy. We swap the logs (and so begin flushing) when the
	 * active tree grows too large, or when we haven't swapped it in
	 * some amount of time, or if something has requested the logs be
	 * flushed ASAP (see ddt_walk_init()).
	 */

	/*
	 * The log tree is too large if the memory usage of its entries is over
	 * half of the memory limit. This effectively gives each log tree half
	 * the available memory.
	 */
	const boolean_t too_large =
	    (avl_numnodes(&ddt->ddt_log_active->ddl_tree) *
	    DDT_LOG_ENTRY_SIZE(ddt)) >= (zfs_dedup_log_mem_max >> 1);

	const boolean_t too_old =
	    tx->tx_txg >=
	    (ddt->ddt_log_active->ddl_first_txg +
	    MAX(1, zfs_dedup_log_txg_max));

	const boolean_t force =
	    ddt->ddt_log_active->ddl_first_txg <= ddt->ddt_flush_force_txg;

	if (!(too_large || too_old || force))
		return (B_FALSE);

	ddt_log_t *swap = ddt->ddt_log_active;
	ddt->ddt_log_active = ddt->ddt_log_flushing;
	ddt->ddt_log_flushing = swap;

	ASSERT(ddt->ddt_log_active->ddl_flags & DDL_FLAG_FLUSHING);
	ddt->ddt_log_active->ddl_flags &=
	    ~(DDL_FLAG_FLUSHING | DDL_FLAG_CHECKPOINT);

	ASSERT(!(ddt->ddt_log_flushing->ddl_flags & DDL_FLAG_FLUSHING));
	ddt->ddt_log_flushing->ddl_flags |= DDL_FLAG_FLUSHING;

	ddt->ddt_log_active->ddl_first_txg = tx->tx_txg;

	ddt_log_update_header(ddt, ddt->ddt_log_active, tx);
	ddt_log_update_header(ddt, ddt->ddt_log_flushing, tx);

	ddt_log_update_stats(ddt);

	return (B_TRUE);
}

static inline void
ddt_log_load_entry(ddt_t *ddt, ddt_log_t *ddl, ddt_log_record_t *dlr,
    const ddt_key_t *checkpoint)
{
	ASSERT3U(DLR_GET_TYPE(dlr), ==, DLR_ENTRY);

	ddt_log_record_entry_t *dlre =
	    (ddt_log_record_entry_t *)dlr->dlr_payload;
	if (checkpoint != NULL &&
	    ddt_key_compare(&dlre->dlre_key, checkpoint) <= 0) {
		/* Skip pre-checkpoint entries; they're already flushed. */
		return;
	}

	ddt_lightweight_entry_t ddlwe;
	ddlwe.ddlwe_type = DLR_GET_ENTRY_TYPE(dlr);
	ddlwe.ddlwe_class = DLR_GET_ENTRY_CLASS(dlr);

	ddlwe.ddlwe_key = dlre->dlre_key;
	memcpy(&ddlwe.ddlwe_phys, dlre->dlre_phys, DDT_PHYS_SIZE(ddt));

	ddt_log_update_entry(ddt, ddl, &ddlwe);
}

static void
ddt_log_empty(ddt_t *ddt, ddt_log_t *ddl)
{
	void *cookie = NULL;
	ddt_log_entry_t *ddle;
	IMPLY(ddt->ddt_version == UINT64_MAX, avl_is_empty(&ddl->ddl_tree));
	while ((ddle =
	    avl_destroy_nodes(&ddl->ddl_tree, &cookie)) != NULL) {
		kmem_cache_free(ddt->ddt_flags & DDT_FLAG_FLAT ?
		    ddt_log_entry_flat_cache : ddt_log_entry_trad_cache, ddle);
	}
	ASSERT(avl_is_empty(&ddl->ddl_tree));
}

static int
ddt_log_load_one(ddt_t *ddt, uint_t n)
{
	ASSERT3U(n, <, 2);

	ddt_log_t *ddl = &ddt->ddt_log[n];

	char name[DDT_NAMELEN];
	ddt_log_name(ddt, name, n);

	uint64_t obj;
	int err = zap_lookup(ddt->ddt_os, ddt->ddt_dir_object, name,
	    sizeof (uint64_t), 1, &obj);
	if (err == ENOENT)
		return (0);
	if (err != 0)
		return (err);

	dnode_t *dn;
	err = dnode_hold(ddt->ddt_os, obj, FTAG, &dn);
	if (err != 0)
		return (err);

	ddt_log_header_t hdr;
	dmu_buf_t *db;
	err = dmu_bonus_hold_by_dnode(dn, FTAG, &db, DMU_READ_NO_PREFETCH);
	if (err != 0) {
		dnode_rele(dn, FTAG);
		return (err);
	}
	memcpy(&hdr, db->db_data, sizeof (ddt_log_header_t));
	dmu_buf_rele(db, FTAG);

	if (DLH_GET_VERSION(&hdr) != 1) {
		dnode_rele(dn, FTAG);
		zfs_dbgmsg("ddt_log_load: spa=%s ddt_log=%s "
		    "unknown version=%llu", spa_name(ddt->ddt_spa), name,
		    (u_longlong_t)DLH_GET_VERSION(&hdr));
		return (SET_ERROR(EINVAL));
	}

	ddt_key_t *checkpoint = NULL;
	if (DLH_GET_FLAGS(&hdr) & DDL_FLAG_CHECKPOINT) {
		/*
		 * If the log has a checkpoint, then we can ignore any entries
		 * that have already been flushed.
		 */
		ASSERT(DLH_GET_FLAGS(&hdr) & DDL_FLAG_FLUSHING);
		checkpoint = &hdr.dlh_checkpoint;
	}

	if (hdr.dlh_length > 0) {
		dmu_prefetch_by_dnode(dn, 0, 0, hdr.dlh_length,
		    ZIO_PRIORITY_SYNC_READ);

		for (uint64_t offset = 0; offset < hdr.dlh_length;
		    offset += dn->dn_datablksz) {
			err = dmu_buf_hold_by_dnode(dn, offset, FTAG, &db,
			    DMU_READ_PREFETCH);
			if (err != 0) {
				dnode_rele(dn, FTAG);
				ddt_log_empty(ddt, ddl);
				return (err);
			}

			uint64_t boffset = 0;
			while (boffset < db->db_size) {
				ddt_log_record_t *dlr =
				    (ddt_log_record_t *)(db->db_data + boffset);

				/* Partially-filled block, skip the rest */
				if (DLR_GET_TYPE(dlr) == DLR_INVALID)
					break;

				switch (DLR_GET_TYPE(dlr)) {
				case DLR_ENTRY:
					ddt_log_load_entry(ddt, ddl, dlr,
					    checkpoint);
					break;

				default:
					dmu_buf_rele(db, FTAG);
					dnode_rele(dn, FTAG);
					ddt_log_empty(ddt, ddl);
					return (SET_ERROR(EINVAL));
				}

				boffset += DLR_GET_RECLEN(dlr);
			}

			dmu_buf_rele(db, FTAG);
		}
	}

	dnode_rele(dn, FTAG);

	ddl->ddl_object = obj;
	ddl->ddl_flags = DLH_GET_FLAGS(&hdr);
	ddl->ddl_length = hdr.dlh_length;
	ddl->ddl_first_txg = hdr.dlh_first_txg;

	if (ddl->ddl_flags & DDL_FLAG_FLUSHING)
		ddt->ddt_log_flushing = ddl;
	else
		ddt->ddt_log_active = ddl;

	return (0);
}

int
ddt_log_load(ddt_t *ddt)
{
	int err;

	if (spa_load_state(ddt->ddt_spa) == SPA_LOAD_TRYIMPORT) {
		/*
		 * The DDT is going to be freed again in a moment, so there's
		 * no point loading the log; it'll just slow down import.
		 */
		return (0);
	}

	ASSERT0(ddt->ddt_log[0].ddl_object);
	ASSERT0(ddt->ddt_log[1].ddl_object);
	if (ddt->ddt_dir_object == 0) {
		/*
		 * If we're configured but the containing dir doesn't exist
		 * yet, then the log object can't possibly exist either.
		 */
		ASSERT3U(ddt->ddt_version, !=, UINT64_MAX);
		return (SET_ERROR(ENOENT));
	}

	if ((err = ddt_log_load_one(ddt, 0)) != 0)
		return (err);
	if ((err = ddt_log_load_one(ddt, 1)) != 0)
		return (err);

	VERIFY3P(ddt->ddt_log_active, !=, ddt->ddt_log_flushing);
	VERIFY(!(ddt->ddt_log_active->ddl_flags & DDL_FLAG_FLUSHING));
	VERIFY(!(ddt->ddt_log_active->ddl_flags & DDL_FLAG_CHECKPOINT));
	VERIFY(ddt->ddt_log_flushing->ddl_flags & DDL_FLAG_FLUSHING);

	/*
	 * We have two finalisation tasks:
	 *
	 * - rebuild the histogram. We do this at the end rather than while
	 *   we're loading so we don't need to uncount and recount entries that
	 *   appear multiple times in the log.
	 *
	 * - remove entries from the flushing tree that are on both trees. This
	 *   happens when ddt_lookup() rehydrates an entry from the flushing
	 *   tree, as ddt_log_take_key() removes the entry from the in-memory
	 *   tree but doesn't remove it from disk.
	 */

	/*
	 * We don't technically need a config lock here, since there shouldn't
	 * be pool config changes during DDT load. dva_get_dsize_sync() via
	 * ddt_stat_generate() is expecting it though, and it won't hurt
	 * anything, so we take it.
	 */
	spa_config_enter(ddt->ddt_spa, SCL_STATE, FTAG, RW_READER);

	avl_tree_t *al = &ddt->ddt_log_active->ddl_tree;
	avl_tree_t *fl = &ddt->ddt_log_flushing->ddl_tree;
	ddt_log_entry_t *ae = avl_first(al);
	ddt_log_entry_t *fe = avl_first(fl);
	while (ae != NULL || fe != NULL) {
		ddt_log_entry_t *ddle;
		if (ae == NULL) {
			/* active exhausted, take flushing */
			ddle = fe;
			fe = AVL_NEXT(fl, fe);
		} else if (fe == NULL) {
			/* flushing exuhausted, take active */
			ddle = ae;
			ae = AVL_NEXT(al, ae);
		} else {
			/* compare active and flushing */
			int c = ddt_key_compare(&ae->ddle_key, &fe->ddle_key);
			if (c < 0) {
				/* active behind, take and advance */
				ddle = ae;
				ae = AVL_NEXT(al, ae);
			} else if (c > 0) {
				/* flushing behind, take and advance */
				ddle = fe;
				fe = AVL_NEXT(fl, fe);
			} else {
				/* match. remove from flushing, take active */
				ddle = fe;
				fe = AVL_NEXT(fl, fe);
				avl_remove(fl, ddle);

				ddle = ae;
				ae = AVL_NEXT(al, ae);
			}
		}

		ddt_lightweight_entry_t ddlwe;
		DDT_LOG_ENTRY_TO_LIGHTWEIGHT(ddt, ddle, &ddlwe);
		ddt_histogram_add_entry(ddt, &ddt->ddt_log_histogram, &ddlwe);
	}

	spa_config_exit(ddt->ddt_spa, SCL_STATE, FTAG);

	ddt_log_update_stats(ddt);

	return (0);
}

void
ddt_log_alloc(ddt_t *ddt)
{
	ASSERT3P(ddt->ddt_log_active, ==, NULL);
	ASSERT3P(ddt->ddt_log_flushing, ==, NULL);

	avl_create(&ddt->ddt_log[0].ddl_tree, ddt_key_compare,
	    sizeof (ddt_log_entry_t), offsetof(ddt_log_entry_t, ddle_node));
	avl_create(&ddt->ddt_log[1].ddl_tree, ddt_key_compare,
	    sizeof (ddt_log_entry_t), offsetof(ddt_log_entry_t, ddle_node));
	ddt->ddt_log_active = &ddt->ddt_log[0];
	ddt->ddt_log_flushing = &ddt->ddt_log[1];
	ddt->ddt_log_flushing->ddl_flags |= DDL_FLAG_FLUSHING;
}

void
ddt_log_free(ddt_t *ddt)
{
	ddt_log_empty(ddt, &ddt->ddt_log[0]);
	ddt_log_empty(ddt, &ddt->ddt_log[1]);
	avl_destroy(&ddt->ddt_log[0].ddl_tree);
	avl_destroy(&ddt->ddt_log[1].ddl_tree);
}

ZFS_MODULE_PARAM(zfs_dedup, zfs_dedup_, log_txg_max, UINT, ZMOD_RW,
	"Max transactions before starting to flush dedup logs");

ZFS_MODULE_PARAM(zfs_dedup, zfs_dedup_, log_mem_max, U64, ZMOD_RD,
	"Max memory for dedup logs");

ZFS_MODULE_PARAM(zfs_dedup, zfs_dedup_, log_mem_max_percent, UINT, ZMOD_RD,
	"Max memory for dedup logs, as % of total memory");