xref: /illumos-gate/usr/src/uts/common/fs/zfs/spa_config.c (revision 144dfaa9a648eea321858b34d4941d2268130176)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/spa.h>
29 #include <sys/spa_impl.h>
30 #include <sys/nvpair.h>
31 #include <sys/uio.h>
32 #include <sys/fs/zfs.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/zfs_ioctl.h>
35 #ifdef _KERNEL
36 #include <sys/kobj.h>
37 #endif
38 
39 /*
40  * Pool configuration repository.
41  *
42  * The configuration for all pools, in addition to being stored on disk, is
43  * stored in /etc/zfs/zpool.cache as a packed nvlist.  The kernel maintains
44  * this list as pools are created, destroyed, or modified.
45  *
46  * We have a single nvlist which holds all the configuration information.  When
47  * the module loads, we read this information from the cache and populate the
48  * SPA namespace.  This namespace is maintained independently in spa.c.
49  * Whenever the namespace is modified, or the configuration of a pool is
50  * changed, we call spa_config_sync(), which walks through all the active pools
51  * and writes the configuration to disk.
52  */
53 
54 static uint64_t spa_config_generation = 1;
55 
56 /*
57  * This can be overridden in userland to preserve an alternate namespace for
58  * userland pools when doing testing.
59  */
60 const char *spa_config_dir = ZPOOL_CACHE_DIR;
61 
62 /*
63  * Called when the module is first loaded, this routine loads the configuration
64  * file into the SPA namespace.  It does not actually open or load the pools; it
65  * only populates the namespace.
66  */
67 void
68 spa_config_load(void)
69 {
70 	void *buf = NULL;
71 	nvlist_t *nvlist, *child;
72 	nvpair_t *nvpair;
73 	spa_t *spa;
74 	char pathname[128];
75 	struct _buf *file;
76 	struct bootstat bst;
77 
78 	/*
79 	 * Open the configuration file.
80 	 */
81 	(void) snprintf(pathname, sizeof (pathname), "%s%s/%s",
82 	    (rootdir != NULL) ? "./" : "", spa_config_dir, ZPOOL_CACHE_FILE);
83 
84 	file = kobj_open_file(pathname);
85 	if (file == (struct _buf *)-1)
86 		return;
87 
88 	if (kobj_fstat(file->_fd, &bst) != 0)
89 		goto out;
90 
91 	buf = kmem_alloc(bst.st_size, KM_SLEEP);
92 
93 	/*
94 	 * Read the nvlist from the file.
95 	 */
96 	if (kobj_read_file(file, buf, bst.st_size, 0) < 0)
97 		goto out;
98 
99 	/*
100 	 * Unpack the nvlist.
101 	 */
102 	if (nvlist_unpack(buf, bst.st_size, &nvlist, KM_SLEEP) != 0)
103 		goto out;
104 
105 	/*
106 	 * Iterate over all elements in the nvlist, creating a new spa_t for
107 	 * each one with the specified configuration.
108 	 */
109 	mutex_enter(&spa_namespace_lock);
110 	nvpair = NULL;
111 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
112 
113 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
114 			continue;
115 
116 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
117 
118 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
119 			continue;
120 		spa = spa_add(nvpair_name(nvpair), NULL);
121 
122 		/*
123 		 * We blindly duplicate the configuration here.  If it's
124 		 * invalid, we will catch it when the pool is first opened.
125 		 */
126 		VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0);
127 	}
128 	mutex_exit(&spa_namespace_lock);
129 
130 	nvlist_free(nvlist);
131 
132 out:
133 	if (buf != NULL)
134 		kmem_free(buf, bst.st_size);
135 
136 	kobj_close_file(file);
137 }
138 
139 /*
140  * Synchronize all pools to disk.  This must be called with the namespace lock
141  * held.
142  */
143 void
144 spa_config_sync(void)
145 {
146 	spa_t *spa = NULL;
147 	nvlist_t *config;
148 	size_t buflen;
149 	char *buf;
150 	vnode_t *vp;
151 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
152 	char pathname[128];
153 	char pathname2[128];
154 
155 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
156 
157 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
158 
159 	/*
160 	 * Add all known pools to the configuration list, ignoring those with
161 	 * alternate root paths.
162 	 */
163 	spa = NULL;
164 	while ((spa = spa_next(spa)) != NULL) {
165 		mutex_enter(&spa->spa_config_cache_lock);
166 		if (spa->spa_config && spa->spa_name && spa->spa_root == NULL)
167 			VERIFY(nvlist_add_nvlist(config, spa->spa_name,
168 			    spa->spa_config) == 0);
169 		mutex_exit(&spa->spa_config_cache_lock);
170 	}
171 
172 	/*
173 	 * Pack the configuration into a buffer.
174 	 */
175 	VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0);
176 
177 	buf = kmem_alloc(buflen, KM_SLEEP);
178 
179 	VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR,
180 	    KM_SLEEP) == 0);
181 
182 	/*
183 	 * Write the configuration to disk.  We need to do the traditional
184 	 * 'write to temporary file, sync, move over original' to make sure we
185 	 * always have a consistent view of the data.
186 	 */
187 	(void) snprintf(pathname, sizeof (pathname), "%s/%s", spa_config_dir,
188 	    ZPOOL_CACHE_TMP);
189 
190 	if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0)
191 		goto out;
192 
193 	if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
194 	    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
195 	    VOP_FSYNC(vp, FSYNC, kcred) == 0) {
196 		(void) snprintf(pathname2, sizeof (pathname2), "%s/%s",
197 		    spa_config_dir, ZPOOL_CACHE_FILE);
198 		(void) vn_rename(pathname, pathname2, UIO_SYSSPACE);
199 	}
200 
201 	(void) VOP_CLOSE(vp, oflags, 1, 0, kcred);
202 	VN_RELE(vp);
203 
204 out:
205 	(void) vn_remove(pathname, UIO_SYSSPACE, RMFILE);
206 	spa_config_generation++;
207 
208 	kmem_free(buf, buflen);
209 	nvlist_free(config);
210 }
211 
212 /*
213  * Sigh.  Inside a local zone, we don't have access to /etc/zfs/zpool.cache,
214  * and we don't want to allow the local zone to see all the pools anyway.
215  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
216  * information for all pool visible within the zone.
217  */
218 nvlist_t *
219 spa_all_configs(uint64_t *generation)
220 {
221 	nvlist_t *pools;
222 	spa_t *spa;
223 
224 	if (*generation == spa_config_generation)
225 		return (NULL);
226 
227 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, KM_SLEEP) == 0);
228 
229 	spa = NULL;
230 	mutex_enter(&spa_namespace_lock);
231 	while ((spa = spa_next(spa)) != NULL) {
232 		if (INGLOBALZONE(curproc) ||
233 		    zone_dataset_visible(spa_name(spa), NULL)) {
234 			mutex_enter(&spa->spa_config_cache_lock);
235 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
236 			    spa->spa_config) == 0);
237 			mutex_exit(&spa->spa_config_cache_lock);
238 		}
239 	}
240 	mutex_exit(&spa_namespace_lock);
241 
242 	*generation = spa_config_generation;
243 
244 	return (pools);
245 }
246 
247 void
248 spa_config_set(spa_t *spa, nvlist_t *config)
249 {
250 	mutex_enter(&spa->spa_config_cache_lock);
251 	if (spa->spa_config != NULL)
252 		nvlist_free(spa->spa_config);
253 	spa->spa_config = config;
254 	mutex_exit(&spa->spa_config_cache_lock);
255 }
256 
257 /*
258  * Generate the pool's configuration based on the current in-core state.
259  * We infer whether to generate a complete config or just one top-level config
260  * based on whether vd is the root vdev.
261  */
262 nvlist_t *
263 spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
264 {
265 	nvlist_t *config, *nvroot;
266 	vdev_t *rvd = spa->spa_root_vdev;
267 
268 	ASSERT(spa_config_held(spa, RW_READER));
269 
270 	if (vd == NULL)
271 		vd = rvd;
272 
273 	/*
274 	 * If txg is -1, report the current value of spa->spa_config_txg.
275 	 */
276 	if (txg == -1ULL)
277 		txg = spa->spa_config_txg;
278 
279 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, KM_SLEEP) == 0);
280 
281 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
282 	    spa->spa_uberblock.ub_version) == 0);
283 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
284 	    spa_name(spa)) == 0);
285 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
286 	    spa_state(spa)) == 0);
287 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
288 	    txg) == 0);
289 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
290 	    spa_guid(spa)) == 0);
291 
292 	if (vd != rvd) {
293 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
294 		    vd->vdev_top->vdev_guid) == 0);
295 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
296 		    vd->vdev_guid) == 0);
297 		vd = vd->vdev_top;		/* label contains top config */
298 	}
299 
300 	nvroot = vdev_config_generate(vd, getstats);
301 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
302 	nvlist_free(nvroot);
303 
304 	return (config);
305 }
306 
307 /*
308  * Update all disk labels, generate a fresh config based on the current
309  * in-core state, and sync the global config cache.
310  */
311 void
312 spa_config_update(spa_t *spa, int what)
313 {
314 	vdev_t *rvd = spa->spa_root_vdev;
315 	uint64_t txg;
316 	int c;
317 
318 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
319 
320 	spa_config_enter(spa, RW_WRITER, FTAG);
321 	txg = spa_last_synced_txg(spa) + 1;
322 	if (what == SPA_CONFIG_UPDATE_POOL) {
323 		vdev_config_dirty(rvd);
324 	} else {
325 		/*
326 		 * If we have top-level vdevs that were added but have
327 		 * not yet been prepared for allocation, do that now.
328 		 * (It's safe now because the config cache is up to date,
329 		 * so it will be able to translate the new DVAs.)
330 		 * See comments in spa_vdev_add() for full details.
331 		 */
332 		for (c = 0; c < rvd->vdev_children; c++) {
333 			vdev_t *tvd = rvd->vdev_child[c];
334 			if (tvd->vdev_ms_array == 0) {
335 				vdev_init(tvd, txg);
336 				vdev_config_dirty(tvd);
337 			}
338 		}
339 	}
340 	spa_config_exit(spa, FTAG);
341 
342 	/*
343 	 * Wait for the mosconfig to be regenerated and synced.
344 	 */
345 	txg_wait_synced(spa->spa_dsl_pool, txg);
346 
347 	/*
348 	 * Update the global config cache to reflect the new mosconfig.
349 	 */
350 	spa_config_sync();
351 
352 	if (what == SPA_CONFIG_UPDATE_POOL)
353 		spa_config_update(spa, SPA_CONFIG_UPDATE_VDEVS);
354 }
355