4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
24 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
25 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
32 #include <linux/blkdev.h>
33 #include <linux/elevator.h>
34 #include <linux/backing-dev.h>
35 #include <linux/hdreg.h>
36 #include <linux/msdos_fs.h> /* for SECTOR_* */
38 #ifndef HAVE_BLK_QUEUE_FLAG_SET
40 blk_queue_flag_set(unsigned int flag, struct request_queue *q)
42 queue_flag_set(flag, q);
46 #ifndef HAVE_BLK_QUEUE_FLAG_CLEAR
48 blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
50 queue_flag_clear(flag, q);
56 * The blk_queue_write_cache() interface has replaced blk_queue_flush()
57 * interface. However, the new interface is GPL-only thus we implement
58 * our own trivial wrapper when the GPL-only version is detected.
61 * The blk_queue_flush() interface has replaced blk_queue_ordered()
62 * interface. However, while the old interface was available to all the
63 * new one is GPL-only. Thus if the GPL-only version is detected we
64 * implement our own trivial helper.
67 blk_queue_set_write_cache(struct request_queue *q, bool wc, bool fua)
69 #if defined(HAVE_BLK_QUEUE_WRITE_CACHE_GPL_ONLY)
71 blk_queue_flag_set(QUEUE_FLAG_WC, q);
73 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
75 blk_queue_flag_set(QUEUE_FLAG_FUA, q);
77 blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
78 #elif defined(HAVE_BLK_QUEUE_WRITE_CACHE)
79 blk_queue_write_cache(q, wc, fua);
80 #elif defined(HAVE_BLK_QUEUE_FLUSH_GPL_ONLY)
82 q->flush_flags |= REQ_FLUSH;
84 q->flush_flags |= REQ_FUA;
85 #elif defined(HAVE_BLK_QUEUE_FLUSH)
86 blk_queue_flush(q, (wc ? REQ_FLUSH : 0) | (fua ? REQ_FUA : 0));
88 #error "Unsupported kernel"
93 blk_queue_set_read_ahead(struct request_queue *q, unsigned long ra_pages)
95 #if !defined(HAVE_BLK_QUEUE_UPDATE_READAHEAD) && \
96 !defined(HAVE_DISK_UPDATE_READAHEAD)
97 #ifdef HAVE_BLK_QUEUE_BDI_DYNAMIC
98 q->backing_dev_info->ra_pages = ra_pages;
100 q->backing_dev_info.ra_pages = ra_pages;
105 #ifdef HAVE_BIO_BVEC_ITER
106 #define BIO_BI_SECTOR(bio) (bio)->bi_iter.bi_sector
107 #define BIO_BI_SIZE(bio) (bio)->bi_iter.bi_size
108 #define BIO_BI_IDX(bio) (bio)->bi_iter.bi_idx
109 #define BIO_BI_SKIP(bio) (bio)->bi_iter.bi_bvec_done
110 #define bio_for_each_segment4(bv, bvp, b, i) \
111 bio_for_each_segment((bv), (b), (i))
112 typedef struct bvec_iter bvec_iterator_t;
114 #define BIO_BI_SECTOR(bio) (bio)->bi_sector
115 #define BIO_BI_SIZE(bio) (bio)->bi_size
116 #define BIO_BI_IDX(bio) (bio)->bi_idx
117 #define BIO_BI_SKIP(bio) (0)
118 #define bio_for_each_segment4(bv, bvp, b, i) \
119 bio_for_each_segment((bvp), (b), (i))
120 typedef int bvec_iterator_t;
124 bio_set_flags_failfast(struct block_device *bdev, int *flags)
128 * Disable FAILFAST for loopback devices because of the
129 * following incorrect BUG_ON() in loop_make_request().
130 * This support is also disabled for md devices because the
131 * test suite layers md devices on top of loopback devices.
132 * This may be removed when the loopback driver is fixed.
134 * BUG_ON(!lo || (rw != READ && rw != WRITE));
136 if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
137 (MAJOR(bdev->bd_dev) == MD_MAJOR))
140 #ifdef BLOCK_EXT_MAJOR
141 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
143 #endif /* BLOCK_EXT_MAJOR */
144 #endif /* CONFIG_BUG */
146 *flags |= REQ_FAILFAST_MASK;
150 * Maximum disk label length, it may be undefined for some kernels.
152 #if !defined(DISK_NAME_LEN)
153 #define DISK_NAME_LEN 32
154 #endif /* DISK_NAME_LEN */
156 #ifdef HAVE_BIO_BI_STATUS
158 bi_status_to_errno(blk_status_t status)
163 case BLK_STS_NOTSUPP:
165 case BLK_STS_TIMEOUT:
169 case BLK_STS_TRANSPORT:
177 case BLK_STS_PROTECTION:
179 case BLK_STS_RESOURCE:
190 static inline blk_status_t
191 errno_to_bi_status(int error)
197 return (BLK_STS_NOTSUPP);
199 return (BLK_STS_TIMEOUT);
201 return (BLK_STS_NOSPC);
203 return (BLK_STS_TRANSPORT);
205 return (BLK_STS_TARGET);
207 return (BLK_STS_NEXUS);
209 return (BLK_STS_MEDIUM);
211 return (BLK_STS_PROTECTION);
213 return (BLK_STS_RESOURCE);
215 return (BLK_STS_AGAIN);
217 return (BLK_STS_IOERR);
219 return (BLK_STS_IOERR);
222 #endif /* HAVE_BIO_BI_STATUS */
226 * The bio_endio() prototype changed slightly. These are helper
227 * macro's to ensure the prototype and invocation are handled.
229 #ifdef HAVE_1ARG_BIO_END_IO_T
230 #ifdef HAVE_BIO_BI_STATUS
231 #define BIO_END_IO_ERROR(bio) bi_status_to_errno(bio->bi_status)
232 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x)
233 #define BIO_END_IO(bio, error) bio_set_bi_status(bio, error)
235 bio_set_bi_status(struct bio *bio, int error)
237 ASSERT3S(error, <=, 0);
238 bio->bi_status = errno_to_bi_status(-error);
242 #define BIO_END_IO_ERROR(bio) (-(bio->bi_error))
243 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x)
244 #define BIO_END_IO(bio, error) bio_set_bi_error(bio, error)
246 bio_set_bi_error(struct bio *bio, int error)
248 ASSERT3S(error, <=, 0);
249 bio->bi_error = error;
252 #endif /* HAVE_BIO_BI_STATUS */
255 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x, int z)
256 #define BIO_END_IO(bio, error) bio_endio(bio, error);
257 #endif /* HAVE_1ARG_BIO_END_IO_T */
261 * 3.10.0 CentOS 7.x API,
262 * blkdev_reread_part()
264 * For older kernels trigger a re-reading of the partition table by calling
265 * check_disk_change() which calls flush_disk() to invalidate the device.
267 * For newer kernels (as of 5.10), bdev_check_media_change is used, in favor of
268 * check_disk_change(), with the modification that invalidation is no longer
271 #ifdef HAVE_CHECK_DISK_CHANGE
272 #define zfs_check_media_change(bdev) check_disk_change(bdev)
273 #ifdef HAVE_BLKDEV_REREAD_PART
274 #define vdev_bdev_reread_part(bdev) blkdev_reread_part(bdev)
276 #define vdev_bdev_reread_part(bdev) check_disk_change(bdev)
277 #endif /* HAVE_BLKDEV_REREAD_PART */
279 #ifdef HAVE_BDEV_CHECK_MEDIA_CHANGE
281 zfs_check_media_change(struct block_device *bdev)
283 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
284 struct gendisk *gd = bdev->bd_disk;
285 const struct block_device_operations *bdo = gd->fops;
288 if (!bdev_check_media_change(bdev))
291 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
293 * Force revalidation, to mimic the old behavior of
294 * check_disk_change()
296 if (bdo->revalidate_disk)
297 bdo->revalidate_disk(gd);
302 #define vdev_bdev_reread_part(bdev) zfs_check_media_change(bdev)
305 * This is encountered if check_disk_change() and bdev_check_media_change()
306 * are not available in the kernel - likely due to an API change that needs
309 #error "Unsupported kernel: no usable disk change check"
310 #endif /* HAVE_BDEV_CHECK_MEDIA_CHANGE */
311 #endif /* HAVE_CHECK_DISK_CHANGE */
315 * The function was exported for use, prior to this it existed but the
316 * symbol was not exported.
318 * 4.4.0-6.21 API change for Ubuntu
319 * lookup_bdev() gained a second argument, FMODE_*, to check inode permissions.
322 * Changed to take a dev_t argument which is set on success and return a
323 * non-zero error code on failure.
326 vdev_lookup_bdev(const char *path, dev_t *dev)
328 #if defined(HAVE_DEVT_LOOKUP_BDEV)
329 return (lookup_bdev(path, dev));
330 #elif defined(HAVE_1ARG_LOOKUP_BDEV)
331 struct block_device *bdev = lookup_bdev(path);
333 return (PTR_ERR(bdev));
339 #elif defined(HAVE_MODE_LOOKUP_BDEV)
340 struct block_device *bdev = lookup_bdev(path, FMODE_READ);
342 return (PTR_ERR(bdev));
349 #error "Unsupported kernel"
354 * Kernels without bio_set_op_attrs use bi_rw for the bio flags.
356 #if !defined(HAVE_BIO_SET_OP_ATTRS)
358 bio_set_op_attrs(struct bio *bio, unsigned rw, unsigned flags)
360 bio->bi_rw |= rw | flags;
365 * bio_set_flush - Set the appropriate flags in a bio to guarantee
366 * data are on non-volatile media on completion.
369 * Introduce WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags as a
370 * replacement for WRITE_BARRIER to allow expressing richer semantics
371 * to the block layer. It's up to the block layer to implement the
372 * semantics correctly. Use the WRITE_FLUSH_FUA flag combination.
375 * REQ_FLUSH was renamed to REQ_PREFLUSH. For consistency with previous
376 * OpenZFS releases, prefer the WRITE_FLUSH_FUA flag set if it's available.
379 * The read/write flags and their modifiers, including WRITE_FLUSH,
380 * WRITE_FUA and WRITE_FLUSH_FUA were removed from fs.h in
381 * torvalds/linux@70fd7614 and replaced by direct flag modification
382 * of the REQ_ flags in bio->bi_opf. Use REQ_PREFLUSH.
385 bio_set_flush(struct bio *bio)
387 #if defined(HAVE_REQ_PREFLUSH) /* >= 4.10 */
388 bio_set_op_attrs(bio, 0, REQ_PREFLUSH);
389 #elif defined(WRITE_FLUSH_FUA) /* >= 2.6.37 and <= 4.9 */
390 bio_set_op_attrs(bio, 0, WRITE_FLUSH_FUA);
392 #error "Allowing the build will cause bio_set_flush requests to be ignored."
406 * in all cases but may have a performance impact for some kernels. It
407 * has the advantage of minimizing kernel specific changes in the zvol code.
410 static inline boolean_t
411 bio_is_flush(struct bio *bio)
413 #if defined(HAVE_REQ_OP_FLUSH) && defined(HAVE_BIO_BI_OPF)
414 return ((bio_op(bio) == REQ_OP_FLUSH) || (bio->bi_opf & REQ_PREFLUSH));
415 #elif defined(HAVE_REQ_PREFLUSH) && defined(HAVE_BIO_BI_OPF)
416 return (bio->bi_opf & REQ_PREFLUSH);
417 #elif defined(HAVE_REQ_PREFLUSH) && !defined(HAVE_BIO_BI_OPF)
418 return (bio->bi_rw & REQ_PREFLUSH);
419 #elif defined(HAVE_REQ_FLUSH)
420 return (bio->bi_rw & REQ_FLUSH);
422 #error "Unsupported kernel"
428 * REQ_FUA flag moved to bio->bi_opf
433 static inline boolean_t
434 bio_is_fua(struct bio *bio)
436 #if defined(HAVE_BIO_BI_OPF)
437 return (bio->bi_opf & REQ_FUA);
438 #elif defined(REQ_FUA)
439 return (bio->bi_rw & REQ_FUA);
441 #error "Allowing the build will cause fua requests to be ignored."
452 * In all cases the normal I/O path is used for discards. The only
453 * difference is how the kernel tags individual I/Os as discards.
455 static inline boolean_t
456 bio_is_discard(struct bio *bio)
458 #if defined(HAVE_REQ_OP_DISCARD)
459 return (bio_op(bio) == REQ_OP_DISCARD);
460 #elif defined(HAVE_REQ_DISCARD)
461 return (bio->bi_rw & REQ_DISCARD);
463 #error "Unsupported kernel"
469 * REQ_OP_SECURE_ERASE
474 static inline boolean_t
475 bio_is_secure_erase(struct bio *bio)
477 #if defined(HAVE_REQ_OP_SECURE_ERASE)
478 return (bio_op(bio) == REQ_OP_SECURE_ERASE);
479 #elif defined(REQ_SECURE)
480 return (bio->bi_rw & REQ_SECURE);
488 * Discard granularity and alignment restrictions may now be set. For
489 * older kernels which do not support this it is safe to skip it.
492 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
494 q->limits.discard_granularity = dg;
499 * blk_queue_secure_erase()
502 * blk_queue_secdiscard()
505 blk_queue_discard_secure(struct request_queue *q)
507 #if defined(HAVE_BLK_QUEUE_SECURE_ERASE)
508 return (blk_queue_secure_erase(q));
509 #elif defined(HAVE_BLK_QUEUE_SECDISCARD)
510 return (blk_queue_secdiscard(q));
517 * A common holder for vdev_bdev_open() is used to relax the exclusive open
518 * semantics slightly. Internal vdev disk callers may pass VDEV_HOLDER to
519 * allow them to open the device multiple times. Other kernel callers and
520 * user space processes which don't pass this value will get EBUSY. This is
521 * currently required for the correct operation of hot spares.
523 #define VDEV_HOLDER ((void *)0x2401de7)
525 static inline unsigned long
526 blk_generic_start_io_acct(struct request_queue *q __attribute__((unused)),
527 struct gendisk *disk __attribute__((unused)),
528 int rw __attribute__((unused)), struct bio *bio)
530 #if defined(HAVE_DISK_IO_ACCT)
531 return (disk_start_io_acct(disk, bio_sectors(bio), bio_op(bio)));
532 #elif defined(HAVE_BIO_IO_ACCT)
533 return (bio_start_io_acct(bio));
534 #elif defined(HAVE_GENERIC_IO_ACCT_3ARG)
535 unsigned long start_time = jiffies;
536 generic_start_io_acct(rw, bio_sectors(bio), &disk->part0);
538 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
539 unsigned long start_time = jiffies;
540 generic_start_io_acct(q, rw, bio_sectors(bio), &disk->part0);
549 blk_generic_end_io_acct(struct request_queue *q __attribute__((unused)),
550 struct gendisk *disk __attribute__((unused)),
551 int rw __attribute__((unused)), struct bio *bio, unsigned long start_time)
553 #if defined(HAVE_DISK_IO_ACCT)
554 disk_end_io_acct(disk, bio_op(bio), start_time);
555 #elif defined(HAVE_BIO_IO_ACCT)
556 bio_end_io_acct(bio, start_time);
557 #elif defined(HAVE_GENERIC_IO_ACCT_3ARG)
558 generic_end_io_acct(rw, &disk->part0, start_time);
559 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
560 generic_end_io_acct(q, rw, &disk->part0, start_time);
564 #ifndef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
565 static inline struct request_queue *
566 blk_generic_alloc_queue(make_request_fn make_request, int node_id)
568 #if defined(HAVE_BLK_ALLOC_QUEUE_REQUEST_FN)
569 return (blk_alloc_queue(make_request, node_id));
570 #elif defined(HAVE_BLK_ALLOC_QUEUE_REQUEST_FN_RH)
571 return (blk_alloc_queue_rh(make_request, node_id));
573 struct request_queue *q = blk_alloc_queue(GFP_KERNEL);
575 blk_queue_make_request(q, make_request);
580 #endif /* !HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
582 #endif /* _ZFS_BLKDEV_H */