blob: 17cd0078377cae91dc86d63541b1bd6b87141b0a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_ELEVATOR_H
3#define _LINUX_ELEVATOR_H
4
Jens Axboe4a893e82006-07-22 15:37:43 +02005#include <linux/percpu.h>
Sasha Levin242d98f2012-12-17 10:01:27 -05006#include <linux/hashtable.h>
Jens Axboe4a893e82006-07-22 15:37:43 +02007
David Howells93614012006-09-30 20:45:40 +02008#ifdef CONFIG_BLOCK
9
Tejun Heo7e5a8792011-12-14 00:33:42 +010010struct io_cq;
Jianpeng Mad50235b2013-07-03 13:25:24 +020011struct elevator_type;
Omar Sandovald332ce02017-05-04 08:24:40 -060012#ifdef CONFIG_BLK_DEBUG_FS
13struct blk_mq_debugfs_attr;
14#endif
Tejun Heo7e5a8792011-12-14 00:33:42 +010015
Christoph Hellwig34fe7c02017-02-08 14:46:48 +010016/*
17 * Return values from elevator merger
18 */
19enum elv_merge {
20 ELEVATOR_NO_MERGE = 0,
21 ELEVATOR_FRONT_MERGE = 1,
22 ELEVATOR_BACK_MERGE = 2,
Christoph Hellwig1e739732017-02-08 14:46:49 +010023 ELEVATOR_DISCARD_MERGE = 3,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +010024};
25
Jens Axboebd166ef2017-01-17 06:03:22 -070026struct blk_mq_alloc_data;
27struct blk_mq_hw_ctx;
28
29struct elevator_mq_ops {
30 int (*init_sched)(struct request_queue *, struct elevator_type *);
31 void (*exit_sched)(struct elevator_queue *);
Omar Sandovalee056f92017-04-05 12:01:34 -070032 int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int);
33 void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
Jens Axboe77f1e0a2019-01-18 10:34:16 -070034 void (*depth_updated)(struct blk_mq_hw_ctx *);
Jens Axboebd166ef2017-01-17 06:03:22 -070035
36 bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
Christoph Hellwig14ccb662019-06-06 12:29:01 +020037 bool (*bio_merge)(struct blk_mq_hw_ctx *, struct bio *, unsigned int);
Jens Axboebd166ef2017-01-17 06:03:22 -070038 int (*request_merge)(struct request_queue *q, struct request **, struct bio *);
Christoph Hellwig34fe7c02017-02-08 14:46:48 +010039 void (*request_merged)(struct request_queue *, struct request *, enum elv_merge);
Jens Axboebd166ef2017-01-17 06:03:22 -070040 void (*requests_merged)(struct request_queue *, struct request *, struct request *);
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +020041 void (*limit_depth)(unsigned int, struct blk_mq_alloc_data *);
42 void (*prepare_request)(struct request *, struct bio *bio);
Christoph Hellwig7b9e9362017-06-16 18:15:21 +020043 void (*finish_request)(struct request *);
Jens Axboebd166ef2017-01-17 06:03:22 -070044 void (*insert_requests)(struct blk_mq_hw_ctx *, struct list_head *, bool);
Jens Axboec13660a2017-01-26 12:40:07 -070045 struct request *(*dispatch_request)(struct blk_mq_hw_ctx *);
Jens Axboebd166ef2017-01-17 06:03:22 -070046 bool (*has_work)(struct blk_mq_hw_ctx *);
Omar Sandovaled886602018-09-27 15:55:51 -070047 void (*completed_request)(struct request *, u64);
Jens Axboebd166ef2017-01-17 06:03:22 -070048 void (*started_request)(struct request *);
49 void (*requeue_request)(struct request *);
50 struct request *(*former_request)(struct request_queue *, struct request *);
51 struct request *(*next_request)(struct request_queue *, struct request *);
Jens Axboebd166ef2017-01-17 06:03:22 -070052 void (*init_icq)(struct io_cq *);
53 void (*exit_icq)(struct io_cq *);
54};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define ELV_NAME_MAX (16)
57
Al Viroe572ec72006-03-18 22:27:18 -050058struct elv_fs_entry {
59 struct attribute attr;
Jens Axboeb374d182008-10-31 10:05:07 +010060 ssize_t (*show)(struct elevator_queue *, char *);
61 ssize_t (*store)(struct elevator_queue *, const char *, size_t);
Al Viroe572ec72006-03-18 22:27:18 -050062};
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * identifies an elevator type, such as AS or deadline
66 */
67struct elevator_type
68{
Tejun Heo3d3c2372011-12-14 00:33:42 +010069 /* managed by elevator core */
70 struct kmem_cache *icq_cache;
71
72 /* fields provided by elevator implementation */
Jens Axboef9cd4bf2018-11-01 16:41:41 -060073 struct elevator_mq_ops ops;
Jens Axboea1ce35f2018-10-29 10:23:51 -060074
Tejun Heof1f8cc92011-12-14 00:33:42 +010075 size_t icq_size; /* see iocontext.h */
76 size_t icq_align; /* ditto */
Al Viroe572ec72006-03-18 22:27:18 -050077 struct elv_fs_entry *elevator_attrs;
Damien Le Moal9305d5d2019-07-11 00:57:41 +090078 const char *elevator_name;
Jens Axboe8ac0d9a2017-10-25 12:35:02 -060079 const char *elevator_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct module *elevator_owner;
Omar Sandovald332ce02017-05-04 08:24:40 -060081#ifdef CONFIG_BLK_DEBUG_FS
82 const struct blk_mq_debugfs_attr *queue_debugfs_attrs;
83 const struct blk_mq_debugfs_attr *hctx_debugfs_attrs;
84#endif
Tejun Heo3d3c2372011-12-14 00:33:42 +010085
86 /* managed by elevator core */
Eric Biggers9bd2bbc2017-06-02 20:35:51 -070087 char icq_cache_name[ELV_NAME_MAX + 6]; /* elvname + "_io_cq" */
Tejun Heo22f746e2011-12-14 00:33:41 +010088 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Sasha Levin242d98f2012-12-17 10:01:27 -050091#define ELV_HASH_BITS 6
92
Jens Axboe70b3ea02016-12-07 08:43:31 -070093void elv_rqhash_del(struct request_queue *q, struct request *rq);
94void elv_rqhash_add(struct request_queue *q, struct request *rq);
95void elv_rqhash_reposition(struct request_queue *q, struct request *rq);
96struct request *elv_rqhash_find(struct request_queue *q, sector_t offset);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Adrian Bunk54069582006-01-10 00:09:36 +010099 * each queue has an elevator_queue associated with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
101struct elevator_queue
102{
Tejun Heo22f746e2011-12-14 00:33:41 +0100103 struct elevator_type *type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 void *elevator_data;
105 struct kobject kobj;
Al Viro3d1ab402006-03-18 18:35:43 -0500106 struct mutex sysfs_lock;
Jens Axboe430c62f2010-10-07 09:35:16 +0200107 unsigned int registered:1;
Sasha Levin242d98f2012-12-17 10:01:27 -0500108 DECLARE_HASHTABLE(hash, ELV_HASH_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111/*
112 * block elevator interface
113 */
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100114extern enum elv_merge elv_merge(struct request_queue *, struct request **,
115 struct bio *);
Jens Axboe165125e2007-07-24 09:28:11 +0200116extern void elv_merge_requests(struct request_queue *, struct request *,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct request *);
Christoph Hellwig34fe7c02017-02-08 14:46:48 +0100118extern void elv_merged_request(struct request_queue *, struct request *,
119 enum elv_merge);
Jens Axboebd166ef2017-01-17 06:03:22 -0700120extern bool elv_attempt_insert_merge(struct request_queue *, struct request *);
Jens Axboe165125e2007-07-24 09:28:11 +0200121extern struct request *elv_former_request(struct request_queue *, struct request *);
122extern struct request *elv_latter_request(struct request_queue *, struct request *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/*
125 * io scheduler registration
126 */
Jens Axboee567bf72014-06-22 16:32:48 -0600127extern int elv_register(struct elevator_type *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128extern void elv_unregister(struct elevator_type *);
129
130/*
131 * io scheduler sysfs switching
132 */
Jens Axboe165125e2007-07-24 09:28:11 +0200133extern ssize_t elv_iosched_show(struct request_queue *, char *);
134extern ssize_t elv_iosched_store(struct request_queue *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Tahsin Erdogan72ef7992016-07-07 11:48:22 -0700136extern bool elv_bio_merge_ok(struct request *, struct bio *);
Jianpeng Mad50235b2013-07-03 13:25:24 +0200137extern struct elevator_queue *elevator_alloc(struct request_queue *,
138 struct elevator_type *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/*
Jens Axboe2e662b62006-07-13 11:55:04 +0200141 * Helper functions.
142 */
Jens Axboe165125e2007-07-24 09:28:11 +0200143extern struct request *elv_rb_former_request(struct request_queue *, struct request *);
144extern struct request *elv_rb_latter_request(struct request_queue *, struct request *);
Jens Axboe2e662b62006-07-13 11:55:04 +0200145
146/*
147 * rb support functions.
148 */
Jeff Moyer796d5112011-06-02 21:19:05 +0200149extern void elv_rb_add(struct rb_root *, struct request *);
Jens Axboe2e662b62006-07-13 11:55:04 +0200150extern void elv_rb_del(struct rb_root *, struct request *);
151extern struct request *elv_rb_find(struct rb_root *, sector_t);
152
153/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Insertion selection
155 */
156#define ELEVATOR_INSERT_FRONT 1
157#define ELEVATOR_INSERT_BACK 2
158#define ELEVATOR_INSERT_SORT 3
Tejun Heo797e7db2006-01-06 09:51:03 +0100159#define ELEVATOR_INSERT_REQUEUE 4
Tejun Heoae1b1532011-01-25 12:43:54 +0100160#define ELEVATOR_INSERT_FLUSH 5
Jens Axboe5e84ea32011-03-21 10:14:27 +0100161#define ELEVATOR_INSERT_SORT_MERGE 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900163#define rq_end_sector(rq) (blk_rq_pos(rq) + blk_rq_sectors(rq))
Jens Axboe2e662b62006-07-13 11:55:04 +0200164#define rb_entry_rq(node) rb_entry((node), struct request, rb_node)
Jens Axboe1b47f532005-10-20 16:37:00 +0200165
Jens Axboe1fbfdfc2006-07-11 21:49:15 +0200166#define rq_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
Frederic Weisbeckerd9a74df2014-02-24 16:39:53 +0100167#define rq_fifo_clear(rq) list_del_init(&(rq)->queuelist)
Jens Axboe1fbfdfc2006-07-11 21:49:15 +0200168
David Howells93614012006-09-30 20:45:40 +0200169#endif /* CONFIG_BLOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#endif