]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/space_map.c
MFC recent ZFS changes from illumos:
[FreeBSD/stable/8.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 ssearch, *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(size != 0);
111         VERIFY3U(start, >=, sm->sm_start);
112         VERIFY3U(end, <=, sm->sm_start + sm->sm_size);
113         VERIFY(sm->sm_space + size <= sm->sm_size);
114         VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
115         VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
116
117         ssearch.ss_start = start;
118         ssearch.ss_end = end;
119         ss = avl_find(&sm->sm_root, &ssearch, &where);
120
121         if (ss != NULL && ss->ss_start <= start && ss->ss_end >= end) {
122                 zfs_panic_recover("zfs: allocating allocated segment"
123                     "(offset=%llu size=%llu)\n",
124                     (longlong_t)start, (longlong_t)size);
125                 return;
126         }
127
128         /* Make sure we don't overlap with either of our neighbors */
129         VERIFY(ss == NULL);
130
131         ss_before = avl_nearest(&sm->sm_root, where, AVL_BEFORE);
132         ss_after = avl_nearest(&sm->sm_root, where, AVL_AFTER);
133
134         merge_before = (ss_before != NULL && ss_before->ss_end == start);
135         merge_after = (ss_after != NULL && ss_after->ss_start == end);
136
137         if (merge_before && merge_after) {
138                 avl_remove(&sm->sm_root, ss_before);
139                 if (sm->sm_pp_root) {
140                         avl_remove(sm->sm_pp_root, ss_before);
141                         avl_remove(sm->sm_pp_root, ss_after);
142                 }
143                 ss_after->ss_start = ss_before->ss_start;
144                 kmem_cache_free(space_seg_cache, ss_before);
145                 ss = ss_after;
146         } else if (merge_before) {
147                 ss_before->ss_end = end;
148                 if (sm->sm_pp_root)
149                         avl_remove(sm->sm_pp_root, ss_before);
150                 ss = ss_before;
151         } else if (merge_after) {
152                 ss_after->ss_start = start;
153                 if (sm->sm_pp_root)
154                         avl_remove(sm->sm_pp_root, ss_after);
155                 ss = ss_after;
156         } else {
157                 ss = kmem_cache_alloc(space_seg_cache, KM_SLEEP);
158                 ss->ss_start = start;
159                 ss->ss_end = end;
160                 avl_insert(&sm->sm_root, ss, where);
161         }
162
163         if (sm->sm_pp_root)
164                 avl_add(sm->sm_pp_root, ss);
165
166         sm->sm_space += size;
167 }
168
169 void
170 space_map_remove(space_map_t *sm, uint64_t start, uint64_t size)
171 {
172         space_seg_t ssearch, *ss, *newseg;
173         uint64_t end = start + size;
174         int left_over, right_over;
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, NULL);
184
185         /* Make sure we completely overlap with someone */
186         if (ss == NULL) {
187                 zfs_panic_recover("zfs: freeing free segment "
188                     "(offset=%llu size=%llu)",
189                     (longlong_t)start, (longlong_t)size);
190                 return;
191         }
192         VERIFY3U(ss->ss_start, <=, start);
193         VERIFY3U(ss->ss_end, >=, end);
194         VERIFY(sm->sm_space - size < sm->sm_size);
195
196         left_over = (ss->ss_start != start);
197         right_over = (ss->ss_end != end);
198
199         if (sm->sm_pp_root)
200                 avl_remove(sm->sm_pp_root, ss);
201
202         if (left_over && right_over) {
203                 newseg = kmem_cache_alloc(space_seg_cache, KM_SLEEP);
204                 newseg->ss_start = end;
205                 newseg->ss_end = ss->ss_end;
206                 ss->ss_end = start;
207                 avl_insert_here(&sm->sm_root, newseg, ss, AVL_AFTER);
208                 if (sm->sm_pp_root)
209                         avl_add(sm->sm_pp_root, newseg);
210         } else if (left_over) {
211                 ss->ss_end = start;
212         } else if (right_over) {
213                 ss->ss_start = end;
214         } else {
215                 avl_remove(&sm->sm_root, ss);
216                 kmem_cache_free(space_seg_cache, ss);
217                 ss = NULL;
218         }
219
220         if (sm->sm_pp_root && ss != NULL)
221                 avl_add(sm->sm_pp_root, ss);
222
223         sm->sm_space -= size;
224 }
225
226 boolean_t
227 space_map_contains(space_map_t *sm, uint64_t start, uint64_t size)
228 {
229         avl_index_t where;
230         space_seg_t ssearch, *ss;
231         uint64_t end = start + size;
232
233         ASSERT(MUTEX_HELD(sm->sm_lock));
234         VERIFY(size != 0);
235         VERIFY(P2PHASE(start, 1ULL << sm->sm_shift) == 0);
236         VERIFY(P2PHASE(size, 1ULL << sm->sm_shift) == 0);
237
238         ssearch.ss_start = start;
239         ssearch.ss_end = end;
240         ss = avl_find(&sm->sm_root, &ssearch, &where);
241
242         return (ss != NULL && ss->ss_start <= start && ss->ss_end >= end);
243 }
244
245 void
246 space_map_vacate(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
247 {
248         space_seg_t *ss;
249         void *cookie = NULL;
250
251         ASSERT(MUTEX_HELD(sm->sm_lock));
252
253         while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
254                 if (func != NULL)
255                         func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
256                 kmem_cache_free(space_seg_cache, ss);
257         }
258         sm->sm_space = 0;
259 }
260
261 void
262 space_map_walk(space_map_t *sm, space_map_func_t *func, space_map_t *mdest)
263 {
264         space_seg_t *ss;
265
266         ASSERT(MUTEX_HELD(sm->sm_lock));
267
268         for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
269                 func(mdest, ss->ss_start, ss->ss_end - ss->ss_start);
270 }
271
272 /*
273  * Wait for any in-progress space_map_load() to complete.
274  */
275 void
276 space_map_load_wait(space_map_t *sm)
277 {
278         ASSERT(MUTEX_HELD(sm->sm_lock));
279
280         while (sm->sm_loading) {
281                 ASSERT(!sm->sm_loaded);
282                 cv_wait(&sm->sm_load_cv, sm->sm_lock);
283         }
284 }
285
286 /*
287  * Note: space_map_load() will drop sm_lock across dmu_read() calls.
288  * The caller must be OK with this.
289  */
290 int
291 space_map_load(space_map_t *sm, space_map_ops_t *ops, uint8_t maptype,
292         space_map_obj_t *smo, objset_t *os)
293 {
294         uint64_t *entry, *entry_map, *entry_map_end;
295         uint64_t bufsize, size, offset, end, space;
296         uint64_t mapstart = sm->sm_start;
297         int error = 0;
298
299         ASSERT(MUTEX_HELD(sm->sm_lock));
300         ASSERT(!sm->sm_loaded);
301         ASSERT(!sm->sm_loading);
302
303         sm->sm_loading = B_TRUE;
304         end = smo->smo_objsize;
305         space = smo->smo_alloc;
306
307         ASSERT(sm->sm_ops == NULL);
308         VERIFY0(sm->sm_space);
309
310         if (maptype == SM_FREE) {
311                 space_map_add(sm, sm->sm_start, sm->sm_size);
312                 space = sm->sm_size - space;
313         }
314
315         bufsize = 1ULL << SPACE_MAP_BLOCKSHIFT;
316         entry_map = zio_buf_alloc(bufsize);
317
318         mutex_exit(sm->sm_lock);
319         if (end > bufsize)
320                 dmu_prefetch(os, smo->smo_object, bufsize, end - bufsize);
321         mutex_enter(sm->sm_lock);
322
323         for (offset = 0; offset < end; offset += bufsize) {
324                 size = MIN(end - offset, bufsize);
325                 VERIFY(P2PHASE(size, sizeof (uint64_t)) == 0);
326                 VERIFY(size != 0);
327
328                 dprintf("object=%llu  offset=%llx  size=%llx\n",
329                     smo->smo_object, offset, size);
330
331                 mutex_exit(sm->sm_lock);
332                 error = dmu_read(os, smo->smo_object, offset, size, entry_map,
333                     DMU_READ_PREFETCH);
334                 mutex_enter(sm->sm_lock);
335                 if (error != 0)
336                         break;
337
338                 entry_map_end = entry_map + (size / sizeof (uint64_t));
339                 for (entry = entry_map; entry < entry_map_end; entry++) {
340                         uint64_t e = *entry;
341
342                         if (SM_DEBUG_DECODE(e))         /* Skip debug entries */
343                                 continue;
344
345                         (SM_TYPE_DECODE(e) == maptype ?
346                             space_map_add : space_map_remove)(sm,
347                             (SM_OFFSET_DECODE(e) << sm->sm_shift) + mapstart,
348                             SM_RUN_DECODE(e) << sm->sm_shift);
349                 }
350         }
351
352         if (error == 0) {
353                 VERIFY3U(sm->sm_space, ==, space);
354
355                 sm->sm_loaded = B_TRUE;
356                 sm->sm_ops = ops;
357                 if (ops != NULL)
358                         ops->smop_load(sm);
359         } else {
360                 space_map_vacate(sm, NULL, NULL);
361         }
362
363         zio_buf_free(entry_map, bufsize);
364
365         sm->sm_loading = B_FALSE;
366
367         cv_broadcast(&sm->sm_load_cv);
368
369         return (error);
370 }
371
372 void
373 space_map_unload(space_map_t *sm)
374 {
375         ASSERT(MUTEX_HELD(sm->sm_lock));
376
377         if (sm->sm_loaded && sm->sm_ops != NULL)
378                 sm->sm_ops->smop_unload(sm);
379
380         sm->sm_loaded = B_FALSE;
381         sm->sm_ops = NULL;
382
383         space_map_vacate(sm, NULL, NULL);
384 }
385
386 uint64_t
387 space_map_maxsize(space_map_t *sm)
388 {
389         ASSERT(sm->sm_ops != NULL);
390         return (sm->sm_ops->smop_max(sm));
391 }
392
393 uint64_t
394 space_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
404 void
405 space_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
411 void
412 space_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  */
421 void
422 space_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, delta, sm_space;
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         delta = 0;
458         sm_space = sm->sm_space;
459         while ((ss = avl_destroy_nodes(&sm->sm_root, &cookie)) != NULL) {
460                 size = ss->ss_end - ss->ss_start;
461                 start = (ss->ss_start - sm->sm_start) >> sm->sm_shift;
462
463                 delta += size;
464                 size >>= sm->sm_shift;
465
466                 while (size) {
467                         run_len = MIN(size, SM_RUN_MAX);
468
469                         if (entry == entry_map_end) {
470                                 mutex_exit(sm->sm_lock);
471                                 dmu_write(os, smo->smo_object, smo->smo_objsize,
472                                     bufsize, entry_map, tx);
473                                 mutex_enter(sm->sm_lock);
474                                 smo->smo_objsize += bufsize;
475                                 entry = entry_map;
476                         }
477
478                         *entry++ = SM_OFFSET_ENCODE(start) |
479                             SM_TYPE_ENCODE(maptype) |
480                             SM_RUN_ENCODE(run_len);
481
482                         start += run_len;
483                         size -= run_len;
484                 }
485                 kmem_cache_free(space_seg_cache, ss);
486         }
487
488         if (entry != entry_map) {
489                 size = (entry - entry_map) * sizeof (uint64_t);
490                 mutex_exit(sm->sm_lock);
491                 dmu_write(os, smo->smo_object, smo->smo_objsize,
492                     size, entry_map, tx);
493                 mutex_enter(sm->sm_lock);
494                 smo->smo_objsize += size;
495         }
496
497         /*
498          * Ensure that the space_map's accounting wasn't changed
499          * while we were in the middle of writing it out.
500          */
501         VERIFY3U(sm->sm_space, ==, sm_space);
502
503         zio_buf_free(entry_map, bufsize);
504
505         sm->sm_space -= delta;
506         VERIFY0(sm->sm_space);
507 }
508
509 void
510 space_map_truncate(space_map_obj_t *smo, objset_t *os, dmu_tx_t *tx)
511 {
512         VERIFY(dmu_free_range(os, smo->smo_object, 0, -1ULL, tx) == 0);
513
514         smo->smo_objsize = 0;
515         smo->smo_alloc = 0;
516 }
517
518 /*
519  * Space map reference trees.
520  *
521  * A space map is a collection of integers.  Every integer is either
522  * in the map, or it's not.  A space map reference tree generalizes
523  * the idea: it allows its members to have arbitrary reference counts,
524  * as opposed to the implicit reference count of 0 or 1 in a space map.
525  * This representation comes in handy when computing the union or
526  * intersection of multiple space maps.  For example, the union of
527  * N space maps is the subset of the reference tree with refcnt >= 1.
528  * The intersection of N space maps is the subset with refcnt >= N.
529  *
530  * [It's very much like a Fourier transform.  Unions and intersections
531  * are hard to perform in the 'space map domain', so we convert the maps
532  * into the 'reference count domain', where it's trivial, then invert.]
533  *
534  * vdev_dtl_reassess() uses computations of this form to determine
535  * DTL_MISSING and DTL_OUTAGE for interior vdevs -- e.g. a RAID-Z vdev
536  * has an outage wherever refcnt >= vdev_nparity + 1, and a mirror vdev
537  * has an outage wherever refcnt >= vdev_children.
538  */
539 static int
540 space_map_ref_compare(const void *x1, const void *x2)
541 {
542         const space_ref_t *sr1 = x1;
543         const space_ref_t *sr2 = x2;
544
545         if (sr1->sr_offset < sr2->sr_offset)
546                 return (-1);
547         if (sr1->sr_offset > sr2->sr_offset)
548                 return (1);
549
550         if (sr1 < sr2)
551                 return (-1);
552         if (sr1 > sr2)
553                 return (1);
554
555         return (0);
556 }
557
558 void
559 space_map_ref_create(avl_tree_t *t)
560 {
561         avl_create(t, space_map_ref_compare,
562             sizeof (space_ref_t), offsetof(space_ref_t, sr_node));
563 }
564
565 void
566 space_map_ref_destroy(avl_tree_t *t)
567 {
568         space_ref_t *sr;
569         void *cookie = NULL;
570
571         while ((sr = avl_destroy_nodes(t, &cookie)) != NULL)
572                 kmem_free(sr, sizeof (*sr));
573
574         avl_destroy(t);
575 }
576
577 static void
578 space_map_ref_add_node(avl_tree_t *t, uint64_t offset, int64_t refcnt)
579 {
580         space_ref_t *sr;
581
582         sr = kmem_alloc(sizeof (*sr), KM_SLEEP);
583         sr->sr_offset = offset;
584         sr->sr_refcnt = refcnt;
585
586         avl_add(t, sr);
587 }
588
589 void
590 space_map_ref_add_seg(avl_tree_t *t, uint64_t start, uint64_t end,
591         int64_t refcnt)
592 {
593         space_map_ref_add_node(t, start, refcnt);
594         space_map_ref_add_node(t, end, -refcnt);
595 }
596
597 /*
598  * Convert (or add) a space map into a reference tree.
599  */
600 void
601 space_map_ref_add_map(avl_tree_t *t, space_map_t *sm, int64_t refcnt)
602 {
603         space_seg_t *ss;
604
605         ASSERT(MUTEX_HELD(sm->sm_lock));
606
607         for (ss = avl_first(&sm->sm_root); ss; ss = AVL_NEXT(&sm->sm_root, ss))
608                 space_map_ref_add_seg(t, ss->ss_start, ss->ss_end, refcnt);
609 }
610
611 /*
612  * Convert a reference tree into a space map.  The space map will contain
613  * all members of the reference tree for which refcnt >= minref.
614  */
615 void
616 space_map_ref_generate_map(avl_tree_t *t, space_map_t *sm, int64_t minref)
617 {
618         uint64_t start = -1ULL;
619         int64_t refcnt = 0;
620         space_ref_t *sr;
621
622         ASSERT(MUTEX_HELD(sm->sm_lock));
623
624         space_map_vacate(sm, NULL, NULL);
625
626         for (sr = avl_first(t); sr != NULL; sr = AVL_NEXT(t, sr)) {
627                 refcnt += sr->sr_refcnt;
628                 if (refcnt >= minref) {
629                         if (start == -1ULL) {
630                                 start = sr->sr_offset;
631                         }
632                 } else {
633                         if (start != -1ULL) {
634                                 uint64_t end = sr->sr_offset;
635                                 ASSERT(start <= end);
636                                 if (end > start)
637                                         space_map_add(sm, start, end - start);
638                                 start = -1ULL;
639                         }
640                 }
641         }
642         ASSERT(refcnt == 0);
643         ASSERT(start == -1ULL);
644 }