source: mainline/uspace/srv/fs/mfs/mfs_dentry.c@ e895352

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e895352 was c2e50d7, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

cstyle

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[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
[bb8cd79]35/**Read a directory entry from disk.
36 *
37 * @param mnode Pointer to the directory node.
38 * @param d_info Pointer to a directory entry structure where the dentry info
39 * will be stored.
40 * @param index index of the dentry in the list.
41 *
42 * @return EOK on success or a negative error code.
43 */
[488f7ed]44int
[3a5ee6c]45mfs_read_dentry(struct mfs_node *mnode,
[6d4d883]46 struct mfs_dentry_info *d_info, unsigned index)
[41202a9]47{
48 const struct mfs_instance *inst = mnode->instance;
49 const struct mfs_sb_info *sbi = inst->sbi;
50 const bool longnames = sbi->long_names;
51 uint32_t block;
52 block_t *b;
53
[3a5ee6c]54 int r = mfs_read_map(&block, mnode, index * sbi->dirsize);
[c699b0c]55 if (r != EOK)
56 goto out_err;
[41202a9]57
[488f7ed]58 if (block == 0) {
[6d4d883]59 /* End of the dentries list */
[9e2d6dc]60 r = EOK;
[488f7ed]61 goto out_err;
62 }
63
[03bc76a]64 r = block_get(&b, inst->service_id, block, BLOCK_FLAGS_NONE);
[c699b0c]65 if (r != EOK)
66 goto out_err;
[41202a9]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
[e80c2ff]76 d_info->d_inum = conv32(sbi->native, d3->d_inum);
77 memcpy(d_info->d_name, d3->d_name, MFS3_MAX_NAME_LEN);
[f7d6b30]78 d_info->d_name[MFS3_MAX_NAME_LEN] = 0;
[41202a9]79 } else {
80 const int namelen = longnames ? MFS_L_MAX_NAME_LEN :
[6d4d883]81 MFS_MAX_NAME_LEN;
[41202a9]82
83 struct mfs_dentry *d;
84
[e666ddc]85 d = b->data + dentry_off * (longnames ? MFSL_DIRSIZE :
[6d4d883]86 MFS_DIRSIZE);
[e80c2ff]87 d_info->d_inum = conv16(sbi->native, d->d_inum);
88 memcpy(d_info->d_name, d->d_name, namelen);
[f7d6b30]89 d_info->d_name[namelen] = 0;
[41202a9]90 }
91
[9530d94]92 r = block_put(b);
[41202a9]93
[e80c2ff]94 d_info->index = index;
95 d_info->node = mnode;
[41202a9]96
97out_err:
[b89281b]98 return r;
[41202a9]99}
100
[bb8cd79]101/**Write a directory entry on disk.
102 *
[6d4d883]103 * @param d_info Pointer to the directory entry structure to write on disk.
[bb8cd79]104 *
[6d4d883]105 * @return EOK on success or a negative error code.
[bb8cd79]106 */
[87d4422]107int
[3a5ee6c]108mfs_write_dentry(struct mfs_dentry_info *d_info)
[87d4422]109{
110 struct mfs_node *mnode = d_info->node;
111 struct mfs_sb_info *sbi = mnode->instance->sbi;
112 const unsigned d_off_bytes = d_info->index * sbi->dirsize;
113 const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
114 block_t *b;
115 uint32_t block;
116 int r;
117
[3a5ee6c]118 r = mfs_read_map(&block, mnode, d_off_bytes);
[c699b0c]119 if (r != EOK)
120 goto out;
[87d4422]121
[03bc76a]122 r = block_get(&b, mnode->instance->service_id, block, BLOCK_FLAGS_NONE);
[c699b0c]123 if (r != EOK)
124 goto out;
[87d4422]125
[07dcec5]126 const size_t name_len = sbi->max_name_len;
[87d4422]127 uint8_t *ptr = b->data;
128 ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
129
130 if (sbi->fs_version == MFS_VERSION_V3) {
131 struct mfs3_dentry *dentry;
132 dentry = (struct mfs3_dentry *) ptr;
133
134 dentry->d_inum = conv32(sbi->native, d_info->d_inum);
135 memcpy(dentry->d_name, d_info->d_name, name_len);
136 } else {
137 struct mfs_dentry *dentry;
138 dentry = (struct mfs_dentry *) ptr;
139
140 dentry->d_inum = conv16(sbi->native, d_info->d_inum);
141 memcpy(dentry->d_name, d_info->d_name, name_len);
142 }
143
144 b->dirty = true;
[9530d94]145 r = block_put(b);
[87d4422]146
147out:
148 return r;
149}
150
[bb8cd79]151/**Remove a directory entry from a directory.
152 *
153 * @param mnode Pointer to the directory node.
154 * @param d_name Name of the directory entry to delete.
155 *
156 * @return EOK on success or a negative error code.
157 */
[c955be91]158int
[3a5ee6c]159mfs_remove_dentry(struct mfs_node *mnode, const char *d_name)
[c955be91]160{
161 struct mfs_sb_info *sbi = mnode->instance->sbi;
[e80c2ff]162 struct mfs_dentry_info d_info;
163 int r;
[c955be91]164
165 const size_t name_len = str_size(d_name);
166
167 if (name_len > sbi->max_name_len)
168 return ENAMETOOLONG;
169
[c2e50d7]170 /* Search the directory entry to be removed */
[e80c2ff]171 unsigned i;
172 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize ; ++i) {
[3a5ee6c]173 r = mfs_read_dentry(mnode, &d_info, i);
[c699b0c]174 if (r != EOK)
175 return r;
[c955be91]176
[7a2be00]177 const size_t d_name_len = str_size(d_info.d_name);
178
179 if (name_len == d_name_len &&
[6d4d883]180 !bcmp(d_info.d_name, d_name, name_len)) {
181
[e80c2ff]182 d_info.d_inum = 0;
[3a5ee6c]183 r = mfs_write_dentry(&d_info);
[c955be91]184 return r;
185 }
186 }
187
188 return ENOENT;
189}
190
[bb8cd79]191/**Insert a new directory entry in a existing directory.
192 *
193 * @param mnode Pointer to the directory node.
194 * @param d_name Name of the new directory entry.
195 * @param d_inum index of the inode that will be pointed by the new dentry.
196 *
197 * @return EOK on success or a negative error code.
198 */
[07dcec5]199int
[6d4d883]200mfs_insert_dentry(struct mfs_node *mnode, const char *d_name,
201 fs_index_t d_inum)
[07dcec5]202{
[e80c2ff]203 int r;
[07dcec5]204 struct mfs_sb_info *sbi = mnode->instance->sbi;
[e80c2ff]205 struct mfs_dentry_info d_info;
[07dcec5]206 bool empty_dentry_found = false;
207
208 const size_t name_len = str_size(d_name);
209
[c955be91]210 if (name_len > sbi->max_name_len)
211 return ENAMETOOLONG;
[07dcec5]212
[6d4d883]213 /* Search for an empty dentry */
[e80c2ff]214 unsigned i;
215 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
[3a5ee6c]216 r = mfs_read_dentry(mnode, &d_info, i);
[c699b0c]217 if (r != EOK)
218 return r;
[07dcec5]219
[e80c2ff]220 if (d_info.d_inum == 0) {
[6d4d883]221 /* This entry is not used */
[07dcec5]222 empty_dentry_found = true;
223 break;
224 }
225 }
226
227 if (!empty_dentry_found) {
[b2a18234]228 uint32_t b, pos;
229 pos = mnode->ino_i->i_size;
[3a5ee6c]230 r = mfs_read_map(&b, mnode, pos);
[c699b0c]231 if (r != EOK)
232 goto out;
[07dcec5]233
[b2a18234]234 if (b == 0) {
[6d4d883]235 /* Increase the inode size */
[b2a18234]236
237 uint32_t dummy;
238 r = mfs_alloc_zone(mnode->instance, &b);
[c699b0c]239 if (r != EOK)
240 goto out;
[3a5ee6c]241 r = mfs_write_map(mnode, pos, b, &dummy);
[c699b0c]242 if (r != EOK)
243 goto out;
[b2a18234]244 }
245
246 mnode->ino_i->i_size += sbi->dirsize;
247 mnode->ino_i->dirty = true;
248
[5b38ecf0]249 d_info.index = i;
250 d_info.node = mnode;
[07dcec5]251 }
252
[e80c2ff]253 d_info.d_inum = d_inum;
254 memcpy(d_info.d_name, d_name, name_len);
[f7d6b30]255 if (name_len < sbi->max_name_len)
256 d_info.d_name[name_len] = 0;
[07dcec5]257
[3a5ee6c]258 r = mfs_write_dentry(&d_info);
[2527b1d5]259out:
[9e2d6dc]260 return r;
[07dcec5]261}
262
263
[41202a9]264/**
265 * @}
[44c6091f]266 */
[41202a9]267
Note: See TracBrowser for help on using the repository browser.