xref: /linux/block/elevator.h (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ELEVATOR_H
3 #define _ELEVATOR_H
4 
5 #include <linux/percpu.h>
6 #include <linux/hashtable.h>
7 
8 struct io_cq;
9 struct elevator_type;
10 struct blk_mq_debugfs_attr;
11 
12 /*
13  * Return values from elevator merger
14  */
15 enum elv_merge {
16 	ELEVATOR_NO_MERGE	= 0,
17 	ELEVATOR_FRONT_MERGE	= 1,
18 	ELEVATOR_BACK_MERGE	= 2,
19 	ELEVATOR_DISCARD_MERGE	= 3,
20 };
21 
22 struct blk_mq_alloc_data;
23 struct blk_mq_hw_ctx;
24 
25 struct elevator_mq_ops {
26 	int (*init_sched)(struct request_queue *, struct elevator_type *);
27 	void (*exit_sched)(struct elevator_queue *);
28 	int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int);
29 	void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
30 	void (*depth_updated)(struct blk_mq_hw_ctx *);
31 
32 	bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
33 	bool (*bio_merge)(struct request_queue *, struct bio *, unsigned int);
34 	int (*request_merge)(struct request_queue *q, struct request **, struct bio *);
35 	void (*request_merged)(struct request_queue *, struct request *, enum elv_merge);
36 	void (*requests_merged)(struct request_queue *, struct request *, struct request *);
37 	void (*limit_depth)(blk_opf_t, struct blk_mq_alloc_data *);
38 	void (*prepare_request)(struct request *);
39 	void (*finish_request)(struct request *);
40 	void (*insert_requests)(struct blk_mq_hw_ctx *, struct list_head *, bool);
41 	struct request *(*dispatch_request)(struct blk_mq_hw_ctx *);
42 	bool (*has_work)(struct blk_mq_hw_ctx *);
43 	void (*completed_request)(struct request *, u64);
44 	void (*requeue_request)(struct request *);
45 	struct request *(*former_request)(struct request_queue *, struct request *);
46 	struct request *(*next_request)(struct request_queue *, struct request *);
47 	void (*init_icq)(struct io_cq *);
48 	void (*exit_icq)(struct io_cq *);
49 };
50 
51 #define ELV_NAME_MAX	(16)
52 
53 struct elv_fs_entry {
54 	struct attribute attr;
55 	ssize_t (*show)(struct elevator_queue *, char *);
56 	ssize_t (*store)(struct elevator_queue *, const char *, size_t);
57 };
58 
59 /*
60  * identifies an elevator type, such as AS or deadline
61  */
62 struct elevator_type
63 {
64 	/* managed by elevator core */
65 	struct kmem_cache *icq_cache;
66 
67 	/* fields provided by elevator implementation */
68 	struct elevator_mq_ops ops;
69 
70 	size_t icq_size;	/* see iocontext.h */
71 	size_t icq_align;	/* ditto */
72 	struct elv_fs_entry *elevator_attrs;
73 	const char *elevator_name;
74 	const char *elevator_alias;
75 	const unsigned int elevator_features;
76 	struct module *elevator_owner;
77 #ifdef CONFIG_BLK_DEBUG_FS
78 	const struct blk_mq_debugfs_attr *queue_debugfs_attrs;
79 	const struct blk_mq_debugfs_attr *hctx_debugfs_attrs;
80 #endif
81 
82 	/* managed by elevator core */
83 	char icq_cache_name[ELV_NAME_MAX + 6];	/* elvname + "_io_cq" */
84 	struct list_head list;
85 };
86 
87 #define ELV_HASH_BITS 6
88 
89 void elv_rqhash_del(struct request_queue *q, struct request *rq);
90 void elv_rqhash_add(struct request_queue *q, struct request *rq);
91 void elv_rqhash_reposition(struct request_queue *q, struct request *rq);
92 struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
93 
94 /*
95  * each queue has an elevator_queue associated with it
96  */
97 struct elevator_queue
98 {
99 	struct elevator_type *type;
100 	void *elevator_data;
101 	struct kobject kobj;
102 	struct mutex sysfs_lock;
103 	unsigned int registered:1;
104 	DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
105 };
106 
107 /*
108  * block elevator interface
109  */
110 extern enum elv_merge elv_merge(struct request_queue *, struct request **,
111 		struct bio *);
112 extern void elv_merge_requests(struct request_queue *, struct request *,
113 			       struct request *);
114 extern void elv_merged_request(struct request_queue *, struct request *,
115 		enum elv_merge);
116 extern bool elv_attempt_insert_merge(struct request_queue *, struct request *,
117 				     struct list_head *);
118 extern struct request *elv_former_request(struct request_queue *, struct request *);
119 extern struct request *elv_latter_request(struct request_queue *, struct request *);
120 void elevator_init_mq(struct request_queue *q);
121 
122 /*
123  * io scheduler registration
124  */
125 extern int elv_register(struct elevator_type *);
126 extern void elv_unregister(struct elevator_type *);
127 
128 /*
129  * io scheduler sysfs switching
130  */
131 extern ssize_t elv_iosched_show(struct request_queue *, char *);
132 extern ssize_t elv_iosched_store(struct request_queue *, const char *, size_t);
133 
134 extern bool elv_bio_merge_ok(struct request *, struct bio *);
135 extern struct elevator_queue *elevator_alloc(struct request_queue *,
136 					struct elevator_type *);
137 
138 /*
139  * Helper functions.
140  */
141 extern struct request *elv_rb_former_request(struct request_queue *, struct request *);
142 extern struct request *elv_rb_latter_request(struct request_queue *, struct request *);
143 
144 /*
145  * rb support functions.
146  */
147 extern void elv_rb_add(struct rb_root *, struct request *);
148 extern void elv_rb_del(struct rb_root *, struct request *);
149 extern struct request *elv_rb_find(struct rb_root *, sector_t);
150 
151 /*
152  * Insertion selection
153  */
154 #define ELEVATOR_INSERT_FRONT	1
155 #define ELEVATOR_INSERT_BACK	2
156 #define ELEVATOR_INSERT_SORT	3
157 #define ELEVATOR_INSERT_REQUEUE	4
158 #define ELEVATOR_INSERT_FLUSH	5
159 #define ELEVATOR_INSERT_SORT_MERGE	6
160 
161 #define rb_entry_rq(node)	rb_entry((node), struct request, rb_node)
162 
163 #define rq_entry_fifo(ptr)	list_entry((ptr), struct request, queuelist)
164 #define rq_fifo_clear(rq)	list_del_init(&(rq)->queuelist)
165 
166 #endif /* _ELEVATOR_H */
167