txg is spelled as tgx in places
[zfs.git] / module / zfs / dsl_pool.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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011 by Delphix. All rights reserved.
24  */
25
26 #include <sys/dsl_pool.h>
27 #include <sys/dsl_dataset.h>
28 #include <sys/dsl_prop.h>
29 #include <sys/dsl_dir.h>
30 #include <sys/dsl_synctask.h>
31 #include <sys/dsl_scan.h>
32 #include <sys/dnode.h>
33 #include <sys/dmu_tx.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/arc.h>
36 #include <sys/zap.h>
37 #include <sys/zio.h>
38 #include <sys/zfs_context.h>
39 #include <sys/fs/zfs.h>
40 #include <sys/zfs_znode.h>
41 #include <sys/spa_impl.h>
42 #include <sys/dsl_deadlist.h>
43
44 int zfs_no_write_throttle = 0;
45 int zfs_write_limit_shift = 3;                  /* 1/8th of physical memory */
46 int zfs_txg_synctime_ms = 1000;         /* target millisecs to sync a txg */
47
48 unsigned long zfs_write_limit_min = 32 << 20;   /* min write limit is 32MB */
49 unsigned long zfs_write_limit_max = 0;          /* max data payload per txg */
50 unsigned long zfs_write_limit_inflated = 0;
51 unsigned long zfs_write_limit_override = 0;
52
53 kmutex_t zfs_write_limit_lock;
54
55 static pgcnt_t old_physmem = 0;
56
57 int
58 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
59 {
60         uint64_t obj;
61         int err;
62
63         err = zap_lookup(dp->dp_meta_objset,
64             dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
65             name, sizeof (obj), 1, &obj);
66         if (err)
67                 return (err);
68
69         return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
70 }
71
72 static dsl_pool_t *
73 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
74 {
75         dsl_pool_t *dp;
76         blkptr_t *bp = spa_get_rootblkptr(spa);
77
78         dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
79         dp->dp_spa = spa;
80         dp->dp_meta_rootbp = *bp;
81         rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
82         dp->dp_write_limit = zfs_write_limit_min;
83         txg_init(dp, txg);
84
85         txg_list_create(&dp->dp_dirty_datasets,
86             offsetof(dsl_dataset_t, ds_dirty_link));
87         txg_list_create(&dp->dp_dirty_dirs,
88             offsetof(dsl_dir_t, dd_dirty_link));
89         txg_list_create(&dp->dp_sync_tasks,
90             offsetof(dsl_sync_task_group_t, dstg_node));
91         list_create(&dp->dp_synced_datasets, sizeof (dsl_dataset_t),
92             offsetof(dsl_dataset_t, ds_synced_link));
93
94         mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
95
96         dp->dp_iput_taskq = taskq_create("zfs_iput_taskq", 1, minclsyspri,
97             1, 4, 0);
98
99         return (dp);
100 }
101
102 int
103 dsl_pool_open(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
104 {
105         int err;
106         dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
107         dsl_dir_t *dd;
108         dsl_dataset_t *ds;
109         uint64_t obj;
110
111         rw_enter(&dp->dp_config_rwlock, RW_WRITER);
112         err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
113             &dp->dp_meta_objset);
114         if (err)
115                 goto out;
116
117         err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
118             DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
119             &dp->dp_root_dir_obj);
120         if (err)
121                 goto out;
122
123         err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
124             NULL, dp, &dp->dp_root_dir);
125         if (err)
126                 goto out;
127
128         err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
129         if (err)
130                 goto out;
131
132         if (spa_version(spa) >= SPA_VERSION_ORIGIN) {
133                 err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
134                 if (err)
135                         goto out;
136                 err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
137                     FTAG, &ds);
138                 if (err == 0) {
139                         err = dsl_dataset_hold_obj(dp,
140                             ds->ds_phys->ds_prev_snap_obj, dp,
141                             &dp->dp_origin_snap);
142                         dsl_dataset_rele(ds, FTAG);
143                 }
144                 dsl_dir_close(dd, dp);
145                 if (err)
146                         goto out;
147         }
148
149         if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
150                 err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
151                     &dp->dp_free_dir);
152                 if (err)
153                         goto out;
154
155                 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
156                     DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
157                 if (err)
158                         goto out;
159                 VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
160                     dp->dp_meta_objset, obj));
161         }
162
163         err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
164             DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
165             &dp->dp_tmp_userrefs_obj);
166         if (err == ENOENT)
167                 err = 0;
168         if (err)
169                 goto out;
170
171         err = dsl_scan_init(dp, txg);
172
173 out:
174         rw_exit(&dp->dp_config_rwlock);
175         if (err)
176                 dsl_pool_close(dp);
177         else
178                 *dpp = dp;
179
180         return (err);
181 }
182
183 void
184 dsl_pool_close(dsl_pool_t *dp)
185 {
186         /* drop our references from dsl_pool_open() */
187
188         /*
189          * Since we held the origin_snap from "syncing" context (which
190          * includes pool-opening context), it actually only got a "ref"
191          * and not a hold, so just drop that here.
192          */
193         if (dp->dp_origin_snap)
194                 dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
195         if (dp->dp_mos_dir)
196                 dsl_dir_close(dp->dp_mos_dir, dp);
197         if (dp->dp_free_dir)
198                 dsl_dir_close(dp->dp_free_dir, dp);
199         if (dp->dp_root_dir)
200                 dsl_dir_close(dp->dp_root_dir, dp);
201
202         bpobj_close(&dp->dp_free_bpobj);
203
204         /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
205         if (dp->dp_meta_objset)
206                 dmu_objset_evict(dp->dp_meta_objset);
207
208         txg_list_destroy(&dp->dp_dirty_datasets);
209         txg_list_destroy(&dp->dp_sync_tasks);
210         txg_list_destroy(&dp->dp_dirty_dirs);
211         list_destroy(&dp->dp_synced_datasets);
212
213         arc_flush(dp->dp_spa);
214         txg_fini(dp);
215         dsl_scan_fini(dp);
216         rw_destroy(&dp->dp_config_rwlock);
217         mutex_destroy(&dp->dp_lock);
218         taskq_destroy(dp->dp_iput_taskq);
219         if (dp->dp_blkstats)
220                 kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
221         kmem_free(dp, sizeof (dsl_pool_t));
222 }
223
224 dsl_pool_t *
225 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
226 {
227         int err;
228         dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
229         dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
230         objset_t *os;
231         dsl_dataset_t *ds;
232         uint64_t obj;
233
234         /* create and open the MOS (meta-objset) */
235         dp->dp_meta_objset = dmu_objset_create_impl(spa,
236             NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
237
238         /* create the pool directory */
239         err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
240             DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
241         ASSERT3U(err, ==, 0);
242
243         /* Initialize scan structures */
244         VERIFY3U(0, ==, dsl_scan_init(dp, txg));
245
246         /* create and open the root dir */
247         dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
248         VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
249             NULL, dp, &dp->dp_root_dir));
250
251         /* create and open the meta-objset dir */
252         (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
253         VERIFY(0 == dsl_pool_open_special_dir(dp,
254             MOS_DIR_NAME, &dp->dp_mos_dir));
255
256         if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
257                 /* create and open the free dir */
258                 (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
259                     FREE_DIR_NAME, tx);
260                 VERIFY(0 == dsl_pool_open_special_dir(dp,
261                     FREE_DIR_NAME, &dp->dp_free_dir));
262
263                 /* create and open the free_bplist */
264                 obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
265                 VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
266                     DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
267                 VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
268                     dp->dp_meta_objset, obj));
269         }
270
271         if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
272                 dsl_pool_create_origin(dp, tx);
273
274         /* create the root dataset */
275         obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
276
277         /* create the root objset */
278         VERIFY(0 == dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
279         VERIFY(NULL != (os = dmu_objset_create_impl(dp->dp_spa, ds,
280             dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx)));
281 #ifdef _KERNEL
282         zfs_create_fs(os, kcred, zplprops, tx);
283 #endif
284         dsl_dataset_rele(ds, FTAG);
285
286         dmu_tx_commit(tx);
287
288         return (dp);
289 }
290
291 static int
292 deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
293 {
294         dsl_deadlist_t *dl = arg;
295         dsl_pool_t *dp = dmu_objset_pool(dl->dl_os);
296         rw_enter(&dp->dp_config_rwlock, RW_READER);
297         dsl_deadlist_insert(dl, bp, tx);
298         rw_exit(&dp->dp_config_rwlock);
299         return (0);
300 }
301
302 void
303 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
304 {
305         zio_t *zio;
306         dmu_tx_t *tx;
307         dsl_dir_t *dd;
308         dsl_dataset_t *ds;
309         dsl_sync_task_group_t *dstg;
310         objset_t *mos = dp->dp_meta_objset;
311         hrtime_t start, write_time;
312         uint64_t data_written;
313         int err;
314
315         /*
316          * We need to copy dp_space_towrite() before doing
317          * dsl_sync_task_group_sync(), because
318          * dsl_dataset_snapshot_reserve_space() will increase
319          * dp_space_towrite but not actually write anything.
320          */
321         data_written = dp->dp_space_towrite[txg & TXG_MASK];
322
323         tx = dmu_tx_create_assigned(dp, txg);
324
325         dp->dp_read_overhead = 0;
326         start = gethrtime();
327
328         zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
329         while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg))) {
330                 /*
331                  * We must not sync any non-MOS datasets twice, because
332                  * we may have taken a snapshot of them.  However, we
333                  * may sync newly-created datasets on pass 2.
334                  */
335                 ASSERT(!list_link_active(&ds->ds_synced_link));
336                 list_insert_tail(&dp->dp_synced_datasets, ds);
337                 dsl_dataset_sync(ds, zio, tx);
338         }
339         DTRACE_PROBE(pool_sync__1setup);
340         err = zio_wait(zio);
341
342         write_time = gethrtime() - start;
343         ASSERT(err == 0);
344         DTRACE_PROBE(pool_sync__2rootzio);
345
346         for (ds = list_head(&dp->dp_synced_datasets); ds;
347             ds = list_next(&dp->dp_synced_datasets, ds))
348                 dmu_objset_do_userquota_updates(ds->ds_objset, tx);
349
350         /*
351          * Sync the datasets again to push out the changes due to
352          * userspace updates.  This must be done before we process the
353          * sync tasks, because that could cause a snapshot of a dataset
354          * whose ds_bp will be rewritten when we do this 2nd sync.
355          */
356         zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
357         while ((ds = txg_list_remove(&dp->dp_dirty_datasets, txg))) {
358                 ASSERT(list_link_active(&ds->ds_synced_link));
359                 dmu_buf_rele(ds->ds_dbuf, ds);
360                 dsl_dataset_sync(ds, zio, tx);
361         }
362         err = zio_wait(zio);
363
364         /*
365          * Move dead blocks from the pending deadlist to the on-disk
366          * deadlist.
367          */
368         for (ds = list_head(&dp->dp_synced_datasets); ds;
369             ds = list_next(&dp->dp_synced_datasets, ds)) {
370                 bplist_iterate(&ds->ds_pending_deadlist,
371                     deadlist_enqueue_cb, &ds->ds_deadlist, tx);
372         }
373
374         while ((dstg = txg_list_remove(&dp->dp_sync_tasks, txg))) {
375                 /*
376                  * No more sync tasks should have been added while we
377                  * were syncing.
378                  */
379                 ASSERT(spa_sync_pass(dp->dp_spa) == 1);
380                 dsl_sync_task_group_sync(dstg, tx);
381         }
382         DTRACE_PROBE(pool_sync__3task);
383
384         start = gethrtime();
385         while ((dd = txg_list_remove(&dp->dp_dirty_dirs, txg)))
386                 dsl_dir_sync(dd, tx);
387         write_time += gethrtime() - start;
388
389         start = gethrtime();
390         if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
391             list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
392                 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
393                 dmu_objset_sync(mos, zio, tx);
394                 err = zio_wait(zio);
395                 ASSERT(err == 0);
396                 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
397                 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
398         }
399         write_time += gethrtime() - start;
400         DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
401             hrtime_t, dp->dp_read_overhead);
402         write_time -= dp->dp_read_overhead;
403
404         dmu_tx_commit(tx);
405
406         dp->dp_space_towrite[txg & TXG_MASK] = 0;
407         ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
408
409         /*
410          * If the write limit max has not been explicitly set, set it
411          * to a fraction of available physical memory (default 1/8th).
412          * Note that we must inflate the limit because the spa
413          * inflates write sizes to account for data replication.
414          * Check this each sync phase to catch changing memory size.
415          */
416         if (physmem != old_physmem && zfs_write_limit_shift) {
417                 mutex_enter(&zfs_write_limit_lock);
418                 old_physmem = physmem;
419                 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
420                 zfs_write_limit_inflated = MAX(zfs_write_limit_min,
421                     spa_get_asize(dp->dp_spa, zfs_write_limit_max));
422                 mutex_exit(&zfs_write_limit_lock);
423         }
424
425         /*
426          * Attempt to keep the sync time consistent by adjusting the
427          * amount of write traffic allowed into each transaction group.
428          * Weight the throughput calculation towards the current value:
429          *      thru = 3/4 old_thru + 1/4 new_thru
430          *
431          * Note: write_time is in nanosecs, so write_time/MICROSEC
432          * yields millisecs
433          */
434         ASSERT(zfs_write_limit_min > 0);
435         if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
436                 uint64_t throughput = data_written / (write_time / MICROSEC);
437
438                 if (dp->dp_throughput)
439                         dp->dp_throughput = throughput / 4 +
440                             3 * dp->dp_throughput / 4;
441                 else
442                         dp->dp_throughput = throughput;
443                 dp->dp_write_limit = MIN(zfs_write_limit_inflated,
444                     MAX(zfs_write_limit_min,
445                     dp->dp_throughput * zfs_txg_synctime_ms));
446         }
447 }
448
449 void
450 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
451 {
452         dsl_dataset_t *ds;
453         objset_t *os;
454
455         while ((ds = list_head(&dp->dp_synced_datasets))) {
456                 list_remove(&dp->dp_synced_datasets, ds);
457                 os = ds->ds_objset;
458                 zil_clean(os->os_zil, txg);
459                 ASSERT(!dmu_objset_is_dirty(os, txg));
460                 dmu_buf_rele(ds->ds_dbuf, ds);
461         }
462         ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
463 }
464
465 /*
466  * TRUE if the current thread is the tx_sync_thread or if we
467  * are being called from SPA context during pool initialization.
468  */
469 int
470 dsl_pool_sync_context(dsl_pool_t *dp)
471 {
472         return (curthread == dp->dp_tx.tx_sync_thread ||
473             spa_get_dsl(dp->dp_spa) == NULL);
474 }
475
476 uint64_t
477 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
478 {
479         uint64_t space, resv;
480
481         /*
482          * Reserve about 1.6% (1/64), or at least 32MB, for allocation
483          * efficiency.
484          * XXX The intent log is not accounted for, so it must fit
485          * within this slop.
486          *
487          * If we're trying to assess whether it's OK to do a free,
488          * cut the reservation in half to allow forward progress
489          * (e.g. make it possible to rm(1) files from a full pool).
490          */
491         space = spa_get_dspace(dp->dp_spa);
492         resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
493         if (netfree)
494                 resv >>= 1;
495
496         return (space - resv);
497 }
498
499 int
500 dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
501 {
502         uint64_t reserved = 0;
503         uint64_t write_limit = (zfs_write_limit_override ?
504             zfs_write_limit_override : dp->dp_write_limit);
505
506         if (zfs_no_write_throttle) {
507                 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
508                     space);
509                 return (0);
510         }
511
512         /*
513          * Check to see if we have exceeded the maximum allowed IO for
514          * this transaction group.  We can do this without locks since
515          * a little slop here is ok.  Note that we do the reserved check
516          * with only half the requested reserve: this is because the
517          * reserve requests are worst-case, and we really don't want to
518          * throttle based off of worst-case estimates.
519          */
520         if (write_limit > 0) {
521                 reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
522                     + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
523
524                 if (reserved && reserved > write_limit) {
525                         DMU_TX_STAT_BUMP(dmu_tx_write_limit);
526                         return (ERESTART);
527                 }
528         }
529
530         atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
531
532         /*
533          * If this transaction group is over 7/8ths capacity, delay
534          * the caller 1 clock tick.  This will slow down the "fill"
535          * rate until the sync process can catch up with us.
536          */
537         if (reserved && reserved > (write_limit - (write_limit >> 3)))
538                 txg_delay(dp, tx->tx_txg, 1);
539
540         return (0);
541 }
542
543 void
544 dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
545 {
546         ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
547         atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
548 }
549
550 void
551 dsl_pool_memory_pressure(dsl_pool_t *dp)
552 {
553         uint64_t space_inuse = 0;
554         int i;
555
556         if (dp->dp_write_limit == zfs_write_limit_min)
557                 return;
558
559         for (i = 0; i < TXG_SIZE; i++) {
560                 space_inuse += dp->dp_space_towrite[i];
561                 space_inuse += dp->dp_tempreserved[i];
562         }
563         dp->dp_write_limit = MAX(zfs_write_limit_min,
564             MIN(dp->dp_write_limit, space_inuse / 4));
565 }
566
567 void
568 dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
569 {
570         if (space > 0) {
571                 mutex_enter(&dp->dp_lock);
572                 dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
573                 mutex_exit(&dp->dp_lock);
574         }
575 }
576
577 /* ARGSUSED */
578 static int
579 upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
580 {
581         dmu_tx_t *tx = arg;
582         dsl_dataset_t *ds, *prev = NULL;
583         int err;
584         dsl_pool_t *dp = spa_get_dsl(spa);
585
586         err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
587         if (err)
588                 return (err);
589
590         while (ds->ds_phys->ds_prev_snap_obj != 0) {
591                 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
592                     FTAG, &prev);
593                 if (err) {
594                         dsl_dataset_rele(ds, FTAG);
595                         return (err);
596                 }
597
598                 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
599                         break;
600                 dsl_dataset_rele(ds, FTAG);
601                 ds = prev;
602                 prev = NULL;
603         }
604
605         if (prev == NULL) {
606                 prev = dp->dp_origin_snap;
607
608                 /*
609                  * The $ORIGIN can't have any data, or the accounting
610                  * will be wrong.
611                  */
612                 ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
613
614                 /* The origin doesn't get attached to itself */
615                 if (ds->ds_object == prev->ds_object) {
616                         dsl_dataset_rele(ds, FTAG);
617                         return (0);
618                 }
619
620                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
621                 ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
622                 ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
623
624                 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
625                 ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
626
627                 dmu_buf_will_dirty(prev->ds_dbuf, tx);
628                 prev->ds_phys->ds_num_children++;
629
630                 if (ds->ds_phys->ds_next_snap_obj == 0) {
631                         ASSERT(ds->ds_prev == NULL);
632                         VERIFY(0 == dsl_dataset_hold_obj(dp,
633                             ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
634                 }
635         }
636
637         ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
638         ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
639
640         if (prev->ds_phys->ds_next_clones_obj == 0) {
641                 dmu_buf_will_dirty(prev->ds_dbuf, tx);
642                 prev->ds_phys->ds_next_clones_obj =
643                     zap_create(dp->dp_meta_objset,
644                     DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
645         }
646         VERIFY(0 == zap_add_int(dp->dp_meta_objset,
647             prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
648
649         dsl_dataset_rele(ds, FTAG);
650         if (prev != dp->dp_origin_snap)
651                 dsl_dataset_rele(prev, FTAG);
652         return (0);
653 }
654
655 void
656 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
657 {
658         ASSERT(dmu_tx_is_syncing(tx));
659         ASSERT(dp->dp_origin_snap != NULL);
660
661         VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
662             tx, DS_FIND_CHILDREN));
663 }
664
665 /* ARGSUSED */
666 static int
667 upgrade_dir_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
668 {
669         dmu_tx_t *tx = arg;
670         dsl_dataset_t *ds;
671         dsl_pool_t *dp = spa_get_dsl(spa);
672         objset_t *mos = dp->dp_meta_objset;
673
674         VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
675
676         if (ds->ds_dir->dd_phys->dd_origin_obj) {
677                 dsl_dataset_t *origin;
678
679                 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
680                     ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
681
682                 if (origin->ds_dir->dd_phys->dd_clones == 0) {
683                         dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
684                         origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
685                             DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
686                 }
687
688                 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
689                     origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
690
691                 dsl_dataset_rele(origin, FTAG);
692         }
693
694         dsl_dataset_rele(ds, FTAG);
695         return (0);
696 }
697
698 void
699 dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
700 {
701         uint64_t obj;
702
703         ASSERT(dmu_tx_is_syncing(tx));
704
705         (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
706         VERIFY(0 == dsl_pool_open_special_dir(dp,
707             FREE_DIR_NAME, &dp->dp_free_dir));
708
709         /*
710          * We can't use bpobj_alloc(), because spa_version() still
711          * returns the old version, and we need a new-version bpobj with
712          * subobj support.  So call dmu_object_alloc() directly.
713          */
714         obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
715             SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
716         VERIFY3U(0, ==, zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
717             DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
718         VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
719             dp->dp_meta_objset, obj));
720
721         VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL,
722             upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
723 }
724
725 void
726 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
727 {
728         uint64_t dsobj;
729         dsl_dataset_t *ds;
730
731         ASSERT(dmu_tx_is_syncing(tx));
732         ASSERT(dp->dp_origin_snap == NULL);
733
734         /* create the origin dir, ds, & snap-ds */
735         rw_enter(&dp->dp_config_rwlock, RW_WRITER);
736         dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
737             NULL, 0, kcred, tx);
738         VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
739         dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx);
740         VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
741             dp, &dp->dp_origin_snap));
742         dsl_dataset_rele(ds, FTAG);
743         rw_exit(&dp->dp_config_rwlock);
744 }
745
746 taskq_t *
747 dsl_pool_iput_taskq(dsl_pool_t *dp)
748 {
749         return (dp->dp_iput_taskq);
750 }
751
752 /*
753  * Walk through the pool-wide zap object of temporary snapshot user holds
754  * and release them.
755  */
756 void
757 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
758 {
759         zap_attribute_t za;
760         zap_cursor_t zc;
761         objset_t *mos = dp->dp_meta_objset;
762         uint64_t zapobj = dp->dp_tmp_userrefs_obj;
763
764         if (zapobj == 0)
765                 return;
766         ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
767
768         for (zap_cursor_init(&zc, mos, zapobj);
769             zap_cursor_retrieve(&zc, &za) == 0;
770             zap_cursor_advance(&zc)) {
771                 char *htag;
772                 uint64_t dsobj;
773
774                 htag = strchr(za.za_name, '-');
775                 *htag = '\0';
776                 ++htag;
777                 dsobj = strtonum(za.za_name, NULL);
778                 (void) dsl_dataset_user_release_tmp(dp, dsobj, htag, B_FALSE);
779         }
780         zap_cursor_fini(&zc);
781 }
782
783 /*
784  * Create the pool-wide zap object for storing temporary snapshot holds.
785  */
786 void
787 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
788 {
789         objset_t *mos = dp->dp_meta_objset;
790
791         ASSERT(dp->dp_tmp_userrefs_obj == 0);
792         ASSERT(dmu_tx_is_syncing(tx));
793
794         dp->dp_tmp_userrefs_obj = zap_create(mos, DMU_OT_USERREFS,
795             DMU_OT_NONE, 0, tx);
796
797         VERIFY(zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS,
798             sizeof (uint64_t), 1, &dp->dp_tmp_userrefs_obj, tx) == 0);
799 }
800
801 static int
802 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
803     const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
804 {
805         objset_t *mos = dp->dp_meta_objset;
806         uint64_t zapobj = dp->dp_tmp_userrefs_obj;
807         char *name;
808         int error;
809
810         ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
811         ASSERT(dmu_tx_is_syncing(tx));
812
813         /*
814          * If the pool was created prior to SPA_VERSION_USERREFS, the
815          * zap object for temporary holds might not exist yet.
816          */
817         if (zapobj == 0) {
818                 if (holding) {
819                         dsl_pool_user_hold_create_obj(dp, tx);
820                         zapobj = dp->dp_tmp_userrefs_obj;
821                 } else {
822                         return (ENOENT);
823                 }
824         }
825
826         name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
827         if (holding)
828                 error = zap_add(mos, zapobj, name, 8, 1, now, tx);
829         else
830                 error = zap_remove(mos, zapobj, name, tx);
831         strfree(name);
832
833         return (error);
834 }
835
836 /*
837  * Add a temporary hold for the given dataset object and tag.
838  */
839 int
840 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
841     uint64_t *now, dmu_tx_t *tx)
842 {
843         return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
844 }
845
846 /*
847  * Release a temporary hold for the given dataset object and tag.
848  */
849 int
850 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
851     dmu_tx_t *tx)
852 {
853         return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
854             tx, B_FALSE));
855 }
856
857 #if defined(_KERNEL) && defined(HAVE_SPL)
858 module_param(zfs_no_write_throttle, int, 0644);
859 MODULE_PARM_DESC(zfs_no_write_throttle, "Disable write throttling");
860
861 module_param(zfs_write_limit_shift, int, 0444);
862 MODULE_PARM_DESC(zfs_write_limit_shift, "log2(fraction of memory) per txg");
863
864 module_param(zfs_txg_synctime_ms, int, 0644);
865 MODULE_PARM_DESC(zfs_txg_synctime_ms, "Target milliseconds between txg sync");
866
867 module_param(zfs_write_limit_min, ulong, 0444);
868 MODULE_PARM_DESC(zfs_write_limit_min, "Min txg write limit");
869
870 module_param(zfs_write_limit_max, ulong, 0444);
871 MODULE_PARM_DESC(zfs_write_limit_max, "Max txg write limit");
872
873 module_param(zfs_write_limit_inflated, ulong, 0444);
874 MODULE_PARM_DESC(zfs_write_limit_inflated, "Inflated txg write limit");
875
876 module_param(zfs_write_limit_override, ulong, 0444);
877 MODULE_PARM_DESC(zfs_write_limit_override, "Override txg write limit");
878 #endif