]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dmu_recv.c
Reject streams that set ->drr_payloadlen to unreasonably large values
[mirror_zfs.git] / module / zfs / dmu_recv.c
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 https://opensource.org/licenses/CDDL-1.0.
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 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26 * Copyright 2014 HybridCluster. All rights reserved.
27 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
28 * Copyright (c) 2019, Klara Inc.
29 * Copyright (c) 2019, Allan Jude
30 * Copyright (c) 2019 Datto Inc.
31 * Copyright (c) 2022 Axcient.
32 */
33
34 #include <sys/arc.h>
35 #include <sys/spa_impl.h>
36 #include <sys/dmu.h>
37 #include <sys/dmu_impl.h>
38 #include <sys/dmu_send.h>
39 #include <sys/dmu_recv.h>
40 #include <sys/dmu_tx.h>
41 #include <sys/dbuf.h>
42 #include <sys/dnode.h>
43 #include <sys/zfs_context.h>
44 #include <sys/dmu_objset.h>
45 #include <sys/dmu_traverse.h>
46 #include <sys/dsl_dataset.h>
47 #include <sys/dsl_dir.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/dsl_pool.h>
50 #include <sys/dsl_synctask.h>
51 #include <sys/zfs_ioctl.h>
52 #include <sys/zap.h>
53 #include <sys/zvol.h>
54 #include <sys/zio_checksum.h>
55 #include <sys/zfs_znode.h>
56 #include <zfs_fletcher.h>
57 #include <sys/avl.h>
58 #include <sys/ddt.h>
59 #include <sys/zfs_onexit.h>
60 #include <sys/dsl_destroy.h>
61 #include <sys/blkptr.h>
62 #include <sys/dsl_bookmark.h>
63 #include <sys/zfeature.h>
64 #include <sys/bqueue.h>
65 #include <sys/objlist.h>
66 #ifdef _KERNEL
67 #include <sys/zfs_vfsops.h>
68 #endif
69 #include <sys/zfs_file.h>
70
71 static uint_t zfs_recv_queue_length = SPA_MAXBLOCKSIZE;
72 static uint_t zfs_recv_queue_ff = 20;
73 static uint_t zfs_recv_write_batch_size = 1024 * 1024;
74 static int zfs_recv_best_effort_corrective = 0;
75
76 static const void *const dmu_recv_tag = "dmu_recv_tag";
77 const char *const recv_clone_name = "%recv";
78
79 static int receive_read_payload_and_next_header(dmu_recv_cookie_t *ra, int len,
80 void *buf);
81
82 struct receive_record_arg {
83 dmu_replay_record_t header;
84 void *payload; /* Pointer to a buffer containing the payload */
85 /*
86 * If the record is a WRITE or SPILL, pointer to the abd containing the
87 * payload.
88 */
89 abd_t *abd;
90 int payload_size;
91 uint64_t bytes_read; /* bytes read from stream when record created */
92 boolean_t eos_marker; /* Marks the end of the stream */
93 bqueue_node_t node;
94 };
95
96 struct receive_writer_arg {
97 objset_t *os;
98 boolean_t byteswap;
99 bqueue_t q;
100
101 /*
102 * These three members are used to signal to the main thread when
103 * we're done.
104 */
105 kmutex_t mutex;
106 kcondvar_t cv;
107 boolean_t done;
108
109 int err;
110 const char *tofs;
111 boolean_t heal;
112 boolean_t resumable;
113 boolean_t raw; /* DMU_BACKUP_FEATURE_RAW set */
114 boolean_t spill; /* DRR_FLAG_SPILL_BLOCK set */
115 boolean_t full; /* this is a full send stream */
116 uint64_t last_object;
117 uint64_t last_offset;
118 uint64_t max_object; /* highest object ID referenced in stream */
119 uint64_t bytes_read; /* bytes read when current record created */
120
121 list_t write_batch;
122
123 /* Encryption parameters for the last received DRR_OBJECT_RANGE */
124 boolean_t or_crypt_params_present;
125 uint64_t or_firstobj;
126 uint64_t or_numslots;
127 uint8_t or_salt[ZIO_DATA_SALT_LEN];
128 uint8_t or_iv[ZIO_DATA_IV_LEN];
129 uint8_t or_mac[ZIO_DATA_MAC_LEN];
130 boolean_t or_byteorder;
131 zio_t *heal_pio;
132 };
133
134 typedef struct dmu_recv_begin_arg {
135 const char *drba_origin;
136 dmu_recv_cookie_t *drba_cookie;
137 cred_t *drba_cred;
138 proc_t *drba_proc;
139 dsl_crypto_params_t *drba_dcp;
140 } dmu_recv_begin_arg_t;
141
142 static void
143 byteswap_record(dmu_replay_record_t *drr)
144 {
145 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
146 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
147 drr->drr_type = BSWAP_32(drr->drr_type);
148 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
149
150 switch (drr->drr_type) {
151 case DRR_BEGIN:
152 DO64(drr_begin.drr_magic);
153 DO64(drr_begin.drr_versioninfo);
154 DO64(drr_begin.drr_creation_time);
155 DO32(drr_begin.drr_type);
156 DO32(drr_begin.drr_flags);
157 DO64(drr_begin.drr_toguid);
158 DO64(drr_begin.drr_fromguid);
159 break;
160 case DRR_OBJECT:
161 DO64(drr_object.drr_object);
162 DO32(drr_object.drr_type);
163 DO32(drr_object.drr_bonustype);
164 DO32(drr_object.drr_blksz);
165 DO32(drr_object.drr_bonuslen);
166 DO32(drr_object.drr_raw_bonuslen);
167 DO64(drr_object.drr_toguid);
168 DO64(drr_object.drr_maxblkid);
169 break;
170 case DRR_FREEOBJECTS:
171 DO64(drr_freeobjects.drr_firstobj);
172 DO64(drr_freeobjects.drr_numobjs);
173 DO64(drr_freeobjects.drr_toguid);
174 break;
175 case DRR_WRITE:
176 DO64(drr_write.drr_object);
177 DO32(drr_write.drr_type);
178 DO64(drr_write.drr_offset);
179 DO64(drr_write.drr_logical_size);
180 DO64(drr_write.drr_toguid);
181 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
182 DO64(drr_write.drr_key.ddk_prop);
183 DO64(drr_write.drr_compressed_size);
184 break;
185 case DRR_WRITE_EMBEDDED:
186 DO64(drr_write_embedded.drr_object);
187 DO64(drr_write_embedded.drr_offset);
188 DO64(drr_write_embedded.drr_length);
189 DO64(drr_write_embedded.drr_toguid);
190 DO32(drr_write_embedded.drr_lsize);
191 DO32(drr_write_embedded.drr_psize);
192 break;
193 case DRR_FREE:
194 DO64(drr_free.drr_object);
195 DO64(drr_free.drr_offset);
196 DO64(drr_free.drr_length);
197 DO64(drr_free.drr_toguid);
198 break;
199 case DRR_SPILL:
200 DO64(drr_spill.drr_object);
201 DO64(drr_spill.drr_length);
202 DO64(drr_spill.drr_toguid);
203 DO64(drr_spill.drr_compressed_size);
204 DO32(drr_spill.drr_type);
205 break;
206 case DRR_OBJECT_RANGE:
207 DO64(drr_object_range.drr_firstobj);
208 DO64(drr_object_range.drr_numslots);
209 DO64(drr_object_range.drr_toguid);
210 break;
211 case DRR_REDACT:
212 DO64(drr_redact.drr_object);
213 DO64(drr_redact.drr_offset);
214 DO64(drr_redact.drr_length);
215 DO64(drr_redact.drr_toguid);
216 break;
217 case DRR_END:
218 DO64(drr_end.drr_toguid);
219 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
220 break;
221 default:
222 break;
223 }
224
225 if (drr->drr_type != DRR_BEGIN) {
226 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
227 }
228
229 #undef DO64
230 #undef DO32
231 }
232
233 static boolean_t
234 redact_snaps_contains(uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
235 {
236 for (int i = 0; i < num_snaps; i++) {
237 if (snaps[i] == guid)
238 return (B_TRUE);
239 }
240 return (B_FALSE);
241 }
242
243 /*
244 * Check that the new stream we're trying to receive is redacted with respect to
245 * a subset of the snapshots that the origin was redacted with respect to. For
246 * the reasons behind this, see the man page on redacted zfs sends and receives.
247 */
248 static boolean_t
249 compatible_redact_snaps(uint64_t *origin_snaps, uint64_t origin_num_snaps,
250 uint64_t *redact_snaps, uint64_t num_redact_snaps)
251 {
252 /*
253 * Short circuit the comparison; if we are redacted with respect to
254 * more snapshots than the origin, we can't be redacted with respect
255 * to a subset.
256 */
257 if (num_redact_snaps > origin_num_snaps) {
258 return (B_FALSE);
259 }
260
261 for (int i = 0; i < num_redact_snaps; i++) {
262 if (!redact_snaps_contains(origin_snaps, origin_num_snaps,
263 redact_snaps[i])) {
264 return (B_FALSE);
265 }
266 }
267 return (B_TRUE);
268 }
269
270 static boolean_t
271 redact_check(dmu_recv_begin_arg_t *drba, dsl_dataset_t *origin)
272 {
273 uint64_t *origin_snaps;
274 uint64_t origin_num_snaps;
275 dmu_recv_cookie_t *drc = drba->drba_cookie;
276 struct drr_begin *drrb = drc->drc_drrb;
277 int featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
278 int err = 0;
279 boolean_t ret = B_TRUE;
280 uint64_t *redact_snaps;
281 uint_t numredactsnaps;
282
283 /*
284 * If this is a full send stream, we're safe no matter what.
285 */
286 if (drrb->drr_fromguid == 0)
287 return (ret);
288
289 VERIFY(dsl_dataset_get_uint64_array_feature(origin,
290 SPA_FEATURE_REDACTED_DATASETS, &origin_num_snaps, &origin_snaps));
291
292 if (nvlist_lookup_uint64_array(drc->drc_begin_nvl,
293 BEGINNV_REDACT_FROM_SNAPS, &redact_snaps, &numredactsnaps) ==
294 0) {
295 /*
296 * If the send stream was sent from the redaction bookmark or
297 * the redacted version of the dataset, then we're safe. Verify
298 * that this is from the a compatible redaction bookmark or
299 * redacted dataset.
300 */
301 if (!compatible_redact_snaps(origin_snaps, origin_num_snaps,
302 redact_snaps, numredactsnaps)) {
303 err = EINVAL;
304 }
305 } else if (featureflags & DMU_BACKUP_FEATURE_REDACTED) {
306 /*
307 * If the stream is redacted, it must be redacted with respect
308 * to a subset of what the origin is redacted with respect to.
309 * See case number 2 in the zfs man page section on redacted zfs
310 * send.
311 */
312 err = nvlist_lookup_uint64_array(drc->drc_begin_nvl,
313 BEGINNV_REDACT_SNAPS, &redact_snaps, &numredactsnaps);
314
315 if (err != 0 || !compatible_redact_snaps(origin_snaps,
316 origin_num_snaps, redact_snaps, numredactsnaps)) {
317 err = EINVAL;
318 }
319 } else if (!redact_snaps_contains(origin_snaps, origin_num_snaps,
320 drrb->drr_toguid)) {
321 /*
322 * If the stream isn't redacted but the origin is, this must be
323 * one of the snapshots the origin is redacted with respect to.
324 * See case number 1 in the zfs man page section on redacted zfs
325 * send.
326 */
327 err = EINVAL;
328 }
329
330 if (err != 0)
331 ret = B_FALSE;
332 return (ret);
333 }
334
335 /*
336 * If we previously received a stream with --large-block, we don't support
337 * receiving an incremental on top of it without --large-block. This avoids
338 * forcing a read-modify-write or trying to re-aggregate a string of WRITE
339 * records.
340 */
341 static int
342 recv_check_large_blocks(dsl_dataset_t *ds, uint64_t featureflags)
343 {
344 if (dsl_dataset_feature_is_active(ds, SPA_FEATURE_LARGE_BLOCKS) &&
345 !(featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS))
346 return (SET_ERROR(ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH));
347 return (0);
348 }
349
350 static int
351 recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
352 uint64_t fromguid, uint64_t featureflags)
353 {
354 uint64_t obj;
355 uint64_t children;
356 int error;
357 dsl_dataset_t *snap;
358 dsl_pool_t *dp = ds->ds_dir->dd_pool;
359 boolean_t encrypted = ds->ds_dir->dd_crypto_obj != 0;
360 boolean_t raw = (featureflags & DMU_BACKUP_FEATURE_RAW) != 0;
361 boolean_t embed = (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) != 0;
362
363 /* Temporary clone name must not exist. */
364 error = zap_lookup(dp->dp_meta_objset,
365 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
366 8, 1, &obj);
367 if (error != ENOENT)
368 return (error == 0 ? SET_ERROR(EBUSY) : error);
369
370 /* Resume state must not be set. */
371 if (dsl_dataset_has_resume_receive_state(ds))
372 return (SET_ERROR(EBUSY));
373
374 /* New snapshot name must not exist if we're not healing it. */
375 error = zap_lookup(dp->dp_meta_objset,
376 dsl_dataset_phys(ds)->ds_snapnames_zapobj,
377 drba->drba_cookie->drc_tosnap, 8, 1, &obj);
378 if (drba->drba_cookie->drc_heal) {
379 if (error != 0)
380 return (error);
381 } else if (error != ENOENT) {
382 return (error == 0 ? SET_ERROR(EEXIST) : error);
383 }
384
385 /* Must not have children if receiving a ZVOL. */
386 error = zap_count(dp->dp_meta_objset,
387 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &children);
388 if (error != 0)
389 return (error);
390 if (drba->drba_cookie->drc_drrb->drr_type != DMU_OST_ZFS &&
391 children > 0)
392 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
393
394 /*
395 * Check snapshot limit before receiving. We'll recheck again at the
396 * end, but might as well abort before receiving if we're already over
397 * the limit.
398 *
399 * Note that we do not check the file system limit with
400 * dsl_dir_fscount_check because the temporary %clones don't count
401 * against that limit.
402 */
403 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
404 NULL, drba->drba_cred, drba->drba_proc);
405 if (error != 0)
406 return (error);
407
408 if (drba->drba_cookie->drc_heal) {
409 /* Encryption is incompatible with embedded data. */
410 if (encrypted && embed)
411 return (SET_ERROR(EINVAL));
412
413 /* Healing is not supported when in 'force' mode. */
414 if (drba->drba_cookie->drc_force)
415 return (SET_ERROR(EINVAL));
416
417 /* Must have keys loaded if doing encrypted non-raw recv. */
418 if (encrypted && !raw) {
419 if (spa_keystore_lookup_key(dp->dp_spa, ds->ds_object,
420 NULL, NULL) != 0)
421 return (SET_ERROR(EACCES));
422 }
423
424 error = dsl_dataset_hold_obj(dp, obj, FTAG, &snap);
425 if (error != 0)
426 return (error);
427
428 /*
429 * When not doing best effort corrective recv healing can only
430 * be done if the send stream is for the same snapshot as the
431 * one we are trying to heal.
432 */
433 if (zfs_recv_best_effort_corrective == 0 &&
434 drba->drba_cookie->drc_drrb->drr_toguid !=
435 dsl_dataset_phys(snap)->ds_guid) {
436 dsl_dataset_rele(snap, FTAG);
437 return (SET_ERROR(ENOTSUP));
438 }
439 dsl_dataset_rele(snap, FTAG);
440 } else if (fromguid != 0) {
441 /* Sanity check the incremental recv */
442 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
443
444 /* Can't perform a raw receive on top of a non-raw receive */
445 if (!encrypted && raw)
446 return (SET_ERROR(EINVAL));
447
448 /* Encryption is incompatible with embedded data */
449 if (encrypted && embed)
450 return (SET_ERROR(EINVAL));
451
452 /* Find snapshot in this dir that matches fromguid. */
453 while (obj != 0) {
454 error = dsl_dataset_hold_obj(dp, obj, FTAG,
455 &snap);
456 if (error != 0)
457 return (SET_ERROR(ENODEV));
458 if (snap->ds_dir != ds->ds_dir) {
459 dsl_dataset_rele(snap, FTAG);
460 return (SET_ERROR(ENODEV));
461 }
462 if (dsl_dataset_phys(snap)->ds_guid == fromguid)
463 break;
464 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
465 dsl_dataset_rele(snap, FTAG);
466 }
467 if (obj == 0)
468 return (SET_ERROR(ENODEV));
469
470 if (drba->drba_cookie->drc_force) {
471 drba->drba_cookie->drc_fromsnapobj = obj;
472 } else {
473 /*
474 * If we are not forcing, there must be no
475 * changes since fromsnap. Raw sends have an
476 * additional constraint that requires that
477 * no "noop" snapshots exist between fromsnap
478 * and tosnap for the IVset checking code to
479 * work properly.
480 */
481 if (dsl_dataset_modified_since_snap(ds, snap) ||
482 (raw &&
483 dsl_dataset_phys(ds)->ds_prev_snap_obj !=
484 snap->ds_object)) {
485 dsl_dataset_rele(snap, FTAG);
486 return (SET_ERROR(ETXTBSY));
487 }
488 drba->drba_cookie->drc_fromsnapobj =
489 ds->ds_prev->ds_object;
490 }
491
492 if (dsl_dataset_feature_is_active(snap,
493 SPA_FEATURE_REDACTED_DATASETS) && !redact_check(drba,
494 snap)) {
495 dsl_dataset_rele(snap, FTAG);
496 return (SET_ERROR(EINVAL));
497 }
498
499 error = recv_check_large_blocks(snap, featureflags);
500 if (error != 0) {
501 dsl_dataset_rele(snap, FTAG);
502 return (error);
503 }
504
505 dsl_dataset_rele(snap, FTAG);
506 } else {
507 /* If full and not healing then must be forced. */
508 if (!drba->drba_cookie->drc_force)
509 return (SET_ERROR(EEXIST));
510
511 /*
512 * We don't support using zfs recv -F to blow away
513 * encrypted filesystems. This would require the
514 * dsl dir to point to the old encryption key and
515 * the new one at the same time during the receive.
516 */
517 if ((!encrypted && raw) || encrypted)
518 return (SET_ERROR(EINVAL));
519
520 /*
521 * Perform the same encryption checks we would if
522 * we were creating a new dataset from scratch.
523 */
524 if (!raw) {
525 boolean_t will_encrypt;
526
527 error = dmu_objset_create_crypt_check(
528 ds->ds_dir->dd_parent, drba->drba_dcp,
529 &will_encrypt);
530 if (error != 0)
531 return (error);
532
533 if (will_encrypt && embed)
534 return (SET_ERROR(EINVAL));
535 }
536 }
537
538 return (0);
539 }
540
541 /*
542 * Check that any feature flags used in the data stream we're receiving are
543 * supported by the pool we are receiving into.
544 *
545 * Note that some of the features we explicitly check here have additional
546 * (implicit) features they depend on, but those dependencies are enforced
547 * through the zfeature_register() calls declaring the features that we
548 * explicitly check.
549 */
550 static int
551 recv_begin_check_feature_flags_impl(uint64_t featureflags, spa_t *spa)
552 {
553 /*
554 * Check if there are any unsupported feature flags.
555 */
556 if (!DMU_STREAM_SUPPORTED(featureflags)) {
557 return (SET_ERROR(ZFS_ERR_UNKNOWN_SEND_STREAM_FEATURE));
558 }
559
560 /* Verify pool version supports SA if SA_SPILL feature set */
561 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
562 spa_version(spa) < SPA_VERSION_SA)
563 return (SET_ERROR(ENOTSUP));
564
565 /*
566 * LZ4 compressed, ZSTD compressed, embedded, mooched, large blocks,
567 * and large_dnodes in the stream can only be used if those pool
568 * features are enabled because we don't attempt to decompress /
569 * un-embed / un-mooch / split up the blocks / dnodes during the
570 * receive process.
571 */
572 if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
573 !spa_feature_is_enabled(spa, SPA_FEATURE_LZ4_COMPRESS))
574 return (SET_ERROR(ENOTSUP));
575 if ((featureflags & DMU_BACKUP_FEATURE_ZSTD) &&
576 !spa_feature_is_enabled(spa, SPA_FEATURE_ZSTD_COMPRESS))
577 return (SET_ERROR(ENOTSUP));
578 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
579 !spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA))
580 return (SET_ERROR(ENOTSUP));
581 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
582 !spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS))
583 return (SET_ERROR(ENOTSUP));
584 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
585 !spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_DNODE))
586 return (SET_ERROR(ENOTSUP));
587
588 /*
589 * Receiving redacted streams requires that redacted datasets are
590 * enabled.
591 */
592 if ((featureflags & DMU_BACKUP_FEATURE_REDACTED) &&
593 !spa_feature_is_enabled(spa, SPA_FEATURE_REDACTED_DATASETS))
594 return (SET_ERROR(ENOTSUP));
595
596 return (0);
597 }
598
599 static int
600 dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
601 {
602 dmu_recv_begin_arg_t *drba = arg;
603 dsl_pool_t *dp = dmu_tx_pool(tx);
604 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
605 uint64_t fromguid = drrb->drr_fromguid;
606 int flags = drrb->drr_flags;
607 ds_hold_flags_t dsflags = DS_HOLD_FLAG_NONE;
608 int error;
609 uint64_t featureflags = drba->drba_cookie->drc_featureflags;
610 dsl_dataset_t *ds;
611 const char *tofs = drba->drba_cookie->drc_tofs;
612
613 /* already checked */
614 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
615 ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
616
617 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
618 DMU_COMPOUNDSTREAM ||
619 drrb->drr_type >= DMU_OST_NUMTYPES ||
620 ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
621 return (SET_ERROR(EINVAL));
622
623 error = recv_begin_check_feature_flags_impl(featureflags, dp->dp_spa);
624 if (error != 0)
625 return (error);
626
627 /* Resumable receives require extensible datasets */
628 if (drba->drba_cookie->drc_resumable &&
629 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
630 return (SET_ERROR(ENOTSUP));
631
632 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
633 /* raw receives require the encryption feature */
634 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION))
635 return (SET_ERROR(ENOTSUP));
636
637 /* embedded data is incompatible with encryption and raw recv */
638 if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
639 return (SET_ERROR(EINVAL));
640
641 /* raw receives require spill block allocation flag */
642 if (!(flags & DRR_FLAG_SPILL_BLOCK))
643 return (SET_ERROR(ZFS_ERR_SPILL_BLOCK_FLAG_MISSING));
644 } else {
645 /*
646 * We support unencrypted datasets below encrypted ones now,
647 * so add the DS_HOLD_FLAG_DECRYPT flag only if we are dealing
648 * with a dataset we may encrypt.
649 */
650 if (drba->drba_dcp == NULL ||
651 drba->drba_dcp->cp_crypt != ZIO_CRYPT_OFF) {
652 dsflags |= DS_HOLD_FLAG_DECRYPT;
653 }
654 }
655
656 error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
657 if (error == 0) {
658 /* target fs already exists; recv into temp clone */
659
660 /* Can't recv a clone into an existing fs */
661 if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
662 dsl_dataset_rele_flags(ds, dsflags, FTAG);
663 return (SET_ERROR(EINVAL));
664 }
665
666 error = recv_begin_check_existing_impl(drba, ds, fromguid,
667 featureflags);
668 dsl_dataset_rele_flags(ds, dsflags, FTAG);
669 } else if (error == ENOENT) {
670 /* target fs does not exist; must be a full backup or clone */
671 char buf[ZFS_MAX_DATASET_NAME_LEN];
672 objset_t *os;
673
674 /* healing recv must be done "into" an existing snapshot */
675 if (drba->drba_cookie->drc_heal == B_TRUE)
676 return (SET_ERROR(ENOTSUP));
677
678 /*
679 * If it's a non-clone incremental, we are missing the
680 * target fs, so fail the recv.
681 */
682 if (fromguid != 0 && !((flags & DRR_FLAG_CLONE) ||
683 drba->drba_origin))
684 return (SET_ERROR(ENOENT));
685
686 /*
687 * If we're receiving a full send as a clone, and it doesn't
688 * contain all the necessary free records and freeobject
689 * records, reject it.
690 */
691 if (fromguid == 0 && drba->drba_origin != NULL &&
692 !(flags & DRR_FLAG_FREERECORDS))
693 return (SET_ERROR(EINVAL));
694
695 /* Open the parent of tofs */
696 ASSERT3U(strlen(tofs), <, sizeof (buf));
697 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
698 error = dsl_dataset_hold(dp, buf, FTAG, &ds);
699 if (error != 0)
700 return (error);
701
702 if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0 &&
703 drba->drba_origin == NULL) {
704 boolean_t will_encrypt;
705
706 /*
707 * Check that we aren't breaking any encryption rules
708 * and that we have all the parameters we need to
709 * create an encrypted dataset if necessary. If we are
710 * making an encrypted dataset the stream can't have
711 * embedded data.
712 */
713 error = dmu_objset_create_crypt_check(ds->ds_dir,
714 drba->drba_dcp, &will_encrypt);
715 if (error != 0) {
716 dsl_dataset_rele(ds, FTAG);
717 return (error);
718 }
719
720 if (will_encrypt &&
721 (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)) {
722 dsl_dataset_rele(ds, FTAG);
723 return (SET_ERROR(EINVAL));
724 }
725 }
726
727 /*
728 * Check filesystem and snapshot limits before receiving. We'll
729 * recheck snapshot limits again at the end (we create the
730 * filesystems and increment those counts during begin_sync).
731 */
732 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
733 ZFS_PROP_FILESYSTEM_LIMIT, NULL,
734 drba->drba_cred, drba->drba_proc);
735 if (error != 0) {
736 dsl_dataset_rele(ds, FTAG);
737 return (error);
738 }
739
740 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
741 ZFS_PROP_SNAPSHOT_LIMIT, NULL,
742 drba->drba_cred, drba->drba_proc);
743 if (error != 0) {
744 dsl_dataset_rele(ds, FTAG);
745 return (error);
746 }
747
748 /* can't recv below anything but filesystems (eg. no ZVOLs) */
749 error = dmu_objset_from_ds(ds, &os);
750 if (error != 0) {
751 dsl_dataset_rele(ds, FTAG);
752 return (error);
753 }
754 if (dmu_objset_type(os) != DMU_OST_ZFS) {
755 dsl_dataset_rele(ds, FTAG);
756 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
757 }
758
759 if (drba->drba_origin != NULL) {
760 dsl_dataset_t *origin;
761 error = dsl_dataset_hold_flags(dp, drba->drba_origin,
762 dsflags, FTAG, &origin);
763 if (error != 0) {
764 dsl_dataset_rele(ds, FTAG);
765 return (error);
766 }
767 if (!origin->ds_is_snapshot) {
768 dsl_dataset_rele_flags(origin, dsflags, FTAG);
769 dsl_dataset_rele(ds, FTAG);
770 return (SET_ERROR(EINVAL));
771 }
772 if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
773 fromguid != 0) {
774 dsl_dataset_rele_flags(origin, dsflags, FTAG);
775 dsl_dataset_rele(ds, FTAG);
776 return (SET_ERROR(ENODEV));
777 }
778
779 if (origin->ds_dir->dd_crypto_obj != 0 &&
780 (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)) {
781 dsl_dataset_rele_flags(origin, dsflags, FTAG);
782 dsl_dataset_rele(ds, FTAG);
783 return (SET_ERROR(EINVAL));
784 }
785
786 /*
787 * If the origin is redacted we need to verify that this
788 * send stream can safely be received on top of the
789 * origin.
790 */
791 if (dsl_dataset_feature_is_active(origin,
792 SPA_FEATURE_REDACTED_DATASETS)) {
793 if (!redact_check(drba, origin)) {
794 dsl_dataset_rele_flags(origin, dsflags,
795 FTAG);
796 dsl_dataset_rele_flags(ds, dsflags,
797 FTAG);
798 return (SET_ERROR(EINVAL));
799 }
800 }
801
802 error = recv_check_large_blocks(ds, featureflags);
803 if (error != 0) {
804 dsl_dataset_rele_flags(origin, dsflags, FTAG);
805 dsl_dataset_rele_flags(ds, dsflags, FTAG);
806 return (error);
807 }
808
809 dsl_dataset_rele_flags(origin, dsflags, FTAG);
810 }
811
812 dsl_dataset_rele(ds, FTAG);
813 error = 0;
814 }
815 return (error);
816 }
817
818 static void
819 dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
820 {
821 dmu_recv_begin_arg_t *drba = arg;
822 dsl_pool_t *dp = dmu_tx_pool(tx);
823 objset_t *mos = dp->dp_meta_objset;
824 dmu_recv_cookie_t *drc = drba->drba_cookie;
825 struct drr_begin *drrb = drc->drc_drrb;
826 const char *tofs = drc->drc_tofs;
827 uint64_t featureflags = drc->drc_featureflags;
828 dsl_dataset_t *ds, *newds;
829 objset_t *os;
830 uint64_t dsobj;
831 ds_hold_flags_t dsflags = DS_HOLD_FLAG_NONE;
832 int error;
833 uint64_t crflags = 0;
834 dsl_crypto_params_t dummy_dcp = { 0 };
835 dsl_crypto_params_t *dcp = drba->drba_dcp;
836
837 if (drrb->drr_flags & DRR_FLAG_CI_DATA)
838 crflags |= DS_FLAG_CI_DATASET;
839
840 if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0)
841 dsflags |= DS_HOLD_FLAG_DECRYPT;
842
843 /*
844 * Raw, non-incremental recvs always use a dummy dcp with
845 * the raw cmd set. Raw incremental recvs do not use a dcp
846 * since the encryption parameters are already set in stone.
847 */
848 if (dcp == NULL && drrb->drr_fromguid == 0 &&
849 drba->drba_origin == NULL) {
850 ASSERT3P(dcp, ==, NULL);
851 dcp = &dummy_dcp;
852
853 if (featureflags & DMU_BACKUP_FEATURE_RAW)
854 dcp->cp_cmd = DCP_CMD_RAW_RECV;
855 }
856
857 error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
858 if (error == 0) {
859 /* Create temporary clone unless we're doing corrective recv */
860 dsl_dataset_t *snap = NULL;
861
862 if (drba->drba_cookie->drc_fromsnapobj != 0) {
863 VERIFY0(dsl_dataset_hold_obj(dp,
864 drba->drba_cookie->drc_fromsnapobj, FTAG, &snap));
865 ASSERT3P(dcp, ==, NULL);
866 }
867 if (drc->drc_heal) {
868 /* When healing we want to use the provided snapshot */
869 VERIFY0(dsl_dataset_snap_lookup(ds, drc->drc_tosnap,
870 &dsobj));
871 } else {
872 dsobj = dsl_dataset_create_sync(ds->ds_dir,
873 recv_clone_name, snap, crflags, drba->drba_cred,
874 dcp, tx);
875 }
876 if (drba->drba_cookie->drc_fromsnapobj != 0)
877 dsl_dataset_rele(snap, FTAG);
878 dsl_dataset_rele_flags(ds, dsflags, FTAG);
879 } else {
880 dsl_dir_t *dd;
881 const char *tail;
882 dsl_dataset_t *origin = NULL;
883
884 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
885
886 if (drba->drba_origin != NULL) {
887 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
888 FTAG, &origin));
889 ASSERT3P(dcp, ==, NULL);
890 }
891
892 /* Create new dataset. */
893 dsobj = dsl_dataset_create_sync(dd, strrchr(tofs, '/') + 1,
894 origin, crflags, drba->drba_cred, dcp, tx);
895 if (origin != NULL)
896 dsl_dataset_rele(origin, FTAG);
897 dsl_dir_rele(dd, FTAG);
898 drc->drc_newfs = B_TRUE;
899 }
900 VERIFY0(dsl_dataset_own_obj_force(dp, dsobj, dsflags, dmu_recv_tag,
901 &newds));
902 if (dsl_dataset_feature_is_active(newds,
903 SPA_FEATURE_REDACTED_DATASETS)) {
904 /*
905 * If the origin dataset is redacted, the child will be redacted
906 * when we create it. We clear the new dataset's
907 * redaction info; if it should be redacted, we'll fill
908 * in its information later.
909 */
910 dsl_dataset_deactivate_feature(newds,
911 SPA_FEATURE_REDACTED_DATASETS, tx);
912 }
913 VERIFY0(dmu_objset_from_ds(newds, &os));
914
915 if (drc->drc_resumable) {
916 dsl_dataset_zapify(newds, tx);
917 if (drrb->drr_fromguid != 0) {
918 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
919 8, 1, &drrb->drr_fromguid, tx));
920 }
921 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
922 8, 1, &drrb->drr_toguid, tx));
923 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
924 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
925 uint64_t one = 1;
926 uint64_t zero = 0;
927 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
928 8, 1, &one, tx));
929 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
930 8, 1, &zero, tx));
931 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
932 8, 1, &zero, tx));
933 if (featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
934 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
935 8, 1, &one, tx));
936 }
937 if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) {
938 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
939 8, 1, &one, tx));
940 }
941 if (featureflags & DMU_BACKUP_FEATURE_COMPRESSED) {
942 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
943 8, 1, &one, tx));
944 }
945 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
946 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_RAWOK,
947 8, 1, &one, tx));
948 }
949
950 uint64_t *redact_snaps;
951 uint_t numredactsnaps;
952 if (nvlist_lookup_uint64_array(drc->drc_begin_nvl,
953 BEGINNV_REDACT_FROM_SNAPS, &redact_snaps,
954 &numredactsnaps) == 0) {
955 VERIFY0(zap_add(mos, dsobj,
956 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS,
957 sizeof (*redact_snaps), numredactsnaps,
958 redact_snaps, tx));
959 }
960 }
961
962 /*
963 * Usually the os->os_encrypted value is tied to the presence of a
964 * DSL Crypto Key object in the dd. However, that will not be received
965 * until dmu_recv_stream(), so we set the value manually for now.
966 */
967 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
968 os->os_encrypted = B_TRUE;
969 drba->drba_cookie->drc_raw = B_TRUE;
970 }
971
972 if (featureflags & DMU_BACKUP_FEATURE_REDACTED) {
973 uint64_t *redact_snaps;
974 uint_t numredactsnaps;
975 VERIFY0(nvlist_lookup_uint64_array(drc->drc_begin_nvl,
976 BEGINNV_REDACT_SNAPS, &redact_snaps, &numredactsnaps));
977 dsl_dataset_activate_redaction(newds, redact_snaps,
978 numredactsnaps, tx);
979 }
980
981 dmu_buf_will_dirty(newds->ds_dbuf, tx);
982 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
983
984 /*
985 * If we actually created a non-clone, we need to create the objset
986 * in our new dataset. If this is a raw send we postpone this until
987 * dmu_recv_stream() so that we can allocate the metadnode with the
988 * properties from the DRR_BEGIN payload.
989 */
990 rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
991 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds)) &&
992 (featureflags & DMU_BACKUP_FEATURE_RAW) == 0 &&
993 !drc->drc_heal) {
994 (void) dmu_objset_create_impl(dp->dp_spa,
995 newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
996 }
997 rrw_exit(&newds->ds_bp_rwlock, FTAG);
998
999 drba->drba_cookie->drc_ds = newds;
1000 drba->drba_cookie->drc_os = os;
1001
1002 spa_history_log_internal_ds(newds, "receive", tx, " ");
1003 }
1004
1005 static int
1006 dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1007 {
1008 dmu_recv_begin_arg_t *drba = arg;
1009 dmu_recv_cookie_t *drc = drba->drba_cookie;
1010 dsl_pool_t *dp = dmu_tx_pool(tx);
1011 struct drr_begin *drrb = drc->drc_drrb;
1012 int error;
1013 ds_hold_flags_t dsflags = DS_HOLD_FLAG_NONE;
1014 dsl_dataset_t *ds;
1015 const char *tofs = drc->drc_tofs;
1016
1017 /* already checked */
1018 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1019 ASSERT(drc->drc_featureflags & DMU_BACKUP_FEATURE_RESUMING);
1020
1021 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1022 DMU_COMPOUNDSTREAM ||
1023 drrb->drr_type >= DMU_OST_NUMTYPES)
1024 return (SET_ERROR(EINVAL));
1025
1026 /*
1027 * This is mostly a sanity check since we should have already done these
1028 * checks during a previous attempt to receive the data.
1029 */
1030 error = recv_begin_check_feature_flags_impl(drc->drc_featureflags,
1031 dp->dp_spa);
1032 if (error != 0)
1033 return (error);
1034
1035 /* 6 extra bytes for /%recv */
1036 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1037
1038 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1039 tofs, recv_clone_name);
1040
1041 if (drc->drc_featureflags & DMU_BACKUP_FEATURE_RAW) {
1042 /* raw receives require spill block allocation flag */
1043 if (!(drrb->drr_flags & DRR_FLAG_SPILL_BLOCK))
1044 return (SET_ERROR(ZFS_ERR_SPILL_BLOCK_FLAG_MISSING));
1045 } else {
1046 dsflags |= DS_HOLD_FLAG_DECRYPT;
1047 }
1048
1049 boolean_t recvexist = B_TRUE;
1050 if (dsl_dataset_hold_flags(dp, recvname, dsflags, FTAG, &ds) != 0) {
1051 /* %recv does not exist; continue in tofs */
1052 recvexist = B_FALSE;
1053 error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
1054 if (error != 0)
1055 return (error);
1056 }
1057
1058 /*
1059 * Resume of full/newfs recv on existing dataset should be done with
1060 * force flag
1061 */
1062 if (recvexist && drrb->drr_fromguid == 0 && !drc->drc_force) {
1063 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1064 return (SET_ERROR(ZFS_ERR_RESUME_EXISTS));
1065 }
1066
1067 /* check that ds is marked inconsistent */
1068 if (!DS_IS_INCONSISTENT(ds)) {
1069 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1070 return (SET_ERROR(EINVAL));
1071 }
1072
1073 /* check that there is resuming data, and that the toguid matches */
1074 if (!dsl_dataset_is_zapified(ds)) {
1075 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1076 return (SET_ERROR(EINVAL));
1077 }
1078 uint64_t val;
1079 error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1080 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1081 if (error != 0 || drrb->drr_toguid != val) {
1082 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1083 return (SET_ERROR(EINVAL));
1084 }
1085
1086 /*
1087 * Check if the receive is still running. If so, it will be owned.
1088 * Note that nothing else can own the dataset (e.g. after the receive
1089 * fails) because it will be marked inconsistent.
1090 */
1091 if (dsl_dataset_has_owner(ds)) {
1092 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1093 return (SET_ERROR(EBUSY));
1094 }
1095
1096 /* There should not be any snapshots of this fs yet. */
1097 if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1098 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1099 return (SET_ERROR(EINVAL));
1100 }
1101
1102 /*
1103 * Note: resume point will be checked when we process the first WRITE
1104 * record.
1105 */
1106
1107 /* check that the origin matches */
1108 val = 0;
1109 (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1110 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1111 if (drrb->drr_fromguid != val) {
1112 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1113 return (SET_ERROR(EINVAL));
1114 }
1115
1116 if (ds->ds_prev != NULL && drrb->drr_fromguid != 0)
1117 drc->drc_fromsnapobj = ds->ds_prev->ds_object;
1118
1119 /*
1120 * If we're resuming, and the send is redacted, then the original send
1121 * must have been redacted, and must have been redacted with respect to
1122 * the same snapshots.
1123 */
1124 if (drc->drc_featureflags & DMU_BACKUP_FEATURE_REDACTED) {
1125 uint64_t num_ds_redact_snaps;
1126 uint64_t *ds_redact_snaps;
1127
1128 uint_t num_stream_redact_snaps;
1129 uint64_t *stream_redact_snaps;
1130
1131 if (nvlist_lookup_uint64_array(drc->drc_begin_nvl,
1132 BEGINNV_REDACT_SNAPS, &stream_redact_snaps,
1133 &num_stream_redact_snaps) != 0) {
1134 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1135 return (SET_ERROR(EINVAL));
1136 }
1137
1138 if (!dsl_dataset_get_uint64_array_feature(ds,
1139 SPA_FEATURE_REDACTED_DATASETS, &num_ds_redact_snaps,
1140 &ds_redact_snaps)) {
1141 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1142 return (SET_ERROR(EINVAL));
1143 }
1144
1145 for (int i = 0; i < num_ds_redact_snaps; i++) {
1146 if (!redact_snaps_contains(ds_redact_snaps,
1147 num_ds_redact_snaps, stream_redact_snaps[i])) {
1148 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1149 return (SET_ERROR(EINVAL));
1150 }
1151 }
1152 }
1153
1154 error = recv_check_large_blocks(ds, drc->drc_featureflags);
1155 if (error != 0) {
1156 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1157 return (error);
1158 }
1159
1160 dsl_dataset_rele_flags(ds, dsflags, FTAG);
1161 return (0);
1162 }
1163
1164 static void
1165 dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1166 {
1167 dmu_recv_begin_arg_t *drba = arg;
1168 dsl_pool_t *dp = dmu_tx_pool(tx);
1169 const char *tofs = drba->drba_cookie->drc_tofs;
1170 uint64_t featureflags = drba->drba_cookie->drc_featureflags;
1171 dsl_dataset_t *ds;
1172 ds_hold_flags_t dsflags = DS_HOLD_FLAG_NONE;
1173 /* 6 extra bytes for /%recv */
1174 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1175
1176 (void) snprintf(recvname, sizeof (recvname), "%s/%s", tofs,
1177 recv_clone_name);
1178
1179 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
1180 drba->drba_cookie->drc_raw = B_TRUE;
1181 } else {
1182 dsflags |= DS_HOLD_FLAG_DECRYPT;
1183 }
1184
1185 if (dsl_dataset_own_force(dp, recvname, dsflags, dmu_recv_tag, &ds)
1186 != 0) {
1187 /* %recv does not exist; continue in tofs */
1188 VERIFY0(dsl_dataset_own_force(dp, tofs, dsflags, dmu_recv_tag,
1189 &ds));
1190 drba->drba_cookie->drc_newfs = B_TRUE;
1191 }
1192
1193 ASSERT(DS_IS_INCONSISTENT(ds));
1194 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1195 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)) ||
1196 drba->drba_cookie->drc_raw);
1197 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1198
1199 drba->drba_cookie->drc_ds = ds;
1200 VERIFY0(dmu_objset_from_ds(ds, &drba->drba_cookie->drc_os));
1201 drba->drba_cookie->drc_should_save = B_TRUE;
1202
1203 spa_history_log_internal_ds(ds, "resume receive", tx, " ");
1204 }
1205
1206 /*
1207 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1208 * succeeds; otherwise we will leak the holds on the datasets.
1209 */
1210 int
1211 dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
1212 boolean_t force, boolean_t heal, boolean_t resumable, nvlist_t *localprops,
1213 nvlist_t *hidden_args, char *origin, dmu_recv_cookie_t *drc,
1214 zfs_file_t *fp, offset_t *voffp)
1215 {
1216 dmu_recv_begin_arg_t drba = { 0 };
1217 int err;
1218
1219 memset(drc, 0, sizeof (dmu_recv_cookie_t));
1220 drc->drc_drr_begin = drr_begin;
1221 drc->drc_drrb = &drr_begin->drr_u.drr_begin;
1222 drc->drc_tosnap = tosnap;
1223 drc->drc_tofs = tofs;
1224 drc->drc_force = force;
1225 drc->drc_heal = heal;
1226 drc->drc_resumable = resumable;
1227 drc->drc_cred = CRED();
1228 drc->drc_proc = curproc;
1229 drc->drc_clone = (origin != NULL);
1230
1231 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1232 drc->drc_byteswap = B_TRUE;
1233 (void) fletcher_4_incremental_byteswap(drr_begin,
1234 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1235 byteswap_record(drr_begin);
1236 } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1237 (void) fletcher_4_incremental_native(drr_begin,
1238 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1239 } else {
1240 return (SET_ERROR(EINVAL));
1241 }
1242
1243 drc->drc_fp = fp;
1244 drc->drc_voff = *voffp;
1245 drc->drc_featureflags =
1246 DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
1247
1248 uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
1249 void *payload = NULL;
1250
1251 /*
1252 * Since OpenZFS 2.0.0, we have enforced a 64MB limit in userspace
1253 * configurable via ZFS_SENDRECV_MAX_NVLIST. We enforce 256MB as a hard
1254 * upper limit. Systems with less than 1GB of RAM will see a lower
1255 * limit from `arc_all_memory() / 4`.
1256 */
1257 if (payloadlen > (MIN((1U << 28), arc_all_memory() / 4)))
1258 return (E2BIG);
1259
1260 if (payloadlen != 0)
1261 payload = vmem_alloc(payloadlen, KM_SLEEP);
1262
1263 err = receive_read_payload_and_next_header(drc, payloadlen,
1264 payload);
1265 if (err != 0) {
1266 vmem_free(payload, payloadlen);
1267 return (err);
1268 }
1269 if (payloadlen != 0) {
1270 err = nvlist_unpack(payload, payloadlen, &drc->drc_begin_nvl,
1271 KM_SLEEP);
1272 vmem_free(payload, payloadlen);
1273 if (err != 0) {
1274 kmem_free(drc->drc_next_rrd,
1275 sizeof (*drc->drc_next_rrd));
1276 return (err);
1277 }
1278 }
1279
1280 if (drc->drc_drrb->drr_flags & DRR_FLAG_SPILL_BLOCK)
1281 drc->drc_spill = B_TRUE;
1282
1283 drba.drba_origin = origin;
1284 drba.drba_cookie = drc;
1285 drba.drba_cred = CRED();
1286 drba.drba_proc = curproc;
1287
1288 if (drc->drc_featureflags & DMU_BACKUP_FEATURE_RESUMING) {
1289 err = dsl_sync_task(tofs,
1290 dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
1291 &drba, 5, ZFS_SPACE_CHECK_NORMAL);
1292 } else {
1293 /*
1294 * For non-raw, non-incremental, non-resuming receives the
1295 * user can specify encryption parameters on the command line
1296 * with "zfs recv -o". For these receives we create a dcp and
1297 * pass it to the sync task. Creating the dcp will implicitly
1298 * remove the encryption params from the localprops nvlist,
1299 * which avoids errors when trying to set these normally
1300 * read-only properties. Any other kind of receive that
1301 * attempts to set these properties will fail as a result.
1302 */
1303 if ((DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1304 DMU_BACKUP_FEATURE_RAW) == 0 &&
1305 origin == NULL && drc->drc_drrb->drr_fromguid == 0) {
1306 err = dsl_crypto_params_create_nvlist(DCP_CMD_NONE,
1307 localprops, hidden_args, &drba.drba_dcp);
1308 }
1309
1310 if (err == 0) {
1311 err = dsl_sync_task(tofs,
1312 dmu_recv_begin_check, dmu_recv_begin_sync,
1313 &drba, 5, ZFS_SPACE_CHECK_NORMAL);
1314 dsl_crypto_params_free(drba.drba_dcp, !!err);
1315 }
1316 }
1317
1318 if (err != 0) {
1319 kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
1320 nvlist_free(drc->drc_begin_nvl);
1321 }
1322 return (err);
1323 }
1324
1325 /*
1326 * Holds data need for corrective recv callback
1327 */
1328 typedef struct cr_cb_data {
1329 uint64_t size;
1330 zbookmark_phys_t zb;
1331 spa_t *spa;
1332 } cr_cb_data_t;
1333
1334 static void
1335 corrective_read_done(zio_t *zio)
1336 {
1337 cr_cb_data_t *data = zio->io_private;
1338 /* Corruption corrected; update error log if needed */
1339 if (zio->io_error == 0)
1340 spa_remove_error(data->spa, &data->zb);
1341 kmem_free(data, sizeof (cr_cb_data_t));
1342 abd_free(zio->io_abd);
1343 }
1344
1345 /*
1346 * zio_rewrite the data pointed to by bp with the data from the rrd's abd.
1347 */
1348 static int
1349 do_corrective_recv(struct receive_writer_arg *rwa, struct drr_write *drrw,
1350 struct receive_record_arg *rrd, blkptr_t *bp)
1351 {
1352 int err;
1353 zio_t *io;
1354 zbookmark_phys_t zb;
1355 dnode_t *dn;
1356 abd_t *abd = rrd->abd;
1357 zio_cksum_t bp_cksum = bp->blk_cksum;
1358 zio_flag_t flags = ZIO_FLAG_SPECULATIVE |
1359 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_CANFAIL;
1360
1361 if (rwa->raw)
1362 flags |= ZIO_FLAG_RAW;
1363
1364 err = dnode_hold(rwa->os, drrw->drr_object, FTAG, &dn);
1365 if (err != 0)
1366 return (err);
1367 SET_BOOKMARK(&zb, dmu_objset_id(rwa->os), drrw->drr_object, 0,
1368 dbuf_whichblock(dn, 0, drrw->drr_offset));
1369 dnode_rele(dn, FTAG);
1370
1371 if (!rwa->raw && DRR_WRITE_COMPRESSED(drrw)) {
1372 /* Decompress the stream data */
1373 abd_t *dabd = abd_alloc_linear(
1374 drrw->drr_logical_size, B_FALSE);
1375 err = zio_decompress_data(drrw->drr_compressiontype,
1376 abd, abd_to_buf(dabd), abd_get_size(abd),
1377 abd_get_size(dabd), NULL);
1378
1379 if (err != 0) {
1380 abd_free(dabd);
1381 return (err);
1382 }
1383 /* Swap in the newly decompressed data into the abd */
1384 abd_free(abd);
1385 abd = dabd;
1386 }
1387
1388 if (!rwa->raw && BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
1389 /* Recompress the data */
1390 abd_t *cabd = abd_alloc_linear(BP_GET_PSIZE(bp),
1391 B_FALSE);
1392 uint64_t csize = zio_compress_data(BP_GET_COMPRESS(bp),
1393 abd, abd_to_buf(cabd), abd_get_size(abd),
1394 rwa->os->os_complevel);
1395 abd_zero_off(cabd, csize, BP_GET_PSIZE(bp) - csize);
1396 /* Swap in newly compressed data into the abd */
1397 abd_free(abd);
1398 abd = cabd;
1399 flags |= ZIO_FLAG_RAW_COMPRESS;
1400 }
1401
1402 /*
1403 * The stream is not encrypted but the data on-disk is.
1404 * We need to re-encrypt the buf using the same
1405 * encryption type, salt, iv, and mac that was used to encrypt
1406 * the block previosly.
1407 */
1408 if (!rwa->raw && BP_USES_CRYPT(bp)) {
1409 dsl_dataset_t *ds;
1410 dsl_crypto_key_t *dck = NULL;
1411 uint8_t salt[ZIO_DATA_SALT_LEN];
1412 uint8_t iv[ZIO_DATA_IV_LEN];
1413 uint8_t mac[ZIO_DATA_MAC_LEN];
1414 boolean_t no_crypt = B_FALSE;
1415 dsl_pool_t *dp = dmu_objset_pool(rwa->os);
1416 abd_t *eabd = abd_alloc_linear(BP_GET_PSIZE(bp), B_FALSE);
1417
1418 zio_crypt_decode_params_bp(bp, salt, iv);
1419 zio_crypt_decode_mac_bp(bp, mac);
1420
1421 dsl_pool_config_enter(dp, FTAG);
1422 err = dsl_dataset_hold_flags(dp, rwa->tofs,
1423 DS_HOLD_FLAG_DECRYPT, FTAG, &ds);
1424 if (err != 0) {
1425 dsl_pool_config_exit(dp, FTAG);
1426 abd_free(eabd);
1427 return (SET_ERROR(EACCES));
1428 }
1429
1430 /* Look up the key from the spa's keystore */
1431 err = spa_keystore_lookup_key(rwa->os->os_spa,
1432 zb.zb_objset, FTAG, &dck);
1433 if (err != 0) {
1434 dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT,
1435 FTAG);
1436 dsl_pool_config_exit(dp, FTAG);
1437 abd_free(eabd);
1438 return (SET_ERROR(EACCES));
1439 }
1440
1441 err = zio_do_crypt_abd(B_TRUE, &dck->dck_key,
1442 BP_GET_TYPE(bp), BP_SHOULD_BYTESWAP(bp), salt, iv,
1443 mac, abd_get_size(abd), abd, eabd, &no_crypt);
1444
1445 spa_keystore_dsl_key_rele(rwa->os->os_spa, dck, FTAG);
1446 dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
1447 dsl_pool_config_exit(dp, FTAG);
1448
1449 ASSERT0(no_crypt);
1450 if (err != 0) {
1451 abd_free(eabd);
1452 return (err);
1453 }
1454 /* Swap in the newly encrypted data into the abd */
1455 abd_free(abd);
1456 abd = eabd;
1457
1458 /*
1459 * We want to prevent zio_rewrite() from trying to
1460 * encrypt the data again
1461 */
1462 flags |= ZIO_FLAG_RAW_ENCRYPT;
1463 }
1464 rrd->abd = abd;
1465
1466 io = zio_rewrite(NULL, rwa->os->os_spa, bp->blk_birth, bp, abd,
1467 BP_GET_PSIZE(bp), NULL, NULL, ZIO_PRIORITY_SYNC_WRITE, flags, &zb);
1468
1469 ASSERT(abd_get_size(abd) == BP_GET_LSIZE(bp) ||
1470 abd_get_size(abd) == BP_GET_PSIZE(bp));
1471
1472 /* compute new bp checksum value and make sure it matches the old one */
1473 zio_checksum_compute(io, BP_GET_CHECKSUM(bp), abd, abd_get_size(abd));
1474 if (!ZIO_CHECKSUM_EQUAL(bp_cksum, io->io_bp->blk_cksum)) {
1475 zio_destroy(io);
1476 if (zfs_recv_best_effort_corrective != 0)
1477 return (0);
1478 return (SET_ERROR(ECKSUM));
1479 }
1480
1481 /* Correct the corruption in place */
1482 err = zio_wait(io);
1483 if (err == 0) {
1484 cr_cb_data_t *cb_data =
1485 kmem_alloc(sizeof (cr_cb_data_t), KM_SLEEP);
1486 cb_data->spa = rwa->os->os_spa;
1487 cb_data->size = drrw->drr_logical_size;
1488 cb_data->zb = zb;
1489 /* Test if healing worked by re-reading the bp */
1490 err = zio_wait(zio_read(rwa->heal_pio, rwa->os->os_spa, bp,
1491 abd_alloc_for_io(drrw->drr_logical_size, B_FALSE),
1492 drrw->drr_logical_size, corrective_read_done,
1493 cb_data, ZIO_PRIORITY_ASYNC_READ, flags, NULL));
1494 }
1495 if (err != 0 && zfs_recv_best_effort_corrective != 0)
1496 err = 0;
1497
1498 return (err);
1499 }
1500
1501 static int
1502 receive_read(dmu_recv_cookie_t *drc, int len, void *buf)
1503 {
1504 int done = 0;
1505
1506 /*
1507 * The code doesn't rely on this (lengths being multiples of 8). See
1508 * comment in dump_bytes.
1509 */
1510 ASSERT(len % 8 == 0 ||
1511 (drc->drc_featureflags & DMU_BACKUP_FEATURE_RAW) != 0);
1512
1513 while (done < len) {
1514 ssize_t resid = len - done;
1515 zfs_file_t *fp = drc->drc_fp;
1516 int err = zfs_file_read(fp, (char *)buf + done,
1517 len - done, &resid);
1518 if (err == 0 && resid == len - done) {
1519 /*
1520 * Note: ECKSUM or ZFS_ERR_STREAM_TRUNCATED indicates
1521 * that the receive was interrupted and can
1522 * potentially be resumed.
1523 */
1524 err = SET_ERROR(ZFS_ERR_STREAM_TRUNCATED);
1525 }
1526 drc->drc_voff += len - done - resid;
1527 done = len - resid;
1528 if (err != 0)
1529 return (err);
1530 }
1531
1532 drc->drc_bytes_read += len;
1533
1534 ASSERT3U(done, ==, len);
1535 return (0);
1536 }
1537
1538 static inline uint8_t
1539 deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1540 {
1541 if (bonus_type == DMU_OT_SA) {
1542 return (1);
1543 } else {
1544 return (1 +
1545 ((DN_OLD_MAX_BONUSLEN -
1546 MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT));
1547 }
1548 }
1549
1550 static void
1551 save_resume_state(struct receive_writer_arg *rwa,
1552 uint64_t object, uint64_t offset, dmu_tx_t *tx)
1553 {
1554 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1555
1556 if (!rwa->resumable)
1557 return;
1558
1559 /*
1560 * We use ds_resume_bytes[] != 0 to indicate that we need to
1561 * update this on disk, so it must not be 0.
1562 */
1563 ASSERT(rwa->bytes_read != 0);
1564
1565 /*
1566 * We only resume from write records, which have a valid
1567 * (non-meta-dnode) object number.
1568 */
1569 ASSERT(object != 0);
1570
1571 /*
1572 * For resuming to work correctly, we must receive records in order,
1573 * sorted by object,offset. This is checked by the callers, but
1574 * assert it here for good measure.
1575 */
1576 ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
1577 ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
1578 offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
1579 ASSERT3U(rwa->bytes_read, >=,
1580 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
1581
1582 rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
1583 rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
1584 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
1585 }
1586
1587 static int
1588 receive_object_is_same_generation(objset_t *os, uint64_t object,
1589 dmu_object_type_t old_bonus_type, dmu_object_type_t new_bonus_type,
1590 const void *new_bonus, boolean_t *samegenp)
1591 {
1592 zfs_file_info_t zoi;
1593 int err;
1594
1595 dmu_buf_t *old_bonus_dbuf;
1596 err = dmu_bonus_hold(os, object, FTAG, &old_bonus_dbuf);
1597 if (err != 0)
1598 return (err);
1599 err = dmu_get_file_info(os, old_bonus_type, old_bonus_dbuf->db_data,
1600 &zoi);
1601 dmu_buf_rele(old_bonus_dbuf, FTAG);
1602 if (err != 0)
1603 return (err);
1604 uint64_t old_gen = zoi.zfi_generation;
1605
1606 err = dmu_get_file_info(os, new_bonus_type, new_bonus, &zoi);
1607 if (err != 0)
1608 return (err);
1609 uint64_t new_gen = zoi.zfi_generation;
1610
1611 *samegenp = (old_gen == new_gen);
1612 return (0);
1613 }
1614
1615 static int
1616 receive_handle_existing_object(const struct receive_writer_arg *rwa,
1617 const struct drr_object *drro, const dmu_object_info_t *doi,
1618 const void *bonus_data,
1619 uint64_t *object_to_hold, uint32_t *new_blksz)
1620 {
1621 uint32_t indblksz = drro->drr_indblkshift ?
1622 1ULL << drro->drr_indblkshift : 0;
1623 int nblkptr = deduce_nblkptr(drro->drr_bonustype,
1624 drro->drr_bonuslen);
1625 uint8_t dn_slots = drro->drr_dn_slots != 0 ?
1626 drro->drr_dn_slots : DNODE_MIN_SLOTS;
1627 boolean_t do_free_range = B_FALSE;
1628 int err;
1629
1630 *object_to_hold = drro->drr_object;
1631
1632 /* nblkptr should be bounded by the bonus size and type */
1633 if (rwa->raw && nblkptr != drro->drr_nblkptr)
1634 return (SET_ERROR(EINVAL));
1635
1636 /*
1637 * After the previous send stream, the sending system may
1638 * have freed this object, and then happened to re-allocate
1639 * this object number in a later txg. In this case, we are
1640 * receiving a different logical file, and the block size may
1641 * appear to be different. i.e. we may have a different
1642 * block size for this object than what the send stream says.
1643 * In this case we need to remove the object's contents,
1644 * so that its structure can be changed and then its contents
1645 * entirely replaced by subsequent WRITE records.
1646 *
1647 * If this is a -L (--large-block) incremental stream, and
1648 * the previous stream was not -L, the block size may appear
1649 * to increase. i.e. we may have a smaller block size for
1650 * this object than what the send stream says. In this case
1651 * we need to keep the object's contents and block size
1652 * intact, so that we don't lose parts of the object's
1653 * contents that are not changed by this incremental send
1654 * stream.
1655 *
1656 * We can distinguish between the two above cases by using
1657 * the ZPL's generation number (see
1658 * receive_object_is_same_generation()). However, we only
1659 * want to rely on the generation number when absolutely
1660 * necessary, because with raw receives, the generation is
1661 * encrypted. We also want to minimize dependence on the
1662 * ZPL, so that other types of datasets can also be received
1663 * (e.g. ZVOLs, although note that ZVOLS currently do not
1664 * reallocate their objects or change their structure).
1665 * Therefore, we check a number of different cases where we
1666 * know it is safe to discard the object's contents, before
1667 * using the ZPL's generation number to make the above
1668 * distinction.
1669 */
1670 if (drro->drr_blksz != doi->doi_data_block_size) {
1671 if (rwa->raw) {
1672 /*
1673 * RAW streams always have large blocks, so
1674 * we are sure that the data is not needed
1675 * due to changing --large-block to be on.
1676 * Which is fortunate since the bonus buffer
1677 * (which contains the ZPL generation) is
1678 * encrypted, and the key might not be
1679 * loaded.
1680 */
1681 do_free_range = B_TRUE;
1682 } else if (rwa->full) {
1683 /*
1684 * This is a full send stream, so it always
1685 * replaces what we have. Even if the
1686 * generation numbers happen to match, this
1687 * can not actually be the same logical file.
1688 * This is relevant when receiving a full
1689 * send as a clone.
1690 */
1691 do_free_range = B_TRUE;
1692 } else if (drro->drr_type !=
1693 DMU_OT_PLAIN_FILE_CONTENTS ||
1694 doi->doi_type != DMU_OT_PLAIN_FILE_CONTENTS) {
1695 /*
1696 * PLAIN_FILE_CONTENTS are the only type of
1697 * objects that have ever been stored with
1698 * large blocks, so we don't need the special
1699 * logic below. ZAP blocks can shrink (when
1700 * there's only one block), so we don't want
1701 * to hit the error below about block size
1702 * only increasing.
1703 */
1704 do_free_range = B_TRUE;
1705 } else if (doi->doi_max_offset <=
1706 doi->doi_data_block_size) {
1707 /*
1708 * There is only one block. We can free it,
1709 * because its contents will be replaced by a
1710 * WRITE record. This can not be the no-L ->
1711 * -L case, because the no-L case would have
1712 * resulted in multiple blocks. If we
1713 * supported -L -> no-L, it would not be safe
1714 * to free the file's contents. Fortunately,
1715 * that is not allowed (see
1716 * recv_check_large_blocks()).
1717 */
1718 do_free_range = B_TRUE;
1719 } else {
1720 boolean_t is_same_gen;
1721 err = receive_object_is_same_generation(rwa->os,
1722 drro->drr_object, doi->doi_bonus_type,
1723 drro->drr_bonustype, bonus_data, &is_same_gen);
1724 if (err != 0)
1725 return (SET_ERROR(EINVAL));
1726
1727 if (is_same_gen) {
1728 /*
1729 * This is the same logical file, and
1730 * the block size must be increasing.
1731 * It could only decrease if
1732 * --large-block was changed to be
1733 * off, which is checked in
1734 * recv_check_large_blocks().
1735 */
1736 if (drro->drr_blksz <=
1737 doi->doi_data_block_size)
1738 return (SET_ERROR(EINVAL));
1739 /*
1740 * We keep the existing blocksize and
1741 * contents.
1742 */
1743 *new_blksz =
1744 doi->doi_data_block_size;
1745 } else {
1746 do_free_range = B_TRUE;
1747 }
1748 }
1749 }
1750
1751 /* nblkptr can only decrease if the object was reallocated */
1752 if (nblkptr < doi->doi_nblkptr)
1753 do_free_range = B_TRUE;
1754
1755 /* number of slots can only change on reallocation */
1756 if (dn_slots != doi->doi_dnodesize >> DNODE_SHIFT)
1757 do_free_range = B_TRUE;
1758
1759 /*
1760 * For raw sends we also check a few other fields to
1761 * ensure we are preserving the objset structure exactly
1762 * as it was on the receive side:
1763 * - A changed indirect block size
1764 * - A smaller nlevels
1765 */
1766 if (rwa->raw) {
1767 if (indblksz != doi->doi_metadata_block_size)
1768 do_free_range = B_TRUE;
1769 if (drro->drr_nlevels < doi->doi_indirection)
1770 do_free_range = B_TRUE;
1771 }
1772
1773 if (do_free_range) {
1774 err = dmu_free_long_range(rwa->os, drro->drr_object,
1775 0, DMU_OBJECT_END);
1776 if (err != 0)
1777 return (SET_ERROR(EINVAL));
1778 }
1779
1780 /*
1781 * The dmu does not currently support decreasing nlevels
1782 * or changing the number of dnode slots on an object. For
1783 * non-raw sends, this does not matter and the new object
1784 * can just use the previous one's nlevels. For raw sends,
1785 * however, the structure of the received dnode (including
1786 * nlevels and dnode slots) must match that of the send
1787 * side. Therefore, instead of using dmu_object_reclaim(),
1788 * we must free the object completely and call
1789 * dmu_object_claim_dnsize() instead.
1790 */
1791 if ((rwa->raw && drro->drr_nlevels < doi->doi_indirection) ||
1792 dn_slots != doi->doi_dnodesize >> DNODE_SHIFT) {
1793 err = dmu_free_long_object(rwa->os, drro->drr_object);
1794 if (err != 0)
1795 return (SET_ERROR(EINVAL));
1796
1797 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1798 *object_to_hold = DMU_NEW_OBJECT;
1799 }
1800
1801 /*
1802 * For raw receives, free everything beyond the new incoming
1803 * maxblkid. Normally this would be done with a DRR_FREE
1804 * record that would come after this DRR_OBJECT record is
1805 * processed. However, for raw receives we manually set the
1806 * maxblkid from the drr_maxblkid and so we must first free
1807 * everything above that blkid to ensure the DMU is always
1808 * consistent with itself. We will never free the first block
1809 * of the object here because a maxblkid of 0 could indicate
1810 * an object with a single block or one with no blocks. This
1811 * free may be skipped when dmu_free_long_range() was called
1812 * above since it covers the entire object's contents.
1813 */
1814 if (rwa->raw && *object_to_hold != DMU_NEW_OBJECT && !do_free_range) {
1815 err = dmu_free_long_range(rwa->os, drro->drr_object,
1816 (drro->drr_maxblkid + 1) * doi->doi_data_block_size,
1817 DMU_OBJECT_END);
1818 if (err != 0)
1819 return (SET_ERROR(EINVAL));
1820 }
1821 return (0);
1822 }
1823
1824 noinline static int
1825 receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1826 void *data)
1827 {
1828 dmu_object_info_t doi;
1829 dmu_tx_t *tx;
1830 int err;
1831 uint32_t new_blksz = drro->drr_blksz;
1832 uint8_t dn_slots = drro->drr_dn_slots != 0 ?
1833 drro->drr_dn_slots : DNODE_MIN_SLOTS;
1834
1835 if (drro->drr_type == DMU_OT_NONE ||
1836 !DMU_OT_IS_VALID(drro->drr_type) ||
1837 !DMU_OT_IS_VALID(drro->drr_bonustype) ||
1838 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1839 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1840 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1841 drro->drr_blksz < SPA_MINBLOCKSIZE ||
1842 drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
1843 drro->drr_bonuslen >
1844 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa->os))) ||
1845 dn_slots >
1846 (spa_maxdnodesize(dmu_objset_spa(rwa->os)) >> DNODE_SHIFT)) {
1847 return (SET_ERROR(EINVAL));
1848 }
1849
1850 if (rwa->raw) {
1851 /*
1852 * We should have received a DRR_OBJECT_RANGE record
1853 * containing this block and stored it in rwa.
1854 */
1855 if (drro->drr_object < rwa->or_firstobj ||
1856 drro->drr_object >= rwa->or_firstobj + rwa->or_numslots ||
1857 drro->drr_raw_bonuslen < drro->drr_bonuslen ||
1858 drro->drr_indblkshift > SPA_MAXBLOCKSHIFT ||
1859 drro->drr_nlevels > DN_MAX_LEVELS ||
1860 drro->drr_nblkptr > DN_MAX_NBLKPTR ||
1861 DN_SLOTS_TO_BONUSLEN(dn_slots) <
1862 drro->drr_raw_bonuslen)
1863 return (SET_ERROR(EINVAL));
1864 } else {
1865 /*
1866 * The DRR_OBJECT_SPILL flag is valid when the DRR_BEGIN
1867 * record indicates this by setting DRR_FLAG_SPILL_BLOCK.
1868 */
1869 if (((drro->drr_flags & ~(DRR_OBJECT_SPILL))) ||
1870 (!rwa->spill && DRR_OBJECT_HAS_SPILL(drro->drr_flags))) {
1871 return (SET_ERROR(EINVAL));
1872 }
1873
1874 if (drro->drr_raw_bonuslen != 0 || drro->drr_nblkptr != 0 ||
1875 drro->drr_indblkshift != 0 || drro->drr_nlevels != 0) {
1876 return (SET_ERROR(EINVAL));
1877 }
1878 }
1879
1880 err = dmu_object_info(rwa->os, drro->drr_object, &doi);
1881
1882 if (err != 0 && err != ENOENT && err != EEXIST)
1883 return (SET_ERROR(EINVAL));
1884
1885 if (drro->drr_object > rwa->max_object)
1886 rwa->max_object = drro->drr_object;
1887
1888 /*
1889 * If we are losing blkptrs or changing the block size this must
1890 * be a new file instance. We must clear out the previous file
1891 * contents before we can change this type of metadata in the dnode.
1892 * Raw receives will also check that the indirect structure of the
1893 * dnode hasn't changed.
1894 */
1895 uint64_t object_to_hold;
1896 if (err == 0) {
1897 err = receive_handle_existing_object(rwa, drro, &doi, data,
1898 &object_to_hold, &new_blksz);
1899 if (err != 0)
1900 return (err);
1901 } else if (err == EEXIST) {
1902 /*
1903 * The object requested is currently an interior slot of a
1904 * multi-slot dnode. This will be resolved when the next txg
1905 * is synced out, since the send stream will have told us
1906 * to free this slot when we freed the associated dnode
1907 * earlier in the stream.
1908 */
1909 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1910
1911 if (dmu_object_info(rwa->os, drro->drr_object, NULL) != ENOENT)
1912 return (SET_ERROR(EINVAL));
1913
1914 /* object was freed and we are about to allocate a new one */
1915 object_to_hold = DMU_NEW_OBJECT;
1916 } else {
1917 /* object is free and we are about to allocate a new one */
1918 object_to_hold = DMU_NEW_OBJECT;
1919 }
1920
1921 /*
1922 * If this is a multi-slot dnode there is a chance that this
1923 * object will expand into a slot that is already used by
1924 * another object from the previous snapshot. We must free
1925 * these objects before we attempt to allocate the new dnode.
1926 */
1927 if (dn_slots > 1) {
1928 boolean_t need_sync = B_FALSE;
1929
1930 for (uint64_t slot = drro->drr_object + 1;
1931 slot < drro->drr_object + dn_slots;
1932 slot++) {
1933 dmu_object_info_t slot_doi;
1934
1935 err = dmu_object_info(rwa->os, slot, &slot_doi);
1936 if (err == ENOENT || err == EEXIST)
1937 continue;
1938 else if (err != 0)
1939 return (err);
1940
1941 err = dmu_free_long_object(rwa->os, slot);
1942 if (err != 0)
1943 return (err);
1944
1945 need_sync = B_TRUE;
1946 }
1947
1948 if (need_sync)
1949 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1950 }
1951
1952 tx = dmu_tx_create(rwa->os);
1953 dmu_tx_hold_bonus(tx, object_to_hold);
1954 dmu_tx_hold_write(tx, object_to_hold, 0, 0);
1955 err = dmu_tx_assign(tx, TXG_WAIT);
1956 if (err != 0) {
1957 dmu_tx_abort(tx);
1958 return (err);
1959 }
1960
1961 if (object_to_hold == DMU_NEW_OBJECT) {
1962 /* Currently free, wants to be allocated */
1963 err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
1964 drro->drr_type, new_blksz,
1965 drro->drr_bonustype, drro->drr_bonuslen,
1966 dn_slots << DNODE_SHIFT, tx);
1967 } else if (drro->drr_type != doi.doi_type ||
1968 new_blksz != doi.doi_data_block_size ||
1969 drro->drr_bonustype != doi.doi_bonus_type ||
1970 drro->drr_bonuslen != doi.doi_bonus_size) {
1971 /* Currently allocated, but with different properties */
1972 err = dmu_object_reclaim_dnsize(rwa->os, drro->drr_object,
1973 drro->drr_type, new_blksz,
1974 drro->drr_bonustype, drro->drr_bonuslen,
1975 dn_slots << DNODE_SHIFT, rwa->spill ?
1976 DRR_OBJECT_HAS_SPILL(drro->drr_flags) : B_FALSE, tx);
1977 } else if (rwa->spill && !DRR_OBJECT_HAS_SPILL(drro->drr_flags)) {
1978 /*
1979 * Currently allocated, the existing version of this object
1980 * may reference a spill block that is no longer allocated
1981 * at the source and needs to be freed.
1982 */
1983 err = dmu_object_rm_spill(rwa->os, drro->drr_object, tx);
1984 }
1985
1986 if (err != 0) {
1987 dmu_tx_commit(tx);
1988 return (SET_ERROR(EINVAL));
1989 }
1990
1991 if (rwa->or_crypt_params_present) {
1992 /*
1993 * Set the crypt params for the buffer associated with this
1994 * range of dnodes. This causes the blkptr_t to have the
1995 * same crypt params (byteorder, salt, iv, mac) as on the
1996 * sending side.
1997 *
1998 * Since we are committing this tx now, it is possible for
1999 * the dnode block to end up on-disk with the incorrect MAC,
2000 * if subsequent objects in this block are received in a
2001 * different txg. However, since the dataset is marked as
2002 * inconsistent, no code paths will do a non-raw read (or
2003 * decrypt the block / verify the MAC). The receive code and
2004 * scrub code can safely do raw reads and verify the
2005 * checksum. They don't need to verify the MAC.
2006 */
2007 dmu_buf_t *db = NULL;
2008 uint64_t offset = rwa->or_firstobj * DNODE_MIN_SIZE;
2009
2010 err = dmu_buf_hold_by_dnode(DMU_META_DNODE(rwa->os),
2011 offset, FTAG, &db, DMU_READ_PREFETCH | DMU_READ_NO_DECRYPT);
2012 if (err != 0) {
2013 dmu_tx_commit(tx);
2014 return (SET_ERROR(EINVAL));
2015 }
2016
2017 dmu_buf_set_crypt_params(db, rwa->or_byteorder,
2018 rwa->or_salt, rwa->or_iv, rwa->or_mac, tx);
2019
2020 dmu_buf_rele(db, FTAG);
2021
2022 rwa->or_crypt_params_present = B_FALSE;
2023 }
2024
2025 dmu_object_set_checksum(rwa->os, drro->drr_object,
2026 drro->drr_checksumtype, tx);
2027 dmu_object_set_compress(rwa->os, drro->drr_object,
2028 drro->drr_compress, tx);
2029
2030 /* handle more restrictive dnode structuring for raw recvs */
2031 if (rwa->raw) {
2032 /*
2033 * Set the indirect block size, block shift, nlevels.
2034 * This will not fail because we ensured all of the
2035 * blocks were freed earlier if this is a new object.
2036 * For non-new objects block size and indirect block
2037 * shift cannot change and nlevels can only increase.
2038 */
2039 ASSERT3U(new_blksz, ==, drro->drr_blksz);
2040 VERIFY0(dmu_object_set_blocksize(rwa->os, drro->drr_object,
2041 drro->drr_blksz, drro->drr_indblkshift, tx));
2042 VERIFY0(dmu_object_set_nlevels(rwa->os, drro->drr_object,
2043 drro->drr_nlevels, tx));
2044
2045 /*
2046 * Set the maxblkid. This will always succeed because
2047 * we freed all blocks beyond the new maxblkid above.
2048 */
2049 VERIFY0(dmu_object_set_maxblkid(rwa->os, drro->drr_object,
2050 drro->drr_maxblkid, tx));
2051 }
2052
2053 if (data != NULL) {
2054 dmu_buf_t *db;
2055 dnode_t *dn;
2056 uint32_t flags = DMU_READ_NO_PREFETCH;
2057
2058 if (rwa->raw)
2059 flags |= DMU_READ_NO_DECRYPT;
2060
2061 VERIFY0(dnode_hold(rwa->os, drro->drr_object, FTAG, &dn));
2062 VERIFY0(dmu_bonus_hold_by_dnode(dn, FTAG, &db, flags));
2063
2064 dmu_buf_will_dirty(db, tx);
2065
2066 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
2067 memcpy(db->db_data, data, DRR_OBJECT_PAYLOAD_SIZE(drro));
2068
2069 /*
2070 * Raw bonus buffers have their byteorder determined by the
2071 * DRR_OBJECT_RANGE record.
2072 */
2073 if (rwa->byteswap && !rwa->raw) {
2074 dmu_object_byteswap_t byteswap =
2075 DMU_OT_BYTESWAP(drro->drr_bonustype);
2076 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2077 DRR_OBJECT_PAYLOAD_SIZE(drro));
2078 }
2079 dmu_buf_rele(db, FTAG);
2080 dnode_rele(dn, FTAG);
2081 }
2082 dmu_tx_commit(tx);
2083
2084 return (0);
2085 }
2086
2087 noinline static int
2088 receive_freeobjects(struct receive_writer_arg *rwa,
2089 struct drr_freeobjects *drrfo)
2090 {
2091 uint64_t obj;
2092 int next_err = 0;
2093
2094 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2095 return (SET_ERROR(EINVAL));
2096
2097 for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
2098 obj < drrfo->drr_firstobj + drrfo->drr_numobjs &&
2099 obj < DN_MAX_OBJECT && next_err == 0;
2100 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2101 dmu_object_info_t doi;
2102 int err;
2103
2104 err = dmu_object_info(rwa->os, obj, &doi);
2105 if (err == ENOENT)
2106 continue;
2107 else if (err != 0)
2108 return (err);
2109
2110 err = dmu_free_long_object(rwa->os, obj);
2111
2112 if (err != 0)
2113 return (err);
2114 }
2115 if (next_err != ESRCH)
2116 return (next_err);
2117 return (0);
2118 }
2119
2120 /*
2121 * Note: if this fails, the caller will clean up any records left on the
2122 * rwa->write_batch list.
2123 */
2124 static int
2125 flush_write_batch_impl(struct receive_writer_arg *rwa)
2126 {
2127 dnode_t *dn;
2128 int err;
2129
2130 if (dnode_hold(rwa->os, rwa->last_object, FTAG, &dn) != 0)
2131 return (SET_ERROR(EINVAL));
2132
2133 struct receive_record_arg *last_rrd = list_tail(&rwa->write_batch);
2134 struct drr_write *last_drrw = &last_rrd->header.drr_u.drr_write;
2135
2136 struct receive_record_arg *first_rrd = list_head(&rwa->write_batch);
2137 struct drr_write *first_drrw = &first_rrd->header.drr_u.drr_write;
2138
2139 ASSERT3U(rwa->last_object, ==, last_drrw->drr_object);
2140 ASSERT3U(rwa->last_offset, ==, last_drrw->drr_offset);
2141
2142 dmu_tx_t *tx = dmu_tx_create(rwa->os);
2143 dmu_tx_hold_write_by_dnode(tx, dn, first_drrw->drr_offset,
2144 last_drrw->drr_offset - first_drrw->drr_offset +
2145 last_drrw->drr_logical_size);
2146 err = dmu_tx_assign(tx, TXG_WAIT);
2147 if (err != 0) {
2148 dmu_tx_abort(tx);
2149 dnode_rele(dn, FTAG);
2150 return (err);
2151 }
2152
2153 struct receive_record_arg *rrd;
2154 while ((rrd = list_head(&rwa->write_batch)) != NULL) {
2155 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2156 abd_t *abd = rrd->abd;
2157
2158 ASSERT3U(drrw->drr_object, ==, rwa->last_object);
2159
2160 if (drrw->drr_logical_size != dn->dn_datablksz) {
2161 /*
2162 * The WRITE record is larger than the object's block
2163 * size. We must be receiving an incremental
2164 * large-block stream into a dataset that previously did
2165 * a non-large-block receive. Lightweight writes must
2166 * be exactly one block, so we need to decompress the
2167 * data (if compressed) and do a normal dmu_write().
2168 */
2169 ASSERT3U(drrw->drr_logical_size, >, dn->dn_datablksz);
2170 if (DRR_WRITE_COMPRESSED(drrw)) {
2171 abd_t *decomp_abd =
2172 abd_alloc_linear(drrw->drr_logical_size,
2173 B_FALSE);
2174
2175 err = zio_decompress_data(
2176 drrw->drr_compressiontype,
2177 abd, abd_to_buf(decomp_abd),
2178 abd_get_size(abd),
2179 abd_get_size(decomp_abd), NULL);
2180
2181 if (err == 0) {
2182 dmu_write_by_dnode(dn,
2183 drrw->drr_offset,
2184 drrw->drr_logical_size,
2185 abd_to_buf(decomp_abd), tx);
2186 }
2187 abd_free(decomp_abd);
2188 } else {
2189 dmu_write_by_dnode(dn,
2190 drrw->drr_offset,
2191 drrw->drr_logical_size,
2192 abd_to_buf(abd), tx);
2193 }
2194 if (err == 0)
2195 abd_free(abd);
2196 } else {
2197 zio_prop_t zp;
2198 dmu_write_policy(rwa->os, dn, 0, 0, &zp);
2199
2200 zio_flag_t zio_flags = 0;
2201
2202 if (rwa->raw) {
2203 zp.zp_encrypt = B_TRUE;
2204 zp.zp_compress = drrw->drr_compressiontype;
2205 zp.zp_byteorder = ZFS_HOST_BYTEORDER ^
2206 !!DRR_IS_RAW_BYTESWAPPED(drrw->drr_flags) ^
2207 rwa->byteswap;
2208 memcpy(zp.zp_salt, drrw->drr_salt,
2209 ZIO_DATA_SALT_LEN);
2210 memcpy(zp.zp_iv, drrw->drr_iv,
2211 ZIO_DATA_IV_LEN);
2212 memcpy(zp.zp_mac, drrw->drr_mac,
2213 ZIO_DATA_MAC_LEN);
2214 if (DMU_OT_IS_ENCRYPTED(zp.zp_type)) {
2215 zp.zp_nopwrite = B_FALSE;
2216 zp.zp_copies = MIN(zp.zp_copies,
2217 SPA_DVAS_PER_BP - 1);
2218 }
2219 zio_flags |= ZIO_FLAG_RAW;
2220 } else if (DRR_WRITE_COMPRESSED(drrw)) {
2221 ASSERT3U(drrw->drr_compressed_size, >, 0);
2222 ASSERT3U(drrw->drr_logical_size, >=,
2223 drrw->drr_compressed_size);
2224 zp.zp_compress = drrw->drr_compressiontype;
2225 zio_flags |= ZIO_FLAG_RAW_COMPRESS;
2226 } else if (rwa->byteswap) {
2227 /*
2228 * Note: compressed blocks never need to be
2229 * byteswapped, because WRITE records for
2230 * metadata blocks are never compressed. The
2231 * exception is raw streams, which are written
2232 * in the original byteorder, and the byteorder
2233 * bit is preserved in the BP by setting
2234 * zp_byteorder above.
2235 */
2236 dmu_object_byteswap_t byteswap =
2237 DMU_OT_BYTESWAP(drrw->drr_type);
2238 dmu_ot_byteswap[byteswap].ob_func(
2239 abd_to_buf(abd),
2240 DRR_WRITE_PAYLOAD_SIZE(drrw));
2241 }
2242
2243 /*
2244 * Since this data can't be read until the receive
2245 * completes, we can do a "lightweight" write for
2246 * improved performance.
2247 */
2248 err = dmu_lightweight_write_by_dnode(dn,
2249 drrw->drr_offset, abd, &zp, zio_flags, tx);
2250 }
2251
2252 if (err != 0) {
2253 /*
2254 * This rrd is left on the list, so the caller will
2255 * free it (and the abd).
2256 */
2257 break;
2258 }
2259
2260 /*
2261 * Note: If the receive fails, we want the resume stream to
2262 * start with the same record that we last successfully
2263 * received (as opposed to the next record), so that we can
2264 * verify that we are resuming from the correct location.
2265 */
2266 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2267
2268 list_remove(&rwa->write_batch, rrd);
2269 kmem_free(rrd, sizeof (*rrd));
2270 }
2271
2272 dmu_tx_commit(tx);
2273 dnode_rele(dn, FTAG);
2274 return (err);
2275 }
2276
2277 noinline static int
2278 flush_write_batch(struct receive_writer_arg *rwa)
2279 {
2280 if (list_is_empty(&rwa->write_batch))
2281 return (0);
2282 int err = rwa->err;
2283 if (err == 0)
2284 err = flush_write_batch_impl(rwa);
2285 if (err != 0) {
2286 struct receive_record_arg *rrd;
2287 while ((rrd = list_remove_head(&rwa->write_batch)) != NULL) {
2288 abd_free(rrd->abd);
2289 kmem_free(rrd, sizeof (*rrd));
2290 }
2291 }
2292 ASSERT(list_is_empty(&rwa->write_batch));
2293 return (err);
2294 }
2295
2296 noinline static int
2297 receive_process_write_record(struct receive_writer_arg *rwa,
2298 struct receive_record_arg *rrd)
2299 {
2300 int err = 0;
2301
2302 ASSERT3U(rrd->header.drr_type, ==, DRR_WRITE);
2303 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2304
2305 if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
2306 !DMU_OT_IS_VALID(drrw->drr_type))
2307 return (SET_ERROR(EINVAL));
2308
2309 if (rwa->heal) {
2310 blkptr_t *bp;
2311 dmu_buf_t *dbp;
2312 dnode_t *dn;
2313 int flags = DB_RF_CANFAIL;
2314
2315 if (rwa->raw)
2316 flags |= DB_RF_NO_DECRYPT;
2317
2318 if (rwa->byteswap) {
2319 dmu_object_byteswap_t byteswap =
2320 DMU_OT_BYTESWAP(drrw->drr_type);
2321 dmu_ot_byteswap[byteswap].ob_func(abd_to_buf(rrd->abd),
2322 DRR_WRITE_PAYLOAD_SIZE(drrw));
2323 }
2324
2325 err = dmu_buf_hold_noread(rwa->os, drrw->drr_object,
2326 drrw->drr_offset, FTAG, &dbp);
2327 if (err != 0)
2328 return (err);
2329
2330 /* Try to read the object to see if it needs healing */
2331 err = dbuf_read((dmu_buf_impl_t *)dbp, NULL, flags);
2332 /*
2333 * We only try to heal when dbuf_read() returns a ECKSUMs.
2334 * Other errors (even EIO) get returned to caller.
2335 * EIO indicates that the device is not present/accessible,
2336 * so writing to it will likely fail.
2337 * If the block is healthy, we don't want to overwrite it
2338 * unnecessarily.
2339 */
2340 if (err != ECKSUM) {
2341 dmu_buf_rele(dbp, FTAG);
2342 return (err);
2343 }
2344 dn = dmu_buf_dnode_enter(dbp);
2345 /* Make sure the on-disk block and recv record sizes match */
2346 if (drrw->drr_logical_size !=
2347 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT) {
2348 err = ENOTSUP;
2349 dmu_buf_dnode_exit(dbp);
2350 dmu_buf_rele(dbp, FTAG);
2351 return (err);
2352 }
2353 /* Get the block pointer for the corrupted block */
2354 bp = dmu_buf_get_blkptr(dbp);
2355 err = do_corrective_recv(rwa, drrw, rrd, bp);
2356 dmu_buf_dnode_exit(dbp);
2357 dmu_buf_rele(dbp, FTAG);
2358 return (err);
2359 }
2360
2361 /*
2362 * For resuming to work, records must be in increasing order
2363 * by (object, offset).
2364 */
2365 if (drrw->drr_object < rwa->last_object ||
2366 (drrw->drr_object == rwa->last_object &&
2367 drrw->drr_offset < rwa->last_offset)) {
2368 return (SET_ERROR(EINVAL));
2369 }
2370
2371 struct receive_record_arg *first_rrd = list_head(&rwa->write_batch);
2372 struct drr_write *first_drrw = &first_rrd->header.drr_u.drr_write;
2373 uint64_t batch_size =
2374 MIN(zfs_recv_write_batch_size, DMU_MAX_ACCESS / 2);
2375 if (first_rrd != NULL &&
2376 (drrw->drr_object != first_drrw->drr_object ||
2377 drrw->drr_offset >= first_drrw->drr_offset + batch_size)) {
2378 err = flush_write_batch(rwa);
2379 if (err != 0)
2380 return (err);
2381 }
2382
2383 rwa->last_object = drrw->drr_object;
2384 rwa->last_offset = drrw->drr_offset;
2385
2386 if (rwa->last_object > rwa->max_object)
2387 rwa->max_object = rwa->last_object;
2388
2389 list_insert_tail(&rwa->write_batch, rrd);
2390 /*
2391 * Return EAGAIN to indicate that we will use this rrd again,
2392 * so the caller should not free it
2393 */
2394 return (EAGAIN);
2395 }
2396
2397 static int
2398 receive_write_embedded(struct receive_writer_arg *rwa,
2399 struct drr_write_embedded *drrwe, void *data)
2400 {
2401 dmu_tx_t *tx;
2402 int err;
2403
2404 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
2405 return (SET_ERROR(EINVAL));
2406
2407 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
2408 return (SET_ERROR(EINVAL));
2409
2410 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
2411 return (SET_ERROR(EINVAL));
2412 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
2413 return (SET_ERROR(EINVAL));
2414 if (rwa->raw)
2415 return (SET_ERROR(EINVAL));
2416
2417 if (drrwe->drr_object > rwa->max_object)
2418 rwa->max_object = drrwe->drr_object;
2419
2420 tx = dmu_tx_create(rwa->os);
2421
2422 dmu_tx_hold_write(tx, drrwe->drr_object,
2423 drrwe->drr_offset, drrwe->drr_length);
2424 err = dmu_tx_assign(tx, TXG_WAIT);
2425 if (err != 0) {
2426 dmu_tx_abort(tx);
2427 return (err);
2428 }
2429
2430 dmu_write_embedded(rwa->os, drrwe->drr_object,
2431 drrwe->drr_offset, data, drrwe->drr_etype,
2432 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2433 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
2434
2435 /* See comment in restore_write. */
2436 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
2437 dmu_tx_commit(tx);
2438 return (0);
2439 }
2440
2441 static int
2442 receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2443 abd_t *abd)
2444 {
2445 dmu_buf_t *db, *db_spill;
2446 int err;
2447
2448 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2449 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2450 return (SET_ERROR(EINVAL));
2451
2452 /*
2453 * This is an unmodified spill block which was added to the stream
2454 * to resolve an issue with incorrectly removing spill blocks. It
2455 * should be ignored by current versions of the code which support
2456 * the DRR_FLAG_SPILL_BLOCK flag.
2457 */
2458 if (rwa->spill && DRR_SPILL_IS_UNMODIFIED(drrs->drr_flags)) {
2459 abd_free(abd);
2460 return (0);
2461 }
2462
2463 if (rwa->raw) {
2464 if (!DMU_OT_IS_VALID(drrs->drr_type) ||
2465 drrs->drr_compressiontype >= ZIO_COMPRESS_FUNCTIONS ||
2466 drrs->drr_compressed_size == 0)
2467 return (SET_ERROR(EINVAL));
2468 }
2469
2470 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2471 return (SET_ERROR(EINVAL));
2472
2473 if (drrs->drr_object > rwa->max_object)
2474 rwa->max_object = drrs->drr_object;
2475
2476 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
2477 if ((err = dmu_spill_hold_by_bonus(db, DMU_READ_NO_DECRYPT, FTAG,
2478 &db_spill)) != 0) {
2479 dmu_buf_rele(db, FTAG);
2480 return (err);
2481 }
2482
2483 dmu_tx_t *tx = dmu_tx_create(rwa->os);
2484
2485 dmu_tx_hold_spill(tx, db->db_object);
2486
2487 err = dmu_tx_assign(tx, TXG_WAIT);
2488 if (err != 0) {
2489 dmu_buf_rele(db, FTAG);
2490 dmu_buf_rele(db_spill, FTAG);
2491 dmu_tx_abort(tx);
2492 return (err);
2493 }
2494
2495 /*
2496 * Spill blocks may both grow and shrink. When a change in size
2497 * occurs any existing dbuf must be updated to match the logical
2498 * size of the provided arc_buf_t.
2499 */
2500 if (db_spill->db_size != drrs->drr_length) {
2501 dmu_buf_will_fill(db_spill, tx);
2502 VERIFY0(dbuf_spill_set_blksz(db_spill,
2503 drrs->drr_length, tx));
2504 }
2505
2506 arc_buf_t *abuf;
2507 if (rwa->raw) {
2508 boolean_t byteorder = ZFS_HOST_BYTEORDER ^
2509 !!DRR_IS_RAW_BYTESWAPPED(drrs->drr_flags) ^
2510 rwa->byteswap;
2511
2512 abuf = arc_loan_raw_buf(dmu_objset_spa(rwa->os),
2513 drrs->drr_object, byteorder, drrs->drr_salt,
2514 drrs->drr_iv, drrs->drr_mac, drrs->drr_type,
2515 drrs->drr_compressed_size, drrs->drr_length,
2516 drrs->drr_compressiontype, 0);
2517 } else {
2518 abuf = arc_loan_buf(dmu_objset_spa(rwa->os),
2519 DMU_OT_IS_METADATA(drrs->drr_type),
2520 drrs->drr_length);
2521 if (rwa->byteswap) {
2522 dmu_object_byteswap_t byteswap =
2523 DMU_OT_BYTESWAP(drrs->drr_type);
2524 dmu_ot_byteswap[byteswap].ob_func(abd_to_buf(abd),
2525 DRR_SPILL_PAYLOAD_SIZE(drrs));
2526 }
2527 }
2528
2529 memcpy(abuf->b_data, abd_to_buf(abd), DRR_SPILL_PAYLOAD_SIZE(drrs));
2530 abd_free(abd);
2531 dbuf_assign_arcbuf((dmu_buf_impl_t *)db_spill, abuf, tx);
2532
2533 dmu_buf_rele(db, FTAG);
2534 dmu_buf_rele(db_spill, FTAG);
2535
2536 dmu_tx_commit(tx);
2537 return (0);
2538 }
2539
2540 noinline static int
2541 receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2542 {
2543 int err;
2544
2545 if (drrf->drr_length != -1ULL &&
2546 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2547 return (SET_ERROR(EINVAL));
2548
2549 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2550 return (SET_ERROR(EINVAL));
2551
2552 if (drrf->drr_object > rwa->max_object)
2553 rwa->max_object = drrf->drr_object;
2554
2555 err = dmu_free_long_range(rwa->os, drrf->drr_object,
2556 drrf->drr_offset, drrf->drr_length);
2557
2558 return (err);
2559 }
2560
2561 static int
2562 receive_object_range(struct receive_writer_arg *rwa,
2563 struct drr_object_range *drror)
2564 {
2565 /*
2566 * By default, we assume this block is in our native format
2567 * (ZFS_HOST_BYTEORDER). We then take into account whether
2568 * the send stream is byteswapped (rwa->byteswap). Finally,
2569 * we need to byteswap again if this particular block was
2570 * in non-native format on the send side.
2571 */
2572 boolean_t byteorder = ZFS_HOST_BYTEORDER ^ rwa->byteswap ^
2573 !!DRR_IS_RAW_BYTESWAPPED(drror->drr_flags);
2574
2575 /*
2576 * Since dnode block sizes are constant, we should not need to worry
2577 * about making sure that the dnode block size is the same on the
2578 * sending and receiving sides for the time being. For non-raw sends,
2579 * this does not matter (and in fact we do not send a DRR_OBJECT_RANGE
2580 * record at all). Raw sends require this record type because the
2581 * encryption parameters are used to protect an entire block of bonus
2582 * buffers. If the size of dnode blocks ever becomes variable,
2583 * handling will need to be added to ensure that dnode block sizes
2584 * match on the sending and receiving side.
2585 */
2586 if (drror->drr_numslots != DNODES_PER_BLOCK ||
2587 P2PHASE(drror->drr_firstobj, DNODES_PER_BLOCK) != 0 ||
2588 !rwa->raw)
2589 return (SET_ERROR(EINVAL));
2590
2591 if (drror->drr_firstobj > rwa->max_object)
2592 rwa->max_object = drror->drr_firstobj;
2593
2594 /*
2595 * The DRR_OBJECT_RANGE handling must be deferred to receive_object()
2596 * so that the block of dnodes is not written out when it's empty,
2597 * and converted to a HOLE BP.
2598 */
2599 rwa->or_crypt_params_present = B_TRUE;
2600 rwa->or_firstobj = drror->drr_firstobj;
2601 rwa->or_numslots = drror->drr_numslots;
2602 memcpy(rwa->or_salt, drror->drr_salt, ZIO_DATA_SALT_LEN);
2603 memcpy(rwa->or_iv, drror->drr_iv, ZIO_DATA_IV_LEN);
2604 memcpy(rwa->or_mac, drror->drr_mac, ZIO_DATA_MAC_LEN);
2605 rwa->or_byteorder = byteorder;
2606
2607 return (0);
2608 }
2609
2610 /*
2611 * Until we have the ability to redact large ranges of data efficiently, we
2612 * process these records as frees.
2613 */
2614 noinline static int
2615 receive_redact(struct receive_writer_arg *rwa, struct drr_redact *drrr)
2616 {
2617 struct drr_free drrf = {0};
2618 drrf.drr_length = drrr->drr_length;
2619 drrf.drr_object = drrr->drr_object;
2620 drrf.drr_offset = drrr->drr_offset;
2621 drrf.drr_toguid = drrr->drr_toguid;
2622 return (receive_free(rwa, &drrf));
2623 }
2624
2625 /* used to destroy the drc_ds on error */
2626 static void
2627 dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
2628 {
2629 dsl_dataset_t *ds = drc->drc_ds;
2630 ds_hold_flags_t dsflags;
2631
2632 dsflags = (drc->drc_raw) ? DS_HOLD_FLAG_NONE : DS_HOLD_FLAG_DECRYPT;
2633 /*
2634 * Wait for the txg sync before cleaning up the receive. For
2635 * resumable receives, this ensures that our resume state has
2636 * been written out to disk. For raw receives, this ensures
2637 * that the user accounting code will not attempt to do anything
2638 * after we stopped receiving the dataset.
2639 */
2640 txg_wait_synced(ds->ds_dir->dd_pool, 0);
2641 ds->ds_objset->os_raw_receive = B_FALSE;
2642
2643 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2644 if (drc->drc_resumable && drc->drc_should_save &&
2645 !BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {
2646 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2647 dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
2648 } else {
2649 char name[ZFS_MAX_DATASET_NAME_LEN];
2650 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2651 dsl_dataset_name(ds, name);
2652 dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
2653 if (!drc->drc_heal)
2654 (void) dsl_destroy_head(name);
2655 }
2656 }
2657
2658 static void
2659 receive_cksum(dmu_recv_cookie_t *drc, int len, void *buf)
2660 {
2661 if (drc->drc_byteswap) {
2662 (void) fletcher_4_incremental_byteswap(buf, len,
2663 &drc->drc_cksum);
2664 } else {
2665 (void) fletcher_4_incremental_native(buf, len, &drc->drc_cksum);
2666 }
2667 }
2668
2669 /*
2670 * Read the payload into a buffer of size len, and update the current record's
2671 * payload field.
2672 * Allocate drc->drc_next_rrd and read the next record's header into
2673 * drc->drc_next_rrd->header.
2674 * Verify checksum of payload and next record.
2675 */
2676 static int
2677 receive_read_payload_and_next_header(dmu_recv_cookie_t *drc, int len, void *buf)
2678 {
2679 int err;
2680
2681 if (len != 0) {
2682 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2683 err = receive_read(drc, len, buf);
2684 if (err != 0)
2685 return (err);
2686 receive_cksum(drc, len, buf);
2687
2688 /* note: rrd is NULL when reading the begin record's payload */
2689 if (drc->drc_rrd != NULL) {
2690 drc->drc_rrd->payload = buf;
2691 drc->drc_rrd->payload_size = len;
2692 drc->drc_rrd->bytes_read = drc->drc_bytes_read;
2693 }
2694 } else {
2695 ASSERT3P(buf, ==, NULL);
2696 }
2697
2698 drc->drc_prev_cksum = drc->drc_cksum;
2699
2700 drc->drc_next_rrd = kmem_zalloc(sizeof (*drc->drc_next_rrd), KM_SLEEP);
2701 err = receive_read(drc, sizeof (drc->drc_next_rrd->header),
2702 &drc->drc_next_rrd->header);
2703 drc->drc_next_rrd->bytes_read = drc->drc_bytes_read;
2704
2705 if (err != 0) {
2706 kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
2707 drc->drc_next_rrd = NULL;
2708 return (err);
2709 }
2710 if (drc->drc_next_rrd->header.drr_type == DRR_BEGIN) {
2711 kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
2712 drc->drc_next_rrd = NULL;
2713 return (SET_ERROR(EINVAL));
2714 }
2715
2716 /*
2717 * Note: checksum is of everything up to but not including the
2718 * checksum itself.
2719 */
2720 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2721 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2722 receive_cksum(drc,
2723 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2724 &drc->drc_next_rrd->header);
2725
2726 zio_cksum_t cksum_orig =
2727 drc->drc_next_rrd->header.drr_u.drr_checksum.drr_checksum;
2728 zio_cksum_t *cksump =
2729 &drc->drc_next_rrd->header.drr_u.drr_checksum.drr_checksum;
2730
2731 if (drc->drc_byteswap)
2732 byteswap_record(&drc->drc_next_rrd->header);
2733
2734 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2735 !ZIO_CHECKSUM_EQUAL(drc->drc_cksum, *cksump)) {
2736 kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
2737 drc->drc_next_rrd = NULL;
2738 return (SET_ERROR(ECKSUM));
2739 }
2740
2741 receive_cksum(drc, sizeof (cksum_orig), &cksum_orig);
2742
2743 return (0);
2744 }
2745
2746 /*
2747 * Issue the prefetch reads for any necessary indirect blocks.
2748 *
2749 * We use the object ignore list to tell us whether or not to issue prefetches
2750 * for a given object. We do this for both correctness (in case the blocksize
2751 * of an object has changed) and performance (if the object doesn't exist, don't
2752 * needlessly try to issue prefetches). We also trim the list as we go through
2753 * the stream to prevent it from growing to an unbounded size.
2754 *
2755 * The object numbers within will always be in sorted order, and any write
2756 * records we see will also be in sorted order, but they're not sorted with
2757 * respect to each other (i.e. we can get several object records before
2758 * receiving each object's write records). As a result, once we've reached a
2759 * given object number, we can safely remove any reference to lower object
2760 * numbers in the ignore list. In practice, we receive up to 32 object records
2761 * before receiving write records, so the list can have up to 32 nodes in it.
2762 */
2763 static void
2764 receive_read_prefetch(dmu_recv_cookie_t *drc, uint64_t object, uint64_t offset,
2765 uint64_t length)
2766 {
2767 if (!objlist_exists(drc->drc_ignore_objlist, object)) {
2768 dmu_prefetch(drc->drc_os, object, 1, offset, length,
2769 ZIO_PRIORITY_SYNC_READ);
2770 }
2771 }
2772
2773 /*
2774 * Read records off the stream, issuing any necessary prefetches.
2775 */
2776 static int
2777 receive_read_record(dmu_recv_cookie_t *drc)
2778 {
2779 int err;
2780
2781 switch (drc->drc_rrd->header.drr_type) {
2782 case DRR_OBJECT:
2783 {
2784 struct drr_object *drro =
2785 &drc->drc_rrd->header.drr_u.drr_object;
2786 uint32_t size = DRR_OBJECT_PAYLOAD_SIZE(drro);
2787 void *buf = NULL;
2788 dmu_object_info_t doi;
2789
2790 if (size != 0)
2791 buf = kmem_zalloc(size, KM_SLEEP);
2792
2793 err = receive_read_payload_and_next_header(drc, size, buf);
2794 if (err != 0) {
2795 kmem_free(buf, size);
2796 return (err);
2797 }
2798 err = dmu_object_info(drc->drc_os, drro->drr_object, &doi);
2799 /*
2800 * See receive_read_prefetch for an explanation why we're
2801 * storing this object in the ignore_obj_list.
2802 */
2803 if (err == ENOENT || err == EEXIST ||
2804 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2805 objlist_insert(drc->drc_ignore_objlist,
2806 drro->drr_object);
2807 err = 0;
2808 }
2809 return (err);
2810 }
2811 case DRR_FREEOBJECTS:
2812 {
2813 err = receive_read_payload_and_next_header(drc, 0, NULL);
2814 return (err);
2815 }
2816 case DRR_WRITE:
2817 {
2818 struct drr_write *drrw = &drc->drc_rrd->header.drr_u.drr_write;
2819 int size = DRR_WRITE_PAYLOAD_SIZE(drrw);
2820 abd_t *abd = abd_alloc_linear(size, B_FALSE);
2821 err = receive_read_payload_and_next_header(drc, size,
2822 abd_to_buf(abd));
2823 if (err != 0) {
2824 abd_free(abd);
2825 return (err);
2826 }
2827 drc->drc_rrd->abd = abd;
2828 receive_read_prefetch(drc, drrw->drr_object, drrw->drr_offset,
2829 drrw->drr_logical_size);
2830 return (err);
2831 }
2832 case DRR_WRITE_EMBEDDED:
2833 {
2834 struct drr_write_embedded *drrwe =
2835 &drc->drc_rrd->header.drr_u.drr_write_embedded;
2836 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2837 void *buf = kmem_zalloc(size, KM_SLEEP);
2838
2839 err = receive_read_payload_and_next_header(drc, size, buf);
2840 if (err != 0) {
2841 kmem_free(buf, size);
2842 return (err);
2843 }
2844
2845 receive_read_prefetch(drc, drrwe->drr_object, drrwe->drr_offset,
2846 drrwe->drr_length);
2847 return (err);
2848 }
2849 case DRR_FREE:
2850 case DRR_REDACT:
2851 {
2852 /*
2853 * It might be beneficial to prefetch indirect blocks here, but
2854 * we don't really have the data to decide for sure.
2855 */
2856 err = receive_read_payload_and_next_header(drc, 0, NULL);
2857 return (err);
2858 }
2859 case DRR_END:
2860 {
2861 struct drr_end *drre = &drc->drc_rrd->header.drr_u.drr_end;
2862 if (!ZIO_CHECKSUM_EQUAL(drc->drc_prev_cksum,
2863 drre->drr_checksum))
2864 return (SET_ERROR(ECKSUM));
2865 return (0);
2866 }
2867 case DRR_SPILL:
2868 {
2869 struct drr_spill *drrs = &drc->drc_rrd->header.drr_u.drr_spill;
2870 int size = DRR_SPILL_PAYLOAD_SIZE(drrs);
2871 abd_t *abd = abd_alloc_linear(size, B_FALSE);
2872 err = receive_read_payload_and_next_header(drc, size,
2873 abd_to_buf(abd));
2874 if (err != 0)
2875 abd_free(abd);
2876 else
2877 drc->drc_rrd->abd = abd;
2878 return (err);
2879 }
2880 case DRR_OBJECT_RANGE:
2881 {
2882 err = receive_read_payload_and_next_header(drc, 0, NULL);
2883 return (err);
2884
2885 }
2886 default:
2887 return (SET_ERROR(EINVAL));
2888 }
2889 }
2890
2891
2892
2893 static void
2894 dprintf_drr(struct receive_record_arg *rrd, int err)
2895 {
2896 #ifdef ZFS_DEBUG
2897 switch (rrd->header.drr_type) {
2898 case DRR_OBJECT:
2899 {
2900 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2901 dprintf("drr_type = OBJECT obj = %llu type = %u "
2902 "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
2903 "compress = %u dn_slots = %u err = %d\n",
2904 (u_longlong_t)drro->drr_object, drro->drr_type,
2905 drro->drr_bonustype, drro->drr_blksz, drro->drr_bonuslen,
2906 drro->drr_checksumtype, drro->drr_compress,
2907 drro->drr_dn_slots, err);
2908 break;
2909 }
2910 case DRR_FREEOBJECTS:
2911 {
2912 struct drr_freeobjects *drrfo =
2913 &rrd->header.drr_u.drr_freeobjects;
2914 dprintf("drr_type = FREEOBJECTS firstobj = %llu "
2915 "numobjs = %llu err = %d\n",
2916 (u_longlong_t)drrfo->drr_firstobj,
2917 (u_longlong_t)drrfo->drr_numobjs, err);
2918 break;
2919 }
2920 case DRR_WRITE:
2921 {
2922 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2923 dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
2924 "lsize = %llu cksumtype = %u flags = %u "
2925 "compress = %u psize = %llu err = %d\n",
2926 (u_longlong_t)drrw->drr_object, drrw->drr_type,
2927 (u_longlong_t)drrw->drr_offset,
2928 (u_longlong_t)drrw->drr_logical_size,
2929 drrw->drr_checksumtype, drrw->drr_flags,
2930 drrw->drr_compressiontype,
2931 (u_longlong_t)drrw->drr_compressed_size, err);
2932 break;
2933 }
2934 case DRR_WRITE_BYREF:
2935 {
2936 struct drr_write_byref *drrwbr =
2937 &rrd->header.drr_u.drr_write_byref;
2938 dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
2939 "length = %llu toguid = %llx refguid = %llx "
2940 "refobject = %llu refoffset = %llu cksumtype = %u "
2941 "flags = %u err = %d\n",
2942 (u_longlong_t)drrwbr->drr_object,
2943 (u_longlong_t)drrwbr->drr_offset,
2944 (u_longlong_t)drrwbr->drr_length,
2945 (u_longlong_t)drrwbr->drr_toguid,
2946 (u_longlong_t)drrwbr->drr_refguid,
2947 (u_longlong_t)drrwbr->drr_refobject,
2948 (u_longlong_t)drrwbr->drr_refoffset,
2949 drrwbr->drr_checksumtype, drrwbr->drr_flags, err);
2950 break;
2951 }
2952 case DRR_WRITE_EMBEDDED:
2953 {
2954 struct drr_write_embedded *drrwe =
2955 &rrd->header.drr_u.drr_write_embedded;
2956 dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
2957 "length = %llu compress = %u etype = %u lsize = %u "
2958 "psize = %u err = %d\n",
2959 (u_longlong_t)drrwe->drr_object,
2960 (u_longlong_t)drrwe->drr_offset,
2961 (u_longlong_t)drrwe->drr_length,
2962 drrwe->drr_compression, drrwe->drr_etype,
2963 drrwe->drr_lsize, drrwe->drr_psize, err);
2964 break;
2965 }
2966 case DRR_FREE:
2967 {
2968 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2969 dprintf("drr_type = FREE obj = %llu offset = %llu "
2970 "length = %lld err = %d\n",
2971 (u_longlong_t)drrf->drr_object,
2972 (u_longlong_t)drrf->drr_offset,
2973 (longlong_t)drrf->drr_length,
2974 err);
2975 break;
2976 }
2977 case DRR_SPILL:
2978 {
2979 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2980 dprintf("drr_type = SPILL obj = %llu length = %llu "
2981 "err = %d\n", (u_longlong_t)drrs->drr_object,
2982 (u_longlong_t)drrs->drr_length, err);
2983 break;
2984 }
2985 case DRR_OBJECT_RANGE:
2986 {
2987 struct drr_object_range *drror =
2988 &rrd->header.drr_u.drr_object_range;
2989 dprintf("drr_type = OBJECT_RANGE firstobj = %llu "
2990 "numslots = %llu flags = %u err = %d\n",
2991 (u_longlong_t)drror->drr_firstobj,
2992 (u_longlong_t)drror->drr_numslots,
2993 drror->drr_flags, err);
2994 break;
2995 }
2996 default:
2997 return;
2998 }
2999 #endif
3000 }
3001
3002 /*
3003 * Commit the records to the pool.
3004 */
3005 static int
3006 receive_process_record(struct receive_writer_arg *rwa,
3007 struct receive_record_arg *rrd)
3008 {
3009 int err;
3010
3011 /* Processing in order, therefore bytes_read should be increasing. */
3012 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
3013 rwa->bytes_read = rrd->bytes_read;
3014
3015 /* We can only heal write records; other ones get ignored */
3016 if (rwa->heal && rrd->header.drr_type != DRR_WRITE) {
3017 if (rrd->abd != NULL) {
3018 abd_free(rrd->abd);
3019 rrd->abd = NULL;
3020 } else if (rrd->payload != NULL) {
3021 kmem_free(rrd->payload, rrd->payload_size);
3022 rrd->payload = NULL;
3023 }
3024 return (0);
3025 }
3026
3027 if (!rwa->heal && rrd->header.drr_type != DRR_WRITE) {
3028 err = flush_write_batch(rwa);
3029 if (err != 0) {
3030 if (rrd->abd != NULL) {
3031 abd_free(rrd->abd);
3032 rrd->abd = NULL;
3033 rrd->payload = NULL;
3034 } else if (rrd->payload != NULL) {
3035 kmem_free(rrd->payload, rrd->payload_size);
3036 rrd->payload = NULL;
3037 }
3038
3039 return (err);
3040 }
3041 }
3042
3043 switch (rrd->header.drr_type) {
3044 case DRR_OBJECT:
3045 {
3046 struct drr_object *drro = &rrd->header.drr_u.drr_object;
3047 err = receive_object(rwa, drro, rrd->payload);
3048 kmem_free(rrd->payload, rrd->payload_size);
3049 rrd->payload = NULL;
3050 break;
3051 }
3052 case DRR_FREEOBJECTS:
3053 {
3054 struct drr_freeobjects *drrfo =
3055 &rrd->header.drr_u.drr_freeobjects;
3056 err = receive_freeobjects(rwa, drrfo);
3057 break;
3058 }
3059 case DRR_WRITE:
3060 {
3061 err = receive_process_write_record(rwa, rrd);
3062 if (rwa->heal) {
3063 /*
3064 * If healing - always free the abd after processing
3065 */
3066 abd_free(rrd->abd);
3067 rrd->abd = NULL;
3068 } else if (err != EAGAIN) {
3069 /*
3070 * On success, a non-healing
3071 * receive_process_write_record() returns
3072 * EAGAIN to indicate that we do not want to free
3073 * the rrd or arc_buf.
3074 */
3075 ASSERT(err != 0);
3076 abd_free(rrd->abd);
3077 rrd->abd = NULL;
3078 }
3079 break;
3080 }
3081 case DRR_WRITE_EMBEDDED:
3082 {
3083 struct drr_write_embedded *drrwe =
3084 &rrd->header.drr_u.drr_write_embedded;
3085 err = receive_write_embedded(rwa, drrwe, rrd->payload);
3086 kmem_free(rrd->payload, rrd->payload_size);
3087 rrd->payload = NULL;
3088 break;
3089 }
3090 case DRR_FREE:
3091 {
3092 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
3093 err = receive_free(rwa, drrf);
3094 break;
3095 }
3096 case DRR_SPILL:
3097 {
3098 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
3099 err = receive_spill(rwa, drrs, rrd->abd);
3100 if (err != 0)
3101 abd_free(rrd->abd);
3102 rrd->abd = NULL;
3103 rrd->payload = NULL;
3104 break;
3105 }
3106 case DRR_OBJECT_RANGE:
3107 {
3108 struct drr_object_range *drror =
3109 &rrd->header.drr_u.drr_object_range;
3110 err = receive_object_range(rwa, drror);
3111 break;
3112 }
3113 case DRR_REDACT:
3114 {
3115 struct drr_redact *drrr = &rrd->header.drr_u.drr_redact;
3116 err = receive_redact(rwa, drrr);
3117 break;
3118 }
3119 default:
3120 err = (SET_ERROR(EINVAL));
3121 }
3122
3123 if (err != 0)
3124 dprintf_drr(rrd, err);
3125
3126 return (err);
3127 }
3128
3129 /*
3130 * dmu_recv_stream's worker thread; pull records off the queue, and then call
3131 * receive_process_record When we're done, signal the main thread and exit.
3132 */
3133 static __attribute__((noreturn)) void
3134 receive_writer_thread(void *arg)
3135 {
3136 struct receive_writer_arg *rwa = arg;
3137 struct receive_record_arg *rrd;
3138 fstrans_cookie_t cookie = spl_fstrans_mark();
3139
3140 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
3141 rrd = bqueue_dequeue(&rwa->q)) {
3142 /*
3143 * If there's an error, the main thread will stop putting things
3144 * on the queue, but we need to clear everything in it before we
3145 * can exit.
3146 */
3147 int err = 0;
3148 if (rwa->err == 0) {
3149 err = receive_process_record(rwa, rrd);
3150 } else if (rrd->abd != NULL) {
3151 abd_free(rrd->abd);
3152 rrd->abd = NULL;
3153 rrd->payload = NULL;
3154 } else if (rrd->payload != NULL) {
3155 kmem_free(rrd->payload, rrd->payload_size);
3156 rrd->payload = NULL;
3157 }
3158 /*
3159 * EAGAIN indicates that this record has been saved (on
3160 * raw->write_batch), and will be used again, so we don't
3161 * free it.
3162 * When healing data we always need to free the record.
3163 */
3164 if (err != EAGAIN || rwa->heal) {
3165 if (rwa->err == 0)
3166 rwa->err = err;
3167 kmem_free(rrd, sizeof (*rrd));
3168 }
3169 }
3170 kmem_free(rrd, sizeof (*rrd));
3171
3172 if (rwa->heal) {
3173 zio_wait(rwa->heal_pio);
3174 } else {
3175 int err = flush_write_batch(rwa);
3176 if (rwa->err == 0)
3177 rwa->err = err;
3178 }
3179 mutex_enter(&rwa->mutex);
3180 rwa->done = B_TRUE;
3181 cv_signal(&rwa->cv);
3182 mutex_exit(&rwa->mutex);
3183 spl_fstrans_unmark(cookie);
3184 thread_exit();
3185 }
3186
3187 static int
3188 resume_check(dmu_recv_cookie_t *drc, nvlist_t *begin_nvl)
3189 {
3190 uint64_t val;
3191 objset_t *mos = dmu_objset_pool(drc->drc_os)->dp_meta_objset;
3192 uint64_t dsobj = dmu_objset_id(drc->drc_os);
3193 uint64_t resume_obj, resume_off;
3194
3195 if (nvlist_lookup_uint64(begin_nvl,
3196 "resume_object", &resume_obj) != 0 ||
3197 nvlist_lookup_uint64(begin_nvl,
3198 "resume_offset", &resume_off) != 0) {
3199 return (SET_ERROR(EINVAL));
3200 }
3201 VERIFY0(zap_lookup(mos, dsobj,
3202 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
3203 if (resume_obj != val)
3204 return (SET_ERROR(EINVAL));
3205 VERIFY0(zap_lookup(mos, dsobj,
3206 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
3207 if (resume_off != val)
3208 return (SET_ERROR(EINVAL));
3209
3210 return (0);
3211 }
3212
3213 /*
3214 * Read in the stream's records, one by one, and apply them to the pool. There
3215 * are two threads involved; the thread that calls this function will spin up a
3216 * worker thread, read the records off the stream one by one, and issue
3217 * prefetches for any necessary indirect blocks. It will then push the records
3218 * onto an internal blocking queue. The worker thread will pull the records off
3219 * the queue, and actually write the data into the DMU. This way, the worker
3220 * thread doesn't have to wait for reads to complete, since everything it needs
3221 * (the indirect blocks) will be prefetched.
3222 *
3223 * NB: callers *must* call dmu_recv_end() if this succeeds.
3224 */
3225 int
3226 dmu_recv_stream(dmu_recv_cookie_t *drc, offset_t *voffp)
3227 {
3228 int err = 0;
3229 struct receive_writer_arg *rwa = kmem_zalloc(sizeof (*rwa), KM_SLEEP);
3230
3231 if (dsl_dataset_has_resume_receive_state(drc->drc_ds)) {
3232 uint64_t bytes = 0;
3233 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
3234 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
3235 sizeof (bytes), 1, &bytes);
3236 drc->drc_bytes_read += bytes;
3237 }
3238
3239 drc->drc_ignore_objlist = objlist_create();
3240
3241 /* these were verified in dmu_recv_begin */
3242 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
3243 DMU_SUBSTREAM);
3244 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
3245
3246 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
3247 ASSERT0(drc->drc_os->os_encrypted &&
3248 (drc->drc_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA));
3249
3250 /* handle DSL encryption key payload */
3251 if (drc->drc_featureflags & DMU_BACKUP_FEATURE_RAW) {
3252 nvlist_t *keynvl = NULL;
3253
3254 ASSERT(drc->drc_os->os_encrypted);
3255 ASSERT(drc->drc_raw);
3256
3257 err = nvlist_lookup_nvlist(drc->drc_begin_nvl, "crypt_keydata",
3258 &keynvl);
3259 if (err != 0)
3260 goto out;
3261
3262 if (!drc->drc_heal) {
3263 /*
3264 * If this is a new dataset we set the key immediately.
3265 * Otherwise we don't want to change the key until we
3266 * are sure the rest of the receive succeeded so we
3267 * stash the keynvl away until then.
3268 */
3269 err = dsl_crypto_recv_raw(spa_name(drc->drc_os->os_spa),
3270 drc->drc_ds->ds_object, drc->drc_fromsnapobj,
3271 drc->drc_drrb->drr_type, keynvl, drc->drc_newfs);
3272 if (err != 0)
3273 goto out;
3274 }
3275
3276 /* see comment in dmu_recv_end_sync() */
3277 drc->drc_ivset_guid = 0;
3278 (void) nvlist_lookup_uint64(keynvl, "to_ivset_guid",
3279 &drc->drc_ivset_guid);
3280
3281 if (!drc->drc_newfs)
3282 drc->drc_keynvl = fnvlist_dup(keynvl);
3283 }
3284
3285 if (drc->drc_featureflags & DMU_BACKUP_FEATURE_RESUMING) {
3286 err = resume_check(drc, drc->drc_begin_nvl);
3287 if (err != 0)
3288 goto out;
3289 }
3290
3291 /*
3292 * If we failed before this point we will clean up any new resume
3293 * state that was created. Now that we've gotten past the initial
3294 * checks we are ok to retain that resume state.
3295 */
3296 drc->drc_should_save = B_TRUE;
3297
3298 (void) bqueue_init(&rwa->q, zfs_recv_queue_ff,
3299 MAX(zfs_recv_queue_length, 2 * zfs_max_recordsize),
3300 offsetof(struct receive_record_arg, node));
3301 cv_init(&rwa->cv, NULL, CV_DEFAULT, NULL);
3302 mutex_init(&rwa->mutex, NULL, MUTEX_DEFAULT, NULL);
3303 rwa->os = drc->drc_os;
3304 rwa->byteswap = drc->drc_byteswap;
3305 rwa->heal = drc->drc_heal;
3306 rwa->tofs = drc->drc_tofs;
3307 rwa->resumable = drc->drc_resumable;
3308 rwa->raw = drc->drc_raw;
3309 rwa->spill = drc->drc_spill;
3310 rwa->full = (drc->drc_drr_begin->drr_u.drr_begin.drr_fromguid == 0);
3311 rwa->os->os_raw_receive = drc->drc_raw;
3312 if (drc->drc_heal) {
3313 rwa->heal_pio = zio_root(drc->drc_os->os_spa, NULL, NULL,
3314 ZIO_FLAG_GODFATHER);
3315 }
3316 list_create(&rwa->write_batch, sizeof (struct receive_record_arg),
3317 offsetof(struct receive_record_arg, node.bqn_node));
3318
3319 (void) thread_create(NULL, 0, receive_writer_thread, rwa, 0, curproc,
3320 TS_RUN, minclsyspri);
3321 /*
3322 * We're reading rwa->err without locks, which is safe since we are the
3323 * only reader, and the worker thread is the only writer. It's ok if we
3324 * miss a write for an iteration or two of the loop, since the writer
3325 * thread will keep freeing records we send it until we send it an eos
3326 * marker.
3327 *
3328 * We can leave this loop in 3 ways: First, if rwa->err is
3329 * non-zero. In that case, the writer thread will free the rrd we just
3330 * pushed. Second, if we're interrupted; in that case, either it's the
3331 * first loop and drc->drc_rrd was never allocated, or it's later, and
3332 * drc->drc_rrd has been handed off to the writer thread who will free
3333 * it. Finally, if receive_read_record fails or we're at the end of the
3334 * stream, then we free drc->drc_rrd and exit.
3335 */
3336 while (rwa->err == 0) {
3337 if (issig(JUSTLOOKING) && issig(FORREAL)) {
3338 err = SET_ERROR(EINTR);
3339 break;
3340 }
3341
3342 ASSERT3P(drc->drc_rrd, ==, NULL);
3343 drc->drc_rrd = drc->drc_next_rrd;
3344 drc->drc_next_rrd = NULL;
3345 /* Allocates and loads header into drc->drc_next_rrd */
3346 err = receive_read_record(drc);
3347
3348 if (drc->drc_rrd->header.drr_type == DRR_END || err != 0) {
3349 kmem_free(drc->drc_rrd, sizeof (*drc->drc_rrd));
3350 drc->drc_rrd = NULL;
3351 break;
3352 }
3353
3354 bqueue_enqueue(&rwa->q, drc->drc_rrd,
3355 sizeof (struct receive_record_arg) +
3356 drc->drc_rrd->payload_size);
3357 drc->drc_rrd = NULL;
3358 }
3359
3360 ASSERT3P(drc->drc_rrd, ==, NULL);
3361 drc->drc_rrd = kmem_zalloc(sizeof (*drc->drc_rrd), KM_SLEEP);
3362 drc->drc_rrd->eos_marker = B_TRUE;
3363 bqueue_enqueue_flush(&rwa->q, drc->drc_rrd, 1);
3364
3365 mutex_enter(&rwa->mutex);
3366 while (!rwa->done) {
3367 /*
3368 * We need to use cv_wait_sig() so that any process that may
3369 * be sleeping here can still fork.
3370 */
3371 (void) cv_wait_sig(&rwa->cv, &rwa->mutex);
3372 }
3373 mutex_exit(&rwa->mutex);
3374
3375 /*
3376 * If we are receiving a full stream as a clone, all object IDs which
3377 * are greater than the maximum ID referenced in the stream are
3378 * by definition unused and must be freed.
3379 */
3380 if (drc->drc_clone && drc->drc_drrb->drr_fromguid == 0) {
3381 uint64_t obj = rwa->max_object + 1;
3382 int free_err = 0;
3383 int next_err = 0;
3384
3385 while (next_err == 0) {
3386 free_err = dmu_free_long_object(rwa->os, obj);
3387 if (free_err != 0 && free_err != ENOENT)
3388 break;
3389
3390 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0);
3391 }
3392
3393 if (err == 0) {
3394 if (free_err != 0 && free_err != ENOENT)
3395 err = free_err;
3396 else if (next_err != ESRCH)
3397 err = next_err;
3398 }
3399 }
3400
3401 cv_destroy(&rwa->cv);
3402 mutex_destroy(&rwa->mutex);
3403 bqueue_destroy(&rwa->q);
3404 list_destroy(&rwa->write_batch);
3405 if (err == 0)
3406 err = rwa->err;
3407
3408 out:
3409 /*
3410 * If we hit an error before we started the receive_writer_thread
3411 * we need to clean up the next_rrd we create by processing the
3412 * DRR_BEGIN record.
3413 */
3414 if (drc->drc_next_rrd != NULL)
3415 kmem_free(drc->drc_next_rrd, sizeof (*drc->drc_next_rrd));
3416
3417 /*
3418 * The objset will be invalidated by dmu_recv_end() when we do
3419 * dsl_dataset_clone_swap_sync_impl().
3420 */
3421 drc->drc_os = NULL;
3422
3423 kmem_free(rwa, sizeof (*rwa));
3424 nvlist_free(drc->drc_begin_nvl);
3425
3426 if (err != 0) {
3427 /*
3428 * Clean up references. If receive is not resumable,
3429 * destroy what we created, so we don't leave it in
3430 * the inconsistent state.
3431 */
3432 dmu_recv_cleanup_ds(drc);
3433 nvlist_free(drc->drc_keynvl);
3434 }
3435
3436 objlist_destroy(drc->drc_ignore_objlist);
3437 drc->drc_ignore_objlist = NULL;
3438 *voffp = drc->drc_voff;
3439 return (err);
3440 }
3441
3442 static int
3443 dmu_recv_end_check(void *arg, dmu_tx_t *tx)
3444 {
3445 dmu_recv_cookie_t *drc = arg;
3446 dsl_pool_t *dp = dmu_tx_pool(tx);
3447 int error;
3448
3449 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
3450
3451 if (drc->drc_heal) {
3452 error = 0;
3453 } else if (!drc->drc_newfs) {
3454 dsl_dataset_t *origin_head;
3455
3456 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
3457 if (error != 0)
3458 return (error);
3459 if (drc->drc_force) {
3460 /*
3461 * We will destroy any snapshots in tofs (i.e. before
3462 * origin_head) that are after the origin (which is
3463 * the snap before drc_ds, because drc_ds can not
3464 * have any snaps of its own).
3465 */
3466 uint64_t obj;
3467
3468 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3469 while (obj !=
3470 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3471 dsl_dataset_t *snap;
3472 error = dsl_dataset_hold_obj(dp, obj, FTAG,
3473 &snap);
3474 if (error != 0)
3475 break;
3476 if (snap->ds_dir != origin_head->ds_dir)
3477 error = SET_ERROR(EINVAL);
3478 if (error == 0) {
3479 error = dsl_destroy_snapshot_check_impl(
3480 snap, B_FALSE);
3481 }
3482 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3483 dsl_dataset_rele(snap, FTAG);
3484 if (error != 0)
3485 break;
3486 }
3487 if (error != 0) {
3488 dsl_dataset_rele(origin_head, FTAG);
3489 return (error);
3490 }
3491 }
3492 if (drc->drc_keynvl != NULL) {
3493 error = dsl_crypto_recv_raw_key_check(drc->drc_ds,
3494 drc->drc_keynvl, tx);
3495 if (error != 0) {
3496 dsl_dataset_rele(origin_head, FTAG);
3497 return (error);
3498 }
3499 }
3500
3501 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
3502 origin_head, drc->drc_force, drc->drc_owner, tx);
3503 if (error != 0) {
3504 dsl_dataset_rele(origin_head, FTAG);
3505 return (error);
3506 }
3507 error = dsl_dataset_snapshot_check_impl(origin_head,
3508 drc->drc_tosnap, tx, B_TRUE, 1,
3509 drc->drc_cred, drc->drc_proc);
3510 dsl_dataset_rele(origin_head, FTAG);
3511 if (error != 0)
3512 return (error);
3513
3514 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
3515 } else {
3516 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
3517 drc->drc_tosnap, tx, B_TRUE, 1,
3518 drc->drc_cred, drc->drc_proc);
3519 }
3520 return (error);
3521 }
3522
3523 static void
3524 dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
3525 {
3526 dmu_recv_cookie_t *drc = arg;
3527 dsl_pool_t *dp = dmu_tx_pool(tx);
3528 boolean_t encrypted = drc->drc_ds->ds_dir->dd_crypto_obj != 0;
3529 uint64_t newsnapobj = 0;
3530
3531 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
3532 tx, "snap=%s", drc->drc_tosnap);
3533 drc->drc_ds->ds_objset->os_raw_receive = B_FALSE;
3534
3535 if (drc->drc_heal) {
3536 if (drc->drc_keynvl != NULL) {
3537 nvlist_free(drc->drc_keynvl);
3538 drc->drc_keynvl = NULL;
3539 }
3540 } else if (!drc->drc_newfs) {
3541 dsl_dataset_t *origin_head;
3542
3543 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3544 &origin_head));
3545
3546 if (drc->drc_force) {
3547 /*
3548 * Destroy any snapshots of drc_tofs (origin_head)
3549 * after the origin (the snap before drc_ds).
3550 */
3551 uint64_t obj;
3552
3553 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3554 while (obj !=
3555 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3556 dsl_dataset_t *snap;
3557 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
3558 &snap));
3559 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3560 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3561 dsl_destroy_snapshot_sync_impl(snap,
3562 B_FALSE, tx);
3563 dsl_dataset_rele(snap, FTAG);
3564 }
3565 }
3566 if (drc->drc_keynvl != NULL) {
3567 dsl_crypto_recv_raw_key_sync(drc->drc_ds,
3568 drc->drc_keynvl, tx);
3569 nvlist_free(drc->drc_keynvl);
3570 drc->drc_keynvl = NULL;
3571 }
3572
3573 VERIFY3P(drc->drc_ds->ds_prev, ==,
3574 origin_head->ds_prev);
3575
3576 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3577 origin_head, tx);
3578 /*
3579 * The objset was evicted by dsl_dataset_clone_swap_sync_impl,
3580 * so drc_os is no longer valid.
3581 */
3582 drc->drc_os = NULL;
3583
3584 dsl_dataset_snapshot_sync_impl(origin_head,
3585 drc->drc_tosnap, tx);
3586
3587 /* set snapshot's creation time and guid */
3588 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3589 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
3590 drc->drc_drrb->drr_creation_time;
3591 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
3592 drc->drc_drrb->drr_toguid;
3593 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
3594 ~DS_FLAG_INCONSISTENT;
3595
3596 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3597 dsl_dataset_phys(origin_head)->ds_flags &=
3598 ~DS_FLAG_INCONSISTENT;
3599
3600 newsnapobj =
3601 dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3602
3603 dsl_dataset_rele(origin_head, FTAG);
3604 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
3605
3606 if (drc->drc_owner != NULL)
3607 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3608 } else {
3609 dsl_dataset_t *ds = drc->drc_ds;
3610
3611 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
3612
3613 /* set snapshot's creation time and guid */
3614 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3615 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
3616 drc->drc_drrb->drr_creation_time;
3617 dsl_dataset_phys(ds->ds_prev)->ds_guid =
3618 drc->drc_drrb->drr_toguid;
3619 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3620 ~DS_FLAG_INCONSISTENT;
3621
3622 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3623 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
3624 if (dsl_dataset_has_resume_receive_state(ds)) {
3625 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3626 DS_FIELD_RESUME_FROMGUID, tx);
3627 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3628 DS_FIELD_RESUME_OBJECT, tx);
3629 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3630 DS_FIELD_RESUME_OFFSET, tx);
3631 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3632 DS_FIELD_RESUME_BYTES, tx);
3633 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3634 DS_FIELD_RESUME_TOGUID, tx);
3635 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3636 DS_FIELD_RESUME_TONAME, tx);
3637 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3638 DS_FIELD_RESUME_REDACT_BOOKMARK_SNAPS, tx);
3639 }
3640 newsnapobj =
3641 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
3642 }
3643
3644 /*
3645 * If this is a raw receive, the crypt_keydata nvlist will include
3646 * a to_ivset_guid for us to set on the new snapshot. This value
3647 * will override the value generated by the snapshot code. However,
3648 * this value may not be present, because older implementations of
3649 * the raw send code did not include this value, and we are still
3650 * allowed to receive them if the zfs_disable_ivset_guid_check
3651 * tunable is set, in which case we will leave the newly-generated
3652 * value.
3653 */
3654 if (!drc->drc_heal && drc->drc_raw && drc->drc_ivset_guid != 0) {
3655 dmu_object_zapify(dp->dp_meta_objset, newsnapobj,
3656 DMU_OT_DSL_DATASET, tx);
3657 VERIFY0(zap_update(dp->dp_meta_objset, newsnapobj,
3658 DS_FIELD_IVSET_GUID, sizeof (uint64_t), 1,
3659 &drc->drc_ivset_guid, tx));
3660 }
3661
3662 /*
3663 * Release the hold from dmu_recv_begin. This must be done before
3664 * we return to open context, so that when we free the dataset's dnode
3665 * we can evict its bonus buffer. Since the dataset may be destroyed
3666 * at this point (and therefore won't have a valid pointer to the spa)
3667 * we release the key mapping manually here while we do have a valid
3668 * pointer, if it exists.
3669 */
3670 if (!drc->drc_raw && encrypted) {
3671 (void) spa_keystore_remove_mapping(dmu_tx_pool(tx)->dp_spa,
3672 drc->drc_ds->ds_object, drc->drc_ds);
3673 }
3674 dsl_dataset_disown(drc->drc_ds, 0, dmu_recv_tag);
3675 drc->drc_ds = NULL;
3676 }
3677
3678 static int dmu_recv_end_modified_blocks = 3;
3679
3680 static int
3681 dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3682 {
3683 #ifdef _KERNEL
3684 /*
3685 * We will be destroying the ds; make sure its origin is unmounted if
3686 * necessary.
3687 */
3688 char name[ZFS_MAX_DATASET_NAME_LEN];
3689 dsl_dataset_name(drc->drc_ds, name);
3690 zfs_destroy_unmount_origin(name);
3691 #endif
3692
3693 return (dsl_sync_task(drc->drc_tofs,
3694 dmu_recv_end_check, dmu_recv_end_sync, drc,
3695 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3696 }
3697
3698 static int
3699 dmu_recv_new_end(dmu_recv_cookie_t *drc)
3700 {
3701 return (dsl_sync_task(drc->drc_tofs,
3702 dmu_recv_end_check, dmu_recv_end_sync, drc,
3703 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3704 }
3705
3706 int
3707 dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3708 {
3709 int error;
3710
3711 drc->drc_owner = owner;
3712
3713 if (drc->drc_newfs)
3714 error = dmu_recv_new_end(drc);
3715 else
3716 error = dmu_recv_existing_end(drc);
3717
3718 if (error != 0) {
3719 dmu_recv_cleanup_ds(drc);
3720 nvlist_free(drc->drc_keynvl);
3721 } else if (!drc->drc_heal) {
3722 if (drc->drc_newfs) {
3723 zvol_create_minor(drc->drc_tofs);
3724 }
3725 char *snapname = kmem_asprintf("%s@%s",
3726 drc->drc_tofs, drc->drc_tosnap);
3727 zvol_create_minor(snapname);
3728 kmem_strfree(snapname);
3729 }
3730 return (error);
3731 }
3732
3733 /*
3734 * Return TRUE if this objset is currently being received into.
3735 */
3736 boolean_t
3737 dmu_objset_is_receiving(objset_t *os)
3738 {
3739 return (os->os_dsl_dataset != NULL &&
3740 os->os_dsl_dataset->ds_owner == dmu_recv_tag);
3741 }
3742
3743 ZFS_MODULE_PARAM(zfs_recv, zfs_recv_, queue_length, UINT, ZMOD_RW,
3744 "Maximum receive queue length");
3745
3746 ZFS_MODULE_PARAM(zfs_recv, zfs_recv_, queue_ff, UINT, ZMOD_RW,
3747 "Receive queue fill fraction");
3748
3749 ZFS_MODULE_PARAM(zfs_recv, zfs_recv_, write_batch_size, UINT, ZMOD_RW,
3750 "Maximum amount of writes to batch into one transaction");
3751
3752 ZFS_MODULE_PARAM(zfs_recv, zfs_recv_, best_effort_corrective, INT, ZMOD_RW,
3753 "Ignore errors during corrective receive");
3754 /* END CSTYLED */