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.
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.
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]
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>.
26 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
29 #include <sys/zfs_context.h>
31 #include <sys/vdev_disk.h>
32 #include <sys/vdev_impl.h>
34 #include <sys/fs/zfs.h>
36 #include <sys/sunldi.h>
38 char *zfs_vdev_scheduler
= VDEV_SCHEDULER
;
39 static void *zfs_vdev_holder
= VDEV_HOLDER
;
42 * Virtual device vector for disks.
44 typedef struct dio_request
{
45 zio_t
*dr_zio
; /* Parent ZIO */
46 atomic_t dr_ref
; /* References */
47 int dr_error
; /* Bio error */
48 int dr_bio_count
; /* Count of bio's */
49 struct bio
*dr_bio
[0]; /* Attached bio's */
53 #ifdef HAVE_OPEN_BDEV_EXCLUSIVE
55 vdev_bdev_mode(int smode
)
59 ASSERT3S(smode
& (FREAD
| FWRITE
), !=, 0);
71 vdev_bdev_mode(int smode
)
75 ASSERT3S(smode
& (FREAD
| FWRITE
), !=, 0);
77 if ((smode
& FREAD
) && !(smode
& FWRITE
))
82 #endif /* HAVE_OPEN_BDEV_EXCLUSIVE */
85 bdev_capacity(struct block_device
*bdev
)
87 struct hd_struct
*part
= bdev
->bd_part
;
89 /* The partition capacity referenced by the block device */
91 return (part
->nr_sects
<< 9);
93 /* Otherwise assume the full device capacity */
94 return (get_capacity(bdev
->bd_disk
) << 9);
98 vdev_disk_error(zio_t
*zio
)
101 printk("ZFS: zio error=%d type=%d offset=%llu size=%llu "
102 "flags=%x\n", zio
->io_error
, zio
->io_type
,
103 (u_longlong_t
)zio
->io_offset
, (u_longlong_t
)zio
->io_size
,
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.
117 vdev_elevator_switch(vdev_t
*v
, char *elevator
)
119 vdev_disk_t
*vd
= v
->vdev_tsd
;
120 struct block_device
*bdev
= vd
->vd_bdev
;
121 struct request_queue
*q
= bdev_get_queue(bdev
);
122 char *device
= bdev
->bd_disk
->disk_name
;
126 * Skip devices which are not whole disks (partitions).
127 * Device-mapper devices are excepted since they may be whole
128 * disks despite the vdev_wholedisk flag, in which case we can
129 * and should switch the elevator. If the device-mapper device
130 * does not have an elevator (i.e. dm-raid, dm-crypt, etc.) the
131 * "Skip devices without schedulers" check below will fail.
133 if (!v
->vdev_wholedisk
&& strncmp(device
, "dm-", 3) != 0)
136 /* Skip devices without schedulers (loop, ram, dm, etc) */
137 if (!q
->elevator
|| !blk_queue_stackable(q
))
140 /* Leave existing scheduler when set to "none" */
141 if ((strncmp(elevator
, "none", 4) == 0) && (strlen(elevator
) == 4))
144 #ifdef HAVE_ELEVATOR_CHANGE
145 error
= elevator_change(q
, elevator
);
148 * For pre-2.6.36 kernels elevator_change() is not available.
149 * Therefore we fall back to using a usermodehelper to echo the
150 * elevator into sysfs; This requires /bin/echo and sysfs to be
151 * mounted which may not be true early in the boot process.
153 #define SET_SCHEDULER_CMD \
154 "exec 0</dev/null " \
155 " 1>/sys/block/%s/queue/scheduler " \
160 char *argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
161 char *envp
[] = { NULL
};
163 argv
[2] = kmem_asprintf(SET_SCHEDULER_CMD
, device
, elevator
);
164 error
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_PROC
);
167 #endif /* HAVE_ELEVATOR_CHANGE */
169 printk("ZFS: Unable to set \"%s\" scheduler for %s (%s): %d\n",
170 elevator
, v
->vdev_path
, device
, error
);
176 * Expanding a whole disk vdev involves invoking BLKRRPART on the
177 * whole disk device. This poses a problem, because BLKRRPART will
178 * return EBUSY if one of the disk's partitions is open. That's why
179 * we have to do it here, just before opening the data partition.
180 * Unfortunately, BLKRRPART works by dropping all partitions and
181 * recreating them, which means that for a short time window, all
182 * /dev/sdxN device files disappear (until udev recreates them).
183 * This means two things:
184 * - When we open the data partition just after a BLKRRPART, we
185 * can't do it using the normal device file path because of the
186 * obvious race condition with udev. Instead, we use reliable
187 * kernel APIs to get a handle to the new partition device from
188 * the whole disk device.
189 * - Because vdev_disk_open() initially needs to find the device
190 * using its path, multiple vdev_disk_open() invocations in
191 * short succession on the same disk with BLKRRPARTs in the
192 * middle have a high probability of failure (because of the
193 * race condition with udev). A typical situation where this
194 * might happen is when the zpool userspace tool does a
195 * TRYIMPORT immediately followed by an IMPORT. For this
196 * reason, we only invoke BLKRRPART in the module when strictly
197 * necessary (zpool online -e case), and rely on userspace to
198 * do it when possible.
200 static struct block_device
*
201 vdev_disk_rrpart(const char *path
, int mode
, vdev_disk_t
*vd
)
203 #if defined(HAVE_3ARG_BLKDEV_GET) && defined(HAVE_GET_GENDISK)
204 struct block_device
*bdev
, *result
= ERR_PTR(-ENXIO
);
205 struct gendisk
*disk
;
208 bdev
= vdev_bdev_open(path
, vdev_bdev_mode(mode
), zfs_vdev_holder
);
212 disk
= get_gendisk(bdev
->bd_dev
, &partno
);
213 vdev_bdev_close(bdev
, vdev_bdev_mode(mode
));
216 bdev
= bdget(disk_devt(disk
));
218 error
= blkdev_get(bdev
, vdev_bdev_mode(mode
), vd
);
220 error
= ioctl_by_bdev(bdev
, BLKRRPART
, 0);
221 vdev_bdev_close(bdev
, vdev_bdev_mode(mode
));
224 bdev
= bdget_disk(disk
, partno
);
226 error
= blkdev_get(bdev
,
227 vdev_bdev_mode(mode
) | FMODE_EXCL
, vd
);
236 return (ERR_PTR(-EOPNOTSUPP
));
237 #endif /* defined(HAVE_3ARG_BLKDEV_GET) && defined(HAVE_GET_GENDISK) */
241 vdev_disk_open(vdev_t
*v
, uint64_t *psize
, uint64_t *max_psize
,
244 struct block_device
*bdev
= ERR_PTR(-ENXIO
);
246 int count
= 0, mode
, block_size
;
248 /* Must have a pathname and it must be absolute. */
249 if (v
->vdev_path
== NULL
|| v
->vdev_path
[0] != '/') {
250 v
->vdev_stat
.vs_aux
= VDEV_AUX_BAD_LABEL
;
251 return (SET_ERROR(EINVAL
));
255 * Reopen the device if it's not currently open. Otherwise,
256 * just update the physical size of the device.
258 if (v
->vdev_tsd
!= NULL
) {
259 ASSERT(v
->vdev_reopening
);
264 vd
= kmem_zalloc(sizeof (vdev_disk_t
), KM_SLEEP
);
266 return (SET_ERROR(ENOMEM
));
269 * Devices are always opened by the path provided at configuration
270 * time. This means that if the provided path is a udev by-id path
271 * then drives may be recabled without an issue. If the provided
272 * path is a udev by-path path, then the physical location information
273 * will be preserved. This can be critical for more complicated
274 * configurations where drives are located in specific physical
275 * locations to maximize the systems tolerence to component failure.
276 * Alternatively, you can provide your own udev rule to flexibly map
277 * the drives as you see fit. It is not advised that you use the
278 * /dev/[hd]d devices which may be reordered due to probing order.
279 * Devices in the wrong locations will be detected by the higher
280 * level vdev validation.
282 * The specified paths may be briefly removed and recreated in
283 * response to udev events. This should be exceptionally unlikely
284 * because the zpool command makes every effort to verify these paths
285 * have already settled prior to reaching this point. Therefore,
286 * a ENOENT failure at this point is highly likely to be transient
287 * and it is reasonable to sleep and retry before giving up. In
288 * practice delays have been observed to be on the order of 100ms.
290 mode
= spa_mode(v
->vdev_spa
);
291 if (v
->vdev_wholedisk
&& v
->vdev_expanding
)
292 bdev
= vdev_disk_rrpart(v
->vdev_path
, mode
, vd
);
294 while (IS_ERR(bdev
) && count
< 50) {
295 bdev
= vdev_bdev_open(v
->vdev_path
,
296 vdev_bdev_mode(mode
), zfs_vdev_holder
);
297 if (unlikely(PTR_ERR(bdev
) == -ENOENT
)) {
300 } else if (IS_ERR(bdev
)) {
306 dprintf("failed open v->vdev_path=%s, error=%d count=%d\n",
307 v
->vdev_path
, -PTR_ERR(bdev
), count
);
308 kmem_free(vd
, sizeof (vdev_disk_t
));
309 return (SET_ERROR(-PTR_ERR(bdev
)));
316 /* Determine the physical block size */
317 block_size
= vdev_bdev_block_size(vd
->vd_bdev
);
319 /* Clear the nowritecache bit, causes vdev_reopen() to try again. */
320 v
->vdev_nowritecache
= B_FALSE
;
322 /* Inform the ZIO pipeline that we are non-rotational */
323 v
->vdev_nonrot
= blk_queue_nonrot(bdev_get_queue(vd
->vd_bdev
));
325 /* Physical volume size in bytes */
326 *psize
= bdev_capacity(vd
->vd_bdev
);
328 /* TODO: report possible expansion size */
331 /* Based on the minimum sector size set the block size */
332 *ashift
= highbit64(MAX(block_size
, SPA_MINBLOCKSIZE
)) - 1;
334 /* Try to set the io scheduler elevator algorithm */
335 (void) vdev_elevator_switch(v
, zfs_vdev_scheduler
);
341 vdev_disk_close(vdev_t
*v
)
343 vdev_disk_t
*vd
= v
->vdev_tsd
;
345 if (v
->vdev_reopening
|| vd
== NULL
)
348 if (vd
->vd_bdev
!= NULL
)
349 vdev_bdev_close(vd
->vd_bdev
,
350 vdev_bdev_mode(spa_mode(v
->vdev_spa
)));
352 kmem_free(vd
, sizeof (vdev_disk_t
));
356 static dio_request_t
*
357 vdev_disk_dio_alloc(int bio_count
)
362 dr
= kmem_zalloc(sizeof (dio_request_t
) +
363 sizeof (struct bio
*) * bio_count
, KM_SLEEP
);
365 atomic_set(&dr
->dr_ref
, 0);
366 dr
->dr_bio_count
= bio_count
;
369 for (i
= 0; i
< dr
->dr_bio_count
; i
++)
370 dr
->dr_bio
[i
] = NULL
;
377 vdev_disk_dio_free(dio_request_t
*dr
)
381 for (i
= 0; i
< dr
->dr_bio_count
; i
++)
383 bio_put(dr
->dr_bio
[i
]);
385 kmem_free(dr
, sizeof (dio_request_t
) +
386 sizeof (struct bio
*) * dr
->dr_bio_count
);
390 vdev_disk_dio_get(dio_request_t
*dr
)
392 atomic_inc(&dr
->dr_ref
);
396 vdev_disk_dio_put(dio_request_t
*dr
)
398 int rc
= atomic_dec_return(&dr
->dr_ref
);
401 * Free the dio_request when the last reference is dropped and
402 * ensure zio_interpret is called only once with the correct zio
405 zio_t
*zio
= dr
->dr_zio
;
406 int error
= dr
->dr_error
;
408 vdev_disk_dio_free(dr
);
411 zio
->io_error
= error
;
412 ASSERT3S(zio
->io_error
, >=, 0);
414 vdev_disk_error(zio
);
416 zio_delay_interrupt(zio
);
423 BIO_END_IO_PROTO(vdev_disk_physio_completion
, bio
, error
)
425 dio_request_t
*dr
= bio
->bi_private
;
428 if (dr
->dr_error
== 0) {
429 #ifdef HAVE_1ARG_BIO_END_IO_T
430 dr
->dr_error
= BIO_END_IO_ERROR(bio
);
433 dr
->dr_error
= -(error
);
434 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
439 /* Drop reference acquired by __vdev_disk_physio */
440 rc
= vdev_disk_dio_put(dr
);
444 bio_map(struct bio
*bio
, void *bio_ptr
, unsigned int bio_size
)
446 unsigned int offset
, size
, i
;
449 offset
= offset_in_page(bio_ptr
);
450 for (i
= 0; i
< bio
->bi_max_vecs
; i
++) {
451 size
= PAGE_SIZE
- offset
;
459 if (is_vmalloc_addr(bio_ptr
))
460 page
= vmalloc_to_page(bio_ptr
);
462 page
= virt_to_page(bio_ptr
);
465 * Some network related block device uses tcp_sendpage, which
466 * doesn't behave well when using 0-count page, this is a
467 * safety net to catch them.
469 ASSERT3S(page_count(page
), >, 0);
471 if (bio_add_page(bio
, page
, size
, offset
) != size
)
483 bio_map_abd_off(struct bio
*bio
, abd_t
*abd
, unsigned int size
, size_t off
)
485 if (abd_is_linear(abd
))
486 return (bio_map(bio
, ((char *)abd_to_buf(abd
)) + off
, size
));
488 return (abd_scatter_bio_map_off(bio
, abd
, size
, off
));
492 vdev_submit_bio_impl(struct bio
*bio
)
494 #ifdef HAVE_1ARG_SUBMIT_BIO
502 vdev_submit_bio(struct bio
*bio
)
504 #ifdef HAVE_CURRENT_BIO_TAIL
505 struct bio
**bio_tail
= current
->bio_tail
;
506 current
->bio_tail
= NULL
;
507 vdev_submit_bio_impl(bio
);
508 current
->bio_tail
= bio_tail
;
510 struct bio_list
*bio_list
= current
->bio_list
;
511 current
->bio_list
= NULL
;
512 vdev_submit_bio_impl(bio
);
513 current
->bio_list
= bio_list
;
518 __vdev_disk_physio(struct block_device
*bdev
, zio_t
*zio
,
519 size_t io_size
, uint64_t io_offset
, int rw
, int flags
)
524 int bio_size
, bio_count
= 16;
525 int i
= 0, error
= 0;
526 #if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG)
527 struct blk_plug plug
;
531 ASSERT3U(io_offset
+ io_size
, <=, bdev
->bd_inode
->i_size
);
534 dr
= vdev_disk_dio_alloc(bio_count
);
536 return (SET_ERROR(ENOMEM
));
538 if (zio
&& !(zio
->io_flags
& (ZIO_FLAG_IO_RETRY
| ZIO_FLAG_TRYHARD
)))
539 bio_set_flags_failfast(bdev
, &flags
);
544 * When the IO size exceeds the maximum bio size for the request
545 * queue we are forced to break the IO in multiple bio's and wait
546 * for them all to complete. Ideally, all pool users will set
547 * their volume block size to match the maximum request size and
548 * the common case will be one bio per vdev IO request.
552 bio_offset
= io_offset
;
554 for (i
= 0; i
<= dr
->dr_bio_count
; i
++) {
556 /* Finished constructing bio's for given buffer */
561 * By default only 'bio_count' bio's per dio are allowed.
562 * However, if we find ourselves in a situation where more
563 * are needed we allocate a larger dio and warn the user.
565 if (dr
->dr_bio_count
== i
) {
566 vdev_disk_dio_free(dr
);
571 /* bio_alloc() with __GFP_WAIT never returns NULL */
572 dr
->dr_bio
[i
] = bio_alloc(GFP_NOIO
,
573 MIN(abd_nr_pages_off(zio
->io_abd
, bio_size
, abd_offset
),
575 if (unlikely(dr
->dr_bio
[i
] == NULL
)) {
576 vdev_disk_dio_free(dr
);
577 return (SET_ERROR(ENOMEM
));
580 /* Matching put called by vdev_disk_physio_completion */
581 vdev_disk_dio_get(dr
);
583 dr
->dr_bio
[i
]->bi_bdev
= bdev
;
584 BIO_BI_SECTOR(dr
->dr_bio
[i
]) = bio_offset
>> 9;
585 dr
->dr_bio
[i
]->bi_end_io
= vdev_disk_physio_completion
;
586 dr
->dr_bio
[i
]->bi_private
= dr
;
587 bio_set_op_attrs(dr
->dr_bio
[i
], rw
, flags
);
589 /* Remaining size is returned to become the new size */
590 bio_size
= bio_map_abd_off(dr
->dr_bio
[i
], zio
->io_abd
,
591 bio_size
, abd_offset
);
593 /* Advance in buffer and construct another bio if needed */
594 abd_offset
+= BIO_BI_SIZE(dr
->dr_bio
[i
]);
595 bio_offset
+= BIO_BI_SIZE(dr
->dr_bio
[i
]);
598 /* Extra reference to protect dio_request during vdev_submit_bio */
599 vdev_disk_dio_get(dr
);
601 #if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG)
602 if (dr
->dr_bio_count
> 1)
603 blk_start_plug(&plug
);
606 /* Submit all bio's associated with this dio */
607 for (i
= 0; i
< dr
->dr_bio_count
; i
++)
609 vdev_submit_bio(dr
->dr_bio
[i
]);
611 #if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG)
612 if (dr
->dr_bio_count
> 1)
613 blk_finish_plug(&plug
);
616 (void) vdev_disk_dio_put(dr
);
621 BIO_END_IO_PROTO(vdev_disk_io_flush_completion
, bio
, error
)
623 zio_t
*zio
= bio
->bi_private
;
624 #ifdef HAVE_1ARG_BIO_END_IO_T
625 zio
->io_error
= BIO_END_IO_ERROR(bio
);
627 zio
->io_error
= -error
;
630 if (zio
->io_error
&& (zio
->io_error
== EOPNOTSUPP
))
631 zio
->io_vd
->vdev_nowritecache
= B_TRUE
;
634 ASSERT3S(zio
->io_error
, >=, 0);
636 vdev_disk_error(zio
);
641 vdev_disk_io_flush(struct block_device
*bdev
, zio_t
*zio
)
643 struct request_queue
*q
;
646 q
= bdev_get_queue(bdev
);
648 return (SET_ERROR(ENXIO
));
650 bio
= bio_alloc(GFP_NOIO
, 0);
651 /* bio_alloc() with __GFP_WAIT never returns NULL */
652 if (unlikely(bio
== NULL
))
653 return (SET_ERROR(ENOMEM
));
655 bio
->bi_end_io
= vdev_disk_io_flush_completion
;
656 bio
->bi_private
= zio
;
659 vdev_submit_bio(bio
);
660 invalidate_bdev(bdev
);
666 vdev_disk_io_start(zio_t
*zio
)
668 vdev_t
*v
= zio
->io_vd
;
669 vdev_disk_t
*vd
= v
->vdev_tsd
;
670 int rw
, flags
, error
;
672 switch (zio
->io_type
) {
675 if (!vdev_readable(v
)) {
676 zio
->io_error
= SET_ERROR(ENXIO
);
681 switch (zio
->io_cmd
) {
682 case DKIOCFLUSHWRITECACHE
:
684 if (zfs_nocacheflush
)
687 if (v
->vdev_nowritecache
) {
688 zio
->io_error
= SET_ERROR(ENOTSUP
);
692 error
= vdev_disk_io_flush(vd
->vd_bdev
, zio
);
696 zio
->io_error
= error
;
701 zio
->io_error
= SET_ERROR(ENOTSUP
);
708 #if defined(HAVE_BLK_QUEUE_HAVE_BIO_RW_UNPLUG)
709 flags
= (1 << BIO_RW_UNPLUG
);
710 #elif defined(REQ_UNPLUG)
719 #if defined(HAVE_BLK_QUEUE_HAVE_BIO_RW_UNPLUG)
720 flags
= (1 << BIO_RW_UNPLUG
);
721 #elif defined(REQ_UNPLUG)
729 zio
->io_error
= SET_ERROR(ENOTSUP
);
734 zio
->io_target_timestamp
= zio_handle_io_delay(zio
);
735 error
= __vdev_disk_physio(vd
->vd_bdev
, zio
,
736 zio
->io_size
, zio
->io_offset
, rw
, flags
);
738 zio
->io_error
= error
;
745 vdev_disk_io_done(zio_t
*zio
)
748 * If the device returned EIO, we revalidate the media. If it is
749 * determined the media has changed this triggers the asynchronous
750 * removal of the device from the configuration.
752 if (zio
->io_error
== EIO
) {
753 vdev_t
*v
= zio
->io_vd
;
754 vdev_disk_t
*vd
= v
->vdev_tsd
;
756 if (check_disk_change(vd
->vd_bdev
)) {
757 vdev_bdev_invalidate(vd
->vd_bdev
);
758 v
->vdev_remove_wanted
= B_TRUE
;
759 spa_async_request(zio
->io_spa
, SPA_ASYNC_REMOVE
);
765 vdev_disk_hold(vdev_t
*vd
)
767 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_STATE
, RW_WRITER
));
769 /* We must have a pathname, and it must be absolute. */
770 if (vd
->vdev_path
== NULL
|| vd
->vdev_path
[0] != '/')
774 * Only prefetch path and devid info if the device has
777 if (vd
->vdev_tsd
!= NULL
)
780 /* XXX: Implement me as a vnode lookup for the device */
781 vd
->vdev_name_vp
= NULL
;
782 vd
->vdev_devid_vp
= NULL
;
786 vdev_disk_rele(vdev_t
*vd
)
788 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_STATE
, RW_WRITER
));
790 /* XXX: Implement me as a vnode rele for the device */
793 vdev_ops_t vdev_disk_ops
= {
803 VDEV_TYPE_DISK
, /* name of this vdev type */
804 B_TRUE
/* leaf vdev */
807 module_param(zfs_vdev_scheduler
, charp
, 0644);
808 MODULE_PARM_DESC(zfs_vdev_scheduler
, "I/O scheduler");