1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23284304Savg * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
24228103Smm * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
25247265Smm * Copyright 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
26286575Smav * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27289422Smav * Copyright 2013 Saso Kiselkov. All rights reserved.
28168404Spjd */
29168404Spjd
30168404Spjd#ifndef _SYS_SPA_IMPL_H
31168404Spjd#define	_SYS_SPA_IMPL_H
32168404Spjd
33168404Spjd#include <sys/spa.h>
34168404Spjd#include <sys/vdev.h>
35168404Spjd#include <sys/metaslab.h>
36168404Spjd#include <sys/dmu.h>
37168404Spjd#include <sys/dsl_pool.h>
38168404Spjd#include <sys/uberblock_impl.h>
39168404Spjd#include <sys/zfs_context.h>
40168404Spjd#include <sys/avl.h>
41168404Spjd#include <sys/refcount.h>
42168404Spjd#include <sys/bplist.h>
43219089Spjd#include <sys/bpobj.h>
44268075Sdelphij#include <sys/zfeature.h>
45260150Sdelphij#include <zfeature_common.h>
46168404Spjd
47168404Spjd#ifdef	__cplusplus
48168404Spjdextern "C" {
49168404Spjd#endif
50168404Spjd
51168404Spjdtypedef struct spa_error_entry {
52268123Sdelphij	zbookmark_phys_t	se_bookmark;
53268123Sdelphij	char			*se_name;
54268123Sdelphij	avl_node_t		se_avl;
55168404Spjd} spa_error_entry_t;
56168404Spjd
57168404Spjdtypedef struct spa_history_phys {
58168404Spjd	uint64_t sh_pool_create_len;	/* ending offset of zpool create */
59168404Spjd	uint64_t sh_phys_max_off;	/* physical EOF */
60168404Spjd	uint64_t sh_bof;		/* logical BOF */
61168404Spjd	uint64_t sh_eof;		/* logical EOF */
62168404Spjd	uint64_t sh_records_lost;	/* num of records overwritten */
63168404Spjd} spa_history_phys_t;
64168404Spjd
65185029Spjdstruct spa_aux_vdev {
66185029Spjd	uint64_t	sav_object;		/* MOS object for device list */
67185029Spjd	nvlist_t	*sav_config;		/* cached device config */
68185029Spjd	vdev_t		**sav_vdevs;		/* devices */
69185029Spjd	int		sav_count;		/* number devices */
70185029Spjd	boolean_t	sav_sync;		/* sync the device list */
71185029Spjd	nvlist_t	**sav_pending;		/* pending device additions */
72185029Spjd	uint_t		sav_npending;		/* # pending devices */
73185029Spjd};
74168404Spjd
75185029Spjdtypedef struct spa_config_lock {
76185029Spjd	kmutex_t	scl_lock;
77185029Spjd	kthread_t	*scl_writer;
78185029Spjd	int		scl_write_wanted;
79185029Spjd	kcondvar_t	scl_cv;
80185029Spjd	refcount_t	scl_count;
81185029Spjd} spa_config_lock_t;
82185029Spjd
83185029Spjdtypedef struct spa_config_dirent {
84185029Spjd	list_node_t	scd_link;
85185029Spjd	char		*scd_path;
86185029Spjd} spa_config_dirent_t;
87185029Spjd
88258631Savgtypedef enum zio_taskq_type {
89185029Spjd	ZIO_TASKQ_ISSUE = 0,
90211931Smm	ZIO_TASKQ_ISSUE_HIGH,
91185029Spjd	ZIO_TASKQ_INTERRUPT,
92211931Smm	ZIO_TASKQ_INTERRUPT_HIGH,
93185029Spjd	ZIO_TASKQ_TYPES
94258631Savg} zio_taskq_type_t;
95185029Spjd
96219089Spjd/*
97258631Savg * State machine for the zpool-poolname process.  The states transitions
98219089Spjd * are done as follows:
99219089Spjd *
100219089Spjd *	From		   To			Routine
101219089Spjd *	PROC_NONE	-> PROC_CREATED		spa_activate()
102219089Spjd *	PROC_CREATED	-> PROC_ACTIVE		spa_thread()
103219089Spjd *	PROC_ACTIVE	-> PROC_DEACTIVATE	spa_deactivate()
104219089Spjd *	PROC_DEACTIVATE	-> PROC_GONE		spa_thread()
105219089Spjd *	PROC_GONE	-> PROC_NONE		spa_deactivate()
106219089Spjd */
107219089Spjdtypedef enum spa_proc_state {
108219089Spjd	SPA_PROC_NONE,		/* spa_proc = &p0, no process created */
109219089Spjd	SPA_PROC_CREATED,	/* spa_activate() has proc, is waiting */
110219089Spjd	SPA_PROC_ACTIVE,	/* taskqs created, spa_proc set */
111219089Spjd	SPA_PROC_DEACTIVATE,	/* spa_deactivate() requests process exit */
112219089Spjd	SPA_PROC_GONE		/* spa_thread() is exiting, spa_proc = &p0 */
113219089Spjd} spa_proc_state_t;
114219089Spjd
115258631Savgtypedef struct spa_taskqs {
116258631Savg	uint_t stqs_count;
117258631Savg	taskq_t **stqs_taskq;
118258631Savg} spa_taskqs_t;
119258631Savg
120299441Smavtypedef enum spa_all_vdev_zap_action {
121299441Smav	AVZ_ACTION_NONE = 0,
122299441Smav	AVZ_ACTION_DESTROY,	/* Destroy all per-vdev ZAPs and the AVZ. */
123299441Smav	AVZ_ACTION_REBUILD	/* Populate the new AVZ, see spa_avz_rebuild */
124299441Smav} spa_avz_action_t;
125299441Smav
126168404Spjdstruct spa {
127168404Spjd	/*
128168404Spjd	 * Fields protected by spa_namespace_lock.
129168404Spjd	 */
130185029Spjd	char		spa_name[MAXNAMELEN];	/* pool name */
131228103Smm	char		*spa_comment;		/* comment */
132168404Spjd	avl_node_t	spa_avl;		/* node in spa_namespace_avl */
133168404Spjd	nvlist_t	*spa_config;		/* last synced config */
134168404Spjd	nvlist_t	*spa_config_syncing;	/* currently syncing config */
135219089Spjd	nvlist_t	*spa_config_splitting;	/* config for splitting */
136219089Spjd	nvlist_t	*spa_load_info;		/* info and errors from load */
137168404Spjd	uint64_t	spa_config_txg;		/* txg of last config change */
138168404Spjd	int		spa_sync_pass;		/* iterate-to-convergence */
139208047Smm	pool_state_t	spa_state;		/* pool state */
140168404Spjd	int		spa_inject_ref;		/* injection references */
141168404Spjd	uint8_t		spa_sync_on;		/* sync threads are running */
142168404Spjd	spa_load_state_t spa_load_state;	/* current load operation */
143219089Spjd	uint64_t	spa_import_flags;	/* import specific flags */
144258631Savg	spa_taskqs_t	spa_zio_taskq[ZIO_TYPES][ZIO_TASKQ_TYPES];
145168404Spjd	dsl_pool_t	*spa_dsl_pool;
146236884Smm	boolean_t	spa_is_initializing;	/* true while opening pool */
147168404Spjd	metaslab_class_t *spa_normal_class;	/* normal data class */
148185029Spjd	metaslab_class_t *spa_log_class;	/* intent log data class */
149168404Spjd	uint64_t	spa_first_txg;		/* first txg after spa_open() */
150168404Spjd	uint64_t	spa_final_txg;		/* txg of export/destroy */
151168404Spjd	uint64_t	spa_freeze_txg;		/* freeze pool at this txg */
152219089Spjd	uint64_t	spa_load_max_txg;	/* best initial ub_txg */
153219089Spjd	uint64_t	spa_claim_max_txg;	/* highest claimed birth txg */
154219089Spjd	timespec_t	spa_loaded_ts;		/* 1st successful open time */
155168404Spjd	objset_t	*spa_meta_objset;	/* copy of dp->dp_meta_objset */
156286575Smav	kmutex_t	spa_evicting_os_lock;	/* Evicting objset list lock */
157286575Smav	list_t		spa_evicting_os_list;	/* Objsets being evicted. */
158286575Smav	kcondvar_t	spa_evicting_os_cv;	/* Objset Eviction Completion */
159168404Spjd	txg_list_t	spa_vdev_txg_list;	/* per-txg dirty vdev list */
160168404Spjd	vdev_t		*spa_root_vdev;		/* top-level vdev container */
161284304Savg	int		spa_min_ashift;		/* of vdevs in normal class */
162284304Savg	int		spa_max_ashift;		/* of vdevs in normal class */
163228103Smm	uint64_t	spa_config_guid;	/* config pool guid */
164228103Smm	uint64_t	spa_load_guid;		/* spa_load initialized guid */
165239620Smm	uint64_t	spa_last_synced_guid;	/* last synced guid */
166185029Spjd	list_t		spa_config_dirty_list;	/* vdevs with dirty config */
167185029Spjd	list_t		spa_state_dirty_list;	/* vdevs with dirty state */
168185029Spjd	spa_aux_vdev_t	spa_spares;		/* hot spares */
169185029Spjd	spa_aux_vdev_t	spa_l2cache;		/* L2ARC cache devices */
170236884Smm	nvlist_t	*spa_label_features;	/* Features for reading MOS */
171168404Spjd	uint64_t	spa_config_object;	/* MOS object for pool config */
172219089Spjd	uint64_t	spa_config_generation;	/* config generation number */
173168404Spjd	uint64_t	spa_syncing_txg;	/* txg currently syncing */
174219089Spjd	bpobj_t		spa_deferred_bpobj;	/* deferred-free bplist */
175219089Spjd	bplist_t	spa_free_bplist[TXG_SIZE]; /* bplist of stuff to free */
176289422Smav	zio_cksum_salt_t spa_cksum_salt;	/* secret salt for cksum */
177289422Smav	/* checksum context templates */
178289422Smav	kmutex_t	spa_cksum_tmpls_lock;
179289422Smav	void		*spa_cksum_tmpls[ZIO_CHECKSUM_FUNCTIONS];
180168404Spjd	uberblock_t	spa_ubsync;		/* last synced uberblock */
181168404Spjd	uberblock_t	spa_uberblock;		/* current uberblock */
182219089Spjd	boolean_t	spa_extreme_rewind;	/* rewind past deferred frees */
183219089Spjd	uint64_t	spa_last_io;		/* lbolt of last non-scan I/O */
184168404Spjd	kmutex_t	spa_scrub_lock;		/* resilver/scrub lock */
185168404Spjd	uint64_t	spa_scrub_inflight;	/* in-flight scrub I/Os */
186168404Spjd	kcondvar_t	spa_scrub_io_cv;	/* scrub I/O completion */
187168404Spjd	uint8_t		spa_scrub_active;	/* active or suspended? */
188168404Spjd	uint8_t		spa_scrub_type;		/* type of scrub we're doing */
189168404Spjd	uint8_t		spa_scrub_finished;	/* indicator to rotate logs */
190185029Spjd	uint8_t		spa_scrub_started;	/* started since last boot */
191185029Spjd	uint8_t		spa_scrub_reopen;	/* scrub doing vdev_reopen */
192219089Spjd	uint64_t	spa_scan_pass_start;	/* start time per pass/reboot */
193219089Spjd	uint64_t	spa_scan_pass_exam;	/* examined bytes per pass */
194168404Spjd	kmutex_t	spa_async_lock;		/* protect async state */
195168404Spjd	kthread_t	*spa_async_thread;	/* thread doing async task */
196253990Smav	kthread_t	*spa_async_thread_vd;	/* thread doing vd async task */
197168404Spjd	int		spa_async_suspended;	/* async tasks suspended */
198168404Spjd	kcondvar_t	spa_async_cv;		/* wait for thread_exit() */
199168404Spjd	uint16_t	spa_async_tasks;	/* async task mask */
200168404Spjd	char		*spa_root;		/* alternate root directory */
201168404Spjd	uint64_t	spa_ena;		/* spa-wide ereport ENA */
202219089Spjd	int		spa_last_open_failed;	/* error if last open failed */
203219089Spjd	uint64_t	spa_last_ubsync_txg;	/* "best" uberblock txg */
204219089Spjd	uint64_t	spa_last_ubsync_txg_ts;	/* timestamp from that ub */
205219089Spjd	uint64_t	spa_load_txg;		/* ub txg that loaded */
206219089Spjd	uint64_t	spa_load_txg_ts;	/* timestamp from that ub */
207219089Spjd	uint64_t	spa_load_meta_errors;	/* verify metadata err count */
208219089Spjd	uint64_t	spa_load_data_errors;	/* verify data err count */
209219089Spjd	uint64_t	spa_verify_min_txg;	/* start txg of verify scrub */
210168404Spjd	kmutex_t	spa_errlog_lock;	/* error log lock */
211168404Spjd	uint64_t	spa_errlog_last;	/* last error log object */
212168404Spjd	uint64_t	spa_errlog_scrub;	/* scrub error log object */
213168404Spjd	kmutex_t	spa_errlist_lock;	/* error list/ereport lock */
214168404Spjd	avl_tree_t	spa_errlist_last;	/* last error list */
215168404Spjd	avl_tree_t	spa_errlist_scrub;	/* scrub error list */
216168404Spjd	uint64_t	spa_deflate;		/* should we deflate? */
217168404Spjd	uint64_t	spa_history;		/* history object */
218168404Spjd	kmutex_t	spa_history_lock;	/* history lock */
219168404Spjd	vdev_t		*spa_pending_vdev;	/* pending vdev additions */
220168404Spjd	kmutex_t	spa_props_lock;		/* property lock */
221168404Spjd	uint64_t	spa_pool_props_object;	/* object for properties */
222168404Spjd	uint64_t	spa_bootfs;		/* default boot filesystem */
223185029Spjd	uint64_t	spa_failmode;		/* failure mode for the pool */
224185029Spjd	uint64_t	spa_delegation;		/* delegation on/off */
225185029Spjd	list_t		spa_config_list;	/* previous cache file(s) */
226272598Sdelphij	/* per-CPU array of root of async I/O: */
227272598Sdelphij	zio_t		**spa_async_zio_root;
228185029Spjd	zio_t		*spa_suspend_zio_root;	/* root of all suspended I/O */
229185029Spjd	kmutex_t	spa_suspend_lock;	/* protects suspend_zio_root */
230185029Spjd	kcondvar_t	spa_suspend_cv;		/* notification of resume */
231185029Spjd	uint8_t		spa_suspended;		/* pool is suspended */
232219089Spjd	uint8_t		spa_claiming;		/* pool is doing zil_claim() */
233224177Smm	boolean_t	spa_debug;		/* debug enabled? */
234185029Spjd	boolean_t	spa_is_root;		/* pool is root */
235185029Spjd	int		spa_minref;		/* num refs when first opened */
236209962Smm	int		spa_mode;		/* FREAD | FWRITE */
237185029Spjd	spa_log_state_t spa_log_state;		/* log state */
238219089Spjd	uint64_t	spa_autoexpand;		/* lun expansion on/off */
239219089Spjd	ddt_t		*spa_ddt[ZIO_CHECKSUM_FUNCTIONS]; /* in-core DDTs */
240219089Spjd	uint64_t	spa_ddt_stat_object;	/* DDT statistics */
241219089Spjd	uint64_t	spa_dedup_ditto;	/* dedup ditto threshold */
242219089Spjd	uint64_t	spa_dedup_checksum;	/* default dedup checksum */
243219089Spjd	uint64_t	spa_dspace;		/* dspace in normal class */
244219089Spjd	kmutex_t	spa_vdev_top_lock;	/* dueling offline/remove */
245219089Spjd	kmutex_t	spa_proc_lock;		/* protects spa_proc* */
246219089Spjd	kcondvar_t	spa_proc_cv;		/* spa_proc_state transitions */
247219089Spjd	spa_proc_state_t spa_proc_state;	/* see definition */
248219089Spjd	struct proc	*spa_proc;		/* "zpool-poolname" process */
249219089Spjd	uint64_t	spa_did;		/* if procp != p0, did of t1 */
250240868Spjd	kthread_t	*spa_trim_thread;	/* thread sending TRIM I/Os */
251240868Spjd	kmutex_t	spa_trim_lock;		/* protects spa_trim_cv */
252240868Spjd	kcondvar_t	spa_trim_cv;		/* used to notify TRIM thread */
253219089Spjd	boolean_t	spa_autoreplace;	/* autoreplace set in open */
254219089Spjd	int		spa_vdev_locks;		/* locks grabbed */
255219089Spjd	uint64_t	spa_creation_version;	/* version at pool creation */
256236884Smm	uint64_t	spa_prev_software_version; /* See ub_software_version */
257236884Smm	uint64_t	spa_feat_for_write_obj;	/* required to write to pool */
258236884Smm	uint64_t	spa_feat_for_read_obj;	/* required to read from pool */
259236884Smm	uint64_t	spa_feat_desc_obj;	/* Feature descriptions */
260260150Sdelphij	uint64_t	spa_feat_enabled_txg_obj; /* Feature enabled txg */
261260150Sdelphij	/* cache feature refcounts */
262260150Sdelphij	uint64_t	spa_feat_refcount_cache[SPA_FEATURES];
263247265Smm#ifdef illumos
264247265Smm	cyclic_id_t	spa_deadman_cycid;	/* cyclic id */
265277300Ssmh#else	/* !illumos */
266247265Smm#ifdef _KERNEL
267247265Smm	struct callout	spa_deadman_cycid;	/* callout id */
268247265Smm#endif
269247265Smm#endif	/* illumos */
270247265Smm	uint64_t	spa_deadman_calls;	/* number of deadman calls */
271258632Savg	hrtime_t	spa_sync_starttime;	/* starting time fo spa_sync */
272247265Smm	uint64_t	spa_deadman_synctime;	/* deadman expiration timer */
273299441Smav	uint64_t	spa_all_vdev_zaps;	/* ZAP of per-vd ZAP obj #s */
274299441Smav	spa_avz_action_t	spa_avz_action;	/* destroy/rebuild AVZ? */
275299441Smav
276258632Savg#ifdef illumos
277258632Savg	/*
278258632Savg	 * spa_iokstat_lock protects spa_iokstat and
279258632Savg	 * spa_queue_stats[].
280258632Savg	 */
281258632Savg	kmutex_t	spa_iokstat_lock;
282258632Savg	struct kstat	*spa_iokstat;		/* kstat of io to this pool */
283258632Savg	struct {
284258632Savg		int spa_active;
285258632Savg		int spa_queued;
286258632Savg	} spa_queue_stats[ZIO_PRIORITY_NUM_QUEUEABLE];
287258632Savg#endif
288251636Sdelphij	hrtime_t	spa_ccw_fail_time;	/* Conf cache write fail time */
289258632Savg
290168404Spjd	/*
291251631Sdelphij	 * spa_refcount & spa_config_lock must be the last elements
292185029Spjd	 * because refcount_t changes size based on compilation options.
293185029Spjd	 * In order for the MDB module to function correctly, the other
294185029Spjd	 * fields must remain in the same location.
295168404Spjd	 */
296185029Spjd	spa_config_lock_t spa_config_lock[SCL_LOCKS]; /* config changes */
297168404Spjd	refcount_t	spa_refcount;		/* number of opens */
298277300Ssmh#ifndef illumos
299219089Spjd	boolean_t	spa_splitting_newspa;	/* creating new spa in split */
300219089Spjd#endif
301168404Spjd};
302168404Spjd
303185029Spjdextern const char *spa_config_path;
304168404Spjd
305258631Savgextern void spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
306258631Savg    task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent);
307258631Savg
308168404Spjd#ifdef	__cplusplus
309168404Spjd}
310168404Spjd#endif
311168404Spjd
312168404Spjd#endif	/* _SYS_SPA_IMPL_H */
313