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