]> git.proxmox.com Git - mirror_zfs-debian.git/blob - include/linux/blkdev_compat.h
Imported Upstream version 0.6.5.9
[mirror_zfs-debian.git] / 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 * 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,
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
49 * implement our own trivial helper.
50 *
51 * 2.6.x - 2.6.35
52 * Legacy blk_queue_ordered() interface.
53 */
54 static inline void
55 blk_queue_set_write_cache(struct request_queue *q, bool wc, bool fua)
56 {
57 #if defined(HAVE_BLK_QUEUE_WRITE_CACHE_GPL_ONLY)
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);
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);
79 #endif
80 }
81
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
89 #define blk_fs_request(rq) ((rq)->cmd_type == REQ_TYPE_FS)
90 #endif
91
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
100 #define blk_queue_stackable(q) ((q)->request_fn == NULL)
101 #endif
102
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
108 #define blk_queue_max_hw_sectors __blk_queue_max_hw_sectors
109 static 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
122 #define blk_queue_max_segments __blk_queue_max_segments
123 static 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
131 #ifndef HAVE_GET_DISK_RO
132 static inline int
133 get_disk_ro(struct gendisk *disk)
134 {
135 int policy = 0;
136
137 if (disk->part[0])
138 policy = disk->part[0]->policy;
139
140 return (policy);
141 }
142 #endif /* HAVE_GET_DISK_RO */
143
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
148 #define bio_for_each_segment4(bv, bvp, b, i) \
149 bio_for_each_segment((bv), (b), (i))
150 typedef struct bvec_iter bvec_iterator_t;
151 #else
152 #define BIO_BI_SECTOR(bio) (bio)->bi_sector
153 #define BIO_BI_SIZE(bio) (bio)->bi_size
154 #define BIO_BI_IDX(bio) (bio)->bi_idx
155 #define bio_for_each_segment4(bv, bvp, b, i) \
156 bio_for_each_segment((bvp), (b), (i))
157 typedef int bvec_iterator_t;
158 #endif
159
160 /*
161 * Portable helper for correctly setting the FAILFAST flags. The
162 * correct usage has changed 3 times from 2.6.12 to 2.6.38.
163 */
164 static inline void
165 bio_set_flags_failfast(struct block_device *bdev, int *flags)
166 {
167 #ifdef CONFIG_BUG
168 /*
169 * Disable FAILFAST for loopback devices because of the
170 * following incorrect BUG_ON() in loop_make_request().
171 * This support is also disabled for md devices because the
172 * test suite layers md devices on top of loopback devices.
173 * This may be removed when the loopback driver is fixed.
174 *
175 * BUG_ON(!lo || (rw != READ && rw != WRITE));
176 */
177 if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
178 (MAJOR(bdev->bd_dev) == MD_MAJOR))
179 return;
180
181 #ifdef BLOCK_EXT_MAJOR
182 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
183 return;
184 #endif /* BLOCK_EXT_MAJOR */
185 #endif /* CONFIG_BUG */
186
187 #if defined(HAVE_BIO_RW_FAILFAST_DTD)
188 /* BIO_RW_FAILFAST_* preferred interface from 2.6.28 - 2.6.35 */
189 *flags |= (
190 (1 << BIO_RW_FAILFAST_DEV) |
191 (1 << BIO_RW_FAILFAST_TRANSPORT) |
192 (1 << BIO_RW_FAILFAST_DRIVER));
193 #elif defined(HAVE_REQ_FAILFAST_MASK)
194 /*
195 * REQ_FAILFAST_* preferred interface from 2.6.36 - 2.6.xx,
196 * the BIO_* and REQ_* flags were unified under REQ_* flags.
197 */
198 *flags |= REQ_FAILFAST_MASK;
199 #else
200 #error "Undefined block IO FAILFAST interface."
201 #endif
202 }
203
204 /*
205 * Maximum disk label length, it may be undefined for some kernels.
206 */
207 #ifndef DISK_NAME_LEN
208 #define DISK_NAME_LEN 32
209 #endif /* DISK_NAME_LEN */
210
211 /*
212 * 4.3 API change
213 * The bio_endio() prototype changed slightly. These are helper
214 * macro's to ensure the prototype and invocation are handled.
215 */
216 #ifdef HAVE_1ARG_BIO_END_IO_T
217 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x)
218 #define BIO_END_IO(bio, error) bio->bi_error = error; bio_endio(bio);
219 #else
220 #define BIO_END_IO_PROTO(fn, x, z) static void fn(struct bio *x, int z)
221 #define BIO_END_IO(bio, error) bio_endio(bio, error);
222 #endif /* HAVE_1ARG_BIO_END_IO_T */
223
224 /*
225 * 2.6.38 - 2.6.x API,
226 * blkdev_get_by_path()
227 * blkdev_put()
228 *
229 * 2.6.28 - 2.6.37 API,
230 * open_bdev_exclusive()
231 * close_bdev_exclusive()
232 *
233 * 2.6.12 - 2.6.27 API,
234 * open_bdev_excl()
235 * close_bdev_excl()
236 *
237 * Used to exclusively open a block device from within the kernel.
238 */
239 #if defined(HAVE_BLKDEV_GET_BY_PATH)
240 #define vdev_bdev_open(path, md, hld) blkdev_get_by_path(path, \
241 (md) | FMODE_EXCL, hld)
242 #define vdev_bdev_close(bdev, md) blkdev_put(bdev, (md) | FMODE_EXCL)
243 #elif defined(HAVE_OPEN_BDEV_EXCLUSIVE)
244 #define vdev_bdev_open(path, md, hld) open_bdev_exclusive(path, md, hld)
245 #define vdev_bdev_close(bdev, md) close_bdev_exclusive(bdev, md)
246 #else
247 #define vdev_bdev_open(path, md, hld) open_bdev_excl(path, md, hld)
248 #define vdev_bdev_close(bdev, md) close_bdev_excl(bdev)
249 #endif /* HAVE_BLKDEV_GET_BY_PATH | HAVE_OPEN_BDEV_EXCLUSIVE */
250
251 /*
252 * 2.6.22 API change
253 * The function invalidate_bdev() lost it's second argument because
254 * it was unused.
255 */
256 #ifdef HAVE_1ARG_INVALIDATE_BDEV
257 #define vdev_bdev_invalidate(bdev) invalidate_bdev(bdev)
258 #else
259 #define vdev_bdev_invalidate(bdev) invalidate_bdev(bdev, 1)
260 #endif /* HAVE_1ARG_INVALIDATE_BDEV */
261
262 /*
263 * 2.6.27 API change
264 * The function was exported for use, prior to this it existed but the
265 * symbol was not exported.
266 *
267 * 4.4.0-6.21 API change for Ubuntu
268 * lookup_bdev() gained a second argument, FMODE_*, to check inode permissions.
269 */
270 #ifdef HAVE_1ARG_LOOKUP_BDEV
271 #define vdev_lookup_bdev(path) lookup_bdev(path)
272 #else
273 #ifdef HAVE_2ARGS_LOOKUP_BDEV
274 #define vdev_lookup_bdev(path) lookup_bdev(path, 0)
275 #else
276 #define vdev_lookup_bdev(path) ERR_PTR(-ENOTSUP)
277 #endif /* HAVE_2ARGS_LOOKUP_BDEV */
278 #endif /* HAVE_1ARG_LOOKUP_BDEV */
279
280 /*
281 * 2.6.30 API change
282 * To ensure good performance preferentially use the physical block size
283 * for proper alignment. The physical size is supposed to be the internal
284 * sector size used by the device. This is often 4096 byte for AF devices,
285 * while a smaller 512 byte logical size is supported for compatibility.
286 *
287 * Unfortunately, many drives still misreport their physical sector size.
288 * For devices which are known to lie you may need to manually set this
289 * at pool creation time with 'zpool create -o ashift=12 ...'.
290 *
291 * When the physical block size interface isn't available, we fall back to
292 * the logical block size interface and then the older hard sector size.
293 */
294 #ifdef HAVE_BDEV_PHYSICAL_BLOCK_SIZE
295 #define vdev_bdev_block_size(bdev) bdev_physical_block_size(bdev)
296 #else
297 #ifdef HAVE_BDEV_LOGICAL_BLOCK_SIZE
298 #define vdev_bdev_block_size(bdev) bdev_logical_block_size(bdev)
299 #else
300 #define vdev_bdev_block_size(bdev) bdev_hardsect_size(bdev)
301 #endif /* HAVE_BDEV_LOGICAL_BLOCK_SIZE */
302 #endif /* HAVE_BDEV_PHYSICAL_BLOCK_SIZE */
303
304 #ifndef HAVE_BIO_SET_OP_ATTRS
305 /*
306 * Kernels without bio_set_op_attrs use bi_rw for the bio flags.
307 */
308 static inline void
309 bio_set_op_attrs(struct bio *bio, unsigned rw, unsigned flags)
310 {
311 bio->bi_rw |= rw | flags;
312 }
313 #endif
314
315 /*
316 * bio_set_flush - Set the appropriate flags in a bio to guarantee
317 * data are on non-volatile media on completion.
318 *
319 * 2.6.X - 2.6.36 API,
320 * WRITE_BARRIER - Tells the block layer to commit all previously submitted
321 * writes to stable storage before this one is started and that the current
322 * write is on stable storage upon completion. Also prevents reordering
323 * on both sides of the current operation.
324 *
325 * 2.6.37 - 4.8 API,
326 * Introduce WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags as a
327 * replacement for WRITE_BARRIER to allow expressing richer semantics
328 * to the block layer. It's up to the block layer to implement the
329 * semantics correctly. Use the WRITE_FLUSH_FUA flag combination.
330 *
331 * 4.8 - 4.9 API,
332 * REQ_FLUSH was renamed to REQ_PREFLUSH. For consistency with previous
333 * ZoL releases, prefer the WRITE_FLUSH_FUA flag set if it's available.
334 *
335 * 4.10 API,
336 * The read/write flags and their modifiers, including WRITE_FLUSH,
337 * WRITE_FUA and WRITE_FLUSH_FUA were removed from fs.h in
338 * torvalds/linux@70fd7614 and replaced by direct flag modification
339 * of the REQ_ flags in bio->bi_opf. Use REQ_PREFLUSH.
340 */
341 static inline void
342 bio_set_flush(struct bio *bio)
343 {
344 #if defined(WRITE_BARRIER) /* < 2.6.37 */
345 bio_set_op_attrs(bio, 0, WRITE_BARRIER);
346 #elif defined(WRITE_FLUSH_FUA) /* >= 2.6.37 and <= 4.9 */
347 bio_set_op_attrs(bio, 0, WRITE_FLUSH_FUA);
348 #elif defined(REQ_PREFLUSH) /* >= 4.10 */
349 bio_set_op_attrs(bio, 0, REQ_PREFLUSH);
350 #else
351 #error "Allowing the build will cause bio_set_flush requests to be ignored."
352 "Please file an issue report at: "
353 "https://github.com/zfsonlinux/zfs/issues/new"
354 #endif
355 }
356
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.
375 *
376 * Note that 2.6.32 era kernels provide both BIO_RW_BARRIER and REQ_FLUSH,
377 * where BIO_RW_BARRIER is the correct interface. Therefore, it is important
378 * that the HAVE_BIO_RW_BARRIER check occur before the REQ_FLUSH check.
379 */
380 static inline boolean_t
381 bio_is_flush(struct bio *bio)
382 {
383 #if defined(HAVE_REQ_OP_FLUSH) && defined(HAVE_BIO_BI_OPF)
384 return ((bio_op(bio) == REQ_OP_FLUSH) || (bio->bi_opf & REQ_PREFLUSH));
385 #elif defined(REQ_PREFLUSH) && defined(HAVE_BIO_BI_OPF)
386 return (bio->bi_opf & REQ_PREFLUSH);
387 #elif defined(REQ_PREFLUSH) && !defined(HAVE_BIO_BI_OPF)
388 return (bio->bi_rw & REQ_PREFLUSH);
389 #elif defined(HAVE_BIO_RW_BARRIER)
390 return (bio->bi_rw & (1 << BIO_RW_BARRIER));
391 #elif defined(REQ_FLUSH)
392 return (bio->bi_rw & REQ_FLUSH);
393 #else
394 #error "Allowing the build will cause flush requests to be ignored. Please "
395 "file an issue report at: https://github.com/zfsonlinux/zfs/issues/new"
396 #endif
397 }
398
399 /*
400 * 4.8 - 4.x API,
401 * REQ_FUA flag moved to bio->bi_opf
402 *
403 * 2.6.x - 4.7 API,
404 * REQ_FUA
405 */
406 static inline boolean_t
407 bio_is_fua(struct bio *bio)
408 {
409 #if defined(HAVE_BIO_BI_OPF)
410 return (bio->bi_opf & REQ_FUA);
411 #elif defined(REQ_FUA)
412 return (bio->bi_rw & REQ_FUA);
413 #else
414 #error "Allowing the build will cause fua requests to be ignored. Please "
415 "file an issue report at: https://github.com/zfsonlinux/zfs/issues/new"
416 #endif
417 }
418
419 /*
420 * 4.8 - 4.x API,
421 * REQ_OP_DISCARD
422 *
423 * 2.6.36 - 4.7 API,
424 * REQ_DISCARD
425 *
426 * 2.6.28 - 2.6.35 API,
427 * BIO_RW_DISCARD
428 *
429 * In all cases the normal I/O path is used for discards. The only
430 * difference is how the kernel tags individual I/Os as discards.
431 *
432 * Note that 2.6.32 era kernels provide both BIO_RW_DISCARD and REQ_DISCARD,
433 * where BIO_RW_DISCARD is the correct interface. Therefore, it is important
434 * that the HAVE_BIO_RW_DISCARD check occur before the REQ_DISCARD check.
435 */
436 static inline boolean_t
437 bio_is_discard(struct bio *bio)
438 {
439 #if defined(HAVE_REQ_OP_DISCARD)
440 return (bio_op(bio) == REQ_OP_DISCARD);
441 #elif defined(HAVE_BIO_RW_DISCARD)
442 return (bio->bi_rw & (1 << BIO_RW_DISCARD));
443 #elif defined(REQ_DISCARD)
444 return (bio->bi_rw & REQ_DISCARD);
445 #else
446 #error "Allowing the build will cause discard requests to become writes "
447 "potentially triggering the DMU_MAX_ACCESS assertion. Please file "
448 "an issue report at: https://github.com/zfsonlinux/zfs/issues/new"
449 #endif
450 }
451
452 /*
453 * 4.8 - 4.x API,
454 * REQ_OP_SECURE_ERASE
455 *
456 * 2.6.36 - 4.7 API,
457 * REQ_SECURE
458 *
459 * 2.6.x - 2.6.35 API,
460 * Unsupported by kernel
461 */
462 static inline boolean_t
463 bio_is_secure_erase(struct bio *bio)
464 {
465 #if defined(HAVE_REQ_OP_SECURE_ERASE)
466 return (bio_op(bio) == REQ_OP_SECURE_ERASE);
467 #elif defined(REQ_SECURE)
468 return (bio->bi_rw & REQ_SECURE);
469 #else
470 return (0);
471 #endif
472 }
473
474 /*
475 * 2.6.33 API change
476 * Discard granularity and alignment restrictions may now be set. For
477 * older kernels which do not support this it is safe to skip it.
478 */
479 #ifdef HAVE_DISCARD_GRANULARITY
480 static inline void
481 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
482 {
483 q->limits.discard_granularity = dg;
484 }
485 #else
486 #define blk_queue_discard_granularity(x, dg) ((void)0)
487 #endif /* HAVE_DISCARD_GRANULARITY */
488
489 /*
490 * Default Linux IO Scheduler,
491 * Setting the scheduler to noop will allow the Linux IO scheduler to
492 * still perform front and back merging, while leaving the request
493 * ordering and prioritization to the ZFS IO scheduler.
494 */
495 #define VDEV_SCHEDULER "noop"
496
497 /*
498 * A common holder for vdev_bdev_open() is used to relax the exclusive open
499 * semantics slightly. Internal vdev disk callers may pass VDEV_HOLDER to
500 * allow them to open the device multiple times. Other kernel callers and
501 * user space processes which don't pass this value will get EBUSY. This is
502 * currently required for the correct operation of hot spares.
503 */
504 #define VDEV_HOLDER ((void *)0x2401de7)
505
506 #ifndef HAVE_GENERIC_IO_ACCT
507 #define generic_start_io_acct(rw, slen, part) ((void)0)
508 #define generic_end_io_acct(rw, part, start_jiffies) ((void)0)
509 #endif
510
511 #endif /* _ZFS_BLKDEV_H */