]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
8d35c149 | 23 | * Copyright 2011 Nexenta Systems, Inc. All rights reserved. |
e6d3a843 | 24 | * Copyright (c) 2011, 2015 by Delphix. All rights reserved. |
788eb90c | 25 | * Copyright (c) 2014, Joyent, Inc. All rights reserved. |
47dfff3b | 26 | * Copyright 2014 HybridCluster. All rights reserved. |
b607405f | 27 | * Copyright 2016 RackTop Systems. |
a0bd735a | 28 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
8d35c149 | 29 | */ |
34dc7c2f | 30 | |
34dc7c2f BB |
31 | #include <sys/dmu.h> |
32 | #include <sys/dmu_impl.h> | |
33 | #include <sys/dmu_tx.h> | |
34 | #include <sys/dbuf.h> | |
35 | #include <sys/dnode.h> | |
36 | #include <sys/zfs_context.h> | |
37 | #include <sys/dmu_objset.h> | |
38 | #include <sys/dmu_traverse.h> | |
39 | #include <sys/dsl_dataset.h> | |
40 | #include <sys/dsl_dir.h> | |
428870ff | 41 | #include <sys/dsl_prop.h> |
34dc7c2f BB |
42 | #include <sys/dsl_pool.h> |
43 | #include <sys/dsl_synctask.h> | |
044baf00 | 44 | #include <sys/spa_impl.h> |
34dc7c2f BB |
45 | #include <sys/zfs_ioctl.h> |
46 | #include <sys/zap.h> | |
47 | #include <sys/zio_checksum.h> | |
428870ff BB |
48 | #include <sys/zfs_znode.h> |
49 | #include <zfs_fletcher.h> | |
50 | #include <sys/avl.h> | |
51 | #include <sys/ddt.h> | |
572e2857 | 52 | #include <sys/zfs_onexit.h> |
13fe0198 MA |
53 | #include <sys/dmu_send.h> |
54 | #include <sys/dsl_destroy.h> | |
9b67f605 | 55 | #include <sys/blkptr.h> |
da536844 | 56 | #include <sys/dsl_bookmark.h> |
9b67f605 | 57 | #include <sys/zfeature.h> |
fcff0f35 | 58 | #include <sys/bqueue.h> |
a0bd735a | 59 | #include <sys/zvol.h> |
f74b821a | 60 | #include <sys/policy.h> |
34dc7c2f | 61 | |
330d06f9 MA |
62 | /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */ |
63 | int zfs_send_corrupt_data = B_FALSE; | |
fcff0f35 PD |
64 | int zfs_send_queue_length = 16 * 1024 * 1024; |
65 | int zfs_recv_queue_length = 16 * 1024 * 1024; | |
b607405f AS |
66 | /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */ |
67 | int zfs_send_set_freerecords_bit = B_TRUE; | |
330d06f9 | 68 | |
34dc7c2f | 69 | static char *dmu_recv_tag = "dmu_recv_tag"; |
47dfff3b | 70 | const char *recv_clone_name = "%recv"; |
34dc7c2f | 71 | |
fcff0f35 PD |
72 | #define BP_SPAN(datablkszsec, indblkshift, level) \ |
73 | (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \ | |
74 | (level) * (indblkshift - SPA_BLKPTRSHIFT))) | |
75 | ||
47dfff3b MA |
76 | static void byteswap_record(dmu_replay_record_t *drr); |
77 | ||
fcff0f35 PD |
78 | struct send_thread_arg { |
79 | bqueue_t q; | |
80 | dsl_dataset_t *ds; /* Dataset to traverse */ | |
81 | uint64_t fromtxg; /* Traverse from this txg */ | |
82 | int flags; /* flags to pass to traverse_dataset */ | |
83 | int error_code; | |
84 | boolean_t cancel; | |
47dfff3b | 85 | zbookmark_phys_t resume; |
fcff0f35 PD |
86 | }; |
87 | ||
88 | struct send_block_record { | |
89 | boolean_t eos_marker; /* Marks the end of the stream */ | |
90 | blkptr_t bp; | |
91 | zbookmark_phys_t zb; | |
92 | uint8_t indblkshift; | |
93 | uint16_t datablkszsec; | |
94 | bqueue_node_t ln; | |
95 | }; | |
96 | ||
044baf00 BB |
97 | typedef struct dump_bytes_io { |
98 | dmu_sendarg_t *dbi_dsp; | |
99 | void *dbi_buf; | |
100 | int dbi_len; | |
101 | } dump_bytes_io_t; | |
102 | ||
103 | static void | |
b58986ee | 104 | dump_bytes_cb(void *arg) |
34dc7c2f | 105 | { |
044baf00 BB |
106 | dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg; |
107 | dmu_sendarg_t *dsp = dbi->dbi_dsp; | |
47dfff3b | 108 | dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os); |
34dc7c2f | 109 | ssize_t resid; /* have to get resid to get detailed errno */ |
f8866f8a ER |
110 | |
111 | /* | |
b5256303 | 112 | * The code does not rely on len being a multiple of 8. We keep |
f8866f8a ER |
113 | * this assertion because of the corresponding assertion in |
114 | * receive_read(). Keeping this assertion ensures that we do not | |
115 | * inadvertently break backwards compatibility (causing the assertion | |
b5256303 TC |
116 | * in receive_read() to trigger on old software). Newer feature flags |
117 | * (such as raw send) may break this assertion since they were | |
118 | * introduced after the requirement was made obsolete. | |
f8866f8a ER |
119 | */ |
120 | ||
b5256303 TC |
121 | ASSERT(dbi->dbi_len % 8 == 0 || |
122 | (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) != 0); | |
34dc7c2f | 123 | |
37abac6d | 124 | dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp, |
044baf00 | 125 | (caddr_t)dbi->dbi_buf, dbi->dbi_len, |
34dc7c2f | 126 | 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid); |
37abac6d BP |
127 | |
128 | mutex_enter(&ds->ds_sendstream_lock); | |
044baf00 | 129 | *dsp->dsa_off += dbi->dbi_len; |
37abac6d | 130 | mutex_exit(&ds->ds_sendstream_lock); |
044baf00 BB |
131 | } |
132 | ||
133 | static int | |
134 | dump_bytes(dmu_sendarg_t *dsp, void *buf, int len) | |
135 | { | |
136 | dump_bytes_io_t dbi; | |
137 | ||
138 | dbi.dbi_dsp = dsp; | |
139 | dbi.dbi_buf = buf; | |
140 | dbi.dbi_len = len; | |
141 | ||
b58986ee BB |
142 | #if defined(HAVE_LARGE_STACKS) |
143 | dump_bytes_cb(&dbi); | |
144 | #else | |
044baf00 BB |
145 | /* |
146 | * The vn_rdwr() call is performed in a taskq to ensure that there is | |
147 | * always enough stack space to write safely to the target filesystem. | |
148 | * The ZIO_TYPE_FREE threads are used because there can be a lot of | |
149 | * them and they are used in vdev_file.c for a similar purpose. | |
150 | */ | |
151 | spa_taskq_dispatch_sync(dmu_objset_spa(dsp->dsa_os), ZIO_TYPE_FREE, | |
b58986ee BB |
152 | ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP); |
153 | #endif /* HAVE_LARGE_STACKS */ | |
37abac6d BP |
154 | |
155 | return (dsp->dsa_err); | |
34dc7c2f BB |
156 | } |
157 | ||
37f8a883 MA |
158 | /* |
159 | * For all record types except BEGIN, fill in the checksum (overlaid in | |
160 | * drr_u.drr_checksum.drr_checksum). The checksum verifies everything | |
161 | * up to the start of the checksum itself. | |
162 | */ | |
163 | static int | |
164 | dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len) | |
165 | { | |
166 | ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), | |
167 | ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t)); | |
a6255b7f | 168 | (void) fletcher_4_incremental_native(dsp->dsa_drr, |
37f8a883 MA |
169 | offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), |
170 | &dsp->dsa_zc); | |
51907a31 K |
171 | if (dsp->dsa_drr->drr_type == DRR_BEGIN) { |
172 | dsp->dsa_sent_begin = B_TRUE; | |
173 | } else { | |
37f8a883 MA |
174 | ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u. |
175 | drr_checksum.drr_checksum)); | |
176 | dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc; | |
177 | } | |
51907a31 K |
178 | if (dsp->dsa_drr->drr_type == DRR_END) { |
179 | dsp->dsa_sent_end = B_TRUE; | |
180 | } | |
a6255b7f | 181 | (void) fletcher_4_incremental_native(&dsp->dsa_drr-> |
37f8a883 MA |
182 | drr_u.drr_checksum.drr_checksum, |
183 | sizeof (zio_cksum_t), &dsp->dsa_zc); | |
184 | if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0) | |
185 | return (SET_ERROR(EINTR)); | |
186 | if (payload_len != 0) { | |
a6255b7f | 187 | (void) fletcher_4_incremental_native(payload, payload_len, |
37f8a883 MA |
188 | &dsp->dsa_zc); |
189 | if (dump_bytes(dsp, payload, payload_len) != 0) | |
190 | return (SET_ERROR(EINTR)); | |
191 | } | |
192 | return (0); | |
193 | } | |
194 | ||
e6d3a843 PD |
195 | /* |
196 | * Fill in the drr_free struct, or perform aggregation if the previous record is | |
197 | * also a free record, and the two are adjacent. | |
198 | * | |
199 | * Note that we send free records even for a full send, because we want to be | |
200 | * able to receive a full send as a clone, which requires a list of all the free | |
201 | * and freeobject records that were generated on the source. | |
202 | */ | |
34dc7c2f | 203 | static int |
37abac6d | 204 | dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset, |
34dc7c2f BB |
205 | uint64_t length) |
206 | { | |
37abac6d | 207 | struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free); |
428870ff | 208 | |
ea97f8ce MA |
209 | /* |
210 | * When we receive a free record, dbuf_free_range() assumes | |
211 | * that the receiving system doesn't have any dbufs in the range | |
212 | * being freed. This is always true because there is a one-record | |
213 | * constraint: we only send one WRITE record for any given | |
47dfff3b | 214 | * object,offset. We know that the one-record constraint is |
ea97f8ce MA |
215 | * true because we always send data in increasing order by |
216 | * object,offset. | |
217 | * | |
218 | * If the increasing-order constraint ever changes, we should find | |
219 | * another way to assert that the one-record constraint is still | |
220 | * satisfied. | |
221 | */ | |
222 | ASSERT(object > dsp->dsa_last_data_object || | |
223 | (object == dsp->dsa_last_data_object && | |
224 | offset > dsp->dsa_last_data_offset)); | |
225 | ||
c578f007 SK |
226 | if (length != -1ULL && offset + length < offset) |
227 | length = -1ULL; | |
228 | ||
428870ff BB |
229 | /* |
230 | * If there is a pending op, but it's not PENDING_FREE, push it out, | |
231 | * since free block aggregation can only be done for blocks of the | |
232 | * same type (i.e., DRR_FREE records can only be aggregated with | |
233 | * other DRR_FREE records. DRR_FREEOBJECTS records can only be | |
234 | * aggregated with other DRR_FREEOBJECTS records. | |
235 | */ | |
37abac6d BP |
236 | if (dsp->dsa_pending_op != PENDING_NONE && |
237 | dsp->dsa_pending_op != PENDING_FREE) { | |
37f8a883 | 238 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 239 | return (SET_ERROR(EINTR)); |
37abac6d | 240 | dsp->dsa_pending_op = PENDING_NONE; |
428870ff BB |
241 | } |
242 | ||
37abac6d | 243 | if (dsp->dsa_pending_op == PENDING_FREE) { |
428870ff BB |
244 | /* |
245 | * There should never be a PENDING_FREE if length is -1 | |
246 | * (because dump_dnode is the only place where this | |
247 | * function is called with a -1, and only after flushing | |
248 | * any pending record). | |
249 | */ | |
250 | ASSERT(length != -1ULL); | |
251 | /* | |
252 | * Check to see whether this free block can be aggregated | |
253 | * with pending one. | |
254 | */ | |
255 | if (drrf->drr_object == object && drrf->drr_offset + | |
256 | drrf->drr_length == offset) { | |
257 | drrf->drr_length += length; | |
258 | return (0); | |
259 | } else { | |
260 | /* not a continuation. Push out pending record */ | |
37f8a883 | 261 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 262 | return (SET_ERROR(EINTR)); |
37abac6d | 263 | dsp->dsa_pending_op = PENDING_NONE; |
428870ff BB |
264 | } |
265 | } | |
266 | /* create a FREE record and make it pending */ | |
37abac6d BP |
267 | bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); |
268 | dsp->dsa_drr->drr_type = DRR_FREE; | |
428870ff BB |
269 | drrf->drr_object = object; |
270 | drrf->drr_offset = offset; | |
271 | drrf->drr_length = length; | |
37abac6d | 272 | drrf->drr_toguid = dsp->dsa_toguid; |
428870ff | 273 | if (length == -1ULL) { |
37f8a883 | 274 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 275 | return (SET_ERROR(EINTR)); |
428870ff | 276 | } else { |
37abac6d | 277 | dsp->dsa_pending_op = PENDING_FREE; |
428870ff | 278 | } |
34dc7c2f | 279 | |
34dc7c2f BB |
280 | return (0); |
281 | } | |
282 | ||
283 | static int | |
b5256303 TC |
284 | dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type, uint64_t object, |
285 | uint64_t offset, int lsize, int psize, const blkptr_t *bp, void *data) | |
34dc7c2f | 286 | { |
2aa34383 | 287 | uint64_t payload_size; |
b5256303 | 288 | boolean_t raw = (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW); |
37abac6d | 289 | struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write); |
428870ff | 290 | |
ea97f8ce MA |
291 | /* |
292 | * We send data in increasing object, offset order. | |
293 | * See comment in dump_free() for details. | |
294 | */ | |
295 | ASSERT(object > dsp->dsa_last_data_object || | |
296 | (object == dsp->dsa_last_data_object && | |
297 | offset > dsp->dsa_last_data_offset)); | |
298 | dsp->dsa_last_data_object = object; | |
2aa34383 | 299 | dsp->dsa_last_data_offset = offset + lsize - 1; |
428870ff BB |
300 | |
301 | /* | |
302 | * If there is any kind of pending aggregation (currently either | |
303 | * a grouping of free objects or free blocks), push it out to | |
304 | * the stream, since aggregation can't be done across operations | |
305 | * of different types. | |
306 | */ | |
37abac6d | 307 | if (dsp->dsa_pending_op != PENDING_NONE) { |
37f8a883 | 308 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 309 | return (SET_ERROR(EINTR)); |
37abac6d | 310 | dsp->dsa_pending_op = PENDING_NONE; |
428870ff | 311 | } |
37f8a883 | 312 | /* write a WRITE record */ |
37abac6d BP |
313 | bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); |
314 | dsp->dsa_drr->drr_type = DRR_WRITE; | |
428870ff BB |
315 | drrw->drr_object = object; |
316 | drrw->drr_type = type; | |
317 | drrw->drr_offset = offset; | |
37abac6d | 318 | drrw->drr_toguid = dsp->dsa_toguid; |
2aa34383 DK |
319 | drrw->drr_logical_size = lsize; |
320 | ||
b5256303 TC |
321 | /* only set the compression fields if the buf is compressed or raw */ |
322 | if (raw || lsize != psize) { | |
2aa34383 | 323 | ASSERT(!BP_IS_EMBEDDED(bp)); |
2aa34383 | 324 | ASSERT3S(psize, >, 0); |
2aa34383 | 325 | |
b5256303 TC |
326 | if (raw) { |
327 | ASSERT(BP_IS_PROTECTED(bp)); | |
328 | ||
329 | /* | |
9b840763 TC |
330 | * This is a raw protected block so we need to pass |
331 | * along everything the receiving side will need to | |
332 | * interpret this block, including the byteswap, salt, | |
333 | * IV, and MAC. | |
b5256303 | 334 | */ |
b5256303 TC |
335 | if (BP_SHOULD_BYTESWAP(bp)) |
336 | drrw->drr_flags |= DRR_RAW_BYTESWAP; | |
337 | zio_crypt_decode_params_bp(bp, drrw->drr_salt, | |
338 | drrw->drr_iv); | |
339 | zio_crypt_decode_mac_bp(bp, drrw->drr_mac); | |
340 | } else { | |
341 | /* this is a compressed block */ | |
342 | ASSERT(dsp->dsa_featureflags & | |
343 | DMU_BACKUP_FEATURE_COMPRESSED); | |
344 | ASSERT(!BP_SHOULD_BYTESWAP(bp)); | |
345 | ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp))); | |
346 | ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF); | |
347 | ASSERT3S(lsize, >=, psize); | |
348 | } | |
349 | ||
350 | /* set fields common to compressed and raw sends */ | |
2aa34383 DK |
351 | drrw->drr_compressiontype = BP_GET_COMPRESS(bp); |
352 | drrw->drr_compressed_size = psize; | |
353 | payload_size = drrw->drr_compressed_size; | |
354 | } else { | |
355 | payload_size = drrw->drr_logical_size; | |
356 | } | |
357 | ||
b5256303 | 358 | if (bp == NULL || BP_IS_EMBEDDED(bp) || (BP_IS_PROTECTED(bp) && !raw)) { |
9b67f605 | 359 | /* |
b5256303 TC |
360 | * There's no pre-computed checksum for partial-block writes, |
361 | * embedded BP's, or encrypted BP's that are being sent as | |
362 | * plaintext, so (like fletcher4-checkummed blocks) userland | |
363 | * will have to compute a dedup-capable checksum itself. | |
9b67f605 MA |
364 | */ |
365 | drrw->drr_checksumtype = ZIO_CHECKSUM_OFF; | |
366 | } else { | |
367 | drrw->drr_checksumtype = BP_GET_CHECKSUM(bp); | |
3c67d83a TH |
368 | if (zio_checksum_table[drrw->drr_checksumtype].ci_flags & |
369 | ZCHECKSUM_FLAG_DEDUP) | |
b5256303 | 370 | drrw->drr_flags |= DRR_CHECKSUM_DEDUP; |
9b67f605 MA |
371 | DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp)); |
372 | DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp)); | |
373 | DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp)); | |
b5256303 | 374 | DDK_SET_CRYPT(&drrw->drr_key, BP_IS_PROTECTED(bp)); |
9b67f605 MA |
375 | drrw->drr_key.ddk_cksum = bp->blk_cksum; |
376 | } | |
428870ff | 377 | |
2aa34383 | 378 | if (dump_record(dsp, data, payload_size) != 0) |
2e528b49 | 379 | return (SET_ERROR(EINTR)); |
428870ff BB |
380 | return (0); |
381 | } | |
382 | ||
9b67f605 MA |
383 | static int |
384 | dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset, | |
385 | int blksz, const blkptr_t *bp) | |
386 | { | |
387 | char buf[BPE_PAYLOAD_SIZE]; | |
388 | struct drr_write_embedded *drrw = | |
389 | &(dsp->dsa_drr->drr_u.drr_write_embedded); | |
390 | ||
391 | if (dsp->dsa_pending_op != PENDING_NONE) { | |
37f8a883 | 392 | if (dump_record(dsp, NULL, 0) != 0) |
ecb2b7dc | 393 | return (SET_ERROR(EINTR)); |
9b67f605 MA |
394 | dsp->dsa_pending_op = PENDING_NONE; |
395 | } | |
396 | ||
397 | ASSERT(BP_IS_EMBEDDED(bp)); | |
398 | ||
399 | bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); | |
400 | dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED; | |
401 | drrw->drr_object = object; | |
402 | drrw->drr_offset = offset; | |
403 | drrw->drr_length = blksz; | |
404 | drrw->drr_toguid = dsp->dsa_toguid; | |
405 | drrw->drr_compression = BP_GET_COMPRESS(bp); | |
406 | drrw->drr_etype = BPE_GET_ETYPE(bp); | |
407 | drrw->drr_lsize = BPE_GET_LSIZE(bp); | |
408 | drrw->drr_psize = BPE_GET_PSIZE(bp); | |
409 | ||
410 | decode_embedded_bp_compressed(bp, buf); | |
411 | ||
37f8a883 | 412 | if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0) |
ecb2b7dc | 413 | return (SET_ERROR(EINTR)); |
9b67f605 MA |
414 | return (0); |
415 | } | |
416 | ||
428870ff | 417 | static int |
b5256303 | 418 | dump_spill(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t object, void *data) |
428870ff | 419 | { |
37abac6d | 420 | struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill); |
b5256303 | 421 | uint64_t blksz = BP_GET_LSIZE(bp); |
428870ff | 422 | |
37abac6d | 423 | if (dsp->dsa_pending_op != PENDING_NONE) { |
37f8a883 | 424 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 425 | return (SET_ERROR(EINTR)); |
37abac6d | 426 | dsp->dsa_pending_op = PENDING_NONE; |
428870ff BB |
427 | } |
428 | ||
429 | /* write a SPILL record */ | |
37abac6d BP |
430 | bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); |
431 | dsp->dsa_drr->drr_type = DRR_SPILL; | |
428870ff BB |
432 | drrs->drr_object = object; |
433 | drrs->drr_length = blksz; | |
37abac6d | 434 | drrs->drr_toguid = dsp->dsa_toguid; |
34dc7c2f | 435 | |
b5256303 | 436 | /* handle raw send fields */ |
9b840763 TC |
437 | if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) { |
438 | ASSERT(BP_IS_PROTECTED(bp)); | |
439 | ||
b5256303 TC |
440 | if (BP_SHOULD_BYTESWAP(bp)) |
441 | drrs->drr_flags |= DRR_RAW_BYTESWAP; | |
442 | drrs->drr_compressiontype = BP_GET_COMPRESS(bp); | |
443 | drrs->drr_compressed_size = BP_GET_PSIZE(bp); | |
444 | zio_crypt_decode_params_bp(bp, drrs->drr_salt, drrs->drr_iv); | |
445 | zio_crypt_decode_mac_bp(bp, drrs->drr_mac); | |
446 | } | |
447 | ||
37f8a883 | 448 | if (dump_record(dsp, data, blksz) != 0) |
2e528b49 | 449 | return (SET_ERROR(EINTR)); |
34dc7c2f BB |
450 | return (0); |
451 | } | |
452 | ||
453 | static int | |
37abac6d | 454 | dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs) |
34dc7c2f | 455 | { |
37abac6d | 456 | struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects); |
829e95c4 FG |
457 | uint64_t maxobj = DNODES_PER_BLOCK * |
458 | (DMU_META_DNODE(dsp->dsa_os)->dn_maxblkid + 1); | |
459 | ||
460 | /* | |
461 | * ZoL < 0.7 does not handle large FREEOBJECTS records correctly, | |
462 | * leading to zfs recv never completing. to avoid this issue, don't | |
463 | * send FREEOBJECTS records for object IDs which cannot exist on the | |
464 | * receiving side. | |
465 | */ | |
466 | if (maxobj > 0) { | |
467 | if (maxobj < firstobj) | |
468 | return (0); | |
469 | ||
470 | if (maxobj < firstobj + numobjs) | |
471 | numobjs = maxobj - firstobj; | |
472 | } | |
428870ff BB |
473 | |
474 | /* | |
475 | * If there is a pending op, but it's not PENDING_FREEOBJECTS, | |
476 | * push it out, since free block aggregation can only be done for | |
477 | * blocks of the same type (i.e., DRR_FREE records can only be | |
478 | * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records | |
479 | * can only be aggregated with other DRR_FREEOBJECTS records. | |
480 | */ | |
37abac6d BP |
481 | if (dsp->dsa_pending_op != PENDING_NONE && |
482 | dsp->dsa_pending_op != PENDING_FREEOBJECTS) { | |
37f8a883 | 483 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 484 | return (SET_ERROR(EINTR)); |
37abac6d | 485 | dsp->dsa_pending_op = PENDING_NONE; |
428870ff | 486 | } |
37abac6d | 487 | if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) { |
428870ff BB |
488 | /* |
489 | * See whether this free object array can be aggregated | |
490 | * with pending one | |
491 | */ | |
492 | if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) { | |
493 | drrfo->drr_numobjs += numobjs; | |
494 | return (0); | |
495 | } else { | |
496 | /* can't be aggregated. Push out pending record */ | |
37f8a883 | 497 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 498 | return (SET_ERROR(EINTR)); |
37abac6d | 499 | dsp->dsa_pending_op = PENDING_NONE; |
428870ff BB |
500 | } |
501 | } | |
502 | ||
34dc7c2f | 503 | /* write a FREEOBJECTS record */ |
37abac6d BP |
504 | bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); |
505 | dsp->dsa_drr->drr_type = DRR_FREEOBJECTS; | |
428870ff BB |
506 | drrfo->drr_firstobj = firstobj; |
507 | drrfo->drr_numobjs = numobjs; | |
37abac6d | 508 | drrfo->drr_toguid = dsp->dsa_toguid; |
428870ff | 509 | |
37abac6d | 510 | dsp->dsa_pending_op = PENDING_FREEOBJECTS; |
34dc7c2f | 511 | |
34dc7c2f BB |
512 | return (0); |
513 | } | |
514 | ||
515 | static int | |
b5256303 TC |
516 | dump_dnode(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t object, |
517 | dnode_phys_t *dnp) | |
34dc7c2f | 518 | { |
37abac6d | 519 | struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object); |
4807c0ba | 520 | int bonuslen; |
428870ff | 521 | |
47dfff3b MA |
522 | if (object < dsp->dsa_resume_object) { |
523 | /* | |
524 | * Note: when resuming, we will visit all the dnodes in | |
525 | * the block of dnodes that we are resuming from. In | |
526 | * this case it's unnecessary to send the dnodes prior to | |
527 | * the one we are resuming from. We should be at most one | |
528 | * block's worth of dnodes behind the resume point. | |
529 | */ | |
530 | ASSERT3U(dsp->dsa_resume_object - object, <, | |
531 | 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT)); | |
532 | return (0); | |
533 | } | |
534 | ||
34dc7c2f | 535 | if (dnp == NULL || dnp->dn_type == DMU_OT_NONE) |
37abac6d | 536 | return (dump_freeobjects(dsp, object, 1)); |
34dc7c2f | 537 | |
37abac6d | 538 | if (dsp->dsa_pending_op != PENDING_NONE) { |
37f8a883 | 539 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 540 | return (SET_ERROR(EINTR)); |
37abac6d | 541 | dsp->dsa_pending_op = PENDING_NONE; |
428870ff BB |
542 | } |
543 | ||
34dc7c2f | 544 | /* write an OBJECT record */ |
37abac6d BP |
545 | bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); |
546 | dsp->dsa_drr->drr_type = DRR_OBJECT; | |
428870ff BB |
547 | drro->drr_object = object; |
548 | drro->drr_type = dnp->dn_type; | |
549 | drro->drr_bonustype = dnp->dn_bonustype; | |
550 | drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT; | |
551 | drro->drr_bonuslen = dnp->dn_bonuslen; | |
50c957f7 | 552 | drro->drr_dn_slots = dnp->dn_extra_slots + 1; |
428870ff BB |
553 | drro->drr_checksumtype = dnp->dn_checksum; |
554 | drro->drr_compress = dnp->dn_compress; | |
37abac6d | 555 | drro->drr_toguid = dsp->dsa_toguid; |
428870ff | 556 | |
f1512ee6 MA |
557 | if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) && |
558 | drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE) | |
559 | drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE; | |
560 | ||
4807c0ba TC |
561 | bonuslen = P2ROUNDUP(dnp->dn_bonuslen, 8); |
562 | ||
9b840763 TC |
563 | if ((dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW)) { |
564 | ASSERT(BP_IS_ENCRYPTED(bp)); | |
565 | ||
b5256303 TC |
566 | if (BP_SHOULD_BYTESWAP(bp)) |
567 | drro->drr_flags |= DRR_RAW_BYTESWAP; | |
568 | ||
569 | /* needed for reconstructing dnp on recv side */ | |
570 | drro->drr_indblkshift = dnp->dn_indblkshift; | |
571 | drro->drr_nlevels = dnp->dn_nlevels; | |
572 | drro->drr_nblkptr = dnp->dn_nblkptr; | |
573 | ||
574 | /* | |
575 | * Since we encrypt the entire bonus area, the (raw) part | |
4807c0ba | 576 | * beyond the bonuslen is actually nonzero, so we need |
b5256303 TC |
577 | * to send it. |
578 | */ | |
579 | if (bonuslen != 0) { | |
580 | drro->drr_raw_bonuslen = DN_MAX_BONUS_LEN(dnp); | |
581 | bonuslen = drro->drr_raw_bonuslen; | |
582 | } | |
37f8a883 | 583 | } |
34dc7c2f | 584 | |
b5256303 TC |
585 | if (dump_record(dsp, DN_BONUS(dnp), bonuslen) != 0) |
586 | return (SET_ERROR(EINTR)); | |
587 | ||
ea97f8ce | 588 | /* Free anything past the end of the file. */ |
37abac6d | 589 | if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) * |
ea97f8ce | 590 | (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0) |
2e528b49 | 591 | return (SET_ERROR(EINTR)); |
13fe0198 | 592 | if (dsp->dsa_err != 0) |
2e528b49 | 593 | return (SET_ERROR(EINTR)); |
34dc7c2f BB |
594 | return (0); |
595 | } | |
596 | ||
b5256303 TC |
597 | static int |
598 | dump_object_range(dmu_sendarg_t *dsp, const blkptr_t *bp, uint64_t firstobj, | |
599 | uint64_t numslots) | |
600 | { | |
601 | struct drr_object_range *drror = | |
602 | &(dsp->dsa_drr->drr_u.drr_object_range); | |
603 | ||
604 | /* we only use this record type for raw sends */ | |
605 | ASSERT(BP_IS_PROTECTED(bp)); | |
606 | ASSERT(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_RAW); | |
607 | ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); | |
608 | ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_DNODE); | |
609 | ASSERT0(BP_GET_LEVEL(bp)); | |
610 | ||
611 | if (dsp->dsa_pending_op != PENDING_NONE) { | |
612 | if (dump_record(dsp, NULL, 0) != 0) | |
613 | return (SET_ERROR(EINTR)); | |
614 | dsp->dsa_pending_op = PENDING_NONE; | |
615 | } | |
616 | ||
617 | bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t)); | |
618 | dsp->dsa_drr->drr_type = DRR_OBJECT_RANGE; | |
619 | drror->drr_firstobj = firstobj; | |
620 | drror->drr_numslots = numslots; | |
621 | drror->drr_toguid = dsp->dsa_toguid; | |
b5256303 TC |
622 | if (BP_SHOULD_BYTESWAP(bp)) |
623 | drror->drr_flags |= DRR_RAW_BYTESWAP; | |
624 | zio_crypt_decode_params_bp(bp, drror->drr_salt, drror->drr_iv); | |
625 | zio_crypt_decode_mac_bp(bp, drror->drr_mac); | |
626 | ||
627 | if (dump_record(dsp, NULL, 0) != 0) | |
628 | return (SET_ERROR(EINTR)); | |
629 | return (0); | |
630 | } | |
631 | ||
9b67f605 MA |
632 | static boolean_t |
633 | backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp) | |
634 | { | |
635 | if (!BP_IS_EMBEDDED(bp)) | |
636 | return (B_FALSE); | |
637 | ||
638 | /* | |
639 | * Compression function must be legacy, or explicitly enabled. | |
640 | */ | |
641 | if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS && | |
2aa34383 | 642 | !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LZ4))) |
9b67f605 MA |
643 | return (B_FALSE); |
644 | ||
645 | /* | |
646 | * Embed type must be explicitly enabled. | |
647 | */ | |
648 | switch (BPE_GET_ETYPE(bp)) { | |
649 | case BP_EMBEDDED_TYPE_DATA: | |
650 | if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) | |
651 | return (B_TRUE); | |
652 | break; | |
653 | default: | |
654 | return (B_FALSE); | |
655 | } | |
656 | return (B_FALSE); | |
657 | } | |
658 | ||
fcff0f35 PD |
659 | /* |
660 | * This is the callback function to traverse_dataset that acts as the worker | |
661 | * thread for dmu_send_impl. | |
662 | */ | |
663 | /*ARGSUSED*/ | |
664 | static int | |
665 | send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
666 | const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg) | |
667 | { | |
668 | struct send_thread_arg *sta = arg; | |
669 | struct send_block_record *record; | |
670 | uint64_t record_size; | |
671 | int err = 0; | |
672 | ||
47dfff3b MA |
673 | ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT || |
674 | zb->zb_object >= sta->resume.zb_object); | |
b5256303 | 675 | ASSERT3P(sta->ds, !=, NULL); |
47dfff3b | 676 | |
fcff0f35 PD |
677 | if (sta->cancel) |
678 | return (SET_ERROR(EINTR)); | |
34dc7c2f | 679 | |
fcff0f35 PD |
680 | if (bp == NULL) { |
681 | ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL); | |
682 | return (0); | |
683 | } else if (zb->zb_level < 0) { | |
684 | return (0); | |
685 | } | |
686 | ||
687 | record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP); | |
688 | record->eos_marker = B_FALSE; | |
689 | record->bp = *bp; | |
690 | record->zb = *zb; | |
691 | record->indblkshift = dnp->dn_indblkshift; | |
692 | record->datablkszsec = dnp->dn_datablkszsec; | |
693 | record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT; | |
694 | bqueue_enqueue(&sta->q, record, record_size); | |
695 | ||
696 | return (err); | |
697 | } | |
698 | ||
699 | /* | |
700 | * This function kicks off the traverse_dataset. It also handles setting the | |
701 | * error code of the thread in case something goes wrong, and pushes the End of | |
702 | * Stream record when the traverse_dataset call has finished. If there is no | |
703 | * dataset to traverse, the thread immediately pushes End of Stream marker. | |
704 | */ | |
705 | static void | |
706 | send_traverse_thread(void *arg) | |
707 | { | |
708 | struct send_thread_arg *st_arg = arg; | |
709 | int err; | |
710 | struct send_block_record *data; | |
3e635ac1 | 711 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
fcff0f35 PD |
712 | |
713 | if (st_arg->ds != NULL) { | |
47dfff3b MA |
714 | err = traverse_dataset_resume(st_arg->ds, |
715 | st_arg->fromtxg, &st_arg->resume, | |
716 | st_arg->flags, send_cb, st_arg); | |
717 | ||
fcff0f35 PD |
718 | if (err != EINTR) |
719 | st_arg->error_code = err; | |
720 | } | |
721 | data = kmem_zalloc(sizeof (*data), KM_SLEEP); | |
722 | data->eos_marker = B_TRUE; | |
723 | bqueue_enqueue(&st_arg->q, data, 1); | |
3e635ac1 | 724 | spl_fstrans_unmark(cookie); |
34a6b428 | 725 | thread_exit(); |
fcff0f35 PD |
726 | } |
727 | ||
728 | /* | |
729 | * This function actually handles figuring out what kind of record needs to be | |
730 | * dumped, reading the data (which has hopefully been prefetched), and calling | |
731 | * the appropriate helper function. | |
732 | */ | |
34dc7c2f | 733 | static int |
fcff0f35 | 734 | do_dump(dmu_sendarg_t *dsa, struct send_block_record *data) |
34dc7c2f | 735 | { |
fcff0f35 PD |
736 | dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os); |
737 | const blkptr_t *bp = &data->bp; | |
738 | const zbookmark_phys_t *zb = &data->zb; | |
739 | uint8_t indblkshift = data->indblkshift; | |
740 | uint16_t dblkszsec = data->datablkszsec; | |
741 | spa_t *spa = ds->ds_dir->dd_pool->dp_spa; | |
34dc7c2f | 742 | dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE; |
34dc7c2f | 743 | int err = 0; |
fcff0f35 | 744 | uint64_t dnobj; |
34dc7c2f | 745 | |
fcff0f35 | 746 | ASSERT3U(zb->zb_level, >=, 0); |
34dc7c2f | 747 | |
47dfff3b MA |
748 | ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT || |
749 | zb->zb_object >= dsa->dsa_resume_object); | |
750 | ||
b5256303 TC |
751 | /* |
752 | * All bps of an encrypted os should have the encryption bit set. | |
753 | * If this is not true it indicates tampering and we report an error. | |
754 | */ | |
755 | if (dsa->dsa_os->os_encrypted && | |
756 | !BP_IS_HOLE(bp) && !BP_USES_CRYPT(bp)) { | |
757 | spa_log_error(spa, zb); | |
758 | zfs_panic_recover("unencrypted block in encrypted " | |
759 | "object set %llu", ds->ds_object); | |
760 | return (SET_ERROR(EIO)); | |
761 | } | |
762 | ||
428870ff BB |
763 | if (zb->zb_object != DMU_META_DNODE_OBJECT && |
764 | DMU_OBJECT_IS_SPECIAL(zb->zb_object)) { | |
9babb374 | 765 | return (0); |
b0bc7a84 MG |
766 | } else if (BP_IS_HOLE(bp) && |
767 | zb->zb_object == DMU_META_DNODE_OBJECT) { | |
fcff0f35 | 768 | uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level); |
b128c09f | 769 | uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT; |
fcff0f35 | 770 | err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT); |
b0bc7a84 | 771 | } else if (BP_IS_HOLE(bp)) { |
fcff0f35 PD |
772 | uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level); |
773 | uint64_t offset = zb->zb_blkid * span; | |
774 | err = dump_free(dsa, zb->zb_object, offset, span); | |
b128c09f BB |
775 | } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) { |
776 | return (0); | |
777 | } else if (type == DMU_OT_DNODE) { | |
50c957f7 NB |
778 | dnode_phys_t *blk; |
779 | int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT; | |
2a432414 | 780 | arc_flags_t aflags = ARC_FLAG_WAIT; |
b128c09f | 781 | arc_buf_t *abuf; |
b5256303 | 782 | enum zio_flag zioflags = ZIO_FLAG_CANFAIL; |
fcff0f35 PD |
783 | int i; |
784 | ||
b5256303 TC |
785 | if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) { |
786 | ASSERT(BP_IS_ENCRYPTED(bp)); | |
787 | ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF); | |
788 | zioflags |= ZIO_FLAG_RAW; | |
789 | } | |
790 | ||
fcff0f35 | 791 | ASSERT0(zb->zb_level); |
b128c09f | 792 | |
294f6806 | 793 | if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf, |
b5256303 | 794 | ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) |
2e528b49 | 795 | return (SET_ERROR(EIO)); |
34dc7c2f | 796 | |
b128c09f | 797 | blk = abuf->b_data; |
50c957f7 | 798 | dnobj = zb->zb_blkid * epb; |
b5256303 TC |
799 | |
800 | /* | |
801 | * Raw sends require sending encryption parameters for the | |
802 | * block of dnodes. Regular sends do not need to send this | |
803 | * info. | |
804 | */ | |
805 | if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) { | |
806 | ASSERT(arc_is_encrypted(abuf)); | |
807 | err = dump_object_range(dsa, bp, dnobj, epb); | |
808 | } | |
809 | ||
810 | if (err == 0) { | |
811 | for (i = 0; i < epb; i += blk[i].dn_extra_slots + 1) { | |
812 | err = dump_dnode(dsa, bp, dnobj + i, blk + i); | |
813 | if (err != 0) | |
814 | break; | |
815 | } | |
34dc7c2f | 816 | } |
d3c2ae1c | 817 | arc_buf_destroy(abuf, &abuf); |
428870ff | 818 | } else if (type == DMU_OT_SA) { |
2a432414 | 819 | arc_flags_t aflags = ARC_FLAG_WAIT; |
b128c09f | 820 | arc_buf_t *abuf; |
b5256303 TC |
821 | enum zio_flag zioflags = ZIO_FLAG_CANFAIL; |
822 | ||
823 | if (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) { | |
824 | ASSERT(BP_IS_PROTECTED(bp)); | |
825 | zioflags |= ZIO_FLAG_RAW; | |
826 | } | |
b128c09f | 827 | |
294f6806 | 828 | if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf, |
b5256303 | 829 | ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) |
2e528b49 | 830 | return (SET_ERROR(EIO)); |
b128c09f | 831 | |
b5256303 | 832 | err = dump_spill(dsa, bp, zb->zb_object, abuf->b_data); |
d3c2ae1c | 833 | arc_buf_destroy(abuf, &abuf); |
fcff0f35 | 834 | } else if (backup_do_embed(dsa, bp)) { |
9b67f605 | 835 | /* it's an embedded level-0 block of a regular object */ |
fcff0f35 PD |
836 | int blksz = dblkszsec << SPA_MINBLOCKSHIFT; |
837 | ASSERT0(zb->zb_level); | |
838 | err = dump_write_embedded(dsa, zb->zb_object, | |
9b67f605 | 839 | zb->zb_blkid * blksz, blksz, bp); |
fcff0f35 PD |
840 | } else { |
841 | /* it's a level-0 block of a regular object */ | |
2a432414 | 842 | arc_flags_t aflags = ARC_FLAG_WAIT; |
428870ff | 843 | arc_buf_t *abuf; |
fcff0f35 PD |
844 | int blksz = dblkszsec << SPA_MINBLOCKSHIFT; |
845 | uint64_t offset; | |
2aa34383 DK |
846 | |
847 | /* | |
848 | * If we have large blocks stored on disk but the send flags | |
849 | * don't allow us to send large blocks, we split the data from | |
850 | * the arc buf into chunks. | |
851 | */ | |
a7004725 | 852 | boolean_t split_large_blocks = blksz > SPA_OLD_MAXBLOCKSIZE && |
2aa34383 | 853 | !(dsa->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS); |
b5256303 TC |
854 | |
855 | /* | |
856 | * Raw sends require that we always get raw data as it exists | |
857 | * on disk, so we assert that we are not splitting blocks here. | |
858 | */ | |
859 | boolean_t request_raw = | |
860 | (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_RAW) != 0; | |
861 | ||
2aa34383 DK |
862 | /* |
863 | * We should only request compressed data from the ARC if all | |
864 | * the following are true: | |
865 | * - stream compression was requested | |
866 | * - we aren't splitting large blocks into smaller chunks | |
867 | * - the data won't need to be byteswapped before sending | |
868 | * - this isn't an embedded block | |
869 | * - this isn't metadata (if receiving on a different endian | |
870 | * system it can be byteswapped more easily) | |
871 | */ | |
872 | boolean_t request_compressed = | |
873 | (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED) && | |
874 | !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) && | |
875 | !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp)); | |
428870ff | 876 | |
b5256303 TC |
877 | IMPLY(request_raw, !split_large_blocks); |
878 | IMPLY(request_raw, BP_IS_PROTECTED(bp)); | |
da536844 | 879 | ASSERT0(zb->zb_level); |
47dfff3b MA |
880 | ASSERT(zb->zb_object > dsa->dsa_resume_object || |
881 | (zb->zb_object == dsa->dsa_resume_object && | |
882 | zb->zb_blkid * blksz >= dsa->dsa_resume_offset)); | |
883 | ||
a7004725 DK |
884 | ASSERT3U(blksz, ==, BP_GET_LSIZE(bp)); |
885 | ||
886 | enum zio_flag zioflags = ZIO_FLAG_CANFAIL; | |
b5256303 | 887 | if (request_raw) |
2aa34383 | 888 | zioflags |= ZIO_FLAG_RAW; |
b5256303 TC |
889 | else if (request_compressed) |
890 | zioflags |= ZIO_FLAG_RAW_COMPRESS; | |
2aa34383 | 891 | |
294f6806 | 892 | if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf, |
a7004725 | 893 | ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) { |
330d06f9 | 894 | if (zfs_send_corrupt_data) { |
330d06f9 | 895 | /* Send a block filled with 0x"zfs badd bloc" */ |
2aa34383 DK |
896 | abuf = arc_alloc_buf(spa, &abuf, ARC_BUFC_DATA, |
897 | blksz); | |
a7004725 | 898 | uint64_t *ptr; |
330d06f9 MA |
899 | for (ptr = abuf->b_data; |
900 | (char *)ptr < (char *)abuf->b_data + blksz; | |
901 | ptr++) | |
dd26aa53 | 902 | *ptr = 0x2f5baddb10cULL; |
330d06f9 | 903 | } else { |
2e528b49 | 904 | return (SET_ERROR(EIO)); |
330d06f9 MA |
905 | } |
906 | } | |
428870ff | 907 | |
f1512ee6 MA |
908 | offset = zb->zb_blkid * blksz; |
909 | ||
2aa34383 | 910 | if (split_large_blocks) { |
b5256303 | 911 | ASSERT0(arc_is_encrypted(abuf)); |
2aa34383 DK |
912 | ASSERT3U(arc_get_compression(abuf), ==, |
913 | ZIO_COMPRESS_OFF); | |
a7004725 | 914 | char *buf = abuf->b_data; |
f1512ee6 MA |
915 | while (blksz > 0 && err == 0) { |
916 | int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE); | |
fcff0f35 | 917 | err = dump_write(dsa, type, zb->zb_object, |
2aa34383 | 918 | offset, n, n, NULL, buf); |
f1512ee6 MA |
919 | offset += n; |
920 | buf += n; | |
921 | blksz -= n; | |
922 | } | |
923 | } else { | |
2aa34383 | 924 | err = dump_write(dsa, type, zb->zb_object, offset, |
b5256303 | 925 | blksz, arc_buf_size(abuf), bp, abuf->b_data); |
f1512ee6 | 926 | } |
d3c2ae1c | 927 | arc_buf_destroy(abuf, &abuf); |
34dc7c2f BB |
928 | } |
929 | ||
930 | ASSERT(err == 0 || err == EINTR); | |
931 | return (err); | |
932 | } | |
933 | ||
6f1ffb06 | 934 | /* |
fcff0f35 PD |
935 | * Pop the new data off the queue, and free the old data. |
936 | */ | |
937 | static struct send_block_record * | |
938 | get_next_record(bqueue_t *bq, struct send_block_record *data) | |
939 | { | |
940 | struct send_block_record *tmp = bqueue_dequeue(bq); | |
941 | kmem_free(data, sizeof (*data)); | |
942 | return (tmp); | |
943 | } | |
944 | ||
945 | /* | |
946 | * Actually do the bulk of the work in a zfs send. | |
947 | * | |
948 | * Note: Releases dp using the specified tag. | |
6f1ffb06 | 949 | */ |
13fe0198 | 950 | static int |
fcff0f35 | 951 | dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds, |
2aa34383 DK |
952 | zfs_bookmark_phys_t *ancestor_zb, boolean_t is_clone, |
953 | boolean_t embedok, boolean_t large_block_ok, boolean_t compressok, | |
b5256303 | 954 | boolean_t rawok, int outfd, uint64_t resumeobj, uint64_t resumeoff, |
47dfff3b | 955 | vnode_t *vp, offset_t *off) |
34dc7c2f | 956 | { |
13fe0198 | 957 | objset_t *os; |
34dc7c2f | 958 | dmu_replay_record_t *drr; |
37abac6d | 959 | dmu_sendarg_t *dsp; |
34dc7c2f BB |
960 | int err; |
961 | uint64_t fromtxg = 0; | |
9b67f605 | 962 | uint64_t featureflags = 0; |
fcff0f35 | 963 | struct send_thread_arg to_arg; |
47dfff3b MA |
964 | void *payload = NULL; |
965 | size_t payload_len = 0; | |
fcff0f35 | 966 | struct send_block_record *to_data; |
34dc7c2f | 967 | |
fcff0f35 | 968 | err = dmu_objset_from_ds(to_ds, &os); |
13fe0198 | 969 | if (err != 0) { |
13fe0198 MA |
970 | dsl_pool_rele(dp, tag); |
971 | return (err); | |
972 | } | |
34dc7c2f | 973 | |
b5256303 TC |
974 | /* |
975 | * If this is a non-raw send of an encrypted ds, we can ensure that | |
976 | * the objset_phys_t is authenticated. This is safe because this is | |
977 | * either a snapshot or we have owned the dataset, ensuring that | |
978 | * it can't be modified. | |
979 | */ | |
980 | if (!rawok && os->os_encrypted && | |
981 | arc_is_unauthenticated(os->os_phys_buf)) { | |
982 | err = arc_untransform(os->os_phys_buf, os->os_spa, | |
983 | to_ds->ds_object, B_FALSE); | |
984 | if (err != 0) { | |
985 | dsl_pool_rele(dp, tag); | |
986 | return (err); | |
987 | } | |
988 | ||
989 | ASSERT0(arc_is_unauthenticated(os->os_phys_buf)); | |
990 | } | |
991 | ||
34dc7c2f BB |
992 | drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP); |
993 | drr->drr_type = DRR_BEGIN; | |
994 | drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC; | |
428870ff BB |
995 | DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo, |
996 | DMU_SUBSTREAM); | |
997 | ||
47dfff3b MA |
998 | bzero(&to_arg, sizeof (to_arg)); |
999 | ||
428870ff | 1000 | #ifdef _KERNEL |
13fe0198 | 1001 | if (dmu_objset_type(os) == DMU_OST_ZFS) { |
428870ff | 1002 | uint64_t version; |
13fe0198 | 1003 | if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) { |
37abac6d | 1004 | kmem_free(drr, sizeof (dmu_replay_record_t)); |
13fe0198 | 1005 | dsl_pool_rele(dp, tag); |
2e528b49 | 1006 | return (SET_ERROR(EINVAL)); |
37abac6d | 1007 | } |
13fe0198 | 1008 | if (version >= ZPL_VERSION_SA) { |
9b67f605 | 1009 | featureflags |= DMU_BACKUP_FEATURE_SA_SPILL; |
428870ff BB |
1010 | } |
1011 | } | |
1012 | #endif | |
1013 | ||
b5256303 TC |
1014 | /* raw sends imply large_block_ok */ |
1015 | if ((large_block_ok || rawok) && | |
1016 | to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS]) | |
f1512ee6 | 1017 | featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS; |
50c957f7 NB |
1018 | if (to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_DNODE]) |
1019 | featureflags |= DMU_BACKUP_FEATURE_LARGE_DNODE; | |
b5256303 TC |
1020 | |
1021 | /* encrypted datasets will not have embedded blocks */ | |
1022 | if ((embedok || rawok) && !os->os_encrypted && | |
9b67f605 MA |
1023 | spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) { |
1024 | featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA; | |
2aa34383 | 1025 | } |
b5256303 TC |
1026 | |
1027 | /* raw send implies compressok */ | |
1028 | if (compressok || rawok) | |
2aa34383 | 1029 | featureflags |= DMU_BACKUP_FEATURE_COMPRESSED; |
b5256303 TC |
1030 | if (rawok && os->os_encrypted) |
1031 | featureflags |= DMU_BACKUP_FEATURE_RAW; | |
1032 | ||
2aa34383 | 1033 | if ((featureflags & |
b5256303 TC |
1034 | (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED | |
1035 | DMU_BACKUP_FEATURE_RAW)) != 0 && | |
1036 | spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) { | |
2aa34383 | 1037 | featureflags |= DMU_BACKUP_FEATURE_LZ4; |
9b67f605 MA |
1038 | } |
1039 | ||
47dfff3b MA |
1040 | if (resumeobj != 0 || resumeoff != 0) { |
1041 | featureflags |= DMU_BACKUP_FEATURE_RESUMING; | |
1042 | } | |
1043 | ||
9b67f605 MA |
1044 | DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo, |
1045 | featureflags); | |
1046 | ||
34dc7c2f | 1047 | drr->drr_u.drr_begin.drr_creation_time = |
fcff0f35 | 1048 | dsl_dataset_phys(to_ds)->ds_creation_time; |
13fe0198 | 1049 | drr->drr_u.drr_begin.drr_type = dmu_objset_type(os); |
da536844 | 1050 | if (is_clone) |
34dc7c2f | 1051 | drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE; |
fcff0f35 PD |
1052 | drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid; |
1053 | if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET) | |
34dc7c2f | 1054 | drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA; |
b607405f AS |
1055 | if (zfs_send_set_freerecords_bit) |
1056 | drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS; | |
34dc7c2f | 1057 | |
fcff0f35 PD |
1058 | if (ancestor_zb != NULL) { |
1059 | drr->drr_u.drr_begin.drr_fromguid = | |
1060 | ancestor_zb->zbm_guid; | |
1061 | fromtxg = ancestor_zb->zbm_creation_txg; | |
da536844 | 1062 | } |
fcff0f35 PD |
1063 | dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname); |
1064 | if (!to_ds->ds_is_snapshot) { | |
da536844 MA |
1065 | (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--", |
1066 | sizeof (drr->drr_u.drr_begin.drr_toname)); | |
13fe0198 | 1067 | } |
34dc7c2f | 1068 | |
37abac6d BP |
1069 | dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP); |
1070 | ||
1071 | dsp->dsa_drr = drr; | |
1072 | dsp->dsa_vp = vp; | |
1073 | dsp->dsa_outfd = outfd; | |
1074 | dsp->dsa_proc = curproc; | |
13fe0198 | 1075 | dsp->dsa_os = os; |
37abac6d | 1076 | dsp->dsa_off = off; |
fcff0f35 | 1077 | dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid; |
37abac6d | 1078 | dsp->dsa_pending_op = PENDING_NONE; |
9b67f605 | 1079 | dsp->dsa_featureflags = featureflags; |
47dfff3b MA |
1080 | dsp->dsa_resume_object = resumeobj; |
1081 | dsp->dsa_resume_offset = resumeoff; | |
37abac6d | 1082 | |
fcff0f35 PD |
1083 | mutex_enter(&to_ds->ds_sendstream_lock); |
1084 | list_insert_head(&to_ds->ds_sendstreams, dsp); | |
1085 | mutex_exit(&to_ds->ds_sendstream_lock); | |
37abac6d | 1086 | |
fcff0f35 | 1087 | dsl_dataset_long_hold(to_ds, FTAG); |
7ec09286 MA |
1088 | dsl_pool_rele(dp, tag); |
1089 | ||
b5256303 TC |
1090 | /* handle features that require a DRR_BEGIN payload */ |
1091 | if (featureflags & | |
1092 | (DMU_BACKUP_FEATURE_RESUMING | DMU_BACKUP_FEATURE_RAW)) { | |
1093 | nvlist_t *keynvl = NULL; | |
1094 | nvlist_t *nvl = fnvlist_alloc(); | |
1095 | ||
1096 | if (featureflags & DMU_BACKUP_FEATURE_RESUMING) { | |
1097 | dmu_object_info_t to_doi; | |
1098 | err = dmu_object_info(os, resumeobj, &to_doi); | |
1099 | if (err != 0) { | |
1100 | fnvlist_free(nvl); | |
1101 | goto out; | |
1102 | } | |
1103 | ||
1104 | SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, | |
1105 | resumeobj, 0, | |
1106 | resumeoff / to_doi.doi_data_block_size); | |
1107 | ||
1108 | fnvlist_add_uint64(nvl, "resume_object", resumeobj); | |
1109 | fnvlist_add_uint64(nvl, "resume_offset", resumeoff); | |
1110 | } | |
1111 | ||
1112 | if (featureflags & DMU_BACKUP_FEATURE_RAW) { | |
1113 | ASSERT(os->os_encrypted); | |
1114 | ||
1115 | err = dsl_crypto_populate_key_nvlist(to_ds, &keynvl); | |
1116 | if (err != 0) { | |
1117 | fnvlist_free(nvl); | |
1118 | goto out; | |
1119 | } | |
1120 | ||
1121 | fnvlist_add_nvlist(nvl, "crypt_keydata", keynvl); | |
1122 | } | |
47dfff3b | 1123 | |
47dfff3b MA |
1124 | payload = fnvlist_pack(nvl, &payload_len); |
1125 | drr->drr_payloadlen = payload_len; | |
b5256303 | 1126 | fnvlist_free(keynvl); |
47dfff3b MA |
1127 | fnvlist_free(nvl); |
1128 | } | |
1129 | ||
1130 | err = dump_record(dsp, payload, payload_len); | |
1131 | fnvlist_pack_free(payload, payload_len); | |
1132 | if (err != 0) { | |
37abac6d BP |
1133 | err = dsp->dsa_err; |
1134 | goto out; | |
34dc7c2f BB |
1135 | } |
1136 | ||
fcff0f35 PD |
1137 | err = bqueue_init(&to_arg.q, zfs_send_queue_length, |
1138 | offsetof(struct send_block_record, ln)); | |
1139 | to_arg.error_code = 0; | |
1140 | to_arg.cancel = B_FALSE; | |
1141 | to_arg.ds = to_ds; | |
1142 | to_arg.fromtxg = fromtxg; | |
1143 | to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH; | |
b5256303 TC |
1144 | if (rawok) |
1145 | to_arg.flags |= TRAVERSE_NO_DECRYPT; | |
fcff0f35 PD |
1146 | (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc, |
1147 | TS_RUN, minclsyspri); | |
1148 | ||
1149 | to_data = bqueue_dequeue(&to_arg.q); | |
1150 | ||
1151 | while (!to_data->eos_marker && err == 0) { | |
1152 | err = do_dump(dsp, to_data); | |
1153 | to_data = get_next_record(&to_arg.q, to_data); | |
1154 | if (issig(JUSTLOOKING) && issig(FORREAL)) | |
1155 | err = EINTR; | |
1156 | } | |
1157 | ||
1158 | if (err != 0) { | |
1159 | to_arg.cancel = B_TRUE; | |
1160 | while (!to_data->eos_marker) { | |
1161 | to_data = get_next_record(&to_arg.q, to_data); | |
1162 | } | |
1163 | } | |
1164 | kmem_free(to_data, sizeof (*to_data)); | |
1165 | ||
1166 | bqueue_destroy(&to_arg.q); | |
1167 | ||
1168 | if (err == 0 && to_arg.error_code != 0) | |
1169 | err = to_arg.error_code; | |
1170 | ||
1171 | if (err != 0) | |
1172 | goto out; | |
34dc7c2f | 1173 | |
37abac6d | 1174 | if (dsp->dsa_pending_op != PENDING_NONE) |
37f8a883 | 1175 | if (dump_record(dsp, NULL, 0) != 0) |
2e528b49 | 1176 | err = SET_ERROR(EINTR); |
428870ff | 1177 | |
13fe0198 MA |
1178 | if (err != 0) { |
1179 | if (err == EINTR && dsp->dsa_err != 0) | |
37abac6d BP |
1180 | err = dsp->dsa_err; |
1181 | goto out; | |
34dc7c2f BB |
1182 | } |
1183 | ||
1184 | bzero(drr, sizeof (dmu_replay_record_t)); | |
1185 | drr->drr_type = DRR_END; | |
37abac6d BP |
1186 | drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc; |
1187 | drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid; | |
34dc7c2f | 1188 | |
fcff0f35 | 1189 | if (dump_record(dsp, NULL, 0) != 0) |
37abac6d | 1190 | err = dsp->dsa_err; |
37abac6d | 1191 | out: |
fcff0f35 PD |
1192 | mutex_enter(&to_ds->ds_sendstream_lock); |
1193 | list_remove(&to_ds->ds_sendstreams, dsp); | |
1194 | mutex_exit(&to_ds->ds_sendstream_lock); | |
37abac6d | 1195 | |
51907a31 K |
1196 | VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end)); |
1197 | ||
34dc7c2f | 1198 | kmem_free(drr, sizeof (dmu_replay_record_t)); |
37abac6d | 1199 | kmem_free(dsp, sizeof (dmu_sendarg_t)); |
34dc7c2f | 1200 | |
fcff0f35 | 1201 | dsl_dataset_long_rele(to_ds, FTAG); |
13fe0198 | 1202 | |
37abac6d | 1203 | return (err); |
34dc7c2f BB |
1204 | } |
1205 | ||
330d06f9 | 1206 | int |
13fe0198 | 1207 | dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap, |
2aa34383 | 1208 | boolean_t embedok, boolean_t large_block_ok, boolean_t compressok, |
b5256303 | 1209 | boolean_t rawok, int outfd, vnode_t *vp, offset_t *off) |
13fe0198 MA |
1210 | { |
1211 | dsl_pool_t *dp; | |
1212 | dsl_dataset_t *ds; | |
1213 | dsl_dataset_t *fromds = NULL; | |
b5256303 | 1214 | ds_hold_flags_t dsflags = (rawok) ? 0 : DS_HOLD_FLAG_DECRYPT; |
13fe0198 MA |
1215 | int err; |
1216 | ||
1217 | err = dsl_pool_hold(pool, FTAG, &dp); | |
1218 | if (err != 0) | |
1219 | return (err); | |
1220 | ||
b5256303 | 1221 | err = dsl_dataset_hold_obj_flags(dp, tosnap, dsflags, FTAG, &ds); |
13fe0198 MA |
1222 | if (err != 0) { |
1223 | dsl_pool_rele(dp, FTAG); | |
1224 | return (err); | |
1225 | } | |
1226 | ||
1227 | if (fromsnap != 0) { | |
da536844 MA |
1228 | zfs_bookmark_phys_t zb; |
1229 | boolean_t is_clone; | |
1230 | ||
13fe0198 MA |
1231 | err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds); |
1232 | if (err != 0) { | |
b5256303 | 1233 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
13fe0198 MA |
1234 | dsl_pool_rele(dp, FTAG); |
1235 | return (err); | |
1236 | } | |
da536844 MA |
1237 | if (!dsl_dataset_is_before(ds, fromds, 0)) |
1238 | err = SET_ERROR(EXDEV); | |
d683ddbb JG |
1239 | zb.zbm_creation_time = |
1240 | dsl_dataset_phys(fromds)->ds_creation_time; | |
1241 | zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg; | |
1242 | zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid; | |
da536844 MA |
1243 | is_clone = (fromds->ds_dir != ds->ds_dir); |
1244 | dsl_dataset_rele(fromds, FTAG); | |
f1512ee6 | 1245 | err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone, |
b5256303 TC |
1246 | embedok, large_block_ok, compressok, rawok, outfd, |
1247 | 0, 0, vp, off); | |
da536844 | 1248 | } else { |
f1512ee6 | 1249 | err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE, |
b5256303 TC |
1250 | embedok, large_block_ok, compressok, rawok, outfd, |
1251 | 0, 0, vp, off); | |
13fe0198 | 1252 | } |
b5256303 | 1253 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
da536844 | 1254 | return (err); |
13fe0198 MA |
1255 | } |
1256 | ||
1257 | int | |
47dfff3b | 1258 | dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok, |
b5256303 TC |
1259 | boolean_t large_block_ok, boolean_t compressok, boolean_t rawok, |
1260 | int outfd, uint64_t resumeobj, uint64_t resumeoff, vnode_t *vp, | |
1261 | offset_t *off) | |
13fe0198 MA |
1262 | { |
1263 | dsl_pool_t *dp; | |
1264 | dsl_dataset_t *ds; | |
13fe0198 | 1265 | int err; |
b5256303 | 1266 | ds_hold_flags_t dsflags = (rawok) ? 0 : DS_HOLD_FLAG_DECRYPT; |
da536844 | 1267 | boolean_t owned = B_FALSE; |
13fe0198 | 1268 | |
da536844 | 1269 | if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL) |
2e528b49 | 1270 | return (SET_ERROR(EINVAL)); |
13fe0198 MA |
1271 | |
1272 | err = dsl_pool_hold(tosnap, FTAG, &dp); | |
1273 | if (err != 0) | |
1274 | return (err); | |
1275 | ||
da536844 MA |
1276 | if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) { |
1277 | /* | |
1278 | * We are sending a filesystem or volume. Ensure | |
1279 | * that it doesn't change by owning the dataset. | |
1280 | */ | |
b5256303 | 1281 | err = dsl_dataset_own(dp, tosnap, dsflags, FTAG, &ds); |
da536844 MA |
1282 | owned = B_TRUE; |
1283 | } else { | |
b5256303 | 1284 | err = dsl_dataset_hold_flags(dp, tosnap, dsflags, FTAG, &ds); |
da536844 | 1285 | } |
13fe0198 MA |
1286 | if (err != 0) { |
1287 | dsl_pool_rele(dp, FTAG); | |
1288 | return (err); | |
1289 | } | |
1290 | ||
1291 | if (fromsnap != NULL) { | |
da536844 MA |
1292 | zfs_bookmark_phys_t zb; |
1293 | boolean_t is_clone = B_FALSE; | |
1294 | int fsnamelen = strchr(tosnap, '@') - tosnap; | |
1295 | ||
1296 | /* | |
1297 | * If the fromsnap is in a different filesystem, then | |
1298 | * mark the send stream as a clone. | |
1299 | */ | |
1300 | if (strncmp(tosnap, fromsnap, fsnamelen) != 0 || | |
1301 | (fromsnap[fsnamelen] != '@' && | |
1302 | fromsnap[fsnamelen] != '#')) { | |
1303 | is_clone = B_TRUE; | |
1304 | } | |
1305 | ||
1306 | if (strchr(fromsnap, '@')) { | |
1307 | dsl_dataset_t *fromds; | |
1308 | err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds); | |
1309 | if (err == 0) { | |
1310 | if (!dsl_dataset_is_before(ds, fromds, 0)) | |
1311 | err = SET_ERROR(EXDEV); | |
1312 | zb.zbm_creation_time = | |
d683ddbb | 1313 | dsl_dataset_phys(fromds)->ds_creation_time; |
da536844 | 1314 | zb.zbm_creation_txg = |
d683ddbb JG |
1315 | dsl_dataset_phys(fromds)->ds_creation_txg; |
1316 | zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid; | |
da536844 MA |
1317 | is_clone = (ds->ds_dir != fromds->ds_dir); |
1318 | dsl_dataset_rele(fromds, FTAG); | |
1319 | } | |
1320 | } else { | |
1321 | err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb); | |
1322 | } | |
13fe0198 | 1323 | if (err != 0) { |
b5256303 TC |
1324 | if (owned) |
1325 | dsl_dataset_disown(ds, dsflags, FTAG); | |
1326 | else | |
1327 | dsl_dataset_rele_flags(ds, dsflags, FTAG); | |
1328 | ||
13fe0198 MA |
1329 | dsl_pool_rele(dp, FTAG); |
1330 | return (err); | |
1331 | } | |
f1512ee6 | 1332 | err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone, |
b5256303 | 1333 | embedok, large_block_ok, compressok, rawok, |
47dfff3b | 1334 | outfd, resumeobj, resumeoff, vp, off); |
da536844 | 1335 | } else { |
f1512ee6 | 1336 | err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE, |
b5256303 | 1337 | embedok, large_block_ok, compressok, rawok, |
47dfff3b | 1338 | outfd, resumeobj, resumeoff, vp, off); |
13fe0198 | 1339 | } |
da536844 | 1340 | if (owned) |
b5256303 | 1341 | dsl_dataset_disown(ds, dsflags, FTAG); |
da536844 | 1342 | else |
b5256303 TC |
1343 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
1344 | ||
da536844 | 1345 | return (err); |
13fe0198 MA |
1346 | } |
1347 | ||
5dc8b736 | 1348 | static int |
2aa34383 DK |
1349 | dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed, |
1350 | uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep) | |
5dc8b736 MG |
1351 | { |
1352 | int err; | |
2aa34383 | 1353 | uint64_t size; |
5dc8b736 MG |
1354 | /* |
1355 | * Assume that space (both on-disk and in-stream) is dominated by | |
1356 | * data. We will adjust for indirect blocks and the copies property, | |
1357 | * but ignore per-object space used (eg, dnodes and DRR_OBJECT records). | |
1358 | */ | |
1359 | ||
2aa34383 DK |
1360 | uint64_t recordsize; |
1361 | uint64_t record_count; | |
dd429b46 PD |
1362 | objset_t *os; |
1363 | VERIFY0(dmu_objset_from_ds(ds, &os)); | |
2aa34383 DK |
1364 | |
1365 | /* Assume all (uncompressed) blocks are recordsize. */ | |
dd429b46 PD |
1366 | if (os->os_phys->os_type == DMU_OST_ZVOL) { |
1367 | err = dsl_prop_get_int_ds(ds, | |
1368 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &recordsize); | |
1369 | } else { | |
1370 | err = dsl_prop_get_int_ds(ds, | |
1371 | zfs_prop_to_name(ZFS_PROP_RECORDSIZE), &recordsize); | |
1372 | } | |
2aa34383 DK |
1373 | if (err != 0) |
1374 | return (err); | |
1375 | record_count = uncompressed / recordsize; | |
1376 | ||
1377 | /* | |
1378 | * If we're estimating a send size for a compressed stream, use the | |
1379 | * compressed data size to estimate the stream size. Otherwise, use the | |
1380 | * uncompressed data size. | |
1381 | */ | |
1382 | size = stream_compressed ? compressed : uncompressed; | |
1383 | ||
5dc8b736 MG |
1384 | /* |
1385 | * Subtract out approximate space used by indirect blocks. | |
1386 | * Assume most space is used by data blocks (non-indirect, non-dnode). | |
2aa34383 | 1387 | * Assume no ditto blocks or internal fragmentation. |
5dc8b736 MG |
1388 | * |
1389 | * Therefore, space used by indirect blocks is sizeof(blkptr_t) per | |
2aa34383 | 1390 | * block. |
5dc8b736 | 1391 | */ |
2aa34383 | 1392 | size -= record_count * sizeof (blkptr_t); |
5dc8b736 MG |
1393 | |
1394 | /* Add in the space for the record associated with each block. */ | |
2aa34383 | 1395 | size += record_count * sizeof (dmu_replay_record_t); |
5dc8b736 MG |
1396 | |
1397 | *sizep = size; | |
1398 | ||
1399 | return (0); | |
1400 | } | |
1401 | ||
13fe0198 | 1402 | int |
2aa34383 DK |
1403 | dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, |
1404 | boolean_t stream_compressed, uint64_t *sizep) | |
330d06f9 | 1405 | { |
330d06f9 | 1406 | int err; |
2aa34383 | 1407 | uint64_t uncomp, comp; |
13fe0198 | 1408 | |
fd0fd646 | 1409 | ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); |
330d06f9 MA |
1410 | |
1411 | /* tosnap must be a snapshot */ | |
0c66c32d | 1412 | if (!ds->ds_is_snapshot) |
2e528b49 | 1413 | return (SET_ERROR(EINVAL)); |
330d06f9 | 1414 | |
71e2fe41 AG |
1415 | /* fromsnap, if provided, must be a snapshot */ |
1416 | if (fromds != NULL && !fromds->ds_is_snapshot) | |
1417 | return (SET_ERROR(EINVAL)); | |
1418 | ||
6f1ffb06 MA |
1419 | /* |
1420 | * fromsnap must be an earlier snapshot from the same fs as tosnap, | |
1421 | * or the origin's fs. | |
1422 | */ | |
da536844 | 1423 | if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0)) |
2e528b49 | 1424 | return (SET_ERROR(EXDEV)); |
330d06f9 | 1425 | |
2aa34383 | 1426 | /* Get compressed and uncompressed size estimates of changed data. */ |
330d06f9 | 1427 | if (fromds == NULL) { |
2aa34383 DK |
1428 | uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes; |
1429 | comp = dsl_dataset_phys(ds)->ds_compressed_bytes; | |
330d06f9 | 1430 | } else { |
2aa34383 | 1431 | uint64_t used; |
330d06f9 | 1432 | err = dsl_dataset_space_written(fromds, ds, |
2aa34383 | 1433 | &used, &comp, &uncomp); |
13fe0198 | 1434 | if (err != 0) |
330d06f9 MA |
1435 | return (err); |
1436 | } | |
1437 | ||
2aa34383 DK |
1438 | err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp, |
1439 | stream_compressed, sizep); | |
dd429b46 PD |
1440 | /* |
1441 | * Add the size of the BEGIN and END records to the estimate. | |
1442 | */ | |
1443 | *sizep += 2 * sizeof (dmu_replay_record_t); | |
5dc8b736 MG |
1444 | return (err); |
1445 | } | |
330d06f9 | 1446 | |
2aa34383 DK |
1447 | struct calculate_send_arg { |
1448 | uint64_t uncompressed; | |
1449 | uint64_t compressed; | |
1450 | }; | |
1451 | ||
5dc8b736 MG |
1452 | /* |
1453 | * Simple callback used to traverse the blocks of a snapshot and sum their | |
2aa34383 | 1454 | * uncompressed and compressed sizes. |
5dc8b736 MG |
1455 | */ |
1456 | /* ARGSUSED */ | |
1457 | static int | |
1458 | dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
1459 | const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg) | |
1460 | { | |
2aa34383 | 1461 | struct calculate_send_arg *space = arg; |
5dc8b736 | 1462 | if (bp != NULL && !BP_IS_HOLE(bp)) { |
2aa34383 DK |
1463 | space->uncompressed += BP_GET_UCSIZE(bp); |
1464 | space->compressed += BP_GET_PSIZE(bp); | |
5dc8b736 MG |
1465 | } |
1466 | return (0); | |
1467 | } | |
1468 | ||
1469 | /* | |
1470 | * Given a desination snapshot and a TXG, calculate the approximate size of a | |
1471 | * send stream sent from that TXG. from_txg may be zero, indicating that the | |
1472 | * whole snapshot will be sent. | |
1473 | */ | |
1474 | int | |
1475 | dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg, | |
2aa34383 | 1476 | boolean_t stream_compressed, uint64_t *sizep) |
5dc8b736 | 1477 | { |
5dc8b736 | 1478 | int err; |
2aa34383 | 1479 | struct calculate_send_arg size = { 0 }; |
5dc8b736 | 1480 | |
fd0fd646 | 1481 | ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool)); |
5dc8b736 MG |
1482 | |
1483 | /* tosnap must be a snapshot */ | |
1484 | if (!dsl_dataset_is_snapshot(ds)) | |
1485 | return (SET_ERROR(EINVAL)); | |
1486 | ||
1487 | /* verify that from_txg is before the provided snapshot was taken */ | |
1488 | if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) { | |
1489 | return (SET_ERROR(EXDEV)); | |
1490 | } | |
330d06f9 | 1491 | /* |
5dc8b736 MG |
1492 | * traverse the blocks of the snapshot with birth times after |
1493 | * from_txg, summing their uncompressed size | |
330d06f9 | 1494 | */ |
b5256303 TC |
1495 | err = traverse_dataset(ds, from_txg, |
1496 | TRAVERSE_POST | TRAVERSE_NO_DECRYPT, | |
5dc8b736 | 1497 | dmu_calculate_send_traversal, &size); |
2aa34383 | 1498 | |
5dc8b736 | 1499 | if (err) |
330d06f9 | 1500 | return (err); |
330d06f9 | 1501 | |
2aa34383 DK |
1502 | err = dmu_adjust_send_estimate_for_indirects(ds, size.uncompressed, |
1503 | size.compressed, stream_compressed, sizep); | |
5dc8b736 | 1504 | return (err); |
330d06f9 MA |
1505 | } |
1506 | ||
13fe0198 MA |
1507 | typedef struct dmu_recv_begin_arg { |
1508 | const char *drba_origin; | |
1509 | dmu_recv_cookie_t *drba_cookie; | |
1510 | cred_t *drba_cred; | |
19580676 | 1511 | uint64_t drba_snapobj; |
13fe0198 | 1512 | } dmu_recv_begin_arg_t; |
34dc7c2f | 1513 | |
34dc7c2f | 1514 | static int |
13fe0198 MA |
1515 | recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds, |
1516 | uint64_t fromguid) | |
34dc7c2f | 1517 | { |
34dc7c2f | 1518 | uint64_t val; |
13fe0198 MA |
1519 | int error; |
1520 | dsl_pool_t *dp = ds->ds_dir->dd_pool; | |
34dc7c2f | 1521 | |
13fe0198 MA |
1522 | /* temporary clone name must not exist */ |
1523 | error = zap_lookup(dp->dp_meta_objset, | |
d683ddbb | 1524 | dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name, |
13fe0198 MA |
1525 | 8, 1, &val); |
1526 | if (error != ENOENT) | |
1527 | return (error == 0 ? EBUSY : error); | |
1528 | ||
572e2857 | 1529 | /* new snapshot name must not exist */ |
13fe0198 | 1530 | error = zap_lookup(dp->dp_meta_objset, |
d683ddbb JG |
1531 | dsl_dataset_phys(ds)->ds_snapnames_zapobj, |
1532 | drba->drba_cookie->drc_tosnap, 8, 1, &val); | |
13fe0198 MA |
1533 | if (error != ENOENT) |
1534 | return (error == 0 ? EEXIST : error); | |
572e2857 | 1535 | |
788eb90c JJ |
1536 | /* |
1537 | * Check snapshot limit before receiving. We'll recheck again at the | |
1538 | * end, but might as well abort before receiving if we're already over | |
1539 | * the limit. | |
1540 | * | |
1541 | * Note that we do not check the file system limit with | |
1542 | * dsl_dir_fscount_check because the temporary %clones don't count | |
1543 | * against that limit. | |
1544 | */ | |
1545 | error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT, | |
1546 | NULL, drba->drba_cred); | |
1547 | if (error != 0) | |
1548 | return (error); | |
1549 | ||
13fe0198 | 1550 | if (fromguid != 0) { |
19580676 | 1551 | dsl_dataset_t *snap; |
d683ddbb | 1552 | uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj; |
19580676 MA |
1553 | |
1554 | /* Find snapshot in this dir that matches fromguid. */ | |
1555 | while (obj != 0) { | |
1556 | error = dsl_dataset_hold_obj(dp, obj, FTAG, | |
1557 | &snap); | |
1558 | if (error != 0) | |
1559 | return (SET_ERROR(ENODEV)); | |
1560 | if (snap->ds_dir != ds->ds_dir) { | |
1561 | dsl_dataset_rele(snap, FTAG); | |
1562 | return (SET_ERROR(ENODEV)); | |
1563 | } | |
d683ddbb | 1564 | if (dsl_dataset_phys(snap)->ds_guid == fromguid) |
19580676 | 1565 | break; |
d683ddbb | 1566 | obj = dsl_dataset_phys(snap)->ds_prev_snap_obj; |
19580676 MA |
1567 | dsl_dataset_rele(snap, FTAG); |
1568 | } | |
1569 | if (obj == 0) | |
2e528b49 | 1570 | return (SET_ERROR(ENODEV)); |
34dc7c2f | 1571 | |
19580676 MA |
1572 | if (drba->drba_cookie->drc_force) { |
1573 | drba->drba_snapobj = obj; | |
1574 | } else { | |
1575 | /* | |
1576 | * If we are not forcing, there must be no | |
1577 | * changes since fromsnap. | |
1578 | */ | |
1579 | if (dsl_dataset_modified_since_snap(ds, snap)) { | |
428870ff | 1580 | dsl_dataset_rele(snap, FTAG); |
19580676 | 1581 | return (SET_ERROR(ETXTBSY)); |
428870ff | 1582 | } |
19580676 | 1583 | drba->drba_snapobj = ds->ds_prev->ds_object; |
428870ff | 1584 | } |
19580676 MA |
1585 | |
1586 | dsl_dataset_rele(snap, FTAG); | |
428870ff | 1587 | } else { |
cf50a2b0 AG |
1588 | /* if full, then must be forced */ |
1589 | if (!drba->drba_cookie->drc_force) | |
1590 | return (SET_ERROR(EEXIST)); | |
b5256303 TC |
1591 | |
1592 | /* | |
1593 | * We don't support using zfs recv -F to blow away | |
1594 | * encrypted filesystems. This would require the | |
1595 | * dsl dir to point to the old encryption key and | |
1596 | * the new one at the same time during the receive. | |
1597 | */ | |
1598 | if (ds->ds_dir->dd_crypto_obj != 0) | |
1599 | return (SET_ERROR(EINVAL)); | |
1600 | ||
1601 | drba->drba_snapobj = 0; | |
428870ff | 1602 | } |
34dc7c2f | 1603 | |
34dc7c2f | 1604 | return (0); |
13fe0198 MA |
1605 | |
1606 | } | |
1607 | ||
1608 | static int | |
1609 | dmu_recv_begin_check(void *arg, dmu_tx_t *tx) | |
1610 | { | |
1611 | dmu_recv_begin_arg_t *drba = arg; | |
1612 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1613 | struct drr_begin *drrb = drba->drba_cookie->drc_drrb; | |
1614 | uint64_t fromguid = drrb->drr_fromguid; | |
1615 | int flags = drrb->drr_flags; | |
b5256303 | 1616 | ds_hold_flags_t dsflags = 0; |
13fe0198 | 1617 | int error; |
9b67f605 | 1618 | uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); |
13fe0198 MA |
1619 | dsl_dataset_t *ds; |
1620 | const char *tofs = drba->drba_cookie->drc_tofs; | |
1621 | ||
1622 | /* already checked */ | |
1623 | ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC); | |
47dfff3b | 1624 | ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING)); |
13fe0198 MA |
1625 | |
1626 | if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == | |
1627 | DMU_COMPOUNDSTREAM || | |
1628 | drrb->drr_type >= DMU_OST_NUMTYPES || | |
1629 | ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL)) | |
2e528b49 | 1630 | return (SET_ERROR(EINVAL)); |
13fe0198 MA |
1631 | |
1632 | /* Verify pool version supports SA if SA_SPILL feature set */ | |
9b67f605 MA |
1633 | if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) && |
1634 | spa_version(dp->dp_spa) < SPA_VERSION_SA) | |
1635 | return (SET_ERROR(ENOTSUP)); | |
1636 | ||
47dfff3b MA |
1637 | if (drba->drba_cookie->drc_resumable && |
1638 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET)) | |
1639 | return (SET_ERROR(ENOTSUP)); | |
1640 | ||
9b67f605 MA |
1641 | /* |
1642 | * The receiving code doesn't know how to translate a WRITE_EMBEDDED | |
2aa34383 | 1643 | * record to a plain WRITE record, so the pool must have the |
9b67f605 MA |
1644 | * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED |
1645 | * records. Same with WRITE_EMBEDDED records that use LZ4 compression. | |
1646 | */ | |
1647 | if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) && | |
1648 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) | |
1649 | return (SET_ERROR(ENOTSUP)); | |
2aa34383 | 1650 | if ((featureflags & DMU_BACKUP_FEATURE_LZ4) && |
9b67f605 | 1651 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) |
2e528b49 | 1652 | return (SET_ERROR(ENOTSUP)); |
13fe0198 | 1653 | |
f1512ee6 MA |
1654 | /* |
1655 | * The receiving code doesn't know how to translate large blocks | |
1656 | * to smaller ones, so the pool must have the LARGE_BLOCKS | |
73aac4aa NB |
1657 | * feature enabled if the stream has LARGE_BLOCKS. Same with |
1658 | * large dnodes. | |
f1512ee6 MA |
1659 | */ |
1660 | if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) && | |
1661 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS)) | |
1662 | return (SET_ERROR(ENOTSUP)); | |
50c957f7 NB |
1663 | if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) && |
1664 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE)) | |
1665 | return (SET_ERROR(ENOTSUP)); | |
1666 | ||
b5256303 TC |
1667 | if ((featureflags & DMU_BACKUP_FEATURE_RAW)) { |
1668 | /* raw receives require the encryption feature */ | |
1669 | if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) | |
1670 | return (SET_ERROR(ENOTSUP)); | |
1671 | } else { | |
1672 | dsflags |= DS_HOLD_FLAG_DECRYPT; | |
1673 | } | |
1674 | ||
1675 | error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds); | |
13fe0198 MA |
1676 | if (error == 0) { |
1677 | /* target fs already exists; recv into temp clone */ | |
1678 | ||
1679 | /* Can't recv a clone into an existing fs */ | |
e6d3a843 | 1680 | if (flags & DRR_FLAG_CLONE || drba->drba_origin) { |
b5256303 | 1681 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
2e528b49 | 1682 | return (SET_ERROR(EINVAL)); |
13fe0198 MA |
1683 | } |
1684 | ||
1685 | error = recv_begin_check_existing_impl(drba, ds, fromguid); | |
b5256303 | 1686 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
13fe0198 MA |
1687 | } else if (error == ENOENT) { |
1688 | /* target fs does not exist; must be a full backup or clone */ | |
eca7b760 | 1689 | char buf[ZFS_MAX_DATASET_NAME_LEN]; |
13fe0198 MA |
1690 | |
1691 | /* | |
1692 | * If it's a non-clone incremental, we are missing the | |
1693 | * target fs, so fail the recv. | |
1694 | */ | |
fcff0f35 PD |
1695 | if (fromguid != 0 && !(flags & DRR_FLAG_CLONE || |
1696 | drba->drba_origin)) | |
2e528b49 | 1697 | return (SET_ERROR(ENOENT)); |
13fe0198 | 1698 | |
e6d3a843 PD |
1699 | /* |
1700 | * If we're receiving a full send as a clone, and it doesn't | |
1701 | * contain all the necessary free records and freeobject | |
1702 | * records, reject it. | |
1703 | */ | |
1704 | if (fromguid == 0 && drba->drba_origin && | |
1705 | !(flags & DRR_FLAG_FREERECORDS)) | |
1706 | return (SET_ERROR(EINVAL)); | |
1707 | ||
13fe0198 | 1708 | /* Open the parent of tofs */ |
eca7b760 | 1709 | ASSERT3U(strlen(tofs), <, sizeof (buf)); |
13fe0198 | 1710 | (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1); |
b5256303 | 1711 | error = dsl_dataset_hold_flags(dp, buf, dsflags, FTAG, &ds); |
13fe0198 MA |
1712 | if (error != 0) |
1713 | return (error); | |
1714 | ||
788eb90c JJ |
1715 | /* |
1716 | * Check filesystem and snapshot limits before receiving. We'll | |
1717 | * recheck snapshot limits again at the end (we create the | |
1718 | * filesystems and increment those counts during begin_sync). | |
1719 | */ | |
1720 | error = dsl_fs_ss_limit_check(ds->ds_dir, 1, | |
1721 | ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred); | |
1722 | if (error != 0) { | |
b5256303 | 1723 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
788eb90c JJ |
1724 | return (error); |
1725 | } | |
1726 | ||
1727 | error = dsl_fs_ss_limit_check(ds->ds_dir, 1, | |
1728 | ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred); | |
1729 | if (error != 0) { | |
b5256303 | 1730 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
788eb90c JJ |
1731 | return (error); |
1732 | } | |
1733 | ||
13fe0198 MA |
1734 | if (drba->drba_origin != NULL) { |
1735 | dsl_dataset_t *origin; | |
b5256303 TC |
1736 | |
1737 | error = dsl_dataset_hold_flags(dp, drba->drba_origin, | |
1738 | dsflags, FTAG, &origin); | |
13fe0198 | 1739 | if (error != 0) { |
b5256303 | 1740 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
13fe0198 MA |
1741 | return (error); |
1742 | } | |
0c66c32d | 1743 | if (!origin->ds_is_snapshot) { |
9b840763 | 1744 | dsl_dataset_rele_flags(origin, dsflags, FTAG); |
b5256303 | 1745 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
2e528b49 | 1746 | return (SET_ERROR(EINVAL)); |
13fe0198 | 1747 | } |
e6d3a843 PD |
1748 | if (dsl_dataset_phys(origin)->ds_guid != fromguid && |
1749 | fromguid != 0) { | |
9b840763 | 1750 | dsl_dataset_rele_flags(origin, dsflags, FTAG); |
b5256303 | 1751 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
2e528b49 | 1752 | return (SET_ERROR(ENODEV)); |
13fe0198 | 1753 | } |
b5256303 TC |
1754 | dsl_dataset_rele_flags(origin, |
1755 | dsflags, FTAG); | |
13fe0198 | 1756 | } |
b5256303 | 1757 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
13fe0198 MA |
1758 | error = 0; |
1759 | } | |
1760 | return (error); | |
34dc7c2f BB |
1761 | } |
1762 | ||
34dc7c2f | 1763 | static void |
13fe0198 | 1764 | dmu_recv_begin_sync(void *arg, dmu_tx_t *tx) |
34dc7c2f | 1765 | { |
13fe0198 MA |
1766 | dmu_recv_begin_arg_t *drba = arg; |
1767 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
47dfff3b | 1768 | objset_t *mos = dp->dp_meta_objset; |
13fe0198 MA |
1769 | struct drr_begin *drrb = drba->drba_cookie->drc_drrb; |
1770 | const char *tofs = drba->drba_cookie->drc_tofs; | |
b5256303 | 1771 | uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); |
13fe0198 | 1772 | dsl_dataset_t *ds, *newds; |
b5256303 | 1773 | objset_t *os; |
34dc7c2f | 1774 | uint64_t dsobj; |
b5256303 | 1775 | ds_hold_flags_t dsflags = 0; |
13fe0198 | 1776 | int error; |
47dfff3b | 1777 | uint64_t crflags = 0; |
b5256303 TC |
1778 | dsl_crypto_params_t *dcpp = NULL; |
1779 | dsl_crypto_params_t dcp = { 0 }; | |
13fe0198 | 1780 | |
47dfff3b MA |
1781 | if (drrb->drr_flags & DRR_FLAG_CI_DATA) |
1782 | crflags |= DS_FLAG_CI_DATASET; | |
b5256303 TC |
1783 | if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0) { |
1784 | dsflags |= DS_HOLD_FLAG_DECRYPT; | |
1785 | } else { | |
1786 | dcp.cp_cmd = DCP_CMD_RAW_RECV; | |
1787 | } | |
34dc7c2f | 1788 | |
b5256303 | 1789 | error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds); |
13fe0198 MA |
1790 | if (error == 0) { |
1791 | /* create temporary clone */ | |
19580676 | 1792 | dsl_dataset_t *snap = NULL; |
b5256303 | 1793 | |
19580676 MA |
1794 | if (drba->drba_snapobj != 0) { |
1795 | VERIFY0(dsl_dataset_hold_obj(dp, | |
1796 | drba->drba_snapobj, FTAG, &snap)); | |
b5256303 TC |
1797 | } else { |
1798 | /* we use the dcp whenever we are not making a clone */ | |
1799 | dcpp = &dcp; | |
19580676 | 1800 | } |
b5256303 | 1801 | |
13fe0198 | 1802 | dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name, |
b5256303 | 1803 | snap, crflags, drba->drba_cred, dcpp, tx); |
6b42ea85 PD |
1804 | if (drba->drba_snapobj != 0) |
1805 | dsl_dataset_rele(snap, FTAG); | |
b5256303 | 1806 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
13fe0198 MA |
1807 | } else { |
1808 | dsl_dir_t *dd; | |
1809 | const char *tail; | |
1810 | dsl_dataset_t *origin = NULL; | |
1811 | ||
1812 | VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail)); | |
1813 | ||
1814 | if (drba->drba_origin != NULL) { | |
1815 | VERIFY0(dsl_dataset_hold(dp, drba->drba_origin, | |
1816 | FTAG, &origin)); | |
b5256303 TC |
1817 | } else { |
1818 | /* we use the dcp whenever we are not making a clone */ | |
1819 | dcpp = &dcp; | |
13fe0198 MA |
1820 | } |
1821 | ||
1822 | /* Create new dataset. */ | |
b5256303 TC |
1823 | dsobj = dsl_dataset_create_sync(dd, strrchr(tofs, '/') + 1, |
1824 | origin, crflags, drba->drba_cred, dcpp, tx); | |
13fe0198 MA |
1825 | if (origin != NULL) |
1826 | dsl_dataset_rele(origin, FTAG); | |
1827 | dsl_dir_rele(dd, FTAG); | |
1828 | drba->drba_cookie->drc_newfs = B_TRUE; | |
1829 | } | |
b5256303 TC |
1830 | VERIFY0(dsl_dataset_own_obj(dp, dsobj, dsflags, dmu_recv_tag, &newds)); |
1831 | VERIFY0(dmu_objset_from_ds(newds, &os)); | |
13fe0198 | 1832 | |
47dfff3b MA |
1833 | if (drba->drba_cookie->drc_resumable) { |
1834 | uint64_t one = 1; | |
1835 | uint64_t zero = 0; | |
1836 | ||
1837 | dsl_dataset_zapify(newds, tx); | |
1838 | if (drrb->drr_fromguid != 0) { | |
1839 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID, | |
1840 | 8, 1, &drrb->drr_fromguid, tx)); | |
1841 | } | |
1842 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID, | |
1843 | 8, 1, &drrb->drr_toguid, tx)); | |
1844 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME, | |
1845 | 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx)); | |
1846 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT, | |
1847 | 8, 1, &one, tx)); | |
1848 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET, | |
1849 | 8, 1, &zero, tx)); | |
1850 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES, | |
1851 | 8, 1, &zero, tx)); | |
b5256303 | 1852 | if (featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) { |
2aa34383 | 1853 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK, |
02730c33 | 1854 | 8, 1, &one, tx)); |
2aa34383 | 1855 | } |
b5256303 | 1856 | if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) { |
47dfff3b MA |
1857 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK, |
1858 | 8, 1, &one, tx)); | |
1859 | } | |
b5256303 | 1860 | if (featureflags & DMU_BACKUP_FEATURE_COMPRESSED) { |
2aa34383 DK |
1861 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK, |
1862 | 8, 1, &one, tx)); | |
1863 | } | |
b5256303 TC |
1864 | if (featureflags & DMU_BACKUP_FEATURE_RAW) { |
1865 | VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_RAWOK, | |
1866 | 8, 1, &one, tx)); | |
1867 | } | |
1868 | } | |
1869 | ||
1870 | /* | |
1871 | * Usually the os->os_encrypted value is tied to the presence of a | |
1872 | * DSL Crypto Key object in the dd. However, that will not be received | |
1873 | * until dmu_recv_stream(), so we set the value manually for now. | |
1874 | */ | |
1875 | if (featureflags & DMU_BACKUP_FEATURE_RAW) { | |
1876 | os->os_encrypted = B_TRUE; | |
1877 | drba->drba_cookie->drc_raw = B_TRUE; | |
47dfff3b MA |
1878 | } |
1879 | ||
13fe0198 | 1880 | dmu_buf_will_dirty(newds->ds_dbuf, tx); |
d683ddbb | 1881 | dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT; |
34dc7c2f | 1882 | |
428870ff | 1883 | /* |
b5256303 TC |
1884 | * If we actually created a non-clone, we need to create the objset |
1885 | * in our new dataset. If this is a raw send we postpone this until | |
1886 | * dmu_recv_stream() so that we can allocate the metadnode with the | |
1887 | * properties from the DRR_BEGIN payload. | |
428870ff | 1888 | */ |
cc9bb3e5 | 1889 | rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG); |
b5256303 TC |
1890 | if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds)) && |
1891 | (featureflags & DMU_BACKUP_FEATURE_RAW) == 0) { | |
428870ff | 1892 | (void) dmu_objset_create_impl(dp->dp_spa, |
13fe0198 | 1893 | newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx); |
428870ff | 1894 | } |
cc9bb3e5 | 1895 | rrw_exit(&newds->ds_bp_rwlock, FTAG); |
34dc7c2f | 1896 | |
13fe0198 | 1897 | drba->drba_cookie->drc_ds = newds; |
428870ff | 1898 | |
13fe0198 | 1899 | spa_history_log_internal_ds(newds, "receive", tx, ""); |
34dc7c2f BB |
1900 | } |
1901 | ||
47dfff3b MA |
1902 | static int |
1903 | dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx) | |
1904 | { | |
1905 | dmu_recv_begin_arg_t *drba = arg; | |
1906 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1907 | struct drr_begin *drrb = drba->drba_cookie->drc_drrb; | |
1908 | int error; | |
b5256303 | 1909 | ds_hold_flags_t dsflags = 0; |
47dfff3b MA |
1910 | uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); |
1911 | dsl_dataset_t *ds; | |
1912 | const char *tofs = drba->drba_cookie->drc_tofs; | |
47dfff3b MA |
1913 | uint64_t val; |
1914 | ||
eca7b760 IK |
1915 | /* 6 extra bytes for /%recv */ |
1916 | char recvname[ZFS_MAX_DATASET_NAME_LEN + 6]; | |
1917 | ||
47dfff3b MA |
1918 | /* already checked */ |
1919 | ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC); | |
1920 | ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING); | |
1921 | ||
1922 | if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == | |
1923 | DMU_COMPOUNDSTREAM || | |
1924 | drrb->drr_type >= DMU_OST_NUMTYPES) | |
1925 | return (SET_ERROR(EINVAL)); | |
1926 | ||
1927 | /* Verify pool version supports SA if SA_SPILL feature set */ | |
1928 | if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) && | |
1929 | spa_version(dp->dp_spa) < SPA_VERSION_SA) | |
1930 | return (SET_ERROR(ENOTSUP)); | |
1931 | ||
1932 | /* | |
1933 | * The receiving code doesn't know how to translate a WRITE_EMBEDDED | |
1934 | * record to a plain WRITE record, so the pool must have the | |
1935 | * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED | |
1936 | * records. Same with WRITE_EMBEDDED records that use LZ4 compression. | |
1937 | */ | |
1938 | if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) && | |
1939 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) | |
1940 | return (SET_ERROR(ENOTSUP)); | |
2aa34383 | 1941 | if ((featureflags & DMU_BACKUP_FEATURE_LZ4) && |
47dfff3b MA |
1942 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) |
1943 | return (SET_ERROR(ENOTSUP)); | |
1944 | ||
73aac4aa NB |
1945 | /* |
1946 | * The receiving code doesn't know how to translate large blocks | |
1947 | * to smaller ones, so the pool must have the LARGE_BLOCKS | |
1948 | * feature enabled if the stream has LARGE_BLOCKS. Same with | |
1949 | * large dnodes. | |
1950 | */ | |
1951 | if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) && | |
1952 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS)) | |
1953 | return (SET_ERROR(ENOTSUP)); | |
1954 | if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) && | |
1955 | !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE)) | |
1956 | return (SET_ERROR(ENOTSUP)); | |
1957 | ||
47dfff3b MA |
1958 | (void) snprintf(recvname, sizeof (recvname), "%s/%s", |
1959 | tofs, recv_clone_name); | |
1960 | ||
b5256303 TC |
1961 | if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0) |
1962 | dsflags |= DS_HOLD_FLAG_DECRYPT; | |
1963 | ||
1964 | if (dsl_dataset_hold_flags(dp, recvname, dsflags, FTAG, &ds) != 0) { | |
47dfff3b | 1965 | /* %recv does not exist; continue in tofs */ |
b5256303 | 1966 | error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds); |
47dfff3b MA |
1967 | if (error != 0) |
1968 | return (error); | |
1969 | } | |
1970 | ||
1971 | /* check that ds is marked inconsistent */ | |
1972 | if (!DS_IS_INCONSISTENT(ds)) { | |
b5256303 | 1973 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
47dfff3b MA |
1974 | return (SET_ERROR(EINVAL)); |
1975 | } | |
1976 | ||
1977 | /* check that there is resuming data, and that the toguid matches */ | |
1978 | if (!dsl_dataset_is_zapified(ds)) { | |
b5256303 | 1979 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
47dfff3b MA |
1980 | return (SET_ERROR(EINVAL)); |
1981 | } | |
1982 | error = zap_lookup(dp->dp_meta_objset, ds->ds_object, | |
1983 | DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val); | |
1984 | if (error != 0 || drrb->drr_toguid != val) { | |
b5256303 | 1985 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
47dfff3b MA |
1986 | return (SET_ERROR(EINVAL)); |
1987 | } | |
1988 | ||
1989 | /* | |
1990 | * Check if the receive is still running. If so, it will be owned. | |
1991 | * Note that nothing else can own the dataset (e.g. after the receive | |
1992 | * fails) because it will be marked inconsistent. | |
1993 | */ | |
1994 | if (dsl_dataset_has_owner(ds)) { | |
b5256303 | 1995 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
47dfff3b MA |
1996 | return (SET_ERROR(EBUSY)); |
1997 | } | |
1998 | ||
1999 | /* There should not be any snapshots of this fs yet. */ | |
2000 | if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) { | |
b5256303 | 2001 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
47dfff3b MA |
2002 | return (SET_ERROR(EINVAL)); |
2003 | } | |
2004 | ||
2005 | /* | |
2006 | * Note: resume point will be checked when we process the first WRITE | |
2007 | * record. | |
2008 | */ | |
2009 | ||
2010 | /* check that the origin matches */ | |
2011 | val = 0; | |
2012 | (void) zap_lookup(dp->dp_meta_objset, ds->ds_object, | |
2013 | DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val); | |
2014 | if (drrb->drr_fromguid != val) { | |
b5256303 | 2015 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
47dfff3b MA |
2016 | return (SET_ERROR(EINVAL)); |
2017 | } | |
2018 | ||
b5256303 | 2019 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
47dfff3b MA |
2020 | return (0); |
2021 | } | |
2022 | ||
2023 | static void | |
2024 | dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx) | |
2025 | { | |
2026 | dmu_recv_begin_arg_t *drba = arg; | |
2027 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
2028 | const char *tofs = drba->drba_cookie->drc_tofs; | |
b5256303 TC |
2029 | struct drr_begin *drrb = drba->drba_cookie->drc_drrb; |
2030 | uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo); | |
47dfff3b | 2031 | dsl_dataset_t *ds; |
b5256303 TC |
2032 | objset_t *os; |
2033 | ds_hold_flags_t dsflags = 0; | |
47dfff3b | 2034 | uint64_t dsobj; |
eca7b760 IK |
2035 | /* 6 extra bytes for /%recv */ |
2036 | char recvname[ZFS_MAX_DATASET_NAME_LEN + 6]; | |
47dfff3b MA |
2037 | |
2038 | (void) snprintf(recvname, sizeof (recvname), "%s/%s", | |
2039 | tofs, recv_clone_name); | |
2040 | ||
b5256303 TC |
2041 | if (featureflags & DMU_BACKUP_FEATURE_RAW) { |
2042 | drba->drba_cookie->drc_raw = B_TRUE; | |
2043 | } else { | |
2044 | dsflags |= DS_HOLD_FLAG_DECRYPT; | |
2045 | } | |
2046 | ||
2047 | if (dsl_dataset_hold_flags(dp, recvname, dsflags, FTAG, &ds) != 0) { | |
47dfff3b | 2048 | /* %recv does not exist; continue in tofs */ |
b5256303 | 2049 | VERIFY0(dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds)); |
47dfff3b MA |
2050 | drba->drba_cookie->drc_newfs = B_TRUE; |
2051 | } | |
2052 | ||
2053 | /* clear the inconsistent flag so that we can own it */ | |
2054 | ASSERT(DS_IS_INCONSISTENT(ds)); | |
2055 | dmu_buf_will_dirty(ds->ds_dbuf, tx); | |
2056 | dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT; | |
2057 | dsobj = ds->ds_object; | |
b5256303 | 2058 | dsl_dataset_rele_flags(ds, dsflags, FTAG); |
47dfff3b | 2059 | |
b5256303 TC |
2060 | VERIFY0(dsl_dataset_own_obj(dp, dsobj, dsflags, dmu_recv_tag, &ds)); |
2061 | VERIFY0(dmu_objset_from_ds(ds, &os)); | |
47dfff3b MA |
2062 | |
2063 | dmu_buf_will_dirty(ds->ds_dbuf, tx); | |
2064 | dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT; | |
2065 | ||
cc9bb3e5 | 2066 | rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); |
2637dda8 TC |
2067 | ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)) || |
2068 | drba->drba_cookie->drc_raw); | |
cc9bb3e5 | 2069 | rrw_exit(&ds->ds_bp_rwlock, FTAG); |
47dfff3b MA |
2070 | |
2071 | drba->drba_cookie->drc_ds = ds; | |
2072 | ||
2073 | spa_history_log_internal_ds(ds, "resume receive", tx, ""); | |
2074 | } | |
2075 | ||
34dc7c2f BB |
2076 | /* |
2077 | * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin() | |
2078 | * succeeds; otherwise we will leak the holds on the datasets. | |
2079 | */ | |
2080 | int | |
47dfff3b MA |
2081 | dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin, |
2082 | boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc) | |
34dc7c2f | 2083 | { |
13fe0198 | 2084 | dmu_recv_begin_arg_t drba = { 0 }; |
34dc7c2f BB |
2085 | |
2086 | bzero(drc, sizeof (dmu_recv_cookie_t)); | |
47dfff3b MA |
2087 | drc->drc_drr_begin = drr_begin; |
2088 | drc->drc_drrb = &drr_begin->drr_u.drr_begin; | |
34dc7c2f | 2089 | drc->drc_tosnap = tosnap; |
13fe0198 | 2090 | drc->drc_tofs = tofs; |
34dc7c2f | 2091 | drc->drc_force = force; |
47dfff3b | 2092 | drc->drc_resumable = resumable; |
788eb90c | 2093 | drc->drc_cred = CRED(); |
48fbb9dd | 2094 | drc->drc_clone = (origin != NULL); |
34dc7c2f | 2095 | |
47dfff3b | 2096 | if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { |
13fe0198 | 2097 | drc->drc_byteswap = B_TRUE; |
a6255b7f | 2098 | (void) fletcher_4_incremental_byteswap(drr_begin, |
13fe0198 | 2099 | sizeof (dmu_replay_record_t), &drc->drc_cksum); |
47dfff3b MA |
2100 | byteswap_record(drr_begin); |
2101 | } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) { | |
a6255b7f | 2102 | (void) fletcher_4_incremental_native(drr_begin, |
13fe0198 | 2103 | sizeof (dmu_replay_record_t), &drc->drc_cksum); |
47dfff3b MA |
2104 | } else { |
2105 | return (SET_ERROR(EINVAL)); | |
34dc7c2f BB |
2106 | } |
2107 | ||
13fe0198 MA |
2108 | drba.drba_origin = origin; |
2109 | drba.drba_cookie = drc; | |
2110 | drba.drba_cred = CRED(); | |
2111 | ||
47dfff3b MA |
2112 | if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) & |
2113 | DMU_BACKUP_FEATURE_RESUMING) { | |
2114 | return (dsl_sync_task(tofs, | |
2115 | dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync, | |
2116 | &drba, 5, ZFS_SPACE_CHECK_NORMAL)); | |
2117 | } else { | |
2118 | return (dsl_sync_task(tofs, | |
2119 | dmu_recv_begin_check, dmu_recv_begin_sync, | |
2120 | &drba, 5, ZFS_SPACE_CHECK_NORMAL)); | |
2121 | } | |
34dc7c2f BB |
2122 | } |
2123 | ||
fcff0f35 PD |
2124 | struct receive_record_arg { |
2125 | dmu_replay_record_t header; | |
2126 | void *payload; /* Pointer to a buffer containing the payload */ | |
2127 | /* | |
2128 | * If the record is a write, pointer to the arc_buf_t containing the | |
2129 | * payload. | |
2130 | */ | |
b5256303 | 2131 | arc_buf_t *arc_buf; |
fcff0f35 | 2132 | int payload_size; |
47dfff3b | 2133 | uint64_t bytes_read; /* bytes read from stream when record created */ |
fcff0f35 PD |
2134 | boolean_t eos_marker; /* Marks the end of the stream */ |
2135 | bqueue_node_t node; | |
2136 | }; | |
2137 | ||
2138 | struct receive_writer_arg { | |
37f8a883 | 2139 | objset_t *os; |
13fe0198 | 2140 | boolean_t byteswap; |
fcff0f35 | 2141 | bqueue_t q; |
47dfff3b | 2142 | |
fcff0f35 PD |
2143 | /* |
2144 | * These three args are used to signal to the main thread that we're | |
2145 | * done. | |
2146 | */ | |
2147 | kmutex_t mutex; | |
2148 | kcondvar_t cv; | |
2149 | boolean_t done; | |
47dfff3b | 2150 | |
fcff0f35 PD |
2151 | int err; |
2152 | /* A map from guid to dataset to help handle dedup'd streams. */ | |
2153 | avl_tree_t *guid_to_ds_map; | |
47dfff3b | 2154 | boolean_t resumable; |
9b840763 | 2155 | boolean_t raw; |
48fbb9dd FG |
2156 | uint64_t last_object; |
2157 | uint64_t last_offset; | |
2158 | uint64_t max_object; /* highest object ID referenced in stream */ | |
47dfff3b | 2159 | uint64_t bytes_read; /* bytes read when current record created */ |
fcff0f35 | 2160 | }; |
37f8a883 | 2161 | |
e6d3a843 PD |
2162 | struct objlist { |
2163 | list_t list; /* List of struct receive_objnode. */ | |
2164 | /* | |
2165 | * Last object looked up. Used to assert that objects are being looked | |
2166 | * up in ascending order. | |
2167 | */ | |
2168 | uint64_t last_lookup; | |
2169 | }; | |
2170 | ||
2171 | struct receive_objnode { | |
2172 | list_node_t node; | |
2173 | uint64_t object; | |
2174 | }; | |
2175 | ||
fcff0f35 PD |
2176 | struct receive_arg { |
2177 | objset_t *os; | |
2178 | vnode_t *vp; /* The vnode to read the stream from */ | |
2179 | uint64_t voff; /* The current offset in the stream */ | |
47dfff3b | 2180 | uint64_t bytes_read; |
fcff0f35 PD |
2181 | /* |
2182 | * A record that has had its payload read in, but hasn't yet been handed | |
2183 | * off to the worker thread. | |
2184 | */ | |
2185 | struct receive_record_arg *rrd; | |
2186 | /* A record that has had its header read in, but not its payload. */ | |
2187 | struct receive_record_arg *next_rrd; | |
34dc7c2f | 2188 | zio_cksum_t cksum; |
37f8a883 | 2189 | zio_cksum_t prev_cksum; |
fcff0f35 PD |
2190 | int err; |
2191 | boolean_t byteswap; | |
9b840763 | 2192 | boolean_t raw; |
b5256303 | 2193 | uint64_t featureflags; |
fcff0f35 | 2194 | /* Sorted list of objects not to issue prefetches for. */ |
e6d3a843 | 2195 | struct objlist ignore_objlist; |
34dc7c2f BB |
2196 | }; |
2197 | ||
428870ff BB |
2198 | typedef struct guid_map_entry { |
2199 | uint64_t guid; | |
b5256303 | 2200 | boolean_t raw; |
428870ff BB |
2201 | dsl_dataset_t *gme_ds; |
2202 | avl_node_t avlnode; | |
2203 | } guid_map_entry_t; | |
2204 | ||
2205 | static int | |
2206 | guid_compare(const void *arg1, const void *arg2) | |
2207 | { | |
ee36c709 GN |
2208 | const guid_map_entry_t *gmep1 = (const guid_map_entry_t *)arg1; |
2209 | const guid_map_entry_t *gmep2 = (const guid_map_entry_t *)arg2; | |
428870ff | 2210 | |
ee36c709 | 2211 | return (AVL_CMP(gmep1->guid, gmep2->guid)); |
428870ff BB |
2212 | } |
2213 | ||
572e2857 BB |
2214 | static void |
2215 | free_guid_map_onexit(void *arg) | |
2216 | { | |
2217 | avl_tree_t *ca = arg; | |
2218 | void *cookie = NULL; | |
2219 | guid_map_entry_t *gmep; | |
2220 | ||
2221 | while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) { | |
13fe0198 | 2222 | dsl_dataset_long_rele(gmep->gme_ds, gmep); |
b5256303 TC |
2223 | dsl_dataset_rele_flags(gmep->gme_ds, |
2224 | (gmep->raw) ? 0 : DS_HOLD_FLAG_DECRYPT, gmep); | |
572e2857 BB |
2225 | kmem_free(gmep, sizeof (guid_map_entry_t)); |
2226 | } | |
2227 | avl_destroy(ca); | |
2228 | kmem_free(ca, sizeof (avl_tree_t)); | |
2229 | } | |
2230 | ||
37f8a883 | 2231 | static int |
fcff0f35 | 2232 | receive_read(struct receive_arg *ra, int len, void *buf) |
34dc7c2f | 2233 | { |
34dc7c2f BB |
2234 | int done = 0; |
2235 | ||
f8866f8a ER |
2236 | /* |
2237 | * The code doesn't rely on this (lengths being multiples of 8). See | |
2238 | * comment in dump_bytes. | |
2239 | */ | |
b5256303 TC |
2240 | ASSERT(len % 8 == 0 || |
2241 | (ra->featureflags & DMU_BACKUP_FEATURE_RAW) != 0); | |
34dc7c2f BB |
2242 | |
2243 | while (done < len) { | |
2244 | ssize_t resid; | |
2245 | ||
2246 | ra->err = vn_rdwr(UIO_READ, ra->vp, | |
37f8a883 | 2247 | (char *)buf + done, len - done, |
34dc7c2f BB |
2248 | ra->voff, UIO_SYSSPACE, FAPPEND, |
2249 | RLIM64_INFINITY, CRED(), &resid); | |
2250 | ||
47dfff3b MA |
2251 | if (resid == len - done) { |
2252 | /* | |
2253 | * Note: ECKSUM indicates that the receive | |
2254 | * was interrupted and can potentially be resumed. | |
2255 | */ | |
2256 | ra->err = SET_ERROR(ECKSUM); | |
2257 | } | |
34dc7c2f BB |
2258 | ra->voff += len - done - resid; |
2259 | done = len - resid; | |
13fe0198 | 2260 | if (ra->err != 0) |
37f8a883 | 2261 | return (ra->err); |
34dc7c2f BB |
2262 | } |
2263 | ||
47dfff3b MA |
2264 | ra->bytes_read += len; |
2265 | ||
34dc7c2f | 2266 | ASSERT3U(done, ==, len); |
37f8a883 | 2267 | return (0); |
34dc7c2f BB |
2268 | } |
2269 | ||
60948de1 | 2270 | noinline static void |
37f8a883 | 2271 | byteswap_record(dmu_replay_record_t *drr) |
34dc7c2f BB |
2272 | { |
2273 | #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X)) | |
2274 | #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X)) | |
2275 | drr->drr_type = BSWAP_32(drr->drr_type); | |
2276 | drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen); | |
37f8a883 | 2277 | |
34dc7c2f BB |
2278 | switch (drr->drr_type) { |
2279 | case DRR_BEGIN: | |
2280 | DO64(drr_begin.drr_magic); | |
428870ff | 2281 | DO64(drr_begin.drr_versioninfo); |
34dc7c2f BB |
2282 | DO64(drr_begin.drr_creation_time); |
2283 | DO32(drr_begin.drr_type); | |
2284 | DO32(drr_begin.drr_flags); | |
2285 | DO64(drr_begin.drr_toguid); | |
2286 | DO64(drr_begin.drr_fromguid); | |
2287 | break; | |
2288 | case DRR_OBJECT: | |
2289 | DO64(drr_object.drr_object); | |
34dc7c2f BB |
2290 | DO32(drr_object.drr_type); |
2291 | DO32(drr_object.drr_bonustype); | |
2292 | DO32(drr_object.drr_blksz); | |
2293 | DO32(drr_object.drr_bonuslen); | |
b5256303 | 2294 | DO32(drr_object.drr_raw_bonuslen); |
428870ff | 2295 | DO64(drr_object.drr_toguid); |
34dc7c2f BB |
2296 | break; |
2297 | case DRR_FREEOBJECTS: | |
2298 | DO64(drr_freeobjects.drr_firstobj); | |
2299 | DO64(drr_freeobjects.drr_numobjs); | |
428870ff | 2300 | DO64(drr_freeobjects.drr_toguid); |
34dc7c2f BB |
2301 | break; |
2302 | case DRR_WRITE: | |
2303 | DO64(drr_write.drr_object); | |
2304 | DO32(drr_write.drr_type); | |
2305 | DO64(drr_write.drr_offset); | |
2aa34383 | 2306 | DO64(drr_write.drr_logical_size); |
428870ff | 2307 | DO64(drr_write.drr_toguid); |
37f8a883 | 2308 | ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum); |
428870ff | 2309 | DO64(drr_write.drr_key.ddk_prop); |
2aa34383 | 2310 | DO64(drr_write.drr_compressed_size); |
428870ff BB |
2311 | break; |
2312 | case DRR_WRITE_BYREF: | |
2313 | DO64(drr_write_byref.drr_object); | |
2314 | DO64(drr_write_byref.drr_offset); | |
2315 | DO64(drr_write_byref.drr_length); | |
2316 | DO64(drr_write_byref.drr_toguid); | |
2317 | DO64(drr_write_byref.drr_refguid); | |
2318 | DO64(drr_write_byref.drr_refobject); | |
2319 | DO64(drr_write_byref.drr_refoffset); | |
37f8a883 MA |
2320 | ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref. |
2321 | drr_key.ddk_cksum); | |
428870ff | 2322 | DO64(drr_write_byref.drr_key.ddk_prop); |
34dc7c2f | 2323 | break; |
9b67f605 MA |
2324 | case DRR_WRITE_EMBEDDED: |
2325 | DO64(drr_write_embedded.drr_object); | |
2326 | DO64(drr_write_embedded.drr_offset); | |
2327 | DO64(drr_write_embedded.drr_length); | |
2328 | DO64(drr_write_embedded.drr_toguid); | |
2329 | DO32(drr_write_embedded.drr_lsize); | |
2330 | DO32(drr_write_embedded.drr_psize); | |
2331 | break; | |
34dc7c2f BB |
2332 | case DRR_FREE: |
2333 | DO64(drr_free.drr_object); | |
2334 | DO64(drr_free.drr_offset); | |
2335 | DO64(drr_free.drr_length); | |
428870ff BB |
2336 | DO64(drr_free.drr_toguid); |
2337 | break; | |
2338 | case DRR_SPILL: | |
2339 | DO64(drr_spill.drr_object); | |
2340 | DO64(drr_spill.drr_length); | |
2341 | DO64(drr_spill.drr_toguid); | |
b5256303 TC |
2342 | DO64(drr_spill.drr_compressed_size); |
2343 | DO32(drr_spill.drr_type); | |
2344 | break; | |
2345 | case DRR_OBJECT_RANGE: | |
2346 | DO64(drr_object_range.drr_firstobj); | |
2347 | DO64(drr_object_range.drr_numslots); | |
2348 | DO64(drr_object_range.drr_toguid); | |
34dc7c2f BB |
2349 | break; |
2350 | case DRR_END: | |
428870ff | 2351 | DO64(drr_end.drr_toguid); |
37f8a883 | 2352 | ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum); |
34dc7c2f | 2353 | break; |
e75c13c3 BB |
2354 | default: |
2355 | break; | |
34dc7c2f | 2356 | } |
37f8a883 MA |
2357 | |
2358 | if (drr->drr_type != DRR_BEGIN) { | |
2359 | ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum); | |
2360 | } | |
2361 | ||
34dc7c2f BB |
2362 | #undef DO64 |
2363 | #undef DO32 | |
2364 | } | |
2365 | ||
6c59307a MA |
2366 | static inline uint8_t |
2367 | deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size) | |
2368 | { | |
2369 | if (bonus_type == DMU_OT_SA) { | |
2370 | return (1); | |
2371 | } else { | |
2372 | return (1 + | |
50c957f7 NB |
2373 | ((DN_OLD_MAX_BONUSLEN - |
2374 | MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT)); | |
6c59307a MA |
2375 | } |
2376 | } | |
2377 | ||
47dfff3b MA |
2378 | static void |
2379 | save_resume_state(struct receive_writer_arg *rwa, | |
2380 | uint64_t object, uint64_t offset, dmu_tx_t *tx) | |
2381 | { | |
2382 | int txgoff = dmu_tx_get_txg(tx) & TXG_MASK; | |
2383 | ||
2384 | if (!rwa->resumable) | |
2385 | return; | |
2386 | ||
2387 | /* | |
2388 | * We use ds_resume_bytes[] != 0 to indicate that we need to | |
2389 | * update this on disk, so it must not be 0. | |
2390 | */ | |
2391 | ASSERT(rwa->bytes_read != 0); | |
2392 | ||
2393 | /* | |
2394 | * We only resume from write records, which have a valid | |
2395 | * (non-meta-dnode) object number. | |
2396 | */ | |
2397 | ASSERT(object != 0); | |
2398 | ||
2399 | /* | |
2400 | * For resuming to work correctly, we must receive records in order, | |
2401 | * sorted by object,offset. This is checked by the callers, but | |
2402 | * assert it here for good measure. | |
2403 | */ | |
2404 | ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]); | |
2405 | ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] || | |
2406 | offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]); | |
2407 | ASSERT3U(rwa->bytes_read, >=, | |
2408 | rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]); | |
2409 | ||
2410 | rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object; | |
2411 | rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset; | |
2412 | rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read; | |
2413 | } | |
2414 | ||
60948de1 | 2415 | noinline static int |
fcff0f35 | 2416 | receive_object(struct receive_writer_arg *rwa, struct drr_object *drro, |
4ea3f864 | 2417 | void *data) |
34dc7c2f | 2418 | { |
6c59307a | 2419 | dmu_object_info_t doi; |
34dc7c2f | 2420 | dmu_tx_t *tx; |
6c59307a MA |
2421 | uint64_t object; |
2422 | int err; | |
34dc7c2f | 2423 | |
34dc7c2f | 2424 | if (drro->drr_type == DMU_OT_NONE || |
9ae529ec CS |
2425 | !DMU_OT_IS_VALID(drro->drr_type) || |
2426 | !DMU_OT_IS_VALID(drro->drr_bonustype) || | |
428870ff | 2427 | drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS || |
34dc7c2f BB |
2428 | drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS || |
2429 | P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) || | |
2430 | drro->drr_blksz < SPA_MINBLOCKSIZE || | |
fcff0f35 | 2431 | drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) || |
50c957f7 | 2432 | drro->drr_bonuslen > |
73aac4aa NB |
2433 | DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa->os))) || |
2434 | drro->drr_dn_slots > | |
2435 | (spa_maxdnodesize(dmu_objset_spa(rwa->os)) >> DNODE_SHIFT)) { | |
2e528b49 | 2436 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
2437 | } |
2438 | ||
9b840763 | 2439 | if (rwa->raw) { |
b5256303 TC |
2440 | if (drro->drr_raw_bonuslen < drro->drr_bonuslen || |
2441 | drro->drr_indblkshift > SPA_MAXBLOCKSHIFT || | |
2442 | drro->drr_nlevels > DN_MAX_LEVELS || | |
2443 | drro->drr_nblkptr > DN_MAX_NBLKPTR || | |
2444 | DN_SLOTS_TO_BONUSLEN(drro->drr_dn_slots) < | |
2445 | drro->drr_raw_bonuslen) | |
2446 | return (SET_ERROR(EINVAL)); | |
2447 | } else { | |
2448 | if (drro->drr_flags != 0 || drro->drr_raw_bonuslen != 0 || | |
2449 | drro->drr_indblkshift != 0 || drro->drr_nlevels != 0 || | |
2450 | drro->drr_nblkptr != 0) | |
2451 | return (SET_ERROR(EINVAL)); | |
2452 | } | |
2453 | ||
fcff0f35 | 2454 | err = dmu_object_info(rwa->os, drro->drr_object, &doi); |
9babb374 BB |
2455 | |
2456 | if (err != 0 && err != ENOENT) | |
2e528b49 | 2457 | return (SET_ERROR(EINVAL)); |
6c59307a | 2458 | object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT; |
9babb374 | 2459 | |
48fbb9dd FG |
2460 | if (drro->drr_object > rwa->max_object) |
2461 | rwa->max_object = drro->drr_object; | |
2462 | ||
6c59307a MA |
2463 | /* |
2464 | * If we are losing blkptrs or changing the block size this must | |
2465 | * be a new file instance. We must clear out the previous file | |
2466 | * contents before we can change this type of metadata in the dnode. | |
b5256303 TC |
2467 | * Raw receives will also check that the indirect structure of the |
2468 | * dnode hasn't changed. | |
6c59307a MA |
2469 | */ |
2470 | if (err == 0) { | |
b5256303 TC |
2471 | uint32_t indblksz = drro->drr_indblkshift ? |
2472 | 1ULL << drro->drr_indblkshift : 0; | |
2473 | int nblkptr = deduce_nblkptr(drro->drr_bonustype, | |
6c59307a MA |
2474 | drro->drr_bonuslen); |
2475 | ||
b5256303 | 2476 | /* nblkptr will be bounded by the bonus size and type */ |
9b840763 | 2477 | if (rwa->raw && nblkptr != drro->drr_nblkptr) |
b5256303 TC |
2478 | return (SET_ERROR(EINVAL)); |
2479 | ||
6c59307a | 2480 | if (drro->drr_blksz != doi.doi_data_block_size || |
b5256303 | 2481 | nblkptr < doi.doi_nblkptr || |
9b840763 | 2482 | (rwa->raw && |
b5256303 TC |
2483 | (indblksz != doi.doi_metadata_block_size || |
2484 | drro->drr_nlevels < doi.doi_indirection))) { | |
fcff0f35 | 2485 | err = dmu_free_long_range(rwa->os, drro->drr_object, |
6c59307a MA |
2486 | 0, DMU_OBJECT_END); |
2487 | if (err != 0) | |
2488 | return (SET_ERROR(EINVAL)); | |
34dc7c2f | 2489 | } |
6c59307a MA |
2490 | } |
2491 | ||
fcff0f35 | 2492 | tx = dmu_tx_create(rwa->os); |
6c59307a | 2493 | dmu_tx_hold_bonus(tx, object); |
b5256303 | 2494 | dmu_tx_hold_write(tx, object, 0, 0); |
6c59307a MA |
2495 | err = dmu_tx_assign(tx, TXG_WAIT); |
2496 | if (err != 0) { | |
2497 | dmu_tx_abort(tx); | |
2498 | return (err); | |
2499 | } | |
2500 | ||
2501 | if (object == DMU_NEW_OBJECT) { | |
2502 | /* currently free, want to be allocated */ | |
50c957f7 | 2503 | err = dmu_object_claim_dnsize(rwa->os, drro->drr_object, |
34dc7c2f | 2504 | drro->drr_type, drro->drr_blksz, |
50c957f7 NB |
2505 | drro->drr_bonustype, drro->drr_bonuslen, |
2506 | drro->drr_dn_slots << DNODE_SHIFT, tx); | |
6c59307a MA |
2507 | } else if (drro->drr_type != doi.doi_type || |
2508 | drro->drr_blksz != doi.doi_data_block_size || | |
2509 | drro->drr_bonustype != doi.doi_bonus_type || | |
e0dd0a32 | 2510 | drro->drr_bonuslen != doi.doi_bonus_size) { |
6c59307a | 2511 | /* currently allocated, but with different properties */ |
e0dd0a32 | 2512 | err = dmu_object_reclaim(rwa->os, drro->drr_object, |
34dc7c2f | 2513 | drro->drr_type, drro->drr_blksz, |
e0dd0a32 | 2514 | drro->drr_bonustype, drro->drr_bonuslen, tx); |
34dc7c2f | 2515 | } |
13fe0198 | 2516 | if (err != 0) { |
9b840763 | 2517 | dmu_tx_commit(tx); |
2e528b49 | 2518 | return (SET_ERROR(EINVAL)); |
428870ff | 2519 | } |
9babb374 | 2520 | |
9b840763 TC |
2521 | if (rwa->raw) |
2522 | VERIFY0(dmu_object_dirty_raw(rwa->os, drro->drr_object, tx)); | |
2523 | ||
fcff0f35 | 2524 | dmu_object_set_checksum(rwa->os, drro->drr_object, |
37f8a883 | 2525 | drro->drr_checksumtype, tx); |
fcff0f35 | 2526 | dmu_object_set_compress(rwa->os, drro->drr_object, |
37f8a883 | 2527 | drro->drr_compress, tx); |
34dc7c2f | 2528 | |
b5256303 | 2529 | /* handle more restrictive dnode structuring for raw recvs */ |
9b840763 | 2530 | if (rwa->raw) { |
b5256303 TC |
2531 | /* |
2532 | * Set the indirect block shift and nlevels. This will not fail | |
2533 | * because we ensured all of the blocks were free earlier if | |
2534 | * this is a new object. | |
2535 | */ | |
2536 | VERIFY0(dmu_object_set_blocksize(rwa->os, drro->drr_object, | |
2537 | drro->drr_blksz, drro->drr_indblkshift, tx)); | |
2538 | VERIFY0(dmu_object_set_nlevels(rwa->os, drro->drr_object, | |
2539 | drro->drr_nlevels, tx)); | |
2540 | } | |
2541 | ||
b128c09f | 2542 | if (data != NULL) { |
34dc7c2f | 2543 | dmu_buf_t *db; |
b5256303 | 2544 | uint32_t flags = DMU_READ_NO_PREFETCH; |
b128c09f | 2545 | |
9b840763 | 2546 | if (rwa->raw) |
b5256303 TC |
2547 | flags |= DMU_READ_NO_DECRYPT; |
2548 | ||
2549 | VERIFY0(dmu_bonus_hold_impl(rwa->os, drro->drr_object, | |
2550 | FTAG, flags, &db)); | |
34dc7c2f BB |
2551 | dmu_buf_will_dirty(db, tx); |
2552 | ||
2553 | ASSERT3U(db->db_size, >=, drro->drr_bonuslen); | |
b5256303 TC |
2554 | bcopy(data, db->db_data, DRR_OBJECT_PAYLOAD_SIZE(drro)); |
2555 | ||
2556 | /* | |
2557 | * Raw bonus buffers have their byteorder determined by the | |
2558 | * DRR_OBJECT_RANGE record. | |
2559 | */ | |
9b840763 | 2560 | if (rwa->byteswap && !rwa->raw) { |
9ae529ec CS |
2561 | dmu_object_byteswap_t byteswap = |
2562 | DMU_OT_BYTESWAP(drro->drr_bonustype); | |
2563 | dmu_ot_byteswap[byteswap].ob_func(db->db_data, | |
b5256303 | 2564 | DRR_OBJECT_PAYLOAD_SIZE(drro)); |
34dc7c2f BB |
2565 | } |
2566 | dmu_buf_rele(db, FTAG); | |
2567 | } | |
2568 | dmu_tx_commit(tx); | |
47dfff3b | 2569 | |
34dc7c2f BB |
2570 | return (0); |
2571 | } | |
2572 | ||
2573 | /* ARGSUSED */ | |
60948de1 | 2574 | noinline static int |
fcff0f35 | 2575 | receive_freeobjects(struct receive_writer_arg *rwa, |
34dc7c2f BB |
2576 | struct drr_freeobjects *drrfo) |
2577 | { | |
2578 | uint64_t obj; | |
e6d3a843 | 2579 | int next_err = 0; |
34dc7c2f BB |
2580 | |
2581 | if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj) | |
2e528b49 | 2582 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 2583 | |
50c957f7 | 2584 | for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj; |
e6d3a843 PD |
2585 | obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0; |
2586 | next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) { | |
50c957f7 | 2587 | dmu_object_info_t doi; |
34dc7c2f BB |
2588 | int err; |
2589 | ||
50c957f7 | 2590 | err = dmu_object_info(rwa->os, obj, &doi); |
39f56627 | 2591 | if (err == ENOENT) |
34dc7c2f | 2592 | continue; |
39f56627 | 2593 | else if (err != 0) |
50c957f7 | 2594 | return (err); |
34dc7c2f | 2595 | |
440a3eb9 TC |
2596 | if (rwa->raw) |
2597 | err = dmu_free_long_object_raw(rwa->os, obj); | |
2598 | else | |
2599 | err = dmu_free_long_object(rwa->os, obj); | |
2600 | ||
13fe0198 | 2601 | if (err != 0) |
34dc7c2f | 2602 | return (err); |
48fbb9dd FG |
2603 | |
2604 | if (obj > rwa->max_object) | |
2605 | rwa->max_object = obj; | |
34dc7c2f | 2606 | } |
e6d3a843 PD |
2607 | if (next_err != ESRCH) |
2608 | return (next_err); | |
34dc7c2f BB |
2609 | return (0); |
2610 | } | |
2611 | ||
60948de1 | 2612 | noinline static int |
fcff0f35 | 2613 | receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw, |
4ea3f864 | 2614 | arc_buf_t *abuf) |
34dc7c2f | 2615 | { |
34dc7c2f | 2616 | int err; |
440a3eb9 TC |
2617 | dmu_tx_t *tx; |
2618 | dnode_t *dn; | |
34dc7c2f | 2619 | |
2aa34383 | 2620 | if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset || |
9ae529ec | 2621 | !DMU_OT_IS_VALID(drrw->drr_type)) |
2e528b49 | 2622 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 2623 | |
47dfff3b MA |
2624 | /* |
2625 | * For resuming to work, records must be in increasing order | |
2626 | * by (object, offset). | |
2627 | */ | |
2628 | if (drrw->drr_object < rwa->last_object || | |
2629 | (drrw->drr_object == rwa->last_object && | |
2630 | drrw->drr_offset < rwa->last_offset)) { | |
2631 | return (SET_ERROR(EINVAL)); | |
2632 | } | |
2633 | rwa->last_object = drrw->drr_object; | |
2634 | rwa->last_offset = drrw->drr_offset; | |
2635 | ||
48fbb9dd FG |
2636 | if (rwa->last_object > rwa->max_object) |
2637 | rwa->max_object = rwa->last_object; | |
2638 | ||
fcff0f35 | 2639 | if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0) |
88904bb3 MA |
2640 | return (SET_ERROR(EINVAL)); |
2641 | ||
fcff0f35 | 2642 | tx = dmu_tx_create(rwa->os); |
34dc7c2f | 2643 | dmu_tx_hold_write(tx, drrw->drr_object, |
2aa34383 | 2644 | drrw->drr_offset, drrw->drr_logical_size); |
34dc7c2f | 2645 | err = dmu_tx_assign(tx, TXG_WAIT); |
13fe0198 | 2646 | if (err != 0) { |
34dc7c2f BB |
2647 | dmu_tx_abort(tx); |
2648 | return (err); | |
2649 | } | |
9b840763 TC |
2650 | |
2651 | if (rwa->raw) | |
2652 | VERIFY0(dmu_object_dirty_raw(rwa->os, drrw->drr_object, tx)); | |
2653 | ||
b5256303 TC |
2654 | if (rwa->byteswap && !arc_is_encrypted(abuf) && |
2655 | arc_get_compression(abuf) == ZIO_COMPRESS_OFF) { | |
9ae529ec CS |
2656 | dmu_object_byteswap_t byteswap = |
2657 | DMU_OT_BYTESWAP(drrw->drr_type); | |
37f8a883 | 2658 | dmu_ot_byteswap[byteswap].ob_func(abuf->b_data, |
2aa34383 | 2659 | DRR_WRITE_PAYLOAD_SIZE(drrw)); |
9ae529ec | 2660 | } |
37f8a883 | 2661 | |
440a3eb9 TC |
2662 | VERIFY0(dnode_hold(rwa->os, drrw->drr_object, FTAG, &dn)); |
2663 | dmu_assign_arcbuf_by_dnode(dn, drrw->drr_offset, abuf, tx); | |
2664 | dnode_rele(dn, FTAG); | |
47dfff3b MA |
2665 | |
2666 | /* | |
2667 | * Note: If the receive fails, we want the resume stream to start | |
2668 | * with the same record that we last successfully received (as opposed | |
2669 | * to the next record), so that we can verify that we are | |
2670 | * resuming from the correct location. | |
2671 | */ | |
2672 | save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx); | |
34dc7c2f | 2673 | dmu_tx_commit(tx); |
47dfff3b | 2674 | |
34dc7c2f BB |
2675 | return (0); |
2676 | } | |
2677 | ||
428870ff BB |
2678 | /* |
2679 | * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed | |
2680 | * streams to refer to a copy of the data that is already on the | |
2681 | * system because it came in earlier in the stream. This function | |
2682 | * finds the earlier copy of the data, and uses that copy instead of | |
2683 | * data from the stream to fulfill this write. | |
2684 | */ | |
2685 | static int | |
fcff0f35 PD |
2686 | receive_write_byref(struct receive_writer_arg *rwa, |
2687 | struct drr_write_byref *drrwbr) | |
428870ff BB |
2688 | { |
2689 | dmu_tx_t *tx; | |
2690 | int err; | |
2691 | guid_map_entry_t gmesrch; | |
2692 | guid_map_entry_t *gmep; | |
9b67f605 | 2693 | avl_index_t where; |
428870ff | 2694 | objset_t *ref_os = NULL; |
b5256303 | 2695 | int flags = DMU_READ_PREFETCH; |
428870ff BB |
2696 | dmu_buf_t *dbp; |
2697 | ||
2698 | if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset) | |
2e528b49 | 2699 | return (SET_ERROR(EINVAL)); |
428870ff BB |
2700 | |
2701 | /* | |
2702 | * If the GUID of the referenced dataset is different from the | |
2703 | * GUID of the target dataset, find the referenced dataset. | |
2704 | */ | |
2705 | if (drrwbr->drr_toguid != drrwbr->drr_refguid) { | |
2706 | gmesrch.guid = drrwbr->drr_refguid; | |
fcff0f35 | 2707 | if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch, |
428870ff | 2708 | &where)) == NULL) { |
2e528b49 | 2709 | return (SET_ERROR(EINVAL)); |
428870ff BB |
2710 | } |
2711 | if (dmu_objset_from_ds(gmep->gme_ds, &ref_os)) | |
2e528b49 | 2712 | return (SET_ERROR(EINVAL)); |
428870ff | 2713 | } else { |
fcff0f35 | 2714 | ref_os = rwa->os; |
428870ff BB |
2715 | } |
2716 | ||
48fbb9dd FG |
2717 | if (drrwbr->drr_object > rwa->max_object) |
2718 | rwa->max_object = drrwbr->drr_object; | |
2719 | ||
9b840763 | 2720 | if (rwa->raw) |
b5256303 | 2721 | flags |= DMU_READ_NO_DECRYPT; |
b5256303 TC |
2722 | |
2723 | /* may return either a regular db or an encrypted one */ | |
c65aa5b2 | 2724 | err = dmu_buf_hold(ref_os, drrwbr->drr_refobject, |
b5256303 | 2725 | drrwbr->drr_refoffset, FTAG, &dbp, flags); |
9b67f605 | 2726 | if (err != 0) |
428870ff BB |
2727 | return (err); |
2728 | ||
fcff0f35 | 2729 | tx = dmu_tx_create(rwa->os); |
428870ff BB |
2730 | |
2731 | dmu_tx_hold_write(tx, drrwbr->drr_object, | |
2732 | drrwbr->drr_offset, drrwbr->drr_length); | |
2733 | err = dmu_tx_assign(tx, TXG_WAIT); | |
13fe0198 | 2734 | if (err != 0) { |
428870ff BB |
2735 | dmu_tx_abort(tx); |
2736 | return (err); | |
2737 | } | |
b5256303 | 2738 | |
9b840763 TC |
2739 | if (rwa->raw) { |
2740 | VERIFY0(dmu_object_dirty_raw(rwa->os, drrwbr->drr_object, tx)); | |
b5256303 TC |
2741 | dmu_copy_from_buf(rwa->os, drrwbr->drr_object, |
2742 | drrwbr->drr_offset, dbp, tx); | |
2743 | } else { | |
2744 | dmu_write(rwa->os, drrwbr->drr_object, | |
2745 | drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx); | |
2746 | } | |
428870ff | 2747 | dmu_buf_rele(dbp, FTAG); |
47dfff3b MA |
2748 | |
2749 | /* See comment in restore_write. */ | |
2750 | save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx); | |
428870ff BB |
2751 | dmu_tx_commit(tx); |
2752 | return (0); | |
2753 | } | |
2754 | ||
9b67f605 | 2755 | static int |
fcff0f35 | 2756 | receive_write_embedded(struct receive_writer_arg *rwa, |
47dfff3b | 2757 | struct drr_write_embedded *drrwe, void *data) |
9b67f605 MA |
2758 | { |
2759 | dmu_tx_t *tx; | |
2760 | int err; | |
9b67f605 | 2761 | |
47dfff3b | 2762 | if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset) |
ecb2b7dc | 2763 | return (SET_ERROR(EINVAL)); |
9b67f605 | 2764 | |
47dfff3b | 2765 | if (drrwe->drr_psize > BPE_PAYLOAD_SIZE) |
ecb2b7dc | 2766 | return (SET_ERROR(EINVAL)); |
9b67f605 | 2767 | |
47dfff3b | 2768 | if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES) |
ecb2b7dc | 2769 | return (SET_ERROR(EINVAL)); |
47dfff3b | 2770 | if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS) |
ecb2b7dc | 2771 | return (SET_ERROR(EINVAL)); |
440a3eb9 TC |
2772 | if (rwa->raw) |
2773 | return (SET_ERROR(EINVAL)); | |
9b67f605 | 2774 | |
48fbb9dd FG |
2775 | if (drrwe->drr_object > rwa->max_object) |
2776 | rwa->max_object = drrwe->drr_object; | |
2777 | ||
fcff0f35 | 2778 | tx = dmu_tx_create(rwa->os); |
9b67f605 | 2779 | |
47dfff3b MA |
2780 | dmu_tx_hold_write(tx, drrwe->drr_object, |
2781 | drrwe->drr_offset, drrwe->drr_length); | |
9b67f605 MA |
2782 | err = dmu_tx_assign(tx, TXG_WAIT); |
2783 | if (err != 0) { | |
2784 | dmu_tx_abort(tx); | |
2785 | return (err); | |
2786 | } | |
2787 | ||
47dfff3b MA |
2788 | dmu_write_embedded(rwa->os, drrwe->drr_object, |
2789 | drrwe->drr_offset, data, drrwe->drr_etype, | |
2790 | drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize, | |
fcff0f35 | 2791 | rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx); |
9b67f605 | 2792 | |
47dfff3b MA |
2793 | /* See comment in restore_write. */ |
2794 | save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx); | |
9b67f605 MA |
2795 | dmu_tx_commit(tx); |
2796 | return (0); | |
2797 | } | |
2798 | ||
428870ff | 2799 | static int |
fcff0f35 | 2800 | receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs, |
b5256303 | 2801 | arc_buf_t *abuf) |
428870ff BB |
2802 | { |
2803 | dmu_tx_t *tx; | |
428870ff BB |
2804 | dmu_buf_t *db, *db_spill; |
2805 | int err; | |
2806 | ||
2807 | if (drrs->drr_length < SPA_MINBLOCKSIZE || | |
fcff0f35 | 2808 | drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os))) |
2e528b49 | 2809 | return (SET_ERROR(EINVAL)); |
428870ff | 2810 | |
9b840763 | 2811 | if (rwa->raw) { |
b5256303 TC |
2812 | if (!DMU_OT_IS_VALID(drrs->drr_type) || |
2813 | drrs->drr_compressiontype >= ZIO_COMPRESS_FUNCTIONS || | |
2814 | drrs->drr_compressed_size == 0) | |
2815 | return (SET_ERROR(EINVAL)); | |
2816 | } | |
2817 | ||
fcff0f35 | 2818 | if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0) |
2e528b49 | 2819 | return (SET_ERROR(EINVAL)); |
428870ff | 2820 | |
48fbb9dd FG |
2821 | if (drrs->drr_object > rwa->max_object) |
2822 | rwa->max_object = drrs->drr_object; | |
2823 | ||
fcff0f35 | 2824 | VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db)); |
428870ff BB |
2825 | if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) { |
2826 | dmu_buf_rele(db, FTAG); | |
2827 | return (err); | |
2828 | } | |
2829 | ||
fcff0f35 | 2830 | tx = dmu_tx_create(rwa->os); |
428870ff BB |
2831 | |
2832 | dmu_tx_hold_spill(tx, db->db_object); | |
2833 | ||
2834 | err = dmu_tx_assign(tx, TXG_WAIT); | |
13fe0198 | 2835 | if (err != 0) { |
428870ff BB |
2836 | dmu_buf_rele(db, FTAG); |
2837 | dmu_buf_rele(db_spill, FTAG); | |
2838 | dmu_tx_abort(tx); | |
2839 | return (err); | |
2840 | } | |
2841 | dmu_buf_will_dirty(db_spill, tx); | |
9b840763 TC |
2842 | if (rwa->raw) |
2843 | VERIFY0(dmu_object_dirty_raw(rwa->os, drrs->drr_object, tx)); | |
428870ff BB |
2844 | |
2845 | if (db_spill->db_size < drrs->drr_length) | |
2846 | VERIFY(0 == dbuf_spill_set_blksz(db_spill, | |
2847 | drrs->drr_length, tx)); | |
440a3eb9 | 2848 | dbuf_assign_arcbuf((dmu_buf_impl_t *)db_spill, abuf, tx); |
428870ff BB |
2849 | |
2850 | dmu_buf_rele(db, FTAG); | |
2851 | dmu_buf_rele(db_spill, FTAG); | |
2852 | ||
2853 | dmu_tx_commit(tx); | |
2854 | return (0); | |
2855 | } | |
2856 | ||
34dc7c2f | 2857 | /* ARGSUSED */ |
60948de1 | 2858 | noinline static int |
fcff0f35 | 2859 | receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf) |
34dc7c2f | 2860 | { |
34dc7c2f BB |
2861 | int err; |
2862 | ||
2863 | if (drrf->drr_length != -1ULL && | |
2864 | drrf->drr_offset + drrf->drr_length < drrf->drr_offset) | |
2e528b49 | 2865 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 2866 | |
fcff0f35 | 2867 | if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0) |
2e528b49 | 2868 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 2869 | |
48fbb9dd FG |
2870 | if (drrf->drr_object > rwa->max_object) |
2871 | rwa->max_object = drrf->drr_object; | |
2872 | ||
440a3eb9 TC |
2873 | if (rwa->raw) { |
2874 | err = dmu_free_long_range_raw(rwa->os, drrf->drr_object, | |
2875 | drrf->drr_offset, drrf->drr_length); | |
2876 | } else { | |
2877 | err = dmu_free_long_range(rwa->os, drrf->drr_object, | |
2878 | drrf->drr_offset, drrf->drr_length); | |
2879 | } | |
fcff0f35 | 2880 | |
34dc7c2f BB |
2881 | return (err); |
2882 | } | |
2883 | ||
b5256303 TC |
2884 | static int |
2885 | receive_object_range(struct receive_writer_arg *rwa, | |
2886 | struct drr_object_range *drror) | |
2887 | { | |
2888 | int ret; | |
2889 | dmu_tx_t *tx; | |
2890 | dnode_t *mdn = NULL; | |
2891 | dmu_buf_t *db = NULL; | |
2892 | uint64_t offset; | |
2893 | ||
2894 | /* | |
2895 | * By default, we assume this block is in our native format | |
2896 | * (ZFS_HOST_BYTEORDER). We then take into account whether | |
2897 | * the send stream is byteswapped (rwa->byteswap). Finally, | |
2898 | * we need to byteswap again if this particular block was | |
2899 | * in non-native format on the send side. | |
2900 | */ | |
2901 | boolean_t byteorder = ZFS_HOST_BYTEORDER ^ rwa->byteswap ^ | |
2902 | !!DRR_IS_RAW_BYTESWAPPED(drror->drr_flags); | |
2903 | ||
2904 | /* | |
2905 | * Since dnode block sizes are constant, we should not need to worry | |
2906 | * about making sure that the dnode block size is the same on the | |
2907 | * sending and receiving sides for the time being. For non-raw sends, | |
2908 | * this does not matter (and in fact we do not send a DRR_OBJECT_RANGE | |
2909 | * record at all). Raw sends require this record type because the | |
2910 | * encryption parameters are used to protect an entire block of bonus | |
2911 | * buffers. If the size of dnode blocks ever becomes variable, | |
2912 | * handling will need to be added to ensure that dnode block sizes | |
2913 | * match on the sending and receiving side. | |
2914 | */ | |
2915 | if (drror->drr_numslots != DNODES_PER_BLOCK || | |
2916 | P2PHASE(drror->drr_firstobj, DNODES_PER_BLOCK) != 0 || | |
9b840763 | 2917 | !rwa->raw) |
b5256303 TC |
2918 | return (SET_ERROR(EINVAL)); |
2919 | ||
48fbb9dd FG |
2920 | if (drror->drr_firstobj > rwa->max_object) |
2921 | rwa->max_object = drror->drr_firstobj; | |
2922 | ||
b5256303 TC |
2923 | offset = drror->drr_firstobj * sizeof (dnode_phys_t); |
2924 | mdn = DMU_META_DNODE(rwa->os); | |
2925 | ||
2926 | tx = dmu_tx_create(rwa->os); | |
2927 | ret = dmu_tx_assign(tx, TXG_WAIT); | |
2928 | if (ret != 0) { | |
2929 | dmu_tx_abort(tx); | |
2930 | return (ret); | |
2931 | } | |
2932 | ||
2933 | ret = dmu_buf_hold_by_dnode(mdn, offset, FTAG, &db, | |
2934 | DMU_READ_PREFETCH | DMU_READ_NO_DECRYPT); | |
2935 | if (ret != 0) { | |
2936 | dmu_tx_commit(tx); | |
2937 | return (ret); | |
2938 | } | |
2939 | ||
2940 | /* | |
2941 | * Convert the buffer associated with this range of dnodes to a | |
2942 | * raw buffer. This ensures that it will be written out as a raw | |
2943 | * buffer when we fill in the dnode objects in future records. | |
2944 | * Since we are commiting this tx now, it is technically possible | |
2945 | * for the dnode block to end up on-disk with the incorrect MAC. | |
2946 | * Despite this, the dataset is marked as inconsistent so no other | |
2947 | * code paths (apart from scrubs) will attempt to read this data. | |
2948 | * Scrubs will not be effected by this either since scrubs only | |
2949 | * read raw data and do not attempt to check the MAC. | |
2950 | */ | |
2951 | dmu_convert_to_raw(db, byteorder, drror->drr_salt, drror->drr_iv, | |
2952 | drror->drr_mac, tx); | |
2953 | dmu_buf_rele(db, FTAG); | |
2954 | dmu_tx_commit(tx); | |
2955 | return (0); | |
2956 | } | |
2957 | ||
13fe0198 MA |
2958 | /* used to destroy the drc_ds on error */ |
2959 | static void | |
2960 | dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc) | |
2961 | { | |
2637dda8 | 2962 | dsl_dataset_t *ds = drc->drc_ds; |
b5256303 TC |
2963 | ds_hold_flags_t dsflags = (drc->drc_raw) ? 0 : DS_HOLD_FLAG_DECRYPT; |
2964 | ||
2965 | /* | |
2966 | * Wait for the txg sync before cleaning up the receive. For | |
2967 | * resumable receives, this ensures that our resume state has | |
2968 | * been written out to disk. For raw receives, this ensures | |
2969 | * that the user accounting code will not attempt to do anything | |
2970 | * after we stopped receiving the dataset. | |
2971 | */ | |
2637dda8 | 2972 | txg_wait_synced(ds->ds_dir->dd_pool, 0); |
b5256303 | 2973 | |
2637dda8 TC |
2974 | rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG); |
2975 | if (drc->drc_resumable && !BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) { | |
2976 | rrw_exit(&ds->ds_bp_rwlock, FTAG); | |
2977 | dsl_dataset_disown(ds, dsflags, dmu_recv_tag); | |
47dfff3b | 2978 | } else { |
eca7b760 | 2979 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
2637dda8 TC |
2980 | rrw_exit(&ds->ds_bp_rwlock, FTAG); |
2981 | dsl_dataset_name(ds, name); | |
2982 | dsl_dataset_disown(ds, dsflags, dmu_recv_tag); | |
47dfff3b MA |
2983 | (void) dsl_destroy_head(name); |
2984 | } | |
13fe0198 MA |
2985 | } |
2986 | ||
37f8a883 | 2987 | static void |
fcff0f35 | 2988 | receive_cksum(struct receive_arg *ra, int len, void *buf) |
37f8a883 MA |
2989 | { |
2990 | if (ra->byteswap) { | |
a6255b7f | 2991 | (void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum); |
37f8a883 | 2992 | } else { |
a6255b7f | 2993 | (void) fletcher_4_incremental_native(buf, len, &ra->cksum); |
37f8a883 MA |
2994 | } |
2995 | } | |
2996 | ||
2997 | /* | |
fcff0f35 PD |
2998 | * Read the payload into a buffer of size len, and update the current record's |
2999 | * payload field. | |
3000 | * Allocate ra->next_rrd and read the next record's header into | |
3001 | * ra->next_rrd->header. | |
37f8a883 MA |
3002 | * Verify checksum of payload and next record. |
3003 | */ | |
3004 | static int | |
fcff0f35 | 3005 | receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf) |
37f8a883 MA |
3006 | { |
3007 | int err; | |
3008 | zio_cksum_t cksum_orig; | |
3009 | zio_cksum_t *cksump; | |
3010 | ||
3011 | if (len != 0) { | |
fcff0f35 | 3012 | ASSERT3U(len, <=, SPA_MAXBLOCKSIZE); |
47dfff3b | 3013 | err = receive_read(ra, len, buf); |
37f8a883 MA |
3014 | if (err != 0) |
3015 | return (err); | |
47dfff3b MA |
3016 | receive_cksum(ra, len, buf); |
3017 | ||
3018 | /* note: rrd is NULL when reading the begin record's payload */ | |
3019 | if (ra->rrd != NULL) { | |
3020 | ra->rrd->payload = buf; | |
3021 | ra->rrd->payload_size = len; | |
3022 | ra->rrd->bytes_read = ra->bytes_read; | |
3023 | } | |
37f8a883 MA |
3024 | } |
3025 | ||
3026 | ra->prev_cksum = ra->cksum; | |
3027 | ||
fcff0f35 PD |
3028 | ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP); |
3029 | err = receive_read(ra, sizeof (ra->next_rrd->header), | |
3030 | &ra->next_rrd->header); | |
47dfff3b | 3031 | ra->next_rrd->bytes_read = ra->bytes_read; |
b5256303 | 3032 | |
fcff0f35 PD |
3033 | if (err != 0) { |
3034 | kmem_free(ra->next_rrd, sizeof (*ra->next_rrd)); | |
3035 | ra->next_rrd = NULL; | |
37f8a883 | 3036 | return (err); |
fcff0f35 PD |
3037 | } |
3038 | if (ra->next_rrd->header.drr_type == DRR_BEGIN) { | |
3039 | kmem_free(ra->next_rrd, sizeof (*ra->next_rrd)); | |
3040 | ra->next_rrd = NULL; | |
37f8a883 | 3041 | return (SET_ERROR(EINVAL)); |
fcff0f35 | 3042 | } |
37f8a883 MA |
3043 | |
3044 | /* | |
3045 | * Note: checksum is of everything up to but not including the | |
3046 | * checksum itself. | |
3047 | */ | |
3048 | ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), | |
3049 | ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t)); | |
fcff0f35 | 3050 | receive_cksum(ra, |
37f8a883 | 3051 | offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), |
fcff0f35 | 3052 | &ra->next_rrd->header); |
37f8a883 | 3053 | |
fcff0f35 PD |
3054 | cksum_orig = ra->next_rrd->header.drr_u.drr_checksum.drr_checksum; |
3055 | cksump = &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum; | |
37f8a883 MA |
3056 | |
3057 | if (ra->byteswap) | |
fcff0f35 | 3058 | byteswap_record(&ra->next_rrd->header); |
37f8a883 MA |
3059 | |
3060 | if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) && | |
fcff0f35 PD |
3061 | !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) { |
3062 | kmem_free(ra->next_rrd, sizeof (*ra->next_rrd)); | |
3063 | ra->next_rrd = NULL; | |
37f8a883 | 3064 | return (SET_ERROR(ECKSUM)); |
fcff0f35 | 3065 | } |
37f8a883 | 3066 | |
fcff0f35 | 3067 | receive_cksum(ra, sizeof (cksum_orig), &cksum_orig); |
37f8a883 MA |
3068 | |
3069 | return (0); | |
3070 | } | |
3071 | ||
e6d3a843 PD |
3072 | static void |
3073 | objlist_create(struct objlist *list) | |
3074 | { | |
3075 | list_create(&list->list, sizeof (struct receive_objnode), | |
3076 | offsetof(struct receive_objnode, node)); | |
3077 | list->last_lookup = 0; | |
3078 | } | |
3079 | ||
3080 | static void | |
3081 | objlist_destroy(struct objlist *list) | |
3082 | { | |
3083 | struct receive_objnode *n; | |
3084 | ||
3085 | for (n = list_remove_head(&list->list); | |
3086 | n != NULL; n = list_remove_head(&list->list)) { | |
3087 | kmem_free(n, sizeof (*n)); | |
3088 | } | |
3089 | list_destroy(&list->list); | |
3090 | } | |
3091 | ||
3092 | /* | |
3093 | * This function looks through the objlist to see if the specified object number | |
3094 | * is contained in the objlist. In the process, it will remove all object | |
3095 | * numbers in the list that are smaller than the specified object number. Thus, | |
3096 | * any lookup of an object number smaller than a previously looked up object | |
3097 | * number will always return false; therefore, all lookups should be done in | |
3098 | * ascending order. | |
3099 | */ | |
3100 | static boolean_t | |
3101 | objlist_exists(struct objlist *list, uint64_t object) | |
3102 | { | |
3103 | struct receive_objnode *node = list_head(&list->list); | |
3104 | ASSERT3U(object, >=, list->last_lookup); | |
3105 | list->last_lookup = object; | |
3106 | while (node != NULL && node->object < object) { | |
3107 | VERIFY3P(node, ==, list_remove_head(&list->list)); | |
3108 | kmem_free(node, sizeof (*node)); | |
3109 | node = list_head(&list->list); | |
3110 | } | |
3111 | return (node != NULL && node->object == object); | |
3112 | } | |
3113 | ||
3114 | /* | |
3115 | * The objlist is a list of object numbers stored in ascending order. However, | |
3116 | * the insertion of new object numbers does not seek out the correct location to | |
3117 | * store a new object number; instead, it appends it to the list for simplicity. | |
3118 | * Thus, any users must take care to only insert new object numbers in ascending | |
3119 | * order. | |
3120 | */ | |
3121 | static void | |
3122 | objlist_insert(struct objlist *list, uint64_t object) | |
3123 | { | |
3124 | struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP); | |
3125 | node->object = object; | |
3126 | #ifdef ZFS_DEBUG | |
3127 | { | |
3128 | struct receive_objnode *last_object = list_tail(&list->list); | |
3129 | uint64_t last_objnum = (last_object != NULL ? last_object->object : 0); | |
3130 | ASSERT3U(node->object, >, last_objnum); | |
3131 | } | |
3132 | #endif | |
3133 | list_insert_tail(&list->list, node); | |
3134 | } | |
3135 | ||
fcff0f35 PD |
3136 | /* |
3137 | * Issue the prefetch reads for any necessary indirect blocks. | |
3138 | * | |
3139 | * We use the object ignore list to tell us whether or not to issue prefetches | |
3140 | * for a given object. We do this for both correctness (in case the blocksize | |
3141 | * of an object has changed) and performance (if the object doesn't exist, don't | |
3142 | * needlessly try to issue prefetches). We also trim the list as we go through | |
3143 | * the stream to prevent it from growing to an unbounded size. | |
3144 | * | |
3145 | * The object numbers within will always be in sorted order, and any write | |
3146 | * records we see will also be in sorted order, but they're not sorted with | |
3147 | * respect to each other (i.e. we can get several object records before | |
3148 | * receiving each object's write records). As a result, once we've reached a | |
3149 | * given object number, we can safely remove any reference to lower object | |
3150 | * numbers in the ignore list. In practice, we receive up to 32 object records | |
3151 | * before receiving write records, so the list can have up to 32 nodes in it. | |
3152 | */ | |
3153 | /* ARGSUSED */ | |
3154 | static void | |
3155 | receive_read_prefetch(struct receive_arg *ra, | |
3156 | uint64_t object, uint64_t offset, uint64_t length) | |
3157 | { | |
e6d3a843 | 3158 | if (!objlist_exists(&ra->ignore_objlist, object)) { |
fcff0f35 PD |
3159 | dmu_prefetch(ra->os, object, 1, offset, length, |
3160 | ZIO_PRIORITY_SYNC_READ); | |
3161 | } | |
3162 | } | |
3163 | ||
3164 | /* | |
3165 | * Read records off the stream, issuing any necessary prefetches. | |
3166 | */ | |
37f8a883 | 3167 | static int |
fcff0f35 | 3168 | receive_read_record(struct receive_arg *ra) |
37f8a883 MA |
3169 | { |
3170 | int err; | |
3171 | ||
fcff0f35 | 3172 | switch (ra->rrd->header.drr_type) { |
37f8a883 MA |
3173 | case DRR_OBJECT: |
3174 | { | |
fcff0f35 | 3175 | struct drr_object *drro = &ra->rrd->header.drr_u.drr_object; |
b5256303 | 3176 | uint32_t size = DRR_OBJECT_PAYLOAD_SIZE(drro); |
fcff0f35 PD |
3177 | void *buf = kmem_zalloc(size, KM_SLEEP); |
3178 | dmu_object_info_t doi; | |
b5256303 | 3179 | |
fcff0f35 PD |
3180 | err = receive_read_payload_and_next_header(ra, size, buf); |
3181 | if (err != 0) { | |
3182 | kmem_free(buf, size); | |
37f8a883 | 3183 | return (err); |
fcff0f35 PD |
3184 | } |
3185 | err = dmu_object_info(ra->os, drro->drr_object, &doi); | |
3186 | /* | |
3187 | * See receive_read_prefetch for an explanation why we're | |
3188 | * storing this object in the ignore_obj_list. | |
3189 | */ | |
3190 | if (err == ENOENT || | |
3191 | (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) { | |
e6d3a843 | 3192 | objlist_insert(&ra->ignore_objlist, drro->drr_object); |
fcff0f35 PD |
3193 | err = 0; |
3194 | } | |
3195 | return (err); | |
37f8a883 MA |
3196 | } |
3197 | case DRR_FREEOBJECTS: | |
3198 | { | |
fcff0f35 PD |
3199 | err = receive_read_payload_and_next_header(ra, 0, NULL); |
3200 | return (err); | |
37f8a883 MA |
3201 | } |
3202 | case DRR_WRITE: | |
3203 | { | |
fcff0f35 | 3204 | struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write; |
2aa34383 DK |
3205 | arc_buf_t *abuf; |
3206 | boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type); | |
b5256303 | 3207 | |
9b840763 | 3208 | if (ra->raw) { |
b5256303 TC |
3209 | boolean_t byteorder = ZFS_HOST_BYTEORDER ^ |
3210 | !!DRR_IS_RAW_BYTESWAPPED(drrw->drr_flags) ^ | |
3211 | ra->byteswap; | |
3212 | ||
3213 | abuf = arc_loan_raw_buf(dmu_objset_spa(ra->os), | |
3214 | drrw->drr_object, byteorder, drrw->drr_salt, | |
3215 | drrw->drr_iv, drrw->drr_mac, drrw->drr_type, | |
3216 | drrw->drr_compressed_size, drrw->drr_logical_size, | |
3217 | drrw->drr_compressiontype); | |
3218 | } else if (DRR_WRITE_COMPRESSED(drrw)) { | |
2aa34383 DK |
3219 | ASSERT3U(drrw->drr_compressed_size, >, 0); |
3220 | ASSERT3U(drrw->drr_logical_size, >=, | |
3221 | drrw->drr_compressed_size); | |
3222 | ASSERT(!is_meta); | |
3223 | abuf = arc_loan_compressed_buf( | |
3224 | dmu_objset_spa(ra->os), | |
3225 | drrw->drr_compressed_size, drrw->drr_logical_size, | |
3226 | drrw->drr_compressiontype); | |
3227 | } else { | |
3228 | abuf = arc_loan_buf(dmu_objset_spa(ra->os), | |
3229 | is_meta, drrw->drr_logical_size); | |
3230 | } | |
37f8a883 | 3231 | |
fcff0f35 | 3232 | err = receive_read_payload_and_next_header(ra, |
2aa34383 | 3233 | DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data); |
fcff0f35 | 3234 | if (err != 0) { |
37f8a883 | 3235 | dmu_return_arcbuf(abuf); |
fcff0f35 PD |
3236 | return (err); |
3237 | } | |
b5256303 | 3238 | ra->rrd->arc_buf = abuf; |
fcff0f35 | 3239 | receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset, |
2aa34383 | 3240 | drrw->drr_logical_size); |
37f8a883 MA |
3241 | return (err); |
3242 | } | |
3243 | case DRR_WRITE_BYREF: | |
3244 | { | |
fcff0f35 PD |
3245 | struct drr_write_byref *drrwb = |
3246 | &ra->rrd->header.drr_u.drr_write_byref; | |
3247 | err = receive_read_payload_and_next_header(ra, 0, NULL); | |
3248 | receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset, | |
3249 | drrwb->drr_length); | |
3250 | return (err); | |
37f8a883 MA |
3251 | } |
3252 | case DRR_WRITE_EMBEDDED: | |
3253 | { | |
3254 | struct drr_write_embedded *drrwe = | |
fcff0f35 PD |
3255 | &ra->rrd->header.drr_u.drr_write_embedded; |
3256 | uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8); | |
3257 | void *buf = kmem_zalloc(size, KM_SLEEP); | |
3258 | ||
3259 | err = receive_read_payload_and_next_header(ra, size, buf); | |
3260 | if (err != 0) { | |
3261 | kmem_free(buf, size); | |
37f8a883 | 3262 | return (err); |
fcff0f35 PD |
3263 | } |
3264 | ||
3265 | receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset, | |
3266 | drrwe->drr_length); | |
3267 | return (err); | |
37f8a883 MA |
3268 | } |
3269 | case DRR_FREE: | |
3270 | { | |
fcff0f35 PD |
3271 | /* |
3272 | * It might be beneficial to prefetch indirect blocks here, but | |
3273 | * we don't really have the data to decide for sure. | |
3274 | */ | |
3275 | err = receive_read_payload_and_next_header(ra, 0, NULL); | |
3276 | return (err); | |
37f8a883 MA |
3277 | } |
3278 | case DRR_END: | |
3279 | { | |
fcff0f35 | 3280 | struct drr_end *drre = &ra->rrd->header.drr_u.drr_end; |
37f8a883 | 3281 | if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum)) |
47dfff3b | 3282 | return (SET_ERROR(ECKSUM)); |
37f8a883 MA |
3283 | return (0); |
3284 | } | |
3285 | case DRR_SPILL: | |
3286 | { | |
fcff0f35 | 3287 | struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill; |
b5256303 TC |
3288 | arc_buf_t *abuf; |
3289 | int len = DRR_SPILL_PAYLOAD_SIZE(drrs); | |
3290 | ||
3291 | /* DRR_SPILL records are either raw or uncompressed */ | |
9b840763 | 3292 | if (ra->raw) { |
b5256303 TC |
3293 | boolean_t byteorder = ZFS_HOST_BYTEORDER ^ |
3294 | !!DRR_IS_RAW_BYTESWAPPED(drrs->drr_flags) ^ | |
3295 | ra->byteswap; | |
3296 | ||
3297 | abuf = arc_loan_raw_buf(dmu_objset_spa(ra->os), | |
3298 | drrs->drr_object, byteorder, drrs->drr_salt, | |
3299 | drrs->drr_iv, drrs->drr_mac, drrs->drr_type, | |
3300 | drrs->drr_compressed_size, drrs->drr_length, | |
3301 | drrs->drr_compressiontype); | |
3302 | } else { | |
3303 | abuf = arc_loan_buf(dmu_objset_spa(ra->os), | |
3304 | DMU_OT_IS_METADATA(drrs->drr_type), | |
3305 | drrs->drr_length); | |
3306 | } | |
3307 | ||
3308 | err = receive_read_payload_and_next_header(ra, len, | |
3309 | abuf->b_data); | |
3310 | if (err != 0) { | |
3311 | dmu_return_arcbuf(abuf); | |
3312 | return (err); | |
3313 | } | |
3314 | ra->rrd->arc_buf = abuf; | |
3315 | return (err); | |
3316 | } | |
3317 | case DRR_OBJECT_RANGE: | |
3318 | { | |
3319 | err = receive_read_payload_and_next_header(ra, 0, NULL); | |
fcff0f35 PD |
3320 | return (err); |
3321 | } | |
3322 | default: | |
3323 | return (SET_ERROR(EINVAL)); | |
3324 | } | |
3325 | } | |
3326 | ||
6a8ee4f7 NB |
3327 | static void |
3328 | dprintf_drr(struct receive_record_arg *rrd, int err) | |
3329 | { | |
3330 | switch (rrd->header.drr_type) { | |
3331 | case DRR_OBJECT: | |
3332 | { | |
3333 | struct drr_object *drro = &rrd->header.drr_u.drr_object; | |
3334 | dprintf("drr_type = OBJECT obj = %llu type = %u " | |
3335 | "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u " | |
3336 | "compress = %u dn_slots = %u err = %d\n", | |
3337 | drro->drr_object, drro->drr_type, drro->drr_bonustype, | |
3338 | drro->drr_blksz, drro->drr_bonuslen, | |
3339 | drro->drr_checksumtype, drro->drr_compress, | |
3340 | drro->drr_dn_slots, err); | |
3341 | break; | |
3342 | } | |
3343 | case DRR_FREEOBJECTS: | |
3344 | { | |
3345 | struct drr_freeobjects *drrfo = | |
3346 | &rrd->header.drr_u.drr_freeobjects; | |
3347 | dprintf("drr_type = FREEOBJECTS firstobj = %llu " | |
3348 | "numobjs = %llu err = %d\n", | |
3349 | drrfo->drr_firstobj, drrfo->drr_numobjs, err); | |
3350 | break; | |
3351 | } | |
3352 | case DRR_WRITE: | |
3353 | { | |
3354 | struct drr_write *drrw = &rrd->header.drr_u.drr_write; | |
3355 | dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu " | |
3356 | "lsize = %llu cksumtype = %u cksumflags = %u " | |
3357 | "compress = %u psize = %llu err = %d\n", | |
3358 | drrw->drr_object, drrw->drr_type, drrw->drr_offset, | |
3359 | drrw->drr_logical_size, drrw->drr_checksumtype, | |
b5256303 | 3360 | drrw->drr_flags, drrw->drr_compressiontype, |
6a8ee4f7 NB |
3361 | drrw->drr_compressed_size, err); |
3362 | break; | |
3363 | } | |
3364 | case DRR_WRITE_BYREF: | |
3365 | { | |
3366 | struct drr_write_byref *drrwbr = | |
3367 | &rrd->header.drr_u.drr_write_byref; | |
3368 | dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu " | |
3369 | "length = %llu toguid = %llx refguid = %llx " | |
3370 | "refobject = %llu refoffset = %llu cksumtype = %u " | |
3371 | "cksumflags = %u err = %d\n", | |
3372 | drrwbr->drr_object, drrwbr->drr_offset, | |
3373 | drrwbr->drr_length, drrwbr->drr_toguid, | |
3374 | drrwbr->drr_refguid, drrwbr->drr_refobject, | |
3375 | drrwbr->drr_refoffset, drrwbr->drr_checksumtype, | |
b5256303 | 3376 | drrwbr->drr_flags, err); |
6a8ee4f7 NB |
3377 | break; |
3378 | } | |
3379 | case DRR_WRITE_EMBEDDED: | |
3380 | { | |
3381 | struct drr_write_embedded *drrwe = | |
3382 | &rrd->header.drr_u.drr_write_embedded; | |
3383 | dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu " | |
3384 | "length = %llu compress = %u etype = %u lsize = %u " | |
3385 | "psize = %u err = %d\n", | |
3386 | drrwe->drr_object, drrwe->drr_offset, drrwe->drr_length, | |
3387 | drrwe->drr_compression, drrwe->drr_etype, | |
3388 | drrwe->drr_lsize, drrwe->drr_psize, err); | |
3389 | break; | |
3390 | } | |
3391 | case DRR_FREE: | |
3392 | { | |
3393 | struct drr_free *drrf = &rrd->header.drr_u.drr_free; | |
3394 | dprintf("drr_type = FREE obj = %llu offset = %llu " | |
3395 | "length = %lld err = %d\n", | |
3396 | drrf->drr_object, drrf->drr_offset, drrf->drr_length, | |
3397 | err); | |
3398 | break; | |
3399 | } | |
3400 | case DRR_SPILL: | |
3401 | { | |
3402 | struct drr_spill *drrs = &rrd->header.drr_u.drr_spill; | |
3403 | dprintf("drr_type = SPILL obj = %llu length = %llu " | |
3404 | "err = %d\n", drrs->drr_object, drrs->drr_length, err); | |
3405 | break; | |
3406 | } | |
3407 | default: | |
3408 | return; | |
3409 | } | |
3410 | } | |
3411 | ||
fcff0f35 PD |
3412 | /* |
3413 | * Commit the records to the pool. | |
3414 | */ | |
3415 | static int | |
3416 | receive_process_record(struct receive_writer_arg *rwa, | |
3417 | struct receive_record_arg *rrd) | |
3418 | { | |
3419 | int err; | |
3420 | ||
47dfff3b MA |
3421 | /* Processing in order, therefore bytes_read should be increasing. */ |
3422 | ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read); | |
3423 | rwa->bytes_read = rrd->bytes_read; | |
3424 | ||
fcff0f35 PD |
3425 | switch (rrd->header.drr_type) { |
3426 | case DRR_OBJECT: | |
3427 | { | |
3428 | struct drr_object *drro = &rrd->header.drr_u.drr_object; | |
3429 | err = receive_object(rwa, drro, rrd->payload); | |
3430 | kmem_free(rrd->payload, rrd->payload_size); | |
3431 | rrd->payload = NULL; | |
6a8ee4f7 | 3432 | break; |
fcff0f35 PD |
3433 | } |
3434 | case DRR_FREEOBJECTS: | |
3435 | { | |
3436 | struct drr_freeobjects *drrfo = | |
3437 | &rrd->header.drr_u.drr_freeobjects; | |
6a8ee4f7 NB |
3438 | err = receive_freeobjects(rwa, drrfo); |
3439 | break; | |
fcff0f35 PD |
3440 | } |
3441 | case DRR_WRITE: | |
3442 | { | |
3443 | struct drr_write *drrw = &rrd->header.drr_u.drr_write; | |
b5256303 | 3444 | err = receive_write(rwa, drrw, rrd->arc_buf); |
fcff0f35 PD |
3445 | /* if receive_write() is successful, it consumes the arc_buf */ |
3446 | if (err != 0) | |
b5256303 TC |
3447 | dmu_return_arcbuf(rrd->arc_buf); |
3448 | rrd->arc_buf = NULL; | |
fcff0f35 | 3449 | rrd->payload = NULL; |
6a8ee4f7 | 3450 | break; |
fcff0f35 PD |
3451 | } |
3452 | case DRR_WRITE_BYREF: | |
3453 | { | |
3454 | struct drr_write_byref *drrwbr = | |
3455 | &rrd->header.drr_u.drr_write_byref; | |
6a8ee4f7 NB |
3456 | err = receive_write_byref(rwa, drrwbr); |
3457 | break; | |
fcff0f35 PD |
3458 | } |
3459 | case DRR_WRITE_EMBEDDED: | |
3460 | { | |
3461 | struct drr_write_embedded *drrwe = | |
3462 | &rrd->header.drr_u.drr_write_embedded; | |
3463 | err = receive_write_embedded(rwa, drrwe, rrd->payload); | |
3464 | kmem_free(rrd->payload, rrd->payload_size); | |
3465 | rrd->payload = NULL; | |
6a8ee4f7 | 3466 | break; |
fcff0f35 PD |
3467 | } |
3468 | case DRR_FREE: | |
3469 | { | |
3470 | struct drr_free *drrf = &rrd->header.drr_u.drr_free; | |
6a8ee4f7 NB |
3471 | err = receive_free(rwa, drrf); |
3472 | break; | |
fcff0f35 PD |
3473 | } |
3474 | case DRR_SPILL: | |
3475 | { | |
3476 | struct drr_spill *drrs = &rrd->header.drr_u.drr_spill; | |
b5256303 TC |
3477 | err = receive_spill(rwa, drrs, rrd->arc_buf); |
3478 | /* if receive_spill() is successful, it consumes the arc_buf */ | |
3479 | if (err != 0) | |
3480 | dmu_return_arcbuf(rrd->arc_buf); | |
3481 | rrd->arc_buf = NULL; | |
fcff0f35 | 3482 | rrd->payload = NULL; |
6a8ee4f7 | 3483 | break; |
37f8a883 | 3484 | } |
b5256303 TC |
3485 | case DRR_OBJECT_RANGE: |
3486 | { | |
3487 | struct drr_object_range *drror = | |
3488 | &rrd->header.drr_u.drr_object_range; | |
3489 | return (receive_object_range(rwa, drror)); | |
3490 | } | |
37f8a883 MA |
3491 | default: |
3492 | return (SET_ERROR(EINVAL)); | |
3493 | } | |
6a8ee4f7 NB |
3494 | |
3495 | if (err != 0) | |
3496 | dprintf_drr(rrd, err); | |
3497 | ||
3498 | return (err); | |
37f8a883 MA |
3499 | } |
3500 | ||
34dc7c2f | 3501 | /* |
fcff0f35 PD |
3502 | * dmu_recv_stream's worker thread; pull records off the queue, and then call |
3503 | * receive_process_record When we're done, signal the main thread and exit. | |
3504 | */ | |
3505 | static void | |
3506 | receive_writer_thread(void *arg) | |
3507 | { | |
3508 | struct receive_writer_arg *rwa = arg; | |
3509 | struct receive_record_arg *rrd; | |
3e635ac1 TC |
3510 | fstrans_cookie_t cookie = spl_fstrans_mark(); |
3511 | ||
fcff0f35 PD |
3512 | for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker; |
3513 | rrd = bqueue_dequeue(&rwa->q)) { | |
3514 | /* | |
3515 | * If there's an error, the main thread will stop putting things | |
3516 | * on the queue, but we need to clear everything in it before we | |
3517 | * can exit. | |
3518 | */ | |
3519 | if (rwa->err == 0) { | |
3520 | rwa->err = receive_process_record(rwa, rrd); | |
b5256303 TC |
3521 | } else if (rrd->arc_buf != NULL) { |
3522 | dmu_return_arcbuf(rrd->arc_buf); | |
3523 | rrd->arc_buf = NULL; | |
fcff0f35 PD |
3524 | rrd->payload = NULL; |
3525 | } else if (rrd->payload != NULL) { | |
3526 | kmem_free(rrd->payload, rrd->payload_size); | |
3527 | rrd->payload = NULL; | |
3528 | } | |
3529 | kmem_free(rrd, sizeof (*rrd)); | |
3530 | } | |
3531 | kmem_free(rrd, sizeof (*rrd)); | |
3532 | mutex_enter(&rwa->mutex); | |
3533 | rwa->done = B_TRUE; | |
3534 | cv_signal(&rwa->cv); | |
3535 | mutex_exit(&rwa->mutex); | |
3e635ac1 | 3536 | spl_fstrans_unmark(cookie); |
34a6b428 | 3537 | thread_exit(); |
fcff0f35 PD |
3538 | } |
3539 | ||
47dfff3b MA |
3540 | static int |
3541 | resume_check(struct receive_arg *ra, nvlist_t *begin_nvl) | |
3542 | { | |
3543 | uint64_t val; | |
3544 | objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset; | |
3545 | uint64_t dsobj = dmu_objset_id(ra->os); | |
3546 | uint64_t resume_obj, resume_off; | |
3547 | ||
3548 | if (nvlist_lookup_uint64(begin_nvl, | |
3549 | "resume_object", &resume_obj) != 0 || | |
3550 | nvlist_lookup_uint64(begin_nvl, | |
3551 | "resume_offset", &resume_off) != 0) { | |
3552 | return (SET_ERROR(EINVAL)); | |
3553 | } | |
3554 | VERIFY0(zap_lookup(mos, dsobj, | |
3555 | DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val)); | |
3556 | if (resume_obj != val) | |
3557 | return (SET_ERROR(EINVAL)); | |
3558 | VERIFY0(zap_lookup(mos, dsobj, | |
3559 | DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val)); | |
3560 | if (resume_off != val) | |
3561 | return (SET_ERROR(EINVAL)); | |
3562 | ||
3563 | return (0); | |
3564 | } | |
3565 | ||
fcff0f35 PD |
3566 | /* |
3567 | * Read in the stream's records, one by one, and apply them to the pool. There | |
3568 | * are two threads involved; the thread that calls this function will spin up a | |
3569 | * worker thread, read the records off the stream one by one, and issue | |
3570 | * prefetches for any necessary indirect blocks. It will then push the records | |
3571 | * onto an internal blocking queue. The worker thread will pull the records off | |
3572 | * the queue, and actually write the data into the DMU. This way, the worker | |
3573 | * thread doesn't have to wait for reads to complete, since everything it needs | |
3574 | * (the indirect blocks) will be prefetched. | |
3575 | * | |
34dc7c2f BB |
3576 | * NB: callers *must* call dmu_recv_end() if this succeeds. |
3577 | */ | |
3578 | int | |
572e2857 BB |
3579 | dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp, |
3580 | int cleanup_fd, uint64_t *action_handlep) | |
34dc7c2f | 3581 | { |
37f8a883 | 3582 | int err = 0; |
04bc4610 NB |
3583 | struct receive_arg *ra; |
3584 | struct receive_writer_arg *rwa; | |
428870ff | 3585 | int featureflags; |
47dfff3b MA |
3586 | uint32_t payloadlen; |
3587 | void *payload; | |
3588 | nvlist_t *begin_nvl = NULL; | |
34dc7c2f | 3589 | |
04bc4610 NB |
3590 | ra = kmem_zalloc(sizeof (*ra), KM_SLEEP); |
3591 | rwa = kmem_zalloc(sizeof (*rwa), KM_SLEEP); | |
3592 | ||
3593 | ra->byteswap = drc->drc_byteswap; | |
9b840763 | 3594 | ra->raw = drc->drc_raw; |
04bc4610 NB |
3595 | ra->cksum = drc->drc_cksum; |
3596 | ra->vp = vp; | |
3597 | ra->voff = *voffp; | |
47dfff3b MA |
3598 | |
3599 | if (dsl_dataset_is_zapified(drc->drc_ds)) { | |
3600 | (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset, | |
3601 | drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES, | |
3602 | sizeof (ra->bytes_read), 1, &ra->bytes_read); | |
3603 | } | |
3604 | ||
e6d3a843 | 3605 | objlist_create(&ra->ignore_objlist); |
34dc7c2f BB |
3606 | |
3607 | /* these were verified in dmu_recv_begin */ | |
13fe0198 | 3608 | ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==, |
428870ff | 3609 | DMU_SUBSTREAM); |
13fe0198 | 3610 | ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES); |
34dc7c2f BB |
3611 | |
3612 | /* | |
3613 | * Open the objset we are modifying. | |
3614 | */ | |
04bc4610 | 3615 | VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra->os)); |
34dc7c2f | 3616 | |
d683ddbb | 3617 | ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT); |
34dc7c2f | 3618 | |
428870ff | 3619 | featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo); |
b5256303 | 3620 | ra->featureflags = featureflags; |
428870ff | 3621 | |
9b840763 TC |
3622 | /* embedded data is incompatible with encrypted datasets */ |
3623 | if (ra->os->os_encrypted && | |
3624 | (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)) { | |
3625 | err = SET_ERROR(EINVAL); | |
3626 | goto out; | |
3627 | } | |
3628 | ||
428870ff BB |
3629 | /* if this stream is dedup'ed, set up the avl tree for guid mapping */ |
3630 | if (featureflags & DMU_BACKUP_FEATURE_DEDUP) { | |
572e2857 BB |
3631 | minor_t minor; |
3632 | ||
3633 | if (cleanup_fd == -1) { | |
9b840763 | 3634 | err = SET_ERROR(EBADF); |
572e2857 BB |
3635 | goto out; |
3636 | } | |
9b840763 TC |
3637 | err = zfs_onexit_fd_hold(cleanup_fd, &minor); |
3638 | if (err != 0) { | |
572e2857 BB |
3639 | cleanup_fd = -1; |
3640 | goto out; | |
3641 | } | |
3642 | ||
3643 | if (*action_handlep == 0) { | |
04bc4610 | 3644 | rwa->guid_to_ds_map = |
572e2857 | 3645 | kmem_alloc(sizeof (avl_tree_t), KM_SLEEP); |
04bc4610 | 3646 | avl_create(rwa->guid_to_ds_map, guid_compare, |
572e2857 BB |
3647 | sizeof (guid_map_entry_t), |
3648 | offsetof(guid_map_entry_t, avlnode)); | |
37f8a883 | 3649 | err = zfs_onexit_add_cb(minor, |
04bc4610 | 3650 | free_guid_map_onexit, rwa->guid_to_ds_map, |
572e2857 | 3651 | action_handlep); |
9b840763 | 3652 | if (err != 0) |
572e2857 BB |
3653 | goto out; |
3654 | } else { | |
37f8a883 | 3655 | err = zfs_onexit_cb_data(minor, *action_handlep, |
04bc4610 | 3656 | (void **)&rwa->guid_to_ds_map); |
9b840763 | 3657 | if (err != 0) |
572e2857 BB |
3658 | goto out; |
3659 | } | |
8d35c149 | 3660 | |
04bc4610 | 3661 | drc->drc_guid_to_ds_map = rwa->guid_to_ds_map; |
428870ff BB |
3662 | } |
3663 | ||
47dfff3b MA |
3664 | payloadlen = drc->drc_drr_begin->drr_payloadlen; |
3665 | payload = NULL; | |
3666 | if (payloadlen != 0) | |
3667 | payload = kmem_alloc(payloadlen, KM_SLEEP); | |
3668 | ||
3669 | err = receive_read_payload_and_next_header(ra, payloadlen, payload); | |
3670 | if (err != 0) { | |
3671 | if (payloadlen != 0) | |
3672 | kmem_free(payload, payloadlen); | |
37f8a883 | 3673 | goto out; |
47dfff3b MA |
3674 | } |
3675 | if (payloadlen != 0) { | |
3676 | err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP); | |
3677 | kmem_free(payload, payloadlen); | |
3678 | if (err != 0) | |
3679 | goto out; | |
3680 | } | |
3681 | ||
b5256303 TC |
3682 | /* handle DSL encryption key payload */ |
3683 | if (featureflags & DMU_BACKUP_FEATURE_RAW) { | |
3684 | nvlist_t *keynvl = NULL; | |
3685 | ||
3686 | ASSERT(ra->os->os_encrypted); | |
3687 | ASSERT(drc->drc_raw); | |
3688 | ||
3689 | err = nvlist_lookup_nvlist(begin_nvl, "crypt_keydata", &keynvl); | |
3690 | if (err != 0) | |
3691 | goto out; | |
3692 | ||
3693 | err = dsl_crypto_recv_key(spa_name(ra->os->os_spa), | |
3694 | drc->drc_ds->ds_object, drc->drc_drrb->drr_type, | |
3695 | keynvl); | |
3696 | if (err != 0) | |
3697 | goto out; | |
3698 | } | |
3699 | ||
47dfff3b MA |
3700 | if (featureflags & DMU_BACKUP_FEATURE_RESUMING) { |
3701 | err = resume_check(ra, begin_nvl); | |
3702 | if (err != 0) | |
3703 | goto out; | |
3704 | } | |
37f8a883 | 3705 | |
04bc4610 | 3706 | (void) bqueue_init(&rwa->q, zfs_recv_queue_length, |
fcff0f35 | 3707 | offsetof(struct receive_record_arg, node)); |
04bc4610 NB |
3708 | cv_init(&rwa->cv, NULL, CV_DEFAULT, NULL); |
3709 | mutex_init(&rwa->mutex, NULL, MUTEX_DEFAULT, NULL); | |
3710 | rwa->os = ra->os; | |
3711 | rwa->byteswap = drc->drc_byteswap; | |
47dfff3b | 3712 | rwa->resumable = drc->drc_resumable; |
9b840763 | 3713 | rwa->raw = drc->drc_raw; |
fcff0f35 | 3714 | |
04bc4610 | 3715 | (void) thread_create(NULL, 0, receive_writer_thread, rwa, 0, curproc, |
fcff0f35 PD |
3716 | TS_RUN, minclsyspri); |
3717 | /* | |
04bc4610 | 3718 | * We're reading rwa->err without locks, which is safe since we are the |
fcff0f35 PD |
3719 | * only reader, and the worker thread is the only writer. It's ok if we |
3720 | * miss a write for an iteration or two of the loop, since the writer | |
3721 | * thread will keep freeing records we send it until we send it an eos | |
3722 | * marker. | |
3723 | * | |
04bc4610 | 3724 | * We can leave this loop in 3 ways: First, if rwa->err is |
fcff0f35 PD |
3725 | * non-zero. In that case, the writer thread will free the rrd we just |
3726 | * pushed. Second, if we're interrupted; in that case, either it's the | |
47dfff3b | 3727 | * first loop and ra->rrd was never allocated, or it's later and ra->rrd |
fcff0f35 PD |
3728 | * has been handed off to the writer thread who will free it. Finally, |
3729 | * if receive_read_record fails or we're at the end of the stream, then | |
04bc4610 | 3730 | * we free ra->rrd and exit. |
fcff0f35 | 3731 | */ |
04bc4610 | 3732 | while (rwa->err == 0) { |
34dc7c2f | 3733 | if (issig(JUSTLOOKING) && issig(FORREAL)) { |
37f8a883 MA |
3734 | err = SET_ERROR(EINTR); |
3735 | break; | |
34dc7c2f BB |
3736 | } |
3737 | ||
04bc4610 NB |
3738 | ASSERT3P(ra->rrd, ==, NULL); |
3739 | ra->rrd = ra->next_rrd; | |
3740 | ra->next_rrd = NULL; | |
3741 | /* Allocates and loads header into ra->next_rrd */ | |
3742 | err = receive_read_record(ra); | |
34dc7c2f | 3743 | |
04bc4610 NB |
3744 | if (ra->rrd->header.drr_type == DRR_END || err != 0) { |
3745 | kmem_free(ra->rrd, sizeof (*ra->rrd)); | |
3746 | ra->rrd = NULL; | |
428870ff | 3747 | break; |
fcff0f35 PD |
3748 | } |
3749 | ||
04bc4610 NB |
3750 | bqueue_enqueue(&rwa->q, ra->rrd, |
3751 | sizeof (struct receive_record_arg) + ra->rrd->payload_size); | |
3752 | ra->rrd = NULL; | |
fcff0f35 | 3753 | } |
04bc4610 NB |
3754 | if (ra->next_rrd == NULL) |
3755 | ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP); | |
3756 | ra->next_rrd->eos_marker = B_TRUE; | |
3757 | bqueue_enqueue(&rwa->q, ra->next_rrd, 1); | |
fcff0f35 | 3758 | |
04bc4610 NB |
3759 | mutex_enter(&rwa->mutex); |
3760 | while (!rwa->done) { | |
3761 | cv_wait(&rwa->cv, &rwa->mutex); | |
34dc7c2f | 3762 | } |
04bc4610 | 3763 | mutex_exit(&rwa->mutex); |
fcff0f35 | 3764 | |
48fbb9dd FG |
3765 | /* |
3766 | * If we are receiving a full stream as a clone, all object IDs which | |
3767 | * are greater than the maximum ID referenced in the stream are | |
3768 | * by definition unused and must be freed. | |
3769 | */ | |
3770 | if (drc->drc_clone && drc->drc_drrb->drr_fromguid == 0) { | |
3771 | uint64_t obj = rwa->max_object + 1; | |
3772 | int free_err = 0; | |
3773 | int next_err = 0; | |
3774 | ||
3775 | while (next_err == 0) { | |
3776 | free_err = dmu_free_long_object(rwa->os, obj); | |
3777 | if (free_err != 0 && free_err != ENOENT) | |
3778 | break; | |
3779 | ||
3780 | next_err = dmu_object_next(rwa->os, &obj, FALSE, 0); | |
3781 | } | |
3782 | ||
3783 | if (err == 0) { | |
3784 | if (free_err != 0 && free_err != ENOENT) | |
3785 | err = free_err; | |
3786 | else if (next_err != ESRCH) | |
3787 | err = next_err; | |
3788 | } | |
3789 | } | |
3790 | ||
04bc4610 NB |
3791 | cv_destroy(&rwa->cv); |
3792 | mutex_destroy(&rwa->mutex); | |
3793 | bqueue_destroy(&rwa->q); | |
fcff0f35 | 3794 | if (err == 0) |
04bc4610 | 3795 | err = rwa->err; |
34dc7c2f BB |
3796 | |
3797 | out: | |
47dfff3b | 3798 | nvlist_free(begin_nvl); |
572e2857 BB |
3799 | if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1)) |
3800 | zfs_onexit_fd_rele(cleanup_fd); | |
3801 | ||
37f8a883 | 3802 | if (err != 0) { |
34dc7c2f | 3803 | /* |
47dfff3b MA |
3804 | * Clean up references. If receive is not resumable, |
3805 | * destroy what we created, so we don't leave it in | |
3806 | * the inconsistent state. | |
34dc7c2f | 3807 | */ |
13fe0198 | 3808 | dmu_recv_cleanup_ds(drc); |
34dc7c2f BB |
3809 | } |
3810 | ||
04bc4610 | 3811 | *voffp = ra->voff; |
e6d3a843 | 3812 | objlist_destroy(&ra->ignore_objlist); |
04bc4610 NB |
3813 | kmem_free(ra, sizeof (*ra)); |
3814 | kmem_free(rwa, sizeof (*rwa)); | |
37f8a883 | 3815 | return (err); |
34dc7c2f BB |
3816 | } |
3817 | ||
34dc7c2f | 3818 | static int |
13fe0198 | 3819 | dmu_recv_end_check(void *arg, dmu_tx_t *tx) |
34dc7c2f | 3820 | { |
13fe0198 MA |
3821 | dmu_recv_cookie_t *drc = arg; |
3822 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
3823 | int error; | |
3824 | ||
3825 | ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag); | |
3826 | ||
3827 | if (!drc->drc_newfs) { | |
3828 | dsl_dataset_t *origin_head; | |
3829 | ||
3830 | error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head); | |
3831 | if (error != 0) | |
3832 | return (error); | |
19580676 MA |
3833 | if (drc->drc_force) { |
3834 | /* | |
3835 | * We will destroy any snapshots in tofs (i.e. before | |
3836 | * origin_head) that are after the origin (which is | |
3837 | * the snap before drc_ds, because drc_ds can not | |
3838 | * have any snaps of its own). | |
3839 | */ | |
d683ddbb JG |
3840 | uint64_t obj; |
3841 | ||
3842 | obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj; | |
3843 | while (obj != | |
3844 | dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) { | |
19580676 MA |
3845 | dsl_dataset_t *snap; |
3846 | error = dsl_dataset_hold_obj(dp, obj, FTAG, | |
3847 | &snap); | |
3848 | if (error != 0) | |
b6640117 | 3849 | break; |
19580676 MA |
3850 | if (snap->ds_dir != origin_head->ds_dir) |
3851 | error = SET_ERROR(EINVAL); | |
3852 | if (error == 0) { | |
3853 | error = dsl_destroy_snapshot_check_impl( | |
3854 | snap, B_FALSE); | |
3855 | } | |
d683ddbb | 3856 | obj = dsl_dataset_phys(snap)->ds_prev_snap_obj; |
19580676 MA |
3857 | dsl_dataset_rele(snap, FTAG); |
3858 | if (error != 0) | |
b6640117 AG |
3859 | break; |
3860 | } | |
3861 | if (error != 0) { | |
3862 | dsl_dataset_rele(origin_head, FTAG); | |
3863 | return (error); | |
19580676 MA |
3864 | } |
3865 | } | |
13fe0198 | 3866 | error = dsl_dataset_clone_swap_check_impl(drc->drc_ds, |
831baf06 | 3867 | origin_head, drc->drc_force, drc->drc_owner, tx); |
13fe0198 MA |
3868 | if (error != 0) { |
3869 | dsl_dataset_rele(origin_head, FTAG); | |
3870 | return (error); | |
3871 | } | |
3872 | error = dsl_dataset_snapshot_check_impl(origin_head, | |
788eb90c | 3873 | drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred); |
13fe0198 MA |
3874 | dsl_dataset_rele(origin_head, FTAG); |
3875 | if (error != 0) | |
3876 | return (error); | |
34dc7c2f | 3877 | |
13fe0198 MA |
3878 | error = dsl_destroy_head_check_impl(drc->drc_ds, 1); |
3879 | } else { | |
3880 | error = dsl_dataset_snapshot_check_impl(drc->drc_ds, | |
788eb90c | 3881 | drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred); |
13fe0198 MA |
3882 | } |
3883 | return (error); | |
34dc7c2f BB |
3884 | } |
3885 | ||
3886 | static void | |
13fe0198 | 3887 | dmu_recv_end_sync(void *arg, dmu_tx_t *tx) |
34dc7c2f | 3888 | { |
13fe0198 MA |
3889 | dmu_recv_cookie_t *drc = arg; |
3890 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
b5256303 | 3891 | boolean_t encrypted = drc->drc_ds->ds_dir->dd_crypto_obj != 0; |
13fe0198 MA |
3892 | |
3893 | spa_history_log_internal_ds(drc->drc_ds, "finish receiving", | |
3894 | tx, "snap=%s", drc->drc_tosnap); | |
3895 | ||
3896 | if (!drc->drc_newfs) { | |
3897 | dsl_dataset_t *origin_head; | |
3898 | ||
3899 | VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG, | |
3900 | &origin_head)); | |
19580676 MA |
3901 | |
3902 | if (drc->drc_force) { | |
3903 | /* | |
3904 | * Destroy any snapshots of drc_tofs (origin_head) | |
3905 | * after the origin (the snap before drc_ds). | |
3906 | */ | |
d683ddbb JG |
3907 | uint64_t obj; |
3908 | ||
3909 | obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj; | |
3910 | while (obj != | |
3911 | dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) { | |
19580676 MA |
3912 | dsl_dataset_t *snap; |
3913 | VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, | |
3914 | &snap)); | |
3915 | ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir); | |
d683ddbb | 3916 | obj = dsl_dataset_phys(snap)->ds_prev_snap_obj; |
19580676 MA |
3917 | dsl_destroy_snapshot_sync_impl(snap, |
3918 | B_FALSE, tx); | |
3919 | dsl_dataset_rele(snap, FTAG); | |
3920 | } | |
3921 | } | |
3922 | VERIFY3P(drc->drc_ds->ds_prev, ==, | |
3923 | origin_head->ds_prev); | |
3924 | ||
13fe0198 MA |
3925 | dsl_dataset_clone_swap_sync_impl(drc->drc_ds, |
3926 | origin_head, tx); | |
3927 | dsl_dataset_snapshot_sync_impl(origin_head, | |
3928 | drc->drc_tosnap, tx); | |
3929 | ||
3930 | /* set snapshot's creation time and guid */ | |
3931 | dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx); | |
d683ddbb | 3932 | dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time = |
13fe0198 | 3933 | drc->drc_drrb->drr_creation_time; |
d683ddbb | 3934 | dsl_dataset_phys(origin_head->ds_prev)->ds_guid = |
13fe0198 | 3935 | drc->drc_drrb->drr_toguid; |
d683ddbb | 3936 | dsl_dataset_phys(origin_head->ds_prev)->ds_flags &= |
13fe0198 MA |
3937 | ~DS_FLAG_INCONSISTENT; |
3938 | ||
3939 | dmu_buf_will_dirty(origin_head->ds_dbuf, tx); | |
d683ddbb JG |
3940 | dsl_dataset_phys(origin_head)->ds_flags &= |
3941 | ~DS_FLAG_INCONSISTENT; | |
13fe0198 | 3942 | |
2e0e443a GM |
3943 | drc->drc_newsnapobj = |
3944 | dsl_dataset_phys(origin_head)->ds_prev_snap_obj; | |
3945 | ||
13fe0198 MA |
3946 | dsl_dataset_rele(origin_head, FTAG); |
3947 | dsl_destroy_head_sync_impl(drc->drc_ds, tx); | |
831baf06 KW |
3948 | |
3949 | if (drc->drc_owner != NULL) | |
3950 | VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner); | |
13fe0198 MA |
3951 | } else { |
3952 | dsl_dataset_t *ds = drc->drc_ds; | |
34dc7c2f | 3953 | |
13fe0198 | 3954 | dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx); |
34dc7c2f | 3955 | |
13fe0198 MA |
3956 | /* set snapshot's creation time and guid */ |
3957 | dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx); | |
d683ddbb | 3958 | dsl_dataset_phys(ds->ds_prev)->ds_creation_time = |
13fe0198 | 3959 | drc->drc_drrb->drr_creation_time; |
d683ddbb JG |
3960 | dsl_dataset_phys(ds->ds_prev)->ds_guid = |
3961 | drc->drc_drrb->drr_toguid; | |
3962 | dsl_dataset_phys(ds->ds_prev)->ds_flags &= | |
3963 | ~DS_FLAG_INCONSISTENT; | |
34dc7c2f | 3964 | |
13fe0198 | 3965 | dmu_buf_will_dirty(ds->ds_dbuf, tx); |
d683ddbb | 3966 | dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT; |
47dfff3b MA |
3967 | if (dsl_dataset_has_resume_receive_state(ds)) { |
3968 | (void) zap_remove(dp->dp_meta_objset, ds->ds_object, | |
3969 | DS_FIELD_RESUME_FROMGUID, tx); | |
3970 | (void) zap_remove(dp->dp_meta_objset, ds->ds_object, | |
3971 | DS_FIELD_RESUME_OBJECT, tx); | |
3972 | (void) zap_remove(dp->dp_meta_objset, ds->ds_object, | |
3973 | DS_FIELD_RESUME_OFFSET, tx); | |
3974 | (void) zap_remove(dp->dp_meta_objset, ds->ds_object, | |
3975 | DS_FIELD_RESUME_BYTES, tx); | |
3976 | (void) zap_remove(dp->dp_meta_objset, ds->ds_object, | |
3977 | DS_FIELD_RESUME_TOGUID, tx); | |
3978 | (void) zap_remove(dp->dp_meta_objset, ds->ds_object, | |
3979 | DS_FIELD_RESUME_TONAME, tx); | |
3980 | } | |
2e0e443a GM |
3981 | drc->drc_newsnapobj = |
3982 | dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj; | |
13fe0198 | 3983 | } |
a0bd735a | 3984 | zvol_create_minors(dp->dp_spa, drc->drc_tofs, B_TRUE); |
b5256303 | 3985 | |
13fe0198 MA |
3986 | /* |
3987 | * Release the hold from dmu_recv_begin. This must be done before | |
b5256303 TC |
3988 | * we return to open context, so that when we free the dataset's dnode |
3989 | * we can evict its bonus buffer. Since the dataset may be destroyed | |
3990 | * at this point (and therefore won't have a valid pointer to the spa) | |
3991 | * we release the key mapping manually here while we do have a valid | |
3992 | * pointer, if it exists. | |
13fe0198 | 3993 | */ |
b5256303 TC |
3994 | if (!drc->drc_raw && encrypted) { |
3995 | (void) spa_keystore_remove_mapping(dmu_tx_pool(tx)->dp_spa, | |
3996 | drc->drc_ds->ds_object, drc->drc_ds); | |
3997 | } | |
3998 | dsl_dataset_disown(drc->drc_ds, 0, dmu_recv_tag); | |
13fe0198 | 3999 | drc->drc_ds = NULL; |
34dc7c2f BB |
4000 | } |
4001 | ||
8d35c149 | 4002 | static int |
b5256303 TC |
4003 | add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj, |
4004 | boolean_t raw) | |
8d35c149 | 4005 | { |
13fe0198 | 4006 | dsl_pool_t *dp; |
8d35c149 AS |
4007 | dsl_dataset_t *snapds; |
4008 | guid_map_entry_t *gmep; | |
b5256303 | 4009 | ds_hold_flags_t dsflags = (raw) ? 0 : DS_HOLD_FLAG_DECRYPT; |
8d35c149 AS |
4010 | int err; |
4011 | ||
4012 | ASSERT(guid_map != NULL); | |
4013 | ||
13fe0198 MA |
4014 | err = dsl_pool_hold(name, FTAG, &dp); |
4015 | if (err != 0) | |
4016 | return (err); | |
7ec09286 | 4017 | gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP); |
b5256303 | 4018 | err = dsl_dataset_hold_obj_flags(dp, snapobj, dsflags, gmep, &snapds); |
8d35c149 | 4019 | if (err == 0) { |
d683ddbb | 4020 | gmep->guid = dsl_dataset_phys(snapds)->ds_guid; |
b5256303 | 4021 | gmep->raw = raw; |
8d35c149 AS |
4022 | gmep->gme_ds = snapds; |
4023 | avl_add(guid_map, gmep); | |
13fe0198 | 4024 | dsl_dataset_long_hold(snapds, gmep); |
7ec09286 MA |
4025 | } else { |
4026 | kmem_free(gmep, sizeof (*gmep)); | |
8d35c149 AS |
4027 | } |
4028 | ||
13fe0198 | 4029 | dsl_pool_rele(dp, FTAG); |
8d35c149 AS |
4030 | return (err); |
4031 | } | |
4032 | ||
13fe0198 MA |
4033 | static int dmu_recv_end_modified_blocks = 3; |
4034 | ||
428870ff BB |
4035 | static int |
4036 | dmu_recv_existing_end(dmu_recv_cookie_t *drc) | |
34dc7c2f | 4037 | { |
13fe0198 | 4038 | #ifdef _KERNEL |
13fe0198 MA |
4039 | /* |
4040 | * We will be destroying the ds; make sure its origin is unmounted if | |
4041 | * necessary. | |
4042 | */ | |
eca7b760 | 4043 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
13fe0198 MA |
4044 | dsl_dataset_name(drc->drc_ds, name); |
4045 | zfs_destroy_unmount_origin(name); | |
13fe0198 | 4046 | #endif |
34dc7c2f | 4047 | |
2e0e443a | 4048 | return (dsl_sync_task(drc->drc_tofs, |
13fe0198 | 4049 | dmu_recv_end_check, dmu_recv_end_sync, drc, |
2e0e443a | 4050 | dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL)); |
34dc7c2f | 4051 | } |
428870ff BB |
4052 | |
4053 | static int | |
4054 | dmu_recv_new_end(dmu_recv_cookie_t *drc) | |
2e0e443a GM |
4055 | { |
4056 | return (dsl_sync_task(drc->drc_tofs, | |
4057 | dmu_recv_end_check, dmu_recv_end_sync, drc, | |
4058 | dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL)); | |
4059 | } | |
4060 | ||
4061 | int | |
4062 | dmu_recv_end(dmu_recv_cookie_t *drc, void *owner) | |
428870ff | 4063 | { |
13fe0198 | 4064 | int error; |
428870ff | 4065 | |
2e0e443a GM |
4066 | drc->drc_owner = owner; |
4067 | ||
4068 | if (drc->drc_newfs) | |
4069 | error = dmu_recv_new_end(drc); | |
4070 | else | |
4071 | error = dmu_recv_existing_end(drc); | |
428870ff | 4072 | |
13fe0198 MA |
4073 | if (error != 0) { |
4074 | dmu_recv_cleanup_ds(drc); | |
4075 | } else if (drc->drc_guid_to_ds_map != NULL) { | |
b5256303 TC |
4076 | (void) add_ds_to_guidmap(drc->drc_tofs, drc->drc_guid_to_ds_map, |
4077 | drc->drc_newsnapobj, drc->drc_raw); | |
428870ff | 4078 | } |
13fe0198 | 4079 | return (error); |
428870ff BB |
4080 | } |
4081 | ||
ea97f8ce MA |
4082 | /* |
4083 | * Return TRUE if this objset is currently being received into. | |
4084 | */ | |
4085 | boolean_t | |
4086 | dmu_objset_is_receiving(objset_t *os) | |
4087 | { | |
4088 | return (os->os_dsl_dataset != NULL && | |
4089 | os->os_dsl_dataset->ds_owner == dmu_recv_tag); | |
4090 | } | |
fd8febbd TF |
4091 | |
4092 | #if defined(_KERNEL) | |
4093 | module_param(zfs_send_corrupt_data, int, 0644); | |
4094 | MODULE_PARM_DESC(zfs_send_corrupt_data, "Allow sending corrupt data"); | |
4095 | #endif |