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>
36 typedef unsigned __bitwise__ fmode_t;
37 #endif /* HAVE_FMODE_T */
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.
47 #if defined(HAVE_BLK_QUEUE_FLUSH) && defined(HAVE_BLK_QUEUE_FLUSH_GPL_ONLY)
48 #define blk_queue_flush __blk_queue_flush
50 __blk_queue_flush(struct request_queue *q, unsigned int flags)
52 q->flush_flags = flags & (REQ_FLUSH | REQ_FUA);
54 #endif /* HAVE_BLK_QUEUE_FLUSH && HAVE_BLK_QUEUE_FLUSH_GPL_ONLY */
56 #ifndef HAVE_BLK_RQ_POS
57 static inline sector_t
58 blk_rq_pos(struct request *req)
62 #endif /* HAVE_BLK_RQ_POS */
64 #ifndef HAVE_BLK_RQ_SECTORS
65 static inline unsigned int
66 blk_rq_sectors(struct request *req)
68 return (req->nr_sectors);
70 #endif /* HAVE_BLK_RQ_SECTORS */
72 #if !defined(HAVE_BLK_RQ_BYTES) || defined(HAVE_BLK_RQ_BYTES_GPL_ONLY)
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.
78 #define blk_rq_bytes __blk_rq_bytes
79 static inline unsigned int
80 __blk_rq_bytes(struct request *req)
82 return (blk_rq_sectors(req) << 9);
84 #endif /* !HAVE_BLK_RQ_BYTES || HAVE_BLK_RQ_BYTES_GPL_ONLY */
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.
92 #ifndef blk_fs_request
93 #define blk_fs_request(rq) ((rq)->cmd_type == REQ_TYPE_FS)
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.
103 #ifndef blk_queue_stackable
104 #define blk_queue_stackable(q) ((q)->request_fn == NULL)
109 * The blk_queue_max_hw_sectors() function replaces blk_queue_max_sectors().
111 #ifndef HAVE_BLK_QUEUE_MAX_HW_SECTORS
112 #define blk_queue_max_hw_sectors __blk_queue_max_hw_sectors
114 __blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
116 blk_queue_max_sectors(q, max_hw_sectors);
122 * The blk_queue_max_segments() function consolidates
123 * blk_queue_max_hw_segments() and blk_queue_max_phys_segments().
125 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
126 #define blk_queue_max_segments __blk_queue_max_segments
128 __blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
130 blk_queue_max_phys_segments(q, max_segments);
131 blk_queue_max_hw_segments(q, max_segments);
135 #ifndef HAVE_GET_DISK_RO
137 get_disk_ro(struct gendisk *disk)
142 policy = disk->part[0]->policy;
146 #endif /* HAVE_GET_DISK_RO */
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;
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;
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.
169 bio_set_flags_failfast(struct block_device *bdev, int *flags)
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.
179 * BUG_ON(!lo || (rw != READ && rw != WRITE));
181 if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
182 (MAJOR(bdev->bd_dev) == MD_MAJOR))
185 #ifdef BLOCK_EXT_MAJOR
186 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
188 #endif /* BLOCK_EXT_MAJOR */
189 #endif /* CONFIG_BUG */
191 #if defined(HAVE_BIO_RW_FAILFAST_DTD)
192 /* BIO_RW_FAILFAST_* preferred interface from 2.6.28 - 2.6.35 */
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)
199 * REQ_FAILFAST_* preferred interface from 2.6.36 - 2.6.xx,
200 * the BIO_* and REQ_* flags were unified under REQ_* flags.
202 *flags |= REQ_FAILFAST_MASK;
204 #error "Undefined block IO FAILFAST interface."
209 * Maximum disk label length, it may be undefined for some kernels.
211 #ifndef DISK_NAME_LEN
212 #define DISK_NAME_LEN 32
213 #endif /* DISK_NAME_LEN */
217 * The bio_end_io() prototype changed slightly. These are helper
218 * macro's to ensure the prototype and return value are handled.
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
224 #define BIO_END_IO_PROTO(fn, x, y, z) static int fn( \
228 #define BIO_END_IO_RETURN(rc) return rc
229 #endif /* HAVE_2ARGS_BIO_END_IO_T */
232 * 2.6.38 - 2.6.x API,
233 * blkdev_get_by_path()
236 * 2.6.28 - 2.6.37 API,
237 * open_bdev_exclusive()
238 * close_bdev_exclusive()
240 * 2.6.12 - 2.6.27 API,
244 * Used to exclusively open a block device from within the kernel.
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)
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 */
260 * The function invalidate_bdev() lost it's second argument because
263 #ifdef HAVE_1ARG_INVALIDATE_BDEV
264 #define vdev_bdev_invalidate(bdev) invalidate_bdev(bdev)
266 #define vdev_bdev_invalidate(bdev) invalidate_bdev(bdev, 1)
267 #endif /* HAVE_1ARG_INVALIDATE_BDEV */
271 * The function was exported for use, prior to this it existed by the
272 * symbol was not exported.
274 #ifndef HAVE_LOOKUP_BDEV
275 #define lookup_bdev(path) ERR_PTR(-ENOTSUP)
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.
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 ...'.
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.
292 #ifdef HAVE_BDEV_PHYSICAL_BLOCK_SIZE
293 #define vdev_bdev_block_size(bdev) bdev_physical_block_size(bdev)
295 #ifdef HAVE_BDEV_LOGICAL_BLOCK_SIZE
296 #define vdev_bdev_block_size(bdev) bdev_logical_block_size(bdev)
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 */
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.
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.
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
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)
324 #define VDEV_REQ_FLUSH REQ_HARDBARRIER
325 #define VDEV_REQ_FUA REQ_FUA
331 * Use the normal I/O patch for discards.
333 #ifdef QUEUE_FLAG_DISCARD
334 #ifdef HAVE_BIO_RW_DISCARD
335 #define VDEV_REQ_DISCARD (1 << BIO_RW_DISCARD)
337 #define VDEV_REQ_DISCARD REQ_DISCARD
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"
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.
350 #ifdef HAVE_DISCARD_GRANULARITY
352 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
354 q->limits.discard_granularity = dg;
357 #define blk_queue_discard_granularity(x, dg) ((void)0)
358 #endif /* HAVE_DISCARD_GRANULARITY */
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.
366 #define VDEV_SCHEDULER "noop"
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.
375 #define VDEV_HOLDER ((void *)0x2401de7)
377 #endif /* _ZFS_BLKDEV_H */