xref: /titanic_44/usr/src/uts/common/fs/zfs/spa_config.c (revision f56c1286e5113aa46bd6e723da14d30c123153f2)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 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 
37 /*
38  * Pool configuration repository.
39  *
40  * The configuration for all pools, in addition to being stored on disk, is
41  * stored in /kernel/drv/zpool.cache as a packed nvlist.  The kernel maintains
42  * this list as pools are created, destroyed, or modified.
43  *
44  * We have a single nvlist which holds all the configuration information.  When
45  * the module loads, we read this information from the cache and populate the
46  * SPA namespace.  This namespace is maintained independently in spa.c.
47  * Whenever the namespace is modified, or the configuration of a pool is
48  * changed, we call spa_config_sync(), which walks through all the active pools
49  * and writes the configuration to disk.
50  */
51 
52 static uint64_t spa_config_generation = 1;
53 
54 /*
55  * This can be overridden in userland to preserve an alternate namespace for
56  * userland pools when doing testing.
57  */
58 const char *spa_config_dir = ZPOOL_CACHE_DIR;
59 
60 /*
61  * Called when the module is first loaded, this routine loads the configuration
62  * file into the SPA namespace.  It does not actually open or load the pools; it
63  * only populates the namespace.
64  */
65 void
66 spa_config_load(void)
67 {
68 	vnode_t *vp;
69 	void *buf = NULL;
70 	vattr_t vattr;
71 	ssize_t resid;
72 	nvlist_t *nvlist, *child;
73 	nvpair_t *nvpair;
74 	spa_t *spa;
75 	char pathname[128];
76 
77 	/*
78 	 * Open the configuration file.
79 	 */
80 	(void) snprintf(pathname, sizeof (pathname), "./%s/%s", spa_config_dir,
81 	    ZPOOL_CACHE_FILE);
82 	if (vn_openat(pathname, UIO_SYSSPACE, FREAD | FOFFMAX, 0, &vp, 0, 0,
83 	    rootdir) != 0)
84 		return;
85 
86 	/*
87 	 * Read the nvlist from the file.
88 	 */
89 	if (VOP_GETATTR(vp, &vattr, 0, kcred) != 0)
90 		goto out;
91 
92 	buf = kmem_alloc(vattr.va_size, KM_SLEEP);
93 
94 	if (vn_rdwr(UIO_READ, vp, buf, vattr.va_size, 0, UIO_SYSSPACE,
95 	    0, RLIM64_INFINITY, kcred, &resid) != 0)
96 		goto out;
97 
98 	if (resid != 0)
99 		goto out;
100 
101 	/*
102 	 * Unpack the nvlist.
103 	 */
104 	if (nvlist_unpack(buf, vattr.va_size, &nvlist, KM_SLEEP) != 0)
105 		goto out;
106 
107 	/*
108 	 * Iterate over all elements in the nvlist, creating a new spa_t for
109 	 * each one with the specified configuration.
110 	 */
111 	mutex_enter(&spa_namespace_lock);
112 	nvpair = NULL;
113 	while ((nvpair = nvlist_next_nvpair(nvlist, nvpair)) != NULL) {
114 
115 		if (nvpair_type(nvpair) != DATA_TYPE_NVLIST)
116 			continue;
117 
118 		VERIFY(nvpair_value_nvlist(nvpair, &child) == 0);
119 
120 		if (spa_lookup(nvpair_name(nvpair)) != NULL)
121 			continue;
122 		spa = spa_add(nvpair_name(nvpair));
123 
124 		/*
125 		 * We blindly duplicate the configuration here.  If it's
126 		 * invalid, we will catch it when the pool is first opened.
127 		 */
128 		VERIFY(nvlist_dup(child, &spa->spa_config, 0) == 0);
129 	}
130 	mutex_exit(&spa_namespace_lock);
131 
132 	nvlist_free(nvlist);
133 
134 out:
135 	if (buf != NULL)
136 		kmem_free(buf, vattr.va_size);
137 
138 	(void) VOP_CLOSE(vp, FREAD | FOFFMAX, 1, 0, kcred);
139 	VN_RELE(vp);
140 }
141 
142 /*
143  * Synchronize all pools to disk.  This must be called with the namespace lock
144  * held.
145  */
146 void
147 spa_config_sync(void)
148 {
149 	spa_t *spa = NULL;
150 	nvlist_t *config;
151 	size_t buflen;
152 	char *buf;
153 	vnode_t *vp;
154 	int oflags = FWRITE | FTRUNC | FCREAT | FOFFMAX;
155 	char pathname[128];
156 	char pathname2[128];
157 
158 	ASSERT(MUTEX_HELD(&spa_namespace_lock));
159 
160 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
161 
162 	/*
163 	 * Add all known pools to the configuration list, ignoring those with
164 	 * alternate root paths.
165 	 */
166 	spa = NULL;
167 	while ((spa = spa_next(spa)) != NULL) {
168 		mutex_enter(&spa->spa_config_cache_lock);
169 		if (spa->spa_config && spa->spa_name && spa->spa_root == NULL)
170 			VERIFY(nvlist_add_nvlist(config, spa->spa_name,
171 			    spa->spa_config) == 0);
172 		mutex_exit(&spa->spa_config_cache_lock);
173 	}
174 
175 	/*
176 	 * Pack the configuration into a buffer.
177 	 */
178 	VERIFY(nvlist_size(config, &buflen, NV_ENCODE_XDR) == 0);
179 
180 	buf = kmem_alloc(buflen, KM_SLEEP);
181 
182 	VERIFY(nvlist_pack(config, &buf, &buflen, NV_ENCODE_XDR, 0) == 0);
183 
184 	/*
185 	 * Write the configuration to disk.  We need to do the traditional
186 	 * 'write to temporary file, sync, move over original' to make sure we
187 	 * always have a consistent view of the data.
188 	 */
189 	(void) snprintf(pathname, sizeof (pathname), "%s/%s", spa_config_dir,
190 	    ZPOOL_CACHE_TMP);
191 
192 	if (vn_open(pathname, UIO_SYSSPACE, oflags, 0644, &vp, CRCREAT, 0) != 0)
193 		goto out;
194 
195 	if (vn_rdwr(UIO_WRITE, vp, buf, buflen, 0, UIO_SYSSPACE,
196 	    0, RLIM64_INFINITY, kcred, NULL) == 0 &&
197 	    VOP_FSYNC(vp, FSYNC, kcred) == 0) {
198 		(void) snprintf(pathname2, sizeof (pathname2), "%s/%s",
199 		    spa_config_dir, ZPOOL_CACHE_FILE);
200 		(void) vn_rename(pathname, pathname2, UIO_SYSSPACE);
201 	}
202 
203 	(void) VOP_CLOSE(vp, oflags, 1, 0, kcred);
204 	VN_RELE(vp);
205 
206 out:
207 	(void) vn_remove(pathname, UIO_SYSSPACE, RMFILE);
208 	spa_config_generation++;
209 
210 	kmem_free(buf, buflen);
211 	nvlist_free(config);
212 }
213 
214 /*
215  * Sigh.  Inside a local zone, we don't have access to /kernel/drv/zpool.cache,
216  * and we don't want to allow the local zone to see all the pools anyway.
217  * So we have to invent the ZFS_IOC_CONFIG ioctl to grab the configuration
218  * information for all pool visible within the zone.
219  */
220 nvlist_t *
221 spa_all_configs(uint64_t *generation)
222 {
223 	nvlist_t *pools;
224 	spa_t *spa;
225 
226 	if (*generation == spa_config_generation)
227 		return (NULL);
228 
229 	VERIFY(nvlist_alloc(&pools, NV_UNIQUE_NAME, 0) == 0);
230 
231 	spa = NULL;
232 	mutex_enter(&spa_namespace_lock);
233 	while ((spa = spa_next(spa)) != NULL) {
234 		if (INGLOBALZONE(curproc) ||
235 		    zone_dataset_visible(spa_name(spa), NULL)) {
236 			mutex_enter(&spa->spa_config_cache_lock);
237 			VERIFY(nvlist_add_nvlist(pools, spa_name(spa),
238 			    spa->spa_config) == 0);
239 			mutex_exit(&spa->spa_config_cache_lock);
240 		}
241 	}
242 	mutex_exit(&spa_namespace_lock);
243 
244 	*generation = spa_config_generation;
245 
246 	return (pools);
247 }
248 
249 void
250 spa_config_set(spa_t *spa, nvlist_t *config)
251 {
252 	mutex_enter(&spa->spa_config_cache_lock);
253 	if (spa->spa_config != NULL)
254 		nvlist_free(spa->spa_config);
255 	spa->spa_config = config;
256 	mutex_exit(&spa->spa_config_cache_lock);
257 }
258 
259 /*
260  * Generate the pool's configuration based on the current in-core state.
261  * We infer whether to generate a complete config or just one top-level config
262  * based on whether vd is the root vdev.
263  */
264 nvlist_t *
265 spa_config_generate(spa_t *spa, vdev_t *vd, uint64_t txg, int getstats)
266 {
267 	nvlist_t *config, *nvroot;
268 	vdev_t *rvd = spa->spa_root_vdev;
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 	 * If txg is any other non-zero value, update spa->spa_config_txg.
276 	 */
277 	if (txg == -1ULL)
278 		txg = spa->spa_config_txg;
279 	else if (txg != 0 && vd == rvd)
280 		spa->spa_config_txg = txg;
281 
282 	VERIFY(nvlist_alloc(&config, NV_UNIQUE_NAME, 0) == 0);
283 
284 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
285 	    UBERBLOCK_VERSION) == 0);
286 	VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
287 	    spa_name(spa)) == 0);
288 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
289 	    spa_state(spa)) == 0);
290 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
291 	    txg) == 0);
292 	VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
293 	    spa_guid(spa)) == 0);
294 
295 	if (vd != rvd) {
296 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TOP_GUID,
297 		    vd->vdev_top->vdev_guid) == 0);
298 		VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_GUID,
299 		    vd->vdev_guid) == 0);
300 		vd = vd->vdev_top;		/* label contains top config */
301 	}
302 
303 	nvroot = vdev_config_generate(vd, getstats);
304 	VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
305 	nvlist_free(nvroot);
306 
307 	return (config);
308 }
309