1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * CDDL HEADER START 3eda14cbcSMatt Macy * 4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0. 10eda14cbcSMatt Macy * See the License for the specific language governing permissions 11eda14cbcSMatt Macy * and limitations under the License. 12eda14cbcSMatt Macy * 13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18eda14cbcSMatt Macy * 19eda14cbcSMatt Macy * CDDL HEADER END 20eda14cbcSMatt Macy */ 21eda14cbcSMatt Macy /* 22eda14cbcSMatt Macy * Copyright (c) 2017, 2018 by Delphix. All rights reserved. 23eda14cbcSMatt Macy */ 24eda14cbcSMatt Macy 25eda14cbcSMatt Macy #include <sys/zfs_context.h> 26eda14cbcSMatt Macy #include <sys/txg.h> 27eda14cbcSMatt Macy #include <sys/dmu_objset.h> 28eda14cbcSMatt Macy #include <sys/dmu_traverse.h> 29eda14cbcSMatt Macy #include <sys/dmu_redact.h> 30eda14cbcSMatt Macy #include <sys/bqueue.h> 31eda14cbcSMatt Macy #include <sys/objlist.h> 32eda14cbcSMatt Macy #include <sys/dmu_tx.h> 33eda14cbcSMatt Macy #ifdef _KERNEL 34eda14cbcSMatt Macy #include <sys/zfs_vfsops.h> 35eda14cbcSMatt Macy #include <sys/zap.h> 36eda14cbcSMatt Macy #include <sys/zfs_znode.h> 37eda14cbcSMatt Macy #endif 38eda14cbcSMatt Macy 39eda14cbcSMatt Macy /* 40eda14cbcSMatt Macy * This controls the number of entries in the buffer the redaction_list_update 41eda14cbcSMatt Macy * synctask uses to buffer writes to the redaction list. 42eda14cbcSMatt Macy */ 43e92ffd9bSMartin Matuska static const int redact_sync_bufsize = 1024; 44eda14cbcSMatt Macy 45eda14cbcSMatt Macy /* 46eda14cbcSMatt Macy * Controls how often to update the redaction list when creating a redaction 47eda14cbcSMatt Macy * list. 48eda14cbcSMatt Macy */ 49e92ffd9bSMartin Matuska static const uint64_t redaction_list_update_interval_ns = 50e92ffd9bSMartin Matuska 1000 * 1000 * 1000ULL; /* 1s */ 51eda14cbcSMatt Macy 52eda14cbcSMatt Macy /* 53eda14cbcSMatt Macy * This tunable controls the length of the queues that zfs redact worker threads 54eda14cbcSMatt Macy * use to communicate. If the dmu_redact_snap thread is blocking on these 55eda14cbcSMatt Macy * queues, this variable may need to be increased. If there is a significant 56eda14cbcSMatt Macy * slowdown at the start of a redact operation as these threads consume all the 57eda14cbcSMatt Macy * available IO resources, or the queues are consuming too much memory, this 58eda14cbcSMatt Macy * variable may need to be decreased. 59eda14cbcSMatt Macy */ 60e92ffd9bSMartin Matuska static const int zfs_redact_queue_length = 1024 * 1024; 61eda14cbcSMatt Macy 62eda14cbcSMatt Macy /* 63eda14cbcSMatt Macy * These tunables control the fill fraction of the queues by zfs redact. The 64eda14cbcSMatt Macy * fill fraction controls the frequency with which threads have to be 65eda14cbcSMatt Macy * cv_signaled. If a lot of cpu time is being spent on cv_signal, then these 66eda14cbcSMatt Macy * should be tuned down. If the queues empty before the signalled thread can 67eda14cbcSMatt Macy * catch up, then these should be tuned up. 68eda14cbcSMatt Macy */ 69e92ffd9bSMartin Matuska static const uint64_t zfs_redact_queue_ff = 20; 70eda14cbcSMatt Macy 71eda14cbcSMatt Macy struct redact_record { 72eda14cbcSMatt Macy bqueue_node_t ln; 73eda14cbcSMatt Macy boolean_t eos_marker; /* Marks the end of the stream */ 74eda14cbcSMatt Macy uint64_t start_object; 75eda14cbcSMatt Macy uint64_t start_blkid; 76eda14cbcSMatt Macy uint64_t end_object; 77eda14cbcSMatt Macy uint64_t end_blkid; 78eda14cbcSMatt Macy uint8_t indblkshift; 79eda14cbcSMatt Macy uint32_t datablksz; 80eda14cbcSMatt Macy }; 81eda14cbcSMatt Macy 82eda14cbcSMatt Macy struct redact_thread_arg { 83eda14cbcSMatt Macy bqueue_t q; 84eda14cbcSMatt Macy objset_t *os; /* Objset to traverse */ 85eda14cbcSMatt Macy dsl_dataset_t *ds; /* Dataset to traverse */ 86eda14cbcSMatt Macy struct redact_record *current_record; 87eda14cbcSMatt Macy int error_code; 88eda14cbcSMatt Macy boolean_t cancel; 89eda14cbcSMatt Macy zbookmark_phys_t resume; 90eda14cbcSMatt Macy objlist_t *deleted_objs; 91eda14cbcSMatt Macy uint64_t *num_blocks_visited; 92eda14cbcSMatt Macy uint64_t ignore_object; /* ignore further callbacks on this */ 93eda14cbcSMatt Macy uint64_t txg; /* txg to traverse since */ 94eda14cbcSMatt Macy }; 95eda14cbcSMatt Macy 96eda14cbcSMatt Macy /* 97eda14cbcSMatt Macy * The redaction node is a wrapper around the redaction record that is used 98eda14cbcSMatt Macy * by the redaction merging thread to sort the records and determine overlaps. 99eda14cbcSMatt Macy * 100eda14cbcSMatt Macy * It contains two nodes; one sorts the records by their start_zb, and the other 101eda14cbcSMatt Macy * sorts the records by their end_zb. 102eda14cbcSMatt Macy */ 103eda14cbcSMatt Macy struct redact_node { 104eda14cbcSMatt Macy avl_node_t avl_node_start; 105eda14cbcSMatt Macy avl_node_t avl_node_end; 106eda14cbcSMatt Macy struct redact_record *record; 107eda14cbcSMatt Macy struct redact_thread_arg *rt_arg; 108eda14cbcSMatt Macy uint32_t thread_num; 109eda14cbcSMatt Macy }; 110eda14cbcSMatt Macy 111eda14cbcSMatt Macy struct merge_data { 112eda14cbcSMatt Macy list_t md_redact_block_pending; 113eda14cbcSMatt Macy redact_block_phys_t md_coalesce_block; 114eda14cbcSMatt Macy uint64_t md_last_time; 115eda14cbcSMatt Macy redact_block_phys_t md_furthest[TXG_SIZE]; 116eda14cbcSMatt Macy /* Lists of struct redact_block_list_node. */ 117eda14cbcSMatt Macy list_t md_blocks[TXG_SIZE]; 118eda14cbcSMatt Macy boolean_t md_synctask_txg[TXG_SIZE]; 119eda14cbcSMatt Macy uint64_t md_latest_synctask_txg; 120eda14cbcSMatt Macy redaction_list_t *md_redaction_list; 121eda14cbcSMatt Macy }; 122eda14cbcSMatt Macy 123eda14cbcSMatt Macy /* 124eda14cbcSMatt Macy * A wrapper around struct redact_block so it can be stored in a list_t. 125eda14cbcSMatt Macy */ 126eda14cbcSMatt Macy struct redact_block_list_node { 127eda14cbcSMatt Macy redact_block_phys_t block; 128eda14cbcSMatt Macy list_node_t node; 129eda14cbcSMatt Macy }; 130eda14cbcSMatt Macy 131eda14cbcSMatt Macy /* 132eda14cbcSMatt Macy * We've found a new redaction candidate. In order to improve performance, we 133eda14cbcSMatt Macy * coalesce these blocks when they're adjacent to each other. This function 134eda14cbcSMatt Macy * handles that. If the new candidate block range is immediately after the 135eda14cbcSMatt Macy * range we're building, coalesce it into the range we're building. Otherwise, 136eda14cbcSMatt Macy * put the record we're building on the queue, and update the build pointer to 137eda14cbcSMatt Macy * point to the new record. 138eda14cbcSMatt Macy */ 139eda14cbcSMatt Macy static void 140eda14cbcSMatt Macy record_merge_enqueue(bqueue_t *q, struct redact_record **build, 141eda14cbcSMatt Macy struct redact_record *new) 142eda14cbcSMatt Macy { 143eda14cbcSMatt Macy if (new->eos_marker) { 144eda14cbcSMatt Macy if (*build != NULL) 145c7046f76SMartin Matuska bqueue_enqueue(q, *build, sizeof (**build)); 146eda14cbcSMatt Macy bqueue_enqueue_flush(q, new, sizeof (*new)); 147eda14cbcSMatt Macy return; 148eda14cbcSMatt Macy } 149eda14cbcSMatt Macy if (*build == NULL) { 150eda14cbcSMatt Macy *build = new; 151eda14cbcSMatt Macy return; 152eda14cbcSMatt Macy } 153eda14cbcSMatt Macy struct redact_record *curbuild = *build; 154eda14cbcSMatt Macy if ((curbuild->end_object == new->start_object && 155eda14cbcSMatt Macy curbuild->end_blkid + 1 == new->start_blkid && 156eda14cbcSMatt Macy curbuild->end_blkid != UINT64_MAX) || 157eda14cbcSMatt Macy (curbuild->end_object + 1 == new->start_object && 158eda14cbcSMatt Macy curbuild->end_blkid == UINT64_MAX && new->start_blkid == 0)) { 159eda14cbcSMatt Macy curbuild->end_object = new->end_object; 160eda14cbcSMatt Macy curbuild->end_blkid = new->end_blkid; 161eda14cbcSMatt Macy kmem_free(new, sizeof (*new)); 162eda14cbcSMatt Macy } else { 163eda14cbcSMatt Macy bqueue_enqueue(q, curbuild, sizeof (*curbuild)); 164eda14cbcSMatt Macy *build = new; 165eda14cbcSMatt Macy } 166eda14cbcSMatt Macy } 167eda14cbcSMatt Macy #ifdef _KERNEL 168eda14cbcSMatt Macy struct objnode { 169eda14cbcSMatt Macy avl_node_t node; 170eda14cbcSMatt Macy uint64_t obj; 171eda14cbcSMatt Macy }; 172eda14cbcSMatt Macy 173eda14cbcSMatt Macy static int 174eda14cbcSMatt Macy objnode_compare(const void *o1, const void *o2) 175eda14cbcSMatt Macy { 176eda14cbcSMatt Macy const struct objnode *obj1 = o1; 177eda14cbcSMatt Macy const struct objnode *obj2 = o2; 178eda14cbcSMatt Macy if (obj1->obj < obj2->obj) 179eda14cbcSMatt Macy return (-1); 180eda14cbcSMatt Macy if (obj1->obj > obj2->obj) 181eda14cbcSMatt Macy return (1); 182eda14cbcSMatt Macy return (0); 183eda14cbcSMatt Macy } 184eda14cbcSMatt Macy 185eda14cbcSMatt Macy 186eda14cbcSMatt Macy static objlist_t * 187eda14cbcSMatt Macy zfs_get_deleteq(objset_t *os) 188eda14cbcSMatt Macy { 189eda14cbcSMatt Macy objlist_t *deleteq_objlist = objlist_create(); 190eda14cbcSMatt Macy uint64_t deleteq_obj; 191eda14cbcSMatt Macy zap_cursor_t zc; 192*7a7741afSMartin Matuska zap_attribute_t *za; 193eda14cbcSMatt Macy dmu_object_info_t doi; 194eda14cbcSMatt Macy 195eda14cbcSMatt Macy ASSERT3U(os->os_phys->os_type, ==, DMU_OST_ZFS); 196eda14cbcSMatt Macy VERIFY0(dmu_object_info(os, MASTER_NODE_OBJ, &doi)); 197eda14cbcSMatt Macy ASSERT3U(doi.doi_type, ==, DMU_OT_MASTER_NODE); 198eda14cbcSMatt Macy 199eda14cbcSMatt Macy VERIFY0(zap_lookup(os, MASTER_NODE_OBJ, 200eda14cbcSMatt Macy ZFS_UNLINKED_SET, sizeof (uint64_t), 1, &deleteq_obj)); 201eda14cbcSMatt Macy 202eda14cbcSMatt Macy /* 203eda14cbcSMatt Macy * In order to insert objects into the objlist, they must be in sorted 204eda14cbcSMatt Macy * order. We don't know what order we'll get them out of the ZAP in, so 205eda14cbcSMatt Macy * we insert them into and remove them from an avl_tree_t to sort them. 206eda14cbcSMatt Macy */ 207eda14cbcSMatt Macy avl_tree_t at; 208eda14cbcSMatt Macy avl_create(&at, objnode_compare, sizeof (struct objnode), 209eda14cbcSMatt Macy offsetof(struct objnode, node)); 210eda14cbcSMatt Macy 211*7a7741afSMartin Matuska za = zap_attribute_alloc(); 212eda14cbcSMatt Macy for (zap_cursor_init(&zc, os, deleteq_obj); 213*7a7741afSMartin Matuska zap_cursor_retrieve(&zc, za) == 0; zap_cursor_advance(&zc)) { 214eda14cbcSMatt Macy struct objnode *obj = kmem_zalloc(sizeof (*obj), KM_SLEEP); 215*7a7741afSMartin Matuska obj->obj = za->za_first_integer; 216eda14cbcSMatt Macy avl_add(&at, obj); 217eda14cbcSMatt Macy } 218eda14cbcSMatt Macy zap_cursor_fini(&zc); 219*7a7741afSMartin Matuska zap_attribute_free(za); 220eda14cbcSMatt Macy 221eda14cbcSMatt Macy struct objnode *next, *found = avl_first(&at); 222eda14cbcSMatt Macy while (found != NULL) { 223eda14cbcSMatt Macy next = AVL_NEXT(&at, found); 224eda14cbcSMatt Macy objlist_insert(deleteq_objlist, found->obj); 225eda14cbcSMatt Macy found = next; 226eda14cbcSMatt Macy } 227eda14cbcSMatt Macy 228eda14cbcSMatt Macy void *cookie = NULL; 229eda14cbcSMatt Macy while ((found = avl_destroy_nodes(&at, &cookie)) != NULL) 230eda14cbcSMatt Macy kmem_free(found, sizeof (*found)); 231eda14cbcSMatt Macy avl_destroy(&at); 232eda14cbcSMatt Macy return (deleteq_objlist); 233eda14cbcSMatt Macy } 234eda14cbcSMatt Macy #endif 235eda14cbcSMatt Macy 236eda14cbcSMatt Macy /* 237eda14cbcSMatt Macy * This is the callback function to traverse_dataset for the redaction threads 238eda14cbcSMatt Macy * for dmu_redact_snap. This thread is responsible for creating redaction 239eda14cbcSMatt Macy * records for all the data that is modified by the snapshots we're redacting 240eda14cbcSMatt Macy * with respect to. Redaction records represent ranges of data that have been 241eda14cbcSMatt Macy * modified by one of the redaction snapshots, and are stored in the 242eda14cbcSMatt Macy * redact_record struct. We need to create redaction records for three 243eda14cbcSMatt Macy * cases: 244eda14cbcSMatt Macy * 245eda14cbcSMatt Macy * First, if there's a normal write, we need to create a redaction record for 246eda14cbcSMatt Macy * that block. 247eda14cbcSMatt Macy * 248eda14cbcSMatt Macy * Second, if there's a hole, we need to create a redaction record that covers 249eda14cbcSMatt Macy * the whole range of the hole. If the hole is in the meta-dnode, it must cover 250eda14cbcSMatt Macy * every block in all of the objects in the hole. 251eda14cbcSMatt Macy * 252eda14cbcSMatt Macy * Third, if there is a deleted object, we need to create a redaction record for 253eda14cbcSMatt Macy * all of the blocks in that object. 254eda14cbcSMatt Macy */ 255eda14cbcSMatt Macy static int 256eda14cbcSMatt Macy redact_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, 257eda14cbcSMatt Macy const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg) 258eda14cbcSMatt Macy { 259e92ffd9bSMartin Matuska (void) spa, (void) zilog; 260eda14cbcSMatt Macy struct redact_thread_arg *rta = arg; 261eda14cbcSMatt Macy struct redact_record *record; 262eda14cbcSMatt Macy 263eda14cbcSMatt Macy ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT || 264eda14cbcSMatt Macy zb->zb_object >= rta->resume.zb_object); 265eda14cbcSMatt Macy 266eda14cbcSMatt Macy if (rta->cancel) 267eda14cbcSMatt Macy return (SET_ERROR(EINTR)); 268eda14cbcSMatt Macy 269eda14cbcSMatt Macy if (rta->ignore_object == zb->zb_object) 270eda14cbcSMatt Macy return (0); 271eda14cbcSMatt Macy 272eda14cbcSMatt Macy /* 273eda14cbcSMatt Macy * If we're visiting a dnode, we need to handle the case where the 274eda14cbcSMatt Macy * object has been deleted. 275eda14cbcSMatt Macy */ 276eda14cbcSMatt Macy if (zb->zb_level == ZB_DNODE_LEVEL) { 277eda14cbcSMatt Macy ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL); 278eda14cbcSMatt Macy 279eda14cbcSMatt Macy if (zb->zb_object == 0) 280eda14cbcSMatt Macy return (0); 281eda14cbcSMatt Macy 282eda14cbcSMatt Macy /* 283eda14cbcSMatt Macy * If the object has been deleted, redact all of the blocks in 284eda14cbcSMatt Macy * it. 285eda14cbcSMatt Macy */ 286eda14cbcSMatt Macy if (dnp->dn_type == DMU_OT_NONE || 287eda14cbcSMatt Macy objlist_exists(rta->deleted_objs, zb->zb_object)) { 288eda14cbcSMatt Macy rta->ignore_object = zb->zb_object; 289eda14cbcSMatt Macy record = kmem_zalloc(sizeof (struct redact_record), 290eda14cbcSMatt Macy KM_SLEEP); 291eda14cbcSMatt Macy 292eda14cbcSMatt Macy record->eos_marker = B_FALSE; 293eda14cbcSMatt Macy record->start_object = record->end_object = 294eda14cbcSMatt Macy zb->zb_object; 295eda14cbcSMatt Macy record->start_blkid = 0; 296eda14cbcSMatt Macy record->end_blkid = UINT64_MAX; 297eda14cbcSMatt Macy record_merge_enqueue(&rta->q, 298eda14cbcSMatt Macy &rta->current_record, record); 299eda14cbcSMatt Macy } 300eda14cbcSMatt Macy return (0); 301eda14cbcSMatt Macy } else if (zb->zb_level < 0) { 302eda14cbcSMatt Macy return (0); 303eda14cbcSMatt Macy } else if (zb->zb_level > 0 && !BP_IS_HOLE(bp)) { 304eda14cbcSMatt Macy /* 305eda14cbcSMatt Macy * If this is an indirect block, but not a hole, it doesn't 306eda14cbcSMatt Macy * provide any useful information for redaction, so ignore it. 307eda14cbcSMatt Macy */ 308eda14cbcSMatt Macy return (0); 309eda14cbcSMatt Macy } 310eda14cbcSMatt Macy 311eda14cbcSMatt Macy /* 312eda14cbcSMatt Macy * At this point, there are two options left for the type of block we're 313eda14cbcSMatt Macy * looking at. Either this is a hole (which could be in the dnode or 314eda14cbcSMatt Macy * the meta-dnode), or it's a level 0 block of some sort. If it's a 315eda14cbcSMatt Macy * hole, we create a redaction record that covers the whole range. If 316eda14cbcSMatt Macy * the hole is in a dnode, we need to redact all the blocks in that 317eda14cbcSMatt Macy * hole. If the hole is in the meta-dnode, we instead need to redact 318eda14cbcSMatt Macy * all blocks in every object covered by that hole. If it's a level 0 319eda14cbcSMatt Macy * block, we only need to redact that single block. 320eda14cbcSMatt Macy */ 321eda14cbcSMatt Macy record = kmem_zalloc(sizeof (struct redact_record), KM_SLEEP); 322eda14cbcSMatt Macy record->eos_marker = B_FALSE; 323eda14cbcSMatt Macy 324eda14cbcSMatt Macy record->start_object = record->end_object = zb->zb_object; 325eda14cbcSMatt Macy if (BP_IS_HOLE(bp)) { 326eda14cbcSMatt Macy record->start_blkid = zb->zb_blkid * 327eda14cbcSMatt Macy bp_span_in_blocks(dnp->dn_indblkshift, zb->zb_level); 328eda14cbcSMatt Macy 329eda14cbcSMatt Macy record->end_blkid = ((zb->zb_blkid + 1) * 330eda14cbcSMatt Macy bp_span_in_blocks(dnp->dn_indblkshift, zb->zb_level)) - 1; 331eda14cbcSMatt Macy 332eda14cbcSMatt Macy if (zb->zb_object == DMU_META_DNODE_OBJECT) { 333eda14cbcSMatt Macy record->start_object = record->start_blkid * 334eda14cbcSMatt Macy ((SPA_MINBLOCKSIZE * dnp->dn_datablkszsec) / 335eda14cbcSMatt Macy sizeof (dnode_phys_t)); 336eda14cbcSMatt Macy record->start_blkid = 0; 337eda14cbcSMatt Macy record->end_object = ((record->end_blkid + 338eda14cbcSMatt Macy 1) * ((SPA_MINBLOCKSIZE * dnp->dn_datablkszsec) / 339eda14cbcSMatt Macy sizeof (dnode_phys_t))) - 1; 340eda14cbcSMatt Macy record->end_blkid = UINT64_MAX; 341eda14cbcSMatt Macy } 342eda14cbcSMatt Macy } else if (zb->zb_level != 0 || 343eda14cbcSMatt Macy zb->zb_object == DMU_META_DNODE_OBJECT) { 344eda14cbcSMatt Macy kmem_free(record, sizeof (*record)); 345eda14cbcSMatt Macy return (0); 346eda14cbcSMatt Macy } else { 347eda14cbcSMatt Macy record->start_blkid = record->end_blkid = zb->zb_blkid; 348eda14cbcSMatt Macy } 349eda14cbcSMatt Macy record->indblkshift = dnp->dn_indblkshift; 350eda14cbcSMatt Macy record->datablksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT; 351eda14cbcSMatt Macy record_merge_enqueue(&rta->q, &rta->current_record, record); 352eda14cbcSMatt Macy 353eda14cbcSMatt Macy return (0); 354eda14cbcSMatt Macy } 355eda14cbcSMatt Macy 356da5137abSMartin Matuska static __attribute__((noreturn)) void 357eda14cbcSMatt Macy redact_traverse_thread(void *arg) 358eda14cbcSMatt Macy { 359eda14cbcSMatt Macy struct redact_thread_arg *rt_arg = arg; 360eda14cbcSMatt Macy int err; 361eda14cbcSMatt Macy struct redact_record *data; 362eda14cbcSMatt Macy #ifdef _KERNEL 363eda14cbcSMatt Macy if (rt_arg->os->os_phys->os_type == DMU_OST_ZFS) 364eda14cbcSMatt Macy rt_arg->deleted_objs = zfs_get_deleteq(rt_arg->os); 365eda14cbcSMatt Macy else 366eda14cbcSMatt Macy rt_arg->deleted_objs = objlist_create(); 367eda14cbcSMatt Macy #else 368eda14cbcSMatt Macy rt_arg->deleted_objs = objlist_create(); 369eda14cbcSMatt Macy #endif 370eda14cbcSMatt Macy 371eda14cbcSMatt Macy err = traverse_dataset_resume(rt_arg->ds, rt_arg->txg, 372eda14cbcSMatt Macy &rt_arg->resume, TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, 373eda14cbcSMatt Macy redact_cb, rt_arg); 374eda14cbcSMatt Macy 375eda14cbcSMatt Macy if (err != EINTR) 376eda14cbcSMatt Macy rt_arg->error_code = err; 377eda14cbcSMatt Macy objlist_destroy(rt_arg->deleted_objs); 378eda14cbcSMatt Macy data = kmem_zalloc(sizeof (*data), KM_SLEEP); 379eda14cbcSMatt Macy data->eos_marker = B_TRUE; 380eda14cbcSMatt Macy record_merge_enqueue(&rt_arg->q, &rt_arg->current_record, data); 381eda14cbcSMatt Macy thread_exit(); 382eda14cbcSMatt Macy } 383eda14cbcSMatt Macy 384eda14cbcSMatt Macy static inline void 385eda14cbcSMatt Macy create_zbookmark_from_obj_off(zbookmark_phys_t *zb, uint64_t object, 386eda14cbcSMatt Macy uint64_t blkid) 387eda14cbcSMatt Macy { 388eda14cbcSMatt Macy zb->zb_object = object; 389eda14cbcSMatt Macy zb->zb_level = 0; 390eda14cbcSMatt Macy zb->zb_blkid = blkid; 391eda14cbcSMatt Macy } 392eda14cbcSMatt Macy 393eda14cbcSMatt Macy /* 394eda14cbcSMatt Macy * This is a utility function that can do the comparison for the start or ends 395eda14cbcSMatt Macy * of the ranges in a redact_record. 396eda14cbcSMatt Macy */ 397eda14cbcSMatt Macy static int 398eda14cbcSMatt Macy redact_range_compare(uint64_t obj1, uint64_t off1, uint32_t dbss1, 399eda14cbcSMatt Macy uint64_t obj2, uint64_t off2, uint32_t dbss2) 400eda14cbcSMatt Macy { 401eda14cbcSMatt Macy zbookmark_phys_t z1, z2; 402eda14cbcSMatt Macy create_zbookmark_from_obj_off(&z1, obj1, off1); 403eda14cbcSMatt Macy create_zbookmark_from_obj_off(&z2, obj2, off2); 404eda14cbcSMatt Macy 405eda14cbcSMatt Macy return (zbookmark_compare(dbss1 >> SPA_MINBLOCKSHIFT, 0, 406eda14cbcSMatt Macy dbss2 >> SPA_MINBLOCKSHIFT, 0, &z1, &z2)); 407eda14cbcSMatt Macy } 408eda14cbcSMatt Macy 409eda14cbcSMatt Macy /* 410eda14cbcSMatt Macy * Compare two redaction records by their range's start location. Also makes 411eda14cbcSMatt Macy * eos records always compare last. We use the thread number in the redact_node 412eda14cbcSMatt Macy * to ensure that records do not compare equal (which is not allowed in our avl 413eda14cbcSMatt Macy * trees). 414eda14cbcSMatt Macy */ 415eda14cbcSMatt Macy static int 416eda14cbcSMatt Macy redact_node_compare_start(const void *arg1, const void *arg2) 417eda14cbcSMatt Macy { 418eda14cbcSMatt Macy const struct redact_node *rn1 = arg1; 419eda14cbcSMatt Macy const struct redact_node *rn2 = arg2; 420eda14cbcSMatt Macy const struct redact_record *rr1 = rn1->record; 421eda14cbcSMatt Macy const struct redact_record *rr2 = rn2->record; 422eda14cbcSMatt Macy if (rr1->eos_marker) 423eda14cbcSMatt Macy return (1); 424eda14cbcSMatt Macy if (rr2->eos_marker) 425eda14cbcSMatt Macy return (-1); 426eda14cbcSMatt Macy 427eda14cbcSMatt Macy int cmp = redact_range_compare(rr1->start_object, rr1->start_blkid, 428eda14cbcSMatt Macy rr1->datablksz, rr2->start_object, rr2->start_blkid, 429eda14cbcSMatt Macy rr2->datablksz); 430eda14cbcSMatt Macy if (cmp == 0) 431eda14cbcSMatt Macy cmp = (rn1->thread_num < rn2->thread_num ? -1 : 1); 432eda14cbcSMatt Macy return (cmp); 433eda14cbcSMatt Macy } 434eda14cbcSMatt Macy 435eda14cbcSMatt Macy /* 436eda14cbcSMatt Macy * Compare two redaction records by their range's end location. Also makes 437eda14cbcSMatt Macy * eos records always compare last. We use the thread number in the redact_node 438eda14cbcSMatt Macy * to ensure that records do not compare equal (which is not allowed in our avl 439eda14cbcSMatt Macy * trees). 440eda14cbcSMatt Macy */ 441eda14cbcSMatt Macy static int 442eda14cbcSMatt Macy redact_node_compare_end(const void *arg1, const void *arg2) 443eda14cbcSMatt Macy { 444eda14cbcSMatt Macy const struct redact_node *rn1 = arg1; 445eda14cbcSMatt Macy const struct redact_node *rn2 = arg2; 446eda14cbcSMatt Macy const struct redact_record *srr1 = rn1->record; 447eda14cbcSMatt Macy const struct redact_record *srr2 = rn2->record; 448eda14cbcSMatt Macy if (srr1->eos_marker) 449eda14cbcSMatt Macy return (1); 450eda14cbcSMatt Macy if (srr2->eos_marker) 451eda14cbcSMatt Macy return (-1); 452eda14cbcSMatt Macy 453eda14cbcSMatt Macy int cmp = redact_range_compare(srr1->end_object, srr1->end_blkid, 454eda14cbcSMatt Macy srr1->datablksz, srr2->end_object, srr2->end_blkid, 455eda14cbcSMatt Macy srr2->datablksz); 456eda14cbcSMatt Macy if (cmp == 0) 457eda14cbcSMatt Macy cmp = (rn1->thread_num < rn2->thread_num ? -1 : 1); 458eda14cbcSMatt Macy return (cmp); 459eda14cbcSMatt Macy } 460eda14cbcSMatt Macy 461eda14cbcSMatt Macy /* 462eda14cbcSMatt Macy * Utility function that compares two redaction records to determine if any part 463eda14cbcSMatt Macy * of the "from" record is before any part of the "to" record. Also causes End 464eda14cbcSMatt Macy * of Stream redaction records to compare after all others, so that the 465eda14cbcSMatt Macy * redaction merging logic can stay simple. 466eda14cbcSMatt Macy */ 467eda14cbcSMatt Macy static boolean_t 468eda14cbcSMatt Macy redact_record_before(const struct redact_record *from, 469eda14cbcSMatt Macy const struct redact_record *to) 470eda14cbcSMatt Macy { 471eda14cbcSMatt Macy if (from->eos_marker == B_TRUE) 472eda14cbcSMatt Macy return (B_FALSE); 473eda14cbcSMatt Macy else if (to->eos_marker == B_TRUE) 474eda14cbcSMatt Macy return (B_TRUE); 475eda14cbcSMatt Macy return (redact_range_compare(from->start_object, from->start_blkid, 476eda14cbcSMatt Macy from->datablksz, to->end_object, to->end_blkid, 477eda14cbcSMatt Macy to->datablksz) <= 0); 478eda14cbcSMatt Macy } 479eda14cbcSMatt Macy 480eda14cbcSMatt Macy /* 481eda14cbcSMatt Macy * Pop a new redaction record off the queue, check that the records are in the 482eda14cbcSMatt Macy * right order, and free the old data. 483eda14cbcSMatt Macy */ 484eda14cbcSMatt Macy static struct redact_record * 485eda14cbcSMatt Macy get_next_redact_record(bqueue_t *bq, struct redact_record *prev) 486eda14cbcSMatt Macy { 487eda14cbcSMatt Macy struct redact_record *next = bqueue_dequeue(bq); 488eda14cbcSMatt Macy ASSERT(redact_record_before(prev, next)); 489eda14cbcSMatt Macy kmem_free(prev, sizeof (*prev)); 490eda14cbcSMatt Macy return (next); 491eda14cbcSMatt Macy } 492eda14cbcSMatt Macy 493eda14cbcSMatt Macy /* 494eda14cbcSMatt Macy * Remove the given redaction node from both trees, pull a new redaction record 495eda14cbcSMatt Macy * off the queue, free the old redaction record, update the redaction node, and 496eda14cbcSMatt Macy * reinsert the node into the trees. 497eda14cbcSMatt Macy */ 498eda14cbcSMatt Macy static int 499eda14cbcSMatt Macy update_avl_trees(avl_tree_t *start_tree, avl_tree_t *end_tree, 500eda14cbcSMatt Macy struct redact_node *redact_node) 501eda14cbcSMatt Macy { 502eda14cbcSMatt Macy avl_remove(start_tree, redact_node); 503eda14cbcSMatt Macy avl_remove(end_tree, redact_node); 504eda14cbcSMatt Macy redact_node->record = get_next_redact_record(&redact_node->rt_arg->q, 505eda14cbcSMatt Macy redact_node->record); 506eda14cbcSMatt Macy avl_add(end_tree, redact_node); 507eda14cbcSMatt Macy avl_add(start_tree, redact_node); 508eda14cbcSMatt Macy return (redact_node->rt_arg->error_code); 509eda14cbcSMatt Macy } 510eda14cbcSMatt Macy 511eda14cbcSMatt Macy /* 512eda14cbcSMatt Macy * Synctask for updating redaction lists. We first take this txg's list of 513eda14cbcSMatt Macy * redacted blocks and append those to the redaction list. We then update the 514eda14cbcSMatt Macy * redaction list's bonus buffer. We store the furthest blocks we visited and 515eda14cbcSMatt Macy * the list of snapshots that we're redacting with respect to. We need these so 516eda14cbcSMatt Macy * that redacted sends and receives can be correctly resumed. 517eda14cbcSMatt Macy */ 518eda14cbcSMatt Macy static void 519eda14cbcSMatt Macy redaction_list_update_sync(void *arg, dmu_tx_t *tx) 520eda14cbcSMatt Macy { 521eda14cbcSMatt Macy struct merge_data *md = arg; 522eda14cbcSMatt Macy uint64_t txg = dmu_tx_get_txg(tx); 523eda14cbcSMatt Macy list_t *list = &md->md_blocks[txg & TXG_MASK]; 524eda14cbcSMatt Macy redact_block_phys_t *furthest_visited = 525eda14cbcSMatt Macy &md->md_furthest[txg & TXG_MASK]; 526eda14cbcSMatt Macy objset_t *mos = tx->tx_pool->dp_meta_objset; 527eda14cbcSMatt Macy redaction_list_t *rl = md->md_redaction_list; 528eda14cbcSMatt Macy int bufsize = redact_sync_bufsize; 529eda14cbcSMatt Macy redact_block_phys_t *buf = kmem_alloc(bufsize * sizeof (*buf), 530eda14cbcSMatt Macy KM_SLEEP); 531eda14cbcSMatt Macy int index = 0; 532eda14cbcSMatt Macy 533eda14cbcSMatt Macy dmu_buf_will_dirty(rl->rl_dbuf, tx); 534eda14cbcSMatt Macy 535eda14cbcSMatt Macy for (struct redact_block_list_node *rbln = list_remove_head(list); 536eda14cbcSMatt Macy rbln != NULL; rbln = list_remove_head(list)) { 537eda14cbcSMatt Macy ASSERT3U(rbln->block.rbp_object, <=, 538eda14cbcSMatt Macy furthest_visited->rbp_object); 539eda14cbcSMatt Macy ASSERT(rbln->block.rbp_object < furthest_visited->rbp_object || 540eda14cbcSMatt Macy rbln->block.rbp_blkid <= furthest_visited->rbp_blkid); 541eda14cbcSMatt Macy buf[index] = rbln->block; 542eda14cbcSMatt Macy index++; 543eda14cbcSMatt Macy if (index == bufsize) { 544eda14cbcSMatt Macy dmu_write(mos, rl->rl_object, 545eda14cbcSMatt Macy rl->rl_phys->rlp_num_entries * sizeof (*buf), 546eda14cbcSMatt Macy bufsize * sizeof (*buf), buf, tx); 547eda14cbcSMatt Macy rl->rl_phys->rlp_num_entries += bufsize; 548eda14cbcSMatt Macy index = 0; 549eda14cbcSMatt Macy } 550eda14cbcSMatt Macy kmem_free(rbln, sizeof (*rbln)); 551eda14cbcSMatt Macy } 552eda14cbcSMatt Macy if (index > 0) { 553eda14cbcSMatt Macy dmu_write(mos, rl->rl_object, rl->rl_phys->rlp_num_entries * 554eda14cbcSMatt Macy sizeof (*buf), index * sizeof (*buf), buf, tx); 555eda14cbcSMatt Macy rl->rl_phys->rlp_num_entries += index; 556eda14cbcSMatt Macy } 557eda14cbcSMatt Macy kmem_free(buf, bufsize * sizeof (*buf)); 558eda14cbcSMatt Macy 559eda14cbcSMatt Macy md->md_synctask_txg[txg & TXG_MASK] = B_FALSE; 560eda14cbcSMatt Macy rl->rl_phys->rlp_last_object = furthest_visited->rbp_object; 561eda14cbcSMatt Macy rl->rl_phys->rlp_last_blkid = furthest_visited->rbp_blkid; 562eda14cbcSMatt Macy } 563eda14cbcSMatt Macy 564eda14cbcSMatt Macy static void 565eda14cbcSMatt Macy commit_rl_updates(objset_t *os, struct merge_data *md, uint64_t object, 566eda14cbcSMatt Macy uint64_t blkid) 567eda14cbcSMatt Macy { 568eda14cbcSMatt Macy dmu_tx_t *tx = dmu_tx_create_dd(spa_get_dsl(os->os_spa)->dp_mos_dir); 569eda14cbcSMatt Macy dmu_tx_hold_space(tx, sizeof (struct redact_block_list_node)); 570eda14cbcSMatt Macy VERIFY0(dmu_tx_assign(tx, TXG_WAIT)); 571eda14cbcSMatt Macy uint64_t txg = dmu_tx_get_txg(tx); 572eda14cbcSMatt Macy if (!md->md_synctask_txg[txg & TXG_MASK]) { 573eda14cbcSMatt Macy dsl_sync_task_nowait(dmu_tx_pool(tx), 5742c48331dSMatt Macy redaction_list_update_sync, md, tx); 575eda14cbcSMatt Macy md->md_synctask_txg[txg & TXG_MASK] = B_TRUE; 576eda14cbcSMatt Macy md->md_latest_synctask_txg = txg; 577eda14cbcSMatt Macy } 578eda14cbcSMatt Macy md->md_furthest[txg & TXG_MASK].rbp_object = object; 579eda14cbcSMatt Macy md->md_furthest[txg & TXG_MASK].rbp_blkid = blkid; 580eda14cbcSMatt Macy list_move_tail(&md->md_blocks[txg & TXG_MASK], 581eda14cbcSMatt Macy &md->md_redact_block_pending); 582eda14cbcSMatt Macy dmu_tx_commit(tx); 583eda14cbcSMatt Macy md->md_last_time = gethrtime(); 584eda14cbcSMatt Macy } 585eda14cbcSMatt Macy 586eda14cbcSMatt Macy /* 587eda14cbcSMatt Macy * We want to store the list of blocks that we're redacting in the bookmark's 588eda14cbcSMatt Macy * redaction list. However, this list is stored in the MOS, which means it can 589eda14cbcSMatt Macy * only be written to in syncing context. To get around this, we create a 590eda14cbcSMatt Macy * synctask that will write to the mos for us. We tell it what to write by 591eda14cbcSMatt Macy * a linked list for each current transaction group; every time we decide to 592eda14cbcSMatt Macy * redact a block, we append it to the transaction group that is currently in 593eda14cbcSMatt Macy * open context. We also update some progress information that the synctask 594eda14cbcSMatt Macy * will store to enable resumable redacted sends. 595eda14cbcSMatt Macy */ 596eda14cbcSMatt Macy static void 597eda14cbcSMatt Macy update_redaction_list(struct merge_data *md, objset_t *os, 598eda14cbcSMatt Macy uint64_t object, uint64_t blkid, uint64_t endblkid, uint32_t blksz) 599eda14cbcSMatt Macy { 600eda14cbcSMatt Macy boolean_t enqueue = B_FALSE; 601eda14cbcSMatt Macy redact_block_phys_t cur = {0}; 602eda14cbcSMatt Macy uint64_t count = endblkid - blkid + 1; 603eda14cbcSMatt Macy while (count > REDACT_BLOCK_MAX_COUNT) { 604eda14cbcSMatt Macy update_redaction_list(md, os, object, blkid, 605eda14cbcSMatt Macy blkid + REDACT_BLOCK_MAX_COUNT - 1, blksz); 606eda14cbcSMatt Macy blkid += REDACT_BLOCK_MAX_COUNT; 607eda14cbcSMatt Macy count -= REDACT_BLOCK_MAX_COUNT; 608eda14cbcSMatt Macy } 609eda14cbcSMatt Macy redact_block_phys_t *coalesce = &md->md_coalesce_block; 610eda14cbcSMatt Macy boolean_t new; 611eda14cbcSMatt Macy if (coalesce->rbp_size_count == 0) { 612eda14cbcSMatt Macy new = B_TRUE; 613eda14cbcSMatt Macy enqueue = B_FALSE; 614eda14cbcSMatt Macy } else { 615eda14cbcSMatt Macy uint64_t old_count = redact_block_get_count(coalesce); 616eda14cbcSMatt Macy if (coalesce->rbp_object == object && 617eda14cbcSMatt Macy coalesce->rbp_blkid + old_count == blkid && 618eda14cbcSMatt Macy old_count + count <= REDACT_BLOCK_MAX_COUNT) { 619eda14cbcSMatt Macy ASSERT3U(redact_block_get_size(coalesce), ==, blksz); 620eda14cbcSMatt Macy redact_block_set_count(coalesce, old_count + count); 621eda14cbcSMatt Macy new = B_FALSE; 622eda14cbcSMatt Macy enqueue = B_FALSE; 623eda14cbcSMatt Macy } else { 624eda14cbcSMatt Macy new = B_TRUE; 625eda14cbcSMatt Macy enqueue = B_TRUE; 626eda14cbcSMatt Macy } 627eda14cbcSMatt Macy } 628eda14cbcSMatt Macy 629eda14cbcSMatt Macy if (new) { 630eda14cbcSMatt Macy cur = *coalesce; 631eda14cbcSMatt Macy coalesce->rbp_blkid = blkid; 632eda14cbcSMatt Macy coalesce->rbp_object = object; 633eda14cbcSMatt Macy 634eda14cbcSMatt Macy redact_block_set_count(coalesce, count); 635eda14cbcSMatt Macy redact_block_set_size(coalesce, blksz); 636eda14cbcSMatt Macy } 637eda14cbcSMatt Macy 638eda14cbcSMatt Macy if (enqueue && redact_block_get_size(&cur) != 0) { 639eda14cbcSMatt Macy struct redact_block_list_node *rbln = 640eda14cbcSMatt Macy kmem_alloc(sizeof (struct redact_block_list_node), 641eda14cbcSMatt Macy KM_SLEEP); 642eda14cbcSMatt Macy rbln->block = cur; 643eda14cbcSMatt Macy list_insert_tail(&md->md_redact_block_pending, rbln); 644eda14cbcSMatt Macy } 645eda14cbcSMatt Macy 646eda14cbcSMatt Macy if (gethrtime() > md->md_last_time + 647eda14cbcSMatt Macy redaction_list_update_interval_ns) { 648eda14cbcSMatt Macy commit_rl_updates(os, md, object, blkid); 649eda14cbcSMatt Macy } 650eda14cbcSMatt Macy } 651eda14cbcSMatt Macy 652eda14cbcSMatt Macy /* 653eda14cbcSMatt Macy * This thread merges all the redaction records provided by the worker threads, 654eda14cbcSMatt Macy * and determines which blocks are redacted by all the snapshots. The algorithm 655eda14cbcSMatt Macy * for doing so is similar to performing a merge in mergesort with n sub-lists 656eda14cbcSMatt Macy * instead of 2, with some added complexity due to the fact that the entries are 657eda14cbcSMatt Macy * ranges, not just single blocks. This algorithm relies on the fact that the 658eda14cbcSMatt Macy * queues are sorted, which is ensured by the fact that traverse_dataset 659eda14cbcSMatt Macy * traverses the dataset in a consistent order. We pull one entry off the front 660eda14cbcSMatt Macy * of the queues of each secure dataset traversal thread. Then we repeat the 661eda14cbcSMatt Macy * following: each record represents a range of blocks modified by one of the 662eda14cbcSMatt Macy * redaction snapshots, and each block in that range may need to be redacted in 663eda14cbcSMatt Macy * the send stream. Find the record with the latest start of its range, and the 664eda14cbcSMatt Macy * record with the earliest end of its range. If the last start is before the 665eda14cbcSMatt Macy * first end, then we know that the blocks in the range [last_start, first_end] 666eda14cbcSMatt Macy * are covered by all of the ranges at the front of the queues, which means 667eda14cbcSMatt Macy * every thread redacts that whole range. For example, let's say the ranges on 668eda14cbcSMatt Macy * each queue look like this: 669eda14cbcSMatt Macy * 670eda14cbcSMatt Macy * Block Id 1 2 3 4 5 6 7 8 9 10 11 671eda14cbcSMatt Macy * Thread 1 | [====================] 672eda14cbcSMatt Macy * Thread 2 | [========] 673eda14cbcSMatt Macy * Thread 3 | [=================] 674eda14cbcSMatt Macy * 675eda14cbcSMatt Macy * Thread 3 has the last start (5), and the thread 2 has the last end (6). All 676eda14cbcSMatt Macy * three threads modified the range [5,6], so that data should not be sent over 677eda14cbcSMatt Macy * the wire. After we've determined whether or not to redact anything, we take 678eda14cbcSMatt Macy * the record with the first end. We discard that record, and pull a new one 679eda14cbcSMatt Macy * off the front of the queue it came from. In the above example, we would 680eda14cbcSMatt Macy * discard Thread 2's record, and pull a new one. Let's say the next record we 681eda14cbcSMatt Macy * pulled from Thread 2 covered range [10,11]. The new layout would look like 682eda14cbcSMatt Macy * this: 683eda14cbcSMatt Macy * 684eda14cbcSMatt Macy * Block Id 1 2 3 4 5 6 7 8 9 10 11 685eda14cbcSMatt Macy * Thread 1 | [====================] 686eda14cbcSMatt Macy * Thread 2 | [==] 687eda14cbcSMatt Macy * Thread 3 | [=================] 688eda14cbcSMatt Macy * 689eda14cbcSMatt Macy * When we compare the last start (10, from Thread 2) and the first end (9, from 690eda14cbcSMatt Macy * Thread 1), we see that the last start is greater than the first end. 691eda14cbcSMatt Macy * Therefore, we do not redact anything from these records. We'll iterate by 692eda14cbcSMatt Macy * replacing the record from Thread 1. 693eda14cbcSMatt Macy * 694eda14cbcSMatt Macy * We iterate by replacing the record with the lowest end because we know 695eda14cbcSMatt Macy * that the record with the lowest end has helped us as much as it can. All the 696eda14cbcSMatt Macy * ranges before it that we will ever redact have been redacted. In addition, 697eda14cbcSMatt Macy * by replacing the one with the lowest end, we guarantee we catch all ranges 698eda14cbcSMatt Macy * that need to be redacted. For example, if in the case above we had replaced 699eda14cbcSMatt Macy * the record from Thread 1 instead, we might have ended up with the following: 700eda14cbcSMatt Macy * 701eda14cbcSMatt Macy * Block Id 1 2 3 4 5 6 7 8 9 10 11 12 702eda14cbcSMatt Macy * Thread 1 | [==] 703eda14cbcSMatt Macy * Thread 2 | [========] 704eda14cbcSMatt Macy * Thread 3 | [=================] 705eda14cbcSMatt Macy * 706eda14cbcSMatt Macy * If the next record from Thread 2 had been [8,10], for example, we should have 707eda14cbcSMatt Macy * redacted part of that range, but because we updated Thread 1's record, we 708eda14cbcSMatt Macy * missed it. 709eda14cbcSMatt Macy * 710eda14cbcSMatt Macy * We implement this algorithm by using two trees. The first sorts the 711eda14cbcSMatt Macy * redaction records by their start_zb, and the second sorts them by their 712eda14cbcSMatt Macy * end_zb. We use these to find the record with the last start and the record 713eda14cbcSMatt Macy * with the first end. We create a record with that start and end, and send it 714eda14cbcSMatt Macy * on. The overall runtime of this implementation is O(n log m), where n is the 715eda14cbcSMatt Macy * total number of redaction records from all the different redaction snapshots, 716eda14cbcSMatt Macy * and m is the number of redaction snapshots. 717eda14cbcSMatt Macy * 718eda14cbcSMatt Macy * If we redact with respect to zero snapshots, we create a redaction 719eda14cbcSMatt Macy * record with the start object and blkid to 0, and the end object and blkid to 720eda14cbcSMatt Macy * UINT64_MAX. This will result in us redacting every block. 721eda14cbcSMatt Macy */ 722eda14cbcSMatt Macy static int 723eda14cbcSMatt Macy perform_thread_merge(bqueue_t *q, uint32_t num_threads, 724eda14cbcSMatt Macy struct redact_thread_arg *thread_args, boolean_t *cancel) 725eda14cbcSMatt Macy { 726eda14cbcSMatt Macy struct redact_node *redact_nodes = NULL; 727eda14cbcSMatt Macy avl_tree_t start_tree, end_tree; 728eda14cbcSMatt Macy struct redact_record *record; 729eda14cbcSMatt Macy struct redact_record *current_record = NULL; 730eda14cbcSMatt Macy int err = 0; 731eda14cbcSMatt Macy struct merge_data md = { {0} }; 732eda14cbcSMatt Macy list_create(&md.md_redact_block_pending, 733eda14cbcSMatt Macy sizeof (struct redact_block_list_node), 734eda14cbcSMatt Macy offsetof(struct redact_block_list_node, node)); 735eda14cbcSMatt Macy 736eda14cbcSMatt Macy /* 737eda14cbcSMatt Macy * If we're redacting with respect to zero snapshots, then no data is 738eda14cbcSMatt Macy * permitted to be sent. We enqueue a record that redacts all blocks, 739eda14cbcSMatt Macy * and an eos marker. 740eda14cbcSMatt Macy */ 741eda14cbcSMatt Macy if (num_threads == 0) { 742eda14cbcSMatt Macy record = kmem_zalloc(sizeof (struct redact_record), 743eda14cbcSMatt Macy KM_SLEEP); 744eda14cbcSMatt Macy // We can't redact object 0, so don't try. 745eda14cbcSMatt Macy record->start_object = 1; 746eda14cbcSMatt Macy record->start_blkid = 0; 747eda14cbcSMatt Macy record->end_object = record->end_blkid = UINT64_MAX; 748eda14cbcSMatt Macy bqueue_enqueue(q, record, sizeof (*record)); 749eda14cbcSMatt Macy return (0); 750eda14cbcSMatt Macy } 7512ad756a6SMartin Matuska redact_nodes = vmem_zalloc(num_threads * 752eda14cbcSMatt Macy sizeof (*redact_nodes), KM_SLEEP); 753eda14cbcSMatt Macy 754eda14cbcSMatt Macy avl_create(&start_tree, redact_node_compare_start, 755eda14cbcSMatt Macy sizeof (struct redact_node), 756eda14cbcSMatt Macy offsetof(struct redact_node, avl_node_start)); 757eda14cbcSMatt Macy avl_create(&end_tree, redact_node_compare_end, 758eda14cbcSMatt Macy sizeof (struct redact_node), 759eda14cbcSMatt Macy offsetof(struct redact_node, avl_node_end)); 760eda14cbcSMatt Macy 761eda14cbcSMatt Macy for (int i = 0; i < num_threads; i++) { 762eda14cbcSMatt Macy struct redact_node *node = &redact_nodes[i]; 763eda14cbcSMatt Macy struct redact_thread_arg *targ = &thread_args[i]; 764eda14cbcSMatt Macy node->record = bqueue_dequeue(&targ->q); 765eda14cbcSMatt Macy node->rt_arg = targ; 766eda14cbcSMatt Macy node->thread_num = i; 767eda14cbcSMatt Macy avl_add(&start_tree, node); 768eda14cbcSMatt Macy avl_add(&end_tree, node); 769eda14cbcSMatt Macy } 770eda14cbcSMatt Macy 771eda14cbcSMatt Macy /* 772eda14cbcSMatt Macy * Once the first record in the end tree has returned EOS, every record 773eda14cbcSMatt Macy * must be an EOS record, so we should stop. 774eda14cbcSMatt Macy */ 775eda14cbcSMatt Macy while (err == 0 && !((struct redact_node *)avl_first(&end_tree))-> 776eda14cbcSMatt Macy record->eos_marker) { 777eda14cbcSMatt Macy if (*cancel) { 778eda14cbcSMatt Macy err = EINTR; 779eda14cbcSMatt Macy break; 780eda14cbcSMatt Macy } 781eda14cbcSMatt Macy struct redact_node *last_start = avl_last(&start_tree); 782eda14cbcSMatt Macy struct redact_node *first_end = avl_first(&end_tree); 783eda14cbcSMatt Macy 784eda14cbcSMatt Macy /* 785eda14cbcSMatt Macy * If the last start record is before the first end record, 786eda14cbcSMatt Macy * then we have blocks that are redacted by all threads. 787eda14cbcSMatt Macy * Therefore, we should redact them. Copy the record, and send 788eda14cbcSMatt Macy * it to the main thread. 789eda14cbcSMatt Macy */ 790eda14cbcSMatt Macy if (redact_record_before(last_start->record, 791eda14cbcSMatt Macy first_end->record)) { 792eda14cbcSMatt Macy record = kmem_zalloc(sizeof (struct redact_record), 793eda14cbcSMatt Macy KM_SLEEP); 794eda14cbcSMatt Macy *record = *first_end->record; 795eda14cbcSMatt Macy record->start_object = last_start->record->start_object; 796eda14cbcSMatt Macy record->start_blkid = last_start->record->start_blkid; 797eda14cbcSMatt Macy record_merge_enqueue(q, ¤t_record, 798eda14cbcSMatt Macy record); 799eda14cbcSMatt Macy } 800eda14cbcSMatt Macy err = update_avl_trees(&start_tree, &end_tree, first_end); 801eda14cbcSMatt Macy } 802eda14cbcSMatt Macy 803eda14cbcSMatt Macy /* 804eda14cbcSMatt Macy * We're done; if we were cancelled, we need to cancel our workers and 805eda14cbcSMatt Macy * clear out their queues. Either way, we need to remove every thread's 806eda14cbcSMatt Macy * redact_node struct from the avl trees. 807eda14cbcSMatt Macy */ 808eda14cbcSMatt Macy for (int i = 0; i < num_threads; i++) { 809eda14cbcSMatt Macy if (err != 0) { 810eda14cbcSMatt Macy thread_args[i].cancel = B_TRUE; 811eda14cbcSMatt Macy while (!redact_nodes[i].record->eos_marker) { 812eda14cbcSMatt Macy (void) update_avl_trees(&start_tree, &end_tree, 813eda14cbcSMatt Macy &redact_nodes[i]); 814eda14cbcSMatt Macy } 815eda14cbcSMatt Macy } 816eda14cbcSMatt Macy avl_remove(&start_tree, &redact_nodes[i]); 817eda14cbcSMatt Macy avl_remove(&end_tree, &redact_nodes[i]); 818eda14cbcSMatt Macy kmem_free(redact_nodes[i].record, 819eda14cbcSMatt Macy sizeof (struct redact_record)); 8203f9d360cSMartin Matuska bqueue_destroy(&thread_args[i].q); 821eda14cbcSMatt Macy } 822eda14cbcSMatt Macy 823eda14cbcSMatt Macy avl_destroy(&start_tree); 824eda14cbcSMatt Macy avl_destroy(&end_tree); 8252ad756a6SMartin Matuska vmem_free(redact_nodes, num_threads * sizeof (*redact_nodes)); 826eda14cbcSMatt Macy if (current_record != NULL) 827c7046f76SMartin Matuska bqueue_enqueue(q, current_record, sizeof (*current_record)); 828eda14cbcSMatt Macy return (err); 829eda14cbcSMatt Macy } 830eda14cbcSMatt Macy 831eda14cbcSMatt Macy struct redact_merge_thread_arg { 832eda14cbcSMatt Macy bqueue_t q; 833eda14cbcSMatt Macy spa_t *spa; 834eda14cbcSMatt Macy int numsnaps; 835eda14cbcSMatt Macy struct redact_thread_arg *thr_args; 836eda14cbcSMatt Macy boolean_t cancel; 837eda14cbcSMatt Macy int error_code; 838eda14cbcSMatt Macy }; 839eda14cbcSMatt Macy 840da5137abSMartin Matuska static __attribute__((noreturn)) void 841eda14cbcSMatt Macy redact_merge_thread(void *arg) 842eda14cbcSMatt Macy { 843eda14cbcSMatt Macy struct redact_merge_thread_arg *rmta = arg; 844eda14cbcSMatt Macy rmta->error_code = perform_thread_merge(&rmta->q, 845eda14cbcSMatt Macy rmta->numsnaps, rmta->thr_args, &rmta->cancel); 846eda14cbcSMatt Macy struct redact_record *rec = kmem_zalloc(sizeof (*rec), KM_SLEEP); 847eda14cbcSMatt Macy rec->eos_marker = B_TRUE; 848eda14cbcSMatt Macy bqueue_enqueue_flush(&rmta->q, rec, 1); 849eda14cbcSMatt Macy thread_exit(); 850eda14cbcSMatt Macy } 851eda14cbcSMatt Macy 852eda14cbcSMatt Macy /* 853eda14cbcSMatt Macy * Find the next object in or after the redaction range passed in, and hold 854eda14cbcSMatt Macy * its dnode with the provided tag. Also update *object to contain the new 855eda14cbcSMatt Macy * object number. 856eda14cbcSMatt Macy */ 857eda14cbcSMatt Macy static int 858a0b956f5SMartin Matuska hold_next_object(objset_t *os, struct redact_record *rec, const void *tag, 859eda14cbcSMatt Macy uint64_t *object, dnode_t **dn) 860eda14cbcSMatt Macy { 861eda14cbcSMatt Macy int err = 0; 862eda14cbcSMatt Macy if (*dn != NULL) 8637877fdebSMatt Macy dnode_rele(*dn, tag); 864eda14cbcSMatt Macy *dn = NULL; 865eda14cbcSMatt Macy if (*object < rec->start_object) { 866eda14cbcSMatt Macy *object = rec->start_object - 1; 867eda14cbcSMatt Macy } 868eda14cbcSMatt Macy err = dmu_object_next(os, object, B_FALSE, 0); 869eda14cbcSMatt Macy if (err != 0) 870eda14cbcSMatt Macy return (err); 871eda14cbcSMatt Macy 872eda14cbcSMatt Macy err = dnode_hold(os, *object, tag, dn); 873eda14cbcSMatt Macy while (err == 0 && (*object < rec->start_object || 874eda14cbcSMatt Macy DMU_OT_IS_METADATA((*dn)->dn_type))) { 875eda14cbcSMatt Macy dnode_rele(*dn, tag); 876eda14cbcSMatt Macy *dn = NULL; 877eda14cbcSMatt Macy err = dmu_object_next(os, object, B_FALSE, 0); 878eda14cbcSMatt Macy if (err != 0) 879eda14cbcSMatt Macy break; 880eda14cbcSMatt Macy err = dnode_hold(os, *object, tag, dn); 881eda14cbcSMatt Macy } 882eda14cbcSMatt Macy return (err); 883eda14cbcSMatt Macy } 884eda14cbcSMatt Macy 885eda14cbcSMatt Macy static int 886eda14cbcSMatt Macy perform_redaction(objset_t *os, redaction_list_t *rl, 887eda14cbcSMatt Macy struct redact_merge_thread_arg *rmta) 888eda14cbcSMatt Macy { 889eda14cbcSMatt Macy int err = 0; 890eda14cbcSMatt Macy bqueue_t *q = &rmta->q; 891eda14cbcSMatt Macy struct redact_record *rec = NULL; 892eda14cbcSMatt Macy struct merge_data md = { {0} }; 893eda14cbcSMatt Macy 894eda14cbcSMatt Macy list_create(&md.md_redact_block_pending, 895eda14cbcSMatt Macy sizeof (struct redact_block_list_node), 896eda14cbcSMatt Macy offsetof(struct redact_block_list_node, node)); 897eda14cbcSMatt Macy md.md_redaction_list = rl; 898eda14cbcSMatt Macy 899eda14cbcSMatt Macy for (int i = 0; i < TXG_SIZE; i++) { 900eda14cbcSMatt Macy list_create(&md.md_blocks[i], 901eda14cbcSMatt Macy sizeof (struct redact_block_list_node), 902eda14cbcSMatt Macy offsetof(struct redact_block_list_node, node)); 903eda14cbcSMatt Macy } 904eda14cbcSMatt Macy dnode_t *dn = NULL; 905eda14cbcSMatt Macy uint64_t prev_obj = 0; 906eda14cbcSMatt Macy for (rec = bqueue_dequeue(q); !rec->eos_marker && err == 0; 907eda14cbcSMatt Macy rec = get_next_redact_record(q, rec)) { 908eda14cbcSMatt Macy ASSERT3U(rec->start_object, !=, 0); 909eda14cbcSMatt Macy uint64_t object; 910eda14cbcSMatt Macy if (prev_obj != rec->start_object) { 911eda14cbcSMatt Macy object = rec->start_object - 1; 912eda14cbcSMatt Macy err = hold_next_object(os, rec, FTAG, &object, &dn); 913eda14cbcSMatt Macy } else { 914eda14cbcSMatt Macy object = prev_obj; 915eda14cbcSMatt Macy } 916eda14cbcSMatt Macy while (err == 0 && object <= rec->end_object) { 917aca928a5SMartin Matuska if (issig()) { 918eda14cbcSMatt Macy err = EINTR; 919eda14cbcSMatt Macy break; 920eda14cbcSMatt Macy } 921eda14cbcSMatt Macy /* 922eda14cbcSMatt Macy * Part of the current object is contained somewhere in 923eda14cbcSMatt Macy * the range covered by rec. 924eda14cbcSMatt Macy */ 925eda14cbcSMatt Macy uint64_t startblkid; 926eda14cbcSMatt Macy uint64_t endblkid; 927eda14cbcSMatt Macy uint64_t maxblkid = dn->dn_phys->dn_maxblkid; 928eda14cbcSMatt Macy 929eda14cbcSMatt Macy if (rec->start_object < object) 930eda14cbcSMatt Macy startblkid = 0; 931eda14cbcSMatt Macy else if (rec->start_blkid > maxblkid) 932eda14cbcSMatt Macy break; 933eda14cbcSMatt Macy else 934eda14cbcSMatt Macy startblkid = rec->start_blkid; 935eda14cbcSMatt Macy 936eda14cbcSMatt Macy if (rec->end_object > object || rec->end_blkid > 937eda14cbcSMatt Macy maxblkid) { 938eda14cbcSMatt Macy endblkid = maxblkid; 939eda14cbcSMatt Macy } else { 940eda14cbcSMatt Macy endblkid = rec->end_blkid; 941eda14cbcSMatt Macy } 942eda14cbcSMatt Macy update_redaction_list(&md, os, object, startblkid, 943eda14cbcSMatt Macy endblkid, dn->dn_datablksz); 944eda14cbcSMatt Macy 945eda14cbcSMatt Macy if (object == rec->end_object) 946eda14cbcSMatt Macy break; 947eda14cbcSMatt Macy err = hold_next_object(os, rec, FTAG, &object, &dn); 948eda14cbcSMatt Macy } 949eda14cbcSMatt Macy if (err == ESRCH) 950eda14cbcSMatt Macy err = 0; 951eda14cbcSMatt Macy if (dn != NULL) 952eda14cbcSMatt Macy prev_obj = object; 953eda14cbcSMatt Macy } 954eda14cbcSMatt Macy if (err == 0 && dn != NULL) 955eda14cbcSMatt Macy dnode_rele(dn, FTAG); 956eda14cbcSMatt Macy 957eda14cbcSMatt Macy if (err == ESRCH) 958eda14cbcSMatt Macy err = 0; 959eda14cbcSMatt Macy rmta->cancel = B_TRUE; 960eda14cbcSMatt Macy while (!rec->eos_marker) 961eda14cbcSMatt Macy rec = get_next_redact_record(q, rec); 962eda14cbcSMatt Macy kmem_free(rec, sizeof (*rec)); 963eda14cbcSMatt Macy 964eda14cbcSMatt Macy /* 965eda14cbcSMatt Macy * There may be a block that's being coalesced, sync that out before we 966eda14cbcSMatt Macy * return. 967eda14cbcSMatt Macy */ 968eda14cbcSMatt Macy if (err == 0 && md.md_coalesce_block.rbp_size_count != 0) { 969eda14cbcSMatt Macy struct redact_block_list_node *rbln = 970eda14cbcSMatt Macy kmem_alloc(sizeof (struct redact_block_list_node), 971eda14cbcSMatt Macy KM_SLEEP); 972eda14cbcSMatt Macy rbln->block = md.md_coalesce_block; 973eda14cbcSMatt Macy list_insert_tail(&md.md_redact_block_pending, rbln); 974eda14cbcSMatt Macy } 975eda14cbcSMatt Macy commit_rl_updates(os, &md, UINT64_MAX, UINT64_MAX); 976eda14cbcSMatt Macy 977eda14cbcSMatt Macy /* 978eda14cbcSMatt Macy * Wait for all the redaction info to sync out before we return, so that 979eda14cbcSMatt Macy * anyone who attempts to resume this redaction will have all the data 980eda14cbcSMatt Macy * they need. 981eda14cbcSMatt Macy */ 982eda14cbcSMatt Macy dsl_pool_t *dp = spa_get_dsl(os->os_spa); 983eda14cbcSMatt Macy if (md.md_latest_synctask_txg != 0) 984eda14cbcSMatt Macy txg_wait_synced(dp, md.md_latest_synctask_txg); 985eda14cbcSMatt Macy for (int i = 0; i < TXG_SIZE; i++) 986eda14cbcSMatt Macy list_destroy(&md.md_blocks[i]); 987eda14cbcSMatt Macy return (err); 988eda14cbcSMatt Macy } 989eda14cbcSMatt Macy 990eda14cbcSMatt Macy static boolean_t 991eda14cbcSMatt Macy redact_snaps_contains(uint64_t *snaps, uint64_t num_snaps, uint64_t guid) 992eda14cbcSMatt Macy { 993eda14cbcSMatt Macy for (int i = 0; i < num_snaps; i++) { 994eda14cbcSMatt Macy if (snaps[i] == guid) 995eda14cbcSMatt Macy return (B_TRUE); 996eda14cbcSMatt Macy } 997eda14cbcSMatt Macy return (B_FALSE); 998eda14cbcSMatt Macy } 999eda14cbcSMatt Macy 1000eda14cbcSMatt Macy int 1001eda14cbcSMatt Macy dmu_redact_snap(const char *snapname, nvlist_t *redactnvl, 1002eda14cbcSMatt Macy const char *redactbook) 1003eda14cbcSMatt Macy { 1004eda14cbcSMatt Macy int err = 0; 1005eda14cbcSMatt Macy dsl_pool_t *dp = NULL; 1006eda14cbcSMatt Macy dsl_dataset_t *ds = NULL; 1007eda14cbcSMatt Macy int numsnaps = 0; 1008eda14cbcSMatt Macy objset_t *os; 1009eda14cbcSMatt Macy struct redact_thread_arg *args = NULL; 1010eda14cbcSMatt Macy redaction_list_t *new_rl = NULL; 10112c48331dSMatt Macy char *newredactbook; 1012eda14cbcSMatt Macy 1013eda14cbcSMatt Macy if ((err = dsl_pool_hold(snapname, FTAG, &dp)) != 0) 1014eda14cbcSMatt Macy return (err); 1015eda14cbcSMatt Macy 10162c48331dSMatt Macy newredactbook = kmem_zalloc(sizeof (char) * ZFS_MAX_DATASET_NAME_LEN, 10172c48331dSMatt Macy KM_SLEEP); 10182c48331dSMatt Macy 1019eda14cbcSMatt Macy if ((err = dsl_dataset_hold_flags(dp, snapname, DS_HOLD_FLAG_DECRYPT, 1020eda14cbcSMatt Macy FTAG, &ds)) != 0) { 1021eda14cbcSMatt Macy goto out; 1022eda14cbcSMatt Macy } 1023eda14cbcSMatt Macy dsl_dataset_long_hold(ds, FTAG); 1024eda14cbcSMatt Macy if (!ds->ds_is_snapshot || dmu_objset_from_ds(ds, &os) != 0) { 1025eda14cbcSMatt Macy err = EINVAL; 1026eda14cbcSMatt Macy goto out; 1027eda14cbcSMatt Macy } 1028eda14cbcSMatt Macy if (dsl_dataset_feature_is_active(ds, SPA_FEATURE_REDACTED_DATASETS)) { 1029eda14cbcSMatt Macy err = EALREADY; 1030eda14cbcSMatt Macy goto out; 1031eda14cbcSMatt Macy } 1032eda14cbcSMatt Macy 1033eda14cbcSMatt Macy numsnaps = fnvlist_num_pairs(redactnvl); 1034eda14cbcSMatt Macy if (numsnaps > 0) 10352ad756a6SMartin Matuska args = vmem_zalloc(numsnaps * sizeof (*args), KM_SLEEP); 1036eda14cbcSMatt Macy 1037eda14cbcSMatt Macy nvpair_t *pair = NULL; 1038eda14cbcSMatt Macy for (int i = 0; i < numsnaps; i++) { 1039eda14cbcSMatt Macy pair = nvlist_next_nvpair(redactnvl, pair); 1040eda14cbcSMatt Macy const char *name = nvpair_name(pair); 1041eda14cbcSMatt Macy struct redact_thread_arg *rta = &args[i]; 1042eda14cbcSMatt Macy err = dsl_dataset_hold_flags(dp, name, DS_HOLD_FLAG_DECRYPT, 1043eda14cbcSMatt Macy FTAG, &rta->ds); 1044eda14cbcSMatt Macy if (err != 0) 1045eda14cbcSMatt Macy break; 1046eda14cbcSMatt Macy /* 1047eda14cbcSMatt Macy * We want to do the long hold before we can get any other 1048eda14cbcSMatt Macy * errors, because the cleanup code will release the long 1049eda14cbcSMatt Macy * hold if rta->ds is filled in. 1050eda14cbcSMatt Macy */ 1051eda14cbcSMatt Macy dsl_dataset_long_hold(rta->ds, FTAG); 1052eda14cbcSMatt Macy 1053eda14cbcSMatt Macy err = dmu_objset_from_ds(rta->ds, &rta->os); 1054eda14cbcSMatt Macy if (err != 0) 1055eda14cbcSMatt Macy break; 1056eda14cbcSMatt Macy if (!dsl_dataset_is_before(rta->ds, ds, 0)) { 1057eda14cbcSMatt Macy err = EINVAL; 1058eda14cbcSMatt Macy break; 1059eda14cbcSMatt Macy } 1060eda14cbcSMatt Macy if (dsl_dataset_feature_is_active(rta->ds, 1061eda14cbcSMatt Macy SPA_FEATURE_REDACTED_DATASETS)) { 1062eda14cbcSMatt Macy err = EALREADY; 1063eda14cbcSMatt Macy break; 1064eda14cbcSMatt Macy 1065eda14cbcSMatt Macy } 1066eda14cbcSMatt Macy } 1067eda14cbcSMatt Macy if (err != 0) 1068eda14cbcSMatt Macy goto out; 1069180f8225SMatt Macy VERIFY3P(nvlist_next_nvpair(redactnvl, pair), ==, NULL); 1070eda14cbcSMatt Macy 1071eda14cbcSMatt Macy boolean_t resuming = B_FALSE; 1072eda14cbcSMatt Macy zfs_bookmark_phys_t bookmark; 1073eda14cbcSMatt Macy 1074eda14cbcSMatt Macy (void) strlcpy(newredactbook, snapname, ZFS_MAX_DATASET_NAME_LEN); 1075eda14cbcSMatt Macy char *c = strchr(newredactbook, '@'); 1076eda14cbcSMatt Macy ASSERT3P(c, !=, NULL); 1077eda14cbcSMatt Macy int n = snprintf(c, ZFS_MAX_DATASET_NAME_LEN - (c - newredactbook), 1078eda14cbcSMatt Macy "#%s", redactbook); 1079eda14cbcSMatt Macy if (n >= ZFS_MAX_DATASET_NAME_LEN - (c - newredactbook)) { 1080eda14cbcSMatt Macy dsl_pool_rele(dp, FTAG); 10812c48331dSMatt Macy kmem_free(newredactbook, 10822c48331dSMatt Macy sizeof (char) * ZFS_MAX_DATASET_NAME_LEN); 10832c48331dSMatt Macy if (args != NULL) 10842ad756a6SMartin Matuska vmem_free(args, numsnaps * sizeof (*args)); 1085eda14cbcSMatt Macy return (SET_ERROR(ENAMETOOLONG)); 1086eda14cbcSMatt Macy } 1087eda14cbcSMatt Macy err = dsl_bookmark_lookup(dp, newredactbook, NULL, &bookmark); 1088eda14cbcSMatt Macy if (err == 0) { 1089eda14cbcSMatt Macy resuming = B_TRUE; 1090eda14cbcSMatt Macy if (bookmark.zbm_redaction_obj == 0) { 1091eda14cbcSMatt Macy err = EEXIST; 1092eda14cbcSMatt Macy goto out; 1093eda14cbcSMatt Macy } 1094eda14cbcSMatt Macy err = dsl_redaction_list_hold_obj(dp, 1095eda14cbcSMatt Macy bookmark.zbm_redaction_obj, FTAG, &new_rl); 1096eda14cbcSMatt Macy if (err != 0) { 1097eda14cbcSMatt Macy err = EIO; 1098eda14cbcSMatt Macy goto out; 1099eda14cbcSMatt Macy } 1100eda14cbcSMatt Macy dsl_redaction_list_long_hold(dp, new_rl, FTAG); 1101eda14cbcSMatt Macy if (new_rl->rl_phys->rlp_num_snaps != numsnaps) { 1102eda14cbcSMatt Macy err = ESRCH; 1103eda14cbcSMatt Macy goto out; 1104eda14cbcSMatt Macy } 1105eda14cbcSMatt Macy for (int i = 0; i < numsnaps; i++) { 1106eda14cbcSMatt Macy struct redact_thread_arg *rta = &args[i]; 1107eda14cbcSMatt Macy if (!redact_snaps_contains(new_rl->rl_phys->rlp_snaps, 1108eda14cbcSMatt Macy new_rl->rl_phys->rlp_num_snaps, 1109eda14cbcSMatt Macy dsl_dataset_phys(rta->ds)->ds_guid)) { 1110eda14cbcSMatt Macy err = ESRCH; 1111eda14cbcSMatt Macy goto out; 1112eda14cbcSMatt Macy } 1113eda14cbcSMatt Macy } 1114eda14cbcSMatt Macy if (new_rl->rl_phys->rlp_last_blkid == UINT64_MAX && 1115eda14cbcSMatt Macy new_rl->rl_phys->rlp_last_object == UINT64_MAX) { 1116eda14cbcSMatt Macy err = EEXIST; 1117eda14cbcSMatt Macy goto out; 1118eda14cbcSMatt Macy } 1119eda14cbcSMatt Macy dsl_pool_rele(dp, FTAG); 1120eda14cbcSMatt Macy dp = NULL; 1121eda14cbcSMatt Macy } else { 1122eda14cbcSMatt Macy uint64_t *guids = NULL; 1123eda14cbcSMatt Macy if (numsnaps > 0) { 11242ad756a6SMartin Matuska guids = vmem_zalloc(numsnaps * sizeof (uint64_t), 1125eda14cbcSMatt Macy KM_SLEEP); 1126eda14cbcSMatt Macy } 1127eda14cbcSMatt Macy for (int i = 0; i < numsnaps; i++) { 1128eda14cbcSMatt Macy struct redact_thread_arg *rta = &args[i]; 1129eda14cbcSMatt Macy guids[i] = dsl_dataset_phys(rta->ds)->ds_guid; 1130eda14cbcSMatt Macy } 1131eda14cbcSMatt Macy 1132eda14cbcSMatt Macy dsl_pool_rele(dp, FTAG); 1133eda14cbcSMatt Macy dp = NULL; 1134eda14cbcSMatt Macy err = dsl_bookmark_create_redacted(newredactbook, snapname, 1135eda14cbcSMatt Macy numsnaps, guids, FTAG, &new_rl); 11362ad756a6SMartin Matuska vmem_free(guids, numsnaps * sizeof (uint64_t)); 11372ad756a6SMartin Matuska if (err != 0) 1138eda14cbcSMatt Macy goto out; 1139eda14cbcSMatt Macy } 1140eda14cbcSMatt Macy 1141eda14cbcSMatt Macy for (int i = 0; i < numsnaps; i++) { 1142eda14cbcSMatt Macy struct redact_thread_arg *rta = &args[i]; 1143eda14cbcSMatt Macy (void) bqueue_init(&rta->q, zfs_redact_queue_ff, 1144eda14cbcSMatt Macy zfs_redact_queue_length, 1145eda14cbcSMatt Macy offsetof(struct redact_record, ln)); 1146eda14cbcSMatt Macy if (resuming) { 1147eda14cbcSMatt Macy rta->resume.zb_blkid = 1148eda14cbcSMatt Macy new_rl->rl_phys->rlp_last_blkid; 1149eda14cbcSMatt Macy rta->resume.zb_object = 1150eda14cbcSMatt Macy new_rl->rl_phys->rlp_last_object; 1151eda14cbcSMatt Macy } 1152eda14cbcSMatt Macy rta->txg = dsl_dataset_phys(ds)->ds_creation_txg; 1153eda14cbcSMatt Macy (void) thread_create(NULL, 0, redact_traverse_thread, rta, 1154eda14cbcSMatt Macy 0, curproc, TS_RUN, minclsyspri); 1155eda14cbcSMatt Macy } 11562c48331dSMatt Macy 11572c48331dSMatt Macy struct redact_merge_thread_arg *rmta; 11582c48331dSMatt Macy rmta = kmem_zalloc(sizeof (struct redact_merge_thread_arg), KM_SLEEP); 11592c48331dSMatt Macy 11602c48331dSMatt Macy (void) bqueue_init(&rmta->q, zfs_redact_queue_ff, 1161eda14cbcSMatt Macy zfs_redact_queue_length, offsetof(struct redact_record, ln)); 11622c48331dSMatt Macy rmta->numsnaps = numsnaps; 11632c48331dSMatt Macy rmta->spa = os->os_spa; 11642c48331dSMatt Macy rmta->thr_args = args; 11652c48331dSMatt Macy (void) thread_create(NULL, 0, redact_merge_thread, rmta, 0, curproc, 1166eda14cbcSMatt Macy TS_RUN, minclsyspri); 11672c48331dSMatt Macy err = perform_redaction(os, new_rl, rmta); 11683f9d360cSMartin Matuska bqueue_destroy(&rmta->q); 11692c48331dSMatt Macy kmem_free(rmta, sizeof (struct redact_merge_thread_arg)); 11702c48331dSMatt Macy 1171eda14cbcSMatt Macy out: 11722c48331dSMatt Macy kmem_free(newredactbook, sizeof (char) * ZFS_MAX_DATASET_NAME_LEN); 11732c48331dSMatt Macy 1174eda14cbcSMatt Macy if (new_rl != NULL) { 1175eda14cbcSMatt Macy dsl_redaction_list_long_rele(new_rl, FTAG); 1176eda14cbcSMatt Macy dsl_redaction_list_rele(new_rl, FTAG); 1177eda14cbcSMatt Macy } 1178eda14cbcSMatt Macy for (int i = 0; i < numsnaps; i++) { 1179eda14cbcSMatt Macy struct redact_thread_arg *rta = &args[i]; 1180eda14cbcSMatt Macy /* 1181eda14cbcSMatt Macy * rta->ds may be NULL if we got an error while filling 1182eda14cbcSMatt Macy * it in. 1183eda14cbcSMatt Macy */ 1184eda14cbcSMatt Macy if (rta->ds != NULL) { 1185eda14cbcSMatt Macy dsl_dataset_long_rele(rta->ds, FTAG); 1186eda14cbcSMatt Macy dsl_dataset_rele_flags(rta->ds, 1187eda14cbcSMatt Macy DS_HOLD_FLAG_DECRYPT, FTAG); 1188eda14cbcSMatt Macy } 1189eda14cbcSMatt Macy } 1190eda14cbcSMatt Macy 1191eda14cbcSMatt Macy if (args != NULL) 11922ad756a6SMartin Matuska vmem_free(args, numsnaps * sizeof (*args)); 1193eda14cbcSMatt Macy if (dp != NULL) 1194eda14cbcSMatt Macy dsl_pool_rele(dp, FTAG); 1195eda14cbcSMatt Macy if (ds != NULL) { 1196eda14cbcSMatt Macy dsl_dataset_long_rele(ds, FTAG); 1197eda14cbcSMatt Macy dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG); 1198eda14cbcSMatt Macy } 1199eda14cbcSMatt Macy return (SET_ERROR(err)); 1200eda14cbcSMatt Macy 1201eda14cbcSMatt Macy } 1202