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