]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zfs_sa.c
New upstream version 0.7.2
[mirror_zfs-debian.git] / module / zfs / zfs_sa.c
CommitLineData
428870ff
BB
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
e10b0808 25#include <sys/zfs_context.h>
428870ff
BB
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
44sa_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},
82a37189 65 {"ZPL_DXATTR", 0, SA_UINT8_ARRAY, 0},
428870ff
BB
66 {NULL, 0, 0, 0}
67};
68
69#ifdef _KERNEL
428870ff
BB
70int
71zfs_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;
3558fd73 84 if ((error = dmu_buf_hold(ZTOZSB(zp)->z_os, zp->z_id,
428870ff
BB
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
94void
95zfs_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()) {
cae5b340 100 VERIFY0(dmu_set_bonus(db, len + ZFS_OLD_ZNODE_PHYS_SIZE, tx));
428870ff
BB
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);
cae5b340
AX
109 VERIFY0(dmu_buf_hold(ZTOZSB(zp)->z_os, zp->z_id, 0, FTAG, &dbp,
110 DMU_READ_NO_PREFETCH));
428870ff
BB
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
120void
121zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
122{
cae5b340 123 zfsvfs_t *zfsvfs = ZTOZSB(zp);
428870ff
BB
124 xoptattr_t *xoap;
125
572e2857 126 ASSERT(MUTEX_HELD(&zp->z_lock));
428870ff
BB
127 VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
128 if (zp->z_is_sa) {
cae5b340 129 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
428870ff
BB
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
154void
155zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
156{
cae5b340 157 zfsvfs_t *zfsvfs = ZTOZSB(zp);
428870ff
BB
158 xoptattr_t *xoap;
159
572e2857 160 ASSERT(MUTEX_HELD(&zp->z_lock));
428870ff
BB
161 VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
162 if (zp->z_is_sa)
cae5b340 163 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
428870ff
BB
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;
cae5b340 180 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
428870ff
BB
181 &zp->z_pflags, sizeof (uint64_t), tx));
182 }
183}
184
82a37189
BB
185int
186zfs_sa_get_xattr(znode_t *zp)
187{
cae5b340 188 zfsvfs_t *zfsvfs = ZTOZSB(zp);
82a37189
BB
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
cae5b340 197 error = sa_size(zp->z_sa_hdl, SA_ZPL_DXATTR(zfsvfs), &size);
82a37189
BB
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
cae5b340 206 obj = vmem_alloc(size, KM_SLEEP);
82a37189 207
cae5b340 208 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DXATTR(zfsvfs), obj, size);
82a37189
BB
209 if (error == 0)
210 error = nvlist_unpack(obj, size, &zp->z_xattr_cached, KM_SLEEP);
211
cae5b340 212 vmem_free(obj, size);
82a37189
BB
213
214 return (error);
215}
216
217int
218zfs_sa_set_xattr(znode_t *zp)
219{
cae5b340 220 zfsvfs_t *zfsvfs = ZTOZSB(zp);
82a37189
BB
221 dmu_tx_t *tx;
222 char *obj;
223 size_t size;
224 int error;
225
226 ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
227 ASSERT(zp->z_xattr_cached);
228 ASSERT(zp->z_is_sa);
229
230 error = nvlist_size(zp->z_xattr_cached, &size, NV_ENCODE_XDR);
4d815aed
AX
231 if ((error == 0) && (size > SA_ATTR_MAX_LEN))
232 error = EFBIG;
82a37189
BB
233 if (error)
234 goto out;
235
cae5b340 236 obj = vmem_alloc(size, KM_SLEEP);
82a37189
BB
237
238 error = nvlist_pack(zp->z_xattr_cached, &obj, &size,
239 NV_ENCODE_XDR, KM_SLEEP);
240 if (error)
241 goto out_free;
242
cae5b340 243 tx = dmu_tx_create(zfsvfs->z_os);
82a37189 244 dmu_tx_hold_sa_create(tx, size);
f828e63a 245 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
82a37189
BB
246
247 error = dmu_tx_assign(tx, TXG_WAIT);
248 if (error) {
249 dmu_tx_abort(tx);
250 } else {
cae5b340
AX
251 int count = 0;
252 sa_bulk_attr_t bulk[2];
253 uint64_t ctime[2];
254
255 zfs_tstamp_update_setup(zp, STATE_CHANGED, NULL, ctime);
256 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DXATTR(zfsvfs),
257 NULL, obj, size);
258 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
259 NULL, &ctime, 16);
260 VERIFY0(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx));
261
4d815aed 262 dmu_tx_commit(tx);
82a37189
BB
263 }
264out_free:
cae5b340 265 vmem_free(obj, size);
82a37189
BB
266out:
267 return (error);
268}
269
428870ff
BB
270/*
271 * I'm not convinced we should do any of this upgrade.
272 * since the SA code can read both old/new znode formats
a08ee875 273 * with probably little to no performance difference.
428870ff
BB
274 *
275 * All new files will be created with the new format.
276 */
277
278void
279zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
280{
281 dmu_buf_t *db = sa_get_db(hdl);
282 znode_t *zp = sa_get_userdata(hdl);
cae5b340 283 zfsvfs_t *zfsvfs = ZTOZSB(zp);
428870ff 284 int count = 0;
1ee1b767 285 sa_bulk_attr_t *bulk, *sa_attrs;
428870ff 286 zfs_acl_locator_cb_t locate = { 0 };
cae5b340 287 uint64_t uid, gid, mode, rdev, xattr, parent, tmp_gen;
68d83c55 288 uint64_t crtime[2], mtime[2], ctime[2], atime[2];
cae5b340 289 uint64_t links;
428870ff
BB
290 zfs_acl_phys_t znode_acl;
291 char scanstamp[AV_SCANSTAMP_SZ];
572e2857 292 boolean_t drop_lock = B_FALSE;
428870ff
BB
293
294 /*
295 * No upgrade if ACL isn't cached
296 * since we won't know which locks are held
297 * and ready the ACL would require special "locked"
298 * interfaces that would be messy
299 */
3558fd73 300 if (zp->z_acl_cached == NULL || S_ISLNK(ZTOI(zp)->i_mode))
428870ff
BB
301 return;
302
572e2857
BB
303 /*
304 * If the z_lock is held and we aren't the owner
305 * the just return since we don't want to deadlock
306 * trying to update the status of z_is_sa. This
307 * file can then be upgraded at a later time.
308 *
309 * Otherwise, we know we are doing the
310 * sa_update() that caused us to enter this function.
311 */
312 if (mutex_owner(&zp->z_lock) != curthread) {
313 if (mutex_tryenter(&zp->z_lock) == 0)
314 return;
315 else
316 drop_lock = B_TRUE;
317 }
318
428870ff 319 /* First do a bulk query of the attributes that aren't cached */
a08ee875 320 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 20, KM_SLEEP);
cae5b340
AX
321 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
322 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
323 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
324 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
325 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
326 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
327 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL, &xattr, 8);
328 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, &rdev, 8);
329 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
330 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
331 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &tmp_gen, 8);
332 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
428870ff
BB
333 &znode_acl, 88);
334
1ee1b767 335 if (sa_bulk_lookup_locked(hdl, bulk, count) != 0) {
a08ee875 336 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 20);
572e2857 337 goto done;
1ee1b767 338 }
428870ff
BB
339
340 /*
341 * While the order here doesn't matter its best to try and organize
342 * it is such a way to pick up an already existing layout number
343 */
344 count = 0;
a08ee875 345 sa_attrs = kmem_zalloc(sizeof (sa_bulk_attr_t) * 20, KM_SLEEP);
cae5b340
AX
346 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
347 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
428870ff 348 &zp->z_size, 8);
cae5b340
AX
349 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zfsvfs),
350 NULL, &tmp_gen, 8);
351 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
352 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
353 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zfsvfs),
428870ff 354 NULL, &parent, 8);
cae5b340 355 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
428870ff 356 &zp->z_pflags, 8);
cae5b340 357 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zfsvfs), NULL,
68d83c55 358 &atime, 16);
cae5b340 359 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zfsvfs), NULL,
428870ff 360 &mtime, 16);
cae5b340 361 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zfsvfs), NULL,
428870ff 362 &ctime, 16);
cae5b340 363 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
428870ff 364 &crtime, 16);
cae5b340
AX
365 links = ZTOI(zp)->i_nlink;
366 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zfsvfs), NULL,
367 &links, 8);
3558fd73 368 if (S_ISBLK(ZTOI(zp)->i_mode) || S_ISCHR(ZTOI(zp)->i_mode))
cae5b340 369 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
428870ff 370 &rdev, 8);
cae5b340 371 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
428870ff
BB
372 &zp->z_acl_cached->z_acl_count, 8);
373
374 if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
375 zfs_acl_xform(zp, zp->z_acl_cached, CRED());
376
377 locate.cb_aclp = zp->z_acl_cached;
cae5b340 378 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
428870ff 379 zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
572e2857 380
428870ff 381 if (xattr)
cae5b340 382 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zfsvfs),
572e2857 383 NULL, &xattr, 8);
428870ff
BB
384
385 /* if scanstamp then add scanstamp */
386
387 if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
388 bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
389 scanstamp, AV_SCANSTAMP_SZ);
cae5b340 390 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zfsvfs),
428870ff
BB
391 NULL, scanstamp, AV_SCANSTAMP_SZ);
392 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
393 }
394
395 VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
396 VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
397 count, tx) == 0);
398 if (znode_acl.z_acl_extern_obj)
cae5b340 399 VERIFY(0 == dmu_object_free(zfsvfs->z_os,
428870ff
BB
400 znode_acl.z_acl_extern_obj, tx));
401
402 zp->z_is_sa = B_TRUE;
a08ee875
LG
403 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * 20);
404 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 20);
572e2857
BB
405done:
406 if (drop_lock)
407 mutex_exit(&zp->z_lock);
428870ff
BB
408}
409
410void
411zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
412{
3558fd73 413 if (!ZTOZSB(zp)->z_use_sa || zp->z_is_sa)
428870ff
BB
414 return;
415
428870ff
BB
416
417 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
418
572e2857
BB
419 if (zfs_external_acl(zp)) {
420 dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
428870ff
BB
421 DMU_OBJECT_END);
422 }
423}
424
e29be02e 425EXPORT_SYMBOL(zfs_attr_table);
e45aa452
BB
426EXPORT_SYMBOL(zfs_sa_readlink);
427EXPORT_SYMBOL(zfs_sa_symlink);
428EXPORT_SYMBOL(zfs_sa_get_scanstamp);
429EXPORT_SYMBOL(zfs_sa_set_scanstamp);
82a37189
BB
430EXPORT_SYMBOL(zfs_sa_get_xattr);
431EXPORT_SYMBOL(zfs_sa_set_xattr);
e45aa452
BB
432EXPORT_SYMBOL(zfs_sa_upgrade);
433EXPORT_SYMBOL(zfs_sa_upgrade_txholds);
434
428870ff 435#endif