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