]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dmu_recv.c
Code improvement and bug fixes for QAT support
[mirror_zfs.git] / module / zfs / dmu_recv.c
CommitLineData
03916905
PD
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
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.
d8d418ff 29 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
03916905
PD
30 */
31
32#include <sys/dmu.h>
33#include <sys/dmu_impl.h>
34#include <sys/dmu_tx.h>
35#include <sys/dbuf.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>
47#include <sys/zap.h>
48#include <sys/zio_checksum.h>
49#include <sys/zfs_znode.h>
50#include <zfs_fletcher.h>
51#include <sys/avl.h>
52#include <sys/ddt.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>
60#include <sys/zvol.h>
61#include <sys/policy.h>
62
63int zfs_recv_queue_length = SPA_MAXBLOCKSIZE;
64
65static char *dmu_recv_tag = "dmu_recv_tag";
66const char *recv_clone_name = "%recv";
67
68static void byteswap_record(dmu_replay_record_t *drr);
69
70typedef struct dmu_recv_begin_arg {
71 const char *drba_origin;
72 dmu_recv_cookie_t *drba_cookie;
73 cred_t *drba_cred;
74 dsl_crypto_params_t *drba_dcp;
03916905
PD
75} dmu_recv_begin_arg_t;
76
77static int
78recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
79 uint64_t fromguid, uint64_t featureflags)
80{
81 uint64_t val;
d8d418ff 82 uint64_t children;
03916905
PD
83 int error;
84 dsl_pool_t *dp = ds->ds_dir->dd_pool;
85 boolean_t encrypted = ds->ds_dir->dd_crypto_obj != 0;
86 boolean_t raw = (featureflags & DMU_BACKUP_FEATURE_RAW) != 0;
87 boolean_t embed = (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) != 0;
88
89 /* temporary clone name must not exist */
90 error = zap_lookup(dp->dp_meta_objset,
91 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
92 8, 1, &val);
93 if (error != ENOENT)
94 return (error == 0 ? EBUSY : error);
95
96 /* new snapshot name must not exist */
97 error = zap_lookup(dp->dp_meta_objset,
98 dsl_dataset_phys(ds)->ds_snapnames_zapobj,
99 drba->drba_cookie->drc_tosnap, 8, 1, &val);
100 if (error != ENOENT)
101 return (error == 0 ? EEXIST : error);
102
d8d418ff 103 /* must not have children if receiving a ZVOL */
104 error = zap_count(dp->dp_meta_objset,
105 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, &children);
106 if (error != 0)
107 return (error);
108 if (drba->drba_cookie->drc_drrb->drr_type != DMU_OST_ZFS &&
109 children > 0)
110 return (SET_ERROR(ZFS_ERR_WRONG_PARENT));
111
03916905
PD
112 /*
113 * Check snapshot limit before receiving. We'll recheck again at the
114 * end, but might as well abort before receiving if we're already over
115 * the limit.
116 *
117 * Note that we do not check the file system limit with
118 * dsl_dir_fscount_check because the temporary %clones don't count
119 * against that limit.
120 */
121 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
122 NULL, drba->drba_cred);
123 if (error != 0)
124 return (error);
125
126 if (fromguid != 0) {
127 dsl_dataset_t *snap;
128 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
129
f00ab3f2 130 /* Can't raw receive on top of an unencrypted dataset */
03916905
PD
131 if (!encrypted && raw)
132 return (SET_ERROR(EINVAL));
133
134 /* Encryption is incompatible with embedded data */
135 if (encrypted && embed)
136 return (SET_ERROR(EINVAL));
137
138 /* Find snapshot in this dir that matches fromguid. */
139 while (obj != 0) {
140 error = dsl_dataset_hold_obj(dp, obj, FTAG,
141 &snap);
142 if (error != 0)
143 return (SET_ERROR(ENODEV));
144 if (snap->ds_dir != ds->ds_dir) {
145 dsl_dataset_rele(snap, FTAG);
146 return (SET_ERROR(ENODEV));
147 }
148 if (dsl_dataset_phys(snap)->ds_guid == fromguid)
149 break;
150 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
151 dsl_dataset_rele(snap, FTAG);
152 }
153 if (obj == 0)
154 return (SET_ERROR(ENODEV));
155
156 if (drba->drba_cookie->drc_force) {
f00ab3f2 157 drba->drba_cookie->drc_fromsnapobj = obj;
03916905
PD
158 } else {
159 /*
160 * If we are not forcing, there must be no
161 * changes since fromsnap.
162 */
163 if (dsl_dataset_modified_since_snap(ds, snap)) {
164 dsl_dataset_rele(snap, FTAG);
165 return (SET_ERROR(ETXTBSY));
166 }
f00ab3f2
TC
167 drba->drba_cookie->drc_fromsnapobj =
168 ds->ds_prev->ds_object;
03916905
PD
169 }
170
171 dsl_dataset_rele(snap, FTAG);
172 } else {
173 /* if full, then must be forced */
174 if (!drba->drba_cookie->drc_force)
175 return (SET_ERROR(EEXIST));
176
177 /*
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.
182 */
183 if ((!encrypted && raw) || encrypted)
184 return (SET_ERROR(EINVAL));
185
186 /*
187 * Perform the same encryption checks we would if
188 * we were creating a new dataset from scratch.
189 */
190 if (!raw) {
191 boolean_t will_encrypt;
192
193 error = dmu_objset_create_crypt_check(
194 ds->ds_dir->dd_parent, drba->drba_dcp,
195 &will_encrypt);
196 if (error != 0)
197 return (error);
198
199 if (will_encrypt && embed)
200 return (SET_ERROR(EINVAL));
201 }
202
f00ab3f2 203 drba->drba_cookie->drc_fromsnapobj = 0;
03916905
PD
204 }
205
206 return (0);
207
208}
209
210static int
211dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
212{
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;
219 int error;
220 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
221 dsl_dataset_t *ds;
222 const char *tofs = drba->drba_cookie->drc_tofs;
223
224 /* already checked */
225 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
226 ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
227
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));
233
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));
238
239 if (drba->drba_cookie->drc_resumable &&
240 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
241 return (SET_ERROR(ENOTSUP));
242
243 /*
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.
248 */
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));
255
256 /*
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
260 * large dnodes.
261 */
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));
268
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));
273
274 /* embedded data is incompatible with encryption and raw recv */
275 if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
276 return (SET_ERROR(EINVAL));
277 } else {
278 dsflags |= DS_HOLD_FLAG_DECRYPT;
279 }
280
281 error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
282 if (error == 0) {
283 /* target fs already exists; recv into temp clone */
284
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));
289 }
290
291 error = recv_begin_check_existing_impl(drba, ds, fromguid,
292 featureflags);
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];
d8d418ff 297 objset_t *os;
03916905
PD
298
299 /*
300 * If it's a non-clone incremental, we are missing the
301 * target fs, so fail the recv.
302 */
303 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
304 drba->drba_origin))
305 return (SET_ERROR(ENOENT));
306
307 /*
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.
311 */
312 if (fromguid == 0 && drba->drba_origin &&
313 !(flags & DRR_FLAG_FREERECORDS))
314 return (SET_ERROR(EINVAL));
315
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);
320 if (error != 0)
321 return (error);
322
323 if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0 &&
324 drba->drba_origin == NULL) {
325 boolean_t will_encrypt;
326
327 /*
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
332 * embedded data.
333 */
334 error = dmu_objset_create_crypt_check(ds->ds_dir,
335 drba->drba_dcp, &will_encrypt);
336 if (error != 0) {
337 dsl_dataset_rele_flags(ds, dsflags, FTAG);
338 return (error);
339 }
340
341 if (will_encrypt &&
342 (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)) {
343 dsl_dataset_rele_flags(ds, dsflags, FTAG);
344 return (SET_ERROR(EINVAL));
345 }
346 }
347
348 /*
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).
352 */
353 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
354 ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
355 if (error != 0) {
356 dsl_dataset_rele_flags(ds, dsflags, FTAG);
357 return (error);
358 }
359
360 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
361 ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
362 if (error != 0) {
363 dsl_dataset_rele_flags(ds, dsflags, FTAG);
364 return (error);
365 }
366
d8d418ff 367 /* can't recv below anything but filesystems (eg. no ZVOLs) */
368 error = dmu_objset_from_ds(ds, &os);
369 if (error != 0) {
370 dsl_dataset_rele_flags(ds, dsflags, FTAG);
371 return (error);
372 }
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));
376 }
377
03916905
PD
378 if (drba->drba_origin != NULL) {
379 dsl_dataset_t *origin;
380
381 error = dsl_dataset_hold_flags(dp, drba->drba_origin,
382 dsflags, FTAG, &origin);
383 if (error != 0) {
384 dsl_dataset_rele_flags(ds, dsflags, FTAG);
385 return (error);
386 }
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));
391 }
392 if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
393 fromguid != 0) {
394 dsl_dataset_rele_flags(origin, dsflags, FTAG);
395 dsl_dataset_rele_flags(ds, dsflags, FTAG);
396 return (SET_ERROR(ENODEV));
397 }
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));
403 }
404 dsl_dataset_rele_flags(origin,
405 dsflags, FTAG);
406 }
d8d418ff 407
03916905
PD
408 dsl_dataset_rele_flags(ds, dsflags, FTAG);
409 error = 0;
410 }
411 return (error);
412}
413
414static void
415dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
416{
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;
424 objset_t *os;
425 uint64_t dsobj;
426 ds_hold_flags_t dsflags = 0;
427 int error;
428 uint64_t crflags = 0;
429 dsl_crypto_params_t dummy_dcp = { 0 };
430 dsl_crypto_params_t *dcp = drba->drba_dcp;
431
432 if (drrb->drr_flags & DRR_FLAG_CI_DATA)
433 crflags |= DS_FLAG_CI_DATASET;
434
435 if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0)
436 dsflags |= DS_HOLD_FLAG_DECRYPT;
437
438 /*
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.
442 */
f00ab3f2 443 if (dcp == NULL && drba->drba_cookie->drc_fromsnapobj == 0 &&
03916905
PD
444 drba->drba_origin == NULL) {
445 ASSERT3P(dcp, ==, NULL);
446 dcp = &dummy_dcp;
447
448 if (featureflags & DMU_BACKUP_FEATURE_RAW)
449 dcp->cp_cmd = DCP_CMD_RAW_RECV;
450 }
451
452 error = dsl_dataset_hold_flags(dp, tofs, dsflags, FTAG, &ds);
453 if (error == 0) {
454 /* create temporary clone */
455 dsl_dataset_t *snap = NULL;
456
f00ab3f2 457 if (drba->drba_cookie->drc_fromsnapobj != 0) {
03916905 458 VERIFY0(dsl_dataset_hold_obj(dp,
f00ab3f2 459 drba->drba_cookie->drc_fromsnapobj, FTAG, &snap));
03916905
PD
460 ASSERT3P(dcp, ==, NULL);
461 }
462
463 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
464 snap, crflags, drba->drba_cred, dcp, tx);
f00ab3f2 465 if (drba->drba_cookie->drc_fromsnapobj != 0)
03916905
PD
466 dsl_dataset_rele(snap, FTAG);
467 dsl_dataset_rele_flags(ds, dsflags, FTAG);
468 } else {
469 dsl_dir_t *dd;
470 const char *tail;
471 dsl_dataset_t *origin = NULL;
472
473 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
474
475 if (drba->drba_origin != NULL) {
476 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
477 FTAG, &origin));
478 ASSERT3P(dcp, ==, NULL);
479 }
480
481 /* Create new dataset. */
482 dsobj = dsl_dataset_create_sync(dd, strrchr(tofs, '/') + 1,
483 origin, crflags, drba->drba_cred, dcp, tx);
484 if (origin != NULL)
485 dsl_dataset_rele(origin, FTAG);
486 dsl_dir_rele(dd, FTAG);
487 drba->drba_cookie->drc_newfs = B_TRUE;
488 }
489
490 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dsflags, dmu_recv_tag, &newds));
491 VERIFY0(dmu_objset_from_ds(newds, &os));
492
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));
498 }
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));
503 uint64_t one = 1;
504 uint64_t zero = 0;
505 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
506 8, 1, &one, tx));
507 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
508 8, 1, &zero, tx));
509 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
510 8, 1, &zero, tx));
511 if (featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
512 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
513 8, 1, &one, tx));
514 }
515 if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) {
516 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
517 8, 1, &one, tx));
518 }
519 if (featureflags & DMU_BACKUP_FEATURE_COMPRESSED) {
520 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
521 8, 1, &one, tx));
522 }
523 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
524 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_RAWOK,
525 8, 1, &one, tx));
526 }
527 }
528
529 /*
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.
533 */
534 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
535 os->os_encrypted = B_TRUE;
536 drba->drba_cookie->drc_raw = B_TRUE;
537 }
538
539 dmu_buf_will_dirty(newds->ds_dbuf, tx);
540 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
541
542 /*
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.
547 */
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);
553 }
554 rrw_exit(&newds->ds_bp_rwlock, FTAG);
555
556 drba->drba_cookie->drc_ds = newds;
557
558 spa_history_log_internal_ds(newds, "receive", tx, "");
559}
560
561static int
562dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
563{
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;
567 int error;
568 ds_hold_flags_t dsflags = 0;
569 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
570 dsl_dataset_t *ds;
571 const char *tofs = drba->drba_cookie->drc_tofs;
572
573 /* already checked */
574 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
575 ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
576
577 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
578 DMU_COMPOUNDSTREAM ||
579 drrb->drr_type >= DMU_OST_NUMTYPES)
580 return (SET_ERROR(EINVAL));
581
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));
586
587 /*
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.
592 */
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));
599
600 /*
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
604 * large dnodes.
605 */
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));
612
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);
617
618 if ((featureflags & DMU_BACKUP_FEATURE_RAW) == 0)
619 dsflags |= DS_HOLD_FLAG_DECRYPT;
620
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);
624 if (error != 0)
625 return (error);
626 }
627
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));
632 }
633
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));
638 }
639 uint64_t val;
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));
645 }
646
647 /*
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.
651 */
652 if (dsl_dataset_has_owner(ds)) {
653 dsl_dataset_rele_flags(ds, dsflags, FTAG);
654 return (SET_ERROR(EBUSY));
655 }
656
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));
661 }
662
663 /*
664 * Note: resume point will be checked when we process the first WRITE
665 * record.
666 */
667
668 /* check that the origin matches */
669 val = 0;
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));
675 }
676
677 dsl_dataset_rele_flags(ds, dsflags, FTAG);
678 return (0);
679}
680
681static void
682dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
683{
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);
689 dsl_dataset_t *ds;
690 objset_t *os;
691 ds_hold_flags_t dsflags = 0;
692 uint64_t dsobj;
693 /* 6 extra bytes for /%recv */
694 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
695
696 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
697 tofs, recv_clone_name);
698
699 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
700 drba->drba_cookie->drc_raw = B_TRUE;
701 } else {
702 dsflags |= DS_HOLD_FLAG_DECRYPT;
703 }
704
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;
709 }
710
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);
717
718 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dsflags, dmu_recv_tag, &ds));
719 VERIFY0(dmu_objset_from_ds(ds, &os));
720
721 dmu_buf_will_dirty(ds->ds_dbuf, tx);
722 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
723
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);
728
729 drba->drba_cookie->drc_ds = ds;
730
731 spa_history_log_internal_ds(ds, "resume receive", tx, "");
732}
733
734/*
735 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
736 * succeeds; otherwise we will leak the holds on the datasets.
737 */
738int
739dmu_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)
742{
743 dmu_recv_begin_arg_t drba = { 0 };
744
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);
754
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);
763 } else {
764 return (SET_ERROR(EINVAL));
765 }
766
767 drba.drba_origin = origin;
768 drba.drba_cookie = drc;
769 drba.drba_cred = CRED();
770
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));
776 } else {
777 int err;
778
779 /*
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.
788 */
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);
794 if (err != 0)
795 return (err);
796 }
797
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);
802
803 return (err);
804 }
805}
806
807struct receive_record_arg {
808 dmu_replay_record_t header;
809 void *payload; /* Pointer to a buffer containing the payload */
810 /*
811 * If the record is a write, pointer to the arc_buf_t containing the
812 * payload.
813 */
814 arc_buf_t *arc_buf;
815 int payload_size;
816 uint64_t bytes_read; /* bytes read from stream when record created */
817 boolean_t eos_marker; /* Marks the end of the stream */
818 bqueue_node_t node;
819};
820
821struct receive_writer_arg {
822 objset_t *os;
823 boolean_t byteswap;
824 bqueue_t q;
825
826 /*
827 * These three args are used to signal to the main thread that we're
828 * done.
829 */
830 kmutex_t mutex;
831 kcondvar_t cv;
832 boolean_t done;
833
834 int err;
835 /* A map from guid to dataset to help handle dedup'd streams. */
836 avl_tree_t *guid_to_ds_map;
837 boolean_t resumable;
838 boolean_t raw;
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 */
843
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;
852};
853
854struct objlist {
855 list_t list; /* List of struct receive_objnode. */
856 /*
857 * Last object looked up. Used to assert that objects are being looked
858 * up in ascending order.
859 */
860 uint64_t last_lookup;
861};
862
863struct receive_objnode {
864 list_node_t node;
865 uint64_t object;
866};
867
868struct receive_arg {
869 objset_t *os;
870 vnode_t *vp; /* The vnode to read the stream from */
871 uint64_t voff; /* The current offset in the stream */
872 uint64_t bytes_read;
873 /*
874 * A record that has had its payload read in, but hasn't yet been handed
875 * off to the worker thread.
876 */
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;
880 zio_cksum_t cksum;
881 zio_cksum_t prev_cksum;
882 int err;
883 boolean_t byteswap;
884 boolean_t raw;
885 uint64_t featureflags;
886 /* Sorted list of objects not to issue prefetches for. */
887 struct objlist ignore_objlist;
888};
889
890typedef struct guid_map_entry {
891 uint64_t guid;
892 boolean_t raw;
893 dsl_dataset_t *gme_ds;
894 avl_node_t avlnode;
895} guid_map_entry_t;
896
897static int
898guid_compare(const void *arg1, const void *arg2)
899{
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;
902
903 return (AVL_CMP(gmep1->guid, gmep2->guid));
904}
905
906static void
907free_guid_map_onexit(void *arg)
908{
909 avl_tree_t *ca = arg;
910 void *cookie = NULL;
911 guid_map_entry_t *gmep;
912
913 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
914 ds_hold_flags_t dsflags = DS_HOLD_FLAG_DECRYPT;
915
916 if (gmep->raw) {
917 gmep->gme_ds->ds_objset->os_raw_receive = B_FALSE;
918 dsflags &= ~DS_HOLD_FLAG_DECRYPT;
919 }
920
921 dsl_dataset_disown(gmep->gme_ds, dsflags, gmep);
922 kmem_free(gmep, sizeof (guid_map_entry_t));
923 }
924 avl_destroy(ca);
925 kmem_free(ca, sizeof (avl_tree_t));
926}
927
928static int
929receive_read(struct receive_arg *ra, int len, void *buf)
930{
931 int done = 0;
932
933 /*
934 * The code doesn't rely on this (lengths being multiples of 8). See
935 * comment in dump_bytes.
936 */
937 ASSERT(len % 8 == 0 ||
938 (ra->featureflags & DMU_BACKUP_FEATURE_RAW) != 0);
939
940 while (done < len) {
941 ssize_t resid;
942
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);
947
948 if (resid == len - done) {
949 /*
950 * Note: ECKSUM indicates that the receive
951 * was interrupted and can potentially be resumed.
952 */
953 ra->err = SET_ERROR(ECKSUM);
954 }
955 ra->voff += len - done - resid;
956 done = len - resid;
957 if (ra->err != 0)
958 return (ra->err);
959 }
960
961 ra->bytes_read += len;
962
963 ASSERT3U(done, ==, len);
964 return (0);
965}
966
967noinline static void
968byteswap_record(dmu_replay_record_t *drr)
969{
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);
974
975 switch (drr->drr_type) {
976 case DRR_BEGIN:
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);
984 break;
985 case DRR_OBJECT:
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);
994 break;
995 case DRR_FREEOBJECTS:
996 DO64(drr_freeobjects.drr_firstobj);
997 DO64(drr_freeobjects.drr_numobjs);
998 DO64(drr_freeobjects.drr_toguid);
999 break;
1000 case DRR_WRITE:
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);
1009 break;
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.
1019 drr_key.ddk_cksum);
1020 DO64(drr_write_byref.drr_key.ddk_prop);
1021 break;
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);
1029 break;
1030 case DRR_FREE:
1031 DO64(drr_free.drr_object);
1032 DO64(drr_free.drr_offset);
1033 DO64(drr_free.drr_length);
1034 DO64(drr_free.drr_toguid);
1035 break;
1036 case DRR_SPILL:
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);
1042 break;
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);
1047 break;
1048 case DRR_END:
1049 DO64(drr_end.drr_toguid);
1050 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1051 break;
1052 default:
1053 break;
1054 }
1055
1056 if (drr->drr_type != DRR_BEGIN) {
1057 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
1058 }
1059
1060#undef DO64
1061#undef DO32
1062}
1063
1064static inline uint8_t
1065deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1066{
1067 if (bonus_type == DMU_OT_SA) {
1068 return (1);
1069 } else {
1070 return (1 +
1071 ((DN_OLD_MAX_BONUSLEN -
1072 MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT));
1073 }
1074}
1075
1076static void
1077save_resume_state(struct receive_writer_arg *rwa,
1078 uint64_t object, uint64_t offset, dmu_tx_t *tx)
1079{
1080 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1081
1082 if (!rwa->resumable)
1083 return;
1084
1085 /*
1086 * We use ds_resume_bytes[] != 0 to indicate that we need to
1087 * update this on disk, so it must not be 0.
1088 */
1089 ASSERT(rwa->bytes_read != 0);
1090
1091 /*
1092 * We only resume from write records, which have a valid
1093 * (non-meta-dnode) object number.
1094 */
1095 ASSERT(object != 0);
1096
1097 /*
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.
1101 */
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]);
1107
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;
1111}
1112
1113noinline static int
1114receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1115 void *data)
1116{
1117 dmu_object_info_t doi;
1118 dmu_tx_t *tx;
1119 uint64_t object;
1120 int err;
1121 uint8_t dn_slots = drro->drr_dn_slots != 0 ?
1122 drro->drr_dn_slots : DNODE_MIN_SLOTS;
1123
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))) ||
1134 dn_slots >
1135 (spa_maxdnodesize(dmu_objset_spa(rwa->os)) >> DNODE_SHIFT)) {
1136 return (SET_ERROR(EINVAL));
1137 }
1138
1139 if (rwa->raw) {
1140 /*
1141 * We should have received a DRR_OBJECT_RANGE record
1142 * containing this block and stored it in rwa.
1143 */
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));
1153 } else {
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));
1158 }
1159
1160 err = dmu_object_info(rwa->os, drro->drr_object, &doi);
1161 if (err != 0 && err != ENOENT && err != EEXIST)
1162 return (SET_ERROR(EINVAL));
1163
1164 if (drro->drr_object > rwa->max_object)
1165 rwa->max_object = drro->drr_object;
1166
1167 /*
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.
1173 */
1174 if (err == 0) {
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);
c2c6eadf 1179 boolean_t did_free = B_FALSE;
03916905
PD
1180
1181 object = drro->drr_object;
1182
369aa501 1183 /* nblkptr should be bounded by the bonus size and type */
03916905
PD
1184 if (rwa->raw && nblkptr != drro->drr_nblkptr)
1185 return (SET_ERROR(EINVAL));
1186
369aa501
TC
1187 /*
1188 * Check for indicators that the object was freed and
1189 * reallocated. For all sends, these indicators are:
1190 * - A changed block size
1191 * - A smaller nblkptr
1192 * - A changed dnode size
1193 * For raw sends we also check a few other fields to
1194 * ensure we are preserving the objset structure exactly
1195 * as it was on the receive side:
1196 * - A changed indirect block size
1197 * - A smaller nlevels
1198 */
03916905
PD
1199 if (drro->drr_blksz != doi.doi_data_block_size ||
1200 nblkptr < doi.doi_nblkptr ||
1201 dn_slots != doi.doi_dnodesize >> DNODE_SHIFT ||
1202 (rwa->raw &&
1203 (indblksz != doi.doi_metadata_block_size ||
1204 drro->drr_nlevels < doi.doi_indirection))) {
1205 err = dmu_free_long_range(rwa->os,
1206 drro->drr_object, 0, DMU_OBJECT_END);
1207 if (err != 0)
1208 return (SET_ERROR(EINVAL));
c2c6eadf
TC
1209 else
1210 did_free = B_TRUE;
03916905
PD
1211 }
1212
1213 /*
1214 * The dmu does not currently support decreasing nlevels
369aa501
TC
1215 * or changing the number of dnode slots on an object. For
1216 * non-raw sends, this does not matter and the new object
1217 * can just use the previous one's nlevels. For raw sends,
1218 * however, the structure of the received dnode (including
1219 * nlevels and dnode slots) must match that of the send
1220 * side. Therefore, instead of using dmu_object_reclaim(),
1221 * we must free the object completely and call
1222 * dmu_object_claim_dnsize() instead.
03916905
PD
1223 */
1224 if ((rwa->raw && drro->drr_nlevels < doi.doi_indirection) ||
1225 dn_slots != doi.doi_dnodesize >> DNODE_SHIFT) {
1226 err = dmu_free_long_object(rwa->os, drro->drr_object);
1227 if (err != 0)
1228 return (SET_ERROR(EINVAL));
1229
1230 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1231 object = DMU_NEW_OBJECT;
1232 }
369aa501
TC
1233
1234 /*
1235 * For raw receives, free everything beyond the new incoming
1236 * maxblkid. Normally this would be done with a DRR_FREE
1237 * record that would come after this DRR_OBJECT record is
1238 * processed. However, for raw receives we manually set the
1239 * maxblkid from the drr_maxblkid and so we must first free
1240 * everything above that blkid to ensure the DMU is always
c2c6eadf
TC
1241 * consistent with itself. We will never free the first block
1242 * of the object here because a maxblkid of 0 could indicate
1243 * an object with a single block or one with no blocks. This
1244 * free may be skipped when dmu_free_long_range() was called
1245 * above since it covers the entire object's contents.
369aa501 1246 */
c2c6eadf 1247 if (rwa->raw && object != DMU_NEW_OBJECT && !did_free) {
369aa501 1248 err = dmu_free_long_range(rwa->os, drro->drr_object,
c2c6eadf 1249 (drro->drr_maxblkid + 1) * doi.doi_data_block_size,
369aa501
TC
1250 DMU_OBJECT_END);
1251 if (err != 0)
1252 return (SET_ERROR(EINVAL));
1253 }
03916905
PD
1254 } else if (err == EEXIST) {
1255 /*
1256 * The object requested is currently an interior slot of a
1257 * multi-slot dnode. This will be resolved when the next txg
1258 * is synced out, since the send stream will have told us
1259 * to free this slot when we freed the associated dnode
1260 * earlier in the stream.
1261 */
1262 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
b92f5d9f
BB
1263
1264 if (dmu_object_info(rwa->os, drro->drr_object, NULL) != ENOENT)
1265 return (SET_ERROR(EINVAL));
1266
1267 /* object was freed and we are about to allocate a new one */
1268 object = DMU_NEW_OBJECT;
03916905
PD
1269 } else {
1270 /* object is free and we are about to allocate a new one */
1271 object = DMU_NEW_OBJECT;
1272 }
1273
1274 /*
1275 * If this is a multi-slot dnode there is a chance that this
1276 * object will expand into a slot that is already used by
1277 * another object from the previous snapshot. We must free
1278 * these objects before we attempt to allocate the new dnode.
1279 */
1280 if (dn_slots > 1) {
1281 boolean_t need_sync = B_FALSE;
1282
1283 for (uint64_t slot = drro->drr_object + 1;
1284 slot < drro->drr_object + dn_slots;
1285 slot++) {
1286 dmu_object_info_t slot_doi;
1287
1288 err = dmu_object_info(rwa->os, slot, &slot_doi);
1289 if (err == ENOENT || err == EEXIST)
1290 continue;
1291 else if (err != 0)
1292 return (err);
1293
1294 err = dmu_free_long_object(rwa->os, slot);
03916905
PD
1295 if (err != 0)
1296 return (err);
1297
1298 need_sync = B_TRUE;
1299 }
1300
1301 if (need_sync)
1302 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1303 }
1304
1305 tx = dmu_tx_create(rwa->os);
1306 dmu_tx_hold_bonus(tx, object);
1307 dmu_tx_hold_write(tx, object, 0, 0);
1308 err = dmu_tx_assign(tx, TXG_WAIT);
1309 if (err != 0) {
1310 dmu_tx_abort(tx);
1311 return (err);
1312 }
1313
1314 if (object == DMU_NEW_OBJECT) {
1315 /* currently free, want to be allocated */
1316 err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
1317 drro->drr_type, drro->drr_blksz,
1318 drro->drr_bonustype, drro->drr_bonuslen,
1319 dn_slots << DNODE_SHIFT, tx);
1320 } else if (drro->drr_type != doi.doi_type ||
1321 drro->drr_blksz != doi.doi_data_block_size ||
1322 drro->drr_bonustype != doi.doi_bonus_type ||
1323 drro->drr_bonuslen != doi.doi_bonus_size) {
1324 /* currently allocated, but with different properties */
1325 err = dmu_object_reclaim_dnsize(rwa->os, drro->drr_object,
1326 drro->drr_type, drro->drr_blksz,
1327 drro->drr_bonustype, drro->drr_bonuslen,
1328 dn_slots << DNODE_SHIFT, tx);
1329 }
3fa93bb8 1330
03916905
PD
1331 if (err != 0) {
1332 dmu_tx_commit(tx);
1333 return (SET_ERROR(EINVAL));
1334 }
1335
1336 if (rwa->or_crypt_params_present) {
1337 /*
1338 * Set the crypt params for the buffer associated with this
1339 * range of dnodes. This causes the blkptr_t to have the
1340 * same crypt params (byteorder, salt, iv, mac) as on the
1341 * sending side.
1342 *
1343 * Since we are committing this tx now, it is possible for
1344 * the dnode block to end up on-disk with the incorrect MAC,
1345 * if subsequent objects in this block are received in a
1346 * different txg. However, since the dataset is marked as
1347 * inconsistent, no code paths will do a non-raw read (or
1348 * decrypt the block / verify the MAC). The receive code and
1349 * scrub code can safely do raw reads and verify the
1350 * checksum. They don't need to verify the MAC.
1351 */
1352 dmu_buf_t *db = NULL;
1353 uint64_t offset = rwa->or_firstobj * DNODE_MIN_SIZE;
1354
1355 err = dmu_buf_hold_by_dnode(DMU_META_DNODE(rwa->os),
1356 offset, FTAG, &db, DMU_READ_PREFETCH | DMU_READ_NO_DECRYPT);
1357 if (err != 0) {
1358 dmu_tx_commit(tx);
1359 return (SET_ERROR(EINVAL));
1360 }
1361
1362 dmu_buf_set_crypt_params(db, rwa->or_byteorder,
1363 rwa->or_salt, rwa->or_iv, rwa->or_mac, tx);
1364
1365 dmu_buf_rele(db, FTAG);
1366
1367 rwa->or_crypt_params_present = B_FALSE;
1368 }
1369
1370 dmu_object_set_checksum(rwa->os, drro->drr_object,
1371 drro->drr_checksumtype, tx);
1372 dmu_object_set_compress(rwa->os, drro->drr_object,
1373 drro->drr_compress, tx);
1374
1375 /* handle more restrictive dnode structuring for raw recvs */
1376 if (rwa->raw) {
1377 /*
369aa501
TC
1378 * Set the indirect block size, block shift, nlevels.
1379 * This will not fail because we ensured all of the
1380 * blocks were freed earlier if this is a new object.
1381 * For non-new objects block size and indirect block
1382 * shift cannot change and nlevels can only increase.
03916905
PD
1383 */
1384 VERIFY0(dmu_object_set_blocksize(rwa->os, drro->drr_object,
1385 drro->drr_blksz, drro->drr_indblkshift, tx));
1386 VERIFY0(dmu_object_set_nlevels(rwa->os, drro->drr_object,
1387 drro->drr_nlevels, tx));
369aa501
TC
1388
1389 /*
c2c6eadf
TC
1390 * Set the maxblkid. This will always succeed because
1391 * we freed all blocks beyond the new maxblkid above.
369aa501 1392 */
03916905
PD
1393 VERIFY0(dmu_object_set_maxblkid(rwa->os, drro->drr_object,
1394 drro->drr_maxblkid, tx));
1395 }
1396
1397 if (data != NULL) {
1398 dmu_buf_t *db;
6955b401 1399 dnode_t *dn;
03916905
PD
1400 uint32_t flags = DMU_READ_NO_PREFETCH;
1401
1402 if (rwa->raw)
1403 flags |= DMU_READ_NO_DECRYPT;
1404
6955b401
BB
1405 VERIFY0(dnode_hold(rwa->os, drro->drr_object, FTAG, &dn));
1406 VERIFY0(dmu_bonus_hold_by_dnode(dn, FTAG, &db, flags));
1407
03916905
PD
1408 dmu_buf_will_dirty(db, tx);
1409
1410 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
1411 bcopy(data, db->db_data, DRR_OBJECT_PAYLOAD_SIZE(drro));
1412
1413 /*
1414 * Raw bonus buffers have their byteorder determined by the
1415 * DRR_OBJECT_RANGE record.
1416 */
1417 if (rwa->byteswap && !rwa->raw) {
1418 dmu_object_byteswap_t byteswap =
1419 DMU_OT_BYTESWAP(drro->drr_bonustype);
1420 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
1421 DRR_OBJECT_PAYLOAD_SIZE(drro));
1422 }
1423 dmu_buf_rele(db, FTAG);
6955b401 1424 dnode_rele(dn, FTAG);
03916905
PD
1425 }
1426 dmu_tx_commit(tx);
1427
1428 return (0);
1429}
1430
1431/* ARGSUSED */
1432noinline static int
1433receive_freeobjects(struct receive_writer_arg *rwa,
1434 struct drr_freeobjects *drrfo)
1435{
1436 uint64_t obj;
1437 int next_err = 0;
1438
1439 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
1440 return (SET_ERROR(EINVAL));
1441
1442 for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
1443 obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
1444 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
1445 dmu_object_info_t doi;
1446 int err;
1447
1448 err = dmu_object_info(rwa->os, obj, &doi);
1449 if (err == ENOENT)
1450 continue;
1451 else if (err != 0)
1452 return (err);
1453
1454 err = dmu_free_long_object(rwa->os, obj);
1455
1456 if (err != 0)
1457 return (err);
1458
1459 if (obj > rwa->max_object)
1460 rwa->max_object = obj;
1461 }
1462 if (next_err != ESRCH)
1463 return (next_err);
1464 return (0);
1465}
1466
1467noinline static int
1468receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
1469 arc_buf_t *abuf)
1470{
1471 int err;
1472 dmu_tx_t *tx;
1473 dnode_t *dn;
1474
1475 if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
1476 !DMU_OT_IS_VALID(drrw->drr_type))
1477 return (SET_ERROR(EINVAL));
1478
1479 /*
1480 * For resuming to work, records must be in increasing order
1481 * by (object, offset).
1482 */
1483 if (drrw->drr_object < rwa->last_object ||
1484 (drrw->drr_object == rwa->last_object &&
1485 drrw->drr_offset < rwa->last_offset)) {
1486 return (SET_ERROR(EINVAL));
1487 }
1488 rwa->last_object = drrw->drr_object;
1489 rwa->last_offset = drrw->drr_offset;
1490
1491 if (rwa->last_object > rwa->max_object)
1492 rwa->max_object = rwa->last_object;
1493
1494 if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
1495 return (SET_ERROR(EINVAL));
1496
1497 tx = dmu_tx_create(rwa->os);
1498 dmu_tx_hold_write(tx, drrw->drr_object,
1499 drrw->drr_offset, drrw->drr_logical_size);
1500 err = dmu_tx_assign(tx, TXG_WAIT);
1501 if (err != 0) {
1502 dmu_tx_abort(tx);
1503 return (err);
1504 }
1505
1506 if (rwa->byteswap && !arc_is_encrypted(abuf) &&
1507 arc_get_compression(abuf) == ZIO_COMPRESS_OFF) {
1508 dmu_object_byteswap_t byteswap =
1509 DMU_OT_BYTESWAP(drrw->drr_type);
1510 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
1511 DRR_WRITE_PAYLOAD_SIZE(drrw));
1512 }
1513
1514 VERIFY0(dnode_hold(rwa->os, drrw->drr_object, FTAG, &dn));
305781da
TC
1515 err = dmu_assign_arcbuf_by_dnode(dn, drrw->drr_offset, abuf, tx);
1516 if (err != 0) {
1517 dnode_rele(dn, FTAG);
1518 dmu_tx_commit(tx);
1519 return (err);
1520 }
03916905
PD
1521 dnode_rele(dn, FTAG);
1522
1523 /*
1524 * Note: If the receive fails, we want the resume stream to start
1525 * with the same record that we last successfully received (as opposed
1526 * to the next record), so that we can verify that we are
1527 * resuming from the correct location.
1528 */
1529 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
1530 dmu_tx_commit(tx);
1531
1532 return (0);
1533}
1534
1535/*
1536 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1537 * streams to refer to a copy of the data that is already on the
1538 * system because it came in earlier in the stream. This function
1539 * finds the earlier copy of the data, and uses that copy instead of
1540 * data from the stream to fulfill this write.
1541 */
1542static int
1543receive_write_byref(struct receive_writer_arg *rwa,
1544 struct drr_write_byref *drrwbr)
1545{
1546 dmu_tx_t *tx;
1547 int err;
1548 guid_map_entry_t gmesrch;
1549 guid_map_entry_t *gmep;
1550 avl_index_t where;
1551 objset_t *ref_os = NULL;
1552 int flags = DMU_READ_PREFETCH;
1553 dmu_buf_t *dbp;
1554
1555 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
1556 return (SET_ERROR(EINVAL));
1557
1558 /*
1559 * If the GUID of the referenced dataset is different from the
1560 * GUID of the target dataset, find the referenced dataset.
1561 */
1562 if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
1563 gmesrch.guid = drrwbr->drr_refguid;
1564 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
1565 &where)) == NULL) {
1566 return (SET_ERROR(EINVAL));
1567 }
1568 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
1569 return (SET_ERROR(EINVAL));
1570 } else {
1571 ref_os = rwa->os;
1572 }
1573
1574 if (drrwbr->drr_object > rwa->max_object)
1575 rwa->max_object = drrwbr->drr_object;
1576
1577 if (rwa->raw)
1578 flags |= DMU_READ_NO_DECRYPT;
1579
1580 /* may return either a regular db or an encrypted one */
1581 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
1582 drrwbr->drr_refoffset, FTAG, &dbp, flags);
1583 if (err != 0)
1584 return (err);
1585
1586 tx = dmu_tx_create(rwa->os);
1587
1588 dmu_tx_hold_write(tx, drrwbr->drr_object,
1589 drrwbr->drr_offset, drrwbr->drr_length);
1590 err = dmu_tx_assign(tx, TXG_WAIT);
1591 if (err != 0) {
1592 dmu_tx_abort(tx);
1593 return (err);
1594 }
1595
1596 if (rwa->raw) {
1597 dmu_copy_from_buf(rwa->os, drrwbr->drr_object,
1598 drrwbr->drr_offset, dbp, tx);
1599 } else {
1600 dmu_write(rwa->os, drrwbr->drr_object,
1601 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
1602 }
1603 dmu_buf_rele(dbp, FTAG);
1604
1605 /* See comment in restore_write. */
1606 save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
1607 dmu_tx_commit(tx);
1608 return (0);
1609}
1610
1611static int
1612receive_write_embedded(struct receive_writer_arg *rwa,
1613 struct drr_write_embedded *drrwe, void *data)
1614{
1615 dmu_tx_t *tx;
1616 int err;
1617
1618 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
1619 return (SET_ERROR(EINVAL));
1620
1621 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
1622 return (SET_ERROR(EINVAL));
1623
1624 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
1625 return (SET_ERROR(EINVAL));
1626 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
1627 return (SET_ERROR(EINVAL));
1628 if (rwa->raw)
1629 return (SET_ERROR(EINVAL));
1630
1631 if (drrwe->drr_object > rwa->max_object)
1632 rwa->max_object = drrwe->drr_object;
1633
1634 tx = dmu_tx_create(rwa->os);
1635
1636 dmu_tx_hold_write(tx, drrwe->drr_object,
1637 drrwe->drr_offset, drrwe->drr_length);
1638 err = dmu_tx_assign(tx, TXG_WAIT);
1639 if (err != 0) {
1640 dmu_tx_abort(tx);
1641 return (err);
1642 }
1643
1644 dmu_write_embedded(rwa->os, drrwe->drr_object,
1645 drrwe->drr_offset, data, drrwe->drr_etype,
1646 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
1647 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
1648
1649 /* See comment in restore_write. */
1650 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
1651 dmu_tx_commit(tx);
1652 return (0);
1653}
1654
1655static int
1656receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
1657 arc_buf_t *abuf)
1658{
1659 dmu_tx_t *tx;
1660 dmu_buf_t *db, *db_spill;
1661 int err;
1662 uint32_t flags = 0;
1663
1664 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
1665 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
1666 return (SET_ERROR(EINVAL));
1667
1668 if (rwa->raw) {
1669 if (!DMU_OT_IS_VALID(drrs->drr_type) ||
1670 drrs->drr_compressiontype >= ZIO_COMPRESS_FUNCTIONS ||
1671 drrs->drr_compressed_size == 0)
1672 return (SET_ERROR(EINVAL));
1673
1674 flags |= DMU_READ_NO_DECRYPT;
1675 }
1676
1677 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
1678 return (SET_ERROR(EINVAL));
1679
1680 if (drrs->drr_object > rwa->max_object)
1681 rwa->max_object = drrs->drr_object;
1682
1683 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
1684 if ((err = dmu_spill_hold_by_bonus(db, DMU_READ_NO_DECRYPT, FTAG,
1685 &db_spill)) != 0) {
1686 dmu_buf_rele(db, FTAG);
1687 return (err);
1688 }
1689
1690 tx = dmu_tx_create(rwa->os);
1691
1692 dmu_tx_hold_spill(tx, db->db_object);
1693
1694 err = dmu_tx_assign(tx, TXG_WAIT);
1695 if (err != 0) {
1696 dmu_buf_rele(db, FTAG);
1697 dmu_buf_rele(db_spill, FTAG);
1698 dmu_tx_abort(tx);
1699 return (err);
1700 }
1701
1702 if (db_spill->db_size < drrs->drr_length)
1703 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
1704 drrs->drr_length, tx));
1705
1706 if (rwa->byteswap && !arc_is_encrypted(abuf) &&
1707 arc_get_compression(abuf) == ZIO_COMPRESS_OFF) {
1708 dmu_object_byteswap_t byteswap =
1709 DMU_OT_BYTESWAP(drrs->drr_type);
1710 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
1711 DRR_SPILL_PAYLOAD_SIZE(drrs));
1712 }
1713
1714 dbuf_assign_arcbuf((dmu_buf_impl_t *)db_spill, abuf, tx);
1715
1716 dmu_buf_rele(db, FTAG);
1717 dmu_buf_rele(db_spill, FTAG);
1718
1719 dmu_tx_commit(tx);
1720 return (0);
1721}
1722
1723/* ARGSUSED */
1724noinline static int
1725receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
1726{
1727 int err;
1728
1729 if (drrf->drr_length != DMU_OBJECT_END &&
1730 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
1731 return (SET_ERROR(EINVAL));
1732
1733 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
1734 return (SET_ERROR(EINVAL));
1735
1736 if (drrf->drr_object > rwa->max_object)
1737 rwa->max_object = drrf->drr_object;
1738
1739 err = dmu_free_long_range(rwa->os, drrf->drr_object,
1740 drrf->drr_offset, drrf->drr_length);
1741
1742 return (err);
1743}
1744
1745static int
1746receive_object_range(struct receive_writer_arg *rwa,
1747 struct drr_object_range *drror)
1748{
1749 /*
1750 * By default, we assume this block is in our native format
1751 * (ZFS_HOST_BYTEORDER). We then take into account whether
1752 * the send stream is byteswapped (rwa->byteswap). Finally,
1753 * we need to byteswap again if this particular block was
1754 * in non-native format on the send side.
1755 */
1756 boolean_t byteorder = ZFS_HOST_BYTEORDER ^ rwa->byteswap ^
1757 !!DRR_IS_RAW_BYTESWAPPED(drror->drr_flags);
1758
1759 /*
1760 * Since dnode block sizes are constant, we should not need to worry
1761 * about making sure that the dnode block size is the same on the
1762 * sending and receiving sides for the time being. For non-raw sends,
1763 * this does not matter (and in fact we do not send a DRR_OBJECT_RANGE
1764 * record at all). Raw sends require this record type because the
1765 * encryption parameters are used to protect an entire block of bonus
1766 * buffers. If the size of dnode blocks ever becomes variable,
1767 * handling will need to be added to ensure that dnode block sizes
1768 * match on the sending and receiving side.
1769 */
1770 if (drror->drr_numslots != DNODES_PER_BLOCK ||
1771 P2PHASE(drror->drr_firstobj, DNODES_PER_BLOCK) != 0 ||
1772 !rwa->raw)
1773 return (SET_ERROR(EINVAL));
1774
1775 if (drror->drr_firstobj > rwa->max_object)
1776 rwa->max_object = drror->drr_firstobj;
1777
1778 /*
1779 * The DRR_OBJECT_RANGE handling must be deferred to receive_object()
1780 * so that the block of dnodes is not written out when it's empty,
1781 * and converted to a HOLE BP.
1782 */
1783 rwa->or_crypt_params_present = B_TRUE;
1784 rwa->or_firstobj = drror->drr_firstobj;
1785 rwa->or_numslots = drror->drr_numslots;
1786 bcopy(drror->drr_salt, rwa->or_salt, ZIO_DATA_SALT_LEN);
1787 bcopy(drror->drr_iv, rwa->or_iv, ZIO_DATA_IV_LEN);
1788 bcopy(drror->drr_mac, rwa->or_mac, ZIO_DATA_MAC_LEN);
1789 rwa->or_byteorder = byteorder;
1790
1791 return (0);
1792}
1793
1794/* used to destroy the drc_ds on error */
1795static void
1796dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
1797{
1798 dsl_dataset_t *ds = drc->drc_ds;
1799 ds_hold_flags_t dsflags = (drc->drc_raw) ? 0 : DS_HOLD_FLAG_DECRYPT;
1800
1801 /*
1802 * Wait for the txg sync before cleaning up the receive. For
1803 * resumable receives, this ensures that our resume state has
1804 * been written out to disk. For raw receives, this ensures
1805 * that the user accounting code will not attempt to do anything
1806 * after we stopped receiving the dataset.
1807 */
1808 txg_wait_synced(ds->ds_dir->dd_pool, 0);
1809 ds->ds_objset->os_raw_receive = B_FALSE;
1810
1811 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1812 if (drc->drc_resumable && !BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {
1813 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1814 dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
1815 } else {
1816 char name[ZFS_MAX_DATASET_NAME_LEN];
1817 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1818 dsl_dataset_name(ds, name);
1819 dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
1820 (void) dsl_destroy_head(name);
1821 }
1822}
1823
1824static void
1825receive_cksum(struct receive_arg *ra, int len, void *buf)
1826{
1827 if (ra->byteswap) {
1828 (void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
1829 } else {
1830 (void) fletcher_4_incremental_native(buf, len, &ra->cksum);
1831 }
1832}
1833
1834/*
1835 * Read the payload into a buffer of size len, and update the current record's
1836 * payload field.
1837 * Allocate ra->next_rrd and read the next record's header into
1838 * ra->next_rrd->header.
1839 * Verify checksum of payload and next record.
1840 */
1841static int
1842receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
1843{
1844 int err;
1845 zio_cksum_t cksum_orig;
1846 zio_cksum_t *cksump;
1847
1848 if (len != 0) {
1849 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
1850 err = receive_read(ra, len, buf);
1851 if (err != 0)
1852 return (err);
1853 receive_cksum(ra, len, buf);
1854
1855 /* note: rrd is NULL when reading the begin record's payload */
1856 if (ra->rrd != NULL) {
1857 ra->rrd->payload = buf;
1858 ra->rrd->payload_size = len;
1859 ra->rrd->bytes_read = ra->bytes_read;
1860 }
960347d3
TC
1861 } else {
1862 ASSERT3P(buf, ==, NULL);
03916905
PD
1863 }
1864
1865 ra->prev_cksum = ra->cksum;
1866
1867 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
1868 err = receive_read(ra, sizeof (ra->next_rrd->header),
1869 &ra->next_rrd->header);
1870 ra->next_rrd->bytes_read = ra->bytes_read;
1871
1872 if (err != 0) {
1873 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
1874 ra->next_rrd = NULL;
1875 return (err);
1876 }
1877 if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
1878 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
1879 ra->next_rrd = NULL;
1880 return (SET_ERROR(EINVAL));
1881 }
1882
1883 /*
1884 * Note: checksum is of everything up to but not including the
1885 * checksum itself.
1886 */
1887 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
1888 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
1889 receive_cksum(ra,
1890 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
1891 &ra->next_rrd->header);
1892
1893 cksum_orig = ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
1894 cksump = &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
1895
1896 if (ra->byteswap)
1897 byteswap_record(&ra->next_rrd->header);
1898
1899 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
1900 !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
1901 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
1902 ra->next_rrd = NULL;
1903 return (SET_ERROR(ECKSUM));
1904 }
1905
1906 receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
1907
1908 return (0);
1909}
1910
1911static void
1912objlist_create(struct objlist *list)
1913{
1914 list_create(&list->list, sizeof (struct receive_objnode),
1915 offsetof(struct receive_objnode, node));
1916 list->last_lookup = 0;
1917}
1918
1919static void
1920objlist_destroy(struct objlist *list)
1921{
1922 for (struct receive_objnode *n = list_remove_head(&list->list);
1923 n != NULL; n = list_remove_head(&list->list)) {
1924 kmem_free(n, sizeof (*n));
1925 }
1926 list_destroy(&list->list);
1927}
1928
1929/*
1930 * This function looks through the objlist to see if the specified object number
1931 * is contained in the objlist. In the process, it will remove all object
1932 * numbers in the list that are smaller than the specified object number. Thus,
1933 * any lookup of an object number smaller than a previously looked up object
1934 * number will always return false; therefore, all lookups should be done in
1935 * ascending order.
1936 */
1937static boolean_t
1938objlist_exists(struct objlist *list, uint64_t object)
1939{
1940 struct receive_objnode *node = list_head(&list->list);
1941 ASSERT3U(object, >=, list->last_lookup);
1942 list->last_lookup = object;
1943 while (node != NULL && node->object < object) {
1944 VERIFY3P(node, ==, list_remove_head(&list->list));
1945 kmem_free(node, sizeof (*node));
1946 node = list_head(&list->list);
1947 }
1948 return (node != NULL && node->object == object);
1949}
1950
1951/*
1952 * The objlist is a list of object numbers stored in ascending order. However,
1953 * the insertion of new object numbers does not seek out the correct location to
1954 * store a new object number; instead, it appends it to the list for simplicity.
1955 * Thus, any users must take care to only insert new object numbers in ascending
1956 * order.
1957 */
1958static void
1959objlist_insert(struct objlist *list, uint64_t object)
1960{
1961 struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
1962 node->object = object;
1963#ifdef ZFS_DEBUG
1964 {
1965 struct receive_objnode *last_object = list_tail(&list->list);
1966 uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
1967 ASSERT3U(node->object, >, last_objnum);
1968 }
1969#endif
1970 list_insert_tail(&list->list, node);
1971}
1972
1973/*
1974 * Issue the prefetch reads for any necessary indirect blocks.
1975 *
1976 * We use the object ignore list to tell us whether or not to issue prefetches
1977 * for a given object. We do this for both correctness (in case the blocksize
1978 * of an object has changed) and performance (if the object doesn't exist, don't
1979 * needlessly try to issue prefetches). We also trim the list as we go through
1980 * the stream to prevent it from growing to an unbounded size.
1981 *
1982 * The object numbers within will always be in sorted order, and any write
1983 * records we see will also be in sorted order, but they're not sorted with
1984 * respect to each other (i.e. we can get several object records before
1985 * receiving each object's write records). As a result, once we've reached a
1986 * given object number, we can safely remove any reference to lower object
1987 * numbers in the ignore list. In practice, we receive up to 32 object records
1988 * before receiving write records, so the list can have up to 32 nodes in it.
1989 */
1990/* ARGSUSED */
1991static void
1992receive_read_prefetch(struct receive_arg *ra,
1993 uint64_t object, uint64_t offset, uint64_t length)
1994{
1995 if (!objlist_exists(&ra->ignore_objlist, object)) {
1996 dmu_prefetch(ra->os, object, 1, offset, length,
1997 ZIO_PRIORITY_SYNC_READ);
1998 }
1999}
2000
2001/*
2002 * Read records off the stream, issuing any necessary prefetches.
2003 */
2004static int
2005receive_read_record(struct receive_arg *ra)
2006{
2007 int err;
2008
2009 switch (ra->rrd->header.drr_type) {
2010 case DRR_OBJECT:
2011 {
2012 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2013 uint32_t size = DRR_OBJECT_PAYLOAD_SIZE(drro);
960347d3 2014 void *buf = NULL;
03916905
PD
2015 dmu_object_info_t doi;
2016
960347d3
TC
2017 if (size != 0)
2018 buf = kmem_zalloc(size, KM_SLEEP);
2019
03916905
PD
2020 err = receive_read_payload_and_next_header(ra, size, buf);
2021 if (err != 0) {
2022 kmem_free(buf, size);
2023 return (err);
2024 }
2025 err = dmu_object_info(ra->os, drro->drr_object, &doi);
2026 /*
2027 * See receive_read_prefetch for an explanation why we're
2028 * storing this object in the ignore_obj_list.
2029 */
2030 if (err == ENOENT || err == EEXIST ||
2031 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2032 objlist_insert(&ra->ignore_objlist, drro->drr_object);
2033 err = 0;
2034 }
2035 return (err);
2036 }
2037 case DRR_FREEOBJECTS:
2038 {
2039 err = receive_read_payload_and_next_header(ra, 0, NULL);
2040 return (err);
2041 }
2042 case DRR_WRITE:
2043 {
2044 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2045 arc_buf_t *abuf;
2046 boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
2047
2048 if (ra->raw) {
2049 boolean_t byteorder = ZFS_HOST_BYTEORDER ^
2050 !!DRR_IS_RAW_BYTESWAPPED(drrw->drr_flags) ^
2051 ra->byteswap;
2052
2053 abuf = arc_loan_raw_buf(dmu_objset_spa(ra->os),
2054 drrw->drr_object, byteorder, drrw->drr_salt,
2055 drrw->drr_iv, drrw->drr_mac, drrw->drr_type,
2056 drrw->drr_compressed_size, drrw->drr_logical_size,
2057 drrw->drr_compressiontype);
2058 } else if (DRR_WRITE_COMPRESSED(drrw)) {
2059 ASSERT3U(drrw->drr_compressed_size, >, 0);
2060 ASSERT3U(drrw->drr_logical_size, >=,
2061 drrw->drr_compressed_size);
2062 ASSERT(!is_meta);
2063 abuf = arc_loan_compressed_buf(
2064 dmu_objset_spa(ra->os),
2065 drrw->drr_compressed_size, drrw->drr_logical_size,
2066 drrw->drr_compressiontype);
2067 } else {
2068 abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2069 is_meta, drrw->drr_logical_size);
2070 }
2071
2072 err = receive_read_payload_and_next_header(ra,
2073 DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
2074 if (err != 0) {
2075 dmu_return_arcbuf(abuf);
2076 return (err);
2077 }
2078 ra->rrd->arc_buf = abuf;
2079 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2080 drrw->drr_logical_size);
2081 return (err);
2082 }
2083 case DRR_WRITE_BYREF:
2084 {
2085 struct drr_write_byref *drrwb =
2086 &ra->rrd->header.drr_u.drr_write_byref;
2087 err = receive_read_payload_and_next_header(ra, 0, NULL);
2088 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2089 drrwb->drr_length);
2090 return (err);
2091 }
2092 case DRR_WRITE_EMBEDDED:
2093 {
2094 struct drr_write_embedded *drrwe =
2095 &ra->rrd->header.drr_u.drr_write_embedded;
2096 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2097 void *buf = kmem_zalloc(size, KM_SLEEP);
2098
2099 err = receive_read_payload_and_next_header(ra, size, buf);
2100 if (err != 0) {
2101 kmem_free(buf, size);
2102 return (err);
2103 }
2104
2105 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2106 drrwe->drr_length);
2107 return (err);
2108 }
2109 case DRR_FREE:
2110 {
2111 /*
2112 * It might be beneficial to prefetch indirect blocks here, but
2113 * we don't really have the data to decide for sure.
2114 */
2115 err = receive_read_payload_and_next_header(ra, 0, NULL);
2116 return (err);
2117 }
2118 case DRR_END:
2119 {
2120 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
2121 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2122 return (SET_ERROR(ECKSUM));
2123 return (0);
2124 }
2125 case DRR_SPILL:
2126 {
2127 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2128 arc_buf_t *abuf;
2129 int len = DRR_SPILL_PAYLOAD_SIZE(drrs);
2130
2131 /* DRR_SPILL records are either raw or uncompressed */
2132 if (ra->raw) {
2133 boolean_t byteorder = ZFS_HOST_BYTEORDER ^
2134 !!DRR_IS_RAW_BYTESWAPPED(drrs->drr_flags) ^
2135 ra->byteswap;
2136
2137 abuf = arc_loan_raw_buf(dmu_objset_spa(ra->os),
2138 dmu_objset_id(ra->os), byteorder, drrs->drr_salt,
2139 drrs->drr_iv, drrs->drr_mac, drrs->drr_type,
2140 drrs->drr_compressed_size, drrs->drr_length,
2141 drrs->drr_compressiontype);
2142 } else {
2143 abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2144 DMU_OT_IS_METADATA(drrs->drr_type),
2145 drrs->drr_length);
2146 }
2147
2148 err = receive_read_payload_and_next_header(ra, len,
2149 abuf->b_data);
2150 if (err != 0) {
2151 dmu_return_arcbuf(abuf);
2152 return (err);
2153 }
2154 ra->rrd->arc_buf = abuf;
2155 return (err);
2156 }
2157 case DRR_OBJECT_RANGE:
2158 {
2159 err = receive_read_payload_and_next_header(ra, 0, NULL);
2160 return (err);
2161 }
2162 default:
2163 return (SET_ERROR(EINVAL));
2164 }
2165}
2166
2167static void
2168dprintf_drr(struct receive_record_arg *rrd, int err)
2169{
2170#ifdef ZFS_DEBUG
2171 switch (rrd->header.drr_type) {
2172 case DRR_OBJECT:
2173 {
2174 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2175 dprintf("drr_type = OBJECT obj = %llu type = %u "
2176 "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
2177 "compress = %u dn_slots = %u err = %d\n",
2178 drro->drr_object, drro->drr_type, drro->drr_bonustype,
2179 drro->drr_blksz, drro->drr_bonuslen,
2180 drro->drr_checksumtype, drro->drr_compress,
2181 drro->drr_dn_slots, err);
2182 break;
2183 }
2184 case DRR_FREEOBJECTS:
2185 {
2186 struct drr_freeobjects *drrfo =
2187 &rrd->header.drr_u.drr_freeobjects;
2188 dprintf("drr_type = FREEOBJECTS firstobj = %llu "
2189 "numobjs = %llu err = %d\n",
2190 drrfo->drr_firstobj, drrfo->drr_numobjs, err);
2191 break;
2192 }
2193 case DRR_WRITE:
2194 {
2195 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2196 dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
5dbf8b4e 2197 "lsize = %llu cksumtype = %u flags = %u "
03916905
PD
2198 "compress = %u psize = %llu err = %d\n",
2199 drrw->drr_object, drrw->drr_type, drrw->drr_offset,
2200 drrw->drr_logical_size, drrw->drr_checksumtype,
2201 drrw->drr_flags, drrw->drr_compressiontype,
2202 drrw->drr_compressed_size, err);
2203 break;
2204 }
2205 case DRR_WRITE_BYREF:
2206 {
2207 struct drr_write_byref *drrwbr =
2208 &rrd->header.drr_u.drr_write_byref;
2209 dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
2210 "length = %llu toguid = %llx refguid = %llx "
2211 "refobject = %llu refoffset = %llu cksumtype = %u "
5dbf8b4e 2212 "flags = %u err = %d\n",
03916905
PD
2213 drrwbr->drr_object, drrwbr->drr_offset,
2214 drrwbr->drr_length, drrwbr->drr_toguid,
2215 drrwbr->drr_refguid, drrwbr->drr_refobject,
2216 drrwbr->drr_refoffset, drrwbr->drr_checksumtype,
2217 drrwbr->drr_flags, err);
2218 break;
2219 }
2220 case DRR_WRITE_EMBEDDED:
2221 {
2222 struct drr_write_embedded *drrwe =
2223 &rrd->header.drr_u.drr_write_embedded;
2224 dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
2225 "length = %llu compress = %u etype = %u lsize = %u "
2226 "psize = %u err = %d\n",
2227 drrwe->drr_object, drrwe->drr_offset, drrwe->drr_length,
2228 drrwe->drr_compression, drrwe->drr_etype,
2229 drrwe->drr_lsize, drrwe->drr_psize, err);
2230 break;
2231 }
2232 case DRR_FREE:
2233 {
2234 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2235 dprintf("drr_type = FREE obj = %llu offset = %llu "
2236 "length = %lld err = %d\n",
2237 drrf->drr_object, drrf->drr_offset, drrf->drr_length,
2238 err);
2239 break;
2240 }
2241 case DRR_SPILL:
2242 {
2243 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2244 dprintf("drr_type = SPILL obj = %llu length = %llu "
2245 "err = %d\n", drrs->drr_object, drrs->drr_length, err);
2246 break;
2247 }
5dbf8b4e
TC
2248 case DRR_OBJECT_RANGE:
2249 {
2250 struct drr_object_range *drror =
2251 &rrd->header.drr_u.drr_object_range;
2252 dprintf("drr_type = OBJECT_RANGE firstobj = %llu "
2253 "numslots = %llu flags = %u err = %d\n",
2254 drror->drr_firstobj, drror->drr_numslots,
2255 drror->drr_flags, err);
2256 break;
2257 }
03916905
PD
2258 default:
2259 return;
2260 }
2261#endif
2262}
2263
2264/*
2265 * Commit the records to the pool.
2266 */
2267static int
2268receive_process_record(struct receive_writer_arg *rwa,
2269 struct receive_record_arg *rrd)
2270{
2271 int err;
2272
2273 /* Processing in order, therefore bytes_read should be increasing. */
2274 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2275 rwa->bytes_read = rrd->bytes_read;
2276
2277 switch (rrd->header.drr_type) {
2278 case DRR_OBJECT:
2279 {
2280 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2281 err = receive_object(rwa, drro, rrd->payload);
2282 kmem_free(rrd->payload, rrd->payload_size);
2283 rrd->payload = NULL;
2284 break;
2285 }
2286 case DRR_FREEOBJECTS:
2287 {
2288 struct drr_freeobjects *drrfo =
2289 &rrd->header.drr_u.drr_freeobjects;
2290 err = receive_freeobjects(rwa, drrfo);
2291 break;
2292 }
2293 case DRR_WRITE:
2294 {
2295 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2296 err = receive_write(rwa, drrw, rrd->arc_buf);
2297 /* if receive_write() is successful, it consumes the arc_buf */
2298 if (err != 0)
2299 dmu_return_arcbuf(rrd->arc_buf);
2300 rrd->arc_buf = NULL;
2301 rrd->payload = NULL;
2302 break;
2303 }
2304 case DRR_WRITE_BYREF:
2305 {
2306 struct drr_write_byref *drrwbr =
2307 &rrd->header.drr_u.drr_write_byref;
2308 err = receive_write_byref(rwa, drrwbr);
2309 break;
2310 }
2311 case DRR_WRITE_EMBEDDED:
2312 {
2313 struct drr_write_embedded *drrwe =
2314 &rrd->header.drr_u.drr_write_embedded;
2315 err = receive_write_embedded(rwa, drrwe, rrd->payload);
2316 kmem_free(rrd->payload, rrd->payload_size);
2317 rrd->payload = NULL;
2318 break;
2319 }
2320 case DRR_FREE:
2321 {
2322 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2323 err = receive_free(rwa, drrf);
2324 break;
2325 }
2326 case DRR_SPILL:
2327 {
2328 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2329 err = receive_spill(rwa, drrs, rrd->arc_buf);
2330 /* if receive_spill() is successful, it consumes the arc_buf */
2331 if (err != 0)
2332 dmu_return_arcbuf(rrd->arc_buf);
2333 rrd->arc_buf = NULL;
2334 rrd->payload = NULL;
2335 break;
2336 }
2337 case DRR_OBJECT_RANGE:
2338 {
2339 struct drr_object_range *drror =
2340 &rrd->header.drr_u.drr_object_range;
5dbf8b4e
TC
2341 err = receive_object_range(rwa, drror);
2342 break;
03916905
PD
2343 }
2344 default:
5dbf8b4e 2345 err = (SET_ERROR(EINVAL));
03916905
PD
2346 }
2347
2348 if (err != 0)
2349 dprintf_drr(rrd, err);
2350
2351 return (err);
2352}
2353
2354/*
2355 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2356 * receive_process_record When we're done, signal the main thread and exit.
2357 */
2358static void
2359receive_writer_thread(void *arg)
2360{
2361 struct receive_writer_arg *rwa = arg;
2362 struct receive_record_arg *rrd;
2363 fstrans_cookie_t cookie = spl_fstrans_mark();
2364
2365 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2366 rrd = bqueue_dequeue(&rwa->q)) {
2367 /*
2368 * If there's an error, the main thread will stop putting things
2369 * on the queue, but we need to clear everything in it before we
2370 * can exit.
2371 */
2372 if (rwa->err == 0) {
2373 rwa->err = receive_process_record(rwa, rrd);
2374 } else if (rrd->arc_buf != NULL) {
2375 dmu_return_arcbuf(rrd->arc_buf);
2376 rrd->arc_buf = NULL;
2377 rrd->payload = NULL;
2378 } else if (rrd->payload != NULL) {
2379 kmem_free(rrd->payload, rrd->payload_size);
2380 rrd->payload = NULL;
2381 }
2382 kmem_free(rrd, sizeof (*rrd));
2383 }
2384 kmem_free(rrd, sizeof (*rrd));
2385 mutex_enter(&rwa->mutex);
2386 rwa->done = B_TRUE;
2387 cv_signal(&rwa->cv);
2388 mutex_exit(&rwa->mutex);
2389 spl_fstrans_unmark(cookie);
2390 thread_exit();
2391}
2392
2393static int
2394resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
2395{
2396 uint64_t val;
2397 objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
2398 uint64_t dsobj = dmu_objset_id(ra->os);
2399 uint64_t resume_obj, resume_off;
2400
2401 if (nvlist_lookup_uint64(begin_nvl,
2402 "resume_object", &resume_obj) != 0 ||
2403 nvlist_lookup_uint64(begin_nvl,
2404 "resume_offset", &resume_off) != 0) {
2405 return (SET_ERROR(EINVAL));
2406 }
2407 VERIFY0(zap_lookup(mos, dsobj,
2408 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
2409 if (resume_obj != val)
2410 return (SET_ERROR(EINVAL));
2411 VERIFY0(zap_lookup(mos, dsobj,
2412 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
2413 if (resume_off != val)
2414 return (SET_ERROR(EINVAL));
2415
2416 return (0);
2417}
2418
2419/*
2420 * Read in the stream's records, one by one, and apply them to the pool. There
2421 * are two threads involved; the thread that calls this function will spin up a
2422 * worker thread, read the records off the stream one by one, and issue
2423 * prefetches for any necessary indirect blocks. It will then push the records
2424 * onto an internal blocking queue. The worker thread will pull the records off
2425 * the queue, and actually write the data into the DMU. This way, the worker
2426 * thread doesn't have to wait for reads to complete, since everything it needs
2427 * (the indirect blocks) will be prefetched.
2428 *
2429 * NB: callers *must* call dmu_recv_end() if this succeeds.
2430 */
2431int
2432dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
2433 int cleanup_fd, uint64_t *action_handlep)
2434{
2435 int err = 0;
2436 struct receive_arg *ra;
2437 struct receive_writer_arg *rwa;
2438 int featureflags;
2439 uint32_t payloadlen;
2440 void *payload;
2441 nvlist_t *begin_nvl = NULL;
2442
2443 ra = kmem_zalloc(sizeof (*ra), KM_SLEEP);
2444 rwa = kmem_zalloc(sizeof (*rwa), KM_SLEEP);
2445
2446 ra->byteswap = drc->drc_byteswap;
2447 ra->raw = drc->drc_raw;
2448 ra->cksum = drc->drc_cksum;
2449 ra->vp = vp;
2450 ra->voff = *voffp;
2451
2452 if (dsl_dataset_is_zapified(drc->drc_ds)) {
2453 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
2454 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
2455 sizeof (ra->bytes_read), 1, &ra->bytes_read);
2456 }
2457
2458 objlist_create(&ra->ignore_objlist);
2459
2460 /* these were verified in dmu_recv_begin */
2461 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
2462 DMU_SUBSTREAM);
2463 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2464
2465 /*
2466 * Open the objset we are modifying.
2467 */
2468 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra->os));
2469
2470 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2471
2472 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
2473 ra->featureflags = featureflags;
2474
2475 ASSERT0(ra->os->os_encrypted &&
2476 (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA));
2477
2478 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2479 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2480 minor_t minor;
2481
2482 if (cleanup_fd == -1) {
2483 err = SET_ERROR(EBADF);
2484 goto out;
2485 }
2486 err = zfs_onexit_fd_hold(cleanup_fd, &minor);
2487 if (err != 0) {
2488 cleanup_fd = -1;
2489 goto out;
2490 }
2491
2492 if (*action_handlep == 0) {
2493 rwa->guid_to_ds_map =
2494 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2495 avl_create(rwa->guid_to_ds_map, guid_compare,
2496 sizeof (guid_map_entry_t),
2497 offsetof(guid_map_entry_t, avlnode));
2498 err = zfs_onexit_add_cb(minor,
2499 free_guid_map_onexit, rwa->guid_to_ds_map,
2500 action_handlep);
2501 if (err != 0)
2502 goto out;
2503 } else {
2504 err = zfs_onexit_cb_data(minor, *action_handlep,
2505 (void **)&rwa->guid_to_ds_map);
2506 if (err != 0)
2507 goto out;
2508 }
2509
2510 drc->drc_guid_to_ds_map = rwa->guid_to_ds_map;
2511 }
2512
2513 payloadlen = drc->drc_drr_begin->drr_payloadlen;
2514 payload = NULL;
2515 if (payloadlen != 0)
2516 payload = kmem_alloc(payloadlen, KM_SLEEP);
2517
2518 err = receive_read_payload_and_next_header(ra, payloadlen, payload);
2519 if (err != 0) {
2520 if (payloadlen != 0)
2521 kmem_free(payload, payloadlen);
2522 goto out;
2523 }
2524 if (payloadlen != 0) {
2525 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
2526 kmem_free(payload, payloadlen);
2527 if (err != 0)
2528 goto out;
2529 }
2530
2531 /* handle DSL encryption key payload */
2532 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
2533 nvlist_t *keynvl = NULL;
2534
2535 ASSERT(ra->os->os_encrypted);
2536 ASSERT(drc->drc_raw);
2537
2538 err = nvlist_lookup_nvlist(begin_nvl, "crypt_keydata", &keynvl);
2539 if (err != 0)
2540 goto out;
2541
2542 /*
2543 * If this is a new dataset we set the key immediately.
2544 * Otherwise we don't want to change the key until we
2545 * are sure the rest of the receive succeeded so we stash
2546 * the keynvl away until then.
2547 */
2548 err = dsl_crypto_recv_raw(spa_name(ra->os->os_spa),
f00ab3f2
TC
2549 drc->drc_ds->ds_object, drc->drc_fromsnapobj,
2550 drc->drc_drrb->drr_type, keynvl, drc->drc_newfs);
03916905
PD
2551 if (err != 0)
2552 goto out;
2553
f00ab3f2
TC
2554 /* see comment in dmu_recv_end_sync() */
2555 drc->drc_ivset_guid = 0;
2556 (void) nvlist_lookup_uint64(keynvl, "to_ivset_guid",
2557 &drc->drc_ivset_guid);
2558
03916905
PD
2559 if (!drc->drc_newfs)
2560 drc->drc_keynvl = fnvlist_dup(keynvl);
2561 }
2562
2563 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
2564 err = resume_check(ra, begin_nvl);
2565 if (err != 0)
2566 goto out;
2567 }
2568
2569 (void) bqueue_init(&rwa->q,
2570 MAX(zfs_recv_queue_length, 2 * zfs_max_recordsize),
2571 offsetof(struct receive_record_arg, node));
2572 cv_init(&rwa->cv, NULL, CV_DEFAULT, NULL);
2573 mutex_init(&rwa->mutex, NULL, MUTEX_DEFAULT, NULL);
2574 rwa->os = ra->os;
2575 rwa->byteswap = drc->drc_byteswap;
2576 rwa->resumable = drc->drc_resumable;
2577 rwa->raw = drc->drc_raw;
2578 rwa->os->os_raw_receive = drc->drc_raw;
2579
2580 (void) thread_create(NULL, 0, receive_writer_thread, rwa, 0, curproc,
2581 TS_RUN, minclsyspri);
2582 /*
2583 * We're reading rwa->err without locks, which is safe since we are the
2584 * only reader, and the worker thread is the only writer. It's ok if we
2585 * miss a write for an iteration or two of the loop, since the writer
2586 * thread will keep freeing records we send it until we send it an eos
2587 * marker.
2588 *
2589 * We can leave this loop in 3 ways: First, if rwa->err is
2590 * non-zero. In that case, the writer thread will free the rrd we just
2591 * pushed. Second, if we're interrupted; in that case, either it's the
2592 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
2593 * has been handed off to the writer thread who will free it. Finally,
2594 * if receive_read_record fails or we're at the end of the stream, then
2595 * we free ra->rrd and exit.
2596 */
2597 while (rwa->err == 0) {
2598 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2599 err = SET_ERROR(EINTR);
2600 break;
2601 }
2602
2603 ASSERT3P(ra->rrd, ==, NULL);
2604 ra->rrd = ra->next_rrd;
2605 ra->next_rrd = NULL;
2606 /* Allocates and loads header into ra->next_rrd */
2607 err = receive_read_record(ra);
2608
2609 if (ra->rrd->header.drr_type == DRR_END || err != 0) {
2610 kmem_free(ra->rrd, sizeof (*ra->rrd));
2611 ra->rrd = NULL;
2612 break;
2613 }
2614
2615 bqueue_enqueue(&rwa->q, ra->rrd,
2616 sizeof (struct receive_record_arg) + ra->rrd->payload_size);
2617 ra->rrd = NULL;
2618 }
f00ab3f2
TC
2619 ASSERT3P(ra->rrd, ==, NULL);
2620 ra->rrd = kmem_zalloc(sizeof (*ra->rrd), KM_SLEEP);
2621 ra->rrd->eos_marker = B_TRUE;
2622 bqueue_enqueue(&rwa->q, ra->rrd, 1);
03916905
PD
2623
2624 mutex_enter(&rwa->mutex);
2625 while (!rwa->done) {
2626 cv_wait(&rwa->cv, &rwa->mutex);
2627 }
2628 mutex_exit(&rwa->mutex);
2629
2630 /*
2631 * If we are receiving a full stream as a clone, all object IDs which
2632 * are greater than the maximum ID referenced in the stream are
2633 * by definition unused and must be freed.
2634 */
2635 if (drc->drc_clone && drc->drc_drrb->drr_fromguid == 0) {
2636 uint64_t obj = rwa->max_object + 1;
2637 int free_err = 0;
2638 int next_err = 0;
2639
2640 while (next_err == 0) {
2641 free_err = dmu_free_long_object(rwa->os, obj);
2642 if (free_err != 0 && free_err != ENOENT)
2643 break;
2644
2645 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0);
2646 }
2647
2648 if (err == 0) {
2649 if (free_err != 0 && free_err != ENOENT)
2650 err = free_err;
2651 else if (next_err != ESRCH)
2652 err = next_err;
2653 }
2654 }
2655
2656 cv_destroy(&rwa->cv);
2657 mutex_destroy(&rwa->mutex);
2658 bqueue_destroy(&rwa->q);
2659 if (err == 0)
2660 err = rwa->err;
2661
2662out:
f00ab3f2
TC
2663 /*
2664 * If we hit an error before we started the receive_writer_thread
2665 * we need to clean up the next_rrd we create by processing the
2666 * DRR_BEGIN record.
2667 */
2668 if (ra->next_rrd != NULL)
2669 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2670
03916905
PD
2671 nvlist_free(begin_nvl);
2672 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2673 zfs_onexit_fd_rele(cleanup_fd);
2674
2675 if (err != 0) {
2676 /*
2677 * Clean up references. If receive is not resumable,
2678 * destroy what we created, so we don't leave it in
2679 * the inconsistent state.
2680 */
2681 dmu_recv_cleanup_ds(drc);
2682 nvlist_free(drc->drc_keynvl);
2683 }
2684
2685 *voffp = ra->voff;
2686 objlist_destroy(&ra->ignore_objlist);
2687 kmem_free(ra, sizeof (*ra));
2688 kmem_free(rwa, sizeof (*rwa));
2689 return (err);
2690}
2691
2692static int
2693dmu_recv_end_check(void *arg, dmu_tx_t *tx)
2694{
2695 dmu_recv_cookie_t *drc = arg;
2696 dsl_pool_t *dp = dmu_tx_pool(tx);
2697 int error;
2698
2699 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
2700
2701 if (!drc->drc_newfs) {
2702 dsl_dataset_t *origin_head;
2703
2704 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
2705 if (error != 0)
2706 return (error);
2707 if (drc->drc_force) {
2708 /*
2709 * We will destroy any snapshots in tofs (i.e. before
2710 * origin_head) that are after the origin (which is
2711 * the snap before drc_ds, because drc_ds can not
2712 * have any snaps of its own).
2713 */
2714 uint64_t obj;
2715
2716 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2717 while (obj !=
2718 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
2719 dsl_dataset_t *snap;
2720 error = dsl_dataset_hold_obj(dp, obj, FTAG,
2721 &snap);
2722 if (error != 0)
2723 break;
2724 if (snap->ds_dir != origin_head->ds_dir)
2725 error = SET_ERROR(EINVAL);
2726 if (error == 0) {
2727 error = dsl_destroy_snapshot_check_impl(
2728 snap, B_FALSE);
2729 }
2730 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
2731 dsl_dataset_rele(snap, FTAG);
2732 if (error != 0)
2733 break;
2734 }
2735 if (error != 0) {
2736 dsl_dataset_rele(origin_head, FTAG);
2737 return (error);
2738 }
2739 }
2740 if (drc->drc_keynvl != NULL) {
2741 error = dsl_crypto_recv_raw_key_check(drc->drc_ds,
2742 drc->drc_keynvl, tx);
2743 if (error != 0) {
2744 dsl_dataset_rele(origin_head, FTAG);
2745 return (error);
2746 }
2747 }
2748
2749 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
2750 origin_head, drc->drc_force, drc->drc_owner, tx);
2751 if (error != 0) {
2752 dsl_dataset_rele(origin_head, FTAG);
2753 return (error);
2754 }
2755 error = dsl_dataset_snapshot_check_impl(origin_head,
2756 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
2757 dsl_dataset_rele(origin_head, FTAG);
2758 if (error != 0)
2759 return (error);
2760
2761 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
2762 } else {
2763 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
2764 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
2765 }
2766 return (error);
2767}
2768
2769static void
2770dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
2771{
2772 dmu_recv_cookie_t *drc = arg;
2773 dsl_pool_t *dp = dmu_tx_pool(tx);
2774 boolean_t encrypted = drc->drc_ds->ds_dir->dd_crypto_obj != 0;
2775
2776 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
2777 tx, "snap=%s", drc->drc_tosnap);
2778 drc->drc_ds->ds_objset->os_raw_receive = B_FALSE;
2779
2780 if (!drc->drc_newfs) {
2781 dsl_dataset_t *origin_head;
2782
2783 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
2784 &origin_head));
2785
2786 if (drc->drc_force) {
2787 /*
2788 * Destroy any snapshots of drc_tofs (origin_head)
2789 * after the origin (the snap before drc_ds).
2790 */
2791 uint64_t obj;
2792
2793 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2794 while (obj !=
2795 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
2796 dsl_dataset_t *snap;
2797 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
2798 &snap));
2799 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
2800 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
2801 dsl_destroy_snapshot_sync_impl(snap,
2802 B_FALSE, tx);
2803 dsl_dataset_rele(snap, FTAG);
2804 }
2805 }
2806 if (drc->drc_keynvl != NULL) {
2807 dsl_crypto_recv_raw_key_sync(drc->drc_ds,
2808 drc->drc_keynvl, tx);
2809 nvlist_free(drc->drc_keynvl);
2810 drc->drc_keynvl = NULL;
2811 }
2812
2813 VERIFY3P(drc->drc_ds->ds_prev, ==, origin_head->ds_prev);
2814
2815 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
2816 origin_head, tx);
2817 dsl_dataset_snapshot_sync_impl(origin_head,
2818 drc->drc_tosnap, tx);
2819
2820 /* set snapshot's creation time and guid */
2821 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
2822 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
2823 drc->drc_drrb->drr_creation_time;
2824 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
2825 drc->drc_drrb->drr_toguid;
2826 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
2827 ~DS_FLAG_INCONSISTENT;
2828
2829 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2830 dsl_dataset_phys(origin_head)->ds_flags &=
2831 ~DS_FLAG_INCONSISTENT;
2832
2833 drc->drc_newsnapobj =
2834 dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2835
2836 dsl_dataset_rele(origin_head, FTAG);
2837 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
2838
2839 if (drc->drc_owner != NULL)
2840 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
2841 } else {
2842 dsl_dataset_t *ds = drc->drc_ds;
2843
2844 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
2845
2846 /* set snapshot's creation time and guid */
2847 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
2848 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
2849 drc->drc_drrb->drr_creation_time;
2850 dsl_dataset_phys(ds->ds_prev)->ds_guid =
2851 drc->drc_drrb->drr_toguid;
2852 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
2853 ~DS_FLAG_INCONSISTENT;
2854
2855 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2856 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
2857 if (dsl_dataset_has_resume_receive_state(ds)) {
2858 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2859 DS_FIELD_RESUME_FROMGUID, tx);
2860 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2861 DS_FIELD_RESUME_OBJECT, tx);
2862 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2863 DS_FIELD_RESUME_OFFSET, tx);
2864 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2865 DS_FIELD_RESUME_BYTES, tx);
2866 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2867 DS_FIELD_RESUME_TOGUID, tx);
2868 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2869 DS_FIELD_RESUME_TONAME, tx);
2870 }
2871 drc->drc_newsnapobj =
2872 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
2873 }
f00ab3f2
TC
2874
2875 /*
2876 * If this is a raw receive, the crypt_keydata nvlist will include
2877 * a to_ivset_guid for us to set on the new snapshot. This value
2878 * will override the value generated by the snapshot code. However,
2879 * this value may not be present, because older implementations of
2880 * the raw send code did not include this value, and we are still
2881 * allowed to receive them if the zfs_disable_ivset_guid_check
2882 * tunable is set, in which case we will leave the newly-generated
2883 * value.
2884 */
2885 if (drc->drc_raw && drc->drc_ivset_guid != 0) {
2886 dmu_object_zapify(dp->dp_meta_objset, drc->drc_newsnapobj,
2887 DMU_OT_DSL_DATASET, tx);
2888 VERIFY0(zap_update(dp->dp_meta_objset, drc->drc_newsnapobj,
2889 DS_FIELD_IVSET_GUID, sizeof (uint64_t), 1,
2890 &drc->drc_ivset_guid, tx));
2891 }
2892
03916905
PD
2893 zvol_create_minors(dp->dp_spa, drc->drc_tofs, B_TRUE);
2894
2895 /*
2896 * Release the hold from dmu_recv_begin. This must be done before
2897 * we return to open context, so that when we free the dataset's dnode
2898 * we can evict its bonus buffer. Since the dataset may be destroyed
2899 * at this point (and therefore won't have a valid pointer to the spa)
2900 * we release the key mapping manually here while we do have a valid
2901 * pointer, if it exists.
2902 */
2903 if (!drc->drc_raw && encrypted) {
2904 (void) spa_keystore_remove_mapping(dmu_tx_pool(tx)->dp_spa,
2905 drc->drc_ds->ds_object, drc->drc_ds);
2906 }
2907 dsl_dataset_disown(drc->drc_ds, 0, dmu_recv_tag);
2908 drc->drc_ds = NULL;
2909}
2910
2911static int
2912add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj,
2913 boolean_t raw)
2914{
2915 dsl_pool_t *dp;
2916 dsl_dataset_t *snapds;
2917 guid_map_entry_t *gmep;
2918 objset_t *os;
2919 ds_hold_flags_t dsflags = (raw) ? 0 : DS_HOLD_FLAG_DECRYPT;
2920 int err;
2921
2922 ASSERT(guid_map != NULL);
2923
2924 err = dsl_pool_hold(name, FTAG, &dp);
2925 if (err != 0)
2926 return (err);
2927 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
2928 err = dsl_dataset_own_obj(dp, snapobj, dsflags, gmep, &snapds);
2929 if (err == 0) {
2930 /*
2931 * If this is a deduplicated raw send stream, we need
2932 * to make sure that we can still read raw blocks from
2933 * earlier datasets in the stream, so we set the
2934 * os_raw_receive flag now.
2935 */
2936 if (raw) {
2937 err = dmu_objset_from_ds(snapds, &os);
2938 if (err != 0) {
2939 dsl_dataset_disown(snapds, dsflags, FTAG);
2940 dsl_pool_rele(dp, FTAG);
2941 kmem_free(gmep, sizeof (*gmep));
2942 return (err);
2943 }
2944 os->os_raw_receive = B_TRUE;
2945 }
2946
2947 gmep->raw = raw;
2948 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
2949 gmep->gme_ds = snapds;
2950 avl_add(guid_map, gmep);
2951 } else {
2952 kmem_free(gmep, sizeof (*gmep));
2953 }
2954
2955 dsl_pool_rele(dp, FTAG);
2956 return (err);
2957}
2958
2959static int dmu_recv_end_modified_blocks = 3;
2960
2961static int
2962dmu_recv_existing_end(dmu_recv_cookie_t *drc)
2963{
2964#ifdef _KERNEL
2965 /*
2966 * We will be destroying the ds; make sure its origin is unmounted if
2967 * necessary.
2968 */
2969 char name[ZFS_MAX_DATASET_NAME_LEN];
2970 dsl_dataset_name(drc->drc_ds, name);
2971 zfs_destroy_unmount_origin(name);
2972#endif
2973
2974 return (dsl_sync_task(drc->drc_tofs,
2975 dmu_recv_end_check, dmu_recv_end_sync, drc,
2976 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
2977}
2978
2979static int
2980dmu_recv_new_end(dmu_recv_cookie_t *drc)
2981{
2982 return (dsl_sync_task(drc->drc_tofs,
2983 dmu_recv_end_check, dmu_recv_end_sync, drc,
2984 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
2985}
2986
2987int
2988dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
2989{
2990 int error;
2991
2992 drc->drc_owner = owner;
2993
2994 if (drc->drc_newfs)
2995 error = dmu_recv_new_end(drc);
2996 else
2997 error = dmu_recv_existing_end(drc);
2998
2999 if (error != 0) {
3000 dmu_recv_cleanup_ds(drc);
3001 nvlist_free(drc->drc_keynvl);
3002 } else if (drc->drc_guid_to_ds_map != NULL) {
3003 (void) add_ds_to_guidmap(drc->drc_tofs, drc->drc_guid_to_ds_map,
3004 drc->drc_newsnapobj, drc->drc_raw);
3005 }
3006 return (error);
3007}
3008
3009/*
3010 * Return TRUE if this objset is currently being received into.
3011 */
3012boolean_t
3013dmu_objset_is_receiving(objset_t *os)
3014{
3015 return (os->os_dsl_dataset != NULL &&
3016 os->os_dsl_dataset->ds_owner == dmu_recv_tag);
3017}
3018
3019#if defined(_KERNEL)
3020module_param(zfs_recv_queue_length, int, 0644);
3021MODULE_PARM_DESC(zfs_recv_queue_length, "Maximum receive queue length");
3022#endif