CardSight 0.1.0
C API reference for the CardSight firmware and application
Loading...
Searching...
No Matches
blkdev.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
14
15#ifndef CARDSIGHT_BLKDEV_H
16#define CARDSIGHT_BLKDEV_H
17
18#include <stdbool.h>
19#include <stdint.h>
20#include <errno.h>
21#include <hal/device.h>
22
24#define DISK_NAME_LEN 16
26#define BDEV_LABEL_LEN 32
27
29#define GENHD_FL_RDONLY 0x02
30
32struct gendisk;
33
44 int (*read)(struct gendisk *disk, uint64_t sector, void *buffer, uint32_t count);
46 int (*write)(struct gendisk *disk, uint64_t sector, const void *buffer, uint32_t count);
48 int (*flush)(struct gendisk *disk);
54 void (*free_disk)(struct gendisk *disk);
55};
56
62 struct device dev;
63 struct gendisk *bd_disk;
64 uint32_t partno;
65 uint64_t start_sect;
66 uint64_t nr_sects;
67 uint8_t part_type;
68 bool read_only;
70 uint32_t openers;
71 uint32_t mounts;
73};
74
78struct gendisk {
82 uint64_t capacity;
84 uint32_t flags;
87 uint32_t part_count;
88 struct gendisk *next;
90 bool dead;
91};
92
98struct gendisk *alloc_disk(void);
99
107void put_disk(struct gendisk *disk);
108
119int add_disk(struct gendisk *disk);
120
128int del_gendisk(struct gendisk *disk);
129
137void del_gendisk_force(struct gendisk *disk);
138
145struct block_device *bdev_open_by_name(const char *name);
153void bdev_close(struct block_device *bdev);
154
164int bdev_read(struct block_device *bdev, uint64_t sector, void *buffer, uint32_t count);
174int bdev_write(struct block_device *bdev, uint64_t sector, const void *buffer, uint32_t count);
181int bdev_flush(struct block_device *bdev);
182
189bool bdev_read_only(const struct block_device *bdev);
196uint64_t bdev_nr_sectors(const struct block_device *bdev);
203uint32_t bdev_sector_size(const struct block_device *bdev);
210bool bdev_is_whole(const struct block_device *bdev);
217const char *bdev_name(const struct block_device *bdev);
218
230
231/* ---- partition tables ---- */
232
240
249
258
271
287 const struct blk_partition_layout_entry *entries, uint32_t count);
288
289#endif /* CARDSIGHT_BLKDEV_H */
int blk_erase_partition_table(struct gendisk *disk)
Erase the MBR and both GPT copies.
uint32_t bdev_sector_size(const struct block_device *bdev)
Logical sector size of the disk behind a block device.
blk_partition_content
File system marker stored in a created partition.
Definition blkdev.h:244
@ BLK_PARTITION_EXT4
Definition blkdev.h:247
@ BLK_PARTITION_FAT32
Definition blkdev.h:245
@ BLK_PARTITION_EXFAT
Definition blkdev.h:246
const char * bdev_name(const struct block_device *bdev)
Name of a block device as registered.
uint64_t bdev_nr_sectors(const struct block_device *bdev)
Length of a block device in sectors.
int del_gendisk(struct gendisk *disk)
Take the disk and its partitions away.
struct block_device * bdev_open_by_name(const char *name)
Open a registered block device by name.
int bdev_flush(struct block_device *bdev)
Ask the disk behind a block device to write out its cached writes.
#define DISK_NAME_LEN
Longest disk name, including the terminating zero.
Definition blkdev.h:24
void put_disk(struct gendisk *disk)
Drop the caller's reference to a disk.
int bdev_read(struct block_device *bdev, uint64_t sector, void *buffer, uint32_t count)
Read sectors through a block device view.
void bdev_close(struct block_device *bdev)
Drop one opener of a block device.
void del_gendisk_force(struct gendisk *disk)
Remove the disk without checking its mounts.
struct gendisk * alloc_disk(void)
Allocate a zeroed disk with its whole-disk block device.
bool bdev_is_whole(const struct block_device *bdev)
Report whether a block device covers a whole disk.
int bdev_write(struct block_device *bdev, uint64_t sector, const void *buffer, uint32_t count)
Write sectors through a block device view.
int blk_write_partition_layout(struct gendisk *disk, enum blk_partition_table_type table, const struct blk_partition_layout_entry *entries, uint32_t count)
Write a fresh partition table and rescan it.
int bdev_claim_mount(struct block_device *bdev)
Claim a block device for one file system instance.
int add_disk(struct gendisk *disk)
Register the disk and read its partition table.
blk_partition_table_type
Partition table format written by blk_write_partition_layout().
Definition blkdev.h:236
@ BLK_PARTITION_TABLE_GPT
Definition blkdev.h:238
@ BLK_PARTITION_TABLE_MBR
Definition blkdev.h:237
void bdev_release_mount(struct block_device *bdev)
Release the mount claim of a block device.
bool bdev_read_only(const struct block_device *bdev)
Report whether a block device refuses writes.
#define BDEV_LABEL_LEN
Longest partition label in bytes.
Definition blkdev.h:26
The tiny device model the mmc card and the block layer need: a name and a reference count.
One partition to create, in disk order.
Definition blkdev.h:253
enum blk_partition_content content
Definition blkdev.h:255
uint64_t sectors
Definition blkdev.h:254
char label[BDEV_LABEL_LEN]
Definition blkdev.h:256
Operations a disk driver provides to the block layer.
Definition blkdev.h:37
int(* write)(struct gendisk *disk, uint64_t sector, const void *buffer, uint32_t count)
Write sectors; may be NULL on a disk with GENHD_FL_RDONLY.
Definition blkdev.h:46
int(* read)(struct gendisk *disk, uint64_t sector, void *buffer, uint32_t count)
Read sectors.
Definition blkdev.h:44
void(* free_disk)(struct gendisk *disk)
Release driver state when the last reference is gone.
Definition blkdev.h:54
int(* flush)(struct gendisk *disk)
Write out what the disk holds back; NULL when it writes through.
Definition blkdev.h:48
One openable view of a disk: the whole disk or one partition.
Definition blkdev.h:60
struct block_device * next
Definition blkdev.h:72
struct device dev
Backing device; the name is the disk name or "<disk>p<N>".
Definition blkdev.h:62
uint8_t part_type
Definition blkdev.h:67
uint32_t openers
Definition blkdev.h:70
char label[BDEV_LABEL_LEN]
Definition blkdev.h:69
bool read_only
Definition blkdev.h:68
uint64_t nr_sects
Definition blkdev.h:66
uint32_t mounts
Definition blkdev.h:71
uint64_t start_sect
Definition blkdev.h:65
uint32_t partno
Definition blkdev.h:64
struct gendisk * bd_disk
Definition blkdev.h:63
Reference-counted object shared between the driver and its users.
Definition device.h:21
A disk as a driver describes it to the block layer.
Definition blkdev.h:78
bool dead
Definition blkdev.h:90
void * private_data
Definition blkdev.h:80
uint32_t flags
Definition blkdev.h:84
uint32_t logical_block_size
Definition blkdev.h:83
bool registered
Definition blkdev.h:89
const struct block_device_operations * fops
Definition blkdev.h:81
uint64_t capacity
Definition blkdev.h:82
uint32_t part_count
Definition blkdev.h:87
struct block_device * parts
Definition blkdev.h:86
struct gendisk * next
Definition blkdev.h:88
struct block_device * part0
Definition blkdev.h:85
char disk_name[DISK_NAME_LEN]
Definition blkdev.h:79