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