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