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