| 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 |
|
|---|
| 33 | #include <stdlib.h>
|
|---|
| 34 | #include <assert.h>
|
|---|
| 35 | #include <errno.h>
|
|---|
| 36 | #include "mfs.h"
|
|---|
| 37 | #include "mfs_utils.h"
|
|---|
| 38 |
|
|---|
| 39 | static int
|
|---|
| 40 | find_free_bit_and_set(bitchunk_t *b, const int bsize,
|
|---|
| 41 | const bool native, unsigned start_bit);
|
|---|
| 42 |
|
|---|
| 43 | int
|
|---|
| 44 | mfs_free_bit(struct mfs_instance *inst, uint32_t idx, bmap_id_t bid)
|
|---|
| 45 | {
|
|---|
| 46 | struct mfs_sb_info *sbi;
|
|---|
| 47 | int r;
|
|---|
| 48 | unsigned start_block;
|
|---|
| 49 | block_t *b;
|
|---|
| 50 |
|
|---|
| 51 | assert(inst != NULL);
|
|---|
| 52 | sbi = inst->sbi;
|
|---|
| 53 | assert(sbi != NULL);
|
|---|
| 54 |
|
|---|
| 55 | if (bid == BMAP_ZONE) {
|
|---|
| 56 | start_block = 2 + sbi->ibmap_blocks;
|
|---|
| 57 | if (idx > sbi->nzones) {
|
|---|
| 58 | printf(NAME ": Error! Trying to free beyond the" \
|
|---|
| 59 | "bitmap max size\n");
|
|---|
| 60 | return -1;
|
|---|
| 61 | }
|
|---|
| 62 | } else {
|
|---|
| 63 | /*bid == BMAP_INODE*/
|
|---|
| 64 | start_block = 2;
|
|---|
| 65 | if (idx > sbi->ninodes) {
|
|---|
| 66 | printf(NAME ": Error! Trying to free beyond the" \
|
|---|
| 67 | "bitmap max size\n");
|
|---|
| 68 | return -1;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /*Compute the bitmap block*/
|
|---|
| 73 | uint32_t block = idx / (sbi->block_size * 8) + start_block;
|
|---|
| 74 |
|
|---|
| 75 | r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NONE);
|
|---|
| 76 | if (r != EOK)
|
|---|
| 77 | goto out_err;
|
|---|
| 78 |
|
|---|
| 79 | /*Compute the bit index in the block*/
|
|---|
| 80 | idx %= (sbi->block_size * 8);
|
|---|
| 81 | bitchunk_t *ptr = b->data;
|
|---|
| 82 | bitchunk_t chunk;
|
|---|
| 83 | const size_t chunk_bits = sizeof(bitchunk_t) * 8;
|
|---|
| 84 |
|
|---|
| 85 | chunk = conv32(sbi->native, ptr[idx / chunk_bits]);
|
|---|
| 86 | chunk &= ~(1 << (idx % chunk_bits));
|
|---|
| 87 | ptr[idx / chunk_bits] = conv32(sbi->native, chunk);
|
|---|
| 88 | b->dirty = true;
|
|---|
| 89 | r = EOK;
|
|---|
| 90 | block_put(b);
|
|---|
| 91 |
|
|---|
| 92 | out_err:
|
|---|
| 93 | return r;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | int
|
|---|
| 97 | mfs_alloc_bit(struct mfs_instance *inst, uint32_t *idx, bmap_id_t bid)
|
|---|
| 98 | {
|
|---|
| 99 | struct mfs_sb_info *sbi;
|
|---|
| 100 | uint32_t limit;
|
|---|
| 101 | unsigned long nblocks;
|
|---|
| 102 | unsigned *search, i, start_block;
|
|---|
| 103 | unsigned bits_per_block;
|
|---|
| 104 | int r, freebit;
|
|---|
| 105 |
|
|---|
| 106 | assert(inst != NULL);
|
|---|
| 107 | sbi = inst->sbi;
|
|---|
| 108 | assert(sbi != NULL);
|
|---|
| 109 |
|
|---|
| 110 | if (bid == BMAP_ZONE) {
|
|---|
| 111 | search = &sbi->zsearch;
|
|---|
| 112 | start_block = 2 + sbi->ibmap_blocks;
|
|---|
| 113 | nblocks = sbi->zbmap_blocks;
|
|---|
| 114 | limit = sbi->nzones;
|
|---|
| 115 | } else {
|
|---|
| 116 | /*bid == BMAP_INODE*/
|
|---|
| 117 | search = &sbi->isearch;
|
|---|
| 118 | start_block = 2;
|
|---|
| 119 | nblocks = sbi->ibmap_blocks;
|
|---|
| 120 | limit = sbi->ninodes;
|
|---|
| 121 | }
|
|---|
| 122 | bits_per_block = sbi->block_size * 8;
|
|---|
| 123 |
|
|---|
| 124 | block_t *b;
|
|---|
| 125 |
|
|---|
| 126 | retry:
|
|---|
| 127 |
|
|---|
| 128 | for (i = *search / bits_per_block; i < nblocks; ++i) {
|
|---|
| 129 | r = block_get(&b, inst->handle, i + start_block,
|
|---|
| 130 | BLOCK_FLAGS_NONE);
|
|---|
| 131 |
|
|---|
| 132 | if (r != EOK)
|
|---|
| 133 | goto out;
|
|---|
| 134 |
|
|---|
| 135 | freebit = find_free_bit_and_set(b->data, sbi->block_size,
|
|---|
| 136 | sbi->native, *search);
|
|---|
| 137 | if (freebit == -1) {
|
|---|
| 138 | /*No free bit in this block*/
|
|---|
| 139 | block_put(b);
|
|---|
| 140 | continue;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /*Free bit found in this block, compute the real index*/
|
|---|
| 144 | *idx = freebit + bits_per_block * i;
|
|---|
| 145 | *idx += (bid == BMAP_INODE);
|
|---|
| 146 | mfsdebug("alloc index %d %d\n", (int) *idx, i);
|
|---|
| 147 | if (*idx > limit) {
|
|---|
| 148 | /*Index is beyond the limit, it is invalid*/
|
|---|
| 149 | block_put(b);
|
|---|
| 150 | break;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | *search = *idx;
|
|---|
| 154 | b->dirty = true;
|
|---|
| 155 | block_put(b);
|
|---|
| 156 | goto found;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | if (*search > 0) {
|
|---|
| 160 | /*Repeat the search from the first bitmap block*/
|
|---|
| 161 | *search = 0;
|
|---|
| 162 | goto retry;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /*Free bit not found, return error*/
|
|---|
| 166 | return ENOSPC;
|
|---|
| 167 |
|
|---|
| 168 | found:
|
|---|
| 169 | r = EOK;
|
|---|
| 170 |
|
|---|
| 171 | out:
|
|---|
| 172 | return r;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | static int
|
|---|
| 176 | find_free_bit_and_set(bitchunk_t *b, const int bsize,
|
|---|
| 177 | const bool native, unsigned start_bit)
|
|---|
| 178 | {
|
|---|
| 179 | int r = -1;
|
|---|
| 180 | unsigned i, j;
|
|---|
| 181 | bitchunk_t chunk;
|
|---|
| 182 | const size_t chunk_bits = sizeof(bitchunk_t) * 8;
|
|---|
| 183 |
|
|---|
| 184 | for (i = start_bit / sizeof(uint32_t);
|
|---|
| 185 | i < bsize / sizeof(uint32_t); ++i) {
|
|---|
| 186 | if (!(~b[i])) {
|
|---|
| 187 | /*No free bit in this chunk*/
|
|---|
| 188 | continue;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | chunk = conv32(native, b[i]);
|
|---|
| 192 |
|
|---|
| 193 | for (j = 0; j < chunk_bits; ++j) {
|
|---|
| 194 | if (!(chunk & (1 << j))) {
|
|---|
| 195 | mfsdebug("i = %d j = %d\n", i, j);
|
|---|
| 196 | r = i * chunk_bits + j;
|
|---|
| 197 | chunk |= 1 << j;
|
|---|
| 198 | b[i] = conv32(native, chunk);
|
|---|
| 199 | goto found;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | found:
|
|---|
| 204 | return r;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /**
|
|---|
| 208 | * @}
|
|---|
| 209 | */
|
|---|
| 210 |
|
|---|