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