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