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