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