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/zap.c
+++ new/usr/src/uts/common/fs/zfs/zap.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
|
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
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
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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 + * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
24 25 */
25 26
26 27 /*
27 28 * This file contains the top half of the zfs directory structure
28 29 * implementation. The bottom half is in zap_leaf.c.
29 30 *
30 31 * The zdir is an extendable hash data structure. There is a table of
31 32 * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
32 33 * each a constant size and hold a variable number of directory entries.
33 34 * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
34 35 *
35 36 * The pointer table holds a power of 2 number of pointers.
36 37 * (1<<zap_t->zd_data->zd_phys->zd_prefix_len). The bucket pointed to
37 38 * by the pointer at index i in the table holds entries whose hash value
38 39 * has a zd_prefix_len - bit prefix
39 40 */
40 41
41 42 #include <sys/spa.h>
42 43 #include <sys/dmu.h>
43 44 #include <sys/zfs_context.h>
44 45 #include <sys/zfs_znode.h>
|
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
45 46 #include <sys/fs/zfs.h>
46 47 #include <sys/zap.h>
47 48 #include <sys/refcount.h>
48 49 #include <sys/zap_impl.h>
49 50 #include <sys/zap_leaf.h>
50 51
51 52 int fzap_default_block_shift = 14; /* 16k blocksize */
52 53
53 54 extern inline zap_phys_t *zap_f_phys(zap_t *zap);
54 55
55 -static void zap_leaf_pageout(dmu_buf_t *db, void *vl);
56 56 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
57 57
58 58 void
59 59 fzap_byteswap(void *vbuf, size_t size)
60 60 {
61 61 uint64_t block_type;
62 62
63 63 block_type = *(uint64_t *)vbuf;
64 64
65 65 if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
66 66 zap_leaf_byteswap(vbuf, size);
67 67 else {
68 68 /* it's a ptrtbl block */
69 69 byteswap_uint64_array(vbuf, size);
70 70 }
71 71 }
72 72
73 73 void
|
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
74 74 fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags)
75 75 {
76 76 dmu_buf_t *db;
77 77 zap_leaf_t *l;
78 78 int i;
79 79 zap_phys_t *zp;
80 80
81 81 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
82 82 zap->zap_ismicro = FALSE;
83 83
84 - (void) dmu_buf_update_user(zap->zap_dbuf, zap, zap, zap_evict);
84 + zap->zap_dbu.dbu_evict_func = zap_evict;
85 85
86 86 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
87 87 zap->zap_f.zap_block_shift = highbit64(zap->zap_dbuf->db_size) - 1;
88 88
89 89 zp = zap_f_phys(zap);
90 90 /*
91 91 * explicitly zero it since it might be coming from an
92 92 * initialized microzap
93 93 */
94 94 bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
95 95 zp->zap_block_type = ZBT_HEADER;
96 96 zp->zap_magic = ZAP_MAGIC;
97 97
98 98 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
99 99
100 100 zp->zap_freeblk = 2; /* block 1 will be the first leaf */
101 101 zp->zap_num_leafs = 1;
102 102 zp->zap_num_entries = 0;
103 103 zp->zap_salt = zap->zap_salt;
104 104 zp->zap_normflags = zap->zap_normflags;
105 105 zp->zap_flags = flags;
106 106
107 107 /* block 1 will be the first leaf */
108 108 for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
109 109 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
110 110
111 111 /*
112 112 * set up block 1 - the first leaf
113 113 */
114 114 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
115 115 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
116 116 dmu_buf_will_dirty(db, tx);
117 117
118 118 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
119 119 l->l_dbuf = db;
120 120
121 121 zap_leaf_init(l, zp->zap_normflags != 0);
122 122
123 123 kmem_free(l, sizeof (zap_leaf_t));
124 124 dmu_buf_rele(db, FTAG);
125 125 }
126 126
127 127 static int
128 128 zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx)
129 129 {
130 130 if (RW_WRITE_HELD(&zap->zap_rwlock))
131 131 return (1);
132 132 if (rw_tryupgrade(&zap->zap_rwlock)) {
133 133 dmu_buf_will_dirty(zap->zap_dbuf, tx);
134 134 return (1);
135 135 }
136 136 return (0);
137 137 }
138 138
139 139 /*
140 140 * Generic routines for dealing with the pointer & cookie tables.
141 141 */
142 142
143 143 static int
144 144 zap_table_grow(zap_t *zap, zap_table_phys_t *tbl,
145 145 void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n),
146 146 dmu_tx_t *tx)
147 147 {
148 148 uint64_t b, newblk;
149 149 dmu_buf_t *db_old, *db_new;
150 150 int err;
151 151 int bs = FZAP_BLOCK_SHIFT(zap);
152 152 int hepb = 1<<(bs-4);
153 153 /* hepb = half the number of entries in a block */
154 154
155 155 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
156 156 ASSERT(tbl->zt_blk != 0);
157 157 ASSERT(tbl->zt_numblks > 0);
158 158
159 159 if (tbl->zt_nextblk != 0) {
160 160 newblk = tbl->zt_nextblk;
161 161 } else {
162 162 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2);
163 163 tbl->zt_nextblk = newblk;
164 164 ASSERT0(tbl->zt_blks_copied);
165 165 dmu_prefetch(zap->zap_objset, zap->zap_object,
166 166 tbl->zt_blk << bs, tbl->zt_numblks << bs);
167 167 }
168 168
169 169 /*
170 170 * Copy the ptrtbl from the old to new location.
171 171 */
172 172
173 173 b = tbl->zt_blks_copied;
174 174 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
175 175 (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
176 176 if (err)
177 177 return (err);
178 178
179 179 /* first half of entries in old[b] go to new[2*b+0] */
180 180 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
181 181 (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
182 182 dmu_buf_will_dirty(db_new, tx);
183 183 transfer_func(db_old->db_data, db_new->db_data, hepb);
184 184 dmu_buf_rele(db_new, FTAG);
185 185
186 186 /* second half of entries in old[b] go to new[2*b+1] */
187 187 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
188 188 (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
189 189 dmu_buf_will_dirty(db_new, tx);
190 190 transfer_func((uint64_t *)db_old->db_data + hepb,
191 191 db_new->db_data, hepb);
192 192 dmu_buf_rele(db_new, FTAG);
193 193
194 194 dmu_buf_rele(db_old, FTAG);
195 195
196 196 tbl->zt_blks_copied++;
197 197
198 198 dprintf("copied block %llu of %llu\n",
199 199 tbl->zt_blks_copied, tbl->zt_numblks);
200 200
201 201 if (tbl->zt_blks_copied == tbl->zt_numblks) {
202 202 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
203 203 tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
204 204
205 205 tbl->zt_blk = newblk;
206 206 tbl->zt_numblks *= 2;
207 207 tbl->zt_shift++;
208 208 tbl->zt_nextblk = 0;
209 209 tbl->zt_blks_copied = 0;
210 210
211 211 dprintf("finished; numblocks now %llu (%lluk entries)\n",
212 212 tbl->zt_numblks, 1<<(tbl->zt_shift-10));
213 213 }
214 214
215 215 return (0);
216 216 }
217 217
218 218 static int
219 219 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
220 220 dmu_tx_t *tx)
221 221 {
222 222 int err;
223 223 uint64_t blk, off;
224 224 int bs = FZAP_BLOCK_SHIFT(zap);
225 225 dmu_buf_t *db;
226 226
227 227 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
228 228 ASSERT(tbl->zt_blk != 0);
229 229
230 230 dprintf("storing %llx at index %llx\n", val, idx);
231 231
232 232 blk = idx >> (bs-3);
233 233 off = idx & ((1<<(bs-3))-1);
234 234
235 235 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
236 236 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
237 237 if (err)
238 238 return (err);
239 239 dmu_buf_will_dirty(db, tx);
240 240
241 241 if (tbl->zt_nextblk != 0) {
242 242 uint64_t idx2 = idx * 2;
243 243 uint64_t blk2 = idx2 >> (bs-3);
244 244 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
245 245 dmu_buf_t *db2;
246 246
247 247 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
248 248 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
249 249 DMU_READ_NO_PREFETCH);
250 250 if (err) {
251 251 dmu_buf_rele(db, FTAG);
252 252 return (err);
253 253 }
254 254 dmu_buf_will_dirty(db2, tx);
255 255 ((uint64_t *)db2->db_data)[off2] = val;
256 256 ((uint64_t *)db2->db_data)[off2+1] = val;
257 257 dmu_buf_rele(db2, FTAG);
258 258 }
259 259
260 260 ((uint64_t *)db->db_data)[off] = val;
261 261 dmu_buf_rele(db, FTAG);
262 262
263 263 return (0);
264 264 }
265 265
266 266 static int
267 267 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
268 268 {
269 269 uint64_t blk, off;
270 270 int err;
271 271 dmu_buf_t *db;
272 272 int bs = FZAP_BLOCK_SHIFT(zap);
273 273
274 274 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
275 275
276 276 blk = idx >> (bs-3);
277 277 off = idx & ((1<<(bs-3))-1);
278 278
279 279 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
280 280 (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
281 281 if (err)
282 282 return (err);
283 283 *valp = ((uint64_t *)db->db_data)[off];
284 284 dmu_buf_rele(db, FTAG);
285 285
286 286 if (tbl->zt_nextblk != 0) {
287 287 /*
288 288 * read the nextblk for the sake of i/o error checking,
289 289 * so that zap_table_load() will catch errors for
290 290 * zap_table_store.
291 291 */
292 292 blk = (idx*2) >> (bs-3);
293 293
294 294 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
295 295 (tbl->zt_nextblk + blk) << bs, FTAG, &db,
296 296 DMU_READ_NO_PREFETCH);
297 297 if (err == 0)
298 298 dmu_buf_rele(db, FTAG);
299 299 }
300 300 return (err);
301 301 }
302 302
303 303 /*
304 304 * Routines for growing the ptrtbl.
305 305 */
306 306
307 307 static void
308 308 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
309 309 {
310 310 int i;
311 311 for (i = 0; i < n; i++) {
312 312 uint64_t lb = src[i];
313 313 dst[2*i+0] = lb;
314 314 dst[2*i+1] = lb;
315 315 }
316 316 }
317 317
318 318 static int
319 319 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
320 320 {
321 321 /*
322 322 * The pointer table should never use more hash bits than we
323 323 * have (otherwise we'd be using useless zero bits to index it).
324 324 * If we are within 2 bits of running out, stop growing, since
325 325 * this is already an aberrant condition.
326 326 */
327 327 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
328 328 return (SET_ERROR(ENOSPC));
329 329
330 330 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
331 331 /*
332 332 * We are outgrowing the "embedded" ptrtbl (the one
333 333 * stored in the header block). Give it its own entire
334 334 * block, which will double the size of the ptrtbl.
335 335 */
336 336 uint64_t newblk;
337 337 dmu_buf_t *db_new;
338 338 int err;
339 339
340 340 ASSERT3U(zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
341 341 ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
342 342 ASSERT0(zap_f_phys(zap)->zap_ptrtbl.zt_blk);
343 343
344 344 newblk = zap_allocate_blocks(zap, 1);
345 345 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
346 346 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
347 347 DMU_READ_NO_PREFETCH);
348 348 if (err)
349 349 return (err);
350 350 dmu_buf_will_dirty(db_new, tx);
351 351 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
352 352 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
353 353 dmu_buf_rele(db_new, FTAG);
354 354
355 355 zap_f_phys(zap)->zap_ptrtbl.zt_blk = newblk;
356 356 zap_f_phys(zap)->zap_ptrtbl.zt_numblks = 1;
357 357 zap_f_phys(zap)->zap_ptrtbl.zt_shift++;
358 358
359 359 ASSERT3U(1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift, ==,
360 360 zap_f_phys(zap)->zap_ptrtbl.zt_numblks <<
361 361 (FZAP_BLOCK_SHIFT(zap)-3));
362 362
363 363 return (0);
364 364 } else {
365 365 return (zap_table_grow(zap, &zap_f_phys(zap)->zap_ptrtbl,
366 366 zap_ptrtbl_transfer, tx));
367 367 }
368 368 }
369 369
370 370 static void
371 371 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
372 372 {
373 373 dmu_buf_will_dirty(zap->zap_dbuf, tx);
374 374 mutex_enter(&zap->zap_f.zap_num_entries_mtx);
375 375 ASSERT(delta > 0 || zap_f_phys(zap)->zap_num_entries >= -delta);
376 376 zap_f_phys(zap)->zap_num_entries += delta;
377 377 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
378 378 }
379 379
|
↓ open down ↓ |
285 lines elided |
↑ open up ↑ |
380 380 static uint64_t
381 381 zap_allocate_blocks(zap_t *zap, int nblocks)
382 382 {
383 383 uint64_t newblk;
384 384 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
385 385 newblk = zap_f_phys(zap)->zap_freeblk;
386 386 zap_f_phys(zap)->zap_freeblk += nblocks;
387 387 return (newblk);
388 388 }
389 389
390 +static void
391 +zap_leaf_pageout(void *dbu)
392 +{
393 + zap_leaf_t *l = dbu;
394 +
395 + rw_destroy(&l->l_rwlock);
396 + kmem_free(l, sizeof (zap_leaf_t));
397 +}
398 +
390 399 static zap_leaf_t *
391 400 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
392 401 {
393 402 void *winner;
394 - zap_leaf_t *l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
403 + zap_leaf_t *l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
395 404
396 405 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
397 406
398 407 rw_init(&l->l_rwlock, 0, 0, 0);
399 408 rw_enter(&l->l_rwlock, RW_WRITER);
400 409 l->l_blkid = zap_allocate_blocks(zap, 1);
401 410 l->l_dbuf = NULL;
402 411
403 412 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
404 413 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
405 414 DMU_READ_NO_PREFETCH));
406 - winner = dmu_buf_set_user(l->l_dbuf, l, zap_leaf_pageout);
415 + dmu_buf_init_user(&l->l_dbu, zap_leaf_pageout, &l->l_dbuf);
416 + winner = dmu_buf_set_user(l->l_dbuf, &l->l_dbu);
407 417 ASSERT(winner == NULL);
408 418 dmu_buf_will_dirty(l->l_dbuf, tx);
409 419
410 420 zap_leaf_init(l, zap->zap_normflags != 0);
411 421
412 422 zap_f_phys(zap)->zap_num_leafs++;
413 423
414 424 return (l);
415 425 }
416 426
417 427 int
418 428 fzap_count(zap_t *zap, uint64_t *count)
419 429 {
420 430 ASSERT(!zap->zap_ismicro);
421 431 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
422 432 *count = zap_f_phys(zap)->zap_num_entries;
423 433 mutex_exit(&zap->zap_f.zap_num_entries_mtx);
424 434 return (0);
425 435 }
426 436
427 437 /*
|
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
428 438 * Routines for obtaining zap_leaf_t's
429 439 */
430 440
431 441 void
432 442 zap_put_leaf(zap_leaf_t *l)
433 443 {
434 444 rw_exit(&l->l_rwlock);
435 445 dmu_buf_rele(l->l_dbuf, NULL);
436 446 }
437 447
438 -_NOTE(ARGSUSED(0))
439 -static void
440 -zap_leaf_pageout(dmu_buf_t *db, void *vl)
441 -{
442 - zap_leaf_t *l = vl;
443 -
444 - rw_destroy(&l->l_rwlock);
445 - kmem_free(l, sizeof (zap_leaf_t));
446 -}
447 -
448 448 static zap_leaf_t *
449 449 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
450 450 {
451 451 zap_leaf_t *l, *winner;
452 452
453 453 ASSERT(blkid != 0);
454 454
455 - l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
455 + l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
456 456 rw_init(&l->l_rwlock, 0, 0, 0);
457 457 rw_enter(&l->l_rwlock, RW_WRITER);
458 458 l->l_blkid = blkid;
459 459 l->l_bs = highbit64(db->db_size) - 1;
460 460 l->l_dbuf = db;
461 461
462 - winner = dmu_buf_set_user(db, l, zap_leaf_pageout);
462 + dmu_buf_init_user(&l->l_dbu, zap_leaf_pageout, &l->l_dbuf);
463 + winner = dmu_buf_set_user(db, &l->l_dbu);
463 464
464 465 rw_exit(&l->l_rwlock);
465 466 if (winner != NULL) {
466 467 /* someone else set it first */
467 - zap_leaf_pageout(NULL, l);
468 + zap_leaf_pageout(&l->l_dbu);
468 469 l = winner;
469 470 }
470 471
471 472 /*
472 473 * lhr_pad was previously used for the next leaf in the leaf
473 474 * chain. There should be no chained leafs (as we have removed
474 475 * support for them).
475 476 */
476 477 ASSERT0(zap_leaf_phys(l)->l_hdr.lh_pad1);
477 478
478 479 /*
479 480 * There should be more hash entries than there can be
480 481 * chunks to put in the hash table
481 482 */
482 483 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
483 484
484 485 /* The chunks should begin at the end of the hash table */
485 486 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
486 487 &zap_leaf_phys(l)->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
487 488
488 489 /* The chunks should end at the end of the block */
489 490 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
490 491 (uintptr_t)zap_leaf_phys(l), ==, l->l_dbuf->db_size);
491 492
492 493 return (l);
493 494 }
494 495
495 496 static int
496 497 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
497 498 zap_leaf_t **lp)
498 499 {
499 500 dmu_buf_t *db;
500 501 zap_leaf_t *l;
501 502 int bs = FZAP_BLOCK_SHIFT(zap);
502 503 int err;
503 504
504 505 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
505 506
506 507 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
507 508 blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
508 509 if (err)
509 510 return (err);
510 511
511 512 ASSERT3U(db->db_object, ==, zap->zap_object);
512 513 ASSERT3U(db->db_offset, ==, blkid << bs);
513 514 ASSERT3U(db->db_size, ==, 1 << bs);
514 515 ASSERT(blkid != 0);
515 516
516 517 l = dmu_buf_get_user(db);
517 518
518 519 if (l == NULL)
519 520 l = zap_open_leaf(blkid, db);
520 521
521 522 rw_enter(&l->l_rwlock, lt);
522 523 /*
523 524 * Must lock before dirtying, otherwise zap_leaf_phys(l) could change,
524 525 * causing ASSERT below to fail.
525 526 */
526 527 if (lt == RW_WRITER)
527 528 dmu_buf_will_dirty(db, tx);
528 529 ASSERT3U(l->l_blkid, ==, blkid);
529 530 ASSERT3P(l->l_dbuf, ==, db);
530 531 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_block_type, ==, ZBT_LEAF);
531 532 ASSERT3U(zap_leaf_phys(l)->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
532 533
533 534 *lp = l;
534 535 return (0);
535 536 }
536 537
537 538 static int
538 539 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
539 540 {
540 541 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
541 542
542 543 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
543 544 ASSERT3U(idx, <,
544 545 (1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift));
545 546 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
546 547 return (0);
547 548 } else {
548 549 return (zap_table_load(zap, &zap_f_phys(zap)->zap_ptrtbl,
549 550 idx, valp));
550 551 }
551 552 }
552 553
553 554 static int
554 555 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
555 556 {
556 557 ASSERT(tx != NULL);
557 558 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
558 559
559 560 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0) {
560 561 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
561 562 return (0);
562 563 } else {
563 564 return (zap_table_store(zap, &zap_f_phys(zap)->zap_ptrtbl,
564 565 idx, blk, tx));
565 566 }
566 567 }
567 568
568 569 static int
569 570 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
570 571 {
571 572 uint64_t idx, blk;
572 573 int err;
573 574
574 575 ASSERT(zap->zap_dbuf == NULL ||
575 576 zap_f_phys(zap) == zap->zap_dbuf->db_data);
576 577 ASSERT3U(zap_f_phys(zap)->zap_magic, ==, ZAP_MAGIC);
577 578 idx = ZAP_HASH_IDX(h, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
578 579 err = zap_idx_to_blk(zap, idx, &blk);
579 580 if (err != 0)
580 581 return (err);
581 582 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
582 583
583 584 ASSERT(err ||
584 585 ZAP_HASH_IDX(h, zap_leaf_phys(*lp)->l_hdr.lh_prefix_len) ==
585 586 zap_leaf_phys(*lp)->l_hdr.lh_prefix);
586 587 return (err);
587 588 }
588 589
589 590 static int
590 591 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx, zap_leaf_t **lp)
591 592 {
592 593 zap_t *zap = zn->zn_zap;
593 594 uint64_t hash = zn->zn_hash;
594 595 zap_leaf_t *nl;
595 596 int prefix_diff, i, err;
596 597 uint64_t sibling;
597 598 int old_prefix_len = zap_leaf_phys(l)->l_hdr.lh_prefix_len;
598 599
599 600 ASSERT3U(old_prefix_len, <=, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
600 601 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
601 602
602 603 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
603 604 zap_leaf_phys(l)->l_hdr.lh_prefix);
604 605
605 606 if (zap_tryupgradedir(zap, tx) == 0 ||
606 607 old_prefix_len == zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
607 608 /* We failed to upgrade, or need to grow the pointer table */
608 609 objset_t *os = zap->zap_objset;
609 610 uint64_t object = zap->zap_object;
610 611
611 612 zap_put_leaf(l);
612 613 zap_unlockdir(zap);
613 614 err = zap_lockdir(os, object, tx, RW_WRITER,
614 615 FALSE, FALSE, &zn->zn_zap);
615 616 zap = zn->zn_zap;
616 617 if (err)
617 618 return (err);
618 619 ASSERT(!zap->zap_ismicro);
619 620
620 621 while (old_prefix_len ==
621 622 zap_f_phys(zap)->zap_ptrtbl.zt_shift) {
622 623 err = zap_grow_ptrtbl(zap, tx);
623 624 if (err)
624 625 return (err);
625 626 }
626 627
627 628 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
628 629 if (err)
629 630 return (err);
630 631
631 632 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len != old_prefix_len) {
632 633 /* it split while our locks were down */
633 634 *lp = l;
634 635 return (0);
635 636 }
636 637 }
637 638 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
638 639 ASSERT3U(old_prefix_len, <, zap_f_phys(zap)->zap_ptrtbl.zt_shift);
639 640 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
640 641 zap_leaf_phys(l)->l_hdr.lh_prefix);
641 642
642 643 prefix_diff = zap_f_phys(zap)->zap_ptrtbl.zt_shift -
643 644 (old_prefix_len + 1);
644 645 sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
645 646
646 647 /* check for i/o errors before doing zap_leaf_split */
647 648 for (i = 0; i < (1ULL<<prefix_diff); i++) {
648 649 uint64_t blk;
649 650 err = zap_idx_to_blk(zap, sibling+i, &blk);
650 651 if (err)
651 652 return (err);
652 653 ASSERT3U(blk, ==, l->l_blkid);
653 654 }
654 655
655 656 nl = zap_create_leaf(zap, tx);
656 657 zap_leaf_split(l, nl, zap->zap_normflags != 0);
657 658
658 659 /* set sibling pointers */
659 660 for (i = 0; i < (1ULL << prefix_diff); i++) {
660 661 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
661 662 ASSERT0(err); /* we checked for i/o errors above */
662 663 }
663 664
664 665 if (hash & (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len))) {
665 666 /* we want the sibling */
666 667 zap_put_leaf(l);
667 668 *lp = nl;
668 669 } else {
669 670 zap_put_leaf(nl);
670 671 *lp = l;
671 672 }
672 673
673 674 return (0);
674 675 }
675 676
676 677 static void
677 678 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx)
678 679 {
679 680 zap_t *zap = zn->zn_zap;
680 681 int shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
681 682 int leaffull = (zap_leaf_phys(l)->l_hdr.lh_prefix_len == shift &&
682 683 zap_leaf_phys(l)->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
683 684
684 685 zap_put_leaf(l);
685 686
686 687 if (leaffull || zap_f_phys(zap)->zap_ptrtbl.zt_nextblk) {
687 688 int err;
688 689
689 690 /*
690 691 * We are in the middle of growing the pointer table, or
691 692 * this leaf will soon make us grow it.
692 693 */
693 694 if (zap_tryupgradedir(zap, tx) == 0) {
694 695 objset_t *os = zap->zap_objset;
695 696 uint64_t zapobj = zap->zap_object;
696 697
697 698 zap_unlockdir(zap);
698 699 err = zap_lockdir(os, zapobj, tx,
699 700 RW_WRITER, FALSE, FALSE, &zn->zn_zap);
700 701 zap = zn->zn_zap;
701 702 if (err)
702 703 return;
703 704 }
704 705
705 706 /* could have finished growing while our locks were down */
706 707 if (zap_f_phys(zap)->zap_ptrtbl.zt_shift == shift)
707 708 (void) zap_grow_ptrtbl(zap, tx);
708 709 }
709 710 }
710 711
711 712 static int
712 713 fzap_checkname(zap_name_t *zn)
713 714 {
714 715 if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
715 716 return (SET_ERROR(ENAMETOOLONG));
716 717 return (0);
717 718 }
718 719
719 720 static int
720 721 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
721 722 {
722 723 /* Only integer sizes supported by C */
723 724 switch (integer_size) {
724 725 case 1:
725 726 case 2:
726 727 case 4:
727 728 case 8:
728 729 break;
729 730 default:
730 731 return (SET_ERROR(EINVAL));
731 732 }
732 733
733 734 if (integer_size * num_integers > ZAP_MAXVALUELEN)
734 735 return (E2BIG);
735 736
736 737 return (0);
737 738 }
738 739
739 740 static int
740 741 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
741 742 {
742 743 int err;
743 744
744 745 if ((err = fzap_checkname(zn)) != 0)
745 746 return (err);
746 747 return (fzap_checksize(integer_size, num_integers));
747 748 }
748 749
749 750 /*
750 751 * Routines for manipulating attributes.
751 752 */
752 753 int
753 754 fzap_lookup(zap_name_t *zn,
754 755 uint64_t integer_size, uint64_t num_integers, void *buf,
755 756 char *realname, int rn_len, boolean_t *ncp)
756 757 {
757 758 zap_leaf_t *l;
758 759 int err;
759 760 zap_entry_handle_t zeh;
760 761
761 762 if ((err = fzap_checkname(zn)) != 0)
762 763 return (err);
763 764
764 765 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
765 766 if (err != 0)
766 767 return (err);
767 768 err = zap_leaf_lookup(l, zn, &zeh);
768 769 if (err == 0) {
769 770 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
770 771 zap_put_leaf(l);
771 772 return (err);
772 773 }
773 774
774 775 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
775 776 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
776 777 if (ncp) {
777 778 *ncp = zap_entry_normalization_conflict(&zeh,
778 779 zn, NULL, zn->zn_zap);
779 780 }
780 781 }
781 782
782 783 zap_put_leaf(l);
783 784 return (err);
784 785 }
785 786
786 787 int
787 788 fzap_add_cd(zap_name_t *zn,
788 789 uint64_t integer_size, uint64_t num_integers,
789 790 const void *val, uint32_t cd, dmu_tx_t *tx)
790 791 {
791 792 zap_leaf_t *l;
792 793 int err;
793 794 zap_entry_handle_t zeh;
794 795 zap_t *zap = zn->zn_zap;
795 796
796 797 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
797 798 ASSERT(!zap->zap_ismicro);
798 799 ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
799 800
800 801 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
801 802 if (err != 0)
802 803 return (err);
803 804 retry:
804 805 err = zap_leaf_lookup(l, zn, &zeh);
805 806 if (err == 0) {
806 807 err = SET_ERROR(EEXIST);
807 808 goto out;
808 809 }
809 810 if (err != ENOENT)
810 811 goto out;
811 812
812 813 err = zap_entry_create(l, zn, cd,
813 814 integer_size, num_integers, val, &zeh);
814 815
815 816 if (err == 0) {
816 817 zap_increment_num_entries(zap, 1, tx);
817 818 } else if (err == EAGAIN) {
818 819 err = zap_expand_leaf(zn, l, tx, &l);
819 820 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
820 821 if (err == 0)
821 822 goto retry;
822 823 }
823 824
824 825 out:
825 826 if (zap != NULL)
826 827 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
827 828 return (err);
828 829 }
829 830
830 831 int
831 832 fzap_add(zap_name_t *zn,
832 833 uint64_t integer_size, uint64_t num_integers,
833 834 const void *val, dmu_tx_t *tx)
834 835 {
835 836 int err = fzap_check(zn, integer_size, num_integers);
836 837 if (err != 0)
837 838 return (err);
838 839
839 840 return (fzap_add_cd(zn, integer_size, num_integers,
840 841 val, ZAP_NEED_CD, tx));
841 842 }
842 843
843 844 int
844 845 fzap_update(zap_name_t *zn,
845 846 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
846 847 {
847 848 zap_leaf_t *l;
848 849 int err, create;
849 850 zap_entry_handle_t zeh;
850 851 zap_t *zap = zn->zn_zap;
851 852
852 853 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
853 854 err = fzap_check(zn, integer_size, num_integers);
854 855 if (err != 0)
855 856 return (err);
856 857
857 858 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
858 859 if (err != 0)
859 860 return (err);
860 861 retry:
861 862 err = zap_leaf_lookup(l, zn, &zeh);
862 863 create = (err == ENOENT);
863 864 ASSERT(err == 0 || err == ENOENT);
864 865
865 866 if (create) {
866 867 err = zap_entry_create(l, zn, ZAP_NEED_CD,
867 868 integer_size, num_integers, val, &zeh);
868 869 if (err == 0)
869 870 zap_increment_num_entries(zap, 1, tx);
870 871 } else {
871 872 err = zap_entry_update(&zeh, integer_size, num_integers, val);
872 873 }
873 874
874 875 if (err == EAGAIN) {
875 876 err = zap_expand_leaf(zn, l, tx, &l);
876 877 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */
877 878 if (err == 0)
878 879 goto retry;
879 880 }
880 881
881 882 if (zap != NULL)
882 883 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
883 884 return (err);
884 885 }
885 886
886 887 int
887 888 fzap_length(zap_name_t *zn,
888 889 uint64_t *integer_size, uint64_t *num_integers)
889 890 {
890 891 zap_leaf_t *l;
891 892 int err;
892 893 zap_entry_handle_t zeh;
893 894
894 895 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
895 896 if (err != 0)
896 897 return (err);
897 898 err = zap_leaf_lookup(l, zn, &zeh);
898 899 if (err != 0)
899 900 goto out;
900 901
901 902 if (integer_size)
902 903 *integer_size = zeh.zeh_integer_size;
903 904 if (num_integers)
904 905 *num_integers = zeh.zeh_num_integers;
905 906 out:
906 907 zap_put_leaf(l);
907 908 return (err);
908 909 }
909 910
910 911 int
911 912 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
912 913 {
913 914 zap_leaf_t *l;
914 915 int err;
915 916 zap_entry_handle_t zeh;
916 917
917 918 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
918 919 if (err != 0)
919 920 return (err);
920 921 err = zap_leaf_lookup(l, zn, &zeh);
921 922 if (err == 0) {
922 923 zap_entry_remove(&zeh);
923 924 zap_increment_num_entries(zn->zn_zap, -1, tx);
924 925 }
925 926 zap_put_leaf(l);
926 927 return (err);
927 928 }
928 929
929 930 void
930 931 fzap_prefetch(zap_name_t *zn)
931 932 {
932 933 uint64_t idx, blk;
933 934 zap_t *zap = zn->zn_zap;
934 935 int bs;
935 936
936 937 idx = ZAP_HASH_IDX(zn->zn_hash,
937 938 zap_f_phys(zap)->zap_ptrtbl.zt_shift);
938 939 if (zap_idx_to_blk(zap, idx, &blk) != 0)
939 940 return;
940 941 bs = FZAP_BLOCK_SHIFT(zap);
941 942 dmu_prefetch(zap->zap_objset, zap->zap_object, blk << bs, 1 << bs);
942 943 }
943 944
944 945 /*
945 946 * Helper functions for consumers.
946 947 */
947 948
948 949 uint64_t
949 950 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
950 951 const char *name, dmu_tx_t *tx)
951 952 {
952 953 uint64_t new_obj;
953 954
954 955 VERIFY((new_obj = zap_create(os, ot, DMU_OT_NONE, 0, tx)) > 0);
955 956 VERIFY(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
956 957 tx) == 0);
957 958
958 959 return (new_obj);
959 960 }
960 961
961 962 int
962 963 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
963 964 char *name)
964 965 {
965 966 zap_cursor_t zc;
966 967 zap_attribute_t *za;
967 968 int err;
968 969
969 970 if (mask == 0)
970 971 mask = -1ULL;
971 972
972 973 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
973 974 for (zap_cursor_init(&zc, os, zapobj);
974 975 (err = zap_cursor_retrieve(&zc, za)) == 0;
975 976 zap_cursor_advance(&zc)) {
976 977 if ((za->za_first_integer & mask) == (value & mask)) {
977 978 (void) strcpy(name, za->za_name);
978 979 break;
979 980 }
980 981 }
981 982 zap_cursor_fini(&zc);
982 983 kmem_free(za, sizeof (zap_attribute_t));
983 984 return (err);
984 985 }
985 986
986 987 int
987 988 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
988 989 {
989 990 zap_cursor_t zc;
990 991 zap_attribute_t za;
991 992 int err;
992 993
993 994 err = 0;
994 995 for (zap_cursor_init(&zc, os, fromobj);
995 996 zap_cursor_retrieve(&zc, &za) == 0;
996 997 (void) zap_cursor_advance(&zc)) {
997 998 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
998 999 err = SET_ERROR(EINVAL);
999 1000 break;
1000 1001 }
1001 1002 err = zap_add(os, intoobj, za.za_name,
1002 1003 8, 1, &za.za_first_integer, tx);
1003 1004 if (err)
1004 1005 break;
1005 1006 }
1006 1007 zap_cursor_fini(&zc);
1007 1008 return (err);
1008 1009 }
1009 1010
1010 1011 int
1011 1012 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1012 1013 uint64_t value, dmu_tx_t *tx)
1013 1014 {
1014 1015 zap_cursor_t zc;
1015 1016 zap_attribute_t za;
1016 1017 int err;
1017 1018
1018 1019 err = 0;
1019 1020 for (zap_cursor_init(&zc, os, fromobj);
1020 1021 zap_cursor_retrieve(&zc, &za) == 0;
1021 1022 (void) zap_cursor_advance(&zc)) {
1022 1023 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1023 1024 err = SET_ERROR(EINVAL);
1024 1025 break;
1025 1026 }
1026 1027 err = zap_add(os, intoobj, za.za_name,
1027 1028 8, 1, &value, tx);
1028 1029 if (err)
1029 1030 break;
1030 1031 }
1031 1032 zap_cursor_fini(&zc);
1032 1033 return (err);
1033 1034 }
1034 1035
1035 1036 int
1036 1037 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1037 1038 dmu_tx_t *tx)
1038 1039 {
1039 1040 zap_cursor_t zc;
1040 1041 zap_attribute_t za;
1041 1042 int err;
1042 1043
1043 1044 err = 0;
1044 1045 for (zap_cursor_init(&zc, os, fromobj);
1045 1046 zap_cursor_retrieve(&zc, &za) == 0;
1046 1047 (void) zap_cursor_advance(&zc)) {
1047 1048 uint64_t delta = 0;
1048 1049
1049 1050 if (za.za_integer_length != 8 || za.za_num_integers != 1) {
1050 1051 err = SET_ERROR(EINVAL);
1051 1052 break;
1052 1053 }
1053 1054
1054 1055 err = zap_lookup(os, intoobj, za.za_name, 8, 1, &delta);
1055 1056 if (err != 0 && err != ENOENT)
1056 1057 break;
1057 1058 delta += za.za_first_integer;
1058 1059 err = zap_update(os, intoobj, za.za_name, 8, 1, &delta, tx);
1059 1060 if (err)
1060 1061 break;
1061 1062 }
1062 1063 zap_cursor_fini(&zc);
1063 1064 return (err);
1064 1065 }
1065 1066
1066 1067 int
1067 1068 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1068 1069 {
1069 1070 char name[20];
1070 1071
1071 1072 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1072 1073 return (zap_add(os, obj, name, 8, 1, &value, tx));
1073 1074 }
1074 1075
1075 1076 int
1076 1077 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1077 1078 {
1078 1079 char name[20];
1079 1080
1080 1081 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1081 1082 return (zap_remove(os, obj, name, tx));
1082 1083 }
1083 1084
1084 1085 int
1085 1086 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1086 1087 {
1087 1088 char name[20];
1088 1089
1089 1090 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1090 1091 return (zap_lookup(os, obj, name, 8, 1, &value));
1091 1092 }
1092 1093
1093 1094 int
1094 1095 zap_add_int_key(objset_t *os, uint64_t obj,
1095 1096 uint64_t key, uint64_t value, dmu_tx_t *tx)
1096 1097 {
1097 1098 char name[20];
1098 1099
1099 1100 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1100 1101 return (zap_add(os, obj, name, 8, 1, &value, tx));
1101 1102 }
1102 1103
1103 1104 int
1104 1105 zap_update_int_key(objset_t *os, uint64_t obj,
1105 1106 uint64_t key, uint64_t value, dmu_tx_t *tx)
1106 1107 {
1107 1108 char name[20];
1108 1109
1109 1110 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1110 1111 return (zap_update(os, obj, name, 8, 1, &value, tx));
1111 1112 }
1112 1113
1113 1114 int
1114 1115 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1115 1116 {
1116 1117 char name[20];
1117 1118
1118 1119 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1119 1120 return (zap_lookup(os, obj, name, 8, 1, valuep));
1120 1121 }
1121 1122
1122 1123 int
1123 1124 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1124 1125 dmu_tx_t *tx)
1125 1126 {
1126 1127 uint64_t value = 0;
1127 1128 int err;
1128 1129
1129 1130 if (delta == 0)
1130 1131 return (0);
1131 1132
1132 1133 err = zap_lookup(os, obj, name, 8, 1, &value);
1133 1134 if (err != 0 && err != ENOENT)
1134 1135 return (err);
1135 1136 value += delta;
1136 1137 if (value == 0)
1137 1138 err = zap_remove(os, obj, name, tx);
1138 1139 else
1139 1140 err = zap_update(os, obj, name, 8, 1, &value, tx);
1140 1141 return (err);
1141 1142 }
1142 1143
1143 1144 int
1144 1145 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1145 1146 dmu_tx_t *tx)
1146 1147 {
1147 1148 char name[20];
1148 1149
1149 1150 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1150 1151 return (zap_increment(os, obj, name, delta, tx));
1151 1152 }
1152 1153
1153 1154 /*
1154 1155 * Routines for iterating over the attributes.
1155 1156 */
1156 1157
1157 1158 int
1158 1159 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1159 1160 {
1160 1161 int err = ENOENT;
1161 1162 zap_entry_handle_t zeh;
1162 1163 zap_leaf_t *l;
1163 1164
1164 1165 /* retrieve the next entry at or after zc_hash/zc_cd */
1165 1166 /* if no entry, return ENOENT */
1166 1167
1167 1168 if (zc->zc_leaf &&
1168 1169 (ZAP_HASH_IDX(zc->zc_hash,
1169 1170 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix_len) !=
1170 1171 zap_leaf_phys(zc->zc_leaf)->l_hdr.lh_prefix)) {
1171 1172 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1172 1173 zap_put_leaf(zc->zc_leaf);
1173 1174 zc->zc_leaf = NULL;
1174 1175 }
1175 1176
1176 1177 again:
1177 1178 if (zc->zc_leaf == NULL) {
1178 1179 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1179 1180 &zc->zc_leaf);
1180 1181 if (err != 0)
1181 1182 return (err);
1182 1183 } else {
1183 1184 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1184 1185 }
1185 1186 l = zc->zc_leaf;
1186 1187
1187 1188 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1188 1189
1189 1190 if (err == ENOENT) {
1190 1191 uint64_t nocare =
1191 1192 (1ULL << (64 - zap_leaf_phys(l)->l_hdr.lh_prefix_len)) - 1;
1192 1193 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1193 1194 zc->zc_cd = 0;
1194 1195 if (zap_leaf_phys(l)->l_hdr.lh_prefix_len == 0 ||
1195 1196 zc->zc_hash == 0) {
1196 1197 zc->zc_hash = -1ULL;
1197 1198 } else {
1198 1199 zap_put_leaf(zc->zc_leaf);
1199 1200 zc->zc_leaf = NULL;
1200 1201 goto again;
1201 1202 }
1202 1203 }
1203 1204
1204 1205 if (err == 0) {
1205 1206 zc->zc_hash = zeh.zeh_hash;
1206 1207 zc->zc_cd = zeh.zeh_cd;
1207 1208 za->za_integer_length = zeh.zeh_integer_size;
1208 1209 za->za_num_integers = zeh.zeh_num_integers;
1209 1210 if (zeh.zeh_num_integers == 0) {
1210 1211 za->za_first_integer = 0;
1211 1212 } else {
1212 1213 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1213 1214 ASSERT(err == 0 || err == EOVERFLOW);
1214 1215 }
1215 1216 err = zap_entry_read_name(zap, &zeh,
1216 1217 sizeof (za->za_name), za->za_name);
1217 1218 ASSERT(err == 0);
1218 1219
1219 1220 za->za_normalization_conflict =
1220 1221 zap_entry_normalization_conflict(&zeh,
1221 1222 NULL, za->za_name, zap);
1222 1223 }
1223 1224 rw_exit(&zc->zc_leaf->l_rwlock);
1224 1225 return (err);
1225 1226 }
1226 1227
1227 1228 static void
1228 1229 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1229 1230 {
1230 1231 int i, err;
1231 1232 uint64_t lastblk = 0;
1232 1233
1233 1234 /*
1234 1235 * NB: if a leaf has more pointers than an entire ptrtbl block
1235 1236 * can hold, then it'll be accounted for more than once, since
1236 1237 * we won't have lastblk.
1237 1238 */
1238 1239 for (i = 0; i < len; i++) {
1239 1240 zap_leaf_t *l;
1240 1241
1241 1242 if (tbl[i] == lastblk)
1242 1243 continue;
1243 1244 lastblk = tbl[i];
1244 1245
1245 1246 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1246 1247 if (err == 0) {
1247 1248 zap_leaf_stats(zap, l, zs);
1248 1249 zap_put_leaf(l);
1249 1250 }
1250 1251 }
1251 1252 }
1252 1253
1253 1254 void
1254 1255 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1255 1256 {
1256 1257 int bs = FZAP_BLOCK_SHIFT(zap);
1257 1258 zs->zs_blocksize = 1ULL << bs;
1258 1259
1259 1260 /*
1260 1261 * Set zap_phys_t fields
1261 1262 */
1262 1263 zs->zs_num_leafs = zap_f_phys(zap)->zap_num_leafs;
1263 1264 zs->zs_num_entries = zap_f_phys(zap)->zap_num_entries;
1264 1265 zs->zs_num_blocks = zap_f_phys(zap)->zap_freeblk;
1265 1266 zs->zs_block_type = zap_f_phys(zap)->zap_block_type;
1266 1267 zs->zs_magic = zap_f_phys(zap)->zap_magic;
1267 1268 zs->zs_salt = zap_f_phys(zap)->zap_salt;
1268 1269
1269 1270 /*
1270 1271 * Set zap_ptrtbl fields
1271 1272 */
1272 1273 zs->zs_ptrtbl_len = 1ULL << zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1273 1274 zs->zs_ptrtbl_nextblk = zap_f_phys(zap)->zap_ptrtbl.zt_nextblk;
1274 1275 zs->zs_ptrtbl_blks_copied =
1275 1276 zap_f_phys(zap)->zap_ptrtbl.zt_blks_copied;
1276 1277 zs->zs_ptrtbl_zt_blk = zap_f_phys(zap)->zap_ptrtbl.zt_blk;
1277 1278 zs->zs_ptrtbl_zt_numblks = zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1278 1279 zs->zs_ptrtbl_zt_shift = zap_f_phys(zap)->zap_ptrtbl.zt_shift;
1279 1280
1280 1281 if (zap_f_phys(zap)->zap_ptrtbl.zt_numblks == 0) {
1281 1282 /* the ptrtbl is entirely in the header block. */
1282 1283 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1283 1284 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1284 1285 } else {
1285 1286 int b;
1286 1287
1287 1288 dmu_prefetch(zap->zap_objset, zap->zap_object,
1288 1289 zap_f_phys(zap)->zap_ptrtbl.zt_blk << bs,
1289 1290 zap_f_phys(zap)->zap_ptrtbl.zt_numblks << bs);
1290 1291
1291 1292 for (b = 0; b < zap_f_phys(zap)->zap_ptrtbl.zt_numblks;
1292 1293 b++) {
1293 1294 dmu_buf_t *db;
1294 1295 int err;
1295 1296
1296 1297 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1297 1298 (zap_f_phys(zap)->zap_ptrtbl.zt_blk + b) << bs,
1298 1299 FTAG, &db, DMU_READ_NO_PREFETCH);
1299 1300 if (err == 0) {
1300 1301 zap_stats_ptrtbl(zap, db->db_data,
1301 1302 1<<(bs-3), zs);
1302 1303 dmu_buf_rele(db, FTAG);
1303 1304 }
1304 1305 }
1305 1306 }
1306 1307 }
1307 1308
1308 1309 int
1309 1310 fzap_count_write(zap_name_t *zn, int add, uint64_t *towrite,
1310 1311 uint64_t *tooverwrite)
1311 1312 {
1312 1313 zap_t *zap = zn->zn_zap;
1313 1314 zap_leaf_t *l;
1314 1315 int err;
1315 1316
1316 1317 /*
1317 1318 * Account for the header block of the fatzap.
1318 1319 */
1319 1320 if (!add && dmu_buf_freeable(zap->zap_dbuf)) {
1320 1321 *tooverwrite += zap->zap_dbuf->db_size;
1321 1322 } else {
1322 1323 *towrite += zap->zap_dbuf->db_size;
1323 1324 }
1324 1325
1325 1326 /*
1326 1327 * Account for the pointer table blocks.
1327 1328 * If we are adding we need to account for the following cases :
1328 1329 * - If the pointer table is embedded, this operation could force an
1329 1330 * external pointer table.
1330 1331 * - If this already has an external pointer table this operation
1331 1332 * could extend the table.
1332 1333 */
1333 1334 if (add) {
1334 1335 if (zap_f_phys(zap)->zap_ptrtbl.zt_blk == 0)
1335 1336 *towrite += zap->zap_dbuf->db_size;
1336 1337 else
1337 1338 *towrite += (zap->zap_dbuf->db_size * 3);
1338 1339 }
1339 1340
1340 1341 /*
1341 1342 * Now, check if the block containing leaf is freeable
1342 1343 * and account accordingly.
1343 1344 */
1344 1345 err = zap_deref_leaf(zap, zn->zn_hash, NULL, RW_READER, &l);
1345 1346 if (err != 0) {
1346 1347 return (err);
1347 1348 }
1348 1349
1349 1350 if (!add && dmu_buf_freeable(l->l_dbuf)) {
1350 1351 *tooverwrite += l->l_dbuf->db_size;
1351 1352 } else {
1352 1353 /*
1353 1354 * If this an add operation, the leaf block could split.
1354 1355 * Hence, we need to account for an additional leaf block.
1355 1356 */
1356 1357 *towrite += (add ? 2 : 1) * l->l_dbuf->db_size;
1357 1358 }
1358 1359
1359 1360 zap_put_leaf(l);
1360 1361 return (0);
1361 1362 }
|
↓ open down ↓ |
884 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX