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