]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/zfs/zfs_sa.c
Remove races from scrub / resilver tests
[FreeBSD/FreeBSD.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/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 #include <sys/dmu_objset.h>
31 #include <sys/sa_impl.h>
32
33 /*
34  * ZPL attribute registration table.
35  * Order of attributes doesn't matter
36  * a unique value will be assigned for each
37  * attribute that is file system specific
38  *
39  * This is just the set of ZPL attributes that this
40  * version of ZFS deals with natively.  The file system
41  * could have other attributes stored in files, but they will be
42  * ignored.  The SA framework will preserve them, just that
43  * this version of ZFS won't change or delete them.
44  */
45
46 sa_attr_reg_t zfs_attr_table[ZPL_END+1] = {
47         {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
48         {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
49         {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
50         {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
51         {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
52         {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
53         {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
54         {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
55         {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
56         {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
57         {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
58         {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
59         {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
60         {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
61         {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
62         {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
63         {"ZPL_DACL_COUNT", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
64         {"ZPL_SYMLINK", 0, SA_UINT8_ARRAY, 0},
65         {"ZPL_SCANSTAMP", 32, SA_UINT8_ARRAY, 0},
66         {"ZPL_DACL_ACES", 0, SA_ACL, 0},
67         {"ZPL_DXATTR", 0, SA_UINT8_ARRAY, 0},
68         {"ZPL_PROJID", sizeof (uint64_t), SA_UINT64_ARRAY, 0},
69         {NULL, 0, 0, 0}
70 };
71
72 #ifdef _KERNEL
73 int
74 zfs_sa_readlink(znode_t *zp, uio_t *uio)
75 {
76         dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
77         size_t bufsz;
78         int error;
79
80         bufsz = zp->z_size;
81         if (bufsz + ZFS_OLD_ZNODE_PHYS_SIZE <= db->db_size) {
82                 error = uiomove((caddr_t)db->db_data +
83                     ZFS_OLD_ZNODE_PHYS_SIZE,
84                     MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
85         } else {
86                 dmu_buf_t *dbp;
87                 if ((error = dmu_buf_hold(ZTOZSB(zp)->z_os, zp->z_id,
88                     0, FTAG, &dbp, DMU_READ_NO_PREFETCH)) == 0) {
89                         error = uiomove(dbp->db_data,
90                             MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
91                         dmu_buf_rele(dbp, FTAG);
92                 }
93         }
94         return (error);
95 }
96
97 void
98 zfs_sa_symlink(znode_t *zp, char *link, int len, dmu_tx_t *tx)
99 {
100         dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
101
102         if (ZFS_OLD_ZNODE_PHYS_SIZE + len <= dmu_bonus_max()) {
103                 VERIFY0(dmu_set_bonus(db, len + ZFS_OLD_ZNODE_PHYS_SIZE, tx));
104                 if (len) {
105                         bcopy(link, (caddr_t)db->db_data +
106                             ZFS_OLD_ZNODE_PHYS_SIZE, len);
107                 }
108         } else {
109                 dmu_buf_t *dbp;
110
111                 zfs_grow_blocksize(zp, len, tx);
112                 VERIFY0(dmu_buf_hold(ZTOZSB(zp)->z_os, zp->z_id, 0, FTAG, &dbp,
113                     DMU_READ_NO_PREFETCH));
114
115                 dmu_buf_will_dirty(dbp, tx);
116
117                 ASSERT3U(len, <=, dbp->db_size);
118                 bcopy(link, dbp->db_data, len);
119                 dmu_buf_rele(dbp, FTAG);
120         }
121 }
122
123 void
124 zfs_sa_get_scanstamp(znode_t *zp, xvattr_t *xvap)
125 {
126         zfsvfs_t *zfsvfs = ZTOZSB(zp);
127         xoptattr_t *xoap;
128
129         ASSERT(MUTEX_HELD(&zp->z_lock));
130         VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
131         if (zp->z_is_sa) {
132                 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
133                     &xoap->xoa_av_scanstamp,
134                     sizeof (xoap->xoa_av_scanstamp)) != 0)
135                         return;
136         } else {
137                 dmu_object_info_t doi;
138                 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
139                 int len;
140
141                 if (!(zp->z_pflags & ZFS_BONUS_SCANSTAMP))
142                         return;
143
144                 sa_object_info(zp->z_sa_hdl, &doi);
145                 len = sizeof (xoap->xoa_av_scanstamp) +
146                     ZFS_OLD_ZNODE_PHYS_SIZE;
147
148                 if (len <= doi.doi_bonus_size) {
149                         (void) memcpy(xoap->xoa_av_scanstamp,
150                             (caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
151                             sizeof (xoap->xoa_av_scanstamp));
152                 }
153         }
154         XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
155 }
156
157 void
158 zfs_sa_set_scanstamp(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
159 {
160         zfsvfs_t *zfsvfs = ZTOZSB(zp);
161         xoptattr_t *xoap;
162
163         ASSERT(MUTEX_HELD(&zp->z_lock));
164         VERIFY((xoap = xva_getxoptattr(xvap)) != NULL);
165         if (zp->z_is_sa)
166                 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SCANSTAMP(zfsvfs),
167                     &xoap->xoa_av_scanstamp,
168                     sizeof (xoap->xoa_av_scanstamp), tx));
169         else {
170                 dmu_object_info_t doi;
171                 dmu_buf_t *db = sa_get_db(zp->z_sa_hdl);
172                 int len;
173
174                 sa_object_info(zp->z_sa_hdl, &doi);
175                 len = sizeof (xoap->xoa_av_scanstamp) +
176                     ZFS_OLD_ZNODE_PHYS_SIZE;
177                 if (len > doi.doi_bonus_size)
178                         VERIFY(dmu_set_bonus(db, len, tx) == 0);
179                 (void) memcpy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
180                     xoap->xoa_av_scanstamp, sizeof (xoap->xoa_av_scanstamp));
181
182                 zp->z_pflags |= ZFS_BONUS_SCANSTAMP;
183                 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
184                     &zp->z_pflags, sizeof (uint64_t), tx));
185         }
186 }
187
188 int
189 zfs_sa_get_xattr(znode_t *zp)
190 {
191         zfsvfs_t *zfsvfs = ZTOZSB(zp);
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_size(zp->z_sa_hdl, SA_ZPL_DXATTR(zfsvfs), &size);
201         if (error) {
202                 if (error == ENOENT)
203                         return nvlist_alloc(&zp->z_xattr_cached,
204                             NV_UNIQUE_NAME, KM_SLEEP);
205                 else
206                         return (error);
207         }
208
209         obj = vmem_alloc(size, KM_SLEEP);
210
211         error = sa_lookup(zp->z_sa_hdl, SA_ZPL_DXATTR(zfsvfs), obj, size);
212         if (error == 0)
213                 error = nvlist_unpack(obj, size, &zp->z_xattr_cached, KM_SLEEP);
214
215         vmem_free(obj, size);
216
217         return (error);
218 }
219
220 int
221 zfs_sa_set_xattr(znode_t *zp)
222 {
223         zfsvfs_t *zfsvfs = ZTOZSB(zp);
224         dmu_tx_t *tx;
225         char *obj;
226         size_t size;
227         int error;
228
229         ASSERT(RW_WRITE_HELD(&zp->z_xattr_lock));
230         ASSERT(zp->z_xattr_cached);
231         ASSERT(zp->z_is_sa);
232
233         error = nvlist_size(zp->z_xattr_cached, &size, NV_ENCODE_XDR);
234         if ((error == 0) && (size > SA_ATTR_MAX_LEN))
235                 error = SET_ERROR(EFBIG);
236         if (error)
237                 goto out;
238
239         obj = vmem_alloc(size, KM_SLEEP);
240
241         error = nvlist_pack(zp->z_xattr_cached, &obj, &size,
242             NV_ENCODE_XDR, KM_SLEEP);
243         if (error)
244                 goto out_free;
245
246         tx = dmu_tx_create(zfsvfs->z_os);
247         dmu_tx_hold_sa_create(tx, size);
248         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
249
250         error = dmu_tx_assign(tx, TXG_WAIT);
251         if (error) {
252                 dmu_tx_abort(tx);
253         } else {
254                 int count = 0;
255                 sa_bulk_attr_t bulk[2];
256                 uint64_t ctime[2];
257
258                 zfs_tstamp_update_setup(zp, STATE_CHANGED, NULL, ctime);
259                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_DXATTR(zfsvfs),
260                     NULL, obj, size);
261                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
262                     NULL, &ctime, 16);
263                 VERIFY0(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx));
264
265                 dmu_tx_commit(tx);
266         }
267 out_free:
268         vmem_free(obj, size);
269 out:
270         return (error);
271 }
272
273 /*
274  * I'm not convinced we should do any of this upgrade.
275  * since the SA code can read both old/new znode formats
276  * with probably little to no performance difference.
277  *
278  * All new files will be created with the new format.
279  */
280
281 void
282 zfs_sa_upgrade(sa_handle_t *hdl, dmu_tx_t *tx)
283 {
284         dmu_buf_t *db = sa_get_db(hdl);
285         znode_t *zp = sa_get_userdata(hdl);
286         zfsvfs_t *zfsvfs = ZTOZSB(zp);
287         int count = 0;
288         sa_bulk_attr_t *bulk, *sa_attrs;
289         zfs_acl_locator_cb_t locate = { 0 };
290         uint64_t uid, gid, mode, rdev, xattr, parent, tmp_gen;
291         uint64_t crtime[2], mtime[2], ctime[2], atime[2];
292         uint64_t links;
293         zfs_acl_phys_t znode_acl;
294         char scanstamp[AV_SCANSTAMP_SZ];
295         boolean_t drop_lock = B_FALSE;
296
297         /*
298          * No upgrade if ACL isn't cached
299          * since we won't know which locks are held
300          * and ready the ACL would require special "locked"
301          * interfaces that would be messy
302          */
303         if (zp->z_acl_cached == NULL || S_ISLNK(ZTOI(zp)->i_mode))
304                 return;
305
306         /*
307          * If the z_lock is held and we aren't the owner
308          * the just return since we don't want to deadlock
309          * trying to update the status of z_is_sa.  This
310          * file can then be upgraded at a later time.
311          *
312          * Otherwise, we know we are doing the
313          * sa_update() that caused us to enter this function.
314          */
315         if (MUTEX_NOT_HELD(&zp->z_lock)) {
316                 if (mutex_tryenter(&zp->z_lock) == 0)
317                         return;
318                 else
319                         drop_lock = B_TRUE;
320         }
321
322         /* First do a bulk query of the attributes that aren't cached */
323         bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
324         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16);
325         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
326         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
327         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CRTIME(zfsvfs), NULL, &crtime, 16);
328         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
329         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
330         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_XATTR(zfsvfs), NULL, &xattr, 8);
331         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_RDEV(zfsvfs), NULL, &rdev, 8);
332         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
333         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
334         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &tmp_gen, 8);
335         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
336             &znode_acl, 88);
337
338         if (sa_bulk_lookup_locked(hdl, bulk, count) != 0)
339                 goto done;
340
341         if (dmu_objset_projectquota_enabled(hdl->sa_os) &&
342             !(zp->z_pflags & ZFS_PROJID)) {
343                 zp->z_pflags |= ZFS_PROJID;
344                 zp->z_projid = ZFS_DEFAULT_PROJID;
345         }
346
347         /*
348          * While the order here doesn't matter its best to try and organize
349          * it is such a way to pick up an already existing layout number
350          */
351         count = 0;
352         sa_attrs = kmem_zalloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_SLEEP);
353         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
354         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SIZE(zfsvfs), NULL,
355             &zp->z_size, 8);
356         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GEN(zfsvfs),
357             NULL, &tmp_gen, 8);
358         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_UID(zfsvfs), NULL, &uid, 8);
359         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_GID(zfsvfs), NULL, &gid, 8);
360         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PARENT(zfsvfs),
361             NULL, &parent, 8);
362         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_FLAGS(zfsvfs), NULL,
363             &zp->z_pflags, 8);
364         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_ATIME(zfsvfs), NULL,
365             &atime, 16);
366         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_MTIME(zfsvfs), NULL,
367             &mtime, 16);
368         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CTIME(zfsvfs), NULL,
369             &ctime, 16);
370         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_CRTIME(zfsvfs), NULL,
371             &crtime, 16);
372         links = ZTOI(zp)->i_nlink;
373         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_LINKS(zfsvfs), NULL,
374             &links, 8);
375         if (dmu_objset_projectquota_enabled(hdl->sa_os))
376                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_PROJID(zfsvfs), NULL,
377                     &zp->z_projid, 8);
378         if (S_ISBLK(ZTOI(zp)->i_mode) || S_ISCHR(ZTOI(zp)->i_mode))
379                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_RDEV(zfsvfs), NULL,
380                     &rdev, 8);
381         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
382             &zp->z_acl_cached->z_acl_count, 8);
383
384         if (zp->z_acl_cached->z_version < ZFS_ACL_VERSION_FUID)
385                 zfs_acl_xform(zp, zp->z_acl_cached, CRED());
386
387         locate.cb_aclp = zp->z_acl_cached;
388         SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_DACL_ACES(zfsvfs),
389             zfs_acl_data_locator, &locate, zp->z_acl_cached->z_acl_bytes);
390
391         if (xattr)
392                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_XATTR(zfsvfs),
393                     NULL, &xattr, 8);
394
395         /* if scanstamp then add scanstamp */
396
397         if (zp->z_pflags & ZFS_BONUS_SCANSTAMP) {
398                 bcopy((caddr_t)db->db_data + ZFS_OLD_ZNODE_PHYS_SIZE,
399                     scanstamp, AV_SCANSTAMP_SZ);
400                 SA_ADD_BULK_ATTR(sa_attrs, count, SA_ZPL_SCANSTAMP(zfsvfs),
401                     NULL, scanstamp, AV_SCANSTAMP_SZ);
402                 zp->z_pflags &= ~ZFS_BONUS_SCANSTAMP;
403         }
404
405         VERIFY(dmu_set_bonustype(db, DMU_OT_SA, tx) == 0);
406         VERIFY(sa_replace_all_by_template_locked(hdl, sa_attrs,
407             count, tx) == 0);
408         if (znode_acl.z_acl_extern_obj)
409                 VERIFY(0 == dmu_object_free(zfsvfs->z_os,
410                     znode_acl.z_acl_extern_obj, tx));
411
412         zp->z_is_sa = B_TRUE;
413         kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
414 done:
415         kmem_free(bulk, sizeof (sa_bulk_attr_t) * ZPL_END);
416         if (drop_lock)
417                 mutex_exit(&zp->z_lock);
418 }
419
420 void
421 zfs_sa_upgrade_txholds(dmu_tx_t *tx, znode_t *zp)
422 {
423         if (!ZTOZSB(zp)->z_use_sa || zp->z_is_sa)
424                 return;
425
426
427         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
428
429         if (zfs_external_acl(zp)) {
430                 dmu_tx_hold_free(tx, zfs_external_acl(zp), 0,
431                     DMU_OBJECT_END);
432         }
433 }
434
435 EXPORT_SYMBOL(zfs_attr_table);
436 EXPORT_SYMBOL(zfs_sa_readlink);
437 EXPORT_SYMBOL(zfs_sa_symlink);
438 EXPORT_SYMBOL(zfs_sa_get_scanstamp);
439 EXPORT_SYMBOL(zfs_sa_set_scanstamp);
440 EXPORT_SYMBOL(zfs_sa_get_xattr);
441 EXPORT_SYMBOL(zfs_sa_set_xattr);
442 EXPORT_SYMBOL(zfs_sa_upgrade);
443 EXPORT_SYMBOL(zfs_sa_upgrade_txholds);
444
445 #endif