Travis Geiselbrecht | 5248673 | 2010-05-06 14:04:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | typedef uint32_t bnum_t; |
| 30 | |
| 31 | typedef 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 */ |
| 52 | bdev_t *bio_open(const char *name); |
| 53 | void bio_close(bdev_t *dev); |
| 54 | ssize_t bio_read(bdev_t *dev, void *buf, off_t offset, size_t len); |
| 55 | ssize_t bio_read_block(bdev_t *dev, void *buf, bnum_t block, uint count); |
| 56 | ssize_t bio_write(bdev_t *dev, const void *buf, off_t offset, size_t len); |
| 57 | ssize_t bio_write_block(bdev_t *dev, const void *buf, bnum_t block, uint count); |
| 58 | ssize_t bio_erase(bdev_t *dev, off_t offset, size_t len); |
| 59 | int bio_ioctl(bdev_t *dev, int request, void *argp); |
| 60 | |
| 61 | /* intialize the block device layer */ |
| 62 | void bio_init(void); |
| 63 | |
| 64 | /* register a block device */ |
| 65 | void bio_register_device(bdev_t *dev); |
| 66 | void bio_unregister_device(bdev_t *dev); |
| 67 | |
| 68 | /* used during bdev construction */ |
| 69 | void bio_initialize_bdev(bdev_t *dev, const char *name, size_t block_size, bnum_t block_count); |
| 70 | |
| 71 | /* debug stuff */ |
| 72 | void bio_dump_devices(void); |
| 73 | |
| 74 | /* subdevice support */ |
| 75 | status_t bio_publish_subdevice(const char *parent_dev, const char *subdev, bnum_t startblock, size_t len); |
| 76 | |
| 77 | /* memory based block device */ |
| 78 | int create_membdev(const char *name, void *ptr, size_t len); |
| 79 | |
| 80 | #endif |
| 81 | |