1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2322cbb50SChristoph Hellwig /*
3322cbb50SChristoph Hellwig * Portions Copyright (C) 1992 Drew Eckhardt
4322cbb50SChristoph Hellwig */
51da177e4SLinus Torvalds #ifndef _LINUX_BLKDEV_H
61da177e4SLinus Torvalds #define _LINUX_BLKDEV_H
71da177e4SLinus Torvalds
8322cbb50SChristoph Hellwig #include <linux/types.h>
9322cbb50SChristoph Hellwig #include <linux/blk_types.h>
10322cbb50SChristoph Hellwig #include <linux/device.h>
111da177e4SLinus Torvalds #include <linux/list.h>
12320ae51fSJens Axboe #include <linux/llist.h>
13b296a6d5SAndy Shevchenko #include <linux/minmax.h>
141da177e4SLinus Torvalds #include <linux/timer.h>
151da177e4SLinus Torvalds #include <linux/workqueue.h>
161da177e4SLinus Torvalds #include <linux/wait.h>
171da177e4SLinus Torvalds #include <linux/bio.h>
183e6053d7SHugh Dickins #include <linux/gfp.h>
19322cbb50SChristoph Hellwig #include <linux/kdev_t.h>
20548bc8e1STejun Heo #include <linux/rcupdate.h>
21add703fdSTejun Heo #include <linux/percpu-refcount.h>
226a0cb1bcSHannes Reinecke #include <linux/blkzoned.h>
23322cbb50SChristoph Hellwig #include <linux/sched.h>
24d97e594cSJohn Garry #include <linux/sbitmap.h>
25322cbb50SChristoph Hellwig #include <linux/uuid.h>
26322cbb50SChristoph Hellwig #include <linux/xarray.h>
27f3a60882SChristian Brauner #include <linux/file.h>
28f1be1788SMing Lei #include <linux/lockdep.h>
291da177e4SLinus Torvalds
30de477254SPaul Gortmaker struct module;
311da177e4SLinus Torvalds struct request_queue;
321da177e4SLinus Torvalds struct elevator_queue;
332056a782SJens Axboe struct blk_trace;
343d6392cfSJens Axboe struct request;
353d6392cfSJens Axboe struct sg_io_hdr;
363c798398STejun Heo struct blkcg_gq;
377c94e1c1SMing Lei struct blk_flush_queue;
383e08773cSChristoph Hellwig struct kiocb;
39bbd3e064SChristoph Hellwig struct pr_ops;
40a7905043SJosef Bacik struct rq_qos;
4134dbad5dSOmar Sandoval struct blk_queue_stats;
4234dbad5dSOmar Sandoval struct blk_stat_callback;
43cb77cb5aSEric Biggers struct blk_crypto_profile;
441da177e4SLinus Torvalds
45322cbb50SChristoph Hellwig extern const struct device_type disk_type;
46cdb37f73SThomas Weißschuh extern const struct device_type part_type;
47f8c7511dSRicardo B. Marliere extern const struct class block_class;
48322cbb50SChristoph Hellwig
498bd435b3STejun Heo /*
508bd435b3STejun Heo * Maximum number of blkcg policies allowed to be registered concurrently.
518bd435b3STejun Heo * Defined here to simplify include dependency.
528bd435b3STejun Heo */
53ec645dc9SOleksandr Natalenko #define BLKCG_MAX_POLS 6
548bd435b3STejun Heo
55322cbb50SChristoph Hellwig #define DISK_MAX_PARTS 256
56322cbb50SChristoph Hellwig #define DISK_NAME_LEN 32
57322cbb50SChristoph Hellwig
58322cbb50SChristoph Hellwig #define PARTITION_META_INFO_VOLNAMELTH 64
59322cbb50SChristoph Hellwig /*
60322cbb50SChristoph Hellwig * Enough for the string representation of any kind of UUID plus NULL.
61322cbb50SChristoph Hellwig * EFI UUID is 36 characters. MSDOS UUID is 11 characters.
62322cbb50SChristoph Hellwig */
63322cbb50SChristoph Hellwig #define PARTITION_META_INFO_UUIDLTH (UUID_STRING_LEN + 1)
64322cbb50SChristoph Hellwig
65322cbb50SChristoph Hellwig struct partition_meta_info {
66322cbb50SChristoph Hellwig char uuid[PARTITION_META_INFO_UUIDLTH];
67322cbb50SChristoph Hellwig u8 volname[PARTITION_META_INFO_VOLNAMELTH];
68322cbb50SChristoph Hellwig };
69322cbb50SChristoph Hellwig
70322cbb50SChristoph Hellwig /**
71322cbb50SChristoph Hellwig * DOC: genhd capability flags
72322cbb50SChristoph Hellwig *
73322cbb50SChristoph Hellwig * ``GENHD_FL_REMOVABLE``: indicates that the block device gives access to
74322cbb50SChristoph Hellwig * removable media. When set, the device remains present even when media is not
75322cbb50SChristoph Hellwig * inserted. Shall not be set for devices which are removed entirely when the
76322cbb50SChristoph Hellwig * media is removed.
77322cbb50SChristoph Hellwig *
78322cbb50SChristoph Hellwig * ``GENHD_FL_HIDDEN``: the block device is hidden; it doesn't produce events,
79322cbb50SChristoph Hellwig * doesn't appear in sysfs, and can't be opened from userspace or using
80322cbb50SChristoph Hellwig * blkdev_get*. Used for the underlying components of multipath devices.
81322cbb50SChristoph Hellwig *
82322cbb50SChristoph Hellwig * ``GENHD_FL_NO_PART``: partition support is disabled. The kernel will not
83322cbb50SChristoph Hellwig * scan for partitions from add_disk, and users can't add partitions manually.
84322cbb50SChristoph Hellwig *
85322cbb50SChristoph Hellwig */
86322cbb50SChristoph Hellwig enum {
87322cbb50SChristoph Hellwig GENHD_FL_REMOVABLE = 1 << 0,
88322cbb50SChristoph Hellwig GENHD_FL_HIDDEN = 1 << 1,
89322cbb50SChristoph Hellwig GENHD_FL_NO_PART = 1 << 2,
90322cbb50SChristoph Hellwig };
91322cbb50SChristoph Hellwig
92322cbb50SChristoph Hellwig enum {
93322cbb50SChristoph Hellwig DISK_EVENT_MEDIA_CHANGE = 1 << 0, /* media changed */
94322cbb50SChristoph Hellwig DISK_EVENT_EJECT_REQUEST = 1 << 1, /* eject requested */
95322cbb50SChristoph Hellwig };
96322cbb50SChristoph Hellwig
97322cbb50SChristoph Hellwig enum {
98322cbb50SChristoph Hellwig /* Poll even if events_poll_msecs is unset */
99322cbb50SChristoph Hellwig DISK_EVENT_FLAG_POLL = 1 << 0,
100322cbb50SChristoph Hellwig /* Forward events to udev */
101322cbb50SChristoph Hellwig DISK_EVENT_FLAG_UEVENT = 1 << 1,
102322cbb50SChristoph Hellwig /* Block event polling when open for exclusive write */
103322cbb50SChristoph Hellwig DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE = 1 << 2,
104322cbb50SChristoph Hellwig };
105322cbb50SChristoph Hellwig
106322cbb50SChristoph Hellwig struct disk_events;
107322cbb50SChristoph Hellwig struct badblocks;
108322cbb50SChristoph Hellwig
109e9f5f44aSChristoph Hellwig enum blk_integrity_checksum {
110e9f5f44aSChristoph Hellwig BLK_INTEGRITY_CSUM_NONE = 0,
111e9f5f44aSChristoph Hellwig BLK_INTEGRITY_CSUM_IP = 1,
112e9f5f44aSChristoph Hellwig BLK_INTEGRITY_CSUM_CRC = 2,
113e9f5f44aSChristoph Hellwig BLK_INTEGRITY_CSUM_CRC64 = 3,
114e9f5f44aSChristoph Hellwig } __packed ;
115e9f5f44aSChristoph Hellwig
116322cbb50SChristoph Hellwig struct blk_integrity {
117322cbb50SChristoph Hellwig unsigned char flags;
118e9f5f44aSChristoph Hellwig enum blk_integrity_checksum csum_type;
119322cbb50SChristoph Hellwig unsigned char tuple_size;
12060d21aacSKanchan Joshi unsigned char pi_offset;
121322cbb50SChristoph Hellwig unsigned char interval_exp;
122322cbb50SChristoph Hellwig unsigned char tag_size;
123322cbb50SChristoph Hellwig };
124322cbb50SChristoph Hellwig
12505bdb996SChristoph Hellwig typedef unsigned int __bitwise blk_mode_t;
12605bdb996SChristoph Hellwig
12705bdb996SChristoph Hellwig /* open for reading */
12805bdb996SChristoph Hellwig #define BLK_OPEN_READ ((__force blk_mode_t)(1 << 0))
12905bdb996SChristoph Hellwig /* open for writing */
13005bdb996SChristoph Hellwig #define BLK_OPEN_WRITE ((__force blk_mode_t)(1 << 1))
13105bdb996SChristoph Hellwig /* open exclusively (vs other exclusive openers */
13205bdb996SChristoph Hellwig #define BLK_OPEN_EXCL ((__force blk_mode_t)(1 << 2))
13305bdb996SChristoph Hellwig /* opened with O_NDELAY */
13405bdb996SChristoph Hellwig #define BLK_OPEN_NDELAY ((__force blk_mode_t)(1 << 3))
13505bdb996SChristoph Hellwig /* open for "writes" only for ioctls (specialy hack for floppy.c) */
13605bdb996SChristoph Hellwig #define BLK_OPEN_WRITE_IOCTL ((__force blk_mode_t)(1 << 4))
137ed5cc702SJan Kara /* open is exclusive wrt all other BLK_OPEN_WRITE opens to the device */
138ed5cc702SJan Kara #define BLK_OPEN_RESTRICT_WRITES ((__force blk_mode_t)(1 << 5))
139752863bdSChristoph Hellwig /* return partition scanning errors */
140752863bdSChristoph Hellwig #define BLK_OPEN_STRICT_SCAN ((__force blk_mode_t)(1 << 6))
14105bdb996SChristoph Hellwig
142322cbb50SChristoph Hellwig struct gendisk {
143322cbb50SChristoph Hellwig /*
144322cbb50SChristoph Hellwig * major/first_minor/minors should not be set by any new driver, the
145322cbb50SChristoph Hellwig * block core will take care of allocating them automatically.
146322cbb50SChristoph Hellwig */
147322cbb50SChristoph Hellwig int major;
148322cbb50SChristoph Hellwig int first_minor;
149322cbb50SChristoph Hellwig int minors;
150322cbb50SChristoph Hellwig
151322cbb50SChristoph Hellwig char disk_name[DISK_NAME_LEN]; /* name of major driver */
152322cbb50SChristoph Hellwig
153322cbb50SChristoph Hellwig unsigned short events; /* supported events */
154322cbb50SChristoph Hellwig unsigned short event_flags; /* flags related to event processing */
155322cbb50SChristoph Hellwig
156322cbb50SChristoph Hellwig struct xarray part_tbl;
157322cbb50SChristoph Hellwig struct block_device *part0;
158322cbb50SChristoph Hellwig
159322cbb50SChristoph Hellwig const struct block_device_operations *fops;
160322cbb50SChristoph Hellwig struct request_queue *queue;
161322cbb50SChristoph Hellwig void *private_data;
162322cbb50SChristoph Hellwig
16346754bd0SChristoph Hellwig struct bio_set bio_split;
16446754bd0SChristoph Hellwig
165322cbb50SChristoph Hellwig int flags;
166322cbb50SChristoph Hellwig unsigned long state;
167322cbb50SChristoph Hellwig #define GD_NEED_PART_SCAN 0
168322cbb50SChristoph Hellwig #define GD_READ_ONLY 1
169322cbb50SChristoph Hellwig #define GD_DEAD 2
170322cbb50SChristoph Hellwig #define GD_NATIVE_CAPACITY 3
17176792055SChristoph Hellwig #define GD_ADDED 4
172b9684a71SChristoph Hellwig #define GD_SUPPRESS_PART_SCAN 5
1736f8191fdSChristoph Hellwig #define GD_OWNS_QUEUE 6
174322cbb50SChristoph Hellwig
175322cbb50SChristoph Hellwig struct mutex open_mutex; /* open/close mutex */
176322cbb50SChristoph Hellwig unsigned open_partitions; /* number of open partitions */
177322cbb50SChristoph Hellwig
178322cbb50SChristoph Hellwig struct backing_dev_info *bdi;
1792bd85221SChristoph Hellwig struct kobject queue_kobj; /* the queue/ directory */
180322cbb50SChristoph Hellwig struct kobject *slave_dir;
181322cbb50SChristoph Hellwig #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
182322cbb50SChristoph Hellwig struct list_head slave_bdevs;
183322cbb50SChristoph Hellwig #endif
184322cbb50SChristoph Hellwig struct timer_rand_state *random;
185322cbb50SChristoph Hellwig atomic_t sync_io; /* RAID */
186322cbb50SChristoph Hellwig struct disk_events *ev;
187d86e716aSChristoph Hellwig
188d86e716aSChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
189d86e716aSChristoph Hellwig /*
19002ccd7c3SDamien Le Moal * Zoned block device information. Reads of this information must be
19102ccd7c3SDamien Le Moal * protected with blk_queue_enter() / blk_queue_exit(). Modifying this
19202ccd7c3SDamien Le Moal * information is only allowed while no requests are being processed.
19302ccd7c3SDamien Le Moal * See also blk_mq_freeze_queue() and blk_mq_unfreeze_queue().
194d86e716aSChristoph Hellwig */
195d86e716aSChristoph Hellwig unsigned int nr_zones;
196ecfe43b1SDamien Le Moal unsigned int zone_capacity;
19729459c3eSDamien Le Moal unsigned int last_zone_capacity;
198d7cb6d74SDamien Le Moal unsigned long __rcu *conv_zones_bitmap;
199dd291d77SDamien Le Moal unsigned int zone_wplugs_hash_bits;
200a6aa36e9SDamien Le Moal atomic_t nr_zone_wplugs;
201dd291d77SDamien Le Moal spinlock_t zone_wplugs_lock;
202dd291d77SDamien Le Moal struct mempool_s *zone_wplugs_pool;
203dd291d77SDamien Le Moal struct hlist_head *zone_wplugs_hash;
204a8f59e5aSDamien Le Moal struct workqueue_struct *zone_wplugs_wq;
205d86e716aSChristoph Hellwig #endif /* CONFIG_BLK_DEV_ZONED */
206d86e716aSChristoph Hellwig
207322cbb50SChristoph Hellwig #if IS_ENABLED(CONFIG_CDROM)
208322cbb50SChristoph Hellwig struct cdrom_device_info *cdi;
209322cbb50SChristoph Hellwig #endif
210322cbb50SChristoph Hellwig int node_id;
211322cbb50SChristoph Hellwig struct badblocks *bb;
212322cbb50SChristoph Hellwig struct lockdep_map lockdep_map;
213322cbb50SChristoph Hellwig u64 diskseq;
21405bdb996SChristoph Hellwig blk_mode_t open_mode;
2156a27d28cSChristoph Hellwig
2166a27d28cSChristoph Hellwig /*
2176a27d28cSChristoph Hellwig * Independent sector access ranges. This is always NULL for
2186a27d28cSChristoph Hellwig * devices that do not have multiple independent access ranges.
2196a27d28cSChristoph Hellwig */
2206a27d28cSChristoph Hellwig struct blk_independent_access_ranges *ia_ranges;
221322cbb50SChristoph Hellwig };
222322cbb50SChristoph Hellwig
223dbdc1be3SChristoph Hellwig /**
224dbdc1be3SChristoph Hellwig * disk_openers - returns how many openers are there for a disk
225dbdc1be3SChristoph Hellwig * @disk: disk to check
226dbdc1be3SChristoph Hellwig *
227dbdc1be3SChristoph Hellwig * This returns the number of openers for a disk. Note that this value is only
228dbdc1be3SChristoph Hellwig * stable if disk->open_mutex is held.
229dbdc1be3SChristoph Hellwig *
230dbdc1be3SChristoph Hellwig * Note: Due to a quirk in the block layer open code, each open partition is
231dbdc1be3SChristoph Hellwig * only counted once even if there are multiple openers.
232dbdc1be3SChristoph Hellwig */
disk_openers(struct gendisk * disk)233dbdc1be3SChristoph Hellwig static inline unsigned int disk_openers(struct gendisk *disk)
234dbdc1be3SChristoph Hellwig {
2359acf381fSChristoph Hellwig return atomic_read(&disk->part0->bd_openers);
236dbdc1be3SChristoph Hellwig }
237dbdc1be3SChristoph Hellwig
238140ce28dSChristoph Hellwig /**
239140ce28dSChristoph Hellwig * disk_has_partscan - return %true if partition scanning is enabled on a disk
240140ce28dSChristoph Hellwig * @disk: disk to check
241140ce28dSChristoph Hellwig *
242140ce28dSChristoph Hellwig * Returns %true if partitions scanning is enabled for @disk, or %false if
243140ce28dSChristoph Hellwig * partition scanning is disabled either permanently or temporarily.
244140ce28dSChristoph Hellwig */
disk_has_partscan(struct gendisk * disk)245140ce28dSChristoph Hellwig static inline bool disk_has_partscan(struct gendisk *disk)
246140ce28dSChristoph Hellwig {
247140ce28dSChristoph Hellwig return !(disk->flags & (GENHD_FL_NO_PART | GENHD_FL_HIDDEN)) &&
248140ce28dSChristoph Hellwig !test_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
249140ce28dSChristoph Hellwig }
250140ce28dSChristoph Hellwig
251322cbb50SChristoph Hellwig /*
252322cbb50SChristoph Hellwig * The gendisk is refcounted by the part0 block_device, and the bd_device
253322cbb50SChristoph Hellwig * therein is also used for device model presentation in sysfs.
254322cbb50SChristoph Hellwig */
255322cbb50SChristoph Hellwig #define dev_to_disk(device) \
256322cbb50SChristoph Hellwig (dev_to_bdev(device)->bd_disk)
257322cbb50SChristoph Hellwig #define disk_to_dev(disk) \
258322cbb50SChristoph Hellwig (&((disk)->part0->bd_device))
259322cbb50SChristoph Hellwig
260322cbb50SChristoph Hellwig #if IS_REACHABLE(CONFIG_CDROM)
261322cbb50SChristoph Hellwig #define disk_to_cdi(disk) ((disk)->cdi)
262322cbb50SChristoph Hellwig #else
263322cbb50SChristoph Hellwig #define disk_to_cdi(disk) NULL
264322cbb50SChristoph Hellwig #endif
265322cbb50SChristoph Hellwig
disk_devt(struct gendisk * disk)266322cbb50SChristoph Hellwig static inline dev_t disk_devt(struct gendisk *disk)
267322cbb50SChristoph Hellwig {
268322cbb50SChristoph Hellwig return MKDEV(disk->major, disk->first_minor);
269322cbb50SChristoph Hellwig }
270322cbb50SChristoph Hellwig
27147dd6753SLuis Chamberlain /*
27247dd6753SLuis Chamberlain * We should strive for 1 << (PAGE_SHIFT + MAX_PAGECACHE_ORDER)
27347dd6753SLuis Chamberlain * however we constrain this to what we can validate and test.
27447dd6753SLuis Chamberlain */
27547dd6753SLuis Chamberlain #define BLK_MAX_BLOCK_SIZE SZ_64K
27647dd6753SLuis Chamberlain
277fe3d508bSJohn Garry /* blk_validate_limits() validates bsize, so drivers don't usually need to */
blk_validate_block_size(unsigned long bsize)27837ae5a0fSTetsuo Handa static inline int blk_validate_block_size(unsigned long bsize)
279570b1cacSXie Yongji {
28047dd6753SLuis Chamberlain if (bsize < 512 || bsize > BLK_MAX_BLOCK_SIZE || !is_power_of_2(bsize))
281570b1cacSXie Yongji return -EINVAL;
282570b1cacSXie Yongji
283570b1cacSXie Yongji return 0;
284570b1cacSXie Yongji }
285570b1cacSXie Yongji
blk_op_is_passthrough(blk_opf_t op)28616458cf3SBart Van Assche static inline bool blk_op_is_passthrough(blk_opf_t op)
28714cb0dc6SMing Lei {
288da6269daSChristoph Hellwig op &= REQ_OP_MASK;
28914cb0dc6SMing Lei return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT;
29014cb0dc6SMing Lei }
29114cb0dc6SMing Lei
2921122c0c1SChristoph Hellwig /* flags set by the driver in queue_limits.features */
293fcf865e3SChristoph Hellwig typedef unsigned int __bitwise blk_features_t;
294fcf865e3SChristoph Hellwig
2951122c0c1SChristoph Hellwig /* supports a volatile write cache */
296fcf865e3SChristoph Hellwig #define BLK_FEAT_WRITE_CACHE ((__force blk_features_t)(1u << 0))
2971122c0c1SChristoph Hellwig
2981122c0c1SChristoph Hellwig /* supports passing on the FUA bit */
299fcf865e3SChristoph Hellwig #define BLK_FEAT_FUA ((__force blk_features_t)(1u << 1))
300bd4a633bSChristoph Hellwig
301bd4a633bSChristoph Hellwig /* rotational device (hard drive or floppy) */
302fcf865e3SChristoph Hellwig #define BLK_FEAT_ROTATIONAL ((__force blk_features_t)(1u << 2))
30339a9f1c3SChristoph Hellwig
30439a9f1c3SChristoph Hellwig /* contributes to the random number pool */
305fcf865e3SChristoph Hellwig #define BLK_FEAT_ADD_RANDOM ((__force blk_features_t)(1u << 3))
306cdb24979SChristoph Hellwig
307cdb24979SChristoph Hellwig /* do disk/partitions IO accounting */
308fcf865e3SChristoph Hellwig #define BLK_FEAT_IO_STAT ((__force blk_features_t)(1u << 4))
3091a02f3a7SChristoph Hellwig
3101a02f3a7SChristoph Hellwig /* don't modify data until writeback is done */
311fcf865e3SChristoph Hellwig #define BLK_FEAT_STABLE_WRITES ((__force blk_features_t)(1u << 5))
312aadd5c59SChristoph Hellwig
313aadd5c59SChristoph Hellwig /* always completes in submit context */
314fcf865e3SChristoph Hellwig #define BLK_FEAT_SYNCHRONOUS ((__force blk_features_t)(1u << 6))
315f76af42fSChristoph Hellwig
316f76af42fSChristoph Hellwig /* supports REQ_NOWAIT */
317fcf865e3SChristoph Hellwig #define BLK_FEAT_NOWAIT ((__force blk_features_t)(1u << 7))
318f467fee4SChristoph Hellwig
319f467fee4SChristoph Hellwig /* supports DAX */
320fcf865e3SChristoph Hellwig #define BLK_FEAT_DAX ((__force blk_features_t)(1u << 8))
3218023e144SChristoph Hellwig
3228023e144SChristoph Hellwig /* supports I/O polling */
323fcf865e3SChristoph Hellwig #define BLK_FEAT_POLL ((__force blk_features_t)(1u << 9))
324b1fc937aSChristoph Hellwig
325b1fc937aSChristoph Hellwig /* is a zoned device */
326fcf865e3SChristoph Hellwig #define BLK_FEAT_ZONED ((__force blk_features_t)(1u << 10))
327a52758a3SChristoph Hellwig
3289c1e42e3SChristoph Hellwig /* supports PCI(e) p2p requests */
329fcf865e3SChristoph Hellwig #define BLK_FEAT_PCI_P2PDMA ((__force blk_features_t)(1u << 12))
3308c8f5c85SChristoph Hellwig
3318c8f5c85SChristoph Hellwig /* skip this queue in blk_mq_(un)quiesce_tagset */
332fcf865e3SChristoph Hellwig #define BLK_FEAT_SKIP_TAGSET_QUIESCE ((__force blk_features_t)(1u << 13))
333339d3948SChristoph Hellwig
334339d3948SChristoph Hellwig /* bounce all highmem pages */
335fcf865e3SChristoph Hellwig #define BLK_FEAT_BOUNCE_HIGH ((__force blk_features_t)(1u << 14))
3367d4dec52SChristoph Hellwig
3377d4dec52SChristoph Hellwig /* undocumented magic for bcache */
338fcf865e3SChristoph Hellwig #define BLK_FEAT_RAID_PARTIAL_STRIPES_EXPENSIVE \
339fcf865e3SChristoph Hellwig ((__force blk_features_t)(1u << 15))
3401122c0c1SChristoph Hellwig
3416a7e17b2SJohn Garry /* atomic writes enabled */
3426a7e17b2SJohn Garry #define BLK_FEAT_ATOMIC_WRITES \
343d7f36dc4SJohn Garry ((__force blk_features_t)(1u << 16))
344d7f36dc4SJohn Garry
345797476b8SDamien Le Moal /*
3461122c0c1SChristoph Hellwig * Flags automatically inherited when stacking limits.
3479bb33f24SChristoph Hellwig */
3481122c0c1SChristoph Hellwig #define BLK_FEAT_INHERIT_MASK \
3491a02f3a7SChristoph Hellwig (BLK_FEAT_WRITE_CACHE | BLK_FEAT_FUA | BLK_FEAT_ROTATIONAL | \
3507d4dec52SChristoph Hellwig BLK_FEAT_STABLE_WRITES | BLK_FEAT_ZONED | BLK_FEAT_BOUNCE_HIGH | \
3517d4dec52SChristoph Hellwig BLK_FEAT_RAID_PARTIAL_STRIPES_EXPENSIVE)
3521122c0c1SChristoph Hellwig
3531122c0c1SChristoph Hellwig /* internal flags in queue_limits.flags */
354fcf865e3SChristoph Hellwig typedef unsigned int __bitwise blk_flags_t;
355fcf865e3SChristoph Hellwig
356bae1c743SChristoph Hellwig /* do not send FLUSH/FUA commands despite advertising a write cache */
357fcf865e3SChristoph Hellwig #define BLK_FLAG_WRITE_CACHE_DISABLED ((__force blk_flags_t)(1u << 0))
3581122c0c1SChristoph Hellwig
3595543217bSChristoph Hellwig /* I/O topology is misaligned */
360fcf865e3SChristoph Hellwig #define BLK_FLAG_MISALIGNED ((__force blk_flags_t)(1u << 1))
3619bb33f24SChristoph Hellwig
362110234daSKeith Busch /* passthrough command IO accounting */
363110234daSKeith Busch #define BLK_FLAG_IOSTATS_PASSTHROUGH ((__force blk_flags_t)(1u << 2))
364110234daSKeith Busch
365025146e1SMartin K. Petersen struct queue_limits {
366fcf865e3SChristoph Hellwig blk_features_t features;
367fcf865e3SChristoph Hellwig blk_flags_t flags;
368025146e1SMartin K. Petersen unsigned long seg_boundary_mask;
36903100aadSKeith Busch unsigned long virt_boundary_mask;
370025146e1SMartin K. Petersen
371025146e1SMartin K. Petersen unsigned int max_hw_sectors;
372ca369d51SMartin K. Petersen unsigned int max_dev_sectors;
373762380adSJens Axboe unsigned int chunk_sectors;
374025146e1SMartin K. Petersen unsigned int max_sectors;
375c9c77418SKeith Busch unsigned int max_user_sectors;
376025146e1SMartin K. Petersen unsigned int max_segment_size;
377889c5706SMing Lei unsigned int min_segment_size;
378c72758f3SMartin K. Petersen unsigned int physical_block_size;
379ad6bf88aSMikulas Patocka unsigned int logical_block_size;
380c72758f3SMartin K. Petersen unsigned int alignment_offset;
381c72758f3SMartin K. Petersen unsigned int io_min;
382c72758f3SMartin K. Petersen unsigned int io_opt;
38367efc925SChristoph Hellwig unsigned int max_discard_sectors;
3840034af03SJens Axboe unsigned int max_hw_discard_sectors;
3854f563a64SChristoph Hellwig unsigned int max_user_discard_sectors;
38644abff2cSChristoph Hellwig unsigned int max_secure_erase_sectors;
387a6f0788eSChaitanya Kulkarni unsigned int max_write_zeroes_sectors;
388559218d4SChristoph Hellwig unsigned int max_hw_zone_append_sectors;
3890512a75bSKeith Busch unsigned int max_zone_append_sectors;
39086b37281SMartin K. Petersen unsigned int discard_granularity;
39186b37281SMartin K. Petersen unsigned int discard_alignment;
392a805a4faSDamien Le Moal unsigned int zone_write_granularity;
393025146e1SMartin K. Petersen
3949da3d1e9SJohn Garry /* atomic write limits */
3959da3d1e9SJohn Garry unsigned int atomic_write_hw_max;
3969da3d1e9SJohn Garry unsigned int atomic_write_max_sectors;
3979da3d1e9SJohn Garry unsigned int atomic_write_hw_boundary;
3989da3d1e9SJohn Garry unsigned int atomic_write_boundary_sectors;
3999da3d1e9SJohn Garry unsigned int atomic_write_hw_unit_min;
4009da3d1e9SJohn Garry unsigned int atomic_write_unit_min;
4019da3d1e9SJohn Garry unsigned int atomic_write_hw_unit_max;
4029da3d1e9SJohn Garry unsigned int atomic_write_unit_max;
4039da3d1e9SJohn Garry
4048a78362cSMartin K. Petersen unsigned short max_segments;
40513f05c8dSMartin K. Petersen unsigned short max_integrity_segments;
4061e739730SChristoph Hellwig unsigned short max_discard_segments;
407025146e1SMartin K. Petersen
4088c4955c0SChristoph Hellwig unsigned int max_open_zones;
4098c4955c0SChristoph Hellwig unsigned int max_active_zones;
410c964d62fSKeith Busch
411c964d62fSKeith Busch /*
412c964d62fSKeith Busch * Drivers that set dma_alignment to less than 511 must be prepared to
413c964d62fSKeith Busch * handle individual bvec's that are not a multiple of a SECTOR_SIZE
414c964d62fSKeith Busch * due to possible offsets.
415c964d62fSKeith Busch */
416c964d62fSKeith Busch unsigned int dma_alignment;
417e94b45d0SChristoph Hellwig unsigned int dma_pad_mask;
418c6e56cf6SChristoph Hellwig
419c6e56cf6SChristoph Hellwig struct blk_integrity integrity;
420025146e1SMartin K. Petersen };
421025146e1SMartin K. Petersen
422d4100351SChristoph Hellwig typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx,
423d4100351SChristoph Hellwig void *data);
424d4100351SChristoph Hellwig
425d4100351SChristoph Hellwig #define BLK_ALL_ZONES ((unsigned int)-1)
426d4100351SChristoph Hellwig int blkdev_report_zones(struct block_device *bdev, sector_t sector,
427d4100351SChristoph Hellwig unsigned int nr_zones, report_zones_cb cb, void *data);
428668bfeeaSChristoph Hellwig int blkdev_zone_mgmt(struct block_device *bdev, enum req_op op,
42971f4ecdbSJohannes Thumshirn sector_t sectors, sector_t nr_sectors);
4309b3c08b9SDamien Le Moal int blk_revalidate_disk_zones(struct gendisk *disk);
4316a0cb1bcSHannes Reinecke
432a2247f19SDamien Le Moal /*
433a2247f19SDamien Le Moal * Independent access ranges: struct blk_independent_access_range describes
434a2247f19SDamien Le Moal * a range of contiguous sectors that can be accessed using device command
435a2247f19SDamien Le Moal * execution resources that are independent from the resources used for
436a2247f19SDamien Le Moal * other access ranges. This is typically found with single-LUN multi-actuator
437a2247f19SDamien Le Moal * HDDs where each access range is served by a different set of heads.
438a2247f19SDamien Le Moal * The set of independent ranges supported by the device is defined using
439a2247f19SDamien Le Moal * struct blk_independent_access_ranges. The independent ranges must not overlap
440a2247f19SDamien Le Moal * and must include all sectors within the disk capacity (no sector holes
441a2247f19SDamien Le Moal * allowed).
442a2247f19SDamien Le Moal * For a device with multiple ranges, requests targeting sectors in different
443a2247f19SDamien Le Moal * ranges can be executed in parallel. A request can straddle an access range
444a2247f19SDamien Le Moal * boundary.
445a2247f19SDamien Le Moal */
446a2247f19SDamien Le Moal struct blk_independent_access_range {
447a2247f19SDamien Le Moal struct kobject kobj;
448a2247f19SDamien Le Moal sector_t sector;
449a2247f19SDamien Le Moal sector_t nr_sectors;
450a2247f19SDamien Le Moal };
451a2247f19SDamien Le Moal
452a2247f19SDamien Le Moal struct blk_independent_access_ranges {
453a2247f19SDamien Le Moal struct kobject kobj;
454a2247f19SDamien Le Moal bool sysfs_registered;
455a2247f19SDamien Le Moal unsigned int nr_ia_ranges;
456a2247f19SDamien Le Moal struct blk_independent_access_range ia_range[];
457a2247f19SDamien Le Moal };
458a2247f19SDamien Le Moal
459d7b76301SRichard Kennedy struct request_queue {
4601da177e4SLinus Torvalds /*
4611da177e4SLinus Torvalds * The queue owner gets to use this for whatever they like.
4621da177e4SLinus Torvalds * ll_rw_blk doesn't touch it.
4631da177e4SLinus Torvalds */
4641da177e4SLinus Torvalds void *queuedata;
4651da177e4SLinus Torvalds
4660c734c5eSJens Axboe struct elevator_queue *elevator;
4670c734c5eSJens Axboe
4680c734c5eSJens Axboe const struct blk_mq_ops *mq_ops;
4690c734c5eSJens Axboe
4700c734c5eSJens Axboe /* sw queues */
4710c734c5eSJens Axboe struct blk_mq_ctx __percpu *queue_ctx;
4720c734c5eSJens Axboe
4731da177e4SLinus Torvalds /*
4741da177e4SLinus Torvalds * various queue flags, see QUEUE_* below
4751da177e4SLinus Torvalds */
4761da177e4SLinus Torvalds unsigned long queue_flags;
4771da177e4SLinus Torvalds
4780c734c5eSJens Axboe unsigned int rq_timeout;
4790c734c5eSJens Axboe
4800c734c5eSJens Axboe unsigned int queue_depth;
4810c734c5eSJens Axboe
4820c734c5eSJens Axboe refcount_t refs;
4830c734c5eSJens Axboe
4840c734c5eSJens Axboe /* hw dispatch queues */
4850c734c5eSJens Axboe unsigned int nr_hw_queues;
4860c734c5eSJens Axboe struct xarray hctx_table;
4870c734c5eSJens Axboe
4880c734c5eSJens Axboe struct percpu_ref q_usage_counter;
489f1be1788SMing Lei struct lock_class_key io_lock_cls_key;
490f1be1788SMing Lei struct lockdep_map io_lockdep_map;
491f1be1788SMing Lei
492f1be1788SMing Lei struct lock_class_key q_lock_cls_key;
493f1be1788SMing Lei struct lockdep_map q_lockdep_map;
4940c734c5eSJens Axboe
4950c734c5eSJens Axboe struct request *last_merge;
496a73f730dSTejun Heo
4970d945c1fSChristoph Hellwig spinlock_t queue_lock;
4981da177e4SLinus Torvalds
4990c734c5eSJens Axboe int quiesce_depth;
500d152c682SChristoph Hellwig
5010c734c5eSJens Axboe struct gendisk *disk;
5021da177e4SLinus Torvalds
503320ae51fSJens Axboe /*
504320ae51fSJens Axboe * mq queue kobject
505320ae51fSJens Axboe */
5061db4909eSMing Lei struct kobject *mq_kobj;
507320ae51fSJens Axboe
5080c734c5eSJens Axboe struct queue_limits limits;
5090c734c5eSJens Axboe
51047fafbc7SRafael J. Wysocki #ifdef CONFIG_PM
5116c954667SLin Ming struct device *dev;
512db04e18dSGeert Uytterhoeven enum rpm_status rpm_status;
5136c954667SLin Ming #endif
5146c954667SLin Ming
5151da177e4SLinus Torvalds /*
5160c734c5eSJens Axboe * Number of contexts that have called blk_set_pm_only(). If this
5170c734c5eSJens Axboe * counter is above zero then only RQF_PM requests are processed.
5180c734c5eSJens Axboe */
5190c734c5eSJens Axboe atomic_t pm_only;
5200c734c5eSJens Axboe
5210c734c5eSJens Axboe struct blk_queue_stats *stats;
5220c734c5eSJens Axboe struct rq_qos *rq_qos;
5230c734c5eSJens Axboe struct mutex rq_qos_mutex;
5240c734c5eSJens Axboe
5250c734c5eSJens Axboe /*
5260c734c5eSJens Axboe * ida allocated id for this queue. Used to index queues from
5270c734c5eSJens Axboe * ioctx.
5280c734c5eSJens Axboe */
5290c734c5eSJens Axboe int id;
5300c734c5eSJens Axboe
5310c734c5eSJens Axboe /*
5321da177e4SLinus Torvalds * queue settings
5331da177e4SLinus Torvalds */
5341da177e4SLinus Torvalds unsigned long nr_requests; /* Max # of requests */
5351da177e4SLinus Torvalds
5361b262839SSatya Tangirala #ifdef CONFIG_BLK_INLINE_ENCRYPTION
537cb77cb5aSEric Biggers struct blk_crypto_profile *crypto_profile;
53820f01f16SEric Biggers struct kobject *crypto_kobject;
5391b262839SSatya Tangirala #endif
5401b262839SSatya Tangirala
541242f9dcbSJens Axboe struct timer_list timeout;
542287922ebSChristoph Hellwig struct work_struct timeout_work;
543242f9dcbSJens Axboe
544079a2e3eSJohn Garry atomic_t nr_active_requests_shared_tags;
545bccf5e26SJohn Garry
546079a2e3eSJohn Garry struct blk_mq_tags *sched_shared_tags;
547d97e594cSJohn Garry
548a612fddfSTejun Heo struct list_head icq_list;
5491231039dSChristoph Hellwig #ifdef CONFIG_BLK_CGROUP
5501231039dSChristoph Hellwig DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS);
5511231039dSChristoph Hellwig struct blkcg_gq *root_blkg;
5521231039dSChristoph Hellwig struct list_head blkg_list;
5531231039dSChristoph Hellwig struct mutex blkcg_mutex;
5541231039dSChristoph Hellwig #endif
555a612fddfSTejun Heo
5561946089aSChristoph Lameter int node;
5570c734c5eSJens Axboe
5580c734c5eSJens Axboe spinlock_t requeue_lock;
5590c734c5eSJens Axboe struct list_head requeue_list;
5600c734c5eSJens Axboe struct delayed_work requeue_work;
5610c734c5eSJens Axboe
5626c5c9341SAlexey Dobriyan #ifdef CONFIG_BLK_DEV_IO_TRACE
563c780e86dSJan Kara struct blk_trace __rcu *blk_trace;
5646c5c9341SAlexey Dobriyan #endif
5651da177e4SLinus Torvalds /*
5664913efe4STejun Heo * for flush operations
5671da177e4SLinus Torvalds */
5687c94e1c1SMing Lei struct blk_flush_queue *fq;
5699a67aa52SChristoph Hellwig struct list_head flush_list;
570483f4afcSAl Viro
5711bf70d08SNilay Shroff /*
5725abba4ceSNilay Shroff * Protects against I/O scheduler switching, particularly when updating
5735abba4ceSNilay Shroff * q->elevator. Since the elevator update code path may also modify q->
5745abba4ceSNilay Shroff * nr_requests and wbt latency, this lock also protects the sysfs attrs
5755abba4ceSNilay Shroff * nr_requests and wbt_lat_usec. Additionally the nr_hw_queues update
5765abba4ceSNilay Shroff * may modify hctx tags, reserved-tags and cpumask, so this lock also
577a3996d11SNilay Shroff * helps protect the hctx sysfs/debugfs attrs. To ensure proper locking
578a3996d11SNilay Shroff * order during an elevator or nr_hw_queue update, first freeze the
579a3996d11SNilay Shroff * queue, then acquire ->elevator_lock.
5801bf70d08SNilay Shroff */
5811bf70d08SNilay Shroff struct mutex elevator_lock;
5821bf70d08SNilay Shroff
583483f4afcSAl Viro struct mutex sysfs_lock;
5845e40f445SNilay Shroff /*
5855e40f445SNilay Shroff * Protects queue limits and also sysfs attribute read_ahead_kb.
5865e40f445SNilay Shroff */
587d690cb8aSChristoph Hellwig struct mutex limits_lock;
588d351af01SFUJITA Tomonori
5892f8f1336SMing Lei /*
5902f8f1336SMing Lei * for reusing dead hctx instance in case of updating
5912f8f1336SMing Lei * nr_hw_queues
5922f8f1336SMing Lei */
5932f8f1336SMing Lei struct list_head unused_hctx_list;
5942f8f1336SMing Lei spinlock_t unused_hctx_lock;
5952f8f1336SMing Lei
5967996a8b5SBob Liu int mq_freeze_depth;
597d732580bSTejun Heo
598e43473b7SVivek Goyal #ifdef CONFIG_BLK_DEV_THROTTLING
599e43473b7SVivek Goyal /* Throttle data */
600e43473b7SVivek Goyal struct throtl_data *td;
601e43473b7SVivek Goyal #endif
602548bc8e1STejun Heo struct rcu_head rcu_head;
6036a786998SMing Lei #ifdef CONFIG_LOCKDEP
6046a786998SMing Lei struct task_struct *mq_freeze_owner;
6056a786998SMing Lei int mq_freeze_owner_depth;
606f6661b1dSMing Lei /*
607f6661b1dSMing Lei * Records disk & queue state in current context, used in unfreeze
608f6661b1dSMing Lei * queue
609f6661b1dSMing Lei */
6106f491a8dSMing Lei bool mq_freeze_disk_dead;
611f6661b1dSMing Lei bool mq_freeze_queue_dying;
6126a786998SMing Lei #endif
613320ae51fSJens Axboe wait_queue_head_t mq_freeze_wq;
6147996a8b5SBob Liu /*
6157996a8b5SBob Liu * Protect concurrent access to q_usage_counter by
6167996a8b5SBob Liu * percpu_ref_kill() and percpu_ref_reinit().
6177996a8b5SBob Liu */
6187996a8b5SBob Liu struct mutex mq_freeze_lock;
6190d2602caSJens Axboe
6200d2602caSJens Axboe struct blk_mq_tag_set *tag_set;
6210d2602caSJens Axboe struct list_head tag_set_list;
6224593fdbeSAkinobu Mita
62307e4feadSOmar Sandoval struct dentry *debugfs_dir;
624d332ce09SOmar Sandoval struct dentry *sched_debugfs_dir;
625cc56694fSMing Lei struct dentry *rqos_debugfs_dir;
6265cf9c91bSChristoph Hellwig /*
6275cf9c91bSChristoph Hellwig * Serializes all debugfs metadata operations using the above dentries.
6285cf9c91bSChristoph Hellwig */
6295cf9c91bSChristoph Hellwig struct mutex debugfs_mutex;
6301da177e4SLinus Torvalds };
6311da177e4SLinus Torvalds
632bfe373f6SHou Tao /* Keep blk_queue_flag_name[] in sync with the definitions below */
63355177adfSJohn Garry enum {
63455177adfSJohn Garry QUEUE_FLAG_DYING, /* queue being torn down */
63555177adfSJohn Garry QUEUE_FLAG_NOMERGES, /* disable merge attempts */
63655177adfSJohn Garry QUEUE_FLAG_SAME_COMP, /* complete on same CPU-group */
63755177adfSJohn Garry QUEUE_FLAG_FAIL_IO, /* fake timeout */
63855177adfSJohn Garry QUEUE_FLAG_NOXMERGES, /* No extended merges */
63955177adfSJohn Garry QUEUE_FLAG_SAME_FORCE, /* force complete on same CPU */
64055177adfSJohn Garry QUEUE_FLAG_INIT_DONE, /* queue is initialized */
64155177adfSJohn Garry QUEUE_FLAG_STATS, /* track IO start and completion times */
64255177adfSJohn Garry QUEUE_FLAG_REGISTERED, /* queue has been registered to a disk */
64355177adfSJohn Garry QUEUE_FLAG_QUIESCED, /* queue has been quiesced */
64455177adfSJohn Garry QUEUE_FLAG_RQ_ALLOC_TIME, /* record rq->alloc_time_ns */
64555177adfSJohn Garry QUEUE_FLAG_HCTX_ACTIVE, /* at least one blk-mq hctx is active */
64655177adfSJohn Garry QUEUE_FLAG_SQ_SCHED, /* single queue style io dispatch */
64755177adfSJohn Garry QUEUE_FLAG_MAX
64855177adfSJohn Garry };
649797e7dbbSTejun Heo
650f76af42fSChristoph Hellwig #define QUEUE_FLAG_MQ_DEFAULT (1UL << QUEUE_FLAG_SAME_COMP)
65194eddfbeSJens Axboe
6528814ce8aSBart Van Assche void blk_queue_flag_set(unsigned int flag, struct request_queue *q);
6538814ce8aSBart Van Assche void blk_queue_flag_clear(unsigned int flag, struct request_queue *q);
6548814ce8aSBart Van Assche
6553f3299d5SBart Van Assche #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags)
656320ae51fSJens Axboe #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags)
657ac9fafa1SAlan D. Brunelle #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags)
658488991e2SAlan D. Brunelle #define blk_queue_noxmerges(q) \
659488991e2SAlan D. Brunelle test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags)
66044348870SChristoph Hellwig #define blk_queue_nonrot(q) (!((q)->limits.features & BLK_FEAT_ROTATIONAL))
661cdb24979SChristoph Hellwig #define blk_queue_io_stat(q) ((q)->limits.features & BLK_FEAT_IO_STAT)
662110234daSKeith Busch #define blk_queue_passthrough_stat(q) \
663110234daSKeith Busch ((q)->limits.flags & BLK_FLAG_IOSTATS_PASSTHROUGH)
664f467fee4SChristoph Hellwig #define blk_queue_dax(q) ((q)->limits.features & BLK_FEAT_DAX)
6659c1e42e3SChristoph Hellwig #define blk_queue_pci_p2pdma(q) ((q)->limits.features & BLK_FEAT_PCI_P2PDMA)
6666f816b4bSTejun Heo #ifdef CONFIG_BLK_RQ_ALLOC_TIME
6676f816b4bSTejun Heo #define blk_queue_rq_alloc_time(q) \
6686f816b4bSTejun Heo test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags)
6696f816b4bSTejun Heo #else
6706f816b4bSTejun Heo #define blk_queue_rq_alloc_time(q) false
6716f816b4bSTejun Heo #endif
6721da177e4SLinus Torvalds
67333659ebbSChristoph Hellwig #define blk_noretry_request(rq) \
67433659ebbSChristoph Hellwig ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
67533659ebbSChristoph Hellwig REQ_FAILFAST_DRIVER))
676f4560ffeSMing Lei #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags)
677cd84a62eSBart Van Assche #define blk_queue_pm_only(q) atomic_read(&(q)->pm_only)
67858c898baSMing Lei #define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags)
6794d337cebSMing Lei #define blk_queue_sq_sched(q) test_bit(QUEUE_FLAG_SQ_SCHED, &(q)->queue_flags)
680414dd48eSChao Leng #define blk_queue_skip_tagset_quiesce(q) \
6818c8f5c85SChristoph Hellwig ((q)->limits.features & BLK_FEAT_SKIP_TAGSET_QUIESCE)
682c9254f2dSBart Van Assche
683cd84a62eSBart Van Assche extern void blk_set_pm_only(struct request_queue *q);
684cd84a62eSBart Van Assche extern void blk_clear_pm_only(struct request_queue *q);
6854aff5e23SJens Axboe
6861da177e4SLinus Torvalds #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist)
6871da177e4SLinus Torvalds
6883ab3a031SChristoph Hellwig #define dma_map_bvec(dev, bv, dir, attrs) \
6893ab3a031SChristoph Hellwig dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \
6903ab3a031SChristoph Hellwig (dir), (attrs))
6913ab3a031SChristoph Hellwig
queue_is_mq(struct request_queue * q)692344e9ffcSJens Axboe static inline bool queue_is_mq(struct request_queue *q)
69349fd524fSJens Axboe {
694a1ce35faSJens Axboe return q->mq_ops;
69549fd524fSJens Axboe }
69649fd524fSJens Axboe
69752abca64SAlan Stern #ifdef CONFIG_PM
queue_rpm_status(struct request_queue * q)69852abca64SAlan Stern static inline enum rpm_status queue_rpm_status(struct request_queue *q)
69952abca64SAlan Stern {
70052abca64SAlan Stern return q->rpm_status;
70152abca64SAlan Stern }
70252abca64SAlan Stern #else
queue_rpm_status(struct request_queue * q)70352abca64SAlan Stern static inline enum rpm_status queue_rpm_status(struct request_queue *q)
70452abca64SAlan Stern {
70552abca64SAlan Stern return RPM_ACTIVE;
70652abca64SAlan Stern }
70752abca64SAlan Stern #endif
70852abca64SAlan Stern
blk_queue_is_zoned(struct request_queue * q)709797476b8SDamien Le Moal static inline bool blk_queue_is_zoned(struct request_queue *q)
710797476b8SDamien Le Moal {
711b1fc937aSChristoph Hellwig return IS_ENABLED(CONFIG_BLK_DEV_ZONED) &&
712b1fc937aSChristoph Hellwig (q->limits.features & BLK_FEAT_ZONED);
713797476b8SDamien Le Moal }
714797476b8SDamien Le Moal
disk_zone_no(struct gendisk * disk,sector_t sector)7156a5ac984SBart Van Assche static inline unsigned int disk_zone_no(struct gendisk *disk, sector_t sector)
716d86e716aSChristoph Hellwig {
717965b652eSDamien Le Moal if (!blk_queue_is_zoned(disk->queue))
718caaf7101SDamien Le Moal return 0;
719965b652eSDamien Le Moal return sector >> ilog2(disk->queue->limits.chunk_sectors);
720caaf7101SDamien Le Moal }
721caaf7101SDamien Le Moal
bdev_max_open_zones(struct block_device * bdev)722caaf7101SDamien Le Moal static inline unsigned int bdev_max_open_zones(struct block_device *bdev)
723caaf7101SDamien Le Moal {
724caaf7101SDamien Le Moal return bdev->bd_disk->queue->limits.max_open_zones;
725caaf7101SDamien Le Moal }
726caaf7101SDamien Le Moal
bdev_max_active_zones(struct block_device * bdev)727caaf7101SDamien Le Moal static inline unsigned int bdev_max_active_zones(struct block_device *bdev)
728caaf7101SDamien Le Moal {
729caaf7101SDamien Le Moal return bdev->bd_disk->queue->limits.max_active_zones;
730caaf7101SDamien Le Moal }
731965b652eSDamien Le Moal
blk_queue_depth(struct request_queue * q)732d86e716aSChristoph Hellwig static inline unsigned int blk_queue_depth(struct request_queue *q)
7336cc77e9cSChristoph Hellwig {
734d86e716aSChristoph Hellwig if (q->queue_depth)
7356cc77e9cSChristoph Hellwig return q->queue_depth;
736d86e716aSChristoph Hellwig
7376cc77e9cSChristoph Hellwig return q->nr_requests;
7386cc77e9cSChristoph Hellwig }
739b6cfe228SDamien Le Moal
740e15864f8SNiklas Cassel /*
741b6cfe228SDamien Le Moal * default timeout for SG_IO if none specified
742659bf827SNiklas Cassel */
743659bf827SNiklas Cassel #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ)
7441dc01720SChristoph Hellwig #define BLK_MIN_SG_TIMEOUT (7 * HZ)
745659bf827SNiklas Cassel
7468c4955c0SChristoph Hellwig /* This should not be used directly - use rq_for_each_segment */
747659bf827SNiklas Cassel #define for_each_bio(_bio) \
7481dc01720SChristoph Hellwig for (; _bio; _bio = _bio->bi_next)
7491dc01720SChristoph Hellwig
7501dc01720SChristoph Hellwig int __must_check add_disk_fwnode(struct device *parent, struct gendisk *disk,
7518c4955c0SChristoph Hellwig const struct attribute_group **groups,
7521dc01720SChristoph Hellwig struct fwnode_handle *fwnode);
7531dc01720SChristoph Hellwig int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
754d278d4a8SJens Axboe const struct attribute_group **groups);
add_disk(struct gendisk * disk)755d278d4a8SJens Axboe static inline int __must_check add_disk(struct gendisk *disk)
756d278d4a8SJens Axboe {
757d278d4a8SJens Axboe return device_add_disk(NULL, disk, NULL);
758d278d4a8SJens Axboe }
759d278d4a8SJens Axboe void del_gendisk(struct gendisk *gp);
760d278d4a8SJens Axboe void invalidate_disk(struct gendisk *disk);
761d278d4a8SJens Axboe void set_disk_ro(struct gendisk *disk, bool read_only);
7623d6392cfSJens Axboe void disk_uevent(struct gendisk *disk, enum kobject_action action);
7633d6392cfSJens Axboe
bdev_partno(const struct block_device * bdev)7643d6392cfSJens Axboe static inline u8 bdev_partno(const struct block_device *bdev)
7653d6392cfSJens Axboe {
766f2f1fa78SLinus Torvalds return atomic_read(&bdev->__bd_flags) & BD_PARTNO;
7673d6392cfSJens Axboe }
7685705f702SNeilBrown
bdev_test_flag(const struct block_device * bdev,unsigned flag)7691e428079SJens Axboe static inline bool bdev_test_flag(const struct block_device *bdev, unsigned flag)
7701e428079SJens Axboe {
7711da177e4SLinus Torvalds return atomic_read(&bdev->__bd_flags) & flag;
7729dfd9ea9SChristian Marangi }
7739dfd9ea9SChristian Marangi
bdev_set_flag(struct block_device * bdev,unsigned flag)7749dfd9ea9SChristian Marangi static inline void bdev_set_flag(struct block_device *bdev, unsigned flag)
775322cbb50SChristoph Hellwig {
776322cbb50SChristoph Hellwig atomic_or(flag, &bdev->__bd_flags);
777322cbb50SChristoph Hellwig }
778322cbb50SChristoph Hellwig
bdev_clear_flag(struct block_device * bdev,unsigned flag)779322cbb50SChristoph Hellwig static inline void bdev_clear_flag(struct block_device *bdev, unsigned flag)
780322cbb50SChristoph Hellwig {
781322cbb50SChristoph Hellwig atomic_andnot(flag, &bdev->__bd_flags);
782322cbb50SChristoph Hellwig }
783322cbb50SChristoph Hellwig
get_disk_ro(struct gendisk * disk)784322cbb50SChristoph Hellwig static inline bool get_disk_ro(struct gendisk *disk)
785322cbb50SChristoph Hellwig {
786b8c873edSAl Viro return bdev_test_flag(disk->part0, BD_READ_ONLY) ||
787b8c873edSAl Viro test_bit(GD_READ_ONLY, &disk->state);
7881116b9faSAl Viro }
7891116b9faSAl Viro
bdev_read_only(struct block_device * bdev)7901116b9faSAl Viro static inline bool bdev_read_only(struct block_device *bdev)
7911116b9faSAl Viro {
7921116b9faSAl Viro return bdev_test_flag(bdev, BD_READ_ONLY) || get_disk_ro(bdev->bd_disk);
7931116b9faSAl Viro }
7941116b9faSAl Viro
7951116b9faSAl Viro bool set_capacity_and_notify(struct gendisk *disk, sector_t size);
7961116b9faSAl Viro void disk_force_media_change(struct gendisk *disk);
7971116b9faSAl Viro void bdev_mark_dead(struct block_device *bdev, bool surprise);
7981116b9faSAl Viro
7991116b9faSAl Viro void add_disk_randomness(struct gendisk *disk) __latent_entropy;
8001116b9faSAl Viro void rand_initialize_disk(struct gendisk *disk);
8011116b9faSAl Viro
get_start_sect(struct block_device * bdev)8021116b9faSAl Viro static inline sector_t get_start_sect(struct block_device *bdev)
8031116b9faSAl Viro {
804b8c873edSAl Viro return bdev->bd_start_sect;
805b8c873edSAl Viro }
806766a71efSChristoph Hellwig
bdev_nr_sectors(struct block_device * bdev)807322cbb50SChristoph Hellwig static inline sector_t bdev_nr_sectors(struct block_device *bdev)
80801e198f0SAl Viro {
809322cbb50SChristoph Hellwig return bdev->bd_nr_sectors;
810322cbb50SChristoph Hellwig }
811322cbb50SChristoph Hellwig
bdev_nr_bytes(struct block_device * bdev)812766a71efSChristoph Hellwig static inline loff_t bdev_nr_bytes(struct block_device *bdev)
813322cbb50SChristoph Hellwig {
81401e198f0SAl Viro return (loff_t)bdev_nr_sectors(bdev) << SECTOR_SHIFT;
815322cbb50SChristoph Hellwig }
816322cbb50SChristoph Hellwig
get_capacity(struct gendisk * disk)817322cbb50SChristoph Hellwig static inline sector_t get_capacity(struct gendisk *disk)
818ab6860f6SChristoph Hellwig {
819560e20e4SChristoph Hellwig return bdev_nr_sectors(disk->part0);
820322cbb50SChristoph Hellwig }
821322cbb50SChristoph Hellwig
sb_bdev_nr_blocks(struct super_block * sb)822322cbb50SChristoph Hellwig static inline u64 sb_bdev_nr_blocks(struct super_block *sb)
823322cbb50SChristoph Hellwig {
824322cbb50SChristoph Hellwig return bdev_nr_sectors(sb->s_bdev) >>
825322cbb50SChristoph Hellwig (sb->s_blocksize_bits - SECTOR_SHIFT);
826322cbb50SChristoph Hellwig }
827322cbb50SChristoph Hellwig
828322cbb50SChristoph Hellwig #ifdef CONFIG_BLK_DEV_ZONED
disk_nr_zones(struct gendisk * disk)829322cbb50SChristoph Hellwig static inline unsigned int disk_nr_zones(struct gendisk *disk)
830322cbb50SChristoph Hellwig {
831322cbb50SChristoph Hellwig return disk->nr_zones;
832322cbb50SChristoph Hellwig }
833322cbb50SChristoph Hellwig bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs);
834322cbb50SChristoph Hellwig
835322cbb50SChristoph Hellwig /**
836322cbb50SChristoph Hellwig * disk_zone_capacity - returns the zone capacity of zone containing @sector
837322cbb50SChristoph Hellwig * @disk: disk to work with
838322cbb50SChristoph Hellwig * @sector: sector number within the querying zone
839322cbb50SChristoph Hellwig *
840322cbb50SChristoph Hellwig * Returns the zone capacity of a zone containing @sector. @sector can be any
841322cbb50SChristoph Hellwig * sector in the zone.
842322cbb50SChristoph Hellwig */
disk_zone_capacity(struct gendisk * disk,sector_t sector)843322cbb50SChristoph Hellwig static inline unsigned int disk_zone_capacity(struct gendisk *disk,
844322cbb50SChristoph Hellwig sector_t sector)
845322cbb50SChristoph Hellwig {
846322cbb50SChristoph Hellwig sector_t zone_sectors = disk->queue->limits.chunk_sectors;
847322cbb50SChristoph Hellwig
848322cbb50SChristoph Hellwig if (sector + zone_sectors >= get_capacity(disk))
849322cbb50SChristoph Hellwig return disk->last_zone_capacity;
850322cbb50SChristoph Hellwig return disk->zone_capacity;
851322cbb50SChristoph Hellwig }
bdev_zone_capacity(struct block_device * bdev,sector_t pos)852322cbb50SChristoph Hellwig static inline unsigned int bdev_zone_capacity(struct block_device *bdev,
85374fa8f9cSChristoph Hellwig sector_t pos)
85474fa8f9cSChristoph Hellwig {
855322cbb50SChristoph Hellwig return disk_zone_capacity(bdev->bd_disk, pos);
856322cbb50SChristoph Hellwig }
857322cbb50SChristoph Hellwig #else /* CONFIG_BLK_DEV_ZONED */
disk_nr_zones(struct gendisk * disk)85874fa8f9cSChristoph Hellwig static inline unsigned int disk_nr_zones(struct gendisk *disk)
859322cbb50SChristoph Hellwig {
860322cbb50SChristoph Hellwig return 0;
861322cbb50SChristoph Hellwig }
blk_zone_plug_bio(struct bio * bio,unsigned int nr_segs)862322cbb50SChristoph Hellwig static inline bool blk_zone_plug_bio(struct bio *bio, unsigned int nr_segs)
863322cbb50SChristoph Hellwig {
86474fa8f9cSChristoph Hellwig return false;
86574fa8f9cSChristoph Hellwig }
866322cbb50SChristoph Hellwig #endif /* CONFIG_BLK_DEV_ZONED */
867322cbb50SChristoph Hellwig
bdev_nr_zones(struct block_device * bdev)86874fa8f9cSChristoph Hellwig static inline unsigned int bdev_nr_zones(struct block_device *bdev)
869322cbb50SChristoph Hellwig {
870322cbb50SChristoph Hellwig return disk_nr_zones(bdev->bd_disk);
871322cbb50SChristoph Hellwig }
87274fa8f9cSChristoph Hellwig
873322cbb50SChristoph Hellwig int bdev_disk_changed(struct gendisk *disk, bool invalidate);
874322cbb50SChristoph Hellwig
875322cbb50SChristoph Hellwig void put_disk(struct gendisk *disk);
876322cbb50SChristoph Hellwig struct gendisk *__blk_alloc_disk(struct queue_limits *lim, int node,
877322cbb50SChristoph Hellwig struct lock_class_key *lkclass);
878322cbb50SChristoph Hellwig
879322cbb50SChristoph Hellwig /**
880322cbb50SChristoph Hellwig * blk_alloc_disk - allocate a gendisk structure
881444aa2c5SChristoph Hellwig * @lim: queue limits to be used for this disk.
882322cbb50SChristoph Hellwig * @node_id: numa node to allocate on
883322cbb50SChristoph Hellwig *
884322cbb50SChristoph Hellwig * Allocate and pre-initialize a gendisk structure for use with BIO based
885322cbb50SChristoph Hellwig * drivers.
886322cbb50SChristoph Hellwig *
887322cbb50SChristoph Hellwig * Returns an ERR_PTR on error, else the allocated disk.
888322cbb50SChristoph Hellwig *
889322cbb50SChristoph Hellwig * Context: can sleep
890322cbb50SChristoph Hellwig */
891322cbb50SChristoph Hellwig #define blk_alloc_disk(lim, node_id) \
892322cbb50SChristoph Hellwig ({ \
893322cbb50SChristoph Hellwig static struct lock_class_key __key; \
894322cbb50SChristoph Hellwig \
895322cbb50SChristoph Hellwig __blk_alloc_disk(lim, node_id, &__key); \
896322cbb50SChristoph Hellwig })
897322cbb50SChristoph Hellwig
898322cbb50SChristoph Hellwig int __register_blkdev(unsigned int major, const char *name,
899322cbb50SChristoph Hellwig void (*probe)(dev_t devt));
900322cbb50SChristoph Hellwig #define register_blkdev(major, name) \
901322cbb50SChristoph Hellwig __register_blkdev(major, name, NULL)
9022d4dc890SIlya Loginov void unregister_blkdev(unsigned int major, const char *name);
9031da177e4SLinus Torvalds
9041da177e4SLinus Torvalds bool disk_check_media_change(struct gendisk *disk);
9053e08773cSChristoph Hellwig void set_capacity(struct gendisk *disk, sector_t size);
9065a97806fSChristoph Hellwig
90724b83debSChristoph Hellwig #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
908ef9e3facSKiyoshi Ueda int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
9099a95e4efSBart Van Assche void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk);
9102e6edc95SDan Williams #else
bd_link_disk_holder(struct block_device * bdev,struct gendisk * disk)9111da177e4SLinus Torvalds static inline int bd_link_disk_holder(struct block_device *bdev,
912fb9b16e1SKeith Busch struct gendisk *disk)
913e47bc4edSChaitanya Kulkarni {
91477e7ffd7SBart Van Assche return 0;
915e47bc4edSChaitanya Kulkarni }
bd_unlink_disk_holder(struct block_device * bdev,struct gendisk * disk)9162a842acaSChristoph Hellwig static inline void bd_unlink_disk_holder(struct block_device *bdev,
9172a842acaSChristoph Hellwig struct gendisk *disk)
9187ba37927SKent Overstreet {
9192a842acaSChristoph Hellwig }
920ef99b2d3SChristoph Hellwig #endif /* CONFIG_BLOCK_HOLDER_DEPRECATED */
921ef99b2d3SChristoph Hellwig
9225a72e899SJens Axboe dev_t part_devt(struct gendisk *disk, u8 partno);
9235a72e899SJens Axboe void inc_diskseq(struct gendisk *disk);
9245a72e899SJens Axboe void blk_request_module(dev_t devt);
92505229beeSJens Axboe
926165125e1SJens Axboe extern int blk_register_queue(struct gendisk *disk);
9271da177e4SLinus Torvalds extern void blk_unregister_queue(struct gendisk *disk);
92817220ca5SPavel Begunkov void submit_bio_noacct(struct bio *bio);
9291da177e4SLinus Torvalds struct bio *bio_split_to_limits(struct bio *bio);
9301da177e4SLinus Torvalds
93102694e86SChaitanya Kulkarni extern int blk_lld_busy(struct request_queue *q);
93202694e86SChaitanya Kulkarni extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags);
93302694e86SChaitanya Kulkarni extern void blk_queue_exit(struct request_queue *q);
934d0ea6bdeSDamien Le Moal extern void blk_sync_queue(struct request_queue *q);
935d0ea6bdeSDamien Le Moal
936d86e716aSChristoph Hellwig /* Helper to convert REQ_OP_XXX to its string format XXX */
937d0ea6bdeSDamien Le Moal extern const char *blk_op_str(enum req_op op);
938d0ea6bdeSDamien Le Moal
939b85a3c1bSDamien Le Moal int blk_status_to_errno(blk_status_t status);
940d0ea6bdeSDamien Le Moal blk_status_t errno_to_blk_status(int errno);
941b85a3c1bSDamien Le Moal const char *blk_status_to_str(blk_status_t status);
942b85a3c1bSDamien Le Moal
943b85a3c1bSDamien Le Moal /* only poll the hardware once, don't continue until a completion was found */
944d0ea6bdeSDamien Le Moal #define BLK_POLL_ONESHOT (1 << 0)
9456cc77e9cSChristoph Hellwig int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags);
946762380adSJens Axboe int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
947f70167a7SJohn Garry unsigned int flags);
948f70167a7SJohn Garry
bdev_get_queue(struct block_device * bdev)9498689461bSChristoph Hellwig static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
950f70167a7SJohn Garry {
951f70167a7SJohn Garry return bdev->bd_queue; /* this is never NULL */
9528689461bSChristoph Hellwig }
953f70167a7SJohn Garry
954f70167a7SJohn Garry /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */
955f70167a7SJohn Garry const char *blk_zone_cond_str(enum blk_zone_cond zone_cond);
9568689461bSChristoph Hellwig
bio_zone_no(struct bio * bio)9578689461bSChristoph Hellwig static inline unsigned int bio_zone_no(struct bio *bio)
958d690cb8aSChristoph Hellwig {
959d690cb8aSChristoph Hellwig return disk_zone_no(bio->bi_bdev->bd_disk, bio->bi_iter.bi_sector);
960d690cb8aSChristoph Hellwig }
961d690cb8aSChristoph Hellwig
bio_straddles_zones(struct bio * bio)962d690cb8aSChristoph Hellwig static inline bool bio_straddles_zones(struct bio *bio)
963d690cb8aSChristoph Hellwig {
964d690cb8aSChristoph Hellwig return bio_sectors(bio) &&
965d690cb8aSChristoph Hellwig bio_zone_no(bio) !=
966d690cb8aSChristoph Hellwig disk_zone_no(bio->bi_bdev->bd_disk, bio_end_sector(bio) - 1);
9679c96821bSChristoph Hellwig }
968d690cb8aSChristoph Hellwig
969d690cb8aSChristoph Hellwig /*
970d690cb8aSChristoph Hellwig * Return how much within the boundary is left to be used for I/O at a given
971d690cb8aSChristoph Hellwig * offset.
972d690cb8aSChristoph Hellwig */
blk_boundary_sectors_left(sector_t offset,unsigned int boundary_sectors)973d690cb8aSChristoph Hellwig static inline unsigned int blk_boundary_sectors_left(sector_t offset,
974d690cb8aSChristoph Hellwig unsigned int boundary_sectors)
975aa427d7bSChristoph Hellwig {
976aa427d7bSChristoph Hellwig if (unlikely(!is_power_of_2(boundary_sectors)))
977d690cb8aSChristoph Hellwig return boundary_sectors - sector_div(offset, boundary_sectors);
978d690cb8aSChristoph Hellwig return boundary_sectors - (offset & (boundary_sectors - 1));
979631d4efbSChristoph Hellwig }
980470d2bc3SChristoph Hellwig
981d690cb8aSChristoph Hellwig /**
98229306626SChristoph Hellwig * queue_limits_start_update - start an atomic update of queue limits
98329306626SChristoph Hellwig * @q: queue to update
98429306626SChristoph Hellwig *
98529306626SChristoph Hellwig * This functions starts an atomic update of the queue limits. It takes a lock
98629306626SChristoph Hellwig * to prevent other updates and returns a snapshot of the current limits that
98729306626SChristoph Hellwig * the caller can modify. The caller must call queue_limits_commit_update()
98829306626SChristoph Hellwig * to finish the update.
98929306626SChristoph Hellwig *
99029306626SChristoph Hellwig * Context: process context.
99129306626SChristoph Hellwig */
99229306626SChristoph Hellwig static inline struct queue_limits
queue_limits_start_update(struct request_queue * q)99329306626SChristoph Hellwig queue_limits_start_update(struct request_queue *q)
99429306626SChristoph Hellwig {
9958689461bSChristoph Hellwig mutex_lock(&q->limits_lock);
99673e3715eSChristoph Hellwig return q->limits;
99773e3715eSChristoph Hellwig }
99873e3715eSChristoph Hellwig int queue_limits_commit_update_frozen(struct request_queue *q,
99973e3715eSChristoph Hellwig struct queue_limits *lim);
100073e3715eSChristoph Hellwig int queue_limits_commit_update(struct request_queue *q,
100173e3715eSChristoph Hellwig struct queue_limits *lim);
100273e3715eSChristoph Hellwig int queue_limits_set(struct request_queue *q, struct queue_limits *lim);
100373e3715eSChristoph Hellwig int blk_validate_limits(struct queue_limits *lim);
100473e3715eSChristoph Hellwig
100573e3715eSChristoph Hellwig /**
100673e3715eSChristoph Hellwig * queue_limits_cancel_update - cancel an atomic update of queue limits
100773e3715eSChristoph Hellwig * @q: queue to update
100873e3715eSChristoph Hellwig *
100973e3715eSChristoph Hellwig * This functions cancels an atomic update of the queue limits started by
101073e3715eSChristoph Hellwig * queue_limits_start_update() and should be used when an error occurs after
101173e3715eSChristoph Hellwig * starting update.
101273e3715eSChristoph Hellwig */
queue_limits_cancel_update(struct request_queue * q)101373e3715eSChristoph Hellwig static inline void queue_limits_cancel_update(struct request_queue *q)
101473e3715eSChristoph Hellwig {
101573e3715eSChristoph Hellwig mutex_unlock(&q->limits_lock);
101673e3715eSChristoph Hellwig }
101773e3715eSChristoph Hellwig
10181da177e4SLinus Torvalds /*
10191da177e4SLinus Torvalds * These helpers are for drivers that have sloppy feature negotiation and might
1020d278d4a8SJens Axboe * have to disable DISCARD, WRITE_ZEROES or SECURE_DISCARD from the I/O
1021b1bd055dSMartin K. Petersen * completion handler when the device returned an indicator that the respective
1022c72758f3SMartin K. Petersen * feature is not actually supported. They are racy and the driver needs to
1023c72758f3SMartin K. Petersen * cope with that. Try to avoid this scheme if you can.
1024c1373f1cSChristoph Hellwig */
blk_queue_disable_discard(struct request_queue * q)1025c1373f1cSChristoph Hellwig static inline void blk_queue_disable_discard(struct request_queue *q)
1026242f9dcbSJens Axboe {
10272e9bc346SChristoph Hellwig q->limits.max_discard_sectors = 0;
1028a2247f19SDamien Le Moal }
1029a2247f19SDamien Le Moal
blk_queue_disable_secure_erase(struct request_queue * q)1030a2247f19SDamien Le Moal static inline void blk_queue_disable_secure_erase(struct request_queue *q)
1031a2247f19SDamien Le Moal {
1032a2247f19SDamien Le Moal q->limits.max_secure_erase_sectors = 0;
103309ac46c4STejun Heo }
1034165125e1SJens Axboe
blk_queue_disable_write_zeroes(struct request_queue * q)10357a5428dcSChristoph Hellwig static inline void blk_queue_disable_write_zeroes(struct request_queue *q)
10367a5428dcSChristoph Hellwig {
10371da177e4SLinus Torvalds q->limits.max_write_zeroes_sectors = 0;
1038a3396b99SChristoph Hellwig }
1039a3396b99SChristoph Hellwig
1040a3396b99SChristoph Hellwig /*
1041a3396b99SChristoph Hellwig * Access functions for manipulating queue properties
1042a3396b99SChristoph Hellwig */
1043957860cbSJens Axboe extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
1044316cc67dSShaohua Li extern void blk_set_stacking_limits(struct queue_limits *lim);
104575df7136SSuresh Jayaraman extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
104675df7136SSuresh Jayaraman sector_t offset);
104775df7136SSuresh Jayaraman void queue_limits_stack_bdev(struct queue_limits *t, struct block_device *bdev,
104875df7136SSuresh Jayaraman sector_t offset, const char *pfx);
104975df7136SSuresh Jayaraman extern void blk_queue_rq_timeout(struct request_queue *, unsigned int);
105075df7136SSuresh Jayaraman
105175df7136SSuresh Jayaraman struct blk_independent_access_ranges *
1052008f75a2SChristoph Hellwig disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges);
1053008f75a2SChristoph Hellwig void disk_set_independent_access_ranges(struct gendisk *disk,
1054316cc67dSShaohua Li struct blk_independent_access_ranges *iars);
105573c10101SJens Axboe
1056a3396b99SChristoph Hellwig bool __must_check blk_get_queue(struct request_queue *);
105747c122e3SJens Axboe extern void blk_put_queue(struct request_queue *);
105847c122e3SJens Axboe
1059a3396b99SChristoph Hellwig void blk_mark_disk_dead(struct gendisk *disk);
1060da4c8c3dSJens Axboe
106147c122e3SJens Axboe struct rq_list {
106247c122e3SJens Axboe struct request *head;
10635f0ed774SJens Axboe struct request *tail;
106447c122e3SJens Axboe };
1065ce5b009cSJens Axboe
1066dc5fc361SJens Axboe #ifdef CONFIG_BLOCK
106747c122e3SJens Axboe /*
106847c122e3SJens Axboe * blk_plug permits building a queue of related requests by holding the I/O
106973c10101SJens Axboe * fragments for a short period. This allows merging of sequential requests
107055c022bbSShaohua Li * into single larger request. As the requests are moved from a per-task list to
10719cbb1750SNeilBrown * the device's request_queue in a batch, this results in improved scalability
107274018dc3SNeilBrown * as the lock contention for request_queue lock is reduced.
1073048c9374SNeilBrown *
1074048c9374SNeilBrown * It is ok not to disable preemption when adding the request to the plug list
10759cbb1750SNeilBrown * or when attempting a merge. For details, please see schedule() where
10769cbb1750SNeilBrown * blk_flush_plug() is called.
1077048c9374SNeilBrown */
10789cbb1750SNeilBrown struct blk_plug {
10799cbb1750SNeilBrown struct rq_list mq_list; /* blk-mq requests */
108073c10101SJens Axboe
108147c122e3SJens Axboe /* if ios_left is > 1, we can batch tag/rq allocations */
108273c10101SJens Axboe struct rq_list cached_rqs;
108373c10101SJens Axboe u64 cur_ktime;
1084aa8dcccaSChristoph Hellwig unsigned short nr_ios;
1085aa8dcccaSChristoph Hellwig
108673c10101SJens Axboe unsigned short rq_count;
1087aa8dcccaSChristoph Hellwig
1088aa8dcccaSChristoph Hellwig bool multiple_queues;
108973c10101SJens Axboe bool has_elevator;
109073c10101SJens Axboe
109106b23f92SJens Axboe struct list_head cb_list; /* md requires an unplug callback */
109206b23f92SJens Axboe };
109306b23f92SJens Axboe
109406b23f92SJens Axboe struct blk_plug_cb;
109506b23f92SJens Axboe typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool);
109606b23f92SJens Axboe struct blk_plug_cb {
109706b23f92SJens Axboe struct list_head list;
109806b23f92SJens Axboe blk_plug_cb_fn callback;
109906b23f92SJens Axboe void *data;
110006b23f92SJens Axboe };
110106b23f92SJens Axboe extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug,
110206b23f92SJens Axboe void *data, int size);
1103c6bf3f0eSChristoph Hellwig extern void blk_start_plug(struct blk_plug *);
11041a4dcfa8SChristoph Hellwig extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short);
11051a4dcfa8SChristoph Hellwig extern void blk_finish_plug(struct blk_plug *);
11061a4dcfa8SChristoph Hellwig
11071a4dcfa8SChristoph Hellwig void __blk_flush_plug(struct blk_plug *plug, bool from_schedule);
blk_flush_plug(struct blk_plug * plug,bool async)11081a4dcfa8SChristoph Hellwig static inline void blk_flush_plug(struct blk_plug *plug, bool async)
110947c122e3SJens Axboe {
111047c122e3SJens Axboe if (plug)
111147c122e3SJens Axboe __blk_flush_plug(plug, async);
111247c122e3SJens Axboe }
111347c122e3SJens Axboe
11141a4dcfa8SChristoph Hellwig /*
11151a4dcfa8SChristoph Hellwig * tsk == current here
11161a4dcfa8SChristoph Hellwig */
blk_plug_invalidate_ts(struct task_struct * tsk)11171a4dcfa8SChristoph Hellwig static inline void blk_plug_invalidate_ts(struct task_struct *tsk)
11181a4dcfa8SChristoph Hellwig {
11191a4dcfa8SChristoph Hellwig struct blk_plug *plug = tsk->plug;
11201a4dcfa8SChristoph Hellwig
11211a4dcfa8SChristoph Hellwig if (plug)
1122008f75a2SChristoph Hellwig plug->cur_ktime = 0;
11231a4dcfa8SChristoph Hellwig current->flags &= ~PF_BLOCK_TS;
11241a4dcfa8SChristoph Hellwig }
11251a4dcfa8SChristoph Hellwig
112606b23f92SJens Axboe int blkdev_issue_flush(struct block_device *bdev);
112706b23f92SJens Axboe long nr_blockdev_pages(void);
112806b23f92SJens Axboe #else /* CONFIG_BLOCK */
112906b23f92SJens Axboe struct blk_plug {
1130c6bf3f0eSChristoph Hellwig };
11311a4dcfa8SChristoph Hellwig
blk_start_plug_nr_ios(struct blk_plug * plug,unsigned short nr_ios)11321a4dcfa8SChristoph Hellwig static inline void blk_start_plug_nr_ios(struct blk_plug *plug,
11331a4dcfa8SChristoph Hellwig unsigned short nr_ios)
11341a4dcfa8SChristoph Hellwig {
11351a4dcfa8SChristoph Hellwig }
11361a4dcfa8SChristoph Hellwig
blk_start_plug(struct blk_plug * plug)11371a4dcfa8SChristoph Hellwig static inline void blk_start_plug(struct blk_plug *plug)
11381a4dcfa8SChristoph Hellwig {
11391a4dcfa8SChristoph Hellwig }
11401a4dcfa8SChristoph Hellwig
blk_finish_plug(struct blk_plug * plug)114171ac860aSMing Lei static inline void blk_finish_plug(struct blk_plug *plug)
114271ac860aSMing Lei {
114344abff2cSChristoph Hellwig }
114444abff2cSChristoph Hellwig
blk_flush_plug(struct blk_plug * plug,bool async)114544abff2cSChristoph Hellwig static inline void blk_flush_plug(struct blk_plug *plug, bool async)
114644abff2cSChristoph Hellwig {
114744abff2cSChristoph Hellwig }
114844abff2cSChristoph Hellwig
blk_plug_invalidate_ts(struct task_struct * tsk)1149ee472d83SChristoph Hellwig static inline void blk_plug_invalidate_ts(struct task_struct *tsk)
1150ee472d83SChristoph Hellwig {
1151cb365b96SChristoph Hellwig }
1152bf86bcdbSChristoph Hellwig
blkdev_issue_flush(struct block_device * bdev)1153ee472d83SChristoph Hellwig static inline int blkdev_issue_flush(struct block_device *bdev)
1154e73c23ffSChaitanya Kulkarni {
1155e73c23ffSChaitanya Kulkarni return 0;
1156ee472d83SChristoph Hellwig }
11573f14d792SDmitry Monakhov
nr_blockdev_pages(void)1158ee472d83SChristoph Hellwig static inline long nr_blockdev_pages(void)
1159ee472d83SChristoph Hellwig {
11602cf6d26aSChristoph Hellwig return 0;
11612cf6d26aSChristoph Hellwig }
1162fb2dce86SDavid Woodhouse #endif /* CONFIG_BLOCK */
1163233bde21SBart Van Assche
1164233bde21SBart Van Assche extern void blk_io_schedule(void);
1165233bde21SBart Van Assche
1166233bde21SBart Van Assche int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1167233bde21SBart Van Assche sector_t nr_sects, gfp_t gfp_mask);
116844abff2cSChristoph Hellwig int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
1169fb2dce86SDavid Woodhouse sector_t nr_sects, gfp_t gfp_mask, struct bio **biop);
1170e6fa0be6SLukas Czerner int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
1171a107e5a3STheodore Ts'o sector_t nr_sects, gfp_t gfp);
1172e6fa0be6SLukas Czerner
1173e6fa0be6SLukas Czerner #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */
1174233bde21SBart Van Assche #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */
1175233bde21SBart Van Assche #define BLKDEV_ZERO_KILLABLE (1 << 2) /* interruptible by fatal signals */
1176233bde21SBart Van Assche
1177233bde21SBart Van Assche extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1178ee472d83SChristoph Hellwig sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
1179e6fa0be6SLukas Czerner unsigned flags);
11801da177e4SLinus Torvalds extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
1181fa01b1e9SChristoph Hellwig sector_t nr_sects, gfp_t gfp_mask, unsigned flags);
1182fa01b1e9SChristoph Hellwig
sb_issue_discard(struct super_block * sb,sector_t block,sector_t nr_blocks,gfp_t gfp_mask,unsigned long flags)1183b8c873edSAl Viro static inline int sb_issue_discard(struct super_block *sb, sector_t block,
1184fa01b1e9SChristoph Hellwig sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags)
1185fa01b1e9SChristoph Hellwig {
1186eb28d31bSMartin K. Petersen return blkdev_issue_discard(sb->s_bdev,
1187eb28d31bSMartin K. Petersen block << (sb->s_blocksize_bits -
1188eb28d31bSMartin K. Petersen SECTOR_SHIFT),
1189eb28d31bSMartin K. Petersen nr_blocks << (sb->s_blocksize_bits -
1190eb28d31bSMartin K. Petersen SECTOR_SHIFT),
1191eb28d31bSMartin K. Petersen gfp_mask);
11920e435ac2SMilan Broz }
sb_issue_zeroout(struct super_block * sb,sector_t block,sector_t nr_blocks,gfp_t gfp_mask)1193d6b9f4e6SChristoph Hellwig static inline int sb_issue_zeroout(struct super_block *sb, sector_t block,
1194d6b9f4e6SChristoph Hellwig sector_t nr_blocks, gfp_t gfp_mask)
1195d6b9f4e6SChristoph Hellwig {
1196d6b9f4e6SChristoph Hellwig return blkdev_issue_zeroout(sb->s_bdev,
1197d6b9f4e6SChristoph Hellwig block << (sb->s_blocksize_bits -
1198d6b9f4e6SChristoph Hellwig SECTOR_SHIFT),
1199d6b9f4e6SChristoph Hellwig nr_blocks << (sb->s_blocksize_bits -
1200d6b9f4e6SChristoph Hellwig SECTOR_SHIFT),
12010a26f327SKeith Busch gfp_mask, 0);
12022f5a65efSChristoph Hellwig }
12032f5a65efSChristoph Hellwig
bdev_is_partition(struct block_device * bdev)12042f5a65efSChristoph Hellwig static inline bool bdev_is_partition(struct block_device *bdev)
12052f5a65efSChristoph Hellwig {
12062f5a65efSChristoph Hellwig return bdev_partno(bdev) != 0;
1207af2c68feSBart Van Assche }
1208ae03bf63SMartin K. Petersen
1209025146e1SMartin K. Petersen enum blk_default_limits {
1210ae03bf63SMartin K. Petersen BLK_MAX_SEGMENTS = 128,
1211ae03bf63SMartin K. Petersen BLK_SAFE_MAX_SECTORS = 255,
1212af2c68feSBart Van Assche BLK_MAX_SEGMENT_SIZE = 65536,
121303100aadSKeith Busch BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL,
121403100aadSKeith Busch };
121503100aadSKeith Busch
121603100aadSKeith Busch /*
1217af2c68feSBart Van Assche * Default upper limit for the software max_sectors limit used for
1218ae03bf63SMartin K. Petersen * regular file system I/O. This can be increased through sysfs.
1219025146e1SMartin K. Petersen *
1220ae03bf63SMartin K. Petersen * Not to be confused with the max_hw_sector limit that is entirely
1221ae03bf63SMartin K. Petersen * controlled by the driver, usually based on hardware limits.
1222547e2f70SChristoph Hellwig */
1223547e2f70SChristoph Hellwig #define BLK_DEF_MAX_SECTORS_CAP 2560u
1224547e2f70SChristoph Hellwig
bdev_limits(struct block_device * bdev)1225547e2f70SChristoph Hellwig static inline struct queue_limits *bdev_limits(struct block_device *bdev)
1226547e2f70SChristoph Hellwig {
1227af2c68feSBart Van Assche return &bdev_get_queue(bdev)->limits;
1228ae03bf63SMartin K. Petersen }
1229025146e1SMartin K. Petersen
queue_segment_boundary(const struct request_queue * q)1230ae03bf63SMartin K. Petersen static inline unsigned long queue_segment_boundary(const struct request_queue *q)
1231ae03bf63SMartin K. Petersen {
1232af2c68feSBart Van Assche return q->limits.seg_boundary_mask;
1233ae03bf63SMartin K. Petersen }
12348a78362cSMartin K. Petersen
queue_virt_boundary(const struct request_queue * q)1235ae03bf63SMartin K. Petersen static inline unsigned long queue_virt_boundary(const struct request_queue *q)
1236ae03bf63SMartin K. Petersen {
1237af2c68feSBart Van Assche return q->limits.virt_boundary_mask;
12381e739730SChristoph Hellwig }
12391e739730SChristoph Hellwig
queue_max_sectors(const struct request_queue * q)12401e739730SChristoph Hellwig static inline unsigned int queue_max_sectors(const struct request_queue *q)
12411e739730SChristoph Hellwig {
1242af2c68feSBart Van Assche return q->limits.max_sectors;
1243ae03bf63SMartin K. Petersen }
1244025146e1SMartin K. Petersen
queue_max_bytes(struct request_queue * q)1245ae03bf63SMartin K. Petersen static inline unsigned int queue_max_bytes(struct request_queue *q)
1246ae03bf63SMartin K. Petersen {
1247ccdbf0aaSDamien Le Moal return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9;
1248ccdbf0aaSDamien Le Moal }
1249559218d4SChristoph Hellwig
queue_max_hw_sectors(const struct request_queue * q)1250ccdbf0aaSDamien Le Moal static inline unsigned int queue_max_hw_sectors(const struct request_queue *q)
1251ccdbf0aaSDamien Le Moal {
1252ccdbf0aaSDamien Le Moal return q->limits.max_hw_sectors;
1253ccdbf0aaSDamien Le Moal }
1254ccdbf0aaSDamien Le Moal
queue_max_segments(const struct request_queue * q)12550512a75bSKeith Busch static inline unsigned short queue_max_segments(const struct request_queue *q)
12560512a75bSKeith Busch {
12572aba0d19SChristoph Hellwig return q->limits.max_segments;
12582aba0d19SChristoph Hellwig }
12592aba0d19SChristoph Hellwig
queue_max_discard_segments(const struct request_queue * q)1260559218d4SChristoph Hellwig static inline unsigned short queue_max_discard_segments(const struct request_queue *q)
12612aba0d19SChristoph Hellwig {
12622aba0d19SChristoph Hellwig return q->limits.max_discard_segments;
126365ea1b66SNaohiro Aota }
126465ea1b66SNaohiro Aota
queue_max_segment_size(const struct request_queue * q)126565ea1b66SNaohiro Aota static inline unsigned int queue_max_segment_size(const struct request_queue *q)
126665ea1b66SNaohiro Aota {
126765ea1b66SNaohiro Aota return q->limits.max_segment_size;
1268ad6bf88aSMikulas Patocka }
12691da177e4SLinus Torvalds
queue_emulates_zone_append(struct request_queue * q)12705476394aSChristoph Hellwig static inline bool queue_emulates_zone_append(struct request_queue *q)
12711da177e4SLinus Torvalds {
12721da177e4SLinus Torvalds return blk_queue_is_zoned(q) && !q->limits.max_hw_zone_append_sectors;
1273ad6bf88aSMikulas Patocka }
12741da177e4SLinus Torvalds
bdev_emulates_zone_append(struct block_device * bdev)1275e1defc4fSMartin K. Petersen static inline bool bdev_emulates_zone_append(struct block_device *bdev)
12761da177e4SLinus Torvalds {
12771da177e4SLinus Torvalds return queue_emulates_zone_append(bdev_get_queue(bdev));
1278af2c68feSBart Van Assche }
1279c72758f3SMartin K. Petersen
1280c72758f3SMartin K. Petersen static inline unsigned int
bdev_max_zone_append_sectors(struct block_device * bdev)1281c72758f3SMartin K. Petersen bdev_max_zone_append_sectors(struct block_device *bdev)
1282c72758f3SMartin K. Petersen {
1283892b6f90SMartin K. Petersen return bdev_limits(bdev)->max_zone_append_sectors;
1284ac481c20SMartin K. Petersen }
1285ac481c20SMartin K. Petersen
bdev_max_segments(struct block_device * bdev)1286ac481c20SMartin K. Petersen static inline unsigned int bdev_max_segments(struct block_device *bdev)
1287ac481c20SMartin K. Petersen {
1288af2c68feSBart Van Assche return queue_max_segments(bdev_get_queue(bdev));
1289c72758f3SMartin K. Petersen }
1290c72758f3SMartin K. Petersen
queue_logical_block_size(const struct request_queue * q)1291c72758f3SMartin K. Petersen static inline unsigned queue_logical_block_size(const struct request_queue *q)
1292c72758f3SMartin K. Petersen {
129346fd48abSChristoph Hellwig return q->limits.logical_block_size;
1294ac481c20SMartin K. Petersen }
1295ac481c20SMartin K. Petersen
bdev_logical_block_size(struct block_device * bdev)1296ac481c20SMartin K. Petersen static inline unsigned int bdev_logical_block_size(struct block_device *bdev)
1297ac481c20SMartin K. Petersen {
1298af2c68feSBart Van Assche return queue_logical_block_size(bdev_get_queue(bdev));
1299c72758f3SMartin K. Petersen }
1300c72758f3SMartin K. Petersen
queue_physical_block_size(const struct request_queue * q)1301c72758f3SMartin K. Petersen static inline unsigned int queue_physical_block_size(const struct request_queue *q)
1302c72758f3SMartin K. Petersen {
13035a9d1b83SChristoph Hellwig return q->limits.physical_block_size;
1304ac481c20SMartin K. Petersen }
1305ac481c20SMartin K. Petersen
bdev_physical_block_size(struct block_device * bdev)1306ac481c20SMartin K. Petersen static inline unsigned int bdev_physical_block_size(struct block_device *bdev)
1307ac481c20SMartin K. Petersen {
1308a805a4faSDamien Le Moal return queue_physical_block_size(bdev_get_queue(bdev));
1309a805a4faSDamien Le Moal }
1310a805a4faSDamien Le Moal
queue_io_min(const struct request_queue * q)1311a805a4faSDamien Le Moal static inline unsigned int queue_io_min(const struct request_queue *q)
1312a805a4faSDamien Le Moal {
1313a805a4faSDamien Le Moal return q->limits.io_min;
1314a805a4faSDamien Le Moal }
1315a805a4faSDamien Le Moal
bdev_io_min(struct block_device * bdev)1316a805a4faSDamien Le Moal static inline unsigned int bdev_io_min(struct block_device *bdev)
1317a805a4faSDamien Le Moal {
1318a805a4faSDamien Le Moal return queue_io_min(bdev_get_queue(bdev));
1319a805a4faSDamien Le Moal }
132089098b07SChristoph Hellwig
queue_io_opt(const struct request_queue * q)13215c4b4a5cSChristoph Hellwig static inline unsigned int queue_io_opt(const struct request_queue *q)
1322c6e66634SPaolo Bonzini {
1323cf0fbf89SChristoph Hellwig return q->limits.io_opt;
1324cf0fbf89SChristoph Hellwig }
13252f5a65efSChristoph Hellwig
bdev_io_opt(struct block_device * bdev)1326cf0fbf89SChristoph Hellwig static inline unsigned int bdev_io_opt(struct block_device *bdev)
1327cf0fbf89SChristoph Hellwig {
13287b47ef52SChristoph Hellwig return queue_io_opt(bdev_get_queue(bdev));
13297b47ef52SChristoph Hellwig }
13302f5a65efSChristoph Hellwig
13317b47ef52SChristoph Hellwig static inline unsigned int
queue_zone_write_granularity(const struct request_queue * q)13327b47ef52SChristoph Hellwig queue_zone_write_granularity(const struct request_queue *q)
133344abff2cSChristoph Hellwig {
133444abff2cSChristoph Hellwig return q->limits.zone_write_granularity;
133544abff2cSChristoph Hellwig }
13362f5a65efSChristoph Hellwig
133744abff2cSChristoph Hellwig static inline unsigned int
bdev_zone_write_granularity(struct block_device * bdev)133844abff2cSChristoph Hellwig bdev_zone_write_granularity(struct block_device *bdev)
1339a6f0788eSChaitanya Kulkarni {
1340a6f0788eSChaitanya Kulkarni return queue_zone_write_granularity(bdev_get_queue(bdev));
13412f5a65efSChristoph Hellwig }
1342a6f0788eSChaitanya Kulkarni
1343a6f0788eSChaitanya Kulkarni int bdev_alignment_offset(struct block_device *bdev);
134410f0d2a5SChristoph Hellwig unsigned int bdev_discard_alignment(struct block_device *bdev);
134510f0d2a5SChristoph Hellwig
bdev_max_discard_sectors(struct block_device * bdev)134610f0d2a5SChristoph Hellwig static inline unsigned int bdev_max_discard_sectors(struct block_device *bdev)
134710f0d2a5SChristoph Hellwig {
134810f0d2a5SChristoph Hellwig return bdev_limits(bdev)->max_discard_sectors;
13493222d8c2SChristoph Hellwig }
13503222d8c2SChristoph Hellwig
bdev_discard_granularity(struct block_device * bdev)1351aadd5c59SChristoph Hellwig static inline unsigned int bdev_discard_granularity(struct block_device *bdev)
13523222d8c2SChristoph Hellwig {
13533222d8c2SChristoph Hellwig return bdev_limits(bdev)->discard_granularity;
135436d25489SChristoph Hellwig }
135536d25489SChristoph Hellwig
13563c3e85ddSChristoph Hellwig static inline unsigned int
bdev_max_secure_erase_sectors(struct block_device * bdev)13573c3e85ddSChristoph Hellwig bdev_max_secure_erase_sectors(struct block_device *bdev)
1358c6e56cf6SChristoph Hellwig {
1359c6e56cf6SChristoph Hellwig return bdev_limits(bdev)->max_secure_erase_sectors;
13603c3e85ddSChristoph Hellwig }
13611a02f3a7SChristoph Hellwig
bdev_write_zeroes_sectors(struct block_device * bdev)136236d25489SChristoph Hellwig static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev)
136336d25489SChristoph Hellwig {
13641122c0c1SChristoph Hellwig return bdev_limits(bdev)->max_write_zeroes_sectors;
13651122c0c1SChristoph Hellwig }
13661122c0c1SChristoph Hellwig
bdev_nonrot(struct block_device * bdev)1367bae1c743SChristoph Hellwig static inline bool bdev_nonrot(struct block_device *bdev)
136808e688fdSChristoph Hellwig {
1369a557e82eSChristoph Hellwig return blk_queue_nonrot(bdev_get_queue(bdev));
1370a557e82eSChristoph Hellwig }
1371a557e82eSChristoph Hellwig
bdev_synchronous(struct block_device * bdev)13721122c0c1SChristoph Hellwig static inline bool bdev_synchronous(struct block_device *bdev)
1373797476b8SDamien Le Moal {
1374797476b8SDamien Le Moal return bdev->bd_disk->queue->limits.features & BLK_FEAT_SYNCHRONOUS;
1375a557e82eSChristoph Hellwig }
1376a557e82eSChristoph Hellwig
bdev_stable_writes(struct block_device * bdev)13772f5a65efSChristoph Hellwig static inline bool bdev_stable_writes(struct block_device *bdev)
1378a557e82eSChristoph Hellwig {
1379a557e82eSChristoph Hellwig struct request_queue *q = bdev_get_queue(bdev);
1380568ec936SChristoph Hellwig
1381568ec936SChristoph Hellwig if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY) &&
1382f76af42fSChristoph Hellwig q->limits.integrity.csum_type != BLK_INTEGRITY_CSUM_NONE)
1383568ec936SChristoph Hellwig return true;
1384568ec936SChristoph Hellwig return q->limits.features & BLK_FEAT_STABLE_WRITES;
1385797476b8SDamien Le Moal }
1386797476b8SDamien Le Moal
blk_queue_write_cache(struct request_queue * q)1387fea127b3SPankaj Raghav static inline bool blk_queue_write_cache(struct request_queue *q)
1388797476b8SDamien Le Moal {
1389797476b8SDamien Le Moal return (q->limits.features & BLK_FEAT_WRITE_CACHE) &&
1390d67ea690SPankaj Raghav !(q->limits.flags & BLK_FLAG_WRITE_CACHE_DISABLED);
1391d67ea690SPankaj Raghav }
1392d67ea690SPankaj Raghav
bdev_write_cache(struct block_device * bdev)1393d67ea690SPankaj Raghav static inline bool bdev_write_cache(struct block_device *bdev)
1394d67ea690SPankaj Raghav {
1395113ab72eSDamien Le Moal return blk_queue_write_cache(bdev_get_queue(bdev));
13966a0cb1bcSHannes Reinecke }
13976a0cb1bcSHannes Reinecke
bdev_fua(struct block_device * bdev)13986a0cb1bcSHannes Reinecke static inline bool bdev_fua(struct block_device *bdev)
1399de71973cSChristoph Hellwig {
14006cc77e9cSChristoph Hellwig return bdev_limits(bdev)->features & BLK_FEAT_FUA;
1401de71973cSChristoph Hellwig }
14026cc77e9cSChristoph Hellwig
bdev_nowait(struct block_device * bdev)14036a0cb1bcSHannes Reinecke static inline bool bdev_nowait(struct block_device *bdev)
1404e29b2100SPankaj Raghav {
1405e29b2100SPankaj Raghav return bdev->bd_disk->queue->limits.features & BLK_FEAT_NOWAIT;
1406e29b2100SPankaj Raghav }
1407e29b2100SPankaj Raghav
bdev_is_zoned(struct block_device * bdev)1408e29b2100SPankaj Raghav static inline bool bdev_is_zoned(struct block_device *bdev)
1409e29b2100SPankaj Raghav {
1410b85a3c1bSDamien Le Moal return blk_queue_is_zoned(bdev_get_queue(bdev));
1411b85a3c1bSDamien Le Moal }
1412b85a3c1bSDamien Le Moal
bdev_zone_no(struct block_device * bdev,sector_t sec)1413b85a3c1bSDamien Le Moal static inline unsigned int bdev_zone_no(struct block_device *bdev, sector_t sec)
1414b85a3c1bSDamien Le Moal {
1415b85a3c1bSDamien Le Moal return disk_zone_no(bdev->bd_disk, sec);
1416e29b2100SPankaj Raghav }
1417e29b2100SPankaj Raghav
bdev_zone_sectors(struct block_device * bdev)1418e29b2100SPankaj Raghav static inline sector_t bdev_zone_sectors(struct block_device *bdev)
1419e29b2100SPankaj Raghav {
1420e29b2100SPankaj Raghav struct request_queue *q = bdev_get_queue(bdev);
1421e29b2100SPankaj Raghav
1422f3d9bf05SDamien Le Moal if (!blk_queue_is_zoned(q))
1423f3d9bf05SDamien Le Moal return 0;
1424f3d9bf05SDamien Le Moal return q->limits.chunk_sectors;
1425f3d9bf05SDamien Le Moal }
1426f3d9bf05SDamien Le Moal
bdev_offset_from_zone_start(struct block_device * bdev,sector_t sector)1427f3d9bf05SDamien Le Moal static inline sector_t bdev_offset_from_zone_start(struct block_device *bdev,
1428f3d9bf05SDamien Le Moal sector_t sector)
1429f3d9bf05SDamien Le Moal {
1430f3d9bf05SDamien Le Moal return sector & (bdev_zone_sectors(bdev) - 1);
1431f3d9bf05SDamien Le Moal }
1432f3d9bf05SDamien Le Moal
bio_offset_from_zone_start(struct bio * bio)1433f3d9bf05SDamien Le Moal static inline sector_t bio_offset_from_zone_start(struct bio *bio)
1434f3d9bf05SDamien Le Moal {
1435f3d9bf05SDamien Le Moal return bdev_offset_from_zone_start(bio->bi_bdev,
1436f3d9bf05SDamien Le Moal bio->bi_iter.bi_sector);
1437f3d9bf05SDamien Le Moal }
1438f3d9bf05SDamien Le Moal
bdev_is_zone_start(struct block_device * bdev,sector_t sector)1439f3d9bf05SDamien Le Moal static inline bool bdev_is_zone_start(struct block_device *bdev,
1440f3d9bf05SDamien Le Moal sector_t sector)
1441f3d9bf05SDamien Le Moal {
1442f3d9bf05SDamien Le Moal return bdev_offset_from_zone_start(bdev, sector) == 0;
1443f3d9bf05SDamien Le Moal }
1444f3d9bf05SDamien Le Moal
1445f3d9bf05SDamien Le Moal /**
1446f3d9bf05SDamien Le Moal * bdev_zone_is_seq - check if a sector belongs to a sequential write zone
1447f3d9bf05SDamien Le Moal * @bdev: block device to check
1448f3d9bf05SDamien Le Moal * @sector: sector number
1449b76b840fSDamien Le Moal *
1450b76b840fSDamien Le Moal * Check if @sector on @bdev is contained in a sequential write required zone.
1451b76b840fSDamien Le Moal */
bdev_zone_is_seq(struct block_device * bdev,sector_t sector)1452ed5db174SChristoph Hellwig static inline bool bdev_zone_is_seq(struct block_device *bdev, sector_t sector)
14531da177e4SLinus Torvalds {
1454abfc9d81SChristoph Hellwig bool is_seq = false;
14551da177e4SLinus Torvalds
14561da177e4SLinus Torvalds #if IS_ENABLED(CONFIG_BLK_DEV_ZONED)
14579da3d1e9SJohn Garry if (bdev_is_zoned(bdev)) {
14589da3d1e9SJohn Garry struct gendisk *disk = bdev->bd_disk;
14599da3d1e9SJohn Garry unsigned long *bitmap;
14609da3d1e9SJohn Garry
14619da3d1e9SJohn Garry rcu_read_lock();
14629da3d1e9SJohn Garry bitmap = rcu_dereference(disk->conv_zones_bitmap);
14639da3d1e9SJohn Garry is_seq = !bitmap ||
14649da3d1e9SJohn Garry !test_bit(disk_zone_no(disk, sector), bitmap);
14659da3d1e9SJohn Garry rcu_read_unlock();
14669da3d1e9SJohn Garry }
14679da3d1e9SJohn Garry #endif
14689da3d1e9SJohn Garry
14699da3d1e9SJohn Garry return is_seq;
14709da3d1e9SJohn Garry }
14719da3d1e9SJohn Garry
14729da3d1e9SJohn Garry int blk_zone_issue_zeroout(struct block_device *bdev, sector_t sector,
14739da3d1e9SJohn Garry sector_t nr_sects, gfp_t gfp_mask);
14749da3d1e9SJohn Garry
queue_dma_alignment(const struct request_queue * q)14759da3d1e9SJohn Garry static inline unsigned int queue_dma_alignment(const struct request_queue *q)
14769da3d1e9SJohn Garry {
14779da3d1e9SJohn Garry return q->limits.dma_alignment;
14789da3d1e9SJohn Garry }
14791da177e4SLinus Torvalds
14801da177e4SLinus Torvalds static inline unsigned int
queue_atomic_write_unit_max_bytes(const struct request_queue * q)14814a2dcc35SKeith Busch queue_atomic_write_unit_max_bytes(const struct request_queue *q)
14824a2dcc35SKeith Busch {
14834a2dcc35SKeith Busch return q->limits.atomic_write_unit_max;
14844a2dcc35SKeith Busch }
14854a2dcc35SKeith Busch
14865debd969SKeith Busch static inline unsigned int
queue_atomic_write_unit_min_bytes(const struct request_queue * q)14875debd969SKeith Busch queue_atomic_write_unit_min_bytes(const struct request_queue *q)
14885debd969SKeith Busch {
14895debd969SKeith Busch return q->limits.atomic_write_unit_min;
14905debd969SKeith Busch }
14915debd969SKeith Busch
14925debd969SKeith Busch static inline unsigned int
queue_atomic_write_boundary_bytes(const struct request_queue * q)1493e769489aSChristoph Hellwig queue_atomic_write_boundary_bytes(const struct request_queue *q)
1494e769489aSChristoph Hellwig {
1495e94b45d0SChristoph Hellwig return q->limits.atomic_write_boundary_sectors << SECTOR_SHIFT;
1496e94b45d0SChristoph Hellwig }
1497e94b45d0SChristoph Hellwig
1498e94b45d0SChristoph Hellwig static inline unsigned int
queue_atomic_write_max_bytes(const struct request_queue * q)1499da77d9b2SChristoph Hellwig queue_atomic_write_max_bytes(const struct request_queue *q)
150087904074SFUJITA Tomonori {
150187904074SFUJITA Tomonori return q->limits.atomic_write_max_sectors << SECTOR_SHIFT;
1502e94b45d0SChristoph Hellwig }
1503e94b45d0SChristoph Hellwig
bdev_dma_alignment(struct block_device * bdev)150414417799SNamhyung Kim static inline unsigned int bdev_dma_alignment(struct block_device *bdev)
150587904074SFUJITA Tomonori {
150687904074SFUJITA Tomonori return queue_dma_alignment(bdev_get_queue(bdev));
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds
bdev_iter_is_aligned(struct block_device * bdev,struct iov_iter * iter)15091da177e4SLinus Torvalds static inline bool bdev_iter_is_aligned(struct block_device *bdev,
1510adff2158SDawei Li struct iov_iter *iter)
15111da177e4SLinus Torvalds {
15121da177e4SLinus Torvalds return iov_iter_is_aligned(iter, bdev_dma_alignment(bdev),
151359c3d45eSJens Axboe bdev_logical_block_size(bdev) - 1);
1514818cd1cbSJens Axboe }
15151da177e4SLinus Torvalds
15161da177e4SLinus Torvalds static inline unsigned int
blk_lim_dma_alignment_and_pad(struct queue_limits * lim)15171da177e4SLinus Torvalds blk_lim_dma_alignment_and_pad(struct queue_limits *lim)
15181da177e4SLinus Torvalds {
15191da177e4SLinus Torvalds return lim->dma_alignment | lim->dma_pad_mask;
15201da177e4SLinus Torvalds }
1521d145dc23SSatya Tangirala
blk_rq_aligned(struct request_queue * q,unsigned long addr,unsigned int len)1522d145dc23SSatya Tangirala static inline bool blk_rq_aligned(struct request_queue *q, unsigned long addr,
1523cb77cb5aSEric Biggers unsigned int len)
1524cb77cb5aSEric Biggers {
1525d145dc23SSatya Tangirala unsigned int alignment = blk_lim_dma_alignment_and_pad(&q->limits);
1526d145dc23SSatya Tangirala
1527d145dc23SSatya Tangirala return !(addr & alignment) && !(len & alignment);
1528cb77cb5aSEric Biggers }
1529d145dc23SSatya Tangirala
1530d145dc23SSatya Tangirala /* assumes size > 256 */
blksize_bits(unsigned int size)1531d145dc23SSatya Tangirala static inline unsigned int blksize_bits(unsigned int size)
1532d145dc23SSatya Tangirala {
1533d145dc23SSatya Tangirala return order_base_2(size >> SECTOR_SHIFT) + SECTOR_SHIFT;
1534d145dc23SSatya Tangirala }
1535d145dc23SSatya Tangirala
15369208d414SChristoph Hellwig int kblockd_schedule_work(struct work_struct *work);
15379208d414SChristoph Hellwig int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay);
15389208d414SChristoph Hellwig
15399208d414SChristoph Hellwig #define MODULE_ALIAS_BLOCKDEV(major,minor) \
15409208d414SChristoph Hellwig MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor))
15419208d414SChristoph Hellwig #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \
15429208d414SChristoph Hellwig MODULE_ALIAS("block-major-" __stringify(major) "-*")
154308f85851SAl Viro
15443e08773cSChristoph Hellwig #ifdef CONFIG_BLK_INLINE_ENCRYPTION
154569fe0f29SMing Lei
154669fe0f29SMing Lei bool blk_crypto_register(struct blk_crypto_profile *profile,
154705bdb996SChristoph Hellwig struct request_queue *q);
1548ae220766SChristoph Hellwig
154905bdb996SChristoph Hellwig #else /* CONFIG_BLK_INLINE_ENCRYPTION */
155005bdb996SChristoph Hellwig
blk_crypto_register(struct blk_crypto_profile * profile,struct request_queue * q)155105bdb996SChristoph Hellwig static inline bool blk_crypto_register(struct blk_crypto_profile *profile,
155205bdb996SChristoph Hellwig struct request_queue *q)
155377ea887eSTejun Heo {
155477ea887eSTejun Heo return true;
1555c3e33e04STejun Heo }
155608f85851SAl Viro
1557e00adcadSChristoph Hellwig #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
155876792055SChristoph Hellwig
1559b3a27d05SNitin Gupta enum blk_unique_id {
1560b3a27d05SNitin Gupta /* these match the Designator Types specified in SPC */
1561e76239a3SChristoph Hellwig BLK_UID_T10 = 1,
1562d4100351SChristoph Hellwig BLK_UID_EUI64 = 2,
1563050a4f34SJens Axboe BLK_UID_NAA = 3,
15649208d414SChristoph Hellwig };
15659208d414SChristoph Hellwig
15669208d414SChristoph Hellwig struct block_device_operations {
156708f85851SAl Viro void (*submit_bio)(struct bio *bio);
1568bbd3e064SChristoph Hellwig int (*poll_bio)(struct bio *bio, struct io_comp_batch *iob,
15690bdfbca8SDmitry Osipenko unsigned int flags);
15700bdfbca8SDmitry Osipenko int (*open)(struct gendisk *disk, blk_mode_t mode);
15710bdfbca8SDmitry Osipenko void (*release)(struct gendisk *disk);
15720bdfbca8SDmitry Osipenko int (*ioctl)(struct block_device *bdev, blk_mode_t mode,
15730bdfbca8SDmitry Osipenko unsigned cmd, unsigned long arg);
15740bdfbca8SDmitry Osipenko int (*compat_ioctl)(struct block_device *bdev, blk_mode_t mode,
15750bdfbca8SDmitry Osipenko unsigned cmd, unsigned long arg);
157608f85851SAl Viro unsigned int (*check_events) (struct gendisk *disk,
157708f85851SAl Viro unsigned int clearing);
1578ee6a129dSArnd Bergmann void (*unlock_native_capacity) (struct gendisk *);
157905bdb996SChristoph Hellwig int (*getgeo)(struct block_device *, struct hd_geometry *);
1580ee6a129dSArnd Bergmann int (*set_read_only)(struct block_device *bdev, bool ro);
1581ee6a129dSArnd Bergmann void (*free_disk)(struct gendisk *disk);
1582ee6a129dSArnd Bergmann /* this callback is with swap_lock and sometimes page table lock held */
1583ee6a129dSArnd Bergmann void (*swap_slot_free_notify) (struct block_device *, unsigned long);
1584ee6a129dSArnd Bergmann int (*report_zones)(struct gendisk *, sector_t sector,
15850619317fSJens Axboe unsigned int nr_zones, report_zones_cb cb, void *data);
15860619317fSJens Axboe char *(*devnode)(struct gendisk *disk, umode_t *mode);
15870619317fSJens Axboe /* returns the length of the identifier or a negative errno: */
15880619317fSJens Axboe int (*get_unique_id)(struct gendisk *disk, u8 id[16],
15890619317fSJens Axboe enum blk_unique_id id_type);
15900619317fSJens Axboe struct module *owner;
15910619317fSJens Axboe const struct pr_ops *pr_ops;
15920619317fSJens Axboe
15930619317fSJens Axboe /*
15940619317fSJens Axboe * Special callback for probing GPT entry at a given sector.
15950619317fSJens Axboe * Needed by Android devices, used by GPT scanner and MMC blk
15960619317fSJens Axboe * driver.
15970619317fSJens Axboe */
15985f275713SYu Kuai int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
15995f0614a5SMing Lei };
160077e7ffd7SBart Van Assche
16015f275713SYu Kuai #ifdef CONFIG_COMPAT
1602956d510eSChristoph Hellwig extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t,
160399dfc43eSChristoph Hellwig unsigned int, unsigned long);
160499dfc43eSChristoph Hellwig #else
160599dfc43eSChristoph Hellwig #define blkdev_compat_ptr_ioctl NULL
1606956d510eSChristoph Hellwig #endif
1607956d510eSChristoph Hellwig
blk_wake_io_task(struct task_struct * waiter)1608956d510eSChristoph Hellwig static inline void blk_wake_io_task(struct task_struct *waiter)
1609956d510eSChristoph Hellwig {
1610b42c1fc3SChristoph Hellwig /*
1611956d510eSChristoph Hellwig * If we're polling, the task itself is doing the completions. For
1612956d510eSChristoph Hellwig * that case, we don't need to signal a wakeup, it's enough to just
1613956d510eSChristoph Hellwig * mark us as RUNNING.
161499dfc43eSChristoph Hellwig */
1615956d510eSChristoph Hellwig if (waiter == current)
1616956d510eSChristoph Hellwig __set_current_state(TASK_RUNNING);
1617*e03463d2SDarrick J. Wong else
1618ead083aeSAl Viro wake_up_process(waiter);
16193f1266f1SChristoph Hellwig }
16204e7b5671SChristoph Hellwig
16213f1266f1SChristoph Hellwig unsigned long bdev_start_io_acct(struct block_device *bdev, enum req_op op,
16223f1266f1SChristoph Hellwig unsigned long start_time);
16233f1266f1SChristoph Hellwig void bdev_end_io_acct(struct block_device *bdev, enum req_op op,
16243f1266f1SChristoph Hellwig unsigned int sectors, unsigned long start_time);
16253f1266f1SChristoph Hellwig
16263f1266f1SChristoph Hellwig unsigned long bio_start_io_acct(struct bio *bio);
16273f1266f1SChristoph Hellwig void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
16283f1266f1SChristoph Hellwig struct block_device *orig_bdev);
16293f1266f1SChristoph Hellwig
16301da177e4SLinus Torvalds /**
16313f1266f1SChristoph Hellwig * bio_end_io_acct - end I/O accounting for bio based drivers
16320718afd4SChristoph Hellwig * @bio: bio to end account for
1633d8530de5SChristoph Hellwig * @start_time: start time returned by bio_start_io_acct()
16342142b88cSChristoph Hellwig */
bio_end_io_acct(struct bio * bio,unsigned long start_time)16352142b88cSChristoph Hellwig static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time)
16362142b88cSChristoph Hellwig {
16372142b88cSChristoph Hellwig return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev);
16382142b88cSChristoph Hellwig }
1639a30561a9SChristian Brauner
1640a30561a9SChristian Brauner int bdev_validate_blocksize(struct block_device *bdev, int block_size);
1641a30561a9SChristian Brauner int set_blocksize(struct file *file, int size);
1642a30561a9SChristian Brauner
1643a30561a9SChristian Brauner int lookup_bdev(const char *pathname, dev_t *dev);
1644a30561a9SChristian Brauner
1645a30561a9SChristian Brauner void blkdev_show(struct seq_file *seqf, off_t offset);
1646a30561a9SChristian Brauner
1647a30561a9SChristian Brauner #define BDEVNAME_SIZE 32 /* Largest string for a blockdev identifier */
1648a30561a9SChristian Brauner #define BDEVT_SIZE 10 /* Largest string for MAJ:MIN for blkdev */
16490718afd4SChristoph Hellwig #ifdef CONFIG_BLOCK
16500718afd4SChristoph Hellwig #define BLKDEV_MAJOR_MAX 512
1651e419cf3eSChristian Brauner #else
1652e419cf3eSChristian Brauner #define BLKDEV_MAJOR_MAX 0
1653e419cf3eSChristian Brauner #endif
1654e419cf3eSChristian Brauner
1655e419cf3eSChristian Brauner struct blk_holder_ops {
16567ecd0b6fSChristoph Hellwig void (*mark_dead)(struct block_device *bdev, bool surprise);
16577ecd0b6fSChristoph Hellwig
16583f0b3e78SChristoph Hellwig /*
16593f0b3e78SChristoph Hellwig * Sync the file system mounted on the block device.
16603f0b3e78SChristoph Hellwig */
16613f0b3e78SChristoph Hellwig void (*sync)(struct block_device *bdev);
16623f0b3e78SChristoph Hellwig
16636f861765SJan Kara /*
16646f861765SJan Kara * Freeze the file system mounted on the block device.
16653f0b3e78SChristoph Hellwig */
1666f3a60882SChristian Brauner int (*freeze)(struct block_device *bdev);
1667f3a60882SChristian Brauner
1668f3a60882SChristian Brauner /*
1669f3a60882SChristian Brauner * Thaw the file system mounted on the block device.
16700718afd4SChristoph Hellwig */
16710718afd4SChristoph Hellwig int (*thaw)(struct block_device *bdev);
167237c3fc9aSChristoph Hellwig };
16733f1266f1SChristoph Hellwig
1674621c1f42SChristoph Hellwig /*
1675f3a60882SChristian Brauner * For filesystems using @fs_holder_ops, the @holder argument passed to
1676186ddac2SYu Kuai * helpers used to open and claim block devices via
1677186ddac2SYu Kuai * bd_prepare_to_claim() must point to a superblock.
16783f1266f1SChristoph Hellwig */
16793f1266f1SChristoph Hellwig extern const struct blk_holder_ops fs_holder_ops;
16803f1266f1SChristoph Hellwig
16813f1266f1SChristoph Hellwig /*
168297d6fb1bSYuezhang Mo * Return the correct open flags for blkdev_get_by_* for super block flags
168370164eb6SChristoph Hellwig * as stored in sb->s_flags.
16841e03a36bSChristoph Hellwig */
16853e781988SLinus Torvalds #define sb_open_mode(flags) \
1686322cbb50SChristoph Hellwig (BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES | \
16872577f53fSChristoph Hellwig (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE))
16883f1266f1SChristoph Hellwig
16893f1266f1SChristoph Hellwig struct file *bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
16903f1266f1SChristoph Hellwig const struct blk_holder_ops *hops);
16913f1266f1SChristoph Hellwig struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode,
16923f1266f1SChristoph Hellwig void *holder, const struct blk_holder_ops *hops);
16933f1266f1SChristoph Hellwig int bd_prepare_to_claim(struct block_device *bdev, void *holder,
16943f1266f1SChristoph Hellwig const struct blk_holder_ops *hops);
16953f1266f1SChristoph Hellwig void bd_abort_claiming(struct block_device *bdev, void *holder);
169670164eb6SChristoph Hellwig
169770164eb6SChristoph Hellwig struct block_device *I_BDEV(struct inode *inode);
169870164eb6SChristoph Hellwig struct block_device *file_bdev(struct file *bdev_file);
169970164eb6SChristoph Hellwig bool disk_live(struct gendisk *disk);
17001e03a36bSChristoph Hellwig unsigned int block_size(struct block_device *bdev);
17011e03a36bSChristoph Hellwig
17021e03a36bSChristoph Hellwig #ifdef CONFIG_BLOCK
17033e781988SLinus Torvalds void invalidate_bdev(struct block_device *bdev);
17049abcfbd2SPrasad Singamsetty int sync_blockdev(struct block_device *bdev);
17052d985f8cSEric Biggers int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend);
17062d985f8cSEric Biggers int sync_blockdev_nowait(struct block_device *bdev);
1707322cbb50SChristoph Hellwig void sync_bdevs(bool wait);
1708322cbb50SChristoph Hellwig void bdev_statx(const struct path *path, struct kstat *stat, u32 request_mask);
1709322cbb50SChristoph Hellwig void printk_all_partitions(void);
1710cf056a43SChristoph Hellwig int __init early_lookup_bdev(const char *pathname, dev_t *dev);
1711cf056a43SChristoph Hellwig #else
invalidate_bdev(struct block_device * bdev)1712cf056a43SChristoph Hellwig static inline void invalidate_bdev(struct block_device *bdev)
1713cf056a43SChristoph Hellwig {
1714322cbb50SChristoph Hellwig }
sync_blockdev(struct block_device * bdev)1715322cbb50SChristoph Hellwig static inline int sync_blockdev(struct block_device *bdev)
1716982c3b30SChristian Brauner {
1717982c3b30SChristian Brauner return 0;
171822650a99SChristian Brauner }
sync_blockdev_nowait(struct block_device * bdev)17193f1266f1SChristoph Hellwig static inline int sync_blockdev_nowait(struct block_device *bdev)
17205a72e899SJens Axboe {
1721a3396b99SChristoph Hellwig return 0;
17225a72e899SJens Axboe }
sync_bdevs(bool wait)17235a72e899SJens Axboe static inline void sync_bdevs(bool wait)
17245a72e899SJens Axboe {
17255a72e899SJens Axboe }
bdev_statx(const struct path * path,struct kstat * stat,u32 request_mask)17266564862dSJohn Garry static inline void bdev_statx(const struct path *path, struct kstat *stat,
17276564862dSJohn Garry u32 request_mask)
17286564862dSJohn Garry {
17296564862dSJohn Garry }
printk_all_partitions(void)17306564862dSJohn Garry static inline void printk_all_partitions(void)
17316564862dSJohn Garry {
17326564862dSJohn Garry }
early_lookup_bdev(const char * pathname,dev_t * dev)17336564862dSJohn Garry static inline int early_lookup_bdev(const char *pathname, dev_t *dev)
17346564862dSJohn Garry {
17359da3d1e9SJohn Garry return -EINVAL;
17369da3d1e9SJohn Garry }
17379da3d1e9SJohn Garry #endif /* CONFIG_BLOCK */
17389da3d1e9SJohn Garry
17399da3d1e9SJohn Garry int bdev_freeze(struct block_device *bdev);
17409da3d1e9SJohn Garry int bdev_thaw(struct block_device *bdev);
17419da3d1e9SJohn Garry void bdev_fput(struct file *bdev_file);
17429da3d1e9SJohn Garry
17436564862dSJohn Garry struct io_comp_batch {
17446564862dSJohn Garry struct rq_list req_list;
17456564862dSJohn Garry bool need_ts;
17469da3d1e9SJohn Garry void (*complete)(struct io_comp_batch *);
17479da3d1e9SJohn Garry };
17489da3d1e9SJohn Garry
blk_atomic_write_start_sect_aligned(sector_t sector,struct queue_limits * limits)17499da3d1e9SJohn Garry static inline bool blk_atomic_write_start_sect_aligned(sector_t sector,
17501eadb157SJohn Garry struct queue_limits *limits)
17511eadb157SJohn Garry {
17521eadb157SJohn Garry unsigned int alignment = max(limits->atomic_write_hw_unit_min,
17531eadb157SJohn Garry limits->atomic_write_hw_boundary);
17541eadb157SJohn Garry
17551eadb157SJohn Garry return IS_ALIGNED(sector, alignment >> SECTOR_SHIFT);
17561eadb157SJohn Garry }
17571eadb157SJohn Garry
bdev_can_atomic_write(struct block_device * bdev)17581eadb157SJohn Garry static inline bool bdev_can_atomic_write(struct block_device *bdev)
17591eadb157SJohn Garry {
17601eadb157SJohn Garry struct request_queue *bd_queue = bdev->bd_queue;
17611eadb157SJohn Garry struct queue_limits *limits = &bd_queue->limits;
17621eadb157SJohn Garry
17631eadb157SJohn Garry if (!limits->atomic_write_unit_min)
17641eadb157SJohn Garry return false;
17651eadb157SJohn Garry
17665a72e899SJens Axboe if (bdev_is_partition(bdev))
17675a72e899SJens Axboe return blk_atomic_write_start_sect_aligned(bdev->bd_start_sect,
17683f1266f1SChristoph Hellwig limits);
1769
1770 return true;
1771 }
1772
1773 static inline unsigned int
bdev_atomic_write_unit_min_bytes(struct block_device * bdev)1774 bdev_atomic_write_unit_min_bytes(struct block_device *bdev)
1775 {
1776 if (!bdev_can_atomic_write(bdev))
1777 return 0;
1778 return queue_atomic_write_unit_min_bytes(bdev_get_queue(bdev));
1779 }
1780
1781 static inline unsigned int
bdev_atomic_write_unit_max_bytes(struct block_device * bdev)1782 bdev_atomic_write_unit_max_bytes(struct block_device *bdev)
1783 {
1784 if (!bdev_can_atomic_write(bdev))
1785 return 0;
1786 return queue_atomic_write_unit_max_bytes(bdev_get_queue(bdev));
1787 }
1788
1789 #define DEFINE_IO_COMP_BATCH(name) struct io_comp_batch name = { }
1790
1791 #endif /* _LINUX_BLKDEV_H */
1792