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, 2018 by Delphix. All rights reserved.
24  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
25  */
26 
27 #ifndef	_SYS_DSL_POOL_H
28 #define	_SYS_DSL_POOL_H
29 
30 #include <sys/spa.h>
31 #include <sys/txg.h>
32 #include <sys/txg_impl.h>
33 #include <sys/zfs_context.h>
34 #include <sys/zio.h>
35 #include <sys/dnode.h>
36 #include <sys/ddt.h>
37 #include <sys/arc.h>
38 #include <sys/bpobj.h>
39 #include <sys/bptree.h>
40 #include <sys/rrwlock.h>
41 #include <sys/dsl_synctask.h>
42 #include <sys/mmp.h>
43 #include <sys/aggsum.h>
44 
45 #ifdef	__cplusplus
46 extern "C" {
47 #endif
48 
49 extern int zfs_txg_synctime_ms;
50 
51 struct objset;
52 struct dsl_dir;
53 struct dsl_dataset;
54 struct dsl_pool;
55 struct dmu_tx;
56 struct dsl_scan;
57 struct dsl_crypto_params;
58 struct dsl_deadlist;
59 
60 extern unsigned long zfs_dirty_data_max;
61 extern unsigned long zfs_dirty_data_max_max;
62 extern unsigned long zfs_wrlog_data_max;
63 extern int zfs_dirty_data_max_percent;
64 extern int zfs_dirty_data_max_max_percent;
65 extern int zfs_delay_min_dirty_percent;
66 extern unsigned long zfs_delay_scale;
67 
68 /* These macros are for indexing into the zfs_all_blkstats_t. */
69 #define	DMU_OT_DEFERRED	DMU_OT_NONE
70 #define	DMU_OT_OTHER	DMU_OT_NUMTYPES /* place holder for DMU_OT() types */
71 #define	DMU_OT_TOTAL	(DMU_OT_NUMTYPES + 1)
72 
73 typedef struct zfs_blkstat {
74 	uint64_t	zb_count;
75 	uint64_t	zb_asize;
76 	uint64_t	zb_lsize;
77 	uint64_t	zb_psize;
78 	uint64_t	zb_gangs;
79 	uint64_t	zb_ditto_2_of_2_samevdev;
80 	uint64_t	zb_ditto_2_of_3_samevdev;
81 	uint64_t	zb_ditto_3_of_3_samevdev;
82 } zfs_blkstat_t;
83 
84 typedef struct zfs_all_blkstats {
85 	zfs_blkstat_t	zab_type[DN_MAX_LEVELS + 1][DMU_OT_TOTAL + 1];
86 	kmutex_t	zab_lock;
87 } zfs_all_blkstats_t;
88 
89 
90 typedef struct dsl_pool {
91 	/* Immutable */
92 	spa_t *dp_spa;
93 	struct objset *dp_meta_objset;
94 	struct dsl_dir *dp_root_dir;
95 	struct dsl_dir *dp_mos_dir;
96 	struct dsl_dir *dp_free_dir;
97 	struct dsl_dir *dp_leak_dir;
98 	struct dsl_dataset *dp_origin_snap;
99 	uint64_t dp_root_dir_obj;
100 	struct taskq *dp_zrele_taskq;
101 	struct taskq *dp_unlinked_drain_taskq;
102 
103 	/* No lock needed - sync context only */
104 	blkptr_t dp_meta_rootbp;
105 	uint64_t dp_tmp_userrefs_obj;
106 	bpobj_t dp_free_bpobj;
107 	uint64_t dp_bptree_obj;
108 	uint64_t dp_empty_bpobj;
109 	bpobj_t dp_obsolete_bpobj;
110 
111 	struct dsl_scan *dp_scan;
112 
113 	/* Uses dp_lock */
114 	kmutex_t dp_lock;
115 	kcondvar_t dp_spaceavail_cv;
116 	uint64_t dp_dirty_pertxg[TXG_SIZE];
117 	uint64_t dp_dirty_total;
118 	uint64_t dp_long_free_dirty_pertxg[TXG_SIZE];
119 	uint64_t dp_mos_used_delta;
120 	uint64_t dp_mos_compressed_delta;
121 	uint64_t dp_mos_uncompressed_delta;
122 
123 	aggsum_t dp_wrlog_pertxg[TXG_SIZE];
124 	aggsum_t dp_wrlog_total;
125 
126 	/*
127 	 * Time of most recently scheduled (furthest in the future)
128 	 * wakeup for delayed transactions.
129 	 */
130 	hrtime_t dp_last_wakeup;
131 
132 	/* Has its own locking */
133 	tx_state_t dp_tx;
134 	txg_list_t dp_dirty_datasets;
135 	txg_list_t dp_dirty_zilogs;
136 	txg_list_t dp_dirty_dirs;
137 	txg_list_t dp_sync_tasks;
138 	txg_list_t dp_early_sync_tasks;
139 	taskq_t *dp_sync_taskq;
140 	taskq_t *dp_zil_clean_taskq;
141 
142 	/*
143 	 * Protects administrative changes (properties, namespace)
144 	 *
145 	 * It is only held for write in syncing context.  Therefore
146 	 * syncing context does not need to ever have it for read, since
147 	 * nobody else could possibly have it for write.
148 	 */
149 	rrwlock_t dp_config_rwlock;
150 
151 	zfs_all_blkstats_t *dp_blkstats;
152 } dsl_pool_t;
153 
154 int dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp);
155 int dsl_pool_open(dsl_pool_t *dp);
156 void dsl_pool_close(dsl_pool_t *dp);
157 dsl_pool_t *dsl_pool_create(spa_t *spa, nvlist_t *zplprops,
158     struct dsl_crypto_params *dcp, uint64_t txg);
159 void dsl_pool_sync(dsl_pool_t *dp, uint64_t txg);
160 void dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg);
161 int dsl_pool_sync_context(dsl_pool_t *dp);
162 uint64_t dsl_pool_adjustedsize(dsl_pool_t *dp, zfs_space_check_t slop_policy);
163 uint64_t dsl_pool_unreserved_space(dsl_pool_t *dp,
164     zfs_space_check_t slop_policy);
165 uint64_t dsl_pool_deferred_space(dsl_pool_t *dp);
166 void dsl_pool_wrlog_count(dsl_pool_t *dp, int64_t size, uint64_t txg);
167 boolean_t dsl_pool_need_wrlog_delay(dsl_pool_t *dp);
168 void dsl_pool_dirty_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx);
169 void dsl_pool_undirty_space(dsl_pool_t *dp, int64_t space, uint64_t txg);
170 void dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp);
171 void dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg,
172     const blkptr_t *bpp);
173 void dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx);
174 void dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx);
175 void dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx);
176 void dsl_pool_mos_diduse_space(dsl_pool_t *dp,
177     int64_t used, int64_t comp, int64_t uncomp);
178 void dsl_pool_ckpoint_diduse_space(dsl_pool_t *dp,
179     int64_t used, int64_t comp, int64_t uncomp);
180 boolean_t dsl_pool_need_dirty_delay(dsl_pool_t *dp);
181 void dsl_pool_config_enter(dsl_pool_t *dp, void *tag);
182 void dsl_pool_config_enter_prio(dsl_pool_t *dp, void *tag);
183 void dsl_pool_config_exit(dsl_pool_t *dp, void *tag);
184 boolean_t dsl_pool_config_held(dsl_pool_t *dp);
185 boolean_t dsl_pool_config_held_writer(dsl_pool_t *dp);
186 
187 taskq_t *dsl_pool_zrele_taskq(dsl_pool_t *dp);
188 taskq_t *dsl_pool_unlinked_drain_taskq(dsl_pool_t *dp);
189 
190 int dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj,
191     const char *tag, uint64_t now, dmu_tx_t *tx);
192 int dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj,
193     const char *tag, dmu_tx_t *tx);
194 void dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp);
195 int dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **);
196 int dsl_pool_hold(const char *name, void *tag, dsl_pool_t **dp);
197 void dsl_pool_rele(dsl_pool_t *dp, void *tag);
198 
199 void dsl_pool_create_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx);
200 void dsl_pool_destroy_obsolete_bpobj(dsl_pool_t *dp, dmu_tx_t *tx);
201 
202 #ifdef	__cplusplus
203 }
204 #endif
205 
206 #endif /* _SYS_DSL_POOL_H */
207