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 =
18 kmalloc(sizeof(struct bio_request), ALLOC_FLAGS_ZERO);
19 if (!req)
20 return NULL;
21
22 req->disk = d;
23 req->lba = lba;
24 req->size = size;
25 req->sector_count = sec;
26 req->priority = p;
27 req->on_complete = cb;
28 req->buffer = buffer ? buffer : kmalloc_aligned(size, PAGE_SIZE);
29 if (!req->buffer) {
30 kfree(req);
31 return NULL;
32 }
33
34 INIT_LIST_HEAD(list: &req->list);
35 req->write = write;
36 req->user_data = user;
37 req->status = -1;
38
39 return req;
40}
41
42struct bio_request *bio_create_read(struct block_device *d, uint64_t lba,
43 uint64_t sectors, uint64_t size,
44 void (*cb)(struct bio_request *),
45 void *user, void *buffer) {
46 return create(d, lba, sec: sectors, size, p: BIO_RQ_MEDIUM, cb, false, user,
47 buffer);
48}
49
50struct bio_request *bio_create_write(struct block_device *d, uint64_t lba,
51 uint64_t sectors, uint64_t size,
52 void (*cb)(struct bio_request *),
53 void *user, void *buffer) {
54 return create(d, lba, sec: sectors, size, p: BIO_RQ_MEDIUM, cb, true, user, buffer);
55}
56
57void bio_request_free(struct bio_request *req) {
58 kfree(req);
59}
60