xref: /onnv-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision 5331:3047ad28a67b)
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);
1705094Slling 		if (spa->spa_config && spa->spa_name && !spa->spa_temporary) {
171789Sahrens 			VERIFY(nvlist_add_nvlist(config, spa->spa_name,
172789Sahrens 			    spa->spa_config) == 0);
1735094Slling 		}
174789Sahrens 		mutex_exit(&spa->spa_config_cache_lock);
175789Sahrens 	}
176789Sahrens 
177789Sahrens 	/*
178789Sahrens 	 * Pack the configuration into a buffer.
179789Sahrens 	 */
180789Sahrens 	VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0);
181789Sahrens 
182789Sahrens 	buf = kmem_alloc(buflen, KM_SLEEP);
183789Sahrens 
1841544Seschrock 	VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR,
1851544Seschrock 	    KM_SLEEP) == 0);
186789Sahrens 
187789Sahrens 	/*
188789Sahrens 	 * Write the configuration to disk.  We need to do the traditional
189789Sahrens 	 * 'write to temporary file, sync, move over original' to make sure we
190789Sahrens 	 * always have a consistent view of the data.
191789Sahrens 	 */
192789Sahrens 	(void) snprintf(pathname, sizeof (pathname), "%s/%s", spa_config_dir,
193789Sahrens 	    ZPOOL_CACHE_TMP);
194789Sahrens 
195789Sahrens 	if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0)
196789Sahrens 		goto out;
197789Sahrens 
198789Sahrens 	if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
199789Sahrens 	    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
200*5331Samw 	    VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) {
201789Sahrens 		(void) snprintf(pathname2, sizeof (pathname2), "%s/%s",
202789Sahrens 		    spa_config_dir, ZPOOL_CACHE_FILE);
203789Sahrens 		(void) vn_rename(pathname, pathname2, UIO_SYSSPACE);
204789Sahrens 	}
205789Sahrens 
206*5331Samw 	(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
207789Sahrens 	VN_RELE(vp);
208789Sahrens 
209789Sahrens out:
210789Sahrens 	(void) vn_remove(pathname, UIO_SYSSPACE, RMFILE);
211789Sahrens 	spa_config_generation++;
212789Sahrens 
213789Sahrens 	kmem_free(buf, buflen);
214789Sahrens 	nvlist_free(config);
215789Sahrens }
216789Sahrens 
217789Sahrens /*
2181635Sbonwick  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
219789Sahrens  * and we don't want to allow the local zone to see all the pools anyway.
220789Sahrens  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
221789Sahrens  * information for all pool visible within the zone.
222789Sahrens  */
223789Sahrens nvlist_t *
224789Sahrens spa_all_configs(uint64_t *generation)
225789Sahrens {
226789Sahrens 	nvlist_t *pools;
227789Sahrens 	spa_t *spa;
228789Sahrens 
229789Sahrens 	if (*generation == spa_config_generation)
230789Sahrens 		return (NULL);
231789Sahrens 
2321544Seschrock 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
233789Sahrens 
234789Sahrens 	spa = NULL;
235789Sahrens 	mutex_enter(&spa_namespace_lock);
236789Sahrens 	while ((spa = spa_next(spa)) != NULL) {
237789Sahrens 		if (INGLOBALZONE(curproc) ||
238789Sahrens 		    zone_dataset_visible(spa_name(spa), NULL)) {
239789Sahrens 			mutex_enter(&spa->spa_config_cache_lock);
240789Sahrens 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
241789Sahrens 			    spa->spa_config) == 0);
242789Sahrens 			mutex_exit(&spa->spa_config_cache_lock);
243789Sahrens 		}
244789Sahrens 	}
245789Sahrens 	mutex_exit(&spa_namespace_lock);
246789Sahrens 
247789Sahrens 	*generation = spa_config_generation;
248789Sahrens 
249789Sahrens 	return (pools);
250789Sahrens }
251789Sahrens 
252789Sahrens void
253789Sahrens spa_config_set(spa_t *spa, nvlist_t *config)
254789Sahrens {
255789Sahrens 	mutex_enter(&spa->spa_config_cache_lock);
256789Sahrens 	if (spa->spa_config != NULL)
257789Sahrens 		nvlist_free(spa->spa_config);
258789Sahrens 	spa->spa_config = config;
259789Sahrens 	mutex_exit(&spa->spa_config_cache_lock);
260789Sahrens }
261789Sahrens 
262789Sahrens /*
263789Sahrens  * Generate the pool's configuration based on the current in-core state.
264789Sahrens  * We infer whether to generate a complete config or just one top-level config
265789Sahrens  * based on whether vd is the root vdev.
266789Sahrens  */
267789Sahrens nvlist_t *
268789Sahrens spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
269789Sahrens {
270789Sahrens 	nvlist_t *config, *nvroot;
271789Sahrens 	vdev_t *rvd = spa->spa_root_vdev;
2723975Sek110237 	unsigned long hostid = 0;
273789Sahrens 
2744787Sahrens 	ASSERT(spa_config_held(spa, RW_READER) ||
2754787Sahrens 	    spa_config_held(spa, RW_WRITER));
2761635Sbonwick 
277789Sahrens 	if (vd == NULL)
278789Sahrens 		vd = rvd;
279789Sahrens 
280789Sahrens 	/*
281789Sahrens 	 * If txg is -1, report the current value of spa->spa_config_txg.
282789Sahrens 	 */
283789Sahrens 	if (txg == -1ULL)
284789Sahrens 		txg = spa->spa_config_txg;
285789Sahrens 
2861544Seschrock 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
287789Sahrens 
288789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
2892082Seschrock 	    spa_version(spa)) == 0);
290789Sahrens 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
291789Sahrens 	    spa_name(spa)) == 0);
292789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
293789Sahrens 	    spa_state(spa)) == 0);
294789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
295789Sahrens 	    txg) == 0);
296789Sahrens 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
297789Sahrens 	    spa_guid(spa)) == 0);
2983975Sek110237 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
2994178Slling 	if (hostid != 0) {
3004178Slling 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
3014527Sperrin 		    hostid) == 0);
3024178Slling 	}
3033975Sek110237 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
3043975Sek110237 	    utsname.nodename) == 0);
305789Sahrens 
306789Sahrens 	if (vd != rvd) {
307789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
308789Sahrens 		    vd->vdev_top->vdev_guid) == 0);
309789Sahrens 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
310789Sahrens 		    vd->vdev_guid) == 0);
3112082Seschrock 		if (vd->vdev_isspare)
3122082Seschrock 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
3132082Seschrock 			    1ULL) == 0);
3144527Sperrin 		if (vd->vdev_islog)
3154527Sperrin 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
3164527Sperrin 			    1ULL) == 0);
317789Sahrens 		vd = vd->vdev_top;		/* label contains top config */
318789Sahrens 	}
319789Sahrens 
3202082Seschrock 	nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE);
321789Sahrens 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
322789Sahrens 	nvlist_free(nvroot);
323789Sahrens 
324789Sahrens 	return (config);
325789Sahrens }
3261635Sbonwick 
3271635Sbonwick /*
3281635Sbonwick  * Update all disk labels, generate a fresh config based on the current
3291635Sbonwick  * in-core state, and sync the global config cache.
3301635Sbonwick  */
3311635Sbonwick void
3321635Sbonwick spa_config_update(spa_t *spa, int what)
3331635Sbonwick {
3341635Sbonwick 	vdev_t *rvd = spa->spa_root_vdev;
3351635Sbonwick 	uint64_t txg;
3361635Sbonwick 	int c;
3371635Sbonwick 
3381635Sbonwick 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
3391635Sbonwick 
3401635Sbonwick 	spa_config_enter(spa, RW_WRITER, FTAG);
3411635Sbonwick 	txg = spa_last_synced_txg(spa) + 1;
3421635Sbonwick 	if (what == SPA_CONFIG_UPDATE_POOL) {
3431635Sbonwick 		vdev_config_dirty(rvd);
3441635Sbonwick 	} else {
3451635Sbonwick 		/*
3461635Sbonwick 		 * If we have top-level vdevs that were added but have
3471635Sbonwick 		 * not yet been prepared for allocation, do that now.
3481635Sbonwick 		 * (It's safe now because the config cache is up to date,
3491635Sbonwick 		 * so it will be able to translate the new DVAs.)
3501635Sbonwick 		 * See comments in spa_vdev_add() for full details.
3511635Sbonwick 		 */
3521635Sbonwick 		for (c = 0; c < rvd->vdev_children; c++) {
3531635Sbonwick 			vdev_t *tvd = rvd->vdev_child[c];
3541635Sbonwick 			if (tvd->vdev_ms_array == 0) {
3551635Sbonwick 				vdev_init(tvd, txg);
3561635Sbonwick 				vdev_config_dirty(tvd);
3571635Sbonwick 			}
3581635Sbonwick 		}
3591635Sbonwick 	}
3601635Sbonwick 	spa_config_exit(spa, FTAG);
3611635Sbonwick 
3621635Sbonwick 	/*
3631635Sbonwick 	 * Wait for the mosconfig to be regenerated and synced.
3641635Sbonwick 	 */
3651635Sbonwick 	txg_wait_synced(spa->spa_dsl_pool, txg);
3661635Sbonwick 
3671635Sbonwick 	/*
3681635Sbonwick 	 * Update the global config cache to reflect the new mosconfig.
3691635Sbonwick 	 */
3701635Sbonwick 	spa_config_sync();
3711635Sbonwick 
3721635Sbonwick 	if (what == SPA_CONFIG_UPDATE_POOL)
3731635Sbonwick 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
3741635Sbonwick }
375