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