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 #ifdef HAVE_BLK_QUEUE_BDI_DYNAMIC
96 q->backing_dev_info->ra_pages = ra_pages;
98 q->backing_dev_info.ra_pages = ra_pages;
102 #ifdef HAVE_BIO_BVEC_ITER
103 #define BIO_BI_SECTOR(bio) (bio)->bi_iter.bi_sector
104 #define BIO_BI_SIZE(bio) (bio)->bi_iter.bi_size
105 #define BIO_BI_IDX(bio) (bio)->bi_iter.bi_idx
106 #define BIO_BI_SKIP(bio) (bio)->bi_iter.bi_bvec_done
107 #define bio_for_each_segment4(bv, bvp, b, i) \
108 bio_for_each_segment((bv), (b), (i))
109 typedef struct bvec_iter bvec_iterator_t;
111 #define BIO_BI_SECTOR(bio) (bio)->bi_sector
112 #define BIO_BI_SIZE(bio) (bio)->bi_size
113 #define BIO_BI_IDX(bio) (bio)->bi_idx
114 #define BIO_BI_SKIP(bio) (0)
115 #define bio_for_each_segment4(bv, bvp, b, i) \
116 bio_for_each_segment((bvp), (b), (i))
117 typedef int bvec_iterator_t;
121 bio_set_flags_failfast(struct block_device *bdev, int *flags)
125 * Disable FAILFAST for loopback devices because of the
126 * following incorrect BUG_ON() in loop_make_request().
127 * This support is also disabled for md devices because the
128 * test suite layers md devices on top of loopback devices.
129 * This may be removed when the loopback driver is fixed.
131 * BUG_ON(!lo || (rw != READ && rw != WRITE));
133 if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
134 (MAJOR(bdev->bd_dev) == MD_MAJOR))
137 #ifdef BLOCK_EXT_MAJOR
138 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
140 #endif /* BLOCK_EXT_MAJOR */
141 #endif /* CONFIG_BUG */
143 *flags |= REQ_FAILFAST_MASK;
147 * Maximum disk label length, it may be undefined for some kernels.
149 #if !defined(DISK_NAME_LEN)
150 #define DISK_NAME_LEN 32
151 #endif /* DISK_NAME_LEN */
153 #ifdef HAVE_BIO_BI_STATUS
155 bi_status_to_errno(blk_status_t status)
160 case BLK_STS_NOTSUPP:
162 case BLK_STS_TIMEOUT:
166 case BLK_STS_TRANSPORT:
174 case BLK_STS_PROTECTION:
176 case BLK_STS_RESOURCE:
187 static inline blk_status_t
188 errno_to_bi_status(int error)
194 return (BLK_STS_NOTSUPP);
196 return (BLK_STS_TIMEOUT);
198 return (BLK_STS_NOSPC);
200 return (BLK_STS_TRANSPORT);
202 return (BLK_STS_TARGET);
204 return (BLK_STS_NEXUS);
206 return (BLK_STS_MEDIUM);
208 return (BLK_STS_PROTECTION);
210 return (BLK_STS_RESOURCE);
212 return (BLK_STS_AGAIN);
214 return (BLK_STS_IOERR);
216 return (BLK_STS_IOERR);
219 #endif /* HAVE_BIO_BI_STATUS */
223 * The bio_endio() prototype changed slightly. These are helper
224 * macro's to ensure the prototype and invocation are handled.
226 #ifdef HAVE_1ARG_BIO_END_IO_T
227 #ifdef HAVE_BIO_BI_STATUS
228 #define BIO_END_IO_ERROR(bio) bi_status_to_errno(bio->bi_status)
229 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x)
230 #define BIO_END_IO(bio, error) bio_set_bi_status(bio, error)
232 bio_set_bi_status(struct bio *bio, int error)
234 ASSERT3S(error, <=, 0);
235 bio->bi_status = errno_to_bi_status(-error);
239 #define BIO_END_IO_ERROR(bio) (-(bio->bi_error))
240 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x)
241 #define BIO_END_IO(bio, error) bio_set_bi_error(bio, error)
243 bio_set_bi_error(struct bio *bio, int error)
245 ASSERT3S(error, <=, 0);
246 bio->bi_error = error;
249 #endif /* HAVE_BIO_BI_STATUS */
252 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x, int z)
253 #define BIO_END_IO(bio, error) bio_endio(bio, error);
254 #endif /* HAVE_1ARG_BIO_END_IO_T */
258 * 3.10.0 CentOS 7.x API,
259 * blkdev_reread_part()
261 * For older kernels trigger a re-reading of the partition table by calling
262 * check_disk_change() which calls flush_disk() to invalidate the device.
264 * For newer kernels (as of 5.10), bdev_check_media_change is used, in favor of
265 * check_disk_change(), with the modification that invalidation is no longer
268 #ifdef HAVE_CHECK_DISK_CHANGE
269 #define zfs_check_media_change(bdev) check_disk_change(bdev)
270 #ifdef HAVE_BLKDEV_REREAD_PART
271 #define vdev_bdev_reread_part(bdev) blkdev_reread_part(bdev)
273 #define vdev_bdev_reread_part(bdev) check_disk_change(bdev)
274 #endif /* HAVE_BLKDEV_REREAD_PART */
276 #ifdef HAVE_BDEV_CHECK_MEDIA_CHANGE
278 zfs_check_media_change(struct block_device *bdev)
280 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
281 struct gendisk *gd = bdev->bd_disk;
282 const struct block_device_operations *bdo = gd->fops;
285 if (!bdev_check_media_change(bdev))
288 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
290 * Force revalidation, to mimic the old behavior of
291 * check_disk_change()
293 if (bdo->revalidate_disk)
294 bdo->revalidate_disk(gd);
299 #define vdev_bdev_reread_part(bdev) zfs_check_media_change(bdev)
302 * This is encountered if check_disk_change() and bdev_check_media_change()
303 * are not available in the kernel - likely due to an API change that needs
306 #error "Unsupported kernel: no usable disk change check"
307 #endif /* HAVE_BDEV_CHECK_MEDIA_CHANGE */
308 #endif /* HAVE_CHECK_DISK_CHANGE */
312 * The function was exported for use, prior to this it existed but the
313 * symbol was not exported.
315 * 4.4.0-6.21 API change for Ubuntu
316 * lookup_bdev() gained a second argument, FMODE_*, to check inode permissions.
319 * Changed to take a dev_t argument which is set on success and return a
320 * non-zero error code on failure.
323 vdev_lookup_bdev(const char *path, dev_t *dev)
325 #if defined(HAVE_DEVT_LOOKUP_BDEV)
326 return (lookup_bdev(path, dev));
327 #elif defined(HAVE_1ARG_LOOKUP_BDEV)
328 struct block_device *bdev = lookup_bdev(path);
330 return (PTR_ERR(bdev));
336 #elif defined(HAVE_MODE_LOOKUP_BDEV)
337 struct block_device *bdev = lookup_bdev(path, FMODE_READ);
339 return (PTR_ERR(bdev));
346 #error "Unsupported kernel"
351 * Kernels without bio_set_op_attrs use bi_rw for the bio flags.
353 #if !defined(HAVE_BIO_SET_OP_ATTRS)
355 bio_set_op_attrs(struct bio *bio, unsigned rw, unsigned flags)
357 bio->bi_rw |= rw | flags;
362 * bio_set_flush - Set the appropriate flags in a bio to guarantee
363 * data are on non-volatile media on completion.
366 * Introduce WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags as a
367 * replacement for WRITE_BARRIER to allow expressing richer semantics
368 * to the block layer. It's up to the block layer to implement the
369 * semantics correctly. Use the WRITE_FLUSH_FUA flag combination.
372 * REQ_FLUSH was renamed to REQ_PREFLUSH. For consistency with previous
373 * OpenZFS releases, prefer the WRITE_FLUSH_FUA flag set if it's available.
376 * The read/write flags and their modifiers, including WRITE_FLUSH,
377 * WRITE_FUA and WRITE_FLUSH_FUA were removed from fs.h in
378 * torvalds/linux@70fd7614 and replaced by direct flag modification
379 * of the REQ_ flags in bio->bi_opf. Use REQ_PREFLUSH.
382 bio_set_flush(struct bio *bio)
384 #if defined(HAVE_REQ_PREFLUSH) /* >= 4.10 */
385 bio_set_op_attrs(bio, 0, REQ_PREFLUSH);
386 #elif defined(WRITE_FLUSH_FUA) /* >= 2.6.37 and <= 4.9 */
387 bio_set_op_attrs(bio, 0, WRITE_FLUSH_FUA);
389 #error "Allowing the build will cause bio_set_flush requests to be ignored."
403 * in all cases but may have a performance impact for some kernels. It
404 * has the advantage of minimizing kernel specific changes in the zvol code.
407 static inline boolean_t
408 bio_is_flush(struct bio *bio)
410 #if defined(HAVE_REQ_OP_FLUSH) && defined(HAVE_BIO_BI_OPF)
411 return ((bio_op(bio) == REQ_OP_FLUSH) || (bio->bi_opf & REQ_PREFLUSH));
412 #elif defined(HAVE_REQ_PREFLUSH) && defined(HAVE_BIO_BI_OPF)
413 return (bio->bi_opf & REQ_PREFLUSH);
414 #elif defined(HAVE_REQ_PREFLUSH) && !defined(HAVE_BIO_BI_OPF)
415 return (bio->bi_rw & REQ_PREFLUSH);
416 #elif defined(HAVE_REQ_FLUSH)
417 return (bio->bi_rw & REQ_FLUSH);
419 #error "Unsupported kernel"
425 * REQ_FUA flag moved to bio->bi_opf
430 static inline boolean_t
431 bio_is_fua(struct bio *bio)
433 #if defined(HAVE_BIO_BI_OPF)
434 return (bio->bi_opf & REQ_FUA);
435 #elif defined(REQ_FUA)
436 return (bio->bi_rw & REQ_FUA);
438 #error "Allowing the build will cause fua requests to be ignored."
449 * In all cases the normal I/O path is used for discards. The only
450 * difference is how the kernel tags individual I/Os as discards.
452 static inline boolean_t
453 bio_is_discard(struct bio *bio)
455 #if defined(HAVE_REQ_OP_DISCARD)
456 return (bio_op(bio) == REQ_OP_DISCARD);
457 #elif defined(HAVE_REQ_DISCARD)
458 return (bio->bi_rw & REQ_DISCARD);
460 #error "Unsupported kernel"
466 * REQ_OP_SECURE_ERASE
471 static inline boolean_t
472 bio_is_secure_erase(struct bio *bio)
474 #if defined(HAVE_REQ_OP_SECURE_ERASE)
475 return (bio_op(bio) == REQ_OP_SECURE_ERASE);
476 #elif defined(REQ_SECURE)
477 return (bio->bi_rw & REQ_SECURE);
485 * Discard granularity and alignment restrictions may now be set. For
486 * older kernels which do not support this it is safe to skip it.
489 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
491 q->limits.discard_granularity = dg;
496 * blk_queue_secure_erase()
499 * blk_queue_secdiscard()
502 blk_queue_discard_secure(struct request_queue *q)
504 #if defined(HAVE_BLK_QUEUE_SECURE_ERASE)
505 return (blk_queue_secure_erase(q));
506 #elif defined(HAVE_BLK_QUEUE_SECDISCARD)
507 return (blk_queue_secdiscard(q));
514 * A common holder for vdev_bdev_open() is used to relax the exclusive open
515 * semantics slightly. Internal vdev disk callers may pass VDEV_HOLDER to
516 * allow them to open the device multiple times. Other kernel callers and
517 * user space processes which don't pass this value will get EBUSY. This is
518 * currently required for the correct operation of hot spares.
520 #define VDEV_HOLDER ((void *)0x2401de7)
522 static inline unsigned long
523 blk_generic_start_io_acct(struct request_queue *q __attribute__((unused)),
524 struct gendisk *disk __attribute__((unused)),
525 int rw __attribute__((unused)), struct bio *bio)
527 #if defined(HAVE_DISK_IO_ACCT)
528 return (disk_start_io_acct(disk, bio_sectors(bio), bio_op(bio)));
529 #elif defined(HAVE_BIO_IO_ACCT)
530 return (bio_start_io_acct(bio));
531 #elif defined(HAVE_GENERIC_IO_ACCT_3ARG)
532 unsigned long start_time = jiffies;
533 generic_start_io_acct(rw, bio_sectors(bio), &disk->part0);
535 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
536 unsigned long start_time = jiffies;
537 generic_start_io_acct(q, rw, bio_sectors(bio), &disk->part0);
546 blk_generic_end_io_acct(struct request_queue *q __attribute__((unused)),
547 struct gendisk *disk __attribute__((unused)),
548 int rw __attribute__((unused)), struct bio *bio, unsigned long start_time)
550 #if defined(HAVE_DISK_IO_ACCT)
551 disk_end_io_acct(disk, bio_op(bio), start_time);
552 #elif defined(HAVE_BIO_IO_ACCT)
553 bio_end_io_acct(bio, start_time);
554 #elif defined(HAVE_GENERIC_IO_ACCT_3ARG)
555 generic_end_io_acct(rw, &disk->part0, start_time);
556 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
557 generic_end_io_acct(q, rw, &disk->part0, start_time);
561 #ifndef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
562 static inline struct request_queue *
563 blk_generic_alloc_queue(make_request_fn make_request, int node_id)
565 #if defined(HAVE_BLK_ALLOC_QUEUE_REQUEST_FN)
566 return (blk_alloc_queue(make_request, node_id));
567 #elif defined(HAVE_BLK_ALLOC_QUEUE_REQUEST_FN_RH)
568 return (blk_alloc_queue_rh(make_request, node_id));
570 struct request_queue *q = blk_alloc_queue(GFP_KERNEL);
572 blk_queue_make_request(q, make_request);
577 #endif /* !HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
579 #endif /* _ZFS_BLKDEV_H */