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