f61dd34de4755cf30b8c397e64174c5a2810d575
[dragonfly.git] / sys / sys / dsched.h
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #ifndef _SYS_DSCHED_H_
35 #define _SYS_DSCHED_H_
36
37 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
38
39 #ifndef _SYS_QUEUE_H_
40 #include <sys/queue.h>
41 #endif
42 #ifndef _SYS_BIO_H_
43 #include <sys/bio.h>
44 #endif
45 #ifndef _SYS_BIOTRACK_H_
46 #include <sys/biotrack.h>
47 #endif
48 #ifndef _SYS_LOCK_H_
49 #include <sys/lock.h>
50 #endif
51 #ifndef _SYS_CONF_H_
52 #include <sys/conf.h>
53 #endif
54 #ifndef _SYS_MSGPORT_H_
55 #include <sys/msgport.h>
56 #endif
57
58 #define DSCHED_POLICY_NAME_LENGTH       64
59
60 #define dsched_set_disk_priv(dp, x)     ((dp)->d_dsched_priv1 = (x))
61 #define dsched_get_disk_priv(dp)        ((dp)?((dp)->d_dsched_priv1):NULL)
62 #define dsched_set_proc_priv(pp, x)     ((pp)->p_dsched_priv1 = (x))
63 #define dsched_get_proc_priv(pp)        ((pp)?((pp)->p_dsched_priv1):NULL)
64
65 #define dsched_set_thread_priv(td, x)   ((td)->td_dsched_priv1 = (x))
66 #define dsched_get_thread_priv(td)      ((td)?((td)->td_dsched_priv1):NULL)
67
68 #define dsched_set_buf_priv(bp, x)      ((bp)->b_iosched = (x))
69 #define dsched_get_buf_priv(bp)         ((bp)?((bp)->b_iosched):NULL)
70 #define dsched_clr_buf_priv(bp)         ((bp)->b_iosched = NULL)
71 #define dsched_is_clear_buf_priv(bp)    ((bp)->b_iosched == NULL)
72
73
74 #define dsched_set_bio_dp(bio, x)       ((bio)->bio_caller_info1.ptr = (x))
75 #define dsched_get_bio_dp(bio)          ((bio)?((bio)->bio_caller_info1.ptr):NULL)
76 #define dsched_set_bio_priv(bio, x)     ((bio)->bio_caller_info2.ptr = (x))
77 #define dsched_get_bio_priv(bio)        ((bio)?((bio)->bio_caller_info2.ptr):NULL)
78 #define dsched_set_bio_stime(bio, x)    ((bio)->bio_caller_info3.lvalue = (x))
79 #define dsched_get_bio_stime(bio)       ((bio)?((bio)->bio_caller_info3.lvalue):0)
80
81
82 typedef int     dsched_prepare_t(struct disk *dp);
83 typedef void    dsched_teardown_t(struct disk *dp);
84 typedef void    dsched_flush_t(struct disk *dp, struct bio *bio);
85 typedef void    dsched_cancel_t(struct disk *dp);
86 typedef int     dsched_queue_t(struct disk *dp, struct bio *bio);
87 typedef void    dsched_new_buf_t(struct buf *bp);
88 typedef void    dsched_new_proc_t(struct proc *p);
89 typedef void    dsched_new_thread_t(struct thread *td);
90 typedef void    dsched_exit_buf_t(struct buf *bp);
91 typedef void    dsched_exit_proc_t(struct proc *p);
92 typedef void    dsched_exit_thread_t(struct thread *td);
93
94 struct dsched_ops {
95         struct {
96                 char            name[DSCHED_POLICY_NAME_LENGTH];
97                 uint64_t        uniq_id;
98                 int             ref_count;
99         } head;
100
101         dsched_prepare_t        *prepare;
102         dsched_teardown_t       *teardown;
103         dsched_flush_t          *flush;
104         dsched_cancel_t         *cancel_all;
105         dsched_queue_t          *bio_queue;
106
107         dsched_new_buf_t        *new_buf;
108         dsched_new_proc_t       *new_proc;
109         dsched_new_thread_t     *new_thread;
110         dsched_exit_buf_t       *exit_buf;
111         dsched_exit_proc_t      *exit_proc;
112         dsched_exit_thread_t    *exit_thread;
113 };
114
115 struct dsched_policy {
116         TAILQ_ENTRY(dsched_policy) link;
117
118         struct dsched_ops       *d_ops;
119 };
120
121 struct dsched_object
122 {
123         struct disk     *dp;
124         struct bio      *bio;
125         int             pid;
126         struct thread   *thread;
127         struct proc     *proc;
128 };
129
130 TAILQ_HEAD(dschedq, dsched_object);
131 TAILQ_HEAD(dsched_policy_head, dsched_policy);
132
133 void    dsched_create(struct disk *dp, const char *head_name, int unit);
134 void    dsched_destroy(struct disk *dp);
135 void    dsched_queue(struct disk *dp, struct bio *bio);
136 int     dsched_register(struct dsched_ops *d_ops);
137 int     dsched_unregister(struct dsched_ops *d_ops);
138 int     dsched_switch(struct disk *dp, struct dsched_ops *new_ops);
139 void    dsched_set_policy(struct disk *dp, struct dsched_ops *new_ops);
140 struct dsched_policy *dsched_find_policy(char *search);
141 struct disk     *dsched_find_disk(char *search);
142 struct dsched_policy *dsched_policy_enumerate(struct dsched_policy *pol);
143 struct disk     *dsched_disk_enumerate(struct disk *dp, struct dsched_ops *ops);
144 void    dsched_cancel_bio(struct bio *bp);
145 void    dsched_strategy_raw(struct disk *dp, struct bio *bp);
146 void    dsched_strategy_sync(struct disk *dp, struct bio *bp);
147 void    dsched_strategy_async(struct disk *dp, struct bio *bp, biodone_t *done, void *priv);
148 int     dsched_debug(int level, char *fmt, ...) __printflike(2, 3);
149 dsched_new_buf_t        dsched_new_buf;
150 dsched_new_proc_t       dsched_new_proc;
151 dsched_new_thread_t     dsched_new_thread;
152 dsched_exit_buf_t       dsched_exit_buf;
153 dsched_exit_proc_t      dsched_exit_proc;
154 dsched_exit_thread_t    dsched_exit_thread;
155
156 #endif /* _KERNEL || _KERNEL_STRUCTURES */
157
158
159 #define DSCHED_NAME_LENGTH              64
160 #define DSCHED_SET_DEVICE_POLICY        _IOWR('d', 1, struct dsched_ioctl)
161 #define DSCHED_LIST_DISKS               _IOWR('d', 2, struct dsched_ioctl)
162 #define DSCHED_LIST_DISK                _IOWR('d', 3, struct dsched_ioctl)
163 #define DSCHED_LIST_POLICIES            _IOWR('d', 4, struct dsched_ioctl)
164
165 struct dsched_ioctl {
166         uint16_t        num_elem;
167         char            dev_name[DSCHED_NAME_LENGTH];
168         char            pol_name[DSCHED_NAME_LENGTH];
169 };
170
171 #endif /* _SYS_DSCHED_H_ */