space_map.c revision b5e70f9763532dbf8f22010666d457240881c038
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 */
36static int
37space_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
55void
56space_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
72void
73space_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
81void
82space_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
135void
136space_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
184int
185space_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
203void
204space_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
219void
220space_map_walk(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
221{
222	space_seg_t *ss;
223
224	for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
225		func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
226}
227
228void
229space_map_excise(space_map_t *sm, uint64_t start, uint64_t size)
230{
231	avl_tree_t *t = &sm->sm_root;
232	avl_index_t where;
233	space_seg_t *ss, search;
234	uint64_t end = start + size;
235	uint64_t rm_start, rm_end;
236
237	ASSERT(MUTEX_HELD(sm->sm_lock));
238
239	search.ss_start = start;
240	search.ss_end = start;
241
242	for (;;) {
243		ss = avl_find(t, &search, &where);
244
245		if (ss == NULL)
246			ss = avl_nearest(t, where, AVL_AFTER);
247
248		if (ss == NULL || ss->ss_start >= end)
249			break;
250
251		rm_start = MAX(ss->ss_start, start);
252		rm_end = MIN(ss->ss_end, end);
253
254		space_map_remove(sm, rm_start, rm_end - rm_start);
255	}
256}
257
258/*
259 * Replace smd with the union of smd and sms.
260 */
261void
262space_map_union(space_map_t *smd, space_map_t *sms)
263{
264	avl_tree_t *t = &sms->sm_root;
265	space_seg_t *ss;
266
267	ASSERT(MUTEX_HELD(smd->sm_lock));
268
269	/*
270	 * For each source segment, remove any intersections with the
271	 * destination, then add the source segment to the destination.
272	 */
273	for (ss = avl_first(t); ss != NULL; ss = AVL_NEXT(t, ss)) {
274		space_map_excise(smd, ss->ss_start, ss->ss_end - ss->ss_start);
275		space_map_add(smd, ss->ss_start, ss->ss_end - ss->ss_start);
276	}
277}
278
279/*
280 * Wait for any in-progress space_map_load() to complete.
281 */
282void
283space_map_load_wait(space_map_t *sm)
284{
285	ASSERT(MUTEX_HELD(sm->sm_lock));
286
287	while (sm->sm_loading)
288		cv_wait(&sm->sm_load_cv, sm->sm_lock);
289}
290
291/*
292 * Note: space_map_load() will drop sm_lock across dmu_read() calls.
293 * The caller must be OK with this.
294 */
295int
296space_map_load(space_map_t *sm, space_map_ops_t *ops, uint8_t maptype,
297	space_map_obj_t *smo, objset_t *os)
298{
299	uint64_t *entry, *entry_map, *entry_map_end;
300	uint64_t bufsize, size, offset, end, space;
301	uint64_t mapstart = sm->sm_start;
302	int error = 0;
303
304	ASSERT(MUTEX_HELD(sm->sm_lock));
305
306	space_map_load_wait(sm);
307
308	if (sm->sm_loaded)
309		return (0);
310
311	sm->sm_loading = B_TRUE;
312	end = smo->smo_objsize;
313	space = smo->smo_alloc;
314
315	ASSERT(sm->sm_ops == NULL);
316	VERIFY3U(sm->sm_space, ==, 0);
317
318	if (maptype == SM_FREE) {
319		space_map_add(sm, sm->sm_start, sm->sm_size);
320		space = sm->sm_size - space;
321	}
322
323	bufsize = 1ULL << SPACE_MAP_BLOCKSHIFT;
324	entry_map = zio_buf_alloc(bufsize);
325
326	mutex_exit(sm->sm_lock);
327	if (end > bufsize)
328		dmu_prefetch(os, smo->smo_object, bufsize, end - bufsize);
329	mutex_enter(sm->sm_lock);
330
331	for (offset = 0; offset < end; offset += bufsize) {
332		size = MIN(end - offset, bufsize);
333		VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
334		VERIFY(size != 0);
335
336		dprintf("object=%llu  offset=%llx  size=%llx\n",
337		    smo->smo_object, offset, size);
338
339		mutex_exit(sm->sm_lock);
340		error = dmu_read(os, smo->smo_object, offset, size, entry_map);
341		mutex_enter(sm->sm_lock);
342		if (error != 0)
343			break;
344
345		entry_map_end = entry_map + (size / sizeof (uint64_t));
346		for (entry = entry_map; entry < entry_map_end; entry++) {
347			uint64_t e = *entry;
348
349			if (SM_DEBUG_DECODE(e))		/* Skip debug entries */
350				continue;
351
352			(SM_TYPE_DECODE(e) == maptype ?
353			    space_map_add : space_map_remove)(sm,
354			    (SM_OFFSET_DECODE(e) << sm->sm_shift) + mapstart,
355			    SM_RUN_DECODE(e) << sm->sm_shift);
356		}
357	}
358
359	if (error == 0) {
360		VERIFY3U(sm->sm_space, ==, space);
361
362		sm->sm_loaded = B_TRUE;
363		sm->sm_ops = ops;
364		if (ops != NULL)
365			ops->smop_load(sm);
366	} else {
367		space_map_vacate(sm, NULL, NULL);
368	}
369
370	zio_buf_free(entry_map, bufsize);
371
372	sm->sm_loading = B_FALSE;
373
374	cv_broadcast(&sm->sm_load_cv);
375
376	return (error);
377}
378
379void
380space_map_unload(space_map_t *sm)
381{
382	ASSERT(MUTEX_HELD(sm->sm_lock));
383
384	if (sm->sm_loaded && sm->sm_ops != NULL)
385		sm->sm_ops->smop_unload(sm);
386
387	sm->sm_loaded = B_FALSE;
388	sm->sm_ops = NULL;
389
390	space_map_vacate(sm, NULL, NULL);
391}
392
393uint64_t
394space_map_alloc(space_map_t *sm, uint64_t size)
395{
396	uint64_t start;
397
398	start = sm->sm_ops->smop_alloc(sm, size);
399	if (start != -1ULL)
400		space_map_remove(sm, start, size);
401	return (start);
402}
403
404void
405space_map_claim(space_map_t *sm, uint64_t start, uint64_t size)
406{
407	sm->sm_ops->smop_claim(sm, start, size);
408	space_map_remove(sm, start, size);
409}
410
411void
412space_map_free(space_map_t *sm, uint64_t start, uint64_t size)
413{
414	space_map_add(sm, start, size);
415	sm->sm_ops->smop_free(sm, start, size);
416}
417
418/*
419 * Note: space_map_sync() will drop sm_lock across dmu_write() calls.
420 */
421void
422space_map_sync(space_map_t *sm, uint8_t maptype,
423	space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
424{
425	spa_t *spa = dmu_objset_spa(os);
426	void *cookie = NULL;
427	space_seg_t *ss;
428	uint64_t bufsize, start, size, run_len;
429	uint64_t *entry, *entry_map, *entry_map_end;
430
431	ASSERT(MUTEX_HELD(sm->sm_lock));
432
433	if (sm->sm_space == 0)
434		return;
435
436	dprintf("object %4llu, txg %llu, pass %d, %c, count %lu, space %llx\n",
437	    smo->smo_object, dmu_tx_get_txg(tx), spa_sync_pass(spa),
438	    maptype == SM_ALLOC ? 'A' : 'F', avl_numnodes(&sm->sm_root),
439	    sm->sm_space);
440
441	if (maptype == SM_ALLOC)
442		smo->smo_alloc += sm->sm_space;
443	else
444		smo->smo_alloc -= sm->sm_space;
445
446	bufsize = (8 + avl_numnodes(&sm->sm_root)) * sizeof (uint64_t);
447	bufsize = MIN(bufsize, 1ULL << SPACE_MAP_BLOCKSHIFT);
448	entry_map = zio_buf_alloc(bufsize);
449	entry_map_end = entry_map + (bufsize / sizeof (uint64_t));
450	entry = entry_map;
451
452	*entry++ = SM_DEBUG_ENCODE(1) |
453	    SM_DEBUG_ACTION_ENCODE(maptype) |
454	    SM_DEBUG_SYNCPASS_ENCODE(spa_sync_pass(spa)) |
455	    SM_DEBUG_TXG_ENCODE(dmu_tx_get_txg(tx));
456
457	while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
458		size = ss->ss_end - ss->ss_start;
459		start = (ss->ss_start - sm->sm_start) >> sm->sm_shift;
460
461		sm->sm_space -= size;
462		size >>= sm->sm_shift;
463
464		while (size) {
465			run_len = MIN(size, SM_RUN_MAX);
466
467			if (entry == entry_map_end) {
468				mutex_exit(sm->sm_lock);
469				dmu_write(os, smo->smo_object, smo->smo_objsize,
470				    bufsize, entry_map, tx);
471				mutex_enter(sm->sm_lock);
472				smo->smo_objsize += bufsize;
473				entry = entry_map;
474			}
475
476			*entry++ = SM_OFFSET_ENCODE(start) |
477			    SM_TYPE_ENCODE(maptype) |
478			    SM_RUN_ENCODE(run_len);
479
480			start += run_len;
481			size -= run_len;
482		}
483		kmem_free(ss, sizeof (*ss));
484	}
485
486	if (entry != entry_map) {
487		size = (entry - entry_map) * sizeof (uint64_t);
488		mutex_exit(sm->sm_lock);
489		dmu_write(os, smo->smo_object, smo->smo_objsize,
490		    size, entry_map, tx);
491		mutex_enter(sm->sm_lock);
492		smo->smo_objsize += size;
493	}
494
495	zio_buf_free(entry_map, bufsize);
496
497	VERIFY3U(sm->sm_space, ==, 0);
498}
499
500void
501space_map_truncate(space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
502{
503	VERIFY(dmu_free_range(os, smo->smo_object, 0, -1ULL, tx) == 0);
504
505	smo->smo_objsize = 0;
506	smo->smo_alloc = 0;
507}
508