xref: /illumos-gate/usr/src/uts/common/fs/zfs/space_map.c (revision b76c1459)
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 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/spa.h>
28 #include <sys/dmu.h>
29 #include <sys/zio.h>
30 #include <sys/space_map.h>
31 
32 /*
33  * Space map routines.
34  * NOTE: caller is responsible for all locking.
35  */
36 static int
37 space_map_seg_compare(const void *x1, const void *x2)
38 {
39 	const space_seg_t *s1 = x1;
40 	const space_seg_t *s2 = x2;
41 
42 	if (s1->ss_start < s2->ss_start) {
43 		if (s1->ss_end > s2->ss_start)
44 			return (0);
45 		return (-1);
46 	}
47 	if (s1->ss_start > s2->ss_start) {
48 		if (s1->ss_start < s2->ss_end)
49 			return (0);
50 		return (1);
51 	}
52 	return (0);
53 }
54 
55 void
56 space_map_create(space_map_t *sm, uint64_t start, uint64_t size, uint8_t shift,
57 	kmutex_t *lp)
58 {
59 	bzero(sm, sizeof (*sm));
60 
61 	cv_init(&sm->sm_load_cv, NULL, CV_DEFAULT, NULL);
62 
63 	avl_create(&sm->sm_root, space_map_seg_compare,
64 	    sizeof (space_seg_t), offsetof(struct space_seg, ss_node));
65 
66 	sm->sm_start = start;
67 	sm->sm_size = size;
68 	sm->sm_shift = shift;
69 	sm->sm_lock = lp;
70 }
71 
72 void
73 space_map_destroy(space_map_t *sm)
74 {
75 	ASSERT(!sm->sm_loaded && !sm->sm_loading);
76 	VERIFY3U(sm->sm_space, ==, 0);
77 	avl_destroy(&sm->sm_root);
78 	cv_destroy(&sm->sm_load_cv);
79 }
80 
81 void
82 space_map_add(space_map_t *sm, uint64_t start, uint64_t size)
83 {
84 	avl_index_t where;
85 	space_seg_t ssearch, *ss_before, *ss_after, *ss;
86 	uint64_t end = start + size;
87 	int merge_before, merge_after;
88 
89 	ASSERT(MUTEX_HELD(sm->sm_lock));
90 	VERIFY(size != 0);
91 	VERIFY3U(start, >=, sm->sm_start);
92 	VERIFY3U(end, <=, sm->sm_start + sm->sm_size);
93 	VERIFY(sm->sm_space + size <= sm->sm_size);
94 	VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
95 	VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
96 
97 	ssearch.ss_start = start;
98 	ssearch.ss_end = end;
99 	ss = avl_find(&sm->sm_root, &ssearch, &where);
100 
101 	if (ss != NULL && ss->ss_start <= start && ss->ss_end >= end) {
102 		zfs_panic_recover("zfs: allocating allocated segment"
103 		    "(offset=%llu size=%llu)\n",
104 		    (longlong_t)start, (longlong_t)size);
105 		return;
106 	}
107 
108 	/* Make sure we don't overlap with either of our neighbors */
109 	VERIFY(ss == NULL);
110 
111 	ss_before = avl_nearest(&sm->sm_root, where, AVL_BEFORE);
112 	ss_after = avl_nearest(&sm->sm_root, where, AVL_AFTER);
113 
114 	merge_before = (ss_before != NULL && ss_before->ss_end == start);
115 	merge_after = (ss_after != NULL && ss_after->ss_start == end);
116 
117 	if (merge_before && merge_after) {
118 		avl_remove(&sm->sm_root, ss_before);
119 		if (sm->sm_pp_root) {
120 			avl_remove(sm->sm_pp_root, ss_before);
121 			avl_remove(sm->sm_pp_root, ss_after);
122 		}
123 		ss_after->ss_start = ss_before->ss_start;
124 		kmem_free(ss_before, sizeof (*ss_before));
125 		ss = ss_after;
126 	} else if (merge_before) {
127 		ss_before->ss_end = end;
128 		if (sm->sm_pp_root)
129 			avl_remove(sm->sm_pp_root, ss_before);
130 		ss = ss_before;
131 	} else if (merge_after) {
132 		ss_after->ss_start = start;
133 		if (sm->sm_pp_root)
134 			avl_remove(sm->sm_pp_root, ss_after);
135 		ss = ss_after;
136 	} else {
137 		ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
138 		ss->ss_start = start;
139 		ss->ss_end = end;
140 		avl_insert(&sm->sm_root, ss, where);
141 	}
142 
143 	if (sm->sm_pp_root)
144 		avl_add(sm->sm_pp_root, ss);
145 
146 	sm->sm_space += size;
147 }
148 
149 void
150 space_map_remove(space_map_t *sm, uint64_t start, uint64_t size)
151 {
152 	avl_index_t where;
153 	space_seg_t ssearch, *ss, *newseg;
154 	uint64_t end = start + size;
155 	int left_over, right_over;
156 
157 	ASSERT(MUTEX_HELD(sm->sm_lock));
158 	VERIFY(size != 0);
159 	VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
160 	VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
161 
162 	ssearch.ss_start = start;
163 	ssearch.ss_end = end;
164 	ss = avl_find(&sm->sm_root, &ssearch, &where);
165 
166 	/* Make sure we completely overlap with someone */
167 	if (ss == NULL) {
168 		zfs_panic_recover("zfs: freeing free segment "
169 		    "(offset=%llu size=%llu)",
170 		    (longlong_t)start, (longlong_t)size);
171 		return;
172 	}
173 	VERIFY3U(ss->ss_start, <=, start);
174 	VERIFY3U(ss->ss_end, >=, end);
175 	VERIFY(sm->sm_space - size <= sm->sm_size);
176 
177 	left_over = (ss->ss_start != start);
178 	right_over = (ss->ss_end != end);
179 
180 	if (sm->sm_pp_root)
181 		avl_remove(sm->sm_pp_root, ss);
182 
183 	if (left_over && right_over) {
184 		newseg = kmem_alloc(sizeof (*newseg), KM_SLEEP);
185 		newseg->ss_start = end;
186 		newseg->ss_end = ss->ss_end;
187 		ss->ss_end = start;
188 		avl_insert_here(&sm->sm_root, newseg, ss, AVL_AFTER);
189 		if (sm->sm_pp_root)
190 			avl_add(sm->sm_pp_root, newseg);
191 	} else if (left_over) {
192 		ss->ss_end = start;
193 	} else if (right_over) {
194 		ss->ss_start = end;
195 	} else {
196 		avl_remove(&sm->sm_root, ss);
197 		kmem_free(ss, sizeof (*ss));
198 		ss = NULL;
199 	}
200 
201 	if (sm->sm_pp_root && ss != NULL)
202 		avl_add(sm->sm_pp_root, ss);
203 
204 	sm->sm_space -= size;
205 }
206 
207 boolean_t
208 space_map_contains(space_map_t *sm, uint64_t start, uint64_t size)
209 {
210 	avl_index_t where;
211 	space_seg_t ssearch, *ss;
212 	uint64_t end = start + size;
213 
214 	ASSERT(MUTEX_HELD(sm->sm_lock));
215 	VERIFY(size != 0);
216 	VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
217 	VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
218 
219 	ssearch.ss_start = start;
220 	ssearch.ss_end = end;
221 	ss = avl_find(&sm->sm_root, &ssearch, &where);
222 
223 	return (ss != NULL && ss->ss_start <= start && ss->ss_end >= end);
224 }
225 
226 void
227 space_map_vacate(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
228 {
229 	space_seg_t *ss;
230 	void *cookie = NULL;
231 
232 	ASSERT(MUTEX_HELD(sm->sm_lock));
233 
234 	while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
235 		if (func != NULL)
236 			func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
237 		kmem_free(ss, sizeof (*ss));
238 	}
239 	sm->sm_space = 0;
240 }
241 
242 void
243 space_map_walk(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
244 {
245 	space_seg_t *ss;
246 
247 	ASSERT(MUTEX_HELD(sm->sm_lock));
248 
249 	for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
250 		func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
251 }
252 
253 /*
254  * Wait for any in-progress space_map_load() to complete.
255  */
256 void
257 space_map_load_wait(space_map_t *sm)
258 {
259 	ASSERT(MUTEX_HELD(sm->sm_lock));
260 
261 	while (sm->sm_loading)
262 		cv_wait(&sm->sm_load_cv, sm->sm_lock);
263 }
264 
265 /*
266  * Note: space_map_load() will drop sm_lock across dmu_read() calls.
267  * The caller must be OK with this.
268  */
269 int
270 space_map_load(space_map_t *sm, space_map_ops_t *ops, uint8_t maptype,
271 	space_map_obj_t *smo, objset_t *os)
272 {
273 	uint64_t *entry, *entry_map, *entry_map_end;
274 	uint64_t bufsize, size, offset, end, space;
275 	uint64_t mapstart = sm->sm_start;
276 	int error = 0;
277 
278 	ASSERT(MUTEX_HELD(sm->sm_lock));
279 
280 	space_map_load_wait(sm);
281 
282 	if (sm->sm_loaded)
283 		return (0);
284 
285 	sm->sm_loading = B_TRUE;
286 	end = smo->smo_objsize;
287 	space = smo->smo_alloc;
288 
289 	ASSERT(sm->sm_ops == NULL);
290 	VERIFY3U(sm->sm_space, ==, 0);
291 
292 	if (maptype == SM_FREE) {
293 		space_map_add(sm, sm->sm_start, sm->sm_size);
294 		space = sm->sm_size - space;
295 	}
296 
297 	bufsize = 1ULL << SPACE_MAP_BLOCKSHIFT;
298 	entry_map = zio_buf_alloc(bufsize);
299 
300 	mutex_exit(sm->sm_lock);
301 	if (end > bufsize)
302 		dmu_prefetch(os, smo->smo_object, bufsize, end - bufsize);
303 	mutex_enter(sm->sm_lock);
304 
305 	for (offset = 0; offset < end; offset += bufsize) {
306 		size = MIN(end - offset, bufsize);
307 		VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
308 		VERIFY(size != 0);
309 
310 		dprintf("object=%llu  offset=%llx  size=%llx\n",
311 		    smo->smo_object, offset, size);
312 
313 		mutex_exit(sm->sm_lock);
314 		error = dmu_read(os, smo->smo_object, offset, size, entry_map,
315 		    DMU_READ_PREFETCH);
316 		mutex_enter(sm->sm_lock);
317 		if (error != 0)
318 			break;
319 
320 		entry_map_end = entry_map + (size / sizeof (uint64_t));
321 		for (entry = entry_map; entry < entry_map_end; entry++) {
322 			uint64_t e = *entry;
323 
324 			if (SM_DEBUG_DECODE(e))		/* Skip debug entries */
325 				continue;
326 
327 			(SM_TYPE_DECODE(e) == maptype ?
328 			    space_map_add : space_map_remove)(sm,
329 			    (SM_OFFSET_DECODE(e) << sm->sm_shift) + mapstart,
330 			    SM_RUN_DECODE(e) << sm->sm_shift);
331 		}
332 	}
333 
334 	if (error == 0) {
335 		VERIFY3U(sm->sm_space, ==, space);
336 
337 		sm->sm_loaded = B_TRUE;
338 		sm->sm_ops = ops;
339 		if (ops != NULL)
340 			ops->smop_load(sm);
341 	} else {
342 		if (ops != NULL)
343 			ops->smop_unload(sm);
344 		sm->sm_ops = NULL;
345 		space_map_vacate(sm, NULL, NULL);
346 	}
347 
348 	zio_buf_free(entry_map, bufsize);
349 
350 	sm->sm_loading = B_FALSE;
351 
352 	cv_broadcast(&sm->sm_load_cv);
353 
354 	return (error);
355 }
356 
357 void
358 space_map_unload(space_map_t *sm)
359 {
360 	ASSERT(MUTEX_HELD(sm->sm_lock));
361 
362 	if (sm->sm_loaded && sm->sm_ops != NULL)
363 		sm->sm_ops->smop_unload(sm);
364 
365 	sm->sm_loaded = B_FALSE;
366 	sm->sm_ops = NULL;
367 
368 	space_map_vacate(sm, NULL, NULL);
369 }
370 
371 uint64_t
372 space_map_maxsize(space_map_t *sm)
373 {
374 	if (sm->sm_loaded && sm->sm_ops != NULL)
375 		return (sm->sm_ops->smop_max(sm));
376 	else
377 		return (-1ULL);
378 }
379 
380 uint64_t
381 space_map_alloc(space_map_t *sm, uint64_t size)
382 {
383 	uint64_t start;
384 
385 	start = sm->sm_ops->smop_alloc(sm, size);
386 	if (start != -1ULL)
387 		space_map_remove(sm, start, size);
388 	return (start);
389 }
390 
391 void
392 space_map_claim(space_map_t *sm, uint64_t start, uint64_t size)
393 {
394 	sm->sm_ops->smop_claim(sm, start, size);
395 	space_map_remove(sm, start, size);
396 }
397 
398 void
399 space_map_free(space_map_t *sm, uint64_t start, uint64_t size)
400 {
401 	space_map_add(sm, start, size);
402 	sm->sm_ops->smop_free(sm, start, size);
403 }
404 
405 /*
406  * Note: space_map_sync() will drop sm_lock across dmu_write() calls.
407  */
408 void
409 space_map_sync(space_map_t *sm, uint8_t maptype,
410 	space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
411 {
412 	spa_t *spa = dmu_objset_spa(os);
413 	void *cookie = NULL;
414 	space_seg_t *ss;
415 	uint64_t bufsize, start, size, run_len;
416 	uint64_t *entry, *entry_map, *entry_map_end;
417 
418 	ASSERT(MUTEX_HELD(sm->sm_lock));
419 
420 	if (sm->sm_space == 0)
421 		return;
422 
423 	dprintf("object %4llu, txg %llu, pass %d, %c, count %lu, space %llx\n",
424 	    smo->smo_object, dmu_tx_get_txg(tx), spa_sync_pass(spa),
425 	    maptype == SM_ALLOC ? 'A' : 'F', avl_numnodes(&sm->sm_root),
426 	    sm->sm_space);
427 
428 	if (maptype == SM_ALLOC)
429 		smo->smo_alloc += sm->sm_space;
430 	else
431 		smo->smo_alloc -= sm->sm_space;
432 
433 	bufsize = (8 + avl_numnodes(&sm->sm_root)) * sizeof (uint64_t);
434 	bufsize = MIN(bufsize, 1ULL << SPACE_MAP_BLOCKSHIFT);
435 	entry_map = zio_buf_alloc(bufsize);
436 	entry_map_end = entry_map + (bufsize / sizeof (uint64_t));
437 	entry = entry_map;
438 
439 	*entry++ = SM_DEBUG_ENCODE(1) |
440 	    SM_DEBUG_ACTION_ENCODE(maptype) |
441 	    SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
442 	    SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
443 
444 	while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
445 		size = ss->ss_end - ss->ss_start;
446 		start = (ss->ss_start - sm->sm_start) >> sm->sm_shift;
447 
448 		sm->sm_space -= size;
449 		size >>= sm->sm_shift;
450 
451 		while (size) {
452 			run_len = MIN(size, SM_RUN_MAX);
453 
454 			if (entry == entry_map_end) {
455 				mutex_exit(sm->sm_lock);
456 				dmu_write(os, smo->smo_object, smo->smo_objsize,
457 				    bufsize, entry_map, tx);
458 				mutex_enter(sm->sm_lock);
459 				smo->smo_objsize += bufsize;
460 				entry = entry_map;
461 			}
462 
463 			*entry++ = SM_OFFSET_ENCODE(start) |
464 			    SM_TYPE_ENCODE(maptype) |
465 			    SM_RUN_ENCODE(run_len);
466 
467 			start += run_len;
468 			size -= run_len;
469 		}
470 		kmem_free(ss, sizeof (*ss));
471 	}
472 
473 	if (entry != entry_map) {
474 		size = (entry - entry_map) * sizeof (uint64_t);
475 		mutex_exit(sm->sm_lock);
476 		dmu_write(os, smo->smo_object, smo->smo_objsize,
477 		    size, entry_map, tx);
478 		mutex_enter(sm->sm_lock);
479 		smo->smo_objsize += size;
480 	}
481 
482 	zio_buf_free(entry_map, bufsize);
483 
484 	VERIFY3U(sm->sm_space, ==, 0);
485 }
486 
487 void
488 space_map_truncate(space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
489 {
490 	VERIFY(dmu_free_range(os, smo->smo_object, 0, -1ULL, tx) == 0);
491 
492 	smo->smo_objsize = 0;
493 	smo->smo_alloc = 0;
494 }
495 
496 /*
497  * Space map reference trees.
498  *
499  * A space map is a collection of integers.  Every integer is either
500  * in the map, or it's not.  A space map reference tree generalizes
501  * the idea: it allows its members to have arbitrary reference counts,
502  * as opposed to the implicit reference count of 0 or 1 in a space map.
503  * This representation comes in handy when computing the union or
504  * intersection of multiple space maps.  For example, the union of
505  * N space maps is the subset of the reference tree with refcnt >= 1.
506  * The intersection of N space maps is the subset with refcnt >= N.
507  *
508  * [It's very much like a Fourier transform.  Unions and intersections
509  * are hard to perform in the 'space map domain', so we convert the maps
510  * into the 'reference count domain', where it's trivial, then invert.]
511  *
512  * vdev_dtl_reassess() uses computations of this form to determine
513  * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
514  * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
515  * has an outage wherever refcnt >= vdev_children.
516  */
517 static int
518 space_map_ref_compare(const void *x1, const void *x2)
519 {
520 	const space_ref_t *sr1 = x1;
521 	const space_ref_t *sr2 = x2;
522 
523 	if (sr1->sr_offset < sr2->sr_offset)
524 		return (-1);
525 	if (sr1->sr_offset > sr2->sr_offset)
526 		return (1);
527 
528 	if (sr1 < sr2)
529 		return (-1);
530 	if (sr1 > sr2)
531 		return (1);
532 
533 	return (0);
534 }
535 
536 void
537 space_map_ref_create(avl_tree_t *t)
538 {
539 	avl_create(t, space_map_ref_compare,
540 	    sizeof (space_ref_t), offsetof(space_ref_t, sr_node));
541 }
542 
543 void
544 space_map_ref_destroy(avl_tree_t *t)
545 {
546 	space_ref_t *sr;
547 	void *cookie = NULL;
548 
549 	while ((sr = avl_destroy_nodes(t, &cookie)) != NULL)
550 		kmem_free(sr, sizeof (*sr));
551 
552 	avl_destroy(t);
553 }
554 
555 static void
556 space_map_ref_add_node(avl_tree_t *t, uint64_t offset, int64_t refcnt)
557 {
558 	space_ref_t *sr;
559 
560 	sr = kmem_alloc(sizeof (*sr), KM_SLEEP);
561 	sr->sr_offset = offset;
562 	sr->sr_refcnt = refcnt;
563 
564 	avl_add(t, sr);
565 }
566 
567 void
568 space_map_ref_add_seg(avl_tree_t *t, uint64_t start, uint64_t end,
569 	int64_t refcnt)
570 {
571 	space_map_ref_add_node(t, start, refcnt);
572 	space_map_ref_add_node(t, end, -refcnt);
573 }
574 
575 /*
576  * Convert (or add) a space map into a reference tree.
577  */
578 void
579 space_map_ref_add_map(avl_tree_t *t, space_map_t *sm, int64_t refcnt)
580 {
581 	space_seg_t *ss;
582 
583 	ASSERT(MUTEX_HELD(sm->sm_lock));
584 
585 	for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
586 		space_map_ref_add_seg(t, ss->ss_start, ss->ss_end, refcnt);
587 }
588 
589 /*
590  * Convert a reference tree into a space map.  The space map will contain
591  * all members of the reference tree for which refcnt >= minref.
592  */
593 void
594 space_map_ref_generate_map(avl_tree_t *t, space_map_t *sm, int64_t minref)
595 {
596 	uint64_t start = -1ULL;
597 	int64_t refcnt = 0;
598 	space_ref_t *sr;
599 
600 	ASSERT(MUTEX_HELD(sm->sm_lock));
601 
602 	space_map_vacate(sm, NULL, NULL);
603 
604 	for (sr = avl_first(t); sr != NULL; sr = AVL_NEXT(t, sr)) {
605 		refcnt += sr->sr_refcnt;
606 		if (refcnt >= minref) {
607 			if (start == -1ULL) {
608 				start = sr->sr_offset;
609 			}
610 		} else {
611 			if (start != -1ULL) {
612 				uint64_t end = sr->sr_offset;
613 				ASSERT(start <= end);
614 				if (end > start)
615 					space_map_add(sm, start, end - start);
616 				start = -1ULL;
617 			}
618 		}
619 	}
620 	ASSERT(refcnt == 0);
621 	ASSERT(start == -1ULL);
622 }
623