xref: /titanic_51/usr/src/uts/common/fs/zfs/dmu_tx.c (revision d8d810637042c2a73a269f125dee75e8ed1421d1)
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 #include <sys/dmu.h>
27 #include <sys/dmu_impl.h>
28 #include <sys/dbuf.h>
29 #include <sys/dmu_tx.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
32 #include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
33 #include <sys/dsl_pool.h>
34 #include <sys/zap_impl.h> /* for fzap_default_block_shift */
35 #include <sys/spa.h>
36 #include <sys/zfs_context.h>
37 
38 typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
39     uint64_t arg1, uint64_t arg2);
40 
41 
42 dmu_tx_t *
43 dmu_tx_create_dd(dsl_dir_t *dd)
44 {
45 	dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
46 	tx->tx_dir = dd;
47 	if (dd)
48 		tx->tx_pool = dd->dd_pool;
49 	list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
50 	    offsetof(dmu_tx_hold_t, txh_node));
51 	list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
52 	    offsetof(dmu_tx_callback_t, dcb_node));
53 #ifdef ZFS_DEBUG
54 	refcount_create(&tx->tx_space_written);
55 	refcount_create(&tx->tx_space_freed);
56 #endif
57 	return (tx);
58 }
59 
60 dmu_tx_t *
61 dmu_tx_create(objset_t *os)
62 {
63 	dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
64 	tx->tx_objset = os;
65 	tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset);
66 	return (tx);
67 }
68 
69 dmu_tx_t *
70 dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
71 {
72 	dmu_tx_t *tx = dmu_tx_create_dd(NULL);
73 
74 	ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
75 	tx->tx_pool = dp;
76 	tx->tx_txg = txg;
77 	tx->tx_anyobj = TRUE;
78 
79 	return (tx);
80 }
81 
82 int
83 dmu_tx_is_syncing(dmu_tx_t *tx)
84 {
85 	return (tx->tx_anyobj);
86 }
87 
88 int
89 dmu_tx_private_ok(dmu_tx_t *tx)
90 {
91 	return (tx->tx_anyobj);
92 }
93 
94 static dmu_tx_hold_t *
95 dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
96     enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
97 {
98 	dmu_tx_hold_t *txh;
99 	dnode_t *dn = NULL;
100 	int err;
101 
102 	if (object != DMU_NEW_OBJECT) {
103 		err = dnode_hold(os, object, tx, &dn);
104 		if (err) {
105 			tx->tx_err = err;
106 			return (NULL);
107 		}
108 
109 		if (err == 0 && tx->tx_txg != 0) {
110 			mutex_enter(&dn->dn_mtx);
111 			/*
112 			 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
113 			 * problem, but there's no way for it to happen (for
114 			 * now, at least).
115 			 */
116 			ASSERT(dn->dn_assigned_txg == 0);
117 			dn->dn_assigned_txg = tx->tx_txg;
118 			(void) refcount_add(&dn->dn_tx_holds, tx);
119 			mutex_exit(&dn->dn_mtx);
120 		}
121 	}
122 
123 	txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
124 	txh->txh_tx = tx;
125 	txh->txh_dnode = dn;
126 #ifdef ZFS_DEBUG
127 	txh->txh_type = type;
128 	txh->txh_arg1 = arg1;
129 	txh->txh_arg2 = arg2;
130 #endif
131 	list_insert_tail(&tx->tx_holds, txh);
132 
133 	return (txh);
134 }
135 
136 void
137 dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
138 {
139 	/*
140 	 * If we're syncing, they can manipulate any object anyhow, and
141 	 * the hold on the dnode_t can cause problems.
142 	 */
143 	if (!dmu_tx_is_syncing(tx)) {
144 		(void) dmu_tx_hold_object_impl(tx, os,
145 		    object, THT_NEWOBJECT, 0, 0);
146 	}
147 }
148 
149 static int
150 dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
151 {
152 	int err;
153 	dmu_buf_impl_t *db;
154 
155 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
156 	db = dbuf_hold_level(dn, level, blkid, FTAG);
157 	rw_exit(&dn->dn_struct_rwlock);
158 	if (db == NULL)
159 		return (EIO);
160 	err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
161 	dbuf_rele(db, FTAG);
162 	return (err);
163 }
164 
165 static void
166 dmu_tx_count_indirects(dmu_tx_hold_t *txh, dmu_buf_impl_t *db,
167     boolean_t freeable, dmu_buf_impl_t **history)
168 {
169 	int i = db->db_level + 1;
170 	dnode_t *dn = db->db_dnode;
171 
172 	if (i >= dn->dn_nlevels)
173 		return;
174 
175 	db = db->db_parent;
176 	if (db == NULL) {
177 		uint64_t lvls = dn->dn_nlevels - i;
178 
179 		txh->txh_space_towrite += lvls << dn->dn_indblkshift;
180 		return;
181 	}
182 
183 	if (db != history[i]) {
184 		dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
185 		uint64_t space = 1ULL << dn->dn_indblkshift;
186 
187 		freeable = (db->db_blkptr && (freeable ||
188 		    dsl_dataset_block_freeable(ds, db->db_blkptr->blk_birth)));
189 		if (freeable)
190 			txh->txh_space_tooverwrite += space;
191 		else
192 			txh->txh_space_towrite += space;
193 		if (db->db_blkptr)
194 			txh->txh_space_tounref += space;
195 		history[i] = db;
196 		dmu_tx_count_indirects(txh, db, freeable, history);
197 	}
198 }
199 
200 /* ARGSUSED */
201 static void
202 dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
203 {
204 	dnode_t *dn = txh->txh_dnode;
205 	uint64_t start, end, i;
206 	int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
207 	int err = 0;
208 
209 	if (len == 0)
210 		return;
211 
212 	min_bs = SPA_MINBLOCKSHIFT;
213 	max_bs = SPA_MAXBLOCKSHIFT;
214 	min_ibs = DN_MIN_INDBLKSHIFT;
215 	max_ibs = DN_MAX_INDBLKSHIFT;
216 
217 	if (dn) {
218 		dmu_buf_impl_t *last[DN_MAX_LEVELS];
219 		int nlvls = dn->dn_nlevels;
220 		int delta;
221 
222 		/*
223 		 * For i/o error checking, read the first and last level-0
224 		 * blocks (if they are not aligned), and all the level-1 blocks.
225 		 */
226 		if (dn->dn_maxblkid == 0) {
227 			delta = dn->dn_datablksz;
228 			start = (off < dn->dn_datablksz) ? 0 : 1;
229 			end = (off+len <= dn->dn_datablksz) ? 0 : 1;
230 			if (start == 0 && (off > 0 || len < dn->dn_datablksz)) {
231 				err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
232 				if (err)
233 					goto out;
234 				delta -= off;
235 			}
236 		} else {
237 			zio_t *zio = zio_root(dn->dn_objset->os_spa,
238 			    NULL, NULL, ZIO_FLAG_CANFAIL);
239 
240 			/* first level-0 block */
241 			start = off >> dn->dn_datablkshift;
242 			if (P2PHASE(off, dn->dn_datablksz) ||
243 			    len < dn->dn_datablksz) {
244 				err = dmu_tx_check_ioerr(zio, dn, 0, start);
245 				if (err)
246 					goto out;
247 			}
248 
249 			/* last level-0 block */
250 			end = (off+len-1) >> dn->dn_datablkshift;
251 			if (end != start && end <= dn->dn_maxblkid &&
252 			    P2PHASE(off+len, dn->dn_datablksz)) {
253 				err = dmu_tx_check_ioerr(zio, dn, 0, end);
254 				if (err)
255 					goto out;
256 			}
257 
258 			/* level-1 blocks */
259 			if (nlvls > 1) {
260 				int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
261 				for (i = (start>>shft)+1; i < end>>shft; i++) {
262 					err = dmu_tx_check_ioerr(zio, dn, 1, i);
263 					if (err)
264 						goto out;
265 				}
266 			}
267 
268 			err = zio_wait(zio);
269 			if (err)
270 				goto out;
271 			delta = P2NPHASE(off, dn->dn_datablksz);
272 		}
273 
274 		if (dn->dn_maxblkid > 0) {
275 			/*
276 			 * The blocksize can't change,
277 			 * so we can make a more precise estimate.
278 			 */
279 			ASSERT(dn->dn_datablkshift != 0);
280 			min_bs = max_bs = dn->dn_datablkshift;
281 			min_ibs = max_ibs = dn->dn_indblkshift;
282 		} else if (dn->dn_indblkshift > max_ibs) {
283 			/*
284 			 * This ensures that if we reduce DN_MAX_INDBLKSHIFT,
285 			 * the code will still work correctly on older pools.
286 			 */
287 			min_ibs = max_ibs = dn->dn_indblkshift;
288 		}
289 
290 		/*
291 		 * If this write is not off the end of the file
292 		 * we need to account for overwrites/unref.
293 		 */
294 		if (start <= dn->dn_maxblkid)
295 			bzero(last, sizeof (dmu_buf_impl_t *) * DN_MAX_LEVELS);
296 		while (start <= dn->dn_maxblkid) {
297 			spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
298 			dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
299 			dmu_buf_impl_t *db;
300 
301 			rw_enter(&dn->dn_struct_rwlock, RW_READER);
302 			db = dbuf_hold_level(dn, 0, start, FTAG);
303 			rw_exit(&dn->dn_struct_rwlock);
304 			if (db->db_blkptr && dsl_dataset_block_freeable(ds,
305 			    db->db_blkptr->blk_birth)) {
306 				dprintf_bp(db->db_blkptr, "can free old%s", "");
307 				txh->txh_space_tooverwrite += dn->dn_datablksz;
308 				txh->txh_space_tounref += dn->dn_datablksz;
309 				dmu_tx_count_indirects(txh, db, TRUE, last);
310 			} else {
311 				txh->txh_space_towrite += dn->dn_datablksz;
312 				if (db->db_blkptr)
313 					txh->txh_space_tounref +=
314 					    bp_get_dasize(spa, db->db_blkptr);
315 				dmu_tx_count_indirects(txh, db, FALSE, last);
316 			}
317 			dbuf_rele(db, FTAG);
318 			if (++start > end) {
319 				/*
320 				 * Account for new indirects appearing
321 				 * before this IO gets assigned into a txg.
322 				 */
323 				bits = 64 - min_bs;
324 				epbs = min_ibs - SPA_BLKPTRSHIFT;
325 				for (bits -= epbs * (nlvls - 1);
326 				    bits >= 0; bits -= epbs)
327 					txh->txh_fudge += 1ULL << max_ibs;
328 				goto out;
329 			}
330 			off += delta;
331 			if (len >= delta)
332 				len -= delta;
333 			delta = dn->dn_datablksz;
334 		}
335 	}
336 
337 	/*
338 	 * 'end' is the last thing we will access, not one past.
339 	 * This way we won't overflow when accessing the last byte.
340 	 */
341 	start = P2ALIGN(off, 1ULL << max_bs);
342 	end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
343 	txh->txh_space_towrite += end - start + 1;
344 
345 	start >>= min_bs;
346 	end >>= min_bs;
347 
348 	epbs = min_ibs - SPA_BLKPTRSHIFT;
349 
350 	/*
351 	 * The object contains at most 2^(64 - min_bs) blocks,
352 	 * and each indirect level maps 2^epbs.
353 	 */
354 	for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
355 		start >>= epbs;
356 		end >>= epbs;
357 		ASSERT3U(end, >=, start);
358 		txh->txh_space_towrite += (end - start + 1) << max_ibs;
359 		if (start != 0) {
360 			/*
361 			 * We also need a new blkid=0 indirect block
362 			 * to reference any existing file data.
363 			 */
364 			txh->txh_space_towrite += 1ULL << max_ibs;
365 		}
366 	}
367 
368 out:
369 	if (txh->txh_space_towrite + txh->txh_space_tooverwrite >
370 	    2 * DMU_MAX_ACCESS)
371 		err = EFBIG;
372 
373 	if (err)
374 		txh->txh_tx->tx_err = err;
375 }
376 
377 static void
378 dmu_tx_count_dnode(dmu_tx_hold_t *txh)
379 {
380 	dnode_t *dn = txh->txh_dnode;
381 	dnode_t *mdn = txh->txh_tx->tx_objset->os_meta_dnode;
382 	uint64_t space = mdn->dn_datablksz +
383 	    ((mdn->dn_nlevels-1) << mdn->dn_indblkshift);
384 
385 	if (dn && dn->dn_dbuf->db_blkptr &&
386 	    dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
387 	    dn->dn_dbuf->db_blkptr->blk_birth)) {
388 		txh->txh_space_tooverwrite += space;
389 		txh->txh_space_tounref += space;
390 	} else {
391 		txh->txh_space_towrite += space;
392 		if (dn && dn->dn_dbuf->db_blkptr)
393 			txh->txh_space_tounref += space;
394 	}
395 }
396 
397 void
398 dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
399 {
400 	dmu_tx_hold_t *txh;
401 
402 	ASSERT(tx->tx_txg == 0);
403 	ASSERT(len < DMU_MAX_ACCESS);
404 	ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
405 
406 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
407 	    object, THT_WRITE, off, len);
408 	if (txh == NULL)
409 		return;
410 
411 	dmu_tx_count_write(txh, off, len);
412 	dmu_tx_count_dnode(txh);
413 }
414 
415 static void
416 dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
417 {
418 	uint64_t blkid, nblks, lastblk;
419 	uint64_t space = 0, unref = 0, skipped = 0;
420 	dnode_t *dn = txh->txh_dnode;
421 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
422 	spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
423 	int epbs;
424 
425 	if (dn->dn_nlevels == 0)
426 		return;
427 
428 	/*
429 	 * The struct_rwlock protects us against dn_nlevels
430 	 * changing, in case (against all odds) we manage to dirty &
431 	 * sync out the changes after we check for being dirty.
432 	 * Also, dbuf_hold_level() wants us to have the struct_rwlock.
433 	 */
434 	rw_enter(&dn->dn_struct_rwlock, RW_READER);
435 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
436 	if (dn->dn_maxblkid == 0) {
437 		if (off == 0 && len >= dn->dn_datablksz) {
438 			blkid = 0;
439 			nblks = 1;
440 		} else {
441 			rw_exit(&dn->dn_struct_rwlock);
442 			return;
443 		}
444 	} else {
445 		blkid = off >> dn->dn_datablkshift;
446 		nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
447 
448 		if (blkid >= dn->dn_maxblkid) {
449 			rw_exit(&dn->dn_struct_rwlock);
450 			return;
451 		}
452 		if (blkid + nblks > dn->dn_maxblkid)
453 			nblks = dn->dn_maxblkid - blkid;
454 
455 	}
456 	if (dn->dn_nlevels == 1) {
457 		int i;
458 		for (i = 0; i < nblks; i++) {
459 			blkptr_t *bp = dn->dn_phys->dn_blkptr;
460 			ASSERT3U(blkid + i, <, dn->dn_nblkptr);
461 			bp += blkid + i;
462 			if (dsl_dataset_block_freeable(ds, bp->blk_birth)) {
463 				dprintf_bp(bp, "can free old%s", "");
464 				space += bp_get_dasize(spa, bp);
465 			}
466 			unref += BP_GET_ASIZE(bp);
467 		}
468 		nblks = 0;
469 	}
470 
471 	/*
472 	 * Add in memory requirements of higher-level indirects.
473 	 * This assumes a worst-possible scenario for dn_nlevels.
474 	 */
475 	{
476 		uint64_t blkcnt = 1 + ((nblks >> epbs) >> epbs);
477 		int level = (dn->dn_nlevels > 1) ? 2 : 1;
478 
479 		while (level++ < DN_MAX_LEVELS) {
480 			txh->txh_memory_tohold += blkcnt << dn->dn_indblkshift;
481 			blkcnt = 1 + (blkcnt >> epbs);
482 		}
483 		ASSERT(blkcnt <= dn->dn_nblkptr);
484 	}
485 
486 	lastblk = blkid + nblks - 1;
487 	while (nblks) {
488 		dmu_buf_impl_t *dbuf;
489 		uint64_t ibyte, new_blkid;
490 		int epb = 1 << epbs;
491 		int err, i, blkoff, tochk;
492 		blkptr_t *bp;
493 
494 		ibyte = blkid << dn->dn_datablkshift;
495 		err = dnode_next_offset(dn,
496 		    DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
497 		new_blkid = ibyte >> dn->dn_datablkshift;
498 		if (err == ESRCH) {
499 			skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
500 			break;
501 		}
502 		if (err) {
503 			txh->txh_tx->tx_err = err;
504 			break;
505 		}
506 		if (new_blkid > lastblk) {
507 			skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
508 			break;
509 		}
510 
511 		if (new_blkid > blkid) {
512 			ASSERT((new_blkid >> epbs) > (blkid >> epbs));
513 			skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1;
514 			nblks -= new_blkid - blkid;
515 			blkid = new_blkid;
516 		}
517 		blkoff = P2PHASE(blkid, epb);
518 		tochk = MIN(epb - blkoff, nblks);
519 
520 		dbuf = dbuf_hold_level(dn, 1, blkid >> epbs, FTAG);
521 
522 		txh->txh_memory_tohold += dbuf->db.db_size;
523 		if (txh->txh_memory_tohold > DMU_MAX_ACCESS) {
524 			txh->txh_tx->tx_err = E2BIG;
525 			dbuf_rele(dbuf, FTAG);
526 			break;
527 		}
528 		err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL);
529 		if (err != 0) {
530 			txh->txh_tx->tx_err = err;
531 			dbuf_rele(dbuf, FTAG);
532 			break;
533 		}
534 
535 		bp = dbuf->db.db_data;
536 		bp += blkoff;
537 
538 		for (i = 0; i < tochk; i++) {
539 			if (dsl_dataset_block_freeable(ds, bp[i].blk_birth)) {
540 				dprintf_bp(&bp[i], "can free old%s", "");
541 				space += bp_get_dasize(spa, &bp[i]);
542 			}
543 			unref += BP_GET_ASIZE(bp);
544 		}
545 		dbuf_rele(dbuf, FTAG);
546 
547 		blkid += tochk;
548 		nblks -= tochk;
549 	}
550 	rw_exit(&dn->dn_struct_rwlock);
551 
552 	/* account for new level 1 indirect blocks that might show up */
553 	if (skipped > 0) {
554 		txh->txh_fudge += skipped << dn->dn_indblkshift;
555 		skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
556 		txh->txh_memory_tohold += skipped << dn->dn_indblkshift;
557 	}
558 	txh->txh_space_tofree += space;
559 	txh->txh_space_tounref += unref;
560 }
561 
562 void
563 dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
564 {
565 	dmu_tx_hold_t *txh;
566 	dnode_t *dn;
567 	uint64_t start, end, i;
568 	int err, shift;
569 	zio_t *zio;
570 
571 	ASSERT(tx->tx_txg == 0);
572 
573 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
574 	    object, THT_FREE, off, len);
575 	if (txh == NULL)
576 		return;
577 	dn = txh->txh_dnode;
578 
579 	/* first block */
580 	if (off != 0)
581 		dmu_tx_count_write(txh, off, 1);
582 	/* last block */
583 	if (len != DMU_OBJECT_END)
584 		dmu_tx_count_write(txh, off+len, 1);
585 
586 	if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
587 		return;
588 	if (len == DMU_OBJECT_END)
589 		len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
590 
591 	/*
592 	 * For i/o error checking, read the first and last level-0
593 	 * blocks, and all the level-1 blocks.  The above count_write's
594 	 * have already taken care of the level-0 blocks.
595 	 */
596 	if (dn->dn_nlevels > 1) {
597 		shift = dn->dn_datablkshift + dn->dn_indblkshift -
598 		    SPA_BLKPTRSHIFT;
599 		start = off >> shift;
600 		end = dn->dn_datablkshift ? ((off+len) >> shift) : 0;
601 
602 		zio = zio_root(tx->tx_pool->dp_spa,
603 		    NULL, NULL, ZIO_FLAG_CANFAIL);
604 		for (i = start; i <= end; i++) {
605 			uint64_t ibyte = i << shift;
606 			err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
607 			i = ibyte >> shift;
608 			if (err == ESRCH)
609 				break;
610 			if (err) {
611 				tx->tx_err = err;
612 				return;
613 			}
614 
615 			err = dmu_tx_check_ioerr(zio, dn, 1, i);
616 			if (err) {
617 				tx->tx_err = err;
618 				return;
619 			}
620 		}
621 		err = zio_wait(zio);
622 		if (err) {
623 			tx->tx_err = err;
624 			return;
625 		}
626 	}
627 
628 	dmu_tx_count_dnode(txh);
629 	dmu_tx_count_free(txh, off, len);
630 }
631 
632 void
633 dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
634 {
635 	dmu_tx_hold_t *txh;
636 	dnode_t *dn;
637 	uint64_t nblocks;
638 	int epbs, err;
639 
640 	ASSERT(tx->tx_txg == 0);
641 
642 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
643 	    object, THT_ZAP, add, (uintptr_t)name);
644 	if (txh == NULL)
645 		return;
646 	dn = txh->txh_dnode;
647 
648 	dmu_tx_count_dnode(txh);
649 
650 	if (dn == NULL) {
651 		/*
652 		 * We will be able to fit a new object's entries into one leaf
653 		 * block.  So there will be at most 2 blocks total,
654 		 * including the header block.
655 		 */
656 		dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
657 		return;
658 	}
659 
660 	ASSERT3P(dmu_ot[dn->dn_type].ot_byteswap, ==, zap_byteswap);
661 
662 	if (dn->dn_maxblkid == 0 && !add) {
663 		/*
664 		 * If there is only one block  (i.e. this is a micro-zap)
665 		 * and we are not adding anything, the accounting is simple.
666 		 */
667 		err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
668 		if (err) {
669 			tx->tx_err = err;
670 			return;
671 		}
672 
673 		/*
674 		 * Use max block size here, since we don't know how much
675 		 * the size will change between now and the dbuf dirty call.
676 		 */
677 		if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
678 		    dn->dn_phys->dn_blkptr[0].blk_birth)) {
679 			txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
680 		} else {
681 			txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
682 		}
683 		if (dn->dn_phys->dn_blkptr[0].blk_birth)
684 			txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
685 		return;
686 	}
687 
688 	if (dn->dn_maxblkid > 0 && name) {
689 		/*
690 		 * access the name in this fat-zap so that we'll check
691 		 * for i/o errors to the leaf blocks, etc.
692 		 */
693 		err = zap_lookup(dn->dn_objset, dn->dn_object, name,
694 		    8, 0, NULL);
695 		if (err == EIO) {
696 			tx->tx_err = err;
697 			return;
698 		}
699 	}
700 
701 	err = zap_count_write(dn->dn_objset, dn->dn_object, name, add,
702 	    &txh->txh_space_towrite, &txh->txh_space_tooverwrite);
703 
704 	/*
705 	 * If the modified blocks are scattered to the four winds,
706 	 * we'll have to modify an indirect twig for each.
707 	 */
708 	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
709 	for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
710 		if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj)
711 			txh->txh_space_towrite += 3 << dn->dn_indblkshift;
712 		else
713 			txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift;
714 }
715 
716 void
717 dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
718 {
719 	dmu_tx_hold_t *txh;
720 
721 	ASSERT(tx->tx_txg == 0);
722 
723 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
724 	    object, THT_BONUS, 0, 0);
725 	if (txh)
726 		dmu_tx_count_dnode(txh);
727 }
728 
729 void
730 dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
731 {
732 	dmu_tx_hold_t *txh;
733 	ASSERT(tx->tx_txg == 0);
734 
735 	txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
736 	    DMU_NEW_OBJECT, THT_SPACE, space, 0);
737 
738 	txh->txh_space_towrite += space;
739 }
740 
741 int
742 dmu_tx_holds(dmu_tx_t *tx, uint64_t object)
743 {
744 	dmu_tx_hold_t *txh;
745 	int holds = 0;
746 
747 	/*
748 	 * By asserting that the tx is assigned, we're counting the
749 	 * number of dn_tx_holds, which is the same as the number of
750 	 * dn_holds.  Otherwise, we'd be counting dn_holds, but
751 	 * dn_tx_holds could be 0.
752 	 */
753 	ASSERT(tx->tx_txg != 0);
754 
755 	/* if (tx->tx_anyobj == TRUE) */
756 		/* return (0); */
757 
758 	for (txh = list_head(&tx->tx_holds); txh;
759 	    txh = list_next(&tx->tx_holds, txh)) {
760 		if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
761 			holds++;
762 	}
763 
764 	return (holds);
765 }
766 
767 #ifdef ZFS_DEBUG
768 void
769 dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
770 {
771 	dmu_tx_hold_t *txh;
772 	int match_object = FALSE, match_offset = FALSE;
773 	dnode_t *dn = db->db_dnode;
774 
775 	ASSERT(tx->tx_txg != 0);
776 	ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
777 	ASSERT3U(dn->dn_object, ==, db->db.db_object);
778 
779 	if (tx->tx_anyobj)
780 		return;
781 
782 	/* XXX No checking on the meta dnode for now */
783 	if (db->db.db_object == DMU_META_DNODE_OBJECT)
784 		return;
785 
786 	for (txh = list_head(&tx->tx_holds); txh;
787 	    txh = list_next(&tx->tx_holds, txh)) {
788 		ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
789 		if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
790 			match_object = TRUE;
791 		if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
792 			int datablkshift = dn->dn_datablkshift ?
793 			    dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
794 			int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
795 			int shift = datablkshift + epbs * db->db_level;
796 			uint64_t beginblk = shift >= 64 ? 0 :
797 			    (txh->txh_arg1 >> shift);
798 			uint64_t endblk = shift >= 64 ? 0 :
799 			    ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
800 			uint64_t blkid = db->db_blkid;
801 
802 			/* XXX txh_arg2 better not be zero... */
803 
804 			dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
805 			    txh->txh_type, beginblk, endblk);
806 
807 			switch (txh->txh_type) {
808 			case THT_WRITE:
809 				if (blkid >= beginblk && blkid <= endblk)
810 					match_offset = TRUE;
811 				/*
812 				 * We will let this hold work for the bonus
813 				 * buffer so that we don't need to hold it
814 				 * when creating a new object.
815 				 */
816 				if (blkid == DB_BONUS_BLKID)
817 					match_offset = TRUE;
818 				/*
819 				 * They might have to increase nlevels,
820 				 * thus dirtying the new TLIBs.  Or the
821 				 * might have to change the block size,
822 				 * thus dirying the new lvl=0 blk=0.
823 				 */
824 				if (blkid == 0)
825 					match_offset = TRUE;
826 				break;
827 			case THT_FREE:
828 				/*
829 				 * We will dirty all the level 1 blocks in
830 				 * the free range and perhaps the first and
831 				 * last level 0 block.
832 				 */
833 				if (blkid >= beginblk && (blkid <= endblk ||
834 				    txh->txh_arg2 == DMU_OBJECT_END))
835 					match_offset = TRUE;
836 				break;
837 			case THT_BONUS:
838 				if (blkid == DB_BONUS_BLKID)
839 					match_offset = TRUE;
840 				break;
841 			case THT_ZAP:
842 				match_offset = TRUE;
843 				break;
844 			case THT_NEWOBJECT:
845 				match_object = TRUE;
846 				break;
847 			default:
848 				ASSERT(!"bad txh_type");
849 			}
850 		}
851 		if (match_object && match_offset)
852 			return;
853 	}
854 	panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
855 	    (u_longlong_t)db->db.db_object, db->db_level,
856 	    (u_longlong_t)db->db_blkid);
857 }
858 #endif
859 
860 static int
861 dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how)
862 {
863 	dmu_tx_hold_t *txh;
864 	spa_t *spa = tx->tx_pool->dp_spa;
865 	uint64_t memory, asize, fsize, usize;
866 	uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
867 
868 	ASSERT3U(tx->tx_txg, ==, 0);
869 
870 	if (tx->tx_err)
871 		return (tx->tx_err);
872 
873 	if (spa_suspended(spa)) {
874 		/*
875 		 * If the user has indicated a blocking failure mode
876 		 * then return ERESTART which will block in dmu_tx_wait().
877 		 * Otherwise, return EIO so that an error can get
878 		 * propagated back to the VOP calls.
879 		 *
880 		 * Note that we always honor the txg_how flag regardless
881 		 * of the failuremode setting.
882 		 */
883 		if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
884 		    txg_how != TXG_WAIT)
885 			return (EIO);
886 
887 		return (ERESTART);
888 	}
889 
890 	tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
891 	tx->tx_needassign_txh = NULL;
892 
893 	/*
894 	 * NB: No error returns are allowed after txg_hold_open, but
895 	 * before processing the dnode holds, due to the
896 	 * dmu_tx_unassign() logic.
897 	 */
898 
899 	towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
900 	for (txh = list_head(&tx->tx_holds); txh;
901 	    txh = list_next(&tx->tx_holds, txh)) {
902 		dnode_t *dn = txh->txh_dnode;
903 		if (dn != NULL) {
904 			mutex_enter(&dn->dn_mtx);
905 			if (dn->dn_assigned_txg == tx->tx_txg - 1) {
906 				mutex_exit(&dn->dn_mtx);
907 				tx->tx_needassign_txh = txh;
908 				return (ERESTART);
909 			}
910 			if (dn->dn_assigned_txg == 0)
911 				dn->dn_assigned_txg = tx->tx_txg;
912 			ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
913 			(void) refcount_add(&dn->dn_tx_holds, tx);
914 			mutex_exit(&dn->dn_mtx);
915 		}
916 		towrite += txh->txh_space_towrite;
917 		tofree += txh->txh_space_tofree;
918 		tooverwrite += txh->txh_space_tooverwrite;
919 		tounref += txh->txh_space_tounref;
920 		tohold += txh->txh_memory_tohold;
921 		fudge += txh->txh_fudge;
922 	}
923 
924 	/*
925 	 * NB: This check must be after we've held the dnodes, so that
926 	 * the dmu_tx_unassign() logic will work properly
927 	 */
928 	if (txg_how >= TXG_INITIAL && txg_how != tx->tx_txg)
929 		return (ERESTART);
930 
931 	/*
932 	 * If a snapshot has been taken since we made our estimates,
933 	 * assume that we won't be able to free or overwrite anything.
934 	 */
935 	if (tx->tx_objset &&
936 	    dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) >
937 	    tx->tx_lastsnap_txg) {
938 		towrite += tooverwrite;
939 		tooverwrite = tofree = 0;
940 	}
941 
942 	/* needed allocation: worst-case estimate of write space */
943 	asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
944 	/* freed space estimate: worst-case overwrite + free estimate */
945 	fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
946 	/* convert unrefd space to worst-case estimate */
947 	usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
948 	/* calculate memory footprint estimate */
949 	memory = towrite + tooverwrite + tohold;
950 
951 #ifdef ZFS_DEBUG
952 	/*
953 	 * Add in 'tohold' to account for our dirty holds on this memory
954 	 * XXX - the "fudge" factor is to account for skipped blocks that
955 	 * we missed because dnode_next_offset() misses in-core-only blocks.
956 	 */
957 	tx->tx_space_towrite = asize +
958 	    spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
959 	tx->tx_space_tofree = tofree;
960 	tx->tx_space_tooverwrite = tooverwrite;
961 	tx->tx_space_tounref = tounref;
962 #endif
963 
964 	if (tx->tx_dir && asize != 0) {
965 		int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
966 		    asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
967 		if (err)
968 			return (err);
969 	}
970 
971 	return (0);
972 }
973 
974 static void
975 dmu_tx_unassign(dmu_tx_t *tx)
976 {
977 	dmu_tx_hold_t *txh;
978 
979 	if (tx->tx_txg == 0)
980 		return;
981 
982 	txg_rele_to_quiesce(&tx->tx_txgh);
983 
984 	for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
985 	    txh = list_next(&tx->tx_holds, txh)) {
986 		dnode_t *dn = txh->txh_dnode;
987 
988 		if (dn == NULL)
989 			continue;
990 		mutex_enter(&dn->dn_mtx);
991 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
992 
993 		if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
994 			dn->dn_assigned_txg = 0;
995 			cv_broadcast(&dn->dn_notxholds);
996 		}
997 		mutex_exit(&dn->dn_mtx);
998 	}
999 
1000 	txg_rele_to_sync(&tx->tx_txgh);
1001 
1002 	tx->tx_lasttried_txg = tx->tx_txg;
1003 	tx->tx_txg = 0;
1004 }
1005 
1006 /*
1007  * Assign tx to a transaction group.  txg_how can be one of:
1008  *
1009  * (1)	TXG_WAIT.  If the current open txg is full, waits until there's
1010  *	a new one.  This should be used when you're not holding locks.
1011  *	If will only fail if we're truly out of space (or over quota).
1012  *
1013  * (2)	TXG_NOWAIT.  If we can't assign into the current open txg without
1014  *	blocking, returns immediately with ERESTART.  This should be used
1015  *	whenever you're holding locks.  On an ERESTART error, the caller
1016  *	should drop locks, do a dmu_tx_wait(tx), and try again.
1017  *
1018  * (3)	A specific txg.  Use this if you need to ensure that multiple
1019  *	transactions all sync in the same txg.  Like TXG_NOWAIT, it
1020  *	returns ERESTART if it can't assign you into the requested txg.
1021  */
1022 int
1023 dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how)
1024 {
1025 	int err;
1026 
1027 	ASSERT(tx->tx_txg == 0);
1028 	ASSERT(txg_how != 0);
1029 	ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1030 
1031 	while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
1032 		dmu_tx_unassign(tx);
1033 
1034 		if (err != ERESTART || txg_how != TXG_WAIT)
1035 			return (err);
1036 
1037 		dmu_tx_wait(tx);
1038 	}
1039 
1040 	txg_rele_to_quiesce(&tx->tx_txgh);
1041 
1042 	return (0);
1043 }
1044 
1045 void
1046 dmu_tx_wait(dmu_tx_t *tx)
1047 {
1048 	spa_t *spa = tx->tx_pool->dp_spa;
1049 
1050 	ASSERT(tx->tx_txg == 0);
1051 
1052 	/*
1053 	 * It's possible that the pool has become active after this thread
1054 	 * has tried to obtain a tx. If that's the case then his
1055 	 * tx_lasttried_txg would not have been assigned.
1056 	 */
1057 	if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
1058 		txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1);
1059 	} else if (tx->tx_needassign_txh) {
1060 		dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1061 
1062 		mutex_enter(&dn->dn_mtx);
1063 		while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1064 			cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1065 		mutex_exit(&dn->dn_mtx);
1066 		tx->tx_needassign_txh = NULL;
1067 	} else {
1068 		txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
1069 	}
1070 }
1071 
1072 void
1073 dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
1074 {
1075 #ifdef ZFS_DEBUG
1076 	if (tx->tx_dir == NULL || delta == 0)
1077 		return;
1078 
1079 	if (delta > 0) {
1080 		ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
1081 		    tx->tx_space_towrite);
1082 		(void) refcount_add_many(&tx->tx_space_written, delta, NULL);
1083 	} else {
1084 		(void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
1085 	}
1086 #endif
1087 }
1088 
1089 void
1090 dmu_tx_commit(dmu_tx_t *tx)
1091 {
1092 	dmu_tx_hold_t *txh;
1093 
1094 	ASSERT(tx->tx_txg != 0);
1095 
1096 	while (txh = list_head(&tx->tx_holds)) {
1097 		dnode_t *dn = txh->txh_dnode;
1098 
1099 		list_remove(&tx->tx_holds, txh);
1100 		kmem_free(txh, sizeof (dmu_tx_hold_t));
1101 		if (dn == NULL)
1102 			continue;
1103 		mutex_enter(&dn->dn_mtx);
1104 		ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1105 
1106 		if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1107 			dn->dn_assigned_txg = 0;
1108 			cv_broadcast(&dn->dn_notxholds);
1109 		}
1110 		mutex_exit(&dn->dn_mtx);
1111 		dnode_rele(dn, tx);
1112 	}
1113 
1114 	if (tx->tx_tempreserve_cookie)
1115 		dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1116 
1117 	if (!list_is_empty(&tx->tx_callbacks))
1118 		txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1119 
1120 	if (tx->tx_anyobj == FALSE)
1121 		txg_rele_to_sync(&tx->tx_txgh);
1122 
1123 	list_destroy(&tx->tx_callbacks);
1124 	list_destroy(&tx->tx_holds);
1125 #ifdef ZFS_DEBUG
1126 	dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
1127 	    tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
1128 	    tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
1129 	refcount_destroy_many(&tx->tx_space_written,
1130 	    refcount_count(&tx->tx_space_written));
1131 	refcount_destroy_many(&tx->tx_space_freed,
1132 	    refcount_count(&tx->tx_space_freed));
1133 #endif
1134 	kmem_free(tx, sizeof (dmu_tx_t));
1135 }
1136 
1137 void
1138 dmu_tx_abort(dmu_tx_t *tx)
1139 {
1140 	dmu_tx_hold_t *txh;
1141 
1142 	ASSERT(tx->tx_txg == 0);
1143 
1144 	while (txh = list_head(&tx->tx_holds)) {
1145 		dnode_t *dn = txh->txh_dnode;
1146 
1147 		list_remove(&tx->tx_holds, txh);
1148 		kmem_free(txh, sizeof (dmu_tx_hold_t));
1149 		if (dn != NULL)
1150 			dnode_rele(dn, tx);
1151 	}
1152 
1153 	/*
1154 	 * Call any registered callbacks with an error code.
1155 	 */
1156 	if (!list_is_empty(&tx->tx_callbacks))
1157 		dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1158 
1159 	list_destroy(&tx->tx_callbacks);
1160 	list_destroy(&tx->tx_holds);
1161 #ifdef ZFS_DEBUG
1162 	refcount_destroy_many(&tx->tx_space_written,
1163 	    refcount_count(&tx->tx_space_written));
1164 	refcount_destroy_many(&tx->tx_space_freed,
1165 	    refcount_count(&tx->tx_space_freed));
1166 #endif
1167 	kmem_free(tx, sizeof (dmu_tx_t));
1168 }
1169 
1170 uint64_t
1171 dmu_tx_get_txg(dmu_tx_t *tx)
1172 {
1173 	ASSERT(tx->tx_txg != 0);
1174 	return (tx->tx_txg);
1175 }
1176 
1177 void
1178 dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1179 {
1180 	dmu_tx_callback_t *dcb;
1181 
1182 	dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1183 
1184 	dcb->dcb_func = func;
1185 	dcb->dcb_data = data;
1186 
1187 	list_insert_tail(&tx->tx_callbacks, dcb);
1188 }
1189 
1190 /*
1191  * Call all the commit callbacks on a list, with a given error code.
1192  */
1193 void
1194 dmu_tx_do_callbacks(list_t *cb_list, int error)
1195 {
1196 	dmu_tx_callback_t *dcb;
1197 
1198 	while (dcb = list_head(cb_list)) {
1199 		list_remove(cb_list, dcb);
1200 		dcb->dcb_func(dcb->dcb_data, error);
1201 		kmem_free(dcb, sizeof (dmu_tx_callback_t));
1202 	}
1203 }
1204