]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dmu_tx.c
MFZoL: Avoid retrieving unused snapshot props
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dmu_tx.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25  * Copyright (c) 2014 Integros [integros.com]
26  */
27
28 #include <sys/dmu.h>
29 #include <sys/dmu_impl.h>
30 #include <sys/dbuf.h>
31 #include <sys/dmu_tx.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dsl_dataset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/zap_impl.h>
37 #include <sys/spa.h>
38 #include <sys/sa.h>
39 #include <sys/sa_impl.h>
40 #include <sys/zfs_context.h>
41 #include <sys/varargs.h>
42
43 typedef void (*dmu_tx_hold_func_t)(dmu_tx_t *tx, struct dnode *dn,
44     uint64_t arg1, uint64_t arg2);
45
46
47 dmu_tx_t *
48 dmu_tx_create_dd(dsl_dir_t *dd)
49 {
50         dmu_tx_t *tx = kmem_zalloc(sizeof (dmu_tx_t), KM_SLEEP);
51         tx->tx_dir = dd;
52         if (dd != NULL)
53                 tx->tx_pool = dd->dd_pool;
54         list_create(&tx->tx_holds, sizeof (dmu_tx_hold_t),
55             offsetof(dmu_tx_hold_t, txh_node));
56         list_create(&tx->tx_callbacks, sizeof (dmu_tx_callback_t),
57             offsetof(dmu_tx_callback_t, dcb_node));
58         tx->tx_start = gethrtime();
59         return (tx);
60 }
61
62 dmu_tx_t *
63 dmu_tx_create(objset_t *os)
64 {
65         dmu_tx_t *tx = dmu_tx_create_dd(os->os_dsl_dataset->ds_dir);
66         tx->tx_objset = os;
67         return (tx);
68 }
69
70 dmu_tx_t *
71 dmu_tx_create_assigned(struct dsl_pool *dp, uint64_t txg)
72 {
73         dmu_tx_t *tx = dmu_tx_create_dd(NULL);
74
75         txg_verify(dp->dp_spa, txg);
76         tx->tx_pool = dp;
77         tx->tx_txg = txg;
78         tx->tx_anyobj = TRUE;
79
80         return (tx);
81 }
82
83 int
84 dmu_tx_is_syncing(dmu_tx_t *tx)
85 {
86         return (tx->tx_anyobj);
87 }
88
89 int
90 dmu_tx_private_ok(dmu_tx_t *tx)
91 {
92         return (tx->tx_anyobj);
93 }
94
95 static dmu_tx_hold_t *
96 dmu_tx_hold_dnode_impl(dmu_tx_t *tx, dnode_t *dn, enum dmu_tx_hold_type type,
97     uint64_t arg1, uint64_t arg2)
98 {
99         dmu_tx_hold_t *txh;
100
101         if (dn != NULL) {
102                 (void) zfs_refcount_add(&dn->dn_holds, tx);
103                 if (tx->tx_txg != 0) {
104                         mutex_enter(&dn->dn_mtx);
105                         /*
106                          * dn->dn_assigned_txg == tx->tx_txg doesn't pose a
107                          * problem, but there's no way for it to happen (for
108                          * now, at least).
109                          */
110                         ASSERT(dn->dn_assigned_txg == 0);
111                         dn->dn_assigned_txg = tx->tx_txg;
112                         (void) zfs_refcount_add(&dn->dn_tx_holds, tx);
113                         mutex_exit(&dn->dn_mtx);
114                 }
115         }
116
117         txh = kmem_zalloc(sizeof (dmu_tx_hold_t), KM_SLEEP);
118         txh->txh_tx = tx;
119         txh->txh_dnode = dn;
120         zfs_refcount_create(&txh->txh_space_towrite);
121         zfs_refcount_create(&txh->txh_memory_tohold);
122         txh->txh_type = type;
123         txh->txh_arg1 = arg1;
124         txh->txh_arg2 = arg2;
125         list_insert_tail(&tx->tx_holds, txh);
126
127         return (txh);
128 }
129
130 static dmu_tx_hold_t *
131 dmu_tx_hold_object_impl(dmu_tx_t *tx, objset_t *os, uint64_t object,
132     enum dmu_tx_hold_type type, uint64_t arg1, uint64_t arg2)
133 {
134         dnode_t *dn = NULL;
135         dmu_tx_hold_t *txh;
136         int err;
137
138         if (object != DMU_NEW_OBJECT) {
139                 err = dnode_hold(os, object, FTAG, &dn);
140                 if (err != 0) {
141                         tx->tx_err = err;
142                         return (NULL);
143                 }
144         }
145         txh = dmu_tx_hold_dnode_impl(tx, dn, type, arg1, arg2);
146         if (dn != NULL)
147                 dnode_rele(dn, FTAG);
148         return (txh);
149 }
150
151 void
152 dmu_tx_add_new_object(dmu_tx_t *tx, dnode_t *dn)
153 {
154         /*
155          * If we're syncing, they can manipulate any object anyhow, and
156          * the hold on the dnode_t can cause problems.
157          */
158         if (!dmu_tx_is_syncing(tx))
159                 (void) dmu_tx_hold_dnode_impl(tx, dn, THT_NEWOBJECT, 0, 0);
160 }
161
162 /*
163  * This function reads specified data from disk.  The specified data will
164  * be needed to perform the transaction -- i.e, it will be read after
165  * we do dmu_tx_assign().  There are two reasons that we read the data now
166  * (before dmu_tx_assign()):
167  *
168  * 1. Reading it now has potentially better performance.  The transaction
169  * has not yet been assigned, so the TXG is not held open, and also the
170  * caller typically has less locks held when calling dmu_tx_hold_*() than
171  * after the transaction has been assigned.  This reduces the lock (and txg)
172  * hold times, thus reducing lock contention.
173  *
174  * 2. It is easier for callers (primarily the ZPL) to handle i/o errors
175  * that are detected before they start making changes to the DMU state
176  * (i.e. now).  Once the transaction has been assigned, and some DMU
177  * state has been changed, it can be difficult to recover from an i/o
178  * error (e.g. to undo the changes already made in memory at the DMU
179  * layer).  Typically code to do so does not exist in the caller -- it
180  * assumes that the data has already been cached and thus i/o errors are
181  * not possible.
182  *
183  * It has been observed that the i/o initiated here can be a performance
184  * problem, and it appears to be optional, because we don't look at the
185  * data which is read.  However, removing this read would only serve to
186  * move the work elsewhere (after the dmu_tx_assign()), where it may
187  * have a greater impact on performance (in addition to the impact on
188  * fault tolerance noted above).
189  */
190 static int
191 dmu_tx_check_ioerr(zio_t *zio, dnode_t *dn, int level, uint64_t blkid)
192 {
193         int err;
194         dmu_buf_impl_t *db;
195
196         rw_enter(&dn->dn_struct_rwlock, RW_READER);
197         db = dbuf_hold_level(dn, level, blkid, FTAG);
198         rw_exit(&dn->dn_struct_rwlock);
199         if (db == NULL)
200                 return (SET_ERROR(EIO));
201         err = dbuf_read(db, zio, DB_RF_CANFAIL | DB_RF_NOPREFETCH);
202         dbuf_rele(db, FTAG);
203         return (err);
204 }
205
206 /* ARGSUSED */
207 static void
208 dmu_tx_count_write(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
209 {
210         dnode_t *dn = txh->txh_dnode;
211         int err = 0;
212
213         if (len == 0)
214                 return;
215
216         (void) zfs_refcount_add_many(&txh->txh_space_towrite, len, FTAG);
217
218         if (zfs_refcount_count(&txh->txh_space_towrite) > 2 * DMU_MAX_ACCESS)
219                 err = SET_ERROR(EFBIG);
220
221         if (dn == NULL)
222                 return;
223
224         /*
225          * For i/o error checking, read the blocks that will be needed
226          * to perform the write: the first and last level-0 blocks (if
227          * they are not aligned, i.e. if they are partial-block writes),
228          * and all the level-1 blocks.
229          */
230         if (dn->dn_maxblkid == 0) {
231                 if (off < dn->dn_datablksz &&
232                     (off > 0 || len < dn->dn_datablksz)) {
233                         err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
234                         if (err != 0) {
235                                 txh->txh_tx->tx_err = err;
236                         }
237                 }
238         } else {
239                 zio_t *zio = zio_root(dn->dn_objset->os_spa,
240                     NULL, NULL, ZIO_FLAG_CANFAIL);
241
242                 /* first level-0 block */
243                 uint64_t start = off >> dn->dn_datablkshift;
244                 if (P2PHASE(off, dn->dn_datablksz) || len < dn->dn_datablksz) {
245                         err = dmu_tx_check_ioerr(zio, dn, 0, start);
246                         if (err != 0) {
247                                 txh->txh_tx->tx_err = err;
248                         }
249                 }
250
251                 /* last level-0 block */
252                 uint64_t end = (off + len - 1) >> dn->dn_datablkshift;
253                 if (end != start && end <= dn->dn_maxblkid &&
254                     P2PHASE(off + len, dn->dn_datablksz)) {
255                         err = dmu_tx_check_ioerr(zio, dn, 0, end);
256                         if (err != 0) {
257                                 txh->txh_tx->tx_err = err;
258                         }
259                 }
260
261                 /* level-1 blocks */
262                 if (dn->dn_nlevels > 1) {
263                         int shft = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
264                         for (uint64_t i = (start >> shft) + 1;
265                             i < end >> shft; i++) {
266                                 err = dmu_tx_check_ioerr(zio, dn, 1, i);
267                                 if (err != 0) {
268                                         txh->txh_tx->tx_err = err;
269                                 }
270                         }
271                 }
272
273                 err = zio_wait(zio);
274                 if (err != 0) {
275                         txh->txh_tx->tx_err = err;
276                 }
277         }
278 }
279
280 static void
281 dmu_tx_count_dnode(dmu_tx_hold_t *txh)
282 {
283         (void) zfs_refcount_add_many(&txh->txh_space_towrite, DNODE_MIN_SIZE,
284             FTAG);
285 }
286
287 void
288 dmu_tx_hold_write(dmu_tx_t *tx, uint64_t object, uint64_t off, int len)
289 {
290         dmu_tx_hold_t *txh;
291
292         ASSERT0(tx->tx_txg);
293         ASSERT3U(len, <=, DMU_MAX_ACCESS);
294         ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
295
296         txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
297             object, THT_WRITE, off, len);
298         if (txh != NULL) {
299                 dmu_tx_count_write(txh, off, len);
300                 dmu_tx_count_dnode(txh);
301         }
302 }
303
304 void
305 dmu_tx_hold_remap_l1indirect(dmu_tx_t *tx, uint64_t object)
306 {
307         dmu_tx_hold_t *txh;
308
309         ASSERT(tx->tx_txg == 0);
310         txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
311             object, THT_WRITE, 0, 0);
312         if (txh == NULL)
313                 return;
314
315         dnode_t *dn = txh->txh_dnode;
316         (void) zfs_refcount_add_many(&txh->txh_space_towrite,
317             1ULL << dn->dn_indblkshift, FTAG);
318         dmu_tx_count_dnode(txh);
319 }
320
321 void
322 dmu_tx_hold_write_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, int len)
323 {
324         dmu_tx_hold_t *txh;
325
326         ASSERT0(tx->tx_txg);
327         ASSERT3U(len, <=, DMU_MAX_ACCESS);
328         ASSERT(len == 0 || UINT64_MAX - off >= len - 1);
329
330         txh = dmu_tx_hold_dnode_impl(tx, dn, THT_WRITE, off, len);
331         if (txh != NULL) {
332                 dmu_tx_count_write(txh, off, len);
333                 dmu_tx_count_dnode(txh);
334         }
335 }
336
337 /*
338  * This function marks the transaction as being a "net free".  The end
339  * result is that refquotas will be disabled for this transaction, and
340  * this transaction will be able to use half of the pool space overhead
341  * (see dsl_pool_adjustedsize()).  Therefore this function should only
342  * be called for transactions that we expect will not cause a net increase
343  * in the amount of space used (but it's OK if that is occasionally not true).
344  */
345 void
346 dmu_tx_mark_netfree(dmu_tx_t *tx)
347 {
348         tx->tx_netfree = B_TRUE;
349 }
350
351 static void
352 dmu_tx_hold_free_impl(dmu_tx_hold_t *txh, uint64_t off, uint64_t len)
353 {
354         dmu_tx_t *tx;
355         dnode_t *dn;
356         int err;
357
358         tx = txh->txh_tx;
359         ASSERT(tx->tx_txg == 0);
360
361         dn = txh->txh_dnode;
362         dmu_tx_count_dnode(txh);
363
364         if (off >= (dn->dn_maxblkid + 1) * dn->dn_datablksz)
365                 return;
366         if (len == DMU_OBJECT_END)
367                 len = (dn->dn_maxblkid + 1) * dn->dn_datablksz - off;
368
369
370         /*
371          * For i/o error checking, we read the first and last level-0
372          * blocks if they are not aligned, and all the level-1 blocks.
373          *
374          * Note:  dbuf_free_range() assumes that we have not instantiated
375          * any level-0 dbufs that will be completely freed.  Therefore we must
376          * exercise care to not read or count the first and last blocks
377          * if they are blocksize-aligned.
378          */
379         if (dn->dn_datablkshift == 0) {
380                 if (off != 0 || len < dn->dn_datablksz)
381                         dmu_tx_count_write(txh, 0, dn->dn_datablksz);
382         } else {
383                 /* first block will be modified if it is not aligned */
384                 if (!IS_P2ALIGNED(off, 1 << dn->dn_datablkshift))
385                         dmu_tx_count_write(txh, off, 1);
386                 /* last block will be modified if it is not aligned */
387                 if (!IS_P2ALIGNED(off + len, 1 << dn->dn_datablkshift))
388                         dmu_tx_count_write(txh, off + len, 1);
389         }
390
391         /*
392          * Check level-1 blocks.
393          */
394         if (dn->dn_nlevels > 1) {
395                 int shift = dn->dn_datablkshift + dn->dn_indblkshift -
396                     SPA_BLKPTRSHIFT;
397                 uint64_t start = off >> shift;
398                 uint64_t end = (off + len) >> shift;
399
400                 ASSERT(dn->dn_indblkshift != 0);
401
402                 /*
403                  * dnode_reallocate() can result in an object with indirect
404                  * blocks having an odd data block size.  In this case,
405                  * just check the single block.
406                  */
407                 if (dn->dn_datablkshift == 0)
408                         start = end = 0;
409
410                 zio_t *zio = zio_root(tx->tx_pool->dp_spa,
411                     NULL, NULL, ZIO_FLAG_CANFAIL);
412                 for (uint64_t i = start; i <= end; i++) {
413                         uint64_t ibyte = i << shift;
414                         err = dnode_next_offset(dn, 0, &ibyte, 2, 1, 0);
415                         i = ibyte >> shift;
416                         if (err == ESRCH || i > end)
417                                 break;
418                         if (err != 0) {
419                                 tx->tx_err = err;
420                                 (void) zio_wait(zio);
421                                 return;
422                         }
423
424                         (void) zfs_refcount_add_many(&txh->txh_memory_tohold,
425                             1 << dn->dn_indblkshift, FTAG);
426
427                         err = dmu_tx_check_ioerr(zio, dn, 1, i);
428                         if (err != 0) {
429                                 tx->tx_err = err;
430                                 (void) zio_wait(zio);
431                                 return;
432                         }
433                 }
434                 err = zio_wait(zio);
435                 if (err != 0) {
436                         tx->tx_err = err;
437                         return;
438                 }
439         }
440 }
441
442 void
443 dmu_tx_hold_free(dmu_tx_t *tx, uint64_t object, uint64_t off, uint64_t len)
444 {
445         dmu_tx_hold_t *txh;
446
447         txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
448             object, THT_FREE, off, len);
449         if (txh != NULL)
450                 (void) dmu_tx_hold_free_impl(txh, off, len);
451 }
452
453 void
454 dmu_tx_hold_free_by_dnode(dmu_tx_t *tx, dnode_t *dn, uint64_t off, uint64_t len)
455 {
456         dmu_tx_hold_t *txh;
457
458         txh = dmu_tx_hold_dnode_impl(tx, dn, THT_FREE, off, len);
459         if (txh != NULL)
460                 (void) dmu_tx_hold_free_impl(txh, off, len);
461 }
462
463 static void
464 dmu_tx_hold_zap_impl(dmu_tx_hold_t *txh, const char *name)
465 {
466         dmu_tx_t *tx = txh->txh_tx;
467         dnode_t *dn;
468         int err;
469
470         ASSERT(tx->tx_txg == 0);
471
472         dn = txh->txh_dnode;
473
474         dmu_tx_count_dnode(txh);
475
476         /*
477          * Modifying a almost-full microzap is around the worst case (128KB)
478          *
479          * If it is a fat zap, the worst case would be 7*16KB=112KB:
480          * - 3 blocks overwritten: target leaf, ptrtbl block, header block
481          * - 4 new blocks written if adding:
482          *    - 2 blocks for possibly split leaves,
483          *    - 2 grown ptrtbl blocks
484          */
485         (void) zfs_refcount_add_many(&txh->txh_space_towrite,
486             MZAP_MAX_BLKSZ, FTAG);
487
488         if (dn == NULL)
489                 return;
490
491         ASSERT3P(DMU_OT_BYTESWAP(dn->dn_type), ==, DMU_BSWAP_ZAP);
492
493         if (dn->dn_maxblkid == 0 || name == NULL) {
494                 /*
495                  * This is a microzap (only one block), or we don't know
496                  * the name.  Check the first block for i/o errors.
497                  */
498                 err = dmu_tx_check_ioerr(NULL, dn, 0, 0);
499                 if (err != 0) {
500                         tx->tx_err = err;
501                 }
502         } else {
503                 /*
504                  * Access the name so that we'll check for i/o errors to
505                  * the leaf blocks, etc.  We ignore ENOENT, as this name
506                  * may not yet exist.
507                  */
508                 err = zap_lookup_by_dnode(dn, name, 8, 0, NULL);
509                 if (err == EIO || err == ECKSUM || err == ENXIO) {
510                         tx->tx_err = err;
511                 }
512         }
513 }
514
515 void
516 dmu_tx_hold_zap(dmu_tx_t *tx, uint64_t object, int add, const char *name)
517 {
518         dmu_tx_hold_t *txh;
519
520         ASSERT0(tx->tx_txg);
521
522         txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
523             object, THT_ZAP, add, (uintptr_t)name);
524         if (txh != NULL)
525                 dmu_tx_hold_zap_impl(txh, name);
526 }
527
528 void
529 dmu_tx_hold_zap_by_dnode(dmu_tx_t *tx, dnode_t *dn, int add, const char *name)
530 {
531         dmu_tx_hold_t *txh;
532
533         ASSERT0(tx->tx_txg);
534         ASSERT(dn != NULL);
535
536         txh = dmu_tx_hold_dnode_impl(tx, dn, THT_ZAP, add, (uintptr_t)name);
537         if (txh != NULL)
538                 dmu_tx_hold_zap_impl(txh, name);
539 }
540
541 void
542 dmu_tx_hold_bonus(dmu_tx_t *tx, uint64_t object)
543 {
544         dmu_tx_hold_t *txh;
545
546         ASSERT(tx->tx_txg == 0);
547
548         txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
549             object, THT_BONUS, 0, 0);
550         if (txh)
551                 dmu_tx_count_dnode(txh);
552 }
553
554 void
555 dmu_tx_hold_bonus_by_dnode(dmu_tx_t *tx, dnode_t *dn)
556 {
557         dmu_tx_hold_t *txh;
558
559         ASSERT0(tx->tx_txg);
560
561         txh = dmu_tx_hold_dnode_impl(tx, dn, THT_BONUS, 0, 0);
562         if (txh)
563                 dmu_tx_count_dnode(txh);
564 }
565
566 void
567 dmu_tx_hold_space(dmu_tx_t *tx, uint64_t space)
568 {
569         dmu_tx_hold_t *txh;
570         ASSERT(tx->tx_txg == 0);
571
572         txh = dmu_tx_hold_object_impl(tx, tx->tx_objset,
573             DMU_NEW_OBJECT, THT_SPACE, space, 0);
574
575         (void) zfs_refcount_add_many(&txh->txh_space_towrite, space, FTAG);
576 }
577
578 #ifdef ZFS_DEBUG
579 void
580 dmu_tx_dirty_buf(dmu_tx_t *tx, dmu_buf_impl_t *db)
581 {
582         boolean_t match_object = B_FALSE;
583         boolean_t match_offset = B_FALSE;
584
585         DB_DNODE_ENTER(db);
586         dnode_t *dn = DB_DNODE(db);
587         ASSERT(tx->tx_txg != 0);
588         ASSERT(tx->tx_objset == NULL || dn->dn_objset == tx->tx_objset);
589         ASSERT3U(dn->dn_object, ==, db->db.db_object);
590
591         if (tx->tx_anyobj) {
592                 DB_DNODE_EXIT(db);
593                 return;
594         }
595
596         /* XXX No checking on the meta dnode for now */
597         if (db->db.db_object == DMU_META_DNODE_OBJECT) {
598                 DB_DNODE_EXIT(db);
599                 return;
600         }
601
602         for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
603             txh = list_next(&tx->tx_holds, txh)) {
604                 ASSERT(dn == NULL || dn->dn_assigned_txg == tx->tx_txg);
605                 if (txh->txh_dnode == dn && txh->txh_type != THT_NEWOBJECT)
606                         match_object = TRUE;
607                 if (txh->txh_dnode == NULL || txh->txh_dnode == dn) {
608                         int datablkshift = dn->dn_datablkshift ?
609                             dn->dn_datablkshift : SPA_MAXBLOCKSHIFT;
610                         int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
611                         int shift = datablkshift + epbs * db->db_level;
612                         uint64_t beginblk = shift >= 64 ? 0 :
613                             (txh->txh_arg1 >> shift);
614                         uint64_t endblk = shift >= 64 ? 0 :
615                             ((txh->txh_arg1 + txh->txh_arg2 - 1) >> shift);
616                         uint64_t blkid = db->db_blkid;
617
618                         /* XXX txh_arg2 better not be zero... */
619
620                         dprintf("found txh type %x beginblk=%llx endblk=%llx\n",
621                             txh->txh_type, beginblk, endblk);
622
623                         switch (txh->txh_type) {
624                         case THT_WRITE:
625                                 if (blkid >= beginblk && blkid <= endblk)
626                                         match_offset = TRUE;
627                                 /*
628                                  * We will let this hold work for the bonus
629                                  * or spill buffer so that we don't need to
630                                  * hold it when creating a new object.
631                                  */
632                                 if (blkid == DMU_BONUS_BLKID ||
633                                     blkid == DMU_SPILL_BLKID)
634                                         match_offset = TRUE;
635                                 /*
636                                  * They might have to increase nlevels,
637                                  * thus dirtying the new TLIBs.  Or the
638                                  * might have to change the block size,
639                                  * thus dirying the new lvl=0 blk=0.
640                                  */
641                                 if (blkid == 0)
642                                         match_offset = TRUE;
643                                 break;
644                         case THT_FREE:
645                                 /*
646                                  * We will dirty all the level 1 blocks in
647                                  * the free range and perhaps the first and
648                                  * last level 0 block.
649                                  */
650                                 if (blkid >= beginblk && (blkid <= endblk ||
651                                     txh->txh_arg2 == DMU_OBJECT_END))
652                                         match_offset = TRUE;
653                                 break;
654                         case THT_SPILL:
655                                 if (blkid == DMU_SPILL_BLKID)
656                                         match_offset = TRUE;
657                                 break;
658                         case THT_BONUS:
659                                 if (blkid == DMU_BONUS_BLKID)
660                                         match_offset = TRUE;
661                                 break;
662                         case THT_ZAP:
663                                 match_offset = TRUE;
664                                 break;
665                         case THT_NEWOBJECT:
666                                 match_object = TRUE;
667                                 break;
668                         default:
669                                 ASSERT(!"bad txh_type");
670                         }
671                 }
672                 if (match_object && match_offset) {
673                         DB_DNODE_EXIT(db);
674                         return;
675                 }
676         }
677         DB_DNODE_EXIT(db);
678         panic("dirtying dbuf obj=%llx lvl=%u blkid=%llx but not tx_held\n",
679             (u_longlong_t)db->db.db_object, db->db_level,
680             (u_longlong_t)db->db_blkid);
681 }
682 #endif
683
684 /*
685  * If we can't do 10 iops, something is wrong.  Let us go ahead
686  * and hit zfs_dirty_data_max.
687  */
688 hrtime_t zfs_delay_max_ns = MSEC2NSEC(100);
689 int zfs_delay_resolution_ns = 100 * 1000; /* 100 microseconds */
690
691 /*
692  * We delay transactions when we've determined that the backend storage
693  * isn't able to accommodate the rate of incoming writes.
694  *
695  * If there is already a transaction waiting, we delay relative to when
696  * that transaction finishes waiting.  This way the calculated min_time
697  * is independent of the number of threads concurrently executing
698  * transactions.
699  *
700  * If we are the only waiter, wait relative to when the transaction
701  * started, rather than the current time.  This credits the transaction for
702  * "time already served", e.g. reading indirect blocks.
703  *
704  * The minimum time for a transaction to take is calculated as:
705  *     min_time = scale * (dirty - min) / (max - dirty)
706  *     min_time is then capped at zfs_delay_max_ns.
707  *
708  * The delay has two degrees of freedom that can be adjusted via tunables.
709  * The percentage of dirty data at which we start to delay is defined by
710  * zfs_delay_min_dirty_percent. This should typically be at or above
711  * zfs_vdev_async_write_active_max_dirty_percent so that we only start to
712  * delay after writing at full speed has failed to keep up with the incoming
713  * write rate. The scale of the curve is defined by zfs_delay_scale. Roughly
714  * speaking, this variable determines the amount of delay at the midpoint of
715  * the curve.
716  *
717  * delay
718  *  10ms +-------------------------------------------------------------*+
719  *       |                                                             *|
720  *   9ms +                                                             *+
721  *       |                                                             *|
722  *   8ms +                                                             *+
723  *       |                                                            * |
724  *   7ms +                                                            * +
725  *       |                                                            * |
726  *   6ms +                                                            * +
727  *       |                                                            * |
728  *   5ms +                                                           *  +
729  *       |                                                           *  |
730  *   4ms +                                                           *  +
731  *       |                                                           *  |
732  *   3ms +                                                          *   +
733  *       |                                                          *   |
734  *   2ms +                                              (midpoint) *    +
735  *       |                                                  |    **     |
736  *   1ms +                                                  v ***       +
737  *       |             zfs_delay_scale ---------->     ********         |
738  *     0 +-------------------------------------*********----------------+
739  *       0%                    <- zfs_dirty_data_max ->               100%
740  *
741  * Note that since the delay is added to the outstanding time remaining on the
742  * most recent transaction, the delay is effectively the inverse of IOPS.
743  * Here the midpoint of 500us translates to 2000 IOPS. The shape of the curve
744  * was chosen such that small changes in the amount of accumulated dirty data
745  * in the first 3/4 of the curve yield relatively small differences in the
746  * amount of delay.
747  *
748  * The effects can be easier to understand when the amount of delay is
749  * represented on a log scale:
750  *
751  * delay
752  * 100ms +-------------------------------------------------------------++
753  *       +                                                              +
754  *       |                                                              |
755  *       +                                                             *+
756  *  10ms +                                                             *+
757  *       +                                                           ** +
758  *       |                                              (midpoint)  **  |
759  *       +                                                  |     **    +
760  *   1ms +                                                  v ****      +
761  *       +             zfs_delay_scale ---------->        *****         +
762  *       |                                             ****             |
763  *       +                                          ****                +
764  * 100us +                                        **                    +
765  *       +                                       *                      +
766  *       |                                      *                       |
767  *       +                                     *                        +
768  *  10us +                                     *                        +
769  *       +                                                              +
770  *       |                                                              |
771  *       +                                                              +
772  *       +--------------------------------------------------------------+
773  *       0%                    <- zfs_dirty_data_max ->               100%
774  *
775  * Note here that only as the amount of dirty data approaches its limit does
776  * the delay start to increase rapidly. The goal of a properly tuned system
777  * should be to keep the amount of dirty data out of that range by first
778  * ensuring that the appropriate limits are set for the I/O scheduler to reach
779  * optimal throughput on the backend storage, and then by changing the value
780  * of zfs_delay_scale to increase the steepness of the curve.
781  */
782 static void
783 dmu_tx_delay(dmu_tx_t *tx, uint64_t dirty)
784 {
785         dsl_pool_t *dp = tx->tx_pool;
786         uint64_t delay_min_bytes =
787             zfs_dirty_data_max * zfs_delay_min_dirty_percent / 100;
788         hrtime_t wakeup, min_tx_time, now;
789
790         if (dirty <= delay_min_bytes)
791                 return;
792
793         /*
794          * The caller has already waited until we are under the max.
795          * We make them pass us the amount of dirty data so we don't
796          * have to handle the case of it being >= the max, which could
797          * cause a divide-by-zero if it's == the max.
798          */
799         ASSERT3U(dirty, <, zfs_dirty_data_max);
800
801         now = gethrtime();
802         min_tx_time = zfs_delay_scale *
803             (dirty - delay_min_bytes) / (zfs_dirty_data_max - dirty);
804         if (now > tx->tx_start + min_tx_time)
805                 return;
806
807         min_tx_time = MIN(min_tx_time, zfs_delay_max_ns);
808
809         DTRACE_PROBE3(delay__mintime, dmu_tx_t *, tx, uint64_t, dirty,
810             uint64_t, min_tx_time);
811
812         mutex_enter(&dp->dp_lock);
813         wakeup = MAX(tx->tx_start + min_tx_time,
814             dp->dp_last_wakeup + min_tx_time);
815         dp->dp_last_wakeup = wakeup;
816         mutex_exit(&dp->dp_lock);
817
818 #ifdef _KERNEL
819 #ifdef illumos
820         mutex_enter(&curthread->t_delay_lock);
821         while (cv_timedwait_hires(&curthread->t_delay_cv,
822             &curthread->t_delay_lock, wakeup, zfs_delay_resolution_ns,
823             CALLOUT_FLAG_ABSOLUTE | CALLOUT_FLAG_ROUNDUP) > 0)
824                 continue;
825         mutex_exit(&curthread->t_delay_lock);
826 #else
827         pause_sbt("dmu_tx_delay", nstosbt(wakeup),
828             nstosbt(zfs_delay_resolution_ns), C_ABSOLUTE);
829 #endif
830 #else
831         hrtime_t delta = wakeup - gethrtime();
832         struct timespec ts;
833         ts.tv_sec = delta / NANOSEC;
834         ts.tv_nsec = delta % NANOSEC;
835         (void) nanosleep(&ts, NULL);
836 #endif
837 }
838
839 /*
840  * This routine attempts to assign the transaction to a transaction group.
841  * To do so, we must determine if there is sufficient free space on disk.
842  *
843  * If this is a "netfree" transaction (i.e. we called dmu_tx_mark_netfree()
844  * on it), then it is assumed that there is sufficient free space,
845  * unless there's insufficient slop space in the pool (see the comment
846  * above spa_slop_shift in spa_misc.c).
847  *
848  * If it is not a "netfree" transaction, then if the data already on disk
849  * is over the allowed usage (e.g. quota), this will fail with EDQUOT or
850  * ENOSPC.  Otherwise, if the current rough estimate of pending changes,
851  * plus the rough estimate of this transaction's changes, may exceed the
852  * allowed usage, then this will fail with ERESTART, which will cause the
853  * caller to wait for the pending changes to be written to disk (by waiting
854  * for the next TXG to open), and then check the space usage again.
855  *
856  * The rough estimate of pending changes is comprised of the sum of:
857  *
858  *  - this transaction's holds' txh_space_towrite
859  *
860  *  - dd_tempreserved[], which is the sum of in-flight transactions'
861  *    holds' txh_space_towrite (i.e. those transactions that have called
862  *    dmu_tx_assign() but not yet called dmu_tx_commit()).
863  *
864  *  - dd_space_towrite[], which is the amount of dirtied dbufs.
865  *
866  * Note that all of these values are inflated by spa_get_worst_case_asize(),
867  * which means that we may get ERESTART well before we are actually in danger
868  * of running out of space, but this also mitigates any small inaccuracies
869  * in the rough estimate (e.g. txh_space_towrite doesn't take into account
870  * indirect blocks, and dd_space_towrite[] doesn't take into account changes
871  * to the MOS).
872  *
873  * Note that due to this algorithm, it is possible to exceed the allowed
874  * usage by one transaction.  Also, as we approach the allowed usage,
875  * we will allow a very limited amount of changes into each TXG, thus
876  * decreasing performance.
877  */
878 static int
879 dmu_tx_try_assign(dmu_tx_t *tx, uint64_t txg_how)
880 {
881         spa_t *spa = tx->tx_pool->dp_spa;
882
883         ASSERT0(tx->tx_txg);
884
885         if (tx->tx_err)
886                 return (tx->tx_err);
887
888         if (spa_suspended(spa)) {
889                 /*
890                  * If the user has indicated a blocking failure mode
891                  * then return ERESTART which will block in dmu_tx_wait().
892                  * Otherwise, return EIO so that an error can get
893                  * propagated back to the VOP calls.
894                  *
895                  * Note that we always honor the txg_how flag regardless
896                  * of the failuremode setting.
897                  */
898                 if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE &&
899                     !(txg_how & TXG_WAIT))
900                         return (SET_ERROR(EIO));
901
902                 return (SET_ERROR(ERESTART));
903         }
904
905         if (!tx->tx_dirty_delayed &&
906             dsl_pool_need_dirty_delay(tx->tx_pool)) {
907                 tx->tx_wait_dirty = B_TRUE;
908                 return (SET_ERROR(ERESTART));
909         }
910
911         tx->tx_txg = txg_hold_open(tx->tx_pool, &tx->tx_txgh);
912         tx->tx_needassign_txh = NULL;
913
914         /*
915          * NB: No error returns are allowed after txg_hold_open, but
916          * before processing the dnode holds, due to the
917          * dmu_tx_unassign() logic.
918          */
919
920         uint64_t towrite = 0;
921         uint64_t tohold = 0;
922         for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
923             txh = list_next(&tx->tx_holds, txh)) {
924                 dnode_t *dn = txh->txh_dnode;
925                 if (dn != NULL) {
926                         mutex_enter(&dn->dn_mtx);
927                         if (dn->dn_assigned_txg == tx->tx_txg - 1) {
928                                 mutex_exit(&dn->dn_mtx);
929                                 tx->tx_needassign_txh = txh;
930                                 return (SET_ERROR(ERESTART));
931                         }
932                         if (dn->dn_assigned_txg == 0)
933                                 dn->dn_assigned_txg = tx->tx_txg;
934                         ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
935                         (void) zfs_refcount_add(&dn->dn_tx_holds, tx);
936                         mutex_exit(&dn->dn_mtx);
937                 }
938                 towrite += zfs_refcount_count(&txh->txh_space_towrite);
939                 tohold += zfs_refcount_count(&txh->txh_memory_tohold);
940         }
941
942         /* needed allocation: worst-case estimate of write space */
943         uint64_t asize = spa_get_worst_case_asize(tx->tx_pool->dp_spa, towrite);
944         /* calculate memory footprint estimate */
945         uint64_t memory = towrite + tohold;
946
947         if (tx->tx_dir != NULL && asize != 0) {
948                 int err = dsl_dir_tempreserve_space(tx->tx_dir, memory,
949                     asize, tx->tx_netfree, &tx->tx_tempreserve_cookie, tx);
950                 if (err != 0)
951                         return (err);
952         }
953
954         return (0);
955 }
956
957 static void
958 dmu_tx_unassign(dmu_tx_t *tx)
959 {
960         if (tx->tx_txg == 0)
961                 return;
962
963         txg_rele_to_quiesce(&tx->tx_txgh);
964
965         /*
966          * Walk the transaction's hold list, removing the hold on the
967          * associated dnode, and notifying waiters if the refcount drops to 0.
968          */
969         for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds);
970             txh != tx->tx_needassign_txh;
971             txh = list_next(&tx->tx_holds, txh)) {
972                 dnode_t *dn = txh->txh_dnode;
973
974                 if (dn == NULL)
975                         continue;
976                 mutex_enter(&dn->dn_mtx);
977                 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
978
979                 if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
980                         dn->dn_assigned_txg = 0;
981                         cv_broadcast(&dn->dn_notxholds);
982                 }
983                 mutex_exit(&dn->dn_mtx);
984         }
985
986         txg_rele_to_sync(&tx->tx_txgh);
987
988         tx->tx_lasttried_txg = tx->tx_txg;
989         tx->tx_txg = 0;
990 }
991
992 /*
993  * Assign tx to a transaction group; txg_how is a bitmask:
994  *
995  * If TXG_WAIT is set and the currently open txg is full, this function
996  * will wait until there's a new txg. This should be used when no locks
997  * are being held. With this bit set, this function will only fail if
998  * we're truly out of space (or over quota).
999  *
1000  * If TXG_WAIT is *not* set and we can't assign into the currently open
1001  * txg without blocking, this function will return immediately with
1002  * ERESTART. This should be used whenever locks are being held.  On an
1003  * ERESTART error, the caller should drop all locks, call dmu_tx_wait(),
1004  * and try again.
1005  *
1006  * If TXG_NOTHROTTLE is set, this indicates that this tx should not be
1007  * delayed due on the ZFS Write Throttle (see comments in dsl_pool.c for
1008  * details on the throttle). This is used by the VFS operations, after
1009  * they have already called dmu_tx_wait() (though most likely on a
1010  * different tx).
1011  */
1012 int
1013 dmu_tx_assign(dmu_tx_t *tx, uint64_t txg_how)
1014 {
1015         int err;
1016
1017         ASSERT(tx->tx_txg == 0);
1018         ASSERT0(txg_how & ~(TXG_WAIT | TXG_NOTHROTTLE));
1019         ASSERT(!dsl_pool_sync_context(tx->tx_pool));
1020
1021         /* If we might wait, we must not hold the config lock. */
1022         IMPLY((txg_how & TXG_WAIT), !dsl_pool_config_held(tx->tx_pool));
1023
1024         if ((txg_how & TXG_NOTHROTTLE))
1025                 tx->tx_dirty_delayed = B_TRUE;
1026
1027         while ((err = dmu_tx_try_assign(tx, txg_how)) != 0) {
1028                 dmu_tx_unassign(tx);
1029
1030                 if (err != ERESTART || !(txg_how & TXG_WAIT))
1031                         return (err);
1032
1033                 dmu_tx_wait(tx);
1034         }
1035
1036         txg_rele_to_quiesce(&tx->tx_txgh);
1037
1038         return (0);
1039 }
1040
1041 void
1042 dmu_tx_wait(dmu_tx_t *tx)
1043 {
1044         spa_t *spa = tx->tx_pool->dp_spa;
1045         dsl_pool_t *dp = tx->tx_pool;
1046
1047         ASSERT(tx->tx_txg == 0);
1048         ASSERT(!dsl_pool_config_held(tx->tx_pool));
1049
1050         if (tx->tx_wait_dirty) {
1051                 /*
1052                  * dmu_tx_try_assign() has determined that we need to wait
1053                  * because we've consumed much or all of the dirty buffer
1054                  * space.
1055                  */
1056                 mutex_enter(&dp->dp_lock);
1057                 while (dp->dp_dirty_total >= zfs_dirty_data_max)
1058                         cv_wait(&dp->dp_spaceavail_cv, &dp->dp_lock);
1059                 uint64_t dirty = dp->dp_dirty_total;
1060                 mutex_exit(&dp->dp_lock);
1061
1062                 dmu_tx_delay(tx, dirty);
1063
1064                 tx->tx_wait_dirty = B_FALSE;
1065
1066                 /*
1067                  * Note: setting tx_dirty_delayed only has effect if the
1068                  * caller used TX_WAIT.  Otherwise they are going to
1069                  * destroy this tx and try again.  The common case,
1070                  * zfs_write(), uses TX_WAIT.
1071                  */
1072                 tx->tx_dirty_delayed = B_TRUE;
1073         } else if (spa_suspended(spa) || tx->tx_lasttried_txg == 0) {
1074                 /*
1075                  * If the pool is suspended we need to wait until it
1076                  * is resumed.  Note that it's possible that the pool
1077                  * has become active after this thread has tried to
1078                  * obtain a tx.  If that's the case then tx_lasttried_txg
1079                  * would not have been set.
1080                  */
1081                 txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
1082         } else if (tx->tx_needassign_txh) {
1083                 /*
1084                  * A dnode is assigned to the quiescing txg.  Wait for its
1085                  * transaction to complete.
1086                  */
1087                 dnode_t *dn = tx->tx_needassign_txh->txh_dnode;
1088
1089                 mutex_enter(&dn->dn_mtx);
1090                 while (dn->dn_assigned_txg == tx->tx_lasttried_txg - 1)
1091                         cv_wait(&dn->dn_notxholds, &dn->dn_mtx);
1092                 mutex_exit(&dn->dn_mtx);
1093                 tx->tx_needassign_txh = NULL;
1094         } else {
1095                 /*
1096                  * If we have a lot of dirty data just wait until we sync
1097                  * out a TXG at which point we'll hopefully have synced
1098                  * a portion of the changes.
1099                  */
1100                 txg_wait_synced(dp, spa_last_synced_txg(spa) + 1);
1101         }
1102 }
1103
1104 static void
1105 dmu_tx_destroy(dmu_tx_t *tx)
1106 {
1107         dmu_tx_hold_t *txh;
1108
1109         while ((txh = list_head(&tx->tx_holds)) != NULL) {
1110                 dnode_t *dn = txh->txh_dnode;
1111
1112                 list_remove(&tx->tx_holds, txh);
1113                 zfs_refcount_destroy_many(&txh->txh_space_towrite,
1114                     zfs_refcount_count(&txh->txh_space_towrite));
1115                 zfs_refcount_destroy_many(&txh->txh_memory_tohold,
1116                     zfs_refcount_count(&txh->txh_memory_tohold));
1117                 kmem_free(txh, sizeof (dmu_tx_hold_t));
1118                 if (dn != NULL)
1119                         dnode_rele(dn, tx);
1120         }
1121
1122         list_destroy(&tx->tx_callbacks);
1123         list_destroy(&tx->tx_holds);
1124         kmem_free(tx, sizeof (dmu_tx_t));
1125 }
1126
1127 void
1128 dmu_tx_commit(dmu_tx_t *tx)
1129 {
1130         ASSERT(tx->tx_txg != 0);
1131
1132         /*
1133          * Go through the transaction's hold list and remove holds on
1134          * associated dnodes, notifying waiters if no holds remain.
1135          */
1136         for (dmu_tx_hold_t *txh = list_head(&tx->tx_holds); txh != NULL;
1137             txh = list_next(&tx->tx_holds, txh)) {
1138                 dnode_t *dn = txh->txh_dnode;
1139
1140                 if (dn == NULL)
1141                         continue;
1142
1143                 mutex_enter(&dn->dn_mtx);
1144                 ASSERT3U(dn->dn_assigned_txg, ==, tx->tx_txg);
1145
1146                 if (zfs_refcount_remove(&dn->dn_tx_holds, tx) == 0) {
1147                         dn->dn_assigned_txg = 0;
1148                         cv_broadcast(&dn->dn_notxholds);
1149                 }
1150                 mutex_exit(&dn->dn_mtx);
1151         }
1152
1153         if (tx->tx_tempreserve_cookie)
1154                 dsl_dir_tempreserve_clear(tx->tx_tempreserve_cookie, tx);
1155
1156         if (!list_is_empty(&tx->tx_callbacks))
1157                 txg_register_callbacks(&tx->tx_txgh, &tx->tx_callbacks);
1158
1159         if (tx->tx_anyobj == FALSE)
1160                 txg_rele_to_sync(&tx->tx_txgh);
1161
1162         dmu_tx_destroy(tx);
1163 }
1164
1165 void
1166 dmu_tx_abort(dmu_tx_t *tx)
1167 {
1168         ASSERT(tx->tx_txg == 0);
1169
1170         /*
1171          * Call any registered callbacks with an error code.
1172          */
1173         if (!list_is_empty(&tx->tx_callbacks))
1174                 dmu_tx_do_callbacks(&tx->tx_callbacks, ECANCELED);
1175
1176         dmu_tx_destroy(tx);
1177 }
1178
1179 uint64_t
1180 dmu_tx_get_txg(dmu_tx_t *tx)
1181 {
1182         ASSERT(tx->tx_txg != 0);
1183         return (tx->tx_txg);
1184 }
1185
1186 dsl_pool_t *
1187 dmu_tx_pool(dmu_tx_t *tx)
1188 {
1189         ASSERT(tx->tx_pool != NULL);
1190         return (tx->tx_pool);
1191 }
1192
1193 void
1194 dmu_tx_callback_register(dmu_tx_t *tx, dmu_tx_callback_func_t *func, void *data)
1195 {
1196         dmu_tx_callback_t *dcb;
1197
1198         dcb = kmem_alloc(sizeof (dmu_tx_callback_t), KM_SLEEP);
1199
1200         dcb->dcb_func = func;
1201         dcb->dcb_data = data;
1202
1203         list_insert_tail(&tx->tx_callbacks, dcb);
1204 }
1205
1206 /*
1207  * Call all the commit callbacks on a list, with a given error code.
1208  */
1209 void
1210 dmu_tx_do_callbacks(list_t *cb_list, int error)
1211 {
1212         dmu_tx_callback_t *dcb;
1213
1214         while ((dcb = list_head(cb_list)) != NULL) {
1215                 list_remove(cb_list, dcb);
1216                 dcb->dcb_func(dcb->dcb_data, error);
1217                 kmem_free(dcb, sizeof (dmu_tx_callback_t));
1218         }
1219 }
1220
1221 /*
1222  * Interface to hold a bunch of attributes.
1223  * used for creating new files.
1224  * attrsize is the total size of all attributes
1225  * to be added during object creation
1226  *
1227  * For updating/adding a single attribute dmu_tx_hold_sa() should be used.
1228  */
1229
1230 /*
1231  * hold necessary attribute name for attribute registration.
1232  * should be a very rare case where this is needed.  If it does
1233  * happen it would only happen on the first write to the file system.
1234  */
1235 static void
1236 dmu_tx_sa_registration_hold(sa_os_t *sa, dmu_tx_t *tx)
1237 {
1238         if (!sa->sa_need_attr_registration)
1239                 return;
1240
1241         for (int i = 0; i != sa->sa_num_attrs; i++) {
1242                 if (!sa->sa_attr_table[i].sa_registered) {
1243                         if (sa->sa_reg_attr_obj)
1244                                 dmu_tx_hold_zap(tx, sa->sa_reg_attr_obj,
1245                                     B_TRUE, sa->sa_attr_table[i].sa_name);
1246                         else
1247                                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT,
1248                                     B_TRUE, sa->sa_attr_table[i].sa_name);
1249                 }
1250         }
1251 }
1252
1253 void
1254 dmu_tx_hold_spill(dmu_tx_t *tx, uint64_t object)
1255 {
1256         dmu_tx_hold_t *txh;
1257
1258         txh = dmu_tx_hold_object_impl(tx, tx->tx_objset, object,
1259             THT_SPILL, 0, 0);
1260         if (txh != NULL)
1261                 (void) zfs_refcount_add_many(&txh->txh_space_towrite,
1262                     SPA_OLD_MAXBLOCKSIZE, FTAG);
1263 }
1264
1265 void
1266 dmu_tx_hold_sa_create(dmu_tx_t *tx, int attrsize)
1267 {
1268         sa_os_t *sa = tx->tx_objset->os_sa;
1269
1270         dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1271
1272         if (tx->tx_objset->os_sa->sa_master_obj == 0)
1273                 return;
1274
1275         if (tx->tx_objset->os_sa->sa_layout_attr_obj) {
1276                 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1277         } else {
1278                 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1279                 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1280                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1281                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1282         }
1283
1284         dmu_tx_sa_registration_hold(sa, tx);
1285
1286         if (attrsize <= DN_OLD_MAX_BONUSLEN && !sa->sa_force_spill)
1287                 return;
1288
1289         (void) dmu_tx_hold_object_impl(tx, tx->tx_objset, DMU_NEW_OBJECT,
1290             THT_SPILL, 0, 0);
1291 }
1292
1293 /*
1294  * Hold SA attribute
1295  *
1296  * dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *, attribute, add, size)
1297  *
1298  * variable_size is the total size of all variable sized attributes
1299  * passed to this function.  It is not the total size of all
1300  * variable size attributes that *may* exist on this object.
1301  */
1302 void
1303 dmu_tx_hold_sa(dmu_tx_t *tx, sa_handle_t *hdl, boolean_t may_grow)
1304 {
1305         uint64_t object;
1306         sa_os_t *sa = tx->tx_objset->os_sa;
1307
1308         ASSERT(hdl != NULL);
1309
1310         object = sa_handle_object(hdl);
1311
1312         dmu_tx_hold_bonus(tx, object);
1313
1314         if (tx->tx_objset->os_sa->sa_master_obj == 0)
1315                 return;
1316
1317         if (tx->tx_objset->os_sa->sa_reg_attr_obj == 0 ||
1318             tx->tx_objset->os_sa->sa_layout_attr_obj == 0) {
1319                 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_LAYOUTS);
1320                 dmu_tx_hold_zap(tx, sa->sa_master_obj, B_TRUE, SA_REGISTRY);
1321                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1322                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, B_TRUE, NULL);
1323         }
1324
1325         dmu_tx_sa_registration_hold(sa, tx);
1326
1327         if (may_grow && tx->tx_objset->os_sa->sa_layout_attr_obj)
1328                 dmu_tx_hold_zap(tx, sa->sa_layout_attr_obj, B_TRUE, NULL);
1329
1330         if (sa->sa_force_spill || may_grow || hdl->sa_spill) {
1331                 ASSERT(tx->tx_txg == 0);
1332                 dmu_tx_hold_spill(tx, object);
1333         } else {
1334                 dmu_buf_impl_t *db = (dmu_buf_impl_t *)hdl->sa_bonus;
1335                 dnode_t *dn;
1336
1337                 DB_DNODE_ENTER(db);
1338                 dn = DB_DNODE(db);
1339                 if (dn->dn_have_spill) {
1340                         ASSERT(tx->tx_txg == 0);
1341                         dmu_tx_hold_spill(tx, object);
1342                 }
1343                 DB_DNODE_EXIT(db);
1344         }
1345 }