]> git.proxmox.com Git - mirror_zfs.git/blame - include/linux/blkdev_compat.h
Update documentation for zfs_vdev_queue_depth_pct
[mirror_zfs.git] / include / linux / blkdev_compat.h
CommitLineData
60101509
BB
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 http://www.opensolaris.org/os/licensing.
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 */
61e90960 21
60101509 22/*
61e90960 23 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
60101509
BB
24 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
25 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
26 * LLNL-CODE-403049.
27 */
28
61e90960 29#ifndef _ZFS_BLKDEV_H
d1d7e268 30#define _ZFS_BLKDEV_H
60101509
BB
31
32#include <linux/blkdev.h>
33#include <linux/elevator.h>
34
35#ifndef HAVE_FMODE_T
36typedef unsigned __bitwise__ fmode_t;
37#endif /* HAVE_FMODE_T */
38
b18019d2 39/*
cf41432c
BB
40 * 4.7 - 4.x API,
41 * The blk_queue_write_cache() interface has replaced blk_queue_flush()
42 * interface. However, the new interface is GPL-only thus we implement
43 * our own trivial wrapper when the GPL-only version is detected.
44 *
45 * 2.6.36 - 4.6 API,
b18019d2
ED
46 * The blk_queue_flush() interface has replaced blk_queue_ordered()
47 * interface. However, while the old interface was available to all the
48 * new one is GPL-only. Thus if the GPL-only version is detected we
cf41432c
BB
49 * implement our own trivial helper.
50 *
51 * 2.6.x - 2.6.35
52 * Legacy blk_queue_ordered() interface.
68e8f59a 53 */
68e8f59a 54static inline void
cf41432c 55blk_queue_set_write_cache(struct request_queue *q, bool wc, bool fua)
68e8f59a 56{
cf41432c 57#if defined(HAVE_BLK_QUEUE_WRITE_CACHE_GPL_ONLY)
68e8f59a
CC
58 spin_lock_irq(q->queue_lock);
59 if (wc)
60 queue_flag_set(QUEUE_FLAG_WC, q);
61 else
62 queue_flag_clear(QUEUE_FLAG_WC, q);
63 if (fua)
64 queue_flag_set(QUEUE_FLAG_FUA, q);
65 else
66 queue_flag_clear(QUEUE_FLAG_FUA, q);
67 spin_unlock_irq(q->queue_lock);
cf41432c
BB
68#elif defined(HAVE_BLK_QUEUE_WRITE_CACHE)
69 blk_queue_write_cache(q, wc, fua);
70#elif defined(HAVE_BLK_QUEUE_FLUSH_GPL_ONLY)
71 if (wc)
72 q->flush_flags |= REQ_FLUSH;
73 if (fua)
74 q->flush_flags |= REQ_FUA;
75#elif defined(HAVE_BLK_QUEUE_FLUSH)
76 blk_queue_flush(q, (wc ? REQ_FLUSH : 0) | (fua ? REQ_FUA : 0));
77#else
78 blk_queue_ordered(q, QUEUE_ORDERED_DRAIN, NULL);
68e8f59a 79#endif
cf41432c 80}
68e8f59a 81
8326eb46
BB
82/*
83 * Most of the blk_* macros were removed in 2.6.36. Ostensibly this was
84 * done to improve readability and allow easier grepping. However, from
85 * a portability stand point the macros are helpful. Therefore the needed
86 * macros are redefined here if they are missing from the kernel.
87 */
88#ifndef blk_fs_request
d1d7e268 89#define blk_fs_request(rq) ((rq)->cmd_type == REQ_TYPE_FS)
8326eb46
BB
90#endif
91
3517f0b7
BB
92/*
93 * 2.6.27 API change,
94 * The blk_queue_stackable() queue flag was added in 2.6.27 to handle dm
95 * stacking drivers. Prior to this request stacking drivers were detected
96 * by checking (q->request_fn == NULL), for earlier kernels we revert to
97 * this legacy behavior.
98 */
99#ifndef blk_queue_stackable
d1d7e268 100#define blk_queue_stackable(q) ((q)->request_fn == NULL)
3517f0b7
BB
101#endif
102
34037afe
ED
103/*
104 * 2.6.34 API change,
105 * The blk_queue_max_hw_sectors() function replaces blk_queue_max_sectors().
106 */
107#ifndef HAVE_BLK_QUEUE_MAX_HW_SECTORS
d1d7e268 108#define blk_queue_max_hw_sectors __blk_queue_max_hw_sectors
34037afe
ED
109static inline void
110__blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
111{
112 blk_queue_max_sectors(q, max_hw_sectors);
113}
114#endif
115
116/*
117 * 2.6.34 API change,
118 * The blk_queue_max_segments() function consolidates
119 * blk_queue_max_hw_segments() and blk_queue_max_phys_segments().
120 */
121#ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
d1d7e268 122#define blk_queue_max_segments __blk_queue_max_segments
34037afe
ED
123static inline void
124__blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
125{
126 blk_queue_max_phys_segments(q, max_segments);
127 blk_queue_max_hw_segments(q, max_segments);
128}
129#endif
130
60101509
BB
131#ifndef HAVE_GET_DISK_RO
132static inline int
133get_disk_ro(struct gendisk *disk)
134{
135 int policy = 0;
136
137 if (disk->part[0])
138 policy = disk->part[0]->policy;
139
d1d7e268 140 return (policy);
60101509
BB
141}
142#endif /* HAVE_GET_DISK_RO */
143
d4541210
CC
144#ifdef HAVE_BIO_BVEC_ITER
145#define BIO_BI_SECTOR(bio) (bio)->bi_iter.bi_sector
146#define BIO_BI_SIZE(bio) (bio)->bi_iter.bi_size
147#define BIO_BI_IDX(bio) (bio)->bi_iter.bi_idx
2727b9d3 148#define BIO_BI_SKIP(bio) (bio)->bi_iter.bi_bvec_done
37f9dac5
RY
149#define bio_for_each_segment4(bv, bvp, b, i) \
150 bio_for_each_segment((bv), (b), (i))
151typedef struct bvec_iter bvec_iterator_t;
d4541210
CC
152#else
153#define BIO_BI_SECTOR(bio) (bio)->bi_sector
154#define BIO_BI_SIZE(bio) (bio)->bi_size
155#define BIO_BI_IDX(bio) (bio)->bi_idx
2727b9d3 156#define BIO_BI_SKIP(bio) (0)
37f9dac5
RY
157#define bio_for_each_segment4(bv, bvp, b, i) \
158 bio_for_each_segment((bvp), (b), (i))
159typedef int bvec_iterator_t;
d4541210
CC
160#endif
161
61e90960
BB
162/*
163 * Portable helper for correctly setting the FAILFAST flags. The
164 * correct usage has changed 3 times from 2.6.12 to 2.6.38.
165 */
2959d94a
BB
166static inline void
167bio_set_flags_failfast(struct block_device *bdev, int *flags)
168{
f4af6bb7 169#ifdef CONFIG_BUG
2959d94a 170 /*
f4af6bb7
BB
171 * Disable FAILFAST for loopback devices because of the
172 * following incorrect BUG_ON() in loop_make_request().
2959d94a
BB
173 * This support is also disabled for md devices because the
174 * test suite layers md devices on top of loopback devices.
175 * This may be removed when the loopback driver is fixed.
176 *
177 * BUG_ON(!lo || (rw != READ && rw != WRITE));
178 */
2959d94a
BB
179 if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
180 (MAJOR(bdev->bd_dev) == MD_MAJOR))
181 return;
182
183#ifdef BLOCK_EXT_MAJOR
184 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
185 return;
186#endif /* BLOCK_EXT_MAJOR */
187#endif /* CONFIG_BUG */
f4af6bb7 188
e4853338 189#if defined(HAVE_BIO_RW_FAILFAST_DTD)
f4af6bb7 190 /* BIO_RW_FAILFAST_* preferred interface from 2.6.28 - 2.6.35 */
d1d7e268
MK
191 *flags |= (
192 (1 << BIO_RW_FAILFAST_DEV) |
193 (1 << BIO_RW_FAILFAST_TRANSPORT) |
194 (1 << BIO_RW_FAILFAST_DRIVER));
e4853338 195#elif defined(HAVE_REQ_FAILFAST_MASK)
d1d7e268
MK
196 /*
197 * REQ_FAILFAST_* preferred interface from 2.6.36 - 2.6.xx,
198 * the BIO_* and REQ_* flags were unified under REQ_* flags.
199 */
f4af6bb7 200 *flags |= REQ_FAILFAST_MASK;
e4853338
TC
201#else
202#error "Undefined block IO FAILFAST interface."
203#endif
2959d94a
BB
204}
205
61e90960
BB
206/*
207 * Maximum disk label length, it may be undefined for some kernels.
208 */
60101509 209#ifndef DISK_NAME_LEN
d1d7e268 210#define DISK_NAME_LEN 32
60101509
BB
211#endif /* DISK_NAME_LEN */
212
61e90960 213/*
784a7fe5
LW
214 * 4.3 API change
215 * The bio_endio() prototype changed slightly. These are helper
216 * macro's to ensure the prototype and invocation are handled.
61e90960 217 */
784a7fe5
LW
218#ifdef HAVE_1ARG_BIO_END_IO_T
219#define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x)
220#define BIO_END_IO(bio, error) bio->bi_error = error; bio_endio(bio);
61e90960 221#else
784a7fe5
LW
222#define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x, int z)
223#define BIO_END_IO(bio, error) bio_endio(bio, error);
224#endif /* HAVE_1ARG_BIO_END_IO_T */
61e90960
BB
225
226/*
45066d1f
BB
227 * 2.6.38 - 2.6.x API,
228 * blkdev_get_by_path()
229 * blkdev_put()
230 *
231 * 2.6.28 - 2.6.37 API,
232 * open_bdev_exclusive()
233 * close_bdev_exclusive()
234 *
235 * 2.6.12 - 2.6.27 API,
236 * open_bdev_excl()
237 * close_bdev_excl()
238 *
61e90960
BB
239 * Used to exclusively open a block device from within the kernel.
240 */
45066d1f 241#if defined(HAVE_BLKDEV_GET_BY_PATH)
d1d7e268 242#define vdev_bdev_open(path, md, hld) blkdev_get_by_path(path, \
45066d1f 243 (md) | FMODE_EXCL, hld)
d1d7e268 244#define vdev_bdev_close(bdev, md) blkdev_put(bdev, (md) | FMODE_EXCL)
45066d1f 245#elif defined(HAVE_OPEN_BDEV_EXCLUSIVE)
d1d7e268
MK
246#define vdev_bdev_open(path, md, hld) open_bdev_exclusive(path, md, hld)
247#define vdev_bdev_close(bdev, md) close_bdev_exclusive(bdev, md)
61e90960 248#else
d1d7e268
MK
249#define vdev_bdev_open(path, md, hld) open_bdev_excl(path, md, hld)
250#define vdev_bdev_close(bdev, md) close_bdev_excl(bdev)
45066d1f 251#endif /* HAVE_BLKDEV_GET_BY_PATH | HAVE_OPEN_BDEV_EXCLUSIVE */
61e90960
BB
252
253/*
254 * 2.6.22 API change
255 * The function invalidate_bdev() lost it's second argument because
256 * it was unused.
257 */
258#ifdef HAVE_1ARG_INVALIDATE_BDEV
d1d7e268 259#define vdev_bdev_invalidate(bdev) invalidate_bdev(bdev)
61e90960 260#else
d1d7e268 261#define vdev_bdev_invalidate(bdev) invalidate_bdev(bdev, 1)
61e90960
BB
262#endif /* HAVE_1ARG_INVALIDATE_BDEV */
263
2b7ab9d4
BB
264/*
265 * 2.6.27 API change
e02aaf17 266 * The function was exported for use, prior to this it existed but the
2b7ab9d4 267 * symbol was not exported.
e02aaf17
HM
268 *
269 * 4.4.0-6.21 API change for Ubuntu
270 * lookup_bdev() gained a second argument, FMODE_*, to check inode permissions.
2b7ab9d4 271 */
e02aaf17
HM
272#ifdef HAVE_1ARG_LOOKUP_BDEV
273#define vdev_lookup_bdev(path) lookup_bdev(path)
274#else
275#ifdef HAVE_2ARGS_LOOKUP_BDEV
276#define vdev_lookup_bdev(path) lookup_bdev(path, 0)
277#else
278#define vdev_lookup_bdev(path) ERR_PTR(-ENOTSUP)
279#endif /* HAVE_2ARGS_LOOKUP_BDEV */
280#endif /* HAVE_1ARG_LOOKUP_BDEV */
2b7ab9d4 281
61e90960
BB
282/*
283 * 2.6.30 API change
2404b014
BB
284 * To ensure good performance preferentially use the physical block size
285 * for proper alignment. The physical size is supposed to be the internal
286 * sector size used by the device. This is often 4096 byte for AF devices,
287 * while a smaller 512 byte logical size is supported for compatibility.
288 *
289 * Unfortunately, many drives still misreport their physical sector size.
290 * For devices which are known to lie you may need to manually set this
291 * at pool creation time with 'zpool create -o ashift=12 ...'.
292 *
293 * When the physical block size interface isn't available, we fall back to
294 * the logical block size interface and then the older hard sector size.
61e90960 295 */
2404b014 296#ifdef HAVE_BDEV_PHYSICAL_BLOCK_SIZE
d1d7e268
MK
297#define vdev_bdev_block_size(bdev) bdev_physical_block_size(bdev)
298#else
299#ifdef HAVE_BDEV_LOGICAL_BLOCK_SIZE
300#define vdev_bdev_block_size(bdev) bdev_logical_block_size(bdev)
61e90960 301#else
d1d7e268
MK
302#define vdev_bdev_block_size(bdev) bdev_hardsect_size(bdev)
303#endif /* HAVE_BDEV_LOGICAL_BLOCK_SIZE */
2404b014 304#endif /* HAVE_BDEV_PHYSICAL_BLOCK_SIZE */
61e90960 305
a5e046ea 306#ifndef HAVE_BIO_SET_OP_ATTRS
96801d29 307/*
a5e046ea 308 * Kernels without bio_set_op_attrs use bi_rw for the bio flags.
96801d29 309 */
a5e046ea
TC
310static inline void
311bio_set_op_attrs(struct bio *bio, unsigned rw, unsigned flags)
312{
313 bio->bi_rw |= rw | flags;
314}
315#endif
316
317/*
318 * bio_set_flush - Set the appropriate flags in a bio to guarantee
319 * data are on non-volatile media on completion.
320 *
321 * 2.6.X - 2.6.36 API,
322 * WRITE_BARRIER - Tells the block layer to commit all previously submitted
323 * writes to stable storage before this one is started and that the current
324 * write is on stable storage upon completion. Also prevents reordering
325 * on both sides of the current operation.
326 *
327 * 2.6.37 - 4.8 API,
328 * Introduce WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags as a
329 * replacement for WRITE_BARRIER to allow expressing richer semantics
330 * to the block layer. It's up to the block layer to implement the
331 * semantics correctly. Use the WRITE_FLUSH_FUA flag combination.
332 *
333 * 4.8 - 4.9 API,
334 * REQ_FLUSH was renamed to REQ_PREFLUSH. For consistency with previous
335 * ZoL releases, prefer the WRITE_FLUSH_FUA flag set if it's available.
336 *
337 * 4.10 API,
338 * The read/write flags and their modifiers, including WRITE_FLUSH,
339 * WRITE_FUA and WRITE_FLUSH_FUA were removed from fs.h in
340 * torvalds/linux@70fd7614 and replaced by direct flag modification
341 * of the REQ_ flags in bio->bi_opf. Use REQ_PREFLUSH.
342 */
343static inline void
344bio_set_flush(struct bio *bio)
345{
46300986
TH
346#if defined(REQ_PREFLUSH) /* >= 4.10 */
347 bio_set_op_attrs(bio, 0, REQ_PREFLUSH);
a5e046ea
TC
348#elif defined(WRITE_FLUSH_FUA) /* >= 2.6.37 and <= 4.9 */
349 bio_set_op_attrs(bio, 0, WRITE_FLUSH_FUA);
46300986
TH
350#elif defined(WRITE_BARRIER) /* < 2.6.37 */
351 bio_set_op_attrs(bio, 0, WRITE_BARRIER);
76e5f6fe 352#else
a5e046ea 353#error "Allowing the build will cause bio_set_flush requests to be ignored."
76e5f6fe 354#endif
a5e046ea 355}
76e5f6fe 356
cf41432c
BB
357/*
358 * 4.8 - 4.x API,
359 * REQ_OP_FLUSH
360 *
361 * 4.8-rc0 - 4.8-rc1,
362 * REQ_PREFLUSH
363 *
364 * 2.6.36 - 4.7 API,
365 * REQ_FLUSH
366 *
367 * 2.6.x - 2.6.35 API,
368 * HAVE_BIO_RW_BARRIER
369 *
370 * Used to determine if a cache flush has been requested. This check has
371 * been left intentionally broad in order to cover both a legacy flush
372 * and the new preflush behavior introduced in Linux 4.8. This is correct
373 * in all cases but may have a performance impact for some kernels. It
374 * has the advantage of minimizing kernel specific changes in the zvol code.
6eb73b00 375 *
cf41432c
BB
376 */
377static inline boolean_t
378bio_is_flush(struct bio *bio)
379{
380#if defined(HAVE_REQ_OP_FLUSH) && defined(HAVE_BIO_BI_OPF)
381 return ((bio_op(bio) == REQ_OP_FLUSH) || (bio->bi_opf & REQ_PREFLUSH));
382#elif defined(REQ_PREFLUSH) && defined(HAVE_BIO_BI_OPF)
383 return (bio->bi_opf & REQ_PREFLUSH);
384#elif defined(REQ_PREFLUSH) && !defined(HAVE_BIO_BI_OPF)
385 return (bio->bi_rw & REQ_PREFLUSH);
6eb73b00
BB
386#elif defined(REQ_FLUSH)
387 return (bio->bi_rw & REQ_FLUSH);
46300986
TH
388#elif defined(HAVE_BIO_RW_BARRIER)
389 return (bio->bi_rw & (1 << BIO_RW_BARRIER));
96801d29 390#else
5fc73c46 391#error "Allowing the build will cause flush requests to be ignored."
96801d29 392#endif
cf41432c 393}
76e5f6fe 394
cf41432c
BB
395/*
396 * 4.8 - 4.x API,
397 * REQ_FUA flag moved to bio->bi_opf
398 *
399 * 2.6.x - 4.7 API,
400 * REQ_FUA
401 */
402static inline boolean_t
403bio_is_fua(struct bio *bio)
404{
405#if defined(HAVE_BIO_BI_OPF)
406 return (bio->bi_opf & REQ_FUA);
407#elif defined(REQ_FUA)
408 return (bio->bi_rw & REQ_FUA);
409#else
5fc73c46 410#error "Allowing the build will cause fua requests to be ignored."
37f9dac5 411#endif
cf41432c 412}
96801d29 413
30930fba 414/*
cf41432c
BB
415 * 4.8 - 4.x API,
416 * REQ_OP_DISCARD
3b86aeb2
CC
417 *
418 * 2.6.36 - 4.7 API,
419 * REQ_DISCARD
420 *
cf41432c
BB
421 * 2.6.28 - 2.6.35 API,
422 * BIO_RW_DISCARD
3b86aeb2
CC
423 *
424 * In all cases the normal I/O path is used for discards. The only
425 * difference is how the kernel tags individual I/Os as discards.
6eb73b00
BB
426 *
427 * Note that 2.6.32 era kernels provide both BIO_RW_DISCARD and REQ_DISCARD,
428 * where BIO_RW_DISCARD is the correct interface. Therefore, it is important
429 * that the HAVE_BIO_RW_DISCARD check occur before the REQ_DISCARD check.
30930fba 430 */
3b86aeb2
CC
431static inline boolean_t
432bio_is_discard(struct bio *bio)
433{
cf41432c
BB
434#if defined(HAVE_REQ_OP_DISCARD)
435 return (bio_op(bio) == REQ_OP_DISCARD);
cf41432c
BB
436#elif defined(HAVE_BIO_RW_DISCARD)
437 return (bio->bi_rw & (1 << BIO_RW_DISCARD));
6eb73b00
BB
438#elif defined(REQ_DISCARD)
439 return (bio->bi_rw & REQ_DISCARD);
37f9dac5 440#else
5fc73c46 441/* potentially triggering the DMU_MAX_ACCESS assertion. */
442#error "Allowing the build will cause discard requests to become writes."
30930fba 443#endif
3b86aeb2 444}
cf41432c
BB
445
446/*
447 * 4.8 - 4.x API,
448 * REQ_OP_SECURE_ERASE
449 *
450 * 2.6.36 - 4.7 API,
451 * REQ_SECURE
452 *
453 * 2.6.x - 2.6.35 API,
454 * Unsupported by kernel
455 */
456static inline boolean_t
457bio_is_secure_erase(struct bio *bio)
458{
459#if defined(HAVE_REQ_OP_SECURE_ERASE)
460 return (bio_op(bio) == REQ_OP_SECURE_ERASE);
461#elif defined(REQ_SECURE)
462 return (bio->bi_rw & REQ_SECURE);
37f9dac5 463#else
cf41432c 464 return (0);
37f9dac5 465#endif
cf41432c 466}
30930fba 467
ee5fd0bb
ED
468/*
469 * 2.6.33 API change
470 * Discard granularity and alignment restrictions may now be set. For
471 * older kernels which do not support this it is safe to skip it.
472 */
473#ifdef HAVE_DISCARD_GRANULARITY
474static inline void
475blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
476{
477 q->limits.discard_granularity = dg;
478}
479#else
d1d7e268 480#define blk_queue_discard_granularity(x, dg) ((void)0)
ee5fd0bb
ED
481#endif /* HAVE_DISCARD_GRANULARITY */
482
61e90960
BB
483/*
484 * Default Linux IO Scheduler,
485 * Setting the scheduler to noop will allow the Linux IO scheduler to
486 * still perform front and back merging, while leaving the request
487 * ordering and prioritization to the ZFS IO scheduler.
488 */
489#define VDEV_SCHEDULER "noop"
60101509 490
8128bd89
BB
491/*
492 * A common holder for vdev_bdev_open() is used to relax the exclusive open
493 * semantics slightly. Internal vdev disk callers may pass VDEV_HOLDER to
494 * allow them to open the device multiple times. Other kernel callers and
495 * user space processes which don't pass this value will get EBUSY. This is
496 * currently required for the correct operation of hot spares.
497 */
d1d7e268 498#define VDEV_HOLDER ((void *)0x2401de7)
8128bd89 499
8198d18c
RY
500#ifndef HAVE_GENERIC_IO_ACCT
501#define generic_start_io_acct(rw, slen, part) ((void)0)
502#define generic_end_io_acct(rw, part, start_jiffies) ((void)0)
503#endif
504
61e90960 505#endif /* _ZFS_BLKDEV_H */