1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_ELEVATOR_H
3 #define _LINUX_ELEVATOR_H
5 #include <linux/percpu.h>
6 #include <linux/hashtable.h>
12 #ifdef CONFIG_BLK_DEBUG_FS
13 struct blk_mq_debugfs_attr
;
17 * Return values from elevator merger
20 ELEVATOR_NO_MERGE
= 0,
21 ELEVATOR_FRONT_MERGE
= 1,
22 ELEVATOR_BACK_MERGE
= 2,
23 ELEVATOR_DISCARD_MERGE
= 3,
26 typedef enum elv_merge (elevator_merge_fn
) (struct request_queue
*, struct request
**,
29 typedef void (elevator_merge_req_fn
) (struct request_queue
*, struct request
*, struct request
*);
31 typedef void (elevator_merged_fn
) (struct request_queue
*, struct request
*, enum elv_merge
);
33 typedef int (elevator_allow_bio_merge_fn
) (struct request_queue
*,
34 struct request
*, struct bio
*);
36 typedef int (elevator_allow_rq_merge_fn
) (struct request_queue
*,
37 struct request
*, struct request
*);
39 typedef void (elevator_bio_merged_fn
) (struct request_queue
*,
40 struct request
*, struct bio
*);
42 typedef int (elevator_dispatch_fn
) (struct request_queue
*, int);
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);
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
*,
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
*);
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
*);
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
;
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
;
76 elevator_completed_req_fn
*elevator_completed_req_fn
;
78 elevator_request_list_fn
*elevator_former_req_fn
;
79 elevator_request_list_fn
*elevator_latter_req_fn
;
81 elevator_init_icq_fn
*elevator_init_icq_fn
; /* see iocontext.h */
82 elevator_exit_icq_fn
*elevator_exit_icq_fn
; /* ditto */
84 elevator_set_req_fn
*elevator_set_req_fn
;
85 elevator_put_req_fn
*elevator_put_req_fn
;
87 elevator_may_queue_fn
*elevator_may_queue_fn
;
89 elevator_init_fn
*elevator_init_fn
;
90 elevator_exit_fn
*elevator_exit_fn
;
91 elevator_registered_fn
*elevator_registered_fn
;
94 struct blk_mq_alloc_data
;
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);
103 bool (*allow_merge
)(struct request_queue
*, struct request
*, struct bio
*);
104 bool (*bio_merge
)(struct blk_mq_hw_ctx
*, struct bio
*);
105 int (*request_merge
)(struct request_queue
*q
, struct request
**, struct bio
*);
106 void (*request_merged
)(struct request_queue
*, struct request
*, enum elv_merge
);
107 void (*requests_merged
)(struct request_queue
*, struct request
*, struct request
*);
108 void (*limit_depth
)(unsigned int, struct blk_mq_alloc_data
*);
109 void (*prepare_request
)(struct request
*, struct bio
*bio
);
110 void (*finish_request
)(struct request
*);
111 void (*insert_requests
)(struct blk_mq_hw_ctx
*, struct list_head
*, bool);
112 struct request
*(*dispatch_request
)(struct blk_mq_hw_ctx
*);
113 bool (*has_work
)(struct blk_mq_hw_ctx
*);
114 void (*completed_request
)(struct request
*);
115 void (*started_request
)(struct request
*);
116 void (*requeue_request
)(struct request
*);
117 struct request
*(*former_request
)(struct request_queue
*, struct request
*);
118 struct request
*(*next_request
)(struct request_queue
*, struct request
*);
119 void (*init_icq
)(struct io_cq
*);
120 void (*exit_icq
)(struct io_cq
*);
123 #define ELV_NAME_MAX (16)
125 struct elv_fs_entry
{
126 struct attribute attr
;
127 ssize_t (*show
)(struct elevator_queue
*, char *);
128 ssize_t (*store
)(struct elevator_queue
*, const char *, size_t);
132 * identifies an elevator type, such as AS or deadline
136 /* managed by elevator core */
137 struct kmem_cache
*icq_cache
;
139 /* fields provided by elevator implementation */
141 struct elevator_ops sq
;
142 struct elevator_mq_ops mq
;
144 size_t icq_size
; /* see iocontext.h */
145 size_t icq_align
; /* ditto */
146 struct elv_fs_entry
*elevator_attrs
;
147 char elevator_name
[ELV_NAME_MAX
];
148 const char *elevator_alias
;
149 struct module
*elevator_owner
;
151 #ifdef CONFIG_BLK_DEBUG_FS
152 const struct blk_mq_debugfs_attr
*queue_debugfs_attrs
;
153 const struct blk_mq_debugfs_attr
*hctx_debugfs_attrs
;
156 /* managed by elevator core */
157 char icq_cache_name
[ELV_NAME_MAX
+ 6]; /* elvname + "_io_cq" */
158 struct list_head list
;
161 #define ELV_HASH_BITS 6
163 void elv_rqhash_del(struct request_queue
*q
, struct request
*rq
);
164 void elv_rqhash_add(struct request_queue
*q
, struct request
*rq
);
165 void elv_rqhash_reposition(struct request_queue
*q
, struct request
*rq
);
166 struct request
*elv_rqhash_find(struct request_queue
*q
, sector_t offset
);
169 * each queue has an elevator_queue associated with it
171 struct elevator_queue
173 struct elevator_type
*type
;
176 struct mutex sysfs_lock
;
177 unsigned int registered
:1;
178 unsigned int uses_mq
:1;
179 DECLARE_HASHTABLE(hash
, ELV_HASH_BITS
);
183 * block elevator interface
185 extern void elv_dispatch_sort(struct request_queue
*, struct request
*);
186 extern void elv_dispatch_add_tail(struct request_queue
*, struct request
*);
187 extern void elv_add_request(struct request_queue
*, struct request
*, int);
188 extern void __elv_add_request(struct request_queue
*, struct request
*, int);
189 extern enum elv_merge
elv_merge(struct request_queue
*, struct request
**,
191 extern void elv_merge_requests(struct request_queue
*, struct request
*,
193 extern void elv_merged_request(struct request_queue
*, struct request
*,
195 extern void elv_bio_merged(struct request_queue
*q
, struct request
*,
197 extern bool elv_attempt_insert_merge(struct request_queue
*, struct request
*);
198 extern void elv_requeue_request(struct request_queue
*, struct request
*);
199 extern struct request
*elv_former_request(struct request_queue
*, struct request
*);
200 extern struct request
*elv_latter_request(struct request_queue
*, struct request
*);
201 extern int elv_may_queue(struct request_queue
*, unsigned int);
202 extern void elv_completed_request(struct request_queue
*, struct request
*);
203 extern int elv_set_request(struct request_queue
*q
, struct request
*rq
,
204 struct bio
*bio
, gfp_t gfp_mask
);
205 extern void elv_put_request(struct request_queue
*, struct request
*);
206 extern void elv_drain_elevator(struct request_queue
*);
209 * io scheduler registration
211 extern void __init
load_default_elevator_module(void);
212 extern int elv_register(struct elevator_type
*);
213 extern void elv_unregister(struct elevator_type
*);
216 * io scheduler sysfs switching
218 extern ssize_t
elv_iosched_show(struct request_queue
*, char *);
219 extern ssize_t
elv_iosched_store(struct request_queue
*, const char *, size_t);
221 extern int elevator_init(struct request_queue
*, char *);
222 extern void elevator_exit(struct request_queue
*, struct elevator_queue
*);
223 extern bool elv_bio_merge_ok(struct request
*, struct bio
*);
224 extern struct elevator_queue
*elevator_alloc(struct request_queue
*,
225 struct elevator_type
*);
230 extern struct request
*elv_rb_former_request(struct request_queue
*, struct request
*);
231 extern struct request
*elv_rb_latter_request(struct request_queue
*, struct request
*);
234 * rb support functions.
236 extern void elv_rb_add(struct rb_root
*, struct request
*);
237 extern void elv_rb_del(struct rb_root
*, struct request
*);
238 extern struct request
*elv_rb_find(struct rb_root
*, sector_t
);
241 * Insertion selection
243 #define ELEVATOR_INSERT_FRONT 1
244 #define ELEVATOR_INSERT_BACK 2
245 #define ELEVATOR_INSERT_SORT 3
246 #define ELEVATOR_INSERT_REQUEUE 4
247 #define ELEVATOR_INSERT_FLUSH 5
248 #define ELEVATOR_INSERT_SORT_MERGE 6
251 * return values from elevator_may_queue_fn
259 #define rq_end_sector(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
260 #define rb_entry_rq(node) rb_entry((node), struct request, rb_node)
262 #define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
263 #define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist)
265 #else /* CONFIG_BLOCK */
267 static inline void load_default_elevator_module(void) { }
269 #endif /* CONFIG_BLOCK */