- /* num_zones might be out of date, but can only increase */
- return quick_random(tdb) % tdb->header.v.num_zones;
+ /*
+ * We read num_zones without a proper lock, so we could have
+ * gotten a partial read. Since zone_bits is 1 byte long, we
+ * can trust that; even if it's increased, the number of zones
+ * cannot have decreased. And using the map size means we
+ * will not start with a zone which hasn't been filled yet.
+ */
+ tdb->last_zone = quick_random(tdb)
+ % ((tdb->map_size >> tdb->header.v.zone_bits) + 1);