Use SA_HDL_PRIVATE for SA xattrs
[zfs.git] / module / zfs / zfs_sa.c
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/types.h>
26 #include <sys/param.h>
27 #include <sys/vnode.h>
28 #include <sys/sa.h>
29 #include <sys/zfs_acl.h>
30 #include <sys/zfs_sa.h>
31
32 /*
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
37  *
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.
43  */
44
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},
67         {NULL, 0, 0, 0}
68 };
69
70 #ifdef _KERNEL
71 int
72 zfs_sa_readlink(znode_t *zp, uio_t *uio)
73 {
74         dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
75         size_t bufsz;
76         int error;
77
78         bufsz = zp->z_size;
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);
83         } else {
84                 dmu_buf_t *dbp;
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);
90                 }
91         }
92         return (error);
93 }
94
95 void
96 zfs_sa_symlink(znode_t *zp, char *link, int len, dmu_tx_t *tx)
97 {
98         dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
99
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);
103                 if (len) {
104                         bcopy(link, (caddr_t)db->db_data +
105                             ZFS_OLD_ZNODE_PHYS_SIZE, len);
106                 }
107         } else {
108                 dmu_buf_t *dbp;
109
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));
113
114                 dmu_buf_will_dirty(dbp, tx);
115
116                 ASSERT3U(len, <=, dbp->db_size);
117                 bcopy(link, dbp->db_data, len);
118                 dmu_buf_rele(dbp, FTAG);
119         }
120 }
121
122 void
123 zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
124 {
125         zfs_sb_t *zsb = ZTOZSB(zp);
126         xoptattr_t *xoap;
127
128         ASSERT(MUTEX_HELD(&zp->z_lock));
129         VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
130         if (zp->z_is_sa) {
131                 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zsb),
132                     &xoap->xoa_av_scanstamp,
133                     sizeof (xoap->xoa_av_scanstamp)) != 0)
134                         return;
135         } else {
136                 dmu_object_info_t doi;
137                 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
138                 int len;
139
140                 if (!(zp->z_pflags & ZFS_BONUS_SCANSTAMP))
141                         return;
142
143                 sa_object_info(zp->z_sa_hdl, &doi);
144                 len = sizeof (xoap->xoa_av_scanstamp) +
145                     ZFS_OLD_ZNODE_PHYS_SIZE;
146
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));
151                 }
152         }
153         XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
154 }
155
156 void
157 zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
158 {
159         zfs_sb_t *zsb = ZTOZSB(zp);
160         xoptattr_t *xoap;
161
162         ASSERT(MUTEX_HELD(&zp->z_lock));
163         VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
164         if (zp->z_is_sa)
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));
168         else {
169                 dmu_object_info_t doi;
170                 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
171                 int len;
172
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));
180
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));
184         }
185 }
186
187 int
188 zfs_sa_get_xattr(znode_t *zp)
189 {
190         zfs_sb_t *zsb = ZTOZSB(zp);
191         sa_handle_t *sa;
192         char *obj;
193         int size;
194         int error;
195
196         ASSERT(RW_LOCK_HELD(&zp->z_xattr_lock));
197         ASSERT(!zp->z_xattr_cached);
198         ASSERT(zp->z_is_sa);
199
200         error = sa_handle_get(zsb->z_os, zp->z_id, NULL, SA_HDL_PRIVATE, &sa);
201         if (error)
202                 return (error);
203
204         error = sa_size(sa, SA_ZPL_DXATTR(zsb), &size);
205         if (error) {
206                 sa_handle_destroy(sa);
207
208                 if (error == ENOENT)
209                         return nvlist_alloc(&zp->z_xattr_cached,
210                             NV_UNIQUE_NAME, KM_SLEEP);
211                 else
212                         return (error);
213         }
214
215         obj = sa_spill_alloc(KM_SLEEP);
216
217         error = sa_lookup(sa, SA_ZPL_DXATTR(zsb), obj, size);
218         if (error == 0)
219                 error = nvlist_unpack(obj, size, &zp->z_xattr_cached, KM_SLEEP);
220
221         sa_spill_free(obj);
222         sa_handle_destroy(sa);
223
224         return (error);
225 }
226
227 int
228 zfs_sa_set_xattr(znode_t *zp)
229 {
230         zfs_sb_t *zsb = ZTOZSB(zp);
231         sa_handle_t *sa;
232         dmu_tx_t *tx;
233         char *obj;
234         size_t size;
235         int error;
236
237         ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
238         ASSERT(zp->z_xattr_cached);
239         ASSERT(zp->z_is_sa);
240
241         error = nvlist_size(zp->z_xattr_cached, &size, NV_ENCODE_XDR);
242         if (error)
243                 goto out;
244
245         obj = sa_spill_alloc(KM_SLEEP);
246
247         error = nvlist_pack(zp->z_xattr_cached, &obj, &size,
248             NV_ENCODE_XDR, KM_SLEEP);
249         if (error)
250                 goto out_free;
251
252         /*
253          * A private SA handle must be used to ensure we can drop the hold
254          * on the spill block prior to calling dmu_tx_commit().  If we call
255          * dmu_tx_commit() before sa_handle_destroy(), then our hold will
256          * trigger a copy of the buffer at txg sync time.  This is done to
257          * prevent data from leaking in to the syncing txg.  As a result
258          * the original dirty spill block will be remain dirty in the arc
259          * while the copy is written and laundered.
260          */
261         error = sa_handle_get(zsb->z_os, zp->z_id, NULL, SA_HDL_PRIVATE, &sa);
262         if (error)
263                 goto out_free;
264
265         tx = dmu_tx_create(zsb->z_os);
266         dmu_tx_hold_sa_create(tx, size);
267         dmu_tx_hold_sa(tx, sa, B_TRUE);
268
269         error = dmu_tx_assign(tx, TXG_WAIT);
270         if (error) {
271                 dmu_tx_abort(tx);
272                 sa_handle_destroy(sa);
273         } else {
274                 error = sa_update(sa, SA_ZPL_DXATTR(zsb), obj, size, tx);
275                 sa_handle_destroy(sa);
276                 if (error)
277                         dmu_tx_abort(tx);
278                 else
279                         dmu_tx_commit(tx);
280         }
281 out_free:
282         sa_spill_free(obj);
283 out:
284         return (error);
285 }
286
287 /*
288  * I'm not convinced we should do any of this upgrade.
289  * since the SA code can read both old/new znode formats
290  * with probably little to know performance difference.
291  *
292  * All new files will be created with the new format.
293  */
294
295 void
296 zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
297 {
298         dmu_buf_t *db = sa_get_db(hdl);
299         znode_t *zp = sa_get_userdata(hdl);
300         zfs_sb_t *zsb = ZTOZSB(zp);
301         int count = 0;
302         sa_bulk_attr_t *bulk, *sa_attrs;
303         zfs_acl_locator_cb_t locate = { 0 };
304         uint64_t uid, gid, mode, rdev, xattr, parent;
305         uint64_t crtime[2], mtime[2], ctime[2];
306         zfs_acl_phys_t znode_acl;
307         char scanstamp[AV_SCANSTAMP_SZ];
308         boolean_t drop_lock = B_FALSE;
309
310         /*
311          * No upgrade if ACL isn't cached
312          * since we won't know which locks are held
313          * and ready the ACL would require special "locked"
314          * interfaces that would be messy
315          */
316         if (zp->z_acl_cached == NULL || S_ISLNK(ZTOI(zp)->i_mode))
317                 return;
318
319         /*
320          * If the z_lock is held and we aren't the owner
321          * the just return since we don't want to deadlock
322          * trying to update the status of z_is_sa.  This
323          * file can then be upgraded at a later time.
324          *
325          * Otherwise, we know we are doing the
326          * sa_update() that caused us to enter this function.
327          */
328         if (mutex_owner(&zp->z_lock) != curthread) {
329                 if (mutex_tryenter(&zp->z_lock) == 0)
330                         return;
331                 else
332                         drop_lock = B_TRUE;
333         }
334
335         /* First do a bulk query of the attributes that aren't cached */
336         bulk = kmem_alloc(sizeof(sa_bulk_attr_t) * 20, KM_SLEEP);
337         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
338         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
339         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zsb), NULL, &crtime, 16);
340         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &mode, 8);
341         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL, &parent, 8);
342         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zsb), NULL, &xattr, 8);
343         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zsb), NULL, &rdev, 8);
344         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &uid, 8);
345         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &gid, 8);
346         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zsb), NULL,
347             &znode_acl, 88);
348
349         if (sa_bulk_lookup_locked(hdl, bulk, count) != 0) {
350                 kmem_free(bulk, sizeof(sa_bulk_attr_t) * 20);
351                 goto done;
352         }
353
354         /*
355          * While the order here doesn't matter its best to try and organize
356          * it is such a way to pick up an already existing layout number
357          */
358         count = 0;
359         sa_attrs = kmem_zalloc(sizeof(sa_bulk_attr_t) * 20, KM_SLEEP);
360         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zsb), NULL, &mode, 8);
361         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zsb), NULL,
362             &zp->z_size, 8);
363         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zsb),
364             NULL, &zp->z_gen, 8);
365         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zsb), NULL, &uid, 8);
366         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zsb), NULL, &gid, 8);
367         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zsb),
368             NULL, &parent, 8);
369         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zsb), NULL,
370             &zp->z_pflags, 8);
371         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zsb), NULL,
372             zp->z_atime, 16);
373         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zsb), NULL,
374             &mtime, 16);
375         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zsb), NULL,
376             &ctime, 16);
377         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zsb), NULL,
378             &crtime, 16);
379         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zsb), NULL,
380             &zp->z_links, 8);
381         if (S_ISBLK(ZTOI(zp)->i_mode) || S_ISCHR(ZTOI(zp)->i_mode))
382                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zsb), NULL,
383                     &rdev, 8);
384         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zsb), NULL,
385             &zp->z_acl_cached->z_acl_count, 8);
386
387         if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
388                 zfs_acl_xform(zp, zp->z_acl_cached, CRED());
389
390         locate.cb_aclp = zp->z_acl_cached;
391         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zsb),
392             zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
393
394         if (xattr)
395                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zsb),
396                     NULL, &xattr, 8);
397
398         /* if scanstamp then add scanstamp */
399
400         if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
401                 bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
402                     scanstamp, AV_SCANSTAMP_SZ);
403                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zsb),
404                     NULL, scanstamp, AV_SCANSTAMP_SZ);
405                 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
406         }
407
408         VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
409         VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
410             count, tx) == 0);
411         if (znode_acl.z_acl_extern_obj)
412                 VERIFY(0 == dmu_object_free(zsb->z_os,
413                     znode_acl.z_acl_extern_obj, tx));
414
415         zp->z_is_sa = B_TRUE;
416         kmem_free(sa_attrs, sizeof(sa_bulk_attr_t) * 20);
417         kmem_free(bulk, sizeof(sa_bulk_attr_t) * 20);
418 done:
419         if (drop_lock)
420                 mutex_exit(&zp->z_lock);
421 }
422
423 void
424 zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
425 {
426         if (!ZTOZSB(zp)->z_use_sa || zp->z_is_sa)
427                 return;
428
429
430         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
431
432         if (zfs_external_acl(zp)) {
433                 dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
434                     DMU_OBJECT_END);
435         }
436 }
437
438 EXPORT_SYMBOL(zfs_attr_table);
439 EXPORT_SYMBOL(zfs_sa_readlink);
440 EXPORT_SYMBOL(zfs_sa_symlink);
441 EXPORT_SYMBOL(zfs_sa_get_scanstamp);
442 EXPORT_SYMBOL(zfs_sa_set_scanstamp);
443 EXPORT_SYMBOL(zfs_sa_get_xattr);
444 EXPORT_SYMBOL(zfs_sa_set_xattr);
445 EXPORT_SYMBOL(zfs_sa_upgrade);
446 EXPORT_SYMBOL(zfs_sa_upgrade_txholds);
447
448 #endif