]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef _LINUX_BLKDEV_H |
3 | #define _LINUX_BLKDEV_H | |
4 | ||
85fd0bc9 | 5 | #include <linux/sched.h> |
1da177e4 LT |
6 | #include <linux/genhd.h> |
7 | #include <linux/list.h> | |
320ae51f | 8 | #include <linux/llist.h> |
b296a6d5 | 9 | #include <linux/minmax.h> |
1da177e4 LT |
10 | #include <linux/timer.h> |
11 | #include <linux/workqueue.h> | |
1da177e4 | 12 | #include <linux/wait.h> |
1da177e4 | 13 | #include <linux/bio.h> |
3e6053d7 | 14 | #include <linux/gfp.h> |
548bc8e1 | 15 | #include <linux/rcupdate.h> |
add703fd | 16 | #include <linux/percpu-refcount.h> |
6a0cb1bc | 17 | #include <linux/blkzoned.h> |
d97e594c | 18 | #include <linux/sbitmap.h> |
1da177e4 | 19 | |
de477254 | 20 | struct module; |
1da177e4 | 21 | struct request_queue; |
1da177e4 | 22 | struct elevator_queue; |
2056a782 | 23 | struct blk_trace; |
3d6392cf JA |
24 | struct request; |
25 | struct sg_io_hdr; | |
3c798398 | 26 | struct blkcg_gq; |
7c94e1c1 | 27 | struct blk_flush_queue; |
3e08773c | 28 | struct kiocb; |
bbd3e064 | 29 | struct pr_ops; |
a7905043 | 30 | struct rq_qos; |
34dbad5d OS |
31 | struct blk_queue_stats; |
32 | struct blk_stat_callback; | |
1b262839 | 33 | struct blk_keyslot_manager; |
1da177e4 | 34 | |
096392e0 | 35 | /* Must be consistent with blk_mq_poll_stats_bkt() */ |
0206319f SB |
36 | #define BLK_MQ_POLL_STATS_BKTS 16 |
37 | ||
29ece8b4 YY |
38 | /* Doing classic polling */ |
39 | #define BLK_MQ_POLL_CLASSIC -1 | |
40 | ||
8bd435b3 TH |
41 | /* |
42 | * Maximum number of blkcg policies allowed to be registered concurrently. | |
43 | * Defined here to simplify include dependency. | |
44 | */ | |
ec645dc9 | 45 | #define BLKCG_MAX_POLS 6 |
8bd435b3 | 46 | |
da6269da | 47 | static inline bool blk_op_is_passthrough(unsigned int op) |
14cb0dc6 | 48 | { |
da6269da | 49 | op &= REQ_OP_MASK; |
14cb0dc6 ML |
50 | return op == REQ_OP_DRV_IN || op == REQ_OP_DRV_OUT; |
51 | } | |
52 | ||
797476b8 DLM |
53 | /* |
54 | * Zoned block device models (zoned limit). | |
3093a479 CH |
55 | * |
56 | * Note: This needs to be ordered from the least to the most severe | |
57 | * restrictions for the inheritance in blk_stack_limits() to work. | |
797476b8 DLM |
58 | */ |
59 | enum blk_zoned_model { | |
3093a479 CH |
60 | BLK_ZONED_NONE = 0, /* Regular block device */ |
61 | BLK_ZONED_HA, /* Host-aware zoned block device */ | |
62 | BLK_ZONED_HM, /* Host-managed zoned block device */ | |
797476b8 DLM |
63 | }; |
64 | ||
9bb33f24 CH |
65 | /* |
66 | * BLK_BOUNCE_NONE: never bounce (default) | |
67 | * BLK_BOUNCE_HIGH: bounce all highmem pages | |
68 | */ | |
69 | enum blk_bounce { | |
70 | BLK_BOUNCE_NONE, | |
71 | BLK_BOUNCE_HIGH, | |
72 | }; | |
73 | ||
025146e1 | 74 | struct queue_limits { |
9bb33f24 | 75 | enum blk_bounce bounce; |
025146e1 | 76 | unsigned long seg_boundary_mask; |
03100aad | 77 | unsigned long virt_boundary_mask; |
025146e1 MP |
78 | |
79 | unsigned int max_hw_sectors; | |
ca369d51 | 80 | unsigned int max_dev_sectors; |
762380ad | 81 | unsigned int chunk_sectors; |
025146e1 MP |
82 | unsigned int max_sectors; |
83 | unsigned int max_segment_size; | |
c72758f3 | 84 | unsigned int physical_block_size; |
ad6bf88a | 85 | unsigned int logical_block_size; |
c72758f3 MP |
86 | unsigned int alignment_offset; |
87 | unsigned int io_min; | |
88 | unsigned int io_opt; | |
67efc925 | 89 | unsigned int max_discard_sectors; |
0034af03 | 90 | unsigned int max_hw_discard_sectors; |
4363ac7c | 91 | unsigned int max_write_same_sectors; |
a6f0788e | 92 | unsigned int max_write_zeroes_sectors; |
0512a75b | 93 | unsigned int max_zone_append_sectors; |
86b37281 MP |
94 | unsigned int discard_granularity; |
95 | unsigned int discard_alignment; | |
a805a4fa | 96 | unsigned int zone_write_granularity; |
025146e1 | 97 | |
8a78362c | 98 | unsigned short max_segments; |
13f05c8d | 99 | unsigned short max_integrity_segments; |
1e739730 | 100 | unsigned short max_discard_segments; |
025146e1 | 101 | |
c72758f3 | 102 | unsigned char misaligned; |
86b37281 | 103 | unsigned char discard_misaligned; |
c78afc62 | 104 | unsigned char raid_partial_stripes_expensive; |
797476b8 | 105 | enum blk_zoned_model zoned; |
025146e1 MP |
106 | }; |
107 | ||
d4100351 CH |
108 | typedef int (*report_zones_cb)(struct blk_zone *zone, unsigned int idx, |
109 | void *data); | |
110 | ||
27ba3e8f DLM |
111 | void blk_queue_set_zoned(struct gendisk *disk, enum blk_zoned_model model); |
112 | ||
6a0cb1bc HR |
113 | #ifdef CONFIG_BLK_DEV_ZONED |
114 | ||
d4100351 CH |
115 | #define BLK_ALL_ZONES ((unsigned int)-1) |
116 | int blkdev_report_zones(struct block_device *bdev, sector_t sector, | |
117 | unsigned int nr_zones, report_zones_cb cb, void *data); | |
9b38bb4b | 118 | unsigned int blkdev_nr_zones(struct gendisk *disk); |
6c1b1da5 AJ |
119 | extern int blkdev_zone_mgmt(struct block_device *bdev, enum req_opf op, |
120 | sector_t sectors, sector_t nr_sectors, | |
121 | gfp_t gfp_mask); | |
e732671a DLM |
122 | int blk_revalidate_disk_zones(struct gendisk *disk, |
123 | void (*update_driver_data)(struct gendisk *disk)); | |
6a0cb1bc | 124 | |
3ed05a98 ST |
125 | extern int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode, |
126 | unsigned int cmd, unsigned long arg); | |
e876df1f AJ |
127 | extern int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode, |
128 | unsigned int cmd, unsigned long arg); | |
3ed05a98 ST |
129 | |
130 | #else /* CONFIG_BLK_DEV_ZONED */ | |
131 | ||
9b38bb4b | 132 | static inline unsigned int blkdev_nr_zones(struct gendisk *disk) |
a91e1380 DLM |
133 | { |
134 | return 0; | |
135 | } | |
bf505456 | 136 | |
3ed05a98 ST |
137 | static inline int blkdev_report_zones_ioctl(struct block_device *bdev, |
138 | fmode_t mode, unsigned int cmd, | |
139 | unsigned long arg) | |
140 | { | |
141 | return -ENOTTY; | |
142 | } | |
143 | ||
e876df1f AJ |
144 | static inline int blkdev_zone_mgmt_ioctl(struct block_device *bdev, |
145 | fmode_t mode, unsigned int cmd, | |
146 | unsigned long arg) | |
3ed05a98 ST |
147 | { |
148 | return -ENOTTY; | |
149 | } | |
150 | ||
6a0cb1bc HR |
151 | #endif /* CONFIG_BLK_DEV_ZONED */ |
152 | ||
d7b76301 | 153 | struct request_queue { |
1da177e4 | 154 | struct request *last_merge; |
b374d18a | 155 | struct elevator_queue *elevator; |
1da177e4 | 156 | |
0549e87c ML |
157 | struct percpu_ref q_usage_counter; |
158 | ||
34dbad5d | 159 | struct blk_queue_stats *stats; |
a7905043 | 160 | struct rq_qos *rq_qos; |
87760e5e | 161 | |
f8a5b122 | 162 | const struct blk_mq_ops *mq_ops; |
320ae51f | 163 | |
320ae51f | 164 | /* sw queues */ |
e6cdb092 | 165 | struct blk_mq_ctx __percpu *queue_ctx; |
320ae51f | 166 | |
d278d4a8 JA |
167 | unsigned int queue_depth; |
168 | ||
320ae51f JA |
169 | /* hw dispatch queues */ |
170 | struct blk_mq_hw_ctx **queue_hw_ctx; | |
171 | unsigned int nr_hw_queues; | |
172 | ||
1da177e4 LT |
173 | /* |
174 | * The queue owner gets to use this for whatever they like. | |
175 | * ll_rw_blk doesn't touch it. | |
176 | */ | |
177 | void *queuedata; | |
178 | ||
1da177e4 | 179 | /* |
d7b76301 | 180 | * various queue flags, see QUEUE_* below |
1da177e4 | 181 | */ |
d7b76301 | 182 | unsigned long queue_flags; |
cd84a62e BVA |
183 | /* |
184 | * Number of contexts that have called blk_set_pm_only(). If this | |
a4d34da7 | 185 | * counter is above zero then only RQF_PM requests are processed. |
cd84a62e BVA |
186 | */ |
187 | atomic_t pm_only; | |
1da177e4 | 188 | |
a73f730d TH |
189 | /* |
190 | * ida allocated id for this queue. Used to index queues from | |
191 | * ioctx. | |
192 | */ | |
193 | int id; | |
194 | ||
0d945c1f | 195 | spinlock_t queue_lock; |
1da177e4 | 196 | |
d152c682 CH |
197 | struct gendisk *disk; |
198 | ||
1da177e4 LT |
199 | /* |
200 | * queue kobject | |
201 | */ | |
202 | struct kobject kobj; | |
203 | ||
320ae51f JA |
204 | /* |
205 | * mq queue kobject | |
206 | */ | |
1db4909e | 207 | struct kobject *mq_kobj; |
320ae51f | 208 | |
ac6fc48c DW |
209 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
210 | struct blk_integrity integrity; | |
211 | #endif /* CONFIG_BLK_DEV_INTEGRITY */ | |
212 | ||
47fafbc7 | 213 | #ifdef CONFIG_PM |
6c954667 | 214 | struct device *dev; |
db04e18d | 215 | enum rpm_status rpm_status; |
6c954667 LM |
216 | #endif |
217 | ||
1da177e4 LT |
218 | /* |
219 | * queue settings | |
220 | */ | |
221 | unsigned long nr_requests; /* Max # of requests */ | |
1da177e4 | 222 | |
e3790c7d | 223 | unsigned int dma_pad_mask; |
1da177e4 LT |
224 | unsigned int dma_alignment; |
225 | ||
1b262839 ST |
226 | #ifdef CONFIG_BLK_INLINE_ENCRYPTION |
227 | /* Inline crypto capabilities */ | |
228 | struct blk_keyslot_manager *ksm; | |
229 | #endif | |
230 | ||
242f9dcb | 231 | unsigned int rq_timeout; |
64f1c21e | 232 | int poll_nsec; |
34dbad5d OS |
233 | |
234 | struct blk_stat_callback *poll_cb; | |
0206319f | 235 | struct blk_rq_stat poll_stat[BLK_MQ_POLL_STATS_BKTS]; |
34dbad5d | 236 | |
242f9dcb | 237 | struct timer_list timeout; |
287922eb | 238 | struct work_struct timeout_work; |
242f9dcb | 239 | |
079a2e3e | 240 | atomic_t nr_active_requests_shared_tags; |
bccf5e26 | 241 | |
079a2e3e | 242 | struct blk_mq_tags *sched_shared_tags; |
d97e594c | 243 | |
a612fddf | 244 | struct list_head icq_list; |
4eef3049 | 245 | #ifdef CONFIG_BLK_CGROUP |
a2b1693b | 246 | DECLARE_BITMAP (blkcg_pols, BLKCG_MAX_POLS); |
3c798398 | 247 | struct blkcg_gq *root_blkg; |
03aa264a | 248 | struct list_head blkg_list; |
4eef3049 | 249 | #endif |
a612fddf | 250 | |
025146e1 MP |
251 | struct queue_limits limits; |
252 | ||
68c43f13 DLM |
253 | unsigned int required_elevator_features; |
254 | ||
6a5ac984 | 255 | #ifdef CONFIG_BLK_DEV_ZONED |
6cc77e9c CH |
256 | /* |
257 | * Zoned block device information for request dispatch control. | |
258 | * nr_zones is the total number of zones of the device. This is always | |
f216fdd7 CH |
259 | * 0 for regular block devices. conv_zones_bitmap is a bitmap of nr_zones |
260 | * bits which indicates if a zone is conventional (bit set) or | |
261 | * sequential (bit clear). seq_zones_wlock is a bitmap of nr_zones | |
6cc77e9c CH |
262 | * bits which indicates if a zone is write locked, that is, if a write |
263 | * request targeting the zone was dispatched. All three fields are | |
264 | * initialized by the low level device driver (e.g. scsi/sd.c). | |
265 | * Stacking drivers (device mappers) may or may not initialize | |
266 | * these fields. | |
ccce20fc BVA |
267 | * |
268 | * Reads of this information must be protected with blk_queue_enter() / | |
269 | * blk_queue_exit(). Modifying this information is only allowed while | |
270 | * no requests are being processed. See also blk_mq_freeze_queue() and | |
271 | * blk_mq_unfreeze_queue(). | |
6cc77e9c CH |
272 | */ |
273 | unsigned int nr_zones; | |
f216fdd7 | 274 | unsigned long *conv_zones_bitmap; |
6cc77e9c | 275 | unsigned long *seq_zones_wlock; |
e15864f8 | 276 | unsigned int max_open_zones; |
659bf827 | 277 | unsigned int max_active_zones; |
6a5ac984 | 278 | #endif /* CONFIG_BLK_DEV_ZONED */ |
6cc77e9c | 279 | |
1946089a | 280 | int node; |
85e0cbbb | 281 | struct mutex debugfs_mutex; |
6c5c9341 | 282 | #ifdef CONFIG_BLK_DEV_IO_TRACE |
c780e86d | 283 | struct blk_trace __rcu *blk_trace; |
6c5c9341 | 284 | #endif |
1da177e4 | 285 | /* |
4913efe4 | 286 | * for flush operations |
1da177e4 | 287 | */ |
7c94e1c1 | 288 | struct blk_flush_queue *fq; |
483f4afc | 289 | |
6fca6a61 CH |
290 | struct list_head requeue_list; |
291 | spinlock_t requeue_lock; | |
2849450a | 292 | struct delayed_work requeue_work; |
6fca6a61 | 293 | |
483f4afc | 294 | struct mutex sysfs_lock; |
cecf5d87 | 295 | struct mutex sysfs_dir_lock; |
d351af01 | 296 | |
2f8f1336 ML |
297 | /* |
298 | * for reusing dead hctx instance in case of updating | |
299 | * nr_hw_queues | |
300 | */ | |
301 | struct list_head unused_hctx_list; | |
302 | spinlock_t unused_hctx_lock; | |
303 | ||
7996a8b5 | 304 | int mq_freeze_depth; |
d732580b | 305 | |
e43473b7 VG |
306 | #ifdef CONFIG_BLK_DEV_THROTTLING |
307 | /* Throttle data */ | |
308 | struct throtl_data *td; | |
309 | #endif | |
548bc8e1 | 310 | struct rcu_head rcu_head; |
320ae51f | 311 | wait_queue_head_t mq_freeze_wq; |
7996a8b5 BL |
312 | /* |
313 | * Protect concurrent access to q_usage_counter by | |
314 | * percpu_ref_kill() and percpu_ref_reinit(). | |
315 | */ | |
316 | struct mutex mq_freeze_lock; | |
0d2602ca JA |
317 | |
318 | struct blk_mq_tag_set *tag_set; | |
319 | struct list_head tag_set_list; | |
338aa96d | 320 | struct bio_set bio_split; |
4593fdbe | 321 | |
07e4fead | 322 | struct dentry *debugfs_dir; |
85e0cbbb LC |
323 | |
324 | #ifdef CONFIG_BLK_DEBUG_FS | |
d332ce09 | 325 | struct dentry *sched_debugfs_dir; |
cc56694f | 326 | struct dentry *rqos_debugfs_dir; |
07e4fead OS |
327 | #endif |
328 | ||
4593fdbe | 329 | bool mq_sysfs_init_done; |
6d247d7f | 330 | |
f793dfd3 JA |
331 | #define BLK_MAX_WRITE_HINTS 5 |
332 | u64 write_hints[BLK_MAX_WRITE_HINTS]; | |
1da177e4 LT |
333 | }; |
334 | ||
bfe373f6 | 335 | /* Keep blk_queue_flag_name[] in sync with the definitions below */ |
eca7abf3 JA |
336 | #define QUEUE_FLAG_STOPPED 0 /* queue is stopped */ |
337 | #define QUEUE_FLAG_DYING 1 /* queue being torn down */ | |
eca7abf3 JA |
338 | #define QUEUE_FLAG_NOMERGES 3 /* disable merge attempts */ |
339 | #define QUEUE_FLAG_SAME_COMP 4 /* complete on same CPU-group */ | |
340 | #define QUEUE_FLAG_FAIL_IO 5 /* fake timeout */ | |
341 | #define QUEUE_FLAG_NONROT 6 /* non-rotational device (SSD) */ | |
342 | #define QUEUE_FLAG_VIRT QUEUE_FLAG_NONROT /* paravirt device */ | |
343 | #define QUEUE_FLAG_IO_STAT 7 /* do disk/partitions IO accounting */ | |
344 | #define QUEUE_FLAG_DISCARD 8 /* supports DISCARD */ | |
345 | #define QUEUE_FLAG_NOXMERGES 9 /* No extended merges */ | |
346 | #define QUEUE_FLAG_ADD_RANDOM 10 /* Contributes to random pool */ | |
347 | #define QUEUE_FLAG_SECERASE 11 /* supports secure erase */ | |
348 | #define QUEUE_FLAG_SAME_FORCE 12 /* force complete on same CPU */ | |
349 | #define QUEUE_FLAG_DEAD 13 /* queue tear-down finished */ | |
350 | #define QUEUE_FLAG_INIT_DONE 14 /* queue is initialized */ | |
1cb039f3 | 351 | #define QUEUE_FLAG_STABLE_WRITES 15 /* don't modify blks until WB is done */ |
eca7abf3 JA |
352 | #define QUEUE_FLAG_POLL 16 /* IO polling enabled if set */ |
353 | #define QUEUE_FLAG_WC 17 /* Write back caching */ | |
354 | #define QUEUE_FLAG_FUA 18 /* device supports FUA writes */ | |
355 | #define QUEUE_FLAG_DAX 19 /* device supports DAX */ | |
356 | #define QUEUE_FLAG_STATS 20 /* track IO start and completion times */ | |
357 | #define QUEUE_FLAG_POLL_STATS 21 /* collecting stats for hybrid polling */ | |
358 | #define QUEUE_FLAG_REGISTERED 22 /* queue has been registered to a disk */ | |
359 | #define QUEUE_FLAG_SCSI_PASSTHROUGH 23 /* queue supports SCSI commands */ | |
360 | #define QUEUE_FLAG_QUIESCED 24 /* queue has been quiesced */ | |
361 | #define QUEUE_FLAG_PCI_P2PDMA 25 /* device supports PCI p2p requests */ | |
e84e8f06 | 362 | #define QUEUE_FLAG_ZONE_RESETALL 26 /* supports Zone Reset All */ |
6f816b4b | 363 | #define QUEUE_FLAG_RQ_ALLOC_TIME 27 /* record rq->alloc_time_ns */ |
021a2446 MS |
364 | #define QUEUE_FLAG_HCTX_ACTIVE 28 /* at least one blk-mq hctx is active */ |
365 | #define QUEUE_FLAG_NOWAIT 29 /* device supports NOWAIT */ | |
797e7dbb | 366 | |
94eddfbe | 367 | #define QUEUE_FLAG_MQ_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \ |
021a2446 MS |
368 | (1 << QUEUE_FLAG_SAME_COMP) | \ |
369 | (1 << QUEUE_FLAG_NOWAIT)) | |
94eddfbe | 370 | |
8814ce8a BVA |
371 | void blk_queue_flag_set(unsigned int flag, struct request_queue *q); |
372 | void blk_queue_flag_clear(unsigned int flag, struct request_queue *q); | |
373 | bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q); | |
8814ce8a | 374 | |
1da177e4 | 375 | #define blk_queue_stopped(q) test_bit(QUEUE_FLAG_STOPPED, &(q)->queue_flags) |
3f3299d5 | 376 | #define blk_queue_dying(q) test_bit(QUEUE_FLAG_DYING, &(q)->queue_flags) |
c246e80d | 377 | #define blk_queue_dead(q) test_bit(QUEUE_FLAG_DEAD, &(q)->queue_flags) |
320ae51f | 378 | #define blk_queue_init_done(q) test_bit(QUEUE_FLAG_INIT_DONE, &(q)->queue_flags) |
ac9fafa1 | 379 | #define blk_queue_nomerges(q) test_bit(QUEUE_FLAG_NOMERGES, &(q)->queue_flags) |
488991e2 AB |
380 | #define blk_queue_noxmerges(q) \ |
381 | test_bit(QUEUE_FLAG_NOXMERGES, &(q)->queue_flags) | |
a68bbddb | 382 | #define blk_queue_nonrot(q) test_bit(QUEUE_FLAG_NONROT, &(q)->queue_flags) |
1cb039f3 CH |
383 | #define blk_queue_stable_writes(q) \ |
384 | test_bit(QUEUE_FLAG_STABLE_WRITES, &(q)->queue_flags) | |
bc58ba94 | 385 | #define blk_queue_io_stat(q) test_bit(QUEUE_FLAG_IO_STAT, &(q)->queue_flags) |
e2e1a148 | 386 | #define blk_queue_add_random(q) test_bit(QUEUE_FLAG_ADD_RANDOM, &(q)->queue_flags) |
c15227de | 387 | #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags) |
e84e8f06 CK |
388 | #define blk_queue_zone_resetall(q) \ |
389 | test_bit(QUEUE_FLAG_ZONE_RESETALL, &(q)->queue_flags) | |
288dab8a CH |
390 | #define blk_queue_secure_erase(q) \ |
391 | (test_bit(QUEUE_FLAG_SECERASE, &(q)->queue_flags)) | |
163d4baa | 392 | #define blk_queue_dax(q) test_bit(QUEUE_FLAG_DAX, &(q)->queue_flags) |
9efc160f BVA |
393 | #define blk_queue_scsi_passthrough(q) \ |
394 | test_bit(QUEUE_FLAG_SCSI_PASSTHROUGH, &(q)->queue_flags) | |
49d92c0d LG |
395 | #define blk_queue_pci_p2pdma(q) \ |
396 | test_bit(QUEUE_FLAG_PCI_P2PDMA, &(q)->queue_flags) | |
6f816b4b TH |
397 | #ifdef CONFIG_BLK_RQ_ALLOC_TIME |
398 | #define blk_queue_rq_alloc_time(q) \ | |
399 | test_bit(QUEUE_FLAG_RQ_ALLOC_TIME, &(q)->queue_flags) | |
400 | #else | |
401 | #define blk_queue_rq_alloc_time(q) false | |
402 | #endif | |
1da177e4 | 403 | |
33659ebb CH |
404 | #define blk_noretry_request(rq) \ |
405 | ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \ | |
406 | REQ_FAILFAST_DRIVER)) | |
f4560ffe | 407 | #define blk_queue_quiesced(q) test_bit(QUEUE_FLAG_QUIESCED, &(q)->queue_flags) |
cd84a62e | 408 | #define blk_queue_pm_only(q) atomic_read(&(q)->pm_only) |
0ce91444 | 409 | #define blk_queue_fua(q) test_bit(QUEUE_FLAG_FUA, &(q)->queue_flags) |
58c898ba | 410 | #define blk_queue_registered(q) test_bit(QUEUE_FLAG_REGISTERED, &(q)->queue_flags) |
021a2446 | 411 | #define blk_queue_nowait(q) test_bit(QUEUE_FLAG_NOWAIT, &(q)->queue_flags) |
c9254f2d | 412 | |
cd84a62e BVA |
413 | extern void blk_set_pm_only(struct request_queue *q); |
414 | extern void blk_clear_pm_only(struct request_queue *q); | |
33659ebb | 415 | |
1da177e4 LT |
416 | #define list_entry_rq(ptr) list_entry((ptr), struct request, queuelist) |
417 | ||
3ab3a031 CH |
418 | #define dma_map_bvec(dev, bv, dir, attrs) \ |
419 | dma_map_page_attrs(dev, (bv)->bv_page, (bv)->bv_offset, (bv)->bv_len, \ | |
420 | (dir), (attrs)) | |
421 | ||
344e9ffc | 422 | static inline bool queue_is_mq(struct request_queue *q) |
49fd524f | 423 | { |
a1ce35fa | 424 | return q->mq_ops; |
49fd524f JA |
425 | } |
426 | ||
52abca64 AS |
427 | #ifdef CONFIG_PM |
428 | static inline enum rpm_status queue_rpm_status(struct request_queue *q) | |
429 | { | |
430 | return q->rpm_status; | |
431 | } | |
432 | #else | |
433 | static inline enum rpm_status queue_rpm_status(struct request_queue *q) | |
434 | { | |
435 | return RPM_ACTIVE; | |
436 | } | |
437 | #endif | |
438 | ||
797476b8 DLM |
439 | static inline enum blk_zoned_model |
440 | blk_queue_zoned_model(struct request_queue *q) | |
441 | { | |
6fcd6695 CH |
442 | if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) |
443 | return q->limits.zoned; | |
444 | return BLK_ZONED_NONE; | |
797476b8 DLM |
445 | } |
446 | ||
447 | static inline bool blk_queue_is_zoned(struct request_queue *q) | |
448 | { | |
449 | switch (blk_queue_zoned_model(q)) { | |
450 | case BLK_ZONED_HA: | |
451 | case BLK_ZONED_HM: | |
452 | return true; | |
453 | default: | |
454 | return false; | |
455 | } | |
456 | } | |
457 | ||
113ab72e | 458 | static inline sector_t blk_queue_zone_sectors(struct request_queue *q) |
6a0cb1bc HR |
459 | { |
460 | return blk_queue_is_zoned(q) ? q->limits.chunk_sectors : 0; | |
461 | } | |
462 | ||
6a5ac984 | 463 | #ifdef CONFIG_BLK_DEV_ZONED |
965b652e DLM |
464 | static inline unsigned int blk_queue_nr_zones(struct request_queue *q) |
465 | { | |
466 | return blk_queue_is_zoned(q) ? q->nr_zones : 0; | |
467 | } | |
468 | ||
6cc77e9c CH |
469 | static inline unsigned int blk_queue_zone_no(struct request_queue *q, |
470 | sector_t sector) | |
471 | { | |
472 | if (!blk_queue_is_zoned(q)) | |
473 | return 0; | |
474 | return sector >> ilog2(q->limits.chunk_sectors); | |
475 | } | |
476 | ||
477 | static inline bool blk_queue_zone_is_seq(struct request_queue *q, | |
478 | sector_t sector) | |
479 | { | |
f216fdd7 | 480 | if (!blk_queue_is_zoned(q)) |
6cc77e9c | 481 | return false; |
f216fdd7 CH |
482 | if (!q->conv_zones_bitmap) |
483 | return true; | |
484 | return !test_bit(blk_queue_zone_no(q, sector), q->conv_zones_bitmap); | |
6cc77e9c | 485 | } |
e15864f8 NC |
486 | |
487 | static inline void blk_queue_max_open_zones(struct request_queue *q, | |
488 | unsigned int max_open_zones) | |
489 | { | |
490 | q->max_open_zones = max_open_zones; | |
491 | } | |
492 | ||
493 | static inline unsigned int queue_max_open_zones(const struct request_queue *q) | |
494 | { | |
495 | return q->max_open_zones; | |
496 | } | |
659bf827 NC |
497 | |
498 | static inline void blk_queue_max_active_zones(struct request_queue *q, | |
499 | unsigned int max_active_zones) | |
500 | { | |
501 | q->max_active_zones = max_active_zones; | |
502 | } | |
503 | ||
504 | static inline unsigned int queue_max_active_zones(const struct request_queue *q) | |
505 | { | |
506 | return q->max_active_zones; | |
507 | } | |
965b652e DLM |
508 | #else /* CONFIG_BLK_DEV_ZONED */ |
509 | static inline unsigned int blk_queue_nr_zones(struct request_queue *q) | |
510 | { | |
511 | return 0; | |
512 | } | |
02992df8 JT |
513 | static inline bool blk_queue_zone_is_seq(struct request_queue *q, |
514 | sector_t sector) | |
515 | { | |
516 | return false; | |
517 | } | |
518 | static inline unsigned int blk_queue_zone_no(struct request_queue *q, | |
519 | sector_t sector) | |
520 | { | |
521 | return 0; | |
522 | } | |
e15864f8 NC |
523 | static inline unsigned int queue_max_open_zones(const struct request_queue *q) |
524 | { | |
525 | return 0; | |
526 | } | |
659bf827 NC |
527 | static inline unsigned int queue_max_active_zones(const struct request_queue *q) |
528 | { | |
529 | return 0; | |
530 | } | |
6a5ac984 | 531 | #endif /* CONFIG_BLK_DEV_ZONED */ |
6cc77e9c | 532 | |
d278d4a8 JA |
533 | static inline unsigned int blk_queue_depth(struct request_queue *q) |
534 | { | |
535 | if (q->queue_depth) | |
536 | return q->queue_depth; | |
537 | ||
538 | return q->nr_requests; | |
539 | } | |
540 | ||
3d6392cf JA |
541 | /* |
542 | * default timeout for SG_IO if none specified | |
543 | */ | |
544 | #define BLK_DEFAULT_SG_TIMEOUT (60 * HZ) | |
f2f1fa78 | 545 | #define BLK_MIN_SG_TIMEOUT (7 * HZ) |
3d6392cf | 546 | |
5705f702 | 547 | /* This should not be used directly - use rq_for_each_segment */ |
1e428079 JA |
548 | #define for_each_bio(_bio) \ |
549 | for (; _bio; _bio = _bio->bi_next) | |
5705f702 | 550 | |
2d4dc890 | 551 | |
1da177e4 LT |
552 | extern int blk_register_queue(struct gendisk *disk); |
553 | extern void blk_unregister_queue(struct gendisk *disk); | |
3e08773c | 554 | void submit_bio_noacct(struct bio *bio); |
24b83deb | 555 | |
ef9e3fac | 556 | extern int blk_lld_busy(struct request_queue *q); |
f695ca38 | 557 | extern void blk_queue_split(struct bio **); |
9a95e4ef | 558 | extern int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags); |
2e6edc95 | 559 | extern void blk_queue_exit(struct request_queue *q); |
1da177e4 | 560 | extern void blk_sync_queue(struct request_queue *q); |
fb9b16e1 | 561 | |
e47bc4ed CK |
562 | /* Helper to convert REQ_OP_XXX to its string format XXX */ |
563 | extern const char *blk_op_str(unsigned int op); | |
564 | ||
2a842aca CH |
565 | int blk_status_to_errno(blk_status_t status); |
566 | blk_status_t errno_to_blk_status(int errno); | |
567 | ||
ef99b2d3 CH |
568 | /* only poll the hardware once, don't continue until a completion was found */ |
569 | #define BLK_POLL_ONESHOT (1 << 0) | |
d729cf9a CH |
570 | /* do not sleep to wait for the expected completion time */ |
571 | #define BLK_POLL_NOSLEEP (1 << 1) | |
3e08773c CH |
572 | int bio_poll(struct bio *bio, unsigned int flags); |
573 | int iocb_bio_iopoll(struct kiocb *kiocb, unsigned int flags); | |
05229bee | 574 | |
165125e1 | 575 | static inline struct request_queue *bdev_get_queue(struct block_device *bdev) |
1da177e4 | 576 | { |
ff9ea323 | 577 | return bdev->bd_disk->queue; /* this is never NULL */ |
1da177e4 LT |
578 | } |
579 | ||
233bde21 BVA |
580 | /* |
581 | * The basic unit of block I/O is a sector. It is used in a number of contexts | |
582 | * in Linux (blk, bio, genhd). The size of one sector is 512 = 2**9 | |
583 | * bytes. Variables of type sector_t represent an offset or size that is a | |
584 | * multiple of 512 bytes. Hence these two constants. | |
585 | */ | |
586 | #ifndef SECTOR_SHIFT | |
587 | #define SECTOR_SHIFT 9 | |
588 | #endif | |
589 | #ifndef SECTOR_SIZE | |
590 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) | |
591 | #endif | |
592 | ||
018eca45 GJ |
593 | #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
594 | #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT) | |
595 | #define SECTOR_MASK (PAGE_SECTORS - 1) | |
596 | ||
6a5ac984 | 597 | #ifdef CONFIG_BLK_DEV_ZONED |
02694e86 CK |
598 | |
599 | /* Helper to convert BLK_ZONE_ZONE_XXX to its string format XXX */ | |
600 | const char *blk_zone_cond_str(enum blk_zone_cond zone_cond); | |
601 | ||
d0ea6bde DLM |
602 | static inline unsigned int bio_zone_no(struct bio *bio) |
603 | { | |
604 | return blk_queue_zone_no(bdev_get_queue(bio->bi_bdev), | |
605 | bio->bi_iter.bi_sector); | |
606 | } | |
607 | ||
608 | static inline unsigned int bio_zone_is_seq(struct bio *bio) | |
609 | { | |
610 | return blk_queue_zone_is_seq(bdev_get_queue(bio->bi_bdev), | |
611 | bio->bi_iter.bi_sector); | |
612 | } | |
6a5ac984 | 613 | #endif /* CONFIG_BLK_DEV_ZONED */ |
6cc77e9c | 614 | |
f31dc1cd | 615 | static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q, |
8fe0d473 | 616 | int op) |
f31dc1cd | 617 | { |
7afafc8a | 618 | if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)) |
233bde21 BVA |
619 | return min(q->limits.max_discard_sectors, |
620 | UINT_MAX >> SECTOR_SHIFT); | |
f31dc1cd | 621 | |
8fe0d473 | 622 | if (unlikely(op == REQ_OP_WRITE_SAME)) |
4363ac7c MP |
623 | return q->limits.max_write_same_sectors; |
624 | ||
a6f0788e CK |
625 | if (unlikely(op == REQ_OP_WRITE_ZEROES)) |
626 | return q->limits.max_write_zeroes_sectors; | |
627 | ||
f31dc1cd MP |
628 | return q->limits.max_sectors; |
629 | } | |
630 | ||
762380ad JA |
631 | /* |
632 | * Return maximum size of a request at given offset. Only valid for | |
633 | * file system requests. | |
634 | */ | |
635 | static inline unsigned int blk_max_size_offset(struct request_queue *q, | |
3ee16db3 MS |
636 | sector_t offset, |
637 | unsigned int chunk_sectors) | |
762380ad | 638 | { |
65f33b35 MS |
639 | if (!chunk_sectors) { |
640 | if (q->limits.chunk_sectors) | |
641 | chunk_sectors = q->limits.chunk_sectors; | |
642 | else | |
643 | return q->limits.max_sectors; | |
644 | } | |
762380ad | 645 | |
07d098e6 MS |
646 | if (likely(is_power_of_2(chunk_sectors))) |
647 | chunk_sectors -= offset & (chunk_sectors - 1); | |
648 | else | |
649 | chunk_sectors -= sector_div(offset, chunk_sectors); | |
650 | ||
651 | return min(q->limits.max_sectors, chunk_sectors); | |
762380ad JA |
652 | } |
653 | ||
1da177e4 LT |
654 | /* |
655 | * Access functions for manipulating queue properties | |
656 | */ | |
165125e1 | 657 | extern void blk_cleanup_queue(struct request_queue *); |
9bb33f24 | 658 | void blk_queue_bounce_limit(struct request_queue *q, enum blk_bounce limit); |
086fa5ff | 659 | extern void blk_queue_max_hw_sectors(struct request_queue *, unsigned int); |
762380ad | 660 | extern void blk_queue_chunk_sectors(struct request_queue *, unsigned int); |
8a78362c | 661 | extern void blk_queue_max_segments(struct request_queue *, unsigned short); |
1e739730 CH |
662 | extern void blk_queue_max_discard_segments(struct request_queue *, |
663 | unsigned short); | |
165125e1 | 664 | extern void blk_queue_max_segment_size(struct request_queue *, unsigned int); |
67efc925 CH |
665 | extern void blk_queue_max_discard_sectors(struct request_queue *q, |
666 | unsigned int max_discard_sectors); | |
4363ac7c MP |
667 | extern void blk_queue_max_write_same_sectors(struct request_queue *q, |
668 | unsigned int max_write_same_sectors); | |
a6f0788e CK |
669 | extern void blk_queue_max_write_zeroes_sectors(struct request_queue *q, |
670 | unsigned int max_write_same_sectors); | |
ad6bf88a | 671 | extern void blk_queue_logical_block_size(struct request_queue *, unsigned int); |
0512a75b KB |
672 | extern void blk_queue_max_zone_append_sectors(struct request_queue *q, |
673 | unsigned int max_zone_append_sectors); | |
892b6f90 | 674 | extern void blk_queue_physical_block_size(struct request_queue *, unsigned int); |
a805a4fa DLM |
675 | void blk_queue_zone_write_granularity(struct request_queue *q, |
676 | unsigned int size); | |
c72758f3 MP |
677 | extern void blk_queue_alignment_offset(struct request_queue *q, |
678 | unsigned int alignment); | |
471aa704 | 679 | void disk_update_readahead(struct gendisk *disk); |
7c958e32 | 680 | extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min); |
c72758f3 | 681 | extern void blk_queue_io_min(struct request_queue *q, unsigned int min); |
3c5820c7 | 682 | extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt); |
c72758f3 | 683 | extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt); |
d278d4a8 | 684 | extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth); |
e475bba2 | 685 | extern void blk_set_default_limits(struct queue_limits *lim); |
b1bd055d | 686 | extern void blk_set_stacking_limits(struct queue_limits *lim); |
c72758f3 MP |
687 | extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, |
688 | sector_t offset); | |
689 | extern void disk_stack_limits(struct gendisk *disk, struct block_device *bdev, | |
690 | sector_t offset); | |
27f8221a | 691 | extern void blk_queue_update_dma_pad(struct request_queue *, unsigned int); |
165125e1 | 692 | extern void blk_queue_segment_boundary(struct request_queue *, unsigned long); |
03100aad | 693 | extern void blk_queue_virt_boundary(struct request_queue *, unsigned long); |
165125e1 | 694 | extern void blk_queue_dma_alignment(struct request_queue *, int); |
11c3e689 | 695 | extern void blk_queue_update_dma_alignment(struct request_queue *, int); |
242f9dcb | 696 | extern void blk_queue_rq_timeout(struct request_queue *, unsigned int); |
93e9d8e8 | 697 | extern void blk_queue_write_cache(struct request_queue *q, bool enabled, bool fua); |
2e9bc346 CH |
698 | |
699 | /* | |
700 | * Elevator features for blk_queue_required_elevator_features: | |
701 | */ | |
702 | /* Supports zoned block devices sequential write constraint */ | |
703 | #define ELEVATOR_F_ZBD_SEQ_WRITE (1U << 0) | |
704 | /* Supports scheduling on multiple hardware queues */ | |
705 | #define ELEVATOR_F_MQ_AWARE (1U << 1) | |
706 | ||
68c43f13 DLM |
707 | extern void blk_queue_required_elevator_features(struct request_queue *q, |
708 | unsigned int features); | |
45147fb5 YS |
709 | extern bool blk_queue_can_use_dma_map_merging(struct request_queue *q, |
710 | struct device *dev); | |
1da177e4 | 711 | |
09ac46c4 | 712 | bool __must_check blk_get_queue(struct request_queue *); |
165125e1 | 713 | extern void blk_put_queue(struct request_queue *); |
3f21c265 | 714 | extern void blk_set_queue_dying(struct request_queue *); |
1da177e4 | 715 | |
1a4dcfa8 | 716 | #ifdef CONFIG_BLOCK |
316cc67d | 717 | /* |
75df7136 SJ |
718 | * blk_plug permits building a queue of related requests by holding the I/O |
719 | * fragments for a short period. This allows merging of sequential requests | |
720 | * into single larger request. As the requests are moved from a per-task list to | |
721 | * the device's request_queue in a batch, this results in improved scalability | |
722 | * as the lock contention for request_queue lock is reduced. | |
723 | * | |
724 | * It is ok not to disable preemption when adding the request to the plug list | |
725 | * or when attempting a merge, because blk_schedule_flush_list() will only flush | |
726 | * the plug list when the task sleeps by itself. For details, please see | |
727 | * schedule() where blk_schedule_flush_plug() is called. | |
316cc67d | 728 | */ |
73c10101 | 729 | struct blk_plug { |
320ae51f | 730 | struct list_head mq_list; /* blk-mq requests */ |
47c122e3 JA |
731 | |
732 | /* if ios_left is > 1, we can batch tag/rq allocations */ | |
733 | struct request *cached_rq; | |
734 | unsigned short nr_ios; | |
735 | ||
5f0ed774 | 736 | unsigned short rq_count; |
47c122e3 | 737 | |
ce5b009c | 738 | bool multiple_queues; |
5a473e83 | 739 | bool nowait; |
47c122e3 JA |
740 | |
741 | struct list_head cb_list; /* md requires an unplug callback */ | |
73c10101 | 742 | }; |
55c022bb | 743 | |
9cbb1750 | 744 | struct blk_plug_cb; |
74018dc3 | 745 | typedef void (*blk_plug_cb_fn)(struct blk_plug_cb *, bool); |
048c9374 N |
746 | struct blk_plug_cb { |
747 | struct list_head list; | |
9cbb1750 N |
748 | blk_plug_cb_fn callback; |
749 | void *data; | |
048c9374 | 750 | }; |
9cbb1750 N |
751 | extern struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, |
752 | void *data, int size); | |
73c10101 | 753 | extern void blk_start_plug(struct blk_plug *); |
47c122e3 | 754 | extern void blk_start_plug_nr_ios(struct blk_plug *, unsigned short); |
73c10101 | 755 | extern void blk_finish_plug(struct blk_plug *); |
f6603783 | 756 | extern void blk_flush_plug_list(struct blk_plug *, bool); |
73c10101 JA |
757 | |
758 | static inline void blk_flush_plug(struct task_struct *tsk) | |
759 | { | |
760 | struct blk_plug *plug = tsk->plug; | |
761 | ||
a237c1c5 JA |
762 | if (plug) |
763 | blk_flush_plug_list(plug, false); | |
764 | } | |
765 | ||
766 | static inline void blk_schedule_flush_plug(struct task_struct *tsk) | |
767 | { | |
768 | struct blk_plug *plug = tsk->plug; | |
769 | ||
88b996cd | 770 | if (plug) |
f6603783 | 771 | blk_flush_plug_list(plug, true); |
73c10101 JA |
772 | } |
773 | ||
774 | static inline bool blk_needs_flush_plug(struct task_struct *tsk) | |
775 | { | |
776 | struct blk_plug *plug = tsk->plug; | |
777 | ||
320ae51f | 778 | return plug && |
a1ce35fa | 779 | (!list_empty(&plug->mq_list) || |
320ae51f | 780 | !list_empty(&plug->cb_list)); |
73c10101 JA |
781 | } |
782 | ||
c6bf3f0e | 783 | int blkdev_issue_flush(struct block_device *bdev); |
1a4dcfa8 CH |
784 | long nr_blockdev_pages(void); |
785 | #else /* CONFIG_BLOCK */ | |
786 | struct blk_plug { | |
787 | }; | |
788 | ||
47c122e3 JA |
789 | static inline void blk_start_plug_nr_ios(struct blk_plug *plug, |
790 | unsigned short nr_ios) | |
791 | { | |
792 | } | |
793 | ||
1a4dcfa8 CH |
794 | static inline void blk_start_plug(struct blk_plug *plug) |
795 | { | |
796 | } | |
797 | ||
798 | static inline void blk_finish_plug(struct blk_plug *plug) | |
799 | { | |
800 | } | |
801 | ||
802 | static inline void blk_flush_plug(struct task_struct *task) | |
803 | { | |
804 | } | |
805 | ||
806 | static inline void blk_schedule_flush_plug(struct task_struct *task) | |
807 | { | |
808 | } | |
809 | ||
810 | ||
811 | static inline bool blk_needs_flush_plug(struct task_struct *tsk) | |
812 | { | |
813 | return false; | |
814 | } | |
815 | ||
c6bf3f0e | 816 | static inline int blkdev_issue_flush(struct block_device *bdev) |
1a4dcfa8 CH |
817 | { |
818 | return 0; | |
819 | } | |
820 | ||
821 | static inline long nr_blockdev_pages(void) | |
822 | { | |
823 | return 0; | |
824 | } | |
825 | #endif /* CONFIG_BLOCK */ | |
826 | ||
71ac860a ML |
827 | extern void blk_io_schedule(void); |
828 | ||
ee472d83 CH |
829 | extern int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, |
830 | sector_t nr_sects, gfp_t gfp_mask, struct page *page); | |
e950fdf7 CH |
831 | |
832 | #define BLKDEV_DISCARD_SECURE (1 << 0) /* issue a secure erase */ | |
dd3932ed | 833 | |
fbd9b09a DM |
834 | extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector, |
835 | sector_t nr_sects, gfp_t gfp_mask, unsigned long flags); | |
38f25255 | 836 | extern int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, |
288dab8a | 837 | sector_t nr_sects, gfp_t gfp_mask, int flags, |
469e3216 | 838 | struct bio **biop); |
ee472d83 CH |
839 | |
840 | #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */ | |
cb365b96 | 841 | #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */ |
ee472d83 | 842 | |
e73c23ff CK |
843 | extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, |
844 | sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, | |
ee472d83 | 845 | unsigned flags); |
3f14d792 | 846 | extern int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, |
ee472d83 CH |
847 | sector_t nr_sects, gfp_t gfp_mask, unsigned flags); |
848 | ||
2cf6d26a CH |
849 | static inline int sb_issue_discard(struct super_block *sb, sector_t block, |
850 | sector_t nr_blocks, gfp_t gfp_mask, unsigned long flags) | |
fb2dce86 | 851 | { |
233bde21 BVA |
852 | return blkdev_issue_discard(sb->s_bdev, |
853 | block << (sb->s_blocksize_bits - | |
854 | SECTOR_SHIFT), | |
855 | nr_blocks << (sb->s_blocksize_bits - | |
856 | SECTOR_SHIFT), | |
2cf6d26a | 857 | gfp_mask, flags); |
fb2dce86 | 858 | } |
e6fa0be6 | 859 | static inline int sb_issue_zeroout(struct super_block *sb, sector_t block, |
a107e5a3 | 860 | sector_t nr_blocks, gfp_t gfp_mask) |
e6fa0be6 LC |
861 | { |
862 | return blkdev_issue_zeroout(sb->s_bdev, | |
233bde21 BVA |
863 | block << (sb->s_blocksize_bits - |
864 | SECTOR_SHIFT), | |
865 | nr_blocks << (sb->s_blocksize_bits - | |
866 | SECTOR_SHIFT), | |
ee472d83 | 867 | gfp_mask, 0); |
e6fa0be6 | 868 | } |
1da177e4 | 869 | |
fa01b1e9 CH |
870 | static inline bool bdev_is_partition(struct block_device *bdev) |
871 | { | |
872 | return bdev->bd_partno; | |
873 | } | |
874 | ||
eb28d31b MP |
875 | enum blk_default_limits { |
876 | BLK_MAX_SEGMENTS = 128, | |
877 | BLK_SAFE_MAX_SECTORS = 255, | |
d2be537c | 878 | BLK_DEF_MAX_SECTORS = 2560, |
eb28d31b MP |
879 | BLK_MAX_SEGMENT_SIZE = 65536, |
880 | BLK_SEG_BOUNDARY_MASK = 0xFFFFFFFFUL, | |
881 | }; | |
0e435ac2 | 882 | |
af2c68fe | 883 | static inline unsigned long queue_segment_boundary(const struct request_queue *q) |
ae03bf63 | 884 | { |
025146e1 | 885 | return q->limits.seg_boundary_mask; |
ae03bf63 MP |
886 | } |
887 | ||
af2c68fe | 888 | static inline unsigned long queue_virt_boundary(const struct request_queue *q) |
03100aad KB |
889 | { |
890 | return q->limits.virt_boundary_mask; | |
891 | } | |
892 | ||
af2c68fe | 893 | static inline unsigned int queue_max_sectors(const struct request_queue *q) |
ae03bf63 | 894 | { |
025146e1 | 895 | return q->limits.max_sectors; |
ae03bf63 MP |
896 | } |
897 | ||
547e2f70 CH |
898 | static inline unsigned int queue_max_bytes(struct request_queue *q) |
899 | { | |
900 | return min_t(unsigned int, queue_max_sectors(q), INT_MAX >> 9) << 9; | |
901 | } | |
902 | ||
af2c68fe | 903 | static inline unsigned int queue_max_hw_sectors(const struct request_queue *q) |
ae03bf63 | 904 | { |
025146e1 | 905 | return q->limits.max_hw_sectors; |
ae03bf63 MP |
906 | } |
907 | ||
af2c68fe | 908 | static inline unsigned short queue_max_segments(const struct request_queue *q) |
ae03bf63 | 909 | { |
8a78362c | 910 | return q->limits.max_segments; |
ae03bf63 MP |
911 | } |
912 | ||
af2c68fe | 913 | static inline unsigned short queue_max_discard_segments(const struct request_queue *q) |
1e739730 CH |
914 | { |
915 | return q->limits.max_discard_segments; | |
916 | } | |
917 | ||
af2c68fe | 918 | static inline unsigned int queue_max_segment_size(const struct request_queue *q) |
ae03bf63 | 919 | { |
025146e1 | 920 | return q->limits.max_segment_size; |
ae03bf63 MP |
921 | } |
922 | ||
0512a75b KB |
923 | static inline unsigned int queue_max_zone_append_sectors(const struct request_queue *q) |
924 | { | |
fe6f0cdc JT |
925 | |
926 | const struct queue_limits *l = &q->limits; | |
927 | ||
928 | return min(l->max_zone_append_sectors, l->max_sectors); | |
0512a75b KB |
929 | } |
930 | ||
ad6bf88a | 931 | static inline unsigned queue_logical_block_size(const struct request_queue *q) |
1da177e4 LT |
932 | { |
933 | int retval = 512; | |
934 | ||
025146e1 MP |
935 | if (q && q->limits.logical_block_size) |
936 | retval = q->limits.logical_block_size; | |
1da177e4 LT |
937 | |
938 | return retval; | |
939 | } | |
940 | ||
ad6bf88a | 941 | static inline unsigned int bdev_logical_block_size(struct block_device *bdev) |
1da177e4 | 942 | { |
e1defc4f | 943 | return queue_logical_block_size(bdev_get_queue(bdev)); |
1da177e4 LT |
944 | } |
945 | ||
af2c68fe | 946 | static inline unsigned int queue_physical_block_size(const struct request_queue *q) |
c72758f3 MP |
947 | { |
948 | return q->limits.physical_block_size; | |
949 | } | |
950 | ||
892b6f90 | 951 | static inline unsigned int bdev_physical_block_size(struct block_device *bdev) |
ac481c20 MP |
952 | { |
953 | return queue_physical_block_size(bdev_get_queue(bdev)); | |
954 | } | |
955 | ||
af2c68fe | 956 | static inline unsigned int queue_io_min(const struct request_queue *q) |
c72758f3 MP |
957 | { |
958 | return q->limits.io_min; | |
959 | } | |
960 | ||
ac481c20 MP |
961 | static inline int bdev_io_min(struct block_device *bdev) |
962 | { | |
963 | return queue_io_min(bdev_get_queue(bdev)); | |
964 | } | |
965 | ||
af2c68fe | 966 | static inline unsigned int queue_io_opt(const struct request_queue *q) |
c72758f3 MP |
967 | { |
968 | return q->limits.io_opt; | |
969 | } | |
970 | ||
ac481c20 MP |
971 | static inline int bdev_io_opt(struct block_device *bdev) |
972 | { | |
973 | return queue_io_opt(bdev_get_queue(bdev)); | |
974 | } | |
975 | ||
a805a4fa DLM |
976 | static inline unsigned int |
977 | queue_zone_write_granularity(const struct request_queue *q) | |
978 | { | |
979 | return q->limits.zone_write_granularity; | |
980 | } | |
981 | ||
982 | static inline unsigned int | |
983 | bdev_zone_write_granularity(struct block_device *bdev) | |
984 | { | |
985 | return queue_zone_write_granularity(bdev_get_queue(bdev)); | |
986 | } | |
987 | ||
af2c68fe | 988 | static inline int queue_alignment_offset(const struct request_queue *q) |
c72758f3 | 989 | { |
ac481c20 | 990 | if (q->limits.misaligned) |
c72758f3 MP |
991 | return -1; |
992 | ||
ac481c20 | 993 | return q->limits.alignment_offset; |
c72758f3 MP |
994 | } |
995 | ||
e03a72e1 | 996 | static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t sector) |
81744ee4 MP |
997 | { |
998 | unsigned int granularity = max(lim->physical_block_size, lim->io_min); | |
233bde21 BVA |
999 | unsigned int alignment = sector_div(sector, granularity >> SECTOR_SHIFT) |
1000 | << SECTOR_SHIFT; | |
81744ee4 | 1001 | |
b8839b8c | 1002 | return (granularity + lim->alignment_offset - alignment) % granularity; |
c72758f3 MP |
1003 | } |
1004 | ||
ac481c20 MP |
1005 | static inline int bdev_alignment_offset(struct block_device *bdev) |
1006 | { | |
1007 | struct request_queue *q = bdev_get_queue(bdev); | |
1008 | ||
1009 | if (q->limits.misaligned) | |
1010 | return -1; | |
fa01b1e9 | 1011 | if (bdev_is_partition(bdev)) |
7b8917f5 | 1012 | return queue_limit_alignment_offset(&q->limits, |
29ff57c6 | 1013 | bdev->bd_start_sect); |
ac481c20 MP |
1014 | return q->limits.alignment_offset; |
1015 | } | |
1016 | ||
af2c68fe | 1017 | static inline int queue_discard_alignment(const struct request_queue *q) |
86b37281 MP |
1018 | { |
1019 | if (q->limits.discard_misaligned) | |
1020 | return -1; | |
1021 | ||
1022 | return q->limits.discard_alignment; | |
1023 | } | |
1024 | ||
e03a72e1 | 1025 | static inline int queue_limit_discard_alignment(struct queue_limits *lim, sector_t sector) |
86b37281 | 1026 | { |
59771079 | 1027 | unsigned int alignment, granularity, offset; |
dd3d145d | 1028 | |
a934a00a MP |
1029 | if (!lim->max_discard_sectors) |
1030 | return 0; | |
1031 | ||
59771079 | 1032 | /* Why are these in bytes, not sectors? */ |
233bde21 BVA |
1033 | alignment = lim->discard_alignment >> SECTOR_SHIFT; |
1034 | granularity = lim->discard_granularity >> SECTOR_SHIFT; | |
59771079 LT |
1035 | if (!granularity) |
1036 | return 0; | |
1037 | ||
1038 | /* Offset of the partition start in 'granularity' sectors */ | |
1039 | offset = sector_div(sector, granularity); | |
1040 | ||
1041 | /* And why do we do this modulus *again* in blkdev_issue_discard()? */ | |
1042 | offset = (granularity + alignment - offset) % granularity; | |
1043 | ||
1044 | /* Turn it back into bytes, gaah */ | |
233bde21 | 1045 | return offset << SECTOR_SHIFT; |
86b37281 MP |
1046 | } |
1047 | ||
c6e66634 PB |
1048 | static inline int bdev_discard_alignment(struct block_device *bdev) |
1049 | { | |
1050 | struct request_queue *q = bdev_get_queue(bdev); | |
1051 | ||
fa01b1e9 | 1052 | if (bdev_is_partition(bdev)) |
7cf34d97 | 1053 | return queue_limit_discard_alignment(&q->limits, |
29ff57c6 | 1054 | bdev->bd_start_sect); |
c6e66634 PB |
1055 | return q->limits.discard_alignment; |
1056 | } | |
1057 | ||
4363ac7c MP |
1058 | static inline unsigned int bdev_write_same(struct block_device *bdev) |
1059 | { | |
1060 | struct request_queue *q = bdev_get_queue(bdev); | |
1061 | ||
1062 | if (q) | |
1063 | return q->limits.max_write_same_sectors; | |
1064 | ||
1065 | return 0; | |
1066 | } | |
1067 | ||
a6f0788e CK |
1068 | static inline unsigned int bdev_write_zeroes_sectors(struct block_device *bdev) |
1069 | { | |
1070 | struct request_queue *q = bdev_get_queue(bdev); | |
1071 | ||
1072 | if (q) | |
1073 | return q->limits.max_write_zeroes_sectors; | |
1074 | ||
1075 | return 0; | |
1076 | } | |
1077 | ||
797476b8 DLM |
1078 | static inline enum blk_zoned_model bdev_zoned_model(struct block_device *bdev) |
1079 | { | |
1080 | struct request_queue *q = bdev_get_queue(bdev); | |
1081 | ||
1082 | if (q) | |
1083 | return blk_queue_zoned_model(q); | |
1084 | ||
1085 | return BLK_ZONED_NONE; | |
1086 | } | |
1087 | ||
1088 | static inline bool bdev_is_zoned(struct block_device *bdev) | |
1089 | { | |
1090 | struct request_queue *q = bdev_get_queue(bdev); | |
1091 | ||
1092 | if (q) | |
1093 | return blk_queue_is_zoned(q); | |
1094 | ||
1095 | return false; | |
1096 | } | |
1097 | ||
113ab72e | 1098 | static inline sector_t bdev_zone_sectors(struct block_device *bdev) |
6a0cb1bc HR |
1099 | { |
1100 | struct request_queue *q = bdev_get_queue(bdev); | |
1101 | ||
1102 | if (q) | |
f99e8648 | 1103 | return blk_queue_zone_sectors(q); |
6cc77e9c CH |
1104 | return 0; |
1105 | } | |
6a0cb1bc | 1106 | |
e15864f8 NC |
1107 | static inline unsigned int bdev_max_open_zones(struct block_device *bdev) |
1108 | { | |
1109 | struct request_queue *q = bdev_get_queue(bdev); | |
1110 | ||
1111 | if (q) | |
1112 | return queue_max_open_zones(q); | |
1113 | return 0; | |
1114 | } | |
1115 | ||
659bf827 NC |
1116 | static inline unsigned int bdev_max_active_zones(struct block_device *bdev) |
1117 | { | |
1118 | struct request_queue *q = bdev_get_queue(bdev); | |
1119 | ||
1120 | if (q) | |
1121 | return queue_max_active_zones(q); | |
1122 | return 0; | |
1123 | } | |
1124 | ||
af2c68fe | 1125 | static inline int queue_dma_alignment(const struct request_queue *q) |
1da177e4 | 1126 | { |
482eb689 | 1127 | return q ? q->dma_alignment : 511; |
1da177e4 LT |
1128 | } |
1129 | ||
14417799 | 1130 | static inline int blk_rq_aligned(struct request_queue *q, unsigned long addr, |
87904074 FT |
1131 | unsigned int len) |
1132 | { | |
1133 | unsigned int alignment = queue_dma_alignment(q) | q->dma_pad_mask; | |
14417799 | 1134 | return !(addr & alignment) && !(len & alignment); |
87904074 FT |
1135 | } |
1136 | ||
1da177e4 LT |
1137 | /* assumes size > 256 */ |
1138 | static inline unsigned int blksize_bits(unsigned int size) | |
1139 | { | |
1140 | unsigned int bits = 8; | |
1141 | do { | |
1142 | bits++; | |
1143 | size >>= 1; | |
1144 | } while (size > 256); | |
1145 | return bits; | |
1146 | } | |
1147 | ||
2befb9e3 | 1148 | static inline unsigned int block_size(struct block_device *bdev) |
1da177e4 | 1149 | { |
6b7b181b | 1150 | return 1 << bdev->bd_inode->i_blkbits; |
1da177e4 LT |
1151 | } |
1152 | ||
59c3d45e | 1153 | int kblockd_schedule_work(struct work_struct *work); |
818cd1cb | 1154 | int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork, unsigned long delay); |
1da177e4 | 1155 | |
1da177e4 LT |
1156 | #define MODULE_ALIAS_BLOCKDEV(major,minor) \ |
1157 | MODULE_ALIAS("block-major-" __stringify(major) "-" __stringify(minor)) | |
1158 | #define MODULE_ALIAS_BLOCKDEV_MAJOR(major) \ | |
1159 | MODULE_ALIAS("block-major-" __stringify(major) "-*") | |
1160 | ||
d145dc23 ST |
1161 | #ifdef CONFIG_BLK_INLINE_ENCRYPTION |
1162 | ||
1163 | bool blk_ksm_register(struct blk_keyslot_manager *ksm, struct request_queue *q); | |
1164 | ||
1165 | void blk_ksm_unregister(struct request_queue *q); | |
1166 | ||
1167 | #else /* CONFIG_BLK_INLINE_ENCRYPTION */ | |
1168 | ||
1169 | static inline bool blk_ksm_register(struct blk_keyslot_manager *ksm, | |
1170 | struct request_queue *q) | |
1171 | { | |
1172 | return true; | |
1173 | } | |
1174 | ||
1175 | static inline void blk_ksm_unregister(struct request_queue *q) { } | |
1176 | ||
1177 | #endif /* CONFIG_BLK_INLINE_ENCRYPTION */ | |
1178 | ||
1179 | ||
08f85851 | 1180 | struct block_device_operations { |
3e08773c | 1181 | void (*submit_bio)(struct bio *bio); |
d4430d62 | 1182 | int (*open) (struct block_device *, fmode_t); |
db2a144b | 1183 | void (*release) (struct gendisk *, fmode_t); |
3f289dcb | 1184 | int (*rw_page)(struct block_device *, sector_t, struct page *, unsigned int); |
d4430d62 AV |
1185 | int (*ioctl) (struct block_device *, fmode_t, unsigned, unsigned long); |
1186 | int (*compat_ioctl) (struct block_device *, fmode_t, unsigned, unsigned long); | |
77ea887e TH |
1187 | unsigned int (*check_events) (struct gendisk *disk, |
1188 | unsigned int clearing); | |
c3e33e04 | 1189 | void (*unlock_native_capacity) (struct gendisk *); |
08f85851 | 1190 | int (*getgeo)(struct block_device *, struct hd_geometry *); |
e00adcad | 1191 | int (*set_read_only)(struct block_device *bdev, bool ro); |
b3a27d05 NG |
1192 | /* this callback is with swap_lock and sometimes page table lock held */ |
1193 | void (*swap_slot_free_notify) (struct block_device *, unsigned long); | |
e76239a3 | 1194 | int (*report_zones)(struct gendisk *, sector_t sector, |
d4100351 | 1195 | unsigned int nr_zones, report_zones_cb cb, void *data); |
348e114b | 1196 | char *(*devnode)(struct gendisk *disk, umode_t *mode); |
08f85851 | 1197 | struct module *owner; |
bbd3e064 | 1198 | const struct pr_ops *pr_ops; |
0bdfbca8 DO |
1199 | |
1200 | /* | |
1201 | * Special callback for probing GPT entry at a given sector. | |
1202 | * Needed by Android devices, used by GPT scanner and MMC blk | |
1203 | * driver. | |
1204 | */ | |
1205 | int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector); | |
08f85851 AV |
1206 | }; |
1207 | ||
ee6a129d AB |
1208 | #ifdef CONFIG_COMPAT |
1209 | extern int blkdev_compat_ptr_ioctl(struct block_device *, fmode_t, | |
1210 | unsigned int, unsigned long); | |
1211 | #else | |
1212 | #define blkdev_compat_ptr_ioctl NULL | |
1213 | #endif | |
1214 | ||
47a191fd MW |
1215 | extern int bdev_read_page(struct block_device *, sector_t, struct page *); |
1216 | extern int bdev_write_page(struct block_device *, sector_t, struct page *, | |
1217 | struct writeback_control *); | |
6cc77e9c | 1218 | |
0619317f JA |
1219 | static inline void blk_wake_io_task(struct task_struct *waiter) |
1220 | { | |
1221 | /* | |
1222 | * If we're polling, the task itself is doing the completions. For | |
1223 | * that case, we don't need to signal a wakeup, it's enough to just | |
1224 | * mark us as RUNNING. | |
1225 | */ | |
1226 | if (waiter == current) | |
1227 | __set_current_state(TASK_RUNNING); | |
1228 | else | |
1229 | wake_up_process(waiter); | |
1230 | } | |
1231 | ||
956d510e CH |
1232 | unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors, |
1233 | unsigned int op); | |
1234 | void disk_end_io_acct(struct gendisk *disk, unsigned int op, | |
1235 | unsigned long start_time); | |
1236 | ||
99dfc43e CH |
1237 | unsigned long bio_start_io_acct(struct bio *bio); |
1238 | void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time, | |
1239 | struct block_device *orig_bdev); | |
956d510e CH |
1240 | |
1241 | /** | |
1242 | * bio_end_io_acct - end I/O accounting for bio based drivers | |
1243 | * @bio: bio to end account for | |
1244 | * @start: start time returned by bio_start_io_acct() | |
1245 | */ | |
1246 | static inline void bio_end_io_acct(struct bio *bio, unsigned long start_time) | |
1247 | { | |
99dfc43e | 1248 | return bio_end_io_acct_remapped(bio, start_time, bio->bi_bdev); |
956d510e | 1249 | } |
956d510e | 1250 | |
3f1266f1 CH |
1251 | int bdev_read_only(struct block_device *bdev); |
1252 | int set_blocksize(struct block_device *bdev, int size); | |
1253 | ||
1254 | const char *bdevname(struct block_device *bdev, char *buffer); | |
4e7b5671 | 1255 | int lookup_bdev(const char *pathname, dev_t *dev); |
3f1266f1 CH |
1256 | |
1257 | void blkdev_show(struct seq_file *seqf, off_t offset); | |
1258 | ||
1259 | #define BDEVNAME_SIZE 32 /* Largest string for a blockdev identifier */ | |
1260 | #define BDEVT_SIZE 10 /* Largest string for MAJ:MIN for blkdev */ | |
1261 | #ifdef CONFIG_BLOCK | |
1262 | #define BLKDEV_MAJOR_MAX 512 | |
1263 | #else | |
1264 | #define BLKDEV_MAJOR_MAX 0 | |
1265 | #endif | |
1266 | ||
3f1266f1 CH |
1267 | struct block_device *blkdev_get_by_path(const char *path, fmode_t mode, |
1268 | void *holder); | |
1269 | struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder); | |
37c3fc9a CH |
1270 | int bd_prepare_to_claim(struct block_device *bdev, void *holder); |
1271 | void bd_abort_claiming(struct block_device *bdev, void *holder); | |
3f1266f1 CH |
1272 | void blkdev_put(struct block_device *bdev, fmode_t mode); |
1273 | ||
22ae8ce8 CH |
1274 | /* just for blk-cgroup, don't use elsewhere */ |
1275 | struct block_device *blkdev_get_no_open(dev_t dev); | |
1276 | void blkdev_put_no_open(struct block_device *bdev); | |
1277 | ||
1278 | struct block_device *bdev_alloc(struct gendisk *disk, u8 partno); | |
1279 | void bdev_add(struct block_device *bdev, dev_t dev); | |
621c1f42 | 1280 | struct block_device *I_BDEV(struct inode *inode); |
2c2b9fd6 CH |
1281 | int truncate_bdev_range(struct block_device *bdev, fmode_t mode, loff_t lstart, |
1282 | loff_t lend); | |
3f1266f1 CH |
1283 | |
1284 | #ifdef CONFIG_BLOCK | |
1285 | void invalidate_bdev(struct block_device *bdev); | |
1286 | int sync_blockdev(struct block_device *bdev); | |
1287 | #else | |
1288 | static inline void invalidate_bdev(struct block_device *bdev) | |
1289 | { | |
1290 | } | |
1291 | static inline int sync_blockdev(struct block_device *bdev) | |
1292 | { | |
1293 | return 0; | |
1294 | } | |
1da177e4 | 1295 | #endif |
3f1266f1 CH |
1296 | int fsync_bdev(struct block_device *bdev); |
1297 | ||
040f04bd CH |
1298 | int freeze_bdev(struct block_device *bdev); |
1299 | int thaw_bdev(struct block_device *bdev); | |
3f1266f1 CH |
1300 | |
1301 | #endif /* _LINUX_BLKDEV_H */ |