]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_sa.c
Implement large_dnode pool feature
[mirror_zfs.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
0c66c32d 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()) {
50c957f7 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);
50c957f7
NB
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{
3558fd73 123 zfs_sb_t *zsb = 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) {
3558fd73 129 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zsb),
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{
3558fd73 157 zfs_sb_t *zsb = 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)
3558fd73 163 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zsb),
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;
3558fd73 180 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
428870ff
BB
181 &zp->z_pflags, sizeof (uint64_t), tx));
182 }
183}
184
82a37189
BB
185int
186zfs_sa_get_xattr(znode_t *zp)
187{
188 zfs_sb_t *zsb = 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
f828e63a 197 error = sa_size(zp->z_sa_hdl, SA_ZPL_DXATTR(zsb), &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
81971b13 206 obj = zio_buf_alloc(size);
82a37189 207
f828e63a 208 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DXATTR(zsb), obj, size);
82a37189
BB
209 if (error == 0)
210 error = nvlist_unpack(obj, size, &zp->z_xattr_cached, KM_SLEEP);
211
81971b13 212 zio_buf_free(obj, size);
82a37189
BB
213
214 return (error);
215}
216
217int
218zfs_sa_set_xattr(znode_t *zp)
219{
220 zfs_sb_t *zsb = ZTOZSB(zp);
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);
43b4935e
NB
231 if ((error == 0) && (size > SA_ATTR_MAX_LEN))
232 error = EFBIG;
82a37189
BB
233 if (error)
234 goto out;
235
81971b13 236 obj = zio_buf_alloc(size);
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
243 tx = dmu_tx_create(zsb->z_os);
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 {
43b4935e
NB
251 VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_DXATTR(zsb),
252 obj, size, tx));
253 dmu_tx_commit(tx);
82a37189
BB
254 }
255out_free:
81971b13 256 zio_buf_free(obj, size);
82a37189
BB
257out:
258 return (error);
259}
260
428870ff
BB
261/*
262 * I'm not convinced we should do any of this upgrade.
263 * since the SA code can read both old/new znode formats
d3cc8b15 264 * with probably little to no performance difference.
428870ff
BB
265 *
266 * All new files will be created with the new format.
267 */
268
269void
270zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
271{
272 dmu_buf_t *db = sa_get_db(hdl);
273 znode_t *zp = sa_get_userdata(hdl);
3558fd73 274 zfs_sb_t *zsb = ZTOZSB(zp);
428870ff 275 int count = 0;
1ee1b767 276 sa_bulk_attr_t *bulk, *sa_attrs;
428870ff 277 zfs_acl_locator_cb_t locate = { 0 };
278f2236 278 uint64_t uid, gid, mode, rdev, xattr, parent, tmp_gen;
0df9673f 279 uint64_t crtime[2], mtime[2], ctime[2], atime[2];
428870ff
BB
280 zfs_acl_phys_t znode_acl;
281 char scanstamp[AV_SCANSTAMP_SZ];
572e2857 282 boolean_t drop_lock = B_FALSE;
428870ff
BB
283
284 /*
285 * No upgrade if ACL isn't cached
286 * since we won't know which locks are held
287 * and ready the ACL would require special "locked"
288 * interfaces that would be messy
289 */
3558fd73 290 if (zp->z_acl_cached == NULL || S_ISLNK(ZTOI(zp)->i_mode))
428870ff
BB
291 return;
292
572e2857
BB
293 /*
294 * If the z_lock is held and we aren't the owner
295 * the just return since we don't want to deadlock
296 * trying to update the status of z_is_sa. This
297 * file can then be upgraded at a later time.
298 *
299 * Otherwise, we know we are doing the
300 * sa_update() that caused us to enter this function.
301 */
302 if (mutex_owner(&zp->z_lock) != curthread) {
303 if (mutex_tryenter(&zp->z_lock) == 0)
304 return;
305 else
306 drop_lock = B_TRUE;
307 }
308
428870ff 309 /* First do a bulk query of the attributes that aren't cached */
d1d7e268 310 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 20, KM_SLEEP);
0df9673f 311 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
3558fd73
BB
312 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
313 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
314 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zsb), NULL, &crtime, 16);
315 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &mode, 8);
316 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL, &parent, 8);
317 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zsb), NULL, &xattr, 8);
318 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zsb), NULL, &rdev, 8);
319 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &uid, 8);
320 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &gid, 8);
278f2236 321 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, &tmp_gen, 8);
3558fd73 322 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zsb), NULL,
428870ff
BB
323 &znode_acl, 88);
324
1ee1b767 325 if (sa_bulk_lookup_locked(hdl, bulk, count) != 0) {
d1d7e268 326 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 20);
572e2857 327 goto done;
1ee1b767 328 }
428870ff
BB
329
330 /*
331 * While the order here doesn't matter its best to try and organize
332 * it is such a way to pick up an already existing layout number
333 */
334 count = 0;
d1d7e268 335 sa_attrs = kmem_zalloc(sizeof (sa_bulk_attr_t) * 20, KM_SLEEP);
3558fd73
BB
336 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zsb), NULL, &mode, 8);
337 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zsb), NULL,
428870ff 338 &zp->z_size, 8);
278f2236 339 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zsb), NULL, &tmp_gen, 8);
3558fd73
BB
340 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zsb), NULL, &uid, 8);
341 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zsb), NULL, &gid, 8);
342 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zsb),
428870ff 343 NULL, &parent, 8);
3558fd73 344 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zsb), NULL,
428870ff 345 &zp->z_pflags, 8);
3558fd73 346 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zsb), NULL,
0df9673f 347 &atime, 16);
3558fd73 348 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zsb), NULL,
428870ff 349 &mtime, 16);
3558fd73 350 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zsb), NULL,
428870ff 351 &ctime, 16);
3558fd73 352 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zsb), NULL,
428870ff 353 &crtime, 16);
3558fd73 354 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zsb), NULL,
428870ff 355 &zp->z_links, 8);
3558fd73
BB
356 if (S_ISBLK(ZTOI(zp)->i_mode) || S_ISCHR(ZTOI(zp)->i_mode))
357 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zsb), NULL,
428870ff 358 &rdev, 8);
3558fd73 359 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zsb), NULL,
428870ff
BB
360 &zp->z_acl_cached->z_acl_count, 8);
361
362 if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
363 zfs_acl_xform(zp, zp->z_acl_cached, CRED());
364
365 locate.cb_aclp = zp->z_acl_cached;
3558fd73 366 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zsb),
428870ff 367 zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
572e2857 368
428870ff 369 if (xattr)
3558fd73 370 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zsb),
572e2857 371 NULL, &xattr, 8);
428870ff
BB
372
373 /* if scanstamp then add scanstamp */
374
375 if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
376 bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
377 scanstamp, AV_SCANSTAMP_SZ);
3558fd73 378 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zsb),
428870ff
BB
379 NULL, scanstamp, AV_SCANSTAMP_SZ);
380 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
381 }
382
383 VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
384 VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
385 count, tx) == 0);
386 if (znode_acl.z_acl_extern_obj)
3558fd73 387 VERIFY(0 == dmu_object_free(zsb->z_os,
428870ff
BB
388 znode_acl.z_acl_extern_obj, tx));
389
390 zp->z_is_sa = B_TRUE;
d1d7e268
MK
391 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * 20);
392 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 20);
572e2857
BB
393done:
394 if (drop_lock)
395 mutex_exit(&zp->z_lock);
428870ff
BB
396}
397
398void
399zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
400{
3558fd73 401 if (!ZTOZSB(zp)->z_use_sa || zp->z_is_sa)
428870ff
BB
402 return;
403
428870ff
BB
404
405 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
406
572e2857
BB
407 if (zfs_external_acl(zp)) {
408 dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
428870ff
BB
409 DMU_OBJECT_END);
410 }
411}
412
e29be02e 413EXPORT_SYMBOL(zfs_attr_table);
e45aa452
BB
414EXPORT_SYMBOL(zfs_sa_readlink);
415EXPORT_SYMBOL(zfs_sa_symlink);
416EXPORT_SYMBOL(zfs_sa_get_scanstamp);
417EXPORT_SYMBOL(zfs_sa_set_scanstamp);
82a37189
BB
418EXPORT_SYMBOL(zfs_sa_get_xattr);
419EXPORT_SYMBOL(zfs_sa_set_xattr);
e45aa452
BB
420EXPORT_SYMBOL(zfs_sa_upgrade);
421EXPORT_SYMBOL(zfs_sa_upgrade_txholds);
422
428870ff 423#endif