[41202a9] | 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 "mfs.h"
|
---|
| 34 |
|
---|
[bb8cd79] | 35 | /**Read a directory entry from disk.
|
---|
| 36 | *
|
---|
| 37 | * @param mnode Pointer to the directory node.
|
---|
| 38 | * @param d_info Pointer to a directory entry structure where the dentry info
|
---|
| 39 | * will be stored.
|
---|
| 40 | * @param index index of the dentry in the list.
|
---|
| 41 | *
|
---|
| 42 | * @return EOK on success or a negative error code.
|
---|
| 43 | */
|
---|
[488f7ed] | 44 | int
|
---|
[3a5ee6c] | 45 | mfs_read_dentry(struct mfs_node *mnode,
|
---|
[e80c2ff] | 46 | struct mfs_dentry_info *d_info, unsigned index)
|
---|
[41202a9] | 47 | {
|
---|
| 48 | const struct mfs_instance *inst = mnode->instance;
|
---|
| 49 | const struct mfs_sb_info *sbi = inst->sbi;
|
---|
| 50 | const bool longnames = sbi->long_names;
|
---|
| 51 | uint32_t block;
|
---|
| 52 | block_t *b;
|
---|
| 53 |
|
---|
[3a5ee6c] | 54 | int r = mfs_read_map(&block, mnode, index * sbi->dirsize);
|
---|
[c699b0c] | 55 | if (r != EOK)
|
---|
| 56 | goto out_err;
|
---|
[41202a9] | 57 |
|
---|
[488f7ed] | 58 | if (block == 0) {
|
---|
[9e2d6dc] | 59 | /*End of the dentries list*/
|
---|
| 60 | r = EOK;
|
---|
[488f7ed] | 61 | goto out_err;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[03bc76a] | 64 | r = block_get(&b, inst->service_id, block, BLOCK_FLAGS_NONE);
|
---|
[c699b0c] | 65 | if (r != EOK)
|
---|
| 66 | goto out_err;
|
---|
[41202a9] | 67 |
|
---|
[7a57269f] | 68 | unsigned dentries_per_zone = sbi->block_size / sbi->dirsize;
|
---|
[1494e52] | 69 | unsigned dentry_off = index % dentries_per_zone;
|
---|
[7a57269f] | 70 |
|
---|
[41202a9] | 71 | if (sbi->fs_version == MFS_VERSION_V3) {
|
---|
| 72 | struct mfs3_dentry *d3;
|
---|
| 73 |
|
---|
[e666ddc] | 74 | d3 = b->data + (dentry_off * MFS3_DIRSIZE);
|
---|
[d0f3692] | 75 |
|
---|
[e80c2ff] | 76 | d_info->d_inum = conv32(sbi->native, d3->d_inum);
|
---|
| 77 | memcpy(d_info->d_name, d3->d_name, MFS3_MAX_NAME_LEN);
|
---|
[41202a9] | 78 | } else {
|
---|
| 79 | const int namelen = longnames ? MFS_L_MAX_NAME_LEN :
|
---|
[44c6091f] | 80 | MFS_MAX_NAME_LEN;
|
---|
[41202a9] | 81 |
|
---|
| 82 | struct mfs_dentry *d;
|
---|
| 83 |
|
---|
[e666ddc] | 84 | d = b->data + dentry_off * (longnames ? MFSL_DIRSIZE :
|
---|
[44c6091f] | 85 | MFS_DIRSIZE);
|
---|
[e80c2ff] | 86 | d_info->d_inum = conv16(sbi->native, d->d_inum);
|
---|
| 87 | memcpy(d_info->d_name, d->d_name, namelen);
|
---|
[41202a9] | 88 | }
|
---|
| 89 |
|
---|
[9530d94] | 90 | r = block_put(b);
|
---|
[41202a9] | 91 |
|
---|
[e80c2ff] | 92 | d_info->index = index;
|
---|
| 93 | d_info->node = mnode;
|
---|
[41202a9] | 94 |
|
---|
| 95 | out_err:
|
---|
[b89281b] | 96 | return r;
|
---|
[41202a9] | 97 | }
|
---|
| 98 |
|
---|
[bb8cd79] | 99 | /**Write a directory entry on disk.
|
---|
| 100 | *
|
---|
| 101 | * @param d_info Pointer to the directory entry structure to write on disk.
|
---|
| 102 | *
|
---|
| 103 | * @return EOK on success or a negative error code.
|
---|
| 104 | */
|
---|
[87d4422] | 105 | int
|
---|
[3a5ee6c] | 106 | mfs_write_dentry(struct mfs_dentry_info *d_info)
|
---|
[87d4422] | 107 | {
|
---|
| 108 | struct mfs_node *mnode = d_info->node;
|
---|
| 109 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
| 110 | const unsigned d_off_bytes = d_info->index * sbi->dirsize;
|
---|
| 111 | const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
|
---|
| 112 | block_t *b;
|
---|
| 113 | uint32_t block;
|
---|
| 114 | int r;
|
---|
| 115 |
|
---|
[3a5ee6c] | 116 | r = mfs_read_map(&block, mnode, d_off_bytes);
|
---|
[c699b0c] | 117 | if (r != EOK)
|
---|
| 118 | goto out;
|
---|
[87d4422] | 119 |
|
---|
[03bc76a] | 120 | r = block_get(&b, mnode->instance->service_id, block, BLOCK_FLAGS_NONE);
|
---|
[c699b0c] | 121 | if (r != EOK)
|
---|
| 122 | goto out;
|
---|
[87d4422] | 123 |
|
---|
[07dcec5] | 124 | const size_t name_len = sbi->max_name_len;
|
---|
[87d4422] | 125 | uint8_t *ptr = b->data;
|
---|
| 126 | ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
|
---|
| 127 |
|
---|
| 128 | if (sbi->fs_version == MFS_VERSION_V3) {
|
---|
| 129 | struct mfs3_dentry *dentry;
|
---|
| 130 | dentry = (struct mfs3_dentry *) ptr;
|
---|
| 131 |
|
---|
| 132 | dentry->d_inum = conv32(sbi->native, d_info->d_inum);
|
---|
| 133 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
---|
| 134 | } else {
|
---|
| 135 | struct mfs_dentry *dentry;
|
---|
| 136 | dentry = (struct mfs_dentry *) ptr;
|
---|
| 137 |
|
---|
| 138 | dentry->d_inum = conv16(sbi->native, d_info->d_inum);
|
---|
| 139 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | b->dirty = true;
|
---|
[9530d94] | 143 | r = block_put(b);
|
---|
[87d4422] | 144 |
|
---|
| 145 | out:
|
---|
| 146 | return r;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[bb8cd79] | 149 | /**Remove a directory entry from a directory.
|
---|
| 150 | *
|
---|
| 151 | * @param mnode Pointer to the directory node.
|
---|
| 152 | * @param d_name Name of the directory entry to delete.
|
---|
| 153 | *
|
---|
| 154 | * @return EOK on success or a negative error code.
|
---|
| 155 | */
|
---|
[c955be91] | 156 | int
|
---|
[3a5ee6c] | 157 | mfs_remove_dentry(struct mfs_node *mnode, const char *d_name)
|
---|
[c955be91] | 158 | {
|
---|
| 159 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
[e80c2ff] | 160 | struct mfs_dentry_info d_info;
|
---|
| 161 | int r;
|
---|
[c955be91] | 162 |
|
---|
| 163 | const size_t name_len = str_size(d_name);
|
---|
| 164 |
|
---|
| 165 | if (name_len > sbi->max_name_len)
|
---|
| 166 | return ENAMETOOLONG;
|
---|
| 167 |
|
---|
| 168 | /*Search the directory entry to be removed*/
|
---|
[e80c2ff] | 169 | unsigned i;
|
---|
| 170 | for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize ; ++i) {
|
---|
[3a5ee6c] | 171 | r = mfs_read_dentry(mnode, &d_info, i);
|
---|
[c699b0c] | 172 | if (r != EOK)
|
---|
| 173 | return r;
|
---|
[c955be91] | 174 |
|
---|
[7a2be00] | 175 | const size_t d_name_len = str_size(d_info.d_name);
|
---|
| 176 |
|
---|
| 177 | if (name_len == d_name_len &&
|
---|
| 178 | !bcmp(d_info.d_name, d_name, name_len)) {
|
---|
[e80c2ff] | 179 | d_info.d_inum = 0;
|
---|
[3a5ee6c] | 180 | r = mfs_write_dentry(&d_info);
|
---|
[c955be91] | 181 | return r;
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | return ENOENT;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[bb8cd79] | 188 | /**Insert a new directory entry in a existing directory.
|
---|
| 189 | *
|
---|
| 190 | * @param mnode Pointer to the directory node.
|
---|
| 191 | * @param d_name Name of the new directory entry.
|
---|
| 192 | * @param d_inum index of the inode that will be pointed by the new dentry.
|
---|
| 193 | *
|
---|
| 194 | * @return EOK on success or a negative error code.
|
---|
| 195 | */
|
---|
[07dcec5] | 196 | int
|
---|
[3a5ee6c] | 197 | mfs_insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum)
|
---|
[07dcec5] | 198 | {
|
---|
[e80c2ff] | 199 | int r;
|
---|
[07dcec5] | 200 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
[e80c2ff] | 201 | struct mfs_dentry_info d_info;
|
---|
[07dcec5] | 202 | bool empty_dentry_found = false;
|
---|
| 203 |
|
---|
| 204 | const size_t name_len = str_size(d_name);
|
---|
| 205 |
|
---|
[c955be91] | 206 | if (name_len > sbi->max_name_len)
|
---|
| 207 | return ENAMETOOLONG;
|
---|
[07dcec5] | 208 |
|
---|
| 209 | /*Search for an empty dentry*/
|
---|
[e80c2ff] | 210 | unsigned i;
|
---|
| 211 | for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
|
---|
[3a5ee6c] | 212 | r = mfs_read_dentry(mnode, &d_info, i);
|
---|
[c699b0c] | 213 | if (r != EOK)
|
---|
| 214 | return r;
|
---|
[07dcec5] | 215 |
|
---|
[e80c2ff] | 216 | if (d_info.d_inum == 0) {
|
---|
[07dcec5] | 217 | /*This entry is not used*/
|
---|
| 218 | empty_dentry_found = true;
|
---|
| 219 | break;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | if (!empty_dentry_found) {
|
---|
[b2a18234] | 224 | uint32_t b, pos;
|
---|
| 225 | pos = mnode->ino_i->i_size;
|
---|
[3a5ee6c] | 226 | r = mfs_read_map(&b, mnode, pos);
|
---|
[c699b0c] | 227 | if (r != EOK)
|
---|
| 228 | goto out;
|
---|
[07dcec5] | 229 |
|
---|
[b2a18234] | 230 | if (b == 0) {
|
---|
| 231 | /*Increase the inode size*/
|
---|
| 232 |
|
---|
| 233 | uint32_t dummy;
|
---|
| 234 | r = mfs_alloc_zone(mnode->instance, &b);
|
---|
[c699b0c] | 235 | if (r != EOK)
|
---|
| 236 | goto out;
|
---|
[3a5ee6c] | 237 | r = mfs_write_map(mnode, pos, b, &dummy);
|
---|
[c699b0c] | 238 | if (r != EOK)
|
---|
| 239 | goto out;
|
---|
[b2a18234] | 240 | }
|
---|
| 241 |
|
---|
| 242 | mnode->ino_i->i_size += sbi->dirsize;
|
---|
| 243 | mnode->ino_i->dirty = true;
|
---|
| 244 |
|
---|
[5b38ecf0] | 245 | d_info.index = i;
|
---|
| 246 | d_info.node = mnode;
|
---|
[07dcec5] | 247 | }
|
---|
| 248 |
|
---|
[e80c2ff] | 249 | d_info.d_inum = d_inum;
|
---|
| 250 | memcpy(d_info.d_name, d_name, name_len);
|
---|
| 251 | d_info.d_name[name_len] = 0;
|
---|
[07dcec5] | 252 |
|
---|
[3a5ee6c] | 253 | r = mfs_write_dentry(&d_info);
|
---|
[2527b1d5] | 254 | out:
|
---|
[9e2d6dc] | 255 | return r;
|
---|
[07dcec5] | 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
[41202a9] | 259 | /**
|
---|
| 260 | * @}
|
---|
[44c6091f] | 261 | */
|
---|
[41202a9] | 262 |
|
---|