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

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

rename functions to avoid conflicts

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