]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dmu.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 /*
375  * Get the next "chunk" of file data to free.  We traverse the file from
376  * the end so that the file gets shorter over time (if we crashes in the
377  * middle, this will leave us in a better state).  We find allocated file
378  * data by simply searching the allocated level 1 indirects.
379  */
380 static int
381 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t limit)
382 {
383         uint64_t len = *start - limit;
384         uint64_t blkcnt = 0;
385         uint64_t maxblks = DMU_MAX_ACCESS / (1ULL << (dn->dn_indblkshift + 1));
386         uint64_t iblkrange =
387             dn->dn_datablksz * EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
388
389         ASSERT(limit <= *start);
390
391         if (len <= iblkrange * maxblks) {
392                 *start = limit;
393                 return (0);
394         }
395         ASSERT(ISP2(iblkrange));
396
397         while (*start > limit && blkcnt < maxblks) {
398                 int err;
399
400                 /* find next allocated L1 indirect */
401                 err = dnode_next_offset(dn,
402                     DNODE_FIND_BACKWARDS, start, 2, 1, 0);
403
404                 /* if there are no more, then we are done */
405                 if (err == ESRCH) {
406                         *start = limit;
407                         return (0);
408                 } else if (err) {
409                         return (err);
410                 }
411                 blkcnt += 1;
412
413                 /* reset offset to end of "next" block back */
414                 *start = P2ALIGN(*start, iblkrange);
415                 if (*start <= limit)
416                         *start = limit;
417                 else
418                         *start -= 1;
419         }
420         return (0);
421 }
422
423 static int
424 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
425     uint64_t length, boolean_t free_dnode)
426 {
427         dmu_tx_t *tx;
428         uint64_t object_size, start, end, len;
429         boolean_t trunc = (length == DMU_OBJECT_END);
430         int align, err;
431
432         align = 1 << dn->dn_datablkshift;
433         ASSERT(align > 0);
434         object_size = align == 1 ? dn->dn_datablksz :
435             (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
436
437         if (trunc || (end = offset + length) > object_size)
438                 end = object_size;
439         if (end <= offset)
440                 return (0);
441         length = end - offset;
442
443         while (length) {
444                 start = end;
445                 err = get_next_chunk(dn, &start, offset);
446                 if (err)
447                         return (err);
448                 len = trunc ? DMU_OBJECT_END : end - start;
449
450                 tx = dmu_tx_create(os);
451                 dmu_tx_hold_free(tx, dn->dn_object, start, len);
452                 err = dmu_tx_assign(tx, TXG_WAIT);
453                 if (err) {
454                         dmu_tx_abort(tx);
455                         return (err);
456                 }
457
458                 dnode_free_range(dn, start, trunc ? -1 : len, tx);
459
460                 if (start == 0 && free_dnode) {
461                         ASSERT(trunc);
462                         dnode_free(dn, tx);
463                 }
464
465                 length -= end - start;
466
467                 dmu_tx_commit(tx);
468                 end = start;
469         }
470         return (0);
471 }
472
473 int
474 dmu_free_long_range(objset_t *os, uint64_t object,
475     uint64_t offset, uint64_t length)
476 {
477         dnode_t *dn;
478         int err;
479
480         err = dnode_hold(os->os, object, FTAG, &dn);
481         if (err != 0)
482                 return (err);
483         err = dmu_free_long_range_impl(os, dn, offset, length, FALSE);
484         dnode_rele(dn, FTAG);
485         return (err);
486 }
487
488 int
489 dmu_free_object(objset_t *os, uint64_t object)
490 {
491         dnode_t *dn;
492         dmu_tx_t *tx;
493         int err;
494
495         err = dnode_hold_impl(os->os, object, DNODE_MUST_BE_ALLOCATED,
496             FTAG, &dn);
497         if (err != 0)
498                 return (err);
499         if (dn->dn_nlevels == 1) {
500                 tx = dmu_tx_create(os);
501                 dmu_tx_hold_bonus(tx, object);
502                 dmu_tx_hold_free(tx, dn->dn_object, 0, DMU_OBJECT_END);
503                 err = dmu_tx_assign(tx, TXG_WAIT);
504                 if (err == 0) {
505                         dnode_free_range(dn, 0, DMU_OBJECT_END, tx);
506                         dnode_free(dn, tx);
507                         dmu_tx_commit(tx);
508                 } else {
509                         dmu_tx_abort(tx);
510                 }
511         } else {
512                 err = dmu_free_long_range_impl(os, dn, 0, DMU_OBJECT_END, TRUE);
513         }
514         dnode_rele(dn, FTAG);
515         return (err);
516 }
517
518 int
519 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
520     uint64_t size, dmu_tx_t *tx)
521 {
522         dnode_t *dn;
523         int err = dnode_hold(os->os, object, FTAG, &dn);
524         if (err)
525                 return (err);
526         ASSERT(offset < UINT64_MAX);
527         ASSERT(size == -1ULL || size <= UINT64_MAX - offset);
528         dnode_free_range(dn, offset, size, tx);
529         dnode_rele(dn, FTAG);
530         return (0);
531 }
532
533 int
534 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
535     void *buf)
536 {
537         dnode_t *dn;
538         dmu_buf_t **dbp;
539         int numbufs, i, err;
540
541         err = dnode_hold(os->os, object, FTAG, &dn);
542         if (err)
543                 return (err);
544
545         /*
546          * Deal with odd block sizes, where there can't be data past the first
547          * block.  If we ever do the tail block optimization, we will need to
548          * handle that here as well.
549          */
550         if (dn->dn_datablkshift == 0) {
551                 int newsz = offset > dn->dn_datablksz ? 0 :
552                     MIN(size, dn->dn_datablksz - offset);
553                 bzero((char *)buf + newsz, size - newsz);
554                 size = newsz;
555         }
556
557         while (size > 0) {
558                 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
559
560                 /*
561                  * NB: we could do this block-at-a-time, but it's nice
562                  * to be reading in parallel.
563                  */
564                 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
565                     TRUE, FTAG, &numbufs, &dbp);
566                 if (err)
567                         break;
568
569                 for (i = 0; i < numbufs; i++) {
570                         int tocpy;
571                         int bufoff;
572                         dmu_buf_t *db = dbp[i];
573
574                         ASSERT(size > 0);
575
576                         bufoff = offset - db->db_offset;
577                         tocpy = (int)MIN(db->db_size - bufoff, size);
578
579                         bcopy((char *)db->db_data + bufoff, buf, tocpy);
580
581                         offset += tocpy;
582                         size -= tocpy;
583                         buf = (char *)buf + tocpy;
584                 }
585                 dmu_buf_rele_array(dbp, numbufs, FTAG);
586         }
587         dnode_rele(dn, FTAG);
588         return (err);
589 }
590
591 void
592 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
593     const void *buf, dmu_tx_t *tx)
594 {
595         dmu_buf_t **dbp;
596         int numbufs, i;
597
598         if (size == 0)
599                 return;
600
601         VERIFY(0 == dmu_buf_hold_array(os, object, offset, size,
602             FALSE, FTAG, &numbufs, &dbp));
603
604         for (i = 0; i < numbufs; i++) {
605                 int tocpy;
606                 int bufoff;
607                 dmu_buf_t *db = dbp[i];
608
609                 ASSERT(size > 0);
610
611                 bufoff = offset - db->db_offset;
612                 tocpy = (int)MIN(db->db_size - bufoff, size);
613
614                 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
615
616                 if (tocpy == db->db_size)
617                         dmu_buf_will_fill(db, tx);
618                 else
619                         dmu_buf_will_dirty(db, tx);
620
621                 bcopy(buf, (char *)db->db_data + bufoff, tocpy);
622
623                 if (tocpy == db->db_size)
624                         dmu_buf_fill_done(db, tx);
625
626                 offset += tocpy;
627                 size -= tocpy;
628                 buf = (char *)buf + tocpy;
629         }
630         dmu_buf_rele_array(dbp, numbufs, FTAG);
631 }
632
633 #ifdef _KERNEL
634 int
635 dmu_read_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size)
636 {
637         dmu_buf_t **dbp;
638         int numbufs, i, err;
639
640         /*
641          * NB: we could do this block-at-a-time, but it's nice
642          * to be reading in parallel.
643          */
644         err = dmu_buf_hold_array(os, object, uio->uio_loffset, size, TRUE, FTAG,
645             &numbufs, &dbp);
646         if (err)
647                 return (err);
648
649         for (i = 0; i < numbufs; i++) {
650                 int tocpy;
651                 int bufoff;
652                 dmu_buf_t *db = dbp[i];
653
654                 ASSERT(size > 0);
655
656                 bufoff = uio->uio_loffset - db->db_offset;
657                 tocpy = (int)MIN(db->db_size - bufoff, size);
658
659                 err = uiomove((char *)db->db_data + bufoff, tocpy,
660                     UIO_READ, uio);
661                 if (err)
662                         break;
663
664                 size -= tocpy;
665         }
666         dmu_buf_rele_array(dbp, numbufs, FTAG);
667
668         return (err);
669 }
670
671 int
672 dmu_write_uio(objset_t *os, uint64_t object, uio_t *uio, uint64_t size,
673     dmu_tx_t *tx)
674 {
675         dmu_buf_t **dbp;
676         int numbufs, i;
677         int err = 0;
678
679         if (size == 0)
680                 return (0);
681
682         err = dmu_buf_hold_array(os, object, uio->uio_loffset, size,
683             FALSE, FTAG, &numbufs, &dbp);
684         if (err)
685                 return (err);
686
687         for (i = 0; i < numbufs; i++) {
688                 int tocpy;
689                 int bufoff;
690                 dmu_buf_t *db = dbp[i];
691
692                 ASSERT(size > 0);
693
694                 bufoff = uio->uio_loffset - db->db_offset;
695                 tocpy = (int)MIN(db->db_size - bufoff, size);
696
697                 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
698
699                 if (tocpy == db->db_size)
700                         dmu_buf_will_fill(db, tx);
701                 else
702                         dmu_buf_will_dirty(db, tx);
703
704                 /*
705                  * XXX uiomove could block forever (eg. nfs-backed
706                  * pages).  There needs to be a uiolockdown() function
707                  * to lock the pages in memory, so that uiomove won't
708                  * block.
709                  */
710                 err = uiomove((char *)db->db_data + bufoff, tocpy,
711                     UIO_WRITE, uio);
712
713                 if (tocpy == db->db_size)
714                         dmu_buf_fill_done(db, tx);
715
716                 if (err)
717                         break;
718
719                 size -= tocpy;
720         }
721         dmu_buf_rele_array(dbp, numbufs, FTAG);
722         return (err);
723 }
724
725 #ifndef __FreeBSD__
726 int
727 dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
728     page_t *pp, dmu_tx_t *tx)
729 {
730         dmu_buf_t **dbp;
731         int numbufs, i;
732         int err;
733
734         if (size == 0)
735                 return (0);
736
737         err = dmu_buf_hold_array(os, object, offset, size,
738             FALSE, FTAG, &numbufs, &dbp);
739         if (err)
740                 return (err);
741
742         for (i = 0; i < numbufs; i++) {
743                 int tocpy, copied, thiscpy;
744                 int bufoff;
745                 dmu_buf_t *db = dbp[i];
746                 caddr_t va;
747
748                 ASSERT(size > 0);
749                 ASSERT3U(db->db_size, >=, PAGESIZE);
750
751                 bufoff = offset - db->db_offset;
752                 tocpy = (int)MIN(db->db_size - bufoff, size);
753
754                 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
755
756                 if (tocpy == db->db_size)
757                         dmu_buf_will_fill(db, tx);
758                 else
759                         dmu_buf_will_dirty(db, tx);
760
761                 for (copied = 0; copied < tocpy; copied += PAGESIZE) {
762                         ASSERT3U(pp->p_offset, ==, db->db_offset + bufoff);
763                         thiscpy = MIN(PAGESIZE, tocpy - copied);
764                         va = zfs_map_page(pp, S_READ);
765                         bcopy(va, (char *)db->db_data + bufoff, thiscpy);
766                         zfs_unmap_page(pp, va);
767                         pp = pp->p_next;
768                         bufoff += PAGESIZE;
769                 }
770
771                 if (tocpy == db->db_size)
772                         dmu_buf_fill_done(db, tx);
773
774                 if (err)
775                         break;
776
777                 offset += tocpy;
778                 size -= tocpy;
779         }
780         dmu_buf_rele_array(dbp, numbufs, FTAG);
781         return (err);
782 }
783 #endif  /* !__FreeBSD__ */
784 #endif  /* _KERNEL */
785
786 typedef struct {
787         dbuf_dirty_record_t     *dr;
788         dmu_sync_cb_t           *done;
789         void                    *arg;
790 } dmu_sync_arg_t;
791
792 /* ARGSUSED */
793 static void
794 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
795 {
796         blkptr_t *bp = zio->io_bp;
797
798         if (!BP_IS_HOLE(bp)) {
799                 dmu_sync_arg_t *in = varg;
800                 dbuf_dirty_record_t *dr = in->dr;
801                 dmu_buf_impl_t *db = dr->dr_dbuf;
802                 ASSERT(BP_GET_TYPE(bp) == db->db_dnode->dn_type);
803                 ASSERT(BP_GET_LEVEL(bp) == 0);
804                 bp->blk_fill = 1;
805         }
806 }
807
808 /* ARGSUSED */
809 static void
810 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
811 {
812         dmu_sync_arg_t *in = varg;
813         dbuf_dirty_record_t *dr = in->dr;
814         dmu_buf_impl_t *db = dr->dr_dbuf;
815         dmu_sync_cb_t *done = in->done;
816
817         mutex_enter(&db->db_mtx);
818         ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
819         dr->dt.dl.dr_overridden_by = *zio->io_bp; /* structure assignment */
820         dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
821         cv_broadcast(&db->db_changed);
822         mutex_exit(&db->db_mtx);
823
824         if (done)
825                 done(&(db->db), in->arg);
826
827         kmem_free(in, sizeof (dmu_sync_arg_t));
828 }
829
830 /*
831  * Intent log support: sync the block associated with db to disk.
832  * N.B. and XXX: the caller is responsible for making sure that the
833  * data isn't changing while dmu_sync() is writing it.
834  *
835  * Return values:
836  *
837  *      EEXIST: this txg has already been synced, so there's nothing to to.
838  *              The caller should not log the write.
839  *
840  *      ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
841  *              The caller should not log the write.
842  *
843  *      EALREADY: this block is already in the process of being synced.
844  *              The caller should track its progress (somehow).
845  *
846  *      EINPROGRESS: the IO has been initiated.
847  *              The caller should log this blkptr in the callback.
848  *
849  *      0: completed.  Sets *bp to the blkptr just written.
850  *              The caller should log this blkptr immediately.
851  */
852 int
853 dmu_sync(zio_t *pio, dmu_buf_t *db_fake,
854     blkptr_t *bp, uint64_t txg, dmu_sync_cb_t *done, void *arg)
855 {
856         dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
857         objset_impl_t *os = db->db_objset;
858         dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
859         tx_state_t *tx = &dp->dp_tx;
860         dbuf_dirty_record_t *dr;
861         dmu_sync_arg_t *in;
862         zbookmark_t zb;
863         writeprops_t wp = { 0 };
864         zio_t *zio;
865         int err;
866
867         ASSERT(BP_IS_HOLE(bp));
868         ASSERT(txg != 0);
869
870         dprintf("dmu_sync txg=%llu, s,o,q %llu %llu %llu\n",
871             txg, tx->tx_synced_txg, tx->tx_open_txg, tx->tx_quiesced_txg);
872
873         /*
874          * XXX - would be nice if we could do this without suspending...
875          */
876         txg_suspend(dp);
877
878         /*
879          * If this txg already synced, there's nothing to do.
880          */
881         if (txg <= tx->tx_synced_txg) {
882                 txg_resume(dp);
883                 /*
884                  * If we're running ziltest, we need the blkptr regardless.
885                  */
886                 if (txg > spa_freeze_txg(dp->dp_spa)) {
887                         /* if db_blkptr == NULL, this was an empty write */
888                         if (db->db_blkptr)
889                                 *bp = *db->db_blkptr; /* structure assignment */
890                         return (0);
891                 }
892                 return (EEXIST);
893         }
894
895         mutex_enter(&db->db_mtx);
896
897         if (txg == tx->tx_syncing_txg) {
898                 while (db->db_data_pending) {
899                         /*
900                          * IO is in-progress.  Wait for it to finish.
901                          * XXX - would be nice to be able to somehow "attach"
902                          * this zio to the parent zio passed in.
903                          */
904                         cv_wait(&db->db_changed, &db->db_mtx);
905                         if (!db->db_data_pending &&
906                             db->db_blkptr && BP_IS_HOLE(db->db_blkptr)) {
907                                 /*
908                                  * IO was compressed away
909                                  */
910                                 *bp = *db->db_blkptr; /* structure assignment */
911                                 mutex_exit(&db->db_mtx);
912                                 txg_resume(dp);
913                                 return (0);
914                         }
915                         ASSERT(db->db_data_pending ||
916                             (db->db_blkptr && db->db_blkptr->blk_birth == txg));
917                 }
918
919                 if (db->db_blkptr && db->db_blkptr->blk_birth == txg) {
920                         /*
921                          * IO is already completed.
922                          */
923                         *bp = *db->db_blkptr; /* structure assignment */
924                         mutex_exit(&db->db_mtx);
925                         txg_resume(dp);
926                         return (0);
927                 }
928         }
929
930         dr = db->db_last_dirty;
931         while (dr && dr->dr_txg > txg)
932                 dr = dr->dr_next;
933         if (dr == NULL || dr->dr_txg < txg) {
934                 /*
935                  * This dbuf isn't dirty, must have been free_range'd.
936                  * There's no need to log writes to freed blocks, so we're done.
937                  */
938                 mutex_exit(&db->db_mtx);
939                 txg_resume(dp);
940                 return (ENOENT);
941         }
942
943         ASSERT(dr->dr_txg == txg);
944         if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
945                 /*
946                  * We have already issued a sync write for this buffer.
947                  */
948                 mutex_exit(&db->db_mtx);
949                 txg_resume(dp);
950                 return (EALREADY);
951         } else if (dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
952                 /*
953                  * This buffer has already been synced.  It could not
954                  * have been dirtied since, or we would have cleared the state.
955                  */
956                 *bp = dr->dt.dl.dr_overridden_by; /* structure assignment */
957                 mutex_exit(&db->db_mtx);
958                 txg_resume(dp);
959                 return (0);
960         }
961
962         dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
963         in = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
964         in->dr = dr;
965         in->done = done;
966         in->arg = arg;
967         mutex_exit(&db->db_mtx);
968         txg_resume(dp);
969
970         zb.zb_objset = os->os_dsl_dataset->ds_object;
971         zb.zb_object = db->db.db_object;
972         zb.zb_level = db->db_level;
973         zb.zb_blkid = db->db_blkid;
974
975         wp.wp_type = db->db_dnode->dn_type;
976         wp.wp_level = db->db_level;
977         wp.wp_copies = os->os_copies;
978         wp.wp_dnchecksum = db->db_dnode->dn_checksum;
979         wp.wp_oschecksum = os->os_checksum;
980         wp.wp_dncompress = db->db_dnode->dn_compress;
981         wp.wp_oscompress = os->os_compress;
982
983         ASSERT(BP_IS_HOLE(bp));
984
985         zio = arc_write(pio, os->os_spa, &wp, DBUF_IS_L2CACHEABLE(db),
986             txg, bp, dr->dt.dl.dr_data, dmu_sync_ready, dmu_sync_done, in,
987             ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
988
989         if (pio) {
990                 zio_nowait(zio);
991                 err = EINPROGRESS;
992         } else {
993                 err = zio_wait(zio);
994                 ASSERT(err == 0);
995         }
996         return (err);
997 }
998
999 int
1000 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
1001         dmu_tx_t *tx)
1002 {
1003         dnode_t *dn;
1004         int err;
1005
1006         err = dnode_hold(os->os, object, FTAG, &dn);
1007         if (err)
1008                 return (err);
1009         err = dnode_set_blksz(dn, size, ibs, tx);
1010         dnode_rele(dn, FTAG);
1011         return (err);
1012 }
1013
1014 void
1015 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
1016         dmu_tx_t *tx)
1017 {
1018         dnode_t *dn;
1019
1020         /* XXX assumes dnode_hold will not get an i/o error */
1021         (void) dnode_hold(os->os, object, FTAG, &dn);
1022         ASSERT(checksum < ZIO_CHECKSUM_FUNCTIONS);
1023         dn->dn_checksum = checksum;
1024         dnode_setdirty(dn, tx);
1025         dnode_rele(dn, FTAG);
1026 }
1027
1028 void
1029 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
1030         dmu_tx_t *tx)
1031 {
1032         dnode_t *dn;
1033
1034         /* XXX assumes dnode_hold will not get an i/o error */
1035         (void) dnode_hold(os->os, object, FTAG, &dn);
1036         ASSERT(compress < ZIO_COMPRESS_FUNCTIONS);
1037         dn->dn_compress = compress;
1038         dnode_setdirty(dn, tx);
1039         dnode_rele(dn, FTAG);
1040 }
1041
1042 int
1043 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
1044 {
1045         dnode_t *dn;
1046         int i, err;
1047
1048         err = dnode_hold(os->os, object, FTAG, &dn);
1049         if (err)
1050                 return (err);
1051         /*
1052          * Sync any current changes before
1053          * we go trundling through the block pointers.
1054          */
1055         for (i = 0; i < TXG_SIZE; i++) {
1056                 if (list_link_active(&dn->dn_dirty_link[i]))
1057                         break;
1058         }
1059         if (i != TXG_SIZE) {
1060                 dnode_rele(dn, FTAG);
1061                 txg_wait_synced(dmu_objset_pool(os), 0);
1062                 err = dnode_hold(os->os, object, FTAG, &dn);
1063                 if (err)
1064                         return (err);
1065         }
1066
1067         err = dnode_next_offset(dn, (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
1068         dnode_rele(dn, FTAG);
1069
1070         return (err);
1071 }
1072
1073 void
1074 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
1075 {
1076         rw_enter(&dn->dn_struct_rwlock, RW_READER);
1077         mutex_enter(&dn->dn_mtx);
1078
1079         doi->doi_data_block_size = dn->dn_datablksz;
1080         doi->doi_metadata_block_size = dn->dn_indblkshift ?
1081             1ULL << dn->dn_indblkshift : 0;
1082         doi->doi_indirection = dn->dn_nlevels;
1083         doi->doi_checksum = dn->dn_checksum;
1084         doi->doi_compress = dn->dn_compress;
1085         doi->doi_physical_blks = (DN_USED_BYTES(dn->dn_phys) +
1086             SPA_MINBLOCKSIZE/2) >> SPA_MINBLOCKSHIFT;
1087         doi->doi_max_block_offset = dn->dn_phys->dn_maxblkid;
1088         doi->doi_type = dn->dn_type;
1089         doi->doi_bonus_size = dn->dn_bonuslen;
1090         doi->doi_bonus_type = dn->dn_bonustype;
1091
1092         mutex_exit(&dn->dn_mtx);
1093         rw_exit(&dn->dn_struct_rwlock);
1094 }
1095
1096 /*
1097  * Get information on a DMU object.
1098  * If doi is NULL, just indicates whether the object exists.
1099  */
1100 int
1101 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
1102 {
1103         dnode_t *dn;
1104         int err = dnode_hold(os->os, object, FTAG, &dn);
1105
1106         if (err)
1107                 return (err);
1108
1109         if (doi != NULL)
1110                 dmu_object_info_from_dnode(dn, doi);
1111
1112         dnode_rele(dn, FTAG);
1113         return (0);
1114 }
1115
1116 /*
1117  * As above, but faster; can be used when you have a held dbuf in hand.
1118  */
1119 void
1120 dmu_object_info_from_db(dmu_buf_t *db, dmu_object_info_t *doi)
1121 {
1122         dmu_object_info_from_dnode(((dmu_buf_impl_t *)db)->db_dnode, doi);
1123 }
1124
1125 /*
1126  * Faster still when you only care about the size.
1127  * This is specifically optimized for zfs_getattr().
1128  */
1129 void
1130 dmu_object_size_from_db(dmu_buf_t *db, uint32_t *blksize, u_longlong_t *nblk512)
1131 {
1132         dnode_t *dn = ((dmu_buf_impl_t *)db)->db_dnode;
1133
1134         *blksize = dn->dn_datablksz;
1135         /* add 1 for dnode space */
1136         *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
1137             SPA_MINBLOCKSHIFT) + 1;
1138 }
1139
1140 void
1141 byteswap_uint64_array(void *vbuf, size_t size)
1142 {
1143         uint64_t *buf = vbuf;
1144         size_t count = size >> 3;
1145         int i;
1146
1147         ASSERT((size & 7) == 0);
1148
1149         for (i = 0; i < count; i++)
1150                 buf[i] = BSWAP_64(buf[i]);
1151 }
1152
1153 void
1154 byteswap_uint32_array(void *vbuf, size_t size)
1155 {
1156         uint32_t *buf = vbuf;
1157         size_t count = size >> 2;
1158         int i;
1159
1160         ASSERT((size & 3) == 0);
1161
1162         for (i = 0; i < count; i++)
1163                 buf[i] = BSWAP_32(buf[i]);
1164 }
1165
1166 void
1167 byteswap_uint16_array(void *vbuf, size_t size)
1168 {
1169         uint16_t *buf = vbuf;
1170         size_t count = size >> 1;
1171         int i;
1172
1173         ASSERT((size & 1) == 0);
1174
1175         for (i = 0; i < count; i++)
1176                 buf[i] = BSWAP_16(buf[i]);
1177 }
1178
1179 /* ARGSUSED */
1180 void
1181 byteswap_uint8_array(void *vbuf, size_t size)
1182 {
1183 }
1184
1185 void
1186 dmu_init(void)
1187 {
1188         dbuf_init();
1189         dnode_init();
1190         zfetch_init();
1191         arc_init();
1192         l2arc_init();
1193 }
1194
1195 void
1196 dmu_fini(void)
1197 {
1198         arc_fini();
1199         zfetch_fini();
1200         dnode_fini();
1201         dbuf_fini();
1202         l2arc_fini();
1203 }