1 #ifndef _LINUX_ELEVATOR_H
2 #define _LINUX_ELEVATOR_H
4 #include <linux/percpu.h>
5 #include <linux/hashtable.h>
11 #ifdef CONFIG_BLK_DEBUG_FS
12 struct blk_mq_debugfs_attr;
16 * Return values from elevator merger
19 ELEVATOR_NO_MERGE = 0,
20 ELEVATOR_FRONT_MERGE = 1,
21 ELEVATOR_BACK_MERGE = 2,
22 ELEVATOR_DISCARD_MERGE = 3,
25 typedef enum elv_merge (elevator_merge_fn) (struct request_queue *, struct request **,
28 typedef void (elevator_merge_req_fn) (struct request_queue *, struct request *, struct request *);
30 typedef void (elevator_merged_fn) (struct request_queue *, struct request *, enum elv_merge);
32 typedef int (elevator_allow_bio_merge_fn) (struct request_queue *,
33 struct request *, struct bio *);
35 typedef int (elevator_allow_rq_merge_fn) (struct request_queue *,
36 struct request *, struct request *);
38 typedef void (elevator_bio_merged_fn) (struct request_queue *,
39 struct request *, struct bio *);
41 typedef int (elevator_dispatch_fn) (struct request_queue *, int);
43 typedef void (elevator_add_req_fn) (struct request_queue *, struct request *);
44 typedef struct request *(elevator_request_list_fn) (struct request_queue *, struct request *);
45 typedef void (elevator_completed_req_fn) (struct request_queue *, struct request *);
46 typedef int (elevator_may_queue_fn) (struct request_queue *, unsigned int);
48 typedef void (elevator_init_icq_fn) (struct io_cq *);
49 typedef void (elevator_exit_icq_fn) (struct io_cq *);
50 typedef int (elevator_set_req_fn) (struct request_queue *, struct request *,
52 typedef void (elevator_put_req_fn) (struct request *);
53 typedef void (elevator_activate_req_fn) (struct request_queue *, struct request *);
54 typedef void (elevator_deactivate_req_fn) (struct request_queue *, struct request *);
56 typedef int (elevator_init_fn) (struct request_queue *,
57 struct elevator_type *e);
58 typedef void (elevator_exit_fn) (struct elevator_queue *);
59 typedef void (elevator_registered_fn) (struct request_queue *);
63 elevator_merge_fn *elevator_merge_fn;
64 elevator_merged_fn *elevator_merged_fn;
65 elevator_merge_req_fn *elevator_merge_req_fn;
66 elevator_allow_bio_merge_fn *elevator_allow_bio_merge_fn;
67 elevator_allow_rq_merge_fn *elevator_allow_rq_merge_fn;
68 elevator_bio_merged_fn *elevator_bio_merged_fn;
70 elevator_dispatch_fn *elevator_dispatch_fn;
71 elevator_add_req_fn *elevator_add_req_fn;
72 elevator_activate_req_fn *elevator_activate_req_fn;
73 elevator_deactivate_req_fn *elevator_deactivate_req_fn;
75 elevator_completed_req_fn *elevator_completed_req_fn;
77 elevator_request_list_fn *elevator_former_req_fn;
78 elevator_request_list_fn *elevator_latter_req_fn;
80 elevator_init_icq_fn *elevator_init_icq_fn; /* see iocontext.h */
81 elevator_exit_icq_fn *elevator_exit_icq_fn; /* ditto */
83 elevator_set_req_fn *elevator_set_req_fn;
84 elevator_put_req_fn *elevator_put_req_fn;
86 elevator_may_queue_fn *elevator_may_queue_fn;
88 elevator_init_fn *elevator_init_fn;
89 elevator_exit_fn *elevator_exit_fn;
90 elevator_registered_fn *elevator_registered_fn;
93 struct blk_mq_alloc_data;
96 struct elevator_mq_ops {
97 int (*init_sched)(struct request_queue *, struct elevator_type *);
98 void (*exit_sched)(struct elevator_queue *);
99 int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int);
100 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
102 bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
103 bool (*bio_merge)(struct blk_mq_hw_ctx *, struct bio *);
104 int (*request_merge)(struct request_queue *q, struct request **, struct bio *);
105 void (*request_merged)(struct request_queue *, struct request *, enum elv_merge);
106 void (*requests_merged)(struct request_queue *, struct request *, struct request *);
107 struct request *(*get_request)(struct request_queue *, unsigned int, struct blk_mq_alloc_data *);
108 void (*put_request)(struct request *);
109 void (*insert_requests)(struct blk_mq_hw_ctx *, struct list_head *, bool);
110 struct request *(*dispatch_request)(struct blk_mq_hw_ctx *);
111 bool (*has_work)(struct blk_mq_hw_ctx *);
112 void (*completed_request)(struct request *);
113 void (*started_request)(struct request *);
114 void (*requeue_request)(struct request *);
115 struct request *(*former_request)(struct request_queue *, struct request *);
116 struct request *(*next_request)(struct request_queue *, struct request *);
117 int (*get_rq_priv)(struct request_queue *, struct request *, struct bio *);
118 void (*put_rq_priv)(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 struct module *elevator_owner;
150 #ifdef CONFIG_BLK_DEBUG_FS
151 const struct blk_mq_debugfs_attr *queue_debugfs_attrs;
152 const struct blk_mq_debugfs_attr *hctx_debugfs_attrs;
155 /* managed by elevator core */
156 char icq_cache_name[ELV_NAME_MAX + 5]; /* elvname + "_io_cq" */
157 struct list_head list;
160 #define ELV_HASH_BITS 6
162 void elv_rqhash_del(struct request_queue *q, struct request *rq);
163 void elv_rqhash_add(struct request_queue *q, struct request *rq);
164 void elv_rqhash_reposition(struct request_queue *q, struct request *rq);
165 struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
168 * each queue has an elevator_queue associated with it
170 struct elevator_queue
172 struct elevator_type *type;
175 struct mutex sysfs_lock;
176 unsigned int registered:1;
177 unsigned int uses_mq:1;
178 DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
182 * block elevator interface
184 extern void elv_dispatch_sort(struct request_queue *, struct request *);
185 extern void elv_dispatch_add_tail(struct request_queue *, struct request *);
186 extern void elv_add_request(struct request_queue *, struct request *, int);
187 extern void __elv_add_request(struct request_queue *, struct request *, int);
188 extern enum elv_merge elv_merge(struct request_queue *, struct request **,
190 extern void elv_merge_requests(struct request_queue *, struct request *,
192 extern void elv_merged_request(struct request_queue *, struct request *,
194 extern void elv_bio_merged(struct request_queue *q, struct request *,
196 extern bool elv_attempt_insert_merge(struct request_queue *, struct request *);
197 extern void elv_requeue_request(struct request_queue *, struct request *);
198 extern struct request *elv_former_request(struct request_queue *, struct request *);
199 extern struct request *elv_latter_request(struct request_queue *, struct request *);
200 extern int elv_register_queue(struct request_queue *q);
201 extern void elv_unregister_queue(struct request_queue *q);
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 *);
210 * io scheduler registration
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 *);
217 * io scheduler sysfs switching
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);
222 extern int elevator_init(struct request_queue *, char *);
223 extern void elevator_exit(struct request_queue *, struct elevator_queue *);
224 extern bool elv_bio_merge_ok(struct request *, struct bio *);
225 extern struct elevator_queue *elevator_alloc(struct request_queue *,
226 struct elevator_type *);
231 extern struct request *elv_rb_former_request(struct request_queue *, struct request *);
232 extern struct request *elv_rb_latter_request(struct request_queue *, struct request *);
235 * rb support functions.
237 extern void elv_rb_add(struct rb_root *, struct request *);
238 extern void elv_rb_del(struct rb_root *, struct request *);
239 extern struct request *elv_rb_find(struct rb_root *, sector_t);
242 * Insertion selection
244 #define ELEVATOR_INSERT_FRONT 1
245 #define ELEVATOR_INSERT_BACK 2
246 #define ELEVATOR_INSERT_SORT 3
247 #define ELEVATOR_INSERT_REQUEUE 4
248 #define ELEVATOR_INSERT_FLUSH 5
249 #define ELEVATOR_INSERT_SORT_MERGE 6
252 * return values from elevator_may_queue_fn
260 #define rq_end_sector(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
261 #define rb_entry_rq(node) rb_entry((node), struct request, rb_node)
263 #define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
264 #define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist)
266 #else /* CONFIG_BLOCK */
268 static inline void load_default_elevator_module(void) { }
270 #endif /* CONFIG_BLOCK */