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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/zfs_context.h>
26 #include <sys/vnode.h>
28 #include <sys/zfs_acl.h>
29 #include <sys/zfs_sa.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/sa_impl.h>
32 #include <sys/zfeature.h>
35 * ZPL attribute registration table.
36 * Order of attributes doesn't matter
37 * a unique value will be assigned for each
38 * attribute that is file system specific
40 * This is just the set of ZPL attributes that this
41 * version of ZFS deals with natively. The file system
42 * could have other attributes stored in files, but they will be
43 * ignored. The SA framework will preserve them, just that
44 * this version of ZFS won't change or delete them.
47 const sa_attr_reg_t zfs_attr_table
[ZPL_END
+1] = {
48 {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY
, 0},
49 {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY
, 1},
50 {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY
, 2},
51 {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY
, 3},
52 {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY
, 4},
53 {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY
, 5},
54 {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY
, 6},
55 {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY
, 7},
56 {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY
, 8},
57 {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY
, 9},
58 {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY
, 10},
59 {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY
, 11},
60 {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY
, 12},
61 {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY
, 13},
62 {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY
, 14},
63 {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY
, 15},
64 {"ZPL_DACL_COUNT", sizeof (uint64_t), SA_UINT64_ARRAY
, 0},
65 {"ZPL_SYMLINK", 0, SA_UINT8_ARRAY
, 0},
66 {"ZPL_SCANSTAMP", 32, SA_UINT8_ARRAY
, 0},
67 {"ZPL_DACL_ACES", 0, SA_ACL
, 0},
68 {"ZPL_DXATTR", 0, SA_UINT8_ARRAY
, 0},
69 {"ZPL_PROJID", sizeof (uint64_t), SA_UINT64_ARRAY
, 0},
75 static int zfs_zil_saxattr
= 1;
78 zfs_sa_readlink(znode_t
*zp
, zfs_uio_t
*uio
)
80 dmu_buf_t
*db
= sa_get_db(zp
->z_sa_hdl
);
85 if (bufsz
+ ZFS_OLD_ZNODE_PHYS_SIZE
<= db
->db_size
) {
86 error
= zfs_uiomove((caddr_t
)db
->db_data
+
87 ZFS_OLD_ZNODE_PHYS_SIZE
,
88 MIN((size_t)bufsz
, zfs_uio_resid(uio
)), UIO_READ
, uio
);
91 if ((error
= dmu_buf_hold(ZTOZSB(zp
)->z_os
, zp
->z_id
,
92 0, FTAG
, &dbp
, DMU_READ_NO_PREFETCH
)) == 0) {
93 error
= zfs_uiomove(dbp
->db_data
,
94 MIN((size_t)bufsz
, zfs_uio_resid(uio
)), UIO_READ
,
96 dmu_buf_rele(dbp
, FTAG
);
103 zfs_sa_symlink(znode_t
*zp
, char *link
, int len
, dmu_tx_t
*tx
)
105 dmu_buf_t
*db
= sa_get_db(zp
->z_sa_hdl
);
107 if (ZFS_OLD_ZNODE_PHYS_SIZE
+ len
<= dmu_bonus_max()) {
108 VERIFY0(dmu_set_bonus(db
, len
+ ZFS_OLD_ZNODE_PHYS_SIZE
, tx
));
110 memcpy((caddr_t
)db
->db_data
+
111 ZFS_OLD_ZNODE_PHYS_SIZE
, link
, len
);
116 zfs_grow_blocksize(zp
, len
, tx
);
117 VERIFY0(dmu_buf_hold(ZTOZSB(zp
)->z_os
, zp
->z_id
, 0, FTAG
, &dbp
,
118 DMU_READ_NO_PREFETCH
));
120 dmu_buf_will_dirty(dbp
, tx
);
122 ASSERT3U(len
, <=, dbp
->db_size
);
123 memcpy(dbp
->db_data
, link
, len
);
124 dmu_buf_rele(dbp
, FTAG
);
129 zfs_sa_get_scanstamp(znode_t
*zp
, xvattr_t
*xvap
)
131 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
134 ASSERT(MUTEX_HELD(&zp
->z_lock
));
135 VERIFY((xoap
= xva_getxoptattr(xvap
)) != NULL
);
137 if (sa_lookup(zp
->z_sa_hdl
, SA_ZPL_SCANSTAMP(zfsvfs
),
138 &xoap
->xoa_av_scanstamp
,
139 sizeof (xoap
->xoa_av_scanstamp
)) != 0)
142 dmu_object_info_t doi
;
143 dmu_buf_t
*db
= sa_get_db(zp
->z_sa_hdl
);
146 if (!(zp
->z_pflags
& ZFS_BONUS_SCANSTAMP
))
149 sa_object_info(zp
->z_sa_hdl
, &doi
);
150 len
= sizeof (xoap
->xoa_av_scanstamp
) +
151 ZFS_OLD_ZNODE_PHYS_SIZE
;
153 if (len
<= doi
.doi_bonus_size
) {
154 (void) memcpy(xoap
->xoa_av_scanstamp
,
155 (caddr_t
)db
->db_data
+ ZFS_OLD_ZNODE_PHYS_SIZE
,
156 sizeof (xoap
->xoa_av_scanstamp
));
159 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
163 zfs_sa_set_scanstamp(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
165 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
168 ASSERT(MUTEX_HELD(&zp
->z_lock
));
169 VERIFY((xoap
= xva_getxoptattr(xvap
)) != NULL
);
171 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SCANSTAMP(zfsvfs
),
172 &xoap
->xoa_av_scanstamp
,
173 sizeof (xoap
->xoa_av_scanstamp
), tx
));
175 dmu_object_info_t doi
;
176 dmu_buf_t
*db
= sa_get_db(zp
->z_sa_hdl
);
179 sa_object_info(zp
->z_sa_hdl
, &doi
);
180 len
= sizeof (xoap
->xoa_av_scanstamp
) +
181 ZFS_OLD_ZNODE_PHYS_SIZE
;
182 if (len
> doi
.doi_bonus_size
)
183 VERIFY(dmu_set_bonus(db
, len
, tx
) == 0);
184 (void) memcpy((caddr_t
)db
->db_data
+ ZFS_OLD_ZNODE_PHYS_SIZE
,
185 xoap
->xoa_av_scanstamp
, sizeof (xoap
->xoa_av_scanstamp
));
187 zp
->z_pflags
|= ZFS_BONUS_SCANSTAMP
;
188 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_FLAGS(zfsvfs
),
189 &zp
->z_pflags
, sizeof (uint64_t), tx
));
194 zfs_sa_get_xattr(znode_t
*zp
)
196 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
201 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
202 ASSERT(!zp
->z_xattr_cached
);
205 error
= sa_size(zp
->z_sa_hdl
, SA_ZPL_DXATTR(zfsvfs
), &size
);
208 return nvlist_alloc(&zp
->z_xattr_cached
,
209 NV_UNIQUE_NAME
, KM_SLEEP
);
214 obj
= vmem_alloc(size
, KM_SLEEP
);
216 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_DXATTR(zfsvfs
), obj
, size
);
218 error
= nvlist_unpack(obj
, size
, &zp
->z_xattr_cached
, KM_SLEEP
);
220 vmem_free(obj
, size
);
226 zfs_sa_set_xattr(znode_t
*zp
, const char *name
, const void *value
, size_t vsize
)
228 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
233 int error
, logsaxattr
= 0;
235 ASSERT(RW_WRITE_HELD(&zp
->z_xattr_lock
));
236 ASSERT(zp
->z_xattr_cached
);
239 error
= nvlist_size(zp
->z_xattr_cached
, &size
, NV_ENCODE_XDR
);
240 if ((error
== 0) && (size
> SA_ATTR_MAX_LEN
))
241 error
= SET_ERROR(EFBIG
);
245 obj
= vmem_alloc(size
, KM_SLEEP
);
247 error
= nvlist_pack(zp
->z_xattr_cached
, &obj
, &size
,
248 NV_ENCODE_XDR
, KM_SLEEP
);
252 zilog
= zfsvfs
->z_log
;
255 * Users enable ZIL logging of xattr=sa operations by enabling the
256 * SPA_FEATURE_ZILSAXATTR feature on the pool. Feature is activated
257 * during zil_process_commit_list/zil_create, if enabled.
259 if (spa_feature_is_enabled(zfsvfs
->z_os
->os_spa
,
260 SPA_FEATURE_ZILSAXATTR
) && zfs_zil_saxattr
)
263 tx
= dmu_tx_create(zfsvfs
->z_os
);
264 dmu_tx_hold_sa_create(tx
, size
);
265 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
267 error
= dmu_tx_assign(tx
, TXG_WAIT
);
272 sa_bulk_attr_t bulk
[2];
276 zfs_log_setsaxattr(zilog
, tx
, TX_SETSAXATTR
, zp
, name
,
279 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, NULL
, ctime
);
280 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_DXATTR(zfsvfs
),
282 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
),
284 VERIFY0(sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
));
287 if (logsaxattr
&& zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
288 zil_commit(zilog
, 0);
291 vmem_free(obj
, size
);
297 * I'm not convinced we should do any of this upgrade.
298 * since the SA code can read both old/new znode formats
299 * with probably little to no performance difference.
301 * All new files will be created with the new format.
305 zfs_sa_upgrade(sa_handle_t
*hdl
, dmu_tx_t
*tx
)
307 dmu_buf_t
*db
= sa_get_db(hdl
);
308 znode_t
*zp
= sa_get_userdata(hdl
);
309 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
311 sa_bulk_attr_t
*bulk
, *sa_attrs
;
312 zfs_acl_locator_cb_t locate
= { 0 };
313 uint64_t uid
, gid
, mode
, rdev
, xattr
, parent
, tmp_gen
;
314 uint64_t crtime
[2], mtime
[2], ctime
[2], atime
[2];
316 zfs_acl_phys_t znode_acl
;
317 char scanstamp
[AV_SCANSTAMP_SZ
];
318 boolean_t drop_lock
= B_FALSE
;
321 * No upgrade if ACL isn't cached
322 * since we won't know which locks are held
323 * and ready the ACL would require special "locked"
324 * interfaces that would be messy
326 if (zp
->z_acl_cached
== NULL
|| Z_ISLNK(ZTOTYPE(zp
)))
330 * If the z_lock is held and we aren't the owner
331 * the just return since we don't want to deadlock
332 * trying to update the status of z_is_sa. This
333 * file can then be upgraded at a later time.
335 * Otherwise, we know we are doing the
336 * sa_update() that caused us to enter this function.
338 if (MUTEX_NOT_HELD(&zp
->z_lock
)) {
339 if (mutex_tryenter(&zp
->z_lock
) == 0)
345 /* First do a bulk query of the attributes that aren't cached */
346 bulk
= kmem_alloc(sizeof (sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
347 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
, &atime
, 16);
348 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
, &mtime
, 16);
349 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
, &ctime
, 16);
350 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CRTIME(zfsvfs
), NULL
, &crtime
, 16);
351 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
352 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zfsvfs
), NULL
, &parent
, 8);
353 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_XATTR(zfsvfs
), NULL
, &xattr
, 8);
354 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_RDEV(zfsvfs
), NULL
, &rdev
, 8);
355 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zfsvfs
), NULL
, &uid
, 8);
356 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zfsvfs
), NULL
, &gid
, 8);
357 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GEN(zfsvfs
), NULL
, &tmp_gen
, 8);
358 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ZNODE_ACL(zfsvfs
), NULL
,
361 if (sa_bulk_lookup_locked(hdl
, bulk
, count
) != 0)
364 if (dmu_objset_projectquota_enabled(hdl
->sa_os
) &&
365 !(zp
->z_pflags
& ZFS_PROJID
)) {
366 zp
->z_pflags
|= ZFS_PROJID
;
367 zp
->z_projid
= ZFS_DEFAULT_PROJID
;
371 * While the order here doesn't matter its best to try and organize
372 * it is such a way to pick up an already existing layout number
375 sa_attrs
= kmem_zalloc(sizeof (sa_bulk_attr_t
) * ZPL_END
, KM_SLEEP
);
376 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_MODE(zfsvfs
), NULL
, &mode
, 8);
377 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
379 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_GEN(zfsvfs
),
381 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_UID(zfsvfs
), NULL
, &uid
, 8);
382 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_GID(zfsvfs
), NULL
, &gid
, 8);
383 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_PARENT(zfsvfs
),
385 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
387 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_ATIME(zfsvfs
), NULL
,
389 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
391 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
393 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_CRTIME(zfsvfs
), NULL
,
396 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
398 if (dmu_objset_projectquota_enabled(hdl
->sa_os
))
399 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_PROJID(zfsvfs
), NULL
,
401 if (Z_ISBLK(ZTOTYPE(zp
)) || Z_ISCHR(ZTOTYPE(zp
)))
402 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_RDEV(zfsvfs
), NULL
,
404 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_DACL_COUNT(zfsvfs
), NULL
,
405 &zp
->z_acl_cached
->z_acl_count
, 8);
407 if (zp
->z_acl_cached
->z_version
< ZFS_ACL_VERSION_FUID
)
408 zfs_acl_xform(zp
, zp
->z_acl_cached
, CRED());
410 locate
.cb_aclp
= zp
->z_acl_cached
;
411 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_DACL_ACES(zfsvfs
),
412 zfs_acl_data_locator
, &locate
, zp
->z_acl_cached
->z_acl_bytes
);
415 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_XATTR(zfsvfs
),
418 /* if scanstamp then add scanstamp */
420 if (zp
->z_pflags
& ZFS_BONUS_SCANSTAMP
) {
422 (caddr_t
)db
->db_data
+ ZFS_OLD_ZNODE_PHYS_SIZE
,
424 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_SCANSTAMP(zfsvfs
),
425 NULL
, scanstamp
, AV_SCANSTAMP_SZ
);
426 zp
->z_pflags
&= ~ZFS_BONUS_SCANSTAMP
;
429 VERIFY(dmu_set_bonustype(db
, DMU_OT_SA
, tx
) == 0);
430 VERIFY(sa_replace_all_by_template_locked(hdl
, sa_attrs
,
432 if (znode_acl
.z_acl_extern_obj
)
433 VERIFY(0 == dmu_object_free(zfsvfs
->z_os
,
434 znode_acl
.z_acl_extern_obj
, tx
));
436 zp
->z_is_sa
= B_TRUE
;
437 kmem_free(sa_attrs
, sizeof (sa_bulk_attr_t
) * ZPL_END
);
439 kmem_free(bulk
, sizeof (sa_bulk_attr_t
) * ZPL_END
);
441 mutex_exit(&zp
->z_lock
);
445 zfs_sa_upgrade_txholds(dmu_tx_t
*tx
, znode_t
*zp
)
447 if (!ZTOZSB(zp
)->z_use_sa
|| zp
->z_is_sa
)
451 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
453 if (zfs_external_acl(zp
)) {
454 dmu_tx_hold_free(tx
, zfs_external_acl(zp
), 0,
459 ZFS_MODULE_PARAM(zfs
, zfs_
, zil_saxattr
, INT
, ZMOD_RW
,
460 "Disable xattr=sa extended attribute logging in ZIL by settng 0.");
462 EXPORT_SYMBOL(zfs_attr_table
);
463 EXPORT_SYMBOL(zfs_sa_readlink
);
464 EXPORT_SYMBOL(zfs_sa_symlink
);
465 EXPORT_SYMBOL(zfs_sa_get_scanstamp
);
466 EXPORT_SYMBOL(zfs_sa_set_scanstamp
);
467 EXPORT_SYMBOL(zfs_sa_get_xattr
);
468 EXPORT_SYMBOL(zfs_sa_set_xattr
);
469 EXPORT_SYMBOL(zfs_sa_upgrade
);
470 EXPORT_SYMBOL(zfs_sa_upgrade_txholds
);