xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_sa.c (revision 7ae7577ce925f555e5e3410f14a3ccc896c842f2)
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/zfs_context.h>
26 #include <sys/vnode.h>
27 #include <sys/sa.h>
28 #include <sys/zfs_acl.h>
29 #include <sys/zfs_sa.h>
30 
31 /*
32  * ZPL attribute registration table.
33  * Order of attributes doesn't matter
34  * a unique value will be assigned for each
35  * attribute that is file system specific
36  *
37  * This is just the set of ZPL attributes that this
38  * version of ZFS deals with natively.  The file system
39  * could have other attributes stored in files, but they will be
40  * ignored.  The SA framework will preserve them, just that
41  * this version of ZFS won't change or delete them.
42  */
43 
44 sa_attr_reg_t zfs_attr_table[ZPL_END+1] = {
45 	{"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
46 	{"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
47 	{"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
48 	{"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
49 	{"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
50 	{"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
51 	{"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
52 	{"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
53 	{"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
54 	{"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
55 	{"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
56 	{"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
57 	{"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
58 	{"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
59 	{"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
60 	{"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
61 	{"ZPL_DACL_COUNT", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
62 	{"ZPL_SYMLINK", 0, SA_UINT8_ARRAY, 0},
63 	{"ZPL_SCANSTAMP", 32, SA_UINT8_ARRAY, 0},
64 	{"ZPL_DACL_ACES", 0, SA_ACL, 0},
65 	{NULL, 0, 0, 0}
66 };
67 
68 #ifdef _KERNEL
69 
70 int
71 zfs_sa_readlink(znode_t *zp, uio_t *uio)
72 {
73 	dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
74 	size_t bufsz;
75 	int error;
76 
77 	bufsz = zp->z_size;
78 	if (bufsz + ZFS_OLD_ZNODE_PHYS_SIZE <= db->db_size) {
79 		error = uiomove((caddr_t)db->db_data +
80 		    ZFS_OLD_ZNODE_PHYS_SIZE,
81 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
82 	} else {
83 		dmu_buf_t *dbp;
84 		if ((error = dmu_buf_hold(zp->z_zfsvfs->z_os, zp->z_id,
85 		    0, FTAG, &dbp, DMU_READ_NO_PREFETCH)) == 0) {
86 			error = uiomove(dbp->db_data,
87 			    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
88 			dmu_buf_rele(dbp, FTAG);
89 		}
90 	}
91 	return (error);
92 }
93 
94 void
95 zfs_sa_symlink(znode_t *zp, char *link, int len, dmu_tx_t *tx)
96 {
97 	dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
98 
99 	if (ZFS_OLD_ZNODE_PHYS_SIZE + len <= dmu_bonus_max()) {
100 		VERIFY0(dmu_set_bonus(db, len + ZFS_OLD_ZNODE_PHYS_SIZE, tx));
101 		if (len) {
102 			bcopy(link, (caddr_t)db->db_data +
103 			    ZFS_OLD_ZNODE_PHYS_SIZE, len);
104 		}
105 	} else {
106 		dmu_buf_t *dbp;
107 
108 		zfs_grow_blocksize(zp, len, tx);
109 		VERIFY(0 == dmu_buf_hold(zp->z_zfsvfs->z_os,
110 		    zp->z_id, 0, FTAG, &dbp, DMU_READ_NO_PREFETCH));
111 
112 		dmu_buf_will_dirty(dbp, tx);
113 
114 		ASSERT3U(len, <=, dbp->db_size);
115 		bcopy(link, dbp->db_data, len);
116 		dmu_buf_rele(dbp, FTAG);
117 	}
118 }
119 
120 void
121 zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
122 {
123 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
124 	xoptattr_t *xoap;
125 
126 	ASSERT(MUTEX_HELD(&zp->z_lock));
127 	VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
128 	if (zp->z_is_sa) {
129 		if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
130 		    &xoap->xoa_av_scanstamp,
131 		    sizeof (xoap->xoa_av_scanstamp)) != 0)
132 			return;
133 	} else {
134 		dmu_object_info_t doi;
135 		dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
136 		int len;
137 
138 		if (!(zp->z_pflags & ZFS_BONUS_SCANSTAMP))
139 			return;
140 
141 		sa_object_info(zp->z_sa_hdl, &doi);
142 		len = sizeof (xoap->xoa_av_scanstamp) +
143 		    ZFS_OLD_ZNODE_PHYS_SIZE;
144 
145 		if (len <= doi.doi_bonus_size) {
146 			(void) memcpy(xoap->xoa_av_scanstamp,
147 			    (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
148 			    sizeof (xoap->xoa_av_scanstamp));
149 		}
150 	}
151 	XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
152 }
153 
154 void
155 zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
156 {
157 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
158 	xoptattr_t *xoap;
159 
160 	ASSERT(MUTEX_HELD(&zp->z_lock));
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 no 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 	boolean_t drop_lock = B_FALSE;
208 
209 	/*
210 	 * No upgrade if ACL isn't cached
211 	 * since we won't know which locks are held
212 	 * and ready the ACL would require special "locked"
213 	 * interfaces that would be messy
214 	 */
215 	if (zp->z_acl_cached == NULL || ZTOV(zp)->v_type == VLNK)
216 		return;
217 
218 	/*
219 	 * If the z_lock is held and we aren't the owner
220 	 * the just return since we don't want to deadlock
221 	 * trying to update the status of z_is_sa.  This
222 	 * file can then be upgraded at a later time.
223 	 *
224 	 * Otherwise, we know we are doing the
225 	 * sa_update() that caused us to enter this function.
226 	 */
227 	if (mutex_owner(&zp->z_lock) != curthread) {
228 		if (mutex_tryenter(&zp->z_lock) == 0)
229 			return;
230 		else
231 			drop_lock = B_TRUE;
232 	}
233 
234 	/* First do a bulk query of the attributes that aren't cached */
235 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
236 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
237 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
238 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
239 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
240 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL, &xattr, 8);
241 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, &rdev, 8);
242 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
243 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
244 	SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
245 	    &znode_acl, 88);
246 
247 	if (sa_bulk_lookup_locked(hdl, bulk, count) != 0)
248 		goto done;
249 
250 
251 	/*
252 	 * While the order here doesn't matter its best to try and organize
253 	 * it is such a way to pick up an already existing layout number
254 	 */
255 	count = 0;
256 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
257 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
258 	    &zp->z_size, 8);
259 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zfsvfs),
260 	    NULL, &zp->z_gen, 8);
261 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
262 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
263 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zfsvfs),
264 	    NULL, &parent, 8);
265 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
266 	    &zp->z_pflags, 8);
267 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zfsvfs), NULL,
268 	    zp->z_atime, 16);
269 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zfsvfs), NULL,
270 	    &mtime, 16);
271 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zfsvfs), NULL,
272 	    &ctime, 16);
273 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
274 	    &crtime, 16);
275 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zfsvfs), NULL,
276 	    &zp->z_links, 8);
277 	if (zp->z_vnode->v_type == VBLK || zp->z_vnode->v_type == VCHR)
278 		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
279 		    &rdev, 8);
280 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
281 	    &zp->z_acl_cached->z_acl_count, 8);
282 
283 	if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
284 		zfs_acl_xform(zp, zp->z_acl_cached, CRED());
285 
286 	locate.cb_aclp = zp->z_acl_cached;
287 	SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
288 	    zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
289 
290 	if (xattr)
291 		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zfsvfs),
292 		    NULL, &xattr, 8);
293 
294 	/* if scanstamp then add scanstamp */
295 
296 	if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
297 		bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
298 		    scanstamp, AV_SCANSTAMP_SZ);
299 		SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zfsvfs),
300 		    NULL, scanstamp, AV_SCANSTAMP_SZ);
301 		zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
302 	}
303 
304 	VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
305 	VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
306 	    count, tx) == 0);
307 	if (znode_acl.z_acl_extern_obj)
308 		VERIFY(0 == dmu_object_free(zfsvfs->z_os,
309 		    znode_acl.z_acl_extern_obj, tx));
310 
311 	zp->z_is_sa = B_TRUE;
312 done:
313 	if (drop_lock)
314 		mutex_exit(&zp->z_lock);
315 }
316 
317 void
318 zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
319 {
320 	if (!zp->z_zfsvfs->z_use_sa || zp->z_is_sa)
321 		return;
322 
323 
324 	dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
325 
326 	if (zfs_external_acl(zp)) {
327 		dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
328 		    DMU_OBJECT_END);
329 	}
330 }
331 
332 #endif
333