bio.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <sys/types.h>
  26. #include <list.h>
  27. typedef uint32_t bnum_t;
  28. typedef struct bdev {
  29. struct list_node node;
  30. volatile int ref;
  31. /* info about the block device */
  32. char *name;
  33. off_t size;
  34. size_t block_size;
  35. bnum_t block_count;
  36. /* function pointers */
  37. ssize_t (*read)(struct bdev *, void *buf, off_t offset, size_t len);
  38. ssize_t (*read_block)(struct bdev *, void *buf, bnum_t block, uint count);
  39. ssize_t (*write)(struct bdev *, const void *buf, off_t offset, size_t len);
  40. ssize_t (*write_block)(struct bdev *, const void *buf, bnum_t block, uint count);
  41. ssize_t (*erase)(struct bdev *, off_t offset, size_t len);
  42. int (*ioctl)(struct bdev *, int request, void *argp);
  43. void (*close)(struct bdev *);
  44. } bdev_t;
  45. /* user api */
  46. bdev_t *bio_open(const char *name);
  47. void bio_close(bdev_t *dev);
  48. ssize_t bio_read(bdev_t *dev, void *buf, off_t offset, size_t len);
  49. ssize_t bio_read_block(bdev_t *dev, void *buf, bnum_t block, uint count);
  50. ssize_t bio_write(bdev_t *dev, const void *buf, off_t offset, size_t len);
  51. ssize_t bio_write_block(bdev_t *dev, const void *buf, bnum_t block, uint count);
  52. ssize_t bio_erase(bdev_t *dev, off_t offset, size_t len);
  53. int bio_ioctl(bdev_t *dev, int request, void *argp);
  54. /* intialize the block device layer */
  55. void bio_init(void);
  56. /* register a block device */
  57. void bio_register_device(bdev_t *dev);
  58. void bio_unregister_device(bdev_t *dev);
  59. /* used during bdev construction */
  60. void bio_initialize_bdev(bdev_t *dev, const char *name, size_t block_size, bnum_t block_count);
  61. /* debug stuff */
  62. void bio_dump_devices(void);
  63. /* subdevice support */
  64. status_t bio_publish_subdevice(const char *parent_dev, const char *subdev, bnum_t startblock, size_t len);
  65. /* memory based block device */
  66. int create_membdev(const char *name, void *ptr, size_t len);
  67. #endif