]>
Commit | Line | Data |
---|---|---|
70e083d2 TG |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. | |
86e3c28a | 24 | * Copyright (c) 2012, 2017 by Delphix. All rights reserved. |
70e083d2 TG |
25 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. |
26 | */ | |
27 | ||
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> | |
36 | #include <sys/range_tree.h> | |
37 | #include <sys/zfeature.h> | |
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 | ||
86e3c28a CIK |
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); | |
70e083d2 TG |
71 | |
72 | /* set dbuf's parent pointers to new indirect buf */ | |
73 | for (i = 0; i < nblkptr; i++) { | |
74 | dmu_buf_impl_t *child = | |
75 | dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i); | |
76 | ||
77 | if (child == NULL) | |
78 | continue; | |
79 | #ifdef DEBUG | |
80 | DB_DNODE_ENTER(child); | |
81 | ASSERT3P(DB_DNODE(child), ==, dn); | |
82 | DB_DNODE_EXIT(child); | |
83 | #endif /* DEBUG */ | |
84 | if (child->db_parent && child->db_parent != dn->dn_dbuf) { | |
85 | ASSERT(child->db_parent->db_level == db->db_level); | |
86 | ASSERT(child->db_blkptr != | |
87 | &dn->dn_phys->dn_blkptr[child->db_blkid]); | |
88 | mutex_exit(&child->db_mtx); | |
89 | continue; | |
90 | } | |
91 | ASSERT(child->db_parent == NULL || | |
92 | child->db_parent == dn->dn_dbuf); | |
93 | ||
94 | child->db_parent = db; | |
95 | dbuf_add_ref(db, child); | |
96 | if (db->db.db_data) | |
97 | child->db_blkptr = (blkptr_t *)db->db.db_data + i; | |
98 | else | |
99 | child->db_blkptr = NULL; | |
100 | dprintf_dbuf_bp(child, child->db_blkptr, | |
101 | "changed db_blkptr to new indirect %s", ""); | |
102 | ||
103 | mutex_exit(&child->db_mtx); | |
104 | } | |
105 | ||
106 | bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); | |
107 | ||
108 | dbuf_rele(db, FTAG); | |
109 | ||
110 | rw_exit(&dn->dn_struct_rwlock); | |
111 | } | |
112 | ||
113 | static void | |
114 | free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) | |
115 | { | |
116 | dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; | |
117 | uint64_t bytesfreed = 0; | |
118 | int i; | |
119 | ||
120 | dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num); | |
121 | ||
122 | for (i = 0; i < num; i++, bp++) { | |
123 | uint64_t lsize, lvl; | |
124 | dmu_object_type_t type; | |
125 | ||
126 | if (BP_IS_HOLE(bp)) | |
127 | continue; | |
128 | ||
129 | bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE); | |
130 | ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); | |
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 | ||
144 | bzero(bp, sizeof (blkptr_t)); | |
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 | } | |
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; | |
164 | dnode_t *dn; | |
165 | ||
166 | DB_DNODE_ENTER(db); | |
167 | dn = DB_DNODE(db); | |
168 | epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; | |
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); | |
175 | ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); | |
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 | ||
187 | rw_enter(&dn->dn_struct_rwlock, RW_READER); | |
188 | err = dbuf_hold_impl(dn, db->db_level-1, | |
86e3c28a | 189 | (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child); |
70e083d2 TG |
190 | rw_exit(&dn->dn_struct_rwlock); |
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", | |
207 | (void *)child, i, off, num); | |
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", | |
224 | (void *)child, i, off, num); | |
225 | } | |
226 | } | |
227 | } | |
228 | mutex_exit(&child->db_mtx); | |
229 | ||
230 | dbuf_rele(child, FTAG); | |
231 | } | |
232 | DB_DNODE_EXIT(db); | |
233 | } | |
234 | #endif | |
235 | ||
236 | static void | |
237 | free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, | |
238 | dmu_tx_t *tx) | |
239 | { | |
240 | dnode_t *dn; | |
241 | blkptr_t *bp; | |
242 | dmu_buf_impl_t *subdb; | |
86e3c28a CIK |
243 | uint64_t start, end, dbstart, dbend; |
244 | unsigned int epbs, shift, i; | |
245 | uint64_t id; | |
70e083d2 TG |
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 | |
250 | * 2 - if this block was evicted since we read it from | |
251 | * dmu_tx_hold_free(). | |
252 | */ | |
253 | if (db->db_state != DB_CACHED) | |
254 | (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); | |
255 | ||
256 | dbuf_release_bp(db); | |
257 | bp = db->db.db_data; | |
258 | ||
259 | DB_DNODE_ENTER(db); | |
260 | dn = DB_DNODE(db); | |
261 | epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; | |
86e3c28a | 262 | ASSERT3U(epbs, <, 31); |
70e083d2 TG |
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; | |
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; | |
275 | ||
276 | ASSERT3U(start, <=, end); | |
277 | ||
278 | if (db->db_level == 1) { | |
279 | FREE_VERIFY(db, start, end, tx); | |
280 | free_blocks(dn, bp, end-start+1, tx); | |
281 | } else { | |
86e3c28a | 282 | for (id = start; id <= end; id++, bp++) { |
70e083d2 TG |
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, | |
86e3c28a | 287 | id, TRUE, FALSE, FTAG, &subdb)); |
70e083d2 TG |
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 | } | |
294 | } | |
295 | ||
296 | /* If this whole block is free, free ourself too. */ | |
86e3c28a | 297 | for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) { |
70e083d2 TG |
298 | if (!BP_IS_HOLE(bp)) |
299 | break; | |
300 | } | |
301 | if (i == 1 << epbs) { | |
86e3c28a CIK |
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); | |
70e083d2 | 308 | bzero(db->db.db_data, db->db.db_size); |
86e3c28a | 309 | rw_exit(&dn->dn_struct_rwlock); |
70e083d2 TG |
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); | |
317 | } | |
318 | ||
319 | DB_DNODE_EXIT(db); | |
320 | arc_buf_freeze(db->db_buf); | |
321 | } | |
322 | ||
323 | /* | |
324 | * Traverse the indicated range of the provided file | |
325 | * and "free" all the blocks contained there. | |
326 | */ | |
327 | static void | |
328 | dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks, | |
329 | dmu_tx_t *tx) | |
330 | { | |
331 | blkptr_t *bp = dn->dn_phys->dn_blkptr; | |
332 | int dnlevel = dn->dn_phys->dn_nlevels; | |
333 | boolean_t trunc = B_FALSE; | |
334 | ||
335 | if (blkid > dn->dn_phys->dn_maxblkid) | |
336 | return; | |
337 | ||
338 | ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); | |
339 | if (blkid + nblks > dn->dn_phys->dn_maxblkid) { | |
340 | nblks = dn->dn_phys->dn_maxblkid - blkid + 1; | |
341 | trunc = B_TRUE; | |
342 | } | |
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); | |
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, | |
86e3c28a | 367 | TRUE, FALSE, FTAG, &db)); |
70e083d2 TG |
368 | rw_exit(&dn->dn_struct_rwlock); |
369 | ||
370 | free_children(db, blkid, nblks, tx); | |
371 | dbuf_rele(db, FTAG); | |
372 | } | |
373 | } | |
374 | ||
375 | if (trunc) { | |
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) * | |
380 | (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT)); | |
381 | ASSERT(off < dn->dn_phys->dn_maxblkid || | |
382 | dn->dn_phys->dn_maxblkid == 0 || | |
383 | dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0); | |
384 | } | |
385 | } | |
386 | ||
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 | ||
403 | /* | |
404 | * Try to kick all the dnode's dbufs out of the cache... | |
405 | */ | |
406 | void | |
407 | dnode_evict_dbufs(dnode_t *dn) | |
408 | { | |
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 | ||
417 | #ifdef DEBUG | |
418 | DB_DNODE_ENTER(db); | |
419 | ASSERT3P(DB_DNODE(db), ==, dn); | |
420 | DB_DNODE_EXIT(db); | |
421 | #endif /* DEBUG */ | |
422 | ||
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 | ||
86e3c28a | 432 | dbuf_destroy(db); |
70e083d2 TG |
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); | |
440 | } | |
441 | } | |
442 | mutex_exit(&dn->dn_dbufs_mtx); | |
443 | ||
444 | kmem_free(db_marker, sizeof (dmu_buf_impl_t)); | |
445 | ||
446 | dnode_evict_bonus(dn); | |
447 | } | |
448 | ||
449 | void | |
450 | dnode_evict_bonus(dnode_t *dn) | |
451 | { | |
452 | rw_enter(&dn->dn_struct_rwlock, RW_WRITER); | |
453 | if (dn->dn_bonus != NULL) { | |
454 | if (refcount_is_zero(&dn->dn_bonus->db_holds)) { | |
455 | mutex_enter(&dn->dn_bonus->db_mtx); | |
86e3c28a | 456 | dbuf_destroy(dn->dn_bonus); |
70e083d2 TG |
457 | dn->dn_bonus = NULL; |
458 | } else { | |
459 | dn->dn_bonus->db_pending_evict = TRUE; | |
460 | } | |
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 | ||
470 | while ((dr = list_head(list))) { | |
471 | dmu_buf_impl_t *db = dr->dr_dbuf; | |
472 | uint64_t txg = dr->dr_txg; | |
473 | ||
474 | if (db->db_level != 0) | |
475 | dnode_undirty_dbufs(&dr->dt.di.dr_children); | |
476 | ||
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) { | |
484 | ASSERT(db->db_blkid == DMU_BONUS_BLKID || | |
485 | dr->dt.dl.dr_data == db->db_buf); | |
486 | dbuf_unoverride(dr); | |
487 | } else { | |
488 | mutex_destroy(&dr->dt.di.dr_mtx); | |
489 | list_destroy(&dr->dt.di.dr_children); | |
490 | } | |
491 | kmem_free(dr, sizeof (dbuf_dirty_record_t)); | |
492 | dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg); | |
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 | ||
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 | */ | |
507 | ASSERT0(DN_USED_BYTES(dn->dn_phys)); | |
508 | ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr)); | |
509 | ||
510 | dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); | |
511 | dnode_evict_dbufs(dn); | |
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 | ||
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) | |
534 | dmu_buf_will_dirty(&dn->dn_dbuf->db, tx); | |
86e3c28a | 535 | bzero(dn->dn_phys, sizeof (dnode_phys_t) * dn->dn_num_slots); |
70e083d2 TG |
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; | |
542 | dn->dn_have_spill = B_FALSE; | |
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 | |
86e3c28a | 550 | * be evicted, so we mustn't access it. |
70e083d2 TG |
551 | */ |
552 | } | |
553 | ||
554 | /* | |
555 | * Write out the dnode's dirty buffers. | |
556 | */ | |
557 | void | |
558 | dnode_sync(dnode_t *dn, dmu_tx_t *tx) | |
559 | { | |
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]; | |
563 | boolean_t kill_spill = B_FALSE; | |
564 | boolean_t freeing_dnode; | |
565 | ASSERTV(static const dnode_phys_t zerodn = { 0 }); | |
566 | ||
567 | ASSERT(dmu_tx_is_syncing(tx)); | |
568 | ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); | |
569 | ASSERT(dnp->dn_type != DMU_OT_NONE || | |
86e3c28a | 570 | bcmp(dnp, &zerodn, DNODE_MIN_SIZE) == 0); |
70e083d2 TG |
571 | DNODE_VERIFY(dn); |
572 | ||
573 | ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); | |
574 | ||
575 | if (dmu_objset_userused_enabled(dn->dn_objset) && | |
576 | !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { | |
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; | |
580 | dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED; | |
86e3c28a CIK |
581 | if (dmu_objset_userobjused_enabled(dn->dn_objset)) |
582 | dn->dn_phys->dn_flags |= | |
583 | DNODE_FLAG_USEROBJUSED_ACCOUNTED; | |
70e083d2 TG |
584 | mutex_exit(&dn->dn_mtx); |
585 | dmu_objset_userquota_get_ids(dn, B_FALSE, tx); | |
586 | } else { | |
587 | /* Once we account for it, we should always account for it. */ | |
588 | ASSERT(!(dn->dn_phys->dn_flags & | |
589 | DNODE_FLAG_USERUSED_ACCOUNTED)); | |
86e3c28a CIK |
590 | ASSERT(!(dn->dn_phys->dn_flags & |
591 | DNODE_FLAG_USEROBJUSED_ACCOUNTED)); | |
70e083d2 TG |
592 | } |
593 | ||
594 | mutex_enter(&dn->dn_mtx); | |
595 | if (dn->dn_allocated_txg == tx->tx_txg) { | |
596 | /* The dnode is newly allocated or reallocated */ | |
597 | if (dnp->dn_type == DMU_OT_NONE) { | |
598 | /* this is a first alloc, not a realloc */ | |
599 | dnp->dn_nlevels = 1; | |
600 | dnp->dn_nblkptr = dn->dn_nblkptr; | |
601 | } | |
602 | ||
603 | dnp->dn_type = dn->dn_type; | |
604 | dnp->dn_bonustype = dn->dn_bonustype; | |
605 | dnp->dn_bonuslen = dn->dn_bonuslen; | |
606 | } | |
86e3c28a CIK |
607 | |
608 | dnp->dn_extra_slots = dn->dn_num_slots - 1; | |
609 | ||
70e083d2 TG |
610 | ASSERT(dnp->dn_nlevels > 1 || |
611 | BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
612 | BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) || | |
613 | BP_GET_LSIZE(&dnp->dn_blkptr[0]) == | |
614 | dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); | |
615 | ASSERT(dnp->dn_nlevels < 2 || | |
616 | BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
617 | BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift); | |
618 | ||
619 | if (dn->dn_next_type[txgoff] != 0) { | |
620 | dnp->dn_type = dn->dn_type; | |
621 | dn->dn_next_type[txgoff] = 0; | |
622 | } | |
623 | ||
624 | if (dn->dn_next_blksz[txgoff] != 0) { | |
625 | ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], | |
626 | SPA_MINBLOCKSIZE) == 0); | |
627 | ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || | |
628 | dn->dn_maxblkid == 0 || list_head(list) != NULL || | |
629 | dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == | |
630 | dnp->dn_datablkszsec || | |
631 | range_tree_space(dn->dn_free_ranges[txgoff]) != 0); | |
632 | dnp->dn_datablkszsec = | |
633 | dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; | |
634 | dn->dn_next_blksz[txgoff] = 0; | |
635 | } | |
636 | ||
637 | if (dn->dn_next_bonuslen[txgoff] != 0) { | |
638 | if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) | |
639 | dnp->dn_bonuslen = 0; | |
640 | else | |
641 | dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; | |
86e3c28a CIK |
642 | ASSERT(dnp->dn_bonuslen <= |
643 | DN_SLOTS_TO_BONUSLEN(dnp->dn_extra_slots + 1)); | |
70e083d2 TG |
644 | dn->dn_next_bonuslen[txgoff] = 0; |
645 | } | |
646 | ||
647 | if (dn->dn_next_bonustype[txgoff] != 0) { | |
648 | ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff])); | |
649 | dnp->dn_bonustype = dn->dn_next_bonustype[txgoff]; | |
650 | dn->dn_next_bonustype[txgoff] = 0; | |
651 | } | |
652 | ||
653 | freeing_dnode = dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg; | |
654 | ||
655 | /* | |
656 | * Remove the spill block if we have been explicitly asked to | |
657 | * remove it, or if the object is being removed. | |
658 | */ | |
659 | if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) { | |
660 | if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) | |
661 | kill_spill = B_TRUE; | |
662 | dn->dn_rm_spillblk[txgoff] = 0; | |
663 | } | |
664 | ||
665 | if (dn->dn_next_indblkshift[txgoff] != 0) { | |
666 | ASSERT(dnp->dn_nlevels == 1); | |
667 | dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; | |
668 | dn->dn_next_indblkshift[txgoff] = 0; | |
669 | } | |
670 | ||
671 | /* | |
672 | * Just take the live (open-context) values for checksum and compress. | |
673 | * Strictly speaking it's a future leak, but nothing bad happens if we | |
674 | * start using the new checksum or compress algorithm a little early. | |
675 | */ | |
676 | dnp->dn_checksum = dn->dn_checksum; | |
677 | dnp->dn_compress = dn->dn_compress; | |
678 | ||
679 | mutex_exit(&dn->dn_mtx); | |
680 | ||
681 | if (kill_spill) { | |
86e3c28a | 682 | free_blocks(dn, DN_SPILL_BLKPTR(dn->dn_phys), 1, tx); |
70e083d2 TG |
683 | mutex_enter(&dn->dn_mtx); |
684 | dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR; | |
685 | mutex_exit(&dn->dn_mtx); | |
686 | } | |
687 | ||
688 | /* process all the "freed" ranges in the file */ | |
689 | if (dn->dn_free_ranges[txgoff] != NULL) { | |
690 | dnode_sync_free_range_arg_t dsfra; | |
691 | dsfra.dsfra_dnode = dn; | |
692 | dsfra.dsfra_tx = tx; | |
693 | mutex_enter(&dn->dn_mtx); | |
694 | range_tree_vacate(dn->dn_free_ranges[txgoff], | |
695 | dnode_sync_free_range, &dsfra); | |
696 | range_tree_destroy(dn->dn_free_ranges[txgoff]); | |
697 | dn->dn_free_ranges[txgoff] = NULL; | |
698 | mutex_exit(&dn->dn_mtx); | |
699 | } | |
700 | ||
701 | if (freeing_dnode) { | |
86e3c28a | 702 | dn->dn_objset->os_freed_dnodes++; |
70e083d2 TG |
703 | dnode_sync_free(dn, tx); |
704 | return; | |
705 | } | |
706 | ||
86e3c28a CIK |
707 | if (dn->dn_num_slots > DNODE_MIN_SLOTS) { |
708 | dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; | |
709 | mutex_enter(&ds->ds_lock); | |
710 | ds->ds_feature_activation_needed[SPA_FEATURE_LARGE_DNODE] = | |
711 | B_TRUE; | |
712 | mutex_exit(&ds->ds_lock); | |
713 | } | |
714 | ||
70e083d2 TG |
715 | if (dn->dn_next_nlevels[txgoff]) { |
716 | dnode_increase_indirection(dn, tx); | |
717 | dn->dn_next_nlevels[txgoff] = 0; | |
718 | } | |
719 | ||
720 | if (dn->dn_next_nblkptr[txgoff]) { | |
721 | /* this should only happen on a realloc */ | |
722 | ASSERT(dn->dn_allocated_txg == tx->tx_txg); | |
723 | if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) { | |
724 | /* zero the new blkptrs we are gaining */ | |
725 | bzero(dnp->dn_blkptr + dnp->dn_nblkptr, | |
726 | sizeof (blkptr_t) * | |
727 | (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr)); | |
728 | #ifdef ZFS_DEBUG | |
729 | } else { | |
730 | int i; | |
731 | ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr); | |
732 | /* the blkptrs we are losing better be unallocated */ | |
733 | for (i = 0; i < dnp->dn_nblkptr; i++) { | |
734 | if (i >= dn->dn_next_nblkptr[txgoff]) | |
735 | ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i])); | |
736 | } | |
737 | #endif | |
738 | } | |
739 | mutex_enter(&dn->dn_mtx); | |
740 | dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff]; | |
741 | dn->dn_next_nblkptr[txgoff] = 0; | |
742 | mutex_exit(&dn->dn_mtx); | |
743 | } | |
744 | ||
745 | dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx); | |
746 | ||
747 | if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { | |
748 | ASSERT3P(list_head(list), ==, NULL); | |
749 | dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); | |
750 | } | |
751 | ||
752 | /* | |
753 | * Although we have dropped our reference to the dnode, it | |
754 | * can't be evicted until its written, and we haven't yet | |
755 | * initiated the IO for the dnode's dbuf. | |
756 | */ | |
757 | } |