]>
Commit | Line | Data |
---|---|---|
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. |
cae5b340 | 24 | * Copyright (c) 2012, 2017 by Delphix. All rights reserved. |
e10b0808 | 25 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. |
34dc7c2f BB |
26 | */ |
27 | ||
34dc7c2f BB |
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_tx.h> | |
33 | #include <sys/dmu_objset.h> | |
34 | #include <sys/dsl_dataset.h> | |
35 | #include <sys/spa.h> | |
ea04106b AX |
36 | #include <sys/range_tree.h> |
37 | #include <sys/zfeature.h> | |
34dc7c2f BB |
38 | |
39 | static void | |
40 | dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) | |
41 | { | |
42 | dmu_buf_impl_t *db; | |
43 | int txgoff = tx->tx_txg & TXG_MASK; | |
44 | int nblkptr = dn->dn_phys->dn_nblkptr; | |
45 | int old_toplvl = dn->dn_phys->dn_nlevels - 1; | |
46 | int new_level = dn->dn_next_nlevels[txgoff]; | |
47 | int i; | |
48 | ||
49 | rw_enter(&dn->dn_struct_rwlock, RW_WRITER); | |
50 | ||
51 | /* this dnode can't be paged out because it's dirty */ | |
52 | ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); | |
53 | ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); | |
54 | ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0); | |
55 | ||
56 | db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); | |
57 | ASSERT(db != NULL); | |
58 | ||
59 | dn->dn_phys->dn_nlevels = new_level; | |
60 | dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset, | |
61 | dn->dn_object, dn->dn_phys->dn_nlevels); | |
62 | ||
cae5b340 AX |
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); | |
34dc7c2f BB |
71 | |
72 | /* set dbuf's parent pointers to new indirect buf */ | |
73 | for (i = 0; i < nblkptr; i++) { | |
e10b0808 AX |
74 | dmu_buf_impl_t *child = |
75 | dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i); | |
34dc7c2f BB |
76 | |
77 | if (child == NULL) | |
78 | continue; | |
572e2857 BB |
79 | #ifdef DEBUG |
80 | DB_DNODE_ENTER(child); | |
81 | ASSERT3P(DB_DNODE(child), ==, dn); | |
82 | DB_DNODE_EXIT(child); | |
83 | #endif /* DEBUG */ | |
34dc7c2f BB |
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 | ||
ea04106b | 113 | static void |
34dc7c2f BB |
114 | free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) |
115 | { | |
b128c09f | 116 | dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; |
34dc7c2f | 117 | uint64_t bytesfreed = 0; |
ea04106b | 118 | int i; |
34dc7c2f | 119 | |
b128c09f | 120 | dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num); |
34dc7c2f BB |
121 | |
122 | for (i = 0; i < num; i++, bp++) { | |
ea04106b AX |
123 | uint64_t lsize, lvl; |
124 | dmu_object_type_t type; | |
125 | ||
34dc7c2f BB |
126 | if (BP_IS_HOLE(bp)) |
127 | continue; | |
128 | ||
428870ff | 129 | bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE); |
34dc7c2f | 130 | ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); |
ea04106b AX |
131 | |
132 | /* | |
133 | * Save some useful information on the holes being | |
134 | * punched, including logical size, type, and indirection | |
135 | * level. Retaining birth time enables detection of when | |
136 | * holes are punched for reducing the number of free | |
137 | * records transmitted during a zfs send. | |
138 | */ | |
139 | ||
140 | lsize = BP_GET_LSIZE(bp); | |
141 | type = BP_GET_TYPE(bp); | |
142 | lvl = BP_GET_LEVEL(bp); | |
143 | ||
34dc7c2f | 144 | bzero(bp, sizeof (blkptr_t)); |
ea04106b AX |
145 | |
146 | if (spa_feature_is_active(dn->dn_objset->os_spa, | |
147 | SPA_FEATURE_HOLE_BIRTH)) { | |
148 | BP_SET_LSIZE(bp, lsize); | |
149 | BP_SET_TYPE(bp, type); | |
150 | BP_SET_LEVEL(bp, lvl); | |
151 | BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0); | |
152 | } | |
34dc7c2f BB |
153 | } |
154 | dnode_diduse_space(dn, -bytesfreed); | |
155 | } | |
156 | ||
157 | #ifdef ZFS_DEBUG | |
158 | static void | |
159 | free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) | |
160 | { | |
161 | int off, num; | |
162 | int i, err, epbs; | |
163 | uint64_t txg = tx->tx_txg; | |
572e2857 | 164 | dnode_t *dn; |
34dc7c2f | 165 | |
572e2857 BB |
166 | DB_DNODE_ENTER(db); |
167 | dn = DB_DNODE(db); | |
168 | epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; | |
34dc7c2f BB |
169 | off = start - (db->db_blkid * 1<<epbs); |
170 | num = end - start + 1; | |
171 | ||
172 | ASSERT3U(off, >=, 0); | |
173 | ASSERT3U(num, >=, 0); | |
174 | ASSERT3U(db->db_level, >, 0); | |
572e2857 | 175 | ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); |
34dc7c2f BB |
176 | ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); |
177 | ASSERT(db->db_blkptr != NULL); | |
178 | ||
179 | for (i = off; i < off+num; i++) { | |
180 | uint64_t *buf; | |
181 | dmu_buf_impl_t *child; | |
182 | dbuf_dirty_record_t *dr; | |
183 | int j; | |
184 | ||
185 | ASSERT(db->db_level == 1); | |
186 | ||
572e2857 BB |
187 | rw_enter(&dn->dn_struct_rwlock, RW_READER); |
188 | err = dbuf_hold_impl(dn, db->db_level-1, | |
cae5b340 | 189 | (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child); |
572e2857 | 190 | rw_exit(&dn->dn_struct_rwlock); |
34dc7c2f BB |
191 | if (err == ENOENT) |
192 | continue; | |
193 | ASSERT(err == 0); | |
194 | ASSERT(child->db_level == 0); | |
195 | dr = child->db_last_dirty; | |
196 | while (dr && dr->dr_txg > txg) | |
197 | dr = dr->dr_next; | |
198 | ASSERT(dr == NULL || dr->dr_txg == txg); | |
199 | ||
200 | /* data_old better be zeroed */ | |
201 | if (dr) { | |
202 | buf = dr->dt.dl.dr_data->b_data; | |
203 | for (j = 0; j < child->db.db_size >> 3; j++) { | |
204 | if (buf[j] != 0) { | |
205 | panic("freed data not zero: " | |
206 | "child=%p i=%d off=%d num=%d\n", | |
b128c09f | 207 | (void *)child, i, off, num); |
34dc7c2f BB |
208 | } |
209 | } | |
210 | } | |
211 | ||
212 | /* | |
213 | * db_data better be zeroed unless it's dirty in a | |
214 | * future txg. | |
215 | */ | |
216 | mutex_enter(&child->db_mtx); | |
217 | buf = child->db.db_data; | |
218 | if (buf != NULL && child->db_state != DB_FILL && | |
219 | child->db_last_dirty == NULL) { | |
220 | for (j = 0; j < child->db.db_size >> 3; j++) { | |
221 | if (buf[j] != 0) { | |
222 | panic("freed data not zero: " | |
223 | "child=%p i=%d off=%d num=%d\n", | |
b128c09f | 224 | (void *)child, i, off, num); |
34dc7c2f BB |
225 | } |
226 | } | |
227 | } | |
228 | mutex_exit(&child->db_mtx); | |
229 | ||
230 | dbuf_rele(child, FTAG); | |
231 | } | |
572e2857 | 232 | DB_DNODE_EXIT(db); |
34dc7c2f BB |
233 | } |
234 | #endif | |
235 | ||
ea04106b AX |
236 | static void |
237 | free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, | |
34dc7c2f BB |
238 | dmu_tx_t *tx) |
239 | { | |
572e2857 | 240 | dnode_t *dn; |
34dc7c2f BB |
241 | blkptr_t *bp; |
242 | dmu_buf_impl_t *subdb; | |
cae5b340 AX |
243 | uint64_t start, end, dbstart, dbend; |
244 | unsigned int epbs, shift, i; | |
245 | uint64_t id; | |
b128c09f BB |
246 | |
247 | /* | |
248 | * There is a small possibility that this block will not be cached: | |
249 | * 1 - if level > 1 and there are no children with level <= 1 | |
ea04106b AX |
250 | * 2 - if this block was evicted since we read it from |
251 | * dmu_tx_hold_free(). | |
b128c09f BB |
252 | */ |
253 | if (db->db_state != DB_CACHED) | |
254 | (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); | |
34dc7c2f | 255 | |
428870ff | 256 | dbuf_release_bp(db); |
ea04106b | 257 | bp = db->db.db_data; |
34dc7c2f | 258 | |
572e2857 BB |
259 | DB_DNODE_ENTER(db); |
260 | dn = DB_DNODE(db); | |
261 | epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; | |
cae5b340 | 262 | ASSERT3U(epbs, <, 31); |
34dc7c2f BB |
263 | shift = (db->db_level - 1) * epbs; |
264 | dbstart = db->db_blkid << epbs; | |
265 | start = blkid >> shift; | |
266 | if (dbstart < start) { | |
267 | bp += start - dbstart; | |
34dc7c2f BB |
268 | } else { |
269 | start = dbstart; | |
270 | } | |
271 | dbend = ((db->db_blkid + 1) << epbs) - 1; | |
272 | end = (blkid + nblks - 1) >> shift; | |
273 | if (dbend <= end) | |
274 | end = dbend; | |
ea04106b | 275 | |
34dc7c2f BB |
276 | ASSERT3U(start, <=, end); |
277 | ||
278 | if (db->db_level == 1) { | |
279 | FREE_VERIFY(db, start, end, tx); | |
ea04106b AX |
280 | free_blocks(dn, bp, end-start+1, tx); |
281 | } else { | |
cae5b340 | 282 | for (id = start; id <= end; id++, bp++) { |
ea04106b AX |
283 | if (BP_IS_HOLE(bp)) |
284 | continue; | |
285 | rw_enter(&dn->dn_struct_rwlock, RW_READER); | |
286 | VERIFY0(dbuf_hold_impl(dn, db->db_level - 1, | |
cae5b340 | 287 | id, TRUE, FALSE, FTAG, &subdb)); |
ea04106b AX |
288 | rw_exit(&dn->dn_struct_rwlock); |
289 | ASSERT3P(bp, ==, subdb->db_blkptr); | |
290 | ||
291 | free_children(subdb, blkid, nblks, tx); | |
292 | dbuf_rele(subdb, FTAG); | |
293 | } | |
34dc7c2f BB |
294 | } |
295 | ||
ea04106b | 296 | /* If this whole block is free, free ourself too. */ |
cae5b340 | 297 | for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) { |
ea04106b AX |
298 | if (!BP_IS_HOLE(bp)) |
299 | break; | |
300 | } | |
301 | if (i == 1 << epbs) { | |
cae5b340 AX |
302 | /* |
303 | * We only found holes. Grab the rwlock to prevent | |
304 | * anybody from reading the blocks we're about to | |
305 | * zero out. | |
306 | */ | |
307 | rw_enter(&dn->dn_struct_rwlock, RW_WRITER); | |
ea04106b | 308 | bzero(db->db.db_data, db->db.db_size); |
cae5b340 | 309 | rw_exit(&dn->dn_struct_rwlock); |
ea04106b AX |
310 | free_blocks(dn, db->db_blkptr, 1, tx); |
311 | } else { | |
312 | /* | |
313 | * Partial block free; must be marked dirty so that it | |
314 | * will be written out. | |
315 | */ | |
316 | ASSERT(db->db_dirtycnt > 0); | |
34dc7c2f | 317 | } |
ea04106b | 318 | |
572e2857 | 319 | DB_DNODE_EXIT(db); |
34dc7c2f | 320 | arc_buf_freeze(db->db_buf); |
34dc7c2f BB |
321 | } |
322 | ||
323 | /* | |
a08ee875 | 324 | * Traverse the indicated range of the provided file |
34dc7c2f BB |
325 | * and "free" all the blocks contained there. |
326 | */ | |
327 | static void | |
ea04106b AX |
328 | dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks, |
329 | dmu_tx_t *tx) | |
34dc7c2f BB |
330 | { |
331 | blkptr_t *bp = dn->dn_phys->dn_blkptr; | |
34dc7c2f | 332 | int dnlevel = dn->dn_phys->dn_nlevels; |
ea04106b | 333 | boolean_t trunc = B_FALSE; |
34dc7c2f BB |
334 | |
335 | if (blkid > dn->dn_phys->dn_maxblkid) | |
336 | return; | |
337 | ||
338 | ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); | |
ea04106b | 339 | if (blkid + nblks > dn->dn_phys->dn_maxblkid) { |
34dc7c2f | 340 | nblks = dn->dn_phys->dn_maxblkid - blkid + 1; |
ea04106b AX |
341 | trunc = B_TRUE; |
342 | } | |
34dc7c2f BB |
343 | |
344 | /* There are no indirect blocks in the object */ | |
345 | if (dnlevel == 1) { | |
346 | if (blkid >= dn->dn_phys->dn_nblkptr) { | |
347 | /* this range was never made persistent */ | |
348 | return; | |
349 | } | |
350 | ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); | |
ea04106b AX |
351 | free_blocks(dn, bp + blkid, nblks, tx); |
352 | } else { | |
353 | int shift = (dnlevel - 1) * | |
354 | (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); | |
355 | int start = blkid >> shift; | |
356 | int end = (blkid + nblks - 1) >> shift; | |
357 | dmu_buf_impl_t *db; | |
358 | int i; | |
359 | ||
360 | ASSERT(start < dn->dn_phys->dn_nblkptr); | |
361 | bp += start; | |
362 | for (i = start; i <= end; i++, bp++) { | |
363 | if (BP_IS_HOLE(bp)) | |
364 | continue; | |
365 | rw_enter(&dn->dn_struct_rwlock, RW_READER); | |
366 | VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i, | |
cae5b340 | 367 | TRUE, FALSE, FTAG, &db)); |
ea04106b AX |
368 | rw_exit(&dn->dn_struct_rwlock); |
369 | ||
370 | free_children(db, blkid, nblks, tx); | |
371 | dbuf_rele(db, FTAG); | |
34dc7c2f | 372 | } |
34dc7c2f BB |
373 | } |
374 | ||
34dc7c2f | 375 | if (trunc) { |
ea04106b AX |
376 | ASSERTV(uint64_t off); |
377 | dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1; | |
378 | ||
379 | ASSERTV(off = (dn->dn_phys->dn_maxblkid + 1) * | |
1fde1e37 | 380 | (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT)); |
34dc7c2f BB |
381 | ASSERT(off < dn->dn_phys->dn_maxblkid || |
382 | dn->dn_phys->dn_maxblkid == 0 || | |
b128c09f | 383 | dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0); |
34dc7c2f BB |
384 | } |
385 | } | |
386 | ||
ea04106b AX |
387 | typedef struct dnode_sync_free_range_arg { |
388 | dnode_t *dsfra_dnode; | |
389 | dmu_tx_t *dsfra_tx; | |
390 | } dnode_sync_free_range_arg_t; | |
391 | ||
392 | static void | |
393 | dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks) | |
394 | { | |
395 | dnode_sync_free_range_arg_t *dsfra = arg; | |
396 | dnode_t *dn = dsfra->dsfra_dnode; | |
397 | ||
398 | mutex_exit(&dn->dn_mtx); | |
399 | dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx); | |
400 | mutex_enter(&dn->dn_mtx); | |
401 | } | |
402 | ||
34dc7c2f | 403 | /* |
a08ee875 | 404 | * Try to kick all the dnode's dbufs out of the cache... |
34dc7c2f BB |
405 | */ |
406 | void | |
407 | dnode_evict_dbufs(dnode_t *dn) | |
408 | { | |
e10b0808 AX |
409 | dmu_buf_impl_t *db_marker; |
410 | dmu_buf_impl_t *db, *db_next; | |
411 | ||
412 | db_marker = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP); | |
413 | ||
414 | mutex_enter(&dn->dn_dbufs_mtx); | |
415 | for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) { | |
416 | ||
572e2857 | 417 | #ifdef DEBUG |
e10b0808 AX |
418 | DB_DNODE_ENTER(db); |
419 | ASSERT3P(DB_DNODE(db), ==, dn); | |
420 | DB_DNODE_EXIT(db); | |
572e2857 | 421 | #endif /* DEBUG */ |
34dc7c2f | 422 | |
e10b0808 AX |
423 | mutex_enter(&db->db_mtx); |
424 | if (db->db_state != DB_EVICTING && | |
425 | refcount_is_zero(&db->db_holds)) { | |
426 | db_marker->db_level = db->db_level; | |
427 | db_marker->db_blkid = db->db_blkid; | |
428 | db_marker->db_state = DB_SEARCH; | |
429 | avl_insert_here(&dn->dn_dbufs, db_marker, db, | |
430 | AVL_BEFORE); | |
431 | ||
cae5b340 | 432 | dbuf_destroy(db); |
e10b0808 AX |
433 | |
434 | db_next = AVL_NEXT(&dn->dn_dbufs, db_marker); | |
435 | avl_remove(&dn->dn_dbufs, db_marker); | |
436 | } else { | |
437 | db->db_pending_evict = TRUE; | |
438 | mutex_exit(&db->db_mtx); | |
439 | db_next = AVL_NEXT(&dn->dn_dbufs, db); | |
34dc7c2f | 440 | } |
e10b0808 AX |
441 | } |
442 | mutex_exit(&dn->dn_dbufs_mtx); | |
34dc7c2f | 443 | |
e10b0808 | 444 | kmem_free(db_marker, sizeof (dmu_buf_impl_t)); |
754c6663 | 445 | |
ea04106b AX |
446 | dnode_evict_bonus(dn); |
447 | } | |
448 | ||
449 | void | |
450 | dnode_evict_bonus(dnode_t *dn) | |
451 | { | |
34dc7c2f | 452 | rw_enter(&dn->dn_struct_rwlock, RW_WRITER); |
e10b0808 AX |
453 | if (dn->dn_bonus != NULL) { |
454 | if (refcount_is_zero(&dn->dn_bonus->db_holds)) { | |
455 | mutex_enter(&dn->dn_bonus->db_mtx); | |
cae5b340 | 456 | dbuf_destroy(dn->dn_bonus); |
e10b0808 AX |
457 | dn->dn_bonus = NULL; |
458 | } else { | |
459 | dn->dn_bonus->db_pending_evict = TRUE; | |
460 | } | |
34dc7c2f BB |
461 | } |
462 | rw_exit(&dn->dn_struct_rwlock); | |
463 | } | |
464 | ||
465 | static void | |
466 | dnode_undirty_dbufs(list_t *list) | |
467 | { | |
468 | dbuf_dirty_record_t *dr; | |
469 | ||
c65aa5b2 | 470 | while ((dr = list_head(list))) { |
34dc7c2f BB |
471 | dmu_buf_impl_t *db = dr->dr_dbuf; |
472 | uint64_t txg = dr->dr_txg; | |
473 | ||
428870ff BB |
474 | if (db->db_level != 0) |
475 | dnode_undirty_dbufs(&dr->dt.di.dr_children); | |
476 | ||
34dc7c2f BB |
477 | mutex_enter(&db->db_mtx); |
478 | /* XXX - use dbuf_undirty()? */ | |
479 | list_remove(list, dr); | |
480 | ASSERT(db->db_last_dirty == dr); | |
481 | db->db_last_dirty = NULL; | |
482 | db->db_dirtycnt -= 1; | |
483 | if (db->db_level == 0) { | |
428870ff | 484 | ASSERT(db->db_blkid == DMU_BONUS_BLKID || |
34dc7c2f BB |
485 | dr->dt.dl.dr_data == db->db_buf); |
486 | dbuf_unoverride(dr); | |
e10b0808 AX |
487 | } else { |
488 | mutex_destroy(&dr->dt.di.dr_mtx); | |
489 | list_destroy(&dr->dt.di.dr_children); | |
34dc7c2f BB |
490 | } |
491 | kmem_free(dr, sizeof (dbuf_dirty_record_t)); | |
428870ff | 492 | dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg); |
34dc7c2f BB |
493 | } |
494 | } | |
495 | ||
496 | static void | |
497 | dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) | |
498 | { | |
499 | int txgoff = tx->tx_txg & TXG_MASK; | |
500 | ||
501 | ASSERT(dmu_tx_is_syncing(tx)); | |
502 | ||
b128c09f BB |
503 | /* |
504 | * Our contents should have been freed in dnode_sync() by the | |
505 | * free range record inserted by the caller of dnode_free(). | |
506 | */ | |
c06d4368 | 507 | ASSERT0(DN_USED_BYTES(dn->dn_phys)); |
b128c09f BB |
508 | ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr)); |
509 | ||
34dc7c2f BB |
510 | dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); |
511 | dnode_evict_dbufs(dn); | |
34dc7c2f BB |
512 | |
513 | /* | |
514 | * XXX - It would be nice to assert this, but we may still | |
515 | * have residual holds from async evictions from the arc... | |
516 | * | |
517 | * zfs_obj_to_path() also depends on this being | |
518 | * commented out. | |
519 | * | |
520 | * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); | |
521 | */ | |
522 | ||
523 | /* Undirty next bits */ | |
524 | dn->dn_next_nlevels[txgoff] = 0; | |
525 | dn->dn_next_indblkshift[txgoff] = 0; | |
526 | dn->dn_next_blksz[txgoff] = 0; | |
527 | ||
34dc7c2f BB |
528 | /* ASSERT(blkptrs are zero); */ |
529 | ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); | |
530 | ASSERT(dn->dn_type != DMU_OT_NONE); | |
531 | ||
532 | ASSERT(dn->dn_free_txg > 0); | |
533 | if (dn->dn_allocated_txg != dn->dn_free_txg) | |
ea04106b | 534 | dmu_buf_will_dirty(&dn->dn_dbuf->db, tx); |
cae5b340 | 535 | bzero(dn->dn_phys, sizeof (dnode_phys_t) * dn->dn_num_slots); |
a07c8b41 | 536 | dnode_free_interior_slots(dn); |
34dc7c2f BB |
537 | |
538 | mutex_enter(&dn->dn_mtx); | |
539 | dn->dn_type = DMU_OT_NONE; | |
540 | dn->dn_maxblkid = 0; | |
541 | dn->dn_allocated_txg = 0; | |
542 | dn->dn_free_txg = 0; | |
428870ff | 543 | dn->dn_have_spill = B_FALSE; |
a07c8b41 | 544 | dn->dn_num_slots = 1; |
34dc7c2f BB |
545 | mutex_exit(&dn->dn_mtx); |
546 | ||
547 | ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); | |
548 | ||
549 | dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); | |
550 | /* | |
551 | * Now that we've released our hold, the dnode may | |
cae5b340 | 552 | * be evicted, so we mustn't access it. |
34dc7c2f BB |
553 | */ |
554 | } | |
555 | ||
556 | /* | |
557 | * Write out the dnode's dirty buffers. | |
34dc7c2f BB |
558 | */ |
559 | void | |
560 | dnode_sync(dnode_t *dn, dmu_tx_t *tx) | |
561 | { | |
34dc7c2f BB |
562 | dnode_phys_t *dnp = dn->dn_phys; |
563 | int txgoff = tx->tx_txg & TXG_MASK; | |
564 | list_t *list = &dn->dn_dirty_records[txgoff]; | |
428870ff | 565 | boolean_t kill_spill = B_FALSE; |
ea04106b | 566 | boolean_t freeing_dnode; |
1fde1e37 | 567 | ASSERTV(static const dnode_phys_t zerodn = { 0 }); |
34dc7c2f BB |
568 | |
569 | ASSERT(dmu_tx_is_syncing(tx)); | |
570 | ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); | |
9babb374 | 571 | ASSERT(dnp->dn_type != DMU_OT_NONE || |
cae5b340 | 572 | bcmp(dnp, &zerodn, DNODE_MIN_SIZE) == 0); |
34dc7c2f BB |
573 | DNODE_VERIFY(dn); |
574 | ||
575 | ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); | |
576 | ||
9babb374 BB |
577 | if (dmu_objset_userused_enabled(dn->dn_objset) && |
578 | !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { | |
428870ff BB |
579 | mutex_enter(&dn->dn_mtx); |
580 | dn->dn_oldused = DN_USED_BYTES(dn->dn_phys); | |
581 | dn->dn_oldflags = dn->dn_phys->dn_flags; | |
9babb374 | 582 | dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED; |
cae5b340 AX |
583 | if (dmu_objset_userobjused_enabled(dn->dn_objset)) |
584 | dn->dn_phys->dn_flags |= | |
585 | DNODE_FLAG_USEROBJUSED_ACCOUNTED; | |
428870ff BB |
586 | mutex_exit(&dn->dn_mtx); |
587 | dmu_objset_userquota_get_ids(dn, B_FALSE, tx); | |
9babb374 BB |
588 | } else { |
589 | /* Once we account for it, we should always account for it. */ | |
590 | ASSERT(!(dn->dn_phys->dn_flags & | |
591 | DNODE_FLAG_USERUSED_ACCOUNTED)); | |
cae5b340 AX |
592 | ASSERT(!(dn->dn_phys->dn_flags & |
593 | DNODE_FLAG_USEROBJUSED_ACCOUNTED)); | |
9babb374 BB |
594 | } |
595 | ||
34dc7c2f BB |
596 | mutex_enter(&dn->dn_mtx); |
597 | if (dn->dn_allocated_txg == tx->tx_txg) { | |
598 | /* The dnode is newly allocated or reallocated */ | |
599 | if (dnp->dn_type == DMU_OT_NONE) { | |
600 | /* this is a first alloc, not a realloc */ | |
34dc7c2f | 601 | dnp->dn_nlevels = 1; |
d164b209 | 602 | dnp->dn_nblkptr = dn->dn_nblkptr; |
34dc7c2f BB |
603 | } |
604 | ||
34dc7c2f BB |
605 | dnp->dn_type = dn->dn_type; |
606 | dnp->dn_bonustype = dn->dn_bonustype; | |
607 | dnp->dn_bonuslen = dn->dn_bonuslen; | |
34dc7c2f | 608 | } |
cae5b340 AX |
609 | |
610 | dnp->dn_extra_slots = dn->dn_num_slots - 1; | |
611 | ||
34dc7c2f BB |
612 | ASSERT(dnp->dn_nlevels > 1 || |
613 | BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
ea04106b | 614 | BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) || |
34dc7c2f BB |
615 | BP_GET_LSIZE(&dnp->dn_blkptr[0]) == |
616 | dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); | |
ea04106b AX |
617 | ASSERT(dnp->dn_nlevels < 2 || |
618 | BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
619 | BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift); | |
34dc7c2f | 620 | |
ea04106b AX |
621 | if (dn->dn_next_type[txgoff] != 0) { |
622 | dnp->dn_type = dn->dn_type; | |
623 | dn->dn_next_type[txgoff] = 0; | |
624 | } | |
625 | ||
626 | if (dn->dn_next_blksz[txgoff] != 0) { | |
34dc7c2f BB |
627 | ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], |
628 | SPA_MINBLOCKSIZE) == 0); | |
629 | ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
b128c09f | 630 | dn->dn_maxblkid == 0 || list_head(list) != NULL || |
34dc7c2f | 631 | dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == |
ea04106b AX |
632 | dnp->dn_datablkszsec || |
633 | range_tree_space(dn->dn_free_ranges[txgoff]) != 0); | |
34dc7c2f BB |
634 | dnp->dn_datablkszsec = |
635 | dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; | |
636 | dn->dn_next_blksz[txgoff] = 0; | |
637 | } | |
638 | ||
ea04106b | 639 | if (dn->dn_next_bonuslen[txgoff] != 0) { |
34dc7c2f BB |
640 | if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) |
641 | dnp->dn_bonuslen = 0; | |
642 | else | |
643 | dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; | |
cae5b340 AX |
644 | ASSERT(dnp->dn_bonuslen <= |
645 | DN_SLOTS_TO_BONUSLEN(dnp->dn_extra_slots + 1)); | |
34dc7c2f BB |
646 | dn->dn_next_bonuslen[txgoff] = 0; |
647 | } | |
648 | ||
ea04106b | 649 | if (dn->dn_next_bonustype[txgoff] != 0) { |
9ae529ec | 650 | ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff])); |
428870ff BB |
651 | dnp->dn_bonustype = dn->dn_next_bonustype[txgoff]; |
652 | dn->dn_next_bonustype[txgoff] = 0; | |
653 | } | |
654 | ||
ea04106b AX |
655 | freeing_dnode = dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg; |
656 | ||
428870ff | 657 | /* |
e10b0808 AX |
658 | * Remove the spill block if we have been explicitly asked to |
659 | * remove it, or if the object is being removed. | |
428870ff | 660 | */ |
e10b0808 AX |
661 | if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) { |
662 | if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) | |
428870ff BB |
663 | kill_spill = B_TRUE; |
664 | dn->dn_rm_spillblk[txgoff] = 0; | |
665 | } | |
666 | ||
ea04106b | 667 | if (dn->dn_next_indblkshift[txgoff] != 0) { |
34dc7c2f BB |
668 | ASSERT(dnp->dn_nlevels == 1); |
669 | dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; | |
670 | dn->dn_next_indblkshift[txgoff] = 0; | |
671 | } | |
672 | ||
673 | /* | |
674 | * Just take the live (open-context) values for checksum and compress. | |
675 | * Strictly speaking it's a future leak, but nothing bad happens if we | |
676 | * start using the new checksum or compress algorithm a little early. | |
677 | */ | |
678 | dnp->dn_checksum = dn->dn_checksum; | |
679 | dnp->dn_compress = dn->dn_compress; | |
680 | ||
681 | mutex_exit(&dn->dn_mtx); | |
682 | ||
428870ff | 683 | if (kill_spill) { |
cae5b340 | 684 | free_blocks(dn, DN_SPILL_BLKPTR(dn->dn_phys), 1, tx); |
428870ff BB |
685 | mutex_enter(&dn->dn_mtx); |
686 | dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR; | |
687 | mutex_exit(&dn->dn_mtx); | |
688 | } | |
689 | ||
34dc7c2f | 690 | /* process all the "freed" ranges in the file */ |
ea04106b AX |
691 | if (dn->dn_free_ranges[txgoff] != NULL) { |
692 | dnode_sync_free_range_arg_t dsfra; | |
693 | dsfra.dsfra_dnode = dn; | |
694 | dsfra.dsfra_tx = tx; | |
b128c09f | 695 | mutex_enter(&dn->dn_mtx); |
ea04106b AX |
696 | range_tree_vacate(dn->dn_free_ranges[txgoff], |
697 | dnode_sync_free_range, &dsfra); | |
698 | range_tree_destroy(dn->dn_free_ranges[txgoff]); | |
699 | dn->dn_free_ranges[txgoff] = NULL; | |
b128c09f | 700 | mutex_exit(&dn->dn_mtx); |
34dc7c2f | 701 | } |
34dc7c2f | 702 | |
ea04106b | 703 | if (freeing_dnode) { |
cae5b340 | 704 | dn->dn_objset->os_freed_dnodes++; |
34dc7c2f BB |
705 | dnode_sync_free(dn, tx); |
706 | return; | |
707 | } | |
708 | ||
cae5b340 AX |
709 | if (dn->dn_num_slots > DNODE_MIN_SLOTS) { |
710 | dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; | |
711 | mutex_enter(&ds->ds_lock); | |
712 | ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_DNODE] = | |
713 | B_TRUE; | |
714 | mutex_exit(&ds->ds_lock); | |
715 | } | |
716 | ||
ea04106b AX |
717 | if (dn->dn_next_nlevels[txgoff]) { |
718 | dnode_increase_indirection(dn, tx); | |
719 | dn->dn_next_nlevels[txgoff] = 0; | |
720 | } | |
721 | ||
d164b209 BB |
722 | if (dn->dn_next_nblkptr[txgoff]) { |
723 | /* this should only happen on a realloc */ | |
724 | ASSERT(dn->dn_allocated_txg == tx->tx_txg); | |
725 | if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) { | |
726 | /* zero the new blkptrs we are gaining */ | |
727 | bzero(dnp->dn_blkptr + dnp->dn_nblkptr, | |
728 | sizeof (blkptr_t) * | |
729 | (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr)); | |
730 | #ifdef ZFS_DEBUG | |
731 | } else { | |
732 | int i; | |
733 | ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr); | |
734 | /* the blkptrs we are losing better be unallocated */ | |
37f000c5 NB |
735 | for (i = 0; i < dnp->dn_nblkptr; i++) { |
736 | if (i >= dn->dn_next_nblkptr[txgoff]) | |
737 | ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i])); | |
738 | } | |
d164b209 BB |
739 | #endif |
740 | } | |
741 | mutex_enter(&dn->dn_mtx); | |
742 | dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff]; | |
743 | dn->dn_next_nblkptr[txgoff] = 0; | |
744 | mutex_exit(&dn->dn_mtx); | |
745 | } | |
746 | ||
e10b0808 | 747 | dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx); |
34dc7c2f | 748 | |
9babb374 | 749 | if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { |
34dc7c2f BB |
750 | ASSERT3P(list_head(list), ==, NULL); |
751 | dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); | |
752 | } | |
753 | ||
754 | /* | |
755 | * Although we have dropped our reference to the dnode, it | |
756 | * can't be evicted until its written, and we haven't yet | |
757 | * initiated the IO for the dnode's dbuf. | |
758 | */ | |
759 | } |