1236884Smm/*
2236884Smm * CDDL HEADER START
3236884Smm *
4236884Smm * The contents of this file are subject to the terms of the
5236884Smm * Common Development and Distribution License (the "License").
6236884Smm * You may not use this file except in compliance with the License.
7236884Smm *
8236884Smm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9236884Smm * or http://www.opensolaris.org/os/licensing.
10236884Smm * See the License for the specific language governing permissions
11236884Smm * and limitations under the License.
12236884Smm *
13236884Smm * When distributing Covered Code, include this CDDL HEADER in each
14236884Smm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15236884Smm * If applicable, add the following below this CDDL HEADER, with the
16236884Smm * fields enclosed by brackets "[]" replaced with your own identifying
17236884Smm * information: Portions Copyright [yyyy] [name of copyright owner]
18236884Smm *
19236884Smm * CDDL HEADER END
20236884Smm */
21236884Smm
22236884Smm/*
23268657Sdelphij * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
24236884Smm */
25236884Smm
26236884Smm#include <sys/arc.h>
27236884Smm#include <sys/bptree.h>
28236884Smm#include <sys/dmu.h>
29236884Smm#include <sys/dmu_objset.h>
30236884Smm#include <sys/dmu_tx.h>
31236884Smm#include <sys/dmu_traverse.h>
32236884Smm#include <sys/dsl_dataset.h>
33236884Smm#include <sys/dsl_dir.h>
34236884Smm#include <sys/dsl_pool.h>
35236884Smm#include <sys/dnode.h>
36236884Smm#include <sys/refcount.h>
37236884Smm#include <sys/spa.h>
38236884Smm
39236884Smm/*
40236884Smm * A bptree is a queue of root block pointers from destroyed datasets. When a
41236884Smm * dataset is destroyed its root block pointer is put on the end of the pool's
42236884Smm * bptree queue so the dataset's blocks can be freed asynchronously by
43236884Smm * dsl_scan_sync. This allows the delete operation to finish without traversing
44236884Smm * all the dataset's blocks.
45236884Smm *
46251631Sdelphij * Note that while bt_begin and bt_end are only ever incremented in this code,
47236884Smm * they are effectively reset to 0 every time the entire bptree is freed because
48236884Smm * the bptree's object is destroyed and re-created.
49236884Smm */
50236884Smm
51236884Smmstruct bptree_args {
52236884Smm	bptree_phys_t *ba_phys;	/* data in bonus buffer, dirtied if freeing */
53236884Smm	boolean_t ba_free;	/* true if freeing during traversal */
54236884Smm
55236884Smm	bptree_itor_t *ba_func;	/* function to call for each blockpointer */
56236884Smm	void *ba_arg;		/* caller supplied argument to ba_func */
57236884Smm	dmu_tx_t *ba_tx;	/* caller supplied tx, NULL if not freeing */
58236884Smm} bptree_args_t;
59236884Smm
60236884Smmuint64_t
61236884Smmbptree_alloc(objset_t *os, dmu_tx_t *tx)
62236884Smm{
63236884Smm	uint64_t obj;
64236884Smm	dmu_buf_t *db;
65236884Smm	bptree_phys_t *bt;
66236884Smm
67236884Smm	obj = dmu_object_alloc(os, DMU_OTN_UINT64_METADATA,
68236884Smm	    SPA_MAXBLOCKSIZE, DMU_OTN_UINT64_METADATA,
69236884Smm	    sizeof (bptree_phys_t), tx);
70236884Smm
71236884Smm	/*
72236884Smm	 * Bonus buffer contents are already initialized to 0, but for
73236884Smm	 * readability we make it explicit.
74236884Smm	 */
75236884Smm	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
76236884Smm	dmu_buf_will_dirty(db, tx);
77236884Smm	bt = db->db_data;
78236884Smm	bt->bt_begin = 0;
79236884Smm	bt->bt_end = 0;
80236884Smm	bt->bt_bytes = 0;
81236884Smm	bt->bt_comp = 0;
82236884Smm	bt->bt_uncomp = 0;
83236884Smm	dmu_buf_rele(db, FTAG);
84236884Smm
85236884Smm	return (obj);
86236884Smm}
87236884Smm
88236884Smmint
89236884Smmbptree_free(objset_t *os, uint64_t obj, dmu_tx_t *tx)
90236884Smm{
91236884Smm	dmu_buf_t *db;
92236884Smm	bptree_phys_t *bt;
93236884Smm
94236884Smm	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
95236884Smm	bt = db->db_data;
96236884Smm	ASSERT3U(bt->bt_begin, ==, bt->bt_end);
97240415Smm	ASSERT0(bt->bt_bytes);
98240415Smm	ASSERT0(bt->bt_comp);
99240415Smm	ASSERT0(bt->bt_uncomp);
100236884Smm	dmu_buf_rele(db, FTAG);
101236884Smm
102236884Smm	return (dmu_object_free(os, obj, tx));
103236884Smm}
104236884Smm
105268650Sdelphijboolean_t
106268650Sdelphijbptree_is_empty(objset_t *os, uint64_t obj)
107268650Sdelphij{
108268650Sdelphij	dmu_buf_t *db;
109268650Sdelphij	bptree_phys_t *bt;
110268650Sdelphij	boolean_t rv;
111268650Sdelphij
112268650Sdelphij	VERIFY0(dmu_bonus_hold(os, obj, FTAG, &db));
113268650Sdelphij	bt = db->db_data;
114268650Sdelphij	rv = (bt->bt_begin == bt->bt_end);
115268650Sdelphij	dmu_buf_rele(db, FTAG);
116268650Sdelphij	return (rv);
117268650Sdelphij}
118268650Sdelphij
119236884Smmvoid
120236884Smmbptree_add(objset_t *os, uint64_t obj, blkptr_t *bp, uint64_t birth_txg,
121236884Smm    uint64_t bytes, uint64_t comp, uint64_t uncomp, dmu_tx_t *tx)
122236884Smm{
123236884Smm	dmu_buf_t *db;
124236884Smm	bptree_phys_t *bt;
125268650Sdelphij	bptree_entry_phys_t bte = { 0 };
126236884Smm
127236884Smm	/*
128236884Smm	 * bptree objects are in the pool mos, therefore they can only be
129236884Smm	 * modified in syncing context. Furthermore, this is only modified
130236884Smm	 * by the sync thread, so no locking is necessary.
131236884Smm	 */
132236884Smm	ASSERT(dmu_tx_is_syncing(tx));
133236884Smm
134236884Smm	VERIFY3U(0, ==, dmu_bonus_hold(os, obj, FTAG, &db));
135236884Smm	bt = db->db_data;
136236884Smm
137236884Smm	bte.be_birth_txg = birth_txg;
138236884Smm	bte.be_bp = *bp;
139236884Smm	dmu_write(os, obj, bt->bt_end * sizeof (bte), sizeof (bte), &bte, tx);
140236884Smm
141236884Smm	dmu_buf_will_dirty(db, tx);
142236884Smm	bt->bt_end++;
143236884Smm	bt->bt_bytes += bytes;
144236884Smm	bt->bt_comp += comp;
145236884Smm	bt->bt_uncomp += uncomp;
146236884Smm	dmu_buf_rele(db, FTAG);
147236884Smm}
148236884Smm
149236884Smm/* ARGSUSED */
150236884Smmstatic int
151246666Smmbptree_visit_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
152268657Sdelphij    const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
153236884Smm{
154236884Smm	int err;
155236884Smm	struct bptree_args *ba = arg;
156236884Smm
157263397Sdelphij	if (BP_IS_HOLE(bp))
158236884Smm		return (0);
159236884Smm
160236884Smm	err = ba->ba_func(ba->ba_arg, bp, ba->ba_tx);
161236884Smm	if (err == 0 && ba->ba_free) {
162236884Smm		ba->ba_phys->bt_bytes -= bp_get_dsize_sync(spa, bp);
163236884Smm		ba->ba_phys->bt_comp -= BP_GET_PSIZE(bp);
164236884Smm		ba->ba_phys->bt_uncomp -= BP_GET_UCSIZE(bp);
165236884Smm	}
166236884Smm	return (err);
167236884Smm}
168236884Smm
169268650Sdelphij/*
170268650Sdelphij * If "free" is set:
171268650Sdelphij *  - It is assumed that "func" will be freeing the block pointers.
172268650Sdelphij *  - If "func" returns nonzero, the bookmark will be remembered and
173268650Sdelphij *    iteration will be restarted from this point on next invocation.
174268650Sdelphij *  - If an i/o error is encountered (e.g. "func" returns EIO or ECKSUM),
175268650Sdelphij *    bptree_iterate will remember the bookmark, continue traversing
176268650Sdelphij *    any additional entries, and return 0.
177268650Sdelphij *
178268650Sdelphij * If "free" is not set, traversal will stop and return an error if
179268650Sdelphij * an i/o error is encountered.
180268650Sdelphij *
181268650Sdelphij * In either case, if zfs_free_leak_on_eio is set, i/o errors will be
182268650Sdelphij * ignored and traversal will continue (i.e. TRAVERSE_HARD will be passed to
183268650Sdelphij * traverse_dataset_destroyed()).
184268650Sdelphij */
185236884Smmint
186236884Smmbptree_iterate(objset_t *os, uint64_t obj, boolean_t free, bptree_itor_t func,
187236884Smm    void *arg, dmu_tx_t *tx)
188236884Smm{
189268650Sdelphij	boolean_t ioerr = B_FALSE;
190236884Smm	int err;
191236884Smm	uint64_t i;
192236884Smm	dmu_buf_t *db;
193236884Smm	struct bptree_args ba;
194236884Smm
195236884Smm	ASSERT(!free || dmu_tx_is_syncing(tx));
196236884Smm
197236884Smm	err = dmu_bonus_hold(os, obj, FTAG, &db);
198236884Smm	if (err != 0)
199236884Smm		return (err);
200236884Smm
201236884Smm	if (free)
202236884Smm		dmu_buf_will_dirty(db, tx);
203236884Smm
204236884Smm	ba.ba_phys = db->db_data;
205236884Smm	ba.ba_free = free;
206236884Smm	ba.ba_func = func;
207236884Smm	ba.ba_arg = arg;
208236884Smm	ba.ba_tx = tx;
209236884Smm
210236884Smm	err = 0;
211236884Smm	for (i = ba.ba_phys->bt_begin; i < ba.ba_phys->bt_end; i++) {
212236884Smm		bptree_entry_phys_t bte;
213262120Savg		int flags = TRAVERSE_PREFETCH_METADATA | TRAVERSE_POST;
214236884Smm
215236884Smm		err = dmu_read(os, obj, i * sizeof (bte), sizeof (bte),
216236884Smm		    &bte, DMU_READ_NO_PREFETCH);
217236884Smm		if (err != 0)
218236884Smm			break;
219236884Smm
220268650Sdelphij		if (zfs_free_leak_on_eio)
221262120Savg			flags |= TRAVERSE_HARD;
222268650Sdelphij		zfs_dbgmsg("bptree index %d: traversing from min_txg=%lld "
223268650Sdelphij		    "bookmark %lld/%lld/%lld/%lld",
224268650Sdelphij		    i, (longlong_t)bte.be_birth_txg,
225268650Sdelphij		    (longlong_t)bte.be_zb.zb_objset,
226268650Sdelphij		    (longlong_t)bte.be_zb.zb_object,
227268650Sdelphij		    (longlong_t)bte.be_zb.zb_level,
228268650Sdelphij		    (longlong_t)bte.be_zb.zb_blkid);
229236884Smm		err = traverse_dataset_destroyed(os->os_spa, &bte.be_bp,
230262120Savg		    bte.be_birth_txg, &bte.be_zb, flags,
231236884Smm		    bptree_visit_cb, &ba);
232236884Smm		if (free) {
233268650Sdelphij			/*
234268650Sdelphij			 * The callback has freed the visited block pointers.
235268650Sdelphij			 * Record our traversal progress on disk, either by
236268650Sdelphij			 * updating this record's bookmark, or by logically
237268650Sdelphij			 * removing this record by advancing bt_begin.
238268650Sdelphij			 */
239268650Sdelphij			if (err != 0) {
240236884Smm				/* save bookmark for future resume */
241236884Smm				ASSERT3U(bte.be_zb.zb_objset, ==,
242236884Smm				    ZB_DESTROYED_OBJSET);
243240415Smm				ASSERT0(bte.be_zb.zb_level);
244236884Smm				dmu_write(os, obj, i * sizeof (bte),
245236884Smm				    sizeof (bte), &bte, tx);
246268650Sdelphij				if (err == EIO || err == ECKSUM ||
247268650Sdelphij				    err == ENXIO) {
248268650Sdelphij					/*
249268650Sdelphij					 * Skip the rest of this tree and
250268650Sdelphij					 * continue on to the next entry.
251268650Sdelphij					 */
252268650Sdelphij					err = 0;
253268650Sdelphij					ioerr = B_TRUE;
254268650Sdelphij				} else {
255268650Sdelphij					break;
256268650Sdelphij				}
257268650Sdelphij			} else if (ioerr) {
258262120Savg				/*
259268650Sdelphij				 * This entry is finished, but there were
260268650Sdelphij				 * i/o errors on previous entries, so we
261268650Sdelphij				 * can't adjust bt_begin.  Set this entry's
262268650Sdelphij				 * be_birth_txg such that it will be
263268650Sdelphij				 * treated as a no-op in future traversals.
264262120Savg				 */
265268650Sdelphij				bte.be_birth_txg = UINT64_MAX;
266268650Sdelphij				dmu_write(os, obj, i * sizeof (bte),
267268650Sdelphij				    sizeof (bte), &bte, tx);
268262120Savg			}
269262120Savg
270268650Sdelphij			if (!ioerr) {
271268650Sdelphij				ba.ba_phys->bt_begin++;
272268650Sdelphij				(void) dmu_free_range(os, obj,
273268650Sdelphij				    i * sizeof (bte), sizeof (bte), tx);
274268650Sdelphij			}
275268650Sdelphij		} else if (err != 0) {
276268650Sdelphij			break;
277236884Smm		}
278236884Smm	}
279236884Smm
280268650Sdelphij	ASSERT(!free || err != 0 || ioerr ||
281268650Sdelphij	    ba.ba_phys->bt_begin == ba.ba_phys->bt_end);
282236884Smm
283236884Smm	/* if all blocks are free there should be no used space */
284236884Smm	if (ba.ba_phys->bt_begin == ba.ba_phys->bt_end) {
285268650Sdelphij		if (zfs_free_leak_on_eio) {
286268650Sdelphij			ba.ba_phys->bt_bytes = 0;
287268650Sdelphij			ba.ba_phys->bt_comp = 0;
288268650Sdelphij			ba.ba_phys->bt_uncomp = 0;
289268650Sdelphij		}
290268650Sdelphij
291240415Smm		ASSERT0(ba.ba_phys->bt_bytes);
292240415Smm		ASSERT0(ba.ba_phys->bt_comp);
293240415Smm		ASSERT0(ba.ba_phys->bt_uncomp);
294236884Smm	}
295236884Smm
296236884Smm	dmu_buf_rele(db, FTAG);
297236884Smm
298236884Smm	return (err);
299236884Smm}
300