source: mainline/uspace/srv/fs/minixfs/mfs_inode.c@ 230229de

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

The root inode (index == 1) is at offset 0 in the inode table.

  • Property mode set to 100644
File size: 8.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 <stdlib.h>
34#include <assert.h>
35#include <errno.h>
36#include <mem.h>
37#include "mfs.h"
38#include "mfs_utils.h"
39
40static int
41mfs_write_inode_raw(struct mfs_node *mnode);
42
43static int
44mfs2_write_inode_raw(struct mfs_node *mnode);
45
46static struct mfs_ino_info *
47mfs_read_inode_raw(const struct mfs_instance *instance, uint16_t inum);
48
49static struct mfs_ino_info *
50mfs2_read_inode_raw(const struct mfs_instance *instance, uint32_t inum);
51
52
53int
54get_inode(struct mfs_instance *inst, struct mfs_ino_info **ino_i,
55 fs_index_t index)
56{
57 struct mfs_sb_info *sbi = inst->sbi;
58
59 if (sbi->fs_version == MFS_VERSION_V1) {
60 /*Read a MFS V1 inode*/
61 *ino_i = mfs_read_inode_raw(inst, index);
62 } else {
63 /*Read a MFS V2/V3 inode*/
64 *ino_i = mfs2_read_inode_raw(inst, index);
65 }
66
67 if (*ino_i == NULL)
68 return -1;
69
70 return EOK;
71}
72
73static struct mfs_ino_info *
74mfs_read_inode_raw(const struct mfs_instance *instance, uint16_t inum)
75{
76 struct mfs_inode *ino = NULL;
77 struct mfs_ino_info *ino_i = NULL;
78 struct mfs_sb_info *sbi;
79 block_t *b;
80 int i;
81
82 sbi = instance->sbi;
83 assert(sbi);
84
85 /*inode 0 does not exist*/
86 inum -= 1;
87
88 const int ino_off = inum % sbi->ino_per_block;
89 const size_t ino_size = sizeof(struct mfs_inode);
90
91 ino_i = malloc(sizeof(*ino_i));
92 ino = malloc(ino_size);
93
94 if (!ino || !ino_i)
95 goto out_err;
96
97 const int itable_off = sbi->itable_off;
98
99 if (block_get(&b, instance->handle,
100 itable_off + inum / sbi->ino_per_block,
101 BLOCK_FLAGS_NONE) != EOK)
102 goto out_err;
103
104 memcpy(ino, ((uint8_t *) b->data) + ino_off * ino_size, ino_size);
105
106 ino_i->i_mode = conv16(sbi->native, ino->i_mode);
107 ino_i->i_uid = conv16(sbi->native, ino->i_uid);
108 ino_i->i_size = conv32(sbi->native, ino->i_size);
109 ino_i->i_mtime = conv32(sbi->native, ino->i_mtime);
110 ino_i->i_nlinks = ino->i_nlinks;
111
112 for (i = 0; i < V1_NR_DIRECT_ZONES; ++i)
113 ino_i->i_dzone[i] = conv16(sbi->native, ino->i_dzone[i]);
114
115 for (i = 0; i < V1_NR_INDIRECT_ZONES; ++i)
116 ino_i->i_izone[i] = conv16(sbi->native, ino->i_izone[i]);
117
118 block_put(b);
119 free(ino);
120 ino_i->dirty = false;
121
122 return ino_i;
123
124out_err:
125 if (ino)
126 free(ino);
127 if (ino_i)
128 free(ino_i);
129 return NULL;
130}
131
132static struct mfs_ino_info *
133mfs2_read_inode_raw(const struct mfs_instance *instance, uint32_t inum)
134{
135 struct mfs2_inode *ino = NULL;
136 struct mfs_ino_info *ino_i = NULL;
137 struct mfs_sb_info *sbi;
138 block_t *b;
139 int i;
140
141 const size_t ino_size = sizeof(struct mfs2_inode);
142
143 ino = malloc(ino_size);
144 ino_i = malloc(sizeof(*ino_i));
145
146 if (!ino || !ino_i)
147 goto out_err;
148
149 sbi = instance->sbi;
150 assert(sbi);
151
152 /*inode 0 does not exist*/
153 inum -= 1;
154
155 const int itable_off = sbi->itable_off;
156 const int ino_off = inum % sbi->ino_per_block;
157
158 if (block_get(&b, instance->handle,
159 itable_off + inum / sbi->ino_per_block,
160 BLOCK_FLAGS_NONE) != EOK)
161 goto out_err;
162
163 memcpy(ino, ((uint8_t *)b->data) + ino_off * ino_size, ino_size);
164
165 ino_i->i_mode = conv16(sbi->native, ino->i_mode);
166 ino_i->i_nlinks = conv16(sbi->native, ino->i_nlinks);
167 ino_i->i_uid = conv16(sbi->native, ino->i_uid);
168 ino_i->i_gid = conv16(sbi->native, ino->i_gid);
169 ino_i->i_size = conv32(sbi->native, ino->i_size);
170 ino_i->i_atime = conv32(sbi->native, ino->i_atime);
171 ino_i->i_mtime = conv32(sbi->native, ino->i_mtime);
172 ino_i->i_ctime = conv32(sbi->native, ino->i_ctime);
173
174 for (i = 0; i < V2_NR_DIRECT_ZONES; ++i)
175 ino_i->i_dzone[i] = conv32(sbi->native, ino->i_dzone[i]);
176
177 for (i = 0; i < V2_NR_INDIRECT_ZONES; ++i)
178 ino_i->i_izone[i] = conv32(sbi->native, ino->i_izone[i]);
179
180 block_put(b);
181 free(ino);
182 ino_i->dirty = false;
183
184 return ino_i;
185
186out_err:
187 if (ino)
188 free(ino);
189 if (ino_i)
190 free(ino_i);
191 return NULL;
192}
193
194int
195put_inode(struct mfs_node *mnode)
196{
197 int rc = EOK;
198
199 assert(mnode);
200 assert(mnode->ino_i);
201
202 if (!mnode->ino_i->dirty)
203 goto out;
204
205 struct mfs_instance *inst = mnode->instance;
206 assert(inst);
207 struct mfs_sb_info *sbi = inst->sbi;
208 assert(sbi);
209
210 if (sbi->fs_version == MFS_VERSION_V1)
211 rc = mfs_write_inode_raw(mnode);
212 else
213 rc = mfs2_write_inode_raw(mnode);
214
215out:
216 return rc;
217}
218
219static int
220mfs_write_inode_raw(struct mfs_node *mnode)
221{
222 int i, r;
223 block_t *b;
224 struct mfs_ino_info *ino_i = mnode->ino_i;
225 struct mfs_sb_info *sbi = mnode->instance->sbi;
226
227 const int itable_off = sbi->itable_off;
228 const int ino_off = ino_i->index % sbi->ino_per_block;
229 const bool native = sbi->native;
230
231 r = block_get(&b, mnode->instance->handle,
232 itable_off + ino_i->index / sbi->ino_per_block,
233 BLOCK_FLAGS_NONE);
234
235 if (r != EOK)
236 goto out;
237
238 struct mfs_inode *ino = b->data;
239 ino += ino_off;
240
241 ino->i_mode = conv16(native, ino_i->i_mode);
242 ino->i_uid = conv16(native, ino_i->i_uid);
243 ino->i_gid = ino_i->i_gid;
244 ino->i_nlinks = ino_i->i_nlinks;
245 ino->i_size = conv32(native, ino_i->i_size);
246 ino->i_mtime = conv32(native, ino_i->i_mtime);
247
248 for (i = 0; i < V1_NR_DIRECT_ZONES; ++i)
249 ino->i_dzone[i] = conv16(native, ino_i->i_dzone[i]);
250 for (i = 0; i < V1_NR_INDIRECT_ZONES; ++i)
251 ino->i_izone[i] = conv16(native, ino_i->i_izone[i]);
252
253 b->dirty = true;
254 block_put(b);
255
256 ino_i->dirty = false;
257out:
258 return r;
259}
260
261static int
262mfs2_write_inode_raw(struct mfs_node *mnode)
263{
264 struct mfs_ino_info *ino_i = mnode->ino_i;
265 struct mfs_sb_info *sbi = mnode->instance->sbi;
266 block_t *b;
267 int i, r;
268
269 const int itable_off = sbi->itable_off;
270 const int ino_off = ino_i->index % sbi->ino_per_block;
271 const bool native = sbi->native;
272
273 r = block_get(&b, mnode->instance->handle,
274 itable_off + ino_i->index / sbi->ino_per_block,
275 BLOCK_FLAGS_NONE);
276
277 if (r != EOK)
278 goto out;
279
280 struct mfs2_inode *ino2 = b->data;
281 ino2 += ino_off;
282
283 ino2->i_mode = conv16(native, ino_i->i_mode);
284 ino2->i_nlinks = conv16(native, ino_i->i_mode);
285 ino2->i_uid = conv16(native, ino_i->i_uid);
286 ino2->i_gid = conv16(native, ino_i->i_gid);
287 ino2->i_size = conv32(native, ino_i->i_size);
288 ino2->i_atime = conv32(native, ino_i->i_atime);
289 ino2->i_mtime = conv32(native, ino_i->i_mtime);
290 ino2->i_ctime = conv32(native, ino_i->i_ctime);
291
292 for (i = 0; i < V2_NR_DIRECT_ZONES; ++i)
293 ino2->i_dzone[i] = conv32(native, ino_i->i_dzone[i]);
294
295 for (i = 0; i < V2_NR_INDIRECT_ZONES; ++i)
296 ino2->i_izone[i] = conv32(native, ino_i->i_izone[i]);
297
298 b->dirty = true;
299 block_put(b);
300 ino_i->dirty = false;
301
302out:
303 return r;
304}
305
306int
307inode_grow(struct mfs_node *mnode, size_t size_grow)
308{
309 unsigned i;
310
311 if (size_grow == 0)
312 return EOK;
313
314 struct mfs_sb_info *sbi = mnode->instance->sbi;
315 struct mfs_ino_info *ino_i = mnode->ino_i;
316 const int bs = sbi->block_size;
317
318 const uint32_t old_size = ino_i->i_size;
319 const uint32_t new_size = old_size + size_grow;
320
321 /*Compute the number of zones to add to the inode*/
322 unsigned zones_to_add = 0;
323 if ((old_size % (bs - 1)) == 0)
324 zones_to_add++;
325
326 zones_to_add += (new_size - old_size) / bs;
327
328 /*Compute the start zone*/
329 unsigned start_zone = old_size / bs;
330 start_zone += (old_size % bs) != 0;
331
332 int r;
333 for (i = 0; i < zones_to_add; ++i) {
334 uint32_t new_zone;
335 uint32_t dummy;
336
337 r = mfs_alloc_bit(mnode->instance, &new_zone, BMAP_ZONE);
338 if (r != EOK)
339 return r;
340
341 r = write_map(mnode, (start_zone + i) * sbi->block_size,
342 new_zone, &dummy);
343 if (r != EOK)
344 return r;
345
346 ino_i->i_size += bs;
347 ino_i->dirty = true;
348 }
349 return EOK;
350}
351
352/**
353 * @}
354 */
355
Note: See TracBrowser for help on using the repository browser.