--- /dev/null	2012-10-17 21:48:39.000000000 +0200
+++ new/usr/src/uts/common/fs/zfs/fits_send.c	2012-10-17 21:48:39.246138000 +0200
@@ -0,0 +1,942 @@
+/*
+ * 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/stat.h>
+#include <sys/mkdev.h>
+#include <sys/errno.h>
+#include <sys/types.h>
+#include <sys/fits.h>
+#include <sys/fits_impl.h>
+#include <sys/fits_crc32c.h>
+
+#define	TEMPNAME_PREFIX "fits-tempname-"
+/* 2^128 needs 39 digits in decimal */
+#define	TEMPNAME_SIZE (sizeof (TEMPNAME_PREFIX) + 39)
+
+void
+fits_send_init(fits_t *f)
+{
+	f->f_alloc_len = FITS_SEND_BUF_SIZE;
+	f->f_buf = kmem_alloc(f->f_alloc_len, KM_SLEEP);
+	f->f_size = 0;
+}
+
+void
+fits_send_fini(fits_t *f)
+{
+	kmem_free(f->f_buf, f->f_alloc_len);
+}
+
+static int
+fits_send_reserve(fits_t *f, void **buf, int len)
+{
+	int res = f->f_alloc_len - f->f_size;
+	if (len > res)
+		return (-E2BIG);
+	*buf = f->f_buf + f->f_size;
+	f->f_size += len;
+
+	return (0);
+}
+
+static int
+fits_send_put(fits_t *f, void *buf, int len)
+{
+	int ret;
+	void *p;
+
+	ret = fits_send_reserve(f, &p, len);
+	if (ret)
+		return (ret);
+
+	memcpy(p, buf, len);
+
+	return (0);
+}
+
+static int
+fits_send_put_attr(fits_t *f, uint16_t attr, void *buf, int len)
+{
+	fits_attr_header_t hdr;
+	int ret;
+
+	LE_OUT16(&hdr.fa_type, attr);
+	LE_OUT16(&hdr.fa_len, len);
+
+	ret = fits_send_put(f, &hdr, sizeof (hdr));
+	if (ret)
+		return (ret);
+	return (fits_send_put(f, buf, len));
+}
+
+static int
+fits_send_reserve_attr(fits_t *f, uint16_t attr, void **buf, int len)
+{
+	fits_attr_header_t hdr;
+	int ret;
+
+	LE_OUT16(&hdr.fa_type, attr);
+	LE_OUT16(&hdr.fa_len, len);
+
+	ret = fits_send_put(f, &hdr, sizeof (hdr));
+	if (ret)
+		return (ret);
+	return (fits_send_reserve(f, buf, len));
+}
+
+static int
+fits_send_put_u64(fits_t *f, uint16_t attr, uint64_t val)
+{
+	uint64_t v;
+
+	LE_OUT64(&v, val);
+	return (fits_send_put_attr(f, attr, &v, sizeof (v)));
+}
+
+static int
+fits_send_put_time(fits_t *f, uint16_t attr, fits_time_t *t)
+{
+	char buf[12];
+
+	LE_OUT64(buf, t->st_sec);
+	LE_OUT32(buf + 8, t->st_nsec);
+
+	return (fits_send_put_attr(f, attr, buf, sizeof (buf)));
+}
+
+static int
+fits_cmd_start(fits_t *f, uint16_t cmd)
+{
+	fits_cmd_header_t ch;
+
+	memset(&ch, 0, sizeof (ch));
+	LE_OUT16(&ch.fc_cmd, cmd);
+	f->f_size = 0;
+	return (fits_send_put(f, &ch, sizeof (ch)));
+}
+
+static int
+fits_cmd_send(fits_t *f)
+{
+	fits_cmd_header_t *ch;
+	uint32_t crc;
+	int ret;
+
+	ch = (fits_cmd_header_t *)f->f_buf;
+	LE_OUT32(&ch->fc_len, f->f_size - sizeof (*ch));
+	ch->fc_crc = 0;
+
+	crc = fits_crc32c(0, f->f_buf, f->f_size);
+	LE_OUT32(&ch->fc_crc, crc);
+
+	ret = fits_write(f, f->f_buf, f->f_size);
+	f->f_size = 0;
+
+	return (ret);
+}
+
+static int
+fits_send_stream_header(fits_t *f)
+{
+	fits_stream_header_t header;
+
+	strcpy(header.fs_magic, FITS_SEND_STREAM_MAGIC);
+	LE_OUT32(&header.fs_version, FITS_SEND_STREAM_VERSION);
+
+	return (fits_write(f, (uint8_t *)&header, sizeof (header)));
+}
+
+static void
+tempname(uint64_t ino, char *buf, int maxlen)
+{
+	int l = sizeof (TEMPNAME_PREFIX) - 1;
+	memcpy(buf, TEMPNAME_PREFIX, MIN(maxlen, l));
+	snprintf(buf + l, maxlen - l, "%llu", (long long)ino);
+}
+
+static void
+path_add_name(fits_path_t **fp, char *name, int namelen)
+{
+	fits_path_t *new;
+
+	new = kmem_alloc(sizeof (*new) + namelen + 1, KM_SLEEP);
+	new->fp_next = *fp;
+	new->fp_len = namelen + 1;
+	new->fp_total_len = namelen + 1;
+	if (*fp)
+		new->fp_total_len += (*fp)->fp_total_len;
+	memcpy(new->fp_buf, name, namelen);
+	new->fp_buf[namelen] = '\0';
+	*fp = new;
+}
+
+static void
+path_copy(fits_path_t *fp, char *b)
+{
+	fits_path_t *cur;
+
+	for (cur = fp; cur; cur = cur->fp_next) {
+		*b = '/';
+		memcpy(b + 1, cur->fp_buf, cur->fp_len - 1);
+		b += cur->fp_len;
+	}
+}
+
+static void
+path2buf(fits_path_t *fp, char **buf, int *buf_len)
+{
+	char *b;
+
+	*buf_len = fp->fp_total_len + 1; /* one for the trailing 0-byte */
+	*buf = b = kmem_alloc(*buf_len, KM_SLEEP);
+
+	path_copy(fp, b);
+
+	b[*buf_len - 1] = '\0';
+}
+
+static int
+put_path(fits_t *f, uint16_t attr, fits_path_t *fp)
+{
+	int ret;
+	void *p;
+
+	ret = fits_send_reserve_attr(f, attr, &p, fp->fp_total_len);
+	if (ret)
+		return (ret);
+	path_copy(fp, p);
+
+	return (0);
+}
+
+void
+fits_path_free(fits_path_t *fp)
+{
+	fits_path_t *next;
+	while (fp) {
+		next = fp->fp_next;
+		kmem_free(fp, fp->fp_len + sizeof (*fp));
+		fp = next;
+	}
+}
+
+static int
+is_ino_run(fits_t *f, uint64_t ino)
+{
+	int ret;
+	fits_info_t si;
+
+	while (1) {
+		if (ino > f->f_current_ino)
+			return (0);
+		ret = fits_get_info(f, ino, FITS_OLD, &si, 0);
+		if (ret && ret != ENOENT)
+			return (ret);
+		if (ret != ENOENT)
+			break;
+		ret = fits_get_info(f, ino, FITS_NEW, &si, FI_ATTR_PARENT);
+		if (ret && ret != ENOENT)
+			return (ret);
+		if (ret)
+			return (0);	/* ignore for now */
+		ino = si.si_parent;
+	}
+	return (1);
+}
+
+static int
+build_path(fits_t *f, fits_dirent_t *dirent, uint64_t ino,
+    int devise_tempname, fits_which_t which_in, fits_path_t **fp)
+{
+	int ret = 0;
+	fits_dirent_t *de;
+	fits_info_t si;
+	fits_which_t which;
+	fits_dirent_t temp_dirent;
+	char temp_buf[TEMPNAME_SIZE];
+
+	if (devise_tempname) {
+		if (!dirent)
+			return (EINVAL);
+		temp_dirent = *dirent;
+		tempname(ino, temp_buf, sizeof (temp_buf));
+		temp_dirent.fd_name = temp_buf;
+		dirent = &temp_dirent;
+	}
+
+	*fp = NULL;
+
+	for (de = dirent; de; de = de->fd_prev) {
+		path_add_name(fp, de->fd_name, strlen(de->fd_name));
+		ino = de->fd_parent_ino;
+	}
+
+	/*
+	 * XXX TODO check if f->f_current_path is set. if yes, use it instead.
+	 * otherwise save result of loop below to f_current_path
+	 */
+	while (1) {
+		int namebuflen;
+		char *name;
+		char *t_name;
+		uint64_t old_parent;
+		uint64_t new_parent;
+		uint64_t old_gen = 0;
+		uint64_t new_gen = 0;
+		uint64_t parent;
+		int check_tempname;
+		uint64_t old_mode = 0;
+
+		old_parent = 0;
+		new_parent = 0;
+		check_tempname = 0;
+		ret = fits_get_info(f, ino, FITS_OLD, &si, FI_ATTR_PARENT |
+		    FI_ATTR_GEN | FI_ATTR_MODE);
+		if (ret && ret != ENOENT)
+			return (ret);
+		if (ret == 0) {
+			old_parent = si.si_parent;
+			old_gen = si.si_gen;
+			old_mode = si.si_mode;
+		}
+		ret = fits_get_info(f, ino, FITS_NEW, &si, FI_ATTR_PARENT |
+		    FI_ATTR_GEN | FI_ATTR_MODE);
+		if (ret && ret != ENOENT)
+			return (ret);
+		if (ret == 0) {
+			new_parent = si.si_parent;
+			new_gen = si.si_gen;
+		}
+		if (old_parent && new_parent && old_gen != new_gen &&
+		    !(S_ISDIR(old_mode) && S_ISDIR(si.si_mode))) {
+			if (which_in == FITS_OLD) {
+				new_parent = 0;
+			} else if (which_in == FITS_NEW) {
+				old_parent = 0;
+				if (S_ISDIR(si.si_mode))
+					check_tempname = 1;
+			}
+		}
+
+		if (f->f_pass == PASS_LINK) {
+			if (old_parent && !new_parent) {
+				which = FITS_OLD;
+			} else if (!old_parent && new_parent) {
+				which = FITS_NEW;
+			} else if (is_ino_run(f, new_parent)) {
+				check_tempname = 1;
+				which = FITS_NEW;
+			} else {
+				which = FITS_OLD;
+			}
+		} else {
+			if (old_parent && !new_parent) {
+				which = FITS_OLD;
+			} else {
+				check_tempname = 1;
+				which = FITS_NEW;
+			}
+		}
+		if (which == FITS_OLD)
+			parent = old_parent;
+		else
+			parent = new_parent;
+		if (parent == ino)
+			break;
+		ret = fits_find_entry(f, parent, ino, which, &t_name);
+		if (ret)
+			return (ret);
+		name = strdup(t_name);
+		fits_free_name(t_name);
+		namebuflen = strlen(name) + 1;
+		if (check_tempname) {
+			fits_info_t si_old;
+			fits_info_t si_new;
+
+			ret = fits_get_info(f, parent, FITS_OLD,
+					    &si_old, FI_ATTR_GEN);
+			if (ret && ret != ENOENT)
+				return (ret);
+			if (ret == 0) {
+				ret = fits_get_info(f, parent, FITS_NEW,
+						    &si_new, FI_ATTR_GEN);
+				if (ret)
+					return (ret);
+				if (si_old.si_gen != si_new.si_gen)
+					check_tempname = 0;
+			} else {
+				check_tempname = 0;
+			}
+		}
+		if (check_tempname) {
+			uint64_t old_ino;
+
+			ret = fits_lookup_entry(f, parent, name,
+						FITS_OLD, &old_ino);
+			if (ret && ret != ENOENT) {
+				fits_free_name(name);
+				return (ret);
+			}
+			if ((ret == 0 && old_ino != ino) ||
+			    (ret == 0 && S_ISDIR(si.si_mode) &&
+			    !S_ISDIR(old_mode) && old_ino == ino)) {
+				int ret;
+				uint64_t cnt = 1;
+
+				if (f->f_pass == PASS_UNLINK &&
+				    new_parent < f->f_current_ino) {
+					ret = fits_get_count(&f->f_put_back_cnt,
+					    old_ino, &cnt, NULL);
+					if (ret && ret != ENOENT)
+						return (ret);
+				}
+				if (cnt) {
+					kmem_free(name, namebuflen);
+					namebuflen = TEMPNAME_SIZE;
+					name = kmem_alloc(namebuflen, KM_SLEEP);
+					tempname(ino, name, namebuflen);
+				}
+			}
+		}
+		ino = parent;
+		path_add_name(fp, name, strlen(name));
+		kmem_free(name, namebuflen);
+	}
+	if (*fp == NULL)
+		path_add_name(fp, "", 0);
+
+	return (0);
+}
+
+int
+fits_send_start(fits_t *f)
+{
+	int ret;
+	uint8_t o_uuid[16];
+	uint8_t n_uuid[16];
+	uint64_t o_ctrans;
+	uint64_t n_ctrans;
+	char *path = NULL;
+	int len;
+	char *p;
+	int cmd = FITS_CMD_SUBVOL;
+
+	ret = fits_send_stream_header(f);
+	if (ret) {
+		fits_abort(f);
+		return (ret);
+	}
+
+	if ((ret = fits_get_uuid(f, FITS_NEW, n_uuid)) ||
+	    (ret = fits_get_ctransid(f, FITS_NEW, &n_ctrans)) ||
+	    (ret = fits_get_snapname(f, FITS_NEW, &path, &len)))
+		goto out;
+	/* for now, strip the pool name */
+	if ((p = strchr(path, '/')))
+		++p;
+	else
+		p = path;
+	ret = fits_get_uuid(f, FITS_OLD, o_uuid);
+	if (ret && ret != ENOENT)
+		goto out;
+	if (ret == 0) {
+		ret = fits_get_ctransid(f, FITS_OLD, &o_ctrans);
+		if (ret)
+			goto out;
+		cmd = FITS_CMD_SNAPSHOT;
+	}
+	if ((ret = fits_cmd_start(f, cmd)) ||
+	    (ret = fits_send_put_attr(f, FITS_ATTR_PATH, p, strlen(p))) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_CTRANSID, n_ctrans)) ||
+	    (ret = fits_send_put_attr(f, FITS_ATTR_UUID, n_uuid, 16)))
+		goto out;
+	if (cmd == FITS_CMD_SNAPSHOT) {
+		if ((ret = fits_send_put_u64(f, FITS_ATTR_CLONE_CTRANSID,
+		    o_ctrans)) ||
+		    (ret = fits_send_put_attr(f, FITS_ATTR_CLONE_UUID,
+		    o_uuid, 16)))
+			goto out;
+	}
+	ret = fits_cmd_send(f);
+
+out:
+	kmem_free(path, len);
+
+	return (ret);
+}
+
+int
+fits_send_create_file(fits_t *f, fits_dirent_t *dirent, uint64_t ino,
+    int devise_tempname, fits_path_t **path_ret)
+{
+	fits_path_t *path = NULL;
+	fits_info_t si;
+	int ret;
+	int send_rdev = 0;
+	int cmd;
+	uint64_t rdev = 0;
+	char *symlink = NULL;
+	int symlen = 0;
+
+	ret = build_path(f, dirent, ino, devise_tempname, FITS_NEW, &path);
+	if (ret)
+		goto out;
+
+	ret = fits_get_info(f, ino, FITS_NEW, &si,
+			    FI_ATTR_MODE | FI_ATTR_UID | FI_ATTR_GID);
+	if (ret)
+		goto out;
+
+	if (S_ISREG(si.si_mode)) {
+		cmd = FITS_CMD_MKFILE;
+	} else if (S_ISDIR(si.si_mode)) {
+		cmd = FITS_CMD_MKDIR;
+	} else if (S_ISLNK(si.si_mode)) {
+		cmd = FITS_CMD_SYMLINK;
+		ret = fits_read_symlink(f, ino, FITS_NEW, &symlink, &symlen);
+		if (ret)
+			goto out;
+	} else if (S_ISCHR(si.si_mode) || S_ISBLK(si.si_mode)) {
+		cmd = FITS_CMD_MKNOD;
+		send_rdev = 1;
+	} else if (S_ISFIFO(si.si_mode)) {
+		cmd = FITS_CMD_MKFIFO;
+	} else if (S_ISSOCK(si.si_mode)) {
+		cmd = FITS_CMD_MKSOCK;
+	} else {
+		/* unknown file type, ignore for now */
+		printf("!send_create_file: ignore file with mode 0%lo\n",
+			(unsigned long)si.si_mode);
+		return (0);
+	}
+
+	if (send_rdev) {
+		fits_info_t sirdev;
+		uint64_t r_major;
+		uint64_t r_minor;
+		ret = fits_get_info(f, ino, FITS_NEW, &sirdev, FI_ATTR_RDEV);
+		if (ret)
+			goto out;
+		rdev = sirdev.si_rdev;
+
+		/* XXX hardcodedly transform rdev to linux form */
+		r_major = rdev >> 32;
+		r_minor = rdev & 0xffffffful;
+		rdev = ((r_minor & 0xff) | ((r_major & 0xfff) << 8) |
+		    ((r_minor >> 8) << 20) | ((r_major >> 12) << 44));
+	}
+	/* send MKFILE */
+	if ((ret = fits_cmd_start(f, cmd)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_INO, ino)))
+		goto out;
+	if (send_rdev) {
+		ret = fits_send_put_u64(f, FITS_ATTR_RDEV, rdev);
+		if (ret)
+			goto out;
+		ret = fits_send_put_u64(f, FITS_ATTR_MODE, si.si_mode);
+		if (ret)
+			goto out;
+	}
+	if (S_ISLNK(si.si_mode)) {
+		ret = fits_send_put_attr(f, FITS_ATTR_PATH_LINK,
+					symlink, strlen(symlink));
+		if (ret)
+			goto out;
+	}
+	if ((ret = fits_cmd_send(f)))
+		goto out;
+
+	/* send CHOWN */
+	if ((ret = fits_cmd_start(f, FITS_CMD_CHOWN)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_UID, si.si_uid)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_GID, si.si_gid)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+	/* send CHMOD */
+	if ((ret = fits_cmd_start(f, FITS_CMD_CHMOD)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_MODE,
+	    si.si_mode & 0xfff)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	if (ret == 0 && path_ret)
+		*path_ret = path;
+	else
+		fits_path_free(path);
+	if (symlink)
+		kmem_free(symlink, symlen);
+	return (ret);
+}
+
+int
+fits_send_link(fits_t *f, fits_dirent_t *new_dirent, uint64_t ino,
+    uint64_t old_parent_ino, fits_which_t which, int devise_tempname)
+{
+	fits_path_t *new_path = NULL;
+	fits_path_t *old_path = NULL;
+	int ret;
+	fits_dirent_t old_dirent = {
+		.fd_name = NULL,
+		.fd_parent_ino = old_parent_ino,
+		.fd_prev = NULL,
+	};
+
+	ret = fits_find_entry(f, old_parent_ino, ino, which,
+	    &old_dirent.fd_name);
+	if (ret)
+		return (ret);
+	ret = build_path(f, &old_dirent, ino, 0, FITS_OLD, &old_path);
+	if (ret)
+		goto out;
+
+	ret = build_path(f, new_dirent, ino, devise_tempname, FITS_NEW,
+	    &new_path);
+	if (ret)
+		goto out;
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_LINK)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH_LINK, old_path)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, new_path)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+	if (f->f_pass == PASS_UNLINK)
+		ret = fits_send_mtime_update(f, new_dirent, ino);
+out:
+	if (old_dirent.fd_name)
+		kmem_free(old_dirent.fd_name, strlen(old_dirent.fd_name) + 1);
+	fits_path_free(old_path);
+	fits_path_free(new_path);
+	return (ret);
+}
+
+int
+fits_send_mkdir(fits_t *f, fits_dirent_t *dirent,
+		uint64_t ino, int devise_tempname)
+{
+	fits_path_t *path = NULL;
+	fits_info_t si;
+	int ret;
+
+	ret = build_path(f, dirent, ino, devise_tempname, FITS_NEW, &path);
+	if (ret)
+		goto out;
+
+	ret = fits_get_info(f, ino, FITS_NEW, &si,
+			    FI_ATTR_UID | FI_ATTR_GID | FI_ATTR_MODE);
+	if (ret)
+		goto out;
+
+	if (path->fp_total_len != 1) {
+		/* don't send an mkdir for the root, but send chown/chmod */
+		if ((ret = fits_cmd_start(f, FITS_CMD_MKDIR)) ||
+		    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+		    (ret = fits_send_put_u64(f, FITS_ATTR_INO, ino)) ||
+		    (ret = fits_cmd_send(f)))
+			goto out;
+	}
+
+	/* send CHOWN */
+	if ((ret = fits_cmd_start(f, FITS_CMD_CHOWN)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_UID, si.si_uid)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_GID, si.si_gid)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+	/* send CHMOD */
+	if ((ret = fits_cmd_start(f, FITS_CMD_CHMOD)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_MODE,
+	    si.si_mode & 0xfff)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	fits_path_free(path);
+	return (ret);
+}
+
+/* this one is only used for directory renames */
+int
+fits_send_rename(fits_t *f, fits_dirent_t *new_dirent, uint64_t ino,
+    uint64_t old_parent_ino, int devise_tempname)
+{
+	fits_path_t *new_path = NULL;
+	fits_path_t *old_path = NULL;
+	int ret;
+	fits_dirent_t old_dirent = {
+		.fd_name = NULL,
+		.fd_parent_ino = old_parent_ino,
+		.fd_prev = NULL,
+	};
+
+	ret = fits_find_entry(f, old_parent_ino, ino, FITS_OLD,
+	    &old_dirent.fd_name);
+	if (ret)
+		return (ret);
+	ret = build_path(f, &old_dirent, ino, 0, FITS_OLD, &old_path);
+	if (ret)
+		goto out;
+
+	ret = build_path(f, new_dirent, ino, devise_tempname, FITS_NEW,
+	    &new_path);
+	if (ret)
+		goto out;
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_RENAME)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, old_path)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH_TO, new_path)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+out:
+	fits_path_free(old_path);
+	fits_path_free(new_path);
+	return (ret);
+}
+
+int
+fits_send_rename_from_tempname(fits_t *f, fits_dirent_t *dirent,
+    uint64_t ino, uint64_t old)
+{
+	char buf[TEMPNAME_SIZE];
+	fits_path_t *new_path = NULL;
+	fits_path_t *old_path = NULL;
+	int ret;
+	fits_dirent_t old_dirent;
+
+	tempname(old, buf, sizeof (buf));
+	old_dirent = *dirent;
+	old_dirent.fd_name = buf;
+
+	ret = build_path(f, &old_dirent, old, 0, FITS_OLD, &old_path);
+	if (ret)
+		goto out;
+	ret = build_path(f, dirent, ino, 0, FITS_NEW, &new_path);
+	if (ret)
+		goto out;
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_RENAME)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, old_path)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH_TO, new_path)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+	ret = fits_send_mtime_update(f, dirent, old);
+
+out:
+	fits_path_free(old_path);
+	fits_path_free(new_path);
+	return (ret);
+}
+
+int
+fits_send_unlink(fits_t *f, fits_dirent_t *dirent, uint64_t ino)
+{
+	fits_path_t *path = NULL;
+	int ret;
+
+	ret = build_path(f, dirent, ino, 0, FITS_OLD, &path);
+	if (ret)
+		goto out;
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_UNLINK)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	fits_path_free(path);
+	return (ret);
+}
+
+int
+fits_send_rmdir(fits_t *f, fits_dirent_t *dirent, uint64_t ino)
+{
+	fits_path_t *path;
+	int ret;
+
+	ret = build_path(f, dirent, ino, 0, FITS_OLD, &path);
+	if (ret)
+		goto out;
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_RMDIR)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	fits_path_free(path);
+	return (ret);
+}
+
+int
+fits_send_file_data(fits_t *f, fits_path_t **path_p,
+		    fits_dirent_t *dirent, uint64_t ino,
+		    uint64_t off, uint64_t len, void *data)
+{
+	int ret = 0;
+
+	if (!*path_p) {
+		ret = build_path(f, dirent, ino, 0, FITS_NEW, path_p);
+		if (ret)
+			return (ret);
+	}
+
+	while (len) {
+		uint64_t l = MIN(len, FITS_SEND_READ_SIZE);
+
+		if ((ret = fits_cmd_start(f, FITS_CMD_WRITE)) ||
+		    (ret = put_path(f, FITS_ATTR_PATH, *path_p)) ||
+		    (ret = fits_send_put_u64(f, FITS_ATTR_FILE_OFFSET, off))||
+		    (ret = fits_send_put_attr(f, FITS_ATTR_DATA, data, l)) ||
+		    (ret = fits_cmd_send(f)))
+			goto out;
+		data += l;
+		off += l;
+		len -= l;
+	}
+
+out:
+	return (ret);
+}
+
+int
+fits_send_mtime_update(fits_t *f, fits_dirent_t *dirent, uint64_t ino)
+{
+	fits_path_t *path = NULL;
+	int ret;
+	fits_info_t si;
+
+	ret = fits_get_info(f, ino, FITS_NEW, &si,
+			    FI_ATTR_ATIME | FI_ATTR_MTIME |
+			    FI_ATTR_CTIME | FI_ATTR_OTIME);
+	if (ret) {
+		if (ret == ENOENT)
+			ret = 0;
+		goto out;
+	}
+
+	ret = build_path(f, dirent, ino, 0, FITS_NEW, &path);
+	if (ret)
+		goto out;
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_UTIMES)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_time(f, FITS_ATTR_ATIME, &si.si_atime)) ||
+	    (ret = fits_send_put_time(f, FITS_ATTR_MTIME, &si.si_mtime)) ||
+	    (ret = fits_send_put_time(f, FITS_ATTR_CTIME, &si.si_ctime)) ||
+	    (ret = fits_send_put_time(f, FITS_ATTR_OTIME, &si.si_otime)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	fits_path_free(path);
+	return (ret);
+}
+
+int
+fits_send_truncate(fits_t *f, fits_dirent_t *dirent, uint64_t ino,
+    uint64_t new_size)
+{
+	fits_path_t *path = NULL;
+	int ret;
+
+	ret = build_path(f, dirent, ino, 0, FITS_NEW, &path);
+	if (ret)
+		return (ret);
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_TRUNCATE)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_SIZE, new_size)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	fits_path_free(path);
+	return (ret);
+}
+
+int
+fits_send_chown(fits_t *f, fits_dirent_t *dirent, uint64_t ino,
+		    uint64_t new_uid, uint64_t new_gid)
+{
+	fits_path_t *path = NULL;
+	int ret;
+
+	ret = build_path(f, dirent, ino, 0, FITS_NEW, &path);
+	if (ret)
+		return (ret);
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_CHOWN)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_UID, new_uid)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_GID, new_gid)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	fits_path_free(path);
+	return (ret);
+}
+
+int
+fits_send_chmod(fits_t *f, fits_dirent_t *dirent, uint64_t ino,
+		    uint64_t new_mode)
+{
+	fits_path_t *path = NULL;
+	int ret;
+
+	ret = build_path(f, dirent, ino, 0, FITS_NEW, &path);
+	if (ret)
+		return (ret);
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_CHMOD)) ||
+	    (ret = put_path(f, FITS_ATTR_PATH, path)) ||
+	    (ret = fits_send_put_u64(f, FITS_ATTR_MODE, new_mode)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	fits_path_free(path);
+	return (ret);
+}
+
+int
+fits_send_end(fits_t *f)
+{
+	int ret;
+
+	if ((ret = fits_cmd_start(f, FITS_CMD_END)) ||
+	    (ret = fits_cmd_send(f)))
+		goto out;
+
+out:
+	return (ret);
+}
