]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dnode_sync.c
Illumos 4171, 4172
[mirror_zfs.git] / module / zfs / dnode_sync.c
CommitLineData
34dc7c2f
BB
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 */
9ae529ec 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
fa86b5db 24 * Copyright (c) 2013 by Delphix. All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27#include <sys/zfs_context.h>
28#include <sys/dbuf.h>
29#include <sys/dnode.h>
30#include <sys/dmu.h>
31#include <sys/dmu_tx.h>
32#include <sys/dmu_objset.h>
33#include <sys/dsl_dataset.h>
34#include <sys/spa.h>
35
36static void
37dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
38{
39 dmu_buf_impl_t *db;
40 int txgoff = tx->tx_txg & TXG_MASK;
41 int nblkptr = dn->dn_phys->dn_nblkptr;
42 int old_toplvl = dn->dn_phys->dn_nlevels - 1;
43 int new_level = dn->dn_next_nlevels[txgoff];
44 int i;
45
46 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
47
48 /* this dnode can't be paged out because it's dirty */
49 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
50 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
51 ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0);
52
53 db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG);
54 ASSERT(db != NULL);
55
56 dn->dn_phys->dn_nlevels = new_level;
57 dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset,
58 dn->dn_object, dn->dn_phys->dn_nlevels);
59
60 /* check for existing blkptrs in the dnode */
61 for (i = 0; i < nblkptr; i++)
62 if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i]))
63 break;
64 if (i != nblkptr) {
65 /* transfer dnode's block pointers to new indirect block */
66 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT);
67 ASSERT(db->db.db_data);
68 ASSERT(arc_released(db->db_buf));
69 ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size);
70 bcopy(dn->dn_phys->dn_blkptr, db->db.db_data,
71 sizeof (blkptr_t) * nblkptr);
72 arc_buf_freeze(db->db_buf);
73 }
74
75 /* set dbuf's parent pointers to new indirect buf */
76 for (i = 0; i < nblkptr; i++) {
77 dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
78
79 if (child == NULL)
80 continue;
572e2857
BB
81#ifdef DEBUG
82 DB_DNODE_ENTER(child);
83 ASSERT3P(DB_DNODE(child), ==, dn);
84 DB_DNODE_EXIT(child);
85#endif /* DEBUG */
34dc7c2f
BB
86 if (child->db_parent && child->db_parent != dn->dn_dbuf) {
87 ASSERT(child->db_parent->db_level == db->db_level);
88 ASSERT(child->db_blkptr !=
89 &dn->dn_phys->dn_blkptr[child->db_blkid]);
90 mutex_exit(&child->db_mtx);
91 continue;
92 }
93 ASSERT(child->db_parent == NULL ||
94 child->db_parent == dn->dn_dbuf);
95
96 child->db_parent = db;
97 dbuf_add_ref(db, child);
98 if (db->db.db_data)
99 child->db_blkptr = (blkptr_t *)db->db.db_data + i;
100 else
101 child->db_blkptr = NULL;
102 dprintf_dbuf_bp(child, child->db_blkptr,
103 "changed db_blkptr to new indirect %s", "");
104
105 mutex_exit(&child->db_mtx);
106 }
107
108 bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr);
109
110 dbuf_rele(db, FTAG);
111
112 rw_exit(&dn->dn_struct_rwlock);
113}
114
b128c09f 115static int
34dc7c2f
BB
116free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx)
117{
b128c09f 118 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
34dc7c2f 119 uint64_t bytesfreed = 0;
b128c09f 120 int i, blocks_freed = 0;
34dc7c2f 121
b128c09f 122 dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num);
34dc7c2f
BB
123
124 for (i = 0; i < num; i++, bp++) {
125 if (BP_IS_HOLE(bp))
126 continue;
127
428870ff 128 bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE);
34dc7c2f 129 ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys));
34dc7c2f 130 bzero(bp, sizeof (blkptr_t));
b128c09f 131 blocks_freed += 1;
34dc7c2f
BB
132 }
133 dnode_diduse_space(dn, -bytesfreed);
b128c09f 134 return (blocks_freed);
34dc7c2f
BB
135}
136
137#ifdef ZFS_DEBUG
138static void
139free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx)
140{
141 int off, num;
142 int i, err, epbs;
143 uint64_t txg = tx->tx_txg;
572e2857 144 dnode_t *dn;
34dc7c2f 145
572e2857
BB
146 DB_DNODE_ENTER(db);
147 dn = DB_DNODE(db);
148 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
34dc7c2f
BB
149 off = start - (db->db_blkid * 1<<epbs);
150 num = end - start + 1;
151
152 ASSERT3U(off, >=, 0);
153 ASSERT3U(num, >=, 0);
154 ASSERT3U(db->db_level, >, 0);
572e2857 155 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
34dc7c2f
BB
156 ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT);
157 ASSERT(db->db_blkptr != NULL);
158
159 for (i = off; i < off+num; i++) {
160 uint64_t *buf;
161 dmu_buf_impl_t *child;
162 dbuf_dirty_record_t *dr;
163 int j;
164
165 ASSERT(db->db_level == 1);
166
572e2857
BB
167 rw_enter(&dn->dn_struct_rwlock, RW_READER);
168 err = dbuf_hold_impl(dn, db->db_level-1,
34dc7c2f 169 (db->db_blkid << epbs) + i, TRUE, FTAG, &child);
572e2857 170 rw_exit(&dn->dn_struct_rwlock);
34dc7c2f
BB
171 if (err == ENOENT)
172 continue;
173 ASSERT(err == 0);
174 ASSERT(child->db_level == 0);
175 dr = child->db_last_dirty;
176 while (dr && dr->dr_txg > txg)
177 dr = dr->dr_next;
178 ASSERT(dr == NULL || dr->dr_txg == txg);
179
180 /* data_old better be zeroed */
181 if (dr) {
182 buf = dr->dt.dl.dr_data->b_data;
183 for (j = 0; j < child->db.db_size >> 3; j++) {
184 if (buf[j] != 0) {
185 panic("freed data not zero: "
186 "child=%p i=%d off=%d num=%d\n",
b128c09f 187 (void *)child, i, off, num);
34dc7c2f
BB
188 }
189 }
190 }
191
192 /*
193 * db_data better be zeroed unless it's dirty in a
194 * future txg.
195 */
196 mutex_enter(&child->db_mtx);
197 buf = child->db.db_data;
198 if (buf != NULL && child->db_state != DB_FILL &&
199 child->db_last_dirty == NULL) {
200 for (j = 0; j < child->db.db_size >> 3; j++) {
201 if (buf[j] != 0) {
202 panic("freed data not zero: "
203 "child=%p i=%d off=%d num=%d\n",
b128c09f 204 (void *)child, i, off, num);
34dc7c2f
BB
205 }
206 }
207 }
208 mutex_exit(&child->db_mtx);
209
210 dbuf_rele(child, FTAG);
211 }
572e2857 212 DB_DNODE_EXIT(db);
34dc7c2f
BB
213}
214#endif
215
b128c09f
BB
216#define ALL -1
217
34dc7c2f
BB
218static int
219free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, int trunc,
220 dmu_tx_t *tx)
221{
572e2857 222 dnode_t *dn;
34dc7c2f
BB
223 blkptr_t *bp;
224 dmu_buf_impl_t *subdb;
225 uint64_t start, end, dbstart, dbend, i;
226 int epbs, shift, err;
227 int all = TRUE;
b128c09f
BB
228 int blocks_freed = 0;
229
230 /*
231 * There is a small possibility that this block will not be cached:
232 * 1 - if level > 1 and there are no children with level <= 1
233 * 2 - if we didn't get a dirty hold (because this block had just
234 * finished being written -- and so had no holds), and then this
235 * block got evicted before we got here.
236 */
237 if (db->db_state != DB_CACHED)
238 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
34dc7c2f 239
428870ff 240 dbuf_release_bp(db);
34dc7c2f
BB
241 bp = (blkptr_t *)db->db.db_data;
242
572e2857
BB
243 DB_DNODE_ENTER(db);
244 dn = DB_DNODE(db);
245 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
34dc7c2f
BB
246 shift = (db->db_level - 1) * epbs;
247 dbstart = db->db_blkid << epbs;
248 start = blkid >> shift;
249 if (dbstart < start) {
250 bp += start - dbstart;
251 all = FALSE;
252 } else {
253 start = dbstart;
254 }
255 dbend = ((db->db_blkid + 1) << epbs) - 1;
256 end = (blkid + nblks - 1) >> shift;
257 if (dbend <= end)
258 end = dbend;
259 else if (all)
260 all = trunc;
261 ASSERT3U(start, <=, end);
262
263 if (db->db_level == 1) {
264 FREE_VERIFY(db, start, end, tx);
b128c09f 265 blocks_freed = free_blocks(dn, bp, end-start+1, tx);
34dc7c2f 266 arc_buf_freeze(db->db_buf);
b128c09f 267 ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
572e2857 268 DB_DNODE_EXIT(db);
b128c09f 269 return (all ? ALL : blocks_freed);
34dc7c2f
BB
270 }
271
272 for (i = start; i <= end; i++, bp++) {
273 if (BP_IS_HOLE(bp))
274 continue;
275 rw_enter(&dn->dn_struct_rwlock, RW_READER);
276 err = dbuf_hold_impl(dn, db->db_level-1, i, TRUE, FTAG, &subdb);
c99c9001 277 ASSERT0(err);
34dc7c2f
BB
278 rw_exit(&dn->dn_struct_rwlock);
279
b128c09f 280 if (free_children(subdb, blkid, nblks, trunc, tx) == ALL) {
34dc7c2f 281 ASSERT3P(subdb->db_blkptr, ==, bp);
b128c09f 282 blocks_freed += free_blocks(dn, bp, 1, tx);
34dc7c2f
BB
283 } else {
284 all = FALSE;
285 }
286 dbuf_rele(subdb, FTAG);
287 }
572e2857 288 DB_DNODE_EXIT(db);
34dc7c2f
BB
289 arc_buf_freeze(db->db_buf);
290#ifdef ZFS_DEBUG
291 bp -= (end-start)+1;
292 for (i = start; i <= end; i++, bp++) {
293 if (i == start && blkid != 0)
294 continue;
295 else if (i == end && !trunc)
296 continue;
c99c9001 297 ASSERT0(bp->blk_birth);
34dc7c2f
BB
298 }
299#endif
b128c09f
BB
300 ASSERT(all || blocks_freed == 0 || db->db_last_dirty);
301 return (all ? ALL : blocks_freed);
34dc7c2f
BB
302}
303
304/*
d3cc8b15 305 * Traverse the indicated range of the provided file
34dc7c2f
BB
306 * and "free" all the blocks contained there.
307 */
308static void
309dnode_sync_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
310{
311 blkptr_t *bp = dn->dn_phys->dn_blkptr;
312 dmu_buf_impl_t *db;
313 int trunc, start, end, shift, i, err;
314 int dnlevel = dn->dn_phys->dn_nlevels;
315
316 if (blkid > dn->dn_phys->dn_maxblkid)
317 return;
318
319 ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX);
320 trunc = blkid + nblks > dn->dn_phys->dn_maxblkid;
321 if (trunc)
322 nblks = dn->dn_phys->dn_maxblkid - blkid + 1;
323
324 /* There are no indirect blocks in the object */
325 if (dnlevel == 1) {
326 if (blkid >= dn->dn_phys->dn_nblkptr) {
327 /* this range was never made persistent */
328 return;
329 }
330 ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr);
b128c09f 331 (void) free_blocks(dn, bp + blkid, nblks, tx);
34dc7c2f 332 if (trunc) {
1fde1e37
BB
333 ASSERTV(uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
334 (dn->dn_phys->dn_datablkszsec<<SPA_MINBLOCKSHIFT));
34dc7c2f
BB
335 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
336 ASSERT(off < dn->dn_phys->dn_maxblkid ||
337 dn->dn_phys->dn_maxblkid == 0 ||
b128c09f 338 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
34dc7c2f
BB
339 }
340 return;
341 }
342
343 shift = (dnlevel - 1) * (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT);
344 start = blkid >> shift;
345 ASSERT(start < dn->dn_phys->dn_nblkptr);
346 end = (blkid + nblks - 1) >> shift;
347 bp += start;
348 for (i = start; i <= end; i++, bp++) {
349 if (BP_IS_HOLE(bp))
350 continue;
351 rw_enter(&dn->dn_struct_rwlock, RW_READER);
352 err = dbuf_hold_impl(dn, dnlevel-1, i, TRUE, FTAG, &db);
c99c9001 353 ASSERT0(err);
34dc7c2f
BB
354 rw_exit(&dn->dn_struct_rwlock);
355
b128c09f 356 if (free_children(db, blkid, nblks, trunc, tx) == ALL) {
34dc7c2f 357 ASSERT3P(db->db_blkptr, ==, bp);
b128c09f 358 (void) free_blocks(dn, bp, 1, tx);
34dc7c2f
BB
359 }
360 dbuf_rele(db, FTAG);
361 }
362 if (trunc) {
1fde1e37
BB
363 ASSERTV(uint64_t off = (dn->dn_phys->dn_maxblkid + 1) *
364 (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT));
34dc7c2f
BB
365 dn->dn_phys->dn_maxblkid = (blkid ? blkid - 1 : 0);
366 ASSERT(off < dn->dn_phys->dn_maxblkid ||
367 dn->dn_phys->dn_maxblkid == 0 ||
b128c09f 368 dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0);
34dc7c2f
BB
369 }
370}
371
372/*
d3cc8b15 373 * Try to kick all the dnode's dbufs out of the cache...
34dc7c2f
BB
374 */
375void
376dnode_evict_dbufs(dnode_t *dn)
377{
378 int progress;
379 int pass = 0;
380
381 do {
382 dmu_buf_impl_t *db, marker;
383 int evicting = FALSE;
384
385 progress = FALSE;
386 mutex_enter(&dn->dn_dbufs_mtx);
387 list_insert_tail(&dn->dn_dbufs, &marker);
388 db = list_head(&dn->dn_dbufs);
389 for (; db != &marker; db = list_head(&dn->dn_dbufs)) {
390 list_remove(&dn->dn_dbufs, db);
391 list_insert_tail(&dn->dn_dbufs, db);
572e2857
BB
392#ifdef DEBUG
393 DB_DNODE_ENTER(db);
394 ASSERT3P(DB_DNODE(db), ==, dn);
395 DB_DNODE_EXIT(db);
396#endif /* DEBUG */
34dc7c2f
BB
397
398 mutex_enter(&db->db_mtx);
399 if (db->db_state == DB_EVICTING) {
400 progress = TRUE;
401 evicting = TRUE;
402 mutex_exit(&db->db_mtx);
403 } else if (refcount_is_zero(&db->db_holds)) {
404 progress = TRUE;
34dc7c2f
BB
405 dbuf_clear(db); /* exits db_mtx for us */
406 } else {
407 mutex_exit(&db->db_mtx);
408 }
409
410 }
411 list_remove(&dn->dn_dbufs, &marker);
412 /*
413 * NB: we need to drop dn_dbufs_mtx between passes so
414 * that any DB_EVICTING dbufs can make progress.
415 * Ideally, we would have some cv we could wait on, but
416 * since we don't, just wait a bit to give the other
417 * thread a chance to run.
418 */
419 mutex_exit(&dn->dn_dbufs_mtx);
420 if (evicting)
421 delay(1);
422 pass++;
754c6663
BB
423 if ((pass % 100) == 0)
424 dprintf("Exceeded %d passes evicting dbufs\n", pass);
34dc7c2f
BB
425 } while (progress);
426
754c6663
BB
427 if (pass >= 100)
428 dprintf("Required %d passes to evict dbufs\n", pass);
429
34dc7c2f
BB
430 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
431 if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) {
432 mutex_enter(&dn->dn_bonus->db_mtx);
433 dbuf_evict(dn->dn_bonus);
434 dn->dn_bonus = NULL;
435 }
436 rw_exit(&dn->dn_struct_rwlock);
437}
438
439static void
440dnode_undirty_dbufs(list_t *list)
441{
442 dbuf_dirty_record_t *dr;
443
c65aa5b2 444 while ((dr = list_head(list))) {
34dc7c2f
BB
445 dmu_buf_impl_t *db = dr->dr_dbuf;
446 uint64_t txg = dr->dr_txg;
447
428870ff
BB
448 if (db->db_level != 0)
449 dnode_undirty_dbufs(&dr->dt.di.dr_children);
450
34dc7c2f
BB
451 mutex_enter(&db->db_mtx);
452 /* XXX - use dbuf_undirty()? */
453 list_remove(list, dr);
454 ASSERT(db->db_last_dirty == dr);
455 db->db_last_dirty = NULL;
456 db->db_dirtycnt -= 1;
457 if (db->db_level == 0) {
428870ff 458 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
34dc7c2f
BB
459 dr->dt.dl.dr_data == db->db_buf);
460 dbuf_unoverride(dr);
34dc7c2f
BB
461 }
462 kmem_free(dr, sizeof (dbuf_dirty_record_t));
428870ff 463 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
34dc7c2f
BB
464 }
465}
466
467static void
468dnode_sync_free(dnode_t *dn, dmu_tx_t *tx)
469{
470 int txgoff = tx->tx_txg & TXG_MASK;
471
472 ASSERT(dmu_tx_is_syncing(tx));
473
b128c09f
BB
474 /*
475 * Our contents should have been freed in dnode_sync() by the
476 * free range record inserted by the caller of dnode_free().
477 */
c99c9001 478 ASSERT0(DN_USED_BYTES(dn->dn_phys));
b128c09f
BB
479 ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr));
480
34dc7c2f
BB
481 dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]);
482 dnode_evict_dbufs(dn);
483 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
13fe0198 484 ASSERT3P(dn->dn_bonus, ==, NULL);
34dc7c2f
BB
485
486 /*
487 * XXX - It would be nice to assert this, but we may still
488 * have residual holds from async evictions from the arc...
489 *
490 * zfs_obj_to_path() also depends on this being
491 * commented out.
492 *
493 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1);
494 */
495
496 /* Undirty next bits */
497 dn->dn_next_nlevels[txgoff] = 0;
498 dn->dn_next_indblkshift[txgoff] = 0;
499 dn->dn_next_blksz[txgoff] = 0;
500
34dc7c2f
BB
501 /* ASSERT(blkptrs are zero); */
502 ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE);
503 ASSERT(dn->dn_type != DMU_OT_NONE);
504
505 ASSERT(dn->dn_free_txg > 0);
506 if (dn->dn_allocated_txg != dn->dn_free_txg)
507 dbuf_will_dirty(dn->dn_dbuf, tx);
508 bzero(dn->dn_phys, sizeof (dnode_phys_t));
509
510 mutex_enter(&dn->dn_mtx);
511 dn->dn_type = DMU_OT_NONE;
512 dn->dn_maxblkid = 0;
513 dn->dn_allocated_txg = 0;
514 dn->dn_free_txg = 0;
428870ff 515 dn->dn_have_spill = B_FALSE;
34dc7c2f
BB
516 mutex_exit(&dn->dn_mtx);
517
518 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
519
520 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
521 /*
522 * Now that we've released our hold, the dnode may
523 * be evicted, so we musn't access it.
524 */
525}
526
527/*
528 * Write out the dnode's dirty buffers.
34dc7c2f
BB
529 */
530void
531dnode_sync(dnode_t *dn, dmu_tx_t *tx)
532{
533 free_range_t *rp;
534 dnode_phys_t *dnp = dn->dn_phys;
535 int txgoff = tx->tx_txg & TXG_MASK;
536 list_t *list = &dn->dn_dirty_records[txgoff];
428870ff 537 boolean_t kill_spill = B_FALSE;
1fde1e37 538 ASSERTV(static const dnode_phys_t zerodn = { 0 });
34dc7c2f
BB
539
540 ASSERT(dmu_tx_is_syncing(tx));
541 ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg);
9babb374
BB
542 ASSERT(dnp->dn_type != DMU_OT_NONE ||
543 bcmp(dnp, &zerodn, DNODE_SIZE) == 0);
34dc7c2f
BB
544 DNODE_VERIFY(dn);
545
546 ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf));
547
9babb374
BB
548 if (dmu_objset_userused_enabled(dn->dn_objset) &&
549 !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
428870ff
BB
550 mutex_enter(&dn->dn_mtx);
551 dn->dn_oldused = DN_USED_BYTES(dn->dn_phys);
552 dn->dn_oldflags = dn->dn_phys->dn_flags;
9babb374 553 dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED;
428870ff
BB
554 mutex_exit(&dn->dn_mtx);
555 dmu_objset_userquota_get_ids(dn, B_FALSE, tx);
9babb374
BB
556 } else {
557 /* Once we account for it, we should always account for it. */
558 ASSERT(!(dn->dn_phys->dn_flags &
559 DNODE_FLAG_USERUSED_ACCOUNTED));
560 }
561
34dc7c2f
BB
562 mutex_enter(&dn->dn_mtx);
563 if (dn->dn_allocated_txg == tx->tx_txg) {
564 /* The dnode is newly allocated or reallocated */
565 if (dnp->dn_type == DMU_OT_NONE) {
566 /* this is a first alloc, not a realloc */
34dc7c2f 567 dnp->dn_nlevels = 1;
d164b209 568 dnp->dn_nblkptr = dn->dn_nblkptr;
34dc7c2f
BB
569 }
570
34dc7c2f
BB
571 dnp->dn_type = dn->dn_type;
572 dnp->dn_bonustype = dn->dn_bonustype;
573 dnp->dn_bonuslen = dn->dn_bonuslen;
34dc7c2f
BB
574 }
575
576 ASSERT(dnp->dn_nlevels > 1 ||
577 BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
578 BP_GET_LSIZE(&dnp->dn_blkptr[0]) ==
579 dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
580
fa86b5db
MA
581 if (dn->dn_next_type[txgoff] != 0) {
582 dnp->dn_type = dn->dn_type;
583 dn->dn_next_type[txgoff] = 0;
584 }
585
586 if (dn->dn_next_blksz[txgoff] != 0) {
34dc7c2f
BB
587 ASSERT(P2PHASE(dn->dn_next_blksz[txgoff],
588 SPA_MINBLOCKSIZE) == 0);
589 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) ||
b128c09f 590 dn->dn_maxblkid == 0 || list_head(list) != NULL ||
428870ff 591 avl_last(&dn->dn_ranges[txgoff]) ||
34dc7c2f
BB
592 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT ==
593 dnp->dn_datablkszsec);
594 dnp->dn_datablkszsec =
595 dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT;
596 dn->dn_next_blksz[txgoff] = 0;
597 }
598
fa86b5db 599 if (dn->dn_next_bonuslen[txgoff] != 0) {
34dc7c2f
BB
600 if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN)
601 dnp->dn_bonuslen = 0;
602 else
603 dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff];
604 ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN);
605 dn->dn_next_bonuslen[txgoff] = 0;
606 }
607
fa86b5db 608 if (dn->dn_next_bonustype[txgoff] != 0) {
9ae529ec 609 ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff]));
428870ff
BB
610 dnp->dn_bonustype = dn->dn_next_bonustype[txgoff];
611 dn->dn_next_bonustype[txgoff] = 0;
612 }
613
614 /*
615 * We will either remove a spill block when a file is being removed
616 * or we have been asked to remove it.
617 */
618 if (dn->dn_rm_spillblk[txgoff] ||
619 ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) &&
620 dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg)) {
621 if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
622 kill_spill = B_TRUE;
623 dn->dn_rm_spillblk[txgoff] = 0;
624 }
625
fa86b5db 626 if (dn->dn_next_indblkshift[txgoff] != 0) {
34dc7c2f
BB
627 ASSERT(dnp->dn_nlevels == 1);
628 dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff];
629 dn->dn_next_indblkshift[txgoff] = 0;
630 }
631
632 /*
633 * Just take the live (open-context) values for checksum and compress.
634 * Strictly speaking it's a future leak, but nothing bad happens if we
635 * start using the new checksum or compress algorithm a little early.
636 */
637 dnp->dn_checksum = dn->dn_checksum;
638 dnp->dn_compress = dn->dn_compress;
639
640 mutex_exit(&dn->dn_mtx);
641
428870ff
BB
642 if (kill_spill) {
643 (void) free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx);
644 mutex_enter(&dn->dn_mtx);
645 dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR;
646 mutex_exit(&dn->dn_mtx);
647 }
648
34dc7c2f 649 /* process all the "freed" ranges in the file */
c65aa5b2 650 while ((rp = avl_last(&dn->dn_ranges[txgoff]))) {
b128c09f
BB
651 dnode_sync_free_range(dn, rp->fr_blkid, rp->fr_nblks, tx);
652 /* grab the mutex so we don't race with dnode_block_freed() */
653 mutex_enter(&dn->dn_mtx);
654 avl_remove(&dn->dn_ranges[txgoff], rp);
655 mutex_exit(&dn->dn_mtx);
656 kmem_free(rp, sizeof (free_range_t));
34dc7c2f 657 }
34dc7c2f 658
34dc7c2f
BB
659 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg) {
660 dnode_sync_free(dn, tx);
661 return;
662 }
663
d164b209
BB
664 if (dn->dn_next_nblkptr[txgoff]) {
665 /* this should only happen on a realloc */
666 ASSERT(dn->dn_allocated_txg == tx->tx_txg);
667 if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) {
668 /* zero the new blkptrs we are gaining */
669 bzero(dnp->dn_blkptr + dnp->dn_nblkptr,
670 sizeof (blkptr_t) *
671 (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr));
672#ifdef ZFS_DEBUG
673 } else {
674 int i;
675 ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr);
676 /* the blkptrs we are losing better be unallocated */
37f000c5
NB
677 for (i = 0; i < dnp->dn_nblkptr; i++) {
678 if (i >= dn->dn_next_nblkptr[txgoff])
679 ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i]));
680 }
d164b209
BB
681#endif
682 }
683 mutex_enter(&dn->dn_mtx);
684 dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff];
685 dn->dn_next_nblkptr[txgoff] = 0;
686 mutex_exit(&dn->dn_mtx);
687 }
688
34dc7c2f
BB
689 if (dn->dn_next_nlevels[txgoff]) {
690 dnode_increase_indirection(dn, tx);
691 dn->dn_next_nlevels[txgoff] = 0;
692 }
693
694 dbuf_sync_list(list, tx);
695
9babb374 696 if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
34dc7c2f
BB
697 ASSERT3P(list_head(list), ==, NULL);
698 dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg);
699 }
700
701 /*
702 * Although we have dropped our reference to the dnode, it
703 * can't be evicted until its written, and we haven't yet
704 * initiated the IO for the dnode's dbuf.
705 */
706}