1#include <block/bio.h>
2#include <block/block.h>
3#include <block/sched.h>
4#include <mem/alloc.h>
5#include <mem/slab.h>
6#include <stdbool.h>
7#include <stdint.h>
8
9SLAB_SIZE_REGISTER_FOR_STRUCT(bio_request, SLAB_OBJ_ALIGN_DEFAULT);
10
11static struct bio_request *create(struct block_device *d, uint64_t lba,
12 uint64_t sec, uint64_t size,
13 enum bio_request_priority p,
14 void (*cb)(struct bio_request *), bool write,
15 void *user, void *buffer) {
16
17 struct bio_request *req = vnew(req, ALLOC_ZERO);
18 if (!req)
19 return NULL;
20
21 req->disk = d;
22 req->lba = lba;
23 req->size = size;
24 req->sector_count = sec;
25 req->priority = p;
26 req->on_complete = cb;
27 req->buffer = buffer ? buffer : valloc_aligned(size, PAGE_SIZE);
28 if (!req->buffer) {
29 vfree(req);
30 return NULL;
31 }
32
33 list_node_init(n: &req->list);
34 req->write = write;
35 req->user_data = user;
36 req->status = -1;
37
38 return req;
39}
40
41struct bio_request *bio_create_read(struct block_device *d, uint64_t lba,
42 uint64_t sectors, uint64_t size,
43 void (*cb)(struct bio_request *),
44 void *user, void *buffer) {
45 return create(d, lba, sec: sectors, size, p: BIO_RQ_MEDIUM, cb, false, user,
46 buffer);
47}
48
49struct bio_request *bio_create_write(struct block_device *d, uint64_t lba,
50 uint64_t sectors, uint64_t size,
51 void (*cb)(struct bio_request *),
52 void *user, void *buffer) {
53 return create(d, lba, sec: sectors, size, p: BIO_RQ_MEDIUM, cb, true, user, buffer);
54}
55
56void bio_request_free(struct bio_request *req) {
57 vfree(req);
58}
59