blob: a8d145ccceb7aaba43d55ca13697aaf4bcd0acfa [file] [log] [blame]
Travis Geiselbrecht52486732010-05-06 14:04:14 -07001/*
2 * Copyright (c) 2009 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#ifndef __LIB_BIO_H
24#define __LIB_BIO_H
25
26#include <sys/types.h>
27#include <list.h>
28
29typedef uint32_t bnum_t;
30
31typedef struct bdev {
32 struct list_node node;
33 volatile int ref;
34
35 /* info about the block device */
36 char *name;
37 off_t size;
38 size_t block_size;
39 bnum_t block_count;
40
41 /* function pointers */
42 ssize_t (*read)(struct bdev *, void *buf, off_t offset, size_t len);
43 ssize_t (*read_block)(struct bdev *, void *buf, bnum_t block, uint count);
44 ssize_t (*write)(struct bdev *, const void *buf, off_t offset, size_t len);
45 ssize_t (*write_block)(struct bdev *, const void *buf, bnum_t block, uint count);
46 ssize_t (*erase)(struct bdev *, off_t offset, size_t len);
47 int (*ioctl)(struct bdev *, int request, void *argp);
48 void (*close)(struct bdev *);
49} bdev_t;
50
51/* user api */
52bdev_t *bio_open(const char *name);
53void bio_close(bdev_t *dev);
54ssize_t bio_read(bdev_t *dev, void *buf, off_t offset, size_t len);
55ssize_t bio_read_block(bdev_t *dev, void *buf, bnum_t block, uint count);
56ssize_t bio_write(bdev_t *dev, const void *buf, off_t offset, size_t len);
57ssize_t bio_write_block(bdev_t *dev, const void *buf, bnum_t block, uint count);
58ssize_t bio_erase(bdev_t *dev, off_t offset, size_t len);
59int bio_ioctl(bdev_t *dev, int request, void *argp);
60
61/* intialize the block device layer */
62void bio_init(void);
63
64/* register a block device */
65void bio_register_device(bdev_t *dev);
66void bio_unregister_device(bdev_t *dev);
67
68/* used during bdev construction */
69void bio_initialize_bdev(bdev_t *dev, const char *name, size_t block_size, bnum_t block_count);
70
71/* debug stuff */
72void bio_dump_devices(void);
73
74/* subdevice support */
75status_t bio_publish_subdevice(const char *parent_dev, const char *subdev, bnum_t startblock, size_t len);
76
77/* memory based block device */
78int create_membdev(const char *name, void *ptr, size_t len);
79
80#endif
81