]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dmu.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / dmu.c
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #include <sys/dmu.h>
27 #include <sys/dmu_impl.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/dbuf.h>
30 #include <sys/dnode.h>
31 #include <sys/zfs_context.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/dsl_prop.h>
39 #include <sys/dmu_zfetch.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zap.h>
42 #include <sys/zio_checksum.h>
43 #include <sys/zfs_znode.h>
44
45 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
46         {       byteswap_uint8_array,   TRUE,   "unallocated"           },
47         {       zap_byteswap,           TRUE,   "object directory"      },
48         {       byteswap_uint64_array,  TRUE,   "object array"          },
49         {       byteswap_uint8_array,   TRUE,   "packed nvlist"         },
50         {       byteswap_uint64_array,  TRUE,   "packed nvlist size"    },
51         {       byteswap_uint64_array,  TRUE,   "bplist"                },
52         {       byteswap_uint64_array,  TRUE,   "bplist header"         },
53         {       byteswap_uint64_array,  TRUE,   "SPA space map header"  },
54         {       byteswap_uint64_array,  TRUE,   "SPA space map"         },
55         {       byteswap_uint64_array,  TRUE,   "ZIL intent log"        },
56         {       dnode_buf_byteswap,     TRUE,   "DMU dnode"             },
57         {       dmu_objset_byteswap,    TRUE,   "DMU objset"            },
58         {       byteswap_uint64_array,  TRUE,   "DSL directory"         },
59         {       zap_byteswap,           TRUE,   "DSL directory child map"},
60         {       zap_byteswap,           TRUE,   "DSL dataset snap map"  },
61         {       zap_byteswap,           TRUE,   "DSL props"             },
62         {       byteswap_uint64_array,  TRUE,   "DSL dataset"           },
63         {       zfs_znode_byteswap,     TRUE,   "ZFS znode"             },
64         {       zfs_oldacl_byteswap,    TRUE,   "ZFS V0 ACL"            },
65         {       byteswap_uint8_array,   FALSE,  "ZFS plain file"        },
66         {       zap_byteswap,           TRUE,   "ZFS directory"         },
67         {       zap_byteswap,           TRUE,   "ZFS master node"       },
68         {       zap_byteswap,           TRUE,   "ZFS delete queue"      },
69         {       byteswap_uint8_array,   FALSE,  "zvol object"           },
70         {       zap_byteswap,           TRUE,   "zvol prop"             },
71         {       byteswap_uint8_array,   FALSE,  "other uint8[]"         },
72         {       byteswap_uint64_array,  FALSE,  "other uint64[]"        },
73         {       zap_byteswap,           TRUE,   "other ZAP"             },
74         {       zap_byteswap,           TRUE,   "persistent error log"  },
75         {       byteswap_uint8_array,   TRUE,   "SPA history"           },
76         {       byteswap_uint64_array,  TRUE,   "SPA history offsets"   },
77         {       zap_byteswap,           TRUE,   "Pool properties"       },
78         {       zap_byteswap,           TRUE,   "DSL permissions"       },
79         {       zfs_acl_byteswap,       TRUE,   "ZFS ACL"               },
80         {       byteswap_uint8_array,   TRUE,   "ZFS SYSACL"            },
81         {       byteswap_uint8_array,   TRUE,   "FUID table"            },
82         {       byteswap_uint64_array,  TRUE,   "FUID table size"       },
83         {       zap_byteswap,           TRUE,   "DSL dataset next clones"},
84         {       zap_byteswap,           TRUE,   "scrub work queue"      },
85 };
86
87 int
88 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
89     void *tag, dmu_buf_t **dbp)
90 {
91         dnode_t *dn;
92         uint64_t blkid;
93         dmu_buf_impl_t *db;
94         int err;
95
96         err = dnode_hold(os->os, object, FTAG, &dn);
97         if (err)
98                 return (err);
99         blkid = dbuf_whichblock(dn, offset);
100         rw_enter(&dn->dn_struct_rwlock, RW_READER);
101         db = dbuf_hold(dn, blkid, tag);
102         rw_exit(&dn->dn_struct_rwlock);
103         if (db == NULL) {
104                 err = EIO;
105         } else {
106                 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
107                 if (err) {
108                         dbuf_rele(db, tag);
109                         db = NULL;
110                 }
111         }
112
113         dnode_rele(dn, FTAG);
114         *dbp = &db->db;
115         return (err);
116 }
117
118 int
119 dmu_bonus_max(void)
120 {
121         return (DN_MAX_BONUSLEN);
122 }
123
124 int
125 dmu_set_bonus(dmu_buf_t *db, int newsize, dmu_tx_t *tx)
126 {
127         dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
128
129         if (dn->dn_bonus != (dmu_buf_impl_t *)db)
130                 return (EINVAL);
131         if (newsize < 0 || newsize > db->db_size)
132                 return (EINVAL);
133         dnode_setbonuslen(dn, newsize, tx);
134         return (0);
135 }
136
137 /*
138  * returns ENOENT, EIO, or 0.
139  */
140 int
141 dmu_bonus_hold(objset_t *os, uint64_t object, void *tag, dmu_buf_t **dbp)
142 {
143         dnode_t *dn;
144         dmu_buf_impl_t *db;
145         int error;
146
147         error = dnode_hold(os->os, object, FTAG, &dn);
148         if (error)
149                 return (error);
150
151         rw_enter(&dn->dn_struct_rwlock, RW_READER);
152         if (dn->dn_bonus == NULL) {
153                 rw_exit(&dn->dn_struct_rwlock);
154                 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
155                 if (dn->dn_bonus == NULL)
156                         dbuf_create_bonus(dn);
157         }
158         db = dn->dn_bonus;
159         rw_exit(&dn->dn_struct_rwlock);
160
161         /* as long as the bonus buf is held, the dnode will be held */
162         if (refcount_add(&db->db_holds, tag) == 1)
163                 VERIFY(dnode_add_ref(dn, db));
164
165         dnode_rele(dn, FTAG);
166
167         VERIFY(0 == dbuf_read(db, NULL, DB_RF_MUST_SUCCEED));
168
169         *dbp = &db->db;
170         return (0);
171 }
172
173 /*
174  * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
175  * to take a held dnode rather than <os, object> -- the lookup is wasteful,
176  * and can induce severe lock contention when writing to several files
177  * whose dnodes are in the same block.
178  */
179 static int
180 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset,
181     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
182 {
183         dsl_pool_t *dp = NULL;
184         dmu_buf_t **dbp;
185         uint64_t blkid, nblks, i;
186         uint32_t flags;
187         int err;
188         zio_t *zio;
189         hrtime_t start;
190
191         ASSERT(length <= DMU_MAX_ACCESS);
192
193         flags = DB_RF_CANFAIL | DB_RF_NEVERWAIT;
194         if (length > zfetch_array_rd_sz)
195                 flags |= DB_RF_NOPREFETCH;
196
197         rw_enter(&dn->dn_struct_rwlock, RW_READER);
198         if (dn->dn_datablkshift) {
199                 int blkshift = dn->dn_datablkshift;
200                 nblks = (P2ROUNDUP(offset+length, 1ULL<<blkshift) -
201                     P2ALIGN(offset, 1ULL<<blkshift)) >> blkshift;
202         } else {
203                 if (offset + length > dn->dn_datablksz) {
204                         zfs_panic_recover("zfs: accessing past end of object "
205                             "%llx/%llx (size=%u access=%llu+%llu)",
206                             (longlong_t)dn->dn_objset->
207                             os_dsl_dataset->ds_object,
208                             (longlong_t)dn->dn_object, dn->dn_datablksz,
209                             (longlong_t)offset, (longlong_t)length);
210                         return (EIO);
211                 }
212                 nblks = 1;
213         }
214         dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
215
216         if (dn->dn_objset->os_dsl_dataset)
217                 dp = dn->dn_objset->os_dsl_dataset->ds_dir->dd_pool;
218         if (dp && dsl_pool_sync_context(dp))
219                 start = gethrtime();
220         zio = zio_root(dn->dn_objset->os_spa, NULL, NULL, ZIO_FLAG_CANFAIL);
221         blkid = dbuf_whichblock(dn, offset);
222         for (i = 0; i < nblks; i++) {
223                 dmu_buf_impl_t *db = dbuf_hold(dn, blkid+i, tag);
224                 if (db == NULL) {
225                         rw_exit(&dn->dn_struct_rwlock);
226                         dmu_buf_rele_array(dbp, nblks, tag);
227                         zio_nowait(zio);
228                         return (EIO);
229                 }
230                 /* initiate async i/o */
231                 if (read) {
232                         rw_exit(&dn->dn_struct_rwlock);
233                         (void) dbuf_read(db, zio, flags);
234                         rw_enter(&dn->dn_struct_rwlock, RW_READER);
235                 }
236                 dbp[i] = &db->db;
237         }
238         rw_exit(&dn->dn_struct_rwlock);
239
240         /* wait for async i/o */
241         err = zio_wait(zio);
242         /* track read overhead when we are in sync context */
243         if (dp && dsl_pool_sync_context(dp))
244                 dp->dp_read_overhead += gethrtime() - start;
245         if (err) {
246                 dmu_buf_rele_array(dbp, nblks, tag);
247                 return (err);
248         }
249
250         /* wait for other io to complete */
251         if (read) {
252                 for (i = 0; i < nblks; i++) {
253                         dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
254                         mutex_enter(&db->db_mtx);
255                         while (db->db_state == DB_READ ||
256                             db->db_state == DB_FILL)
257                                 cv_wait(&db->db_changed, &db->db_mtx);
258                         if (db->db_state == DB_UNCACHED)
259                                 err = EIO;
260                         mutex_exit(&db->db_mtx);
261                         if (err) {
262                                 dmu_buf_rele_array(dbp, nblks, tag);
263                                 return (err);
264                         }
265                 }
266         }
267
268         *numbufsp = nblks;
269         *dbpp = dbp;
270         return (0);
271 }
272
273 static int
274 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
275     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
276 {
277         dnode_t *dn;
278         int err;
279
280         err = dnode_hold(os->os, object, FTAG, &dn);
281         if (err)
282                 return (err);
283
284         err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
285             numbufsp, dbpp);
286
287         dnode_rele(dn, FTAG);
288
289         return (err);
290 }
291
292 int
293 dmu_buf_hold_array_by_bonus(dmu_buf_t *db, uint64_t offset,
294     uint64_t length, int read, void *tag, int *numbufsp, dmu_buf_t ***dbpp)
295 {
296         dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
297         int err;
298
299         err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
300             numbufsp, dbpp);
301
302         return (err);
303 }
304
305 void
306 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, void *tag)
307 {
308         int i;
309         dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
310
311         if (numbufs == 0)
312                 return;
313
314         for (i = 0; i < numbufs; i++) {
315                 if (dbp[i])
316                         dbuf_rele(dbp[i], tag);
317         }
318
319         kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
320 }
321
322 void
323 dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
324 {
325         dnode_t *dn;
326         uint64_t blkid;
327         int nblks, i, err;
328
329         if (zfs_prefetch_disable)
330                 return;
331
332         if (len == 0) {  /* they're interested in the bonus buffer */
333                 dn = os->os->os_meta_dnode;
334
335                 if (object == 0 || object >= DN_MAX_OBJECT)
336                         return;
337
338                 rw_enter(&dn->dn_struct_rwlock, RW_READER);
339                 blkid = dbuf_whichblock(dn, object * sizeof (dnode_phys_t));
340                 dbuf_prefetch(dn, blkid);
341                 rw_exit(&dn->dn_struct_rwlock);
342                 return;
343         }
344
345         /*
346          * XXX - Note, if the dnode for the requested object is not
347          * already cached, we will do a *synchronous* read in the
348          * dnode_hold() call.  The same is true for any indirects.
349          */
350         err = dnode_hold(os->os, object, FTAG, &dn);
351         if (err != 0)
352                 return;
353
354         rw_enter(&dn->dn_struct_rwlock, RW_READER);
355         if (dn->dn_datablkshift) {
356                 int blkshift = dn->dn_datablkshift;
357                 nblks = (P2ROUNDUP(offset+len, 1<<blkshift) -
358                     P2ALIGN(offset, 1<<blkshift)) >> blkshift;
359         } else {
360                 nblks = (offset < dn->dn_datablksz);
361         }
362
363         if (nblks != 0) {
364                 blkid = dbuf_whichblock(dn, offset);
365                 for (i = 0; i < nblks; i++)
366                         dbuf_prefetch(dn, blkid+i);
367         }
368
369         rw_exit(&dn->dn_struct_rwlock);
370
371         dnode_rele(dn, FTAG);
372 }
373
374 static int
375 get_next_chunk(dnode_t *dn, uint64_t *offset, uint64_t limit)
376 {
377         uint64_t len = *offset - limit;
378         uint64_t chunk_len = dn->dn_datablksz * DMU_MAX_DELETEBLKCNT;
379         uint64_t subchunk =
380             dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
381
382         ASSERT(limit <= *offset);
383
384         if (len <= chunk_len) {
385                 *offset = limit;
386                 return (0);
387         }
388
389         ASSERT(ISP2(subchunk));
390
391         while (*offset > limit) {
392                 uint64_t initial_offset = P2ROUNDUP(*offset, subchunk);
393                 uint64_t delta;
394                 int err;
395
396                 /* skip over allocated data */
397                 err = dnode_next_offset(dn,
398                     DNODE_FIND_HOLE|DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
399                 if (err == ESRCH)
400                         *offset = limit;
401                 else if (err)
402                         return (err);
403
404                 ASSERT3U(*offset, <=, initial_offset);
405                 *offset = P2ALIGN(*offset, subchunk);
406                 delta = initial_offset - *offset;
407                 if (delta >= chunk_len) {
408                         *offset += delta - chunk_len;
409                         return (0);
410                 }
411                 chunk_len -= delta;
412
413                 /* skip over unallocated data */
414                 err = dnode_next_offset(dn,
415                     DNODE_FIND_BACKWARDS, offset, 1, 1, 0);
416                 if (err == ESRCH)
417                         *offset = limit;
418                 else if (err)
419                         return (err);
420
421                 if (*offset < limit)
422                         *offset = limit;
423                 ASSERT3U(*offset, <, initial_offset);
424         }
425         return (0);
426 }
427
428 static int
429 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
430     uint64_t length, boolean_t free_dnode)
431 {
432         dmu_tx_t *tx;
433         uint64_t object_size, start, end, len;
434         boolean_t trunc = (length == DMU_OBJECT_END);
435         int align, err;
436
437         align = 1 << dn->dn_datablkshift;
438         ASSERT(align > 0);
439         object_size = align == 1 ? dn->dn_datablksz :
440             (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
441
442         if (trunc || (end = offset + length) > object_size)
443                 end = object_size;
444         if (end <= offset)
445                 return (0);
446         length = end - offset;
447
448         while (length) {
449                 start = end;
450                 err = get_next_chunk(dn, &start, offset);
451                 if (err)
452                         return (err);
453                 len = trunc ? DMU_OBJECT_END : end - start;
454
455                 tx = dmu_tx_create(os);
456                 dmu_tx_hold_free(tx, dn->dn_object, start, len);
457                 err = dmu_tx_assign(tx, TXG_WAIT);
458                 if (err) {
459                         dmu_tx_abort(tx);
460                         return (err);
461                 }
462
463                 dnode_free_range(dn, start, trunc ? -1 : len, tx);
464
465                 if (start == 0 && free_dnode) {
466                         ASSERT(trunc);
467                         dnode_free(dn, tx);
468                 }
469
470                 length -= end - start;
471
472                 dmu_tx_commit(tx);
473                 end = start;
474         }
475         return (0);
476 }
477
478 int
479 dmu_free_long_range(objset_t *os, uint64_t object,
480     uint64_t offset, uint64_t length)
481 {
482         dnode_t *dn;
483         int err;
484
485         err = dnode_hold(os->os, object, FTAG, &dn);
486         if (err != 0)
487                 return (err);
488         err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
489         dnode_rele(dn, FTAG);
490         return (err);
491 }
492
493 int
494 dmu_free_object(objset_t *os, uint64_t object)
495 {
496         dnode_t *dn;
497         dmu_tx_t *tx;
498         int err;
499
500         err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
501             FTAG, &dn);
502         if (err != 0)
503                 return (err);
504         if (dn->dn_nlevels == 1) {
505                 tx = dmu_tx_create(os);
506                 dmu_tx_hold_bonus(tx, object);
507                 dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
508                 err = dmu_tx_assign(tx, TXG_WAIT);
509                 if (err == 0) {
510                         dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
511                         dnode_free(dn, tx);
512                         dmu_tx_commit(tx);
513                 } else {
514                         dmu_tx_abort(tx);
515                 }
516         } else {
517                 err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
518         }
519         dnode_rele(dn, FTAG);
520         return (err);
521 }
522
523 int
524 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
525     uint64_t size, dmu_tx_t *tx)
526 {
527         dnode_t *dn;
528         int err = dnode_hold(os->os, object, FTAG, &dn);
529         if (err)
530                 return (err);
531         ASSERT(offset < UINT64_MAX);
532         ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
533         dnode_free_range(dn, offset, size, tx);
534         dnode_rele(dn, FTAG);
535         return (0);
536 }
537
538 int
539 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
540     void *buf)
541 {
542         dnode_t *dn;
543         dmu_buf_t **dbp;
544         int numbufs, i, err;
545
546         err = dnode_hold(os->os, object, FTAG, &dn);
547         if (err)
548                 return (err);
549
550         /*
551          * Deal with odd block sizes, where there can't be data past the first
552          * block.  If we ever do the tail block optimization, we will need to
553          * handle that here as well.
554          */
555         if (dn->dn_datablkshift == 0) {
556                 int newsz = offset > dn->dn_datablksz ? 0 :
557                     MIN(size, dn->dn_datablksz - offset);
558                 bzero((char *)buf + newsz, size - newsz);
559                 size = newsz;
560         }
561
562         while (size > 0) {
563                 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
564
565                 /*
566                  * NB: we could do this block-at-a-time, but it's nice
567                  * to be reading in parallel.
568                  */
569                 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
570                     TRUE, FTAG, &numbufs, &dbp);
571                 if (err)
572                         break;
573
574                 for (i = 0; i < numbufs; i++) {
575                         int tocpy;
576                         int bufoff;
577                         dmu_buf_t *db = dbp[i];
578
579                         ASSERT(size > 0);
580
581                         bufoff = offset - db->db_offset;
582                         tocpy = (int)MIN(db->db_size - bufoff, size);
583
584                         bcopy((char *)db->db_data + bufoff, buf, tocpy);
585
586                         offset += tocpy;
587                         size -= tocpy;
588                         buf = (char *)buf + tocpy;
589                 }
590                 dmu_buf_rele_array(dbp, numbufs, FTAG);
591         }
592         dnode_rele(dn, FTAG);
593         return (err);
594 }
595
596 void
597 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
598     const void *buf, dmu_tx_t *tx)
599 {
600         dmu_buf_t **dbp;
601         int numbufs, i;
602
603         if (size == 0)
604                 return;
605
606         VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
607             FALSE, FTAG, &numbufs, &dbp));
608
609         for (i = 0; i < numbufs; i++) {
610                 int tocpy;
611                 int bufoff;
612                 dmu_buf_t *db = dbp[i];
613
614                 ASSERT(size > 0);
615
616                 bufoff = offset - db->db_offset;
617                 tocpy = (int)MIN(db->db_size - bufoff, size);
618
619                 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
620
621                 if (tocpy == db->db_size)
622                         dmu_buf_will_fill(db, tx);
623                 else
624                         dmu_buf_will_dirty(db, tx);
625
626                 bcopy(buf, (char *)db->db_data + bufoff, tocpy);
627
628                 if (tocpy == db->db_size)
629                         dmu_buf_fill_done(db, tx);
630
631                 offset += tocpy;
632                 size -= tocpy;
633                 buf = (char *)buf + tocpy;
634         }
635         dmu_buf_rele_array(dbp, numbufs, FTAG);
636 }
637
638 #ifdef _KERNEL
639 int
640 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
641 {
642         dmu_buf_t **dbp;
643         int numbufs, i, err;
644
645         /*
646          * NB: we could do this block-at-a-time, but it's nice
647          * to be reading in parallel.
648          */
649         err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
650             &numbufs, &dbp);
651         if (err)
652                 return (err);
653
654         for (i = 0; i < numbufs; i++) {
655                 int tocpy;
656                 int bufoff;
657                 dmu_buf_t *db = dbp[i];
658
659                 ASSERT(size > 0);
660
661                 bufoff = uio->uio_loffset - db->db_offset;
662                 tocpy = (int)MIN(db->db_size - bufoff, size);
663
664                 err = uiomove((char *)db->db_data + bufoff, tocpy,
665                     UIO_READ, uio);
666                 if (err)
667                         break;
668
669                 size -= tocpy;
670         }
671         dmu_buf_rele_array(dbp, numbufs, FTAG);
672
673         return (err);
674 }
675
676 int
677 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
678     dmu_tx_t *tx)
679 {
680         dmu_buf_t **dbp;
681         int numbufs, i;
682         int err = 0;
683
684         if (size == 0)
685                 return (0);
686
687         err = dmu_buf_hold_array(os, object, uio->uio_loffset, size,
688             FALSE, FTAG, &numbufs, &dbp);
689         if (err)
690                 return (err);
691
692         for (i = 0; i < numbufs; i++) {
693                 int tocpy;
694                 int bufoff;
695                 dmu_buf_t *db = dbp[i];
696
697                 ASSERT(size > 0);
698
699                 bufoff = uio->uio_loffset - db->db_offset;
700                 tocpy = (int)MIN(db->db_size - bufoff, size);
701
702                 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
703
704                 if (tocpy == db->db_size)
705                         dmu_buf_will_fill(db, tx);
706                 else
707                         dmu_buf_will_dirty(db, tx);
708
709                 /*
710                  * XXX uiomove could block forever (eg. nfs-backed
711                  * pages).  There needs to be a uiolockdown() function
712                  * to lock the pages in memory, so that uiomove won't
713                  * block.
714                  */
715                 err = uiomove((char *)db->db_data + bufoff, tocpy,
716                     UIO_WRITE, uio);
717
718                 if (tocpy == db->db_size)
719                         dmu_buf_fill_done(db, tx);
720
721                 if (err)
722                         break;
723
724                 size -= tocpy;
725         }
726         dmu_buf_rele_array(dbp, numbufs, FTAG);
727         return (err);
728 }
729
730 #ifndef __FreeBSD__
731 int
732 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
733     page_t *pp, dmu_tx_t *tx)
734 {
735         dmu_buf_t **dbp;
736         int numbufs, i;
737         int err;
738
739         if (size == 0)
740                 return (0);
741
742         err = dmu_buf_hold_array(os, object, offset, size,
743             FALSE, FTAG, &numbufs, &dbp);
744         if (err)
745                 return (err);
746
747         for (i = 0; i < numbufs; i++) {
748                 int tocpy, copied, thiscpy;
749                 int bufoff;
750                 dmu_buf_t *db = dbp[i];
751                 caddr_t va;
752
753                 ASSERT(size > 0);
754                 ASSERT3U(db->db_size, >=, PAGESIZE);
755
756                 bufoff = offset - db->db_offset;
757                 tocpy = (int)MIN(db->db_size - bufoff, size);
758
759                 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
760
761                 if (tocpy == db->db_size)
762                         dmu_buf_will_fill(db, tx);
763                 else
764                         dmu_buf_will_dirty(db, tx);
765
766                 for (copied = 0; copied < tocpy; copied += PAGESIZE) {
767                         ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
768                         thiscpy = MIN(PAGESIZE, tocpy - copied);
769                         va = zfs_map_page(pp, S_READ);
770                         bcopy(va, (char *)db->db_data + bufoff, thiscpy);
771                         zfs_unmap_page(pp, va);
772                         pp = pp->p_next;
773                         bufoff += PAGESIZE;
774                 }
775
776                 if (tocpy == db->db_size)
777                         dmu_buf_fill_done(db, tx);
778
779                 if (err)
780                         break;
781
782                 offset += tocpy;
783                 size -= tocpy;
784         }
785         dmu_buf_rele_array(dbp, numbufs, FTAG);
786         return (err);
787 }
788 #endif  /* !__FreeBSD__ */
789 #endif  /* _KERNEL */
790
791 typedef struct {
792         dbuf_dirty_record_t     *dr;
793         dmu_sync_cb_t           *done;
794         void                    *arg;
795 } dmu_sync_arg_t;
796
797 /* ARGSUSED */
798 static void
799 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
800 {
801         blkptr_t *bp = zio->io_bp;
802
803         if (!BP_IS_HOLE(bp)) {
804                 dmu_sync_arg_t *in = varg;
805                 dbuf_dirty_record_t *dr = in->dr;
806                 dmu_buf_impl_t *db = dr->dr_dbuf;
807                 ASSERT(BP_GET_TYPE(bp) == db->db_dnode->dn_type);
808                 ASSERT(BP_GET_LEVEL(bp) == 0);
809                 bp->blk_fill = 1;
810         }
811 }
812
813 /* ARGSUSED */
814 static void
815 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
816 {
817         dmu_sync_arg_t *in = varg;
818         dbuf_dirty_record_t *dr = in->dr;
819         dmu_buf_impl_t *db = dr->dr_dbuf;
820         dmu_sync_cb_t *done = in->done;
821
822         mutex_enter(&db->db_mtx);
823         ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
824         dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */
825         dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
826         cv_broadcast(&db->db_changed);
827         mutex_exit(&db->db_mtx);
828
829         if (done)
830                 done(&(db->db), in->arg);
831
832         kmem_free(in, sizeof (dmu_sync_arg_t));
833 }
834
835 /*
836  * Intent log support: sync the block associated with db to disk.
837  * N.B. and XXX: the caller is responsible for making sure that the
838  * data isn't changing while dmu_sync() is writing it.
839  *
840  * Return values:
841  *
842  *      EEXIST: this txg has already been synced, so there's nothing to to.
843  *              The caller should not log the write.
844  *
845  *      ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
846  *              The caller should not log the write.
847  *
848  *      EALREADY: this block is already in the process of being synced.
849  *              The caller should track its progress (somehow).
850  *
851  *      EINPROGRESS: the IO has been initiated.
852  *              The caller should log this blkptr in the callback.
853  *
854  *      0: completed.  Sets *bp to the blkptr just written.
855  *              The caller should log this blkptr immediately.
856  */
857 int
858 dmu_sync(zio_t *pio, dmu_buf_t *db_fake,
859     blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg)
860 {
861         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
862         objset_impl_t *os = db->db_objset;
863         dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
864         tx_state_t *tx = &dp->dp_tx;
865         dbuf_dirty_record_t *dr;
866         dmu_sync_arg_t *in;
867         zbookmark_t zb;
868         writeprops_t wp = { 0 };
869         zio_t *zio;
870         int err;
871
872         ASSERT(BP_IS_HOLE(bp));
873         ASSERT(txg != 0);
874
875         dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
876             txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
877
878         /*
879          * XXX - would be nice if we could do this without suspending...
880          */
881         txg_suspend(dp);
882
883         /*
884          * If this txg already synced, there's nothing to do.
885          */
886         if (txg <= tx->tx_synced_txg) {
887                 txg_resume(dp);
888                 /*
889                  * If we're running ziltest, we need the blkptr regardless.
890                  */
891                 if (txg > spa_freeze_txg(dp->dp_spa)) {
892                         /* if db_blkptr == NULL, this was an empty write */
893                         if (db->db_blkptr)
894                                 *bp = *db->db_blkptr; /* structure assignment */
895                         return (0);
896                 }
897                 return (EEXIST);
898         }
899
900         mutex_enter(&db->db_mtx);
901
902         if (txg == tx->tx_syncing_txg) {
903                 while (db->db_data_pending) {
904                         /*
905                          * IO is in-progress.  Wait for it to finish.
906                          * XXX - would be nice to be able to somehow "attach"
907                          * this zio to the parent zio passed in.
908                          */
909                         cv_wait(&db->db_changed, &db->db_mtx);
910                         if (!db->db_data_pending &&
911                             db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) {
912                                 /*
913                                  * IO was compressed away
914                                  */
915                                 *bp = *db->db_blkptr; /* structure assignment */
916                                 mutex_exit(&db->db_mtx);
917                                 txg_resume(dp);
918                                 return (0);
919                         }
920                         ASSERT(db->db_data_pending ||
921                             (db->db_blkptr && db->db_blkptr->blk_birth == txg));
922                 }
923
924                 if (db->db_blkptr && db->db_blkptr->blk_birth == txg) {
925                         /*
926                          * IO is already completed.
927                          */
928                         *bp = *db->db_blkptr; /* structure assignment */
929                         mutex_exit(&db->db_mtx);
930                         txg_resume(dp);
931                         return (0);
932                 }
933         }
934
935         dr = db->db_last_dirty;
936         while (dr && dr->dr_txg > txg)
937                 dr = dr->dr_next;
938         if (dr == NULL || dr->dr_txg < txg) {
939                 /*
940                  * This dbuf isn't dirty, must have been free_range'd.
941                  * There's no need to log writes to freed blocks, so we're done.
942                  */
943                 mutex_exit(&db->db_mtx);
944                 txg_resume(dp);
945                 return (ENOENT);
946         }
947
948         ASSERT(dr->dr_txg == txg);
949         if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
950                 /*
951                  * We have already issued a sync write for this buffer.
952                  */
953                 mutex_exit(&db->db_mtx);
954                 txg_resume(dp);
955                 return (EALREADY);
956         } else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
957                 /*
958                  * This buffer has already been synced.  It could not
959                  * have been dirtied since, or we would have cleared the state.
960                  */
961                 *bp = dr->dt.dl.dr_overridden_by; /* structure assignment */
962                 mutex_exit(&db->db_mtx);
963                 txg_resume(dp);
964                 return (0);
965         }
966
967         dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
968         in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
969         in->dr = dr;
970         in->done = done;
971         in->arg = arg;
972         mutex_exit(&db->db_mtx);
973         txg_resume(dp);
974
975         zb.zb_objset = os->os_dsl_dataset->ds_object;
976         zb.zb_object = db->db.db_object;
977         zb.zb_level = db->db_level;
978         zb.zb_blkid = db->db_blkid;
979
980         wp.wp_type = db->db_dnode->dn_type;
981         wp.wp_level = db->db_level;
982         wp.wp_copies = os->os_copies;
983         wp.wp_dnchecksum = db->db_dnode->dn_checksum;
984         wp.wp_oschecksum = os->os_checksum;
985         wp.wp_dncompress = db->db_dnode->dn_compress;
986         wp.wp_oscompress = os->os_compress;
987
988         ASSERT(BP_IS_HOLE(bp));
989
990         zio = arc_write(pio, os->os_spa, &wp, DBUF_IS_L2CACHEABLE(db),
991             txg, bp, dr->dt.dl.dr_data, dmu_sync_ready, dmu_sync_done, in,
992             ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
993
994         if (pio) {
995                 zio_nowait(zio);
996                 err = EINPROGRESS;
997         } else {
998                 err = zio_wait(zio);
999                 ASSERT(err == 0);
1000         }
1001         return (err);
1002 }
1003
1004 int
1005 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1006         dmu_tx_t *tx)
1007 {
1008         dnode_t *dn;
1009         int err;
1010
1011         err = dnode_hold(os->os, object, FTAG, &dn);
1012         if (err)
1013                 return (err);
1014         err = dnode_set_blksz(dn, size, ibs, tx);
1015         dnode_rele(dn, FTAG);
1016         return (err);
1017 }
1018
1019 void
1020 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1021         dmu_tx_t *tx)
1022 {
1023         dnode_t *dn;
1024
1025         /* XXX assumes dnode_hold will not get an i/o error */
1026         (void) dnode_hold(os->os, object, FTAG, &dn);
1027         ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1028         dn->dn_checksum = checksum;
1029         dnode_setdirty(dn, tx);
1030         dnode_rele(dn, FTAG);
1031 }
1032
1033 void
1034 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1035         dmu_tx_t *tx)
1036 {
1037         dnode_t *dn;
1038
1039         /* XXX assumes dnode_hold will not get an i/o error */
1040         (void) dnode_hold(os->os, object, FTAG, &dn);
1041         ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1042         dn->dn_compress = compress;
1043         dnode_setdirty(dn, tx);
1044         dnode_rele(dn, FTAG);
1045 }
1046
1047 int
1048 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1049 {
1050         dnode_t *dn;
1051         int i, err;
1052
1053         err = dnode_hold(os->os, object, FTAG, &dn);
1054         if (err)
1055                 return (err);
1056         /*
1057          * Sync any current changes before
1058          * we go trundling through the block pointers.
1059          */
1060         for (i = 0; i < TXG_SIZE; i++) {
1061                 if (list_link_active(&dn->dn_dirty_link[i]))
1062                         break;
1063         }
1064         if (i != TXG_SIZE) {
1065                 dnode_rele(dn, FTAG);
1066                 txg_wait_synced(dmu_objset_pool(os), 0);
1067                 err = dnode_hold(os->os, object, FTAG, &dn);
1068                 if (err)
1069                         return (err);
1070         }
1071
1072         err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1073         dnode_rele(dn, FTAG);
1074
1075         return (err);
1076 }
1077
1078 void
1079 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1080 {
1081         rw_enter(&dn->dn_struct_rwlock, RW_READER);
1082         mutex_enter(&dn->dn_mtx);
1083
1084         doi->doi_data_block_size = dn->dn_datablksz;
1085         doi->doi_metadata_block_size = dn->dn_indblkshift ?
1086             1ULL << dn->dn_indblkshift : 0;
1087         doi->doi_indirection = dn->dn_nlevels;
1088         doi->doi_checksum = dn->dn_checksum;
1089         doi->doi_compress = dn->dn_compress;
1090         doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) +
1091             SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT;
1092         doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
1093         doi->doi_type = dn->dn_type;
1094         doi->doi_bonus_size = dn->dn_bonuslen;
1095         doi->doi_bonus_type = dn->dn_bonustype;
1096
1097         mutex_exit(&dn->dn_mtx);
1098         rw_exit(&dn->dn_struct_rwlock);
1099 }
1100
1101 /*
1102  * Get information on a DMU object.
1103  * If doi is NULL, just indicates whether the object exists.
1104  */
1105 int
1106 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1107 {
1108         dnode_t *dn;
1109         int err = dnode_hold(os->os, object, FTAG, &dn);
1110
1111         if (err)
1112                 return (err);
1113
1114         if (doi != NULL)
1115                 dmu_object_info_from_dnode(dn, doi);
1116
1117         dnode_rele(dn, FTAG);
1118         return (0);
1119 }
1120
1121 /*
1122  * As above, but faster; can be used when you have a held dbuf in hand.
1123  */
1124 void
1125 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1126 {
1127         dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1128 }
1129
1130 /*
1131  * Faster still when you only care about the size.
1132  * This is specifically optimized for zfs_getattr().
1133  */
1134 void
1135 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1136 {
1137         dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1138
1139         *blksize = dn->dn_datablksz;
1140         /* add 1 for dnode space */
1141         *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
1142             SPA_MINBLOCKSHIFT) + 1;
1143 }
1144
1145 void
1146 byteswap_uint64_array(void *vbuf, size_t size)
1147 {
1148         uint64_t *buf = vbuf;
1149         size_t count = size >> 3;
1150         int i;
1151
1152         ASSERT((size & 7) == 0);
1153
1154         for (i = 0; i < count; i++)
1155                 buf[i] = BSWAP_64(buf[i]);
1156 }
1157
1158 void
1159 byteswap_uint32_array(void *vbuf, size_t size)
1160 {
1161         uint32_t *buf = vbuf;
1162         size_t count = size >> 2;
1163         int i;
1164
1165         ASSERT((size & 3) == 0);
1166
1167         for (i = 0; i < count; i++)
1168                 buf[i] = BSWAP_32(buf[i]);
1169 }
1170
1171 void
1172 byteswap_uint16_array(void *vbuf, size_t size)
1173 {
1174         uint16_t *buf = vbuf;
1175         size_t count = size >> 1;
1176         int i;
1177
1178         ASSERT((size & 1) == 0);
1179
1180         for (i = 0; i < count; i++)
1181                 buf[i] = BSWAP_16(buf[i]);
1182 }
1183
1184 /* ARGSUSED */
1185 void
1186 byteswap_uint8_array(void *vbuf, size_t size)
1187 {
1188 }
1189
1190 void
1191 dmu_init(void)
1192 {
1193         dbuf_init();
1194         dnode_init();
1195         arc_init();
1196         l2arc_init();
1197 }
1198
1199 void
1200 dmu_fini(void)
1201 {
1202         arc_fini();
1203         dnode_fini();
1204         dbuf_fini();
1205         l2arc_fini();
1206 }