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