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 #if !defined(HAVE_GET_DISK_AND_MODULE)
103 static inline struct kobject *
104 get_disk_and_module(struct gendisk *disk)
106 return (get_disk(disk));
110 #ifdef HAVE_BIO_BVEC_ITER
111 #define BIO_BI_SECTOR(bio) (bio)->bi_iter.bi_sector
112 #define BIO_BI_SIZE(bio) (bio)->bi_iter.bi_size
113 #define BIO_BI_IDX(bio) (bio)->bi_iter.bi_idx
114 #define BIO_BI_SKIP(bio) (bio)->bi_iter.bi_bvec_done
115 #define bio_for_each_segment4(bv, bvp, b, i) \
116 bio_for_each_segment((bv), (b), (i))
117 typedef struct bvec_iter bvec_iterator_t;
119 #define BIO_BI_SECTOR(bio) (bio)->bi_sector
120 #define BIO_BI_SIZE(bio) (bio)->bi_size
121 #define BIO_BI_IDX(bio) (bio)->bi_idx
122 #define BIO_BI_SKIP(bio) (0)
123 #define bio_for_each_segment4(bv, bvp, b, i) \
124 bio_for_each_segment((bvp), (b), (i))
125 typedef int bvec_iterator_t;
129 bio_set_flags_failfast(struct block_device *bdev, int *flags)
133 * Disable FAILFAST for loopback devices because of the
134 * following incorrect BUG_ON() in loop_make_request().
135 * This support is also disabled for md devices because the
136 * test suite layers md devices on top of loopback devices.
137 * This may be removed when the loopback driver is fixed.
139 * BUG_ON(!lo || (rw != READ && rw != WRITE));
141 if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
142 (MAJOR(bdev->bd_dev) == MD_MAJOR))
145 #ifdef BLOCK_EXT_MAJOR
146 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
148 #endif /* BLOCK_EXT_MAJOR */
149 #endif /* CONFIG_BUG */
151 *flags |= REQ_FAILFAST_MASK;
155 * Maximum disk label length, it may be undefined for some kernels.
157 #if !defined(DISK_NAME_LEN)
158 #define DISK_NAME_LEN 32
159 #endif /* DISK_NAME_LEN */
161 #ifdef HAVE_BIO_BI_STATUS
163 bi_status_to_errno(blk_status_t status)
168 case BLK_STS_NOTSUPP:
170 case BLK_STS_TIMEOUT:
174 case BLK_STS_TRANSPORT:
182 case BLK_STS_PROTECTION:
184 case BLK_STS_RESOURCE:
195 static inline blk_status_t
196 errno_to_bi_status(int error)
202 return (BLK_STS_NOTSUPP);
204 return (BLK_STS_TIMEOUT);
206 return (BLK_STS_NOSPC);
208 return (BLK_STS_TRANSPORT);
210 return (BLK_STS_TARGET);
212 return (BLK_STS_NEXUS);
214 return (BLK_STS_MEDIUM);
216 return (BLK_STS_PROTECTION);
218 return (BLK_STS_RESOURCE);
220 return (BLK_STS_AGAIN);
222 return (BLK_STS_IOERR);
224 return (BLK_STS_IOERR);
227 #endif /* HAVE_BIO_BI_STATUS */
231 * The bio_endio() prototype changed slightly. These are helper
232 * macro's to ensure the prototype and invocation are handled.
234 #ifdef HAVE_1ARG_BIO_END_IO_T
235 #ifdef HAVE_BIO_BI_STATUS
236 #define BIO_END_IO_ERROR(bio) bi_status_to_errno(bio->bi_status)
237 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x)
238 #define BIO_END_IO(bio, error) bio_set_bi_status(bio, error)
240 bio_set_bi_status(struct bio *bio, int error)
242 ASSERT3S(error, <=, 0);
243 bio->bi_status = errno_to_bi_status(-error);
247 #define BIO_END_IO_ERROR(bio) (-(bio->bi_error))
248 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x)
249 #define BIO_END_IO(bio, error) bio_set_bi_error(bio, error)
251 bio_set_bi_error(struct bio *bio, int error)
253 ASSERT3S(error, <=, 0);
254 bio->bi_error = error;
257 #endif /* HAVE_BIO_BI_STATUS */
260 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x, int z)
261 #define BIO_END_IO(bio, error) bio_endio(bio, error);
262 #endif /* HAVE_1ARG_BIO_END_IO_T */
266 * 3.10.0 CentOS 7.x API,
267 * blkdev_reread_part()
269 * For older kernels trigger a re-reading of the partition table by calling
270 * check_disk_change() which calls flush_disk() to invalidate the device.
272 #ifdef HAVE_BLKDEV_REREAD_PART
273 #define vdev_bdev_reread_part(bdev) blkdev_reread_part(bdev)
275 #define vdev_bdev_reread_part(bdev) check_disk_change(bdev)
276 #endif /* HAVE_BLKDEV_REREAD_PART */
280 * The function was exported for use, prior to this it existed but the
281 * symbol was not exported.
283 * 4.4.0-6.21 API change for Ubuntu
284 * lookup_bdev() gained a second argument, FMODE_*, to check inode permissions.
286 #ifdef HAVE_1ARG_LOOKUP_BDEV
287 #define vdev_lookup_bdev(path) lookup_bdev(path)
289 #ifdef HAVE_2ARGS_LOOKUP_BDEV
290 #define vdev_lookup_bdev(path) lookup_bdev(path, 0)
292 #error "Unsupported kernel"
293 #endif /* HAVE_2ARGS_LOOKUP_BDEV */
294 #endif /* HAVE_1ARG_LOOKUP_BDEV */
297 * Kernels without bio_set_op_attrs use bi_rw for the bio flags.
299 #if !defined(HAVE_BIO_SET_OP_ATTRS)
301 bio_set_op_attrs(struct bio *bio, unsigned rw, unsigned flags)
303 bio->bi_rw |= rw | flags;
308 * bio_set_flush - Set the appropriate flags in a bio to guarantee
309 * data are on non-volatile media on completion.
312 * Introduce WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags as a
313 * replacement for WRITE_BARRIER to allow expressing richer semantics
314 * to the block layer. It's up to the block layer to implement the
315 * semantics correctly. Use the WRITE_FLUSH_FUA flag combination.
318 * REQ_FLUSH was renamed to REQ_PREFLUSH. For consistency with previous
319 * ZoL releases, prefer the WRITE_FLUSH_FUA flag set if it's available.
322 * The read/write flags and their modifiers, including WRITE_FLUSH,
323 * WRITE_FUA and WRITE_FLUSH_FUA were removed from fs.h in
324 * torvalds/linux@70fd7614 and replaced by direct flag modification
325 * of the REQ_ flags in bio->bi_opf. Use REQ_PREFLUSH.
328 bio_set_flush(struct bio *bio)
330 #if defined(HAVE_REQ_PREFLUSH) /* >= 4.10 */
331 bio_set_op_attrs(bio, 0, REQ_PREFLUSH);
332 #elif defined(WRITE_FLUSH_FUA) /* >= 2.6.37 and <= 4.9 */
333 bio_set_op_attrs(bio, 0, WRITE_FLUSH_FUA);
335 #error "Allowing the build will cause bio_set_flush requests to be ignored."
349 * in all cases but may have a performance impact for some kernels. It
350 * has the advantage of minimizing kernel specific changes in the zvol code.
353 static inline boolean_t
354 bio_is_flush(struct bio *bio)
356 #if defined(HAVE_REQ_OP_FLUSH) && defined(HAVE_BIO_BI_OPF)
357 return ((bio_op(bio) == REQ_OP_FLUSH) || (bio->bi_opf & REQ_PREFLUSH));
358 #elif defined(HAVE_REQ_PREFLUSH) && defined(HAVE_BIO_BI_OPF)
359 return (bio->bi_opf & REQ_PREFLUSH);
360 #elif defined(HAVE_REQ_PREFLUSH) && !defined(HAVE_BIO_BI_OPF)
361 return (bio->bi_rw & REQ_PREFLUSH);
362 #elif defined(HAVE_REQ_FLUSH)
363 return (bio->bi_rw & REQ_FLUSH);
365 #error "Unsupported kernel"
371 * REQ_FUA flag moved to bio->bi_opf
376 static inline boolean_t
377 bio_is_fua(struct bio *bio)
379 #if defined(HAVE_BIO_BI_OPF)
380 return (bio->bi_opf & REQ_FUA);
381 #elif defined(REQ_FUA)
382 return (bio->bi_rw & REQ_FUA);
384 #error "Allowing the build will cause fua requests to be ignored."
395 * In all cases the normal I/O path is used for discards. The only
396 * difference is how the kernel tags individual I/Os as discards.
398 static inline boolean_t
399 bio_is_discard(struct bio *bio)
401 #if defined(HAVE_REQ_OP_DISCARD)
402 return (bio_op(bio) == REQ_OP_DISCARD);
403 #elif defined(HAVE_REQ_DISCARD)
404 return (bio->bi_rw & REQ_DISCARD);
406 #error "Unsupported kernel"
412 * REQ_OP_SECURE_ERASE
417 static inline boolean_t
418 bio_is_secure_erase(struct bio *bio)
420 #if defined(HAVE_REQ_OP_SECURE_ERASE)
421 return (bio_op(bio) == REQ_OP_SECURE_ERASE);
422 #elif defined(REQ_SECURE)
423 return (bio->bi_rw & REQ_SECURE);
431 * Discard granularity and alignment restrictions may now be set. For
432 * older kernels which do not support this it is safe to skip it.
435 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
437 q->limits.discard_granularity = dg;
442 * blk_queue_secure_erase()
445 * blk_queue_secdiscard()
448 blk_queue_discard_secure(struct request_queue *q)
450 #if defined(HAVE_BLK_QUEUE_SECURE_ERASE)
451 return (blk_queue_secure_erase(q));
452 #elif defined(HAVE_BLK_QUEUE_SECDISCARD)
453 return (blk_queue_secdiscard(q));
460 * A common holder for vdev_bdev_open() is used to relax the exclusive open
461 * semantics slightly. Internal vdev disk callers may pass VDEV_HOLDER to
462 * allow them to open the device multiple times. Other kernel callers and
463 * user space processes which don't pass this value will get EBUSY. This is
464 * currently required for the correct operation of hot spares.
466 #define VDEV_HOLDER ((void *)0x2401de7)
469 blk_generic_start_io_acct(struct request_queue *q, int rw,
470 unsigned long sectors, struct hd_struct *part)
472 #if defined(HAVE_GENERIC_IO_ACCT_3ARG)
473 generic_start_io_acct(rw, sectors, part);
474 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
475 generic_start_io_acct(q, rw, sectors, part);
480 blk_generic_end_io_acct(struct request_queue *q, int rw,
481 struct hd_struct *part, unsigned long start_time)
483 #if defined(HAVE_GENERIC_IO_ACCT_3ARG)
484 generic_end_io_acct(rw, part, start_time);
485 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
486 generic_end_io_acct(q, rw, part, start_time);
490 #endif /* _ZFS_BLKDEV_H */