]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dmu_tx.c
Illumos #3954, #4080, #4081
[mirror_zfs.git] / module / zfs / dmu_tx.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
22cd4a46 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
295304be 24 * Copyright (c) 2013 by Delphix. All rights reserved.
22cd4a46 25 */
34dc7c2f 26
34dc7c2f
BB
27#include <sys/dmu.h>
28#include <sys/dmu_impl.h>
29#include <sys/dbuf.h>
30#include <sys/dmu_tx.h>
31#include <sys/dmu_objset.h>
32#include <sys/dsl_dataset.h> /* for dsl_dataset_block_freeable() */
33#include <sys/dsl_dir.h> /* for dsl_dir_tempreserve_*() */
34#include <sys/dsl_pool.h>
35#include <sys/zap_impl.h> /* for fzap_default_block_shift */
36#include <sys/spa.h>
428870ff
BB
37#include <sys/sa.h>
38#include <sys/sa_impl.h>
34dc7c2f 39#include <sys/zfs_context.h>
428870ff 40#include <sys/varargs.h>
34dc7c2f
BB
41
42typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
43 uint64_t arg1, uint64_t arg2);
44
570827e1
BB
45dmu_tx_stats_t dmu_tx_stats = {
46 { "dmu_tx_assigned", KSTAT_DATA_UINT64 },
47 { "dmu_tx_delay", KSTAT_DATA_UINT64 },
48 { "dmu_tx_error", KSTAT_DATA_UINT64 },
49 { "dmu_tx_suspended", KSTAT_DATA_UINT64 },
50 { "dmu_tx_group", KSTAT_DATA_UINT64 },
51 { "dmu_tx_how", KSTAT_DATA_UINT64 },
52 { "dmu_tx_memory_reserve", KSTAT_DATA_UINT64 },
53 { "dmu_tx_memory_reclaim", KSTAT_DATA_UINT64 },
54 { "dmu_tx_memory_inflight", KSTAT_DATA_UINT64 },
55 { "dmu_tx_dirty_throttle", KSTAT_DATA_UINT64 },
56 { "dmu_tx_write_limit", KSTAT_DATA_UINT64 },
57 { "dmu_tx_quota", KSTAT_DATA_UINT64 },
58};
59
60static kstat_t *dmu_tx_ksp;
34dc7c2f
BB
61
62dmu_tx_t *
63dmu_tx_create_dd(dsl_dir_t *dd)
64{
b8d06fca 65 dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_PUSHPAGE);
34dc7c2f 66 tx->tx_dir = dd;
6f1ffb06 67 if (dd != NULL)
34dc7c2f
BB
68 tx->tx_pool = dd->dd_pool;
69 list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
70 offsetof(dmu_tx_hold_t, txh_node));
428870ff
BB
71 list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
72 offsetof(dmu_tx_callback_t, dcb_node));
1c5de20a 73#ifdef DEBUG_DMU_TX
34dc7c2f
BB
74 refcount_create(&tx->tx_space_written);
75 refcount_create(&tx->tx_space_freed);
76#endif
77 return (tx);
78}
79
80dmu_tx_t *
81dmu_tx_create(objset_t *os)
82{
428870ff 83 dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
34dc7c2f 84 tx->tx_objset = os;
428870ff 85 tx->tx_lastsnap_txg = dsl_dataset_prev_snap_txg(os->os_dsl_dataset);
34dc7c2f
BB
86 return (tx);
87}
88
89dmu_tx_t *
90dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
91{
92 dmu_tx_t *tx = dmu_tx_create_dd(NULL);
93
94 ASSERT3U(txg, <=, dp->dp_tx.tx_open_txg);
95 tx->tx_pool = dp;
96 tx->tx_txg = txg;
97 tx->tx_anyobj = TRUE;
98
99 return (tx);
100}
101
102int
103dmu_tx_is_syncing(dmu_tx_t *tx)
104{
105 return (tx->tx_anyobj);
106}
107
108int
109dmu_tx_private_ok(dmu_tx_t *tx)
110{
111 return (tx->tx_anyobj);
112}
113
114static dmu_tx_hold_t *
115dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
116 enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
117{
118 dmu_tx_hold_t *txh;
119 dnode_t *dn = NULL;
120 int err;
121
122 if (object != DMU_NEW_OBJECT) {
428870ff 123 err = dnode_hold(os, object, tx, &dn);
34dc7c2f
BB
124 if (err) {
125 tx->tx_err = err;
126 return (NULL);
127 }
128
129 if (err == 0 && tx->tx_txg != 0) {
130 mutex_enter(&dn->dn_mtx);
131 /*
132 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
133 * problem, but there's no way for it to happen (for
134 * now, at least).
135 */
136 ASSERT(dn->dn_assigned_txg == 0);
137 dn->dn_assigned_txg = tx->tx_txg;
138 (void) refcount_add(&dn->dn_tx_holds, tx);
139 mutex_exit(&dn->dn_mtx);
140 }
141 }
142
b8d06fca 143 txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_PUSHPAGE);
34dc7c2f
BB
144 txh->txh_tx = tx;
145 txh->txh_dnode = dn;
1c5de20a 146#ifdef DEBUG_DMU_TX
34dc7c2f
BB
147 txh->txh_type = type;
148 txh->txh_arg1 = arg1;
149 txh->txh_arg2 = arg2;
150#endif
151 list_insert_tail(&tx->tx_holds, txh);
152
153 return (txh);
154}
155
156void
157dmu_tx_add_new_object(dmu_tx_t *tx, objset_t *os, uint64_t object)
158{
159 /*
160 * If we're syncing, they can manipulate any object anyhow, and
161 * the hold on the dnode_t can cause problems.
162 */
163 if (!dmu_tx_is_syncing(tx)) {
164 (void) dmu_tx_hold_object_impl(tx, os,
165 object, THT_NEWOBJECT, 0, 0);
166 }
167}
168
169static int
170dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
171{
172 int err;
173 dmu_buf_impl_t *db;
174
175 rw_enter(&dn->dn_struct_rwlock, RW_READER);
176 db = dbuf_hold_level(dn, level, blkid, FTAG);
177 rw_exit(&dn->dn_struct_rwlock);
178 if (db == NULL)
2e528b49 179 return (SET_ERROR(EIO));
34dc7c2f
BB
180 err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
181 dbuf_rele(db, FTAG);
182 return (err);
183}
184
9babb374 185static void
428870ff
BB
186dmu_tx_count_twig(dmu_tx_hold_t *txh, dnode_t *dn, dmu_buf_impl_t *db,
187 int level, uint64_t blkid, boolean_t freeable, uint64_t *history)
9babb374 188{
428870ff
BB
189 objset_t *os = dn->dn_objset;
190 dsl_dataset_t *ds = os->os_dsl_dataset;
191 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
192 dmu_buf_impl_t *parent = NULL;
193 blkptr_t *bp = NULL;
194 uint64_t space;
195
196 if (level >= dn->dn_nlevels || history[level] == blkid)
9babb374
BB
197 return;
198
428870ff 199 history[level] = blkid;
9babb374 200
428870ff
BB
201 space = (level == 0) ? dn->dn_datablksz : (1ULL << dn->dn_indblkshift);
202
203 if (db == NULL || db == dn->dn_dbuf) {
204 ASSERT(level != 0);
205 db = NULL;
206 } else {
572e2857 207 ASSERT(DB_DNODE(db) == dn);
428870ff
BB
208 ASSERT(db->db_level == level);
209 ASSERT(db->db.db_size == space);
210 ASSERT(db->db_blkid == blkid);
211 bp = db->db_blkptr;
212 parent = db->db_parent;
9babb374
BB
213 }
214
428870ff
BB
215 freeable = (bp && (freeable ||
216 dsl_dataset_block_freeable(ds, bp, bp->blk_birth)));
9babb374 217
428870ff
BB
218 if (freeable)
219 txh->txh_space_tooverwrite += space;
220 else
221 txh->txh_space_towrite += space;
222 if (bp)
223 txh->txh_space_tounref += bp_get_dsize(os->os_spa, bp);
224
225 dmu_tx_count_twig(txh, dn, parent, level + 1,
226 blkid >> epbs, freeable, history);
9babb374
BB
227}
228
34dc7c2f
BB
229/* ARGSUSED */
230static void
231dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
232{
233 dnode_t *dn = txh->txh_dnode;
234 uint64_t start, end, i;
235 int min_bs, max_bs, min_ibs, max_ibs, epbs, bits;
236 int err = 0;
d6320ddb 237 int l;
34dc7c2f
BB
238
239 if (len == 0)
240 return;
241
242 min_bs = SPA_MINBLOCKSHIFT;
243 max_bs = SPA_MAXBLOCKSHIFT;
244 min_ibs = DN_MIN_INDBLKSHIFT;
245 max_ibs = DN_MAX_INDBLKSHIFT;
246
34dc7c2f 247 if (dn) {
428870ff 248 uint64_t history[DN_MAX_LEVELS];
9babb374
BB
249 int nlvls = dn->dn_nlevels;
250 int delta;
251
252 /*
253 * For i/o error checking, read the first and last level-0
254 * blocks (if they are not aligned), and all the level-1 blocks.
255 */
34dc7c2f 256 if (dn->dn_maxblkid == 0) {
9babb374
BB
257 delta = dn->dn_datablksz;
258 start = (off < dn->dn_datablksz) ? 0 : 1;
259 end = (off+len <= dn->dn_datablksz) ? 0 : 1;
260 if (start == 0 && (off > 0 || len < dn->dn_datablksz)) {
b128c09f
BB
261 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
262 if (err)
263 goto out;
9babb374 264 delta -= off;
b128c09f 265 }
34dc7c2f
BB
266 } else {
267 zio_t *zio = zio_root(dn->dn_objset->os_spa,
268 NULL, NULL, ZIO_FLAG_CANFAIL);
269
270 /* first level-0 block */
271 start = off >> dn->dn_datablkshift;
272 if (P2PHASE(off, dn->dn_datablksz) ||
273 len < dn->dn_datablksz) {
274 err = dmu_tx_check_ioerr(zio, dn, 0, start);
275 if (err)
276 goto out;
277 }
278
279 /* last level-0 block */
280 end = (off+len-1) >> dn->dn_datablkshift;
b128c09f 281 if (end != start && end <= dn->dn_maxblkid &&
34dc7c2f
BB
282 P2PHASE(off+len, dn->dn_datablksz)) {
283 err = dmu_tx_check_ioerr(zio, dn, 0, end);
284 if (err)
285 goto out;
286 }
287
288 /* level-1 blocks */
9babb374
BB
289 if (nlvls > 1) {
290 int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
291 for (i = (start>>shft)+1; i < end>>shft; i++) {
34dc7c2f
BB
292 err = dmu_tx_check_ioerr(zio, dn, 1, i);
293 if (err)
294 goto out;
295 }
296 }
297
298 err = zio_wait(zio);
299 if (err)
300 goto out;
9babb374 301 delta = P2NPHASE(off, dn->dn_datablksz);
34dc7c2f 302 }
34dc7c2f 303
295304be 304 min_ibs = max_ibs = dn->dn_indblkshift;
9babb374
BB
305 if (dn->dn_maxblkid > 0) {
306 /*
307 * The blocksize can't change,
308 * so we can make a more precise estimate.
309 */
310 ASSERT(dn->dn_datablkshift != 0);
34dc7c2f 311 min_bs = max_bs = dn->dn_datablkshift;
9babb374
BB
312 }
313
314 /*
315 * If this write is not off the end of the file
316 * we need to account for overwrites/unref.
317 */
428870ff 318 if (start <= dn->dn_maxblkid) {
d6320ddb 319 for (l = 0; l < DN_MAX_LEVELS; l++)
428870ff
BB
320 history[l] = -1ULL;
321 }
9babb374 322 while (start <= dn->dn_maxblkid) {
9babb374
BB
323 dmu_buf_impl_t *db;
324
325 rw_enter(&dn->dn_struct_rwlock, RW_READER);
428870ff 326 err = dbuf_hold_impl(dn, 0, start, FALSE, FTAG, &db);
9babb374 327 rw_exit(&dn->dn_struct_rwlock);
428870ff
BB
328
329 if (err) {
330 txh->txh_tx->tx_err = err;
331 return;
9babb374 332 }
428870ff
BB
333
334 dmu_tx_count_twig(txh, dn, db, 0, start, B_FALSE,
335 history);
9babb374
BB
336 dbuf_rele(db, FTAG);
337 if (++start > end) {
338 /*
339 * Account for new indirects appearing
340 * before this IO gets assigned into a txg.
341 */
342 bits = 64 - min_bs;
343 epbs = min_ibs - SPA_BLKPTRSHIFT;
344 for (bits -= epbs * (nlvls - 1);
345 bits >= 0; bits -= epbs)
346 txh->txh_fudge += 1ULL << max_ibs;
347 goto out;
348 }
349 off += delta;
350 if (len >= delta)
351 len -= delta;
352 delta = dn->dn_datablksz;
353 }
34dc7c2f
BB
354 }
355
356 /*
357 * 'end' is the last thing we will access, not one past.
358 * This way we won't overflow when accessing the last byte.
359 */
360 start = P2ALIGN(off, 1ULL << max_bs);
361 end = P2ROUNDUP(off + len, 1ULL << max_bs) - 1;
362 txh->txh_space_towrite += end - start + 1;
363
364 start >>= min_bs;
365 end >>= min_bs;
366
367 epbs = min_ibs - SPA_BLKPTRSHIFT;
368
369 /*
370 * The object contains at most 2^(64 - min_bs) blocks,
371 * and each indirect level maps 2^epbs.
372 */
373 for (bits = 64 - min_bs; bits >= 0; bits -= epbs) {
374 start >>= epbs;
375 end >>= epbs;
9babb374 376 ASSERT3U(end, >=, start);
34dc7c2f 377 txh->txh_space_towrite += (end - start + 1) << max_ibs;
9babb374
BB
378 if (start != 0) {
379 /*
380 * We also need a new blkid=0 indirect block
381 * to reference any existing file data.
382 */
383 txh->txh_space_towrite += 1ULL << max_ibs;
384 }
34dc7c2f
BB
385 }
386
34dc7c2f 387out:
9babb374
BB
388 if (txh->txh_space_towrite + txh->txh_space_tooverwrite >
389 2 * DMU_MAX_ACCESS)
2e528b49 390 err = SET_ERROR(EFBIG);
9babb374 391
34dc7c2f
BB
392 if (err)
393 txh->txh_tx->tx_err = err;
394}
395
396static void
397dmu_tx_count_dnode(dmu_tx_hold_t *txh)
398{
399 dnode_t *dn = txh->txh_dnode;
572e2857 400 dnode_t *mdn = DMU_META_DNODE(txh->txh_tx->tx_objset);
34dc7c2f
BB
401 uint64_t space = mdn->dn_datablksz +
402 ((mdn->dn_nlevels-1) << mdn->dn_indblkshift);
403
404 if (dn && dn->dn_dbuf->db_blkptr &&
405 dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
428870ff 406 dn->dn_dbuf->db_blkptr, dn->dn_dbuf->db_blkptr->blk_birth)) {
34dc7c2f 407 txh->txh_space_tooverwrite += space;
9babb374 408 txh->txh_space_tounref += space;
34dc7c2f
BB
409 } else {
410 txh->txh_space_towrite += space;
411 if (dn && dn->dn_dbuf->db_blkptr)
412 txh->txh_space_tounref += space;
413 }
414}
415
416void
417dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
418{
419 dmu_tx_hold_t *txh;
420
421 ASSERT(tx->tx_txg == 0);
422 ASSERT(len < DMU_MAX_ACCESS);
423 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
424
425 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
426 object, THT_WRITE, off, len);
427 if (txh == NULL)
428 return;
429
430 dmu_tx_count_write(txh, off, len);
431 dmu_tx_count_dnode(txh);
432}
433
434static void
435dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
436{
b128c09f
BB
437 uint64_t blkid, nblks, lastblk;
438 uint64_t space = 0, unref = 0, skipped = 0;
34dc7c2f
BB
439 dnode_t *dn = txh->txh_dnode;
440 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
441 spa_t *spa = txh->txh_tx->tx_pool->dp_spa;
b128c09f 442 int epbs;
ff80d9b1 443 uint64_t l0span = 0, nl1blks = 0;
34dc7c2f 444
b128c09f 445 if (dn->dn_nlevels == 0)
34dc7c2f
BB
446 return;
447
448 /*
b128c09f 449 * The struct_rwlock protects us against dn_nlevels
34dc7c2f
BB
450 * changing, in case (against all odds) we manage to dirty &
451 * sync out the changes after we check for being dirty.
428870ff 452 * Also, dbuf_hold_impl() wants us to have the struct_rwlock.
34dc7c2f
BB
453 */
454 rw_enter(&dn->dn_struct_rwlock, RW_READER);
b128c09f
BB
455 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
456 if (dn->dn_maxblkid == 0) {
34dc7c2f
BB
457 if (off == 0 && len >= dn->dn_datablksz) {
458 blkid = 0;
459 nblks = 1;
460 } else {
461 rw_exit(&dn->dn_struct_rwlock);
462 return;
463 }
464 } else {
465 blkid = off >> dn->dn_datablkshift;
b128c09f 466 nblks = (len + dn->dn_datablksz - 1) >> dn->dn_datablkshift;
34dc7c2f 467
383fc4a9 468 if (blkid > dn->dn_maxblkid) {
34dc7c2f
BB
469 rw_exit(&dn->dn_struct_rwlock);
470 return;
471 }
b128c09f 472 if (blkid + nblks > dn->dn_maxblkid)
383fc4a9 473 nblks = dn->dn_maxblkid - blkid + 1;
34dc7c2f 474
34dc7c2f 475 }
ff80d9b1 476 l0span = nblks; /* save for later use to calc level > 1 overhead */
b128c09f 477 if (dn->dn_nlevels == 1) {
34dc7c2f
BB
478 int i;
479 for (i = 0; i < nblks; i++) {
480 blkptr_t *bp = dn->dn_phys->dn_blkptr;
b128c09f 481 ASSERT3U(blkid + i, <, dn->dn_nblkptr);
34dc7c2f 482 bp += blkid + i;
428870ff 483 if (dsl_dataset_block_freeable(ds, bp, bp->blk_birth)) {
34dc7c2f 484 dprintf_bp(bp, "can free old%s", "");
428870ff 485 space += bp_get_dsize(spa, bp);
34dc7c2f
BB
486 }
487 unref += BP_GET_ASIZE(bp);
488 }
ff80d9b1 489 nl1blks = 1;
34dc7c2f
BB
490 nblks = 0;
491 }
492
b128c09f 493 lastblk = blkid + nblks - 1;
34dc7c2f
BB
494 while (nblks) {
495 dmu_buf_impl_t *dbuf;
b128c09f
BB
496 uint64_t ibyte, new_blkid;
497 int epb = 1 << epbs;
498 int err, i, blkoff, tochk;
499 blkptr_t *bp;
500
501 ibyte = blkid << dn->dn_datablkshift;
502 err = dnode_next_offset(dn,
503 DNODE_FIND_HAVELOCK, &ibyte, 2, 1, 0);
504 new_blkid = ibyte >> dn->dn_datablkshift;
505 if (err == ESRCH) {
506 skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
507 break;
508 }
509 if (err) {
510 txh->txh_tx->tx_err = err;
511 break;
512 }
513 if (new_blkid > lastblk) {
514 skipped += (lastblk >> epbs) - (blkid >> epbs) + 1;
515 break;
516 }
34dc7c2f 517
b128c09f
BB
518 if (new_blkid > blkid) {
519 ASSERT((new_blkid >> epbs) > (blkid >> epbs));
520 skipped += (new_blkid >> epbs) - (blkid >> epbs) - 1;
521 nblks -= new_blkid - blkid;
522 blkid = new_blkid;
523 }
524 blkoff = P2PHASE(blkid, epb);
525 tochk = MIN(epb - blkoff, nblks);
34dc7c2f 526
428870ff
BB
527 err = dbuf_hold_impl(dn, 1, blkid >> epbs, FALSE, FTAG, &dbuf);
528 if (err) {
529 txh->txh_tx->tx_err = err;
b128c09f 530 break;
34dc7c2f 531 }
428870ff
BB
532
533 txh->txh_memory_tohold += dbuf->db.db_size;
534
535 /*
536 * We don't check memory_tohold against DMU_MAX_ACCESS because
537 * memory_tohold is an over-estimation (especially the >L1
538 * indirect blocks), so it could fail. Callers should have
539 * already verified that they will not be holding too much
540 * memory.
541 */
542
b128c09f
BB
543 err = dbuf_read(dbuf, NULL, DB_RF_HAVESTRUCT | DB_RF_CANFAIL);
544 if (err != 0) {
34dc7c2f 545 txh->txh_tx->tx_err = err;
b128c09f 546 dbuf_rele(dbuf, FTAG);
34dc7c2f
BB
547 break;
548 }
549
b128c09f
BB
550 bp = dbuf->db.db_data;
551 bp += blkoff;
552
553 for (i = 0; i < tochk; i++) {
428870ff
BB
554 if (dsl_dataset_block_freeable(ds, &bp[i],
555 bp[i].blk_birth)) {
b128c09f 556 dprintf_bp(&bp[i], "can free old%s", "");
428870ff 557 space += bp_get_dsize(spa, &bp[i]);
b128c09f
BB
558 }
559 unref += BP_GET_ASIZE(bp);
560 }
561 dbuf_rele(dbuf, FTAG);
562
ff80d9b1 563 ++nl1blks;
34dc7c2f
BB
564 blkid += tochk;
565 nblks -= tochk;
566 }
567 rw_exit(&dn->dn_struct_rwlock);
568
ff80d9b1
AJ
569 /*
570 * Add in memory requirements of higher-level indirects.
571 * This assumes a worst-possible scenario for dn_nlevels and a
572 * worst-possible distribution of l1-blocks over the region to free.
573 */
574 {
575 uint64_t blkcnt = 1 + ((l0span >> epbs) >> epbs);
576 int level = 2;
577 /*
578 * Here we don't use DN_MAX_LEVEL, but calculate it with the
579 * given datablkshift and indblkshift. This makes the
580 * difference between 19 and 8 on large files.
581 */
582 int maxlevel = 2 + (DN_MAX_OFFSET_SHIFT - dn->dn_datablkshift) /
583 (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
584
585 while (level++ < maxlevel) {
b077fd4c 586 txh->txh_memory_tohold += MAX(MIN(blkcnt, nl1blks), 1)
ff80d9b1
AJ
587 << dn->dn_indblkshift;
588 blkcnt = 1 + (blkcnt >> epbs);
589 }
590 }
591
b128c09f
BB
592 /* account for new level 1 indirect blocks that might show up */
593 if (skipped > 0) {
594 txh->txh_fudge += skipped << dn->dn_indblkshift;
595 skipped = MIN(skipped, DMU_MAX_DELETEBLKCNT >> epbs);
596 txh->txh_memory_tohold += skipped << dn->dn_indblkshift;
597 }
34dc7c2f
BB
598 txh->txh_space_tofree += space;
599 txh->txh_space_tounref += unref;
600}
601
602void
603dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
604{
605 dmu_tx_hold_t *txh;
606 dnode_t *dn;
ea97f8ce 607 int err;
34dc7c2f
BB
608 zio_t *zio;
609
610 ASSERT(tx->tx_txg == 0);
611
612 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
613 object, THT_FREE, off, len);
614 if (txh == NULL)
615 return;
616 dn = txh->txh_dnode;
617
34dc7c2f
BB
618 if (off >= (dn->dn_maxblkid+1) * dn->dn_datablksz)
619 return;
620 if (len == DMU_OBJECT_END)
621 len = (dn->dn_maxblkid+1) * dn->dn_datablksz - off;
622
ea97f8ce
MA
623 dmu_tx_count_dnode(txh);
624
34dc7c2f 625 /*
ea97f8ce
MA
626 * For i/o error checking, we read the first and last level-0
627 * blocks if they are not aligned, and all the level-1 blocks.
628 *
629 * Note: dbuf_free_range() assumes that we have not instantiated
630 * any level-0 dbufs that will be completely freed. Therefore we must
631 * exercise care to not read or count the first and last blocks
632 * if they are blocksize-aligned.
633 */
634 if (dn->dn_datablkshift == 0) {
b663a23d
MA
635 if (off != 0 || len < dn->dn_datablksz)
636 dmu_tx_count_write(txh, off, len);
ea97f8ce
MA
637 } else {
638 /* first block will be modified if it is not aligned */
639 if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
640 dmu_tx_count_write(txh, off, 1);
641 /* last block will be modified if it is not aligned */
642 if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
643 dmu_tx_count_write(txh, off+len, 1);
644 }
645
646 /*
647 * Check level-1 blocks.
34dc7c2f
BB
648 */
649 if (dn->dn_nlevels > 1) {
ea97f8ce 650 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
34dc7c2f 651 SPA_BLKPTRSHIFT;
ea97f8ce
MA
652 uint64_t start = off >> shift;
653 uint64_t end = (off + len) >> shift;
654 uint64_t i;
655
656 ASSERT(dn->dn_datablkshift != 0);
657 ASSERT(dn->dn_indblkshift != 0);
34dc7c2f
BB
658
659 zio = zio_root(tx->tx_pool->dp_spa,
660 NULL, NULL, ZIO_FLAG_CANFAIL);
661 for (i = start; i <= end; i++) {
662 uint64_t ibyte = i << shift;
b128c09f 663 err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
34dc7c2f
BB
664 i = ibyte >> shift;
665 if (err == ESRCH)
666 break;
667 if (err) {
668 tx->tx_err = err;
669 return;
670 }
671
672 err = dmu_tx_check_ioerr(zio, dn, 1, i);
673 if (err) {
674 tx->tx_err = err;
675 return;
676 }
677 }
678 err = zio_wait(zio);
679 if (err) {
680 tx->tx_err = err;
681 return;
682 }
683 }
684
34dc7c2f
BB
685 dmu_tx_count_free(txh, off, len);
686}
687
688void
9babb374 689dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
34dc7c2f
BB
690{
691 dmu_tx_hold_t *txh;
692 dnode_t *dn;
693 uint64_t nblocks;
694 int epbs, err;
695
696 ASSERT(tx->tx_txg == 0);
697
698 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
699 object, THT_ZAP, add, (uintptr_t)name);
700 if (txh == NULL)
701 return;
702 dn = txh->txh_dnode;
703
704 dmu_tx_count_dnode(txh);
705
706 if (dn == NULL) {
707 /*
708 * We will be able to fit a new object's entries into one leaf
709 * block. So there will be at most 2 blocks total,
710 * including the header block.
711 */
712 dmu_tx_count_write(txh, 0, 2 << fzap_default_block_shift);
713 return;
714 }
715
9ae529ec 716 ASSERT3U(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
34dc7c2f
BB
717
718 if (dn->dn_maxblkid == 0 && !add) {
22cd4a46
AL
719 blkptr_t *bp;
720
34dc7c2f
BB
721 /*
722 * If there is only one block (i.e. this is a micro-zap)
723 * and we are not adding anything, the accounting is simple.
724 */
725 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
726 if (err) {
727 tx->tx_err = err;
728 return;
729 }
730
731 /*
732 * Use max block size here, since we don't know how much
733 * the size will change between now and the dbuf dirty call.
734 */
22cd4a46 735 bp = &dn->dn_phys->dn_blkptr[0];
34dc7c2f 736 if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
22cd4a46 737 bp, bp->blk_birth))
34dc7c2f 738 txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
22cd4a46 739 else
34dc7c2f 740 txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
22cd4a46 741 if (!BP_IS_HOLE(bp))
9babb374 742 txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
34dc7c2f
BB
743 return;
744 }
745
746 if (dn->dn_maxblkid > 0 && name) {
747 /*
748 * access the name in this fat-zap so that we'll check
749 * for i/o errors to the leaf blocks, etc.
750 */
428870ff 751 err = zap_lookup(dn->dn_objset, dn->dn_object, name,
34dc7c2f
BB
752 8, 0, NULL);
753 if (err == EIO) {
754 tx->tx_err = err;
755 return;
756 }
757 }
758
428870ff 759 err = zap_count_write(dn->dn_objset, dn->dn_object, name, add,
45d1cae3 760 &txh->txh_space_towrite, &txh->txh_space_tooverwrite);
34dc7c2f
BB
761
762 /*
763 * If the modified blocks are scattered to the four winds,
764 * we'll have to modify an indirect twig for each.
765 */
766 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
767 for (nblocks = dn->dn_maxblkid >> epbs; nblocks != 0; nblocks >>= epbs)
9babb374
BB
768 if (dn->dn_objset->os_dsl_dataset->ds_phys->ds_prev_snap_obj)
769 txh->txh_space_towrite += 3 << dn->dn_indblkshift;
770 else
771 txh->txh_space_tooverwrite += 3 << dn->dn_indblkshift;
34dc7c2f
BB
772}
773
774void
775dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
776{
777 dmu_tx_hold_t *txh;
778
779 ASSERT(tx->tx_txg == 0);
780
781 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
782 object, THT_BONUS, 0, 0);
783 if (txh)
784 dmu_tx_count_dnode(txh);
785}
786
787void
788dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
789{
790 dmu_tx_hold_t *txh;
7d637211 791
34dc7c2f
BB
792 ASSERT(tx->tx_txg == 0);
793
794 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
795 DMU_NEW_OBJECT, THT_SPACE, space, 0);
7d637211
NC
796 if (txh)
797 txh->txh_space_towrite += space;
34dc7c2f
BB
798}
799
800int
801dmu_tx_holds(dmu_tx_t *tx, uint64_t object)
802{
803 dmu_tx_hold_t *txh;
804 int holds = 0;
805
806 /*
807 * By asserting that the tx is assigned, we're counting the
808 * number of dn_tx_holds, which is the same as the number of
809 * dn_holds. Otherwise, we'd be counting dn_holds, but
810 * dn_tx_holds could be 0.
811 */
812 ASSERT(tx->tx_txg != 0);
813
814 /* if (tx->tx_anyobj == TRUE) */
815 /* return (0); */
816
817 for (txh = list_head(&tx->tx_holds); txh;
818 txh = list_next(&tx->tx_holds, txh)) {
819 if (txh->txh_dnode && txh->txh_dnode->dn_object == object)
820 holds++;
821 }
822
823 return (holds);
824}
825
1c5de20a 826#ifdef DEBUG_DMU_TX
34dc7c2f
BB
827void
828dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
829{
830 dmu_tx_hold_t *txh;
831 int match_object = FALSE, match_offset = FALSE;
572e2857 832 dnode_t *dn;
34dc7c2f 833
572e2857
BB
834 DB_DNODE_ENTER(db);
835 dn = DB_DNODE(db);
99ea23c5 836 ASSERT(dn != NULL);
34dc7c2f 837 ASSERT(tx->tx_txg != 0);
428870ff 838 ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
34dc7c2f
BB
839 ASSERT3U(dn->dn_object, ==, db->db.db_object);
840
572e2857
BB
841 if (tx->tx_anyobj) {
842 DB_DNODE_EXIT(db);
34dc7c2f 843 return;
572e2857 844 }
34dc7c2f
BB
845
846 /* XXX No checking on the meta dnode for now */
572e2857
BB
847 if (db->db.db_object == DMU_META_DNODE_OBJECT) {
848 DB_DNODE_EXIT(db);
34dc7c2f 849 return;
572e2857 850 }
34dc7c2f
BB
851
852 for (txh = list_head(&tx->tx_holds); txh;
853 txh = list_next(&tx->tx_holds, txh)) {
99ea23c5 854 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
34dc7c2f
BB
855 if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
856 match_object = TRUE;
857 if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
858 int datablkshift = dn->dn_datablkshift ?
859 dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
860 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
861 int shift = datablkshift + epbs * db->db_level;
862 uint64_t beginblk = shift >= 64 ? 0 :
863 (txh->txh_arg1 >> shift);
864 uint64_t endblk = shift >= 64 ? 0 :
865 ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
866 uint64_t blkid = db->db_blkid;
867
868 /* XXX txh_arg2 better not be zero... */
869
870 dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
871 txh->txh_type, beginblk, endblk);
872
873 switch (txh->txh_type) {
874 case THT_WRITE:
875 if (blkid >= beginblk && blkid <= endblk)
876 match_offset = TRUE;
877 /*
878 * We will let this hold work for the bonus
428870ff
BB
879 * or spill buffer so that we don't need to
880 * hold it when creating a new object.
34dc7c2f 881 */
428870ff
BB
882 if (blkid == DMU_BONUS_BLKID ||
883 blkid == DMU_SPILL_BLKID)
34dc7c2f
BB
884 match_offset = TRUE;
885 /*
886 * They might have to increase nlevels,
887 * thus dirtying the new TLIBs. Or the
888 * might have to change the block size,
889 * thus dirying the new lvl=0 blk=0.
890 */
891 if (blkid == 0)
892 match_offset = TRUE;
893 break;
894 case THT_FREE:
b128c09f
BB
895 /*
896 * We will dirty all the level 1 blocks in
897 * the free range and perhaps the first and
898 * last level 0 block.
899 */
900 if (blkid >= beginblk && (blkid <= endblk ||
901 txh->txh_arg2 == DMU_OBJECT_END))
34dc7c2f
BB
902 match_offset = TRUE;
903 break;
428870ff
BB
904 case THT_SPILL:
905 if (blkid == DMU_SPILL_BLKID)
906 match_offset = TRUE;
907 break;
34dc7c2f 908 case THT_BONUS:
428870ff 909 if (blkid == DMU_BONUS_BLKID)
34dc7c2f
BB
910 match_offset = TRUE;
911 break;
912 case THT_ZAP:
913 match_offset = TRUE;
914 break;
915 case THT_NEWOBJECT:
916 match_object = TRUE;
917 break;
918 default:
919 ASSERT(!"bad txh_type");
920 }
921 }
572e2857
BB
922 if (match_object && match_offset) {
923 DB_DNODE_EXIT(db);
34dc7c2f 924 return;
572e2857 925 }
34dc7c2f 926 }
572e2857 927 DB_DNODE_EXIT(db);
34dc7c2f
BB
928 panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
929 (u_longlong_t)db->db.db_object, db->db_level,
930 (u_longlong_t)db->db_blkid);
931}
932#endif
933
934static int
13fe0198 935dmu_tx_try_assign(dmu_tx_t *tx, txg_how_t txg_how)
34dc7c2f
BB
936{
937 dmu_tx_hold_t *txh;
938 spa_t *spa = tx->tx_pool->dp_spa;
b128c09f
BB
939 uint64_t memory, asize, fsize, usize;
940 uint64_t towrite, tofree, tooverwrite, tounref, tohold, fudge;
34dc7c2f 941
c99c9001 942 ASSERT0(tx->tx_txg);
34dc7c2f 943
570827e1
BB
944 if (tx->tx_err) {
945 DMU_TX_STAT_BUMP(dmu_tx_error);
34dc7c2f 946 return (tx->tx_err);
570827e1 947 }
34dc7c2f 948
b128c09f 949 if (spa_suspended(spa)) {
570827e1
BB
950 DMU_TX_STAT_BUMP(dmu_tx_suspended);
951
34dc7c2f
BB
952 /*
953 * If the user has indicated a blocking failure mode
954 * then return ERESTART which will block in dmu_tx_wait().
955 * Otherwise, return EIO so that an error can get
956 * propagated back to the VOP calls.
957 *
958 * Note that we always honor the txg_how flag regardless
959 * of the failuremode setting.
960 */
961 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
962 txg_how != TXG_WAIT)
2e528b49 963 return (SET_ERROR(EIO));
34dc7c2f 964
2e528b49 965 return (SET_ERROR(ERESTART));
34dc7c2f
BB
966 }
967
968 tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
969 tx->tx_needassign_txh = NULL;
970
971 /*
972 * NB: No error returns are allowed after txg_hold_open, but
973 * before processing the dnode holds, due to the
974 * dmu_tx_unassign() logic.
975 */
976
b128c09f 977 towrite = tofree = tooverwrite = tounref = tohold = fudge = 0;
34dc7c2f
BB
978 for (txh = list_head(&tx->tx_holds); txh;
979 txh = list_next(&tx->tx_holds, txh)) {
980 dnode_t *dn = txh->txh_dnode;
981 if (dn != NULL) {
982 mutex_enter(&dn->dn_mtx);
983 if (dn->dn_assigned_txg == tx->tx_txg - 1) {
984 mutex_exit(&dn->dn_mtx);
985 tx->tx_needassign_txh = txh;
570827e1 986 DMU_TX_STAT_BUMP(dmu_tx_group);
2e528b49 987 return (SET_ERROR(ERESTART));
34dc7c2f
BB
988 }
989 if (dn->dn_assigned_txg == 0)
990 dn->dn_assigned_txg = tx->tx_txg;
991 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
992 (void) refcount_add(&dn->dn_tx_holds, tx);
993 mutex_exit(&dn->dn_mtx);
994 }
995 towrite += txh->txh_space_towrite;
996 tofree += txh->txh_space_tofree;
997 tooverwrite += txh->txh_space_tooverwrite;
998 tounref += txh->txh_space_tounref;
b128c09f
BB
999 tohold += txh->txh_memory_tohold;
1000 fudge += txh->txh_fudge;
34dc7c2f
BB
1001 }
1002
34dc7c2f
BB
1003 /*
1004 * If a snapshot has been taken since we made our estimates,
1005 * assume that we won't be able to free or overwrite anything.
1006 */
1007 if (tx->tx_objset &&
428870ff 1008 dsl_dataset_prev_snap_txg(tx->tx_objset->os_dsl_dataset) >
34dc7c2f
BB
1009 tx->tx_lastsnap_txg) {
1010 towrite += tooverwrite;
1011 tooverwrite = tofree = 0;
1012 }
1013
b128c09f
BB
1014 /* needed allocation: worst-case estimate of write space */
1015 asize = spa_get_asize(tx->tx_pool->dp_spa, towrite + tooverwrite);
1016 /* freed space estimate: worst-case overwrite + free estimate */
34dc7c2f 1017 fsize = spa_get_asize(tx->tx_pool->dp_spa, tooverwrite) + tofree;
b128c09f 1018 /* convert unrefd space to worst-case estimate */
34dc7c2f 1019 usize = spa_get_asize(tx->tx_pool->dp_spa, tounref);
b128c09f
BB
1020 /* calculate memory footprint estimate */
1021 memory = towrite + tooverwrite + tohold;
34dc7c2f 1022
1c5de20a 1023#ifdef DEBUG_DMU_TX
b128c09f
BB
1024 /*
1025 * Add in 'tohold' to account for our dirty holds on this memory
1026 * XXX - the "fudge" factor is to account for skipped blocks that
1027 * we missed because dnode_next_offset() misses in-core-only blocks.
1028 */
1029 tx->tx_space_towrite = asize +
1030 spa_get_asize(tx->tx_pool->dp_spa, tohold + fudge);
34dc7c2f
BB
1031 tx->tx_space_tofree = tofree;
1032 tx->tx_space_tooverwrite = tooverwrite;
1033 tx->tx_space_tounref = tounref;
1034#endif
1035
1036 if (tx->tx_dir && asize != 0) {
b128c09f
BB
1037 int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1038 asize, fsize, usize, &tx->tx_tempreserve_cookie, tx);
34dc7c2f
BB
1039 if (err)
1040 return (err);
1041 }
1042
570827e1
BB
1043 DMU_TX_STAT_BUMP(dmu_tx_assigned);
1044
34dc7c2f
BB
1045 return (0);
1046}
1047
1048static void
1049dmu_tx_unassign(dmu_tx_t *tx)
1050{
1051 dmu_tx_hold_t *txh;
1052
1053 if (tx->tx_txg == 0)
1054 return;
1055
1056 txg_rele_to_quiesce(&tx->tx_txgh);
1057
e49f1e20
WA
1058 /*
1059 * Walk the transaction's hold list, removing the hold on the
1060 * associated dnode, and notifying waiters if the refcount drops to 0.
1061 */
34dc7c2f
BB
1062 for (txh = list_head(&tx->tx_holds); txh != tx->tx_needassign_txh;
1063 txh = list_next(&tx->tx_holds, txh)) {
1064 dnode_t *dn = txh->txh_dnode;
1065
1066 if (dn == NULL)
1067 continue;
1068 mutex_enter(&dn->dn_mtx);
1069 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1070
1071 if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1072 dn->dn_assigned_txg = 0;
1073 cv_broadcast(&dn->dn_notxholds);
1074 }
1075 mutex_exit(&dn->dn_mtx);
1076 }
1077
1078 txg_rele_to_sync(&tx->tx_txgh);
1079
1080 tx->tx_lasttried_txg = tx->tx_txg;
1081 tx->tx_txg = 0;
1082}
1083
1084/*
1085 * Assign tx to a transaction group. txg_how can be one of:
1086 *
1087 * (1) TXG_WAIT. If the current open txg is full, waits until there's
1088 * a new one. This should be used when you're not holding locks.
13fe0198 1089 * It will only fail if we're truly out of space (or over quota).
34dc7c2f
BB
1090 *
1091 * (2) TXG_NOWAIT. If we can't assign into the current open txg without
1092 * blocking, returns immediately with ERESTART. This should be used
1093 * whenever you're holding locks. On an ERESTART error, the caller
1094 * should drop locks, do a dmu_tx_wait(tx), and try again.
34dc7c2f
BB
1095 */
1096int
13fe0198 1097dmu_tx_assign(dmu_tx_t *tx, txg_how_t txg_how)
34dc7c2f 1098{
2d37239a 1099 hrtime_t before;
34dc7c2f
BB
1100 int err;
1101
1102 ASSERT(tx->tx_txg == 0);
13fe0198 1103 ASSERT(txg_how == TXG_WAIT || txg_how == TXG_NOWAIT);
34dc7c2f
BB
1104 ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1105
2d37239a
BB
1106 before = gethrtime();
1107
13fe0198
MA
1108 /* If we might wait, we must not hold the config lock. */
1109 ASSERT(txg_how != TXG_WAIT || !dsl_pool_config_held(tx->tx_pool));
1110
34dc7c2f
BB
1111 while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
1112 dmu_tx_unassign(tx);
1113
1114 if (err != ERESTART || txg_how != TXG_WAIT)
1115 return (err);
1116
1117 dmu_tx_wait(tx);
1118 }
1119
1120 txg_rele_to_quiesce(&tx->tx_txgh);
1121
2d37239a
BB
1122 spa_tx_assign_add_nsecs(tx->tx_pool->dp_spa, gethrtime() - before);
1123
34dc7c2f
BB
1124 return (0);
1125}
1126
1127void
1128dmu_tx_wait(dmu_tx_t *tx)
1129{
1130 spa_t *spa = tx->tx_pool->dp_spa;
1131
1132 ASSERT(tx->tx_txg == 0);
13fe0198 1133 ASSERT(!dsl_pool_config_held(tx->tx_pool));
34dc7c2f
BB
1134
1135 /*
1136 * It's possible that the pool has become active after this thread
1137 * has tried to obtain a tx. If that's the case then his
1138 * tx_lasttried_txg would not have been assigned.
1139 */
b128c09f 1140 if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
34dc7c2f
BB
1141 txg_wait_synced(tx->tx_pool, spa_last_synced_txg(spa) + 1);
1142 } else if (tx->tx_needassign_txh) {
1143 dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1144
1145 mutex_enter(&dn->dn_mtx);
1146 while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1147 cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1148 mutex_exit(&dn->dn_mtx);
1149 tx->tx_needassign_txh = NULL;
1150 } else {
1151 txg_wait_open(tx->tx_pool, tx->tx_lasttried_txg + 1);
1152 }
1153}
1154
1155void
1156dmu_tx_willuse_space(dmu_tx_t *tx, int64_t delta)
1157{
1c5de20a 1158#ifdef DEBUG_DMU_TX
34dc7c2f
BB
1159 if (tx->tx_dir == NULL || delta == 0)
1160 return;
1161
1162 if (delta > 0) {
1163 ASSERT3U(refcount_count(&tx->tx_space_written) + delta, <=,
1164 tx->tx_space_towrite);
1165 (void) refcount_add_many(&tx->tx_space_written, delta, NULL);
1166 } else {
1167 (void) refcount_add_many(&tx->tx_space_freed, -delta, NULL);
1168 }
1169#endif
1170}
1171
1172void
1173dmu_tx_commit(dmu_tx_t *tx)
1174{
1175 dmu_tx_hold_t *txh;
1176
1177 ASSERT(tx->tx_txg != 0);
1178
e49f1e20
WA
1179 /*
1180 * Go through the transaction's hold list and remove holds on
1181 * associated dnodes, notifying waiters if no holds remain.
1182 */
c65aa5b2 1183 while ((txh = list_head(&tx->tx_holds))) {
34dc7c2f
BB
1184 dnode_t *dn = txh->txh_dnode;
1185
1186 list_remove(&tx->tx_holds, txh);
1187 kmem_free(txh, sizeof (dmu_tx_hold_t));
1188 if (dn == NULL)
1189 continue;
1190 mutex_enter(&dn->dn_mtx);
1191 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1192
1193 if (refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1194 dn->dn_assigned_txg = 0;
1195 cv_broadcast(&dn->dn_notxholds);
1196 }
1197 mutex_exit(&dn->dn_mtx);
1198 dnode_rele(dn, tx);
1199 }
1200
1201 if (tx->tx_tempreserve_cookie)
1202 dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1203
428870ff
BB
1204 if (!list_is_empty(&tx->tx_callbacks))
1205 txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1206
34dc7c2f
BB
1207 if (tx->tx_anyobj == FALSE)
1208 txg_rele_to_sync(&tx->tx_txgh);
428870ff
BB
1209
1210 list_destroy(&tx->tx_callbacks);
34dc7c2f 1211 list_destroy(&tx->tx_holds);
1c5de20a 1212#ifdef DEBUG_DMU_TX
34dc7c2f
BB
1213 dprintf("towrite=%llu written=%llu tofree=%llu freed=%llu\n",
1214 tx->tx_space_towrite, refcount_count(&tx->tx_space_written),
1215 tx->tx_space_tofree, refcount_count(&tx->tx_space_freed));
1216 refcount_destroy_many(&tx->tx_space_written,
1217 refcount_count(&tx->tx_space_written));
1218 refcount_destroy_many(&tx->tx_space_freed,
1219 refcount_count(&tx->tx_space_freed));
1220#endif
1221 kmem_free(tx, sizeof (dmu_tx_t));
1222}
1223
1224void
1225dmu_tx_abort(dmu_tx_t *tx)
1226{
1227 dmu_tx_hold_t *txh;
1228
1229 ASSERT(tx->tx_txg == 0);
1230
c65aa5b2 1231 while ((txh = list_head(&tx->tx_holds))) {
34dc7c2f
BB
1232 dnode_t *dn = txh->txh_dnode;
1233
1234 list_remove(&tx->tx_holds, txh);
1235 kmem_free(txh, sizeof (dmu_tx_hold_t));
1236 if (dn != NULL)
1237 dnode_rele(dn, tx);
1238 }
428870ff
BB
1239
1240 /*
1241 * Call any registered callbacks with an error code.
1242 */
1243 if (!list_is_empty(&tx->tx_callbacks))
1244 dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1245
1246 list_destroy(&tx->tx_callbacks);
34dc7c2f 1247 list_destroy(&tx->tx_holds);
1c5de20a 1248#ifdef DEBUG_DMU_TX
34dc7c2f
BB
1249 refcount_destroy_many(&tx->tx_space_written,
1250 refcount_count(&tx->tx_space_written));
1251 refcount_destroy_many(&tx->tx_space_freed,
1252 refcount_count(&tx->tx_space_freed));
1253#endif
1254 kmem_free(tx, sizeof (dmu_tx_t));
1255}
1256
1257uint64_t
1258dmu_tx_get_txg(dmu_tx_t *tx)
1259{
1260 ASSERT(tx->tx_txg != 0);
1261 return (tx->tx_txg);
1262}
428870ff 1263
13fe0198
MA
1264dsl_pool_t *
1265dmu_tx_pool(dmu_tx_t *tx)
1266{
1267 ASSERT(tx->tx_pool != NULL);
1268 return (tx->tx_pool);
1269}
1270
1271
428870ff
BB
1272void
1273dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1274{
1275 dmu_tx_callback_t *dcb;
1276
b8d06fca 1277 dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_PUSHPAGE);
428870ff
BB
1278
1279 dcb->dcb_func = func;
1280 dcb->dcb_data = data;
1281
1282 list_insert_tail(&tx->tx_callbacks, dcb);
1283}
1284
1285/*
1286 * Call all the commit callbacks on a list, with a given error code.
1287 */
1288void
1289dmu_tx_do_callbacks(list_t *cb_list, int error)
1290{
1291 dmu_tx_callback_t *dcb;
1292
c65aa5b2 1293 while ((dcb = list_head(cb_list))) {
428870ff
BB
1294 list_remove(cb_list, dcb);
1295 dcb->dcb_func(dcb->dcb_data, error);
1296 kmem_free(dcb, sizeof (dmu_tx_callback_t));
1297 }
1298}
1299
1300/*
1301 * Interface to hold a bunch of attributes.
1302 * used for creating new files.
1303 * attrsize is the total size of all attributes
1304 * to be added during object creation
1305 *
1306 * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
1307 */
1308
1309/*
1310 * hold necessary attribute name for attribute registration.
1311 * should be a very rare case where this is needed. If it does
1312 * happen it would only happen on the first write to the file system.
1313 */
1314static void
1315dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
1316{
1317 int i;
1318
1319 if (!sa->sa_need_attr_registration)
1320 return;
1321
1322 for (i = 0; i != sa->sa_num_attrs; i++) {
1323 if (!sa->sa_attr_table[i].sa_registered) {
1324 if (sa->sa_reg_attr_obj)
1325 dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
1326 B_TRUE, sa->sa_attr_table[i].sa_name);
1327 else
1328 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
1329 B_TRUE, sa->sa_attr_table[i].sa_name);
1330 }
1331 }
1332}
1333
1334
1335void
1336dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
1337{
1338 dnode_t *dn;
1339 dmu_tx_hold_t *txh;
428870ff
BB
1340
1341 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
1342 THT_SPILL, 0, 0);
7d637211
NC
1343 if (txh == NULL)
1344 return;
428870ff
BB
1345
1346 dn = txh->txh_dnode;
1347
1348 if (dn == NULL)
1349 return;
1350
1351 /* If blkptr doesn't exist then add space to towrite */
22cd4a46 1352 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
428870ff 1353 txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
428870ff 1354 } else {
22cd4a46
AL
1355 blkptr_t *bp;
1356
1357 bp = &dn->dn_phys->dn_spill;
428870ff
BB
1358 if (dsl_dataset_block_freeable(dn->dn_objset->os_dsl_dataset,
1359 bp, bp->blk_birth))
1360 txh->txh_space_tooverwrite += SPA_MAXBLOCKSIZE;
1361 else
1362 txh->txh_space_towrite += SPA_MAXBLOCKSIZE;
22cd4a46 1363 if (!BP_IS_HOLE(bp))
428870ff
BB
1364 txh->txh_space_tounref += SPA_MAXBLOCKSIZE;
1365 }
1366}
1367
1368void
1369dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
1370{
1371 sa_os_t *sa = tx->tx_objset->os_sa;
1372
1373 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1374
1375 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1376 return;
1377
1378 if (tx->tx_objset->os_sa->sa_layout_attr_obj)
1379 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1380 else {
1381 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1382 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1383 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1384 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1385 }
1386
1387 dmu_tx_sa_registration_hold(sa, tx);
1388
1389 if (attrsize <= DN_MAX_BONUSLEN && !sa->sa_force_spill)
1390 return;
1391
1392 (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
1393 THT_SPILL, 0, 0);
1394}
1395
1396/*
1397 * Hold SA attribute
1398 *
1399 * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
1400 *
1401 * variable_size is the total size of all variable sized attributes
1402 * passed to this function. It is not the total size of all
1403 * variable size attributes that *may* exist on this object.
1404 */
1405void
1406dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
1407{
1408 uint64_t object;
1409 sa_os_t *sa = tx->tx_objset->os_sa;
1410
1411 ASSERT(hdl != NULL);
1412
1413 object = sa_handle_object(hdl);
1414
1415 dmu_tx_hold_bonus(tx, object);
1416
1417 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1418 return;
1419
1420 if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
1421 tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
1422 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1423 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1424 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1425 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1426 }
1427
1428 dmu_tx_sa_registration_hold(sa, tx);
1429
1430 if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
1431 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1432
572e2857 1433 if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
428870ff
BB
1434 ASSERT(tx->tx_txg == 0);
1435 dmu_tx_hold_spill(tx, object);
572e2857
BB
1436 } else {
1437 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1438 dnode_t *dn;
1439
1440 DB_DNODE_ENTER(db);
1441 dn = DB_DNODE(db);
1442 if (dn->dn_have_spill) {
1443 ASSERT(tx->tx_txg == 0);
1444 dmu_tx_hold_spill(tx, object);
1445 }
1446 DB_DNODE_EXIT(db);
428870ff
BB
1447 }
1448}
c28b2279 1449
570827e1
BB
1450void
1451dmu_tx_init(void)
1452{
1453 dmu_tx_ksp = kstat_create("zfs", 0, "dmu_tx", "misc",
1454 KSTAT_TYPE_NAMED, sizeof (dmu_tx_stats) / sizeof (kstat_named_t),
1455 KSTAT_FLAG_VIRTUAL);
1456
1457 if (dmu_tx_ksp != NULL) {
1458 dmu_tx_ksp->ks_data = &dmu_tx_stats;
1459 kstat_install(dmu_tx_ksp);
1460 }
1461}
1462
1463void
1464dmu_tx_fini(void)
1465{
1466 if (dmu_tx_ksp != NULL) {
1467 kstat_delete(dmu_tx_ksp);
1468 dmu_tx_ksp = NULL;
1469 }
1470}
1471
c28b2279
BB
1472#if defined(_KERNEL) && defined(HAVE_SPL)
1473EXPORT_SYMBOL(dmu_tx_create);
1474EXPORT_SYMBOL(dmu_tx_hold_write);
1475EXPORT_SYMBOL(dmu_tx_hold_free);
1476EXPORT_SYMBOL(dmu_tx_hold_zap);
1477EXPORT_SYMBOL(dmu_tx_hold_bonus);
1478EXPORT_SYMBOL(dmu_tx_abort);
1479EXPORT_SYMBOL(dmu_tx_assign);
1480EXPORT_SYMBOL(dmu_tx_wait);
1481EXPORT_SYMBOL(dmu_tx_commit);
1482EXPORT_SYMBOL(dmu_tx_get_txg);
1483EXPORT_SYMBOL(dmu_tx_callback_register);
1484EXPORT_SYMBOL(dmu_tx_do_callbacks);
1485EXPORT_SYMBOL(dmu_tx_hold_spill);
1486EXPORT_SYMBOL(dmu_tx_hold_sa_create);
1487EXPORT_SYMBOL(dmu_tx_hold_sa);
1488#endif