29 #include <sys/zfs_context.h>
30 #include <sys/spa.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dnode.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/zio.h>
36 #include <sys/space_map.h>
37 #include <sys/refcount.h>
38 #include <sys/zfeature.h>
39
40 /*
41 * The data for a given space map can be kept on blocks of any size.
42 * Larger blocks entail fewer i/o operations, but they also cause the
43 * DMU to keep more data in-core, and also to waste more i/o bandwidth
44 * when only a few blocks have changed since the last transaction group.
45 */
46 int space_map_blksz = (1 << 12);
47
48 /*
49 * Iterate through the space map, invoking the callback on each (non-debug)
50 * space map entry.
51 */
52 int
53 space_map_iterate(space_map_t *sm, sm_cb_t callback, void *arg)
54 {
55 uint64_t *entry, *entry_map, *entry_map_end;
56 uint64_t bufsize, size, offset, end;
57 int error = 0;
58
59 end = space_map_length(sm);
60
61 bufsize = MAX(sm->sm_blksz, SPA_MINBLOCKSIZE);
62 entry_map = zio_buf_alloc(bufsize);
63
64 if (end > bufsize) {
65 dmu_prefetch(sm->sm_os, space_map_object(sm), 0, bufsize,
66 end - bufsize, ZIO_PRIORITY_SYNC_READ);
67 }
68
69 for (offset = 0; offset < end && error == 0; offset += bufsize) {
70 size = MIN(end - offset, bufsize);
71 VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
72 VERIFY(size != 0);
73 ASSERT3U(sm->sm_blksz, !=, 0);
74
75 dprintf("object=%llu offset=%llx size=%llx\n",
76 space_map_object(sm), offset, size);
77
78 error = dmu_read(sm->sm_os, space_map_object(sm), offset, size,
79 entry_map, DMU_READ_PREFETCH);
80 if (error != 0)
81 break;
82
83 entry_map_end = entry_map + (size / sizeof (uint64_t));
84 for (entry = entry_map; entry < entry_map_end && error == 0;
85 entry++) {
86 uint64_t e = *entry;
87 uint64_t offset, size;
88
89 if (SM_DEBUG_DECODE(e)) /* Skip debug entries */
90 continue;
91
92 offset = (SM_OFFSET_DECODE(e) << sm->sm_shift) +
93 sm->sm_start;
94 size = SM_RUN_DECODE(e) << sm->sm_shift;
95
96 VERIFY0(P2PHASE(offset, 1ULL << sm->sm_shift));
97 VERIFY0(P2PHASE(size, 1ULL << sm->sm_shift));
98 VERIFY3U(offset, >=, sm->sm_start);
99 VERIFY3U(offset + size, <=, sm->sm_start + sm->sm_size);
100 error = callback(SM_TYPE_DECODE(e), offset, size, arg);
101 }
102 }
103
104 zio_buf_free(entry_map, bufsize);
105 return (error);
106 }
107
108 typedef struct space_map_load_arg {
109 space_map_t *smla_sm;
110 range_tree_t *smla_rt;
111 maptype_t smla_type;
112 } space_map_load_arg_t;
113
114 static int
115 space_map_load_callback(maptype_t type, uint64_t offset, uint64_t size,
116 void *arg)
117 {
118 space_map_load_arg_t *smla = arg;
119 if (type == smla->smla_type) {
120 VERIFY3U(range_tree_space(smla->smla_rt) + size, <=,
121 smla->smla_sm->sm_size);
122 range_tree_add(smla->smla_rt, offset, size);
123 } else {
124 range_tree_remove(smla->smla_rt, offset, size);
125 }
126
127 return (0);
128 }
129
130 /*
131 * Load the space map disk into the specified range tree. Segments of maptype
132 * are added to the range tree, other segment types are removed.
133 */
134 int
135 space_map_load(space_map_t *sm, range_tree_t *rt, maptype_t maptype)
136 {
137 uint64_t space;
138 int err;
139 space_map_load_arg_t smla;
140
141 VERIFY0(range_tree_space(rt));
142 space = space_map_allocated(sm);
143
144 if (maptype == SM_FREE) {
145 range_tree_add(rt, sm->sm_start, sm->sm_size);
146 space = sm->sm_size - space;
147 }
148
149 smla.smla_rt = rt;
150 smla.smla_sm = sm;
151 smla.smla_type = maptype;
152 err = space_map_iterate(sm, space_map_load_callback, &smla);
153
154 if (err == 0) {
155 VERIFY3U(range_tree_space(rt), ==, space);
156 } else {
157 range_tree_vacate(rt, NULL, NULL);
158 }
159
160 return (err);
161 }
162
163 void
164 space_map_histogram_clear(space_map_t *sm)
165 {
166 if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t))
167 return;
168
169 bzero(sm->sm_phys->smp_histogram, sizeof (sm->sm_phys->smp_histogram));
170 }
171
172 boolean_t
173 space_map_histogram_verify(space_map_t *sm, range_tree_t *rt)
174 {
175 /*
176 * Verify that the in-core range tree does not have any
177 * ranges smaller than our sm_shift size.
178 */
179 for (int i = 0; i < sm->sm_shift; i++) {
180 if (rt->rt_histogram[i] != 0)
181 return (B_FALSE);
182 }
183 return (B_TRUE);
184 }
185
186 void
187 space_map_histogram_add(space_map_t *sm, range_tree_t *rt, dmu_tx_t *tx)
188 {
189 int idx = 0;
190
191 ASSERT(dmu_tx_is_syncing(tx));
192 VERIFY3U(space_map_object(sm), !=, 0);
193
194 if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t))
195 return;
196
197 dmu_buf_will_dirty(sm->sm_dbuf, tx);
198
199 ASSERT(space_map_histogram_verify(sm, rt));
200 /*
201 * Transfer the content of the range tree histogram to the space
202 * map histogram. The space map histogram contains 32 buckets ranging
203 * between 2^sm_shift to 2^(32+sm_shift-1). The range tree,
204 * however, can represent ranges from 2^0 to 2^63. Since the space
205 * map only cares about allocatable blocks (minimum of sm_shift) we
206 * can safely ignore all ranges in the range tree smaller than sm_shift.
207 */
208 for (int i = sm->sm_shift; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
209
210 /*
239 avl_tree_t *t = &rt->rt_root;
240 range_seg_t *rs;
241 uint64_t size, entries;
242
243 /*
244 * All space_maps always have a debug entry so account for it here.
245 */
246 entries = 1;
247
248 /*
249 * Traverse the range tree and calculate the number of space map
250 * entries that would be required to write out the range tree.
251 */
252 for (rs = avl_first(t); rs != NULL; rs = AVL_NEXT(t, rs)) {
253 size = (rs->rs_end - rs->rs_start) >> sm->sm_shift;
254 entries += howmany(size, SM_RUN_MAX);
255 }
256 return (entries);
257 }
258
259 void
260 space_map_write(space_map_t *sm, range_tree_t *rt, maptype_t maptype,
261 dmu_tx_t *tx)
262 {
263 objset_t *os = sm->sm_os;
264 spa_t *spa = dmu_objset_spa(os);
265 avl_tree_t *t = &rt->rt_root;
266 range_seg_t *rs;
267 uint64_t size, total, rt_space, nodes;
268 uint64_t *entry, *entry_map, *entry_map_end;
269 uint64_t expected_entries, actual_entries = 1;
270
271 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
272 VERIFY3U(space_map_object(sm), !=, 0);
273 dmu_buf_will_dirty(sm->sm_dbuf, tx);
274
275 /*
276 * This field is no longer necessary since the in-core space map
277 * now contains the object number but is maintained for backwards
278 * compatibility.
279 */
280 sm->sm_phys->smp_object = sm->sm_object;
281
282 if (range_tree_space(rt) == 0) {
283 VERIFY3U(sm->sm_object, ==, sm->sm_phys->smp_object);
284 return;
285 }
286
287 if (maptype == SM_ALLOC)
288 sm->sm_phys->smp_alloc += range_tree_space(rt);
289 else
290 sm->sm_phys->smp_alloc -= range_tree_space(rt);
300 SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
301 SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
302
303 total = 0;
304 nodes = avl_numnodes(&rt->rt_root);
305 rt_space = range_tree_space(rt);
306 for (rs = avl_first(t); rs != NULL; rs = AVL_NEXT(t, rs)) {
307 uint64_t start;
308
309 size = (rs->rs_end - rs->rs_start) >> sm->sm_shift;
310 start = (rs->rs_start - sm->sm_start) >> sm->sm_shift;
311
312 total += size << sm->sm_shift;
313
314 while (size != 0) {
315 uint64_t run_len;
316
317 run_len = MIN(size, SM_RUN_MAX);
318
319 if (entry == entry_map_end) {
320 dmu_write(os, space_map_object(sm),
321 sm->sm_phys->smp_objsize, sm->sm_blksz,
322 entry_map, tx);
323 sm->sm_phys->smp_objsize += sm->sm_blksz;
324 entry = entry_map;
325 }
326
327 *entry++ = SM_OFFSET_ENCODE(start) |
328 SM_TYPE_ENCODE(maptype) |
329 SM_RUN_ENCODE(run_len);
330
331 start += run_len;
332 size -= run_len;
333 actual_entries++;
334 }
335 }
336
337 if (entry != entry_map) {
338 size = (entry - entry_map) * sizeof (uint64_t);
339 dmu_write(os, space_map_object(sm), sm->sm_phys->smp_objsize,
340 size, entry_map, tx);
341 sm->sm_phys->smp_objsize += size;
342 }
343 ASSERT3U(expected_entries, ==, actual_entries);
344
345 /*
346 * Ensure that the space_map's accounting wasn't changed
347 * while we were in the middle of writing it out.
348 */
349 VERIFY3U(nodes, ==, avl_numnodes(&rt->rt_root));
350 VERIFY3U(range_tree_space(rt), ==, rt_space);
351 VERIFY3U(range_tree_space(rt), ==, total);
352
353 zio_buf_free(entry_map, sm->sm_blksz);
354 }
355
356 static int
357 space_map_open_impl(space_map_t *sm)
358 {
359 int error;
360 u_longlong_t blocks;
361
362 error = dmu_bonus_hold(sm->sm_os, sm->sm_object, sm, &sm->sm_dbuf);
363 if (error)
364 return (error);
365
366 dmu_object_size_from_db(sm->sm_dbuf, &sm->sm_blksz, &blocks);
367 sm->sm_phys = sm->sm_dbuf->db_data;
368 return (0);
369 }
370
371 int
372 space_map_open(space_map_t **smp, objset_t *os, uint64_t object,
373 uint64_t start, uint64_t size, uint8_t shift)
374 {
375 space_map_t *sm;
376 int error;
377
378 ASSERT(*smp == NULL);
379 ASSERT(os != NULL);
380 ASSERT(object != 0);
381
382 sm = kmem_zalloc(sizeof (space_map_t), KM_SLEEP);
383
384 sm->sm_start = start;
385 sm->sm_size = size;
386 sm->sm_shift = shift;
387 sm->sm_os = os;
388 sm->sm_object = object;
389
390 error = space_map_open_impl(sm);
391 if (error != 0) {
392 space_map_close(sm);
393 return (error);
394 }
395
396 *smp = sm;
397
398 return (0);
399 }
400
401 void
402 space_map_close(space_map_t *sm)
403 {
404 if (sm == NULL)
405 return;
406
455 * bugs related to the uncommon case do not go unnoticed.
456 */
457 bzero(sm->sm_phys->smp_histogram,
458 sizeof (sm->sm_phys->smp_histogram));
459 }
460
461 dmu_buf_will_dirty(sm->sm_dbuf, tx);
462 sm->sm_phys->smp_objsize = 0;
463 sm->sm_phys->smp_alloc = 0;
464 }
465
466 /*
467 * Update the in-core space_map allocation and length values.
468 */
469 void
470 space_map_update(space_map_t *sm)
471 {
472 if (sm == NULL)
473 return;
474
475 sm->sm_alloc = sm->sm_phys->smp_alloc;
476 sm->sm_length = sm->sm_phys->smp_objsize;
477 }
478
479 uint64_t
480 space_map_alloc(objset_t *os, dmu_tx_t *tx)
481 {
482 spa_t *spa = dmu_objset_spa(os);
483 uint64_t object;
484 int bonuslen;
485
486 if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
487 spa_feature_incr(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM, tx);
488 bonuslen = sizeof (space_map_phys_t);
489 ASSERT3U(bonuslen, <=, dmu_bonus_max());
490 } else {
491 bonuslen = SPACE_MAP_SIZE_V0;
492 }
493
494 object = dmu_object_alloc(os,
495 DMU_OT_SPACE_MAP, space_map_blksz,
496 DMU_OT_SPACE_MAP_HEADER, bonuslen, tx);
497
498 return (object);
499 }
500
501 void
502 space_map_free_obj(objset_t *os, uint64_t smobj, dmu_tx_t *tx)
503 {
504 spa_t *spa = dmu_objset_spa(os);
505 if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
506 dmu_object_info_t doi;
507
508 VERIFY0(dmu_object_info(os, smobj, &doi));
509 if (doi.doi_bonus_size != SPACE_MAP_SIZE_V0) {
510 spa_feature_decr(spa,
511 SPA_FEATURE_SPACEMAP_HISTOGRAM, tx);
512 }
513 }
514
515 VERIFY0(dmu_object_free(os, smobj, tx));
516 }
517
518 void
519 space_map_free(space_map_t *sm, dmu_tx_t *tx)
520 {
521 if (sm == NULL)
522 return;
523
524 space_map_free_obj(sm->sm_os, space_map_object(sm), tx);
525 sm->sm_object = 0;
526 }
527
528 uint64_t
529 space_map_object(space_map_t *sm)
530 {
531 return (sm != NULL ? sm->sm_object : 0);
532 }
533
534 /*
535 * Returns the already synced, on-disk allocated space.
536 */
537 uint64_t
538 space_map_allocated(space_map_t *sm)
539 {
540 return (sm != NULL ? sm->sm_alloc : 0);
541 }
542
543 /*
544 * Returns the already synced, on-disk length;
|
29 #include <sys/zfs_context.h>
30 #include <sys/spa.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dnode.h>
34 #include <sys/dsl_pool.h>
35 #include <sys/zio.h>
36 #include <sys/space_map.h>
37 #include <sys/refcount.h>
38 #include <sys/zfeature.h>
39
40 /*
41 * The data for a given space map can be kept on blocks of any size.
42 * Larger blocks entail fewer i/o operations, but they also cause the
43 * DMU to keep more data in-core, and also to waste more i/o bandwidth
44 * when only a few blocks have changed since the last transaction group.
45 */
46 int space_map_blksz = (1 << 12);
47
48 /*
49 * Load the space map disk into the specified range tree. Segments of maptype
50 * are added to the range tree, other segment types are removed.
51 *
52 * Note: space_map_load() will drop sm_lock across dmu_read() calls.
53 * The caller must be OK with this.
54 */
55 int
56 space_map_load(space_map_t *sm, range_tree_t *rt, maptype_t maptype)
57 {
58 uint64_t *entry, *entry_map, *entry_map_end;
59 uint64_t bufsize, size, offset, end, space;
60 int error = 0;
61
62 ASSERT(MUTEX_HELD(sm->sm_lock));
63
64 end = space_map_length(sm);
65 space = space_map_allocated(sm);
66
67 VERIFY0(range_tree_space(rt));
68
69 if (maptype == SM_FREE) {
70 range_tree_add(rt, sm->sm_start, sm->sm_size);
71 space = sm->sm_size - space;
72 }
73
74 bufsize = MAX(sm->sm_blksz, SPA_MINBLOCKSIZE);
75 entry_map = zio_buf_alloc(bufsize);
76
77 mutex_exit(sm->sm_lock);
78 if (end > bufsize) {
79 dmu_prefetch(sm->sm_os, space_map_object(sm), 0, bufsize,
80 end - bufsize, ZIO_PRIORITY_SYNC_READ);
81 }
82 mutex_enter(sm->sm_lock);
83
84 for (offset = 0; offset < end; offset += bufsize) {
85 size = MIN(end - offset, bufsize);
86 VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
87 VERIFY(size != 0);
88 ASSERT3U(sm->sm_blksz, !=, 0);
89
90 dprintf("object=%llu offset=%llx size=%llx\n",
91 space_map_object(sm), offset, size);
92
93 mutex_exit(sm->sm_lock);
94 error = dmu_read(sm->sm_os, space_map_object(sm), offset, size,
95 entry_map, DMU_READ_PREFETCH);
96 mutex_enter(sm->sm_lock);
97 if (error != 0)
98 break;
99
100 entry_map_end = entry_map + (size / sizeof (uint64_t));
101 for (entry = entry_map; entry < entry_map_end; entry++) {
102 uint64_t e = *entry;
103 uint64_t offset, size;
104
105 if (SM_DEBUG_DECODE(e)) /* Skip debug entries */
106 continue;
107
108 offset = (SM_OFFSET_DECODE(e) << sm->sm_shift) +
109 sm->sm_start;
110 size = SM_RUN_DECODE(e) << sm->sm_shift;
111
112 VERIFY0(P2PHASE(offset, 1ULL << sm->sm_shift));
113 VERIFY0(P2PHASE(size, 1ULL << sm->sm_shift));
114 VERIFY3U(offset, >=, sm->sm_start);
115 VERIFY3U(offset + size, <=, sm->sm_start + sm->sm_size);
116 if (SM_TYPE_DECODE(e) == maptype) {
117 VERIFY3U(range_tree_space(rt) + size, <=,
118 sm->sm_size);
119 range_tree_add(rt, offset, size);
120 } else {
121 range_tree_remove(rt, offset, size);
122 }
123 }
124 }
125
126 if (error == 0)
127 VERIFY3U(range_tree_space(rt), ==, space);
128 else
129 range_tree_vacate(rt, NULL, NULL);
130
131 zio_buf_free(entry_map, bufsize);
132 return (error);
133 }
134
135 void
136 space_map_histogram_clear(space_map_t *sm)
137 {
138 if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t))
139 return;
140
141 bzero(sm->sm_phys->smp_histogram, sizeof (sm->sm_phys->smp_histogram));
142 }
143
144 boolean_t
145 space_map_histogram_verify(space_map_t *sm, range_tree_t *rt)
146 {
147 /*
148 * Verify that the in-core range tree does not have any
149 * ranges smaller than our sm_shift size.
150 */
151 for (int i = 0; i < sm->sm_shift; i++) {
152 if (rt->rt_histogram[i] != 0)
153 return (B_FALSE);
154 }
155 return (B_TRUE);
156 }
157
158 void
159 space_map_histogram_add(space_map_t *sm, range_tree_t *rt, dmu_tx_t *tx)
160 {
161 int idx = 0;
162
163 ASSERT(MUTEX_HELD(rt->rt_lock));
164 ASSERT(dmu_tx_is_syncing(tx));
165 VERIFY3U(space_map_object(sm), !=, 0);
166
167 if (sm->sm_dbuf->db_size != sizeof (space_map_phys_t))
168 return;
169
170 dmu_buf_will_dirty(sm->sm_dbuf, tx);
171
172 ASSERT(space_map_histogram_verify(sm, rt));
173 /*
174 * Transfer the content of the range tree histogram to the space
175 * map histogram. The space map histogram contains 32 buckets ranging
176 * between 2^sm_shift to 2^(32+sm_shift-1). The range tree,
177 * however, can represent ranges from 2^0 to 2^63. Since the space
178 * map only cares about allocatable blocks (minimum of sm_shift) we
179 * can safely ignore all ranges in the range tree smaller than sm_shift.
180 */
181 for (int i = sm->sm_shift; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
182
183 /*
212 avl_tree_t *t = &rt->rt_root;
213 range_seg_t *rs;
214 uint64_t size, entries;
215
216 /*
217 * All space_maps always have a debug entry so account for it here.
218 */
219 entries = 1;
220
221 /*
222 * Traverse the range tree and calculate the number of space map
223 * entries that would be required to write out the range tree.
224 */
225 for (rs = avl_first(t); rs != NULL; rs = AVL_NEXT(t, rs)) {
226 size = (rs->rs_end - rs->rs_start) >> sm->sm_shift;
227 entries += howmany(size, SM_RUN_MAX);
228 }
229 return (entries);
230 }
231
232 /*
233 * Note: space_map_write() will drop sm_lock across dmu_write() calls.
234 */
235 void
236 space_map_write(space_map_t *sm, range_tree_t *rt, maptype_t maptype,
237 dmu_tx_t *tx)
238 {
239 objset_t *os = sm->sm_os;
240 spa_t *spa = dmu_objset_spa(os);
241 avl_tree_t *t = &rt->rt_root;
242 range_seg_t *rs;
243 uint64_t size, total, rt_space, nodes;
244 uint64_t *entry, *entry_map, *entry_map_end;
245 uint64_t expected_entries, actual_entries = 1;
246
247 ASSERT(MUTEX_HELD(rt->rt_lock));
248 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
249 VERIFY3U(space_map_object(sm), !=, 0);
250 dmu_buf_will_dirty(sm->sm_dbuf, tx);
251
252 /*
253 * This field is no longer necessary since the in-core space map
254 * now contains the object number but is maintained for backwards
255 * compatibility.
256 */
257 sm->sm_phys->smp_object = sm->sm_object;
258
259 if (range_tree_space(rt) == 0) {
260 VERIFY3U(sm->sm_object, ==, sm->sm_phys->smp_object);
261 return;
262 }
263
264 if (maptype == SM_ALLOC)
265 sm->sm_phys->smp_alloc += range_tree_space(rt);
266 else
267 sm->sm_phys->smp_alloc -= range_tree_space(rt);
277 SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
278 SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
279
280 total = 0;
281 nodes = avl_numnodes(&rt->rt_root);
282 rt_space = range_tree_space(rt);
283 for (rs = avl_first(t); rs != NULL; rs = AVL_NEXT(t, rs)) {
284 uint64_t start;
285
286 size = (rs->rs_end - rs->rs_start) >> sm->sm_shift;
287 start = (rs->rs_start - sm->sm_start) >> sm->sm_shift;
288
289 total += size << sm->sm_shift;
290
291 while (size != 0) {
292 uint64_t run_len;
293
294 run_len = MIN(size, SM_RUN_MAX);
295
296 if (entry == entry_map_end) {
297 mutex_exit(rt->rt_lock);
298 dmu_write(os, space_map_object(sm),
299 sm->sm_phys->smp_objsize, sm->sm_blksz,
300 entry_map, tx);
301 mutex_enter(rt->rt_lock);
302 sm->sm_phys->smp_objsize += sm->sm_blksz;
303 entry = entry_map;
304 }
305
306 *entry++ = SM_OFFSET_ENCODE(start) |
307 SM_TYPE_ENCODE(maptype) |
308 SM_RUN_ENCODE(run_len);
309
310 start += run_len;
311 size -= run_len;
312 actual_entries++;
313 }
314 }
315
316 if (entry != entry_map) {
317 size = (entry - entry_map) * sizeof (uint64_t);
318 mutex_exit(rt->rt_lock);
319 dmu_write(os, space_map_object(sm), sm->sm_phys->smp_objsize,
320 size, entry_map, tx);
321 mutex_enter(rt->rt_lock);
322 sm->sm_phys->smp_objsize += size;
323 }
324 ASSERT3U(expected_entries, ==, actual_entries);
325
326 /*
327 * Ensure that the space_map's accounting wasn't changed
328 * while we were in the middle of writing it out.
329 */
330 VERIFY3U(nodes, ==, avl_numnodes(&rt->rt_root));
331 VERIFY3U(range_tree_space(rt), ==, rt_space);
332 VERIFY3U(range_tree_space(rt), ==, total);
333
334 zio_buf_free(entry_map, sm->sm_blksz);
335 }
336
337 static int
338 space_map_open_impl(space_map_t *sm)
339 {
340 int error;
341 u_longlong_t blocks;
342
343 error = dmu_bonus_hold(sm->sm_os, sm->sm_object, sm, &sm->sm_dbuf);
344 if (error)
345 return (error);
346
347 dmu_object_size_from_db(sm->sm_dbuf, &sm->sm_blksz, &blocks);
348 sm->sm_phys = sm->sm_dbuf->db_data;
349 return (0);
350 }
351
352 int
353 space_map_open(space_map_t **smp, objset_t *os, uint64_t object,
354 uint64_t start, uint64_t size, uint8_t shift, kmutex_t *lp)
355 {
356 space_map_t *sm;
357 int error;
358
359 ASSERT(*smp == NULL);
360 ASSERT(os != NULL);
361 ASSERT(object != 0);
362
363 sm = kmem_zalloc(sizeof (space_map_t), KM_SLEEP);
364
365 sm->sm_start = start;
366 sm->sm_size = size;
367 sm->sm_shift = shift;
368 sm->sm_lock = lp;
369 sm->sm_os = os;
370 sm->sm_object = object;
371
372 error = space_map_open_impl(sm);
373 if (error != 0) {
374 space_map_close(sm);
375 return (error);
376 }
377
378 *smp = sm;
379
380 return (0);
381 }
382
383 void
384 space_map_close(space_map_t *sm)
385 {
386 if (sm == NULL)
387 return;
388
437 * bugs related to the uncommon case do not go unnoticed.
438 */
439 bzero(sm->sm_phys->smp_histogram,
440 sizeof (sm->sm_phys->smp_histogram));
441 }
442
443 dmu_buf_will_dirty(sm->sm_dbuf, tx);
444 sm->sm_phys->smp_objsize = 0;
445 sm->sm_phys->smp_alloc = 0;
446 }
447
448 /*
449 * Update the in-core space_map allocation and length values.
450 */
451 void
452 space_map_update(space_map_t *sm)
453 {
454 if (sm == NULL)
455 return;
456
457 ASSERT(MUTEX_HELD(sm->sm_lock));
458
459 sm->sm_alloc = sm->sm_phys->smp_alloc;
460 sm->sm_length = sm->sm_phys->smp_objsize;
461 }
462
463 uint64_t
464 space_map_alloc(objset_t *os, dmu_tx_t *tx)
465 {
466 spa_t *spa = dmu_objset_spa(os);
467 uint64_t object;
468 int bonuslen;
469
470 if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
471 spa_feature_incr(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM, tx);
472 bonuslen = sizeof (space_map_phys_t);
473 ASSERT3U(bonuslen, <=, dmu_bonus_max());
474 } else {
475 bonuslen = SPACE_MAP_SIZE_V0;
476 }
477
478 object = dmu_object_alloc(os,
479 DMU_OT_SPACE_MAP, space_map_blksz,
480 DMU_OT_SPACE_MAP_HEADER, bonuslen, tx);
481
482 return (object);
483 }
484
485 void
486 space_map_free(space_map_t *sm, dmu_tx_t *tx)
487 {
488 spa_t *spa;
489
490 if (sm == NULL)
491 return;
492
493 spa = dmu_objset_spa(sm->sm_os);
494 if (spa_feature_is_enabled(spa, SPA_FEATURE_SPACEMAP_HISTOGRAM)) {
495 dmu_object_info_t doi;
496
497 dmu_object_info_from_db(sm->sm_dbuf, &doi);
498 if (doi.doi_bonus_size != SPACE_MAP_SIZE_V0) {
499 VERIFY(spa_feature_is_active(spa,
500 SPA_FEATURE_SPACEMAP_HISTOGRAM));
501 spa_feature_decr(spa,
502 SPA_FEATURE_SPACEMAP_HISTOGRAM, tx);
503 }
504 }
505
506 VERIFY3U(dmu_object_free(sm->sm_os, space_map_object(sm), tx), ==, 0);
507 sm->sm_object = 0;
508 }
509
510 uint64_t
511 space_map_object(space_map_t *sm)
512 {
513 return (sm != NULL ? sm->sm_object : 0);
514 }
515
516 /*
517 * Returns the already synced, on-disk allocated space.
518 */
519 uint64_t
520 space_map_allocated(space_map_t *sm)
521 {
522 return (sm != NULL ? sm->sm_alloc : 0);
523 }
524
525 /*
526 * Returns the already synced, on-disk length;
|