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

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

Fix dentry in-block offset calculation.

  • Property mode set to 100644
File size: 5.0 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 <assert.h>
34#include <errno.h>
35#include "mfs.h"
36#include "mfs_utils.h"
37
38int
39read_directory_entry(struct mfs_node *mnode,
40 struct mfs_dentry_info **d_info, unsigned index)
41{
42 const struct mfs_instance *inst = mnode->instance;
43 const struct mfs_sb_info *sbi = inst->sbi;
44 const bool longnames = sbi->long_names;
45 uint32_t block;
46 block_t *b;
47
48 mfsdebug("read_directory(%u)\n", index);
49
50 *d_info = malloc(sizeof(**d_info));
51 if (!*d_info)
52 return ENOMEM;
53
54 int r = 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->handle, 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 block_put(b);
91
92 (*d_info)->index = index;
93 (*d_info)->node = mnode;
94 return EOK;
95
96out_err:
97 free(*d_info);
98 *d_info = NULL;
99 return r;
100}
101
102int
103write_dentry(struct mfs_dentry_info *d_info)
104{
105 struct mfs_node *mnode = d_info->node;
106 struct mfs_sb_info *sbi = mnode->instance->sbi;
107 const unsigned d_off_bytes = d_info->index * sbi->dirsize;
108 const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
109 block_t *b;
110 uint32_t block;
111 int r;
112
113 r = read_map(&block, mnode, d_off_bytes);
114 if (r != EOK)
115 goto out;
116
117 r = block_get(&b, mnode->instance->handle, block, BLOCK_FLAGS_NONE);
118 if (r != EOK)
119 goto out;
120
121 const size_t name_len = sbi->max_name_len;
122 uint8_t *ptr = b->data;
123 ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
124
125 if (sbi->fs_version == MFS_VERSION_V3) {
126 struct mfs3_dentry *dentry;
127 dentry = (struct mfs3_dentry *) ptr;
128
129 dentry->d_inum = conv32(sbi->native, d_info->d_inum);
130 memcpy(dentry->d_name, d_info->d_name, name_len);
131 } else {
132 struct mfs_dentry *dentry;
133 dentry = (struct mfs_dentry *) ptr;
134
135 dentry->d_inum = conv16(sbi->native, d_info->d_inum);
136 memcpy(dentry->d_name, d_info->d_name, name_len);
137 }
138
139 b->dirty = true;
140 block_put(b);
141
142out:
143 return r;
144}
145
146int
147insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum)
148{
149 int i, r;
150 struct mfs_sb_info *sbi = mnode->instance->sbi;
151 struct mfs_dentry_info *d_info;
152 bool empty_dentry_found = false;
153
154 const size_t name_len = str_size(d_name);
155
156 assert(name_len <= sbi->max_name_len);
157
158 /*Search for an empty dentry*/
159
160 for (i = 2; ; ++i) {
161 r = read_directory_entry(mnode, &d_info, i);
162 if (r != EOK)
163 return r;
164
165 if (!d_info) {
166 /*Reached the end of the dentries list*/
167 break;
168 }
169
170 if (d_info->d_inum == 0) {
171 /*This entry is not used*/
172 empty_dentry_found = true;
173 break;
174 }
175 free(d_info);
176 }
177
178 if (!empty_dentry_found) {
179 mfsdebug("inode grow\n");
180 r = inode_grow(mnode, sbi->dirsize);
181 if (r != EOK)
182 return r;
183
184 mfsdebug("read dentry\n");
185 r = read_directory_entry(mnode, &d_info, i);
186 if (r != EOK)
187 return r;
188
189 assert(d_info != NULL);
190 }
191
192 d_info->d_inum = d_inum;
193 memcpy(d_info->d_name, d_name, name_len);
194 d_info->d_name[name_len] = 0;
195
196 r = write_dentry(d_info);
197 mfsdebug("write inode\n");
198 free(d_info);
199
200 return r;
201}
202
203
204/**
205 * @}
206 */
207
Note: See TracBrowser for help on using the repository browser.