10a586ceMark Shellenbaum/*
20a586ceMark Shellenbaum * CDDL HEADER START
30a586ceMark Shellenbaum *
40a586ceMark Shellenbaum * The contents of this file are subject to the terms of the
50a586ceMark Shellenbaum * Common Development and Distribution License (the "License").
60a586ceMark Shellenbaum * You may not use this file except in compliance with the License.
70a586ceMark Shellenbaum *
80a586ceMark Shellenbaum * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90a586ceMark Shellenbaum * or http://www.opensolaris.org/os/licensing.
100a586ceMark Shellenbaum * See the License for the specific language governing permissions
110a586ceMark Shellenbaum * and limitations under the License.
120a586ceMark Shellenbaum *
130a586ceMark Shellenbaum * When distributing Covered Code, include this CDDL HEADER in each
140a586ceMark Shellenbaum * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150a586ceMark Shellenbaum * If applicable, add the following below this CDDL HEADER, with the
160a586ceMark Shellenbaum * fields enclosed by brackets "[]" replaced with your own identifying
170a586ceMark Shellenbaum * information: Portions Copyright [yyyy] [name of copyright owner]
180a586ceMark Shellenbaum *
190a586ceMark Shellenbaum * CDDL HEADER END
200a586ceMark Shellenbaum */
210a586ceMark Shellenbaum/*
228f2529dMark Shellenbaum * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
230a586ceMark Shellenbaum */
240a586ceMark Shellenbaum
25bc9014eJustin Gibbs#include <sys/zfs_context.h>
260a586ceMark Shellenbaum#include <sys/vnode.h>
270a586ceMark Shellenbaum#include <sys/sa.h>
280a586ceMark Shellenbaum#include <sys/zfs_acl.h>
290a586ceMark Shellenbaum#include <sys/zfs_sa.h>
30f67950bNasf-Fan#include <sys/dmu_objset.h>
31f67950bNasf-Fan#include <sys/sa_impl.h>
320a586ceMark Shellenbaum
330a586ceMark Shellenbaum/*
340a586ceMark Shellenbaum * ZPL attribute registration table.
350a586ceMark Shellenbaum * Order of attributes doesn't matter
360a586ceMark Shellenbaum * a unique value will be assigned for each
370a586ceMark Shellenbaum * attribute that is file system specific
380a586ceMark Shellenbaum *
390a586ceMark Shellenbaum * This is just the set of ZPL attributes that this
400a586ceMark Shellenbaum * version of ZFS deals with natively.  The file system
410a586ceMark Shellenbaum * could have other attributes stored in files, but they will be
420a586ceMark Shellenbaum * ignored.  The SA framework will preserve them, just that
430a586ceMark Shellenbaum * this version of ZFS won't change or delete them.
440a586ceMark Shellenbaum */
450a586ceMark Shellenbaum
460a586ceMark Shellenbaumsa_attr_reg_t zfs_attr_table[ZPL_END+1] = {
470a586ceMark Shellenbaum	{"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
480a586ceMark Shellenbaum	{"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
490a586ceMark Shellenbaum	{"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
500a586ceMark Shellenbaum	{"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
510a586ceMark Shellenbaum	{"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
520a586ceMark Shellenbaum	{"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
530a586ceMark Shellenbaum	{"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
540a586ceMark Shellenbaum	{"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
550a586ceMark Shellenbaum	{"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
560a586ceMark Shellenbaum	{"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
570a586ceMark Shellenbaum	{"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
580a586ceMark Shellenbaum	{"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
590a586ceMark Shellenbaum	{"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
600a586ceMark Shellenbaum	{"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
610a586ceMark Shellenbaum	{"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
620a586ceMark Shellenbaum	{"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
630a586ceMark Shellenbaum	{"ZPL_DACL_COUNT", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
640a586ceMark Shellenbaum	{"ZPL_SYMLINK", 0, SA_UINT8_ARRAY, 0},
650a586ceMark Shellenbaum	{"ZPL_SCANSTAMP", 32, SA_UINT8_ARRAY, 0},
660a586ceMark Shellenbaum	{"ZPL_DACL_ACES", 0, SA_ACL, 0},
67f67950bNasf-Fan	{"ZPL_PROJID", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
680a586ceMark Shellenbaum	{NULL, 0, 0, 0}
690a586ceMark Shellenbaum};
700a586ceMark Shellenbaum
710a586ceMark Shellenbaum#ifdef _KERNEL
720a586ceMark Shellenbaum
730a586ceMark Shellenbaumint
740a586ceMark Shellenbaumzfs_sa_readlink(znode_t *zp, uio_t *uio)
750a586ceMark Shellenbaum{
760a586ceMark Shellenbaum	dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
770a586ceMark Shellenbaum	size_t bufsz;
780a586ceMark Shellenbaum	int error;
790a586ceMark Shellenbaum
800a586ceMark Shellenbaum	bufsz = zp->z_size;
81faba5caMark Shellenbaum	if (bufsz + ZFS_OLD_ZNODE_PHYS_SIZE <= db->db_size) {
820a586ceMark Shellenbaum		error = uiomove((caddr_t)db->db_data +
830a586ceMark Shellenbaum		    ZFS_OLD_ZNODE_PHYS_SIZE,
840a586ceMark Shellenbaum		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
850a586ceMark Shellenbaum	} else {
860a586ceMark Shellenbaum		dmu_buf_t *dbp;
870a586ceMark Shellenbaum		if ((error = dmu_buf_hold(zp->z_zfsvfs->z_os, zp->z_id,
8847cb52dJeff Bonwick		    0, FTAG, &dbp, DMU_READ_NO_PREFETCH)) == 0) {
890a586ceMark Shellenbaum			error = uiomove(dbp->db_data,
900a586ceMark Shellenbaum			    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
910a586ceMark Shellenbaum			dmu_buf_rele(dbp, FTAG);
920a586ceMark Shellenbaum		}
930a586ceMark Shellenbaum	}
940a586ceMark Shellenbaum	return (error);
950a586ceMark Shellenbaum}
960a586ceMark Shellenbaum
970a586ceMark Shellenbaumvoid
980a586ceMark Shellenbaumzfs_sa_symlink(znode_t *zp, char *link, int len, dmu_tx_t *tx)
990a586ceMark Shellenbaum{
1000a586ceMark Shellenbaum	dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
1010a586ceMark Shellenbaum
1020a586ceMark Shellenbaum	if (ZFS_OLD_ZNODE_PHYS_SIZE + len <= dmu_bonus_max()) {
10354811daToomas Soome		VERIFY0(dmu_set_bonus(db, len + ZFS_OLD_ZNODE_PHYS_SIZE, tx));
1040a586ceMark Shellenbaum		if (len) {
1050a586ceMark Shellenbaum			bcopy(link, (caddr_t)db->db_data +
1060a586ceMark Shellenbaum			    ZFS_OLD_ZNODE_PHYS_SIZE, len);
1070a586ceMark Shellenbaum		}
1080a586ceMark Shellenbaum	} else {
1090a586ceMark Shellenbaum		dmu_buf_t *dbp;
1100a586ceMark Shellenbaum
1110a586ceMark Shellenbaum		zfs_grow_blocksize(zp, len, tx);
1120a586ceMark Shellenbaum		VERIFY(0 == dmu_buf_hold(zp->z_zfsvfs->z_os,
11347cb52dJeff Bonwick		    zp->z_id, 0, FTAG, &dbp, DMU_READ_NO_PREFETCH));
1140a586ceMark Shellenbaum
1150a586ceMark Shellenbaum		dmu_buf_will_dirty(dbp, tx);
1160a586ceMark Shellenbaum
1170a586ceMark Shellenbaum		ASSERT3U(len, <=, dbp->db_size);
1180a586ceMark Shellenbaum		bcopy(link, dbp->db_data, len);
1190a586ceMark Shellenbaum		dmu_buf_rele(dbp, FTAG);
1200a586ceMark Shellenbaum	}
1210a586ceMark Shellenbaum}
1220a586ceMark Shellenbaum
1230a586ceMark Shellenbaumvoid
1240a586ceMark Shellenbaumzfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
1250a586ceMark Shellenbaum{
1260a586ceMark Shellenbaum	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1270a586ceMark Shellenbaum	xoptattr_t *xoap;
1280a586ceMark Shellenbaum
1291412a1aMark Shellenbaum	ASSERT(MUTEX_HELD(&zp->z_lock));
1300a586ceMark Shellenbaum	VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
1310a586ceMark Shellenbaum	if (zp->z_is_sa) {
1320a586ceMark Shellenbaum		if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
1330a586ceMark Shellenbaum		    &xoap->xoa_av_scanstamp,
1340a586ceMark Shellenbaum		    sizeof (xoap->xoa_av_scanstamp)) != 0)
1350a586ceMark Shellenbaum			return;
1360a586ceMark Shellenbaum	} else {
1370a586ceMark Shellenbaum		dmu_object_info_t doi;
1380a586ceMark Shellenbaum		dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
1390a586ceMark Shellenbaum		int len;
1400a586ceMark Shellenbaum
1410a586ceMark Shellenbaum		if (!(zp->z_pflags & ZFS_BONUS_SCANSTAMP))
1420a586ceMark Shellenbaum			return;
1430a586ceMark Shellenbaum
1440a586ceMark Shellenbaum		sa_object_info(zp->z_sa_hdl, &doi);
1450a586ceMark Shellenbaum		len = sizeof (xoap->xoa_av_scanstamp) +
1460a586ceMark Shellenbaum		    ZFS_OLD_ZNODE_PHYS_SIZE;
1470a586ceMark Shellenbaum
1480a586ceMark Shellenbaum		if (len <= doi.doi_bonus_size) {
1490a586ceMark Shellenbaum			(void) memcpy(xoap->xoa_av_scanstamp,
1500a586ceMark Shellenbaum			    (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
1510a586ceMark Shellenbaum			    sizeof (xoap->xoa_av_scanstamp));
1520a586ceMark Shellenbaum		}
1530a586ceMark Shellenbaum	}
1540a586ceMark Shellenbaum	XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1550a586ceMark Shellenbaum}
1560a586ceMark Shellenbaum
1570a586ceMark Shellenbaumvoid
1580a586ceMark Shellenbaumzfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1590a586ceMark Shellenbaum{
1600a586ceMark Shellenbaum	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1610a586ceMark Shellenbaum	xoptattr_t *xoap;
1620a586ceMark Shellenbaum
1631412a1aMark Shellenbaum	ASSERT(MUTEX_HELD(&zp->z_lock));
1640a586ceMark Shellenbaum	VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
1650a586ceMark Shellenbaum	if (zp->z_is_sa)
1660a586ceMark Shellenbaum		VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
1670a586ceMark Shellenbaum		    &xoap->xoa_av_scanstamp,
1680a586ceMark Shellenbaum		    sizeof (xoap->xoa_av_scanstamp), tx));
1690a586ceMark Shellenbaum	else {
1700a586ceMark Shellenbaum		dmu_object_info_t doi;
1710a586ceMark Shellenbaum		dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
1720a586ceMark Shellenbaum		int len;
1730a586ceMark Shellenbaum
1740a586ceMark Shellenbaum		sa_object_info(zp->z_sa_hdl, &doi);
1750a586ceMark Shellenbaum		len = sizeof (xoap->xoa_av_scanstamp) +
1760a586ceMark Shellenbaum		    ZFS_OLD_ZNODE_PHYS_SIZE;
1770a586ceMark Shellenbaum		if (len > doi.doi_bonus_size)
1780a586ceMark Shellenbaum			VERIFY(dmu_set_bonus(db, len, tx) == 0);
1790a586ceMark Shellenbaum		(void) memcpy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
1800a586ceMark Shellenbaum		    xoap->xoa_av_scanstamp, sizeof (xoap->xoa_av_scanstamp));
1810a586ceMark Shellenbaum
1820a586ceMark Shellenbaum		zp->z_pflags |= ZFS_BONUS_SCANSTAMP;
1830a586ceMark Shellenbaum		VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
1840a586ceMark Shellenbaum		    &zp->z_pflags, sizeof (uint64_t), tx));
1850a586ceMark Shellenbaum	}
1860a586ceMark Shellenbaum}
1870a586ceMark Shellenbaum
1880a586ceMark Shellenbaum/*
1890a586ceMark Shellenbaum * I'm not convinced we should do any of this upgrade.
1900a586ceMark Shellenbaum * since the SA code can read both old/new znode formats
191f717074Will Andrews * with probably little to no performance difference.
1920a586ceMark Shellenbaum *
1930a586ceMark Shellenbaum * All new files will be created with the new format.
1940a586ceMark Shellenbaum */
1950a586ceMark Shellenbaum
1960a586ceMark Shellenbaumvoid
1970a586ceMark Shellenbaumzfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
1980a586ceMark Shellenbaum{
1990a586ceMark Shellenbaum	dmu_buf_t *db = sa_get_db(hdl);
2000a586ceMark Shellenbaum	znode_t *zp = sa_get_userdata(hdl);
2010a586ceMark Shellenbaum	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
202f67950bNasf-Fan	sa_bulk_attr_t bulk[22];
2030a586ceMark Shellenbaum	int count = 0;
2040a586ceMark Shellenbaum	sa_bulk_attr_t sa_attrs[20] = { 0 };
2050a586ceMark Shellenbaum	zfs_acl_locator_cb_t locate = { 0 };
2060a586ceMark Shellenbaum	uint64_t uid, gid, mode, rdev, xattr, parent;
2070a586ceMark Shellenbaum	uint64_t crtime[2], mtime[2], ctime[2];
2080a586ceMark Shellenbaum	zfs_acl_phys_t znode_acl;
2090a586ceMark Shellenbaum	char scanstamp[AV_SCANSTAMP_SZ];
2101412a1aMark Shellenbaum	boolean_t drop_lock = B_FALSE;
2110a586ceMark Shellenbaum
2120a586ceMark Shellenbaum	/*
2130a586ceMark Shellenbaum	 * No upgrade if ACL isn't cached
2140a586ceMark Shellenbaum	 * since we won't know which locks are held
2150a586ceMark Shellenbaum	 * and ready the ACL would require special "locked"
2160a586ceMark Shellenbaum	 * interfaces that would be messy
2170a586ceMark Shellenbaum	 */
21806e0070Mark Shellenbaum	if (zp->z_acl_cached == NULL || ZTOV(zp)->v_type == VLNK)
2190a586ceMark Shellenbaum		return;
2200a586ceMark Shellenbaum
2211412a1aMark Shellenbaum	/*
2221412a1aMark Shellenbaum	 * If the z_lock is held and we aren't the owner
2231412a1aMark Shellenbaum	 * the just return since we don't want to deadlock
2241412a1aMark Shellenbaum	 * trying to update the status of z_is_sa.  This
2251412a1aMark Shellenbaum	 * file can then be upgraded at a later time.
2261412a1aMark Shellenbaum	 *
2271412a1aMark Shellenbaum	 * Otherwise, we know we are doing the
2281412a1aMark Shellenbaum	 * sa_update() that caused us to enter this function.
2291412a1aMark Shellenbaum	 */
2301412a1aMark Shellenbaum	if (mutex_owner(&zp->z_lock) != curthread) {
2311412a1aMark Shellenbaum		if (mutex_tryenter(&zp->z_lock) == 0)
2321412a1aMark Shellenbaum			return;
2331412a1aMark Shellenbaum		else
2341412a1aMark Shellenbaum			drop_lock = B_TRUE;
2351412a1aMark Shellenbaum	}
2361412a1aMark Shellenbaum
2370a586ceMark Shellenbaum	/* First do a bulk query of the attributes that aren't cached */
2380a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2390a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2400a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
2410a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
2420a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
2430a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL, &xattr, 8);
2440a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, &rdev, 8);
2450a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
2460a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
2470a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
2480a586ceMark Shellenbaum	    &znode_acl, 88);
2490a586ceMark Shellenbaum
2500a586ceMark Shellenbaum	if (sa_bulk_lookup_locked(hdl, bulk, count) != 0)
2511412a1aMark Shellenbaum		goto done;
2520a586ceMark Shellenbaum
253f67950bNasf-Fan	if (dmu_objset_projectquota_enabled(hdl->sa_os) &&
254f67950bNasf-Fan	    !(zp->z_pflags & ZFS_PROJID)) {
255f67950bNasf-Fan		zp->z_pflags |= ZFS_PROJID;
256f67950bNasf-Fan		zp->z_projid = ZFS_DEFAULT_PROJID;
257f67950bNasf-Fan	}
2580a586ceMark Shellenbaum
2590a586ceMark Shellenbaum	/*
2600a586ceMark Shellenbaum	 * While the order here doesn't matter its best to try and organize
2610a586ceMark Shellenbaum	 * it is such a way to pick up an already existing layout number
2620a586ceMark Shellenbaum	 */
2630a586ceMark Shellenbaum	count = 0;
2640a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
2650a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
2660a586ceMark Shellenbaum	    &zp->z_size, 8);
2670a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zfsvfs),
2680a586ceMark Shellenbaum	    NULL, &zp->z_gen, 8);
2690a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
2700a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
2710a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zfsvfs),
2720a586ceMark Shellenbaum	    NULL, &parent, 8);
2730a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
2740a586ceMark Shellenbaum	    &zp->z_pflags, 8);
2750a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zfsvfs), NULL,
2760a586ceMark Shellenbaum	    zp->z_atime, 16);
2770a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zfsvfs), NULL,
2780a586ceMark Shellenbaum	    &mtime, 16);
2790a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zfsvfs), NULL,
2800a586ceMark Shellenbaum	    &ctime, 16);
2810a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
2820a586ceMark Shellenbaum	    &crtime, 16);
2830a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zfsvfs), NULL,
2840a586ceMark Shellenbaum	    &zp->z_links, 8);
285f67950bNasf-Fan	if (dmu_objset_projectquota_enabled(hdl->sa_os))
286f67950bNasf-Fan		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PROJID(zfsvfs), NULL,
287f67950bNasf-Fan		    &zp->z_projid, 8);
2880a586ceMark Shellenbaum	if (zp->z_vnode->v_type == VBLK || zp->z_vnode->v_type == VCHR)
2890a586ceMark Shellenbaum		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
2900a586ceMark Shellenbaum		    &rdev, 8);
2910a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
2920a586ceMark Shellenbaum	    &zp->z_acl_cached->z_acl_count, 8);
2930a586ceMark Shellenbaum
2940a586ceMark Shellenbaum	if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
2950a586ceMark Shellenbaum		zfs_acl_xform(zp, zp->z_acl_cached, CRED());
2960a586ceMark Shellenbaum
2970a586ceMark Shellenbaum	locate.cb_aclp = zp->z_acl_cached;
2980a586ceMark Shellenbaum	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
2990a586ceMark Shellenbaum	    zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
3001412a1aMark Shellenbaum
3010a586ceMark Shellenbaum	if (xattr)
3021412a1aMark Shellenbaum		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zfsvfs),
3031412a1aMark Shellenbaum		    NULL, &xattr, 8);
3040a586ceMark Shellenbaum
3050a586ceMark Shellenbaum	/* if scanstamp then add scanstamp */
3060a586ceMark Shellenbaum
3070a586ceMark Shellenbaum	if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
3080a586ceMark Shellenbaum		bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
3090a586ceMark Shellenbaum		    scanstamp, AV_SCANSTAMP_SZ);
3100a586ceMark Shellenbaum		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zfsvfs),
3110a586ceMark Shellenbaum		    NULL, scanstamp, AV_SCANSTAMP_SZ);
3120a586ceMark Shellenbaum		zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
3130a586ceMark Shellenbaum	}
3140a586ceMark Shellenbaum
3150a586ceMark Shellenbaum	VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
3160a586ceMark Shellenbaum	VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
3170a586ceMark Shellenbaum	    count, tx) == 0);
3180a586ceMark Shellenbaum	if (znode_acl.z_acl_extern_obj)
3190a586ceMark Shellenbaum		VERIFY(0 == dmu_object_free(zfsvfs->z_os,
3200a586ceMark Shellenbaum		    znode_acl.z_acl_extern_obj, tx));
3210a586ceMark Shellenbaum
3220a586ceMark Shellenbaum	zp->z_is_sa = B_TRUE;
3231412a1aMark Shellenbaumdone:
3241412a1aMark Shellenbaum	if (drop_lock)
3251412a1aMark Shellenbaum		mutex_exit(&zp->z_lock);
3260a586ceMark Shellenbaum}
3270a586ceMark Shellenbaum
3280a586ceMark Shellenbaumvoid
3290a586ceMark Shellenbaumzfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
3300a586ceMark Shellenbaum{
3310a586ceMark Shellenbaum	if (!zp->z_zfsvfs->z_use_sa || zp->z_is_sa)
3320a586ceMark Shellenbaum		return;
3330a586ceMark Shellenbaum
3340a586ceMark Shellenbaum
3350a586ceMark Shellenbaum	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3360a586ceMark Shellenbaum
3371412a1aMark Shellenbaum	if (zfs_external_acl(zp)) {
3381412a1aMark Shellenbaum		dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
3390a586ceMark Shellenbaum		    DMU_OBJECT_END);
3400a586ceMark Shellenbaum	}
3410a586ceMark Shellenbaum}
3420a586ceMark Shellenbaum
3430a586ceMark Shellenbaum#endif
344