Print this page
5056 ZFS deadlock on db_mtx and dn_holds
Reviewed by: Will Andrews <willa@spectralogic.com>
Reviewed by: Matt Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Approved by: Dan McDonald <danmcd@omniti.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/zfs/sa.c
+++ new/usr/src/uts/common/fs/zfs/sa.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
|
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Portions Copyright 2011 iXsystems, Inc
25 25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 + * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
26 27 */
27 28
28 29 #include <sys/zfs_context.h>
29 30 #include <sys/types.h>
30 31 #include <sys/param.h>
31 32 #include <sys/systm.h>
32 33 #include <sys/sysmacros.h>
33 34 #include <sys/dmu.h>
34 35 #include <sys/dmu_impl.h>
35 36 #include <sys/dmu_objset.h>
36 37 #include <sys/dbuf.h>
37 38 #include <sys/dnode.h>
38 39 #include <sys/zap.h>
39 40 #include <sys/sa.h>
40 41 #include <sys/sunddi.h>
41 42 #include <sys/sa_impl.h>
42 43 #include <sys/dnode.h>
43 44 #include <sys/errno.h>
44 45 #include <sys/zfs_context.h>
45 46
46 47 /*
47 48 * ZFS System attributes:
48 49 *
49 50 * A generic mechanism to allow for arbitrary attributes
50 51 * to be stored in a dnode. The data will be stored in the bonus buffer of
51 52 * the dnode and if necessary a special "spill" block will be used to handle
52 53 * overflow situations. The spill block will be sized to fit the data
53 54 * from 512 - 128K. When a spill block is used the BP (blkptr_t) for the
54 55 * spill block is stored at the end of the current bonus buffer. Any
55 56 * attributes that would be in the way of the blkptr_t will be relocated
56 57 * into the spill block.
57 58 *
58 59 * Attribute registration:
59 60 *
60 61 * Stored persistently on a per dataset basis
61 62 * a mapping between attribute "string" names and their actual attribute
62 63 * numeric values, length, and byteswap function. The names are only used
63 64 * during registration. All attributes are known by their unique attribute
64 65 * id value. If an attribute can have a variable size then the value
65 66 * 0 will be used to indicate this.
66 67 *
67 68 * Attribute Layout:
68 69 *
69 70 * Attribute layouts are a way to compactly store multiple attributes, but
70 71 * without taking the overhead associated with managing each attribute
71 72 * individually. Since you will typically have the same set of attributes
72 73 * stored in the same order a single table will be used to represent that
73 74 * layout. The ZPL for example will usually have only about 10 different
74 75 * layouts (regular files, device files, symlinks,
75 76 * regular files + scanstamp, files/dir with extended attributes, and then
76 77 * you have the possibility of all of those minus ACL, because it would
77 78 * be kicked out into the spill block)
78 79 *
79 80 * Layouts are simply an array of the attributes and their
80 81 * ordering i.e. [0, 1, 4, 5, 2]
81 82 *
82 83 * Each distinct layout is given a unique layout number and that is whats
83 84 * stored in the header at the beginning of the SA data buffer.
84 85 *
85 86 * A layout only covers a single dbuf (bonus or spill). If a set of
86 87 * attributes is split up between the bonus buffer and a spill buffer then
87 88 * two different layouts will be used. This allows us to byteswap the
88 89 * spill without looking at the bonus buffer and keeps the on disk format of
89 90 * the bonus and spill buffer the same.
90 91 *
91 92 * Adding a single attribute will cause the entire set of attributes to
92 93 * be rewritten and could result in a new layout number being constructed
93 94 * as part of the rewrite if no such layout exists for the new set of
94 95 * attribues. The new attribute will be appended to the end of the already
95 96 * existing attributes.
96 97 *
97 98 * Both the attribute registration and attribute layout information are
98 99 * stored in normal ZAP attributes. Their should be a small number of
99 100 * known layouts and the set of attributes is assumed to typically be quite
100 101 * small.
101 102 *
102 103 * The registered attributes and layout "table" information is maintained
103 104 * in core and a special "sa_os_t" is attached to the objset_t.
104 105 *
105 106 * A special interface is provided to allow for quickly applying
106 107 * a large set of attributes at once. sa_replace_all_by_template() is
107 108 * used to set an array of attributes. This is used by the ZPL when
108 109 * creating a brand new file. The template that is passed into the function
109 110 * specifies the attribute, size for variable length attributes, location of
110 111 * data and special "data locator" function if the data isn't in a contiguous
111 112 * location.
112 113 *
113 114 * Byteswap implications:
114 115 *
115 116 * Since the SA attributes are not entirely self describing we can't do
116 117 * the normal byteswap processing. The special ZAP layout attribute and
117 118 * attribute registration attributes define the byteswap function and the
118 119 * size of the attributes, unless it is variable sized.
119 120 * The normal ZFS byteswapping infrastructure assumes you don't need
120 121 * to read any objects in order to do the necessary byteswapping. Whereas
121 122 * SA attributes can only be properly byteswapped if the dataset is opened
122 123 * and the layout/attribute ZAP attributes are available. Because of this
123 124 * the SA attributes will be byteswapped when they are first accessed by
124 125 * the SA code that will read the SA data.
125 126 */
126 127
127 128 typedef void (sa_iterfunc_t)(void *hdr, void *addr, sa_attr_type_t,
128 129 uint16_t length, int length_idx, boolean_t, void *userp);
129 130
130 131 static int sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype);
131 132 static void sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab);
132 133 static void *sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype,
133 134 void *data);
134 135 static void sa_idx_tab_rele(objset_t *os, void *arg);
135 136 static void sa_copy_data(sa_data_locator_t *func, void *start, void *target,
136 137 int buflen);
137 138 static int sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
138 139 sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
139 140 uint16_t buflen, dmu_tx_t *tx);
140 141
141 142 arc_byteswap_func_t *sa_bswap_table[] = {
142 143 byteswap_uint64_array,
143 144 byteswap_uint32_array,
144 145 byteswap_uint16_array,
145 146 byteswap_uint8_array,
146 147 zfs_acl_byteswap,
147 148 };
148 149
149 150 #define SA_COPY_DATA(f, s, t, l) \
150 151 { \
151 152 if (f == NULL) { \
152 153 if (l == 8) { \
153 154 *(uint64_t *)t = *(uint64_t *)s; \
154 155 } else if (l == 16) { \
155 156 *(uint64_t *)t = *(uint64_t *)s; \
156 157 *(uint64_t *)((uintptr_t)t + 8) = \
157 158 *(uint64_t *)((uintptr_t)s + 8); \
158 159 } else { \
159 160 bcopy(s, t, l); \
160 161 } \
161 162 } else \
162 163 sa_copy_data(f, s, t, l); \
163 164 }
164 165
165 166 /*
166 167 * This table is fixed and cannot be changed. Its purpose is to
167 168 * allow the SA code to work with both old/new ZPL file systems.
168 169 * It contains the list of legacy attributes. These attributes aren't
169 170 * stored in the "attribute" registry zap objects, since older ZPL file systems
170 171 * won't have the registry. Only objsets of type ZFS_TYPE_FILESYSTEM will
171 172 * use this static table.
172 173 */
173 174 sa_attr_reg_t sa_legacy_attrs[] = {
174 175 {"ZPL_ATIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 0},
175 176 {"ZPL_MTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 1},
176 177 {"ZPL_CTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 2},
177 178 {"ZPL_CRTIME", sizeof (uint64_t) * 2, SA_UINT64_ARRAY, 3},
178 179 {"ZPL_GEN", sizeof (uint64_t), SA_UINT64_ARRAY, 4},
179 180 {"ZPL_MODE", sizeof (uint64_t), SA_UINT64_ARRAY, 5},
180 181 {"ZPL_SIZE", sizeof (uint64_t), SA_UINT64_ARRAY, 6},
181 182 {"ZPL_PARENT", sizeof (uint64_t), SA_UINT64_ARRAY, 7},
182 183 {"ZPL_LINKS", sizeof (uint64_t), SA_UINT64_ARRAY, 8},
183 184 {"ZPL_XATTR", sizeof (uint64_t), SA_UINT64_ARRAY, 9},
184 185 {"ZPL_RDEV", sizeof (uint64_t), SA_UINT64_ARRAY, 10},
185 186 {"ZPL_FLAGS", sizeof (uint64_t), SA_UINT64_ARRAY, 11},
186 187 {"ZPL_UID", sizeof (uint64_t), SA_UINT64_ARRAY, 12},
187 188 {"ZPL_GID", sizeof (uint64_t), SA_UINT64_ARRAY, 13},
188 189 {"ZPL_PAD", sizeof (uint64_t) * 4, SA_UINT64_ARRAY, 14},
189 190 {"ZPL_ZNODE_ACL", 88, SA_UINT8_ARRAY, 15},
190 191 };
191 192
192 193 /*
193 194 * This is only used for objects of type DMU_OT_ZNODE
194 195 */
195 196 sa_attr_type_t sa_legacy_zpl_layout[] = {
196 197 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
197 198 };
198 199
199 200 /*
200 201 * Special dummy layout used for buffers with no attributes.
201 202 */
202 203 sa_attr_type_t sa_dummy_zpl_layout[] = { 0 };
|
↓ open down ↓ |
167 lines elided |
↑ open up ↑ |
203 204
204 205 static int sa_legacy_attr_count = 16;
205 206 static kmem_cache_t *sa_cache = NULL;
206 207
207 208 /*ARGSUSED*/
208 209 static int
209 210 sa_cache_constructor(void *buf, void *unused, int kmflag)
210 211 {
211 212 sa_handle_t *hdl = buf;
212 213
214 + hdl->sa_dbu.dbu_evict_func = NULL;
213 215 hdl->sa_bonus_tab = NULL;
214 216 hdl->sa_spill_tab = NULL;
215 217 hdl->sa_os = NULL;
216 218 hdl->sa_userp = NULL;
217 219 hdl->sa_bonus = NULL;
218 220 hdl->sa_spill = NULL;
219 221 mutex_init(&hdl->sa_lock, NULL, MUTEX_DEFAULT, NULL);
220 222 return (0);
221 223 }
222 224
223 225 /*ARGSUSED*/
224 226 static void
225 227 sa_cache_destructor(void *buf, void *unused)
226 228 {
227 229 sa_handle_t *hdl = buf;
230 + hdl->sa_dbu.dbu_evict_func = NULL;
228 231 mutex_destroy(&hdl->sa_lock);
229 232 }
230 233
231 234 void
232 235 sa_cache_init(void)
233 236 {
234 237 sa_cache = kmem_cache_create("sa_cache",
235 238 sizeof (sa_handle_t), 0, sa_cache_constructor,
236 239 sa_cache_destructor, NULL, NULL, NULL, 0);
237 240 }
238 241
239 242 void
240 243 sa_cache_fini(void)
241 244 {
242 245 if (sa_cache)
243 246 kmem_cache_destroy(sa_cache);
244 247 }
245 248
246 249 static int
247 250 layout_num_compare(const void *arg1, const void *arg2)
248 251 {
249 252 const sa_lot_t *node1 = arg1;
250 253 const sa_lot_t *node2 = arg2;
251 254
252 255 if (node1->lot_num > node2->lot_num)
253 256 return (1);
254 257 else if (node1->lot_num < node2->lot_num)
255 258 return (-1);
256 259 return (0);
257 260 }
258 261
259 262 static int
260 263 layout_hash_compare(const void *arg1, const void *arg2)
261 264 {
262 265 const sa_lot_t *node1 = arg1;
263 266 const sa_lot_t *node2 = arg2;
264 267
265 268 if (node1->lot_hash > node2->lot_hash)
266 269 return (1);
267 270 if (node1->lot_hash < node2->lot_hash)
268 271 return (-1);
269 272 if (node1->lot_instance > node2->lot_instance)
270 273 return (1);
271 274 if (node1->lot_instance < node2->lot_instance)
272 275 return (-1);
273 276 return (0);
274 277 }
275 278
276 279 boolean_t
277 280 sa_layout_equal(sa_lot_t *tbf, sa_attr_type_t *attrs, int count)
278 281 {
279 282 int i;
280 283
281 284 if (count != tbf->lot_attr_count)
282 285 return (1);
283 286
284 287 for (i = 0; i != count; i++) {
285 288 if (attrs[i] != tbf->lot_attrs[i])
286 289 return (1);
287 290 }
288 291 return (0);
289 292 }
290 293
291 294 #define SA_ATTR_HASH(attr) (zfs_crc64_table[(-1ULL ^ attr) & 0xFF])
292 295
293 296 static uint64_t
294 297 sa_layout_info_hash(sa_attr_type_t *attrs, int attr_count)
295 298 {
296 299 int i;
297 300 uint64_t crc = -1ULL;
298 301
299 302 for (i = 0; i != attr_count; i++)
300 303 crc ^= SA_ATTR_HASH(attrs[i]);
301 304
302 305 return (crc);
303 306 }
304 307
305 308 static int
306 309 sa_get_spill(sa_handle_t *hdl)
307 310 {
308 311 int rc;
309 312 if (hdl->sa_spill == NULL) {
310 313 if ((rc = dmu_spill_hold_existing(hdl->sa_bonus, NULL,
311 314 &hdl->sa_spill)) == 0)
312 315 VERIFY(0 == sa_build_index(hdl, SA_SPILL));
313 316 } else {
314 317 rc = 0;
315 318 }
316 319
317 320 return (rc);
318 321 }
319 322
320 323 /*
321 324 * Main attribute lookup/update function
322 325 * returns 0 for success or non zero for failures
323 326 *
324 327 * Operates on bulk array, first failure will abort further processing
325 328 */
326 329 int
327 330 sa_attr_op(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
328 331 sa_data_op_t data_op, dmu_tx_t *tx)
329 332 {
330 333 sa_os_t *sa = hdl->sa_os->os_sa;
331 334 int i;
332 335 int error = 0;
333 336 sa_buf_type_t buftypes;
334 337
335 338 buftypes = 0;
336 339
337 340 ASSERT(count > 0);
338 341 for (i = 0; i != count; i++) {
339 342 ASSERT(bulk[i].sa_attr <= hdl->sa_os->os_sa->sa_num_attrs);
340 343
341 344 bulk[i].sa_addr = NULL;
342 345 /* First check the bonus buffer */
343 346
344 347 if (hdl->sa_bonus_tab && TOC_ATTR_PRESENT(
345 348 hdl->sa_bonus_tab->sa_idx_tab[bulk[i].sa_attr])) {
346 349 SA_ATTR_INFO(sa, hdl->sa_bonus_tab,
347 350 SA_GET_HDR(hdl, SA_BONUS),
348 351 bulk[i].sa_attr, bulk[i], SA_BONUS, hdl);
349 352 if (tx && !(buftypes & SA_BONUS)) {
350 353 dmu_buf_will_dirty(hdl->sa_bonus, tx);
351 354 buftypes |= SA_BONUS;
352 355 }
353 356 }
354 357 if (bulk[i].sa_addr == NULL &&
355 358 ((error = sa_get_spill(hdl)) == 0)) {
356 359 if (TOC_ATTR_PRESENT(
357 360 hdl->sa_spill_tab->sa_idx_tab[bulk[i].sa_attr])) {
358 361 SA_ATTR_INFO(sa, hdl->sa_spill_tab,
359 362 SA_GET_HDR(hdl, SA_SPILL),
360 363 bulk[i].sa_attr, bulk[i], SA_SPILL, hdl);
361 364 if (tx && !(buftypes & SA_SPILL) &&
362 365 bulk[i].sa_size == bulk[i].sa_length) {
363 366 dmu_buf_will_dirty(hdl->sa_spill, tx);
364 367 buftypes |= SA_SPILL;
365 368 }
366 369 }
367 370 }
368 371 if (error && error != ENOENT) {
369 372 return ((error == ECKSUM) ? EIO : error);
370 373 }
371 374
372 375 switch (data_op) {
373 376 case SA_LOOKUP:
374 377 if (bulk[i].sa_addr == NULL)
375 378 return (SET_ERROR(ENOENT));
376 379 if (bulk[i].sa_data) {
377 380 SA_COPY_DATA(bulk[i].sa_data_func,
378 381 bulk[i].sa_addr, bulk[i].sa_data,
379 382 bulk[i].sa_size);
380 383 }
381 384 continue;
382 385
383 386 case SA_UPDATE:
384 387 /* existing rewrite of attr */
385 388 if (bulk[i].sa_addr &&
386 389 bulk[i].sa_size == bulk[i].sa_length) {
387 390 SA_COPY_DATA(bulk[i].sa_data_func,
388 391 bulk[i].sa_data, bulk[i].sa_addr,
389 392 bulk[i].sa_length);
390 393 continue;
391 394 } else if (bulk[i].sa_addr) { /* attr size change */
392 395 error = sa_modify_attrs(hdl, bulk[i].sa_attr,
393 396 SA_REPLACE, bulk[i].sa_data_func,
394 397 bulk[i].sa_data, bulk[i].sa_length, tx);
395 398 } else { /* adding new attribute */
396 399 error = sa_modify_attrs(hdl, bulk[i].sa_attr,
397 400 SA_ADD, bulk[i].sa_data_func,
398 401 bulk[i].sa_data, bulk[i].sa_length, tx);
399 402 }
400 403 if (error)
401 404 return (error);
402 405 break;
403 406 }
404 407 }
405 408 return (error);
406 409 }
407 410
408 411 static sa_lot_t *
409 412 sa_add_layout_entry(objset_t *os, sa_attr_type_t *attrs, int attr_count,
410 413 uint64_t lot_num, uint64_t hash, boolean_t zapadd, dmu_tx_t *tx)
411 414 {
412 415 sa_os_t *sa = os->os_sa;
413 416 sa_lot_t *tb, *findtb;
414 417 int i;
415 418 avl_index_t loc;
416 419
417 420 ASSERT(MUTEX_HELD(&sa->sa_lock));
418 421 tb = kmem_zalloc(sizeof (sa_lot_t), KM_SLEEP);
419 422 tb->lot_attr_count = attr_count;
420 423 tb->lot_attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
421 424 KM_SLEEP);
422 425 bcopy(attrs, tb->lot_attrs, sizeof (sa_attr_type_t) * attr_count);
423 426 tb->lot_num = lot_num;
424 427 tb->lot_hash = hash;
425 428 tb->lot_instance = 0;
426 429
427 430 if (zapadd) {
428 431 char attr_name[8];
429 432
430 433 if (sa->sa_layout_attr_obj == 0) {
431 434 sa->sa_layout_attr_obj = zap_create_link(os,
432 435 DMU_OT_SA_ATTR_LAYOUTS,
433 436 sa->sa_master_obj, SA_LAYOUTS, tx);
434 437 }
435 438
436 439 (void) snprintf(attr_name, sizeof (attr_name),
437 440 "%d", (int)lot_num);
438 441 VERIFY(0 == zap_update(os, os->os_sa->sa_layout_attr_obj,
439 442 attr_name, 2, attr_count, attrs, tx));
440 443 }
441 444
442 445 list_create(&tb->lot_idx_tab, sizeof (sa_idx_tab_t),
443 446 offsetof(sa_idx_tab_t, sa_next));
444 447
445 448 for (i = 0; i != attr_count; i++) {
446 449 if (sa->sa_attr_table[tb->lot_attrs[i]].sa_length == 0)
447 450 tb->lot_var_sizes++;
448 451 }
449 452
450 453 avl_add(&sa->sa_layout_num_tree, tb);
451 454
452 455 /* verify we don't have a hash collision */
453 456 if ((findtb = avl_find(&sa->sa_layout_hash_tree, tb, &loc)) != NULL) {
454 457 for (; findtb && findtb->lot_hash == hash;
455 458 findtb = AVL_NEXT(&sa->sa_layout_hash_tree, findtb)) {
456 459 if (findtb->lot_instance != tb->lot_instance)
457 460 break;
458 461 tb->lot_instance++;
459 462 }
460 463 }
461 464 avl_add(&sa->sa_layout_hash_tree, tb);
462 465 return (tb);
463 466 }
464 467
465 468 static void
466 469 sa_find_layout(objset_t *os, uint64_t hash, sa_attr_type_t *attrs,
467 470 int count, dmu_tx_t *tx, sa_lot_t **lot)
468 471 {
469 472 sa_lot_t *tb, tbsearch;
470 473 avl_index_t loc;
471 474 sa_os_t *sa = os->os_sa;
472 475 boolean_t found = B_FALSE;
473 476
474 477 mutex_enter(&sa->sa_lock);
475 478 tbsearch.lot_hash = hash;
476 479 tbsearch.lot_instance = 0;
477 480 tb = avl_find(&sa->sa_layout_hash_tree, &tbsearch, &loc);
478 481 if (tb) {
479 482 for (; tb && tb->lot_hash == hash;
480 483 tb = AVL_NEXT(&sa->sa_layout_hash_tree, tb)) {
481 484 if (sa_layout_equal(tb, attrs, count) == 0) {
482 485 found = B_TRUE;
483 486 break;
484 487 }
485 488 }
486 489 }
487 490 if (!found) {
488 491 tb = sa_add_layout_entry(os, attrs, count,
489 492 avl_numnodes(&sa->sa_layout_num_tree), hash, B_TRUE, tx);
490 493 }
491 494 mutex_exit(&sa->sa_lock);
492 495 *lot = tb;
493 496 }
494 497
495 498 static int
496 499 sa_resize_spill(sa_handle_t *hdl, uint32_t size, dmu_tx_t *tx)
497 500 {
498 501 int error;
499 502 uint32_t blocksize;
500 503
501 504 if (size == 0) {
502 505 blocksize = SPA_MINBLOCKSIZE;
503 506 } else if (size > SPA_OLD_MAXBLOCKSIZE) {
504 507 ASSERT(0);
505 508 return (SET_ERROR(EFBIG));
506 509 } else {
507 510 blocksize = P2ROUNDUP_TYPED(size, SPA_MINBLOCKSIZE, uint32_t);
508 511 }
509 512
510 513 error = dbuf_spill_set_blksz(hdl->sa_spill, blocksize, tx);
511 514 ASSERT(error == 0);
512 515 return (error);
513 516 }
514 517
515 518 static void
516 519 sa_copy_data(sa_data_locator_t *func, void *datastart, void *target, int buflen)
517 520 {
518 521 if (func == NULL) {
519 522 bcopy(datastart, target, buflen);
520 523 } else {
521 524 boolean_t start;
522 525 int bytes;
523 526 void *dataptr;
524 527 void *saptr = target;
525 528 uint32_t length;
526 529
527 530 start = B_TRUE;
528 531 bytes = 0;
529 532 while (bytes < buflen) {
530 533 func(&dataptr, &length, buflen, start, datastart);
531 534 bcopy(dataptr, saptr, length);
532 535 saptr = (void *)((caddr_t)saptr + length);
533 536 bytes += length;
534 537 start = B_FALSE;
535 538 }
536 539 }
537 540 }
538 541
539 542 /*
540 543 * Determine several different sizes
541 544 * first the sa header size
542 545 * the number of bytes to be stored
543 546 * if spill would occur the index in the attribute array is returned
544 547 *
545 548 * the boolean will_spill will be set when spilling is necessary. It
546 549 * is only set when the buftype is SA_BONUS
547 550 */
548 551 static int
549 552 sa_find_sizes(sa_os_t *sa, sa_bulk_attr_t *attr_desc, int attr_count,
550 553 dmu_buf_t *db, sa_buf_type_t buftype, int *index, int *total,
551 554 boolean_t *will_spill)
552 555 {
553 556 int var_size = 0;
554 557 int i;
555 558 int j = -1;
556 559 int full_space;
557 560 int hdrsize;
558 561 boolean_t done = B_FALSE;
559 562
560 563 if (buftype == SA_BONUS && sa->sa_force_spill) {
561 564 *total = 0;
562 565 *index = 0;
563 566 *will_spill = B_TRUE;
564 567 return (0);
565 568 }
566 569
567 570 *index = -1;
568 571 *total = 0;
569 572
570 573 if (buftype == SA_BONUS)
571 574 *will_spill = B_FALSE;
572 575
573 576 hdrsize = (SA_BONUSTYPE_FROM_DB(db) == DMU_OT_ZNODE) ? 0 :
574 577 sizeof (sa_hdr_phys_t);
575 578
576 579 full_space = (buftype == SA_BONUS) ? DN_MAX_BONUSLEN : db->db_size;
577 580 ASSERT(IS_P2ALIGNED(full_space, 8));
578 581
579 582 for (i = 0; i != attr_count; i++) {
580 583 boolean_t is_var_sz;
581 584
582 585 *total = P2ROUNDUP(*total, 8);
583 586 *total += attr_desc[i].sa_length;
584 587 if (done)
585 588 goto next;
586 589
587 590 is_var_sz = (SA_REGISTERED_LEN(sa, attr_desc[i].sa_attr) == 0);
588 591 if (is_var_sz) {
589 592 var_size++;
590 593 }
591 594
592 595 if (is_var_sz && var_size > 1) {
593 596 if (P2ROUNDUP(hdrsize + sizeof (uint16_t), 8) +
594 597 *total < full_space) {
595 598 /*
596 599 * Account for header space used by array of
597 600 * optional sizes of variable-length attributes.
598 601 * Record the index in case this increase needs
599 602 * to be reversed due to spill-over.
600 603 */
601 604 hdrsize += sizeof (uint16_t);
602 605 j = i;
603 606 } else {
604 607 done = B_TRUE;
605 608 *index = i;
606 609 if (buftype == SA_BONUS)
607 610 *will_spill = B_TRUE;
608 611 continue;
609 612 }
610 613 }
611 614
612 615 /*
613 616 * find index of where spill *could* occur.
614 617 * Then continue to count of remainder attribute
615 618 * space. The sum is used later for sizing bonus
616 619 * and spill buffer.
617 620 */
618 621 if (buftype == SA_BONUS && *index == -1 &&
619 622 *total + P2ROUNDUP(hdrsize, 8) >
620 623 (full_space - sizeof (blkptr_t))) {
621 624 *index = i;
622 625 done = B_TRUE;
623 626 }
624 627
625 628 next:
626 629 if (*total + P2ROUNDUP(hdrsize, 8) > full_space &&
627 630 buftype == SA_BONUS)
628 631 *will_spill = B_TRUE;
629 632 }
630 633
631 634 /*
632 635 * j holds the index of the last variable-sized attribute for
633 636 * which hdrsize was increased. Reverse the increase if that
634 637 * attribute will be relocated to the spill block.
635 638 */
636 639 if (*will_spill && j == *index)
637 640 hdrsize -= sizeof (uint16_t);
638 641
639 642 hdrsize = P2ROUNDUP(hdrsize, 8);
640 643 return (hdrsize);
641 644 }
642 645
643 646 #define BUF_SPACE_NEEDED(total, header) (total + header)
644 647
645 648 /*
646 649 * Find layout that corresponds to ordering of attributes
647 650 * If not found a new layout number is created and added to
648 651 * persistent layout tables.
649 652 */
650 653 static int
651 654 sa_build_layouts(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc, int attr_count,
652 655 dmu_tx_t *tx)
653 656 {
654 657 sa_os_t *sa = hdl->sa_os->os_sa;
655 658 uint64_t hash;
656 659 sa_buf_type_t buftype;
657 660 sa_hdr_phys_t *sahdr;
658 661 void *data_start;
659 662 int buf_space;
660 663 sa_attr_type_t *attrs, *attrs_start;
661 664 int i, lot_count;
662 665 int hdrsize;
663 666 int spillhdrsize = 0;
664 667 int used;
665 668 dmu_object_type_t bonustype;
666 669 sa_lot_t *lot;
667 670 int len_idx;
668 671 int spill_used;
669 672 boolean_t spilling;
670 673
671 674 dmu_buf_will_dirty(hdl->sa_bonus, tx);
672 675 bonustype = SA_BONUSTYPE_FROM_DB(hdl->sa_bonus);
673 676
674 677 /* first determine bonus header size and sum of all attributes */
675 678 hdrsize = sa_find_sizes(sa, attr_desc, attr_count, hdl->sa_bonus,
676 679 SA_BONUS, &i, &used, &spilling);
677 680
678 681 if (used > SPA_OLD_MAXBLOCKSIZE)
679 682 return (SET_ERROR(EFBIG));
680 683
681 684 VERIFY(0 == dmu_set_bonus(hdl->sa_bonus, spilling ?
682 685 MIN(DN_MAX_BONUSLEN - sizeof (blkptr_t), used + hdrsize) :
683 686 used + hdrsize, tx));
684 687
685 688 ASSERT((bonustype == DMU_OT_ZNODE && spilling == 0) ||
686 689 bonustype == DMU_OT_SA);
687 690
688 691 /* setup and size spill buffer when needed */
689 692 if (spilling) {
690 693 boolean_t dummy;
691 694
692 695 if (hdl->sa_spill == NULL) {
693 696 VERIFY(dmu_spill_hold_by_bonus(hdl->sa_bonus, NULL,
694 697 &hdl->sa_spill) == 0);
695 698 }
696 699 dmu_buf_will_dirty(hdl->sa_spill, tx);
697 700
698 701 spillhdrsize = sa_find_sizes(sa, &attr_desc[i],
699 702 attr_count - i, hdl->sa_spill, SA_SPILL, &i,
700 703 &spill_used, &dummy);
701 704
702 705 if (spill_used > SPA_OLD_MAXBLOCKSIZE)
703 706 return (SET_ERROR(EFBIG));
704 707
705 708 buf_space = hdl->sa_spill->db_size - spillhdrsize;
706 709 if (BUF_SPACE_NEEDED(spill_used, spillhdrsize) >
707 710 hdl->sa_spill->db_size)
708 711 VERIFY(0 == sa_resize_spill(hdl,
709 712 BUF_SPACE_NEEDED(spill_used, spillhdrsize), tx));
710 713 }
711 714
712 715 /* setup starting pointers to lay down data */
713 716 data_start = (void *)((uintptr_t)hdl->sa_bonus->db_data + hdrsize);
714 717 sahdr = (sa_hdr_phys_t *)hdl->sa_bonus->db_data;
715 718 buftype = SA_BONUS;
716 719
717 720 if (spilling)
718 721 buf_space = (sa->sa_force_spill) ?
719 722 0 : SA_BLKPTR_SPACE - hdrsize;
720 723 else
721 724 buf_space = hdl->sa_bonus->db_size - hdrsize;
722 725
723 726 attrs_start = attrs = kmem_alloc(sizeof (sa_attr_type_t) * attr_count,
724 727 KM_SLEEP);
725 728 lot_count = 0;
726 729
727 730 for (i = 0, len_idx = 0, hash = -1ULL; i != attr_count; i++) {
728 731 uint16_t length;
729 732
730 733 ASSERT(IS_P2ALIGNED(data_start, 8));
731 734 ASSERT(IS_P2ALIGNED(buf_space, 8));
732 735 attrs[i] = attr_desc[i].sa_attr;
733 736 length = SA_REGISTERED_LEN(sa, attrs[i]);
734 737 if (length == 0)
735 738 length = attr_desc[i].sa_length;
736 739
737 740 if (buf_space < length) { /* switch to spill buffer */
738 741 VERIFY(spilling);
739 742 VERIFY(bonustype == DMU_OT_SA);
740 743 if (buftype == SA_BONUS && !sa->sa_force_spill) {
741 744 sa_find_layout(hdl->sa_os, hash, attrs_start,
742 745 lot_count, tx, &lot);
743 746 SA_SET_HDR(sahdr, lot->lot_num, hdrsize);
744 747 }
745 748
746 749 buftype = SA_SPILL;
747 750 hash = -1ULL;
748 751 len_idx = 0;
749 752
750 753 sahdr = (sa_hdr_phys_t *)hdl->sa_spill->db_data;
751 754 sahdr->sa_magic = SA_MAGIC;
752 755 data_start = (void *)((uintptr_t)sahdr +
753 756 spillhdrsize);
754 757 attrs_start = &attrs[i];
755 758 buf_space = hdl->sa_spill->db_size - spillhdrsize;
756 759 lot_count = 0;
757 760 }
758 761 hash ^= SA_ATTR_HASH(attrs[i]);
759 762 attr_desc[i].sa_addr = data_start;
760 763 attr_desc[i].sa_size = length;
761 764 SA_COPY_DATA(attr_desc[i].sa_data_func, attr_desc[i].sa_data,
762 765 data_start, length);
763 766 if (sa->sa_attr_table[attrs[i]].sa_length == 0) {
764 767 sahdr->sa_lengths[len_idx++] = length;
765 768 }
766 769 data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
767 770 length), 8);
768 771 buf_space -= P2ROUNDUP(length, 8);
769 772 lot_count++;
770 773 }
771 774
772 775 sa_find_layout(hdl->sa_os, hash, attrs_start, lot_count, tx, &lot);
773 776
774 777 /*
775 778 * Verify that old znodes always have layout number 0.
776 779 * Must be DMU_OT_SA for arbitrary layouts
777 780 */
778 781 VERIFY((bonustype == DMU_OT_ZNODE && lot->lot_num == 0) ||
779 782 (bonustype == DMU_OT_SA && lot->lot_num > 1));
780 783
781 784 if (bonustype == DMU_OT_SA) {
782 785 SA_SET_HDR(sahdr, lot->lot_num,
783 786 buftype == SA_BONUS ? hdrsize : spillhdrsize);
784 787 }
785 788
786 789 kmem_free(attrs, sizeof (sa_attr_type_t) * attr_count);
787 790 if (hdl->sa_bonus_tab) {
788 791 sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
789 792 hdl->sa_bonus_tab = NULL;
790 793 }
791 794 if (!sa->sa_force_spill)
792 795 VERIFY(0 == sa_build_index(hdl, SA_BONUS));
793 796 if (hdl->sa_spill) {
794 797 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
795 798 if (!spilling) {
796 799 /*
797 800 * remove spill block that is no longer needed.
798 801 */
799 802 dmu_buf_rele(hdl->sa_spill, NULL);
800 803 hdl->sa_spill = NULL;
801 804 hdl->sa_spill_tab = NULL;
802 805 VERIFY(0 == dmu_rm_spill(hdl->sa_os,
803 806 sa_handle_object(hdl), tx));
804 807 } else {
805 808 VERIFY(0 == sa_build_index(hdl, SA_SPILL));
806 809 }
807 810 }
808 811
809 812 return (0);
810 813 }
811 814
812 815 static void
813 816 sa_free_attr_table(sa_os_t *sa)
814 817 {
815 818 int i;
816 819
817 820 if (sa->sa_attr_table == NULL)
818 821 return;
819 822
820 823 for (i = 0; i != sa->sa_num_attrs; i++) {
821 824 if (sa->sa_attr_table[i].sa_name)
822 825 kmem_free(sa->sa_attr_table[i].sa_name,
823 826 strlen(sa->sa_attr_table[i].sa_name) + 1);
824 827 }
825 828
826 829 kmem_free(sa->sa_attr_table,
827 830 sizeof (sa_attr_table_t) * sa->sa_num_attrs);
828 831
829 832 sa->sa_attr_table = NULL;
830 833 }
831 834
832 835 static int
833 836 sa_attr_table_setup(objset_t *os, sa_attr_reg_t *reg_attrs, int count)
834 837 {
835 838 sa_os_t *sa = os->os_sa;
836 839 uint64_t sa_attr_count = 0;
837 840 uint64_t sa_reg_count = 0;
838 841 int error = 0;
839 842 uint64_t attr_value;
840 843 sa_attr_table_t *tb;
841 844 zap_cursor_t zc;
842 845 zap_attribute_t za;
843 846 int registered_count = 0;
844 847 int i;
845 848 dmu_objset_type_t ostype = dmu_objset_type(os);
846 849
847 850 sa->sa_user_table =
848 851 kmem_zalloc(count * sizeof (sa_attr_type_t), KM_SLEEP);
849 852 sa->sa_user_table_sz = count * sizeof (sa_attr_type_t);
850 853
851 854 if (sa->sa_reg_attr_obj != 0) {
852 855 error = zap_count(os, sa->sa_reg_attr_obj,
853 856 &sa_attr_count);
854 857
855 858 /*
856 859 * Make sure we retrieved a count and that it isn't zero
857 860 */
858 861 if (error || (error == 0 && sa_attr_count == 0)) {
859 862 if (error == 0)
860 863 error = SET_ERROR(EINVAL);
861 864 goto bail;
862 865 }
863 866 sa_reg_count = sa_attr_count;
864 867 }
865 868
866 869 if (ostype == DMU_OST_ZFS && sa_attr_count == 0)
867 870 sa_attr_count += sa_legacy_attr_count;
868 871
869 872 /* Allocate attribute numbers for attributes that aren't registered */
870 873 for (i = 0; i != count; i++) {
871 874 boolean_t found = B_FALSE;
872 875 int j;
873 876
874 877 if (ostype == DMU_OST_ZFS) {
875 878 for (j = 0; j != sa_legacy_attr_count; j++) {
876 879 if (strcmp(reg_attrs[i].sa_name,
877 880 sa_legacy_attrs[j].sa_name) == 0) {
878 881 sa->sa_user_table[i] =
879 882 sa_legacy_attrs[j].sa_attr;
880 883 found = B_TRUE;
881 884 }
882 885 }
883 886 }
884 887 if (found)
885 888 continue;
886 889
887 890 if (sa->sa_reg_attr_obj)
888 891 error = zap_lookup(os, sa->sa_reg_attr_obj,
889 892 reg_attrs[i].sa_name, 8, 1, &attr_value);
890 893 else
891 894 error = SET_ERROR(ENOENT);
892 895 switch (error) {
893 896 case ENOENT:
894 897 sa->sa_user_table[i] = (sa_attr_type_t)sa_attr_count;
895 898 sa_attr_count++;
896 899 break;
897 900 case 0:
898 901 sa->sa_user_table[i] = ATTR_NUM(attr_value);
899 902 break;
900 903 default:
901 904 goto bail;
902 905 }
903 906 }
904 907
905 908 sa->sa_num_attrs = sa_attr_count;
906 909 tb = sa->sa_attr_table =
907 910 kmem_zalloc(sizeof (sa_attr_table_t) * sa_attr_count, KM_SLEEP);
908 911
909 912 /*
910 913 * Attribute table is constructed from requested attribute list,
911 914 * previously foreign registered attributes, and also the legacy
912 915 * ZPL set of attributes.
913 916 */
914 917
915 918 if (sa->sa_reg_attr_obj) {
916 919 for (zap_cursor_init(&zc, os, sa->sa_reg_attr_obj);
917 920 (error = zap_cursor_retrieve(&zc, &za)) == 0;
918 921 zap_cursor_advance(&zc)) {
919 922 uint64_t value;
920 923 value = za.za_first_integer;
921 924
922 925 registered_count++;
923 926 tb[ATTR_NUM(value)].sa_attr = ATTR_NUM(value);
924 927 tb[ATTR_NUM(value)].sa_length = ATTR_LENGTH(value);
925 928 tb[ATTR_NUM(value)].sa_byteswap = ATTR_BSWAP(value);
926 929 tb[ATTR_NUM(value)].sa_registered = B_TRUE;
927 930
928 931 if (tb[ATTR_NUM(value)].sa_name) {
929 932 continue;
930 933 }
931 934 tb[ATTR_NUM(value)].sa_name =
932 935 kmem_zalloc(strlen(za.za_name) +1, KM_SLEEP);
933 936 (void) strlcpy(tb[ATTR_NUM(value)].sa_name, za.za_name,
934 937 strlen(za.za_name) +1);
935 938 }
936 939 zap_cursor_fini(&zc);
937 940 /*
938 941 * Make sure we processed the correct number of registered
939 942 * attributes
940 943 */
941 944 if (registered_count != sa_reg_count) {
942 945 ASSERT(error != 0);
943 946 goto bail;
944 947 }
945 948
946 949 }
947 950
948 951 if (ostype == DMU_OST_ZFS) {
949 952 for (i = 0; i != sa_legacy_attr_count; i++) {
950 953 if (tb[i].sa_name)
951 954 continue;
952 955 tb[i].sa_attr = sa_legacy_attrs[i].sa_attr;
953 956 tb[i].sa_length = sa_legacy_attrs[i].sa_length;
954 957 tb[i].sa_byteswap = sa_legacy_attrs[i].sa_byteswap;
955 958 tb[i].sa_registered = B_FALSE;
956 959 tb[i].sa_name =
957 960 kmem_zalloc(strlen(sa_legacy_attrs[i].sa_name) +1,
958 961 KM_SLEEP);
959 962 (void) strlcpy(tb[i].sa_name,
960 963 sa_legacy_attrs[i].sa_name,
961 964 strlen(sa_legacy_attrs[i].sa_name) + 1);
962 965 }
963 966 }
964 967
965 968 for (i = 0; i != count; i++) {
966 969 sa_attr_type_t attr_id;
967 970
968 971 attr_id = sa->sa_user_table[i];
969 972 if (tb[attr_id].sa_name)
970 973 continue;
971 974
972 975 tb[attr_id].sa_length = reg_attrs[i].sa_length;
973 976 tb[attr_id].sa_byteswap = reg_attrs[i].sa_byteswap;
974 977 tb[attr_id].sa_attr = attr_id;
975 978 tb[attr_id].sa_name =
976 979 kmem_zalloc(strlen(reg_attrs[i].sa_name) + 1, KM_SLEEP);
977 980 (void) strlcpy(tb[attr_id].sa_name, reg_attrs[i].sa_name,
978 981 strlen(reg_attrs[i].sa_name) + 1);
979 982 }
980 983
981 984 sa->sa_need_attr_registration =
982 985 (sa_attr_count != registered_count);
983 986
984 987 return (0);
985 988 bail:
986 989 kmem_free(sa->sa_user_table, count * sizeof (sa_attr_type_t));
987 990 sa->sa_user_table = NULL;
988 991 sa_free_attr_table(sa);
989 992 return ((error != 0) ? error : EINVAL);
990 993 }
991 994
992 995 int
993 996 sa_setup(objset_t *os, uint64_t sa_obj, sa_attr_reg_t *reg_attrs, int count,
994 997 sa_attr_type_t **user_table)
995 998 {
996 999 zap_cursor_t zc;
997 1000 zap_attribute_t za;
998 1001 sa_os_t *sa;
999 1002 dmu_objset_type_t ostype = dmu_objset_type(os);
1000 1003 sa_attr_type_t *tb;
1001 1004 int error;
1002 1005
1003 1006 mutex_enter(&os->os_user_ptr_lock);
1004 1007 if (os->os_sa) {
1005 1008 mutex_enter(&os->os_sa->sa_lock);
1006 1009 mutex_exit(&os->os_user_ptr_lock);
1007 1010 tb = os->os_sa->sa_user_table;
1008 1011 mutex_exit(&os->os_sa->sa_lock);
1009 1012 *user_table = tb;
1010 1013 return (0);
1011 1014 }
1012 1015
1013 1016 sa = kmem_zalloc(sizeof (sa_os_t), KM_SLEEP);
1014 1017 mutex_init(&sa->sa_lock, NULL, MUTEX_DEFAULT, NULL);
1015 1018 sa->sa_master_obj = sa_obj;
1016 1019
1017 1020 os->os_sa = sa;
1018 1021 mutex_enter(&sa->sa_lock);
1019 1022 mutex_exit(&os->os_user_ptr_lock);
1020 1023 avl_create(&sa->sa_layout_num_tree, layout_num_compare,
1021 1024 sizeof (sa_lot_t), offsetof(sa_lot_t, lot_num_node));
1022 1025 avl_create(&sa->sa_layout_hash_tree, layout_hash_compare,
1023 1026 sizeof (sa_lot_t), offsetof(sa_lot_t, lot_hash_node));
1024 1027
1025 1028 if (sa_obj) {
1026 1029 error = zap_lookup(os, sa_obj, SA_LAYOUTS,
1027 1030 8, 1, &sa->sa_layout_attr_obj);
1028 1031 if (error != 0 && error != ENOENT)
1029 1032 goto fail;
1030 1033 error = zap_lookup(os, sa_obj, SA_REGISTRY,
1031 1034 8, 1, &sa->sa_reg_attr_obj);
1032 1035 if (error != 0 && error != ENOENT)
1033 1036 goto fail;
1034 1037 }
1035 1038
1036 1039 if ((error = sa_attr_table_setup(os, reg_attrs, count)) != 0)
1037 1040 goto fail;
1038 1041
1039 1042 if (sa->sa_layout_attr_obj != 0) {
1040 1043 uint64_t layout_count;
1041 1044
1042 1045 error = zap_count(os, sa->sa_layout_attr_obj,
1043 1046 &layout_count);
1044 1047
1045 1048 /*
1046 1049 * Layout number count should be > 0
1047 1050 */
1048 1051 if (error || (error == 0 && layout_count == 0)) {
1049 1052 if (error == 0)
1050 1053 error = SET_ERROR(EINVAL);
1051 1054 goto fail;
1052 1055 }
1053 1056
1054 1057 for (zap_cursor_init(&zc, os, sa->sa_layout_attr_obj);
1055 1058 (error = zap_cursor_retrieve(&zc, &za)) == 0;
1056 1059 zap_cursor_advance(&zc)) {
1057 1060 sa_attr_type_t *lot_attrs;
1058 1061 uint64_t lot_num;
1059 1062
1060 1063 lot_attrs = kmem_zalloc(sizeof (sa_attr_type_t) *
1061 1064 za.za_num_integers, KM_SLEEP);
1062 1065
1063 1066 if ((error = (zap_lookup(os, sa->sa_layout_attr_obj,
1064 1067 za.za_name, 2, za.za_num_integers,
1065 1068 lot_attrs))) != 0) {
1066 1069 kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1067 1070 za.za_num_integers);
1068 1071 break;
1069 1072 }
1070 1073 VERIFY(ddi_strtoull(za.za_name, NULL, 10,
1071 1074 (unsigned long long *)&lot_num) == 0);
1072 1075
1073 1076 (void) sa_add_layout_entry(os, lot_attrs,
1074 1077 za.za_num_integers, lot_num,
1075 1078 sa_layout_info_hash(lot_attrs,
1076 1079 za.za_num_integers), B_FALSE, NULL);
1077 1080 kmem_free(lot_attrs, sizeof (sa_attr_type_t) *
1078 1081 za.za_num_integers);
1079 1082 }
1080 1083 zap_cursor_fini(&zc);
1081 1084
1082 1085 /*
1083 1086 * Make sure layout count matches number of entries added
1084 1087 * to AVL tree
1085 1088 */
1086 1089 if (avl_numnodes(&sa->sa_layout_num_tree) != layout_count) {
1087 1090 ASSERT(error != 0);
1088 1091 goto fail;
1089 1092 }
1090 1093 }
1091 1094
1092 1095 /* Add special layout number for old ZNODES */
1093 1096 if (ostype == DMU_OST_ZFS) {
1094 1097 (void) sa_add_layout_entry(os, sa_legacy_zpl_layout,
1095 1098 sa_legacy_attr_count, 0,
1096 1099 sa_layout_info_hash(sa_legacy_zpl_layout,
1097 1100 sa_legacy_attr_count), B_FALSE, NULL);
1098 1101
1099 1102 (void) sa_add_layout_entry(os, sa_dummy_zpl_layout, 0, 1,
1100 1103 0, B_FALSE, NULL);
1101 1104 }
1102 1105 *user_table = os->os_sa->sa_user_table;
1103 1106 mutex_exit(&sa->sa_lock);
1104 1107 return (0);
1105 1108 fail:
1106 1109 os->os_sa = NULL;
1107 1110 sa_free_attr_table(sa);
1108 1111 if (sa->sa_user_table)
1109 1112 kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1110 1113 mutex_exit(&sa->sa_lock);
1111 1114 avl_destroy(&sa->sa_layout_hash_tree);
1112 1115 avl_destroy(&sa->sa_layout_num_tree);
1113 1116 mutex_destroy(&sa->sa_lock);
1114 1117 kmem_free(sa, sizeof (sa_os_t));
1115 1118 return ((error == ECKSUM) ? EIO : error);
1116 1119 }
1117 1120
1118 1121 void
1119 1122 sa_tear_down(objset_t *os)
1120 1123 {
1121 1124 sa_os_t *sa = os->os_sa;
1122 1125 sa_lot_t *layout;
1123 1126 void *cookie;
1124 1127
1125 1128 kmem_free(sa->sa_user_table, sa->sa_user_table_sz);
1126 1129
1127 1130 /* Free up attr table */
1128 1131
1129 1132 sa_free_attr_table(sa);
1130 1133
1131 1134 cookie = NULL;
1132 1135 while (layout = avl_destroy_nodes(&sa->sa_layout_hash_tree, &cookie)) {
1133 1136 sa_idx_tab_t *tab;
1134 1137 while (tab = list_head(&layout->lot_idx_tab)) {
1135 1138 ASSERT(refcount_count(&tab->sa_refcount));
1136 1139 sa_idx_tab_rele(os, tab);
1137 1140 }
1138 1141 }
1139 1142
1140 1143 cookie = NULL;
1141 1144 while (layout = avl_destroy_nodes(&sa->sa_layout_num_tree, &cookie)) {
1142 1145 kmem_free(layout->lot_attrs,
1143 1146 sizeof (sa_attr_type_t) * layout->lot_attr_count);
1144 1147 kmem_free(layout, sizeof (sa_lot_t));
1145 1148 }
1146 1149
1147 1150 avl_destroy(&sa->sa_layout_hash_tree);
1148 1151 avl_destroy(&sa->sa_layout_num_tree);
1149 1152 mutex_destroy(&sa->sa_lock);
1150 1153
1151 1154 kmem_free(sa, sizeof (sa_os_t));
1152 1155 os->os_sa = NULL;
1153 1156 }
1154 1157
1155 1158 void
1156 1159 sa_build_idx_tab(void *hdr, void *attr_addr, sa_attr_type_t attr,
1157 1160 uint16_t length, int length_idx, boolean_t var_length, void *userp)
1158 1161 {
1159 1162 sa_idx_tab_t *idx_tab = userp;
1160 1163
1161 1164 if (var_length) {
1162 1165 ASSERT(idx_tab->sa_variable_lengths);
1163 1166 idx_tab->sa_variable_lengths[length_idx] = length;
1164 1167 }
1165 1168 TOC_ATTR_ENCODE(idx_tab->sa_idx_tab[attr], length_idx,
1166 1169 (uint32_t)((uintptr_t)attr_addr - (uintptr_t)hdr));
1167 1170 }
1168 1171
1169 1172 static void
1170 1173 sa_attr_iter(objset_t *os, sa_hdr_phys_t *hdr, dmu_object_type_t type,
1171 1174 sa_iterfunc_t func, sa_lot_t *tab, void *userp)
1172 1175 {
1173 1176 void *data_start;
1174 1177 sa_lot_t *tb = tab;
1175 1178 sa_lot_t search;
1176 1179 avl_index_t loc;
1177 1180 sa_os_t *sa = os->os_sa;
1178 1181 int i;
1179 1182 uint16_t *length_start = NULL;
1180 1183 uint8_t length_idx = 0;
1181 1184
1182 1185 if (tab == NULL) {
1183 1186 search.lot_num = SA_LAYOUT_NUM(hdr, type);
1184 1187 tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1185 1188 ASSERT(tb);
1186 1189 }
1187 1190
1188 1191 if (IS_SA_BONUSTYPE(type)) {
1189 1192 data_start = (void *)P2ROUNDUP(((uintptr_t)hdr +
1190 1193 offsetof(sa_hdr_phys_t, sa_lengths) +
1191 1194 (sizeof (uint16_t) * tb->lot_var_sizes)), 8);
1192 1195 length_start = hdr->sa_lengths;
1193 1196 } else {
1194 1197 data_start = hdr;
1195 1198 }
1196 1199
1197 1200 for (i = 0; i != tb->lot_attr_count; i++) {
1198 1201 int attr_length, reg_length;
1199 1202 uint8_t idx_len;
1200 1203
1201 1204 reg_length = sa->sa_attr_table[tb->lot_attrs[i]].sa_length;
1202 1205 if (reg_length) {
1203 1206 attr_length = reg_length;
1204 1207 idx_len = 0;
1205 1208 } else {
1206 1209 attr_length = length_start[length_idx];
1207 1210 idx_len = length_idx++;
1208 1211 }
1209 1212
1210 1213 func(hdr, data_start, tb->lot_attrs[i], attr_length,
1211 1214 idx_len, reg_length == 0 ? B_TRUE : B_FALSE, userp);
1212 1215
1213 1216 data_start = (void *)P2ROUNDUP(((uintptr_t)data_start +
1214 1217 attr_length), 8);
1215 1218 }
1216 1219 }
1217 1220
1218 1221 /*ARGSUSED*/
1219 1222 void
1220 1223 sa_byteswap_cb(void *hdr, void *attr_addr, sa_attr_type_t attr,
1221 1224 uint16_t length, int length_idx, boolean_t variable_length, void *userp)
1222 1225 {
1223 1226 sa_handle_t *hdl = userp;
1224 1227 sa_os_t *sa = hdl->sa_os->os_sa;
1225 1228
1226 1229 sa_bswap_table[sa->sa_attr_table[attr].sa_byteswap](attr_addr, length);
1227 1230 }
1228 1231
1229 1232 void
1230 1233 sa_byteswap(sa_handle_t *hdl, sa_buf_type_t buftype)
1231 1234 {
1232 1235 sa_hdr_phys_t *sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1233 1236 dmu_buf_impl_t *db;
1234 1237 sa_os_t *sa = hdl->sa_os->os_sa;
1235 1238 int num_lengths = 1;
1236 1239 int i;
1237 1240
1238 1241 ASSERT(MUTEX_HELD(&sa->sa_lock));
1239 1242 if (sa_hdr_phys->sa_magic == SA_MAGIC)
1240 1243 return;
1241 1244
1242 1245 db = SA_GET_DB(hdl, buftype);
1243 1246
1244 1247 if (buftype == SA_SPILL) {
1245 1248 arc_release(db->db_buf, NULL);
1246 1249 arc_buf_thaw(db->db_buf);
1247 1250 }
1248 1251
1249 1252 sa_hdr_phys->sa_magic = BSWAP_32(sa_hdr_phys->sa_magic);
1250 1253 sa_hdr_phys->sa_layout_info = BSWAP_16(sa_hdr_phys->sa_layout_info);
1251 1254
1252 1255 /*
1253 1256 * Determine number of variable lenghts in header
1254 1257 * The standard 8 byte header has one for free and a
1255 1258 * 16 byte header would have 4 + 1;
1256 1259 */
1257 1260 if (SA_HDR_SIZE(sa_hdr_phys) > 8)
1258 1261 num_lengths += (SA_HDR_SIZE(sa_hdr_phys) - 8) >> 1;
1259 1262 for (i = 0; i != num_lengths; i++)
1260 1263 sa_hdr_phys->sa_lengths[i] =
1261 1264 BSWAP_16(sa_hdr_phys->sa_lengths[i]);
1262 1265
1263 1266 sa_attr_iter(hdl->sa_os, sa_hdr_phys, DMU_OT_SA,
1264 1267 sa_byteswap_cb, NULL, hdl);
1265 1268
1266 1269 if (buftype == SA_SPILL)
1267 1270 arc_buf_freeze(((dmu_buf_impl_t *)hdl->sa_spill)->db_buf);
1268 1271 }
1269 1272
1270 1273 static int
1271 1274 sa_build_index(sa_handle_t *hdl, sa_buf_type_t buftype)
1272 1275 {
1273 1276 sa_hdr_phys_t *sa_hdr_phys;
1274 1277 dmu_buf_impl_t *db = SA_GET_DB(hdl, buftype);
1275 1278 dmu_object_type_t bonustype = SA_BONUSTYPE_FROM_DB(db);
1276 1279 sa_os_t *sa = hdl->sa_os->os_sa;
1277 1280 sa_idx_tab_t *idx_tab;
1278 1281
1279 1282 sa_hdr_phys = SA_GET_HDR(hdl, buftype);
1280 1283
1281 1284 mutex_enter(&sa->sa_lock);
1282 1285
1283 1286 /* Do we need to byteswap? */
1284 1287
1285 1288 /* only check if not old znode */
1286 1289 if (IS_SA_BONUSTYPE(bonustype) && sa_hdr_phys->sa_magic != SA_MAGIC &&
1287 1290 sa_hdr_phys->sa_magic != 0) {
1288 1291 VERIFY(BSWAP_32(sa_hdr_phys->sa_magic) == SA_MAGIC);
1289 1292 sa_byteswap(hdl, buftype);
1290 1293 }
1291 1294
1292 1295 idx_tab = sa_find_idx_tab(hdl->sa_os, bonustype, sa_hdr_phys);
1293 1296
|
↓ open down ↓ |
1056 lines elided |
↑ open up ↑ |
1294 1297 if (buftype == SA_BONUS)
1295 1298 hdl->sa_bonus_tab = idx_tab;
1296 1299 else
1297 1300 hdl->sa_spill_tab = idx_tab;
1298 1301
1299 1302 mutex_exit(&sa->sa_lock);
1300 1303 return (0);
1301 1304 }
1302 1305
1303 1306 /*ARGSUSED*/
1304 -void
1305 -sa_evict(dmu_buf_t *db, void *sap)
1307 +static void
1308 +sa_evict(void *dbu)
1306 1309 {
1307 - panic("evicting sa dbuf %p\n", (void *)db);
1310 + panic("evicting sa dbuf\n");
1308 1311 }
1309 1312
1310 1313 static void
1311 1314 sa_idx_tab_rele(objset_t *os, void *arg)
1312 1315 {
1313 1316 sa_os_t *sa = os->os_sa;
1314 1317 sa_idx_tab_t *idx_tab = arg;
1315 1318
1316 1319 if (idx_tab == NULL)
1317 1320 return;
1318 1321
1319 1322 mutex_enter(&sa->sa_lock);
1320 1323 if (refcount_remove(&idx_tab->sa_refcount, NULL) == 0) {
1321 1324 list_remove(&idx_tab->sa_layout->lot_idx_tab, idx_tab);
1322 1325 if (idx_tab->sa_variable_lengths)
1323 1326 kmem_free(idx_tab->sa_variable_lengths,
1324 1327 sizeof (uint16_t) *
1325 1328 idx_tab->sa_layout->lot_var_sizes);
1326 1329 refcount_destroy(&idx_tab->sa_refcount);
1327 1330 kmem_free(idx_tab->sa_idx_tab,
1328 1331 sizeof (uint32_t) * sa->sa_num_attrs);
1329 1332 kmem_free(idx_tab, sizeof (sa_idx_tab_t));
1330 1333 }
1331 1334 mutex_exit(&sa->sa_lock);
1332 1335 }
1333 1336
1334 1337 static void
1335 1338 sa_idx_tab_hold(objset_t *os, sa_idx_tab_t *idx_tab)
|
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
1336 1339 {
1337 1340 sa_os_t *sa = os->os_sa;
1338 1341
1339 1342 ASSERT(MUTEX_HELD(&sa->sa_lock));
1340 1343 (void) refcount_add(&idx_tab->sa_refcount, NULL);
1341 1344 }
1342 1345
1343 1346 void
1344 1347 sa_handle_destroy(sa_handle_t *hdl)
1345 1348 {
1349 + dmu_buf_t *db = hdl->sa_bonus;
1350 +
1346 1351 mutex_enter(&hdl->sa_lock);
1347 - (void) dmu_buf_update_user((dmu_buf_t *)hdl->sa_bonus, hdl,
1348 - NULL, NULL);
1352 + (void) dmu_buf_remove_user(db, &hdl->sa_dbu);
1349 1353
1350 1354 if (hdl->sa_bonus_tab) {
1351 1355 sa_idx_tab_rele(hdl->sa_os, hdl->sa_bonus_tab);
1352 1356 hdl->sa_bonus_tab = NULL;
1353 1357 }
1354 1358 if (hdl->sa_spill_tab) {
1355 1359 sa_idx_tab_rele(hdl->sa_os, hdl->sa_spill_tab);
1356 1360 hdl->sa_spill_tab = NULL;
1357 1361 }
1358 1362
1359 1363 dmu_buf_rele(hdl->sa_bonus, NULL);
1360 1364
1361 1365 if (hdl->sa_spill)
1362 1366 dmu_buf_rele((dmu_buf_t *)hdl->sa_spill, NULL);
1363 1367 mutex_exit(&hdl->sa_lock);
|
↓ open down ↓ |
5 lines elided |
↑ open up ↑ |
1364 1368
1365 1369 kmem_cache_free(sa_cache, hdl);
1366 1370 }
1367 1371
1368 1372 int
1369 1373 sa_handle_get_from_db(objset_t *os, dmu_buf_t *db, void *userp,
1370 1374 sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1371 1375 {
1372 1376 int error = 0;
1373 1377 dmu_object_info_t doi;
1374 - sa_handle_t *handle;
1378 + sa_handle_t *handle = NULL;
1375 1379
1376 1380 #ifdef ZFS_DEBUG
1377 1381 dmu_object_info_from_db(db, &doi);
1378 1382 ASSERT(doi.doi_bonus_type == DMU_OT_SA ||
1379 1383 doi.doi_bonus_type == DMU_OT_ZNODE);
1380 1384 #endif
1381 1385 /* find handle, if it exists */
1382 1386 /* if one doesn't exist then create a new one, and initialize it */
1383 1387
1384 - handle = (hdl_type == SA_HDL_SHARED) ? dmu_buf_get_user(db) : NULL;
1388 + if (hdl_type == SA_HDL_SHARED)
1389 + handle = dmu_buf_get_user(db);
1390 +
1385 1391 if (handle == NULL) {
1386 - sa_handle_t *newhandle;
1392 + sa_handle_t *winner = NULL;
1393 +
1387 1394 handle = kmem_cache_alloc(sa_cache, KM_SLEEP);
1388 1395 handle->sa_userp = userp;
1389 1396 handle->sa_bonus = db;
1390 1397 handle->sa_os = os;
1391 1398 handle->sa_spill = NULL;
1392 1399
1393 1400 error = sa_build_index(handle, SA_BONUS);
1394 - newhandle = (hdl_type == SA_HDL_SHARED) ?
1395 - dmu_buf_set_user_ie(db, handle, sa_evict) : NULL;
1396 1401
1397 - if (newhandle != NULL) {
1402 + if (hdl_type == SA_HDL_SHARED) {
1403 + dmu_buf_init_user(&handle->sa_dbu, sa_evict, NULL);
1404 + winner = dmu_buf_set_user_ie(db, &handle->sa_dbu);
1405 + }
1406 +
1407 + if (winner != NULL) {
1398 1408 kmem_cache_free(sa_cache, handle);
1399 - handle = newhandle;
1409 + handle = winner;
1400 1410 }
1401 1411 }
1402 1412 *handlepp = handle;
1403 1413
1404 1414 return (error);
1405 1415 }
1406 1416
1407 1417 int
1408 1418 sa_handle_get(objset_t *objset, uint64_t objid, void *userp,
1409 1419 sa_handle_type_t hdl_type, sa_handle_t **handlepp)
1410 1420 {
1411 1421 dmu_buf_t *db;
1412 1422 int error;
1413 1423
1414 1424 if (error = dmu_bonus_hold(objset, objid, NULL, &db))
1415 1425 return (error);
1416 1426
1417 1427 return (sa_handle_get_from_db(objset, db, userp, hdl_type,
1418 1428 handlepp));
1419 1429 }
1420 1430
1421 1431 int
1422 1432 sa_buf_hold(objset_t *objset, uint64_t obj_num, void *tag, dmu_buf_t **db)
1423 1433 {
1424 1434 return (dmu_bonus_hold(objset, obj_num, tag, db));
1425 1435 }
1426 1436
1427 1437 void
1428 1438 sa_buf_rele(dmu_buf_t *db, void *tag)
1429 1439 {
1430 1440 dmu_buf_rele(db, tag);
1431 1441 }
1432 1442
1433 1443 int
1434 1444 sa_lookup_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count)
1435 1445 {
1436 1446 ASSERT(hdl);
1437 1447 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1438 1448 return (sa_attr_op(hdl, bulk, count, SA_LOOKUP, NULL));
1439 1449 }
1440 1450
1441 1451 int
1442 1452 sa_lookup(sa_handle_t *hdl, sa_attr_type_t attr, void *buf, uint32_t buflen)
1443 1453 {
1444 1454 int error;
1445 1455 sa_bulk_attr_t bulk;
1446 1456
1447 1457 bulk.sa_attr = attr;
1448 1458 bulk.sa_data = buf;
1449 1459 bulk.sa_length = buflen;
1450 1460 bulk.sa_data_func = NULL;
1451 1461
1452 1462 ASSERT(hdl);
1453 1463 mutex_enter(&hdl->sa_lock);
1454 1464 error = sa_lookup_impl(hdl, &bulk, 1);
1455 1465 mutex_exit(&hdl->sa_lock);
1456 1466 return (error);
1457 1467 }
1458 1468
1459 1469 #ifdef _KERNEL
1460 1470 int
1461 1471 sa_lookup_uio(sa_handle_t *hdl, sa_attr_type_t attr, uio_t *uio)
1462 1472 {
1463 1473 int error;
1464 1474 sa_bulk_attr_t bulk;
1465 1475
1466 1476 bulk.sa_data = NULL;
1467 1477 bulk.sa_attr = attr;
1468 1478 bulk.sa_data_func = NULL;
1469 1479
1470 1480 ASSERT(hdl);
1471 1481
1472 1482 mutex_enter(&hdl->sa_lock);
1473 1483 if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) == 0) {
1474 1484 error = uiomove((void *)bulk.sa_addr, MIN(bulk.sa_size,
1475 1485 uio->uio_resid), UIO_READ, uio);
1476 1486 }
1477 1487 mutex_exit(&hdl->sa_lock);
1478 1488 return (error);
1479 1489
1480 1490 }
1481 1491 #endif
1482 1492
1483 1493 void *
1484 1494 sa_find_idx_tab(objset_t *os, dmu_object_type_t bonustype, void *data)
1485 1495 {
1486 1496 sa_idx_tab_t *idx_tab;
1487 1497 sa_hdr_phys_t *hdr = (sa_hdr_phys_t *)data;
1488 1498 sa_os_t *sa = os->os_sa;
1489 1499 sa_lot_t *tb, search;
1490 1500 avl_index_t loc;
1491 1501
1492 1502 /*
1493 1503 * Deterimine layout number. If SA node and header == 0 then
1494 1504 * force the index table to the dummy "1" empty layout.
1495 1505 *
1496 1506 * The layout number would only be zero for a newly created file
1497 1507 * that has not added any attributes yet, or with crypto enabled which
1498 1508 * doesn't write any attributes to the bonus buffer.
1499 1509 */
1500 1510
1501 1511 search.lot_num = SA_LAYOUT_NUM(hdr, bonustype);
1502 1512
1503 1513 tb = avl_find(&sa->sa_layout_num_tree, &search, &loc);
1504 1514
1505 1515 /* Verify header size is consistent with layout information */
1506 1516 ASSERT(tb);
1507 1517 ASSERT(IS_SA_BONUSTYPE(bonustype) &&
1508 1518 SA_HDR_SIZE_MATCH_LAYOUT(hdr, tb) || !IS_SA_BONUSTYPE(bonustype) ||
1509 1519 (IS_SA_BONUSTYPE(bonustype) && hdr->sa_layout_info == 0));
1510 1520
1511 1521 /*
1512 1522 * See if any of the already existing TOC entries can be reused?
1513 1523 */
1514 1524
1515 1525 for (idx_tab = list_head(&tb->lot_idx_tab); idx_tab;
1516 1526 idx_tab = list_next(&tb->lot_idx_tab, idx_tab)) {
1517 1527 boolean_t valid_idx = B_TRUE;
1518 1528 int i;
1519 1529
1520 1530 if (tb->lot_var_sizes != 0 &&
1521 1531 idx_tab->sa_variable_lengths != NULL) {
1522 1532 for (i = 0; i != tb->lot_var_sizes; i++) {
1523 1533 if (hdr->sa_lengths[i] !=
1524 1534 idx_tab->sa_variable_lengths[i]) {
1525 1535 valid_idx = B_FALSE;
1526 1536 break;
1527 1537 }
1528 1538 }
1529 1539 }
1530 1540 if (valid_idx) {
1531 1541 sa_idx_tab_hold(os, idx_tab);
1532 1542 return (idx_tab);
1533 1543 }
1534 1544 }
1535 1545
1536 1546 /* No such luck, create a new entry */
1537 1547 idx_tab = kmem_zalloc(sizeof (sa_idx_tab_t), KM_SLEEP);
1538 1548 idx_tab->sa_idx_tab =
1539 1549 kmem_zalloc(sizeof (uint32_t) * sa->sa_num_attrs, KM_SLEEP);
1540 1550 idx_tab->sa_layout = tb;
1541 1551 refcount_create(&idx_tab->sa_refcount);
1542 1552 if (tb->lot_var_sizes)
1543 1553 idx_tab->sa_variable_lengths = kmem_alloc(sizeof (uint16_t) *
1544 1554 tb->lot_var_sizes, KM_SLEEP);
1545 1555
1546 1556 sa_attr_iter(os, hdr, bonustype, sa_build_idx_tab,
1547 1557 tb, idx_tab);
1548 1558 sa_idx_tab_hold(os, idx_tab); /* one hold for consumer */
1549 1559 sa_idx_tab_hold(os, idx_tab); /* one for layout */
1550 1560 list_insert_tail(&tb->lot_idx_tab, idx_tab);
1551 1561 return (idx_tab);
1552 1562 }
1553 1563
1554 1564 void
1555 1565 sa_default_locator(void **dataptr, uint32_t *len, uint32_t total_len,
1556 1566 boolean_t start, void *userdata)
1557 1567 {
1558 1568 ASSERT(start);
1559 1569
1560 1570 *dataptr = userdata;
1561 1571 *len = total_len;
1562 1572 }
1563 1573
1564 1574 static void
1565 1575 sa_attr_register_sync(sa_handle_t *hdl, dmu_tx_t *tx)
1566 1576 {
1567 1577 uint64_t attr_value = 0;
1568 1578 sa_os_t *sa = hdl->sa_os->os_sa;
1569 1579 sa_attr_table_t *tb = sa->sa_attr_table;
1570 1580 int i;
1571 1581
1572 1582 mutex_enter(&sa->sa_lock);
1573 1583
1574 1584 if (!sa->sa_need_attr_registration || sa->sa_master_obj == NULL) {
1575 1585 mutex_exit(&sa->sa_lock);
1576 1586 return;
1577 1587 }
1578 1588
1579 1589 if (sa->sa_reg_attr_obj == NULL) {
1580 1590 sa->sa_reg_attr_obj = zap_create_link(hdl->sa_os,
1581 1591 DMU_OT_SA_ATTR_REGISTRATION,
1582 1592 sa->sa_master_obj, SA_REGISTRY, tx);
1583 1593 }
1584 1594 for (i = 0; i != sa->sa_num_attrs; i++) {
1585 1595 if (sa->sa_attr_table[i].sa_registered)
1586 1596 continue;
1587 1597 ATTR_ENCODE(attr_value, tb[i].sa_attr, tb[i].sa_length,
1588 1598 tb[i].sa_byteswap);
1589 1599 VERIFY(0 == zap_update(hdl->sa_os, sa->sa_reg_attr_obj,
1590 1600 tb[i].sa_name, 8, 1, &attr_value, tx));
1591 1601 tb[i].sa_registered = B_TRUE;
1592 1602 }
1593 1603 sa->sa_need_attr_registration = B_FALSE;
1594 1604 mutex_exit(&sa->sa_lock);
1595 1605 }
1596 1606
1597 1607 /*
1598 1608 * Replace all attributes with attributes specified in template.
1599 1609 * If dnode had a spill buffer then those attributes will be
1600 1610 * also be replaced, possibly with just an empty spill block
1601 1611 *
1602 1612 * This interface is intended to only be used for bulk adding of
1603 1613 * attributes for a new file. It will also be used by the ZPL
1604 1614 * when converting and old formatted znode to native SA support.
1605 1615 */
1606 1616 int
1607 1617 sa_replace_all_by_template_locked(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1608 1618 int attr_count, dmu_tx_t *tx)
1609 1619 {
1610 1620 sa_os_t *sa = hdl->sa_os->os_sa;
1611 1621
1612 1622 if (sa->sa_need_attr_registration)
1613 1623 sa_attr_register_sync(hdl, tx);
1614 1624 return (sa_build_layouts(hdl, attr_desc, attr_count, tx));
1615 1625 }
1616 1626
1617 1627 int
1618 1628 sa_replace_all_by_template(sa_handle_t *hdl, sa_bulk_attr_t *attr_desc,
1619 1629 int attr_count, dmu_tx_t *tx)
1620 1630 {
1621 1631 int error;
1622 1632
1623 1633 mutex_enter(&hdl->sa_lock);
1624 1634 error = sa_replace_all_by_template_locked(hdl, attr_desc,
1625 1635 attr_count, tx);
1626 1636 mutex_exit(&hdl->sa_lock);
1627 1637 return (error);
1628 1638 }
1629 1639
1630 1640 /*
1631 1641 * add/remove/replace a single attribute and then rewrite the entire set
1632 1642 * of attributes.
1633 1643 */
1634 1644 static int
1635 1645 sa_modify_attrs(sa_handle_t *hdl, sa_attr_type_t newattr,
1636 1646 sa_data_op_t action, sa_data_locator_t *locator, void *datastart,
1637 1647 uint16_t buflen, dmu_tx_t *tx)
1638 1648 {
1639 1649 sa_os_t *sa = hdl->sa_os->os_sa;
1640 1650 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1641 1651 dnode_t *dn;
1642 1652 sa_bulk_attr_t *attr_desc;
1643 1653 void *old_data[2];
1644 1654 int bonus_attr_count = 0;
1645 1655 int bonus_data_size = 0;
1646 1656 int spill_data_size = 0;
1647 1657 int spill_attr_count = 0;
1648 1658 int error;
1649 1659 uint16_t length;
1650 1660 int i, j, k, length_idx;
1651 1661 sa_hdr_phys_t *hdr;
1652 1662 sa_idx_tab_t *idx_tab;
1653 1663 int attr_count;
1654 1664 int count;
1655 1665
1656 1666 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1657 1667
1658 1668 /* First make of copy of the old data */
1659 1669
1660 1670 DB_DNODE_ENTER(db);
1661 1671 dn = DB_DNODE(db);
1662 1672 if (dn->dn_bonuslen != 0) {
1663 1673 bonus_data_size = hdl->sa_bonus->db_size;
1664 1674 old_data[0] = kmem_alloc(bonus_data_size, KM_SLEEP);
1665 1675 bcopy(hdl->sa_bonus->db_data, old_data[0],
1666 1676 hdl->sa_bonus->db_size);
1667 1677 bonus_attr_count = hdl->sa_bonus_tab->sa_layout->lot_attr_count;
1668 1678 } else {
1669 1679 old_data[0] = NULL;
1670 1680 }
1671 1681 DB_DNODE_EXIT(db);
1672 1682
1673 1683 /* Bring spill buffer online if it isn't currently */
1674 1684
1675 1685 if ((error = sa_get_spill(hdl)) == 0) {
1676 1686 spill_data_size = hdl->sa_spill->db_size;
1677 1687 old_data[1] = kmem_alloc(spill_data_size, KM_SLEEP);
1678 1688 bcopy(hdl->sa_spill->db_data, old_data[1],
1679 1689 hdl->sa_spill->db_size);
1680 1690 spill_attr_count =
1681 1691 hdl->sa_spill_tab->sa_layout->lot_attr_count;
1682 1692 } else if (error && error != ENOENT) {
1683 1693 if (old_data[0])
1684 1694 kmem_free(old_data[0], bonus_data_size);
1685 1695 return (error);
1686 1696 } else {
1687 1697 old_data[1] = NULL;
1688 1698 }
1689 1699
1690 1700 /* build descriptor of all attributes */
1691 1701
1692 1702 attr_count = bonus_attr_count + spill_attr_count;
1693 1703 if (action == SA_ADD)
1694 1704 attr_count++;
1695 1705 else if (action == SA_REMOVE)
1696 1706 attr_count--;
1697 1707
1698 1708 attr_desc = kmem_zalloc(sizeof (sa_bulk_attr_t) * attr_count, KM_SLEEP);
1699 1709
1700 1710 /*
1701 1711 * loop through bonus and spill buffer if it exists, and
1702 1712 * build up new attr_descriptor to reset the attributes
1703 1713 */
1704 1714 k = j = 0;
1705 1715 count = bonus_attr_count;
1706 1716 hdr = SA_GET_HDR(hdl, SA_BONUS);
1707 1717 idx_tab = SA_IDX_TAB_GET(hdl, SA_BONUS);
1708 1718 for (; k != 2; k++) {
1709 1719 /* iterate over each attribute in layout */
1710 1720 for (i = 0, length_idx = 0; i != count; i++) {
1711 1721 sa_attr_type_t attr;
1712 1722
1713 1723 attr = idx_tab->sa_layout->lot_attrs[i];
1714 1724 if (attr == newattr) {
1715 1725 if (action == SA_REMOVE) {
1716 1726 j++;
1717 1727 continue;
1718 1728 }
1719 1729 ASSERT(SA_REGISTERED_LEN(sa, attr) == 0);
1720 1730 ASSERT(action == SA_REPLACE);
1721 1731 SA_ADD_BULK_ATTR(attr_desc, j, attr,
1722 1732 locator, datastart, buflen);
1723 1733 } else {
1724 1734 length = SA_REGISTERED_LEN(sa, attr);
1725 1735 if (length == 0) {
1726 1736 length = hdr->sa_lengths[length_idx++];
1727 1737 }
1728 1738
1729 1739 SA_ADD_BULK_ATTR(attr_desc, j, attr,
1730 1740 NULL, (void *)
1731 1741 (TOC_OFF(idx_tab->sa_idx_tab[attr]) +
1732 1742 (uintptr_t)old_data[k]), length);
1733 1743 }
1734 1744 }
1735 1745 if (k == 0 && hdl->sa_spill) {
1736 1746 hdr = SA_GET_HDR(hdl, SA_SPILL);
1737 1747 idx_tab = SA_IDX_TAB_GET(hdl, SA_SPILL);
1738 1748 count = spill_attr_count;
1739 1749 } else {
1740 1750 break;
1741 1751 }
1742 1752 }
1743 1753 if (action == SA_ADD) {
1744 1754 length = SA_REGISTERED_LEN(sa, newattr);
1745 1755 if (length == 0) {
1746 1756 length = buflen;
1747 1757 }
1748 1758 SA_ADD_BULK_ATTR(attr_desc, j, newattr, locator,
1749 1759 datastart, buflen);
1750 1760 }
1751 1761
1752 1762 error = sa_build_layouts(hdl, attr_desc, attr_count, tx);
1753 1763
1754 1764 if (old_data[0])
1755 1765 kmem_free(old_data[0], bonus_data_size);
1756 1766 if (old_data[1])
1757 1767 kmem_free(old_data[1], spill_data_size);
1758 1768 kmem_free(attr_desc, sizeof (sa_bulk_attr_t) * attr_count);
1759 1769
1760 1770 return (error);
1761 1771 }
1762 1772
1763 1773 static int
1764 1774 sa_bulk_update_impl(sa_handle_t *hdl, sa_bulk_attr_t *bulk, int count,
1765 1775 dmu_tx_t *tx)
1766 1776 {
1767 1777 int error;
1768 1778 sa_os_t *sa = hdl->sa_os->os_sa;
1769 1779 dmu_object_type_t bonustype;
1770 1780
1771 1781 bonustype = SA_BONUSTYPE_FROM_DB(SA_GET_DB(hdl, SA_BONUS));
1772 1782
1773 1783 ASSERT(hdl);
1774 1784 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1775 1785
1776 1786 /* sync out registration table if necessary */
1777 1787 if (sa->sa_need_attr_registration)
1778 1788 sa_attr_register_sync(hdl, tx);
1779 1789
1780 1790 error = sa_attr_op(hdl, bulk, count, SA_UPDATE, tx);
1781 1791 if (error == 0 && !IS_SA_BONUSTYPE(bonustype) && sa->sa_update_cb)
1782 1792 sa->sa_update_cb(hdl, tx);
1783 1793
1784 1794 return (error);
1785 1795 }
1786 1796
1787 1797 /*
1788 1798 * update or add new attribute
1789 1799 */
1790 1800 int
1791 1801 sa_update(sa_handle_t *hdl, sa_attr_type_t type,
1792 1802 void *buf, uint32_t buflen, dmu_tx_t *tx)
1793 1803 {
1794 1804 int error;
1795 1805 sa_bulk_attr_t bulk;
1796 1806
1797 1807 bulk.sa_attr = type;
1798 1808 bulk.sa_data_func = NULL;
1799 1809 bulk.sa_length = buflen;
1800 1810 bulk.sa_data = buf;
1801 1811
1802 1812 mutex_enter(&hdl->sa_lock);
1803 1813 error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
1804 1814 mutex_exit(&hdl->sa_lock);
1805 1815 return (error);
1806 1816 }
1807 1817
1808 1818 int
1809 1819 sa_update_from_cb(sa_handle_t *hdl, sa_attr_type_t attr,
1810 1820 uint32_t buflen, sa_data_locator_t *locator, void *userdata, dmu_tx_t *tx)
1811 1821 {
1812 1822 int error;
1813 1823 sa_bulk_attr_t bulk;
1814 1824
1815 1825 bulk.sa_attr = attr;
1816 1826 bulk.sa_data = userdata;
1817 1827 bulk.sa_data_func = locator;
1818 1828 bulk.sa_length = buflen;
1819 1829
1820 1830 mutex_enter(&hdl->sa_lock);
1821 1831 error = sa_bulk_update_impl(hdl, &bulk, 1, tx);
1822 1832 mutex_exit(&hdl->sa_lock);
1823 1833 return (error);
1824 1834 }
1825 1835
1826 1836 /*
1827 1837 * Return size of an attribute
1828 1838 */
1829 1839
1830 1840 int
1831 1841 sa_size(sa_handle_t *hdl, sa_attr_type_t attr, int *size)
1832 1842 {
1833 1843 sa_bulk_attr_t bulk;
1834 1844 int error;
1835 1845
1836 1846 bulk.sa_data = NULL;
1837 1847 bulk.sa_attr = attr;
1838 1848 bulk.sa_data_func = NULL;
1839 1849
1840 1850 ASSERT(hdl);
1841 1851 mutex_enter(&hdl->sa_lock);
1842 1852 if ((error = sa_attr_op(hdl, &bulk, 1, SA_LOOKUP, NULL)) != 0) {
1843 1853 mutex_exit(&hdl->sa_lock);
1844 1854 return (error);
1845 1855 }
1846 1856 *size = bulk.sa_size;
1847 1857
1848 1858 mutex_exit(&hdl->sa_lock);
1849 1859 return (0);
1850 1860 }
1851 1861
1852 1862 int
1853 1863 sa_bulk_lookup_locked(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
1854 1864 {
1855 1865 ASSERT(hdl);
1856 1866 ASSERT(MUTEX_HELD(&hdl->sa_lock));
1857 1867 return (sa_lookup_impl(hdl, attrs, count));
1858 1868 }
1859 1869
1860 1870 int
1861 1871 sa_bulk_lookup(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count)
1862 1872 {
1863 1873 int error;
1864 1874
1865 1875 ASSERT(hdl);
1866 1876 mutex_enter(&hdl->sa_lock);
1867 1877 error = sa_bulk_lookup_locked(hdl, attrs, count);
1868 1878 mutex_exit(&hdl->sa_lock);
1869 1879 return (error);
1870 1880 }
1871 1881
1872 1882 int
1873 1883 sa_bulk_update(sa_handle_t *hdl, sa_bulk_attr_t *attrs, int count, dmu_tx_t *tx)
1874 1884 {
1875 1885 int error;
1876 1886
1877 1887 ASSERT(hdl);
1878 1888 mutex_enter(&hdl->sa_lock);
1879 1889 error = sa_bulk_update_impl(hdl, attrs, count, tx);
1880 1890 mutex_exit(&hdl->sa_lock);
1881 1891 return (error);
1882 1892 }
1883 1893
1884 1894 int
1885 1895 sa_remove(sa_handle_t *hdl, sa_attr_type_t attr, dmu_tx_t *tx)
1886 1896 {
1887 1897 int error;
1888 1898
1889 1899 mutex_enter(&hdl->sa_lock);
1890 1900 error = sa_modify_attrs(hdl, attr, SA_REMOVE, NULL,
1891 1901 NULL, 0, tx);
1892 1902 mutex_exit(&hdl->sa_lock);
1893 1903 return (error);
1894 1904 }
1895 1905
1896 1906 void
1897 1907 sa_object_info(sa_handle_t *hdl, dmu_object_info_t *doi)
1898 1908 {
|
↓ open down ↓ |
489 lines elided |
↑ open up ↑ |
1899 1909 dmu_object_info_from_db((dmu_buf_t *)hdl->sa_bonus, doi);
1900 1910 }
1901 1911
1902 1912 void
1903 1913 sa_object_size(sa_handle_t *hdl, uint32_t *blksize, u_longlong_t *nblocks)
1904 1914 {
1905 1915 dmu_object_size_from_db((dmu_buf_t *)hdl->sa_bonus,
1906 1916 blksize, nblocks);
1907 1917 }
1908 1918
1909 -void
1910 -sa_update_user(sa_handle_t *newhdl, sa_handle_t *oldhdl)
1911 -{
1912 - (void) dmu_buf_update_user((dmu_buf_t *)newhdl->sa_bonus,
1913 - oldhdl, newhdl, sa_evict);
1914 - oldhdl->sa_bonus = NULL;
1915 -}
1916 -
1917 1919 void
1918 1920 sa_set_userp(sa_handle_t *hdl, void *ptr)
1919 1921 {
1920 1922 hdl->sa_userp = ptr;
1921 1923 }
1922 1924
1923 1925 dmu_buf_t *
1924 1926 sa_get_db(sa_handle_t *hdl)
1925 1927 {
1926 1928 return ((dmu_buf_t *)hdl->sa_bonus);
1927 1929 }
1928 1930
1929 1931 void *
1930 1932 sa_get_userdata(sa_handle_t *hdl)
1931 1933 {
1932 1934 return (hdl->sa_userp);
1933 1935 }
1934 1936
1935 1937 void
1936 1938 sa_register_update_callback_locked(objset_t *os, sa_update_cb_t *func)
1937 1939 {
1938 1940 ASSERT(MUTEX_HELD(&os->os_sa->sa_lock));
1939 1941 os->os_sa->sa_update_cb = func;
1940 1942 }
1941 1943
1942 1944 void
1943 1945 sa_register_update_callback(objset_t *os, sa_update_cb_t *func)
1944 1946 {
1945 1947
1946 1948 mutex_enter(&os->os_sa->sa_lock);
1947 1949 sa_register_update_callback_locked(os, func);
1948 1950 mutex_exit(&os->os_sa->sa_lock);
1949 1951 }
1950 1952
1951 1953 uint64_t
1952 1954 sa_handle_object(sa_handle_t *hdl)
1953 1955 {
1954 1956 return (hdl->sa_bonus->db_object);
1955 1957 }
1956 1958
1957 1959 boolean_t
1958 1960 sa_enabled(objset_t *os)
1959 1961 {
1960 1962 return (os->os_sa == NULL);
1961 1963 }
1962 1964
1963 1965 int
1964 1966 sa_set_sa_object(objset_t *os, uint64_t sa_object)
1965 1967 {
1966 1968 sa_os_t *sa = os->os_sa;
1967 1969
1968 1970 if (sa->sa_master_obj)
1969 1971 return (1);
1970 1972
1971 1973 sa->sa_master_obj = sa_object;
1972 1974
1973 1975 return (0);
1974 1976 }
1975 1977
1976 1978 int
1977 1979 sa_hdrsize(void *arg)
1978 1980 {
1979 1981 sa_hdr_phys_t *hdr = arg;
1980 1982
1981 1983 return (SA_HDR_SIZE(hdr));
1982 1984 }
1983 1985
1984 1986 void
1985 1987 sa_handle_lock(sa_handle_t *hdl)
1986 1988 {
1987 1989 ASSERT(hdl);
1988 1990 mutex_enter(&hdl->sa_lock);
1989 1991 }
1990 1992
1991 1993 void
1992 1994 sa_handle_unlock(sa_handle_t *hdl)
1993 1995 {
1994 1996 ASSERT(hdl);
1995 1997 mutex_exit(&hdl->sa_lock);
1996 1998 }
|
↓ open down ↓ |
70 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX