summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--manifest4
-rw-r--r--usr/src/cmd/chgrp/Makefile28
-rw-r--r--usr/src/cmd/chgrp/chgrp.c32
-rw-r--r--usr/src/cmd/chown/Makefile29
-rw-r--r--usr/src/cmd/chown/chown.c34
-rw-r--r--usr/src/cmd/cmd-inet/usr.bin/pppd/plugins/passprompt.c7
-rw-r--r--usr/src/cmd/cmd-inet/usr.sbin/ipadm/ipadm.c5
-rw-r--r--usr/src/cmd/cmd-inet/usr.sbin/ping/ping.c4
-rw-r--r--usr/src/cmd/cmd-inet/usr.sbin/wificonfig/wificonfig.c9
-rw-r--r--usr/src/cmd/ksh/builtins/alias.c6
-rw-r--r--usr/src/lib/libshell/common/data/solaris_cmdlist.h2
-rw-r--r--usr/src/man/man1/chgrp.176
-rw-r--r--usr/src/man/man1/chown.1326
-rw-r--r--usr/src/pkg/manifests/system-test-utiltest.mf13
-rw-r--r--usr/src/pkg/manifests/system-xopen-xcu4.mf4
-rw-r--r--usr/src/test/util-tests/runfiles/default.run1
-rw-r--r--usr/src/test/util-tests/tests/Makefile1
-rw-r--r--usr/src/test/util-tests/tests/chown/Makefile45
-rw-r--r--usr/src/test/util-tests/tests/chown/chown_test.ksh103
-rw-r--r--usr/src/test/util-tests/tests/chown/files/Makefile50
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout010
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout110
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout1010
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout1110
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout120
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout210
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout310
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout410
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout510
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout610
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout710
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout810
-rw-r--r--usr/src/test/util-tests/tests/chown/files/cout910
-rw-r--r--usr/src/test/zfs-tests/tests/functional/snapshot/deadlist_lock.ksh14
-rw-r--r--usr/src/uts/common/Makefile.files3
-rw-r--r--usr/src/uts/common/fs/zfs/dmu_recv.c2264
-rw-r--r--usr/src/uts/common/fs/zfs/dmu_send.c2206
-rw-r--r--usr/src/uts/common/fs/zfs/dsl_dataset.c2
-rw-r--r--usr/src/uts/common/fs/zfs/sys/dmu_recv.h64
-rw-r--r--usr/src/uts/common/fs/zfs/sys/dmu_send.h28
-rw-r--r--usr/src/uts/common/fs/zfs/sys/dsl_bookmark.h1
-rw-r--r--usr/src/uts/common/fs/zfs/zfs_ioctl.c1
42 files changed, 2763 insertions, 2719 deletions
diff --git a/manifest b/manifest
index a0953be15e..4a9a558cb8 100644
--- a/manifest
+++ b/manifest
@@ -20832,8 +20832,8 @@ f usr/xpg4/bin/basename 0555 root bin
f usr/xpg4/bin/batch 0555 root bin
s usr/xpg4/bin/bg=../../bin/alias
s usr/xpg4/bin/cd=../../bin/alias
-f usr/xpg4/bin/chgrp 0555 root bin
-f usr/xpg4/bin/chown 0555 root bin
+s usr/xpg4/bin/chgrp=../../bin/chgrp
+s usr/xpg4/bin/chown=../../bin/chown
s usr/xpg4/bin/command=../../bin/alias
f usr/xpg4/bin/cp 0555 root bin
f usr/xpg4/bin/crontab 4555 root bin
diff --git a/usr/src/cmd/chgrp/Makefile b/usr/src/cmd/chgrp/Makefile
index cac25266b7..7de84a25df 100644
--- a/usr/src/cmd/chgrp/Makefile
+++ b/usr/src/cmd/chgrp/Makefile
@@ -19,49 +19,37 @@
# CDDL HEADER END
#
#
-#ident "%Z%%M% %I% %E% SMI"
-#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
PROG= chgrp
-XPG4PROG= chgrp
-XD= exobjs.xpg4
EXOBJS= chgrp.o
-XPG4EXOBJS= exobjs.xpg4/chgrp.o
include ../Makefile.cmd
-$(XPG4) := CFLAGS += -DXPG4
+ROOTXPG4LINK=$(ROOTXPG4BIN)/$(PROG)
+
CPPFLAGS += -D_FILE_OFFSET_BITS=64
LDLIBS += -lcmdutils -lsec
.KEEP_STATE:
-all: $(PROG) $(XPG4)
+all: $(PROG)
$(PROG): $(EXOBJS)
$(LINK.c) -o $@ $(EXOBJS) $(LDLIBS)
$(POST_PROCESS)
-$(XPG4): $(XD) $(XPG4EXOBJS)
- $(LINK.c) -o $@ $(XPG4EXOBJS) $(LDLIBS)
- $(POST_PROCESS)
+$(ROOTXPG4LINK): $(ROOTPROG)
+ -$(RM) $@
+ -$(SYMLINK) ../../bin/chgrp $@
-install: all $(ROOTPROG) $(ROOTXPG4PROG)
+install: all $(ROOTPROG) $(ROOTXPG4LINK)
clean:
- -@rm -rf $(EXOBJS) $(XD)
+ -@rm -rf $(EXOBJS)
lint: lint_PROG
-$(XPG4EXOBJS): $(XD)
-
-$(XD)/%.o: %.c
- $(COMPILE.c) -o $@ $<
-
-$(XD):
- -@mkdir -p $@
-
include ../Makefile.targ
diff --git a/usr/src/cmd/chgrp/chgrp.c b/usr/src/cmd/chgrp/chgrp.c
index a1fe7fefbf..ed27bbd47a 100644
--- a/usr/src/cmd/chgrp/chgrp.c
+++ b/usr/src/cmd/chgrp/chgrp.c
@@ -38,8 +38,6 @@
* contributors.
*/
-#pragma ident "%Z%%M% %I% %E% SMI"
-
/*
* chgrp [-fhR] gid file ...
* chgrp -R [-f] [-H|-L|-P] gid file ...
@@ -82,41 +80,18 @@ static int isnumber(char *);
static int Perror(char *);
static void chgrpr(char *, gid_t);
-#ifdef XPG4
-/*
- * Check to see if we are to follow symlinks specified on the command line.
- * This assumes we've already checked to make sure neither -h or -P was
- * specified, so we are just looking to see if -R -L, or -R -H was specified,
- * or, since -R has the same behavior as -R -L, if -R was specified by itself.
- * Therefore, all we really need to check for is if -R was specified.
- */
-#define FOLLOW_CL_LINKS (rflag)
-#else
/*
* Check to see if we are to follow symlinks specified on the command line.
* This assumes we've already checked to make sure neither -h or -P was
* specified, so we are just looking to see if -R -L, or -R -H was specified.
- * Note: -R by itself will change the group of a directory referenced by a
- * symlink however it will not follow the symlink to any other part of the
- * file hierarchy.
*/
#define FOLLOW_CL_LINKS (rflag && (Hflag || Lflag))
-#endif
-#ifdef XPG4
-/*
- * Follow symlinks when traversing directories. Since -R behaves the
- * same as -R -L, we always want to follow symlinks to other parts
- * of the file hierarchy unless -H was specified.
- */
-#define FOLLOW_D_LINKS (!Hflag)
-#else
/*
* Follow symlinks when traversing directories. Only follow symlinks
* to other parts of the file hierarchy if -L was specified.
*/
#define FOLLOW_D_LINKS (Lflag)
-#endif
#define CHOWN(f, u, g) if (chown(f, u, g) < 0) { \
status += Perror(f); \
@@ -185,6 +160,13 @@ main(int argc, char *argv[])
default:
usage();
}
+ /*
+ * Set Pflag by default for recursive operations
+ * if no other options were specified.
+ */
+ if (rflag && !(Lflag || Hflag || Pflag || hflag)) {
+ Pflag = 1;
+ }
/*
* Check for sufficient arguments
diff --git a/usr/src/cmd/chown/Makefile b/usr/src/cmd/chown/Makefile
index 622030cf38..9ee61bfc5f 100644
--- a/usr/src/cmd/chown/Makefile
+++ b/usr/src/cmd/chown/Makefile
@@ -19,50 +19,37 @@
# CDDL HEADER END
#
#
-#ident "%Z%%M% %I% %E% SMI"
-#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
PROG= chown
-XPG4PROG= chown
-XD= exobjs.xpg4
EXOBJS= chown.o
-XPG4EXOBJS= exobjs.xpg4/chown.o
include ../Makefile.cmd
-$(XPG4) := CFLAGS += -DXPG4
+ROOTXPG4LINK=$(ROOTXPG4BIN)/$(PROG)
+
CPPFLAGS += -D_FILE_OFFSET_BITS=64
LDLIBS += -lcmdutils -lsec
.KEEP_STATE:
-all: $(PROG) $(XPG4)
+all: $(PROG)
$(PROG): $(EXOBJS)
$(LINK.c) -o $@ $(EXOBJS) $(LDLIBS)
$(POST_PROCESS)
-$(XPG4): $(XD) $(XPG4EXOBJS)
- $(LINK.c) -o $@ $(XPG4EXOBJS) $(LDLIBS)
- $(POST_PROCESS)
-
+$(ROOTXPG4LINK): $(ROOTPROG)
+ -$(RM) $@
+ -$(SYMLINK) ../../bin/chown $@
-install: all $(ROOTPROG) $(ROOTXPG4PROG)
+install: all $(ROOTPROG) $(ROOTXPG4LINK)
clean:
- -@rm -rf $(EXOBJS) $(XD)
+ -@rm -rf $(EXOBJS)
lint: lint_PROG
-$(XPG4EXOBJS): $(XD)
-
-$(XD)/%.o: %.c
- $(COMPILE.c) -o $@ $<
-
-$(XD):
- -@mkdir -p $@
-
include ../Makefile.targ
diff --git a/usr/src/cmd/chown/chown.c b/usr/src/cmd/chown/chown.c
index 319f571384..15db448a5b 100644
--- a/usr/src/cmd/chown/chown.c
+++ b/usr/src/cmd/chown/chown.c
@@ -24,15 +24,13 @@
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
-/* All Rights Reserved */
+/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
-#pragma ident "%Z%%M% %I% %E% SMI"
-
/*
* chown [-fhR] uid[:gid] file ...
* chown -R [-f] [-H|-L|-P] uid[:gid] file ...
@@ -76,41 +74,18 @@ static int isnumber(char *);
static void chownr(char *, uid_t, gid_t);
static void usage();
-#ifdef XPG4
-/*
- * Check to see if we are to follow symlinks specified on the command line.
- * This assumes we've already checked to make sure neither -h or -P was
- * specified, so we are just looking to see if -R -H, or -R -L was specified,
- * or, since -R has the same behavior as -R -L, if -R was specified by itself.
- * Therefore, all we really need to check for is if -R was specified.
- */
-#define FOLLOW_CL_LINKS (rflag)
-#else
/*
* Check to see if we are to follow symlinks specified on the command line.
* This assumes we've already checked to make sure neither -h or -P was
* specified, so we are just looking to see if -R -H, or -R -L was specified.
- * Note: -R by itself will change the ownership of a directory referenced by a
- * symlink however it will now follow the symlink to any other part of the
- * file hierarchy.
*/
#define FOLLOW_CL_LINKS (rflag && (Hflag || Lflag))
-#endif
-#ifdef XPG4
-/*
- * Follow symlinks when traversing directories. Since -R behaves the
- * same as -R -L, we always want to follow symlinks to other parts
- * of the file hierarchy unless -H was specified.
- */
-#define FOLLOW_D_LINKS (!Hflag)
-#else
/*
* Follow symlinks when traversing directories. Only follow symlinks
* to other parts of the file hierarchy if -L was specified.
*/
#define FOLLOW_D_LINKS (Lflag)
-#endif
#define CHOWN(f, u, g) if (chown(f, u, g) < 0) { \
status += Perror(f); \
@@ -180,6 +155,13 @@ main(int argc, char *argv[])
}
}
/*
+ * Set Pflag by default for recursive operations
+ * if no other options were specified.
+ */
+ if (rflag && !(Lflag || Hflag || Pflag || hflag)) {
+ Pflag = 1;
+ }
+ /*
* Check for sufficient arguments
* or a usage error.
*/
diff --git a/usr/src/cmd/cmd-inet/usr.bin/pppd/plugins/passprompt.c b/usr/src/cmd/cmd-inet/usr.bin/pppd/plugins/passprompt.c
index ce361e9c37..a96fa32ba8 100644
--- a/usr/src/cmd/cmd-inet/usr.bin/pppd/plugins/passprompt.c
+++ b/usr/src/cmd/cmd-inet/usr.bin/pppd/plugins/passprompt.c
@@ -8,6 +8,11 @@
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
+
+/*
+ * Copyright (c) 2018, Joyent, Inc.
+ */
+
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
@@ -70,7 +75,7 @@ static int promptpass(char *user, char *passwd)
argv[0] = promptprog;
argv[1] = user == NULL ? "" : user;
argv[2] = remote_name;
- slprintf(fdstr, sizeof (fdstr), "%d", p[1]);
+ (void) slprintf(fdstr, sizeof (fdstr), "%d", p[1]);
argv[3] = fdstr;
argv[4] = NULL;
(void) execv(*argv, argv);
diff --git a/usr/src/cmd/cmd-inet/usr.sbin/ipadm/ipadm.c b/usr/src/cmd/cmd-inet/usr.sbin/ipadm/ipadm.c
index 4dbbb83590..30e2f0f549 100644
--- a/usr/src/cmd/cmd-inet/usr.sbin/ipadm/ipadm.c
+++ b/usr/src/cmd/cmd-inet/usr.sbin/ipadm/ipadm.c
@@ -22,7 +22,7 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2017 Nexenta Systems, Inc.
- * Copyright 2017 Joyent, Inc.
+ * Copyright (c) 2018, Joyent, Inc.
* Copyright 2017 Gary Mills
* Copyright (c) 2016, Chris Fraire <cfraire@me.com>.
*/
@@ -1636,8 +1636,7 @@ is_from_gz(const char *lifname)
if (if_info->ifi_cflags & IFIF_L3PROTECT)
ret = _B_TRUE;
- if (if_info)
- ipadm_free_if_info(if_info);
+ ipadm_free_if_info(if_info);
return (ret);
}
diff --git a/usr/src/cmd/cmd-inet/usr.sbin/ping/ping.c b/usr/src/cmd/cmd-inet/usr.sbin/ping/ping.c
index c6f8257ae9..9f959976a8 100644
--- a/usr/src/cmd/cmd-inet/usr.sbin/ping/ping.c
+++ b/usr/src/cmd/cmd-inet/usr.sbin/ping/ping.c
@@ -38,7 +38,7 @@
*/
/*
- * Copyright (c) 2017, Joyent, Inc.
+ * Copyright (c) 2018, Joyent, Inc.
*/
#include <assert.h>
@@ -1989,7 +1989,7 @@ recv_icmp_packet(struct addrinfo *ai_dst, int recv_sock6, int recv_sock,
progname, strerror(errno));
}
continue;
- } if (cc > 0) {
+ } else if (cc > 0) {
check_reply(ai_dst, &in_msg, cc,
udp_src_port);
}
diff --git a/usr/src/cmd/cmd-inet/usr.sbin/wificonfig/wificonfig.c b/usr/src/cmd/cmd-inet/usr.sbin/wificonfig/wificonfig.c
index ad5100c1b0..5a9dc9889b 100644
--- a/usr/src/cmd/cmd-inet/usr.sbin/wificonfig/wificonfig.c
+++ b/usr/src/cmd/cmd-inet/usr.sbin/wificonfig/wificonfig.c
@@ -23,7 +23,9 @@
* Use is subject to license terms.
*/
-#pragma ident "%Z%%M% %I% %E% SMI"
+/*
+ * Copyright (c) 2018, Joyent, Inc.
+ */
#include <stdio.h>
#include <stdlib.h>
@@ -2531,7 +2533,7 @@ do_deletepf(int fd, int argc, char **argv)
B_FALSE);
free(section_id);
p_section = p_sectionbak;
- continue;
+ continue;
}
p_section = p_section->section_next;
}
@@ -2751,8 +2753,9 @@ do_rmprefer(int fd, int argc, char **argv)
return (B_FALSE);
pae = plist->ael_head;
while (pae != NULL) {
+ ae_t *next = pae->ae_next;
free(pae);
- pae = pae->ae_next;
+ pae = next;
}
plist->ael_head = plist->ael_tail = NULL;
plist->ael_argc = 0;
diff --git a/usr/src/cmd/ksh/builtins/alias.c b/usr/src/cmd/ksh/builtins/alias.c
index a17e2dabd1..88edbc1e9b 100644
--- a/usr/src/cmd/ksh/builtins/alias.c
+++ b/usr/src/cmd/ksh/builtins/alias.c
@@ -68,9 +68,7 @@ bfastpathrec fastpath_builtins[] =
/* This list must be alphabetically sorted for |strcmp()| usage */
{ "basename", b_basename },
{ "cat", b_cat },
- { "chgrp", b_chgrp },
{ "chmod", b_chmod },
- { "chown", b_chown },
#ifdef ENABLE_PERFORMANCE_PARADOXON
{ "cksum", b_cksum },
#endif /* ENABLE_PERFORMANCE_PARADOXON */
@@ -98,7 +96,7 @@ bfastpathrec fastpath_builtins[] =
{ "mkfifo", b_mkfifo },
{ "mktemp", b_mktemp },
{ "mv", b_mv },
- { "paste", b_paste },
+ { "paste", b_paste },
{ "pathchk", b_pathchk },
{ "pids", b_pids },
{ "readlink", b_readlink },
@@ -119,7 +117,7 @@ bfastpathrec fastpath_builtins[] =
{ "uniq", b_uniq },
{ "wc", b_wc },
{ "xgrep", b_xgrep },
- { NULL, (int (*)(int, char **, void *))NULL }
+ { NULL, (int (*)(int, char **, void *))NULL }
};
static inline
diff --git a/usr/src/lib/libshell/common/data/solaris_cmdlist.h b/usr/src/lib/libshell/common/data/solaris_cmdlist.h
index f394e79aa7..ba724a15b1 100644
--- a/usr/src/lib/libshell/common/data/solaris_cmdlist.h
+++ b/usr/src/lib/libshell/common/data/solaris_cmdlist.h
@@ -84,7 +84,7 @@ ASTCMDLIST(chgrp)
ASTCMDLIST(chmod)
ASTCMDLIST(chown)
// XPG4CMDLIST(chown)
-BINCMDLIST(chown)
+// BINCMDLIST(chown)
ASTCMDLIST(cksum)
BINCMDLIST(cksum)
GNUCMDLIST(cksum)
diff --git a/usr/src/man/man1/chgrp.1 b/usr/src/man/man1/chgrp.1
index 795bc56f66..cab549e34b 100644
--- a/usr/src/man/man1/chgrp.1
+++ b/usr/src/man/man1/chgrp.1
@@ -49,7 +49,7 @@
.\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
.\" Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved
.\"
-.TH CHGRP 1 "Jul 11, 2008"
+.TH CHGRP 1 "Feb 21, 2019"
.SH NAME
chgrp \- change file group ownership
.SH SYNOPSIS
@@ -74,7 +74,6 @@ chgrp \- change file group ownership
.fi
.SH DESCRIPTION
-.sp
.LP
The \fBchgrp\fR utility will set the group ID of the file named by each
\fIfile\fR operand to the group ID specified by the \fIgroup\fR operand.
@@ -136,10 +135,9 @@ set rstchown = 0
\fB_POSIX_CHOWN_RESTRICTED\fR is enabled by default. See \fBsystem\fR(4) and
\fBfpathconf\fR(2).
.SH OPTIONS
-.sp
.LP
The following options are supported.
-.SS "/usr/bin/chgrp and /usr/xpg4/bin/chgrp"
+
.sp
.ne 2
.na
@@ -202,49 +200,34 @@ of the file hierarchy.
.sp
.ne 2
.na
-\fB\fB-s\fR\fR
-.ad
-.RS 6n
-The specified group is Windows SID. This option requires a file system that
-supports storing SIDs, such as ZFS.
-.RE
-
-.sp
-.LP
-Specifying more than one of the mutually-exclusive options \fB-H\fR, \fB-L\fR,
-or \fB-P\fR is not considered an error. The last option specified determines
-the behavior of \fBchgrp\fR.
-.SS "/usr/bin/chgrp"
-.sp
-.ne 2
-.na
\fB\fB-R\fR\fR
.ad
.RS 6n
Recursive. \fBchgrp\fR descends through the directory, and any subdirectories,
setting the specified group \fBID\fR as it proceeds. When a symbolic link is
-encountered, the group of the target file is changed, unless the \fB-h\fR or
-\fB-P\fR option is specified. However, no recursion takes place, unless the
-\fB-H\fR or \fB-L\fR option is specified.
+encountered, the group of the of the symbolic link is changed, unless the
+\fB-H\fR or \fB-L\fR option is specified. Unless the \fB-H\fR, \fB-L\fR,
+or \fB-P\fR option is specified, the \fB-P\fR option is used as the default mode.
.RE
-.SS "/usr/xpg4/bin/chgrp"
.sp
.ne 2
.na
-\fB\fB-R\fR\fR
+\fB\fB-s\fR\fR
.ad
.RS 6n
-Recursive. \fBchgrp\fR descends through the directory, and any subdirectories,
-setting the specified group \fBID\fR as it proceeds. When a symbolic link is
-encountered, the group of the target file is changed, unless the \fB-h\fR or
-\fB-P\fR option is specified. Unless the \fB-H\fR, \fB-L\fR, or \fB-P\fR option
-is specified, the \fB-L\fR option is used as the default mode.
+The specified group is Windows SID. This option requires a file system that
+supports storing SIDs, such as ZFS.
.RE
-.SH OPERANDS
.sp
.LP
+Specifying more than one of the mutually-exclusive options \fB-H\fR, \fB-L\fR,
+or \fB-P\fR is not considered an error. The last option specified determines
+the behavior of \fBchgrp\fR.
+
+.SH OPERANDS
+.LP
The following operands are supported:
.sp
.ne 2
@@ -268,18 +251,15 @@ A path name of a file whose group ID is to be modified.
.RE
.SH USAGE
-.sp
.LP
See \fBlargefile\fR(5) for the description of the behavior of \fBchgrp\fR when
encountering files greater than or equal to 2 Gbyte (2^31 bytes).
.SH ENVIRONMENT VARIABLES
-.sp
.LP
See \fBenviron\fR(5) for descriptions of the following environment variables
that affect the execution of \fBchgrp\fR: \fBLANG\fR, \fBLC_ALL\fR,
\fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
.SH EXIT STATUS
-.sp
.LP
The following exit values are returned:
.sp
@@ -301,7 +281,6 @@ An error occurred.
.RE
.SH FILES
-.sp
.ne 2
.na
\fB\fB/etc/group\fR\fR
@@ -311,30 +290,9 @@ group file
.RE
.SH ATTRIBUTES
-.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
-.SS "/usr/bin/chgrp"
-.sp
-.sp
-.TS
-box;
-c | c
-l | l .
-ATTRIBUTE TYPE ATTRIBUTE VALUE
-_
-CSI Enabled. See NOTES.
-_
-Interface Stability Committed
-_
-Standard See \fBstandards\fR(5).
-.TE
-
-.SS "/usr/xpg4/bin/chgrp"
-.sp
-
-.sp
.TS
box;
c | c
@@ -349,12 +307,14 @@ Standard See \fBstandards\fR(5).
.TE
.SH SEE ALSO
-.sp
.LP
\fBchmod\fR(1), \fBchown\fR(1), \fBid\fR(1M), \fBchown\fR(2),
\fBfpathconf\fR(2), \fBgroup\fR(4), \fBpasswd\fR(4), \fBsystem\fR(4),
\fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5), \fBstandards\fR(5)
.SH NOTES
-.sp
.LP
\fBchgrp\fR is CSI-enabled except for the \fIgroup\fR name.
+.sp
+.LP
+In the past the behavior of \fB/usr/xpg4/bin/chgrp\fR and
+\fB/usr/bin/chgrp\fR utilities was different. Now they behave the same way.
diff --git a/usr/src/man/man1/chown.1 b/usr/src/man/man1/chown.1
index f309fa3e37..6ec6a8247a 100644
--- a/usr/src/man/man1/chown.1
+++ b/usr/src/man/man1/chown.1
@@ -45,61 +45,31 @@
.\" Portions Copyright (c) 1982-2007 AT&T Knowledge Ventures
.\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved
.\"
-.TH CHOWN 1 "Jul 11, 2008"
+.TH CHOWN 1 "Feb 21, 2019"
.SH NAME
chown \- change file ownership
.SH SYNOPSIS
-.SS "/usr/bin/chown"
.LP
.nf
-\fB/usr/bin/chown\fR [\fB-fhR\fR] \fIowner\fR[:\fIgroup\fR] \fIfile\fR...
+\fBchown\fR [\fB-fhR\fR] \fIowner\fR[:\fIgroup\fR] \fIfile\fR...
.fi
.LP
.nf
-\fB/usr/bin/chown\fR \fB-s\fR [\fB-fhR\fR] \fIownersid\fR[:\fIgroupsid\fR] \fIfile\fR...
+\fBchown\fR \fB-s\fR [\fB-fhR\fR] \fIownersid\fR[:\fIgroupsid\fR] \fIfile\fR...
.fi
.LP
.nf
-\fB/usr/bin/chown\fR \fB-R\fR [\fB-f\fR] [\fB-H\fR | \fB-L\fR | \fB-P\fR] \fIowner\fR[:\fIgroup\fR] \fIfile\fR...
+\fBchown\fR \fB-R\fR [\fB-f\fR] [\fB-H\fR | \fB-L\fR | \fB-P\fR] \fIowner\fR[:\fIgroup\fR] \fIfile\fR...
.fi
.LP
.nf
-\fB/usr/bin/chown\fR \fB-s\fR \fB-R\fR [\fB-f\fR] [\fB-H\fR | \fB-L\fR | \fB-P\fR] \fIownersid\fR[:\fIgroupsid\fR] \fIfile\fR...
-.fi
-
-.SS "/usr/xpg4/bin/chown"
-.LP
-.nf
-\fB/usr/xpg4/bin/chown\fR [\fB-fhR\fR] \fIowner\fR[:\fIgroup\fR] \fIfile\fR...
-.fi
-
-.LP
-.nf
-\fB/usr/xpg4/bin/chown\fR \fB-s\fR [\fB-fhR\fR] \fIownersid\fR[:\fIgroupsid\fR] \fIfile\fR...
-.fi
-
-.LP
-.nf
-\fB/usr/xpg4/bin/chown\fR \fB-R\fR [\fB-f\fR] [\fB-H\fR | \fB-L\fR | \fB-P\fR] \fIowner\fR[:\fIgroup\fR] \fIfile\fR...
-.fi
-
-.LP
-.nf
-\fB/usr/xpg4/bin/chown\fR \fB-s\fR \fB-R\fR [\fB-f\fR] [\fB-H\fR | \fB-L\fR | \fB-P\fR] \fIownersid\fR[:\fIgroupsid\fR] \fIfile\fR...
-.fi
-
-.SS "ksh93"
-.LP
-.nf
-\fBchown\fR [\fB-cflhmnvHLPRX\fR] [\fB-r\fR \fIfile\fR] \fIowner\fR[:\fIgroup\fR] \fIfile\fR...
+\fBchown\fR \fB-s\fR \fB-R\fR [\fB-f\fR] [\fB-H\fR | \fB-L\fR | \fB-P\fR] \fIownersid\fR[:\fIgroupsid\fR] \fIfile\fR...
.fi
.SH DESCRIPTION
-.SS "/usr/bin/chown and /usr/xpg4/bin/chown"
-.sp
.LP
The \fBchown\fR utility sets the user \fBID\fR of the file named by each
\fBfile\fR to the user \fBID\fR specified by \fIowner\fR, and, optionally, sets
@@ -142,22 +112,7 @@ To disable this option, include the following line in \fB/etc/system\fR:
.LP
\fB{_POSIX_CHOWN_RESTRICTED}\fR is enabled by default. See \fBsystem\fR(4) and
\fBfpathconf\fR(2).
-.SS "ksh93"
-.sp
-.LP
-The \fBchown\fR built-in in \fBksh93\fR is associated with the \fB/bin\fR and
-\fB/usr/bin\fR paths. It is invoked when \fBchown\fR is executed without a
-pathname prefix and the pathname search finds a \fB/bin/chown\fR or
-/usr/bin/chown executable.
-.sp
-.LP
-\fBchown\fR changes the ownership of each file to \fIowner\fR. \fIowner\fR can
-be specified as either a user name or a numeric user id. The group ownership of
-each file can also be changed to \fIgroup\fR by appending \fI:group\fR to the
-user name.
.SH OPTIONS
-.SS "/usr/bin/chown and /usr/xpg4/bin/chown"
-.sp
.LP
The following options are supported:
.sp
@@ -222,239 +177,33 @@ of the file hierarchy.
.sp
.ne 2
.na
-\fB\fB-s\fR\fR
-.ad
-.RS 6n
-The owner and/or group arguments are Windows SID strings. This option requires
-a file system that supports storing SIDs, such as ZFS.
-.RE
-
-.sp
-.LP
-Specifying more than one of the mutually-exclusive options \fB-H\fR, \fB-L\fR,
-or \fB-P\fR is not considered an error. The last option specified determines
-the behavior of \fBchown\fR.
-.SS "/usr/bin/chown"
-.sp
-.LP
-The following options are supported:
-.sp
-.ne 2
-.na
\fB\fB-R\fR\fR
.ad
.RS 6n
Recursive. \fBchown\fR descends through the directory, and any subdirectories,
setting the specified ownership \fBID\fR as it proceeds. When a symbolic link
-is encountered, the owner of the target file is changed, unless the \fB-h\fR or
-\fB-P\fR option is specified. However, no recursion takes place, unless the
-\fB-H\fR or \fB-L\fR option is specified.
+is encountered, the owner of the symbolic link is changed, unless the
+\fB-H\fR or \fB-L\fR option is specified. Unless the \fB-H\fR, \fB-L\fR, or \fB-P\fR
+option is specified, the \fB-P\fR option is used as the default mode.
.RE
-.SS "/usr/xpg4/bin/chown"
-.sp
-.LP
-The following options are supported:
.sp
.ne 2
.na
-\fB\fB-R\fR\fR
+\fB\fB-s\fR\fR
.ad
.RS 6n
-Recursive. \fBchown\fR descends through the directory, and any subdirectories,
-setting the specified ownership \fBID\fR as it proceeds. When a symbolic link
-is encountered, the owner of the target file is changed, unless the \fB-h\fR or
-\fB-P\fR option is specified. Unless the \fB-H\fR, \fB-L\fR, or \fB-P\fR option
-is specified, the \fB-L\fR option is used as the default mode.
+The owner and/or group arguments are Windows SID strings. This option requires
+a file system that supports storing SIDs, such as ZFS.
.RE
-.SS "ksh93"
.sp
.LP
-The following options are supported by the \fBksh93\fR built-in \fBchown\fR
-command:
-.sp
-.ne 2
-.na
-\fB\fB-c\fR\fR
-.ad
-.br
-.na
-\fB\fB--changes\fR\fR
-.ad
-.sp .6
-.RS 4n
-Describe only files whose ownership actually changes.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-f\fR\fR
-.ad
-.br
-.na
-\fB\fB--quiet | silent\fR\fR
-.ad
-.sp .6
-.RS 4n
-Do not report files whose ownership fails to change.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-l | h\fR\fR
-.ad
-.br
-.na
-\fB\fB--symlink\fR\fR
-.ad
-.sp .6
-.RS 4n
-Change the ownership of the symbolic links on systems that support this option.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-m\fR\fR
-.ad
-.br
-.na
-\fB\fB--map\fR\fR
-.ad
-.sp .6
-.RS 4n
-Interpret the first operand as a file that contains a map of:
-.sp
-.in +2
-.nf
-\fIfrom_uid\fR:\fIfrom_gid to_uid:to_gid\fR
-.fi
-.in -2
-.sp
-
-pairs. Ownership of files matching the \fIfrom\fR part of any pair is changed
-to the corresponding \fIto\fR part of the pair. The process stops at the first
-match for each file. Unmatched files are silently ignored.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-n\fR\fR
-.ad
-.br
-.na
-\fB\fB--show\fR\fR
-.ad
-.sp .6
-.RS 4n
-Show actions but do not execute.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-r\fR\fR
-.ad
-.br
-.na
-\fB\fB--reference=file\fR\fR
-.ad
-.sp .6
-.RS 4n
-Omit the explicit ownership operand and use the ownership of the file instead.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-v\fR\fR
-.ad
-.br
-.na
-\fB\fB--verbose\fR\fR
-.ad
-.sp .6
-.RS 4n
-Describe the changed permissions of all files.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-H\fR\fR
-.ad
-.br
-.na
-\fB\fB--metaphysical\fR\fR
-.ad
-.sp .6
-.RS 4n
-Follow symbolic links for command arguments. Otherwise do not follow symbolic
-links when traversing directories.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-L\fR\fR
-.ad
-.br
-.na
-\fB\fB--logical | follow\fR\fR
-.ad
-.sp .6
-.RS 4n
-Follow symbolic links when traversing directories.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-P\fR\fR
-.ad
-.br
-.na
-\fB\fB--physical | nofollow\fR\fR
-.ad
-.sp .6
-.RS 4n
-Do not follow symbolic links when traversing directories.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-R\fR\fR
-.ad
-.br
-.na
-\fB\fB--recursive\fR\fR
-.ad
-.sp .6
-.RS 4n
-Recursively change ownership of directories and their contents.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fB-X\fR\fR
-.ad
-.br
-.na
-\fB\fB--test\fR\fR
-.ad
-.sp .6
-.RS 4n
-Canonicalize output for testing.
-.RE
+Specifying more than one of the mutually-exclusive options \fB-H\fR, \fB-L\fR,
+or \fB-P\fR is not considered an error. The last option specified determines
+the behavior of \fBchown\fR.
.SH OPERANDS
-.sp
.LP
The following operands are supported:
.sp
@@ -486,7 +235,6 @@ A path name of a file whose user \fBID\fR is to be modified.
.RE
.SH USAGE
-.sp
.LP
See \fBlargefile\fR(5) for the description of the behavior of \fBchown\fR when
encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
@@ -507,13 +255,11 @@ example% \fBchown \(miR \(mih \fIowner\fR[:group] \fIfile\fR...\fR
.sp
.SH ENVIRONMENT VARIABLES
-.sp
.LP
See \fBenviron\fR(5) for descriptions of the following environment variables
that affect the execution of \fBchown\fR: \fBLANG\fR, \fBLC_ALL\fR,
\fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
.SH EXIT STATUS
-.sp
.LP
The following exit values are returned:
.sp
@@ -535,7 +281,6 @@ An error occurred.
.RE
.SH FILES
-.sp
.ne 2
.na
\fB\fB/etc/passwd\fR\fR
@@ -545,28 +290,8 @@ System password file
.RE
.SH ATTRIBUTES
-.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
-.SS "/usr/bin/chown"
-.sp
-
-.sp
-.TS
-box;
-c | c
-l | l .
-ATTRIBUTE TYPE ATTRIBUTE VALUE
-_
-CSI Enabled. See NOTES.
-_
-Interface Stability Committed
-_
-Standard See \fBstandards\fR(5).
-.TE
-
-.SS "/usr/xpg4/bin/chown"
-.sp
.sp
.TS
@@ -582,31 +307,16 @@ _
Standard See \fBstandards\fR(5).
.TE
-.SS "ksh93"
-.sp
-
-.sp
-.TS
-box;
-c | c
-l | l .
-ATTRIBUTE TYPE ATTRIBUTE VALUE
-_
-Interface Stability See below.
-.TE
-
-.sp
-.LP
-The \fBksh93\fR built-in binding to \fB/bin\fR and \fB/usr/bin\fR is Volatile.
-The built-in interfaces are Uncommitted.
.SH SEE ALSO
-.sp
.LP
\fBchgrp\fR(1), \fBchmod\fR(1), \fBksh93\fR(1), \fBchown\fR(2),
\fBfpathconf\fR(2), \fBpasswd\fR(4), \fBsystem\fR(4), \fBattributes\fR(5),
\fBenviron\fR(5), \fBlargefile\fR(5), \fBstandards\fR(5)
.SH NOTES
-.sp
.LP
\fBchown\fR is \fBCSI\fR-enabled except for the \fIowner\fR and \fIgroup\fR
names.
+.sp
+.LP
+In the past the behavior of \fB/usr/xpg4/bin/chown\fR and
+\fB/usr/bin/chown\fR utilities was different. Now they behave the same way.
diff --git a/usr/src/pkg/manifests/system-test-utiltest.mf b/usr/src/pkg/manifests/system-test-utiltest.mf
index 5fdd891b11..3ed9baeba5 100644
--- a/usr/src/pkg/manifests/system-test-utiltest.mf
+++ b/usr/src/pkg/manifests/system-test-utiltest.mf
@@ -1118,6 +1118,7 @@ file path=opt/util-tests/tests/awk/tests/T.redir mode=0555
file path=opt/util-tests/tests/awk/tests/T.split mode=0555
file path=opt/util-tests/tests/awk/tests/T.sub mode=0555
file path=opt/util-tests/tests/awk/tests/T.system mode=0555
+file path=opt/util-tests/tests/chown_test mode=0555
file path=opt/util-tests/tests/date_test mode=0555
file path=opt/util-tests/tests/demangle/afl-fast mode=0555
file path=opt/util-tests/tests/demangle/gcc-libstdc++ mode=0555
@@ -1291,6 +1292,18 @@ file path=opt/util-tests/tests/dis/risc-v/tst.supervisor.out mode=0444
file path=opt/util-tests/tests/dis/risc-v/tst.supervisor.s mode=0444
file path=opt/util-tests/tests/dis/sparc/tst.regs.out mode=0444
file path=opt/util-tests/tests/dis/sparc/tst.regs.s mode=0444
+file path=opt/util-tests/tests/files/cout0 mode=0444
+file path=opt/util-tests/tests/files/cout1 mode=0444
+file path=opt/util-tests/tests/files/cout10 mode=0444
+file path=opt/util-tests/tests/files/cout11 mode=0444
+file path=opt/util-tests/tests/files/cout2 mode=0444
+file path=opt/util-tests/tests/files/cout3 mode=0444
+file path=opt/util-tests/tests/files/cout4 mode=0444
+file path=opt/util-tests/tests/files/cout5 mode=0444
+file path=opt/util-tests/tests/files/cout6 mode=0444
+file path=opt/util-tests/tests/files/cout7 mode=0444
+file path=opt/util-tests/tests/files/cout8 mode=0444
+file path=opt/util-tests/tests/files/cout9 mode=0444
file path=opt/util-tests/tests/files/gout0 mode=0444
file path=opt/util-tests/tests/files/gout1 mode=0444
file path=opt/util-tests/tests/files/gout10 mode=0444
diff --git a/usr/src/pkg/manifests/system-xopen-xcu4.mf b/usr/src/pkg/manifests/system-xopen-xcu4.mf
index c9de2bb7fa..b7f5733eb4 100644
--- a/usr/src/pkg/manifests/system-xopen-xcu4.mf
+++ b/usr/src/pkg/manifests/system-xopen-xcu4.mf
@@ -38,8 +38,6 @@ file path=usr/xpg4/bin/at group=sys mode=4755
file path=usr/xpg4/bin/awk mode=0555
file path=usr/xpg4/bin/basename mode=0555
file path=usr/xpg4/bin/batch mode=0555
-file path=usr/xpg4/bin/chgrp mode=0555
-file path=usr/xpg4/bin/chown mode=0555
file path=usr/xpg4/bin/cp mode=0555
file path=usr/xpg4/bin/crontab mode=4555
file path=usr/xpg4/bin/ctags mode=0555
@@ -83,6 +81,8 @@ license lic_OSBL_preamble license=lic_OSBL_preamble
link path=usr/xpg4/bin/alias target=../../bin/alias
link path=usr/xpg4/bin/bg target=../../bin/alias
link path=usr/xpg4/bin/cd target=../../bin/alias
+link path=usr/xpg4/bin/chgrp target=../../bin/chgrp
+link path=usr/xpg4/bin/chown target=../../bin/chown
link path=usr/xpg4/bin/command target=../../bin/alias
link path=usr/xpg4/bin/df target=../../sbin/df
link path=usr/xpg4/bin/egrep target=../../bin/grep
diff --git a/usr/src/test/util-tests/runfiles/default.run b/usr/src/test/util-tests/runfiles/default.run
index b328670f8e..3cccf62ac9 100644
--- a/usr/src/test/util-tests/runfiles/default.run
+++ b/usr/src/test/util-tests/runfiles/default.run
@@ -52,6 +52,7 @@ tests = [ 'runtests.sh' ]
[/opt/util-tests/tests/grep_test]
[/opt/util-tests/tests/date_test]
+[/opt/util-tests/tests/chown_test]
[/opt/util-tests/tests/demangle]
tests = ['afl-fast', 'gcc-libstdc++', 'llvm-stdcxxabi']
diff --git a/usr/src/test/util-tests/tests/Makefile b/usr/src/test/util-tests/tests/Makefile
index f947f4dc71..c84899363e 100644
--- a/usr/src/test/util-tests/tests/Makefile
+++ b/usr/src/test/util-tests/tests/Makefile
@@ -19,6 +19,7 @@
SUBDIRS = date dis dladm iconv libnvpair_json libsff printf xargs grep_xpg4
SUBDIRS += demangle mergeq workq
+SUBDIRS += demangle mergeq workq chown
SUBDIRS += bunyan awk smbios libjedec
include $(SRC)/test/Makefile.com
diff --git a/usr/src/test/util-tests/tests/chown/Makefile b/usr/src/test/util-tests/tests/chown/Makefile
new file mode 100644
index 0000000000..52c70f8b3c
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/Makefile
@@ -0,0 +1,45 @@
+#
+# This file and its contents are supplied under the terms of the
+# Common Development and Distribution License ("CDDL"), version 1.0.
+# You may only use this file in accordance with the terms of version
+# 1.0 of the CDDL.
+#
+# A full copy of the text of the CDDL should have accompanied this
+# source. A copy of the CDDL is also available via the Internet at
+# http://www.illumos.org/license/CDDL.
+#
+
+#
+# Copyright (c) 2013 by Delphix. All rights reserved.
+# Copyright 2014 Nexenta Systems, Inc. All rights reserved.
+# Copyright 2019 Alexander Pyhalov
+#
+
+.PARALLEL: $(SUBDIRS)
+
+include $(SRC)/cmd/Makefile.cmd
+include $(SRC)/test/Makefile.com
+
+ROOTOPTPKG = $(ROOT)/opt/util-tests
+TESTDIR = $(ROOTOPTPKG)/tests
+
+PROGS = chown_test
+
+CMDS = $(PROGS:%=$(TESTDIR)/%)
+$(CMDS) := FILEMODE = 0555
+
+all lint clean clobber:
+
+install: $(CMDS)
+
+$(CMDS): $(TESTDIR)
+
+$(TESTDIR):
+ $(INS.dir)
+
+$(TESTDIR)/%: %.ksh
+ $(INS.rename)
+
+SUBDIRS = files
+
+include $(SRC)/test/Makefile.com
diff --git a/usr/src/test/util-tests/tests/chown/chown_test.ksh b/usr/src/test/util-tests/tests/chown/chown_test.ksh
new file mode 100644
index 0000000000..d3f3478a9d
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/chown_test.ksh
@@ -0,0 +1,103 @@
+#! /usr/bin/ksh
+#
+#
+# This file and its contents are supplied under the terms of the
+# Common Development and Distribution License ("CDDL"), version 1.0.
+# You may only use this file in accordance with the terms of version
+# 1.0 of the CDDL.
+#
+# A full copy of the text of the CDDL should have accompanied this
+# source. A copy of the CDDL is also available via the Internet at
+# http://www.illumos.org/license/CDDL.
+#
+
+#
+# Copyright 2017 Nexenta Systems, Inc. All rights reserved.
+# Copyright 2019 Alexander Pyhalov
+#
+
+CHOWN=${CHOWN:=/usr/bin/chown}
+FILEDIR=/opt/util-tests/tests/files
+
+fail() {
+ echo $1
+ exit -1
+}
+
+create_test_hier() {
+ mkdir -p $1/src $1/dst
+ touch $1/target
+ touch $1/file
+ touch $1/dst/file1
+ touch $1/src/file2
+ ln -s ../target $1/src/tlink
+ ln -s ../dst $1/src/dstlink
+ ln -s target $1/tlink
+}
+
+SUCCESSFLAGS="
+-f
+-h
+-fhR
+-R
+-RH
+-RP
+-RL
+-Rh
+-RHL
+-RPH
+-RLP"
+
+FAILFLAGS="-RPh
+-RLh
+-RHh
+-P
+-H
+-L"
+
+NEWOWNER=daemon
+
+# We set PATH to /bin to try get ksh chown builtin
+# and to ensure that /usr/bin/chown is used instead.
+export PATH=/bin
+
+# We want unified output from tools
+export LC_ALL=en_US.UTF-8
+
+i=0
+echo "$SUCCESSFLAGS" | while read flags; do
+ print -n "test $i: chown $flags: "
+ TD=$(mktemp -d -t)
+ if [ -d "$TD" ]; then
+ create_test_hier $TD
+ chown $flags $NEWOWNER $TD/src || fail "chown $flags $NEWOWNER $TD/src failed on exit"
+ chown $flags $NEWOWNER $TD/tlink || fail "chown $flags $NEWOWNER $TD/tlink failed on exit"
+ chown $flags $NEWOWNER $TD/file || fail "chown $flags $NEWOWNER $TD/file failed on exit"
+ (cd $TD ; find . -ls |\
+ awk ' { print $3 " " $5 " " $11 }'|\
+ sort -k 3 > /tmp/out.$$)
+ if [ -n "$(diff /tmp/out.$$ $FILEDIR/cout$i)" ]; then
+ print "$(diff -u /tmp/out.$$ $FILEDIR/cout$i)"
+ fail "result is different"
+ fi
+ echo "passed"
+ rm -fr $TD /tmp/out.$$
+ else
+ fail "couldn't create $TD"
+ fi
+ ((i++))
+done
+
+echo "$FAILFLAGS" | while read flags; do
+ print -n "test $i: chown $flags: "
+ TD=$(mktemp -d -t)
+ if [ -d "$TD" ]; then
+ create_test_hier $TD
+ chown $flags $NEWOWNER $TD/file && fail "chown $flags $NEWOWNER $TD/file should have failed"
+ echo "passed"
+ rm -fr $TD
+ else
+ fail "couldn't create $TD"
+ fi
+ ((i++))
+done
diff --git a/usr/src/test/util-tests/tests/chown/files/Makefile b/usr/src/test/util-tests/tests/chown/files/Makefile
new file mode 100644
index 0000000000..4c46abaa63
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/Makefile
@@ -0,0 +1,50 @@
+#
+# This file and its contents are supplied under the terms of the
+# Common Development and Distribution License ("CDDL"), version 1.0.
+# You may only use this file in accordance with the terms of version
+# 1.0 of the CDDL.
+#
+# A full copy of the text of the CDDL should have accompanied this
+# source. A copy of the CDDL is also available via the Internet at
+# http://www.illumos.org/license/CDDL.
+#
+
+#
+# Copyright (c) 2013 by Delphix. All rights reserved.
+# Copyright 2014 Nexenta Systems, Inc. All rights reserved.
+# Copyright 2019 Alexander Pyhalov
+#
+
+include $(SRC)/cmd/Makefile.cmd
+include $(SRC)/test/Makefile.com
+
+ROOTOPTPKG = $(ROOT)/opt/util-tests
+TESTDIR = $(ROOTOPTPKG)/tests/files
+
+PROGS = cout0 \
+ cout1 \
+ cout2 \
+ cout3 \
+ cout4 \
+ cout5 \
+ cout6 \
+ cout7 \
+ cout8 \
+ cout9 \
+ cout10 \
+ cout11
+
+CMDS = $(PROGS:%=$(TESTDIR)/%)
+$(CMDS) := FILEMODE = 0444
+
+all lint clean clobber:
+
+install: $(CMDS)
+
+$(CMDS): $(TESTDIR)
+
+$(TESTDIR):
+ $(INS.dir)
+
+$(TESTDIR)/%: %
+ $(INS.file)
diff --git a/usr/src/test/util-tests/tests/chown/files/cout0 b/usr/src/test/util-tests/tests/chown/files/cout0
new file mode 100644
index 0000000000..d12a3e0a90
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout0
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x root ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx root ./src/dstlink
+-rw-r--r-- root ./src/file2
+lrwxrwxrwx root ./src/tlink
+-rw-r--r-- daemon ./target
+lrwxrwxrwx root ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout1 b/usr/src/test/util-tests/tests/chown/files/cout1
new file mode 100644
index 0000000000..d12a3e0a90
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout1
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x root ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx root ./src/dstlink
+-rw-r--r-- root ./src/file2
+lrwxrwxrwx root ./src/tlink
+-rw-r--r-- daemon ./target
+lrwxrwxrwx root ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout10 b/usr/src/test/util-tests/tests/chown/files/cout10
new file mode 100644
index 0000000000..f128e53d0a
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout10
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x daemon ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx root ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx root ./src/tlink
+-rw-r--r-- daemon ./target
+lrwxrwxrwx root ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout11 b/usr/src/test/util-tests/tests/chown/files/cout11
new file mode 100644
index 0000000000..e990d2c3d7
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout11
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x root ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx daemon ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx daemon ./src/tlink
+-rw-r--r-- root ./target
+lrwxrwxrwx daemon ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout12 b/usr/src/test/util-tests/tests/chown/files/cout12
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout12
diff --git a/usr/src/test/util-tests/tests/chown/files/cout2 b/usr/src/test/util-tests/tests/chown/files/cout2
new file mode 100644
index 0000000000..68b3cfaf8b
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout2
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x root ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx root ./src/dstlink
+-rw-r--r-- root ./src/file2
+lrwxrwxrwx root ./src/tlink
+-rw-r--r-- root ./target
+lrwxrwxrwx daemon ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout3 b/usr/src/test/util-tests/tests/chown/files/cout3
new file mode 100644
index 0000000000..e990d2c3d7
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout3
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x root ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx daemon ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx daemon ./src/tlink
+-rw-r--r-- root ./target
+lrwxrwxrwx daemon ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout4 b/usr/src/test/util-tests/tests/chown/files/cout4
new file mode 100644
index 0000000000..e990d2c3d7
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout4
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x root ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx daemon ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx daemon ./src/tlink
+-rw-r--r-- root ./target
+lrwxrwxrwx daemon ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout5 b/usr/src/test/util-tests/tests/chown/files/cout5
new file mode 100644
index 0000000000..f128e53d0a
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout5
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x daemon ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx root ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx root ./src/tlink
+-rw-r--r-- daemon ./target
+lrwxrwxrwx root ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout6 b/usr/src/test/util-tests/tests/chown/files/cout6
new file mode 100644
index 0000000000..e990d2c3d7
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout6
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x root ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx daemon ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx daemon ./src/tlink
+-rw-r--r-- root ./target
+lrwxrwxrwx daemon ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout7 b/usr/src/test/util-tests/tests/chown/files/cout7
new file mode 100644
index 0000000000..dcf411a1e5
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout7
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x daemon ./dst
+-rw-r--r-- daemon ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx root ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx root ./src/tlink
+-rw-r--r-- daemon ./target
+lrwxrwxrwx root ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout8 b/usr/src/test/util-tests/tests/chown/files/cout8
new file mode 100644
index 0000000000..e990d2c3d7
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout8
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x root ./dst
+-rw-r--r-- root ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx daemon ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx daemon ./src/tlink
+-rw-r--r-- root ./target
+lrwxrwxrwx daemon ./tlink
diff --git a/usr/src/test/util-tests/tests/chown/files/cout9 b/usr/src/test/util-tests/tests/chown/files/cout9
new file mode 100644
index 0000000000..dcf411a1e5
--- /dev/null
+++ b/usr/src/test/util-tests/tests/chown/files/cout9
@@ -0,0 +1,10 @@
+drwx------ root .
+drwxr-xr-x daemon ./dst
+-rw-r--r-- daemon ./dst/file1
+-rw-r--r-- daemon ./file
+drwxr-xr-x daemon ./src
+lrwxrwxrwx root ./src/dstlink
+-rw-r--r-- daemon ./src/file2
+lrwxrwxrwx root ./src/tlink
+-rw-r--r-- daemon ./target
+lrwxrwxrwx root ./tlink
diff --git a/usr/src/test/zfs-tests/tests/functional/snapshot/deadlist_lock.ksh b/usr/src/test/zfs-tests/tests/functional/snapshot/deadlist_lock.ksh
index 47f912dc9c..a592916a61 100644
--- a/usr/src/test/zfs-tests/tests/functional/snapshot/deadlist_lock.ksh
+++ b/usr/src/test/zfs-tests/tests/functional/snapshot/deadlist_lock.ksh
@@ -13,6 +13,7 @@
#
# Copyright (c) 2016 by Delphix. All rights reserved.
+# Copyright 2019 Joyent, Inc.
#
. $STF_SUITE/include/libtest.shlib
@@ -43,15 +44,15 @@
# snapshot right after you create a new test file.
# 2. Start DTrace in the background to put a delay in the
# sync thread after it closes the empty bpobj and before
-# it reopens it.
+# it reopens it. The dtrace process is set to exit when this
+# script exits.
# 3. Start a process in the backgroud that runs zfs-destroy
# dry-runs in an infinite loop. The idea is to keep calling
# dsl_deadlist_space_range().
# 4. Go ahead and start removing the test files. This should
# start populating the deadlist of each snapshot with
# entries and go through the dle_enqueue() target code.
-# 5. If the test passes, kill the process running on a loop
-# and dtrace, and cleanup the dataset.
+# 5. Kill the 'zfs destroy' loop and clean up the dataset.
#
verify_runnable "both"
@@ -61,7 +62,7 @@ DLDS="dl_race"
function cleanup
{
- log_must kill -9 $DLOOP_PID $DTRACE_PID
+ log_must kill -9 $DLOOP_PID
log_must zfs destroy -fR $TESTPOOL/$TESTFS/$DLDS
}
@@ -86,8 +87,7 @@ log_onexit cleanup
setup
log_must sync
-log_must dtrace -qwn "fbt::bpobj_decr_empty:entry { chill(500000000); }" &
-DTRACE_PID="$!"
+log_must dtrace -p "$PPID" -qwn "fbt::bpobj_decr_empty:entry { chill(500000000); }" &
sleep 1
destroy_nv_loop &
@@ -100,5 +100,5 @@ done
log_must sync
log_pass "There should be no race condition when an administrative command" \
- " attempts to read a deadlist's entries at the same time a that a sync" \
+ " attempts to read a deadlist's entries while a sync" \
" thread is manipulating it."
diff --git a/usr/src/uts/common/Makefile.files b/usr/src/uts/common/Makefile.files
index 0c734ff062..8b236155fb 100644
--- a/usr/src/uts/common/Makefile.files
+++ b/usr/src/uts/common/Makefile.files
@@ -25,7 +25,7 @@
# Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
# Copyright 2018 Nexenta Systems, Inc.
# Copyright 2016 Garrett D'Amore <garrett@damore.org>
-# Copyright 2017 Joyent, Inc.
+# Copyright 2019 Joyent, Inc.
# Copyright 2016 OmniTI Computer Consulting, Inc. All rights reserved.
# Copyright 2016 Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
#
@@ -1356,6 +1356,7 @@ ZFS_COMMON_OBJS += \
ddt_zap.o \
dmu.o \
dmu_diff.o \
+ dmu_recv.o \
dmu_send.o \
dmu_object.o \
dmu_objset.o \
diff --git a/usr/src/uts/common/fs/zfs/dmu_recv.c b/usr/src/uts/common/fs/zfs/dmu_recv.c
new file mode 100644
index 0000000000..bee41bd95e
--- /dev/null
+++ b/usr/src/uts/common/fs/zfs/dmu_recv.c
@@ -0,0 +1,2264 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
+ * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
+ * Copyright (c) 2014, Joyent, Inc. All rights reserved.
+ * Copyright 2014 HybridCluster. All rights reserved.
+ * Copyright 2016 RackTop Systems.
+ * Copyright (c) 2014 Integros [integros.com]
+ */
+
+#include <sys/dmu.h>
+#include <sys/dmu_impl.h>
+#include <sys/dmu_tx.h>
+#include <sys/dbuf.h>
+#include <sys/dnode.h>
+#include <sys/zfs_context.h>
+#include <sys/dmu_objset.h>
+#include <sys/dmu_traverse.h>
+#include <sys/dsl_dataset.h>
+#include <sys/dsl_dir.h>
+#include <sys/dsl_prop.h>
+#include <sys/dsl_pool.h>
+#include <sys/dsl_synctask.h>
+#include <sys/zfs_ioctl.h>
+#include <sys/zap.h>
+#include <sys/zio_checksum.h>
+#include <sys/zfs_znode.h>
+#include <zfs_fletcher.h>
+#include <sys/avl.h>
+#include <sys/ddt.h>
+#include <sys/zfs_onexit.h>
+#include <sys/dmu_recv.h>
+#include <sys/dsl_destroy.h>
+#include <sys/blkptr.h>
+#include <sys/dsl_bookmark.h>
+#include <sys/zfeature.h>
+#include <sys/bqueue.h>
+
+int zfs_recv_queue_length = SPA_MAXBLOCKSIZE;
+
+static char *dmu_recv_tag = "dmu_recv_tag";
+const char *recv_clone_name = "%recv";
+
+static void byteswap_record(dmu_replay_record_t *drr);
+
+typedef struct dmu_recv_begin_arg {
+ const char *drba_origin;
+ dmu_recv_cookie_t *drba_cookie;
+ cred_t *drba_cred;
+ uint64_t drba_snapobj;
+} dmu_recv_begin_arg_t;
+
+static int
+recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
+ uint64_t fromguid)
+{
+ uint64_t val;
+ int error;
+ dsl_pool_t *dp = ds->ds_dir->dd_pool;
+
+ /* temporary clone name must not exist */
+ error = zap_lookup(dp->dp_meta_objset,
+ dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
+ 8, 1, &val);
+ if (error != ENOENT)
+ return (error == 0 ? EBUSY : error);
+
+ /* new snapshot name must not exist */
+ error = zap_lookup(dp->dp_meta_objset,
+ dsl_dataset_phys(ds)->ds_snapnames_zapobj,
+ drba->drba_cookie->drc_tosnap, 8, 1, &val);
+ if (error != ENOENT)
+ return (error == 0 ? EEXIST : error);
+
+ /*
+ * Check snapshot limit before receiving. We'll recheck again at the
+ * end, but might as well abort before receiving if we're already over
+ * the limit.
+ *
+ * Note that we do not check the file system limit with
+ * dsl_dir_fscount_check because the temporary %clones don't count
+ * against that limit.
+ */
+ error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
+ NULL, drba->drba_cred);
+ if (error != 0)
+ return (error);
+
+ if (fromguid != 0) {
+ dsl_dataset_t *snap;
+ uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
+
+ /* Find snapshot in this dir that matches fromguid. */
+ while (obj != 0) {
+ error = dsl_dataset_hold_obj(dp, obj, FTAG,
+ &snap);
+ if (error != 0)
+ return (SET_ERROR(ENODEV));
+ if (snap->ds_dir != ds->ds_dir) {
+ dsl_dataset_rele(snap, FTAG);
+ return (SET_ERROR(ENODEV));
+ }
+ if (dsl_dataset_phys(snap)->ds_guid == fromguid)
+ break;
+ obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
+ dsl_dataset_rele(snap, FTAG);
+ }
+ if (obj == 0)
+ return (SET_ERROR(ENODEV));
+
+ if (drba->drba_cookie->drc_force) {
+ drba->drba_snapobj = obj;
+ } else {
+ /*
+ * If we are not forcing, there must be no
+ * changes since fromsnap.
+ */
+ if (dsl_dataset_modified_since_snap(ds, snap)) {
+ dsl_dataset_rele(snap, FTAG);
+ return (SET_ERROR(ETXTBSY));
+ }
+ drba->drba_snapobj = ds->ds_prev->ds_object;
+ }
+
+ dsl_dataset_rele(snap, FTAG);
+ } else {
+ /* if full, then must be forced */
+ if (!drba->drba_cookie->drc_force)
+ return (SET_ERROR(EEXIST));
+ /* start from $ORIGIN@$ORIGIN, if supported */
+ drba->drba_snapobj = dp->dp_origin_snap != NULL ?
+ dp->dp_origin_snap->ds_object : 0;
+ }
+
+ return (0);
+
+}
+
+static int
+dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
+{
+ dmu_recv_begin_arg_t *drba = arg;
+ dsl_pool_t *dp = dmu_tx_pool(tx);
+ struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
+ uint64_t fromguid = drrb->drr_fromguid;
+ int flags = drrb->drr_flags;
+ int error;
+ uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
+ dsl_dataset_t *ds;
+ const char *tofs = drba->drba_cookie->drc_tofs;
+
+ /* already checked */
+ ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
+ ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
+
+ if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
+ DMU_COMPOUNDSTREAM ||
+ drrb->drr_type >= DMU_OST_NUMTYPES ||
+ ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
+ return (SET_ERROR(EINVAL));
+
+ /* Verify pool version supports SA if SA_SPILL feature set */
+ if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
+ spa_version(dp->dp_spa) < SPA_VERSION_SA)
+ return (SET_ERROR(ENOTSUP));
+
+ if (drba->drba_cookie->drc_resumable &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
+ return (SET_ERROR(ENOTSUP));
+
+ /*
+ * The receiving code doesn't know how to translate a WRITE_EMBEDDED
+ * record to a plain WRITE record, so the pool must have the
+ * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
+ * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
+ */
+ if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
+ return (SET_ERROR(ENOTSUP));
+ if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
+ return (SET_ERROR(ENOTSUP));
+
+ /*
+ * The receiving code doesn't know how to translate large blocks
+ * to smaller ones, so the pool must have the LARGE_BLOCKS
+ * feature enabled if the stream has LARGE_BLOCKS. Same with
+ * large dnodes.
+ */
+ if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
+ return (SET_ERROR(ENOTSUP));
+ if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
+ return (SET_ERROR(ENOTSUP));
+
+ error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
+ if (error == 0) {
+ /* target fs already exists; recv into temp clone */
+
+ /* Can't recv a clone into an existing fs */
+ if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(EINVAL));
+ }
+
+ error = recv_begin_check_existing_impl(drba, ds, fromguid);
+ dsl_dataset_rele(ds, FTAG);
+ } else if (error == ENOENT) {
+ /* target fs does not exist; must be a full backup or clone */
+ char buf[ZFS_MAX_DATASET_NAME_LEN];
+
+ /*
+ * If it's a non-clone incremental, we are missing the
+ * target fs, so fail the recv.
+ */
+ if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
+ drba->drba_origin))
+ return (SET_ERROR(ENOENT));
+
+ /*
+ * If we're receiving a full send as a clone, and it doesn't
+ * contain all the necessary free records and freeobject
+ * records, reject it.
+ */
+ if (fromguid == 0 && drba->drba_origin &&
+ !(flags & DRR_FLAG_FREERECORDS))
+ return (SET_ERROR(EINVAL));
+
+ /* Open the parent of tofs */
+ ASSERT3U(strlen(tofs), <, sizeof (buf));
+ (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
+ error = dsl_dataset_hold(dp, buf, FTAG, &ds);
+ if (error != 0)
+ return (error);
+
+ /*
+ * Check filesystem and snapshot limits before receiving. We'll
+ * recheck snapshot limits again at the end (we create the
+ * filesystems and increment those counts during begin_sync).
+ */
+ error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
+ ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
+ if (error != 0) {
+ dsl_dataset_rele(ds, FTAG);
+ return (error);
+ }
+
+ error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
+ ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
+ if (error != 0) {
+ dsl_dataset_rele(ds, FTAG);
+ return (error);
+ }
+
+ if (drba->drba_origin != NULL) {
+ dsl_dataset_t *origin;
+ error = dsl_dataset_hold(dp, drba->drba_origin,
+ FTAG, &origin);
+ if (error != 0) {
+ dsl_dataset_rele(ds, FTAG);
+ return (error);
+ }
+ if (!origin->ds_is_snapshot) {
+ dsl_dataset_rele(origin, FTAG);
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(EINVAL));
+ }
+ if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
+ fromguid != 0) {
+ dsl_dataset_rele(origin, FTAG);
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(ENODEV));
+ }
+ dsl_dataset_rele(origin, FTAG);
+ }
+ dsl_dataset_rele(ds, FTAG);
+ error = 0;
+ }
+ return (error);
+}
+
+static void
+dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
+{
+ dmu_recv_begin_arg_t *drba = arg;
+ dsl_pool_t *dp = dmu_tx_pool(tx);
+ objset_t *mos = dp->dp_meta_objset;
+ struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
+ const char *tofs = drba->drba_cookie->drc_tofs;
+ dsl_dataset_t *ds, *newds;
+ uint64_t dsobj;
+ int error;
+ uint64_t crflags = 0;
+
+ if (drrb->drr_flags & DRR_FLAG_CI_DATA)
+ crflags |= DS_FLAG_CI_DATASET;
+
+ error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
+ if (error == 0) {
+ /* create temporary clone */
+ dsl_dataset_t *snap = NULL;
+ if (drba->drba_snapobj != 0) {
+ VERIFY0(dsl_dataset_hold_obj(dp,
+ drba->drba_snapobj, FTAG, &snap));
+ }
+ dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
+ snap, crflags, drba->drba_cred, tx);
+ if (drba->drba_snapobj != 0)
+ dsl_dataset_rele(snap, FTAG);
+ dsl_dataset_rele(ds, FTAG);
+ } else {
+ dsl_dir_t *dd;
+ const char *tail;
+ dsl_dataset_t *origin = NULL;
+
+ VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
+
+ if (drba->drba_origin != NULL) {
+ VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
+ FTAG, &origin));
+ }
+
+ /* Create new dataset. */
+ dsobj = dsl_dataset_create_sync(dd,
+ strrchr(tofs, '/') + 1,
+ origin, crflags, drba->drba_cred, tx);
+ if (origin != NULL)
+ dsl_dataset_rele(origin, FTAG);
+ dsl_dir_rele(dd, FTAG);
+ drba->drba_cookie->drc_newfs = B_TRUE;
+ }
+ VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
+
+ if (drba->drba_cookie->drc_resumable) {
+ dsl_dataset_zapify(newds, tx);
+ if (drrb->drr_fromguid != 0) {
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
+ 8, 1, &drrb->drr_fromguid, tx));
+ }
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
+ 8, 1, &drrb->drr_toguid, tx));
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
+ 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
+ uint64_t one = 1;
+ uint64_t zero = 0;
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
+ 8, 1, &one, tx));
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
+ 8, 1, &zero, tx));
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
+ 8, 1, &zero, tx));
+ if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
+ DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
+ 8, 1, &one, tx));
+ }
+ if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
+ DMU_BACKUP_FEATURE_EMBED_DATA) {
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
+ 8, 1, &one, tx));
+ }
+ if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
+ DMU_BACKUP_FEATURE_COMPRESSED) {
+ VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
+ 8, 1, &one, tx));
+ }
+ }
+
+ dmu_buf_will_dirty(newds->ds_dbuf, tx);
+ dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
+
+ /*
+ * If we actually created a non-clone, we need to create the
+ * objset in our new dataset.
+ */
+ rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
+ if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
+ (void) dmu_objset_create_impl(dp->dp_spa,
+ newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
+ }
+ rrw_exit(&newds->ds_bp_rwlock, FTAG);
+
+ drba->drba_cookie->drc_ds = newds;
+
+ spa_history_log_internal_ds(newds, "receive", tx, "");
+}
+
+static int
+dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
+{
+ dmu_recv_begin_arg_t *drba = arg;
+ dsl_pool_t *dp = dmu_tx_pool(tx);
+ struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
+ int error;
+ uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
+ dsl_dataset_t *ds;
+ const char *tofs = drba->drba_cookie->drc_tofs;
+
+ /* 6 extra bytes for /%recv */
+ char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
+
+ /* already checked */
+ ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
+ ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
+
+ if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
+ DMU_COMPOUNDSTREAM ||
+ drrb->drr_type >= DMU_OST_NUMTYPES)
+ return (SET_ERROR(EINVAL));
+
+ /* Verify pool version supports SA if SA_SPILL feature set */
+ if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
+ spa_version(dp->dp_spa) < SPA_VERSION_SA)
+ return (SET_ERROR(ENOTSUP));
+
+ /*
+ * The receiving code doesn't know how to translate a WRITE_EMBEDDED
+ * record to a plain WRITE record, so the pool must have the
+ * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
+ * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
+ */
+ if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
+ return (SET_ERROR(ENOTSUP));
+ if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
+ return (SET_ERROR(ENOTSUP));
+
+ /*
+ * The receiving code doesn't know how to translate large blocks
+ * to smaller ones, so the pool must have the LARGE_BLOCKS
+ * feature enabled if the stream has LARGE_BLOCKS. Same with
+ * large dnodes.
+ */
+ if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
+ return (SET_ERROR(ENOTSUP));
+ if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
+ !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
+ return (SET_ERROR(ENOTSUP));
+
+ (void) snprintf(recvname, sizeof (recvname), "%s/%s",
+ tofs, recv_clone_name);
+
+ if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
+ /* %recv does not exist; continue in tofs */
+ error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
+ if (error != 0)
+ return (error);
+ }
+
+ /* check that ds is marked inconsistent */
+ if (!DS_IS_INCONSISTENT(ds)) {
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(EINVAL));
+ }
+
+ /* check that there is resuming data, and that the toguid matches */
+ if (!dsl_dataset_is_zapified(ds)) {
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(EINVAL));
+ }
+ uint64_t val;
+ error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
+ DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
+ if (error != 0 || drrb->drr_toguid != val) {
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(EINVAL));
+ }
+
+ /*
+ * Check if the receive is still running. If so, it will be owned.
+ * Note that nothing else can own the dataset (e.g. after the receive
+ * fails) because it will be marked inconsistent.
+ */
+ if (dsl_dataset_has_owner(ds)) {
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(EBUSY));
+ }
+
+ /* There should not be any snapshots of this fs yet. */
+ if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(EINVAL));
+ }
+
+ /*
+ * Note: resume point will be checked when we process the first WRITE
+ * record.
+ */
+
+ /* check that the origin matches */
+ val = 0;
+ (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
+ DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
+ if (drrb->drr_fromguid != val) {
+ dsl_dataset_rele(ds, FTAG);
+ return (SET_ERROR(EINVAL));
+ }
+
+ dsl_dataset_rele(ds, FTAG);
+ return (0);
+}
+
+static void
+dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
+{
+ dmu_recv_begin_arg_t *drba = arg;
+ dsl_pool_t *dp = dmu_tx_pool(tx);
+ const char *tofs = drba->drba_cookie->drc_tofs;
+ dsl_dataset_t *ds;
+ uint64_t dsobj;
+ /* 6 extra bytes for /%recv */
+ char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
+
+ (void) snprintf(recvname, sizeof (recvname), "%s/%s",
+ tofs, recv_clone_name);
+
+ if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
+ /* %recv does not exist; continue in tofs */
+ VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
+ drba->drba_cookie->drc_newfs = B_TRUE;
+ }
+
+ /* clear the inconsistent flag so that we can own it */
+ ASSERT(DS_IS_INCONSISTENT(ds));
+ dmu_buf_will_dirty(ds->ds_dbuf, tx);
+ dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
+ dsobj = ds->ds_object;
+ dsl_dataset_rele(ds, FTAG);
+
+ VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
+
+ dmu_buf_will_dirty(ds->ds_dbuf, tx);
+ dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
+
+ rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
+ ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
+ rrw_exit(&ds->ds_bp_rwlock, FTAG);
+
+ drba->drba_cookie->drc_ds = ds;
+
+ spa_history_log_internal_ds(ds, "resume receive", tx, "");
+}
+
+/*
+ * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
+ * succeeds; otherwise we will leak the holds on the datasets.
+ */
+int
+dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
+ boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
+{
+ dmu_recv_begin_arg_t drba = { 0 };
+
+ bzero(drc, sizeof (dmu_recv_cookie_t));
+ drc->drc_drr_begin = drr_begin;
+ drc->drc_drrb = &drr_begin->drr_u.drr_begin;
+ drc->drc_tosnap = tosnap;
+ drc->drc_tofs = tofs;
+ drc->drc_force = force;
+ drc->drc_resumable = resumable;
+ drc->drc_cred = CRED();
+ drc->drc_clone = (origin != NULL);
+
+ if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
+ drc->drc_byteswap = B_TRUE;
+ (void) fletcher_4_incremental_byteswap(drr_begin,
+ sizeof (dmu_replay_record_t), &drc->drc_cksum);
+ byteswap_record(drr_begin);
+ } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
+ (void) fletcher_4_incremental_native(drr_begin,
+ sizeof (dmu_replay_record_t), &drc->drc_cksum);
+ } else {
+ return (SET_ERROR(EINVAL));
+ }
+
+ drba.drba_origin = origin;
+ drba.drba_cookie = drc;
+ drba.drba_cred = CRED();
+
+ if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
+ DMU_BACKUP_FEATURE_RESUMING) {
+ return (dsl_sync_task(tofs,
+ dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
+ &drba, 5, ZFS_SPACE_CHECK_NORMAL));
+ } else {
+ return (dsl_sync_task(tofs,
+ dmu_recv_begin_check, dmu_recv_begin_sync,
+ &drba, 5, ZFS_SPACE_CHECK_NORMAL));
+ }
+}
+
+struct receive_record_arg {
+ dmu_replay_record_t header;
+ void *payload; /* Pointer to a buffer containing the payload */
+ /*
+ * If the record is a write, pointer to the arc_buf_t containing the
+ * payload.
+ */
+ arc_buf_t *write_buf;
+ int payload_size;
+ uint64_t bytes_read; /* bytes read from stream when record created */
+ boolean_t eos_marker; /* Marks the end of the stream */
+ bqueue_node_t node;
+};
+
+struct receive_writer_arg {
+ objset_t *os;
+ boolean_t byteswap;
+ bqueue_t q;
+
+ /*
+ * These three args are used to signal to the main thread that we're
+ * done.
+ */
+ kmutex_t mutex;
+ kcondvar_t cv;
+ boolean_t done;
+
+ int err;
+ /* A map from guid to dataset to help handle dedup'd streams. */
+ avl_tree_t *guid_to_ds_map;
+ boolean_t resumable;
+ uint64_t last_object;
+ uint64_t last_offset;
+ uint64_t max_object; /* highest object ID referenced in stream */
+ uint64_t bytes_read; /* bytes read when current record created */
+};
+
+struct objlist {
+ list_t list; /* List of struct receive_objnode. */
+ /*
+ * Last object looked up. Used to assert that objects are being looked
+ * up in ascending order.
+ */
+ uint64_t last_lookup;
+};
+
+struct receive_objnode {
+ list_node_t node;
+ uint64_t object;
+};
+
+struct receive_arg {
+ objset_t *os;
+ vnode_t *vp; /* The vnode to read the stream from */
+ uint64_t voff; /* The current offset in the stream */
+ uint64_t bytes_read;
+ /*
+ * A record that has had its payload read in, but hasn't yet been handed
+ * off to the worker thread.
+ */
+ struct receive_record_arg *rrd;
+ /* A record that has had its header read in, but not its payload. */
+ struct receive_record_arg *next_rrd;
+ zio_cksum_t cksum;
+ zio_cksum_t prev_cksum;
+ int err;
+ boolean_t byteswap;
+ /* Sorted list of objects not to issue prefetches for. */
+ struct objlist ignore_objlist;
+};
+
+typedef struct guid_map_entry {
+ uint64_t guid;
+ dsl_dataset_t *gme_ds;
+ avl_node_t avlnode;
+} guid_map_entry_t;
+
+static int
+guid_compare(const void *arg1, const void *arg2)
+{
+ const guid_map_entry_t *gmep1 = arg1;
+ const guid_map_entry_t *gmep2 = arg2;
+
+ if (gmep1->guid < gmep2->guid)
+ return (-1);
+ else if (gmep1->guid > gmep2->guid)
+ return (1);
+ return (0);
+}
+
+static void
+free_guid_map_onexit(void *arg)
+{
+ avl_tree_t *ca = arg;
+ void *cookie = NULL;
+ guid_map_entry_t *gmep;
+
+ while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
+ dsl_dataset_long_rele(gmep->gme_ds, gmep);
+ dsl_dataset_rele(gmep->gme_ds, gmep);
+ kmem_free(gmep, sizeof (guid_map_entry_t));
+ }
+ avl_destroy(ca);
+ kmem_free(ca, sizeof (avl_tree_t));
+}
+
+static int
+receive_read(struct receive_arg *ra, int len, void *buf)
+{
+ int done = 0;
+
+ /*
+ * The code doesn't rely on this (lengths being multiples of 8). See
+ * comment in dump_bytes.
+ */
+ ASSERT0(len % 8);
+
+ while (done < len) {
+ ssize_t resid;
+
+ ra->err = vn_rdwr(UIO_READ, ra->vp,
+ (char *)buf + done, len - done,
+ ra->voff, UIO_SYSSPACE, FAPPEND,
+ RLIM64_INFINITY, CRED(), &resid);
+
+ if (resid == len - done) {
+ /*
+ * Note: ECKSUM indicates that the receive
+ * was interrupted and can potentially be resumed.
+ */
+ ra->err = SET_ERROR(ECKSUM);
+ }
+ ra->voff += len - done - resid;
+ done = len - resid;
+ if (ra->err != 0)
+ return (ra->err);
+ }
+
+ ra->bytes_read += len;
+
+ ASSERT3U(done, ==, len);
+ return (0);
+}
+
+static void
+byteswap_record(dmu_replay_record_t *drr)
+{
+#define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
+#define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
+ drr->drr_type = BSWAP_32(drr->drr_type);
+ drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
+
+ switch (drr->drr_type) {
+ case DRR_BEGIN:
+ DO64(drr_begin.drr_magic);
+ DO64(drr_begin.drr_versioninfo);
+ DO64(drr_begin.drr_creation_time);
+ DO32(drr_begin.drr_type);
+ DO32(drr_begin.drr_flags);
+ DO64(drr_begin.drr_toguid);
+ DO64(drr_begin.drr_fromguid);
+ break;
+ case DRR_OBJECT:
+ DO64(drr_object.drr_object);
+ DO32(drr_object.drr_type);
+ DO32(drr_object.drr_bonustype);
+ DO32(drr_object.drr_blksz);
+ DO32(drr_object.drr_bonuslen);
+ DO64(drr_object.drr_toguid);
+ break;
+ case DRR_FREEOBJECTS:
+ DO64(drr_freeobjects.drr_firstobj);
+ DO64(drr_freeobjects.drr_numobjs);
+ DO64(drr_freeobjects.drr_toguid);
+ break;
+ case DRR_WRITE:
+ DO64(drr_write.drr_object);
+ DO32(drr_write.drr_type);
+ DO64(drr_write.drr_offset);
+ DO64(drr_write.drr_logical_size);
+ DO64(drr_write.drr_toguid);
+ ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
+ DO64(drr_write.drr_key.ddk_prop);
+ DO64(drr_write.drr_compressed_size);
+ break;
+ case DRR_WRITE_BYREF:
+ DO64(drr_write_byref.drr_object);
+ DO64(drr_write_byref.drr_offset);
+ DO64(drr_write_byref.drr_length);
+ DO64(drr_write_byref.drr_toguid);
+ DO64(drr_write_byref.drr_refguid);
+ DO64(drr_write_byref.drr_refobject);
+ DO64(drr_write_byref.drr_refoffset);
+ ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
+ drr_key.ddk_cksum);
+ DO64(drr_write_byref.drr_key.ddk_prop);
+ break;
+ case DRR_WRITE_EMBEDDED:
+ DO64(drr_write_embedded.drr_object);
+ DO64(drr_write_embedded.drr_offset);
+ DO64(drr_write_embedded.drr_length);
+ DO64(drr_write_embedded.drr_toguid);
+ DO32(drr_write_embedded.drr_lsize);
+ DO32(drr_write_embedded.drr_psize);
+ break;
+ case DRR_FREE:
+ DO64(drr_free.drr_object);
+ DO64(drr_free.drr_offset);
+ DO64(drr_free.drr_length);
+ DO64(drr_free.drr_toguid);
+ break;
+ case DRR_SPILL:
+ DO64(drr_spill.drr_object);
+ DO64(drr_spill.drr_length);
+ DO64(drr_spill.drr_toguid);
+ break;
+ case DRR_END:
+ DO64(drr_end.drr_toguid);
+ ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
+ break;
+ }
+
+ if (drr->drr_type != DRR_BEGIN) {
+ ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
+ }
+
+#undef DO64
+#undef DO32
+}
+
+static inline uint8_t
+deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
+{
+ if (bonus_type == DMU_OT_SA) {
+ return (1);
+ } else {
+ return (1 +
+ ((DN_OLD_MAX_BONUSLEN -
+ MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT));
+ }
+}
+
+static void
+save_resume_state(struct receive_writer_arg *rwa,
+ uint64_t object, uint64_t offset, dmu_tx_t *tx)
+{
+ int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
+
+ if (!rwa->resumable)
+ return;
+
+ /*
+ * We use ds_resume_bytes[] != 0 to indicate that we need to
+ * update this on disk, so it must not be 0.
+ */
+ ASSERT(rwa->bytes_read != 0);
+
+ /*
+ * We only resume from write records, which have a valid
+ * (non-meta-dnode) object number.
+ */
+ ASSERT(object != 0);
+
+ /*
+ * For resuming to work correctly, we must receive records in order,
+ * sorted by object,offset. This is checked by the callers, but
+ * assert it here for good measure.
+ */
+ ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
+ ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
+ offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
+ ASSERT3U(rwa->bytes_read, >=,
+ rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
+
+ rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
+ rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
+ rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
+}
+
+static int
+receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
+ void *data)
+{
+ dmu_object_info_t doi;
+ dmu_tx_t *tx;
+ uint64_t object;
+ int err;
+ uint8_t dn_slots = drro->drr_dn_slots != 0 ?
+ drro->drr_dn_slots : DNODE_MIN_SLOTS;
+
+ if (drro->drr_type == DMU_OT_NONE ||
+ !DMU_OT_IS_VALID(drro->drr_type) ||
+ !DMU_OT_IS_VALID(drro->drr_bonustype) ||
+ drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
+ drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
+ P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
+ drro->drr_blksz < SPA_MINBLOCKSIZE ||
+ drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
+ drro->drr_bonuslen >
+ DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa->os))) ||
+ dn_slots >
+ (spa_maxdnodesize(dmu_objset_spa(rwa->os)) >> DNODE_SHIFT)) {
+ return (SET_ERROR(EINVAL));
+ }
+
+ err = dmu_object_info(rwa->os, drro->drr_object, &doi);
+
+ if (err != 0 && err != ENOENT && err != EEXIST)
+ return (SET_ERROR(EINVAL));
+
+ if (drro->drr_object > rwa->max_object)
+ rwa->max_object = drro->drr_object;
+
+ /*
+ * If we are losing blkptrs or changing the block size this must
+ * be a new file instance. We must clear out the previous file
+ * contents before we can change this type of metadata in the dnode.
+ */
+ if (err == 0) {
+ int nblkptr;
+
+ object = drro->drr_object;
+
+ nblkptr = deduce_nblkptr(drro->drr_bonustype,
+ drro->drr_bonuslen);
+
+ if (drro->drr_blksz != doi.doi_data_block_size ||
+ nblkptr < doi.doi_nblkptr ||
+ dn_slots != doi.doi_dnodesize >> DNODE_SHIFT) {
+ err = dmu_free_long_range(rwa->os, drro->drr_object,
+ 0, DMU_OBJECT_END);
+ if (err != 0)
+ return (SET_ERROR(EINVAL));
+ }
+ } else if (err == EEXIST) {
+ /*
+ * The object requested is currently an interior slot of a
+ * multi-slot dnode. This will be resolved when the next txg
+ * is synced out, since the send stream will have told us
+ * to free this slot when we freed the associated dnode
+ * earlier in the stream.
+ */
+ txg_wait_synced(dmu_objset_pool(rwa->os), 0);
+ object = drro->drr_object;
+ } else {
+ /* object is free and we are about to allocate a new one */
+ object = DMU_NEW_OBJECT;
+ }
+
+ /*
+ * If this is a multi-slot dnode there is a chance that this
+ * object will expand into a slot that is already used by
+ * another object from the previous snapshot. We must free
+ * these objects before we attempt to allocate the new dnode.
+ */
+ if (dn_slots > 1) {
+ boolean_t need_sync = B_FALSE;
+
+ for (uint64_t slot = drro->drr_object + 1;
+ slot < drro->drr_object + dn_slots;
+ slot++) {
+ dmu_object_info_t slot_doi;
+
+ err = dmu_object_info(rwa->os, slot, &slot_doi);
+ if (err == ENOENT || err == EEXIST)
+ continue;
+ else if (err != 0)
+ return (err);
+
+ err = dmu_free_long_object(rwa->os, slot);
+
+ if (err != 0)
+ return (err);
+
+ need_sync = B_TRUE;
+ }
+
+ if (need_sync)
+ txg_wait_synced(dmu_objset_pool(rwa->os), 0);
+ }
+
+ tx = dmu_tx_create(rwa->os);
+ dmu_tx_hold_bonus(tx, object);
+ err = dmu_tx_assign(tx, TXG_WAIT);
+ if (err != 0) {
+ dmu_tx_abort(tx);
+ return (err);
+ }
+
+ if (object == DMU_NEW_OBJECT) {
+ /* currently free, want to be allocated */
+ err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
+ drro->drr_type, drro->drr_blksz,
+ drro->drr_bonustype, drro->drr_bonuslen,
+ dn_slots << DNODE_SHIFT, tx);
+ } else if (drro->drr_type != doi.doi_type ||
+ drro->drr_blksz != doi.doi_data_block_size ||
+ drro->drr_bonustype != doi.doi_bonus_type ||
+ drro->drr_bonuslen != doi.doi_bonus_size ||
+ drro->drr_dn_slots != (doi.doi_dnodesize >> DNODE_SHIFT)) {
+ /* currently allocated, but with different properties */
+ err = dmu_object_reclaim_dnsize(rwa->os, drro->drr_object,
+ drro->drr_type, drro->drr_blksz,
+ drro->drr_bonustype, drro->drr_bonuslen,
+ drro->drr_dn_slots << DNODE_SHIFT, tx);
+ }
+ if (err != 0) {
+ dmu_tx_commit(tx);
+ return (SET_ERROR(EINVAL));
+ }
+
+ dmu_object_set_checksum(rwa->os, drro->drr_object,
+ drro->drr_checksumtype, tx);
+ dmu_object_set_compress(rwa->os, drro->drr_object,
+ drro->drr_compress, tx);
+
+ if (data != NULL) {
+ dmu_buf_t *db;
+
+ VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
+ dmu_buf_will_dirty(db, tx);
+
+ ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
+ bcopy(data, db->db_data, drro->drr_bonuslen);
+ if (rwa->byteswap) {
+ dmu_object_byteswap_t byteswap =
+ DMU_OT_BYTESWAP(drro->drr_bonustype);
+ dmu_ot_byteswap[byteswap].ob_func(db->db_data,
+ drro->drr_bonuslen);
+ }
+ dmu_buf_rele(db, FTAG);
+ }
+ dmu_tx_commit(tx);
+
+ return (0);
+}
+
+/* ARGSUSED */
+static int
+receive_freeobjects(struct receive_writer_arg *rwa,
+ struct drr_freeobjects *drrfo)
+{
+ uint64_t obj;
+ int next_err = 0;
+
+ if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
+ return (SET_ERROR(EINVAL));
+
+ for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
+ obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
+ next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
+ int err;
+
+ err = dmu_object_info(rwa->os, obj, NULL);
+ if (err == ENOENT)
+ continue;
+ else if (err != 0)
+ return (err);
+
+ err = dmu_free_long_object(rwa->os, obj);
+ if (err != 0)
+ return (err);
+
+ if (obj > rwa->max_object)
+ rwa->max_object = obj;
+ }
+ if (next_err != ESRCH)
+ return (next_err);
+ return (0);
+}
+
+static int
+receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
+ arc_buf_t *abuf)
+{
+ dmu_tx_t *tx;
+ int err;
+
+ if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
+ !DMU_OT_IS_VALID(drrw->drr_type))
+ return (SET_ERROR(EINVAL));
+
+ /*
+ * For resuming to work, records must be in increasing order
+ * by (object, offset).
+ */
+ if (drrw->drr_object < rwa->last_object ||
+ (drrw->drr_object == rwa->last_object &&
+ drrw->drr_offset < rwa->last_offset)) {
+ return (SET_ERROR(EINVAL));
+ }
+ rwa->last_object = drrw->drr_object;
+ rwa->last_offset = drrw->drr_offset;
+
+ if (rwa->last_object > rwa->max_object)
+ rwa->max_object = rwa->last_object;
+
+ if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
+ return (SET_ERROR(EINVAL));
+
+ tx = dmu_tx_create(rwa->os);
+
+ dmu_tx_hold_write(tx, drrw->drr_object,
+ drrw->drr_offset, drrw->drr_logical_size);
+ err = dmu_tx_assign(tx, TXG_WAIT);
+ if (err != 0) {
+ dmu_tx_abort(tx);
+ return (err);
+ }
+ if (rwa->byteswap) {
+ dmu_object_byteswap_t byteswap =
+ DMU_OT_BYTESWAP(drrw->drr_type);
+ dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
+ DRR_WRITE_PAYLOAD_SIZE(drrw));
+ }
+
+ /* use the bonus buf to look up the dnode in dmu_assign_arcbuf */
+ dmu_buf_t *bonus;
+ if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
+ return (SET_ERROR(EINVAL));
+ dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
+
+ /*
+ * Note: If the receive fails, we want the resume stream to start
+ * with the same record that we last successfully received (as opposed
+ * to the next record), so that we can verify that we are
+ * resuming from the correct location.
+ */
+ save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
+ dmu_tx_commit(tx);
+ dmu_buf_rele(bonus, FTAG);
+
+ return (0);
+}
+
+/*
+ * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
+ * streams to refer to a copy of the data that is already on the
+ * system because it came in earlier in the stream. This function
+ * finds the earlier copy of the data, and uses that copy instead of
+ * data from the stream to fulfill this write.
+ */
+static int
+receive_write_byref(struct receive_writer_arg *rwa,
+ struct drr_write_byref *drrwbr)
+{
+ dmu_tx_t *tx;
+ int err;
+ guid_map_entry_t gmesrch;
+ guid_map_entry_t *gmep;
+ avl_index_t where;
+ objset_t *ref_os = NULL;
+ dmu_buf_t *dbp;
+
+ if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
+ return (SET_ERROR(EINVAL));
+
+ /*
+ * If the GUID of the referenced dataset is different from the
+ * GUID of the target dataset, find the referenced dataset.
+ */
+ if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
+ gmesrch.guid = drrwbr->drr_refguid;
+ if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
+ &where)) == NULL) {
+ return (SET_ERROR(EINVAL));
+ }
+ if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
+ return (SET_ERROR(EINVAL));
+ } else {
+ ref_os = rwa->os;
+ }
+
+ if (drrwbr->drr_object > rwa->max_object)
+ rwa->max_object = drrwbr->drr_object;
+
+ err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
+ drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
+ if (err != 0)
+ return (err);
+
+ tx = dmu_tx_create(rwa->os);
+
+ dmu_tx_hold_write(tx, drrwbr->drr_object,
+ drrwbr->drr_offset, drrwbr->drr_length);
+ err = dmu_tx_assign(tx, TXG_WAIT);
+ if (err != 0) {
+ dmu_tx_abort(tx);
+ return (err);
+ }
+ dmu_write(rwa->os, drrwbr->drr_object,
+ drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
+ dmu_buf_rele(dbp, FTAG);
+
+ /* See comment in restore_write. */
+ save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
+ dmu_tx_commit(tx);
+ return (0);
+}
+
+static int
+receive_write_embedded(struct receive_writer_arg *rwa,
+ struct drr_write_embedded *drrwe, void *data)
+{
+ dmu_tx_t *tx;
+ int err;
+
+ if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
+ return (EINVAL);
+
+ if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
+ return (EINVAL);
+
+ if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
+ return (EINVAL);
+ if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
+ return (EINVAL);
+
+ if (drrwe->drr_object > rwa->max_object)
+ rwa->max_object = drrwe->drr_object;
+
+ tx = dmu_tx_create(rwa->os);
+
+ dmu_tx_hold_write(tx, drrwe->drr_object,
+ drrwe->drr_offset, drrwe->drr_length);
+ err = dmu_tx_assign(tx, TXG_WAIT);
+ if (err != 0) {
+ dmu_tx_abort(tx);
+ return (err);
+ }
+
+ dmu_write_embedded(rwa->os, drrwe->drr_object,
+ drrwe->drr_offset, data, drrwe->drr_etype,
+ drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
+ rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
+
+ /* See comment in restore_write. */
+ save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
+ dmu_tx_commit(tx);
+ return (0);
+}
+
+static int
+receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
+ void *data)
+{
+ dmu_tx_t *tx;
+ dmu_buf_t *db, *db_spill;
+ int err;
+
+ if (drrs->drr_length < SPA_MINBLOCKSIZE ||
+ drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
+ return (SET_ERROR(EINVAL));
+
+ if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
+ return (SET_ERROR(EINVAL));
+
+ if (drrs->drr_object > rwa->max_object)
+ rwa->max_object = drrs->drr_object;
+
+ VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
+ if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
+ dmu_buf_rele(db, FTAG);
+ return (err);
+ }
+
+ tx = dmu_tx_create(rwa->os);
+
+ dmu_tx_hold_spill(tx, db->db_object);
+
+ err = dmu_tx_assign(tx, TXG_WAIT);
+ if (err != 0) {
+ dmu_buf_rele(db, FTAG);
+ dmu_buf_rele(db_spill, FTAG);
+ dmu_tx_abort(tx);
+ return (err);
+ }
+ dmu_buf_will_dirty(db_spill, tx);
+
+ if (db_spill->db_size < drrs->drr_length)
+ VERIFY(0 == dbuf_spill_set_blksz(db_spill,
+ drrs->drr_length, tx));
+ bcopy(data, db_spill->db_data, drrs->drr_length);
+
+ dmu_buf_rele(db, FTAG);
+ dmu_buf_rele(db_spill, FTAG);
+
+ dmu_tx_commit(tx);
+ return (0);
+}
+
+/* ARGSUSED */
+static int
+receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
+{
+ int err;
+
+ if (drrf->drr_length != -1ULL &&
+ drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
+ return (SET_ERROR(EINVAL));
+
+ if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
+ return (SET_ERROR(EINVAL));
+
+ if (drrf->drr_object > rwa->max_object)
+ rwa->max_object = drrf->drr_object;
+
+ err = dmu_free_long_range(rwa->os, drrf->drr_object,
+ drrf->drr_offset, drrf->drr_length);
+
+ return (err);
+}
+
+/* used to destroy the drc_ds on error */
+static void
+dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
+{
+ if (drc->drc_resumable) {
+ /* wait for our resume state to be written to disk */
+ txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
+ dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
+ } else {
+ char name[ZFS_MAX_DATASET_NAME_LEN];
+ dsl_dataset_name(drc->drc_ds, name);
+ dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
+ (void) dsl_destroy_head(name);
+ }
+}
+
+static void
+receive_cksum(struct receive_arg *ra, int len, void *buf)
+{
+ if (ra->byteswap) {
+ (void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
+ } else {
+ (void) fletcher_4_incremental_native(buf, len, &ra->cksum);
+ }
+}
+
+/*
+ * Read the payload into a buffer of size len, and update the current record's
+ * payload field.
+ * Allocate ra->next_rrd and read the next record's header into
+ * ra->next_rrd->header.
+ * Verify checksum of payload and next record.
+ */
+static int
+receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
+{
+ int err;
+
+ if (len != 0) {
+ ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
+ err = receive_read(ra, len, buf);
+ if (err != 0)
+ return (err);
+ receive_cksum(ra, len, buf);
+
+ /* note: rrd is NULL when reading the begin record's payload */
+ if (ra->rrd != NULL) {
+ ra->rrd->payload = buf;
+ ra->rrd->payload_size = len;
+ ra->rrd->bytes_read = ra->bytes_read;
+ }
+ }
+
+ ra->prev_cksum = ra->cksum;
+
+ ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
+ err = receive_read(ra, sizeof (ra->next_rrd->header),
+ &ra->next_rrd->header);
+ ra->next_rrd->bytes_read = ra->bytes_read;
+ if (err != 0) {
+ kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
+ ra->next_rrd = NULL;
+ return (err);
+ }
+ if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
+ kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
+ ra->next_rrd = NULL;
+ return (SET_ERROR(EINVAL));
+ }
+
+ /*
+ * Note: checksum is of everything up to but not including the
+ * checksum itself.
+ */
+ ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
+ ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
+ receive_cksum(ra,
+ offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
+ &ra->next_rrd->header);
+
+ zio_cksum_t cksum_orig =
+ ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
+ zio_cksum_t *cksump =
+ &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
+
+ if (ra->byteswap)
+ byteswap_record(&ra->next_rrd->header);
+
+ if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
+ !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
+ kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
+ ra->next_rrd = NULL;
+ return (SET_ERROR(ECKSUM));
+ }
+
+ receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
+
+ return (0);
+}
+
+static void
+objlist_create(struct objlist *list)
+{
+ list_create(&list->list, sizeof (struct receive_objnode),
+ offsetof(struct receive_objnode, node));
+ list->last_lookup = 0;
+}
+
+static void
+objlist_destroy(struct objlist *list)
+{
+ for (struct receive_objnode *n = list_remove_head(&list->list);
+ n != NULL; n = list_remove_head(&list->list)) {
+ kmem_free(n, sizeof (*n));
+ }
+ list_destroy(&list->list);
+}
+
+/*
+ * This function looks through the objlist to see if the specified object number
+ * is contained in the objlist. In the process, it will remove all object
+ * numbers in the list that are smaller than the specified object number. Thus,
+ * any lookup of an object number smaller than a previously looked up object
+ * number will always return false; therefore, all lookups should be done in
+ * ascending order.
+ */
+static boolean_t
+objlist_exists(struct objlist *list, uint64_t object)
+{
+ struct receive_objnode *node = list_head(&list->list);
+ ASSERT3U(object, >=, list->last_lookup);
+ list->last_lookup = object;
+ while (node != NULL && node->object < object) {
+ VERIFY3P(node, ==, list_remove_head(&list->list));
+ kmem_free(node, sizeof (*node));
+ node = list_head(&list->list);
+ }
+ return (node != NULL && node->object == object);
+}
+
+/*
+ * The objlist is a list of object numbers stored in ascending order. However,
+ * the insertion of new object numbers does not seek out the correct location to
+ * store a new object number; instead, it appends it to the list for simplicity.
+ * Thus, any users must take care to only insert new object numbers in ascending
+ * order.
+ */
+static void
+objlist_insert(struct objlist *list, uint64_t object)
+{
+ struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
+ node->object = object;
+#ifdef ZFS_DEBUG
+ struct receive_objnode *last_object = list_tail(&list->list);
+ uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
+ ASSERT3U(node->object, >, last_objnum);
+#endif
+ list_insert_tail(&list->list, node);
+}
+
+/*
+ * Issue the prefetch reads for any necessary indirect blocks.
+ *
+ * We use the object ignore list to tell us whether or not to issue prefetches
+ * for a given object. We do this for both correctness (in case the blocksize
+ * of an object has changed) and performance (if the object doesn't exist, don't
+ * needlessly try to issue prefetches). We also trim the list as we go through
+ * the stream to prevent it from growing to an unbounded size.
+ *
+ * The object numbers within will always be in sorted order, and any write
+ * records we see will also be in sorted order, but they're not sorted with
+ * respect to each other (i.e. we can get several object records before
+ * receiving each object's write records). As a result, once we've reached a
+ * given object number, we can safely remove any reference to lower object
+ * numbers in the ignore list. In practice, we receive up to 32 object records
+ * before receiving write records, so the list can have up to 32 nodes in it.
+ */
+/* ARGSUSED */
+static void
+receive_read_prefetch(struct receive_arg *ra,
+ uint64_t object, uint64_t offset, uint64_t length)
+{
+ if (!objlist_exists(&ra->ignore_objlist, object)) {
+ dmu_prefetch(ra->os, object, 1, offset, length,
+ ZIO_PRIORITY_SYNC_READ);
+ }
+}
+
+/*
+ * Read records off the stream, issuing any necessary prefetches.
+ */
+static int
+receive_read_record(struct receive_arg *ra)
+{
+ int err;
+
+ switch (ra->rrd->header.drr_type) {
+ case DRR_OBJECT:
+ {
+ struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
+ uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
+ void *buf = NULL;
+ dmu_object_info_t doi;
+
+ if (size > 0)
+ buf = kmem_zalloc(size, KM_SLEEP);
+
+ err = receive_read_payload_and_next_header(ra, size, buf);
+ if (err != 0) {
+ kmem_free(buf, size);
+ return (err);
+ }
+ err = dmu_object_info(ra->os, drro->drr_object, &doi);
+ /*
+ * See receive_read_prefetch for an explanation why we're
+ * storing this object in the ignore_obj_list.
+ */
+ if (err == ENOENT ||
+ (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
+ objlist_insert(&ra->ignore_objlist, drro->drr_object);
+ err = 0;
+ }
+ return (err);
+ }
+ case DRR_FREEOBJECTS:
+ {
+ err = receive_read_payload_and_next_header(ra, 0, NULL);
+ return (err);
+ }
+ case DRR_WRITE:
+ {
+ struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
+ arc_buf_t *abuf;
+ boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
+ if (DRR_WRITE_COMPRESSED(drrw)) {
+ ASSERT3U(drrw->drr_compressed_size, >, 0);
+ ASSERT3U(drrw->drr_logical_size, >=,
+ drrw->drr_compressed_size);
+ ASSERT(!is_meta);
+ abuf = arc_loan_compressed_buf(
+ dmu_objset_spa(ra->os),
+ drrw->drr_compressed_size, drrw->drr_logical_size,
+ drrw->drr_compressiontype);
+ } else {
+ abuf = arc_loan_buf(dmu_objset_spa(ra->os),
+ is_meta, drrw->drr_logical_size);
+ }
+
+ err = receive_read_payload_and_next_header(ra,
+ DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
+ if (err != 0) {
+ dmu_return_arcbuf(abuf);
+ return (err);
+ }
+ ra->rrd->write_buf = abuf;
+ receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
+ drrw->drr_logical_size);
+ return (err);
+ }
+ case DRR_WRITE_BYREF:
+ {
+ struct drr_write_byref *drrwb =
+ &ra->rrd->header.drr_u.drr_write_byref;
+ err = receive_read_payload_and_next_header(ra, 0, NULL);
+ receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
+ drrwb->drr_length);
+ return (err);
+ }
+ case DRR_WRITE_EMBEDDED:
+ {
+ struct drr_write_embedded *drrwe =
+ &ra->rrd->header.drr_u.drr_write_embedded;
+ uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
+ void *buf = kmem_zalloc(size, KM_SLEEP);
+
+ err = receive_read_payload_and_next_header(ra, size, buf);
+ if (err != 0) {
+ kmem_free(buf, size);
+ return (err);
+ }
+
+ receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
+ drrwe->drr_length);
+ return (err);
+ }
+ case DRR_FREE:
+ {
+ /*
+ * It might be beneficial to prefetch indirect blocks here, but
+ * we don't really have the data to decide for sure.
+ */
+ err = receive_read_payload_and_next_header(ra, 0, NULL);
+ return (err);
+ }
+ case DRR_END:
+ {
+ struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
+ if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
+ return (SET_ERROR(ECKSUM));
+ return (0);
+ }
+ case DRR_SPILL:
+ {
+ struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
+ void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
+ err = receive_read_payload_and_next_header(ra, drrs->drr_length,
+ buf);
+ if (err != 0)
+ kmem_free(buf, drrs->drr_length);
+ return (err);
+ }
+ default:
+ return (SET_ERROR(EINVAL));
+ }
+}
+
+/*
+ * Commit the records to the pool.
+ */
+static int
+receive_process_record(struct receive_writer_arg *rwa,
+ struct receive_record_arg *rrd)
+{
+ int err;
+
+ /* Processing in order, therefore bytes_read should be increasing. */
+ ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
+ rwa->bytes_read = rrd->bytes_read;
+
+ switch (rrd->header.drr_type) {
+ case DRR_OBJECT:
+ {
+ struct drr_object *drro = &rrd->header.drr_u.drr_object;
+ err = receive_object(rwa, drro, rrd->payload);
+ kmem_free(rrd->payload, rrd->payload_size);
+ rrd->payload = NULL;
+ return (err);
+ }
+ case DRR_FREEOBJECTS:
+ {
+ struct drr_freeobjects *drrfo =
+ &rrd->header.drr_u.drr_freeobjects;
+ return (receive_freeobjects(rwa, drrfo));
+ }
+ case DRR_WRITE:
+ {
+ struct drr_write *drrw = &rrd->header.drr_u.drr_write;
+ err = receive_write(rwa, drrw, rrd->write_buf);
+ /* if receive_write() is successful, it consumes the arc_buf */
+ if (err != 0)
+ dmu_return_arcbuf(rrd->write_buf);
+ rrd->write_buf = NULL;
+ rrd->payload = NULL;
+ return (err);
+ }
+ case DRR_WRITE_BYREF:
+ {
+ struct drr_write_byref *drrwbr =
+ &rrd->header.drr_u.drr_write_byref;
+ return (receive_write_byref(rwa, drrwbr));
+ }
+ case DRR_WRITE_EMBEDDED:
+ {
+ struct drr_write_embedded *drrwe =
+ &rrd->header.drr_u.drr_write_embedded;
+ err = receive_write_embedded(rwa, drrwe, rrd->payload);
+ kmem_free(rrd->payload, rrd->payload_size);
+ rrd->payload = NULL;
+ return (err);
+ }
+ case DRR_FREE:
+ {
+ struct drr_free *drrf = &rrd->header.drr_u.drr_free;
+ return (receive_free(rwa, drrf));
+ }
+ case DRR_SPILL:
+ {
+ struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
+ err = receive_spill(rwa, drrs, rrd->payload);
+ kmem_free(rrd->payload, rrd->payload_size);
+ rrd->payload = NULL;
+ return (err);
+ }
+ default:
+ return (SET_ERROR(EINVAL));
+ }
+}
+
+/*
+ * dmu_recv_stream's worker thread; pull records off the queue, and then call
+ * receive_process_record When we're done, signal the main thread and exit.
+ */
+static void
+receive_writer_thread(void *arg)
+{
+ struct receive_writer_arg *rwa = arg;
+ struct receive_record_arg *rrd;
+ for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
+ rrd = bqueue_dequeue(&rwa->q)) {
+ /*
+ * If there's an error, the main thread will stop putting things
+ * on the queue, but we need to clear everything in it before we
+ * can exit.
+ */
+ if (rwa->err == 0) {
+ rwa->err = receive_process_record(rwa, rrd);
+ } else if (rrd->write_buf != NULL) {
+ dmu_return_arcbuf(rrd->write_buf);
+ rrd->write_buf = NULL;
+ rrd->payload = NULL;
+ } else if (rrd->payload != NULL) {
+ kmem_free(rrd->payload, rrd->payload_size);
+ rrd->payload = NULL;
+ }
+ kmem_free(rrd, sizeof (*rrd));
+ }
+ kmem_free(rrd, sizeof (*rrd));
+ mutex_enter(&rwa->mutex);
+ rwa->done = B_TRUE;
+ cv_signal(&rwa->cv);
+ mutex_exit(&rwa->mutex);
+ thread_exit();
+}
+
+static int
+resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
+{
+ uint64_t val;
+ objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
+ uint64_t dsobj = dmu_objset_id(ra->os);
+ uint64_t resume_obj, resume_off;
+
+ if (nvlist_lookup_uint64(begin_nvl,
+ "resume_object", &resume_obj) != 0 ||
+ nvlist_lookup_uint64(begin_nvl,
+ "resume_offset", &resume_off) != 0) {
+ return (SET_ERROR(EINVAL));
+ }
+ VERIFY0(zap_lookup(mos, dsobj,
+ DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
+ if (resume_obj != val)
+ return (SET_ERROR(EINVAL));
+ VERIFY0(zap_lookup(mos, dsobj,
+ DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
+ if (resume_off != val)
+ return (SET_ERROR(EINVAL));
+
+ return (0);
+}
+
+/*
+ * Read in the stream's records, one by one, and apply them to the pool. There
+ * are two threads involved; the thread that calls this function will spin up a
+ * worker thread, read the records off the stream one by one, and issue
+ * prefetches for any necessary indirect blocks. It will then push the records
+ * onto an internal blocking queue. The worker thread will pull the records off
+ * the queue, and actually write the data into the DMU. This way, the worker
+ * thread doesn't have to wait for reads to complete, since everything it needs
+ * (the indirect blocks) will be prefetched.
+ *
+ * NB: callers *must* call dmu_recv_end() if this succeeds.
+ */
+int
+dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
+ int cleanup_fd, uint64_t *action_handlep)
+{
+ int err = 0;
+ struct receive_arg ra = { 0 };
+ struct receive_writer_arg rwa = { 0 };
+ int featureflags;
+ nvlist_t *begin_nvl = NULL;
+
+ ra.byteswap = drc->drc_byteswap;
+ ra.cksum = drc->drc_cksum;
+ ra.vp = vp;
+ ra.voff = *voffp;
+
+ if (dsl_dataset_is_zapified(drc->drc_ds)) {
+ (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
+ drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
+ sizeof (ra.bytes_read), 1, &ra.bytes_read);
+ }
+
+ objlist_create(&ra.ignore_objlist);
+
+ /* these were verified in dmu_recv_begin */
+ ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
+ DMU_SUBSTREAM);
+ ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
+
+ /*
+ * Open the objset we are modifying.
+ */
+ VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
+
+ ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
+
+ featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
+
+ /* if this stream is dedup'ed, set up the avl tree for guid mapping */
+ if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
+ minor_t minor;
+
+ if (cleanup_fd == -1) {
+ ra.err = SET_ERROR(EBADF);
+ goto out;
+ }
+ ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
+ if (ra.err != 0) {
+ cleanup_fd = -1;
+ goto out;
+ }
+
+ if (*action_handlep == 0) {
+ rwa.guid_to_ds_map =
+ kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
+ avl_create(rwa.guid_to_ds_map, guid_compare,
+ sizeof (guid_map_entry_t),
+ offsetof(guid_map_entry_t, avlnode));
+ err = zfs_onexit_add_cb(minor,
+ free_guid_map_onexit, rwa.guid_to_ds_map,
+ action_handlep);
+ if (ra.err != 0)
+ goto out;
+ } else {
+ err = zfs_onexit_cb_data(minor, *action_handlep,
+ (void **)&rwa.guid_to_ds_map);
+ if (ra.err != 0)
+ goto out;
+ }
+
+ drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
+ }
+
+ uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
+ void *payload = NULL;
+ if (payloadlen != 0)
+ payload = kmem_alloc(payloadlen, KM_SLEEP);
+
+ err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
+ if (err != 0) {
+ if (payloadlen != 0)
+ kmem_free(payload, payloadlen);
+ goto out;
+ }
+ if (payloadlen != 0) {
+ err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
+ kmem_free(payload, payloadlen);
+ if (err != 0)
+ goto out;
+ }
+
+ if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
+ err = resume_check(&ra, begin_nvl);
+ if (err != 0)
+ goto out;
+ }
+
+ (void) bqueue_init(&rwa.q, zfs_recv_queue_length,
+ offsetof(struct receive_record_arg, node));
+ cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
+ mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
+ rwa.os = ra.os;
+ rwa.byteswap = drc->drc_byteswap;
+ rwa.resumable = drc->drc_resumable;
+
+ (void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, curproc,
+ TS_RUN, minclsyspri);
+ /*
+ * We're reading rwa.err without locks, which is safe since we are the
+ * only reader, and the worker thread is the only writer. It's ok if we
+ * miss a write for an iteration or two of the loop, since the writer
+ * thread will keep freeing records we send it until we send it an eos
+ * marker.
+ *
+ * We can leave this loop in 3 ways: First, if rwa.err is
+ * non-zero. In that case, the writer thread will free the rrd we just
+ * pushed. Second, if we're interrupted; in that case, either it's the
+ * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
+ * has been handed off to the writer thread who will free it. Finally,
+ * if receive_read_record fails or we're at the end of the stream, then
+ * we free ra.rrd and exit.
+ */
+ while (rwa.err == 0) {
+ if (issig(JUSTLOOKING) && issig(FORREAL)) {
+ err = SET_ERROR(EINTR);
+ break;
+ }
+
+ ASSERT3P(ra.rrd, ==, NULL);
+ ra.rrd = ra.next_rrd;
+ ra.next_rrd = NULL;
+ /* Allocates and loads header into ra.next_rrd */
+ err = receive_read_record(&ra);
+
+ if (ra.rrd->header.drr_type == DRR_END || err != 0) {
+ kmem_free(ra.rrd, sizeof (*ra.rrd));
+ ra.rrd = NULL;
+ break;
+ }
+
+ bqueue_enqueue(&rwa.q, ra.rrd,
+ sizeof (struct receive_record_arg) + ra.rrd->payload_size);
+ ra.rrd = NULL;
+ }
+ if (ra.next_rrd == NULL)
+ ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
+ ra.next_rrd->eos_marker = B_TRUE;
+ bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
+
+ mutex_enter(&rwa.mutex);
+ while (!rwa.done) {
+ cv_wait(&rwa.cv, &rwa.mutex);
+ }
+ mutex_exit(&rwa.mutex);
+
+ /*
+ * If we are receiving a full stream as a clone, all object IDs which
+ * are greater than the maximum ID referenced in the stream are
+ * by definition unused and must be freed. Note that it's possible that
+ * we've resumed this send and the first record we received was the END
+ * record. In that case, max_object would be 0, but we shouldn't start
+ * freeing all objects from there; instead we should start from the
+ * resumeobj.
+ */
+ if (drc->drc_clone && drc->drc_drrb->drr_fromguid == 0) {
+ uint64_t obj;
+ if (nvlist_lookup_uint64(begin_nvl, "resume_object", &obj) != 0)
+ obj = 0;
+ if (rwa.max_object > obj)
+ obj = rwa.max_object;
+ obj++;
+ int free_err = 0;
+ int next_err = 0;
+
+ while (next_err == 0) {
+ free_err = dmu_free_long_object(rwa.os, obj);
+ if (free_err != 0 && free_err != ENOENT)
+ break;
+
+ next_err = dmu_object_next(rwa.os, &obj, FALSE, 0);
+ }
+
+ if (err == 0) {
+ if (free_err != 0 && free_err != ENOENT)
+ err = free_err;
+ else if (next_err != ESRCH)
+ err = next_err;
+ }
+ }
+
+ cv_destroy(&rwa.cv);
+ mutex_destroy(&rwa.mutex);
+ bqueue_destroy(&rwa.q);
+ if (err == 0)
+ err = rwa.err;
+
+out:
+ nvlist_free(begin_nvl);
+ if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
+ zfs_onexit_fd_rele(cleanup_fd);
+
+ if (err != 0) {
+ /*
+ * Clean up references. If receive is not resumable,
+ * destroy what we created, so we don't leave it in
+ * the inconsistent state.
+ */
+ dmu_recv_cleanup_ds(drc);
+ }
+
+ *voffp = ra.voff;
+ objlist_destroy(&ra.ignore_objlist);
+ return (err);
+}
+
+static int
+dmu_recv_end_check(void *arg, dmu_tx_t *tx)
+{
+ dmu_recv_cookie_t *drc = arg;
+ dsl_pool_t *dp = dmu_tx_pool(tx);
+ int error;
+
+ ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
+
+ if (!drc->drc_newfs) {
+ dsl_dataset_t *origin_head;
+
+ error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
+ if (error != 0)
+ return (error);
+ if (drc->drc_force) {
+ /*
+ * We will destroy any snapshots in tofs (i.e. before
+ * origin_head) that are after the origin (which is
+ * the snap before drc_ds, because drc_ds can not
+ * have any snaps of its own).
+ */
+ uint64_t obj;
+
+ obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
+ while (obj !=
+ dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
+ dsl_dataset_t *snap;
+ error = dsl_dataset_hold_obj(dp, obj, FTAG,
+ &snap);
+ if (error != 0)
+ break;
+ if (snap->ds_dir != origin_head->ds_dir)
+ error = SET_ERROR(EINVAL);
+ if (error == 0) {
+ error = dsl_destroy_snapshot_check_impl(
+ snap, B_FALSE);
+ }
+ obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
+ dsl_dataset_rele(snap, FTAG);
+ if (error != 0)
+ break;
+ }
+ if (error != 0) {
+ dsl_dataset_rele(origin_head, FTAG);
+ return (error);
+ }
+ }
+ error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
+ origin_head, drc->drc_force, drc->drc_owner, tx);
+ if (error != 0) {
+ dsl_dataset_rele(origin_head, FTAG);
+ return (error);
+ }
+ error = dsl_dataset_snapshot_check_impl(origin_head,
+ drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
+ dsl_dataset_rele(origin_head, FTAG);
+ if (error != 0)
+ return (error);
+
+ error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
+ } else {
+ error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
+ drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
+ }
+ return (error);
+}
+
+static void
+dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
+{
+ dmu_recv_cookie_t *drc = arg;
+ dsl_pool_t *dp = dmu_tx_pool(tx);
+
+ spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
+ tx, "snap=%s", drc->drc_tosnap);
+
+ if (!drc->drc_newfs) {
+ dsl_dataset_t *origin_head;
+
+ VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
+ &origin_head));
+
+ if (drc->drc_force) {
+ /*
+ * Destroy any snapshots of drc_tofs (origin_head)
+ * after the origin (the snap before drc_ds).
+ */
+ uint64_t obj;
+
+ obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
+ while (obj !=
+ dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
+ dsl_dataset_t *snap;
+ VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
+ &snap));
+ ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
+ obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
+ dsl_destroy_snapshot_sync_impl(snap,
+ B_FALSE, tx);
+ dsl_dataset_rele(snap, FTAG);
+ }
+ }
+ VERIFY3P(drc->drc_ds->ds_prev, ==,
+ origin_head->ds_prev);
+
+ dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
+ origin_head, tx);
+ dsl_dataset_snapshot_sync_impl(origin_head,
+ drc->drc_tosnap, tx);
+
+ /* set snapshot's creation time and guid */
+ dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
+ dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
+ drc->drc_drrb->drr_creation_time;
+ dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
+ drc->drc_drrb->drr_toguid;
+ dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
+ ~DS_FLAG_INCONSISTENT;
+
+ dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
+ dsl_dataset_phys(origin_head)->ds_flags &=
+ ~DS_FLAG_INCONSISTENT;
+
+ drc->drc_newsnapobj =
+ dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
+
+ dsl_dataset_rele(origin_head, FTAG);
+ dsl_destroy_head_sync_impl(drc->drc_ds, tx);
+
+ if (drc->drc_owner != NULL)
+ VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
+ } else {
+ dsl_dataset_t *ds = drc->drc_ds;
+
+ dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
+
+ /* set snapshot's creation time and guid */
+ dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
+ dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
+ drc->drc_drrb->drr_creation_time;
+ dsl_dataset_phys(ds->ds_prev)->ds_guid =
+ drc->drc_drrb->drr_toguid;
+ dsl_dataset_phys(ds->ds_prev)->ds_flags &=
+ ~DS_FLAG_INCONSISTENT;
+
+ dmu_buf_will_dirty(ds->ds_dbuf, tx);
+ dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
+ if (dsl_dataset_has_resume_receive_state(ds)) {
+ (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
+ DS_FIELD_RESUME_FROMGUID, tx);
+ (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
+ DS_FIELD_RESUME_OBJECT, tx);
+ (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
+ DS_FIELD_RESUME_OFFSET, tx);
+ (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
+ DS_FIELD_RESUME_BYTES, tx);
+ (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
+ DS_FIELD_RESUME_TOGUID, tx);
+ (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
+ DS_FIELD_RESUME_TONAME, tx);
+ }
+ drc->drc_newsnapobj =
+ dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
+ }
+ /*
+ * Release the hold from dmu_recv_begin. This must be done before
+ * we return to open context, so that when we free the dataset's dnode,
+ * we can evict its bonus buffer.
+ */
+ dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
+ drc->drc_ds = NULL;
+}
+
+static int
+add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
+{
+ dsl_pool_t *dp;
+ dsl_dataset_t *snapds;
+ guid_map_entry_t *gmep;
+ int err;
+
+ ASSERT(guid_map != NULL);
+
+ err = dsl_pool_hold(name, FTAG, &dp);
+ if (err != 0)
+ return (err);
+ gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
+ err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
+ if (err == 0) {
+ gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
+ gmep->gme_ds = snapds;
+ avl_add(guid_map, gmep);
+ dsl_dataset_long_hold(snapds, gmep);
+ } else {
+ kmem_free(gmep, sizeof (*gmep));
+ }
+
+ dsl_pool_rele(dp, FTAG);
+ return (err);
+}
+
+static int dmu_recv_end_modified_blocks = 3;
+
+static int
+dmu_recv_existing_end(dmu_recv_cookie_t *drc)
+{
+#ifdef _KERNEL
+ /*
+ * We will be destroying the ds; make sure its origin is unmounted if
+ * necessary.
+ */
+ char name[ZFS_MAX_DATASET_NAME_LEN];
+ dsl_dataset_name(drc->drc_ds, name);
+ zfs_destroy_unmount_origin(name);
+#endif
+
+ return (dsl_sync_task(drc->drc_tofs,
+ dmu_recv_end_check, dmu_recv_end_sync, drc,
+ dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
+}
+
+static int
+dmu_recv_new_end(dmu_recv_cookie_t *drc)
+{
+ return (dsl_sync_task(drc->drc_tofs,
+ dmu_recv_end_check, dmu_recv_end_sync, drc,
+ dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
+}
+
+int
+dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
+{
+ int error;
+
+ drc->drc_owner = owner;
+
+ if (drc->drc_newfs)
+ error = dmu_recv_new_end(drc);
+ else
+ error = dmu_recv_existing_end(drc);
+
+ if (error != 0) {
+ dmu_recv_cleanup_ds(drc);
+ } else if (drc->drc_guid_to_ds_map != NULL) {
+ (void) add_ds_to_guidmap(drc->drc_tofs,
+ drc->drc_guid_to_ds_map,
+ drc->drc_newsnapobj);
+ }
+ return (error);
+}
+
+/*
+ * Return TRUE if this objset is currently being received into.
+ */
+boolean_t
+dmu_objset_is_receiving(objset_t *os)
+{
+ return (os->os_dsl_dataset != NULL &&
+ os->os_dsl_dataset->ds_owner == dmu_recv_tag);
+}
diff --git a/usr/src/uts/common/fs/zfs/dmu_send.c b/usr/src/uts/common/fs/zfs/dmu_send.c
index f4b181f62a..d42a7c66de 100644
--- a/usr/src/uts/common/fs/zfs/dmu_send.c
+++ b/usr/src/uts/common/fs/zfs/dmu_send.c
@@ -59,13 +59,9 @@
/* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
int zfs_send_corrupt_data = B_FALSE;
int zfs_send_queue_length = 16 * 1024 * 1024;
-int zfs_recv_queue_length = 16 * 1024 * 1024;
/* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
int zfs_send_set_freerecords_bit = B_TRUE;
-static char *dmu_recv_tag = "dmu_recv_tag";
-const char *recv_clone_name = "%recv";
-
/*
* Use this to override the recordsize calculation for fast zfs send estimates.
*/
@@ -75,8 +71,6 @@ uint64_t zfs_override_estimate_recordsize = 0;
(((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
(level) * (indblkshift - SPA_BLKPTRSHIFT)))
-static void byteswap_record(dmu_replay_record_t *drr);
-
struct send_thread_arg {
bqueue_t q;
dsl_dataset_t *ds; /* Dataset to traverse */
@@ -1257,2203 +1251,3 @@ dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
size.compressed, stream_compressed, sizep);
return (err);
}
-
-typedef struct dmu_recv_begin_arg {
- const char *drba_origin;
- dmu_recv_cookie_t *drba_cookie;
- cred_t *drba_cred;
- uint64_t drba_snapobj;
-} dmu_recv_begin_arg_t;
-
-static int
-recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
- uint64_t fromguid)
-{
- uint64_t val;
- int error;
- dsl_pool_t *dp = ds->ds_dir->dd_pool;
-
- /* temporary clone name must not exist */
- error = zap_lookup(dp->dp_meta_objset,
- dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
- 8, 1, &val);
- if (error != ENOENT)
- return (error == 0 ? EBUSY : error);
-
- /* new snapshot name must not exist */
- error = zap_lookup(dp->dp_meta_objset,
- dsl_dataset_phys(ds)->ds_snapnames_zapobj,
- drba->drba_cookie->drc_tosnap, 8, 1, &val);
- if (error != ENOENT)
- return (error == 0 ? EEXIST : error);
-
- /*
- * Check snapshot limit before receiving. We'll recheck again at the
- * end, but might as well abort before receiving if we're already over
- * the limit.
- *
- * Note that we do not check the file system limit with
- * dsl_dir_fscount_check because the temporary %clones don't count
- * against that limit.
- */
- error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
- NULL, drba->drba_cred);
- if (error != 0)
- return (error);
-
- if (fromguid != 0) {
- dsl_dataset_t *snap;
- uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
-
- /* Find snapshot in this dir that matches fromguid. */
- while (obj != 0) {
- error = dsl_dataset_hold_obj(dp, obj, FTAG,
- &snap);
- if (error != 0)
- return (SET_ERROR(ENODEV));
- if (snap->ds_dir != ds->ds_dir) {
- dsl_dataset_rele(snap, FTAG);
- return (SET_ERROR(ENODEV));
- }
- if (dsl_dataset_phys(snap)->ds_guid == fromguid)
- break;
- obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
- dsl_dataset_rele(snap, FTAG);
- }
- if (obj == 0)
- return (SET_ERROR(ENODEV));
-
- if (drba->drba_cookie->drc_force) {
- drba->drba_snapobj = obj;
- } else {
- /*
- * If we are not forcing, there must be no
- * changes since fromsnap.
- */
- if (dsl_dataset_modified_since_snap(ds, snap)) {
- dsl_dataset_rele(snap, FTAG);
- return (SET_ERROR(ETXTBSY));
- }
- drba->drba_snapobj = ds->ds_prev->ds_object;
- }
-
- dsl_dataset_rele(snap, FTAG);
- } else {
- /* if full, then must be forced */
- if (!drba->drba_cookie->drc_force)
- return (SET_ERROR(EEXIST));
- /* start from $ORIGIN@$ORIGIN, if supported */
- drba->drba_snapobj = dp->dp_origin_snap != NULL ?
- dp->dp_origin_snap->ds_object : 0;
- }
-
- return (0);
-
-}
-
-static int
-dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
-{
- dmu_recv_begin_arg_t *drba = arg;
- dsl_pool_t *dp = dmu_tx_pool(tx);
- struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
- uint64_t fromguid = drrb->drr_fromguid;
- int flags = drrb->drr_flags;
- int error;
- uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
- dsl_dataset_t *ds;
- const char *tofs = drba->drba_cookie->drc_tofs;
-
- /* already checked */
- ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
- ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
-
- if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
- DMU_COMPOUNDSTREAM ||
- drrb->drr_type >= DMU_OST_NUMTYPES ||
- ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
- return (SET_ERROR(EINVAL));
-
- /* Verify pool version supports SA if SA_SPILL feature set */
- if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
- spa_version(dp->dp_spa) < SPA_VERSION_SA)
- return (SET_ERROR(ENOTSUP));
-
- if (drba->drba_cookie->drc_resumable &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
- return (SET_ERROR(ENOTSUP));
-
- /*
- * The receiving code doesn't know how to translate a WRITE_EMBEDDED
- * record to a plain WRITE record, so the pool must have the
- * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
- * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
- */
- if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
- return (SET_ERROR(ENOTSUP));
- if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
- return (SET_ERROR(ENOTSUP));
-
- /*
- * The receiving code doesn't know how to translate large blocks
- * to smaller ones, so the pool must have the LARGE_BLOCKS
- * feature enabled if the stream has LARGE_BLOCKS. Same with
- * large dnodes.
- */
- if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
- return (SET_ERROR(ENOTSUP));
- if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
- return (SET_ERROR(ENOTSUP));
-
- error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
- if (error == 0) {
- /* target fs already exists; recv into temp clone */
-
- /* Can't recv a clone into an existing fs */
- if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(EINVAL));
- }
-
- error = recv_begin_check_existing_impl(drba, ds, fromguid);
- dsl_dataset_rele(ds, FTAG);
- } else if (error == ENOENT) {
- /* target fs does not exist; must be a full backup or clone */
- char buf[ZFS_MAX_DATASET_NAME_LEN];
-
- /*
- * If it's a non-clone incremental, we are missing the
- * target fs, so fail the recv.
- */
- if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
- drba->drba_origin))
- return (SET_ERROR(ENOENT));
-
- /*
- * If we're receiving a full send as a clone, and it doesn't
- * contain all the necessary free records and freeobject
- * records, reject it.
- */
- if (fromguid == 0 && drba->drba_origin &&
- !(flags & DRR_FLAG_FREERECORDS))
- return (SET_ERROR(EINVAL));
-
- /* Open the parent of tofs */
- ASSERT3U(strlen(tofs), <, sizeof (buf));
- (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
- error = dsl_dataset_hold(dp, buf, FTAG, &ds);
- if (error != 0)
- return (error);
-
- /*
- * Check filesystem and snapshot limits before receiving. We'll
- * recheck snapshot limits again at the end (we create the
- * filesystems and increment those counts during begin_sync).
- */
- error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
- ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
- if (error != 0) {
- dsl_dataset_rele(ds, FTAG);
- return (error);
- }
-
- error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
- ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
- if (error != 0) {
- dsl_dataset_rele(ds, FTAG);
- return (error);
- }
-
- if (drba->drba_origin != NULL) {
- dsl_dataset_t *origin;
- error = dsl_dataset_hold(dp, drba->drba_origin,
- FTAG, &origin);
- if (error != 0) {
- dsl_dataset_rele(ds, FTAG);
- return (error);
- }
- if (!origin->ds_is_snapshot) {
- dsl_dataset_rele(origin, FTAG);
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(EINVAL));
- }
- if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
- fromguid != 0) {
- dsl_dataset_rele(origin, FTAG);
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(ENODEV));
- }
- dsl_dataset_rele(origin, FTAG);
- }
- dsl_dataset_rele(ds, FTAG);
- error = 0;
- }
- return (error);
-}
-
-static void
-dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
-{
- dmu_recv_begin_arg_t *drba = arg;
- dsl_pool_t *dp = dmu_tx_pool(tx);
- objset_t *mos = dp->dp_meta_objset;
- struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
- const char *tofs = drba->drba_cookie->drc_tofs;
- dsl_dataset_t *ds, *newds;
- uint64_t dsobj;
- int error;
- uint64_t crflags = 0;
-
- if (drrb->drr_flags & DRR_FLAG_CI_DATA)
- crflags |= DS_FLAG_CI_DATASET;
-
- error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
- if (error == 0) {
- /* create temporary clone */
- dsl_dataset_t *snap = NULL;
- if (drba->drba_snapobj != 0) {
- VERIFY0(dsl_dataset_hold_obj(dp,
- drba->drba_snapobj, FTAG, &snap));
- }
- dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
- snap, crflags, drba->drba_cred, tx);
- if (drba->drba_snapobj != 0)
- dsl_dataset_rele(snap, FTAG);
- dsl_dataset_rele(ds, FTAG);
- } else {
- dsl_dir_t *dd;
- const char *tail;
- dsl_dataset_t *origin = NULL;
-
- VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
-
- if (drba->drba_origin != NULL) {
- VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
- FTAG, &origin));
- }
-
- /* Create new dataset. */
- dsobj = dsl_dataset_create_sync(dd,
- strrchr(tofs, '/') + 1,
- origin, crflags, drba->drba_cred, tx);
- if (origin != NULL)
- dsl_dataset_rele(origin, FTAG);
- dsl_dir_rele(dd, FTAG);
- drba->drba_cookie->drc_newfs = B_TRUE;
- }
- VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
-
- if (drba->drba_cookie->drc_resumable) {
- dsl_dataset_zapify(newds, tx);
- if (drrb->drr_fromguid != 0) {
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
- 8, 1, &drrb->drr_fromguid, tx));
- }
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
- 8, 1, &drrb->drr_toguid, tx));
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
- 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
- uint64_t one = 1;
- uint64_t zero = 0;
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
- 8, 1, &one, tx));
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
- 8, 1, &zero, tx));
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
- 8, 1, &zero, tx));
- if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
- DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
- 8, 1, &one, tx));
- }
- if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
- DMU_BACKUP_FEATURE_EMBED_DATA) {
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
- 8, 1, &one, tx));
- }
- if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
- DMU_BACKUP_FEATURE_COMPRESSED) {
- VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
- 8, 1, &one, tx));
- }
- }
-
- dmu_buf_will_dirty(newds->ds_dbuf, tx);
- dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
-
- /*
- * If we actually created a non-clone, we need to create the
- * objset in our new dataset.
- */
- rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
- if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
- (void) dmu_objset_create_impl(dp->dp_spa,
- newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
- }
- rrw_exit(&newds->ds_bp_rwlock, FTAG);
-
- drba->drba_cookie->drc_ds = newds;
-
- spa_history_log_internal_ds(newds, "receive", tx, "");
-}
-
-static int
-dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
-{
- dmu_recv_begin_arg_t *drba = arg;
- dsl_pool_t *dp = dmu_tx_pool(tx);
- struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
- int error;
- uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
- dsl_dataset_t *ds;
- const char *tofs = drba->drba_cookie->drc_tofs;
-
- /* 6 extra bytes for /%recv */
- char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
-
- /* already checked */
- ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
- ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
-
- if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
- DMU_COMPOUNDSTREAM ||
- drrb->drr_type >= DMU_OST_NUMTYPES)
- return (SET_ERROR(EINVAL));
-
- /* Verify pool version supports SA if SA_SPILL feature set */
- if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
- spa_version(dp->dp_spa) < SPA_VERSION_SA)
- return (SET_ERROR(ENOTSUP));
-
- /*
- * The receiving code doesn't know how to translate a WRITE_EMBEDDED
- * record to a plain WRITE record, so the pool must have the
- * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
- * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
- */
- if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
- return (SET_ERROR(ENOTSUP));
- if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
- return (SET_ERROR(ENOTSUP));
-
- /*
- * The receiving code doesn't know how to translate large blocks
- * to smaller ones, so the pool must have the LARGE_BLOCKS
- * feature enabled if the stream has LARGE_BLOCKS. Same with
- * large dnodes.
- */
- if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
- return (SET_ERROR(ENOTSUP));
- if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
- !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
- return (SET_ERROR(ENOTSUP));
-
- (void) snprintf(recvname, sizeof (recvname), "%s/%s",
- tofs, recv_clone_name);
-
- if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
- /* %recv does not exist; continue in tofs */
- error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
- if (error != 0)
- return (error);
- }
-
- /* check that ds is marked inconsistent */
- if (!DS_IS_INCONSISTENT(ds)) {
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(EINVAL));
- }
-
- /* check that there is resuming data, and that the toguid matches */
- if (!dsl_dataset_is_zapified(ds)) {
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(EINVAL));
- }
- uint64_t val;
- error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
- DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
- if (error != 0 || drrb->drr_toguid != val) {
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(EINVAL));
- }
-
- /*
- * Check if the receive is still running. If so, it will be owned.
- * Note that nothing else can own the dataset (e.g. after the receive
- * fails) because it will be marked inconsistent.
- */
- if (dsl_dataset_has_owner(ds)) {
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(EBUSY));
- }
-
- /* There should not be any snapshots of this fs yet. */
- if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(EINVAL));
- }
-
- /*
- * Note: resume point will be checked when we process the first WRITE
- * record.
- */
-
- /* check that the origin matches */
- val = 0;
- (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
- DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
- if (drrb->drr_fromguid != val) {
- dsl_dataset_rele(ds, FTAG);
- return (SET_ERROR(EINVAL));
- }
-
- dsl_dataset_rele(ds, FTAG);
- return (0);
-}
-
-static void
-dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
-{
- dmu_recv_begin_arg_t *drba = arg;
- dsl_pool_t *dp = dmu_tx_pool(tx);
- const char *tofs = drba->drba_cookie->drc_tofs;
- dsl_dataset_t *ds;
- uint64_t dsobj;
- /* 6 extra bytes for /%recv */
- char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
-
- (void) snprintf(recvname, sizeof (recvname), "%s/%s",
- tofs, recv_clone_name);
-
- if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
- /* %recv does not exist; continue in tofs */
- VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
- drba->drba_cookie->drc_newfs = B_TRUE;
- }
-
- /* clear the inconsistent flag so that we can own it */
- ASSERT(DS_IS_INCONSISTENT(ds));
- dmu_buf_will_dirty(ds->ds_dbuf, tx);
- dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
- dsobj = ds->ds_object;
- dsl_dataset_rele(ds, FTAG);
-
- VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
-
- dmu_buf_will_dirty(ds->ds_dbuf, tx);
- dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
-
- rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
- ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
- rrw_exit(&ds->ds_bp_rwlock, FTAG);
-
- drba->drba_cookie->drc_ds = ds;
-
- spa_history_log_internal_ds(ds, "resume receive", tx, "");
-}
-
-/*
- * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
- * succeeds; otherwise we will leak the holds on the datasets.
- */
-int
-dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
- boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
-{
- dmu_recv_begin_arg_t drba = { 0 };
-
- bzero(drc, sizeof (dmu_recv_cookie_t));
- drc->drc_drr_begin = drr_begin;
- drc->drc_drrb = &drr_begin->drr_u.drr_begin;
- drc->drc_tosnap = tosnap;
- drc->drc_tofs = tofs;
- drc->drc_force = force;
- drc->drc_resumable = resumable;
- drc->drc_cred = CRED();
- drc->drc_clone = (origin != NULL);
-
- if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
- drc->drc_byteswap = B_TRUE;
- (void) fletcher_4_incremental_byteswap(drr_begin,
- sizeof (dmu_replay_record_t), &drc->drc_cksum);
- byteswap_record(drr_begin);
- } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
- (void) fletcher_4_incremental_native(drr_begin,
- sizeof (dmu_replay_record_t), &drc->drc_cksum);
- } else {
- return (SET_ERROR(EINVAL));
- }
-
- drba.drba_origin = origin;
- drba.drba_cookie = drc;
- drba.drba_cred = CRED();
-
- if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
- DMU_BACKUP_FEATURE_RESUMING) {
- return (dsl_sync_task(tofs,
- dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
- &drba, 5, ZFS_SPACE_CHECK_NORMAL));
- } else {
- return (dsl_sync_task(tofs,
- dmu_recv_begin_check, dmu_recv_begin_sync,
- &drba, 5, ZFS_SPACE_CHECK_NORMAL));
- }
-}
-
-struct receive_record_arg {
- dmu_replay_record_t header;
- void *payload; /* Pointer to a buffer containing the payload */
- /*
- * If the record is a write, pointer to the arc_buf_t containing the
- * payload.
- */
- arc_buf_t *write_buf;
- int payload_size;
- uint64_t bytes_read; /* bytes read from stream when record created */
- boolean_t eos_marker; /* Marks the end of the stream */
- bqueue_node_t node;
-};
-
-struct receive_writer_arg {
- objset_t *os;
- boolean_t byteswap;
- bqueue_t q;
-
- /*
- * These three args are used to signal to the main thread that we're
- * done.
- */
- kmutex_t mutex;
- kcondvar_t cv;
- boolean_t done;
-
- int err;
- /* A map from guid to dataset to help handle dedup'd streams. */
- avl_tree_t *guid_to_ds_map;
- boolean_t resumable;
- uint64_t last_object;
- uint64_t last_offset;
- uint64_t max_object; /* highest object ID referenced in stream */
- uint64_t bytes_read; /* bytes read when current record created */
-};
-
-struct objlist {
- list_t list; /* List of struct receive_objnode. */
- /*
- * Last object looked up. Used to assert that objects are being looked
- * up in ascending order.
- */
- uint64_t last_lookup;
-};
-
-struct receive_objnode {
- list_node_t node;
- uint64_t object;
-};
-
-struct receive_arg {
- objset_t *os;
- vnode_t *vp; /* The vnode to read the stream from */
- uint64_t voff; /* The current offset in the stream */
- uint64_t bytes_read;
- /*
- * A record that has had its payload read in, but hasn't yet been handed
- * off to the worker thread.
- */
- struct receive_record_arg *rrd;
- /* A record that has had its header read in, but not its payload. */
- struct receive_record_arg *next_rrd;
- zio_cksum_t cksum;
- zio_cksum_t prev_cksum;
- int err;
- boolean_t byteswap;
- /* Sorted list of objects not to issue prefetches for. */
- struct objlist ignore_objlist;
-};
-
-typedef struct guid_map_entry {
- uint64_t guid;
- dsl_dataset_t *gme_ds;
- avl_node_t avlnode;
-} guid_map_entry_t;
-
-static int
-guid_compare(const void *arg1, const void *arg2)
-{
- const guid_map_entry_t *gmep1 = arg1;
- const guid_map_entry_t *gmep2 = arg2;
-
- if (gmep1->guid < gmep2->guid)
- return (-1);
- else if (gmep1->guid > gmep2->guid)
- return (1);
- return (0);
-}
-
-static void
-free_guid_map_onexit(void *arg)
-{
- avl_tree_t *ca = arg;
- void *cookie = NULL;
- guid_map_entry_t *gmep;
-
- while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
- dsl_dataset_long_rele(gmep->gme_ds, gmep);
- dsl_dataset_rele(gmep->gme_ds, gmep);
- kmem_free(gmep, sizeof (guid_map_entry_t));
- }
- avl_destroy(ca);
- kmem_free(ca, sizeof (avl_tree_t));
-}
-
-static int
-receive_read(struct receive_arg *ra, int len, void *buf)
-{
- int done = 0;
-
- /*
- * The code doesn't rely on this (lengths being multiples of 8). See
- * comment in dump_bytes.
- */
- ASSERT0(len % 8);
-
- while (done < len) {
- ssize_t resid;
-
- ra->err = vn_rdwr(UIO_READ, ra->vp,
- (char *)buf + done, len - done,
- ra->voff, UIO_SYSSPACE, FAPPEND,
- RLIM64_INFINITY, CRED(), &resid);
-
- if (resid == len - done) {
- /*
- * Note: ECKSUM indicates that the receive
- * was interrupted and can potentially be resumed.
- */
- ra->err = SET_ERROR(ECKSUM);
- }
- ra->voff += len - done - resid;
- done = len - resid;
- if (ra->err != 0)
- return (ra->err);
- }
-
- ra->bytes_read += len;
-
- ASSERT3U(done, ==, len);
- return (0);
-}
-
-static void
-byteswap_record(dmu_replay_record_t *drr)
-{
-#define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
-#define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
- drr->drr_type = BSWAP_32(drr->drr_type);
- drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
-
- switch (drr->drr_type) {
- case DRR_BEGIN:
- DO64(drr_begin.drr_magic);
- DO64(drr_begin.drr_versioninfo);
- DO64(drr_begin.drr_creation_time);
- DO32(drr_begin.drr_type);
- DO32(drr_begin.drr_flags);
- DO64(drr_begin.drr_toguid);
- DO64(drr_begin.drr_fromguid);
- break;
- case DRR_OBJECT:
- DO64(drr_object.drr_object);
- DO32(drr_object.drr_type);
- DO32(drr_object.drr_bonustype);
- DO32(drr_object.drr_blksz);
- DO32(drr_object.drr_bonuslen);
- DO64(drr_object.drr_toguid);
- break;
- case DRR_FREEOBJECTS:
- DO64(drr_freeobjects.drr_firstobj);
- DO64(drr_freeobjects.drr_numobjs);
- DO64(drr_freeobjects.drr_toguid);
- break;
- case DRR_WRITE:
- DO64(drr_write.drr_object);
- DO32(drr_write.drr_type);
- DO64(drr_write.drr_offset);
- DO64(drr_write.drr_logical_size);
- DO64(drr_write.drr_toguid);
- ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
- DO64(drr_write.drr_key.ddk_prop);
- DO64(drr_write.drr_compressed_size);
- break;
- case DRR_WRITE_BYREF:
- DO64(drr_write_byref.drr_object);
- DO64(drr_write_byref.drr_offset);
- DO64(drr_write_byref.drr_length);
- DO64(drr_write_byref.drr_toguid);
- DO64(drr_write_byref.drr_refguid);
- DO64(drr_write_byref.drr_refobject);
- DO64(drr_write_byref.drr_refoffset);
- ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
- drr_key.ddk_cksum);
- DO64(drr_write_byref.drr_key.ddk_prop);
- break;
- case DRR_WRITE_EMBEDDED:
- DO64(drr_write_embedded.drr_object);
- DO64(drr_write_embedded.drr_offset);
- DO64(drr_write_embedded.drr_length);
- DO64(drr_write_embedded.drr_toguid);
- DO32(drr_write_embedded.drr_lsize);
- DO32(drr_write_embedded.drr_psize);
- break;
- case DRR_FREE:
- DO64(drr_free.drr_object);
- DO64(drr_free.drr_offset);
- DO64(drr_free.drr_length);
- DO64(drr_free.drr_toguid);
- break;
- case DRR_SPILL:
- DO64(drr_spill.drr_object);
- DO64(drr_spill.drr_length);
- DO64(drr_spill.drr_toguid);
- break;
- case DRR_END:
- DO64(drr_end.drr_toguid);
- ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
- break;
- }
-
- if (drr->drr_type != DRR_BEGIN) {
- ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
- }
-
-#undef DO64
-#undef DO32
-}
-
-static inline uint8_t
-deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
-{
- if (bonus_type == DMU_OT_SA) {
- return (1);
- } else {
- return (1 +
- ((DN_OLD_MAX_BONUSLEN -
- MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT));
- }
-}
-
-static void
-save_resume_state(struct receive_writer_arg *rwa,
- uint64_t object, uint64_t offset, dmu_tx_t *tx)
-{
- int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
-
- if (!rwa->resumable)
- return;
-
- /*
- * We use ds_resume_bytes[] != 0 to indicate that we need to
- * update this on disk, so it must not be 0.
- */
- ASSERT(rwa->bytes_read != 0);
-
- /*
- * We only resume from write records, which have a valid
- * (non-meta-dnode) object number.
- */
- ASSERT(object != 0);
-
- /*
- * For resuming to work correctly, we must receive records in order,
- * sorted by object,offset. This is checked by the callers, but
- * assert it here for good measure.
- */
- ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
- ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
- offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
- ASSERT3U(rwa->bytes_read, >=,
- rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
-
- rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
- rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
- rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
-}
-
-static int
-receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
- void *data)
-{
- dmu_object_info_t doi;
- dmu_tx_t *tx;
- uint64_t object;
- int err;
- uint8_t dn_slots = drro->drr_dn_slots != 0 ?
- drro->drr_dn_slots : DNODE_MIN_SLOTS;
-
- if (drro->drr_type == DMU_OT_NONE ||
- !DMU_OT_IS_VALID(drro->drr_type) ||
- !DMU_OT_IS_VALID(drro->drr_bonustype) ||
- drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
- drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
- P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
- drro->drr_blksz < SPA_MINBLOCKSIZE ||
- drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
- drro->drr_bonuslen >
- DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa->os))) ||
- dn_slots >
- (spa_maxdnodesize(dmu_objset_spa(rwa->os)) >> DNODE_SHIFT)) {
- return (SET_ERROR(EINVAL));
- }
-
- err = dmu_object_info(rwa->os, drro->drr_object, &doi);
-
- if (err != 0 && err != ENOENT && err != EEXIST)
- return (SET_ERROR(EINVAL));
-
- if (drro->drr_object > rwa->max_object)
- rwa->max_object = drro->drr_object;
-
- /*
- * If we are losing blkptrs or changing the block size this must
- * be a new file instance. We must clear out the previous file
- * contents before we can change this type of metadata in the dnode.
- */
- if (err == 0) {
- int nblkptr;
-
- object = drro->drr_object;
-
- nblkptr = deduce_nblkptr(drro->drr_bonustype,
- drro->drr_bonuslen);
-
- if (drro->drr_blksz != doi.doi_data_block_size ||
- nblkptr < doi.doi_nblkptr ||
- dn_slots != doi.doi_dnodesize >> DNODE_SHIFT) {
- err = dmu_free_long_range(rwa->os, drro->drr_object,
- 0, DMU_OBJECT_END);
- if (err != 0)
- return (SET_ERROR(EINVAL));
- }
- } else if (err == EEXIST) {
- /*
- * The object requested is currently an interior slot of a
- * multi-slot dnode. This will be resolved when the next txg
- * is synced out, since the send stream will have told us
- * to free this slot when we freed the associated dnode
- * earlier in the stream.
- */
- txg_wait_synced(dmu_objset_pool(rwa->os), 0);
- object = drro->drr_object;
- } else {
- /* object is free and we are about to allocate a new one */
- object = DMU_NEW_OBJECT;
- }
-
- /*
- * If this is a multi-slot dnode there is a chance that this
- * object will expand into a slot that is already used by
- * another object from the previous snapshot. We must free
- * these objects before we attempt to allocate the new dnode.
- */
- if (dn_slots > 1) {
- boolean_t need_sync = B_FALSE;
-
- for (uint64_t slot = drro->drr_object + 1;
- slot < drro->drr_object + dn_slots;
- slot++) {
- dmu_object_info_t slot_doi;
-
- err = dmu_object_info(rwa->os, slot, &slot_doi);
- if (err == ENOENT || err == EEXIST)
- continue;
- else if (err != 0)
- return (err);
-
- err = dmu_free_long_object(rwa->os, slot);
-
- if (err != 0)
- return (err);
-
- need_sync = B_TRUE;
- }
-
- if (need_sync)
- txg_wait_synced(dmu_objset_pool(rwa->os), 0);
- }
-
- tx = dmu_tx_create(rwa->os);
- dmu_tx_hold_bonus(tx, object);
- err = dmu_tx_assign(tx, TXG_WAIT);
- if (err != 0) {
- dmu_tx_abort(tx);
- return (err);
- }
-
- if (object == DMU_NEW_OBJECT) {
- /* currently free, want to be allocated */
- err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
- drro->drr_type, drro->drr_blksz,
- drro->drr_bonustype, drro->drr_bonuslen,
- dn_slots << DNODE_SHIFT, tx);
- } else if (drro->drr_type != doi.doi_type ||
- drro->drr_blksz != doi.doi_data_block_size ||
- drro->drr_bonustype != doi.doi_bonus_type ||
- drro->drr_bonuslen != doi.doi_bonus_size ||
- drro->drr_dn_slots != (doi.doi_dnodesize >> DNODE_SHIFT)) {
- /* currently allocated, but with different properties */
- err = dmu_object_reclaim_dnsize(rwa->os, drro->drr_object,
- drro->drr_type, drro->drr_blksz,
- drro->drr_bonustype, drro->drr_bonuslen,
- drro->drr_dn_slots << DNODE_SHIFT, tx);
- }
- if (err != 0) {
- dmu_tx_commit(tx);
- return (SET_ERROR(EINVAL));
- }
-
- dmu_object_set_checksum(rwa->os, drro->drr_object,
- drro->drr_checksumtype, tx);
- dmu_object_set_compress(rwa->os, drro->drr_object,
- drro->drr_compress, tx);
-
- if (data != NULL) {
- dmu_buf_t *db;
-
- VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
- dmu_buf_will_dirty(db, tx);
-
- ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
- bcopy(data, db->db_data, drro->drr_bonuslen);
- if (rwa->byteswap) {
- dmu_object_byteswap_t byteswap =
- DMU_OT_BYTESWAP(drro->drr_bonustype);
- dmu_ot_byteswap[byteswap].ob_func(db->db_data,
- drro->drr_bonuslen);
- }
- dmu_buf_rele(db, FTAG);
- }
- dmu_tx_commit(tx);
-
- return (0);
-}
-
-/* ARGSUSED */
-static int
-receive_freeobjects(struct receive_writer_arg *rwa,
- struct drr_freeobjects *drrfo)
-{
- uint64_t obj;
- int next_err = 0;
-
- if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
- return (SET_ERROR(EINVAL));
-
- for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
- obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
- next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
- int err;
-
- err = dmu_object_info(rwa->os, obj, NULL);
- if (err == ENOENT)
- continue;
- else if (err != 0)
- return (err);
-
- err = dmu_free_long_object(rwa->os, obj);
- if (err != 0)
- return (err);
-
- if (obj > rwa->max_object)
- rwa->max_object = obj;
- }
- if (next_err != ESRCH)
- return (next_err);
- return (0);
-}
-
-static int
-receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
- arc_buf_t *abuf)
-{
- dmu_tx_t *tx;
- int err;
-
- if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
- !DMU_OT_IS_VALID(drrw->drr_type))
- return (SET_ERROR(EINVAL));
-
- /*
- * For resuming to work, records must be in increasing order
- * by (object, offset).
- */
- if (drrw->drr_object < rwa->last_object ||
- (drrw->drr_object == rwa->last_object &&
- drrw->drr_offset < rwa->last_offset)) {
- return (SET_ERROR(EINVAL));
- }
- rwa->last_object = drrw->drr_object;
- rwa->last_offset = drrw->drr_offset;
-
- if (rwa->last_object > rwa->max_object)
- rwa->max_object = rwa->last_object;
-
- if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
- return (SET_ERROR(EINVAL));
-
- tx = dmu_tx_create(rwa->os);
-
- dmu_tx_hold_write(tx, drrw->drr_object,
- drrw->drr_offset, drrw->drr_logical_size);
- err = dmu_tx_assign(tx, TXG_WAIT);
- if (err != 0) {
- dmu_tx_abort(tx);
- return (err);
- }
- if (rwa->byteswap) {
- dmu_object_byteswap_t byteswap =
- DMU_OT_BYTESWAP(drrw->drr_type);
- dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
- DRR_WRITE_PAYLOAD_SIZE(drrw));
- }
-
- /* use the bonus buf to look up the dnode in dmu_assign_arcbuf */
- dmu_buf_t *bonus;
- if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
- return (SET_ERROR(EINVAL));
- dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
-
- /*
- * Note: If the receive fails, we want the resume stream to start
- * with the same record that we last successfully received (as opposed
- * to the next record), so that we can verify that we are
- * resuming from the correct location.
- */
- save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
- dmu_tx_commit(tx);
- dmu_buf_rele(bonus, FTAG);
-
- return (0);
-}
-
-/*
- * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
- * streams to refer to a copy of the data that is already on the
- * system because it came in earlier in the stream. This function
- * finds the earlier copy of the data, and uses that copy instead of
- * data from the stream to fulfill this write.
- */
-static int
-receive_write_byref(struct receive_writer_arg *rwa,
- struct drr_write_byref *drrwbr)
-{
- dmu_tx_t *tx;
- int err;
- guid_map_entry_t gmesrch;
- guid_map_entry_t *gmep;
- avl_index_t where;
- objset_t *ref_os = NULL;
- dmu_buf_t *dbp;
-
- if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
- return (SET_ERROR(EINVAL));
-
- /*
- * If the GUID of the referenced dataset is different from the
- * GUID of the target dataset, find the referenced dataset.
- */
- if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
- gmesrch.guid = drrwbr->drr_refguid;
- if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
- &where)) == NULL) {
- return (SET_ERROR(EINVAL));
- }
- if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
- return (SET_ERROR(EINVAL));
- } else {
- ref_os = rwa->os;
- }
-
- if (drrwbr->drr_object > rwa->max_object)
- rwa->max_object = drrwbr->drr_object;
-
- err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
- drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
- if (err != 0)
- return (err);
-
- tx = dmu_tx_create(rwa->os);
-
- dmu_tx_hold_write(tx, drrwbr->drr_object,
- drrwbr->drr_offset, drrwbr->drr_length);
- err = dmu_tx_assign(tx, TXG_WAIT);
- if (err != 0) {
- dmu_tx_abort(tx);
- return (err);
- }
- dmu_write(rwa->os, drrwbr->drr_object,
- drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
- dmu_buf_rele(dbp, FTAG);
-
- /* See comment in restore_write. */
- save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
- dmu_tx_commit(tx);
- return (0);
-}
-
-static int
-receive_write_embedded(struct receive_writer_arg *rwa,
- struct drr_write_embedded *drrwe, void *data)
-{
- dmu_tx_t *tx;
- int err;
-
- if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
- return (EINVAL);
-
- if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
- return (EINVAL);
-
- if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
- return (EINVAL);
- if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
- return (EINVAL);
-
- if (drrwe->drr_object > rwa->max_object)
- rwa->max_object = drrwe->drr_object;
-
- tx = dmu_tx_create(rwa->os);
-
- dmu_tx_hold_write(tx, drrwe->drr_object,
- drrwe->drr_offset, drrwe->drr_length);
- err = dmu_tx_assign(tx, TXG_WAIT);
- if (err != 0) {
- dmu_tx_abort(tx);
- return (err);
- }
-
- dmu_write_embedded(rwa->os, drrwe->drr_object,
- drrwe->drr_offset, data, drrwe->drr_etype,
- drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
- rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
-
- /* See comment in restore_write. */
- save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
- dmu_tx_commit(tx);
- return (0);
-}
-
-static int
-receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
- void *data)
-{
- dmu_tx_t *tx;
- dmu_buf_t *db, *db_spill;
- int err;
-
- if (drrs->drr_length < SPA_MINBLOCKSIZE ||
- drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
- return (SET_ERROR(EINVAL));
-
- if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
- return (SET_ERROR(EINVAL));
-
- if (drrs->drr_object > rwa->max_object)
- rwa->max_object = drrs->drr_object;
-
- VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
- if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
- dmu_buf_rele(db, FTAG);
- return (err);
- }
-
- tx = dmu_tx_create(rwa->os);
-
- dmu_tx_hold_spill(tx, db->db_object);
-
- err = dmu_tx_assign(tx, TXG_WAIT);
- if (err != 0) {
- dmu_buf_rele(db, FTAG);
- dmu_buf_rele(db_spill, FTAG);
- dmu_tx_abort(tx);
- return (err);
- }
- dmu_buf_will_dirty(db_spill, tx);
-
- if (db_spill->db_size < drrs->drr_length)
- VERIFY(0 == dbuf_spill_set_blksz(db_spill,
- drrs->drr_length, tx));
- bcopy(data, db_spill->db_data, drrs->drr_length);
-
- dmu_buf_rele(db, FTAG);
- dmu_buf_rele(db_spill, FTAG);
-
- dmu_tx_commit(tx);
- return (0);
-}
-
-/* ARGSUSED */
-static int
-receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
-{
- int err;
-
- if (drrf->drr_length != -1ULL &&
- drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
- return (SET_ERROR(EINVAL));
-
- if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
- return (SET_ERROR(EINVAL));
-
- if (drrf->drr_object > rwa->max_object)
- rwa->max_object = drrf->drr_object;
-
- err = dmu_free_long_range(rwa->os, drrf->drr_object,
- drrf->drr_offset, drrf->drr_length);
-
- return (err);
-}
-
-/* used to destroy the drc_ds on error */
-static void
-dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
-{
- if (drc->drc_resumable) {
- /* wait for our resume state to be written to disk */
- txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
- dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
- } else {
- char name[ZFS_MAX_DATASET_NAME_LEN];
- dsl_dataset_name(drc->drc_ds, name);
- dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
- (void) dsl_destroy_head(name);
- }
-}
-
-static void
-receive_cksum(struct receive_arg *ra, int len, void *buf)
-{
- if (ra->byteswap) {
- (void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
- } else {
- (void) fletcher_4_incremental_native(buf, len, &ra->cksum);
- }
-}
-
-/*
- * Read the payload into a buffer of size len, and update the current record's
- * payload field.
- * Allocate ra->next_rrd and read the next record's header into
- * ra->next_rrd->header.
- * Verify checksum of payload and next record.
- */
-static int
-receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
-{
- int err;
-
- if (len != 0) {
- ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
- err = receive_read(ra, len, buf);
- if (err != 0)
- return (err);
- receive_cksum(ra, len, buf);
-
- /* note: rrd is NULL when reading the begin record's payload */
- if (ra->rrd != NULL) {
- ra->rrd->payload = buf;
- ra->rrd->payload_size = len;
- ra->rrd->bytes_read = ra->bytes_read;
- }
- }
-
- ra->prev_cksum = ra->cksum;
-
- ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
- err = receive_read(ra, sizeof (ra->next_rrd->header),
- &ra->next_rrd->header);
- ra->next_rrd->bytes_read = ra->bytes_read;
- if (err != 0) {
- kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
- ra->next_rrd = NULL;
- return (err);
- }
- if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
- kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
- ra->next_rrd = NULL;
- return (SET_ERROR(EINVAL));
- }
-
- /*
- * Note: checksum is of everything up to but not including the
- * checksum itself.
- */
- ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
- ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
- receive_cksum(ra,
- offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
- &ra->next_rrd->header);
-
- zio_cksum_t cksum_orig =
- ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
- zio_cksum_t *cksump =
- &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
-
- if (ra->byteswap)
- byteswap_record(&ra->next_rrd->header);
-
- if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
- !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
- kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
- ra->next_rrd = NULL;
- return (SET_ERROR(ECKSUM));
- }
-
- receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
-
- return (0);
-}
-
-static void
-objlist_create(struct objlist *list)
-{
- list_create(&list->list, sizeof (struct receive_objnode),
- offsetof(struct receive_objnode, node));
- list->last_lookup = 0;
-}
-
-static void
-objlist_destroy(struct objlist *list)
-{
- for (struct receive_objnode *n = list_remove_head(&list->list);
- n != NULL; n = list_remove_head(&list->list)) {
- kmem_free(n, sizeof (*n));
- }
- list_destroy(&list->list);
-}
-
-/*
- * This function looks through the objlist to see if the specified object number
- * is contained in the objlist. In the process, it will remove all object
- * numbers in the list that are smaller than the specified object number. Thus,
- * any lookup of an object number smaller than a previously looked up object
- * number will always return false; therefore, all lookups should be done in
- * ascending order.
- */
-static boolean_t
-objlist_exists(struct objlist *list, uint64_t object)
-{
- struct receive_objnode *node = list_head(&list->list);
- ASSERT3U(object, >=, list->last_lookup);
- list->last_lookup = object;
- while (node != NULL && node->object < object) {
- VERIFY3P(node, ==, list_remove_head(&list->list));
- kmem_free(node, sizeof (*node));
- node = list_head(&list->list);
- }
- return (node != NULL && node->object == object);
-}
-
-/*
- * The objlist is a list of object numbers stored in ascending order. However,
- * the insertion of new object numbers does not seek out the correct location to
- * store a new object number; instead, it appends it to the list for simplicity.
- * Thus, any users must take care to only insert new object numbers in ascending
- * order.
- */
-static void
-objlist_insert(struct objlist *list, uint64_t object)
-{
- struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
- node->object = object;
-#ifdef ZFS_DEBUG
- struct receive_objnode *last_object = list_tail(&list->list);
- uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
- ASSERT3U(node->object, >, last_objnum);
-#endif
- list_insert_tail(&list->list, node);
-}
-
-/*
- * Issue the prefetch reads for any necessary indirect blocks.
- *
- * We use the object ignore list to tell us whether or not to issue prefetches
- * for a given object. We do this for both correctness (in case the blocksize
- * of an object has changed) and performance (if the object doesn't exist, don't
- * needlessly try to issue prefetches). We also trim the list as we go through
- * the stream to prevent it from growing to an unbounded size.
- *
- * The object numbers within will always be in sorted order, and any write
- * records we see will also be in sorted order, but they're not sorted with
- * respect to each other (i.e. we can get several object records before
- * receiving each object's write records). As a result, once we've reached a
- * given object number, we can safely remove any reference to lower object
- * numbers in the ignore list. In practice, we receive up to 32 object records
- * before receiving write records, so the list can have up to 32 nodes in it.
- */
-/* ARGSUSED */
-static void
-receive_read_prefetch(struct receive_arg *ra,
- uint64_t object, uint64_t offset, uint64_t length)
-{
- if (!objlist_exists(&ra->ignore_objlist, object)) {
- dmu_prefetch(ra->os, object, 1, offset, length,
- ZIO_PRIORITY_SYNC_READ);
- }
-}
-
-/*
- * Read records off the stream, issuing any necessary prefetches.
- */
-static int
-receive_read_record(struct receive_arg *ra)
-{
- int err;
-
- switch (ra->rrd->header.drr_type) {
- case DRR_OBJECT:
- {
- struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
- uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
- void *buf = NULL;
- dmu_object_info_t doi;
-
- if (size > 0)
- buf = kmem_zalloc(size, KM_SLEEP);
-
- err = receive_read_payload_and_next_header(ra, size, buf);
- if (err != 0) {
- kmem_free(buf, size);
- return (err);
- }
- err = dmu_object_info(ra->os, drro->drr_object, &doi);
- /*
- * See receive_read_prefetch for an explanation why we're
- * storing this object in the ignore_obj_list.
- */
- if (err == ENOENT ||
- (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
- objlist_insert(&ra->ignore_objlist, drro->drr_object);
- err = 0;
- }
- return (err);
- }
- case DRR_FREEOBJECTS:
- {
- err = receive_read_payload_and_next_header(ra, 0, NULL);
- return (err);
- }
- case DRR_WRITE:
- {
- struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
- arc_buf_t *abuf;
- boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
- if (DRR_WRITE_COMPRESSED(drrw)) {
- ASSERT3U(drrw->drr_compressed_size, >, 0);
- ASSERT3U(drrw->drr_logical_size, >=,
- drrw->drr_compressed_size);
- ASSERT(!is_meta);
- abuf = arc_loan_compressed_buf(
- dmu_objset_spa(ra->os),
- drrw->drr_compressed_size, drrw->drr_logical_size,
- drrw->drr_compressiontype);
- } else {
- abuf = arc_loan_buf(dmu_objset_spa(ra->os),
- is_meta, drrw->drr_logical_size);
- }
-
- err = receive_read_payload_and_next_header(ra,
- DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
- if (err != 0) {
- dmu_return_arcbuf(abuf);
- return (err);
- }
- ra->rrd->write_buf = abuf;
- receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
- drrw->drr_logical_size);
- return (err);
- }
- case DRR_WRITE_BYREF:
- {
- struct drr_write_byref *drrwb =
- &ra->rrd->header.drr_u.drr_write_byref;
- err = receive_read_payload_and_next_header(ra, 0, NULL);
- receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
- drrwb->drr_length);
- return (err);
- }
- case DRR_WRITE_EMBEDDED:
- {
- struct drr_write_embedded *drrwe =
- &ra->rrd->header.drr_u.drr_write_embedded;
- uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
- void *buf = kmem_zalloc(size, KM_SLEEP);
-
- err = receive_read_payload_and_next_header(ra, size, buf);
- if (err != 0) {
- kmem_free(buf, size);
- return (err);
- }
-
- receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
- drrwe->drr_length);
- return (err);
- }
- case DRR_FREE:
- {
- /*
- * It might be beneficial to prefetch indirect blocks here, but
- * we don't really have the data to decide for sure.
- */
- err = receive_read_payload_and_next_header(ra, 0, NULL);
- return (err);
- }
- case DRR_END:
- {
- struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
- if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
- return (SET_ERROR(ECKSUM));
- return (0);
- }
- case DRR_SPILL:
- {
- struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
- void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
- err = receive_read_payload_and_next_header(ra, drrs->drr_length,
- buf);
- if (err != 0)
- kmem_free(buf, drrs->drr_length);
- return (err);
- }
- default:
- return (SET_ERROR(EINVAL));
- }
-}
-
-/*
- * Commit the records to the pool.
- */
-static int
-receive_process_record(struct receive_writer_arg *rwa,
- struct receive_record_arg *rrd)
-{
- int err;
-
- /* Processing in order, therefore bytes_read should be increasing. */
- ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
- rwa->bytes_read = rrd->bytes_read;
-
- switch (rrd->header.drr_type) {
- case DRR_OBJECT:
- {
- struct drr_object *drro = &rrd->header.drr_u.drr_object;
- err = receive_object(rwa, drro, rrd->payload);
- kmem_free(rrd->payload, rrd->payload_size);
- rrd->payload = NULL;
- return (err);
- }
- case DRR_FREEOBJECTS:
- {
- struct drr_freeobjects *drrfo =
- &rrd->header.drr_u.drr_freeobjects;
- return (receive_freeobjects(rwa, drrfo));
- }
- case DRR_WRITE:
- {
- struct drr_write *drrw = &rrd->header.drr_u.drr_write;
- err = receive_write(rwa, drrw, rrd->write_buf);
- /* if receive_write() is successful, it consumes the arc_buf */
- if (err != 0)
- dmu_return_arcbuf(rrd->write_buf);
- rrd->write_buf = NULL;
- rrd->payload = NULL;
- return (err);
- }
- case DRR_WRITE_BYREF:
- {
- struct drr_write_byref *drrwbr =
- &rrd->header.drr_u.drr_write_byref;
- return (receive_write_byref(rwa, drrwbr));
- }
- case DRR_WRITE_EMBEDDED:
- {
- struct drr_write_embedded *drrwe =
- &rrd->header.drr_u.drr_write_embedded;
- err = receive_write_embedded(rwa, drrwe, rrd->payload);
- kmem_free(rrd->payload, rrd->payload_size);
- rrd->payload = NULL;
- return (err);
- }
- case DRR_FREE:
- {
- struct drr_free *drrf = &rrd->header.drr_u.drr_free;
- return (receive_free(rwa, drrf));
- }
- case DRR_SPILL:
- {
- struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
- err = receive_spill(rwa, drrs, rrd->payload);
- kmem_free(rrd->payload, rrd->payload_size);
- rrd->payload = NULL;
- return (err);
- }
- default:
- return (SET_ERROR(EINVAL));
- }
-}
-
-/*
- * dmu_recv_stream's worker thread; pull records off the queue, and then call
- * receive_process_record When we're done, signal the main thread and exit.
- */
-static void
-receive_writer_thread(void *arg)
-{
- struct receive_writer_arg *rwa = arg;
- struct receive_record_arg *rrd;
- for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
- rrd = bqueue_dequeue(&rwa->q)) {
- /*
- * If there's an error, the main thread will stop putting things
- * on the queue, but we need to clear everything in it before we
- * can exit.
- */
- if (rwa->err == 0) {
- rwa->err = receive_process_record(rwa, rrd);
- } else if (rrd->write_buf != NULL) {
- dmu_return_arcbuf(rrd->write_buf);
- rrd->write_buf = NULL;
- rrd->payload = NULL;
- } else if (rrd->payload != NULL) {
- kmem_free(rrd->payload, rrd->payload_size);
- rrd->payload = NULL;
- }
- kmem_free(rrd, sizeof (*rrd));
- }
- kmem_free(rrd, sizeof (*rrd));
- mutex_enter(&rwa->mutex);
- rwa->done = B_TRUE;
- cv_signal(&rwa->cv);
- mutex_exit(&rwa->mutex);
- thread_exit();
-}
-
-static int
-resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
-{
- uint64_t val;
- objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
- uint64_t dsobj = dmu_objset_id(ra->os);
- uint64_t resume_obj, resume_off;
-
- if (nvlist_lookup_uint64(begin_nvl,
- "resume_object", &resume_obj) != 0 ||
- nvlist_lookup_uint64(begin_nvl,
- "resume_offset", &resume_off) != 0) {
- return (SET_ERROR(EINVAL));
- }
- VERIFY0(zap_lookup(mos, dsobj,
- DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
- if (resume_obj != val)
- return (SET_ERROR(EINVAL));
- VERIFY0(zap_lookup(mos, dsobj,
- DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
- if (resume_off != val)
- return (SET_ERROR(EINVAL));
-
- return (0);
-}
-
-/*
- * Read in the stream's records, one by one, and apply them to the pool. There
- * are two threads involved; the thread that calls this function will spin up a
- * worker thread, read the records off the stream one by one, and issue
- * prefetches for any necessary indirect blocks. It will then push the records
- * onto an internal blocking queue. The worker thread will pull the records off
- * the queue, and actually write the data into the DMU. This way, the worker
- * thread doesn't have to wait for reads to complete, since everything it needs
- * (the indirect blocks) will be prefetched.
- *
- * NB: callers *must* call dmu_recv_end() if this succeeds.
- */
-int
-dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
- int cleanup_fd, uint64_t *action_handlep)
-{
- int err = 0;
- struct receive_arg ra = { 0 };
- struct receive_writer_arg rwa = { 0 };
- int featureflags;
- nvlist_t *begin_nvl = NULL;
-
- ra.byteswap = drc->drc_byteswap;
- ra.cksum = drc->drc_cksum;
- ra.vp = vp;
- ra.voff = *voffp;
-
- if (dsl_dataset_is_zapified(drc->drc_ds)) {
- (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
- drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
- sizeof (ra.bytes_read), 1, &ra.bytes_read);
- }
-
- objlist_create(&ra.ignore_objlist);
-
- /* these were verified in dmu_recv_begin */
- ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
- DMU_SUBSTREAM);
- ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
-
- /*
- * Open the objset we are modifying.
- */
- VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
-
- ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
-
- featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
-
- /* if this stream is dedup'ed, set up the avl tree for guid mapping */
- if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
- minor_t minor;
-
- if (cleanup_fd == -1) {
- ra.err = SET_ERROR(EBADF);
- goto out;
- }
- ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
- if (ra.err != 0) {
- cleanup_fd = -1;
- goto out;
- }
-
- if (*action_handlep == 0) {
- rwa.guid_to_ds_map =
- kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
- avl_create(rwa.guid_to_ds_map, guid_compare,
- sizeof (guid_map_entry_t),
- offsetof(guid_map_entry_t, avlnode));
- err = zfs_onexit_add_cb(minor,
- free_guid_map_onexit, rwa.guid_to_ds_map,
- action_handlep);
- if (ra.err != 0)
- goto out;
- } else {
- err = zfs_onexit_cb_data(minor, *action_handlep,
- (void **)&rwa.guid_to_ds_map);
- if (ra.err != 0)
- goto out;
- }
-
- drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
- }
-
- uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
- void *payload = NULL;
- if (payloadlen != 0)
- payload = kmem_alloc(payloadlen, KM_SLEEP);
-
- err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
- if (err != 0) {
- if (payloadlen != 0)
- kmem_free(payload, payloadlen);
- goto out;
- }
- if (payloadlen != 0) {
- err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
- kmem_free(payload, payloadlen);
- if (err != 0)
- goto out;
- }
-
- if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
- err = resume_check(&ra, begin_nvl);
- if (err != 0)
- goto out;
- }
-
- (void) bqueue_init(&rwa.q, zfs_recv_queue_length,
- offsetof(struct receive_record_arg, node));
- cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
- mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
- rwa.os = ra.os;
- rwa.byteswap = drc->drc_byteswap;
- rwa.resumable = drc->drc_resumable;
-
- (void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, curproc,
- TS_RUN, minclsyspri);
- /*
- * We're reading rwa.err without locks, which is safe since we are the
- * only reader, and the worker thread is the only writer. It's ok if we
- * miss a write for an iteration or two of the loop, since the writer
- * thread will keep freeing records we send it until we send it an eos
- * marker.
- *
- * We can leave this loop in 3 ways: First, if rwa.err is
- * non-zero. In that case, the writer thread will free the rrd we just
- * pushed. Second, if we're interrupted; in that case, either it's the
- * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
- * has been handed off to the writer thread who will free it. Finally,
- * if receive_read_record fails or we're at the end of the stream, then
- * we free ra.rrd and exit.
- */
- while (rwa.err == 0) {
- if (issig(JUSTLOOKING) && issig(FORREAL)) {
- err = SET_ERROR(EINTR);
- break;
- }
-
- ASSERT3P(ra.rrd, ==, NULL);
- ra.rrd = ra.next_rrd;
- ra.next_rrd = NULL;
- /* Allocates and loads header into ra.next_rrd */
- err = receive_read_record(&ra);
-
- if (ra.rrd->header.drr_type == DRR_END || err != 0) {
- kmem_free(ra.rrd, sizeof (*ra.rrd));
- ra.rrd = NULL;
- break;
- }
-
- bqueue_enqueue(&rwa.q, ra.rrd,
- sizeof (struct receive_record_arg) + ra.rrd->payload_size);
- ra.rrd = NULL;
- }
- if (ra.next_rrd == NULL)
- ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
- ra.next_rrd->eos_marker = B_TRUE;
- bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
-
- mutex_enter(&rwa.mutex);
- while (!rwa.done) {
- cv_wait(&rwa.cv, &rwa.mutex);
- }
- mutex_exit(&rwa.mutex);
-
- /*
- * If we are receiving a full stream as a clone, all object IDs which
- * are greater than the maximum ID referenced in the stream are
- * by definition unused and must be freed. Note that it's possible that
- * we've resumed this send and the first record we received was the END
- * record. In that case, max_object would be 0, but we shouldn't start
- * freeing all objects from there; instead we should start from the
- * resumeobj.
- */
- if (drc->drc_clone && drc->drc_drrb->drr_fromguid == 0) {
- uint64_t obj;
- if (nvlist_lookup_uint64(begin_nvl, "resume_object", &obj) != 0)
- obj = 0;
- if (rwa.max_object > obj)
- obj = rwa.max_object;
- obj++;
- int free_err = 0;
- int next_err = 0;
-
- while (next_err == 0) {
- free_err = dmu_free_long_object(rwa.os, obj);
- if (free_err != 0 && free_err != ENOENT)
- break;
-
- next_err = dmu_object_next(rwa.os, &obj, FALSE, 0);
- }
-
- if (err == 0) {
- if (free_err != 0 && free_err != ENOENT)
- err = free_err;
- else if (next_err != ESRCH)
- err = next_err;
- }
- }
-
- cv_destroy(&rwa.cv);
- mutex_destroy(&rwa.mutex);
- bqueue_destroy(&rwa.q);
- if (err == 0)
- err = rwa.err;
-
-out:
- nvlist_free(begin_nvl);
- if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
- zfs_onexit_fd_rele(cleanup_fd);
-
- if (err != 0) {
- /*
- * Clean up references. If receive is not resumable,
- * destroy what we created, so we don't leave it in
- * the inconsistent state.
- */
- dmu_recv_cleanup_ds(drc);
- }
-
- *voffp = ra.voff;
- objlist_destroy(&ra.ignore_objlist);
- return (err);
-}
-
-static int
-dmu_recv_end_check(void *arg, dmu_tx_t *tx)
-{
- dmu_recv_cookie_t *drc = arg;
- dsl_pool_t *dp = dmu_tx_pool(tx);
- int error;
-
- ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
-
- if (!drc->drc_newfs) {
- dsl_dataset_t *origin_head;
-
- error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
- if (error != 0)
- return (error);
- if (drc->drc_force) {
- /*
- * We will destroy any snapshots in tofs (i.e. before
- * origin_head) that are after the origin (which is
- * the snap before drc_ds, because drc_ds can not
- * have any snaps of its own).
- */
- uint64_t obj;
-
- obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
- while (obj !=
- dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
- dsl_dataset_t *snap;
- error = dsl_dataset_hold_obj(dp, obj, FTAG,
- &snap);
- if (error != 0)
- break;
- if (snap->ds_dir != origin_head->ds_dir)
- error = SET_ERROR(EINVAL);
- if (error == 0) {
- error = dsl_destroy_snapshot_check_impl(
- snap, B_FALSE);
- }
- obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
- dsl_dataset_rele(snap, FTAG);
- if (error != 0)
- break;
- }
- if (error != 0) {
- dsl_dataset_rele(origin_head, FTAG);
- return (error);
- }
- }
- error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
- origin_head, drc->drc_force, drc->drc_owner, tx);
- if (error != 0) {
- dsl_dataset_rele(origin_head, FTAG);
- return (error);
- }
- error = dsl_dataset_snapshot_check_impl(origin_head,
- drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
- dsl_dataset_rele(origin_head, FTAG);
- if (error != 0)
- return (error);
-
- error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
- } else {
- error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
- drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
- }
- return (error);
-}
-
-static void
-dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
-{
- dmu_recv_cookie_t *drc = arg;
- dsl_pool_t *dp = dmu_tx_pool(tx);
-
- spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
- tx, "snap=%s", drc->drc_tosnap);
-
- if (!drc->drc_newfs) {
- dsl_dataset_t *origin_head;
-
- VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
- &origin_head));
-
- if (drc->drc_force) {
- /*
- * Destroy any snapshots of drc_tofs (origin_head)
- * after the origin (the snap before drc_ds).
- */
- uint64_t obj;
-
- obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
- while (obj !=
- dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
- dsl_dataset_t *snap;
- VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
- &snap));
- ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
- obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
- dsl_destroy_snapshot_sync_impl(snap,
- B_FALSE, tx);
- dsl_dataset_rele(snap, FTAG);
- }
- }
- VERIFY3P(drc->drc_ds->ds_prev, ==,
- origin_head->ds_prev);
-
- dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
- origin_head, tx);
- dsl_dataset_snapshot_sync_impl(origin_head,
- drc->drc_tosnap, tx);
-
- /* set snapshot's creation time and guid */
- dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
- dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
- drc->drc_drrb->drr_creation_time;
- dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
- drc->drc_drrb->drr_toguid;
- dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
- ~DS_FLAG_INCONSISTENT;
-
- dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
- dsl_dataset_phys(origin_head)->ds_flags &=
- ~DS_FLAG_INCONSISTENT;
-
- drc->drc_newsnapobj =
- dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
-
- dsl_dataset_rele(origin_head, FTAG);
- dsl_destroy_head_sync_impl(drc->drc_ds, tx);
-
- if (drc->drc_owner != NULL)
- VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
- } else {
- dsl_dataset_t *ds = drc->drc_ds;
-
- dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
-
- /* set snapshot's creation time and guid */
- dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
- dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
- drc->drc_drrb->drr_creation_time;
- dsl_dataset_phys(ds->ds_prev)->ds_guid =
- drc->drc_drrb->drr_toguid;
- dsl_dataset_phys(ds->ds_prev)->ds_flags &=
- ~DS_FLAG_INCONSISTENT;
-
- dmu_buf_will_dirty(ds->ds_dbuf, tx);
- dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
- if (dsl_dataset_has_resume_receive_state(ds)) {
- (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
- DS_FIELD_RESUME_FROMGUID, tx);
- (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
- DS_FIELD_RESUME_OBJECT, tx);
- (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
- DS_FIELD_RESUME_OFFSET, tx);
- (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
- DS_FIELD_RESUME_BYTES, tx);
- (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
- DS_FIELD_RESUME_TOGUID, tx);
- (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
- DS_FIELD_RESUME_TONAME, tx);
- }
- drc->drc_newsnapobj =
- dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
- }
- /*
- * Release the hold from dmu_recv_begin. This must be done before
- * we return to open context, so that when we free the dataset's dnode,
- * we can evict its bonus buffer.
- */
- dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
- drc->drc_ds = NULL;
-}
-
-static int
-add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
-{
- dsl_pool_t *dp;
- dsl_dataset_t *snapds;
- guid_map_entry_t *gmep;
- int err;
-
- ASSERT(guid_map != NULL);
-
- err = dsl_pool_hold(name, FTAG, &dp);
- if (err != 0)
- return (err);
- gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
- err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
- if (err == 0) {
- gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
- gmep->gme_ds = snapds;
- avl_add(guid_map, gmep);
- dsl_dataset_long_hold(snapds, gmep);
- } else {
- kmem_free(gmep, sizeof (*gmep));
- }
-
- dsl_pool_rele(dp, FTAG);
- return (err);
-}
-
-static int dmu_recv_end_modified_blocks = 3;
-
-static int
-dmu_recv_existing_end(dmu_recv_cookie_t *drc)
-{
-#ifdef _KERNEL
- /*
- * We will be destroying the ds; make sure its origin is unmounted if
- * necessary.
- */
- char name[ZFS_MAX_DATASET_NAME_LEN];
- dsl_dataset_name(drc->drc_ds, name);
- zfs_destroy_unmount_origin(name);
-#endif
-
- return (dsl_sync_task(drc->drc_tofs,
- dmu_recv_end_check, dmu_recv_end_sync, drc,
- dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
-}
-
-static int
-dmu_recv_new_end(dmu_recv_cookie_t *drc)
-{
- return (dsl_sync_task(drc->drc_tofs,
- dmu_recv_end_check, dmu_recv_end_sync, drc,
- dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
-}
-
-int
-dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
-{
- int error;
-
- drc->drc_owner = owner;
-
- if (drc->drc_newfs)
- error = dmu_recv_new_end(drc);
- else
- error = dmu_recv_existing_end(drc);
-
- if (error != 0) {
- dmu_recv_cleanup_ds(drc);
- } else if (drc->drc_guid_to_ds_map != NULL) {
- (void) add_ds_to_guidmap(drc->drc_tofs,
- drc->drc_guid_to_ds_map,
- drc->drc_newsnapobj);
- }
- return (error);
-}
-
-/*
- * Return TRUE if this objset is currently being received into.
- */
-boolean_t
-dmu_objset_is_receiving(objset_t *os)
-{
- return (os->os_dsl_dataset != NULL &&
- os->os_dsl_dataset->ds_owner == dmu_recv_tag);
-}
diff --git a/usr/src/uts/common/fs/zfs/dsl_dataset.c b/usr/src/uts/common/fs/zfs/dsl_dataset.c
index 27873c0d45..caa6855034 100644
--- a/usr/src/uts/common/fs/zfs/dsl_dataset.c
+++ b/usr/src/uts/common/fs/zfs/dsl_dataset.c
@@ -56,7 +56,7 @@
#include <sys/dsl_destroy.h>
#include <sys/dsl_userhold.h>
#include <sys/dsl_bookmark.h>
-#include <sys/dmu_send.h>
+#include <sys/dmu_recv.h>
#include <sys/zio_checksum.h>
#include <sys/zio_compress.h>
#include <zfs_fletcher.h>
diff --git a/usr/src/uts/common/fs/zfs/sys/dmu_recv.h b/usr/src/uts/common/fs/zfs/sys/dmu_recv.h
new file mode 100644
index 0000000000..56b69e61b1
--- /dev/null
+++ b/usr/src/uts/common/fs/zfs/sys/dmu_recv.h
@@ -0,0 +1,64 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
+ * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
+ * Copyright (c) 2013, Joyent, Inc. All rights reserved.
+ * Copyright (c) 2014 Integros [integros.com]
+ */
+
+#ifndef _DMU_RECV_H
+#define _DMU_RECV_H
+
+#include <sys/inttypes.h>
+#include <sys/spa.h>
+
+extern const char *recv_clone_name;
+
+typedef struct dmu_recv_cookie {
+ struct dsl_dataset *drc_ds;
+ struct dmu_replay_record *drc_drr_begin;
+ struct drr_begin *drc_drrb;
+ const char *drc_tofs;
+ const char *drc_tosnap;
+ boolean_t drc_newfs;
+ boolean_t drc_byteswap;
+ boolean_t drc_force;
+ boolean_t drc_resumable;
+ boolean_t drc_clone;
+ struct avl_tree *drc_guid_to_ds_map;
+ zio_cksum_t drc_cksum;
+ uint64_t drc_newsnapobj;
+ void *drc_owner;
+ cred_t *drc_cred;
+} dmu_recv_cookie_t;
+
+int dmu_recv_begin(char *tofs, char *tosnap,
+ struct dmu_replay_record *drr_begin,
+ boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc);
+int dmu_recv_stream(dmu_recv_cookie_t *drc, struct vnode *vp, offset_t *voffp,
+ int cleanup_fd, uint64_t *action_handlep);
+int dmu_recv_end(dmu_recv_cookie_t *drc, void *owner);
+boolean_t dmu_objset_is_receiving(objset_t *os);
+
+#endif /* _DMU_RECV_H */
diff --git a/usr/src/uts/common/fs/zfs/sys/dmu_send.h b/usr/src/uts/common/fs/zfs/sys/dmu_send.h
index b8403313e9..65d8e99db6 100644
--- a/usr/src/uts/common/fs/zfs/sys/dmu_send.h
+++ b/usr/src/uts/common/fs/zfs/sys/dmu_send.h
@@ -39,8 +39,6 @@ struct drr_begin;
struct avl_tree;
struct dmu_replay_record;
-extern const char *recv_clone_name;
-
int dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
boolean_t large_block_ok, boolean_t compressok, int outfd,
uint64_t resumeobj, uint64_t resumeoff,
@@ -53,30 +51,4 @@ int dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
int outfd, struct vnode *vp, offset_t *off);
-typedef struct dmu_recv_cookie {
- struct dsl_dataset *drc_ds;
- struct dmu_replay_record *drc_drr_begin;
- struct drr_begin *drc_drrb;
- const char *drc_tofs;
- const char *drc_tosnap;
- boolean_t drc_newfs;
- boolean_t drc_byteswap;
- boolean_t drc_force;
- boolean_t drc_resumable;
- boolean_t drc_clone;
- struct avl_tree *drc_guid_to_ds_map;
- zio_cksum_t drc_cksum;
- uint64_t drc_newsnapobj;
- void *drc_owner;
- cred_t *drc_cred;
-} dmu_recv_cookie_t;
-
-int dmu_recv_begin(char *tofs, char *tosnap,
- struct dmu_replay_record *drr_begin,
- boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc);
-int dmu_recv_stream(dmu_recv_cookie_t *drc, struct vnode *vp, offset_t *voffp,
- int cleanup_fd, uint64_t *action_handlep);
-int dmu_recv_end(dmu_recv_cookie_t *drc, void *owner);
-boolean_t dmu_objset_is_receiving(objset_t *os);
-
#endif /* _DMU_SEND_H */
diff --git a/usr/src/uts/common/fs/zfs/sys/dsl_bookmark.h b/usr/src/uts/common/fs/zfs/sys/dsl_bookmark.h
index 3591986d7b..e477bb231c 100644
--- a/usr/src/uts/common/fs/zfs/sys/dsl_bookmark.h
+++ b/usr/src/uts/common/fs/zfs/sys/dsl_bookmark.h
@@ -20,6 +20,7 @@
#define _SYS_DSL_BOOKMARK_H
#include <sys/zfs_context.h>
+#include <sys/dsl_dataset.h>
#ifdef __cplusplus
extern "C" {
diff --git a/usr/src/uts/common/fs/zfs/zfs_ioctl.c b/usr/src/uts/common/fs/zfs/zfs_ioctl.c
index 486b7398a4..1b0dc60587 100644
--- a/usr/src/uts/common/fs/zfs/zfs_ioctl.c
+++ b/usr/src/uts/common/fs/zfs/zfs_ioctl.c
@@ -181,6 +181,7 @@
#include <sys/dsl_scan.h>
#include <sharefs/share.h>
#include <sys/dmu_objset.h>
+#include <sys/dmu_recv.h>
#include <sys/dmu_send.h>
#include <sys/dsl_destroy.h>
#include <sys/dsl_bookmark.h>