Add -p switch to "zpool get"
[zfs.git] / module / zfs / dsl_deadlist.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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012 by Delphix. All rights reserved.
24  */
25
26 #include <sys/dsl_dataset.h>
27 #include <sys/dmu.h>
28 #include <sys/refcount.h>
29 #include <sys/zap.h>
30 #include <sys/zfs_context.h>
31 #include <sys/dsl_pool.h>
32
33 /*
34  * Deadlist concurrency:
35  *
36  * Deadlists can only be modified from the syncing thread.
37  *
38  * Except for dsl_deadlist_insert(), it can only be modified with the
39  * dp_config_rwlock held with RW_WRITER.
40  *
41  * The accessors (dsl_deadlist_space() and dsl_deadlist_space_range()) can
42  * be called concurrently, from open context, with the dl_config_rwlock held
43  * with RW_READER.
44  *
45  * Therefore, we only need to provide locking between dsl_deadlist_insert() and
46  * the accessors, protecting:
47  *     dl_phys->dl_used,comp,uncomp
48  *     and protecting the dl_tree from being loaded.
49  * The locking is provided by dl_lock.  Note that locking on the bpobj_t
50  * provides its own locking, and dl_oldfmt is immutable.
51  */
52
53 static int
54 dsl_deadlist_compare(const void *arg1, const void *arg2)
55 {
56         const dsl_deadlist_entry_t *dle1 = arg1;
57         const dsl_deadlist_entry_t *dle2 = arg2;
58
59         if (dle1->dle_mintxg < dle2->dle_mintxg)
60                 return (-1);
61         else if (dle1->dle_mintxg > dle2->dle_mintxg)
62                 return (+1);
63         else
64                 return (0);
65 }
66
67 static void
68 dsl_deadlist_load_tree(dsl_deadlist_t *dl)
69 {
70         zap_cursor_t zc;
71         zap_attribute_t za;
72
73         ASSERT(!dl->dl_oldfmt);
74         if (dl->dl_havetree)
75                 return;
76
77         avl_create(&dl->dl_tree, dsl_deadlist_compare,
78             sizeof (dsl_deadlist_entry_t),
79             offsetof(dsl_deadlist_entry_t, dle_node));
80         for (zap_cursor_init(&zc, dl->dl_os, dl->dl_object);
81             zap_cursor_retrieve(&zc, &za) == 0;
82             zap_cursor_advance(&zc)) {
83                 dsl_deadlist_entry_t *dle;
84
85                 dle = kmem_alloc(sizeof (*dle), KM_PUSHPAGE);
86                 dle->dle_mintxg = strtonum(za.za_name, NULL);
87                 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os,
88                     za.za_first_integer));
89                 avl_add(&dl->dl_tree, dle);
90         }
91         zap_cursor_fini(&zc);
92         dl->dl_havetree = B_TRUE;
93 }
94
95 void
96 dsl_deadlist_open(dsl_deadlist_t *dl, objset_t *os, uint64_t object)
97 {
98         dmu_object_info_t doi;
99
100         mutex_init(&dl->dl_lock, NULL, MUTEX_DEFAULT, NULL);
101         dl->dl_os = os;
102         dl->dl_object = object;
103         VERIFY3U(0, ==, dmu_bonus_hold(os, object, dl, &dl->dl_dbuf));
104         dmu_object_info_from_db(dl->dl_dbuf, &doi);
105         if (doi.doi_type == DMU_OT_BPOBJ) {
106                 dmu_buf_rele(dl->dl_dbuf, dl);
107                 dl->dl_dbuf = NULL;
108                 dl->dl_oldfmt = B_TRUE;
109                 VERIFY3U(0, ==, bpobj_open(&dl->dl_bpobj, os, object));
110                 return;
111         }
112
113         dl->dl_oldfmt = B_FALSE;
114         dl->dl_phys = dl->dl_dbuf->db_data;
115         dl->dl_havetree = B_FALSE;
116 }
117
118 void
119 dsl_deadlist_close(dsl_deadlist_t *dl)
120 {
121         void *cookie = NULL;
122         dsl_deadlist_entry_t *dle;
123
124         if (dl->dl_oldfmt) {
125                 dl->dl_oldfmt = B_FALSE;
126                 bpobj_close(&dl->dl_bpobj);
127                 return;
128         }
129
130         if (dl->dl_havetree) {
131                 while ((dle = avl_destroy_nodes(&dl->dl_tree, &cookie))
132                     != NULL) {
133                         bpobj_close(&dle->dle_bpobj);
134                         kmem_free(dle, sizeof (*dle));
135                 }
136                 avl_destroy(&dl->dl_tree);
137         }
138         dmu_buf_rele(dl->dl_dbuf, dl);
139         mutex_destroy(&dl->dl_lock);
140         dl->dl_dbuf = NULL;
141         dl->dl_phys = NULL;
142 }
143
144 uint64_t
145 dsl_deadlist_alloc(objset_t *os, dmu_tx_t *tx)
146 {
147         if (spa_version(dmu_objset_spa(os)) < SPA_VERSION_DEADLISTS)
148                 return (bpobj_alloc(os, SPA_MAXBLOCKSIZE, tx));
149         return (zap_create(os, DMU_OT_DEADLIST, DMU_OT_DEADLIST_HDR,
150             sizeof (dsl_deadlist_phys_t), tx));
151 }
152
153 void
154 dsl_deadlist_free(objset_t *os, uint64_t dlobj, dmu_tx_t *tx)
155 {
156         dmu_object_info_t doi;
157         zap_cursor_t zc;
158         zap_attribute_t za;
159
160         VERIFY3U(0, ==, dmu_object_info(os, dlobj, &doi));
161         if (doi.doi_type == DMU_OT_BPOBJ) {
162                 bpobj_free(os, dlobj, tx);
163                 return;
164         }
165
166         for (zap_cursor_init(&zc, os, dlobj);
167             zap_cursor_retrieve(&zc, &za) == 0;
168             zap_cursor_advance(&zc)) {
169                 uint64_t obj = za.za_first_integer;
170                 if (obj == dmu_objset_pool(os)->dp_empty_bpobj)
171                         bpobj_decr_empty(os, tx);
172                 else
173                         bpobj_free(os, obj, tx);
174         }
175         zap_cursor_fini(&zc);
176         VERIFY3U(0, ==, dmu_object_free(os, dlobj, tx));
177 }
178
179 static void
180 dle_enqueue(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
181     const blkptr_t *bp, dmu_tx_t *tx)
182 {
183         if (dle->dle_bpobj.bpo_object ==
184             dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
185                 uint64_t obj = bpobj_alloc(dl->dl_os, SPA_MAXBLOCKSIZE, tx);
186                 bpobj_close(&dle->dle_bpobj);
187                 bpobj_decr_empty(dl->dl_os, tx);
188                 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
189                 VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object,
190                     dle->dle_mintxg, obj, tx));
191         }
192         bpobj_enqueue(&dle->dle_bpobj, bp, tx);
193 }
194
195 static void
196 dle_enqueue_subobj(dsl_deadlist_t *dl, dsl_deadlist_entry_t *dle,
197     uint64_t obj, dmu_tx_t *tx)
198 {
199         if (dle->dle_bpobj.bpo_object !=
200             dmu_objset_pool(dl->dl_os)->dp_empty_bpobj) {
201                 bpobj_enqueue_subobj(&dle->dle_bpobj, obj, tx);
202         } else {
203                 bpobj_close(&dle->dle_bpobj);
204                 bpobj_decr_empty(dl->dl_os, tx);
205                 VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
206                 VERIFY3U(0, ==, zap_update_int_key(dl->dl_os, dl->dl_object,
207                     dle->dle_mintxg, obj, tx));
208         }
209 }
210
211 void
212 dsl_deadlist_insert(dsl_deadlist_t *dl, const blkptr_t *bp, dmu_tx_t *tx)
213 {
214         dsl_deadlist_entry_t dle_tofind;
215         dsl_deadlist_entry_t *dle;
216         avl_index_t where;
217
218         if (dl->dl_oldfmt) {
219                 bpobj_enqueue(&dl->dl_bpobj, bp, tx);
220                 return;
221         }
222
223         dsl_deadlist_load_tree(dl);
224
225         dmu_buf_will_dirty(dl->dl_dbuf, tx);
226         mutex_enter(&dl->dl_lock);
227         dl->dl_phys->dl_used +=
228             bp_get_dsize_sync(dmu_objset_spa(dl->dl_os), bp);
229         dl->dl_phys->dl_comp += BP_GET_PSIZE(bp);
230         dl->dl_phys->dl_uncomp += BP_GET_UCSIZE(bp);
231         mutex_exit(&dl->dl_lock);
232
233         dle_tofind.dle_mintxg = bp->blk_birth;
234         dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
235         if (dle == NULL)
236                 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
237         else
238                 dle = AVL_PREV(&dl->dl_tree, dle);
239         dle_enqueue(dl, dle, bp, tx);
240 }
241
242 /*
243  * Insert new key in deadlist, which must be > all current entries.
244  * mintxg is not inclusive.
245  */
246 void
247 dsl_deadlist_add_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
248 {
249         uint64_t obj;
250         dsl_deadlist_entry_t *dle;
251
252         if (dl->dl_oldfmt)
253                 return;
254
255         dsl_deadlist_load_tree(dl);
256
257         dle = kmem_alloc(sizeof (*dle), KM_PUSHPAGE);
258         dle->dle_mintxg = mintxg;
259         obj = bpobj_alloc_empty(dl->dl_os, SPA_MAXBLOCKSIZE, tx);
260         VERIFY3U(0, ==, bpobj_open(&dle->dle_bpobj, dl->dl_os, obj));
261         avl_add(&dl->dl_tree, dle);
262
263         VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, dl->dl_object,
264             mintxg, obj, tx));
265 }
266
267 /*
268  * Remove this key, merging its entries into the previous key.
269  */
270 void
271 dsl_deadlist_remove_key(dsl_deadlist_t *dl, uint64_t mintxg, dmu_tx_t *tx)
272 {
273         dsl_deadlist_entry_t dle_tofind;
274         dsl_deadlist_entry_t *dle, *dle_prev;
275
276         if (dl->dl_oldfmt)
277                 return;
278
279         dsl_deadlist_load_tree(dl);
280
281         dle_tofind.dle_mintxg = mintxg;
282         dle = avl_find(&dl->dl_tree, &dle_tofind, NULL);
283         dle_prev = AVL_PREV(&dl->dl_tree, dle);
284
285         dle_enqueue_subobj(dl, dle_prev, dle->dle_bpobj.bpo_object, tx);
286
287         avl_remove(&dl->dl_tree, dle);
288         bpobj_close(&dle->dle_bpobj);
289         kmem_free(dle, sizeof (*dle));
290
291         VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object, mintxg, tx));
292 }
293
294 /*
295  * Walk ds's snapshots to regenerate generate ZAP & AVL.
296  */
297 static void
298 dsl_deadlist_regenerate(objset_t *os, uint64_t dlobj,
299     uint64_t mrs_obj, dmu_tx_t *tx)
300 {
301         dsl_deadlist_t dl;
302         dsl_pool_t *dp = dmu_objset_pool(os);
303
304         dsl_deadlist_open(&dl, os, dlobj);
305         if (dl.dl_oldfmt) {
306                 dsl_deadlist_close(&dl);
307                 return;
308         }
309
310         while (mrs_obj != 0) {
311                 dsl_dataset_t *ds;
312                 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, mrs_obj, FTAG, &ds));
313                 dsl_deadlist_add_key(&dl, ds->ds_phys->ds_prev_snap_txg, tx);
314                 mrs_obj = ds->ds_phys->ds_prev_snap_obj;
315                 dsl_dataset_rele(ds, FTAG);
316         }
317         dsl_deadlist_close(&dl);
318 }
319
320 uint64_t
321 dsl_deadlist_clone(dsl_deadlist_t *dl, uint64_t maxtxg,
322     uint64_t mrs_obj, dmu_tx_t *tx)
323 {
324         dsl_deadlist_entry_t *dle;
325         uint64_t newobj;
326
327         newobj = dsl_deadlist_alloc(dl->dl_os, tx);
328
329         if (dl->dl_oldfmt) {
330                 dsl_deadlist_regenerate(dl->dl_os, newobj, mrs_obj, tx);
331                 return (newobj);
332         }
333
334         dsl_deadlist_load_tree(dl);
335
336         for (dle = avl_first(&dl->dl_tree); dle;
337             dle = AVL_NEXT(&dl->dl_tree, dle)) {
338                 uint64_t obj;
339
340                 if (dle->dle_mintxg >= maxtxg)
341                         break;
342
343                 obj = bpobj_alloc_empty(dl->dl_os, SPA_MAXBLOCKSIZE, tx);
344                 VERIFY3U(0, ==, zap_add_int_key(dl->dl_os, newobj,
345                     dle->dle_mintxg, obj, tx));
346         }
347         return (newobj);
348 }
349
350 void
351 dsl_deadlist_space(dsl_deadlist_t *dl,
352     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
353 {
354         if (dl->dl_oldfmt) {
355                 VERIFY3U(0, ==, bpobj_space(&dl->dl_bpobj,
356                     usedp, compp, uncompp));
357                 return;
358         }
359
360         mutex_enter(&dl->dl_lock);
361         *usedp = dl->dl_phys->dl_used;
362         *compp = dl->dl_phys->dl_comp;
363         *uncompp = dl->dl_phys->dl_uncomp;
364         mutex_exit(&dl->dl_lock);
365 }
366
367 /*
368  * return space used in the range (mintxg, maxtxg].
369  * Includes maxtxg, does not include mintxg.
370  * mintxg and maxtxg must both be keys in the deadlist (unless maxtxg is
371  * larger than any bp in the deadlist (eg. UINT64_MAX)).
372  */
373 void
374 dsl_deadlist_space_range(dsl_deadlist_t *dl, uint64_t mintxg, uint64_t maxtxg,
375     uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
376 {
377         dsl_deadlist_entry_t *dle;
378         dsl_deadlist_entry_t dle_tofind;
379         avl_index_t where;
380
381         if (dl->dl_oldfmt) {
382                 VERIFY3U(0, ==, bpobj_space_range(&dl->dl_bpobj,
383                     mintxg, maxtxg, usedp, compp, uncompp));
384                 return;
385         }
386
387         *usedp = *compp = *uncompp = 0;
388
389         mutex_enter(&dl->dl_lock);
390         dsl_deadlist_load_tree(dl);
391         dle_tofind.dle_mintxg = mintxg;
392         dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
393         /*
394          * If we don't find this mintxg, there shouldn't be anything
395          * after it either.
396          */
397         ASSERT(dle != NULL ||
398             avl_nearest(&dl->dl_tree, where, AVL_AFTER) == NULL);
399
400         for (; dle && dle->dle_mintxg < maxtxg;
401             dle = AVL_NEXT(&dl->dl_tree, dle)) {
402                 uint64_t used, comp, uncomp;
403
404                 VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
405                     &used, &comp, &uncomp));
406
407                 *usedp += used;
408                 *compp += comp;
409                 *uncompp += uncomp;
410         }
411         mutex_exit(&dl->dl_lock);
412 }
413
414 static void
415 dsl_deadlist_insert_bpobj(dsl_deadlist_t *dl, uint64_t obj, uint64_t birth,
416     dmu_tx_t *tx)
417 {
418         dsl_deadlist_entry_t dle_tofind;
419         dsl_deadlist_entry_t *dle;
420         avl_index_t where;
421         uint64_t used, comp, uncomp;
422         bpobj_t bpo;
423
424         VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
425         VERIFY3U(0, ==, bpobj_space(&bpo, &used, &comp, &uncomp));
426         bpobj_close(&bpo);
427
428         dsl_deadlist_load_tree(dl);
429
430         dmu_buf_will_dirty(dl->dl_dbuf, tx);
431         mutex_enter(&dl->dl_lock);
432         dl->dl_phys->dl_used += used;
433         dl->dl_phys->dl_comp += comp;
434         dl->dl_phys->dl_uncomp += uncomp;
435         mutex_exit(&dl->dl_lock);
436
437         dle_tofind.dle_mintxg = birth;
438         dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
439         if (dle == NULL)
440                 dle = avl_nearest(&dl->dl_tree, where, AVL_BEFORE);
441         dle_enqueue_subobj(dl, dle, obj, tx);
442 }
443
444 static int
445 dsl_deadlist_insert_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
446 {
447         dsl_deadlist_t *dl = arg;
448         dsl_deadlist_insert(dl, bp, tx);
449         return (0);
450 }
451
452 /*
453  * Merge the deadlist pointed to by 'obj' into dl.  obj will be left as
454  * an empty deadlist.
455  */
456 void
457 dsl_deadlist_merge(dsl_deadlist_t *dl, uint64_t obj, dmu_tx_t *tx)
458 {
459         zap_cursor_t zc;
460         zap_attribute_t za;
461         dmu_buf_t *bonus;
462         dsl_deadlist_phys_t *dlp;
463         dmu_object_info_t doi;
464
465         VERIFY3U(0, ==, dmu_object_info(dl->dl_os, obj, &doi));
466         if (doi.doi_type == DMU_OT_BPOBJ) {
467                 bpobj_t bpo;
468                 VERIFY3U(0, ==, bpobj_open(&bpo, dl->dl_os, obj));
469                 VERIFY3U(0, ==, bpobj_iterate(&bpo,
470                     dsl_deadlist_insert_cb, dl, tx));
471                 bpobj_close(&bpo);
472                 return;
473         }
474
475         for (zap_cursor_init(&zc, dl->dl_os, obj);
476             zap_cursor_retrieve(&zc, &za) == 0;
477             zap_cursor_advance(&zc)) {
478                 uint64_t mintxg = strtonum(za.za_name, NULL);
479                 dsl_deadlist_insert_bpobj(dl, za.za_first_integer, mintxg, tx);
480                 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, obj, mintxg, tx));
481         }
482         zap_cursor_fini(&zc);
483
484         VERIFY3U(0, ==, dmu_bonus_hold(dl->dl_os, obj, FTAG, &bonus));
485         dlp = bonus->db_data;
486         dmu_buf_will_dirty(bonus, tx);
487         bzero(dlp, sizeof (*dlp));
488         dmu_buf_rele(bonus, FTAG);
489 }
490
491 /*
492  * Remove entries on dl that are >= mintxg, and put them on the bpobj.
493  */
494 void
495 dsl_deadlist_move_bpobj(dsl_deadlist_t *dl, bpobj_t *bpo, uint64_t mintxg,
496     dmu_tx_t *tx)
497 {
498         dsl_deadlist_entry_t dle_tofind;
499         dsl_deadlist_entry_t *dle;
500         avl_index_t where;
501
502         ASSERT(!dl->dl_oldfmt);
503         dmu_buf_will_dirty(dl->dl_dbuf, tx);
504         dsl_deadlist_load_tree(dl);
505
506         dle_tofind.dle_mintxg = mintxg;
507         dle = avl_find(&dl->dl_tree, &dle_tofind, &where);
508         if (dle == NULL)
509                 dle = avl_nearest(&dl->dl_tree, where, AVL_AFTER);
510         while (dle) {
511                 uint64_t used, comp, uncomp;
512                 dsl_deadlist_entry_t *dle_next;
513
514                 bpobj_enqueue_subobj(bpo, dle->dle_bpobj.bpo_object, tx);
515
516                 VERIFY3U(0, ==, bpobj_space(&dle->dle_bpobj,
517                     &used, &comp, &uncomp));
518                 mutex_enter(&dl->dl_lock);
519                 ASSERT3U(dl->dl_phys->dl_used, >=, used);
520                 ASSERT3U(dl->dl_phys->dl_comp, >=, comp);
521                 ASSERT3U(dl->dl_phys->dl_uncomp, >=, uncomp);
522                 dl->dl_phys->dl_used -= used;
523                 dl->dl_phys->dl_comp -= comp;
524                 dl->dl_phys->dl_uncomp -= uncomp;
525                 mutex_exit(&dl->dl_lock);
526
527                 VERIFY3U(0, ==, zap_remove_int(dl->dl_os, dl->dl_object,
528                     dle->dle_mintxg, tx));
529
530                 dle_next = AVL_NEXT(&dl->dl_tree, dle);
531                 avl_remove(&dl->dl_tree, dle);
532                 bpobj_close(&dle->dle_bpobj);
533                 kmem_free(dle, sizeof (*dle));
534                 dle = dle_next;
535         }
536 }