[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 <assert.h>
|
---|
| 34 | #include <errno.h>
|
---|
| 35 | #include "mfs.h"
|
---|
| 36 | #include "mfs_utils.h"
|
---|
| 37 |
|
---|
[488f7ed] | 38 | int
|
---|
| 39 | read_directory_entry(struct mfs_node *mnode,
|
---|
| 40 | struct mfs_dentry_info **d_info, unsigned index)
|
---|
[41202a9] | 41 | {
|
---|
| 42 | const struct mfs_instance *inst = mnode->instance;
|
---|
| 43 | const struct mfs_sb_info *sbi = inst->sbi;
|
---|
| 44 | const bool longnames = sbi->long_names;
|
---|
| 45 | uint32_t block;
|
---|
| 46 | block_t *b;
|
---|
| 47 |
|
---|
[e666ddc] | 48 | mfsdebug("read_directory(%u)\n", index);
|
---|
[41202a9] | 49 |
|
---|
[488f7ed] | 50 | *d_info = malloc(sizeof(**d_info));
|
---|
| 51 | if (!*d_info)
|
---|
| 52 | return ENOMEM;
|
---|
[41202a9] | 53 |
|
---|
[ac28650] | 54 | int r = read_map(&block, mnode, index * sbi->dirsize);
|
---|
[488f7ed] | 55 | if (r != EOK)
|
---|
[41202a9] | 56 | goto out_err;
|
---|
| 57 |
|
---|
[488f7ed] | 58 | if (block == 0) {
|
---|
[9e2d6dc] | 59 | /*End of the dentries list*/
|
---|
| 60 | r = EOK;
|
---|
[488f7ed] | 61 | goto out_err;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[41202a9] | 64 | r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NONE);
|
---|
| 65 | if (r != EOK)
|
---|
| 66 | goto out_err;
|
---|
| 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 |
|
---|
[488f7ed] | 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 :
|
---|
| 80 | MFS_MAX_NAME_LEN;
|
---|
| 81 |
|
---|
| 82 | struct mfs_dentry *d;
|
---|
| 83 |
|
---|
[e666ddc] | 84 | d = b->data + dentry_off * (longnames ? MFSL_DIRSIZE :
|
---|
| 85 | MFS_DIRSIZE);
|
---|
[488f7ed] | 86 | (*d_info)->d_inum = conv16(sbi->native, d->d_inum);
|
---|
| 87 | memcpy((*d_info)->d_name, d->d_name, namelen);
|
---|
[41202a9] | 88 | }
|
---|
| 89 |
|
---|
| 90 | block_put(b);
|
---|
| 91 |
|
---|
[488f7ed] | 92 | (*d_info)->index = index;
|
---|
| 93 | (*d_info)->node = mnode;
|
---|
| 94 | return EOK;
|
---|
[41202a9] | 95 |
|
---|
| 96 | out_err:
|
---|
[488f7ed] | 97 | free(*d_info);
|
---|
| 98 | *d_info = NULL;
|
---|
| 99 | return r;
|
---|
[41202a9] | 100 | }
|
---|
| 101 |
|
---|
[87d4422] | 102 | int
|
---|
| 103 | write_dentry(struct mfs_dentry_info *d_info)
|
---|
| 104 | {
|
---|
| 105 | struct mfs_node *mnode = d_info->node;
|
---|
| 106 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
| 107 | const unsigned d_off_bytes = d_info->index * sbi->dirsize;
|
---|
| 108 | const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
|
---|
| 109 | block_t *b;
|
---|
| 110 | uint32_t block;
|
---|
| 111 | int r;
|
---|
| 112 |
|
---|
| 113 | r = read_map(&block, mnode, d_off_bytes);
|
---|
| 114 | if (r != EOK)
|
---|
| 115 | goto out;
|
---|
| 116 |
|
---|
| 117 | r = block_get(&b, mnode->instance->handle, block, BLOCK_FLAGS_NONE);
|
---|
| 118 | if (r != EOK)
|
---|
| 119 | goto out;
|
---|
| 120 |
|
---|
[07dcec5] | 121 | const size_t name_len = sbi->max_name_len;
|
---|
[87d4422] | 122 | uint8_t *ptr = b->data;
|
---|
| 123 | ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
|
---|
| 124 |
|
---|
| 125 | if (sbi->fs_version == MFS_VERSION_V3) {
|
---|
| 126 | struct mfs3_dentry *dentry;
|
---|
| 127 | dentry = (struct mfs3_dentry *) ptr;
|
---|
| 128 |
|
---|
| 129 | dentry->d_inum = conv32(sbi->native, d_info->d_inum);
|
---|
| 130 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
---|
| 131 | } else {
|
---|
| 132 | struct mfs_dentry *dentry;
|
---|
| 133 | dentry = (struct mfs_dentry *) ptr;
|
---|
| 134 |
|
---|
| 135 | dentry->d_inum = conv16(sbi->native, d_info->d_inum);
|
---|
| 136 | memcpy(dentry->d_name, d_info->d_name, name_len);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | b->dirty = true;
|
---|
| 140 | block_put(b);
|
---|
| 141 |
|
---|
| 142 | out:
|
---|
| 143 | return r;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[07dcec5] | 146 | int
|
---|
[13ab195] | 147 | insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum)
|
---|
[07dcec5] | 148 | {
|
---|
| 149 | int i, r;
|
---|
| 150 | struct mfs_sb_info *sbi = mnode->instance->sbi;
|
---|
| 151 | struct mfs_dentry_info *d_info;
|
---|
| 152 | bool empty_dentry_found = false;
|
---|
| 153 |
|
---|
| 154 | const size_t name_len = str_size(d_name);
|
---|
| 155 |
|
---|
| 156 | assert(name_len <= sbi->max_name_len);
|
---|
| 157 |
|
---|
| 158 | /*Search for an empty dentry*/
|
---|
| 159 |
|
---|
| 160 | for (i = 2; ; ++i) {
|
---|
[488f7ed] | 161 | r = read_directory_entry(mnode, &d_info, i);
|
---|
| 162 | if (r != EOK)
|
---|
| 163 | return r;
|
---|
[07dcec5] | 164 |
|
---|
| 165 | if (!d_info) {
|
---|
| 166 | /*Reached the end of the dentries list*/
|
---|
| 167 | break;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | if (d_info->d_inum == 0) {
|
---|
| 171 | /*This entry is not used*/
|
---|
| 172 | empty_dentry_found = true;
|
---|
| 173 | break;
|
---|
| 174 | }
|
---|
| 175 | free(d_info);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if (!empty_dentry_found) {
|
---|
[1494e52] | 179 | mfsdebug("inode grow\n");
|
---|
[07dcec5] | 180 | r = inode_grow(mnode, sbi->dirsize);
|
---|
| 181 | if (r != EOK)
|
---|
| 182 | return r;
|
---|
| 183 |
|
---|
[1494e52] | 184 | mfsdebug("read dentry\n");
|
---|
[488f7ed] | 185 | r = read_directory_entry(mnode, &d_info, i);
|
---|
| 186 | if (r != EOK)
|
---|
| 187 | return r;
|
---|
[1494e52] | 188 |
|
---|
| 189 | assert(d_info != NULL);
|
---|
[07dcec5] | 190 | }
|
---|
| 191 |
|
---|
| 192 | d_info->d_inum = d_inum;
|
---|
| 193 | memcpy(d_info->d_name, d_name, name_len);
|
---|
| 194 | d_info->d_name[name_len] = 0;
|
---|
| 195 |
|
---|
[9e2d6dc] | 196 | r = write_dentry(d_info);
|
---|
[1494e52] | 197 | mfsdebug("write inode\n");
|
---|
[9e2d6dc] | 198 | free(d_info);
|
---|
| 199 |
|
---|
| 200 | return r;
|
---|
[07dcec5] | 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
[41202a9] | 204 | /**
|
---|
| 205 | * @}
|
---|
| 206 | */
|
---|
| 207 |
|
---|