]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/include/os/linux/kernel/linux/blkdev_compat.h
bsddialog: import version 0.0.1
[FreeBSD/FreeBSD.git] / sys / contrib / openzfs / include / os / linux / kernel / 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/backing-dev.h>
34 #include <linux/hdreg.h>
35 #include <linux/major.h>
36 #include <linux/msdos_fs.h>     /* for SECTOR_* */
37
38 #ifndef HAVE_BLK_QUEUE_FLAG_SET
39 static inline void
40 blk_queue_flag_set(unsigned int flag, struct request_queue *q)
41 {
42         queue_flag_set(flag, q);
43 }
44 #endif
45
46 #ifndef HAVE_BLK_QUEUE_FLAG_CLEAR
47 static inline void
48 blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
49 {
50         queue_flag_clear(flag, q);
51 }
52 #endif
53
54 /*
55  * 4.7 API,
56  * The blk_queue_write_cache() interface has replaced blk_queue_flush()
57  * interface.  However, the new interface is GPL-only thus we implement
58  * our own trivial wrapper when the GPL-only version is detected.
59  *
60  * 2.6.36 - 4.6 API,
61  * The blk_queue_flush() interface has replaced blk_queue_ordered()
62  * interface.  However, while the old interface was available to all the
63  * new one is GPL-only.   Thus if the GPL-only version is detected we
64  * implement our own trivial helper.
65  */
66 static inline void
67 blk_queue_set_write_cache(struct request_queue *q, bool wc, bool fua)
68 {
69 #if defined(HAVE_BLK_QUEUE_WRITE_CACHE_GPL_ONLY)
70         if (wc)
71                 blk_queue_flag_set(QUEUE_FLAG_WC, q);
72         else
73                 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
74         if (fua)
75                 blk_queue_flag_set(QUEUE_FLAG_FUA, q);
76         else
77                 blk_queue_flag_clear(QUEUE_FLAG_FUA, q);
78 #elif defined(HAVE_BLK_QUEUE_WRITE_CACHE)
79         blk_queue_write_cache(q, wc, fua);
80 #elif defined(HAVE_BLK_QUEUE_FLUSH_GPL_ONLY)
81         if (wc)
82                 q->flush_flags |= REQ_FLUSH;
83         if (fua)
84                 q->flush_flags |= REQ_FUA;
85 #elif defined(HAVE_BLK_QUEUE_FLUSH)
86         blk_queue_flush(q, (wc ? REQ_FLUSH : 0) | (fua ? REQ_FUA : 0));
87 #else
88 #error "Unsupported kernel"
89 #endif
90 }
91
92 static inline void
93 blk_queue_set_read_ahead(struct request_queue *q, unsigned long ra_pages)
94 {
95 #if !defined(HAVE_BLK_QUEUE_UPDATE_READAHEAD) && \
96         !defined(HAVE_DISK_UPDATE_READAHEAD)
97 #ifdef HAVE_BLK_QUEUE_BDI_DYNAMIC
98         q->backing_dev_info->ra_pages = ra_pages;
99 #else
100         q->backing_dev_info.ra_pages = ra_pages;
101 #endif
102 #endif
103 }
104
105 #ifdef HAVE_BIO_BVEC_ITER
106 #define BIO_BI_SECTOR(bio)      (bio)->bi_iter.bi_sector
107 #define BIO_BI_SIZE(bio)        (bio)->bi_iter.bi_size
108 #define BIO_BI_IDX(bio)         (bio)->bi_iter.bi_idx
109 #define BIO_BI_SKIP(bio)        (bio)->bi_iter.bi_bvec_done
110 #define bio_for_each_segment4(bv, bvp, b, i)    \
111         bio_for_each_segment((bv), (b), (i))
112 typedef struct bvec_iter bvec_iterator_t;
113 #else
114 #define BIO_BI_SECTOR(bio)      (bio)->bi_sector
115 #define BIO_BI_SIZE(bio)        (bio)->bi_size
116 #define BIO_BI_IDX(bio)         (bio)->bi_idx
117 #define BIO_BI_SKIP(bio)        (0)
118 #define bio_for_each_segment4(bv, bvp, b, i)    \
119         bio_for_each_segment((bvp), (b), (i))
120 typedef int bvec_iterator_t;
121 #endif
122
123 static inline void
124 bio_set_flags_failfast(struct block_device *bdev, int *flags)
125 {
126 #ifdef CONFIG_BUG
127         /*
128          * Disable FAILFAST for loopback devices because of the
129          * following incorrect BUG_ON() in loop_make_request().
130          * This support is also disabled for md devices because the
131          * test suite layers md devices on top of loopback devices.
132          * This may be removed when the loopback driver is fixed.
133          *
134          *   BUG_ON(!lo || (rw != READ && rw != WRITE));
135          */
136         if ((MAJOR(bdev->bd_dev) == LOOP_MAJOR) ||
137             (MAJOR(bdev->bd_dev) == MD_MAJOR))
138                 return;
139
140 #ifdef BLOCK_EXT_MAJOR
141         if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
142                 return;
143 #endif /* BLOCK_EXT_MAJOR */
144 #endif /* CONFIG_BUG */
145
146         *flags |= REQ_FAILFAST_MASK;
147 }
148
149 /*
150  * Maximum disk label length, it may be undefined for some kernels.
151  */
152 #if !defined(DISK_NAME_LEN)
153 #define DISK_NAME_LEN   32
154 #endif /* DISK_NAME_LEN */
155
156 #ifdef HAVE_BIO_BI_STATUS
157 static inline int
158 bi_status_to_errno(blk_status_t status)
159 {
160         switch (status) {
161         case BLK_STS_OK:
162                 return (0);
163         case BLK_STS_NOTSUPP:
164                 return (EOPNOTSUPP);
165         case BLK_STS_TIMEOUT:
166                 return (ETIMEDOUT);
167         case BLK_STS_NOSPC:
168                 return (ENOSPC);
169         case BLK_STS_TRANSPORT:
170                 return (ENOLINK);
171         case BLK_STS_TARGET:
172                 return (EREMOTEIO);
173         case BLK_STS_NEXUS:
174                 return (EBADE);
175         case BLK_STS_MEDIUM:
176                 return (ENODATA);
177         case BLK_STS_PROTECTION:
178                 return (EILSEQ);
179         case BLK_STS_RESOURCE:
180                 return (ENOMEM);
181         case BLK_STS_AGAIN:
182                 return (EAGAIN);
183         case BLK_STS_IOERR:
184                 return (EIO);
185         default:
186                 return (EIO);
187         }
188 }
189
190 static inline blk_status_t
191 errno_to_bi_status(int error)
192 {
193         switch (error) {
194         case 0:
195                 return (BLK_STS_OK);
196         case EOPNOTSUPP:
197                 return (BLK_STS_NOTSUPP);
198         case ETIMEDOUT:
199                 return (BLK_STS_TIMEOUT);
200         case ENOSPC:
201                 return (BLK_STS_NOSPC);
202         case ENOLINK:
203                 return (BLK_STS_TRANSPORT);
204         case EREMOTEIO:
205                 return (BLK_STS_TARGET);
206         case EBADE:
207                 return (BLK_STS_NEXUS);
208         case ENODATA:
209                 return (BLK_STS_MEDIUM);
210         case EILSEQ:
211                 return (BLK_STS_PROTECTION);
212         case ENOMEM:
213                 return (BLK_STS_RESOURCE);
214         case EAGAIN:
215                 return (BLK_STS_AGAIN);
216         case EIO:
217                 return (BLK_STS_IOERR);
218         default:
219                 return (BLK_STS_IOERR);
220         }
221 }
222 #endif /* HAVE_BIO_BI_STATUS */
223
224 /*
225  * 4.3 API change
226  * The bio_endio() prototype changed slightly.  These are helper
227  * macro's to ensure the prototype and invocation are handled.
228  */
229 #ifdef HAVE_1ARG_BIO_END_IO_T
230 #ifdef HAVE_BIO_BI_STATUS
231 #define BIO_END_IO_ERROR(bio)           bi_status_to_errno(bio->bi_status)
232 #define BIO_END_IO_PROTO(fn, x, z)      static void fn(struct bio *x)
233 #define BIO_END_IO(bio, error)          bio_set_bi_status(bio, error)
234 static inline void
235 bio_set_bi_status(struct bio *bio, int error)
236 {
237         ASSERT3S(error, <=, 0);
238         bio->bi_status = errno_to_bi_status(-error);
239         bio_endio(bio);
240 }
241 #else
242 #define BIO_END_IO_ERROR(bio)           (-(bio->bi_error))
243 #define BIO_END_IO_PROTO(fn, x, z)      static void fn(struct bio *x)
244 #define BIO_END_IO(bio, error)          bio_set_bi_error(bio, error)
245 static inline void
246 bio_set_bi_error(struct bio *bio, int error)
247 {
248         ASSERT3S(error, <=, 0);
249         bio->bi_error = error;
250         bio_endio(bio);
251 }
252 #endif /* HAVE_BIO_BI_STATUS */
253
254 #else
255 #define BIO_END_IO_PROTO(fn, x, z)      static void fn(struct bio *x, int z)
256 #define BIO_END_IO(bio, error)          bio_endio(bio, error);
257 #endif /* HAVE_1ARG_BIO_END_IO_T */
258
259 /*
260  * 4.1 API,
261  * 3.10.0 CentOS 7.x API,
262  *   blkdev_reread_part()
263  *
264  * For older kernels trigger a re-reading of the partition table by calling
265  * check_disk_change() which calls flush_disk() to invalidate the device.
266  *
267  * For newer kernels (as of 5.10), bdev_check_media_change is used, in favor of
268  * check_disk_change(), with the modification that invalidation is no longer
269  * forced.
270  */
271 #ifdef HAVE_CHECK_DISK_CHANGE
272 #define zfs_check_media_change(bdev)    check_disk_change(bdev)
273 #ifdef HAVE_BLKDEV_REREAD_PART
274 #define vdev_bdev_reread_part(bdev)     blkdev_reread_part(bdev)
275 #else
276 #define vdev_bdev_reread_part(bdev)     check_disk_change(bdev)
277 #endif /* HAVE_BLKDEV_REREAD_PART */
278 #else
279 #ifdef HAVE_BDEV_CHECK_MEDIA_CHANGE
280 static inline int
281 zfs_check_media_change(struct block_device *bdev)
282 {
283 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
284         struct gendisk *gd = bdev->bd_disk;
285         const struct block_device_operations *bdo = gd->fops;
286 #endif
287
288         if (!bdev_check_media_change(bdev))
289                 return (0);
290
291 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
292         /*
293          * Force revalidation, to mimic the old behavior of
294          * check_disk_change()
295          */
296         if (bdo->revalidate_disk)
297                 bdo->revalidate_disk(gd);
298 #endif
299
300         return (0);
301 }
302 #define vdev_bdev_reread_part(bdev)     zfs_check_media_change(bdev)
303 #else
304 /*
305  * This is encountered if check_disk_change() and bdev_check_media_change()
306  * are not available in the kernel - likely due to an API change that needs
307  * to be chased down.
308  */
309 #error "Unsupported kernel: no usable disk change check"
310 #endif /* HAVE_BDEV_CHECK_MEDIA_CHANGE */
311 #endif /* HAVE_CHECK_DISK_CHANGE */
312
313 /*
314  * 2.6.27 API change
315  * The function was exported for use, prior to this it existed but the
316  * symbol was not exported.
317  *
318  * 4.4.0-6.21 API change for Ubuntu
319  * lookup_bdev() gained a second argument, FMODE_*, to check inode permissions.
320  *
321  * 5.11 API change
322  * Changed to take a dev_t argument which is set on success and return a
323  * non-zero error code on failure.
324  */
325 static inline int
326 vdev_lookup_bdev(const char *path, dev_t *dev)
327 {
328 #if defined(HAVE_DEVT_LOOKUP_BDEV)
329         return (lookup_bdev(path, dev));
330 #elif defined(HAVE_1ARG_LOOKUP_BDEV)
331         struct block_device *bdev = lookup_bdev(path);
332         if (IS_ERR(bdev))
333                 return (PTR_ERR(bdev));
334
335         *dev = bdev->bd_dev;
336         bdput(bdev);
337
338         return (0);
339 #elif defined(HAVE_MODE_LOOKUP_BDEV)
340         struct block_device *bdev = lookup_bdev(path, FMODE_READ);
341         if (IS_ERR(bdev))
342                 return (PTR_ERR(bdev));
343
344         *dev = bdev->bd_dev;
345         bdput(bdev);
346
347         return (0);
348 #else
349 #error "Unsupported kernel"
350 #endif
351 }
352
353 /*
354  * Kernels without bio_set_op_attrs use bi_rw for the bio flags.
355  */
356 #if !defined(HAVE_BIO_SET_OP_ATTRS)
357 static inline void
358 bio_set_op_attrs(struct bio *bio, unsigned rw, unsigned flags)
359 {
360         bio->bi_rw |= rw | flags;
361 }
362 #endif
363
364 /*
365  * bio_set_flush - Set the appropriate flags in a bio to guarantee
366  * data are on non-volatile media on completion.
367  *
368  * 2.6.37 - 4.8 API,
369  *   Introduce WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags as a
370  *   replacement for WRITE_BARRIER to allow expressing richer semantics
371  *   to the block layer.  It's up to the block layer to implement the
372  *   semantics correctly. Use the WRITE_FLUSH_FUA flag combination.
373  *
374  * 4.8 - 4.9 API,
375  *   REQ_FLUSH was renamed to REQ_PREFLUSH.  For consistency with previous
376  *   OpenZFS releases, prefer the WRITE_FLUSH_FUA flag set if it's available.
377  *
378  * 4.10 API,
379  *   The read/write flags and their modifiers, including WRITE_FLUSH,
380  *   WRITE_FUA and WRITE_FLUSH_FUA were removed from fs.h in
381  *   torvalds/linux@70fd7614 and replaced by direct flag modification
382  *   of the REQ_ flags in bio->bi_opf.  Use REQ_PREFLUSH.
383  */
384 static inline void
385 bio_set_flush(struct bio *bio)
386 {
387 #if defined(HAVE_REQ_PREFLUSH)  /* >= 4.10 */
388         bio_set_op_attrs(bio, 0, REQ_PREFLUSH);
389 #elif defined(WRITE_FLUSH_FUA)  /* >= 2.6.37 and <= 4.9 */
390         bio_set_op_attrs(bio, 0, WRITE_FLUSH_FUA);
391 #else
392 #error  "Allowing the build will cause bio_set_flush requests to be ignored."
393 #endif
394 }
395
396 /*
397  * 4.8 API,
398  *   REQ_OP_FLUSH
399  *
400  * 4.8-rc0 - 4.8-rc1,
401  *   REQ_PREFLUSH
402  *
403  * 2.6.36 - 4.7 API,
404  *   REQ_FLUSH
405  *
406  * in all cases but may have a performance impact for some kernels.  It
407  * has the advantage of minimizing kernel specific changes in the zvol code.
408  *
409  */
410 static inline boolean_t
411 bio_is_flush(struct bio *bio)
412 {
413 #if defined(HAVE_REQ_OP_FLUSH) && defined(HAVE_BIO_BI_OPF)
414         return ((bio_op(bio) == REQ_OP_FLUSH) || (bio->bi_opf & REQ_PREFLUSH));
415 #elif defined(HAVE_REQ_PREFLUSH) && defined(HAVE_BIO_BI_OPF)
416         return (bio->bi_opf & REQ_PREFLUSH);
417 #elif defined(HAVE_REQ_PREFLUSH) && !defined(HAVE_BIO_BI_OPF)
418         return (bio->bi_rw & REQ_PREFLUSH);
419 #elif defined(HAVE_REQ_FLUSH)
420         return (bio->bi_rw & REQ_FLUSH);
421 #else
422 #error  "Unsupported kernel"
423 #endif
424 }
425
426 /*
427  * 4.8 API,
428  *   REQ_FUA flag moved to bio->bi_opf
429  *
430  * 2.6.x - 4.7 API,
431  *   REQ_FUA
432  */
433 static inline boolean_t
434 bio_is_fua(struct bio *bio)
435 {
436 #if defined(HAVE_BIO_BI_OPF)
437         return (bio->bi_opf & REQ_FUA);
438 #elif defined(REQ_FUA)
439         return (bio->bi_rw & REQ_FUA);
440 #else
441 #error  "Allowing the build will cause fua requests to be ignored."
442 #endif
443 }
444
445 /*
446  * 4.8 API,
447  *   REQ_OP_DISCARD
448  *
449  * 2.6.36 - 4.7 API,
450  *   REQ_DISCARD
451  *
452  * In all cases the normal I/O path is used for discards.  The only
453  * difference is how the kernel tags individual I/Os as discards.
454  */
455 static inline boolean_t
456 bio_is_discard(struct bio *bio)
457 {
458 #if defined(HAVE_REQ_OP_DISCARD)
459         return (bio_op(bio) == REQ_OP_DISCARD);
460 #elif defined(HAVE_REQ_DISCARD)
461         return (bio->bi_rw & REQ_DISCARD);
462 #else
463 #error "Unsupported kernel"
464 #endif
465 }
466
467 /*
468  * 4.8 API,
469  *   REQ_OP_SECURE_ERASE
470  *
471  * 2.6.36 - 4.7 API,
472  *   REQ_SECURE
473  */
474 static inline boolean_t
475 bio_is_secure_erase(struct bio *bio)
476 {
477 #if defined(HAVE_REQ_OP_SECURE_ERASE)
478         return (bio_op(bio) == REQ_OP_SECURE_ERASE);
479 #elif defined(REQ_SECURE)
480         return (bio->bi_rw & REQ_SECURE);
481 #else
482         return (0);
483 #endif
484 }
485
486 /*
487  * 2.6.33 API change
488  * Discard granularity and alignment restrictions may now be set.  For
489  * older kernels which do not support this it is safe to skip it.
490  */
491 static inline void
492 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
493 {
494         q->limits.discard_granularity = dg;
495 }
496
497 /*
498  * 4.8 API,
499  *   blk_queue_secure_erase()
500  *
501  * 2.6.36 - 4.7 API,
502  *   blk_queue_secdiscard()
503  */
504 static inline int
505 blk_queue_discard_secure(struct request_queue *q)
506 {
507 #if defined(HAVE_BLK_QUEUE_SECURE_ERASE)
508         return (blk_queue_secure_erase(q));
509 #elif defined(HAVE_BLK_QUEUE_SECDISCARD)
510         return (blk_queue_secdiscard(q));
511 #else
512         return (0);
513 #endif
514 }
515
516 /*
517  * A common holder for vdev_bdev_open() is used to relax the exclusive open
518  * semantics slightly.  Internal vdev disk callers may pass VDEV_HOLDER to
519  * allow them to open the device multiple times.  Other kernel callers and
520  * user space processes which don't pass this value will get EBUSY.  This is
521  * currently required for the correct operation of hot spares.
522  */
523 #define VDEV_HOLDER                     ((void *)0x2401de7)
524
525 static inline unsigned long
526 blk_generic_start_io_acct(struct request_queue *q __attribute__((unused)),
527     struct gendisk *disk __attribute__((unused)),
528     int rw __attribute__((unused)), struct bio *bio)
529 {
530 #if defined(HAVE_DISK_IO_ACCT)
531         return (disk_start_io_acct(disk, bio_sectors(bio), bio_op(bio)));
532 #elif defined(HAVE_BIO_IO_ACCT)
533         return (bio_start_io_acct(bio));
534 #elif defined(HAVE_GENERIC_IO_ACCT_3ARG)
535         unsigned long start_time = jiffies;
536         generic_start_io_acct(rw, bio_sectors(bio), &disk->part0);
537         return (start_time);
538 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
539         unsigned long start_time = jiffies;
540         generic_start_io_acct(q, rw, bio_sectors(bio), &disk->part0);
541         return (start_time);
542 #else
543         /* Unsupported */
544         return (0);
545 #endif
546 }
547
548 static inline void
549 blk_generic_end_io_acct(struct request_queue *q __attribute__((unused)),
550     struct gendisk *disk __attribute__((unused)),
551     int rw __attribute__((unused)), struct bio *bio, unsigned long start_time)
552 {
553 #if defined(HAVE_DISK_IO_ACCT)
554         disk_end_io_acct(disk, bio_op(bio), start_time);
555 #elif defined(HAVE_BIO_IO_ACCT)
556         bio_end_io_acct(bio, start_time);
557 #elif defined(HAVE_GENERIC_IO_ACCT_3ARG)
558         generic_end_io_acct(rw, &disk->part0, start_time);
559 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
560         generic_end_io_acct(q, rw, &disk->part0, start_time);
561 #endif
562 }
563
564 #ifndef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
565 static inline struct request_queue *
566 blk_generic_alloc_queue(make_request_fn make_request, int node_id)
567 {
568 #if defined(HAVE_BLK_ALLOC_QUEUE_REQUEST_FN)
569         return (blk_alloc_queue(make_request, node_id));
570 #elif defined(HAVE_BLK_ALLOC_QUEUE_REQUEST_FN_RH)
571         return (blk_alloc_queue_rh(make_request, node_id));
572 #else
573         struct request_queue *q = blk_alloc_queue(GFP_KERNEL);
574         if (q != NULL)
575                 blk_queue_make_request(q, make_request);
576
577         return (q);
578 #endif
579 }
580 #endif /* !HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
581
582 #endif /* _ZFS_BLKDEV_H */