1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
26 * Copyright (c) 2024, 2025, Klara, Inc.
27 */
28
29 #include <sys/dmu.h>
30 #include <sys/dmu_impl.h>
31 #include <sys/dbuf.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/zap_impl.h>
38 #include <sys/spa.h>
39 #include <sys/brt_impl.h>
40 #include <sys/sa.h>
41 #include <sys/sa_impl.h>
42 #include <sys/zfs_context.h>
43 #include <sys/trace_zfs.h>
44
45 typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
46 uint64_t arg1, uint64_t arg2);
47
48 dmu_tx_stats_t dmu_tx_stats = {
49 { "dmu_tx_assigned", KSTAT_DATA_UINT64 },
50 { "dmu_tx_delay", KSTAT_DATA_UINT64 },
51 { "dmu_tx_error", KSTAT_DATA_UINT64 },
52 { "dmu_tx_suspended", KSTAT_DATA_UINT64 },
53 { "dmu_tx_group", KSTAT_DATA_UINT64 },
54 { "dmu_tx_memory_reserve", KSTAT_DATA_UINT64 },
55 { "dmu_tx_memory_reclaim", KSTAT_DATA_UINT64 },
56 { "dmu_tx_dirty_throttle", KSTAT_DATA_UINT64 },
57 { "dmu_tx_dirty_delay", KSTAT_DATA_UINT64 },
58 { "dmu_tx_dirty_over_max", KSTAT_DATA_UINT64 },
59 { "dmu_tx_dirty_frees_delay", KSTAT_DATA_UINT64 },
60 { "dmu_tx_wrlog_delay", KSTAT_DATA_UINT64 },
61 { "dmu_tx_quota", KSTAT_DATA_UINT64 },
62 };
63
64 static kstat_t *dmu_tx_ksp;
65
66 dmu_tx_t *
dmu_tx_create_dd(dsl_dir_t * dd)67 dmu_tx_create_dd(dsl_dir_t *dd)
68 {
69 dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
70 tx->tx_dir = dd;
71 if (dd != NULL)
72 tx->tx_pool = dd->dd_pool;
73 list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
74 offsetof(dmu_tx_hold_t, txh_node));
75 list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
76 offsetof(dmu_tx_callback_t, dcb_node));
77 tx->tx_start = gethrtime();
78 return (tx);
79 }
80
81 dmu_tx_t *
dmu_tx_create(objset_t * os)82 dmu_tx_create(objset_t *os)
83 {
84 dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
85 tx->tx_objset = os;
86 return (tx);
87 }
88
89 dmu_tx_t *
dmu_tx_create_assigned(struct dsl_pool * dp,uint64_t txg)90 dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
91 {
92 dmu_tx_t *tx = dmu_tx_create_dd(NULL);
93
94 TXG_VERIFY(dp->dp_spa, txg);
95 tx->tx_pool = dp;
96 tx->tx_txg = txg;
97 tx->tx_anyobj = TRUE;
98
99 return (tx);
100 }
101
102 int
dmu_tx_is_syncing(dmu_tx_t * tx)103 dmu_tx_is_syncing(dmu_tx_t *tx)
104 {
105 return (tx->tx_anyobj);
106 }
107
108 int
dmu_tx_private_ok(dmu_tx_t * tx)109 dmu_tx_private_ok(dmu_tx_t *tx)
110 {
111 return (tx->tx_anyobj);
112 }
113
114 static dmu_tx_hold_t *
dmu_tx_hold_dnode_impl(dmu_tx_t * tx,dnode_t * dn,enum dmu_tx_hold_type type,uint64_t arg1,uint64_t arg2)115 dmu_tx_hold_dnode_impl(dmu_tx_t *tx, dnode_t *dn, enum dmu_tx_hold_type type,
116 uint64_t arg1, uint64_t arg2)
117 {
118 dmu_tx_hold_t *txh;
119
120 if (dn != NULL) {
121 (void) zfs_refcount_add(&dn->dn_holds, tx);
122 if (tx->tx_txg != 0) {
123 mutex_enter(&dn->dn_mtx);
124 /*
125 * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
126 * problem, but there's no way for it to happen (for
127 * now, at least).
128 */
129 ASSERT0(dn->dn_assigned_txg);
130 dn->dn_assigned_txg = tx->tx_txg;
131 (void) zfs_refcount_add(&dn->dn_tx_holds, tx);
132 mutex_exit(&dn->dn_mtx);
133 }
134 }
135
136 txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
137 txh->txh_tx = tx;
138 txh->txh_dnode = dn;
139 zfs_refcount_create(&txh->txh_space_towrite);
140 zfs_refcount_create(&txh->txh_memory_tohold);
141 txh->txh_type = type;
142 txh->txh_arg1 = arg1;
143 txh->txh_arg2 = arg2;
144 list_insert_tail(&tx->tx_holds, txh);
145
146 return (txh);
147 }
148
149 static dmu_tx_hold_t *
dmu_tx_hold_object_impl(dmu_tx_t * tx,objset_t * os,uint64_t object,enum dmu_tx_hold_type type,uint64_t arg1,uint64_t arg2)150 dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
151 enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
152 {
153 dnode_t *dn = NULL;
154 dmu_tx_hold_t *txh;
155 int err;
156
157 if (object != DMU_NEW_OBJECT) {
158 err = dnode_hold(os, object, FTAG, &dn);
159 if (err != 0) {
160 tx->tx_err = err;
161 return (NULL);
162 }
163 }
164 txh = dmu_tx_hold_dnode_impl(tx, dn, type, arg1, arg2);
165 if (dn != NULL)
166 dnode_rele(dn, FTAG);
167 return (txh);
168 }
169
170 void
dmu_tx_add_new_object(dmu_tx_t * tx,dnode_t * dn)171 dmu_tx_add_new_object(dmu_tx_t *tx, dnode_t *dn)
172 {
173 /*
174 * If we're syncing, they can manipulate any object anyhow, and
175 * the hold on the dnode_t can cause problems.
176 */
177 if (!dmu_tx_is_syncing(tx))
178 (void) dmu_tx_hold_dnode_impl(tx, dn, THT_NEWOBJECT, 0, 0);
179 }
180
181 /*
182 * This function reads specified data from disk. The specified data will
183 * be needed to perform the transaction -- i.e, it will be read after
184 * we do dmu_tx_assign(). There are two reasons that we read the data now
185 * (before dmu_tx_assign()):
186 *
187 * 1. Reading it now has potentially better performance. The transaction
188 * has not yet been assigned, so the TXG is not held open, and also the
189 * caller typically has less locks held when calling dmu_tx_hold_*() than
190 * after the transaction has been assigned. This reduces the lock (and txg)
191 * hold times, thus reducing lock contention.
192 *
193 * 2. It is easier for callers (primarily the ZPL) to handle i/o errors
194 * that are detected before they start making changes to the DMU state
195 * (i.e. now). Once the transaction has been assigned, and some DMU
196 * state has been changed, it can be difficult to recover from an i/o
197 * error (e.g. to undo the changes already made in memory at the DMU
198 * layer). Typically code to do so does not exist in the caller -- it
199 * assumes that the data has already been cached and thus i/o errors are
200 * not possible.
201 *
202 * It has been observed that the i/o initiated here can be a performance
203 * problem, and it appears to be optional, because we don't look at the
204 * data which is read. However, removing this read would only serve to
205 * move the work elsewhere (after the dmu_tx_assign()), where it may
206 * have a greater impact on performance (in addition to the impact on
207 * fault tolerance noted above).
208 */
209 static int
dmu_tx_check_ioerr(zio_t * zio,dnode_t * dn,int level,uint64_t blkid)210 dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
211 {
212 int err;
213 dmu_buf_impl_t *db;
214
215 rw_enter(&dn->dn_struct_rwlock, RW_READER);
216 err = dbuf_hold_impl(dn, level, blkid, TRUE, FALSE, FTAG, &db);
217 rw_exit(&dn->dn_struct_rwlock);
218 if (err == ENOENT)
219 return (0);
220 if (err != 0)
221 return (err);
222 /*
223 * DMU_IS_PREFETCH keeps the buffer temporarily in DBUF cache and ARC
224 * to avoid immediate eviction after the check. It will be promoted
225 * to demand access when dmu_buf_will_dirty() read it again.
226 */
227 err = dbuf_read(db, zio, DB_RF_CANFAIL | DMU_READ_NO_PREFETCH |
228 (level == 0 ? (DMU_KEEP_CACHING | DMU_IS_PREFETCH) : 0));
229 dbuf_rele(db, FTAG);
230 return (err);
231 }
232
233 static void
dmu_tx_count_write(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)234 dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
235 {
236 dnode_t *dn = txh->txh_dnode;
237 int err = 0;
238
239 if (len == 0)
240 return;
241
242 (void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG);
243
244 if (dn == NULL)
245 return;
246
247 /*
248 * For i/o error checking, read the blocks that will be needed
249 * to perform the write: the first and last level-0 blocks (if
250 * they are not aligned, i.e. if they are partial-block writes),
251 * and all the level-1 blocks.
252 */
253 if (dn->dn_maxblkid == 0) {
254 if (off < dn->dn_datablksz &&
255 (off > 0 || len < dn->dn_datablksz)) {
256 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
257 if (err != 0) {
258 txh->txh_tx->tx_err = err;
259 }
260 }
261 } else {
262 zio_t *zio = zio_root(dn->dn_objset->os_spa,
263 NULL, NULL, ZIO_FLAG_CANFAIL);
264
265 /* first level-0 block */
266 uint64_t start = off >> dn->dn_datablkshift;
267 if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) {
268 err = dmu_tx_check_ioerr(zio, dn, 0, start);
269 if (err != 0) {
270 txh->txh_tx->tx_err = err;
271 }
272 }
273
274 /* last level-0 block */
275 uint64_t end = (off + len - 1) >> dn->dn_datablkshift;
276 if (end != start && end <= dn->dn_maxblkid &&
277 P2PHASE(off + len, dn->dn_datablksz)) {
278 err = dmu_tx_check_ioerr(zio, dn, 0, end);
279 if (err != 0) {
280 txh->txh_tx->tx_err = err;
281 }
282 }
283
284 /* level-1 blocks */
285 if (dn->dn_nlevels > 1) {
286 int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
287 for (uint64_t i = (start >> shft) + 1;
288 i < end >> shft; i++) {
289 err = dmu_tx_check_ioerr(zio, dn, 1, i);
290 if (err != 0) {
291 txh->txh_tx->tx_err = err;
292 }
293 }
294 }
295
296 err = zio_wait(zio);
297 if (err != 0) {
298 txh->txh_tx->tx_err = err;
299 }
300 }
301 }
302
303 static void
dmu_tx_count_append(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)304 dmu_tx_count_append(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
305 {
306 dnode_t *dn = txh->txh_dnode;
307 int err = 0;
308
309 if (len == 0)
310 return;
311
312 (void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG);
313
314 if (dn == NULL)
315 return;
316
317 /*
318 * For i/o error checking, read the blocks that will be needed
319 * to perform the append; first level-0 block (if not aligned, i.e.
320 * if they are partial-block writes), no additional blocks are read.
321 */
322 if (dn->dn_maxblkid == 0) {
323 if (off < dn->dn_datablksz &&
324 (off > 0 || len < dn->dn_datablksz)) {
325 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
326 if (err != 0) {
327 txh->txh_tx->tx_err = err;
328 }
329 }
330 } else {
331 zio_t *zio = zio_root(dn->dn_objset->os_spa,
332 NULL, NULL, ZIO_FLAG_CANFAIL);
333
334 /* first level-0 block */
335 uint64_t start = off >> dn->dn_datablkshift;
336 if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) {
337 err = dmu_tx_check_ioerr(zio, dn, 0, start);
338 if (err != 0) {
339 txh->txh_tx->tx_err = err;
340 }
341 }
342
343 err = zio_wait(zio);
344 if (err != 0) {
345 txh->txh_tx->tx_err = err;
346 }
347 }
348 }
349
350 static void
dmu_tx_count_dnode(dmu_tx_hold_t * txh)351 dmu_tx_count_dnode(dmu_tx_hold_t *txh)
352 {
353 (void) zfs_refcount_add_many(&txh->txh_space_towrite,
354 DNODE_MIN_SIZE, FTAG);
355 }
356
357 void
dmu_tx_hold_write(dmu_tx_t * tx,uint64_t object,uint64_t off,int len)358 dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
359 {
360 dmu_tx_hold_t *txh;
361
362 ASSERT0(tx->tx_txg);
363 ASSERT3U(len, <=, DMU_MAX_ACCESS);
364 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
365
366 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
367 object, THT_WRITE, off, len);
368 if (txh != NULL) {
369 dmu_tx_count_write(txh, off, len);
370 dmu_tx_count_dnode(txh);
371 }
372 }
373
374 void
dmu_tx_hold_write_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,int len)375 dmu_tx_hold_write_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
376 {
377 dmu_tx_hold_t *txh;
378
379 ASSERT0(tx->tx_txg);
380 ASSERT3U(len, <=, DMU_MAX_ACCESS);
381 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
382
383 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_WRITE, off, len);
384 if (txh != NULL) {
385 dmu_tx_count_write(txh, off, len);
386 dmu_tx_count_dnode(txh);
387 }
388 }
389
390 /*
391 * Should be used when appending to an object and the exact offset is unknown.
392 * The write must occur at or beyond the specified offset. Only the L0 block
393 * at provided offset will be prefetched.
394 */
395 void
dmu_tx_hold_append(dmu_tx_t * tx,uint64_t object,uint64_t off,int len)396 dmu_tx_hold_append(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
397 {
398 dmu_tx_hold_t *txh;
399
400 ASSERT0(tx->tx_txg);
401 ASSERT3U(len, <=, DMU_MAX_ACCESS);
402
403 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
404 object, THT_APPEND, off, DMU_OBJECT_END);
405 if (txh != NULL) {
406 dmu_tx_count_append(txh, off, len);
407 dmu_tx_count_dnode(txh);
408 }
409 }
410
411 void
dmu_tx_hold_append_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,int len)412 dmu_tx_hold_append_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
413 {
414 dmu_tx_hold_t *txh;
415
416 ASSERT0(tx->tx_txg);
417 ASSERT3U(len, <=, DMU_MAX_ACCESS);
418
419 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_APPEND, off, DMU_OBJECT_END);
420 if (txh != NULL) {
421 dmu_tx_count_append(txh, off, len);
422 dmu_tx_count_dnode(txh);
423 }
424 }
425
426 /*
427 * This function marks the transaction as being a "net free". The end
428 * result is that refquotas will be disabled for this transaction, and
429 * this transaction will be able to use half of the pool space overhead
430 * (see dsl_pool_adjustedsize()). Therefore this function should only
431 * be called for transactions that we expect will not cause a net increase
432 * in the amount of space used (but it's OK if that is occasionally not true).
433 */
434 void
dmu_tx_mark_netfree(dmu_tx_t * tx)435 dmu_tx_mark_netfree(dmu_tx_t *tx)
436 {
437 tx->tx_netfree = B_TRUE;
438 }
439
440 static void
dmu_tx_count_free(dmu_tx_hold_t * txh,uint64_t off,uint64_t len)441 dmu_tx_count_free(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
442 {
443 dmu_tx_t *tx = txh->txh_tx;
444 dnode_t *dn = txh->txh_dnode;
445 int err;
446
447 ASSERT0(tx->tx_txg);
448
449 if (off >= (dn->dn_maxblkid + 1) * dn->dn_datablksz)
450 return;
451 if (len == DMU_OBJECT_END)
452 len = (dn->dn_maxblkid + 1) * dn->dn_datablksz - off;
453
454 /*
455 * For i/o error checking, we read the first and last level-0
456 * blocks if they are not aligned, and all the level-1 blocks.
457 *
458 * Note: dbuf_free_range() assumes that we have not instantiated
459 * any level-0 dbufs that will be completely freed. Therefore we must
460 * exercise care to not read or count the first and last blocks
461 * if they are blocksize-aligned.
462 */
463 if (dn->dn_datablkshift == 0) {
464 if (off != 0 || len < dn->dn_datablksz)
465 dmu_tx_count_write(txh, 0, dn->dn_datablksz);
466 } else {
467 /* first block will be modified if it is not aligned */
468 if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
469 dmu_tx_count_write(txh, off, 1);
470 /* last block will be modified if it is not aligned */
471 if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
472 dmu_tx_count_write(txh, off + len, 1);
473 }
474
475 /*
476 * Check level-1 blocks.
477 */
478 if (dn->dn_nlevels > 1) {
479 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
480 SPA_BLKPTRSHIFT;
481 uint64_t start = off >> shift;
482 uint64_t end = (off + len) >> shift;
483
484 ASSERT(dn->dn_indblkshift != 0);
485
486 /*
487 * dnode_reallocate() can result in an object with indirect
488 * blocks having an odd data block size. In this case,
489 * just check the single block.
490 */
491 if (dn->dn_datablkshift == 0)
492 start = end = 0;
493
494 zio_t *zio = zio_root(tx->tx_pool->dp_spa,
495 NULL, NULL, ZIO_FLAG_CANFAIL);
496 for (uint64_t i = start; i <= end; i++) {
497 uint64_t ibyte = i << shift;
498 err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
499 i = ibyte >> shift;
500 if (err == ESRCH || i > end)
501 break;
502 if (err != 0) {
503 tx->tx_err = err;
504 (void) zio_wait(zio);
505 return;
506 }
507
508 (void) zfs_refcount_add_many(&txh->txh_memory_tohold,
509 1 << dn->dn_indblkshift, FTAG);
510
511 err = dmu_tx_check_ioerr(zio, dn, 1, i);
512 if (err != 0) {
513 tx->tx_err = err;
514 (void) zio_wait(zio);
515 return;
516 }
517 }
518 err = zio_wait(zio);
519 if (err != 0) {
520 tx->tx_err = err;
521 return;
522 }
523 }
524 }
525
526 void
dmu_tx_hold_free(dmu_tx_t * tx,uint64_t object,uint64_t off,uint64_t len)527 dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
528 {
529 dmu_tx_hold_t *txh;
530
531 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
532 object, THT_FREE, off, len);
533 if (txh != NULL) {
534 dmu_tx_count_dnode(txh);
535 dmu_tx_count_free(txh, off, len);
536 }
537 }
538
539 void
dmu_tx_hold_free_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,uint64_t len)540 dmu_tx_hold_free_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len)
541 {
542 dmu_tx_hold_t *txh;
543
544 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_FREE, off, len);
545 if (txh != NULL) {
546 dmu_tx_count_dnode(txh);
547 dmu_tx_count_free(txh, off, len);
548 }
549 }
550
551 static void
dmu_tx_count_clone(dmu_tx_hold_t * txh,uint64_t off,uint64_t len,uint_t blksz)552 dmu_tx_count_clone(dmu_tx_hold_t *txh, uint64_t off, uint64_t len,
553 uint_t blksz)
554 {
555 dmu_tx_t *tx = txh->txh_tx;
556 dnode_t *dn = txh->txh_dnode;
557 int err;
558
559 ASSERT0(tx->tx_txg);
560 ASSERT(dn->dn_indblkshift != 0);
561 ASSERT(blksz != 0);
562 ASSERT0(off % blksz);
563
564 (void) zfs_refcount_add_many(&txh->txh_memory_tohold,
565 len / blksz * sizeof (brt_entry_t), FTAG);
566
567 int shift = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
568 uint64_t start = off / blksz >> shift;
569 uint64_t end = (off + len) / blksz >> shift;
570
571 (void) zfs_refcount_add_many(&txh->txh_space_towrite,
572 (end - start + 1) << dn->dn_indblkshift, FTAG);
573
574 zio_t *zio = zio_root(tx->tx_pool->dp_spa,
575 NULL, NULL, ZIO_FLAG_CANFAIL);
576 for (uint64_t i = start; i <= end; i++) {
577 err = dmu_tx_check_ioerr(zio, dn, 1, i);
578 if (err != 0) {
579 tx->tx_err = err;
580 break;
581 }
582 }
583 err = zio_wait(zio);
584 if (err != 0)
585 tx->tx_err = err;
586 }
587
588 void
dmu_tx_hold_clone_by_dnode(dmu_tx_t * tx,dnode_t * dn,uint64_t off,uint64_t len,uint_t blksz)589 dmu_tx_hold_clone_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off,
590 uint64_t len, uint_t blksz)
591 {
592 dmu_tx_hold_t *txh;
593
594 ASSERT0(tx->tx_txg);
595 ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
596
597 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_CLONE, off, len);
598 if (txh != NULL) {
599 dmu_tx_count_dnode(txh);
600 dmu_tx_count_clone(txh, off, len, blksz);
601 }
602 }
603
604 static void
dmu_tx_hold_zap_impl(dmu_tx_hold_t * txh,const char * name)605 dmu_tx_hold_zap_impl(dmu_tx_hold_t *txh, const char *name)
606 {
607 dmu_tx_t *tx = txh->txh_tx;
608 dnode_t *dn = txh->txh_dnode;
609 int err;
610
611 ASSERT0(tx->tx_txg);
612
613 dmu_tx_count_dnode(txh);
614
615 /*
616 * Modifying a almost-full microzap is around the worst case (128KB)
617 *
618 * If it is a fat zap, the worst case would be 7*16KB=112KB:
619 * - 3 blocks overwritten: target leaf, ptrtbl block, header block
620 * - 4 new blocks written if adding:
621 * - 2 blocks for possibly split leaves,
622 * - 2 grown ptrtbl blocks
623 */
624 (void) zfs_refcount_add_many(&txh->txh_space_towrite,
625 zap_get_micro_max_size(tx->tx_pool->dp_spa), FTAG);
626
627 if (dn == NULL)
628 return;
629
630 ASSERT3U(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
631
632 if (dn->dn_maxblkid == 0 || name == NULL) {
633 /*
634 * This is a microzap (only one block), or we don't know
635 * the name. Check the first block for i/o errors.
636 */
637 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
638 if (err != 0) {
639 tx->tx_err = err;
640 }
641 } else {
642 /*
643 * Access the name so that we'll check for i/o errors to
644 * the leaf blocks, etc. We ignore ENOENT, as this name
645 * may not yet exist.
646 */
647 err = zap_lookup_by_dnode(dn, name, 8, 0, NULL);
648 if (err == EIO || err == ECKSUM || err == ENXIO) {
649 tx->tx_err = err;
650 }
651 }
652 }
653
654 void
dmu_tx_hold_zap(dmu_tx_t * tx,uint64_t object,int add,const char * name)655 dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
656 {
657 dmu_tx_hold_t *txh;
658
659 ASSERT0(tx->tx_txg);
660
661 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
662 object, THT_ZAP, add, (uintptr_t)name);
663 if (txh != NULL)
664 dmu_tx_hold_zap_impl(txh, name);
665 }
666
667 void
dmu_tx_hold_zap_by_dnode(dmu_tx_t * tx,dnode_t * dn,int add,const char * name)668 dmu_tx_hold_zap_by_dnode(dmu_tx_t *tx, dnode_t *dn, int add, const char *name)
669 {
670 dmu_tx_hold_t *txh;
671
672 ASSERT0(tx->tx_txg);
673 ASSERT(dn != NULL);
674
675 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_ZAP, add, (uintptr_t)name);
676 if (txh != NULL)
677 dmu_tx_hold_zap_impl(txh, name);
678 }
679
680 void
dmu_tx_hold_bonus(dmu_tx_t * tx,uint64_t object)681 dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
682 {
683 dmu_tx_hold_t *txh;
684
685 ASSERT0(tx->tx_txg);
686
687 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
688 object, THT_BONUS, 0, 0);
689 if (txh)
690 dmu_tx_count_dnode(txh);
691 }
692
693 void
dmu_tx_hold_bonus_by_dnode(dmu_tx_t * tx,dnode_t * dn)694 dmu_tx_hold_bonus_by_dnode(dmu_tx_t *tx, dnode_t *dn)
695 {
696 dmu_tx_hold_t *txh;
697
698 ASSERT0(tx->tx_txg);
699
700 txh = dmu_tx_hold_dnode_impl(tx, dn, THT_BONUS, 0, 0);
701 if (txh)
702 dmu_tx_count_dnode(txh);
703 }
704
705 void
dmu_tx_hold_space(dmu_tx_t * tx,uint64_t space)706 dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
707 {
708 dmu_tx_hold_t *txh;
709
710 ASSERT0(tx->tx_txg);
711
712 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
713 DMU_NEW_OBJECT, THT_SPACE, space, 0);
714 if (txh) {
715 (void) zfs_refcount_add_many(
716 &txh->txh_space_towrite, space, FTAG);
717 }
718 }
719
720 #ifdef ZFS_DEBUG
721 void
dmu_tx_dirty_buf(dmu_tx_t * tx,dmu_buf_impl_t * db)722 dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
723 {
724 boolean_t match_object = B_FALSE;
725 boolean_t match_offset = B_FALSE;
726
727 DB_DNODE_ENTER(db);
728 dnode_t *dn = DB_DNODE(db);
729 ASSERT(tx->tx_txg != 0);
730 ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
731 ASSERT3U(dn->dn_object, ==, db->db.db_object);
732
733 if (tx->tx_anyobj) {
734 DB_DNODE_EXIT(db);
735 return;
736 }
737
738 /* XXX No checking on the meta dnode for now */
739 if (db->db.db_object == DMU_META_DNODE_OBJECT) {
740 DB_DNODE_EXIT(db);
741 return;
742 }
743
744 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
745 txh = list_next(&tx->tx_holds, txh)) {
746 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
747 if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
748 match_object = TRUE;
749 if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
750 int datablkshift = dn->dn_datablkshift ?
751 dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
752 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
753 int shift = datablkshift + epbs * db->db_level;
754 uint64_t beginblk = shift >= 64 ? 0 :
755 (txh->txh_arg1 >> shift);
756 uint64_t endblk = shift >= 64 ? 0 :
757 ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
758 uint64_t blkid = db->db_blkid;
759
760 /* XXX txh_arg2 better not be zero... */
761
762 dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
763 txh->txh_type, (u_longlong_t)beginblk,
764 (u_longlong_t)endblk);
765
766 switch (txh->txh_type) {
767 case THT_WRITE:
768 if (blkid >= beginblk && blkid <= endblk)
769 match_offset = TRUE;
770 /*
771 * We will let this hold work for the bonus
772 * or spill buffer so that we don't need to
773 * hold it when creating a new object.
774 */
775 if (blkid == DMU_BONUS_BLKID ||
776 blkid == DMU_SPILL_BLKID)
777 match_offset = TRUE;
778 /*
779 * They might have to increase nlevels,
780 * thus dirtying the new TLIBs. Or the
781 * might have to change the block size,
782 * thus dirying the new lvl=0 blk=0.
783 */
784 if (blkid == 0)
785 match_offset = TRUE;
786 break;
787 case THT_APPEND:
788 if (blkid >= beginblk && (blkid <= endblk ||
789 txh->txh_arg2 == DMU_OBJECT_END))
790 match_offset = TRUE;
791
792 /*
793 * THT_WRITE used for bonus and spill blocks.
794 */
795 ASSERT(blkid != DMU_BONUS_BLKID &&
796 blkid != DMU_SPILL_BLKID);
797
798 /*
799 * They might have to increase nlevels,
800 * thus dirtying the new TLIBs. Or the
801 * might have to change the block size,
802 * thus dirying the new lvl=0 blk=0.
803 */
804 if (blkid == 0)
805 match_offset = TRUE;
806 break;
807 case THT_FREE:
808 /*
809 * We will dirty all the level 1 blocks in
810 * the free range and perhaps the first and
811 * last level 0 block.
812 */
813 if (blkid >= beginblk && (blkid <= endblk ||
814 txh->txh_arg2 == DMU_OBJECT_END))
815 match_offset = TRUE;
816 break;
817 case THT_SPILL:
818 if (blkid == DMU_SPILL_BLKID)
819 match_offset = TRUE;
820 break;
821 case THT_BONUS:
822 if (blkid == DMU_BONUS_BLKID)
823 match_offset = TRUE;
824 break;
825 case THT_ZAP:
826 match_offset = TRUE;
827 break;
828 case THT_NEWOBJECT:
829 match_object = TRUE;
830 break;
831 case THT_CLONE:
832 if (blkid >= beginblk && blkid <= endblk)
833 match_offset = TRUE;
834 /*
835 * They might have to increase nlevels,
836 * thus dirtying the new TLIBs. Or the
837 * might have to change the block size,
838 * thus dirying the new lvl=0 blk=0.
839 */
840 if (blkid == 0)
841 match_offset = TRUE;
842 break;
843 default:
844 cmn_err(CE_PANIC, "bad txh_type %d",
845 txh->txh_type);
846 }
847 }
848 if (match_object && match_offset) {
849 DB_DNODE_EXIT(db);
850 return;
851 }
852 }
853 DB_DNODE_EXIT(db);
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 /*
861 * If we can't do 10 iops, something is wrong. Let us go ahead
862 * and hit zfs_dirty_data_max.
863 */
864 static const hrtime_t zfs_delay_max_ns = 100 * MICROSEC; /* 100 milliseconds */
865
866 /*
867 * We delay transactions when we've determined that the backend storage
868 * isn't able to accommodate the rate of incoming writes.
869 *
870 * If there is already a transaction waiting, we delay relative to when
871 * that transaction finishes waiting. This way the calculated min_time
872 * is independent of the number of threads concurrently executing
873 * transactions.
874 *
875 * If we are the only waiter, wait relative to when the transaction
876 * started, rather than the current time. This credits the transaction for
877 * "time already served", e.g. reading indirect blocks.
878 *
879 * The minimum time for a transaction to take is calculated as:
880 * min_time = scale * (dirty - min) / (max - dirty)
881 * min_time is then capped at zfs_delay_max_ns.
882 *
883 * The delay has two degrees of freedom that can be adjusted via tunables.
884 * The percentage of dirty data at which we start to delay is defined by
885 * zfs_delay_min_dirty_percent. This should typically be at or above
886 * zfs_vdev_async_write_active_max_dirty_percent so that we only start to
887 * delay after writing at full speed has failed to keep up with the incoming
888 * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly
889 * speaking, this variable determines the amount of delay at the midpoint of
890 * the curve.
891 *
892 * delay
893 * 10ms +-------------------------------------------------------------*+
894 * | *|
895 * 9ms + *+
896 * | *|
897 * 8ms + *+
898 * | * |
899 * 7ms + * +
900 * | * |
901 * 6ms + * +
902 * | * |
903 * 5ms + * +
904 * | * |
905 * 4ms + * +
906 * | * |
907 * 3ms + * +
908 * | * |
909 * 2ms + (midpoint) * +
910 * | | ** |
911 * 1ms + v *** +
912 * | zfs_delay_scale ----------> ******** |
913 * 0 +-------------------------------------*********----------------+
914 * 0% <- zfs_dirty_data_max -> 100%
915 *
916 * Note that since the delay is added to the outstanding time remaining on the
917 * most recent transaction, the delay is effectively the inverse of IOPS.
918 * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve
919 * was chosen such that small changes in the amount of accumulated dirty data
920 * in the first 3/4 of the curve yield relatively small differences in the
921 * amount of delay.
922 *
923 * The effects can be easier to understand when the amount of delay is
924 * represented on a log scale:
925 *
926 * delay
927 * 100ms +-------------------------------------------------------------++
928 * + +
929 * | |
930 * + *+
931 * 10ms + *+
932 * + ** +
933 * | (midpoint) ** |
934 * + | ** +
935 * 1ms + v **** +
936 * + zfs_delay_scale ----------> ***** +
937 * | **** |
938 * + **** +
939 * 100us + ** +
940 * + * +
941 * | * |
942 * + * +
943 * 10us + * +
944 * + +
945 * | |
946 * + +
947 * +--------------------------------------------------------------+
948 * 0% <- zfs_dirty_data_max -> 100%
949 *
950 * Note here that only as the amount of dirty data approaches its limit does
951 * the delay start to increase rapidly. The goal of a properly tuned system
952 * should be to keep the amount of dirty data out of that range by first
953 * ensuring that the appropriate limits are set for the I/O scheduler to reach
954 * optimal throughput on the backend storage, and then by changing the value
955 * of zfs_delay_scale to increase the steepness of the curve.
956 */
957 static void
dmu_tx_delay(dmu_tx_t * tx,uint64_t dirty)958 dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty)
959 {
960 dsl_pool_t *dp = tx->tx_pool;
961 uint64_t delay_min_bytes, wrlog;
962 hrtime_t wakeup, tx_time = 0, now;
963
964 /* Calculate minimum transaction time for the dirty data amount. */
965 delay_min_bytes =
966 zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
967 if (dirty > delay_min_bytes) {
968 /*
969 * The caller has already waited until we are under the max.
970 * We make them pass us the amount of dirty data so we don't
971 * have to handle the case of it being >= the max, which
972 * could cause a divide-by-zero if it's == the max.
973 */
974 ASSERT3U(dirty, <, zfs_dirty_data_max);
975
976 tx_time = zfs_delay_scale * (dirty - delay_min_bytes) /
977 (zfs_dirty_data_max - dirty);
978 }
979
980 /* Calculate minimum transaction time for the TX_WRITE log size. */
981 wrlog = aggsum_upper_bound(&dp->dp_wrlog_total);
982 delay_min_bytes =
983 zfs_wrlog_data_max * zfs_delay_min_dirty_percent / 100;
984 if (wrlog >= zfs_wrlog_data_max) {
985 tx_time = zfs_delay_max_ns;
986 } else if (wrlog > delay_min_bytes) {
987 tx_time = MAX(zfs_delay_scale * (wrlog - delay_min_bytes) /
988 (zfs_wrlog_data_max - wrlog), tx_time);
989 }
990
991 if (tx_time == 0)
992 return;
993
994 tx_time = MIN(tx_time, zfs_delay_max_ns);
995 now = gethrtime();
996 if (now > tx->tx_start + tx_time)
997 return;
998
999 DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty,
1000 uint64_t, tx_time);
1001
1002 mutex_enter(&dp->dp_lock);
1003 wakeup = MAX(tx->tx_start + tx_time, dp->dp_last_wakeup + tx_time);
1004 dp->dp_last_wakeup = wakeup;
1005 mutex_exit(&dp->dp_lock);
1006
1007 zfs_sleep_until(wakeup);
1008 }
1009
1010 /*
1011 * This routine attempts to assign the transaction to a transaction group.
1012 * To do so, we must determine if there is sufficient free space on disk.
1013 *
1014 * If this is a "netfree" transaction (i.e. we called dmu_tx_mark_netfree()
1015 * on it), then it is assumed that there is sufficient free space,
1016 * unless there's insufficient slop space in the pool (see the comment
1017 * above spa_slop_shift in spa_misc.c).
1018 *
1019 * If it is not a "netfree" transaction, then if the data already on disk
1020 * is over the allowed usage (e.g. quota), this will fail with EDQUOT or
1021 * ENOSPC. Otherwise, if the current rough estimate of pending changes,
1022 * plus the rough estimate of this transaction's changes, may exceed the
1023 * allowed usage, then this will fail with ERESTART, which will cause the
1024 * caller to wait for the pending changes to be written to disk (by waiting
1025 * for the next TXG to open), and then check the space usage again.
1026 *
1027 * The rough estimate of pending changes is comprised of the sum of:
1028 *
1029 * - this transaction's holds' txh_space_towrite
1030 *
1031 * - dd_tempreserved[], which is the sum of in-flight transactions'
1032 * holds' txh_space_towrite (i.e. those transactions that have called
1033 * dmu_tx_assign() but not yet called dmu_tx_commit()).
1034 *
1035 * - dd_space_towrite[], which is the amount of dirtied dbufs.
1036 *
1037 * Note that all of these values are inflated by spa_get_worst_case_asize(),
1038 * which means that we may get ERESTART well before we are actually in danger
1039 * of running out of space, but this also mitigates any small inaccuracies
1040 * in the rough estimate (e.g. txh_space_towrite doesn't take into account
1041 * indirect blocks, and dd_space_towrite[] doesn't take into account changes
1042 * to the MOS).
1043 *
1044 * Note that due to this algorithm, it is possible to exceed the allowed
1045 * usage by one transaction. Also, as we approach the allowed usage,
1046 * we will allow a very limited amount of changes into each TXG, thus
1047 * decreasing performance.
1048 */
1049 static int
dmu_tx_try_assign(dmu_tx_t * tx)1050 dmu_tx_try_assign(dmu_tx_t *tx)
1051 {
1052 spa_t *spa = tx->tx_pool->dp_spa;
1053
1054 ASSERT0(tx->tx_txg);
1055
1056 if (tx->tx_err) {
1057 DMU_TX_STAT_BUMP(dmu_tx_error);
1058 return (SET_ERROR(EIO));
1059 }
1060
1061 if (spa_suspended(spa)) {
1062 DMU_TX_STAT_BUMP(dmu_tx_suspended);
1063
1064 /*
1065 * Let dmu_tx_assign() know specifically what happened, so
1066 * it can make the right choice based on the caller flags.
1067 */
1068 return (SET_ERROR(ESHUTDOWN));
1069 }
1070
1071 if (!tx->tx_dirty_delayed &&
1072 dsl_pool_need_wrlog_delay(tx->tx_pool)) {
1073 tx->tx_wait_dirty = B_TRUE;
1074 DMU_TX_STAT_BUMP(dmu_tx_wrlog_delay);
1075 return (SET_ERROR(ERESTART));
1076 }
1077
1078 if (!tx->tx_dirty_delayed &&
1079 dsl_pool_need_dirty_delay(tx->tx_pool)) {
1080 tx->tx_wait_dirty = B_TRUE;
1081 DMU_TX_STAT_BUMP(dmu_tx_dirty_delay);
1082 return (SET_ERROR(ERESTART));
1083 }
1084
1085 tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
1086 tx->tx_needassign_txh = NULL;
1087
1088 /*
1089 * NB: No error returns are allowed after txg_hold_open, but
1090 * before processing the dnode holds, due to the
1091 * dmu_tx_unassign() logic.
1092 */
1093
1094 uint64_t towrite = 0;
1095 uint64_t tohold = 0;
1096 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
1097 txh = list_next(&tx->tx_holds, txh)) {
1098 dnode_t *dn = txh->txh_dnode;
1099 if (dn != NULL) {
1100 /*
1101 * This thread can't hold the dn_struct_rwlock
1102 * while assigning the tx, because this can lead to
1103 * deadlock. Specifically, if this dnode is already
1104 * assigned to an earlier txg, this thread may need
1105 * to wait for that txg to sync (the ERESTART case
1106 * below). The other thread that has assigned this
1107 * dnode to an earlier txg prevents this txg from
1108 * syncing until its tx can complete (calling
1109 * dmu_tx_commit()), but it may need to acquire the
1110 * dn_struct_rwlock to do so (e.g. via
1111 * dmu_buf_hold*()).
1112 *
1113 * Note that this thread can't hold the lock for
1114 * read either, but the rwlock doesn't record
1115 * enough information to make that assertion.
1116 */
1117 ASSERT(!RW_WRITE_HELD(&dn->dn_struct_rwlock));
1118
1119 mutex_enter(&dn->dn_mtx);
1120 if (dn->dn_assigned_txg == tx->tx_txg - 1) {
1121 mutex_exit(&dn->dn_mtx);
1122 tx->tx_needassign_txh = txh;
1123 DMU_TX_STAT_BUMP(dmu_tx_group);
1124 return (SET_ERROR(ERESTART));
1125 }
1126 if (dn->dn_assigned_txg == 0)
1127 dn->dn_assigned_txg = tx->tx_txg;
1128 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1129 (void) zfs_refcount_add(&dn->dn_tx_holds, tx);
1130 mutex_exit(&dn->dn_mtx);
1131 }
1132 towrite += zfs_refcount_count(&txh->txh_space_towrite);
1133 tohold += zfs_refcount_count(&txh->txh_memory_tohold);
1134 }
1135
1136 /* needed allocation: worst-case estimate of write space */
1137 uint64_t asize = spa_get_worst_case_asize(tx->tx_pool->dp_spa, towrite);
1138 /* calculate memory footprint estimate */
1139 uint64_t memory = towrite + tohold;
1140
1141 if (tx->tx_dir != NULL && asize != 0) {
1142 int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
1143 asize, tx->tx_netfree, &tx->tx_tempreserve_cookie, tx);
1144 if (err != 0)
1145 return (err);
1146 }
1147
1148 DMU_TX_STAT_BUMP(dmu_tx_assigned);
1149
1150 return (0);
1151 }
1152
1153 static void
dmu_tx_unassign(dmu_tx_t * tx)1154 dmu_tx_unassign(dmu_tx_t *tx)
1155 {
1156 if (tx->tx_txg == 0)
1157 return;
1158
1159 txg_rele_to_quiesce(&tx->tx_txgh);
1160
1161 /*
1162 * Walk the transaction's hold list, removing the hold on the
1163 * associated dnode, and notifying waiters if the refcount drops to 0.
1164 */
1165 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds);
1166 txh && txh != tx->tx_needassign_txh;
1167 txh = list_next(&tx->tx_holds, txh)) {
1168 dnode_t *dn = txh->txh_dnode;
1169
1170 if (dn == NULL)
1171 continue;
1172 mutex_enter(&dn->dn_mtx);
1173 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1174
1175 if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1176 dn->dn_assigned_txg = 0;
1177 cv_broadcast(&dn->dn_notxholds);
1178 }
1179 mutex_exit(&dn->dn_mtx);
1180 }
1181
1182 txg_rele_to_sync(&tx->tx_txgh);
1183
1184 tx->tx_lasttried_txg = tx->tx_txg;
1185 tx->tx_txg = 0;
1186 }
1187
1188 /*
1189 * Assign tx to a transaction group; `flags` is a bitmask:
1190 *
1191 * If DMU_TX_WAIT is set and the currently open txg is full, this function
1192 * will wait until there's a new txg. This should be used when no locks
1193 * are being held. With this bit set, this function will only fail if
1194 * we're truly out of space (ENOSPC), over quota (EDQUOT), or required
1195 * data for the transaction could not be read from disk (EIO).
1196 *
1197 * If DMU_TX_WAIT is *not* set and we can't assign into the currently open
1198 * txg without blocking, this function will return immediately with
1199 * ERESTART. This should be used whenever locks are being held. On an
1200 * ERESTART error, the caller should drop all locks, call dmu_tx_wait(),
1201 * and try again.
1202 *
1203 * If DMU_TX_NOTHROTTLE is set, this indicates that this tx should not be
1204 * delayed due on the ZFS Write Throttle (see comments in dsl_pool.c for
1205 * details on the throttle). This is used by the VFS operations, after
1206 * they have already called dmu_tx_wait() (though most likely on a
1207 * different tx).
1208 *
1209 * If DMU_TX_SUSPEND is set, this indicates that this tx should ignore
1210 * the pool being or becoming suspending while it is in progress. This will
1211 * cause dmu_tx_assign() (and dmu_tx_wait()) to block until the pool resumes.
1212 * If this flag is not set and the pool suspends, the return will be either
1213 * ERESTART or EIO, depending on the value of the pool's failmode= property.
1214 *
1215 * It is guaranteed that subsequent successful calls to dmu_tx_assign()
1216 * will assign the tx to monotonically increasing txgs. Of course this is
1217 * not strong monotonicity, because the same txg can be returned multiple
1218 * times in a row. This guarantee holds both for subsequent calls from
1219 * one thread and for multiple threads. For example, it is impossible to
1220 * observe the following sequence of events:
1221 *
1222 * Thread 1 Thread 2
1223 *
1224 * dmu_tx_assign(T1, ...)
1225 * 1 <- dmu_tx_get_txg(T1)
1226 * dmu_tx_assign(T2, ...)
1227 * 2 <- dmu_tx_get_txg(T2)
1228 * dmu_tx_assign(T3, ...)
1229 * 1 <- dmu_tx_get_txg(T3)
1230 */
1231 int
dmu_tx_assign(dmu_tx_t * tx,dmu_tx_flag_t flags)1232 dmu_tx_assign(dmu_tx_t *tx, dmu_tx_flag_t flags)
1233 {
1234 int err;
1235
1236 ASSERT0(tx->tx_txg);
1237 ASSERT0(flags & ~(DMU_TX_WAIT | DMU_TX_NOTHROTTLE | DMU_TX_SUSPEND));
1238 IMPLY(flags & DMU_TX_SUSPEND, flags & DMU_TX_WAIT);
1239 ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1240
1241 /* If we might wait, we must not hold the config lock. */
1242 IMPLY((flags & DMU_TX_WAIT), !dsl_pool_config_held(tx->tx_pool));
1243
1244 if ((flags & DMU_TX_NOTHROTTLE))
1245 tx->tx_dirty_delayed = B_TRUE;
1246
1247 if (!(flags & DMU_TX_SUSPEND))
1248 tx->tx_break_on_suspend = B_TRUE;
1249
1250 while ((err = dmu_tx_try_assign(tx)) != 0) {
1251 dmu_tx_unassign(tx);
1252
1253 boolean_t suspended = (err == ESHUTDOWN);
1254 if (suspended) {
1255 /*
1256 * Pool suspended. We need to decide whether to block
1257 * and retry, or return error, depending on the
1258 * caller's flags and the pool config.
1259 */
1260 if (flags & DMU_TX_SUSPEND)
1261 /*
1262 * The caller expressly does not care about
1263 * suspend, so treat it as a normal retry.
1264 */
1265 err = SET_ERROR(ERESTART);
1266 else if ((flags & DMU_TX_WAIT) &&
1267 spa_get_failmode(tx->tx_pool->dp_spa) ==
1268 ZIO_FAILURE_MODE_CONTINUE)
1269 /*
1270 * Caller wants to wait, but pool config is
1271 * overriding that, so return EIO to be
1272 * propagated back to userspace.
1273 */
1274 err = SET_ERROR(EIO);
1275 else
1276 /* Anything else, we should just block. */
1277 err = SET_ERROR(ERESTART);
1278 }
1279
1280 /*
1281 * Return unless we decided to retry, or the caller does not
1282 * want to block.
1283 */
1284 if (err != ERESTART || !(flags & DMU_TX_WAIT)) {
1285 ASSERT(err == EDQUOT || err == ENOSPC ||
1286 err == ERESTART || err == EIO);
1287 return (err);
1288 }
1289
1290 /*
1291 * Wait until there's room in this txg, or until it's been
1292 * synced out and a new one is available.
1293 *
1294 * If we're here because the pool suspended above, then we
1295 * unset tx_break_on_suspend to make sure that if dmu_tx_wait()
1296 * has to fall back to a txg_wait_synced_flags(), it doesn't
1297 * immediately return because the pool is suspended. That would
1298 * then immediately return here, and we'd end up in a busy loop
1299 * until the pool resumes.
1300 *
1301 * On the other hand, if the pool hasn't suspended yet, then it
1302 * should be allowed to break a txg wait if the pool does
1303 * suspend, so we can loop and reassess it in
1304 * dmu_tx_try_assign().
1305 */
1306 if (suspended)
1307 tx->tx_break_on_suspend = B_FALSE;
1308
1309 dmu_tx_wait(tx);
1310
1311 /*
1312 * Reset tx_break_on_suspend for DMU_TX_SUSPEND. We do this
1313 * here so that it's available if we return for some other
1314 * reason, and then the caller calls dmu_tx_wait().
1315 */
1316 if (!(flags & DMU_TX_SUSPEND))
1317 tx->tx_break_on_suspend = B_TRUE;
1318 }
1319
1320 txg_rele_to_quiesce(&tx->tx_txgh);
1321
1322 return (0);
1323 }
1324
1325 void
dmu_tx_wait(dmu_tx_t * tx)1326 dmu_tx_wait(dmu_tx_t *tx)
1327 {
1328 spa_t *spa = tx->tx_pool->dp_spa;
1329 dsl_pool_t *dp = tx->tx_pool;
1330 hrtime_t before;
1331
1332 ASSERT0(tx->tx_txg);
1333 ASSERT(!dsl_pool_config_held(tx->tx_pool));
1334
1335 /*
1336 * Break on suspend according to whether or not DMU_TX_SUSPEND was
1337 * supplied to the previous dmu_tx_assign() call. For clients, this
1338 * ensures that after dmu_tx_assign() fails, the followup dmu_tx_wait()
1339 * gets the same behaviour wrt suspend. See also the comments in
1340 * dmu_tx_assign().
1341 */
1342 txg_wait_flag_t flags =
1343 (tx->tx_break_on_suspend ? TXG_WAIT_SUSPEND : TXG_WAIT_NONE);
1344
1345 before = gethrtime();
1346
1347 if (tx->tx_wait_dirty) {
1348 uint64_t dirty;
1349
1350 /*
1351 * dmu_tx_try_assign() has determined that we need to wait
1352 * because we've consumed much or all of the dirty buffer
1353 * space.
1354 */
1355 mutex_enter(&dp->dp_lock);
1356 if (dp->dp_dirty_total >= zfs_dirty_data_max)
1357 DMU_TX_STAT_BUMP(dmu_tx_dirty_over_max);
1358 while (dp->dp_dirty_total >= zfs_dirty_data_max)
1359 cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock);
1360 dirty = dp->dp_dirty_total;
1361 mutex_exit(&dp->dp_lock);
1362
1363 dmu_tx_delay(tx, dirty);
1364
1365 tx->tx_wait_dirty = B_FALSE;
1366
1367 /*
1368 * Note: setting tx_dirty_delayed only has effect if the
1369 * caller used DMU_TX_WAIT. Otherwise they are going to
1370 * destroy this tx and try again. The common case,
1371 * zfs_write(), uses DMU_TX_WAIT.
1372 */
1373 tx->tx_dirty_delayed = B_TRUE;
1374 } else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
1375 /*
1376 * If the pool is suspended we need to wait until it
1377 * is resumed. Note that it's possible that the pool
1378 * has become active after this thread has tried to
1379 * obtain a tx. If that's the case then tx_lasttried_txg
1380 * would not have been set.
1381 */
1382 txg_wait_synced_flags(dp, spa_last_synced_txg(spa) + 1, flags);
1383 } else if (tx->tx_needassign_txh) {
1384 dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1385
1386 mutex_enter(&dn->dn_mtx);
1387 while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1388 cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1389 mutex_exit(&dn->dn_mtx);
1390 tx->tx_needassign_txh = NULL;
1391 } else {
1392 /*
1393 * If we have a lot of dirty data just wait until we sync
1394 * out a TXG at which point we'll hopefully have synced
1395 * a portion of the changes.
1396 */
1397 txg_wait_synced_flags(dp, spa_last_synced_txg(spa) + 1, flags);
1398 }
1399
1400 spa_tx_assign_add_nsecs(spa, gethrtime() - before);
1401 }
1402
1403 static void
dmu_tx_destroy(dmu_tx_t * tx)1404 dmu_tx_destroy(dmu_tx_t *tx)
1405 {
1406 dmu_tx_hold_t *txh;
1407
1408 while ((txh = list_head(&tx->tx_holds)) != NULL) {
1409 dnode_t *dn = txh->txh_dnode;
1410
1411 list_remove(&tx->tx_holds, txh);
1412 zfs_refcount_destroy_many(&txh->txh_space_towrite,
1413 zfs_refcount_count(&txh->txh_space_towrite));
1414 zfs_refcount_destroy_many(&txh->txh_memory_tohold,
1415 zfs_refcount_count(&txh->txh_memory_tohold));
1416 kmem_free(txh, sizeof (dmu_tx_hold_t));
1417 if (dn != NULL)
1418 dnode_rele(dn, tx);
1419 }
1420
1421 list_destroy(&tx->tx_callbacks);
1422 list_destroy(&tx->tx_holds);
1423 kmem_free(tx, sizeof (dmu_tx_t));
1424 }
1425
1426 void
dmu_tx_commit(dmu_tx_t * tx)1427 dmu_tx_commit(dmu_tx_t *tx)
1428 {
1429 /* This function should only be used on assigned transactions. */
1430 ASSERT(tx->tx_txg != 0);
1431
1432 /*
1433 * Go through the transaction's hold list and remove holds on
1434 * associated dnodes, notifying waiters if no holds remain.
1435 */
1436 for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
1437 txh = list_next(&tx->tx_holds, txh)) {
1438 dnode_t *dn = txh->txh_dnode;
1439
1440 if (dn == NULL)
1441 continue;
1442
1443 mutex_enter(&dn->dn_mtx);
1444 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1445
1446 if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1447 dn->dn_assigned_txg = 0;
1448 cv_broadcast(&dn->dn_notxholds);
1449 }
1450 mutex_exit(&dn->dn_mtx);
1451 }
1452
1453 if (tx->tx_tempreserve_cookie)
1454 dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1455
1456 if (!list_is_empty(&tx->tx_callbacks))
1457 txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1458
1459 if (tx->tx_anyobj == FALSE)
1460 txg_rele_to_sync(&tx->tx_txgh);
1461
1462 dmu_tx_destroy(tx);
1463 }
1464
1465 void
dmu_tx_abort(dmu_tx_t * tx)1466 dmu_tx_abort(dmu_tx_t *tx)
1467 {
1468 /* This function should not be used on assigned transactions. */
1469 ASSERT0(tx->tx_txg);
1470
1471 /* Should not be needed, but better be safe than sorry. */
1472 if (tx->tx_tempreserve_cookie)
1473 dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1474
1475 /*
1476 * Call any registered callbacks with an error code.
1477 */
1478 if (!list_is_empty(&tx->tx_callbacks))
1479 dmu_tx_do_callbacks(&tx->tx_callbacks, SET_ERROR(ECANCELED));
1480
1481 /* Should not be needed, but better be safe than sorry. */
1482 dmu_tx_unassign(tx);
1483
1484 dmu_tx_destroy(tx);
1485 }
1486
1487 uint64_t
dmu_tx_get_txg(dmu_tx_t * tx)1488 dmu_tx_get_txg(dmu_tx_t *tx)
1489 {
1490 ASSERT(tx->tx_txg != 0);
1491 return (tx->tx_txg);
1492 }
1493
1494 dsl_pool_t *
dmu_tx_pool(dmu_tx_t * tx)1495 dmu_tx_pool(dmu_tx_t *tx)
1496 {
1497 ASSERT(tx->tx_pool != NULL);
1498 return (tx->tx_pool);
1499 }
1500
1501 /*
1502 * Register a callback to be executed at the end of a TXG.
1503 *
1504 * Note: This currently exists for outside consumers, specifically the ZFS OSD
1505 * for Lustre. Please do not remove before checking that project. For examples
1506 * on how to use this see `ztest_commit_callback`.
1507 */
1508 void
dmu_tx_callback_register(dmu_tx_t * tx,dmu_tx_callback_func_t * func,void * data)1509 dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1510 {
1511 dmu_tx_callback_t *dcb;
1512
1513 dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1514
1515 dcb->dcb_func = func;
1516 dcb->dcb_data = data;
1517
1518 list_insert_tail(&tx->tx_callbacks, dcb);
1519 }
1520
1521 /*
1522 * Call all the commit callbacks on a list, with a given error code.
1523 */
1524 void
dmu_tx_do_callbacks(list_t * cb_list,int error)1525 dmu_tx_do_callbacks(list_t *cb_list, int error)
1526 {
1527 dmu_tx_callback_t *dcb;
1528
1529 while ((dcb = list_remove_tail(cb_list)) != NULL) {
1530 dcb->dcb_func(dcb->dcb_data, error);
1531 kmem_free(dcb, sizeof (dmu_tx_callback_t));
1532 }
1533 }
1534
1535 /*
1536 * Interface to hold a bunch of attributes.
1537 * used for creating new files.
1538 * attrsize is the total size of all attributes
1539 * to be added during object creation
1540 *
1541 * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
1542 */
1543
1544 /*
1545 * hold necessary attribute name for attribute registration.
1546 * should be a very rare case where this is needed. If it does
1547 * happen it would only happen on the first write to the file system.
1548 */
1549 static void
dmu_tx_sa_registration_hold(sa_os_t * sa,dmu_tx_t * tx)1550 dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
1551 {
1552 if (!sa->sa_need_attr_registration)
1553 return;
1554
1555 for (int i = 0; i != sa->sa_num_attrs; i++) {
1556 if (!sa->sa_attr_table[i].sa_registered) {
1557 if (sa->sa_reg_attr_obj)
1558 dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
1559 B_TRUE, sa->sa_attr_table[i].sa_name);
1560 else
1561 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
1562 B_TRUE, sa->sa_attr_table[i].sa_name);
1563 }
1564 }
1565 }
1566
1567 void
dmu_tx_hold_spill(dmu_tx_t * tx,uint64_t object)1568 dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
1569 {
1570 dmu_tx_hold_t *txh;
1571
1572 txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
1573 THT_SPILL, 0, 0);
1574 if (txh != NULL)
1575 (void) zfs_refcount_add_many(&txh->txh_space_towrite,
1576 SPA_OLD_MAXBLOCKSIZE, FTAG);
1577 }
1578
1579 void
dmu_tx_hold_sa_create(dmu_tx_t * tx,int attrsize)1580 dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
1581 {
1582 sa_os_t *sa = tx->tx_objset->os_sa;
1583
1584 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1585
1586 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1587 return;
1588
1589 if (tx->tx_objset->os_sa->sa_layout_attr_obj) {
1590 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1591 } else {
1592 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1593 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1594 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1595 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1596 }
1597
1598 dmu_tx_sa_registration_hold(sa, tx);
1599
1600 if (attrsize <= DN_OLD_MAX_BONUSLEN && !sa->sa_force_spill)
1601 return;
1602
1603 (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
1604 THT_SPILL, 0, 0);
1605 }
1606
1607 /*
1608 * Hold SA attribute
1609 *
1610 * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
1611 *
1612 * variable_size is the total size of all variable sized attributes
1613 * passed to this function. It is not the total size of all
1614 * variable size attributes that *may* exist on this object.
1615 */
1616 void
dmu_tx_hold_sa(dmu_tx_t * tx,sa_handle_t * hdl,boolean_t may_grow)1617 dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
1618 {
1619 uint64_t object;
1620 sa_os_t *sa = tx->tx_objset->os_sa;
1621
1622 ASSERT(hdl != NULL);
1623
1624 object = sa_handle_object(hdl);
1625
1626 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1627 DB_DNODE_ENTER(db);
1628 dmu_tx_hold_bonus_by_dnode(tx, DB_DNODE(db));
1629 DB_DNODE_EXIT(db);
1630
1631 if (tx->tx_objset->os_sa->sa_master_obj == 0)
1632 return;
1633
1634 if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
1635 tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
1636 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1637 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1638 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1639 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1640 }
1641
1642 dmu_tx_sa_registration_hold(sa, tx);
1643
1644 if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
1645 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1646
1647 if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
1648 ASSERT0(tx->tx_txg);
1649 dmu_tx_hold_spill(tx, object);
1650 } else {
1651 DB_DNODE_ENTER(db);
1652 if (DB_DNODE(db)->dn_have_spill) {
1653 ASSERT0(tx->tx_txg);
1654 dmu_tx_hold_spill(tx, object);
1655 }
1656 DB_DNODE_EXIT(db);
1657 }
1658 }
1659
1660 void
dmu_tx_init(void)1661 dmu_tx_init(void)
1662 {
1663 dmu_tx_ksp = kstat_create("zfs", 0, "dmu_tx", "misc",
1664 KSTAT_TYPE_NAMED, sizeof (dmu_tx_stats) / sizeof (kstat_named_t),
1665 KSTAT_FLAG_VIRTUAL);
1666
1667 if (dmu_tx_ksp != NULL) {
1668 dmu_tx_ksp->ks_data = &dmu_tx_stats;
1669 kstat_install(dmu_tx_ksp);
1670 }
1671 }
1672
1673 void
dmu_tx_fini(void)1674 dmu_tx_fini(void)
1675 {
1676 if (dmu_tx_ksp != NULL) {
1677 kstat_delete(dmu_tx_ksp);
1678 dmu_tx_ksp = NULL;
1679 }
1680 }
1681
1682 #if defined(_KERNEL)
1683 EXPORT_SYMBOL(dmu_tx_create);
1684 EXPORT_SYMBOL(dmu_tx_hold_write);
1685 EXPORT_SYMBOL(dmu_tx_hold_write_by_dnode);
1686 EXPORT_SYMBOL(dmu_tx_hold_append);
1687 EXPORT_SYMBOL(dmu_tx_hold_append_by_dnode);
1688 EXPORT_SYMBOL(dmu_tx_hold_free);
1689 EXPORT_SYMBOL(dmu_tx_hold_free_by_dnode);
1690 EXPORT_SYMBOL(dmu_tx_hold_zap);
1691 EXPORT_SYMBOL(dmu_tx_hold_zap_by_dnode);
1692 EXPORT_SYMBOL(dmu_tx_hold_bonus);
1693 EXPORT_SYMBOL(dmu_tx_hold_bonus_by_dnode);
1694 EXPORT_SYMBOL(dmu_tx_abort);
1695 EXPORT_SYMBOL(dmu_tx_assign);
1696 EXPORT_SYMBOL(dmu_tx_wait);
1697 EXPORT_SYMBOL(dmu_tx_commit);
1698 EXPORT_SYMBOL(dmu_tx_mark_netfree);
1699 EXPORT_SYMBOL(dmu_tx_get_txg);
1700 EXPORT_SYMBOL(dmu_tx_callback_register);
1701 EXPORT_SYMBOL(dmu_tx_do_callbacks);
1702 EXPORT_SYMBOL(dmu_tx_hold_spill);
1703 EXPORT_SYMBOL(dmu_tx_hold_sa_create);
1704 EXPORT_SYMBOL(dmu_tx_hold_sa);
1705 #endif
1706