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