1 #ifndef __LUNAIX_BLKIO_H
2 #define __LUNAIX_BLKIO_H
4 #include <lunaix/buffer.h>
5 #include <lunaix/buffer.h>
6 #include <lunaix/ds/llist.h>
7 #include <lunaix/ds/waitq.h>
8 #include <lunaix/ds/mutex.h>
9 #include <lunaix/types.h>
11 #define BLKIO_WRITE 0x1
12 #define BLKIO_ERROR 0x2
14 #define BLKIO_BUSY 0x4
15 #define BLKIO_PENDING 0x8
17 #define BLKIO_FOC 0x10
18 #define BLKIO_SHOULD_WAIT 0x20
20 #define BLKIO_WAIT 0x1
21 #define BLKIO_NOWAIT 0
22 #define BLKIO_NOASYNC 0x2
24 #define BLKIO_SCHED_IDEL 0x1
28 typedef void (*blkio_cb)(struct blkio_req*);
29 typedef void (*req_handler)(struct blkio_req*);
33 struct llist_header reqs;
34 struct blkio_context* io_ctx;
46 struct llist_header queue;
54 req_handler handle_one;
63 blkio_lock(struct blkio_context* contex)
65 mutex_lock(&contex->lock);
69 blkio_unlock(struct blkio_context* contex)
71 mutex_unlock(&contex->lock);
75 blkio_stalled(struct blkio_context* contex)
84 * @brief Vectorized read request
91 * @return struct blkio_req*
94 blkio_vrd(struct vecbuf* vbuf,
101 * @brief Vectorized write request
108 * @return struct blkio_req*
111 blkio_vwr(struct vecbuf* vbuf,
118 * @brief Vectorized request (no write/read preference)
125 * @return struct blkio_req*
127 static inline struct blkio_req*
128 blkio_vreq(struct vecbuf* buffer,
134 This is currently aliased to blkio_vrd. Although `no preference`
135 does essentially mean `default read`, the blkio_vreq just used
136 to enhance readability
138 return blkio_vrd(buffer, start_lba, completed, evt_args, options);
143 * @brief Bind a block IO context to request
149 blkio_bindctx(struct blkio_req* req, struct blkio_context* ctx)
155 * @brief Set block IO request to read
161 blkio_setread(struct blkio_req* req)
163 if ((req->flags & BLKIO_PENDING)) {
167 req->flags &= ~BLKIO_WRITE;
171 * @brief Set block IO request to write
177 blkio_setwrite(struct blkio_req* req)
179 if ((req->flags & BLKIO_PENDING)) {
183 req->flags |= BLKIO_WRITE;
187 * @brief Set callback when request complete
190 * @param on_completed
193 blkio_when_completed(struct blkio_req* req, blkio_cb on_completed)
195 req->completed = on_completed;
199 blkio_is_pending(struct blkio_req* req)
201 return (req->flags & BLKIO_PENDING);
205 * @brief Mark request to be freed-on-completion (FOC)
210 blkio_mark_foc(struct blkio_req* req)
212 req->flags |= BLKIO_FOC;
216 * @brief Mark request to be not-freed-on-completion (nFOC)
221 blkio_mark_nfoc(struct blkio_req* req)
223 req->flags &= ~BLKIO_FOC;
227 blkio_read_aligned(struct blkio_context* ctx,
228 unsigned long lba, void* block, size_t n_blk);
231 blkio_read(struct blkio_context* ctx,
232 unsigned long offset, void* block, size_t len);
235 blkio_write_aligned(struct blkio_context* ctx,
236 unsigned long lba, void* block, size_t n_blk);
239 blkio_read(struct blkio_context* ctx,
240 unsigned long offset, void* block, size_t len);
243 blkio_free_req(struct blkio_req* req);
246 * @brief Commit an IO request to scheduler.
252 blkio_commit(struct blkio_req* req, int options);
256 * @brief Schedule an IO request to be handled.
261 blkio_schedule(struct blkio_context* ctx);
264 * @brief Notify the scheduler when request is completed, either successful or
271 blkio_complete(struct blkio_req* req);
274 * @brief Create a new block IO scheduling context
276 * @param handler Handler to handle request
277 * @return struct blkio_context*
279 struct blkio_context*
280 blkio_newctx(req_handler handler);
282 #endif /* __LUNAIX_BLKIO_H */