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

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

Check the block_put() error code

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