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