xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_sa.c (revision 1b645c8fdbd648b7c64cdb03528d79b17a942acb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/vnode.h>
28 #include <sys/sa.h>
29 #include <sys/zfs_acl.h>
30 #include <sys/zfs_sa.h>
31 
32 /*
33  * ZPL attribute registration table.
34  * Order of attributes doesn't matter
35  * a unique value will be assigned for each
36  * attribute that is file system specific
37  *
38  * This is just the set of ZPL attributes that this
39  * version of ZFS deals with natively.  The file system
40  * could have other attributes stored in files, but they will be
41  * ignored.  The SA framework will preserve them, just that
42  * this version of ZFS won't change or delete them.
43  */
44 
45 sa_attr_reg_t zfs_attr_table[ZPL_END+1] = {
46 	{"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
47 	{"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
48 	{"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
49 	{"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
50 	{"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
51 	{"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
52 	{"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
53 	{"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
54 	{"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
55 	{"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
56 	{"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
57 	{"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
58 	{"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
59 	{"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
60 	{"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
61 	{"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
62 	{"ZPL_DACL_COUNT", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
63 	{"ZPL_SYMLINK", 0, SA_UINT8_ARRAY, 0},
64 	{"ZPL_SCANSTAMP", 32, SA_UINT8_ARRAY, 0},
65 	{"ZPL_DACL_ACES", 0, SA_ACL, 0},
66 	{NULL, 0, 0, 0}
67 };
68 
69 #ifdef _KERNEL
70 
71 int
72 zfs_sa_readlink(znode_t *zp, uio_t *uio)
73 {
74 	dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
75 	size_t bufsz;
76 	int error;
77 
78 	bufsz = zp->z_size;
79 	if (bufsz + ZFS_OLD_ZNODE_PHYS_SIZE <= db->db_size) {
80 		error = uiomove((caddr_t)db->db_data +
81 		    ZFS_OLD_ZNODE_PHYS_SIZE,
82 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
83 	} else {
84 		dmu_buf_t *dbp;
85 		if ((error = dmu_buf_hold(zp->z_zfsvfs->z_os, zp->z_id,
86 		    0, FTAG, &dbp, DMU_READ_NO_PREFETCH)) == 0) {
87 			error = uiomove(dbp->db_data,
88 			    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
89 			dmu_buf_rele(dbp, FTAG);
90 		}
91 	}
92 	return (error);
93 }
94 
95 void
96 zfs_sa_symlink(znode_t *zp, char *link, int len, dmu_tx_t *tx)
97 {
98 	dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
99 
100 	if (ZFS_OLD_ZNODE_PHYS_SIZE + len <= dmu_bonus_max()) {
101 		VERIFY(dmu_set_bonus(db,
102 		    len + ZFS_OLD_ZNODE_PHYS_SIZE, tx) == 0);
103 		if (len) {
104 			bcopy(link, (caddr_t)db->db_data +
105 			    ZFS_OLD_ZNODE_PHYS_SIZE, len);
106 		}
107 	} else {
108 		dmu_buf_t *dbp;
109 
110 		zfs_grow_blocksize(zp, len, tx);
111 		VERIFY(0 == dmu_buf_hold(zp->z_zfsvfs->z_os,
112 		    zp->z_id, 0, FTAG, &dbp, DMU_READ_NO_PREFETCH));
113 
114 		dmu_buf_will_dirty(dbp, tx);
115 
116 		ASSERT3U(len, <=, dbp->db_size);
117 		bcopy(link, dbp->db_data, len);
118 		dmu_buf_rele(dbp, FTAG);
119 	}
120 }
121 
122 void
123 zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
124 {
125 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
126 	xoptattr_t *xoap;
127 
128 	VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
129 	if (zp->z_is_sa) {
130 		if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
131 		    &xoap->xoa_av_scanstamp,
132 		    sizeof (xoap->xoa_av_scanstamp)) != 0)
133 			return;
134 	} else {
135 		dmu_object_info_t doi;
136 		dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
137 		int len;
138 
139 		if (!(zp->z_pflags & ZFS_BONUS_SCANSTAMP))
140 			return;
141 
142 		sa_object_info(zp->z_sa_hdl, &doi);
143 		len = sizeof (xoap->xoa_av_scanstamp) +
144 		    ZFS_OLD_ZNODE_PHYS_SIZE;
145 
146 		if (len <= doi.doi_bonus_size) {
147 			(void) memcpy(xoap->xoa_av_scanstamp,
148 			    (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
149 			    sizeof (xoap->xoa_av_scanstamp));
150 		}
151 	}
152 	XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
153 }
154 
155 void
156 zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
157 {
158 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
159 	xoptattr_t *xoap;
160 
161 	VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
162 	if (zp->z_is_sa)
163 		VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
164 		    &xoap->xoa_av_scanstamp,
165 		    sizeof (xoap->xoa_av_scanstamp), tx));
166 	else {
167 		dmu_object_info_t doi;
168 		dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
169 		int len;
170 
171 		sa_object_info(zp->z_sa_hdl, &doi);
172 		len = sizeof (xoap->xoa_av_scanstamp) +
173 		    ZFS_OLD_ZNODE_PHYS_SIZE;
174 		if (len > doi.doi_bonus_size)
175 			VERIFY(dmu_set_bonus(db, len, tx) == 0);
176 		(void) memcpy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
177 		    xoap->xoa_av_scanstamp, sizeof (xoap->xoa_av_scanstamp));
178 
179 		zp->z_pflags |= ZFS_BONUS_SCANSTAMP;
180 		VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
181 		    &zp->z_pflags, sizeof (uint64_t), tx));
182 	}
183 }
184 
185 /*
186  * I'm not convinced we should do any of this upgrade.
187  * since the SA code can read both old/new znode formats
188  * with probably little to know performance difference.
189  *
190  * All new files will be created with the new format.
191  */
192 
193 void
194 zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
195 {
196 	dmu_buf_t *db = sa_get_db(hdl);
197 	znode_t *zp = sa_get_userdata(hdl);
198 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
199 	sa_bulk_attr_t bulk[20];
200 	int count = 0;
201 	sa_bulk_attr_t sa_attrs[20] = { 0 };
202 	zfs_acl_locator_cb_t locate = { 0 };
203 	uint64_t uid, gid, mode, rdev, xattr, parent;
204 	uint64_t crtime[2], mtime[2], ctime[2];
205 	zfs_acl_phys_t znode_acl;
206 	char scanstamp[AV_SCANSTAMP_SZ];
207 
208 	/*
209 	 * No upgrade if ACL isn't cached
210 	 * since we won't know which locks are held
211 	 * and ready the ACL would require special "locked"
212 	 * interfaces that would be messy
213 	 */
214 	if (zp->z_acl_cached == NULL || ZTOV(zp)->v_type == VLNK)
215 		return;
216 
217 	/* First do a bulk query of the attributes that aren't cached */
218 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
219 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
220 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
221 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
222 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
223 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL, &xattr, 8);
224 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, &rdev, 8);
225 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
226 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
227 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
228 	    &znode_acl, 88);
229 
230 	if (sa_bulk_lookup_locked(hdl, bulk, count) != 0)
231 		return;
232 
233 
234 	/*
235 	 * While the order here doesn't matter its best to try and organize
236 	 * it is such a way to pick up an already existing layout number
237 	 */
238 	count = 0;
239 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
240 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
241 	    &zp->z_size, 8);
242 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zfsvfs),
243 	    NULL, &zp->z_gen, 8);
244 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
245 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
246 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zfsvfs),
247 	    NULL, &parent, 8);
248 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
249 	    &zp->z_pflags, 8);
250 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zfsvfs), NULL,
251 	    zp->z_atime, 16);
252 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zfsvfs), NULL,
253 	    &mtime, 16);
254 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zfsvfs), NULL,
255 	    &ctime, 16);
256 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
257 	    &crtime, 16);
258 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zfsvfs), NULL,
259 	    &zp->z_links, 8);
260 	if (zp->z_vnode->v_type == VBLK || zp->z_vnode->v_type == VCHR)
261 		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
262 		    &rdev, 8);
263 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
264 	    &zp->z_acl_cached->z_acl_count, 8);
265 
266 	if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
267 		zfs_acl_xform(zp, zp->z_acl_cached, CRED());
268 
269 	locate.cb_aclp = zp->z_acl_cached;
270 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
271 	    zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
272 	if (xattr)
273 		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs),
274 		    NULL, &rdev, 8);
275 
276 	/* if scanstamp then add scanstamp */
277 
278 	if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
279 		bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
280 		    scanstamp, AV_SCANSTAMP_SZ);
281 		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zfsvfs),
282 		    NULL, scanstamp, AV_SCANSTAMP_SZ);
283 		zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
284 	}
285 
286 	VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
287 	VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
288 	    count, tx) == 0);
289 	if (znode_acl.z_acl_extern_obj)
290 		VERIFY(0 == dmu_object_free(zfsvfs->z_os,
291 		    znode_acl.z_acl_extern_obj, tx));
292 
293 	zp->z_is_sa = B_TRUE;
294 }
295 
296 void
297 zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
298 {
299 	if (!zp->z_zfsvfs->z_use_sa || zp->z_is_sa)
300 		return;
301 
302 	ASSERT(!zp->z_is_sa);
303 
304 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
305 
306 	if (ZFS_EXTERNAL_ACL(zp)) {
307 		dmu_tx_hold_free(tx, ZFS_EXTERNAL_ACL(zp), 0,
308 		    DMU_OBJECT_END);
309 	}
310 }
311 
312 #endif
313