]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/zfs/dmu_send.c
Merge branch 'illumos-2605'
[FreeBSD/FreeBSD.git] / module / zfs / dmu_send.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or 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.
29  */
30
31 #include <sys/dmu.h>
32 #include <sys/dmu_impl.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dbuf.h>
35 #include <sys/dnode.h>
36 #include <sys/zfs_context.h>
37 #include <sys/dmu_objset.h>
38 #include <sys/dmu_traverse.h>
39 #include <sys/dsl_dataset.h>
40 #include <sys/dsl_dir.h>
41 #include <sys/dsl_prop.h>
42 #include <sys/dsl_pool.h>
43 #include <sys/dsl_synctask.h>
44 #include <sys/spa_impl.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/zap.h>
47 #include <sys/zio_checksum.h>
48 #include <sys/zfs_znode.h>
49 #include <zfs_fletcher.h>
50 #include <sys/avl.h>
51 #include <sys/ddt.h>
52 #include <sys/zfs_onexit.h>
53 #include <sys/dmu_send.h>
54 #include <sys/dsl_destroy.h>
55 #include <sys/blkptr.h>
56 #include <sys/dsl_bookmark.h>
57 #include <sys/zfeature.h>
58 #include <sys/bqueue.h>
59 #include <sys/zvol.h>
60 #include <sys/policy.h>
61
62 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
63 int zfs_send_corrupt_data = B_FALSE;
64 int zfs_send_queue_length = 16 * 1024 * 1024;
65 int zfs_recv_queue_length = 16 * 1024 * 1024;
66 /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
67 int zfs_send_set_freerecords_bit = B_TRUE;
68
69 static char *dmu_recv_tag = "dmu_recv_tag";
70 const char *recv_clone_name = "%recv";
71
72 #define BP_SPAN(datablkszsec, indblkshift, level) \
73         (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
74         (level) * (indblkshift - SPA_BLKPTRSHIFT)))
75
76 static void byteswap_record(dmu_replay_record_t *drr);
77
78 struct send_thread_arg {
79         bqueue_t        q;
80         dsl_dataset_t   *ds;            /* Dataset to traverse */
81         uint64_t        fromtxg;        /* Traverse from this txg */
82         int             flags;          /* flags to pass to traverse_dataset */
83         int             error_code;
84         boolean_t       cancel;
85         zbookmark_phys_t resume;
86 };
87
88 struct send_block_record {
89         boolean_t               eos_marker; /* Marks the end of the stream */
90         blkptr_t                bp;
91         zbookmark_phys_t        zb;
92         uint8_t                 indblkshift;
93         uint16_t                datablkszsec;
94         bqueue_node_t           ln;
95 };
96
97 typedef struct dump_bytes_io {
98         dmu_sendarg_t   *dbi_dsp;
99         void            *dbi_buf;
100         int             dbi_len;
101 } dump_bytes_io_t;
102
103 static void
104 dump_bytes_cb(void *arg)
105 {
106         dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
107         dmu_sendarg_t *dsp = dbi->dbi_dsp;
108         dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
109         ssize_t resid; /* have to get resid to get detailed errno */
110
111         /*
112          * The code does not rely on this (len being a multiple of 8).  We keep
113          * this assertion because of the corresponding assertion in
114          * receive_read().  Keeping this assertion ensures that we do not
115          * inadvertently break backwards compatibility (causing the assertion
116          * in receive_read() to trigger on old software).
117          *
118          * Removing the assertions could be rolled into a new feature that uses
119          * data that isn't 8-byte aligned; if the assertions were removed, a
120          * feature flag would have to be added.
121          */
122
123         ASSERT0(dbi->dbi_len % 8);
124
125         dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
126             (caddr_t)dbi->dbi_buf, dbi->dbi_len,
127             0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
128
129         mutex_enter(&ds->ds_sendstream_lock);
130         *dsp->dsa_off += dbi->dbi_len;
131         mutex_exit(&ds->ds_sendstream_lock);
132 }
133
134 static int
135 dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
136 {
137         dump_bytes_io_t dbi;
138
139         dbi.dbi_dsp = dsp;
140         dbi.dbi_buf = buf;
141         dbi.dbi_len = len;
142
143 #if defined(HAVE_LARGE_STACKS)
144         dump_bytes_cb(&dbi);
145 #else
146         /*
147          * The vn_rdwr() call is performed in a taskq to ensure that there is
148          * always enough stack space to write safely to the target filesystem.
149          * The ZIO_TYPE_FREE threads are used because there can be a lot of
150          * them and they are used in vdev_file.c for a similar purpose.
151          */
152         spa_taskq_dispatch_sync(dmu_objset_spa(dsp->dsa_os), ZIO_TYPE_FREE,
153             ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
154 #endif /* HAVE_LARGE_STACKS */
155
156         return (dsp->dsa_err);
157 }
158
159 /*
160  * For all record types except BEGIN, fill in the checksum (overlaid in
161  * drr_u.drr_checksum.drr_checksum).  The checksum verifies everything
162  * up to the start of the checksum itself.
163  */
164 static int
165 dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
166 {
167         ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
168             ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
169         fletcher_4_incremental_native(dsp->dsa_drr,
170             offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
171             &dsp->dsa_zc);
172         if (dsp->dsa_drr->drr_type != DRR_BEGIN) {
173                 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
174                     drr_checksum.drr_checksum));
175                 dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
176         }
177         fletcher_4_incremental_native(&dsp->dsa_drr->
178             drr_u.drr_checksum.drr_checksum,
179             sizeof (zio_cksum_t), &dsp->dsa_zc);
180         if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
181                 return (SET_ERROR(EINTR));
182         if (payload_len != 0) {
183                 fletcher_4_incremental_native(payload, payload_len,
184                     &dsp->dsa_zc);
185                 if (dump_bytes(dsp, payload, payload_len) != 0)
186                         return (SET_ERROR(EINTR));
187         }
188         return (0);
189 }
190
191 /*
192  * Fill in the drr_free struct, or perform aggregation if the previous record is
193  * also a free record, and the two are adjacent.
194  *
195  * Note that we send free records even for a full send, because we want to be
196  * able to receive a full send as a clone, which requires a list of all the free
197  * and freeobject records that were generated on the source.
198  */
199 static int
200 dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
201     uint64_t length)
202 {
203         struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
204
205         /*
206          * When we receive a free record, dbuf_free_range() assumes
207          * that the receiving system doesn't have any dbufs in the range
208          * being freed.  This is always true because there is a one-record
209          * constraint: we only send one WRITE record for any given
210          * object,offset.  We know that the one-record constraint is
211          * true because we always send data in increasing order by
212          * object,offset.
213          *
214          * If the increasing-order constraint ever changes, we should find
215          * another way to assert that the one-record constraint is still
216          * satisfied.
217          */
218         ASSERT(object > dsp->dsa_last_data_object ||
219             (object == dsp->dsa_last_data_object &&
220             offset > dsp->dsa_last_data_offset));
221
222         if (length != -1ULL && offset + length < offset)
223                 length = -1ULL;
224
225         /*
226          * If there is a pending op, but it's not PENDING_FREE, push it out,
227          * since free block aggregation can only be done for blocks of the
228          * same type (i.e., DRR_FREE records can only be aggregated with
229          * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
230          * aggregated with other DRR_FREEOBJECTS records.
231          */
232         if (dsp->dsa_pending_op != PENDING_NONE &&
233             dsp->dsa_pending_op != PENDING_FREE) {
234                 if (dump_record(dsp, NULL, 0) != 0)
235                         return (SET_ERROR(EINTR));
236                 dsp->dsa_pending_op = PENDING_NONE;
237         }
238
239         if (dsp->dsa_pending_op == PENDING_FREE) {
240                 /*
241                  * There should never be a PENDING_FREE if length is -1
242                  * (because dump_dnode is the only place where this
243                  * function is called with a -1, and only after flushing
244                  * any pending record).
245                  */
246                 ASSERT(length != -1ULL);
247                 /*
248                  * Check to see whether this free block can be aggregated
249                  * with pending one.
250                  */
251                 if (drrf->drr_object == object && drrf->drr_offset +
252                     drrf->drr_length == offset) {
253                         drrf->drr_length += length;
254                         return (0);
255                 } else {
256                         /* not a continuation.  Push out pending record */
257                         if (dump_record(dsp, NULL, 0) != 0)
258                                 return (SET_ERROR(EINTR));
259                         dsp->dsa_pending_op = PENDING_NONE;
260                 }
261         }
262         /* create a FREE record and make it pending */
263         bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
264         dsp->dsa_drr->drr_type = DRR_FREE;
265         drrf->drr_object = object;
266         drrf->drr_offset = offset;
267         drrf->drr_length = length;
268         drrf->drr_toguid = dsp->dsa_toguid;
269         if (length == -1ULL) {
270                 if (dump_record(dsp, NULL, 0) != 0)
271                         return (SET_ERROR(EINTR));
272         } else {
273                 dsp->dsa_pending_op = PENDING_FREE;
274         }
275
276         return (0);
277 }
278
279 static int
280 dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
281     uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
282 {
283         struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
284
285         /*
286          * We send data in increasing object, offset order.
287          * See comment in dump_free() for details.
288          */
289         ASSERT(object > dsp->dsa_last_data_object ||
290             (object == dsp->dsa_last_data_object &&
291             offset > dsp->dsa_last_data_offset));
292         dsp->dsa_last_data_object = object;
293         dsp->dsa_last_data_offset = offset + blksz - 1;
294
295         /*
296          * If there is any kind of pending aggregation (currently either
297          * a grouping of free objects or free blocks), push it out to
298          * the stream, since aggregation can't be done across operations
299          * of different types.
300          */
301         if (dsp->dsa_pending_op != PENDING_NONE) {
302                 if (dump_record(dsp, NULL, 0) != 0)
303                         return (SET_ERROR(EINTR));
304                 dsp->dsa_pending_op = PENDING_NONE;
305         }
306         /* write a WRITE record */
307         bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
308         dsp->dsa_drr->drr_type = DRR_WRITE;
309         drrw->drr_object = object;
310         drrw->drr_type = type;
311         drrw->drr_offset = offset;
312         drrw->drr_length = blksz;
313         drrw->drr_toguid = dsp->dsa_toguid;
314         if (bp == NULL || BP_IS_EMBEDDED(bp)) {
315                 /*
316                  * There's no pre-computed checksum for partial-block
317                  * writes or embedded BP's, so (like
318                  * fletcher4-checkummed blocks) userland will have to
319                  * compute a dedup-capable checksum itself.
320                  */
321                 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
322         } else {
323                 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
324                 if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup)
325                         drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
326                 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
327                 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
328                 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
329                 drrw->drr_key.ddk_cksum = bp->blk_cksum;
330         }
331
332         if (dump_record(dsp, data, blksz) != 0)
333                 return (SET_ERROR(EINTR));
334         return (0);
335 }
336
337 static int
338 dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
339     int blksz, const blkptr_t *bp)
340 {
341         char buf[BPE_PAYLOAD_SIZE];
342         struct drr_write_embedded *drrw =
343             &(dsp->dsa_drr->drr_u.drr_write_embedded);
344
345         if (dsp->dsa_pending_op != PENDING_NONE) {
346                 if (dump_record(dsp, NULL, 0) != 0)
347                         return (EINTR);
348                 dsp->dsa_pending_op = PENDING_NONE;
349         }
350
351         ASSERT(BP_IS_EMBEDDED(bp));
352
353         bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
354         dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
355         drrw->drr_object = object;
356         drrw->drr_offset = offset;
357         drrw->drr_length = blksz;
358         drrw->drr_toguid = dsp->dsa_toguid;
359         drrw->drr_compression = BP_GET_COMPRESS(bp);
360         drrw->drr_etype = BPE_GET_ETYPE(bp);
361         drrw->drr_lsize = BPE_GET_LSIZE(bp);
362         drrw->drr_psize = BPE_GET_PSIZE(bp);
363
364         decode_embedded_bp_compressed(bp, buf);
365
366         if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
367                 return (EINTR);
368         return (0);
369 }
370
371 static int
372 dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
373 {
374         struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
375
376         if (dsp->dsa_pending_op != PENDING_NONE) {
377                 if (dump_record(dsp, NULL, 0) != 0)
378                         return (SET_ERROR(EINTR));
379                 dsp->dsa_pending_op = PENDING_NONE;
380         }
381
382         /* write a SPILL record */
383         bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
384         dsp->dsa_drr->drr_type = DRR_SPILL;
385         drrs->drr_object = object;
386         drrs->drr_length = blksz;
387         drrs->drr_toguid = dsp->dsa_toguid;
388
389         if (dump_record(dsp, data, blksz) != 0)
390                 return (SET_ERROR(EINTR));
391         return (0);
392 }
393
394 static int
395 dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
396 {
397         struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
398
399         /*
400          * If there is a pending op, but it's not PENDING_FREEOBJECTS,
401          * push it out, since free block aggregation can only be done for
402          * blocks of the same type (i.e., DRR_FREE records can only be
403          * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
404          * can only be aggregated with other DRR_FREEOBJECTS records.
405          */
406         if (dsp->dsa_pending_op != PENDING_NONE &&
407             dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
408                 if (dump_record(dsp, NULL, 0) != 0)
409                         return (SET_ERROR(EINTR));
410                 dsp->dsa_pending_op = PENDING_NONE;
411         }
412         if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
413                 /*
414                  * See whether this free object array can be aggregated
415                  * with pending one
416                  */
417                 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
418                         drrfo->drr_numobjs += numobjs;
419                         return (0);
420                 } else {
421                         /* can't be aggregated.  Push out pending record */
422                         if (dump_record(dsp, NULL, 0) != 0)
423                                 return (SET_ERROR(EINTR));
424                         dsp->dsa_pending_op = PENDING_NONE;
425                 }
426         }
427
428         /* write a FREEOBJECTS record */
429         bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
430         dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
431         drrfo->drr_firstobj = firstobj;
432         drrfo->drr_numobjs = numobjs;
433         drrfo->drr_toguid = dsp->dsa_toguid;
434
435         dsp->dsa_pending_op = PENDING_FREEOBJECTS;
436
437         return (0);
438 }
439
440 static int
441 dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
442 {
443         struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
444
445         if (object < dsp->dsa_resume_object) {
446                 /*
447                  * Note: when resuming, we will visit all the dnodes in
448                  * the block of dnodes that we are resuming from.  In
449                  * this case it's unnecessary to send the dnodes prior to
450                  * the one we are resuming from.  We should be at most one
451                  * block's worth of dnodes behind the resume point.
452                  */
453                 ASSERT3U(dsp->dsa_resume_object - object, <,
454                     1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
455                 return (0);
456         }
457
458         if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
459                 return (dump_freeobjects(dsp, object, 1));
460
461         if (dsp->dsa_pending_op != PENDING_NONE) {
462                 if (dump_record(dsp, NULL, 0) != 0)
463                         return (SET_ERROR(EINTR));
464                 dsp->dsa_pending_op = PENDING_NONE;
465         }
466
467         /* write an OBJECT record */
468         bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
469         dsp->dsa_drr->drr_type = DRR_OBJECT;
470         drro->drr_object = object;
471         drro->drr_type = dnp->dn_type;
472         drro->drr_bonustype = dnp->dn_bonustype;
473         drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
474         drro->drr_bonuslen = dnp->dn_bonuslen;
475         drro->drr_dn_slots = dnp->dn_extra_slots + 1;
476         drro->drr_checksumtype = dnp->dn_checksum;
477         drro->drr_compress = dnp->dn_compress;
478         drro->drr_toguid = dsp->dsa_toguid;
479
480         if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
481             drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
482                 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
483
484         if (dump_record(dsp, DN_BONUS(dnp),
485             P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
486                 return (SET_ERROR(EINTR));
487         }
488
489         /* Free anything past the end of the file. */
490         if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
491             (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
492                 return (SET_ERROR(EINTR));
493         if (dsp->dsa_err != 0)
494                 return (SET_ERROR(EINTR));
495         return (0);
496 }
497
498 static boolean_t
499 backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
500 {
501         if (!BP_IS_EMBEDDED(bp))
502                 return (B_FALSE);
503
504         /*
505          * Compression function must be legacy, or explicitly enabled.
506          */
507         if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
508             !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
509                 return (B_FALSE);
510
511         /*
512          * Embed type must be explicitly enabled.
513          */
514         switch (BPE_GET_ETYPE(bp)) {
515         case BP_EMBEDDED_TYPE_DATA:
516                 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
517                         return (B_TRUE);
518                 break;
519         default:
520                 return (B_FALSE);
521         }
522         return (B_FALSE);
523 }
524
525 /*
526  * This is the callback function to traverse_dataset that acts as the worker
527  * thread for dmu_send_impl.
528  */
529 /*ARGSUSED*/
530 static int
531 send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
532     const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
533 {
534         struct send_thread_arg *sta = arg;
535         struct send_block_record *record;
536         uint64_t record_size;
537         int err = 0;
538
539         ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
540             zb->zb_object >= sta->resume.zb_object);
541
542         if (sta->cancel)
543                 return (SET_ERROR(EINTR));
544
545         if (bp == NULL) {
546                 ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
547                 return (0);
548         } else if (zb->zb_level < 0) {
549                 return (0);
550         }
551
552         record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
553         record->eos_marker = B_FALSE;
554         record->bp = *bp;
555         record->zb = *zb;
556         record->indblkshift = dnp->dn_indblkshift;
557         record->datablkszsec = dnp->dn_datablkszsec;
558         record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
559         bqueue_enqueue(&sta->q, record, record_size);
560
561         return (err);
562 }
563
564 /*
565  * This function kicks off the traverse_dataset.  It also handles setting the
566  * error code of the thread in case something goes wrong, and pushes the End of
567  * Stream record when the traverse_dataset call has finished.  If there is no
568  * dataset to traverse, the thread immediately pushes End of Stream marker.
569  */
570 static void
571 send_traverse_thread(void *arg)
572 {
573         struct send_thread_arg *st_arg = arg;
574         int err;
575         struct send_block_record *data;
576
577         if (st_arg->ds != NULL) {
578                 err = traverse_dataset_resume(st_arg->ds,
579                     st_arg->fromtxg, &st_arg->resume,
580                     st_arg->flags, send_cb, st_arg);
581
582                 if (err != EINTR)
583                         st_arg->error_code = err;
584         }
585         data = kmem_zalloc(sizeof (*data), KM_SLEEP);
586         data->eos_marker = B_TRUE;
587         bqueue_enqueue(&st_arg->q, data, 1);
588 }
589
590 /*
591  * This function actually handles figuring out what kind of record needs to be
592  * dumped, reading the data (which has hopefully been prefetched), and calling
593  * the appropriate helper function.
594  */
595 static int
596 do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
597 {
598         dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
599         const blkptr_t *bp = &data->bp;
600         const zbookmark_phys_t *zb = &data->zb;
601         uint8_t indblkshift = data->indblkshift;
602         uint16_t dblkszsec = data->datablkszsec;
603         spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
604         dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
605         int err = 0;
606         uint64_t dnobj;
607
608         ASSERT3U(zb->zb_level, >=, 0);
609
610         ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
611             zb->zb_object >= dsa->dsa_resume_object);
612
613         if (zb->zb_object != DMU_META_DNODE_OBJECT &&
614             DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
615                 return (0);
616         } else if (BP_IS_HOLE(bp) &&
617             zb->zb_object == DMU_META_DNODE_OBJECT) {
618                 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
619                 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
620                 err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
621         } else if (BP_IS_HOLE(bp)) {
622                 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
623                 uint64_t offset = zb->zb_blkid * span;
624                 err = dump_free(dsa, zb->zb_object, offset, span);
625         } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
626                 return (0);
627         } else if (type == DMU_OT_DNODE) {
628                 dnode_phys_t *blk;
629                 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
630                 arc_flags_t aflags = ARC_FLAG_WAIT;
631                 arc_buf_t *abuf;
632                 int i;
633
634                 ASSERT0(zb->zb_level);
635
636                 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
637                     ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
638                     &aflags, zb) != 0)
639                         return (SET_ERROR(EIO));
640
641                 blk = abuf->b_data;
642                 dnobj = zb->zb_blkid * epb;
643                 for (i = 0; i < epb; i += blk[i].dn_extra_slots + 1) {
644                         err = dump_dnode(dsa, dnobj + i, blk + i);
645                         if (err != 0)
646                                 break;
647                 }
648                 (void) arc_buf_remove_ref(abuf, &abuf);
649         } else if (type == DMU_OT_SA) {
650                 arc_flags_t aflags = ARC_FLAG_WAIT;
651                 arc_buf_t *abuf;
652                 int blksz = BP_GET_LSIZE(bp);
653
654                 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
655                     ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
656                     &aflags, zb) != 0)
657                         return (SET_ERROR(EIO));
658
659                 err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
660                 (void) arc_buf_remove_ref(abuf, &abuf);
661         } else if (backup_do_embed(dsa, bp)) {
662                 /* it's an embedded level-0 block of a regular object */
663                 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
664                 ASSERT0(zb->zb_level);
665                 err = dump_write_embedded(dsa, zb->zb_object,
666                     zb->zb_blkid * blksz, blksz, bp);
667         } else {
668                 /* it's a level-0 block of a regular object */
669                 arc_flags_t aflags = ARC_FLAG_WAIT;
670                 arc_buf_t *abuf;
671                 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
672                 uint64_t offset;
673
674                 ASSERT0(zb->zb_level);
675                 ASSERT(zb->zb_object > dsa->dsa_resume_object ||
676                     (zb->zb_object == dsa->dsa_resume_object &&
677                     zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
678
679                 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
680                     ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
681                     &aflags, zb) != 0) {
682                         if (zfs_send_corrupt_data) {
683                                 uint64_t *ptr;
684                                 /* Send a block filled with 0x"zfs badd bloc" */
685                                 abuf = arc_buf_alloc(spa, blksz, &abuf,
686                                     ARC_BUFC_DATA);
687                                 for (ptr = abuf->b_data;
688                                     (char *)ptr < (char *)abuf->b_data + blksz;
689                                     ptr++)
690                                         *ptr = 0x2f5baddb10cULL;
691                         } else {
692                                 return (SET_ERROR(EIO));
693                         }
694                 }
695
696                 offset = zb->zb_blkid * blksz;
697
698                 if (!(dsa->dsa_featureflags &
699                     DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
700                     blksz > SPA_OLD_MAXBLOCKSIZE) {
701                         char *buf = abuf->b_data;
702                         while (blksz > 0 && err == 0) {
703                                 int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
704                                 err = dump_write(dsa, type, zb->zb_object,
705                                     offset, n, NULL, buf);
706                                 offset += n;
707                                 buf += n;
708                                 blksz -= n;
709                         }
710                 } else {
711                         err = dump_write(dsa, type, zb->zb_object,
712                             offset, blksz, bp, abuf->b_data);
713                 }
714                 (void) arc_buf_remove_ref(abuf, &abuf);
715         }
716
717         ASSERT(err == 0 || err == EINTR);
718         return (err);
719 }
720
721 /*
722  * Pop the new data off the queue, and free the old data.
723  */
724 static struct send_block_record *
725 get_next_record(bqueue_t *bq, struct send_block_record *data)
726 {
727         struct send_block_record *tmp = bqueue_dequeue(bq);
728         kmem_free(data, sizeof (*data));
729         return (tmp);
730 }
731
732 /*
733  * Actually do the bulk of the work in a zfs send.
734  *
735  * Note: Releases dp using the specified tag.
736  */
737 static int
738 dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
739     zfs_bookmark_phys_t *ancestor_zb,
740     boolean_t is_clone, boolean_t embedok, boolean_t large_block_ok, int outfd,
741     uint64_t resumeobj, uint64_t resumeoff,
742     vnode_t *vp, offset_t *off)
743 {
744         objset_t *os;
745         dmu_replay_record_t *drr;
746         dmu_sendarg_t *dsp;
747         int err;
748         uint64_t fromtxg = 0;
749         uint64_t featureflags = 0;
750         struct send_thread_arg to_arg;
751         void *payload = NULL;
752         size_t payload_len = 0;
753         struct send_block_record *to_data;
754
755         err = dmu_objset_from_ds(to_ds, &os);
756         if (err != 0) {
757                 dsl_pool_rele(dp, tag);
758                 return (err);
759         }
760
761         drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
762         drr->drr_type = DRR_BEGIN;
763         drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
764         DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
765             DMU_SUBSTREAM);
766
767         bzero(&to_arg, sizeof (to_arg));
768
769 #ifdef _KERNEL
770         if (dmu_objset_type(os) == DMU_OST_ZFS) {
771                 uint64_t version;
772                 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
773                         kmem_free(drr, sizeof (dmu_replay_record_t));
774                         dsl_pool_rele(dp, tag);
775                         return (SET_ERROR(EINVAL));
776                 }
777                 if (version >= ZPL_VERSION_SA) {
778                         featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
779                 }
780         }
781 #endif
782
783         if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
784                 featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
785         if (to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_DNODE])
786                 featureflags |= DMU_BACKUP_FEATURE_LARGE_DNODE;
787         if (embedok &&
788             spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
789                 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
790                 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
791                         featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
792         }
793
794         if (resumeobj != 0 || resumeoff != 0) {
795                 featureflags |= DMU_BACKUP_FEATURE_RESUMING;
796         }
797
798         DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
799             featureflags);
800
801         drr->drr_u.drr_begin.drr_creation_time =
802             dsl_dataset_phys(to_ds)->ds_creation_time;
803         drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
804         if (is_clone)
805                 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
806         drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
807         if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
808                 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
809         if (zfs_send_set_freerecords_bit)
810                 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
811
812         if (ancestor_zb != NULL) {
813                 drr->drr_u.drr_begin.drr_fromguid =
814                     ancestor_zb->zbm_guid;
815                 fromtxg = ancestor_zb->zbm_creation_txg;
816         }
817         dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
818         if (!to_ds->ds_is_snapshot) {
819                 (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
820                     sizeof (drr->drr_u.drr_begin.drr_toname));
821         }
822
823         dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
824
825         dsp->dsa_drr = drr;
826         dsp->dsa_vp = vp;
827         dsp->dsa_outfd = outfd;
828         dsp->dsa_proc = curproc;
829         dsp->dsa_os = os;
830         dsp->dsa_off = off;
831         dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
832         dsp->dsa_pending_op = PENDING_NONE;
833         dsp->dsa_featureflags = featureflags;
834         dsp->dsa_resume_object = resumeobj;
835         dsp->dsa_resume_offset = resumeoff;
836
837         mutex_enter(&to_ds->ds_sendstream_lock);
838         list_insert_head(&to_ds->ds_sendstreams, dsp);
839         mutex_exit(&to_ds->ds_sendstream_lock);
840
841         dsl_dataset_long_hold(to_ds, FTAG);
842         dsl_pool_rele(dp, tag);
843
844         if (resumeobj != 0 || resumeoff != 0) {
845                 dmu_object_info_t to_doi;
846                 nvlist_t *nvl;
847                 err = dmu_object_info(os, resumeobj, &to_doi);
848                 if (err != 0)
849                         goto out;
850                 SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
851                     resumeoff / to_doi.doi_data_block_size);
852
853                 nvl = fnvlist_alloc();
854                 fnvlist_add_uint64(nvl, "resume_object", resumeobj);
855                 fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
856                 payload = fnvlist_pack(nvl, &payload_len);
857                 drr->drr_payloadlen = payload_len;
858                 fnvlist_free(nvl);
859         }
860
861         err = dump_record(dsp, payload, payload_len);
862         fnvlist_pack_free(payload, payload_len);
863         if (err != 0) {
864                 err = dsp->dsa_err;
865                 goto out;
866         }
867
868         err = bqueue_init(&to_arg.q, zfs_send_queue_length,
869             offsetof(struct send_block_record, ln));
870         to_arg.error_code = 0;
871         to_arg.cancel = B_FALSE;
872         to_arg.ds = to_ds;
873         to_arg.fromtxg = fromtxg;
874         to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
875         (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
876             TS_RUN, minclsyspri);
877
878         to_data = bqueue_dequeue(&to_arg.q);
879
880         while (!to_data->eos_marker && err == 0) {
881                 err = do_dump(dsp, to_data);
882                 to_data = get_next_record(&to_arg.q, to_data);
883                 if (issig(JUSTLOOKING) && issig(FORREAL))
884                         err = EINTR;
885         }
886
887         if (err != 0) {
888                 to_arg.cancel = B_TRUE;
889                 while (!to_data->eos_marker) {
890                         to_data = get_next_record(&to_arg.q, to_data);
891                 }
892         }
893         kmem_free(to_data, sizeof (*to_data));
894
895         bqueue_destroy(&to_arg.q);
896
897         if (err == 0 && to_arg.error_code != 0)
898                 err = to_arg.error_code;
899
900         if (err != 0)
901                 goto out;
902
903         if (dsp->dsa_pending_op != PENDING_NONE)
904                 if (dump_record(dsp, NULL, 0) != 0)
905                         err = SET_ERROR(EINTR);
906
907         if (err != 0) {
908                 if (err == EINTR && dsp->dsa_err != 0)
909                         err = dsp->dsa_err;
910                 goto out;
911         }
912
913         bzero(drr, sizeof (dmu_replay_record_t));
914         drr->drr_type = DRR_END;
915         drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
916         drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
917
918         if (dump_record(dsp, NULL, 0) != 0)
919                 err = dsp->dsa_err;
920
921 out:
922         mutex_enter(&to_ds->ds_sendstream_lock);
923         list_remove(&to_ds->ds_sendstreams, dsp);
924         mutex_exit(&to_ds->ds_sendstream_lock);
925
926         kmem_free(drr, sizeof (dmu_replay_record_t));
927         kmem_free(dsp, sizeof (dmu_sendarg_t));
928
929         dsl_dataset_long_rele(to_ds, FTAG);
930
931         return (err);
932 }
933
934 int
935 dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
936     boolean_t embedok, boolean_t large_block_ok,
937     int outfd, vnode_t *vp, offset_t *off)
938 {
939         dsl_pool_t *dp;
940         dsl_dataset_t *ds;
941         dsl_dataset_t *fromds = NULL;
942         int err;
943
944         err = dsl_pool_hold(pool, FTAG, &dp);
945         if (err != 0)
946                 return (err);
947
948         err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
949         if (err != 0) {
950                 dsl_pool_rele(dp, FTAG);
951                 return (err);
952         }
953
954         if (fromsnap != 0) {
955                 zfs_bookmark_phys_t zb;
956                 boolean_t is_clone;
957
958                 err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
959                 if (err != 0) {
960                         dsl_dataset_rele(ds, FTAG);
961                         dsl_pool_rele(dp, FTAG);
962                         return (err);
963                 }
964                 if (!dsl_dataset_is_before(ds, fromds, 0))
965                         err = SET_ERROR(EXDEV);
966                 zb.zbm_creation_time =
967                     dsl_dataset_phys(fromds)->ds_creation_time;
968                 zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
969                 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
970                 is_clone = (fromds->ds_dir != ds->ds_dir);
971                 dsl_dataset_rele(fromds, FTAG);
972                 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
973                     embedok, large_block_ok, outfd, 0, 0, vp, off);
974         } else {
975                 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
976                     embedok, large_block_ok, outfd, 0, 0, vp, off);
977         }
978         dsl_dataset_rele(ds, FTAG);
979         return (err);
980 }
981
982 int
983 dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
984     boolean_t large_block_ok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
985     vnode_t *vp, offset_t *off)
986 {
987         dsl_pool_t *dp;
988         dsl_dataset_t *ds;
989         int err;
990         boolean_t owned = B_FALSE;
991
992         if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
993                 return (SET_ERROR(EINVAL));
994
995         err = dsl_pool_hold(tosnap, FTAG, &dp);
996         if (err != 0)
997                 return (err);
998
999         if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
1000                 /*
1001                  * We are sending a filesystem or volume.  Ensure
1002                  * that it doesn't change by owning the dataset.
1003                  */
1004                 err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
1005                 owned = B_TRUE;
1006         } else {
1007                 err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
1008         }
1009         if (err != 0) {
1010                 dsl_pool_rele(dp, FTAG);
1011                 return (err);
1012         }
1013
1014         if (fromsnap != NULL) {
1015                 zfs_bookmark_phys_t zb;
1016                 boolean_t is_clone = B_FALSE;
1017                 int fsnamelen = strchr(tosnap, '@') - tosnap;
1018
1019                 /*
1020                  * If the fromsnap is in a different filesystem, then
1021                  * mark the send stream as a clone.
1022                  */
1023                 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
1024                     (fromsnap[fsnamelen] != '@' &&
1025                     fromsnap[fsnamelen] != '#')) {
1026                         is_clone = B_TRUE;
1027                 }
1028
1029                 if (strchr(fromsnap, '@')) {
1030                         dsl_dataset_t *fromds;
1031                         err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
1032                         if (err == 0) {
1033                                 if (!dsl_dataset_is_before(ds, fromds, 0))
1034                                         err = SET_ERROR(EXDEV);
1035                                 zb.zbm_creation_time =
1036                                     dsl_dataset_phys(fromds)->ds_creation_time;
1037                                 zb.zbm_creation_txg =
1038                                     dsl_dataset_phys(fromds)->ds_creation_txg;
1039                                 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
1040                                 is_clone = (ds->ds_dir != fromds->ds_dir);
1041                                 dsl_dataset_rele(fromds, FTAG);
1042                         }
1043                 } else {
1044                         err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
1045                 }
1046                 if (err != 0) {
1047                         dsl_dataset_rele(ds, FTAG);
1048                         dsl_pool_rele(dp, FTAG);
1049                         return (err);
1050                 }
1051                 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
1052                     embedok, large_block_ok,
1053                     outfd, resumeobj, resumeoff, vp, off);
1054         } else {
1055                 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1056                     embedok, large_block_ok,
1057                     outfd, resumeobj, resumeoff, vp, off);
1058         }
1059         if (owned)
1060                 dsl_dataset_disown(ds, FTAG);
1061         else
1062                 dsl_dataset_rele(ds, FTAG);
1063         return (err);
1064 }
1065
1066 static int
1067 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t size,
1068     uint64_t *sizep)
1069 {
1070         int err;
1071         /*
1072          * Assume that space (both on-disk and in-stream) is dominated by
1073          * data.  We will adjust for indirect blocks and the copies property,
1074          * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1075          */
1076
1077         /*
1078          * Subtract out approximate space used by indirect blocks.
1079          * Assume most space is used by data blocks (non-indirect, non-dnode).
1080          * Assume all blocks are recordsize.  Assume ditto blocks and
1081          * internal fragmentation counter out compression.
1082          *
1083          * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1084          * block, which we observe in practice.
1085          */
1086         uint64_t recordsize;
1087         err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
1088         if (err != 0)
1089                 return (err);
1090         size -= size / recordsize * sizeof (blkptr_t);
1091
1092         /* Add in the space for the record associated with each block. */
1093         size += size / recordsize * sizeof (dmu_replay_record_t);
1094
1095         *sizep = size;
1096
1097         return (0);
1098 }
1099
1100 int
1101 dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
1102 {
1103         int err;
1104         uint64_t size;
1105
1106         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1107
1108         /* tosnap must be a snapshot */
1109         if (!ds->ds_is_snapshot)
1110                 return (SET_ERROR(EINVAL));
1111
1112         /* fromsnap, if provided, must be a snapshot */
1113         if (fromds != NULL && !fromds->ds_is_snapshot)
1114                 return (SET_ERROR(EINVAL));
1115
1116         /*
1117          * fromsnap must be an earlier snapshot from the same fs as tosnap,
1118          * or the origin's fs.
1119          */
1120         if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1121                 return (SET_ERROR(EXDEV));
1122
1123         /* Get uncompressed size estimate of changed data. */
1124         if (fromds == NULL) {
1125                 size = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1126         } else {
1127                 uint64_t used, comp;
1128                 err = dsl_dataset_space_written(fromds, ds,
1129                     &used, &comp, &size);
1130                 if (err != 0)
1131                         return (err);
1132         }
1133
1134         err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1135         return (err);
1136 }
1137
1138 /*
1139  * Simple callback used to traverse the blocks of a snapshot and sum their
1140  * uncompressed size
1141  */
1142 /* ARGSUSED */
1143 static int
1144 dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1145     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1146 {
1147         uint64_t *spaceptr = arg;
1148         if (bp != NULL && !BP_IS_HOLE(bp)) {
1149                 *spaceptr += BP_GET_UCSIZE(bp);
1150         }
1151         return (0);
1152 }
1153
1154 /*
1155  * Given a desination snapshot and a TXG, calculate the approximate size of a
1156  * send stream sent from that TXG. from_txg may be zero, indicating that the
1157  * whole snapshot will be sent.
1158  */
1159 int
1160 dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
1161     uint64_t *sizep)
1162 {
1163         int err;
1164         uint64_t size = 0;
1165
1166         ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1167
1168         /* tosnap must be a snapshot */
1169         if (!dsl_dataset_is_snapshot(ds))
1170                 return (SET_ERROR(EINVAL));
1171
1172         /* verify that from_txg is before the provided snapshot was taken */
1173         if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1174                 return (SET_ERROR(EXDEV));
1175         }
1176         /*
1177          * traverse the blocks of the snapshot with birth times after
1178          * from_txg, summing their uncompressed size
1179          */
1180         err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1181             dmu_calculate_send_traversal, &size);
1182         if (err)
1183                 return (err);
1184
1185         err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1186         return (err);
1187 }
1188
1189 typedef struct dmu_recv_begin_arg {
1190         const char *drba_origin;
1191         dmu_recv_cookie_t *drba_cookie;
1192         cred_t *drba_cred;
1193         uint64_t drba_snapobj;
1194 } dmu_recv_begin_arg_t;
1195
1196 static int
1197 recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
1198     uint64_t fromguid)
1199 {
1200         uint64_t val;
1201         int error;
1202         dsl_pool_t *dp = ds->ds_dir->dd_pool;
1203
1204         /* temporary clone name must not exist */
1205         error = zap_lookup(dp->dp_meta_objset,
1206             dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
1207             8, 1, &val);
1208         if (error != ENOENT)
1209                 return (error == 0 ? EBUSY : error);
1210
1211         /* new snapshot name must not exist */
1212         error = zap_lookup(dp->dp_meta_objset,
1213             dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1214             drba->drba_cookie->drc_tosnap, 8, 1, &val);
1215         if (error != ENOENT)
1216                 return (error == 0 ? EEXIST : error);
1217
1218         /*
1219          * Check snapshot limit before receiving. We'll recheck again at the
1220          * end, but might as well abort before receiving if we're already over
1221          * the limit.
1222          *
1223          * Note that we do not check the file system limit with
1224          * dsl_dir_fscount_check because the temporary %clones don't count
1225          * against that limit.
1226          */
1227         error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1228             NULL, drba->drba_cred);
1229         if (error != 0)
1230                 return (error);
1231
1232         if (fromguid != 0) {
1233                 dsl_dataset_t *snap;
1234                 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1235
1236                 /* Find snapshot in this dir that matches fromguid. */
1237                 while (obj != 0) {
1238                         error = dsl_dataset_hold_obj(dp, obj, FTAG,
1239                             &snap);
1240                         if (error != 0)
1241                                 return (SET_ERROR(ENODEV));
1242                         if (snap->ds_dir != ds->ds_dir) {
1243                                 dsl_dataset_rele(snap, FTAG);
1244                                 return (SET_ERROR(ENODEV));
1245                         }
1246                         if (dsl_dataset_phys(snap)->ds_guid == fromguid)
1247                                 break;
1248                         obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
1249                         dsl_dataset_rele(snap, FTAG);
1250                 }
1251                 if (obj == 0)
1252                         return (SET_ERROR(ENODEV));
1253
1254                 if (drba->drba_cookie->drc_force) {
1255                         drba->drba_snapobj = obj;
1256                 } else {
1257                         /*
1258                          * If we are not forcing, there must be no
1259                          * changes since fromsnap.
1260                          */
1261                         if (dsl_dataset_modified_since_snap(ds, snap)) {
1262                                 dsl_dataset_rele(snap, FTAG);
1263                                 return (SET_ERROR(ETXTBSY));
1264                         }
1265                         drba->drba_snapobj = ds->ds_prev->ds_object;
1266                 }
1267
1268                 dsl_dataset_rele(snap, FTAG);
1269         } else {
1270                 /* if full, then must be forced */
1271                 if (!drba->drba_cookie->drc_force)
1272                         return (SET_ERROR(EEXIST));
1273                 /* start from $ORIGIN@$ORIGIN, if supported */
1274                 drba->drba_snapobj = dp->dp_origin_snap != NULL ?
1275                     dp->dp_origin_snap->ds_object : 0;
1276         }
1277
1278         return (0);
1279
1280 }
1281
1282 static int
1283 dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
1284 {
1285         dmu_recv_begin_arg_t *drba = arg;
1286         dsl_pool_t *dp = dmu_tx_pool(tx);
1287         struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1288         uint64_t fromguid = drrb->drr_fromguid;
1289         int flags = drrb->drr_flags;
1290         int error;
1291         uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1292         dsl_dataset_t *ds;
1293         const char *tofs = drba->drba_cookie->drc_tofs;
1294
1295         /* already checked */
1296         ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1297         ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
1298
1299         if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1300             DMU_COMPOUNDSTREAM ||
1301             drrb->drr_type >= DMU_OST_NUMTYPES ||
1302             ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1303                 return (SET_ERROR(EINVAL));
1304
1305         /* Verify pool version supports SA if SA_SPILL feature set */
1306         if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1307             spa_version(dp->dp_spa) < SPA_VERSION_SA)
1308                 return (SET_ERROR(ENOTSUP));
1309
1310         if (drba->drba_cookie->drc_resumable &&
1311             !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
1312                 return (SET_ERROR(ENOTSUP));
1313
1314         /*
1315          * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1316          * record to a plan WRITE record, so the pool must have the
1317          * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1318          * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
1319          */
1320         if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1321             !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1322                 return (SET_ERROR(ENOTSUP));
1323         if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1324             !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1325                 return (SET_ERROR(ENOTSUP));
1326
1327         /*
1328          * The receiving code doesn't know how to translate large blocks
1329          * to smaller ones, so the pool must have the LARGE_BLOCKS
1330          * feature enabled if the stream has LARGE_BLOCKS.
1331          */
1332         if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1333             !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1334                 return (SET_ERROR(ENOTSUP));
1335
1336         /*
1337          * The receiving code doesn't know how to translate large dnodes
1338          * to smaller ones, so the pool must have the LARGE_DNODE
1339          * feature enabled if the stream has LARGE_DNODE.
1340          */
1341         if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
1342             !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
1343                 return (SET_ERROR(ENOTSUP));
1344
1345         error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1346         if (error == 0) {
1347                 /* target fs already exists; recv into temp clone */
1348
1349                 /* Can't recv a clone into an existing fs */
1350                 if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
1351                         dsl_dataset_rele(ds, FTAG);
1352                         return (SET_ERROR(EINVAL));
1353                 }
1354
1355                 error = recv_begin_check_existing_impl(drba, ds, fromguid);
1356                 dsl_dataset_rele(ds, FTAG);
1357         } else if (error == ENOENT) {
1358                 /* target fs does not exist; must be a full backup or clone */
1359                 char buf[ZFS_MAX_DATASET_NAME_LEN];
1360
1361                 /*
1362                  * If it's a non-clone incremental, we are missing the
1363                  * target fs, so fail the recv.
1364                  */
1365                 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1366                     drba->drba_origin))
1367                         return (SET_ERROR(ENOENT));
1368
1369                 /*
1370                  * If we're receiving a full send as a clone, and it doesn't
1371                  * contain all the necessary free records and freeobject
1372                  * records, reject it.
1373                  */
1374                 if (fromguid == 0 && drba->drba_origin &&
1375                     !(flags & DRR_FLAG_FREERECORDS))
1376                         return (SET_ERROR(EINVAL));
1377
1378                 /* Open the parent of tofs */
1379                 ASSERT3U(strlen(tofs), <, sizeof (buf));
1380                 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
1381                 error = dsl_dataset_hold(dp, buf, FTAG, &ds);
1382                 if (error != 0)
1383                         return (error);
1384
1385                 /*
1386                  * Check filesystem and snapshot limits before receiving. We'll
1387                  * recheck snapshot limits again at the end (we create the
1388                  * filesystems and increment those counts during begin_sync).
1389                  */
1390                 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1391                     ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1392                 if (error != 0) {
1393                         dsl_dataset_rele(ds, FTAG);
1394                         return (error);
1395                 }
1396
1397                 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1398                     ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1399                 if (error != 0) {
1400                         dsl_dataset_rele(ds, FTAG);
1401                         return (error);
1402                 }
1403
1404                 if (drba->drba_origin != NULL) {
1405                         dsl_dataset_t *origin;
1406                         error = dsl_dataset_hold(dp, drba->drba_origin,
1407                             FTAG, &origin);
1408                         if (error != 0) {
1409                                 dsl_dataset_rele(ds, FTAG);
1410                                 return (error);
1411                         }
1412                         if (!origin->ds_is_snapshot) {
1413                                 dsl_dataset_rele(origin, FTAG);
1414                                 dsl_dataset_rele(ds, FTAG);
1415                                 return (SET_ERROR(EINVAL));
1416                         }
1417                         if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
1418                             fromguid != 0) {
1419                                 dsl_dataset_rele(origin, FTAG);
1420                                 dsl_dataset_rele(ds, FTAG);
1421                                 return (SET_ERROR(ENODEV));
1422                         }
1423                         dsl_dataset_rele(origin, FTAG);
1424                 }
1425                 dsl_dataset_rele(ds, FTAG);
1426                 error = 0;
1427         }
1428         return (error);
1429 }
1430
1431 static void
1432 dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1433 {
1434         dmu_recv_begin_arg_t *drba = arg;
1435         dsl_pool_t *dp = dmu_tx_pool(tx);
1436         objset_t *mos = dp->dp_meta_objset;
1437         struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1438         const char *tofs = drba->drba_cookie->drc_tofs;
1439         dsl_dataset_t *ds, *newds;
1440         uint64_t dsobj;
1441         int error;
1442         uint64_t crflags = 0;
1443
1444         if (drrb->drr_flags & DRR_FLAG_CI_DATA)
1445                 crflags |= DS_FLAG_CI_DATASET;
1446
1447         error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1448         if (error == 0) {
1449                 /* create temporary clone */
1450                 dsl_dataset_t *snap = NULL;
1451                 if (drba->drba_snapobj != 0) {
1452                         VERIFY0(dsl_dataset_hold_obj(dp,
1453                             drba->drba_snapobj, FTAG, &snap));
1454                 }
1455                 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
1456                     snap, crflags, drba->drba_cred, tx);
1457                 if (drba->drba_snapobj != 0)
1458                         dsl_dataset_rele(snap, FTAG);
1459                 dsl_dataset_rele(ds, FTAG);
1460         } else {
1461                 dsl_dir_t *dd;
1462                 const char *tail;
1463                 dsl_dataset_t *origin = NULL;
1464
1465                 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
1466
1467                 if (drba->drba_origin != NULL) {
1468                         VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
1469                             FTAG, &origin));
1470                 }
1471
1472                 /* Create new dataset. */
1473                 dsobj = dsl_dataset_create_sync(dd,
1474                     strrchr(tofs, '/') + 1,
1475                     origin, crflags, drba->drba_cred, tx);
1476                 if (origin != NULL)
1477                         dsl_dataset_rele(origin, FTAG);
1478                 dsl_dir_rele(dd, FTAG);
1479                 drba->drba_cookie->drc_newfs = B_TRUE;
1480         }
1481         VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
1482
1483         if (drba->drba_cookie->drc_resumable) {
1484                 uint64_t one = 1;
1485                 uint64_t zero = 0;
1486
1487                 dsl_dataset_zapify(newds, tx);
1488                 if (drrb->drr_fromguid != 0) {
1489                         VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
1490                             8, 1, &drrb->drr_fromguid, tx));
1491                 }
1492                 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
1493                     8, 1, &drrb->drr_toguid, tx));
1494                 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
1495                     1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
1496                 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
1497                     8, 1, &one, tx));
1498                 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
1499                     8, 1, &zero, tx));
1500                 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
1501                     8, 1, &zero, tx));
1502                 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1503                     DMU_BACKUP_FEATURE_EMBED_DATA) {
1504                         VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
1505                             8, 1, &one, tx));
1506                 }
1507         }
1508
1509         dmu_buf_will_dirty(newds->ds_dbuf, tx);
1510         dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
1511
1512         /*
1513          * If we actually created a non-clone, we need to create the
1514          * objset in our new dataset.
1515          */
1516         if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1517                 (void) dmu_objset_create_impl(dp->dp_spa,
1518                     newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1519         }
1520
1521         drba->drba_cookie->drc_ds = newds;
1522
1523         spa_history_log_internal_ds(newds, "receive", tx, "");
1524 }
1525
1526 static int
1527 dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1528 {
1529         dmu_recv_begin_arg_t *drba = arg;
1530         dsl_pool_t *dp = dmu_tx_pool(tx);
1531         struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1532         int error;
1533         uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1534         dsl_dataset_t *ds;
1535         const char *tofs = drba->drba_cookie->drc_tofs;
1536         uint64_t val;
1537
1538         /* 6 extra bytes for /%recv */
1539         char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1540
1541         /* already checked */
1542         ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1543         ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
1544
1545         if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1546             DMU_COMPOUNDSTREAM ||
1547             drrb->drr_type >= DMU_OST_NUMTYPES)
1548                 return (SET_ERROR(EINVAL));
1549
1550         /* Verify pool version supports SA if SA_SPILL feature set */
1551         if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1552             spa_version(dp->dp_spa) < SPA_VERSION_SA)
1553                 return (SET_ERROR(ENOTSUP));
1554
1555         /*
1556          * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1557          * record to a plain WRITE record, so the pool must have the
1558          * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1559          * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
1560          */
1561         if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1562             !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1563                 return (SET_ERROR(ENOTSUP));
1564         if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1565             !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1566                 return (SET_ERROR(ENOTSUP));
1567
1568         (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1569             tofs, recv_clone_name);
1570
1571         if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1572                 /* %recv does not exist; continue in tofs */
1573                 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1574                 if (error != 0)
1575                         return (error);
1576         }
1577
1578         /* check that ds is marked inconsistent */
1579         if (!DS_IS_INCONSISTENT(ds)) {
1580                 dsl_dataset_rele(ds, FTAG);
1581                 return (SET_ERROR(EINVAL));
1582         }
1583
1584         /* check that there is resuming data, and that the toguid matches */
1585         if (!dsl_dataset_is_zapified(ds)) {
1586                 dsl_dataset_rele(ds, FTAG);
1587                 return (SET_ERROR(EINVAL));
1588         }
1589         error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1590             DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1591         if (error != 0 || drrb->drr_toguid != val) {
1592                 dsl_dataset_rele(ds, FTAG);
1593                 return (SET_ERROR(EINVAL));
1594         }
1595
1596         /*
1597          * Check if the receive is still running.  If so, it will be owned.
1598          * Note that nothing else can own the dataset (e.g. after the receive
1599          * fails) because it will be marked inconsistent.
1600          */
1601         if (dsl_dataset_has_owner(ds)) {
1602                 dsl_dataset_rele(ds, FTAG);
1603                 return (SET_ERROR(EBUSY));
1604         }
1605
1606         /* There should not be any snapshots of this fs yet. */
1607         if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1608                 dsl_dataset_rele(ds, FTAG);
1609                 return (SET_ERROR(EINVAL));
1610         }
1611
1612         /*
1613          * Note: resume point will be checked when we process the first WRITE
1614          * record.
1615          */
1616
1617         /* check that the origin matches */
1618         val = 0;
1619         (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1620             DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1621         if (drrb->drr_fromguid != val) {
1622                 dsl_dataset_rele(ds, FTAG);
1623                 return (SET_ERROR(EINVAL));
1624         }
1625
1626         dsl_dataset_rele(ds, FTAG);
1627         return (0);
1628 }
1629
1630 static void
1631 dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1632 {
1633         dmu_recv_begin_arg_t *drba = arg;
1634         dsl_pool_t *dp = dmu_tx_pool(tx);
1635         const char *tofs = drba->drba_cookie->drc_tofs;
1636         dsl_dataset_t *ds;
1637         uint64_t dsobj;
1638         /* 6 extra bytes for /%recv */
1639         char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1640
1641         (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1642             tofs, recv_clone_name);
1643
1644         if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1645                 /* %recv does not exist; continue in tofs */
1646                 VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
1647                 drba->drba_cookie->drc_newfs = B_TRUE;
1648         }
1649
1650         /* clear the inconsistent flag so that we can own it */
1651         ASSERT(DS_IS_INCONSISTENT(ds));
1652         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1653         dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
1654         dsobj = ds->ds_object;
1655         dsl_dataset_rele(ds, FTAG);
1656
1657         VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
1658
1659         dmu_buf_will_dirty(ds->ds_dbuf, tx);
1660         dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1661
1662         ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
1663
1664         drba->drba_cookie->drc_ds = ds;
1665
1666         spa_history_log_internal_ds(ds, "resume receive", tx, "");
1667 }
1668
1669 /*
1670  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1671  * succeeds; otherwise we will leak the holds on the datasets.
1672  */
1673 int
1674 dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
1675     boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1676 {
1677         dmu_recv_begin_arg_t drba = { 0 };
1678
1679         bzero(drc, sizeof (dmu_recv_cookie_t));
1680         drc->drc_drr_begin = drr_begin;
1681         drc->drc_drrb = &drr_begin->drr_u.drr_begin;
1682         drc->drc_tosnap = tosnap;
1683         drc->drc_tofs = tofs;
1684         drc->drc_force = force;
1685         drc->drc_resumable = resumable;
1686         drc->drc_cred = CRED();
1687
1688         if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1689                 drc->drc_byteswap = B_TRUE;
1690                 fletcher_4_incremental_byteswap(drr_begin,
1691                     sizeof (dmu_replay_record_t), &drc->drc_cksum);
1692                 byteswap_record(drr_begin);
1693         } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1694                 fletcher_4_incremental_native(drr_begin,
1695                     sizeof (dmu_replay_record_t), &drc->drc_cksum);
1696         } else {
1697                 return (SET_ERROR(EINVAL));
1698         }
1699
1700         drba.drba_origin = origin;
1701         drba.drba_cookie = drc;
1702         drba.drba_cred = CRED();
1703
1704         if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1705             DMU_BACKUP_FEATURE_RESUMING) {
1706                 return (dsl_sync_task(tofs,
1707                     dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
1708                     &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1709         } else  {
1710                 return (dsl_sync_task(tofs,
1711                     dmu_recv_begin_check, dmu_recv_begin_sync,
1712                     &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1713         }
1714 }
1715
1716 struct receive_record_arg {
1717         dmu_replay_record_t header;
1718         void *payload; /* Pointer to a buffer containing the payload */
1719         /*
1720          * If the record is a write, pointer to the arc_buf_t containing the
1721          * payload.
1722          */
1723         arc_buf_t *write_buf;
1724         int payload_size;
1725         uint64_t bytes_read; /* bytes read from stream when record created */
1726         boolean_t eos_marker; /* Marks the end of the stream */
1727         bqueue_node_t node;
1728 };
1729
1730 struct receive_writer_arg {
1731         objset_t *os;
1732         boolean_t byteswap;
1733         bqueue_t q;
1734
1735         /*
1736          * These three args are used to signal to the main thread that we're
1737          * done.
1738          */
1739         kmutex_t mutex;
1740         kcondvar_t cv;
1741         boolean_t done;
1742
1743         int err;
1744         /* A map from guid to dataset to help handle dedup'd streams. */
1745         avl_tree_t *guid_to_ds_map;
1746         boolean_t resumable;
1747         uint64_t last_object, last_offset;
1748         uint64_t bytes_read; /* bytes read when current record created */
1749 };
1750
1751 struct objlist {
1752         list_t list; /* List of struct receive_objnode. */
1753         /*
1754          * Last object looked up. Used to assert that objects are being looked
1755          * up in ascending order.
1756          */
1757         uint64_t last_lookup;
1758 };
1759
1760 struct receive_objnode {
1761         list_node_t node;
1762         uint64_t object;
1763 };
1764
1765 struct receive_arg  {
1766         objset_t *os;
1767         vnode_t *vp; /* The vnode to read the stream from */
1768         uint64_t voff; /* The current offset in the stream */
1769         uint64_t bytes_read;
1770         /*
1771          * A record that has had its payload read in, but hasn't yet been handed
1772          * off to the worker thread.
1773          */
1774         struct receive_record_arg *rrd;
1775         /* A record that has had its header read in, but not its payload. */
1776         struct receive_record_arg *next_rrd;
1777         zio_cksum_t cksum;
1778         zio_cksum_t prev_cksum;
1779         int err;
1780         boolean_t byteswap;
1781         /* Sorted list of objects not to issue prefetches for. */
1782         struct objlist ignore_objlist;
1783 };
1784
1785 typedef struct guid_map_entry {
1786         uint64_t        guid;
1787         dsl_dataset_t   *gme_ds;
1788         avl_node_t      avlnode;
1789 } guid_map_entry_t;
1790
1791 static int
1792 guid_compare(const void *arg1, const void *arg2)
1793 {
1794         const guid_map_entry_t *gmep1 = arg1;
1795         const guid_map_entry_t *gmep2 = arg2;
1796
1797         if (gmep1->guid < gmep2->guid)
1798                 return (-1);
1799         else if (gmep1->guid > gmep2->guid)
1800                 return (1);
1801         return (0);
1802 }
1803
1804 static void
1805 free_guid_map_onexit(void *arg)
1806 {
1807         avl_tree_t *ca = arg;
1808         void *cookie = NULL;
1809         guid_map_entry_t *gmep;
1810
1811         while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
1812                 dsl_dataset_long_rele(gmep->gme_ds, gmep);
1813                 dsl_dataset_rele(gmep->gme_ds, gmep);
1814                 kmem_free(gmep, sizeof (guid_map_entry_t));
1815         }
1816         avl_destroy(ca);
1817         kmem_free(ca, sizeof (avl_tree_t));
1818 }
1819
1820 static int
1821 receive_read(struct receive_arg *ra, int len, void *buf)
1822 {
1823         int done = 0;
1824
1825         /*
1826          * The code doesn't rely on this (lengths being multiples of 8).  See
1827          * comment in dump_bytes.
1828          */
1829         ASSERT0(len % 8);
1830
1831         while (done < len) {
1832                 ssize_t resid;
1833
1834                 ra->err = vn_rdwr(UIO_READ, ra->vp,
1835                     (char *)buf + done, len - done,
1836                     ra->voff, UIO_SYSSPACE, FAPPEND,
1837                     RLIM64_INFINITY, CRED(), &resid);
1838
1839                 if (resid == len - done) {
1840                         /*
1841                          * Note: ECKSUM indicates that the receive
1842                          * was interrupted and can potentially be resumed.
1843                          */
1844                         ra->err = SET_ERROR(ECKSUM);
1845                 }
1846                 ra->voff += len - done - resid;
1847                 done = len - resid;
1848                 if (ra->err != 0)
1849                         return (ra->err);
1850         }
1851
1852         ra->bytes_read += len;
1853
1854         ASSERT3U(done, ==, len);
1855         return (0);
1856 }
1857
1858 noinline static void
1859 byteswap_record(dmu_replay_record_t *drr)
1860 {
1861 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1862 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1863         drr->drr_type = BSWAP_32(drr->drr_type);
1864         drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
1865
1866         switch (drr->drr_type) {
1867         case DRR_BEGIN:
1868                 DO64(drr_begin.drr_magic);
1869                 DO64(drr_begin.drr_versioninfo);
1870                 DO64(drr_begin.drr_creation_time);
1871                 DO32(drr_begin.drr_type);
1872                 DO32(drr_begin.drr_flags);
1873                 DO64(drr_begin.drr_toguid);
1874                 DO64(drr_begin.drr_fromguid);
1875                 break;
1876         case DRR_OBJECT:
1877                 DO64(drr_object.drr_object);
1878                 DO32(drr_object.drr_type);
1879                 DO32(drr_object.drr_bonustype);
1880                 DO32(drr_object.drr_blksz);
1881                 DO32(drr_object.drr_bonuslen);
1882                 DO64(drr_object.drr_toguid);
1883                 break;
1884         case DRR_FREEOBJECTS:
1885                 DO64(drr_freeobjects.drr_firstobj);
1886                 DO64(drr_freeobjects.drr_numobjs);
1887                 DO64(drr_freeobjects.drr_toguid);
1888                 break;
1889         case DRR_WRITE:
1890                 DO64(drr_write.drr_object);
1891                 DO32(drr_write.drr_type);
1892                 DO64(drr_write.drr_offset);
1893                 DO64(drr_write.drr_length);
1894                 DO64(drr_write.drr_toguid);
1895                 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
1896                 DO64(drr_write.drr_key.ddk_prop);
1897                 break;
1898         case DRR_WRITE_BYREF:
1899                 DO64(drr_write_byref.drr_object);
1900                 DO64(drr_write_byref.drr_offset);
1901                 DO64(drr_write_byref.drr_length);
1902                 DO64(drr_write_byref.drr_toguid);
1903                 DO64(drr_write_byref.drr_refguid);
1904                 DO64(drr_write_byref.drr_refobject);
1905                 DO64(drr_write_byref.drr_refoffset);
1906                 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
1907                     drr_key.ddk_cksum);
1908                 DO64(drr_write_byref.drr_key.ddk_prop);
1909                 break;
1910         case DRR_WRITE_EMBEDDED:
1911                 DO64(drr_write_embedded.drr_object);
1912                 DO64(drr_write_embedded.drr_offset);
1913                 DO64(drr_write_embedded.drr_length);
1914                 DO64(drr_write_embedded.drr_toguid);
1915                 DO32(drr_write_embedded.drr_lsize);
1916                 DO32(drr_write_embedded.drr_psize);
1917                 break;
1918         case DRR_FREE:
1919                 DO64(drr_free.drr_object);
1920                 DO64(drr_free.drr_offset);
1921                 DO64(drr_free.drr_length);
1922                 DO64(drr_free.drr_toguid);
1923                 break;
1924         case DRR_SPILL:
1925                 DO64(drr_spill.drr_object);
1926                 DO64(drr_spill.drr_length);
1927                 DO64(drr_spill.drr_toguid);
1928                 break;
1929         case DRR_END:
1930                 DO64(drr_end.drr_toguid);
1931                 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1932                 break;
1933         default:
1934                 break;
1935         }
1936
1937         if (drr->drr_type != DRR_BEGIN) {
1938                 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
1939         }
1940
1941 #undef DO64
1942 #undef DO32
1943 }
1944
1945 static inline uint8_t
1946 deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1947 {
1948         if (bonus_type == DMU_OT_SA) {
1949                 return (1);
1950         } else {
1951                 return (1 +
1952                     ((DN_OLD_MAX_BONUSLEN -
1953                     MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT));
1954         }
1955 }
1956
1957 static void
1958 save_resume_state(struct receive_writer_arg *rwa,
1959     uint64_t object, uint64_t offset, dmu_tx_t *tx)
1960 {
1961         int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1962
1963         if (!rwa->resumable)
1964                 return;
1965
1966         /*
1967          * We use ds_resume_bytes[] != 0 to indicate that we need to
1968          * update this on disk, so it must not be 0.
1969          */
1970         ASSERT(rwa->bytes_read != 0);
1971
1972         /*
1973          * We only resume from write records, which have a valid
1974          * (non-meta-dnode) object number.
1975          */
1976         ASSERT(object != 0);
1977
1978         /*
1979          * For resuming to work correctly, we must receive records in order,
1980          * sorted by object,offset.  This is checked by the callers, but
1981          * assert it here for good measure.
1982          */
1983         ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
1984         ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
1985             offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
1986         ASSERT3U(rwa->bytes_read, >=,
1987             rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
1988
1989         rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
1990         rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
1991         rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
1992 }
1993
1994 noinline static int
1995 receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1996         void *data)
1997 {
1998         dmu_object_info_t doi;
1999         dmu_tx_t *tx;
2000         uint64_t object;
2001         int err;
2002
2003         if (drro->drr_type == DMU_OT_NONE ||
2004             !DMU_OT_IS_VALID(drro->drr_type) ||
2005             !DMU_OT_IS_VALID(drro->drr_bonustype) ||
2006             drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
2007             drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
2008             P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
2009             drro->drr_blksz < SPA_MINBLOCKSIZE ||
2010             drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
2011             drro->drr_bonuslen >
2012             DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa->os)))) {
2013                 return (SET_ERROR(EINVAL));
2014         }
2015
2016         err = dmu_object_info(rwa->os, drro->drr_object, &doi);
2017
2018         if (err != 0 && err != ENOENT)
2019                 return (SET_ERROR(EINVAL));
2020         object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
2021
2022         /*
2023          * If we are losing blkptrs or changing the block size this must
2024          * be a new file instance.  We must clear out the previous file
2025          * contents before we can change this type of metadata in the dnode.
2026          */
2027         if (err == 0) {
2028                 int nblkptr;
2029
2030                 nblkptr = deduce_nblkptr(drro->drr_bonustype,
2031                     drro->drr_bonuslen);
2032
2033                 if (drro->drr_blksz != doi.doi_data_block_size ||
2034                     nblkptr < doi.doi_nblkptr) {
2035                         err = dmu_free_long_range(rwa->os, drro->drr_object,
2036                             0, DMU_OBJECT_END);
2037                         if (err != 0)
2038                                 return (SET_ERROR(EINVAL));
2039                 }
2040         }
2041
2042         tx = dmu_tx_create(rwa->os);
2043         dmu_tx_hold_bonus(tx, object);
2044         err = dmu_tx_assign(tx, TXG_WAIT);
2045         if (err != 0) {
2046                 dmu_tx_abort(tx);
2047                 return (err);
2048         }
2049
2050         if (object == DMU_NEW_OBJECT) {
2051                 /* currently free, want to be allocated */
2052                 err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
2053                     drro->drr_type, drro->drr_blksz,
2054                     drro->drr_bonustype, drro->drr_bonuslen,
2055                     drro->drr_dn_slots << DNODE_SHIFT, tx);
2056         } else if (drro->drr_type != doi.doi_type ||
2057             drro->drr_blksz != doi.doi_data_block_size ||
2058             drro->drr_bonustype != doi.doi_bonus_type ||
2059             drro->drr_bonuslen != doi.doi_bonus_size) {
2060                 /* currently allocated, but with different properties */
2061                 err = dmu_object_reclaim(rwa->os, drro->drr_object,
2062                     drro->drr_type, drro->drr_blksz,
2063                     drro->drr_bonustype, drro->drr_bonuslen, tx);
2064         }
2065         if (err != 0) {
2066                 dmu_tx_commit(tx);
2067                 return (SET_ERROR(EINVAL));
2068         }
2069
2070         dmu_object_set_checksum(rwa->os, drro->drr_object,
2071             drro->drr_checksumtype, tx);
2072         dmu_object_set_compress(rwa->os, drro->drr_object,
2073             drro->drr_compress, tx);
2074
2075         if (data != NULL) {
2076                 dmu_buf_t *db;
2077
2078                 VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
2079                 dmu_buf_will_dirty(db, tx);
2080
2081                 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
2082                 bcopy(data, db->db_data, drro->drr_bonuslen);
2083                 if (rwa->byteswap) {
2084                         dmu_object_byteswap_t byteswap =
2085                             DMU_OT_BYTESWAP(drro->drr_bonustype);
2086                         dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2087                             drro->drr_bonuslen);
2088                 }
2089                 dmu_buf_rele(db, FTAG);
2090         }
2091         dmu_tx_commit(tx);
2092
2093         return (0);
2094 }
2095
2096 /* ARGSUSED */
2097 noinline static int
2098 receive_freeobjects(struct receive_writer_arg *rwa,
2099     struct drr_freeobjects *drrfo)
2100 {
2101         uint64_t obj;
2102         int next_err = 0;
2103
2104         if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2105                 return (SET_ERROR(EINVAL));
2106
2107         for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
2108             obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
2109             next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2110                 dmu_object_info_t doi;
2111                 int err;
2112
2113                 err = dmu_object_info(rwa->os, obj, &doi);
2114                 if (err == ENOENT) {
2115                         obj++;
2116                         continue;
2117                 } else if (err != 0) {
2118                         return (err);
2119                 }
2120
2121                 err = dmu_free_long_object(rwa->os, obj);
2122                 if (err != 0)
2123                         return (err);
2124         }
2125         if (next_err != ESRCH)
2126                 return (next_err);
2127         return (0);
2128 }
2129
2130 noinline static int
2131 receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2132         arc_buf_t *abuf)
2133 {
2134         dmu_tx_t *tx;
2135         dmu_buf_t *bonus;
2136         int err;
2137
2138         if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
2139             !DMU_OT_IS_VALID(drrw->drr_type))
2140                 return (SET_ERROR(EINVAL));
2141
2142         /*
2143          * For resuming to work, records must be in increasing order
2144          * by (object, offset).
2145          */
2146         if (drrw->drr_object < rwa->last_object ||
2147             (drrw->drr_object == rwa->last_object &&
2148             drrw->drr_offset < rwa->last_offset)) {
2149                 return (SET_ERROR(EINVAL));
2150         }
2151         rwa->last_object = drrw->drr_object;
2152         rwa->last_offset = drrw->drr_offset;
2153
2154         if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
2155                 return (SET_ERROR(EINVAL));
2156
2157         tx = dmu_tx_create(rwa->os);
2158
2159         dmu_tx_hold_write(tx, drrw->drr_object,
2160             drrw->drr_offset, drrw->drr_length);
2161         err = dmu_tx_assign(tx, TXG_WAIT);
2162         if (err != 0) {
2163                 dmu_tx_abort(tx);
2164                 return (err);
2165         }
2166         if (rwa->byteswap) {
2167                 dmu_object_byteswap_t byteswap =
2168                     DMU_OT_BYTESWAP(drrw->drr_type);
2169                 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
2170                     drrw->drr_length);
2171         }
2172
2173         if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
2174                 return (SET_ERROR(EINVAL));
2175         dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
2176
2177         /*
2178          * Note: If the receive fails, we want the resume stream to start
2179          * with the same record that we last successfully received (as opposed
2180          * to the next record), so that we can verify that we are
2181          * resuming from the correct location.
2182          */
2183         save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2184         dmu_tx_commit(tx);
2185         dmu_buf_rele(bonus, FTAG);
2186
2187         return (0);
2188 }
2189
2190 /*
2191  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
2192  * streams to refer to a copy of the data that is already on the
2193  * system because it came in earlier in the stream.  This function
2194  * finds the earlier copy of the data, and uses that copy instead of
2195  * data from the stream to fulfill this write.
2196  */
2197 static int
2198 receive_write_byref(struct receive_writer_arg *rwa,
2199     struct drr_write_byref *drrwbr)
2200 {
2201         dmu_tx_t *tx;
2202         int err;
2203         guid_map_entry_t gmesrch;
2204         guid_map_entry_t *gmep;
2205         avl_index_t where;
2206         objset_t *ref_os = NULL;
2207         dmu_buf_t *dbp;
2208
2209         if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2210                 return (SET_ERROR(EINVAL));
2211
2212         /*
2213          * If the GUID of the referenced dataset is different from the
2214          * GUID of the target dataset, find the referenced dataset.
2215          */
2216         if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
2217                 gmesrch.guid = drrwbr->drr_refguid;
2218                 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
2219                     &where)) == NULL) {
2220                         return (SET_ERROR(EINVAL));
2221                 }
2222                 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2223                         return (SET_ERROR(EINVAL));
2224         } else {
2225                 ref_os = rwa->os;
2226         }
2227
2228         err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
2229             drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
2230         if (err != 0)
2231                 return (err);
2232
2233         tx = dmu_tx_create(rwa->os);
2234
2235         dmu_tx_hold_write(tx, drrwbr->drr_object,
2236             drrwbr->drr_offset, drrwbr->drr_length);
2237         err = dmu_tx_assign(tx, TXG_WAIT);
2238         if (err != 0) {
2239                 dmu_tx_abort(tx);
2240                 return (err);
2241         }
2242         dmu_write(rwa->os, drrwbr->drr_object,
2243             drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
2244         dmu_buf_rele(dbp, FTAG);
2245
2246         /* See comment in restore_write. */
2247         save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
2248         dmu_tx_commit(tx);
2249         return (0);
2250 }
2251
2252 static int
2253 receive_write_embedded(struct receive_writer_arg *rwa,
2254     struct drr_write_embedded *drrwe, void *data)
2255 {
2256         dmu_tx_t *tx;
2257         int err;
2258
2259         if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
2260                 return (EINVAL);
2261
2262         if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
2263                 return (EINVAL);
2264
2265         if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
2266                 return (EINVAL);
2267         if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
2268                 return (EINVAL);
2269
2270         tx = dmu_tx_create(rwa->os);
2271
2272         dmu_tx_hold_write(tx, drrwe->drr_object,
2273             drrwe->drr_offset, drrwe->drr_length);
2274         err = dmu_tx_assign(tx, TXG_WAIT);
2275         if (err != 0) {
2276                 dmu_tx_abort(tx);
2277                 return (err);
2278         }
2279
2280         dmu_write_embedded(rwa->os, drrwe->drr_object,
2281             drrwe->drr_offset, data, drrwe->drr_etype,
2282             drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2283             rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
2284
2285         /* See comment in restore_write. */
2286         save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
2287         dmu_tx_commit(tx);
2288         return (0);
2289 }
2290
2291 static int
2292 receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2293     void *data)
2294 {
2295         dmu_tx_t *tx;
2296         dmu_buf_t *db, *db_spill;
2297         int err;
2298
2299         if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2300             drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2301                 return (SET_ERROR(EINVAL));
2302
2303         if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2304                 return (SET_ERROR(EINVAL));
2305
2306         VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
2307         if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
2308                 dmu_buf_rele(db, FTAG);
2309                 return (err);
2310         }
2311
2312         tx = dmu_tx_create(rwa->os);
2313
2314         dmu_tx_hold_spill(tx, db->db_object);
2315
2316         err = dmu_tx_assign(tx, TXG_WAIT);
2317         if (err != 0) {
2318                 dmu_buf_rele(db, FTAG);
2319                 dmu_buf_rele(db_spill, FTAG);
2320                 dmu_tx_abort(tx);
2321                 return (err);
2322         }
2323         dmu_buf_will_dirty(db_spill, tx);
2324
2325         if (db_spill->db_size < drrs->drr_length)
2326                 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
2327                     drrs->drr_length, tx));
2328         bcopy(data, db_spill->db_data, drrs->drr_length);
2329
2330         dmu_buf_rele(db, FTAG);
2331         dmu_buf_rele(db_spill, FTAG);
2332
2333         dmu_tx_commit(tx);
2334         return (0);
2335 }
2336
2337 /* ARGSUSED */
2338 noinline static int
2339 receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2340 {
2341         int err;
2342
2343         if (drrf->drr_length != -1ULL &&
2344             drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2345                 return (SET_ERROR(EINVAL));
2346
2347         if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2348                 return (SET_ERROR(EINVAL));
2349
2350         err = dmu_free_long_range(rwa->os, drrf->drr_object,
2351             drrf->drr_offset, drrf->drr_length);
2352
2353         return (err);
2354 }
2355
2356 /* used to destroy the drc_ds on error */
2357 static void
2358 dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
2359 {
2360         if (drc->drc_resumable) {
2361                 /* wait for our resume state to be written to disk */
2362                 txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
2363                 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2364         } else {
2365                 char name[ZFS_MAX_DATASET_NAME_LEN];
2366                 dsl_dataset_name(drc->drc_ds, name);
2367                 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2368                 (void) dsl_destroy_head(name);
2369         }
2370 }
2371
2372 static void
2373 receive_cksum(struct receive_arg *ra, int len, void *buf)
2374 {
2375         if (ra->byteswap) {
2376                 fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
2377         } else {
2378                 fletcher_4_incremental_native(buf, len, &ra->cksum);
2379         }
2380 }
2381
2382 /*
2383  * Read the payload into a buffer of size len, and update the current record's
2384  * payload field.
2385  * Allocate ra->next_rrd and read the next record's header into
2386  * ra->next_rrd->header.
2387  * Verify checksum of payload and next record.
2388  */
2389 static int
2390 receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
2391 {
2392         int err;
2393         zio_cksum_t cksum_orig;
2394         zio_cksum_t *cksump;
2395
2396         if (len != 0) {
2397                 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2398                 err = receive_read(ra, len, buf);
2399                 if (err != 0)
2400                         return (err);
2401                 receive_cksum(ra, len, buf);
2402
2403                 /* note: rrd is NULL when reading the begin record's payload */
2404                 if (ra->rrd != NULL) {
2405                         ra->rrd->payload = buf;
2406                         ra->rrd->payload_size = len;
2407                         ra->rrd->bytes_read = ra->bytes_read;
2408                 }
2409         }
2410
2411         ra->prev_cksum = ra->cksum;
2412
2413         ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2414         err = receive_read(ra, sizeof (ra->next_rrd->header),
2415             &ra->next_rrd->header);
2416         ra->next_rrd->bytes_read = ra->bytes_read;
2417         if (err != 0) {
2418                 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2419                 ra->next_rrd = NULL;
2420                 return (err);
2421         }
2422         if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2423                 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2424                 ra->next_rrd = NULL;
2425                 return (SET_ERROR(EINVAL));
2426         }
2427
2428         /*
2429          * Note: checksum is of everything up to but not including the
2430          * checksum itself.
2431          */
2432         ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2433             ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2434         receive_cksum(ra,
2435             offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2436             &ra->next_rrd->header);
2437
2438         cksum_orig = ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2439         cksump = &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2440
2441         if (ra->byteswap)
2442                 byteswap_record(&ra->next_rrd->header);
2443
2444         if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2445             !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2446                 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2447                 ra->next_rrd = NULL;
2448                 return (SET_ERROR(ECKSUM));
2449         }
2450
2451         receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
2452
2453         return (0);
2454 }
2455
2456 static void
2457 objlist_create(struct objlist *list)
2458 {
2459         list_create(&list->list, sizeof (struct receive_objnode),
2460             offsetof(struct receive_objnode, node));
2461         list->last_lookup = 0;
2462 }
2463
2464 static void
2465 objlist_destroy(struct objlist *list)
2466 {
2467         struct receive_objnode *n;
2468
2469         for (n = list_remove_head(&list->list);
2470             n != NULL; n = list_remove_head(&list->list)) {
2471                 kmem_free(n, sizeof (*n));
2472         }
2473         list_destroy(&list->list);
2474 }
2475
2476 /*
2477  * This function looks through the objlist to see if the specified object number
2478  * is contained in the objlist.  In the process, it will remove all object
2479  * numbers in the list that are smaller than the specified object number.  Thus,
2480  * any lookup of an object number smaller than a previously looked up object
2481  * number will always return false; therefore, all lookups should be done in
2482  * ascending order.
2483  */
2484 static boolean_t
2485 objlist_exists(struct objlist *list, uint64_t object)
2486 {
2487         struct receive_objnode *node = list_head(&list->list);
2488         ASSERT3U(object, >=, list->last_lookup);
2489         list->last_lookup = object;
2490         while (node != NULL && node->object < object) {
2491                 VERIFY3P(node, ==, list_remove_head(&list->list));
2492                 kmem_free(node, sizeof (*node));
2493                 node = list_head(&list->list);
2494         }
2495         return (node != NULL && node->object == object);
2496 }
2497
2498 /*
2499  * The objlist is a list of object numbers stored in ascending order.  However,
2500  * the insertion of new object numbers does not seek out the correct location to
2501  * store a new object number; instead, it appends it to the list for simplicity.
2502  * Thus, any users must take care to only insert new object numbers in ascending
2503  * order.
2504  */
2505 static void
2506 objlist_insert(struct objlist *list, uint64_t object)
2507 {
2508         struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
2509         node->object = object;
2510 #ifdef ZFS_DEBUG
2511         {
2512         struct receive_objnode *last_object = list_tail(&list->list);
2513         uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
2514         ASSERT3U(node->object, >, last_objnum);
2515         }
2516 #endif
2517         list_insert_tail(&list->list, node);
2518 }
2519
2520 /*
2521  * Issue the prefetch reads for any necessary indirect blocks.
2522  *
2523  * We use the object ignore list to tell us whether or not to issue prefetches
2524  * for a given object.  We do this for both correctness (in case the blocksize
2525  * of an object has changed) and performance (if the object doesn't exist, don't
2526  * needlessly try to issue prefetches).  We also trim the list as we go through
2527  * the stream to prevent it from growing to an unbounded size.
2528  *
2529  * The object numbers within will always be in sorted order, and any write
2530  * records we see will also be in sorted order, but they're not sorted with
2531  * respect to each other (i.e. we can get several object records before
2532  * receiving each object's write records).  As a result, once we've reached a
2533  * given object number, we can safely remove any reference to lower object
2534  * numbers in the ignore list. In practice, we receive up to 32 object records
2535  * before receiving write records, so the list can have up to 32 nodes in it.
2536  */
2537 /* ARGSUSED */
2538 static void
2539 receive_read_prefetch(struct receive_arg *ra,
2540     uint64_t object, uint64_t offset, uint64_t length)
2541 {
2542         if (!objlist_exists(&ra->ignore_objlist, object)) {
2543                 dmu_prefetch(ra->os, object, 1, offset, length,
2544                     ZIO_PRIORITY_SYNC_READ);
2545         }
2546 }
2547
2548 /*
2549  * Read records off the stream, issuing any necessary prefetches.
2550  */
2551 static int
2552 receive_read_record(struct receive_arg *ra)
2553 {
2554         int err;
2555
2556         switch (ra->rrd->header.drr_type) {
2557         case DRR_OBJECT:
2558         {
2559                 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2560                 uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2561                 void *buf = kmem_zalloc(size, KM_SLEEP);
2562                 dmu_object_info_t doi;
2563                 err = receive_read_payload_and_next_header(ra, size, buf);
2564                 if (err != 0) {
2565                         kmem_free(buf, size);
2566                         return (err);
2567                 }
2568                 err = dmu_object_info(ra->os, drro->drr_object, &doi);
2569                 /*
2570                  * See receive_read_prefetch for an explanation why we're
2571                  * storing this object in the ignore_obj_list.
2572                  */
2573                 if (err == ENOENT ||
2574                     (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2575                         objlist_insert(&ra->ignore_objlist, drro->drr_object);
2576                         err = 0;
2577                 }
2578                 return (err);
2579         }
2580         case DRR_FREEOBJECTS:
2581         {
2582                 err = receive_read_payload_and_next_header(ra, 0, NULL);
2583                 return (err);
2584         }
2585         case DRR_WRITE:
2586         {
2587                 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2588                 arc_buf_t *abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2589                     drrw->drr_length);
2590
2591                 err = receive_read_payload_and_next_header(ra,
2592                     drrw->drr_length, abuf->b_data);
2593                 if (err != 0) {
2594                         dmu_return_arcbuf(abuf);
2595                         return (err);
2596                 }
2597                 ra->rrd->write_buf = abuf;
2598                 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2599                     drrw->drr_length);
2600                 return (err);
2601         }
2602         case DRR_WRITE_BYREF:
2603         {
2604                 struct drr_write_byref *drrwb =
2605                     &ra->rrd->header.drr_u.drr_write_byref;
2606                 err = receive_read_payload_and_next_header(ra, 0, NULL);
2607                 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2608                     drrwb->drr_length);
2609                 return (err);
2610         }
2611         case DRR_WRITE_EMBEDDED:
2612         {
2613                 struct drr_write_embedded *drrwe =
2614                     &ra->rrd->header.drr_u.drr_write_embedded;
2615                 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2616                 void *buf = kmem_zalloc(size, KM_SLEEP);
2617
2618                 err = receive_read_payload_and_next_header(ra, size, buf);
2619                 if (err != 0) {
2620                         kmem_free(buf, size);
2621                         return (err);
2622                 }
2623
2624                 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2625                     drrwe->drr_length);
2626                 return (err);
2627         }
2628         case DRR_FREE:
2629         {
2630                 /*
2631                  * It might be beneficial to prefetch indirect blocks here, but
2632                  * we don't really have the data to decide for sure.
2633                  */
2634                 err = receive_read_payload_and_next_header(ra, 0, NULL);
2635                 return (err);
2636         }
2637         case DRR_END:
2638         {
2639                 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
2640                 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2641                         return (SET_ERROR(ECKSUM));
2642                 return (0);
2643         }
2644         case DRR_SPILL:
2645         {
2646                 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2647                 void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2648                 err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2649                     buf);
2650                 if (err != 0)
2651                         kmem_free(buf, drrs->drr_length);
2652                 return (err);
2653         }
2654         default:
2655                 return (SET_ERROR(EINVAL));
2656         }
2657 }
2658
2659 /*
2660  * Commit the records to the pool.
2661  */
2662 static int
2663 receive_process_record(struct receive_writer_arg *rwa,
2664     struct receive_record_arg *rrd)
2665 {
2666         int err;
2667
2668         /* Processing in order, therefore bytes_read should be increasing. */
2669         ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2670         rwa->bytes_read = rrd->bytes_read;
2671
2672         switch (rrd->header.drr_type) {
2673         case DRR_OBJECT:
2674         {
2675                 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2676                 err = receive_object(rwa, drro, rrd->payload);
2677                 kmem_free(rrd->payload, rrd->payload_size);
2678                 rrd->payload = NULL;
2679                 return (err);
2680         }
2681         case DRR_FREEOBJECTS:
2682         {
2683                 struct drr_freeobjects *drrfo =
2684                     &rrd->header.drr_u.drr_freeobjects;
2685                 return (receive_freeobjects(rwa, drrfo));
2686         }
2687         case DRR_WRITE:
2688         {
2689                 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2690                 err = receive_write(rwa, drrw, rrd->write_buf);
2691                 /* if receive_write() is successful, it consumes the arc_buf */
2692                 if (err != 0)
2693                         dmu_return_arcbuf(rrd->write_buf);
2694                 rrd->write_buf = NULL;
2695                 rrd->payload = NULL;
2696                 return (err);
2697         }
2698         case DRR_WRITE_BYREF:
2699         {
2700                 struct drr_write_byref *drrwbr =
2701                     &rrd->header.drr_u.drr_write_byref;
2702                 return (receive_write_byref(rwa, drrwbr));
2703         }
2704         case DRR_WRITE_EMBEDDED:
2705         {
2706                 struct drr_write_embedded *drrwe =
2707                     &rrd->header.drr_u.drr_write_embedded;
2708                 err = receive_write_embedded(rwa, drrwe, rrd->payload);
2709                 kmem_free(rrd->payload, rrd->payload_size);
2710                 rrd->payload = NULL;
2711                 return (err);
2712         }
2713         case DRR_FREE:
2714         {
2715                 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2716                 return (receive_free(rwa, drrf));
2717         }
2718         case DRR_SPILL:
2719         {
2720                 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2721                 err = receive_spill(rwa, drrs, rrd->payload);
2722                 kmem_free(rrd->payload, rrd->payload_size);
2723                 rrd->payload = NULL;
2724                 return (err);
2725         }
2726         default:
2727                 return (SET_ERROR(EINVAL));
2728         }
2729 }
2730
2731 /*
2732  * dmu_recv_stream's worker thread; pull records off the queue, and then call
2733  * receive_process_record  When we're done, signal the main thread and exit.
2734  */
2735 static void
2736 receive_writer_thread(void *arg)
2737 {
2738         struct receive_writer_arg *rwa = arg;
2739         struct receive_record_arg *rrd;
2740         for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2741             rrd = bqueue_dequeue(&rwa->q)) {
2742                 /*
2743                  * If there's an error, the main thread will stop putting things
2744                  * on the queue, but we need to clear everything in it before we
2745                  * can exit.
2746                  */
2747                 if (rwa->err == 0) {
2748                         rwa->err = receive_process_record(rwa, rrd);
2749                 } else if (rrd->write_buf != NULL) {
2750                         dmu_return_arcbuf(rrd->write_buf);
2751                         rrd->write_buf = NULL;
2752                         rrd->payload = NULL;
2753                 } else if (rrd->payload != NULL) {
2754                         kmem_free(rrd->payload, rrd->payload_size);
2755                         rrd->payload = NULL;
2756                 }
2757                 kmem_free(rrd, sizeof (*rrd));
2758         }
2759         kmem_free(rrd, sizeof (*rrd));
2760         mutex_enter(&rwa->mutex);
2761         rwa->done = B_TRUE;
2762         cv_signal(&rwa->cv);
2763         mutex_exit(&rwa->mutex);
2764 }
2765
2766 static int
2767 resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
2768 {
2769         uint64_t val;
2770         objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
2771         uint64_t dsobj = dmu_objset_id(ra->os);
2772         uint64_t resume_obj, resume_off;
2773
2774         if (nvlist_lookup_uint64(begin_nvl,
2775             "resume_object", &resume_obj) != 0 ||
2776             nvlist_lookup_uint64(begin_nvl,
2777             "resume_offset", &resume_off) != 0) {
2778                 return (SET_ERROR(EINVAL));
2779         }
2780         VERIFY0(zap_lookup(mos, dsobj,
2781             DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
2782         if (resume_obj != val)
2783                 return (SET_ERROR(EINVAL));
2784         VERIFY0(zap_lookup(mos, dsobj,
2785             DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
2786         if (resume_off != val)
2787                 return (SET_ERROR(EINVAL));
2788
2789         return (0);
2790 }
2791
2792 /*
2793  * Read in the stream's records, one by one, and apply them to the pool.  There
2794  * are two threads involved; the thread that calls this function will spin up a
2795  * worker thread, read the records off the stream one by one, and issue
2796  * prefetches for any necessary indirect blocks.  It will then push the records
2797  * onto an internal blocking queue.  The worker thread will pull the records off
2798  * the queue, and actually write the data into the DMU.  This way, the worker
2799  * thread doesn't have to wait for reads to complete, since everything it needs
2800  * (the indirect blocks) will be prefetched.
2801  *
2802  * NB: callers *must* call dmu_recv_end() if this succeeds.
2803  */
2804 int
2805 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
2806     int cleanup_fd, uint64_t *action_handlep)
2807 {
2808         int err = 0;
2809         struct receive_arg *ra;
2810         struct receive_writer_arg *rwa;
2811         int featureflags;
2812         uint32_t payloadlen;
2813         void *payload;
2814         nvlist_t *begin_nvl = NULL;
2815
2816         ra = kmem_zalloc(sizeof (*ra), KM_SLEEP);
2817         rwa = kmem_zalloc(sizeof (*rwa), KM_SLEEP);
2818
2819         ra->byteswap = drc->drc_byteswap;
2820         ra->cksum = drc->drc_cksum;
2821         ra->vp = vp;
2822         ra->voff = *voffp;
2823
2824         if (dsl_dataset_is_zapified(drc->drc_ds)) {
2825                 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
2826                     drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
2827                     sizeof (ra->bytes_read), 1, &ra->bytes_read);
2828         }
2829
2830         objlist_create(&ra->ignore_objlist);
2831
2832         /* these were verified in dmu_recv_begin */
2833         ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
2834             DMU_SUBSTREAM);
2835         ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2836
2837         /*
2838          * Open the objset we are modifying.
2839          */
2840         VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra->os));
2841
2842         ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2843
2844         featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
2845
2846         /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2847         if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2848                 minor_t minor;
2849
2850                 if (cleanup_fd == -1) {
2851                         ra->err = SET_ERROR(EBADF);
2852                         goto out;
2853                 }
2854                 ra->err = zfs_onexit_fd_hold(cleanup_fd, &minor);
2855                 if (ra->err != 0) {
2856                         cleanup_fd = -1;
2857                         goto out;
2858                 }
2859
2860                 if (*action_handlep == 0) {
2861                         rwa->guid_to_ds_map =
2862                             kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2863                         avl_create(rwa->guid_to_ds_map, guid_compare,
2864                             sizeof (guid_map_entry_t),
2865                             offsetof(guid_map_entry_t, avlnode));
2866                         err = zfs_onexit_add_cb(minor,
2867                             free_guid_map_onexit, rwa->guid_to_ds_map,
2868                             action_handlep);
2869                         if (ra->err != 0)
2870                                 goto out;
2871                 } else {
2872                         err = zfs_onexit_cb_data(minor, *action_handlep,
2873                             (void **)&rwa->guid_to_ds_map);
2874                         if (ra->err != 0)
2875                                 goto out;
2876                 }
2877
2878                 drc->drc_guid_to_ds_map = rwa->guid_to_ds_map;
2879         }
2880
2881         payloadlen = drc->drc_drr_begin->drr_payloadlen;
2882         payload = NULL;
2883         if (payloadlen != 0)
2884                 payload = kmem_alloc(payloadlen, KM_SLEEP);
2885
2886         err = receive_read_payload_and_next_header(ra, payloadlen, payload);
2887         if (err != 0) {
2888                 if (payloadlen != 0)
2889                         kmem_free(payload, payloadlen);
2890                 goto out;
2891         }
2892         if (payloadlen != 0) {
2893                 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
2894                 kmem_free(payload, payloadlen);
2895                 if (err != 0)
2896                         goto out;
2897         }
2898
2899         if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
2900                 err = resume_check(ra, begin_nvl);
2901                 if (err != 0)
2902                         goto out;
2903         }
2904
2905         (void) bqueue_init(&rwa->q, zfs_recv_queue_length,
2906             offsetof(struct receive_record_arg, node));
2907         cv_init(&rwa->cv, NULL, CV_DEFAULT, NULL);
2908         mutex_init(&rwa->mutex, NULL, MUTEX_DEFAULT, NULL);
2909         rwa->os = ra->os;
2910         rwa->byteswap = drc->drc_byteswap;
2911         rwa->resumable = drc->drc_resumable;
2912
2913         (void) thread_create(NULL, 0, receive_writer_thread, rwa, 0, curproc,
2914             TS_RUN, minclsyspri);
2915         /*
2916          * We're reading rwa->err without locks, which is safe since we are the
2917          * only reader, and the worker thread is the only writer.  It's ok if we
2918          * miss a write for an iteration or two of the loop, since the writer
2919          * thread will keep freeing records we send it until we send it an eos
2920          * marker.
2921          *
2922          * We can leave this loop in 3 ways:  First, if rwa->err is
2923          * non-zero.  In that case, the writer thread will free the rrd we just
2924          * pushed.  Second, if  we're interrupted; in that case, either it's the
2925          * first loop and ra->rrd was never allocated, or it's later and ra->rrd
2926          * has been handed off to the writer thread who will free it.  Finally,
2927          * if receive_read_record fails or we're at the end of the stream, then
2928          * we free ra->rrd and exit.
2929          */
2930         while (rwa->err == 0) {
2931                 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2932                         err = SET_ERROR(EINTR);
2933                         break;
2934                 }
2935
2936                 ASSERT3P(ra->rrd, ==, NULL);
2937                 ra->rrd = ra->next_rrd;
2938                 ra->next_rrd = NULL;
2939                 /* Allocates and loads header into ra->next_rrd */
2940                 err = receive_read_record(ra);
2941
2942                 if (ra->rrd->header.drr_type == DRR_END || err != 0) {
2943                         kmem_free(ra->rrd, sizeof (*ra->rrd));
2944                         ra->rrd = NULL;
2945                         break;
2946                 }
2947
2948                 bqueue_enqueue(&rwa->q, ra->rrd,
2949                     sizeof (struct receive_record_arg) + ra->rrd->payload_size);
2950                 ra->rrd = NULL;
2951         }
2952         if (ra->next_rrd == NULL)
2953                 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2954         ra->next_rrd->eos_marker = B_TRUE;
2955         bqueue_enqueue(&rwa->q, ra->next_rrd, 1);
2956
2957         mutex_enter(&rwa->mutex);
2958         while (!rwa->done) {
2959                 cv_wait(&rwa->cv, &rwa->mutex);
2960         }
2961         mutex_exit(&rwa->mutex);
2962
2963         cv_destroy(&rwa->cv);
2964         mutex_destroy(&rwa->mutex);
2965         bqueue_destroy(&rwa->q);
2966         if (err == 0)
2967                 err = rwa->err;
2968
2969 out:
2970         nvlist_free(begin_nvl);
2971         if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2972                 zfs_onexit_fd_rele(cleanup_fd);
2973
2974         if (err != 0) {
2975                 /*
2976                  * Clean up references. If receive is not resumable,
2977                  * destroy what we created, so we don't leave it in
2978                  * the inconsistent state.
2979                  */
2980                 dmu_recv_cleanup_ds(drc);
2981         }
2982
2983         *voffp = ra->voff;
2984         objlist_destroy(&ra->ignore_objlist);
2985         kmem_free(ra, sizeof (*ra));
2986         kmem_free(rwa, sizeof (*rwa));
2987         return (err);
2988 }
2989
2990 static int
2991 dmu_recv_end_check(void *arg, dmu_tx_t *tx)
2992 {
2993         dmu_recv_cookie_t *drc = arg;
2994         dsl_pool_t *dp = dmu_tx_pool(tx);
2995         int error;
2996
2997         ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
2998
2999         if (!drc->drc_newfs) {
3000                 dsl_dataset_t *origin_head;
3001
3002                 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
3003                 if (error != 0)
3004                         return (error);
3005                 if (drc->drc_force) {
3006                         /*
3007                          * We will destroy any snapshots in tofs (i.e. before
3008                          * origin_head) that are after the origin (which is
3009                          * the snap before drc_ds, because drc_ds can not
3010                          * have any snaps of its own).
3011                          */
3012                         uint64_t obj;
3013
3014                         obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3015                         while (obj !=
3016                             dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3017                                 dsl_dataset_t *snap;
3018                                 error = dsl_dataset_hold_obj(dp, obj, FTAG,
3019                                     &snap);
3020                                 if (error != 0)
3021                                         break;
3022                                 if (snap->ds_dir != origin_head->ds_dir)
3023                                         error = SET_ERROR(EINVAL);
3024                                 if (error == 0)  {
3025                                         error = dsl_destroy_snapshot_check_impl(
3026                                             snap, B_FALSE);
3027                                 }
3028                                 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3029                                 dsl_dataset_rele(snap, FTAG);
3030                                 if (error != 0)
3031                                         break;
3032                         }
3033                         if (error != 0) {
3034                                 dsl_dataset_rele(origin_head, FTAG);
3035                                 return (error);
3036                         }
3037                 }
3038                 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
3039                     origin_head, drc->drc_force, drc->drc_owner, tx);
3040                 if (error != 0) {
3041                         dsl_dataset_rele(origin_head, FTAG);
3042                         return (error);
3043                 }
3044                 error = dsl_dataset_snapshot_check_impl(origin_head,
3045                     drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3046                 dsl_dataset_rele(origin_head, FTAG);
3047                 if (error != 0)
3048                         return (error);
3049
3050                 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
3051         } else {
3052                 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
3053                     drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3054         }
3055         return (error);
3056 }
3057
3058 static void
3059 dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
3060 {
3061         dmu_recv_cookie_t *drc = arg;
3062         dsl_pool_t *dp = dmu_tx_pool(tx);
3063
3064         spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
3065             tx, "snap=%s", drc->drc_tosnap);
3066
3067         if (!drc->drc_newfs) {
3068                 dsl_dataset_t *origin_head;
3069
3070                 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3071                     &origin_head));
3072
3073                 if (drc->drc_force) {
3074                         /*
3075                          * Destroy any snapshots of drc_tofs (origin_head)
3076                          * after the origin (the snap before drc_ds).
3077                          */
3078                         uint64_t obj;
3079
3080                         obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3081                         while (obj !=
3082                             dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3083                                 dsl_dataset_t *snap;
3084                                 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
3085                                     &snap));
3086                                 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3087                                 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3088                                 dsl_destroy_snapshot_sync_impl(snap,
3089                                     B_FALSE, tx);
3090                                 dsl_dataset_rele(snap, FTAG);
3091                         }
3092                 }
3093                 VERIFY3P(drc->drc_ds->ds_prev, ==,
3094                     origin_head->ds_prev);
3095
3096                 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3097                     origin_head, tx);
3098                 dsl_dataset_snapshot_sync_impl(origin_head,
3099                     drc->drc_tosnap, tx);
3100
3101                 /* set snapshot's creation time and guid */
3102                 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3103                 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
3104                     drc->drc_drrb->drr_creation_time;
3105                 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
3106                     drc->drc_drrb->drr_toguid;
3107                 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
3108                     ~DS_FLAG_INCONSISTENT;
3109
3110                 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3111                 dsl_dataset_phys(origin_head)->ds_flags &=
3112                     ~DS_FLAG_INCONSISTENT;
3113
3114                 dsl_dataset_rele(origin_head, FTAG);
3115                 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
3116
3117                 if (drc->drc_owner != NULL)
3118                         VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3119         } else {
3120                 dsl_dataset_t *ds = drc->drc_ds;
3121
3122                 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
3123
3124                 /* set snapshot's creation time and guid */
3125                 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3126                 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
3127                     drc->drc_drrb->drr_creation_time;
3128                 dsl_dataset_phys(ds->ds_prev)->ds_guid =
3129                     drc->drc_drrb->drr_toguid;
3130                 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3131                     ~DS_FLAG_INCONSISTENT;
3132
3133                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3134                 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
3135                 if (dsl_dataset_has_resume_receive_state(ds)) {
3136                         (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3137                             DS_FIELD_RESUME_FROMGUID, tx);
3138                         (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3139                             DS_FIELD_RESUME_OBJECT, tx);
3140                         (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3141                             DS_FIELD_RESUME_OFFSET, tx);
3142                         (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3143                             DS_FIELD_RESUME_BYTES, tx);
3144                         (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3145                             DS_FIELD_RESUME_TOGUID, tx);
3146                         (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3147                             DS_FIELD_RESUME_TONAME, tx);
3148                 }
3149         }
3150         drc->drc_newsnapobj = dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
3151         zvol_create_minors(dp->dp_spa, drc->drc_tofs, B_TRUE);
3152         /*
3153          * Release the hold from dmu_recv_begin.  This must be done before
3154          * we return to open context, so that when we free the dataset's dnode,
3155          * we can evict its bonus buffer.
3156          */
3157         dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
3158         drc->drc_ds = NULL;
3159 }
3160
3161 static int
3162 add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3163 {
3164         dsl_pool_t *dp;
3165         dsl_dataset_t *snapds;
3166         guid_map_entry_t *gmep;
3167         int err;
3168
3169         ASSERT(guid_map != NULL);
3170
3171         err = dsl_pool_hold(name, FTAG, &dp);
3172         if (err != 0)
3173                 return (err);
3174         gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3175         err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3176         if (err == 0) {
3177                 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3178                 gmep->gme_ds = snapds;
3179                 avl_add(guid_map, gmep);
3180                 dsl_dataset_long_hold(snapds, gmep);
3181         } else {
3182                 kmem_free(gmep, sizeof (*gmep));
3183         }
3184
3185         dsl_pool_rele(dp, FTAG);
3186         return (err);
3187 }
3188
3189 static int dmu_recv_end_modified_blocks = 3;
3190
3191 static int
3192 dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3193 {
3194         int error;
3195
3196 #ifdef _KERNEL
3197         /*
3198          * We will be destroying the ds; make sure its origin is unmounted if
3199          * necessary.
3200          */
3201         char name[ZFS_MAX_DATASET_NAME_LEN];
3202         dsl_dataset_name(drc->drc_ds, name);
3203         zfs_destroy_unmount_origin(name);
3204 #endif
3205
3206         error = dsl_sync_task(drc->drc_tofs,
3207             dmu_recv_end_check, dmu_recv_end_sync, drc,
3208             dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3209
3210         if (error != 0)
3211                 dmu_recv_cleanup_ds(drc);
3212         return (error);
3213 }
3214
3215 static int
3216 dmu_recv_new_end(dmu_recv_cookie_t *drc)
3217 {
3218         int error;
3219
3220         error = dsl_sync_task(drc->drc_tofs,
3221             dmu_recv_end_check, dmu_recv_end_sync, drc,
3222             dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3223
3224         if (error != 0) {
3225                 dmu_recv_cleanup_ds(drc);
3226         } else if (drc->drc_guid_to_ds_map != NULL) {
3227                 (void) add_ds_to_guidmap(drc->drc_tofs,
3228                     drc->drc_guid_to_ds_map,
3229                     drc->drc_newsnapobj);
3230         }
3231         return (error);
3232 }
3233
3234 int
3235 dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3236 {
3237         drc->drc_owner = owner;
3238
3239         if (drc->drc_newfs)
3240                 return (dmu_recv_new_end(drc));
3241         else
3242                 return (dmu_recv_existing_end(drc));
3243 }
3244
3245 /*
3246  * Return TRUE if this objset is currently being received into.
3247  */
3248 boolean_t
3249 dmu_objset_is_receiving(objset_t *os)
3250 {
3251         return (os->os_dsl_dataset != NULL &&
3252             os->os_dsl_dataset->ds_owner == dmu_recv_tag);
3253 }
3254
3255 #if defined(_KERNEL)
3256 module_param(zfs_send_corrupt_data, int, 0644);
3257 MODULE_PARM_DESC(zfs_send_corrupt_data, "Allow sending corrupt data");
3258 #endif