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