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