]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dnode.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dnode.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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #pragma ident   "%Z%%M% %I%     %E% SMI"
27
28 #include <sys/zfs_context.h>
29 #include <sys/dbuf.h>
30 #include <sys/dnode.h>
31 #include <sys/dmu.h>
32 #include <sys/dmu_impl.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/spa.h>
38 #include <sys/zio.h>
39 #include <sys/dmu_zfetch.h>
40
41 static int free_range_compar(const void *node1, const void *node2);
42
43 static kmem_cache_t *dnode_cache;
44
45 static dnode_phys_t dnode_phys_zero;
46
47 int zfs_default_bs = SPA_MINBLOCKSHIFT;
48 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
49
50 /* ARGSUSED */
51 static int
52 dnode_cons(void *arg, void *unused, int kmflag)
53 {
54         int i;
55         dnode_t *dn = arg;
56         bzero(dn, sizeof (dnode_t));
57
58         cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
59         rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
60         mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
61         mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
62         refcount_create(&dn->dn_holds);
63         refcount_create(&dn->dn_tx_holds);
64
65         for (i = 0; i < TXG_SIZE; i++) {
66                 avl_create(&dn->dn_ranges[i], free_range_compar,
67                     sizeof (free_range_t),
68                     offsetof(struct free_range, fr_node));
69                 list_create(&dn->dn_dirty_records[i],
70                     sizeof (dbuf_dirty_record_t),
71                     offsetof(dbuf_dirty_record_t, dr_dirty_node));
72         }
73
74         list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
75             offsetof(dmu_buf_impl_t, db_link));
76
77         return (0);
78 }
79
80 /* ARGSUSED */
81 static void
82 dnode_dest(void *arg, void *unused)
83 {
84         int i;
85         dnode_t *dn = arg;
86
87         cv_destroy(&dn->dn_notxholds);
88         rw_destroy(&dn->dn_struct_rwlock);
89         mutex_destroy(&dn->dn_mtx);
90         mutex_destroy(&dn->dn_dbufs_mtx);
91         refcount_destroy(&dn->dn_holds);
92         refcount_destroy(&dn->dn_tx_holds);
93
94         for (i = 0; i < TXG_SIZE; i++) {
95                 avl_destroy(&dn->dn_ranges[i]);
96                 list_destroy(&dn->dn_dirty_records[i]);
97         }
98
99         list_destroy(&dn->dn_dbufs);
100 }
101
102 void
103 dnode_init(void)
104 {
105         dnode_cache = kmem_cache_create("dnode_t",
106             sizeof (dnode_t),
107             0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
108 }
109
110 void
111 dnode_fini(void)
112 {
113         kmem_cache_destroy(dnode_cache);
114 }
115
116
117 #ifdef ZFS_DEBUG
118 void
119 dnode_verify(dnode_t *dn)
120 {
121         int drop_struct_lock = FALSE;
122
123         ASSERT(dn->dn_phys);
124         ASSERT(dn->dn_objset);
125
126         ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
127
128         if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
129                 return;
130
131         if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
132                 rw_enter(&dn->dn_struct_rwlock, RW_READER);
133                 drop_struct_lock = TRUE;
134         }
135         if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
136                 int i;
137                 ASSERT3U(dn->dn_indblkshift, >=, 0);
138                 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
139                 if (dn->dn_datablkshift) {
140                         ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
141                         ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
142                         ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
143                 }
144                 ASSERT3U(dn->dn_nlevels, <=, 30);
145                 ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES);
146                 ASSERT3U(dn->dn_nblkptr, >=, 1);
147                 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
148                 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
149                 ASSERT3U(dn->dn_datablksz, ==,
150                     dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
151                 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
152                 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
153                     dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
154                 for (i = 0; i < TXG_SIZE; i++) {
155                         ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
156                 }
157         }
158         if (dn->dn_phys->dn_type != DMU_OT_NONE)
159                 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
160         ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL);
161         if (dn->dn_dbuf != NULL) {
162                 ASSERT3P(dn->dn_phys, ==,
163                     (dnode_phys_t *)dn->dn_dbuf->db.db_data +
164                     (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
165         }
166         if (drop_struct_lock)
167                 rw_exit(&dn->dn_struct_rwlock);
168 }
169 #endif
170
171 void
172 dnode_byteswap(dnode_phys_t *dnp)
173 {
174         uint64_t *buf64 = (void*)&dnp->dn_blkptr;
175         int i;
176
177         if (dnp->dn_type == DMU_OT_NONE) {
178                 bzero(dnp, sizeof (dnode_phys_t));
179                 return;
180         }
181
182         dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
183         dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
184         dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
185         dnp->dn_used = BSWAP_64(dnp->dn_used);
186
187         /*
188          * dn_nblkptr is only one byte, so it's OK to read it in either
189          * byte order.  We can't read dn_bouslen.
190          */
191         ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
192         ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
193         for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
194                 buf64[i] = BSWAP_64(buf64[i]);
195
196         /*
197          * OK to check dn_bonuslen for zero, because it won't matter if
198          * we have the wrong byte order.  This is necessary because the
199          * dnode dnode is smaller than a regular dnode.
200          */
201         if (dnp->dn_bonuslen != 0) {
202                 /*
203                  * Note that the bonus length calculated here may be
204                  * longer than the actual bonus buffer.  This is because
205                  * we always put the bonus buffer after the last block
206                  * pointer (instead of packing it against the end of the
207                  * dnode buffer).
208                  */
209                 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
210                 size_t len = DN_MAX_BONUSLEN - off;
211                 ASSERT3U(dnp->dn_bonustype, <, DMU_OT_NUMTYPES);
212                 dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len);
213         }
214 }
215
216 void
217 dnode_buf_byteswap(void *vbuf, size_t size)
218 {
219         dnode_phys_t *buf = vbuf;
220         int i;
221
222         ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
223         ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
224
225         size >>= DNODE_SHIFT;
226         for (i = 0; i < size; i++) {
227                 dnode_byteswap(buf);
228                 buf++;
229         }
230 }
231
232 static int
233 free_range_compar(const void *node1, const void *node2)
234 {
235         const free_range_t *rp1 = node1;
236         const free_range_t *rp2 = node2;
237
238         if (rp1->fr_blkid < rp2->fr_blkid)
239                 return (-1);
240         else if (rp1->fr_blkid > rp2->fr_blkid)
241                 return (1);
242         else return (0);
243 }
244
245 static void
246 dnode_setdblksz(dnode_t *dn, int size)
247 {
248         ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0);
249         ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
250         ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
251         ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
252             1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
253         dn->dn_datablksz = size;
254         dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
255         dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
256 }
257
258 static dnode_t *
259 dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
260     uint64_t object)
261 {
262         dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
263
264         dn->dn_objset = os;
265         dn->dn_object = object;
266         dn->dn_dbuf = db;
267         dn->dn_phys = dnp;
268
269         if (dnp->dn_datablkszsec)
270                 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
271         dn->dn_indblkshift = dnp->dn_indblkshift;
272         dn->dn_nlevels = dnp->dn_nlevels;
273         dn->dn_type = dnp->dn_type;
274         dn->dn_nblkptr = dnp->dn_nblkptr;
275         dn->dn_checksum = dnp->dn_checksum;
276         dn->dn_compress = dnp->dn_compress;
277         dn->dn_bonustype = dnp->dn_bonustype;
278         dn->dn_bonuslen = dnp->dn_bonuslen;
279         dn->dn_maxblkid = dnp->dn_maxblkid;
280
281         dmu_zfetch_init(&dn->dn_zfetch, dn);
282
283         ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES);
284         mutex_enter(&os->os_lock);
285         list_insert_head(&os->os_dnodes, dn);
286         mutex_exit(&os->os_lock);
287
288         return (dn);
289 }
290
291 static void
292 dnode_destroy(dnode_t *dn)
293 {
294         objset_impl_t *os = dn->dn_objset;
295
296 #ifdef ZFS_DEBUG
297         int i;
298
299         for (i = 0; i < TXG_SIZE; i++) {
300                 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
301                 ASSERT(NULL == list_head(&dn->dn_dirty_records[i]));
302                 ASSERT(0 == avl_numnodes(&dn->dn_ranges[i]));
303         }
304         ASSERT(NULL == list_head(&dn->dn_dbufs));
305 #endif
306
307         mutex_enter(&os->os_lock);
308         list_remove(&os->os_dnodes, dn);
309         mutex_exit(&os->os_lock);
310
311         if (dn->dn_dirtyctx_firstset) {
312                 kmem_free(dn->dn_dirtyctx_firstset, 1);
313                 dn->dn_dirtyctx_firstset = NULL;
314         }
315         dmu_zfetch_rele(&dn->dn_zfetch);
316         if (dn->dn_bonus) {
317                 mutex_enter(&dn->dn_bonus->db_mtx);
318                 dbuf_evict(dn->dn_bonus);
319                 dn->dn_bonus = NULL;
320         }
321         kmem_cache_free(dnode_cache, dn);
322 }
323
324 void
325 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
326     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
327 {
328         int i;
329
330         if (blocksize == 0)
331                 blocksize = 1 << zfs_default_bs;
332         else if (blocksize > SPA_MAXBLOCKSIZE)
333                 blocksize = SPA_MAXBLOCKSIZE;
334         else
335                 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
336
337         if (ibs == 0)
338                 ibs = zfs_default_ibs;
339
340         ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
341
342         dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
343             dn->dn_object, tx->tx_txg, blocksize, ibs);
344
345         ASSERT(dn->dn_type == DMU_OT_NONE);
346         ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
347         ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
348         ASSERT(ot != DMU_OT_NONE);
349         ASSERT3U(ot, <, DMU_OT_NUMTYPES);
350         ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
351             (bonustype != DMU_OT_NONE && bonuslen != 0));
352         ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
353         ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
354         ASSERT(dn->dn_type == DMU_OT_NONE);
355         ASSERT3U(dn->dn_maxblkid, ==, 0);
356         ASSERT3U(dn->dn_allocated_txg, ==, 0);
357         ASSERT3U(dn->dn_assigned_txg, ==, 0);
358         ASSERT(refcount_is_zero(&dn->dn_tx_holds));
359         ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
360         ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
361
362         for (i = 0; i < TXG_SIZE; i++) {
363                 ASSERT3U(dn->dn_next_nlevels[i], ==, 0);
364                 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0);
365                 ASSERT3U(dn->dn_next_blksz[i], ==, 0);
366                 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
367                 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
368                 ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0);
369         }
370
371         dn->dn_type = ot;
372         dnode_setdblksz(dn, blocksize);
373         dn->dn_indblkshift = ibs;
374         dn->dn_nlevels = 1;
375         dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
376         dn->dn_bonustype = bonustype;
377         dn->dn_bonuslen = bonuslen;
378         dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
379         dn->dn_compress = ZIO_COMPRESS_INHERIT;
380         dn->dn_dirtyctx = 0;
381
382         dn->dn_free_txg = 0;
383         if (dn->dn_dirtyctx_firstset) {
384                 kmem_free(dn->dn_dirtyctx_firstset, 1);
385                 dn->dn_dirtyctx_firstset = NULL;
386         }
387
388         dn->dn_allocated_txg = tx->tx_txg;
389
390         dnode_setdirty(dn, tx);
391         dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
392         dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
393 }
394
395 void
396 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
397     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
398 {
399         int i;
400         dmu_buf_impl_t *db = NULL;
401
402         ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
403         ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
404         ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0);
405         ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
406         ASSERT(tx->tx_txg != 0);
407         ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
408             (bonustype != DMU_OT_NONE && bonuslen != 0));
409         ASSERT3U(bonustype, <, DMU_OT_NUMTYPES);
410         ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
411
412         for (i = 0; i < TXG_SIZE; i++)
413                 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
414
415         /* clean up any unreferenced dbufs */
416         (void) dnode_evict_dbufs(dn, 0);
417         ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
418
419         /*
420          * XXX I should really have a generation number to tell if we
421          * need to do this...
422          */
423         if (blocksize != dn->dn_datablksz ||
424             dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) {
425                 /* free all old data */
426                 dnode_free_range(dn, 0, -1ULL, tx);
427         }
428
429         /* change blocksize */
430         rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
431         if (blocksize != dn->dn_datablksz &&
432             (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
433             list_head(&dn->dn_dbufs) != NULL)) {
434                 db = dbuf_hold(dn, 0, FTAG);
435                 dbuf_new_size(db, blocksize, tx);
436         }
437         dnode_setdblksz(dn, blocksize);
438         dnode_setdirty(dn, tx);
439         dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
440         rw_exit(&dn->dn_struct_rwlock);
441         if (db) {
442                 dbuf_rele(db, FTAG);
443                 db = NULL;
444         }
445
446         /* change type */
447         dn->dn_type = ot;
448
449         if (dn->dn_bonuslen != bonuslen) {
450                 /* change bonus size */
451                 if (bonuslen == 0)
452                         bonuslen = 1; /* XXX */
453                 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
454                 if (dn->dn_bonus == NULL)
455                         dn->dn_bonus = dbuf_create_bonus(dn);
456                 db = dn->dn_bonus;
457                 rw_exit(&dn->dn_struct_rwlock);
458                 if (refcount_add(&db->db_holds, FTAG) == 1)
459                         dnode_add_ref(dn, db);
460                 VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED));
461                 mutex_enter(&db->db_mtx);
462                 ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen);
463                 ASSERT(db->db.db_data != NULL);
464                 db->db.db_size = bonuslen;
465                 mutex_exit(&db->db_mtx);
466                 (void) dbuf_dirty(db, tx);
467         }
468
469         /* change bonus size and type */
470         mutex_enter(&dn->dn_mtx);
471         dn->dn_bonustype = bonustype;
472         dn->dn_bonuslen = bonuslen;
473         dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
474         dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
475         dn->dn_compress = ZIO_COMPRESS_INHERIT;
476         ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
477
478         /*
479          * NB: we have to do the dbuf_rele after we've changed the
480          * dn_bonuslen, for the sake of dbuf_verify().
481          */
482         if (db)
483                 dbuf_rele(db, FTAG);
484
485         dn->dn_allocated_txg = tx->tx_txg;
486         mutex_exit(&dn->dn_mtx);
487 }
488
489 void
490 dnode_special_close(dnode_t *dn)
491 {
492         /*
493          * Wait for final references to the dnode to clear.  This can
494          * only happen if the arc is asyncronously evicting state that
495          * has a hold on this dnode while we are trying to evict this
496          * dnode.
497          */
498         while (refcount_count(&dn->dn_holds) > 0)
499                 delay(1);
500         dnode_destroy(dn);
501 }
502
503 dnode_t *
504 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object)
505 {
506         dnode_t *dn = dnode_create(os, dnp, NULL, object);
507         DNODE_VERIFY(dn);
508         return (dn);
509 }
510
511 static void
512 dnode_buf_pageout(dmu_buf_t *db, void *arg)
513 {
514         dnode_t **children_dnodes = arg;
515         int i;
516         int epb = db->db_size >> DNODE_SHIFT;
517
518         for (i = 0; i < epb; i++) {
519                 dnode_t *dn = children_dnodes[i];
520                 int n;
521
522                 if (dn == NULL)
523                         continue;
524 #ifdef ZFS_DEBUG
525                 /*
526                  * If there are holds on this dnode, then there should
527                  * be holds on the dnode's containing dbuf as well; thus
528                  * it wouldn't be eligable for eviction and this function
529                  * would not have been called.
530                  */
531                 ASSERT(refcount_is_zero(&dn->dn_holds));
532                 ASSERT(list_head(&dn->dn_dbufs) == NULL);
533                 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
534
535                 for (n = 0; n < TXG_SIZE; n++)
536                         ASSERT(!list_link_active(&dn->dn_dirty_link[n]));
537 #endif
538                 children_dnodes[i] = NULL;
539                 dnode_destroy(dn);
540         }
541         kmem_free(children_dnodes, epb * sizeof (dnode_t *));
542 }
543
544 /*
545  * errors:
546  * EINVAL - invalid object number.
547  * EIO - i/o error.
548  * succeeds even for free dnodes.
549  */
550 int
551 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag,
552     void *tag, dnode_t **dnp)
553 {
554         int epb, idx, err;
555         int drop_struct_lock = FALSE;
556         int type;
557         uint64_t blk;
558         dnode_t *mdn, *dn;
559         dmu_buf_impl_t *db;
560         dnode_t **children_dnodes;
561
562         if (object == 0 || object >= DN_MAX_OBJECT)
563                 return (EINVAL);
564
565         mdn = os->os_meta_dnode;
566
567         DNODE_VERIFY(mdn);
568
569         if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
570                 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
571                 drop_struct_lock = TRUE;
572         }
573
574         blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
575
576         db = dbuf_hold(mdn, blk, FTAG);
577         if (drop_struct_lock)
578                 rw_exit(&mdn->dn_struct_rwlock);
579         if (db == NULL)
580                 return (EIO);
581         err = dbuf_read(db, NULL, DB_RF_CANFAIL);
582         if (err) {
583                 dbuf_rele(db, FTAG);
584                 return (err);
585         }
586
587         ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
588         epb = db->db.db_size >> DNODE_SHIFT;
589
590         idx = object & (epb-1);
591
592         children_dnodes = dmu_buf_get_user(&db->db);
593         if (children_dnodes == NULL) {
594                 dnode_t **winner;
595                 children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *),
596                     KM_SLEEP);
597                 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
598                     dnode_buf_pageout)) {
599                         kmem_free(children_dnodes, epb * sizeof (dnode_t *));
600                         children_dnodes = winner;
601                 }
602         }
603
604         if ((dn = children_dnodes[idx]) == NULL) {
605                 dnode_t *winner;
606                 dn = dnode_create(os, (dnode_phys_t *)db->db.db_data+idx,
607                         db, object);
608                 winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn);
609                 if (winner != NULL) {
610                         dnode_destroy(dn);
611                         dn = winner;
612                 }
613         }
614
615         mutex_enter(&dn->dn_mtx);
616         type = dn->dn_type;
617         if (dn->dn_free_txg ||
618             ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
619             ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) {
620                 mutex_exit(&dn->dn_mtx);
621                 dbuf_rele(db, FTAG);
622                 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
623         }
624         mutex_exit(&dn->dn_mtx);
625
626         if (refcount_add(&dn->dn_holds, tag) == 1)
627                 dbuf_add_ref(db, dn);
628
629         DNODE_VERIFY(dn);
630         ASSERT3P(dn->dn_dbuf, ==, db);
631         ASSERT3U(dn->dn_object, ==, object);
632         dbuf_rele(db, FTAG);
633
634         *dnp = dn;
635         return (0);
636 }
637
638 /*
639  * Return held dnode if the object is allocated, NULL if not.
640  */
641 int
642 dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp)
643 {
644         return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
645 }
646
647 void
648 dnode_add_ref(dnode_t *dn, void *tag)
649 {
650         ASSERT(refcount_count(&dn->dn_holds) > 0);
651         (void) refcount_add(&dn->dn_holds, tag);
652 }
653
654 void
655 dnode_rele(dnode_t *dn, void *tag)
656 {
657         uint64_t refs;
658
659         refs = refcount_remove(&dn->dn_holds, tag);
660         /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
661         if (refs == 0 && dn->dn_dbuf)
662                 dbuf_rele(dn->dn_dbuf, dn);
663 }
664
665 void
666 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
667 {
668         objset_impl_t *os = dn->dn_objset;
669         uint64_t txg = tx->tx_txg;
670
671         if (dn->dn_object == DMU_META_DNODE_OBJECT)
672                 return;
673
674         DNODE_VERIFY(dn);
675
676 #ifdef ZFS_DEBUG
677         mutex_enter(&dn->dn_mtx);
678         ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
679         /* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */
680         mutex_exit(&dn->dn_mtx);
681 #endif
682
683         mutex_enter(&os->os_lock);
684
685         /*
686          * If we are already marked dirty, we're done.
687          */
688         if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
689                 mutex_exit(&os->os_lock);
690                 return;
691         }
692
693         ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
694         ASSERT(dn->dn_datablksz != 0);
695         ASSERT3U(dn->dn_next_blksz[txg&TXG_MASK], ==, 0);
696
697         dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
698             dn->dn_object, txg);
699
700         if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
701                 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
702         } else {
703                 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
704         }
705
706         mutex_exit(&os->os_lock);
707
708         /*
709          * The dnode maintains a hold on its containing dbuf as
710          * long as there are holds on it.  Each instantiated child
711          * dbuf maintaines a hold on the dnode.  When the last child
712          * drops its hold, the dnode will drop its hold on the
713          * containing dbuf. We add a "dirty hold" here so that the
714          * dnode will hang around after we finish processing its
715          * children.
716          */
717         dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg);
718
719         (void) dbuf_dirty(dn->dn_dbuf, tx);
720
721         dsl_dataset_dirty(os->os_dsl_dataset, tx);
722 }
723
724 void
725 dnode_free(dnode_t *dn, dmu_tx_t *tx)
726 {
727         int txgoff = tx->tx_txg & TXG_MASK;
728
729         dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
730
731         /* we should be the only holder... hopefully */
732         /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
733
734         mutex_enter(&dn->dn_mtx);
735         if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
736                 mutex_exit(&dn->dn_mtx);
737                 return;
738         }
739         dn->dn_free_txg = tx->tx_txg;
740         mutex_exit(&dn->dn_mtx);
741
742         /*
743          * If the dnode is already dirty, it needs to be moved from
744          * the dirty list to the free list.
745          */
746         mutex_enter(&dn->dn_objset->os_lock);
747         if (list_link_active(&dn->dn_dirty_link[txgoff])) {
748                 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
749                 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
750                 mutex_exit(&dn->dn_objset->os_lock);
751         } else {
752                 mutex_exit(&dn->dn_objset->os_lock);
753                 dnode_setdirty(dn, tx);
754         }
755 }
756
757 /*
758  * Try to change the block size for the indicated dnode.  This can only
759  * succeed if there are no blocks allocated or dirty beyond first block
760  */
761 int
762 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
763 {
764         dmu_buf_impl_t *db, *db_next;
765         int have_db0 = FALSE;
766
767         if (size == 0)
768                 size = SPA_MINBLOCKSIZE;
769         if (size > SPA_MAXBLOCKSIZE)
770                 size = SPA_MAXBLOCKSIZE;
771         else
772                 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
773
774         if (ibs == dn->dn_indblkshift)
775                 ibs = 0;
776
777         if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
778                 return (0);
779
780         rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
781
782         /* Check for any allocated blocks beyond the first */
783         if (dn->dn_phys->dn_maxblkid != 0)
784                 goto fail;
785
786         mutex_enter(&dn->dn_dbufs_mtx);
787         for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
788                 db_next = list_next(&dn->dn_dbufs, db);
789
790                 if (db->db_blkid == 0) {
791                         have_db0 = TRUE;
792                 } else if (db->db_blkid != DB_BONUS_BLKID) {
793                         mutex_exit(&dn->dn_dbufs_mtx);
794                         goto fail;
795                 }
796         }
797         mutex_exit(&dn->dn_dbufs_mtx);
798
799         if (ibs && dn->dn_nlevels != 1)
800                 goto fail;
801
802         db = NULL;
803         if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || have_db0) {
804                 /* obtain the old block */
805                 db = dbuf_hold(dn, 0, FTAG);
806                 dbuf_new_size(db, size, tx);
807         }
808
809         dnode_setdblksz(dn, size);
810         dnode_setdirty(dn, tx);
811         dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
812         if (ibs) {
813                 dn->dn_indblkshift = ibs;
814                 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
815         }
816
817         if (db)
818                 dbuf_rele(db, FTAG);
819
820         rw_exit(&dn->dn_struct_rwlock);
821         return (0);
822
823 fail:
824         rw_exit(&dn->dn_struct_rwlock);
825         return (ENOTSUP);
826 }
827
828 void
829 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx)
830 {
831         uint64_t txgoff = tx->tx_txg & TXG_MASK;
832         int drop_struct_lock = FALSE;
833         int epbs, new_nlevels;
834         uint64_t sz;
835
836         ASSERT(blkid != DB_BONUS_BLKID);
837
838         if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
839                 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
840                 drop_struct_lock = TRUE;
841         }
842
843         if (blkid <= dn->dn_maxblkid)
844                 goto out;
845
846         dn->dn_maxblkid = blkid;
847
848         /*
849          * Compute the number of levels necessary to support the new maxblkid.
850          */
851         new_nlevels = 1;
852         epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
853         for (sz = dn->dn_nblkptr;
854             sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
855                 new_nlevels++;
856
857         if (new_nlevels > dn->dn_nlevels) {
858                 int old_nlevels = dn->dn_nlevels;
859                 dmu_buf_impl_t *db;
860                 list_t *list;
861                 dbuf_dirty_record_t *new, *dr, *dr_next;
862
863                 dn->dn_nlevels = new_nlevels;
864
865                 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
866                 dn->dn_next_nlevels[txgoff] = new_nlevels;
867
868                 /* dirty the left indirects */
869                 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
870                 new = dbuf_dirty(db, tx);
871                 dbuf_rele(db, FTAG);
872
873                 /* transfer the dirty records to the new indirect */
874                 mutex_enter(&dn->dn_mtx);
875                 mutex_enter(&new->dt.di.dr_mtx);
876                 list = &dn->dn_dirty_records[txgoff];
877                 for (dr = list_head(list); dr; dr = dr_next) {
878                         dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
879                         if (dr->dr_dbuf->db_level != new_nlevels-1 &&
880                             dr->dr_dbuf->db_blkid != DB_BONUS_BLKID) {
881                                 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
882                                 list_remove(&dn->dn_dirty_records[txgoff], dr);
883                                 list_insert_tail(&new->dt.di.dr_children, dr);
884                                 dr->dr_parent = new;
885                         }
886                 }
887                 mutex_exit(&new->dt.di.dr_mtx);
888                 mutex_exit(&dn->dn_mtx);
889         }
890
891 out:
892         if (drop_struct_lock)
893                 rw_exit(&dn->dn_struct_rwlock);
894 }
895
896 void
897 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
898 {
899         avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
900         avl_index_t where;
901         free_range_t *rp;
902         free_range_t rp_tofind;
903         uint64_t endblk = blkid + nblks;
904
905         ASSERT(MUTEX_HELD(&dn->dn_mtx));
906         ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
907
908         dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
909             blkid, nblks, tx->tx_txg);
910         rp_tofind.fr_blkid = blkid;
911         rp = avl_find(tree, &rp_tofind, &where);
912         if (rp == NULL)
913                 rp = avl_nearest(tree, where, AVL_BEFORE);
914         if (rp == NULL)
915                 rp = avl_nearest(tree, where, AVL_AFTER);
916
917         while (rp && (rp->fr_blkid <= blkid + nblks)) {
918                 uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
919                 free_range_t *nrp = AVL_NEXT(tree, rp);
920
921                 if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
922                         /* clear this entire range */
923                         avl_remove(tree, rp);
924                         kmem_free(rp, sizeof (free_range_t));
925                 } else if (blkid <= rp->fr_blkid &&
926                     endblk > rp->fr_blkid && endblk < fr_endblk) {
927                         /* clear the beginning of this range */
928                         rp->fr_blkid = endblk;
929                         rp->fr_nblks = fr_endblk - endblk;
930                 } else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
931                     endblk >= fr_endblk) {
932                         /* clear the end of this range */
933                         rp->fr_nblks = blkid - rp->fr_blkid;
934                 } else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
935                         /* clear a chunk out of this range */
936                         free_range_t *new_rp =
937                             kmem_alloc(sizeof (free_range_t), KM_SLEEP);
938
939                         new_rp->fr_blkid = endblk;
940                         new_rp->fr_nblks = fr_endblk - endblk;
941                         avl_insert_here(tree, new_rp, rp, AVL_AFTER);
942                         rp->fr_nblks = blkid - rp->fr_blkid;
943                 }
944                 /* there may be no overlap */
945                 rp = nrp;
946         }
947 }
948
949 void
950 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
951 {
952         dmu_buf_impl_t *db;
953         uint64_t blkoff, blkid, nblks;
954         int blksz, head;
955         int trunc = FALSE;
956
957         rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
958         blksz = dn->dn_datablksz;
959
960         /* If the range is past the end of the file, this is a no-op */
961         if (off >= blksz * (dn->dn_maxblkid+1))
962                 goto out;
963         if (len == -1ULL) {
964                 len = UINT64_MAX - off;
965                 trunc = TRUE;
966         }
967
968         /*
969          * First, block align the region to free:
970          */
971         if (ISP2(blksz)) {
972                 head = P2NPHASE(off, blksz);
973                 blkoff = P2PHASE(off, blksz);
974         } else {
975                 ASSERT(dn->dn_maxblkid == 0);
976                 if (off == 0 && len >= blksz) {
977                         /* Freeing the whole block; don't do any head. */
978                         head = 0;
979                 } else {
980                         /* Freeing part of the block. */
981                         head = blksz - off;
982                         ASSERT3U(head, >, 0);
983                 }
984                 blkoff = off;
985         }
986         /* zero out any partial block data at the start of the range */
987         if (head) {
988                 ASSERT3U(blkoff + head, ==, blksz);
989                 if (len < head)
990                         head = len;
991                 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
992                     FTAG, &db) == 0) {
993                         caddr_t data;
994
995                         /* don't dirty if it isn't on disk and isn't dirty */
996                         if (db->db_last_dirty ||
997                             (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
998                                 rw_exit(&dn->dn_struct_rwlock);
999                                 dbuf_will_dirty(db, tx);
1000                                 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1001                                 data = db->db.db_data;
1002                                 bzero(data + blkoff, head);
1003                         }
1004                         dbuf_rele(db, FTAG);
1005                 }
1006                 off += head;
1007                 len -= head;
1008         }
1009
1010         /* If the range was less than one block, we're done */
1011         if (len == 0 || off >= blksz * (dn->dn_maxblkid+1))
1012                 goto out;
1013
1014         if (!ISP2(blksz)) {
1015                 /*
1016                  * They are freeing the whole block of a
1017                  * non-power-of-two blocksize file.  Skip all the messy
1018                  * math.
1019                  */
1020                 ASSERT3U(off, ==, 0);
1021                 ASSERT3U(len, >=, blksz);
1022                 blkid = 0;
1023                 nblks = 1;
1024         } else {
1025                 int tail;
1026                 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1027                 int blkshift = dn->dn_datablkshift;
1028
1029                 /* If the remaining range is past end of file, we're done */
1030                 if (off > dn->dn_maxblkid << blkshift)
1031                         goto out;
1032
1033                 if (off + len == UINT64_MAX)
1034                         tail = 0;
1035                 else
1036                         tail = P2PHASE(len, blksz);
1037
1038                 ASSERT3U(P2PHASE(off, blksz), ==, 0);
1039                 /* zero out any partial block data at the end of the range */
1040                 if (tail) {
1041                         if (len < tail)
1042                                 tail = len;
1043                         if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1044                             TRUE, FTAG, &db) == 0) {
1045                                 /* don't dirty if not on disk and not dirty */
1046                                 if (db->db_last_dirty ||
1047                                     (db->db_blkptr &&
1048                                     !BP_IS_HOLE(db->db_blkptr))) {
1049                                         rw_exit(&dn->dn_struct_rwlock);
1050                                         dbuf_will_dirty(db, tx);
1051                                         rw_enter(&dn->dn_struct_rwlock,
1052                                             RW_WRITER);
1053                                         bzero(db->db.db_data, tail);
1054                                 }
1055                                 dbuf_rele(db, FTAG);
1056                         }
1057                         len -= tail;
1058                 }
1059                 /* If the range did not include a full block, we are done */
1060                 if (len == 0)
1061                         goto out;
1062
1063                 /* dirty the left indirects */
1064                 if (dn->dn_nlevels > 1 && off != 0) {
1065                         db = dbuf_hold_level(dn, 1,
1066                             (off - head) >> (blkshift + epbs), FTAG);
1067                         dbuf_will_dirty(db, tx);
1068                         dbuf_rele(db, FTAG);
1069                 }
1070
1071                 /* dirty the right indirects */
1072                 if (dn->dn_nlevels > 1 && !trunc) {
1073                         db = dbuf_hold_level(dn, 1,
1074                             (off + len + tail - 1) >> (blkshift + epbs), FTAG);
1075                         dbuf_will_dirty(db, tx);
1076                         dbuf_rele(db, FTAG);
1077                 }
1078
1079                 /*
1080                  * Finally, add this range to the dnode range list, we
1081                  * will finish up this free operation in the syncing phase.
1082                  */
1083                 ASSERT(IS_P2ALIGNED(off, 1<<blkshift));
1084                 ASSERT(off + len == UINT64_MAX ||
1085                     IS_P2ALIGNED(len, 1<<blkshift));
1086                 blkid = off >> blkshift;
1087                 nblks = len >> blkshift;
1088
1089                 if (trunc)
1090                         dn->dn_maxblkid = (blkid ? blkid - 1 : 0);
1091         }
1092
1093         mutex_enter(&dn->dn_mtx);
1094         dnode_clear_range(dn, blkid, nblks, tx);
1095         {
1096                 free_range_t *rp, *found;
1097                 avl_index_t where;
1098                 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1099
1100                 /* Add new range to dn_ranges */
1101                 rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1102                 rp->fr_blkid = blkid;
1103                 rp->fr_nblks = nblks;
1104                 found = avl_find(tree, rp, &where);
1105                 ASSERT(found == NULL);
1106                 avl_insert(tree, rp, where);
1107                 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1108                     blkid, nblks, tx->tx_txg);
1109         }
1110         mutex_exit(&dn->dn_mtx);
1111
1112         dbuf_free_range(dn, blkid, nblks, tx);
1113         dnode_setdirty(dn, tx);
1114 out:
1115         rw_exit(&dn->dn_struct_rwlock);
1116 }
1117
1118 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1119 uint64_t
1120 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1121 {
1122         free_range_t range_tofind;
1123         void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1124         int i;
1125
1126         if (blkid == DB_BONUS_BLKID)
1127                 return (FALSE);
1128
1129         /*
1130          * If we're in the process of opening the pool, dp will not be
1131          * set yet, but there shouldn't be anything dirty.
1132          */
1133         if (dp == NULL)
1134                 return (FALSE);
1135
1136         if (dn->dn_free_txg)
1137                 return (TRUE);
1138
1139         /*
1140          * If dn_datablkshift is not set, then there's only a single
1141          * block, in which case there will never be a free range so it
1142          * won't matter.
1143          */
1144         range_tofind.fr_blkid = blkid;
1145         mutex_enter(&dn->dn_mtx);
1146         for (i = 0; i < TXG_SIZE; i++) {
1147                 free_range_t *range_found;
1148                 avl_index_t idx;
1149
1150                 range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1151                 if (range_found) {
1152                         ASSERT(range_found->fr_nblks > 0);
1153                         break;
1154                 }
1155                 range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1156                 if (range_found &&
1157                     range_found->fr_blkid + range_found->fr_nblks > blkid)
1158                         break;
1159         }
1160         mutex_exit(&dn->dn_mtx);
1161         return (i < TXG_SIZE);
1162 }
1163
1164 /* call from syncing context when we actually write/free space for this dnode */
1165 void
1166 dnode_diduse_space(dnode_t *dn, int64_t delta)
1167 {
1168         uint64_t space;
1169         dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1170             dn, dn->dn_phys,
1171             (u_longlong_t)dn->dn_phys->dn_used,
1172             (longlong_t)delta);
1173
1174         mutex_enter(&dn->dn_mtx);
1175         space = DN_USED_BYTES(dn->dn_phys);
1176         if (delta > 0) {
1177                 ASSERT3U(space + delta, >=, space); /* no overflow */
1178         } else {
1179                 ASSERT3U(space, >=, -delta); /* no underflow */
1180         }
1181         space += delta;
1182         if (spa_version(dn->dn_objset->os_spa) < ZFS_VERSION_DNODE_BYTES) {
1183                 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1184                 ASSERT3U(P2PHASE(space, 1<<DEV_BSHIFT), ==, 0);
1185                 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1186         } else {
1187                 dn->dn_phys->dn_used = space;
1188                 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1189         }
1190         mutex_exit(&dn->dn_mtx);
1191 }
1192
1193 /*
1194  * Call when we think we're going to write/free space in open context.
1195  * Be conservative (ie. OK to write less than this or free more than
1196  * this, but don't write more or free less).
1197  */
1198 void
1199 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1200 {
1201         objset_impl_t *os = dn->dn_objset;
1202         dsl_dataset_t *ds = os->os_dsl_dataset;
1203
1204         if (space > 0)
1205                 space = spa_get_asize(os->os_spa, space);
1206
1207         if (ds)
1208                 dsl_dir_willuse_space(ds->ds_dir, space, tx);
1209
1210         dmu_tx_willuse_space(tx, space);
1211 }
1212
1213 static int
1214 dnode_next_offset_level(dnode_t *dn, boolean_t hole, uint64_t *offset,
1215         int lvl, uint64_t blkfill, uint64_t txg)
1216 {
1217         dmu_buf_impl_t *db = NULL;
1218         void *data = NULL;
1219         uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1220         uint64_t epb = 1ULL << epbs;
1221         uint64_t minfill, maxfill;
1222         int i, error, span;
1223
1224         dprintf("probing object %llu offset %llx level %d of %u\n",
1225             dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1226
1227         if (lvl == dn->dn_phys->dn_nlevels) {
1228                 error = 0;
1229                 epb = dn->dn_phys->dn_nblkptr;
1230                 data = dn->dn_phys->dn_blkptr;
1231         } else {
1232                 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1233                 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1234                 if (error) {
1235                         if (error == ENOENT)
1236                                 return (hole ? 0 : ESRCH);
1237                         return (error);
1238                 }
1239                 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1240                 if (error) {
1241                         dbuf_rele(db, FTAG);
1242                         return (error);
1243                 }
1244                 data = db->db.db_data;
1245         }
1246
1247         if (db && txg &&
1248             (db->db_blkptr == NULL || db->db_blkptr->blk_birth <= txg)) {
1249                 error = ESRCH;
1250         } else if (lvl == 0) {
1251                 dnode_phys_t *dnp = data;
1252                 span = DNODE_SHIFT;
1253                 ASSERT(dn->dn_type == DMU_OT_DNODE);
1254
1255                 for (i = (*offset >> span) & (blkfill - 1); i < blkfill; i++) {
1256                         boolean_t newcontents = B_TRUE;
1257                         if (txg) {
1258                                 int j;
1259                                 newcontents = B_FALSE;
1260                                 for (j = 0; j < dnp[i].dn_nblkptr; j++) {
1261                                         if (dnp[i].dn_blkptr[j].blk_birth > txg)
1262                                                 newcontents = B_TRUE;
1263                                 }
1264                         }
1265                         if (!dnp[i].dn_type == hole && newcontents)
1266                                 break;
1267                         *offset += 1ULL << span;
1268                 }
1269                 if (i == blkfill)
1270                         error = ESRCH;
1271         } else {
1272                 blkptr_t *bp = data;
1273                 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1274                 minfill = 0;
1275                 maxfill = blkfill << ((lvl - 1) * epbs);
1276
1277                 if (hole)
1278                         maxfill--;
1279                 else
1280                         minfill++;
1281
1282                 for (i = (*offset >> span) & ((1ULL << epbs) - 1);
1283                     i < epb; i++) {
1284                         if (bp[i].blk_fill >= minfill &&
1285                             bp[i].blk_fill <= maxfill &&
1286                             bp[i].blk_birth > txg)
1287                                 break;
1288                         *offset += 1ULL << span;
1289                 }
1290                 if (i >= epb)
1291                         error = ESRCH;
1292         }
1293
1294         if (db)
1295                 dbuf_rele(db, FTAG);
1296
1297         return (error);
1298 }
1299
1300 /*
1301  * Find the next hole, data, or sparse region at or after *offset.
1302  * The value 'blkfill' tells us how many items we expect to find
1303  * in an L0 data block; this value is 1 for normal objects,
1304  * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1305  * DNODES_PER_BLOCK when searching for sparse regions thereof.
1306  *
1307  * Examples:
1308  *
1309  * dnode_next_offset(dn, hole, offset, 1, 1, 0);
1310  *      Finds the next hole/data in a file.
1311  *      Used in dmu_offset_next().
1312  *
1313  * dnode_next_offset(mdn, hole, offset, 0, DNODES_PER_BLOCK, txg);
1314  *      Finds the next free/allocated dnode an objset's meta-dnode.
1315  *      Only finds objects that have new contents since txg (ie.
1316  *      bonus buffer changes and content removal are ignored).
1317  *      Used in dmu_object_next().
1318  *
1319  * dnode_next_offset(mdn, TRUE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1320  *      Finds the next L2 meta-dnode bp that's at most 1/4 full.
1321  *      Used in dmu_object_alloc().
1322  */
1323 int
1324 dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *offset,
1325     int minlvl, uint64_t blkfill, uint64_t txg)
1326 {
1327         int lvl, maxlvl;
1328         int error = 0;
1329         uint64_t initial_offset = *offset;
1330
1331         rw_enter(&dn->dn_struct_rwlock, RW_READER);
1332
1333         if (dn->dn_phys->dn_nlevels == 0) {
1334                 rw_exit(&dn->dn_struct_rwlock);
1335                 return (ESRCH);
1336         }
1337
1338         if (dn->dn_datablkshift == 0) {
1339                 if (*offset < dn->dn_datablksz) {
1340                         if (hole)
1341                                 *offset = dn->dn_datablksz;
1342                 } else {
1343                         error = ESRCH;
1344                 }
1345                 rw_exit(&dn->dn_struct_rwlock);
1346                 return (error);
1347         }
1348
1349         maxlvl = dn->dn_phys->dn_nlevels;
1350
1351         for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1352                 error = dnode_next_offset_level(dn,
1353                     hole, offset, lvl, blkfill, txg);
1354                 if (error != ESRCH)
1355                         break;
1356         }
1357
1358         while (--lvl >= minlvl && error == 0) {
1359                 error = dnode_next_offset_level(dn,
1360                     hole, offset, lvl, blkfill, txg);
1361         }
1362
1363         rw_exit(&dn->dn_struct_rwlock);
1364
1365         if (error == 0 && initial_offset > *offset)
1366                 error = ESRCH;
1367
1368         return (error);
1369 }