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 | #include <debug.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <lib/bio.h> |
| 26 | |
| 27 | #define LOCAL_TRACE 0 |
| 28 | |
| 29 | typedef struct { |
| 30 | // inheirit the usual bits |
| 31 | bdev_t dev; |
| 32 | |
| 33 | // we're a subdevice of this |
| 34 | bdev_t *parent; |
| 35 | |
| 36 | // we're this many blocks into it |
| 37 | bnum_t offset; |
| 38 | } subdev_t; |
| 39 | |
| 40 | static ssize_t subdev_read(struct bdev *_dev, void *buf, off_t offset, size_t len) |
| 41 | { |
| 42 | subdev_t *subdev = (subdev_t *)_dev; |
| 43 | |
| 44 | return bio_read(subdev->parent, buf, offset + subdev->offset * subdev->dev.block_size, len); |
| 45 | } |
| 46 | |
| 47 | static ssize_t subdev_read_block(struct bdev *_dev, void *buf, bnum_t block, uint count) |
| 48 | { |
| 49 | subdev_t *subdev = (subdev_t *)_dev; |
| 50 | |
| 51 | return bio_read_block(subdev->parent, buf, block + subdev->offset, count); |
| 52 | } |
| 53 | |
| 54 | static ssize_t subdev_write(struct bdev *_dev, const void *buf, off_t offset, size_t len) |
| 55 | { |
| 56 | subdev_t *subdev = (subdev_t *)_dev; |
| 57 | |
| 58 | return bio_write(subdev->parent, buf, offset + subdev->offset * subdev->dev.block_size, len); |
| 59 | } |
| 60 | |
| 61 | static ssize_t subdev_write_block(struct bdev *_dev, const void *buf, bnum_t block, uint count) |
| 62 | { |
| 63 | subdev_t *subdev = (subdev_t *)_dev; |
| 64 | |
| 65 | return bio_write_block(subdev->parent, buf, block + subdev->offset, count); |
| 66 | } |
| 67 | |
| 68 | static ssize_t subdev_erase(struct bdev *_dev, off_t offset, size_t len) |
| 69 | { |
| 70 | subdev_t *subdev = (subdev_t *)_dev; |
| 71 | |
| 72 | return bio_erase(subdev->parent, offset + subdev->offset * subdev->dev.block_size, len); |
| 73 | } |
| 74 | |
| 75 | static void subdev_close(struct bdev *_dev) |
| 76 | { |
| 77 | subdev_t *subdev = (subdev_t *)_dev; |
| 78 | |
| 79 | bio_close(subdev->parent); |
| 80 | subdev->parent = NULL; |
| 81 | } |
| 82 | |
| 83 | status_t bio_publish_subdevice(const char *parent_dev, const char *subdev, bnum_t startblock, size_t len) |
| 84 | { |
| 85 | LTRACEF("parent %s, sub %s, startblock %u, len %zd\n", parent_dev, subdev, startblock, len); |
| 86 | |
| 87 | bdev_t *parent = bio_open(parent_dev); |
| 88 | if (!parent) |
| 89 | return -1; |
| 90 | |
| 91 | /* make sure we're able to do this */ |
| 92 | if (startblock + len > parent->block_count) |
| 93 | return -1; |
| 94 | |
| 95 | subdev_t *sub = malloc(sizeof(subdev_t)); |
| 96 | bio_initialize_bdev(&sub->dev, subdev, parent->block_size, len); |
| 97 | |
| 98 | sub->parent = parent; |
| 99 | sub->offset = startblock; |
| 100 | |
| 101 | sub->dev.read = &subdev_read; |
| 102 | sub->dev.read_block = &subdev_read_block; |
| 103 | sub->dev.write = &subdev_write; |
| 104 | sub->dev.write_block = &subdev_write_block; |
| 105 | sub->dev.erase = &subdev_erase; |
| 106 | sub->dev.close = &subdev_close; |
| 107 | |
| 108 | bio_register_device(&sub->dev); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |