]> git.proxmox.com Git - mirror_zfs.git/blame - module/os/linux/zfs/vdev_disk.c
vdev_disk: add module parameter to select BIO submission method
[mirror_zfs.git] / module / os / linux / 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
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
60101509
BB
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.
1eacf2b3 26 * Copyright (c) 2012, 2019 by Delphix. All rights reserved.
06a19602 27 * Copyright (c) 2023, 2024, Klara Inc.
60101509
BB
28 */
29
30#include <sys/zfs_context.h>
e771de53 31#include <sys/spa_impl.h>
60101509
BB
32#include <sys/vdev_disk.h>
33#include <sys/vdev_impl.h>
1b939560 34#include <sys/vdev_trim.h>
a6255b7f 35#include <sys/abd.h>
60101509
BB
36#include <sys/fs/zfs.h>
37#include <sys/zio.h>
8e82ffba 38#include <linux/blkpg.h>
74d42600 39#include <linux/msdos_fs.h>
05805494 40#include <linux/vfs_compat.h>
1e767532
CK
41#ifdef HAVE_LINUX_BLK_CGROUP_HEADER
42#include <linux/blk-cgroup.h>
43#endif
60101509 44
386d6a75
RN
45/*
46 * Linux 6.8.x uses a bdev_handle as an instance/refcount for an underlying
47 * block_device. Since it carries the block_device inside, its convenient to
48 * just use the handle as a proxy. For pre-6.8, we just emulate this with
49 * a cast, since we don't need any of the other fields inside the handle.
50 */
51#ifdef HAVE_BDEV_OPEN_BY_PATH
52typedef struct bdev_handle zfs_bdev_handle_t;
53#define BDH_BDEV(bdh) ((bdh)->bdev)
54#define BDH_IS_ERR(bdh) (IS_ERR(bdh))
55#define BDH_PTR_ERR(bdh) (PTR_ERR(bdh))
56#define BDH_ERR_PTR(err) (ERR_PTR(err))
57#else
58typedef void zfs_bdev_handle_t;
59#define BDH_BDEV(bdh) ((struct block_device *)bdh)
60#define BDH_IS_ERR(bdh) (IS_ERR(BDH_BDEV(bdh)))
61#define BDH_PTR_ERR(bdh) (PTR_ERR(BDH_BDEV(bdh)))
62#define BDH_ERR_PTR(err) (ERR_PTR(err))
63#endif
64
d366c8fd 65typedef struct vdev_disk {
386d6a75 66 zfs_bdev_handle_t *vd_bdh;
d366c8fd
JL
67 krwlock_t vd_lock;
68} vdev_disk_t;
69
06a19602
RN
70/*
71 * Maximum number of segments to add to a bio (min 4). If this is higher than
72 * the maximum allowed by the device queue or the kernel itself, it will be
73 * clamped. Setting it to zero will cause the kernel's ideal size to be used.
74 */
75uint_t zfs_vdev_disk_max_segs = 0;
76
a25861dc
BB
77/*
78 * Unique identifier for the exclusive vdev holder.
79 */
8128bd89 80static void *zfs_vdev_holder = VDEV_HOLDER;
6839eed2 81
a25861dc
BB
82/*
83 * Wait up to zfs_vdev_open_timeout_ms milliseconds before determining the
84 * device is missing. The missing path may be transient since the links
85 * can be briefly removed and recreated in response to udev events.
86 */
f66ffe68 87static uint_t zfs_vdev_open_timeout_ms = 1000;
a25861dc
BB
88
89/*
90 * Size of the "reserved" partition, in blocks.
91 */
74d42600
SH
92#define EFI_MIN_RESV_SIZE (16 * 1024)
93
16f0fdad
MZ
94/*
95 * BIO request failfast mask.
96 */
97
98static unsigned int zfs_vdev_failfast_mask = 1;
99
43e8f6e3
CK
100#ifdef HAVE_BLK_MODE_T
101static blk_mode_t
102#else
60101509 103static fmode_t
43e8f6e3 104#endif
233d34e4 105vdev_bdev_mode(spa_mode_t spa_mode, boolean_t exclusive)
60101509 106{
43e8f6e3
CK
107#ifdef HAVE_BLK_MODE_T
108 blk_mode_t mode = 0;
109
110 if (spa_mode & SPA_MODE_READ)
111 mode |= BLK_OPEN_READ;
112
113 if (spa_mode & SPA_MODE_WRITE)
114 mode |= BLK_OPEN_WRITE;
233d34e4
BB
115
116 if (exclusive)
117 mode |= BLK_OPEN_EXCL;
43e8f6e3 118#else
60101509
BB
119 fmode_t mode = 0;
120
da92d5cb 121 if (spa_mode & SPA_MODE_READ)
60101509
BB
122 mode |= FMODE_READ;
123
da92d5cb 124 if (spa_mode & SPA_MODE_WRITE)
60101509 125 mode |= FMODE_WRITE;
233d34e4
BB
126
127 if (exclusive)
128 mode |= FMODE_EXCL;
43e8f6e3 129#endif
60101509 130
d1d7e268 131 return (mode);
60101509 132}
60101509 133
d441e85d
BB
134/*
135 * Returns the usable capacity (in bytes) for the partition or disk.
136 */
60101509 137static uint64_t
d441e85d 138bdev_capacity(struct block_device *bdev)
60101509 139{
d441e85d
BB
140 return (i_size_read(bdev->bd_inode));
141}
60101509 142
72ba4b2a
BB
143#if !defined(HAVE_BDEV_WHOLE)
144static inline struct block_device *
145bdev_whole(struct block_device *bdev)
146{
147 return (bdev->bd_contains);
148}
149#endif
150
bebdf52a
BB
151#if defined(HAVE_BDEVNAME)
152#define vdev_bdevname(bdev, name) bdevname(bdev, name)
153#else
154static inline void
155vdev_bdevname(struct block_device *bdev, char *name)
156{
157 snprintf(name, BDEVNAME_SIZE, "%pg", bdev);
158}
159#endif
160
d441e85d
BB
161/*
162 * Returns the maximum expansion capacity of the block device (in bytes).
163 *
164 * It is possible to expand a vdev when it has been created as a wholedisk
165 * and the containing block device has increased in capacity. Or when the
166 * partition containing the pool has been manually increased in size.
167 *
168 * This function is only responsible for calculating the potential expansion
169 * size so it can be reported by 'zpool list'. The efi_use_whole_disk() is
170 * responsible for verifying the expected partition layout in the wholedisk
171 * case, and updating the partition table if appropriate. Once the partition
172 * size has been increased the additional capacity will be visible using
173 * bdev_capacity().
0c637f31 174 *
175 * The returned maximum expansion capacity is always expected to be larger, or
176 * at the very least equal, to its usable capacity to prevent overestimating
177 * the pool expandsize.
d441e85d
BB
178 */
179static uint64_t
180bdev_max_capacity(struct block_device *bdev, uint64_t wholedisk)
181{
182 uint64_t psize;
183 int64_t available;
184
72ba4b2a 185 if (wholedisk && bdev != bdev_whole(bdev)) {
74d42600 186 /*
d441e85d
BB
187 * When reporting maximum expansion capacity for a wholedisk
188 * deduct any capacity which is expected to be lost due to
189 * alignment restrictions. Over reporting this value isn't
190 * harmful and would only result in slightly less capacity
191 * than expected post expansion.
0c637f31 192 * The estimated available space may be slightly smaller than
193 * bdev_capacity() for devices where the number of sectors is
194 * not a multiple of the alignment size and the partition layout
195 * is keeping less than PARTITION_END_ALIGNMENT bytes after the
196 * "reserved" EFI partition: in such cases return the device
197 * usable capacity.
74d42600 198 */
72ba4b2a 199 available = i_size_read(bdev_whole(bdev)->bd_inode) -
d441e85d
BB
200 ((EFI_MIN_RESV_SIZE + NEW_START_BLOCK +
201 PARTITION_END_ALIGNMENT) << SECTOR_BITS);
0c637f31 202 psize = MAX(available, bdev_capacity(bdev));
74d42600 203 } else {
d441e85d 204 psize = bdev_capacity(bdev);
74d42600 205 }
d441e85d
BB
206
207 return (psize);
60101509
BB
208}
209
d148e951
BB
210static void
211vdev_disk_error(zio_t *zio)
212{
c71c8c71 213 /*
214 * This function can be called in interrupt context, for instance while
215 * handling IRQs coming from a misbehaving disk device; use printk()
216 * which is safe from any context.
217 */
218 printk(KERN_WARNING "zio pool=%s vdev=%s error=%d type=%d "
4938d01d 219 "offset=%llu size=%llu flags=%llu\n", spa_name(zio->io_spa),
c71c8c71 220 zio->io_vd->vdev_path, zio->io_error, zio->io_type,
221 (u_longlong_t)zio->io_offset, (u_longlong_t)zio->io_size,
222 zio->io_flags);
d148e951
BB
223}
224
55c12724
AH
225static void
226vdev_disk_kobj_evt_post(vdev_t *v)
227{
228 vdev_disk_t *vd = v->vdev_tsd;
386d6a75
RN
229 if (vd && vd->vd_bdh) {
230 spl_signal_kobj_evt(BDH_BDEV(vd->vd_bdh));
55c12724
AH
231 } else {
232 vdev_dbgmsg(v, "vdev_disk_t is NULL for VDEV:%s\n",
233 v->vdev_path);
234 }
235}
236
386d6a75
RN
237static zfs_bdev_handle_t *
238vdev_blkdev_get_by_path(const char *path, spa_mode_t mode, void *holder)
43e8f6e3 239{
386d6a75
RN
240#if defined(HAVE_BDEV_OPEN_BY_PATH)
241 return (bdev_open_by_path(path,
242 vdev_bdev_mode(mode, B_TRUE), holder, NULL));
243#elif defined(HAVE_BLKDEV_GET_BY_PATH_4ARG)
43e8f6e3 244 return (blkdev_get_by_path(path,
386d6a75 245 vdev_bdev_mode(mode, B_TRUE), holder, NULL));
43e8f6e3
CK
246#else
247 return (blkdev_get_by_path(path,
233d34e4 248 vdev_bdev_mode(mode, B_TRUE), holder));
43e8f6e3
CK
249#endif
250}
251
252static void
386d6a75 253vdev_blkdev_put(zfs_bdev_handle_t *bdh, spa_mode_t mode, void *holder)
43e8f6e3 254{
386d6a75
RN
255#if defined(HAVE_BDEV_RELEASE)
256 return (bdev_release(bdh));
257#elif defined(HAVE_BLKDEV_PUT_HOLDER)
258 return (blkdev_put(BDH_BDEV(bdh), holder));
43e8f6e3 259#else
386d6a75
RN
260 return (blkdev_put(BDH_BDEV(bdh),
261 vdev_bdev_mode(mode, B_TRUE)));
43e8f6e3
CK
262#endif
263}
264
60101509 265static int
1bd201e7 266vdev_disk_open(vdev_t *v, uint64_t *psize, uint64_t *max_psize,
6fe3498c 267 uint64_t *logical_ashift, uint64_t *physical_ashift)
60101509 268{
386d6a75 269 zfs_bdev_handle_t *bdh;
43e8f6e3 270#ifdef HAVE_BLK_MODE_T
233d34e4 271 blk_mode_t mode = vdev_bdev_mode(spa_mode(v->vdev_spa), B_FALSE);
43e8f6e3 272#else
233d34e4 273 fmode_t mode = vdev_bdev_mode(spa_mode(v->vdev_spa), B_FALSE);
43e8f6e3 274#endif
a25861dc 275 hrtime_t timeout = MSEC2NSEC(zfs_vdev_open_timeout_ms);
60101509 276 vdev_disk_t *vd;
60101509
BB
277
278 /* Must have a pathname and it must be absolute. */
279 if (v->vdev_path == NULL || v->vdev_path[0] != '/') {
280 v->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
d441e85d 281 vdev_dbgmsg(v, "invalid vdev_path");
2d82ea8b 282 return (SET_ERROR(EINVAL));
60101509
BB
283 }
284
0d8103d9 285 /*
d441e85d 286 * Reopen the device if it is currently open. When expanding a
8e82ffba
GW
287 * partition force re-scanning the partition table if userland
288 * did not take care of this already. We need to do this while closed
d441e85d
BB
289 * in order to get an accurate updated block device size. Then
290 * since udev may need to recreate the device links increase the
a25861dc 291 * open retry timeout before reporting the device as unavailable.
0d8103d9 292 */
d441e85d
BB
293 vd = v->vdev_tsd;
294 if (vd) {
295 char disk_name[BDEVNAME_SIZE + 6] = "/dev/";
296 boolean_t reread_part = B_FALSE;
0d8103d9 297
d441e85d 298 rw_enter(&vd->vd_lock, RW_WRITER);
386d6a75
RN
299 bdh = vd->vd_bdh;
300 vd->vd_bdh = NULL;
d441e85d 301
386d6a75
RN
302 if (bdh) {
303 struct block_device *bdev = BDH_BDEV(bdh);
72ba4b2a 304 if (v->vdev_expanding && bdev != bdev_whole(bdev)) {
bebdf52a 305 vdev_bdevname(bdev_whole(bdev), disk_name + 5);
8e82ffba
GW
306 /*
307 * If userland has BLKPG_RESIZE_PARTITION,
308 * then it should have updated the partition
309 * table already. We can detect this by
310 * comparing our current physical size
311 * with that of the device. If they are
312 * the same, then we must not have
313 * BLKPG_RESIZE_PARTITION or it failed to
314 * update the partition table online. We
315 * fallback to rescanning the partition
316 * table from the kernel below. However,
317 * if the capacity already reflects the
318 * updated partition, then we skip
319 * rescanning the partition table here.
320 */
321 if (v->vdev_psize == bdev_capacity(bdev))
322 reread_part = B_TRUE;
d441e85d
BB
323 }
324
386d6a75 325 vdev_blkdev_put(bdh, mode, zfs_vdev_holder);
d441e85d
BB
326 }
327
328 if (reread_part) {
386d6a75
RN
329 bdh = vdev_blkdev_get_by_path(disk_name, mode,
330 zfs_vdev_holder);
331 if (!BDH_IS_ERR(bdh)) {
332 int error =
333 vdev_bdev_reread_part(BDH_BDEV(bdh));
334 vdev_blkdev_put(bdh, mode, zfs_vdev_holder);
a25861dc
BB
335 if (error == 0) {
336 timeout = MSEC2NSEC(
337 zfs_vdev_open_timeout_ms * 2);
338 }
d441e85d
BB
339 }
340 }
341 } else {
342 vd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
343
344 rw_init(&vd->vd_lock, NULL, RW_DEFAULT, NULL);
345 rw_enter(&vd->vd_lock, RW_WRITER);
346 }
60101509
BB
347
348 /*
349 * Devices are always opened by the path provided at configuration
350 * time. This means that if the provided path is a udev by-id path
d441e85d 351 * then drives may be re-cabled without an issue. If the provided
4e95cc99 352 * path is a udev by-path path, then the physical location information
60101509
BB
353 * will be preserved. This can be critical for more complicated
354 * configurations where drives are located in specific physical
d441e85d
BB
355 * locations to maximize the systems tolerance to component failure.
356 *
4e95cc99 357 * Alternatively, you can provide your own udev rule to flexibly map
60101509 358 * the drives as you see fit. It is not advised that you use the
4e95cc99 359 * /dev/[hd]d devices which may be reordered due to probing order.
60101509
BB
360 * Devices in the wrong locations will be detected by the higher
361 * level vdev validation.
2d82ea8b
BB
362 *
363 * The specified paths may be briefly removed and recreated in
364 * response to udev events. This should be exceptionally unlikely
365 * because the zpool command makes every effort to verify these paths
366 * have already settled prior to reaching this point. Therefore,
367 * a ENOENT failure at this point is highly likely to be transient
368 * and it is reasonable to sleep and retry before giving up. In
369 * practice delays have been observed to be on the order of 100ms.
77e2756d
BB
370 *
371 * When ERESTARTSYS is returned it indicates the block device is
372 * a zvol which could not be opened due to the deadlock detection
373 * logic in zvol_open(). Extend the timeout and retry the open
374 * subsequent attempts are expected to eventually succeed.
60101509 375 */
a25861dc 376 hrtime_t start = gethrtime();
386d6a75
RN
377 bdh = BDH_ERR_PTR(-ENXIO);
378 while (BDH_IS_ERR(bdh) && ((gethrtime() - start) < timeout)) {
379 bdh = vdev_blkdev_get_by_path(v->vdev_path, mode,
380 zfs_vdev_holder);
381 if (unlikely(BDH_PTR_ERR(bdh) == -ENOENT)) {
55c12724
AH
382 /*
383 * There is no point of waiting since device is removed
384 * explicitly
385 */
386 if (v->vdev_removed)
387 break;
388
d441e85d 389 schedule_timeout(MSEC_TO_TICK(10));
386d6a75 390 } else if (unlikely(BDH_PTR_ERR(bdh) == -ERESTARTSYS)) {
77e2756d
BB
391 timeout = MSEC2NSEC(zfs_vdev_open_timeout_ms * 10);
392 continue;
386d6a75 393 } else if (BDH_IS_ERR(bdh)) {
2d82ea8b
BB
394 break;
395 }
396 }
397
386d6a75
RN
398 if (BDH_IS_ERR(bdh)) {
399 int error = -BDH_PTR_ERR(bdh);
a25861dc
BB
400 vdev_dbgmsg(v, "open error=%d timeout=%llu/%llu", error,
401 (u_longlong_t)(gethrtime() - start),
402 (u_longlong_t)timeout);
386d6a75 403 vd->vd_bdh = NULL;
d441e85d
BB
404 v->vdev_tsd = vd;
405 rw_exit(&vd->vd_lock);
406 return (SET_ERROR(error));
407 } else {
386d6a75 408 vd->vd_bdh = bdh;
d441e85d
BB
409 v->vdev_tsd = vd;
410 rw_exit(&vd->vd_lock);
60101509
BB
411 }
412
386d6a75
RN
413 struct block_device *bdev = BDH_BDEV(vd->vd_bdh);
414
0d8103d9 415 /* Determine the physical block size */
386d6a75 416 int physical_block_size = bdev_physical_block_size(bdev);
6fe3498c
RM
417
418 /* Determine the logical block size */
386d6a75 419 int logical_block_size = bdev_logical_block_size(bdev);
60101509 420
60101509
BB
421 /* Clear the nowritecache bit, causes vdev_reopen() to try again. */
422 v->vdev_nowritecache = B_FALSE;
423
1b939560 424 /* Set when device reports it supports TRIM. */
386d6a75 425 v->vdev_has_trim = bdev_discard_supported(bdev);
1b939560
BB
426
427 /* Set when device reports it supports secure TRIM. */
386d6a75 428 v->vdev_has_securetrim = bdev_secure_discard_supported(bdev);
1b939560 429
fb40095f 430 /* Inform the ZIO pipeline that we are non-rotational */
386d6a75 431 v->vdev_nonrot = blk_queue_nonrot(bdev_get_queue(bdev));
fb40095f 432
d441e85d 433 /* Physical volume size in bytes for the partition */
386d6a75 434 *psize = bdev_capacity(bdev);
d441e85d
BB
435
436 /* Physical volume size in bytes including possible expansion space */
386d6a75 437 *max_psize = bdev_max_capacity(bdev, v->vdev_wholedisk);
1bd201e7 438
60101509 439 /* Based on the minimum sector size set the block size */
6fe3498c
RM
440 *physical_ashift = highbit64(MAX(physical_block_size,
441 SPA_MINBLOCKSIZE)) - 1;
442
443 *logical_ashift = highbit64(MAX(logical_block_size,
444 SPA_MINBLOCKSIZE)) - 1;
60101509 445
d1d7e268 446 return (0);
60101509
BB
447}
448
449static void
450vdev_disk_close(vdev_t *v)
451{
452 vdev_disk_t *vd = v->vdev_tsd;
453
0d8103d9 454 if (v->vdev_reopening || vd == NULL)
60101509
BB
455 return;
456
386d6a75
RN
457 if (vd->vd_bdh != NULL) {
458 vdev_blkdev_put(vd->vd_bdh, spa_mode(v->vdev_spa),
43e8f6e3 459 zfs_vdev_holder);
d441e85d 460 }
60101509 461
d441e85d 462 rw_destroy(&vd->vd_lock);
d1d7e268 463 kmem_free(vd, sizeof (vdev_disk_t));
60101509
BB
464 v->vdev_tsd = NULL;
465}
466
bbb1b6ce 467static inline void
3b86aeb2 468vdev_submit_bio_impl(struct bio *bio)
bbb1b6ce
BB
469{
470#ifdef HAVE_1ARG_SUBMIT_BIO
453c63e9 471 (void) submit_bio(bio);
bbb1b6ce 472#else
a3fbe2b9 473 (void) submit_bio(bio_data_dir(bio), bio);
bbb1b6ce
BB
474#endif
475}
476
2e407941
BB
477/*
478 * preempt_schedule_notrace is GPL-only which breaks the ZFS build, so
479 * replace it with preempt_schedule under the following condition:
480 */
481#if defined(CONFIG_ARM64) && \
482 defined(CONFIG_PREEMPTION) && \
483 defined(CONFIG_BLK_CGROUP)
484#define preempt_schedule_notrace(x) preempt_schedule(x)
485#endif
486
5f264996
BB
487/*
488 * As for the Linux 5.18 kernel bio_alloc() expects a block_device struct
489 * as an argument removing the need to set it with bio_set_dev(). This
490 * removes the need for all of the following compatibility code.
491 */
492#if !defined(HAVE_BIO_ALLOC_4ARG)
493
26a85659
BB
494#ifdef HAVE_BIO_SET_DEV
495#if defined(CONFIG_BLK_CGROUP) && defined(HAVE_BIO_SET_DEV_GPL_ONLY)
bd0d24e0
BB
496/*
497 * The Linux 5.5 kernel updated percpu_ref_tryget() which is inlined by
498 * blkg_tryget() to use rcu_read_lock() instead of rcu_read_lock_sched().
499 * As a side effect the function was converted to GPL-only. Define our
500 * own version when needed which uses rcu_read_lock_sched().
036e846a
RS
501 *
502 * The Linux 5.17 kernel split linux/blk-cgroup.h into a private and a public
503 * part, moving blkg_tryget into the private one. Define our own version.
bd0d24e0 504 */
036e846a 505#if defined(HAVE_BLKG_TRYGET_GPL_ONLY) || !defined(HAVE_BLKG_TRYGET)
bd0d24e0
BB
506static inline bool
507vdev_blkg_tryget(struct blkcg_gq *blkg)
508{
509 struct percpu_ref *ref = &blkg->refcnt;
510 unsigned long __percpu *count;
511 bool rc;
512
513 rcu_read_lock_sched();
514
515 if (__ref_is_percpu(ref, &count)) {
516 this_cpu_inc(*count);
517 rc = true;
518 } else {
838a2490
CK
519#ifdef ZFS_PERCPU_REF_COUNT_IN_DATA
520 rc = atomic_long_inc_not_zero(&ref->data->count);
521#else
bd0d24e0 522 rc = atomic_long_inc_not_zero(&ref->count);
838a2490 523#endif
bd0d24e0
BB
524 }
525
526 rcu_read_unlock_sched();
527
528 return (rc);
529}
036e846a 530#else
bd0d24e0
BB
531#define vdev_blkg_tryget(bg) blkg_tryget(bg)
532#endif
d08b99ac 533#ifdef HAVE_BIO_SET_DEV_MACRO
26a85659
BB
534/*
535 * The Linux 5.0 kernel updated the bio_set_dev() macro so it calls the
536 * GPL-only bio_associate_blkg() symbol thus inadvertently converting
537 * the entire macro. Provide a minimal version which always assigns the
538 * request queue's root_blkg to the bio.
539 */
540static inline void
541vdev_bio_associate_blkg(struct bio *bio)
542{
d939930f
CK
543#if defined(HAVE_BIO_BDEV_DISK)
544 struct request_queue *q = bio->bi_bdev->bd_disk->queue;
545#else
26a85659 546 struct request_queue *q = bio->bi_disk->queue;
d939930f 547#endif
26a85659
BB
548
549 ASSERT3P(q, !=, NULL);
26a85659
BB
550 ASSERT3P(bio->bi_blkg, ==, NULL);
551
bd0d24e0 552 if (q->root_blkg && vdev_blkg_tryget(q->root_blkg))
26a85659
BB
553 bio->bi_blkg = q->root_blkg;
554}
d08b99ac 555
26a85659 556#define bio_associate_blkg vdev_bio_associate_blkg
d08b99ac
CK
557#else
558static inline void
559vdev_bio_set_dev(struct bio *bio, struct block_device *bdev)
560{
561#if defined(HAVE_BIO_BDEV_DISK)
562 struct request_queue *q = bdev->bd_disk->queue;
563#else
564 struct request_queue *q = bio->bi_disk->queue;
565#endif
566 bio_clear_flag(bio, BIO_REMAPPED);
567 if (bio->bi_bdev != bdev)
568 bio_clear_flag(bio, BIO_THROTTLED);
569 bio->bi_bdev = bdev;
570
571 ASSERT3P(q, !=, NULL);
572 ASSERT3P(bio->bi_blkg, ==, NULL);
573
574 if (q->root_blkg && vdev_blkg_tryget(q->root_blkg))
575 bio->bi_blkg = q->root_blkg;
576}
577#define bio_set_dev vdev_bio_set_dev
578#endif
26a85659
BB
579#endif
580#else
581/*
582 * Provide a bio_set_dev() helper macro for pre-Linux 4.14 kernels.
583 */
787acae0
GDN
584static inline void
585bio_set_dev(struct bio *bio, struct block_device *bdev)
586{
587 bio->bi_bdev = bdev;
588}
26a85659 589#endif /* HAVE_BIO_SET_DEV */
5f264996 590#endif /* !HAVE_BIO_ALLOC_4ARG */
787acae0 591
37f9dac5 592static inline void
3b86aeb2 593vdev_submit_bio(struct bio *bio)
37f9dac5 594{
37f9dac5
RY
595 struct bio_list *bio_list = current->bio_list;
596 current->bio_list = NULL;
3b86aeb2 597 vdev_submit_bio_impl(bio);
37f9dac5 598 current->bio_list = bio_list;
37f9dac5
RY
599}
600
5f264996
BB
601static inline struct bio *
602vdev_bio_alloc(struct block_device *bdev, gfp_t gfp_mask,
603 unsigned short nr_vecs)
604{
605 struct bio *bio;
606
d1325b4f 607#ifdef HAVE_BIO_ALLOC_4ARG
5f264996
BB
608 bio = bio_alloc(bdev, nr_vecs, 0, gfp_mask);
609#else
610 bio = bio_alloc(gfp_mask, nr_vecs);
611 if (likely(bio != NULL))
612 bio_set_dev(bio, bdev);
d1325b4f
AZ
613#endif
614
5f264996
BB
615 return (bio);
616}
617
06a19602
RN
618static inline uint_t
619vdev_bio_max_segs(struct block_device *bdev)
620{
621 /*
622 * Smallest of the device max segs and the tuneable max segs. Minimum
623 * 4, so there's room to finish split pages if they come up.
624 */
625 const uint_t dev_max_segs = queue_max_segments(bdev_get_queue(bdev));
626 const uint_t tune_max_segs = (zfs_vdev_disk_max_segs > 0) ?
627 MAX(4, zfs_vdev_disk_max_segs) : dev_max_segs;
628 const uint_t max_segs = MIN(tune_max_segs, dev_max_segs);
629
630#ifdef HAVE_BIO_MAX_SEGS
631 return (bio_max_segs(max_segs));
632#else
633 return (MIN(max_segs, BIO_MAX_PAGES));
634#endif
635}
636
637static inline uint_t
638vdev_bio_max_bytes(struct block_device *bdev)
639{
640 return (queue_max_sectors(bdev_get_queue(bdev)) << 9);
641}
642
643
644/*
645 * Virtual block IO object (VBIO)
646 *
647 * Linux block IO (BIO) objects have a limit on how many data segments (pages)
648 * they can hold. Depending on how they're allocated and structured, a large
649 * ZIO can require more than one BIO to be submitted to the kernel, which then
650 * all have to complete before we can return the completed ZIO back to ZFS.
651 *
652 * A VBIO is a wrapper around multiple BIOs, carrying everything needed to
653 * translate a ZIO down into the kernel block layer and back again.
654 *
655 * Note that these are only used for data ZIOs (read/write). Meta-operations
656 * (flush/trim) don't need multiple BIOs and so can just make the call
657 * directly.
658 */
659typedef struct {
660 zio_t *vbio_zio; /* parent zio */
661
662 struct block_device *vbio_bdev; /* blockdev to submit bios to */
663
664 abd_t *vbio_abd; /* abd carrying borrowed linear buf */
665
666 atomic_t vbio_ref; /* bio refcount */
667 int vbio_error; /* error from failed bio */
668
669 uint_t vbio_max_segs; /* max segs per bio */
670
671 uint_t vbio_max_bytes; /* max bytes per bio */
672 uint_t vbio_lbs_mask; /* logical block size mask */
673
674 uint64_t vbio_offset; /* start offset of next bio */
675
676 struct bio *vbio_bio; /* pointer to the current bio */
677 struct bio *vbio_bios; /* list of all bios */
678} vbio_t;
679
680static vbio_t *
681vbio_alloc(zio_t *zio, struct block_device *bdev)
682{
683 vbio_t *vbio = kmem_zalloc(sizeof (vbio_t), KM_SLEEP);
684
685 vbio->vbio_zio = zio;
686 vbio->vbio_bdev = bdev;
687 atomic_set(&vbio->vbio_ref, 0);
688 vbio->vbio_max_segs = vdev_bio_max_segs(bdev);
689 vbio->vbio_max_bytes = vdev_bio_max_bytes(bdev);
690 vbio->vbio_lbs_mask = ~(bdev_logical_block_size(bdev)-1);
691 vbio->vbio_offset = zio->io_offset;
692
693 return (vbio);
694}
695
696static int
697vbio_add_page(vbio_t *vbio, struct page *page, uint_t size, uint_t offset)
698{
699 struct bio *bio;
700 uint_t ssize;
701
702 while (size > 0) {
703 bio = vbio->vbio_bio;
704 if (bio == NULL) {
705 /* New BIO, allocate and set up */
706 bio = vdev_bio_alloc(vbio->vbio_bdev, GFP_NOIO,
707 vbio->vbio_max_segs);
708 if (unlikely(bio == NULL))
709 return (SET_ERROR(ENOMEM));
710 BIO_BI_SECTOR(bio) = vbio->vbio_offset >> 9;
711
712 bio->bi_next = vbio->vbio_bios;
713 vbio->vbio_bios = vbio->vbio_bio = bio;
714 }
715
716 /*
717 * Only load as much of the current page data as will fit in
718 * the space left in the BIO, respecting lbs alignment. Older
719 * kernels will error if we try to overfill the BIO, while
720 * newer ones will accept it and split the BIO. This ensures
721 * everything works on older kernels, and avoids an additional
722 * overhead on the new.
723 */
724 ssize = MIN(size, (vbio->vbio_max_bytes - BIO_BI_SIZE(bio)) &
725 vbio->vbio_lbs_mask);
726 if (ssize > 0 &&
727 bio_add_page(bio, page, ssize, offset) == ssize) {
728 /* Accepted, adjust and load any remaining. */
729 size -= ssize;
730 offset += ssize;
731 continue;
732 }
733
734 /* No room, set up for a new BIO and loop */
735 vbio->vbio_offset += BIO_BI_SIZE(bio);
736
737 /* Signal new BIO allocation wanted */
738 vbio->vbio_bio = NULL;
739 }
740
741 return (0);
742}
743
744BIO_END_IO_PROTO(vdev_disk_io_rw_completion, bio, error);
745static void vbio_put(vbio_t *vbio);
746
747static void
748vbio_submit(vbio_t *vbio, int flags)
749{
750 ASSERT(vbio->vbio_bios);
751 struct bio *bio = vbio->vbio_bios;
752 vbio->vbio_bio = vbio->vbio_bios = NULL;
753
754 /*
755 * We take a reference for each BIO as we submit it, plus one to
756 * protect us from BIOs completing before we're done submitting them
757 * all, causing vbio_put() to free vbio out from under us and/or the
758 * zio to be returned before all its IO has completed.
759 */
760 atomic_set(&vbio->vbio_ref, 1);
761
762 /*
763 * If we're submitting more than one BIO, inform the block layer so
764 * it can batch them if it wants.
765 */
766 struct blk_plug plug;
767 boolean_t do_plug = (bio->bi_next != NULL);
768 if (do_plug)
769 blk_start_plug(&plug);
770
771 /* Submit all the BIOs */
772 while (bio != NULL) {
773 atomic_inc(&vbio->vbio_ref);
774
775 struct bio *next = bio->bi_next;
776 bio->bi_next = NULL;
777
778 bio->bi_end_io = vdev_disk_io_rw_completion;
779 bio->bi_private = vbio;
780 bio_set_op_attrs(bio,
781 vbio->vbio_zio->io_type == ZIO_TYPE_WRITE ?
782 WRITE : READ, flags);
783
784 vdev_submit_bio(bio);
785
786 bio = next;
787 }
788
789 /* Finish the batch */
790 if (do_plug)
791 blk_finish_plug(&plug);
792
793 /* Release the extra reference */
794 vbio_put(vbio);
795}
796
797static void
798vbio_return_abd(vbio_t *vbio)
799{
800 zio_t *zio = vbio->vbio_zio;
801 if (vbio->vbio_abd == NULL)
802 return;
803
804 /*
805 * If we copied the ABD before issuing it, clean up and return the copy
806 * to the ADB, with changes if appropriate.
807 */
808 void *buf = abd_to_buf(vbio->vbio_abd);
809 abd_free(vbio->vbio_abd);
810 vbio->vbio_abd = NULL;
811
812 if (zio->io_type == ZIO_TYPE_READ)
813 abd_return_buf_copy(zio->io_abd, buf, zio->io_size);
814 else
815 abd_return_buf(zio->io_abd, buf, zio->io_size);
816}
817
818static void
819vbio_free(vbio_t *vbio)
820{
821 VERIFY0(atomic_read(&vbio->vbio_ref));
822
823 vbio_return_abd(vbio);
824
825 kmem_free(vbio, sizeof (vbio_t));
826}
827
828static void
829vbio_put(vbio_t *vbio)
830{
831 if (atomic_dec_return(&vbio->vbio_ref) > 0)
832 return;
833
834 /*
835 * This was the last reference, so the entire IO is completed. Clean
836 * up and submit it for processing.
837 */
838
839 /*
840 * Get any data buf back to the original ABD, if necessary. We do this
841 * now so we can get the ZIO into the pipeline as quickly as possible,
842 * and then do the remaining cleanup after.
843 */
844 vbio_return_abd(vbio);
845
846 zio_t *zio = vbio->vbio_zio;
847
848 /*
849 * Set the overall error. If multiple BIOs returned an error, only the
850 * first will be taken; the others are dropped (see
851 * vdev_disk_io_rw_completion()). Its pretty much impossible for
852 * multiple IOs to the same device to fail with different errors, so
853 * there's no real risk.
854 */
855 zio->io_error = vbio->vbio_error;
856 if (zio->io_error)
857 vdev_disk_error(zio);
858
859 /* All done, submit for processing */
860 zio_delay_interrupt(zio);
861
862 /* Finish cleanup */
863 vbio_free(vbio);
864}
865
866BIO_END_IO_PROTO(vdev_disk_io_rw_completion, bio, error)
867{
868 vbio_t *vbio = bio->bi_private;
869
870 if (vbio->vbio_error == 0) {
871#ifdef HAVE_1ARG_BIO_END_IO_T
872 vbio->vbio_error = BIO_END_IO_ERROR(bio);
873#else
874 if (error)
875 vbio->vbio_error = -(error);
876 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
877 vbio->vbio_error = EIO;
878#endif
879 }
880
881 /*
882 * Destroy the BIO. This is safe to do; the vbio owns its data and the
883 * kernel won't touch it again after the completion function runs.
884 */
885 bio_put(bio);
886
887 /* Drop this BIOs reference acquired by vbio_submit() */
888 vbio_put(vbio);
889}
890
891/*
892 * Iterator callback to count ABD pages and check their size & alignment.
893 *
894 * On Linux, each BIO segment can take a page pointer, and an offset+length of
895 * the data within that page. A page can be arbitrarily large ("compound"
896 * pages) but we still have to ensure the data portion is correctly sized and
897 * aligned to the logical block size, to ensure that if the kernel wants to
898 * split the BIO, the two halves will still be properly aligned.
899 */
900typedef struct {
901 uint_t bmask;
902 uint_t npages;
903 uint_t end;
904} vdev_disk_check_pages_t;
905
906static int
907vdev_disk_check_pages_cb(struct page *page, size_t off, size_t len, void *priv)
908{
909 vdev_disk_check_pages_t *s = priv;
910
911 /*
912 * If we didn't finish on a block size boundary last time, then there
913 * would be a gap if we tried to use this ABD as-is, so abort.
914 */
915 if (s->end != 0)
916 return (1);
917
918 /*
919 * Note if we're taking less than a full block, so we can check it
920 * above on the next call.
921 */
922 s->end = len & s->bmask;
923
924 /* All blocks after the first must start on a block size boundary. */
925 if (s->npages != 0 && (off & s->bmask) != 0)
926 return (1);
927
928 s->npages++;
929 return (0);
930}
931
932/*
933 * Check if we can submit the pages in this ABD to the kernel as-is. Returns
934 * the number of pages, or 0 if it can't be submitted like this.
935 */
936static boolean_t
937vdev_disk_check_pages(abd_t *abd, uint64_t size, struct block_device *bdev)
938{
939 vdev_disk_check_pages_t s = {
940 .bmask = bdev_logical_block_size(bdev)-1,
941 .npages = 0,
942 .end = 0,
943 };
944
945 if (abd_iterate_page_func(abd, 0, size, vdev_disk_check_pages_cb, &s))
946 return (B_FALSE);
947
948 return (B_TRUE);
949}
950
951/* Iterator callback to submit ABD pages to the vbio. */
952static int
953vdev_disk_fill_vbio_cb(struct page *page, size_t off, size_t len, void *priv)
954{
955 vbio_t *vbio = priv;
956 return (vbio_add_page(vbio, page, len, off));
957}
958
959static int
960vdev_disk_io_rw(zio_t *zio)
961{
962 vdev_t *v = zio->io_vd;
963 vdev_disk_t *vd = v->vdev_tsd;
964 struct block_device *bdev = BDH_BDEV(vd->vd_bdh);
965 int flags = 0;
966
967 /*
968 * Accessing outside the block device is never allowed.
969 */
970 if (zio->io_offset + zio->io_size > bdev->bd_inode->i_size) {
971 vdev_dbgmsg(zio->io_vd,
972 "Illegal access %llu size %llu, device size %llu",
973 (u_longlong_t)zio->io_offset,
974 (u_longlong_t)zio->io_size,
975 (u_longlong_t)i_size_read(bdev->bd_inode));
976 return (SET_ERROR(EIO));
977 }
978
979 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)) &&
980 v->vdev_failfast == B_TRUE) {
981 bio_set_flags_failfast(bdev, &flags, zfs_vdev_failfast_mask & 1,
982 zfs_vdev_failfast_mask & 2, zfs_vdev_failfast_mask & 4);
983 }
984
985 /*
986 * Check alignment of the incoming ABD. If any part of it would require
987 * submitting a page that is not aligned to the logical block size,
988 * then we take a copy into a linear buffer and submit that instead.
989 * This should be impossible on a 512b LBS, and fairly rare on 4K,
990 * usually requiring abnormally-small data blocks (eg gang blocks)
991 * mixed into the same ABD as larger ones (eg aggregated).
992 */
993 abd_t *abd = zio->io_abd;
994 if (!vdev_disk_check_pages(abd, zio->io_size, bdev)) {
995 void *buf;
996 if (zio->io_type == ZIO_TYPE_READ)
997 buf = abd_borrow_buf(zio->io_abd, zio->io_size);
998 else
999 buf = abd_borrow_buf_copy(zio->io_abd, zio->io_size);
1000
1001 /*
1002 * Wrap the copy in an abd_t, so we can use the same iterators
1003 * to count and fill the vbio later.
1004 */
1005 abd = abd_get_from_buf(buf, zio->io_size);
1006
1007 /*
1008 * False here would mean the borrowed copy has an invalid
1009 * alignment too, which would mean we've somehow been passed a
1010 * linear ABD with an interior page that has a non-zero offset
1011 * or a size not a multiple of PAGE_SIZE. This is not possible.
1012 * It would mean either zio_buf_alloc() or its underlying
1013 * allocators have done something extremely strange, or our
1014 * math in vdev_disk_check_pages() is wrong. In either case,
1015 * something in seriously wrong and its not safe to continue.
1016 */
1017 VERIFY(vdev_disk_check_pages(abd, zio->io_size, bdev));
1018 }
1019
1020 /* Allocate vbio, with a pointer to the borrowed ABD if necessary */
1021 int error = 0;
1022 vbio_t *vbio = vbio_alloc(zio, bdev);
1023 if (abd != zio->io_abd)
1024 vbio->vbio_abd = abd;
1025
1026 /* Fill it with pages */
1027 error = abd_iterate_page_func(abd, 0, zio->io_size,
1028 vdev_disk_fill_vbio_cb, vbio);
1029 if (error != 0) {
1030 vbio_free(vbio);
1031 return (error);
1032 }
1033
1034 vbio_submit(vbio, flags);
1035 return (0);
1036}
1037
f3b85d70
RN
1038/* ========== */
1039
1040/*
06a19602
RN
1041 * This is the classic, battle-tested BIO submission code. Until we're totally
1042 * sure that the new code is safe and correct in all cases, this will remain
1043 * available and can be enabled by setting zfs_vdev_disk_classic=1 at module
1044 * load time.
f3b85d70
RN
1045 *
1046 * These functions have been renamed to vdev_classic_* to make it clear what
1047 * they belong to, but their implementations are unchanged.
1048 */
1049
1050/*
1051 * Virtual device vector for disks.
1052 */
1053typedef struct dio_request {
1054 zio_t *dr_zio; /* Parent ZIO */
1055 atomic_t dr_ref; /* References */
1056 int dr_error; /* Bio error */
1057 int dr_bio_count; /* Count of bio's */
1058 struct bio *dr_bio[]; /* Attached bio's */
1059} dio_request_t;
1060
1061static dio_request_t *
1062vdev_classic_dio_alloc(int bio_count)
1063{
1064 dio_request_t *dr = kmem_zalloc(sizeof (dio_request_t) +
1065 sizeof (struct bio *) * bio_count, KM_SLEEP);
1066 atomic_set(&dr->dr_ref, 0);
1067 dr->dr_bio_count = bio_count;
1068 dr->dr_error = 0;
1069
1070 for (int i = 0; i < dr->dr_bio_count; i++)
1071 dr->dr_bio[i] = NULL;
1072
1073 return (dr);
1074}
1075
1076static void
1077vdev_classic_dio_free(dio_request_t *dr)
1078{
1079 int i;
1080
1081 for (i = 0; i < dr->dr_bio_count; i++)
1082 if (dr->dr_bio[i])
1083 bio_put(dr->dr_bio[i]);
1084
1085 kmem_free(dr, sizeof (dio_request_t) +
1086 sizeof (struct bio *) * dr->dr_bio_count);
1087}
1088
1089static void
1090vdev_classic_dio_get(dio_request_t *dr)
1091{
1092 atomic_inc(&dr->dr_ref);
1093}
1094
1095static void
1096vdev_classic_dio_put(dio_request_t *dr)
1097{
1098 int rc = atomic_dec_return(&dr->dr_ref);
1099
1100 /*
1101 * Free the dio_request when the last reference is dropped and
1102 * ensure zio_interpret is called only once with the correct zio
1103 */
1104 if (rc == 0) {
1105 zio_t *zio = dr->dr_zio;
1106 int error = dr->dr_error;
1107
1108 vdev_classic_dio_free(dr);
1109
1110 if (zio) {
1111 zio->io_error = error;
1112 ASSERT3S(zio->io_error, >=, 0);
1113 if (zio->io_error)
1114 vdev_disk_error(zio);
1115
1116 zio_delay_interrupt(zio);
1117 }
1118 }
1119}
1120
1121BIO_END_IO_PROTO(vdev_classic_physio_completion, bio, error)
1122{
1123 dio_request_t *dr = bio->bi_private;
1124
1125 if (dr->dr_error == 0) {
1126#ifdef HAVE_1ARG_BIO_END_IO_T
1127 dr->dr_error = BIO_END_IO_ERROR(bio);
1128#else
1129 if (error)
1130 dr->dr_error = -(error);
1131 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
1132 dr->dr_error = EIO;
1133#endif
1134 }
1135
1136 /* Drop reference acquired by vdev_classic_physio */
1137 vdev_classic_dio_put(dr);
1138}
1139
5f264996 1140static inline unsigned int
f3b85d70 1141vdev_classic_bio_max_segs(zio_t *zio, int bio_size, uint64_t abd_offset)
5f264996
BB
1142{
1143 unsigned long nr_segs = abd_nr_pages_off(zio->io_abd,
1144 bio_size, abd_offset);
1145
1146#ifdef HAVE_BIO_MAX_SEGS
1147 return (bio_max_segs(nr_segs));
1148#else
1149 return (MIN(nr_segs, BIO_MAX_PAGES));
1150#endif
1151}
1152
60101509 1153static int
867178ae 1154vdev_classic_physio(zio_t *zio)
60101509 1155{
867178ae
RN
1156 vdev_t *v = zio->io_vd;
1157 vdev_disk_t *vd = v->vdev_tsd;
1158 struct block_device *bdev = BDH_BDEV(vd->vd_bdh);
1159 size_t io_size = zio->io_size;
1160 uint64_t io_offset = zio->io_offset;
1161 int rw = zio->io_type == ZIO_TYPE_READ ? READ : WRITE;
1162 int flags = 0;
1163
d1d7e268 1164 dio_request_t *dr;
b0be93e8 1165 uint64_t abd_offset;
60101509 1166 uint64_t bio_offset;
f8c0d7e1
MA
1167 int bio_size;
1168 int bio_count = 16;
1169 int error = 0;
e8ac4557 1170 struct blk_plug plug;
5f264996 1171 unsigned short nr_vecs;
066e8252 1172
d441e85d
BB
1173 /*
1174 * Accessing outside the block device is never allowed.
1175 */
1176 if (io_offset + io_size > bdev->bd_inode->i_size) {
1177 vdev_dbgmsg(zio->io_vd,
1178 "Illegal access %llu size %llu, device size %llu",
5dbf6c5a
AZ
1179 (u_longlong_t)io_offset,
1180 (u_longlong_t)io_size,
1181 (u_longlong_t)i_size_read(bdev->bd_inode));
d441e85d
BB
1182 return (SET_ERROR(EIO));
1183 }
e06be586 1184
60101509 1185retry:
f3b85d70 1186 dr = vdev_classic_dio_alloc(bio_count);
60101509 1187
f1100863 1188 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)) &&
16f0fdad
MZ
1189 zio->io_vd->vdev_failfast == B_TRUE) {
1190 bio_set_flags_failfast(bdev, &flags, zfs_vdev_failfast_mask & 1,
1191 zfs_vdev_failfast_mask & 2, zfs_vdev_failfast_mask & 4);
1192 }
2959d94a 1193
60101509 1194 dr->dr_zio = zio;
60101509 1195
60101509 1196 /*
f8c0d7e1
MA
1197 * Since bio's can have up to BIO_MAX_PAGES=256 iovec's, each of which
1198 * is at least 512 bytes and at most PAGESIZE (typically 4K), one bio
1199 * can cover at least 128KB and at most 1MB. When the required number
1200 * of iovec's exceeds this, we are forced to break the IO in multiple
1201 * bio's and wait for them all to complete. This is likely if the
1202 * recordsize property is increased beyond 1MB. The default
1203 * bio_count=16 should typically accommodate the maximum-size zio of
1204 * 16MB.
60101509 1205 */
a6255b7f 1206
b0be93e8
IH
1207 abd_offset = 0;
1208 bio_offset = io_offset;
f8c0d7e1
MA
1209 bio_size = io_size;
1210 for (int i = 0; i <= dr->dr_bio_count; i++) {
60101509
BB
1211
1212 /* Finished constructing bio's for given buffer */
1213 if (bio_size <= 0)
1214 break;
1215
1216 /*
f8c0d7e1
MA
1217 * If additional bio's are required, we have to retry, but
1218 * this should be rare - see the comment above.
60101509
BB
1219 */
1220 if (dr->dr_bio_count == i) {
f3b85d70 1221 vdev_classic_dio_free(dr);
60101509 1222 bio_count *= 2;
60101509
BB
1223 goto retry;
1224 }
1225
f3b85d70 1226 nr_vecs = vdev_classic_bio_max_segs(zio, bio_size, abd_offset);
5f264996 1227 dr->dr_bio[i] = vdev_bio_alloc(bdev, GFP_NOIO, nr_vecs);
1086f542 1228 if (unlikely(dr->dr_bio[i] == NULL)) {
f3b85d70 1229 vdev_classic_dio_free(dr);
ecb2b7dc 1230 return (SET_ERROR(ENOMEM));
60101509
BB
1231 }
1232
f3b85d70
RN
1233 /* Matching put called by vdev_classic_physio_completion */
1234 vdev_classic_dio_get(dr);
60101509 1235
d4541210 1236 BIO_BI_SECTOR(dr->dr_bio[i]) = bio_offset >> 9;
f3b85d70 1237 dr->dr_bio[i]->bi_end_io = vdev_classic_physio_completion;
60101509 1238 dr->dr_bio[i]->bi_private = dr;
3b86aeb2 1239 bio_set_op_attrs(dr->dr_bio[i], rw, flags);
60101509
BB
1240
1241 /* Remaining size is returned to become the new size */
fb822260 1242 bio_size = abd_bio_map_off(dr->dr_bio[i], zio->io_abd,
02730c33 1243 bio_size, abd_offset);
60101509
BB
1244
1245 /* Advance in buffer and construct another bio if needed */
b0be93e8 1246 abd_offset += BIO_BI_SIZE(dr->dr_bio[i]);
d4541210 1247 bio_offset += BIO_BI_SIZE(dr->dr_bio[i]);
60101509
BB
1248 }
1249
37f9dac5 1250 /* Extra reference to protect dio_request during vdev_submit_bio */
f3b85d70 1251 vdev_classic_dio_get(dr);
60101509 1252
e8ac4557
IH
1253 if (dr->dr_bio_count > 1)
1254 blk_start_plug(&plug);
e8ac4557 1255
60101509 1256 /* Submit all bio's associated with this dio */
f8c0d7e1 1257 for (int i = 0; i < dr->dr_bio_count; i++) {
60101509 1258 if (dr->dr_bio[i])
3b86aeb2 1259 vdev_submit_bio(dr->dr_bio[i]);
f8c0d7e1 1260 }
60101509 1261
e8ac4557
IH
1262 if (dr->dr_bio_count > 1)
1263 blk_finish_plug(&plug);
e8ac4557 1264
f3b85d70 1265 vdev_classic_dio_put(dr);
60101509 1266
d1d7e268 1267 return (error);
60101509
BB
1268}
1269
f3b85d70
RN
1270/* ========== */
1271
36ba27e9 1272BIO_END_IO_PROTO(vdev_disk_io_flush_completion, bio, error)
60101509
BB
1273{
1274 zio_t *zio = bio->bi_private;
784a7fe5 1275#ifdef HAVE_1ARG_BIO_END_IO_T
36ba27e9
BB
1276 zio->io_error = BIO_END_IO_ERROR(bio);
1277#else
1278 zio->io_error = -error;
784a7fe5 1279#endif
60101509 1280
36ba27e9 1281 if (zio->io_error && (zio->io_error == EOPNOTSUPP))
60101509
BB
1282 zio->io_vd->vdev_nowritecache = B_TRUE;
1283
1284 bio_put(bio);
d148e951
BB
1285 ASSERT3S(zio->io_error, >=, 0);
1286 if (zio->io_error)
1287 vdev_disk_error(zio);
60101509 1288 zio_interrupt(zio);
60101509
BB
1289}
1290
1291static int
1292vdev_disk_io_flush(struct block_device *bdev, zio_t *zio)
1293{
1294 struct request_queue *q;
1295 struct bio *bio;
1296
1297 q = bdev_get_queue(bdev);
1298 if (!q)
ecb2b7dc 1299 return (SET_ERROR(ENXIO));
60101509 1300
5f264996 1301 bio = vdev_bio_alloc(bdev, GFP_NOIO, 0);
29b763cd 1302 if (unlikely(bio == NULL))
ecb2b7dc 1303 return (SET_ERROR(ENOMEM));
60101509
BB
1304
1305 bio->bi_end_io = vdev_disk_io_flush_completion;
1306 bio->bi_private = zio;
a5e046ea 1307 bio_set_flush(bio);
3b86aeb2 1308 vdev_submit_bio(bio);
cecb7487 1309 invalidate_bdev(bdev);
60101509 1310
d1d7e268 1311 return (0);
60101509 1312}
60101509 1313
06e25f9c
US
1314#if defined(HAVE_BLKDEV_ISSUE_SECURE_ERASE) || \
1315 defined(HAVE_BLKDEV_ISSUE_DISCARD_ASYNC)
1316BIO_END_IO_PROTO(vdev_disk_discard_end_io, bio, error)
1317{
1318 zio_t *zio = bio->bi_private;
1319#ifdef HAVE_1ARG_BIO_END_IO_T
1320 zio->io_error = BIO_END_IO_ERROR(bio);
1321#else
1322 zio->io_error = -error;
1323#endif
1324 bio_put(bio);
1325 if (zio->io_error)
1326 vdev_disk_error(zio);
1327 zio_interrupt(zio);
1328}
1329
a12a5cb5 1330static int
06e25f9c 1331vdev_issue_discard_trim(zio_t *zio, unsigned long flags)
a12a5cb5 1332{
06e25f9c
US
1333 int ret;
1334 struct bio *bio = NULL;
a12a5cb5 1335
06e25f9c
US
1336#if defined(BLKDEV_DISCARD_SECURE)
1337 ret = - __blkdev_issue_discard(
1338 BDH_BDEV(((vdev_disk_t *)zio->io_vd->vdev_tsd)->vd_bdh),
1339 zio->io_offset >> 9, zio->io_size >> 9, GFP_NOFS, flags, &bio);
1340#else
1341 (void) flags;
1342 ret = - __blkdev_issue_discard(
1343 BDH_BDEV(((vdev_disk_t *)zio->io_vd->vdev_tsd)->vd_bdh),
1344 zio->io_offset >> 9, zio->io_size >> 9, GFP_NOFS, &bio);
1345#endif
1346 if (!ret && bio) {
1347 bio->bi_private = zio;
1348 bio->bi_end_io = vdev_disk_discard_end_io;
1349 vdev_submit_bio(bio);
a12a5cb5 1350 }
06e25f9c
US
1351 return (ret);
1352}
1353#endif
1354
1355static int
1356vdev_disk_io_trim(zio_t *zio)
1357{
a12a5cb5 1358 unsigned long trim_flags = 0;
06e25f9c
US
1359 if (zio->io_trim_flags & ZIO_TRIM_SECURE) {
1360#if defined(HAVE_BLKDEV_ISSUE_SECURE_ERASE)
1361 return (-blkdev_issue_secure_erase(
1362 BDH_BDEV(((vdev_disk_t *)zio->io_vd->vdev_tsd)->vd_bdh),
1363 zio->io_offset >> 9, zio->io_size >> 9, GFP_NOFS));
1364#elif defined(BLKDEV_DISCARD_SECURE)
a12a5cb5
BB
1365 trim_flags |= BLKDEV_DISCARD_SECURE;
1366#endif
06e25f9c
US
1367 }
1368#if defined(HAVE_BLKDEV_ISSUE_SECURE_ERASE) || \
1369 defined(HAVE_BLKDEV_ISSUE_DISCARD_ASYNC)
1370 return (vdev_issue_discard_trim(zio, trim_flags));
1371#elif defined(HAVE_BLKDEV_ISSUE_DISCARD)
1372 return (-blkdev_issue_discard(
1373 BDH_BDEV(((vdev_disk_t *)zio->io_vd->vdev_tsd)->vd_bdh),
a12a5cb5
BB
1374 zio->io_offset >> 9, zio->io_size >> 9, GFP_NOFS, trim_flags));
1375#else
1376#error "Unsupported kernel"
1377#endif
1378}
1379
c4a13ba4
RN
1380int (*vdev_disk_io_rw_fn)(zio_t *zio) = NULL;
1381
98b25418 1382static void
60101509
BB
1383vdev_disk_io_start(zio_t *zio)
1384{
1385 vdev_t *v = zio->io_vd;
1386 vdev_disk_t *vd = v->vdev_tsd;
867178ae 1387 int error;
60101509 1388
d441e85d
BB
1389 /*
1390 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
1391 * Nothing to be done here but return failure.
1392 */
1393 if (vd == NULL) {
1394 zio->io_error = ENXIO;
1395 zio_interrupt(zio);
1396 return;
1397 }
1398
1399 rw_enter(&vd->vd_lock, RW_READER);
1400
1401 /*
1402 * If the vdev is closed, it's likely due to a failed reopen and is
1403 * in the UNAVAIL state. Nothing to be done here but return failure.
1404 */
386d6a75 1405 if (vd->vd_bdh == NULL) {
d441e85d
BB
1406 rw_exit(&vd->vd_lock);
1407 zio->io_error = ENXIO;
1408 zio_interrupt(zio);
1409 return;
1410 }
1411
60101509
BB
1412 switch (zio->io_type) {
1413 case ZIO_TYPE_IOCTL:
1414
1415 if (!vdev_readable(v)) {
d441e85d 1416 rw_exit(&vd->vd_lock);
2e528b49 1417 zio->io_error = SET_ERROR(ENXIO);
98b25418
GW
1418 zio_interrupt(zio);
1419 return;
60101509
BB
1420 }
1421
1422 switch (zio->io_cmd) {
1423 case DKIOCFLUSHWRITECACHE:
1424
1425 if (zfs_nocacheflush)
1426 break;
1427
1428 if (v->vdev_nowritecache) {
2e528b49 1429 zio->io_error = SET_ERROR(ENOTSUP);
60101509
BB
1430 break;
1431 }
1432
386d6a75 1433 error = vdev_disk_io_flush(BDH_BDEV(vd->vd_bdh), zio);
d441e85d
BB
1434 if (error == 0) {
1435 rw_exit(&vd->vd_lock);
98b25418 1436 return;
d441e85d 1437 }
60101509
BB
1438
1439 zio->io_error = error;
60101509
BB
1440
1441 break;
1442
1443 default:
2e528b49 1444 zio->io_error = SET_ERROR(ENOTSUP);
60101509
BB
1445 }
1446
d441e85d 1447 rw_exit(&vd->vd_lock);
98b25418
GW
1448 zio_execute(zio);
1449 return;
60101509 1450
1b939560 1451 case ZIO_TYPE_TRIM:
a12a5cb5 1452 zio->io_error = vdev_disk_io_trim(zio);
1b939560 1453 rw_exit(&vd->vd_lock);
06e25f9c
US
1454#if defined(HAVE_BLKDEV_ISSUE_SECURE_ERASE)
1455 if (zio->io_trim_flags & ZIO_TRIM_SECURE)
1456 zio_interrupt(zio);
1457#elif defined(HAVE_BLKDEV_ISSUE_DISCARD)
1b939560 1458 zio_interrupt(zio);
06e25f9c 1459#endif
1b939560
BB
1460 return;
1461
867178ae
RN
1462 case ZIO_TYPE_READ:
1463 case ZIO_TYPE_WRITE:
1464 zio->io_target_timestamp = zio_handle_io_delay(zio);
c4a13ba4 1465 error = vdev_disk_io_rw_fn(zio);
d441e85d 1466 rw_exit(&vd->vd_lock);
867178ae
RN
1467 if (error) {
1468 zio->io_error = error;
1469 zio_interrupt(zio);
1470 }
98b25418 1471 return;
60101509 1472
867178ae
RN
1473 default:
1474 /*
1475 * Getting here means our parent vdev has made a very strange
1476 * request of us, and shouldn't happen. Assert here to force a
1477 * crash in dev builds, but in production return the IO
1478 * unhandled. The pool will likely suspend anyway but that's
1479 * nicer than crashing the kernel.
1480 */
1481 ASSERT3S(zio->io_type, ==, -1);
d441e85d 1482
867178ae
RN
1483 rw_exit(&vd->vd_lock);
1484 zio->io_error = SET_ERROR(ENOTSUP);
98b25418
GW
1485 zio_interrupt(zio);
1486 return;
60101509 1487 }
867178ae
RN
1488
1489 __builtin_unreachable();
60101509
BB
1490}
1491
1492static void
1493vdev_disk_io_done(zio_t *zio)
1494{
1495 /*
1496 * If the device returned EIO, we revalidate the media. If it is
1497 * determined the media has changed this triggers the asynchronous
1498 * removal of the device from the configuration.
1499 */
1500 if (zio->io_error == EIO) {
d1d7e268 1501 vdev_t *v = zio->io_vd;
60101509
BB
1502 vdev_disk_t *vd = v->vdev_tsd;
1503
386d6a75
RN
1504 if (!zfs_check_disk_status(BDH_BDEV(vd->vd_bdh))) {
1505 invalidate_bdev(BDH_BDEV(vd->vd_bdh));
60101509
BB
1506 v->vdev_remove_wanted = B_TRUE;
1507 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
1508 }
1509 }
1510}
1511
1512static void
1513vdev_disk_hold(vdev_t *vd)
1514{
1515 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
1516
1517 /* We must have a pathname, and it must be absolute. */
1518 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
1519 return;
1520
1521 /*
1522 * Only prefetch path and devid info if the device has
1523 * never been opened.
1524 */
1525 if (vd->vdev_tsd != NULL)
1526 return;
1527
60101509
BB
1528}
1529
1530static void
1531vdev_disk_rele(vdev_t *vd)
1532{
1533 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
1534
1535 /* XXX: Implement me as a vnode rele for the device */
1536}
1537
df2169d1
RN
1538/*
1539 * BIO submission method. See comment above about vdev_classic.
1540 * Set zfs_vdev_disk_classic=0 for new, =1 for classic
1541 */
1542static uint_t zfs_vdev_disk_classic = 0; /* default new */
1543
1544/* Set submission function from module parameter */
1545static int
1546vdev_disk_param_set_classic(const char *buf, zfs_kernel_param_t *kp)
1547{
1548 int err = param_set_uint(buf, kp);
1549 if (err < 0)
1550 return (SET_ERROR(err));
1551
1552 vdev_disk_io_rw_fn =
1553 zfs_vdev_disk_classic ? vdev_classic_physio : vdev_disk_io_rw;
1554
1555 printk(KERN_INFO "ZFS: forcing %s BIO submission\n",
1556 zfs_vdev_disk_classic ? "classic" : "new");
1557
1558 return (0);
1559}
1560
c4a13ba4
RN
1561/*
1562 * At first use vdev use, set the submission function from the default value if
1563 * it hasn't been set already.
1564 */
1565static int
1566vdev_disk_init(spa_t *spa, nvlist_t *nv, void **tsd)
1567{
1568 (void) spa;
1569 (void) nv;
1570 (void) tsd;
1571
1572 if (vdev_disk_io_rw_fn == NULL)
df2169d1
RN
1573 vdev_disk_io_rw_fn = zfs_vdev_disk_classic ?
1574 vdev_classic_physio : vdev_disk_io_rw;
c4a13ba4
RN
1575
1576 return (0);
1577}
1578
60101509 1579vdev_ops_t vdev_disk_ops = {
c4a13ba4 1580 .vdev_op_init = vdev_disk_init,
b2255edc 1581 .vdev_op_fini = NULL,
a64f8276
I
1582 .vdev_op_open = vdev_disk_open,
1583 .vdev_op_close = vdev_disk_close,
1584 .vdev_op_asize = vdev_default_asize,
b2255edc
BB
1585 .vdev_op_min_asize = vdev_default_min_asize,
1586 .vdev_op_min_alloc = NULL,
a64f8276
I
1587 .vdev_op_io_start = vdev_disk_io_start,
1588 .vdev_op_io_done = vdev_disk_io_done,
1589 .vdev_op_state_change = NULL,
1590 .vdev_op_need_resilver = NULL,
1591 .vdev_op_hold = vdev_disk_hold,
1592 .vdev_op_rele = vdev_disk_rele,
1593 .vdev_op_remap = NULL,
1594 .vdev_op_xlate = vdev_default_xlate,
b2255edc
BB
1595 .vdev_op_rebuild_asize = NULL,
1596 .vdev_op_metaslab_init = NULL,
1597 .vdev_op_config_generate = NULL,
1598 .vdev_op_nparity = NULL,
1599 .vdev_op_ndisks = NULL,
a64f8276 1600 .vdev_op_type = VDEV_TYPE_DISK, /* name of this vdev type */
55c12724
AH
1601 .vdev_op_leaf = B_TRUE, /* leaf vdev */
1602 .vdev_op_kobj_evt_post = vdev_disk_kobj_evt_post
60101509
BB
1603};
1604
9e17e6f2
BB
1605/*
1606 * The zfs_vdev_scheduler module option has been deprecated. Setting this
1607 * value no longer has any effect. It has not yet been entirely removed
1608 * to allow the module to be loaded if this option is specified in the
1609 * /etc/modprobe.d/zfs.conf file. The following warning will be logged.
1610 */
1611static int
1612param_set_vdev_scheduler(const char *val, zfs_kernel_param_t *kp)
1613{
1614 int error = param_set_charp(val, kp);
1615 if (error == 0) {
1616 printk(KERN_INFO "The 'zfs_vdev_scheduler' module option "
1617 "is not supported.\n");
1618 }
1619
1620 return (error);
1621}
1622
18168da7 1623static const char *zfs_vdev_scheduler = "unused";
e771de53
BB
1624module_param_call(zfs_vdev_scheduler, param_set_vdev_scheduler,
1625 param_get_charp, &zfs_vdev_scheduler, 0644);
c409e464 1626MODULE_PARM_DESC(zfs_vdev_scheduler, "I/O scheduler");
6fe3498c
RM
1627
1628int
1629param_set_min_auto_ashift(const char *buf, zfs_kernel_param_t *kp)
1630{
ab8d9c17 1631 uint_t val;
6fe3498c
RM
1632 int error;
1633
ab8d9c17 1634 error = kstrtouint(buf, 0, &val);
6fe3498c
RM
1635 if (error < 0)
1636 return (SET_ERROR(error));
1637
1638 if (val < ASHIFT_MIN || val > zfs_vdev_max_auto_ashift)
1639 return (SET_ERROR(-EINVAL));
1640
ab8d9c17 1641 error = param_set_uint(buf, kp);
6fe3498c
RM
1642 if (error < 0)
1643 return (SET_ERROR(error));
1644
1645 return (0);
1646}
1647
1648int
1649param_set_max_auto_ashift(const char *buf, zfs_kernel_param_t *kp)
1650{
ab8d9c17 1651 uint_t val;
6fe3498c
RM
1652 int error;
1653
ab8d9c17 1654 error = kstrtouint(buf, 0, &val);
6fe3498c
RM
1655 if (error < 0)
1656 return (SET_ERROR(error));
1657
1658 if (val > ASHIFT_MAX || val < zfs_vdev_min_auto_ashift)
1659 return (SET_ERROR(-EINVAL));
1660
ab8d9c17 1661 error = param_set_uint(buf, kp);
6fe3498c
RM
1662 if (error < 0)
1663 return (SET_ERROR(error));
1664
1665 return (0);
1666}
f66ffe68
SD
1667
1668ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, open_timeout_ms, UINT, ZMOD_RW,
1669 "Timeout before determining that a device is missing");
16f0fdad
MZ
1670
1671ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, failfast_mask, UINT, ZMOD_RW,
1672 "Defines failfast mask: 1 - device, 2 - transport, 4 - driver");
06a19602
RN
1673
1674ZFS_MODULE_PARAM(zfs_vdev_disk, zfs_vdev_disk_, max_segs, UINT, ZMOD_RW,
1675 "Maximum number of data segments to add to an IO request (min 4)");
df2169d1
RN
1676
1677ZFS_MODULE_PARAM_CALL(zfs_vdev_disk, zfs_vdev_disk_, classic,
1678 vdev_disk_param_set_classic, param_get_uint, ZMOD_RD,
1679 "Use classic BIO submission method");