xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision 4787:602d3f97842c)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
212082Seschrock 
22789Sahrens /*
233912Slling  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24789Sahrens  * Use is subject to license terms.
25789Sahrens  */
26789Sahrens 
27789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28789Sahrens 
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/spa_impl.h>
31789Sahrens #include <sys/nvpair.h>
32789Sahrens #include <sys/uio.h>
33789Sahrens #include <sys/fs/zfs.h>
34789Sahrens #include <sys/vdev_impl.h>
35789Sahrens #include <sys/zfs_ioctl.h>
363975Sek110237 #include <sys/utsname.h>
373975Sek110237 #include <sys/systeminfo.h>
383975Sek110237 #include <sys/sunddi.h>
391544Seschrock #ifdef _KERNEL
401544Seschrock #include <sys/kobj.h>
411544Seschrock #endif
421544Seschrock 
43789Sahrens /*
44789Sahrens  * Pool configuration repository.
45789Sahrens  *
46789Sahrens  * The configuration for all pools, in addition to being stored on disk, is
471635Sbonwick  * stored in /etc/zfs/zpool.cache as a packed nvlist.  The kernel maintains
48789Sahrens  * this list as pools are created, destroyed, or modified.
49789Sahrens  *
50789Sahrens  * We have a single nvlist which holds all the configuration information.  When
51789Sahrens  * the module loads, we read this information from the cache and populate the
52789Sahrens  * SPA namespace.  This namespace is maintained independently in spa.c.
53789Sahrens  * Whenever the namespace is modified, or the configuration of a pool is
54789Sahrens  * changed, we call spa_config_sync(), which walks through all the active pools
55789Sahrens  * and writes the configuration to disk.
56789Sahrens  */
57789Sahrens 
58789Sahrens static uint64_t spa_config_generation = 1;
59789Sahrens 
60789Sahrens /*
61789Sahrens  * This can be overridden in userland to preserve an alternate namespace for
62789Sahrens  * userland pools when doing testing.
63789Sahrens  */
64789Sahrens const char *spa_config_dir = ZPOOL_CACHE_DIR;
65789Sahrens 
66789Sahrens /*
67789Sahrens  * Called when the module is first loaded, this routine loads the configuration
68789Sahrens  * file into the SPA namespace.  It does not actually open or load the pools; it
69789Sahrens  * only populates the namespace.
70789Sahrens  */
71789Sahrens void
72789Sahrens spa_config_load(void)
73789Sahrens {
74789Sahrens 	void *buf = NULL;
75789Sahrens 	nvlist_t *nvlist, *child;
76789Sahrens 	nvpair_t *nvpair;
77789Sahrens 	spa_t *spa;
78789Sahrens 	char pathname[128];
791544Seschrock 	struct _buf *file;
803912Slling 	uint64_t fsize;
81789Sahrens 
82789Sahrens 	/*
83789Sahrens 	 * Open the configuration file.
84789Sahrens 	 */
851544Seschrock 	(void) snprintf(pathname, sizeof (pathname), "%s%s/%s",
861635Sbonwick 	    (rootdir != NULL) ? "./" : "", spa_config_dir, ZPOOL_CACHE_FILE);
871544Seschrock 
881544Seschrock 	file = kobj_open_file(pathname);
891544Seschrock 	if (file == (struct _buf *)-1)
90789Sahrens 		return;
91789Sahrens 
923912Slling 	if (kobj_get_filesize(file, &fsize) != 0)
931544Seschrock 		goto out;
941544Seschrock 
953912Slling 	buf = kmem_alloc(fsize, KM_SLEEP);
961544Seschrock 
97789Sahrens 	/*
98789Sahrens 	 * Read the nvlist from the file.
99789Sahrens 	 */
1003912Slling 	if (kobj_read_file(file, buf, fsize, 0) < 0)
101789Sahrens 		goto out;
102789Sahrens 
103789Sahrens 	/*
104789Sahrens 	 * Unpack the nvlist.
105789Sahrens 	 */
1063912Slling 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
107789Sahrens 		goto out;
108789Sahrens 
109789Sahrens 	/*
110789Sahrens 	 * Iterate over all elements in the nvlist, creating a new spa_t for
111789Sahrens 	 * each one with the specified configuration.
112789Sahrens 	 */
113789Sahrens 	mutex_enter(&spa_namespace_lock);
114789Sahrens 	nvpair = NULL;
115789Sahrens 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
116789Sahrens 
117789Sahrens 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
118789Sahrens 			continue;
119789Sahrens 
120789Sahrens 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
121789Sahrens 
122789Sahrens 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
123789Sahrens 			continue;
1241635Sbonwick 		spa = spa_add(nvpair_name(nvpair), NULL);
125789Sahrens 
126789Sahrens 		/*
127789Sahrens 		 * We blindly duplicate the configuration here.  If it's
128789Sahrens 		 * invalid, we will catch it when the pool is first opened.
129789Sahrens 		 */
130789Sahrens 		VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0);
131789Sahrens 	}
132789Sahrens 	mutex_exit(&spa_namespace_lock);
133789Sahrens 
134789Sahrens 	nvlist_free(nvlist);
135789Sahrens 
136789Sahrens out:
137789Sahrens 	if (buf != NULL)
1383912Slling 		kmem_free(buf, fsize);
139789Sahrens 
1401544Seschrock 	kobj_close_file(file);
141789Sahrens }
142789Sahrens 
143789Sahrens /*
144789Sahrens  * Synchronize all pools to disk.  This must be called with the namespace lock
145789Sahrens  * held.
146789Sahrens  */
147789Sahrens void
148789Sahrens spa_config_sync(void)
149789Sahrens {
150789Sahrens 	spa_t *spa = NULL;
151789Sahrens 	nvlist_t *config;
152789Sahrens 	size_t buflen;
153789Sahrens 	char *buf;
154789Sahrens 	vnode_t *vp;
155789Sahrens 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
156789Sahrens 	char pathname[128];
157789Sahrens 	char pathname2[128];
158789Sahrens 
159789Sahrens 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
160789Sahrens 
1611544Seschrock 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
162789Sahrens 
163789Sahrens 	/*
164789Sahrens 	 * Add all known pools to the configuration list, ignoring those with
165789Sahrens 	 * alternate root paths.
166789Sahrens 	 */
167789Sahrens 	spa = NULL;
168789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
169789Sahrens 		mutex_enter(&spa->spa_config_cache_lock);
170789Sahrens 		if (spa->spa_config && spa->spa_name && spa->spa_root == NULL)
171789Sahrens 			VERIFY(nvlist_add_nvlist(config, spa->spa_name,
172789Sahrens 			    spa->spa_config) == 0);
173789Sahrens 		mutex_exit(&spa->spa_config_cache_lock);
174789Sahrens 	}
175789Sahrens 
176789Sahrens 	/*
177789Sahrens 	 * Pack the configuration into a buffer.
178789Sahrens 	 */
179789Sahrens 	VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0);
180789Sahrens 
181789Sahrens 	buf = kmem_alloc(buflen, KM_SLEEP);
182789Sahrens 
1831544Seschrock 	VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR,
1841544Seschrock 	    KM_SLEEP) == 0);
185789Sahrens 
186789Sahrens 	/*
187789Sahrens 	 * Write the configuration to disk.  We need to do the traditional
188789Sahrens 	 * 'write to temporary file, sync, move over original' to make sure we
189789Sahrens 	 * always have a consistent view of the data.
190789Sahrens 	 */
191789Sahrens 	(void) snprintf(pathname, sizeof (pathname), "%s/%s", spa_config_dir,
192789Sahrens 	    ZPOOL_CACHE_TMP);
193789Sahrens 
194789Sahrens 	if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0)
195789Sahrens 		goto out;
196789Sahrens 
197789Sahrens 	if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
198789Sahrens 	    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
199789Sahrens 	    VOP_FSYNC(vp, FSYNC, kcred) == 0) {
200789Sahrens 		(void) snprintf(pathname2, sizeof (pathname2), "%s/%s",
201789Sahrens 		    spa_config_dir, ZPOOL_CACHE_FILE);
202789Sahrens 		(void) vn_rename(pathname, pathname2, UIO_SYSSPACE);
203789Sahrens 	}
204789Sahrens 
205789Sahrens 	(void) VOP_CLOSE(vp, oflags, 1, 0, kcred);
206789Sahrens 	VN_RELE(vp);
207789Sahrens 
208789Sahrens out:
209789Sahrens 	(void) vn_remove(pathname, UIO_SYSSPACE, RMFILE);
210789Sahrens 	spa_config_generation++;
211789Sahrens 
212789Sahrens 	kmem_free(buf, buflen);
213789Sahrens 	nvlist_free(config);
214789Sahrens }
215789Sahrens 
216789Sahrens /*
2171635Sbonwick  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
218789Sahrens  * and we don't want to allow the local zone to see all the pools anyway.
219789Sahrens  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
220789Sahrens  * information for all pool visible within the zone.
221789Sahrens  */
222789Sahrens nvlist_t *
223789Sahrens spa_all_configs(uint64_t *generation)
224789Sahrens {
225789Sahrens 	nvlist_t *pools;
226789Sahrens 	spa_t *spa;
227789Sahrens 
228789Sahrens 	if (*generation == spa_config_generation)
229789Sahrens 		return (NULL);
230789Sahrens 
2311544Seschrock 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
232789Sahrens 
233789Sahrens 	spa = NULL;
234789Sahrens 	mutex_enter(&spa_namespace_lock);
235789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
236789Sahrens 		if (INGLOBALZONE(curproc) ||
237789Sahrens 		    zone_dataset_visible(spa_name(spa), NULL)) {
238789Sahrens 			mutex_enter(&spa->spa_config_cache_lock);
239789Sahrens 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
240789Sahrens 			    spa->spa_config) == 0);
241789Sahrens 			mutex_exit(&spa->spa_config_cache_lock);
242789Sahrens 		}
243789Sahrens 	}
244789Sahrens 	mutex_exit(&spa_namespace_lock);
245789Sahrens 
246789Sahrens 	*generation = spa_config_generation;
247789Sahrens 
248789Sahrens 	return (pools);
249789Sahrens }
250789Sahrens 
251789Sahrens void
252789Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
253789Sahrens {
254789Sahrens 	mutex_enter(&spa->spa_config_cache_lock);
255789Sahrens 	if (spa->spa_config != NULL)
256789Sahrens 		nvlist_free(spa->spa_config);
257789Sahrens 	spa->spa_config = config;
258789Sahrens 	mutex_exit(&spa->spa_config_cache_lock);
259789Sahrens }
260789Sahrens 
261789Sahrens /*
262789Sahrens  * Generate the pool's configuration based on the current in-core state.
263789Sahrens  * We infer whether to generate a complete config or just one top-level config
264789Sahrens  * based on whether vd is the root vdev.
265789Sahrens  */
266789Sahrens nvlist_t *
267789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
268789Sahrens {
269789Sahrens 	nvlist_t *config, *nvroot;
270789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2713975Sek110237 	unsigned long hostid = 0;
272789Sahrens 
273*4787Sahrens 	ASSERT(spa_config_held(spa, RW_READER) ||
274*4787Sahrens 	    spa_config_held(spa, RW_WRITER));
2751635Sbonwick 
276789Sahrens 	if (vd == NULL)
277789Sahrens 		vd = rvd;
278789Sahrens 
279789Sahrens 	/*
280789Sahrens 	 * If txg is -1, report the current value of spa->spa_config_txg.
281789Sahrens 	 */
282789Sahrens 	if (txg == -1ULL)
283789Sahrens 		txg = spa->spa_config_txg;
284789Sahrens 
2851544Seschrock 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
286789Sahrens 
287789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
2882082Seschrock 	    spa_version(spa)) == 0);
289789Sahrens 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
290789Sahrens 	    spa_name(spa)) == 0);
291789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
292789Sahrens 	    spa_state(spa)) == 0);
293789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
294789Sahrens 	    txg) == 0);
295789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
296789Sahrens 	    spa_guid(spa)) == 0);
2973975Sek110237 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
2984178Slling 	if (hostid != 0) {
2994178Slling 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
3004527Sperrin 		    hostid) == 0);
3014178Slling 	}
3023975Sek110237 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
3033975Sek110237 	    utsname.nodename) == 0);
304789Sahrens 
305789Sahrens 	if (vd != rvd) {
306789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
307789Sahrens 		    vd->vdev_top->vdev_guid) == 0);
308789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
309789Sahrens 		    vd->vdev_guid) == 0);
3102082Seschrock 		if (vd->vdev_isspare)
3112082Seschrock 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
3122082Seschrock 			    1ULL) == 0);
3134527Sperrin 		if (vd->vdev_islog)
3144527Sperrin 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
3154527Sperrin 			    1ULL) == 0);
316789Sahrens 		vd = vd->vdev_top;		/* label contains top config */
317789Sahrens 	}
318789Sahrens 
3192082Seschrock 	nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE);
320789Sahrens 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
321789Sahrens 	nvlist_free(nvroot);
322789Sahrens 
323789Sahrens 	return (config);
324789Sahrens }
3251635Sbonwick 
3261635Sbonwick /*
3271635Sbonwick  * Update all disk labels, generate a fresh config based on the current
3281635Sbonwick  * in-core state, and sync the global config cache.
3291635Sbonwick  */
3301635Sbonwick void
3311635Sbonwick spa_config_update(spa_t *spa, int what)
3321635Sbonwick {
3331635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3341635Sbonwick 	uint64_t txg;
3351635Sbonwick 	int c;
3361635Sbonwick 
3371635Sbonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3381635Sbonwick 
3391635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
3401635Sbonwick 	txg = spa_last_synced_txg(spa) + 1;
3411635Sbonwick 	if (what == SPA_CONFIG_UPDATE_POOL) {
3421635Sbonwick 		vdev_config_dirty(rvd);
3431635Sbonwick 	} else {
3441635Sbonwick 		/*
3451635Sbonwick 		 * If we have top-level vdevs that were added but have
3461635Sbonwick 		 * not yet been prepared for allocation, do that now.
3471635Sbonwick 		 * (It's safe now because the config cache is up to date,
3481635Sbonwick 		 * so it will be able to translate the new DVAs.)
3491635Sbonwick 		 * See comments in spa_vdev_add() for full details.
3501635Sbonwick 		 */
3511635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++) {
3521635Sbonwick 			vdev_t *tvd = rvd->vdev_child[c];
3531635Sbonwick 			if (tvd->vdev_ms_array == 0) {
3541635Sbonwick 				vdev_init(tvd, txg);
3551635Sbonwick 				vdev_config_dirty(tvd);
3561635Sbonwick 			}
3571635Sbonwick 		}
3581635Sbonwick 	}
3591635Sbonwick 	spa_config_exit(spa, FTAG);
3601635Sbonwick 
3611635Sbonwick 	/*
3621635Sbonwick 	 * Wait for the mosconfig to be regenerated and synced.
3631635Sbonwick 	 */
3641635Sbonwick 	txg_wait_synced(spa->spa_dsl_pool, txg);
3651635Sbonwick 
3661635Sbonwick 	/*
3671635Sbonwick 	 * Update the global config cache to reflect the new mosconfig.
3681635Sbonwick 	 */
3691635Sbonwick 	spa_config_sync();
3701635Sbonwick 
3711635Sbonwick 	if (what == SPA_CONFIG_UPDATE_POOL)
3721635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
3731635Sbonwick }
374