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