Print this page
3741 zfs needs better comments
Submitted by: Will Andrews <willa@spectralogic.com>
Submitted by: Justin Gibbs <justing@spectralogic.com>
Submitted by: Alan Somers <alans@spectralogic.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/dmu.c
+++ new/usr/src/uts/common/fs/zfs/dmu.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 (c) 2013 by Delphix. All rights reserved.
24 24 */
25 25
26 26 #include <sys/dmu.h>
27 27 #include <sys/dmu_impl.h>
28 28 #include <sys/dmu_tx.h>
29 29 #include <sys/dbuf.h>
30 30 #include <sys/dnode.h>
31 31 #include <sys/zfs_context.h>
32 32 #include <sys/dmu_objset.h>
33 33 #include <sys/dmu_traverse.h>
34 34 #include <sys/dsl_dataset.h>
35 35 #include <sys/dsl_dir.h>
36 36 #include <sys/dsl_pool.h>
37 37 #include <sys/dsl_synctask.h>
38 38 #include <sys/dsl_prop.h>
39 39 #include <sys/dmu_zfetch.h>
40 40 #include <sys/zfs_ioctl.h>
41 41 #include <sys/zap.h>
42 42 #include <sys/zio_checksum.h>
43 43 #include <sys/zio_compress.h>
44 44 #include <sys/sa.h>
45 45 #ifdef _KERNEL
46 46 #include <sys/vmsystm.h>
47 47 #include <sys/zfs_znode.h>
48 48 #endif
49 49
50 50 /*
51 51 * Enable/disable nopwrite feature.
52 52 */
53 53 int zfs_nopwrite_enabled = 1;
54 54
55 55 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
56 56 { DMU_BSWAP_UINT8, TRUE, "unallocated" },
57 57 { DMU_BSWAP_ZAP, TRUE, "object directory" },
58 58 { DMU_BSWAP_UINT64, TRUE, "object array" },
59 59 { DMU_BSWAP_UINT8, TRUE, "packed nvlist" },
60 60 { DMU_BSWAP_UINT64, TRUE, "packed nvlist size" },
61 61 { DMU_BSWAP_UINT64, TRUE, "bpobj" },
62 62 { DMU_BSWAP_UINT64, TRUE, "bpobj header" },
63 63 { DMU_BSWAP_UINT64, TRUE, "SPA space map header" },
64 64 { DMU_BSWAP_UINT64, TRUE, "SPA space map" },
65 65 { DMU_BSWAP_UINT64, TRUE, "ZIL intent log" },
66 66 { DMU_BSWAP_DNODE, TRUE, "DMU dnode" },
67 67 { DMU_BSWAP_OBJSET, TRUE, "DMU objset" },
68 68 { DMU_BSWAP_UINT64, TRUE, "DSL directory" },
69 69 { DMU_BSWAP_ZAP, TRUE, "DSL directory child map"},
70 70 { DMU_BSWAP_ZAP, TRUE, "DSL dataset snap map" },
71 71 { DMU_BSWAP_ZAP, TRUE, "DSL props" },
72 72 { DMU_BSWAP_UINT64, TRUE, "DSL dataset" },
73 73 { DMU_BSWAP_ZNODE, TRUE, "ZFS znode" },
74 74 { DMU_BSWAP_OLDACL, TRUE, "ZFS V0 ACL" },
75 75 { DMU_BSWAP_UINT8, FALSE, "ZFS plain file" },
76 76 { DMU_BSWAP_ZAP, TRUE, "ZFS directory" },
77 77 { DMU_BSWAP_ZAP, TRUE, "ZFS master node" },
78 78 { DMU_BSWAP_ZAP, TRUE, "ZFS delete queue" },
79 79 { DMU_BSWAP_UINT8, FALSE, "zvol object" },
80 80 { DMU_BSWAP_ZAP, TRUE, "zvol prop" },
81 81 { DMU_BSWAP_UINT8, FALSE, "other uint8[]" },
82 82 { DMU_BSWAP_UINT64, FALSE, "other uint64[]" },
83 83 { DMU_BSWAP_ZAP, TRUE, "other ZAP" },
84 84 { DMU_BSWAP_ZAP, TRUE, "persistent error log" },
85 85 { DMU_BSWAP_UINT8, TRUE, "SPA history" },
86 86 { DMU_BSWAP_UINT64, TRUE, "SPA history offsets" },
87 87 { DMU_BSWAP_ZAP, TRUE, "Pool properties" },
88 88 { DMU_BSWAP_ZAP, TRUE, "DSL permissions" },
89 89 { DMU_BSWAP_ACL, TRUE, "ZFS ACL" },
90 90 { DMU_BSWAP_UINT8, TRUE, "ZFS SYSACL" },
91 91 { DMU_BSWAP_UINT8, TRUE, "FUID table" },
92 92 { DMU_BSWAP_UINT64, TRUE, "FUID table size" },
93 93 { DMU_BSWAP_ZAP, TRUE, "DSL dataset next clones"},
94 94 { DMU_BSWAP_ZAP, TRUE, "scan work queue" },
95 95 { DMU_BSWAP_ZAP, TRUE, "ZFS user/group used" },
96 96 { DMU_BSWAP_ZAP, TRUE, "ZFS user/group quota" },
97 97 { DMU_BSWAP_ZAP, TRUE, "snapshot refcount tags"},
98 98 { DMU_BSWAP_ZAP, TRUE, "DDT ZAP algorithm" },
99 99 { DMU_BSWAP_ZAP, TRUE, "DDT statistics" },
100 100 { DMU_BSWAP_UINT8, TRUE, "System attributes" },
101 101 { DMU_BSWAP_ZAP, TRUE, "SA master node" },
102 102 { DMU_BSWAP_ZAP, TRUE, "SA attr registration" },
103 103 { DMU_BSWAP_ZAP, TRUE, "SA attr layouts" },
104 104 { DMU_BSWAP_ZAP, TRUE, "scan translations" },
105 105 { DMU_BSWAP_UINT8, FALSE, "deduplicated block" },
106 106 { DMU_BSWAP_ZAP, TRUE, "DSL deadlist map" },
107 107 { DMU_BSWAP_UINT64, TRUE, "DSL deadlist map hdr" },
108 108 { DMU_BSWAP_ZAP, TRUE, "DSL dir clones" },
109 109 { DMU_BSWAP_UINT64, TRUE, "bpobj subobj" }
110 110 };
111 111
112 112 const dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {
113 113 { byteswap_uint8_array, "uint8" },
114 114 { byteswap_uint16_array, "uint16" },
115 115 { byteswap_uint32_array, "uint32" },
116 116 { byteswap_uint64_array, "uint64" },
117 117 { zap_byteswap, "zap" },
118 118 { dnode_buf_byteswap, "dnode" },
119 119 { dmu_objset_byteswap, "objset" },
120 120 { zfs_znode_byteswap, "znode" },
121 121 { zfs_oldacl_byteswap, "oldacl" },
122 122 { zfs_acl_byteswap, "acl" }
123 123 };
124 124
125 125 int
126 126 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
127 127 void *tag, dmu_buf_t **dbp, int flags)
128 128 {
129 129 dnode_t *dn;
130 130 uint64_t blkid;
131 131 dmu_buf_impl_t *db;
132 132 int err;
133 133 int db_flags = DB_RF_CANFAIL;
134 134
135 135 if (flags & DMU_READ_NO_PREFETCH)
136 136 db_flags |= DB_RF_NOPREFETCH;
137 137
138 138 err = dnode_hold(os, object, FTAG, &dn);
139 139 if (err)
140 140 return (err);
141 141 blkid = dbuf_whichblock(dn, offset);
142 142 rw_enter(&dn->dn_struct_rwlock, RW_READER);
143 143 db = dbuf_hold(dn, blkid, tag);
144 144 rw_exit(&dn->dn_struct_rwlock);
145 145 if (db == NULL) {
146 146 err = SET_ERROR(EIO);
147 147 } else {
148 148 err = dbuf_read(db, NULL, db_flags);
149 149 if (err) {
150 150 dbuf_rele(db, tag);
151 151 db = NULL;
152 152 }
153 153 }
154 154
155 155 dnode_rele(dn, FTAG);
156 156 *dbp = &db->db; /* NULL db plus first field offset is NULL */
157 157 return (err);
158 158 }
159 159
160 160 int
161 161 dmu_bonus_max(void)
162 162 {
163 163 return (DN_MAX_BONUSLEN);
164 164 }
165 165
166 166 int
167 167 dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)
168 168 {
169 169 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
170 170 dnode_t *dn;
171 171 int error;
172 172
173 173 DB_DNODE_ENTER(db);
174 174 dn = DB_DNODE(db);
175 175
176 176 if (dn->dn_bonus != db) {
177 177 error = SET_ERROR(EINVAL);
178 178 } else if (newsize < 0 || newsize > db_fake->db_size) {
179 179 error = SET_ERROR(EINVAL);
180 180 } else {
181 181 dnode_setbonuslen(dn, newsize, tx);
182 182 error = 0;
183 183 }
184 184
185 185 DB_DNODE_EXIT(db);
186 186 return (error);
187 187 }
188 188
189 189 int
190 190 dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)
191 191 {
192 192 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
193 193 dnode_t *dn;
194 194 int error;
195 195
196 196 DB_DNODE_ENTER(db);
197 197 dn = DB_DNODE(db);
198 198
199 199 if (!DMU_OT_IS_VALID(type)) {
200 200 error = SET_ERROR(EINVAL);
201 201 } else if (dn->dn_bonus != db) {
202 202 error = SET_ERROR(EINVAL);
203 203 } else {
204 204 dnode_setbonus_type(dn, type, tx);
205 205 error = 0;
206 206 }
207 207
208 208 DB_DNODE_EXIT(db);
209 209 return (error);
210 210 }
211 211
212 212 dmu_object_type_t
213 213 dmu_get_bonustype(dmu_buf_t *db_fake)
214 214 {
215 215 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
216 216 dnode_t *dn;
217 217 dmu_object_type_t type;
218 218
219 219 DB_DNODE_ENTER(db);
220 220 dn = DB_DNODE(db);
221 221 type = dn->dn_bonustype;
222 222 DB_DNODE_EXIT(db);
223 223
224 224 return (type);
225 225 }
226 226
227 227 int
228 228 dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
229 229 {
230 230 dnode_t *dn;
231 231 int error;
232 232
233 233 error = dnode_hold(os, object, FTAG, &dn);
234 234 dbuf_rm_spill(dn, tx);
235 235 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
236 236 dnode_rm_spill(dn, tx);
237 237 rw_exit(&dn->dn_struct_rwlock);
238 238 dnode_rele(dn, FTAG);
239 239 return (error);
240 240 }
241 241
242 242 /*
243 243 * returns ENOENT, EIO, or 0.
244 244 */
245 245 int
246 246 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
247 247 {
248 248 dnode_t *dn;
249 249 dmu_buf_impl_t *db;
250 250 int error;
251 251
252 252 error = dnode_hold(os, object, FTAG, &dn);
253 253 if (error)
254 254 return (error);
255 255
256 256 rw_enter(&dn->dn_struct_rwlock, RW_READER);
257 257 if (dn->dn_bonus == NULL) {
258 258 rw_exit(&dn->dn_struct_rwlock);
259 259 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
260 260 if (dn->dn_bonus == NULL)
261 261 dbuf_create_bonus(dn);
262 262 }
263 263 db = dn->dn_bonus;
264 264
265 265 /* as long as the bonus buf is held, the dnode will be held */
266 266 if (refcount_add(&db->db_holds, tag) == 1) {
267 267 VERIFY(dnode_add_ref(dn, db));
268 268 (void) atomic_inc_32_nv(&dn->dn_dbufs_count);
269 269 }
270 270
271 271 /*
272 272 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
273 273 * hold and incrementing the dbuf count to ensure that dnode_move() sees
274 274 * a dnode hold for every dbuf.
275 275 */
276 276 rw_exit(&dn->dn_struct_rwlock);
277 277
278 278 dnode_rele(dn, FTAG);
279 279
280 280 VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH));
281 281
282 282 *dbp = &db->db;
283 283 return (0);
284 284 }
285 285
286 286 /*
287 287 * returns ENOENT, EIO, or 0.
288 288 *
289 289 * This interface will allocate a blank spill dbuf when a spill blk
290 290 * doesn't already exist on the dnode.
291 291 *
292 292 * if you only want to find an already existing spill db, then
293 293 * dmu_spill_hold_existing() should be used.
294 294 */
295 295 int
296 296 dmu_spill_hold_by_dnode(dnode_t *dn, uint32_t flags, void *tag, dmu_buf_t **dbp)
297 297 {
298 298 dmu_buf_impl_t *db = NULL;
299 299 int err;
300 300
301 301 if ((flags & DB_RF_HAVESTRUCT) == 0)
302 302 rw_enter(&dn->dn_struct_rwlock, RW_READER);
303 303
304 304 db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
305 305
306 306 if ((flags & DB_RF_HAVESTRUCT) == 0)
307 307 rw_exit(&dn->dn_struct_rwlock);
308 308
309 309 ASSERT(db != NULL);
310 310 err = dbuf_read(db, NULL, flags);
311 311 if (err == 0)
312 312 *dbp = &db->db;
313 313 else
314 314 dbuf_rele(db, tag);
315 315 return (err);
316 316 }
317 317
318 318 int
319 319 dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
320 320 {
321 321 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
322 322 dnode_t *dn;
323 323 int err;
324 324
325 325 DB_DNODE_ENTER(db);
326 326 dn = DB_DNODE(db);
327 327
328 328 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {
329 329 err = SET_ERROR(EINVAL);
330 330 } else {
331 331 rw_enter(&dn->dn_struct_rwlock, RW_READER);
332 332
333 333 if (!dn->dn_have_spill) {
334 334 err = SET_ERROR(ENOENT);
335 335 } else {
336 336 err = dmu_spill_hold_by_dnode(dn,
337 337 DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);
338 338 }
339 339
340 340 rw_exit(&dn->dn_struct_rwlock);
341 341 }
342 342
343 343 DB_DNODE_EXIT(db);
344 344 return (err);
345 345 }
346 346
347 347 int
348 348 dmu_spill_hold_by_bonus(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp)
349 349 {
350 350 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
351 351 dnode_t *dn;
352 352 int err;
353 353
354 354 DB_DNODE_ENTER(db);
355 355 dn = DB_DNODE(db);
356 356 err = dmu_spill_hold_by_dnode(dn, DB_RF_CANFAIL, tag, dbp);
357 357 DB_DNODE_EXIT(db);
358 358
359 359 return (err);
360 360 }
361 361
362 362 /*
363 363 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
364 364 * to take a held dnode rather than <os, object> -- the lookup is wasteful,
365 365 * and can induce severe lock contention when writing to several files
366 366 * whose dnodes are in the same block.
367 367 */
368 368 static int
369 369 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
370 370 int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp, uint32_t flags)
371 371 {
372 372 dsl_pool_t *dp = NULL;
373 373 dmu_buf_t **dbp;
374 374 uint64_t blkid, nblks, i;
375 375 uint32_t dbuf_flags;
376 376 int err;
377 377 zio_t *zio;
378 378 hrtime_t start;
379 379
380 380 ASSERT(length <= DMU_MAX_ACCESS);
381 381
382 382 dbuf_flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT;
383 383 if (flags & DMU_READ_NO_PREFETCH || length > zfetch_array_rd_sz)
384 384 dbuf_flags |= DB_RF_NOPREFETCH;
385 385
386 386 rw_enter(&dn->dn_struct_rwlock, RW_READER);
387 387 if (dn->dn_datablkshift) {
388 388 int blkshift = dn->dn_datablkshift;
389 389 nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
390 390 P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
391 391 } else {
392 392 if (offset + length > dn->dn_datablksz) {
393 393 zfs_panic_recover("zfs: accessing past end of object "
394 394 "%llx/%llx (size=%u access=%llu+%llu)",
395 395 (longlong_t)dn->dn_objset->
396 396 os_dsl_dataset->ds_object,
397 397 (longlong_t)dn->dn_object, dn->dn_datablksz,
398 398 (longlong_t)offset, (longlong_t)length);
399 399 rw_exit(&dn->dn_struct_rwlock);
400 400 return (SET_ERROR(EIO));
401 401 }
402 402 nblks = 1;
403 403 }
404 404 dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
405 405
406 406 if (dn->dn_objset->os_dsl_dataset)
407 407 dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool;
408 408 start = gethrtime();
409 409 zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
410 410 blkid = dbuf_whichblock(dn, offset);
411 411 for (i = 0; i < nblks; i++) {
412 412 dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
413 413 if (db == NULL) {
414 414 rw_exit(&dn->dn_struct_rwlock);
415 415 dmu_buf_rele_array(dbp, nblks, tag);
416 416 zio_nowait(zio);
417 417 return (SET_ERROR(EIO));
418 418 }
419 419 /* initiate async i/o */
420 420 if (read) {
421 421 (void) dbuf_read(db, zio, dbuf_flags);
422 422 }
423 423 dbp[i] = &db->db;
424 424 }
425 425 rw_exit(&dn->dn_struct_rwlock);
426 426
427 427 /* wait for async i/o */
428 428 err = zio_wait(zio);
429 429 /* track read overhead when we are in sync context */
430 430 if (dp && dsl_pool_sync_context(dp))
431 431 dp->dp_read_overhead += gethrtime() - start;
432 432 if (err) {
433 433 dmu_buf_rele_array(dbp, nblks, tag);
434 434 return (err);
435 435 }
436 436
437 437 /* wait for other io to complete */
438 438 if (read) {
439 439 for (i = 0; i < nblks; i++) {
440 440 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
441 441 mutex_enter(&db->db_mtx);
442 442 while (db->db_state == DB_READ ||
443 443 db->db_state == DB_FILL)
444 444 cv_wait(&db->db_changed, &db->db_mtx);
445 445 if (db->db_state == DB_UNCACHED)
446 446 err = SET_ERROR(EIO);
447 447 mutex_exit(&db->db_mtx);
448 448 if (err) {
449 449 dmu_buf_rele_array(dbp, nblks, tag);
450 450 return (err);
451 451 }
452 452 }
453 453 }
454 454
455 455 *numbufsp = nblks;
456 456 *dbpp = dbp;
457 457 return (0);
458 458 }
459 459
460 460 static int
461 461 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
462 462 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
463 463 {
464 464 dnode_t *dn;
465 465 int err;
466 466
467 467 err = dnode_hold(os, object, FTAG, &dn);
468 468 if (err)
469 469 return (err);
470 470
471 471 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
472 472 numbufsp, dbpp, DMU_READ_PREFETCH);
473 473
474 474 dnode_rele(dn, FTAG);
475 475
476 476 return (err);
477 477 }
478 478
479 479 int
480 480 dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,
481 481 uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
482 482 {
483 483 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
484 484 dnode_t *dn;
485 485 int err;
486 486
487 487 DB_DNODE_ENTER(db);
488 488 dn = DB_DNODE(db);
489 489 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
490 490 numbufsp, dbpp, DMU_READ_PREFETCH);
491 491 DB_DNODE_EXIT(db);
492 492
493 493 return (err);
494 494 }
495 495
496 496 void
497 497 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
498 498 {
499 499 int i;
500 500 dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
501 501
502 502 if (numbufs == 0)
503 503 return;
504 504
505 505 for (i = 0; i < numbufs; i++) {
506 506 if (dbp[i])
507 507 dbuf_rele(dbp[i], tag);
508 508 }
509 509
510 510 kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
511 511 }
512 512
513 513 void
514 514 dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
515 515 {
516 516 dnode_t *dn;
517 517 uint64_t blkid;
518 518 int nblks, i, err;
519 519
520 520 if (zfs_prefetch_disable)
521 521 return;
522 522
523 523 if (len == 0) { /* they're interested in the bonus buffer */
524 524 dn = DMU_META_DNODE(os);
525 525
526 526 if (object == 0 || object >= DN_MAX_OBJECT)
527 527 return;
528 528
529 529 rw_enter(&dn->dn_struct_rwlock, RW_READER);
530 530 blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
531 531 dbuf_prefetch(dn, blkid);
532 532 rw_exit(&dn->dn_struct_rwlock);
533 533 return;
534 534 }
535 535
536 536 /*
537 537 * XXX - Note, if the dnode for the requested object is not
538 538 * already cached, we will do a *synchronous* read in the
539 539 * dnode_hold() call. The same is true for any indirects.
540 540 */
541 541 err = dnode_hold(os, object, FTAG, &dn);
542 542 if (err != 0)
543 543 return;
544 544
545 545 rw_enter(&dn->dn_struct_rwlock, RW_READER);
546 546 if (dn->dn_datablkshift) {
547 547 int blkshift = dn->dn_datablkshift;
548 548 nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
549 549 P2ALIGN(offset, 1<<blkshift)) >> blkshift;
550 550 } else {
551 551 nblks = (offset < dn->dn_datablksz);
552 552 }
553 553
554 554 if (nblks != 0) {
555 555 blkid = dbuf_whichblock(dn, offset);
556 556 for (i = 0; i < nblks; i++)
557 557 dbuf_prefetch(dn, blkid+i);
558 558 }
559 559
560 560 rw_exit(&dn->dn_struct_rwlock);
561 561
562 562 dnode_rele(dn, FTAG);
563 563 }
564 564
565 565 /*
566 566 * Get the next "chunk" of file data to free. We traverse the file from
567 567 * the end so that the file gets shorter over time (if we crashes in the
568 568 * middle, this will leave us in a better state). We find allocated file
569 569 * data by simply searching the allocated level 1 indirects.
570 570 */
571 571 static int
572 572 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t limit)
573 573 {
574 574 uint64_t len = *start - limit;
575 575 uint64_t blkcnt = 0;
576 576 uint64_t maxblks = DMU_MAX_ACCESS / (1ULL << (dn->dn_indblkshift + 1));
577 577 uint64_t iblkrange =
578 578 dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
579 579
580 580 ASSERT(limit <= *start);
581 581
582 582 if (len <= iblkrange * maxblks) {
583 583 *start = limit;
584 584 return (0);
585 585 }
586 586 ASSERT(ISP2(iblkrange));
587 587
588 588 while (*start > limit && blkcnt < maxblks) {
589 589 int err;
590 590
591 591 /* find next allocated L1 indirect */
592 592 err = dnode_next_offset(dn,
593 593 DNODE_FIND_BACKWARDS, start, 2, 1, 0);
594 594
595 595 /* if there are no more, then we are done */
596 596 if (err == ESRCH) {
597 597 *start = limit;
598 598 return (0);
599 599 } else if (err) {
600 600 return (err);
601 601 }
602 602 blkcnt += 1;
603 603
604 604 /* reset offset to end of "next" block back */
605 605 *start = P2ALIGN(*start, iblkrange);
606 606 if (*start <= limit)
607 607 *start = limit;
608 608 else
609 609 *start -= 1;
610 610 }
611 611 return (0);
612 612 }
613 613
614 614 static int
615 615 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
616 616 uint64_t length, boolean_t free_dnode)
617 617 {
618 618 dmu_tx_t *tx;
619 619 uint64_t object_size, start, end, len;
620 620 boolean_t trunc = (length == DMU_OBJECT_END);
621 621 int align, err;
622 622
623 623 align = 1 << dn->dn_datablkshift;
624 624 ASSERT(align > 0);
625 625 object_size = align == 1 ? dn->dn_datablksz :
626 626 (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
627 627
628 628 end = offset + length;
629 629 if (trunc || end > object_size)
630 630 end = object_size;
631 631 if (end <= offset)
632 632 return (0);
633 633 length = end - offset;
634 634
635 635 while (length) {
636 636 start = end;
637 637 /* assert(offset <= start) */
638 638 err = get_next_chunk(dn, &start, offset);
639 639 if (err)
640 640 return (err);
641 641 len = trunc ? DMU_OBJECT_END : end - start;
642 642
643 643 tx = dmu_tx_create(os);
644 644 dmu_tx_hold_free(tx, dn->dn_object, start, len);
645 645 err = dmu_tx_assign(tx, TXG_WAIT);
646 646 if (err) {
647 647 dmu_tx_abort(tx);
648 648 return (err);
649 649 }
650 650
651 651 dnode_free_range(dn, start, trunc ? -1 : len, tx);
652 652
653 653 if (start == 0 && free_dnode) {
654 654 ASSERT(trunc);
655 655 dnode_free(dn, tx);
656 656 }
657 657
658 658 length -= end - start;
659 659
660 660 dmu_tx_commit(tx);
661 661 end = start;
662 662 }
663 663 return (0);
664 664 }
665 665
666 666 int
667 667 dmu_free_long_range(objset_t *os, uint64_t object,
668 668 uint64_t offset, uint64_t length)
669 669 {
670 670 dnode_t *dn;
671 671 int err;
672 672
673 673 err = dnode_hold(os, object, FTAG, &dn);
674 674 if (err != 0)
675 675 return (err);
676 676 err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
677 677 dnode_rele(dn, FTAG);
678 678 return (err);
679 679 }
680 680
681 681 int
682 682 dmu_free_object(objset_t *os, uint64_t object)
683 683 {
684 684 dnode_t *dn;
685 685 dmu_tx_t *tx;
686 686 int err;
687 687
688 688 err = dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED,
689 689 FTAG, &dn);
690 690 if (err != 0)
691 691 return (err);
692 692 if (dn->dn_nlevels == 1) {
693 693 tx = dmu_tx_create(os);
694 694 dmu_tx_hold_bonus(tx, object);
695 695 dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
696 696 err = dmu_tx_assign(tx, TXG_WAIT);
697 697 if (err == 0) {
698 698 dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
699 699 dnode_free(dn, tx);
700 700 dmu_tx_commit(tx);
701 701 } else {
702 702 dmu_tx_abort(tx);
703 703 }
704 704 } else {
705 705 err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
706 706 }
707 707 dnode_rele(dn, FTAG);
708 708 return (err);
709 709 }
710 710
711 711 int
712 712 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
713 713 uint64_t size, dmu_tx_t *tx)
714 714 {
715 715 dnode_t *dn;
716 716 int err = dnode_hold(os, object, FTAG, &dn);
717 717 if (err)
718 718 return (err);
719 719 ASSERT(offset < UINT64_MAX);
720 720 ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
721 721 dnode_free_range(dn, offset, size, tx);
722 722 dnode_rele(dn, FTAG);
723 723 return (0);
724 724 }
725 725
726 726 int
727 727 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
728 728 void *buf, uint32_t flags)
729 729 {
730 730 dnode_t *dn;
731 731 dmu_buf_t **dbp;
732 732 int numbufs, err;
733 733
734 734 err = dnode_hold(os, object, FTAG, &dn);
735 735 if (err)
736 736 return (err);
737 737
738 738 /*
739 739 * Deal with odd block sizes, where there can't be data past the first
740 740 * block. If we ever do the tail block optimization, we will need to
741 741 * handle that here as well.
742 742 */
743 743 if (dn->dn_maxblkid == 0) {
744 744 int newsz = offset > dn->dn_datablksz ? 0 :
745 745 MIN(size, dn->dn_datablksz - offset);
746 746 bzero((char *)buf + newsz, size - newsz);
747 747 size = newsz;
748 748 }
749 749
750 750 while (size > 0) {
751 751 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
752 752 int i;
753 753
754 754 /*
755 755 * NB: we could do this block-at-a-time, but it's nice
756 756 * to be reading in parallel.
757 757 */
758 758 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
759 759 TRUE, FTAG, &numbufs, &dbp, flags);
760 760 if (err)
761 761 break;
762 762
763 763 for (i = 0; i < numbufs; i++) {
764 764 int tocpy;
765 765 int bufoff;
766 766 dmu_buf_t *db = dbp[i];
767 767
768 768 ASSERT(size > 0);
769 769
770 770 bufoff = offset - db->db_offset;
771 771 tocpy = (int)MIN(db->db_size - bufoff, size);
772 772
773 773 bcopy((char *)db->db_data + bufoff, buf, tocpy);
774 774
775 775 offset += tocpy;
776 776 size -= tocpy;
777 777 buf = (char *)buf + tocpy;
778 778 }
779 779 dmu_buf_rele_array(dbp, numbufs, FTAG);
780 780 }
781 781 dnode_rele(dn, FTAG);
782 782 return (err);
783 783 }
784 784
785 785 void
786 786 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
787 787 const void *buf, dmu_tx_t *tx)
788 788 {
789 789 dmu_buf_t **dbp;
790 790 int numbufs, i;
791 791
792 792 if (size == 0)
793 793 return;
794 794
795 795 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
796 796 FALSE, FTAG, &numbufs, &dbp));
797 797
798 798 for (i = 0; i < numbufs; i++) {
799 799 int tocpy;
800 800 int bufoff;
801 801 dmu_buf_t *db = dbp[i];
802 802
803 803 ASSERT(size > 0);
804 804
805 805 bufoff = offset - db->db_offset;
806 806 tocpy = (int)MIN(db->db_size - bufoff, size);
807 807
808 808 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
809 809
810 810 if (tocpy == db->db_size)
811 811 dmu_buf_will_fill(db, tx);
812 812 else
813 813 dmu_buf_will_dirty(db, tx);
814 814
815 815 bcopy(buf, (char *)db->db_data + bufoff, tocpy);
816 816
817 817 if (tocpy == db->db_size)
818 818 dmu_buf_fill_done(db, tx);
819 819
820 820 offset += tocpy;
821 821 size -= tocpy;
822 822 buf = (char *)buf + tocpy;
823 823 }
824 824 dmu_buf_rele_array(dbp, numbufs, FTAG);
825 825 }
826 826
827 827 void
828 828 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
829 829 dmu_tx_t *tx)
830 830 {
831 831 dmu_buf_t **dbp;
832 832 int numbufs, i;
833 833
834 834 if (size == 0)
835 835 return;
836 836
837 837 VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
838 838 FALSE, FTAG, &numbufs, &dbp));
839 839
840 840 for (i = 0; i < numbufs; i++) {
841 841 dmu_buf_t *db = dbp[i];
842 842
843 843 dmu_buf_will_not_fill(db, tx);
844 844 }
845 845 dmu_buf_rele_array(dbp, numbufs, FTAG);
846 846 }
847 847
848 848 /*
849 849 * DMU support for xuio
850 850 */
851 851 kstat_t *xuio_ksp = NULL;
852 852
853 853 int
854 854 dmu_xuio_init(xuio_t *xuio, int nblk)
855 855 {
856 856 dmu_xuio_t *priv;
857 857 uio_t *uio = &xuio->xu_uio;
858 858
859 859 uio->uio_iovcnt = nblk;
860 860 uio->uio_iov = kmem_zalloc(nblk * sizeof (iovec_t), KM_SLEEP);
861 861
862 862 priv = kmem_zalloc(sizeof (dmu_xuio_t), KM_SLEEP);
863 863 priv->cnt = nblk;
864 864 priv->bufs = kmem_zalloc(nblk * sizeof (arc_buf_t *), KM_SLEEP);
865 865 priv->iovp = uio->uio_iov;
866 866 XUIO_XUZC_PRIV(xuio) = priv;
867 867
868 868 if (XUIO_XUZC_RW(xuio) == UIO_READ)
869 869 XUIOSTAT_INCR(xuiostat_onloan_rbuf, nblk);
870 870 else
871 871 XUIOSTAT_INCR(xuiostat_onloan_wbuf, nblk);
872 872
873 873 return (0);
874 874 }
875 875
876 876 void
877 877 dmu_xuio_fini(xuio_t *xuio)
878 878 {
879 879 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
880 880 int nblk = priv->cnt;
881 881
882 882 kmem_free(priv->iovp, nblk * sizeof (iovec_t));
883 883 kmem_free(priv->bufs, nblk * sizeof (arc_buf_t *));
884 884 kmem_free(priv, sizeof (dmu_xuio_t));
885 885
886 886 if (XUIO_XUZC_RW(xuio) == UIO_READ)
887 887 XUIOSTAT_INCR(xuiostat_onloan_rbuf, -nblk);
888 888 else
889 889 XUIOSTAT_INCR(xuiostat_onloan_wbuf, -nblk);
890 890 }
891 891
892 892 /*
893 893 * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
894 894 * and increase priv->next by 1.
895 895 */
896 896 int
897 897 dmu_xuio_add(xuio_t *xuio, arc_buf_t *abuf, offset_t off, size_t n)
898 898 {
899 899 struct iovec *iov;
900 900 uio_t *uio = &xuio->xu_uio;
901 901 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
902 902 int i = priv->next++;
903 903
904 904 ASSERT(i < priv->cnt);
905 905 ASSERT(off + n <= arc_buf_size(abuf));
906 906 iov = uio->uio_iov + i;
907 907 iov->iov_base = (char *)abuf->b_data + off;
908 908 iov->iov_len = n;
909 909 priv->bufs[i] = abuf;
910 910 return (0);
911 911 }
912 912
913 913 int
914 914 dmu_xuio_cnt(xuio_t *xuio)
915 915 {
916 916 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
917 917 return (priv->cnt);
918 918 }
919 919
920 920 arc_buf_t *
921 921 dmu_xuio_arcbuf(xuio_t *xuio, int i)
922 922 {
923 923 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
924 924
925 925 ASSERT(i < priv->cnt);
926 926 return (priv->bufs[i]);
927 927 }
928 928
929 929 void
930 930 dmu_xuio_clear(xuio_t *xuio, int i)
931 931 {
932 932 dmu_xuio_t *priv = XUIO_XUZC_PRIV(xuio);
933 933
934 934 ASSERT(i < priv->cnt);
935 935 priv->bufs[i] = NULL;
936 936 }
937 937
938 938 static void
939 939 xuio_stat_init(void)
940 940 {
941 941 xuio_ksp = kstat_create("zfs", 0, "xuio_stats", "misc",
942 942 KSTAT_TYPE_NAMED, sizeof (xuio_stats) / sizeof (kstat_named_t),
943 943 KSTAT_FLAG_VIRTUAL);
944 944 if (xuio_ksp != NULL) {
945 945 xuio_ksp->ks_data = &xuio_stats;
946 946 kstat_install(xuio_ksp);
947 947 }
948 948 }
949 949
950 950 static void
951 951 xuio_stat_fini(void)
952 952 {
953 953 if (xuio_ksp != NULL) {
954 954 kstat_delete(xuio_ksp);
955 955 xuio_ksp = NULL;
956 956 }
957 957 }
958 958
959 959 void
960 960 xuio_stat_wbuf_copied()
961 961 {
962 962 XUIOSTAT_BUMP(xuiostat_wbuf_copied);
963 963 }
964 964
965 965 void
966 966 xuio_stat_wbuf_nocopy()
967 967 {
968 968 XUIOSTAT_BUMP(xuiostat_wbuf_nocopy);
969 969 }
970 970
971 971 #ifdef _KERNEL
972 972 int
973 973 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
974 974 {
975 975 dmu_buf_t **dbp;
976 976 int numbufs, i, err;
977 977 xuio_t *xuio = NULL;
978 978
979 979 /*
980 980 * NB: we could do this block-at-a-time, but it's nice
981 981 * to be reading in parallel.
982 982 */
983 983 err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
984 984 &numbufs, &dbp);
985 985 if (err)
986 986 return (err);
987 987
988 988 if (uio->uio_extflg == UIO_XUIO)
989 989 xuio = (xuio_t *)uio;
990 990
991 991 for (i = 0; i < numbufs; i++) {
992 992 int tocpy;
993 993 int bufoff;
994 994 dmu_buf_t *db = dbp[i];
995 995
996 996 ASSERT(size > 0);
997 997
998 998 bufoff = uio->uio_loffset - db->db_offset;
999 999 tocpy = (int)MIN(db->db_size - bufoff, size);
1000 1000
1001 1001 if (xuio) {
1002 1002 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
1003 1003 arc_buf_t *dbuf_abuf = dbi->db_buf;
1004 1004 arc_buf_t *abuf = dbuf_loan_arcbuf(dbi);
1005 1005 err = dmu_xuio_add(xuio, abuf, bufoff, tocpy);
1006 1006 if (!err) {
1007 1007 uio->uio_resid -= tocpy;
1008 1008 uio->uio_loffset += tocpy;
1009 1009 }
1010 1010
1011 1011 if (abuf == dbuf_abuf)
1012 1012 XUIOSTAT_BUMP(xuiostat_rbuf_nocopy);
1013 1013 else
1014 1014 XUIOSTAT_BUMP(xuiostat_rbuf_copied);
1015 1015 } else {
1016 1016 err = uiomove((char *)db->db_data + bufoff, tocpy,
1017 1017 UIO_READ, uio);
1018 1018 }
1019 1019 if (err)
1020 1020 break;
1021 1021
1022 1022 size -= tocpy;
1023 1023 }
1024 1024 dmu_buf_rele_array(dbp, numbufs, FTAG);
1025 1025
1026 1026 return (err);
1027 1027 }
1028 1028
1029 1029 static int
1030 1030 dmu_write_uio_dnode(dnode_t *dn, uio_t *uio, uint64_t size, dmu_tx_t *tx)
1031 1031 {
1032 1032 dmu_buf_t **dbp;
1033 1033 int numbufs;
1034 1034 int err = 0;
1035 1035 int i;
1036 1036
1037 1037 err = dmu_buf_hold_array_by_dnode(dn, uio->uio_loffset, size,
1038 1038 FALSE, FTAG, &numbufs, &dbp, DMU_READ_PREFETCH);
1039 1039 if (err)
1040 1040 return (err);
1041 1041
1042 1042 for (i = 0; i < numbufs; i++) {
1043 1043 int tocpy;
1044 1044 int bufoff;
1045 1045 dmu_buf_t *db = dbp[i];
1046 1046
1047 1047 ASSERT(size > 0);
1048 1048
1049 1049 bufoff = uio->uio_loffset - db->db_offset;
1050 1050 tocpy = (int)MIN(db->db_size - bufoff, size);
1051 1051
1052 1052 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1053 1053
1054 1054 if (tocpy == db->db_size)
1055 1055 dmu_buf_will_fill(db, tx);
1056 1056 else
1057 1057 dmu_buf_will_dirty(db, tx);
1058 1058
1059 1059 /*
1060 1060 * XXX uiomove could block forever (eg. nfs-backed
1061 1061 * pages). There needs to be a uiolockdown() function
1062 1062 * to lock the pages in memory, so that uiomove won't
1063 1063 * block.
1064 1064 */
1065 1065 err = uiomove((char *)db->db_data + bufoff, tocpy,
1066 1066 UIO_WRITE, uio);
1067 1067
1068 1068 if (tocpy == db->db_size)
1069 1069 dmu_buf_fill_done(db, tx);
1070 1070
1071 1071 if (err)
1072 1072 break;
1073 1073
1074 1074 size -= tocpy;
1075 1075 }
1076 1076
1077 1077 dmu_buf_rele_array(dbp, numbufs, FTAG);
1078 1078 return (err);
1079 1079 }
1080 1080
1081 1081 int
1082 1082 dmu_write_uio_dbuf(dmu_buf_t *zdb, uio_t *uio, uint64_t size,
1083 1083 dmu_tx_t *tx)
1084 1084 {
1085 1085 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1086 1086 dnode_t *dn;
1087 1087 int err;
1088 1088
1089 1089 if (size == 0)
1090 1090 return (0);
1091 1091
1092 1092 DB_DNODE_ENTER(db);
1093 1093 dn = DB_DNODE(db);
1094 1094 err = dmu_write_uio_dnode(dn, uio, size, tx);
1095 1095 DB_DNODE_EXIT(db);
1096 1096
1097 1097 return (err);
1098 1098 }
1099 1099
1100 1100 int
1101 1101 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
1102 1102 dmu_tx_t *tx)
1103 1103 {
1104 1104 dnode_t *dn;
1105 1105 int err;
1106 1106
1107 1107 if (size == 0)
1108 1108 return (0);
1109 1109
1110 1110 err = dnode_hold(os, object, FTAG, &dn);
1111 1111 if (err)
1112 1112 return (err);
1113 1113
1114 1114 err = dmu_write_uio_dnode(dn, uio, size, tx);
1115 1115
1116 1116 dnode_rele(dn, FTAG);
1117 1117
1118 1118 return (err);
1119 1119 }
1120 1120
1121 1121 int
1122 1122 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1123 1123 page_t *pp, dmu_tx_t *tx)
1124 1124 {
1125 1125 dmu_buf_t **dbp;
1126 1126 int numbufs, i;
1127 1127 int err;
1128 1128
1129 1129 if (size == 0)
1130 1130 return (0);
1131 1131
1132 1132 err = dmu_buf_hold_array(os, object, offset, size,
1133 1133 FALSE, FTAG, &numbufs, &dbp);
1134 1134 if (err)
1135 1135 return (err);
1136 1136
1137 1137 for (i = 0; i < numbufs; i++) {
1138 1138 int tocpy, copied, thiscpy;
1139 1139 int bufoff;
1140 1140 dmu_buf_t *db = dbp[i];
1141 1141 caddr_t va;
1142 1142
1143 1143 ASSERT(size > 0);
1144 1144 ASSERT3U(db->db_size, >=, PAGESIZE);
1145 1145
1146 1146 bufoff = offset - db->db_offset;
1147 1147 tocpy = (int)MIN(db->db_size - bufoff, size);
1148 1148
1149 1149 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1150 1150
1151 1151 if (tocpy == db->db_size)
1152 1152 dmu_buf_will_fill(db, tx);
1153 1153 else
1154 1154 dmu_buf_will_dirty(db, tx);
1155 1155
1156 1156 for (copied = 0; copied < tocpy; copied += PAGESIZE) {
1157 1157 ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
1158 1158 thiscpy = MIN(PAGESIZE, tocpy - copied);
1159 1159 va = zfs_map_page(pp, S_READ);
1160 1160 bcopy(va, (char *)db->db_data + bufoff, thiscpy);
1161 1161 zfs_unmap_page(pp, va);
1162 1162 pp = pp->p_next;
1163 1163 bufoff += PAGESIZE;
1164 1164 }
1165 1165
1166 1166 if (tocpy == db->db_size)
1167 1167 dmu_buf_fill_done(db, tx);
1168 1168
1169 1169 offset += tocpy;
1170 1170 size -= tocpy;
1171 1171 }
1172 1172 dmu_buf_rele_array(dbp, numbufs, FTAG);
1173 1173 return (err);
1174 1174 }
1175 1175 #endif
1176 1176
1177 1177 /*
1178 1178 * Allocate a loaned anonymous arc buffer.
1179 1179 */
1180 1180 arc_buf_t *
1181 1181 dmu_request_arcbuf(dmu_buf_t *handle, int size)
1182 1182 {
1183 1183 dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1184 1184 spa_t *spa;
1185 1185
1186 1186 DB_GET_SPA(&spa, db);
1187 1187 return (arc_loan_buf(spa, size));
1188 1188 }
1189 1189
1190 1190 /*
1191 1191 * Free a loaned arc buffer.
1192 1192 */
1193 1193 void
1194 1194 dmu_return_arcbuf(arc_buf_t *buf)
1195 1195 {
1196 1196 arc_return_buf(buf, FTAG);
1197 1197 VERIFY(arc_buf_remove_ref(buf, FTAG));
1198 1198 }
1199 1199
1200 1200 /*
1201 1201 * When possible directly assign passed loaned arc buffer to a dbuf.
1202 1202 * If this is not possible copy the contents of passed arc buf via
1203 1203 * dmu_write().
1204 1204 */
1205 1205 void
1206 1206 dmu_assign_arcbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
1207 1207 dmu_tx_t *tx)
1208 1208 {
1209 1209 dmu_buf_impl_t *dbuf = (dmu_buf_impl_t *)handle;
1210 1210 dnode_t *dn;
1211 1211 dmu_buf_impl_t *db;
1212 1212 uint32_t blksz = (uint32_t)arc_buf_size(buf);
1213 1213 uint64_t blkid;
1214 1214
1215 1215 DB_DNODE_ENTER(dbuf);
1216 1216 dn = DB_DNODE(dbuf);
1217 1217 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1218 1218 blkid = dbuf_whichblock(dn, offset);
1219 1219 VERIFY((db = dbuf_hold(dn, blkid, FTAG)) != NULL);
1220 1220 rw_exit(&dn->dn_struct_rwlock);
1221 1221 DB_DNODE_EXIT(dbuf);
1222 1222
1223 1223 if (offset == db->db.db_offset && blksz == db->db.db_size) {
1224 1224 dbuf_assign_arcbuf(db, buf, tx);
1225 1225 dbuf_rele(db, FTAG);
1226 1226 } else {
1227 1227 objset_t *os;
1228 1228 uint64_t object;
1229 1229
1230 1230 DB_DNODE_ENTER(dbuf);
1231 1231 dn = DB_DNODE(dbuf);
1232 1232 os = dn->dn_objset;
1233 1233 object = dn->dn_object;
1234 1234 DB_DNODE_EXIT(dbuf);
1235 1235
1236 1236 dbuf_rele(db, FTAG);
1237 1237 dmu_write(os, object, offset, blksz, buf->b_data, tx);
1238 1238 dmu_return_arcbuf(buf);
1239 1239 XUIOSTAT_BUMP(xuiostat_wbuf_copied);
1240 1240 }
1241 1241 }
1242 1242
1243 1243 typedef struct {
1244 1244 dbuf_dirty_record_t *dsa_dr;
1245 1245 dmu_sync_cb_t *dsa_done;
1246 1246 zgd_t *dsa_zgd;
1247 1247 dmu_tx_t *dsa_tx;
1248 1248 } dmu_sync_arg_t;
1249 1249
1250 1250 /* ARGSUSED */
1251 1251 static void
1252 1252 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1253 1253 {
1254 1254 dmu_sync_arg_t *dsa = varg;
1255 1255 dmu_buf_t *db = dsa->dsa_zgd->zgd_db;
1256 1256 blkptr_t *bp = zio->io_bp;
1257 1257
1258 1258 if (zio->io_error == 0) {
1259 1259 if (BP_IS_HOLE(bp)) {
1260 1260 /*
1261 1261 * A block of zeros may compress to a hole, but the
1262 1262 * block size still needs to be known for replay.
1263 1263 */
1264 1264 BP_SET_LSIZE(bp, db->db_size);
1265 1265 } else {
1266 1266 ASSERT(BP_GET_LEVEL(bp) == 0);
1267 1267 bp->blk_fill = 1;
1268 1268 }
1269 1269 }
1270 1270 }
1271 1271
1272 1272 static void
1273 1273 dmu_sync_late_arrival_ready(zio_t *zio)
1274 1274 {
1275 1275 dmu_sync_ready(zio, NULL, zio->io_private);
1276 1276 }
1277 1277
1278 1278 /* ARGSUSED */
1279 1279 static void
1280 1280 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1281 1281 {
1282 1282 dmu_sync_arg_t *dsa = varg;
1283 1283 dbuf_dirty_record_t *dr = dsa->dsa_dr;
1284 1284 dmu_buf_impl_t *db = dr->dr_dbuf;
1285 1285
1286 1286 mutex_enter(&db->db_mtx);
1287 1287 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1288 1288 if (zio->io_error == 0) {
1289 1289 dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);
1290 1290 if (dr->dt.dl.dr_nopwrite) {
1291 1291 blkptr_t *bp = zio->io_bp;
1292 1292 blkptr_t *bp_orig = &zio->io_bp_orig;
1293 1293 uint8_t chksum = BP_GET_CHECKSUM(bp_orig);
1294 1294
1295 1295 ASSERT(BP_EQUAL(bp, bp_orig));
1296 1296 ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);
1297 1297 ASSERT(zio_checksum_table[chksum].ci_dedup);
1298 1298 }
1299 1299 dr->dt.dl.dr_overridden_by = *zio->io_bp;
1300 1300 dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1301 1301 dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
1302 1302 if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by))
1303 1303 BP_ZERO(&dr->dt.dl.dr_overridden_by);
1304 1304 } else {
1305 1305 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1306 1306 }
1307 1307 cv_broadcast(&db->db_changed);
1308 1308 mutex_exit(&db->db_mtx);
1309 1309
1310 1310 dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1311 1311
1312 1312 kmem_free(dsa, sizeof (*dsa));
1313 1313 }
1314 1314
1315 1315 static void
1316 1316 dmu_sync_late_arrival_done(zio_t *zio)
1317 1317 {
1318 1318 blkptr_t *bp = zio->io_bp;
1319 1319 dmu_sync_arg_t *dsa = zio->io_private;
1320 1320 blkptr_t *bp_orig = &zio->io_bp_orig;
1321 1321
1322 1322 if (zio->io_error == 0 && !BP_IS_HOLE(bp)) {
1323 1323 /*
1324 1324 * If we didn't allocate a new block (i.e. ZIO_FLAG_NOPWRITE)
1325 1325 * then there is nothing to do here. Otherwise, free the
1326 1326 * newly allocated block in this txg.
1327 1327 */
1328 1328 if (zio->io_flags & ZIO_FLAG_NOPWRITE) {
1329 1329 ASSERT(BP_EQUAL(bp, bp_orig));
1330 1330 } else {
1331 1331 ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));
1332 1332 ASSERT(zio->io_bp->blk_birth == zio->io_txg);
1333 1333 ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1334 1334 zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1335 1335 }
1336 1336 }
1337 1337
1338 1338 dmu_tx_commit(dsa->dsa_tx);
1339 1339
1340 1340 dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1341 1341
1342 1342 kmem_free(dsa, sizeof (*dsa));
1343 1343 }
1344 1344
1345 1345 static int
1346 1346 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
1347 1347 zio_prop_t *zp, zbookmark_t *zb)
1348 1348 {
1349 1349 dmu_sync_arg_t *dsa;
1350 1350 dmu_tx_t *tx;
1351 1351
1352 1352 tx = dmu_tx_create(os);
1353 1353 dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
1354 1354 if (dmu_tx_assign(tx, TXG_WAIT) != 0) {
1355 1355 dmu_tx_abort(tx);
1356 1356 /* Make zl_get_data do txg_waited_synced() */
1357 1357 return (SET_ERROR(EIO));
1358 1358 }
1359 1359
1360 1360 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1361 1361 dsa->dsa_dr = NULL;
1362 1362 dsa->dsa_done = done;
1363 1363 dsa->dsa_zgd = zgd;
1364 1364 dsa->dsa_tx = tx;
1365 1365
1366 1366 zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
1367 1367 zgd->zgd_db->db_data, zgd->zgd_db->db_size, zp,
1368 1368 dmu_sync_late_arrival_ready, dmu_sync_late_arrival_done, dsa,
1369 1369 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
1370 1370
1371 1371 return (0);
1372 1372 }
1373 1373
1374 1374 /*
1375 1375 * Intent log support: sync the block associated with db to disk.
1376 1376 * N.B. and XXX: the caller is responsible for making sure that the
1377 1377 * data isn't changing while dmu_sync() is writing it.
1378 1378 *
1379 1379 * Return values:
1380 1380 *
1381 1381 * EEXIST: this txg has already been synced, so there's nothing to do.
1382 1382 * The caller should not log the write.
1383 1383 *
1384 1384 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1385 1385 * The caller should not log the write.
1386 1386 *
1387 1387 * EALREADY: this block is already in the process of being synced.
1388 1388 * The caller should track its progress (somehow).
1389 1389 *
1390 1390 * EIO: could not do the I/O.
1391 1391 * The caller should do a txg_wait_synced().
1392 1392 *
1393 1393 * 0: the I/O has been initiated.
1394 1394 * The caller should log this blkptr in the done callback.
1395 1395 * It is possible that the I/O will fail, in which case
1396 1396 * the error will be reported to the done callback and
1397 1397 * propagated to pio from zio_done().
1398 1398 */
1399 1399 int
1400 1400 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
1401 1401 {
1402 1402 blkptr_t *bp = zgd->zgd_bp;
1403 1403 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
1404 1404 objset_t *os = db->db_objset;
1405 1405 dsl_dataset_t *ds = os->os_dsl_dataset;
1406 1406 dbuf_dirty_record_t *dr;
1407 1407 dmu_sync_arg_t *dsa;
1408 1408 zbookmark_t zb;
1409 1409 zio_prop_t zp;
1410 1410 dnode_t *dn;
1411 1411
1412 1412 ASSERT(pio != NULL);
1413 1413 ASSERT(txg != 0);
1414 1414
1415 1415 SET_BOOKMARK(&zb, ds->ds_object,
1416 1416 db->db.db_object, db->db_level, db->db_blkid);
1417 1417
1418 1418 DB_DNODE_ENTER(db);
1419 1419 dn = DB_DNODE(db);
1420 1420 dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp);
1421 1421 DB_DNODE_EXIT(db);
1422 1422
1423 1423 /*
1424 1424 * If we're frozen (running ziltest), we always need to generate a bp.
1425 1425 */
1426 1426 if (txg > spa_freeze_txg(os->os_spa))
1427 1427 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1428 1428
1429 1429 /*
1430 1430 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
1431 1431 * and us. If we determine that this txg is not yet syncing,
1432 1432 * but it begins to sync a moment later, that's OK because the
1433 1433 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
1434 1434 */
1435 1435 mutex_enter(&db->db_mtx);
1436 1436
1437 1437 if (txg <= spa_last_synced_txg(os->os_spa)) {
1438 1438 /*
1439 1439 * This txg has already synced. There's nothing to do.
1440 1440 */
1441 1441 mutex_exit(&db->db_mtx);
1442 1442 return (SET_ERROR(EEXIST));
1443 1443 }
1444 1444
1445 1445 if (txg <= spa_syncing_txg(os->os_spa)) {
1446 1446 /*
1447 1447 * This txg is currently syncing, so we can't mess with
1448 1448 * the dirty record anymore; just write a new log block.
1449 1449 */
1450 1450 mutex_exit(&db->db_mtx);
1451 1451 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
1452 1452 }
1453 1453
1454 1454 dr = db->db_last_dirty;
1455 1455 while (dr && dr->dr_txg != txg)
1456 1456 dr = dr->dr_next;
1457 1457
1458 1458 if (dr == NULL) {
1459 1459 /*
1460 1460 * There's no dr for this dbuf, so it must have been freed.
1461 1461 * There's no need to log writes to freed blocks, so we're done.
1462 1462 */
1463 1463 mutex_exit(&db->db_mtx);
1464 1464 return (SET_ERROR(ENOENT));
1465 1465 }
1466 1466
1467 1467 ASSERT(dr->dr_next == NULL || dr->dr_next->dr_txg < txg);
1468 1468
1469 1469 /*
1470 1470 * Assume the on-disk data is X, the current syncing data is Y,
1471 1471 * and the current in-memory data is Z (currently in dmu_sync).
1472 1472 * X and Z are identical but Y is has been modified. Normally,
1473 1473 * when X and Z are the same we will perform a nopwrite but if Y
1474 1474 * is different we must disable nopwrite since the resulting write
1475 1475 * of Y to disk can free the block containing X. If we allowed a
1476 1476 * nopwrite to occur the block pointing to Z would reference a freed
1477 1477 * block. Since this is a rare case we simplify this by disabling
1478 1478 * nopwrite if the current dmu_sync-ing dbuf has been modified in
1479 1479 * a previous transaction.
1480 1480 */
1481 1481 if (dr->dr_next)
1482 1482 zp.zp_nopwrite = B_FALSE;
1483 1483
1484 1484 ASSERT(dr->dr_txg == txg);
1485 1485 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
1486 1486 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
1487 1487 /*
1488 1488 * We have already issued a sync write for this buffer,
1489 1489 * or this buffer has already been synced. It could not
1490 1490 * have been dirtied since, or we would have cleared the state.
1491 1491 */
1492 1492 mutex_exit(&db->db_mtx);
1493 1493 return (SET_ERROR(EALREADY));
1494 1494 }
1495 1495
1496 1496 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
1497 1497 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
1498 1498 mutex_exit(&db->db_mtx);
1499 1499
1500 1500 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
1501 1501 dsa->dsa_dr = dr;
1502 1502 dsa->dsa_done = done;
1503 1503 dsa->dsa_zgd = zgd;
1504 1504 dsa->dsa_tx = NULL;
1505 1505
1506 1506 zio_nowait(arc_write(pio, os->os_spa, txg,
1507 1507 bp, dr->dt.dl.dr_data, DBUF_IS_L2CACHEABLE(db), &zp,
1508 1508 dmu_sync_ready, dmu_sync_done, dsa,
1509 1509 ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, &zb));
1510 1510
1511 1511 return (0);
1512 1512 }
1513 1513
1514 1514 int
1515 1515 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1516 1516 dmu_tx_t *tx)
1517 1517 {
1518 1518 dnode_t *dn;
1519 1519 int err;
1520 1520
1521 1521 err = dnode_hold(os, object, FTAG, &dn);
1522 1522 if (err)
1523 1523 return (err);
1524 1524 err = dnode_set_blksz(dn, size, ibs, tx);
1525 1525 dnode_rele(dn, FTAG);
1526 1526 return (err);
1527 1527 }
1528 1528
1529 1529 void
1530 1530 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1531 1531 dmu_tx_t *tx)
1532 1532 {
1533 1533 dnode_t *dn;
1534 1534
1535 1535 /* XXX assumes dnode_hold will not get an i/o error */
1536 1536 (void) dnode_hold(os, object, FTAG, &dn);
1537 1537 ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1538 1538 dn->dn_checksum = checksum;
1539 1539 dnode_setdirty(dn, tx);
1540 1540 dnode_rele(dn, FTAG);
1541 1541 }
1542 1542
1543 1543 void
1544 1544 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1545 1545 dmu_tx_t *tx)
1546 1546 {
1547 1547 dnode_t *dn;
1548 1548
1549 1549 /* XXX assumes dnode_hold will not get an i/o error */
1550 1550 (void) dnode_hold(os, object, FTAG, &dn);
1551 1551 ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1552 1552 dn->dn_compress = compress;
1553 1553 dnode_setdirty(dn, tx);
1554 1554 dnode_rele(dn, FTAG);
1555 1555 }
1556 1556
1557 1557 int zfs_mdcomp_disable = 0;
1558 1558
1559 1559 void
1560 1560 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
1561 1561 {
1562 1562 dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
1563 1563 boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
1564 1564 (wp & WP_SPILL));
1565 1565 enum zio_checksum checksum = os->os_checksum;
1566 1566 enum zio_compress compress = os->os_compress;
1567 1567 enum zio_checksum dedup_checksum = os->os_dedup_checksum;
1568 1568 boolean_t dedup = B_FALSE;
1569 1569 boolean_t nopwrite = B_FALSE;
1570 1570 boolean_t dedup_verify = os->os_dedup_verify;
1571 1571 int copies = os->os_copies;
1572 1572
1573 1573 /*
1574 1574 * We maintain different write policies for each of the following
1575 1575 * types of data:
1576 1576 * 1. metadata
1577 1577 * 2. preallocated blocks (i.e. level-0 blocks of a dump device)
1578 1578 * 3. all other level 0 blocks
1579 1579 */
1580 1580 if (ismd) {
1581 1581 /*
1582 1582 * XXX -- we should design a compression algorithm
1583 1583 * that specializes in arrays of bps.
1584 1584 */
1585 1585 compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY :
1586 1586 ZIO_COMPRESS_LZJB;
1587 1587
1588 1588 /*
1589 1589 * Metadata always gets checksummed. If the data
1590 1590 * checksum is multi-bit correctable, and it's not a
1591 1591 * ZBT-style checksum, then it's suitable for metadata
1592 1592 * as well. Otherwise, the metadata checksum defaults
1593 1593 * to fletcher4.
1594 1594 */
1595 1595 if (zio_checksum_table[checksum].ci_correctable < 1 ||
1596 1596 zio_checksum_table[checksum].ci_eck)
1597 1597 checksum = ZIO_CHECKSUM_FLETCHER_4;
1598 1598 } else if (wp & WP_NOFILL) {
1599 1599 ASSERT(level == 0);
1600 1600
1601 1601 /*
1602 1602 * If we're writing preallocated blocks, we aren't actually
1603 1603 * writing them so don't set any policy properties. These
1604 1604 * blocks are currently only used by an external subsystem
1605 1605 * outside of zfs (i.e. dump) and not written by the zio
1606 1606 * pipeline.
1607 1607 */
1608 1608 compress = ZIO_COMPRESS_OFF;
1609 1609 checksum = ZIO_CHECKSUM_OFF;
1610 1610 } else {
1611 1611 compress = zio_compress_select(dn->dn_compress, compress);
1612 1612
1613 1613 checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
1614 1614 zio_checksum_select(dn->dn_checksum, checksum) :
1615 1615 dedup_checksum;
1616 1616
1617 1617 /*
1618 1618 * Determine dedup setting. If we are in dmu_sync(),
1619 1619 * we won't actually dedup now because that's all
1620 1620 * done in syncing context; but we do want to use the
1621 1621 * dedup checkum. If the checksum is not strong
1622 1622 * enough to ensure unique signatures, force
1623 1623 * dedup_verify.
1624 1624 */
1625 1625 if (dedup_checksum != ZIO_CHECKSUM_OFF) {
1626 1626 dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;
1627 1627 if (!zio_checksum_table[checksum].ci_dedup)
1628 1628 dedup_verify = B_TRUE;
1629 1629 }
1630 1630
1631 1631 /*
1632 1632 * Enable nopwrite if we have a cryptographically secure
1633 1633 * checksum that has no known collisions (i.e. SHA-256)
1634 1634 * and compression is enabled. We don't enable nopwrite if
1635 1635 * dedup is enabled as the two features are mutually exclusive.
1636 1636 */
1637 1637 nopwrite = (!dedup && zio_checksum_table[checksum].ci_dedup &&
1638 1638 compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);
1639 1639 }
1640 1640
1641 1641 zp->zp_checksum = checksum;
1642 1642 zp->zp_compress = compress;
1643 1643 zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
1644 1644 zp->zp_level = level;
1645 1645 zp->zp_copies = MIN(copies + ismd, spa_max_replication(os->os_spa));
1646 1646 zp->zp_dedup = dedup;
1647 1647 zp->zp_dedup_verify = dedup && dedup_verify;
1648 1648 zp->zp_nopwrite = nopwrite;
1649 1649 }
1650 1650
1651 1651 int
1652 1652 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1653 1653 {
1654 1654 dnode_t *dn;
1655 1655 int i, err;
1656 1656
1657 1657 err = dnode_hold(os, object, FTAG, &dn);
1658 1658 if (err)
1659 1659 return (err);
1660 1660 /*
1661 1661 * Sync any current changes before
1662 1662 * we go trundling through the block pointers.
1663 1663 */
1664 1664 for (i = 0; i < TXG_SIZE; i++) {
1665 1665 if (list_link_active(&dn->dn_dirty_link[i]))
1666 1666 break;
1667 1667 }
1668 1668 if (i != TXG_SIZE) {
1669 1669 dnode_rele(dn, FTAG);
1670 1670 txg_wait_synced(dmu_objset_pool(os), 0);
1671 1671 err = dnode_hold(os, object, FTAG, &dn);
1672 1672 if (err)
1673 1673 return (err);
1674 1674 }
1675 1675
1676 1676 err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1677 1677 dnode_rele(dn, FTAG);
1678 1678
1679 1679 return (err);
1680 1680 }
1681 1681
1682 1682 void
1683 1683 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1684 1684 {
1685 1685 dnode_phys_t *dnp;
1686 1686
1687 1687 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1688 1688 mutex_enter(&dn->dn_mtx);
1689 1689
1690 1690 dnp = dn->dn_phys;
1691 1691
1692 1692 doi->doi_data_block_size = dn->dn_datablksz;
1693 1693 doi->doi_metadata_block_size = dn->dn_indblkshift ?
1694 1694 1ULL << dn->dn_indblkshift : 0;
1695 1695 doi->doi_type = dn->dn_type;
1696 1696 doi->doi_bonus_type = dn->dn_bonustype;
1697 1697 doi->doi_bonus_size = dn->dn_bonuslen;
1698 1698 doi->doi_indirection = dn->dn_nlevels;
1699 1699 doi->doi_checksum = dn->dn_checksum;
1700 1700 doi->doi_compress = dn->dn_compress;
1701 1701 doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
1702 1702 doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
1703 1703 doi->doi_fill_count = 0;
1704 1704 for (int i = 0; i < dnp->dn_nblkptr; i++)
1705 1705 doi->doi_fill_count += dnp->dn_blkptr[i].blk_fill;
1706 1706
1707 1707 mutex_exit(&dn->dn_mtx);
1708 1708 rw_exit(&dn->dn_struct_rwlock);
1709 1709 }
1710 1710
1711 1711 /*
1712 1712 * Get information on a DMU object.
1713 1713 * If doi is NULL, just indicates whether the object exists.
1714 1714 */
1715 1715 int
1716 1716 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1717 1717 {
1718 1718 dnode_t *dn;
1719 1719 int err = dnode_hold(os, object, FTAG, &dn);
1720 1720
1721 1721 if (err)
1722 1722 return (err);
1723 1723
1724 1724 if (doi != NULL)
1725 1725 dmu_object_info_from_dnode(dn, doi);
1726 1726
1727 1727 dnode_rele(dn, FTAG);
1728 1728 return (0);
1729 1729 }
1730 1730
1731 1731 /*
1732 1732 * As above, but faster; can be used when you have a held dbuf in hand.
1733 1733 */
1734 1734 void
1735 1735 dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
1736 1736 {
1737 1737 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1738 1738
1739 1739 DB_DNODE_ENTER(db);
1740 1740 dmu_object_info_from_dnode(DB_DNODE(db), doi);
1741 1741 DB_DNODE_EXIT(db);
1742 1742 }
1743 1743
1744 1744 /*
1745 1745 * Faster still when you only care about the size.
1746 1746 * This is specifically optimized for zfs_getattr().
1747 1747 */
1748 1748 void
1749 1749 dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
1750 1750 u_longlong_t *nblk512)
1751 1751 {
1752 1752 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
1753 1753 dnode_t *dn;
1754 1754
1755 1755 DB_DNODE_ENTER(db);
1756 1756 dn = DB_DNODE(db);
1757 1757
1758 1758 *blksize = dn->dn_datablksz;
1759 1759 /* add 1 for dnode space */
1760 1760 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
1761 1761 SPA_MINBLOCKSHIFT) + 1;
1762 1762 DB_DNODE_EXIT(db);
1763 1763 }
1764 1764
1765 1765 void
1766 1766 byteswap_uint64_array(void *vbuf, size_t size)
1767 1767 {
1768 1768 uint64_t *buf = vbuf;
1769 1769 size_t count = size >> 3;
1770 1770 int i;
1771 1771
1772 1772 ASSERT((size & 7) == 0);
1773 1773
1774 1774 for (i = 0; i < count; i++)
1775 1775 buf[i] = BSWAP_64(buf[i]);
1776 1776 }
1777 1777
1778 1778 void
1779 1779 byteswap_uint32_array(void *vbuf, size_t size)
1780 1780 {
1781 1781 uint32_t *buf = vbuf;
1782 1782 size_t count = size >> 2;
1783 1783 int i;
1784 1784
1785 1785 ASSERT((size & 3) == 0);
1786 1786
1787 1787 for (i = 0; i < count; i++)
1788 1788 buf[i] = BSWAP_32(buf[i]);
1789 1789 }
1790 1790
1791 1791 void
1792 1792 byteswap_uint16_array(void *vbuf, size_t size)
1793 1793 {
1794 1794 uint16_t *buf = vbuf;
1795 1795 size_t count = size >> 1;
1796 1796 int i;
1797 1797
1798 1798 ASSERT((size & 1) == 0);
1799 1799
1800 1800 for (i = 0; i < count; i++)
1801 1801 buf[i] = BSWAP_16(buf[i]);
1802 1802 }
1803 1803
1804 1804 /* ARGSUSED */
1805 1805 void
1806 1806 byteswap_uint8_array(void *vbuf, size_t size)
1807 1807 {
1808 1808 }
1809 1809
1810 1810 void
1811 1811 dmu_init(void)
1812 1812 {
1813 1813 zfs_dbgmsg_init();
1814 1814 sa_cache_init();
1815 1815 xuio_stat_init();
1816 1816 dmu_objset_init();
↓ open down ↓ |
1816 lines elided |
↑ open up ↑ |
1817 1817 dnode_init();
1818 1818 dbuf_init();
1819 1819 zfetch_init();
1820 1820 l2arc_init();
1821 1821 arc_init();
1822 1822 }
1823 1823
1824 1824 void
1825 1825 dmu_fini(void)
1826 1826 {
1827 - arc_fini();
1827 + arc_fini(); /* arc depends on l2arc, so arc must go first */
1828 1828 l2arc_fini();
1829 1829 zfetch_fini();
1830 1830 dbuf_fini();
1831 1831 dnode_fini();
1832 1832 dmu_objset_fini();
1833 1833 xuio_stat_fini();
1834 1834 sa_cache_fini();
1835 1835 zfs_dbgmsg_fini();
1836 1836 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX