Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / include / linux / blkdev.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
322cbb50
CH
2/*
3 * Portions Copyright (C) 1992 Drew Eckhardt
4 */
1da177e4
LT
5#ifndef _LINUX_BLKDEV_H
6#define _LINUX_BLKDEV_H
7
322cbb50
CH
8#include <linux/types.h>
9#include <linux/blk_types.h>
10#include <linux/device.h>
1da177e4 11#include <linux/list.h>
320ae51f 12#include <linux/llist.h>
b296a6d5 13#include <linux/minmax.h>
1da177e4
LT
14#include <linux/timer.h>
15#include <linux/workqueue.h>
1da177e4 16#include <linux/wait.h>
1da177e4 17#include <linux/bio.h>
3e6053d7 18#include <linux/gfp.h>
322cbb50 19#include <linux/kdev_t.h>
548bc8e1 20#include <linux/rcupdate.h>
add703fd 21#include <linux/percpu-refcount.h>
6a0cb1bc 22#include <linux/blkzoned.h>
322cbb50 23#include <linux/sched.h>
d97e594c 24#include <linux/sbitmap.h>
322cbb50
CH
25#include <linux/uuid.h>
26#include <linux/xarray.h>
f3a60882 27#include <linux/file.h>
f1be1788 28#include <linux/lockdep.h>
1da177e4 29
de477254 30struct module;
1da177e4 31struct request_queue;
1da177e4 32struct elevator_queue;
2056a782 33struct blk_trace;
3d6392cf
JA
34struct request;
35struct sg_io_hdr;
3c798398 36struct blkcg_gq;
7c94e1c1 37struct blk_flush_queue;
3e08773c 38struct kiocb;
bbd3e064 39struct pr_ops;
a7905043 40struct rq_qos;
34dbad5d
OS
41struct blk_queue_stats;
42struct blk_stat_callback;
cb77cb5a 43struct blk_crypto_profile;
1da177e4 44
322cbb50 45extern const struct device_type disk_type;
cdb37f73 46extern const struct device_type part_type;
f8c7511d 47extern const struct class block_class;
322cbb50 48
8bd435b3
TH
49/*
50 * Maximum number of blkcg policies allowed to be registered concurrently.
51 * Defined here to simplify include dependency.
52 */
ec645dc9 53#define BLKCG_MAX_POLS 6
8bd435b3 54
322cbb50
CH
55#define DISK_MAX_PARTS 256
56#define DISK_NAME_LEN 32
57
58#define PARTITION_META_INFO_VOLNAMELTH 64
59/*
60 * Enough for the string representation of any kind of UUID plus NULL.
61 * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
62 */
63#define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
64
65struct partition_meta_info {
66 char uuid[PARTITION_META_INFO_UUIDLTH];
67 u8 volname[PARTITION_META_INFO_VOLNAMELTH];
68};
69
70/**
71 * DOC: genhd capability flags
72 *
73 * ``GENHD_FL_REMOVABLE``: indicates that the block device gives access to
74 * removable media. When set, the device remains present even when media is not
75 * inserted. Shall not be set for devices which are removed entirely when the
76 * media is removed.
77 *
78 * ``GENHD_FL_HIDDEN``: the block device is hidden; it doesn't produce events,
79 * doesn't appear in sysfs, and can't be opened from userspace or using
80 * blkdev_get*. Used for the underlying components of multipath devices.
81 *
82 * ``GENHD_FL_NO_PART``: partition support is disabled. The kernel will not
83 * scan for partitions from add_disk, and users can't add partitions manually.
84 *
85 */
86enum {
87 GENHD_FL_REMOVABLE = 1 << 0,
88 GENHD_FL_HIDDEN = 1 << 1,
89 GENHD_FL_NO_PART = 1 << 2,
90};
91
92enum {
93 DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
94 DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
95};
96
97enum {
98 /* Poll even if events_poll_msecs is unset */
99 DISK_EVENT_FLAG_POLL = 1 << 0,
100 /* Forward events to udev */
101 DISK_EVENT_FLAG_UEVENT = 1 << 1,
102 /* Block event polling when open for exclusive write */
103 DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE = 1 << 2,
104};
105
106struct disk_events;
107struct badblocks;
108
e9f5f44a
CH
109enum blk_integrity_checksum {
110 BLK_INTEGRITY_CSUM_NONE = 0,
111 BLK_INTEGRITY_CSUM_IP = 1,
112 BLK_INTEGRITY_CSUM_CRC = 2,
113 BLK_INTEGRITY_CSUM_CRC64 = 3,
114} __packed ;
115
322cbb50 116struct blk_integrity {
322cbb50 117 unsigned char flags;
e9f5f44a 118 enum blk_integrity_checksum csum_type;
322cbb50 119 unsigned char tuple_size;
60d21aac 120 unsigned char pi_offset;
322cbb50
CH
121 unsigned char interval_exp;
122 unsigned char tag_size;
123};
124
05bdb996
CH
125typedef unsigned int __bitwise blk_mode_t;
126
127/* open for reading */
128#define BLK_OPEN_READ ((__force blk_mode_t)(1 << 0))
129/* open for writing */
130#define BLK_OPEN_WRITE ((__force blk_mode_t)(1 << 1))
131/* open exclusively (vs other exclusive openers */
132#define BLK_OPEN_EXCL ((__force blk_mode_t)(1 << 2))
133/* opened with O_NDELAY */
134#define BLK_OPEN_NDELAY ((__force blk_mode_t)(1 << 3))
135/* open for "writes" only for ioctls (specialy hack for floppy.c) */
136#define BLK_OPEN_WRITE_IOCTL ((__force blk_mode_t)(1 << 4))
ed5cc702
JK
137/* open is exclusive wrt all other BLK_OPEN_WRITE opens to the device */
138#define BLK_OPEN_RESTRICT_WRITES ((__force blk_mode_t)(1 << 5))
752863bd
CH
139/* return partition scanning errors */
140#define BLK_OPEN_STRICT_SCAN ((__force blk_mode_t)(1 << 6))
05bdb996 141
322cbb50
CH
142struct gendisk {
143 /*
144 * major/first_minor/minors should not be set by any new driver, the
145 * block core will take care of allocating them automatically.
146 */
147 int major;
148 int first_minor;
149 int minors;
150
151 char disk_name[DISK_NAME_LEN]; /* name of major driver */
152
153 unsigned short events; /* supported events */
154 unsigned short event_flags; /* flags related to event processing */
155
156 struct xarray part_tbl;
157 struct block_device *part0;
158
159 const struct block_device_operations *fops;
160 struct request_queue *queue;
161 void *private_data;
162
46754bd0
CH
163 struct bio_set bio_split;
164
322cbb50
CH
165 int flags;
166 unsigned long state;
167#define GD_NEED_PART_SCAN 0
168#define GD_READ_ONLY 1
169#define GD_DEAD 2
170#define GD_NATIVE_CAPACITY 3
76792055 171#define GD_ADDED 4
b9684a71 172#define GD_SUPPRESS_PART_SCAN 5
6f8191fd 173#define GD_OWNS_QUEUE 6
322cbb50
CH
174
175 struct mutex open_mutex; /* open/close mutex */
176 unsigned open_partitions; /* number of open partitions */
177
178 struct backing_dev_info *bdi;
2bd85221 179 struct kobject queue_kobj; /* the queue/ directory */
322cbb50
CH
180 struct kobject *slave_dir;
181#ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
182 struct list_head slave_bdevs;
183#endif
184 struct timer_rand_state *random;
322cbb50 185 struct disk_events *ev;
d86e716a
CH
186
187#ifdef CONFIG_BLK_DEV_ZONED
188 /*
02ccd7c3
DLM
189 * Zoned block device information. Reads of this information must be
190 * protected with blk_queue_enter() / blk_queue_exit(). Modifying this
191 * information is only allowed while no requests are being processed.
192 * See also blk_mq_freeze_queue() and blk_mq_unfreeze_queue().
d86e716a
CH
193 */
194 unsigned int nr_zones;
ecfe43b1 195 unsigned int zone_capacity;
29459c3e 196 unsigned int last_zone_capacity;
d7cb6d74 197 unsigned long __rcu *conv_zones_bitmap;
a6aa36e9
DLM
198 unsigned int zone_wplugs_hash_bits;
199 atomic_t nr_zone_wplugs;
200 spinlock_t zone_wplugs_lock;
dd291d77 201 struct mempool_s *zone_wplugs_pool;
a6aa36e9 202 struct hlist_head *zone_wplugs_hash;
a8f59e5a 203 struct workqueue_struct *zone_wplugs_wq;
d86e716a
CH
204#endif /* CONFIG_BLK_DEV_ZONED */
205
322cbb50
CH
206#if IS_ENABLED(CONFIG_CDROM)
207 struct cdrom_device_info *cdi;
208#endif
209 int node_id;
210 struct badblocks *bb;
211 struct lockdep_map lockdep_map;
212 u64 diskseq;
05bdb996 213 blk_mode_t open_mode;
6a27d28c
CH
214
215 /*
216 * Independent sector access ranges. This is always NULL for
217 * devices that do not have multiple independent access ranges.
218 */
219 struct blk_independent_access_ranges *ia_ranges;
78c27134
ML
220
221 struct mutex rqos_state_mutex; /* rqos state change mutex */
322cbb50
CH
222};
223
dbdc1be3
CH
224/**
225 * disk_openers - returns how many openers are there for a disk
226 * @disk: disk to check
227 *
228 * This returns the number of openers for a disk. Note that this value is only
229 * stable if disk->open_mutex is held.
230 *
231 * Note: Due to a quirk in the block layer open code, each open partition is
232 * only counted once even if there are multiple openers.
233 */
234static inline unsigned int disk_openers(struct gendisk *disk)
235{
9acf381f 236 return atomic_read(&disk->part0->bd_openers);
dbdc1be3
CH
237}
238
140ce28d
CH
239/**
240 * disk_has_partscan - return %true if partition scanning is enabled on a disk
241 * @disk: disk to check
242 *
243 * Returns %true if partitions scanning is enabled for @disk, or %false if
244 * partition scanning is disabled either permanently or temporarily.
245 */
246static inline bool disk_has_partscan(struct gendisk *disk)
247{
248 return !(disk->flags & (GENHD_FL_NO_PART | GENHD_FL_HIDDEN)) &&
249 !test_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
250}
251
322cbb50
CH
252/*
253 * The gendisk is refcounted by the part0 block_device, and the bd_device
254 * therein is also used for device model presentation in sysfs.
255 */
256#define dev_to_disk(device) \
257 (dev_to_bdev(device)->bd_disk)
258#define disk_to_dev(disk) \
259 (&((disk)->part0->bd_device))
260
261#if IS_REACHABLE(CONFIG_CDROM)
262#define disk_to_cdi(disk) ((disk)->cdi)
263#else
264#define disk_to_cdi(disk) NULL
265#endif
266
267static inline dev_t disk_devt(struct gendisk *disk)
268{
269 return MKDEV(disk->major, disk->first_minor);
270}
271
4cdf1bdd 272#ifdef CONFIG_TRANSPARENT_HUGEPAGE
47dd6753
LC
273/*
274 * We should strive for 1 << (PAGE_SHIFT + MAX_PAGECACHE_ORDER)
275 * however we constrain this to what we can validate and test.
276 */
277#define BLK_MAX_BLOCK_SIZE SZ_64K
4cdf1bdd
PR
278#else
279#define BLK_MAX_BLOCK_SIZE PAGE_SIZE
280#endif
281
47dd6753 282
fe3d508b 283/* blk_validate_limits() validates bsize, so drivers don't usually need to */
37ae5a0f 284static inline int blk_validate_block_size(unsigned long bsize)
570b1cac 285{
47dd6753 286 if (bsize < 512 || bsize > BLK_MAX_BLOCK_SIZE || !is_power_of_2(bsize))
570b1cac
XY
287 return -EINVAL;
288
289 return 0;
290}
291
16458cf3 292static inline bool blk_op_is_passthrough(blk_opf_t op)
14cb0dc6 293{
da6269da 294 op &= REQ_OP_MASK;
14cb0dc6
ML
295 return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
296}
297
1122c0c1 298/* flags set by the driver in queue_limits.features */
fcf865e3 299typedef unsigned int __bitwise blk_features_t;
1122c0c1 300
fcf865e3
CH
301/* supports a volatile write cache */
302#define BLK_FEAT_WRITE_CACHE ((__force blk_features_t)(1u << 0))
bd4a633b 303
fcf865e3
CH
304/* supports passing on the FUA bit */
305#define BLK_FEAT_FUA ((__force blk_features_t)(1u << 1))
39a9f1c3 306
fcf865e3
CH
307/* rotational device (hard drive or floppy) */
308#define BLK_FEAT_ROTATIONAL ((__force blk_features_t)(1u << 2))
cdb24979 309
fcf865e3
CH
310/* contributes to the random number pool */
311#define BLK_FEAT_ADD_RANDOM ((__force blk_features_t)(1u << 3))
1a02f3a7 312
fcf865e3
CH
313/* do disk/partitions IO accounting */
314#define BLK_FEAT_IO_STAT ((__force blk_features_t)(1u << 4))
aadd5c59 315
fcf865e3
CH
316/* don't modify data until writeback is done */
317#define BLK_FEAT_STABLE_WRITES ((__force blk_features_t)(1u << 5))
f76af42f 318
fcf865e3
CH
319/* always completes in submit context */
320#define BLK_FEAT_SYNCHRONOUS ((__force blk_features_t)(1u << 6))
f467fee4 321
fcf865e3
CH
322/* supports REQ_NOWAIT */
323#define BLK_FEAT_NOWAIT ((__force blk_features_t)(1u << 7))
8023e144 324
fcf865e3
CH
325/* supports DAX */
326#define BLK_FEAT_DAX ((__force blk_features_t)(1u << 8))
b1fc937a 327
fcf865e3
CH
328/* supports I/O polling */
329#define BLK_FEAT_POLL ((__force blk_features_t)(1u << 9))
a52758a3 330
fcf865e3
CH
331/* is a zoned device */
332#define BLK_FEAT_ZONED ((__force blk_features_t)(1u << 10))
9c1e42e3 333
fcf865e3
CH
334/* supports PCI(e) p2p requests */
335#define BLK_FEAT_PCI_P2PDMA ((__force blk_features_t)(1u << 12))
339d3948 336
fcf865e3
CH
337/* skip this queue in blk_mq_(un)quiesce_tagset */
338#define BLK_FEAT_SKIP_TAGSET_QUIESCE ((__force blk_features_t)(1u << 13))
7d4dec52 339
fcf865e3
CH
340/* undocumented magic for bcache */
341#define BLK_FEAT_RAID_PARTIAL_STRIPES_EXPENSIVE \
342 ((__force blk_features_t)(1u << 15))
1122c0c1 343
6a7e17b2
JG
344/* atomic writes enabled */
345#define BLK_FEAT_ATOMIC_WRITES \
d7f36dc4
JG
346 ((__force blk_features_t)(1u << 16))
347
9bb33f24 348/*
1122c0c1 349 * Flags automatically inherited when stacking limits.
9bb33f24 350 */
1122c0c1 351#define BLK_FEAT_INHERIT_MASK \
1a02f3a7 352 (BLK_FEAT_WRITE_CACHE | BLK_FEAT_FUA | BLK_FEAT_ROTATIONAL | \
eeadd68e 353 BLK_FEAT_STABLE_WRITES | BLK_FEAT_ZONED | \
7d4dec52 354 BLK_FEAT_RAID_PARTIAL_STRIPES_EXPENSIVE)
1122c0c1
CH
355
356/* internal flags in queue_limits.flags */
fcf865e3 357typedef unsigned int __bitwise blk_flags_t;
1122c0c1 358
fcf865e3
CH
359/* do not send FLUSH/FUA commands despite advertising a write cache */
360#define BLK_FLAG_WRITE_CACHE_DISABLED ((__force blk_flags_t)(1u << 0))
361
362/* I/O topology is misaligned */
363#define BLK_FLAG_MISALIGNED ((__force blk_flags_t)(1u << 1))
9bb33f24 364
110234da
KB
365/* passthrough command IO accounting */
366#define BLK_FLAG_IOSTATS_PASSTHROUGH ((__force blk_flags_t)(1u << 2))
367
025146e1 368struct queue_limits {
fcf865e3
CH
369 blk_features_t features;
370 blk_flags_t flags;
025146e1 371 unsigned long seg_boundary_mask;
03100aad 372 unsigned long virt_boundary_mask;
025146e1
MP
373
374 unsigned int max_hw_sectors;
ca369d51 375 unsigned int max_dev_sectors;
762380ad 376 unsigned int chunk_sectors;
025146e1 377 unsigned int max_sectors;
c9c77418 378 unsigned int max_user_sectors;
025146e1 379 unsigned int max_segment_size;
889c5706 380 unsigned int min_segment_size;
c72758f3 381 unsigned int physical_block_size;
ad6bf88a 382 unsigned int logical_block_size;
c72758f3
MP
383 unsigned int alignment_offset;
384 unsigned int io_min;
385 unsigned int io_opt;
67efc925 386 unsigned int max_discard_sectors;
0034af03 387 unsigned int max_hw_discard_sectors;
4f563a64 388 unsigned int max_user_discard_sectors;
44abff2c 389 unsigned int max_secure_erase_sectors;
a6f0788e 390 unsigned int max_write_zeroes_sectors;
559218d4 391 unsigned int max_hw_zone_append_sectors;
0512a75b 392 unsigned int max_zone_append_sectors;
86b37281
MP
393 unsigned int discard_granularity;
394 unsigned int discard_alignment;
a805a4fa 395 unsigned int zone_write_granularity;
025146e1 396
9da3d1e9
JG
397 /* atomic write limits */
398 unsigned int atomic_write_hw_max;
399 unsigned int atomic_write_max_sectors;
400 unsigned int atomic_write_hw_boundary;
401 unsigned int atomic_write_boundary_sectors;
402 unsigned int atomic_write_hw_unit_min;
403 unsigned int atomic_write_unit_min;
404 unsigned int atomic_write_hw_unit_max;
405 unsigned int atomic_write_unit_max;
406
8a78362c 407 unsigned short max_segments;
13f05c8d 408 unsigned short max_integrity_segments;
1e739730 409 unsigned short max_discard_segments;
025146e1 410
d2f526ba 411 unsigned short max_write_streams;
c23acfac 412 unsigned int write_stream_granularity;
d2f526ba 413
8c4955c0
CH
414 unsigned int max_open_zones;
415 unsigned int max_active_zones;
c964d62f
KB
416
417 /*
418 * Drivers that set dma_alignment to less than 511 must be prepared to
419 * handle individual bvec's that are not a multiple of a SECTOR_SIZE
420 * due to possible offsets.
421 */
422 unsigned int dma_alignment;
e94b45d0 423 unsigned int dma_pad_mask;
c6e56cf6
CH
424
425 struct blk_integrity integrity;
025146e1
MP
426};
427
d4100351
CH
428typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
429 void *data);
430
d4100351
CH
431#define BLK_ALL_ZONES ((unsigned int)-1)
432int blkdev_report_zones(struct block_device *bdev, sector_t sector,
668bfeea
CH
433 unsigned int nr_zones, report_zones_cb cb, void *data);
434int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op,
71f4ecdb 435 sector_t sectors, sector_t nr_sectors);
9b3c08b9 436int blk_revalidate_disk_zones(struct gendisk *disk);
6a0cb1bc 437
a2247f19
DLM
438/*
439 * Independent access ranges: struct blk_independent_access_range describes
440 * a range of contiguous sectors that can be accessed using device command
441 * execution resources that are independent from the resources used for
442 * other access ranges. This is typically found with single-LUN multi-actuator
443 * HDDs where each access range is served by a different set of heads.
444 * The set of independent ranges supported by the device is defined using
445 * struct blk_independent_access_ranges. The independent ranges must not overlap
446 * and must include all sectors within the disk capacity (no sector holes
447 * allowed).
448 * For a device with multiple ranges, requests targeting sectors in different
449 * ranges can be executed in parallel. A request can straddle an access range
450 * boundary.
451 */
452struct blk_independent_access_range {
453 struct kobject kobj;
a2247f19
DLM
454 sector_t sector;
455 sector_t nr_sectors;
456};
457
458struct blk_independent_access_ranges {
459 struct kobject kobj;
460 bool sysfs_registered;
461 unsigned int nr_ia_ranges;
462 struct blk_independent_access_range ia_range[];
463};
464
d7b76301 465struct request_queue {
0c734c5e
JA
466 /*
467 * The queue owner gets to use this for whatever they like.
468 * ll_rw_blk doesn't touch it.
469 */
470 void *queuedata;
0549e87c 471
0c734c5e 472 struct elevator_queue *elevator;
87760e5e 473
f8a5b122 474 const struct blk_mq_ops *mq_ops;
320ae51f 475
320ae51f 476 /* sw queues */
e6cdb092 477 struct blk_mq_ctx __percpu *queue_ctx;
320ae51f 478
0c734c5e
JA
479 /*
480 * various queue flags, see QUEUE_* below
481 */
482 unsigned long queue_flags;
483
484 unsigned int rq_timeout;
485
d278d4a8
JA
486 unsigned int queue_depth;
487
0c734c5e
JA
488 refcount_t refs;
489
320ae51f 490 /* hw dispatch queues */
320ae51f 491 unsigned int nr_hw_queues;
0c734c5e 492 struct xarray hctx_table;
320ae51f 493
0c734c5e 494 struct percpu_ref q_usage_counter;
f1be1788
ML
495 struct lock_class_key io_lock_cls_key;
496 struct lockdep_map io_lockdep_map;
497
498 struct lock_class_key q_lock_cls_key;
499 struct lockdep_map q_lockdep_map;
1da177e4 500
0c734c5e 501 struct request *last_merge;
a73f730d 502
0d945c1f 503 spinlock_t queue_lock;
1da177e4 504
0c734c5e 505 int quiesce_depth;
d152c682 506
0c734c5e 507 struct gendisk *disk;
1da177e4 508
320ae51f
JA
509 /*
510 * mq queue kobject
511 */
1db4909e 512 struct kobject *mq_kobj;
320ae51f 513
0c734c5e
JA
514 struct queue_limits limits;
515
47fafbc7 516#ifdef CONFIG_PM
6c954667 517 struct device *dev;
db04e18d 518 enum rpm_status rpm_status;
6c954667
LM
519#endif
520
1da177e4 521 /*
0c734c5e
JA
522 * Number of contexts that have called blk_set_pm_only(). If this
523 * counter is above zero then only RQF_PM requests are processed.
1da177e4 524 */
0c734c5e
JA
525 atomic_t pm_only;
526
527 struct blk_queue_stats *stats;
528 struct rq_qos *rq_qos;
529 struct mutex rq_qos_mutex;
530
531 /*
532 * ida allocated id for this queue. Used to index queues from
533 * ioctx.
534 */
535 int id;
1da177e4 536
0c734c5e
JA
537 /*
538 * queue settings
539 */
540 unsigned long nr_requests; /* Max # of requests */
541
1b262839 542#ifdef CONFIG_BLK_INLINE_ENCRYPTION
cb77cb5a 543 struct blk_crypto_profile *crypto_profile;
20f01f16 544 struct kobject *crypto_kobject;
1b262839
ST
545#endif
546
242f9dcb 547 struct timer_list timeout;
287922eb 548 struct work_struct timeout_work;
242f9dcb 549
079a2e3e 550 atomic_t nr_active_requests_shared_tags;
bccf5e26 551
079a2e3e 552 struct blk_mq_tags *sched_shared_tags;
d97e594c 553
a612fddf 554 struct list_head icq_list;
1231039d
CH
555#ifdef CONFIG_BLK_CGROUP
556 DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
557 struct blkcg_gq *root_blkg;
558 struct list_head blkg_list;
559 struct mutex blkcg_mutex;
560#endif
a612fddf 561
0c734c5e 562 int node;
025146e1 563
0c734c5e
JA
564 spinlock_t requeue_lock;
565 struct list_head requeue_list;
566 struct delayed_work requeue_work;
68c43f13 567
6c5c9341 568#ifdef CONFIG_BLK_DEV_IO_TRACE
c780e86d 569 struct blk_trace __rcu *blk_trace;
6c5c9341 570#endif
1da177e4 571 /*
4913efe4 572 * for flush operations
1da177e4 573 */
7c94e1c1 574 struct blk_flush_queue *fq;
9a67aa52 575 struct list_head flush_list;
483f4afc 576
1bf70d08 577 /*
5abba4ce
NS
578 * Protects against I/O scheduler switching, particularly when updating
579 * q->elevator. Since the elevator update code path may also modify q->
580 * nr_requests and wbt latency, this lock also protects the sysfs attrs
581 * nr_requests and wbt_lat_usec. Additionally the nr_hw_queues update
582 * may modify hctx tags, reserved-tags and cpumask, so this lock also
a3996d11
NS
583 * helps protect the hctx sysfs/debugfs attrs. To ensure proper locking
584 * order during an elevator or nr_hw_queue update, first freeze the
585 * queue, then acquire ->elevator_lock.
1bf70d08
NS
586 */
587 struct mutex elevator_lock;
588
483f4afc 589 struct mutex sysfs_lock;
5e40f445
NS
590 /*
591 * Protects queue limits and also sysfs attribute read_ahead_kb.
592 */
d690cb8a 593 struct mutex limits_lock;
d351af01 594
2f8f1336
ML
595 /*
596 * for reusing dead hctx instance in case of updating
597 * nr_hw_queues
598 */
599 struct list_head unused_hctx_list;
600 spinlock_t unused_hctx_lock;
601
7996a8b5 602 int mq_freeze_depth;
d732580b 603
e43473b7
VG
604#ifdef CONFIG_BLK_DEV_THROTTLING
605 /* Throttle data */
606 struct throtl_data *td;
607#endif
548bc8e1 608 struct rcu_head rcu_head;
6a786998
ML
609#ifdef CONFIG_LOCKDEP
610 struct task_struct *mq_freeze_owner;
611 int mq_freeze_owner_depth;
f6661b1d
ML
612 /*
613 * Records disk & queue state in current context, used in unfreeze
614 * queue
615 */
6f491a8d 616 bool mq_freeze_disk_dead;
f6661b1d 617 bool mq_freeze_queue_dying;
6a786998 618#endif
320ae51f 619 wait_queue_head_t mq_freeze_wq;
7996a8b5
BL
620 /*
621 * Protect concurrent access to q_usage_counter by
622 * percpu_ref_kill() and percpu_ref_reinit().
623 */
624 struct mutex mq_freeze_lock;
0d2602ca
JA
625
626 struct blk_mq_tag_set *tag_set;
627 struct list_head tag_set_list;
4593fdbe 628
07e4fead 629 struct dentry *debugfs_dir;
d332ce09 630 struct dentry *sched_debugfs_dir;
cc56694f 631 struct dentry *rqos_debugfs_dir;
5cf9c91b
CH
632 /*
633 * Serializes all debugfs metadata operations using the above dentries.
634 */
635 struct mutex debugfs_mutex;
1da177e4
LT
636};
637
bfe373f6 638/* Keep blk_queue_flag_name[] in sync with the definitions below */
55177adf
JG
639enum {
640 QUEUE_FLAG_DYING, /* queue being torn down */
641 QUEUE_FLAG_NOMERGES, /* disable merge attempts */
642 QUEUE_FLAG_SAME_COMP, /* complete on same CPU-group */
643 QUEUE_FLAG_FAIL_IO, /* fake timeout */
644 QUEUE_FLAG_NOXMERGES, /* No extended merges */
645 QUEUE_FLAG_SAME_FORCE, /* force complete on same CPU */
646 QUEUE_FLAG_INIT_DONE, /* queue is initialized */
647 QUEUE_FLAG_STATS, /* track IO start and completion times */
648 QUEUE_FLAG_REGISTERED, /* queue has been registered to a disk */
649 QUEUE_FLAG_QUIESCED, /* queue has been quiesced */
650 QUEUE_FLAG_RQ_ALLOC_TIME, /* record rq->alloc_time_ns */
651 QUEUE_FLAG_HCTX_ACTIVE, /* at least one blk-mq hctx is active */
652 QUEUE_FLAG_SQ_SCHED, /* single queue style io dispatch */
56dee46f 653 QUEUE_FLAG_DISABLE_WBT_DEF, /* for sched to disable/enable wbt */
21eed794 654 QUEUE_FLAG_NO_ELV_SWITCH, /* can't switch elevator any more */
55177adf
JG
655 QUEUE_FLAG_MAX
656};
797e7dbb 657
f76af42f 658#define QUEUE_FLAG_MQ_DEFAULT (1UL << QUEUE_FLAG_SAME_COMP)
94eddfbe 659
8814ce8a
BVA
660void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
661void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
8814ce8a 662
3f3299d5 663#define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
320ae51f 664#define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
ac9fafa1 665#define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
488991e2
AB
666#define blk_queue_noxmerges(q) \
667 test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
44348870 668#define blk_queue_nonrot(q) (!((q)->limits.features & BLK_FEAT_ROTATIONAL))
cdb24979 669#define blk_queue_io_stat(q) ((q)->limits.features & BLK_FEAT_IO_STAT)
110234da
KB
670#define blk_queue_passthrough_stat(q) \
671 ((q)->limits.flags & BLK_FLAG_IOSTATS_PASSTHROUGH)
f467fee4 672#define blk_queue_dax(q) ((q)->limits.features & BLK_FEAT_DAX)
9c1e42e3 673#define blk_queue_pci_p2pdma(q) ((q)->limits.features & BLK_FEAT_PCI_P2PDMA)
6f816b4b
TH
674#ifdef CONFIG_BLK_RQ_ALLOC_TIME
675#define blk_queue_rq_alloc_time(q) \
676 test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
677#else
678#define blk_queue_rq_alloc_time(q) false
679#endif
1da177e4 680
33659ebb
CH
681#define blk_noretry_request(rq) \
682 ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
683 REQ_FAILFAST_DRIVER))
f4560ffe 684#define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
cd84a62e 685#define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
58c898ba 686#define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
4d337ceb 687#define blk_queue_sq_sched(q) test_bit(QUEUE_FLAG_SQ_SCHED, &(q)->queue_flags)
414dd48e 688#define blk_queue_skip_tagset_quiesce(q) \
8c8f5c85 689 ((q)->limits.features & BLK_FEAT_SKIP_TAGSET_QUIESCE)
56dee46f
ML
690#define blk_queue_disable_wbt(q) \
691 test_bit(QUEUE_FLAG_DISABLE_WBT_DEF, &(q)->queue_flags)
21eed794
ML
692#define blk_queue_no_elv_switch(q) \
693 test_bit(QUEUE_FLAG_NO_ELV_SWITCH, &(q)->queue_flags)
c9254f2d 694
cd84a62e
BVA
695extern void blk_set_pm_only(struct request_queue *q);
696extern void blk_clear_pm_only(struct request_queue *q);
33659ebb 697
1da177e4
LT
698#define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
699
3ab3a031
CH
700#define dma_map_bvec(dev, bv, dir, attrs) \
701 dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
702 (dir), (attrs))
703
344e9ffc 704static inline bool queue_is_mq(struct request_queue *q)
49fd524f 705{
a1ce35fa 706 return q->mq_ops;
49fd524f
JA
707}
708
52abca64
AS
709#ifdef CONFIG_PM
710static inline enum rpm_status queue_rpm_status(struct request_queue *q)
711{
712 return q->rpm_status;
713}
714#else
715static inline enum rpm_status queue_rpm_status(struct request_queue *q)
716{
717 return RPM_ACTIVE;
718}
719#endif
720
797476b8
DLM
721static inline bool blk_queue_is_zoned(struct request_queue *q)
722{
b1fc937a
CH
723 return IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
724 (q->limits.features & BLK_FEAT_ZONED);
797476b8
DLM
725}
726
d86e716a 727static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector)
6cc77e9c 728{
d86e716a 729 if (!blk_queue_is_zoned(disk->queue))
6cc77e9c 730 return 0;
d86e716a 731 return sector >> ilog2(disk->queue->limits.chunk_sectors);
6cc77e9c
CH
732}
733
1dc01720
CH
734static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
735{
8c4955c0 736 return bdev->bd_disk->queue->limits.max_open_zones;
1dc01720
CH
737}
738
739static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
659bf827 740{
8c4955c0 741 return bdev->bd_disk->queue->limits.max_active_zones;
659bf827 742}
1dc01720 743
d278d4a8
JA
744static inline unsigned int blk_queue_depth(struct request_queue *q)
745{
746 if (q->queue_depth)
747 return q->queue_depth;
748
749 return q->nr_requests;
750}
751
3d6392cf
JA
752/*
753 * default timeout for SG_IO if none specified
754 */
755#define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
f2f1fa78 756#define BLK_MIN_SG_TIMEOUT (7 * HZ)
3d6392cf 757
5705f702 758/* This should not be used directly - use rq_for_each_segment */
1e428079
JA
759#define for_each_bio(_bio) \
760 for (; _bio; _bio = _bio->bi_next)
5705f702 761
9dfd9ea9
CM
762int __must_check add_disk_fwnode(struct device *parent, struct gendisk *disk,
763 const struct attribute_group **groups,
764 struct fwnode_handle *fwnode);
322cbb50
CH
765int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
766 const struct attribute_group **groups);
767static inline int __must_check add_disk(struct gendisk *disk)
768{
769 return device_add_disk(NULL, disk, NULL);
770}
771void del_gendisk(struct gendisk *gp);
772void invalidate_disk(struct gendisk *disk);
773void set_disk_ro(struct gendisk *disk, bool read_only);
774void disk_uevent(struct gendisk *disk, enum kobject_action action);
775
b8c873ed
AV
776static inline u8 bdev_partno(const struct block_device *bdev)
777{
1116b9fa
AV
778 return atomic_read(&bdev->__bd_flags) & BD_PARTNO;
779}
780
781static inline bool bdev_test_flag(const struct block_device *bdev, unsigned flag)
782{
783 return atomic_read(&bdev->__bd_flags) & flag;
784}
785
786static inline void bdev_set_flag(struct block_device *bdev, unsigned flag)
787{
788 atomic_or(flag, &bdev->__bd_flags);
789}
790
791static inline void bdev_clear_flag(struct block_device *bdev, unsigned flag)
792{
793 atomic_andnot(flag, &bdev->__bd_flags);
b8c873ed
AV
794}
795
766a71ef 796static inline bool get_disk_ro(struct gendisk *disk)
322cbb50 797{
01e198f0 798 return bdev_test_flag(disk->part0, BD_READ_ONLY) ||
322cbb50
CH
799 test_bit(GD_READ_ONLY, &disk->state);
800}
801
766a71ef 802static inline bool bdev_read_only(struct block_device *bdev)
322cbb50 803{
01e198f0 804 return bdev_test_flag(bdev, BD_READ_ONLY) || get_disk_ro(bdev->bd_disk);
322cbb50
CH
805}
806
807bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
ab6860f6 808void disk_force_media_change(struct gendisk *disk);
560e20e4 809void bdev_mark_dead(struct block_device *bdev, bool surprise);
322cbb50
CH
810
811void add_disk_randomness(struct gendisk *disk) __latent_entropy;
812void rand_initialize_disk(struct gendisk *disk);
813
814static inline sector_t get_start_sect(struct block_device *bdev)
815{
816 return bdev->bd_start_sect;
817}
818
819static inline sector_t bdev_nr_sectors(struct block_device *bdev)
820{
821 return bdev->bd_nr_sectors;
822}
823
824static inline loff_t bdev_nr_bytes(struct block_device *bdev)
825{
826 return (loff_t)bdev_nr_sectors(bdev) << SECTOR_SHIFT;
827}
828
829static inline sector_t get_capacity(struct gendisk *disk)
830{
831 return bdev_nr_sectors(disk->part0);
832}
833
834static inline u64 sb_bdev_nr_blocks(struct super_block *sb)
835{
836 return bdev_nr_sectors(sb->s_bdev) >>
837 (sb->s_blocksize_bits - SECTOR_SHIFT);
838}
839
c1a79b1a
NA
840#ifdef CONFIG_BLK_DEV_ZONED
841static inline unsigned int disk_nr_zones(struct gendisk *disk)
842{
843 return disk->nr_zones;
844}
845bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs);
846
847/**
848 * disk_zone_capacity - returns the zone capacity of zone containing @sector
849 * @disk: disk to work with
850 * @sector: sector number within the querying zone
851 *
852 * Returns the zone capacity of a zone containing @sector. @sector can be any
853 * sector in the zone.
854 */
855static inline unsigned int disk_zone_capacity(struct gendisk *disk,
856 sector_t sector)
857{
858 sector_t zone_sectors = disk->queue->limits.chunk_sectors;
859
860 if (sector + zone_sectors >= get_capacity(disk))
861 return disk->last_zone_capacity;
862 return disk->zone_capacity;
863}
864static inline unsigned int bdev_zone_capacity(struct block_device *bdev,
865 sector_t pos)
866{
867 return disk_zone_capacity(bdev->bd_disk, pos);
868}
869#else /* CONFIG_BLK_DEV_ZONED */
870static inline unsigned int disk_nr_zones(struct gendisk *disk)
871{
872 return 0;
873}
874static inline bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs)
875{
876 return false;
877}
878#endif /* CONFIG_BLK_DEV_ZONED */
879
880static inline unsigned int bdev_nr_zones(struct block_device *bdev)
881{
882 return disk_nr_zones(bdev->bd_disk);
883}
884
322cbb50
CH
885int bdev_disk_changed(struct gendisk *disk, bool invalidate);
886
322cbb50 887void put_disk(struct gendisk *disk);
74fa8f9c
CH
888struct gendisk *__blk_alloc_disk(struct queue_limits *lim, int node,
889 struct lock_class_key *lkclass);
322cbb50
CH
890
891/**
892 * blk_alloc_disk - allocate a gendisk structure
74fa8f9c 893 * @lim: queue limits to be used for this disk.
322cbb50
CH
894 * @node_id: numa node to allocate on
895 *
896 * Allocate and pre-initialize a gendisk structure for use with BIO based
897 * drivers.
898 *
74fa8f9c
CH
899 * Returns an ERR_PTR on error, else the allocated disk.
900 *
322cbb50
CH
901 * Context: can sleep
902 */
74fa8f9c 903#define blk_alloc_disk(lim, node_id) \
322cbb50
CH
904({ \
905 static struct lock_class_key __key; \
906 \
74fa8f9c 907 __blk_alloc_disk(lim, node_id, &__key); \
322cbb50 908})
322cbb50
CH
909
910int __register_blkdev(unsigned int major, const char *name,
911 void (*probe)(dev_t devt));
912#define register_blkdev(major, name) \
913 __register_blkdev(major, name, NULL)
914void unregister_blkdev(unsigned int major, const char *name);
915
444aa2c5 916bool disk_check_media_change(struct gendisk *disk);
322cbb50
CH
917void set_capacity(struct gendisk *disk, sector_t size);
918
919#ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
920int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
921void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
322cbb50
CH
922#else
923static inline int bd_link_disk_holder(struct block_device *bdev,
924 struct gendisk *disk)
925{
926 return 0;
927}
928static inline void bd_unlink_disk_holder(struct block_device *bdev,
929 struct gendisk *disk)
930{
931}
322cbb50
CH
932#endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */
933
934dev_t part_devt(struct gendisk *disk, u8 partno);
935void inc_diskseq(struct gendisk *disk);
322cbb50 936void blk_request_module(dev_t devt);
2d4dc890 937
1da177e4
LT
938extern int blk_register_queue(struct gendisk *disk);
939extern void blk_unregister_queue(struct gendisk *disk);
3e08773c 940void submit_bio_noacct(struct bio *bio);
5a97806f 941struct bio *bio_split_to_limits(struct bio *bio);
24b83deb 942
ef9e3fac 943extern int blk_lld_busy(struct request_queue *q);
9a95e4ef 944extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
2e6edc95 945extern void blk_queue_exit(struct request_queue *q);
1da177e4 946extern void blk_sync_queue(struct request_queue *q);
fb9b16e1 947
e47bc4ed 948/* Helper to convert REQ_OP_XXX to its string format XXX */
77e7ffd7 949extern const char *blk_op_str(enum req_op op);
e47bc4ed 950
2a842aca
CH
951int blk_status_to_errno(blk_status_t status);
952blk_status_t errno_to_blk_status(int errno);
7ba37927 953const char *blk_status_to_str(blk_status_t status);
2a842aca 954
ef99b2d3
CH
955/* only poll the hardware once, don't continue until a completion was found */
956#define BLK_POLL_ONESHOT (1 << 0)
5a72e899
JA
957int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags);
958int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
959 unsigned int flags);
05229bee 960
165125e1 961static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
1da177e4 962{
17220ca5 963 return bdev->bd_queue; /* this is never NULL */
1da177e4
LT
964}
965
02694e86
CK
966/* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
967const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
968
d0ea6bde
DLM
969static inline unsigned int bio_zone_no(struct bio *bio)
970{
d86e716a 971 return disk_zone_no(bio->bi_bdev->bd_disk, bio->bi_iter.bi_sector);
d0ea6bde
DLM
972}
973
b85a3c1b 974static inline bool bio_straddles_zones(struct bio *bio)
d0ea6bde 975{
b85a3c1b
DLM
976 return bio_sectors(bio) &&
977 bio_zone_no(bio) !=
978 disk_zone_no(bio->bi_bdev->bd_disk, bio_end_sector(bio) - 1);
d0ea6bde 979}
6cc77e9c 980
8689461b 981/*
f70167a7
JG
982 * Return how much within the boundary is left to be used for I/O at a given
983 * offset.
8689461b 984 */
f70167a7
JG
985static inline unsigned int blk_boundary_sectors_left(sector_t offset,
986 unsigned int boundary_sectors)
8689461b 987{
f70167a7
JG
988 if (unlikely(!is_power_of_2(boundary_sectors)))
989 return boundary_sectors - sector_div(offset, boundary_sectors);
990 return boundary_sectors - (offset & (boundary_sectors - 1));
8689461b
CH
991}
992
d690cb8a
CH
993/**
994 * queue_limits_start_update - start an atomic update of queue limits
995 * @q: queue to update
996 *
997 * This functions starts an atomic update of the queue limits. It takes a lock
998 * to prevent other updates and returns a snapshot of the current limits that
999 * the caller can modify. The caller must call queue_limits_commit_update()
1000 * to finish the update.
1001 *
9c96821b 1002 * Context: process context.
d690cb8a
CH
1003 */
1004static inline struct queue_limits
1005queue_limits_start_update(struct request_queue *q)
d690cb8a
CH
1006{
1007 mutex_lock(&q->limits_lock);
1008 return q->limits;
1009}
aa427d7b
CH
1010int queue_limits_commit_update_frozen(struct request_queue *q,
1011 struct queue_limits *lim);
d690cb8a
CH
1012int queue_limits_commit_update(struct request_queue *q,
1013 struct queue_limits *lim);
631d4efb 1014int queue_limits_set(struct request_queue *q, struct queue_limits *lim);
470d2bc3 1015int blk_validate_limits(struct queue_limits *lim);
d690cb8a 1016
29306626
CH
1017/**
1018 * queue_limits_cancel_update - cancel an atomic update of queue limits
1019 * @q: queue to update
1020 *
1021 * This functions cancels an atomic update of the queue limits started by
1022 * queue_limits_start_update() and should be used when an error occurs after
1023 * starting update.
1024 */
1025static inline void queue_limits_cancel_update(struct request_queue *q)
1026{
1027 mutex_unlock(&q->limits_lock);
1028}
1029
73e3715e
CH
1030/*
1031 * These helpers are for drivers that have sloppy feature negotiation and might
1032 * have to disable DISCARD, WRITE_ZEROES or SECURE_DISCARD from the I/O
1033 * completion handler when the device returned an indicator that the respective
1034 * feature is not actually supported. They are racy and the driver needs to
1035 * cope with that. Try to avoid this scheme if you can.
1036 */
1037static inline void blk_queue_disable_discard(struct request_queue *q)
1038{
1039 q->limits.max_discard_sectors = 0;
1040}
1041
1042static inline void blk_queue_disable_secure_erase(struct request_queue *q)
1043{
1044 q->limits.max_secure_erase_sectors = 0;
1045}
1046
1047static inline void blk_queue_disable_write_zeroes(struct request_queue *q)
1048{
1049 q->limits.max_write_zeroes_sectors = 0;
1050}
1051
1da177e4
LT
1052/*
1053 * Access functions for manipulating queue properties
1054 */
d278d4a8 1055extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
b1bd055d 1056extern void blk_set_stacking_limits(struct queue_limits *lim);
c72758f3
MP
1057extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
1058 sector_t offset);
c1373f1c
CH
1059void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev,
1060 sector_t offset, const char *pfx);
242f9dcb 1061extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
2e9bc346 1062
a2247f19
DLM
1063struct blk_independent_access_ranges *
1064disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges);
1065void disk_set_independent_access_ranges(struct gendisk *disk,
1066 struct blk_independent_access_ranges *iars);
1067
09ac46c4 1068bool __must_check blk_get_queue(struct request_queue *);
165125e1 1069extern void blk_put_queue(struct request_queue *);
7a5428dc
CH
1070
1071void blk_mark_disk_dead(struct gendisk *disk);
1da177e4 1072
a3396b99
CH
1073struct rq_list {
1074 struct request *head;
1075 struct request *tail;
1076};
1077
957860cb 1078#ifdef CONFIG_BLOCK
316cc67d 1079/*
75df7136
SJ
1080 * blk_plug permits building a queue of related requests by holding the I/O
1081 * fragments for a short period. This allows merging of sequential requests
1082 * into single larger request. As the requests are moved from a per-task list to
1083 * the device's request_queue in a batch, this results in improved scalability
1084 * as the lock contention for request_queue lock is reduced.
1085 *
1086 * It is ok not to disable preemption when adding the request to the plug list
008f75a2
CH
1087 * or when attempting a merge. For details, please see schedule() where
1088 * blk_flush_plug() is called.
316cc67d 1089 */
73c10101 1090struct blk_plug {
a3396b99 1091 struct rq_list mq_list; /* blk-mq requests */
47c122e3
JA
1092
1093 /* if ios_left is > 1, we can batch tag/rq allocations */
a3396b99 1094 struct rq_list cached_rqs;
da4c8c3d 1095 u64 cur_ktime;
47c122e3
JA
1096 unsigned short nr_ios;
1097
5f0ed774 1098 unsigned short rq_count;
47c122e3 1099
ce5b009c 1100 bool multiple_queues;
dc5fc361 1101 bool has_elevator;
47c122e3
JA
1102
1103 struct list_head cb_list; /* md requires an unplug callback */
73c10101 1104};
55c022bb 1105
9cbb1750 1106struct blk_plug_cb;
74018dc3 1107typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
048c9374
N
1108struct blk_plug_cb {
1109 struct list_head list;
9cbb1750
N
1110 blk_plug_cb_fn callback;
1111 void *data;
048c9374 1112};
9cbb1750
N
1113extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
1114 void *data, int size);
73c10101 1115extern void blk_start_plug(struct blk_plug *);
47c122e3 1116extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short);
73c10101 1117extern void blk_finish_plug(struct blk_plug *);
73c10101 1118
aa8dccca
CH
1119void __blk_flush_plug(struct blk_plug *plug, bool from_schedule);
1120static inline void blk_flush_plug(struct blk_plug *plug, bool async)
73c10101 1121{
aa8dccca
CH
1122 if (plug)
1123 __blk_flush_plug(plug, async);
73c10101
JA
1124}
1125
06b23f92
JA
1126/*
1127 * tsk == current here
1128 */
1129static inline void blk_plug_invalidate_ts(struct task_struct *tsk)
1130{
1131 struct blk_plug *plug = tsk->plug;
1132
1133 if (plug)
1134 plug->cur_ktime = 0;
1135 current->flags &= ~PF_BLOCK_TS;
1136}
1137
c6bf3f0e 1138int blkdev_issue_flush(struct block_device *bdev);
1a4dcfa8
CH
1139long nr_blockdev_pages(void);
1140#else /* CONFIG_BLOCK */
1141struct blk_plug {
1142};
1143
47c122e3
JA
1144static inline void blk_start_plug_nr_ios(struct blk_plug *plug,
1145 unsigned short nr_ios)
1146{
1147}
1148
1a4dcfa8
CH
1149static inline void blk_start_plug(struct blk_plug *plug)
1150{
1151}
1152
1153static inline void blk_finish_plug(struct blk_plug *plug)
1154{
1155}
1156
008f75a2 1157static inline void blk_flush_plug(struct blk_plug *plug, bool async)
1a4dcfa8
CH
1158{
1159}
1160
06b23f92
JA
1161static inline void blk_plug_invalidate_ts(struct task_struct *tsk)
1162{
1163}
1a4dcfa8 1164
c6bf3f0e 1165static inline int blkdev_issue_flush(struct block_device *bdev)
1a4dcfa8
CH
1166{
1167 return 0;
1168}
1169
1170static inline long nr_blockdev_pages(void)
1171{
1172 return 0;
1173}
1174#endif /* CONFIG_BLOCK */
1175
71ac860a
ML
1176extern void blk_io_schedule(void);
1177
44abff2c
CH
1178int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1179 sector_t nr_sects, gfp_t gfp_mask);
1180int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1181 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop);
1182int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
1183 sector_t nr_sects, gfp_t gfp);
ee472d83
CH
1184
1185#define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
cb365b96 1186#define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
bf86bcdb 1187#define BLKDEV_ZERO_KILLABLE (1 << 2) /* interruptible by fatal signals */
ee472d83 1188
e73c23ff
CK
1189extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1190 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
ee472d83 1191 unsigned flags);
3f14d792 1192extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
ee472d83
CH
1193 sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1194
2cf6d26a
CH
1195static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1196 sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
fb2dce86 1197{
233bde21
BVA
1198 return blkdev_issue_discard(sb->s_bdev,
1199 block << (sb->s_blocksize_bits -
1200 SECTOR_SHIFT),
1201 nr_blocks << (sb->s_blocksize_bits -
1202 SECTOR_SHIFT),
44abff2c 1203 gfp_mask);
fb2dce86 1204}
e6fa0be6 1205static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
a107e5a3 1206 sector_t nr_blocks, gfp_t gfp_mask)
e6fa0be6
LC
1207{
1208 return blkdev_issue_zeroout(sb->s_bdev,
233bde21
BVA
1209 block << (sb->s_blocksize_bits -
1210 SECTOR_SHIFT),
1211 nr_blocks << (sb->s_blocksize_bits -
1212 SECTOR_SHIFT),
ee472d83 1213 gfp_mask, 0);
e6fa0be6 1214}
1da177e4 1215
fa01b1e9
CH
1216static inline bool bdev_is_partition(struct block_device *bdev)
1217{
b8c873ed 1218 return bdev_partno(bdev) != 0;
fa01b1e9
CH
1219}
1220
eb28d31b
MP
1221enum blk_default_limits {
1222 BLK_MAX_SEGMENTS = 128,
1223 BLK_SAFE_MAX_SECTORS = 255,
eb28d31b
MP
1224 BLK_MAX_SEGMENT_SIZE = 65536,
1225 BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
1226};
0e435ac2 1227
d6b9f4e6
CH
1228/*
1229 * Default upper limit for the software max_sectors limit used for
1230 * regular file system I/O. This can be increased through sysfs.
1231 *
1232 * Not to be confused with the max_hw_sector limit that is entirely
1233 * controlled by the driver, usually based on hardware limits.
1234 */
1235#define BLK_DEF_MAX_SECTORS_CAP 2560u
0a26f327 1236
2f5a65ef
CH
1237static inline struct queue_limits *bdev_limits(struct block_device *bdev)
1238{
1239 return &bdev_get_queue(bdev)->limits;
1240}
1241
af2c68fe 1242static inline unsigned long queue_segment_boundary(const struct request_queue *q)
ae03bf63 1243{
025146e1 1244 return q->limits.seg_boundary_mask;
ae03bf63
MP
1245}
1246
af2c68fe 1247static inline unsigned long queue_virt_boundary(const struct request_queue *q)
03100aad
KB
1248{
1249 return q->limits.virt_boundary_mask;
1250}
1251
af2c68fe 1252static inline unsigned int queue_max_sectors(const struct request_queue *q)
ae03bf63 1253{
025146e1 1254 return q->limits.max_sectors;
ae03bf63
MP
1255}
1256
547e2f70
CH
1257static inline unsigned int queue_max_bytes(struct request_queue *q)
1258{
1259 return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
1260}
1261
af2c68fe 1262static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
ae03bf63 1263{
025146e1 1264 return q->limits.max_hw_sectors;
ae03bf63
MP
1265}
1266
af2c68fe 1267static inline unsigned short queue_max_segments(const struct request_queue *q)
ae03bf63 1268{
8a78362c 1269 return q->limits.max_segments;
ae03bf63
MP
1270}
1271
af2c68fe 1272static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
1e739730
CH
1273{
1274 return q->limits.max_discard_segments;
1275}
1276
af2c68fe 1277static inline unsigned int queue_max_segment_size(const struct request_queue *q)
ae03bf63 1278{
025146e1 1279 return q->limits.max_segment_size;
ae03bf63
MP
1280}
1281
ccdbf0aa
DLM
1282static inline bool queue_emulates_zone_append(struct request_queue *q)
1283{
559218d4 1284 return blk_queue_is_zoned(q) && !q->limits.max_hw_zone_append_sectors;
ccdbf0aa 1285}
fe6f0cdc 1286
ccdbf0aa
DLM
1287static inline bool bdev_emulates_zone_append(struct block_device *bdev)
1288{
1289 return queue_emulates_zone_append(bdev_get_queue(bdev));
0512a75b
KB
1290}
1291
2aba0d19
CH
1292static inline unsigned int
1293bdev_max_zone_append_sectors(struct block_device *bdev)
1294{
559218d4 1295 return bdev_limits(bdev)->max_zone_append_sectors;
2aba0d19
CH
1296}
1297
65ea1b66
NA
1298static inline unsigned int bdev_max_segments(struct block_device *bdev)
1299{
1300 return queue_max_segments(bdev_get_queue(bdev));
1301}
1302
d2f526ba
KB
1303static inline unsigned short bdev_max_write_streams(struct block_device *bdev)
1304{
1305 if (bdev_is_partition(bdev))
1306 return 0;
1307 return bdev_limits(bdev)->max_write_streams;
1308}
1309
ad6bf88a 1310static inline unsigned queue_logical_block_size(const struct request_queue *q)
1da177e4 1311{
5476394a 1312 return q->limits.logical_block_size;
1da177e4
LT
1313}
1314
ad6bf88a 1315static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1da177e4 1316{
e1defc4f 1317 return queue_logical_block_size(bdev_get_queue(bdev));
1da177e4
LT
1318}
1319
af2c68fe 1320static inline unsigned int queue_physical_block_size(const struct request_queue *q)
c72758f3
MP
1321{
1322 return q->limits.physical_block_size;
1323}
1324
892b6f90 1325static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
ac481c20
MP
1326{
1327 return queue_physical_block_size(bdev_get_queue(bdev));
1328}
1329
af2c68fe 1330static inline unsigned int queue_io_min(const struct request_queue *q)
c72758f3
MP
1331{
1332 return q->limits.io_min;
1333}
1334
46fd48ab 1335static inline unsigned int bdev_io_min(struct block_device *bdev)
ac481c20
MP
1336{
1337 return queue_io_min(bdev_get_queue(bdev));
1338}
1339
af2c68fe 1340static inline unsigned int queue_io_opt(const struct request_queue *q)
c72758f3
MP
1341{
1342 return q->limits.io_opt;
1343}
1344
5a9d1b83 1345static inline unsigned int bdev_io_opt(struct block_device *bdev)
ac481c20
MP
1346{
1347 return queue_io_opt(bdev_get_queue(bdev));
1348}
1349
a805a4fa
DLM
1350static inline unsigned int
1351queue_zone_write_granularity(const struct request_queue *q)
1352{
1353 return q->limits.zone_write_granularity;
1354}
1355
1356static inline unsigned int
1357bdev_zone_write_granularity(struct block_device *bdev)
1358{
1359 return queue_zone_write_granularity(bdev_get_queue(bdev));
1360}
1361
89098b07 1362int bdev_alignment_offset(struct block_device *bdev);
5c4b4a5c 1363unsigned int bdev_discard_alignment(struct block_device *bdev);
c6e66634 1364
cf0fbf89
CH
1365static inline unsigned int bdev_max_discard_sectors(struct block_device *bdev)
1366{
2f5a65ef 1367 return bdev_limits(bdev)->max_discard_sectors;
cf0fbf89
CH
1368}
1369
7b47ef52
CH
1370static inline unsigned int bdev_discard_granularity(struct block_device *bdev)
1371{
2f5a65ef 1372 return bdev_limits(bdev)->discard_granularity;
7b47ef52
CH
1373}
1374
44abff2c
CH
1375static inline unsigned int
1376bdev_max_secure_erase_sectors(struct block_device *bdev)
1377{
2f5a65ef 1378 return bdev_limits(bdev)->max_secure_erase_sectors;
44abff2c
CH
1379}
1380
a6f0788e
CK
1381static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
1382{
2f5a65ef 1383 return bdev_limits(bdev)->max_write_zeroes_sectors;
a6f0788e
CK
1384}
1385
10f0d2a5
CH
1386static inline bool bdev_nonrot(struct block_device *bdev)
1387{
1388 return blk_queue_nonrot(bdev_get_queue(bdev));
1389}
1390
3222d8c2
CH
1391static inline bool bdev_synchronous(struct block_device *bdev)
1392{
aadd5c59 1393 return bdev->bd_disk->queue->limits.features & BLK_FEAT_SYNCHRONOUS;
3222d8c2
CH
1394}
1395
36d25489
CH
1396static inline bool bdev_stable_writes(struct block_device *bdev)
1397{
3c3e85dd
CH
1398 struct request_queue *q = bdev_get_queue(bdev);
1399
c6e56cf6
CH
1400 if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
1401 q->limits.integrity.csum_type != BLK_INTEGRITY_CSUM_NONE)
3c3e85dd 1402 return true;
1a02f3a7 1403 return q->limits.features & BLK_FEAT_STABLE_WRITES;
36d25489
CH
1404}
1405
1122c0c1
CH
1406static inline bool blk_queue_write_cache(struct request_queue *q)
1407{
1408 return (q->limits.features & BLK_FEAT_WRITE_CACHE) &&
bae1c743 1409 !(q->limits.flags & BLK_FLAG_WRITE_CACHE_DISABLED);
36d25489
CH
1410}
1411
08e688fd
CH
1412static inline bool bdev_write_cache(struct block_device *bdev)
1413{
1122c0c1 1414 return blk_queue_write_cache(bdev_get_queue(bdev));
08e688fd
CH
1415}
1416
a557e82e
CH
1417static inline bool bdev_fua(struct block_device *bdev)
1418{
2f5a65ef 1419 return bdev_limits(bdev)->features & BLK_FEAT_FUA;
a557e82e
CH
1420}
1421
568ec936
CH
1422static inline bool bdev_nowait(struct block_device *bdev)
1423{
f76af42f 1424 return bdev->bd_disk->queue->limits.features & BLK_FEAT_NOWAIT;
568ec936
CH
1425}
1426
797476b8
DLM
1427static inline bool bdev_is_zoned(struct block_device *bdev)
1428{
fea127b3 1429 return blk_queue_is_zoned(bdev_get_queue(bdev));
797476b8
DLM
1430}
1431
d67ea690
PR
1432static inline unsigned int bdev_zone_no(struct block_device *bdev, sector_t sec)
1433{
1434 return disk_zone_no(bdev->bd_disk, sec);
1435}
1436
113ab72e 1437static inline sector_t bdev_zone_sectors(struct block_device *bdev)
6a0cb1bc
HR
1438{
1439 struct request_queue *q = bdev_get_queue(bdev);
1440
de71973c
CH
1441 if (!blk_queue_is_zoned(q))
1442 return 0;
1443 return q->limits.chunk_sectors;
6cc77e9c 1444}
6a0cb1bc 1445
e29b2100
PR
1446static inline sector_t bdev_offset_from_zone_start(struct block_device *bdev,
1447 sector_t sector)
1448{
1449 return sector & (bdev_zone_sectors(bdev) - 1);
1450}
1451
b85a3c1b
DLM
1452static inline sector_t bio_offset_from_zone_start(struct bio *bio)
1453{
1454 return bdev_offset_from_zone_start(bio->bi_bdev,
1455 bio->bi_iter.bi_sector);
1456}
1457
e29b2100
PR
1458static inline bool bdev_is_zone_start(struct block_device *bdev,
1459 sector_t sector)
1460{
1461 return bdev_offset_from_zone_start(bdev, sector) == 0;
1462}
1463
241b9b58
BVA
1464/* Check whether @sector is a multiple of the zone size. */
1465static inline bool bdev_is_zone_aligned(struct block_device *bdev,
1466 sector_t sector)
1467{
1468 return bdev_is_zone_start(bdev, sector);
1469}
1470
f3d9bf05
DLM
1471/**
1472 * bdev_zone_is_seq - check if a sector belongs to a sequential write zone
1473 * @bdev: block device to check
1474 * @sector: sector number
1475 *
1476 * Check if @sector on @bdev is contained in a sequential write required zone.
1477 */
1478static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector)
1479{
1480 bool is_seq = false;
1481
1482#if IS_ENABLED(CONFIG_BLK_DEV_ZONED)
1483 if (bdev_is_zoned(bdev)) {
1484 struct gendisk *disk = bdev->bd_disk;
1485 unsigned long *bitmap;
1486
1487 rcu_read_lock();
1488 bitmap = rcu_dereference(disk->conv_zones_bitmap);
1489 is_seq = !bitmap ||
1490 !test_bit(disk_zone_no(disk, sector), bitmap);
1491 rcu_read_unlock();
1492 }
1493#endif
1494
1495 return is_seq;
1496}
1497
b76b840f
DLM
1498int blk_zone_issue_zeroout(struct block_device *bdev, sector_t sector,
1499 sector_t nr_sects, gfp_t gfp_mask);
1500
ed5db174 1501static inline unsigned int queue_dma_alignment(const struct request_queue *q)
1da177e4 1502{
abfc9d81 1503 return q->limits.dma_alignment;
1da177e4
LT
1504}
1505
9da3d1e9
JG
1506static inline unsigned int
1507queue_atomic_write_unit_max_bytes(const struct request_queue *q)
1508{
1509 return q->limits.atomic_write_unit_max;
1510}
1511
1512static inline unsigned int
1513queue_atomic_write_unit_min_bytes(const struct request_queue *q)
1514{
1515 return q->limits.atomic_write_unit_min;
1516}
1517
1518static inline unsigned int
1519queue_atomic_write_boundary_bytes(const struct request_queue *q)
1520{
1521 return q->limits.atomic_write_boundary_sectors << SECTOR_SHIFT;
1522}
1523
1524static inline unsigned int
1525queue_atomic_write_max_bytes(const struct request_queue *q)
1526{
1527 return q->limits.atomic_write_max_sectors << SECTOR_SHIFT;
1da177e4
LT
1528}
1529
4a2dcc35
KB
1530static inline unsigned int bdev_dma_alignment(struct block_device *bdev)
1531{
1532 return queue_dma_alignment(bdev_get_queue(bdev));
1533}
1534
5debd969
KB
1535static inline bool bdev_iter_is_aligned(struct block_device *bdev,
1536 struct iov_iter *iter)
1537{
1538 return iov_iter_is_aligned(iter, bdev_dma_alignment(bdev),
1539 bdev_logical_block_size(bdev) - 1);
1540}
1541
e769489a
CH
1542static inline unsigned int
1543blk_lim_dma_alignment_and_pad(struct queue_limits *lim)
e94b45d0
CH
1544{
1545 return lim->dma_alignment | lim->dma_pad_mask;
1546}
1547
da77d9b2 1548static inline bool blk_rq_aligned(struct request_queue *q, unsigned long addr,
87904074
FT
1549 unsigned int len)
1550{
e94b45d0
CH
1551 unsigned int alignment = blk_lim_dma_alignment_and_pad(&q->limits);
1552
14417799 1553 return !(addr & alignment) && !(len & alignment);
87904074
FT
1554}
1555
1da177e4
LT
1556/* assumes size > 256 */
1557static inline unsigned int blksize_bits(unsigned int size)
1558{
adff2158 1559 return order_base_2(size >> SECTOR_SHIFT) + SECTOR_SHIFT;
1da177e4
LT
1560}
1561
59c3d45e 1562int kblockd_schedule_work(struct work_struct *work);
818cd1cb 1563int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
1da177e4 1564
1da177e4
LT
1565#define MODULE_ALIAS_BLOCKDEV(major,minor) \
1566 MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
1567#define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
1568 MODULE_ALIAS("block-major-" __stringify(major) "-*")
1569
d145dc23
ST
1570#ifdef CONFIG_BLK_INLINE_ENCRYPTION
1571
cb77cb5a
EB
1572bool blk_crypto_register(struct blk_crypto_profile *profile,
1573 struct request_queue *q);
d145dc23 1574
d145dc23
ST
1575#else /* CONFIG_BLK_INLINE_ENCRYPTION */
1576
cb77cb5a
EB
1577static inline bool blk_crypto_register(struct blk_crypto_profile *profile,
1578 struct request_queue *q)
d145dc23
ST
1579{
1580 return true;
1581}
1582
d145dc23
ST
1583#endif /* CONFIG_BLK_INLINE_ENCRYPTION */
1584
9208d414
CH
1585enum blk_unique_id {
1586 /* these match the Designator Types specified in SPC */
1587 BLK_UID_T10 = 1,
1588 BLK_UID_EUI64 = 2,
1589 BLK_UID_NAA = 3,
1590};
1591
08f85851 1592struct block_device_operations {
3e08773c 1593 void (*submit_bio)(struct bio *bio);
69fe0f29
ML
1594 int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
1595 unsigned int flags);
05bdb996 1596 int (*open)(struct gendisk *disk, blk_mode_t mode);
ae220766 1597 void (*release)(struct gendisk *disk);
05bdb996
CH
1598 int (*ioctl)(struct block_device *bdev, blk_mode_t mode,
1599 unsigned cmd, unsigned long arg);
1600 int (*compat_ioctl)(struct block_device *bdev, blk_mode_t mode,
1601 unsigned cmd, unsigned long arg);
77ea887e
TH
1602 unsigned int (*check_events) (struct gendisk *disk,
1603 unsigned int clearing);
c3e33e04 1604 void (*unlock_native_capacity) (struct gendisk *);
08f85851 1605 int (*getgeo)(struct block_device *, struct hd_geometry *);
e00adcad 1606 int (*set_read_only)(struct block_device *bdev, bool ro);
76792055 1607 void (*free_disk)(struct gendisk *disk);
b3a27d05
NG
1608 /* this callback is with swap_lock and sometimes page table lock held */
1609 void (*swap_slot_free_notify) (struct block_device *, unsigned long);
e76239a3 1610 int (*report_zones)(struct gendisk *, sector_t sector,
d4100351 1611 unsigned int nr_zones, report_zones_cb cb, void *data);
050a4f34 1612 char *(*devnode)(struct gendisk *disk, umode_t *mode);
9208d414
CH
1613 /* returns the length of the identifier or a negative errno: */
1614 int (*get_unique_id)(struct gendisk *disk, u8 id[16],
1615 enum blk_unique_id id_type);
08f85851 1616 struct module *owner;
bbd3e064 1617 const struct pr_ops *pr_ops;
0bdfbca8
DO
1618
1619 /*
1620 * Special callback for probing GPT entry at a given sector.
1621 * Needed by Android devices, used by GPT scanner and MMC blk
1622 * driver.
1623 */
1624 int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
08f85851
AV
1625};
1626
ee6a129d 1627#ifdef CONFIG_COMPAT
05bdb996 1628extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t,
ee6a129d
AB
1629 unsigned int, unsigned long);
1630#else
1631#define blkdev_compat_ptr_ioctl NULL
1632#endif
1633
0619317f
JA
1634static inline void blk_wake_io_task(struct task_struct *waiter)
1635{
1636 /*
1637 * If we're polling, the task itself is doing the completions. For
1638 * that case, we don't need to signal a wakeup, it's enough to just
1639 * mark us as RUNNING.
1640 */
1641 if (waiter == current)
1642 __set_current_state(TASK_RUNNING);
1643 else
1644 wake_up_process(waiter);
1645}
1646
5f275713 1647unsigned long bdev_start_io_acct(struct block_device *bdev, enum req_op op,
5f0614a5 1648 unsigned long start_time);
77e7ffd7 1649void bdev_end_io_acct(struct block_device *bdev, enum req_op op,
5f275713 1650 unsigned int sectors, unsigned long start_time);
956d510e 1651
99dfc43e
CH
1652unsigned long bio_start_io_acct(struct bio *bio);
1653void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1654 struct block_device *orig_bdev);
956d510e
CH
1655
1656/**
1657 * bio_end_io_acct - end I/O accounting for bio based drivers
1658 * @bio: bio to end account for
b42c1fc3 1659 * @start_time: start time returned by bio_start_io_acct()
956d510e
CH
1660 */
1661static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
1662{
99dfc43e 1663 return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
956d510e 1664}
956d510e 1665
e03463d2 1666int bdev_validate_blocksize(struct block_device *bdev, int block_size);
ead083ae 1667int set_blocksize(struct file *file, int size);
3f1266f1 1668
4e7b5671 1669int lookup_bdev(const char *pathname, dev_t *dev);
3f1266f1
CH
1670
1671void blkdev_show(struct seq_file *seqf, off_t offset);
1672
1673#define BDEVNAME_SIZE 32 /* Largest string for a blockdev identifier */
1674#define BDEVT_SIZE 10 /* Largest string for MAJ:MIN for blkdev */
1675#ifdef CONFIG_BLOCK
1676#define BLKDEV_MAJOR_MAX 512
1677#else
1678#define BLKDEV_MAJOR_MAX 0
1679#endif
1680
0718afd4 1681struct blk_holder_ops {
d8530de5 1682 void (*mark_dead)(struct block_device *bdev, bool surprise);
2142b88c
CH
1683
1684 /*
1685 * Sync the file system mounted on the block device.
1686 */
1687 void (*sync)(struct block_device *bdev);
a30561a9
CB
1688
1689 /*
1690 * Freeze the file system mounted on the block device.
1691 */
1692 int (*freeze)(struct block_device *bdev);
1693
1694 /*
1695 * Thaw the file system mounted on the block device.
1696 */
1697 int (*thaw)(struct block_device *bdev);
0718afd4
CH
1698};
1699
e419cf3e
CB
1700/*
1701 * For filesystems using @fs_holder_ops, the @holder argument passed to
1702 * helpers used to open and claim block devices via
1703 * bd_prepare_to_claim() must point to a superblock.
1704 */
7ecd0b6f
CH
1705extern const struct blk_holder_ops fs_holder_ops;
1706
3f0b3e78
CH
1707/*
1708 * Return the correct open flags for blkdev_get_by_* for super block flags
1709 * as stored in sb->s_flags.
1710 */
1711#define sb_open_mode(flags) \
6f861765
JK
1712 (BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES | \
1713 (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE))
3f0b3e78 1714
f3a60882
CB
1715struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
1716 const struct blk_holder_ops *hops);
1717struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode,
1718 void *holder, const struct blk_holder_ops *hops);
0718afd4
CH
1719int bd_prepare_to_claim(struct block_device *bdev, void *holder,
1720 const struct blk_holder_ops *hops);
37c3fc9a 1721void bd_abort_claiming(struct block_device *bdev, void *holder);
3f1266f1 1722
621c1f42 1723struct block_device *I_BDEV(struct inode *inode);
f3a60882 1724struct block_device *file_bdev(struct file *bdev_file);
186ddac2
YK
1725bool disk_live(struct gendisk *disk);
1726unsigned int block_size(struct block_device *bdev);
3f1266f1
CH
1727
1728#ifdef CONFIG_BLOCK
1729void invalidate_bdev(struct block_device *bdev);
1730int sync_blockdev(struct block_device *bdev);
97d6fb1b 1731int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend);
70164eb6 1732int sync_blockdev_nowait(struct block_device *bdev);
1e03a36b 1733void sync_bdevs(bool wait);
777d0961 1734void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask);
322cbb50 1735void printk_all_partitions(void);
2577f53f 1736int __init early_lookup_bdev(const char *pathname, dev_t *dev);
3f1266f1
CH
1737#else
1738static inline void invalidate_bdev(struct block_device *bdev)
1739{
1740}
1741static inline int sync_blockdev(struct block_device *bdev)
1742{
1743 return 0;
1744}
70164eb6
CH
1745static inline int sync_blockdev_nowait(struct block_device *bdev)
1746{
1747 return 0;
1748}
1e03a36b
CH
1749static inline void sync_bdevs(bool wait)
1750{
1751}
777d0961
CH
1752static inline void bdev_statx(const struct path *path, struct kstat *stat,
1753 u32 request_mask)
2d985f8c
EB
1754{
1755}
322cbb50
CH
1756static inline void printk_all_partitions(void)
1757{
1758}
cf056a43
CH
1759static inline int early_lookup_bdev(const char *pathname, dev_t *dev)
1760{
1761 return -EINVAL;
1762}
322cbb50
CH
1763#endif /* CONFIG_BLOCK */
1764
982c3b30
CB
1765int bdev_freeze(struct block_device *bdev);
1766int bdev_thaw(struct block_device *bdev);
22650a99 1767void bdev_fput(struct file *bdev_file);
3f1266f1 1768
5a72e899 1769struct io_comp_batch {
a3396b99 1770 struct rq_list req_list;
5a72e899
JA
1771 bool need_ts;
1772 void (*complete)(struct io_comp_batch *);
1773};
1774
6564862d
JG
1775static inline bool blk_atomic_write_start_sect_aligned(sector_t sector,
1776 struct queue_limits *limits)
1777{
1778 unsigned int alignment = max(limits->atomic_write_hw_unit_min,
1779 limits->atomic_write_hw_boundary);
1780
1781 return IS_ALIGNED(sector, alignment >> SECTOR_SHIFT);
1782}
1783
9da3d1e9
JG
1784static inline bool bdev_can_atomic_write(struct block_device *bdev)
1785{
1786 struct request_queue *bd_queue = bdev->bd_queue;
1787 struct queue_limits *limits = &bd_queue->limits;
1788
1789 if (!limits->atomic_write_unit_min)
1790 return false;
1791
6564862d
JG
1792 if (bdev_is_partition(bdev))
1793 return blk_atomic_write_start_sect_aligned(bdev->bd_start_sect,
1794 limits);
9da3d1e9
JG
1795
1796 return true;
1797}
1798
1eadb157
JG
1799static inline unsigned int
1800bdev_atomic_write_unit_min_bytes(struct block_device *bdev)
1801{
1802 if (!bdev_can_atomic_write(bdev))
1803 return 0;
1804 return queue_atomic_write_unit_min_bytes(bdev_get_queue(bdev));
1805}
1806
1807static inline unsigned int
1808bdev_atomic_write_unit_max_bytes(struct block_device *bdev)
1809{
1810 if (!bdev_can_atomic_write(bdev))
1811 return 0;
1812 return queue_atomic_write_unit_max_bytes(bdev_get_queue(bdev));
1813}
1814
5a72e899
JA
1815#define DEFINE_IO_COMP_BATCH(name) struct io_comp_batch name = { }
1816
3f1266f1 1817#endif /* _LINUX_BLKDEV_H */