Deleted Added
sdiff udiff text old ( 208372 ) new ( 209962 )
full compact
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

--- 5 unchanged lines hidden (view full) ---

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>

--- 80 unchanged lines hidden (view full) ---

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;

--- 18 unchanged lines hidden (view full) ---

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);

--- 23 unchanged lines hidden (view full) ---

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)

--- 44 unchanged lines hidden (view full) ---

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

--- 37 unchanged lines hidden (view full) ---

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);

--- 98 unchanged lines hidden (view full) ---

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}