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