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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "@(#)spa_config.c 1.15 08/04/01 SMI"
30 #include <sys/spa_impl.h>
31 #include <sys/nvpair.h>
33 #include <sys/fs/zfs.h>
34 #include <sys/vdev_impl.h>
35 #include <sys/zfs_ioctl.h>
36 #include <sys/utsname.h>
37 #include <sys/systeminfo.h>
38 #include <sys/sunddi.h>
44 * Pool configuration repository.
46 * Pool configuration is stored as a packed nvlist on the filesystem. By
47 * default, all pools are stored in /etc/zfs/zpool.cache and loaded on boot
48 * (when the ZFS module is loaded). Pools can also have the 'cachefile'
49 * property set that allows them to be stored in an alternate location until
50 * the control of external software.
52 * For each cache file, we have a single nvlist which holds all the
53 * configuration information. When the module loads, we read this information
54 * from /etc/zfs/zpool.cache and populate the SPA namespace. This namespace is
55 * maintained independently in spa.c. Whenever the namespace is modified, or
56 * the configuration of a pool is changed, we call spa_config_sync(), which
57 * walks through all the active pools and writes the configuration to disk.
60 static uint64_t spa_config_generation = 1;
63 * This can be overridden in userland to preserve an alternate namespace for
64 * userland pools when doing testing.
66 const char *spa_config_dir = ZPOOL_CACHE_DIR;
69 * Called when the module is first loaded, this routine loads the configuration
70 * file into the SPA namespace. It does not actually open or load the pools; it
71 * only populates the namespace.
77 nvlist_t *nvlist, *child;
85 * Open the configuration file.
87 (void) snprintf(pathname, sizeof (pathname), "%s%s/%s",
88 (rootdir != NULL) ? "./" : "", spa_config_dir, ZPOOL_CACHE_FILE);
90 file = kobj_open_file(pathname);
91 if (file == (struct _buf *)-1)
94 if (kobj_get_filesize(file, &fsize) != 0)
97 buf = kmem_alloc(fsize, KM_SLEEP);
100 * Read the nvlist from the file.
102 if (kobj_read_file(file, buf, fsize, 0) < 0)
108 if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
112 * Iterate over all elements in the nvlist, creating a new spa_t for
113 * each one with the specified configuration.
115 mutex_enter(&spa_namespace_lock);
117 while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
119 if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
122 VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
124 if (spa_lookup(nvpair_name(nvpair)) != NULL)
126 spa = spa_add(nvpair_name(nvpair), NULL);
129 * We blindly duplicate the configuration here. If it's
130 * invalid, we will catch it when the pool is first opened.
132 VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0);
134 mutex_exit(&spa_namespace_lock);
140 kmem_free(buf, fsize);
142 kobj_close_file(file);
146 * This function is called when destroying or exporting a pool. It walks the
147 * list of active pools, and searches for any that match the given cache file.
148 * If there is only one cachefile, then the file is removed immediately,
149 * because we won't see the pool when iterating in spa_config_sync().
152 spa_config_check(const char *dir, const char *file)
158 if (dir != NULL && strcmp(dir, "none") == 0)
161 ASSERT(MUTEX_HELD(&spa_namespace_lock));
163 while ((spa = spa_next(spa)) != NULL) {
165 if (spa->spa_config_dir == NULL)
168 if (spa->spa_config_dir &&
169 strcmp(spa->spa_config_dir, dir) == 0 &&
170 strcmp(spa->spa_config_file, file) == 0)
177 dir = spa_config_dir;
178 file = ZPOOL_CACHE_FILE;
181 (void) snprintf(pathname, sizeof (pathname),
183 (void) vn_remove(pathname, UIO_SYSSPACE, RMFILE);
187 typedef struct spa_config_entry {
192 } spa_config_entry_t;
195 spa_config_entry_add(list_t *listp, spa_t *spa)
197 spa_config_entry_t *entry;
198 const char *dir, *file;
200 mutex_enter(&spa->spa_config_cache_lock);
201 if (!spa->spa_config || !spa->spa_name) {
202 mutex_exit(&spa->spa_config_cache_lock);
206 if (spa->spa_config_dir) {
207 dir = spa->spa_config_dir;
208 file = spa->spa_config_file;
210 dir = spa_config_dir;
211 file = ZPOOL_CACHE_FILE;
214 if (strcmp(dir, "none") == 0) {
215 mutex_exit(&spa->spa_config_cache_lock);
219 for (entry = list_head(listp); entry != NULL;
220 entry = list_next(listp, entry)) {
221 if (strcmp(entry->sc_dir, dir) == 0 &&
222 strcmp(entry->sc_file, file) == 0)
227 entry = kmem_alloc(sizeof (spa_config_entry_t), KM_SLEEP);
229 entry->sc_file = file;
230 VERIFY(nvlist_alloc(&entry->sc_nvl, NV_UNIQUE_NAME,
232 list_insert_tail(listp, entry);
235 VERIFY(nvlist_add_nvlist(entry->sc_nvl, spa->spa_name,
236 spa->spa_config) == 0);
237 mutex_exit(&spa->spa_config_cache_lock);
241 spa_config_entry_write(spa_config_entry_t *entry)
243 nvlist_t *config = entry->sc_nvl;
247 int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
252 * Pack the configuration into a buffer.
254 VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0);
256 buf = kmem_alloc(buflen, KM_SLEEP);
258 VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR,
262 * Write the configuration to disk. We need to do the traditional
263 * 'write to temporary file, sync, move over original' to make sure we
264 * always have a consistent view of the data.
266 (void) snprintf(pathname, sizeof (pathname), "%s/.%s", entry->sc_dir,
269 if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0)
272 if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
273 0, RLIM64_INFINITY, kcred, NULL) == 0 &&
274 VOP_FSYNC(vp, FSYNC, kcred, NULL) == 0) {
275 (void) snprintf(pathname2, sizeof (pathname2), "%s/%s",
276 entry->sc_dir, entry->sc_file);
277 (void) vn_rename(pathname, pathname2, UIO_SYSSPACE);
280 (void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
284 (void) vn_remove(pathname, UIO_SYSSPACE, RMFILE);
285 kmem_free(buf, buflen);
289 * Synchronize all pools to disk. This must be called with the namespace lock
293 spa_config_sync(void)
296 list_t files = { 0 };
297 spa_config_entry_t *entry;
299 ASSERT(MUTEX_HELD(&spa_namespace_lock));
301 list_create(&files, sizeof (spa_config_entry_t),
302 offsetof(spa_config_entry_t, sc_link));
305 * Add all known pools to the configuration list, ignoring those with
306 * alternate root paths.
309 while ((spa = spa_next(spa)) != NULL)
310 spa_config_entry_add(&files, spa);
312 while ((entry = list_head(&files)) != NULL) {
313 spa_config_entry_write(entry);
314 list_remove(&files, entry);
315 nvlist_free(entry->sc_nvl);
316 kmem_free(entry, sizeof (spa_config_entry_t));
319 spa_config_generation++;
323 * Sigh. Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
324 * and we don't want to allow the local zone to see all the pools anyway.
325 * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
326 * information for all pool visible within the zone.
329 spa_all_configs(uint64_t *generation)
334 if (*generation == spa_config_generation)
337 VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
340 mutex_enter(&spa_namespace_lock);
341 while ((spa = spa_next(spa)) != NULL) {
342 if (INGLOBALZONE(curproc) ||
343 zone_dataset_visible(spa_name(spa), NULL)) {
344 mutex_enter(&spa->spa_config_cache_lock);
345 VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
346 spa->spa_config) == 0);
347 mutex_exit(&spa->spa_config_cache_lock);
350 mutex_exit(&spa_namespace_lock);
352 *generation = spa_config_generation;
358 spa_config_set(spa_t *spa, nvlist_t *config)
360 mutex_enter(&spa->spa_config_cache_lock);
361 if (spa->spa_config != NULL)
362 nvlist_free(spa->spa_config);
363 spa->spa_config = config;
364 mutex_exit(&spa->spa_config_cache_lock);
368 * Generate the pool's configuration based on the current in-core state.
369 * We infer whether to generate a complete config or just one top-level config
370 * based on whether vd is the root vdev.
373 spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
375 nvlist_t *config, *nvroot;
376 vdev_t *rvd = spa->spa_root_vdev;
377 unsigned long hostid = 0;
379 ASSERT(spa_config_held(spa, RW_READER) ||
380 spa_config_held(spa, RW_WRITER));
386 * If txg is -1, report the current value of spa->spa_config_txg.
389 txg = spa->spa_config_txg;
391 VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
393 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
394 spa_version(spa)) == 0);
395 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
396 spa_name(spa)) == 0);
397 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
398 spa_state(spa)) == 0);
399 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
401 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
402 spa_guid(spa)) == 0);
403 (void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
405 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
408 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
409 utsname.nodename) == 0);
412 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
413 vd->vdev_top->vdev_guid) == 0);
414 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
415 vd->vdev_guid) == 0);
416 if (vd->vdev_isspare)
417 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_SPARE,
420 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
422 vd = vd->vdev_top; /* label contains top config */
425 nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE, B_FALSE);
426 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
433 * For a pool that's not currently a booting rootpool, update all disk labels,
434 * generate a fresh config based on the current in-core state, and sync the
435 * global config cache.
438 spa_config_update(spa_t *spa, int what)
440 spa_config_update_common(spa, what, FALSE);
444 * Update all disk labels, generate a fresh config based on the current
445 * in-core state, and sync the global config cache (do not sync the config
446 * cache if this is a booting rootpool).
449 spa_config_update_common(spa_t *spa, int what, boolean_t isroot)
451 vdev_t *rvd = spa->spa_root_vdev;
455 ASSERT(MUTEX_HELD(&spa_namespace_lock));
457 spa_config_enter(spa, RW_WRITER, FTAG);
458 txg = spa_last_synced_txg(spa) + 1;
459 if (what == SPA_CONFIG_UPDATE_POOL) {
460 vdev_config_dirty(rvd);
463 * If we have top-level vdevs that were added but have
464 * not yet been prepared for allocation, do that now.
465 * (It's safe now because the config cache is up to date,
466 * so it will be able to translate the new DVAs.)
467 * See comments in spa_vdev_add() for full details.
469 for (c = 0; c < rvd->vdev_children; c++) {
470 vdev_t *tvd = rvd->vdev_child[c];
471 if (tvd->vdev_ms_array == 0) {
473 vdev_config_dirty(tvd);
477 spa_config_exit(spa, FTAG);
480 * Wait for the mosconfig to be regenerated and synced.
482 txg_wait_synced(spa->spa_dsl_pool, txg);
485 * Update the global config cache to reflect the new mosconfig.
490 if (what == SPA_CONFIG_UPDATE_POOL)
491 spa_config_update_common(spa, SPA_CONFIG_UPDATE_VDEVS, isroot);