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