Print this page
4374 dn_free_ranges should use range_tree_t
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Max Grossman <max.grossman@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com
Reviewed by: Garrett D'Amore <garrett@damore.org>
Reviewed by: Dan McDonald <danmcd@omniti.com>
Approved by: Dan McDonald <danmcd@omniti.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/zfs/dnode.c
+++ new/usr/src/uts/common/fs/zfs/dnode.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 *
|
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
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 - * Copyright (c) 2013 by Delphix. All rights reserved.
23 + * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 24 */
25 25
26 26 #include <sys/zfs_context.h>
27 27 #include <sys/dbuf.h>
28 28 #include <sys/dnode.h>
29 29 #include <sys/dmu.h>
30 30 #include <sys/dmu_impl.h>
31 31 #include <sys/dmu_tx.h>
32 32 #include <sys/dmu_objset.h>
33 33 #include <sys/dsl_dir.h>
34 34 #include <sys/dsl_dataset.h>
35 35 #include <sys/spa.h>
36 36 #include <sys/zio.h>
37 37 #include <sys/dmu_zfetch.h>
38 +#include <sys/range_tree.h>
38 39
39 -static int free_range_compar(const void *node1, const void *node2);
40 -
41 40 static kmem_cache_t *dnode_cache;
42 41 /*
43 42 * Define DNODE_STATS to turn on statistic gathering. By default, it is only
44 43 * turned on when DEBUG is also defined.
45 44 */
46 45 #ifdef DEBUG
47 46 #define DNODE_STATS
48 47 #endif /* DEBUG */
49 48
50 49 #ifdef DNODE_STATS
51 50 #define DNODE_STAT_ADD(stat) ((stat)++)
52 51 #else
53 52 #define DNODE_STAT_ADD(stat) /* nothing */
54 53 #endif /* DNODE_STATS */
55 54
56 55 static dnode_phys_t dnode_phys_zero;
57 56
58 57 int zfs_default_bs = SPA_MINBLOCKSHIFT;
59 58 int zfs_default_ibs = DN_MAX_INDBLKSHIFT;
60 59
61 60 static kmem_cbrc_t dnode_move(void *, void *, size_t, void *);
62 61
63 62 /* ARGSUSED */
64 63 static int
65 64 dnode_cons(void *arg, void *unused, int kmflag)
66 65 {
67 66 dnode_t *dn = arg;
68 67 int i;
69 68
70 69 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL);
71 70 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL);
72 71 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL);
73 72 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL);
74 73
75 74 /*
76 75 * Every dbuf has a reference, and dropping a tracked reference is
77 76 * O(number of references), so don't track dn_holds.
78 77 */
79 78 refcount_create_untracked(&dn->dn_holds);
80 79 refcount_create(&dn->dn_tx_holds);
81 80 list_link_init(&dn->dn_link);
82 81
|
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
83 82 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr));
84 83 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels));
85 84 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift));
86 85 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype));
87 86 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk));
88 87 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen));
89 88 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz));
90 89
91 90 for (i = 0; i < TXG_SIZE; i++) {
92 91 list_link_init(&dn->dn_dirty_link[i]);
93 - avl_create(&dn->dn_ranges[i], free_range_compar,
94 - sizeof (free_range_t),
95 - offsetof(struct free_range, fr_node));
92 + dn->dn_free_ranges[i] = NULL;
96 93 list_create(&dn->dn_dirty_records[i],
97 94 sizeof (dbuf_dirty_record_t),
98 95 offsetof(dbuf_dirty_record_t, dr_dirty_node));
99 96 }
100 97
101 98 dn->dn_allocated_txg = 0;
102 99 dn->dn_free_txg = 0;
103 100 dn->dn_assigned_txg = 0;
104 101 dn->dn_dirtyctx = 0;
105 102 dn->dn_dirtyctx_firstset = NULL;
106 103 dn->dn_bonus = NULL;
107 104 dn->dn_have_spill = B_FALSE;
108 105 dn->dn_zio = NULL;
109 106 dn->dn_oldused = 0;
110 107 dn->dn_oldflags = 0;
111 108 dn->dn_olduid = 0;
112 109 dn->dn_oldgid = 0;
113 110 dn->dn_newuid = 0;
114 111 dn->dn_newgid = 0;
115 112 dn->dn_id_flags = 0;
116 113
117 114 dn->dn_dbufs_count = 0;
118 115 dn->dn_unlisted_l0_blkid = 0;
119 116 list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t),
120 117 offsetof(dmu_buf_impl_t, db_link));
121 118
122 119 dn->dn_moved = 0;
123 120 return (0);
124 121 }
125 122
126 123 /* ARGSUSED */
127 124 static void
128 125 dnode_dest(void *arg, void *unused)
129 126 {
130 127 int i;
131 128 dnode_t *dn = arg;
132 129
|
↓ open down ↓ |
27 lines elided |
↑ open up ↑ |
133 130 rw_destroy(&dn->dn_struct_rwlock);
134 131 mutex_destroy(&dn->dn_mtx);
135 132 mutex_destroy(&dn->dn_dbufs_mtx);
136 133 cv_destroy(&dn->dn_notxholds);
137 134 refcount_destroy(&dn->dn_holds);
138 135 refcount_destroy(&dn->dn_tx_holds);
139 136 ASSERT(!list_link_active(&dn->dn_link));
140 137
141 138 for (i = 0; i < TXG_SIZE; i++) {
142 139 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
143 - avl_destroy(&dn->dn_ranges[i]);
140 + ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
144 141 list_destroy(&dn->dn_dirty_records[i]);
145 142 ASSERT0(dn->dn_next_nblkptr[i]);
146 143 ASSERT0(dn->dn_next_nlevels[i]);
147 144 ASSERT0(dn->dn_next_indblkshift[i]);
148 145 ASSERT0(dn->dn_next_bonustype[i]);
149 146 ASSERT0(dn->dn_rm_spillblk[i]);
150 147 ASSERT0(dn->dn_next_bonuslen[i]);
151 148 ASSERT0(dn->dn_next_blksz[i]);
152 149 }
153 150
154 151 ASSERT0(dn->dn_allocated_txg);
155 152 ASSERT0(dn->dn_free_txg);
156 153 ASSERT0(dn->dn_assigned_txg);
157 154 ASSERT0(dn->dn_dirtyctx);
158 155 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL);
159 156 ASSERT3P(dn->dn_bonus, ==, NULL);
160 157 ASSERT(!dn->dn_have_spill);
161 158 ASSERT3P(dn->dn_zio, ==, NULL);
162 159 ASSERT0(dn->dn_oldused);
163 160 ASSERT0(dn->dn_oldflags);
164 161 ASSERT0(dn->dn_olduid);
165 162 ASSERT0(dn->dn_oldgid);
166 163 ASSERT0(dn->dn_newuid);
167 164 ASSERT0(dn->dn_newgid);
168 165 ASSERT0(dn->dn_id_flags);
169 166
170 167 ASSERT0(dn->dn_dbufs_count);
171 168 ASSERT0(dn->dn_unlisted_l0_blkid);
172 169 list_destroy(&dn->dn_dbufs);
173 170 }
174 171
175 172 void
176 173 dnode_init(void)
177 174 {
178 175 ASSERT(dnode_cache == NULL);
179 176 dnode_cache = kmem_cache_create("dnode_t",
180 177 sizeof (dnode_t),
181 178 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0);
182 179 kmem_cache_set_move(dnode_cache, dnode_move);
183 180 }
184 181
185 182 void
186 183 dnode_fini(void)
187 184 {
188 185 kmem_cache_destroy(dnode_cache);
189 186 dnode_cache = NULL;
190 187 }
191 188
192 189
193 190 #ifdef ZFS_DEBUG
194 191 void
195 192 dnode_verify(dnode_t *dn)
196 193 {
197 194 int drop_struct_lock = FALSE;
198 195
199 196 ASSERT(dn->dn_phys);
200 197 ASSERT(dn->dn_objset);
201 198 ASSERT(dn->dn_handle->dnh_dnode == dn);
202 199
203 200 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
204 201
205 202 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY))
206 203 return;
207 204
208 205 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
209 206 rw_enter(&dn->dn_struct_rwlock, RW_READER);
210 207 drop_struct_lock = TRUE;
211 208 }
212 209 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) {
213 210 int i;
214 211 ASSERT3U(dn->dn_indblkshift, >=, 0);
215 212 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT);
216 213 if (dn->dn_datablkshift) {
217 214 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT);
218 215 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT);
219 216 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz);
220 217 }
221 218 ASSERT3U(dn->dn_nlevels, <=, 30);
222 219 ASSERT(DMU_OT_IS_VALID(dn->dn_type));
223 220 ASSERT3U(dn->dn_nblkptr, >=, 1);
224 221 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
225 222 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
226 223 ASSERT3U(dn->dn_datablksz, ==,
227 224 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT);
228 225 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0);
229 226 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) +
230 227 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN);
231 228 for (i = 0; i < TXG_SIZE; i++) {
232 229 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels);
233 230 }
234 231 }
235 232 if (dn->dn_phys->dn_type != DMU_OT_NONE)
236 233 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels);
237 234 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL);
238 235 if (dn->dn_dbuf != NULL) {
239 236 ASSERT3P(dn->dn_phys, ==,
240 237 (dnode_phys_t *)dn->dn_dbuf->db.db_data +
241 238 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT)));
242 239 }
243 240 if (drop_struct_lock)
244 241 rw_exit(&dn->dn_struct_rwlock);
245 242 }
246 243 #endif
247 244
248 245 void
249 246 dnode_byteswap(dnode_phys_t *dnp)
250 247 {
251 248 uint64_t *buf64 = (void*)&dnp->dn_blkptr;
252 249 int i;
253 250
254 251 if (dnp->dn_type == DMU_OT_NONE) {
255 252 bzero(dnp, sizeof (dnode_phys_t));
256 253 return;
257 254 }
258 255
259 256 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec);
260 257 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen);
261 258 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid);
262 259 dnp->dn_used = BSWAP_64(dnp->dn_used);
263 260
264 261 /*
265 262 * dn_nblkptr is only one byte, so it's OK to read it in either
266 263 * byte order. We can't read dn_bouslen.
267 264 */
268 265 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT);
269 266 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR);
270 267 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++)
271 268 buf64[i] = BSWAP_64(buf64[i]);
272 269
273 270 /*
274 271 * OK to check dn_bonuslen for zero, because it won't matter if
275 272 * we have the wrong byte order. This is necessary because the
276 273 * dnode dnode is smaller than a regular dnode.
277 274 */
278 275 if (dnp->dn_bonuslen != 0) {
279 276 /*
280 277 * Note that the bonus length calculated here may be
281 278 * longer than the actual bonus buffer. This is because
282 279 * we always put the bonus buffer after the last block
283 280 * pointer (instead of packing it against the end of the
284 281 * dnode buffer).
285 282 */
286 283 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t);
287 284 size_t len = DN_MAX_BONUSLEN - off;
288 285 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype));
289 286 dmu_object_byteswap_t byteswap =
290 287 DMU_OT_BYTESWAP(dnp->dn_bonustype);
291 288 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len);
292 289 }
293 290
294 291 /* Swap SPILL block if we have one */
295 292 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
296 293 byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t));
297 294
298 295 }
299 296
300 297 void
301 298 dnode_buf_byteswap(void *vbuf, size_t size)
302 299 {
303 300 dnode_phys_t *buf = vbuf;
304 301 int i;
305 302
|
↓ open down ↓ |
152 lines elided |
↑ open up ↑ |
306 303 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT));
307 304 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0);
308 305
309 306 size >>= DNODE_SHIFT;
310 307 for (i = 0; i < size; i++) {
311 308 dnode_byteswap(buf);
312 309 buf++;
313 310 }
314 311 }
315 312
316 -static int
317 -free_range_compar(const void *node1, const void *node2)
318 -{
319 - const free_range_t *rp1 = node1;
320 - const free_range_t *rp2 = node2;
321 -
322 - if (rp1->fr_blkid < rp2->fr_blkid)
323 - return (-1);
324 - else if (rp1->fr_blkid > rp2->fr_blkid)
325 - return (1);
326 - else return (0);
327 -}
328 -
329 313 void
330 314 dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx)
331 315 {
332 316 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
333 317
334 318 dnode_setdirty(dn, tx);
335 319 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
336 320 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN -
337 321 (dn->dn_nblkptr-1) * sizeof (blkptr_t));
338 322 dn->dn_bonuslen = newsize;
339 323 if (newsize == 0)
340 324 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN;
341 325 else
342 326 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
343 327 rw_exit(&dn->dn_struct_rwlock);
344 328 }
345 329
346 330 void
347 331 dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx)
348 332 {
349 333 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
350 334 dnode_setdirty(dn, tx);
351 335 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
352 336 dn->dn_bonustype = newtype;
353 337 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
354 338 rw_exit(&dn->dn_struct_rwlock);
355 339 }
356 340
357 341 void
358 342 dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx)
359 343 {
360 344 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1);
361 345 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
362 346 dnode_setdirty(dn, tx);
363 347 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK;
364 348 dn->dn_have_spill = B_FALSE;
365 349 }
366 350
|
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
367 351 static void
368 352 dnode_setdblksz(dnode_t *dn, int size)
369 353 {
370 354 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE));
371 355 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
372 356 ASSERT3U(size, >=, SPA_MINBLOCKSIZE);
373 357 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <,
374 358 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8));
375 359 dn->dn_datablksz = size;
376 360 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT;
377 - dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0;
361 + dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0;
378 362 }
379 363
380 364 static dnode_t *
381 365 dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db,
382 366 uint64_t object, dnode_handle_t *dnh)
383 367 {
384 368 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP);
385 369
386 370 ASSERT(!POINTER_IS_VALID(dn->dn_objset));
387 371 dn->dn_moved = 0;
388 372
389 373 /*
390 374 * Defer setting dn_objset until the dnode is ready to be a candidate
391 375 * for the dnode_move() callback.
392 376 */
393 377 dn->dn_object = object;
394 378 dn->dn_dbuf = db;
395 379 dn->dn_handle = dnh;
396 380 dn->dn_phys = dnp;
397 381
398 382 if (dnp->dn_datablkszsec) {
399 383 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
400 384 } else {
401 385 dn->dn_datablksz = 0;
402 386 dn->dn_datablkszsec = 0;
403 387 dn->dn_datablkshift = 0;
404 388 }
405 389 dn->dn_indblkshift = dnp->dn_indblkshift;
406 390 dn->dn_nlevels = dnp->dn_nlevels;
407 391 dn->dn_type = dnp->dn_type;
408 392 dn->dn_nblkptr = dnp->dn_nblkptr;
409 393 dn->dn_checksum = dnp->dn_checksum;
410 394 dn->dn_compress = dnp->dn_compress;
411 395 dn->dn_bonustype = dnp->dn_bonustype;
412 396 dn->dn_bonuslen = dnp->dn_bonuslen;
413 397 dn->dn_maxblkid = dnp->dn_maxblkid;
414 398 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0);
415 399 dn->dn_id_flags = 0;
416 400
417 401 dmu_zfetch_init(&dn->dn_zfetch, dn);
418 402
419 403 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type));
420 404
421 405 mutex_enter(&os->os_lock);
422 406 list_insert_head(&os->os_dnodes, dn);
423 407 membar_producer();
424 408 /*
425 409 * Everything else must be valid before assigning dn_objset makes the
426 410 * dnode eligible for dnode_move().
427 411 */
428 412 dn->dn_objset = os;
429 413 mutex_exit(&os->os_lock);
430 414
431 415 arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER);
432 416 return (dn);
433 417 }
434 418
435 419 /*
436 420 * Caller must be holding the dnode handle, which is released upon return.
437 421 */
438 422 static void
439 423 dnode_destroy(dnode_t *dn)
440 424 {
441 425 objset_t *os = dn->dn_objset;
442 426
443 427 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0);
444 428
445 429 mutex_enter(&os->os_lock);
446 430 POINTER_INVALIDATE(&dn->dn_objset);
447 431 list_remove(&os->os_dnodes, dn);
448 432 mutex_exit(&os->os_lock);
449 433
450 434 /* the dnode can no longer move, so we can release the handle */
451 435 zrl_remove(&dn->dn_handle->dnh_zrlock);
452 436
453 437 dn->dn_allocated_txg = 0;
454 438 dn->dn_free_txg = 0;
455 439 dn->dn_assigned_txg = 0;
456 440
457 441 dn->dn_dirtyctx = 0;
458 442 if (dn->dn_dirtyctx_firstset != NULL) {
459 443 kmem_free(dn->dn_dirtyctx_firstset, 1);
460 444 dn->dn_dirtyctx_firstset = NULL;
461 445 }
462 446 if (dn->dn_bonus != NULL) {
463 447 mutex_enter(&dn->dn_bonus->db_mtx);
464 448 dbuf_evict(dn->dn_bonus);
465 449 dn->dn_bonus = NULL;
466 450 }
467 451 dn->dn_zio = NULL;
468 452
469 453 dn->dn_have_spill = B_FALSE;
470 454 dn->dn_oldused = 0;
471 455 dn->dn_oldflags = 0;
472 456 dn->dn_olduid = 0;
473 457 dn->dn_oldgid = 0;
474 458 dn->dn_newuid = 0;
475 459 dn->dn_newgid = 0;
476 460 dn->dn_id_flags = 0;
477 461 dn->dn_unlisted_l0_blkid = 0;
478 462
479 463 dmu_zfetch_rele(&dn->dn_zfetch);
480 464 kmem_cache_free(dnode_cache, dn);
481 465 arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER);
482 466 }
483 467
484 468 void
485 469 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs,
486 470 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
487 471 {
488 472 int i;
489 473
490 474 if (blocksize == 0)
491 475 blocksize = 1 << zfs_default_bs;
492 476 else if (blocksize > SPA_MAXBLOCKSIZE)
493 477 blocksize = SPA_MAXBLOCKSIZE;
494 478 else
495 479 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE);
496 480
497 481 if (ibs == 0)
498 482 ibs = zfs_default_ibs;
499 483
500 484 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT);
501 485
502 486 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset,
503 487 dn->dn_object, tx->tx_txg, blocksize, ibs);
504 488
505 489 ASSERT(dn->dn_type == DMU_OT_NONE);
506 490 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0);
507 491 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE);
508 492 ASSERT(ot != DMU_OT_NONE);
509 493 ASSERT(DMU_OT_IS_VALID(ot));
510 494 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
511 495 (bonustype == DMU_OT_SA && bonuslen == 0) ||
512 496 (bonustype != DMU_OT_NONE && bonuslen != 0));
513 497 ASSERT(DMU_OT_IS_VALID(bonustype));
514 498 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
515 499 ASSERT(dn->dn_type == DMU_OT_NONE);
516 500 ASSERT0(dn->dn_maxblkid);
517 501 ASSERT0(dn->dn_allocated_txg);
518 502 ASSERT0(dn->dn_assigned_txg);
519 503 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
520 504 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1);
521 505 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL);
522 506
|
↓ open down ↓ |
135 lines elided |
↑ open up ↑ |
523 507 for (i = 0; i < TXG_SIZE; i++) {
524 508 ASSERT0(dn->dn_next_nblkptr[i]);
525 509 ASSERT0(dn->dn_next_nlevels[i]);
526 510 ASSERT0(dn->dn_next_indblkshift[i]);
527 511 ASSERT0(dn->dn_next_bonuslen[i]);
528 512 ASSERT0(dn->dn_next_bonustype[i]);
529 513 ASSERT0(dn->dn_rm_spillblk[i]);
530 514 ASSERT0(dn->dn_next_blksz[i]);
531 515 ASSERT(!list_link_active(&dn->dn_dirty_link[i]));
532 516 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL);
533 - ASSERT0(avl_numnodes(&dn->dn_ranges[i]));
517 + ASSERT3P(dn->dn_free_ranges[i], ==, NULL);
534 518 }
535 519
536 520 dn->dn_type = ot;
537 521 dnode_setdblksz(dn, blocksize);
538 522 dn->dn_indblkshift = ibs;
539 523 dn->dn_nlevels = 1;
540 524 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
541 525 dn->dn_nblkptr = 1;
542 526 else
543 527 dn->dn_nblkptr = 1 +
544 528 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
545 529 dn->dn_bonustype = bonustype;
546 530 dn->dn_bonuslen = bonuslen;
547 531 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
548 532 dn->dn_compress = ZIO_COMPRESS_INHERIT;
549 533 dn->dn_dirtyctx = 0;
550 534
551 535 dn->dn_free_txg = 0;
552 536 if (dn->dn_dirtyctx_firstset) {
553 537 kmem_free(dn->dn_dirtyctx_firstset, 1);
554 538 dn->dn_dirtyctx_firstset = NULL;
555 539 }
556 540
557 541 dn->dn_allocated_txg = tx->tx_txg;
558 542 dn->dn_id_flags = 0;
559 543
560 544 dnode_setdirty(dn, tx);
561 545 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs;
562 546 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen;
563 547 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype;
564 548 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz;
565 549 }
566 550
567 551 void
568 552 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize,
569 553 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
570 554 {
571 555 int nblkptr;
572 556
573 557 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE);
574 558 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE);
575 559 ASSERT0(blocksize % SPA_MINBLOCKSIZE);
576 560 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx));
577 561 ASSERT(tx->tx_txg != 0);
578 562 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) ||
579 563 (bonustype != DMU_OT_NONE && bonuslen != 0) ||
580 564 (bonustype == DMU_OT_SA && bonuslen == 0));
581 565 ASSERT(DMU_OT_IS_VALID(bonustype));
582 566 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN);
583 567
584 568 /* clean up any unreferenced dbufs */
585 569 dnode_evict_dbufs(dn);
586 570
587 571 dn->dn_id_flags = 0;
588 572
589 573 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
590 574 dnode_setdirty(dn, tx);
591 575 if (dn->dn_datablksz != blocksize) {
592 576 /* change blocksize */
593 577 ASSERT(dn->dn_maxblkid == 0 &&
594 578 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) ||
595 579 dnode_block_freed(dn, 0)));
596 580 dnode_setdblksz(dn, blocksize);
597 581 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize;
598 582 }
599 583 if (dn->dn_bonuslen != bonuslen)
600 584 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen;
601 585
602 586 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */
603 587 nblkptr = 1;
604 588 else
605 589 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT);
606 590 if (dn->dn_bonustype != bonustype)
607 591 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype;
608 592 if (dn->dn_nblkptr != nblkptr)
609 593 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr;
610 594 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
611 595 dbuf_rm_spill(dn, tx);
612 596 dnode_rm_spill(dn, tx);
613 597 }
614 598 rw_exit(&dn->dn_struct_rwlock);
615 599
616 600 /* change type */
617 601 dn->dn_type = ot;
618 602
619 603 /* change bonus size and type */
620 604 mutex_enter(&dn->dn_mtx);
621 605 dn->dn_bonustype = bonustype;
622 606 dn->dn_bonuslen = bonuslen;
623 607 dn->dn_nblkptr = nblkptr;
624 608 dn->dn_checksum = ZIO_CHECKSUM_INHERIT;
625 609 dn->dn_compress = ZIO_COMPRESS_INHERIT;
626 610 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR);
627 611
628 612 /* fix up the bonus db_size */
629 613 if (dn->dn_bonus) {
630 614 dn->dn_bonus->db.db_size =
631 615 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t);
632 616 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size);
633 617 }
634 618
635 619 dn->dn_allocated_txg = tx->tx_txg;
636 620 mutex_exit(&dn->dn_mtx);
637 621 }
638 622
639 623 #ifdef DNODE_STATS
640 624 static struct {
641 625 uint64_t dms_dnode_invalid;
642 626 uint64_t dms_dnode_recheck1;
643 627 uint64_t dms_dnode_recheck2;
644 628 uint64_t dms_dnode_special;
645 629 uint64_t dms_dnode_handle;
646 630 uint64_t dms_dnode_rwlock;
647 631 uint64_t dms_dnode_active;
648 632 } dnode_move_stats;
649 633 #endif /* DNODE_STATS */
650 634
651 635 static void
652 636 dnode_move_impl(dnode_t *odn, dnode_t *ndn)
653 637 {
654 638 int i;
655 639
656 640 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock));
657 641 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx));
658 642 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx));
659 643 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock));
660 644
661 645 /* Copy fields. */
662 646 ndn->dn_objset = odn->dn_objset;
663 647 ndn->dn_object = odn->dn_object;
664 648 ndn->dn_dbuf = odn->dn_dbuf;
665 649 ndn->dn_handle = odn->dn_handle;
666 650 ndn->dn_phys = odn->dn_phys;
667 651 ndn->dn_type = odn->dn_type;
668 652 ndn->dn_bonuslen = odn->dn_bonuslen;
669 653 ndn->dn_bonustype = odn->dn_bonustype;
670 654 ndn->dn_nblkptr = odn->dn_nblkptr;
671 655 ndn->dn_checksum = odn->dn_checksum;
672 656 ndn->dn_compress = odn->dn_compress;
673 657 ndn->dn_nlevels = odn->dn_nlevels;
674 658 ndn->dn_indblkshift = odn->dn_indblkshift;
675 659 ndn->dn_datablkshift = odn->dn_datablkshift;
676 660 ndn->dn_datablkszsec = odn->dn_datablkszsec;
677 661 ndn->dn_datablksz = odn->dn_datablksz;
678 662 ndn->dn_maxblkid = odn->dn_maxblkid;
679 663 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0],
680 664 sizeof (odn->dn_next_nblkptr));
681 665 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0],
682 666 sizeof (odn->dn_next_nlevels));
683 667 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0],
684 668 sizeof (odn->dn_next_indblkshift));
685 669 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0],
686 670 sizeof (odn->dn_next_bonustype));
|
↓ open down ↓ |
143 lines elided |
↑ open up ↑ |
687 671 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0],
688 672 sizeof (odn->dn_rm_spillblk));
689 673 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0],
690 674 sizeof (odn->dn_next_bonuslen));
691 675 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0],
692 676 sizeof (odn->dn_next_blksz));
693 677 for (i = 0; i < TXG_SIZE; i++) {
694 678 list_move_tail(&ndn->dn_dirty_records[i],
695 679 &odn->dn_dirty_records[i]);
696 680 }
697 - bcopy(&odn->dn_ranges[0], &ndn->dn_ranges[0], sizeof (odn->dn_ranges));
681 + bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0],
682 + sizeof (odn->dn_free_ranges));
698 683 ndn->dn_allocated_txg = odn->dn_allocated_txg;
699 684 ndn->dn_free_txg = odn->dn_free_txg;
700 685 ndn->dn_assigned_txg = odn->dn_assigned_txg;
701 686 ndn->dn_dirtyctx = odn->dn_dirtyctx;
702 687 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset;
703 688 ASSERT(refcount_count(&odn->dn_tx_holds) == 0);
704 689 refcount_transfer(&ndn->dn_holds, &odn->dn_holds);
705 690 ASSERT(list_is_empty(&ndn->dn_dbufs));
706 691 list_move_tail(&ndn->dn_dbufs, &odn->dn_dbufs);
707 692 ndn->dn_dbufs_count = odn->dn_dbufs_count;
708 693 ndn->dn_unlisted_l0_blkid = odn->dn_unlisted_l0_blkid;
709 694 ndn->dn_bonus = odn->dn_bonus;
710 695 ndn->dn_have_spill = odn->dn_have_spill;
711 696 ndn->dn_zio = odn->dn_zio;
712 697 ndn->dn_oldused = odn->dn_oldused;
713 698 ndn->dn_oldflags = odn->dn_oldflags;
714 699 ndn->dn_olduid = odn->dn_olduid;
715 700 ndn->dn_oldgid = odn->dn_oldgid;
716 701 ndn->dn_newuid = odn->dn_newuid;
717 702 ndn->dn_newgid = odn->dn_newgid;
718 703 ndn->dn_id_flags = odn->dn_id_flags;
719 704 dmu_zfetch_init(&ndn->dn_zfetch, NULL);
720 705 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream);
721 706 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode;
722 707 ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt;
723 708 ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail;
724 709
725 710 /*
726 711 * Update back pointers. Updating the handle fixes the back pointer of
727 712 * every descendant dbuf as well as the bonus dbuf.
728 713 */
729 714 ASSERT(ndn->dn_handle->dnh_dnode == odn);
730 715 ndn->dn_handle->dnh_dnode = ndn;
731 716 if (ndn->dn_zfetch.zf_dnode == odn) {
732 717 ndn->dn_zfetch.zf_dnode = ndn;
733 718 }
734 719
735 720 /*
736 721 * Invalidate the original dnode by clearing all of its back pointers.
737 722 */
738 723 odn->dn_dbuf = NULL;
739 724 odn->dn_handle = NULL;
740 725 list_create(&odn->dn_dbufs, sizeof (dmu_buf_impl_t),
741 726 offsetof(dmu_buf_impl_t, db_link));
742 727 odn->dn_dbufs_count = 0;
743 728 odn->dn_unlisted_l0_blkid = 0;
744 729 odn->dn_bonus = NULL;
745 730 odn->dn_zfetch.zf_dnode = NULL;
746 731
747 732 /*
748 733 * Set the low bit of the objset pointer to ensure that dnode_move()
749 734 * recognizes the dnode as invalid in any subsequent callback.
|
↓ open down ↓ |
42 lines elided |
↑ open up ↑ |
750 735 */
751 736 POINTER_INVALIDATE(&odn->dn_objset);
752 737
753 738 /*
754 739 * Satisfy the destructor.
755 740 */
756 741 for (i = 0; i < TXG_SIZE; i++) {
757 742 list_create(&odn->dn_dirty_records[i],
758 743 sizeof (dbuf_dirty_record_t),
759 744 offsetof(dbuf_dirty_record_t, dr_dirty_node));
760 - odn->dn_ranges[i].avl_root = NULL;
761 - odn->dn_ranges[i].avl_numnodes = 0;
745 + odn->dn_free_ranges[i] = NULL;
762 746 odn->dn_next_nlevels[i] = 0;
763 747 odn->dn_next_indblkshift[i] = 0;
764 748 odn->dn_next_bonustype[i] = 0;
765 749 odn->dn_rm_spillblk[i] = 0;
766 750 odn->dn_next_bonuslen[i] = 0;
767 751 odn->dn_next_blksz[i] = 0;
768 752 }
769 753 odn->dn_allocated_txg = 0;
770 754 odn->dn_free_txg = 0;
771 755 odn->dn_assigned_txg = 0;
772 756 odn->dn_dirtyctx = 0;
773 757 odn->dn_dirtyctx_firstset = NULL;
774 758 odn->dn_have_spill = B_FALSE;
775 759 odn->dn_zio = NULL;
776 760 odn->dn_oldused = 0;
777 761 odn->dn_oldflags = 0;
778 762 odn->dn_olduid = 0;
779 763 odn->dn_oldgid = 0;
780 764 odn->dn_newuid = 0;
781 765 odn->dn_newgid = 0;
782 766 odn->dn_id_flags = 0;
783 767
784 768 /*
785 769 * Mark the dnode.
786 770 */
787 771 ndn->dn_moved = 1;
788 772 odn->dn_moved = (uint8_t)-1;
789 773 }
790 774
791 775 #ifdef _KERNEL
792 776 /*ARGSUSED*/
793 777 static kmem_cbrc_t
794 778 dnode_move(void *buf, void *newbuf, size_t size, void *arg)
795 779 {
796 780 dnode_t *odn = buf, *ndn = newbuf;
797 781 objset_t *os;
798 782 int64_t refcount;
799 783 uint32_t dbufs;
800 784
801 785 /*
802 786 * The dnode is on the objset's list of known dnodes if the objset
803 787 * pointer is valid. We set the low bit of the objset pointer when
804 788 * freeing the dnode to invalidate it, and the memory patterns written
805 789 * by kmem (baddcafe and deadbeef) set at least one of the two low bits.
806 790 * A newly created dnode sets the objset pointer last of all to indicate
807 791 * that the dnode is known and in a valid state to be moved by this
808 792 * function.
809 793 */
810 794 os = odn->dn_objset;
811 795 if (!POINTER_IS_VALID(os)) {
812 796 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid);
813 797 return (KMEM_CBRC_DONT_KNOW);
814 798 }
815 799
816 800 /*
817 801 * Ensure that the objset does not go away during the move.
818 802 */
819 803 rw_enter(&os_lock, RW_WRITER);
820 804 if (os != odn->dn_objset) {
821 805 rw_exit(&os_lock);
822 806 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1);
823 807 return (KMEM_CBRC_DONT_KNOW);
824 808 }
825 809
826 810 /*
827 811 * If the dnode is still valid, then so is the objset. We know that no
828 812 * valid objset can be freed while we hold os_lock, so we can safely
829 813 * ensure that the objset remains in use.
830 814 */
831 815 mutex_enter(&os->os_lock);
832 816
833 817 /*
834 818 * Recheck the objset pointer in case the dnode was removed just before
835 819 * acquiring the lock.
836 820 */
837 821 if (os != odn->dn_objset) {
838 822 mutex_exit(&os->os_lock);
839 823 rw_exit(&os_lock);
840 824 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2);
841 825 return (KMEM_CBRC_DONT_KNOW);
842 826 }
843 827
844 828 /*
845 829 * At this point we know that as long as we hold os->os_lock, the dnode
846 830 * cannot be freed and fields within the dnode can be safely accessed.
847 831 * The objset listing this dnode cannot go away as long as this dnode is
848 832 * on its list.
849 833 */
850 834 rw_exit(&os_lock);
851 835 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) {
852 836 mutex_exit(&os->os_lock);
853 837 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special);
854 838 return (KMEM_CBRC_NO);
855 839 }
856 840 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */
857 841
858 842 /*
859 843 * Lock the dnode handle to prevent the dnode from obtaining any new
860 844 * holds. This also prevents the descendant dbufs and the bonus dbuf
861 845 * from accessing the dnode, so that we can discount their holds. The
862 846 * handle is safe to access because we know that while the dnode cannot
863 847 * go away, neither can its handle. Once we hold dnh_zrlock, we can
864 848 * safely move any dnode referenced only by dbufs.
865 849 */
866 850 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) {
867 851 mutex_exit(&os->os_lock);
868 852 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle);
869 853 return (KMEM_CBRC_LATER);
870 854 }
871 855
872 856 /*
873 857 * Ensure a consistent view of the dnode's holds and the dnode's dbufs.
874 858 * We need to guarantee that there is a hold for every dbuf in order to
875 859 * determine whether the dnode is actively referenced. Falsely matching
876 860 * a dbuf to an active hold would lead to an unsafe move. It's possible
877 861 * that a thread already having an active dnode hold is about to add a
878 862 * dbuf, and we can't compare hold and dbuf counts while the add is in
879 863 * progress.
880 864 */
881 865 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) {
882 866 zrl_exit(&odn->dn_handle->dnh_zrlock);
883 867 mutex_exit(&os->os_lock);
884 868 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock);
885 869 return (KMEM_CBRC_LATER);
886 870 }
887 871
888 872 /*
889 873 * A dbuf may be removed (evicted) without an active dnode hold. In that
890 874 * case, the dbuf count is decremented under the handle lock before the
891 875 * dbuf's hold is released. This order ensures that if we count the hold
892 876 * after the dbuf is removed but before its hold is released, we will
893 877 * treat the unmatched hold as active and exit safely. If we count the
894 878 * hold before the dbuf is removed, the hold is discounted, and the
895 879 * removal is blocked until the move completes.
896 880 */
897 881 refcount = refcount_count(&odn->dn_holds);
898 882 ASSERT(refcount >= 0);
899 883 dbufs = odn->dn_dbufs_count;
900 884
901 885 /* We can't have more dbufs than dnode holds. */
902 886 ASSERT3U(dbufs, <=, refcount);
903 887 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount,
904 888 uint32_t, dbufs);
905 889
906 890 if (refcount > dbufs) {
907 891 rw_exit(&odn->dn_struct_rwlock);
908 892 zrl_exit(&odn->dn_handle->dnh_zrlock);
909 893 mutex_exit(&os->os_lock);
910 894 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active);
911 895 return (KMEM_CBRC_LATER);
912 896 }
913 897
914 898 rw_exit(&odn->dn_struct_rwlock);
915 899
916 900 /*
917 901 * At this point we know that anyone with a hold on the dnode is not
918 902 * actively referencing it. The dnode is known and in a valid state to
919 903 * move. We're holding the locks needed to execute the critical section.
920 904 */
921 905 dnode_move_impl(odn, ndn);
922 906
923 907 list_link_replace(&odn->dn_link, &ndn->dn_link);
924 908 /* If the dnode was safe to move, the refcount cannot have changed. */
925 909 ASSERT(refcount == refcount_count(&ndn->dn_holds));
926 910 ASSERT(dbufs == ndn->dn_dbufs_count);
927 911 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */
928 912 mutex_exit(&os->os_lock);
929 913
930 914 return (KMEM_CBRC_YES);
931 915 }
932 916 #endif /* _KERNEL */
933 917
934 918 void
935 919 dnode_special_close(dnode_handle_t *dnh)
936 920 {
937 921 dnode_t *dn = dnh->dnh_dnode;
938 922
939 923 /*
940 924 * Wait for final references to the dnode to clear. This can
941 925 * only happen if the arc is asyncronously evicting state that
942 926 * has a hold on this dnode while we are trying to evict this
943 927 * dnode.
944 928 */
945 929 while (refcount_count(&dn->dn_holds) > 0)
946 930 delay(1);
947 931 zrl_add(&dnh->dnh_zrlock);
948 932 dnode_destroy(dn); /* implicit zrl_remove() */
949 933 zrl_destroy(&dnh->dnh_zrlock);
950 934 dnh->dnh_dnode = NULL;
951 935 }
952 936
953 937 dnode_t *
954 938 dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object,
955 939 dnode_handle_t *dnh)
956 940 {
957 941 dnode_t *dn = dnode_create(os, dnp, NULL, object, dnh);
958 942 dnh->dnh_dnode = dn;
959 943 zrl_init(&dnh->dnh_zrlock);
960 944 DNODE_VERIFY(dn);
961 945 return (dn);
962 946 }
963 947
964 948 static void
965 949 dnode_buf_pageout(dmu_buf_t *db, void *arg)
966 950 {
967 951 dnode_children_t *children_dnodes = arg;
968 952 int i;
969 953 int epb = db->db_size >> DNODE_SHIFT;
970 954
971 955 ASSERT(epb == children_dnodes->dnc_count);
972 956
973 957 for (i = 0; i < epb; i++) {
974 958 dnode_handle_t *dnh = &children_dnodes->dnc_children[i];
975 959 dnode_t *dn;
976 960
977 961 /*
978 962 * The dnode handle lock guards against the dnode moving to
979 963 * another valid address, so there is no need here to guard
980 964 * against changes to or from NULL.
981 965 */
982 966 if (dnh->dnh_dnode == NULL) {
983 967 zrl_destroy(&dnh->dnh_zrlock);
984 968 continue;
985 969 }
986 970
987 971 zrl_add(&dnh->dnh_zrlock);
988 972 dn = dnh->dnh_dnode;
989 973 /*
990 974 * If there are holds on this dnode, then there should
991 975 * be holds on the dnode's containing dbuf as well; thus
992 976 * it wouldn't be eligible for eviction and this function
993 977 * would not have been called.
994 978 */
995 979 ASSERT(refcount_is_zero(&dn->dn_holds));
996 980 ASSERT(refcount_is_zero(&dn->dn_tx_holds));
997 981
998 982 dnode_destroy(dn); /* implicit zrl_remove() */
999 983 zrl_destroy(&dnh->dnh_zrlock);
1000 984 dnh->dnh_dnode = NULL;
1001 985 }
1002 986 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1003 987 (epb - 1) * sizeof (dnode_handle_t));
1004 988 }
1005 989
1006 990 /*
1007 991 * errors:
1008 992 * EINVAL - invalid object number.
1009 993 * EIO - i/o error.
1010 994 * succeeds even for free dnodes.
1011 995 */
1012 996 int
1013 997 dnode_hold_impl(objset_t *os, uint64_t object, int flag,
1014 998 void *tag, dnode_t **dnp)
1015 999 {
1016 1000 int epb, idx, err;
1017 1001 int drop_struct_lock = FALSE;
1018 1002 int type;
1019 1003 uint64_t blk;
1020 1004 dnode_t *mdn, *dn;
1021 1005 dmu_buf_impl_t *db;
1022 1006 dnode_children_t *children_dnodes;
1023 1007 dnode_handle_t *dnh;
1024 1008
1025 1009 /*
1026 1010 * If you are holding the spa config lock as writer, you shouldn't
1027 1011 * be asking the DMU to do *anything* unless it's the root pool
1028 1012 * which may require us to read from the root filesystem while
1029 1013 * holding some (not all) of the locks as writer.
1030 1014 */
1031 1015 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 ||
1032 1016 (spa_is_root(os->os_spa) &&
1033 1017 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER)));
1034 1018
1035 1019 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) {
1036 1020 dn = (object == DMU_USERUSED_OBJECT) ?
1037 1021 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os);
1038 1022 if (dn == NULL)
1039 1023 return (SET_ERROR(ENOENT));
1040 1024 type = dn->dn_type;
1041 1025 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE)
1042 1026 return (SET_ERROR(ENOENT));
1043 1027 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)
1044 1028 return (SET_ERROR(EEXIST));
1045 1029 DNODE_VERIFY(dn);
1046 1030 (void) refcount_add(&dn->dn_holds, tag);
1047 1031 *dnp = dn;
1048 1032 return (0);
1049 1033 }
1050 1034
1051 1035 if (object == 0 || object >= DN_MAX_OBJECT)
1052 1036 return (SET_ERROR(EINVAL));
1053 1037
1054 1038 mdn = DMU_META_DNODE(os);
1055 1039 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT);
1056 1040
1057 1041 DNODE_VERIFY(mdn);
1058 1042
1059 1043 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) {
1060 1044 rw_enter(&mdn->dn_struct_rwlock, RW_READER);
1061 1045 drop_struct_lock = TRUE;
1062 1046 }
1063 1047
1064 1048 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t));
1065 1049
1066 1050 db = dbuf_hold(mdn, blk, FTAG);
1067 1051 if (drop_struct_lock)
1068 1052 rw_exit(&mdn->dn_struct_rwlock);
1069 1053 if (db == NULL)
1070 1054 return (SET_ERROR(EIO));
1071 1055 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1072 1056 if (err) {
1073 1057 dbuf_rele(db, FTAG);
1074 1058 return (err);
1075 1059 }
1076 1060
1077 1061 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT);
1078 1062 epb = db->db.db_size >> DNODE_SHIFT;
1079 1063
1080 1064 idx = object & (epb-1);
1081 1065
1082 1066 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE);
1083 1067 children_dnodes = dmu_buf_get_user(&db->db);
1084 1068 if (children_dnodes == NULL) {
1085 1069 int i;
1086 1070 dnode_children_t *winner;
1087 1071 children_dnodes = kmem_alloc(sizeof (dnode_children_t) +
1088 1072 (epb - 1) * sizeof (dnode_handle_t), KM_SLEEP);
1089 1073 children_dnodes->dnc_count = epb;
1090 1074 dnh = &children_dnodes->dnc_children[0];
1091 1075 for (i = 0; i < epb; i++) {
1092 1076 zrl_init(&dnh[i].dnh_zrlock);
1093 1077 dnh[i].dnh_dnode = NULL;
1094 1078 }
1095 1079 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL,
1096 1080 dnode_buf_pageout)) {
1097 1081 kmem_free(children_dnodes, sizeof (dnode_children_t) +
1098 1082 (epb - 1) * sizeof (dnode_handle_t));
1099 1083 children_dnodes = winner;
1100 1084 }
1101 1085 }
1102 1086 ASSERT(children_dnodes->dnc_count == epb);
1103 1087
1104 1088 dnh = &children_dnodes->dnc_children[idx];
1105 1089 zrl_add(&dnh->dnh_zrlock);
1106 1090 if ((dn = dnh->dnh_dnode) == NULL) {
1107 1091 dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx;
1108 1092 dnode_t *winner;
1109 1093
1110 1094 dn = dnode_create(os, phys, db, object, dnh);
1111 1095 winner = atomic_cas_ptr(&dnh->dnh_dnode, NULL, dn);
1112 1096 if (winner != NULL) {
1113 1097 zrl_add(&dnh->dnh_zrlock);
1114 1098 dnode_destroy(dn); /* implicit zrl_remove() */
1115 1099 dn = winner;
1116 1100 }
1117 1101 }
1118 1102
1119 1103 mutex_enter(&dn->dn_mtx);
1120 1104 type = dn->dn_type;
1121 1105 if (dn->dn_free_txg ||
1122 1106 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) ||
1123 1107 ((flag & DNODE_MUST_BE_FREE) &&
1124 1108 (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) {
1125 1109 mutex_exit(&dn->dn_mtx);
1126 1110 zrl_remove(&dnh->dnh_zrlock);
1127 1111 dbuf_rele(db, FTAG);
1128 1112 return (type == DMU_OT_NONE ? ENOENT : EEXIST);
1129 1113 }
1130 1114 mutex_exit(&dn->dn_mtx);
1131 1115
1132 1116 if (refcount_add(&dn->dn_holds, tag) == 1)
1133 1117 dbuf_add_ref(db, dnh);
1134 1118 /* Now we can rely on the hold to prevent the dnode from moving. */
1135 1119 zrl_remove(&dnh->dnh_zrlock);
1136 1120
1137 1121 DNODE_VERIFY(dn);
1138 1122 ASSERT3P(dn->dn_dbuf, ==, db);
1139 1123 ASSERT3U(dn->dn_object, ==, object);
1140 1124 dbuf_rele(db, FTAG);
1141 1125
1142 1126 *dnp = dn;
1143 1127 return (0);
1144 1128 }
1145 1129
1146 1130 /*
1147 1131 * Return held dnode if the object is allocated, NULL if not.
1148 1132 */
1149 1133 int
1150 1134 dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp)
1151 1135 {
1152 1136 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp));
1153 1137 }
1154 1138
1155 1139 /*
1156 1140 * Can only add a reference if there is already at least one
1157 1141 * reference on the dnode. Returns FALSE if unable to add a
1158 1142 * new reference.
1159 1143 */
1160 1144 boolean_t
1161 1145 dnode_add_ref(dnode_t *dn, void *tag)
1162 1146 {
1163 1147 mutex_enter(&dn->dn_mtx);
1164 1148 if (refcount_is_zero(&dn->dn_holds)) {
1165 1149 mutex_exit(&dn->dn_mtx);
1166 1150 return (FALSE);
1167 1151 }
1168 1152 VERIFY(1 < refcount_add(&dn->dn_holds, tag));
1169 1153 mutex_exit(&dn->dn_mtx);
1170 1154 return (TRUE);
1171 1155 }
1172 1156
1173 1157 void
1174 1158 dnode_rele(dnode_t *dn, void *tag)
1175 1159 {
1176 1160 uint64_t refs;
1177 1161 /* Get while the hold prevents the dnode from moving. */
1178 1162 dmu_buf_impl_t *db = dn->dn_dbuf;
1179 1163 dnode_handle_t *dnh = dn->dn_handle;
1180 1164
1181 1165 mutex_enter(&dn->dn_mtx);
1182 1166 refs = refcount_remove(&dn->dn_holds, tag);
1183 1167 mutex_exit(&dn->dn_mtx);
1184 1168
1185 1169 /*
1186 1170 * It's unsafe to release the last hold on a dnode by dnode_rele() or
1187 1171 * indirectly by dbuf_rele() while relying on the dnode handle to
1188 1172 * prevent the dnode from moving, since releasing the last hold could
1189 1173 * result in the dnode's parent dbuf evicting its dnode handles. For
1190 1174 * that reason anyone calling dnode_rele() or dbuf_rele() without some
1191 1175 * other direct or indirect hold on the dnode must first drop the dnode
1192 1176 * handle.
1193 1177 */
1194 1178 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread);
1195 1179
1196 1180 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */
1197 1181 if (refs == 0 && db != NULL) {
1198 1182 /*
1199 1183 * Another thread could add a hold to the dnode handle in
1200 1184 * dnode_hold_impl() while holding the parent dbuf. Since the
1201 1185 * hold on the parent dbuf prevents the handle from being
1202 1186 * destroyed, the hold on the handle is OK. We can't yet assert
1203 1187 * that the handle has zero references, but that will be
1204 1188 * asserted anyway when the handle gets destroyed.
1205 1189 */
1206 1190 dbuf_rele(db, dnh);
1207 1191 }
1208 1192 }
1209 1193
1210 1194 void
1211 1195 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx)
1212 1196 {
1213 1197 objset_t *os = dn->dn_objset;
1214 1198 uint64_t txg = tx->tx_txg;
1215 1199
1216 1200 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) {
1217 1201 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1218 1202 return;
1219 1203 }
1220 1204
1221 1205 DNODE_VERIFY(dn);
1222 1206
1223 1207 #ifdef ZFS_DEBUG
1224 1208 mutex_enter(&dn->dn_mtx);
1225 1209 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg);
1226 1210 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg);
1227 1211 mutex_exit(&dn->dn_mtx);
1228 1212 #endif
1229 1213
1230 1214 /*
1231 1215 * Determine old uid/gid when necessary
1232 1216 */
1233 1217 dmu_objset_userquota_get_ids(dn, B_TRUE, tx);
1234 1218
1235 1219 mutex_enter(&os->os_lock);
1236 1220
1237 1221 /*
1238 1222 * If we are already marked dirty, we're done.
1239 1223 */
1240 1224 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) {
1241 1225 mutex_exit(&os->os_lock);
1242 1226 return;
1243 1227 }
1244 1228
1245 1229 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs));
1246 1230 ASSERT(dn->dn_datablksz != 0);
1247 1231 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]);
1248 1232 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]);
1249 1233 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]);
1250 1234
1251 1235 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n",
1252 1236 dn->dn_object, txg);
1253 1237
1254 1238 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) {
1255 1239 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn);
1256 1240 } else {
1257 1241 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn);
1258 1242 }
1259 1243
1260 1244 mutex_exit(&os->os_lock);
1261 1245
1262 1246 /*
1263 1247 * The dnode maintains a hold on its containing dbuf as
1264 1248 * long as there are holds on it. Each instantiated child
1265 1249 * dbuf maintains a hold on the dnode. When the last child
1266 1250 * drops its hold, the dnode will drop its hold on the
1267 1251 * containing dbuf. We add a "dirty hold" here so that the
1268 1252 * dnode will hang around after we finish processing its
1269 1253 * children.
1270 1254 */
1271 1255 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg));
1272 1256
1273 1257 (void) dbuf_dirty(dn->dn_dbuf, tx);
1274 1258
1275 1259 dsl_dataset_dirty(os->os_dsl_dataset, tx);
1276 1260 }
1277 1261
1278 1262 void
1279 1263 dnode_free(dnode_t *dn, dmu_tx_t *tx)
1280 1264 {
1281 1265 int txgoff = tx->tx_txg & TXG_MASK;
1282 1266
1283 1267 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg);
1284 1268
1285 1269 /* we should be the only holder... hopefully */
1286 1270 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */
1287 1271
1288 1272 mutex_enter(&dn->dn_mtx);
1289 1273 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) {
1290 1274 mutex_exit(&dn->dn_mtx);
1291 1275 return;
1292 1276 }
1293 1277 dn->dn_free_txg = tx->tx_txg;
1294 1278 mutex_exit(&dn->dn_mtx);
1295 1279
1296 1280 /*
1297 1281 * If the dnode is already dirty, it needs to be moved from
1298 1282 * the dirty list to the free list.
1299 1283 */
1300 1284 mutex_enter(&dn->dn_objset->os_lock);
1301 1285 if (list_link_active(&dn->dn_dirty_link[txgoff])) {
1302 1286 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn);
1303 1287 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn);
1304 1288 mutex_exit(&dn->dn_objset->os_lock);
1305 1289 } else {
1306 1290 mutex_exit(&dn->dn_objset->os_lock);
1307 1291 dnode_setdirty(dn, tx);
1308 1292 }
1309 1293 }
1310 1294
1311 1295 /*
1312 1296 * Try to change the block size for the indicated dnode. This can only
1313 1297 * succeed if there are no blocks allocated or dirty beyond first block
1314 1298 */
1315 1299 int
1316 1300 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx)
1317 1301 {
1318 1302 dmu_buf_impl_t *db, *db_next;
1319 1303 int err;
1320 1304
1321 1305 if (size == 0)
1322 1306 size = SPA_MINBLOCKSIZE;
1323 1307 if (size > SPA_MAXBLOCKSIZE)
1324 1308 size = SPA_MAXBLOCKSIZE;
1325 1309 else
1326 1310 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE);
1327 1311
1328 1312 if (ibs == dn->dn_indblkshift)
1329 1313 ibs = 0;
1330 1314
1331 1315 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0)
1332 1316 return (0);
1333 1317
1334 1318 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1335 1319
1336 1320 /* Check for any allocated blocks beyond the first */
1337 1321 if (dn->dn_maxblkid != 0)
1338 1322 goto fail;
1339 1323
1340 1324 mutex_enter(&dn->dn_dbufs_mtx);
1341 1325 for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
1342 1326 db_next = list_next(&dn->dn_dbufs, db);
1343 1327
1344 1328 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID &&
1345 1329 db->db_blkid != DMU_SPILL_BLKID) {
1346 1330 mutex_exit(&dn->dn_dbufs_mtx);
1347 1331 goto fail;
1348 1332 }
1349 1333 }
1350 1334 mutex_exit(&dn->dn_dbufs_mtx);
1351 1335
1352 1336 if (ibs && dn->dn_nlevels != 1)
1353 1337 goto fail;
1354 1338
1355 1339 /* resize the old block */
1356 1340 err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db);
1357 1341 if (err == 0)
1358 1342 dbuf_new_size(db, size, tx);
1359 1343 else if (err != ENOENT)
1360 1344 goto fail;
1361 1345
1362 1346 dnode_setdblksz(dn, size);
1363 1347 dnode_setdirty(dn, tx);
1364 1348 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size;
1365 1349 if (ibs) {
1366 1350 dn->dn_indblkshift = ibs;
1367 1351 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs;
1368 1352 }
1369 1353 /* rele after we have fixed the blocksize in the dnode */
1370 1354 if (db)
1371 1355 dbuf_rele(db, FTAG);
1372 1356
1373 1357 rw_exit(&dn->dn_struct_rwlock);
1374 1358 return (0);
1375 1359
1376 1360 fail:
1377 1361 rw_exit(&dn->dn_struct_rwlock);
1378 1362 return (SET_ERROR(ENOTSUP));
1379 1363 }
1380 1364
1381 1365 /* read-holding callers must not rely on the lock being continuously held */
1382 1366 void
1383 1367 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read)
1384 1368 {
1385 1369 uint64_t txgoff = tx->tx_txg & TXG_MASK;
1386 1370 int epbs, new_nlevels;
1387 1371 uint64_t sz;
1388 1372
1389 1373 ASSERT(blkid != DMU_BONUS_BLKID);
1390 1374
1391 1375 ASSERT(have_read ?
1392 1376 RW_READ_HELD(&dn->dn_struct_rwlock) :
1393 1377 RW_WRITE_HELD(&dn->dn_struct_rwlock));
1394 1378
1395 1379 /*
1396 1380 * if we have a read-lock, check to see if we need to do any work
1397 1381 * before upgrading to a write-lock.
1398 1382 */
1399 1383 if (have_read) {
1400 1384 if (blkid <= dn->dn_maxblkid)
1401 1385 return;
1402 1386
1403 1387 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
1404 1388 rw_exit(&dn->dn_struct_rwlock);
1405 1389 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1406 1390 }
1407 1391 }
1408 1392
1409 1393 if (blkid <= dn->dn_maxblkid)
1410 1394 goto out;
1411 1395
1412 1396 dn->dn_maxblkid = blkid;
1413 1397
1414 1398 /*
1415 1399 * Compute the number of levels necessary to support the new maxblkid.
1416 1400 */
1417 1401 new_nlevels = 1;
1418 1402 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1419 1403 for (sz = dn->dn_nblkptr;
1420 1404 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs)
1421 1405 new_nlevels++;
1422 1406
1423 1407 if (new_nlevels > dn->dn_nlevels) {
1424 1408 int old_nlevels = dn->dn_nlevels;
1425 1409 dmu_buf_impl_t *db;
1426 1410 list_t *list;
1427 1411 dbuf_dirty_record_t *new, *dr, *dr_next;
1428 1412
1429 1413 dn->dn_nlevels = new_nlevels;
1430 1414
1431 1415 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]);
1432 1416 dn->dn_next_nlevels[txgoff] = new_nlevels;
1433 1417
1434 1418 /* dirty the left indirects */
1435 1419 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG);
1436 1420 ASSERT(db != NULL);
1437 1421 new = dbuf_dirty(db, tx);
1438 1422 dbuf_rele(db, FTAG);
1439 1423
1440 1424 /* transfer the dirty records to the new indirect */
1441 1425 mutex_enter(&dn->dn_mtx);
1442 1426 mutex_enter(&new->dt.di.dr_mtx);
1443 1427 list = &dn->dn_dirty_records[txgoff];
1444 1428 for (dr = list_head(list); dr; dr = dr_next) {
1445 1429 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr);
1446 1430 if (dr->dr_dbuf->db_level != new_nlevels-1 &&
1447 1431 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
1448 1432 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
1449 1433 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1);
1450 1434 list_remove(&dn->dn_dirty_records[txgoff], dr);
1451 1435 list_insert_tail(&new->dt.di.dr_children, dr);
1452 1436 dr->dr_parent = new;
1453 1437 }
1454 1438 }
|
↓ open down ↓ |
683 lines elided |
↑ open up ↑ |
1455 1439 mutex_exit(&new->dt.di.dr_mtx);
1456 1440 mutex_exit(&dn->dn_mtx);
1457 1441 }
1458 1442
1459 1443 out:
1460 1444 if (have_read)
1461 1445 rw_downgrade(&dn->dn_struct_rwlock);
1462 1446 }
1463 1447
1464 1448 void
1465 -dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
1466 -{
1467 - avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1468 - avl_index_t where;
1469 - free_range_t *rp;
1470 - free_range_t rp_tofind;
1471 - uint64_t endblk = blkid + nblks;
1472 -
1473 - ASSERT(MUTEX_HELD(&dn->dn_mtx));
1474 - ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */
1475 -
1476 - dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1477 - blkid, nblks, tx->tx_txg);
1478 - rp_tofind.fr_blkid = blkid;
1479 - rp = avl_find(tree, &rp_tofind, &where);
1480 - if (rp == NULL)
1481 - rp = avl_nearest(tree, where, AVL_BEFORE);
1482 - if (rp == NULL)
1483 - rp = avl_nearest(tree, where, AVL_AFTER);
1484 -
1485 - while (rp && (rp->fr_blkid <= blkid + nblks)) {
1486 - uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks;
1487 - free_range_t *nrp = AVL_NEXT(tree, rp);
1488 -
1489 - if (blkid <= rp->fr_blkid && endblk >= fr_endblk) {
1490 - /* clear this entire range */
1491 - avl_remove(tree, rp);
1492 - kmem_free(rp, sizeof (free_range_t));
1493 - } else if (blkid <= rp->fr_blkid &&
1494 - endblk > rp->fr_blkid && endblk < fr_endblk) {
1495 - /* clear the beginning of this range */
1496 - rp->fr_blkid = endblk;
1497 - rp->fr_nblks = fr_endblk - endblk;
1498 - } else if (blkid > rp->fr_blkid && blkid < fr_endblk &&
1499 - endblk >= fr_endblk) {
1500 - /* clear the end of this range */
1501 - rp->fr_nblks = blkid - rp->fr_blkid;
1502 - } else if (blkid > rp->fr_blkid && endblk < fr_endblk) {
1503 - /* clear a chunk out of this range */
1504 - free_range_t *new_rp =
1505 - kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1506 -
1507 - new_rp->fr_blkid = endblk;
1508 - new_rp->fr_nblks = fr_endblk - endblk;
1509 - avl_insert_here(tree, new_rp, rp, AVL_AFTER);
1510 - rp->fr_nblks = blkid - rp->fr_blkid;
1511 - }
1512 - /* there may be no overlap */
1513 - rp = nrp;
1514 - }
1515 -}
1516 -
1517 -void
1518 1449 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx)
1519 1450 {
1520 1451 dmu_buf_impl_t *db;
1521 1452 uint64_t blkoff, blkid, nblks;
1522 1453 int blksz, blkshift, head, tail;
1523 1454 int trunc = FALSE;
1524 1455 int epbs;
1525 1456
1526 1457 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1527 1458 blksz = dn->dn_datablksz;
1528 1459 blkshift = dn->dn_datablkshift;
1529 1460 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
1530 1461
1531 1462 if (len == DMU_OBJECT_END) {
1532 1463 len = UINT64_MAX - off;
1533 1464 trunc = TRUE;
1534 1465 }
1535 1466
1536 1467 /*
1537 1468 * First, block align the region to free:
1538 1469 */
1539 1470 if (ISP2(blksz)) {
1540 1471 head = P2NPHASE(off, blksz);
1541 1472 blkoff = P2PHASE(off, blksz);
1542 1473 if ((off >> blkshift) > dn->dn_maxblkid)
1543 1474 goto out;
1544 1475 } else {
1545 1476 ASSERT(dn->dn_maxblkid == 0);
1546 1477 if (off == 0 && len >= blksz) {
1547 1478 /*
1548 1479 * Freeing the whole block; fast-track this request.
1549 1480 * Note that we won't dirty any indirect blocks,
1550 1481 * which is fine because we will be freeing the entire
1551 1482 * file and thus all indirect blocks will be freed
1552 1483 * by free_children().
1553 1484 */
1554 1485 blkid = 0;
1555 1486 nblks = 1;
1556 1487 goto done;
1557 1488 } else if (off >= blksz) {
1558 1489 /* Freeing past end-of-data */
1559 1490 goto out;
1560 1491 } else {
1561 1492 /* Freeing part of the block. */
1562 1493 head = blksz - off;
1563 1494 ASSERT3U(head, >, 0);
1564 1495 }
1565 1496 blkoff = off;
1566 1497 }
1567 1498 /* zero out any partial block data at the start of the range */
1568 1499 if (head) {
1569 1500 ASSERT3U(blkoff + head, ==, blksz);
1570 1501 if (len < head)
1571 1502 head = len;
1572 1503 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE,
1573 1504 FTAG, &db) == 0) {
1574 1505 caddr_t data;
1575 1506
1576 1507 /* don't dirty if it isn't on disk and isn't dirty */
1577 1508 if (db->db_last_dirty ||
1578 1509 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1579 1510 rw_exit(&dn->dn_struct_rwlock);
1580 1511 dmu_buf_will_dirty(&db->db, tx);
1581 1512 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1582 1513 data = db->db.db_data;
1583 1514 bzero(data + blkoff, head);
1584 1515 }
1585 1516 dbuf_rele(db, FTAG);
1586 1517 }
1587 1518 off += head;
1588 1519 len -= head;
1589 1520 }
1590 1521
1591 1522 /* If the range was less than one block, we're done */
1592 1523 if (len == 0)
1593 1524 goto out;
1594 1525
1595 1526 /* If the remaining range is past end of file, we're done */
1596 1527 if ((off >> blkshift) > dn->dn_maxblkid)
1597 1528 goto out;
1598 1529
1599 1530 ASSERT(ISP2(blksz));
1600 1531 if (trunc)
1601 1532 tail = 0;
1602 1533 else
1603 1534 tail = P2PHASE(len, blksz);
1604 1535
1605 1536 ASSERT0(P2PHASE(off, blksz));
1606 1537 /* zero out any partial block data at the end of the range */
1607 1538 if (tail) {
1608 1539 if (len < tail)
1609 1540 tail = len;
1610 1541 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len),
1611 1542 TRUE, FTAG, &db) == 0) {
1612 1543 /* don't dirty if not on disk and not dirty */
1613 1544 if (db->db_last_dirty ||
1614 1545 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) {
1615 1546 rw_exit(&dn->dn_struct_rwlock);
1616 1547 dmu_buf_will_dirty(&db->db, tx);
1617 1548 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
1618 1549 bzero(db->db.db_data, tail);
1619 1550 }
1620 1551 dbuf_rele(db, FTAG);
1621 1552 }
1622 1553 len -= tail;
1623 1554 }
1624 1555
1625 1556 /* If the range did not include a full block, we are done */
1626 1557 if (len == 0)
1627 1558 goto out;
1628 1559
1629 1560 ASSERT(IS_P2ALIGNED(off, blksz));
1630 1561 ASSERT(trunc || IS_P2ALIGNED(len, blksz));
1631 1562 blkid = off >> blkshift;
1632 1563 nblks = len >> blkshift;
1633 1564 if (trunc)
1634 1565 nblks += 1;
1635 1566
1636 1567 /*
1637 1568 * Dirty the first and last indirect blocks, as they (and/or their
1638 1569 * parents) will need to be written out if they were only
1639 1570 * partially freed. Interior indirect blocks will be themselves freed,
1640 1571 * by free_children(), so they need not be dirtied. Note that these
1641 1572 * interior blocks have already been prefetched by dmu_tx_hold_free().
1642 1573 */
1643 1574 if (dn->dn_nlevels > 1) {
1644 1575 uint64_t first, last;
1645 1576
1646 1577 first = blkid >> epbs;
1647 1578 if (db = dbuf_hold_level(dn, 1, first, FTAG)) {
1648 1579 dmu_buf_will_dirty(&db->db, tx);
1649 1580 dbuf_rele(db, FTAG);
1650 1581 }
1651 1582 if (trunc)
1652 1583 last = dn->dn_maxblkid >> epbs;
1653 1584 else
1654 1585 last = (blkid + nblks - 1) >> epbs;
1655 1586 if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) {
1656 1587 dmu_buf_will_dirty(&db->db, tx);
|
↓ open down ↓ |
129 lines elided |
↑ open up ↑ |
1657 1588 dbuf_rele(db, FTAG);
1658 1589 }
1659 1590 }
1660 1591
1661 1592 done:
1662 1593 /*
1663 1594 * Add this range to the dnode range list.
1664 1595 * We will finish up this free operation in the syncing phase.
1665 1596 */
1666 1597 mutex_enter(&dn->dn_mtx);
1667 - dnode_clear_range(dn, blkid, nblks, tx);
1668 - {
1669 - free_range_t *rp, *found;
1670 - avl_index_t where;
1671 - avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK];
1672 -
1673 - /* Add new range to dn_ranges */
1674 - rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP);
1675 - rp->fr_blkid = blkid;
1676 - rp->fr_nblks = nblks;
1677 - found = avl_find(tree, rp, &where);
1678 - ASSERT(found == NULL);
1679 - avl_insert(tree, rp, where);
1680 - dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1681 - blkid, nblks, tx->tx_txg);
1598 + int txgoff = tx->tx_txg & TXG_MASK;
1599 + if (dn->dn_free_ranges[txgoff] == NULL) {
1600 + dn->dn_free_ranges[txgoff] =
1601 + range_tree_create(NULL, NULL, &dn->dn_mtx);
1682 1602 }
1603 + range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks);
1604 + range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks);
1605 + dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n",
1606 + blkid, nblks, tx->tx_txg);
1683 1607 mutex_exit(&dn->dn_mtx);
1684 1608
1685 1609 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx);
1686 1610 dnode_setdirty(dn, tx);
1687 1611 out:
1688 1612
1689 1613 rw_exit(&dn->dn_struct_rwlock);
1690 1614 }
1691 1615
1692 1616 static boolean_t
1693 1617 dnode_spill_freed(dnode_t *dn)
1694 1618 {
1695 1619 int i;
1696 1620
1697 1621 mutex_enter(&dn->dn_mtx);
1698 1622 for (i = 0; i < TXG_SIZE; i++) {
1699 1623 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK)
|
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
1700 1624 break;
1701 1625 }
1702 1626 mutex_exit(&dn->dn_mtx);
1703 1627 return (i < TXG_SIZE);
1704 1628 }
1705 1629
1706 1630 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */
1707 1631 uint64_t
1708 1632 dnode_block_freed(dnode_t *dn, uint64_t blkid)
1709 1633 {
1710 - free_range_t range_tofind;
1711 1634 void *dp = spa_get_dsl(dn->dn_objset->os_spa);
1712 1635 int i;
1713 1636
1714 1637 if (blkid == DMU_BONUS_BLKID)
1715 1638 return (FALSE);
1716 1639
1717 1640 /*
1718 1641 * If we're in the process of opening the pool, dp will not be
1719 1642 * set yet, but there shouldn't be anything dirty.
1720 1643 */
1721 1644 if (dp == NULL)
1722 1645 return (FALSE);
1723 1646
1724 1647 if (dn->dn_free_txg)
1725 1648 return (TRUE);
1726 1649
1727 1650 if (blkid == DMU_SPILL_BLKID)
1728 1651 return (dnode_spill_freed(dn));
1729 1652
1730 - range_tofind.fr_blkid = blkid;
1731 1653 mutex_enter(&dn->dn_mtx);
1732 1654 for (i = 0; i < TXG_SIZE; i++) {
1733 - free_range_t *range_found;
1734 - avl_index_t idx;
1735 -
1736 - range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx);
1737 - if (range_found) {
1738 - ASSERT(range_found->fr_nblks > 0);
1655 + if (dn->dn_free_ranges[i] != NULL &&
1656 + range_tree_contains(dn->dn_free_ranges[i], blkid, 1))
1739 1657 break;
1740 - }
1741 - range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE);
1742 - if (range_found &&
1743 - range_found->fr_blkid + range_found->fr_nblks > blkid)
1744 - break;
1745 1658 }
1746 1659 mutex_exit(&dn->dn_mtx);
1747 1660 return (i < TXG_SIZE);
1748 1661 }
1749 1662
1750 1663 /* call from syncing context when we actually write/free space for this dnode */
1751 1664 void
1752 1665 dnode_diduse_space(dnode_t *dn, int64_t delta)
1753 1666 {
1754 1667 uint64_t space;
1755 1668 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n",
1756 1669 dn, dn->dn_phys,
1757 1670 (u_longlong_t)dn->dn_phys->dn_used,
1758 1671 (longlong_t)delta);
1759 1672
1760 1673 mutex_enter(&dn->dn_mtx);
1761 1674 space = DN_USED_BYTES(dn->dn_phys);
1762 1675 if (delta > 0) {
1763 1676 ASSERT3U(space + delta, >=, space); /* no overflow */
1764 1677 } else {
1765 1678 ASSERT3U(space, >=, -delta); /* no underflow */
1766 1679 }
1767 1680 space += delta;
1768 1681 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) {
1769 1682 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0);
1770 1683 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT));
1771 1684 dn->dn_phys->dn_used = space >> DEV_BSHIFT;
1772 1685 } else {
1773 1686 dn->dn_phys->dn_used = space;
1774 1687 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES;
1775 1688 }
1776 1689 mutex_exit(&dn->dn_mtx);
1777 1690 }
1778 1691
1779 1692 /*
1780 1693 * Call when we think we're going to write/free space in open context to track
1781 1694 * the amount of memory in use by the currently open txg.
1782 1695 */
1783 1696 void
1784 1697 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx)
1785 1698 {
1786 1699 objset_t *os = dn->dn_objset;
1787 1700 dsl_dataset_t *ds = os->os_dsl_dataset;
1788 1701 int64_t aspace = spa_get_asize(os->os_spa, space);
1789 1702
1790 1703 if (ds != NULL) {
1791 1704 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
1792 1705 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
1793 1706 }
1794 1707
1795 1708 dmu_tx_willuse_space(tx, aspace);
1796 1709 }
1797 1710
1798 1711 /*
1799 1712 * Scans a block at the indicated "level" looking for a hole or data,
1800 1713 * depending on 'flags'.
1801 1714 *
1802 1715 * If level > 0, then we are scanning an indirect block looking at its
1803 1716 * pointers. If level == 0, then we are looking at a block of dnodes.
1804 1717 *
1805 1718 * If we don't find what we are looking for in the block, we return ESRCH.
1806 1719 * Otherwise, return with *offset pointing to the beginning (if searching
1807 1720 * forwards) or end (if searching backwards) of the range covered by the
1808 1721 * block pointer we matched on (or dnode).
1809 1722 *
1810 1723 * The basic search algorithm used below by dnode_next_offset() is to
1811 1724 * use this function to search up the block tree (widen the search) until
1812 1725 * we find something (i.e., we don't return ESRCH) and then search back
1813 1726 * down the tree (narrow the search) until we reach our original search
1814 1727 * level.
1815 1728 */
1816 1729 static int
1817 1730 dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset,
1818 1731 int lvl, uint64_t blkfill, uint64_t txg)
1819 1732 {
1820 1733 dmu_buf_impl_t *db = NULL;
1821 1734 void *data = NULL;
1822 1735 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
1823 1736 uint64_t epb = 1ULL << epbs;
1824 1737 uint64_t minfill, maxfill;
1825 1738 boolean_t hole;
1826 1739 int i, inc, error, span;
1827 1740
1828 1741 dprintf("probing object %llu offset %llx level %d of %u\n",
1829 1742 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels);
1830 1743
1831 1744 hole = ((flags & DNODE_FIND_HOLE) != 0);
1832 1745 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1;
1833 1746 ASSERT(txg == 0 || !hole);
1834 1747
1835 1748 if (lvl == dn->dn_phys->dn_nlevels) {
1836 1749 error = 0;
1837 1750 epb = dn->dn_phys->dn_nblkptr;
1838 1751 data = dn->dn_phys->dn_blkptr;
1839 1752 } else {
1840 1753 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl);
1841 1754 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db);
1842 1755 if (error) {
1843 1756 if (error != ENOENT)
1844 1757 return (error);
1845 1758 if (hole)
1846 1759 return (0);
1847 1760 /*
1848 1761 * This can only happen when we are searching up
1849 1762 * the block tree for data. We don't really need to
1850 1763 * adjust the offset, as we will just end up looking
1851 1764 * at the pointer to this block in its parent, and its
1852 1765 * going to be unallocated, so we will skip over it.
1853 1766 */
1854 1767 return (SET_ERROR(ESRCH));
1855 1768 }
1856 1769 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT);
1857 1770 if (error) {
1858 1771 dbuf_rele(db, FTAG);
1859 1772 return (error);
1860 1773 }
1861 1774 data = db->db.db_data;
1862 1775 }
1863 1776
1864 1777
1865 1778 if (db != NULL && txg != 0 && (db->db_blkptr == NULL ||
1866 1779 db->db_blkptr->blk_birth <= txg ||
1867 1780 BP_IS_HOLE(db->db_blkptr))) {
1868 1781 /*
1869 1782 * This can only happen when we are searching up the tree
1870 1783 * and these conditions mean that we need to keep climbing.
1871 1784 */
1872 1785 error = SET_ERROR(ESRCH);
1873 1786 } else if (lvl == 0) {
1874 1787 dnode_phys_t *dnp = data;
1875 1788 span = DNODE_SHIFT;
1876 1789 ASSERT(dn->dn_type == DMU_OT_DNODE);
1877 1790
1878 1791 for (i = (*offset >> span) & (blkfill - 1);
1879 1792 i >= 0 && i < blkfill; i += inc) {
1880 1793 if ((dnp[i].dn_type == DMU_OT_NONE) == hole)
1881 1794 break;
1882 1795 *offset += (1ULL << span) * inc;
1883 1796 }
1884 1797 if (i < 0 || i == blkfill)
1885 1798 error = SET_ERROR(ESRCH);
1886 1799 } else {
1887 1800 blkptr_t *bp = data;
1888 1801 uint64_t start = *offset;
1889 1802 span = (lvl - 1) * epbs + dn->dn_datablkshift;
1890 1803 minfill = 0;
1891 1804 maxfill = blkfill << ((lvl - 1) * epbs);
1892 1805
1893 1806 if (hole)
1894 1807 maxfill--;
1895 1808 else
1896 1809 minfill++;
1897 1810
1898 1811 *offset = *offset >> span;
1899 1812 for (i = BF64_GET(*offset, 0, epbs);
1900 1813 i >= 0 && i < epb; i += inc) {
1901 1814 if (bp[i].blk_fill >= minfill &&
1902 1815 bp[i].blk_fill <= maxfill &&
1903 1816 (hole || bp[i].blk_birth > txg))
1904 1817 break;
1905 1818 if (inc > 0 || *offset > 0)
1906 1819 *offset += inc;
1907 1820 }
1908 1821 *offset = *offset << span;
1909 1822 if (inc < 0) {
1910 1823 /* traversing backwards; position offset at the end */
1911 1824 ASSERT3U(*offset, <=, start);
1912 1825 *offset = MIN(*offset + (1ULL << span) - 1, start);
1913 1826 } else if (*offset < start) {
1914 1827 *offset = start;
1915 1828 }
1916 1829 if (i < 0 || i >= epb)
1917 1830 error = SET_ERROR(ESRCH);
1918 1831 }
1919 1832
1920 1833 if (db)
1921 1834 dbuf_rele(db, FTAG);
1922 1835
1923 1836 return (error);
1924 1837 }
1925 1838
1926 1839 /*
1927 1840 * Find the next hole, data, or sparse region at or after *offset.
1928 1841 * The value 'blkfill' tells us how many items we expect to find
1929 1842 * in an L0 data block; this value is 1 for normal objects,
1930 1843 * DNODES_PER_BLOCK for the meta dnode, and some fraction of
1931 1844 * DNODES_PER_BLOCK when searching for sparse regions thereof.
1932 1845 *
1933 1846 * Examples:
1934 1847 *
1935 1848 * dnode_next_offset(dn, flags, offset, 1, 1, 0);
1936 1849 * Finds the next/previous hole/data in a file.
1937 1850 * Used in dmu_offset_next().
1938 1851 *
1939 1852 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg);
1940 1853 * Finds the next free/allocated dnode an objset's meta-dnode.
1941 1854 * Only finds objects that have new contents since txg (ie.
1942 1855 * bonus buffer changes and content removal are ignored).
1943 1856 * Used in dmu_object_next().
1944 1857 *
1945 1858 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0);
1946 1859 * Finds the next L2 meta-dnode bp that's at most 1/4 full.
1947 1860 * Used in dmu_object_alloc().
1948 1861 */
1949 1862 int
1950 1863 dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset,
1951 1864 int minlvl, uint64_t blkfill, uint64_t txg)
1952 1865 {
1953 1866 uint64_t initial_offset = *offset;
1954 1867 int lvl, maxlvl;
1955 1868 int error = 0;
1956 1869
1957 1870 if (!(flags & DNODE_FIND_HAVELOCK))
1958 1871 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1959 1872
1960 1873 if (dn->dn_phys->dn_nlevels == 0) {
1961 1874 error = SET_ERROR(ESRCH);
1962 1875 goto out;
1963 1876 }
1964 1877
1965 1878 if (dn->dn_datablkshift == 0) {
1966 1879 if (*offset < dn->dn_datablksz) {
1967 1880 if (flags & DNODE_FIND_HOLE)
1968 1881 *offset = dn->dn_datablksz;
1969 1882 } else {
1970 1883 error = SET_ERROR(ESRCH);
1971 1884 }
1972 1885 goto out;
1973 1886 }
1974 1887
1975 1888 maxlvl = dn->dn_phys->dn_nlevels;
1976 1889
1977 1890 for (lvl = minlvl; lvl <= maxlvl; lvl++) {
1978 1891 error = dnode_next_offset_level(dn,
1979 1892 flags, offset, lvl, blkfill, txg);
1980 1893 if (error != ESRCH)
1981 1894 break;
1982 1895 }
1983 1896
1984 1897 while (error == 0 && --lvl >= minlvl) {
1985 1898 error = dnode_next_offset_level(dn,
1986 1899 flags, offset, lvl, blkfill, txg);
1987 1900 }
1988 1901
1989 1902 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ?
1990 1903 initial_offset < *offset : initial_offset > *offset))
1991 1904 error = SET_ERROR(ESRCH);
1992 1905 out:
1993 1906 if (!(flags & DNODE_FIND_HAVELOCK))
1994 1907 rw_exit(&dn->dn_struct_rwlock);
1995 1908
1996 1909 return (error);
1997 1910 }
|
↓ open down ↓ |
243 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX