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