xref: /freebsd-13-stable/sys/contrib/openzfs/include/os/linux/kernel/linux/blkdev_compat.h (revision d9a61490b0988b1187ea169e538762f70dfaeb12)
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
blk_queue_flag_set(unsigned int flag,struct request_queue * q)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
blk_queue_flag_clear(unsigned int flag,struct request_queue * q)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
blk_queue_set_write_cache(struct request_queue * q,bool wc,bool fua)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
blk_queue_set_read_ahead(struct request_queue * q,unsigned long ra_pages)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
bio_set_flags_failfast(struct block_device * bdev,int * flags)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
bi_status_to_errno(blk_status_t status)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 #ifdef HAVE_BLK_STS_RESV_CONFLICT
174 	case BLK_STS_RESV_CONFLICT:
175 #else
176 	case BLK_STS_NEXUS:
177 #endif
178 		return (EBADE);
179 	case BLK_STS_MEDIUM:
180 		return (ENODATA);
181 	case BLK_STS_PROTECTION:
182 		return (EILSEQ);
183 	case BLK_STS_RESOURCE:
184 		return (ENOMEM);
185 	case BLK_STS_AGAIN:
186 		return (EAGAIN);
187 	case BLK_STS_IOERR:
188 		return (EIO);
189 	default:
190 		return (EIO);
191 	}
192 }
193 
194 static inline blk_status_t
errno_to_bi_status(int error)195 errno_to_bi_status(int error)
196 {
197 	switch (error) {
198 	case 0:
199 		return (BLK_STS_OK);
200 	case EOPNOTSUPP:
201 		return (BLK_STS_NOTSUPP);
202 	case ETIMEDOUT:
203 		return (BLK_STS_TIMEOUT);
204 	case ENOSPC:
205 		return (BLK_STS_NOSPC);
206 	case ENOLINK:
207 		return (BLK_STS_TRANSPORT);
208 	case EREMOTEIO:
209 		return (BLK_STS_TARGET);
210 	case EBADE:
211 #ifdef HAVE_BLK_STS_RESV_CONFLICT
212 		return (BLK_STS_RESV_CONFLICT);
213 #else
214 		return (BLK_STS_NEXUS);
215 #endif
216 	case ENODATA:
217 		return (BLK_STS_MEDIUM);
218 	case EILSEQ:
219 		return (BLK_STS_PROTECTION);
220 	case ENOMEM:
221 		return (BLK_STS_RESOURCE);
222 	case EAGAIN:
223 		return (BLK_STS_AGAIN);
224 	case EIO:
225 		return (BLK_STS_IOERR);
226 	default:
227 		return (BLK_STS_IOERR);
228 	}
229 }
230 #endif /* HAVE_BIO_BI_STATUS */
231 
232 /*
233  * 4.3 API change
234  * The bio_endio() prototype changed slightly.  These are helper
235  * macro's to ensure the prototype and invocation are handled.
236  */
237 #ifdef HAVE_1ARG_BIO_END_IO_T
238 #ifdef HAVE_BIO_BI_STATUS
239 #define	BIO_END_IO_ERROR(bio)		bi_status_to_errno(bio->bi_status)
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_status(bio, error)
242 static inline void
bio_set_bi_status(struct bio * bio,int error)243 bio_set_bi_status(struct bio *bio, int error)
244 {
245 	ASSERT3S(error, <=, 0);
246 	bio->bi_status = errno_to_bi_status(-error);
247 	bio_endio(bio);
248 }
249 #else
250 #define	BIO_END_IO_ERROR(bio)		(-(bio->bi_error))
251 #define	BIO_END_IO_PROTO(fn, x, z)	static void fn(struct bio *x)
252 #define	BIO_END_IO(bio, error)		bio_set_bi_error(bio, error)
253 static inline void
bio_set_bi_error(struct bio * bio,int error)254 bio_set_bi_error(struct bio *bio, int error)
255 {
256 	ASSERT3S(error, <=, 0);
257 	bio->bi_error = error;
258 	bio_endio(bio);
259 }
260 #endif /* HAVE_BIO_BI_STATUS */
261 
262 #else
263 #define	BIO_END_IO_PROTO(fn, x, z)	static void fn(struct bio *x, int z)
264 #define	BIO_END_IO(bio, error)		bio_endio(bio, error);
265 #endif /* HAVE_1ARG_BIO_END_IO_T */
266 
267 /*
268  * 5.15 MACRO,
269  *   GD_DEAD
270  *
271  * 2.6.36 - 5.14 MACRO,
272  *   GENHD_FL_UP
273  *
274  * Check the disk status and return B_TRUE if alive
275  * otherwise B_FALSE
276  */
277 static inline boolean_t
zfs_check_disk_status(struct block_device * bdev)278 zfs_check_disk_status(struct block_device *bdev)
279 {
280 #if defined(GENHD_FL_UP)
281 	return (!!(bdev->bd_disk->flags & GENHD_FL_UP));
282 #elif defined(GD_DEAD)
283 	return (!test_bit(GD_DEAD, &bdev->bd_disk->state));
284 #else
285 /*
286  * This is encountered if neither GENHD_FL_UP nor GD_DEAD is available in
287  * the kernel - likely due to an MACRO change that needs to be chased down.
288  */
289 #error "Unsupported kernel: no usable disk status check"
290 #endif
291 }
292 
293 /*
294  * 4.1 API,
295  * 3.10.0 CentOS 7.x API,
296  *   blkdev_reread_part()
297  *
298  * For older kernels trigger a re-reading of the partition table by calling
299  * check_disk_change() which calls flush_disk() to invalidate the device.
300  *
301  * For newer kernels (as of 5.10), bdev_check_media_change is used, in favor of
302  * check_disk_change(), with the modification that invalidation is no longer
303  * forced.
304  */
305 #ifdef HAVE_CHECK_DISK_CHANGE
306 #define	zfs_check_media_change(bdev)	check_disk_change(bdev)
307 #ifdef HAVE_BLKDEV_REREAD_PART
308 #define	vdev_bdev_reread_part(bdev)	blkdev_reread_part(bdev)
309 #else
310 #define	vdev_bdev_reread_part(bdev)	check_disk_change(bdev)
311 #endif /* HAVE_BLKDEV_REREAD_PART */
312 #else
313 #ifdef HAVE_BDEV_CHECK_MEDIA_CHANGE
314 static inline int
zfs_check_media_change(struct block_device * bdev)315 zfs_check_media_change(struct block_device *bdev)
316 {
317 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
318 	struct gendisk *gd = bdev->bd_disk;
319 	const struct block_device_operations *bdo = gd->fops;
320 #endif
321 
322 	if (!bdev_check_media_change(bdev))
323 		return (0);
324 
325 #ifdef HAVE_BLOCK_DEVICE_OPERATIONS_REVALIDATE_DISK
326 	/*
327 	 * Force revalidation, to mimic the old behavior of
328 	 * check_disk_change()
329 	 */
330 	if (bdo->revalidate_disk)
331 		bdo->revalidate_disk(gd);
332 #endif
333 
334 	return (0);
335 }
336 #define	vdev_bdev_reread_part(bdev)	zfs_check_media_change(bdev)
337 #elif defined(HAVE_DISK_CHECK_MEDIA_CHANGE)
338 #define	vdev_bdev_reread_part(bdev)	disk_check_media_change(bdev->bd_disk)
339 #define	zfs_check_media_change(bdev)	disk_check_media_change(bdev->bd_disk)
340 #else
341 /*
342  * This is encountered if check_disk_change() and bdev_check_media_change()
343  * are not available in the kernel - likely due to an API change that needs
344  * to be chased down.
345  */
346 #error "Unsupported kernel: no usable disk change check"
347 #endif /* HAVE_BDEV_CHECK_MEDIA_CHANGE */
348 #endif /* HAVE_CHECK_DISK_CHANGE */
349 
350 /*
351  * 2.6.27 API change
352  * The function was exported for use, prior to this it existed but the
353  * symbol was not exported.
354  *
355  * 4.4.0-6.21 API change for Ubuntu
356  * lookup_bdev() gained a second argument, FMODE_*, to check inode permissions.
357  *
358  * 5.11 API change
359  * Changed to take a dev_t argument which is set on success and return a
360  * non-zero error code on failure.
361  */
362 static inline int
vdev_lookup_bdev(const char * path,dev_t * dev)363 vdev_lookup_bdev(const char *path, dev_t *dev)
364 {
365 #if defined(HAVE_DEVT_LOOKUP_BDEV)
366 	return (lookup_bdev(path, dev));
367 #elif defined(HAVE_1ARG_LOOKUP_BDEV)
368 	struct block_device *bdev = lookup_bdev(path);
369 	if (IS_ERR(bdev))
370 		return (PTR_ERR(bdev));
371 
372 	*dev = bdev->bd_dev;
373 	bdput(bdev);
374 
375 	return (0);
376 #elif defined(HAVE_MODE_LOOKUP_BDEV)
377 	struct block_device *bdev = lookup_bdev(path, FMODE_READ);
378 	if (IS_ERR(bdev))
379 		return (PTR_ERR(bdev));
380 
381 	*dev = bdev->bd_dev;
382 	bdput(bdev);
383 
384 	return (0);
385 #else
386 #error "Unsupported kernel"
387 #endif
388 }
389 
390 #if defined(HAVE_BLK_MODE_T)
391 #define	blk_mode_is_open_write(flag)	((flag) & BLK_OPEN_WRITE)
392 #else
393 #define	blk_mode_is_open_write(flag)	((flag) & FMODE_WRITE)
394 #endif
395 
396 /*
397  * Kernels without bio_set_op_attrs use bi_rw for the bio flags.
398  */
399 #if !defined(HAVE_BIO_SET_OP_ATTRS)
400 static inline void
bio_set_op_attrs(struct bio * bio,unsigned rw,unsigned flags)401 bio_set_op_attrs(struct bio *bio, unsigned rw, unsigned flags)
402 {
403 #if defined(HAVE_BIO_BI_OPF)
404 	bio->bi_opf = rw | flags;
405 #else
406 	bio->bi_rw |= rw | flags;
407 #endif /* HAVE_BIO_BI_OPF */
408 }
409 #endif
410 
411 /*
412  * bio_set_flush - Set the appropriate flags in a bio to guarantee
413  * data are on non-volatile media on completion.
414  *
415  * 2.6.37 - 4.8 API,
416  *   Introduce WRITE_FLUSH, WRITE_FUA, and WRITE_FLUSH_FUA flags as a
417  *   replacement for WRITE_BARRIER to allow expressing richer semantics
418  *   to the block layer.  It's up to the block layer to implement the
419  *   semantics correctly. Use the WRITE_FLUSH_FUA flag combination.
420  *
421  * 4.8 - 4.9 API,
422  *   REQ_FLUSH was renamed to REQ_PREFLUSH.  For consistency with previous
423  *   OpenZFS releases, prefer the WRITE_FLUSH_FUA flag set if it's available.
424  *
425  * 4.10 API,
426  *   The read/write flags and their modifiers, including WRITE_FLUSH,
427  *   WRITE_FUA and WRITE_FLUSH_FUA were removed from fs.h in
428  *   torvalds/linux@70fd7614 and replaced by direct flag modification
429  *   of the REQ_ flags in bio->bi_opf.  Use REQ_PREFLUSH.
430  */
431 static inline void
bio_set_flush(struct bio * bio)432 bio_set_flush(struct bio *bio)
433 {
434 #if defined(HAVE_REQ_PREFLUSH)	/* >= 4.10 */
435 	bio_set_op_attrs(bio, 0, REQ_PREFLUSH | REQ_OP_WRITE);
436 #elif defined(WRITE_FLUSH_FUA)	/* >= 2.6.37 and <= 4.9 */
437 	bio_set_op_attrs(bio, 0, WRITE_FLUSH_FUA);
438 #else
439 #error	"Allowing the build will cause bio_set_flush requests to be ignored."
440 #endif
441 }
442 
443 /*
444  * 4.8 API,
445  *   REQ_OP_FLUSH
446  *
447  * 4.8-rc0 - 4.8-rc1,
448  *   REQ_PREFLUSH
449  *
450  * 2.6.36 - 4.7 API,
451  *   REQ_FLUSH
452  *
453  * in all cases but may have a performance impact for some kernels.  It
454  * has the advantage of minimizing kernel specific changes in the zvol code.
455  *
456  */
457 static inline boolean_t
bio_is_flush(struct bio * bio)458 bio_is_flush(struct bio *bio)
459 {
460 #if defined(HAVE_REQ_OP_FLUSH) && defined(HAVE_BIO_BI_OPF)
461 	return ((bio_op(bio) == REQ_OP_FLUSH) || (bio->bi_opf & REQ_PREFLUSH));
462 #elif defined(HAVE_REQ_PREFLUSH) && defined(HAVE_BIO_BI_OPF)
463 	return (bio->bi_opf & REQ_PREFLUSH);
464 #elif defined(HAVE_REQ_PREFLUSH) && !defined(HAVE_BIO_BI_OPF)
465 	return (bio->bi_rw & REQ_PREFLUSH);
466 #elif defined(HAVE_REQ_FLUSH)
467 	return (bio->bi_rw & REQ_FLUSH);
468 #else
469 #error	"Unsupported kernel"
470 #endif
471 }
472 
473 /*
474  * 4.8 API,
475  *   REQ_FUA flag moved to bio->bi_opf
476  *
477  * 2.6.x - 4.7 API,
478  *   REQ_FUA
479  */
480 static inline boolean_t
bio_is_fua(struct bio * bio)481 bio_is_fua(struct bio *bio)
482 {
483 #if defined(HAVE_BIO_BI_OPF)
484 	return (bio->bi_opf & REQ_FUA);
485 #elif defined(REQ_FUA)
486 	return (bio->bi_rw & REQ_FUA);
487 #else
488 #error	"Allowing the build will cause fua requests to be ignored."
489 #endif
490 }
491 
492 /*
493  * 4.8 API,
494  *   REQ_OP_DISCARD
495  *
496  * 2.6.36 - 4.7 API,
497  *   REQ_DISCARD
498  *
499  * In all cases the normal I/O path is used for discards.  The only
500  * difference is how the kernel tags individual I/Os as discards.
501  */
502 static inline boolean_t
bio_is_discard(struct bio * bio)503 bio_is_discard(struct bio *bio)
504 {
505 #if defined(HAVE_REQ_OP_DISCARD)
506 	return (bio_op(bio) == REQ_OP_DISCARD);
507 #elif defined(HAVE_REQ_DISCARD)
508 	return (bio->bi_rw & REQ_DISCARD);
509 #else
510 #error "Unsupported kernel"
511 #endif
512 }
513 
514 /*
515  * 4.8 API,
516  *   REQ_OP_SECURE_ERASE
517  *
518  * 2.6.36 - 4.7 API,
519  *   REQ_SECURE
520  */
521 static inline boolean_t
bio_is_secure_erase(struct bio * bio)522 bio_is_secure_erase(struct bio *bio)
523 {
524 #if defined(HAVE_REQ_OP_SECURE_ERASE)
525 	return (bio_op(bio) == REQ_OP_SECURE_ERASE);
526 #elif defined(REQ_SECURE)
527 	return (bio->bi_rw & REQ_SECURE);
528 #else
529 	return (0);
530 #endif
531 }
532 
533 /*
534  * 2.6.33 API change
535  * Discard granularity and alignment restrictions may now be set.  For
536  * older kernels which do not support this it is safe to skip it.
537  */
538 static inline void
blk_queue_discard_granularity(struct request_queue * q,unsigned int dg)539 blk_queue_discard_granularity(struct request_queue *q, unsigned int dg)
540 {
541 	q->limits.discard_granularity = dg;
542 }
543 
544 /*
545  * 5.19 API,
546  *   bdev_max_discard_sectors()
547  *
548  * 2.6.32 API,
549  *   blk_queue_discard()
550  */
551 static inline boolean_t
bdev_discard_supported(struct block_device * bdev)552 bdev_discard_supported(struct block_device *bdev)
553 {
554 #if defined(HAVE_BDEV_MAX_DISCARD_SECTORS)
555 	return (!!bdev_max_discard_sectors(bdev));
556 #elif defined(HAVE_BLK_QUEUE_DISCARD)
557 	return (!!blk_queue_discard(bdev_get_queue(bdev)));
558 #else
559 #error "Unsupported kernel"
560 #endif
561 }
562 
563 /*
564  * 5.19 API,
565  *   bdev_max_secure_erase_sectors()
566  *
567  * 4.8 API,
568  *   blk_queue_secure_erase()
569  *
570  * 2.6.36 - 4.7 API,
571  *   blk_queue_secdiscard()
572  */
573 static inline boolean_t
bdev_secure_discard_supported(struct block_device * bdev)574 bdev_secure_discard_supported(struct block_device *bdev)
575 {
576 #if defined(HAVE_BDEV_MAX_SECURE_ERASE_SECTORS)
577 	return (!!bdev_max_secure_erase_sectors(bdev));
578 #elif defined(HAVE_BLK_QUEUE_SECURE_ERASE)
579 	return (!!blk_queue_secure_erase(bdev_get_queue(bdev)));
580 #elif defined(HAVE_BLK_QUEUE_SECDISCARD)
581 	return (!!blk_queue_secdiscard(bdev_get_queue(bdev)));
582 #else
583 #error "Unsupported kernel"
584 #endif
585 }
586 
587 /*
588  * A common holder for vdev_bdev_open() is used to relax the exclusive open
589  * semantics slightly.  Internal vdev disk callers may pass VDEV_HOLDER to
590  * allow them to open the device multiple times.  Other kernel callers and
591  * user space processes which don't pass this value will get EBUSY.  This is
592  * currently required for the correct operation of hot spares.
593  */
594 #define	VDEV_HOLDER			((void *)0x2401de7)
595 
596 static inline unsigned long
blk_generic_start_io_acct(struct request_queue * q,struct gendisk * disk,int rw,struct bio * bio)597 blk_generic_start_io_acct(struct request_queue *q __attribute__((unused)),
598     struct gendisk *disk __attribute__((unused)),
599     int rw __attribute__((unused)), struct bio *bio)
600 {
601 #if defined(HAVE_BDEV_IO_ACCT_63)
602 	return (bdev_start_io_acct(bio->bi_bdev, bio_op(bio),
603 	    jiffies));
604 #elif defined(HAVE_BDEV_IO_ACCT_OLD)
605 	return (bdev_start_io_acct(bio->bi_bdev, bio_sectors(bio),
606 	    bio_op(bio), jiffies));
607 #elif defined(HAVE_DISK_IO_ACCT)
608 	return (disk_start_io_acct(disk, bio_sectors(bio), bio_op(bio)));
609 #elif defined(HAVE_BIO_IO_ACCT)
610 	return (bio_start_io_acct(bio));
611 #elif defined(HAVE_GENERIC_IO_ACCT_3ARG)
612 	unsigned long start_time = jiffies;
613 	generic_start_io_acct(rw, bio_sectors(bio), &disk->part0);
614 	return (start_time);
615 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
616 	unsigned long start_time = jiffies;
617 	generic_start_io_acct(q, rw, bio_sectors(bio), &disk->part0);
618 	return (start_time);
619 #else
620 	/* Unsupported */
621 	return (0);
622 #endif
623 }
624 
625 static inline void
blk_generic_end_io_acct(struct request_queue * q,struct gendisk * disk,int rw,struct bio * bio,unsigned long start_time)626 blk_generic_end_io_acct(struct request_queue *q __attribute__((unused)),
627     struct gendisk *disk __attribute__((unused)),
628     int rw __attribute__((unused)), struct bio *bio, unsigned long start_time)
629 {
630 #if defined(HAVE_BDEV_IO_ACCT_63)
631 	bdev_end_io_acct(bio->bi_bdev, bio_op(bio), bio_sectors(bio),
632 	    start_time);
633 #elif defined(HAVE_BDEV_IO_ACCT_OLD)
634 	bdev_end_io_acct(bio->bi_bdev, bio_op(bio), start_time);
635 #elif defined(HAVE_DISK_IO_ACCT)
636 	disk_end_io_acct(disk, bio_op(bio), start_time);
637 #elif defined(HAVE_BIO_IO_ACCT)
638 	bio_end_io_acct(bio, start_time);
639 #elif defined(HAVE_GENERIC_IO_ACCT_3ARG)
640 	generic_end_io_acct(rw, &disk->part0, start_time);
641 #elif defined(HAVE_GENERIC_IO_ACCT_4ARG)
642 	generic_end_io_acct(q, rw, &disk->part0, start_time);
643 #endif
644 }
645 
646 #ifndef HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS
647 static inline struct request_queue *
blk_generic_alloc_queue(make_request_fn make_request,int node_id)648 blk_generic_alloc_queue(make_request_fn make_request, int node_id)
649 {
650 #if defined(HAVE_BLK_ALLOC_QUEUE_REQUEST_FN)
651 	return (blk_alloc_queue(make_request, node_id));
652 #elif defined(HAVE_BLK_ALLOC_QUEUE_REQUEST_FN_RH)
653 	return (blk_alloc_queue_rh(make_request, node_id));
654 #else
655 	struct request_queue *q = blk_alloc_queue(GFP_KERNEL);
656 	if (q != NULL)
657 		blk_queue_make_request(q, make_request);
658 
659 	return (q);
660 #endif
661 }
662 #endif /* !HAVE_SUBMIT_BIO_IN_BLOCK_DEVICE_OPERATIONS */
663 
664 #endif /* _ZFS_BLKDEV_H */
665