| [6adba0a8] | 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Maurizio Lombardi
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup fs
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| [930baca] | 33 | #include <assert.h>
|
|---|
| 34 | #include <errno.h>
|
|---|
| 35 | #include "mfs.h"
|
|---|
| 36 | #include "mfs_utils.h"
|
|---|
| 37 |
|
|---|
| [155f792] | 38 | static int read_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock);
|
|---|
| [930baca] | 39 |
|
|---|
| 40 | /*Given the position in the file expressed in
|
|---|
| 41 | *bytes, this function returns the on-disk block
|
|---|
| 42 | *relative to that position.
|
|---|
| 43 | *Returns zero if the block does not exist.
|
|---|
| 44 | */
|
|---|
| 45 | int read_map(uint32_t *b, const struct mfs_node *mnode, const uint32_t pos)
|
|---|
| 46 | {
|
|---|
| 47 | int r;
|
|---|
| 48 |
|
|---|
| 49 | assert(mnode);
|
|---|
| 50 | assert(mnode->instance);
|
|---|
| 51 |
|
|---|
| 52 | const struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 53 | assert(sbi);
|
|---|
| 54 |
|
|---|
| 55 | const int block_size = sbi->block_size;
|
|---|
| 56 |
|
|---|
| 57 | /*Compute relative block number in file*/
|
|---|
| 58 | int rblock = pos / block_size;
|
|---|
| 59 |
|
|---|
| [155f792] | 60 | if (mnode->ino_i->i_size < (int32_t) pos) {
|
|---|
| [cfbcd86] | 61 | /*Trying to read beyond the end of file*/
|
|---|
| [930baca] | 62 | r = EOK;
|
|---|
| [155f792] | 63 | *b = 0;
|
|---|
| [930baca] | 64 | goto out;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| [155f792] | 67 | r = read_map_ondisk(b, mnode, rblock);
|
|---|
| [930baca] | 68 | out:
|
|---|
| 69 | return r;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| [155f792] | 72 | static int read_map_ondisk(uint32_t *b, const struct mfs_node *mnode, int rblock)
|
|---|
| [930baca] | 73 | {
|
|---|
| [8c76c30] | 74 | int r, nr_direct;
|
|---|
| [155f792] | 75 | int ptrs_per_block;
|
|---|
| [152610a8] | 76 | block_t *bi1;
|
|---|
| 77 | block_t *bi2;
|
|---|
| [930baca] | 78 |
|
|---|
| 79 | assert(mnode);
|
|---|
| [155f792] | 80 | const struct mfs_ino_info *ino_i = mnode->ino_i;
|
|---|
| [930baca] | 81 |
|
|---|
| [155f792] | 82 | assert(ino_i);
|
|---|
| [930baca] | 83 | assert(mnode->instance);
|
|---|
| 84 |
|
|---|
| [152610a8] | 85 | const struct mfs_instance *inst = mnode->instance;
|
|---|
| 86 | const struct mfs_sb_info *sbi = inst->sbi;
|
|---|
| [930baca] | 87 | assert(sbi);
|
|---|
| 88 |
|
|---|
| [155f792] | 89 | const int fs_version = sbi->fs_version;
|
|---|
| 90 |
|
|---|
| 91 | if (fs_version == MFS_VERSION_V1) {
|
|---|
| 92 | nr_direct = V1_NR_DIRECT_ZONES;
|
|---|
| 93 | ptrs_per_block = MFS_BLOCKSIZE / sizeof(uint16_t);
|
|---|
| 94 | } else {
|
|---|
| 95 | nr_direct = V2_NR_DIRECT_ZONES;
|
|---|
| 96 | ptrs_per_block = sbi->block_size / sizeof(uint32_t);
|
|---|
| 97 | }
|
|---|
| [930baca] | 98 |
|
|---|
| [8c76c30] | 99 | /*Check if the wanted block is in the direct zones*/
|
|---|
| [155f792] | 100 | if (rblock < nr_direct) {
|
|---|
| 101 | *b = ino_i->i_dzone[rblock];
|
|---|
| [930baca] | 102 | r = EOK;
|
|---|
| 103 | goto out;
|
|---|
| 104 | }
|
|---|
| [155f792] | 105 | rblock -= nr_direct - 1;
|
|---|
| [930baca] | 106 |
|
|---|
| 107 | if (rblock < ptrs_per_block) {
|
|---|
| [8c76c30] | 108 | /*The wanted block is in the single indirect zone chain*/
|
|---|
| [155f792] | 109 | if (ino_i->i_izone[0] == 0) {
|
|---|
| [930baca] | 110 | r = -1;
|
|---|
| 111 | goto out;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| [152610a8] | 114 | r = block_get(&bi1, inst->handle, ino_i->i_izone[0],
|
|---|
| 115 | BLOCK_FLAGS_NONE);
|
|---|
| 116 | if (r != EOK)
|
|---|
| 117 | goto out;
|
|---|
| [930baca] | 118 |
|
|---|
| [152610a8] | 119 | if (fs_version == MFS_VERSION_V1) {
|
|---|
| 120 | uint16_t tmp = ((uint16_t *) bi1->data)[rblock];
|
|---|
| 121 | *b = conv16(sbi->native, tmp);
|
|---|
| 122 | } else {
|
|---|
| 123 | uint32_t tmp = ((uint32_t *) bi1->data)[rblock];
|
|---|
| 124 | *b = conv32(sbi->native, tmp);
|
|---|
| 125 | }
|
|---|
| [930baca] | 126 |
|
|---|
| [152610a8] | 127 | goto out_put_1;
|
|---|
| [930baca] | 128 | }
|
|---|
| 129 |
|
|---|
| 130 | rblock -= ptrs_per_block - 1;
|
|---|
| 131 |
|
|---|
| [8c76c30] | 132 | /*The wanted block is in the double indirect zone chain*/
|
|---|
| [930baca] | 133 |
|
|---|
| [8c76c30] | 134 | /*read the first indirect zone of the chain*/
|
|---|
| [155f792] | 135 | if (ino_i->i_izone[1] == 0) {
|
|---|
| [930baca] | 136 | r = -1;
|
|---|
| 137 | goto out;
|
|---|
| 138 | }
|
|---|
| [fde8a276] | 139 |
|
|---|
| [152610a8] | 140 | r = block_get(&bi1, inst->handle, ino_i->i_izone[1],
|
|---|
| 141 | BLOCK_FLAGS_NONE);
|
|---|
| [930baca] | 142 |
|
|---|
| 143 | if (r != EOK)
|
|---|
| 144 | goto out;
|
|---|
| 145 |
|
|---|
| [8c76c30] | 146 | /*
|
|---|
| 147 | *Compute the position of the second indirect
|
|---|
| 148 | *zone pointer in the chain.
|
|---|
| 149 | */
|
|---|
| 150 | uint32_t di_block = rblock / ptrs_per_block;
|
|---|
| 151 |
|
|---|
| 152 | /*read the second indirect zone of the chain*/
|
|---|
| [155f792] | 153 | if (fs_version == MFS_VERSION_V1) {
|
|---|
| [152610a8] | 154 | uint16_t *pt16 = bi1->data;
|
|---|
| 155 | uint16_t blk = conv16(sbi->native, pt16[di_block]);
|
|---|
| 156 | r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
|
|---|
| [155f792] | 157 |
|
|---|
| 158 | if (r != EOK)
|
|---|
| [152610a8] | 159 | goto out_put_1;
|
|---|
| [155f792] | 160 |
|
|---|
| [152610a8] | 161 | pt16 = bi2->data;
|
|---|
| 162 | blk = conv16(sbi->native, pt16[di_block % ptrs_per_block]);
|
|---|
| 163 | *b = blk;
|
|---|
| [155f792] | 164 | } else {
|
|---|
| [152610a8] | 165 | uint32_t *pt32 = bi1->data;
|
|---|
| 166 | uint32_t blk = conv32(sbi->native, pt32[di_block]);
|
|---|
| 167 | r = block_get(&bi2, inst->handle, blk, BLOCK_FLAGS_NONE);
|
|---|
| [930baca] | 168 |
|
|---|
| [155f792] | 169 | if (r != EOK)
|
|---|
| [152610a8] | 170 | goto out_put_1;
|
|---|
| [930baca] | 171 |
|
|---|
| [152610a8] | 172 | pt32 = bi2->data;
|
|---|
| 173 | blk = conv32(sbi->native, pt32[di_block % ptrs_per_block]);
|
|---|
| 174 | *b = blk;
|
|---|
| [155f792] | 175 | }
|
|---|
| [8c76c30] | 176 | r = EOK;
|
|---|
| [930baca] | 177 |
|
|---|
| [152610a8] | 178 | block_put(bi2);
|
|---|
| 179 | out_put_1:
|
|---|
| 180 | block_put(bi1);
|
|---|
| [930baca] | 181 | out:
|
|---|
| 182 | return r;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| [6adba0a8] | 185 | /**
|
|---|
| 186 | * @}
|
|---|
| 187 | */
|
|---|
| 188 |
|
|---|