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