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