]>
Commit | Line | Data |
---|---|---|
60101509 BB |
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 (C) 2008-2010 Lawrence Livermore National Security, LLC. | |
23 | * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). | |
24 | * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>. | |
25 | * LLNL-CODE-403049. | |
cae5b340 | 26 | * Copyright (c) 2012, 2015 by Delphix. All rights reserved. |
60101509 BB |
27 | */ |
28 | ||
29 | #include <sys/zfs_context.h> | |
a07c8b41 | 30 | #include <sys/spa_impl.h> |
60101509 BB |
31 | #include <sys/vdev_disk.h> |
32 | #include <sys/vdev_impl.h> | |
cae5b340 | 33 | #include <sys/abd.h> |
60101509 BB |
34 | #include <sys/fs/zfs.h> |
35 | #include <sys/zio.h> | |
36 | #include <sys/sunldi.h> | |
a07c8b41 | 37 | #include <linux/mod_compat.h> |
60101509 | 38 | |
6839eed2 | 39 | char *zfs_vdev_scheduler = VDEV_SCHEDULER; |
8128bd89 | 40 | static void *zfs_vdev_holder = VDEV_HOLDER; |
6839eed2 | 41 | |
60101509 BB |
42 | /* |
43 | * Virtual device vector for disks. | |
44 | */ | |
45 | typedef struct dio_request { | |
60101509 | 46 | zio_t *dr_zio; /* Parent ZIO */ |
e10b0808 | 47 | atomic_t dr_ref; /* References */ |
60101509 BB |
48 | int dr_error; /* Bio error */ |
49 | int dr_bio_count; /* Count of bio's */ | |
a08ee875 | 50 | struct bio *dr_bio[0]; /* Attached bio's */ |
60101509 BB |
51 | } dio_request_t; |
52 | ||
53 | ||
54 | #ifdef HAVE_OPEN_BDEV_EXCLUSIVE | |
55 | static fmode_t | |
56 | vdev_bdev_mode(int smode) | |
57 | { | |
58 | fmode_t mode = 0; | |
59 | ||
60 | ASSERT3S(smode & (FREAD | FWRITE), !=, 0); | |
61 | ||
62 | if (smode & FREAD) | |
63 | mode |= FMODE_READ; | |
64 | ||
65 | if (smode & FWRITE) | |
66 | mode |= FMODE_WRITE; | |
67 | ||
a08ee875 | 68 | return (mode); |
60101509 BB |
69 | } |
70 | #else | |
71 | static int | |
72 | vdev_bdev_mode(int smode) | |
73 | { | |
74 | int mode = 0; | |
75 | ||
76 | ASSERT3S(smode & (FREAD | FWRITE), !=, 0); | |
77 | ||
78 | if ((smode & FREAD) && !(smode & FWRITE)) | |
79 | mode = MS_RDONLY; | |
80 | ||
a08ee875 | 81 | return (mode); |
60101509 BB |
82 | } |
83 | #endif /* HAVE_OPEN_BDEV_EXCLUSIVE */ | |
84 | ||
85 | static uint64_t | |
86 | bdev_capacity(struct block_device *bdev) | |
87 | { | |
88 | struct hd_struct *part = bdev->bd_part; | |
89 | ||
90 | /* The partition capacity referenced by the block device */ | |
91 | if (part) | |
f74fae8b | 92 | return (part->nr_sects << 9); |
60101509 BB |
93 | |
94 | /* Otherwise assume the full device capacity */ | |
f74fae8b | 95 | return (get_capacity(bdev->bd_disk) << 9); |
60101509 BB |
96 | } |
97 | ||
d148e951 BB |
98 | static void |
99 | vdev_disk_error(zio_t *zio) | |
100 | { | |
101 | #ifdef ZFS_DEBUG | |
a69052be | 102 | printk("ZFS: zio error=%d type=%d offset=%llu size=%llu " |
cae5b340 | 103 | "flags=%x\n", zio->io_error, zio->io_type, |
d148e951 | 104 | (u_longlong_t)zio->io_offset, (u_longlong_t)zio->io_size, |
cae5b340 | 105 | zio->io_flags); |
d148e951 BB |
106 | #endif |
107 | } | |
108 | ||
6839eed2 BB |
109 | /* |
110 | * Use the Linux 'noop' elevator for zfs managed block devices. This | |
111 | * strikes the ideal balance by allowing the zfs elevator to do all | |
112 | * request ordering and prioritization. While allowing the Linux | |
113 | * elevator to do the maximum front/back merging allowed by the | |
114 | * physical device. This yields the largest possible requests for | |
115 | * the device with the lowest total overhead. | |
6839eed2 | 116 | */ |
a07c8b41 | 117 | static void |
fdcd952b | 118 | vdev_elevator_switch(vdev_t *v, char *elevator) |
6839eed2 | 119 | { |
fdcd952b | 120 | vdev_disk_t *vd = v->vdev_tsd; |
a07c8b41 MZ |
121 | struct request_queue *q; |
122 | char *device; | |
e2448b0e | 123 | int error; |
fdcd952b | 124 | |
a07c8b41 MZ |
125 | for (int c = 0; c < v->vdev_children; c++) |
126 | vdev_elevator_switch(v->vdev_child[c], elevator); | |
127 | ||
128 | if (!v->vdev_ops->vdev_op_leaf || vd->vd_bdev == NULL) | |
129 | return; | |
130 | ||
131 | q = bdev_get_queue(vd->vd_bdev); | |
132 | device = vd->vd_bdev->bd_disk->disk_name; | |
133 | ||
84daadde PS |
134 | /* |
135 | * Skip devices which are not whole disks (partitions). | |
136 | * Device-mapper devices are excepted since they may be whole | |
137 | * disks despite the vdev_wholedisk flag, in which case we can | |
138 | * and should switch the elevator. If the device-mapper device | |
139 | * does not have an elevator (i.e. dm-raid, dm-crypt, etc.) the | |
140 | * "Skip devices without schedulers" check below will fail. | |
141 | */ | |
142 | if (!v->vdev_wholedisk && strncmp(device, "dm-", 3) != 0) | |
a07c8b41 | 143 | return; |
6839eed2 | 144 | |
fdcd952b | 145 | /* Leave existing scheduler when set to "none" */ |
5eacc075 | 146 | if ((strncmp(elevator, "none", 4) == 0) && (strlen(elevator) == 4)) |
a07c8b41 | 147 | return; |
6839eed2 | 148 | |
a07c8b41 MZ |
149 | /* |
150 | * The elevator_change() function was available in kernels from | |
151 | * 2.6.36 to 4.11. When not available fall back to using the user | |
152 | * mode helper functionality to set the elevator via sysfs. This | |
153 | * requires /bin/echo and sysfs to be mounted which may not be true | |
154 | * early in the boot process. | |
155 | */ | |
6d1d976b BB |
156 | #ifdef HAVE_ELEVATOR_CHANGE |
157 | error = elevator_change(q, elevator); | |
158 | #else | |
a08ee875 | 159 | #define SET_SCHEDULER_CMD \ |
6d1d976b BB |
160 | "exec 0</dev/null " \ |
161 | " 1>/sys/block/%s/queue/scheduler " \ | |
162 | " 2>/dev/null; " \ | |
163 | "echo %s" | |
164 | ||
a07c8b41 MZ |
165 | char *argv[] = { "/bin/sh", "-c", NULL, NULL }; |
166 | char *envp[] = { NULL }; | |
6d1d976b | 167 | |
a07c8b41 MZ |
168 | argv[2] = kmem_asprintf(SET_SCHEDULER_CMD, device, elevator); |
169 | error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); | |
170 | strfree(argv[2]); | |
6d1d976b | 171 | #endif /* HAVE_ELEVATOR_CHANGE */ |
a07c8b41 MZ |
172 | if (error) { |
173 | zfs_dbgmsg("Unable to set \"%s\" scheduler for %s (%s): %d\n", | |
a08ee875 | 174 | elevator, v->vdev_path, device, error); |
a07c8b41 | 175 | } |
6839eed2 BB |
176 | } |
177 | ||
b5a28807 ED |
178 | /* |
179 | * Expanding a whole disk vdev involves invoking BLKRRPART on the | |
180 | * whole disk device. This poses a problem, because BLKRRPART will | |
181 | * return EBUSY if one of the disk's partitions is open. That's why | |
182 | * we have to do it here, just before opening the data partition. | |
183 | * Unfortunately, BLKRRPART works by dropping all partitions and | |
184 | * recreating them, which means that for a short time window, all | |
185 | * /dev/sdxN device files disappear (until udev recreates them). | |
186 | * This means two things: | |
187 | * - When we open the data partition just after a BLKRRPART, we | |
188 | * can't do it using the normal device file path because of the | |
189 | * obvious race condition with udev. Instead, we use reliable | |
190 | * kernel APIs to get a handle to the new partition device from | |
191 | * the whole disk device. | |
192 | * - Because vdev_disk_open() initially needs to find the device | |
193 | * using its path, multiple vdev_disk_open() invocations in | |
194 | * short succession on the same disk with BLKRRPARTs in the | |
195 | * middle have a high probability of failure (because of the | |
196 | * race condition with udev). A typical situation where this | |
197 | * might happen is when the zpool userspace tool does a | |
198 | * TRYIMPORT immediately followed by an IMPORT. For this | |
199 | * reason, we only invoke BLKRRPART in the module when strictly | |
200 | * necessary (zpool online -e case), and rely on userspace to | |
201 | * do it when possible. | |
202 | */ | |
203 | static struct block_device * | |
204 | vdev_disk_rrpart(const char *path, int mode, vdev_disk_t *vd) | |
205 | { | |
206 | #if defined(HAVE_3ARG_BLKDEV_GET) && defined(HAVE_GET_GENDISK) | |
207 | struct block_device *bdev, *result = ERR_PTR(-ENXIO); | |
208 | struct gendisk *disk; | |
209 | int error, partno; | |
210 | ||
8128bd89 | 211 | bdev = vdev_bdev_open(path, vdev_bdev_mode(mode), zfs_vdev_holder); |
b5a28807 | 212 | if (IS_ERR(bdev)) |
a08ee875 | 213 | return (bdev); |
b5a28807 ED |
214 | |
215 | disk = get_gendisk(bdev->bd_dev, &partno); | |
216 | vdev_bdev_close(bdev, vdev_bdev_mode(mode)); | |
217 | ||
218 | if (disk) { | |
219 | bdev = bdget(disk_devt(disk)); | |
220 | if (bdev) { | |
221 | error = blkdev_get(bdev, vdev_bdev_mode(mode), vd); | |
222 | if (error == 0) | |
223 | error = ioctl_by_bdev(bdev, BLKRRPART, 0); | |
224 | vdev_bdev_close(bdev, vdev_bdev_mode(mode)); | |
225 | } | |
226 | ||
227 | bdev = bdget_disk(disk, partno); | |
228 | if (bdev) { | |
229 | error = blkdev_get(bdev, | |
230 | vdev_bdev_mode(mode) | FMODE_EXCL, vd); | |
231 | if (error == 0) | |
232 | result = bdev; | |
233 | } | |
234 | put_disk(disk); | |
235 | } | |
236 | ||
a08ee875 | 237 | return (result); |
b5a28807 | 238 | #else |
a08ee875 | 239 | return (ERR_PTR(-EOPNOTSUPP)); |
b5a28807 ED |
240 | #endif /* defined(HAVE_3ARG_BLKDEV_GET) && defined(HAVE_GET_GENDISK) */ |
241 | } | |
242 | ||
60101509 | 243 | static int |
1bd201e7 CS |
244 | vdev_disk_open(vdev_t *v, uint64_t *psize, uint64_t *max_psize, |
245 | uint64_t *ashift) | |
60101509 | 246 | { |
b5a28807 | 247 | struct block_device *bdev = ERR_PTR(-ENXIO); |
60101509 | 248 | vdev_disk_t *vd; |
5eacc075 | 249 | int count = 0, mode, block_size; |
60101509 BB |
250 | |
251 | /* Must have a pathname and it must be absolute. */ | |
252 | if (v->vdev_path == NULL || v->vdev_path[0] != '/') { | |
253 | v->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; | |
5eacc075 | 254 | return (SET_ERROR(EINVAL)); |
60101509 BB |
255 | } |
256 | ||
0d8103d9 BB |
257 | /* |
258 | * Reopen the device if it's not currently open. Otherwise, | |
259 | * just update the physical size of the device. | |
260 | */ | |
261 | if (v->vdev_tsd != NULL) { | |
262 | ASSERT(v->vdev_reopening); | |
263 | vd = v->vdev_tsd; | |
264 | goto skip_open; | |
265 | } | |
266 | ||
ea04106b | 267 | vd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); |
60101509 | 268 | if (vd == NULL) |
5eacc075 | 269 | return (SET_ERROR(ENOMEM)); |
60101509 BB |
270 | |
271 | /* | |
272 | * Devices are always opened by the path provided at configuration | |
273 | * time. This means that if the provided path is a udev by-id path | |
274 | * then drives may be recabled without an issue. If the provided | |
c06d4368 | 275 | * path is a udev by-path path, then the physical location information |
60101509 BB |
276 | * will be preserved. This can be critical for more complicated |
277 | * configurations where drives are located in specific physical | |
278 | * locations to maximize the systems tolerence to component failure. | |
c06d4368 | 279 | * Alternatively, you can provide your own udev rule to flexibly map |
60101509 | 280 | * the drives as you see fit. It is not advised that you use the |
c06d4368 | 281 | * /dev/[hd]d devices which may be reordered due to probing order. |
60101509 BB |
282 | * Devices in the wrong locations will be detected by the higher |
283 | * level vdev validation. | |
5eacc075 AX |
284 | * |
285 | * The specified paths may be briefly removed and recreated in | |
286 | * response to udev events. This should be exceptionally unlikely | |
287 | * because the zpool command makes every effort to verify these paths | |
288 | * have already settled prior to reaching this point. Therefore, | |
289 | * a ENOENT failure at this point is highly likely to be transient | |
290 | * and it is reasonable to sleep and retry before giving up. In | |
291 | * practice delays have been observed to be on the order of 100ms. | |
60101509 BB |
292 | */ |
293 | mode = spa_mode(v->vdev_spa); | |
b5a28807 ED |
294 | if (v->vdev_wholedisk && v->vdev_expanding) |
295 | bdev = vdev_disk_rrpart(v->vdev_path, mode, vd); | |
5eacc075 AX |
296 | |
297 | while (IS_ERR(bdev) && count < 50) { | |
8128bd89 BB |
298 | bdev = vdev_bdev_open(v->vdev_path, |
299 | vdev_bdev_mode(mode), zfs_vdev_holder); | |
5eacc075 AX |
300 | if (unlikely(PTR_ERR(bdev) == -ENOENT)) { |
301 | msleep(10); | |
302 | count++; | |
303 | } else if (IS_ERR(bdev)) { | |
304 | break; | |
305 | } | |
306 | } | |
307 | ||
60101509 | 308 | if (IS_ERR(bdev)) { |
5eacc075 AX |
309 | dprintf("failed open v->vdev_path=%s, error=%d count=%d\n", |
310 | v->vdev_path, -PTR_ERR(bdev), count); | |
a08ee875 | 311 | kmem_free(vd, sizeof (vdev_disk_t)); |
5eacc075 | 312 | return (SET_ERROR(-PTR_ERR(bdev))); |
60101509 BB |
313 | } |
314 | ||
315 | v->vdev_tsd = vd; | |
316 | vd->vd_bdev = bdev; | |
0d8103d9 BB |
317 | |
318 | skip_open: | |
319 | /* Determine the physical block size */ | |
320 | block_size = vdev_bdev_block_size(vd->vd_bdev); | |
60101509 | 321 | |
60101509 BB |
322 | /* Clear the nowritecache bit, causes vdev_reopen() to try again. */ |
323 | v->vdev_nowritecache = B_FALSE; | |
324 | ||
e10b0808 AX |
325 | /* Inform the ZIO pipeline that we are non-rotational */ |
326 | v->vdev_nonrot = blk_queue_nonrot(bdev_get_queue(vd->vd_bdev)); | |
327 | ||
60101509 | 328 | /* Physical volume size in bytes */ |
0d8103d9 | 329 | *psize = bdev_capacity(vd->vd_bdev); |
60101509 | 330 | |
1bd201e7 CS |
331 | /* TODO: report possible expansion size */ |
332 | *max_psize = *psize; | |
333 | ||
60101509 | 334 | /* Based on the minimum sector size set the block size */ |
ea04106b | 335 | *ashift = highbit64(MAX(block_size, SPA_MINBLOCKSIZE)) - 1; |
60101509 | 336 | |
6839eed2 | 337 | /* Try to set the io scheduler elevator algorithm */ |
fdcd952b | 338 | (void) vdev_elevator_switch(v, zfs_vdev_scheduler); |
6839eed2 | 339 | |
a08ee875 | 340 | return (0); |
60101509 BB |
341 | } |
342 | ||
343 | static void | |
344 | vdev_disk_close(vdev_t *v) | |
345 | { | |
346 | vdev_disk_t *vd = v->vdev_tsd; | |
347 | ||
0d8103d9 | 348 | if (v->vdev_reopening || vd == NULL) |
60101509 BB |
349 | return; |
350 | ||
351 | if (vd->vd_bdev != NULL) | |
352 | vdev_bdev_close(vd->vd_bdev, | |
a08ee875 | 353 | vdev_bdev_mode(spa_mode(v->vdev_spa))); |
60101509 | 354 | |
a08ee875 | 355 | kmem_free(vd, sizeof (vdev_disk_t)); |
60101509 BB |
356 | v->vdev_tsd = NULL; |
357 | } | |
358 | ||
359 | static dio_request_t * | |
360 | vdev_disk_dio_alloc(int bio_count) | |
361 | { | |
362 | dio_request_t *dr; | |
363 | int i; | |
364 | ||
a08ee875 | 365 | dr = kmem_zalloc(sizeof (dio_request_t) + |
ea04106b | 366 | sizeof (struct bio *) * bio_count, KM_SLEEP); |
60101509 | 367 | if (dr) { |
60101509 BB |
368 | atomic_set(&dr->dr_ref, 0); |
369 | dr->dr_bio_count = bio_count; | |
370 | dr->dr_error = 0; | |
371 | ||
372 | for (i = 0; i < dr->dr_bio_count; i++) | |
373 | dr->dr_bio[i] = NULL; | |
374 | } | |
375 | ||
a08ee875 | 376 | return (dr); |
60101509 BB |
377 | } |
378 | ||
379 | static void | |
380 | vdev_disk_dio_free(dio_request_t *dr) | |
381 | { | |
382 | int i; | |
383 | ||
384 | for (i = 0; i < dr->dr_bio_count; i++) | |
385 | if (dr->dr_bio[i]) | |
386 | bio_put(dr->dr_bio[i]); | |
387 | ||
a08ee875 LG |
388 | kmem_free(dr, sizeof (dio_request_t) + |
389 | sizeof (struct bio *) * dr->dr_bio_count); | |
60101509 BB |
390 | } |
391 | ||
392 | static void | |
393 | vdev_disk_dio_get(dio_request_t *dr) | |
394 | { | |
395 | atomic_inc(&dr->dr_ref); | |
396 | } | |
397 | ||
398 | static int | |
399 | vdev_disk_dio_put(dio_request_t *dr) | |
400 | { | |
401 | int rc = atomic_dec_return(&dr->dr_ref); | |
402 | ||
403 | /* | |
404 | * Free the dio_request when the last reference is dropped and | |
405 | * ensure zio_interpret is called only once with the correct zio | |
406 | */ | |
407 | if (rc == 0) { | |
408 | zio_t *zio = dr->dr_zio; | |
409 | int error = dr->dr_error; | |
410 | ||
411 | vdev_disk_dio_free(dr); | |
412 | ||
413 | if (zio) { | |
414 | zio->io_error = error; | |
d148e951 BB |
415 | ASSERT3S(zio->io_error, >=, 0); |
416 | if (zio->io_error) | |
417 | vdev_disk_error(zio); | |
cae5b340 AX |
418 | |
419 | zio_delay_interrupt(zio); | |
60101509 BB |
420 | } |
421 | } | |
422 | ||
a08ee875 | 423 | return (rc); |
60101509 BB |
424 | } |
425 | ||
e10b0808 | 426 | BIO_END_IO_PROTO(vdev_disk_physio_completion, bio, error) |
60101509 BB |
427 | { |
428 | dio_request_t *dr = bio->bi_private; | |
429 | int rc; | |
430 | ||
e10b0808 AX |
431 | if (dr->dr_error == 0) { |
432 | #ifdef HAVE_1ARG_BIO_END_IO_T | |
cae5b340 | 433 | dr->dr_error = BIO_END_IO_ERROR(bio); |
e10b0808 AX |
434 | #else |
435 | if (error) | |
436 | dr->dr_error = -(error); | |
437 | else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
438 | dr->dr_error = EIO; | |
439 | #endif | |
440 | } | |
60101509 | 441 | |
cae5b340 | 442 | /* Drop reference acquired by __vdev_disk_physio */ |
60101509 | 443 | rc = vdev_disk_dio_put(dr); |
60101509 BB |
444 | } |
445 | ||
60101509 BB |
446 | static unsigned int |
447 | bio_map(struct bio *bio, void *bio_ptr, unsigned int bio_size) | |
448 | { | |
449 | unsigned int offset, size, i; | |
450 | struct page *page; | |
451 | ||
452 | offset = offset_in_page(bio_ptr); | |
453 | for (i = 0; i < bio->bi_max_vecs; i++) { | |
454 | size = PAGE_SIZE - offset; | |
455 | ||
456 | if (bio_size <= 0) | |
457 | break; | |
458 | ||
459 | if (size > bio_size) | |
460 | size = bio_size; | |
461 | ||
ea04106b | 462 | if (is_vmalloc_addr(bio_ptr)) |
60101509 BB |
463 | page = vmalloc_to_page(bio_ptr); |
464 | else | |
465 | page = virt_to_page(bio_ptr); | |
466 | ||
ea04106b AX |
467 | /* |
468 | * Some network related block device uses tcp_sendpage, which | |
469 | * doesn't behave well when using 0-count page, this is a | |
470 | * safety net to catch them. | |
471 | */ | |
472 | ASSERT3S(page_count(page), >, 0); | |
473 | ||
60101509 BB |
474 | if (bio_add_page(bio, page, size, offset) != size) |
475 | break; | |
476 | ||
477 | bio_ptr += size; | |
478 | bio_size -= size; | |
479 | offset = 0; | |
480 | } | |
481 | ||
a08ee875 | 482 | return (bio_size); |
60101509 BB |
483 | } |
484 | ||
cae5b340 AX |
485 | static unsigned int |
486 | bio_map_abd_off(struct bio *bio, abd_t *abd, unsigned int size, size_t off) | |
487 | { | |
488 | if (abd_is_linear(abd)) | |
489 | return (bio_map(bio, ((char *)abd_to_buf(abd)) + off, size)); | |
490 | ||
491 | return (abd_scatter_bio_map_off(bio, abd, size, off)); | |
492 | } | |
493 | ||
87dac73d AX |
494 | static inline void |
495 | vdev_submit_bio_impl(struct bio *bio) | |
496 | { | |
497 | #ifdef HAVE_1ARG_SUBMIT_BIO | |
498 | submit_bio(bio); | |
499 | #else | |
500 | submit_bio(0, bio); | |
501 | #endif | |
502 | } | |
503 | ||
cae5b340 AX |
504 | #ifndef HAVE_BIO_SET_DEV |
505 | static inline void | |
506 | bio_set_dev(struct bio *bio, struct block_device *bdev) | |
507 | { | |
508 | bio->bi_bdev = bdev; | |
509 | } | |
510 | #endif /* !HAVE_BIO_SET_DEV */ | |
511 | ||
e10b0808 | 512 | static inline void |
87dac73d | 513 | vdev_submit_bio(struct bio *bio) |
e10b0808 AX |
514 | { |
515 | #ifdef HAVE_CURRENT_BIO_TAIL | |
516 | struct bio **bio_tail = current->bio_tail; | |
517 | current->bio_tail = NULL; | |
87dac73d | 518 | vdev_submit_bio_impl(bio); |
e10b0808 AX |
519 | current->bio_tail = bio_tail; |
520 | #else | |
521 | struct bio_list *bio_list = current->bio_list; | |
522 | current->bio_list = NULL; | |
87dac73d | 523 | vdev_submit_bio_impl(bio); |
e10b0808 AX |
524 | current->bio_list = bio_list; |
525 | #endif | |
526 | } | |
527 | ||
60101509 | 528 | static int |
cae5b340 AX |
529 | __vdev_disk_physio(struct block_device *bdev, zio_t *zio, |
530 | size_t io_size, uint64_t io_offset, int rw, int flags) | |
60101509 | 531 | { |
a08ee875 | 532 | dio_request_t *dr; |
cae5b340 | 533 | uint64_t abd_offset; |
60101509 | 534 | uint64_t bio_offset; |
87dac73d | 535 | int bio_size, bio_count = 16; |
f74fae8b | 536 | int i = 0, error = 0; |
68d83c55 AX |
537 | #if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG) |
538 | struct blk_plug plug; | |
539 | #endif | |
60101509 | 540 | |
cae5b340 AX |
541 | ASSERT(zio != NULL); |
542 | ASSERT3U(io_offset + io_size, <=, bdev->bd_inode->i_size); | |
e06be586 | 543 | |
60101509 BB |
544 | retry: |
545 | dr = vdev_disk_dio_alloc(bio_count); | |
546 | if (dr == NULL) | |
a08ee875 | 547 | return (ENOMEM); |
60101509 | 548 | |
2959d94a | 549 | if (zio && !(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD))) |
ea04106b | 550 | bio_set_flags_failfast(bdev, &flags); |
2959d94a | 551 | |
60101509 | 552 | dr->dr_zio = zio; |
60101509 | 553 | |
60101509 BB |
554 | /* |
555 | * When the IO size exceeds the maximum bio size for the request | |
556 | * queue we are forced to break the IO in multiple bio's and wait | |
557 | * for them all to complete. Ideally, all pool users will set | |
558 | * their volume block size to match the maximum request size and | |
559 | * the common case will be one bio per vdev IO request. | |
560 | */ | |
cae5b340 AX |
561 | |
562 | abd_offset = 0; | |
563 | bio_offset = io_offset; | |
564 | bio_size = io_size; | |
60101509 BB |
565 | for (i = 0; i <= dr->dr_bio_count; i++) { |
566 | ||
567 | /* Finished constructing bio's for given buffer */ | |
568 | if (bio_size <= 0) | |
569 | break; | |
570 | ||
571 | /* | |
572 | * By default only 'bio_count' bio's per dio are allowed. | |
573 | * However, if we find ourselves in a situation where more | |
574 | * are needed we allocate a larger dio and warn the user. | |
575 | */ | |
576 | if (dr->dr_bio_count == i) { | |
577 | vdev_disk_dio_free(dr); | |
578 | bio_count *= 2; | |
60101509 BB |
579 | goto retry; |
580 | } | |
581 | ||
ea04106b | 582 | /* bio_alloc() with __GFP_WAIT never returns NULL */ |
e10b0808 | 583 | dr->dr_bio[i] = bio_alloc(GFP_NOIO, |
cae5b340 AX |
584 | MIN(abd_nr_pages_off(zio->io_abd, bio_size, abd_offset), |
585 | BIO_MAX_PAGES)); | |
ea04106b | 586 | if (unlikely(dr->dr_bio[i] == NULL)) { |
60101509 | 587 | vdev_disk_dio_free(dr); |
a08ee875 | 588 | return (ENOMEM); |
60101509 BB |
589 | } |
590 | ||
591 | /* Matching put called by vdev_disk_physio_completion */ | |
592 | vdev_disk_dio_get(dr); | |
593 | ||
cae5b340 | 594 | bio_set_dev(dr->dr_bio[i], bdev); |
ea04106b | 595 | BIO_BI_SECTOR(dr->dr_bio[i]) = bio_offset >> 9; |
60101509 BB |
596 | dr->dr_bio[i]->bi_end_io = vdev_disk_physio_completion; |
597 | dr->dr_bio[i]->bi_private = dr; | |
87dac73d | 598 | bio_set_op_attrs(dr->dr_bio[i], rw, flags); |
60101509 BB |
599 | |
600 | /* Remaining size is returned to become the new size */ | |
cae5b340 AX |
601 | bio_size = bio_map_abd_off(dr->dr_bio[i], zio->io_abd, |
602 | bio_size, abd_offset); | |
60101509 BB |
603 | |
604 | /* Advance in buffer and construct another bio if needed */ | |
cae5b340 | 605 | abd_offset += BIO_BI_SIZE(dr->dr_bio[i]); |
ea04106b | 606 | bio_offset += BIO_BI_SIZE(dr->dr_bio[i]); |
60101509 BB |
607 | } |
608 | ||
e10b0808 | 609 | /* Extra reference to protect dio_request during vdev_submit_bio */ |
60101509 BB |
610 | vdev_disk_dio_get(dr); |
611 | ||
68d83c55 AX |
612 | #if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG) |
613 | if (dr->dr_bio_count > 1) | |
614 | blk_start_plug(&plug); | |
615 | #endif | |
616 | ||
60101509 BB |
617 | /* Submit all bio's associated with this dio */ |
618 | for (i = 0; i < dr->dr_bio_count; i++) | |
619 | if (dr->dr_bio[i]) | |
87dac73d | 620 | vdev_submit_bio(dr->dr_bio[i]); |
60101509 | 621 | |
68d83c55 AX |
622 | #if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG) |
623 | if (dr->dr_bio_count > 1) | |
624 | blk_finish_plug(&plug); | |
625 | #endif | |
60101509 | 626 | |
a08ee875 | 627 | (void) vdev_disk_dio_put(dr); |
60101509 | 628 | |
a08ee875 | 629 | return (error); |
60101509 BB |
630 | } |
631 | ||
cae5b340 | 632 | BIO_END_IO_PROTO(vdev_disk_io_flush_completion, bio, error) |
60101509 BB |
633 | { |
634 | zio_t *zio = bio->bi_private; | |
e10b0808 | 635 | #ifdef HAVE_1ARG_BIO_END_IO_T |
cae5b340 AX |
636 | zio->io_error = BIO_END_IO_ERROR(bio); |
637 | #else | |
638 | zio->io_error = -error; | |
e10b0808 | 639 | #endif |
60101509 | 640 | |
cae5b340 | 641 | if (zio->io_error && (zio->io_error == EOPNOTSUPP)) |
60101509 BB |
642 | zio->io_vd->vdev_nowritecache = B_TRUE; |
643 | ||
644 | bio_put(bio); | |
d148e951 BB |
645 | ASSERT3S(zio->io_error, >=, 0); |
646 | if (zio->io_error) | |
647 | vdev_disk_error(zio); | |
60101509 | 648 | zio_interrupt(zio); |
60101509 BB |
649 | } |
650 | ||
651 | static int | |
652 | vdev_disk_io_flush(struct block_device *bdev, zio_t *zio) | |
653 | { | |
654 | struct request_queue *q; | |
655 | struct bio *bio; | |
656 | ||
657 | q = bdev_get_queue(bdev); | |
658 | if (!q) | |
a08ee875 | 659 | return (ENXIO); |
60101509 | 660 | |
c06d4368 | 661 | bio = bio_alloc(GFP_NOIO, 0); |
ea04106b AX |
662 | /* bio_alloc() with __GFP_WAIT never returns NULL */ |
663 | if (unlikely(bio == NULL)) | |
a08ee875 | 664 | return (ENOMEM); |
60101509 BB |
665 | |
666 | bio->bi_end_io = vdev_disk_io_flush_completion; | |
667 | bio->bi_private = zio; | |
cae5b340 | 668 | bio_set_dev(bio, bdev); |
68d83c55 | 669 | bio_set_flush(bio); |
87dac73d | 670 | vdev_submit_bio(bio); |
ea04106b | 671 | invalidate_bdev(bdev); |
60101509 | 672 | |
a08ee875 | 673 | return (0); |
60101509 | 674 | } |
60101509 | 675 | |
e10b0808 | 676 | static void |
60101509 BB |
677 | vdev_disk_io_start(zio_t *zio) |
678 | { | |
679 | vdev_t *v = zio->io_vd; | |
680 | vdev_disk_t *vd = v->vdev_tsd; | |
87dac73d | 681 | int rw, flags, error; |
60101509 BB |
682 | |
683 | switch (zio->io_type) { | |
684 | case ZIO_TYPE_IOCTL: | |
685 | ||
686 | if (!vdev_readable(v)) { | |
a08ee875 | 687 | zio->io_error = SET_ERROR(ENXIO); |
e10b0808 AX |
688 | zio_interrupt(zio); |
689 | return; | |
60101509 BB |
690 | } |
691 | ||
692 | switch (zio->io_cmd) { | |
693 | case DKIOCFLUSHWRITECACHE: | |
694 | ||
695 | if (zfs_nocacheflush) | |
696 | break; | |
697 | ||
698 | if (v->vdev_nowritecache) { | |
a08ee875 | 699 | zio->io_error = SET_ERROR(ENOTSUP); |
60101509 BB |
700 | break; |
701 | } | |
702 | ||
703 | error = vdev_disk_io_flush(vd->vd_bdev, zio); | |
704 | if (error == 0) | |
e10b0808 | 705 | return; |
60101509 BB |
706 | |
707 | zio->io_error = error; | |
60101509 BB |
708 | |
709 | break; | |
710 | ||
711 | default: | |
a08ee875 | 712 | zio->io_error = SET_ERROR(ENOTSUP); |
60101509 BB |
713 | } |
714 | ||
e10b0808 AX |
715 | zio_execute(zio); |
716 | return; | |
60101509 | 717 | case ZIO_TYPE_WRITE: |
87dac73d | 718 | rw = WRITE; |
68d83c55 AX |
719 | #if defined(HAVE_BLK_QUEUE_HAVE_BIO_RW_UNPLUG) |
720 | flags = (1 << BIO_RW_UNPLUG); | |
721 | #elif defined(REQ_UNPLUG) | |
722 | flags = REQ_UNPLUG; | |
723 | #else | |
724 | flags = 0; | |
725 | #endif | |
60101509 BB |
726 | break; |
727 | ||
728 | case ZIO_TYPE_READ: | |
87dac73d | 729 | rw = READ; |
68d83c55 AX |
730 | #if defined(HAVE_BLK_QUEUE_HAVE_BIO_RW_UNPLUG) |
731 | flags = (1 << BIO_RW_UNPLUG); | |
732 | #elif defined(REQ_UNPLUG) | |
733 | flags = REQ_UNPLUG; | |
734 | #else | |
735 | flags = 0; | |
736 | #endif | |
60101509 BB |
737 | break; |
738 | ||
739 | default: | |
a08ee875 | 740 | zio->io_error = SET_ERROR(ENOTSUP); |
e10b0808 AX |
741 | zio_interrupt(zio); |
742 | return; | |
60101509 BB |
743 | } |
744 | ||
cae5b340 AX |
745 | zio->io_target_timestamp = zio_handle_io_delay(zio); |
746 | error = __vdev_disk_physio(vd->vd_bdev, zio, | |
68d83c55 | 747 | zio->io_size, zio->io_offset, rw, flags); |
60101509 BB |
748 | if (error) { |
749 | zio->io_error = error; | |
e10b0808 AX |
750 | zio_interrupt(zio); |
751 | return; | |
60101509 | 752 | } |
60101509 BB |
753 | } |
754 | ||
755 | static void | |
756 | vdev_disk_io_done(zio_t *zio) | |
757 | { | |
758 | /* | |
759 | * If the device returned EIO, we revalidate the media. If it is | |
760 | * determined the media has changed this triggers the asynchronous | |
761 | * removal of the device from the configuration. | |
762 | */ | |
763 | if (zio->io_error == EIO) { | |
a08ee875 | 764 | vdev_t *v = zio->io_vd; |
60101509 BB |
765 | vdev_disk_t *vd = v->vdev_tsd; |
766 | ||
767 | if (check_disk_change(vd->vd_bdev)) { | |
768 | vdev_bdev_invalidate(vd->vd_bdev); | |
769 | v->vdev_remove_wanted = B_TRUE; | |
770 | spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); | |
771 | } | |
772 | } | |
773 | } | |
774 | ||
775 | static void | |
776 | vdev_disk_hold(vdev_t *vd) | |
777 | { | |
778 | ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); | |
779 | ||
780 | /* We must have a pathname, and it must be absolute. */ | |
781 | if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') | |
782 | return; | |
783 | ||
784 | /* | |
785 | * Only prefetch path and devid info if the device has | |
786 | * never been opened. | |
787 | */ | |
788 | if (vd->vdev_tsd != NULL) | |
789 | return; | |
790 | ||
791 | /* XXX: Implement me as a vnode lookup for the device */ | |
792 | vd->vdev_name_vp = NULL; | |
793 | vd->vdev_devid_vp = NULL; | |
794 | } | |
795 | ||
796 | static void | |
797 | vdev_disk_rele(vdev_t *vd) | |
798 | { | |
799 | ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); | |
800 | ||
801 | /* XXX: Implement me as a vnode rele for the device */ | |
802 | } | |
803 | ||
a07c8b41 MZ |
804 | static int |
805 | param_set_vdev_scheduler(const char *val, zfs_kernel_param_t *kp) | |
806 | { | |
807 | spa_t *spa = NULL; | |
808 | char *p; | |
809 | ||
810 | if (val == NULL) | |
811 | return (SET_ERROR(-EINVAL)); | |
812 | ||
813 | if ((p = strchr(val, '\n')) != NULL) | |
814 | *p = '\0'; | |
815 | ||
816 | if (spa_mode_global != 0) { | |
817 | mutex_enter(&spa_namespace_lock); | |
818 | while ((spa = spa_next(spa)) != NULL) { | |
819 | if (spa_state(spa) != POOL_STATE_ACTIVE || | |
820 | !spa_writeable(spa) || spa_suspended(spa)) | |
821 | continue; | |
822 | ||
823 | spa_open_ref(spa, FTAG); | |
824 | mutex_exit(&spa_namespace_lock); | |
825 | vdev_elevator_switch(spa->spa_root_vdev, (char *)val); | |
826 | mutex_enter(&spa_namespace_lock); | |
827 | spa_close(spa, FTAG); | |
828 | } | |
829 | mutex_exit(&spa_namespace_lock); | |
830 | } | |
831 | ||
832 | return (param_set_charp(val, kp)); | |
833 | } | |
834 | ||
60101509 BB |
835 | vdev_ops_t vdev_disk_ops = { |
836 | vdev_disk_open, | |
837 | vdev_disk_close, | |
838 | vdev_default_asize, | |
839 | vdev_disk_io_start, | |
840 | vdev_disk_io_done, | |
841 | NULL, | |
cae5b340 | 842 | NULL, |
60101509 BB |
843 | vdev_disk_hold, |
844 | vdev_disk_rele, | |
845 | VDEV_TYPE_DISK, /* name of this vdev type */ | |
846 | B_TRUE /* leaf vdev */ | |
847 | }; | |
848 | ||
a07c8b41 MZ |
849 | module_param_call(zfs_vdev_scheduler, param_set_vdev_scheduler, |
850 | param_get_charp, &zfs_vdev_scheduler, 0644); | |
c409e464 | 851 | MODULE_PARM_DESC(zfs_vdev_scheduler, "I/O scheduler"); |