Merge tag 'pull-set_blocksize' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-block.git] / include / linux / blkdev.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Portions Copyright (C) 1992 Drew Eckhardt
4  */
5 #ifndef _LINUX_BLKDEV_H
6 #define _LINUX_BLKDEV_H
7
8 #include <linux/types.h>
9 #include <linux/blk_types.h>
10 #include <linux/device.h>
11 #include <linux/list.h>
12 #include <linux/llist.h>
13 #include <linux/minmax.h>
14 #include <linux/timer.h>
15 #include <linux/workqueue.h>
16 #include <linux/wait.h>
17 #include <linux/bio.h>
18 #include <linux/gfp.h>
19 #include <linux/kdev_t.h>
20 #include <linux/rcupdate.h>
21 #include <linux/percpu-refcount.h>
22 #include <linux/blkzoned.h>
23 #include <linux/sched.h>
24 #include <linux/sbitmap.h>
25 #include <linux/uuid.h>
26 #include <linux/xarray.h>
27 #include <linux/file.h>
28
29 struct module;
30 struct request_queue;
31 struct elevator_queue;
32 struct blk_trace;
33 struct request;
34 struct sg_io_hdr;
35 struct blkcg_gq;
36 struct blk_flush_queue;
37 struct kiocb;
38 struct pr_ops;
39 struct rq_qos;
40 struct blk_queue_stats;
41 struct blk_stat_callback;
42 struct blk_crypto_profile;
43
44 extern const struct device_type disk_type;
45 extern const struct device_type part_type;
46 extern const struct class block_class;
47
48 /*
49  * Maximum number of blkcg policies allowed to be registered concurrently.
50  * Defined here to simplify include dependency.
51  */
52 #define BLKCG_MAX_POLS          6
53
54 #define DISK_MAX_PARTS                  256
55 #define DISK_NAME_LEN                   32
56
57 #define PARTITION_META_INFO_VOLNAMELTH  64
58 /*
59  * Enough for the string representation of any kind of UUID plus NULL.
60  * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
61  */
62 #define PARTITION_META_INFO_UUIDLTH     (UUID_STRING_LEN + 1)
63
64 struct partition_meta_info {
65         char uuid[PARTITION_META_INFO_UUIDLTH];
66         u8 volname[PARTITION_META_INFO_VOLNAMELTH];
67 };
68
69 /**
70  * DOC: genhd capability flags
71  *
72  * ``GENHD_FL_REMOVABLE``: indicates that the block device gives access to
73  * removable media.  When set, the device remains present even when media is not
74  * inserted.  Shall not be set for devices which are removed entirely when the
75  * media is removed.
76  *
77  * ``GENHD_FL_HIDDEN``: the block device is hidden; it doesn't produce events,
78  * doesn't appear in sysfs, and can't be opened from userspace or using
79  * blkdev_get*. Used for the underlying components of multipath devices.
80  *
81  * ``GENHD_FL_NO_PART``: partition support is disabled.  The kernel will not
82  * scan for partitions from add_disk, and users can't add partitions manually.
83  *
84  */
85 enum {
86         GENHD_FL_REMOVABLE                      = 1 << 0,
87         GENHD_FL_HIDDEN                         = 1 << 1,
88         GENHD_FL_NO_PART                        = 1 << 2,
89 };
90
91 enum {
92         DISK_EVENT_MEDIA_CHANGE                 = 1 << 0, /* media changed */
93         DISK_EVENT_EJECT_REQUEST                = 1 << 1, /* eject requested */
94 };
95
96 enum {
97         /* Poll even if events_poll_msecs is unset */
98         DISK_EVENT_FLAG_POLL                    = 1 << 0,
99         /* Forward events to udev */
100         DISK_EVENT_FLAG_UEVENT                  = 1 << 1,
101         /* Block event polling when open for exclusive write */
102         DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE     = 1 << 2,
103 };
104
105 struct disk_events;
106 struct badblocks;
107
108 struct blk_integrity {
109         const struct blk_integrity_profile      *profile;
110         unsigned char                           flags;
111         unsigned char                           tuple_size;
112         unsigned char                           pi_offset;
113         unsigned char                           interval_exp;
114         unsigned char                           tag_size;
115 };
116
117 typedef unsigned int __bitwise blk_mode_t;
118
119 /* open for reading */
120 #define BLK_OPEN_READ           ((__force blk_mode_t)(1 << 0))
121 /* open for writing */
122 #define BLK_OPEN_WRITE          ((__force blk_mode_t)(1 << 1))
123 /* open exclusively (vs other exclusive openers */
124 #define BLK_OPEN_EXCL           ((__force blk_mode_t)(1 << 2))
125 /* opened with O_NDELAY */
126 #define BLK_OPEN_NDELAY         ((__force blk_mode_t)(1 << 3))
127 /* open for "writes" only for ioctls (specialy hack for floppy.c) */
128 #define BLK_OPEN_WRITE_IOCTL    ((__force blk_mode_t)(1 << 4))
129 /* open is exclusive wrt all other BLK_OPEN_WRITE opens to the device */
130 #define BLK_OPEN_RESTRICT_WRITES        ((__force blk_mode_t)(1 << 5))
131 /* return partition scanning errors */
132 #define BLK_OPEN_STRICT_SCAN    ((__force blk_mode_t)(1 << 6))
133
134 struct gendisk {
135         /*
136          * major/first_minor/minors should not be set by any new driver, the
137          * block core will take care of allocating them automatically.
138          */
139         int major;
140         int first_minor;
141         int minors;
142
143         char disk_name[DISK_NAME_LEN];  /* name of major driver */
144
145         unsigned short events;          /* supported events */
146         unsigned short event_flags;     /* flags related to event processing */
147
148         struct xarray part_tbl;
149         struct block_device *part0;
150
151         const struct block_device_operations *fops;
152         struct request_queue *queue;
153         void *private_data;
154
155         struct bio_set bio_split;
156
157         int flags;
158         unsigned long state;
159 #define GD_NEED_PART_SCAN               0
160 #define GD_READ_ONLY                    1
161 #define GD_DEAD                         2
162 #define GD_NATIVE_CAPACITY              3
163 #define GD_ADDED                        4
164 #define GD_SUPPRESS_PART_SCAN           5
165 #define GD_OWNS_QUEUE                   6
166
167         struct mutex open_mutex;        /* open/close mutex */
168         unsigned open_partitions;       /* number of open partitions */
169
170         struct backing_dev_info *bdi;
171         struct kobject queue_kobj;      /* the queue/ directory */
172         struct kobject *slave_dir;
173 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
174         struct list_head slave_bdevs;
175 #endif
176         struct timer_rand_state *random;
177         atomic_t sync_io;               /* RAID */
178         struct disk_events *ev;
179
180 #ifdef CONFIG_BLK_DEV_ZONED
181         /*
182          * Zoned block device information. Reads of this information must be
183          * protected with blk_queue_enter() / blk_queue_exit(). Modifying this
184          * information is only allowed while no requests are being processed.
185          * See also blk_mq_freeze_queue() and blk_mq_unfreeze_queue().
186          */
187         unsigned int            nr_zones;
188         unsigned int            zone_capacity;
189         unsigned long           *conv_zones_bitmap;
190         unsigned int            zone_wplugs_hash_bits;
191         spinlock_t              zone_wplugs_lock;
192         struct mempool_s        *zone_wplugs_pool;
193         struct hlist_head       *zone_wplugs_hash;
194         struct list_head        zone_wplugs_err_list;
195         struct work_struct      zone_wplugs_work;
196         struct workqueue_struct *zone_wplugs_wq;
197 #endif /* CONFIG_BLK_DEV_ZONED */
198
199 #if IS_ENABLED(CONFIG_CDROM)
200         struct cdrom_device_info *cdi;
201 #endif
202         int node_id;
203         struct badblocks *bb;
204         struct lockdep_map lockdep_map;
205         u64 diskseq;
206         blk_mode_t open_mode;
207
208         /*
209          * Independent sector access ranges. This is always NULL for
210          * devices that do not have multiple independent access ranges.
211          */
212         struct blk_independent_access_ranges *ia_ranges;
213 };
214
215 static inline bool disk_live(struct gendisk *disk)
216 {
217         return !inode_unhashed(disk->part0->bd_inode);
218 }
219
220 /**
221  * disk_openers - returns how many openers are there for a disk
222  * @disk: disk to check
223  *
224  * This returns the number of openers for a disk.  Note that this value is only
225  * stable if disk->open_mutex is held.
226  *
227  * Note: Due to a quirk in the block layer open code, each open partition is
228  * only counted once even if there are multiple openers.
229  */
230 static inline unsigned int disk_openers(struct gendisk *disk)
231 {
232         return atomic_read(&disk->part0->bd_openers);
233 }
234
235 /**
236  * disk_has_partscan - return %true if partition scanning is enabled on a disk
237  * @disk: disk to check
238  *
239  * Returns %true if partitions scanning is enabled for @disk, or %false if
240  * partition scanning is disabled either permanently or temporarily.
241  */
242 static inline bool disk_has_partscan(struct gendisk *disk)
243 {
244         return !(disk->flags & (GENHD_FL_NO_PART | GENHD_FL_HIDDEN)) &&
245                 !test_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
246 }
247
248 /*
249  * The gendisk is refcounted by the part0 block_device, and the bd_device
250  * therein is also used for device model presentation in sysfs.
251  */
252 #define dev_to_disk(device) \
253         (dev_to_bdev(device)->bd_disk)
254 #define disk_to_dev(disk) \
255         (&((disk)->part0->bd_device))
256
257 #if IS_REACHABLE(CONFIG_CDROM)
258 #define disk_to_cdi(disk)       ((disk)->cdi)
259 #else
260 #define disk_to_cdi(disk)       NULL
261 #endif
262
263 static inline dev_t disk_devt(struct gendisk *disk)
264 {
265         return MKDEV(disk->major, disk->first_minor);
266 }
267
268 static inline int blk_validate_block_size(unsigned long bsize)
269 {
270         if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
271                 return -EINVAL;
272
273         return 0;
274 }
275
276 static inline bool blk_op_is_passthrough(blk_opf_t op)
277 {
278         op &= REQ_OP_MASK;
279         return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
280 }
281
282 /*
283  * BLK_BOUNCE_NONE:     never bounce (default)
284  * BLK_BOUNCE_HIGH:     bounce all highmem pages
285  */
286 enum blk_bounce {
287         BLK_BOUNCE_NONE,
288         BLK_BOUNCE_HIGH,
289 };
290
291 struct queue_limits {
292         enum blk_bounce         bounce;
293         unsigned long           seg_boundary_mask;
294         unsigned long           virt_boundary_mask;
295
296         unsigned int            max_hw_sectors;
297         unsigned int            max_dev_sectors;
298         unsigned int            chunk_sectors;
299         unsigned int            max_sectors;
300         unsigned int            max_user_sectors;
301         unsigned int            max_segment_size;
302         unsigned int            physical_block_size;
303         unsigned int            logical_block_size;
304         unsigned int            alignment_offset;
305         unsigned int            io_min;
306         unsigned int            io_opt;
307         unsigned int            max_discard_sectors;
308         unsigned int            max_hw_discard_sectors;
309         unsigned int            max_user_discard_sectors;
310         unsigned int            max_secure_erase_sectors;
311         unsigned int            max_write_zeroes_sectors;
312         unsigned int            max_zone_append_sectors;
313         unsigned int            discard_granularity;
314         unsigned int            discard_alignment;
315         unsigned int            zone_write_granularity;
316
317         unsigned short          max_segments;
318         unsigned short          max_integrity_segments;
319         unsigned short          max_discard_segments;
320
321         unsigned char           misaligned;
322         unsigned char           discard_misaligned;
323         unsigned char           raid_partial_stripes_expensive;
324         bool                    zoned;
325         unsigned int            max_open_zones;
326         unsigned int            max_active_zones;
327
328         /*
329          * Drivers that set dma_alignment to less than 511 must be prepared to
330          * handle individual bvec's that are not a multiple of a SECTOR_SIZE
331          * due to possible offsets.
332          */
333         unsigned int            dma_alignment;
334 };
335
336 typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
337                                void *data);
338
339 void disk_set_zoned(struct gendisk *disk);
340
341 #define BLK_ALL_ZONES  ((unsigned int)-1)
342 int blkdev_report_zones(struct block_device *bdev, sector_t sector,
343                 unsigned int nr_zones, report_zones_cb cb, void *data);
344 int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op,
345                 sector_t sectors, sector_t nr_sectors);
346 int blk_revalidate_disk_zones(struct gendisk *disk);
347
348 /*
349  * Independent access ranges: struct blk_independent_access_range describes
350  * a range of contiguous sectors that can be accessed using device command
351  * execution resources that are independent from the resources used for
352  * other access ranges. This is typically found with single-LUN multi-actuator
353  * HDDs where each access range is served by a different set of heads.
354  * The set of independent ranges supported by the device is defined using
355  * struct blk_independent_access_ranges. The independent ranges must not overlap
356  * and must include all sectors within the disk capacity (no sector holes
357  * allowed).
358  * For a device with multiple ranges, requests targeting sectors in different
359  * ranges can be executed in parallel. A request can straddle an access range
360  * boundary.
361  */
362 struct blk_independent_access_range {
363         struct kobject          kobj;
364         sector_t                sector;
365         sector_t                nr_sectors;
366 };
367
368 struct blk_independent_access_ranges {
369         struct kobject                          kobj;
370         bool                                    sysfs_registered;
371         unsigned int                            nr_ia_ranges;
372         struct blk_independent_access_range     ia_range[];
373 };
374
375 struct request_queue {
376         /*
377          * The queue owner gets to use this for whatever they like.
378          * ll_rw_blk doesn't touch it.
379          */
380         void                    *queuedata;
381
382         struct elevator_queue   *elevator;
383
384         const struct blk_mq_ops *mq_ops;
385
386         /* sw queues */
387         struct blk_mq_ctx __percpu      *queue_ctx;
388
389         /*
390          * various queue flags, see QUEUE_* below
391          */
392         unsigned long           queue_flags;
393
394         unsigned int            rq_timeout;
395
396         unsigned int            queue_depth;
397
398         refcount_t              refs;
399
400         /* hw dispatch queues */
401         unsigned int            nr_hw_queues;
402         struct xarray           hctx_table;
403
404         struct percpu_ref       q_usage_counter;
405
406         struct request          *last_merge;
407
408         spinlock_t              queue_lock;
409
410         int                     quiesce_depth;
411
412         struct gendisk          *disk;
413
414         /*
415          * mq queue kobject
416          */
417         struct kobject *mq_kobj;
418
419         struct queue_limits     limits;
420
421 #ifdef  CONFIG_BLK_DEV_INTEGRITY
422         struct blk_integrity integrity;
423 #endif  /* CONFIG_BLK_DEV_INTEGRITY */
424
425 #ifdef CONFIG_PM
426         struct device           *dev;
427         enum rpm_status         rpm_status;
428 #endif
429
430         /*
431          * Number of contexts that have called blk_set_pm_only(). If this
432          * counter is above zero then only RQF_PM requests are processed.
433          */
434         atomic_t                pm_only;
435
436         struct blk_queue_stats  *stats;
437         struct rq_qos           *rq_qos;
438         struct mutex            rq_qos_mutex;
439
440         /*
441          * ida allocated id for this queue.  Used to index queues from
442          * ioctx.
443          */
444         int                     id;
445
446         unsigned int            dma_pad_mask;
447
448         /*
449          * queue settings
450          */
451         unsigned long           nr_requests;    /* Max # of requests */
452
453 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
454         struct blk_crypto_profile *crypto_profile;
455         struct kobject *crypto_kobject;
456 #endif
457
458         struct timer_list       timeout;
459         struct work_struct      timeout_work;
460
461         atomic_t                nr_active_requests_shared_tags;
462
463         struct blk_mq_tags      *sched_shared_tags;
464
465         struct list_head        icq_list;
466 #ifdef CONFIG_BLK_CGROUP
467         DECLARE_BITMAP          (blkcg_pols, BLKCG_MAX_POLS);
468         struct blkcg_gq         *root_blkg;
469         struct list_head        blkg_list;
470         struct mutex            blkcg_mutex;
471 #endif
472
473         int                     node;
474
475         spinlock_t              requeue_lock;
476         struct list_head        requeue_list;
477         struct delayed_work     requeue_work;
478
479 #ifdef CONFIG_BLK_DEV_IO_TRACE
480         struct blk_trace __rcu  *blk_trace;
481 #endif
482         /*
483          * for flush operations
484          */
485         struct blk_flush_queue  *fq;
486         struct list_head        flush_list;
487
488         struct mutex            sysfs_lock;
489         struct mutex            sysfs_dir_lock;
490         struct mutex            limits_lock;
491
492         /*
493          * for reusing dead hctx instance in case of updating
494          * nr_hw_queues
495          */
496         struct list_head        unused_hctx_list;
497         spinlock_t              unused_hctx_lock;
498
499         int                     mq_freeze_depth;
500
501 #ifdef CONFIG_BLK_DEV_THROTTLING
502         /* Throttle data */
503         struct throtl_data *td;
504 #endif
505         struct rcu_head         rcu_head;
506         wait_queue_head_t       mq_freeze_wq;
507         /*
508          * Protect concurrent access to q_usage_counter by
509          * percpu_ref_kill() and percpu_ref_reinit().
510          */
511         struct mutex            mq_freeze_lock;
512
513         struct blk_mq_tag_set   *tag_set;
514         struct list_head        tag_set_list;
515
516         struct dentry           *debugfs_dir;
517         struct dentry           *sched_debugfs_dir;
518         struct dentry           *rqos_debugfs_dir;
519         /*
520          * Serializes all debugfs metadata operations using the above dentries.
521          */
522         struct mutex            debugfs_mutex;
523
524         bool                    mq_sysfs_init_done;
525 };
526
527 /* Keep blk_queue_flag_name[] in sync with the definitions below */
528 #define QUEUE_FLAG_STOPPED      0       /* queue is stopped */
529 #define QUEUE_FLAG_DYING        1       /* queue being torn down */
530 #define QUEUE_FLAG_NOMERGES     3       /* disable merge attempts */
531 #define QUEUE_FLAG_SAME_COMP    4       /* complete on same CPU-group */
532 #define QUEUE_FLAG_FAIL_IO      5       /* fake timeout */
533 #define QUEUE_FLAG_NONROT       6       /* non-rotational device (SSD) */
534 #define QUEUE_FLAG_VIRT         QUEUE_FLAG_NONROT /* paravirt device */
535 #define QUEUE_FLAG_IO_STAT      7       /* do disk/partitions IO accounting */
536 #define QUEUE_FLAG_NOXMERGES    9       /* No extended merges */
537 #define QUEUE_FLAG_ADD_RANDOM   10      /* Contributes to random pool */
538 #define QUEUE_FLAG_SYNCHRONOUS  11      /* always completes in submit context */
539 #define QUEUE_FLAG_SAME_FORCE   12      /* force complete on same CPU */
540 #define QUEUE_FLAG_HW_WC        13      /* Write back caching supported */
541 #define QUEUE_FLAG_INIT_DONE    14      /* queue is initialized */
542 #define QUEUE_FLAG_STABLE_WRITES 15     /* don't modify blks until WB is done */
543 #define QUEUE_FLAG_POLL         16      /* IO polling enabled if set */
544 #define QUEUE_FLAG_WC           17      /* Write back caching */
545 #define QUEUE_FLAG_FUA          18      /* device supports FUA writes */
546 #define QUEUE_FLAG_DAX          19      /* device supports DAX */
547 #define QUEUE_FLAG_STATS        20      /* track IO start and completion times */
548 #define QUEUE_FLAG_REGISTERED   22      /* queue has been registered to a disk */
549 #define QUEUE_FLAG_QUIESCED     24      /* queue has been quiesced */
550 #define QUEUE_FLAG_PCI_P2PDMA   25      /* device supports PCI p2p requests */
551 #define QUEUE_FLAG_ZONE_RESETALL 26     /* supports Zone Reset All */
552 #define QUEUE_FLAG_RQ_ALLOC_TIME 27     /* record rq->alloc_time_ns */
553 #define QUEUE_FLAG_HCTX_ACTIVE  28      /* at least one blk-mq hctx is active */
554 #define QUEUE_FLAG_NOWAIT       29      /* device supports NOWAIT */
555 #define QUEUE_FLAG_SQ_SCHED     30      /* single queue style io dispatch */
556 #define QUEUE_FLAG_SKIP_TAGSET_QUIESCE  31 /* quiesce_tagset skip the queue*/
557
558 #define QUEUE_FLAG_MQ_DEFAULT   ((1UL << QUEUE_FLAG_IO_STAT) |          \
559                                  (1UL << QUEUE_FLAG_SAME_COMP) |        \
560                                  (1UL << QUEUE_FLAG_NOWAIT))
561
562 void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
563 void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
564 bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q);
565
566 #define blk_queue_stopped(q)    test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags)
567 #define blk_queue_dying(q)      test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
568 #define blk_queue_init_done(q)  test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
569 #define blk_queue_nomerges(q)   test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
570 #define blk_queue_noxmerges(q)  \
571         test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
572 #define blk_queue_nonrot(q)     test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags)
573 #define blk_queue_stable_writes(q) \
574         test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags)
575 #define blk_queue_io_stat(q)    test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags)
576 #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags)
577 #define blk_queue_zone_resetall(q)      \
578         test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags)
579 #define blk_queue_dax(q)        test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags)
580 #define blk_queue_pci_p2pdma(q) \
581         test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags)
582 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
583 #define blk_queue_rq_alloc_time(q)      \
584         test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
585 #else
586 #define blk_queue_rq_alloc_time(q)      false
587 #endif
588
589 #define blk_noretry_request(rq) \
590         ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
591                              REQ_FAILFAST_DRIVER))
592 #define blk_queue_quiesced(q)   test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
593 #define blk_queue_pm_only(q)    atomic_read(&(q)->pm_only)
594 #define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
595 #define blk_queue_sq_sched(q)   test_bit(QUEUE_FLAG_SQ_SCHED, &(q)->queue_flags)
596 #define blk_queue_skip_tagset_quiesce(q) \
597         test_bit(QUEUE_FLAG_SKIP_TAGSET_QUIESCE, &(q)->queue_flags)
598
599 extern void blk_set_pm_only(struct request_queue *q);
600 extern void blk_clear_pm_only(struct request_queue *q);
601
602 #define list_entry_rq(ptr)      list_entry((ptr), struct request, queuelist)
603
604 #define dma_map_bvec(dev, bv, dir, attrs) \
605         dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
606         (dir), (attrs))
607
608 static inline bool queue_is_mq(struct request_queue *q)
609 {
610         return q->mq_ops;
611 }
612
613 #ifdef CONFIG_PM
614 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
615 {
616         return q->rpm_status;
617 }
618 #else
619 static inline enum rpm_status queue_rpm_status(struct request_queue *q)
620 {
621         return RPM_ACTIVE;
622 }
623 #endif
624
625 static inline bool blk_queue_is_zoned(struct request_queue *q)
626 {
627         return IS_ENABLED(CONFIG_BLK_DEV_ZONED) && q->limits.zoned;
628 }
629
630 #ifdef CONFIG_BLK_DEV_ZONED
631 unsigned int bdev_nr_zones(struct block_device *bdev);
632
633 static inline unsigned int disk_nr_zones(struct gendisk *disk)
634 {
635         return blk_queue_is_zoned(disk->queue) ? disk->nr_zones : 0;
636 }
637
638 static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector)
639 {
640         if (!blk_queue_is_zoned(disk->queue))
641                 return 0;
642         return sector >> ilog2(disk->queue->limits.chunk_sectors);
643 }
644
645 static inline void disk_set_max_open_zones(struct gendisk *disk,
646                 unsigned int max_open_zones)
647 {
648         disk->queue->limits.max_open_zones = max_open_zones;
649 }
650
651 static inline void disk_set_max_active_zones(struct gendisk *disk,
652                 unsigned int max_active_zones)
653 {
654         disk->queue->limits.max_active_zones = max_active_zones;
655 }
656
657 static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
658 {
659         return bdev->bd_disk->queue->limits.max_open_zones;
660 }
661
662 static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
663 {
664         return bdev->bd_disk->queue->limits.max_active_zones;
665 }
666
667 bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs);
668 #else /* CONFIG_BLK_DEV_ZONED */
669 static inline unsigned int bdev_nr_zones(struct block_device *bdev)
670 {
671         return 0;
672 }
673
674 static inline unsigned int disk_nr_zones(struct gendisk *disk)
675 {
676         return 0;
677 }
678 static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector)
679 {
680         return 0;
681 }
682 static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
683 {
684         return 0;
685 }
686
687 static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
688 {
689         return 0;
690 }
691 static inline bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs)
692 {
693         return false;
694 }
695 #endif /* CONFIG_BLK_DEV_ZONED */
696
697 static inline unsigned int blk_queue_depth(struct request_queue *q)
698 {
699         if (q->queue_depth)
700                 return q->queue_depth;
701
702         return q->nr_requests;
703 }
704
705 /*
706  * default timeout for SG_IO if none specified
707  */
708 #define BLK_DEFAULT_SG_TIMEOUT  (60 * HZ)
709 #define BLK_MIN_SG_TIMEOUT      (7 * HZ)
710
711 /* This should not be used directly - use rq_for_each_segment */
712 #define for_each_bio(_bio)              \
713         for (; _bio; _bio = _bio->bi_next)
714
715 int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
716                                  const struct attribute_group **groups);
717 static inline int __must_check add_disk(struct gendisk *disk)
718 {
719         return device_add_disk(NULL, disk, NULL);
720 }
721 void del_gendisk(struct gendisk *gp);
722 void invalidate_disk(struct gendisk *disk);
723 void set_disk_ro(struct gendisk *disk, bool read_only);
724 void disk_uevent(struct gendisk *disk, enum kobject_action action);
725
726 static inline int get_disk_ro(struct gendisk *disk)
727 {
728         return disk->part0->bd_read_only ||
729                 test_bit(GD_READ_ONLY, &disk->state);
730 }
731
732 static inline int bdev_read_only(struct block_device *bdev)
733 {
734         return bdev->bd_read_only || get_disk_ro(bdev->bd_disk);
735 }
736
737 bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
738 void disk_force_media_change(struct gendisk *disk);
739 void bdev_mark_dead(struct block_device *bdev, bool surprise);
740
741 void add_disk_randomness(struct gendisk *disk) __latent_entropy;
742 void rand_initialize_disk(struct gendisk *disk);
743
744 static inline sector_t get_start_sect(struct block_device *bdev)
745 {
746         return bdev->bd_start_sect;
747 }
748
749 static inline sector_t bdev_nr_sectors(struct block_device *bdev)
750 {
751         return bdev->bd_nr_sectors;
752 }
753
754 static inline loff_t bdev_nr_bytes(struct block_device *bdev)
755 {
756         return (loff_t)bdev_nr_sectors(bdev) << SECTOR_SHIFT;
757 }
758
759 static inline sector_t get_capacity(struct gendisk *disk)
760 {
761         return bdev_nr_sectors(disk->part0);
762 }
763
764 static inline u64 sb_bdev_nr_blocks(struct super_block *sb)
765 {
766         return bdev_nr_sectors(sb->s_bdev) >>
767                 (sb->s_blocksize_bits - SECTOR_SHIFT);
768 }
769
770 int bdev_disk_changed(struct gendisk *disk, bool invalidate);
771
772 void put_disk(struct gendisk *disk);
773 struct gendisk *__blk_alloc_disk(struct queue_limits *lim, int node,
774                 struct lock_class_key *lkclass);
775
776 /**
777  * blk_alloc_disk - allocate a gendisk structure
778  * @lim: queue limits to be used for this disk.
779  * @node_id: numa node to allocate on
780  *
781  * Allocate and pre-initialize a gendisk structure for use with BIO based
782  * drivers.
783  *
784  * Returns an ERR_PTR on error, else the allocated disk.
785  *
786  * Context: can sleep
787  */
788 #define blk_alloc_disk(lim, node_id)                                    \
789 ({                                                                      \
790         static struct lock_class_key __key;                             \
791                                                                         \
792         __blk_alloc_disk(lim, node_id, &__key);                         \
793 })
794
795 int __register_blkdev(unsigned int major, const char *name,
796                 void (*probe)(dev_t devt));
797 #define register_blkdev(major, name) \
798         __register_blkdev(major, name, NULL)
799 void unregister_blkdev(unsigned int major, const char *name);
800
801 bool disk_check_media_change(struct gendisk *disk);
802 void set_capacity(struct gendisk *disk, sector_t size);
803
804 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
805 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
806 void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
807 #else
808 static inline int bd_link_disk_holder(struct block_device *bdev,
809                                       struct gendisk *disk)
810 {
811         return 0;
812 }
813 static inline void bd_unlink_disk_holder(struct block_device *bdev,
814                                          struct gendisk *disk)
815 {
816 }
817 #endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */
818
819 dev_t part_devt(struct gendisk *disk, u8 partno);
820 void inc_diskseq(struct gendisk *disk);
821 void blk_request_module(dev_t devt);
822
823 extern int blk_register_queue(struct gendisk *disk);
824 extern void blk_unregister_queue(struct gendisk *disk);
825 void submit_bio_noacct(struct bio *bio);
826 struct bio *bio_split_to_limits(struct bio *bio);
827
828 extern int blk_lld_busy(struct request_queue *q);
829 extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
830 extern void blk_queue_exit(struct request_queue *q);
831 extern void blk_sync_queue(struct request_queue *q);
832
833 /* Helper to convert REQ_OP_XXX to its string format XXX */
834 extern const char *blk_op_str(enum req_op op);
835
836 int blk_status_to_errno(blk_status_t status);
837 blk_status_t errno_to_blk_status(int errno);
838 const char *blk_status_to_str(blk_status_t status);
839
840 /* only poll the hardware once, don't continue until a completion was found */
841 #define BLK_POLL_ONESHOT                (1 << 0)
842 int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags);
843 int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
844                         unsigned int flags);
845
846 static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
847 {
848         return bdev->bd_queue;  /* this is never NULL */
849 }
850
851 /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
852 const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
853
854 static inline unsigned int bio_zone_no(struct bio *bio)
855 {
856         return disk_zone_no(bio->bi_bdev->bd_disk, bio->bi_iter.bi_sector);
857 }
858
859 static inline bool bio_straddles_zones(struct bio *bio)
860 {
861         return bio_sectors(bio) &&
862                 bio_zone_no(bio) !=
863                 disk_zone_no(bio->bi_bdev->bd_disk, bio_end_sector(bio) - 1);
864 }
865
866 /*
867  * Return how much of the chunk is left to be used for I/O at a given offset.
868  */
869 static inline unsigned int blk_chunk_sectors_left(sector_t offset,
870                 unsigned int chunk_sectors)
871 {
872         if (unlikely(!is_power_of_2(chunk_sectors)))
873                 return chunk_sectors - sector_div(offset, chunk_sectors);
874         return chunk_sectors - (offset & (chunk_sectors - 1));
875 }
876
877 /**
878  * queue_limits_start_update - start an atomic update of queue limits
879  * @q:          queue to update
880  *
881  * This functions starts an atomic update of the queue limits.  It takes a lock
882  * to prevent other updates and returns a snapshot of the current limits that
883  * the caller can modify.  The caller must call queue_limits_commit_update()
884  * to finish the update.
885  *
886  * Context: process context.  The caller must have frozen the queue or ensured
887  * that there is outstanding I/O by other means.
888  */
889 static inline struct queue_limits
890 queue_limits_start_update(struct request_queue *q)
891         __acquires(q->limits_lock)
892 {
893         mutex_lock(&q->limits_lock);
894         return q->limits;
895 }
896 int queue_limits_commit_update(struct request_queue *q,
897                 struct queue_limits *lim);
898 int queue_limits_set(struct request_queue *q, struct queue_limits *lim);
899
900 /**
901  * queue_limits_cancel_update - cancel an atomic update of queue limits
902  * @q:          queue to update
903  *
904  * This functions cancels an atomic update of the queue limits started by
905  * queue_limits_start_update() and should be used when an error occurs after
906  * starting update.
907  */
908 static inline void queue_limits_cancel_update(struct request_queue *q)
909 {
910         mutex_unlock(&q->limits_lock);
911 }
912
913 /*
914  * Access functions for manipulating queue properties
915  */
916 extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int);
917 void blk_queue_max_secure_erase_sectors(struct request_queue *q,
918                 unsigned int max_sectors);
919 extern void blk_queue_max_discard_sectors(struct request_queue *q,
920                 unsigned int max_discard_sectors);
921 extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q,
922                 unsigned int max_write_same_sectors);
923 extern void blk_queue_logical_block_size(struct request_queue *, unsigned int);
924 extern void blk_queue_max_zone_append_sectors(struct request_queue *q,
925                 unsigned int max_zone_append_sectors);
926 extern void blk_queue_physical_block_size(struct request_queue *, unsigned int);
927 void blk_queue_zone_write_granularity(struct request_queue *q,
928                                       unsigned int size);
929 extern void blk_queue_alignment_offset(struct request_queue *q,
930                                        unsigned int alignment);
931 void disk_update_readahead(struct gendisk *disk);
932 extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
933 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
934 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
935 extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
936 extern void blk_set_stacking_limits(struct queue_limits *lim);
937 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
938                             sector_t offset);
939 void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev,
940                 sector_t offset, const char *pfx);
941 extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int);
942 extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
943 extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua);
944
945 struct blk_independent_access_ranges *
946 disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges);
947 void disk_set_independent_access_ranges(struct gendisk *disk,
948                                 struct blk_independent_access_ranges *iars);
949
950 bool __must_check blk_get_queue(struct request_queue *);
951 extern void blk_put_queue(struct request_queue *);
952
953 void blk_mark_disk_dead(struct gendisk *disk);
954
955 #ifdef CONFIG_BLOCK
956 /*
957  * blk_plug permits building a queue of related requests by holding the I/O
958  * fragments for a short period. This allows merging of sequential requests
959  * into single larger request. As the requests are moved from a per-task list to
960  * the device's request_queue in a batch, this results in improved scalability
961  * as the lock contention for request_queue lock is reduced.
962  *
963  * It is ok not to disable preemption when adding the request to the plug list
964  * or when attempting a merge. For details, please see schedule() where
965  * blk_flush_plug() is called.
966  */
967 struct blk_plug {
968         struct request *mq_list; /* blk-mq requests */
969
970         /* if ios_left is > 1, we can batch tag/rq allocations */
971         struct request *cached_rq;
972         u64 cur_ktime;
973         unsigned short nr_ios;
974
975         unsigned short rq_count;
976
977         bool multiple_queues;
978         bool has_elevator;
979
980         struct list_head cb_list; /* md requires an unplug callback */
981 };
982
983 struct blk_plug_cb;
984 typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
985 struct blk_plug_cb {
986         struct list_head list;
987         blk_plug_cb_fn callback;
988         void *data;
989 };
990 extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
991                                              void *data, int size);
992 extern void blk_start_plug(struct blk_plug *);
993 extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short);
994 extern void blk_finish_plug(struct blk_plug *);
995
996 void __blk_flush_plug(struct blk_plug *plug, bool from_schedule);
997 static inline void blk_flush_plug(struct blk_plug *plug, bool async)
998 {
999         if (plug)
1000                 __blk_flush_plug(plug, async);
1001 }
1002
1003 /*
1004  * tsk == current here
1005  */
1006 static inline void blk_plug_invalidate_ts(struct task_struct *tsk)
1007 {
1008         struct blk_plug *plug = tsk->plug;
1009
1010         if (plug)
1011                 plug->cur_ktime = 0;
1012         current->flags &= ~PF_BLOCK_TS;
1013 }
1014
1015 int blkdev_issue_flush(struct block_device *bdev);
1016 long nr_blockdev_pages(void);
1017 #else /* CONFIG_BLOCK */
1018 struct blk_plug {
1019 };
1020
1021 static inline void blk_start_plug_nr_ios(struct blk_plug *plug,
1022                                          unsigned short nr_ios)
1023 {
1024 }
1025
1026 static inline void blk_start_plug(struct blk_plug *plug)
1027 {
1028 }
1029
1030 static inline void blk_finish_plug(struct blk_plug *plug)
1031 {
1032 }
1033
1034 static inline void blk_flush_plug(struct blk_plug *plug, bool async)
1035 {
1036 }
1037
1038 static inline void blk_plug_invalidate_ts(struct task_struct *tsk)
1039 {
1040 }
1041
1042 static inline int blkdev_issue_flush(struct block_device *bdev)
1043 {
1044         return 0;
1045 }
1046
1047 static inline long nr_blockdev_pages(void)
1048 {
1049         return 0;
1050 }
1051 #endif /* CONFIG_BLOCK */
1052
1053 extern void blk_io_schedule(void);
1054
1055 int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1056                 sector_t nr_sects, gfp_t gfp_mask);
1057 int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1058                 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop);
1059 int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
1060                 sector_t nr_sects, gfp_t gfp);
1061
1062 #define BLKDEV_ZERO_NOUNMAP     (1 << 0)  /* do not free blocks */
1063 #define BLKDEV_ZERO_NOFALLBACK  (1 << 1)  /* don't write explicit zeroes */
1064
1065 extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1066                 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1067                 unsigned flags);
1068 extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1069                 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1070
1071 static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1072                 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1073 {
1074         return blkdev_issue_discard(sb->s_bdev,
1075                                     block << (sb->s_blocksize_bits -
1076                                               SECTOR_SHIFT),
1077                                     nr_blocks << (sb->s_blocksize_bits -
1078                                                   SECTOR_SHIFT),
1079                                     gfp_mask);
1080 }
1081 static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1082                 sector_t nr_blocks, gfp_t gfp_mask)
1083 {
1084         return blkdev_issue_zeroout(sb->s_bdev,
1085                                     block << (sb->s_blocksize_bits -
1086                                               SECTOR_SHIFT),
1087                                     nr_blocks << (sb->s_blocksize_bits -
1088                                                   SECTOR_SHIFT),
1089                                     gfp_mask, 0);
1090 }
1091
1092 static inline bool bdev_is_partition(struct block_device *bdev)
1093 {
1094         return bdev->bd_partno;
1095 }
1096
1097 enum blk_default_limits {
1098         BLK_MAX_SEGMENTS        = 128,
1099         BLK_SAFE_MAX_SECTORS    = 255,
1100         BLK_MAX_SEGMENT_SIZE    = 65536,
1101         BLK_SEG_BOUNDARY_MASK   = 0xFFFFFFFFUL,
1102 };
1103
1104 /*
1105  * Default upper limit for the software max_sectors limit used for
1106  * regular file system I/O.  This can be increased through sysfs.
1107  *
1108  * Not to be confused with the max_hw_sector limit that is entirely
1109  * controlled by the driver, usually based on hardware limits.
1110  */
1111 #define BLK_DEF_MAX_SECTORS_CAP 2560u
1112
1113 static inline unsigned long queue_segment_boundary(const struct request_queue *q)
1114 {
1115         return q->limits.seg_boundary_mask;
1116 }
1117
1118 static inline unsigned long queue_virt_boundary(const struct request_queue *q)
1119 {
1120         return q->limits.virt_boundary_mask;
1121 }
1122
1123 static inline unsigned int queue_max_sectors(const struct request_queue *q)
1124 {
1125         return q->limits.max_sectors;
1126 }
1127
1128 static inline unsigned int queue_max_bytes(struct request_queue *q)
1129 {
1130         return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
1131 }
1132
1133 static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
1134 {
1135         return q->limits.max_hw_sectors;
1136 }
1137
1138 static inline unsigned short queue_max_segments(const struct request_queue *q)
1139 {
1140         return q->limits.max_segments;
1141 }
1142
1143 static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
1144 {
1145         return q->limits.max_discard_segments;
1146 }
1147
1148 static inline unsigned int queue_max_segment_size(const struct request_queue *q)
1149 {
1150         return q->limits.max_segment_size;
1151 }
1152
1153 static inline unsigned int queue_limits_max_zone_append_sectors(struct queue_limits *l)
1154 {
1155         unsigned int max_sectors = min(l->chunk_sectors, l->max_hw_sectors);
1156
1157         return min_not_zero(l->max_zone_append_sectors, max_sectors);
1158 }
1159
1160 static inline unsigned int queue_max_zone_append_sectors(struct request_queue *q)
1161 {
1162         if (!blk_queue_is_zoned(q))
1163                 return 0;
1164
1165         return queue_limits_max_zone_append_sectors(&q->limits);
1166 }
1167
1168 static inline bool queue_emulates_zone_append(struct request_queue *q)
1169 {
1170         return blk_queue_is_zoned(q) && !q->limits.max_zone_append_sectors;
1171 }
1172
1173 static inline bool bdev_emulates_zone_append(struct block_device *bdev)
1174 {
1175         return queue_emulates_zone_append(bdev_get_queue(bdev));
1176 }
1177
1178 static inline unsigned int
1179 bdev_max_zone_append_sectors(struct block_device *bdev)
1180 {
1181         return queue_max_zone_append_sectors(bdev_get_queue(bdev));
1182 }
1183
1184 static inline unsigned int bdev_max_segments(struct block_device *bdev)
1185 {
1186         return queue_max_segments(bdev_get_queue(bdev));
1187 }
1188
1189 static inline unsigned queue_logical_block_size(const struct request_queue *q)
1190 {
1191         int retval = 512;
1192
1193         if (q && q->limits.logical_block_size)
1194                 retval = q->limits.logical_block_size;
1195
1196         return retval;
1197 }
1198
1199 static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1200 {
1201         return queue_logical_block_size(bdev_get_queue(bdev));
1202 }
1203
1204 static inline unsigned int queue_physical_block_size(const struct request_queue *q)
1205 {
1206         return q->limits.physical_block_size;
1207 }
1208
1209 static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1210 {
1211         return queue_physical_block_size(bdev_get_queue(bdev));
1212 }
1213
1214 static inline unsigned int queue_io_min(const struct request_queue *q)
1215 {
1216         return q->limits.io_min;
1217 }
1218
1219 static inline int bdev_io_min(struct block_device *bdev)
1220 {
1221         return queue_io_min(bdev_get_queue(bdev));
1222 }
1223
1224 static inline unsigned int queue_io_opt(const struct request_queue *q)
1225 {
1226         return q->limits.io_opt;
1227 }
1228
1229 static inline int bdev_io_opt(struct block_device *bdev)
1230 {
1231         return queue_io_opt(bdev_get_queue(bdev));
1232 }
1233
1234 static inline unsigned int
1235 queue_zone_write_granularity(const struct request_queue *q)
1236 {
1237         return q->limits.zone_write_granularity;
1238 }
1239
1240 static inline unsigned int
1241 bdev_zone_write_granularity(struct block_device *bdev)
1242 {
1243         return queue_zone_write_granularity(bdev_get_queue(bdev));
1244 }
1245
1246 int bdev_alignment_offset(struct block_device *bdev);
1247 unsigned int bdev_discard_alignment(struct block_device *bdev);
1248
1249 static inline unsigned int bdev_max_discard_sectors(struct block_device *bdev)
1250 {
1251         return bdev_get_queue(bdev)->limits.max_discard_sectors;
1252 }
1253
1254 static inline unsigned int bdev_discard_granularity(struct block_device *bdev)
1255 {
1256         return bdev_get_queue(bdev)->limits.discard_granularity;
1257 }
1258
1259 static inline unsigned int
1260 bdev_max_secure_erase_sectors(struct block_device *bdev)
1261 {
1262         return bdev_get_queue(bdev)->limits.max_secure_erase_sectors;
1263 }
1264
1265 static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1266 {
1267         struct request_queue *q = bdev_get_queue(bdev);
1268
1269         if (q)
1270                 return q->limits.max_write_zeroes_sectors;
1271
1272         return 0;
1273 }
1274
1275 static inline bool bdev_nonrot(struct block_device *bdev)
1276 {
1277         return blk_queue_nonrot(bdev_get_queue(bdev));
1278 }
1279
1280 static inline bool bdev_synchronous(struct block_device *bdev)
1281 {
1282         return test_bit(QUEUE_FLAG_SYNCHRONOUS,
1283                         &bdev_get_queue(bdev)->queue_flags);
1284 }
1285
1286 static inline bool bdev_stable_writes(struct block_device *bdev)
1287 {
1288         return test_bit(QUEUE_FLAG_STABLE_WRITES,
1289                         &bdev_get_queue(bdev)->queue_flags);
1290 }
1291
1292 static inline bool bdev_write_cache(struct block_device *bdev)
1293 {
1294         return test_bit(QUEUE_FLAG_WC, &bdev_get_queue(bdev)->queue_flags);
1295 }
1296
1297 static inline bool bdev_fua(struct block_device *bdev)
1298 {
1299         return test_bit(QUEUE_FLAG_FUA, &bdev_get_queue(bdev)->queue_flags);
1300 }
1301
1302 static inline bool bdev_nowait(struct block_device *bdev)
1303 {
1304         return test_bit(QUEUE_FLAG_NOWAIT, &bdev_get_queue(bdev)->queue_flags);
1305 }
1306
1307 static inline bool bdev_is_zoned(struct block_device *bdev)
1308 {
1309         return blk_queue_is_zoned(bdev_get_queue(bdev));
1310 }
1311
1312 static inline unsigned int bdev_zone_no(struct block_device *bdev, sector_t sec)
1313 {
1314         return disk_zone_no(bdev->bd_disk, sec);
1315 }
1316
1317 static inline sector_t bdev_zone_sectors(struct block_device *bdev)
1318 {
1319         struct request_queue *q = bdev_get_queue(bdev);
1320
1321         if (!blk_queue_is_zoned(q))
1322                 return 0;
1323         return q->limits.chunk_sectors;
1324 }
1325
1326 static inline sector_t bdev_offset_from_zone_start(struct block_device *bdev,
1327                                                    sector_t sector)
1328 {
1329         return sector & (bdev_zone_sectors(bdev) - 1);
1330 }
1331
1332 static inline sector_t bio_offset_from_zone_start(struct bio *bio)
1333 {
1334         return bdev_offset_from_zone_start(bio->bi_bdev,
1335                                            bio->bi_iter.bi_sector);
1336 }
1337
1338 static inline bool bdev_is_zone_start(struct block_device *bdev,
1339                                       sector_t sector)
1340 {
1341         return bdev_offset_from_zone_start(bdev, sector) == 0;
1342 }
1343
1344 static inline int queue_dma_alignment(const struct request_queue *q)
1345 {
1346         return q ? q->limits.dma_alignment : 511;
1347 }
1348
1349 static inline unsigned int bdev_dma_alignment(struct block_device *bdev)
1350 {
1351         return queue_dma_alignment(bdev_get_queue(bdev));
1352 }
1353
1354 static inline bool bdev_iter_is_aligned(struct block_device *bdev,
1355                                         struct iov_iter *iter)
1356 {
1357         return iov_iter_is_aligned(iter, bdev_dma_alignment(bdev),
1358                                    bdev_logical_block_size(bdev) - 1);
1359 }
1360
1361 static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr,
1362                                  unsigned int len)
1363 {
1364         unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask;
1365         return !(addr & alignment) && !(len & alignment);
1366 }
1367
1368 /* assumes size > 256 */
1369 static inline unsigned int blksize_bits(unsigned int size)
1370 {
1371         return order_base_2(size >> SECTOR_SHIFT) + SECTOR_SHIFT;
1372 }
1373
1374 static inline unsigned int block_size(struct block_device *bdev)
1375 {
1376         return 1 << bdev->bd_inode->i_blkbits;
1377 }
1378
1379 int kblockd_schedule_work(struct work_struct *work);
1380 int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1381
1382 #define MODULE_ALIAS_BLOCKDEV(major,minor) \
1383         MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1384 #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1385         MODULE_ALIAS("block-major-" __stringify(major) "-*")
1386
1387 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
1388
1389 bool blk_crypto_register(struct blk_crypto_profile *profile,
1390                          struct request_queue *q);
1391
1392 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
1393
1394 static inline bool blk_crypto_register(struct blk_crypto_profile *profile,
1395                                        struct request_queue *q)
1396 {
1397         return true;
1398 }
1399
1400 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1401
1402 enum blk_unique_id {
1403         /* these match the Designator Types specified in SPC */
1404         BLK_UID_T10     = 1,
1405         BLK_UID_EUI64   = 2,
1406         BLK_UID_NAA     = 3,
1407 };
1408
1409 struct block_device_operations {
1410         void (*submit_bio)(struct bio *bio);
1411         int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
1412                         unsigned int flags);
1413         int (*open)(struct gendisk *disk, blk_mode_t mode);
1414         void (*release)(struct gendisk *disk);
1415         int (*ioctl)(struct block_device *bdev, blk_mode_t mode,
1416                         unsigned cmd, unsigned long arg);
1417         int (*compat_ioctl)(struct block_device *bdev, blk_mode_t mode,
1418                         unsigned cmd, unsigned long arg);
1419         unsigned int (*check_events) (struct gendisk *disk,
1420                                       unsigned int clearing);
1421         void (*unlock_native_capacity) (struct gendisk *);
1422         int (*getgeo)(struct block_device *, struct hd_geometry *);
1423         int (*set_read_only)(struct block_device *bdev, bool ro);
1424         void (*free_disk)(struct gendisk *disk);
1425         /* this callback is with swap_lock and sometimes page table lock held */
1426         void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1427         int (*report_zones)(struct gendisk *, sector_t sector,
1428                         unsigned int nr_zones, report_zones_cb cb, void *data);
1429         char *(*devnode)(struct gendisk *disk, umode_t *mode);
1430         /* returns the length of the identifier or a negative errno: */
1431         int (*get_unique_id)(struct gendisk *disk, u8 id[16],
1432                         enum blk_unique_id id_type);
1433         struct module *owner;
1434         const struct pr_ops *pr_ops;
1435
1436         /*
1437          * Special callback for probing GPT entry at a given sector.
1438          * Needed by Android devices, used by GPT scanner and MMC blk
1439          * driver.
1440          */
1441         int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
1442 };
1443
1444 #ifdef CONFIG_COMPAT
1445 extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t,
1446                                       unsigned int, unsigned long);
1447 #else
1448 #define blkdev_compat_ptr_ioctl NULL
1449 #endif
1450
1451 static inline void blk_wake_io_task(struct task_struct *waiter)
1452 {
1453         /*
1454          * If we're polling, the task itself is doing the completions. For
1455          * that case, we don't need to signal a wakeup, it's enough to just
1456          * mark us as RUNNING.
1457          */
1458         if (waiter == current)
1459                 __set_current_state(TASK_RUNNING);
1460         else
1461                 wake_up_process(waiter);
1462 }
1463
1464 unsigned long bdev_start_io_acct(struct block_device *bdev, enum req_op op,
1465                                  unsigned long start_time);
1466 void bdev_end_io_acct(struct block_device *bdev, enum req_op op,
1467                       unsigned int sectors, unsigned long start_time);
1468
1469 unsigned long bio_start_io_acct(struct bio *bio);
1470 void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1471                 struct block_device *orig_bdev);
1472
1473 /**
1474  * bio_end_io_acct - end I/O accounting for bio based drivers
1475  * @bio:        bio to end account for
1476  * @start_time: start time returned by bio_start_io_acct()
1477  */
1478 static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1479 {
1480         return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
1481 }
1482
1483 int bdev_read_only(struct block_device *bdev);
1484 int set_blocksize(struct file *file, int size);
1485
1486 int lookup_bdev(const char *pathname, dev_t *dev);
1487
1488 void blkdev_show(struct seq_file *seqf, off_t offset);
1489
1490 #define BDEVNAME_SIZE   32      /* Largest string for a blockdev identifier */
1491 #define BDEVT_SIZE      10      /* Largest string for MAJ:MIN for blkdev */
1492 #ifdef CONFIG_BLOCK
1493 #define BLKDEV_MAJOR_MAX        512
1494 #else
1495 #define BLKDEV_MAJOR_MAX        0
1496 #endif
1497
1498 struct blk_holder_ops {
1499         void (*mark_dead)(struct block_device *bdev, bool surprise);
1500
1501         /*
1502          * Sync the file system mounted on the block device.
1503          */
1504         void (*sync)(struct block_device *bdev);
1505
1506         /*
1507          * Freeze the file system mounted on the block device.
1508          */
1509         int (*freeze)(struct block_device *bdev);
1510
1511         /*
1512          * Thaw the file system mounted on the block device.
1513          */
1514         int (*thaw)(struct block_device *bdev);
1515 };
1516
1517 /*
1518  * For filesystems using @fs_holder_ops, the @holder argument passed to
1519  * helpers used to open and claim block devices via
1520  * bd_prepare_to_claim() must point to a superblock.
1521  */
1522 extern const struct blk_holder_ops fs_holder_ops;
1523
1524 /*
1525  * Return the correct open flags for blkdev_get_by_* for super block flags
1526  * as stored in sb->s_flags.
1527  */
1528 #define sb_open_mode(flags) \
1529         (BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES | \
1530          (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE))
1531
1532 struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
1533                 const struct blk_holder_ops *hops);
1534 struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode,
1535                 void *holder, const struct blk_holder_ops *hops);
1536 int bd_prepare_to_claim(struct block_device *bdev, void *holder,
1537                 const struct blk_holder_ops *hops);
1538 void bd_abort_claiming(struct block_device *bdev, void *holder);
1539
1540 /* just for blk-cgroup, don't use elsewhere */
1541 struct block_device *blkdev_get_no_open(dev_t dev);
1542 void blkdev_put_no_open(struct block_device *bdev);
1543
1544 struct block_device *I_BDEV(struct inode *inode);
1545 struct block_device *file_bdev(struct file *bdev_file);
1546
1547 #ifdef CONFIG_BLOCK
1548 void invalidate_bdev(struct block_device *bdev);
1549 int sync_blockdev(struct block_device *bdev);
1550 int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend);
1551 int sync_blockdev_nowait(struct block_device *bdev);
1552 void sync_bdevs(bool wait);
1553 void bdev_statx_dioalign(struct inode *inode, struct kstat *stat);
1554 void printk_all_partitions(void);
1555 int __init early_lookup_bdev(const char *pathname, dev_t *dev);
1556 #else
1557 static inline void invalidate_bdev(struct block_device *bdev)
1558 {
1559 }
1560 static inline int sync_blockdev(struct block_device *bdev)
1561 {
1562         return 0;
1563 }
1564 static inline int sync_blockdev_nowait(struct block_device *bdev)
1565 {
1566         return 0;
1567 }
1568 static inline void sync_bdevs(bool wait)
1569 {
1570 }
1571 static inline void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
1572 {
1573 }
1574 static inline void printk_all_partitions(void)
1575 {
1576 }
1577 static inline int early_lookup_bdev(const char *pathname, dev_t *dev)
1578 {
1579         return -EINVAL;
1580 }
1581 #endif /* CONFIG_BLOCK */
1582
1583 int bdev_freeze(struct block_device *bdev);
1584 int bdev_thaw(struct block_device *bdev);
1585 void bdev_fput(struct file *bdev_file);
1586
1587 struct io_comp_batch {
1588         struct request *req_list;
1589         bool need_ts;
1590         void (*complete)(struct io_comp_batch *);
1591 };
1592
1593 #define DEFINE_IO_COMP_BATCH(name)      struct io_comp_batch name = { }
1594
1595 #endif /* _LINUX_BLKDEV_H */