2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
\r
6 * Copyright (c) 2012 Martin Sucha
\r
7 * Copyright (c) 2012 Frantisek Princ
\r
8 * All rights reserved.
\r
10 * Redistribution and use in source and binary forms, with or without
\r
11 * modification, are permitted provided that the following conditions
\r
14 * - Redistributions of source code must retain the above copyright
\r
15 * notice, this list of conditions and the following disclaimer.
\r
16 * - Redistributions in binary form must reproduce the above copyright
\r
17 * notice, this list of conditions and the following disclaimer in the
\r
18 * documentation and/or other materials provided with the distribution.
\r
19 * - The name of the author may not be used to endorse or promote products
\r
20 * derived from this software without specific prior written permission.
\r
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
34 /** @addtogroup lwext4
\r
38 * @file ext4_inode.c
\r
39 * @brief Inode handle functions
\r
42 #include <ext4_config.h>
\r
43 #include <ext4_types.h>
\r
44 #include <ext4_inode.h>
\r
45 #include <ext4_super.h>
\r
47 /**@brief TODO: ...*/
\r
48 static uint32_t ext4_inode_block_bits_count(uint32_t block_size)
\r
51 uint32_t size = block_size;
\r
56 } while (size > 256);
\r
61 uint32_t ext4_inode_get_mode(struct ext4_sblock *sb, struct ext4_inode *inode)
\r
63 uint32_t v = to_le16(inode->mode);
\r
65 if(ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD){
\r
66 v |= ((uint32_t) to_le16(inode->osd2.hurd2.mode_high)) << 16;
\r
72 void ext4_inode_set_mode(struct ext4_sblock *sb, struct ext4_inode *inode,
\r
75 inode->mode = to_le16((mode << 16) >> 16);
\r
77 if(ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD)
\r
78 inode->osd2.hurd2.mode_high = to_le16(mode >> 16);
\r
81 uint32_t ext4_inode_get_uid(struct ext4_inode *inode)
\r
83 return to_le32(inode->uid);
\r
86 void ext4_inode_set_uid(struct ext4_inode *inode, uint32_t uid)
\r
88 inode->uid = to_le32(uid);
\r
91 uint64_t ext4_inode_get_size(struct ext4_sblock *sb, struct ext4_inode *inode)
\r
93 uint64_t v = to_le32(inode->size_lo);
\r
95 if ((ext4_get32(sb, rev_level) > 0) && (ext4_inode_is_type(sb, inode,
\r
96 EXT4_INODE_MODE_FILE)))
\r
97 v |= ((uint64_t)to_le32(inode->size_hi)) << 32;
\r
102 void ext4_inode_set_size(struct ext4_inode *inode, uint64_t size)
\r
104 inode->size_lo = to_le32((size << 32) >> 32);
\r
105 inode->size_hi = to_le32(size >> 32);
\r
108 uint32_t ext4_inode_get_access_time(struct ext4_inode *inode)
\r
110 return to_le32(inode->access_time);
\r
112 void ext4_inode_set_access_time(struct ext4_inode *inode, uint32_t time)
\r
114 inode->access_time = to_le32(time);
\r
118 uint32_t ext4_inode_get_change_inode_time(struct ext4_inode *inode)
\r
120 return to_le32(inode->change_inode_time);
\r
122 void ext4_inode_set_change_inode_time(struct ext4_inode *inode,
\r
125 inode->change_inode_time = to_le32(time);
\r
129 uint32_t ext4_inode_get_modification_time(struct ext4_inode *inode)
\r
131 return to_le32(inode->modification_time);
\r
134 void ext4_inode_set_modification_time(struct ext4_inode *inode,
\r
137 inode->modification_time = to_le32(time);
\r
141 uint32_t ext4_inode_get_deletion_time(struct ext4_inode *inode)
\r
143 return to_le32(inode->deletion_time);
\r
146 void ext4_inode_set_deletion_time(struct ext4_inode *inode, uint32_t time)
\r
148 inode->deletion_time = to_le32(time);
\r
151 uint32_t ext4_inode_get_gid(struct ext4_inode *inode)
\r
153 return to_le32(inode->gid);
\r
155 void ext4_inode_set_gid(struct ext4_inode *inode, uint32_t gid)
\r
157 inode->gid = to_le32(gid);
\r
160 uint16_t ext4_inode_get_links_count(struct ext4_inode *inode)
\r
162 return to_le16(inode->links_count);
\r
164 void ext4_inode_set_links_count(struct ext4_inode *inode, uint16_t cnt)
\r
166 inode->links_count = to_le16(cnt);
\r
169 uint64_t ext4_inode_get_blocks_count(struct ext4_sblock *sb,
\r
170 struct ext4_inode *inode)
\r
172 uint64_t count = to_le32(inode->blocks_count_lo);
\r
174 if (ext4_sb_check_read_only(sb,
\r
175 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
\r
178 count |= ((uint64_t) to_le16(inode->osd2.linux2.blocks_high)) << 32;
\r
180 if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
\r
182 uint32_t block_bits =
\r
183 ext4_inode_block_bits_count(ext4_sb_get_block_size(sb));
\r
184 return count << (block_bits - 9);
\r
191 int ext4_inode_set_blocks_count(struct ext4_sblock *sb,
\r
192 struct ext4_inode *inode, uint64_t count)
\r
194 /* 32-bit maximum */
\r
198 if (count <= max) {
\r
199 inode->blocks_count_lo = to_le32(count);
\r
200 inode->osd2.linux2.blocks_high = 0;
\r
201 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
\r
206 /* Check if there can be used huge files (many blocks) */
\r
207 if (!ext4_sb_check_read_only(sb,
\r
208 EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
\r
211 /* 48-bit maximum */
\r
215 if (count <= max) {
\r
216 inode->blocks_count_lo = to_le32(count);
\r
217 inode->osd2.linux2.blocks_high = to_le16(count >> 32);
\r
218 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
\r
220 uint32_t block_bits = ext4_inode_block_bits_count(ext4_sb_get_block_size(sb));
\r
222 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
\r
223 count = count >> (block_bits - 9);
\r
224 inode->blocks_count_lo = to_le32(count);
\r
225 inode->osd2.linux2.blocks_high = to_le16(count >> 32);
\r
231 uint32_t ext4_inode_get_flags(struct ext4_inode *inode)
\r
233 return to_le32(inode->flags);
\r
235 void ext4_inode_set_flags(struct ext4_inode *inode, uint32_t flags)
\r
237 inode->flags = to_le32(flags);
\r
240 uint32_t ext4_inode_get_generation(struct ext4_inode *inode)
\r
242 return to_le32(inode->generation);
\r
244 void ext4_inode_set_generation(struct ext4_inode *inode, uint32_t gen)
\r
246 inode->generation = to_le32(gen);
\r
249 uint64_t ext4_inode_get_file_acl(struct ext4_inode *inode,
\r
250 struct ext4_sblock *sb)
\r
252 /*TODO: Verify it*/
\r
253 uint64_t v = to_le32(inode->file_acl_lo);
\r
255 if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
\r
256 v |= ((uint32_t) to_le16(inode->osd2.linux2.file_acl_high)) << 16;
\r
262 void ext4_inode_set_file_acl(struct ext4_inode *inode, struct ext4_sblock *sb,
\r
265 /*TODO: Verify it*/
\r
266 inode->file_acl_lo = to_le32((acl << 32) >> 32);
\r
268 if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
\r
269 inode->osd2.linux2.file_acl_high = to_le16(acl >> 32);
\r
272 uint32_t ext4_inode_get_direct_block(struct ext4_inode *inode, uint32_t idx)
\r
274 return to_le32(inode->blocks[idx]);
\r
276 void ext4_inode_set_direct_block(struct ext4_inode *inode, uint32_t idx,
\r
279 inode->blocks[idx] = to_le32(block);
\r
282 uint32_t ext4_inode_get_indirect_block(struct ext4_inode *inode, uint32_t idx)
\r
284 return to_le32(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
\r
287 void ext4_inode_set_indirect_block(struct ext4_inode *inode, uint32_t idx,
\r
290 inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] = to_le32(block);
\r
293 bool ext4_inode_is_type(struct ext4_sblock *sb, struct ext4_inode *inode,
\r
296 return (ext4_inode_get_mode(sb, inode) &
\r
297 EXT4_INODE_MODE_TYPE_MASK) == type;
\r
300 bool ext4_inode_has_flag(struct ext4_inode *inode, uint32_t f)
\r
302 return ext4_inode_get_flags(inode) & f;
\r
305 void ext4_inode_clear_flag(struct ext4_inode *inode, uint32_t f)
\r
307 uint32_t flags = ext4_inode_get_flags(inode);
\r
308 flags = flags & (~f);
\r
309 ext4_inode_set_flags(inode, flags);
\r
312 void ext4_inode_set_flag(struct ext4_inode *inode, uint32_t f)
\r
314 uint32_t flags = ext4_inode_get_flags(inode);
\r
316 ext4_inode_set_flags(inode, flags);
\r
319 bool ext4_inode_can_truncate(struct ext4_sblock *sb,
\r
320 struct ext4_inode *inode)
\r
322 if ((ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)) ||
\r
323 (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)))
\r
326 if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) ||
\r
327 (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)))
\r
333 struct ext4_extent_header * ext4_inode_get_extent_header(
\r
334 struct ext4_inode *inode)
\r
336 return (struct ext4_extent_header *) inode->blocks;
\r