-
-static bool larger_buckets_might_help(struct tdb_context *tdb)
-{
- /* If our buckets are already covering 1/8 of a zone, don't
- * bother (note: might become an 1/16 of a zone if we double
- * zone size). */
- tdb_len_t size = (1ULL << tdb->header.v.zone_bits) / 8;
-
- if (size >= MIN_DATA_LEN
- && size_to_bucket(tdb, size) < tdb->header.v.free_buckets) {
- return false;
- }
-
- /* FIXME: Put stats in tdb_context or examine db itself! */
- /* It's fairly cheap to do as we expand database. */
- return true;
-}
-
-static bool zones_happy(struct tdb_context *tdb)
-{
- /* FIXME: look at distribution of zones. */
- return true;
-}
-
-/* Expand the database. */
-int tdb_expand(struct tdb_context *tdb, tdb_len_t klen, tdb_len_t dlen,
- bool growing)
-{
- uint64_t new_num_buckets, new_num_zones, new_zone_bits;
- uint64_t old_num_total, i;
- tdb_len_t add, freebucket_size, needed;
- tdb_off_t off, old_free_off;
- const tdb_off_t *oldf;
- struct tdb_used_record fhdr;
-
- /* We need room for the record header too. */
- needed = sizeof(struct tdb_used_record)
- + adjust_size(klen, dlen, growing);
-
- /* FIXME: this is overkill. An expand lock? */
- if (tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false) == -1)
- return -1;
-
- /* Someone may have expanded for us. */
- if (update_header(tdb))
- goto success;
-
- /* Make sure we have the latest size. */
- tdb->methods->oob(tdb, tdb->map_size + 1, true);
-
- /* Did we enlarge zones without enlarging file? */
- if (tdb->map_size < tdb->header.v.num_zones<<tdb->header.v.zone_bits) {
- add = (tdb->header.v.num_zones<<tdb->header.v.zone_bits)
- - tdb->map_size;
- /* Updates tdb->map_size. */
- if (tdb->methods->expand_file(tdb, tdb->map_size, add) == -1)
- goto fail;
- if (add_free_record(tdb, tdb->map_size - add, add) == -1)
- goto fail;
- if (add >= needed) {
- /* Allocate from this zone. */
- tdb->last_zone = zone_of(tdb, tdb->map_size - add);
- goto success;
- }
- }
-
- /* Slow path. Should we increase the number of buckets? */
- new_num_buckets = tdb->header.v.free_buckets;
- if (larger_buckets_might_help(tdb))
- new_num_buckets++;
-
- /* Now we'll need room for the new free buckets, too. Assume
- * worst case (zones expand). */
- needed += sizeof(fhdr)
- + ((tdb->header.v.num_zones+1)
- * (new_num_buckets+1) * sizeof(tdb_off_t));
-
- /* If we need less that one zone, and they're working well, just add
- * another one. */
- if (needed < (1UL<<tdb->header.v.zone_bits) && zones_happy(tdb)) {
- new_num_zones = tdb->header.v.num_zones+1;
- new_zone_bits = tdb->header.v.zone_bits;
- add = 1ULL << tdb->header.v.zone_bits;
- } else {
- /* Increase the zone size. */
- new_num_zones = tdb->header.v.num_zones;
- new_zone_bits = tdb->header.v.zone_bits+1;
- while ((new_num_zones << new_zone_bits) - tdb->map_size
- < needed) {
- new_zone_bits++;
- }
-
- /* We expand by enough zones to meet the need. */
- add = (needed + (1ULL << new_zone_bits)-1)
- & ~((1ULL << new_zone_bits)-1);
- }
-
- /* Updates tdb->map_size. */
- if (tdb->methods->expand_file(tdb, tdb->map_size, add) == -1)
- goto fail;
-
- /* Use first part as new free bucket array. */
- off = tdb->map_size - add;
- freebucket_size = new_num_zones
- * (new_num_buckets + 1) * sizeof(tdb_off_t);
-
- /* Write header. */
- if (set_header(tdb, &fhdr, 0, freebucket_size, freebucket_size, 0))
- goto fail;
- if (tdb_write_convert(tdb, off, &fhdr, sizeof(fhdr)) == -1)
- goto fail;
-
- /* Adjust off to point to start of buckets, add to be remainder. */
- add -= freebucket_size + sizeof(fhdr);
- off += sizeof(fhdr);
-
- /* Access the old zones. */
- old_num_total = tdb->header.v.num_zones*(tdb->header.v.free_buckets+1);
- old_free_off = tdb->header.v.free_off;
- oldf = tdb_access_read(tdb, old_free_off,
- old_num_total * sizeof(tdb_off_t));
- if (!oldf)
- goto fail;
-
- /* Switch to using our new zone. */
- if (zero_out(tdb, off, new_num_zones * (new_num_buckets + 1)) == -1)
- goto fail_release;
- tdb->header.v.free_off = off;
- tdb->header.v.num_zones = new_num_zones;
- tdb->header.v.free_buckets = new_num_buckets;
-
- /* FIXME: If zone size hasn't changed, can simply copy pointers. */
- /* FIXME: Coalesce? */
- for (i = 0; i < old_num_total; i++) {
- tdb_off_t next;
- struct tdb_free_record rec;
- tdb_off_t list;
-
- for (off = oldf[i]; off; off = next) {
- if (tdb_read_convert(tdb, off, &rec, sizeof(rec)))
- goto fail_release;
-
- list = zone_of(tdb, off)
- * (tdb->header.v.free_buckets+1)
- + size_to_bucket(tdb, rec.data_len);
- next = rec.next;
-
- if (enqueue_in_free(tdb, list, off, &rec) == -1)
- goto fail_release;
- }
- }
-
-
- /* Free up the old free buckets. */
- old_free_off -= sizeof(fhdr);
- if (tdb_read_convert(tdb, old_free_off, &fhdr, sizeof(fhdr)) == -1)
- goto fail_release;
- if (add_free_record(tdb, old_free_off,
- rec_data_length(&fhdr)+rec_extra_padding(&fhdr)))
- goto fail_release;
-
- /* Add the rest as a new free record. */
- if (add_free_record(tdb, tdb->map_size - add, add) == -1)
- goto fail_release;
-
- /* Start allocating from where the new space is. */
- tdb->last_zone = zone_of(tdb, tdb->map_size - add);
- tdb_access_release(tdb, oldf);
-success:
- tdb_allrecord_unlock(tdb, F_WRLCK);
- return 0;
-
-fail_release:
- tdb_access_release(tdb, oldf);
-fail:
- tdb_allrecord_unlock(tdb, F_WRLCK);
- return -1;
-}