blob: 857c15d878e4b414607082bd7767c3aa32a8277d [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
Simon Glasse6f6f9e2020-05-10 11:39:58 -060021#include <blk.h>
Uma Shankara1596432012-05-25 21:21:44 +053022#include <ext_common.h>
23#include <ext4fs.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060024#include <log.h>
Uma Shankara1596432012-05-25 21:21:44 +053025#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060026#include <memalign.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060027#include <part.h>
Uma Shankara1596432012-05-25 21:21:44 +053028#include <stddef.h>
29#include <linux/stat.h>
30#include <linux/time.h>
31#include <asm/byteorder.h>
32#include "ext4_common.h"
33
34struct ext2_data *ext4fs_root;
35struct ext2fs_node *ext4fs_file;
Michael Walle58a9ecb2016-09-01 11:21:40 +020036__le32 *ext4fs_indir1_block;
Uma Shankara1596432012-05-25 21:21:44 +053037int ext4fs_indir1_size;
38int ext4fs_indir1_blkno = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +020039__le32 *ext4fs_indir2_block;
Uma Shankara1596432012-05-25 21:21:44 +053040int ext4fs_indir2_size;
41int ext4fs_indir2_blkno = -1;
42
Michael Walle58a9ecb2016-09-01 11:21:40 +020043__le32 *ext4fs_indir3_block;
Uma Shankara1596432012-05-25 21:21:44 +053044int ext4fs_indir3_size;
45int ext4fs_indir3_blkno = -1;
46struct ext2_inode *g_parent_inode;
47static int symlinknest;
48
Stephen Warren03e2ecf2012-10-22 06:43:50 +000049#if defined(CONFIG_EXT4_WRITE)
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +020050struct ext2_block_group *ext4fs_get_group_descriptor
51 (const struct ext_filesystem *fs, uint32_t bg_idx)
52{
53 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
54}
55
Michael Walle58a9ecb2016-09-01 11:21:40 +020056static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
57{
58 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
59}
60
61static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
62{
Stefan Brüns749e93e2016-09-20 01:13:01 +020063 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
64 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
65 free_blocks--;
66
67 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
68 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
Michael Walle58a9ecb2016-09-01 11:21:40 +020069}
70
Stefan Brüns749e93e2016-09-20 01:13:01 +020071static inline void ext4fs_bg_free_inodes_dec
72 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020073{
Stefan Brüns749e93e2016-09-20 01:13:01 +020074 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
75 if (fs->gdsize == 64)
76 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
77 free_inodes--;
78
79 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
80 if (fs->gdsize == 64)
81 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020082}
83
Stefan Brüns749e93e2016-09-20 01:13:01 +020084static inline void ext4fs_bg_free_blocks_dec
85 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020086{
Stefan Brüns749e93e2016-09-20 01:13:01 +020087 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
88 if (fs->gdsize == 64)
89 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
90 free_blocks--;
91
92 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
93 if (fs->gdsize == 64)
94 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020095}
96
Stefan Brüns749e93e2016-09-20 01:13:01 +020097static inline void ext4fs_bg_itable_unused_dec
98 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020099{
Stefan Brüns749e93e2016-09-20 01:13:01 +0200100 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
101 if (fs->gdsize == 64)
102 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
103 free_inodes--;
104
105 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
106 if (fs->gdsize == 64)
107 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200108}
109
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +0200110uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
111{
112 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
113 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
114 return free_blocks;
115}
116
117void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
118{
119 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
120 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
121}
122
123uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
124 const struct ext_filesystem *fs)
125{
126 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
127 if (fs->gdsize == 64)
128 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
129 return free_blocks;
130}
131
132static inline
133uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
134 const struct ext_filesystem *fs)
135{
136 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
137 if (fs->gdsize == 64)
138 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
139 return free_inodes;
140}
141
142static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
143{
144 return le16_to_cpu(bg->bg_flags);
145}
146
147static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
148 uint16_t flags)
149{
150 bg->bg_flags = cpu_to_le16(flags);
151}
152
153/* Block number of the block bitmap */
154uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
155 const struct ext_filesystem *fs)
156{
157 uint64_t block_nr = le32_to_cpu(bg->block_id);
158 if (fs->gdsize == 64)
159 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
160 return block_nr;
161}
162
163/* Block number of the inode bitmap */
164uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
165 const struct ext_filesystem *fs)
166{
167 uint64_t block_nr = le32_to_cpu(bg->inode_id);
168 if (fs->gdsize == 64)
169 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
170 return block_nr;
171}
172#endif
173
174/* Block number of the inode table */
175uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
176 const struct ext_filesystem *fs)
177{
178 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
179 if (fs->gdsize == 64)
180 block_nr +=
181 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
182 return block_nr;
183}
184
185#if defined(CONFIG_EXT4_WRITE)
Uma Shankared34f342012-05-25 21:22:49 +0530186uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
187{
188 uint32_t res = size / n;
189 if (res * n != size)
190 res++;
191
192 return res;
193}
194
Jean-Jacques Hiblotb0001802019-02-13 12:15:24 +0100195void put_ext4(uint64_t off, const void *buf, uint32_t size)
Uma Shankared34f342012-05-25 21:22:49 +0530196{
197 uint64_t startblock;
198 uint64_t remainder;
199 unsigned char *temp_ptr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530200 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +0000201 int log2blksz = fs->dev_desc->log2blksz;
202 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530203
Egbert Eich50ce4c02013-05-01 01:13:19 +0000204 startblock = off >> log2blksz;
Uma Shankared34f342012-05-25 21:22:49 +0530205 startblock += part_offset;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000206 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankared34f342012-05-25 21:22:49 +0530207
208 if (fs->dev_desc == NULL)
209 return;
210
Egbert Eich50ce4c02013-05-01 01:13:19 +0000211 if ((startblock + (size >> log2blksz)) >
Uma Shankared34f342012-05-25 21:22:49 +0530212 (part_offset + fs->total_sect)) {
Frederic Leroy04735e92013-06-26 18:11:25 +0200213 printf("part_offset is " LBAFU "\n", part_offset);
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900214 printf("total_sector is %llu\n", fs->total_sect);
Uma Shankared34f342012-05-25 21:22:49 +0530215 printf("error: overflow occurs\n");
216 return;
217 }
218
219 if (remainder) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700220 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
221 temp_ptr = sec_buf;
222 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
223 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530224 } else {
Egbert Eich50ce4c02013-05-01 01:13:19 +0000225 if (size >> log2blksz != 0) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700226 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
227 (unsigned long *)buf);
Uma Shankared34f342012-05-25 21:22:49 +0530228 } else {
Simon Glass2a981dc2016-02-29 15:25:52 -0700229 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530230 temp_ptr = sec_buf;
231 memcpy(temp_ptr, buf, size);
Simon Glass2a981dc2016-02-29 15:25:52 -0700232 blk_dwrite(fs->dev_desc, startblock, 1,
233 (unsigned long *)sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530234 }
235 }
236}
237
238static int _get_new_inode_no(unsigned char *buffer)
239{
240 struct ext_filesystem *fs = get_fs();
241 unsigned char input;
242 int operand, status;
243 int count = 1;
244 int j = 0;
245
246 /* get the blocksize of the filesystem */
247 unsigned char *ptr = buffer;
248 while (*ptr == 255) {
249 ptr++;
250 count += 8;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200251 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankared34f342012-05-25 21:22:49 +0530252 return -1;
253 }
254
255 for (j = 0; j < fs->blksz; j++) {
256 input = *ptr;
257 int i = 0;
258 while (i <= 7) {
259 operand = 1 << i;
260 status = input & operand;
261 if (status) {
262 i++;
263 count++;
264 } else {
265 *ptr |= operand;
266 return count;
267 }
268 }
269 ptr = ptr + 1;
270 }
271
272 return -1;
273}
274
275static int _get_new_blk_no(unsigned char *buffer)
276{
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200277 int operand;
Uma Shankared34f342012-05-25 21:22:49 +0530278 int count = 0;
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200279 int i;
Uma Shankared34f342012-05-25 21:22:49 +0530280 unsigned char *ptr = buffer;
281 struct ext_filesystem *fs = get_fs();
282
Uma Shankared34f342012-05-25 21:22:49 +0530283 while (*ptr == 255) {
284 ptr++;
285 count += 8;
286 if (count == (fs->blksz * 8))
287 return -1;
288 }
289
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200290 if (fs->blksz == 1024)
291 count += 1;
292
293 for (i = 0; i <= 7; i++) {
294 operand = 1 << i;
295 if (*ptr & operand) {
296 count++;
297 } else {
298 *ptr |= operand;
299 return count;
Uma Shankared34f342012-05-25 21:22:49 +0530300 }
Uma Shankared34f342012-05-25 21:22:49 +0530301 }
302
303 return -1;
304}
305
306int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
307{
308 int i, remainder, status;
309 unsigned char *ptr = buffer;
310 unsigned char operand;
311 i = blockno / 8;
312 remainder = blockno % 8;
313 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
314
315 i = i - (index * blocksize);
316 if (blocksize != 1024) {
317 ptr = ptr + i;
318 operand = 1 << remainder;
319 status = *ptr & operand;
320 if (status)
321 return -1;
322
323 *ptr = *ptr | operand;
324 return 0;
325 } else {
326 if (remainder == 0) {
327 ptr = ptr + i - 1;
328 operand = (1 << 7);
329 } else {
330 ptr = ptr + i;
331 operand = (1 << (remainder - 1));
332 }
333 status = *ptr & operand;
334 if (status)
335 return -1;
336
337 *ptr = *ptr | operand;
338 return 0;
339 }
340}
341
342void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
343{
344 int i, remainder, status;
345 unsigned char *ptr = buffer;
346 unsigned char operand;
347 i = blockno / 8;
348 remainder = blockno % 8;
349 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
350
351 i = i - (index * blocksize);
352 if (blocksize != 1024) {
353 ptr = ptr + i;
354 operand = (1 << remainder);
355 status = *ptr & operand;
356 if (status)
357 *ptr = *ptr & ~(operand);
358 } else {
359 if (remainder == 0) {
360 ptr = ptr + i - 1;
361 operand = (1 << 7);
362 } else {
363 ptr = ptr + i;
364 operand = (1 << (remainder - 1));
365 }
366 status = *ptr & operand;
367 if (status)
368 *ptr = *ptr & ~(operand);
369 }
370}
371
372int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
373{
374 int i, remainder, status;
375 unsigned char *ptr = buffer;
376 unsigned char operand;
377
Michael Walle58a9ecb2016-09-01 11:21:40 +0200378 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530379 i = inode_no / 8;
380 remainder = inode_no % 8;
381 if (remainder == 0) {
382 ptr = ptr + i - 1;
383 operand = (1 << 7);
384 } else {
385 ptr = ptr + i;
386 operand = (1 << (remainder - 1));
387 }
388 status = *ptr & operand;
389 if (status)
390 return -1;
391
392 *ptr = *ptr | operand;
393
394 return 0;
395}
396
397void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
398{
399 int i, remainder, status;
400 unsigned char *ptr = buffer;
401 unsigned char operand;
402
Michael Walle58a9ecb2016-09-01 11:21:40 +0200403 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530404 i = inode_no / 8;
405 remainder = inode_no % 8;
406 if (remainder == 0) {
407 ptr = ptr + i - 1;
408 operand = (1 << 7);
409 } else {
410 ptr = ptr + i;
411 operand = (1 << (remainder - 1));
412 }
413 status = *ptr & operand;
414 if (status)
415 *ptr = *ptr & ~(operand);
416}
417
Michael Walle58a9ecb2016-09-01 11:21:40 +0200418uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankared34f342012-05-25 21:22:49 +0530419{
420 struct ext2_block_group *desc;
421 struct ext_filesystem *fs = get_fs();
Michael Walle58a9ecb2016-09-01 11:21:40 +0200422 uint16_t crc = 0;
423 __le32 le32_i = cpu_to_le32(i);
Uma Shankared34f342012-05-25 21:22:49 +0530424
Stefan Brüns688d0e72016-09-17 02:10:10 +0200425 desc = ext4fs_get_group_descriptor(fs, i);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200426 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankared34f342012-05-25 21:22:49 +0530427 int offset = offsetof(struct ext2_block_group, bg_checksum);
428
Pali Rohár4f0e77f2022-04-12 11:20:43 +0200429 crc = crc16(~0, (__u8 *)fs->sb->unique_id,
Uma Shankared34f342012-05-25 21:22:49 +0530430 sizeof(fs->sb->unique_id));
Pali Rohár4f0e77f2022-04-12 11:20:43 +0200431 crc = crc16(crc, (__u8 *)&le32_i, sizeof(le32_i));
432 crc = crc16(crc, (__u8 *)desc, offset);
Uma Shankared34f342012-05-25 21:22:49 +0530433 offset += sizeof(desc->bg_checksum); /* skip checksum */
434 assert(offset == sizeof(*desc));
Tuomas Tynkkynen385b7312017-09-25 22:06:31 +0300435 if (offset < fs->gdsize) {
Pali Rohár4f0e77f2022-04-12 11:20:43 +0200436 crc = crc16(crc, (__u8 *)desc + offset,
Tuomas Tynkkynen385b7312017-09-25 22:06:31 +0300437 fs->gdsize - offset);
438 }
Uma Shankared34f342012-05-25 21:22:49 +0530439 }
440
441 return crc;
442}
443
444static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
445{
446 int dentry_length;
447 int sizeof_void_space;
448 int new_entry_byte_reqd;
449 short padding_factor = 0;
450
451 if (dir->namelen % 4 != 0)
452 padding_factor = 4 - (dir->namelen % 4);
453
454 dentry_length = sizeof(struct ext2_dirent) +
455 dir->namelen + padding_factor;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200456 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankared34f342012-05-25 21:22:49 +0530457 if (sizeof_void_space == 0)
458 return 0;
459
460 padding_factor = 0;
461 if (strlen(filename) % 4 != 0)
462 padding_factor = 4 - (strlen(filename) % 4);
463
464 new_entry_byte_reqd = strlen(filename) +
465 sizeof(struct ext2_dirent) + padding_factor;
466 if (sizeof_void_space >= new_entry_byte_reqd) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200467 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankared34f342012-05-25 21:22:49 +0530468 return sizeof_void_space;
469 }
470
471 return 0;
472}
473
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200474int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankared34f342012-05-25 21:22:49 +0530475{
476 unsigned int *zero_buffer = NULL;
477 char *root_first_block_buffer = NULL;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200478 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530479 long int first_block_no_of_root = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530480 int totalbytes = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530481 unsigned int new_entry_byte_reqd;
Uma Shankared34f342012-05-25 21:22:49 +0530482 int sizeof_void_space = 0;
483 int templength = 0;
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200484 int inodeno = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530485 int status;
486 struct ext_filesystem *fs = get_fs();
487 /* directory entry */
488 struct ext2_dirent *dir;
Uma Shankared34f342012-05-25 21:22:49 +0530489 char *temp_dir = NULL;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200490 uint32_t new_blk_no;
491 uint32_t new_size;
492 uint32_t new_blockcnt;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200493 uint32_t directory_blocks;
Uma Shankared34f342012-05-25 21:22:49 +0530494
495 zero_buffer = zalloc(fs->blksz);
496 if (!zero_buffer) {
497 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200498 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530499 }
500 root_first_block_buffer = zalloc(fs->blksz);
501 if (!root_first_block_buffer) {
502 free(zero_buffer);
503 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200504 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530505 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200506 new_entry_byte_reqd = ROUND(strlen(filename) +
507 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530508restart:
Stefan Brünsa321abd2016-09-06 04:36:44 +0200509 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
510 LOG2_BLOCK_SIZE(ext4fs_root);
511 blk_idx = directory_blocks - 1;
Uma Shankared34f342012-05-25 21:22:49 +0530512
Stefan Brünsa321abd2016-09-06 04:36:44 +0200513restart_read:
Uma Shankared34f342012-05-25 21:22:49 +0530514 /* read the block no allocated to a file */
Stephen Warrend5aee652019-01-30 12:58:05 -0700515 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx,
516 NULL);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200517 if (first_block_no_of_root <= 0)
518 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530519
Frederic Leroy04735e92013-06-26 18:11:25 +0200520 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankared34f342012-05-25 21:22:49 +0530521 * fs->sect_perblk,
522 0, fs->blksz, root_first_block_buffer);
523 if (status == 0)
524 goto fail;
525
526 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
527 goto fail;
528 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankared34f342012-05-25 21:22:49 +0530529 totalbytes = 0;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200530
Michael Walle58a9ecb2016-09-01 11:21:40 +0200531 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsa321abd2016-09-06 04:36:44 +0200532 unsigned short used_len = ROUND(dir->namelen +
533 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530534
Stefan Brünsa321abd2016-09-06 04:36:44 +0200535 /* last entry of block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200536 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankared34f342012-05-25 21:22:49 +0530537
Stefan Brünsa321abd2016-09-06 04:36:44 +0200538 /* check if new entry fits */
539 if ((used_len + new_entry_byte_reqd) <=
540 le16_to_cpu(dir->direntlen)) {
541 dir->direntlen = cpu_to_le16(used_len);
542 break;
543 } else {
544 if (blk_idx > 0) {
545 printf("Block full, trying previous\n");
546 blk_idx--;
547 goto restart_read;
548 }
549 printf("All blocks full: Allocate new\n");
Uma Shankared34f342012-05-25 21:22:49 +0530550
Stefan Brünsb96c3c72016-09-06 04:36:43 +0200551 if (le32_to_cpu(g_parent_inode->flags) &
552 EXT4_EXTENTS_FL) {
553 printf("Directory uses extents\n");
554 goto fail;
555 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200556 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankared34f342012-05-25 21:22:49 +0530557 printf("Directory exceeds limit\n");
558 goto fail;
559 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200560 new_blk_no = ext4fs_get_new_blk_no();
561 if (new_blk_no == -1) {
Uma Shankared34f342012-05-25 21:22:49 +0530562 printf("no block left to assign\n");
563 goto fail;
564 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200565 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200566 g_parent_inode->b.blocks.
567 dir_blocks[directory_blocks] =
Michael Walle58a9ecb2016-09-01 11:21:40 +0200568 cpu_to_le32(new_blk_no);
569
570 new_size = le32_to_cpu(g_parent_inode->size);
571 new_size += fs->blksz;
572 g_parent_inode->size = cpu_to_le32(new_size);
573
574 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
Marek Szyprowski1c9f8f62019-06-21 15:32:51 +0200575 new_blockcnt += fs->blksz >> LOG2_SECTOR_SIZE;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200576 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
577
Uma Shankared34f342012-05-25 21:22:49 +0530578 if (ext4fs_put_metadata
579 (root_first_block_buffer,
580 first_block_no_of_root))
581 goto fail;
582 goto restart;
583 }
Uma Shankared34f342012-05-25 21:22:49 +0530584 }
585
Michael Walle58a9ecb2016-09-01 11:21:40 +0200586 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530587 totalbytes = totalbytes + templength;
588 sizeof_void_space = check_void_in_dentry(dir, filename);
589 if (sizeof_void_space)
590 break;
591
592 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530593 }
594
595 /* make a pointer ready for creating next directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200596 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530597 totalbytes = totalbytes + templength;
598 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530599
600 /* get the next available inode number */
601 inodeno = ext4fs_get_new_inode_no();
602 if (inodeno == -1) {
603 printf("no inode left to assign\n");
604 goto fail;
605 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200606 dir->inode = cpu_to_le32(inodeno);
Uma Shankared34f342012-05-25 21:22:49 +0530607 if (sizeof_void_space)
Michael Walle58a9ecb2016-09-01 11:21:40 +0200608 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankared34f342012-05-25 21:22:49 +0530609 else
Michael Walle58a9ecb2016-09-01 11:21:40 +0200610 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankared34f342012-05-25 21:22:49 +0530611
612 dir->namelen = strlen(filename);
Jean-Jacques Hiblot5efc0682019-02-13 12:15:25 +0100613 dir->filetype = file_type;
Uma Shankared34f342012-05-25 21:22:49 +0530614 temp_dir = (char *)dir;
615 temp_dir = temp_dir + sizeof(struct ext2_dirent);
616 memcpy(temp_dir, filename, strlen(filename));
617
Uma Shankared34f342012-05-25 21:22:49 +0530618 /* update or write the 1st block of root inode */
619 if (ext4fs_put_metadata(root_first_block_buffer,
620 first_block_no_of_root))
621 goto fail;
622
623fail:
624 free(zero_buffer);
625 free(root_first_block_buffer);
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200626
627 return inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530628}
629
630static int search_dir(struct ext2_inode *parent_inode, char *dirname)
631{
632 int status;
Stefan Brüns76a29512016-09-06 04:36:41 +0200633 int inodeno = 0;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200634 int offset;
635 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530636 long int blknr;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200637 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530638 struct ext2_dirent *dir = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530639 struct ext_filesystem *fs = get_fs();
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200640 uint32_t directory_blocks;
641 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530642
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200643 directory_blocks = le32_to_cpu(parent_inode->size) >>
644 LOG2_BLOCK_SIZE(ext4fs_root);
645
646 block_buffer = zalloc(fs->blksz);
647 if (!block_buffer)
648 goto fail;
649
650 /* get the block no allocated to a file */
651 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
Stephen Warrend5aee652019-01-30 12:58:05 -0700652 blknr = read_allocated_block(parent_inode, blk_idx, NULL);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200653 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530654 goto fail;
655
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200656 /* read the directory block */
Frederic Leroy04735e92013-06-26 18:11:25 +0200657 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +0530658 0, fs->blksz, (char *)block_buffer);
659 if (status == 0)
660 goto fail;
661
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200662 offset = 0;
663 do {
Ian Rayecdfb412017-11-08 15:35:10 +0000664 if (offset & 3) {
665 printf("Badly aligned ext2_dirent\n");
666 break;
667 }
668
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200669 dir = (struct ext2_dirent *)(block_buffer + offset);
670 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankared34f342012-05-25 21:22:49 +0530671
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200672 int direntlen = le16_to_cpu(dir->direntlen);
673 if (direntlen < sizeof(struct ext2_dirent))
Uma Shankared34f342012-05-25 21:22:49 +0530674 break;
675
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200676 if (dir->inode && (strlen(dirname) == dir->namelen) &&
677 (strncmp(dirname, direntname, dir->namelen) == 0)) {
678 inodeno = le32_to_cpu(dir->inode);
679 break;
680 }
Uma Shankared34f342012-05-25 21:22:49 +0530681
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200682 offset += direntlen;
Stefan Brüns76a29512016-09-06 04:36:41 +0200683
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200684 } while (offset < fs->blksz);
685
686 if (inodeno > 0) {
687 free(block_buffer);
Stefan Brüns76a29512016-09-06 04:36:41 +0200688 return inodeno;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200689 }
Uma Shankared34f342012-05-25 21:22:49 +0530690 }
691
692fail:
693 free(block_buffer);
694
695 return -1;
696}
697
698static int find_dir_depth(char *dirname)
699{
700 char *token = strtok(dirname, "/");
701 int count = 0;
702 while (token != NULL) {
703 token = strtok(NULL, "/");
704 count++;
705 }
706 return count + 1 + 1;
707 /*
708 * for example for string /home/temp
709 * depth=home(1)+temp(1)+1 extra for NULL;
710 * so count is 4;
711 */
712}
713
714static int parse_path(char **arr, char *dirname)
715{
716 char *token = strtok(dirname, "/");
717 int i = 0;
718
719 /* add root */
720 arr[i] = zalloc(strlen("/") + 1);
721 if (!arr[i])
722 return -ENOMEM;
Stephen Warren934b14f2015-09-04 22:03:44 -0600723 memcpy(arr[i++], "/", strlen("/"));
Uma Shankared34f342012-05-25 21:22:49 +0530724
725 /* add each path entry after root */
726 while (token != NULL) {
727 arr[i] = zalloc(strlen(token) + 1);
728 if (!arr[i])
729 return -ENOMEM;
730 memcpy(arr[i++], token, strlen(token));
731 token = strtok(NULL, "/");
732 }
733 arr[i] = NULL;
734
735 return 0;
736}
737
738int ext4fs_iget(int inode_no, struct ext2_inode *inode)
739{
740 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
741 return -1;
742
743 return 0;
744}
745
746/*
747 * Function: ext4fs_get_parent_inode_num
748 * Return Value: inode Number of the parent directory of file/Directory to be
749 * created
750 * dirname : Input parmater, input path name of the file/directory to be created
751 * dname : Output parameter, to be filled with the name of the directory
752 * extracted from dirname
753 */
754int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
755{
756 int i;
757 int depth = 0;
758 int matched_inode_no;
759 int result_inode_no = -1;
760 char **ptr = NULL;
761 char *depth_dirname = NULL;
762 char *parse_dirname = NULL;
763 struct ext2_inode *parent_inode = NULL;
764 struct ext2_inode *first_inode = NULL;
765 struct ext2_inode temp_inode;
766
Uma Shankared34f342012-05-25 21:22:49 +0530767 /* TODO: input validation make equivalent to linux */
768 depth_dirname = zalloc(strlen(dirname) + 1);
769 if (!depth_dirname)
770 return -ENOMEM;
771
772 memcpy(depth_dirname, dirname, strlen(dirname));
773 depth = find_dir_depth(depth_dirname);
774 parse_dirname = zalloc(strlen(dirname) + 1);
775 if (!parse_dirname)
776 goto fail;
777 memcpy(parse_dirname, dirname, strlen(dirname));
778
779 /* allocate memory for each directory level */
780 ptr = zalloc((depth) * sizeof(char *));
781 if (!ptr)
782 goto fail;
783 if (parse_path(ptr, parse_dirname))
784 goto fail;
785 parent_inode = zalloc(sizeof(struct ext2_inode));
786 if (!parent_inode)
787 goto fail;
788 first_inode = zalloc(sizeof(struct ext2_inode));
789 if (!first_inode)
790 goto fail;
791 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
792 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
793 if (flags & F_FILE)
794 result_inode_no = EXT2_ROOT_INO;
795 for (i = 1; i < depth; i++) {
796 matched_inode_no = search_dir(parent_inode, ptr[i]);
797 if (matched_inode_no == -1) {
798 if (ptr[i + 1] == NULL && i == 1) {
799 result_inode_no = EXT2_ROOT_INO;
800 goto end;
801 } else {
802 if (ptr[i + 1] == NULL)
803 break;
804 printf("Invalid path\n");
805 result_inode_no = -1;
806 goto fail;
807 }
808 } else {
809 if (ptr[i + 1] != NULL) {
810 memset(parent_inode, '\0',
811 sizeof(struct ext2_inode));
812 if (ext4fs_iget(matched_inode_no,
813 parent_inode)) {
814 result_inode_no = -1;
815 goto fail;
816 }
817 result_inode_no = matched_inode_no;
818 } else {
819 break;
820 }
821 }
822 }
823
824end:
825 if (i == 1)
826 matched_inode_no = search_dir(first_inode, ptr[i]);
827 else
828 matched_inode_no = search_dir(parent_inode, ptr[i]);
829
830 if (matched_inode_no != -1) {
831 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200832 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankared34f342012-05-25 21:22:49 +0530833 printf("It is a Directory\n");
834 result_inode_no = -1;
835 goto fail;
836 }
837 }
838
839 if (strlen(ptr[i]) > 256) {
840 result_inode_no = -1;
841 goto fail;
842 }
843 memcpy(dname, ptr[i], strlen(ptr[i]));
844
845fail:
846 free(depth_dirname);
Mikhail Ilin04e63322022-11-22 11:00:55 +0300847 if (parse_dirname)
848 free(parse_dirname);
849 if (ptr) {
850 for (i = 0; i < depth; i++) {
851 if (!ptr[i])
852 break;
853 free(ptr[i]);
854 }
855 free(ptr);
Stephen Warren934b14f2015-09-04 22:03:44 -0600856 }
Mikhail Ilin04e63322022-11-22 11:00:55 +0300857 if (parent_inode)
858 free(parent_inode);
859 if (first_inode)
860 free(first_inode);
Uma Shankared34f342012-05-25 21:22:49 +0530861
862 return result_inode_no;
863}
864
Stefan Brüns76a29512016-09-06 04:36:41 +0200865static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankared34f342012-05-25 21:22:49 +0530866{
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200867 int status;
868 int inodeno = 0;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200869 int offset;
870 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530871 struct ext2_dirent *dir = NULL;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200872 struct ext2_dirent *previous_dir;
Uma Shankared34f342012-05-25 21:22:49 +0530873 struct ext_filesystem *fs = get_fs();
Stephen Warrend56b2012015-09-04 22:03:45 -0600874 int ret = -1;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200875 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530876
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200877 block_buffer = zalloc(fs->blksz);
878 if (!block_buffer)
Uma Shankared34f342012-05-25 21:22:49 +0530879 return -ENOMEM;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200880
881 /* read the directory block */
Stefan Brüns76a29512016-09-06 04:36:41 +0200882 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200883 fs->blksz, block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530884 if (status == 0)
885 goto fail;
886
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200887 offset = 0;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200888 do {
Ian Rayecdfb412017-11-08 15:35:10 +0000889 if (offset & 3) {
890 printf("Badly aligned ext2_dirent\n");
891 break;
892 }
893
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200894 previous_dir = dir;
895 dir = (struct ext2_dirent *)(block_buffer + offset);
896 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
897
898 int direntlen = le16_to_cpu(dir->direntlen);
899 if (direntlen < sizeof(struct ext2_dirent))
900 break;
901
Stefan Brüns76a29512016-09-06 04:36:41 +0200902 if (dir->inode && (strlen(filename) == dir->namelen) &&
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200903 (strncmp(direntname, filename, dir->namelen) == 0)) {
Stefan Brüns76a29512016-09-06 04:36:41 +0200904 inodeno = le32_to_cpu(dir->inode);
Stefan Brüns76a29512016-09-06 04:36:41 +0200905 break;
Uma Shankared34f342012-05-25 21:22:49 +0530906 }
907
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200908 offset += direntlen;
Uma Shankared34f342012-05-25 21:22:49 +0530909
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200910 } while (offset < fs->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530911
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200912 if (inodeno > 0) {
Stefan Brüns805e3e002016-10-09 20:15:28 +0200913 printf("file found, deleting\n");
914 if (ext4fs_log_journal(block_buffer, blknr))
915 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530916
Stefan Brüns805e3e002016-10-09 20:15:28 +0200917 if (previous_dir) {
918 /* merge dir entry with predecessor */
919 uint16_t new_len;
920 new_len = le16_to_cpu(previous_dir->direntlen);
921 new_len += le16_to_cpu(dir->direntlen);
922 previous_dir->direntlen = cpu_to_le16(new_len);
923 } else {
924 /* invalidate dir entry */
925 dir->inode = 0;
926 }
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200927 if (ext4fs_put_metadata(block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530928 goto fail;
Stephen Warrend56b2012015-09-04 22:03:45 -0600929 ret = inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530930 }
931fail:
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200932 free(block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530933
Stephen Warrend56b2012015-09-04 22:03:45 -0600934 return ret;
Uma Shankared34f342012-05-25 21:22:49 +0530935}
936
Stefan Brüns76a29512016-09-06 04:36:41 +0200937int ext4fs_filename_unlink(char *filename)
Uma Shankared34f342012-05-25 21:22:49 +0530938{
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200939 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530940 long int blknr = -1;
941 int inodeno = -1;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200942 uint32_t directory_blocks;
943
944 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
945 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankared34f342012-05-25 21:22:49 +0530946
947 /* read the block no allocated to a file */
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200948 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
Stephen Warrend5aee652019-01-30 12:58:05 -0700949 blknr = read_allocated_block(g_parent_inode, blk_idx, NULL);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200950 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530951 break;
Stefan Brüns76a29512016-09-06 04:36:41 +0200952 inodeno = unlink_filename(filename, blknr);
Uma Shankared34f342012-05-25 21:22:49 +0530953 if (inodeno != -1)
954 return inodeno;
955 }
956
957 return -1;
958}
959
Michael Walle58a9ecb2016-09-01 11:21:40 +0200960uint32_t ext4fs_get_new_blk_no(void)
Uma Shankared34f342012-05-25 21:22:49 +0530961{
962 short i;
963 short status;
964 int remainder;
965 unsigned int bg_idx;
966 static int prev_bg_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200967 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankared34f342012-05-25 21:22:49 +0530968 struct ext_filesystem *fs = get_fs();
969 char *journal_buffer = zalloc(fs->blksz);
970 char *zero_buffer = zalloc(fs->blksz);
971 if (!journal_buffer || !zero_buffer)
972 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530973
974 if (fs->first_pass_bbmap == 0) {
975 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200976 struct ext2_block_group *bgd = NULL;
977 bgd = ext4fs_get_group_descriptor(fs, i);
978 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
979 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
980 uint64_t b_bitmap_blk =
981 ext4fs_bg_get_block_id(bgd, fs);
982 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +0530983 memcpy(fs->blk_bmaps[i], zero_buffer,
984 fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200985 put_ext4(b_bitmap_blk * fs->blksz,
986 fs->blk_bmaps[i], fs->blksz);
987 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
988 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +0530989 }
990 fs->curr_blkno =
991 _get_new_blk_no(fs->blk_bmaps[i]);
992 if (fs->curr_blkno == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +0200993 /* block bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +0530994 continue;
995 fs->curr_blkno = fs->curr_blkno +
996 (i * fs->blksz * 8);
997 fs->first_pass_bbmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +0200998 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200999 ext4fs_sb_free_blocks_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001000 status = ext4fs_devread(b_bitmap_blk *
1001 fs->sect_perblk,
1002 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301003 journal_buffer);
1004 if (status == 0)
1005 goto fail;
1006 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001007 b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301008 goto fail;
1009 goto success;
1010 } else {
1011 debug("no space left on block group %d\n", i);
1012 }
1013 }
1014
1015 goto fail;
1016 } else {
Uma Shankared34f342012-05-25 21:22:49 +05301017 fs->curr_blkno++;
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001018restart:
Uma Shankared34f342012-05-25 21:22:49 +05301019 /* get the blockbitmap index respective to blockno */
Łukasz Majewski35dd0552014-05-06 09:36:04 +02001020 bg_idx = fs->curr_blkno / blk_per_grp;
1021 if (fs->blksz == 1024) {
Uma Shankared34f342012-05-25 21:22:49 +05301022 remainder = fs->curr_blkno % blk_per_grp;
1023 if (!remainder)
1024 bg_idx--;
1025 }
1026
1027 /*
1028 * To skip completely filled block group bitmaps
1029 * Optimize the block allocation
1030 */
1031 if (bg_idx >= fs->no_blkgrp)
1032 goto fail;
1033
Stefan Brüns688d0e72016-09-17 02:10:10 +02001034 struct ext2_block_group *bgd = NULL;
1035 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1036 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
Uma Shankared34f342012-05-25 21:22:49 +05301037 debug("block group %u is full. Skipping\n", bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001038 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1039 if (fs->blksz == 1024)
1040 fs->curr_blkno += 1;
Uma Shankared34f342012-05-25 21:22:49 +05301041 goto restart;
1042 }
1043
Stefan Brüns688d0e72016-09-17 02:10:10 +02001044 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1045 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1046 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +05301047 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001048 put_ext4(b_bitmap_blk * fs->blksz,
1049 zero_buffer, fs->blksz);
1050 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1051 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301052 }
1053
1054 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1055 bg_idx) != 0) {
1056 debug("going for restart for the block no %ld %u\n",
1057 fs->curr_blkno, bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001058 fs->curr_blkno++;
Uma Shankared34f342012-05-25 21:22:49 +05301059 goto restart;
1060 }
1061
1062 /* journal backup */
1063 if (prev_bg_bitmap_index != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001064 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301065 0, fs->blksz, journal_buffer);
1066 if (status == 0)
1067 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001068 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301069 goto fail;
1070
1071 prev_bg_bitmap_index = bg_idx;
1072 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001073 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001074 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301075 goto success;
1076 }
1077success:
1078 free(journal_buffer);
1079 free(zero_buffer);
1080
1081 return fs->curr_blkno;
1082fail:
1083 free(journal_buffer);
1084 free(zero_buffer);
1085
1086 return -1;
1087}
1088
1089int ext4fs_get_new_inode_no(void)
1090{
1091 short i;
1092 short status;
1093 unsigned int ibmap_idx;
1094 static int prev_inode_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001095 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankared34f342012-05-25 21:22:49 +05301096 struct ext_filesystem *fs = get_fs();
1097 char *journal_buffer = zalloc(fs->blksz);
1098 char *zero_buffer = zalloc(fs->blksz);
1099 if (!journal_buffer || !zero_buffer)
1100 goto fail;
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001101 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1102 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
Uma Shankared34f342012-05-25 21:22:49 +05301103
1104 if (fs->first_pass_ibmap == 0) {
1105 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001106 uint32_t free_inodes;
1107 struct ext2_block_group *bgd = NULL;
1108 bgd = ext4fs_get_group_descriptor(fs, i);
1109 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1110 if (free_inodes) {
1111 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1112 uint64_t i_bitmap_blk =
1113 ext4fs_bg_get_inode_id(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001114 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001115 bgd->bg_itable_unused = free_inodes;
1116 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1117 put_ext4(i_bitmap_blk * fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301118 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001119 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1120 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301121 memcpy(fs->inode_bmaps[i],
1122 zero_buffer, fs->blksz);
1123 }
1124 fs->curr_inode_no =
1125 _get_new_inode_no(fs->inode_bmaps[i]);
1126 if (fs->curr_inode_no == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001127 /* inode bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +05301128 continue;
1129 fs->curr_inode_no = fs->curr_inode_no +
1130 (i * inodes_per_grp);
1131 fs->first_pass_ibmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +02001132 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001133 if (has_gdt_chksum)
Stefan Brüns749e93e2016-09-20 01:13:01 +02001134 ext4fs_bg_itable_unused_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001135 ext4fs_sb_free_inodes_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001136 status = ext4fs_devread(i_bitmap_blk *
1137 fs->sect_perblk,
1138 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301139 journal_buffer);
1140 if (status == 0)
1141 goto fail;
1142 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001143 i_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301144 goto fail;
1145 goto success;
1146 } else
1147 debug("no inode left on block group %d\n", i);
1148 }
1149 goto fail;
1150 } else {
1151restart:
1152 fs->curr_inode_no++;
1153 /* get the blockbitmap index respective to blockno */
1154 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001155 struct ext2_block_group *bgd =
1156 ext4fs_get_group_descriptor(fs, ibmap_idx);
1157 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1158 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1159
1160 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1161 put_ext4(i_bitmap_blk * fs->blksz,
Michael Walle58a9ecb2016-09-01 11:21:40 +02001162 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001163 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1164 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301165 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1166 fs->blksz);
1167 }
1168
1169 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1170 fs->inode_bmaps[ibmap_idx],
1171 ibmap_idx) != 0) {
1172 debug("going for restart for the block no %d %u\n",
1173 fs->curr_inode_no, ibmap_idx);
1174 goto restart;
1175 }
1176
1177 /* journal backup */
1178 if (prev_inode_bitmap_index != ibmap_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001179 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301180 0, fs->blksz, journal_buffer);
1181 if (status == 0)
1182 goto fail;
1183 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001184 le32_to_cpu(bgd->inode_id)))
Uma Shankared34f342012-05-25 21:22:49 +05301185 goto fail;
1186 prev_inode_bitmap_index = ibmap_idx;
1187 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001188 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001189 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001190 bgd->bg_itable_unused = bgd->free_inodes;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001191 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301192 goto success;
1193 }
1194
1195success:
1196 free(journal_buffer);
1197 free(zero_buffer);
1198
1199 return fs->curr_inode_no;
1200fail:
1201 free(journal_buffer);
1202 free(zero_buffer);
1203
1204 return -1;
1205
1206}
1207
1208
1209static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1210 unsigned int *total_remaining_blocks,
1211 unsigned int *no_blks_reqd)
1212{
1213 short i;
1214 short status;
1215 long int actual_block_no;
1216 long int si_blockno;
1217 /* si :single indirect */
Michael Walle58a9ecb2016-09-01 11:21:40 +02001218 __le32 *si_buffer = NULL;
1219 __le32 *si_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301220 struct ext_filesystem *fs = get_fs();
1221
1222 if (*total_remaining_blocks != 0) {
1223 si_buffer = zalloc(fs->blksz);
1224 if (!si_buffer) {
1225 printf("No Memory\n");
1226 return;
1227 }
1228 si_start_addr = si_buffer;
1229 si_blockno = ext4fs_get_new_blk_no();
1230 if (si_blockno == -1) {
1231 printf("no block left to assign\n");
1232 goto fail;
1233 }
1234 (*no_blks_reqd)++;
1235 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1236
Frederic Leroy04735e92013-06-26 18:11:25 +02001237 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301238 0, fs->blksz, (char *)si_buffer);
1239 memset(si_buffer, '\0', fs->blksz);
1240 if (status == 0)
1241 goto fail;
1242
1243 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1244 actual_block_no = ext4fs_get_new_blk_no();
1245 if (actual_block_no == -1) {
1246 printf("no block left to assign\n");
1247 goto fail;
1248 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001249 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301250 debug("SIAB %u: %u\n", *si_buffer,
1251 *total_remaining_blocks);
1252
1253 si_buffer++;
1254 (*total_remaining_blocks)--;
1255 if (*total_remaining_blocks == 0)
1256 break;
1257 }
1258
1259 /* write the block to disk */
Ma Haijun05508702014-01-08 08:15:33 +08001260 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301261 si_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001262 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301263 }
1264fail:
1265 free(si_start_addr);
1266}
1267
1268static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1269 unsigned int *total_remaining_blocks,
1270 unsigned int *no_blks_reqd)
1271{
1272 short i;
1273 short j;
1274 short status;
1275 long int actual_block_no;
1276 /* di:double indirect */
1277 long int di_blockno_parent;
1278 long int di_blockno_child;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001279 __le32 *di_parent_buffer = NULL;
1280 __le32 *di_child_buff = NULL;
1281 __le32 *di_block_start_addr = NULL;
1282 __le32 *di_child_buff_start = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301283 struct ext_filesystem *fs = get_fs();
1284
1285 if (*total_remaining_blocks != 0) {
1286 /* double indirect parent block connecting to inode */
1287 di_blockno_parent = ext4fs_get_new_blk_no();
1288 if (di_blockno_parent == -1) {
1289 printf("no block left to assign\n");
1290 goto fail;
1291 }
1292 di_parent_buffer = zalloc(fs->blksz);
1293 if (!di_parent_buffer)
1294 goto fail;
1295
1296 di_block_start_addr = di_parent_buffer;
1297 (*no_blks_reqd)++;
1298 debug("DIPB %ld: %u\n", di_blockno_parent,
1299 *total_remaining_blocks);
1300
Frederic Leroy04735e92013-06-26 18:11:25 +02001301 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankared34f342012-05-25 21:22:49 +05301302 fs->sect_perblk, 0,
1303 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001304
1305 if (!status) {
1306 printf("%s: Device read error!\n", __func__);
1307 goto fail;
1308 }
Uma Shankared34f342012-05-25 21:22:49 +05301309 memset(di_parent_buffer, '\0', fs->blksz);
1310
1311 /*
1312 * start:for each double indirect parent
1313 * block create one more block
1314 */
1315 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1316 di_blockno_child = ext4fs_get_new_blk_no();
1317 if (di_blockno_child == -1) {
1318 printf("no block left to assign\n");
1319 goto fail;
1320 }
1321 di_child_buff = zalloc(fs->blksz);
1322 if (!di_child_buff)
1323 goto fail;
1324
1325 di_child_buff_start = di_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001326 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankared34f342012-05-25 21:22:49 +05301327 di_parent_buffer++;
1328 (*no_blks_reqd)++;
1329 debug("DICB %ld: %u\n", di_blockno_child,
1330 *total_remaining_blocks);
1331
Frederic Leroy04735e92013-06-26 18:11:25 +02001332 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankared34f342012-05-25 21:22:49 +05301333 fs->sect_perblk, 0,
1334 fs->blksz,
1335 (char *)di_child_buff);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001336
1337 if (!status) {
1338 printf("%s: Device read error!\n", __func__);
1339 goto fail;
1340 }
Uma Shankared34f342012-05-25 21:22:49 +05301341 memset(di_child_buff, '\0', fs->blksz);
1342 /* filling of actual datablocks for each child */
1343 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1344 actual_block_no = ext4fs_get_new_blk_no();
1345 if (actual_block_no == -1) {
1346 printf("no block left to assign\n");
1347 goto fail;
1348 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001349 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301350 debug("DIAB %ld: %u\n", actual_block_no,
1351 *total_remaining_blocks);
1352
1353 di_child_buff++;
1354 (*total_remaining_blocks)--;
1355 if (*total_remaining_blocks == 0)
1356 break;
1357 }
1358 /* write the block table */
Ma Haijun05508702014-01-08 08:15:33 +08001359 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301360 di_child_buff_start, fs->blksz);
1361 free(di_child_buff_start);
1362 di_child_buff_start = NULL;
1363
1364 if (*total_remaining_blocks == 0)
1365 break;
1366 }
Ma Haijun05508702014-01-08 08:15:33 +08001367 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301368 di_block_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001369 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankared34f342012-05-25 21:22:49 +05301370 }
1371fail:
1372 free(di_block_start_addr);
1373}
1374
1375static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1376 unsigned int *total_remaining_blocks,
1377 unsigned int *no_blks_reqd)
1378{
1379 short i;
1380 short j;
1381 short k;
1382 long int actual_block_no;
1383 /* ti: Triple Indirect */
1384 long int ti_gp_blockno;
1385 long int ti_parent_blockno;
1386 long int ti_child_blockno;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001387 __le32 *ti_gp_buff = NULL;
1388 __le32 *ti_parent_buff = NULL;
1389 __le32 *ti_child_buff = NULL;
1390 __le32 *ti_gp_buff_start_addr = NULL;
1391 __le32 *ti_pbuff_start_addr = NULL;
1392 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301393 struct ext_filesystem *fs = get_fs();
1394 if (*total_remaining_blocks != 0) {
1395 /* triple indirect grand parent block connecting to inode */
1396 ti_gp_blockno = ext4fs_get_new_blk_no();
1397 if (ti_gp_blockno == -1) {
1398 printf("no block left to assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001399 return;
Uma Shankared34f342012-05-25 21:22:49 +05301400 }
1401 ti_gp_buff = zalloc(fs->blksz);
1402 if (!ti_gp_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001403 return;
Uma Shankared34f342012-05-25 21:22:49 +05301404
1405 ti_gp_buff_start_addr = ti_gp_buff;
1406 (*no_blks_reqd)++;
1407 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1408 *total_remaining_blocks);
1409
1410 /* for each 4 byte grand parent entry create one more block */
1411 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1412 ti_parent_blockno = ext4fs_get_new_blk_no();
1413 if (ti_parent_blockno == -1) {
1414 printf("no block left to assign\n");
1415 goto fail;
1416 }
1417 ti_parent_buff = zalloc(fs->blksz);
1418 if (!ti_parent_buff)
1419 goto fail;
1420
1421 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001422 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301423 ti_gp_buff++;
1424 (*no_blks_reqd)++;
1425 debug("TIPB %ld: %u\n", ti_parent_blockno,
1426 *total_remaining_blocks);
1427
1428 /* for each 4 byte entry parent create one more block */
1429 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1430 ti_child_blockno = ext4fs_get_new_blk_no();
1431 if (ti_child_blockno == -1) {
1432 printf("no block left assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001433 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301434 }
1435 ti_child_buff = zalloc(fs->blksz);
1436 if (!ti_child_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001437 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301438
1439 ti_cbuff_start_addr = ti_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001440 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301441 ti_parent_buff++;
1442 (*no_blks_reqd)++;
1443 debug("TICB %ld: %u\n", ti_parent_blockno,
1444 *total_remaining_blocks);
1445
1446 /* fill actual datablocks for each child */
1447 for (k = 0; k < (fs->blksz / sizeof(int));
1448 k++) {
1449 actual_block_no =
1450 ext4fs_get_new_blk_no();
1451 if (actual_block_no == -1) {
1452 printf("no block left\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001453 free(ti_cbuff_start_addr);
1454 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301455 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001456 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301457 debug("TIAB %ld: %u\n", actual_block_no,
1458 *total_remaining_blocks);
1459
1460 ti_child_buff++;
1461 (*total_remaining_blocks)--;
1462 if (*total_remaining_blocks == 0)
1463 break;
1464 }
1465 /* write the child block */
Ma Haijun05508702014-01-08 08:15:33 +08001466 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1467 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301468 ti_cbuff_start_addr, fs->blksz);
1469 free(ti_cbuff_start_addr);
1470
1471 if (*total_remaining_blocks == 0)
1472 break;
1473 }
1474 /* write the parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001475 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301476 ti_pbuff_start_addr, fs->blksz);
1477 free(ti_pbuff_start_addr);
1478
1479 if (*total_remaining_blocks == 0)
1480 break;
1481 }
1482 /* write the grand parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001483 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301484 ti_gp_buff_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001485 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini495c3a12015-12-10 16:42:21 -05001486 free(ti_gp_buff_start_addr);
1487 return;
Uma Shankared34f342012-05-25 21:22:49 +05301488 }
Tom Rini495c3a12015-12-10 16:42:21 -05001489fail1:
1490 free(ti_pbuff_start_addr);
Uma Shankared34f342012-05-25 21:22:49 +05301491fail:
1492 free(ti_gp_buff_start_addr);
1493}
1494
1495void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1496 unsigned int total_remaining_blocks,
1497 unsigned int *total_no_of_block)
1498{
1499 short i;
1500 long int direct_blockno;
1501 unsigned int no_blks_reqd = 0;
1502
1503 /* allocation of direct blocks */
Stephen Warrend0180282014-06-11 12:46:16 -06001504 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankared34f342012-05-25 21:22:49 +05301505 direct_blockno = ext4fs_get_new_blk_no();
1506 if (direct_blockno == -1) {
1507 printf("no block left to assign\n");
1508 return;
1509 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001510 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301511 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1512
1513 total_remaining_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +05301514 }
1515
1516 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1517 &no_blks_reqd);
1518 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1519 &no_blks_reqd);
1520 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1521 &no_blks_reqd);
1522 *total_no_of_block += no_blks_reqd;
1523}
1524
1525#endif
1526
Uma Shankara1596432012-05-25 21:21:44 +05301527static struct ext4_extent_header *ext4fs_get_extent_block
Stephen Warrend5aee652019-01-30 12:58:05 -07001528 (struct ext2_data *data, struct ext_block_cache *cache,
Uma Shankara1596432012-05-25 21:21:44 +05301529 struct ext4_extent_header *ext_block,
1530 uint32_t fileblock, int log2_blksz)
1531{
1532 struct ext4_extent_idx *index;
1533 unsigned long long block;
Ionut Nicu47017322014-01-13 11:59:24 +01001534 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankara1596432012-05-25 21:21:44 +05301535 int i;
1536
1537 while (1) {
1538 index = (struct ext4_extent_idx *)(ext_block + 1);
1539
Rommel Custodio8b415f72013-07-21 10:53:25 +02001540 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001541 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301542
1543 if (ext_block->eh_depth == 0)
1544 return ext_block;
1545 i = -1;
1546 do {
1547 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001548 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301549 break;
Ionut Nicub5bbac12014-01-13 12:00:08 +01001550 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankara1596432012-05-25 21:21:44 +05301551
Gero Schumacher1c48fda2019-02-26 15:45:22 +00001552 /*
1553 * If first logical block number is higher than requested fileblock,
1554 * it is a sparse file. This is handled on upper layer.
1555 */
1556 if (i > 0)
1557 i--;
Uma Shankara1596432012-05-25 21:21:44 +05301558
Rommel Custodio8b415f72013-07-21 10:53:25 +02001559 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301560 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
Stephen Warrend5aee652019-01-30 12:58:05 -07001561 block <<= log2_blksz;
1562 if (!ext_cache_read(cache, (lbaint_t)block, blksz))
Michael Walle58a9ecb2016-09-01 11:21:40 +02001563 return NULL;
Stephen Warrend5aee652019-01-30 12:58:05 -07001564 ext_block = (struct ext4_extent_header *)cache->buf;
Uma Shankara1596432012-05-25 21:21:44 +05301565 }
1566}
1567
1568static int ext4fs_blockgroup
1569 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1570{
1571 long int blkno;
1572 unsigned int blkoff, desc_per_blk;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001573 int log2blksz = get_fs()->dev_desc->log2blksz;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001574 int desc_size = get_fs()->gdsize;
Uma Shankara1596432012-05-25 21:21:44 +05301575
Paul Emge084be432019-07-08 16:37:06 -07001576 if (desc_size == 0)
1577 return 0;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001578 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301579
Paul Emge084be432019-07-08 16:37:06 -07001580 if (desc_per_blk == 0)
1581 return 0;
Michael Walle7f101be2016-08-29 10:46:44 +02001582 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankara1596432012-05-25 21:21:44 +05301583 group / desc_per_blk;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001584 blkoff = (group % desc_per_blk) * desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301585
1586 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1587 group, blkno, blkoff);
1588
Frederic Leroy04735e92013-06-26 18:11:25 +02001589 return ext4fs_devread((lbaint_t)blkno <<
1590 (LOG2_BLOCK_SIZE(data) - log2blksz),
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001591 blkoff, desc_size, (char *)blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301592}
1593
1594int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1595{
Benjamin Limfebbc582019-03-29 07:29:45 -04001596 struct ext2_block_group *blkgrp;
Uma Shankara1596432012-05-25 21:21:44 +05301597 struct ext2_sblock *sblock = &data->sblock;
1598 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00001599 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301600 int inodes_per_block, status;
1601 long int blkno;
1602 unsigned int blkoff;
1603
Benjamin Limfebbc582019-03-29 07:29:45 -04001604 /* Allocate blkgrp based on gdsize (for 64-bit support). */
1605 blkgrp = zalloc(get_fs()->gdsize);
1606 if (!blkgrp)
1607 return 0;
1608
Uma Shankara1596432012-05-25 21:21:44 +05301609 /* It is easier to calculate if the first inode is 0. */
1610 ino--;
Paul Emge084be432019-07-08 16:37:06 -07001611 if ( le32_to_cpu(sblock->inodes_per_group) == 0 || fs->inodesz == 0) {
1612 free(blkgrp);
1613 return 0;
1614 }
Michael Walle7f101be2016-08-29 10:46:44 +02001615 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Benjamin Limfebbc582019-03-29 07:29:45 -04001616 (sblock->inodes_per_group), blkgrp);
1617 if (status == 0) {
1618 free(blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301619 return 0;
Benjamin Limfebbc582019-03-29 07:29:45 -04001620 }
Uma Shankara1596432012-05-25 21:21:44 +05301621
1622 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Paul Emge084be432019-07-08 16:37:06 -07001623 if ( inodes_per_block == 0 ) {
1624 free(blkgrp);
1625 return 0;
1626 }
Benjamin Limfebbc582019-03-29 07:29:45 -04001627 blkno = ext4fs_bg_get_inode_table_id(blkgrp, fs) +
Michael Walle7f101be2016-08-29 10:46:44 +02001628 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankara1596432012-05-25 21:21:44 +05301629 blkoff = (ino % inodes_per_block) * fs->inodesz;
Benjamin Limfebbc582019-03-29 07:29:45 -04001630
1631 /* Free blkgrp as it is no longer required. */
1632 free(blkgrp);
1633
Uma Shankara1596432012-05-25 21:21:44 +05301634 /* Read the inode. */
Frederic Leroy04735e92013-06-26 18:11:25 +02001635 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1636 log2blksz), blkoff,
Uma Shankara1596432012-05-25 21:21:44 +05301637 sizeof(struct ext2_inode), (char *)inode);
1638 if (status == 0)
1639 return 0;
1640
1641 return 1;
1642}
1643
Stephen Warrend5aee652019-01-30 12:58:05 -07001644long int read_allocated_block(struct ext2_inode *inode, int fileblock,
1645 struct ext_block_cache *cache)
Uma Shankara1596432012-05-25 21:21:44 +05301646{
1647 long int blknr;
1648 int blksz;
1649 int log2_blksz;
1650 int status;
1651 long int rblock;
1652 long int perblock_parent;
1653 long int perblock_child;
1654 unsigned long long start;
1655 /* get the blocksize of the filesystem */
1656 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +00001657 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1658 - get_fs()->dev_desc->log2blksz;
1659
Uma Shankara1596432012-05-25 21:21:44 +05301660 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
Stefan Brünsf81db562016-11-05 22:17:14 +01001661 long int startblock, endblock;
Stephen Warrend5aee652019-01-30 12:58:05 -07001662 struct ext_block_cache *c, cd;
Uma Shankara1596432012-05-25 21:21:44 +05301663 struct ext4_extent_header *ext_block;
1664 struct ext4_extent *extent;
Stefan Brünsf81db562016-11-05 22:17:14 +01001665 int i;
Stephen Warrend5aee652019-01-30 12:58:05 -07001666
1667 if (cache) {
1668 c = cache;
1669 } else {
1670 c = &cd;
1671 ext_cache_init(c);
1672 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001673 ext_block =
Stephen Warrend5aee652019-01-30 12:58:05 -07001674 ext4fs_get_extent_block(ext4fs_root, c,
Egbert Eich50ce4c02013-05-01 01:13:19 +00001675 (struct ext4_extent_header *)
1676 inode->b.blocks.dir_blocks,
1677 fileblock, log2_blksz);
Uma Shankara1596432012-05-25 21:21:44 +05301678 if (!ext_block) {
1679 printf("invalid extent block\n");
Stephen Warrend5aee652019-01-30 12:58:05 -07001680 if (!cache)
1681 ext_cache_fini(c);
Uma Shankara1596432012-05-25 21:21:44 +05301682 return -EINVAL;
1683 }
1684
1685 extent = (struct ext4_extent *)(ext_block + 1);
1686
Stefan Brünsf81db562016-11-05 22:17:14 +01001687 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1688 startblock = le32_to_cpu(extent[i].ee_block);
1689 endblock = startblock + le16_to_cpu(extent[i].ee_len);
1690
1691 if (startblock > fileblock) {
1692 /* Sparse file */
Stephen Warrend5aee652019-01-30 12:58:05 -07001693 if (!cache)
1694 ext_cache_fini(c);
Uma Shankara1596432012-05-25 21:21:44 +05301695 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05301696
Stefan Brünsf81db562016-11-05 22:17:14 +01001697 } else if (fileblock < endblock) {
1698 start = le16_to_cpu(extent[i].ee_start_hi);
1699 start = (start << 32) +
Uma Shankara1596432012-05-25 21:21:44 +05301700 le32_to_cpu(extent[i].ee_start_lo);
Stephen Warrend5aee652019-01-30 12:58:05 -07001701 if (!cache)
1702 ext_cache_fini(c);
Stefan Brünsf81db562016-11-05 22:17:14 +01001703 return (fileblock - startblock) + start;
1704 }
Uma Shankara1596432012-05-25 21:21:44 +05301705 }
1706
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 0;
Uma Shankara1596432012-05-25 21:21:44 +05301710 }
1711
1712 /* Direct blocks. */
1713 if (fileblock < INDIRECT_BLOCKS)
Michael Walle7f101be2016-08-29 10:46:44 +02001714 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301715
1716 /* Indirect. */
1717 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1718 if (ext4fs_indir1_block == NULL) {
1719 ext4fs_indir1_block = zalloc(blksz);
1720 if (ext4fs_indir1_block == NULL) {
1721 printf("** SI ext2fs read block (indir 1)"
1722 "malloc failed. **\n");
1723 return -1;
1724 }
1725 ext4fs_indir1_size = blksz;
1726 ext4fs_indir1_blkno = -1;
1727 }
1728 if (blksz != ext4fs_indir1_size) {
1729 free(ext4fs_indir1_block);
1730 ext4fs_indir1_block = NULL;
1731 ext4fs_indir1_size = 0;
1732 ext4fs_indir1_blkno = -1;
1733 ext4fs_indir1_block = zalloc(blksz);
1734 if (ext4fs_indir1_block == NULL) {
1735 printf("** SI ext2fs read block (indir 1):"
1736 "malloc failed. **\n");
1737 return -1;
1738 }
1739 ext4fs_indir1_size = blksz;
1740 }
Michael Walle7f101be2016-08-29 10:46:44 +02001741 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301742 log2_blksz) != ext4fs_indir1_blkno) {
1743 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001744 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301745 (inode->b.blocks.
1746 indir_block) << log2_blksz, 0,
1747 blksz, (char *)ext4fs_indir1_block);
1748 if (status == 0) {
1749 printf("** SI ext2fs read block (indir 1)"
1750 "failed. **\n");
Stefan Brünsde9e8312016-09-06 04:36:55 +02001751 return -1;
Uma Shankara1596432012-05-25 21:21:44 +05301752 }
1753 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001754 le32_to_cpu(inode->b.blocks.
Uma Shankara1596432012-05-25 21:21:44 +05301755 indir_block) << log2_blksz;
1756 }
Michael Walle7f101be2016-08-29 10:46:44 +02001757 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankara1596432012-05-25 21:21:44 +05301758 [fileblock - INDIRECT_BLOCKS]);
1759 }
1760 /* Double indirect. */
1761 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1762 (blksz / 4 + 1)))) {
1763
1764 long int perblock = blksz / 4;
1765 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1766
1767 if (ext4fs_indir1_block == NULL) {
1768 ext4fs_indir1_block = zalloc(blksz);
1769 if (ext4fs_indir1_block == NULL) {
1770 printf("** DI ext2fs read block (indir 2 1)"
1771 "malloc failed. **\n");
1772 return -1;
1773 }
1774 ext4fs_indir1_size = blksz;
1775 ext4fs_indir1_blkno = -1;
1776 }
1777 if (blksz != ext4fs_indir1_size) {
1778 free(ext4fs_indir1_block);
1779 ext4fs_indir1_block = NULL;
1780 ext4fs_indir1_size = 0;
1781 ext4fs_indir1_blkno = -1;
1782 ext4fs_indir1_block = zalloc(blksz);
1783 if (ext4fs_indir1_block == NULL) {
1784 printf("** DI ext2fs read block (indir 2 1)"
1785 "malloc failed. **\n");
1786 return -1;
1787 }
1788 ext4fs_indir1_size = blksz;
1789 }
Michael Walle7f101be2016-08-29 10:46:44 +02001790 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301791 log2_blksz) != ext4fs_indir1_blkno) {
1792 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001793 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301794 (inode->b.blocks.
1795 double_indir_block) << log2_blksz,
1796 0, blksz,
1797 (char *)ext4fs_indir1_block);
1798 if (status == 0) {
1799 printf("** DI ext2fs read block (indir 2 1)"
1800 "failed. **\n");
1801 return -1;
1802 }
1803 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001804 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301805 log2_blksz;
1806 }
1807
1808 if (ext4fs_indir2_block == NULL) {
1809 ext4fs_indir2_block = zalloc(blksz);
1810 if (ext4fs_indir2_block == NULL) {
1811 printf("** DI ext2fs read block (indir 2 2)"
1812 "malloc failed. **\n");
1813 return -1;
1814 }
1815 ext4fs_indir2_size = blksz;
1816 ext4fs_indir2_blkno = -1;
1817 }
1818 if (blksz != ext4fs_indir2_size) {
1819 free(ext4fs_indir2_block);
1820 ext4fs_indir2_block = NULL;
1821 ext4fs_indir2_size = 0;
1822 ext4fs_indir2_blkno = -1;
1823 ext4fs_indir2_block = zalloc(blksz);
1824 if (ext4fs_indir2_block == NULL) {
1825 printf("** DI ext2fs read block (indir 2 2)"
1826 "malloc failed. **\n");
1827 return -1;
1828 }
1829 ext4fs_indir2_size = blksz;
1830 }
Michael Walle7f101be2016-08-29 10:46:44 +02001831 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankara1596432012-05-25 21:21:44 +05301832 log2_blksz) != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001833 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301834 (ext4fs_indir1_block
1835 [rblock /
1836 perblock]) << log2_blksz, 0,
1837 blksz,
1838 (char *)ext4fs_indir2_block);
1839 if (status == 0) {
1840 printf("** DI ext2fs read block (indir 2 2)"
1841 "failed. **\n");
1842 return -1;
1843 }
1844 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001845 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301846 /
1847 perblock]) <<
1848 log2_blksz;
1849 }
Michael Walle7f101be2016-08-29 10:46:44 +02001850 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301851 }
1852 /* Tripple indirect. */
1853 else {
1854 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1855 (blksz / 4 * blksz / 4));
1856 perblock_child = blksz / 4;
1857 perblock_parent = ((blksz / 4) * (blksz / 4));
1858
1859 if (ext4fs_indir1_block == NULL) {
1860 ext4fs_indir1_block = zalloc(blksz);
1861 if (ext4fs_indir1_block == NULL) {
1862 printf("** TI ext2fs read block (indir 2 1)"
1863 "malloc failed. **\n");
1864 return -1;
1865 }
1866 ext4fs_indir1_size = blksz;
1867 ext4fs_indir1_blkno = -1;
1868 }
1869 if (blksz != ext4fs_indir1_size) {
1870 free(ext4fs_indir1_block);
1871 ext4fs_indir1_block = NULL;
1872 ext4fs_indir1_size = 0;
1873 ext4fs_indir1_blkno = -1;
1874 ext4fs_indir1_block = zalloc(blksz);
1875 if (ext4fs_indir1_block == NULL) {
1876 printf("** TI ext2fs read block (indir 2 1)"
1877 "malloc failed. **\n");
1878 return -1;
1879 }
1880 ext4fs_indir1_size = blksz;
1881 }
Michael Walle7f101be2016-08-29 10:46:44 +02001882 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301883 log2_blksz) != ext4fs_indir1_blkno) {
1884 status = ext4fs_devread
Frederic Leroy04735e92013-06-26 18:11:25 +02001885 ((lbaint_t)
Michael Walle7f101be2016-08-29 10:46:44 +02001886 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankara1596432012-05-25 21:21:44 +05301887 << log2_blksz, 0, blksz,
1888 (char *)ext4fs_indir1_block);
1889 if (status == 0) {
1890 printf("** TI ext2fs read block (indir 2 1)"
1891 "failed. **\n");
1892 return -1;
1893 }
1894 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001895 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301896 log2_blksz;
1897 }
1898
1899 if (ext4fs_indir2_block == NULL) {
1900 ext4fs_indir2_block = zalloc(blksz);
1901 if (ext4fs_indir2_block == NULL) {
1902 printf("** TI ext2fs read block (indir 2 2)"
1903 "malloc failed. **\n");
1904 return -1;
1905 }
1906 ext4fs_indir2_size = blksz;
1907 ext4fs_indir2_blkno = -1;
1908 }
1909 if (blksz != ext4fs_indir2_size) {
1910 free(ext4fs_indir2_block);
1911 ext4fs_indir2_block = NULL;
1912 ext4fs_indir2_size = 0;
1913 ext4fs_indir2_blkno = -1;
1914 ext4fs_indir2_block = zalloc(blksz);
1915 if (ext4fs_indir2_block == NULL) {
1916 printf("** TI ext2fs read block (indir 2 2)"
1917 "malloc failed. **\n");
1918 return -1;
1919 }
1920 ext4fs_indir2_size = blksz;
1921 }
Michael Walle7f101be2016-08-29 10:46:44 +02001922 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301923 perblock_parent]) <<
1924 log2_blksz)
1925 != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001926 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301927 (ext4fs_indir1_block
1928 [rblock /
1929 perblock_parent]) <<
1930 log2_blksz, 0, blksz,
1931 (char *)ext4fs_indir2_block);
1932 if (status == 0) {
1933 printf("** TI ext2fs read block (indir 2 2)"
1934 "failed. **\n");
1935 return -1;
1936 }
1937 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001938 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301939 perblock_parent])
1940 << log2_blksz;
1941 }
1942
1943 if (ext4fs_indir3_block == NULL) {
1944 ext4fs_indir3_block = zalloc(blksz);
1945 if (ext4fs_indir3_block == NULL) {
1946 printf("** TI ext2fs read block (indir 2 2)"
1947 "malloc failed. **\n");
1948 return -1;
1949 }
1950 ext4fs_indir3_size = blksz;
1951 ext4fs_indir3_blkno = -1;
1952 }
1953 if (blksz != ext4fs_indir3_size) {
1954 free(ext4fs_indir3_block);
1955 ext4fs_indir3_block = NULL;
1956 ext4fs_indir3_size = 0;
1957 ext4fs_indir3_blkno = -1;
1958 ext4fs_indir3_block = zalloc(blksz);
1959 if (ext4fs_indir3_block == NULL) {
1960 printf("** TI ext2fs read block (indir 2 2)"
1961 "malloc failed. **\n");
1962 return -1;
1963 }
1964 ext4fs_indir3_size = blksz;
1965 }
Michael Walle7f101be2016-08-29 10:46:44 +02001966 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301967 /
1968 perblock_child]) <<
1969 log2_blksz) != ext4fs_indir3_blkno) {
1970 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001971 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301972 (ext4fs_indir2_block
1973 [(rblock / perblock_child)
1974 % (blksz / 4)]) << log2_blksz, 0,
1975 blksz, (char *)ext4fs_indir3_block);
1976 if (status == 0) {
1977 printf("** TI ext2fs read block (indir 2 2)"
1978 "failed. **\n");
1979 return -1;
1980 }
1981 ext4fs_indir3_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001982 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301983 perblock_child) %
1984 (blksz /
1985 4)]) <<
1986 log2_blksz;
1987 }
1988
Michael Walle7f101be2016-08-29 10:46:44 +02001989 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankara1596432012-05-25 21:21:44 +05301990 [rblock % perblock_child]);
1991 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001992 debug("read_allocated_block %ld\n", blknr);
Uma Shankara1596432012-05-25 21:21:44 +05301993
1994 return blknr;
1995}
1996
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001997/**
1998 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1999 * global pointers
2000 *
2001 * This function assures that for a file with the same name but different size
2002 * the sequential store on the ext4 filesystem will be correct.
2003 *
2004 * In this function the global data, responsible for internal representation
2005 * of the ext4 data are initialized to the reset state. Without this, during
2006 * replacement of the smaller file with the bigger truncation of new file was
2007 * performed.
2008 */
2009void ext4fs_reinit_global(void)
Uma Shankara1596432012-05-25 21:21:44 +05302010{
Uma Shankara1596432012-05-25 21:21:44 +05302011 if (ext4fs_indir1_block != NULL) {
2012 free(ext4fs_indir1_block);
2013 ext4fs_indir1_block = NULL;
2014 ext4fs_indir1_size = 0;
2015 ext4fs_indir1_blkno = -1;
2016 }
2017 if (ext4fs_indir2_block != NULL) {
2018 free(ext4fs_indir2_block);
2019 ext4fs_indir2_block = NULL;
2020 ext4fs_indir2_size = 0;
2021 ext4fs_indir2_blkno = -1;
2022 }
2023 if (ext4fs_indir3_block != NULL) {
2024 free(ext4fs_indir3_block);
2025 ext4fs_indir3_block = NULL;
2026 ext4fs_indir3_size = 0;
2027 ext4fs_indir3_blkno = -1;
2028 }
2029}
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02002030void ext4fs_close(void)
2031{
2032 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
2033 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
2034 ext4fs_file = NULL;
2035 }
2036 if (ext4fs_root != NULL) {
2037 free(ext4fs_root);
2038 ext4fs_root = NULL;
2039 }
2040
2041 ext4fs_reinit_global();
2042}
Uma Shankara1596432012-05-25 21:21:44 +05302043
2044int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
2045 struct ext2fs_node **fnode, int *ftype)
2046{
2047 unsigned int fpos = 0;
2048 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002049 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302050 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2051
2052#ifdef DEBUG
2053 if (name != NULL)
2054 printf("Iterate dir %s\n", name);
2055#endif /* of DEBUG */
2056 if (!diro->inode_read) {
2057 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2058 if (status == 0)
2059 return 0;
2060 }
2061 /* Search the file. */
Michael Walle7f101be2016-08-29 10:46:44 +02002062 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankara1596432012-05-25 21:21:44 +05302063 struct ext2_dirent dirent;
2064
2065 status = ext4fs_read_file(diro, fpos,
2066 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002067 (char *)&dirent, &actread);
2068 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302069 return 0;
2070
Thomas Fitzsimmons54d68e92015-11-18 12:42:53 -05002071 if (dirent.direntlen == 0) {
2072 printf("Failed to iterate over directory %s\n", name);
2073 return 0;
2074 }
2075
Uma Shankara1596432012-05-25 21:21:44 +05302076 if (dirent.namelen != 0) {
2077 char filename[dirent.namelen + 1];
2078 struct ext2fs_node *fdiro;
2079 int type = FILETYPE_UNKNOWN;
2080
2081 status = ext4fs_read_file(diro,
2082 fpos +
2083 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002084 dirent.namelen, filename,
2085 &actread);
2086 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302087 return 0;
2088
2089 fdiro = zalloc(sizeof(struct ext2fs_node));
2090 if (!fdiro)
2091 return 0;
2092
2093 fdiro->data = diro->data;
Michael Walle7f101be2016-08-29 10:46:44 +02002094 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankara1596432012-05-25 21:21:44 +05302095
2096 filename[dirent.namelen] = '\0';
2097
2098 if (dirent.filetype != FILETYPE_UNKNOWN) {
2099 fdiro->inode_read = 0;
2100
2101 if (dirent.filetype == FILETYPE_DIRECTORY)
2102 type = FILETYPE_DIRECTORY;
2103 else if (dirent.filetype == FILETYPE_SYMLINK)
2104 type = FILETYPE_SYMLINK;
2105 else if (dirent.filetype == FILETYPE_REG)
2106 type = FILETYPE_REG;
2107 } else {
2108 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002109 le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05302110 (dirent.inode),
2111 &fdiro->inode);
2112 if (status == 0) {
2113 free(fdiro);
2114 return 0;
2115 }
2116 fdiro->inode_read = 1;
2117
Michael Walle7f101be2016-08-29 10:46:44 +02002118 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankara1596432012-05-25 21:21:44 +05302119 FILETYPE_INO_MASK) ==
2120 FILETYPE_INO_DIRECTORY) {
2121 type = FILETYPE_DIRECTORY;
Michael Walle7f101be2016-08-29 10:46:44 +02002122 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302123 & FILETYPE_INO_MASK) ==
2124 FILETYPE_INO_SYMLINK) {
2125 type = FILETYPE_SYMLINK;
Michael Walle7f101be2016-08-29 10:46:44 +02002126 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302127 & FILETYPE_INO_MASK) ==
2128 FILETYPE_INO_REG) {
2129 type = FILETYPE_REG;
2130 }
2131 }
2132#ifdef DEBUG
2133 printf("iterate >%s<\n", filename);
2134#endif /* of DEBUG */
2135 if ((name != NULL) && (fnode != NULL)
2136 && (ftype != NULL)) {
2137 if (strcmp(filename, name) == 0) {
2138 *ftype = type;
2139 *fnode = fdiro;
2140 return 1;
2141 }
2142 } else {
2143 if (fdiro->inode_read == 0) {
2144 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002145 le32_to_cpu(
Uma Shankara1596432012-05-25 21:21:44 +05302146 dirent.inode),
2147 &fdiro->inode);
2148 if (status == 0) {
2149 free(fdiro);
2150 return 0;
2151 }
2152 fdiro->inode_read = 1;
2153 }
2154 switch (type) {
2155 case FILETYPE_DIRECTORY:
2156 printf("<DIR> ");
2157 break;
2158 case FILETYPE_SYMLINK:
2159 printf("<SYM> ");
2160 break;
2161 case FILETYPE_REG:
2162 printf(" ");
2163 break;
2164 default:
2165 printf("< ? > ");
2166 break;
2167 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002168 printf("%10u %s\n",
Michael Walle7f101be2016-08-29 10:46:44 +02002169 le32_to_cpu(fdiro->inode.size),
Uma Shankara1596432012-05-25 21:21:44 +05302170 filename);
2171 }
2172 free(fdiro);
2173 }
Michael Walle7f101be2016-08-29 10:46:44 +02002174 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankara1596432012-05-25 21:21:44 +05302175 }
2176 return 0;
2177}
2178
2179static char *ext4fs_read_symlink(struct ext2fs_node *node)
2180{
2181 char *symlink;
2182 struct ext2fs_node *diro = node;
2183 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002184 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302185
2186 if (!diro->inode_read) {
2187 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2188 if (status == 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002189 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302190 }
Michael Walle7f101be2016-08-29 10:46:44 +02002191 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankara1596432012-05-25 21:21:44 +05302192 if (!symlink)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002193 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302194
Michael Walle7f101be2016-08-29 10:46:44 +02002195 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankara1596432012-05-25 21:21:44 +05302196 strncpy(symlink, diro->inode.b.symlink,
Michael Walle7f101be2016-08-29 10:46:44 +02002197 le32_to_cpu(diro->inode.size));
Uma Shankara1596432012-05-25 21:21:44 +05302198 } else {
2199 status = ext4fs_read_file(diro, 0,
Michael Walle7f101be2016-08-29 10:46:44 +02002200 le32_to_cpu(diro->inode.size),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002201 symlink, &actread);
Gary Bisson9d2f6a92015-09-07 11:20:07 +02002202 if ((status < 0) || (actread == 0)) {
Uma Shankara1596432012-05-25 21:21:44 +05302203 free(symlink);
Michael Walle58a9ecb2016-09-01 11:21:40 +02002204 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302205 }
2206 }
Michael Walle7f101be2016-08-29 10:46:44 +02002207 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankara1596432012-05-25 21:21:44 +05302208 return symlink;
2209}
2210
Heinrich Schuchardte70f04f2024-02-20 12:54:23 +01002211int ext4fs_find_file1(const char *currpath, struct ext2fs_node *currroot,
2212 struct ext2fs_node **currfound, int *foundtype)
Uma Shankara1596432012-05-25 21:21:44 +05302213{
2214 char fpath[strlen(currpath) + 1];
2215 char *name = fpath;
2216 char *next;
2217 int status;
2218 int type = FILETYPE_DIRECTORY;
2219 struct ext2fs_node *currnode = currroot;
2220 struct ext2fs_node *oldnode = currroot;
2221
2222 strncpy(fpath, currpath, strlen(currpath) + 1);
2223
2224 /* Remove all leading slashes. */
2225 while (*name == '/')
2226 name++;
2227
2228 if (!*name) {
2229 *currfound = currnode;
2230 return 1;
2231 }
2232
2233 for (;;) {
2234 int found;
2235
2236 /* Extract the actual part from the pathname. */
2237 next = strchr(name, '/');
2238 if (next) {
2239 /* Remove all leading slashes. */
2240 while (*next == '/')
2241 *(next++) = '\0';
2242 }
2243
2244 if (type != FILETYPE_DIRECTORY) {
2245 ext4fs_free_node(currnode, currroot);
2246 return 0;
2247 }
2248
2249 oldnode = currnode;
2250
2251 /* Iterate over the directory. */
2252 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2253 if (found == 0)
2254 return 0;
2255
2256 if (found == -1)
2257 break;
2258
2259 /* Read in the symlink and follow it. */
2260 if (type == FILETYPE_SYMLINK) {
2261 char *symlink;
2262
2263 /* Test if the symlink does not loop. */
2264 if (++symlinknest == 8) {
2265 ext4fs_free_node(currnode, currroot);
2266 ext4fs_free_node(oldnode, currroot);
2267 return 0;
2268 }
2269
2270 symlink = ext4fs_read_symlink(currnode);
2271 ext4fs_free_node(currnode, currroot);
2272
2273 if (!symlink) {
2274 ext4fs_free_node(oldnode, currroot);
2275 return 0;
2276 }
2277
2278 debug("Got symlink >%s<\n", symlink);
2279
2280 if (symlink[0] == '/') {
2281 ext4fs_free_node(oldnode, currroot);
2282 oldnode = &ext4fs_root->diropen;
2283 }
2284
2285 /* Lookup the node the symlink points to. */
2286 status = ext4fs_find_file1(symlink, oldnode,
2287 &currnode, &type);
2288
2289 free(symlink);
2290
2291 if (status == 0) {
2292 ext4fs_free_node(oldnode, currroot);
2293 return 0;
2294 }
2295 }
2296
2297 ext4fs_free_node(oldnode, currroot);
2298
2299 /* Found the node! */
2300 if (!next || *next == '\0') {
2301 *currfound = currnode;
2302 *foundtype = type;
2303 return 1;
2304 }
2305 name = next;
2306 }
2307 return -1;
2308}
2309
2310int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2311 struct ext2fs_node **foundnode, int expecttype)
2312{
2313 int status;
2314 int foundtype = FILETYPE_DIRECTORY;
2315
2316 symlinknest = 0;
2317 if (!path)
2318 return 0;
2319
2320 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2321 if (status == 0)
2322 return 0;
2323
2324 /* Check if the node that was found was of the expected type. */
2325 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2326 return 0;
2327 else if ((expecttype == FILETYPE_DIRECTORY)
2328 && (foundtype != expecttype))
2329 return 0;
2330
2331 return 1;
2332}
2333
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002334int ext4fs_open(const char *filename, loff_t *len)
Uma Shankara1596432012-05-25 21:21:44 +05302335{
2336 struct ext2fs_node *fdiro = NULL;
2337 int status;
Uma Shankara1596432012-05-25 21:21:44 +05302338
2339 if (ext4fs_root == NULL)
2340 return -1;
2341
2342 ext4fs_file = NULL;
2343 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2344 FILETYPE_REG);
2345 if (status == 0)
2346 goto fail;
2347
2348 if (!fdiro->inode_read) {
2349 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2350 &fdiro->inode);
2351 if (status == 0)
2352 goto fail;
2353 }
Michael Walle7f101be2016-08-29 10:46:44 +02002354 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankara1596432012-05-25 21:21:44 +05302355 ext4fs_file = fdiro;
2356
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002357 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05302358fail:
2359 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2360
2361 return -1;
2362}
2363
Sean Anderson7667bde2023-11-08 12:51:09 -05002364int ext4fs_mount(void)
Uma Shankara1596432012-05-25 21:21:44 +05302365{
2366 struct ext2_data *data;
2367 int status;
2368 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00002369 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankara1596432012-05-25 21:21:44 +05302370 if (!data)
2371 return 0;
2372
2373 /* Read the superblock. */
Egbert Eich50ce4c02013-05-01 01:13:19 +00002374 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankara1596432012-05-25 21:21:44 +05302375
2376 if (status == 0)
2377 goto fail;
2378
2379 /* Make sure this is an ext2 filesystem. */
Michael Walle7f101be2016-08-29 10:46:44 +02002380 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Marek Behún51be4712018-03-08 00:26:08 +01002381 goto fail_noerr;
Uma Shankara1596432012-05-25 21:21:44 +05302382
Tom Rini6f94ab62016-07-22 17:59:11 -04002383
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002384 if (le32_to_cpu(data->sblock.revision_level) == 0) {
Uma Shankara1596432012-05-25 21:21:44 +05302385 fs->inodesz = 128;
Stefan Brüns3cc5bbb2016-12-27 02:35:08 +01002386 fs->gdsize = 32;
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002387 } else {
2388 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2389 __le32_to_cpu(data->sblock.feature_compatibility),
2390 __le32_to_cpu(data->sblock.feature_incompat),
2391 __le32_to_cpu(data->sblock.feature_ro_compat));
Uma Shankara1596432012-05-25 21:21:44 +05302392
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002393 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2394 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2395 EXT4_FEATURE_INCOMPAT_64BIT ?
2396 le16_to_cpu(data->sblock.descriptor_size) : 32;
2397 }
2398
2399 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2400 le32_to_cpu(data->sblock.revision_level),
2401 fs->inodesz, fs->gdsize);
Uma Shankara1596432012-05-25 21:21:44 +05302402
2403 data->diropen.data = data;
2404 data->diropen.ino = 2;
2405 data->diropen.inode_read = 1;
2406 data->inode = &data->diropen.inode;
2407
2408 status = ext4fs_read_inode(data, 2, data->inode);
2409 if (status == 0)
2410 goto fail;
2411
2412 ext4fs_root = data;
2413
2414 return 1;
2415fail:
Simon Glassf337fb92022-10-11 09:47:11 -06002416 log_debug("Failed to mount ext2 filesystem...\n");
Marek Behún51be4712018-03-08 00:26:08 +01002417fail_noerr:
Uma Shankara1596432012-05-25 21:21:44 +05302418 free(data);
2419 ext4fs_root = NULL;
2420
2421 return 0;
2422}