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