| 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 | #include "mfs_utils.h"
|
|---|
| 35 |
|
|---|
| 36 | int
|
|---|
| 37 | read_directory_entry(struct mfs_node *mnode,
|
|---|
| 38 | struct mfs_dentry_info **d_info, unsigned index)
|
|---|
| 39 | {
|
|---|
| 40 | const struct mfs_instance *inst = mnode->instance;
|
|---|
| 41 | const struct mfs_sb_info *sbi = inst->sbi;
|
|---|
| 42 | const bool longnames = sbi->long_names;
|
|---|
| 43 | uint32_t block;
|
|---|
| 44 | block_t *b;
|
|---|
| 45 |
|
|---|
| 46 | *d_info = malloc(sizeof(**d_info));
|
|---|
| 47 | if (!*d_info)
|
|---|
| 48 | return ENOMEM;
|
|---|
| 49 |
|
|---|
| 50 | int r = read_map(&block, mnode, index * sbi->dirsize);
|
|---|
| 51 | if (r != EOK)
|
|---|
| 52 | goto out_err;
|
|---|
| 53 |
|
|---|
| 54 | if (block == 0) {
|
|---|
| 55 | /*End of the dentries list*/
|
|---|
| 56 | r = EOK;
|
|---|
| 57 | goto out_err;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NONE);
|
|---|
| 61 | if (r != EOK)
|
|---|
| 62 | goto out_err;
|
|---|
| 63 |
|
|---|
| 64 | unsigned dentries_per_zone = sbi->block_size / sbi->dirsize;
|
|---|
| 65 | unsigned dentry_off = index % dentries_per_zone;
|
|---|
| 66 |
|
|---|
| 67 | if (sbi->fs_version == MFS_VERSION_V3) {
|
|---|
| 68 | struct mfs3_dentry *d3;
|
|---|
| 69 |
|
|---|
| 70 | d3 = b->data + (dentry_off * MFS3_DIRSIZE);
|
|---|
| 71 |
|
|---|
| 72 | (*d_info)->d_inum = conv32(sbi->native, d3->d_inum);
|
|---|
| 73 | memcpy((*d_info)->d_name, d3->d_name, MFS3_MAX_NAME_LEN);
|
|---|
| 74 | } else {
|
|---|
| 75 | const int namelen = longnames ? MFS_L_MAX_NAME_LEN :
|
|---|
| 76 | MFS_MAX_NAME_LEN;
|
|---|
| 77 |
|
|---|
| 78 | struct mfs_dentry *d;
|
|---|
| 79 |
|
|---|
| 80 | d = b->data + dentry_off * (longnames ? MFSL_DIRSIZE :
|
|---|
| 81 | MFS_DIRSIZE);
|
|---|
| 82 | (*d_info)->d_inum = conv16(sbi->native, d->d_inum);
|
|---|
| 83 | memcpy((*d_info)->d_name, d->d_name, namelen);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | block_put(b);
|
|---|
| 87 |
|
|---|
| 88 | (*d_info)->index = index;
|
|---|
| 89 | (*d_info)->node = mnode;
|
|---|
| 90 | return EOK;
|
|---|
| 91 |
|
|---|
| 92 | out_err:
|
|---|
| 93 | free(*d_info);
|
|---|
| 94 | *d_info = NULL;
|
|---|
| 95 | return r;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | int
|
|---|
| 99 | write_dentry(struct mfs_dentry_info *d_info)
|
|---|
| 100 | {
|
|---|
| 101 | struct mfs_node *mnode = d_info->node;
|
|---|
| 102 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 103 | const unsigned d_off_bytes = d_info->index * sbi->dirsize;
|
|---|
| 104 | const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
|
|---|
| 105 | block_t *b;
|
|---|
| 106 | uint32_t block;
|
|---|
| 107 | int r;
|
|---|
| 108 |
|
|---|
| 109 | r = read_map(&block, mnode, d_off_bytes);
|
|---|
| 110 | if (r != EOK)
|
|---|
| 111 | goto out;
|
|---|
| 112 |
|
|---|
| 113 | r = block_get(&b, mnode->instance->handle, block, BLOCK_FLAGS_NONE);
|
|---|
| 114 | if (r != EOK)
|
|---|
| 115 | goto out;
|
|---|
| 116 |
|
|---|
| 117 | const size_t name_len = sbi->max_name_len;
|
|---|
| 118 | uint8_t *ptr = b->data;
|
|---|
| 119 | ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
|
|---|
| 120 |
|
|---|
| 121 | if (sbi->fs_version == MFS_VERSION_V3) {
|
|---|
| 122 | struct mfs3_dentry *dentry;
|
|---|
| 123 | dentry = (struct mfs3_dentry *) ptr;
|
|---|
| 124 |
|
|---|
| 125 | dentry->d_inum = conv32(sbi->native, d_info->d_inum);
|
|---|
| 126 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
|---|
| 127 | } else {
|
|---|
| 128 | struct mfs_dentry *dentry;
|
|---|
| 129 | dentry = (struct mfs_dentry *) ptr;
|
|---|
| 130 |
|
|---|
| 131 | dentry->d_inum = conv16(sbi->native, d_info->d_inum);
|
|---|
| 132 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | b->dirty = true;
|
|---|
| 136 | block_put(b);
|
|---|
| 137 |
|
|---|
| 138 | out:
|
|---|
| 139 | return r;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | int
|
|---|
| 143 | remove_dentry(struct mfs_node *mnode, const char *d_name)
|
|---|
| 144 | {
|
|---|
| 145 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 146 | struct mfs_dentry_info *d_info;
|
|---|
| 147 | int i, r;
|
|---|
| 148 |
|
|---|
| 149 | const size_t name_len = str_size(d_name);
|
|---|
| 150 |
|
|---|
| 151 | if (name_len > sbi->max_name_len)
|
|---|
| 152 | return ENAMETOOLONG;
|
|---|
| 153 |
|
|---|
| 154 | /*Search the directory entry to be removed*/
|
|---|
| 155 | for (i = 0; ; ++i) {
|
|---|
| 156 | r = read_directory_entry(mnode, &d_info, i);
|
|---|
| 157 | on_error(r, return r);
|
|---|
| 158 |
|
|---|
| 159 | if (!d_info) {
|
|---|
| 160 | /*Reached the end of the dentries list*/
|
|---|
| 161 | break;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | if (!bcmp(d_info->d_name, d_name, name_len)) {
|
|---|
| 165 | d_info->d_inum = 0;
|
|---|
| 166 | r = write_dentry(d_info);
|
|---|
| 167 | return r;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | return ENOENT;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | int
|
|---|
| 175 | insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum)
|
|---|
| 176 | {
|
|---|
| 177 | int i, r;
|
|---|
| 178 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
|---|
| 179 | struct mfs_dentry_info *d_info;
|
|---|
| 180 | bool empty_dentry_found = false;
|
|---|
| 181 |
|
|---|
| 182 | const size_t name_len = str_size(d_name);
|
|---|
| 183 |
|
|---|
| 184 | if (name_len > sbi->max_name_len)
|
|---|
| 185 | return ENAMETOOLONG;
|
|---|
| 186 |
|
|---|
| 187 | /*Search for an empty dentry*/
|
|---|
| 188 |
|
|---|
| 189 | for (i = 0; ; ++i) {
|
|---|
| 190 | r = read_directory_entry(mnode, &d_info, i);
|
|---|
| 191 | if (r != EOK)
|
|---|
| 192 | return r;
|
|---|
| 193 |
|
|---|
| 194 | if (!d_info) {
|
|---|
| 195 | /*Reached the end of the dentries list*/
|
|---|
| 196 | break;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | if (d_info->d_inum == 0) {
|
|---|
| 200 | /*This entry is not used*/
|
|---|
| 201 | empty_dentry_found = true;
|
|---|
| 202 | break;
|
|---|
| 203 | }
|
|---|
| 204 | free(d_info);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | if (!empty_dentry_found) {
|
|---|
| 208 | mfsdebug("inode grow\n");
|
|---|
| 209 | r = inode_grow(mnode, sbi->dirsize);
|
|---|
| 210 | if (r != EOK)
|
|---|
| 211 | return r;
|
|---|
| 212 |
|
|---|
| 213 | mfsdebug("read dentry %d\n", i);
|
|---|
| 214 | r = read_directory_entry(mnode, &d_info, i);
|
|---|
| 215 | if (r != EOK)
|
|---|
| 216 | return r;
|
|---|
| 217 |
|
|---|
| 218 | assert(d_info != NULL);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | d_info->d_inum = d_inum;
|
|---|
| 222 | memcpy(d_info->d_name, d_name, name_len);
|
|---|
| 223 | d_info->d_name[name_len] = 0;
|
|---|
| 224 |
|
|---|
| 225 | r = write_dentry(d_info);
|
|---|
| 226 | mfsdebug("write inode\n");
|
|---|
| 227 | free(d_info);
|
|---|
| 228 |
|
|---|
| 229 | return r;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 | /**
|
|---|
| 234 | * @}
|
|---|
| 235 | */
|
|---|
| 236 |
|
|---|