source: mainline/uspace/srv/fs/minixfs/mfs_inode.c@ 9530d94

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9530d94 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
RevLine 
[6adba0a8]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
[f213ae7]33#include <stdlib.h>
34#include "mfs.h"
35
[10eb754]36static int
37mfs_write_inode_raw(struct mfs_node *mnode);
38
[cdab59e]39static int
40mfs2_write_inode_raw(struct mfs_node *mnode);
41
[4bf0052a]42static int
43mfs_read_inode_raw(const struct mfs_instance *instance,
44 struct mfs_ino_info **ino_ptr, uint16_t inum);
[c922bc7]45
[4bf0052a]46static int
47mfs2_read_inode_raw(const struct mfs_instance *instance,
48 struct mfs_ino_info **ino_ptr, uint32_t inum);
[c922bc7]49
50
51int
52get_inode(struct mfs_instance *inst, struct mfs_ino_info **ino_i,
[44c6091f]53 fs_index_t index)
[c922bc7]54{
55 struct mfs_sb_info *sbi = inst->sbi;
[4bf0052a]56 int r;
[c922bc7]57
58 if (sbi->fs_version == MFS_VERSION_V1) {
59 /*Read a MFS V1 inode*/
[4bf0052a]60 r = mfs_read_inode_raw(inst, ino_i, index);
[c922bc7]61 } else {
62 /*Read a MFS V2/V3 inode*/
[4bf0052a]63 r = mfs2_read_inode_raw(inst, ino_i, index);
[c922bc7]64 }
65
[4bf0052a]66 return r;
[c922bc7]67}
68
[4bf0052a]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;
[155f792]73 struct mfs_ino_info *ino_i = NULL;
[f213ae7]74 struct mfs_sb_info *sbi;
75 block_t *b;
[4bf0052a]76 int i, r;
[10eb754]77
78 sbi = instance->sbi;
79 assert(sbi);
[e33100c]80
81 /*inode 0 does not exist*/
82 inum -= 1;
83
[10eb754]84 const int ino_off = inum % sbi->ino_per_block;
[f213ae7]85
[b438804]86 ino_i = malloc(sizeof(*ino_i));
[f213ae7]87
[4bf0052a]88 if (!ino_i) {
89 r = ENOMEM;
[155f792]90 goto out_err;
[4bf0052a]91 }
[f213ae7]92
[10eb754]93 const int itable_off = sbi->itable_off;
[8b86ed26]94
[4bf0052a]95 r = block_get(&b, instance->handle,
[44c6091f]96 itable_off + inum / sbi->ino_per_block,
[4bf0052a]97 BLOCK_FLAGS_NONE);
98 on_error(r, goto out_err);
[f213ae7]99
[4bf0052a]100 ino = b->data + ino_off * sizeof(struct mfs_inode);
[f213ae7]101
[155f792]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);
[7cb975e]106 ino_i->i_nlinks = ino->i_nlinks;
[f213ae7]107
108 for (i = 0; i < V1_NR_DIRECT_ZONES; ++i)
[155f792]109 ino_i->i_dzone[i] = conv16(sbi->native, ino->i_dzone[i]);
[f213ae7]110
111 for (i = 0; i < V1_NR_INDIRECT_ZONES; ++i)
[155f792]112 ino_i->i_izone[i] = conv16(sbi->native, ino->i_izone[i]);
[f213ae7]113
[4bf0052a]114 r = block_put(b);
[54caa41b]115 ino_i->dirty = false;
[4bf0052a]116 *ino_ptr = ino_i;
[54caa41b]117
[4bf0052a]118 return r;
[155f792]119
120out_err:
121 if (ino_i)
122 free(ino_i);
[4bf0052a]123 return EOK;
[f213ae7]124}
125
[4bf0052a]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;
[155f792]130 struct mfs_ino_info *ino_i = NULL;
[df22c36]131 struct mfs_sb_info *sbi;
132 block_t *b;
[4bf0052a]133 int i, r;
[df22c36]134
[b438804]135 ino_i = malloc(sizeof(*ino_i));
[df22c36]136
[4bf0052a]137 if (!ino_i) {
138 r = ENOMEM;
[155f792]139 goto out_err;
[4bf0052a]140 }
[df22c36]141
142 sbi = instance->sbi;
[8b86ed26]143 assert(sbi);
[df22c36]144
[e33100c]145 /*inode 0 does not exist*/
146 inum -= 1;
147
[10eb754]148 const int itable_off = sbi->itable_off;
149 const int ino_off = inum % sbi->ino_per_block;
[cfff7a8f]150
[4bf0052a]151 r = block_get(&b, instance->handle,
[44c6091f]152 itable_off + inum / sbi->ino_per_block,
[4bf0052a]153 BLOCK_FLAGS_NONE);
154 on_error(r, goto out_err);
[df22c36]155
[4bf0052a]156 ino = b->data + ino_off * sizeof(struct mfs2_inode);
[df22c36]157
[155f792]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);
[df22c36]166
167 for (i = 0; i < V2_NR_DIRECT_ZONES; ++i)
[155f792]168 ino_i->i_dzone[i] = conv32(sbi->native, ino->i_dzone[i]);
[df22c36]169
170 for (i = 0; i < V2_NR_INDIRECT_ZONES; ++i)
[155f792]171 ino_i->i_izone[i] = conv32(sbi->native, ino->i_izone[i]);
[df22c36]172
[4bf0052a]173 r = block_put(b);
[54caa41b]174 ino_i->dirty = false;
[4bf0052a]175 *ino_ptr = ino_i;
[54caa41b]176
[4bf0052a]177 return r;
[155f792]178
179out_err:
180 if (ino_i)
181 free(ino_i);
[4bf0052a]182 return EOK;
[df22c36]183}
184
[10eb754]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);
[cdab59e]203 else
204 rc = mfs2_write_inode_raw(mnode);
[10eb754]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
[3ab580a]218 const uint32_t inum = ino_i->index - 1;
[10eb754]219 const int itable_off = sbi->itable_off;
[3ab580a]220 const int ino_off = inum % sbi->ino_per_block;
[10eb754]221 const bool native = sbi->native;
222
223 r = block_get(&b, mnode->instance->handle,
[44c6091f]224 itable_off + inum / sbi->ino_per_block,
225 BLOCK_FLAGS_NONE);
[10eb754]226
[cfac897]227 on_error(r, goto out);
[10eb754]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;
[9530d94]245 r = block_put(b);
[10eb754]246
247 ino_i->dirty = false;
248out:
249 return r;
250}
251
[cdab59e]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
[3ab580a]260 const uint32_t inum = ino_i->index - 1;
[cdab59e]261 const int itable_off = sbi->itable_off;
[3ab580a]262 const int ino_off = inum % sbi->ino_per_block;
[cdab59e]263 const bool native = sbi->native;
[44c6091f]264
[cdab59e]265 r = block_get(&b, mnode->instance->handle,
[44c6091f]266 itable_off + inum / sbi->ino_per_block,
267 BLOCK_FLAGS_NONE);
[cdab59e]268
[cfac897]269 on_error(r, goto out);
[cdab59e]270
271 struct mfs2_inode *ino2 = b->data;
272 ino2 += ino_off;
273
274 ino2->i_mode = conv16(native, ino_i->i_mode);
[c2fcfc0]275 ino2->i_nlinks = conv16(native, ino_i->i_nlinks);
[cdab59e]276 ino2->i_uid = conv16(native, ino_i->i_uid);
277 ino2->i_gid = conv16(native, ino_i->i_gid);
[44c6091f]278 ino2->i_size = conv32(native, ino_i->i_size);
[cdab59e]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;
[9530d94]290 r = block_put(b);
[cdab59e]291 ino_i->dirty = false;
292
293out:
294 return r;
295}
296
[8a49fed]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
[44799a0]305 if (size_shrink == 0) {
306 /*File is empty*/
307 return EOK;
308 }
[8a49fed]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*/
[38b7233]318 unsigned zones_to_free;
[8a49fed]319
[38b7233]320 size_t diff = old_size - new_size;
321 zones_to_free = diff / bs;
[8a49fed]322
[38b7233]323 if (diff % bs != 0)
324 zones_to_free++;
[8a49fed]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
[70ac0af]339 r = mfs_free_zone(mnode->instance, old_zone);
[8a49fed]340 on_error(r, goto exit_error);
341 }
342
343 ino_i->i_size = new_size;
[1878386]344
345 return prune_ind_zones(mnode, new_size);
[8a49fed]346
347exit_error:
348 return r;
349}
350
[6adba0a8]351/**
352 * @}
[44c6091f]353 */
[6adba0a8]354
Note: See TracBrowser for help on using the repository browser.