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

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

cstyle

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