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