Home | History | Annotate | Download | only in zfs
      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 /*
     23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #pragma ident	"@(#)spa_config.c	1.14	07/11/09 SMI"
     28 
     29 #include <sys/spa.h>
     30 #include <sys/spa_impl.h>
     31 #include <sys/nvpair.h>
     32 #include <sys/uio.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>
     39 #ifdef _KERNEL
     40 #include <sys/kobj.h>
     41 #endif
     42 
     43 /*
     44  * Pool configuration repository.
     45  *
     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.
     51  *
     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.
     58  */
     59 
     60 static uint64_t spa_config_generation = 1;
     61 
     62 /*
     63  * This can be overridden in userland to preserve an alternate namespace for
     64  * userland pools when doing testing.
     65  */
     66 const char *spa_config_dir = ZPOOL_CACHE_DIR;
     67 
     68 /*
     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.
     72  */
     73 void
     74 spa_config_load(void)
     75 {
     76 	void *buf = NULL;
     77 	nvlist_t *nvlist, *child;
     78 	nvpair_t *nvpair;
     79 	spa_t *spa;
     80 	char pathname[128];
     81 	struct _buf *file;
     82 	uint64_t fsize;
     83 
     84 	/*
     85 	 * Open the configuration file.
     86 	 */
     87 	(void) snprintf(pathname, sizeof (pathname), "%s%s/%s",
     88 	    (rootdir != NULL) ? "./" : "", spa_config_dir, ZPOOL_CACHE_FILE);
     89 
     90 	file = kobj_open_file(pathname);
     91 	if (file == (struct _buf *)-1)
     92 		return;
     93 
     94 	if (kobj_get_filesize(file, &fsize) != 0)
     95 		goto out;
     96 
     97 	buf = kmem_alloc(fsize, KM_SLEEP);
     98 
     99 	/*
    100 	 * Read the nvlist from the file.
    101 	 */
    102 	if (kobj_read_file(file, buf, fsize, 0) < 0)
    103 		goto out;
    104 
    105 	/*
    106 	 * Unpack the nvlist.
    107 	 */
    108 	if (nvlist_unpack(buf, fsize, &nvlist, KM_SLEEP) != 0)
    109 		goto out;
    110 
    111 	/*
    112 	 * Iterate over all elements in the nvlist, creating a new spa_t for
    113 	 * each one with the specified configuration.
    114 	 */
    115 	mutex_enter(&spa_namespace_lock);
    116 	nvpair = NULL;
    117 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
    118 
    119 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
    120 			continue;
    121 
    122 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
    123 
    124 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
    125 			continue;
    126 		spa = spa_add(nvpair_name(nvpair), NULL);
    127 
    128 		/*
    129 		 * We blindly duplicate the configuration here.  If it's
    130 		 * invalid, we will catch it when the pool is first opened.
    131 		 */
    132 		VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0);
    133 	}
    134 	mutex_exit(&spa_namespace_lock);
    135 
    136 	nvlist_free(nvlist);
    137 
    138 out:
    139 	if (buf != NULL)
    140 		kmem_free(buf, fsize);
    141 
    142 	kobj_close_file(file);
    143 }
    144 
    145 /*
    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().
    150  */
    151 void
    152 spa_config_check(const char *dir, const char *file)
    153 {
    154 	size_t count = 0;
    155 	char pathname[128];
    156 	spa_t *spa;
    157 
    158 	if (dir != NULL && strcmp(dir, "none") == 0)
    159 		return;
    160 
    161 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
    162 	spa = NULL;
    163 	while ((spa = spa_next(spa)) != NULL) {
    164 		if (dir == NULL) {
    165 			if (spa->spa_config_dir == NULL)
    166 				count++;
    167 		} else {
    168 			if (spa->spa_config_dir &&
    169 			    strcmp(spa->spa_config_dir, dir) == 0 &&
    170 			    strcmp(spa->spa_config_file, file) == 0)
    171 				count++;
    172 		}
    173 	}
    174 
    175 	if (count == 1) {
    176 		if (dir == NULL) {
    177 			dir = spa_config_dir;
    178 			file = ZPOOL_CACHE_FILE;
    179 		}
    180 
    181 		(void) snprintf(pathname, sizeof (pathname),
    182 		    "%s/%s", dir, file);
    183 		(void) vn_remove(pathname, UIO_SYSSPACE, RMFILE);
    184 	}
    185 }
    186 
    187 typedef struct spa_config_entry {
    188 	list_t		sc_link;
    189 	const char	*sc_dir;
    190 	const char	*sc_file;
    191 	nvlist_t	*sc_nvl;
    192 } spa_config_entry_t;
    193 
    194 static void
    195 spa_config_entry_add(list_t *listp, spa_t *spa)
    196 {
    197 	spa_config_entry_t *entry;
    198 	const char *dir, *file;
    199 
    200 	mutex_enter(&spa->spa_config_cache_lock);
    201 	if (!spa->spa_config || !spa->spa_name) {
    202 		mutex_exit(&spa->spa_config_cache_lock);
    203 		return;
    204 	}
    205 
    206 	if (spa->spa_config_dir) {
    207 		dir = spa->spa_config_dir;
    208 		file = spa->spa_config_file;
    209 	} else {
    210 		dir = spa_config_dir;
    211 		file = ZPOOL_CACHE_FILE;
    212 	}
    213 
    214 	if (strcmp(dir, "none") == 0) {
    215 		mutex_exit(&spa->spa_config_cache_lock);
    216 		return;
    217 	}
    218 
    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)
    223 			break;
    224 	}
    225 
    226 	if (entry == NULL) {
    227 		entry = kmem_alloc(sizeof (spa_config_entry_t), KM_SLEEP);
    228 		entry->sc_dir = dir;
    229 		entry->sc_file = file;
    230 		VERIFY(nvlist_alloc(&entry->sc_nvl, NV_UNIQUE_NAME,
    231 		    KM_SLEEP) == 0);
    232 		list_insert_tail(listp, entry);
    233 	}
    234 
    235 	VERIFY(nvlist_add_nvlist(entry->sc_nvl, spa->spa_name,
    236 	    spa->spa_config) == 0);
    237 	mutex_exit(&spa->spa_config_cache_lock);
    238 }
    239 
    240 static void
    241 spa_config_entry_write(spa_config_entry_t *entry)
    242 {
    243 	nvlist_t *config = entry->sc_nvl;
    244 	size_t buflen;
    245 	char *buf;
    246 	vnode_t *vp;
    247 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
    248 	char pathname[128];
    249 	char pathname2[128];
    250 
    251 	/*
    252 	 * Pack the configuration into a buffer.
    253 	 */
    254 	VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0);
    255 
    256 	buf = kmem_alloc(buflen, KM_SLEEP);
    257 
    258 	VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR,
    259 	    KM_SLEEP) == 0);
    260 
    261 	/*
    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.
    265 	 */
    266 	(void) snprintf(pathname, sizeof (pathname), "%s/.%s", entry->sc_dir,
    267 	    entry->sc_file);
    268 
    269 	if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0)
    270 		goto out;
    271 
    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);
    278 	}
    279 
    280 	(void) VOP_CLOSE(vp, oflags, 1, 0, kcred, NULL);
    281 	VN_RELE(vp);
    282 
    283 out:
    284 	(void) vn_remove(pathname, UIO_SYSSPACE, RMFILE);
    285 	kmem_free(buf, buflen);
    286 }
    287 
    288 /*
    289  * Synchronize all pools to disk.  This must be called with the namespace lock
    290  * held.
    291  */
    292 void
    293 spa_config_sync(void)
    294 {
    295 	spa_t *spa = NULL;
    296 	list_t files = { 0 };
    297 	spa_config_entry_t *entry;
    298 
    299 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
    300 
    301 	list_create(&files, sizeof (spa_config_entry_t),
    302 	    offsetof(spa_config_entry_t, sc_link));
    303 
    304 	/*
    305 	 * Add all known pools to the configuration list, ignoring those with
    306 	 * alternate root paths.
    307 	 */
    308 	spa = NULL;
    309 	while ((spa = spa_next(spa)) != NULL)
    310 		spa_config_entry_add(&files, spa);
    311 
    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));
    317 	}
    318 
    319 	spa_config_generation++;
    320 }
    321 
    322 /*
    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.
    327  */
    328 nvlist_t *
    329 spa_all_configs(uint64_t *generation)
    330 {
    331 	nvlist_t *pools;
    332 	spa_t *spa;
    333 
    334 	if (*generation == spa_config_generation)
    335 		return (NULL);
    336 
    337 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
    338 
    339 	spa = NULL;
    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);
    348 		}
    349 	}
    350 	mutex_exit(&spa_namespace_lock);
    351 
    352 	*generation = spa_config_generation;
    353 
    354 	return (pools);
    355 }
    356 
    357 void
    358 spa_config_set(spa_t *spa, nvlist_t *config)
    359 {
    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);
    365 }
    366 
    367 /*
    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.
    371  */
    372 nvlist_t *
    373 spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
    374 {
    375 	nvlist_t *config, *nvroot;
    376 	vdev_t *rvd = spa->spa_root_vdev;
    377 	unsigned long hostid = 0;
    378 
    379 	ASSERT(spa_config_held(spa, RW_READER) ||
    380 	    spa_config_held(spa, RW_WRITER));
    381 
    382 	if (vd == NULL)
    383 		vd = rvd;
    384 
    385 	/*
    386 	 * If txg is -1, report the current value of spa->spa_config_txg.
    387 	 */
    388 	if (txg == -1ULL)
    389 		txg = spa->spa_config_txg;
    390 
    391 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
    392 
    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,
    400 	    txg) == 0);
    401 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
    402 	    spa_guid(spa)) == 0);
    403 	(void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
    404 	if (hostid != 0) {
    405 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_HOSTID,
    406 		    hostid) == 0);
    407 	}
    408 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_HOSTNAME,
    409 	    utsname.nodename) == 0);
    410 
    411 	if (vd != rvd) {
    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,
    418 			    1ULL) == 0);
    419 		if (vd->vdev_islog)
    420 			VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_IS_LOG,
    421 			    1ULL) == 0);
    422 		vd = vd->vdev_top;		/* label contains top config */
    423 	}
    424 
    425 	nvroot = vdev_config_generate(spa, vd, getstats, B_FALSE, B_FALSE);
    426 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
    427 	nvlist_free(nvroot);
    428 
    429 	return (config);
    430 }
    431 
    432 /*
    433  * Update all disk labels, generate a fresh config based on the current
    434  * in-core state, and sync the global config cache.
    435  */
    436 void
    437 spa_config_update(spa_t *spa, int what)
    438 {
    439 	vdev_t *rvd = spa->spa_root_vdev;
    440 	uint64_t txg;
    441 	int c;
    442 
    443 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
    444 
    445 	spa_config_enter(spa, RW_WRITER, FTAG);
    446 	txg = spa_last_synced_txg(spa) + 1;
    447 	if (what == SPA_CONFIG_UPDATE_POOL) {
    448 		vdev_config_dirty(rvd);
    449 	} else {
    450 		/*
    451 		 * If we have top-level vdevs that were added but have
    452 		 * not yet been prepared for allocation, do that now.
    453 		 * (It's safe now because the config cache is up to date,
    454 		 * so it will be able to translate the new DVAs.)
    455 		 * See comments in spa_vdev_add() for full details.
    456 		 */
    457 		for (c = 0; c < rvd->vdev_children; c++) {
    458 			vdev_t *tvd = rvd->vdev_child[c];
    459 			if (tvd->vdev_ms_array == 0) {
    460 				vdev_init(tvd, txg);
    461 				vdev_config_dirty(tvd);
    462 			}
    463 		}
    464 	}
    465 	spa_config_exit(spa, FTAG);
    466 
    467 	/*
    468 	 * Wait for the mosconfig to be regenerated and synced.
    469 	 */
    470 	txg_wait_synced(spa->spa_dsl_pool, txg);
    471 
    472 	/*
    473 	 * Update the global config cache to reflect the new mosconfig.
    474 	 */
    475 	spa_config_sync();
    476 
    477 	if (what == SPA_CONFIG_UPDATE_POOL)
    478 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
    479 }
    480