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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "@(#)vdev_file.c 1.7 07/11/27 SMI"
#include <sys/zfs_context.h>
#include <sys/spa.h>
#include <sys/vdev_file.h>
#include <sys/vdev_impl.h>
#include <sys/zio.h>
#include <sys/fs/zfs.h>
/*
* Virtual device vector for files.
*/
static int
vdev_file_open_common(vdev_t *vd)
{
vdev_file_t *vf;
vnode_t *vp;
int error;
/*
* We must have a pathname, and it must be absolute.
*/
if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
return (EINVAL);
}
vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
/*
* We always open the files from the root of the global zone, even if
* we're in a local zone. If the user has gotten to this point, the
* administrator has already decided that the pool should be available
* to local zone users, so the underlying devices should be as well.
*/
ASSERT(vd->vdev_path != NULL && vd->vdev_path[0] == '/');
error = vn_openat(vd->vdev_path + 1, UIO_SYSSPACE,
spa_mode | FOFFMAX, 0, &vp, 0, 0, rootdir, -1);
if (error) {
vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
return (error);
}
vf->vf_vnode = vp;
#ifdef _KERNEL
/*
* Make sure it's a regular file.
*/
if (vp->v_type != VREG) {
vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
return (ENODEV);
}
#endif
return (0);
}
static int
vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
{
vdev_file_t *vf;
vattr_t vattr;
int error;
if ((error = vdev_file_open_common(vd)) != 0)
return (error);
vf = vd->vdev_tsd;
/*
* Determine the physical size of the file.
*/
vattr.va_mask = AT_SIZE;
error = VOP_GETATTR(vf->vf_vnode, &vattr, 0, kcred, NULL);
if (error) {
vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
return (error);
}
*psize = vattr.va_size;
*ashift = SPA_MINBLOCKSHIFT;
return (0);
}
static void
vdev_file_close(vdev_t *vd)
{
vdev_file_t *vf = vd->vdev_tsd;
if (vf == NULL)
return;
if (vf->vf_vnode != NULL) {
(void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred, NULL);
(void) VOP_CLOSE(vf->vf_vnode, spa_mode, 1, 0, kcred, NULL);
VN_RELE(vf->vf_vnode);
}
kmem_free(vf, sizeof (vdev_file_t));
vd->vdev_tsd = NULL;
}
static int
vdev_file_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset,
enum uio_rw rw)
{
vdev_file_t *vf = vd->vdev_tsd;
ssize_t resid;
int error = 0;
if (vd == NULL || vf == NULL || vf->vf_vnode == NULL)
return (EINVAL);
ASSERT(rw == UIO_READ || rw == UIO_WRITE);
error = vn_rdwr(rw, vf->vf_vnode, data, size, offset, UIO_SYSSPACE,
0, RLIM64_INFINITY, kcred, &resid);
if (error || resid != 0)
return (EIO);
return (0);
}
/*
* Determine if the underlying device is accessible by reading and writing
* to a known location. We must be able to do this during syncing context
* and thus we cannot set the vdev state directly.
*/
static int
vdev_file_probe(vdev_t *vd)
{
vdev_t *nvd;
char *vl_boot;
uint64_t offset;
int l, error = 0, retries = 0;
if (vd == NULL)
return (EINVAL);
/* Hijack the current vdev */
nvd = vd;
/*
* Pick a random label to rewrite.
*/
l = spa_get_random(VDEV_LABELS);
ASSERT(l < VDEV_LABELS);
offset = vdev_label_offset(vd->vdev_psize, l,
offsetof(vdev_label_t, vl_boot_header));
vl_boot = kmem_alloc(VDEV_BOOT_HEADER_SIZE, KM_SLEEP);
while ((error = vdev_file_probe_io(nvd, vl_boot, VDEV_BOOT_HEADER_SIZE,
offset, UIO_READ)) != 0 && retries == 0) {
/*
* If we failed with the vdev that was passed in then
* try allocating a new one and try again.
*/
nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
if (vd->vdev_path)
nvd->vdev_path = spa_strdup(vd->vdev_path);
retries++;
error = vdev_file_open_common(nvd);
if (error)
break;
}
if ((spa_mode & FWRITE) && !error) {
error = vdev_file_probe_io(nvd, vl_boot, VDEV_BOOT_HEADER_SIZE,
offset, UIO_WRITE);
}
if (retries) {
vdev_file_close(nvd);
if (nvd->vdev_path)
spa_strfree(nvd->vdev_path);
kmem_free(nvd, sizeof (vdev_t));
}
kmem_free(vl_boot, VDEV_BOOT_HEADER_SIZE);
if (!error)
vd->vdev_is_failing = B_FALSE;
return (error);
}
static int
vdev_file_io_start(zio_t *zio)
{
vdev_t *vd = zio->io_vd;
vdev_file_t *vf = vd->vdev_tsd;
ssize_t resid;
int error;
if (zio->io_type == ZIO_TYPE_IOCTL) {
zio_vdev_io_bypass(zio);
/* XXPOLICY */
if (!vdev_readable(vd)) {
zio->io_error = ENXIO;
return (ZIO_PIPELINE_CONTINUE);
}
switch (zio->io_cmd) {
case DKIOCFLUSHWRITECACHE:
zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
kcred, NULL);
dprintf("fsync(%s) = %d\n", vdev_description(vd),
zio->io_error);
break;
default:
zio->io_error = ENOTSUP;
}
return (ZIO_PIPELINE_CONTINUE);
}
/*
* In the kernel, don't bother double-caching, but in userland,
* we want to test the vdev_cache code.
*/
#ifndef _KERNEL
if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
return (ZIO_PIPELINE_STOP);
#endif
if ((zio = vdev_queue_io(zio)) == NULL)
return (ZIO_PIPELINE_STOP);
/* XXPOLICY */
if (zio->io_type == ZIO_TYPE_WRITE)
error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
else
error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error;
if (error) {
zio->io_error = error;
zio_interrupt(zio);
return (ZIO_PIPELINE_STOP);
}
zio->io_error = vn_rdwr(zio->io_type == ZIO_TYPE_READ ?
UIO_READ : UIO_WRITE, vf->vf_vnode, zio->io_data,
zio->io_size, zio->io_offset, UIO_SYSSPACE,
0, RLIM64_INFINITY, kcred, &resid);
if (resid != 0 && zio->io_error == 0)
zio->io_error = ENOSPC;
zio_interrupt(zio);
return (ZIO_PIPELINE_STOP);
}
static int
vdev_file_io_done(zio_t *zio)
{
vdev_t *vd = zio->io_vd;
if (zio_injection_enabled && zio->io_error == 0)
zio->io_error = zio_handle_device_injection(vd, EIO);
/*
* If an error has been encountered then attempt to probe the device
* to determine if it's still accessible.
*/
if (zio->io_error == EIO && vdev_probe(vd) != 0)
vd->vdev_is_failing = B_TRUE;
vdev_queue_io_done(zio);
#ifndef _KERNEL
if (zio->io_type == ZIO_TYPE_WRITE)
vdev_cache_write(zio);
#endif
return (ZIO_PIPELINE_CONTINUE);
}
vdev_ops_t vdev_file_ops = {
vdev_file_open,
vdev_file_close,
vdev_file_probe,
vdev_default_asize,
vdev_file_io_start,
vdev_file_io_done,
NULL,
VDEV_TYPE_FILE, /* name of this vdev type */
B_TRUE /* leaf vdev */
};
/*
* From userland we access disks just like files.
*/
#ifndef _KERNEL
vdev_ops_t vdev_disk_ops = {
vdev_file_open,
vdev_file_close,
vdev_file_probe,
vdev_default_asize,
vdev_file_io_start,
vdev_file_io_done,
NULL,
VDEV_TYPE_DISK, /* name of this vdev type */
B_TRUE /* leaf vdev */
};
#endif
|