1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
14 */
15
16 #include <sys/zfs_context.h>
17 #include <sys/vnode.h>
18 #include <sys/sa.h>
19 #include <sys/zfs_acl.h>
20 #include <sys/zfs_sa.h>
21 #include <sys/dmu_objset.h>
22 #include <sys/sa_impl.h>
23 #include <sys/zfeature.h>
24
25 /*
26 * ZPL attribute registration table.
27 * Order of attributes doesn't matter
28 * a unique value will be assigned for each
29 * attribute that is file system specific
30 *
31 * This is just the set of ZPL attributes that this
32 * version of ZFS deals with natively. The file system
33 * could have other attributes stored in files, but they will be
34 * ignored. The SA framework will preserve them, just that
35 * this version of ZFS won't change or delete them.
36 */
37
38 const sa_attr_reg_t zfs_attr_table[ZPL_END+1] = {
39 {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
40 {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
41 {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
42 {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
43 {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
44 {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
45 {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
46 {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
47 {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
48 {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
49 {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
50 {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
51 {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
52 {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
53 {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
54 {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
55 {"ZPL_DACL_COUNT", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
56 {"ZPL_SYMLINK", 0, SA_UINT8_ARRAY, 0},
57 {"ZPL_SCANSTAMP", 32, SA_UINT8_ARRAY, 0},
58 {"ZPL_DACL_ACES", 0, SA_ACL, 0},
59 {"ZPL_DXATTR", 0, SA_UINT8_ARRAY, 0},
60 {"ZPL_PROJID", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
61 {"ZPL_SEQ", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
62 {NULL, 0, 0, 0}
63 };
64
65
66 #ifdef _KERNEL
67 static int zfs_zil_saxattr = 1;
68
69 int
zfs_sa_readlink(znode_t * zp,zfs_uio_t * uio)70 zfs_sa_readlink(znode_t *zp, zfs_uio_t *uio)
71 {
72 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
73 size_t bufsz;
74 int error;
75
76 bufsz = zp->z_size;
77 if (bufsz + ZFS_OLD_ZNODE_PHYS_SIZE <= db->db_size) {
78 error = zfs_uiomove((caddr_t)db->db_data +
79 ZFS_OLD_ZNODE_PHYS_SIZE,
80 MIN((size_t)bufsz, zfs_uio_resid(uio)), UIO_READ, uio);
81 } else {
82 dmu_buf_t *dbp;
83 if ((error = dmu_buf_hold(ZTOZSB(zp)->z_os, zp->z_id,
84 0, FTAG, &dbp, DMU_READ_NO_PREFETCH)) == 0) {
85 error = zfs_uiomove(dbp->db_data,
86 MIN((size_t)bufsz, zfs_uio_resid(uio)), UIO_READ,
87 uio);
88 dmu_buf_rele(dbp, FTAG);
89 }
90 }
91 return (error);
92 }
93
94 void
zfs_sa_symlink(znode_t * zp,char * link,int len,dmu_tx_t * tx)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 memcpy((caddr_t)db->db_data +
103 ZFS_OLD_ZNODE_PHYS_SIZE, link, len);
104 }
105 } else {
106 dmu_buf_t *dbp;
107
108 zfs_grow_blocksize(zp, len, tx);
109 VERIFY0(dmu_buf_hold(ZTOZSB(zp)->z_os, zp->z_id, 0, FTAG, &dbp,
110 DMU_READ_NO_PREFETCH));
111
112 dmu_buf_will_dirty(dbp, tx);
113
114 ASSERT3U(len, <=, dbp->db_size);
115 memcpy(dbp->db_data, link, len);
116 dmu_buf_rele(dbp, FTAG);
117 }
118 }
119
120 void
zfs_sa_get_scanstamp(znode_t * zp,xvattr_t * xvap)121 zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
122 {
123 zfsvfs_t *zfsvfs = ZTOZSB(zp);
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
zfs_sa_set_scanstamp(znode_t * zp,xvattr_t * xvap,dmu_tx_t * tx)155 zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
156 {
157 zfsvfs_t *zfsvfs = ZTOZSB(zp);
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 VERIFY0(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 VERIFY0(dmu_set_bonus(db, len, tx));
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 VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
181 &zp->z_pflags, sizeof (uint64_t), tx));
182 }
183 }
184
185 int
zfs_sa_get_xattr(znode_t * zp)186 zfs_sa_get_xattr(znode_t *zp)
187 {
188 zfsvfs_t *zfsvfs = ZTOZSB(zp);
189 char *obj;
190 int size;
191 int error;
192
193 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
194 ASSERT(!zp->z_xattr_cached);
195 ASSERT(zp->z_is_sa);
196
197 error = sa_size(zp->z_sa_hdl, SA_ZPL_DXATTR(zfsvfs), &size);
198 if (error) {
199 if (error == ENOENT)
200 return nvlist_alloc(&zp->z_xattr_cached,
201 NV_UNIQUE_NAME, KM_SLEEP);
202 else
203 return (error);
204 }
205
206 obj = vmem_alloc(size, KM_SLEEP);
207
208 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DXATTR(zfsvfs), obj, size);
209 if (error == 0)
210 error = nvlist_unpack(obj, size, &zp->z_xattr_cached, KM_SLEEP);
211
212 vmem_free(obj, size);
213
214 return (error);
215 }
216
217 int
zfs_sa_set_xattr(znode_t * zp,const char * name,const void * value,size_t vsize)218 zfs_sa_set_xattr(znode_t *zp, const char *name, const void *value, size_t vsize)
219 {
220 zfsvfs_t *zfsvfs = ZTOZSB(zp);
221 zilog_t *zilog;
222 dmu_tx_t *tx;
223 char *obj;
224 size_t size;
225 int error, logsaxattr = 0;
226
227 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
228 ASSERT(zp->z_xattr_cached);
229 ASSERT(zp->z_is_sa);
230
231 error = nvlist_size(zp->z_xattr_cached, &size, NV_ENCODE_XDR);
232 if ((error == 0) && (size > SA_ATTR_MAX_LEN))
233 error = SET_ERROR(EFBIG);
234 if (error)
235 goto out;
236
237 obj = vmem_alloc(size, KM_SLEEP);
238
239 error = nvlist_pack(zp->z_xattr_cached, &obj, &size,
240 NV_ENCODE_XDR, KM_SLEEP);
241 if (error)
242 goto out_free;
243
244 zilog = zfsvfs->z_log;
245
246 /*
247 * Users enable ZIL logging of xattr=sa operations by enabling the
248 * SPA_FEATURE_ZILSAXATTR feature on the pool. Feature is activated
249 * during zil_process_commit_list/zil_create, if enabled.
250 */
251 if (spa_feature_is_enabled(zfsvfs->z_os->os_spa,
252 SPA_FEATURE_ZILSAXATTR) && zfs_zil_saxattr)
253 logsaxattr = 1;
254
255 tx = dmu_tx_create(zfsvfs->z_os);
256 dmu_tx_hold_sa_create(tx, size);
257 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
258
259 error = dmu_tx_assign(tx, DMU_TX_WAIT);
260 if (error) {
261 dmu_tx_abort(tx);
262 } else {
263 int count = 0;
264 sa_bulk_attr_t bulk[3];
265 uint64_t ctime[2];
266
267 if (logsaxattr)
268 zfs_log_setsaxattr(zilog, tx, TX_SETSAXATTR, zp, name,
269 value, vsize);
270
271 zfs_tstamp_update_setup(zp, STATE_CHANGED, NULL, ctime);
272 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DXATTR(zfsvfs),
273 NULL, obj, size);
274 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
275 NULL, &ctime, 16);
276 ZFS_PERSIST_SEQ(zp, bulk, count);
277 ASSERT3S(count, <=, ARRAY_SIZE(bulk));
278 VERIFY0(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx));
279
280 dmu_tx_commit(tx);
281 if (logsaxattr && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
282 error = zil_commit(zilog, 0);
283 }
284 out_free:
285 vmem_free(obj, size);
286 out:
287 return (error);
288 }
289
290 /*
291 * I'm not convinced we should do any of this upgrade.
292 * since the SA code can read both old/new znode formats
293 * with probably little to no performance difference.
294 *
295 * All new files will be created with the new format.
296 */
297
298 void
zfs_sa_upgrade(sa_handle_t * hdl,dmu_tx_t * tx)299 zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
300 {
301 dmu_buf_t *db = sa_get_db(hdl);
302 znode_t *zp = sa_get_userdata(hdl);
303 zfsvfs_t *zfsvfs = ZTOZSB(zp);
304 int count = 0;
305 sa_bulk_attr_t *bulk, *sa_attrs;
306 zfs_acl_locator_cb_t locate = { 0 };
307 uint64_t uid, gid, mode, rdev, xattr, parent, tmp_gen;
308 uint64_t crtime[2], mtime[2], ctime[2], atime[2];
309 uint64_t links;
310 zfs_acl_phys_t znode_acl;
311 char scanstamp[AV_SCANSTAMP_SZ];
312 boolean_t drop_lock = B_FALSE;
313
314 /*
315 * No upgrade if ACL isn't cached
316 * since we won't know which locks are held
317 * and ready the ACL would require special "locked"
318 * interfaces that would be messy
319 */
320 if (zp->z_acl_cached == NULL || Z_ISLNK(ZTOTYPE(zp)))
321 return;
322
323 /*
324 * If the z_lock is held and we aren't the owner
325 * the just return since we don't want to deadlock
326 * trying to update the status of z_is_sa. This
327 * file can then be upgraded at a later time.
328 *
329 * Otherwise, we know we are doing the
330 * sa_update() that caused us to enter this function.
331 */
332 if (MUTEX_NOT_HELD(&zp->z_lock)) {
333 if (mutex_tryenter(&zp->z_lock) == 0)
334 return;
335 else
336 drop_lock = B_TRUE;
337 }
338
339 /* First do a bulk query of the attributes that aren't cached */
340 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
341 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
342 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
343 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
344 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
345 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
346 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
347 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL, &xattr, 8);
348 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, &rdev, 8);
349 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
350 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
351 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &tmp_gen, 8);
352 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
353 &znode_acl, 88);
354
355 if (sa_bulk_lookup_locked(hdl, bulk, count) != 0)
356 goto done;
357
358 if (dmu_objset_projectquota_enabled(hdl->sa_os) &&
359 !(zp->z_pflags & ZFS_PROJID)) {
360 zp->z_pflags |= ZFS_PROJID;
361 zp->z_projid = ZFS_DEFAULT_PROJID;
362 }
363
364 /*
365 * While the order here doesn't matter its best to try and organize
366 * it is such a way to pick up an already existing layout number
367 */
368 count = 0;
369 sa_attrs = kmem_zalloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
370 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
371 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
372 &zp->z_size, 8);
373 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zfsvfs),
374 NULL, &tmp_gen, 8);
375 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
376 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
377 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zfsvfs),
378 NULL, &parent, 8);
379 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
380 &zp->z_pflags, 8);
381 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zfsvfs), NULL,
382 &atime, 16);
383 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zfsvfs), NULL,
384 &mtime, 16);
385 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zfsvfs), NULL,
386 &ctime, 16);
387 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
388 &crtime, 16);
389 links = ZTONLNK(zp);
390 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zfsvfs), NULL,
391 &links, 8);
392 if (dmu_objset_projectquota_enabled(hdl->sa_os))
393 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PROJID(zfsvfs), NULL,
394 &zp->z_projid, 8);
395 if (Z_ISBLK(ZTOTYPE(zp)) || Z_ISCHR(ZTOTYPE(zp)))
396 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
397 &rdev, 8);
398 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
399 &zp->z_acl_cached->z_acl_count, 8);
400
401 if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
402 zfs_acl_xform(zp, zp->z_acl_cached, CRED());
403
404 locate.cb_aclp = zp->z_acl_cached;
405 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
406 zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
407
408 if (xattr)
409 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zfsvfs),
410 NULL, &xattr, 8);
411
412 /* if scanstamp then add scanstamp */
413
414 if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
415 memcpy(scanstamp,
416 (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
417 AV_SCANSTAMP_SZ);
418 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zfsvfs),
419 NULL, scanstamp, AV_SCANSTAMP_SZ);
420 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
421 }
422
423 VERIFY0(dmu_set_bonustype(db, DMU_OT_SA, tx));
424 VERIFY0(sa_replace_all_by_template_locked(hdl, sa_attrs, count, tx));
425 if (znode_acl.z_acl_extern_obj)
426 VERIFY0(dmu_object_free(zfsvfs->z_os,
427 znode_acl.z_acl_extern_obj, tx));
428
429 zp->z_is_sa = B_TRUE;
430 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
431 done:
432 kmem_free(bulk, sizeof (sa_bulk_attr_t) * ZPL_END);
433 if (drop_lock)
434 mutex_exit(&zp->z_lock);
435 }
436
437 void
zfs_sa_upgrade_txholds(dmu_tx_t * tx,znode_t * zp)438 zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
439 {
440 if (!ZTOZSB(zp)->z_use_sa || zp->z_is_sa)
441 return;
442
443
444 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
445
446 if (zfs_external_acl(zp)) {
447 dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
448 DMU_OBJECT_END);
449 }
450 }
451
452 ZFS_MODULE_PARAM(zfs, zfs_, zil_saxattr, INT, ZMOD_RW,
453 "Disable xattr=sa extended attribute logging in ZIL by settng 0.");
454
455 EXPORT_SYMBOL(zfs_attr_table);
456 EXPORT_SYMBOL(zfs_sa_readlink);
457 EXPORT_SYMBOL(zfs_sa_symlink);
458 EXPORT_SYMBOL(zfs_sa_get_scanstamp);
459 EXPORT_SYMBOL(zfs_sa_set_scanstamp);
460 EXPORT_SYMBOL(zfs_sa_get_xattr);
461 EXPORT_SYMBOL(zfs_sa_set_xattr);
462 EXPORT_SYMBOL(zfs_sa_upgrade);
463 EXPORT_SYMBOL(zfs_sa_upgrade_txholds);
464
465 #endif
466