blob: ea9b92298ba759f3cb7f13cf57e3c335a944808a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Uma Shankara1596432012-05-25 21:21:44 +05302/*
3 * (C) Copyright 2011 - 2012 Samsung Electronics
4 * EXT4 filesystem implementation in Uboot by
5 * Uma Shankar <uma.shankar@samsung.com>
6 * Manjunatha C Achar <a.manjunatha@samsung.com>
7 *
8 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
9 *
10 * (C) Copyright 2004
11 * esd gmbh <www.esd-electronics.com>
12 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
13 *
14 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
15 * GRUB -- GRand Unified Bootloader
16 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
17 *
Uma Shankared34f342012-05-25 21:22:49 +053018 * ext4write : Based on generic ext4 protocol.
Uma Shankara1596432012-05-25 21:21:44 +053019 */
20
21#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060022#include <blk.h>
Uma Shankara1596432012-05-25 21:21:44 +053023#include <ext_common.h>
24#include <ext4fs.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060025#include <log.h>
Uma Shankara1596432012-05-25 21:21:44 +053026#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060027#include <memalign.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060028#include <part.h>
Uma Shankara1596432012-05-25 21:21:44 +053029#include <stddef.h>
30#include <linux/stat.h>
31#include <linux/time.h>
32#include <asm/byteorder.h>
33#include "ext4_common.h"
34
35struct ext2_data *ext4fs_root;
36struct ext2fs_node *ext4fs_file;
Michael Walle58a9ecb2016-09-01 11:21:40 +020037__le32 *ext4fs_indir1_block;
Uma Shankara1596432012-05-25 21:21:44 +053038int ext4fs_indir1_size;
39int ext4fs_indir1_blkno = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +020040__le32 *ext4fs_indir2_block;
Uma Shankara1596432012-05-25 21:21:44 +053041int ext4fs_indir2_size;
42int ext4fs_indir2_blkno = -1;
43
Michael Walle58a9ecb2016-09-01 11:21:40 +020044__le32 *ext4fs_indir3_block;
Uma Shankara1596432012-05-25 21:21:44 +053045int ext4fs_indir3_size;
46int ext4fs_indir3_blkno = -1;
47struct ext2_inode *g_parent_inode;
48static int symlinknest;
49
Stephen Warren03e2ecf2012-10-22 06:43:50 +000050#if defined(CONFIG_EXT4_WRITE)
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +020051struct ext2_block_group *ext4fs_get_group_descriptor
52 (const struct ext_filesystem *fs, uint32_t bg_idx)
53{
54 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
55}
56
Michael Walle58a9ecb2016-09-01 11:21:40 +020057static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
58{
59 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
60}
61
62static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
63{
Stefan Brüns749e93e2016-09-20 01:13:01 +020064 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
65 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
66 free_blocks--;
67
68 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
69 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
Michael Walle58a9ecb2016-09-01 11:21:40 +020070}
71
Stefan Brüns749e93e2016-09-20 01:13:01 +020072static inline void ext4fs_bg_free_inodes_dec
73 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020074{
Stefan Brüns749e93e2016-09-20 01:13:01 +020075 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
76 if (fs->gdsize == 64)
77 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
78 free_inodes--;
79
80 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
81 if (fs->gdsize == 64)
82 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020083}
84
Stefan Brüns749e93e2016-09-20 01:13:01 +020085static inline void ext4fs_bg_free_blocks_dec
86 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020087{
Stefan Brüns749e93e2016-09-20 01:13:01 +020088 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
89 if (fs->gdsize == 64)
90 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
91 free_blocks--;
92
93 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
94 if (fs->gdsize == 64)
95 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020096}
97
Stefan Brüns749e93e2016-09-20 01:13:01 +020098static inline void ext4fs_bg_itable_unused_dec
99 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +0200100{
Stefan Brüns749e93e2016-09-20 01:13:01 +0200101 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
102 if (fs->gdsize == 64)
103 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
104 free_inodes--;
105
106 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
107 if (fs->gdsize == 64)
108 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200109}
110
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +0200111uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
112{
113 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
114 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
115 return free_blocks;
116}
117
118void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
119{
120 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
121 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
122}
123
124uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
125 const struct ext_filesystem *fs)
126{
127 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
128 if (fs->gdsize == 64)
129 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
130 return free_blocks;
131}
132
133static inline
134uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
135 const struct ext_filesystem *fs)
136{
137 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
138 if (fs->gdsize == 64)
139 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
140 return free_inodes;
141}
142
143static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
144{
145 return le16_to_cpu(bg->bg_flags);
146}
147
148static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
149 uint16_t flags)
150{
151 bg->bg_flags = cpu_to_le16(flags);
152}
153
154/* Block number of the block bitmap */
155uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
156 const struct ext_filesystem *fs)
157{
158 uint64_t block_nr = le32_to_cpu(bg->block_id);
159 if (fs->gdsize == 64)
160 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
161 return block_nr;
162}
163
164/* Block number of the inode bitmap */
165uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
166 const struct ext_filesystem *fs)
167{
168 uint64_t block_nr = le32_to_cpu(bg->inode_id);
169 if (fs->gdsize == 64)
170 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
171 return block_nr;
172}
173#endif
174
175/* Block number of the inode table */
176uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
177 const struct ext_filesystem *fs)
178{
179 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
180 if (fs->gdsize == 64)
181 block_nr +=
182 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
183 return block_nr;
184}
185
186#if defined(CONFIG_EXT4_WRITE)
Uma Shankared34f342012-05-25 21:22:49 +0530187uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
188{
189 uint32_t res = size / n;
190 if (res * n != size)
191 res++;
192
193 return res;
194}
195
Jean-Jacques Hiblotb0001802019-02-13 12:15:24 +0100196void put_ext4(uint64_t off, const void *buf, uint32_t size)
Uma Shankared34f342012-05-25 21:22:49 +0530197{
198 uint64_t startblock;
199 uint64_t remainder;
200 unsigned char *temp_ptr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530201 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +0000202 int log2blksz = fs->dev_desc->log2blksz;
203 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530204
Egbert Eich50ce4c02013-05-01 01:13:19 +0000205 startblock = off >> log2blksz;
Uma Shankared34f342012-05-25 21:22:49 +0530206 startblock += part_offset;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000207 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankared34f342012-05-25 21:22:49 +0530208
209 if (fs->dev_desc == NULL)
210 return;
211
Egbert Eich50ce4c02013-05-01 01:13:19 +0000212 if ((startblock + (size >> log2blksz)) >
Uma Shankared34f342012-05-25 21:22:49 +0530213 (part_offset + fs->total_sect)) {
Frederic Leroy04735e92013-06-26 18:11:25 +0200214 printf("part_offset is " LBAFU "\n", part_offset);
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900215 printf("total_sector is %llu\n", fs->total_sect);
Uma Shankared34f342012-05-25 21:22:49 +0530216 printf("error: overflow occurs\n");
217 return;
218 }
219
220 if (remainder) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700221 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
222 temp_ptr = sec_buf;
223 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
224 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530225 } else {
Egbert Eich50ce4c02013-05-01 01:13:19 +0000226 if (size >> log2blksz != 0) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700227 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
228 (unsigned long *)buf);
Uma Shankared34f342012-05-25 21:22:49 +0530229 } else {
Simon Glass2a981dc2016-02-29 15:25:52 -0700230 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530231 temp_ptr = sec_buf;
232 memcpy(temp_ptr, buf, size);
Simon Glass2a981dc2016-02-29 15:25:52 -0700233 blk_dwrite(fs->dev_desc, startblock, 1,
234 (unsigned long *)sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530235 }
236 }
237}
238
239static int _get_new_inode_no(unsigned char *buffer)
240{
241 struct ext_filesystem *fs = get_fs();
242 unsigned char input;
243 int operand, status;
244 int count = 1;
245 int j = 0;
246
247 /* get the blocksize of the filesystem */
248 unsigned char *ptr = buffer;
249 while (*ptr == 255) {
250 ptr++;
251 count += 8;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200252 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankared34f342012-05-25 21:22:49 +0530253 return -1;
254 }
255
256 for (j = 0; j < fs->blksz; j++) {
257 input = *ptr;
258 int i = 0;
259 while (i <= 7) {
260 operand = 1 << i;
261 status = input & operand;
262 if (status) {
263 i++;
264 count++;
265 } else {
266 *ptr |= operand;
267 return count;
268 }
269 }
270 ptr = ptr + 1;
271 }
272
273 return -1;
274}
275
276static int _get_new_blk_no(unsigned char *buffer)
277{
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200278 int operand;
Uma Shankared34f342012-05-25 21:22:49 +0530279 int count = 0;
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200280 int i;
Uma Shankared34f342012-05-25 21:22:49 +0530281 unsigned char *ptr = buffer;
282 struct ext_filesystem *fs = get_fs();
283
Uma Shankared34f342012-05-25 21:22:49 +0530284 while (*ptr == 255) {
285 ptr++;
286 count += 8;
287 if (count == (fs->blksz * 8))
288 return -1;
289 }
290
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200291 if (fs->blksz == 1024)
292 count += 1;
293
294 for (i = 0; i <= 7; i++) {
295 operand = 1 << i;
296 if (*ptr & operand) {
297 count++;
298 } else {
299 *ptr |= operand;
300 return count;
Uma Shankared34f342012-05-25 21:22:49 +0530301 }
Uma Shankared34f342012-05-25 21:22:49 +0530302 }
303
304 return -1;
305}
306
307int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
308{
309 int i, remainder, status;
310 unsigned char *ptr = buffer;
311 unsigned char operand;
312 i = blockno / 8;
313 remainder = blockno % 8;
314 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
315
316 i = i - (index * blocksize);
317 if (blocksize != 1024) {
318 ptr = ptr + i;
319 operand = 1 << remainder;
320 status = *ptr & operand;
321 if (status)
322 return -1;
323
324 *ptr = *ptr | operand;
325 return 0;
326 } else {
327 if (remainder == 0) {
328 ptr = ptr + i - 1;
329 operand = (1 << 7);
330 } else {
331 ptr = ptr + i;
332 operand = (1 << (remainder - 1));
333 }
334 status = *ptr & operand;
335 if (status)
336 return -1;
337
338 *ptr = *ptr | operand;
339 return 0;
340 }
341}
342
343void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
344{
345 int i, remainder, status;
346 unsigned char *ptr = buffer;
347 unsigned char operand;
348 i = blockno / 8;
349 remainder = blockno % 8;
350 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
351
352 i = i - (index * blocksize);
353 if (blocksize != 1024) {
354 ptr = ptr + i;
355 operand = (1 << remainder);
356 status = *ptr & operand;
357 if (status)
358 *ptr = *ptr & ~(operand);
359 } else {
360 if (remainder == 0) {
361 ptr = ptr + i - 1;
362 operand = (1 << 7);
363 } else {
364 ptr = ptr + i;
365 operand = (1 << (remainder - 1));
366 }
367 status = *ptr & operand;
368 if (status)
369 *ptr = *ptr & ~(operand);
370 }
371}
372
373int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
374{
375 int i, remainder, status;
376 unsigned char *ptr = buffer;
377 unsigned char operand;
378
Michael Walle58a9ecb2016-09-01 11:21:40 +0200379 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530380 i = inode_no / 8;
381 remainder = inode_no % 8;
382 if (remainder == 0) {
383 ptr = ptr + i - 1;
384 operand = (1 << 7);
385 } else {
386 ptr = ptr + i;
387 operand = (1 << (remainder - 1));
388 }
389 status = *ptr & operand;
390 if (status)
391 return -1;
392
393 *ptr = *ptr | operand;
394
395 return 0;
396}
397
398void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
399{
400 int i, remainder, status;
401 unsigned char *ptr = buffer;
402 unsigned char operand;
403
Michael Walle58a9ecb2016-09-01 11:21:40 +0200404 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530405 i = inode_no / 8;
406 remainder = inode_no % 8;
407 if (remainder == 0) {
408 ptr = ptr + i - 1;
409 operand = (1 << 7);
410 } else {
411 ptr = ptr + i;
412 operand = (1 << (remainder - 1));
413 }
414 status = *ptr & operand;
415 if (status)
416 *ptr = *ptr & ~(operand);
417}
418
Michael Walle58a9ecb2016-09-01 11:21:40 +0200419uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankared34f342012-05-25 21:22:49 +0530420{
421 struct ext2_block_group *desc;
422 struct ext_filesystem *fs = get_fs();
Michael Walle58a9ecb2016-09-01 11:21:40 +0200423 uint16_t crc = 0;
424 __le32 le32_i = cpu_to_le32(i);
Uma Shankared34f342012-05-25 21:22:49 +0530425
Stefan Brüns688d0e72016-09-17 02:10:10 +0200426 desc = ext4fs_get_group_descriptor(fs, i);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200427 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankared34f342012-05-25 21:22:49 +0530428 int offset = offsetof(struct ext2_block_group, bg_checksum);
429
Pali Rohár4f0e77f2022-04-12 11:20:43 +0200430 crc = crc16(~0, (__u8 *)fs->sb->unique_id,
Uma Shankared34f342012-05-25 21:22:49 +0530431 sizeof(fs->sb->unique_id));
Pali Rohár4f0e77f2022-04-12 11:20:43 +0200432 crc = crc16(crc, (__u8 *)&le32_i, sizeof(le32_i));
433 crc = crc16(crc, (__u8 *)desc, offset);
Uma Shankared34f342012-05-25 21:22:49 +0530434 offset += sizeof(desc->bg_checksum); /* skip checksum */
435 assert(offset == sizeof(*desc));
Tuomas Tynkkynen385b7312017-09-25 22:06:31 +0300436 if (offset < fs->gdsize) {
Pali Rohár4f0e77f2022-04-12 11:20:43 +0200437 crc = crc16(crc, (__u8 *)desc + offset,
Tuomas Tynkkynen385b7312017-09-25 22:06:31 +0300438 fs->gdsize - offset);
439 }
Uma Shankared34f342012-05-25 21:22:49 +0530440 }
441
442 return crc;
443}
444
445static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
446{
447 int dentry_length;
448 int sizeof_void_space;
449 int new_entry_byte_reqd;
450 short padding_factor = 0;
451
452 if (dir->namelen % 4 != 0)
453 padding_factor = 4 - (dir->namelen % 4);
454
455 dentry_length = sizeof(struct ext2_dirent) +
456 dir->namelen + padding_factor;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200457 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankared34f342012-05-25 21:22:49 +0530458 if (sizeof_void_space == 0)
459 return 0;
460
461 padding_factor = 0;
462 if (strlen(filename) % 4 != 0)
463 padding_factor = 4 - (strlen(filename) % 4);
464
465 new_entry_byte_reqd = strlen(filename) +
466 sizeof(struct ext2_dirent) + padding_factor;
467 if (sizeof_void_space >= new_entry_byte_reqd) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200468 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankared34f342012-05-25 21:22:49 +0530469 return sizeof_void_space;
470 }
471
472 return 0;
473}
474
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200475int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankared34f342012-05-25 21:22:49 +0530476{
477 unsigned int *zero_buffer = NULL;
478 char *root_first_block_buffer = NULL;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200479 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530480 long int first_block_no_of_root = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530481 int totalbytes = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530482 unsigned int new_entry_byte_reqd;
Uma Shankared34f342012-05-25 21:22:49 +0530483 int sizeof_void_space = 0;
484 int templength = 0;
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200485 int inodeno = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530486 int status;
487 struct ext_filesystem *fs = get_fs();
488 /* directory entry */
489 struct ext2_dirent *dir;
Uma Shankared34f342012-05-25 21:22:49 +0530490 char *temp_dir = NULL;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200491 uint32_t new_blk_no;
492 uint32_t new_size;
493 uint32_t new_blockcnt;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200494 uint32_t directory_blocks;
Uma Shankared34f342012-05-25 21:22:49 +0530495
496 zero_buffer = zalloc(fs->blksz);
497 if (!zero_buffer) {
498 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200499 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530500 }
501 root_first_block_buffer = zalloc(fs->blksz);
502 if (!root_first_block_buffer) {
503 free(zero_buffer);
504 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200505 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530506 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200507 new_entry_byte_reqd = ROUND(strlen(filename) +
508 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530509restart:
Stefan Brünsa321abd2016-09-06 04:36:44 +0200510 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
511 LOG2_BLOCK_SIZE(ext4fs_root);
512 blk_idx = directory_blocks - 1;
Uma Shankared34f342012-05-25 21:22:49 +0530513
Stefan Brünsa321abd2016-09-06 04:36:44 +0200514restart_read:
Uma Shankared34f342012-05-25 21:22:49 +0530515 /* read the block no allocated to a file */
Stephen Warrend5aee652019-01-30 12:58:05 -0700516 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx,
517 NULL);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200518 if (first_block_no_of_root <= 0)
519 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530520
Frederic Leroy04735e92013-06-26 18:11:25 +0200521 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankared34f342012-05-25 21:22:49 +0530522 * fs->sect_perblk,
523 0, fs->blksz, root_first_block_buffer);
524 if (status == 0)
525 goto fail;
526
527 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
528 goto fail;
529 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankared34f342012-05-25 21:22:49 +0530530 totalbytes = 0;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200531
Michael Walle58a9ecb2016-09-01 11:21:40 +0200532 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsa321abd2016-09-06 04:36:44 +0200533 unsigned short used_len = ROUND(dir->namelen +
534 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530535
Stefan Brünsa321abd2016-09-06 04:36:44 +0200536 /* last entry of block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200537 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankared34f342012-05-25 21:22:49 +0530538
Stefan Brünsa321abd2016-09-06 04:36:44 +0200539 /* check if new entry fits */
540 if ((used_len + new_entry_byte_reqd) <=
541 le16_to_cpu(dir->direntlen)) {
542 dir->direntlen = cpu_to_le16(used_len);
543 break;
544 } else {
545 if (blk_idx > 0) {
546 printf("Block full, trying previous\n");
547 blk_idx--;
548 goto restart_read;
549 }
550 printf("All blocks full: Allocate new\n");
Uma Shankared34f342012-05-25 21:22:49 +0530551
Stefan Brünsb96c3c72016-09-06 04:36:43 +0200552 if (le32_to_cpu(g_parent_inode->flags) &
553 EXT4_EXTENTS_FL) {
554 printf("Directory uses extents\n");
555 goto fail;
556 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200557 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankared34f342012-05-25 21:22:49 +0530558 printf("Directory exceeds limit\n");
559 goto fail;
560 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200561 new_blk_no = ext4fs_get_new_blk_no();
562 if (new_blk_no == -1) {
Uma Shankared34f342012-05-25 21:22:49 +0530563 printf("no block left to assign\n");
564 goto fail;
565 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200566 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200567 g_parent_inode->b.blocks.
568 dir_blocks[directory_blocks] =
Michael Walle58a9ecb2016-09-01 11:21:40 +0200569 cpu_to_le32(new_blk_no);
570
571 new_size = le32_to_cpu(g_parent_inode->size);
572 new_size += fs->blksz;
573 g_parent_inode->size = cpu_to_le32(new_size);
574
575 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
Marek Szyprowski1c9f8f62019-06-21 15:32:51 +0200576 new_blockcnt += fs->blksz >> LOG2_SECTOR_SIZE;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200577 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
578
Uma Shankared34f342012-05-25 21:22:49 +0530579 if (ext4fs_put_metadata
580 (root_first_block_buffer,
581 first_block_no_of_root))
582 goto fail;
583 goto restart;
584 }
Uma Shankared34f342012-05-25 21:22:49 +0530585 }
586
Michael Walle58a9ecb2016-09-01 11:21:40 +0200587 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530588 totalbytes = totalbytes + templength;
589 sizeof_void_space = check_void_in_dentry(dir, filename);
590 if (sizeof_void_space)
591 break;
592
593 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530594 }
595
596 /* make a pointer ready for creating next directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200597 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530598 totalbytes = totalbytes + templength;
599 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530600
601 /* get the next available inode number */
602 inodeno = ext4fs_get_new_inode_no();
603 if (inodeno == -1) {
604 printf("no inode left to assign\n");
605 goto fail;
606 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200607 dir->inode = cpu_to_le32(inodeno);
Uma Shankared34f342012-05-25 21:22:49 +0530608 if (sizeof_void_space)
Michael Walle58a9ecb2016-09-01 11:21:40 +0200609 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankared34f342012-05-25 21:22:49 +0530610 else
Michael Walle58a9ecb2016-09-01 11:21:40 +0200611 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankared34f342012-05-25 21:22:49 +0530612
613 dir->namelen = strlen(filename);
Jean-Jacques Hiblot5efc0682019-02-13 12:15:25 +0100614 dir->filetype = file_type;
Uma Shankared34f342012-05-25 21:22:49 +0530615 temp_dir = (char *)dir;
616 temp_dir = temp_dir + sizeof(struct ext2_dirent);
617 memcpy(temp_dir, filename, strlen(filename));
618
Uma Shankared34f342012-05-25 21:22:49 +0530619 /* update or write the 1st block of root inode */
620 if (ext4fs_put_metadata(root_first_block_buffer,
621 first_block_no_of_root))
622 goto fail;
623
624fail:
625 free(zero_buffer);
626 free(root_first_block_buffer);
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200627
628 return inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530629}
630
631static int search_dir(struct ext2_inode *parent_inode, char *dirname)
632{
633 int status;
Stefan Brüns76a29512016-09-06 04:36:41 +0200634 int inodeno = 0;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200635 int offset;
636 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530637 long int blknr;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200638 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530639 struct ext2_dirent *dir = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530640 struct ext_filesystem *fs = get_fs();
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200641 uint32_t directory_blocks;
642 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530643
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200644 directory_blocks = le32_to_cpu(parent_inode->size) >>
645 LOG2_BLOCK_SIZE(ext4fs_root);
646
647 block_buffer = zalloc(fs->blksz);
648 if (!block_buffer)
649 goto fail;
650
651 /* get the block no allocated to a file */
652 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
Stephen Warrend5aee652019-01-30 12:58:05 -0700653 blknr = read_allocated_block(parent_inode, blk_idx, NULL);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200654 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530655 goto fail;
656
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200657 /* read the directory block */
Frederic Leroy04735e92013-06-26 18:11:25 +0200658 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +0530659 0, fs->blksz, (char *)block_buffer);
660 if (status == 0)
661 goto fail;
662
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200663 offset = 0;
664 do {
Ian Rayecdfb412017-11-08 15:35:10 +0000665 if (offset & 3) {
666 printf("Badly aligned ext2_dirent\n");
667 break;
668 }
669
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200670 dir = (struct ext2_dirent *)(block_buffer + offset);
671 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankared34f342012-05-25 21:22:49 +0530672
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200673 int direntlen = le16_to_cpu(dir->direntlen);
674 if (direntlen < sizeof(struct ext2_dirent))
Uma Shankared34f342012-05-25 21:22:49 +0530675 break;
676
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200677 if (dir->inode && (strlen(dirname) == dir->namelen) &&
678 (strncmp(dirname, direntname, dir->namelen) == 0)) {
679 inodeno = le32_to_cpu(dir->inode);
680 break;
681 }
Uma Shankared34f342012-05-25 21:22:49 +0530682
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200683 offset += direntlen;
Stefan Brüns76a29512016-09-06 04:36:41 +0200684
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200685 } while (offset < fs->blksz);
686
687 if (inodeno > 0) {
688 free(block_buffer);
Stefan Brüns76a29512016-09-06 04:36:41 +0200689 return inodeno;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200690 }
Uma Shankared34f342012-05-25 21:22:49 +0530691 }
692
693fail:
694 free(block_buffer);
695
696 return -1;
697}
698
699static int find_dir_depth(char *dirname)
700{
701 char *token = strtok(dirname, "/");
702 int count = 0;
703 while (token != NULL) {
704 token = strtok(NULL, "/");
705 count++;
706 }
707 return count + 1 + 1;
708 /*
709 * for example for string /home/temp
710 * depth=home(1)+temp(1)+1 extra for NULL;
711 * so count is 4;
712 */
713}
714
715static int parse_path(char **arr, char *dirname)
716{
717 char *token = strtok(dirname, "/");
718 int i = 0;
719
720 /* add root */
721 arr[i] = zalloc(strlen("/") + 1);
722 if (!arr[i])
723 return -ENOMEM;
Stephen Warren934b14f2015-09-04 22:03:44 -0600724 memcpy(arr[i++], "/", strlen("/"));
Uma Shankared34f342012-05-25 21:22:49 +0530725
726 /* add each path entry after root */
727 while (token != NULL) {
728 arr[i] = zalloc(strlen(token) + 1);
729 if (!arr[i])
730 return -ENOMEM;
731 memcpy(arr[i++], token, strlen(token));
732 token = strtok(NULL, "/");
733 }
734 arr[i] = NULL;
735
736 return 0;
737}
738
739int ext4fs_iget(int inode_no, struct ext2_inode *inode)
740{
741 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
742 return -1;
743
744 return 0;
745}
746
747/*
748 * Function: ext4fs_get_parent_inode_num
749 * Return Value: inode Number of the parent directory of file/Directory to be
750 * created
751 * dirname : Input parmater, input path name of the file/directory to be created
752 * dname : Output parameter, to be filled with the name of the directory
753 * extracted from dirname
754 */
755int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
756{
757 int i;
758 int depth = 0;
759 int matched_inode_no;
760 int result_inode_no = -1;
761 char **ptr = NULL;
762 char *depth_dirname = NULL;
763 char *parse_dirname = NULL;
764 struct ext2_inode *parent_inode = NULL;
765 struct ext2_inode *first_inode = NULL;
766 struct ext2_inode temp_inode;
767
768 if (*dirname != '/') {
769 printf("Please supply Absolute path\n");
770 return -1;
771 }
772
773 /* TODO: input validation make equivalent to linux */
774 depth_dirname = zalloc(strlen(dirname) + 1);
775 if (!depth_dirname)
776 return -ENOMEM;
777
778 memcpy(depth_dirname, dirname, strlen(dirname));
779 depth = find_dir_depth(depth_dirname);
780 parse_dirname = zalloc(strlen(dirname) + 1);
781 if (!parse_dirname)
782 goto fail;
783 memcpy(parse_dirname, dirname, strlen(dirname));
784
785 /* allocate memory for each directory level */
786 ptr = zalloc((depth) * sizeof(char *));
787 if (!ptr)
788 goto fail;
789 if (parse_path(ptr, parse_dirname))
790 goto fail;
791 parent_inode = zalloc(sizeof(struct ext2_inode));
792 if (!parent_inode)
793 goto fail;
794 first_inode = zalloc(sizeof(struct ext2_inode));
795 if (!first_inode)
796 goto fail;
797 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
798 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
799 if (flags & F_FILE)
800 result_inode_no = EXT2_ROOT_INO;
801 for (i = 1; i < depth; i++) {
802 matched_inode_no = search_dir(parent_inode, ptr[i]);
803 if (matched_inode_no == -1) {
804 if (ptr[i + 1] == NULL && i == 1) {
805 result_inode_no = EXT2_ROOT_INO;
806 goto end;
807 } else {
808 if (ptr[i + 1] == NULL)
809 break;
810 printf("Invalid path\n");
811 result_inode_no = -1;
812 goto fail;
813 }
814 } else {
815 if (ptr[i + 1] != NULL) {
816 memset(parent_inode, '\0',
817 sizeof(struct ext2_inode));
818 if (ext4fs_iget(matched_inode_no,
819 parent_inode)) {
820 result_inode_no = -1;
821 goto fail;
822 }
823 result_inode_no = matched_inode_no;
824 } else {
825 break;
826 }
827 }
828 }
829
830end:
831 if (i == 1)
832 matched_inode_no = search_dir(first_inode, ptr[i]);
833 else
834 matched_inode_no = search_dir(parent_inode, ptr[i]);
835
836 if (matched_inode_no != -1) {
837 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200838 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankared34f342012-05-25 21:22:49 +0530839 printf("It is a Directory\n");
840 result_inode_no = -1;
841 goto fail;
842 }
843 }
844
845 if (strlen(ptr[i]) > 256) {
846 result_inode_no = -1;
847 goto fail;
848 }
849 memcpy(dname, ptr[i], strlen(ptr[i]));
850
851fail:
852 free(depth_dirname);
Mikhail Ilin04e63322022-11-22 11:00:55 +0300853 if (parse_dirname)
854 free(parse_dirname);
855 if (ptr) {
856 for (i = 0; i < depth; i++) {
857 if (!ptr[i])
858 break;
859 free(ptr[i]);
860 }
861 free(ptr);
Stephen Warren934b14f2015-09-04 22:03:44 -0600862 }
Mikhail Ilin04e63322022-11-22 11:00:55 +0300863 if (parent_inode)
864 free(parent_inode);
865 if (first_inode)
866 free(first_inode);
Uma Shankared34f342012-05-25 21:22:49 +0530867
868 return result_inode_no;
869}
870
Stefan Brüns76a29512016-09-06 04:36:41 +0200871static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankared34f342012-05-25 21:22:49 +0530872{
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200873 int status;
874 int inodeno = 0;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200875 int offset;
876 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530877 struct ext2_dirent *dir = NULL;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200878 struct ext2_dirent *previous_dir;
Uma Shankared34f342012-05-25 21:22:49 +0530879 struct ext_filesystem *fs = get_fs();
Stephen Warrend56b2012015-09-04 22:03:45 -0600880 int ret = -1;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200881 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530882
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200883 block_buffer = zalloc(fs->blksz);
884 if (!block_buffer)
Uma Shankared34f342012-05-25 21:22:49 +0530885 return -ENOMEM;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200886
887 /* read the directory block */
Stefan Brüns76a29512016-09-06 04:36:41 +0200888 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200889 fs->blksz, block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530890 if (status == 0)
891 goto fail;
892
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200893 offset = 0;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200894 do {
Ian Rayecdfb412017-11-08 15:35:10 +0000895 if (offset & 3) {
896 printf("Badly aligned ext2_dirent\n");
897 break;
898 }
899
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200900 previous_dir = dir;
901 dir = (struct ext2_dirent *)(block_buffer + offset);
902 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
903
904 int direntlen = le16_to_cpu(dir->direntlen);
905 if (direntlen < sizeof(struct ext2_dirent))
906 break;
907
Stefan Brüns76a29512016-09-06 04:36:41 +0200908 if (dir->inode && (strlen(filename) == dir->namelen) &&
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200909 (strncmp(direntname, filename, dir->namelen) == 0)) {
Stefan Brüns76a29512016-09-06 04:36:41 +0200910 inodeno = le32_to_cpu(dir->inode);
Stefan Brüns76a29512016-09-06 04:36:41 +0200911 break;
Uma Shankared34f342012-05-25 21:22:49 +0530912 }
913
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200914 offset += direntlen;
Uma Shankared34f342012-05-25 21:22:49 +0530915
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200916 } while (offset < fs->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530917
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200918 if (inodeno > 0) {
Stefan Brüns805e3e002016-10-09 20:15:28 +0200919 printf("file found, deleting\n");
920 if (ext4fs_log_journal(block_buffer, blknr))
921 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530922
Stefan Brüns805e3e002016-10-09 20:15:28 +0200923 if (previous_dir) {
924 /* merge dir entry with predecessor */
925 uint16_t new_len;
926 new_len = le16_to_cpu(previous_dir->direntlen);
927 new_len += le16_to_cpu(dir->direntlen);
928 previous_dir->direntlen = cpu_to_le16(new_len);
929 } else {
930 /* invalidate dir entry */
931 dir->inode = 0;
932 }
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200933 if (ext4fs_put_metadata(block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530934 goto fail;
Stephen Warrend56b2012015-09-04 22:03:45 -0600935 ret = inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530936 }
937fail:
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200938 free(block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530939
Stephen Warrend56b2012015-09-04 22:03:45 -0600940 return ret;
Uma Shankared34f342012-05-25 21:22:49 +0530941}
942
Stefan Brüns76a29512016-09-06 04:36:41 +0200943int ext4fs_filename_unlink(char *filename)
Uma Shankared34f342012-05-25 21:22:49 +0530944{
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200945 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530946 long int blknr = -1;
947 int inodeno = -1;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200948 uint32_t directory_blocks;
949
950 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
951 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankared34f342012-05-25 21:22:49 +0530952
953 /* read the block no allocated to a file */
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200954 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
Stephen Warrend5aee652019-01-30 12:58:05 -0700955 blknr = read_allocated_block(g_parent_inode, blk_idx, NULL);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200956 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530957 break;
Stefan Brüns76a29512016-09-06 04:36:41 +0200958 inodeno = unlink_filename(filename, blknr);
Uma Shankared34f342012-05-25 21:22:49 +0530959 if (inodeno != -1)
960 return inodeno;
961 }
962
963 return -1;
964}
965
Michael Walle58a9ecb2016-09-01 11:21:40 +0200966uint32_t ext4fs_get_new_blk_no(void)
Uma Shankared34f342012-05-25 21:22:49 +0530967{
968 short i;
969 short status;
970 int remainder;
971 unsigned int bg_idx;
972 static int prev_bg_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200973 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankared34f342012-05-25 21:22:49 +0530974 struct ext_filesystem *fs = get_fs();
975 char *journal_buffer = zalloc(fs->blksz);
976 char *zero_buffer = zalloc(fs->blksz);
977 if (!journal_buffer || !zero_buffer)
978 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530979
980 if (fs->first_pass_bbmap == 0) {
981 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200982 struct ext2_block_group *bgd = NULL;
983 bgd = ext4fs_get_group_descriptor(fs, i);
984 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
985 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
986 uint64_t b_bitmap_blk =
987 ext4fs_bg_get_block_id(bgd, fs);
988 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +0530989 memcpy(fs->blk_bmaps[i], zero_buffer,
990 fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200991 put_ext4(b_bitmap_blk * fs->blksz,
992 fs->blk_bmaps[i], fs->blksz);
993 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
994 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +0530995 }
996 fs->curr_blkno =
997 _get_new_blk_no(fs->blk_bmaps[i]);
998 if (fs->curr_blkno == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +0200999 /* block bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +05301000 continue;
1001 fs->curr_blkno = fs->curr_blkno +
1002 (i * fs->blksz * 8);
1003 fs->first_pass_bbmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +02001004 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001005 ext4fs_sb_free_blocks_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001006 status = ext4fs_devread(b_bitmap_blk *
1007 fs->sect_perblk,
1008 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301009 journal_buffer);
1010 if (status == 0)
1011 goto fail;
1012 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001013 b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301014 goto fail;
1015 goto success;
1016 } else {
1017 debug("no space left on block group %d\n", i);
1018 }
1019 }
1020
1021 goto fail;
1022 } else {
Uma Shankared34f342012-05-25 21:22:49 +05301023 fs->curr_blkno++;
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001024restart:
Uma Shankared34f342012-05-25 21:22:49 +05301025 /* get the blockbitmap index respective to blockno */
Łukasz Majewski35dd0552014-05-06 09:36:04 +02001026 bg_idx = fs->curr_blkno / blk_per_grp;
1027 if (fs->blksz == 1024) {
Uma Shankared34f342012-05-25 21:22:49 +05301028 remainder = fs->curr_blkno % blk_per_grp;
1029 if (!remainder)
1030 bg_idx--;
1031 }
1032
1033 /*
1034 * To skip completely filled block group bitmaps
1035 * Optimize the block allocation
1036 */
1037 if (bg_idx >= fs->no_blkgrp)
1038 goto fail;
1039
Stefan Brüns688d0e72016-09-17 02:10:10 +02001040 struct ext2_block_group *bgd = NULL;
1041 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1042 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
Uma Shankared34f342012-05-25 21:22:49 +05301043 debug("block group %u is full. Skipping\n", bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001044 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1045 if (fs->blksz == 1024)
1046 fs->curr_blkno += 1;
Uma Shankared34f342012-05-25 21:22:49 +05301047 goto restart;
1048 }
1049
Stefan Brüns688d0e72016-09-17 02:10:10 +02001050 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1051 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1052 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +05301053 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001054 put_ext4(b_bitmap_blk * fs->blksz,
1055 zero_buffer, fs->blksz);
1056 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1057 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301058 }
1059
1060 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1061 bg_idx) != 0) {
1062 debug("going for restart for the block no %ld %u\n",
1063 fs->curr_blkno, bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001064 fs->curr_blkno++;
Uma Shankared34f342012-05-25 21:22:49 +05301065 goto restart;
1066 }
1067
1068 /* journal backup */
1069 if (prev_bg_bitmap_index != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001070 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301071 0, fs->blksz, journal_buffer);
1072 if (status == 0)
1073 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001074 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301075 goto fail;
1076
1077 prev_bg_bitmap_index = bg_idx;
1078 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001079 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001080 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301081 goto success;
1082 }
1083success:
1084 free(journal_buffer);
1085 free(zero_buffer);
1086
1087 return fs->curr_blkno;
1088fail:
1089 free(journal_buffer);
1090 free(zero_buffer);
1091
1092 return -1;
1093}
1094
1095int ext4fs_get_new_inode_no(void)
1096{
1097 short i;
1098 short status;
1099 unsigned int ibmap_idx;
1100 static int prev_inode_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001101 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankared34f342012-05-25 21:22:49 +05301102 struct ext_filesystem *fs = get_fs();
1103 char *journal_buffer = zalloc(fs->blksz);
1104 char *zero_buffer = zalloc(fs->blksz);
1105 if (!journal_buffer || !zero_buffer)
1106 goto fail;
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001107 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1108 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
Uma Shankared34f342012-05-25 21:22:49 +05301109
1110 if (fs->first_pass_ibmap == 0) {
1111 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001112 uint32_t free_inodes;
1113 struct ext2_block_group *bgd = NULL;
1114 bgd = ext4fs_get_group_descriptor(fs, i);
1115 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1116 if (free_inodes) {
1117 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1118 uint64_t i_bitmap_blk =
1119 ext4fs_bg_get_inode_id(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001120 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001121 bgd->bg_itable_unused = free_inodes;
1122 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1123 put_ext4(i_bitmap_blk * fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301124 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001125 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1126 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301127 memcpy(fs->inode_bmaps[i],
1128 zero_buffer, fs->blksz);
1129 }
1130 fs->curr_inode_no =
1131 _get_new_inode_no(fs->inode_bmaps[i]);
1132 if (fs->curr_inode_no == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001133 /* inode bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +05301134 continue;
1135 fs->curr_inode_no = fs->curr_inode_no +
1136 (i * inodes_per_grp);
1137 fs->first_pass_ibmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +02001138 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001139 if (has_gdt_chksum)
Stefan Brüns749e93e2016-09-20 01:13:01 +02001140 ext4fs_bg_itable_unused_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001141 ext4fs_sb_free_inodes_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001142 status = ext4fs_devread(i_bitmap_blk *
1143 fs->sect_perblk,
1144 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301145 journal_buffer);
1146 if (status == 0)
1147 goto fail;
1148 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001149 i_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301150 goto fail;
1151 goto success;
1152 } else
1153 debug("no inode left on block group %d\n", i);
1154 }
1155 goto fail;
1156 } else {
1157restart:
1158 fs->curr_inode_no++;
1159 /* get the blockbitmap index respective to blockno */
1160 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001161 struct ext2_block_group *bgd =
1162 ext4fs_get_group_descriptor(fs, ibmap_idx);
1163 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1164 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1165
1166 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1167 put_ext4(i_bitmap_blk * fs->blksz,
Michael Walle58a9ecb2016-09-01 11:21:40 +02001168 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001169 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1170 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301171 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1172 fs->blksz);
1173 }
1174
1175 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1176 fs->inode_bmaps[ibmap_idx],
1177 ibmap_idx) != 0) {
1178 debug("going for restart for the block no %d %u\n",
1179 fs->curr_inode_no, ibmap_idx);
1180 goto restart;
1181 }
1182
1183 /* journal backup */
1184 if (prev_inode_bitmap_index != ibmap_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001185 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301186 0, fs->blksz, journal_buffer);
1187 if (status == 0)
1188 goto fail;
1189 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001190 le32_to_cpu(bgd->inode_id)))
Uma Shankared34f342012-05-25 21:22:49 +05301191 goto fail;
1192 prev_inode_bitmap_index = ibmap_idx;
1193 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001194 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001195 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001196 bgd->bg_itable_unused = bgd->free_inodes;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001197 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301198 goto success;
1199 }
1200
1201success:
1202 free(journal_buffer);
1203 free(zero_buffer);
1204
1205 return fs->curr_inode_no;
1206fail:
1207 free(journal_buffer);
1208 free(zero_buffer);
1209
1210 return -1;
1211
1212}
1213
1214
1215static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1216 unsigned int *total_remaining_blocks,
1217 unsigned int *no_blks_reqd)
1218{
1219 short i;
1220 short status;
1221 long int actual_block_no;
1222 long int si_blockno;
1223 /* si :single indirect */
Michael Walle58a9ecb2016-09-01 11:21:40 +02001224 __le32 *si_buffer = NULL;
1225 __le32 *si_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301226 struct ext_filesystem *fs = get_fs();
1227
1228 if (*total_remaining_blocks != 0) {
1229 si_buffer = zalloc(fs->blksz);
1230 if (!si_buffer) {
1231 printf("No Memory\n");
1232 return;
1233 }
1234 si_start_addr = si_buffer;
1235 si_blockno = ext4fs_get_new_blk_no();
1236 if (si_blockno == -1) {
1237 printf("no block left to assign\n");
1238 goto fail;
1239 }
1240 (*no_blks_reqd)++;
1241 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1242
Frederic Leroy04735e92013-06-26 18:11:25 +02001243 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301244 0, fs->blksz, (char *)si_buffer);
1245 memset(si_buffer, '\0', fs->blksz);
1246 if (status == 0)
1247 goto fail;
1248
1249 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1250 actual_block_no = ext4fs_get_new_blk_no();
1251 if (actual_block_no == -1) {
1252 printf("no block left to assign\n");
1253 goto fail;
1254 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001255 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301256 debug("SIAB %u: %u\n", *si_buffer,
1257 *total_remaining_blocks);
1258
1259 si_buffer++;
1260 (*total_remaining_blocks)--;
1261 if (*total_remaining_blocks == 0)
1262 break;
1263 }
1264
1265 /* write the block to disk */
Ma Haijun05508702014-01-08 08:15:33 +08001266 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301267 si_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001268 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301269 }
1270fail:
1271 free(si_start_addr);
1272}
1273
1274static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1275 unsigned int *total_remaining_blocks,
1276 unsigned int *no_blks_reqd)
1277{
1278 short i;
1279 short j;
1280 short status;
1281 long int actual_block_no;
1282 /* di:double indirect */
1283 long int di_blockno_parent;
1284 long int di_blockno_child;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001285 __le32 *di_parent_buffer = NULL;
1286 __le32 *di_child_buff = NULL;
1287 __le32 *di_block_start_addr = NULL;
1288 __le32 *di_child_buff_start = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301289 struct ext_filesystem *fs = get_fs();
1290
1291 if (*total_remaining_blocks != 0) {
1292 /* double indirect parent block connecting to inode */
1293 di_blockno_parent = ext4fs_get_new_blk_no();
1294 if (di_blockno_parent == -1) {
1295 printf("no block left to assign\n");
1296 goto fail;
1297 }
1298 di_parent_buffer = zalloc(fs->blksz);
1299 if (!di_parent_buffer)
1300 goto fail;
1301
1302 di_block_start_addr = di_parent_buffer;
1303 (*no_blks_reqd)++;
1304 debug("DIPB %ld: %u\n", di_blockno_parent,
1305 *total_remaining_blocks);
1306
Frederic Leroy04735e92013-06-26 18:11:25 +02001307 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankared34f342012-05-25 21:22:49 +05301308 fs->sect_perblk, 0,
1309 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001310
1311 if (!status) {
1312 printf("%s: Device read error!\n", __func__);
1313 goto fail;
1314 }
Uma Shankared34f342012-05-25 21:22:49 +05301315 memset(di_parent_buffer, '\0', fs->blksz);
1316
1317 /*
1318 * start:for each double indirect parent
1319 * block create one more block
1320 */
1321 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1322 di_blockno_child = ext4fs_get_new_blk_no();
1323 if (di_blockno_child == -1) {
1324 printf("no block left to assign\n");
1325 goto fail;
1326 }
1327 di_child_buff = zalloc(fs->blksz);
1328 if (!di_child_buff)
1329 goto fail;
1330
1331 di_child_buff_start = di_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001332 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankared34f342012-05-25 21:22:49 +05301333 di_parent_buffer++;
1334 (*no_blks_reqd)++;
1335 debug("DICB %ld: %u\n", di_blockno_child,
1336 *total_remaining_blocks);
1337
Frederic Leroy04735e92013-06-26 18:11:25 +02001338 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankared34f342012-05-25 21:22:49 +05301339 fs->sect_perblk, 0,
1340 fs->blksz,
1341 (char *)di_child_buff);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001342
1343 if (!status) {
1344 printf("%s: Device read error!\n", __func__);
1345 goto fail;
1346 }
Uma Shankared34f342012-05-25 21:22:49 +05301347 memset(di_child_buff, '\0', fs->blksz);
1348 /* filling of actual datablocks for each child */
1349 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1350 actual_block_no = ext4fs_get_new_blk_no();
1351 if (actual_block_no == -1) {
1352 printf("no block left to assign\n");
1353 goto fail;
1354 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001355 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301356 debug("DIAB %ld: %u\n", actual_block_no,
1357 *total_remaining_blocks);
1358
1359 di_child_buff++;
1360 (*total_remaining_blocks)--;
1361 if (*total_remaining_blocks == 0)
1362 break;
1363 }
1364 /* write the block table */
Ma Haijun05508702014-01-08 08:15:33 +08001365 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301366 di_child_buff_start, fs->blksz);
1367 free(di_child_buff_start);
1368 di_child_buff_start = NULL;
1369
1370 if (*total_remaining_blocks == 0)
1371 break;
1372 }
Ma Haijun05508702014-01-08 08:15:33 +08001373 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301374 di_block_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001375 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankared34f342012-05-25 21:22:49 +05301376 }
1377fail:
1378 free(di_block_start_addr);
1379}
1380
1381static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1382 unsigned int *total_remaining_blocks,
1383 unsigned int *no_blks_reqd)
1384{
1385 short i;
1386 short j;
1387 short k;
1388 long int actual_block_no;
1389 /* ti: Triple Indirect */
1390 long int ti_gp_blockno;
1391 long int ti_parent_blockno;
1392 long int ti_child_blockno;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001393 __le32 *ti_gp_buff = NULL;
1394 __le32 *ti_parent_buff = NULL;
1395 __le32 *ti_child_buff = NULL;
1396 __le32 *ti_gp_buff_start_addr = NULL;
1397 __le32 *ti_pbuff_start_addr = NULL;
1398 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301399 struct ext_filesystem *fs = get_fs();
1400 if (*total_remaining_blocks != 0) {
1401 /* triple indirect grand parent block connecting to inode */
1402 ti_gp_blockno = ext4fs_get_new_blk_no();
1403 if (ti_gp_blockno == -1) {
1404 printf("no block left to assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001405 return;
Uma Shankared34f342012-05-25 21:22:49 +05301406 }
1407 ti_gp_buff = zalloc(fs->blksz);
1408 if (!ti_gp_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001409 return;
Uma Shankared34f342012-05-25 21:22:49 +05301410
1411 ti_gp_buff_start_addr = ti_gp_buff;
1412 (*no_blks_reqd)++;
1413 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1414 *total_remaining_blocks);
1415
1416 /* for each 4 byte grand parent entry create one more block */
1417 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1418 ti_parent_blockno = ext4fs_get_new_blk_no();
1419 if (ti_parent_blockno == -1) {
1420 printf("no block left to assign\n");
1421 goto fail;
1422 }
1423 ti_parent_buff = zalloc(fs->blksz);
1424 if (!ti_parent_buff)
1425 goto fail;
1426
1427 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001428 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301429 ti_gp_buff++;
1430 (*no_blks_reqd)++;
1431 debug("TIPB %ld: %u\n", ti_parent_blockno,
1432 *total_remaining_blocks);
1433
1434 /* for each 4 byte entry parent create one more block */
1435 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1436 ti_child_blockno = ext4fs_get_new_blk_no();
1437 if (ti_child_blockno == -1) {
1438 printf("no block left assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001439 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301440 }
1441 ti_child_buff = zalloc(fs->blksz);
1442 if (!ti_child_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001443 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301444
1445 ti_cbuff_start_addr = ti_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001446 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301447 ti_parent_buff++;
1448 (*no_blks_reqd)++;
1449 debug("TICB %ld: %u\n", ti_parent_blockno,
1450 *total_remaining_blocks);
1451
1452 /* fill actual datablocks for each child */
1453 for (k = 0; k < (fs->blksz / sizeof(int));
1454 k++) {
1455 actual_block_no =
1456 ext4fs_get_new_blk_no();
1457 if (actual_block_no == -1) {
1458 printf("no block left\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001459 free(ti_cbuff_start_addr);
1460 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301461 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001462 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301463 debug("TIAB %ld: %u\n", actual_block_no,
1464 *total_remaining_blocks);
1465
1466 ti_child_buff++;
1467 (*total_remaining_blocks)--;
1468 if (*total_remaining_blocks == 0)
1469 break;
1470 }
1471 /* write the child block */
Ma Haijun05508702014-01-08 08:15:33 +08001472 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1473 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301474 ti_cbuff_start_addr, fs->blksz);
1475 free(ti_cbuff_start_addr);
1476
1477 if (*total_remaining_blocks == 0)
1478 break;
1479 }
1480 /* write the parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001481 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301482 ti_pbuff_start_addr, fs->blksz);
1483 free(ti_pbuff_start_addr);
1484
1485 if (*total_remaining_blocks == 0)
1486 break;
1487 }
1488 /* write the grand parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001489 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301490 ti_gp_buff_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001491 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini495c3a12015-12-10 16:42:21 -05001492 free(ti_gp_buff_start_addr);
1493 return;
Uma Shankared34f342012-05-25 21:22:49 +05301494 }
Tom Rini495c3a12015-12-10 16:42:21 -05001495fail1:
1496 free(ti_pbuff_start_addr);
Uma Shankared34f342012-05-25 21:22:49 +05301497fail:
1498 free(ti_gp_buff_start_addr);
1499}
1500
1501void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1502 unsigned int total_remaining_blocks,
1503 unsigned int *total_no_of_block)
1504{
1505 short i;
1506 long int direct_blockno;
1507 unsigned int no_blks_reqd = 0;
1508
1509 /* allocation of direct blocks */
Stephen Warrend0180282014-06-11 12:46:16 -06001510 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankared34f342012-05-25 21:22:49 +05301511 direct_blockno = ext4fs_get_new_blk_no();
1512 if (direct_blockno == -1) {
1513 printf("no block left to assign\n");
1514 return;
1515 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001516 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301517 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1518
1519 total_remaining_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +05301520 }
1521
1522 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1523 &no_blks_reqd);
1524 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1525 &no_blks_reqd);
1526 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1527 &no_blks_reqd);
1528 *total_no_of_block += no_blks_reqd;
1529}
1530
1531#endif
1532
Uma Shankara1596432012-05-25 21:21:44 +05301533static struct ext4_extent_header *ext4fs_get_extent_block
Stephen Warrend5aee652019-01-30 12:58:05 -07001534 (struct ext2_data *data, struct ext_block_cache *cache,
Uma Shankara1596432012-05-25 21:21:44 +05301535 struct ext4_extent_header *ext_block,
1536 uint32_t fileblock, int log2_blksz)
1537{
1538 struct ext4_extent_idx *index;
1539 unsigned long long block;
Ionut Nicu47017322014-01-13 11:59:24 +01001540 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankara1596432012-05-25 21:21:44 +05301541 int i;
1542
1543 while (1) {
1544 index = (struct ext4_extent_idx *)(ext_block + 1);
1545
Rommel Custodio8b415f72013-07-21 10:53:25 +02001546 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001547 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301548
1549 if (ext_block->eh_depth == 0)
1550 return ext_block;
1551 i = -1;
1552 do {
1553 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001554 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301555 break;
Ionut Nicub5bbac12014-01-13 12:00:08 +01001556 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankara1596432012-05-25 21:21:44 +05301557
Gero Schumacher1c48fda2019-02-26 15:45:22 +00001558 /*
1559 * If first logical block number is higher than requested fileblock,
1560 * it is a sparse file. This is handled on upper layer.
1561 */
1562 if (i > 0)
1563 i--;
Uma Shankara1596432012-05-25 21:21:44 +05301564
Rommel Custodio8b415f72013-07-21 10:53:25 +02001565 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301566 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
Stephen Warrend5aee652019-01-30 12:58:05 -07001567 block <<= log2_blksz;
1568 if (!ext_cache_read(cache, (lbaint_t)block, blksz))
Michael Walle58a9ecb2016-09-01 11:21:40 +02001569 return NULL;
Stephen Warrend5aee652019-01-30 12:58:05 -07001570 ext_block = (struct ext4_extent_header *)cache->buf;
Uma Shankara1596432012-05-25 21:21:44 +05301571 }
1572}
1573
1574static int ext4fs_blockgroup
1575 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1576{
1577 long int blkno;
1578 unsigned int blkoff, desc_per_blk;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001579 int log2blksz = get_fs()->dev_desc->log2blksz;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001580 int desc_size = get_fs()->gdsize;
Uma Shankara1596432012-05-25 21:21:44 +05301581
Paul Emge084be432019-07-08 16:37:06 -07001582 if (desc_size == 0)
1583 return 0;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001584 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301585
Paul Emge084be432019-07-08 16:37:06 -07001586 if (desc_per_blk == 0)
1587 return 0;
Michael Walle7f101be2016-08-29 10:46:44 +02001588 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankara1596432012-05-25 21:21:44 +05301589 group / desc_per_blk;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001590 blkoff = (group % desc_per_blk) * desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301591
1592 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1593 group, blkno, blkoff);
1594
Frederic Leroy04735e92013-06-26 18:11:25 +02001595 return ext4fs_devread((lbaint_t)blkno <<
1596 (LOG2_BLOCK_SIZE(data) - log2blksz),
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001597 blkoff, desc_size, (char *)blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301598}
1599
1600int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1601{
Benjamin Limfebbc582019-03-29 07:29:45 -04001602 struct ext2_block_group *blkgrp;
Uma Shankara1596432012-05-25 21:21:44 +05301603 struct ext2_sblock *sblock = &data->sblock;
1604 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00001605 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301606 int inodes_per_block, status;
1607 long int blkno;
1608 unsigned int blkoff;
1609
Benjamin Limfebbc582019-03-29 07:29:45 -04001610 /* Allocate blkgrp based on gdsize (for 64-bit support). */
1611 blkgrp = zalloc(get_fs()->gdsize);
1612 if (!blkgrp)
1613 return 0;
1614
Uma Shankara1596432012-05-25 21:21:44 +05301615 /* It is easier to calculate if the first inode is 0. */
1616 ino--;
Paul Emge084be432019-07-08 16:37:06 -07001617 if ( le32_to_cpu(sblock->inodes_per_group) == 0 || fs->inodesz == 0) {
1618 free(blkgrp);
1619 return 0;
1620 }
Michael Walle7f101be2016-08-29 10:46:44 +02001621 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Benjamin Limfebbc582019-03-29 07:29:45 -04001622 (sblock->inodes_per_group), blkgrp);
1623 if (status == 0) {
1624 free(blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301625 return 0;
Benjamin Limfebbc582019-03-29 07:29:45 -04001626 }
Uma Shankara1596432012-05-25 21:21:44 +05301627
1628 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Paul Emge084be432019-07-08 16:37:06 -07001629 if ( inodes_per_block == 0 ) {
1630 free(blkgrp);
1631 return 0;
1632 }
Benjamin Limfebbc582019-03-29 07:29:45 -04001633 blkno = ext4fs_bg_get_inode_table_id(blkgrp, fs) +
Michael Walle7f101be2016-08-29 10:46:44 +02001634 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankara1596432012-05-25 21:21:44 +05301635 blkoff = (ino % inodes_per_block) * fs->inodesz;
Benjamin Limfebbc582019-03-29 07:29:45 -04001636
1637 /* Free blkgrp as it is no longer required. */
1638 free(blkgrp);
1639
Uma Shankara1596432012-05-25 21:21:44 +05301640 /* Read the inode. */
Frederic Leroy04735e92013-06-26 18:11:25 +02001641 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1642 log2blksz), blkoff,
Uma Shankara1596432012-05-25 21:21:44 +05301643 sizeof(struct ext2_inode), (char *)inode);
1644 if (status == 0)
1645 return 0;
1646
1647 return 1;
1648}
1649
Stephen Warrend5aee652019-01-30 12:58:05 -07001650long int read_allocated_block(struct ext2_inode *inode, int fileblock,
1651 struct ext_block_cache *cache)
Uma Shankara1596432012-05-25 21:21:44 +05301652{
1653 long int blknr;
1654 int blksz;
1655 int log2_blksz;
1656 int status;
1657 long int rblock;
1658 long int perblock_parent;
1659 long int perblock_child;
1660 unsigned long long start;
1661 /* get the blocksize of the filesystem */
1662 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +00001663 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1664 - get_fs()->dev_desc->log2blksz;
1665
Uma Shankara1596432012-05-25 21:21:44 +05301666 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
Stefan Brünsf81db562016-11-05 22:17:14 +01001667 long int startblock, endblock;
Stephen Warrend5aee652019-01-30 12:58:05 -07001668 struct ext_block_cache *c, cd;
Uma Shankara1596432012-05-25 21:21:44 +05301669 struct ext4_extent_header *ext_block;
1670 struct ext4_extent *extent;
Stefan Brünsf81db562016-11-05 22:17:14 +01001671 int i;
Stephen Warrend5aee652019-01-30 12:58:05 -07001672
1673 if (cache) {
1674 c = cache;
1675 } else {
1676 c = &cd;
1677 ext_cache_init(c);
1678 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001679 ext_block =
Stephen Warrend5aee652019-01-30 12:58:05 -07001680 ext4fs_get_extent_block(ext4fs_root, c,
Egbert Eich50ce4c02013-05-01 01:13:19 +00001681 (struct ext4_extent_header *)
1682 inode->b.blocks.dir_blocks,
1683 fileblock, log2_blksz);
Uma Shankara1596432012-05-25 21:21:44 +05301684 if (!ext_block) {
1685 printf("invalid extent block\n");
Stephen Warrend5aee652019-01-30 12:58:05 -07001686 if (!cache)
1687 ext_cache_fini(c);
Uma Shankara1596432012-05-25 21:21:44 +05301688 return -EINVAL;
1689 }
1690
1691 extent = (struct ext4_extent *)(ext_block + 1);
1692
Stefan Brünsf81db562016-11-05 22:17:14 +01001693 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1694 startblock = le32_to_cpu(extent[i].ee_block);
1695 endblock = startblock + le16_to_cpu(extent[i].ee_len);
1696
1697 if (startblock > fileblock) {
1698 /* Sparse file */
Stephen Warrend5aee652019-01-30 12:58:05 -07001699 if (!cache)
1700 ext_cache_fini(c);
Uma Shankara1596432012-05-25 21:21:44 +05301701 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05301702
Stefan Brünsf81db562016-11-05 22:17:14 +01001703 } else if (fileblock < endblock) {
1704 start = le16_to_cpu(extent[i].ee_start_hi);
1705 start = (start << 32) +
Uma Shankara1596432012-05-25 21:21:44 +05301706 le32_to_cpu(extent[i].ee_start_lo);
Stephen Warrend5aee652019-01-30 12:58:05 -07001707 if (!cache)
1708 ext_cache_fini(c);
Stefan Brünsf81db562016-11-05 22:17:14 +01001709 return (fileblock - startblock) + start;
1710 }
Uma Shankara1596432012-05-25 21:21:44 +05301711 }
1712
Stephen Warrend5aee652019-01-30 12:58:05 -07001713 if (!cache)
1714 ext_cache_fini(c);
Stefan Brünsf81db562016-11-05 22:17:14 +01001715 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05301716 }
1717
1718 /* Direct blocks. */
1719 if (fileblock < INDIRECT_BLOCKS)
Michael Walle7f101be2016-08-29 10:46:44 +02001720 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301721
1722 /* Indirect. */
1723 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1724 if (ext4fs_indir1_block == NULL) {
1725 ext4fs_indir1_block = zalloc(blksz);
1726 if (ext4fs_indir1_block == NULL) {
1727 printf("** SI ext2fs read block (indir 1)"
1728 "malloc failed. **\n");
1729 return -1;
1730 }
1731 ext4fs_indir1_size = blksz;
1732 ext4fs_indir1_blkno = -1;
1733 }
1734 if (blksz != ext4fs_indir1_size) {
1735 free(ext4fs_indir1_block);
1736 ext4fs_indir1_block = NULL;
1737 ext4fs_indir1_size = 0;
1738 ext4fs_indir1_blkno = -1;
1739 ext4fs_indir1_block = zalloc(blksz);
1740 if (ext4fs_indir1_block == NULL) {
1741 printf("** SI ext2fs read block (indir 1):"
1742 "malloc failed. **\n");
1743 return -1;
1744 }
1745 ext4fs_indir1_size = blksz;
1746 }
Michael Walle7f101be2016-08-29 10:46:44 +02001747 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301748 log2_blksz) != ext4fs_indir1_blkno) {
1749 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001750 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301751 (inode->b.blocks.
1752 indir_block) << log2_blksz, 0,
1753 blksz, (char *)ext4fs_indir1_block);
1754 if (status == 0) {
1755 printf("** SI ext2fs read block (indir 1)"
1756 "failed. **\n");
Stefan Brünsde9e8312016-09-06 04:36:55 +02001757 return -1;
Uma Shankara1596432012-05-25 21:21:44 +05301758 }
1759 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001760 le32_to_cpu(inode->b.blocks.
Uma Shankara1596432012-05-25 21:21:44 +05301761 indir_block) << log2_blksz;
1762 }
Michael Walle7f101be2016-08-29 10:46:44 +02001763 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankara1596432012-05-25 21:21:44 +05301764 [fileblock - INDIRECT_BLOCKS]);
1765 }
1766 /* Double indirect. */
1767 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1768 (blksz / 4 + 1)))) {
1769
1770 long int perblock = blksz / 4;
1771 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1772
1773 if (ext4fs_indir1_block == NULL) {
1774 ext4fs_indir1_block = zalloc(blksz);
1775 if (ext4fs_indir1_block == NULL) {
1776 printf("** DI ext2fs read block (indir 2 1)"
1777 "malloc failed. **\n");
1778 return -1;
1779 }
1780 ext4fs_indir1_size = blksz;
1781 ext4fs_indir1_blkno = -1;
1782 }
1783 if (blksz != ext4fs_indir1_size) {
1784 free(ext4fs_indir1_block);
1785 ext4fs_indir1_block = NULL;
1786 ext4fs_indir1_size = 0;
1787 ext4fs_indir1_blkno = -1;
1788 ext4fs_indir1_block = zalloc(blksz);
1789 if (ext4fs_indir1_block == NULL) {
1790 printf("** DI ext2fs read block (indir 2 1)"
1791 "malloc failed. **\n");
1792 return -1;
1793 }
1794 ext4fs_indir1_size = blksz;
1795 }
Michael Walle7f101be2016-08-29 10:46:44 +02001796 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301797 log2_blksz) != ext4fs_indir1_blkno) {
1798 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001799 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301800 (inode->b.blocks.
1801 double_indir_block) << log2_blksz,
1802 0, blksz,
1803 (char *)ext4fs_indir1_block);
1804 if (status == 0) {
1805 printf("** DI ext2fs read block (indir 2 1)"
1806 "failed. **\n");
1807 return -1;
1808 }
1809 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001810 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301811 log2_blksz;
1812 }
1813
1814 if (ext4fs_indir2_block == NULL) {
1815 ext4fs_indir2_block = zalloc(blksz);
1816 if (ext4fs_indir2_block == NULL) {
1817 printf("** DI ext2fs read block (indir 2 2)"
1818 "malloc failed. **\n");
1819 return -1;
1820 }
1821 ext4fs_indir2_size = blksz;
1822 ext4fs_indir2_blkno = -1;
1823 }
1824 if (blksz != ext4fs_indir2_size) {
1825 free(ext4fs_indir2_block);
1826 ext4fs_indir2_block = NULL;
1827 ext4fs_indir2_size = 0;
1828 ext4fs_indir2_blkno = -1;
1829 ext4fs_indir2_block = zalloc(blksz);
1830 if (ext4fs_indir2_block == NULL) {
1831 printf("** DI ext2fs read block (indir 2 2)"
1832 "malloc failed. **\n");
1833 return -1;
1834 }
1835 ext4fs_indir2_size = blksz;
1836 }
Michael Walle7f101be2016-08-29 10:46:44 +02001837 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankara1596432012-05-25 21:21:44 +05301838 log2_blksz) != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001839 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301840 (ext4fs_indir1_block
1841 [rblock /
1842 perblock]) << log2_blksz, 0,
1843 blksz,
1844 (char *)ext4fs_indir2_block);
1845 if (status == 0) {
1846 printf("** DI ext2fs read block (indir 2 2)"
1847 "failed. **\n");
1848 return -1;
1849 }
1850 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001851 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301852 /
1853 perblock]) <<
1854 log2_blksz;
1855 }
Michael Walle7f101be2016-08-29 10:46:44 +02001856 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301857 }
1858 /* Tripple indirect. */
1859 else {
1860 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1861 (blksz / 4 * blksz / 4));
1862 perblock_child = blksz / 4;
1863 perblock_parent = ((blksz / 4) * (blksz / 4));
1864
1865 if (ext4fs_indir1_block == NULL) {
1866 ext4fs_indir1_block = zalloc(blksz);
1867 if (ext4fs_indir1_block == NULL) {
1868 printf("** TI ext2fs read block (indir 2 1)"
1869 "malloc failed. **\n");
1870 return -1;
1871 }
1872 ext4fs_indir1_size = blksz;
1873 ext4fs_indir1_blkno = -1;
1874 }
1875 if (blksz != ext4fs_indir1_size) {
1876 free(ext4fs_indir1_block);
1877 ext4fs_indir1_block = NULL;
1878 ext4fs_indir1_size = 0;
1879 ext4fs_indir1_blkno = -1;
1880 ext4fs_indir1_block = zalloc(blksz);
1881 if (ext4fs_indir1_block == NULL) {
1882 printf("** TI ext2fs read block (indir 2 1)"
1883 "malloc failed. **\n");
1884 return -1;
1885 }
1886 ext4fs_indir1_size = blksz;
1887 }
Michael Walle7f101be2016-08-29 10:46:44 +02001888 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301889 log2_blksz) != ext4fs_indir1_blkno) {
1890 status = ext4fs_devread
Frederic Leroy04735e92013-06-26 18:11:25 +02001891 ((lbaint_t)
Michael Walle7f101be2016-08-29 10:46:44 +02001892 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankara1596432012-05-25 21:21:44 +05301893 << log2_blksz, 0, blksz,
1894 (char *)ext4fs_indir1_block);
1895 if (status == 0) {
1896 printf("** TI ext2fs read block (indir 2 1)"
1897 "failed. **\n");
1898 return -1;
1899 }
1900 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001901 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301902 log2_blksz;
1903 }
1904
1905 if (ext4fs_indir2_block == NULL) {
1906 ext4fs_indir2_block = zalloc(blksz);
1907 if (ext4fs_indir2_block == NULL) {
1908 printf("** TI ext2fs read block (indir 2 2)"
1909 "malloc failed. **\n");
1910 return -1;
1911 }
1912 ext4fs_indir2_size = blksz;
1913 ext4fs_indir2_blkno = -1;
1914 }
1915 if (blksz != ext4fs_indir2_size) {
1916 free(ext4fs_indir2_block);
1917 ext4fs_indir2_block = NULL;
1918 ext4fs_indir2_size = 0;
1919 ext4fs_indir2_blkno = -1;
1920 ext4fs_indir2_block = zalloc(blksz);
1921 if (ext4fs_indir2_block == NULL) {
1922 printf("** TI ext2fs read block (indir 2 2)"
1923 "malloc failed. **\n");
1924 return -1;
1925 }
1926 ext4fs_indir2_size = blksz;
1927 }
Michael Walle7f101be2016-08-29 10:46:44 +02001928 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301929 perblock_parent]) <<
1930 log2_blksz)
1931 != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001932 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301933 (ext4fs_indir1_block
1934 [rblock /
1935 perblock_parent]) <<
1936 log2_blksz, 0, blksz,
1937 (char *)ext4fs_indir2_block);
1938 if (status == 0) {
1939 printf("** TI ext2fs read block (indir 2 2)"
1940 "failed. **\n");
1941 return -1;
1942 }
1943 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001944 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301945 perblock_parent])
1946 << log2_blksz;
1947 }
1948
1949 if (ext4fs_indir3_block == NULL) {
1950 ext4fs_indir3_block = zalloc(blksz);
1951 if (ext4fs_indir3_block == NULL) {
1952 printf("** TI ext2fs read block (indir 2 2)"
1953 "malloc failed. **\n");
1954 return -1;
1955 }
1956 ext4fs_indir3_size = blksz;
1957 ext4fs_indir3_blkno = -1;
1958 }
1959 if (blksz != ext4fs_indir3_size) {
1960 free(ext4fs_indir3_block);
1961 ext4fs_indir3_block = NULL;
1962 ext4fs_indir3_size = 0;
1963 ext4fs_indir3_blkno = -1;
1964 ext4fs_indir3_block = zalloc(blksz);
1965 if (ext4fs_indir3_block == NULL) {
1966 printf("** TI ext2fs read block (indir 2 2)"
1967 "malloc failed. **\n");
1968 return -1;
1969 }
1970 ext4fs_indir3_size = blksz;
1971 }
Michael Walle7f101be2016-08-29 10:46:44 +02001972 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301973 /
1974 perblock_child]) <<
1975 log2_blksz) != ext4fs_indir3_blkno) {
1976 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001977 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301978 (ext4fs_indir2_block
1979 [(rblock / perblock_child)
1980 % (blksz / 4)]) << log2_blksz, 0,
1981 blksz, (char *)ext4fs_indir3_block);
1982 if (status == 0) {
1983 printf("** TI ext2fs read block (indir 2 2)"
1984 "failed. **\n");
1985 return -1;
1986 }
1987 ext4fs_indir3_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001988 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301989 perblock_child) %
1990 (blksz /
1991 4)]) <<
1992 log2_blksz;
1993 }
1994
Michael Walle7f101be2016-08-29 10:46:44 +02001995 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankara1596432012-05-25 21:21:44 +05301996 [rblock % perblock_child]);
1997 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001998 debug("read_allocated_block %ld\n", blknr);
Uma Shankara1596432012-05-25 21:21:44 +05301999
2000 return blknr;
2001}
2002
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02002003/**
2004 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
2005 * global pointers
2006 *
2007 * This function assures that for a file with the same name but different size
2008 * the sequential store on the ext4 filesystem will be correct.
2009 *
2010 * In this function the global data, responsible for internal representation
2011 * of the ext4 data are initialized to the reset state. Without this, during
2012 * replacement of the smaller file with the bigger truncation of new file was
2013 * performed.
2014 */
2015void ext4fs_reinit_global(void)
Uma Shankara1596432012-05-25 21:21:44 +05302016{
Uma Shankara1596432012-05-25 21:21:44 +05302017 if (ext4fs_indir1_block != NULL) {
2018 free(ext4fs_indir1_block);
2019 ext4fs_indir1_block = NULL;
2020 ext4fs_indir1_size = 0;
2021 ext4fs_indir1_blkno = -1;
2022 }
2023 if (ext4fs_indir2_block != NULL) {
2024 free(ext4fs_indir2_block);
2025 ext4fs_indir2_block = NULL;
2026 ext4fs_indir2_size = 0;
2027 ext4fs_indir2_blkno = -1;
2028 }
2029 if (ext4fs_indir3_block != NULL) {
2030 free(ext4fs_indir3_block);
2031 ext4fs_indir3_block = NULL;
2032 ext4fs_indir3_size = 0;
2033 ext4fs_indir3_blkno = -1;
2034 }
2035}
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02002036void ext4fs_close(void)
2037{
2038 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
2039 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
2040 ext4fs_file = NULL;
2041 }
2042 if (ext4fs_root != NULL) {
2043 free(ext4fs_root);
2044 ext4fs_root = NULL;
2045 }
2046
2047 ext4fs_reinit_global();
2048}
Uma Shankara1596432012-05-25 21:21:44 +05302049
2050int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
2051 struct ext2fs_node **fnode, int *ftype)
2052{
2053 unsigned int fpos = 0;
2054 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002055 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302056 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2057
2058#ifdef DEBUG
2059 if (name != NULL)
2060 printf("Iterate dir %s\n", name);
2061#endif /* of DEBUG */
2062 if (!diro->inode_read) {
2063 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2064 if (status == 0)
2065 return 0;
2066 }
2067 /* Search the file. */
Michael Walle7f101be2016-08-29 10:46:44 +02002068 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankara1596432012-05-25 21:21:44 +05302069 struct ext2_dirent dirent;
2070
2071 status = ext4fs_read_file(diro, fpos,
2072 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002073 (char *)&dirent, &actread);
2074 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302075 return 0;
2076
Thomas Fitzsimmons54d68e92015-11-18 12:42:53 -05002077 if (dirent.direntlen == 0) {
2078 printf("Failed to iterate over directory %s\n", name);
2079 return 0;
2080 }
2081
Uma Shankara1596432012-05-25 21:21:44 +05302082 if (dirent.namelen != 0) {
2083 char filename[dirent.namelen + 1];
2084 struct ext2fs_node *fdiro;
2085 int type = FILETYPE_UNKNOWN;
2086
2087 status = ext4fs_read_file(diro,
2088 fpos +
2089 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002090 dirent.namelen, filename,
2091 &actread);
2092 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302093 return 0;
2094
2095 fdiro = zalloc(sizeof(struct ext2fs_node));
2096 if (!fdiro)
2097 return 0;
2098
2099 fdiro->data = diro->data;
Michael Walle7f101be2016-08-29 10:46:44 +02002100 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankara1596432012-05-25 21:21:44 +05302101
2102 filename[dirent.namelen] = '\0';
2103
2104 if (dirent.filetype != FILETYPE_UNKNOWN) {
2105 fdiro->inode_read = 0;
2106
2107 if (dirent.filetype == FILETYPE_DIRECTORY)
2108 type = FILETYPE_DIRECTORY;
2109 else if (dirent.filetype == FILETYPE_SYMLINK)
2110 type = FILETYPE_SYMLINK;
2111 else if (dirent.filetype == FILETYPE_REG)
2112 type = FILETYPE_REG;
2113 } else {
2114 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002115 le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05302116 (dirent.inode),
2117 &fdiro->inode);
2118 if (status == 0) {
2119 free(fdiro);
2120 return 0;
2121 }
2122 fdiro->inode_read = 1;
2123
Michael Walle7f101be2016-08-29 10:46:44 +02002124 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankara1596432012-05-25 21:21:44 +05302125 FILETYPE_INO_MASK) ==
2126 FILETYPE_INO_DIRECTORY) {
2127 type = FILETYPE_DIRECTORY;
Michael Walle7f101be2016-08-29 10:46:44 +02002128 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302129 & FILETYPE_INO_MASK) ==
2130 FILETYPE_INO_SYMLINK) {
2131 type = FILETYPE_SYMLINK;
Michael Walle7f101be2016-08-29 10:46:44 +02002132 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302133 & FILETYPE_INO_MASK) ==
2134 FILETYPE_INO_REG) {
2135 type = FILETYPE_REG;
2136 }
2137 }
2138#ifdef DEBUG
2139 printf("iterate >%s<\n", filename);
2140#endif /* of DEBUG */
2141 if ((name != NULL) && (fnode != NULL)
2142 && (ftype != NULL)) {
2143 if (strcmp(filename, name) == 0) {
2144 *ftype = type;
2145 *fnode = fdiro;
2146 return 1;
2147 }
2148 } else {
2149 if (fdiro->inode_read == 0) {
2150 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002151 le32_to_cpu(
Uma Shankara1596432012-05-25 21:21:44 +05302152 dirent.inode),
2153 &fdiro->inode);
2154 if (status == 0) {
2155 free(fdiro);
2156 return 0;
2157 }
2158 fdiro->inode_read = 1;
2159 }
2160 switch (type) {
2161 case FILETYPE_DIRECTORY:
2162 printf("<DIR> ");
2163 break;
2164 case FILETYPE_SYMLINK:
2165 printf("<SYM> ");
2166 break;
2167 case FILETYPE_REG:
2168 printf(" ");
2169 break;
2170 default:
2171 printf("< ? > ");
2172 break;
2173 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002174 printf("%10u %s\n",
Michael Walle7f101be2016-08-29 10:46:44 +02002175 le32_to_cpu(fdiro->inode.size),
Uma Shankara1596432012-05-25 21:21:44 +05302176 filename);
2177 }
2178 free(fdiro);
2179 }
Michael Walle7f101be2016-08-29 10:46:44 +02002180 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankara1596432012-05-25 21:21:44 +05302181 }
2182 return 0;
2183}
2184
2185static char *ext4fs_read_symlink(struct ext2fs_node *node)
2186{
2187 char *symlink;
2188 struct ext2fs_node *diro = node;
2189 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002190 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302191
2192 if (!diro->inode_read) {
2193 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2194 if (status == 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002195 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302196 }
Michael Walle7f101be2016-08-29 10:46:44 +02002197 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankara1596432012-05-25 21:21:44 +05302198 if (!symlink)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002199 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302200
Michael Walle7f101be2016-08-29 10:46:44 +02002201 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankara1596432012-05-25 21:21:44 +05302202 strncpy(symlink, diro->inode.b.symlink,
Michael Walle7f101be2016-08-29 10:46:44 +02002203 le32_to_cpu(diro->inode.size));
Uma Shankara1596432012-05-25 21:21:44 +05302204 } else {
2205 status = ext4fs_read_file(diro, 0,
Michael Walle7f101be2016-08-29 10:46:44 +02002206 le32_to_cpu(diro->inode.size),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002207 symlink, &actread);
Gary Bisson9d2f6a92015-09-07 11:20:07 +02002208 if ((status < 0) || (actread == 0)) {
Uma Shankara1596432012-05-25 21:21:44 +05302209 free(symlink);
Michael Walle58a9ecb2016-09-01 11:21:40 +02002210 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302211 }
2212 }
Michael Walle7f101be2016-08-29 10:46:44 +02002213 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankara1596432012-05-25 21:21:44 +05302214 return symlink;
2215}
2216
2217static int ext4fs_find_file1(const char *currpath,
2218 struct ext2fs_node *currroot,
2219 struct ext2fs_node **currfound, int *foundtype)
2220{
2221 char fpath[strlen(currpath) + 1];
2222 char *name = fpath;
2223 char *next;
2224 int status;
2225 int type = FILETYPE_DIRECTORY;
2226 struct ext2fs_node *currnode = currroot;
2227 struct ext2fs_node *oldnode = currroot;
2228
2229 strncpy(fpath, currpath, strlen(currpath) + 1);
2230
2231 /* Remove all leading slashes. */
2232 while (*name == '/')
2233 name++;
2234
2235 if (!*name) {
2236 *currfound = currnode;
2237 return 1;
2238 }
2239
2240 for (;;) {
2241 int found;
2242
2243 /* Extract the actual part from the pathname. */
2244 next = strchr(name, '/');
2245 if (next) {
2246 /* Remove all leading slashes. */
2247 while (*next == '/')
2248 *(next++) = '\0';
2249 }
2250
2251 if (type != FILETYPE_DIRECTORY) {
2252 ext4fs_free_node(currnode, currroot);
2253 return 0;
2254 }
2255
2256 oldnode = currnode;
2257
2258 /* Iterate over the directory. */
2259 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2260 if (found == 0)
2261 return 0;
2262
2263 if (found == -1)
2264 break;
2265
2266 /* Read in the symlink and follow it. */
2267 if (type == FILETYPE_SYMLINK) {
2268 char *symlink;
2269
2270 /* Test if the symlink does not loop. */
2271 if (++symlinknest == 8) {
2272 ext4fs_free_node(currnode, currroot);
2273 ext4fs_free_node(oldnode, currroot);
2274 return 0;
2275 }
2276
2277 symlink = ext4fs_read_symlink(currnode);
2278 ext4fs_free_node(currnode, currroot);
2279
2280 if (!symlink) {
2281 ext4fs_free_node(oldnode, currroot);
2282 return 0;
2283 }
2284
2285 debug("Got symlink >%s<\n", symlink);
2286
2287 if (symlink[0] == '/') {
2288 ext4fs_free_node(oldnode, currroot);
2289 oldnode = &ext4fs_root->diropen;
2290 }
2291
2292 /* Lookup the node the symlink points to. */
2293 status = ext4fs_find_file1(symlink, oldnode,
2294 &currnode, &type);
2295
2296 free(symlink);
2297
2298 if (status == 0) {
2299 ext4fs_free_node(oldnode, currroot);
2300 return 0;
2301 }
2302 }
2303
2304 ext4fs_free_node(oldnode, currroot);
2305
2306 /* Found the node! */
2307 if (!next || *next == '\0') {
2308 *currfound = currnode;
2309 *foundtype = type;
2310 return 1;
2311 }
2312 name = next;
2313 }
2314 return -1;
2315}
2316
2317int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2318 struct ext2fs_node **foundnode, int expecttype)
2319{
2320 int status;
2321 int foundtype = FILETYPE_DIRECTORY;
2322
2323 symlinknest = 0;
2324 if (!path)
2325 return 0;
2326
2327 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2328 if (status == 0)
2329 return 0;
2330
2331 /* Check if the node that was found was of the expected type. */
2332 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2333 return 0;
2334 else if ((expecttype == FILETYPE_DIRECTORY)
2335 && (foundtype != expecttype))
2336 return 0;
2337
2338 return 1;
2339}
2340
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002341int ext4fs_open(const char *filename, loff_t *len)
Uma Shankara1596432012-05-25 21:21:44 +05302342{
2343 struct ext2fs_node *fdiro = NULL;
2344 int status;
Uma Shankara1596432012-05-25 21:21:44 +05302345
2346 if (ext4fs_root == NULL)
2347 return -1;
2348
2349 ext4fs_file = NULL;
2350 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2351 FILETYPE_REG);
2352 if (status == 0)
2353 goto fail;
2354
2355 if (!fdiro->inode_read) {
2356 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2357 &fdiro->inode);
2358 if (status == 0)
2359 goto fail;
2360 }
Michael Walle7f101be2016-08-29 10:46:44 +02002361 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankara1596432012-05-25 21:21:44 +05302362 ext4fs_file = fdiro;
2363
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002364 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05302365fail:
2366 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2367
2368 return -1;
2369}
2370
Sean Anderson7667bde2023-11-08 12:51:09 -05002371int ext4fs_mount(void)
Uma Shankara1596432012-05-25 21:21:44 +05302372{
2373 struct ext2_data *data;
2374 int status;
2375 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00002376 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankara1596432012-05-25 21:21:44 +05302377 if (!data)
2378 return 0;
2379
2380 /* Read the superblock. */
Egbert Eich50ce4c02013-05-01 01:13:19 +00002381 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankara1596432012-05-25 21:21:44 +05302382
2383 if (status == 0)
2384 goto fail;
2385
2386 /* Make sure this is an ext2 filesystem. */
Michael Walle7f101be2016-08-29 10:46:44 +02002387 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Marek Behún51be4712018-03-08 00:26:08 +01002388 goto fail_noerr;
Uma Shankara1596432012-05-25 21:21:44 +05302389
Tom Rini6f94ab62016-07-22 17:59:11 -04002390
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002391 if (le32_to_cpu(data->sblock.revision_level) == 0) {
Uma Shankara1596432012-05-25 21:21:44 +05302392 fs->inodesz = 128;
Stefan Brüns3cc5bbb2016-12-27 02:35:08 +01002393 fs->gdsize = 32;
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002394 } else {
2395 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2396 __le32_to_cpu(data->sblock.feature_compatibility),
2397 __le32_to_cpu(data->sblock.feature_incompat),
2398 __le32_to_cpu(data->sblock.feature_ro_compat));
Uma Shankara1596432012-05-25 21:21:44 +05302399
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002400 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2401 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2402 EXT4_FEATURE_INCOMPAT_64BIT ?
2403 le16_to_cpu(data->sblock.descriptor_size) : 32;
2404 }
2405
2406 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2407 le32_to_cpu(data->sblock.revision_level),
2408 fs->inodesz, fs->gdsize);
Uma Shankara1596432012-05-25 21:21:44 +05302409
2410 data->diropen.data = data;
2411 data->diropen.ino = 2;
2412 data->diropen.inode_read = 1;
2413 data->inode = &data->diropen.inode;
2414
2415 status = ext4fs_read_inode(data, 2, data->inode);
2416 if (status == 0)
2417 goto fail;
2418
2419 ext4fs_root = data;
2420
2421 return 1;
2422fail:
Simon Glassf337fb92022-10-11 09:47:11 -06002423 log_debug("Failed to mount ext2 filesystem...\n");
Marek Behún51be4712018-03-08 00:26:08 +01002424fail_noerr:
Uma Shankara1596432012-05-25 21:21:44 +05302425 free(data);
2426 ext4fs_root = NULL;
2427
2428 return 0;
2429}