1/*
2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3 *
4 *
5 * HelenOS:
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * - The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/** @addtogroup lwext4
35 * @{
36 */
37/**
38 * @file ext4_inode.c
39 * @brief Inode handle functions
40 */
41
42#include <ext4_config.h>
43#include <ext4_types.h>
44#include <ext4_misc.h>
45#include <ext4_errno.h>
46#include <ext4_debug.h>
47
48#include <ext4_inode.h>
49#include <ext4_super.h>
50
51/**@brief Compute number of bits for block count.
52 * @param block_size Filesystem block_size
53 * @return Number of bits
54 */
55static uint32_t ext4_inode_block_bits_count(uint32_t block_size)
56{
57 uint32_t bits = 8;
58 uint32_t size = block_size;
59
60 do {
61 bits++;
62 size = size >> 1;
63 } while (size > 256);
64
65 return bits;
66}
67
68uint32_t ext4_inode_get_mode(struct ext4_sblock *sb, struct ext4_inode *inode)
69{
70 uint32_t v = to_le16(inode->mode);
71
72 if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD) {
73 v |= ((uint32_t)to_le16(inode->osd2.hurd2.mode_high)) << 16;
74 }
75
76 return v;
77}
78
79void ext4_inode_set_mode(struct ext4_sblock *sb, struct ext4_inode *inode,
80 uint32_t mode)
81{
82 inode->mode = to_le16((mode << 16) >> 16);
83
84 if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD)
85 inode->osd2.hurd2.mode_high = to_le16(mode >> 16);
86}
87
88uint32_t ext4_inode_get_uid(struct ext4_inode *inode)
89{
90 return to_le32(inode->uid);
91}
92
93void ext4_inode_set_uid(struct ext4_inode *inode, uint32_t uid)
94{
95 inode->uid = to_le32(uid);
96}
97
98uint64_t ext4_inode_get_size(struct ext4_sblock *sb, struct ext4_inode *inode)
99{
100 uint64_t v = to_le32(inode->size_lo);
101
102 if ((ext4_get32(sb, rev_level) > 0) &&
103 (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)))
104 v |= ((uint64_t)to_le32(inode->size_hi)) << 32;
105
106 return v;
107}
108
109void ext4_inode_set_size(struct ext4_inode *inode, uint64_t size)
110{
111 inode->size_lo = to_le32((size << 32) >> 32);
112 inode->size_hi = to_le32(size >> 32);
113}
114
115uint32_t ext4_inode_get_csum(struct ext4_sblock *sb, struct ext4_inode *inode)
116{
117 uint16_t inode_size = ext4_get16(sb, inode_size);
118 uint32_t v = to_le16(inode->osd2.linux2.checksum_lo);
119
120 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE)
121 v |= ((uint32_t)to_le16(inode->checksum_hi)) << 16;
122
123 return v;
124}
125
126void ext4_inode_set_csum(struct ext4_sblock *sb, struct ext4_inode *inode,
127 uint32_t checksum)
128{
129 uint16_t inode_size = ext4_get16(sb, inode_size);
130 inode->osd2.linux2.checksum_lo =
131 to_le16((checksum << 16) >> 16);
132
133 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE)
134 inode->checksum_hi = to_le16(checksum >> 16);
135
136}
137
138uint32_t ext4_inode_get_access_time(struct ext4_inode *inode)
139{
140 return to_le32(inode->access_time);
141}
142void ext4_inode_set_access_time(struct ext4_inode *inode, uint32_t time)
143{
144 inode->access_time = to_le32(time);
145}
146
147uint32_t ext4_inode_get_change_inode_time(struct ext4_inode *inode)
148{
149 return to_le32(inode->change_inode_time);
150}
151void ext4_inode_set_change_inode_time(struct ext4_inode *inode, uint32_t time)
152{
153 inode->change_inode_time = to_le32(time);
154}
155
156uint32_t ext4_inode_get_modif_time(struct ext4_inode *inode)
157{
158 return to_le32(inode->modification_time);
159}
160
161void ext4_inode_set_modif_time(struct ext4_inode *inode, uint32_t time)
162{
163 inode->modification_time = to_le32(time);
164}
165
166uint32_t ext4_inode_get_del_time(struct ext4_inode *inode)
167{
168 return to_le32(inode->deletion_time);
169}
170
171void ext4_inode_set_del_time(struct ext4_inode *inode, uint32_t time)
172{
173 inode->deletion_time = to_le32(time);
174}
175
176uint32_t ext4_inode_get_gid(struct ext4_inode *inode)
177{
178 return to_le32(inode->gid);
179}
180void ext4_inode_set_gid(struct ext4_inode *inode, uint32_t gid)
181{
182 inode->gid = to_le32(gid);
183}
184
185uint16_t ext4_inode_get_links_cnt(struct ext4_inode *inode)
186{
187 return to_le16(inode->links_count);
188}
189void ext4_inode_set_links_cnt(struct ext4_inode *inode, uint16_t cnt)
190{
191 inode->links_count = to_le16(cnt);
192}
193
194uint64_t ext4_inode_get_blocks_count(struct ext4_sblock *sb,
195 struct ext4_inode *inode)
196{
197 uint64_t cnt = to_le32(inode->blocks_count_lo);
198
199 if (ext4_sb_feature_ro_com(s: sb, EXT4_FRO_COM_HUGE_FILE)) {
200
201 /* 48-bit field */
202 cnt |= (uint64_t)to_le16(inode->osd2.linux2.blocks_high) << 32;
203
204 if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
205
206 uint32_t block_count = ext4_sb_get_block_size(s: sb);
207 uint32_t b = ext4_inode_block_bits_count(block_size: block_count);
208 return cnt << (b - 9);
209 }
210 }
211
212 return cnt;
213}
214
215int ext4_inode_set_blocks_count(struct ext4_sblock *sb,
216 struct ext4_inode *inode, uint64_t count)
217{
218 /* 32-bit maximum */
219 uint64_t max = 0;
220 max = ~max >> 32;
221
222 if (count <= max) {
223 inode->blocks_count_lo = to_le32((uint32_t)count);
224 inode->osd2.linux2.blocks_high = 0;
225 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
226
227 return EOK;
228 }
229
230 /* Check if there can be used huge files (many blocks) */
231 if (!ext4_sb_feature_ro_com(s: sb, EXT4_FRO_COM_HUGE_FILE))
232 return EINVAL;
233
234 /* 48-bit maximum */
235 max = 0;
236 max = ~max >> 16;
237
238 if (count <= max) {
239 inode->blocks_count_lo = to_le32((uint32_t)count);
240 inode->osd2.linux2.blocks_high = to_le16((uint16_t)(count >> 32));
241 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
242 } else {
243 uint32_t block_count = ext4_sb_get_block_size(s: sb);
244 uint32_t block_bits =ext4_inode_block_bits_count(block_size: block_count);
245
246 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
247 count = count >> (block_bits - 9);
248 inode->blocks_count_lo = to_le32((uint32_t)count);
249 inode->osd2.linux2.blocks_high = to_le16((uint16_t)(count >> 32));
250 }
251
252 return EOK;
253}
254
255uint32_t ext4_inode_get_flags(struct ext4_inode *inode)
256{
257 return to_le32(inode->flags);
258}
259void ext4_inode_set_flags(struct ext4_inode *inode, uint32_t flags)
260{
261 inode->flags = to_le32(flags);
262}
263
264uint32_t ext4_inode_get_generation(struct ext4_inode *inode)
265{
266 return to_le32(inode->generation);
267}
268void ext4_inode_set_generation(struct ext4_inode *inode, uint32_t gen)
269{
270 inode->generation = to_le32(gen);
271}
272
273uint16_t ext4_inode_get_extra_isize(struct ext4_sblock *sb,
274 struct ext4_inode *inode)
275{
276 uint16_t inode_size = ext4_get16(sb, inode_size);
277 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE)
278 return to_le16(inode->extra_isize);
279 else
280 return 0;
281}
282
283void ext4_inode_set_extra_isize(struct ext4_sblock *sb,
284 struct ext4_inode *inode,
285 uint16_t size)
286{
287 uint16_t inode_size = ext4_get16(sb, inode_size);
288 if (inode_size > EXT4_GOOD_OLD_INODE_SIZE)
289 inode->extra_isize = to_le16(size);
290}
291
292uint64_t ext4_inode_get_file_acl(struct ext4_inode *inode,
293 struct ext4_sblock *sb)
294{
295 uint64_t v = to_le32(inode->file_acl_lo);
296
297 if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
298 v |= (uint32_t)to_le16(inode->osd2.linux2.file_acl_high) << 16;
299
300 return v;
301}
302
303void ext4_inode_set_file_acl(struct ext4_inode *inode, struct ext4_sblock *sb,
304 uint64_t acl)
305{
306 inode->file_acl_lo = to_le32((acl << 32) >> 32);
307
308 if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
309 inode->osd2.linux2.file_acl_high = to_le16((uint16_t)(acl >> 32));
310}
311
312uint32_t ext4_inode_get_direct_block(struct ext4_inode *inode, uint32_t idx)
313{
314 return to_le32(inode->blocks[idx]);
315}
316void ext4_inode_set_direct_block(struct ext4_inode *inode, uint32_t idx,
317 uint32_t block)
318{
319 inode->blocks[idx] = to_le32(block);
320}
321
322uint32_t ext4_inode_get_indirect_block(struct ext4_inode *inode, uint32_t idx)
323{
324 return to_le32(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
325}
326
327void ext4_inode_set_indirect_block(struct ext4_inode *inode, uint32_t idx,
328 uint32_t block)
329{
330 inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] = to_le32(block);
331}
332
333uint32_t ext4_inode_get_dev(struct ext4_inode *inode)
334{
335 uint32_t dev_0, dev_1;
336 dev_0 = ext4_inode_get_direct_block(inode, idx: 0);
337 dev_1 = ext4_inode_get_direct_block(inode, idx: 1);
338
339 if (dev_0)
340 return dev_0;
341 else
342 return dev_1;
343}
344
345void ext4_inode_set_dev(struct ext4_inode *inode, uint32_t dev)
346{
347 if (dev & ~0xFFFF)
348 ext4_inode_set_direct_block(inode, idx: 1, block: dev);
349 else
350 ext4_inode_set_direct_block(inode, idx: 0, block: dev);
351}
352
353uint32_t ext4_inode_type(struct ext4_sblock *sb, struct ext4_inode *inode)
354{
355 return (ext4_inode_get_mode(sb, inode) & EXT4_INODE_MODE_TYPE_MASK);
356}
357
358bool ext4_inode_is_type(struct ext4_sblock *sb, struct ext4_inode *inode,
359 uint32_t type)
360{
361 return ext4_inode_type(sb, inode) == type;
362}
363
364bool ext4_inode_has_flag(struct ext4_inode *inode, uint32_t f)
365{
366 return ext4_inode_get_flags(inode) & f;
367}
368
369void ext4_inode_clear_flag(struct ext4_inode *inode, uint32_t f)
370{
371 uint32_t flags = ext4_inode_get_flags(inode);
372 flags = flags & (~f);
373 ext4_inode_set_flags(inode, flags);
374}
375
376void ext4_inode_set_flag(struct ext4_inode *inode, uint32_t f)
377{
378 uint32_t flags = ext4_inode_get_flags(inode);
379 flags = flags | f;
380 ext4_inode_set_flags(inode, flags);
381}
382
383bool ext4_inode_can_truncate(struct ext4_sblock *sb, struct ext4_inode *inode)
384{
385 if ((ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)) ||
386 (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)))
387 return false;
388
389 if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) ||
390 (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)) ||
391 (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_SOFTLINK)))
392 return true;
393
394 return false;
395}
396
397struct ext4_extent_header *
398ext4_inode_get_extent_header(struct ext4_inode *inode)
399{
400 return (struct ext4_extent_header *)inode->blocks;
401}
402
403/**
404 * @}
405 */
406