]>
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. |
9bd274dd | 24 | * Copyright (c) 2012, 2014 by Delphix. All rights reserved. |
34dc7c2f BB |
25 | */ |
26 | ||
34dc7c2f BB |
27 | #include <sys/zfs_context.h> |
28 | #include <sys/dbuf.h> | |
29 | #include <sys/dnode.h> | |
30 | #include <sys/dmu.h> | |
31 | #include <sys/dmu_tx.h> | |
32 | #include <sys/dmu_objset.h> | |
33 | #include <sys/dsl_dataset.h> | |
34 | #include <sys/spa.h> | |
9bd274dd | 35 | #include <sys/range_tree.h> |
b0bc7a84 | 36 | #include <sys/zfeature.h> |
34dc7c2f BB |
37 | |
38 | static void | |
39 | dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) | |
40 | { | |
41 | dmu_buf_impl_t *db; | |
42 | int txgoff = tx->tx_txg & TXG_MASK; | |
43 | int nblkptr = dn->dn_phys->dn_nblkptr; | |
44 | int old_toplvl = dn->dn_phys->dn_nlevels - 1; | |
45 | int new_level = dn->dn_next_nlevels[txgoff]; | |
46 | int i; | |
47 | ||
48 | rw_enter(&dn->dn_struct_rwlock, RW_WRITER); | |
49 | ||
50 | /* this dnode can't be paged out because it's dirty */ | |
51 | ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); | |
52 | ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); | |
53 | ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0); | |
54 | ||
55 | db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); | |
56 | ASSERT(db != NULL); | |
57 | ||
58 | dn->dn_phys->dn_nlevels = new_level; | |
59 | dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset, | |
60 | dn->dn_object, dn->dn_phys->dn_nlevels); | |
61 | ||
62 | /* check for existing blkptrs in the dnode */ | |
63 | for (i = 0; i < nblkptr; i++) | |
64 | if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[i])) | |
65 | break; | |
66 | if (i != nblkptr) { | |
67 | /* transfer dnode's block pointers to new indirect block */ | |
68 | (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT); | |
69 | ASSERT(db->db.db_data); | |
70 | ASSERT(arc_released(db->db_buf)); | |
71 | ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size); | |
72 | bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, | |
73 | sizeof (blkptr_t) * nblkptr); | |
74 | arc_buf_freeze(db->db_buf); | |
75 | } | |
76 | ||
77 | /* set dbuf's parent pointers to new indirect buf */ | |
78 | for (i = 0; i < nblkptr; i++) { | |
79 | dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i); | |
80 | ||
81 | if (child == NULL) | |
82 | continue; | |
572e2857 BB |
83 | #ifdef DEBUG |
84 | DB_DNODE_ENTER(child); | |
85 | ASSERT3P(DB_DNODE(child), ==, dn); | |
86 | DB_DNODE_EXIT(child); | |
87 | #endif /* DEBUG */ | |
34dc7c2f BB |
88 | if (child->db_parent && child->db_parent != dn->dn_dbuf) { |
89 | ASSERT(child->db_parent->db_level == db->db_level); | |
90 | ASSERT(child->db_blkptr != | |
91 | &dn->dn_phys->dn_blkptr[child->db_blkid]); | |
92 | mutex_exit(&child->db_mtx); | |
93 | continue; | |
94 | } | |
95 | ASSERT(child->db_parent == NULL || | |
96 | child->db_parent == dn->dn_dbuf); | |
97 | ||
98 | child->db_parent = db; | |
99 | dbuf_add_ref(db, child); | |
100 | if (db->db.db_data) | |
101 | child->db_blkptr = (blkptr_t *)db->db.db_data + i; | |
102 | else | |
103 | child->db_blkptr = NULL; | |
104 | dprintf_dbuf_bp(child, child->db_blkptr, | |
105 | "changed db_blkptr to new indirect %s", ""); | |
106 | ||
107 | mutex_exit(&child->db_mtx); | |
108 | } | |
109 | ||
110 | bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); | |
111 | ||
112 | dbuf_rele(db, FTAG); | |
113 | ||
114 | rw_exit(&dn->dn_struct_rwlock); | |
115 | } | |
116 | ||
b0bc7a84 | 117 | static void |
34dc7c2f BB |
118 | free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) |
119 | { | |
b128c09f | 120 | dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; |
34dc7c2f | 121 | uint64_t bytesfreed = 0; |
b0bc7a84 | 122 | int i; |
34dc7c2f | 123 | |
b128c09f | 124 | dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num); |
34dc7c2f BB |
125 | |
126 | for (i = 0; i < num; i++, bp++) { | |
b0bc7a84 MG |
127 | uint64_t lsize, lvl; |
128 | dmu_object_type_t type; | |
129 | ||
34dc7c2f BB |
130 | if (BP_IS_HOLE(bp)) |
131 | continue; | |
132 | ||
428870ff | 133 | bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE); |
34dc7c2f | 134 | ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); |
b0bc7a84 MG |
135 | |
136 | /* | |
137 | * Save some useful information on the holes being | |
138 | * punched, including logical size, type, and indirection | |
139 | * level. Retaining birth time enables detection of when | |
140 | * holes are punched for reducing the number of free | |
141 | * records transmitted during a zfs send. | |
142 | */ | |
143 | ||
144 | lsize = BP_GET_LSIZE(bp); | |
145 | type = BP_GET_TYPE(bp); | |
146 | lvl = BP_GET_LEVEL(bp); | |
147 | ||
34dc7c2f | 148 | bzero(bp, sizeof (blkptr_t)); |
b0bc7a84 MG |
149 | |
150 | if (spa_feature_is_active(dn->dn_objset->os_spa, | |
151 | SPA_FEATURE_HOLE_BIRTH)) { | |
152 | BP_SET_LSIZE(bp, lsize); | |
153 | BP_SET_TYPE(bp, type); | |
154 | BP_SET_LEVEL(bp, lvl); | |
155 | BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0); | |
156 | } | |
34dc7c2f BB |
157 | } |
158 | dnode_diduse_space(dn, -bytesfreed); | |
159 | } | |
160 | ||
161 | #ifdef ZFS_DEBUG | |
162 | static void | |
163 | free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) | |
164 | { | |
165 | int off, num; | |
166 | int i, err, epbs; | |
167 | uint64_t txg = tx->tx_txg; | |
572e2857 | 168 | dnode_t *dn; |
34dc7c2f | 169 | |
572e2857 BB |
170 | DB_DNODE_ENTER(db); |
171 | dn = DB_DNODE(db); | |
172 | epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; | |
34dc7c2f BB |
173 | off = start - (db->db_blkid * 1<<epbs); |
174 | num = end - start + 1; | |
175 | ||
176 | ASSERT3U(off, >=, 0); | |
177 | ASSERT3U(num, >=, 0); | |
178 | ASSERT3U(db->db_level, >, 0); | |
572e2857 | 179 | ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); |
34dc7c2f BB |
180 | ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); |
181 | ASSERT(db->db_blkptr != NULL); | |
182 | ||
183 | for (i = off; i < off+num; i++) { | |
184 | uint64_t *buf; | |
185 | dmu_buf_impl_t *child; | |
186 | dbuf_dirty_record_t *dr; | |
187 | int j; | |
188 | ||
189 | ASSERT(db->db_level == 1); | |
190 | ||
572e2857 BB |
191 | rw_enter(&dn->dn_struct_rwlock, RW_READER); |
192 | err = dbuf_hold_impl(dn, db->db_level-1, | |
34dc7c2f | 193 | (db->db_blkid << epbs) + i, TRUE, FTAG, &child); |
572e2857 | 194 | rw_exit(&dn->dn_struct_rwlock); |
34dc7c2f BB |
195 | if (err == ENOENT) |
196 | continue; | |
197 | ASSERT(err == 0); | |
198 | ASSERT(child->db_level == 0); | |
199 | dr = child->db_last_dirty; | |
200 | while (dr && dr->dr_txg > txg) | |
201 | dr = dr->dr_next; | |
202 | ASSERT(dr == NULL || dr->dr_txg == txg); | |
203 | ||
204 | /* data_old better be zeroed */ | |
205 | if (dr) { | |
206 | buf = dr->dt.dl.dr_data->b_data; | |
207 | for (j = 0; j < child->db.db_size >> 3; j++) { | |
208 | if (buf[j] != 0) { | |
209 | panic("freed data not zero: " | |
210 | "child=%p i=%d off=%d num=%d\n", | |
b128c09f | 211 | (void *)child, i, off, num); |
34dc7c2f BB |
212 | } |
213 | } | |
214 | } | |
215 | ||
216 | /* | |
217 | * db_data better be zeroed unless it's dirty in a | |
218 | * future txg. | |
219 | */ | |
220 | mutex_enter(&child->db_mtx); | |
221 | buf = child->db.db_data; | |
222 | if (buf != NULL && child->db_state != DB_FILL && | |
223 | child->db_last_dirty == NULL) { | |
224 | for (j = 0; j < child->db.db_size >> 3; j++) { | |
225 | if (buf[j] != 0) { | |
226 | panic("freed data not zero: " | |
227 | "child=%p i=%d off=%d num=%d\n", | |
b128c09f | 228 | (void *)child, i, off, num); |
34dc7c2f BB |
229 | } |
230 | } | |
231 | } | |
232 | mutex_exit(&child->db_mtx); | |
233 | ||
234 | dbuf_rele(child, FTAG); | |
235 | } | |
572e2857 | 236 | DB_DNODE_EXIT(db); |
34dc7c2f BB |
237 | } |
238 | #endif | |
239 | ||
b0bc7a84 MG |
240 | static void |
241 | free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, | |
34dc7c2f BB |
242 | dmu_tx_t *tx) |
243 | { | |
572e2857 | 244 | dnode_t *dn; |
34dc7c2f BB |
245 | blkptr_t *bp; |
246 | dmu_buf_impl_t *subdb; | |
247 | uint64_t start, end, dbstart, dbend, i; | |
b0bc7a84 | 248 | int epbs, shift; |
b128c09f BB |
249 | |
250 | /* | |
251 | * There is a small possibility that this block will not be cached: | |
252 | * 1 - if level > 1 and there are no children with level <= 1 | |
b0bc7a84 MG |
253 | * 2 - if this block was evicted since we read it from |
254 | * dmu_tx_hold_free(). | |
b128c09f BB |
255 | */ |
256 | if (db->db_state != DB_CACHED) | |
257 | (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); | |
34dc7c2f | 258 | |
428870ff | 259 | dbuf_release_bp(db); |
b0bc7a84 | 260 | bp = db->db.db_data; |
34dc7c2f | 261 | |
572e2857 BB |
262 | DB_DNODE_ENTER(db); |
263 | dn = DB_DNODE(db); | |
264 | epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; | |
34dc7c2f BB |
265 | shift = (db->db_level - 1) * epbs; |
266 | dbstart = db->db_blkid << epbs; | |
267 | start = blkid >> shift; | |
268 | if (dbstart < start) { | |
269 | bp += start - dbstart; | |
34dc7c2f BB |
270 | } else { |
271 | start = dbstart; | |
272 | } | |
273 | dbend = ((db->db_blkid + 1) << epbs) - 1; | |
274 | end = (blkid + nblks - 1) >> shift; | |
275 | if (dbend <= end) | |
276 | end = dbend; | |
b0bc7a84 | 277 | |
34dc7c2f BB |
278 | ASSERT3U(start, <=, end); |
279 | ||
280 | if (db->db_level == 1) { | |
281 | FREE_VERIFY(db, start, end, tx); | |
b0bc7a84 MG |
282 | free_blocks(dn, bp, end-start+1, tx); |
283 | } else { | |
284 | for (i = start; i <= end; i++, bp++) { | |
285 | if (BP_IS_HOLE(bp)) | |
286 | continue; | |
287 | rw_enter(&dn->dn_struct_rwlock, RW_READER); | |
288 | VERIFY0(dbuf_hold_impl(dn, db->db_level - 1, | |
289 | i, B_TRUE, FTAG, &subdb)); | |
290 | rw_exit(&dn->dn_struct_rwlock); | |
291 | ASSERT3P(bp, ==, subdb->db_blkptr); | |
292 | ||
293 | free_children(subdb, blkid, nblks, tx); | |
294 | dbuf_rele(subdb, FTAG); | |
295 | } | |
34dc7c2f BB |
296 | } |
297 | ||
b0bc7a84 MG |
298 | /* If this whole block is free, free ourself too. */ |
299 | for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) { | |
300 | if (!BP_IS_HOLE(bp)) | |
301 | break; | |
302 | } | |
303 | if (i == 1 << epbs) { | |
304 | /* didn't find any non-holes */ | |
305 | bzero(db->db.db_data, db->db.db_size); | |
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; | |
354 | int i; | |
355 | ||
356 | ASSERT(start < dn->dn_phys->dn_nblkptr); | |
357 | bp += start; | |
358 | for (i = start; i <= end; i++, bp++) { | |
359 | if (BP_IS_HOLE(bp)) | |
360 | continue; | |
361 | rw_enter(&dn->dn_struct_rwlock, RW_READER); | |
362 | VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i, | |
363 | TRUE, FTAG, &db)); | |
364 | rw_exit(&dn->dn_struct_rwlock); | |
365 | ||
366 | free_children(db, blkid, nblks, tx); | |
367 | dbuf_rele(db, FTAG); | |
34dc7c2f | 368 | } |
34dc7c2f BB |
369 | } |
370 | ||
34dc7c2f | 371 | if (trunc) { |
b0bc7a84 MG |
372 | ASSERTV(uint64_t off); |
373 | dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1; | |
374 | ||
375 | ASSERTV(off = (dn->dn_phys->dn_maxblkid + 1) * | |
1fde1e37 | 376 | (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT)); |
34dc7c2f BB |
377 | ASSERT(off < dn->dn_phys->dn_maxblkid || |
378 | dn->dn_phys->dn_maxblkid == 0 || | |
b128c09f | 379 | dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0); |
34dc7c2f BB |
380 | } |
381 | } | |
382 | ||
9bd274dd MA |
383 | typedef struct dnode_sync_free_range_arg { |
384 | dnode_t *dsfra_dnode; | |
385 | dmu_tx_t *dsfra_tx; | |
386 | } dnode_sync_free_range_arg_t; | |
387 | ||
388 | static void | |
389 | dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks) | |
390 | { | |
391 | dnode_sync_free_range_arg_t *dsfra = arg; | |
392 | dnode_t *dn = dsfra->dsfra_dnode; | |
393 | ||
394 | mutex_exit(&dn->dn_mtx); | |
395 | dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx); | |
396 | mutex_enter(&dn->dn_mtx); | |
397 | } | |
398 | ||
34dc7c2f | 399 | /* |
d3cc8b15 | 400 | * Try to kick all the dnode's dbufs out of the cache... |
34dc7c2f BB |
401 | */ |
402 | void | |
403 | dnode_evict_dbufs(dnode_t *dn) | |
404 | { | |
405 | int progress; | |
406 | int pass = 0; | |
407 | ||
408 | do { | |
409 | dmu_buf_impl_t *db, marker; | |
410 | int evicting = FALSE; | |
411 | ||
412 | progress = FALSE; | |
413 | mutex_enter(&dn->dn_dbufs_mtx); | |
414 | list_insert_tail(&dn->dn_dbufs, &marker); | |
415 | db = list_head(&dn->dn_dbufs); | |
416 | for (; db != ▮ db = list_head(&dn->dn_dbufs)) { | |
417 | list_remove(&dn->dn_dbufs, db); | |
418 | list_insert_tail(&dn->dn_dbufs, db); | |
572e2857 BB |
419 | #ifdef DEBUG |
420 | DB_DNODE_ENTER(db); | |
421 | ASSERT3P(DB_DNODE(db), ==, dn); | |
422 | DB_DNODE_EXIT(db); | |
423 | #endif /* DEBUG */ | |
34dc7c2f BB |
424 | |
425 | mutex_enter(&db->db_mtx); | |
426 | if (db->db_state == DB_EVICTING) { | |
427 | progress = TRUE; | |
428 | evicting = TRUE; | |
429 | mutex_exit(&db->db_mtx); | |
430 | } else if (refcount_is_zero(&db->db_holds)) { | |
431 | progress = TRUE; | |
34dc7c2f BB |
432 | dbuf_clear(db); /* exits db_mtx for us */ |
433 | } else { | |
434 | mutex_exit(&db->db_mtx); | |
435 | } | |
436 | ||
437 | } | |
438 | list_remove(&dn->dn_dbufs, &marker); | |
439 | /* | |
440 | * NB: we need to drop dn_dbufs_mtx between passes so | |
441 | * that any DB_EVICTING dbufs can make progress. | |
442 | * Ideally, we would have some cv we could wait on, but | |
443 | * since we don't, just wait a bit to give the other | |
444 | * thread a chance to run. | |
445 | */ | |
446 | mutex_exit(&dn->dn_dbufs_mtx); | |
447 | if (evicting) | |
448 | delay(1); | |
449 | pass++; | |
754c6663 BB |
450 | if ((pass % 100) == 0) |
451 | dprintf("Exceeded %d passes evicting dbufs\n", pass); | |
34dc7c2f BB |
452 | } while (progress); |
453 | ||
754c6663 BB |
454 | if (pass >= 100) |
455 | dprintf("Required %d passes to evict dbufs\n", pass); | |
456 | ||
34dc7c2f BB |
457 | rw_enter(&dn->dn_struct_rwlock, RW_WRITER); |
458 | if (dn->dn_bonus && refcount_is_zero(&dn->dn_bonus->db_holds)) { | |
459 | mutex_enter(&dn->dn_bonus->db_mtx); | |
460 | dbuf_evict(dn->dn_bonus); | |
461 | dn->dn_bonus = NULL; | |
462 | } | |
463 | rw_exit(&dn->dn_struct_rwlock); | |
464 | } | |
465 | ||
466 | static void | |
467 | dnode_undirty_dbufs(list_t *list) | |
468 | { | |
469 | dbuf_dirty_record_t *dr; | |
470 | ||
c65aa5b2 | 471 | while ((dr = list_head(list))) { |
34dc7c2f BB |
472 | dmu_buf_impl_t *db = dr->dr_dbuf; |
473 | uint64_t txg = dr->dr_txg; | |
474 | ||
428870ff BB |
475 | if (db->db_level != 0) |
476 | dnode_undirty_dbufs(&dr->dt.di.dr_children); | |
477 | ||
34dc7c2f BB |
478 | mutex_enter(&db->db_mtx); |
479 | /* XXX - use dbuf_undirty()? */ | |
480 | list_remove(list, dr); | |
481 | ASSERT(db->db_last_dirty == dr); | |
482 | db->db_last_dirty = NULL; | |
483 | db->db_dirtycnt -= 1; | |
484 | if (db->db_level == 0) { | |
428870ff | 485 | ASSERT(db->db_blkid == DMU_BONUS_BLKID || |
34dc7c2f BB |
486 | dr->dt.dl.dr_data == db->db_buf); |
487 | dbuf_unoverride(dr); | |
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 | */ | |
c99c9001 | 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); | |
510 | ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); | |
13fe0198 | 511 | ASSERT3P(dn->dn_bonus, ==, NULL); |
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) | |
b0bc7a84 | 534 | dmu_buf_will_dirty(&dn->dn_dbuf->db, tx); |
34dc7c2f BB |
535 | bzero(dn->dn_phys, sizeof (dnode_phys_t)); |
536 | ||
537 | mutex_enter(&dn->dn_mtx); | |
538 | dn->dn_type = DMU_OT_NONE; | |
539 | dn->dn_maxblkid = 0; | |
540 | dn->dn_allocated_txg = 0; | |
541 | dn->dn_free_txg = 0; | |
428870ff | 542 | dn->dn_have_spill = B_FALSE; |
34dc7c2f BB |
543 | mutex_exit(&dn->dn_mtx); |
544 | ||
545 | ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); | |
546 | ||
547 | dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); | |
548 | /* | |
549 | * Now that we've released our hold, the dnode may | |
550 | * be evicted, so we musn't access it. | |
551 | */ | |
552 | } | |
553 | ||
554 | /* | |
555 | * Write out the dnode's dirty buffers. | |
34dc7c2f BB |
556 | */ |
557 | void | |
558 | dnode_sync(dnode_t *dn, dmu_tx_t *tx) | |
559 | { | |
34dc7c2f BB |
560 | dnode_phys_t *dnp = dn->dn_phys; |
561 | int txgoff = tx->tx_txg & TXG_MASK; | |
562 | list_t *list = &dn->dn_dirty_records[txgoff]; | |
428870ff | 563 | boolean_t kill_spill = B_FALSE; |
b0bc7a84 | 564 | boolean_t freeing_dnode; |
1fde1e37 | 565 | ASSERTV(static const dnode_phys_t zerodn = { 0 }); |
34dc7c2f BB |
566 | |
567 | ASSERT(dmu_tx_is_syncing(tx)); | |
568 | ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); | |
9babb374 BB |
569 | ASSERT(dnp->dn_type != DMU_OT_NONE || |
570 | bcmp(dnp, &zerodn, DNODE_SIZE) == 0); | |
34dc7c2f BB |
571 | DNODE_VERIFY(dn); |
572 | ||
573 | ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); | |
574 | ||
9babb374 BB |
575 | if (dmu_objset_userused_enabled(dn->dn_objset) && |
576 | !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { | |
428870ff BB |
577 | mutex_enter(&dn->dn_mtx); |
578 | dn->dn_oldused = DN_USED_BYTES(dn->dn_phys); | |
579 | dn->dn_oldflags = dn->dn_phys->dn_flags; | |
9babb374 | 580 | dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED; |
428870ff BB |
581 | mutex_exit(&dn->dn_mtx); |
582 | dmu_objset_userquota_get_ids(dn, B_FALSE, tx); | |
9babb374 BB |
583 | } else { |
584 | /* Once we account for it, we should always account for it. */ | |
585 | ASSERT(!(dn->dn_phys->dn_flags & | |
586 | DNODE_FLAG_USERUSED_ACCOUNTED)); | |
587 | } | |
588 | ||
34dc7c2f BB |
589 | mutex_enter(&dn->dn_mtx); |
590 | if (dn->dn_allocated_txg == tx->tx_txg) { | |
591 | /* The dnode is newly allocated or reallocated */ | |
592 | if (dnp->dn_type == DMU_OT_NONE) { | |
593 | /* this is a first alloc, not a realloc */ | |
34dc7c2f | 594 | dnp->dn_nlevels = 1; |
d164b209 | 595 | dnp->dn_nblkptr = dn->dn_nblkptr; |
34dc7c2f BB |
596 | } |
597 | ||
34dc7c2f BB |
598 | dnp->dn_type = dn->dn_type; |
599 | dnp->dn_bonustype = dn->dn_bonustype; | |
600 | dnp->dn_bonuslen = dn->dn_bonuslen; | |
34dc7c2f | 601 | } |
34dc7c2f BB |
602 | ASSERT(dnp->dn_nlevels > 1 || |
603 | BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
9b67f605 | 604 | BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) || |
34dc7c2f BB |
605 | BP_GET_LSIZE(&dnp->dn_blkptr[0]) == |
606 | dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); | |
9b67f605 MA |
607 | ASSERT(dnp->dn_nlevels < 2 || |
608 | BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
609 | BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift); | |
34dc7c2f | 610 | |
fa86b5db MA |
611 | if (dn->dn_next_type[txgoff] != 0) { |
612 | dnp->dn_type = dn->dn_type; | |
613 | dn->dn_next_type[txgoff] = 0; | |
614 | } | |
615 | ||
616 | if (dn->dn_next_blksz[txgoff] != 0) { | |
34dc7c2f BB |
617 | ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], |
618 | SPA_MINBLOCKSIZE) == 0); | |
619 | ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
b128c09f | 620 | dn->dn_maxblkid == 0 || list_head(list) != NULL || |
34dc7c2f | 621 | dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == |
9bd274dd MA |
622 | dnp->dn_datablkszsec || |
623 | range_tree_space(dn->dn_free_ranges[txgoff]) != 0); | |
34dc7c2f BB |
624 | dnp->dn_datablkszsec = |
625 | dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; | |
626 | dn->dn_next_blksz[txgoff] = 0; | |
627 | } | |
628 | ||
fa86b5db | 629 | if (dn->dn_next_bonuslen[txgoff] != 0) { |
34dc7c2f BB |
630 | if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) |
631 | dnp->dn_bonuslen = 0; | |
632 | else | |
633 | dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; | |
634 | ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN); | |
635 | dn->dn_next_bonuslen[txgoff] = 0; | |
636 | } | |
637 | ||
fa86b5db | 638 | if (dn->dn_next_bonustype[txgoff] != 0) { |
9ae529ec | 639 | ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff])); |
428870ff BB |
640 | dnp->dn_bonustype = dn->dn_next_bonustype[txgoff]; |
641 | dn->dn_next_bonustype[txgoff] = 0; | |
642 | } | |
643 | ||
b0bc7a84 MG |
644 | freeing_dnode = dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg; |
645 | ||
428870ff BB |
646 | /* |
647 | * We will either remove a spill block when a file is being removed | |
648 | * or we have been asked to remove it. | |
649 | */ | |
650 | if (dn->dn_rm_spillblk[txgoff] || | |
b0bc7a84 | 651 | ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) && freeing_dnode)) { |
428870ff BB |
652 | if ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) |
653 | kill_spill = B_TRUE; | |
654 | dn->dn_rm_spillblk[txgoff] = 0; | |
655 | } | |
656 | ||
fa86b5db | 657 | if (dn->dn_next_indblkshift[txgoff] != 0) { |
34dc7c2f BB |
658 | ASSERT(dnp->dn_nlevels == 1); |
659 | dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; | |
660 | dn->dn_next_indblkshift[txgoff] = 0; | |
661 | } | |
662 | ||
663 | /* | |
664 | * Just take the live (open-context) values for checksum and compress. | |
665 | * Strictly speaking it's a future leak, but nothing bad happens if we | |
666 | * start using the new checksum or compress algorithm a little early. | |
667 | */ | |
668 | dnp->dn_checksum = dn->dn_checksum; | |
669 | dnp->dn_compress = dn->dn_compress; | |
670 | ||
671 | mutex_exit(&dn->dn_mtx); | |
672 | ||
428870ff | 673 | if (kill_spill) { |
b0bc7a84 | 674 | free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx); |
428870ff BB |
675 | mutex_enter(&dn->dn_mtx); |
676 | dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR; | |
677 | mutex_exit(&dn->dn_mtx); | |
678 | } | |
679 | ||
34dc7c2f | 680 | /* process all the "freed" ranges in the file */ |
9bd274dd MA |
681 | if (dn->dn_free_ranges[txgoff] != NULL) { |
682 | dnode_sync_free_range_arg_t dsfra; | |
683 | dsfra.dsfra_dnode = dn; | |
684 | dsfra.dsfra_tx = tx; | |
b128c09f | 685 | mutex_enter(&dn->dn_mtx); |
9bd274dd MA |
686 | range_tree_vacate(dn->dn_free_ranges[txgoff], |
687 | dnode_sync_free_range, &dsfra); | |
688 | range_tree_destroy(dn->dn_free_ranges[txgoff]); | |
689 | dn->dn_free_ranges[txgoff] = NULL; | |
b128c09f | 690 | mutex_exit(&dn->dn_mtx); |
34dc7c2f | 691 | } |
34dc7c2f | 692 | |
b0bc7a84 | 693 | if (freeing_dnode) { |
34dc7c2f BB |
694 | dnode_sync_free(dn, tx); |
695 | return; | |
696 | } | |
697 | ||
4f68d787 GW |
698 | if (dn->dn_next_nlevels[txgoff]) { |
699 | dnode_increase_indirection(dn, tx); | |
700 | dn->dn_next_nlevels[txgoff] = 0; | |
701 | } | |
702 | ||
d164b209 BB |
703 | if (dn->dn_next_nblkptr[txgoff]) { |
704 | /* this should only happen on a realloc */ | |
705 | ASSERT(dn->dn_allocated_txg == tx->tx_txg); | |
706 | if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) { | |
707 | /* zero the new blkptrs we are gaining */ | |
708 | bzero(dnp->dn_blkptr + dnp->dn_nblkptr, | |
709 | sizeof (blkptr_t) * | |
710 | (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr)); | |
711 | #ifdef ZFS_DEBUG | |
712 | } else { | |
713 | int i; | |
714 | ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr); | |
715 | /* the blkptrs we are losing better be unallocated */ | |
37f000c5 NB |
716 | for (i = 0; i < dnp->dn_nblkptr; i++) { |
717 | if (i >= dn->dn_next_nblkptr[txgoff]) | |
718 | ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i])); | |
719 | } | |
d164b209 BB |
720 | #endif |
721 | } | |
722 | mutex_enter(&dn->dn_mtx); | |
723 | dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff]; | |
724 | dn->dn_next_nblkptr[txgoff] = 0; | |
725 | mutex_exit(&dn->dn_mtx); | |
726 | } | |
727 | ||
34dc7c2f BB |
728 | dbuf_sync_list(list, tx); |
729 | ||
9babb374 | 730 | if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { |
34dc7c2f BB |
731 | ASSERT3P(list_head(list), ==, NULL); |
732 | dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); | |
733 | } | |
734 | ||
735 | /* | |
736 | * Although we have dropped our reference to the dnode, it | |
737 | * can't be evicted until its written, and we haven't yet | |
738 | * initiated the IO for the dnode's dbuf. | |
739 | */ | |
740 | } |