]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - zfs/module/zfs/zfs_sa.c
UBUNTU: SAUCE: (noup) Update spl to 0.6.5.9-1, zfs to 0.6.5.9-2
[mirror_ubuntu-artful-kernel.git] / zfs / module / zfs / zfs_sa.c
CommitLineData
87d546d8
TG
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
25#include <sys/zfs_context.h>
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},
65 {"ZPL_DXATTR", 0, SA_UINT8_ARRAY, 0},
66 {NULL, 0, 0, 0}
67};
68
69#ifdef _KERNEL
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;
84 if ((error = dmu_buf_hold(ZTOZSB(zp)->z_os, zp->z_id,
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()) {
100 VERIFY(dmu_set_bonus(db,
101 len + ZFS_OLD_ZNODE_PHYS_SIZE, tx) == 0);
102 if (len) {
103 bcopy(link, (caddr_t)db->db_data +
104 ZFS_OLD_ZNODE_PHYS_SIZE, len);
105 }
106 } else {
107 dmu_buf_t *dbp;
108
109 zfs_grow_blocksize(zp, len, tx);
110 VERIFY(0 == dmu_buf_hold(ZTOZSB(zp)->z_os,
111 zp->z_id, 0, FTAG, &dbp, DMU_READ_NO_PREFETCH));
112
113 dmu_buf_will_dirty(dbp, tx);
114
115 ASSERT3U(len, <=, dbp->db_size);
116 bcopy(link, dbp->db_data, len);
117 dmu_buf_rele(dbp, FTAG);
118 }
119}
120
121void
122zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
123{
124 zfs_sb_t *zsb = ZTOZSB(zp);
125 xoptattr_t *xoap;
126
127 ASSERT(MUTEX_HELD(&zp->z_lock));
128 VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
129 if (zp->z_is_sa) {
130 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zsb),
131 &xoap->xoa_av_scanstamp,
132 sizeof (xoap->xoa_av_scanstamp)) != 0)
133 return;
134 } else {
135 dmu_object_info_t doi;
136 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
137 int len;
138
139 if (!(zp->z_pflags & ZFS_BONUS_SCANSTAMP))
140 return;
141
142 sa_object_info(zp->z_sa_hdl, &doi);
143 len = sizeof (xoap->xoa_av_scanstamp) +
144 ZFS_OLD_ZNODE_PHYS_SIZE;
145
146 if (len <= doi.doi_bonus_size) {
147 (void) memcpy(xoap->xoa_av_scanstamp,
148 (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
149 sizeof (xoap->xoa_av_scanstamp));
150 }
151 }
152 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
153}
154
155void
156zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
157{
158 zfs_sb_t *zsb = ZTOZSB(zp);
159 xoptattr_t *xoap;
160
161 ASSERT(MUTEX_HELD(&zp->z_lock));
162 VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
163 if (zp->z_is_sa)
164 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zsb),
165 &xoap->xoa_av_scanstamp,
166 sizeof (xoap->xoa_av_scanstamp), tx));
167 else {
168 dmu_object_info_t doi;
169 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
170 int len;
171
172 sa_object_info(zp->z_sa_hdl, &doi);
173 len = sizeof (xoap->xoa_av_scanstamp) +
174 ZFS_OLD_ZNODE_PHYS_SIZE;
175 if (len > doi.doi_bonus_size)
176 VERIFY(dmu_set_bonus(db, len, tx) == 0);
177 (void) memcpy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
178 xoap->xoa_av_scanstamp, sizeof (xoap->xoa_av_scanstamp));
179
180 zp->z_pflags |= ZFS_BONUS_SCANSTAMP;
181 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
182 &zp->z_pflags, sizeof (uint64_t), tx));
183 }
184}
185
186int
187zfs_sa_get_xattr(znode_t *zp)
188{
189 zfs_sb_t *zsb = ZTOZSB(zp);
190 char *obj;
191 int size;
192 int error;
193
194 ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
195 ASSERT(!zp->z_xattr_cached);
196 ASSERT(zp->z_is_sa);
197
198 error = sa_size(zp->z_sa_hdl, SA_ZPL_DXATTR(zsb), &size);
199 if (error) {
200 if (error == ENOENT)
201 return nvlist_alloc(&zp->z_xattr_cached,
202 NV_UNIQUE_NAME, KM_SLEEP);
203 else
204 return (error);
205 }
206
207 obj = zio_buf_alloc(size);
208
209 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DXATTR(zsb), obj, size);
210 if (error == 0)
211 error = nvlist_unpack(obj, size, &zp->z_xattr_cached, KM_SLEEP);
212
213 zio_buf_free(obj, size);
214
215 return (error);
216}
217
218int
219zfs_sa_set_xattr(znode_t *zp)
220{
221 zfs_sb_t *zsb = ZTOZSB(zp);
222 dmu_tx_t *tx;
223 char *obj;
224 size_t size;
225 int error;
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 = EFBIG;
234 if (error)
235 goto out;
236
237 obj = zio_buf_alloc(size);
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 tx = dmu_tx_create(zsb->z_os);
245 dmu_tx_hold_sa_create(tx, size);
246 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
247
248 error = dmu_tx_assign(tx, TXG_WAIT);
249 if (error) {
250 dmu_tx_abort(tx);
251 } else {
252 VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_DXATTR(zsb),
253 obj, size, tx));
254 dmu_tx_commit(tx);
255 }
256out_free:
257 zio_buf_free(obj, size);
258out:
259 return (error);
260}
261
262/*
263 * I'm not convinced we should do any of this upgrade.
264 * since the SA code can read both old/new znode formats
265 * with probably little to no performance difference.
266 *
267 * All new files will be created with the new format.
268 */
269
270void
271zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
272{
273 dmu_buf_t *db = sa_get_db(hdl);
274 znode_t *zp = sa_get_userdata(hdl);
275 zfs_sb_t *zsb = ZTOZSB(zp);
276 int count = 0;
277 sa_bulk_attr_t *bulk, *sa_attrs;
278 zfs_acl_locator_cb_t locate = { 0 };
279 uint64_t uid, gid, mode, rdev, xattr, parent;
280 uint64_t crtime[2], mtime[2], ctime[2], atime[2];
281 zfs_acl_phys_t znode_acl;
282 char scanstamp[AV_SCANSTAMP_SZ];
283 boolean_t drop_lock = B_FALSE;
284
285 /*
286 * No upgrade if ACL isn't cached
287 * since we won't know which locks are held
288 * and ready the ACL would require special "locked"
289 * interfaces that would be messy
290 */
291 if (zp->z_acl_cached == NULL || S_ISLNK(ZTOI(zp)->i_mode))
292 return;
293
294 /*
295 * If the z_lock is held and we aren't the owner
296 * the just return since we don't want to deadlock
297 * trying to update the status of z_is_sa. This
298 * file can then be upgraded at a later time.
299 *
300 * Otherwise, we know we are doing the
301 * sa_update() that caused us to enter this function.
302 */
303 if (mutex_owner(&zp->z_lock) != curthread) {
304 if (mutex_tryenter(&zp->z_lock) == 0)
305 return;
306 else
307 drop_lock = B_TRUE;
308 }
309
310 /* First do a bulk query of the attributes that aren't cached */
311 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 20, KM_SLEEP);
312 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
313 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
314 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
315 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zsb), NULL, &crtime, 16);
316 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &mode, 8);
317 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL, &parent, 8);
318 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zsb), NULL, &xattr, 8);
319 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zsb), NULL, &rdev, 8);
320 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &uid, 8);
321 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &gid, 8);
322 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zsb), NULL,
323 &znode_acl, 88);
324
325 if (sa_bulk_lookup_locked(hdl, bulk, count) != 0) {
326 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 20);
327 goto done;
328 }
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;
335 sa_attrs = kmem_zalloc(sizeof (sa_bulk_attr_t) * 20, KM_SLEEP);
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,
338 &zp->z_size, 8);
339 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zsb),
340 NULL, &zp->z_gen, 8);
341 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zsb), NULL, &uid, 8);
342 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zsb), NULL, &gid, 8);
343 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zsb),
344 NULL, &parent, 8);
345 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zsb), NULL,
346 &zp->z_pflags, 8);
347 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zsb), NULL,
348 &atime, 16);
349 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zsb), NULL,
350 &mtime, 16);
351 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zsb), NULL,
352 &ctime, 16);
353 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zsb), NULL,
354 &crtime, 16);
355 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zsb), NULL,
356 &zp->z_links, 8);
357 if (S_ISBLK(ZTOI(zp)->i_mode) || S_ISCHR(ZTOI(zp)->i_mode))
358 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zsb), NULL,
359 &rdev, 8);
360 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zsb), NULL,
361 &zp->z_acl_cached->z_acl_count, 8);
362
363 if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
364 zfs_acl_xform(zp, zp->z_acl_cached, CRED());
365
366 locate.cb_aclp = zp->z_acl_cached;
367 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zsb),
368 zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
369
370 if (xattr)
371 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zsb),
372 NULL, &xattr, 8);
373
374 /* if scanstamp then add scanstamp */
375
376 if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
377 bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
378 scanstamp, AV_SCANSTAMP_SZ);
379 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zsb),
380 NULL, scanstamp, AV_SCANSTAMP_SZ);
381 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
382 }
383
384 VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
385 VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
386 count, tx) == 0);
387 if (znode_acl.z_acl_extern_obj)
388 VERIFY(0 == dmu_object_free(zsb->z_os,
389 znode_acl.z_acl_extern_obj, tx));
390
391 zp->z_is_sa = B_TRUE;
392 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * 20);
393 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 20);
394done:
395 if (drop_lock)
396 mutex_exit(&zp->z_lock);
397}
398
399void
400zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
401{
402 if (!ZTOZSB(zp)->z_use_sa || zp->z_is_sa)
403 return;
404
405
406 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
407
408 if (zfs_external_acl(zp)) {
409 dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
410 DMU_OBJECT_END);
411 }
412}
413
414EXPORT_SYMBOL(zfs_attr_table);
415EXPORT_SYMBOL(zfs_sa_readlink);
416EXPORT_SYMBOL(zfs_sa_symlink);
417EXPORT_SYMBOL(zfs_sa_get_scanstamp);
418EXPORT_SYMBOL(zfs_sa_set_scanstamp);
419EXPORT_SYMBOL(zfs_sa_get_xattr);
420EXPORT_SYMBOL(zfs_sa_set_xattr);
421EXPORT_SYMBOL(zfs_sa_upgrade);
422EXPORT_SYMBOL(zfs_sa_upgrade_txholds);
423
424#endif