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