xref: /titanic_51/usr/src/uts/common/fs/zfs/space_map.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/zfs_context.h>
30 #include <sys/spa.h>
31 #include <sys/dmu.h>
32 #include <sys/space_map.h>
33 
34 /*
35  * Space map routines.
36  * NOTE: caller is responsible for all locking.
37  */
38 static int
39 space_map_seg_compare(const void *x1, const void *x2)
40 {
41 	const space_seg_t *s1 = x1;
42 	const space_seg_t *s2 = x2;
43 
44 	if (s1->ss_start < s2->ss_start) {
45 		if (s1->ss_end > s2->ss_start)
46 			return (0);
47 		return (-1);
48 	}
49 	if (s1->ss_start > s2->ss_start) {
50 		if (s1->ss_start < s2->ss_end)
51 			return (0);
52 		return (1);
53 	}
54 	return (0);
55 }
56 
57 void
58 space_map_create(space_map_t *sm, uint64_t start, uint64_t size, uint64_t shift,
59 	kmutex_t *lp)
60 {
61 	avl_create(&sm->sm_root, space_map_seg_compare,
62 	    sizeof (space_seg_t), offsetof(struct space_seg, ss_node));
63 	sm->sm_start = start;
64 	sm->sm_end = start + size;
65 	sm->sm_size = size;
66 	sm->sm_shift = shift;
67 	sm->sm_space = 0;
68 	sm->sm_lock = lp;
69 }
70 
71 void
72 space_map_destroy(space_map_t *sm)
73 {
74 	VERIFY3U(sm->sm_space, ==, 0);
75 	avl_destroy(&sm->sm_root);
76 }
77 
78 void
79 space_map_add(space_map_t *sm, uint64_t start, uint64_t size)
80 {
81 	avl_index_t where;
82 	space_seg_t ssearch, *ss_before, *ss_after, *ss;
83 	uint64_t end = start + size;
84 	int merge_before, merge_after;
85 
86 	ASSERT(MUTEX_HELD(sm->sm_lock));
87 	VERIFY(size != 0);
88 	VERIFY3U(start, >=, sm->sm_start);
89 	VERIFY3U(end, <=, sm->sm_end);
90 	VERIFY(sm->sm_space + size <= sm->sm_size);
91 	VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
92 	VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
93 
94 	ssearch.ss_start = start;
95 	ssearch.ss_end = end;
96 	ss = avl_find(&sm->sm_root, &ssearch, &where);
97 
98 	/* Make sure we don't overlap with either of our neighbors */
99 	VERIFY(ss == NULL);
100 
101 	ss_before = avl_nearest(&sm->sm_root, where, AVL_BEFORE);
102 	ss_after = avl_nearest(&sm->sm_root, where, AVL_AFTER);
103 
104 	merge_before = (ss_before != NULL && ss_before->ss_end == start);
105 	merge_after = (ss_after != NULL && ss_after->ss_start == end);
106 
107 	if (merge_before && merge_after) {
108 		avl_remove(&sm->sm_root, ss_before);
109 		ss_after->ss_start = ss_before->ss_start;
110 		kmem_free(ss_before, sizeof (*ss_before));
111 	} else if (merge_before) {
112 		ss_before->ss_end = end;
113 	} else if (merge_after) {
114 		ss_after->ss_start = start;
115 	} else {
116 		ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
117 		ss->ss_start = start;
118 		ss->ss_end = end;
119 		avl_insert(&sm->sm_root, ss, where);
120 	}
121 
122 	sm->sm_space += size;
123 }
124 
125 void
126 space_map_remove(space_map_t *sm, uint64_t start, uint64_t size)
127 {
128 	avl_index_t where;
129 	space_seg_t ssearch, *ss, *newseg;
130 	uint64_t end = start + size;
131 	int left_over, right_over;
132 
133 	ASSERT(MUTEX_HELD(sm->sm_lock));
134 	VERIFY(size != 0);
135 	VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
136 	VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
137 
138 	ssearch.ss_start = start;
139 	ssearch.ss_end = end;
140 	ss = avl_find(&sm->sm_root, &ssearch, &where);
141 
142 	/* Make sure we completely overlap with someone */
143 	VERIFY(ss != NULL);
144 	VERIFY3U(ss->ss_start, <=, start);
145 	VERIFY3U(ss->ss_end, >=, end);
146 	VERIFY(sm->sm_space - size <= sm->sm_size);
147 
148 	left_over = (ss->ss_start != start);
149 	right_over = (ss->ss_end != end);
150 
151 	if (left_over && right_over) {
152 		newseg = kmem_alloc(sizeof (*newseg), KM_SLEEP);
153 		newseg->ss_start = end;
154 		newseg->ss_end = ss->ss_end;
155 		ss->ss_end = start;
156 		avl_insert_here(&sm->sm_root, newseg, ss, AVL_AFTER);
157 	} else if (left_over) {
158 		ss->ss_end = start;
159 	} else if (right_over) {
160 		ss->ss_start = end;
161 	} else {
162 		avl_remove(&sm->sm_root, ss);
163 		kmem_free(ss, sizeof (*ss));
164 	}
165 
166 	sm->sm_space -= size;
167 }
168 
169 int
170 space_map_contains(space_map_t *sm, uint64_t start, uint64_t size)
171 {
172 	avl_index_t where;
173 	space_seg_t ssearch, *ss;
174 	uint64_t end = start + size;
175 
176 	ASSERT(MUTEX_HELD(sm->sm_lock));
177 	VERIFY(size != 0);
178 	VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
179 	VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
180 
181 	ssearch.ss_start = start;
182 	ssearch.ss_end = end;
183 	ss = avl_find(&sm->sm_root, &ssearch, &where);
184 
185 	return (ss != NULL && ss->ss_start <= start && ss->ss_end >= end);
186 }
187 
188 void
189 space_map_vacate(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
190 {
191 	space_seg_t *ss;
192 	void *cookie = NULL;
193 
194 	ASSERT(MUTEX_HELD(sm->sm_lock));
195 
196 	while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
197 		if (func != NULL)
198 			func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
199 		kmem_free(ss, sizeof (*ss));
200 	}
201 	sm->sm_space = 0;
202 }
203 
204 void
205 space_map_iterate(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
206 {
207 	space_seg_t *ss;
208 
209 	for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
210 		func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
211 }
212 
213 void
214 space_map_merge(space_map_t *src, space_map_t *dest)
215 {
216 	space_map_vacate(src, space_map_add, dest);
217 }
218 
219 void
220 space_map_excise(space_map_t *sm, uint64_t start, uint64_t size)
221 {
222 	avl_tree_t *t = &sm->sm_root;
223 	avl_index_t where;
224 	space_seg_t *ss, search;
225 	uint64_t end = start + size;
226 	uint64_t rm_start, rm_end;
227 
228 	ASSERT(MUTEX_HELD(sm->sm_lock));
229 
230 	search.ss_start = start;
231 	search.ss_end = start;
232 
233 	for (;;) {
234 		ss = avl_find(t, &search, &where);
235 
236 		if (ss == NULL)
237 			ss = avl_nearest(t, where, AVL_AFTER);
238 
239 		if (ss == NULL || ss->ss_start >= end)
240 			break;
241 
242 		rm_start = MAX(ss->ss_start, start);
243 		rm_end = MIN(ss->ss_end, end);
244 
245 		space_map_remove(sm, rm_start, rm_end - rm_start);
246 	}
247 }
248 
249 /*
250  * Replace smd with the union of smd and sms.
251  */
252 void
253 space_map_union(space_map_t *smd, space_map_t *sms)
254 {
255 	avl_tree_t *t = &sms->sm_root;
256 	space_seg_t *ss;
257 
258 	ASSERT(MUTEX_HELD(smd->sm_lock));
259 
260 	/*
261 	 * For each source segment, remove any intersections with the
262 	 * destination, then add the source segment to the destination.
263 	 */
264 	for (ss = avl_first(t); ss != NULL; ss = AVL_NEXT(t, ss)) {
265 		space_map_excise(smd, ss->ss_start, ss->ss_end - ss->ss_start);
266 		space_map_add(smd, ss->ss_start, ss->ss_end - ss->ss_start);
267 	}
268 }
269 
270 int
271 space_map_load(space_map_t *sm, space_map_obj_t *smo, uint8_t maptype,
272 	objset_t *os, uint64_t end, uint64_t space)
273 {
274 	uint64_t *entry, *entry_map, *entry_map_end;
275 	uint64_t bufsize, size, offset;
276 	uint64_t mapstart = sm->sm_start;
277 
278 	ASSERT(MUTEX_HELD(sm->sm_lock));
279 	VERIFY3U(sm->sm_space, ==, 0);
280 
281 	bufsize = MIN(end, SPACE_MAP_CHUNKSIZE);
282 	entry_map = kmem_alloc(bufsize, KM_SLEEP);
283 
284 	if (maptype == SM_FREE) {
285 		space_map_add(sm, sm->sm_start, sm->sm_size);
286 		space = sm->sm_size - space;
287 	}
288 
289 	for (offset = 0; offset < end; offset += bufsize) {
290 		size = MIN(end - offset, bufsize);
291 		VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
292 		VERIFY(size != 0);
293 
294 		dprintf("object=%llu  offset=%llx  size=%llx\n",
295 		    smo->smo_object, offset, size);
296 		dmu_read(os, smo->smo_object, offset, size, entry_map);
297 
298 		entry_map_end = entry_map + (size / sizeof (uint64_t));
299 		for (entry = entry_map; entry < entry_map_end; entry++) {
300 			uint64_t e = *entry;
301 
302 			if (SM_DEBUG_DECODE(e))		/* Skip debug entries */
303 				continue;
304 
305 			(SM_TYPE_DECODE(e) == maptype ?
306 			    space_map_add : space_map_remove)(sm,
307 			    (SM_OFFSET_DECODE(e) << sm->sm_shift) + mapstart,
308 			    SM_RUN_DECODE(e) << sm->sm_shift);
309 		}
310 	}
311 	VERIFY3U(sm->sm_space, ==, space);
312 
313 	kmem_free(entry_map, bufsize);
314 
315 	return (0);
316 }
317 
318 void
319 space_map_sync(space_map_t *sm, space_map_t *dest, space_map_obj_t *smo,
320     uint8_t maptype, objset_t *os, dmu_tx_t *tx)
321 {
322 	spa_t *spa = dmu_objset_spa(os);
323 	void *cookie = NULL;
324 	space_seg_t *ss;
325 	uint64_t bufsize, start, size, run_len;
326 	uint64_t *entry, *entry_map, *entry_map_end;
327 
328 	ASSERT(MUTEX_HELD(sm->sm_lock));
329 
330 	if (sm->sm_space == 0)
331 		return;
332 
333 	dprintf("object %4llu, txg %llu, pass %d, %c, count %lu, space %llx\n",
334 	    smo->smo_object, dmu_tx_get_txg(tx), spa_sync_pass(spa),
335 	    maptype == SM_ALLOC ? 'A' : 'F', avl_numnodes(&sm->sm_root),
336 	    sm->sm_space);
337 
338 	bufsize = (8 + avl_numnodes(&sm->sm_root)) * sizeof (uint64_t);
339 	bufsize = MIN(bufsize, SPACE_MAP_CHUNKSIZE);
340 	entry_map = kmem_alloc(bufsize, KM_SLEEP);
341 	entry_map_end = entry_map + (bufsize / sizeof (uint64_t));
342 	entry = entry_map;
343 
344 	*entry++ = SM_DEBUG_ENCODE(1) |
345 	    SM_DEBUG_ACTION_ENCODE(maptype) |
346 	    SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
347 	    SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
348 
349 	while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
350 		size = ss->ss_end - ss->ss_start;
351 		start = (ss->ss_start - sm->sm_start) >> sm->sm_shift;
352 
353 		if (dest)
354 			space_map_add(dest, ss->ss_start, size);
355 
356 		sm->sm_space -= size;
357 		size >>= sm->sm_shift;
358 
359 		while (size) {
360 			run_len = MIN(size, SM_RUN_MAX);
361 
362 			if (entry == entry_map_end) {
363 				dmu_write(os, smo->smo_object, smo->smo_objsize,
364 				    bufsize, entry_map, tx);
365 				smo->smo_objsize += bufsize;
366 				entry = entry_map;
367 			}
368 
369 			*entry++ = SM_OFFSET_ENCODE(start) |
370 			    SM_TYPE_ENCODE(maptype) |
371 			    SM_RUN_ENCODE(run_len);
372 
373 			start += run_len;
374 			size -= run_len;
375 		}
376 		kmem_free(ss, sizeof (*ss));
377 	}
378 
379 	if (entry != entry_map) {
380 		size = (entry - entry_map) * sizeof (uint64_t);
381 		dmu_write(os, smo->smo_object, smo->smo_objsize,
382 		    size, entry_map, tx);
383 		smo->smo_objsize += size;
384 	}
385 
386 	kmem_free(entry_map, bufsize);
387 
388 	VERIFY3U(sm->sm_space, ==, 0);
389 }
390 
391 void
392 space_map_write(space_map_t *sm, space_map_obj_t *smo, objset_t *os,
393     dmu_tx_t *tx)
394 {
395 	uint64_t oldsize = smo->smo_objsize;
396 
397 	dmu_free_range(os, smo->smo_object, 0, smo->smo_objsize, tx);
398 
399 	smo->smo_objsize = 0;
400 
401 	VERIFY3U(sm->sm_space, ==, smo->smo_alloc);
402 	space_map_sync(sm, NULL, smo, SM_ALLOC, os, tx);
403 
404 	dprintf("write sm object %llu from %llu to %llu bytes in txg %llu\n",
405 	    smo->smo_object, oldsize, smo->smo_objsize, dmu_tx_get_txg(tx));
406 }
407