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