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