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