]> granicus.if.org Git - zfs/blob - include/linux/blkdev_compat.h
VDEV_REQ_FUA should be mapped to REQ_FUA
[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 #ifndef HAVE_BLK_FETCH_REQUEST
40 static inline struct request *
41 blk_fetch_request(struct request_queue *q)
42 {
43         struct request *req;
44
45         req = elv_next_request(q);
46         if (req)
47                 blkdev_dequeue_request(req);
48
49         return (req);
50 }
51 #endif /* HAVE_BLK_FETCH_REQUEST */
52
53 #ifndef HAVE_BLK_REQUEUE_REQUEST
54 static inline void
55 blk_requeue_request(request_queue_t *q, struct request *req)
56 {
57         elv_requeue_request(q, req);
58 }
59 #endif /* HAVE_BLK_REQUEUE_REQUEST */
60
61 #ifndef HAVE_BLK_END_REQUEST
62 static inline bool
63 __blk_end_request(struct request *req, int error, unsigned int nr_bytes)
64 {
65         LIST_HEAD(list);
66
67         /*
68          * Request has already been dequeued but 2.6.18 version of
69          * end_request() unconditionally dequeues the request so we
70          * add it to a local list to prevent hitting the BUG_ON.
71          */
72         list_add(&req->queuelist, &list);
73
74         /*
75          * The old API required the driver to end each segment and not
76          * the entire request.  In our case we always need to end the
77          * entire request partial requests are not supported.
78          */
79         req->hard_cur_sectors = nr_bytes >> 9;
80         end_request(req, ((error == 0) ? 1 : error));
81
82         return (0);
83 }
84
85 static inline bool
86 blk_end_request(struct request *req, int error, unsigned int nr_bytes)
87 {
88         struct request_queue *q = req->q;
89         bool rc;
90
91         spin_lock_irq(q->queue_lock);
92         rc = __blk_end_request(req, error, nr_bytes);
93         spin_unlock_irq(q->queue_lock);
94
95         return (rc);
96 }
97 #else
98 #ifdef HAVE_BLK_END_REQUEST_GPL_ONLY
99 /*
100  * Define required to avoid conflicting 2.6.29 non-static prototype for a
101  * GPL-only version of the helper.  As of 2.6.31 the helper is available
102  * to non-GPL modules and is not explicitly exported GPL-only.
103  */
104 #define __blk_end_request __blk_end_request_x
105 #define blk_end_request blk_end_request_x
106
107 static inline bool
108 __blk_end_request_x(struct request *req, int error, unsigned int nr_bytes)
109 {
110         /*
111          * The old API required the driver to end each segment and not
112          * the entire request.  In our case we always need to end the
113          * entire request partial requests are not supported.
114          */
115         req->hard_cur_sectors = nr_bytes >> 9;
116         end_request(req, ((error == 0) ? 1 : error));
117
118         return (0);
119 }
120 static inline bool
121 blk_end_request_x(struct request *req, int error, unsigned int nr_bytes)
122 {
123         struct request_queue *q = req->q;
124         bool rc;
125
126         spin_lock_irq(q->queue_lock);
127         rc = __blk_end_request_x(req, error, nr_bytes);
128         spin_unlock_irq(q->queue_lock);
129
130         return (rc);
131 }
132 #endif /* HAVE_BLK_END_REQUEST_GPL_ONLY */
133 #endif /* HAVE_BLK_END_REQUEST */
134
135 /*
136  * 2.6.36 API change,
137  * The blk_queue_flush() interface has replaced blk_queue_ordered()
138  * interface.  However, while the old interface was available to all the
139  * new one is GPL-only.   Thus if the GPL-only version is detected we
140  * implement our own trivial helper compatibility funcion.   The hope is
141  * that long term this function will be opened up.
142  */
143 #if defined(HAVE_BLK_QUEUE_FLUSH) && defined(HAVE_BLK_QUEUE_FLUSH_GPL_ONLY)
144 #define blk_queue_flush __blk_queue_flush
145 static inline void
146 __blk_queue_flush(struct request_queue *q, unsigned int flags)
147 {
148         q->flush_flags = flags & (REQ_FLUSH | REQ_FUA);
149 }
150 #endif /* HAVE_BLK_QUEUE_FLUSH && HAVE_BLK_QUEUE_FLUSH_GPL_ONLY */
151
152 #ifndef HAVE_BLK_RQ_POS
153 static inline sector_t
154 blk_rq_pos(struct request *req)
155 {
156         return (req->sector);
157 }
158 #endif /* HAVE_BLK_RQ_POS */
159
160 #ifndef HAVE_BLK_RQ_SECTORS
161 static inline unsigned int
162 blk_rq_sectors(struct request *req)
163 {
164         return (req->nr_sectors);
165 }
166 #endif /* HAVE_BLK_RQ_SECTORS */
167
168 #if !defined(HAVE_BLK_RQ_BYTES) || defined(HAVE_BLK_RQ_BYTES_GPL_ONLY)
169 /*
170  * Define required to avoid conflicting 2.6.29 non-static prototype for a
171  * GPL-only version of the helper.  As of 2.6.31 the helper is available
172  * to non-GPL modules in the form of a static inline in the header.
173  */
174 #define blk_rq_bytes __blk_rq_bytes
175 static inline unsigned int
176 __blk_rq_bytes(struct request *req)
177 {
178         return (blk_rq_sectors(req) << 9);
179 }
180 #endif /* !HAVE_BLK_RQ_BYTES || HAVE_BLK_RQ_BYTES_GPL_ONLY */
181
182 /*
183  * Most of the blk_* macros were removed in 2.6.36.  Ostensibly this was
184  * done to improve readability and allow easier grepping.  However, from
185  * a portability stand point the macros are helpful.  Therefore the needed
186  * macros are redefined here if they are missing from the kernel.
187  */
188 #ifndef blk_fs_request
189 #define blk_fs_request(rq)      ((rq)->cmd_type == REQ_TYPE_FS)
190 #endif
191
192 /*
193  * 2.6.27 API change,
194  * The blk_queue_stackable() queue flag was added in 2.6.27 to handle dm
195  * stacking drivers.  Prior to this request stacking drivers were detected
196  * by checking (q->request_fn == NULL), for earlier kernels we revert to
197  * this legacy behavior.
198  */
199 #ifndef blk_queue_stackable
200 #define blk_queue_stackable(q)  ((q)->request_fn == NULL)
201 #endif
202
203 /*
204  * 2.6.34 API change,
205  * The blk_queue_max_hw_sectors() function replaces blk_queue_max_sectors().
206  */
207 #ifndef HAVE_BLK_QUEUE_MAX_HW_SECTORS
208 #define blk_queue_max_hw_sectors __blk_queue_max_hw_sectors
209 static inline void
210 __blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
211 {
212         blk_queue_max_sectors(q, max_hw_sectors);
213 }
214 #endif
215
216 /*
217  * 2.6.34 API change,
218  * The blk_queue_max_segments() function consolidates
219  * blk_queue_max_hw_segments() and blk_queue_max_phys_segments().
220  */
221 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
222 #define blk_queue_max_segments __blk_queue_max_segments
223 static inline void
224 __blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
225 {
226         blk_queue_max_phys_segments(q, max_segments);
227         blk_queue_max_hw_segments(q, max_segments);
228 }
229 #endif
230
231 #ifndef HAVE_GET_DISK_RO
232 static inline int
233 get_disk_ro(struct gendisk *disk)
234 {
235         int policy = 0;
236
237         if (disk->part[0])
238                 policy = disk->part[0]->policy;
239
240         return (policy);
241 }
242 #endif /* HAVE_GET_DISK_RO */
243
244 #ifndef HAVE_RQ_IS_SYNC
245 static inline bool
246 rq_is_sync(struct request *req)
247 {
248         return (req->flags & REQ_RW_SYNC);
249 }
250 #endif /* HAVE_RQ_IS_SYNC */
251
252 #ifndef HAVE_RQ_FOR_EACH_SEGMENT
253 struct req_iterator {
254         int i;
255         struct bio *bio;
256 };
257
258 #define for_each_bio(_bio)              \
259         for (; _bio; _bio = _bio->bi_next)
260
261 #define __rq_for_each_bio(_bio, rq)     \
262         if ((rq->bio))                  \
263                 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
264
265 #define rq_for_each_segment(bvl, _rq, _iter)                    \
266         __rq_for_each_bio(_iter.bio, _rq)                       \
267                 bio_for_each_segment(bvl, _iter.bio, _iter.i)
268
269 #define HAVE_RQ_FOR_EACH_SEGMENT_BVP 1
270 #endif /* HAVE_RQ_FOR_EACH_SEGMENT */
271
272 /*
273  * 3.14 API change
274  * rq_for_each_segment changed from taking bio_vec * to taking bio_vec.
275  * We provide rq_for_each_segment4 which takes both.
276  * You should not modify the fields in @bv and @bvp.
277  *
278  * Note: the if-else is just to inject the assignment before the loop body.
279  */
280 #ifdef HAVE_RQ_FOR_EACH_SEGMENT_BVP
281 #define rq_for_each_segment4(bv, bvp, rq, iter) \
282         rq_for_each_segment(bvp, rq, iter)      \
283                 if ((bv = *bvp), 0)             \
284                         ;                       \
285                 else
286 #else
287 #define rq_for_each_segment4(bv, bvp, rq, iter) \
288         rq_for_each_segment(bv, rq, iter)       \
289                 if ((bvp = &bv), 0)             \
290                         ;                       \
291                 else
292 #endif
293
294 #ifdef HAVE_BIO_BVEC_ITER
295 #define BIO_BI_SECTOR(bio)      (bio)->bi_iter.bi_sector
296 #define BIO_BI_SIZE(bio)        (bio)->bi_iter.bi_size
297 #define BIO_BI_IDX(bio)         (bio)->bi_iter.bi_idx
298 #else
299 #define BIO_BI_SECTOR(bio)      (bio)->bi_sector
300 #define BIO_BI_SIZE(bio)        (bio)->bi_size
301 #define BIO_BI_IDX(bio)         (bio)->bi_idx
302 #endif
303
304 /*
305  * Portable helper for correctly setting the FAILFAST flags.  The
306  * correct usage has changed 3 times from 2.6.12 to 2.6.38.
307  */
308 static inline void
309 bio_set_flags_failfast(struct block_device *bdev, int *flags)
310 {
311 #ifdef CONFIG_BUG
312         /*
313          * Disable FAILFAST for loopback devices because of the
314          * following incorrect BUG_ON() in loop_make_request().
315          * This support is also disabled for md devices because the
316          * test suite layers md devices on top of loopback devices.
317          * This may be removed when the loopback driver is fixed.
318          *
319          *   BUG_ON(!lo || (rw != READ && rw != WRITE));
320          */
321         if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
322             (MAJOR(bdev->bd_dev) == MD_MAJOR))
323                 return;
324
325 #ifdef BLOCK_EXT_MAJOR
326         if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
327                 return;
328 #endif /* BLOCK_EXT_MAJOR */
329 #endif /* CONFIG_BUG */
330
331 #if defined(HAVE_BIO_RW_FAILFAST_DTD)
332         /* BIO_RW_FAILFAST_* preferred interface from 2.6.28 - 2.6.35 */
333         *flags |= (
334             (1 << BIO_RW_FAILFAST_DEV) |
335             (1 << BIO_RW_FAILFAST_TRANSPORT) |
336             (1 << BIO_RW_FAILFAST_DRIVER));
337 #elif defined(HAVE_REQ_FAILFAST_MASK)
338         /*
339          * REQ_FAILFAST_* preferred interface from 2.6.36 - 2.6.xx,
340          * the BIO_* and REQ_* flags were unified under REQ_* flags.
341          */
342         *flags |= REQ_FAILFAST_MASK;
343 #else
344 #error "Undefined block IO FAILFAST interface."
345 #endif
346 }
347
348 /*
349  * Maximum disk label length, it may be undefined for some kernels.
350  */
351 #ifndef DISK_NAME_LEN
352 #define DISK_NAME_LEN   32
353 #endif /* DISK_NAME_LEN */
354
355 /*
356  * 2.6.24 API change,
357  * The bio_end_io() prototype changed slightly.  These are helper
358  * macro's to ensure the prototype and return value are handled.
359  */
360 #ifdef HAVE_2ARGS_BIO_END_IO_T
361 #define BIO_END_IO_PROTO(fn, x, y, z)   static void fn(struct bio *x, int z)
362 #define BIO_END_IO_RETURN(rc)           return
363 #else
364 #define BIO_END_IO_PROTO(fn, x, y, z)   static int fn( \
365                                             struct bio *x, \
366                                             unsigned int y, \
367                                             int z)
368 #define BIO_END_IO_RETURN(rc)           return rc
369 #endif /* HAVE_2ARGS_BIO_END_IO_T */
370
371 /*
372  * 2.6.38 - 2.6.x API,
373  *   blkdev_get_by_path()
374  *   blkdev_put()
375  *
376  * 2.6.28 - 2.6.37 API,
377  *   open_bdev_exclusive()
378  *   close_bdev_exclusive()
379  *
380  * 2.6.12 - 2.6.27 API,
381  *   open_bdev_excl()
382  *   close_bdev_excl()
383  *
384  * Used to exclusively open a block device from within the kernel.
385  */
386 #if defined(HAVE_BLKDEV_GET_BY_PATH)
387 #define vdev_bdev_open(path, md, hld)   blkdev_get_by_path(path, \
388                                             (md) | FMODE_EXCL, hld)
389 #define vdev_bdev_close(bdev, md)       blkdev_put(bdev, (md) | FMODE_EXCL)
390 #elif defined(HAVE_OPEN_BDEV_EXCLUSIVE)
391 #define vdev_bdev_open(path, md, hld)   open_bdev_exclusive(path, md, hld)
392 #define vdev_bdev_close(bdev, md)       close_bdev_exclusive(bdev, md)
393 #else
394 #define vdev_bdev_open(path, md, hld)   open_bdev_excl(path, md, hld)
395 #define vdev_bdev_close(bdev, md)       close_bdev_excl(bdev)
396 #endif /* HAVE_BLKDEV_GET_BY_PATH | HAVE_OPEN_BDEV_EXCLUSIVE */
397
398 /*
399  * 2.6.22 API change
400  * The function invalidate_bdev() lost it's second argument because
401  * it was unused.
402  */
403 #ifdef HAVE_1ARG_INVALIDATE_BDEV
404 #define vdev_bdev_invalidate(bdev)      invalidate_bdev(bdev)
405 #else
406 #define vdev_bdev_invalidate(bdev)      invalidate_bdev(bdev, 1)
407 #endif /* HAVE_1ARG_INVALIDATE_BDEV */
408
409 /*
410  * 2.6.27 API change
411  * The function was exported for use, prior to this it existed by the
412  * symbol was not exported.
413  */
414 #ifndef HAVE_LOOKUP_BDEV
415 #define lookup_bdev(path)               ERR_PTR(-ENOTSUP)
416 #endif
417
418 /*
419  * 2.6.30 API change
420  * To ensure good performance preferentially use the physical block size
421  * for proper alignment.  The physical size is supposed to be the internal
422  * sector size used by the device.  This is often 4096 byte for AF devices,
423  * while a smaller 512 byte logical size is supported for compatibility.
424  *
425  * Unfortunately, many drives still misreport their physical sector size.
426  * For devices which are known to lie you may need to manually set this
427  * at pool creation time with 'zpool create -o ashift=12 ...'.
428  *
429  * When the physical block size interface isn't available, we fall back to
430  * the logical block size interface and then the older hard sector size.
431  */
432 #ifdef HAVE_BDEV_PHYSICAL_BLOCK_SIZE
433 #define vdev_bdev_block_size(bdev)      bdev_physical_block_size(bdev)
434 #else
435 #ifdef HAVE_BDEV_LOGICAL_BLOCK_SIZE
436 #define vdev_bdev_block_size(bdev)      bdev_logical_block_size(bdev)
437 #else
438 #define vdev_bdev_block_size(bdev)      bdev_hardsect_size(bdev)
439 #endif /* HAVE_BDEV_LOGICAL_BLOCK_SIZE */
440 #endif /* HAVE_BDEV_PHYSICAL_BLOCK_SIZE */
441
442 /*
443  * 2.6.37 API change
444  * The WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags have been
445  * introduced as a replacement for WRITE_BARRIER.  This was done to
446  * allow richer semantics to be expressed to the block layer.  It is
447  * the block layers responsibility to choose the correct way to
448  * implement these semantics.
449  *
450  * The existence of these flags implies that REQ_FLUSH an REQ_FUA are
451  * defined.  Thus we can safely define VDEV_REQ_FLUSH and VDEV_REQ_FUA
452  * compatibility macros.
453  */
454 #ifdef WRITE_FLUSH_FUA
455 #define VDEV_WRITE_FLUSH_FUA            WRITE_FLUSH_FUA
456 #define VDEV_REQ_FLUSH                  REQ_FLUSH
457 #define VDEV_REQ_FUA                    REQ_FUA
458 #else
459 #define VDEV_WRITE_FLUSH_FUA            WRITE_BARRIER
460 #define VDEV_REQ_FLUSH                  REQ_HARDBARRIER
461 #define VDEV_REQ_FUA                    REQ_FUA
462 #endif
463
464 /*
465  * 2.6.32 API change
466  * Use the normal I/O patch for discards.
467  */
468 #ifdef REQ_DISCARD
469 #define VDEV_REQ_DISCARD                REQ_DISCARD
470 #endif
471
472 /*
473  * 2.6.33 API change
474  * Discard granularity and alignment restrictions may now be set.  For
475  * older kernels which do not support this it is safe to skip it.
476  */
477 #ifdef HAVE_DISCARD_GRANULARITY
478 static inline void
479 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
480 {
481         q->limits.discard_granularity = dg;
482 }
483 #else
484 #define blk_queue_discard_granularity(x, dg)    ((void)0)
485 #endif /* HAVE_DISCARD_GRANULARITY */
486
487 /*
488  * Default Linux IO Scheduler,
489  * Setting the scheduler to noop will allow the Linux IO scheduler to
490  * still perform front and back merging, while leaving the request
491  * ordering and prioritization to the ZFS IO scheduler.
492  */
493 #define VDEV_SCHEDULER                  "noop"
494
495 /*
496  * A common holder for vdev_bdev_open() is used to relax the exclusive open
497  * semantics slightly.  Internal vdev disk callers may pass VDEV_HOLDER to
498  * allow them to open the device multiple times.  Other kernel callers and
499  * user space processes which don't pass this value will get EBUSY.  This is
500  * currently required for the correct operation of hot spares.
501  */
502 #define VDEV_HOLDER                     ((void *)0x2401de7)
503
504 #endif /* _ZFS_BLKDEV_H */