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