]> granicus.if.org Git - zfs/blob - include/linux/blkdev_compat.h
Remove blk_fetch_request() autotools check
[zfs] / include / linux / blkdev_compat.h
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  */
21
22 /*
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>.
26  * LLNL-CODE-403049.
27  */
28
29 #ifndef _ZFS_BLKDEV_H
30 #define _ZFS_BLKDEV_H
31
32 #include <linux/blkdev.h>
33 #include <linux/elevator.h>
34
35 #ifndef HAVE_FMODE_T
36 typedef unsigned __bitwise__ fmode_t;
37 #endif /* HAVE_FMODE_T */
38
39 /*
40  * 2.6.36 API change,
41  * The blk_queue_flush() interface has replaced blk_queue_ordered()
42  * interface.  However, while the old interface was available to all the
43  * new one is GPL-only.   Thus if the GPL-only version is detected we
44  * implement our own trivial helper compatibility funcion.   The hope is
45  * that long term this function will be opened up.
46  */
47 #if defined(HAVE_BLK_QUEUE_FLUSH) && defined(HAVE_BLK_QUEUE_FLUSH_GPL_ONLY)
48 #define blk_queue_flush __blk_queue_flush
49 static inline void
50 __blk_queue_flush(struct request_queue *q, unsigned int flags)
51 {
52         q->flush_flags = flags & (REQ_FLUSH | REQ_FUA);
53 }
54 #endif /* HAVE_BLK_QUEUE_FLUSH && HAVE_BLK_QUEUE_FLUSH_GPL_ONLY */
55
56 #ifndef HAVE_BLK_RQ_POS
57 static inline sector_t
58 blk_rq_pos(struct request *req)
59 {
60         return (req->sector);
61 }
62 #endif /* HAVE_BLK_RQ_POS */
63
64 #ifndef HAVE_BLK_RQ_SECTORS
65 static inline unsigned int
66 blk_rq_sectors(struct request *req)
67 {
68         return (req->nr_sectors);
69 }
70 #endif /* HAVE_BLK_RQ_SECTORS */
71
72 #if !defined(HAVE_BLK_RQ_BYTES) || defined(HAVE_BLK_RQ_BYTES_GPL_ONLY)
73 /*
74  * Define required to avoid conflicting 2.6.29 non-static prototype for a
75  * GPL-only version of the helper.  As of 2.6.31 the helper is available
76  * to non-GPL modules in the form of a static inline in the header.
77  */
78 #define blk_rq_bytes __blk_rq_bytes
79 static inline unsigned int
80 __blk_rq_bytes(struct request *req)
81 {
82         return (blk_rq_sectors(req) << 9);
83 }
84 #endif /* !HAVE_BLK_RQ_BYTES || HAVE_BLK_RQ_BYTES_GPL_ONLY */
85
86 /*
87  * Most of the blk_* macros were removed in 2.6.36.  Ostensibly this was
88  * done to improve readability and allow easier grepping.  However, from
89  * a portability stand point the macros are helpful.  Therefore the needed
90  * macros are redefined here if they are missing from the kernel.
91  */
92 #ifndef blk_fs_request
93 #define blk_fs_request(rq)      ((rq)->cmd_type == REQ_TYPE_FS)
94 #endif
95
96 /*
97  * 2.6.27 API change,
98  * The blk_queue_stackable() queue flag was added in 2.6.27 to handle dm
99  * stacking drivers.  Prior to this request stacking drivers were detected
100  * by checking (q->request_fn == NULL), for earlier kernels we revert to
101  * this legacy behavior.
102  */
103 #ifndef blk_queue_stackable
104 #define blk_queue_stackable(q)  ((q)->request_fn == NULL)
105 #endif
106
107 /*
108  * 2.6.34 API change,
109  * The blk_queue_max_hw_sectors() function replaces blk_queue_max_sectors().
110  */
111 #ifndef HAVE_BLK_QUEUE_MAX_HW_SECTORS
112 #define blk_queue_max_hw_sectors __blk_queue_max_hw_sectors
113 static inline void
114 __blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
115 {
116         blk_queue_max_sectors(q, max_hw_sectors);
117 }
118 #endif
119
120 /*
121  * 2.6.34 API change,
122  * The blk_queue_max_segments() function consolidates
123  * blk_queue_max_hw_segments() and blk_queue_max_phys_segments().
124  */
125 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
126 #define blk_queue_max_segments __blk_queue_max_segments
127 static inline void
128 __blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
129 {
130         blk_queue_max_phys_segments(q, max_segments);
131         blk_queue_max_hw_segments(q, max_segments);
132 }
133 #endif
134
135 #ifndef HAVE_GET_DISK_RO
136 static inline int
137 get_disk_ro(struct gendisk *disk)
138 {
139         int policy = 0;
140
141         if (disk->part[0])
142                 policy = disk->part[0]->policy;
143
144         return (policy);
145 }
146 #endif /* HAVE_GET_DISK_RO */
147
148 #ifdef HAVE_BIO_BVEC_ITER
149 #define BIO_BI_SECTOR(bio)      (bio)->bi_iter.bi_sector
150 #define BIO_BI_SIZE(bio)        (bio)->bi_iter.bi_size
151 #define BIO_BI_IDX(bio)         (bio)->bi_iter.bi_idx
152 #define bio_for_each_segment4(bv, bvp, b, i)    \
153         bio_for_each_segment((bv), (b), (i))
154 typedef struct bvec_iter bvec_iterator_t;
155 #else
156 #define BIO_BI_SECTOR(bio)      (bio)->bi_sector
157 #define BIO_BI_SIZE(bio)        (bio)->bi_size
158 #define BIO_BI_IDX(bio)         (bio)->bi_idx
159 #define bio_for_each_segment4(bv, bvp, b, i)    \
160         bio_for_each_segment((bvp), (b), (i))
161 typedef int bvec_iterator_t;
162 #endif
163
164 /*
165  * Portable helper for correctly setting the FAILFAST flags.  The
166  * correct usage has changed 3 times from 2.6.12 to 2.6.38.
167  */
168 static inline void
169 bio_set_flags_failfast(struct block_device *bdev, int *flags)
170 {
171 #ifdef CONFIG_BUG
172         /*
173          * Disable FAILFAST for loopback devices because of the
174          * following incorrect BUG_ON() in loop_make_request().
175          * This support is also disabled for md devices because the
176          * test suite layers md devices on top of loopback devices.
177          * This may be removed when the loopback driver is fixed.
178          *
179          *   BUG_ON(!lo || (rw != READ && rw != WRITE));
180          */
181         if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
182             (MAJOR(bdev->bd_dev) == MD_MAJOR))
183                 return;
184
185 #ifdef BLOCK_EXT_MAJOR
186         if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
187                 return;
188 #endif /* BLOCK_EXT_MAJOR */
189 #endif /* CONFIG_BUG */
190
191 #if defined(HAVE_BIO_RW_FAILFAST_DTD)
192         /* BIO_RW_FAILFAST_* preferred interface from 2.6.28 - 2.6.35 */
193         *flags |= (
194             (1 << BIO_RW_FAILFAST_DEV) |
195             (1 << BIO_RW_FAILFAST_TRANSPORT) |
196             (1 << BIO_RW_FAILFAST_DRIVER));
197 #elif defined(HAVE_REQ_FAILFAST_MASK)
198         /*
199          * REQ_FAILFAST_* preferred interface from 2.6.36 - 2.6.xx,
200          * the BIO_* and REQ_* flags were unified under REQ_* flags.
201          */
202         *flags |= REQ_FAILFAST_MASK;
203 #else
204 #error "Undefined block IO FAILFAST interface."
205 #endif
206 }
207
208 /*
209  * Maximum disk label length, it may be undefined for some kernels.
210  */
211 #ifndef DISK_NAME_LEN
212 #define DISK_NAME_LEN   32
213 #endif /* DISK_NAME_LEN */
214
215 /*
216  * 2.6.24 API change,
217  * The bio_end_io() prototype changed slightly.  These are helper
218  * macro's to ensure the prototype and return value are handled.
219  */
220 #ifdef HAVE_2ARGS_BIO_END_IO_T
221 #define BIO_END_IO_PROTO(fn, x, y, z)   static void fn(struct bio *x, int z)
222 #define BIO_END_IO_RETURN(rc)           return
223 #else
224 #define BIO_END_IO_PROTO(fn, x, y, z)   static int fn( \
225                                             struct bio *x, \
226                                             unsigned int y, \
227                                             int z)
228 #define BIO_END_IO_RETURN(rc)           return rc
229 #endif /* HAVE_2ARGS_BIO_END_IO_T */
230
231 /*
232  * 2.6.38 - 2.6.x API,
233  *   blkdev_get_by_path()
234  *   blkdev_put()
235  *
236  * 2.6.28 - 2.6.37 API,
237  *   open_bdev_exclusive()
238  *   close_bdev_exclusive()
239  *
240  * 2.6.12 - 2.6.27 API,
241  *   open_bdev_excl()
242  *   close_bdev_excl()
243  *
244  * Used to exclusively open a block device from within the kernel.
245  */
246 #if defined(HAVE_BLKDEV_GET_BY_PATH)
247 #define vdev_bdev_open(path, md, hld)   blkdev_get_by_path(path, \
248                                             (md) | FMODE_EXCL, hld)
249 #define vdev_bdev_close(bdev, md)       blkdev_put(bdev, (md) | FMODE_EXCL)
250 #elif defined(HAVE_OPEN_BDEV_EXCLUSIVE)
251 #define vdev_bdev_open(path, md, hld)   open_bdev_exclusive(path, md, hld)
252 #define vdev_bdev_close(bdev, md)       close_bdev_exclusive(bdev, md)
253 #else
254 #define vdev_bdev_open(path, md, hld)   open_bdev_excl(path, md, hld)
255 #define vdev_bdev_close(bdev, md)       close_bdev_excl(bdev)
256 #endif /* HAVE_BLKDEV_GET_BY_PATH | HAVE_OPEN_BDEV_EXCLUSIVE */
257
258 /*
259  * 2.6.22 API change
260  * The function invalidate_bdev() lost it's second argument because
261  * it was unused.
262  */
263 #ifdef HAVE_1ARG_INVALIDATE_BDEV
264 #define vdev_bdev_invalidate(bdev)      invalidate_bdev(bdev)
265 #else
266 #define vdev_bdev_invalidate(bdev)      invalidate_bdev(bdev, 1)
267 #endif /* HAVE_1ARG_INVALIDATE_BDEV */
268
269 /*
270  * 2.6.27 API change
271  * The function was exported for use, prior to this it existed by the
272  * symbol was not exported.
273  */
274 #ifndef HAVE_LOOKUP_BDEV
275 #define lookup_bdev(path)               ERR_PTR(-ENOTSUP)
276 #endif
277
278 /*
279  * 2.6.30 API change
280  * To ensure good performance preferentially use the physical block size
281  * for proper alignment.  The physical size is supposed to be the internal
282  * sector size used by the device.  This is often 4096 byte for AF devices,
283  * while a smaller 512 byte logical size is supported for compatibility.
284  *
285  * Unfortunately, many drives still misreport their physical sector size.
286  * For devices which are known to lie you may need to manually set this
287  * at pool creation time with 'zpool create -o ashift=12 ...'.
288  *
289  * When the physical block size interface isn't available, we fall back to
290  * the logical block size interface and then the older hard sector size.
291  */
292 #ifdef HAVE_BDEV_PHYSICAL_BLOCK_SIZE
293 #define vdev_bdev_block_size(bdev)      bdev_physical_block_size(bdev)
294 #else
295 #ifdef HAVE_BDEV_LOGICAL_BLOCK_SIZE
296 #define vdev_bdev_block_size(bdev)      bdev_logical_block_size(bdev)
297 #else
298 #define vdev_bdev_block_size(bdev)      bdev_hardsect_size(bdev)
299 #endif /* HAVE_BDEV_LOGICAL_BLOCK_SIZE */
300 #endif /* HAVE_BDEV_PHYSICAL_BLOCK_SIZE */
301
302 /*
303  * 2.6.37 API change
304  * The WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags have been
305  * introduced as a replacement for WRITE_BARRIER.  This was done to
306  * allow richer semantics to be expressed to the block layer.  It is
307  * the block layers responsibility to choose the correct way to
308  * implement these semantics.
309  *
310  * The existence of these flags implies that REQ_FLUSH an REQ_FUA are
311  * defined.  Thus we can safely define VDEV_REQ_FLUSH and VDEV_REQ_FUA
312  * compatibility macros.
313  */
314 #ifdef WRITE_FLUSH_FUA
315 #define VDEV_WRITE_FLUSH_FUA            WRITE_FLUSH_FUA
316 #define VDEV_REQ_FLUSH                  REQ_FLUSH
317 #define VDEV_REQ_FUA                    REQ_FUA
318 #else
319 #define VDEV_WRITE_FLUSH_FUA            WRITE_BARRIER
320 #ifdef HAVE_BIO_RW_BARRIER
321 #define VDEV_REQ_FLUSH                  (1 << BIO_RW_BARRIER)
322 #define VDEV_REQ_FUA                    (1 << BIO_RW_BARRIER)
323 #else
324 #define VDEV_REQ_FLUSH                  REQ_HARDBARRIER
325 #define VDEV_REQ_FUA                    REQ_FUA
326 #endif
327 #endif
328
329 /*
330  * 2.6.32 API change
331  * Use the normal I/O patch for discards.
332  */
333 #ifdef QUEUE_FLAG_DISCARD
334 #ifdef HAVE_BIO_RW_DISCARD
335 #define VDEV_REQ_DISCARD                (1 << BIO_RW_DISCARD)
336 #else
337 #define VDEV_REQ_DISCARD                REQ_DISCARD
338 #endif
339 #else
340 #error  "Allowing the build will cause discard requests to become writes "
341         "potentially triggering the DMU_MAX_ACCESS assertion. Please file a "
342         "an issue report at: https://github.com/zfsonlinux/zfs/issues/new"
343 #endif
344
345 /*
346  * 2.6.33 API change
347  * Discard granularity and alignment restrictions may now be set.  For
348  * older kernels which do not support this it is safe to skip it.
349  */
350 #ifdef HAVE_DISCARD_GRANULARITY
351 static inline void
352 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
353 {
354         q->limits.discard_granularity = dg;
355 }
356 #else
357 #define blk_queue_discard_granularity(x, dg)    ((void)0)
358 #endif /* HAVE_DISCARD_GRANULARITY */
359
360 /*
361  * Default Linux IO Scheduler,
362  * Setting the scheduler to noop will allow the Linux IO scheduler to
363  * still perform front and back merging, while leaving the request
364  * ordering and prioritization to the ZFS IO scheduler.
365  */
366 #define VDEV_SCHEDULER                  "noop"
367
368 /*
369  * A common holder for vdev_bdev_open() is used to relax the exclusive open
370  * semantics slightly.  Internal vdev disk callers may pass VDEV_HOLDER to
371  * allow them to open the device multiple times.  Other kernel callers and
372  * user space processes which don't pass this value will get EBUSY.  This is
373  * currently required for the correct operation of hot spares.
374  */
375 #define VDEV_HOLDER                     ((void *)0x2401de7)
376
377 #endif /* _ZFS_BLKDEV_H */