source: mainline/uspace/srv/fs/mfs/mfs_inode.c@ dcc44ca1

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

remove a lot of useless asserts

  • Property mode set to 100644
File size: 9.1 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 "mfs.h"
35
36static int
37mfs_write_inode_raw(struct mfs_node *mnode);
38
39static int
40mfs2_write_inode_raw(struct mfs_node *mnode);
41
42static int
43mfs_read_inode_raw(const struct mfs_instance *instance,
44 struct mfs_ino_info **ino_ptr, uint16_t inum);
45
46static int
47mfs2_read_inode_raw(const struct mfs_instance *instance,
48 struct mfs_ino_info **ino_ptr, uint32_t inum);
49
50/**Read a MINIX inode from disk
51 *
52 * @param inst Pointer to the filesystem instance.
53 * @param ino_i Pointer to the generic MINIX inode
54 * where the inode content will be stored.
55 * @param index index of the inode to read.
56 *
57 * @return EOK on success or a negative error code.
58 */
59int
60mfs_get_inode(struct mfs_instance *inst, struct mfs_ino_info **ino_i,
61 fs_index_t index)
62{
63 struct mfs_sb_info *sbi = inst->sbi;
64 int r;
65
66 if (sbi->fs_version == MFS_VERSION_V1) {
67 /*Read a MFS V1 inode*/
68 r = mfs_read_inode_raw(inst, ino_i, index);
69 } else {
70 /*Read a MFS V2/V3 inode*/
71 r = mfs2_read_inode_raw(inst, ino_i, index);
72 }
73
74 return r;
75}
76
77static int
78mfs_read_inode_raw(const struct mfs_instance *instance,
79 struct mfs_ino_info **ino_ptr, uint16_t inum) {
80 struct mfs_inode *ino;
81 struct mfs_ino_info *ino_i = NULL;
82 struct mfs_sb_info *sbi;
83 block_t *b;
84 int i, r;
85
86 sbi = instance->sbi;
87
88 /*inode 0 does not exist*/
89 inum -= 1;
90
91 const int ino_off = inum % sbi->ino_per_block;
92
93 ino_i = malloc(sizeof(*ino_i));
94
95 if (!ino_i) {
96 r = ENOMEM;
97 goto out_err;
98 }
99
100 const int itable_off = sbi->itable_off;
101
102 r = block_get(&b, instance->service_id,
103 itable_off + inum / sbi->ino_per_block,
104 BLOCK_FLAGS_NONE);
105 if (r != EOK)
106 goto out_err;
107
108 ino = b->data + ino_off * sizeof(struct mfs_inode);
109
110 ino_i->i_mode = conv16(sbi->native, ino->i_mode);
111 ino_i->i_uid = conv16(sbi->native, ino->i_uid);
112 ino_i->i_size = conv32(sbi->native, ino->i_size);
113 ino_i->i_mtime = conv32(sbi->native, ino->i_mtime);
114 ino_i->i_nlinks = ino->i_nlinks;
115
116 for (i = 0; i < V1_NR_DIRECT_ZONES; ++i)
117 ino_i->i_dzone[i] = conv16(sbi->native, ino->i_dzone[i]);
118
119 for (i = 0; i < V1_NR_INDIRECT_ZONES; ++i)
120 ino_i->i_izone[i] = conv16(sbi->native, ino->i_izone[i]);
121
122 r = block_put(b);
123 ino_i->dirty = false;
124 *ino_ptr = ino_i;
125
126 return r;
127
128out_err:
129 if (ino_i)
130 free(ino_i);
131 return EOK;
132}
133
134static int
135mfs2_read_inode_raw(const struct mfs_instance *instance,
136 struct mfs_ino_info **ino_ptr, uint32_t inum) {
137 struct mfs2_inode *ino;
138 struct mfs_ino_info *ino_i = NULL;
139 struct mfs_sb_info *sbi;
140 block_t *b;
141 int i, r;
142
143 ino_i = malloc(sizeof(*ino_i));
144
145 if (!ino_i) {
146 r = ENOMEM;
147 goto out_err;
148 }
149
150 sbi = instance->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 r = block_get(&b, instance->service_id,
159 itable_off + inum / sbi->ino_per_block,
160 BLOCK_FLAGS_NONE);
161 if (r != EOK)
162 goto out_err;
163
164 ino = b->data + ino_off * sizeof(struct mfs2_inode);
165
166 ino_i->i_mode = conv16(sbi->native, ino->i_mode);
167 ino_i->i_nlinks = conv16(sbi->native, ino->i_nlinks);
168 ino_i->i_uid = conv16(sbi->native, ino->i_uid);
169 ino_i->i_gid = conv16(sbi->native, ino->i_gid);
170 ino_i->i_size = conv32(sbi->native, ino->i_size);
171 ino_i->i_atime = conv32(sbi->native, ino->i_atime);
172 ino_i->i_mtime = conv32(sbi->native, ino->i_mtime);
173 ino_i->i_ctime = conv32(sbi->native, ino->i_ctime);
174
175 for (i = 0; i < V2_NR_DIRECT_ZONES; ++i)
176 ino_i->i_dzone[i] = conv32(sbi->native, ino->i_dzone[i]);
177
178 for (i = 0; i < V2_NR_INDIRECT_ZONES; ++i)
179 ino_i->i_izone[i] = conv32(sbi->native, ino->i_izone[i]);
180
181 r = block_put(b);
182 ino_i->dirty = false;
183 *ino_ptr = ino_i;
184
185 return r;
186
187out_err:
188 if (ino_i)
189 free(ino_i);
190 return EOK;
191}
192
193/**Write a MINIX inode on disk (if marked as dirty)
194 *
195 * @param mnode Pointer to the generic MINIX inode in memory.
196 *
197 * @return EOK on success or a negative error code.
198 */
199int
200mfs_put_inode(struct mfs_node *mnode)
201{
202 int rc = EOK;
203
204 if (!mnode->ino_i->dirty)
205 goto out;
206
207 struct mfs_instance *inst = mnode->instance;
208 struct mfs_sb_info *sbi = inst->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 uint32_t inum = ino_i->index - 1;
228 const int itable_off = sbi->itable_off;
229 const int ino_off = inum % sbi->ino_per_block;
230 const bool native = sbi->native;
231
232 r = block_get(&b, mnode->instance->service_id,
233 itable_off + inum / sbi->ino_per_block,
234 BLOCK_FLAGS_NONE);
235
236 if (r != EOK)
237 goto out;
238
239 struct mfs_inode *ino = b->data;
240 ino += ino_off;
241
242 ino->i_mode = conv16(native, ino_i->i_mode);
243 ino->i_uid = conv16(native, ino_i->i_uid);
244 ino->i_gid = ino_i->i_gid;
245 ino->i_nlinks = ino_i->i_nlinks;
246 ino->i_size = conv32(native, ino_i->i_size);
247 ino->i_mtime = conv32(native, ino_i->i_mtime);
248
249 for (i = 0; i < V1_NR_DIRECT_ZONES; ++i)
250 ino->i_dzone[i] = conv16(native, ino_i->i_dzone[i]);
251 for (i = 0; i < V1_NR_INDIRECT_ZONES; ++i)
252 ino->i_izone[i] = conv16(native, ino_i->i_izone[i]);
253
254 b->dirty = true;
255 r = block_put(b);
256
257 ino_i->dirty = false;
258out:
259 return r;
260}
261
262static int
263mfs2_write_inode_raw(struct mfs_node *mnode)
264{
265 struct mfs_ino_info *ino_i = mnode->ino_i;
266 struct mfs_sb_info *sbi = mnode->instance->sbi;
267 block_t *b;
268 int i, r;
269
270 const uint32_t inum = ino_i->index - 1;
271 const int itable_off = sbi->itable_off;
272 const int ino_off = inum % sbi->ino_per_block;
273 const bool native = sbi->native;
274
275 r = block_get(&b, mnode->instance->service_id,
276 itable_off + inum / sbi->ino_per_block,
277 BLOCK_FLAGS_NONE);
278
279 if (r != EOK)
280 goto out;
281
282 struct mfs2_inode *ino2 = b->data;
283 ino2 += ino_off;
284
285 ino2->i_mode = conv16(native, ino_i->i_mode);
286 ino2->i_nlinks = conv16(native, ino_i->i_nlinks);
287 ino2->i_uid = conv16(native, ino_i->i_uid);
288 ino2->i_gid = conv16(native, ino_i->i_gid);
289 ino2->i_size = conv32(native, ino_i->i_size);
290 ino2->i_atime = conv32(native, ino_i->i_atime);
291 ino2->i_mtime = conv32(native, ino_i->i_mtime);
292 ino2->i_ctime = conv32(native, ino_i->i_ctime);
293
294 for (i = 0; i < V2_NR_DIRECT_ZONES; ++i)
295 ino2->i_dzone[i] = conv32(native, ino_i->i_dzone[i]);
296
297 for (i = 0; i < V2_NR_INDIRECT_ZONES; ++i)
298 ino2->i_izone[i] = conv32(native, ino_i->i_izone[i]);
299
300 b->dirty = true;
301 r = block_put(b);
302 ino_i->dirty = false;
303
304out:
305 return r;
306}
307
308/**Reduce the inode size of a given number of bytes
309 *
310 * @param mnode Pointer to the generic MINIX inode in memory.
311 * @param size_shrink Number of bytes that will be subtracted to the inode.
312 *
313 * @return EOK on success or a negative error code.
314 */
315int
316mfs_inode_shrink(struct mfs_node *mnode, size_t size_shrink)
317{
318 struct mfs_sb_info *sbi = mnode->instance->sbi;
319 struct mfs_ino_info *ino_i = mnode->ino_i;
320 const size_t bs = sbi->block_size;
321 int r;
322
323 if (size_shrink == 0) {
324 /*File is empty*/
325 return EOK;
326 }
327
328 const size_t old_size = ino_i->i_size;
329 const size_t new_size = ino_i->i_size - size_shrink;
330
331 assert(size_shrink <= old_size);
332
333 ino_i->dirty = true;
334
335 /*Compute the number of zones to free*/
336 unsigned zones_to_free;
337
338 size_t diff = old_size - new_size;
339 zones_to_free = diff / bs;
340
341 if (diff % bs != 0)
342 zones_to_free++;
343
344 uint32_t pos = old_size - 1;
345 unsigned i;
346 for (i = 0; i < zones_to_free; ++i, pos -= bs) {
347 uint32_t old_zone;
348
349 r = mfs_write_map(mnode, pos, 0, &old_zone);
350 if (r != EOK)
351 goto exit_error;
352
353 ino_i->i_size -= bs;
354
355 if (old_zone == 0)
356 continue; /*Sparse block*/
357
358 r = mfs_free_zone(mnode->instance, old_zone);
359 if (r != EOK)
360 goto exit_error;
361 }
362
363 ino_i->i_size = new_size;
364
365 return mfs_prune_ind_zones(mnode, new_size);
366
367exit_error:
368 return r;
369}
370
371/**
372 * @}
373 */
374
Note: See TracBrowser for help on using the repository browser.