]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dmu_recv.c
Revert "Fix issues with truncated files in raw sends"
[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);
1179
1180 object = drro->drr_object;
1181
369aa501 1182 /* nblkptr should be bounded by the bonus size and type */
03916905
PD
1183 if (rwa->raw && nblkptr != drro->drr_nblkptr)
1184 return (SET_ERROR(EINVAL));
1185
369aa501
TC
1186 /*
1187 * Check for indicators that the object was freed and
1188 * reallocated. For all sends, these indicators are:
1189 * - A changed block size
1190 * - A smaller nblkptr
1191 * - A changed dnode size
1192 * For raw sends we also check a few other fields to
1193 * ensure we are preserving the objset structure exactly
1194 * as it was on the receive side:
1195 * - A changed indirect block size
1196 * - A smaller nlevels
1197 */
03916905
PD
1198 if (drro->drr_blksz != doi.doi_data_block_size ||
1199 nblkptr < doi.doi_nblkptr ||
1200 dn_slots != doi.doi_dnodesize >> DNODE_SHIFT ||
1201 (rwa->raw &&
1202 (indblksz != doi.doi_metadata_block_size ||
1203 drro->drr_nlevels < doi.doi_indirection))) {
1204 err = dmu_free_long_range(rwa->os,
1205 drro->drr_object, 0, DMU_OBJECT_END);
1206 if (err != 0)
1207 return (SET_ERROR(EINVAL));
1208 }
1209
1210 /*
1211 * The dmu does not currently support decreasing nlevels
369aa501
TC
1212 * or changing the number of dnode slots on an object. For
1213 * non-raw sends, this does not matter and the new object
1214 * can just use the previous one's nlevels. For raw sends,
1215 * however, the structure of the received dnode (including
1216 * nlevels and dnode slots) must match that of the send
1217 * side. Therefore, instead of using dmu_object_reclaim(),
1218 * we must free the object completely and call
1219 * dmu_object_claim_dnsize() instead.
03916905
PD
1220 */
1221 if ((rwa->raw && drro->drr_nlevels < doi.doi_indirection) ||
1222 dn_slots != doi.doi_dnodesize >> DNODE_SHIFT) {
1223 err = dmu_free_long_object(rwa->os, drro->drr_object);
1224 if (err != 0)
1225 return (SET_ERROR(EINVAL));
1226
1227 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1228 object = DMU_NEW_OBJECT;
1229 }
369aa501
TC
1230
1231 /*
1232 * For raw receives, free everything beyond the new incoming
1233 * maxblkid. Normally this would be done with a DRR_FREE
1234 * record that would come after this DRR_OBJECT record is
1235 * processed. However, for raw receives we manually set the
1236 * maxblkid from the drr_maxblkid and so we must first free
1237 * everything above that blkid to ensure the DMU is always
d93d4b1a 1238 * consistent with itself.
369aa501 1239 */
d93d4b1a 1240 if (rwa->raw) {
369aa501 1241 err = dmu_free_long_range(rwa->os, drro->drr_object,
d93d4b1a 1242 (drro->drr_maxblkid + 1) * drro->drr_blksz,
369aa501
TC
1243 DMU_OBJECT_END);
1244 if (err != 0)
1245 return (SET_ERROR(EINVAL));
1246 }
03916905
PD
1247 } else if (err == EEXIST) {
1248 /*
1249 * The object requested is currently an interior slot of a
1250 * multi-slot dnode. This will be resolved when the next txg
1251 * is synced out, since the send stream will have told us
1252 * to free this slot when we freed the associated dnode
1253 * earlier in the stream.
1254 */
1255 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1256 object = drro->drr_object;
1257 } else {
1258 /* object is free and we are about to allocate a new one */
1259 object = DMU_NEW_OBJECT;
1260 }
1261
1262 /*
1263 * If this is a multi-slot dnode there is a chance that this
1264 * object will expand into a slot that is already used by
1265 * another object from the previous snapshot. We must free
1266 * these objects before we attempt to allocate the new dnode.
1267 */
1268 if (dn_slots > 1) {
1269 boolean_t need_sync = B_FALSE;
1270
1271 for (uint64_t slot = drro->drr_object + 1;
1272 slot < drro->drr_object + dn_slots;
1273 slot++) {
1274 dmu_object_info_t slot_doi;
1275
1276 err = dmu_object_info(rwa->os, slot, &slot_doi);
1277 if (err == ENOENT || err == EEXIST)
1278 continue;
1279 else if (err != 0)
1280 return (err);
1281
1282 err = dmu_free_long_object(rwa->os, slot);
1283
1284 if (err != 0)
1285 return (err);
1286
1287 need_sync = B_TRUE;
1288 }
1289
1290 if (need_sync)
1291 txg_wait_synced(dmu_objset_pool(rwa->os), 0);
1292 }
1293
1294 tx = dmu_tx_create(rwa->os);
1295 dmu_tx_hold_bonus(tx, object);
1296 dmu_tx_hold_write(tx, object, 0, 0);
1297 err = dmu_tx_assign(tx, TXG_WAIT);
1298 if (err != 0) {
1299 dmu_tx_abort(tx);
1300 return (err);
1301 }
1302
1303 if (object == DMU_NEW_OBJECT) {
1304 /* currently free, want to be allocated */
1305 err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
1306 drro->drr_type, drro->drr_blksz,
1307 drro->drr_bonustype, drro->drr_bonuslen,
1308 dn_slots << DNODE_SHIFT, tx);
1309 } else if (drro->drr_type != doi.doi_type ||
1310 drro->drr_blksz != doi.doi_data_block_size ||
1311 drro->drr_bonustype != doi.doi_bonus_type ||
1312 drro->drr_bonuslen != doi.doi_bonus_size) {
1313 /* currently allocated, but with different properties */
1314 err = dmu_object_reclaim_dnsize(rwa->os, drro->drr_object,
1315 drro->drr_type, drro->drr_blksz,
1316 drro->drr_bonustype, drro->drr_bonuslen,
1317 dn_slots << DNODE_SHIFT, tx);
1318 }
1319 if (err != 0) {
1320 dmu_tx_commit(tx);
1321 return (SET_ERROR(EINVAL));
1322 }
1323
1324 if (rwa->or_crypt_params_present) {
1325 /*
1326 * Set the crypt params for the buffer associated with this
1327 * range of dnodes. This causes the blkptr_t to have the
1328 * same crypt params (byteorder, salt, iv, mac) as on the
1329 * sending side.
1330 *
1331 * Since we are committing this tx now, it is possible for
1332 * the dnode block to end up on-disk with the incorrect MAC,
1333 * if subsequent objects in this block are received in a
1334 * different txg. However, since the dataset is marked as
1335 * inconsistent, no code paths will do a non-raw read (or
1336 * decrypt the block / verify the MAC). The receive code and
1337 * scrub code can safely do raw reads and verify the
1338 * checksum. They don't need to verify the MAC.
1339 */
1340 dmu_buf_t *db = NULL;
1341 uint64_t offset = rwa->or_firstobj * DNODE_MIN_SIZE;
1342
1343 err = dmu_buf_hold_by_dnode(DMU_META_DNODE(rwa->os),
1344 offset, FTAG, &db, DMU_READ_PREFETCH | DMU_READ_NO_DECRYPT);
1345 if (err != 0) {
1346 dmu_tx_commit(tx);
1347 return (SET_ERROR(EINVAL));
1348 }
1349
1350 dmu_buf_set_crypt_params(db, rwa->or_byteorder,
1351 rwa->or_salt, rwa->or_iv, rwa->or_mac, tx);
1352
1353 dmu_buf_rele(db, FTAG);
1354
1355 rwa->or_crypt_params_present = B_FALSE;
1356 }
1357
1358 dmu_object_set_checksum(rwa->os, drro->drr_object,
1359 drro->drr_checksumtype, tx);
1360 dmu_object_set_compress(rwa->os, drro->drr_object,
1361 drro->drr_compress, tx);
1362
1363 /* handle more restrictive dnode structuring for raw recvs */
1364 if (rwa->raw) {
1365 /*
369aa501
TC
1366 * Set the indirect block size, block shift, nlevels.
1367 * This will not fail because we ensured all of the
1368 * blocks were freed earlier if this is a new object.
1369 * For non-new objects block size and indirect block
1370 * shift cannot change and nlevels can only increase.
03916905
PD
1371 */
1372 VERIFY0(dmu_object_set_blocksize(rwa->os, drro->drr_object,
1373 drro->drr_blksz, drro->drr_indblkshift, tx));
1374 VERIFY0(dmu_object_set_nlevels(rwa->os, drro->drr_object,
1375 drro->drr_nlevels, tx));
369aa501
TC
1376
1377 /*
d93d4b1a
BB
1378 * Set the maxblkid. We will never free the first block of
1379 * an object here because a maxblkid of 0 could indicate
1380 * an object with a single block or one with no blocks.
1381 * This will always succeed because we freed all blocks
1382 * beyond the new maxblkid above.
369aa501 1383 */
03916905
PD
1384 VERIFY0(dmu_object_set_maxblkid(rwa->os, drro->drr_object,
1385 drro->drr_maxblkid, tx));
1386 }
1387
1388 if (data != NULL) {
1389 dmu_buf_t *db;
6955b401 1390 dnode_t *dn;
03916905
PD
1391 uint32_t flags = DMU_READ_NO_PREFETCH;
1392
1393 if (rwa->raw)
1394 flags |= DMU_READ_NO_DECRYPT;
1395
6955b401
BB
1396 VERIFY0(dnode_hold(rwa->os, drro->drr_object, FTAG, &dn));
1397 VERIFY0(dmu_bonus_hold_by_dnode(dn, FTAG, &db, flags));
1398
03916905
PD
1399 dmu_buf_will_dirty(db, tx);
1400
1401 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
1402 bcopy(data, db->db_data, DRR_OBJECT_PAYLOAD_SIZE(drro));
1403
1404 /*
1405 * Raw bonus buffers have their byteorder determined by the
1406 * DRR_OBJECT_RANGE record.
1407 */
1408 if (rwa->byteswap && !rwa->raw) {
1409 dmu_object_byteswap_t byteswap =
1410 DMU_OT_BYTESWAP(drro->drr_bonustype);
1411 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
1412 DRR_OBJECT_PAYLOAD_SIZE(drro));
1413 }
1414 dmu_buf_rele(db, FTAG);
6955b401 1415 dnode_rele(dn, FTAG);
03916905
PD
1416 }
1417 dmu_tx_commit(tx);
1418
1419 return (0);
1420}
1421
1422/* ARGSUSED */
1423noinline static int
1424receive_freeobjects(struct receive_writer_arg *rwa,
1425 struct drr_freeobjects *drrfo)
1426{
1427 uint64_t obj;
1428 int next_err = 0;
1429
1430 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
1431 return (SET_ERROR(EINVAL));
1432
1433 for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
1434 obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
1435 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
1436 dmu_object_info_t doi;
1437 int err;
1438
1439 err = dmu_object_info(rwa->os, obj, &doi);
1440 if (err == ENOENT)
1441 continue;
1442 else if (err != 0)
1443 return (err);
1444
1445 err = dmu_free_long_object(rwa->os, obj);
1446
1447 if (err != 0)
1448 return (err);
1449
1450 if (obj > rwa->max_object)
1451 rwa->max_object = obj;
1452 }
1453 if (next_err != ESRCH)
1454 return (next_err);
1455 return (0);
1456}
1457
1458noinline static int
1459receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
1460 arc_buf_t *abuf)
1461{
1462 int err;
1463 dmu_tx_t *tx;
1464 dnode_t *dn;
1465
1466 if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
1467 !DMU_OT_IS_VALID(drrw->drr_type))
1468 return (SET_ERROR(EINVAL));
1469
1470 /*
1471 * For resuming to work, records must be in increasing order
1472 * by (object, offset).
1473 */
1474 if (drrw->drr_object < rwa->last_object ||
1475 (drrw->drr_object == rwa->last_object &&
1476 drrw->drr_offset < rwa->last_offset)) {
1477 return (SET_ERROR(EINVAL));
1478 }
1479 rwa->last_object = drrw->drr_object;
1480 rwa->last_offset = drrw->drr_offset;
1481
1482 if (rwa->last_object > rwa->max_object)
1483 rwa->max_object = rwa->last_object;
1484
1485 if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
1486 return (SET_ERROR(EINVAL));
1487
1488 tx = dmu_tx_create(rwa->os);
1489 dmu_tx_hold_write(tx, drrw->drr_object,
1490 drrw->drr_offset, drrw->drr_logical_size);
1491 err = dmu_tx_assign(tx, TXG_WAIT);
1492 if (err != 0) {
1493 dmu_tx_abort(tx);
1494 return (err);
1495 }
1496
1497 if (rwa->byteswap && !arc_is_encrypted(abuf) &&
1498 arc_get_compression(abuf) == ZIO_COMPRESS_OFF) {
1499 dmu_object_byteswap_t byteswap =
1500 DMU_OT_BYTESWAP(drrw->drr_type);
1501 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
1502 DRR_WRITE_PAYLOAD_SIZE(drrw));
1503 }
1504
1505 VERIFY0(dnode_hold(rwa->os, drrw->drr_object, FTAG, &dn));
305781da
TC
1506 err = dmu_assign_arcbuf_by_dnode(dn, drrw->drr_offset, abuf, tx);
1507 if (err != 0) {
1508 dnode_rele(dn, FTAG);
1509 dmu_tx_commit(tx);
1510 return (err);
1511 }
03916905
PD
1512 dnode_rele(dn, FTAG);
1513
1514 /*
1515 * Note: If the receive fails, we want the resume stream to start
1516 * with the same record that we last successfully received (as opposed
1517 * to the next record), so that we can verify that we are
1518 * resuming from the correct location.
1519 */
1520 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
1521 dmu_tx_commit(tx);
1522
1523 return (0);
1524}
1525
1526/*
1527 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
1528 * streams to refer to a copy of the data that is already on the
1529 * system because it came in earlier in the stream. This function
1530 * finds the earlier copy of the data, and uses that copy instead of
1531 * data from the stream to fulfill this write.
1532 */
1533static int
1534receive_write_byref(struct receive_writer_arg *rwa,
1535 struct drr_write_byref *drrwbr)
1536{
1537 dmu_tx_t *tx;
1538 int err;
1539 guid_map_entry_t gmesrch;
1540 guid_map_entry_t *gmep;
1541 avl_index_t where;
1542 objset_t *ref_os = NULL;
1543 int flags = DMU_READ_PREFETCH;
1544 dmu_buf_t *dbp;
1545
1546 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
1547 return (SET_ERROR(EINVAL));
1548
1549 /*
1550 * If the GUID of the referenced dataset is different from the
1551 * GUID of the target dataset, find the referenced dataset.
1552 */
1553 if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
1554 gmesrch.guid = drrwbr->drr_refguid;
1555 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
1556 &where)) == NULL) {
1557 return (SET_ERROR(EINVAL));
1558 }
1559 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
1560 return (SET_ERROR(EINVAL));
1561 } else {
1562 ref_os = rwa->os;
1563 }
1564
1565 if (drrwbr->drr_object > rwa->max_object)
1566 rwa->max_object = drrwbr->drr_object;
1567
1568 if (rwa->raw)
1569 flags |= DMU_READ_NO_DECRYPT;
1570
1571 /* may return either a regular db or an encrypted one */
1572 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
1573 drrwbr->drr_refoffset, FTAG, &dbp, flags);
1574 if (err != 0)
1575 return (err);
1576
1577 tx = dmu_tx_create(rwa->os);
1578
1579 dmu_tx_hold_write(tx, drrwbr->drr_object,
1580 drrwbr->drr_offset, drrwbr->drr_length);
1581 err = dmu_tx_assign(tx, TXG_WAIT);
1582 if (err != 0) {
1583 dmu_tx_abort(tx);
1584 return (err);
1585 }
1586
1587 if (rwa->raw) {
1588 dmu_copy_from_buf(rwa->os, drrwbr->drr_object,
1589 drrwbr->drr_offset, dbp, tx);
1590 } else {
1591 dmu_write(rwa->os, drrwbr->drr_object,
1592 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
1593 }
1594 dmu_buf_rele(dbp, FTAG);
1595
1596 /* See comment in restore_write. */
1597 save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
1598 dmu_tx_commit(tx);
1599 return (0);
1600}
1601
1602static int
1603receive_write_embedded(struct receive_writer_arg *rwa,
1604 struct drr_write_embedded *drrwe, void *data)
1605{
1606 dmu_tx_t *tx;
1607 int err;
1608
1609 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
1610 return (SET_ERROR(EINVAL));
1611
1612 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
1613 return (SET_ERROR(EINVAL));
1614
1615 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
1616 return (SET_ERROR(EINVAL));
1617 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
1618 return (SET_ERROR(EINVAL));
1619 if (rwa->raw)
1620 return (SET_ERROR(EINVAL));
1621
1622 if (drrwe->drr_object > rwa->max_object)
1623 rwa->max_object = drrwe->drr_object;
1624
1625 tx = dmu_tx_create(rwa->os);
1626
1627 dmu_tx_hold_write(tx, drrwe->drr_object,
1628 drrwe->drr_offset, drrwe->drr_length);
1629 err = dmu_tx_assign(tx, TXG_WAIT);
1630 if (err != 0) {
1631 dmu_tx_abort(tx);
1632 return (err);
1633 }
1634
1635 dmu_write_embedded(rwa->os, drrwe->drr_object,
1636 drrwe->drr_offset, data, drrwe->drr_etype,
1637 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
1638 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
1639
1640 /* See comment in restore_write. */
1641 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
1642 dmu_tx_commit(tx);
1643 return (0);
1644}
1645
1646static int
1647receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
1648 arc_buf_t *abuf)
1649{
1650 dmu_tx_t *tx;
1651 dmu_buf_t *db, *db_spill;
1652 int err;
1653 uint32_t flags = 0;
1654
1655 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
1656 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
1657 return (SET_ERROR(EINVAL));
1658
1659 if (rwa->raw) {
1660 if (!DMU_OT_IS_VALID(drrs->drr_type) ||
1661 drrs->drr_compressiontype >= ZIO_COMPRESS_FUNCTIONS ||
1662 drrs->drr_compressed_size == 0)
1663 return (SET_ERROR(EINVAL));
1664
1665 flags |= DMU_READ_NO_DECRYPT;
1666 }
1667
1668 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
1669 return (SET_ERROR(EINVAL));
1670
1671 if (drrs->drr_object > rwa->max_object)
1672 rwa->max_object = drrs->drr_object;
1673
1674 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
1675 if ((err = dmu_spill_hold_by_bonus(db, DMU_READ_NO_DECRYPT, FTAG,
1676 &db_spill)) != 0) {
1677 dmu_buf_rele(db, FTAG);
1678 return (err);
1679 }
1680
1681 tx = dmu_tx_create(rwa->os);
1682
1683 dmu_tx_hold_spill(tx, db->db_object);
1684
1685 err = dmu_tx_assign(tx, TXG_WAIT);
1686 if (err != 0) {
1687 dmu_buf_rele(db, FTAG);
1688 dmu_buf_rele(db_spill, FTAG);
1689 dmu_tx_abort(tx);
1690 return (err);
1691 }
1692
1693 if (db_spill->db_size < drrs->drr_length)
1694 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
1695 drrs->drr_length, tx));
1696
1697 if (rwa->byteswap && !arc_is_encrypted(abuf) &&
1698 arc_get_compression(abuf) == ZIO_COMPRESS_OFF) {
1699 dmu_object_byteswap_t byteswap =
1700 DMU_OT_BYTESWAP(drrs->drr_type);
1701 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
1702 DRR_SPILL_PAYLOAD_SIZE(drrs));
1703 }
1704
1705 dbuf_assign_arcbuf((dmu_buf_impl_t *)db_spill, abuf, tx);
1706
1707 dmu_buf_rele(db, FTAG);
1708 dmu_buf_rele(db_spill, FTAG);
1709
1710 dmu_tx_commit(tx);
1711 return (0);
1712}
1713
1714/* ARGSUSED */
1715noinline static int
1716receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
1717{
1718 int err;
1719
1720 if (drrf->drr_length != DMU_OBJECT_END &&
1721 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
1722 return (SET_ERROR(EINVAL));
1723
1724 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
1725 return (SET_ERROR(EINVAL));
1726
1727 if (drrf->drr_object > rwa->max_object)
1728 rwa->max_object = drrf->drr_object;
1729
1730 err = dmu_free_long_range(rwa->os, drrf->drr_object,
1731 drrf->drr_offset, drrf->drr_length);
1732
1733 return (err);
1734}
1735
1736static int
1737receive_object_range(struct receive_writer_arg *rwa,
1738 struct drr_object_range *drror)
1739{
1740 /*
1741 * By default, we assume this block is in our native format
1742 * (ZFS_HOST_BYTEORDER). We then take into account whether
1743 * the send stream is byteswapped (rwa->byteswap). Finally,
1744 * we need to byteswap again if this particular block was
1745 * in non-native format on the send side.
1746 */
1747 boolean_t byteorder = ZFS_HOST_BYTEORDER ^ rwa->byteswap ^
1748 !!DRR_IS_RAW_BYTESWAPPED(drror->drr_flags);
1749
1750 /*
1751 * Since dnode block sizes are constant, we should not need to worry
1752 * about making sure that the dnode block size is the same on the
1753 * sending and receiving sides for the time being. For non-raw sends,
1754 * this does not matter (and in fact we do not send a DRR_OBJECT_RANGE
1755 * record at all). Raw sends require this record type because the
1756 * encryption parameters are used to protect an entire block of bonus
1757 * buffers. If the size of dnode blocks ever becomes variable,
1758 * handling will need to be added to ensure that dnode block sizes
1759 * match on the sending and receiving side.
1760 */
1761 if (drror->drr_numslots != DNODES_PER_BLOCK ||
1762 P2PHASE(drror->drr_firstobj, DNODES_PER_BLOCK) != 0 ||
1763 !rwa->raw)
1764 return (SET_ERROR(EINVAL));
1765
1766 if (drror->drr_firstobj > rwa->max_object)
1767 rwa->max_object = drror->drr_firstobj;
1768
1769 /*
1770 * The DRR_OBJECT_RANGE handling must be deferred to receive_object()
1771 * so that the block of dnodes is not written out when it's empty,
1772 * and converted to a HOLE BP.
1773 */
1774 rwa->or_crypt_params_present = B_TRUE;
1775 rwa->or_firstobj = drror->drr_firstobj;
1776 rwa->or_numslots = drror->drr_numslots;
1777 bcopy(drror->drr_salt, rwa->or_salt, ZIO_DATA_SALT_LEN);
1778 bcopy(drror->drr_iv, rwa->or_iv, ZIO_DATA_IV_LEN);
1779 bcopy(drror->drr_mac, rwa->or_mac, ZIO_DATA_MAC_LEN);
1780 rwa->or_byteorder = byteorder;
1781
1782 return (0);
1783}
1784
1785/* used to destroy the drc_ds on error */
1786static void
1787dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
1788{
1789 dsl_dataset_t *ds = drc->drc_ds;
1790 ds_hold_flags_t dsflags = (drc->drc_raw) ? 0 : DS_HOLD_FLAG_DECRYPT;
1791
1792 /*
1793 * Wait for the txg sync before cleaning up the receive. For
1794 * resumable receives, this ensures that our resume state has
1795 * been written out to disk. For raw receives, this ensures
1796 * that the user accounting code will not attempt to do anything
1797 * after we stopped receiving the dataset.
1798 */
1799 txg_wait_synced(ds->ds_dir->dd_pool, 0);
1800 ds->ds_objset->os_raw_receive = B_FALSE;
1801
1802 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1803 if (drc->drc_resumable && !BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {
1804 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1805 dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
1806 } else {
1807 char name[ZFS_MAX_DATASET_NAME_LEN];
1808 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1809 dsl_dataset_name(ds, name);
1810 dsl_dataset_disown(ds, dsflags, dmu_recv_tag);
1811 (void) dsl_destroy_head(name);
1812 }
1813}
1814
1815static void
1816receive_cksum(struct receive_arg *ra, int len, void *buf)
1817{
1818 if (ra->byteswap) {
1819 (void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
1820 } else {
1821 (void) fletcher_4_incremental_native(buf, len, &ra->cksum);
1822 }
1823}
1824
1825/*
1826 * Read the payload into a buffer of size len, and update the current record's
1827 * payload field.
1828 * Allocate ra->next_rrd and read the next record's header into
1829 * ra->next_rrd->header.
1830 * Verify checksum of payload and next record.
1831 */
1832static int
1833receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
1834{
1835 int err;
1836 zio_cksum_t cksum_orig;
1837 zio_cksum_t *cksump;
1838
1839 if (len != 0) {
1840 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
1841 err = receive_read(ra, len, buf);
1842 if (err != 0)
1843 return (err);
1844 receive_cksum(ra, len, buf);
1845
1846 /* note: rrd is NULL when reading the begin record's payload */
1847 if (ra->rrd != NULL) {
1848 ra->rrd->payload = buf;
1849 ra->rrd->payload_size = len;
1850 ra->rrd->bytes_read = ra->bytes_read;
1851 }
960347d3
TC
1852 } else {
1853 ASSERT3P(buf, ==, NULL);
03916905
PD
1854 }
1855
1856 ra->prev_cksum = ra->cksum;
1857
1858 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
1859 err = receive_read(ra, sizeof (ra->next_rrd->header),
1860 &ra->next_rrd->header);
1861 ra->next_rrd->bytes_read = ra->bytes_read;
1862
1863 if (err != 0) {
1864 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
1865 ra->next_rrd = NULL;
1866 return (err);
1867 }
1868 if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
1869 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
1870 ra->next_rrd = NULL;
1871 return (SET_ERROR(EINVAL));
1872 }
1873
1874 /*
1875 * Note: checksum is of everything up to but not including the
1876 * checksum itself.
1877 */
1878 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
1879 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
1880 receive_cksum(ra,
1881 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
1882 &ra->next_rrd->header);
1883
1884 cksum_orig = ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
1885 cksump = &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
1886
1887 if (ra->byteswap)
1888 byteswap_record(&ra->next_rrd->header);
1889
1890 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
1891 !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
1892 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
1893 ra->next_rrd = NULL;
1894 return (SET_ERROR(ECKSUM));
1895 }
1896
1897 receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
1898
1899 return (0);
1900}
1901
1902static void
1903objlist_create(struct objlist *list)
1904{
1905 list_create(&list->list, sizeof (struct receive_objnode),
1906 offsetof(struct receive_objnode, node));
1907 list->last_lookup = 0;
1908}
1909
1910static void
1911objlist_destroy(struct objlist *list)
1912{
1913 for (struct receive_objnode *n = list_remove_head(&list->list);
1914 n != NULL; n = list_remove_head(&list->list)) {
1915 kmem_free(n, sizeof (*n));
1916 }
1917 list_destroy(&list->list);
1918}
1919
1920/*
1921 * This function looks through the objlist to see if the specified object number
1922 * is contained in the objlist. In the process, it will remove all object
1923 * numbers in the list that are smaller than the specified object number. Thus,
1924 * any lookup of an object number smaller than a previously looked up object
1925 * number will always return false; therefore, all lookups should be done in
1926 * ascending order.
1927 */
1928static boolean_t
1929objlist_exists(struct objlist *list, uint64_t object)
1930{
1931 struct receive_objnode *node = list_head(&list->list);
1932 ASSERT3U(object, >=, list->last_lookup);
1933 list->last_lookup = object;
1934 while (node != NULL && node->object < object) {
1935 VERIFY3P(node, ==, list_remove_head(&list->list));
1936 kmem_free(node, sizeof (*node));
1937 node = list_head(&list->list);
1938 }
1939 return (node != NULL && node->object == object);
1940}
1941
1942/*
1943 * The objlist is a list of object numbers stored in ascending order. However,
1944 * the insertion of new object numbers does not seek out the correct location to
1945 * store a new object number; instead, it appends it to the list for simplicity.
1946 * Thus, any users must take care to only insert new object numbers in ascending
1947 * order.
1948 */
1949static void
1950objlist_insert(struct objlist *list, uint64_t object)
1951{
1952 struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
1953 node->object = object;
1954#ifdef ZFS_DEBUG
1955 {
1956 struct receive_objnode *last_object = list_tail(&list->list);
1957 uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
1958 ASSERT3U(node->object, >, last_objnum);
1959 }
1960#endif
1961 list_insert_tail(&list->list, node);
1962}
1963
1964/*
1965 * Issue the prefetch reads for any necessary indirect blocks.
1966 *
1967 * We use the object ignore list to tell us whether or not to issue prefetches
1968 * for a given object. We do this for both correctness (in case the blocksize
1969 * of an object has changed) and performance (if the object doesn't exist, don't
1970 * needlessly try to issue prefetches). We also trim the list as we go through
1971 * the stream to prevent it from growing to an unbounded size.
1972 *
1973 * The object numbers within will always be in sorted order, and any write
1974 * records we see will also be in sorted order, but they're not sorted with
1975 * respect to each other (i.e. we can get several object records before
1976 * receiving each object's write records). As a result, once we've reached a
1977 * given object number, we can safely remove any reference to lower object
1978 * numbers in the ignore list. In practice, we receive up to 32 object records
1979 * before receiving write records, so the list can have up to 32 nodes in it.
1980 */
1981/* ARGSUSED */
1982static void
1983receive_read_prefetch(struct receive_arg *ra,
1984 uint64_t object, uint64_t offset, uint64_t length)
1985{
1986 if (!objlist_exists(&ra->ignore_objlist, object)) {
1987 dmu_prefetch(ra->os, object, 1, offset, length,
1988 ZIO_PRIORITY_SYNC_READ);
1989 }
1990}
1991
1992/*
1993 * Read records off the stream, issuing any necessary prefetches.
1994 */
1995static int
1996receive_read_record(struct receive_arg *ra)
1997{
1998 int err;
1999
2000 switch (ra->rrd->header.drr_type) {
2001 case DRR_OBJECT:
2002 {
2003 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2004 uint32_t size = DRR_OBJECT_PAYLOAD_SIZE(drro);
960347d3 2005 void *buf = NULL;
03916905
PD
2006 dmu_object_info_t doi;
2007
960347d3
TC
2008 if (size != 0)
2009 buf = kmem_zalloc(size, KM_SLEEP);
2010
03916905
PD
2011 err = receive_read_payload_and_next_header(ra, size, buf);
2012 if (err != 0) {
2013 kmem_free(buf, size);
2014 return (err);
2015 }
2016 err = dmu_object_info(ra->os, drro->drr_object, &doi);
2017 /*
2018 * See receive_read_prefetch for an explanation why we're
2019 * storing this object in the ignore_obj_list.
2020 */
2021 if (err == ENOENT || err == EEXIST ||
2022 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2023 objlist_insert(&ra->ignore_objlist, drro->drr_object);
2024 err = 0;
2025 }
2026 return (err);
2027 }
2028 case DRR_FREEOBJECTS:
2029 {
2030 err = receive_read_payload_and_next_header(ra, 0, NULL);
2031 return (err);
2032 }
2033 case DRR_WRITE:
2034 {
2035 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2036 arc_buf_t *abuf;
2037 boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
2038
2039 if (ra->raw) {
2040 boolean_t byteorder = ZFS_HOST_BYTEORDER ^
2041 !!DRR_IS_RAW_BYTESWAPPED(drrw->drr_flags) ^
2042 ra->byteswap;
2043
2044 abuf = arc_loan_raw_buf(dmu_objset_spa(ra->os),
2045 drrw->drr_object, byteorder, drrw->drr_salt,
2046 drrw->drr_iv, drrw->drr_mac, drrw->drr_type,
2047 drrw->drr_compressed_size, drrw->drr_logical_size,
2048 drrw->drr_compressiontype);
2049 } else if (DRR_WRITE_COMPRESSED(drrw)) {
2050 ASSERT3U(drrw->drr_compressed_size, >, 0);
2051 ASSERT3U(drrw->drr_logical_size, >=,
2052 drrw->drr_compressed_size);
2053 ASSERT(!is_meta);
2054 abuf = arc_loan_compressed_buf(
2055 dmu_objset_spa(ra->os),
2056 drrw->drr_compressed_size, drrw->drr_logical_size,
2057 drrw->drr_compressiontype);
2058 } else {
2059 abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2060 is_meta, drrw->drr_logical_size);
2061 }
2062
2063 err = receive_read_payload_and_next_header(ra,
2064 DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
2065 if (err != 0) {
2066 dmu_return_arcbuf(abuf);
2067 return (err);
2068 }
2069 ra->rrd->arc_buf = abuf;
2070 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2071 drrw->drr_logical_size);
2072 return (err);
2073 }
2074 case DRR_WRITE_BYREF:
2075 {
2076 struct drr_write_byref *drrwb =
2077 &ra->rrd->header.drr_u.drr_write_byref;
2078 err = receive_read_payload_and_next_header(ra, 0, NULL);
2079 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2080 drrwb->drr_length);
2081 return (err);
2082 }
2083 case DRR_WRITE_EMBEDDED:
2084 {
2085 struct drr_write_embedded *drrwe =
2086 &ra->rrd->header.drr_u.drr_write_embedded;
2087 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2088 void *buf = kmem_zalloc(size, KM_SLEEP);
2089
2090 err = receive_read_payload_and_next_header(ra, size, buf);
2091 if (err != 0) {
2092 kmem_free(buf, size);
2093 return (err);
2094 }
2095
2096 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2097 drrwe->drr_length);
2098 return (err);
2099 }
2100 case DRR_FREE:
2101 {
2102 /*
2103 * It might be beneficial to prefetch indirect blocks here, but
2104 * we don't really have the data to decide for sure.
2105 */
2106 err = receive_read_payload_and_next_header(ra, 0, NULL);
2107 return (err);
2108 }
2109 case DRR_END:
2110 {
2111 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
2112 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2113 return (SET_ERROR(ECKSUM));
2114 return (0);
2115 }
2116 case DRR_SPILL:
2117 {
2118 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2119 arc_buf_t *abuf;
2120 int len = DRR_SPILL_PAYLOAD_SIZE(drrs);
2121
2122 /* DRR_SPILL records are either raw or uncompressed */
2123 if (ra->raw) {
2124 boolean_t byteorder = ZFS_HOST_BYTEORDER ^
2125 !!DRR_IS_RAW_BYTESWAPPED(drrs->drr_flags) ^
2126 ra->byteswap;
2127
2128 abuf = arc_loan_raw_buf(dmu_objset_spa(ra->os),
2129 dmu_objset_id(ra->os), byteorder, drrs->drr_salt,
2130 drrs->drr_iv, drrs->drr_mac, drrs->drr_type,
2131 drrs->drr_compressed_size, drrs->drr_length,
2132 drrs->drr_compressiontype);
2133 } else {
2134 abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2135 DMU_OT_IS_METADATA(drrs->drr_type),
2136 drrs->drr_length);
2137 }
2138
2139 err = receive_read_payload_and_next_header(ra, len,
2140 abuf->b_data);
2141 if (err != 0) {
2142 dmu_return_arcbuf(abuf);
2143 return (err);
2144 }
2145 ra->rrd->arc_buf = abuf;
2146 return (err);
2147 }
2148 case DRR_OBJECT_RANGE:
2149 {
2150 err = receive_read_payload_and_next_header(ra, 0, NULL);
2151 return (err);
2152 }
2153 default:
2154 return (SET_ERROR(EINVAL));
2155 }
2156}
2157
2158static void
2159dprintf_drr(struct receive_record_arg *rrd, int err)
2160{
2161#ifdef ZFS_DEBUG
2162 switch (rrd->header.drr_type) {
2163 case DRR_OBJECT:
2164 {
2165 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2166 dprintf("drr_type = OBJECT obj = %llu type = %u "
2167 "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
2168 "compress = %u dn_slots = %u err = %d\n",
2169 drro->drr_object, drro->drr_type, drro->drr_bonustype,
2170 drro->drr_blksz, drro->drr_bonuslen,
2171 drro->drr_checksumtype, drro->drr_compress,
2172 drro->drr_dn_slots, err);
2173 break;
2174 }
2175 case DRR_FREEOBJECTS:
2176 {
2177 struct drr_freeobjects *drrfo =
2178 &rrd->header.drr_u.drr_freeobjects;
2179 dprintf("drr_type = FREEOBJECTS firstobj = %llu "
2180 "numobjs = %llu err = %d\n",
2181 drrfo->drr_firstobj, drrfo->drr_numobjs, err);
2182 break;
2183 }
2184 case DRR_WRITE:
2185 {
2186 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2187 dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
5dbf8b4e 2188 "lsize = %llu cksumtype = %u flags = %u "
03916905
PD
2189 "compress = %u psize = %llu err = %d\n",
2190 drrw->drr_object, drrw->drr_type, drrw->drr_offset,
2191 drrw->drr_logical_size, drrw->drr_checksumtype,
2192 drrw->drr_flags, drrw->drr_compressiontype,
2193 drrw->drr_compressed_size, err);
2194 break;
2195 }
2196 case DRR_WRITE_BYREF:
2197 {
2198 struct drr_write_byref *drrwbr =
2199 &rrd->header.drr_u.drr_write_byref;
2200 dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
2201 "length = %llu toguid = %llx refguid = %llx "
2202 "refobject = %llu refoffset = %llu cksumtype = %u "
5dbf8b4e 2203 "flags = %u err = %d\n",
03916905
PD
2204 drrwbr->drr_object, drrwbr->drr_offset,
2205 drrwbr->drr_length, drrwbr->drr_toguid,
2206 drrwbr->drr_refguid, drrwbr->drr_refobject,
2207 drrwbr->drr_refoffset, drrwbr->drr_checksumtype,
2208 drrwbr->drr_flags, err);
2209 break;
2210 }
2211 case DRR_WRITE_EMBEDDED:
2212 {
2213 struct drr_write_embedded *drrwe =
2214 &rrd->header.drr_u.drr_write_embedded;
2215 dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
2216 "length = %llu compress = %u etype = %u lsize = %u "
2217 "psize = %u err = %d\n",
2218 drrwe->drr_object, drrwe->drr_offset, drrwe->drr_length,
2219 drrwe->drr_compression, drrwe->drr_etype,
2220 drrwe->drr_lsize, drrwe->drr_psize, err);
2221 break;
2222 }
2223 case DRR_FREE:
2224 {
2225 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2226 dprintf("drr_type = FREE obj = %llu offset = %llu "
2227 "length = %lld err = %d\n",
2228 drrf->drr_object, drrf->drr_offset, drrf->drr_length,
2229 err);
2230 break;
2231 }
2232 case DRR_SPILL:
2233 {
2234 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2235 dprintf("drr_type = SPILL obj = %llu length = %llu "
2236 "err = %d\n", drrs->drr_object, drrs->drr_length, err);
2237 break;
2238 }
5dbf8b4e
TC
2239 case DRR_OBJECT_RANGE:
2240 {
2241 struct drr_object_range *drror =
2242 &rrd->header.drr_u.drr_object_range;
2243 dprintf("drr_type = OBJECT_RANGE firstobj = %llu "
2244 "numslots = %llu flags = %u err = %d\n",
2245 drror->drr_firstobj, drror->drr_numslots,
2246 drror->drr_flags, err);
2247 break;
2248 }
03916905
PD
2249 default:
2250 return;
2251 }
2252#endif
2253}
2254
2255/*
2256 * Commit the records to the pool.
2257 */
2258static int
2259receive_process_record(struct receive_writer_arg *rwa,
2260 struct receive_record_arg *rrd)
2261{
2262 int err;
2263
2264 /* Processing in order, therefore bytes_read should be increasing. */
2265 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2266 rwa->bytes_read = rrd->bytes_read;
2267
2268 switch (rrd->header.drr_type) {
2269 case DRR_OBJECT:
2270 {
2271 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2272 err = receive_object(rwa, drro, rrd->payload);
2273 kmem_free(rrd->payload, rrd->payload_size);
2274 rrd->payload = NULL;
2275 break;
2276 }
2277 case DRR_FREEOBJECTS:
2278 {
2279 struct drr_freeobjects *drrfo =
2280 &rrd->header.drr_u.drr_freeobjects;
2281 err = receive_freeobjects(rwa, drrfo);
2282 break;
2283 }
2284 case DRR_WRITE:
2285 {
2286 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2287 err = receive_write(rwa, drrw, rrd->arc_buf);
2288 /* if receive_write() is successful, it consumes the arc_buf */
2289 if (err != 0)
2290 dmu_return_arcbuf(rrd->arc_buf);
2291 rrd->arc_buf = NULL;
2292 rrd->payload = NULL;
2293 break;
2294 }
2295 case DRR_WRITE_BYREF:
2296 {
2297 struct drr_write_byref *drrwbr =
2298 &rrd->header.drr_u.drr_write_byref;
2299 err = receive_write_byref(rwa, drrwbr);
2300 break;
2301 }
2302 case DRR_WRITE_EMBEDDED:
2303 {
2304 struct drr_write_embedded *drrwe =
2305 &rrd->header.drr_u.drr_write_embedded;
2306 err = receive_write_embedded(rwa, drrwe, rrd->payload);
2307 kmem_free(rrd->payload, rrd->payload_size);
2308 rrd->payload = NULL;
2309 break;
2310 }
2311 case DRR_FREE:
2312 {
2313 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2314 err = receive_free(rwa, drrf);
2315 break;
2316 }
2317 case DRR_SPILL:
2318 {
2319 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2320 err = receive_spill(rwa, drrs, rrd->arc_buf);
2321 /* if receive_spill() is successful, it consumes the arc_buf */
2322 if (err != 0)
2323 dmu_return_arcbuf(rrd->arc_buf);
2324 rrd->arc_buf = NULL;
2325 rrd->payload = NULL;
2326 break;
2327 }
2328 case DRR_OBJECT_RANGE:
2329 {
2330 struct drr_object_range *drror =
2331 &rrd->header.drr_u.drr_object_range;
5dbf8b4e
TC
2332 err = receive_object_range(rwa, drror);
2333 break;
03916905
PD
2334 }
2335 default:
5dbf8b4e 2336 err = (SET_ERROR(EINVAL));
03916905
PD
2337 }
2338
2339 if (err != 0)
2340 dprintf_drr(rrd, err);
2341
2342 return (err);
2343}
2344
2345/*
2346 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2347 * receive_process_record When we're done, signal the main thread and exit.
2348 */
2349static void
2350receive_writer_thread(void *arg)
2351{
2352 struct receive_writer_arg *rwa = arg;
2353 struct receive_record_arg *rrd;
2354 fstrans_cookie_t cookie = spl_fstrans_mark();
2355
2356 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2357 rrd = bqueue_dequeue(&rwa->q)) {
2358 /*
2359 * If there's an error, the main thread will stop putting things
2360 * on the queue, but we need to clear everything in it before we
2361 * can exit.
2362 */
2363 if (rwa->err == 0) {
2364 rwa->err = receive_process_record(rwa, rrd);
2365 } else if (rrd->arc_buf != NULL) {
2366 dmu_return_arcbuf(rrd->arc_buf);
2367 rrd->arc_buf = NULL;
2368 rrd->payload = NULL;
2369 } else if (rrd->payload != NULL) {
2370 kmem_free(rrd->payload, rrd->payload_size);
2371 rrd->payload = NULL;
2372 }
2373 kmem_free(rrd, sizeof (*rrd));
2374 }
2375 kmem_free(rrd, sizeof (*rrd));
2376 mutex_enter(&rwa->mutex);
2377 rwa->done = B_TRUE;
2378 cv_signal(&rwa->cv);
2379 mutex_exit(&rwa->mutex);
2380 spl_fstrans_unmark(cookie);
2381 thread_exit();
2382}
2383
2384static int
2385resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
2386{
2387 uint64_t val;
2388 objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
2389 uint64_t dsobj = dmu_objset_id(ra->os);
2390 uint64_t resume_obj, resume_off;
2391
2392 if (nvlist_lookup_uint64(begin_nvl,
2393 "resume_object", &resume_obj) != 0 ||
2394 nvlist_lookup_uint64(begin_nvl,
2395 "resume_offset", &resume_off) != 0) {
2396 return (SET_ERROR(EINVAL));
2397 }
2398 VERIFY0(zap_lookup(mos, dsobj,
2399 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
2400 if (resume_obj != val)
2401 return (SET_ERROR(EINVAL));
2402 VERIFY0(zap_lookup(mos, dsobj,
2403 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
2404 if (resume_off != val)
2405 return (SET_ERROR(EINVAL));
2406
2407 return (0);
2408}
2409
2410/*
2411 * Read in the stream's records, one by one, and apply them to the pool. There
2412 * are two threads involved; the thread that calls this function will spin up a
2413 * worker thread, read the records off the stream one by one, and issue
2414 * prefetches for any necessary indirect blocks. It will then push the records
2415 * onto an internal blocking queue. The worker thread will pull the records off
2416 * the queue, and actually write the data into the DMU. This way, the worker
2417 * thread doesn't have to wait for reads to complete, since everything it needs
2418 * (the indirect blocks) will be prefetched.
2419 *
2420 * NB: callers *must* call dmu_recv_end() if this succeeds.
2421 */
2422int
2423dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
2424 int cleanup_fd, uint64_t *action_handlep)
2425{
2426 int err = 0;
2427 struct receive_arg *ra;
2428 struct receive_writer_arg *rwa;
2429 int featureflags;
2430 uint32_t payloadlen;
2431 void *payload;
2432 nvlist_t *begin_nvl = NULL;
2433
2434 ra = kmem_zalloc(sizeof (*ra), KM_SLEEP);
2435 rwa = kmem_zalloc(sizeof (*rwa), KM_SLEEP);
2436
2437 ra->byteswap = drc->drc_byteswap;
2438 ra->raw = drc->drc_raw;
2439 ra->cksum = drc->drc_cksum;
2440 ra->vp = vp;
2441 ra->voff = *voffp;
2442
2443 if (dsl_dataset_is_zapified(drc->drc_ds)) {
2444 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
2445 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
2446 sizeof (ra->bytes_read), 1, &ra->bytes_read);
2447 }
2448
2449 objlist_create(&ra->ignore_objlist);
2450
2451 /* these were verified in dmu_recv_begin */
2452 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
2453 DMU_SUBSTREAM);
2454 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2455
2456 /*
2457 * Open the objset we are modifying.
2458 */
2459 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra->os));
2460
2461 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2462
2463 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
2464 ra->featureflags = featureflags;
2465
2466 ASSERT0(ra->os->os_encrypted &&
2467 (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA));
2468
2469 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2470 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2471 minor_t minor;
2472
2473 if (cleanup_fd == -1) {
2474 err = SET_ERROR(EBADF);
2475 goto out;
2476 }
2477 err = zfs_onexit_fd_hold(cleanup_fd, &minor);
2478 if (err != 0) {
2479 cleanup_fd = -1;
2480 goto out;
2481 }
2482
2483 if (*action_handlep == 0) {
2484 rwa->guid_to_ds_map =
2485 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2486 avl_create(rwa->guid_to_ds_map, guid_compare,
2487 sizeof (guid_map_entry_t),
2488 offsetof(guid_map_entry_t, avlnode));
2489 err = zfs_onexit_add_cb(minor,
2490 free_guid_map_onexit, rwa->guid_to_ds_map,
2491 action_handlep);
2492 if (err != 0)
2493 goto out;
2494 } else {
2495 err = zfs_onexit_cb_data(minor, *action_handlep,
2496 (void **)&rwa->guid_to_ds_map);
2497 if (err != 0)
2498 goto out;
2499 }
2500
2501 drc->drc_guid_to_ds_map = rwa->guid_to_ds_map;
2502 }
2503
2504 payloadlen = drc->drc_drr_begin->drr_payloadlen;
2505 payload = NULL;
2506 if (payloadlen != 0)
2507 payload = kmem_alloc(payloadlen, KM_SLEEP);
2508
2509 err = receive_read_payload_and_next_header(ra, payloadlen, payload);
2510 if (err != 0) {
2511 if (payloadlen != 0)
2512 kmem_free(payload, payloadlen);
2513 goto out;
2514 }
2515 if (payloadlen != 0) {
2516 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
2517 kmem_free(payload, payloadlen);
2518 if (err != 0)
2519 goto out;
2520 }
2521
2522 /* handle DSL encryption key payload */
2523 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
2524 nvlist_t *keynvl = NULL;
2525
2526 ASSERT(ra->os->os_encrypted);
2527 ASSERT(drc->drc_raw);
2528
2529 err = nvlist_lookup_nvlist(begin_nvl, "crypt_keydata", &keynvl);
2530 if (err != 0)
2531 goto out;
2532
2533 /*
2534 * If this is a new dataset we set the key immediately.
2535 * Otherwise we don't want to change the key until we
2536 * are sure the rest of the receive succeeded so we stash
2537 * the keynvl away until then.
2538 */
2539 err = dsl_crypto_recv_raw(spa_name(ra->os->os_spa),
f00ab3f2
TC
2540 drc->drc_ds->ds_object, drc->drc_fromsnapobj,
2541 drc->drc_drrb->drr_type, keynvl, drc->drc_newfs);
03916905
PD
2542 if (err != 0)
2543 goto out;
2544
f00ab3f2
TC
2545 /* see comment in dmu_recv_end_sync() */
2546 drc->drc_ivset_guid = 0;
2547 (void) nvlist_lookup_uint64(keynvl, "to_ivset_guid",
2548 &drc->drc_ivset_guid);
2549
03916905
PD
2550 if (!drc->drc_newfs)
2551 drc->drc_keynvl = fnvlist_dup(keynvl);
2552 }
2553
2554 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
2555 err = resume_check(ra, begin_nvl);
2556 if (err != 0)
2557 goto out;
2558 }
2559
2560 (void) bqueue_init(&rwa->q,
2561 MAX(zfs_recv_queue_length, 2 * zfs_max_recordsize),
2562 offsetof(struct receive_record_arg, node));
2563 cv_init(&rwa->cv, NULL, CV_DEFAULT, NULL);
2564 mutex_init(&rwa->mutex, NULL, MUTEX_DEFAULT, NULL);
2565 rwa->os = ra->os;
2566 rwa->byteswap = drc->drc_byteswap;
2567 rwa->resumable = drc->drc_resumable;
2568 rwa->raw = drc->drc_raw;
2569 rwa->os->os_raw_receive = drc->drc_raw;
2570
2571 (void) thread_create(NULL, 0, receive_writer_thread, rwa, 0, curproc,
2572 TS_RUN, minclsyspri);
2573 /*
2574 * We're reading rwa->err without locks, which is safe since we are the
2575 * only reader, and the worker thread is the only writer. It's ok if we
2576 * miss a write for an iteration or two of the loop, since the writer
2577 * thread will keep freeing records we send it until we send it an eos
2578 * marker.
2579 *
2580 * We can leave this loop in 3 ways: First, if rwa->err is
2581 * non-zero. In that case, the writer thread will free the rrd we just
2582 * pushed. Second, if we're interrupted; in that case, either it's the
2583 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
2584 * has been handed off to the writer thread who will free it. Finally,
2585 * if receive_read_record fails or we're at the end of the stream, then
2586 * we free ra->rrd and exit.
2587 */
2588 while (rwa->err == 0) {
2589 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2590 err = SET_ERROR(EINTR);
2591 break;
2592 }
2593
2594 ASSERT3P(ra->rrd, ==, NULL);
2595 ra->rrd = ra->next_rrd;
2596 ra->next_rrd = NULL;
2597 /* Allocates and loads header into ra->next_rrd */
2598 err = receive_read_record(ra);
2599
2600 if (ra->rrd->header.drr_type == DRR_END || err != 0) {
2601 kmem_free(ra->rrd, sizeof (*ra->rrd));
2602 ra->rrd = NULL;
2603 break;
2604 }
2605
2606 bqueue_enqueue(&rwa->q, ra->rrd,
2607 sizeof (struct receive_record_arg) + ra->rrd->payload_size);
2608 ra->rrd = NULL;
2609 }
f00ab3f2
TC
2610 ASSERT3P(ra->rrd, ==, NULL);
2611 ra->rrd = kmem_zalloc(sizeof (*ra->rrd), KM_SLEEP);
2612 ra->rrd->eos_marker = B_TRUE;
2613 bqueue_enqueue(&rwa->q, ra->rrd, 1);
03916905
PD
2614
2615 mutex_enter(&rwa->mutex);
2616 while (!rwa->done) {
2617 cv_wait(&rwa->cv, &rwa->mutex);
2618 }
2619 mutex_exit(&rwa->mutex);
2620
2621 /*
2622 * If we are receiving a full stream as a clone, all object IDs which
2623 * are greater than the maximum ID referenced in the stream are
2624 * by definition unused and must be freed.
2625 */
2626 if (drc->drc_clone && drc->drc_drrb->drr_fromguid == 0) {
2627 uint64_t obj = rwa->max_object + 1;
2628 int free_err = 0;
2629 int next_err = 0;
2630
2631 while (next_err == 0) {
2632 free_err = dmu_free_long_object(rwa->os, obj);
2633 if (free_err != 0 && free_err != ENOENT)
2634 break;
2635
2636 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0);
2637 }
2638
2639 if (err == 0) {
2640 if (free_err != 0 && free_err != ENOENT)
2641 err = free_err;
2642 else if (next_err != ESRCH)
2643 err = next_err;
2644 }
2645 }
2646
2647 cv_destroy(&rwa->cv);
2648 mutex_destroy(&rwa->mutex);
2649 bqueue_destroy(&rwa->q);
2650 if (err == 0)
2651 err = rwa->err;
2652
2653out:
f00ab3f2
TC
2654 /*
2655 * If we hit an error before we started the receive_writer_thread
2656 * we need to clean up the next_rrd we create by processing the
2657 * DRR_BEGIN record.
2658 */
2659 if (ra->next_rrd != NULL)
2660 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2661
03916905
PD
2662 nvlist_free(begin_nvl);
2663 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2664 zfs_onexit_fd_rele(cleanup_fd);
2665
2666 if (err != 0) {
2667 /*
2668 * Clean up references. If receive is not resumable,
2669 * destroy what we created, so we don't leave it in
2670 * the inconsistent state.
2671 */
2672 dmu_recv_cleanup_ds(drc);
2673 nvlist_free(drc->drc_keynvl);
2674 }
2675
2676 *voffp = ra->voff;
2677 objlist_destroy(&ra->ignore_objlist);
2678 kmem_free(ra, sizeof (*ra));
2679 kmem_free(rwa, sizeof (*rwa));
2680 return (err);
2681}
2682
2683static int
2684dmu_recv_end_check(void *arg, dmu_tx_t *tx)
2685{
2686 dmu_recv_cookie_t *drc = arg;
2687 dsl_pool_t *dp = dmu_tx_pool(tx);
2688 int error;
2689
2690 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
2691
2692 if (!drc->drc_newfs) {
2693 dsl_dataset_t *origin_head;
2694
2695 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
2696 if (error != 0)
2697 return (error);
2698 if (drc->drc_force) {
2699 /*
2700 * We will destroy any snapshots in tofs (i.e. before
2701 * origin_head) that are after the origin (which is
2702 * the snap before drc_ds, because drc_ds can not
2703 * have any snaps of its own).
2704 */
2705 uint64_t obj;
2706
2707 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2708 while (obj !=
2709 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
2710 dsl_dataset_t *snap;
2711 error = dsl_dataset_hold_obj(dp, obj, FTAG,
2712 &snap);
2713 if (error != 0)
2714 break;
2715 if (snap->ds_dir != origin_head->ds_dir)
2716 error = SET_ERROR(EINVAL);
2717 if (error == 0) {
2718 error = dsl_destroy_snapshot_check_impl(
2719 snap, B_FALSE);
2720 }
2721 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
2722 dsl_dataset_rele(snap, FTAG);
2723 if (error != 0)
2724 break;
2725 }
2726 if (error != 0) {
2727 dsl_dataset_rele(origin_head, FTAG);
2728 return (error);
2729 }
2730 }
2731 if (drc->drc_keynvl != NULL) {
2732 error = dsl_crypto_recv_raw_key_check(drc->drc_ds,
2733 drc->drc_keynvl, tx);
2734 if (error != 0) {
2735 dsl_dataset_rele(origin_head, FTAG);
2736 return (error);
2737 }
2738 }
2739
2740 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
2741 origin_head, drc->drc_force, drc->drc_owner, tx);
2742 if (error != 0) {
2743 dsl_dataset_rele(origin_head, FTAG);
2744 return (error);
2745 }
2746 error = dsl_dataset_snapshot_check_impl(origin_head,
2747 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
2748 dsl_dataset_rele(origin_head, FTAG);
2749 if (error != 0)
2750 return (error);
2751
2752 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
2753 } else {
2754 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
2755 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
2756 }
2757 return (error);
2758}
2759
2760static void
2761dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
2762{
2763 dmu_recv_cookie_t *drc = arg;
2764 dsl_pool_t *dp = dmu_tx_pool(tx);
2765 boolean_t encrypted = drc->drc_ds->ds_dir->dd_crypto_obj != 0;
2766
2767 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
2768 tx, "snap=%s", drc->drc_tosnap);
2769 drc->drc_ds->ds_objset->os_raw_receive = B_FALSE;
2770
2771 if (!drc->drc_newfs) {
2772 dsl_dataset_t *origin_head;
2773
2774 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
2775 &origin_head));
2776
2777 if (drc->drc_force) {
2778 /*
2779 * Destroy any snapshots of drc_tofs (origin_head)
2780 * after the origin (the snap before drc_ds).
2781 */
2782 uint64_t obj;
2783
2784 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2785 while (obj !=
2786 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
2787 dsl_dataset_t *snap;
2788 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
2789 &snap));
2790 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
2791 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
2792 dsl_destroy_snapshot_sync_impl(snap,
2793 B_FALSE, tx);
2794 dsl_dataset_rele(snap, FTAG);
2795 }
2796 }
2797 if (drc->drc_keynvl != NULL) {
2798 dsl_crypto_recv_raw_key_sync(drc->drc_ds,
2799 drc->drc_keynvl, tx);
2800 nvlist_free(drc->drc_keynvl);
2801 drc->drc_keynvl = NULL;
2802 }
2803
2804 VERIFY3P(drc->drc_ds->ds_prev, ==, origin_head->ds_prev);
2805
2806 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
2807 origin_head, tx);
2808 dsl_dataset_snapshot_sync_impl(origin_head,
2809 drc->drc_tosnap, tx);
2810
2811 /* set snapshot's creation time and guid */
2812 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
2813 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
2814 drc->drc_drrb->drr_creation_time;
2815 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
2816 drc->drc_drrb->drr_toguid;
2817 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
2818 ~DS_FLAG_INCONSISTENT;
2819
2820 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2821 dsl_dataset_phys(origin_head)->ds_flags &=
2822 ~DS_FLAG_INCONSISTENT;
2823
2824 drc->drc_newsnapobj =
2825 dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2826
2827 dsl_dataset_rele(origin_head, FTAG);
2828 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
2829
2830 if (drc->drc_owner != NULL)
2831 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
2832 } else {
2833 dsl_dataset_t *ds = drc->drc_ds;
2834
2835 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
2836
2837 /* set snapshot's creation time and guid */
2838 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
2839 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
2840 drc->drc_drrb->drr_creation_time;
2841 dsl_dataset_phys(ds->ds_prev)->ds_guid =
2842 drc->drc_drrb->drr_toguid;
2843 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
2844 ~DS_FLAG_INCONSISTENT;
2845
2846 dmu_buf_will_dirty(ds->ds_dbuf, tx);
2847 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
2848 if (dsl_dataset_has_resume_receive_state(ds)) {
2849 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2850 DS_FIELD_RESUME_FROMGUID, tx);
2851 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2852 DS_FIELD_RESUME_OBJECT, tx);
2853 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2854 DS_FIELD_RESUME_OFFSET, tx);
2855 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2856 DS_FIELD_RESUME_BYTES, tx);
2857 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2858 DS_FIELD_RESUME_TOGUID, tx);
2859 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2860 DS_FIELD_RESUME_TONAME, tx);
2861 }
2862 drc->drc_newsnapobj =
2863 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
2864 }
f00ab3f2
TC
2865
2866 /*
2867 * If this is a raw receive, the crypt_keydata nvlist will include
2868 * a to_ivset_guid for us to set on the new snapshot. This value
2869 * will override the value generated by the snapshot code. However,
2870 * this value may not be present, because older implementations of
2871 * the raw send code did not include this value, and we are still
2872 * allowed to receive them if the zfs_disable_ivset_guid_check
2873 * tunable is set, in which case we will leave the newly-generated
2874 * value.
2875 */
2876 if (drc->drc_raw && drc->drc_ivset_guid != 0) {
2877 dmu_object_zapify(dp->dp_meta_objset, drc->drc_newsnapobj,
2878 DMU_OT_DSL_DATASET, tx);
2879 VERIFY0(zap_update(dp->dp_meta_objset, drc->drc_newsnapobj,
2880 DS_FIELD_IVSET_GUID, sizeof (uint64_t), 1,
2881 &drc->drc_ivset_guid, tx));
2882 }
2883
03916905
PD
2884 zvol_create_minors(dp->dp_spa, drc->drc_tofs, B_TRUE);
2885
2886 /*
2887 * Release the hold from dmu_recv_begin. This must be done before
2888 * we return to open context, so that when we free the dataset's dnode
2889 * we can evict its bonus buffer. Since the dataset may be destroyed
2890 * at this point (and therefore won't have a valid pointer to the spa)
2891 * we release the key mapping manually here while we do have a valid
2892 * pointer, if it exists.
2893 */
2894 if (!drc->drc_raw && encrypted) {
2895 (void) spa_keystore_remove_mapping(dmu_tx_pool(tx)->dp_spa,
2896 drc->drc_ds->ds_object, drc->drc_ds);
2897 }
2898 dsl_dataset_disown(drc->drc_ds, 0, dmu_recv_tag);
2899 drc->drc_ds = NULL;
2900}
2901
2902static int
2903add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj,
2904 boolean_t raw)
2905{
2906 dsl_pool_t *dp;
2907 dsl_dataset_t *snapds;
2908 guid_map_entry_t *gmep;
2909 objset_t *os;
2910 ds_hold_flags_t dsflags = (raw) ? 0 : DS_HOLD_FLAG_DECRYPT;
2911 int err;
2912
2913 ASSERT(guid_map != NULL);
2914
2915 err = dsl_pool_hold(name, FTAG, &dp);
2916 if (err != 0)
2917 return (err);
2918 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
2919 err = dsl_dataset_own_obj(dp, snapobj, dsflags, gmep, &snapds);
2920 if (err == 0) {
2921 /*
2922 * If this is a deduplicated raw send stream, we need
2923 * to make sure that we can still read raw blocks from
2924 * earlier datasets in the stream, so we set the
2925 * os_raw_receive flag now.
2926 */
2927 if (raw) {
2928 err = dmu_objset_from_ds(snapds, &os);
2929 if (err != 0) {
2930 dsl_dataset_disown(snapds, dsflags, FTAG);
2931 dsl_pool_rele(dp, FTAG);
2932 kmem_free(gmep, sizeof (*gmep));
2933 return (err);
2934 }
2935 os->os_raw_receive = B_TRUE;
2936 }
2937
2938 gmep->raw = raw;
2939 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
2940 gmep->gme_ds = snapds;
2941 avl_add(guid_map, gmep);
2942 } else {
2943 kmem_free(gmep, sizeof (*gmep));
2944 }
2945
2946 dsl_pool_rele(dp, FTAG);
2947 return (err);
2948}
2949
2950static int dmu_recv_end_modified_blocks = 3;
2951
2952static int
2953dmu_recv_existing_end(dmu_recv_cookie_t *drc)
2954{
2955#ifdef _KERNEL
2956 /*
2957 * We will be destroying the ds; make sure its origin is unmounted if
2958 * necessary.
2959 */
2960 char name[ZFS_MAX_DATASET_NAME_LEN];
2961 dsl_dataset_name(drc->drc_ds, name);
2962 zfs_destroy_unmount_origin(name);
2963#endif
2964
2965 return (dsl_sync_task(drc->drc_tofs,
2966 dmu_recv_end_check, dmu_recv_end_sync, drc,
2967 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
2968}
2969
2970static int
2971dmu_recv_new_end(dmu_recv_cookie_t *drc)
2972{
2973 return (dsl_sync_task(drc->drc_tofs,
2974 dmu_recv_end_check, dmu_recv_end_sync, drc,
2975 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
2976}
2977
2978int
2979dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
2980{
2981 int error;
2982
2983 drc->drc_owner = owner;
2984
2985 if (drc->drc_newfs)
2986 error = dmu_recv_new_end(drc);
2987 else
2988 error = dmu_recv_existing_end(drc);
2989
2990 if (error != 0) {
2991 dmu_recv_cleanup_ds(drc);
2992 nvlist_free(drc->drc_keynvl);
2993 } else if (drc->drc_guid_to_ds_map != NULL) {
2994 (void) add_ds_to_guidmap(drc->drc_tofs, drc->drc_guid_to_ds_map,
2995 drc->drc_newsnapobj, drc->drc_raw);
2996 }
2997 return (error);
2998}
2999
3000/*
3001 * Return TRUE if this objset is currently being received into.
3002 */
3003boolean_t
3004dmu_objset_is_receiving(objset_t *os)
3005{
3006 return (os->os_dsl_dataset != NULL &&
3007 os->os_dsl_dataset->ds_owner == dmu_recv_tag);
3008}
3009
3010#if defined(_KERNEL)
3011module_param(zfs_recv_queue_length, int, 0644);
3012MODULE_PARM_DESC(zfs_recv_queue_length, "Maximum receive queue length");
3013#endif