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