Path: blob/main/sys/contrib/openzfs/module/zfs/dmu_redact.c
107994 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2017, 2018 by Delphix. All rights reserved.23*/2425#include <sys/zfs_context.h>26#include <sys/txg.h>27#include <sys/dmu_objset.h>28#include <sys/dmu_traverse.h>29#include <sys/dmu_redact.h>30#include <sys/bqueue.h>31#include <sys/objlist.h>32#include <sys/dmu_tx.h>33#ifdef _KERNEL34#include <sys/zfs_vfsops.h>35#include <sys/zap.h>36#include <sys/zfs_znode.h>37#endif3839/*40* This controls the number of entries in the buffer the redaction_list_update41* synctask uses to buffer writes to the redaction list.42*/43static const int redact_sync_bufsize = 1024;4445/*46* Controls how often to update the redaction list when creating a redaction47* list.48*/49static const uint64_t redaction_list_update_interval_ns =501000 * 1000 * 1000ULL; /* 1s */5152/*53* This tunable controls the length of the queues that zfs redact worker threads54* use to communicate. If the dmu_redact_snap thread is blocking on these55* queues, this variable may need to be increased. If there is a significant56* slowdown at the start of a redact operation as these threads consume all the57* available IO resources, or the queues are consuming too much memory, this58* variable may need to be decreased.59*/60static const int zfs_redact_queue_length = 1024 * 1024;6162/*63* These tunables control the fill fraction of the queues by zfs redact. The64* fill fraction controls the frequency with which threads have to be65* cv_signaled. If a lot of cpu time is being spent on cv_signal, then these66* should be tuned down. If the queues empty before the signalled thread can67* catch up, then these should be tuned up.68*/69static const uint64_t zfs_redact_queue_ff = 20;7071struct redact_record {72bqueue_node_t ln;73boolean_t eos_marker; /* Marks the end of the stream */74uint64_t start_object;75uint64_t start_blkid;76uint64_t end_object;77uint64_t end_blkid;78uint8_t indblkshift;79uint32_t datablksz;80};8182struct redact_thread_arg {83bqueue_t q;84objset_t *os; /* Objset to traverse */85dsl_dataset_t *ds; /* Dataset to traverse */86struct redact_record *current_record;87int error_code;88boolean_t cancel;89zbookmark_phys_t resume;90objlist_t *deleted_objs;91uint64_t *num_blocks_visited;92uint64_t ignore_object; /* ignore further callbacks on this */93uint64_t txg; /* txg to traverse since */94};9596/*97* The redaction node is a wrapper around the redaction record that is used98* by the redaction merging thread to sort the records and determine overlaps.99*100* It contains two nodes; one sorts the records by their start_zb, and the other101* sorts the records by their end_zb.102*/103struct redact_node {104avl_node_t avl_node_start;105avl_node_t avl_node_end;106struct redact_record *record;107struct redact_thread_arg *rt_arg;108uint32_t thread_num;109};110111struct merge_data {112list_t md_redact_block_pending;113redact_block_phys_t md_coalesce_block;114uint64_t md_last_time;115redact_block_phys_t md_furthest[TXG_SIZE];116/* Lists of struct redact_block_list_node. */117list_t md_blocks[TXG_SIZE];118boolean_t md_synctask_txg[TXG_SIZE];119uint64_t md_latest_synctask_txg;120redaction_list_t *md_redaction_list;121};122123/*124* A wrapper around struct redact_block so it can be stored in a list_t.125*/126struct redact_block_list_node {127redact_block_phys_t block;128list_node_t node;129};130131/*132* We've found a new redaction candidate. In order to improve performance, we133* coalesce these blocks when they're adjacent to each other. This function134* handles that. If the new candidate block range is immediately after the135* range we're building, coalesce it into the range we're building. Otherwise,136* put the record we're building on the queue, and update the build pointer to137* point to the new record.138*/139static void140record_merge_enqueue(bqueue_t *q, struct redact_record **build,141struct redact_record *new)142{143if (new->eos_marker) {144if (*build != NULL)145bqueue_enqueue(q, *build, sizeof (**build));146bqueue_enqueue_flush(q, new, sizeof (*new));147return;148}149if (*build == NULL) {150*build = new;151return;152}153struct redact_record *curbuild = *build;154if ((curbuild->end_object == new->start_object &&155curbuild->end_blkid + 1 == new->start_blkid &&156curbuild->end_blkid != UINT64_MAX) ||157(curbuild->end_object + 1 == new->start_object &&158curbuild->end_blkid == UINT64_MAX && new->start_blkid == 0)) {159curbuild->end_object = new->end_object;160curbuild->end_blkid = new->end_blkid;161kmem_free(new, sizeof (*new));162} else {163bqueue_enqueue(q, curbuild, sizeof (*curbuild));164*build = new;165}166}167#ifdef _KERNEL168struct objnode {169avl_node_t node;170uint64_t obj;171};172173static int174objnode_compare(const void *o1, const void *o2)175{176const struct objnode *obj1 = o1;177const struct objnode *obj2 = o2;178if (obj1->obj < obj2->obj)179return (-1);180if (obj1->obj > obj2->obj)181return (1);182return (0);183}184185186static objlist_t *187zfs_get_deleteq(objset_t *os)188{189objlist_t *deleteq_objlist = objlist_create();190uint64_t deleteq_obj;191zap_cursor_t zc;192zap_attribute_t *za;193dmu_object_info_t doi;194195ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS);196VERIFY0(dmu_object_info(os, MASTER_NODE_OBJ, &doi));197ASSERT3U(doi.doi_type, ==, DMU_OT_MASTER_NODE);198199VERIFY0(zap_lookup(os, MASTER_NODE_OBJ,200ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj));201202/*203* In order to insert objects into the objlist, they must be in sorted204* order. We don't know what order we'll get them out of the ZAP in, so205* we insert them into and remove them from an avl_tree_t to sort them.206*/207avl_tree_t at;208avl_create(&at, objnode_compare, sizeof (struct objnode),209offsetof(struct objnode, node));210211za = zap_attribute_alloc();212for (zap_cursor_init(&zc, os, deleteq_obj);213zap_cursor_retrieve(&zc, za) == 0; zap_cursor_advance(&zc)) {214struct objnode *obj = kmem_zalloc(sizeof (*obj), KM_SLEEP);215obj->obj = za->za_first_integer;216avl_add(&at, obj);217}218zap_cursor_fini(&zc);219zap_attribute_free(za);220221struct objnode *next, *found = avl_first(&at);222while (found != NULL) {223next = AVL_NEXT(&at, found);224objlist_insert(deleteq_objlist, found->obj);225found = next;226}227228void *cookie = NULL;229while ((found = avl_destroy_nodes(&at, &cookie)) != NULL)230kmem_free(found, sizeof (*found));231avl_destroy(&at);232return (deleteq_objlist);233}234#endif235236/*237* This is the callback function to traverse_dataset for the redaction threads238* for dmu_redact_snap. This thread is responsible for creating redaction239* records for all the data that is modified by the snapshots we're redacting240* with respect to. Redaction records represent ranges of data that have been241* modified by one of the redaction snapshots, and are stored in the242* redact_record struct. We need to create redaction records for three243* cases:244*245* First, if there's a normal write, we need to create a redaction record for246* that block.247*248* Second, if there's a hole, we need to create a redaction record that covers249* the whole range of the hole. If the hole is in the meta-dnode, it must cover250* every block in all of the objects in the hole.251*252* Third, if there is a deleted object, we need to create a redaction record for253* all of the blocks in that object.254*/255static int256redact_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,257const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)258{259(void) spa, (void) zilog;260struct redact_thread_arg *rta = arg;261struct redact_record *record;262263ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||264zb->zb_object >= rta->resume.zb_object);265266if (rta->cancel)267return (SET_ERROR(EINTR));268269if (rta->ignore_object == zb->zb_object)270return (0);271272/*273* If we're visiting a dnode, we need to handle the case where the274* object has been deleted.275*/276if (zb->zb_level == ZB_DNODE_LEVEL) {277ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);278279if (zb->zb_object == 0)280return (0);281282/*283* If the object has been deleted, redact all of the blocks in284* it.285*/286if (dnp->dn_type == DMU_OT_NONE ||287objlist_exists(rta->deleted_objs, zb->zb_object)) {288rta->ignore_object = zb->zb_object;289record = kmem_zalloc(sizeof (struct redact_record),290KM_SLEEP);291292record->eos_marker = B_FALSE;293record->start_object = record->end_object =294zb->zb_object;295record->start_blkid = 0;296record->end_blkid = UINT64_MAX;297record_merge_enqueue(&rta->q,298&rta->current_record, record);299}300return (0);301} else if (zb->zb_level < 0) {302return (0);303} else if (zb->zb_level > 0 && !BP_IS_HOLE(bp)) {304/*305* If this is an indirect block, but not a hole, it doesn't306* provide any useful information for redaction, so ignore it.307*/308return (0);309}310311/*312* At this point, there are two options left for the type of block we're313* looking at. Either this is a hole (which could be in the dnode or314* the meta-dnode), or it's a level 0 block of some sort. If it's a315* hole, we create a redaction record that covers the whole range. If316* the hole is in a dnode, we need to redact all the blocks in that317* hole. If the hole is in the meta-dnode, we instead need to redact318* all blocks in every object covered by that hole. If it's a level 0319* block, we only need to redact that single block.320*/321record = kmem_zalloc(sizeof (struct redact_record), KM_SLEEP);322record->eos_marker = B_FALSE;323324record->start_object = record->end_object = zb->zb_object;325if (BP_IS_HOLE(bp)) {326record->start_blkid = zb->zb_blkid *327bp_span_in_blocks(dnp->dn_indblkshift, zb->zb_level);328329record->end_blkid = ((zb->zb_blkid + 1) *330bp_span_in_blocks(dnp->dn_indblkshift, zb->zb_level)) - 1;331332if (zb->zb_object == DMU_META_DNODE_OBJECT) {333record->start_object = record->start_blkid *334((SPA_MINBLOCKSIZE * dnp->dn_datablkszsec) /335sizeof (dnode_phys_t));336record->start_blkid = 0;337record->end_object = ((record->end_blkid +3381) * ((SPA_MINBLOCKSIZE * dnp->dn_datablkszsec) /339sizeof (dnode_phys_t))) - 1;340record->end_blkid = UINT64_MAX;341}342} else if (zb->zb_level != 0 ||343zb->zb_object == DMU_META_DNODE_OBJECT) {344kmem_free(record, sizeof (*record));345return (0);346} else {347record->start_blkid = record->end_blkid = zb->zb_blkid;348}349record->indblkshift = dnp->dn_indblkshift;350record->datablksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;351record_merge_enqueue(&rta->q, &rta->current_record, record);352353return (0);354}355356static __attribute__((noreturn)) void357redact_traverse_thread(void *arg)358{359struct redact_thread_arg *rt_arg = arg;360int err;361struct redact_record *data;362#ifdef _KERNEL363if (rt_arg->os->os_phys->os_type == DMU_OST_ZFS)364rt_arg->deleted_objs = zfs_get_deleteq(rt_arg->os);365else366rt_arg->deleted_objs = objlist_create();367#else368rt_arg->deleted_objs = objlist_create();369#endif370371err = traverse_dataset_resume(rt_arg->ds, rt_arg->txg,372&rt_arg->resume, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA |373TRAVERSE_LOGICAL, redact_cb, rt_arg);374375if (err != EINTR)376rt_arg->error_code = err;377objlist_destroy(rt_arg->deleted_objs);378data = kmem_zalloc(sizeof (*data), KM_SLEEP);379data->eos_marker = B_TRUE;380record_merge_enqueue(&rt_arg->q, &rt_arg->current_record, data);381thread_exit();382}383384static inline void385create_zbookmark_from_obj_off(zbookmark_phys_t *zb, uint64_t object,386uint64_t blkid)387{388zb->zb_object = object;389zb->zb_level = 0;390zb->zb_blkid = blkid;391}392393/*394* This is a utility function that can do the comparison for the start or ends395* of the ranges in a redact_record.396*/397static int398redact_range_compare(uint64_t obj1, uint64_t off1, uint32_t dbss1,399uint64_t obj2, uint64_t off2, uint32_t dbss2)400{401zbookmark_phys_t z1, z2;402create_zbookmark_from_obj_off(&z1, obj1, off1);403create_zbookmark_from_obj_off(&z2, obj2, off2);404405return (zbookmark_compare(dbss1 >> SPA_MINBLOCKSHIFT, 0,406dbss2 >> SPA_MINBLOCKSHIFT, 0, &z1, &z2));407}408409/*410* Compare two redaction records by their range's start location. Also makes411* eos records always compare last. We use the thread number in the redact_node412* to ensure that records do not compare equal (which is not allowed in our avl413* trees).414*/415static int416redact_node_compare_start(const void *arg1, const void *arg2)417{418const struct redact_node *rn1 = arg1;419const struct redact_node *rn2 = arg2;420const struct redact_record *rr1 = rn1->record;421const struct redact_record *rr2 = rn2->record;422if (rr1->eos_marker)423return (1);424if (rr2->eos_marker)425return (-1);426427int cmp = redact_range_compare(rr1->start_object, rr1->start_blkid,428rr1->datablksz, rr2->start_object, rr2->start_blkid,429rr2->datablksz);430if (cmp == 0)431cmp = (rn1->thread_num < rn2->thread_num ? -1 : 1);432return (cmp);433}434435/*436* Compare two redaction records by their range's end location. Also makes437* eos records always compare last. We use the thread number in the redact_node438* to ensure that records do not compare equal (which is not allowed in our avl439* trees).440*/441static int442redact_node_compare_end(const void *arg1, const void *arg2)443{444const struct redact_node *rn1 = arg1;445const struct redact_node *rn2 = arg2;446const struct redact_record *srr1 = rn1->record;447const struct redact_record *srr2 = rn2->record;448if (srr1->eos_marker)449return (1);450if (srr2->eos_marker)451return (-1);452453int cmp = redact_range_compare(srr1->end_object, srr1->end_blkid,454srr1->datablksz, srr2->end_object, srr2->end_blkid,455srr2->datablksz);456if (cmp == 0)457cmp = (rn1->thread_num < rn2->thread_num ? -1 : 1);458return (cmp);459}460461/*462* Utility function that compares two redaction records to determine if any part463* of the "from" record is before any part of the "to" record. Also causes End464* of Stream redaction records to compare after all others, so that the465* redaction merging logic can stay simple.466*/467static boolean_t468redact_record_before(const struct redact_record *from,469const struct redact_record *to)470{471if (from->eos_marker == B_TRUE)472return (B_FALSE);473else if (to->eos_marker == B_TRUE)474return (B_TRUE);475return (redact_range_compare(from->start_object, from->start_blkid,476from->datablksz, to->end_object, to->end_blkid,477to->datablksz) <= 0);478}479480/*481* Pop a new redaction record off the queue, check that the records are in the482* right order, and free the old data.483*/484static struct redact_record *485get_next_redact_record(bqueue_t *bq, struct redact_record *prev)486{487struct redact_record *next = bqueue_dequeue(bq);488ASSERT(redact_record_before(prev, next));489kmem_free(prev, sizeof (*prev));490return (next);491}492493/*494* Remove the given redaction node from both trees, pull a new redaction record495* off the queue, free the old redaction record, update the redaction node, and496* reinsert the node into the trees.497*/498static int499update_avl_trees(avl_tree_t *start_tree, avl_tree_t *end_tree,500struct redact_node *redact_node)501{502avl_remove(start_tree, redact_node);503avl_remove(end_tree, redact_node);504redact_node->record = get_next_redact_record(&redact_node->rt_arg->q,505redact_node->record);506avl_add(end_tree, redact_node);507avl_add(start_tree, redact_node);508return (redact_node->rt_arg->error_code);509}510511/*512* Synctask for updating redaction lists. We first take this txg's list of513* redacted blocks and append those to the redaction list. We then update the514* redaction list's bonus buffer. We store the furthest blocks we visited and515* the list of snapshots that we're redacting with respect to. We need these so516* that redacted sends and receives can be correctly resumed.517*/518static void519redaction_list_update_sync(void *arg, dmu_tx_t *tx)520{521struct merge_data *md = arg;522uint64_t txg = dmu_tx_get_txg(tx);523list_t *list = &md->md_blocks[txg & TXG_MASK];524redact_block_phys_t *furthest_visited =525&md->md_furthest[txg & TXG_MASK];526objset_t *mos = tx->tx_pool->dp_meta_objset;527redaction_list_t *rl = md->md_redaction_list;528int bufsize = redact_sync_bufsize;529redact_block_phys_t *buf = kmem_alloc(bufsize * sizeof (*buf),530KM_SLEEP);531int index = 0;532533dmu_buf_will_dirty(rl->rl_dbuf, tx);534535for (struct redact_block_list_node *rbln = list_remove_head(list);536rbln != NULL; rbln = list_remove_head(list)) {537ASSERT3U(rbln->block.rbp_object, <=,538furthest_visited->rbp_object);539ASSERT(rbln->block.rbp_object < furthest_visited->rbp_object ||540rbln->block.rbp_blkid <= furthest_visited->rbp_blkid);541buf[index] = rbln->block;542index++;543if (index == bufsize) {544dmu_write(mos, rl->rl_object,545rl->rl_phys->rlp_num_entries * sizeof (*buf),546bufsize * sizeof (*buf), buf, tx,547DMU_READ_NO_PREFETCH);548rl->rl_phys->rlp_num_entries += bufsize;549index = 0;550}551kmem_free(rbln, sizeof (*rbln));552}553if (index > 0) {554dmu_write(mos, rl->rl_object, rl->rl_phys->rlp_num_entries *555sizeof (*buf), index * sizeof (*buf), buf, tx,556DMU_READ_NO_PREFETCH);557rl->rl_phys->rlp_num_entries += index;558}559kmem_free(buf, bufsize * sizeof (*buf));560561md->md_synctask_txg[txg & TXG_MASK] = B_FALSE;562rl->rl_phys->rlp_last_object = furthest_visited->rbp_object;563rl->rl_phys->rlp_last_blkid = furthest_visited->rbp_blkid;564}565566static void567commit_rl_updates(objset_t *os, struct merge_data *md, uint64_t object,568uint64_t blkid)569{570dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(os->os_spa)->dp_mos_dir);571dmu_tx_hold_space(tx, sizeof (struct redact_block_list_node));572VERIFY0(dmu_tx_assign(tx, DMU_TX_WAIT | DMU_TX_SUSPEND));573uint64_t txg = dmu_tx_get_txg(tx);574if (!md->md_synctask_txg[txg & TXG_MASK]) {575dsl_sync_task_nowait(dmu_tx_pool(tx),576redaction_list_update_sync, md, tx);577md->md_synctask_txg[txg & TXG_MASK] = B_TRUE;578md->md_latest_synctask_txg = txg;579}580md->md_furthest[txg & TXG_MASK].rbp_object = object;581md->md_furthest[txg & TXG_MASK].rbp_blkid = blkid;582list_move_tail(&md->md_blocks[txg & TXG_MASK],583&md->md_redact_block_pending);584dmu_tx_commit(tx);585md->md_last_time = gethrtime();586}587588/*589* We want to store the list of blocks that we're redacting in the bookmark's590* redaction list. However, this list is stored in the MOS, which means it can591* only be written to in syncing context. To get around this, we create a592* synctask that will write to the mos for us. We tell it what to write by593* a linked list for each current transaction group; every time we decide to594* redact a block, we append it to the transaction group that is currently in595* open context. We also update some progress information that the synctask596* will store to enable resumable redacted sends.597*/598static void599update_redaction_list(struct merge_data *md, objset_t *os,600uint64_t object, uint64_t blkid, uint64_t endblkid, uint32_t blksz)601{602boolean_t enqueue = B_FALSE;603redact_block_phys_t cur = {0};604uint64_t count = endblkid - blkid + 1;605while (count > REDACT_BLOCK_MAX_COUNT) {606update_redaction_list(md, os, object, blkid,607blkid + REDACT_BLOCK_MAX_COUNT - 1, blksz);608blkid += REDACT_BLOCK_MAX_COUNT;609count -= REDACT_BLOCK_MAX_COUNT;610}611redact_block_phys_t *coalesce = &md->md_coalesce_block;612boolean_t new;613if (coalesce->rbp_size_count == 0) {614new = B_TRUE;615enqueue = B_FALSE;616} else {617uint64_t old_count = redact_block_get_count(coalesce);618if (coalesce->rbp_object == object &&619coalesce->rbp_blkid + old_count == blkid &&620old_count + count <= REDACT_BLOCK_MAX_COUNT) {621ASSERT3U(redact_block_get_size(coalesce), ==, blksz);622redact_block_set_count(coalesce, old_count + count);623new = B_FALSE;624enqueue = B_FALSE;625} else {626new = B_TRUE;627enqueue = B_TRUE;628}629}630631if (new) {632cur = *coalesce;633coalesce->rbp_blkid = blkid;634coalesce->rbp_object = object;635636redact_block_set_count(coalesce, count);637redact_block_set_size(coalesce, blksz);638}639640if (enqueue && redact_block_get_size(&cur) != 0) {641struct redact_block_list_node *rbln =642kmem_alloc(sizeof (struct redact_block_list_node),643KM_SLEEP);644rbln->block = cur;645list_insert_tail(&md->md_redact_block_pending, rbln);646}647648if (gethrtime() > md->md_last_time +649redaction_list_update_interval_ns) {650commit_rl_updates(os, md, object, blkid);651}652}653654/*655* This thread merges all the redaction records provided by the worker threads,656* and determines which blocks are redacted by all the snapshots. The algorithm657* for doing so is similar to performing a merge in mergesort with n sub-lists658* instead of 2, with some added complexity due to the fact that the entries are659* ranges, not just single blocks. This algorithm relies on the fact that the660* queues are sorted, which is ensured by the fact that traverse_dataset661* traverses the dataset in a consistent order. We pull one entry off the front662* of the queues of each secure dataset traversal thread. Then we repeat the663* following: each record represents a range of blocks modified by one of the664* redaction snapshots, and each block in that range may need to be redacted in665* the send stream. Find the record with the latest start of its range, and the666* record with the earliest end of its range. If the last start is before the667* first end, then we know that the blocks in the range [last_start, first_end]668* are covered by all of the ranges at the front of the queues, which means669* every thread redacts that whole range. For example, let's say the ranges on670* each queue look like this:671*672* Block Id 1 2 3 4 5 6 7 8 9 10 11673* Thread 1 | [====================]674* Thread 2 | [========]675* Thread 3 | [=================]676*677* Thread 3 has the last start (5), and the thread 2 has the last end (6). All678* three threads modified the range [5,6], so that data should not be sent over679* the wire. After we've determined whether or not to redact anything, we take680* the record with the first end. We discard that record, and pull a new one681* off the front of the queue it came from. In the above example, we would682* discard Thread 2's record, and pull a new one. Let's say the next record we683* pulled from Thread 2 covered range [10,11]. The new layout would look like684* this:685*686* Block Id 1 2 3 4 5 6 7 8 9 10 11687* Thread 1 | [====================]688* Thread 2 | [==]689* Thread 3 | [=================]690*691* When we compare the last start (10, from Thread 2) and the first end (9, from692* Thread 1), we see that the last start is greater than the first end.693* Therefore, we do not redact anything from these records. We'll iterate by694* replacing the record from Thread 1.695*696* We iterate by replacing the record with the lowest end because we know697* that the record with the lowest end has helped us as much as it can. All the698* ranges before it that we will ever redact have been redacted. In addition,699* by replacing the one with the lowest end, we guarantee we catch all ranges700* that need to be redacted. For example, if in the case above we had replaced701* the record from Thread 1 instead, we might have ended up with the following:702*703* Block Id 1 2 3 4 5 6 7 8 9 10 11 12704* Thread 1 | [==]705* Thread 2 | [========]706* Thread 3 | [=================]707*708* If the next record from Thread 2 had been [8,10], for example, we should have709* redacted part of that range, but because we updated Thread 1's record, we710* missed it.711*712* We implement this algorithm by using two trees. The first sorts the713* redaction records by their start_zb, and the second sorts them by their714* end_zb. We use these to find the record with the last start and the record715* with the first end. We create a record with that start and end, and send it716* on. The overall runtime of this implementation is O(n log m), where n is the717* total number of redaction records from all the different redaction snapshots,718* and m is the number of redaction snapshots.719*720* If we redact with respect to zero snapshots, we create a redaction721* record with the start object and blkid to 0, and the end object and blkid to722* UINT64_MAX. This will result in us redacting every block.723*/724static int725perform_thread_merge(bqueue_t *q, uint32_t num_threads,726struct redact_thread_arg *thread_args, boolean_t *cancel)727{728struct redact_node *redact_nodes = NULL;729avl_tree_t start_tree, end_tree;730struct redact_record *record;731struct redact_record *current_record = NULL;732int err = 0;733struct merge_data md = { {0} };734list_create(&md.md_redact_block_pending,735sizeof (struct redact_block_list_node),736offsetof(struct redact_block_list_node, node));737738/*739* If we're redacting with respect to zero snapshots, then no data is740* permitted to be sent. We enqueue a record that redacts all blocks,741* and an eos marker.742*/743if (num_threads == 0) {744record = kmem_zalloc(sizeof (struct redact_record),745KM_SLEEP);746// We can't redact object 0, so don't try.747record->start_object = 1;748record->start_blkid = 0;749record->end_object = record->end_blkid = UINT64_MAX;750bqueue_enqueue(q, record, sizeof (*record));751return (0);752}753redact_nodes = vmem_zalloc(num_threads *754sizeof (*redact_nodes), KM_SLEEP);755756avl_create(&start_tree, redact_node_compare_start,757sizeof (struct redact_node),758offsetof(struct redact_node, avl_node_start));759avl_create(&end_tree, redact_node_compare_end,760sizeof (struct redact_node),761offsetof(struct redact_node, avl_node_end));762763for (int i = 0; i < num_threads; i++) {764struct redact_node *node = &redact_nodes[i];765struct redact_thread_arg *targ = &thread_args[i];766node->record = bqueue_dequeue(&targ->q);767node->rt_arg = targ;768node->thread_num = i;769avl_add(&start_tree, node);770avl_add(&end_tree, node);771}772773/*774* Once the first record in the end tree has returned EOS, every record775* must be an EOS record, so we should stop.776*/777while (err == 0 && !((struct redact_node *)avl_first(&end_tree))->778record->eos_marker) {779if (*cancel) {780err = EINTR;781break;782}783struct redact_node *last_start = avl_last(&start_tree);784struct redact_node *first_end = avl_first(&end_tree);785786/*787* If the last start record is before the first end record,788* then we have blocks that are redacted by all threads.789* Therefore, we should redact them. Copy the record, and send790* it to the main thread.791*/792if (redact_record_before(last_start->record,793first_end->record)) {794record = kmem_zalloc(sizeof (struct redact_record),795KM_SLEEP);796*record = *first_end->record;797record->start_object = last_start->record->start_object;798record->start_blkid = last_start->record->start_blkid;799record_merge_enqueue(q, ¤t_record,800record);801}802err = update_avl_trees(&start_tree, &end_tree, first_end);803}804805/*806* We're done; if we were cancelled, we need to cancel our workers and807* clear out their queues. Either way, we need to remove every thread's808* redact_node struct from the avl trees.809*/810for (int i = 0; i < num_threads; i++) {811if (err != 0) {812thread_args[i].cancel = B_TRUE;813while (!redact_nodes[i].record->eos_marker) {814(void) update_avl_trees(&start_tree, &end_tree,815&redact_nodes[i]);816}817}818avl_remove(&start_tree, &redact_nodes[i]);819avl_remove(&end_tree, &redact_nodes[i]);820kmem_free(redact_nodes[i].record,821sizeof (struct redact_record));822bqueue_destroy(&thread_args[i].q);823}824825avl_destroy(&start_tree);826avl_destroy(&end_tree);827vmem_free(redact_nodes, num_threads * sizeof (*redact_nodes));828if (current_record != NULL)829bqueue_enqueue(q, current_record, sizeof (*current_record));830return (err);831}832833struct redact_merge_thread_arg {834bqueue_t q;835spa_t *spa;836int numsnaps;837struct redact_thread_arg *thr_args;838boolean_t cancel;839int error_code;840};841842static __attribute__((noreturn)) void843redact_merge_thread(void *arg)844{845struct redact_merge_thread_arg *rmta = arg;846rmta->error_code = perform_thread_merge(&rmta->q,847rmta->numsnaps, rmta->thr_args, &rmta->cancel);848struct redact_record *rec = kmem_zalloc(sizeof (*rec), KM_SLEEP);849rec->eos_marker = B_TRUE;850bqueue_enqueue_flush(&rmta->q, rec, 1);851thread_exit();852}853854/*855* Find the next object in or after the redaction range passed in, and hold856* its dnode with the provided tag. Also update *object to contain the new857* object number.858*/859static int860hold_next_object(objset_t *os, struct redact_record *rec, const void *tag,861uint64_t *object, dnode_t **dn)862{863int err = 0;864if (*dn != NULL)865dnode_rele(*dn, tag);866*dn = NULL;867if (*object < rec->start_object) {868*object = rec->start_object - 1;869}870err = dmu_object_next(os, object, B_FALSE, 0);871if (err != 0)872return (err);873874err = dnode_hold(os, *object, tag, dn);875while (err == 0 && (*object < rec->start_object ||876DMU_OT_IS_METADATA((*dn)->dn_type))) {877dnode_rele(*dn, tag);878*dn = NULL;879err = dmu_object_next(os, object, B_FALSE, 0);880if (err != 0)881break;882err = dnode_hold(os, *object, tag, dn);883}884return (err);885}886887static int888perform_redaction(objset_t *os, redaction_list_t *rl,889struct redact_merge_thread_arg *rmta)890{891int err = 0;892bqueue_t *q = &rmta->q;893struct redact_record *rec = NULL;894struct merge_data md = { {0} };895896list_create(&md.md_redact_block_pending,897sizeof (struct redact_block_list_node),898offsetof(struct redact_block_list_node, node));899md.md_redaction_list = rl;900901for (int i = 0; i < TXG_SIZE; i++) {902list_create(&md.md_blocks[i],903sizeof (struct redact_block_list_node),904offsetof(struct redact_block_list_node, node));905}906dnode_t *dn = NULL;907uint64_t prev_obj = 0;908for (rec = bqueue_dequeue(q); !rec->eos_marker && err == 0;909rec = get_next_redact_record(q, rec)) {910ASSERT3U(rec->start_object, !=, 0);911uint64_t object;912if (prev_obj != rec->start_object) {913object = rec->start_object - 1;914err = hold_next_object(os, rec, FTAG, &object, &dn);915} else {916object = prev_obj;917}918while (err == 0 && object <= rec->end_object) {919if (issig()) {920err = EINTR;921break;922}923/*924* Part of the current object is contained somewhere in925* the range covered by rec.926*/927uint64_t startblkid;928uint64_t endblkid;929uint64_t maxblkid = dn->dn_phys->dn_maxblkid;930931if (rec->start_object < object)932startblkid = 0;933else if (rec->start_blkid > maxblkid)934break;935else936startblkid = rec->start_blkid;937938if (rec->end_object > object || rec->end_blkid >939maxblkid) {940endblkid = maxblkid;941} else {942endblkid = rec->end_blkid;943}944update_redaction_list(&md, os, object, startblkid,945endblkid, dn->dn_datablksz);946947if (object == rec->end_object)948break;949err = hold_next_object(os, rec, FTAG, &object, &dn);950}951if (err == ESRCH)952err = 0;953if (dn != NULL)954prev_obj = object;955}956if (err == 0 && dn != NULL)957dnode_rele(dn, FTAG);958959if (err == ESRCH)960err = 0;961rmta->cancel = B_TRUE;962while (!rec->eos_marker)963rec = get_next_redact_record(q, rec);964kmem_free(rec, sizeof (*rec));965966/*967* There may be a block that's being coalesced, sync that out before we968* return.969*/970if (err == 0 && md.md_coalesce_block.rbp_size_count != 0) {971struct redact_block_list_node *rbln =972kmem_alloc(sizeof (struct redact_block_list_node),973KM_SLEEP);974rbln->block = md.md_coalesce_block;975list_insert_tail(&md.md_redact_block_pending, rbln);976}977commit_rl_updates(os, &md, UINT64_MAX, UINT64_MAX);978979/*980* Wait for all the redaction info to sync out before we return, so that981* anyone who attempts to resume this redaction will have all the data982* they need.983*/984dsl_pool_t *dp = spa_get_dsl(os->os_spa);985if (md.md_latest_synctask_txg != 0)986txg_wait_synced(dp, md.md_latest_synctask_txg);987for (int i = 0; i < TXG_SIZE; i++)988list_destroy(&md.md_blocks[i]);989return (err);990}991992static boolean_t993redact_snaps_contains(uint64_t *snaps, uint64_t num_snaps, uint64_t guid)994{995for (int i = 0; i < num_snaps; i++) {996if (snaps[i] == guid)997return (B_TRUE);998}999return (B_FALSE);1000}10011002int1003dmu_redact_snap(const char *snapname, nvlist_t *redactnvl,1004const char *redactbook)1005{1006int err = 0;1007dsl_pool_t *dp = NULL;1008dsl_dataset_t *ds = NULL;1009int numsnaps = 0;1010objset_t *os;1011struct redact_thread_arg *args = NULL;1012redaction_list_t *new_rl = NULL;1013char *newredactbook;10141015if ((err = dsl_pool_hold(snapname, FTAG, &dp)) != 0)1016return (err);10171018newredactbook = kmem_zalloc(sizeof (char) * ZFS_MAX_DATASET_NAME_LEN,1019KM_SLEEP);10201021if ((err = dsl_dataset_hold_flags(dp, snapname, DS_HOLD_FLAG_DECRYPT,1022FTAG, &ds)) != 0) {1023goto out;1024}1025dsl_dataset_long_hold(ds, FTAG);1026if (!ds->ds_is_snapshot || dmu_objset_from_ds(ds, &os) != 0) {1027err = EINVAL;1028goto out;1029}1030if (dsl_dataset_feature_is_active(ds, SPA_FEATURE_REDACTED_DATASETS)) {1031err = EALREADY;1032goto out;1033}10341035numsnaps = fnvlist_num_pairs(redactnvl);1036if (numsnaps > 0)1037args = vmem_zalloc(numsnaps * sizeof (*args), KM_SLEEP);10381039nvpair_t *pair = NULL;1040for (int i = 0; i < numsnaps; i++) {1041pair = nvlist_next_nvpair(redactnvl, pair);1042const char *name = nvpair_name(pair);1043struct redact_thread_arg *rta = &args[i];1044err = dsl_dataset_hold_flags(dp, name, DS_HOLD_FLAG_DECRYPT,1045FTAG, &rta->ds);1046if (err != 0)1047break;1048/*1049* We want to do the long hold before we can get any other1050* errors, because the cleanup code will release the long1051* hold if rta->ds is filled in.1052*/1053dsl_dataset_long_hold(rta->ds, FTAG);10541055err = dmu_objset_from_ds(rta->ds, &rta->os);1056if (err != 0)1057break;1058if (!dsl_dataset_is_before(rta->ds, ds, 0)) {1059err = EINVAL;1060break;1061}1062if (dsl_dataset_feature_is_active(rta->ds,1063SPA_FEATURE_REDACTED_DATASETS)) {1064err = EALREADY;1065break;10661067}1068}1069if (err != 0)1070goto out;1071VERIFY0P(nvlist_next_nvpair(redactnvl, pair));10721073boolean_t resuming = B_FALSE;1074zfs_bookmark_phys_t bookmark;10751076(void) strlcpy(newredactbook, snapname, ZFS_MAX_DATASET_NAME_LEN);1077char *c = strchr(newredactbook, '@');1078ASSERT3P(c, !=, NULL);1079int n = snprintf(c, ZFS_MAX_DATASET_NAME_LEN - (c - newredactbook),1080"#%s", redactbook);1081if (n >= ZFS_MAX_DATASET_NAME_LEN - (c - newredactbook)) {1082dsl_pool_rele(dp, FTAG);1083kmem_free(newredactbook,1084sizeof (char) * ZFS_MAX_DATASET_NAME_LEN);1085if (args != NULL)1086vmem_free(args, numsnaps * sizeof (*args));1087return (SET_ERROR(ENAMETOOLONG));1088}1089err = dsl_bookmark_lookup(dp, newredactbook, NULL, &bookmark);1090if (err == 0) {1091resuming = B_TRUE;1092if (bookmark.zbm_redaction_obj == 0) {1093err = EEXIST;1094goto out;1095}1096err = dsl_redaction_list_hold_obj(dp,1097bookmark.zbm_redaction_obj, FTAG, &new_rl);1098if (err != 0) {1099err = EIO;1100goto out;1101}1102dsl_redaction_list_long_hold(dp, new_rl, FTAG);1103if (new_rl->rl_phys->rlp_num_snaps != numsnaps) {1104err = ESRCH;1105goto out;1106}1107for (int i = 0; i < numsnaps; i++) {1108struct redact_thread_arg *rta = &args[i];1109if (!redact_snaps_contains(new_rl->rl_phys->rlp_snaps,1110new_rl->rl_phys->rlp_num_snaps,1111dsl_dataset_phys(rta->ds)->ds_guid)) {1112err = ESRCH;1113goto out;1114}1115}1116if (new_rl->rl_phys->rlp_last_blkid == UINT64_MAX &&1117new_rl->rl_phys->rlp_last_object == UINT64_MAX) {1118err = EEXIST;1119goto out;1120}1121dsl_pool_rele(dp, FTAG);1122dp = NULL;1123} else {1124uint64_t *guids = NULL;1125if (numsnaps > 0) {1126guids = vmem_zalloc(numsnaps * sizeof (uint64_t),1127KM_SLEEP);1128}1129for (int i = 0; i < numsnaps; i++) {1130struct redact_thread_arg *rta = &args[i];1131guids[i] = dsl_dataset_phys(rta->ds)->ds_guid;1132}11331134dsl_pool_rele(dp, FTAG);1135dp = NULL;1136err = dsl_bookmark_create_redacted(newredactbook, snapname,1137numsnaps, guids, FTAG, &new_rl);1138vmem_free(guids, numsnaps * sizeof (uint64_t));1139if (err != 0)1140goto out;1141}11421143for (int i = 0; i < numsnaps; i++) {1144struct redact_thread_arg *rta = &args[i];1145(void) bqueue_init(&rta->q, zfs_redact_queue_ff,1146zfs_redact_queue_length,1147offsetof(struct redact_record, ln));1148if (resuming) {1149rta->resume.zb_blkid =1150new_rl->rl_phys->rlp_last_blkid;1151rta->resume.zb_object =1152new_rl->rl_phys->rlp_last_object;1153}1154rta->txg = dsl_dataset_phys(ds)->ds_creation_txg;1155(void) thread_create(NULL, 0, redact_traverse_thread, rta,11560, curproc, TS_RUN, minclsyspri);1157}11581159struct redact_merge_thread_arg *rmta;1160rmta = kmem_zalloc(sizeof (struct redact_merge_thread_arg), KM_SLEEP);11611162(void) bqueue_init(&rmta->q, zfs_redact_queue_ff,1163zfs_redact_queue_length, offsetof(struct redact_record, ln));1164rmta->numsnaps = numsnaps;1165rmta->spa = os->os_spa;1166rmta->thr_args = args;1167(void) thread_create(NULL, 0, redact_merge_thread, rmta, 0, curproc,1168TS_RUN, minclsyspri);1169err = perform_redaction(os, new_rl, rmta);1170bqueue_destroy(&rmta->q);1171kmem_free(rmta, sizeof (struct redact_merge_thread_arg));11721173out:1174kmem_free(newredactbook, sizeof (char) * ZFS_MAX_DATASET_NAME_LEN);11751176if (new_rl != NULL) {1177dsl_redaction_list_long_rele(new_rl, FTAG);1178dsl_redaction_list_rele(new_rl, FTAG);1179}1180for (int i = 0; i < numsnaps; i++) {1181struct redact_thread_arg *rta = &args[i];1182/*1183* rta->ds may be NULL if we got an error while filling1184* it in.1185*/1186if (rta->ds != NULL) {1187dsl_dataset_long_rele(rta->ds, FTAG);1188dsl_dataset_rele_flags(rta->ds,1189DS_HOLD_FLAG_DECRYPT, FTAG);1190}1191}11921193if (args != NULL)1194vmem_free(args, numsnaps * sizeof (*args));1195if (dp != NULL)1196dsl_pool_rele(dp, FTAG);1197if (ds != NULL) {1198dsl_dataset_long_rele(ds, FTAG);1199dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);1200}1201return (SET_ERROR(err));12021203}120412051206