source: mainline/uspace/srv/fs/minixfs/mfs_dentry.c@ 6f7e0cd

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

Add comments

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