blob: 5bf78b530a989108e9298f4c374f7794de10a9a5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Uma Shankara1596432012-05-25 21:21:44 +05302/*
3 * (C) Copyright 2011 - 2012 Samsung Electronics
4 * EXT4 filesystem implementation in Uboot by
5 * Uma Shankar <uma.shankar@samsung.com>
6 * Manjunatha C Achar <a.manjunatha@samsung.com>
7 *
8 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
9 *
10 * (C) Copyright 2004
11 * esd gmbh <www.esd-electronics.com>
12 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
13 *
14 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
15 * GRUB -- GRand Unified Bootloader
16 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
17 *
Uma Shankared34f342012-05-25 21:22:49 +053018 * ext4write : Based on generic ext4 protocol.
Uma Shankara1596432012-05-25 21:21:44 +053019 */
20
21#include <common.h>
22#include <ext_common.h>
23#include <ext4fs.h>
24#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060025#include <memalign.h>
Uma Shankara1596432012-05-25 21:21:44 +053026#include <stddef.h>
27#include <linux/stat.h>
28#include <linux/time.h>
29#include <asm/byteorder.h>
30#include "ext4_common.h"
31
32struct ext2_data *ext4fs_root;
33struct ext2fs_node *ext4fs_file;
Michael Walle58a9ecb2016-09-01 11:21:40 +020034__le32 *ext4fs_indir1_block;
Uma Shankara1596432012-05-25 21:21:44 +053035int ext4fs_indir1_size;
36int ext4fs_indir1_blkno = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +020037__le32 *ext4fs_indir2_block;
Uma Shankara1596432012-05-25 21:21:44 +053038int ext4fs_indir2_size;
39int ext4fs_indir2_blkno = -1;
40
Michael Walle58a9ecb2016-09-01 11:21:40 +020041__le32 *ext4fs_indir3_block;
Uma Shankara1596432012-05-25 21:21:44 +053042int ext4fs_indir3_size;
43int ext4fs_indir3_blkno = -1;
44struct ext2_inode *g_parent_inode;
45static int symlinknest;
46
Stephen Warren03e2ecf2012-10-22 06:43:50 +000047#if defined(CONFIG_EXT4_WRITE)
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +020048struct ext2_block_group *ext4fs_get_group_descriptor
49 (const struct ext_filesystem *fs, uint32_t bg_idx)
50{
51 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
52}
53
Michael Walle58a9ecb2016-09-01 11:21:40 +020054static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
55{
56 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
57}
58
59static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
60{
Stefan Brüns749e93e2016-09-20 01:13:01 +020061 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
62 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
63 free_blocks--;
64
65 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
66 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
Michael Walle58a9ecb2016-09-01 11:21:40 +020067}
68
Stefan Brüns749e93e2016-09-20 01:13:01 +020069static inline void ext4fs_bg_free_inodes_dec
70 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020071{
Stefan Brüns749e93e2016-09-20 01:13:01 +020072 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
73 if (fs->gdsize == 64)
74 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
75 free_inodes--;
76
77 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
78 if (fs->gdsize == 64)
79 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020080}
81
Stefan Brüns749e93e2016-09-20 01:13:01 +020082static inline void ext4fs_bg_free_blocks_dec
83 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020084{
Stefan Brüns749e93e2016-09-20 01:13:01 +020085 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
86 if (fs->gdsize == 64)
87 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
88 free_blocks--;
89
90 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
91 if (fs->gdsize == 64)
92 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020093}
94
Stefan Brüns749e93e2016-09-20 01:13:01 +020095static inline void ext4fs_bg_itable_unused_dec
96 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020097{
Stefan Brüns749e93e2016-09-20 01:13:01 +020098 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
99 if (fs->gdsize == 64)
100 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
101 free_inodes--;
102
103 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
104 if (fs->gdsize == 64)
105 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200106}
107
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +0200108uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
109{
110 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
111 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
112 return free_blocks;
113}
114
115void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
116{
117 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
118 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
119}
120
121uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
122 const struct ext_filesystem *fs)
123{
124 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
125 if (fs->gdsize == 64)
126 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
127 return free_blocks;
128}
129
130static inline
131uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
132 const struct ext_filesystem *fs)
133{
134 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
135 if (fs->gdsize == 64)
136 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
137 return free_inodes;
138}
139
140static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
141{
142 return le16_to_cpu(bg->bg_flags);
143}
144
145static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
146 uint16_t flags)
147{
148 bg->bg_flags = cpu_to_le16(flags);
149}
150
151/* Block number of the block bitmap */
152uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
153 const struct ext_filesystem *fs)
154{
155 uint64_t block_nr = le32_to_cpu(bg->block_id);
156 if (fs->gdsize == 64)
157 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
158 return block_nr;
159}
160
161/* Block number of the inode bitmap */
162uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
163 const struct ext_filesystem *fs)
164{
165 uint64_t block_nr = le32_to_cpu(bg->inode_id);
166 if (fs->gdsize == 64)
167 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
168 return block_nr;
169}
170#endif
171
172/* Block number of the inode table */
173uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
174 const struct ext_filesystem *fs)
175{
176 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
177 if (fs->gdsize == 64)
178 block_nr +=
179 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
180 return block_nr;
181}
182
183#if defined(CONFIG_EXT4_WRITE)
Uma Shankared34f342012-05-25 21:22:49 +0530184uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
185{
186 uint32_t res = size / n;
187 if (res * n != size)
188 res++;
189
190 return res;
191}
192
Jean-Jacques Hiblotb0001802019-02-13 12:15:24 +0100193void put_ext4(uint64_t off, const void *buf, uint32_t size)
Uma Shankared34f342012-05-25 21:22:49 +0530194{
195 uint64_t startblock;
196 uint64_t remainder;
197 unsigned char *temp_ptr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530198 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +0000199 int log2blksz = fs->dev_desc->log2blksz;
200 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530201
Egbert Eich50ce4c02013-05-01 01:13:19 +0000202 startblock = off >> log2blksz;
Uma Shankared34f342012-05-25 21:22:49 +0530203 startblock += part_offset;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000204 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankared34f342012-05-25 21:22:49 +0530205
206 if (fs->dev_desc == NULL)
207 return;
208
Egbert Eich50ce4c02013-05-01 01:13:19 +0000209 if ((startblock + (size >> log2blksz)) >
Uma Shankared34f342012-05-25 21:22:49 +0530210 (part_offset + fs->total_sect)) {
Frederic Leroy04735e92013-06-26 18:11:25 +0200211 printf("part_offset is " LBAFU "\n", part_offset);
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900212 printf("total_sector is %llu\n", fs->total_sect);
Uma Shankared34f342012-05-25 21:22:49 +0530213 printf("error: overflow occurs\n");
214 return;
215 }
216
217 if (remainder) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700218 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
219 temp_ptr = sec_buf;
220 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
221 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530222 } else {
Egbert Eich50ce4c02013-05-01 01:13:19 +0000223 if (size >> log2blksz != 0) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700224 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
225 (unsigned long *)buf);
Uma Shankared34f342012-05-25 21:22:49 +0530226 } else {
Simon Glass2a981dc2016-02-29 15:25:52 -0700227 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530228 temp_ptr = sec_buf;
229 memcpy(temp_ptr, buf, size);
Simon Glass2a981dc2016-02-29 15:25:52 -0700230 blk_dwrite(fs->dev_desc, startblock, 1,
231 (unsigned long *)sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530232 }
233 }
234}
235
236static int _get_new_inode_no(unsigned char *buffer)
237{
238 struct ext_filesystem *fs = get_fs();
239 unsigned char input;
240 int operand, status;
241 int count = 1;
242 int j = 0;
243
244 /* get the blocksize of the filesystem */
245 unsigned char *ptr = buffer;
246 while (*ptr == 255) {
247 ptr++;
248 count += 8;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200249 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankared34f342012-05-25 21:22:49 +0530250 return -1;
251 }
252
253 for (j = 0; j < fs->blksz; j++) {
254 input = *ptr;
255 int i = 0;
256 while (i <= 7) {
257 operand = 1 << i;
258 status = input & operand;
259 if (status) {
260 i++;
261 count++;
262 } else {
263 *ptr |= operand;
264 return count;
265 }
266 }
267 ptr = ptr + 1;
268 }
269
270 return -1;
271}
272
273static int _get_new_blk_no(unsigned char *buffer)
274{
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200275 int operand;
Uma Shankared34f342012-05-25 21:22:49 +0530276 int count = 0;
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200277 int i;
Uma Shankared34f342012-05-25 21:22:49 +0530278 unsigned char *ptr = buffer;
279 struct ext_filesystem *fs = get_fs();
280
Uma Shankared34f342012-05-25 21:22:49 +0530281 while (*ptr == 255) {
282 ptr++;
283 count += 8;
284 if (count == (fs->blksz * 8))
285 return -1;
286 }
287
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200288 if (fs->blksz == 1024)
289 count += 1;
290
291 for (i = 0; i <= 7; i++) {
292 operand = 1 << i;
293 if (*ptr & operand) {
294 count++;
295 } else {
296 *ptr |= operand;
297 return count;
Uma Shankared34f342012-05-25 21:22:49 +0530298 }
Uma Shankared34f342012-05-25 21:22:49 +0530299 }
300
301 return -1;
302}
303
304int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
305{
306 int i, remainder, status;
307 unsigned char *ptr = buffer;
308 unsigned char operand;
309 i = blockno / 8;
310 remainder = blockno % 8;
311 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
312
313 i = i - (index * blocksize);
314 if (blocksize != 1024) {
315 ptr = ptr + i;
316 operand = 1 << remainder;
317 status = *ptr & operand;
318 if (status)
319 return -1;
320
321 *ptr = *ptr | operand;
322 return 0;
323 } else {
324 if (remainder == 0) {
325 ptr = ptr + i - 1;
326 operand = (1 << 7);
327 } else {
328 ptr = ptr + i;
329 operand = (1 << (remainder - 1));
330 }
331 status = *ptr & operand;
332 if (status)
333 return -1;
334
335 *ptr = *ptr | operand;
336 return 0;
337 }
338}
339
340void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
341{
342 int i, remainder, status;
343 unsigned char *ptr = buffer;
344 unsigned char operand;
345 i = blockno / 8;
346 remainder = blockno % 8;
347 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
348
349 i = i - (index * blocksize);
350 if (blocksize != 1024) {
351 ptr = ptr + i;
352 operand = (1 << remainder);
353 status = *ptr & operand;
354 if (status)
355 *ptr = *ptr & ~(operand);
356 } else {
357 if (remainder == 0) {
358 ptr = ptr + i - 1;
359 operand = (1 << 7);
360 } else {
361 ptr = ptr + i;
362 operand = (1 << (remainder - 1));
363 }
364 status = *ptr & operand;
365 if (status)
366 *ptr = *ptr & ~(operand);
367 }
368}
369
370int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
371{
372 int i, remainder, status;
373 unsigned char *ptr = buffer;
374 unsigned char operand;
375
Michael Walle58a9ecb2016-09-01 11:21:40 +0200376 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530377 i = inode_no / 8;
378 remainder = inode_no % 8;
379 if (remainder == 0) {
380 ptr = ptr + i - 1;
381 operand = (1 << 7);
382 } else {
383 ptr = ptr + i;
384 operand = (1 << (remainder - 1));
385 }
386 status = *ptr & operand;
387 if (status)
388 return -1;
389
390 *ptr = *ptr | operand;
391
392 return 0;
393}
394
395void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
396{
397 int i, remainder, status;
398 unsigned char *ptr = buffer;
399 unsigned char operand;
400
Michael Walle58a9ecb2016-09-01 11:21:40 +0200401 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530402 i = inode_no / 8;
403 remainder = inode_no % 8;
404 if (remainder == 0) {
405 ptr = ptr + i - 1;
406 operand = (1 << 7);
407 } else {
408 ptr = ptr + i;
409 operand = (1 << (remainder - 1));
410 }
411 status = *ptr & operand;
412 if (status)
413 *ptr = *ptr & ~(operand);
414}
415
Michael Walle58a9ecb2016-09-01 11:21:40 +0200416uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankared34f342012-05-25 21:22:49 +0530417{
418 struct ext2_block_group *desc;
419 struct ext_filesystem *fs = get_fs();
Michael Walle58a9ecb2016-09-01 11:21:40 +0200420 uint16_t crc = 0;
421 __le32 le32_i = cpu_to_le32(i);
Uma Shankared34f342012-05-25 21:22:49 +0530422
Stefan Brüns688d0e72016-09-17 02:10:10 +0200423 desc = ext4fs_get_group_descriptor(fs, i);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200424 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankared34f342012-05-25 21:22:49 +0530425 int offset = offsetof(struct ext2_block_group, bg_checksum);
426
427 crc = ext2fs_crc16(~0, fs->sb->unique_id,
428 sizeof(fs->sb->unique_id));
Michael Walle58a9ecb2016-09-01 11:21:40 +0200429 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
Uma Shankared34f342012-05-25 21:22:49 +0530430 crc = ext2fs_crc16(crc, desc, offset);
431 offset += sizeof(desc->bg_checksum); /* skip checksum */
432 assert(offset == sizeof(*desc));
Tuomas Tynkkynen385b7312017-09-25 22:06:31 +0300433 if (offset < fs->gdsize) {
434 crc = ext2fs_crc16(crc, (__u8 *)desc + offset,
435 fs->gdsize - offset);
436 }
Uma Shankared34f342012-05-25 21:22:49 +0530437 }
438
439 return crc;
440}
441
442static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
443{
444 int dentry_length;
445 int sizeof_void_space;
446 int new_entry_byte_reqd;
447 short padding_factor = 0;
448
449 if (dir->namelen % 4 != 0)
450 padding_factor = 4 - (dir->namelen % 4);
451
452 dentry_length = sizeof(struct ext2_dirent) +
453 dir->namelen + padding_factor;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200454 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankared34f342012-05-25 21:22:49 +0530455 if (sizeof_void_space == 0)
456 return 0;
457
458 padding_factor = 0;
459 if (strlen(filename) % 4 != 0)
460 padding_factor = 4 - (strlen(filename) % 4);
461
462 new_entry_byte_reqd = strlen(filename) +
463 sizeof(struct ext2_dirent) + padding_factor;
464 if (sizeof_void_space >= new_entry_byte_reqd) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200465 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankared34f342012-05-25 21:22:49 +0530466 return sizeof_void_space;
467 }
468
469 return 0;
470}
471
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200472int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankared34f342012-05-25 21:22:49 +0530473{
474 unsigned int *zero_buffer = NULL;
475 char *root_first_block_buffer = NULL;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200476 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530477 long int first_block_no_of_root = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530478 int totalbytes = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530479 unsigned int new_entry_byte_reqd;
Uma Shankared34f342012-05-25 21:22:49 +0530480 int sizeof_void_space = 0;
481 int templength = 0;
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200482 int inodeno = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530483 int status;
484 struct ext_filesystem *fs = get_fs();
485 /* directory entry */
486 struct ext2_dirent *dir;
Uma Shankared34f342012-05-25 21:22:49 +0530487 char *temp_dir = NULL;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200488 uint32_t new_blk_no;
489 uint32_t new_size;
490 uint32_t new_blockcnt;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200491 uint32_t directory_blocks;
Uma Shankared34f342012-05-25 21:22:49 +0530492
493 zero_buffer = zalloc(fs->blksz);
494 if (!zero_buffer) {
495 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200496 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530497 }
498 root_first_block_buffer = zalloc(fs->blksz);
499 if (!root_first_block_buffer) {
500 free(zero_buffer);
501 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200502 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530503 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200504 new_entry_byte_reqd = ROUND(strlen(filename) +
505 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530506restart:
Stefan Brünsa321abd2016-09-06 04:36:44 +0200507 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
508 LOG2_BLOCK_SIZE(ext4fs_root);
509 blk_idx = directory_blocks - 1;
Uma Shankared34f342012-05-25 21:22:49 +0530510
Stefan Brünsa321abd2016-09-06 04:36:44 +0200511restart_read:
Uma Shankared34f342012-05-25 21:22:49 +0530512 /* read the block no allocated to a file */
Stephen Warrend5aee652019-01-30 12:58:05 -0700513 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx,
514 NULL);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200515 if (first_block_no_of_root <= 0)
516 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530517
Frederic Leroy04735e92013-06-26 18:11:25 +0200518 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankared34f342012-05-25 21:22:49 +0530519 * fs->sect_perblk,
520 0, fs->blksz, root_first_block_buffer);
521 if (status == 0)
522 goto fail;
523
524 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
525 goto fail;
526 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankared34f342012-05-25 21:22:49 +0530527 totalbytes = 0;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200528
Michael Walle58a9ecb2016-09-01 11:21:40 +0200529 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsa321abd2016-09-06 04:36:44 +0200530 unsigned short used_len = ROUND(dir->namelen +
531 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530532
Stefan Brünsa321abd2016-09-06 04:36:44 +0200533 /* last entry of block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200534 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankared34f342012-05-25 21:22:49 +0530535
Stefan Brünsa321abd2016-09-06 04:36:44 +0200536 /* check if new entry fits */
537 if ((used_len + new_entry_byte_reqd) <=
538 le16_to_cpu(dir->direntlen)) {
539 dir->direntlen = cpu_to_le16(used_len);
540 break;
541 } else {
542 if (blk_idx > 0) {
543 printf("Block full, trying previous\n");
544 blk_idx--;
545 goto restart_read;
546 }
547 printf("All blocks full: Allocate new\n");
Uma Shankared34f342012-05-25 21:22:49 +0530548
Stefan Brünsb96c3c72016-09-06 04:36:43 +0200549 if (le32_to_cpu(g_parent_inode->flags) &
550 EXT4_EXTENTS_FL) {
551 printf("Directory uses extents\n");
552 goto fail;
553 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200554 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankared34f342012-05-25 21:22:49 +0530555 printf("Directory exceeds limit\n");
556 goto fail;
557 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200558 new_blk_no = ext4fs_get_new_blk_no();
559 if (new_blk_no == -1) {
Uma Shankared34f342012-05-25 21:22:49 +0530560 printf("no block left to assign\n");
561 goto fail;
562 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200563 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200564 g_parent_inode->b.blocks.
565 dir_blocks[directory_blocks] =
Michael Walle58a9ecb2016-09-01 11:21:40 +0200566 cpu_to_le32(new_blk_no);
567
568 new_size = le32_to_cpu(g_parent_inode->size);
569 new_size += fs->blksz;
570 g_parent_inode->size = cpu_to_le32(new_size);
571
572 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
Marek Szyprowski1c9f8f62019-06-21 15:32:51 +0200573 new_blockcnt += fs->blksz >> LOG2_SECTOR_SIZE;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200574 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
575
Uma Shankared34f342012-05-25 21:22:49 +0530576 if (ext4fs_put_metadata
577 (root_first_block_buffer,
578 first_block_no_of_root))
579 goto fail;
580 goto restart;
581 }
Uma Shankared34f342012-05-25 21:22:49 +0530582 }
583
Michael Walle58a9ecb2016-09-01 11:21:40 +0200584 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530585 totalbytes = totalbytes + templength;
586 sizeof_void_space = check_void_in_dentry(dir, filename);
587 if (sizeof_void_space)
588 break;
589
590 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530591 }
592
593 /* make a pointer ready for creating next directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200594 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530595 totalbytes = totalbytes + templength;
596 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530597
598 /* get the next available inode number */
599 inodeno = ext4fs_get_new_inode_no();
600 if (inodeno == -1) {
601 printf("no inode left to assign\n");
602 goto fail;
603 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200604 dir->inode = cpu_to_le32(inodeno);
Uma Shankared34f342012-05-25 21:22:49 +0530605 if (sizeof_void_space)
Michael Walle58a9ecb2016-09-01 11:21:40 +0200606 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankared34f342012-05-25 21:22:49 +0530607 else
Michael Walle58a9ecb2016-09-01 11:21:40 +0200608 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankared34f342012-05-25 21:22:49 +0530609
610 dir->namelen = strlen(filename);
Jean-Jacques Hiblot5efc0682019-02-13 12:15:25 +0100611 dir->filetype = file_type;
Uma Shankared34f342012-05-25 21:22:49 +0530612 temp_dir = (char *)dir;
613 temp_dir = temp_dir + sizeof(struct ext2_dirent);
614 memcpy(temp_dir, filename, strlen(filename));
615
Uma Shankared34f342012-05-25 21:22:49 +0530616 /* update or write the 1st block of root inode */
617 if (ext4fs_put_metadata(root_first_block_buffer,
618 first_block_no_of_root))
619 goto fail;
620
621fail:
622 free(zero_buffer);
623 free(root_first_block_buffer);
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200624
625 return inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530626}
627
628static int search_dir(struct ext2_inode *parent_inode, char *dirname)
629{
630 int status;
Stefan Brüns76a29512016-09-06 04:36:41 +0200631 int inodeno = 0;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200632 int offset;
633 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530634 long int blknr;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200635 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530636 struct ext2_dirent *dir = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530637 struct ext_filesystem *fs = get_fs();
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200638 uint32_t directory_blocks;
639 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530640
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200641 directory_blocks = le32_to_cpu(parent_inode->size) >>
642 LOG2_BLOCK_SIZE(ext4fs_root);
643
644 block_buffer = zalloc(fs->blksz);
645 if (!block_buffer)
646 goto fail;
647
648 /* get the block no allocated to a file */
649 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
Stephen Warrend5aee652019-01-30 12:58:05 -0700650 blknr = read_allocated_block(parent_inode, blk_idx, NULL);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200651 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530652 goto fail;
653
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200654 /* read the directory block */
Frederic Leroy04735e92013-06-26 18:11:25 +0200655 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +0530656 0, fs->blksz, (char *)block_buffer);
657 if (status == 0)
658 goto fail;
659
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200660 offset = 0;
661 do {
Ian Rayecdfb412017-11-08 15:35:10 +0000662 if (offset & 3) {
663 printf("Badly aligned ext2_dirent\n");
664 break;
665 }
666
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200667 dir = (struct ext2_dirent *)(block_buffer + offset);
668 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankared34f342012-05-25 21:22:49 +0530669
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200670 int direntlen = le16_to_cpu(dir->direntlen);
671 if (direntlen < sizeof(struct ext2_dirent))
Uma Shankared34f342012-05-25 21:22:49 +0530672 break;
673
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200674 if (dir->inode && (strlen(dirname) == dir->namelen) &&
675 (strncmp(dirname, direntname, dir->namelen) == 0)) {
676 inodeno = le32_to_cpu(dir->inode);
677 break;
678 }
Uma Shankared34f342012-05-25 21:22:49 +0530679
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200680 offset += direntlen;
Stefan Brüns76a29512016-09-06 04:36:41 +0200681
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200682 } while (offset < fs->blksz);
683
684 if (inodeno > 0) {
685 free(block_buffer);
Stefan Brüns76a29512016-09-06 04:36:41 +0200686 return inodeno;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200687 }
Uma Shankared34f342012-05-25 21:22:49 +0530688 }
689
690fail:
691 free(block_buffer);
692
693 return -1;
694}
695
696static int find_dir_depth(char *dirname)
697{
698 char *token = strtok(dirname, "/");
699 int count = 0;
700 while (token != NULL) {
701 token = strtok(NULL, "/");
702 count++;
703 }
704 return count + 1 + 1;
705 /*
706 * for example for string /home/temp
707 * depth=home(1)+temp(1)+1 extra for NULL;
708 * so count is 4;
709 */
710}
711
712static int parse_path(char **arr, char *dirname)
713{
714 char *token = strtok(dirname, "/");
715 int i = 0;
716
717 /* add root */
718 arr[i] = zalloc(strlen("/") + 1);
719 if (!arr[i])
720 return -ENOMEM;
Stephen Warren934b14f2015-09-04 22:03:44 -0600721 memcpy(arr[i++], "/", strlen("/"));
Uma Shankared34f342012-05-25 21:22:49 +0530722
723 /* add each path entry after root */
724 while (token != NULL) {
725 arr[i] = zalloc(strlen(token) + 1);
726 if (!arr[i])
727 return -ENOMEM;
728 memcpy(arr[i++], token, strlen(token));
729 token = strtok(NULL, "/");
730 }
731 arr[i] = NULL;
732
733 return 0;
734}
735
736int ext4fs_iget(int inode_no, struct ext2_inode *inode)
737{
738 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
739 return -1;
740
741 return 0;
742}
743
744/*
745 * Function: ext4fs_get_parent_inode_num
746 * Return Value: inode Number of the parent directory of file/Directory to be
747 * created
748 * dirname : Input parmater, input path name of the file/directory to be created
749 * dname : Output parameter, to be filled with the name of the directory
750 * extracted from dirname
751 */
752int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
753{
754 int i;
755 int depth = 0;
756 int matched_inode_no;
757 int result_inode_no = -1;
758 char **ptr = NULL;
759 char *depth_dirname = NULL;
760 char *parse_dirname = NULL;
761 struct ext2_inode *parent_inode = NULL;
762 struct ext2_inode *first_inode = NULL;
763 struct ext2_inode temp_inode;
764
765 if (*dirname != '/') {
766 printf("Please supply Absolute path\n");
767 return -1;
768 }
769
770 /* TODO: input validation make equivalent to linux */
771 depth_dirname = zalloc(strlen(dirname) + 1);
772 if (!depth_dirname)
773 return -ENOMEM;
774
775 memcpy(depth_dirname, dirname, strlen(dirname));
776 depth = find_dir_depth(depth_dirname);
777 parse_dirname = zalloc(strlen(dirname) + 1);
778 if (!parse_dirname)
779 goto fail;
780 memcpy(parse_dirname, dirname, strlen(dirname));
781
782 /* allocate memory for each directory level */
783 ptr = zalloc((depth) * sizeof(char *));
784 if (!ptr)
785 goto fail;
786 if (parse_path(ptr, parse_dirname))
787 goto fail;
788 parent_inode = zalloc(sizeof(struct ext2_inode));
789 if (!parent_inode)
790 goto fail;
791 first_inode = zalloc(sizeof(struct ext2_inode));
792 if (!first_inode)
793 goto fail;
794 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
795 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
796 if (flags & F_FILE)
797 result_inode_no = EXT2_ROOT_INO;
798 for (i = 1; i < depth; i++) {
799 matched_inode_no = search_dir(parent_inode, ptr[i]);
800 if (matched_inode_no == -1) {
801 if (ptr[i + 1] == NULL && i == 1) {
802 result_inode_no = EXT2_ROOT_INO;
803 goto end;
804 } else {
805 if (ptr[i + 1] == NULL)
806 break;
807 printf("Invalid path\n");
808 result_inode_no = -1;
809 goto fail;
810 }
811 } else {
812 if (ptr[i + 1] != NULL) {
813 memset(parent_inode, '\0',
814 sizeof(struct ext2_inode));
815 if (ext4fs_iget(matched_inode_no,
816 parent_inode)) {
817 result_inode_no = -1;
818 goto fail;
819 }
820 result_inode_no = matched_inode_no;
821 } else {
822 break;
823 }
824 }
825 }
826
827end:
828 if (i == 1)
829 matched_inode_no = search_dir(first_inode, ptr[i]);
830 else
831 matched_inode_no = search_dir(parent_inode, ptr[i]);
832
833 if (matched_inode_no != -1) {
834 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200835 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankared34f342012-05-25 21:22:49 +0530836 printf("It is a Directory\n");
837 result_inode_no = -1;
838 goto fail;
839 }
840 }
841
842 if (strlen(ptr[i]) > 256) {
843 result_inode_no = -1;
844 goto fail;
845 }
846 memcpy(dname, ptr[i], strlen(ptr[i]));
847
848fail:
849 free(depth_dirname);
850 free(parse_dirname);
Stephen Warren934b14f2015-09-04 22:03:44 -0600851 for (i = 0; i < depth; i++) {
852 if (!ptr[i])
853 break;
854 free(ptr[i]);
855 }
Uma Shankared34f342012-05-25 21:22:49 +0530856 free(ptr);
857 free(parent_inode);
858 free(first_inode);
859
860 return result_inode_no;
861}
862
Stefan Brüns76a29512016-09-06 04:36:41 +0200863static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankared34f342012-05-25 21:22:49 +0530864{
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200865 int status;
866 int inodeno = 0;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200867 int offset;
868 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530869 struct ext2_dirent *dir = NULL;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200870 struct ext2_dirent *previous_dir;
Uma Shankared34f342012-05-25 21:22:49 +0530871 struct ext_filesystem *fs = get_fs();
Stephen Warrend56b2012015-09-04 22:03:45 -0600872 int ret = -1;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200873 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530874
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200875 block_buffer = zalloc(fs->blksz);
876 if (!block_buffer)
Uma Shankared34f342012-05-25 21:22:49 +0530877 return -ENOMEM;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200878
879 /* read the directory block */
Stefan Brüns76a29512016-09-06 04:36:41 +0200880 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200881 fs->blksz, block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530882 if (status == 0)
883 goto fail;
884
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200885 offset = 0;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200886 do {
Ian Rayecdfb412017-11-08 15:35:10 +0000887 if (offset & 3) {
888 printf("Badly aligned ext2_dirent\n");
889 break;
890 }
891
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200892 previous_dir = dir;
893 dir = (struct ext2_dirent *)(block_buffer + offset);
894 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
895
896 int direntlen = le16_to_cpu(dir->direntlen);
897 if (direntlen < sizeof(struct ext2_dirent))
898 break;
899
Stefan Brüns76a29512016-09-06 04:36:41 +0200900 if (dir->inode && (strlen(filename) == dir->namelen) &&
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200901 (strncmp(direntname, filename, dir->namelen) == 0)) {
Stefan Brüns76a29512016-09-06 04:36:41 +0200902 inodeno = le32_to_cpu(dir->inode);
Stefan Brüns76a29512016-09-06 04:36:41 +0200903 break;
Uma Shankared34f342012-05-25 21:22:49 +0530904 }
905
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200906 offset += direntlen;
Uma Shankared34f342012-05-25 21:22:49 +0530907
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200908 } while (offset < fs->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530909
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200910 if (inodeno > 0) {
Stefan Brüns805e3e002016-10-09 20:15:28 +0200911 printf("file found, deleting\n");
912 if (ext4fs_log_journal(block_buffer, blknr))
913 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530914
Stefan Brüns805e3e002016-10-09 20:15:28 +0200915 if (previous_dir) {
916 /* merge dir entry with predecessor */
917 uint16_t new_len;
918 new_len = le16_to_cpu(previous_dir->direntlen);
919 new_len += le16_to_cpu(dir->direntlen);
920 previous_dir->direntlen = cpu_to_le16(new_len);
921 } else {
922 /* invalidate dir entry */
923 dir->inode = 0;
924 }
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200925 if (ext4fs_put_metadata(block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530926 goto fail;
Stephen Warrend56b2012015-09-04 22:03:45 -0600927 ret = inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530928 }
929fail:
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200930 free(block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530931
Stephen Warrend56b2012015-09-04 22:03:45 -0600932 return ret;
Uma Shankared34f342012-05-25 21:22:49 +0530933}
934
Stefan Brüns76a29512016-09-06 04:36:41 +0200935int ext4fs_filename_unlink(char *filename)
Uma Shankared34f342012-05-25 21:22:49 +0530936{
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200937 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530938 long int blknr = -1;
939 int inodeno = -1;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200940 uint32_t directory_blocks;
941
942 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
943 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankared34f342012-05-25 21:22:49 +0530944
945 /* read the block no allocated to a file */
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200946 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
Stephen Warrend5aee652019-01-30 12:58:05 -0700947 blknr = read_allocated_block(g_parent_inode, blk_idx, NULL);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200948 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530949 break;
Stefan Brüns76a29512016-09-06 04:36:41 +0200950 inodeno = unlink_filename(filename, blknr);
Uma Shankared34f342012-05-25 21:22:49 +0530951 if (inodeno != -1)
952 return inodeno;
953 }
954
955 return -1;
956}
957
Michael Walle58a9ecb2016-09-01 11:21:40 +0200958uint32_t ext4fs_get_new_blk_no(void)
Uma Shankared34f342012-05-25 21:22:49 +0530959{
960 short i;
961 short status;
962 int remainder;
963 unsigned int bg_idx;
964 static int prev_bg_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200965 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankared34f342012-05-25 21:22:49 +0530966 struct ext_filesystem *fs = get_fs();
967 char *journal_buffer = zalloc(fs->blksz);
968 char *zero_buffer = zalloc(fs->blksz);
969 if (!journal_buffer || !zero_buffer)
970 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530971
972 if (fs->first_pass_bbmap == 0) {
973 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200974 struct ext2_block_group *bgd = NULL;
975 bgd = ext4fs_get_group_descriptor(fs, i);
976 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
977 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
978 uint64_t b_bitmap_blk =
979 ext4fs_bg_get_block_id(bgd, fs);
980 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +0530981 memcpy(fs->blk_bmaps[i], zero_buffer,
982 fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200983 put_ext4(b_bitmap_blk * fs->blksz,
984 fs->blk_bmaps[i], fs->blksz);
985 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
986 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +0530987 }
988 fs->curr_blkno =
989 _get_new_blk_no(fs->blk_bmaps[i]);
990 if (fs->curr_blkno == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +0200991 /* block bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +0530992 continue;
993 fs->curr_blkno = fs->curr_blkno +
994 (i * fs->blksz * 8);
995 fs->first_pass_bbmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +0200996 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200997 ext4fs_sb_free_blocks_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200998 status = ext4fs_devread(b_bitmap_blk *
999 fs->sect_perblk,
1000 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301001 journal_buffer);
1002 if (status == 0)
1003 goto fail;
1004 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001005 b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301006 goto fail;
1007 goto success;
1008 } else {
1009 debug("no space left on block group %d\n", i);
1010 }
1011 }
1012
1013 goto fail;
1014 } else {
Uma Shankared34f342012-05-25 21:22:49 +05301015 fs->curr_blkno++;
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001016restart:
Uma Shankared34f342012-05-25 21:22:49 +05301017 /* get the blockbitmap index respective to blockno */
Łukasz Majewski35dd0552014-05-06 09:36:04 +02001018 bg_idx = fs->curr_blkno / blk_per_grp;
1019 if (fs->blksz == 1024) {
Uma Shankared34f342012-05-25 21:22:49 +05301020 remainder = fs->curr_blkno % blk_per_grp;
1021 if (!remainder)
1022 bg_idx--;
1023 }
1024
1025 /*
1026 * To skip completely filled block group bitmaps
1027 * Optimize the block allocation
1028 */
1029 if (bg_idx >= fs->no_blkgrp)
1030 goto fail;
1031
Stefan Brüns688d0e72016-09-17 02:10:10 +02001032 struct ext2_block_group *bgd = NULL;
1033 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1034 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
Uma Shankared34f342012-05-25 21:22:49 +05301035 debug("block group %u is full. Skipping\n", bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001036 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1037 if (fs->blksz == 1024)
1038 fs->curr_blkno += 1;
Uma Shankared34f342012-05-25 21:22:49 +05301039 goto restart;
1040 }
1041
Stefan Brüns688d0e72016-09-17 02:10:10 +02001042 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1043 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1044 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +05301045 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001046 put_ext4(b_bitmap_blk * fs->blksz,
1047 zero_buffer, fs->blksz);
1048 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1049 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301050 }
1051
1052 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1053 bg_idx) != 0) {
1054 debug("going for restart for the block no %ld %u\n",
1055 fs->curr_blkno, bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001056 fs->curr_blkno++;
Uma Shankared34f342012-05-25 21:22:49 +05301057 goto restart;
1058 }
1059
1060 /* journal backup */
1061 if (prev_bg_bitmap_index != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001062 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301063 0, fs->blksz, journal_buffer);
1064 if (status == 0)
1065 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001066 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301067 goto fail;
1068
1069 prev_bg_bitmap_index = bg_idx;
1070 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001071 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001072 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301073 goto success;
1074 }
1075success:
1076 free(journal_buffer);
1077 free(zero_buffer);
1078
1079 return fs->curr_blkno;
1080fail:
1081 free(journal_buffer);
1082 free(zero_buffer);
1083
1084 return -1;
1085}
1086
1087int ext4fs_get_new_inode_no(void)
1088{
1089 short i;
1090 short status;
1091 unsigned int ibmap_idx;
1092 static int prev_inode_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001093 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankared34f342012-05-25 21:22:49 +05301094 struct ext_filesystem *fs = get_fs();
1095 char *journal_buffer = zalloc(fs->blksz);
1096 char *zero_buffer = zalloc(fs->blksz);
1097 if (!journal_buffer || !zero_buffer)
1098 goto fail;
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001099 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1100 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
Uma Shankared34f342012-05-25 21:22:49 +05301101
1102 if (fs->first_pass_ibmap == 0) {
1103 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001104 uint32_t free_inodes;
1105 struct ext2_block_group *bgd = NULL;
1106 bgd = ext4fs_get_group_descriptor(fs, i);
1107 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1108 if (free_inodes) {
1109 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1110 uint64_t i_bitmap_blk =
1111 ext4fs_bg_get_inode_id(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001112 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001113 bgd->bg_itable_unused = free_inodes;
1114 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1115 put_ext4(i_bitmap_blk * fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301116 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001117 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1118 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301119 memcpy(fs->inode_bmaps[i],
1120 zero_buffer, fs->blksz);
1121 }
1122 fs->curr_inode_no =
1123 _get_new_inode_no(fs->inode_bmaps[i]);
1124 if (fs->curr_inode_no == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001125 /* inode bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +05301126 continue;
1127 fs->curr_inode_no = fs->curr_inode_no +
1128 (i * inodes_per_grp);
1129 fs->first_pass_ibmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +02001130 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001131 if (has_gdt_chksum)
Stefan Brüns749e93e2016-09-20 01:13:01 +02001132 ext4fs_bg_itable_unused_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001133 ext4fs_sb_free_inodes_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001134 status = ext4fs_devread(i_bitmap_blk *
1135 fs->sect_perblk,
1136 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301137 journal_buffer);
1138 if (status == 0)
1139 goto fail;
1140 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001141 i_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301142 goto fail;
1143 goto success;
1144 } else
1145 debug("no inode left on block group %d\n", i);
1146 }
1147 goto fail;
1148 } else {
1149restart:
1150 fs->curr_inode_no++;
1151 /* get the blockbitmap index respective to blockno */
1152 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001153 struct ext2_block_group *bgd =
1154 ext4fs_get_group_descriptor(fs, ibmap_idx);
1155 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1156 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1157
1158 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1159 put_ext4(i_bitmap_blk * fs->blksz,
Michael Walle58a9ecb2016-09-01 11:21:40 +02001160 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001161 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1162 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301163 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1164 fs->blksz);
1165 }
1166
1167 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1168 fs->inode_bmaps[ibmap_idx],
1169 ibmap_idx) != 0) {
1170 debug("going for restart for the block no %d %u\n",
1171 fs->curr_inode_no, ibmap_idx);
1172 goto restart;
1173 }
1174
1175 /* journal backup */
1176 if (prev_inode_bitmap_index != ibmap_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001177 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301178 0, fs->blksz, journal_buffer);
1179 if (status == 0)
1180 goto fail;
1181 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001182 le32_to_cpu(bgd->inode_id)))
Uma Shankared34f342012-05-25 21:22:49 +05301183 goto fail;
1184 prev_inode_bitmap_index = ibmap_idx;
1185 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001186 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001187 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001188 bgd->bg_itable_unused = bgd->free_inodes;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001189 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301190 goto success;
1191 }
1192
1193success:
1194 free(journal_buffer);
1195 free(zero_buffer);
1196
1197 return fs->curr_inode_no;
1198fail:
1199 free(journal_buffer);
1200 free(zero_buffer);
1201
1202 return -1;
1203
1204}
1205
1206
1207static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1208 unsigned int *total_remaining_blocks,
1209 unsigned int *no_blks_reqd)
1210{
1211 short i;
1212 short status;
1213 long int actual_block_no;
1214 long int si_blockno;
1215 /* si :single indirect */
Michael Walle58a9ecb2016-09-01 11:21:40 +02001216 __le32 *si_buffer = NULL;
1217 __le32 *si_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301218 struct ext_filesystem *fs = get_fs();
1219
1220 if (*total_remaining_blocks != 0) {
1221 si_buffer = zalloc(fs->blksz);
1222 if (!si_buffer) {
1223 printf("No Memory\n");
1224 return;
1225 }
1226 si_start_addr = si_buffer;
1227 si_blockno = ext4fs_get_new_blk_no();
1228 if (si_blockno == -1) {
1229 printf("no block left to assign\n");
1230 goto fail;
1231 }
1232 (*no_blks_reqd)++;
1233 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1234
Frederic Leroy04735e92013-06-26 18:11:25 +02001235 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301236 0, fs->blksz, (char *)si_buffer);
1237 memset(si_buffer, '\0', fs->blksz);
1238 if (status == 0)
1239 goto fail;
1240
1241 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1242 actual_block_no = ext4fs_get_new_blk_no();
1243 if (actual_block_no == -1) {
1244 printf("no block left to assign\n");
1245 goto fail;
1246 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001247 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301248 debug("SIAB %u: %u\n", *si_buffer,
1249 *total_remaining_blocks);
1250
1251 si_buffer++;
1252 (*total_remaining_blocks)--;
1253 if (*total_remaining_blocks == 0)
1254 break;
1255 }
1256
1257 /* write the block to disk */
Ma Haijun05508702014-01-08 08:15:33 +08001258 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301259 si_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001260 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301261 }
1262fail:
1263 free(si_start_addr);
1264}
1265
1266static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1267 unsigned int *total_remaining_blocks,
1268 unsigned int *no_blks_reqd)
1269{
1270 short i;
1271 short j;
1272 short status;
1273 long int actual_block_no;
1274 /* di:double indirect */
1275 long int di_blockno_parent;
1276 long int di_blockno_child;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001277 __le32 *di_parent_buffer = NULL;
1278 __le32 *di_child_buff = NULL;
1279 __le32 *di_block_start_addr = NULL;
1280 __le32 *di_child_buff_start = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301281 struct ext_filesystem *fs = get_fs();
1282
1283 if (*total_remaining_blocks != 0) {
1284 /* double indirect parent block connecting to inode */
1285 di_blockno_parent = ext4fs_get_new_blk_no();
1286 if (di_blockno_parent == -1) {
1287 printf("no block left to assign\n");
1288 goto fail;
1289 }
1290 di_parent_buffer = zalloc(fs->blksz);
1291 if (!di_parent_buffer)
1292 goto fail;
1293
1294 di_block_start_addr = di_parent_buffer;
1295 (*no_blks_reqd)++;
1296 debug("DIPB %ld: %u\n", di_blockno_parent,
1297 *total_remaining_blocks);
1298
Frederic Leroy04735e92013-06-26 18:11:25 +02001299 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankared34f342012-05-25 21:22:49 +05301300 fs->sect_perblk, 0,
1301 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001302
1303 if (!status) {
1304 printf("%s: Device read error!\n", __func__);
1305 goto fail;
1306 }
Uma Shankared34f342012-05-25 21:22:49 +05301307 memset(di_parent_buffer, '\0', fs->blksz);
1308
1309 /*
1310 * start:for each double indirect parent
1311 * block create one more block
1312 */
1313 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1314 di_blockno_child = ext4fs_get_new_blk_no();
1315 if (di_blockno_child == -1) {
1316 printf("no block left to assign\n");
1317 goto fail;
1318 }
1319 di_child_buff = zalloc(fs->blksz);
1320 if (!di_child_buff)
1321 goto fail;
1322
1323 di_child_buff_start = di_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001324 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankared34f342012-05-25 21:22:49 +05301325 di_parent_buffer++;
1326 (*no_blks_reqd)++;
1327 debug("DICB %ld: %u\n", di_blockno_child,
1328 *total_remaining_blocks);
1329
Frederic Leroy04735e92013-06-26 18:11:25 +02001330 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankared34f342012-05-25 21:22:49 +05301331 fs->sect_perblk, 0,
1332 fs->blksz,
1333 (char *)di_child_buff);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001334
1335 if (!status) {
1336 printf("%s: Device read error!\n", __func__);
1337 goto fail;
1338 }
Uma Shankared34f342012-05-25 21:22:49 +05301339 memset(di_child_buff, '\0', fs->blksz);
1340 /* filling of actual datablocks for each child */
1341 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1342 actual_block_no = ext4fs_get_new_blk_no();
1343 if (actual_block_no == -1) {
1344 printf("no block left to assign\n");
1345 goto fail;
1346 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001347 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301348 debug("DIAB %ld: %u\n", actual_block_no,
1349 *total_remaining_blocks);
1350
1351 di_child_buff++;
1352 (*total_remaining_blocks)--;
1353 if (*total_remaining_blocks == 0)
1354 break;
1355 }
1356 /* write the block table */
Ma Haijun05508702014-01-08 08:15:33 +08001357 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301358 di_child_buff_start, fs->blksz);
1359 free(di_child_buff_start);
1360 di_child_buff_start = NULL;
1361
1362 if (*total_remaining_blocks == 0)
1363 break;
1364 }
Ma Haijun05508702014-01-08 08:15:33 +08001365 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301366 di_block_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001367 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankared34f342012-05-25 21:22:49 +05301368 }
1369fail:
1370 free(di_block_start_addr);
1371}
1372
1373static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1374 unsigned int *total_remaining_blocks,
1375 unsigned int *no_blks_reqd)
1376{
1377 short i;
1378 short j;
1379 short k;
1380 long int actual_block_no;
1381 /* ti: Triple Indirect */
1382 long int ti_gp_blockno;
1383 long int ti_parent_blockno;
1384 long int ti_child_blockno;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001385 __le32 *ti_gp_buff = NULL;
1386 __le32 *ti_parent_buff = NULL;
1387 __le32 *ti_child_buff = NULL;
1388 __le32 *ti_gp_buff_start_addr = NULL;
1389 __le32 *ti_pbuff_start_addr = NULL;
1390 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301391 struct ext_filesystem *fs = get_fs();
1392 if (*total_remaining_blocks != 0) {
1393 /* triple indirect grand parent block connecting to inode */
1394 ti_gp_blockno = ext4fs_get_new_blk_no();
1395 if (ti_gp_blockno == -1) {
1396 printf("no block left to assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001397 return;
Uma Shankared34f342012-05-25 21:22:49 +05301398 }
1399 ti_gp_buff = zalloc(fs->blksz);
1400 if (!ti_gp_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001401 return;
Uma Shankared34f342012-05-25 21:22:49 +05301402
1403 ti_gp_buff_start_addr = ti_gp_buff;
1404 (*no_blks_reqd)++;
1405 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1406 *total_remaining_blocks);
1407
1408 /* for each 4 byte grand parent entry create one more block */
1409 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1410 ti_parent_blockno = ext4fs_get_new_blk_no();
1411 if (ti_parent_blockno == -1) {
1412 printf("no block left to assign\n");
1413 goto fail;
1414 }
1415 ti_parent_buff = zalloc(fs->blksz);
1416 if (!ti_parent_buff)
1417 goto fail;
1418
1419 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001420 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301421 ti_gp_buff++;
1422 (*no_blks_reqd)++;
1423 debug("TIPB %ld: %u\n", ti_parent_blockno,
1424 *total_remaining_blocks);
1425
1426 /* for each 4 byte entry parent create one more block */
1427 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1428 ti_child_blockno = ext4fs_get_new_blk_no();
1429 if (ti_child_blockno == -1) {
1430 printf("no block left assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001431 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301432 }
1433 ti_child_buff = zalloc(fs->blksz);
1434 if (!ti_child_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001435 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301436
1437 ti_cbuff_start_addr = ti_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001438 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301439 ti_parent_buff++;
1440 (*no_blks_reqd)++;
1441 debug("TICB %ld: %u\n", ti_parent_blockno,
1442 *total_remaining_blocks);
1443
1444 /* fill actual datablocks for each child */
1445 for (k = 0; k < (fs->blksz / sizeof(int));
1446 k++) {
1447 actual_block_no =
1448 ext4fs_get_new_blk_no();
1449 if (actual_block_no == -1) {
1450 printf("no block left\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001451 free(ti_cbuff_start_addr);
1452 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301453 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001454 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301455 debug("TIAB %ld: %u\n", actual_block_no,
1456 *total_remaining_blocks);
1457
1458 ti_child_buff++;
1459 (*total_remaining_blocks)--;
1460 if (*total_remaining_blocks == 0)
1461 break;
1462 }
1463 /* write the child block */
Ma Haijun05508702014-01-08 08:15:33 +08001464 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1465 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301466 ti_cbuff_start_addr, fs->blksz);
1467 free(ti_cbuff_start_addr);
1468
1469 if (*total_remaining_blocks == 0)
1470 break;
1471 }
1472 /* write the parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001473 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301474 ti_pbuff_start_addr, fs->blksz);
1475 free(ti_pbuff_start_addr);
1476
1477 if (*total_remaining_blocks == 0)
1478 break;
1479 }
1480 /* write the grand parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001481 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301482 ti_gp_buff_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001483 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini495c3a12015-12-10 16:42:21 -05001484 free(ti_gp_buff_start_addr);
1485 return;
Uma Shankared34f342012-05-25 21:22:49 +05301486 }
Tom Rini495c3a12015-12-10 16:42:21 -05001487fail1:
1488 free(ti_pbuff_start_addr);
Uma Shankared34f342012-05-25 21:22:49 +05301489fail:
1490 free(ti_gp_buff_start_addr);
1491}
1492
1493void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1494 unsigned int total_remaining_blocks,
1495 unsigned int *total_no_of_block)
1496{
1497 short i;
1498 long int direct_blockno;
1499 unsigned int no_blks_reqd = 0;
1500
1501 /* allocation of direct blocks */
Stephen Warrend0180282014-06-11 12:46:16 -06001502 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankared34f342012-05-25 21:22:49 +05301503 direct_blockno = ext4fs_get_new_blk_no();
1504 if (direct_blockno == -1) {
1505 printf("no block left to assign\n");
1506 return;
1507 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001508 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301509 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1510
1511 total_remaining_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +05301512 }
1513
1514 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1515 &no_blks_reqd);
1516 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1517 &no_blks_reqd);
1518 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1519 &no_blks_reqd);
1520 *total_no_of_block += no_blks_reqd;
1521}
1522
1523#endif
1524
Uma Shankara1596432012-05-25 21:21:44 +05301525static struct ext4_extent_header *ext4fs_get_extent_block
Stephen Warrend5aee652019-01-30 12:58:05 -07001526 (struct ext2_data *data, struct ext_block_cache *cache,
Uma Shankara1596432012-05-25 21:21:44 +05301527 struct ext4_extent_header *ext_block,
1528 uint32_t fileblock, int log2_blksz)
1529{
1530 struct ext4_extent_idx *index;
1531 unsigned long long block;
Ionut Nicu47017322014-01-13 11:59:24 +01001532 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankara1596432012-05-25 21:21:44 +05301533 int i;
1534
1535 while (1) {
1536 index = (struct ext4_extent_idx *)(ext_block + 1);
1537
Rommel Custodio8b415f72013-07-21 10:53:25 +02001538 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001539 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301540
1541 if (ext_block->eh_depth == 0)
1542 return ext_block;
1543 i = -1;
1544 do {
1545 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001546 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301547 break;
Ionut Nicub5bbac12014-01-13 12:00:08 +01001548 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankara1596432012-05-25 21:21:44 +05301549
Gero Schumacher1c48fda2019-02-26 15:45:22 +00001550 /*
1551 * If first logical block number is higher than requested fileblock,
1552 * it is a sparse file. This is handled on upper layer.
1553 */
1554 if (i > 0)
1555 i--;
Uma Shankara1596432012-05-25 21:21:44 +05301556
Rommel Custodio8b415f72013-07-21 10:53:25 +02001557 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301558 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
Stephen Warrend5aee652019-01-30 12:58:05 -07001559 block <<= log2_blksz;
1560 if (!ext_cache_read(cache, (lbaint_t)block, blksz))
Michael Walle58a9ecb2016-09-01 11:21:40 +02001561 return NULL;
Stephen Warrend5aee652019-01-30 12:58:05 -07001562 ext_block = (struct ext4_extent_header *)cache->buf;
Uma Shankara1596432012-05-25 21:21:44 +05301563 }
1564}
1565
1566static int ext4fs_blockgroup
1567 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1568{
1569 long int blkno;
1570 unsigned int blkoff, desc_per_blk;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001571 int log2blksz = get_fs()->dev_desc->log2blksz;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001572 int desc_size = get_fs()->gdsize;
Uma Shankara1596432012-05-25 21:21:44 +05301573
Paul Emge084be432019-07-08 16:37:06 -07001574 if (desc_size == 0)
1575 return 0;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001576 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301577
Paul Emge084be432019-07-08 16:37:06 -07001578 if (desc_per_blk == 0)
1579 return 0;
Michael Walle7f101be2016-08-29 10:46:44 +02001580 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankara1596432012-05-25 21:21:44 +05301581 group / desc_per_blk;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001582 blkoff = (group % desc_per_blk) * desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301583
1584 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1585 group, blkno, blkoff);
1586
Frederic Leroy04735e92013-06-26 18:11:25 +02001587 return ext4fs_devread((lbaint_t)blkno <<
1588 (LOG2_BLOCK_SIZE(data) - log2blksz),
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001589 blkoff, desc_size, (char *)blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301590}
1591
1592int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1593{
Benjamin Limfebbc582019-03-29 07:29:45 -04001594 struct ext2_block_group *blkgrp;
Uma Shankara1596432012-05-25 21:21:44 +05301595 struct ext2_sblock *sblock = &data->sblock;
1596 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00001597 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301598 int inodes_per_block, status;
1599 long int blkno;
1600 unsigned int blkoff;
1601
Benjamin Limfebbc582019-03-29 07:29:45 -04001602 /* Allocate blkgrp based on gdsize (for 64-bit support). */
1603 blkgrp = zalloc(get_fs()->gdsize);
1604 if (!blkgrp)
1605 return 0;
1606
Uma Shankara1596432012-05-25 21:21:44 +05301607 /* It is easier to calculate if the first inode is 0. */
1608 ino--;
Paul Emge084be432019-07-08 16:37:06 -07001609 if ( le32_to_cpu(sblock->inodes_per_group) == 0 || fs->inodesz == 0) {
1610 free(blkgrp);
1611 return 0;
1612 }
Michael Walle7f101be2016-08-29 10:46:44 +02001613 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Benjamin Limfebbc582019-03-29 07:29:45 -04001614 (sblock->inodes_per_group), blkgrp);
1615 if (status == 0) {
1616 free(blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301617 return 0;
Benjamin Limfebbc582019-03-29 07:29:45 -04001618 }
Uma Shankara1596432012-05-25 21:21:44 +05301619
1620 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Paul Emge084be432019-07-08 16:37:06 -07001621 if ( inodes_per_block == 0 ) {
1622 free(blkgrp);
1623 return 0;
1624 }
Benjamin Limfebbc582019-03-29 07:29:45 -04001625 blkno = ext4fs_bg_get_inode_table_id(blkgrp, fs) +
Michael Walle7f101be2016-08-29 10:46:44 +02001626 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankara1596432012-05-25 21:21:44 +05301627 blkoff = (ino % inodes_per_block) * fs->inodesz;
Benjamin Limfebbc582019-03-29 07:29:45 -04001628
1629 /* Free blkgrp as it is no longer required. */
1630 free(blkgrp);
1631
Uma Shankara1596432012-05-25 21:21:44 +05301632 /* Read the inode. */
Frederic Leroy04735e92013-06-26 18:11:25 +02001633 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1634 log2blksz), blkoff,
Uma Shankara1596432012-05-25 21:21:44 +05301635 sizeof(struct ext2_inode), (char *)inode);
1636 if (status == 0)
1637 return 0;
1638
1639 return 1;
1640}
1641
Stephen Warrend5aee652019-01-30 12:58:05 -07001642long int read_allocated_block(struct ext2_inode *inode, int fileblock,
1643 struct ext_block_cache *cache)
Uma Shankara1596432012-05-25 21:21:44 +05301644{
1645 long int blknr;
1646 int blksz;
1647 int log2_blksz;
1648 int status;
1649 long int rblock;
1650 long int perblock_parent;
1651 long int perblock_child;
1652 unsigned long long start;
1653 /* get the blocksize of the filesystem */
1654 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +00001655 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1656 - get_fs()->dev_desc->log2blksz;
1657
Uma Shankara1596432012-05-25 21:21:44 +05301658 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
Stefan Brünsf81db562016-11-05 22:17:14 +01001659 long int startblock, endblock;
Stephen Warrend5aee652019-01-30 12:58:05 -07001660 struct ext_block_cache *c, cd;
Uma Shankara1596432012-05-25 21:21:44 +05301661 struct ext4_extent_header *ext_block;
1662 struct ext4_extent *extent;
Stefan Brünsf81db562016-11-05 22:17:14 +01001663 int i;
Stephen Warrend5aee652019-01-30 12:58:05 -07001664
1665 if (cache) {
1666 c = cache;
1667 } else {
1668 c = &cd;
1669 ext_cache_init(c);
1670 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001671 ext_block =
Stephen Warrend5aee652019-01-30 12:58:05 -07001672 ext4fs_get_extent_block(ext4fs_root, c,
Egbert Eich50ce4c02013-05-01 01:13:19 +00001673 (struct ext4_extent_header *)
1674 inode->b.blocks.dir_blocks,
1675 fileblock, log2_blksz);
Uma Shankara1596432012-05-25 21:21:44 +05301676 if (!ext_block) {
1677 printf("invalid extent block\n");
Stephen Warrend5aee652019-01-30 12:58:05 -07001678 if (!cache)
1679 ext_cache_fini(c);
Uma Shankara1596432012-05-25 21:21:44 +05301680 return -EINVAL;
1681 }
1682
1683 extent = (struct ext4_extent *)(ext_block + 1);
1684
Stefan Brünsf81db562016-11-05 22:17:14 +01001685 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1686 startblock = le32_to_cpu(extent[i].ee_block);
1687 endblock = startblock + le16_to_cpu(extent[i].ee_len);
1688
1689 if (startblock > fileblock) {
1690 /* Sparse file */
Stephen Warrend5aee652019-01-30 12:58:05 -07001691 if (!cache)
1692 ext_cache_fini(c);
Uma Shankara1596432012-05-25 21:21:44 +05301693 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05301694
Stefan Brünsf81db562016-11-05 22:17:14 +01001695 } else if (fileblock < endblock) {
1696 start = le16_to_cpu(extent[i].ee_start_hi);
1697 start = (start << 32) +
Uma Shankara1596432012-05-25 21:21:44 +05301698 le32_to_cpu(extent[i].ee_start_lo);
Stephen Warrend5aee652019-01-30 12:58:05 -07001699 if (!cache)
1700 ext_cache_fini(c);
Stefan Brünsf81db562016-11-05 22:17:14 +01001701 return (fileblock - startblock) + start;
1702 }
Uma Shankara1596432012-05-25 21:21:44 +05301703 }
1704
Stephen Warrend5aee652019-01-30 12:58:05 -07001705 if (!cache)
1706 ext_cache_fini(c);
Stefan Brünsf81db562016-11-05 22:17:14 +01001707 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05301708 }
1709
1710 /* Direct blocks. */
1711 if (fileblock < INDIRECT_BLOCKS)
Michael Walle7f101be2016-08-29 10:46:44 +02001712 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301713
1714 /* Indirect. */
1715 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1716 if (ext4fs_indir1_block == NULL) {
1717 ext4fs_indir1_block = zalloc(blksz);
1718 if (ext4fs_indir1_block == NULL) {
1719 printf("** SI ext2fs read block (indir 1)"
1720 "malloc failed. **\n");
1721 return -1;
1722 }
1723 ext4fs_indir1_size = blksz;
1724 ext4fs_indir1_blkno = -1;
1725 }
1726 if (blksz != ext4fs_indir1_size) {
1727 free(ext4fs_indir1_block);
1728 ext4fs_indir1_block = NULL;
1729 ext4fs_indir1_size = 0;
1730 ext4fs_indir1_blkno = -1;
1731 ext4fs_indir1_block = zalloc(blksz);
1732 if (ext4fs_indir1_block == NULL) {
1733 printf("** SI ext2fs read block (indir 1):"
1734 "malloc failed. **\n");
1735 return -1;
1736 }
1737 ext4fs_indir1_size = blksz;
1738 }
Michael Walle7f101be2016-08-29 10:46:44 +02001739 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301740 log2_blksz) != ext4fs_indir1_blkno) {
1741 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001742 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301743 (inode->b.blocks.
1744 indir_block) << log2_blksz, 0,
1745 blksz, (char *)ext4fs_indir1_block);
1746 if (status == 0) {
1747 printf("** SI ext2fs read block (indir 1)"
1748 "failed. **\n");
Stefan Brünsde9e8312016-09-06 04:36:55 +02001749 return -1;
Uma Shankara1596432012-05-25 21:21:44 +05301750 }
1751 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001752 le32_to_cpu(inode->b.blocks.
Uma Shankara1596432012-05-25 21:21:44 +05301753 indir_block) << log2_blksz;
1754 }
Michael Walle7f101be2016-08-29 10:46:44 +02001755 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankara1596432012-05-25 21:21:44 +05301756 [fileblock - INDIRECT_BLOCKS]);
1757 }
1758 /* Double indirect. */
1759 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1760 (blksz / 4 + 1)))) {
1761
1762 long int perblock = blksz / 4;
1763 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1764
1765 if (ext4fs_indir1_block == NULL) {
1766 ext4fs_indir1_block = zalloc(blksz);
1767 if (ext4fs_indir1_block == NULL) {
1768 printf("** DI ext2fs read block (indir 2 1)"
1769 "malloc failed. **\n");
1770 return -1;
1771 }
1772 ext4fs_indir1_size = blksz;
1773 ext4fs_indir1_blkno = -1;
1774 }
1775 if (blksz != ext4fs_indir1_size) {
1776 free(ext4fs_indir1_block);
1777 ext4fs_indir1_block = NULL;
1778 ext4fs_indir1_size = 0;
1779 ext4fs_indir1_blkno = -1;
1780 ext4fs_indir1_block = zalloc(blksz);
1781 if (ext4fs_indir1_block == NULL) {
1782 printf("** DI ext2fs read block (indir 2 1)"
1783 "malloc failed. **\n");
1784 return -1;
1785 }
1786 ext4fs_indir1_size = blksz;
1787 }
Michael Walle7f101be2016-08-29 10:46:44 +02001788 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301789 log2_blksz) != ext4fs_indir1_blkno) {
1790 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001791 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301792 (inode->b.blocks.
1793 double_indir_block) << log2_blksz,
1794 0, blksz,
1795 (char *)ext4fs_indir1_block);
1796 if (status == 0) {
1797 printf("** DI ext2fs read block (indir 2 1)"
1798 "failed. **\n");
1799 return -1;
1800 }
1801 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001802 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301803 log2_blksz;
1804 }
1805
1806 if (ext4fs_indir2_block == NULL) {
1807 ext4fs_indir2_block = zalloc(blksz);
1808 if (ext4fs_indir2_block == NULL) {
1809 printf("** DI ext2fs read block (indir 2 2)"
1810 "malloc failed. **\n");
1811 return -1;
1812 }
1813 ext4fs_indir2_size = blksz;
1814 ext4fs_indir2_blkno = -1;
1815 }
1816 if (blksz != ext4fs_indir2_size) {
1817 free(ext4fs_indir2_block);
1818 ext4fs_indir2_block = NULL;
1819 ext4fs_indir2_size = 0;
1820 ext4fs_indir2_blkno = -1;
1821 ext4fs_indir2_block = zalloc(blksz);
1822 if (ext4fs_indir2_block == NULL) {
1823 printf("** DI ext2fs read block (indir 2 2)"
1824 "malloc failed. **\n");
1825 return -1;
1826 }
1827 ext4fs_indir2_size = blksz;
1828 }
Michael Walle7f101be2016-08-29 10:46:44 +02001829 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankara1596432012-05-25 21:21:44 +05301830 log2_blksz) != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001831 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301832 (ext4fs_indir1_block
1833 [rblock /
1834 perblock]) << log2_blksz, 0,
1835 blksz,
1836 (char *)ext4fs_indir2_block);
1837 if (status == 0) {
1838 printf("** DI ext2fs read block (indir 2 2)"
1839 "failed. **\n");
1840 return -1;
1841 }
1842 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001843 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301844 /
1845 perblock]) <<
1846 log2_blksz;
1847 }
Michael Walle7f101be2016-08-29 10:46:44 +02001848 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301849 }
1850 /* Tripple indirect. */
1851 else {
1852 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1853 (blksz / 4 * blksz / 4));
1854 perblock_child = blksz / 4;
1855 perblock_parent = ((blksz / 4) * (blksz / 4));
1856
1857 if (ext4fs_indir1_block == NULL) {
1858 ext4fs_indir1_block = zalloc(blksz);
1859 if (ext4fs_indir1_block == NULL) {
1860 printf("** TI ext2fs read block (indir 2 1)"
1861 "malloc failed. **\n");
1862 return -1;
1863 }
1864 ext4fs_indir1_size = blksz;
1865 ext4fs_indir1_blkno = -1;
1866 }
1867 if (blksz != ext4fs_indir1_size) {
1868 free(ext4fs_indir1_block);
1869 ext4fs_indir1_block = NULL;
1870 ext4fs_indir1_size = 0;
1871 ext4fs_indir1_blkno = -1;
1872 ext4fs_indir1_block = zalloc(blksz);
1873 if (ext4fs_indir1_block == NULL) {
1874 printf("** TI ext2fs read block (indir 2 1)"
1875 "malloc failed. **\n");
1876 return -1;
1877 }
1878 ext4fs_indir1_size = blksz;
1879 }
Michael Walle7f101be2016-08-29 10:46:44 +02001880 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301881 log2_blksz) != ext4fs_indir1_blkno) {
1882 status = ext4fs_devread
Frederic Leroy04735e92013-06-26 18:11:25 +02001883 ((lbaint_t)
Michael Walle7f101be2016-08-29 10:46:44 +02001884 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankara1596432012-05-25 21:21:44 +05301885 << log2_blksz, 0, blksz,
1886 (char *)ext4fs_indir1_block);
1887 if (status == 0) {
1888 printf("** TI ext2fs read block (indir 2 1)"
1889 "failed. **\n");
1890 return -1;
1891 }
1892 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001893 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301894 log2_blksz;
1895 }
1896
1897 if (ext4fs_indir2_block == NULL) {
1898 ext4fs_indir2_block = zalloc(blksz);
1899 if (ext4fs_indir2_block == NULL) {
1900 printf("** TI ext2fs read block (indir 2 2)"
1901 "malloc failed. **\n");
1902 return -1;
1903 }
1904 ext4fs_indir2_size = blksz;
1905 ext4fs_indir2_blkno = -1;
1906 }
1907 if (blksz != ext4fs_indir2_size) {
1908 free(ext4fs_indir2_block);
1909 ext4fs_indir2_block = NULL;
1910 ext4fs_indir2_size = 0;
1911 ext4fs_indir2_blkno = -1;
1912 ext4fs_indir2_block = zalloc(blksz);
1913 if (ext4fs_indir2_block == NULL) {
1914 printf("** TI ext2fs read block (indir 2 2)"
1915 "malloc failed. **\n");
1916 return -1;
1917 }
1918 ext4fs_indir2_size = blksz;
1919 }
Michael Walle7f101be2016-08-29 10:46:44 +02001920 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301921 perblock_parent]) <<
1922 log2_blksz)
1923 != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001924 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301925 (ext4fs_indir1_block
1926 [rblock /
1927 perblock_parent]) <<
1928 log2_blksz, 0, blksz,
1929 (char *)ext4fs_indir2_block);
1930 if (status == 0) {
1931 printf("** TI ext2fs read block (indir 2 2)"
1932 "failed. **\n");
1933 return -1;
1934 }
1935 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001936 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301937 perblock_parent])
1938 << log2_blksz;
1939 }
1940
1941 if (ext4fs_indir3_block == NULL) {
1942 ext4fs_indir3_block = zalloc(blksz);
1943 if (ext4fs_indir3_block == NULL) {
1944 printf("** TI ext2fs read block (indir 2 2)"
1945 "malloc failed. **\n");
1946 return -1;
1947 }
1948 ext4fs_indir3_size = blksz;
1949 ext4fs_indir3_blkno = -1;
1950 }
1951 if (blksz != ext4fs_indir3_size) {
1952 free(ext4fs_indir3_block);
1953 ext4fs_indir3_block = NULL;
1954 ext4fs_indir3_size = 0;
1955 ext4fs_indir3_blkno = -1;
1956 ext4fs_indir3_block = zalloc(blksz);
1957 if (ext4fs_indir3_block == NULL) {
1958 printf("** TI ext2fs read block (indir 2 2)"
1959 "malloc failed. **\n");
1960 return -1;
1961 }
1962 ext4fs_indir3_size = blksz;
1963 }
Michael Walle7f101be2016-08-29 10:46:44 +02001964 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301965 /
1966 perblock_child]) <<
1967 log2_blksz) != ext4fs_indir3_blkno) {
1968 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001969 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301970 (ext4fs_indir2_block
1971 [(rblock / perblock_child)
1972 % (blksz / 4)]) << log2_blksz, 0,
1973 blksz, (char *)ext4fs_indir3_block);
1974 if (status == 0) {
1975 printf("** TI ext2fs read block (indir 2 2)"
1976 "failed. **\n");
1977 return -1;
1978 }
1979 ext4fs_indir3_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001980 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301981 perblock_child) %
1982 (blksz /
1983 4)]) <<
1984 log2_blksz;
1985 }
1986
Michael Walle7f101be2016-08-29 10:46:44 +02001987 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankara1596432012-05-25 21:21:44 +05301988 [rblock % perblock_child]);
1989 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001990 debug("read_allocated_block %ld\n", blknr);
Uma Shankara1596432012-05-25 21:21:44 +05301991
1992 return blknr;
1993}
1994
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001995/**
1996 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1997 * global pointers
1998 *
1999 * This function assures that for a file with the same name but different size
2000 * the sequential store on the ext4 filesystem will be correct.
2001 *
2002 * In this function the global data, responsible for internal representation
2003 * of the ext4 data are initialized to the reset state. Without this, during
2004 * replacement of the smaller file with the bigger truncation of new file was
2005 * performed.
2006 */
2007void ext4fs_reinit_global(void)
Uma Shankara1596432012-05-25 21:21:44 +05302008{
Uma Shankara1596432012-05-25 21:21:44 +05302009 if (ext4fs_indir1_block != NULL) {
2010 free(ext4fs_indir1_block);
2011 ext4fs_indir1_block = NULL;
2012 ext4fs_indir1_size = 0;
2013 ext4fs_indir1_blkno = -1;
2014 }
2015 if (ext4fs_indir2_block != NULL) {
2016 free(ext4fs_indir2_block);
2017 ext4fs_indir2_block = NULL;
2018 ext4fs_indir2_size = 0;
2019 ext4fs_indir2_blkno = -1;
2020 }
2021 if (ext4fs_indir3_block != NULL) {
2022 free(ext4fs_indir3_block);
2023 ext4fs_indir3_block = NULL;
2024 ext4fs_indir3_size = 0;
2025 ext4fs_indir3_blkno = -1;
2026 }
2027}
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02002028void ext4fs_close(void)
2029{
2030 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
2031 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
2032 ext4fs_file = NULL;
2033 }
2034 if (ext4fs_root != NULL) {
2035 free(ext4fs_root);
2036 ext4fs_root = NULL;
2037 }
2038
2039 ext4fs_reinit_global();
2040}
Uma Shankara1596432012-05-25 21:21:44 +05302041
2042int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
2043 struct ext2fs_node **fnode, int *ftype)
2044{
2045 unsigned int fpos = 0;
2046 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002047 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302048 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2049
2050#ifdef DEBUG
2051 if (name != NULL)
2052 printf("Iterate dir %s\n", name);
2053#endif /* of DEBUG */
2054 if (!diro->inode_read) {
2055 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2056 if (status == 0)
2057 return 0;
2058 }
2059 /* Search the file. */
Michael Walle7f101be2016-08-29 10:46:44 +02002060 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankara1596432012-05-25 21:21:44 +05302061 struct ext2_dirent dirent;
2062
2063 status = ext4fs_read_file(diro, fpos,
2064 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002065 (char *)&dirent, &actread);
2066 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302067 return 0;
2068
Thomas Fitzsimmons54d68e92015-11-18 12:42:53 -05002069 if (dirent.direntlen == 0) {
2070 printf("Failed to iterate over directory %s\n", name);
2071 return 0;
2072 }
2073
Uma Shankara1596432012-05-25 21:21:44 +05302074 if (dirent.namelen != 0) {
2075 char filename[dirent.namelen + 1];
2076 struct ext2fs_node *fdiro;
2077 int type = FILETYPE_UNKNOWN;
2078
2079 status = ext4fs_read_file(diro,
2080 fpos +
2081 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002082 dirent.namelen, filename,
2083 &actread);
2084 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302085 return 0;
2086
2087 fdiro = zalloc(sizeof(struct ext2fs_node));
2088 if (!fdiro)
2089 return 0;
2090
2091 fdiro->data = diro->data;
Michael Walle7f101be2016-08-29 10:46:44 +02002092 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankara1596432012-05-25 21:21:44 +05302093
2094 filename[dirent.namelen] = '\0';
2095
2096 if (dirent.filetype != FILETYPE_UNKNOWN) {
2097 fdiro->inode_read = 0;
2098
2099 if (dirent.filetype == FILETYPE_DIRECTORY)
2100 type = FILETYPE_DIRECTORY;
2101 else if (dirent.filetype == FILETYPE_SYMLINK)
2102 type = FILETYPE_SYMLINK;
2103 else if (dirent.filetype == FILETYPE_REG)
2104 type = FILETYPE_REG;
2105 } else {
2106 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002107 le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05302108 (dirent.inode),
2109 &fdiro->inode);
2110 if (status == 0) {
2111 free(fdiro);
2112 return 0;
2113 }
2114 fdiro->inode_read = 1;
2115
Michael Walle7f101be2016-08-29 10:46:44 +02002116 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankara1596432012-05-25 21:21:44 +05302117 FILETYPE_INO_MASK) ==
2118 FILETYPE_INO_DIRECTORY) {
2119 type = FILETYPE_DIRECTORY;
Michael Walle7f101be2016-08-29 10:46:44 +02002120 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302121 & FILETYPE_INO_MASK) ==
2122 FILETYPE_INO_SYMLINK) {
2123 type = FILETYPE_SYMLINK;
Michael Walle7f101be2016-08-29 10:46:44 +02002124 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302125 & FILETYPE_INO_MASK) ==
2126 FILETYPE_INO_REG) {
2127 type = FILETYPE_REG;
2128 }
2129 }
2130#ifdef DEBUG
2131 printf("iterate >%s<\n", filename);
2132#endif /* of DEBUG */
2133 if ((name != NULL) && (fnode != NULL)
2134 && (ftype != NULL)) {
2135 if (strcmp(filename, name) == 0) {
2136 *ftype = type;
2137 *fnode = fdiro;
2138 return 1;
2139 }
2140 } else {
2141 if (fdiro->inode_read == 0) {
2142 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002143 le32_to_cpu(
Uma Shankara1596432012-05-25 21:21:44 +05302144 dirent.inode),
2145 &fdiro->inode);
2146 if (status == 0) {
2147 free(fdiro);
2148 return 0;
2149 }
2150 fdiro->inode_read = 1;
2151 }
2152 switch (type) {
2153 case FILETYPE_DIRECTORY:
2154 printf("<DIR> ");
2155 break;
2156 case FILETYPE_SYMLINK:
2157 printf("<SYM> ");
2158 break;
2159 case FILETYPE_REG:
2160 printf(" ");
2161 break;
2162 default:
2163 printf("< ? > ");
2164 break;
2165 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002166 printf("%10u %s\n",
Michael Walle7f101be2016-08-29 10:46:44 +02002167 le32_to_cpu(fdiro->inode.size),
Uma Shankara1596432012-05-25 21:21:44 +05302168 filename);
2169 }
2170 free(fdiro);
2171 }
Michael Walle7f101be2016-08-29 10:46:44 +02002172 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankara1596432012-05-25 21:21:44 +05302173 }
2174 return 0;
2175}
2176
2177static char *ext4fs_read_symlink(struct ext2fs_node *node)
2178{
2179 char *symlink;
2180 struct ext2fs_node *diro = node;
2181 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002182 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302183
2184 if (!diro->inode_read) {
2185 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2186 if (status == 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002187 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302188 }
Michael Walle7f101be2016-08-29 10:46:44 +02002189 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankara1596432012-05-25 21:21:44 +05302190 if (!symlink)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002191 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302192
Michael Walle7f101be2016-08-29 10:46:44 +02002193 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankara1596432012-05-25 21:21:44 +05302194 strncpy(symlink, diro->inode.b.symlink,
Michael Walle7f101be2016-08-29 10:46:44 +02002195 le32_to_cpu(diro->inode.size));
Uma Shankara1596432012-05-25 21:21:44 +05302196 } else {
2197 status = ext4fs_read_file(diro, 0,
Michael Walle7f101be2016-08-29 10:46:44 +02002198 le32_to_cpu(diro->inode.size),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002199 symlink, &actread);
Gary Bisson9d2f6a92015-09-07 11:20:07 +02002200 if ((status < 0) || (actread == 0)) {
Uma Shankara1596432012-05-25 21:21:44 +05302201 free(symlink);
Michael Walle58a9ecb2016-09-01 11:21:40 +02002202 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302203 }
2204 }
Michael Walle7f101be2016-08-29 10:46:44 +02002205 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankara1596432012-05-25 21:21:44 +05302206 return symlink;
2207}
2208
2209static int ext4fs_find_file1(const char *currpath,
2210 struct ext2fs_node *currroot,
2211 struct ext2fs_node **currfound, int *foundtype)
2212{
2213 char fpath[strlen(currpath) + 1];
2214 char *name = fpath;
2215 char *next;
2216 int status;
2217 int type = FILETYPE_DIRECTORY;
2218 struct ext2fs_node *currnode = currroot;
2219 struct ext2fs_node *oldnode = currroot;
2220
2221 strncpy(fpath, currpath, strlen(currpath) + 1);
2222
2223 /* Remove all leading slashes. */
2224 while (*name == '/')
2225 name++;
2226
2227 if (!*name) {
2228 *currfound = currnode;
2229 return 1;
2230 }
2231
2232 for (;;) {
2233 int found;
2234
2235 /* Extract the actual part from the pathname. */
2236 next = strchr(name, '/');
2237 if (next) {
2238 /* Remove all leading slashes. */
2239 while (*next == '/')
2240 *(next++) = '\0';
2241 }
2242
2243 if (type != FILETYPE_DIRECTORY) {
2244 ext4fs_free_node(currnode, currroot);
2245 return 0;
2246 }
2247
2248 oldnode = currnode;
2249
2250 /* Iterate over the directory. */
2251 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2252 if (found == 0)
2253 return 0;
2254
2255 if (found == -1)
2256 break;
2257
2258 /* Read in the symlink and follow it. */
2259 if (type == FILETYPE_SYMLINK) {
2260 char *symlink;
2261
2262 /* Test if the symlink does not loop. */
2263 if (++symlinknest == 8) {
2264 ext4fs_free_node(currnode, currroot);
2265 ext4fs_free_node(oldnode, currroot);
2266 return 0;
2267 }
2268
2269 symlink = ext4fs_read_symlink(currnode);
2270 ext4fs_free_node(currnode, currroot);
2271
2272 if (!symlink) {
2273 ext4fs_free_node(oldnode, currroot);
2274 return 0;
2275 }
2276
2277 debug("Got symlink >%s<\n", symlink);
2278
2279 if (symlink[0] == '/') {
2280 ext4fs_free_node(oldnode, currroot);
2281 oldnode = &ext4fs_root->diropen;
2282 }
2283
2284 /* Lookup the node the symlink points to. */
2285 status = ext4fs_find_file1(symlink, oldnode,
2286 &currnode, &type);
2287
2288 free(symlink);
2289
2290 if (status == 0) {
2291 ext4fs_free_node(oldnode, currroot);
2292 return 0;
2293 }
2294 }
2295
2296 ext4fs_free_node(oldnode, currroot);
2297
2298 /* Found the node! */
2299 if (!next || *next == '\0') {
2300 *currfound = currnode;
2301 *foundtype = type;
2302 return 1;
2303 }
2304 name = next;
2305 }
2306 return -1;
2307}
2308
2309int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2310 struct ext2fs_node **foundnode, int expecttype)
2311{
2312 int status;
2313 int foundtype = FILETYPE_DIRECTORY;
2314
2315 symlinknest = 0;
2316 if (!path)
2317 return 0;
2318
2319 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2320 if (status == 0)
2321 return 0;
2322
2323 /* Check if the node that was found was of the expected type. */
2324 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2325 return 0;
2326 else if ((expecttype == FILETYPE_DIRECTORY)
2327 && (foundtype != expecttype))
2328 return 0;
2329
2330 return 1;
2331}
2332
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002333int ext4fs_open(const char *filename, loff_t *len)
Uma Shankara1596432012-05-25 21:21:44 +05302334{
2335 struct ext2fs_node *fdiro = NULL;
2336 int status;
Uma Shankara1596432012-05-25 21:21:44 +05302337
2338 if (ext4fs_root == NULL)
2339 return -1;
2340
2341 ext4fs_file = NULL;
2342 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2343 FILETYPE_REG);
2344 if (status == 0)
2345 goto fail;
2346
2347 if (!fdiro->inode_read) {
2348 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2349 &fdiro->inode);
2350 if (status == 0)
2351 goto fail;
2352 }
Michael Walle7f101be2016-08-29 10:46:44 +02002353 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankara1596432012-05-25 21:21:44 +05302354 ext4fs_file = fdiro;
2355
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002356 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05302357fail:
2358 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2359
2360 return -1;
2361}
2362
2363int ext4fs_mount(unsigned part_length)
2364{
2365 struct ext2_data *data;
2366 int status;
2367 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00002368 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankara1596432012-05-25 21:21:44 +05302369 if (!data)
2370 return 0;
2371
2372 /* Read the superblock. */
Egbert Eich50ce4c02013-05-01 01:13:19 +00002373 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankara1596432012-05-25 21:21:44 +05302374
2375 if (status == 0)
2376 goto fail;
2377
2378 /* Make sure this is an ext2 filesystem. */
Michael Walle7f101be2016-08-29 10:46:44 +02002379 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Marek Behún51be4712018-03-08 00:26:08 +01002380 goto fail_noerr;
Uma Shankara1596432012-05-25 21:21:44 +05302381
Tom Rini6f94ab62016-07-22 17:59:11 -04002382
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002383 if (le32_to_cpu(data->sblock.revision_level) == 0) {
Uma Shankara1596432012-05-25 21:21:44 +05302384 fs->inodesz = 128;
Stefan Brüns3cc5bbb2016-12-27 02:35:08 +01002385 fs->gdsize = 32;
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002386 } else {
2387 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2388 __le32_to_cpu(data->sblock.feature_compatibility),
2389 __le32_to_cpu(data->sblock.feature_incompat),
2390 __le32_to_cpu(data->sblock.feature_ro_compat));
Uma Shankara1596432012-05-25 21:21:44 +05302391
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002392 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2393 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2394 EXT4_FEATURE_INCOMPAT_64BIT ?
2395 le16_to_cpu(data->sblock.descriptor_size) : 32;
2396 }
2397
2398 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2399 le32_to_cpu(data->sblock.revision_level),
2400 fs->inodesz, fs->gdsize);
Uma Shankara1596432012-05-25 21:21:44 +05302401
2402 data->diropen.data = data;
2403 data->diropen.ino = 2;
2404 data->diropen.inode_read = 1;
2405 data->inode = &data->diropen.inode;
2406
2407 status = ext4fs_read_inode(data, 2, data->inode);
2408 if (status == 0)
2409 goto fail;
2410
2411 ext4fs_root = data;
2412
2413 return 1;
2414fail:
2415 printf("Failed to mount ext2 filesystem...\n");
Marek Behún51be4712018-03-08 00:26:08 +01002416fail_noerr:
Uma Shankara1596432012-05-25 21:21:44 +05302417 free(data);
2418 ext4fs_root = NULL;
2419
2420 return 0;
2421}