source: mainline/uspace/lib/ext4/libext4_inode.c@ 38542dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 38542dc was 38542dc, checked in by Martin Decky <martin@…>, 13 years ago

ext4 code review and coding style cleanup

  • Property mode set to 100644
File size: 14.3 KB
RevLine 
[eb91db7]1/*
[f22d5ef0]2 * Copyright (c) 2012 Frantisek Princ
[eb91db7]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 libext4
30 * @{
[38542dc]31 */
[eb91db7]32/**
[38542dc]33 * @file libext4_inode.c
34 * @brief Ext4 i-node structure operations.
[eb91db7]35 */
36
[9c0c0e1]37#include <byteorder.h>
[1a7756a]38#include <errno.h>
39#include <libblock.h>
[3711e7e]40#include "libext4.h"
[6c501f8]41
[296ef5d6]42/** Compute number of bits for block count.
43 *
[38542dc]44 * @param block_size Filesystem block_size
45 *
46 * @return Number of bits
47 *
[296ef5d6]48 */
[b3d7277]49static uint32_t ext4_inode_block_bits_count(uint32_t block_size)
50{
51 uint32_t bits = 8;
52 uint32_t size = block_size;
[38542dc]53
[b3d7277]54 do {
55 bits++;
56 size = size >> 1;
57 } while (size > 256);
[38542dc]58
[b3d7277]59 return bits;
60}
61
[296ef5d6]62/** Get mode of the i-node.
63 *
[38542dc]64 * @param sb Superblock
65 * @param inode I-node to load mode from
66 *
67 * @return Mode of the i-node
68 *
[296ef5d6]69 */
[9b9d37bb]70uint32_t ext4_inode_get_mode(ext4_superblock_t *sb, ext4_inode_t *inode)
71{
72 if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD) {
[38542dc]73 return ((uint32_t) uint16_t_le2host(inode->osd2.hurd2.mode_high)) << 16 |
74 ((uint32_t) uint16_t_le2host(inode->mode));
[9b9d37bb]75 }
[38542dc]76
[9b9d37bb]77 return uint16_t_le2host(inode->mode);
78}
79
[296ef5d6]80/** Set mode of the i-node.
81 *
[38542dc]82 * @param sb Superblock
83 * @param inode I-node to set mode to
84 * @param mode Mode to set to i-node
85 *
[296ef5d6]86 */
[fe27eb4]87void ext4_inode_set_mode(ext4_superblock_t *sb, ext4_inode_t *inode, uint32_t mode)
[9b9d37bb]88{
[fe27eb4]89 inode->mode = host2uint16_t_le((mode << 16) >> 16);
[38542dc]90
91 if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD)
[fe27eb4]92 inode->osd2.hurd2.mode_high = host2uint16_t_le(mode >> 16);
[9b9d37bb]93}
94
[296ef5d6]95/** Get ID of the i-node owner (user id).
96 *
[38542dc]97 * @param inode I-node to load uid from
98 *
99 * @return User ID of the i-node owner
100 *
[296ef5d6]101 */
[3712434]102uint32_t ext4_inode_get_uid(ext4_inode_t *inode)
[fe27eb4]103{
104 return uint32_t_le2host(inode->uid);
105}
106
[296ef5d6]107/** Set ID of the i-node owner.
108 *
[38542dc]109 * @param inode I-node to set uid to
110 * @param uid ID of the i-node owner
111 *
[296ef5d6]112 */
[fe27eb4]113void ext4_inode_set_uid(ext4_inode_t *inode, uint32_t uid)
114{
115 inode->uid = host2uint32_t_le(uid);
116}
[3712434]117
[296ef5d6]118/** Get real i-node size.
119 *
[38542dc]120 * @param sb Superblock
121 * @param inode I-node to load size from
122 *
123 * @return Real size of i-node
124 *
[296ef5d6]125 */
[9b9d37bb]126uint64_t ext4_inode_get_size(ext4_superblock_t *sb, ext4_inode_t *inode)
[3712434]127{
[9b9d37bb]128 uint32_t major_rev = ext4_superblock_get_rev_level(sb);
[38542dc]129
130 if ((major_rev > 0) &&
131 (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)))
[9b9d37bb]132 return ((uint64_t)uint32_t_le2host(inode->size_hi)) << 32 |
[38542dc]133 ((uint64_t)uint32_t_le2host(inode->size_lo));
134
[9b9d37bb]135 return uint32_t_le2host(inode->size_lo);
[3712434]136}
137
[296ef5d6]138/** Set real i-node size.
139 *
[38542dc]140 * @param inode I-node to set size to
141 * @param size Size of the i-node
142 *
[296ef5d6]143 */
[38542dc]144void ext4_inode_set_size(ext4_inode_t *inode, uint64_t size)
145{
[296ef5d6]146 inode->size_lo = host2uint32_t_le((size << 32) >> 32);
147 inode->size_hi = host2uint32_t_le(size >> 32);
[052e82d]148}
149
[2add9ec]150/** Get time, when i-node was last accessed.
151 *
[38542dc]152 * @param inode I-node
153 *
154 * @return Time of the last access (POSIX)
155 *
[2add9ec]156 */
[fe27eb4]157uint32_t ext4_inode_get_access_time(ext4_inode_t *inode)
158{
159 return uint32_t_le2host(inode->access_time);
160}
161
[2add9ec]162/** Set time, when i-node was last accessed.
[296ef5d6]163 *
[38542dc]164 * @param inode I-node
165 * @param time Time of the last access (POSIX)
166 *
[296ef5d6]167 */
[fe27eb4]168void ext4_inode_set_access_time(ext4_inode_t *inode, uint32_t time)
169{
170 inode->access_time = host2uint32_t_le(time);
171}
172
[da9f220d]173/** Get time, when i-node was last changed.
[296ef5d6]174 *
[38542dc]175 * @param inode I-node
176 *
177 * @return Time of the last change (POSIX)
178 *
[296ef5d6]179 */
[fe27eb4]180uint32_t ext4_inode_get_change_inode_time(ext4_inode_t *inode)
181{
182 return uint32_t_le2host(inode->change_inode_time);
183}
184
[da9f220d]185/** Set time, when i-node was last changed.
[296ef5d6]186 *
[38542dc]187 * @param inode I-node
188 * @param time Time of the last change (POSIX)
189 *
[296ef5d6]190 */
[fe27eb4]191void ext4_inode_set_change_inode_time(ext4_inode_t *inode, uint32_t time)
192{
193 inode->change_inode_time = host2uint32_t_le(time);
194}
195
[da9f220d]196/** Get time, when i-node content was last modified.
[296ef5d6]197 *
[38542dc]198 * @param inode I-node
199 *
200 * @return Time of the last content modification (POSIX)
201 *
[296ef5d6]202 */
[fe27eb4]203uint32_t ext4_inode_get_modification_time(ext4_inode_t *inode)
204{
205 return uint32_t_le2host(inode->modification_time);
206}
207
[da9f220d]208/** Set time, when i-node content was last modified.
[296ef5d6]209 *
[38542dc]210 * @param inode I-node
211 * @param time Time of the last content modification (POSIX)
212 *
[296ef5d6]213 */
[fe27eb4]214void ext4_inode_set_modification_time(ext4_inode_t *inode, uint32_t time)
215{
216 inode->modification_time = host2uint32_t_le(time);
217}
218
[5b26747]219/** Get time, when i-node was deleted.
[296ef5d6]220 *
[38542dc]221 * @param inode I-node
222 *
223 * @return Time of the delete action (POSIX)
224 *
[296ef5d6]225 */
[fe27eb4]226uint32_t ext4_inode_get_deletion_time(ext4_inode_t *inode)
227{
228 return uint32_t_le2host(inode->deletion_time);
229}
230
[5b26747]231/** Set time, when i-node was deleted.
[296ef5d6]232 *
[38542dc]233 * @param inode I-node
234 * @param time Time of the delete action (POSIX)
235 *
[296ef5d6]236 */
[fe27eb4]237void ext4_inode_set_deletion_time(ext4_inode_t *inode, uint32_t time)
238{
239 inode->deletion_time = host2uint32_t_le(time);
240}
241
[296ef5d6]242/** Get ID of the i-node owner's group.
243 *
[38542dc]244 * @param inode I-node to load gid from
245 *
246 * @return Group ID of the i-node owner
247 *
[296ef5d6]248 */
[fe27eb4]249uint32_t ext4_inode_get_gid(ext4_inode_t *inode)
250{
251 return uint32_t_le2host(inode->gid);
252}
253
[296ef5d6]254/** Set ID ot the i-node owner's group.
255 *
[38542dc]256 * @param inode I-node to set gid to
257 * @param gid Group ID of the i-node owner
258 *
[296ef5d6]259 */
[fe27eb4]260void ext4_inode_set_gid(ext4_inode_t *inode, uint32_t gid)
261{
262 inode->gid = host2uint32_t_le(gid);
263}
[3712434]264
[296ef5d6]265/** Get number of links to i-node.
266 *
[38542dc]267 * @param inode I-node to load number of links from
268 *
269 * @return Number of links to i-node
270 *
[296ef5d6]271 */
[3712434]272uint16_t ext4_inode_get_links_count(ext4_inode_t *inode)
[6c501f8]273{
[9c0c0e1]274 return uint16_t_le2host(inode->links_count);
[6c501f8]275}
276
[296ef5d6]277/** Set number of links to i-node.
278 *
[38542dc]279 * @param inode I-node to set number of links to
280 * @param count Number of links to i-node
281 *
[296ef5d6]282 */
[fe27eb4]283void ext4_inode_set_links_count(ext4_inode_t *inode, uint16_t count)
284{
285 inode->links_count = host2uint16_t_le(count);
286}
[b3d7277]287
[2add9ec]288/** Get number of 512-bytes blocks used for i-node.
[296ef5d6]289 *
[38542dc]290 * @param sb Superblock
291 * @param inode I-node
292 *
293 * @return Number of 512-bytes blocks
294 *
[296ef5d6]295 */
[b3d7277]296uint64_t ext4_inode_get_blocks_count(ext4_superblock_t *sb, ext4_inode_t *inode)
297{
[38542dc]298 if (ext4_superblock_has_feature_read_only(sb,
299 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
[06d85e5]300 /* 48-bit field */
[38542dc]301 uint64_t count = ((uint64_t)
302 uint16_t_le2host(inode->osd2.linux2.blocks_high)) << 32 |
303 uint32_t_le2host(inode->blocks_count_lo);
304
[b3d7277]305 if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
[38542dc]306 uint32_t block_size = ext4_superblock_get_block_size(sb);
307 uint32_t block_bits = ext4_inode_block_bits_count(block_size);
308 return count << (block_bits - 9);
309 } else
[b3d7277]310 return count;
[38542dc]311 } else
[b3d7277]312 return uint32_t_le2host(inode->blocks_count_lo);
313}
314
[2add9ec]315/** Set number of 512-bytes blocks used for i-node.
[296ef5d6]316 *
[38542dc]317 * @param sb Superblock
318 * @param inode I-node
319 * @param count Number of 512-bytes blocks
320 *
321 * @return Error code
322 *
[296ef5d6]323 */
[b3d7277]324int ext4_inode_set_blocks_count(ext4_superblock_t *sb, ext4_inode_t *inode,
[38542dc]325 uint64_t count)
326{
327 /* 32-bit maximum */
328 uint64_t max = 0;
329 max = ~max >> 32;
330
331 if (count <= max) {
332 inode->blocks_count_lo = host2uint32_t_le(count);
333 inode->osd2.linux2.blocks_high = 0;
334 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
335
336 return EOK;
337 }
338
339 /* Check if there can be used huge files (many blocks) */
340 if (!ext4_superblock_has_feature_read_only(sb,
341 EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
342 return EINVAL;
343
344 /* 48-bit maximum */
345 max = 0;
346 max = ~max >> 16;
347
348 if (count <= max) {
349 inode->blocks_count_lo = host2uint32_t_le(count);
350 inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
351 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
352 } else {
353 uint32_t block_size = ext4_superblock_get_block_size(sb);
354 uint32_t block_bits = ext4_inode_block_bits_count(block_size);
355 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
356 count = count >> (block_bits - 9);
357 inode->blocks_count_lo = host2uint32_t_le(count);
358 inode->osd2.linux2.blocks_high = host2uint16_t_le(count >> 32);
359 }
360
361 return EOK;
[b3d7277]362}
[eb91db7]363
[296ef5d6]364/** Get flags (features) of i-node.
[38542dc]365 *
366 * @param inode I-node to get flags from
367 *
368 * @return Flags (bitmap)
369 *
[296ef5d6]370 */
[38542dc]371uint32_t ext4_inode_get_flags(ext4_inode_t *inode)
372{
[7b9381b]373 return uint32_t_le2host(inode->flags);
374}
375
[296ef5d6]376/** Set flags (features) of i-node.
377 *
[38542dc]378 * @param inode I-node to set flags to
379 * @param flags Flags to set to i-node
380 *
[296ef5d6]381 */
[38542dc]382void ext4_inode_set_flags(ext4_inode_t *inode, uint32_t flags)
383{
[b3d7277]384 inode->flags = host2uint32_t_le(flags);
385}
386
[2add9ec]387/** Get file generation (used by NFS).
[296ef5d6]388 *
[38542dc]389 * @param inode I-node
390 *
391 * @return File generation
392 *
[296ef5d6]393 */
[fe27eb4]394uint32_t ext4_inode_get_generation(ext4_inode_t *inode)
395{
396 return uint32_t_le2host(inode->generation);
397}
398
[2add9ec]399/** Set file generation (used by NFS).
[296ef5d6]400 *
[38542dc]401 * @param inode I-node
402 * @param generation File generation
403 *
[296ef5d6]404 */
[fe27eb4]405void ext4_inode_set_generation(ext4_inode_t *inode, uint32_t generation)
406{
407 inode->generation = host2uint32_t_le(generation);
408}
409
[2add9ec]410/** Get address of block, where are extended attributes located.
[296ef5d6]411 *
[38542dc]412 * @param inode I-node
413 * @param sb Superblock
414 *
415 * @return Block address
416 *
[296ef5d6]417 */
[b191acae]418uint64_t ext4_inode_get_file_acl(ext4_inode_t *inode, ext4_superblock_t *sb)
[343ccfd]419{
[38542dc]420 if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_LINUX)
421 return ((uint32_t)
422 uint16_t_le2host(inode->osd2.linux2.file_acl_high)) << 16 |
[343ccfd]423 (uint32_t_le2host(inode->file_acl_lo));
[38542dc]424
[343ccfd]425 return uint32_t_le2host(inode->file_acl_lo);
426}
427
[2add9ec]428/** Set address of block, where are extended attributes located.
[296ef5d6]429 *
[38542dc]430 * @param inode I-node
431 * @param sb Superblock
432 * @param file_acl Block address
433 *
[296ef5d6]434 */
[b191acae]435void ext4_inode_set_file_acl(ext4_inode_t *inode, ext4_superblock_t *sb,
[38542dc]436 uint64_t file_acl)
[343ccfd]437{
438 inode->file_acl_lo = host2uint32_t_le((file_acl << 32) >> 32);
[38542dc]439
440 if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_LINUX)
[343ccfd]441 inode->osd2.linux2.file_acl_high = host2uint16_t_le(file_acl >> 32);
442}
443
[296ef5d6]444/** Get block address of specified direct block.
445 *
[38542dc]446 * @param inode I-node to load block from
447 * @param idx Index of logical block
448 *
449 * @return Physical block address
450 *
[296ef5d6]451 */
[1e65444]452uint32_t ext4_inode_get_direct_block(ext4_inode_t *inode, uint32_t idx)
[9b9d37bb]453{
454 assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
[38542dc]455
[9b9d37bb]456 return uint32_t_le2host(inode->blocks[idx]);
457}
458
[296ef5d6]459/** Set block address of specified direct block.
460 *
[38542dc]461 * @param inode I-node to set block address to
462 * @param idx Index of logical block
463 * @param fblock Physical block address
464 *
[296ef5d6]465 */
[1e65444]466void ext4_inode_set_direct_block(ext4_inode_t *inode, uint32_t idx, uint32_t fblock)
[d5a78e28]467{
468 assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
[38542dc]469
[d5a78e28]470 inode->blocks[idx] = host2uint32_t_le(fblock);
471}
472
[296ef5d6]473/** Get block address of specified indirect block.
474 *
[38542dc]475 * @param inode I-node to get block address from
476 * @param idx Index of indirect block
477 *
478 * @return Physical block address
479 *
[296ef5d6]480 */
[1e65444]481uint32_t ext4_inode_get_indirect_block(ext4_inode_t *inode, uint32_t idx)
[9b9d37bb]482{
483 return uint32_t_le2host(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
484}
485
[296ef5d6]486/** Set block address of specified indirect block.
487 *
[38542dc]488 * @param inode I-node to set block address to
489 * @param idx Index of indirect block
490 * @param fblock Physical block address
491 *
[296ef5d6]492 */
[38542dc]493void ext4_inode_set_indirect_block(ext4_inode_t *inode, uint32_t idx,
494 uint32_t fblock)
[1e65444]495{
[38542dc]496 inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] =
497 host2uint32_t_le(fblock);
[1e65444]498}
499
[296ef5d6]500/** Check if i-node has specified type.
501 *
[38542dc]502 * @param sb Superblock
503 * @param inode I-node to check type of
504 * @param type Type to check
505 *
506 * @return Result of check operation
507 *
[296ef5d6]508 */
[38542dc]509bool ext4_inode_is_type(ext4_superblock_t *sb, ext4_inode_t *inode,
510 uint32_t type)
[fe27eb4]511{
512 uint32_t mode = ext4_inode_get_mode(sb, inode);
513 return (mode & EXT4_INODE_MODE_TYPE_MASK) == type;
514}
515
[296ef5d6]516/** Get extent header from the root of the extent tree.
517 *
[38542dc]518 * @param inode I-node to get extent header from
519 *
520 * @return Pointer to extent header of the root node
521 *
[296ef5d6]522 */
[acd869e]523ext4_extent_header_t * ext4_inode_get_extent_header(ext4_inode_t *inode)
524{
[38542dc]525 return (ext4_extent_header_t *) inode->blocks;
[acd869e]526}
[7b9381b]527
[296ef5d6]528/** Check if i-node has specified flag.
529 *
[38542dc]530 * @param inode I-node to check flags of
531 * @param flag Flag to check
532 *
533 * @return Result of check operation
534 *
[296ef5d6]535 */
[7bc4508]536bool ext4_inode_has_flag(ext4_inode_t *inode, uint32_t flag)
537{
[38542dc]538 if (ext4_inode_get_flags(inode) & flag)
[7b9381b]539 return true;
[38542dc]540
[7b9381b]541 return false;
542}
543
[296ef5d6]544/** Remove specified flag from i-node.
545 *
[38542dc]546 * @param inode I-node to clear flag on
547 * @param clear_flag Flag to be cleared
548 *
[296ef5d6]549 */
[b3d7277]550void ext4_inode_clear_flag(ext4_inode_t *inode, uint32_t clear_flag)
551{
552 uint32_t flags = ext4_inode_get_flags(inode);
553 flags = flags & (~clear_flag);
554 ext4_inode_set_flags(inode, flags);
555}
556
[296ef5d6]557/** Set specified flag to i-node.
558 *
[38542dc]559 * @param inode I-node to set flag on
560 * @param set_flag Flag to be set
561 *
[296ef5d6]562 */
[b3d7277]563void ext4_inode_set_flag(ext4_inode_t *inode, uint32_t set_flag)
564{
565 uint32_t flags = ext4_inode_get_flags(inode);
566 flags = flags | set_flag;
567 ext4_inode_set_flags(inode, flags);
568}
569
[296ef5d6]570/** Check if i-node can be truncated.
571 *
[38542dc]572 * @param sb Superblock
573 * @param inode I-node to check
574 *
575 * @return Result of the check operation
576 *
[296ef5d6]577 */
[43a9968]578bool ext4_inode_can_truncate(ext4_superblock_t *sb, ext4_inode_t *inode)
[12b4a7f]579{
[38542dc]580 if ((ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)) ||
581 (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)))
582 return false;
583
584 if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) ||
585 (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)))
586 return true;
587
588 return false;
[12b4a7f]589}
590
[eb91db7]591/**
592 * @}
[38542dc]593 */
Note: See TracBrowser for help on using the repository browser.