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