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 http://www.opensolaris.org/os/licensing.
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/types.h>
26 #include <sys/param.h>
27 #include <sys/vnode.h>
29 #include <sys/zfs_acl.h>
30 #include <sys/zfs_sa.h>
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
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.
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 {"ZPL_DXATTR", 0, SA_UINT8_ARRAY
, 0},
72 zfs_sa_readlink(znode_t
*zp
, uio_t
*uio
)
74 dmu_buf_t
*db
= sa_get_db(zp
->z_sa_hdl
);
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
);
85 if ((error
= dmu_buf_hold(ZTOZSB(zp
)->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
);
96 zfs_sa_symlink(znode_t
*zp
, char *link
, int len
, dmu_tx_t
*tx
)
98 dmu_buf_t
*db
= sa_get_db(zp
->z_sa_hdl
);
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);
104 bcopy(link
, (caddr_t
)db
->db_data
+
105 ZFS_OLD_ZNODE_PHYS_SIZE
, len
);
110 zfs_grow_blocksize(zp
, len
, tx
);
111 VERIFY(0 == dmu_buf_hold(ZTOZSB(zp
)->z_os
,
112 zp
->z_id
, 0, FTAG
, &dbp
, DMU_READ_NO_PREFETCH
));
114 dmu_buf_will_dirty(dbp
, tx
);
116 ASSERT3U(len
, <=, dbp
->db_size
);
117 bcopy(link
, dbp
->db_data
, len
);
118 dmu_buf_rele(dbp
, FTAG
);
123 zfs_sa_get_scanstamp(znode_t
*zp
, xvattr_t
*xvap
)
125 zfs_sb_t
*zsb
= ZTOZSB(zp
);
128 ASSERT(MUTEX_HELD(&zp
->z_lock
));
129 VERIFY((xoap
= xva_getxoptattr(xvap
)) != NULL
);
131 if (sa_lookup(zp
->z_sa_hdl
, SA_ZPL_SCANSTAMP(zsb
),
132 &xoap
->xoa_av_scanstamp
,
133 sizeof (xoap
->xoa_av_scanstamp
)) != 0)
136 dmu_object_info_t doi
;
137 dmu_buf_t
*db
= sa_get_db(zp
->z_sa_hdl
);
140 if (!(zp
->z_pflags
& ZFS_BONUS_SCANSTAMP
))
143 sa_object_info(zp
->z_sa_hdl
, &doi
);
144 len
= sizeof (xoap
->xoa_av_scanstamp
) +
145 ZFS_OLD_ZNODE_PHYS_SIZE
;
147 if (len
<= doi
.doi_bonus_size
) {
148 (void) memcpy(xoap
->xoa_av_scanstamp
,
149 (caddr_t
)db
->db_data
+ ZFS_OLD_ZNODE_PHYS_SIZE
,
150 sizeof (xoap
->xoa_av_scanstamp
));
153 XVA_SET_RTN(xvap
, XAT_AV_SCANSTAMP
);
157 zfs_sa_set_scanstamp(znode_t
*zp
, xvattr_t
*xvap
, dmu_tx_t
*tx
)
159 zfs_sb_t
*zsb
= ZTOZSB(zp
);
162 ASSERT(MUTEX_HELD(&zp
->z_lock
));
163 VERIFY((xoap
= xva_getxoptattr(xvap
)) != NULL
);
165 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_SCANSTAMP(zsb
),
166 &xoap
->xoa_av_scanstamp
,
167 sizeof (xoap
->xoa_av_scanstamp
), tx
));
169 dmu_object_info_t doi
;
170 dmu_buf_t
*db
= sa_get_db(zp
->z_sa_hdl
);
173 sa_object_info(zp
->z_sa_hdl
, &doi
);
174 len
= sizeof (xoap
->xoa_av_scanstamp
) +
175 ZFS_OLD_ZNODE_PHYS_SIZE
;
176 if (len
> doi
.doi_bonus_size
)
177 VERIFY(dmu_set_bonus(db
, len
, tx
) == 0);
178 (void) memcpy((caddr_t
)db
->db_data
+ ZFS_OLD_ZNODE_PHYS_SIZE
,
179 xoap
->xoa_av_scanstamp
, sizeof (xoap
->xoa_av_scanstamp
));
181 zp
->z_pflags
|= ZFS_BONUS_SCANSTAMP
;
182 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_FLAGS(zsb
),
183 &zp
->z_pflags
, sizeof (uint64_t), tx
));
188 zfs_sa_get_xattr(znode_t
*zp
)
190 zfs_sb_t
*zsb
= ZTOZSB(zp
);
195 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
196 ASSERT(!zp
->z_xattr_cached
);
199 error
= sa_size(zp
->z_sa_hdl
, SA_ZPL_DXATTR(zsb
), &size
);
202 return nvlist_alloc(&zp
->z_xattr_cached
,
203 NV_UNIQUE_NAME
, KM_SLEEP
);
208 obj
= sa_spill_alloc(KM_SLEEP
);
210 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_DXATTR(zsb
), obj
, size
);
212 error
= nvlist_unpack(obj
, size
, &zp
->z_xattr_cached
, KM_SLEEP
);
220 zfs_sa_set_xattr(znode_t
*zp
)
222 zfs_sb_t
*zsb
= ZTOZSB(zp
);
228 ASSERT(RW_WRITE_HELD(&zp
->z_xattr_lock
));
229 ASSERT(zp
->z_xattr_cached
);
232 error
= nvlist_size(zp
->z_xattr_cached
, &size
, NV_ENCODE_XDR
);
236 obj
= sa_spill_alloc(KM_SLEEP
);
238 error
= nvlist_pack(zp
->z_xattr_cached
, &obj
, &size
,
239 NV_ENCODE_XDR
, KM_SLEEP
);
243 tx
= dmu_tx_create(zsb
->z_os
);
244 dmu_tx_hold_sa_create(tx
, size
);
245 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
247 error
= dmu_tx_assign(tx
, TXG_WAIT
);
251 error
= sa_update(zp
->z_sa_hdl
, SA_ZPL_DXATTR(zsb
),
265 * I'm not convinced we should do any of this upgrade.
266 * since the SA code can read both old/new znode formats
267 * with probably little to know performance difference.
269 * All new files will be created with the new format.
273 zfs_sa_upgrade(sa_handle_t
*hdl
, dmu_tx_t
*tx
)
275 dmu_buf_t
*db
= sa_get_db(hdl
);
276 znode_t
*zp
= sa_get_userdata(hdl
);
277 zfs_sb_t
*zsb
= ZTOZSB(zp
);
279 sa_bulk_attr_t
*bulk
, *sa_attrs
;
280 zfs_acl_locator_cb_t locate
= { 0 };
281 uint64_t uid
, gid
, mode
, rdev
, xattr
, parent
;
282 uint64_t crtime
[2], mtime
[2], ctime
[2];
283 zfs_acl_phys_t znode_acl
;
284 char scanstamp
[AV_SCANSTAMP_SZ
];
285 boolean_t drop_lock
= B_FALSE
;
288 * No upgrade if ACL isn't cached
289 * since we won't know which locks are held
290 * and ready the ACL would require special "locked"
291 * interfaces that would be messy
293 if (zp
->z_acl_cached
== NULL
|| S_ISLNK(ZTOI(zp
)->i_mode
))
297 * If the z_lock is held and we aren't the owner
298 * the just return since we don't want to deadlock
299 * trying to update the status of z_is_sa. This
300 * file can then be upgraded at a later time.
302 * Otherwise, we know we are doing the
303 * sa_update() that caused us to enter this function.
305 if (mutex_owner(&zp
->z_lock
) != curthread
) {
306 if (mutex_tryenter(&zp
->z_lock
) == 0)
312 /* First do a bulk query of the attributes that aren't cached */
313 bulk
= kmem_alloc(sizeof(sa_bulk_attr_t
) * 20, KM_SLEEP
);
314 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zsb
), NULL
, &mtime
, 16);
315 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zsb
), NULL
, &ctime
, 16);
316 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CRTIME(zsb
), NULL
, &crtime
, 16);
317 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
318 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zsb
), NULL
, &parent
, 8);
319 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_XATTR(zsb
), NULL
, &xattr
, 8);
320 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_RDEV(zsb
), NULL
, &rdev
, 8);
321 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_UID(zsb
), NULL
, &uid
, 8);
322 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_GID(zsb
), NULL
, &gid
, 8);
323 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_ZNODE_ACL(zsb
), NULL
,
326 if (sa_bulk_lookup_locked(hdl
, bulk
, count
) != 0) {
327 kmem_free(bulk
, sizeof(sa_bulk_attr_t
) * 20);
332 * While the order here doesn't matter its best to try and organize
333 * it is such a way to pick up an already existing layout number
336 sa_attrs
= kmem_zalloc(sizeof(sa_bulk_attr_t
) * 20, KM_SLEEP
);
337 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_MODE(zsb
), NULL
, &mode
, 8);
338 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_SIZE(zsb
), NULL
,
340 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_GEN(zsb
),
341 NULL
, &zp
->z_gen
, 8);
342 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_UID(zsb
), NULL
, &uid
, 8);
343 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_GID(zsb
), NULL
, &gid
, 8);
344 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_PARENT(zsb
),
346 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_FLAGS(zsb
), NULL
,
348 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_ATIME(zsb
), NULL
,
350 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_MTIME(zsb
), NULL
,
352 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_CTIME(zsb
), NULL
,
354 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_CRTIME(zsb
), NULL
,
356 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_LINKS(zsb
), NULL
,
358 if (S_ISBLK(ZTOI(zp
)->i_mode
) || S_ISCHR(ZTOI(zp
)->i_mode
))
359 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_RDEV(zsb
), NULL
,
361 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_DACL_COUNT(zsb
), NULL
,
362 &zp
->z_acl_cached
->z_acl_count
, 8);
364 if (zp
->z_acl_cached
->z_version
< ZFS_ACL_VERSION_FUID
)
365 zfs_acl_xform(zp
, zp
->z_acl_cached
, CRED());
367 locate
.cb_aclp
= zp
->z_acl_cached
;
368 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_DACL_ACES(zsb
),
369 zfs_acl_data_locator
, &locate
, zp
->z_acl_cached
->z_acl_bytes
);
372 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_XATTR(zsb
),
375 /* if scanstamp then add scanstamp */
377 if (zp
->z_pflags
& ZFS_BONUS_SCANSTAMP
) {
378 bcopy((caddr_t
)db
->db_data
+ ZFS_OLD_ZNODE_PHYS_SIZE
,
379 scanstamp
, AV_SCANSTAMP_SZ
);
380 SA_ADD_BULK_ATTR(sa_attrs
, count
, SA_ZPL_SCANSTAMP(zsb
),
381 NULL
, scanstamp
, AV_SCANSTAMP_SZ
);
382 zp
->z_pflags
&= ~ZFS_BONUS_SCANSTAMP
;
385 VERIFY(dmu_set_bonustype(db
, DMU_OT_SA
, tx
) == 0);
386 VERIFY(sa_replace_all_by_template_locked(hdl
, sa_attrs
,
388 if (znode_acl
.z_acl_extern_obj
)
389 VERIFY(0 == dmu_object_free(zsb
->z_os
,
390 znode_acl
.z_acl_extern_obj
, tx
));
392 zp
->z_is_sa
= B_TRUE
;
393 kmem_free(sa_attrs
, sizeof(sa_bulk_attr_t
) * 20);
394 kmem_free(bulk
, sizeof(sa_bulk_attr_t
) * 20);
397 mutex_exit(&zp
->z_lock
);
401 zfs_sa_upgrade_txholds(dmu_tx_t
*tx
, znode_t
*zp
)
403 if (!ZTOZSB(zp
)->z_use_sa
|| zp
->z_is_sa
)
407 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
409 if (zfs_external_acl(zp
)) {
410 dmu_tx_hold_free(tx
, zfs_external_acl(zp
), 0,
415 EXPORT_SYMBOL(zfs_sa_readlink
);
416 EXPORT_SYMBOL(zfs_sa_symlink
);
417 EXPORT_SYMBOL(zfs_sa_get_scanstamp
);
418 EXPORT_SYMBOL(zfs_sa_set_scanstamp
);
419 EXPORT_SYMBOL(zfs_sa_get_xattr
);
420 EXPORT_SYMBOL(zfs_sa_set_xattr
);
421 EXPORT_SYMBOL(zfs_sa_upgrade
);
422 EXPORT_SYMBOL(zfs_sa_upgrade_txholds
);