]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dsl_pool.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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  * Copyright (c) 2013 by Delphix. All rights reserved.
24  * Copyright (c) 2013 Steven Hartland. All rights reserved.
25  */
26
27 #include <sys/dsl_pool.h>
28 #include <sys/dsl_dataset.h>
29 #include <sys/dsl_prop.h>
30 #include <sys/dsl_dir.h>
31 #include <sys/dsl_synctask.h>
32 #include <sys/dsl_scan.h>
33 #include <sys/dnode.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/arc.h>
37 #include <sys/zap.h>
38 #include <sys/zio.h>
39 #include <sys/zfs_context.h>
40 #include <sys/fs/zfs.h>
41 #include <sys/zfs_znode.h>
42 #include <sys/spa_impl.h>
43 #include <sys/dsl_deadlist.h>
44 #include <sys/bptree.h>
45 #include <sys/zfeature.h>
46 #include <sys/zil_impl.h>
47 #include <sys/dsl_userhold.h>
48
49 int zfs_no_write_throttle = 0;
50 int zfs_write_limit_shift = 3;                  /* 1/8th of physical memory */
51 int zfs_txg_synctime_ms = 1000;         /* target millisecs to sync a txg */
52
53 uint64_t zfs_write_limit_min = 32 << 20;        /* min write limit is 32MB */
54 uint64_t zfs_write_limit_max = 0;               /* max data payload per txg */
55 uint64_t zfs_write_limit_inflated = 0;
56 uint64_t zfs_write_limit_override = 0;
57
58 kmutex_t zfs_write_limit_lock;
59
60 static pgcnt_t old_physmem = 0;
61
62 SYSCTL_DECL(_vfs_zfs);
63 TUNABLE_INT("vfs.zfs.no_write_throttle", &zfs_no_write_throttle);
64 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_write_throttle, CTLFLAG_RDTUN,
65     &zfs_no_write_throttle, 0, "");
66 TUNABLE_INT("vfs.zfs.write_limit_shift", &zfs_write_limit_shift);
67 SYSCTL_INT(_vfs_zfs, OID_AUTO, write_limit_shift, CTLFLAG_RDTUN,
68     &zfs_write_limit_shift, 0, "2^N of physical memory");
69 SYSCTL_DECL(_vfs_zfs_txg);
70 TUNABLE_INT("vfs.zfs.txg.synctime_ms", &zfs_txg_synctime_ms);
71 SYSCTL_INT(_vfs_zfs_txg, OID_AUTO, synctime_ms, CTLFLAG_RDTUN,
72     &zfs_txg_synctime_ms, 0, "Target milliseconds to sync a txg");
73
74 TUNABLE_QUAD("vfs.zfs.write_limit_min", &zfs_write_limit_min);
75 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_min, CTLFLAG_RDTUN,
76     &zfs_write_limit_min, 0, "Minimum write limit");
77 TUNABLE_QUAD("vfs.zfs.write_limit_max", &zfs_write_limit_max);
78 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_max, CTLFLAG_RDTUN,
79     &zfs_write_limit_max, 0, "Maximum data payload per txg");
80 TUNABLE_QUAD("vfs.zfs.write_limit_inflated", &zfs_write_limit_inflated);
81 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_inflated, CTLFLAG_RDTUN,
82     &zfs_write_limit_inflated, 0, "Maximum size of the dynamic write limit");
83 TUNABLE_QUAD("vfs.zfs.write_limit_override", &zfs_write_limit_override);
84 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_override, CTLFLAG_RDTUN,
85     &zfs_write_limit_override, 0,
86     "Force a txg if dirty buffers exceed this value (bytes)");
87
88 int
89 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
90 {
91         uint64_t obj;
92         int err;
93
94         err = zap_lookup(dp->dp_meta_objset,
95             dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
96             name, sizeof (obj), 1, &obj);
97         if (err)
98                 return (err);
99
100         return (dsl_dir_hold_obj(dp, obj, name, dp, ddp));
101 }
102
103 static dsl_pool_t *
104 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
105 {
106         dsl_pool_t *dp;
107         blkptr_t *bp = spa_get_rootblkptr(spa);
108
109         dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
110         dp->dp_spa = spa;
111         dp->dp_meta_rootbp = *bp;
112         rrw_init(&dp->dp_config_rwlock, B_TRUE);
113         dp->dp_write_limit = zfs_write_limit_min;
114         txg_init(dp, txg);
115
116         txg_list_create(&dp->dp_dirty_datasets,
117             offsetof(dsl_dataset_t, ds_dirty_link));
118         txg_list_create(&dp->dp_dirty_zilogs,
119             offsetof(zilog_t, zl_dirty_link));
120         txg_list_create(&dp->dp_dirty_dirs,
121             offsetof(dsl_dir_t, dd_dirty_link));
122         txg_list_create(&dp->dp_sync_tasks,
123             offsetof(dsl_sync_task_t, dst_node));
124
125         mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
126
127         dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
128             1, 4, 0);
129
130         return (dp);
131 }
132
133 int
134 dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
135 {
136         int err;
137         dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
138
139         err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
140             &dp->dp_meta_objset);
141         if (err != 0)
142                 dsl_pool_close(dp);
143         else
144                 *dpp = dp;
145
146         return (err);
147 }
148
149 int
150 dsl_pool_open(dsl_pool_t *dp)
151 {
152         int err;
153         dsl_dir_t *dd;
154         dsl_dataset_t *ds;
155         uint64_t obj;
156
157         rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
158         err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
159             DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
160             &dp->dp_root_dir_obj);
161         if (err)
162                 goto out;
163
164         err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
165             NULL, dp, &dp->dp_root_dir);
166         if (err)
167                 goto out;
168
169         err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
170         if (err)
171                 goto out;
172
173         if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
174                 err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
175                 if (err)
176                         goto out;
177                 err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
178                     FTAG, &ds);
179                 if (err == 0) {
180                         err = dsl_dataset_hold_obj(dp,
181                             ds->ds_phys->ds_prev_snap_obj, dp,
182                             &dp->dp_origin_snap);
183                         dsl_dataset_rele(ds, FTAG);
184                 }
185                 dsl_dir_rele(dd, dp);
186                 if (err)
187                         goto out;
188         }
189
190         if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
191                 err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
192                     &dp->dp_free_dir);
193                 if (err)
194                         goto out;
195
196                 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
197                     DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
198                 if (err)
199                         goto out;
200                 VERIFY0(bpobj_open(&dp->dp_free_bpobj,
201                     dp->dp_meta_objset, obj));
202         }
203
204         if (spa_feature_is_active(dp->dp_spa,
205             &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
206                 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
207                     DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
208                     &dp->dp_bptree_obj);
209                 if (err != 0)
210                         goto out;
211         }
212
213         if (spa_feature_is_active(dp->dp_spa,
214             &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ])) {
215                 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
216                     DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
217                     &dp->dp_empty_bpobj);
218                 if (err != 0)
219                         goto out;
220         }
221
222         err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
223             DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
224             &dp->dp_tmp_userrefs_obj);
225         if (err == ENOENT)
226                 err = 0;
227         if (err)
228                 goto out;
229
230         err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
231
232 out:
233         rrw_exit(&dp->dp_config_rwlock, FTAG);
234         return (err);
235 }
236
237 void
238 dsl_pool_close(dsl_pool_t *dp)
239 {
240         /* drop our references from dsl_pool_open() */
241
242         /*
243          * Since we held the origin_snap from "syncing" context (which
244          * includes pool-opening context), it actually only got a "ref"
245          * and not a hold, so just drop that here.
246          */
247         if (dp->dp_origin_snap)
248                 dsl_dataset_rele(dp->dp_origin_snap, dp);
249         if (dp->dp_mos_dir)
250                 dsl_dir_rele(dp->dp_mos_dir, dp);
251         if (dp->dp_free_dir)
252                 dsl_dir_rele(dp->dp_free_dir, dp);
253         if (dp->dp_root_dir)
254                 dsl_dir_rele(dp->dp_root_dir, dp);
255
256         bpobj_close(&dp->dp_free_bpobj);
257
258         /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
259         if (dp->dp_meta_objset)
260                 dmu_objset_evict(dp->dp_meta_objset);
261
262         txg_list_destroy(&dp->dp_dirty_datasets);
263         txg_list_destroy(&dp->dp_dirty_zilogs);
264         txg_list_destroy(&dp->dp_sync_tasks);
265         txg_list_destroy(&dp->dp_dirty_dirs);
266
267         arc_flush(dp->dp_spa);
268         txg_fini(dp);
269         dsl_scan_fini(dp);
270         rrw_destroy(&dp->dp_config_rwlock);
271         mutex_destroy(&dp->dp_lock);
272         taskq_destroy(dp->dp_vnrele_taskq);
273         if (dp->dp_blkstats)
274                 kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
275         kmem_free(dp, sizeof (dsl_pool_t));
276 }
277
278 dsl_pool_t *
279 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
280 {
281         int err;
282         dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
283         dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
284         objset_t *os;
285         dsl_dataset_t *ds;
286         uint64_t obj;
287
288         rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
289
290         /* create and open the MOS (meta-objset) */
291         dp->dp_meta_objset = dmu_objset_create_impl(spa,
292             NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
293
294         /* create the pool directory */
295         err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
296             DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
297         ASSERT0(err);
298
299         /* Initialize scan structures */
300         VERIFY0(dsl_scan_init(dp, txg));
301
302         /* create and open the root dir */
303         dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
304         VERIFY0(dsl_dir_hold_obj(dp, dp->dp_root_dir_obj,
305             NULL, dp, &dp->dp_root_dir));
306
307         /* create and open the meta-objset dir */
308         (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
309         VERIFY0(dsl_pool_open_special_dir(dp,
310             MOS_DIR_NAME, &dp->dp_mos_dir));
311
312         if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
313                 /* create and open the free dir */
314                 (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
315                     FREE_DIR_NAME, tx);
316                 VERIFY0(dsl_pool_open_special_dir(dp,
317                     FREE_DIR_NAME, &dp->dp_free_dir));
318
319                 /* create and open the free_bplist */
320                 obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
321                 VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
322                     DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
323                 VERIFY0(bpobj_open(&dp->dp_free_bpobj,
324                     dp->dp_meta_objset, obj));
325         }
326
327         if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
328                 dsl_pool_create_origin(dp, tx);
329
330         /* create the root dataset */
331         obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
332
333         /* create the root objset */
334         VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
335         os = dmu_objset_create_impl(dp->dp_spa, ds,
336             dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
337 #ifdef _KERNEL
338         zfs_create_fs(os, kcred, zplprops, tx);
339 #endif
340         dsl_dataset_rele(ds, FTAG);
341
342         dmu_tx_commit(tx);
343
344         rrw_exit(&dp->dp_config_rwlock, FTAG);
345
346         return (dp);
347 }
348
349 /*
350  * Account for the meta-objset space in its placeholder dsl_dir.
351  */
352 void
353 dsl_pool_mos_diduse_space(dsl_pool_t *dp,
354     int64_t used, int64_t comp, int64_t uncomp)
355 {
356         ASSERT3U(comp, ==, uncomp); /* it's all metadata */
357         mutex_enter(&dp->dp_lock);
358         dp->dp_mos_used_delta += used;
359         dp->dp_mos_compressed_delta += comp;
360         dp->dp_mos_uncompressed_delta += uncomp;
361         mutex_exit(&dp->dp_lock);
362 }
363
364 static int
365 deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
366 {
367         dsl_deadlist_t *dl = arg;
368         dsl_deadlist_insert(dl, bp, tx);
369         return (0);
370 }
371
372 void
373 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
374 {
375         zio_t *zio;
376         dmu_tx_t *tx;
377         dsl_dir_t *dd;
378         dsl_dataset_t *ds;
379         objset_t *mos = dp->dp_meta_objset;
380         hrtime_t start, write_time;
381         uint64_t data_written;
382         int err;
383         list_t synced_datasets;
384
385         list_create(&synced_datasets, sizeof (dsl_dataset_t),
386             offsetof(dsl_dataset_t, ds_synced_link));
387
388         /*
389          * We need to copy dp_space_towrite() before doing
390          * dsl_sync_task_sync(), because
391          * dsl_dataset_snapshot_reserve_space() will increase
392          * dp_space_towrite but not actually write anything.
393          */
394         data_written = dp->dp_space_towrite[txg & TXG_MASK];
395
396         tx = dmu_tx_create_assigned(dp, txg);
397
398         dp->dp_read_overhead = 0;
399         start = gethrtime();
400
401         zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
402         while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
403                 /*
404                  * We must not sync any non-MOS datasets twice, because
405                  * we may have taken a snapshot of them.  However, we
406                  * may sync newly-created datasets on pass 2.
407                  */
408                 ASSERT(!list_link_active(&ds->ds_synced_link));
409                 list_insert_tail(&synced_datasets, ds);
410                 dsl_dataset_sync(ds, zio, tx);
411         }
412         DTRACE_PROBE(pool_sync__1setup);
413         err = zio_wait(zio);
414
415         write_time = gethrtime() - start;
416         ASSERT(err == 0);
417         DTRACE_PROBE(pool_sync__2rootzio);
418
419         /*
420          * After the data blocks have been written (ensured by the zio_wait()
421          * above), update the user/group space accounting.
422          */
423         for (ds = list_head(&synced_datasets); ds;
424             ds = list_next(&synced_datasets, ds))
425                 dmu_objset_do_userquota_updates(ds->ds_objset, tx);
426
427         /*
428          * Sync the datasets again to push out the changes due to
429          * userspace updates.  This must be done before we process the
430          * sync tasks, so that any snapshots will have the correct
431          * user accounting information (and we won't get confused
432          * about which blocks are part of the snapshot).
433          */
434         zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
435         while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
436                 ASSERT(list_link_active(&ds->ds_synced_link));
437                 dmu_buf_rele(ds->ds_dbuf, ds);
438                 dsl_dataset_sync(ds, zio, tx);
439         }
440         err = zio_wait(zio);
441
442         /*
443          * Now that the datasets have been completely synced, we can
444          * clean up our in-memory structures accumulated while syncing:
445          *
446          *  - move dead blocks from the pending deadlist to the on-disk deadlist
447          *  - release hold from dsl_dataset_dirty()
448          */
449         while (ds = list_remove_head(&synced_datasets)) {
450                 objset_t *os = ds->ds_objset;
451                 bplist_iterate(&ds->ds_pending_deadlist,
452                     deadlist_enqueue_cb, &ds->ds_deadlist, tx);
453                 ASSERT(!dmu_objset_is_dirty(os, txg));
454                 dmu_buf_rele(ds->ds_dbuf, ds);
455         }
456
457         start = gethrtime();
458         while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
459                 dsl_dir_sync(dd, tx);
460         write_time += gethrtime() - start;
461
462         /*
463          * The MOS's space is accounted for in the pool/$MOS
464          * (dp_mos_dir).  We can't modify the mos while we're syncing
465          * it, so we remember the deltas and apply them here.
466          */
467         if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
468             dp->dp_mos_uncompressed_delta != 0) {
469                 dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
470                     dp->dp_mos_used_delta,
471                     dp->dp_mos_compressed_delta,
472                     dp->dp_mos_uncompressed_delta, tx);
473                 dp->dp_mos_used_delta = 0;
474                 dp->dp_mos_compressed_delta = 0;
475                 dp->dp_mos_uncompressed_delta = 0;
476         }
477
478         start = gethrtime();
479         if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
480             list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
481                 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
482                 dmu_objset_sync(mos, zio, tx);
483                 err = zio_wait(zio);
484                 ASSERT(err == 0);
485                 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
486                 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
487         }
488         write_time += gethrtime() - start;
489         DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
490             hrtime_t, dp->dp_read_overhead);
491         write_time -= dp->dp_read_overhead;
492
493         /*
494          * If we modify a dataset in the same txg that we want to destroy it,
495          * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
496          * dsl_dir_destroy_check() will fail if there are unexpected holds.
497          * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
498          * and clearing the hold on it) before we process the sync_tasks.
499          * The MOS data dirtied by the sync_tasks will be synced on the next
500          * pass.
501          */
502         DTRACE_PROBE(pool_sync__3task);
503         if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
504                 dsl_sync_task_t *dst;
505                 /*
506                  * No more sync tasks should have been added while we
507                  * were syncing.
508                  */
509                 ASSERT(spa_sync_pass(dp->dp_spa) == 1);
510                 while (dst = txg_list_remove(&dp->dp_sync_tasks, txg))
511                         dsl_sync_task_sync(dst, tx);
512         }
513
514         dmu_tx_commit(tx);
515
516         dp->dp_space_towrite[txg & TXG_MASK] = 0;
517         ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
518
519         /*
520          * If the write limit max has not been explicitly set, set it
521          * to a fraction of available physical memory (default 1/8th).
522          * Note that we must inflate the limit because the spa
523          * inflates write sizes to account for data replication.
524          * Check this each sync phase to catch changing memory size.
525          */
526         if (physmem != old_physmem && zfs_write_limit_shift) {
527                 mutex_enter(&zfs_write_limit_lock);
528                 old_physmem = physmem;
529                 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
530                 zfs_write_limit_inflated = MAX(zfs_write_limit_min,
531                     spa_get_asize(dp->dp_spa, zfs_write_limit_max));
532                 mutex_exit(&zfs_write_limit_lock);
533         }
534
535         /*
536          * Attempt to keep the sync time consistent by adjusting the
537          * amount of write traffic allowed into each transaction group.
538          * Weight the throughput calculation towards the current value:
539          *      thru = 3/4 old_thru + 1/4 new_thru
540          *
541          * Note: write_time is in nanosecs, so write_time/MICROSEC
542          * yields millisecs
543          */
544         ASSERT(zfs_write_limit_min > 0);
545         if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
546                 uint64_t throughput = data_written / (write_time / MICROSEC);
547
548                 if (dp->dp_throughput)
549                         dp->dp_throughput = throughput / 4 +
550                             3 * dp->dp_throughput / 4;
551                 else
552                         dp->dp_throughput = throughput;
553                 dp->dp_write_limit = MIN(zfs_write_limit_inflated,
554                     MAX(zfs_write_limit_min,
555                     dp->dp_throughput * zfs_txg_synctime_ms));
556         }
557 }
558
559 void
560 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
561 {
562         zilog_t *zilog;
563         dsl_dataset_t *ds;
564
565         while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) {
566                 ds = dmu_objset_ds(zilog->zl_os);
567                 zil_clean(zilog, txg);
568                 ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
569                 dmu_buf_rele(ds->ds_dbuf, zilog);
570         }
571         ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
572 }
573
574 /*
575  * TRUE if the current thread is the tx_sync_thread or if we
576  * are being called from SPA context during pool initialization.
577  */
578 int
579 dsl_pool_sync_context(dsl_pool_t *dp)
580 {
581         return (curthread == dp->dp_tx.tx_sync_thread ||
582             spa_is_initializing(dp->dp_spa));
583 }
584
585 uint64_t
586 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
587 {
588         uint64_t space, resv;
589
590         /*
591          * Reserve about 1.6% (1/64), or at least 32MB, for allocation
592          * efficiency.
593          * XXX The intent log is not accounted for, so it must fit
594          * within this slop.
595          *
596          * If we're trying to assess whether it's OK to do a free,
597          * cut the reservation in half to allow forward progress
598          * (e.g. make it possible to rm(1) files from a full pool).
599          */
600         space = spa_get_dspace(dp->dp_spa);
601         resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
602         if (netfree)
603                 resv >>= 1;
604
605         return (space - resv);
606 }
607
608 int
609 dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
610 {
611         uint64_t reserved = 0;
612         uint64_t write_limit = (zfs_write_limit_override ?
613             zfs_write_limit_override : dp->dp_write_limit);
614
615         if (zfs_no_write_throttle) {
616                 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
617                     space);
618                 return (0);
619         }
620
621         /*
622          * Check to see if we have exceeded the maximum allowed IO for
623          * this transaction group.  We can do this without locks since
624          * a little slop here is ok.  Note that we do the reserved check
625          * with only half the requested reserve: this is because the
626          * reserve requests are worst-case, and we really don't want to
627          * throttle based off of worst-case estimates.
628          */
629         if (write_limit > 0) {
630                 reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
631                     + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
632
633                 if (reserved && reserved > write_limit)
634                         return (SET_ERROR(ERESTART));
635         }
636
637         atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
638
639         /*
640          * If this transaction group is over 7/8ths capacity, delay
641          * the caller 1 clock tick.  This will slow down the "fill"
642          * rate until the sync process can catch up with us.
643          */
644         if (reserved && reserved > (write_limit - (write_limit >> 3)))
645                 txg_delay(dp, tx->tx_txg, 1);
646
647         return (0);
648 }
649
650 void
651 dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
652 {
653         ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
654         atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
655 }
656
657 void
658 dsl_pool_memory_pressure(dsl_pool_t *dp)
659 {
660         uint64_t space_inuse = 0;
661         int i;
662
663         if (dp->dp_write_limit == zfs_write_limit_min)
664                 return;
665
666         for (i = 0; i < TXG_SIZE; i++) {
667                 space_inuse += dp->dp_space_towrite[i];
668                 space_inuse += dp->dp_tempreserved[i];
669         }
670         dp->dp_write_limit = MAX(zfs_write_limit_min,
671             MIN(dp->dp_write_limit, space_inuse / 4));
672 }
673
674 void
675 dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
676 {
677         if (space > 0) {
678                 mutex_enter(&dp->dp_lock);
679                 dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
680                 mutex_exit(&dp->dp_lock);
681         }
682 }
683
684 /* ARGSUSED */
685 static int
686 upgrade_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
687 {
688         dmu_tx_t *tx = arg;
689         dsl_dataset_t *ds, *prev = NULL;
690         int err;
691
692         err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
693         if (err)
694                 return (err);
695
696         while (ds->ds_phys->ds_prev_snap_obj != 0) {
697                 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
698                     FTAG, &prev);
699                 if (err) {
700                         dsl_dataset_rele(ds, FTAG);
701                         return (err);
702                 }
703
704                 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
705                         break;
706                 dsl_dataset_rele(ds, FTAG);
707                 ds = prev;
708                 prev = NULL;
709         }
710
711         if (prev == NULL) {
712                 prev = dp->dp_origin_snap;
713
714                 /*
715                  * The $ORIGIN can't have any data, or the accounting
716                  * will be wrong.
717                  */
718                 ASSERT0(prev->ds_phys->ds_bp.blk_birth);
719
720                 /* The origin doesn't get attached to itself */
721                 if (ds->ds_object == prev->ds_object) {
722                         dsl_dataset_rele(ds, FTAG);
723                         return (0);
724                 }
725
726                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
727                 ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
728                 ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
729
730                 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
731                 ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
732
733                 dmu_buf_will_dirty(prev->ds_dbuf, tx);
734                 prev->ds_phys->ds_num_children++;
735
736                 if (ds->ds_phys->ds_next_snap_obj == 0) {
737                         ASSERT(ds->ds_prev == NULL);
738                         VERIFY0(dsl_dataset_hold_obj(dp,
739                             ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
740                 }
741         }
742
743         ASSERT3U(ds->ds_dir->dd_phys->dd_origin_obj, ==, prev->ds_object);
744         ASSERT3U(ds->ds_phys->ds_prev_snap_obj, ==, prev->ds_object);
745
746         if (prev->ds_phys->ds_next_clones_obj == 0) {
747                 dmu_buf_will_dirty(prev->ds_dbuf, tx);
748                 prev->ds_phys->ds_next_clones_obj =
749                     zap_create(dp->dp_meta_objset,
750                     DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
751         }
752         VERIFY0(zap_add_int(dp->dp_meta_objset,
753             prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
754
755         dsl_dataset_rele(ds, FTAG);
756         if (prev != dp->dp_origin_snap)
757                 dsl_dataset_rele(prev, FTAG);
758         return (0);
759 }
760
761 void
762 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
763 {
764         ASSERT(dmu_tx_is_syncing(tx));
765         ASSERT(dp->dp_origin_snap != NULL);
766
767         VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj, upgrade_clones_cb,
768             tx, DS_FIND_CHILDREN));
769 }
770
771 /* ARGSUSED */
772 static int
773 upgrade_dir_clones_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
774 {
775         dmu_tx_t *tx = arg;
776         objset_t *mos = dp->dp_meta_objset;
777
778         if (ds->ds_dir->dd_phys->dd_origin_obj != 0) {
779                 dsl_dataset_t *origin;
780
781                 VERIFY0(dsl_dataset_hold_obj(dp,
782                     ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
783
784                 if (origin->ds_dir->dd_phys->dd_clones == 0) {
785                         dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
786                         origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
787                             DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
788                 }
789
790                 VERIFY0(zap_add_int(dp->dp_meta_objset,
791                     origin->ds_dir->dd_phys->dd_clones, ds->ds_object, tx));
792
793                 dsl_dataset_rele(origin, FTAG);
794         }
795         return (0);
796 }
797
798 void
799 dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
800 {
801         ASSERT(dmu_tx_is_syncing(tx));
802         uint64_t obj;
803
804         (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
805         VERIFY0(dsl_pool_open_special_dir(dp,
806             FREE_DIR_NAME, &dp->dp_free_dir));
807
808         /*
809          * We can't use bpobj_alloc(), because spa_version() still
810          * returns the old version, and we need a new-version bpobj with
811          * subobj support.  So call dmu_object_alloc() directly.
812          */
813         obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
814             SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
815         VERIFY0(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
816             DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
817         VERIFY0(bpobj_open(&dp->dp_free_bpobj, dp->dp_meta_objset, obj));
818
819         VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
820             upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
821 }
822
823 void
824 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
825 {
826         uint64_t dsobj;
827         dsl_dataset_t *ds;
828
829         ASSERT(dmu_tx_is_syncing(tx));
830         ASSERT(dp->dp_origin_snap == NULL);
831         ASSERT(rrw_held(&dp->dp_config_rwlock, RW_WRITER));
832
833         /* create the origin dir, ds, & snap-ds */
834         dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
835             NULL, 0, kcred, tx);
836         VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
837         dsl_dataset_snapshot_sync_impl(ds, ORIGIN_DIR_NAME, tx);
838         VERIFY0(dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
839             dp, &dp->dp_origin_snap));
840         dsl_dataset_rele(ds, FTAG);
841 }
842
843 taskq_t *
844 dsl_pool_vnrele_taskq(dsl_pool_t *dp)
845 {
846         return (dp->dp_vnrele_taskq);
847 }
848
849 /*
850  * Walk through the pool-wide zap object of temporary snapshot user holds
851  * and release them.
852  */
853 void
854 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
855 {
856         zap_attribute_t za;
857         zap_cursor_t zc;
858         objset_t *mos = dp->dp_meta_objset;
859         uint64_t zapobj = dp->dp_tmp_userrefs_obj;
860         nvlist_t *holds;
861
862         if (zapobj == 0)
863                 return;
864         ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
865
866         holds = fnvlist_alloc();
867
868         for (zap_cursor_init(&zc, mos, zapobj);
869             zap_cursor_retrieve(&zc, &za) == 0;
870             zap_cursor_advance(&zc)) {
871                 char *htag;
872                 nvlist_t *tags;
873
874                 htag = strchr(za.za_name, '-');
875                 *htag = '\0';
876                 ++htag;
877                 if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
878                         tags = fnvlist_alloc();
879                         fnvlist_add_boolean(tags, htag);
880                         fnvlist_add_nvlist(holds, za.za_name, tags);
881                         fnvlist_free(tags);
882                 } else {
883                         fnvlist_add_boolean(tags, htag);
884                 }
885         }
886         dsl_dataset_user_release_tmp(dp, holds);
887         fnvlist_free(holds);
888         zap_cursor_fini(&zc);
889 }
890
891 /*
892  * Create the pool-wide zap object for storing temporary snapshot holds.
893  */
894 void
895 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
896 {
897         objset_t *mos = dp->dp_meta_objset;
898
899         ASSERT(dp->dp_tmp_userrefs_obj == 0);
900         ASSERT(dmu_tx_is_syncing(tx));
901
902         dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
903             DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
904 }
905
906 static int
907 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
908     const char *tag, uint64_t now, dmu_tx_t *tx, boolean_t holding)
909 {
910         objset_t *mos = dp->dp_meta_objset;
911         uint64_t zapobj = dp->dp_tmp_userrefs_obj;
912         char *name;
913         int error;
914
915         ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
916         ASSERT(dmu_tx_is_syncing(tx));
917
918         /*
919          * If the pool was created prior to SPA_VERSION_USERREFS, the
920          * zap object for temporary holds might not exist yet.
921          */
922         if (zapobj == 0) {
923                 if (holding) {
924                         dsl_pool_user_hold_create_obj(dp, tx);
925                         zapobj = dp->dp_tmp_userrefs_obj;
926                 } else {
927                         return (SET_ERROR(ENOENT));
928                 }
929         }
930
931         name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
932         if (holding)
933                 error = zap_add(mos, zapobj, name, 8, 1, &now, tx);
934         else
935                 error = zap_remove(mos, zapobj, name, tx);
936         strfree(name);
937
938         return (error);
939 }
940
941 /*
942  * Add a temporary hold for the given dataset object and tag.
943  */
944 int
945 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
946     uint64_t now, dmu_tx_t *tx)
947 {
948         return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
949 }
950
951 /*
952  * Release a temporary hold for the given dataset object and tag.
953  */
954 int
955 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
956     dmu_tx_t *tx)
957 {
958         return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, 0,
959             tx, B_FALSE));
960 }
961
962 /*
963  * DSL Pool Configuration Lock
964  *
965  * The dp_config_rwlock protects against changes to DSL state (e.g. dataset
966  * creation / destruction / rename / property setting).  It must be held for
967  * read to hold a dataset or dsl_dir.  I.e. you must call
968  * dsl_pool_config_enter() or dsl_pool_hold() before calling
969  * dsl_{dataset,dir}_hold{_obj}.  In most circumstances, the dp_config_rwlock
970  * must be held continuously until all datasets and dsl_dirs are released.
971  *
972  * The only exception to this rule is that if a "long hold" is placed on
973  * a dataset, then the dp_config_rwlock may be dropped while the dataset
974  * is still held.  The long hold will prevent the dataset from being
975  * destroyed -- the destroy will fail with EBUSY.  A long hold can be
976  * obtained by calling dsl_dataset_long_hold(), or by "owning" a dataset
977  * (by calling dsl_{dataset,objset}_{try}own{_obj}).
978  *
979  * Legitimate long-holders (including owners) should be long-running, cancelable
980  * tasks that should cause "zfs destroy" to fail.  This includes DMU
981  * consumers (i.e. a ZPL filesystem being mounted or ZVOL being open),
982  * "zfs send", and "zfs diff".  There are several other long-holders whose
983  * uses are suboptimal (e.g. "zfs promote", and zil_suspend()).
984  *
985  * The usual formula for long-holding would be:
986  * dsl_pool_hold()
987  * dsl_dataset_hold()
988  * ... perform checks ...
989  * dsl_dataset_long_hold()
990  * dsl_pool_rele()
991  * ... perform long-running task ...
992  * dsl_dataset_long_rele()
993  * dsl_dataset_rele()
994  *
995  * Note that when the long hold is released, the dataset is still held but
996  * the pool is not held.  The dataset may change arbitrarily during this time
997  * (e.g. it could be destroyed).  Therefore you shouldn't do anything to the
998  * dataset except release it.
999  *
1000  * User-initiated operations (e.g. ioctls, zfs_ioc_*()) are either read-only
1001  * or modifying operations.
1002  *
1003  * Modifying operations should generally use dsl_sync_task().  The synctask
1004  * infrastructure enforces proper locking strategy with respect to the
1005  * dp_config_rwlock.  See the comment above dsl_sync_task() for details.
1006  *
1007  * Read-only operations will manually hold the pool, then the dataset, obtain
1008  * information from the dataset, then release the pool and dataset.
1009  * dmu_objset_{hold,rele}() are convenience routines that also do the pool
1010  * hold/rele.
1011  */
1012
1013 int
1014 dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp)
1015 {
1016         spa_t *spa;
1017         int error;
1018
1019         error = spa_open(name, &spa, tag);
1020         if (error == 0) {
1021                 *dp = spa_get_dsl(spa);
1022                 dsl_pool_config_enter(*dp, tag);
1023         }
1024         return (error);
1025 }
1026
1027 void
1028 dsl_pool_rele(dsl_pool_t *dp, void *tag)
1029 {
1030         dsl_pool_config_exit(dp, tag);
1031         spa_close(dp->dp_spa, tag);
1032 }
1033
1034 void
1035 dsl_pool_config_enter(dsl_pool_t *dp, void *tag)
1036 {
1037         /*
1038          * We use a "reentrant" reader-writer lock, but not reentrantly.
1039          *
1040          * The rrwlock can (with the track_all flag) track all reading threads,
1041          * which is very useful for debugging which code path failed to release
1042          * the lock, and for verifying that the *current* thread does hold
1043          * the lock.
1044          *
1045          * (Unlike a rwlock, which knows that N threads hold it for
1046          * read, but not *which* threads, so rw_held(RW_READER) returns TRUE
1047          * if any thread holds it for read, even if this thread doesn't).
1048          */
1049         ASSERT(!rrw_held(&dp->dp_config_rwlock, RW_READER));
1050         rrw_enter(&dp->dp_config_rwlock, RW_READER, tag);
1051 }
1052
1053 void
1054 dsl_pool_config_exit(dsl_pool_t *dp, void *tag)
1055 {
1056         rrw_exit(&dp->dp_config_rwlock, tag);
1057 }
1058
1059 boolean_t
1060 dsl_pool_config_held(dsl_pool_t *dp)
1061 {
1062         return (RRW_LOCK_HELD(&dp->dp_config_rwlock));
1063 }