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