cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

blkdev.h (46470B)


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