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