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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
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, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26 * Copyright 2014 HybridCluster. All rights reserved.
27 * Copyright 2016 RackTop Systems.
28 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
29 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
33 #include <sys/dmu_impl.h>
34 #include <sys/dmu_tx.h>
36 #include <sys/dnode.h>
37 #include <sys/zfs_context.h>
38 #include <sys/dmu_objset.h>
39 #include <sys/dmu_traverse.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/dsl_dir.h>
42 #include <sys/dsl_prop.h>
43 #include <sys/dsl_pool.h>
44 #include <sys/dsl_synctask.h>
45 #include <sys/spa_impl.h>
46 #include <sys/zfs_ioctl.h>
48 #include <sys/zio_checksum.h>
49 #include <sys/zfs_znode.h>
50 #include <zfs_fletcher.h>
53 #include <sys/zfs_onexit.h>
54 #include <sys/dmu_recv.h>
55 #include <sys/dsl_destroy.h>
56 #include <sys/blkptr.h>
57 #include <sys/dsl_bookmark.h>
58 #include <sys/zfeature.h>
59 #include <sys/bqueue.h>
61 #include <sys/policy.h>
63 int zfs_recv_queue_length
= SPA_MAXBLOCKSIZE
;
65 static char *dmu_recv_tag
= "dmu_recv_tag";
66 const char *recv_clone_name
= "%recv";
68 static void byteswap_record(dmu_replay_record_t
*drr
);
70 typedef struct dmu_recv_begin_arg
{
71 const char *drba_origin
;
72 dmu_recv_cookie_t
*drba_cookie
;
74 dsl_crypto_params_t
*drba_dcp
;
75 uint64_t drba_snapobj
;
76 } dmu_recv_begin_arg_t
;
79 recv_begin_check_existing_impl(dmu_recv_begin_arg_t
*drba
, dsl_dataset_t
*ds
,
80 uint64_t fromguid
, uint64_t featureflags
)
85 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
86 boolean_t encrypted
= ds
->ds_dir
->dd_crypto_obj
!= 0;
87 boolean_t raw
= (featureflags
& DMU_BACKUP_FEATURE_RAW
) != 0;
88 boolean_t embed
= (featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) != 0;
90 /* temporary clone name must not exist */
91 error
= zap_lookup(dp
->dp_meta_objset
,
92 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, recv_clone_name
,
95 return (error
== 0 ? EBUSY
: error
);
97 /* new snapshot name must not exist */
98 error
= zap_lookup(dp
->dp_meta_objset
,
99 dsl_dataset_phys(ds
)->ds_snapnames_zapobj
,
100 drba
->drba_cookie
->drc_tosnap
, 8, 1, &val
);
102 return (error
== 0 ? EEXIST
: error
);
104 /* must not have children if receiving a ZVOL */
105 error
= zap_count(dp
->dp_meta_objset
,
106 dsl_dir_phys(ds
->ds_dir
)->dd_child_dir_zapobj
, &children
);
109 if (drba
->drba_cookie
->drc_drrb
->drr_type
!= DMU_OST_ZFS
&&
111 return (SET_ERROR(ZFS_ERR_WRONG_PARENT
));
114 * Check snapshot limit before receiving. We'll recheck again at the
115 * end, but might as well abort before receiving if we're already over
118 * Note that we do not check the file system limit with
119 * dsl_dir_fscount_check because the temporary %clones don't count
120 * against that limit.
122 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1, ZFS_PROP_SNAPSHOT_LIMIT
,
123 NULL
, drba
->drba_cred
);
129 uint64_t obj
= dsl_dataset_phys(ds
)->ds_prev_snap_obj
;
131 /* Can't perform a raw receive on top of a non-raw receive */
132 if (!encrypted
&& raw
)
133 return (SET_ERROR(EINVAL
));
135 /* Encryption is incompatible with embedded data */
136 if (encrypted
&& embed
)
137 return (SET_ERROR(EINVAL
));
139 /* Find snapshot in this dir that matches fromguid. */
141 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
144 return (SET_ERROR(ENODEV
));
145 if (snap
->ds_dir
!= ds
->ds_dir
) {
146 dsl_dataset_rele(snap
, FTAG
);
147 return (SET_ERROR(ENODEV
));
149 if (dsl_dataset_phys(snap
)->ds_guid
== fromguid
)
151 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
152 dsl_dataset_rele(snap
, FTAG
);
155 return (SET_ERROR(ENODEV
));
157 if (drba
->drba_cookie
->drc_force
) {
158 drba
->drba_snapobj
= obj
;
161 * If we are not forcing, there must be no
162 * changes since fromsnap.
164 if (dsl_dataset_modified_since_snap(ds
, snap
)) {
165 dsl_dataset_rele(snap
, FTAG
);
166 return (SET_ERROR(ETXTBSY
));
168 drba
->drba_snapobj
= ds
->ds_prev
->ds_object
;
171 dsl_dataset_rele(snap
, FTAG
);
173 /* if full, then must be forced */
174 if (!drba
->drba_cookie
->drc_force
)
175 return (SET_ERROR(EEXIST
));
178 * We don't support using zfs recv -F to blow away
179 * encrypted filesystems. This would require the
180 * dsl dir to point to the old encryption key and
181 * the new one at the same time during the receive.
183 if ((!encrypted
&& raw
) || encrypted
)
184 return (SET_ERROR(EINVAL
));
187 * Perform the same encryption checks we would if
188 * we were creating a new dataset from scratch.
191 boolean_t will_encrypt
;
193 error
= dmu_objset_create_crypt_check(
194 ds
->ds_dir
->dd_parent
, drba
->drba_dcp
,
199 if (will_encrypt
&& embed
)
200 return (SET_ERROR(EINVAL
));
203 drba
->drba_snapobj
= 0;
211 dmu_recv_begin_check(void *arg
, dmu_tx_t
*tx
)
213 dmu_recv_begin_arg_t
*drba
= arg
;
214 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
215 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
216 uint64_t fromguid
= drrb
->drr_fromguid
;
217 int flags
= drrb
->drr_flags
;
218 ds_hold_flags_t dsflags
= 0;
220 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
222 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
224 /* already checked */
225 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
226 ASSERT(!(featureflags
& DMU_BACKUP_FEATURE_RESUMING
));
228 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
229 DMU_COMPOUNDSTREAM
||
230 drrb
->drr_type
>= DMU_OST_NUMTYPES
||
231 ((flags
& DRR_FLAG_CLONE
) && drba
->drba_origin
== NULL
))
232 return (SET_ERROR(EINVAL
));
234 /* Verify pool version supports SA if SA_SPILL feature set */
235 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
236 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
237 return (SET_ERROR(ENOTSUP
));
239 if (drba
->drba_cookie
->drc_resumable
&&
240 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EXTENSIBLE_DATASET
))
241 return (SET_ERROR(ENOTSUP
));
244 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
245 * record to a plain WRITE record, so the pool must have the
246 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
247 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
249 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
250 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
251 return (SET_ERROR(ENOTSUP
));
252 if ((featureflags
& DMU_BACKUP_FEATURE_LZ4
) &&
253 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
254 return (SET_ERROR(ENOTSUP
));
257 * The receiving code doesn't know how to translate large blocks
258 * to smaller ones, so the pool must have the LARGE_BLOCKS
259 * feature enabled if the stream has LARGE_BLOCKS. Same with
262 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
263 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
264 return (SET_ERROR(ENOTSUP
));
265 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_DNODE
) &&
266 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_DNODE
))
267 return (SET_ERROR(ENOTSUP
));
269 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
270 /* raw receives require the encryption feature */
271 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ENCRYPTION
))
272 return (SET_ERROR(ENOTSUP
));
274 /* embedded data is incompatible with encryption and raw recv */
275 if (featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)
276 return (SET_ERROR(EINVAL
));
278 dsflags
|= DS_HOLD_FLAG_DECRYPT
;
281 error
= dsl_dataset_hold_flags(dp
, tofs
, dsflags
, FTAG
, &ds
);
283 /* target fs already exists; recv into temp clone */
285 /* Can't recv a clone into an existing fs */
286 if (flags
& DRR_FLAG_CLONE
|| drba
->drba_origin
) {
287 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
288 return (SET_ERROR(EINVAL
));
291 error
= recv_begin_check_existing_impl(drba
, ds
, fromguid
,
293 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
294 } else if (error
== ENOENT
) {
295 /* target fs does not exist; must be a full backup or clone */
296 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
300 * If it's a non-clone incremental, we are missing the
301 * target fs, so fail the recv.
303 if (fromguid
!= 0 && !(flags
& DRR_FLAG_CLONE
||
305 return (SET_ERROR(ENOENT
));
308 * If we're receiving a full send as a clone, and it doesn't
309 * contain all the necessary free records and freeobject
310 * records, reject it.
312 if (fromguid
== 0 && drba
->drba_origin
&&
313 !(flags
& DRR_FLAG_FREERECORDS
))
314 return (SET_ERROR(EINVAL
));
316 /* Open the parent of tofs */
317 ASSERT3U(strlen(tofs
), <, sizeof (buf
));
318 (void) strlcpy(buf
, tofs
, strrchr(tofs
, '/') - tofs
+ 1);
319 error
= dsl_dataset_hold_flags(dp
, buf
, dsflags
, FTAG
, &ds
);
323 if ((featureflags
& DMU_BACKUP_FEATURE_RAW
) == 0 &&
324 drba
->drba_origin
== NULL
) {
325 boolean_t will_encrypt
;
328 * Check that we aren't breaking any encryption rules
329 * and that we have all the parameters we need to
330 * create an encrypted dataset if necessary. If we are
331 * making an encrypted dataset the stream can't have
334 error
= dmu_objset_create_crypt_check(ds
->ds_dir
,
335 drba
->drba_dcp
, &will_encrypt
);
337 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
342 (featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)) {
343 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
344 return (SET_ERROR(EINVAL
));
349 * Check filesystem and snapshot limits before receiving. We'll
350 * recheck snapshot limits again at the end (we create the
351 * filesystems and increment those counts during begin_sync).
353 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
354 ZFS_PROP_FILESYSTEM_LIMIT
, NULL
, drba
->drba_cred
);
356 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
360 error
= dsl_fs_ss_limit_check(ds
->ds_dir
, 1,
361 ZFS_PROP_SNAPSHOT_LIMIT
, NULL
, drba
->drba_cred
);
363 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
367 /* can't recv below anything but filesystems (eg. no ZVOLs) */
368 error
= dmu_objset_from_ds(ds
, &os
);
370 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
373 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
374 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
375 return (SET_ERROR(ZFS_ERR_WRONG_PARENT
));
378 if (drba
->drba_origin
!= NULL
) {
379 dsl_dataset_t
*origin
;
381 error
= dsl_dataset_hold_flags(dp
, drba
->drba_origin
,
382 dsflags
, FTAG
, &origin
);
384 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
387 if (!origin
->ds_is_snapshot
) {
388 dsl_dataset_rele_flags(origin
, dsflags
, FTAG
);
389 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
390 return (SET_ERROR(EINVAL
));
392 if (dsl_dataset_phys(origin
)->ds_guid
!= fromguid
&&
394 dsl_dataset_rele_flags(origin
, dsflags
, FTAG
);
395 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
396 return (SET_ERROR(ENODEV
));
398 if (origin
->ds_dir
->dd_crypto_obj
!= 0 &&
399 (featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
)) {
400 dsl_dataset_rele_flags(origin
, dsflags
, FTAG
);
401 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
402 return (SET_ERROR(EINVAL
));
404 dsl_dataset_rele_flags(origin
,
408 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
415 dmu_recv_begin_sync(void *arg
, dmu_tx_t
*tx
)
417 dmu_recv_begin_arg_t
*drba
= arg
;
418 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
419 objset_t
*mos
= dp
->dp_meta_objset
;
420 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
421 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
422 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
423 dsl_dataset_t
*ds
, *newds
;
426 ds_hold_flags_t dsflags
= 0;
428 uint64_t crflags
= 0;
429 dsl_crypto_params_t dummy_dcp
= { 0 };
430 dsl_crypto_params_t
*dcp
= drba
->drba_dcp
;
432 if (drrb
->drr_flags
& DRR_FLAG_CI_DATA
)
433 crflags
|= DS_FLAG_CI_DATASET
;
435 if ((featureflags
& DMU_BACKUP_FEATURE_RAW
) == 0)
436 dsflags
|= DS_HOLD_FLAG_DECRYPT
;
439 * Raw, non-incremental recvs always use a dummy dcp with
440 * the raw cmd set. Raw incremental recvs do not use a dcp
441 * since the encryption parameters are already set in stone.
443 if (dcp
== NULL
&& drba
->drba_snapobj
== 0 &&
444 drba
->drba_origin
== NULL
) {
445 ASSERT3P(dcp
, ==, NULL
);
448 if (featureflags
& DMU_BACKUP_FEATURE_RAW
)
449 dcp
->cp_cmd
= DCP_CMD_RAW_RECV
;
452 error
= dsl_dataset_hold_flags(dp
, tofs
, dsflags
, FTAG
, &ds
);
454 /* create temporary clone */
455 dsl_dataset_t
*snap
= NULL
;
457 if (drba
->drba_snapobj
!= 0) {
458 VERIFY0(dsl_dataset_hold_obj(dp
,
459 drba
->drba_snapobj
, FTAG
, &snap
));
460 ASSERT3P(dcp
, ==, NULL
);
463 dsobj
= dsl_dataset_create_sync(ds
->ds_dir
, recv_clone_name
,
464 snap
, crflags
, drba
->drba_cred
, dcp
, tx
);
465 if (drba
->drba_snapobj
!= 0)
466 dsl_dataset_rele(snap
, FTAG
);
467 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
471 dsl_dataset_t
*origin
= NULL
;
473 VERIFY0(dsl_dir_hold(dp
, tofs
, FTAG
, &dd
, &tail
));
475 if (drba
->drba_origin
!= NULL
) {
476 VERIFY0(dsl_dataset_hold(dp
, drba
->drba_origin
,
478 ASSERT3P(dcp
, ==, NULL
);
481 /* Create new dataset. */
482 dsobj
= dsl_dataset_create_sync(dd
, strrchr(tofs
, '/') + 1,
483 origin
, crflags
, drba
->drba_cred
, dcp
, tx
);
485 dsl_dataset_rele(origin
, FTAG
);
486 dsl_dir_rele(dd
, FTAG
);
487 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
490 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dsflags
, dmu_recv_tag
, &newds
));
491 VERIFY0(dmu_objset_from_ds(newds
, &os
));
493 if (drba
->drba_cookie
->drc_resumable
) {
494 dsl_dataset_zapify(newds
, tx
);
495 if (drrb
->drr_fromguid
!= 0) {
496 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_FROMGUID
,
497 8, 1, &drrb
->drr_fromguid
, tx
));
499 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TOGUID
,
500 8, 1, &drrb
->drr_toguid
, tx
));
501 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_TONAME
,
502 1, strlen(drrb
->drr_toname
) + 1, drrb
->drr_toname
, tx
));
505 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OBJECT
,
507 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_OFFSET
,
509 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_BYTES
,
511 if (featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) {
512 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_LARGEBLOCK
,
515 if (featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) {
516 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_EMBEDOK
,
519 if (featureflags
& DMU_BACKUP_FEATURE_COMPRESSED
) {
520 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_COMPRESSOK
,
523 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
524 VERIFY0(zap_add(mos
, dsobj
, DS_FIELD_RESUME_RAWOK
,
530 * Usually the os->os_encrypted value is tied to the presence of a
531 * DSL Crypto Key object in the dd. However, that will not be received
532 * until dmu_recv_stream(), so we set the value manually for now.
534 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
535 os
->os_encrypted
= B_TRUE
;
536 drba
->drba_cookie
->drc_raw
= B_TRUE
;
539 dmu_buf_will_dirty(newds
->ds_dbuf
, tx
);
540 dsl_dataset_phys(newds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
543 * If we actually created a non-clone, we need to create the objset
544 * in our new dataset. If this is a raw send we postpone this until
545 * dmu_recv_stream() so that we can allocate the metadnode with the
546 * properties from the DRR_BEGIN payload.
548 rrw_enter(&newds
->ds_bp_rwlock
, RW_READER
, FTAG
);
549 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds
)) &&
550 (featureflags
& DMU_BACKUP_FEATURE_RAW
) == 0) {
551 (void) dmu_objset_create_impl(dp
->dp_spa
,
552 newds
, dsl_dataset_get_blkptr(newds
), drrb
->drr_type
, tx
);
554 rrw_exit(&newds
->ds_bp_rwlock
, FTAG
);
556 drba
->drba_cookie
->drc_ds
= newds
;
558 spa_history_log_internal_ds(newds
, "receive", tx
, "");
562 dmu_recv_resume_begin_check(void *arg
, dmu_tx_t
*tx
)
564 dmu_recv_begin_arg_t
*drba
= arg
;
565 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
566 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
568 ds_hold_flags_t dsflags
= 0;
569 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
571 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
573 /* already checked */
574 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
575 ASSERT(featureflags
& DMU_BACKUP_FEATURE_RESUMING
);
577 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
578 DMU_COMPOUNDSTREAM
||
579 drrb
->drr_type
>= DMU_OST_NUMTYPES
)
580 return (SET_ERROR(EINVAL
));
582 /* Verify pool version supports SA if SA_SPILL feature set */
583 if ((featureflags
& DMU_BACKUP_FEATURE_SA_SPILL
) &&
584 spa_version(dp
->dp_spa
) < SPA_VERSION_SA
)
585 return (SET_ERROR(ENOTSUP
));
588 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
589 * record to a plain WRITE record, so the pool must have the
590 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
591 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
593 if ((featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
) &&
594 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_EMBEDDED_DATA
))
595 return (SET_ERROR(ENOTSUP
));
596 if ((featureflags
& DMU_BACKUP_FEATURE_LZ4
) &&
597 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LZ4_COMPRESS
))
598 return (SET_ERROR(ENOTSUP
));
601 * The receiving code doesn't know how to translate large blocks
602 * to smaller ones, so the pool must have the LARGE_BLOCKS
603 * feature enabled if the stream has LARGE_BLOCKS. Same with
606 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_BLOCKS
) &&
607 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_BLOCKS
))
608 return (SET_ERROR(ENOTSUP
));
609 if ((featureflags
& DMU_BACKUP_FEATURE_LARGE_DNODE
) &&
610 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_LARGE_DNODE
))
611 return (SET_ERROR(ENOTSUP
));
613 /* 6 extra bytes for /%recv */
614 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
615 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
616 tofs
, recv_clone_name
);
618 if ((featureflags
& DMU_BACKUP_FEATURE_RAW
) == 0)
619 dsflags
|= DS_HOLD_FLAG_DECRYPT
;
621 if (dsl_dataset_hold_flags(dp
, recvname
, dsflags
, FTAG
, &ds
) != 0) {
622 /* %recv does not exist; continue in tofs */
623 error
= dsl_dataset_hold_flags(dp
, tofs
, dsflags
, FTAG
, &ds
);
628 /* check that ds is marked inconsistent */
629 if (!DS_IS_INCONSISTENT(ds
)) {
630 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
631 return (SET_ERROR(EINVAL
));
634 /* check that there is resuming data, and that the toguid matches */
635 if (!dsl_dataset_is_zapified(ds
)) {
636 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
637 return (SET_ERROR(EINVAL
));
640 error
= zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
641 DS_FIELD_RESUME_TOGUID
, sizeof (val
), 1, &val
);
642 if (error
!= 0 || drrb
->drr_toguid
!= val
) {
643 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
644 return (SET_ERROR(EINVAL
));
648 * Check if the receive is still running. If so, it will be owned.
649 * Note that nothing else can own the dataset (e.g. after the receive
650 * fails) because it will be marked inconsistent.
652 if (dsl_dataset_has_owner(ds
)) {
653 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
654 return (SET_ERROR(EBUSY
));
657 /* There should not be any snapshots of this fs yet. */
658 if (ds
->ds_prev
!= NULL
&& ds
->ds_prev
->ds_dir
== ds
->ds_dir
) {
659 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
660 return (SET_ERROR(EINVAL
));
664 * Note: resume point will be checked when we process the first WRITE
668 /* check that the origin matches */
670 (void) zap_lookup(dp
->dp_meta_objset
, ds
->ds_object
,
671 DS_FIELD_RESUME_FROMGUID
, sizeof (val
), 1, &val
);
672 if (drrb
->drr_fromguid
!= val
) {
673 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
674 return (SET_ERROR(EINVAL
));
677 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
682 dmu_recv_resume_begin_sync(void *arg
, dmu_tx_t
*tx
)
684 dmu_recv_begin_arg_t
*drba
= arg
;
685 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
686 const char *tofs
= drba
->drba_cookie
->drc_tofs
;
687 struct drr_begin
*drrb
= drba
->drba_cookie
->drc_drrb
;
688 uint64_t featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
691 ds_hold_flags_t dsflags
= 0;
693 /* 6 extra bytes for /%recv */
694 char recvname
[ZFS_MAX_DATASET_NAME_LEN
+ 6];
696 (void) snprintf(recvname
, sizeof (recvname
), "%s/%s",
697 tofs
, recv_clone_name
);
699 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
700 drba
->drba_cookie
->drc_raw
= B_TRUE
;
702 dsflags
|= DS_HOLD_FLAG_DECRYPT
;
705 if (dsl_dataset_hold_flags(dp
, recvname
, dsflags
, FTAG
, &ds
) != 0) {
706 /* %recv does not exist; continue in tofs */
707 VERIFY0(dsl_dataset_hold_flags(dp
, tofs
, dsflags
, FTAG
, &ds
));
708 drba
->drba_cookie
->drc_newfs
= B_TRUE
;
711 /* clear the inconsistent flag so that we can own it */
712 ASSERT(DS_IS_INCONSISTENT(ds
));
713 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
714 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
715 dsobj
= ds
->ds_object
;
716 dsl_dataset_rele_flags(ds
, dsflags
, FTAG
);
718 VERIFY0(dsl_dataset_own_obj(dp
, dsobj
, dsflags
, dmu_recv_tag
, &ds
));
719 VERIFY0(dmu_objset_from_ds(ds
, &os
));
721 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
722 dsl_dataset_phys(ds
)->ds_flags
|= DS_FLAG_INCONSISTENT
;
724 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
725 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds
)) ||
726 drba
->drba_cookie
->drc_raw
);
727 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
729 drba
->drba_cookie
->drc_ds
= ds
;
731 spa_history_log_internal_ds(ds
, "resume receive", tx
, "");
735 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
736 * succeeds; otherwise we will leak the holds on the datasets.
739 dmu_recv_begin(char *tofs
, char *tosnap
, dmu_replay_record_t
*drr_begin
,
740 boolean_t force
, boolean_t resumable
, nvlist_t
*localprops
,
741 nvlist_t
*hidden_args
, char *origin
, dmu_recv_cookie_t
*drc
)
743 dmu_recv_begin_arg_t drba
= { 0 };
745 bzero(drc
, sizeof (dmu_recv_cookie_t
));
746 drc
->drc_drr_begin
= drr_begin
;
747 drc
->drc_drrb
= &drr_begin
->drr_u
.drr_begin
;
748 drc
->drc_tosnap
= tosnap
;
749 drc
->drc_tofs
= tofs
;
750 drc
->drc_force
= force
;
751 drc
->drc_resumable
= resumable
;
752 drc
->drc_cred
= CRED();
753 drc
->drc_clone
= (origin
!= NULL
);
755 if (drc
->drc_drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
756 drc
->drc_byteswap
= B_TRUE
;
757 (void) fletcher_4_incremental_byteswap(drr_begin
,
758 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
759 byteswap_record(drr_begin
);
760 } else if (drc
->drc_drrb
->drr_magic
== DMU_BACKUP_MAGIC
) {
761 (void) fletcher_4_incremental_native(drr_begin
,
762 sizeof (dmu_replay_record_t
), &drc
->drc_cksum
);
764 return (SET_ERROR(EINVAL
));
767 drba
.drba_origin
= origin
;
768 drba
.drba_cookie
= drc
;
769 drba
.drba_cred
= CRED();
771 if (DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
) &
772 DMU_BACKUP_FEATURE_RESUMING
) {
773 return (dsl_sync_task(tofs
,
774 dmu_recv_resume_begin_check
, dmu_recv_resume_begin_sync
,
775 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
));
780 * For non-raw, non-incremental, non-resuming receives the
781 * user can specify encryption parameters on the command line
782 * with "zfs recv -o". For these receives we create a dcp and
783 * pass it to the sync task. Creating the dcp will implicitly
784 * remove the encryption params from the localprops nvlist,
785 * which avoids errors when trying to set these normally
786 * read-only properties. Any other kind of receive that
787 * attempts to set these properties will fail as a result.
789 if ((DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
) &
790 DMU_BACKUP_FEATURE_RAW
) == 0 &&
791 origin
== NULL
&& drc
->drc_drrb
->drr_fromguid
== 0) {
792 err
= dsl_crypto_params_create_nvlist(DCP_CMD_NONE
,
793 localprops
, hidden_args
, &drba
.drba_dcp
);
798 err
= dsl_sync_task(tofs
,
799 dmu_recv_begin_check
, dmu_recv_begin_sync
,
800 &drba
, 5, ZFS_SPACE_CHECK_NORMAL
);
801 dsl_crypto_params_free(drba
.drba_dcp
, !!err
);
807 struct receive_record_arg
{
808 dmu_replay_record_t header
;
809 void *payload
; /* Pointer to a buffer containing the payload */
811 * If the record is a write, pointer to the arc_buf_t containing the
816 uint64_t bytes_read
; /* bytes read from stream when record created */
817 boolean_t eos_marker
; /* Marks the end of the stream */
821 struct receive_writer_arg
{
827 * These three args are used to signal to the main thread that we're
835 /* A map from guid to dataset to help handle dedup'd streams. */
836 avl_tree_t
*guid_to_ds_map
;
839 uint64_t last_object
;
840 uint64_t last_offset
;
841 uint64_t max_object
; /* highest object ID referenced in stream */
842 uint64_t bytes_read
; /* bytes read when current record created */
844 /* Encryption parameters for the last received DRR_OBJECT_RANGE */
845 boolean_t or_crypt_params_present
;
846 uint64_t or_firstobj
;
847 uint64_t or_numslots
;
848 uint8_t or_salt
[ZIO_DATA_SALT_LEN
];
849 uint8_t or_iv
[ZIO_DATA_IV_LEN
];
850 uint8_t or_mac
[ZIO_DATA_MAC_LEN
];
851 boolean_t or_byteorder
;
855 list_t list
; /* List of struct receive_objnode. */
857 * Last object looked up. Used to assert that objects are being looked
858 * up in ascending order.
860 uint64_t last_lookup
;
863 struct receive_objnode
{
870 vnode_t
*vp
; /* The vnode to read the stream from */
871 uint64_t voff
; /* The current offset in the stream */
874 * A record that has had its payload read in, but hasn't yet been handed
875 * off to the worker thread.
877 struct receive_record_arg
*rrd
;
878 /* A record that has had its header read in, but not its payload. */
879 struct receive_record_arg
*next_rrd
;
881 zio_cksum_t prev_cksum
;
885 uint64_t featureflags
;
886 /* Sorted list of objects not to issue prefetches for. */
887 struct objlist ignore_objlist
;
890 typedef struct guid_map_entry
{
893 dsl_dataset_t
*gme_ds
;
898 guid_compare(const void *arg1
, const void *arg2
)
900 const guid_map_entry_t
*gmep1
= (const guid_map_entry_t
*)arg1
;
901 const guid_map_entry_t
*gmep2
= (const guid_map_entry_t
*)arg2
;
903 return (AVL_CMP(gmep1
->guid
, gmep2
->guid
));
907 free_guid_map_onexit(void *arg
)
909 avl_tree_t
*ca
= arg
;
911 guid_map_entry_t
*gmep
;
913 while ((gmep
= avl_destroy_nodes(ca
, &cookie
)) != NULL
) {
914 ds_hold_flags_t dsflags
= DS_HOLD_FLAG_DECRYPT
;
917 gmep
->gme_ds
->ds_objset
->os_raw_receive
= B_FALSE
;
918 dsflags
&= ~DS_HOLD_FLAG_DECRYPT
;
921 dsl_dataset_disown(gmep
->gme_ds
, dsflags
, gmep
);
922 kmem_free(gmep
, sizeof (guid_map_entry_t
));
925 kmem_free(ca
, sizeof (avl_tree_t
));
929 receive_read(struct receive_arg
*ra
, int len
, void *buf
)
934 * The code doesn't rely on this (lengths being multiples of 8). See
935 * comment in dump_bytes.
937 ASSERT(len
% 8 == 0 ||
938 (ra
->featureflags
& DMU_BACKUP_FEATURE_RAW
) != 0);
943 ra
->err
= vn_rdwr(UIO_READ
, ra
->vp
,
944 (char *)buf
+ done
, len
- done
,
945 ra
->voff
, UIO_SYSSPACE
, FAPPEND
,
946 RLIM64_INFINITY
, CRED(), &resid
);
948 if (resid
== len
- done
) {
950 * Note: ECKSUM indicates that the receive
951 * was interrupted and can potentially be resumed.
953 ra
->err
= SET_ERROR(ECKSUM
);
955 ra
->voff
+= len
- done
- resid
;
961 ra
->bytes_read
+= len
;
963 ASSERT3U(done
, ==, len
);
968 byteswap_record(dmu_replay_record_t
*drr
)
970 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
971 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
972 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
973 drr
->drr_payloadlen
= BSWAP_32(drr
->drr_payloadlen
);
975 switch (drr
->drr_type
) {
977 DO64(drr_begin
.drr_magic
);
978 DO64(drr_begin
.drr_versioninfo
);
979 DO64(drr_begin
.drr_creation_time
);
980 DO32(drr_begin
.drr_type
);
981 DO32(drr_begin
.drr_flags
);
982 DO64(drr_begin
.drr_toguid
);
983 DO64(drr_begin
.drr_fromguid
);
986 DO64(drr_object
.drr_object
);
987 DO32(drr_object
.drr_type
);
988 DO32(drr_object
.drr_bonustype
);
989 DO32(drr_object
.drr_blksz
);
990 DO32(drr_object
.drr_bonuslen
);
991 DO32(drr_object
.drr_raw_bonuslen
);
992 DO64(drr_object
.drr_toguid
);
993 DO64(drr_object
.drr_maxblkid
);
995 case DRR_FREEOBJECTS
:
996 DO64(drr_freeobjects
.drr_firstobj
);
997 DO64(drr_freeobjects
.drr_numobjs
);
998 DO64(drr_freeobjects
.drr_toguid
);
1001 DO64(drr_write
.drr_object
);
1002 DO32(drr_write
.drr_type
);
1003 DO64(drr_write
.drr_offset
);
1004 DO64(drr_write
.drr_logical_size
);
1005 DO64(drr_write
.drr_toguid
);
1006 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write
.drr_key
.ddk_cksum
);
1007 DO64(drr_write
.drr_key
.ddk_prop
);
1008 DO64(drr_write
.drr_compressed_size
);
1010 case DRR_WRITE_BYREF
:
1011 DO64(drr_write_byref
.drr_object
);
1012 DO64(drr_write_byref
.drr_offset
);
1013 DO64(drr_write_byref
.drr_length
);
1014 DO64(drr_write_byref
.drr_toguid
);
1015 DO64(drr_write_byref
.drr_refguid
);
1016 DO64(drr_write_byref
.drr_refobject
);
1017 DO64(drr_write_byref
.drr_refoffset
);
1018 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_write_byref
.
1020 DO64(drr_write_byref
.drr_key
.ddk_prop
);
1022 case DRR_WRITE_EMBEDDED
:
1023 DO64(drr_write_embedded
.drr_object
);
1024 DO64(drr_write_embedded
.drr_offset
);
1025 DO64(drr_write_embedded
.drr_length
);
1026 DO64(drr_write_embedded
.drr_toguid
);
1027 DO32(drr_write_embedded
.drr_lsize
);
1028 DO32(drr_write_embedded
.drr_psize
);
1031 DO64(drr_free
.drr_object
);
1032 DO64(drr_free
.drr_offset
);
1033 DO64(drr_free
.drr_length
);
1034 DO64(drr_free
.drr_toguid
);
1037 DO64(drr_spill
.drr_object
);
1038 DO64(drr_spill
.drr_length
);
1039 DO64(drr_spill
.drr_toguid
);
1040 DO64(drr_spill
.drr_compressed_size
);
1041 DO32(drr_spill
.drr_type
);
1043 case DRR_OBJECT_RANGE
:
1044 DO64(drr_object_range
.drr_firstobj
);
1045 DO64(drr_object_range
.drr_numslots
);
1046 DO64(drr_object_range
.drr_toguid
);
1049 DO64(drr_end
.drr_toguid
);
1050 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_end
.drr_checksum
);
1056 if (drr
->drr_type
!= DRR_BEGIN
) {
1057 ZIO_CHECKSUM_BSWAP(&drr
->drr_u
.drr_checksum
.drr_checksum
);
1064 static inline uint8_t
1065 deduce_nblkptr(dmu_object_type_t bonus_type
, uint64_t bonus_size
)
1067 if (bonus_type
== DMU_OT_SA
) {
1071 ((DN_OLD_MAX_BONUSLEN
-
1072 MIN(DN_OLD_MAX_BONUSLEN
, bonus_size
)) >> SPA_BLKPTRSHIFT
));
1077 save_resume_state(struct receive_writer_arg
*rwa
,
1078 uint64_t object
, uint64_t offset
, dmu_tx_t
*tx
)
1080 int txgoff
= dmu_tx_get_txg(tx
) & TXG_MASK
;
1082 if (!rwa
->resumable
)
1086 * We use ds_resume_bytes[] != 0 to indicate that we need to
1087 * update this on disk, so it must not be 0.
1089 ASSERT(rwa
->bytes_read
!= 0);
1092 * We only resume from write records, which have a valid
1093 * (non-meta-dnode) object number.
1095 ASSERT(object
!= 0);
1098 * For resuming to work correctly, we must receive records in order,
1099 * sorted by object,offset. This is checked by the callers, but
1100 * assert it here for good measure.
1102 ASSERT3U(object
, >=, rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
]);
1103 ASSERT(object
!= rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] ||
1104 offset
>= rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
]);
1105 ASSERT3U(rwa
->bytes_read
, >=,
1106 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
]);
1108 rwa
->os
->os_dsl_dataset
->ds_resume_object
[txgoff
] = object
;
1109 rwa
->os
->os_dsl_dataset
->ds_resume_offset
[txgoff
] = offset
;
1110 rwa
->os
->os_dsl_dataset
->ds_resume_bytes
[txgoff
] = rwa
->bytes_read
;
1114 receive_object(struct receive_writer_arg
*rwa
, struct drr_object
*drro
,
1117 dmu_object_info_t doi
;
1121 uint8_t dn_slots
= drro
->drr_dn_slots
!= 0 ?
1122 drro
->drr_dn_slots
: DNODE_MIN_SLOTS
;
1124 if (drro
->drr_type
== DMU_OT_NONE
||
1125 !DMU_OT_IS_VALID(drro
->drr_type
) ||
1126 !DMU_OT_IS_VALID(drro
->drr_bonustype
) ||
1127 drro
->drr_checksumtype
>= ZIO_CHECKSUM_FUNCTIONS
||
1128 drro
->drr_compress
>= ZIO_COMPRESS_FUNCTIONS
||
1129 P2PHASE(drro
->drr_blksz
, SPA_MINBLOCKSIZE
) ||
1130 drro
->drr_blksz
< SPA_MINBLOCKSIZE
||
1131 drro
->drr_blksz
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)) ||
1132 drro
->drr_bonuslen
>
1133 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa
->os
))) ||
1135 (spa_maxdnodesize(dmu_objset_spa(rwa
->os
)) >> DNODE_SHIFT
)) {
1136 return (SET_ERROR(EINVAL
));
1141 * We should have received a DRR_OBJECT_RANGE record
1142 * containing this block and stored it in rwa.
1144 if (drro
->drr_object
< rwa
->or_firstobj
||
1145 drro
->drr_object
>= rwa
->or_firstobj
+ rwa
->or_numslots
||
1146 drro
->drr_raw_bonuslen
< drro
->drr_bonuslen
||
1147 drro
->drr_indblkshift
> SPA_MAXBLOCKSHIFT
||
1148 drro
->drr_nlevels
> DN_MAX_LEVELS
||
1149 drro
->drr_nblkptr
> DN_MAX_NBLKPTR
||
1150 DN_SLOTS_TO_BONUSLEN(dn_slots
) <
1151 drro
->drr_raw_bonuslen
)
1152 return (SET_ERROR(EINVAL
));
1154 if (drro
->drr_flags
!= 0 || drro
->drr_raw_bonuslen
!= 0 ||
1155 drro
->drr_indblkshift
!= 0 || drro
->drr_nlevels
!= 0 ||
1156 drro
->drr_nblkptr
!= 0)
1157 return (SET_ERROR(EINVAL
));
1160 err
= dmu_object_info(rwa
->os
, drro
->drr_object
, &doi
);
1161 if (err
!= 0 && err
!= ENOENT
&& err
!= EEXIST
)
1162 return (SET_ERROR(EINVAL
));
1164 if (drro
->drr_object
> rwa
->max_object
)
1165 rwa
->max_object
= drro
->drr_object
;
1168 * If we are losing blkptrs or changing the block size this must
1169 * be a new file instance. We must clear out the previous file
1170 * contents before we can change this type of metadata in the dnode.
1171 * Raw receives will also check that the indirect structure of the
1172 * dnode hasn't changed.
1175 uint32_t indblksz
= drro
->drr_indblkshift
?
1176 1ULL << drro
->drr_indblkshift
: 0;
1177 int nblkptr
= deduce_nblkptr(drro
->drr_bonustype
,
1178 drro
->drr_bonuslen
);
1180 object
= drro
->drr_object
;
1182 /* nblkptr will be bounded by the bonus size and type */
1183 if (rwa
->raw
&& nblkptr
!= drro
->drr_nblkptr
)
1184 return (SET_ERROR(EINVAL
));
1186 if (drro
->drr_blksz
!= doi
.doi_data_block_size
||
1187 nblkptr
< doi
.doi_nblkptr
||
1188 dn_slots
!= doi
.doi_dnodesize
>> DNODE_SHIFT
||
1190 (indblksz
!= doi
.doi_metadata_block_size
||
1191 drro
->drr_nlevels
< doi
.doi_indirection
))) {
1192 err
= dmu_free_long_range(rwa
->os
,
1193 drro
->drr_object
, 0, DMU_OBJECT_END
);
1195 return (SET_ERROR(EINVAL
));
1199 * The dmu does not currently support decreasing nlevels
1200 * on an object. For non-raw sends, this does not matter
1201 * and the new object can just use the previous one's nlevels.
1202 * For raw sends, however, the structure of the received dnode
1203 * (including nlevels) must match that of the send side.
1204 * Therefore, instead of using dmu_object_reclaim(), we must
1205 * free the object completely and call dmu_object_claim_dnsize()
1208 if ((rwa
->raw
&& drro
->drr_nlevels
< doi
.doi_indirection
) ||
1209 dn_slots
!= doi
.doi_dnodesize
>> DNODE_SHIFT
) {
1210 err
= dmu_free_long_object(rwa
->os
, drro
->drr_object
);
1212 return (SET_ERROR(EINVAL
));
1214 txg_wait_synced(dmu_objset_pool(rwa
->os
), 0);
1215 object
= DMU_NEW_OBJECT
;
1217 } else if (err
== EEXIST
) {
1219 * The object requested is currently an interior slot of a
1220 * multi-slot dnode. This will be resolved when the next txg
1221 * is synced out, since the send stream will have told us
1222 * to free this slot when we freed the associated dnode
1223 * earlier in the stream.
1225 txg_wait_synced(dmu_objset_pool(rwa
->os
), 0);
1226 object
= drro
->drr_object
;
1228 /* object is free and we are about to allocate a new one */
1229 object
= DMU_NEW_OBJECT
;
1233 * If this is a multi-slot dnode there is a chance that this
1234 * object will expand into a slot that is already used by
1235 * another object from the previous snapshot. We must free
1236 * these objects before we attempt to allocate the new dnode.
1239 boolean_t need_sync
= B_FALSE
;
1241 for (uint64_t slot
= drro
->drr_object
+ 1;
1242 slot
< drro
->drr_object
+ dn_slots
;
1244 dmu_object_info_t slot_doi
;
1246 err
= dmu_object_info(rwa
->os
, slot
, &slot_doi
);
1247 if (err
== ENOENT
|| err
== EEXIST
)
1252 err
= dmu_free_long_object(rwa
->os
, slot
);
1261 txg_wait_synced(dmu_objset_pool(rwa
->os
), 0);
1264 tx
= dmu_tx_create(rwa
->os
);
1265 dmu_tx_hold_bonus(tx
, object
);
1266 dmu_tx_hold_write(tx
, object
, 0, 0);
1267 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1273 if (object
== DMU_NEW_OBJECT
) {
1274 /* currently free, want to be allocated */
1275 err
= dmu_object_claim_dnsize(rwa
->os
, drro
->drr_object
,
1276 drro
->drr_type
, drro
->drr_blksz
,
1277 drro
->drr_bonustype
, drro
->drr_bonuslen
,
1278 dn_slots
<< DNODE_SHIFT
, tx
);
1279 } else if (drro
->drr_type
!= doi
.doi_type
||
1280 drro
->drr_blksz
!= doi
.doi_data_block_size
||
1281 drro
->drr_bonustype
!= doi
.doi_bonus_type
||
1282 drro
->drr_bonuslen
!= doi
.doi_bonus_size
) {
1283 /* currently allocated, but with different properties */
1284 err
= dmu_object_reclaim_dnsize(rwa
->os
, drro
->drr_object
,
1285 drro
->drr_type
, drro
->drr_blksz
,
1286 drro
->drr_bonustype
, drro
->drr_bonuslen
,
1287 dn_slots
<< DNODE_SHIFT
, tx
);
1291 return (SET_ERROR(EINVAL
));
1294 if (rwa
->or_crypt_params_present
) {
1296 * Set the crypt params for the buffer associated with this
1297 * range of dnodes. This causes the blkptr_t to have the
1298 * same crypt params (byteorder, salt, iv, mac) as on the
1301 * Since we are committing this tx now, it is possible for
1302 * the dnode block to end up on-disk with the incorrect MAC,
1303 * if subsequent objects in this block are received in a
1304 * different txg. However, since the dataset is marked as
1305 * inconsistent, no code paths will do a non-raw read (or
1306 * decrypt the block / verify the MAC). The receive code and
1307 * scrub code can safely do raw reads and verify the
1308 * checksum. They don't need to verify the MAC.
1310 dmu_buf_t
*db
= NULL
;
1311 uint64_t offset
= rwa
->or_firstobj
* DNODE_MIN_SIZE
;
1313 err
= dmu_buf_hold_by_dnode(DMU_META_DNODE(rwa
->os
),
1314 offset
, FTAG
, &db
, DMU_READ_PREFETCH
| DMU_READ_NO_DECRYPT
);
1317 return (SET_ERROR(EINVAL
));
1320 dmu_buf_set_crypt_params(db
, rwa
->or_byteorder
,
1321 rwa
->or_salt
, rwa
->or_iv
, rwa
->or_mac
, tx
);
1323 dmu_buf_rele(db
, FTAG
);
1325 rwa
->or_crypt_params_present
= B_FALSE
;
1328 dmu_object_set_checksum(rwa
->os
, drro
->drr_object
,
1329 drro
->drr_checksumtype
, tx
);
1330 dmu_object_set_compress(rwa
->os
, drro
->drr_object
,
1331 drro
->drr_compress
, tx
);
1333 /* handle more restrictive dnode structuring for raw recvs */
1336 * Set the indirect block shift and nlevels. This will not fail
1337 * because we ensured all of the blocks were free earlier if
1338 * this is a new object.
1340 VERIFY0(dmu_object_set_blocksize(rwa
->os
, drro
->drr_object
,
1341 drro
->drr_blksz
, drro
->drr_indblkshift
, tx
));
1342 VERIFY0(dmu_object_set_nlevels(rwa
->os
, drro
->drr_object
,
1343 drro
->drr_nlevels
, tx
));
1344 VERIFY0(dmu_object_set_maxblkid(rwa
->os
, drro
->drr_object
,
1345 drro
->drr_maxblkid
, tx
));
1351 uint32_t flags
= DMU_READ_NO_PREFETCH
;
1354 flags
|= DMU_READ_NO_DECRYPT
;
1356 VERIFY0(dnode_hold(rwa
->os
, drro
->drr_object
, FTAG
, &dn
));
1357 VERIFY0(dmu_bonus_hold_by_dnode(dn
, FTAG
, &db
, flags
));
1359 dmu_buf_will_dirty(db
, tx
);
1361 ASSERT3U(db
->db_size
, >=, drro
->drr_bonuslen
);
1362 bcopy(data
, db
->db_data
, DRR_OBJECT_PAYLOAD_SIZE(drro
));
1365 * Raw bonus buffers have their byteorder determined by the
1366 * DRR_OBJECT_RANGE record.
1368 if (rwa
->byteswap
&& !rwa
->raw
) {
1369 dmu_object_byteswap_t byteswap
=
1370 DMU_OT_BYTESWAP(drro
->drr_bonustype
);
1371 dmu_ot_byteswap
[byteswap
].ob_func(db
->db_data
,
1372 DRR_OBJECT_PAYLOAD_SIZE(drro
));
1374 dmu_buf_rele(db
, FTAG
);
1375 dnode_rele(dn
, FTAG
);
1384 receive_freeobjects(struct receive_writer_arg
*rwa
,
1385 struct drr_freeobjects
*drrfo
)
1390 if (drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
< drrfo
->drr_firstobj
)
1391 return (SET_ERROR(EINVAL
));
1393 for (obj
= drrfo
->drr_firstobj
== 0 ? 1 : drrfo
->drr_firstobj
;
1394 obj
< drrfo
->drr_firstobj
+ drrfo
->drr_numobjs
&& next_err
== 0;
1395 next_err
= dmu_object_next(rwa
->os
, &obj
, FALSE
, 0)) {
1396 dmu_object_info_t doi
;
1399 err
= dmu_object_info(rwa
->os
, obj
, &doi
);
1405 err
= dmu_free_long_object(rwa
->os
, obj
);
1410 if (obj
> rwa
->max_object
)
1411 rwa
->max_object
= obj
;
1413 if (next_err
!= ESRCH
)
1419 receive_write(struct receive_writer_arg
*rwa
, struct drr_write
*drrw
,
1426 if (drrw
->drr_offset
+ drrw
->drr_logical_size
< drrw
->drr_offset
||
1427 !DMU_OT_IS_VALID(drrw
->drr_type
))
1428 return (SET_ERROR(EINVAL
));
1431 * For resuming to work, records must be in increasing order
1432 * by (object, offset).
1434 if (drrw
->drr_object
< rwa
->last_object
||
1435 (drrw
->drr_object
== rwa
->last_object
&&
1436 drrw
->drr_offset
< rwa
->last_offset
)) {
1437 return (SET_ERROR(EINVAL
));
1439 rwa
->last_object
= drrw
->drr_object
;
1440 rwa
->last_offset
= drrw
->drr_offset
;
1442 if (rwa
->last_object
> rwa
->max_object
)
1443 rwa
->max_object
= rwa
->last_object
;
1445 if (dmu_object_info(rwa
->os
, drrw
->drr_object
, NULL
) != 0)
1446 return (SET_ERROR(EINVAL
));
1448 tx
= dmu_tx_create(rwa
->os
);
1449 dmu_tx_hold_write(tx
, drrw
->drr_object
,
1450 drrw
->drr_offset
, drrw
->drr_logical_size
);
1451 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1457 if (rwa
->byteswap
&& !arc_is_encrypted(abuf
) &&
1458 arc_get_compression(abuf
) == ZIO_COMPRESS_OFF
) {
1459 dmu_object_byteswap_t byteswap
=
1460 DMU_OT_BYTESWAP(drrw
->drr_type
);
1461 dmu_ot_byteswap
[byteswap
].ob_func(abuf
->b_data
,
1462 DRR_WRITE_PAYLOAD_SIZE(drrw
));
1465 VERIFY0(dnode_hold(rwa
->os
, drrw
->drr_object
, FTAG
, &dn
));
1466 err
= dmu_assign_arcbuf_by_dnode(dn
, drrw
->drr_offset
, abuf
, tx
);
1468 dnode_rele(dn
, FTAG
);
1472 dnode_rele(dn
, FTAG
);
1475 * Note: If the receive fails, we want the resume stream to start
1476 * with the same record that we last successfully received (as opposed
1477 * to the next record), so that we can verify that we are
1478 * resuming from the correct location.
1480 save_resume_state(rwa
, drrw
->drr_object
, drrw
->drr_offset
, tx
);
1487 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1488 * streams to refer to a copy of the data that is already on the
1489 * system because it came in earlier in the stream. This function
1490 * finds the earlier copy of the data, and uses that copy instead of
1491 * data from the stream to fulfill this write.
1494 receive_write_byref(struct receive_writer_arg
*rwa
,
1495 struct drr_write_byref
*drrwbr
)
1499 guid_map_entry_t gmesrch
;
1500 guid_map_entry_t
*gmep
;
1502 objset_t
*ref_os
= NULL
;
1503 int flags
= DMU_READ_PREFETCH
;
1506 if (drrwbr
->drr_offset
+ drrwbr
->drr_length
< drrwbr
->drr_offset
)
1507 return (SET_ERROR(EINVAL
));
1510 * If the GUID of the referenced dataset is different from the
1511 * GUID of the target dataset, find the referenced dataset.
1513 if (drrwbr
->drr_toguid
!= drrwbr
->drr_refguid
) {
1514 gmesrch
.guid
= drrwbr
->drr_refguid
;
1515 if ((gmep
= avl_find(rwa
->guid_to_ds_map
, &gmesrch
,
1517 return (SET_ERROR(EINVAL
));
1519 if (dmu_objset_from_ds(gmep
->gme_ds
, &ref_os
))
1520 return (SET_ERROR(EINVAL
));
1525 if (drrwbr
->drr_object
> rwa
->max_object
)
1526 rwa
->max_object
= drrwbr
->drr_object
;
1529 flags
|= DMU_READ_NO_DECRYPT
;
1531 /* may return either a regular db or an encrypted one */
1532 err
= dmu_buf_hold(ref_os
, drrwbr
->drr_refobject
,
1533 drrwbr
->drr_refoffset
, FTAG
, &dbp
, flags
);
1537 tx
= dmu_tx_create(rwa
->os
);
1539 dmu_tx_hold_write(tx
, drrwbr
->drr_object
,
1540 drrwbr
->drr_offset
, drrwbr
->drr_length
);
1541 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1548 dmu_copy_from_buf(rwa
->os
, drrwbr
->drr_object
,
1549 drrwbr
->drr_offset
, dbp
, tx
);
1551 dmu_write(rwa
->os
, drrwbr
->drr_object
,
1552 drrwbr
->drr_offset
, drrwbr
->drr_length
, dbp
->db_data
, tx
);
1554 dmu_buf_rele(dbp
, FTAG
);
1556 /* See comment in restore_write. */
1557 save_resume_state(rwa
, drrwbr
->drr_object
, drrwbr
->drr_offset
, tx
);
1563 receive_write_embedded(struct receive_writer_arg
*rwa
,
1564 struct drr_write_embedded
*drrwe
, void *data
)
1569 if (drrwe
->drr_offset
+ drrwe
->drr_length
< drrwe
->drr_offset
)
1570 return (SET_ERROR(EINVAL
));
1572 if (drrwe
->drr_psize
> BPE_PAYLOAD_SIZE
)
1573 return (SET_ERROR(EINVAL
));
1575 if (drrwe
->drr_etype
>= NUM_BP_EMBEDDED_TYPES
)
1576 return (SET_ERROR(EINVAL
));
1577 if (drrwe
->drr_compression
>= ZIO_COMPRESS_FUNCTIONS
)
1578 return (SET_ERROR(EINVAL
));
1580 return (SET_ERROR(EINVAL
));
1582 if (drrwe
->drr_object
> rwa
->max_object
)
1583 rwa
->max_object
= drrwe
->drr_object
;
1585 tx
= dmu_tx_create(rwa
->os
);
1587 dmu_tx_hold_write(tx
, drrwe
->drr_object
,
1588 drrwe
->drr_offset
, drrwe
->drr_length
);
1589 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1595 dmu_write_embedded(rwa
->os
, drrwe
->drr_object
,
1596 drrwe
->drr_offset
, data
, drrwe
->drr_etype
,
1597 drrwe
->drr_compression
, drrwe
->drr_lsize
, drrwe
->drr_psize
,
1598 rwa
->byteswap
^ ZFS_HOST_BYTEORDER
, tx
);
1600 /* See comment in restore_write. */
1601 save_resume_state(rwa
, drrwe
->drr_object
, drrwe
->drr_offset
, tx
);
1607 receive_spill(struct receive_writer_arg
*rwa
, struct drr_spill
*drrs
,
1611 dmu_buf_t
*db
, *db_spill
;
1615 if (drrs
->drr_length
< SPA_MINBLOCKSIZE
||
1616 drrs
->drr_length
> spa_maxblocksize(dmu_objset_spa(rwa
->os
)))
1617 return (SET_ERROR(EINVAL
));
1620 if (!DMU_OT_IS_VALID(drrs
->drr_type
) ||
1621 drrs
->drr_compressiontype
>= ZIO_COMPRESS_FUNCTIONS
||
1622 drrs
->drr_compressed_size
== 0)
1623 return (SET_ERROR(EINVAL
));
1625 flags
|= DMU_READ_NO_DECRYPT
;
1628 if (dmu_object_info(rwa
->os
, drrs
->drr_object
, NULL
) != 0)
1629 return (SET_ERROR(EINVAL
));
1631 if (drrs
->drr_object
> rwa
->max_object
)
1632 rwa
->max_object
= drrs
->drr_object
;
1634 VERIFY0(dmu_bonus_hold(rwa
->os
, drrs
->drr_object
, FTAG
, &db
));
1635 if ((err
= dmu_spill_hold_by_bonus(db
, DMU_READ_NO_DECRYPT
, FTAG
,
1637 dmu_buf_rele(db
, FTAG
);
1641 tx
= dmu_tx_create(rwa
->os
);
1643 dmu_tx_hold_spill(tx
, db
->db_object
);
1645 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1647 dmu_buf_rele(db
, FTAG
);
1648 dmu_buf_rele(db_spill
, FTAG
);
1653 if (db_spill
->db_size
< drrs
->drr_length
)
1654 VERIFY(0 == dbuf_spill_set_blksz(db_spill
,
1655 drrs
->drr_length
, tx
));
1657 if (rwa
->byteswap
&& !arc_is_encrypted(abuf
) &&
1658 arc_get_compression(abuf
) == ZIO_COMPRESS_OFF
) {
1659 dmu_object_byteswap_t byteswap
=
1660 DMU_OT_BYTESWAP(drrs
->drr_type
);
1661 dmu_ot_byteswap
[byteswap
].ob_func(abuf
->b_data
,
1662 DRR_SPILL_PAYLOAD_SIZE(drrs
));
1665 dbuf_assign_arcbuf((dmu_buf_impl_t
*)db_spill
, abuf
, tx
);
1667 dmu_buf_rele(db
, FTAG
);
1668 dmu_buf_rele(db_spill
, FTAG
);
1676 receive_free(struct receive_writer_arg
*rwa
, struct drr_free
*drrf
)
1680 if (drrf
->drr_length
!= DMU_OBJECT_END
&&
1681 drrf
->drr_offset
+ drrf
->drr_length
< drrf
->drr_offset
)
1682 return (SET_ERROR(EINVAL
));
1684 if (dmu_object_info(rwa
->os
, drrf
->drr_object
, NULL
) != 0)
1685 return (SET_ERROR(EINVAL
));
1687 if (drrf
->drr_object
> rwa
->max_object
)
1688 rwa
->max_object
= drrf
->drr_object
;
1690 err
= dmu_free_long_range(rwa
->os
, drrf
->drr_object
,
1691 drrf
->drr_offset
, drrf
->drr_length
);
1697 receive_object_range(struct receive_writer_arg
*rwa
,
1698 struct drr_object_range
*drror
)
1701 * By default, we assume this block is in our native format
1702 * (ZFS_HOST_BYTEORDER). We then take into account whether
1703 * the send stream is byteswapped (rwa->byteswap). Finally,
1704 * we need to byteswap again if this particular block was
1705 * in non-native format on the send side.
1707 boolean_t byteorder
= ZFS_HOST_BYTEORDER
^ rwa
->byteswap
^
1708 !!DRR_IS_RAW_BYTESWAPPED(drror
->drr_flags
);
1711 * Since dnode block sizes are constant, we should not need to worry
1712 * about making sure that the dnode block size is the same on the
1713 * sending and receiving sides for the time being. For non-raw sends,
1714 * this does not matter (and in fact we do not send a DRR_OBJECT_RANGE
1715 * record at all). Raw sends require this record type because the
1716 * encryption parameters are used to protect an entire block of bonus
1717 * buffers. If the size of dnode blocks ever becomes variable,
1718 * handling will need to be added to ensure that dnode block sizes
1719 * match on the sending and receiving side.
1721 if (drror
->drr_numslots
!= DNODES_PER_BLOCK
||
1722 P2PHASE(drror
->drr_firstobj
, DNODES_PER_BLOCK
) != 0 ||
1724 return (SET_ERROR(EINVAL
));
1726 if (drror
->drr_firstobj
> rwa
->max_object
)
1727 rwa
->max_object
= drror
->drr_firstobj
;
1730 * The DRR_OBJECT_RANGE handling must be deferred to receive_object()
1731 * so that the block of dnodes is not written out when it's empty,
1732 * and converted to a HOLE BP.
1734 rwa
->or_crypt_params_present
= B_TRUE
;
1735 rwa
->or_firstobj
= drror
->drr_firstobj
;
1736 rwa
->or_numslots
= drror
->drr_numslots
;
1737 bcopy(drror
->drr_salt
, rwa
->or_salt
, ZIO_DATA_SALT_LEN
);
1738 bcopy(drror
->drr_iv
, rwa
->or_iv
, ZIO_DATA_IV_LEN
);
1739 bcopy(drror
->drr_mac
, rwa
->or_mac
, ZIO_DATA_MAC_LEN
);
1740 rwa
->or_byteorder
= byteorder
;
1745 /* used to destroy the drc_ds on error */
1747 dmu_recv_cleanup_ds(dmu_recv_cookie_t
*drc
)
1749 dsl_dataset_t
*ds
= drc
->drc_ds
;
1750 ds_hold_flags_t dsflags
= (drc
->drc_raw
) ? 0 : DS_HOLD_FLAG_DECRYPT
;
1753 * Wait for the txg sync before cleaning up the receive. For
1754 * resumable receives, this ensures that our resume state has
1755 * been written out to disk. For raw receives, this ensures
1756 * that the user accounting code will not attempt to do anything
1757 * after we stopped receiving the dataset.
1759 txg_wait_synced(ds
->ds_dir
->dd_pool
, 0);
1760 ds
->ds_objset
->os_raw_receive
= B_FALSE
;
1762 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
1763 if (drc
->drc_resumable
&& !BP_IS_HOLE(dsl_dataset_get_blkptr(ds
))) {
1764 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1765 dsl_dataset_disown(ds
, dsflags
, dmu_recv_tag
);
1767 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1768 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
1769 dsl_dataset_name(ds
, name
);
1770 dsl_dataset_disown(ds
, dsflags
, dmu_recv_tag
);
1771 (void) dsl_destroy_head(name
);
1776 receive_cksum(struct receive_arg
*ra
, int len
, void *buf
)
1779 (void) fletcher_4_incremental_byteswap(buf
, len
, &ra
->cksum
);
1781 (void) fletcher_4_incremental_native(buf
, len
, &ra
->cksum
);
1786 * Read the payload into a buffer of size len, and update the current record's
1788 * Allocate ra->next_rrd and read the next record's header into
1789 * ra->next_rrd->header.
1790 * Verify checksum of payload and next record.
1793 receive_read_payload_and_next_header(struct receive_arg
*ra
, int len
, void *buf
)
1796 zio_cksum_t cksum_orig
;
1797 zio_cksum_t
*cksump
;
1800 ASSERT3U(len
, <=, SPA_MAXBLOCKSIZE
);
1801 err
= receive_read(ra
, len
, buf
);
1804 receive_cksum(ra
, len
, buf
);
1806 /* note: rrd is NULL when reading the begin record's payload */
1807 if (ra
->rrd
!= NULL
) {
1808 ra
->rrd
->payload
= buf
;
1809 ra
->rrd
->payload_size
= len
;
1810 ra
->rrd
->bytes_read
= ra
->bytes_read
;
1813 ASSERT3P(buf
, ==, NULL
);
1816 ra
->prev_cksum
= ra
->cksum
;
1818 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
1819 err
= receive_read(ra
, sizeof (ra
->next_rrd
->header
),
1820 &ra
->next_rrd
->header
);
1821 ra
->next_rrd
->bytes_read
= ra
->bytes_read
;
1824 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
1825 ra
->next_rrd
= NULL
;
1828 if (ra
->next_rrd
->header
.drr_type
== DRR_BEGIN
) {
1829 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
1830 ra
->next_rrd
= NULL
;
1831 return (SET_ERROR(EINVAL
));
1835 * Note: checksum is of everything up to but not including the
1838 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
1839 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
1841 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
1842 &ra
->next_rrd
->header
);
1844 cksum_orig
= ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
1845 cksump
= &ra
->next_rrd
->header
.drr_u
.drr_checksum
.drr_checksum
;
1848 byteswap_record(&ra
->next_rrd
->header
);
1850 if ((!ZIO_CHECKSUM_IS_ZERO(cksump
)) &&
1851 !ZIO_CHECKSUM_EQUAL(ra
->cksum
, *cksump
)) {
1852 kmem_free(ra
->next_rrd
, sizeof (*ra
->next_rrd
));
1853 ra
->next_rrd
= NULL
;
1854 return (SET_ERROR(ECKSUM
));
1857 receive_cksum(ra
, sizeof (cksum_orig
), &cksum_orig
);
1863 objlist_create(struct objlist
*list
)
1865 list_create(&list
->list
, sizeof (struct receive_objnode
),
1866 offsetof(struct receive_objnode
, node
));
1867 list
->last_lookup
= 0;
1871 objlist_destroy(struct objlist
*list
)
1873 for (struct receive_objnode
*n
= list_remove_head(&list
->list
);
1874 n
!= NULL
; n
= list_remove_head(&list
->list
)) {
1875 kmem_free(n
, sizeof (*n
));
1877 list_destroy(&list
->list
);
1881 * This function looks through the objlist to see if the specified object number
1882 * is contained in the objlist. In the process, it will remove all object
1883 * numbers in the list that are smaller than the specified object number. Thus,
1884 * any lookup of an object number smaller than a previously looked up object
1885 * number will always return false; therefore, all lookups should be done in
1889 objlist_exists(struct objlist
*list
, uint64_t object
)
1891 struct receive_objnode
*node
= list_head(&list
->list
);
1892 ASSERT3U(object
, >=, list
->last_lookup
);
1893 list
->last_lookup
= object
;
1894 while (node
!= NULL
&& node
->object
< object
) {
1895 VERIFY3P(node
, ==, list_remove_head(&list
->list
));
1896 kmem_free(node
, sizeof (*node
));
1897 node
= list_head(&list
->list
);
1899 return (node
!= NULL
&& node
->object
== object
);
1903 * The objlist is a list of object numbers stored in ascending order. However,
1904 * the insertion of new object numbers does not seek out the correct location to
1905 * store a new object number; instead, it appends it to the list for simplicity.
1906 * Thus, any users must take care to only insert new object numbers in ascending
1910 objlist_insert(struct objlist
*list
, uint64_t object
)
1912 struct receive_objnode
*node
= kmem_zalloc(sizeof (*node
), KM_SLEEP
);
1913 node
->object
= object
;
1916 struct receive_objnode
*last_object
= list_tail(&list
->list
);
1917 uint64_t last_objnum
= (last_object
!= NULL
? last_object
->object
: 0);
1918 ASSERT3U(node
->object
, >, last_objnum
);
1921 list_insert_tail(&list
->list
, node
);
1925 * Issue the prefetch reads for any necessary indirect blocks.
1927 * We use the object ignore list to tell us whether or not to issue prefetches
1928 * for a given object. We do this for both correctness (in case the blocksize
1929 * of an object has changed) and performance (if the object doesn't exist, don't
1930 * needlessly try to issue prefetches). We also trim the list as we go through
1931 * the stream to prevent it from growing to an unbounded size.
1933 * The object numbers within will always be in sorted order, and any write
1934 * records we see will also be in sorted order, but they're not sorted with
1935 * respect to each other (i.e. we can get several object records before
1936 * receiving each object's write records). As a result, once we've reached a
1937 * given object number, we can safely remove any reference to lower object
1938 * numbers in the ignore list. In practice, we receive up to 32 object records
1939 * before receiving write records, so the list can have up to 32 nodes in it.
1943 receive_read_prefetch(struct receive_arg
*ra
,
1944 uint64_t object
, uint64_t offset
, uint64_t length
)
1946 if (!objlist_exists(&ra
->ignore_objlist
, object
)) {
1947 dmu_prefetch(ra
->os
, object
, 1, offset
, length
,
1948 ZIO_PRIORITY_SYNC_READ
);
1953 * Read records off the stream, issuing any necessary prefetches.
1956 receive_read_record(struct receive_arg
*ra
)
1960 switch (ra
->rrd
->header
.drr_type
) {
1963 struct drr_object
*drro
= &ra
->rrd
->header
.drr_u
.drr_object
;
1964 uint32_t size
= DRR_OBJECT_PAYLOAD_SIZE(drro
);
1966 dmu_object_info_t doi
;
1969 buf
= kmem_zalloc(size
, KM_SLEEP
);
1971 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
1973 kmem_free(buf
, size
);
1976 err
= dmu_object_info(ra
->os
, drro
->drr_object
, &doi
);
1978 * See receive_read_prefetch for an explanation why we're
1979 * storing this object in the ignore_obj_list.
1981 if (err
== ENOENT
|| err
== EEXIST
||
1982 (err
== 0 && doi
.doi_data_block_size
!= drro
->drr_blksz
)) {
1983 objlist_insert(&ra
->ignore_objlist
, drro
->drr_object
);
1988 case DRR_FREEOBJECTS
:
1990 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
1995 struct drr_write
*drrw
= &ra
->rrd
->header
.drr_u
.drr_write
;
1997 boolean_t is_meta
= DMU_OT_IS_METADATA(drrw
->drr_type
);
2000 boolean_t byteorder
= ZFS_HOST_BYTEORDER
^
2001 !!DRR_IS_RAW_BYTESWAPPED(drrw
->drr_flags
) ^
2004 abuf
= arc_loan_raw_buf(dmu_objset_spa(ra
->os
),
2005 drrw
->drr_object
, byteorder
, drrw
->drr_salt
,
2006 drrw
->drr_iv
, drrw
->drr_mac
, drrw
->drr_type
,
2007 drrw
->drr_compressed_size
, drrw
->drr_logical_size
,
2008 drrw
->drr_compressiontype
);
2009 } else if (DRR_WRITE_COMPRESSED(drrw
)) {
2010 ASSERT3U(drrw
->drr_compressed_size
, >, 0);
2011 ASSERT3U(drrw
->drr_logical_size
, >=,
2012 drrw
->drr_compressed_size
);
2014 abuf
= arc_loan_compressed_buf(
2015 dmu_objset_spa(ra
->os
),
2016 drrw
->drr_compressed_size
, drrw
->drr_logical_size
,
2017 drrw
->drr_compressiontype
);
2019 abuf
= arc_loan_buf(dmu_objset_spa(ra
->os
),
2020 is_meta
, drrw
->drr_logical_size
);
2023 err
= receive_read_payload_and_next_header(ra
,
2024 DRR_WRITE_PAYLOAD_SIZE(drrw
), abuf
->b_data
);
2026 dmu_return_arcbuf(abuf
);
2029 ra
->rrd
->arc_buf
= abuf
;
2030 receive_read_prefetch(ra
, drrw
->drr_object
, drrw
->drr_offset
,
2031 drrw
->drr_logical_size
);
2034 case DRR_WRITE_BYREF
:
2036 struct drr_write_byref
*drrwb
=
2037 &ra
->rrd
->header
.drr_u
.drr_write_byref
;
2038 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2039 receive_read_prefetch(ra
, drrwb
->drr_object
, drrwb
->drr_offset
,
2043 case DRR_WRITE_EMBEDDED
:
2045 struct drr_write_embedded
*drrwe
=
2046 &ra
->rrd
->header
.drr_u
.drr_write_embedded
;
2047 uint32_t size
= P2ROUNDUP(drrwe
->drr_psize
, 8);
2048 void *buf
= kmem_zalloc(size
, KM_SLEEP
);
2050 err
= receive_read_payload_and_next_header(ra
, size
, buf
);
2052 kmem_free(buf
, size
);
2056 receive_read_prefetch(ra
, drrwe
->drr_object
, drrwe
->drr_offset
,
2063 * It might be beneficial to prefetch indirect blocks here, but
2064 * we don't really have the data to decide for sure.
2066 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2071 struct drr_end
*drre
= &ra
->rrd
->header
.drr_u
.drr_end
;
2072 if (!ZIO_CHECKSUM_EQUAL(ra
->prev_cksum
, drre
->drr_checksum
))
2073 return (SET_ERROR(ECKSUM
));
2078 struct drr_spill
*drrs
= &ra
->rrd
->header
.drr_u
.drr_spill
;
2080 int len
= DRR_SPILL_PAYLOAD_SIZE(drrs
);
2082 /* DRR_SPILL records are either raw or uncompressed */
2084 boolean_t byteorder
= ZFS_HOST_BYTEORDER
^
2085 !!DRR_IS_RAW_BYTESWAPPED(drrs
->drr_flags
) ^
2088 abuf
= arc_loan_raw_buf(dmu_objset_spa(ra
->os
),
2089 dmu_objset_id(ra
->os
), byteorder
, drrs
->drr_salt
,
2090 drrs
->drr_iv
, drrs
->drr_mac
, drrs
->drr_type
,
2091 drrs
->drr_compressed_size
, drrs
->drr_length
,
2092 drrs
->drr_compressiontype
);
2094 abuf
= arc_loan_buf(dmu_objset_spa(ra
->os
),
2095 DMU_OT_IS_METADATA(drrs
->drr_type
),
2099 err
= receive_read_payload_and_next_header(ra
, len
,
2102 dmu_return_arcbuf(abuf
);
2105 ra
->rrd
->arc_buf
= abuf
;
2108 case DRR_OBJECT_RANGE
:
2110 err
= receive_read_payload_and_next_header(ra
, 0, NULL
);
2114 return (SET_ERROR(EINVAL
));
2119 dprintf_drr(struct receive_record_arg
*rrd
, int err
)
2122 switch (rrd
->header
.drr_type
) {
2125 struct drr_object
*drro
= &rrd
->header
.drr_u
.drr_object
;
2126 dprintf("drr_type = OBJECT obj = %llu type = %u "
2127 "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
2128 "compress = %u dn_slots = %u err = %d\n",
2129 drro
->drr_object
, drro
->drr_type
, drro
->drr_bonustype
,
2130 drro
->drr_blksz
, drro
->drr_bonuslen
,
2131 drro
->drr_checksumtype
, drro
->drr_compress
,
2132 drro
->drr_dn_slots
, err
);
2135 case DRR_FREEOBJECTS
:
2137 struct drr_freeobjects
*drrfo
=
2138 &rrd
->header
.drr_u
.drr_freeobjects
;
2139 dprintf("drr_type = FREEOBJECTS firstobj = %llu "
2140 "numobjs = %llu err = %d\n",
2141 drrfo
->drr_firstobj
, drrfo
->drr_numobjs
, err
);
2146 struct drr_write
*drrw
= &rrd
->header
.drr_u
.drr_write
;
2147 dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
2148 "lsize = %llu cksumtype = %u cksumflags = %u "
2149 "compress = %u psize = %llu err = %d\n",
2150 drrw
->drr_object
, drrw
->drr_type
, drrw
->drr_offset
,
2151 drrw
->drr_logical_size
, drrw
->drr_checksumtype
,
2152 drrw
->drr_flags
, drrw
->drr_compressiontype
,
2153 drrw
->drr_compressed_size
, err
);
2156 case DRR_WRITE_BYREF
:
2158 struct drr_write_byref
*drrwbr
=
2159 &rrd
->header
.drr_u
.drr_write_byref
;
2160 dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
2161 "length = %llu toguid = %llx refguid = %llx "
2162 "refobject = %llu refoffset = %llu cksumtype = %u "
2163 "cksumflags = %u err = %d\n",
2164 drrwbr
->drr_object
, drrwbr
->drr_offset
,
2165 drrwbr
->drr_length
, drrwbr
->drr_toguid
,
2166 drrwbr
->drr_refguid
, drrwbr
->drr_refobject
,
2167 drrwbr
->drr_refoffset
, drrwbr
->drr_checksumtype
,
2168 drrwbr
->drr_flags
, err
);
2171 case DRR_WRITE_EMBEDDED
:
2173 struct drr_write_embedded
*drrwe
=
2174 &rrd
->header
.drr_u
.drr_write_embedded
;
2175 dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
2176 "length = %llu compress = %u etype = %u lsize = %u "
2177 "psize = %u err = %d\n",
2178 drrwe
->drr_object
, drrwe
->drr_offset
, drrwe
->drr_length
,
2179 drrwe
->drr_compression
, drrwe
->drr_etype
,
2180 drrwe
->drr_lsize
, drrwe
->drr_psize
, err
);
2185 struct drr_free
*drrf
= &rrd
->header
.drr_u
.drr_free
;
2186 dprintf("drr_type = FREE obj = %llu offset = %llu "
2187 "length = %lld err = %d\n",
2188 drrf
->drr_object
, drrf
->drr_offset
, drrf
->drr_length
,
2194 struct drr_spill
*drrs
= &rrd
->header
.drr_u
.drr_spill
;
2195 dprintf("drr_type = SPILL obj = %llu length = %llu "
2196 "err = %d\n", drrs
->drr_object
, drrs
->drr_length
, err
);
2206 * Commit the records to the pool.
2209 receive_process_record(struct receive_writer_arg
*rwa
,
2210 struct receive_record_arg
*rrd
)
2214 /* Processing in order, therefore bytes_read should be increasing. */
2215 ASSERT3U(rrd
->bytes_read
, >=, rwa
->bytes_read
);
2216 rwa
->bytes_read
= rrd
->bytes_read
;
2218 switch (rrd
->header
.drr_type
) {
2221 struct drr_object
*drro
= &rrd
->header
.drr_u
.drr_object
;
2222 err
= receive_object(rwa
, drro
, rrd
->payload
);
2223 kmem_free(rrd
->payload
, rrd
->payload_size
);
2224 rrd
->payload
= NULL
;
2227 case DRR_FREEOBJECTS
:
2229 struct drr_freeobjects
*drrfo
=
2230 &rrd
->header
.drr_u
.drr_freeobjects
;
2231 err
= receive_freeobjects(rwa
, drrfo
);
2236 struct drr_write
*drrw
= &rrd
->header
.drr_u
.drr_write
;
2237 err
= receive_write(rwa
, drrw
, rrd
->arc_buf
);
2238 /* if receive_write() is successful, it consumes the arc_buf */
2240 dmu_return_arcbuf(rrd
->arc_buf
);
2241 rrd
->arc_buf
= NULL
;
2242 rrd
->payload
= NULL
;
2245 case DRR_WRITE_BYREF
:
2247 struct drr_write_byref
*drrwbr
=
2248 &rrd
->header
.drr_u
.drr_write_byref
;
2249 err
= receive_write_byref(rwa
, drrwbr
);
2252 case DRR_WRITE_EMBEDDED
:
2254 struct drr_write_embedded
*drrwe
=
2255 &rrd
->header
.drr_u
.drr_write_embedded
;
2256 err
= receive_write_embedded(rwa
, drrwe
, rrd
->payload
);
2257 kmem_free(rrd
->payload
, rrd
->payload_size
);
2258 rrd
->payload
= NULL
;
2263 struct drr_free
*drrf
= &rrd
->header
.drr_u
.drr_free
;
2264 err
= receive_free(rwa
, drrf
);
2269 struct drr_spill
*drrs
= &rrd
->header
.drr_u
.drr_spill
;
2270 err
= receive_spill(rwa
, drrs
, rrd
->arc_buf
);
2271 /* if receive_spill() is successful, it consumes the arc_buf */
2273 dmu_return_arcbuf(rrd
->arc_buf
);
2274 rrd
->arc_buf
= NULL
;
2275 rrd
->payload
= NULL
;
2278 case DRR_OBJECT_RANGE
:
2280 struct drr_object_range
*drror
=
2281 &rrd
->header
.drr_u
.drr_object_range
;
2282 return (receive_object_range(rwa
, drror
));
2285 return (SET_ERROR(EINVAL
));
2289 dprintf_drr(rrd
, err
);
2295 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2296 * receive_process_record When we're done, signal the main thread and exit.
2299 receive_writer_thread(void *arg
)
2301 struct receive_writer_arg
*rwa
= arg
;
2302 struct receive_record_arg
*rrd
;
2303 fstrans_cookie_t cookie
= spl_fstrans_mark();
2305 for (rrd
= bqueue_dequeue(&rwa
->q
); !rrd
->eos_marker
;
2306 rrd
= bqueue_dequeue(&rwa
->q
)) {
2308 * If there's an error, the main thread will stop putting things
2309 * on the queue, but we need to clear everything in it before we
2312 if (rwa
->err
== 0) {
2313 rwa
->err
= receive_process_record(rwa
, rrd
);
2314 } else if (rrd
->arc_buf
!= NULL
) {
2315 dmu_return_arcbuf(rrd
->arc_buf
);
2316 rrd
->arc_buf
= NULL
;
2317 rrd
->payload
= NULL
;
2318 } else if (rrd
->payload
!= NULL
) {
2319 kmem_free(rrd
->payload
, rrd
->payload_size
);
2320 rrd
->payload
= NULL
;
2322 kmem_free(rrd
, sizeof (*rrd
));
2324 kmem_free(rrd
, sizeof (*rrd
));
2325 mutex_enter(&rwa
->mutex
);
2327 cv_signal(&rwa
->cv
);
2328 mutex_exit(&rwa
->mutex
);
2329 spl_fstrans_unmark(cookie
);
2334 resume_check(struct receive_arg
*ra
, nvlist_t
*begin_nvl
)
2337 objset_t
*mos
= dmu_objset_pool(ra
->os
)->dp_meta_objset
;
2338 uint64_t dsobj
= dmu_objset_id(ra
->os
);
2339 uint64_t resume_obj
, resume_off
;
2341 if (nvlist_lookup_uint64(begin_nvl
,
2342 "resume_object", &resume_obj
) != 0 ||
2343 nvlist_lookup_uint64(begin_nvl
,
2344 "resume_offset", &resume_off
) != 0) {
2345 return (SET_ERROR(EINVAL
));
2347 VERIFY0(zap_lookup(mos
, dsobj
,
2348 DS_FIELD_RESUME_OBJECT
, sizeof (val
), 1, &val
));
2349 if (resume_obj
!= val
)
2350 return (SET_ERROR(EINVAL
));
2351 VERIFY0(zap_lookup(mos
, dsobj
,
2352 DS_FIELD_RESUME_OFFSET
, sizeof (val
), 1, &val
));
2353 if (resume_off
!= val
)
2354 return (SET_ERROR(EINVAL
));
2360 * Read in the stream's records, one by one, and apply them to the pool. There
2361 * are two threads involved; the thread that calls this function will spin up a
2362 * worker thread, read the records off the stream one by one, and issue
2363 * prefetches for any necessary indirect blocks. It will then push the records
2364 * onto an internal blocking queue. The worker thread will pull the records off
2365 * the queue, and actually write the data into the DMU. This way, the worker
2366 * thread doesn't have to wait for reads to complete, since everything it needs
2367 * (the indirect blocks) will be prefetched.
2369 * NB: callers *must* call dmu_recv_end() if this succeeds.
2372 dmu_recv_stream(dmu_recv_cookie_t
*drc
, vnode_t
*vp
, offset_t
*voffp
,
2373 int cleanup_fd
, uint64_t *action_handlep
)
2376 struct receive_arg
*ra
;
2377 struct receive_writer_arg
*rwa
;
2379 uint32_t payloadlen
;
2381 nvlist_t
*begin_nvl
= NULL
;
2383 ra
= kmem_zalloc(sizeof (*ra
), KM_SLEEP
);
2384 rwa
= kmem_zalloc(sizeof (*rwa
), KM_SLEEP
);
2386 ra
->byteswap
= drc
->drc_byteswap
;
2387 ra
->raw
= drc
->drc_raw
;
2388 ra
->cksum
= drc
->drc_cksum
;
2392 if (dsl_dataset_is_zapified(drc
->drc_ds
)) {
2393 (void) zap_lookup(drc
->drc_ds
->ds_dir
->dd_pool
->dp_meta_objset
,
2394 drc
->drc_ds
->ds_object
, DS_FIELD_RESUME_BYTES
,
2395 sizeof (ra
->bytes_read
), 1, &ra
->bytes_read
);
2398 objlist_create(&ra
->ignore_objlist
);
2400 /* these were verified in dmu_recv_begin */
2401 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc
->drc_drrb
->drr_versioninfo
), ==,
2403 ASSERT3U(drc
->drc_drrb
->drr_type
, <, DMU_OST_NUMTYPES
);
2406 * Open the objset we are modifying.
2408 VERIFY0(dmu_objset_from_ds(drc
->drc_ds
, &ra
->os
));
2410 ASSERT(dsl_dataset_phys(drc
->drc_ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
2412 featureflags
= DMU_GET_FEATUREFLAGS(drc
->drc_drrb
->drr_versioninfo
);
2413 ra
->featureflags
= featureflags
;
2415 ASSERT0(ra
->os
->os_encrypted
&&
2416 (featureflags
& DMU_BACKUP_FEATURE_EMBED_DATA
));
2418 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2419 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
2422 if (cleanup_fd
== -1) {
2423 err
= SET_ERROR(EBADF
);
2426 err
= zfs_onexit_fd_hold(cleanup_fd
, &minor
);
2432 if (*action_handlep
== 0) {
2433 rwa
->guid_to_ds_map
=
2434 kmem_alloc(sizeof (avl_tree_t
), KM_SLEEP
);
2435 avl_create(rwa
->guid_to_ds_map
, guid_compare
,
2436 sizeof (guid_map_entry_t
),
2437 offsetof(guid_map_entry_t
, avlnode
));
2438 err
= zfs_onexit_add_cb(minor
,
2439 free_guid_map_onexit
, rwa
->guid_to_ds_map
,
2444 err
= zfs_onexit_cb_data(minor
, *action_handlep
,
2445 (void **)&rwa
->guid_to_ds_map
);
2450 drc
->drc_guid_to_ds_map
= rwa
->guid_to_ds_map
;
2453 payloadlen
= drc
->drc_drr_begin
->drr_payloadlen
;
2455 if (payloadlen
!= 0)
2456 payload
= kmem_alloc(payloadlen
, KM_SLEEP
);
2458 err
= receive_read_payload_and_next_header(ra
, payloadlen
, payload
);
2460 if (payloadlen
!= 0)
2461 kmem_free(payload
, payloadlen
);
2464 if (payloadlen
!= 0) {
2465 err
= nvlist_unpack(payload
, payloadlen
, &begin_nvl
, KM_SLEEP
);
2466 kmem_free(payload
, payloadlen
);
2471 /* handle DSL encryption key payload */
2472 if (featureflags
& DMU_BACKUP_FEATURE_RAW
) {
2473 nvlist_t
*keynvl
= NULL
;
2475 ASSERT(ra
->os
->os_encrypted
);
2476 ASSERT(drc
->drc_raw
);
2478 err
= nvlist_lookup_nvlist(begin_nvl
, "crypt_keydata", &keynvl
);
2483 * If this is a new dataset we set the key immediately.
2484 * Otherwise we don't want to change the key until we
2485 * are sure the rest of the receive succeeded so we stash
2486 * the keynvl away until then.
2488 err
= dsl_crypto_recv_raw(spa_name(ra
->os
->os_spa
),
2489 drc
->drc_ds
->ds_object
, drc
->drc_drrb
->drr_type
,
2490 keynvl
, drc
->drc_newfs
);
2494 if (!drc
->drc_newfs
)
2495 drc
->drc_keynvl
= fnvlist_dup(keynvl
);
2498 if (featureflags
& DMU_BACKUP_FEATURE_RESUMING
) {
2499 err
= resume_check(ra
, begin_nvl
);
2504 (void) bqueue_init(&rwa
->q
,
2505 MAX(zfs_recv_queue_length
, 2 * zfs_max_recordsize
),
2506 offsetof(struct receive_record_arg
, node
));
2507 cv_init(&rwa
->cv
, NULL
, CV_DEFAULT
, NULL
);
2508 mutex_init(&rwa
->mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
2510 rwa
->byteswap
= drc
->drc_byteswap
;
2511 rwa
->resumable
= drc
->drc_resumable
;
2512 rwa
->raw
= drc
->drc_raw
;
2513 rwa
->os
->os_raw_receive
= drc
->drc_raw
;
2515 (void) thread_create(NULL
, 0, receive_writer_thread
, rwa
, 0, curproc
,
2516 TS_RUN
, minclsyspri
);
2518 * We're reading rwa->err without locks, which is safe since we are the
2519 * only reader, and the worker thread is the only writer. It's ok if we
2520 * miss a write for an iteration or two of the loop, since the writer
2521 * thread will keep freeing records we send it until we send it an eos
2524 * We can leave this loop in 3 ways: First, if rwa->err is
2525 * non-zero. In that case, the writer thread will free the rrd we just
2526 * pushed. Second, if we're interrupted; in that case, either it's the
2527 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
2528 * has been handed off to the writer thread who will free it. Finally,
2529 * if receive_read_record fails or we're at the end of the stream, then
2530 * we free ra->rrd and exit.
2532 while (rwa
->err
== 0) {
2533 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
2534 err
= SET_ERROR(EINTR
);
2538 ASSERT3P(ra
->rrd
, ==, NULL
);
2539 ra
->rrd
= ra
->next_rrd
;
2540 ra
->next_rrd
= NULL
;
2541 /* Allocates and loads header into ra->next_rrd */
2542 err
= receive_read_record(ra
);
2544 if (ra
->rrd
->header
.drr_type
== DRR_END
|| err
!= 0) {
2545 kmem_free(ra
->rrd
, sizeof (*ra
->rrd
));
2550 bqueue_enqueue(&rwa
->q
, ra
->rrd
,
2551 sizeof (struct receive_record_arg
) + ra
->rrd
->payload_size
);
2554 if (ra
->next_rrd
== NULL
)
2555 ra
->next_rrd
= kmem_zalloc(sizeof (*ra
->next_rrd
), KM_SLEEP
);
2556 ra
->next_rrd
->eos_marker
= B_TRUE
;
2557 bqueue_enqueue(&rwa
->q
, ra
->next_rrd
, 1);
2559 mutex_enter(&rwa
->mutex
);
2560 while (!rwa
->done
) {
2561 cv_wait(&rwa
->cv
, &rwa
->mutex
);
2563 mutex_exit(&rwa
->mutex
);
2566 * If we are receiving a full stream as a clone, all object IDs which
2567 * are greater than the maximum ID referenced in the stream are
2568 * by definition unused and must be freed.
2570 if (drc
->drc_clone
&& drc
->drc_drrb
->drr_fromguid
== 0) {
2571 uint64_t obj
= rwa
->max_object
+ 1;
2575 while (next_err
== 0) {
2576 free_err
= dmu_free_long_object(rwa
->os
, obj
);
2577 if (free_err
!= 0 && free_err
!= ENOENT
)
2580 next_err
= dmu_object_next(rwa
->os
, &obj
, FALSE
, 0);
2584 if (free_err
!= 0 && free_err
!= ENOENT
)
2586 else if (next_err
!= ESRCH
)
2591 cv_destroy(&rwa
->cv
);
2592 mutex_destroy(&rwa
->mutex
);
2593 bqueue_destroy(&rwa
->q
);
2598 nvlist_free(begin_nvl
);
2599 if ((featureflags
& DMU_BACKUP_FEATURE_DEDUP
) && (cleanup_fd
!= -1))
2600 zfs_onexit_fd_rele(cleanup_fd
);
2604 * Clean up references. If receive is not resumable,
2605 * destroy what we created, so we don't leave it in
2606 * the inconsistent state.
2608 dmu_recv_cleanup_ds(drc
);
2609 nvlist_free(drc
->drc_keynvl
);
2613 objlist_destroy(&ra
->ignore_objlist
);
2614 kmem_free(ra
, sizeof (*ra
));
2615 kmem_free(rwa
, sizeof (*rwa
));
2620 dmu_recv_end_check(void *arg
, dmu_tx_t
*tx
)
2622 dmu_recv_cookie_t
*drc
= arg
;
2623 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2626 ASSERT3P(drc
->drc_ds
->ds_owner
, ==, dmu_recv_tag
);
2628 if (!drc
->drc_newfs
) {
2629 dsl_dataset_t
*origin_head
;
2631 error
= dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
, &origin_head
);
2634 if (drc
->drc_force
) {
2636 * We will destroy any snapshots in tofs (i.e. before
2637 * origin_head) that are after the origin (which is
2638 * the snap before drc_ds, because drc_ds can not
2639 * have any snaps of its own).
2643 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
2645 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
2646 dsl_dataset_t
*snap
;
2647 error
= dsl_dataset_hold_obj(dp
, obj
, FTAG
,
2651 if (snap
->ds_dir
!= origin_head
->ds_dir
)
2652 error
= SET_ERROR(EINVAL
);
2654 error
= dsl_destroy_snapshot_check_impl(
2657 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
2658 dsl_dataset_rele(snap
, FTAG
);
2663 dsl_dataset_rele(origin_head
, FTAG
);
2667 if (drc
->drc_keynvl
!= NULL
) {
2668 error
= dsl_crypto_recv_raw_key_check(drc
->drc_ds
,
2669 drc
->drc_keynvl
, tx
);
2671 dsl_dataset_rele(origin_head
, FTAG
);
2676 error
= dsl_dataset_clone_swap_check_impl(drc
->drc_ds
,
2677 origin_head
, drc
->drc_force
, drc
->drc_owner
, tx
);
2679 dsl_dataset_rele(origin_head
, FTAG
);
2682 error
= dsl_dataset_snapshot_check_impl(origin_head
,
2683 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
2684 dsl_dataset_rele(origin_head
, FTAG
);
2688 error
= dsl_destroy_head_check_impl(drc
->drc_ds
, 1);
2690 error
= dsl_dataset_snapshot_check_impl(drc
->drc_ds
,
2691 drc
->drc_tosnap
, tx
, B_TRUE
, 1, drc
->drc_cred
);
2697 dmu_recv_end_sync(void *arg
, dmu_tx_t
*tx
)
2699 dmu_recv_cookie_t
*drc
= arg
;
2700 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
2701 boolean_t encrypted
= drc
->drc_ds
->ds_dir
->dd_crypto_obj
!= 0;
2703 spa_history_log_internal_ds(drc
->drc_ds
, "finish receiving",
2704 tx
, "snap=%s", drc
->drc_tosnap
);
2705 drc
->drc_ds
->ds_objset
->os_raw_receive
= B_FALSE
;
2707 if (!drc
->drc_newfs
) {
2708 dsl_dataset_t
*origin_head
;
2710 VERIFY0(dsl_dataset_hold(dp
, drc
->drc_tofs
, FTAG
,
2713 if (drc
->drc_force
) {
2715 * Destroy any snapshots of drc_tofs (origin_head)
2716 * after the origin (the snap before drc_ds).
2720 obj
= dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
2722 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
) {
2723 dsl_dataset_t
*snap
;
2724 VERIFY0(dsl_dataset_hold_obj(dp
, obj
, FTAG
,
2726 ASSERT3P(snap
->ds_dir
, ==, origin_head
->ds_dir
);
2727 obj
= dsl_dataset_phys(snap
)->ds_prev_snap_obj
;
2728 dsl_destroy_snapshot_sync_impl(snap
,
2730 dsl_dataset_rele(snap
, FTAG
);
2733 if (drc
->drc_keynvl
!= NULL
) {
2734 dsl_crypto_recv_raw_key_sync(drc
->drc_ds
,
2735 drc
->drc_keynvl
, tx
);
2736 nvlist_free(drc
->drc_keynvl
);
2737 drc
->drc_keynvl
= NULL
;
2740 VERIFY3P(drc
->drc_ds
->ds_prev
, ==, origin_head
->ds_prev
);
2742 dsl_dataset_clone_swap_sync_impl(drc
->drc_ds
,
2744 dsl_dataset_snapshot_sync_impl(origin_head
,
2745 drc
->drc_tosnap
, tx
);
2747 /* set snapshot's creation time and guid */
2748 dmu_buf_will_dirty(origin_head
->ds_prev
->ds_dbuf
, tx
);
2749 dsl_dataset_phys(origin_head
->ds_prev
)->ds_creation_time
=
2750 drc
->drc_drrb
->drr_creation_time
;
2751 dsl_dataset_phys(origin_head
->ds_prev
)->ds_guid
=
2752 drc
->drc_drrb
->drr_toguid
;
2753 dsl_dataset_phys(origin_head
->ds_prev
)->ds_flags
&=
2754 ~DS_FLAG_INCONSISTENT
;
2756 dmu_buf_will_dirty(origin_head
->ds_dbuf
, tx
);
2757 dsl_dataset_phys(origin_head
)->ds_flags
&=
2758 ~DS_FLAG_INCONSISTENT
;
2760 drc
->drc_newsnapobj
=
2761 dsl_dataset_phys(origin_head
)->ds_prev_snap_obj
;
2763 dsl_dataset_rele(origin_head
, FTAG
);
2764 dsl_destroy_head_sync_impl(drc
->drc_ds
, tx
);
2766 if (drc
->drc_owner
!= NULL
)
2767 VERIFY3P(origin_head
->ds_owner
, ==, drc
->drc_owner
);
2769 dsl_dataset_t
*ds
= drc
->drc_ds
;
2771 dsl_dataset_snapshot_sync_impl(ds
, drc
->drc_tosnap
, tx
);
2773 /* set snapshot's creation time and guid */
2774 dmu_buf_will_dirty(ds
->ds_prev
->ds_dbuf
, tx
);
2775 dsl_dataset_phys(ds
->ds_prev
)->ds_creation_time
=
2776 drc
->drc_drrb
->drr_creation_time
;
2777 dsl_dataset_phys(ds
->ds_prev
)->ds_guid
=
2778 drc
->drc_drrb
->drr_toguid
;
2779 dsl_dataset_phys(ds
->ds_prev
)->ds_flags
&=
2780 ~DS_FLAG_INCONSISTENT
;
2782 dmu_buf_will_dirty(ds
->ds_dbuf
, tx
);
2783 dsl_dataset_phys(ds
)->ds_flags
&= ~DS_FLAG_INCONSISTENT
;
2784 if (dsl_dataset_has_resume_receive_state(ds
)) {
2785 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
2786 DS_FIELD_RESUME_FROMGUID
, tx
);
2787 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
2788 DS_FIELD_RESUME_OBJECT
, tx
);
2789 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
2790 DS_FIELD_RESUME_OFFSET
, tx
);
2791 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
2792 DS_FIELD_RESUME_BYTES
, tx
);
2793 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
2794 DS_FIELD_RESUME_TOGUID
, tx
);
2795 (void) zap_remove(dp
->dp_meta_objset
, ds
->ds_object
,
2796 DS_FIELD_RESUME_TONAME
, tx
);
2798 drc
->drc_newsnapobj
=
2799 dsl_dataset_phys(drc
->drc_ds
)->ds_prev_snap_obj
;
2801 zvol_create_minors(dp
->dp_spa
, drc
->drc_tofs
, B_TRUE
);
2804 * Release the hold from dmu_recv_begin. This must be done before
2805 * we return to open context, so that when we free the dataset's dnode
2806 * we can evict its bonus buffer. Since the dataset may be destroyed
2807 * at this point (and therefore won't have a valid pointer to the spa)
2808 * we release the key mapping manually here while we do have a valid
2809 * pointer, if it exists.
2811 if (!drc
->drc_raw
&& encrypted
) {
2812 (void) spa_keystore_remove_mapping(dmu_tx_pool(tx
)->dp_spa
,
2813 drc
->drc_ds
->ds_object
, drc
->drc_ds
);
2815 dsl_dataset_disown(drc
->drc_ds
, 0, dmu_recv_tag
);
2820 add_ds_to_guidmap(const char *name
, avl_tree_t
*guid_map
, uint64_t snapobj
,
2824 dsl_dataset_t
*snapds
;
2825 guid_map_entry_t
*gmep
;
2827 ds_hold_flags_t dsflags
= (raw
) ? 0 : DS_HOLD_FLAG_DECRYPT
;
2830 ASSERT(guid_map
!= NULL
);
2832 err
= dsl_pool_hold(name
, FTAG
, &dp
);
2835 gmep
= kmem_alloc(sizeof (*gmep
), KM_SLEEP
);
2836 err
= dsl_dataset_own_obj(dp
, snapobj
, dsflags
, gmep
, &snapds
);
2839 * If this is a deduplicated raw send stream, we need
2840 * to make sure that we can still read raw blocks from
2841 * earlier datasets in the stream, so we set the
2842 * os_raw_receive flag now.
2845 err
= dmu_objset_from_ds(snapds
, &os
);
2847 dsl_dataset_disown(snapds
, dsflags
, FTAG
);
2848 dsl_pool_rele(dp
, FTAG
);
2849 kmem_free(gmep
, sizeof (*gmep
));
2852 os
->os_raw_receive
= B_TRUE
;
2856 gmep
->guid
= dsl_dataset_phys(snapds
)->ds_guid
;
2857 gmep
->gme_ds
= snapds
;
2858 avl_add(guid_map
, gmep
);
2860 kmem_free(gmep
, sizeof (*gmep
));
2863 dsl_pool_rele(dp
, FTAG
);
2867 static int dmu_recv_end_modified_blocks
= 3;
2870 dmu_recv_existing_end(dmu_recv_cookie_t
*drc
)
2874 * We will be destroying the ds; make sure its origin is unmounted if
2877 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2878 dsl_dataset_name(drc
->drc_ds
, name
);
2879 zfs_destroy_unmount_origin(name
);
2882 return (dsl_sync_task(drc
->drc_tofs
,
2883 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2884 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
));
2888 dmu_recv_new_end(dmu_recv_cookie_t
*drc
)
2890 return (dsl_sync_task(drc
->drc_tofs
,
2891 dmu_recv_end_check
, dmu_recv_end_sync
, drc
,
2892 dmu_recv_end_modified_blocks
, ZFS_SPACE_CHECK_NORMAL
));
2896 dmu_recv_end(dmu_recv_cookie_t
*drc
, void *owner
)
2900 drc
->drc_owner
= owner
;
2903 error
= dmu_recv_new_end(drc
);
2905 error
= dmu_recv_existing_end(drc
);
2908 dmu_recv_cleanup_ds(drc
);
2909 nvlist_free(drc
->drc_keynvl
);
2910 } else if (drc
->drc_guid_to_ds_map
!= NULL
) {
2911 (void) add_ds_to_guidmap(drc
->drc_tofs
, drc
->drc_guid_to_ds_map
,
2912 drc
->drc_newsnapobj
, drc
->drc_raw
);
2918 * Return TRUE if this objset is currently being received into.
2921 dmu_objset_is_receiving(objset_t
*os
)
2923 return (os
->os_dsl_dataset
!= NULL
&&
2924 os
->os_dsl_dataset
->ds_owner
== dmu_recv_tag
);
2927 #if defined(_KERNEL)
2928 module_param(zfs_recv_queue_length
, int, 0644);
2929 MODULE_PARM_DESC(zfs_recv_queue_length
, "Maximum receive queue length");