1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51491Sahrens * Common Development and Distribution License (the "License"). 61491Sahrens * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 22*9643SEric.Taylor@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens /* 27789Sahrens * The 512-byte leaf is broken into 32 16-byte chunks. 28789Sahrens * chunk number n means l_chunk[n], even though the header precedes it. 29789Sahrens * the names are stored null-terminated. 30789Sahrens */ 31789Sahrens 32*9643SEric.Taylor@Sun.COM #include <sys/spa.h> 33*9643SEric.Taylor@Sun.COM #include <sys/dmu.h> 34789Sahrens #include <sys/zfs_context.h> 35*9643SEric.Taylor@Sun.COM #include <sys/fs/zfs.h> 36789Sahrens #include <sys/zap.h> 37789Sahrens #include <sys/zap_impl.h> 38789Sahrens #include <sys/zap_leaf.h> 39789Sahrens 405331Samw static uint16_t *zap_leaf_rehash_entry(zap_leaf_t *l, uint16_t entry); 415331Samw 42789Sahrens #define CHAIN_END 0xffff /* end of the chunk chain */ 43789Sahrens 441491Sahrens /* half the (current) minimum block size */ 45789Sahrens #define MAX_ARRAY_BYTES (8<<10) 46789Sahrens 47789Sahrens #define LEAF_HASH(l, h) \ 481491Sahrens ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \ 491578Sahrens ((h) >> (64 - ZAP_LEAF_HASH_SHIFT(l)-(l)->l_phys->l_hdr.lh_prefix_len))) 50789Sahrens 51789Sahrens #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)]) 52789Sahrens 53789Sahrens 54789Sahrens static void 55789Sahrens zap_memset(void *a, int c, size_t n) 56789Sahrens { 57789Sahrens char *cp = a; 58789Sahrens char *cpend = cp + n; 59789Sahrens 60789Sahrens while (cp < cpend) 61789Sahrens *cp++ = c; 62789Sahrens } 63789Sahrens 64789Sahrens static void 65789Sahrens stv(int len, void *addr, uint64_t value) 66789Sahrens { 67789Sahrens switch (len) { 68789Sahrens case 1: 69789Sahrens *(uint8_t *)addr = value; 70789Sahrens return; 71789Sahrens case 2: 72789Sahrens *(uint16_t *)addr = value; 73789Sahrens return; 74789Sahrens case 4: 75789Sahrens *(uint32_t *)addr = value; 76789Sahrens return; 77789Sahrens case 8: 78789Sahrens *(uint64_t *)addr = value; 79789Sahrens return; 80789Sahrens } 81789Sahrens ASSERT(!"bad int len"); 82789Sahrens } 83789Sahrens 84789Sahrens static uint64_t 85789Sahrens ldv(int len, const void *addr) 86789Sahrens { 87789Sahrens switch (len) { 88789Sahrens case 1: 89789Sahrens return (*(uint8_t *)addr); 90789Sahrens case 2: 91789Sahrens return (*(uint16_t *)addr); 92789Sahrens case 4: 93789Sahrens return (*(uint32_t *)addr); 94789Sahrens case 8: 95789Sahrens return (*(uint64_t *)addr); 96789Sahrens } 97789Sahrens ASSERT(!"bad int len"); 982856Snd150628 return (0xFEEDFACEDEADBEEFULL); 99789Sahrens } 100789Sahrens 101789Sahrens void 1021491Sahrens zap_leaf_byteswap(zap_leaf_phys_t *buf, int size) 103789Sahrens { 104789Sahrens int i; 1051491Sahrens zap_leaf_t l; 1061491Sahrens l.l_bs = highbit(size)-1; 1071491Sahrens l.l_phys = buf; 108789Sahrens 1091578Sahrens buf->l_hdr.lh_block_type = BSWAP_64(buf->l_hdr.lh_block_type); 1101578Sahrens buf->l_hdr.lh_prefix = BSWAP_64(buf->l_hdr.lh_prefix); 1111578Sahrens buf->l_hdr.lh_magic = BSWAP_32(buf->l_hdr.lh_magic); 1121578Sahrens buf->l_hdr.lh_nfree = BSWAP_16(buf->l_hdr.lh_nfree); 1131578Sahrens buf->l_hdr.lh_nentries = BSWAP_16(buf->l_hdr.lh_nentries); 1141578Sahrens buf->l_hdr.lh_prefix_len = BSWAP_16(buf->l_hdr.lh_prefix_len); 115789Sahrens buf->l_hdr.lh_freelist = BSWAP_16(buf->l_hdr.lh_freelist); 116789Sahrens 1171491Sahrens for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(&l); i++) 118789Sahrens buf->l_hash[i] = BSWAP_16(buf->l_hash[i]); 119789Sahrens 1201491Sahrens for (i = 0; i < ZAP_LEAF_NUMCHUNKS(&l); i++) { 1211491Sahrens zap_leaf_chunk_t *lc = &ZAP_LEAF_CHUNK(&l, i); 122789Sahrens struct zap_leaf_entry *le; 123789Sahrens 1241491Sahrens switch (lc->l_free.lf_type) { 1251491Sahrens case ZAP_CHUNK_ENTRY: 1261491Sahrens le = &lc->l_entry; 127789Sahrens 1281578Sahrens le->le_type = BSWAP_8(le->le_type); 1291578Sahrens le->le_int_size = BSWAP_8(le->le_int_size); 1301578Sahrens le->le_next = BSWAP_16(le->le_next); 1311578Sahrens le->le_name_chunk = BSWAP_16(le->le_name_chunk); 1321578Sahrens le->le_name_length = BSWAP_16(le->le_name_length); 1331578Sahrens le->le_value_chunk = BSWAP_16(le->le_value_chunk); 1341578Sahrens le->le_value_length = BSWAP_16(le->le_value_length); 1351578Sahrens le->le_cd = BSWAP_32(le->le_cd); 1361578Sahrens le->le_hash = BSWAP_64(le->le_hash); 137789Sahrens break; 1381491Sahrens case ZAP_CHUNK_FREE: 1391578Sahrens lc->l_free.lf_type = BSWAP_8(lc->l_free.lf_type); 1401578Sahrens lc->l_free.lf_next = BSWAP_16(lc->l_free.lf_next); 141789Sahrens break; 1421491Sahrens case ZAP_CHUNK_ARRAY: 1431578Sahrens lc->l_array.la_type = BSWAP_8(lc->l_array.la_type); 1441578Sahrens lc->l_array.la_next = BSWAP_16(lc->l_array.la_next); 145789Sahrens /* la_array doesn't need swapping */ 146789Sahrens break; 147789Sahrens default: 148789Sahrens ASSERT(!"bad leaf type"); 149789Sahrens } 150789Sahrens } 151789Sahrens } 152789Sahrens 153789Sahrens void 1545498Stimh zap_leaf_init(zap_leaf_t *l, boolean_t sort) 155789Sahrens { 156789Sahrens int i; 157789Sahrens 1581491Sahrens l->l_bs = highbit(l->l_dbuf->db_size)-1; 159789Sahrens zap_memset(&l->l_phys->l_hdr, 0, sizeof (struct zap_leaf_header)); 1601491Sahrens zap_memset(l->l_phys->l_hash, CHAIN_END, 2*ZAP_LEAF_HASH_NUMENTRIES(l)); 1611491Sahrens for (i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) { 1621491Sahrens ZAP_LEAF_CHUNK(l, i).l_free.lf_type = ZAP_CHUNK_FREE; 1631491Sahrens ZAP_LEAF_CHUNK(l, i).l_free.lf_next = i+1; 164789Sahrens } 1651491Sahrens ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)-1).l_free.lf_next = CHAIN_END; 1661578Sahrens l->l_phys->l_hdr.lh_block_type = ZBT_LEAF; 1671578Sahrens l->l_phys->l_hdr.lh_magic = ZAP_LEAF_MAGIC; 1681578Sahrens l->l_phys->l_hdr.lh_nfree = ZAP_LEAF_NUMCHUNKS(l); 1695498Stimh if (sort) 1705331Samw l->l_phys->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED; 171789Sahrens } 172789Sahrens 173789Sahrens /* 174789Sahrens * Routines which manipulate leaf chunks (l_chunk[]). 175789Sahrens */ 176789Sahrens 177789Sahrens static uint16_t 178789Sahrens zap_leaf_chunk_alloc(zap_leaf_t *l) 179789Sahrens { 180789Sahrens int chunk; 181789Sahrens 1821578Sahrens ASSERT(l->l_phys->l_hdr.lh_nfree > 0); 183789Sahrens 184789Sahrens chunk = l->l_phys->l_hdr.lh_freelist; 1851491Sahrens ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l)); 1861491Sahrens ASSERT3U(ZAP_LEAF_CHUNK(l, chunk).l_free.lf_type, ==, ZAP_CHUNK_FREE); 187789Sahrens 1881491Sahrens l->l_phys->l_hdr.lh_freelist = ZAP_LEAF_CHUNK(l, chunk).l_free.lf_next; 189789Sahrens 1901578Sahrens l->l_phys->l_hdr.lh_nfree--; 191789Sahrens 192789Sahrens return (chunk); 193789Sahrens } 194789Sahrens 195789Sahrens static void 196789Sahrens zap_leaf_chunk_free(zap_leaf_t *l, uint16_t chunk) 197789Sahrens { 1981491Sahrens struct zap_leaf_free *zlf = &ZAP_LEAF_CHUNK(l, chunk).l_free; 1991578Sahrens ASSERT3U(l->l_phys->l_hdr.lh_nfree, <, ZAP_LEAF_NUMCHUNKS(l)); 2001491Sahrens ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l)); 2011491Sahrens ASSERT(zlf->lf_type != ZAP_CHUNK_FREE); 202789Sahrens 2031491Sahrens zlf->lf_type = ZAP_CHUNK_FREE; 204789Sahrens zlf->lf_next = l->l_phys->l_hdr.lh_freelist; 205789Sahrens bzero(zlf->lf_pad, sizeof (zlf->lf_pad)); /* help it to compress */ 206789Sahrens l->l_phys->l_hdr.lh_freelist = chunk; 207789Sahrens 2081578Sahrens l->l_phys->l_hdr.lh_nfree++; 209789Sahrens } 210789Sahrens 211789Sahrens /* 212789Sahrens * Routines which manipulate leaf arrays (zap_leaf_array type chunks). 213789Sahrens */ 214789Sahrens 215789Sahrens static uint16_t 2161578Sahrens zap_leaf_array_create(zap_leaf_t *l, const char *buf, 217789Sahrens int integer_size, int num_integers) 218789Sahrens { 219789Sahrens uint16_t chunk_head; 220789Sahrens uint16_t *chunkp = &chunk_head; 221789Sahrens int byten = 0; 222789Sahrens uint64_t value; 223789Sahrens int shift = (integer_size-1)*8; 224789Sahrens int len = num_integers; 225789Sahrens 226789Sahrens ASSERT3U(num_integers * integer_size, <, MAX_ARRAY_BYTES); 227789Sahrens 228789Sahrens while (len > 0) { 229789Sahrens uint16_t chunk = zap_leaf_chunk_alloc(l); 2301491Sahrens struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array; 231789Sahrens int i; 232789Sahrens 2331491Sahrens la->la_type = ZAP_CHUNK_ARRAY; 234789Sahrens for (i = 0; i < ZAP_LEAF_ARRAY_BYTES; i++) { 235789Sahrens if (byten == 0) 236789Sahrens value = ldv(integer_size, buf); 2373052Sahrens la->la_array[i] = value >> shift; 238789Sahrens value <<= 8; 239789Sahrens if (++byten == integer_size) { 240789Sahrens byten = 0; 241789Sahrens buf += integer_size; 242789Sahrens if (--len == 0) 243789Sahrens break; 244789Sahrens } 245789Sahrens } 246789Sahrens 247789Sahrens *chunkp = chunk; 248789Sahrens chunkp = &la->la_next; 249789Sahrens } 250789Sahrens *chunkp = CHAIN_END; 251789Sahrens 252789Sahrens return (chunk_head); 253789Sahrens } 254789Sahrens 255789Sahrens static void 2561578Sahrens zap_leaf_array_free(zap_leaf_t *l, uint16_t *chunkp) 257789Sahrens { 258789Sahrens uint16_t chunk = *chunkp; 259789Sahrens 260789Sahrens *chunkp = CHAIN_END; 261789Sahrens 262789Sahrens while (chunk != CHAIN_END) { 2631491Sahrens int nextchunk = ZAP_LEAF_CHUNK(l, chunk).l_array.la_next; 2641491Sahrens ASSERT3U(ZAP_LEAF_CHUNK(l, chunk).l_array.la_type, ==, 2651491Sahrens ZAP_CHUNK_ARRAY); 266789Sahrens zap_leaf_chunk_free(l, chunk); 267789Sahrens chunk = nextchunk; 268789Sahrens } 269789Sahrens } 270789Sahrens 271789Sahrens /* array_len and buf_len are in integers, not bytes */ 272789Sahrens static void 2731578Sahrens zap_leaf_array_read(zap_leaf_t *l, uint16_t chunk, 274789Sahrens int array_int_len, int array_len, int buf_int_len, uint64_t buf_len, 275789Sahrens char *buf) 276789Sahrens { 277789Sahrens int len = MIN(array_len, buf_len); 278789Sahrens int byten = 0; 279789Sahrens uint64_t value = 0; 280789Sahrens 281789Sahrens ASSERT3U(array_int_len, <=, buf_int_len); 282789Sahrens 283885Sahrens /* Fast path for one 8-byte integer */ 284885Sahrens if (array_int_len == 8 && buf_int_len == 8 && len == 1) { 2851491Sahrens struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array; 286899Sbonwick uint8_t *ip = la->la_array; 287885Sahrens uint64_t *buf64 = (uint64_t *)buf; 288899Sbonwick 289899Sbonwick *buf64 = (uint64_t)ip[0] << 56 | (uint64_t)ip[1] << 48 | 290899Sbonwick (uint64_t)ip[2] << 40 | (uint64_t)ip[3] << 32 | 291899Sbonwick (uint64_t)ip[4] << 24 | (uint64_t)ip[5] << 16 | 292899Sbonwick (uint64_t)ip[6] << 8 | (uint64_t)ip[7]; 293885Sahrens return; 294885Sahrens } 295885Sahrens 296885Sahrens /* Fast path for an array of 1-byte integers (eg. the entry name) */ 297885Sahrens if (array_int_len == 1 && buf_int_len == 1 && 298885Sahrens buf_len > array_len + ZAP_LEAF_ARRAY_BYTES) { 299885Sahrens while (chunk != CHAIN_END) { 300885Sahrens struct zap_leaf_array *la = 3011491Sahrens &ZAP_LEAF_CHUNK(l, chunk).l_array; 302885Sahrens bcopy(la->la_array, buf, ZAP_LEAF_ARRAY_BYTES); 303885Sahrens buf += ZAP_LEAF_ARRAY_BYTES; 304885Sahrens chunk = la->la_next; 305885Sahrens } 306885Sahrens return; 307885Sahrens } 308885Sahrens 309789Sahrens while (len > 0) { 3101491Sahrens struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array; 311789Sahrens int i; 312789Sahrens 3131491Sahrens ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l)); 314789Sahrens for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) { 315789Sahrens value = (value << 8) | la->la_array[i]; 316789Sahrens byten++; 317789Sahrens if (byten == array_int_len) { 318789Sahrens stv(buf_int_len, buf, value); 319789Sahrens byten = 0; 320789Sahrens len--; 321789Sahrens if (len == 0) 322789Sahrens return; 323789Sahrens buf += buf_int_len; 324789Sahrens } 325789Sahrens } 326789Sahrens chunk = la->la_next; 327789Sahrens } 328789Sahrens } 329789Sahrens 330789Sahrens /* 331789Sahrens * Only to be used on 8-bit arrays. 332789Sahrens * array_len is actual len in bytes (not encoded le_value_length). 3335331Samw * namenorm is null-terminated. 334789Sahrens */ 3355331Samw static boolean_t 3365331Samw zap_leaf_array_match(zap_leaf_t *l, zap_name_t *zn, int chunk, int array_len) 337789Sahrens { 338789Sahrens int bseen = 0; 339789Sahrens 3405331Samw if (zn->zn_matchtype == MT_FIRST) { 3415331Samw char *thisname = kmem_alloc(array_len, KM_SLEEP); 3425331Samw boolean_t match; 3435331Samw 3445331Samw zap_leaf_array_read(l, chunk, 1, array_len, 1, 3455331Samw array_len, thisname); 3465331Samw match = zap_match(zn, thisname); 3475331Samw kmem_free(thisname, array_len); 3485331Samw return (match); 3495331Samw } 3505331Samw 3515331Samw /* Fast path for exact matching */ 352789Sahrens while (bseen < array_len) { 3531491Sahrens struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(l, chunk).l_array; 354789Sahrens int toread = MIN(array_len - bseen, ZAP_LEAF_ARRAY_BYTES); 3551491Sahrens ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l)); 3565331Samw if (bcmp(la->la_array, zn->zn_name_orij + bseen, toread)) 357789Sahrens break; 358789Sahrens chunk = la->la_next; 359789Sahrens bseen += toread; 360789Sahrens } 361789Sahrens return (bseen == array_len); 362789Sahrens } 363789Sahrens 364789Sahrens /* 365789Sahrens * Routines which manipulate leaf entries. 366789Sahrens */ 367789Sahrens 368789Sahrens int 3695331Samw zap_leaf_lookup(zap_leaf_t *l, zap_name_t *zn, zap_entry_handle_t *zeh) 370789Sahrens { 371789Sahrens uint16_t *chunkp; 372789Sahrens struct zap_leaf_entry *le; 373789Sahrens 3741578Sahrens ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC); 375789Sahrens 3765331Samw again: 3775331Samw for (chunkp = LEAF_HASH_ENTPTR(l, zn->zn_hash); 378789Sahrens *chunkp != CHAIN_END; chunkp = &le->le_next) { 379789Sahrens uint16_t chunk = *chunkp; 3801491Sahrens le = ZAP_LEAF_ENTRY(l, chunk); 381789Sahrens 3821491Sahrens ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l)); 3831491Sahrens ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY); 384789Sahrens 3855331Samw if (le->le_hash != zn->zn_hash) 386789Sahrens continue; 387789Sahrens 3885331Samw /* 3895331Samw * NB: the entry chain is always sorted by cd on 3905331Samw * normalized zap objects, so this will find the 3915331Samw * lowest-cd match for MT_FIRST. 3925331Samw */ 3935331Samw ASSERT(zn->zn_matchtype == MT_EXACT || 3945331Samw (l->l_phys->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED)); 3955331Samw if (zap_leaf_array_match(l, zn, le->le_name_chunk, 3965331Samw le->le_name_length)) { 397789Sahrens zeh->zeh_num_integers = le->le_value_length; 398789Sahrens zeh->zeh_integer_size = le->le_int_size; 399789Sahrens zeh->zeh_cd = le->le_cd; 400789Sahrens zeh->zeh_hash = le->le_hash; 401789Sahrens zeh->zeh_chunkp = chunkp; 4021578Sahrens zeh->zeh_leaf = l; 403789Sahrens return (0); 404789Sahrens } 405789Sahrens } 406789Sahrens 4075331Samw /* 4085331Samw * NB: we could of course do this in one pass, but that would be 4095331Samw * a pain. We'll see if MT_BEST is even used much. 4105331Samw */ 4115331Samw if (zn->zn_matchtype == MT_BEST) { 4125331Samw zn->zn_matchtype = MT_FIRST; 4135331Samw goto again; 4145331Samw } 4155331Samw 416789Sahrens return (ENOENT); 417789Sahrens } 418789Sahrens 419789Sahrens /* Return (h1,cd1 >= h2,cd2) */ 420885Sahrens #define HCD_GTEQ(h1, cd1, h2, cd2) \ 421885Sahrens ((h1 > h2) ? TRUE : ((h1 == h2 && cd1 >= cd2) ? TRUE : FALSE)) 422789Sahrens 423789Sahrens int 424789Sahrens zap_leaf_lookup_closest(zap_leaf_t *l, 425789Sahrens uint64_t h, uint32_t cd, zap_entry_handle_t *zeh) 426789Sahrens { 427789Sahrens uint16_t chunk; 428789Sahrens uint64_t besth = -1ULL; 429789Sahrens uint32_t bestcd = ZAP_MAXCD; 4301491Sahrens uint16_t bestlh = ZAP_LEAF_HASH_NUMENTRIES(l)-1; 431789Sahrens uint16_t lh; 432789Sahrens struct zap_leaf_entry *le; 433789Sahrens 4341578Sahrens ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC); 435789Sahrens 436789Sahrens for (lh = LEAF_HASH(l, h); lh <= bestlh; lh++) { 437789Sahrens for (chunk = l->l_phys->l_hash[lh]; 438789Sahrens chunk != CHAIN_END; chunk = le->le_next) { 4391491Sahrens le = ZAP_LEAF_ENTRY(l, chunk); 440789Sahrens 4411491Sahrens ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l)); 4421491Sahrens ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY); 443789Sahrens 444885Sahrens if (HCD_GTEQ(le->le_hash, le->le_cd, h, cd) && 445885Sahrens HCD_GTEQ(besth, bestcd, le->le_hash, le->le_cd)) { 446789Sahrens ASSERT3U(bestlh, >=, lh); 447789Sahrens bestlh = lh; 448789Sahrens besth = le->le_hash; 449789Sahrens bestcd = le->le_cd; 450789Sahrens 451789Sahrens zeh->zeh_num_integers = le->le_value_length; 452789Sahrens zeh->zeh_integer_size = le->le_int_size; 453789Sahrens zeh->zeh_cd = le->le_cd; 454789Sahrens zeh->zeh_hash = le->le_hash; 455789Sahrens zeh->zeh_fakechunk = chunk; 456789Sahrens zeh->zeh_chunkp = &zeh->zeh_fakechunk; 4571578Sahrens zeh->zeh_leaf = l; 458789Sahrens } 459789Sahrens } 460789Sahrens } 461789Sahrens 462789Sahrens return (bestcd == ZAP_MAXCD ? ENOENT : 0); 463789Sahrens } 464789Sahrens 465789Sahrens int 466789Sahrens zap_entry_read(const zap_entry_handle_t *zeh, 467789Sahrens uint8_t integer_size, uint64_t num_integers, void *buf) 468789Sahrens { 4691491Sahrens struct zap_leaf_entry *le = 4701578Sahrens ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp); 4711491Sahrens ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY); 472789Sahrens 473789Sahrens if (le->le_int_size > integer_size) 474789Sahrens return (EINVAL); 475789Sahrens 4761578Sahrens zap_leaf_array_read(zeh->zeh_leaf, le->le_value_chunk, le->le_int_size, 477789Sahrens le->le_value_length, integer_size, num_integers, buf); 478789Sahrens 479789Sahrens if (zeh->zeh_num_integers > num_integers) 480789Sahrens return (EOVERFLOW); 481789Sahrens return (0); 482789Sahrens 483789Sahrens } 484789Sahrens 485789Sahrens int 486789Sahrens zap_entry_read_name(const zap_entry_handle_t *zeh, uint16_t buflen, char *buf) 487789Sahrens { 4881491Sahrens struct zap_leaf_entry *le = 4891578Sahrens ZAP_LEAF_ENTRY(zeh->zeh_leaf, *zeh->zeh_chunkp); 4901491Sahrens ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY); 491789Sahrens 4921578Sahrens zap_leaf_array_read(zeh->zeh_leaf, le->le_name_chunk, 1, 493789Sahrens le->le_name_length, 1, buflen, buf); 494789Sahrens if (le->le_name_length > buflen) 495789Sahrens return (EOVERFLOW); 496789Sahrens return (0); 497789Sahrens } 498789Sahrens 499789Sahrens int 500789Sahrens zap_entry_update(zap_entry_handle_t *zeh, 501789Sahrens uint8_t integer_size, uint64_t num_integers, const void *buf) 502789Sahrens { 503789Sahrens int delta_chunks; 5041578Sahrens zap_leaf_t *l = zeh->zeh_leaf; 5051578Sahrens struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, *zeh->zeh_chunkp); 506789Sahrens 5071578Sahrens delta_chunks = ZAP_LEAF_ARRAY_NCHUNKS(num_integers * integer_size) - 5081578Sahrens ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_length * le->le_int_size); 509789Sahrens 5101578Sahrens if ((int)l->l_phys->l_hdr.lh_nfree < delta_chunks) 511789Sahrens return (EAGAIN); 512789Sahrens 513789Sahrens /* 514789Sahrens * We should search other chained leaves (via 515789Sahrens * zap_entry_remove,create?) otherwise returning EAGAIN will 516789Sahrens * just send us into an infinite loop if we have to chain 517789Sahrens * another leaf block, rather than being able to split this 518789Sahrens * block. 519789Sahrens */ 520789Sahrens 5211578Sahrens zap_leaf_array_free(l, &le->le_value_chunk); 522789Sahrens le->le_value_chunk = 5231578Sahrens zap_leaf_array_create(l, buf, integer_size, num_integers); 5241491Sahrens le->le_value_length = num_integers; 525789Sahrens le->le_int_size = integer_size; 526789Sahrens return (0); 527789Sahrens } 528789Sahrens 529789Sahrens void 530789Sahrens zap_entry_remove(zap_entry_handle_t *zeh) 531789Sahrens { 532789Sahrens uint16_t entry_chunk; 533789Sahrens struct zap_leaf_entry *le; 5341578Sahrens zap_leaf_t *l = zeh->zeh_leaf; 535789Sahrens 536789Sahrens ASSERT3P(zeh->zeh_chunkp, !=, &zeh->zeh_fakechunk); 537789Sahrens 538789Sahrens entry_chunk = *zeh->zeh_chunkp; 5391491Sahrens le = ZAP_LEAF_ENTRY(l, entry_chunk); 5401491Sahrens ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY); 541789Sahrens 5421578Sahrens zap_leaf_array_free(l, &le->le_name_chunk); 5431578Sahrens zap_leaf_array_free(l, &le->le_value_chunk); 544789Sahrens 545789Sahrens *zeh->zeh_chunkp = le->le_next; 546789Sahrens zap_leaf_chunk_free(l, entry_chunk); 547789Sahrens 5481578Sahrens l->l_phys->l_hdr.lh_nentries--; 549789Sahrens } 550789Sahrens 551789Sahrens int 552789Sahrens zap_entry_create(zap_leaf_t *l, const char *name, uint64_t h, uint32_t cd, 553789Sahrens uint8_t integer_size, uint64_t num_integers, const void *buf, 554789Sahrens zap_entry_handle_t *zeh) 555789Sahrens { 556789Sahrens uint16_t chunk; 557789Sahrens uint16_t *chunkp; 558789Sahrens struct zap_leaf_entry *le; 559789Sahrens uint64_t namelen, valuelen; 560789Sahrens int numchunks; 561789Sahrens 562789Sahrens valuelen = integer_size * num_integers; 563789Sahrens namelen = strlen(name) + 1; 564789Sahrens ASSERT(namelen >= 2); 565789Sahrens 5661578Sahrens numchunks = 1 + ZAP_LEAF_ARRAY_NCHUNKS(namelen) + 5671578Sahrens ZAP_LEAF_ARRAY_NCHUNKS(valuelen); 5681491Sahrens if (numchunks > ZAP_LEAF_NUMCHUNKS(l)) 569789Sahrens return (E2BIG); 570789Sahrens 571789Sahrens if (cd == ZAP_MAXCD) { 5725331Samw /* find the lowest unused cd */ 5735331Samw if (l->l_phys->l_hdr.lh_flags & ZLF_ENTRIES_CDSORTED) { 5745331Samw cd = 0; 5755331Samw 5761578Sahrens for (chunk = *LEAF_HASH_ENTPTR(l, h); 5771578Sahrens chunk != CHAIN_END; chunk = le->le_next) { 5781578Sahrens le = ZAP_LEAF_ENTRY(l, chunk); 5795331Samw if (le->le_cd > cd) 5801578Sahrens break; 5815331Samw if (le->le_hash == h) { 5825331Samw ASSERT3U(cd, ==, le->le_cd); 5835331Samw cd++; 584789Sahrens } 585789Sahrens } 5865331Samw } else { 5875331Samw /* old unsorted format; do it the O(n^2) way */ 5885331Samw for (cd = 0; cd < ZAP_MAXCD; cd++) { 5895331Samw for (chunk = *LEAF_HASH_ENTPTR(l, h); 5905331Samw chunk != CHAIN_END; chunk = le->le_next) { 5915331Samw le = ZAP_LEAF_ENTRY(l, chunk); 5925331Samw if (le->le_hash == h && 5935331Samw le->le_cd == cd) { 5945331Samw break; 5955331Samw } 5965331Samw } 5975331Samw /* If this cd is not in use, we are good. */ 5985331Samw if (chunk == CHAIN_END) 5995331Samw break; 6005331Samw } 601789Sahrens } 6025331Samw /* 6035331Samw * we would run out of space in a block before we could 6045331Samw * have ZAP_MAXCD entries 6055331Samw */ 6065331Samw ASSERT3U(cd, <, ZAP_MAXCD); 607789Sahrens } 608789Sahrens 6091578Sahrens if (l->l_phys->l_hdr.lh_nfree < numchunks) 610789Sahrens return (EAGAIN); 611789Sahrens 612789Sahrens /* make the entry */ 613789Sahrens chunk = zap_leaf_chunk_alloc(l); 6141491Sahrens le = ZAP_LEAF_ENTRY(l, chunk); 6151491Sahrens le->le_type = ZAP_CHUNK_ENTRY; 6161578Sahrens le->le_name_chunk = zap_leaf_array_create(l, name, 1, namelen); 617789Sahrens le->le_name_length = namelen; 618789Sahrens le->le_value_chunk = 6191578Sahrens zap_leaf_array_create(l, buf, integer_size, num_integers); 6201491Sahrens le->le_value_length = num_integers; 621789Sahrens le->le_int_size = integer_size; 622789Sahrens le->le_hash = h; 623789Sahrens le->le_cd = cd; 624789Sahrens 625789Sahrens /* link it into the hash chain */ 6265331Samw /* XXX if we did the search above, we could just use that */ 6275331Samw chunkp = zap_leaf_rehash_entry(l, chunk); 628789Sahrens 6291578Sahrens l->l_phys->l_hdr.lh_nentries++; 630789Sahrens 6311578Sahrens zeh->zeh_leaf = l; 632789Sahrens zeh->zeh_num_integers = num_integers; 633789Sahrens zeh->zeh_integer_size = le->le_int_size; 634789Sahrens zeh->zeh_cd = le->le_cd; 635789Sahrens zeh->zeh_hash = le->le_hash; 636789Sahrens zeh->zeh_chunkp = chunkp; 637789Sahrens 638789Sahrens return (0); 639789Sahrens } 640789Sahrens 641789Sahrens /* 6425331Samw * Determine if there is another entry with the same normalized form. 6435331Samw * For performance purposes, either zn or name must be provided (the 6445331Samw * other can be NULL). Note, there usually won't be any hash 6455331Samw * conflicts, in which case we don't need the concatenated/normalized 6465331Samw * form of the name. But all callers have one of these on hand anyway, 6475331Samw * so might as well take advantage. A cleaner but slower interface 6485331Samw * would accept neither argument, and compute the normalized name as 6495331Samw * needed (using zap_name_alloc(zap_entry_read_name(zeh))). 6505331Samw */ 6515331Samw boolean_t 6525331Samw zap_entry_normalization_conflict(zap_entry_handle_t *zeh, zap_name_t *zn, 6535331Samw const char *name, zap_t *zap) 6545331Samw { 6555331Samw uint64_t chunk; 6565331Samw struct zap_leaf_entry *le; 6575331Samw boolean_t allocdzn = B_FALSE; 6585331Samw 6595331Samw if (zap->zap_normflags == 0) 6605331Samw return (B_FALSE); 6615331Samw 6625331Samw for (chunk = *LEAF_HASH_ENTPTR(zeh->zeh_leaf, zeh->zeh_hash); 6635331Samw chunk != CHAIN_END; chunk = le->le_next) { 6645331Samw le = ZAP_LEAF_ENTRY(zeh->zeh_leaf, chunk); 6655331Samw if (le->le_hash != zeh->zeh_hash) 6665331Samw continue; 6675331Samw if (le->le_cd == zeh->zeh_cd) 6685331Samw continue; 6695331Samw 6705331Samw if (zn == NULL) { 6715331Samw zn = zap_name_alloc(zap, name, MT_FIRST); 6725331Samw allocdzn = B_TRUE; 6735331Samw } 6745331Samw if (zap_leaf_array_match(zeh->zeh_leaf, zn, 6755331Samw le->le_name_chunk, le->le_name_length)) { 6765331Samw if (allocdzn) 6775331Samw zap_name_free(zn); 6785331Samw return (B_TRUE); 6795331Samw } 6805331Samw } 6815331Samw if (allocdzn) 6825331Samw zap_name_free(zn); 6835331Samw return (B_FALSE); 6845331Samw } 6855331Samw 6865331Samw /* 687789Sahrens * Routines for transferring entries between leafs. 688789Sahrens */ 689789Sahrens 6905331Samw static uint16_t * 691789Sahrens zap_leaf_rehash_entry(zap_leaf_t *l, uint16_t entry) 692789Sahrens { 6931491Sahrens struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, entry); 6945331Samw struct zap_leaf_entry *le2; 6955331Samw uint16_t *chunkp; 6965331Samw 6975331Samw /* 6985331Samw * keep the entry chain sorted by cd 6995331Samw * NB: this will not cause problems for unsorted leafs, though 7005331Samw * it is unnecessary there. 7015331Samw */ 7025331Samw for (chunkp = LEAF_HASH_ENTPTR(l, le->le_hash); 7035331Samw *chunkp != CHAIN_END; chunkp = &le2->le_next) { 7045331Samw le2 = ZAP_LEAF_ENTRY(l, *chunkp); 7055331Samw if (le2->le_cd > le->le_cd) 7065331Samw break; 7075331Samw } 7085331Samw 7095331Samw le->le_next = *chunkp; 7105331Samw *chunkp = entry; 7115331Samw return (chunkp); 712789Sahrens } 713789Sahrens 714789Sahrens static uint16_t 715789Sahrens zap_leaf_transfer_array(zap_leaf_t *l, uint16_t chunk, zap_leaf_t *nl) 716789Sahrens { 717789Sahrens uint16_t new_chunk; 718789Sahrens uint16_t *nchunkp = &new_chunk; 719789Sahrens 720789Sahrens while (chunk != CHAIN_END) { 721789Sahrens uint16_t nchunk = zap_leaf_chunk_alloc(nl); 722789Sahrens struct zap_leaf_array *nla = 7231491Sahrens &ZAP_LEAF_CHUNK(nl, nchunk).l_array; 724789Sahrens struct zap_leaf_array *la = 7251491Sahrens &ZAP_LEAF_CHUNK(l, chunk).l_array; 726789Sahrens int nextchunk = la->la_next; 727789Sahrens 7281491Sahrens ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(l)); 7291491Sahrens ASSERT3U(nchunk, <, ZAP_LEAF_NUMCHUNKS(l)); 730789Sahrens 7311578Sahrens *nla = *la; /* structure assignment */ 732789Sahrens 733789Sahrens zap_leaf_chunk_free(l, chunk); 734789Sahrens chunk = nextchunk; 735789Sahrens *nchunkp = nchunk; 736789Sahrens nchunkp = &nla->la_next; 737789Sahrens } 738789Sahrens *nchunkp = CHAIN_END; 739789Sahrens return (new_chunk); 740789Sahrens } 741789Sahrens 742789Sahrens static void 7431578Sahrens zap_leaf_transfer_entry(zap_leaf_t *l, int entry, zap_leaf_t *nl) 744789Sahrens { 745789Sahrens struct zap_leaf_entry *le, *nle; 7461578Sahrens uint16_t chunk; 747789Sahrens 7481491Sahrens le = ZAP_LEAF_ENTRY(l, entry); 7491491Sahrens ASSERT3U(le->le_type, ==, ZAP_CHUNK_ENTRY); 750789Sahrens 751789Sahrens chunk = zap_leaf_chunk_alloc(nl); 7521491Sahrens nle = ZAP_LEAF_ENTRY(nl, chunk); 7531578Sahrens *nle = *le; /* structure assignment */ 754789Sahrens 7555331Samw (void) zap_leaf_rehash_entry(nl, chunk); 756789Sahrens 757789Sahrens nle->le_name_chunk = zap_leaf_transfer_array(l, le->le_name_chunk, nl); 758789Sahrens nle->le_value_chunk = 759789Sahrens zap_leaf_transfer_array(l, le->le_value_chunk, nl); 760789Sahrens 761789Sahrens zap_leaf_chunk_free(l, entry); 762789Sahrens 7631578Sahrens l->l_phys->l_hdr.lh_nentries--; 7641578Sahrens nl->l_phys->l_hdr.lh_nentries++; 765789Sahrens } 766789Sahrens 767789Sahrens /* 7681578Sahrens * Transfer the entries whose hash prefix ends in 1 to the new leaf. 769789Sahrens */ 7701578Sahrens void 7715498Stimh zap_leaf_split(zap_leaf_t *l, zap_leaf_t *nl, boolean_t sort) 772789Sahrens { 773789Sahrens int i; 7741578Sahrens int bit = 64 - 1 - l->l_phys->l_hdr.lh_prefix_len; 775789Sahrens 7761578Sahrens /* set new prefix and prefix_len */ 7771578Sahrens l->l_phys->l_hdr.lh_prefix <<= 1; 7781578Sahrens l->l_phys->l_hdr.lh_prefix_len++; 7791578Sahrens nl->l_phys->l_hdr.lh_prefix = l->l_phys->l_hdr.lh_prefix | 1; 7801578Sahrens nl->l_phys->l_hdr.lh_prefix_len = l->l_phys->l_hdr.lh_prefix_len; 7811578Sahrens 782789Sahrens /* break existing hash chains */ 7831491Sahrens zap_memset(l->l_phys->l_hash, CHAIN_END, 2*ZAP_LEAF_HASH_NUMENTRIES(l)); 784789Sahrens 7855498Stimh if (sort) 7865331Samw l->l_phys->l_hdr.lh_flags |= ZLF_ENTRIES_CDSORTED; 7875331Samw 7881578Sahrens /* 7891578Sahrens * Transfer entries whose hash bit 'bit' is set to nl; rehash 7901578Sahrens * the remaining entries 7911578Sahrens * 7921578Sahrens * NB: We could find entries via the hashtable instead. That 7931578Sahrens * would be O(hashents+numents) rather than O(numblks+numents), 7941578Sahrens * but this accesses memory more sequentially, and when we're 7951578Sahrens * called, the block is usually pretty full. 7961578Sahrens */ 7971491Sahrens for (i = 0; i < ZAP_LEAF_NUMCHUNKS(l); i++) { 7981491Sahrens struct zap_leaf_entry *le = ZAP_LEAF_ENTRY(l, i); 7991491Sahrens if (le->le_type != ZAP_CHUNK_ENTRY) 800789Sahrens continue; 801789Sahrens 8021578Sahrens if (le->le_hash & (1ULL << bit)) 8031578Sahrens zap_leaf_transfer_entry(l, i, nl); 8041578Sahrens else 8055331Samw (void) zap_leaf_rehash_entry(l, i); 806789Sahrens } 807789Sahrens } 808789Sahrens 809789Sahrens void 8101578Sahrens zap_leaf_stats(zap_t *zap, zap_leaf_t *l, zap_stats_t *zs) 811789Sahrens { 8121578Sahrens int i, n; 813789Sahrens 8141578Sahrens n = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift - 8151578Sahrens l->l_phys->l_hdr.lh_prefix_len; 816789Sahrens n = MIN(n, ZAP_HISTOGRAM_SIZE-1); 817789Sahrens zs->zs_leafs_with_2n_pointers[n]++; 818789Sahrens 819789Sahrens 8201578Sahrens n = l->l_phys->l_hdr.lh_nentries/5; 8211578Sahrens n = MIN(n, ZAP_HISTOGRAM_SIZE-1); 8221578Sahrens zs->zs_blocks_with_n5_entries[n]++; 823789Sahrens 8241578Sahrens n = ((1<<FZAP_BLOCK_SHIFT(zap)) - 8251578Sahrens l->l_phys->l_hdr.lh_nfree * (ZAP_LEAF_ARRAY_BYTES+1))*10 / 8261578Sahrens (1<<FZAP_BLOCK_SHIFT(zap)); 8271578Sahrens n = MIN(n, ZAP_HISTOGRAM_SIZE-1); 8281578Sahrens zs->zs_blocks_n_tenths_full[n]++; 829789Sahrens 8301578Sahrens for (i = 0; i < ZAP_LEAF_HASH_NUMENTRIES(l); i++) { 8311578Sahrens int nentries = 0; 8321578Sahrens int chunk = l->l_phys->l_hash[i]; 8331578Sahrens 8341578Sahrens while (chunk != CHAIN_END) { 8351578Sahrens struct zap_leaf_entry *le = 8361578Sahrens ZAP_LEAF_ENTRY(l, chunk); 837789Sahrens 8381578Sahrens n = 1 + ZAP_LEAF_ARRAY_NCHUNKS(le->le_name_length) + 8391578Sahrens ZAP_LEAF_ARRAY_NCHUNKS(le->le_value_length * 8405498Stimh le->le_int_size); 8411578Sahrens n = MIN(n, ZAP_HISTOGRAM_SIZE-1); 8421578Sahrens zs->zs_entries_using_n_chunks[n]++; 843789Sahrens 8441578Sahrens chunk = le->le_next; 8451578Sahrens nentries++; 846789Sahrens } 847789Sahrens 8481578Sahrens n = nentries; 8491578Sahrens n = MIN(n, ZAP_HISTOGRAM_SIZE-1); 8501578Sahrens zs->zs_buckets_with_n_entries[n]++; 8511578Sahrens } 852789Sahrens } 853