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