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