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