--- /dev/null	2012-10-17 21:48:39.000000000 +0200
+++ new/usr/src/uts/common/fs/zfs/fits_pass1.c	2012-10-17 21:48:39.031136000 +0200
@@ -0,0 +1,509 @@
+/*
+ * 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) 2012 STRATO AG. All rights reserved.
+ */
+#include <sys/zfs_context.h>
+#include <sys/errno.h>
+#include <sys/stat.h>
+#include <sys/fits.h>
+#include <sys/fits_impl.h>
+
+struct fits_enum {
+	fits_t *fe_fits;
+	uint64_t fe_parent_ino;
+	fits_dirent_t *fe_dirent_chain;
+};
+
+struct fits_file {
+	fits_t *ff_fits;
+	uint64_t ff_len;
+	uint64_t ff_last_byte;
+	uint64_t ff_ino;
+	fits_path_t *ff_path;
+	fits_dirent_t *ff_dirent;
+};
+
+static int fits_file_data_pass1(void *fits_filep, void *data, uint64_t off,
+    uint64_t len);
+static int fits_dirent_add_pass1(void *fits_enump, char *name, uint64_t ino);
+static int fits_dirent_mod_pass1(void *fits_enump, char *name,
+    uint64_t ino_old, uint64_t ino_new);
+static int fits_dir_add_pass1(fits_t *f, uint64_t ino);
+static int fits_mod_pass1(fits_t *f, uint64_t ino);
+
+static fits_ops_t _ops = {
+	.fits_dir_add = fits_dir_add_pass1,
+	.fits_dir_mod = fits_mod_pass1,
+	.fits_dirent_add = fits_dirent_add_pass1,
+	.fits_dirent_mod = fits_dirent_mod_pass1,
+	.fits_file_mod = fits_mod_pass1,
+	.fits_file_data = fits_file_data_pass1
+};
+
+static int fits_file_add_genchange(fits_t *f, uint64_t ino);
+
+int
+fits_start(fits_t *f, fits_ops_t **ops)
+{
+	int ret;
+
+	f->f_pass = PASS_LINK;
+	f->f_link_add_cnt.fc_head = NULL;
+	f->f_link_add_cnt.fc_name = "link_add_cnt";
+	f->f_del_dir_cnt.fc_head = NULL;
+	f->f_del_dir_cnt.fc_name = "del_dir_cnt";
+	f->f_put_back_cnt.fc_head = NULL;
+	f->f_put_back_cnt.fc_name = "put_back_cnt";
+	fits_send_init(f);
+
+	*ops = &_ops;
+
+	ret = fits_send_start(f);
+	if (ret) {
+		fits_abort(f);
+		return (ret);
+	}
+
+	return (0);
+}
+
+static int
+enum_dir(fits_t *f, uint64_t ino, fits_dirent_t *chain)
+{
+	struct fits_enum fe = {
+		.fe_fits = f,
+		.fe_parent_ino = ino,
+		.fe_dirent_chain = chain
+	};
+
+	return (fits_dir_contents(f, ino, &fe));
+}
+
+static int
+fits_file_data_pass1(void *fits_filep, void *data, uint64_t off, uint64_t len)
+{
+	struct fits_file *ff = fits_filep;
+
+	if (off + len > ff->ff_len)
+		len = ff->ff_len - off;
+
+	ff->ff_last_byte = off + len;
+
+	return fits_send_file_data(ff->ff_fits, &ff->ff_path,
+	    ff->ff_dirent, ff->ff_ino, off, len, data);
+}
+
+static int
+dirent_add_dir(fits_t *f, fits_dirent_t *dirent, uint64_t ino, int exists)
+{
+	fits_info_t si_old;
+	fits_info_t si_new;
+	int ret;
+
+	ret = fits_get_info(f, ino, FITS_OLD, &si_old,
+			    FI_ATTR_PARENT | FI_ATTR_GEN | FI_ATTR_MODE);
+	if (ret && ret != ENOENT)
+		return (ret);
+
+	if (ret == 0) {
+		ret = fits_get_info(f, ino, FITS_NEW, &si_new,
+				    FI_ATTR_GEN | FI_ATTR_MODE);
+		if (ret)
+			return (ret);
+
+		if (si_old.si_gen == si_new.si_gen ||
+		    (S_ISDIR(si_new.si_mode) && S_ISDIR(si_old.si_mode)))
+			return fits_send_rename(f, dirent, ino,
+						si_old.si_parent, exists);
+		return (0);
+	}
+
+	if (ino > f->f_current_ino)
+		return (0);
+
+	/* dir is new */
+	ret = fits_send_mkdir(f, dirent, ino, exists);
+	if (ret)
+		return (ret);
+
+	return (enum_dir(f, ino, dirent));
+}
+
+int
+fits_dirent_add_file(fits_t *f, fits_dirent_t *dirent,
+    uint64_t ino, uint64_t mode, int exists)
+{
+	fits_info_t si_old;
+	fits_info_t si_new;
+	int ret;
+	fits_path_t *fits_path;
+	uint64_t new_count;
+	uint64_t old_aux;
+
+	ret = fits_get_info(f, ino, FITS_OLD, &si_old,
+			    FI_ATTR_GEN | FI_ATTR_PARENT);
+	if (ret && ret != ENOENT)
+		return (ret);
+
+	if (ret == 0) {
+		ret = fits_get_info(f, ino, FITS_NEW, &si_new,
+				    FI_ATTR_GEN);
+		if (ret)
+			return (ret);
+
+		if (si_old.si_gen == si_new.si_gen)
+			return fits_send_link(f, dirent, ino, si_old.si_parent,
+			    FITS_OLD, exists);
+	}
+
+	/* file is new */
+	ret = fits_add_count(&f->f_link_add_cnt, ino, 1, dirent->fd_parent_ino,
+	    &new_count, &old_aux);
+	if (ret)
+		return (ret);
+
+	if (new_count == 1)
+		ret = fits_send_create_file(f, dirent, ino,
+					    exists, &fits_path);
+	else
+		ret = fits_send_link(f, dirent, ino, old_aux, FITS_NEW, exists);
+	if (ret)
+		return (ret);
+
+	ret = fits_get_info(f, ino, FITS_NEW, &si_new,
+			    FI_ATTR_SIZE | FI_ATTR_LINKS);
+	ASSERT(ret == 0);
+	if (new_count == 1 && S_ISREG(mode)) {
+		struct fits_file ff;
+
+		ff.ff_ino = ino;
+		ff.ff_len = si_new.si_size;
+		ff.ff_fits = f;
+		ff.ff_path = fits_path;
+		ff.ff_dirent = dirent;
+		ff.ff_last_byte = 0;
+		ret = fits_file_contents(f, ino, &ff);
+		fits_path_free(ff.ff_path);
+		if (ret)
+			return (ret);
+		if (ff.ff_last_byte != si_new.si_size) {
+			/* sparse end */
+			ret = fits_send_truncate(f, NULL, ino, si_new.si_size);
+			if (ret)
+				return (ret);
+		}
+	}
+	if (new_count == si_new.si_nlinks)
+		fits_free_count(&f->f_link_add_cnt, ino);
+
+	return (0);
+}
+
+static int
+dirent_add(fits_t *f, fits_dirent_t *dirent, uint64_t ino, int exists)
+{
+	fits_info_t si;
+	int ret;
+
+	ret = fits_get_info(f, ino, FITS_NEW, &si, FI_ATTR_MODE);
+	if (ret)
+		return (ret);
+
+	if (S_ISDIR(si.si_mode)) {
+		return (dirent_add_dir(f, dirent, ino, exists));
+	} else {
+		return (fits_dirent_add_file(f, dirent, ino, si.si_mode,
+		    exists));
+	}
+}
+
+static int
+fits_dirent_add_pass1(void *fits_enump, char *name, uint64_t ino)
+{
+	struct fits_enum *fe = fits_enump;
+	fits_dirent_t dirent = {
+		.fd_name = name,
+		.fd_parent_ino = fe->fe_parent_ino,
+		.fd_prev = fe->fe_dirent_chain,
+	};
+
+	return (dirent_add(fe->fe_fits, &dirent, ino, 0));
+}
+
+static int
+fits_dirent_mod_pass1(void *fits_enump, char *name,
+    uint64_t ino_old, uint64_t ino_new)
+{
+	struct fits_enum *fe = fits_enump;
+	fits_dirent_t dirent = {
+		.fd_name = name,
+		.fd_parent_ino = fe->fe_parent_ino,
+		.fd_prev = fe->fe_dirent_chain,
+	};
+
+	return (dirent_add(fe->fe_fits, &dirent, ino_new, 1));
+}
+
+static int
+fits_file_add_genchange(fits_t *f, uint64_t ino)
+{
+	int ret;
+	char *name = NULL;
+
+	f->f_current_ino = ino;
+	f->f_current_path = NULL;
+
+	/*
+	 * only called when generation has changed. TODO: move to own
+	 * function
+	 */
+	fits_info_t si_old;
+	fits_info_t si_new;
+	int same_name = 0;
+
+	ret = fits_get_info(f, ino, FITS_OLD, &si_old, FI_ATTR_MODE |
+			    FI_ATTR_LINKS | FI_ATTR_PARENT);
+	if (ret)
+		return (ret);
+	ret = fits_get_info(f, ino, FITS_NEW, &si_new, FI_ATTR_MODE |
+			    FI_ATTR_LINKS | FI_ATTR_PARENT);
+	if (ret)
+		return (ret);
+
+	if (si_old.si_nlinks > 1 && si_new.si_nlinks > 1)
+		return fits_add_count(&f->f_link_add_cnt, ino, 0, 0, NULL,
+		    NULL);
+
+	if (S_ISDIR(si_old.si_mode))
+		return (0);
+
+	fits_which_t from;
+	fits_which_t to;
+	uint64_t new_ino;
+	uint64_t parent = si_new.si_parent;
+
+	if (si_old.si_nlinks == 1) {
+		from = FITS_OLD;
+		to = FITS_NEW;
+		parent = si_old.si_parent;
+	} else if (si_old.si_nlinks > 1 && si_new.si_nlinks == 1) {
+		from = FITS_NEW;
+		to = FITS_OLD;
+		parent = si_new.si_parent;
+	} else {
+		return (EINVAL);
+	}
+
+	ret = fits_find_entry(f, parent, ino, from, &name);
+	if (ret)
+		return (ret);
+
+	ret = fits_lookup_entry(f, parent, name, to, &new_ino);
+	if (ret && ret != ENOENT)
+		goto out;
+	if (ret == 0 && new_ino == ino)
+		same_name = 1;
+
+	if ((si_old.si_nlinks == 1 || si_new.si_nlinks == 1) && !same_name) {
+		ret = 0;
+		goto out;
+	}
+
+	fits_dirent_t dirent = {
+		.fd_parent_ino = parent,
+		.fd_name = name,
+		.fd_prev = NULL
+	};
+
+	ret = fits_send_unlink(f, &dirent, ino);
+	if (ret)
+		goto out;
+	ret = fits_dirent_add_file(f, &dirent, ino, si_new.si_mode, 0);
+
+out:
+	fits_free_name(name);
+	return (ret);
+}
+
+static int
+fits_dir_add_pass1(fits_t *f, uint64_t ino)
+{
+	int ret;
+	uint64_t parent;
+	uint64_t first_parent = FITS_NO_INO;
+	fits_info_t si;
+	fits_dirent_t dirent;
+	int same_name = 0;
+	char *name = NULL;
+
+	f->f_current_ino = ino;
+	f->f_current_path = NULL;
+
+	parent = ino;
+	while (1) {
+		/* the new parent must exist, otherwise the fs is wrong */
+		ret = fits_get_info(f, parent, FITS_NEW, &si,
+				    FI_ATTR_PARENT);
+		if (ret)
+			return (ret);
+		if (first_parent == FITS_NO_INO)
+			first_parent = si.si_parent;
+
+		ret = fits_get_info(f, parent, FITS_OLD, &si, 0);
+		if (ret && ret != ENOENT)
+			return (ret);
+		if (ret != ENOENT)
+			break;
+
+		/*
+		 * this check is only needed for a full send, on all
+		 * incrementals the parent already exists and it breaks out
+		 * above
+		 */
+		if (parent == si.si_parent) {
+			first_parent = FITS_NO_INO;
+			break;
+		}
+		parent = si.si_parent;
+
+		if (parent > ino)
+			return (0);
+	}
+
+	/*
+	 * check for same-name
+	 */
+	if (first_parent != FITS_NO_INO) {
+		ret = fits_get_info(f, first_parent, FITS_OLD, &si,
+		    FI_ATTR_MODE);
+		if (ret && ret != ENOENT)
+			return (ret);
+		if (ret == 0 && S_ISDIR(si.si_mode)) {
+			uint64_t old_ino;
+
+			ret = fits_find_entry(f, first_parent, ino,
+			    FITS_NEW, &name);
+			if (ret)
+				return (ret);
+
+			ret = fits_lookup_entry(f, first_parent, name,
+			    FITS_OLD, &old_ino);
+			if (ret && ret != ENOENT) {
+				goto out;
+			}
+			if (ret == 0) {
+				same_name = 1;
+				dirent.fd_name = name;
+				dirent.fd_parent_ino = first_parent;
+				dirent.fd_prev = NULL;
+				if (old_ino == ino) {
+					ret = fits_add_count(&f->f_link_add_cnt,
+					    ino, 0, 0, NULL, NULL);
+					if (ret)
+						goto out;
+				}
+			}
+		}
+	}
+	/* dir is new */
+	ret = fits_send_mkdir(f, same_name ? &dirent : NULL, ino, same_name);
+	if (ret)
+		goto out;
+
+	ret = enum_dir(f, ino, NULL);
+
+out:
+	fits_free_name(name);
+	return (ret);
+}
+
+static int
+fits_mod_pass1(fits_t *f, uint64_t ino)
+{
+	fits_info_t si_old;
+	fits_info_t si_new;
+	int ret;
+
+	f->f_current_ino = ino;
+	f->f_current_path = NULL;
+
+	ret = fits_get_info(f, ino, FITS_NEW, &si_new, FI_ATTR_SIZE |
+			    FI_ATTR_MODE | FI_ATTR_GEN | FI_ATTR_UID |
+			    FI_ATTR_GID | FI_ATTR_SIZE);
+	if (ret)
+		return (ret);
+	ret = fits_get_info(f, ino, FITS_OLD, &si_old, FI_ATTR_GEN |
+			    FI_ATTR_MODE | FI_ATTR_UID |
+			    FI_ATTR_GID | FI_ATTR_SIZE);
+	if (ret)
+		return (ret);
+
+	if (!(S_ISDIR(si_old.si_mode) && S_ISDIR(si_new.si_mode)) &&
+	    si_new.si_gen != si_old.si_gen) {
+		if (S_ISDIR(si_new.si_mode))
+			return (fits_dir_add_pass1(f, ino));
+		else
+			return (fits_file_add_genchange(f, ino));
+	}
+
+	if (S_ISDIR(si_new.si_mode)) {
+		ret = enum_dir(f, ino, NULL);
+		if (ret)
+			return (ret);
+	}
+
+	if (S_ISREG(si_new.si_mode)) {
+		struct fits_file ff;
+		ff.ff_ino = ino;
+		ff.ff_len = si_new.si_size;
+		ff.ff_fits = f;
+		ff.ff_path = NULL;
+		ff.ff_dirent = NULL;
+		ff.ff_last_byte = 0;
+
+		ret = fits_file_contents(f, ino, &ff);
+		fits_path_free(ff.ff_path);
+		if (ret)
+			return (ret);
+		if (si_new.si_size < si_old.si_size ||
+		    (si_new.si_size != si_old.si_size &&
+		    si_new.si_size != ff.ff_last_byte)) {
+			ret = fits_send_truncate(f, NULL, ino, si_new.si_size);
+			if (ret)
+				return (ret);
+		}
+	}
+
+	if (si_old.si_uid != si_new.si_uid || si_old.si_gid != si_new.si_gid) {
+		ret = fits_send_chown(f, NULL, ino, si_new.si_uid,
+		    si_new.si_gid);
+		if (ret)
+			return (ret);
+	}
+	if (si_old.si_mode != si_new.si_mode) {
+		ret = fits_send_chmod(f, NULL, ino, si_new.si_mode);
+		if (ret)
+			return (ret);
+	}
+
+	return (ret);
+}
