summaryrefslogtreecommitdiffstats
path: root/include/block.h
blob: a0f226c7649f4bb86079d6fa3f23c1389896ec75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef __BLOCK_H
#define __BLOCK_H

#include <driver.h>
#include <linux/list.h>
#include <linux/types.h>

struct block_device;
struct file_list;

struct block_device_ops {
	int (*read)(struct block_device *, void *buf, sector_t block, blkcnt_t num_blocks);
	int (*write)(struct block_device *, const void *buf, sector_t block, blkcnt_t num_blocks);
	int (*flush)(struct block_device *);
};

struct chunk;

enum blk_type {
	BLK_TYPE_UNSPEC = 0,
	BLK_TYPE_USB,
	BLK_TYPE_SD,
	BLK_TYPE_AHCI,
	BLK_TYPE_IDE,
	BLK_TYPE_NVME,
	BLK_TYPE_VIRTUAL,
	BLK_TYPE_MMC,
};

const char *blk_type_str(enum blk_type);

struct block_device {
	struct device *dev;
	struct list_head list;
	struct block_device_ops *ops;
	u8 blockbits;
	u8 type; /* holds enum blk_type */
	blkcnt_t num_blocks;
	int rdbufsize;
	int blkmask;

	sector_t discard_start;
	blkcnt_t discard_size;

	struct list_head buffered_blocks;
	struct list_head idle_blocks;

	struct cdev cdev;

	bool need_reparse;
};

#define BLOCKSIZE(blk)	(1u << (blk)->blockbits)

extern struct list_head block_device_list;

#define for_each_block_device(bdev) list_for_each_entry(bdev, &block_device_list, list)

int blockdevice_register(struct block_device *blk);
int blockdevice_unregister(struct block_device *blk);

int block_read(struct block_device *blk, void *buf, sector_t block, blkcnt_t num_blocks);
int block_write(struct block_device *blk, void *buf, sector_t block, blkcnt_t num_blocks);

static inline int block_flush(struct block_device *blk)
{
	return cdev_flush(&blk->cdev);
}

#ifdef CONFIG_BLOCK
struct block_device *cdev_get_block_device(const struct cdev *cdev);
unsigned file_list_add_blockdevs(struct file_list *files);
#else
static inline struct block_device *cdev_get_block_device(const struct cdev *cdev)
{
	return NULL;
}
static inline unsigned file_list_add_blockdevs(struct file_list *files)
{
	return 0;
}
#endif

static inline bool cdev_is_block_device(const struct cdev *cdev)
{
	return cdev_get_block_device(cdev) != NULL;
}

static inline bool cdev_is_block_partition(const struct cdev *cdev)
{
	return cdev_is_block_device(cdev) && cdev_is_partition(cdev);
}

static inline bool cdev_is_block_disk(const struct cdev *cdev)
{
	return cdev_is_block_device(cdev) && !cdev_is_partition(cdev);
}

#endif /* __BLOCK_H */