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

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

Fix a bug that will corrupt the directory entry when creating files/dirs using the maximum file name lenght

  • Property mode set to 100644
File size: 6.7 KB
Line 
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
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 */
44int
45mfs_read_dentry(struct mfs_node *mnode,
46 struct mfs_dentry_info *d_info, unsigned index)
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
54 int r = mfs_read_map(&block, mnode, index * sbi->dirsize);
55 if (r != EOK)
56 goto out_err;
57
58 if (block == 0) {
59 /*End of the dentries list*/
60 r = EOK;
61 goto out_err;
62 }
63
64 r = block_get(&b, inst->service_id, block, BLOCK_FLAGS_NONE);
65 if (r != EOK)
66 goto out_err;
67
68 unsigned dentries_per_zone = sbi->block_size / sbi->dirsize;
69 unsigned dentry_off = index % dentries_per_zone;
70
71 if (sbi->fs_version == MFS_VERSION_V3) {
72 struct mfs3_dentry *d3;
73
74 d3 = b->data + (dentry_off * MFS3_DIRSIZE);
75
76 d_info->d_inum = conv32(sbi->native, d3->d_inum);
77 memcpy(d_info->d_name, d3->d_name, MFS3_MAX_NAME_LEN);
78 d_info->d_name[MFS3_MAX_NAME_LEN] = 0;
79 } else {
80 const int namelen = longnames ? MFS_L_MAX_NAME_LEN :
81 MFS_MAX_NAME_LEN;
82
83 struct mfs_dentry *d;
84
85 d = b->data + dentry_off * (longnames ? MFSL_DIRSIZE :
86 MFS_DIRSIZE);
87 d_info->d_inum = conv16(sbi->native, d->d_inum);
88 memcpy(d_info->d_name, d->d_name, namelen);
89 d_info->d_name[namelen] = 0;
90 }
91
92 r = block_put(b);
93
94 d_info->index = index;
95 d_info->node = mnode;
96
97out_err:
98 return r;
99}
100
101/**Write a directory entry on disk.
102 *
103 * @param d_info Pointer to the directory entry structure to write on disk.
104 *
105 * @return EOK on success or a negative error code.
106 */
107int
108mfs_write_dentry(struct mfs_dentry_info *d_info)
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
118 r = mfs_read_map(&block, mnode, d_off_bytes);
119 if (r != EOK)
120 goto out;
121
122 r = block_get(&b, mnode->instance->service_id, block, BLOCK_FLAGS_NONE);
123 if (r != EOK)
124 goto out;
125
126 const size_t name_len = sbi->max_name_len;
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;
145 r = block_put(b);
146
147out:
148 return r;
149}
150
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 */
158int
159mfs_remove_dentry(struct mfs_node *mnode, const char *d_name)
160{
161 struct mfs_sb_info *sbi = mnode->instance->sbi;
162 struct mfs_dentry_info d_info;
163 int r;
164
165 const size_t name_len = str_size(d_name);
166
167 if (name_len > sbi->max_name_len)
168 return ENAMETOOLONG;
169
170 /*Search the directory entry to be removed*/
171 unsigned i;
172 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize ; ++i) {
173 r = mfs_read_dentry(mnode, &d_info, i);
174 if (r != EOK)
175 return r;
176
177 const size_t d_name_len = str_size(d_info.d_name);
178
179 if (name_len == d_name_len &&
180 !bcmp(d_info.d_name, d_name, name_len)) {
181 d_info.d_inum = 0;
182 r = mfs_write_dentry(&d_info);
183 return r;
184 }
185 }
186
187 return ENOENT;
188}
189
190/**Insert a new directory entry in a existing directory.
191 *
192 * @param mnode Pointer to the directory node.
193 * @param d_name Name of the new directory entry.
194 * @param d_inum index of the inode that will be pointed by the new dentry.
195 *
196 * @return EOK on success or a negative error code.
197 */
198int
199mfs_insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum)
200{
201 int r;
202 struct mfs_sb_info *sbi = mnode->instance->sbi;
203 struct mfs_dentry_info d_info;
204 bool empty_dentry_found = false;
205
206 const size_t name_len = str_size(d_name);
207
208 if (name_len > sbi->max_name_len)
209 return ENAMETOOLONG;
210
211 /*Search for an empty dentry*/
212 unsigned i;
213 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
214 r = mfs_read_dentry(mnode, &d_info, i);
215 if (r != EOK)
216 return r;
217
218 if (d_info.d_inum == 0) {
219 /*This entry is not used*/
220 empty_dentry_found = true;
221 break;
222 }
223 }
224
225 if (!empty_dentry_found) {
226 uint32_t b, pos;
227 pos = mnode->ino_i->i_size;
228 r = mfs_read_map(&b, mnode, pos);
229 if (r != EOK)
230 goto out;
231
232 if (b == 0) {
233 /*Increase the inode size*/
234
235 uint32_t dummy;
236 r = mfs_alloc_zone(mnode->instance, &b);
237 if (r != EOK)
238 goto out;
239 r = mfs_write_map(mnode, pos, b, &dummy);
240 if (r != EOK)
241 goto out;
242 }
243
244 mnode->ino_i->i_size += sbi->dirsize;
245 mnode->ino_i->dirty = true;
246
247 d_info.index = i;
248 d_info.node = mnode;
249 }
250
251 d_info.d_inum = d_inum;
252 memcpy(d_info.d_name, d_name, name_len);
253 if (name_len < sbi->max_name_len)
254 d_info.d_name[name_len] = 0;
255
256 r = mfs_write_dentry(&d_info);
257out:
258 return r;
259}
260
261
262/**
263 * @}
264 */
265
Note: See TracBrowser for help on using the repository browser.