1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_ELEVATOR_H
3 #define _LINUX_ELEVATOR_H
4
5 #include <linux/percpu.h>
6 #include <linux/hashtable.h>
7
8 #ifdef CONFIG_BLOCK
9
10 struct io_cq;
11 struct elevator_type;
12 #ifdef CONFIG_BLK_DEBUG_FS
13 struct blk_mq_debugfs_attr;
14 #endif
15
16 /*
17 * Return values from elevator merger
18 */
19 enum elv_merge {
20 ELEVATOR_NO_MERGE = 0,
21 ELEVATOR_FRONT_MERGE = 1,
22 ELEVATOR_BACK_MERGE = 2,
23 ELEVATOR_DISCARD_MERGE = 3,
24 };
25
26 typedef enum elv_merge (elevator_merge_fn) (struct request_queue *, struct request **,
27 struct bio *);
28
29 typedef void (elevator_merge_req_fn) (struct request_queue *, struct request *, struct request *);
30
31 typedef void (elevator_merged_fn) (struct request_queue *, struct request *, enum elv_merge);
32
33 typedef int (elevator_allow_bio_merge_fn) (struct request_queue *,
34 struct request *, struct bio *);
35
36 typedef int (elevator_allow_rq_merge_fn) (struct request_queue *,
37 struct request *, struct request *);
38
39 typedef void (elevator_bio_merged_fn) (struct request_queue *,
40 struct request *, struct bio *);
41
42 typedef int (elevator_dispatch_fn) (struct request_queue *, int);
43
44 typedef void (elevator_add_req_fn) (struct request_queue *, struct request *);
45 typedef struct request *(elevator_request_list_fn) (struct request_queue *, struct request *);
46 typedef void (elevator_completed_req_fn) (struct request_queue *, struct request *);
47 typedef int (elevator_may_queue_fn) (struct request_queue *, unsigned int);
48
49 typedef void (elevator_init_icq_fn) (struct io_cq *);
50 typedef void (elevator_exit_icq_fn) (struct io_cq *);
51 typedef int (elevator_set_req_fn) (struct request_queue *, struct request *,
52 struct bio *, gfp_t);
53 typedef void (elevator_put_req_fn) (struct request *);
54 typedef void (elevator_activate_req_fn) (struct request_queue *, struct request *);
55 typedef void (elevator_deactivate_req_fn) (struct request_queue *, struct request *);
56
57 typedef int (elevator_init_fn) (struct request_queue *,
58 struct elevator_type *e);
59 typedef void (elevator_exit_fn) (struct elevator_queue *);
60 typedef void (elevator_registered_fn) (struct request_queue *);
61
62 struct elevator_ops
63 {
64 elevator_merge_fn *elevator_merge_fn;
65 elevator_merged_fn *elevator_merged_fn;
66 elevator_merge_req_fn *elevator_merge_req_fn;
67 elevator_allow_bio_merge_fn *elevator_allow_bio_merge_fn;
68 elevator_allow_rq_merge_fn *elevator_allow_rq_merge_fn;
69 elevator_bio_merged_fn *elevator_bio_merged_fn;
70
71 elevator_dispatch_fn *elevator_dispatch_fn;
72 elevator_add_req_fn *elevator_add_req_fn;
73 elevator_activate_req_fn *elevator_activate_req_fn;
74 elevator_deactivate_req_fn *elevator_deactivate_req_fn;
75
76 elevator_completed_req_fn *elevator_completed_req_fn;
77
78 elevator_request_list_fn *elevator_former_req_fn;
79 elevator_request_list_fn *elevator_latter_req_fn;
80
81 elevator_init_icq_fn *elevator_init_icq_fn; /* see iocontext.h */
82 elevator_exit_icq_fn *elevator_exit_icq_fn; /* ditto */
83
84 elevator_set_req_fn *elevator_set_req_fn;
85 elevator_put_req_fn *elevator_put_req_fn;
86
87 elevator_may_queue_fn *elevator_may_queue_fn;
88
89 elevator_init_fn *elevator_init_fn;
90 elevator_exit_fn *elevator_exit_fn;
91 elevator_registered_fn *elevator_registered_fn;
92 };
93
94 struct blk_mq_alloc_data;
95 struct blk_mq_hw_ctx;
96
97 struct elevator_mq_ops {
98 int (*init_sched)(struct request_queue *, struct elevator_type *);
99 void (*exit_sched)(struct elevator_queue *);
100 int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int);
101 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
102 void (*depth_updated)(struct blk_mq_hw_ctx *);
103
104 bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
105 bool (*bio_merge)(struct blk_mq_hw_ctx *, struct bio *);
106 int (*request_merge)(struct request_queue *q, struct request **, struct bio *);
107 void (*request_merged)(struct request_queue *, struct request *, enum elv_merge);
108 void (*requests_merged)(struct request_queue *, struct request *, struct request *);
109 void (*limit_depth)(unsigned int, struct blk_mq_alloc_data *);
110 void (*prepare_request)(struct request *, struct bio *bio);
111 void (*finish_request)(struct request *);
112 void (*insert_requests)(struct blk_mq_hw_ctx *, struct list_head *, bool);
113 struct request *(*dispatch_request)(struct blk_mq_hw_ctx *);
114 bool (*has_work)(struct blk_mq_hw_ctx *);
115 void (*completed_request)(struct request *);
116 void (*started_request)(struct request *);
117 void (*requeue_request)(struct request *);
118 struct request *(*former_request)(struct request_queue *, struct request *);
119 struct request *(*next_request)(struct request_queue *, struct request *);
120 void (*init_icq)(struct io_cq *);
121 void (*exit_icq)(struct io_cq *);
122 };
123
124 #define ELV_NAME_MAX (16)
125
126 struct elv_fs_entry {
127 struct attribute attr;
128 ssize_t (*show)(struct elevator_queue *, char *);
129 ssize_t (*store)(struct elevator_queue *, const char *, size_t);
130 };
131
132 /*
133 * identifies an elevator type, such as AS or deadline
134 */
135 struct elevator_type
136 {
137 /* managed by elevator core */
138 struct kmem_cache *icq_cache;
139
140 /* fields provided by elevator implementation */
141 union {
142 struct elevator_ops sq;
143 struct elevator_mq_ops mq;
144 } ops;
145 size_t icq_size; /* see iocontext.h */
146 size_t icq_align; /* ditto */
147 struct elv_fs_entry *elevator_attrs;
148 char elevator_name[ELV_NAME_MAX];
149 const char *elevator_alias;
150 struct module *elevator_owner;
151 bool uses_mq;
152 #ifdef CONFIG_BLK_DEBUG_FS
153 const struct blk_mq_debugfs_attr *queue_debugfs_attrs;
154 const struct blk_mq_debugfs_attr *hctx_debugfs_attrs;
155 #endif
156
157 /* managed by elevator core */
158 char icq_cache_name[ELV_NAME_MAX + 6]; /* elvname + "_io_cq" */
159 struct list_head list;
160 };
161
162 #define ELV_HASH_BITS 6
163
164 void elv_rqhash_del(struct request_queue *q, struct request *rq);
165 void elv_rqhash_add(struct request_queue *q, struct request *rq);
166 void elv_rqhash_reposition(struct request_queue *q, struct request *rq);
167 struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
168
169 /*
170 * each queue has an elevator_queue associated with it
171 */
172 struct elevator_queue
173 {
174 struct elevator_type *type;
175 void *elevator_data;
176 struct kobject kobj;
177 struct mutex sysfs_lock;
178 unsigned int registered:1;
179 unsigned int uses_mq:1;
180 DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
181 };
182
183 /*
184 * block elevator interface
185 */
186 extern void elv_dispatch_sort(struct request_queue *, struct request *);
187 extern void elv_dispatch_add_tail(struct request_queue *, struct request *);
188 extern void elv_add_request(struct request_queue *, struct request *, int);
189 extern void __elv_add_request(struct request_queue *, struct request *, int);
190 extern enum elv_merge elv_merge(struct request_queue *, struct request **,
191 struct bio *);
192 extern void elv_merge_requests(struct request_queue *, struct request *,
193 struct request *);
194 extern void elv_merged_request(struct request_queue *, struct request *,
195 enum elv_merge);
196 extern void elv_bio_merged(struct request_queue *q, struct request *,
197 struct bio *);
198 extern bool elv_attempt_insert_merge(struct request_queue *, struct request *);
199 extern void elv_requeue_request(struct request_queue *, struct request *);
200 extern struct request *elv_former_request(struct request_queue *, struct request *);
201 extern struct request *elv_latter_request(struct request_queue *, struct request *);
202 extern int elv_may_queue(struct request_queue *, unsigned int);
203 extern void elv_completed_request(struct request_queue *, struct request *);
204 extern int elv_set_request(struct request_queue *q, struct request *rq,
205 struct bio *bio, gfp_t gfp_mask);
206 extern void elv_put_request(struct request_queue *, struct request *);
207 extern void elv_drain_elevator(struct request_queue *);
208
209 /*
210 * io scheduler registration
211 */
212 extern void __init load_default_elevator_module(void);
213 extern int elv_register(struct elevator_type *);
214 extern void elv_unregister(struct elevator_type *);
215
216 /*
217 * io scheduler sysfs switching
218 */
219 extern ssize_t elv_iosched_show(struct request_queue *, char *);
220 extern ssize_t elv_iosched_store(struct request_queue *, const char *, size_t);
221
222 extern bool elv_bio_merge_ok(struct request *, struct bio *);
223 extern struct elevator_queue *elevator_alloc(struct request_queue *,
224 struct elevator_type *);
225
226 /*
227 * Helper functions.
228 */
229 extern struct request *elv_rb_former_request(struct request_queue *, struct request *);
230 extern struct request *elv_rb_latter_request(struct request_queue *, struct request *);
231
232 /*
233 * rb support functions.
234 */
235 extern void elv_rb_add(struct rb_root *, struct request *);
236 extern void elv_rb_del(struct rb_root *, struct request *);
237 extern struct request *elv_rb_find(struct rb_root *, sector_t);
238
239 /*
240 * Insertion selection
241 */
242 #define ELEVATOR_INSERT_FRONT 1
243 #define ELEVATOR_INSERT_BACK 2
244 #define ELEVATOR_INSERT_SORT 3
245 #define ELEVATOR_INSERT_REQUEUE 4
246 #define ELEVATOR_INSERT_FLUSH 5
247 #define ELEVATOR_INSERT_SORT_MERGE 6
248
249 /*
250 * return values from elevator_may_queue_fn
251 */
252 enum {
253 ELV_MQUEUE_MAY,
254 ELV_MQUEUE_NO,
255 ELV_MQUEUE_MUST,
256 };
257
258 #define rq_end_sector(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
259 #define rb_entry_rq(node) rb_entry((node), struct request, rb_node)
260
261 #define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
262 #define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist)
263
264 #else /* CONFIG_BLOCK */
265
load_default_elevator_module(void)266 static inline void load_default_elevator_module(void) { }
267
268 #endif /* CONFIG_BLOCK */
269 #endif
270