Print this page
4047 panic from dbuf_free_range() from dmu_free_object() while doing zfs receive
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/dbuf.c
+++ new/usr/src/uts/common/fs/zfs/dbuf.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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 26 */
27 27
28 28 #include <sys/zfs_context.h>
29 29 #include <sys/dmu.h>
30 30 #include <sys/dmu_send.h>
31 31 #include <sys/dmu_impl.h>
32 32 #include <sys/dbuf.h>
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
33 33 #include <sys/dmu_objset.h>
34 34 #include <sys/dsl_dataset.h>
35 35 #include <sys/dsl_dir.h>
36 36 #include <sys/dmu_tx.h>
37 37 #include <sys/spa.h>
38 38 #include <sys/zio.h>
39 39 #include <sys/dmu_zfetch.h>
40 40 #include <sys/sa.h>
41 41 #include <sys/sa_impl.h>
42 42
43 +/*
44 + * Number of times that zfs_free_range() took the slow path while doing
45 + * a zfs receive. A nonzero value indicates a potential performance problem.
46 + */
47 +uint64_t zfs_free_range_recv_miss;
48 +
43 49 static void dbuf_destroy(dmu_buf_impl_t *db);
44 50 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
45 51 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
46 52
47 53 /*
48 54 * Global data structures and functions for the dbuf cache.
49 55 */
50 56 static kmem_cache_t *dbuf_cache;
51 57
52 58 /* ARGSUSED */
53 59 static int
54 60 dbuf_cons(void *vdb, void *unused, int kmflag)
55 61 {
56 62 dmu_buf_impl_t *db = vdb;
57 63 bzero(db, sizeof (dmu_buf_impl_t));
58 64
59 65 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
60 66 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
61 67 refcount_create(&db->db_holds);
62 68 return (0);
63 69 }
64 70
65 71 /* ARGSUSED */
66 72 static void
67 73 dbuf_dest(void *vdb, void *unused)
68 74 {
69 75 dmu_buf_impl_t *db = vdb;
70 76 mutex_destroy(&db->db_mtx);
71 77 cv_destroy(&db->db_changed);
72 78 refcount_destroy(&db->db_holds);
73 79 }
74 80
75 81 /*
76 82 * dbuf hash table routines
77 83 */
78 84 static dbuf_hash_table_t dbuf_hash_table;
79 85
80 86 static uint64_t dbuf_hash_count;
81 87
82 88 static uint64_t
83 89 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
84 90 {
85 91 uintptr_t osv = (uintptr_t)os;
86 92 uint64_t crc = -1ULL;
87 93
88 94 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
89 95 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
90 96 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
91 97 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
92 98 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
93 99 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
94 100 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
95 101
96 102 crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
97 103
98 104 return (crc);
99 105 }
100 106
101 107 #define DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
102 108
103 109 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
104 110 ((dbuf)->db.db_object == (obj) && \
105 111 (dbuf)->db_objset == (os) && \
106 112 (dbuf)->db_level == (level) && \
107 113 (dbuf)->db_blkid == (blkid))
108 114
109 115 dmu_buf_impl_t *
110 116 dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
111 117 {
112 118 dbuf_hash_table_t *h = &dbuf_hash_table;
113 119 objset_t *os = dn->dn_objset;
114 120 uint64_t obj = dn->dn_object;
115 121 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
116 122 uint64_t idx = hv & h->hash_table_mask;
117 123 dmu_buf_impl_t *db;
118 124
119 125 mutex_enter(DBUF_HASH_MUTEX(h, idx));
120 126 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
121 127 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
122 128 mutex_enter(&db->db_mtx);
123 129 if (db->db_state != DB_EVICTING) {
124 130 mutex_exit(DBUF_HASH_MUTEX(h, idx));
125 131 return (db);
126 132 }
127 133 mutex_exit(&db->db_mtx);
128 134 }
129 135 }
130 136 mutex_exit(DBUF_HASH_MUTEX(h, idx));
131 137 return (NULL);
132 138 }
133 139
134 140 /*
135 141 * Insert an entry into the hash table. If there is already an element
136 142 * equal to elem in the hash table, then the already existing element
137 143 * will be returned and the new element will not be inserted.
138 144 * Otherwise returns NULL.
139 145 */
140 146 static dmu_buf_impl_t *
141 147 dbuf_hash_insert(dmu_buf_impl_t *db)
142 148 {
143 149 dbuf_hash_table_t *h = &dbuf_hash_table;
144 150 objset_t *os = db->db_objset;
145 151 uint64_t obj = db->db.db_object;
146 152 int level = db->db_level;
147 153 uint64_t blkid = db->db_blkid;
148 154 uint64_t hv = DBUF_HASH(os, obj, level, blkid);
149 155 uint64_t idx = hv & h->hash_table_mask;
150 156 dmu_buf_impl_t *dbf;
151 157
152 158 mutex_enter(DBUF_HASH_MUTEX(h, idx));
153 159 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
154 160 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
155 161 mutex_enter(&dbf->db_mtx);
156 162 if (dbf->db_state != DB_EVICTING) {
157 163 mutex_exit(DBUF_HASH_MUTEX(h, idx));
158 164 return (dbf);
159 165 }
160 166 mutex_exit(&dbf->db_mtx);
161 167 }
162 168 }
163 169
164 170 mutex_enter(&db->db_mtx);
165 171 db->db_hash_next = h->hash_table[idx];
166 172 h->hash_table[idx] = db;
167 173 mutex_exit(DBUF_HASH_MUTEX(h, idx));
168 174 atomic_add_64(&dbuf_hash_count, 1);
169 175
170 176 return (NULL);
171 177 }
172 178
173 179 /*
174 180 * Remove an entry from the hash table. This operation will
175 181 * fail if there are any existing holds on the db.
176 182 */
177 183 static void
178 184 dbuf_hash_remove(dmu_buf_impl_t *db)
179 185 {
180 186 dbuf_hash_table_t *h = &dbuf_hash_table;
181 187 uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
182 188 db->db_level, db->db_blkid);
183 189 uint64_t idx = hv & h->hash_table_mask;
184 190 dmu_buf_impl_t *dbf, **dbp;
185 191
186 192 /*
187 193 * We musn't hold db_mtx to maintin lock ordering:
188 194 * DBUF_HASH_MUTEX > db_mtx.
189 195 */
190 196 ASSERT(refcount_is_zero(&db->db_holds));
191 197 ASSERT(db->db_state == DB_EVICTING);
192 198 ASSERT(!MUTEX_HELD(&db->db_mtx));
193 199
194 200 mutex_enter(DBUF_HASH_MUTEX(h, idx));
195 201 dbp = &h->hash_table[idx];
196 202 while ((dbf = *dbp) != db) {
197 203 dbp = &dbf->db_hash_next;
198 204 ASSERT(dbf != NULL);
199 205 }
200 206 *dbp = db->db_hash_next;
201 207 db->db_hash_next = NULL;
202 208 mutex_exit(DBUF_HASH_MUTEX(h, idx));
203 209 atomic_add_64(&dbuf_hash_count, -1);
204 210 }
205 211
206 212 static arc_evict_func_t dbuf_do_evict;
207 213
208 214 static void
209 215 dbuf_evict_user(dmu_buf_impl_t *db)
210 216 {
211 217 ASSERT(MUTEX_HELD(&db->db_mtx));
212 218
213 219 if (db->db_level != 0 || db->db_evict_func == NULL)
214 220 return;
215 221
216 222 if (db->db_user_data_ptr_ptr)
217 223 *db->db_user_data_ptr_ptr = db->db.db_data;
218 224 db->db_evict_func(&db->db, db->db_user_ptr);
219 225 db->db_user_ptr = NULL;
220 226 db->db_user_data_ptr_ptr = NULL;
221 227 db->db_evict_func = NULL;
222 228 }
223 229
224 230 boolean_t
225 231 dbuf_is_metadata(dmu_buf_impl_t *db)
226 232 {
227 233 if (db->db_level > 0) {
228 234 return (B_TRUE);
229 235 } else {
230 236 boolean_t is_metadata;
231 237
232 238 DB_DNODE_ENTER(db);
233 239 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
234 240 DB_DNODE_EXIT(db);
235 241
236 242 return (is_metadata);
237 243 }
238 244 }
239 245
240 246 void
241 247 dbuf_evict(dmu_buf_impl_t *db)
242 248 {
243 249 ASSERT(MUTEX_HELD(&db->db_mtx));
244 250 ASSERT(db->db_buf == NULL);
245 251 ASSERT(db->db_data_pending == NULL);
246 252
247 253 dbuf_clear(db);
248 254 dbuf_destroy(db);
249 255 }
250 256
251 257 void
252 258 dbuf_init(void)
253 259 {
254 260 uint64_t hsize = 1ULL << 16;
255 261 dbuf_hash_table_t *h = &dbuf_hash_table;
256 262 int i;
257 263
258 264 /*
259 265 * The hash table is big enough to fill all of physical memory
260 266 * with an average 4K block size. The table will take up
261 267 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
262 268 */
263 269 while (hsize * 4096 < physmem * PAGESIZE)
264 270 hsize <<= 1;
265 271
266 272 retry:
267 273 h->hash_table_mask = hsize - 1;
268 274 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
269 275 if (h->hash_table == NULL) {
270 276 /* XXX - we should really return an error instead of assert */
271 277 ASSERT(hsize > (1ULL << 10));
272 278 hsize >>= 1;
273 279 goto retry;
274 280 }
275 281
276 282 dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
277 283 sizeof (dmu_buf_impl_t),
278 284 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
279 285
280 286 for (i = 0; i < DBUF_MUTEXES; i++)
281 287 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
282 288 }
283 289
284 290 void
285 291 dbuf_fini(void)
286 292 {
287 293 dbuf_hash_table_t *h = &dbuf_hash_table;
288 294 int i;
289 295
290 296 for (i = 0; i < DBUF_MUTEXES; i++)
291 297 mutex_destroy(&h->hash_mutexes[i]);
292 298 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
293 299 kmem_cache_destroy(dbuf_cache);
294 300 }
295 301
296 302 /*
297 303 * Other stuff.
298 304 */
299 305
300 306 #ifdef ZFS_DEBUG
301 307 static void
302 308 dbuf_verify(dmu_buf_impl_t *db)
303 309 {
304 310 dnode_t *dn;
305 311 dbuf_dirty_record_t *dr;
306 312
307 313 ASSERT(MUTEX_HELD(&db->db_mtx));
308 314
309 315 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
310 316 return;
311 317
312 318 ASSERT(db->db_objset != NULL);
313 319 DB_DNODE_ENTER(db);
314 320 dn = DB_DNODE(db);
315 321 if (dn == NULL) {
316 322 ASSERT(db->db_parent == NULL);
317 323 ASSERT(db->db_blkptr == NULL);
318 324 } else {
319 325 ASSERT3U(db->db.db_object, ==, dn->dn_object);
320 326 ASSERT3P(db->db_objset, ==, dn->dn_objset);
321 327 ASSERT3U(db->db_level, <, dn->dn_nlevels);
322 328 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
323 329 db->db_blkid == DMU_SPILL_BLKID ||
324 330 !list_is_empty(&dn->dn_dbufs));
325 331 }
326 332 if (db->db_blkid == DMU_BONUS_BLKID) {
327 333 ASSERT(dn != NULL);
328 334 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
329 335 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
330 336 } else if (db->db_blkid == DMU_SPILL_BLKID) {
331 337 ASSERT(dn != NULL);
332 338 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
333 339 ASSERT0(db->db.db_offset);
334 340 } else {
335 341 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
336 342 }
337 343
338 344 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next)
339 345 ASSERT(dr->dr_dbuf == db);
340 346
341 347 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next)
342 348 ASSERT(dr->dr_dbuf == db);
343 349
344 350 /*
345 351 * We can't assert that db_size matches dn_datablksz because it
346 352 * can be momentarily different when another thread is doing
347 353 * dnode_set_blksz().
348 354 */
349 355 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
350 356 dr = db->db_data_pending;
351 357 /*
352 358 * It should only be modified in syncing context, so
353 359 * make sure we only have one copy of the data.
354 360 */
355 361 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
356 362 }
357 363
358 364 /* verify db->db_blkptr */
359 365 if (db->db_blkptr) {
360 366 if (db->db_parent == dn->dn_dbuf) {
361 367 /* db is pointed to by the dnode */
362 368 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
363 369 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
364 370 ASSERT(db->db_parent == NULL);
365 371 else
366 372 ASSERT(db->db_parent != NULL);
367 373 if (db->db_blkid != DMU_SPILL_BLKID)
368 374 ASSERT3P(db->db_blkptr, ==,
369 375 &dn->dn_phys->dn_blkptr[db->db_blkid]);
370 376 } else {
371 377 /* db is pointed to by an indirect block */
372 378 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
373 379 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
374 380 ASSERT3U(db->db_parent->db.db_object, ==,
375 381 db->db.db_object);
376 382 /*
377 383 * dnode_grow_indblksz() can make this fail if we don't
378 384 * have the struct_rwlock. XXX indblksz no longer
379 385 * grows. safe to do this now?
380 386 */
381 387 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
382 388 ASSERT3P(db->db_blkptr, ==,
383 389 ((blkptr_t *)db->db_parent->db.db_data +
384 390 db->db_blkid % epb));
385 391 }
386 392 }
387 393 }
388 394 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
389 395 (db->db_buf == NULL || db->db_buf->b_data) &&
390 396 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
391 397 db->db_state != DB_FILL && !dn->dn_free_txg) {
392 398 /*
393 399 * If the blkptr isn't set but they have nonzero data,
394 400 * it had better be dirty, otherwise we'll lose that
395 401 * data when we evict this buffer.
396 402 */
397 403 if (db->db_dirtycnt == 0) {
398 404 uint64_t *buf = db->db.db_data;
399 405 int i;
400 406
401 407 for (i = 0; i < db->db.db_size >> 3; i++) {
402 408 ASSERT(buf[i] == 0);
403 409 }
404 410 }
405 411 }
406 412 DB_DNODE_EXIT(db);
407 413 }
408 414 #endif
409 415
410 416 static void
411 417 dbuf_update_data(dmu_buf_impl_t *db)
412 418 {
413 419 ASSERT(MUTEX_HELD(&db->db_mtx));
414 420 if (db->db_level == 0 && db->db_user_data_ptr_ptr) {
415 421 ASSERT(!refcount_is_zero(&db->db_holds));
416 422 *db->db_user_data_ptr_ptr = db->db.db_data;
417 423 }
418 424 }
419 425
420 426 static void
421 427 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
422 428 {
423 429 ASSERT(MUTEX_HELD(&db->db_mtx));
424 430 ASSERT(db->db_buf == NULL || !arc_has_callback(db->db_buf));
425 431 db->db_buf = buf;
426 432 if (buf != NULL) {
427 433 ASSERT(buf->b_data != NULL);
428 434 db->db.db_data = buf->b_data;
429 435 if (!arc_released(buf))
430 436 arc_set_callback(buf, dbuf_do_evict, db);
431 437 dbuf_update_data(db);
432 438 } else {
433 439 dbuf_evict_user(db);
434 440 db->db.db_data = NULL;
435 441 if (db->db_state != DB_NOFILL)
436 442 db->db_state = DB_UNCACHED;
437 443 }
438 444 }
439 445
440 446 /*
441 447 * Loan out an arc_buf for read. Return the loaned arc_buf.
442 448 */
443 449 arc_buf_t *
444 450 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
445 451 {
446 452 arc_buf_t *abuf;
447 453
448 454 mutex_enter(&db->db_mtx);
449 455 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) {
450 456 int blksz = db->db.db_size;
451 457 spa_t *spa;
452 458
453 459 mutex_exit(&db->db_mtx);
454 460 DB_GET_SPA(&spa, db);
455 461 abuf = arc_loan_buf(spa, blksz);
456 462 bcopy(db->db.db_data, abuf->b_data, blksz);
457 463 } else {
458 464 abuf = db->db_buf;
459 465 arc_loan_inuse_buf(abuf, db);
460 466 dbuf_set_data(db, NULL);
461 467 mutex_exit(&db->db_mtx);
462 468 }
463 469 return (abuf);
464 470 }
465 471
466 472 uint64_t
467 473 dbuf_whichblock(dnode_t *dn, uint64_t offset)
468 474 {
469 475 if (dn->dn_datablkshift) {
470 476 return (offset >> dn->dn_datablkshift);
471 477 } else {
472 478 ASSERT3U(offset, <, dn->dn_datablksz);
473 479 return (0);
474 480 }
475 481 }
476 482
477 483 static void
478 484 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
479 485 {
480 486 dmu_buf_impl_t *db = vdb;
481 487
482 488 mutex_enter(&db->db_mtx);
483 489 ASSERT3U(db->db_state, ==, DB_READ);
484 490 /*
485 491 * All reads are synchronous, so we must have a hold on the dbuf
486 492 */
487 493 ASSERT(refcount_count(&db->db_holds) > 0);
488 494 ASSERT(db->db_buf == NULL);
489 495 ASSERT(db->db.db_data == NULL);
490 496 if (db->db_level == 0 && db->db_freed_in_flight) {
491 497 /* we were freed in flight; disregard any error */
492 498 arc_release(buf, db);
493 499 bzero(buf->b_data, db->db.db_size);
494 500 arc_buf_freeze(buf);
495 501 db->db_freed_in_flight = FALSE;
496 502 dbuf_set_data(db, buf);
497 503 db->db_state = DB_CACHED;
498 504 } else if (zio == NULL || zio->io_error == 0) {
499 505 dbuf_set_data(db, buf);
500 506 db->db_state = DB_CACHED;
501 507 } else {
502 508 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
503 509 ASSERT3P(db->db_buf, ==, NULL);
504 510 VERIFY(arc_buf_remove_ref(buf, db));
505 511 db->db_state = DB_UNCACHED;
506 512 }
507 513 cv_broadcast(&db->db_changed);
508 514 dbuf_rele_and_unlock(db, NULL);
509 515 }
510 516
511 517 static void
512 518 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t *flags)
513 519 {
514 520 dnode_t *dn;
515 521 spa_t *spa;
516 522 zbookmark_t zb;
517 523 uint32_t aflags = ARC_NOWAIT;
518 524
519 525 DB_DNODE_ENTER(db);
520 526 dn = DB_DNODE(db);
521 527 ASSERT(!refcount_is_zero(&db->db_holds));
522 528 /* We need the struct_rwlock to prevent db_blkptr from changing. */
523 529 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
524 530 ASSERT(MUTEX_HELD(&db->db_mtx));
525 531 ASSERT(db->db_state == DB_UNCACHED);
526 532 ASSERT(db->db_buf == NULL);
527 533
528 534 if (db->db_blkid == DMU_BONUS_BLKID) {
529 535 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
530 536
531 537 ASSERT3U(bonuslen, <=, db->db.db_size);
532 538 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN);
533 539 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
534 540 if (bonuslen < DN_MAX_BONUSLEN)
535 541 bzero(db->db.db_data, DN_MAX_BONUSLEN);
536 542 if (bonuslen)
537 543 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
538 544 DB_DNODE_EXIT(db);
539 545 dbuf_update_data(db);
540 546 db->db_state = DB_CACHED;
541 547 mutex_exit(&db->db_mtx);
542 548 return;
543 549 }
544 550
545 551 /*
546 552 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
547 553 * processes the delete record and clears the bp while we are waiting
548 554 * for the dn_mtx (resulting in a "no" from block_freed).
549 555 */
550 556 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) ||
551 557 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) ||
552 558 BP_IS_HOLE(db->db_blkptr)))) {
553 559 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
554 560
555 561 dbuf_set_data(db, arc_buf_alloc(dn->dn_objset->os_spa,
556 562 db->db.db_size, db, type));
557 563 DB_DNODE_EXIT(db);
558 564 bzero(db->db.db_data, db->db.db_size);
559 565 db->db_state = DB_CACHED;
560 566 *flags |= DB_RF_CACHED;
561 567 mutex_exit(&db->db_mtx);
562 568 return;
563 569 }
564 570
565 571 spa = dn->dn_objset->os_spa;
566 572 DB_DNODE_EXIT(db);
567 573
568 574 db->db_state = DB_READ;
569 575 mutex_exit(&db->db_mtx);
570 576
571 577 if (DBUF_IS_L2CACHEABLE(db))
572 578 aflags |= ARC_L2CACHE;
573 579 if (DBUF_IS_L2COMPRESSIBLE(db))
574 580 aflags |= ARC_L2COMPRESS;
575 581
576 582 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ?
577 583 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET,
578 584 db->db.db_object, db->db_level, db->db_blkid);
579 585
580 586 dbuf_add_ref(db, NULL);
581 587
582 588 (void) arc_read(zio, spa, db->db_blkptr,
583 589 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
584 590 (*flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
585 591 &aflags, &zb);
586 592 if (aflags & ARC_CACHED)
587 593 *flags |= DB_RF_CACHED;
588 594 }
589 595
590 596 int
591 597 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
592 598 {
593 599 int err = 0;
594 600 int havepzio = (zio != NULL);
595 601 int prefetch;
596 602 dnode_t *dn;
597 603
598 604 /*
599 605 * We don't have to hold the mutex to check db_state because it
600 606 * can't be freed while we have a hold on the buffer.
601 607 */
602 608 ASSERT(!refcount_is_zero(&db->db_holds));
603 609
604 610 if (db->db_state == DB_NOFILL)
605 611 return (SET_ERROR(EIO));
606 612
607 613 DB_DNODE_ENTER(db);
608 614 dn = DB_DNODE(db);
609 615 if ((flags & DB_RF_HAVESTRUCT) == 0)
610 616 rw_enter(&dn->dn_struct_rwlock, RW_READER);
611 617
612 618 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
613 619 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
614 620 DBUF_IS_CACHEABLE(db);
615 621
616 622 mutex_enter(&db->db_mtx);
617 623 if (db->db_state == DB_CACHED) {
618 624 mutex_exit(&db->db_mtx);
619 625 if (prefetch)
620 626 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
621 627 db->db.db_size, TRUE);
622 628 if ((flags & DB_RF_HAVESTRUCT) == 0)
623 629 rw_exit(&dn->dn_struct_rwlock);
624 630 DB_DNODE_EXIT(db);
625 631 } else if (db->db_state == DB_UNCACHED) {
626 632 spa_t *spa = dn->dn_objset->os_spa;
627 633
628 634 if (zio == NULL)
629 635 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
630 636 dbuf_read_impl(db, zio, &flags);
631 637
632 638 /* dbuf_read_impl has dropped db_mtx for us */
633 639
634 640 if (prefetch)
635 641 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
636 642 db->db.db_size, flags & DB_RF_CACHED);
637 643
638 644 if ((flags & DB_RF_HAVESTRUCT) == 0)
639 645 rw_exit(&dn->dn_struct_rwlock);
640 646 DB_DNODE_EXIT(db);
641 647
642 648 if (!havepzio)
643 649 err = zio_wait(zio);
644 650 } else {
645 651 /*
646 652 * Another reader came in while the dbuf was in flight
647 653 * between UNCACHED and CACHED. Either a writer will finish
648 654 * writing the buffer (sending the dbuf to CACHED) or the
649 655 * first reader's request will reach the read_done callback
650 656 * and send the dbuf to CACHED. Otherwise, a failure
651 657 * occurred and the dbuf went to UNCACHED.
652 658 */
653 659 mutex_exit(&db->db_mtx);
654 660 if (prefetch)
655 661 dmu_zfetch(&dn->dn_zfetch, db->db.db_offset,
656 662 db->db.db_size, TRUE);
657 663 if ((flags & DB_RF_HAVESTRUCT) == 0)
658 664 rw_exit(&dn->dn_struct_rwlock);
659 665 DB_DNODE_EXIT(db);
660 666
661 667 /* Skip the wait per the caller's request. */
662 668 mutex_enter(&db->db_mtx);
663 669 if ((flags & DB_RF_NEVERWAIT) == 0) {
664 670 while (db->db_state == DB_READ ||
665 671 db->db_state == DB_FILL) {
666 672 ASSERT(db->db_state == DB_READ ||
667 673 (flags & DB_RF_HAVESTRUCT) == 0);
668 674 cv_wait(&db->db_changed, &db->db_mtx);
669 675 }
670 676 if (db->db_state == DB_UNCACHED)
671 677 err = SET_ERROR(EIO);
672 678 }
673 679 mutex_exit(&db->db_mtx);
674 680 }
675 681
676 682 ASSERT(err || havepzio || db->db_state == DB_CACHED);
677 683 return (err);
678 684 }
679 685
680 686 static void
681 687 dbuf_noread(dmu_buf_impl_t *db)
682 688 {
683 689 ASSERT(!refcount_is_zero(&db->db_holds));
684 690 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
685 691 mutex_enter(&db->db_mtx);
686 692 while (db->db_state == DB_READ || db->db_state == DB_FILL)
687 693 cv_wait(&db->db_changed, &db->db_mtx);
688 694 if (db->db_state == DB_UNCACHED) {
689 695 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
690 696 spa_t *spa;
691 697
692 698 ASSERT(db->db_buf == NULL);
693 699 ASSERT(db->db.db_data == NULL);
694 700 DB_GET_SPA(&spa, db);
695 701 dbuf_set_data(db, arc_buf_alloc(spa, db->db.db_size, db, type));
696 702 db->db_state = DB_FILL;
697 703 } else if (db->db_state == DB_NOFILL) {
698 704 dbuf_set_data(db, NULL);
699 705 } else {
700 706 ASSERT3U(db->db_state, ==, DB_CACHED);
701 707 }
702 708 mutex_exit(&db->db_mtx);
703 709 }
704 710
705 711 /*
706 712 * This is our just-in-time copy function. It makes a copy of
707 713 * buffers, that have been modified in a previous transaction
708 714 * group, before we modify them in the current active group.
709 715 *
710 716 * This function is used in two places: when we are dirtying a
711 717 * buffer for the first time in a txg, and when we are freeing
712 718 * a range in a dnode that includes this buffer.
713 719 *
714 720 * Note that when we are called from dbuf_free_range() we do
715 721 * not put a hold on the buffer, we just traverse the active
716 722 * dbuf list for the dnode.
717 723 */
718 724 static void
719 725 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
720 726 {
721 727 dbuf_dirty_record_t *dr = db->db_last_dirty;
722 728
723 729 ASSERT(MUTEX_HELD(&db->db_mtx));
724 730 ASSERT(db->db.db_data != NULL);
725 731 ASSERT(db->db_level == 0);
726 732 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
727 733
728 734 if (dr == NULL ||
729 735 (dr->dt.dl.dr_data !=
730 736 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
731 737 return;
732 738
733 739 /*
734 740 * If the last dirty record for this dbuf has not yet synced
735 741 * and its referencing the dbuf data, either:
736 742 * reset the reference to point to a new copy,
737 743 * or (if there a no active holders)
738 744 * just null out the current db_data pointer.
739 745 */
740 746 ASSERT(dr->dr_txg >= txg - 2);
741 747 if (db->db_blkid == DMU_BONUS_BLKID) {
742 748 /* Note that the data bufs here are zio_bufs */
743 749 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN);
744 750 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
745 751 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN);
746 752 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
747 753 int size = db->db.db_size;
748 754 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
749 755 spa_t *spa;
750 756
751 757 DB_GET_SPA(&spa, db);
752 758 dr->dt.dl.dr_data = arc_buf_alloc(spa, size, db, type);
753 759 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size);
754 760 } else {
755 761 dbuf_set_data(db, NULL);
756 762 }
757 763 }
758 764
759 765 void
760 766 dbuf_unoverride(dbuf_dirty_record_t *dr)
761 767 {
762 768 dmu_buf_impl_t *db = dr->dr_dbuf;
763 769 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
764 770 uint64_t txg = dr->dr_txg;
765 771
766 772 ASSERT(MUTEX_HELD(&db->db_mtx));
767 773 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
768 774 ASSERT(db->db_level == 0);
769 775
770 776 if (db->db_blkid == DMU_BONUS_BLKID ||
771 777 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
772 778 return;
773 779
774 780 ASSERT(db->db_data_pending != dr);
775 781
776 782 /* free this block */
777 783 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite) {
778 784 spa_t *spa;
779 785
780 786 DB_GET_SPA(&spa, db);
781 787 zio_free(spa, txg, bp);
782 788 }
783 789 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
784 790 dr->dt.dl.dr_nopwrite = B_FALSE;
785 791
786 792 /*
787 793 * Release the already-written buffer, so we leave it in
788 794 * a consistent dirty state. Note that all callers are
789 795 * modifying the buffer, so they will immediately do
790 796 * another (redundant) arc_release(). Therefore, leave
791 797 * the buf thawed to save the effort of freezing &
792 798 * immediately re-thawing it.
793 799 */
794 800 arc_release(dr->dt.dl.dr_data, db);
795 801 }
796 802
797 803 /*
798 804 * Evict (if its unreferenced) or clear (if its referenced) any level-0
799 805 * data blocks in the free range, so that any future readers will find
800 806 * empty blocks. Also, if we happen across any level-1 dbufs in the
801 807 * range that have not already been marked dirty, mark them dirty so
802 808 * they stay in memory.
803 809 *
804 810 * This is a no-op if the dataset is in the middle of an incremental
805 811 * receive; see comment below for details.
806 812 */
807 813 void
808 814 dbuf_free_range(dnode_t *dn, uint64_t start, uint64_t end, dmu_tx_t *tx)
809 815 {
810 816 dmu_buf_impl_t *db, *db_next;
811 817 uint64_t txg = tx->tx_txg;
↓ open down ↓ |
759 lines elided |
↑ open up ↑ |
812 818 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
813 819 uint64_t first_l1 = start >> epbs;
814 820 uint64_t last_l1 = end >> epbs;
815 821
816 822 if (end > dn->dn_maxblkid && (end != DMU_SPILL_BLKID)) {
817 823 end = dn->dn_maxblkid;
818 824 last_l1 = end >> epbs;
819 825 }
820 826 dprintf_dnode(dn, "start=%llu end=%llu\n", start, end);
821 827
822 - if (dmu_objset_is_receiving(dn->dn_objset)) {
828 + mutex_enter(&dn->dn_dbufs_mtx);
829 + if (start >= dn->dn_unlisted_l0_blkid * dn->dn_datablksz) {
830 + /* There can't be any dbufs in this range; no need to search. */
831 + mutex_exit(&dn->dn_dbufs_mtx);
832 + return;
833 + } else if (dmu_objset_is_receiving(dn->dn_objset)) {
823 834 /*
824 - * When processing a free record from a zfs receive,
825 - * there should have been no previous modifications to the
826 - * data in this range. Therefore there should be no dbufs
827 - * in the range. Searching dn_dbufs for these non-existent
828 - * dbufs can be very expensive, so simply ignore this.
835 + * If we are receiving, we expect there to be no dbufs in
836 + * the range to be freed, because receive modifies each
837 + * block at most once, and in offset order. If this is
838 + * not the case, it can lead to performance problems,
839 + * so note that we unexpectedly took the slow path.
829 840 */
830 - VERIFY3P(dbuf_find(dn, 0, start), ==, NULL);
831 - VERIFY3P(dbuf_find(dn, 0, end), ==, NULL);
832 - return;
841 + atomic_inc_64(&zfs_free_range_recv_miss);
833 842 }
834 843
835 - mutex_enter(&dn->dn_dbufs_mtx);
836 844 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
837 845 db_next = list_next(&dn->dn_dbufs, db);
838 846 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
839 847
840 848 if (db->db_level == 1 &&
841 849 db->db_blkid >= first_l1 && db->db_blkid <= last_l1) {
842 850 mutex_enter(&db->db_mtx);
843 851 if (db->db_last_dirty &&
844 852 db->db_last_dirty->dr_txg < txg) {
845 853 dbuf_add_ref(db, FTAG);
846 854 mutex_exit(&db->db_mtx);
847 855 dbuf_will_dirty(db, tx);
848 856 dbuf_rele(db, FTAG);
849 857 } else {
850 858 mutex_exit(&db->db_mtx);
851 859 }
852 860 }
853 861
854 862 if (db->db_level != 0)
855 863 continue;
856 864 dprintf_dbuf(db, "found buf %s\n", "");
857 865 if (db->db_blkid < start || db->db_blkid > end)
858 866 continue;
859 867
860 868 /* found a level 0 buffer in the range */
861 869 mutex_enter(&db->db_mtx);
862 870 if (dbuf_undirty(db, tx)) {
863 871 /* mutex has been dropped and dbuf destroyed */
864 872 continue;
865 873 }
866 874
867 875 if (db->db_state == DB_UNCACHED ||
868 876 db->db_state == DB_NOFILL ||
869 877 db->db_state == DB_EVICTING) {
870 878 ASSERT(db->db.db_data == NULL);
871 879 mutex_exit(&db->db_mtx);
872 880 continue;
873 881 }
874 882 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
875 883 /* will be handled in dbuf_read_done or dbuf_rele */
876 884 db->db_freed_in_flight = TRUE;
877 885 mutex_exit(&db->db_mtx);
878 886 continue;
879 887 }
880 888 if (refcount_count(&db->db_holds) == 0) {
881 889 ASSERT(db->db_buf);
882 890 dbuf_clear(db);
883 891 continue;
884 892 }
885 893 /* The dbuf is referenced */
886 894
887 895 if (db->db_last_dirty != NULL) {
888 896 dbuf_dirty_record_t *dr = db->db_last_dirty;
889 897
890 898 if (dr->dr_txg == txg) {
891 899 /*
892 900 * This buffer is "in-use", re-adjust the file
893 901 * size to reflect that this buffer may
894 902 * contain new data when we sync.
895 903 */
896 904 if (db->db_blkid != DMU_SPILL_BLKID &&
897 905 db->db_blkid > dn->dn_maxblkid)
898 906 dn->dn_maxblkid = db->db_blkid;
899 907 dbuf_unoverride(dr);
900 908 } else {
901 909 /*
902 910 * This dbuf is not dirty in the open context.
903 911 * Either uncache it (if its not referenced in
904 912 * the open context) or reset its contents to
905 913 * empty.
906 914 */
907 915 dbuf_fix_old_data(db, txg);
908 916 }
909 917 }
910 918 /* clear the contents if its cached */
911 919 if (db->db_state == DB_CACHED) {
912 920 ASSERT(db->db.db_data != NULL);
913 921 arc_release(db->db_buf, db);
914 922 bzero(db->db.db_data, db->db.db_size);
915 923 arc_buf_freeze(db->db_buf);
916 924 }
917 925
918 926 mutex_exit(&db->db_mtx);
919 927 }
920 928 mutex_exit(&dn->dn_dbufs_mtx);
921 929 }
922 930
923 931 static int
924 932 dbuf_block_freeable(dmu_buf_impl_t *db)
925 933 {
926 934 dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
927 935 uint64_t birth_txg = 0;
928 936
929 937 /*
930 938 * We don't need any locking to protect db_blkptr:
931 939 * If it's syncing, then db_last_dirty will be set
932 940 * so we'll ignore db_blkptr.
933 941 */
934 942 ASSERT(MUTEX_HELD(&db->db_mtx));
935 943 if (db->db_last_dirty)
936 944 birth_txg = db->db_last_dirty->dr_txg;
937 945 else if (db->db_blkptr)
938 946 birth_txg = db->db_blkptr->blk_birth;
939 947
940 948 /*
941 949 * If we don't exist or are in a snapshot, we can't be freed.
942 950 * Don't pass the bp to dsl_dataset_block_freeable() since we
943 951 * are holding the db_mtx lock and might deadlock if we are
944 952 * prefetching a dedup-ed block.
945 953 */
946 954 if (birth_txg)
947 955 return (ds == NULL ||
948 956 dsl_dataset_block_freeable(ds, NULL, birth_txg));
949 957 else
950 958 return (FALSE);
951 959 }
952 960
953 961 void
954 962 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
955 963 {
956 964 arc_buf_t *buf, *obuf;
957 965 int osize = db->db.db_size;
958 966 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
959 967 dnode_t *dn;
960 968
961 969 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
962 970
963 971 DB_DNODE_ENTER(db);
964 972 dn = DB_DNODE(db);
965 973
966 974 /* XXX does *this* func really need the lock? */
967 975 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
968 976
969 977 /*
970 978 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
971 979 * is OK, because there can be no other references to the db
972 980 * when we are changing its size, so no concurrent DB_FILL can
973 981 * be happening.
974 982 */
975 983 /*
976 984 * XXX we should be doing a dbuf_read, checking the return
977 985 * value and returning that up to our callers
978 986 */
979 987 dbuf_will_dirty(db, tx);
980 988
981 989 /* create the data buffer for the new block */
982 990 buf = arc_buf_alloc(dn->dn_objset->os_spa, size, db, type);
983 991
984 992 /* copy old block data to the new block */
985 993 obuf = db->db_buf;
986 994 bcopy(obuf->b_data, buf->b_data, MIN(osize, size));
987 995 /* zero the remainder */
988 996 if (size > osize)
989 997 bzero((uint8_t *)buf->b_data + osize, size - osize);
990 998
991 999 mutex_enter(&db->db_mtx);
992 1000 dbuf_set_data(db, buf);
993 1001 VERIFY(arc_buf_remove_ref(obuf, db));
994 1002 db->db.db_size = size;
995 1003
996 1004 if (db->db_level == 0) {
997 1005 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg);
998 1006 db->db_last_dirty->dt.dl.dr_data = buf;
999 1007 }
1000 1008 mutex_exit(&db->db_mtx);
1001 1009
1002 1010 dnode_willuse_space(dn, size-osize, tx);
1003 1011 DB_DNODE_EXIT(db);
1004 1012 }
1005 1013
1006 1014 void
1007 1015 dbuf_release_bp(dmu_buf_impl_t *db)
1008 1016 {
1009 1017 objset_t *os;
1010 1018
1011 1019 DB_GET_OBJSET(&os, db);
1012 1020 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1013 1021 ASSERT(arc_released(os->os_phys_buf) ||
1014 1022 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1015 1023 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1016 1024
1017 1025 (void) arc_release(db->db_buf, db);
1018 1026 }
1019 1027
1020 1028 dbuf_dirty_record_t *
1021 1029 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1022 1030 {
1023 1031 dnode_t *dn;
1024 1032 objset_t *os;
1025 1033 dbuf_dirty_record_t **drp, *dr;
1026 1034 int drop_struct_lock = FALSE;
1027 1035 boolean_t do_free_accounting = B_FALSE;
1028 1036 int txgoff = tx->tx_txg & TXG_MASK;
1029 1037
1030 1038 ASSERT(tx->tx_txg != 0);
1031 1039 ASSERT(!refcount_is_zero(&db->db_holds));
1032 1040 DMU_TX_DIRTY_BUF(tx, db);
1033 1041
1034 1042 DB_DNODE_ENTER(db);
1035 1043 dn = DB_DNODE(db);
1036 1044 /*
1037 1045 * Shouldn't dirty a regular buffer in syncing context. Private
1038 1046 * objects may be dirtied in syncing context, but only if they
1039 1047 * were already pre-dirtied in open context.
1040 1048 */
1041 1049 ASSERT(!dmu_tx_is_syncing(tx) ||
1042 1050 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
1043 1051 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1044 1052 dn->dn_objset->os_dsl_dataset == NULL);
1045 1053 /*
1046 1054 * We make this assert for private objects as well, but after we
1047 1055 * check if we're already dirty. They are allowed to re-dirty
1048 1056 * in syncing context.
1049 1057 */
1050 1058 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1051 1059 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1052 1060 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1053 1061
1054 1062 mutex_enter(&db->db_mtx);
1055 1063 /*
1056 1064 * XXX make this true for indirects too? The problem is that
1057 1065 * transactions created with dmu_tx_create_assigned() from
1058 1066 * syncing context don't bother holding ahead.
1059 1067 */
1060 1068 ASSERT(db->db_level != 0 ||
1061 1069 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
1062 1070 db->db_state == DB_NOFILL);
1063 1071
1064 1072 mutex_enter(&dn->dn_mtx);
1065 1073 /*
1066 1074 * Don't set dirtyctx to SYNC if we're just modifying this as we
1067 1075 * initialize the objset.
1068 1076 */
1069 1077 if (dn->dn_dirtyctx == DN_UNDIRTIED &&
1070 1078 !BP_IS_HOLE(dn->dn_objset->os_rootbp)) {
1071 1079 dn->dn_dirtyctx =
1072 1080 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
1073 1081 ASSERT(dn->dn_dirtyctx_firstset == NULL);
1074 1082 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
1075 1083 }
1076 1084 mutex_exit(&dn->dn_mtx);
1077 1085
1078 1086 if (db->db_blkid == DMU_SPILL_BLKID)
1079 1087 dn->dn_have_spill = B_TRUE;
1080 1088
1081 1089 /*
1082 1090 * If this buffer is already dirty, we're done.
1083 1091 */
1084 1092 drp = &db->db_last_dirty;
1085 1093 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg ||
1086 1094 db->db.db_object == DMU_META_DNODE_OBJECT);
1087 1095 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg)
1088 1096 drp = &dr->dr_next;
1089 1097 if (dr && dr->dr_txg == tx->tx_txg) {
1090 1098 DB_DNODE_EXIT(db);
1091 1099
1092 1100 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1093 1101 /*
1094 1102 * If this buffer has already been written out,
1095 1103 * we now need to reset its state.
1096 1104 */
1097 1105 dbuf_unoverride(dr);
1098 1106 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1099 1107 db->db_state != DB_NOFILL)
1100 1108 arc_buf_thaw(db->db_buf);
1101 1109 }
1102 1110 mutex_exit(&db->db_mtx);
1103 1111 return (dr);
1104 1112 }
1105 1113
1106 1114 /*
1107 1115 * Only valid if not already dirty.
1108 1116 */
1109 1117 ASSERT(dn->dn_object == 0 ||
1110 1118 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
1111 1119 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
1112 1120
1113 1121 ASSERT3U(dn->dn_nlevels, >, db->db_level);
1114 1122 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
1115 1123 dn->dn_phys->dn_nlevels > db->db_level ||
1116 1124 dn->dn_next_nlevels[txgoff] > db->db_level ||
1117 1125 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
1118 1126 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
1119 1127
1120 1128 /*
1121 1129 * We should only be dirtying in syncing context if it's the
1122 1130 * mos or we're initializing the os or it's a special object.
1123 1131 * However, we are allowed to dirty in syncing context provided
1124 1132 * we already dirtied it in open context. Hence we must make
1125 1133 * this assertion only if we're not already dirty.
1126 1134 */
1127 1135 os = dn->dn_objset;
1128 1136 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
1129 1137 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
1130 1138 ASSERT(db->db.db_size != 0);
1131 1139
1132 1140 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1133 1141
1134 1142 if (db->db_blkid != DMU_BONUS_BLKID) {
1135 1143 /*
1136 1144 * Update the accounting.
1137 1145 * Note: we delay "free accounting" until after we drop
1138 1146 * the db_mtx. This keeps us from grabbing other locks
1139 1147 * (and possibly deadlocking) in bp_get_dsize() while
1140 1148 * also holding the db_mtx.
1141 1149 */
1142 1150 dnode_willuse_space(dn, db->db.db_size, tx);
1143 1151 do_free_accounting = dbuf_block_freeable(db);
1144 1152 }
1145 1153
1146 1154 /*
1147 1155 * If this buffer is dirty in an old transaction group we need
1148 1156 * to make a copy of it so that the changes we make in this
1149 1157 * transaction group won't leak out when we sync the older txg.
1150 1158 */
1151 1159 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
1152 1160 if (db->db_level == 0) {
1153 1161 void *data_old = db->db_buf;
1154 1162
1155 1163 if (db->db_state != DB_NOFILL) {
1156 1164 if (db->db_blkid == DMU_BONUS_BLKID) {
1157 1165 dbuf_fix_old_data(db, tx->tx_txg);
1158 1166 data_old = db->db.db_data;
1159 1167 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
1160 1168 /*
1161 1169 * Release the data buffer from the cache so
1162 1170 * that we can modify it without impacting
1163 1171 * possible other users of this cached data
1164 1172 * block. Note that indirect blocks and
1165 1173 * private objects are not released until the
1166 1174 * syncing state (since they are only modified
1167 1175 * then).
1168 1176 */
1169 1177 arc_release(db->db_buf, db);
1170 1178 dbuf_fix_old_data(db, tx->tx_txg);
1171 1179 data_old = db->db_buf;
1172 1180 }
1173 1181 ASSERT(data_old != NULL);
1174 1182 }
1175 1183 dr->dt.dl.dr_data = data_old;
1176 1184 } else {
1177 1185 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL);
1178 1186 list_create(&dr->dt.di.dr_children,
1179 1187 sizeof (dbuf_dirty_record_t),
1180 1188 offsetof(dbuf_dirty_record_t, dr_dirty_node));
1181 1189 }
1182 1190 dr->dr_dbuf = db;
1183 1191 dr->dr_txg = tx->tx_txg;
1184 1192 dr->dr_next = *drp;
1185 1193 *drp = dr;
1186 1194
1187 1195 /*
1188 1196 * We could have been freed_in_flight between the dbuf_noread
1189 1197 * and dbuf_dirty. We win, as though the dbuf_noread() had
1190 1198 * happened after the free.
1191 1199 */
1192 1200 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1193 1201 db->db_blkid != DMU_SPILL_BLKID) {
1194 1202 mutex_enter(&dn->dn_mtx);
1195 1203 dnode_clear_range(dn, db->db_blkid, 1, tx);
1196 1204 mutex_exit(&dn->dn_mtx);
1197 1205 db->db_freed_in_flight = FALSE;
1198 1206 }
1199 1207
1200 1208 /*
1201 1209 * This buffer is now part of this txg
1202 1210 */
1203 1211 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
1204 1212 db->db_dirtycnt += 1;
1205 1213 ASSERT3U(db->db_dirtycnt, <=, 3);
1206 1214
1207 1215 mutex_exit(&db->db_mtx);
1208 1216
1209 1217 if (db->db_blkid == DMU_BONUS_BLKID ||
1210 1218 db->db_blkid == DMU_SPILL_BLKID) {
1211 1219 mutex_enter(&dn->dn_mtx);
1212 1220 ASSERT(!list_link_active(&dr->dr_dirty_node));
1213 1221 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1214 1222 mutex_exit(&dn->dn_mtx);
1215 1223 dnode_setdirty(dn, tx);
1216 1224 DB_DNODE_EXIT(db);
1217 1225 return (dr);
1218 1226 } else if (do_free_accounting) {
1219 1227 blkptr_t *bp = db->db_blkptr;
1220 1228 int64_t willfree = (bp && !BP_IS_HOLE(bp)) ?
1221 1229 bp_get_dsize(os->os_spa, bp) : db->db.db_size;
1222 1230 /*
1223 1231 * This is only a guess -- if the dbuf is dirty
1224 1232 * in a previous txg, we don't know how much
1225 1233 * space it will use on disk yet. We should
1226 1234 * really have the struct_rwlock to access
1227 1235 * db_blkptr, but since this is just a guess,
1228 1236 * it's OK if we get an odd answer.
1229 1237 */
1230 1238 ddt_prefetch(os->os_spa, bp);
1231 1239 dnode_willuse_space(dn, -willfree, tx);
1232 1240 }
1233 1241
1234 1242 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
1235 1243 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1236 1244 drop_struct_lock = TRUE;
1237 1245 }
1238 1246
1239 1247 if (db->db_level == 0) {
1240 1248 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock);
1241 1249 ASSERT(dn->dn_maxblkid >= db->db_blkid);
1242 1250 }
1243 1251
1244 1252 if (db->db_level+1 < dn->dn_nlevels) {
1245 1253 dmu_buf_impl_t *parent = db->db_parent;
1246 1254 dbuf_dirty_record_t *di;
1247 1255 int parent_held = FALSE;
1248 1256
1249 1257 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
1250 1258 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1251 1259
1252 1260 parent = dbuf_hold_level(dn, db->db_level+1,
1253 1261 db->db_blkid >> epbs, FTAG);
1254 1262 ASSERT(parent != NULL);
1255 1263 parent_held = TRUE;
1256 1264 }
1257 1265 if (drop_struct_lock)
1258 1266 rw_exit(&dn->dn_struct_rwlock);
1259 1267 ASSERT3U(db->db_level+1, ==, parent->db_level);
1260 1268 di = dbuf_dirty(parent, tx);
1261 1269 if (parent_held)
1262 1270 dbuf_rele(parent, FTAG);
1263 1271
1264 1272 mutex_enter(&db->db_mtx);
1265 1273 /* possible race with dbuf_undirty() */
1266 1274 if (db->db_last_dirty == dr ||
1267 1275 dn->dn_object == DMU_META_DNODE_OBJECT) {
1268 1276 mutex_enter(&di->dt.di.dr_mtx);
1269 1277 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
1270 1278 ASSERT(!list_link_active(&dr->dr_dirty_node));
1271 1279 list_insert_tail(&di->dt.di.dr_children, dr);
1272 1280 mutex_exit(&di->dt.di.dr_mtx);
1273 1281 dr->dr_parent = di;
1274 1282 }
1275 1283 mutex_exit(&db->db_mtx);
1276 1284 } else {
1277 1285 ASSERT(db->db_level+1 == dn->dn_nlevels);
1278 1286 ASSERT(db->db_blkid < dn->dn_nblkptr);
1279 1287 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
1280 1288 mutex_enter(&dn->dn_mtx);
1281 1289 ASSERT(!list_link_active(&dr->dr_dirty_node));
1282 1290 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
1283 1291 mutex_exit(&dn->dn_mtx);
1284 1292 if (drop_struct_lock)
1285 1293 rw_exit(&dn->dn_struct_rwlock);
1286 1294 }
1287 1295
1288 1296 dnode_setdirty(dn, tx);
1289 1297 DB_DNODE_EXIT(db);
1290 1298 return (dr);
1291 1299 }
1292 1300
1293 1301 /*
1294 1302 * Undirty a buffer in the transaction group referenced by the given
1295 1303 * transaction. Return whether this evicted the dbuf.
1296 1304 */
1297 1305 static boolean_t
1298 1306 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1299 1307 {
1300 1308 dnode_t *dn;
1301 1309 uint64_t txg = tx->tx_txg;
1302 1310 dbuf_dirty_record_t *dr, **drp;
1303 1311
1304 1312 ASSERT(txg != 0);
1305 1313 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1306 1314 ASSERT0(db->db_level);
1307 1315 ASSERT(MUTEX_HELD(&db->db_mtx));
1308 1316
1309 1317 /*
1310 1318 * If this buffer is not dirty, we're done.
1311 1319 */
1312 1320 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1313 1321 if (dr->dr_txg <= txg)
1314 1322 break;
1315 1323 if (dr == NULL || dr->dr_txg < txg)
1316 1324 return (B_FALSE);
1317 1325 ASSERT(dr->dr_txg == txg);
1318 1326 ASSERT(dr->dr_dbuf == db);
1319 1327
1320 1328 DB_DNODE_ENTER(db);
1321 1329 dn = DB_DNODE(db);
1322 1330
1323 1331 /*
1324 1332 * Note: This code will probably work even if there are concurrent
1325 1333 * holders, but it is untested in that scenerio, as the ZPL and
1326 1334 * ztest have additional locking (the range locks) that prevents
1327 1335 * that type of concurrent access.
1328 1336 */
1329 1337 ASSERT3U(refcount_count(&db->db_holds), ==, db->db_dirtycnt);
1330 1338
1331 1339 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
1332 1340
1333 1341 ASSERT(db->db.db_size != 0);
1334 1342
1335 1343 /* XXX would be nice to fix up dn_towrite_space[] */
1336 1344
1337 1345 *drp = dr->dr_next;
1338 1346
1339 1347 /*
1340 1348 * Note that there are three places in dbuf_dirty()
1341 1349 * where this dirty record may be put on a list.
1342 1350 * Make sure to do a list_remove corresponding to
1343 1351 * every one of those list_insert calls.
1344 1352 */
1345 1353 if (dr->dr_parent) {
1346 1354 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
1347 1355 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
1348 1356 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
1349 1357 } else if (db->db_blkid == DMU_SPILL_BLKID ||
1350 1358 db->db_level+1 == dn->dn_nlevels) {
1351 1359 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
1352 1360 mutex_enter(&dn->dn_mtx);
1353 1361 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
1354 1362 mutex_exit(&dn->dn_mtx);
1355 1363 }
1356 1364 DB_DNODE_EXIT(db);
1357 1365
1358 1366 if (db->db_state != DB_NOFILL) {
1359 1367 dbuf_unoverride(dr);
1360 1368
1361 1369 ASSERT(db->db_buf != NULL);
1362 1370 ASSERT(dr->dt.dl.dr_data != NULL);
1363 1371 if (dr->dt.dl.dr_data != db->db_buf)
1364 1372 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data, db));
1365 1373 }
1366 1374 kmem_free(dr, sizeof (dbuf_dirty_record_t));
1367 1375
1368 1376 ASSERT(db->db_dirtycnt > 0);
1369 1377 db->db_dirtycnt -= 1;
1370 1378
1371 1379 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
1372 1380 arc_buf_t *buf = db->db_buf;
1373 1381
1374 1382 ASSERT(db->db_state == DB_NOFILL || arc_released(buf));
1375 1383 dbuf_set_data(db, NULL);
1376 1384 VERIFY(arc_buf_remove_ref(buf, db));
1377 1385 dbuf_evict(db);
1378 1386 return (B_TRUE);
1379 1387 }
1380 1388
1381 1389 return (B_FALSE);
1382 1390 }
1383 1391
1384 1392 #pragma weak dmu_buf_will_dirty = dbuf_will_dirty
1385 1393 void
1386 1394 dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1387 1395 {
1388 1396 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH;
1389 1397
1390 1398 ASSERT(tx->tx_txg != 0);
1391 1399 ASSERT(!refcount_is_zero(&db->db_holds));
1392 1400
1393 1401 DB_DNODE_ENTER(db);
1394 1402 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
1395 1403 rf |= DB_RF_HAVESTRUCT;
1396 1404 DB_DNODE_EXIT(db);
1397 1405 (void) dbuf_read(db, NULL, rf);
1398 1406 (void) dbuf_dirty(db, tx);
1399 1407 }
1400 1408
1401 1409 void
1402 1410 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1403 1411 {
1404 1412 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1405 1413
1406 1414 db->db_state = DB_NOFILL;
1407 1415
1408 1416 dmu_buf_will_fill(db_fake, tx);
1409 1417 }
1410 1418
1411 1419 void
1412 1420 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
1413 1421 {
1414 1422 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1415 1423
1416 1424 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1417 1425 ASSERT(tx->tx_txg != 0);
1418 1426 ASSERT(db->db_level == 0);
1419 1427 ASSERT(!refcount_is_zero(&db->db_holds));
1420 1428
1421 1429 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
1422 1430 dmu_tx_private_ok(tx));
1423 1431
1424 1432 dbuf_noread(db);
1425 1433 (void) dbuf_dirty(db, tx);
1426 1434 }
1427 1435
1428 1436 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1429 1437 /* ARGSUSED */
1430 1438 void
1431 1439 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
1432 1440 {
1433 1441 mutex_enter(&db->db_mtx);
1434 1442 DBUF_VERIFY(db);
1435 1443
1436 1444 if (db->db_state == DB_FILL) {
1437 1445 if (db->db_level == 0 && db->db_freed_in_flight) {
1438 1446 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1439 1447 /* we were freed while filling */
1440 1448 /* XXX dbuf_undirty? */
1441 1449 bzero(db->db.db_data, db->db.db_size);
1442 1450 db->db_freed_in_flight = FALSE;
1443 1451 }
1444 1452 db->db_state = DB_CACHED;
1445 1453 cv_broadcast(&db->db_changed);
1446 1454 }
1447 1455 mutex_exit(&db->db_mtx);
1448 1456 }
1449 1457
1450 1458 /*
1451 1459 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1452 1460 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1453 1461 */
1454 1462 void
1455 1463 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
1456 1464 {
1457 1465 ASSERT(!refcount_is_zero(&db->db_holds));
1458 1466 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1459 1467 ASSERT(db->db_level == 0);
1460 1468 ASSERT(DBUF_GET_BUFC_TYPE(db) == ARC_BUFC_DATA);
1461 1469 ASSERT(buf != NULL);
1462 1470 ASSERT(arc_buf_size(buf) == db->db.db_size);
1463 1471 ASSERT(tx->tx_txg != 0);
1464 1472
1465 1473 arc_return_buf(buf, db);
1466 1474 ASSERT(arc_released(buf));
1467 1475
1468 1476 mutex_enter(&db->db_mtx);
1469 1477
1470 1478 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1471 1479 cv_wait(&db->db_changed, &db->db_mtx);
1472 1480
1473 1481 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
1474 1482
1475 1483 if (db->db_state == DB_CACHED &&
1476 1484 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
1477 1485 mutex_exit(&db->db_mtx);
1478 1486 (void) dbuf_dirty(db, tx);
1479 1487 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
1480 1488 VERIFY(arc_buf_remove_ref(buf, db));
1481 1489 xuio_stat_wbuf_copied();
1482 1490 return;
1483 1491 }
1484 1492
1485 1493 xuio_stat_wbuf_nocopy();
1486 1494 if (db->db_state == DB_CACHED) {
1487 1495 dbuf_dirty_record_t *dr = db->db_last_dirty;
1488 1496
1489 1497 ASSERT(db->db_buf != NULL);
1490 1498 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
1491 1499 ASSERT(dr->dt.dl.dr_data == db->db_buf);
1492 1500 if (!arc_released(db->db_buf)) {
1493 1501 ASSERT(dr->dt.dl.dr_override_state ==
1494 1502 DR_OVERRIDDEN);
1495 1503 arc_release(db->db_buf, db);
1496 1504 }
1497 1505 dr->dt.dl.dr_data = buf;
1498 1506 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1499 1507 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
1500 1508 arc_release(db->db_buf, db);
1501 1509 VERIFY(arc_buf_remove_ref(db->db_buf, db));
1502 1510 }
1503 1511 db->db_buf = NULL;
1504 1512 }
1505 1513 ASSERT(db->db_buf == NULL);
1506 1514 dbuf_set_data(db, buf);
1507 1515 db->db_state = DB_FILL;
1508 1516 mutex_exit(&db->db_mtx);
1509 1517 (void) dbuf_dirty(db, tx);
1510 1518 dbuf_fill_done(db, tx);
1511 1519 }
1512 1520
1513 1521 /*
1514 1522 * "Clear" the contents of this dbuf. This will mark the dbuf
1515 1523 * EVICTING and clear *most* of its references. Unfortunetely,
1516 1524 * when we are not holding the dn_dbufs_mtx, we can't clear the
1517 1525 * entry in the dn_dbufs list. We have to wait until dbuf_destroy()
1518 1526 * in this case. For callers from the DMU we will usually see:
1519 1527 * dbuf_clear()->arc_buf_evict()->dbuf_do_evict()->dbuf_destroy()
1520 1528 * For the arc callback, we will usually see:
1521 1529 * dbuf_do_evict()->dbuf_clear();dbuf_destroy()
1522 1530 * Sometimes, though, we will get a mix of these two:
1523 1531 * DMU: dbuf_clear()->arc_buf_evict()
1524 1532 * ARC: dbuf_do_evict()->dbuf_destroy()
1525 1533 */
1526 1534 void
1527 1535 dbuf_clear(dmu_buf_impl_t *db)
1528 1536 {
1529 1537 dnode_t *dn;
1530 1538 dmu_buf_impl_t *parent = db->db_parent;
1531 1539 dmu_buf_impl_t *dndb;
1532 1540 int dbuf_gone = FALSE;
1533 1541
1534 1542 ASSERT(MUTEX_HELD(&db->db_mtx));
1535 1543 ASSERT(refcount_is_zero(&db->db_holds));
1536 1544
1537 1545 dbuf_evict_user(db);
1538 1546
1539 1547 if (db->db_state == DB_CACHED) {
1540 1548 ASSERT(db->db.db_data != NULL);
1541 1549 if (db->db_blkid == DMU_BONUS_BLKID) {
1542 1550 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN);
1543 1551 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
1544 1552 }
1545 1553 db->db.db_data = NULL;
1546 1554 db->db_state = DB_UNCACHED;
1547 1555 }
1548 1556
1549 1557 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
1550 1558 ASSERT(db->db_data_pending == NULL);
1551 1559
1552 1560 db->db_state = DB_EVICTING;
1553 1561 db->db_blkptr = NULL;
1554 1562
1555 1563 DB_DNODE_ENTER(db);
1556 1564 dn = DB_DNODE(db);
1557 1565 dndb = dn->dn_dbuf;
1558 1566 if (db->db_blkid != DMU_BONUS_BLKID && MUTEX_HELD(&dn->dn_dbufs_mtx)) {
1559 1567 list_remove(&dn->dn_dbufs, db);
1560 1568 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1561 1569 membar_producer();
1562 1570 DB_DNODE_EXIT(db);
1563 1571 /*
1564 1572 * Decrementing the dbuf count means that the hold corresponding
1565 1573 * to the removed dbuf is no longer discounted in dnode_move(),
1566 1574 * so the dnode cannot be moved until after we release the hold.
1567 1575 * The membar_producer() ensures visibility of the decremented
1568 1576 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
1569 1577 * release any lock.
1570 1578 */
1571 1579 dnode_rele(dn, db);
1572 1580 db->db_dnode_handle = NULL;
1573 1581 } else {
1574 1582 DB_DNODE_EXIT(db);
1575 1583 }
1576 1584
1577 1585 if (db->db_buf)
1578 1586 dbuf_gone = arc_buf_evict(db->db_buf);
1579 1587
1580 1588 if (!dbuf_gone)
1581 1589 mutex_exit(&db->db_mtx);
1582 1590
1583 1591 /*
1584 1592 * If this dbuf is referenced from an indirect dbuf,
1585 1593 * decrement the ref count on the indirect dbuf.
1586 1594 */
1587 1595 if (parent && parent != dndb)
1588 1596 dbuf_rele(parent, db);
1589 1597 }
1590 1598
1591 1599 static int
1592 1600 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
1593 1601 dmu_buf_impl_t **parentp, blkptr_t **bpp)
1594 1602 {
1595 1603 int nlevels, epbs;
1596 1604
1597 1605 *parentp = NULL;
1598 1606 *bpp = NULL;
1599 1607
1600 1608 ASSERT(blkid != DMU_BONUS_BLKID);
1601 1609
1602 1610 if (blkid == DMU_SPILL_BLKID) {
1603 1611 mutex_enter(&dn->dn_mtx);
1604 1612 if (dn->dn_have_spill &&
1605 1613 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
1606 1614 *bpp = &dn->dn_phys->dn_spill;
1607 1615 else
1608 1616 *bpp = NULL;
1609 1617 dbuf_add_ref(dn->dn_dbuf, NULL);
1610 1618 *parentp = dn->dn_dbuf;
1611 1619 mutex_exit(&dn->dn_mtx);
1612 1620 return (0);
1613 1621 }
1614 1622
1615 1623 if (dn->dn_phys->dn_nlevels == 0)
1616 1624 nlevels = 1;
1617 1625 else
1618 1626 nlevels = dn->dn_phys->dn_nlevels;
1619 1627
1620 1628 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1621 1629
1622 1630 ASSERT3U(level * epbs, <, 64);
1623 1631 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1624 1632 if (level >= nlevels ||
1625 1633 (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
1626 1634 /* the buffer has no parent yet */
1627 1635 return (SET_ERROR(ENOENT));
1628 1636 } else if (level < nlevels-1) {
1629 1637 /* this block is referenced from an indirect block */
1630 1638 int err = dbuf_hold_impl(dn, level+1,
1631 1639 blkid >> epbs, fail_sparse, NULL, parentp);
1632 1640 if (err)
1633 1641 return (err);
1634 1642 err = dbuf_read(*parentp, NULL,
1635 1643 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
1636 1644 if (err) {
1637 1645 dbuf_rele(*parentp, NULL);
1638 1646 *parentp = NULL;
1639 1647 return (err);
1640 1648 }
1641 1649 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
1642 1650 (blkid & ((1ULL << epbs) - 1));
1643 1651 return (0);
1644 1652 } else {
1645 1653 /* the block is referenced from the dnode */
1646 1654 ASSERT3U(level, ==, nlevels-1);
1647 1655 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
1648 1656 blkid < dn->dn_phys->dn_nblkptr);
1649 1657 if (dn->dn_dbuf) {
1650 1658 dbuf_add_ref(dn->dn_dbuf, NULL);
1651 1659 *parentp = dn->dn_dbuf;
1652 1660 }
1653 1661 *bpp = &dn->dn_phys->dn_blkptr[blkid];
1654 1662 return (0);
1655 1663 }
1656 1664 }
1657 1665
1658 1666 static dmu_buf_impl_t *
1659 1667 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
1660 1668 dmu_buf_impl_t *parent, blkptr_t *blkptr)
1661 1669 {
1662 1670 objset_t *os = dn->dn_objset;
1663 1671 dmu_buf_impl_t *db, *odb;
1664 1672
1665 1673 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1666 1674 ASSERT(dn->dn_type != DMU_OT_NONE);
1667 1675
1668 1676 db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
1669 1677
1670 1678 db->db_objset = os;
1671 1679 db->db.db_object = dn->dn_object;
1672 1680 db->db_level = level;
1673 1681 db->db_blkid = blkid;
1674 1682 db->db_last_dirty = NULL;
1675 1683 db->db_dirtycnt = 0;
1676 1684 db->db_dnode_handle = dn->dn_handle;
1677 1685 db->db_parent = parent;
1678 1686 db->db_blkptr = blkptr;
1679 1687
1680 1688 db->db_user_ptr = NULL;
1681 1689 db->db_user_data_ptr_ptr = NULL;
1682 1690 db->db_evict_func = NULL;
1683 1691 db->db_immediate_evict = 0;
1684 1692 db->db_freed_in_flight = 0;
1685 1693
1686 1694 if (blkid == DMU_BONUS_BLKID) {
1687 1695 ASSERT3P(parent, ==, dn->dn_dbuf);
1688 1696 db->db.db_size = DN_MAX_BONUSLEN -
1689 1697 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
1690 1698 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
1691 1699 db->db.db_offset = DMU_BONUS_BLKID;
1692 1700 db->db_state = DB_UNCACHED;
1693 1701 /* the bonus dbuf is not placed in the hash table */
1694 1702 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1695 1703 return (db);
1696 1704 } else if (blkid == DMU_SPILL_BLKID) {
1697 1705 db->db.db_size = (blkptr != NULL) ?
1698 1706 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
1699 1707 db->db.db_offset = 0;
1700 1708 } else {
1701 1709 int blocksize =
1702 1710 db->db_level ? 1<<dn->dn_indblkshift : dn->dn_datablksz;
1703 1711 db->db.db_size = blocksize;
1704 1712 db->db.db_offset = db->db_blkid * blocksize;
1705 1713 }
1706 1714
1707 1715 /*
1708 1716 * Hold the dn_dbufs_mtx while we get the new dbuf
1709 1717 * in the hash table *and* added to the dbufs list.
1710 1718 * This prevents a possible deadlock with someone
1711 1719 * trying to look up this dbuf before its added to the
1712 1720 * dn_dbufs list.
↓ open down ↓ |
867 lines elided |
↑ open up ↑ |
1713 1721 */
1714 1722 mutex_enter(&dn->dn_dbufs_mtx);
1715 1723 db->db_state = DB_EVICTING;
1716 1724 if ((odb = dbuf_hash_insert(db)) != NULL) {
1717 1725 /* someone else inserted it first */
1718 1726 kmem_cache_free(dbuf_cache, db);
1719 1727 mutex_exit(&dn->dn_dbufs_mtx);
1720 1728 return (odb);
1721 1729 }
1722 1730 list_insert_head(&dn->dn_dbufs, db);
1731 + if (db->db_level == 0 && db->db_blkid >=
1732 + dn->dn_unlisted_l0_blkid)
1733 + dn->dn_unlisted_l0_blkid = db->db_blkid + 1;
1723 1734 db->db_state = DB_UNCACHED;
1724 1735 mutex_exit(&dn->dn_dbufs_mtx);
1725 1736 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1726 1737
1727 1738 if (parent && parent != dn->dn_dbuf)
1728 1739 dbuf_add_ref(parent, db);
1729 1740
1730 1741 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
1731 1742 refcount_count(&dn->dn_holds) > 0);
1732 1743 (void) refcount_add(&dn->dn_holds, db);
1733 1744 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
1734 1745
1735 1746 dprintf_dbuf(db, "db=%p\n", db);
1736 1747
1737 1748 return (db);
1738 1749 }
1739 1750
1740 1751 static int
1741 1752 dbuf_do_evict(void *private)
1742 1753 {
1743 1754 arc_buf_t *buf = private;
1744 1755 dmu_buf_impl_t *db = buf->b_private;
1745 1756
1746 1757 if (!MUTEX_HELD(&db->db_mtx))
1747 1758 mutex_enter(&db->db_mtx);
1748 1759
1749 1760 ASSERT(refcount_is_zero(&db->db_holds));
1750 1761
1751 1762 if (db->db_state != DB_EVICTING) {
1752 1763 ASSERT(db->db_state == DB_CACHED);
1753 1764 DBUF_VERIFY(db);
1754 1765 db->db_buf = NULL;
1755 1766 dbuf_evict(db);
1756 1767 } else {
1757 1768 mutex_exit(&db->db_mtx);
1758 1769 dbuf_destroy(db);
1759 1770 }
1760 1771 return (0);
1761 1772 }
1762 1773
1763 1774 static void
1764 1775 dbuf_destroy(dmu_buf_impl_t *db)
1765 1776 {
1766 1777 ASSERT(refcount_is_zero(&db->db_holds));
1767 1778
1768 1779 if (db->db_blkid != DMU_BONUS_BLKID) {
1769 1780 /*
1770 1781 * If this dbuf is still on the dn_dbufs list,
1771 1782 * remove it from that list.
1772 1783 */
1773 1784 if (db->db_dnode_handle != NULL) {
1774 1785 dnode_t *dn;
1775 1786
1776 1787 DB_DNODE_ENTER(db);
1777 1788 dn = DB_DNODE(db);
1778 1789 mutex_enter(&dn->dn_dbufs_mtx);
1779 1790 list_remove(&dn->dn_dbufs, db);
1780 1791 (void) atomic_dec_32_nv(&dn->dn_dbufs_count);
1781 1792 mutex_exit(&dn->dn_dbufs_mtx);
1782 1793 DB_DNODE_EXIT(db);
1783 1794 /*
1784 1795 * Decrementing the dbuf count means that the hold
1785 1796 * corresponding to the removed dbuf is no longer
1786 1797 * discounted in dnode_move(), so the dnode cannot be
1787 1798 * moved until after we release the hold.
1788 1799 */
1789 1800 dnode_rele(dn, db);
1790 1801 db->db_dnode_handle = NULL;
1791 1802 }
1792 1803 dbuf_hash_remove(db);
1793 1804 }
1794 1805 db->db_parent = NULL;
1795 1806 db->db_buf = NULL;
1796 1807
1797 1808 ASSERT(!list_link_active(&db->db_link));
1798 1809 ASSERT(db->db.db_data == NULL);
1799 1810 ASSERT(db->db_hash_next == NULL);
1800 1811 ASSERT(db->db_blkptr == NULL);
1801 1812 ASSERT(db->db_data_pending == NULL);
1802 1813
1803 1814 kmem_cache_free(dbuf_cache, db);
1804 1815 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER);
1805 1816 }
1806 1817
1807 1818 void
1808 1819 dbuf_prefetch(dnode_t *dn, uint64_t blkid)
1809 1820 {
1810 1821 dmu_buf_impl_t *db = NULL;
1811 1822 blkptr_t *bp = NULL;
1812 1823
1813 1824 ASSERT(blkid != DMU_BONUS_BLKID);
1814 1825 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1815 1826
1816 1827 if (dnode_block_freed(dn, blkid))
1817 1828 return;
1818 1829
1819 1830 /* dbuf_find() returns with db_mtx held */
1820 1831 if (db = dbuf_find(dn, 0, blkid)) {
1821 1832 /*
1822 1833 * This dbuf is already in the cache. We assume that
1823 1834 * it is already CACHED, or else about to be either
1824 1835 * read or filled.
1825 1836 */
1826 1837 mutex_exit(&db->db_mtx);
1827 1838 return;
1828 1839 }
1829 1840
1830 1841 if (dbuf_findbp(dn, 0, blkid, TRUE, &db, &bp) == 0) {
1831 1842 if (bp && !BP_IS_HOLE(bp)) {
1832 1843 int priority = dn->dn_type == DMU_OT_DDT_ZAP ?
1833 1844 ZIO_PRIORITY_DDT_PREFETCH : ZIO_PRIORITY_ASYNC_READ;
1834 1845 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
1835 1846 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1836 1847 zbookmark_t zb;
1837 1848
1838 1849 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
1839 1850 dn->dn_object, 0, blkid);
1840 1851
1841 1852 (void) arc_read(NULL, dn->dn_objset->os_spa,
1842 1853 bp, NULL, NULL, priority,
1843 1854 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1844 1855 &aflags, &zb);
1845 1856 }
1846 1857 if (db)
1847 1858 dbuf_rele(db, NULL);
1848 1859 }
1849 1860 }
1850 1861
1851 1862 /*
1852 1863 * Returns with db_holds incremented, and db_mtx not held.
1853 1864 * Note: dn_struct_rwlock must be held.
1854 1865 */
1855 1866 int
1856 1867 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
1857 1868 void *tag, dmu_buf_impl_t **dbp)
1858 1869 {
1859 1870 dmu_buf_impl_t *db, *parent = NULL;
1860 1871
1861 1872 ASSERT(blkid != DMU_BONUS_BLKID);
1862 1873 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
1863 1874 ASSERT3U(dn->dn_nlevels, >, level);
1864 1875
1865 1876 *dbp = NULL;
1866 1877 top:
1867 1878 /* dbuf_find() returns with db_mtx held */
1868 1879 db = dbuf_find(dn, level, blkid);
1869 1880
1870 1881 if (db == NULL) {
1871 1882 blkptr_t *bp = NULL;
1872 1883 int err;
1873 1884
1874 1885 ASSERT3P(parent, ==, NULL);
1875 1886 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
1876 1887 if (fail_sparse) {
1877 1888 if (err == 0 && bp && BP_IS_HOLE(bp))
1878 1889 err = SET_ERROR(ENOENT);
1879 1890 if (err) {
1880 1891 if (parent)
1881 1892 dbuf_rele(parent, NULL);
1882 1893 return (err);
1883 1894 }
1884 1895 }
1885 1896 if (err && err != ENOENT)
1886 1897 return (err);
1887 1898 db = dbuf_create(dn, level, blkid, parent, bp);
1888 1899 }
1889 1900
1890 1901 if (db->db_buf && refcount_is_zero(&db->db_holds)) {
1891 1902 arc_buf_add_ref(db->db_buf, db);
1892 1903 if (db->db_buf->b_data == NULL) {
1893 1904 dbuf_clear(db);
1894 1905 if (parent) {
1895 1906 dbuf_rele(parent, NULL);
1896 1907 parent = NULL;
1897 1908 }
1898 1909 goto top;
1899 1910 }
1900 1911 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
1901 1912 }
1902 1913
1903 1914 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
1904 1915
1905 1916 /*
1906 1917 * If this buffer is currently syncing out, and we are are
1907 1918 * still referencing it from db_data, we need to make a copy
1908 1919 * of it in case we decide we want to dirty it again in this txg.
1909 1920 */
1910 1921 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1911 1922 dn->dn_object != DMU_META_DNODE_OBJECT &&
1912 1923 db->db_state == DB_CACHED && db->db_data_pending) {
1913 1924 dbuf_dirty_record_t *dr = db->db_data_pending;
1914 1925
1915 1926 if (dr->dt.dl.dr_data == db->db_buf) {
1916 1927 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1917 1928
1918 1929 dbuf_set_data(db,
1919 1930 arc_buf_alloc(dn->dn_objset->os_spa,
1920 1931 db->db.db_size, db, type));
1921 1932 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data,
1922 1933 db->db.db_size);
1923 1934 }
1924 1935 }
1925 1936
1926 1937 (void) refcount_add(&db->db_holds, tag);
1927 1938 dbuf_update_data(db);
1928 1939 DBUF_VERIFY(db);
1929 1940 mutex_exit(&db->db_mtx);
1930 1941
1931 1942 /* NOTE: we can't rele the parent until after we drop the db_mtx */
1932 1943 if (parent)
1933 1944 dbuf_rele(parent, NULL);
1934 1945
1935 1946 ASSERT3P(DB_DNODE(db), ==, dn);
1936 1947 ASSERT3U(db->db_blkid, ==, blkid);
1937 1948 ASSERT3U(db->db_level, ==, level);
1938 1949 *dbp = db;
1939 1950
1940 1951 return (0);
1941 1952 }
1942 1953
1943 1954 dmu_buf_impl_t *
1944 1955 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
1945 1956 {
1946 1957 dmu_buf_impl_t *db;
1947 1958 int err = dbuf_hold_impl(dn, 0, blkid, FALSE, tag, &db);
1948 1959 return (err ? NULL : db);
1949 1960 }
1950 1961
1951 1962 dmu_buf_impl_t *
1952 1963 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
1953 1964 {
1954 1965 dmu_buf_impl_t *db;
1955 1966 int err = dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
1956 1967 return (err ? NULL : db);
1957 1968 }
1958 1969
1959 1970 void
1960 1971 dbuf_create_bonus(dnode_t *dn)
1961 1972 {
1962 1973 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
1963 1974
1964 1975 ASSERT(dn->dn_bonus == NULL);
1965 1976 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
1966 1977 }
1967 1978
1968 1979 int
1969 1980 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
1970 1981 {
1971 1982 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1972 1983 dnode_t *dn;
1973 1984
1974 1985 if (db->db_blkid != DMU_SPILL_BLKID)
1975 1986 return (SET_ERROR(ENOTSUP));
1976 1987 if (blksz == 0)
1977 1988 blksz = SPA_MINBLOCKSIZE;
1978 1989 if (blksz > SPA_MAXBLOCKSIZE)
1979 1990 blksz = SPA_MAXBLOCKSIZE;
1980 1991 else
1981 1992 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
1982 1993
1983 1994 DB_DNODE_ENTER(db);
1984 1995 dn = DB_DNODE(db);
1985 1996 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1986 1997 dbuf_new_size(db, blksz, tx);
1987 1998 rw_exit(&dn->dn_struct_rwlock);
1988 1999 DB_DNODE_EXIT(db);
1989 2000
1990 2001 return (0);
1991 2002 }
1992 2003
1993 2004 void
1994 2005 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
1995 2006 {
1996 2007 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
1997 2008 }
1998 2009
1999 2010 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2000 2011 void
2001 2012 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
2002 2013 {
2003 2014 int64_t holds = refcount_add(&db->db_holds, tag);
2004 2015 ASSERT(holds > 1);
2005 2016 }
2006 2017
2007 2018 /*
2008 2019 * If you call dbuf_rele() you had better not be referencing the dnode handle
2009 2020 * unless you have some other direct or indirect hold on the dnode. (An indirect
2010 2021 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2011 2022 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2012 2023 * dnode's parent dbuf evicting its dnode handles.
2013 2024 */
2014 2025 #pragma weak dmu_buf_rele = dbuf_rele
2015 2026 void
2016 2027 dbuf_rele(dmu_buf_impl_t *db, void *tag)
2017 2028 {
2018 2029 mutex_enter(&db->db_mtx);
2019 2030 dbuf_rele_and_unlock(db, tag);
2020 2031 }
2021 2032
2022 2033 /*
2023 2034 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2024 2035 * db_dirtycnt and db_holds to be updated atomically.
2025 2036 */
2026 2037 void
2027 2038 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag)
2028 2039 {
2029 2040 int64_t holds;
2030 2041
2031 2042 ASSERT(MUTEX_HELD(&db->db_mtx));
2032 2043 DBUF_VERIFY(db);
2033 2044
2034 2045 /*
2035 2046 * Remove the reference to the dbuf before removing its hold on the
2036 2047 * dnode so we can guarantee in dnode_move() that a referenced bonus
2037 2048 * buffer has a corresponding dnode hold.
2038 2049 */
2039 2050 holds = refcount_remove(&db->db_holds, tag);
2040 2051 ASSERT(holds >= 0);
2041 2052
2042 2053 /*
2043 2054 * We can't freeze indirects if there is a possibility that they
2044 2055 * may be modified in the current syncing context.
2045 2056 */
2046 2057 if (db->db_buf && holds == (db->db_level == 0 ? db->db_dirtycnt : 0))
2047 2058 arc_buf_freeze(db->db_buf);
2048 2059
2049 2060 if (holds == db->db_dirtycnt &&
2050 2061 db->db_level == 0 && db->db_immediate_evict)
2051 2062 dbuf_evict_user(db);
2052 2063
2053 2064 if (holds == 0) {
2054 2065 if (db->db_blkid == DMU_BONUS_BLKID) {
2055 2066 mutex_exit(&db->db_mtx);
2056 2067
2057 2068 /*
2058 2069 * If the dnode moves here, we cannot cross this barrier
2059 2070 * until the move completes.
2060 2071 */
2061 2072 DB_DNODE_ENTER(db);
2062 2073 (void) atomic_dec_32_nv(&DB_DNODE(db)->dn_dbufs_count);
2063 2074 DB_DNODE_EXIT(db);
2064 2075 /*
2065 2076 * The bonus buffer's dnode hold is no longer discounted
2066 2077 * in dnode_move(). The dnode cannot move until after
2067 2078 * the dnode_rele().
2068 2079 */
2069 2080 dnode_rele(DB_DNODE(db), db);
2070 2081 } else if (db->db_buf == NULL) {
2071 2082 /*
2072 2083 * This is a special case: we never associated this
2073 2084 * dbuf with any data allocated from the ARC.
2074 2085 */
2075 2086 ASSERT(db->db_state == DB_UNCACHED ||
2076 2087 db->db_state == DB_NOFILL);
2077 2088 dbuf_evict(db);
2078 2089 } else if (arc_released(db->db_buf)) {
2079 2090 arc_buf_t *buf = db->db_buf;
2080 2091 /*
2081 2092 * This dbuf has anonymous data associated with it.
2082 2093 */
2083 2094 dbuf_set_data(db, NULL);
2084 2095 VERIFY(arc_buf_remove_ref(buf, db));
2085 2096 dbuf_evict(db);
2086 2097 } else {
2087 2098 VERIFY(!arc_buf_remove_ref(db->db_buf, db));
2088 2099
2089 2100 /*
2090 2101 * A dbuf will be eligible for eviction if either the
2091 2102 * 'primarycache' property is set or a duplicate
2092 2103 * copy of this buffer is already cached in the arc.
2093 2104 *
2094 2105 * In the case of the 'primarycache' a buffer
2095 2106 * is considered for eviction if it matches the
2096 2107 * criteria set in the property.
2097 2108 *
2098 2109 * To decide if our buffer is considered a
2099 2110 * duplicate, we must call into the arc to determine
2100 2111 * if multiple buffers are referencing the same
2101 2112 * block on-disk. If so, then we simply evict
2102 2113 * ourselves.
2103 2114 */
2104 2115 if (!DBUF_IS_CACHEABLE(db) ||
2105 2116 arc_buf_eviction_needed(db->db_buf))
2106 2117 dbuf_clear(db);
2107 2118 else
2108 2119 mutex_exit(&db->db_mtx);
2109 2120 }
2110 2121 } else {
2111 2122 mutex_exit(&db->db_mtx);
2112 2123 }
2113 2124 }
2114 2125
2115 2126 #pragma weak dmu_buf_refcount = dbuf_refcount
2116 2127 uint64_t
2117 2128 dbuf_refcount(dmu_buf_impl_t *db)
2118 2129 {
2119 2130 return (refcount_count(&db->db_holds));
2120 2131 }
2121 2132
2122 2133 void *
2123 2134 dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2124 2135 dmu_buf_evict_func_t *evict_func)
2125 2136 {
2126 2137 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2127 2138 user_data_ptr_ptr, evict_func));
2128 2139 }
2129 2140
2130 2141 void *
2131 2142 dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
2132 2143 dmu_buf_evict_func_t *evict_func)
2133 2144 {
2134 2145 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2135 2146
2136 2147 db->db_immediate_evict = TRUE;
2137 2148 return (dmu_buf_update_user(db_fake, NULL, user_ptr,
2138 2149 user_data_ptr_ptr, evict_func));
2139 2150 }
2140 2151
2141 2152 void *
2142 2153 dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
2143 2154 void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
2144 2155 {
2145 2156 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2146 2157 ASSERT(db->db_level == 0);
2147 2158
2148 2159 ASSERT((user_ptr == NULL) == (evict_func == NULL));
2149 2160
2150 2161 mutex_enter(&db->db_mtx);
2151 2162
2152 2163 if (db->db_user_ptr == old_user_ptr) {
2153 2164 db->db_user_ptr = user_ptr;
2154 2165 db->db_user_data_ptr_ptr = user_data_ptr_ptr;
2155 2166 db->db_evict_func = evict_func;
2156 2167
2157 2168 dbuf_update_data(db);
2158 2169 } else {
2159 2170 old_user_ptr = db->db_user_ptr;
2160 2171 }
2161 2172
2162 2173 mutex_exit(&db->db_mtx);
2163 2174 return (old_user_ptr);
2164 2175 }
2165 2176
2166 2177 void *
2167 2178 dmu_buf_get_user(dmu_buf_t *db_fake)
2168 2179 {
2169 2180 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2170 2181 ASSERT(!refcount_is_zero(&db->db_holds));
2171 2182
2172 2183 return (db->db_user_ptr);
2173 2184 }
2174 2185
2175 2186 boolean_t
2176 2187 dmu_buf_freeable(dmu_buf_t *dbuf)
2177 2188 {
2178 2189 boolean_t res = B_FALSE;
2179 2190 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2180 2191
2181 2192 if (db->db_blkptr)
2182 2193 res = dsl_dataset_block_freeable(db->db_objset->os_dsl_dataset,
2183 2194 db->db_blkptr, db->db_blkptr->blk_birth);
2184 2195
2185 2196 return (res);
2186 2197 }
2187 2198
2188 2199 blkptr_t *
2189 2200 dmu_buf_get_blkptr(dmu_buf_t *db)
2190 2201 {
2191 2202 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
2192 2203 return (dbi->db_blkptr);
2193 2204 }
2194 2205
2195 2206 static void
2196 2207 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
2197 2208 {
2198 2209 /* ASSERT(dmu_tx_is_syncing(tx) */
2199 2210 ASSERT(MUTEX_HELD(&db->db_mtx));
2200 2211
2201 2212 if (db->db_blkptr != NULL)
2202 2213 return;
2203 2214
2204 2215 if (db->db_blkid == DMU_SPILL_BLKID) {
2205 2216 db->db_blkptr = &dn->dn_phys->dn_spill;
2206 2217 BP_ZERO(db->db_blkptr);
2207 2218 return;
2208 2219 }
2209 2220 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
2210 2221 /*
2211 2222 * This buffer was allocated at a time when there was
2212 2223 * no available blkptrs from the dnode, or it was
2213 2224 * inappropriate to hook it in (i.e., nlevels mis-match).
2214 2225 */
2215 2226 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
2216 2227 ASSERT(db->db_parent == NULL);
2217 2228 db->db_parent = dn->dn_dbuf;
2218 2229 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
2219 2230 DBUF_VERIFY(db);
2220 2231 } else {
2221 2232 dmu_buf_impl_t *parent = db->db_parent;
2222 2233 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2223 2234
2224 2235 ASSERT(dn->dn_phys->dn_nlevels > 1);
2225 2236 if (parent == NULL) {
2226 2237 mutex_exit(&db->db_mtx);
2227 2238 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2228 2239 (void) dbuf_hold_impl(dn, db->db_level+1,
2229 2240 db->db_blkid >> epbs, FALSE, db, &parent);
2230 2241 rw_exit(&dn->dn_struct_rwlock);
2231 2242 mutex_enter(&db->db_mtx);
2232 2243 db->db_parent = parent;
2233 2244 }
2234 2245 db->db_blkptr = (blkptr_t *)parent->db.db_data +
2235 2246 (db->db_blkid & ((1ULL << epbs) - 1));
2236 2247 DBUF_VERIFY(db);
2237 2248 }
2238 2249 }
2239 2250
2240 2251 static void
2241 2252 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2242 2253 {
2243 2254 dmu_buf_impl_t *db = dr->dr_dbuf;
2244 2255 dnode_t *dn;
2245 2256 zio_t *zio;
2246 2257
2247 2258 ASSERT(dmu_tx_is_syncing(tx));
2248 2259
2249 2260 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2250 2261
2251 2262 mutex_enter(&db->db_mtx);
2252 2263
2253 2264 ASSERT(db->db_level > 0);
2254 2265 DBUF_VERIFY(db);
2255 2266
2256 2267 /* Read the block if it hasn't been read yet. */
2257 2268 if (db->db_buf == NULL) {
2258 2269 mutex_exit(&db->db_mtx);
2259 2270 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
2260 2271 mutex_enter(&db->db_mtx);
2261 2272 }
2262 2273 ASSERT3U(db->db_state, ==, DB_CACHED);
2263 2274 ASSERT(db->db_buf != NULL);
2264 2275
2265 2276 DB_DNODE_ENTER(db);
2266 2277 dn = DB_DNODE(db);
2267 2278 /* Indirect block size must match what the dnode thinks it is. */
2268 2279 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2269 2280 dbuf_check_blkptr(dn, db);
2270 2281 DB_DNODE_EXIT(db);
2271 2282
2272 2283 /* Provide the pending dirty record to child dbufs */
2273 2284 db->db_data_pending = dr;
2274 2285
2275 2286 mutex_exit(&db->db_mtx);
2276 2287 dbuf_write(dr, db->db_buf, tx);
2277 2288
2278 2289 zio = dr->dr_zio;
2279 2290 mutex_enter(&dr->dt.di.dr_mtx);
2280 2291 dbuf_sync_list(&dr->dt.di.dr_children, tx);
2281 2292 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2282 2293 mutex_exit(&dr->dt.di.dr_mtx);
2283 2294 zio_nowait(zio);
2284 2295 }
2285 2296
2286 2297 static void
2287 2298 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
2288 2299 {
2289 2300 arc_buf_t **datap = &dr->dt.dl.dr_data;
2290 2301 dmu_buf_impl_t *db = dr->dr_dbuf;
2291 2302 dnode_t *dn;
2292 2303 objset_t *os;
2293 2304 uint64_t txg = tx->tx_txg;
2294 2305
2295 2306 ASSERT(dmu_tx_is_syncing(tx));
2296 2307
2297 2308 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
2298 2309
2299 2310 mutex_enter(&db->db_mtx);
2300 2311 /*
2301 2312 * To be synced, we must be dirtied. But we
2302 2313 * might have been freed after the dirty.
2303 2314 */
2304 2315 if (db->db_state == DB_UNCACHED) {
2305 2316 /* This buffer has been freed since it was dirtied */
2306 2317 ASSERT(db->db.db_data == NULL);
2307 2318 } else if (db->db_state == DB_FILL) {
2308 2319 /* This buffer was freed and is now being re-filled */
2309 2320 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
2310 2321 } else {
2311 2322 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
2312 2323 }
2313 2324 DBUF_VERIFY(db);
2314 2325
2315 2326 DB_DNODE_ENTER(db);
2316 2327 dn = DB_DNODE(db);
2317 2328
2318 2329 if (db->db_blkid == DMU_SPILL_BLKID) {
2319 2330 mutex_enter(&dn->dn_mtx);
2320 2331 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
2321 2332 mutex_exit(&dn->dn_mtx);
2322 2333 }
2323 2334
2324 2335 /*
2325 2336 * If this is a bonus buffer, simply copy the bonus data into the
2326 2337 * dnode. It will be written out when the dnode is synced (and it
2327 2338 * will be synced, since it must have been dirty for dbuf_sync to
2328 2339 * be called).
2329 2340 */
2330 2341 if (db->db_blkid == DMU_BONUS_BLKID) {
2331 2342 dbuf_dirty_record_t **drp;
2332 2343
2333 2344 ASSERT(*datap != NULL);
2334 2345 ASSERT0(db->db_level);
2335 2346 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN);
2336 2347 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen);
2337 2348 DB_DNODE_EXIT(db);
2338 2349
2339 2350 if (*datap != db->db.db_data) {
2340 2351 zio_buf_free(*datap, DN_MAX_BONUSLEN);
2341 2352 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER);
2342 2353 }
2343 2354 db->db_data_pending = NULL;
2344 2355 drp = &db->db_last_dirty;
2345 2356 while (*drp != dr)
2346 2357 drp = &(*drp)->dr_next;
2347 2358 ASSERT(dr->dr_next == NULL);
2348 2359 ASSERT(dr->dr_dbuf == db);
2349 2360 *drp = dr->dr_next;
2350 2361 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2351 2362 ASSERT(db->db_dirtycnt > 0);
2352 2363 db->db_dirtycnt -= 1;
2353 2364 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2354 2365 return;
2355 2366 }
2356 2367
2357 2368 os = dn->dn_objset;
2358 2369
2359 2370 /*
2360 2371 * This function may have dropped the db_mtx lock allowing a dmu_sync
2361 2372 * operation to sneak in. As a result, we need to ensure that we
2362 2373 * don't check the dr_override_state until we have returned from
2363 2374 * dbuf_check_blkptr.
2364 2375 */
2365 2376 dbuf_check_blkptr(dn, db);
2366 2377
2367 2378 /*
2368 2379 * If this buffer is in the middle of an immediate write,
2369 2380 * wait for the synchronous IO to complete.
2370 2381 */
2371 2382 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
2372 2383 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
2373 2384 cv_wait(&db->db_changed, &db->db_mtx);
2374 2385 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
2375 2386 }
2376 2387
2377 2388 if (db->db_state != DB_NOFILL &&
2378 2389 dn->dn_object != DMU_META_DNODE_OBJECT &&
2379 2390 refcount_count(&db->db_holds) > 1 &&
2380 2391 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
2381 2392 *datap == db->db_buf) {
2382 2393 /*
2383 2394 * If this buffer is currently "in use" (i.e., there
2384 2395 * are active holds and db_data still references it),
2385 2396 * then make a copy before we start the write so that
2386 2397 * any modifications from the open txg will not leak
2387 2398 * into this write.
2388 2399 *
2389 2400 * NOTE: this copy does not need to be made for
2390 2401 * objects only modified in the syncing context (e.g.
2391 2402 * DNONE_DNODE blocks).
2392 2403 */
2393 2404 int blksz = arc_buf_size(*datap);
2394 2405 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
2395 2406 *datap = arc_buf_alloc(os->os_spa, blksz, db, type);
2396 2407 bcopy(db->db.db_data, (*datap)->b_data, blksz);
2397 2408 }
2398 2409 db->db_data_pending = dr;
2399 2410
2400 2411 mutex_exit(&db->db_mtx);
2401 2412
2402 2413 dbuf_write(dr, *datap, tx);
2403 2414
2404 2415 ASSERT(!list_link_active(&dr->dr_dirty_node));
2405 2416 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
2406 2417 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr);
2407 2418 DB_DNODE_EXIT(db);
2408 2419 } else {
2409 2420 /*
2410 2421 * Although zio_nowait() does not "wait for an IO", it does
2411 2422 * initiate the IO. If this is an empty write it seems plausible
2412 2423 * that the IO could actually be completed before the nowait
2413 2424 * returns. We need to DB_DNODE_EXIT() first in case
2414 2425 * zio_nowait() invalidates the dbuf.
2415 2426 */
2416 2427 DB_DNODE_EXIT(db);
2417 2428 zio_nowait(dr->dr_zio);
2418 2429 }
2419 2430 }
2420 2431
2421 2432 void
2422 2433 dbuf_sync_list(list_t *list, dmu_tx_t *tx)
2423 2434 {
2424 2435 dbuf_dirty_record_t *dr;
2425 2436
2426 2437 while (dr = list_head(list)) {
2427 2438 if (dr->dr_zio != NULL) {
2428 2439 /*
2429 2440 * If we find an already initialized zio then we
2430 2441 * are processing the meta-dnode, and we have finished.
2431 2442 * The dbufs for all dnodes are put back on the list
2432 2443 * during processing, so that we can zio_wait()
2433 2444 * these IOs after initiating all child IOs.
2434 2445 */
2435 2446 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
2436 2447 DMU_META_DNODE_OBJECT);
2437 2448 break;
2438 2449 }
2439 2450 list_remove(list, dr);
2440 2451 if (dr->dr_dbuf->db_level > 0)
2441 2452 dbuf_sync_indirect(dr, tx);
2442 2453 else
2443 2454 dbuf_sync_leaf(dr, tx);
2444 2455 }
2445 2456 }
2446 2457
2447 2458 /* ARGSUSED */
2448 2459 static void
2449 2460 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
2450 2461 {
2451 2462 dmu_buf_impl_t *db = vdb;
2452 2463 dnode_t *dn;
2453 2464 blkptr_t *bp = zio->io_bp;
2454 2465 blkptr_t *bp_orig = &zio->io_bp_orig;
2455 2466 spa_t *spa = zio->io_spa;
2456 2467 int64_t delta;
2457 2468 uint64_t fill = 0;
2458 2469 int i;
2459 2470
2460 2471 ASSERT(db->db_blkptr == bp);
2461 2472
2462 2473 DB_DNODE_ENTER(db);
2463 2474 dn = DB_DNODE(db);
2464 2475 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
2465 2476 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
2466 2477 zio->io_prev_space_delta = delta;
2467 2478
2468 2479 if (BP_IS_HOLE(bp)) {
2469 2480 ASSERT(bp->blk_fill == 0);
2470 2481 DB_DNODE_EXIT(db);
2471 2482 return;
2472 2483 }
2473 2484
2474 2485 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
2475 2486 BP_GET_TYPE(bp) == dn->dn_type) ||
2476 2487 (db->db_blkid == DMU_SPILL_BLKID &&
2477 2488 BP_GET_TYPE(bp) == dn->dn_bonustype));
2478 2489 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
2479 2490
2480 2491 mutex_enter(&db->db_mtx);
2481 2492
2482 2493 #ifdef ZFS_DEBUG
2483 2494 if (db->db_blkid == DMU_SPILL_BLKID) {
2484 2495 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2485 2496 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2486 2497 db->db_blkptr == &dn->dn_phys->dn_spill);
2487 2498 }
2488 2499 #endif
2489 2500
2490 2501 if (db->db_level == 0) {
2491 2502 mutex_enter(&dn->dn_mtx);
2492 2503 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
2493 2504 db->db_blkid != DMU_SPILL_BLKID)
2494 2505 dn->dn_phys->dn_maxblkid = db->db_blkid;
2495 2506 mutex_exit(&dn->dn_mtx);
2496 2507
2497 2508 if (dn->dn_type == DMU_OT_DNODE) {
2498 2509 dnode_phys_t *dnp = db->db.db_data;
2499 2510 for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
2500 2511 i--, dnp++) {
2501 2512 if (dnp->dn_type != DMU_OT_NONE)
2502 2513 fill++;
2503 2514 }
2504 2515 } else {
2505 2516 fill = 1;
2506 2517 }
2507 2518 } else {
2508 2519 blkptr_t *ibp = db->db.db_data;
2509 2520 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2510 2521 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
2511 2522 if (BP_IS_HOLE(ibp))
2512 2523 continue;
2513 2524 fill += ibp->blk_fill;
2514 2525 }
2515 2526 }
2516 2527 DB_DNODE_EXIT(db);
2517 2528
2518 2529 bp->blk_fill = fill;
2519 2530
2520 2531 mutex_exit(&db->db_mtx);
2521 2532 }
2522 2533
2523 2534 /* ARGSUSED */
2524 2535 static void
2525 2536 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
2526 2537 {
2527 2538 dmu_buf_impl_t *db = vdb;
2528 2539 blkptr_t *bp = zio->io_bp;
2529 2540 blkptr_t *bp_orig = &zio->io_bp_orig;
2530 2541 uint64_t txg = zio->io_txg;
2531 2542 dbuf_dirty_record_t **drp, *dr;
2532 2543
2533 2544 ASSERT0(zio->io_error);
2534 2545 ASSERT(db->db_blkptr == bp);
2535 2546
2536 2547 /*
2537 2548 * For nopwrites and rewrites we ensure that the bp matches our
2538 2549 * original and bypass all the accounting.
2539 2550 */
2540 2551 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
2541 2552 ASSERT(BP_EQUAL(bp, bp_orig));
2542 2553 } else {
2543 2554 objset_t *os;
2544 2555 dsl_dataset_t *ds;
2545 2556 dmu_tx_t *tx;
2546 2557
2547 2558 DB_GET_OBJSET(&os, db);
2548 2559 ds = os->os_dsl_dataset;
2549 2560 tx = os->os_synctx;
2550 2561
2551 2562 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
2552 2563 dsl_dataset_block_born(ds, bp, tx);
2553 2564 }
2554 2565
2555 2566 mutex_enter(&db->db_mtx);
2556 2567
2557 2568 DBUF_VERIFY(db);
2558 2569
2559 2570 drp = &db->db_last_dirty;
2560 2571 while ((dr = *drp) != db->db_data_pending)
2561 2572 drp = &dr->dr_next;
2562 2573 ASSERT(!list_link_active(&dr->dr_dirty_node));
2563 2574 ASSERT(dr->dr_txg == txg);
2564 2575 ASSERT(dr->dr_dbuf == db);
2565 2576 ASSERT(dr->dr_next == NULL);
2566 2577 *drp = dr->dr_next;
2567 2578
2568 2579 #ifdef ZFS_DEBUG
2569 2580 if (db->db_blkid == DMU_SPILL_BLKID) {
2570 2581 dnode_t *dn;
2571 2582
2572 2583 DB_DNODE_ENTER(db);
2573 2584 dn = DB_DNODE(db);
2574 2585 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
2575 2586 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
2576 2587 db->db_blkptr == &dn->dn_phys->dn_spill);
2577 2588 DB_DNODE_EXIT(db);
2578 2589 }
2579 2590 #endif
2580 2591
2581 2592 if (db->db_level == 0) {
2582 2593 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2583 2594 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2584 2595 if (db->db_state != DB_NOFILL) {
2585 2596 if (dr->dt.dl.dr_data != db->db_buf)
2586 2597 VERIFY(arc_buf_remove_ref(dr->dt.dl.dr_data,
2587 2598 db));
2588 2599 else if (!arc_released(db->db_buf))
2589 2600 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2590 2601 }
2591 2602 } else {
2592 2603 dnode_t *dn;
2593 2604
2594 2605 DB_DNODE_ENTER(db);
2595 2606 dn = DB_DNODE(db);
2596 2607 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
2597 2608 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
2598 2609 if (!BP_IS_HOLE(db->db_blkptr)) {
2599 2610 int epbs =
2600 2611 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
2601 2612 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
2602 2613 db->db.db_size);
2603 2614 ASSERT3U(dn->dn_phys->dn_maxblkid
2604 2615 >> (db->db_level * epbs), >=, db->db_blkid);
2605 2616 arc_set_callback(db->db_buf, dbuf_do_evict, db);
2606 2617 }
2607 2618 DB_DNODE_EXIT(db);
2608 2619 mutex_destroy(&dr->dt.di.dr_mtx);
2609 2620 list_destroy(&dr->dt.di.dr_children);
2610 2621 }
2611 2622 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2612 2623
2613 2624 cv_broadcast(&db->db_changed);
2614 2625 ASSERT(db->db_dirtycnt > 0);
2615 2626 db->db_dirtycnt -= 1;
2616 2627 db->db_data_pending = NULL;
2617 2628 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg);
2618 2629 }
2619 2630
2620 2631 static void
2621 2632 dbuf_write_nofill_ready(zio_t *zio)
2622 2633 {
2623 2634 dbuf_write_ready(zio, NULL, zio->io_private);
2624 2635 }
2625 2636
2626 2637 static void
2627 2638 dbuf_write_nofill_done(zio_t *zio)
2628 2639 {
2629 2640 dbuf_write_done(zio, NULL, zio->io_private);
2630 2641 }
2631 2642
2632 2643 static void
2633 2644 dbuf_write_override_ready(zio_t *zio)
2634 2645 {
2635 2646 dbuf_dirty_record_t *dr = zio->io_private;
2636 2647 dmu_buf_impl_t *db = dr->dr_dbuf;
2637 2648
2638 2649 dbuf_write_ready(zio, NULL, db);
2639 2650 }
2640 2651
2641 2652 static void
2642 2653 dbuf_write_override_done(zio_t *zio)
2643 2654 {
2644 2655 dbuf_dirty_record_t *dr = zio->io_private;
2645 2656 dmu_buf_impl_t *db = dr->dr_dbuf;
2646 2657 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
2647 2658
2648 2659 mutex_enter(&db->db_mtx);
2649 2660 if (!BP_EQUAL(zio->io_bp, obp)) {
2650 2661 if (!BP_IS_HOLE(obp))
2651 2662 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
2652 2663 arc_release(dr->dt.dl.dr_data, db);
2653 2664 }
2654 2665 mutex_exit(&db->db_mtx);
2655 2666
2656 2667 dbuf_write_done(zio, NULL, db);
2657 2668 }
2658 2669
2659 2670 /* Issue I/O to commit a dirty buffer to disk. */
2660 2671 static void
2661 2672 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
2662 2673 {
2663 2674 dmu_buf_impl_t *db = dr->dr_dbuf;
2664 2675 dnode_t *dn;
2665 2676 objset_t *os;
2666 2677 dmu_buf_impl_t *parent = db->db_parent;
2667 2678 uint64_t txg = tx->tx_txg;
2668 2679 zbookmark_t zb;
2669 2680 zio_prop_t zp;
2670 2681 zio_t *zio;
2671 2682 int wp_flag = 0;
2672 2683
2673 2684 DB_DNODE_ENTER(db);
2674 2685 dn = DB_DNODE(db);
2675 2686 os = dn->dn_objset;
2676 2687
2677 2688 if (db->db_state != DB_NOFILL) {
2678 2689 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
2679 2690 /*
2680 2691 * Private object buffers are released here rather
2681 2692 * than in dbuf_dirty() since they are only modified
2682 2693 * in the syncing context and we don't want the
2683 2694 * overhead of making multiple copies of the data.
2684 2695 */
2685 2696 if (BP_IS_HOLE(db->db_blkptr)) {
2686 2697 arc_buf_thaw(data);
2687 2698 } else {
2688 2699 dbuf_release_bp(db);
2689 2700 }
2690 2701 }
2691 2702 }
2692 2703
2693 2704 if (parent != dn->dn_dbuf) {
2694 2705 /* Our parent is an indirect block. */
2695 2706 /* We have a dirty parent that has been scheduled for write. */
2696 2707 ASSERT(parent && parent->db_data_pending);
2697 2708 /* Our parent's buffer is one level closer to the dnode. */
2698 2709 ASSERT(db->db_level == parent->db_level-1);
2699 2710 /*
2700 2711 * We're about to modify our parent's db_data by modifying
2701 2712 * our block pointer, so the parent must be released.
2702 2713 */
2703 2714 ASSERT(arc_released(parent->db_buf));
2704 2715 zio = parent->db_data_pending->dr_zio;
2705 2716 } else {
2706 2717 /* Our parent is the dnode itself. */
2707 2718 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
2708 2719 db->db_blkid != DMU_SPILL_BLKID) ||
2709 2720 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
2710 2721 if (db->db_blkid != DMU_SPILL_BLKID)
2711 2722 ASSERT3P(db->db_blkptr, ==,
2712 2723 &dn->dn_phys->dn_blkptr[db->db_blkid]);
2713 2724 zio = dn->dn_zio;
2714 2725 }
2715 2726
2716 2727 ASSERT(db->db_level == 0 || data == db->db_buf);
2717 2728 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
2718 2729 ASSERT(zio);
2719 2730
2720 2731 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
2721 2732 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
2722 2733 db->db.db_object, db->db_level, db->db_blkid);
2723 2734
2724 2735 if (db->db_blkid == DMU_SPILL_BLKID)
2725 2736 wp_flag = WP_SPILL;
2726 2737 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
2727 2738
2728 2739 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
2729 2740 DB_DNODE_EXIT(db);
2730 2741
2731 2742 if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2732 2743 ASSERT(db->db_state != DB_NOFILL);
2733 2744 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2734 2745 db->db_blkptr, data->b_data, arc_buf_size(data), &zp,
2735 2746 dbuf_write_override_ready, dbuf_write_override_done, dr,
2736 2747 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
2737 2748 mutex_enter(&db->db_mtx);
2738 2749 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
2739 2750 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
2740 2751 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
2741 2752 mutex_exit(&db->db_mtx);
2742 2753 } else if (db->db_state == DB_NOFILL) {
2743 2754 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF);
2744 2755 dr->dr_zio = zio_write(zio, os->os_spa, txg,
2745 2756 db->db_blkptr, NULL, db->db.db_size, &zp,
2746 2757 dbuf_write_nofill_ready, dbuf_write_nofill_done, db,
2747 2758 ZIO_PRIORITY_ASYNC_WRITE,
2748 2759 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
2749 2760 } else {
2750 2761 ASSERT(arc_released(data));
2751 2762 dr->dr_zio = arc_write(zio, os->os_spa, txg,
2752 2763 db->db_blkptr, data, DBUF_IS_L2CACHEABLE(db),
2753 2764 DBUF_IS_L2COMPRESSIBLE(db), &zp, dbuf_write_ready,
2754 2765 dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
2755 2766 ZIO_FLAG_MUSTSUCCEED, &zb);
2756 2767 }
2757 2768 }
↓ open down ↓ |
1025 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX