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 */
39 #ifndef HAVE_BLK_FETCH_REQUEST
40 static inline struct request *
41 blk_fetch_request(struct request_queue *q)
45 req = elv_next_request(q);
47 blkdev_dequeue_request(req);
51 #endif /* HAVE_BLK_FETCH_REQUEST */
53 #ifndef HAVE_BLK_REQUEUE_REQUEST
55 blk_requeue_request(request_queue_t *q, struct request *req)
57 elv_requeue_request(q, req);
59 #endif /* HAVE_BLK_REQUEUE_REQUEST */
61 #ifndef HAVE_BLK_END_REQUEST
63 __blk_end_request(struct request *req, int error, unsigned int nr_bytes)
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.
72 list_add(&req->queuelist, &list);
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.
79 req->hard_cur_sectors = nr_bytes >> 9;
80 end_request(req, ((error == 0) ? 1 : error));
86 blk_end_request(struct request *req, int error, unsigned int nr_bytes)
88 struct request_queue *q = req->q;
91 spin_lock_irq(q->queue_lock);
92 rc = __blk_end_request(req, error, nr_bytes);
93 spin_unlock_irq(q->queue_lock);
98 #ifdef HAVE_BLK_END_REQUEST_GPL_ONLY
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.
104 #define __blk_end_request __blk_end_request_x
105 #define blk_end_request blk_end_request_x
108 __blk_end_request_x(struct request *req, int error, unsigned int nr_bytes)
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.
115 req->hard_cur_sectors = nr_bytes >> 9;
116 end_request(req, ((error == 0) ? 1 : error));
121 blk_end_request_x(struct request *req, int error, unsigned int nr_bytes)
123 struct request_queue *q = req->q;
126 spin_lock_irq(q->queue_lock);
127 rc = __blk_end_request_x(req, error, nr_bytes);
128 spin_unlock_irq(q->queue_lock);
132 #endif /* HAVE_BLK_END_REQUEST_GPL_ONLY */
133 #endif /* HAVE_BLK_END_REQUEST */
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.
143 #if defined(HAVE_BLK_QUEUE_FLUSH) && defined(HAVE_BLK_QUEUE_FLUSH_GPL_ONLY)
144 #define blk_queue_flush __blk_queue_flush
146 __blk_queue_flush(struct request_queue *q, unsigned int flags)
148 q->flush_flags = flags & (REQ_FLUSH | REQ_FUA);
150 #endif /* HAVE_BLK_QUEUE_FLUSH && HAVE_BLK_QUEUE_FLUSH_GPL_ONLY */
152 #ifndef HAVE_BLK_RQ_POS
153 static inline sector_t
154 blk_rq_pos(struct request *req)
156 return (req->sector);
158 #endif /* HAVE_BLK_RQ_POS */
160 #ifndef HAVE_BLK_RQ_SECTORS
161 static inline unsigned int
162 blk_rq_sectors(struct request *req)
164 return (req->nr_sectors);
166 #endif /* HAVE_BLK_RQ_SECTORS */
168 #if !defined(HAVE_BLK_RQ_BYTES) || defined(HAVE_BLK_RQ_BYTES_GPL_ONLY)
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.
174 #define blk_rq_bytes __blk_rq_bytes
175 static inline unsigned int
176 __blk_rq_bytes(struct request *req)
178 return (blk_rq_sectors(req) << 9);
180 #endif /* !HAVE_BLK_RQ_BYTES || HAVE_BLK_RQ_BYTES_GPL_ONLY */
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.
188 #ifndef blk_fs_request
189 #define blk_fs_request(rq) ((rq)->cmd_type == REQ_TYPE_FS)
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.
199 #ifndef blk_queue_stackable
200 #define blk_queue_stackable(q) ((q)->request_fn == NULL)
205 * The blk_queue_max_hw_sectors() function replaces blk_queue_max_sectors().
207 #ifndef HAVE_BLK_QUEUE_MAX_HW_SECTORS
208 #define blk_queue_max_hw_sectors __blk_queue_max_hw_sectors
210 __blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_sectors)
212 blk_queue_max_sectors(q, max_hw_sectors);
218 * The blk_queue_max_segments() function consolidates
219 * blk_queue_max_hw_segments() and blk_queue_max_phys_segments().
221 #ifndef HAVE_BLK_QUEUE_MAX_SEGMENTS
222 #define blk_queue_max_segments __blk_queue_max_segments
224 __blk_queue_max_segments(struct request_queue *q, unsigned short max_segments)
226 blk_queue_max_phys_segments(q, max_segments);
227 blk_queue_max_hw_segments(q, max_segments);
233 * The blk_queue_io_opt() function was added to indicate the optimal
234 * I/O size for the device. For older kernels this is a no-op.
236 #ifndef HAVE_BLK_QUEUE_IO_OPT
237 #define blk_queue_io_opt(q, x) ((void)(0))
240 #ifndef HAVE_GET_DISK_RO
242 get_disk_ro(struct gendisk *disk)
247 policy = disk->part[0]->policy;
251 #endif /* HAVE_GET_DISK_RO */
253 #ifndef HAVE_RQ_IS_SYNC
255 rq_is_sync(struct request *req)
257 return (req->flags & REQ_RW_SYNC);
259 #endif /* HAVE_RQ_IS_SYNC */
261 #ifndef HAVE_RQ_FOR_EACH_SEGMENT
262 struct req_iterator {
267 #define for_each_bio(_bio) \
268 for (; _bio; _bio = _bio->bi_next)
270 #define __rq_for_each_bio(_bio, rq) \
272 for (_bio = (rq)->bio; _bio; _bio = _bio->bi_next)
274 #define rq_for_each_segment(bvl, _rq, _iter) \
275 __rq_for_each_bio(_iter.bio, _rq) \
276 bio_for_each_segment(bvl, _iter.bio, _iter.i)
278 #define HAVE_RQ_FOR_EACH_SEGMENT_BVP 1
279 #endif /* HAVE_RQ_FOR_EACH_SEGMENT */
283 * rq_for_each_segment changed from taking bio_vec * to taking bio_vec.
284 * We provide rq_for_each_segment4 which takes both.
285 * You should not modify the fields in @bv and @bvp.
287 * Note: the if-else is just to inject the assignment before the loop body.
289 #ifdef HAVE_RQ_FOR_EACH_SEGMENT_BVP
290 #define rq_for_each_segment4(bv, bvp, rq, iter) \
291 rq_for_each_segment(bvp, rq, iter) \
292 if ((bv = *bvp), 0) \
296 #define rq_for_each_segment4(bv, bvp, rq, iter) \
297 rq_for_each_segment(bv, rq, iter) \
298 if ((bvp = &bv), 0) \
303 #ifdef HAVE_BIO_BVEC_ITER
304 #define BIO_BI_SECTOR(bio) (bio)->bi_iter.bi_sector
305 #define BIO_BI_SIZE(bio) (bio)->bi_iter.bi_size
306 #define BIO_BI_IDX(bio) (bio)->bi_iter.bi_idx
308 #define BIO_BI_SECTOR(bio) (bio)->bi_sector
309 #define BIO_BI_SIZE(bio) (bio)->bi_size
310 #define BIO_BI_IDX(bio) (bio)->bi_idx
314 * Portable helper for correctly setting the FAILFAST flags. The
315 * correct usage has changed 3 times from 2.6.12 to 2.6.38.
318 bio_set_flags_failfast(struct block_device *bdev, int *flags)
322 * Disable FAILFAST for loopback devices because of the
323 * following incorrect BUG_ON() in loop_make_request().
324 * This support is also disabled for md devices because the
325 * test suite layers md devices on top of loopback devices.
326 * This may be removed when the loopback driver is fixed.
328 * BUG_ON(!lo || (rw != READ && rw != WRITE));
330 if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
331 (MAJOR(bdev->bd_dev) == MD_MAJOR))
334 #ifdef BLOCK_EXT_MAJOR
335 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
337 #endif /* BLOCK_EXT_MAJOR */
338 #endif /* CONFIG_BUG */
340 #if defined(HAVE_BIO_RW_FAILFAST_DTD)
341 /* BIO_RW_FAILFAST_* preferred interface from 2.6.28 - 2.6.35 */
343 (1 << BIO_RW_FAILFAST_DEV) |
344 (1 << BIO_RW_FAILFAST_TRANSPORT) |
345 (1 << BIO_RW_FAILFAST_DRIVER));
346 #elif defined(HAVE_REQ_FAILFAST_MASK)
348 * REQ_FAILFAST_* preferred interface from 2.6.36 - 2.6.xx,
349 * the BIO_* and REQ_* flags were unified under REQ_* flags.
351 *flags |= REQ_FAILFAST_MASK;
353 #error "Undefined block IO FAILFAST interface."
358 * Maximum disk label length, it may be undefined for some kernels.
360 #ifndef DISK_NAME_LEN
361 #define DISK_NAME_LEN 32
362 #endif /* DISK_NAME_LEN */
366 * The bio_end_io() prototype changed slightly. These are helper
367 * macro's to ensure the prototype and return value are handled.
369 #ifdef HAVE_2ARGS_BIO_END_IO_T
370 #define BIO_END_IO_PROTO(fn, x, y, z) static void fn(struct bio *x, int z)
371 #define BIO_END_IO_RETURN(rc) return
373 #define BIO_END_IO_PROTO(fn, x, y, z) static int fn( \
377 #define BIO_END_IO_RETURN(rc) return rc
378 #endif /* HAVE_2ARGS_BIO_END_IO_T */
381 * 2.6.38 - 2.6.x API,
382 * blkdev_get_by_path()
385 * 2.6.28 - 2.6.37 API,
386 * open_bdev_exclusive()
387 * close_bdev_exclusive()
389 * 2.6.12 - 2.6.27 API,
393 * Used to exclusively open a block device from within the kernel.
395 #if defined(HAVE_BLKDEV_GET_BY_PATH)
396 #define vdev_bdev_open(path, md, hld) blkdev_get_by_path(path, \
397 (md) | FMODE_EXCL, hld)
398 #define vdev_bdev_close(bdev, md) blkdev_put(bdev, (md) | FMODE_EXCL)
399 #elif defined(HAVE_OPEN_BDEV_EXCLUSIVE)
400 #define vdev_bdev_open(path, md, hld) open_bdev_exclusive(path, md, hld)
401 #define vdev_bdev_close(bdev, md) close_bdev_exclusive(bdev, md)
403 #define vdev_bdev_open(path, md, hld) open_bdev_excl(path, md, hld)
404 #define vdev_bdev_close(bdev, md) close_bdev_excl(bdev)
405 #endif /* HAVE_BLKDEV_GET_BY_PATH | HAVE_OPEN_BDEV_EXCLUSIVE */
409 * The function invalidate_bdev() lost it's second argument because
412 #ifdef HAVE_1ARG_INVALIDATE_BDEV
413 #define vdev_bdev_invalidate(bdev) invalidate_bdev(bdev)
415 #define vdev_bdev_invalidate(bdev) invalidate_bdev(bdev, 1)
416 #endif /* HAVE_1ARG_INVALIDATE_BDEV */
420 * The function was exported for use, prior to this it existed by the
421 * symbol was not exported.
423 #ifndef HAVE_LOOKUP_BDEV
424 #define lookup_bdev(path) ERR_PTR(-ENOTSUP)
429 * To ensure good performance preferentially use the physical block size
430 * for proper alignment. The physical size is supposed to be the internal
431 * sector size used by the device. This is often 4096 byte for AF devices,
432 * while a smaller 512 byte logical size is supported for compatibility.
434 * Unfortunately, many drives still misreport their physical sector size.
435 * For devices which are known to lie you may need to manually set this
436 * at pool creation time with 'zpool create -o ashift=12 ...'.
438 * When the physical block size interface isn't available, we fall back to
439 * the logical block size interface and then the older hard sector size.
441 #ifdef HAVE_BDEV_PHYSICAL_BLOCK_SIZE
442 #define vdev_bdev_block_size(bdev) bdev_physical_block_size(bdev)
444 #ifdef HAVE_BDEV_LOGICAL_BLOCK_SIZE
445 #define vdev_bdev_block_size(bdev) bdev_logical_block_size(bdev)
447 #define vdev_bdev_block_size(bdev) bdev_hardsect_size(bdev)
448 #endif /* HAVE_BDEV_LOGICAL_BLOCK_SIZE */
449 #endif /* HAVE_BDEV_PHYSICAL_BLOCK_SIZE */
453 * The WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags have been
454 * introduced as a replacement for WRITE_BARRIER. This was done to
455 * allow richer semantics to be expressed to the block layer. It is
456 * the block layers responsibility to choose the correct way to
457 * implement these semantics.
459 * The existence of these flags implies that REQ_FLUSH an REQ_FUA are
460 * defined. Thus we can safely define VDEV_REQ_FLUSH and VDEV_REQ_FUA
461 * compatibility macros.
463 #ifdef WRITE_FLUSH_FUA
464 #define VDEV_WRITE_FLUSH_FUA WRITE_FLUSH_FUA
465 #define VDEV_REQ_FLUSH REQ_FLUSH
466 #define VDEV_REQ_FUA REQ_FUA
468 #define VDEV_WRITE_FLUSH_FUA WRITE_BARRIER
469 #define VDEV_REQ_FLUSH REQ_HARDBARRIER
470 #define VDEV_REQ_FUA REQ_HARDBARRIER
475 * Use the normal I/O patch for discards.
478 #define VDEV_REQ_DISCARD REQ_DISCARD
483 * Discard granularity and alignment restrictions may now be set. For
484 * older kernels which do not support this it is safe to skip it.
486 #ifdef HAVE_DISCARD_GRANULARITY
488 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
490 q->limits.discard_granularity = dg;
493 #define blk_queue_discard_granularity(x, dg) ((void)0)
494 #endif /* HAVE_DISCARD_GRANULARITY */
497 * Default Linux IO Scheduler,
498 * Setting the scheduler to noop will allow the Linux IO scheduler to
499 * still perform front and back merging, while leaving the request
500 * ordering and prioritization to the ZFS IO scheduler.
502 #define VDEV_SCHEDULER "noop"
505 * A common holder for vdev_bdev_open() is used to relax the exclusive open
506 * semantics slightly. Internal vdev disk callers may pass VDEV_HOLDER to
507 * allow them to open the device multiple times. Other kernel callers and
508 * user space processes which don't pass this value will get EBUSY. This is
509 * currently required for the correct operation of hot spares.
511 #define VDEV_HOLDER ((void *)0x2401de7)
513 #endif /* _ZFS_BLKDEV_H */