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
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
182 d_info.d_inum = 0;
183 r = mfs_write_dentry(&d_info);
184 return r;
185 }
186 }
187
188 return ENOENT;
189}
190
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 */
199int
200mfs_insert_dentry(struct mfs_node *mnode, const char *d_name,
201 fs_index_t d_inum)
202{
203 int r;
204 struct mfs_sb_info *sbi = mnode->instance->sbi;
205 struct mfs_dentry_info d_info;
206 bool empty_dentry_found = false;
207
208 const size_t name_len = str_size(d_name);
209
210 if (name_len > sbi->max_name_len)
211 return ENAMETOOLONG;
212
213 /* Search for an empty dentry */
214 unsigned i;
215 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
216 r = mfs_read_dentry(mnode, &d_info, i);
217 if (r != EOK)
218 return r;
219
220 if (d_info.d_inum == 0) {
221 /* This entry is not used */
222 empty_dentry_found = true;
223 break;
224 }
225 }
226
227 if (!empty_dentry_found) {
228 uint32_t b, pos;
229 pos = mnode->ino_i->i_size;
230 r = mfs_read_map(&b, mnode, pos);
231 if (r != EOK)
232 goto out;
233
234 if (b == 0) {
235 /* Increase the inode size */
236
237 uint32_t dummy;
238 r = mfs_alloc_zone(mnode->instance, &b);
239 if (r != EOK)
240 goto out;
241 r = mfs_write_map(mnode, pos, b, &dummy);
242 if (r != EOK)
243 goto out;
244 }
245
246 mnode->ino_i->i_size += sbi->dirsize;
247 mnode->ino_i->dirty = true;
248
249 d_info.index = i;
250 d_info.node = mnode;
251 }
252
253 d_info.d_inum = d_inum;
254 memcpy(d_info.d_name, d_name, name_len);
255 if (name_len < sbi->max_name_len)
256 d_info.d_name[name_len] = 0;
257
258 r = mfs_write_dentry(&d_info);
259out:
260 return r;
261}
262
263
264/**
265 * @}
266 */
267
Note: See TracBrowser for help on using the repository browser.