Symlink now can be removed by ext4_fremove.
[lwext4.git] / lwext4 / ext4_inode.c
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_inode.h"
45 #include "ext4_super.h"
46
47 /**@brief  Compute number of bits for block count.
48  * @param block_size Filesystem block_size
49  * @return Number of bits
50  */
51 static uint32_t ext4_inode_block_bits_count(uint32_t block_size)
52 {
53         uint32_t bits = 8;
54         uint32_t size = block_size;
55
56         do {
57                 bits++;
58                 size = size >> 1;
59         } while (size > 256);
60
61         return bits;
62 }
63
64 uint32_t ext4_inode_get_mode(struct ext4_sblock *sb, struct ext4_inode *inode)
65 {
66         uint32_t v = to_le16(inode->mode);
67
68         if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD) {
69                 v |= ((uint32_t)to_le16(inode->osd2.hurd2.mode_high)) << 16;
70         }
71
72         return v;
73 }
74
75 void ext4_inode_set_mode(struct ext4_sblock *sb, struct ext4_inode *inode,
76                          uint32_t mode)
77 {
78         inode->mode = to_le16((mode << 16) >> 16);
79
80         if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD)
81                 inode->osd2.hurd2.mode_high = to_le16(mode >> 16);
82 }
83
84 uint32_t ext4_inode_get_uid(struct ext4_inode *inode)
85 {
86         return to_le32(inode->uid);
87 }
88
89 void ext4_inode_set_uid(struct ext4_inode *inode, uint32_t uid)
90 {
91         inode->uid = to_le32(uid);
92 }
93
94 uint64_t ext4_inode_get_size(struct ext4_sblock *sb, struct ext4_inode *inode)
95 {
96         uint64_t v = to_le32(inode->size_lo);
97
98         if ((ext4_get32(sb, rev_level) > 0) &&
99             (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)))
100                 v |= ((uint64_t)to_le32(inode->size_hi)) << 32;
101
102         return v;
103 }
104
105 void ext4_inode_set_size(struct ext4_inode *inode, uint64_t size)
106 {
107         inode->size_lo = to_le32((size << 32) >> 32);
108         inode->size_hi = to_le32(size >> 32);
109 }
110
111 uint32_t ext4_inode_get_access_time(struct ext4_inode *inode)
112 {
113         return to_le32(inode->access_time);
114 }
115 void ext4_inode_set_access_time(struct ext4_inode *inode, uint32_t time)
116 {
117         inode->access_time = to_le32(time);
118 }
119
120 uint32_t ext4_inode_get_change_inode_time(struct ext4_inode *inode)
121 {
122         return to_le32(inode->change_inode_time);
123 }
124 void ext4_inode_set_change_inode_time(struct ext4_inode *inode, uint32_t time)
125 {
126         inode->change_inode_time = to_le32(time);
127 }
128
129 uint32_t ext4_inode_get_modification_time(struct ext4_inode *inode)
130 {
131         return to_le32(inode->modification_time);
132 }
133
134 void ext4_inode_set_modification_time(struct ext4_inode *inode, uint32_t time)
135 {
136         inode->modification_time = to_le32(time);
137 }
138
139 uint32_t ext4_inode_get_deletion_time(struct ext4_inode *inode)
140 {
141         return to_le32(inode->deletion_time);
142 }
143
144 void ext4_inode_set_deletion_time(struct ext4_inode *inode, uint32_t time)
145 {
146         inode->deletion_time = to_le32(time);
147 }
148
149 uint32_t ext4_inode_get_gid(struct ext4_inode *inode)
150 {
151         return to_le32(inode->gid);
152 }
153 void ext4_inode_set_gid(struct ext4_inode *inode, uint32_t gid)
154 {
155         inode->gid = to_le32(gid);
156 }
157
158 uint16_t ext4_inode_get_links_count(struct ext4_inode *inode)
159 {
160         return to_le16(inode->links_count);
161 }
162 void ext4_inode_set_links_count(struct ext4_inode *inode, uint16_t cnt)
163 {
164         inode->links_count = to_le16(cnt);
165 }
166
167 uint64_t ext4_inode_get_blocks_count(struct ext4_sblock *sb,
168                                      struct ext4_inode *inode)
169 {
170         uint64_t count = to_le32(inode->blocks_count_lo);
171
172         if (ext4_sb_has_feature_read_only(sb,
173                                           EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
174
175                 /* 48-bit field */
176                 count |= ((uint64_t)to_le16(inode->osd2.linux2.blocks_high))
177                          << 32;
178
179                 if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) {
180
181                         uint32_t block_bits = ext4_inode_block_bits_count(
182                             ext4_sb_get_block_size(sb));
183                         return count << (block_bits - 9);
184                 }
185         }
186
187         return count;
188 }
189
190 int ext4_inode_set_blocks_count(struct ext4_sblock *sb,
191                                 struct ext4_inode *inode, uint64_t count)
192 {
193         /* 32-bit maximum */
194         uint64_t max = 0;
195         max = ~max >> 32;
196
197         if (count <= max) {
198                 inode->blocks_count_lo = to_le32(count);
199                 inode->osd2.linux2.blocks_high = 0;
200                 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
201
202                 return EOK;
203         }
204
205         /* Check if there can be used huge files (many blocks) */
206         if (!ext4_sb_has_feature_read_only(sb,
207                                            EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
208                 return EINVAL;
209
210         /* 48-bit maximum */
211         max = 0;
212         max = ~max >> 16;
213
214         if (count <= max) {
215                 inode->blocks_count_lo = to_le32(count);
216                 inode->osd2.linux2.blocks_high = to_le16(count >> 32);
217                 ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
218         } else {
219                 uint32_t block_bits =
220                     ext4_inode_block_bits_count(ext4_sb_get_block_size(sb));
221
222                 ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE);
223                 count = count >> (block_bits - 9);
224                 inode->blocks_count_lo = to_le32(count);
225                 inode->osd2.linux2.blocks_high = to_le16(count >> 32);
226         }
227
228         return EOK;
229 }
230
231 uint32_t ext4_inode_get_flags(struct ext4_inode *inode)
232 {
233         return to_le32(inode->flags);
234 }
235 void ext4_inode_set_flags(struct ext4_inode *inode, uint32_t flags)
236 {
237         inode->flags = to_le32(flags);
238 }
239
240 uint32_t ext4_inode_get_generation(struct ext4_inode *inode)
241 {
242         return to_le32(inode->generation);
243 }
244 void ext4_inode_set_generation(struct ext4_inode *inode, uint32_t gen)
245 {
246         inode->generation = to_le32(gen);
247 }
248
249 uint64_t ext4_inode_get_file_acl(struct ext4_inode *inode,
250                                  struct ext4_sblock *sb)
251 {
252         /*TODO: Verify it*/
253         uint64_t v = to_le32(inode->file_acl_lo);
254
255         if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
256                 v |= ((uint32_t)to_le16(inode->osd2.linux2.file_acl_high))
257                      << 16;
258
259         return v;
260 }
261
262 void ext4_inode_set_file_acl(struct ext4_inode *inode, struct ext4_sblock *sb,
263                              uint64_t acl)
264 {
265         /*TODO: Verify it*/
266         inode->file_acl_lo = to_le32((acl << 32) >> 32);
267
268         if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX)
269                 inode->osd2.linux2.file_acl_high = to_le16(acl >> 32);
270 }
271
272 uint32_t ext4_inode_get_direct_block(struct ext4_inode *inode, uint32_t idx)
273 {
274         return to_le32(inode->blocks[idx]);
275 }
276 void ext4_inode_set_direct_block(struct ext4_inode *inode, uint32_t idx,
277                                  uint32_t block)
278 {
279         inode->blocks[idx] = to_le32(block);
280 }
281
282 uint32_t ext4_inode_get_indirect_block(struct ext4_inode *inode, uint32_t idx)
283 {
284         return to_le32(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
285 }
286
287 void ext4_inode_set_indirect_block(struct ext4_inode *inode, uint32_t idx,
288                                    uint32_t block)
289 {
290         inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] = to_le32(block);
291 }
292
293 uint32_t ext4_inode_type(struct ext4_sblock *sb, struct ext4_inode *inode)
294 {
295         return (ext4_inode_get_mode(sb, inode) & EXT4_INODE_MODE_TYPE_MASK);
296 }
297
298 bool ext4_inode_is_type(struct ext4_sblock *sb, struct ext4_inode *inode,
299                         uint32_t type)
300 {
301         return ext4_inode_type(sb, inode) == type;
302 }
303
304 bool ext4_inode_has_flag(struct ext4_inode *inode, uint32_t f)
305 {
306         return ext4_inode_get_flags(inode) & f;
307 }
308
309 void ext4_inode_clear_flag(struct ext4_inode *inode, uint32_t f)
310 {
311         uint32_t flags = ext4_inode_get_flags(inode);
312         flags = flags & (~f);
313         ext4_inode_set_flags(inode, flags);
314 }
315
316 void ext4_inode_set_flag(struct ext4_inode *inode, uint32_t f)
317 {
318         uint32_t flags = ext4_inode_get_flags(inode);
319         flags = flags | f;
320         ext4_inode_set_flags(inode, flags);
321 }
322
323 bool ext4_inode_can_truncate(struct ext4_sblock *sb, struct ext4_inode *inode)
324 {
325         if ((ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)) ||
326             (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE)))
327                 return false;
328
329         if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) ||
330             (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)) ||
331             (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_SOFTLINK)))
332                 return true;
333
334         return false;
335 }
336
337 struct ext4_extent_header *
338 ext4_inode_get_extent_header(struct ext4_inode *inode)
339 {
340         return (struct ext4_extent_header *)inode->blocks;
341 }
342
343 /**
344  * @}
345  */