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