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