blob: deca954e08c300b00180032898888779e35acf15 [file] [log] [blame]
Uma Shankara1596432012-05-25 21:21:44 +05301/*
2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
4 * Uma Shankar <uma.shankar@samsung.com>
5 * Manjunatha C Achar <a.manjunatha@samsung.com>
6 *
7 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
8 *
9 * (C) Copyright 2004
10 * esd gmbh <www.esd-electronics.com>
11 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
12 *
13 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
14 * GRUB -- GRand Unified Bootloader
15 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
16 *
Uma Shankared34f342012-05-25 21:22:49 +053017 * ext4write : Based on generic ext4 protocol.
18 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020019 * SPDX-License-Identifier: GPL-2.0+
Uma Shankara1596432012-05-25 21:21:44 +053020 */
21
22#include <common.h>
23#include <ext_common.h>
24#include <ext4fs.h>
Simon Glassaac618a2014-10-15 04:38:32 -060025#include <inttypes.h>
Uma Shankara1596432012-05-25 21:21:44 +053026#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060027#include <memalign.h>
Uma Shankara1596432012-05-25 21:21:44 +053028#include <stddef.h>
29#include <linux/stat.h>
30#include <linux/time.h>
31#include <asm/byteorder.h>
32#include "ext4_common.h"
33
34struct ext2_data *ext4fs_root;
35struct ext2fs_node *ext4fs_file;
Michael Walle58a9ecb2016-09-01 11:21:40 +020036__le32 *ext4fs_indir1_block;
Uma Shankara1596432012-05-25 21:21:44 +053037int ext4fs_indir1_size;
38int ext4fs_indir1_blkno = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +020039__le32 *ext4fs_indir2_block;
Uma Shankara1596432012-05-25 21:21:44 +053040int ext4fs_indir2_size;
41int ext4fs_indir2_blkno = -1;
42
Michael Walle58a9ecb2016-09-01 11:21:40 +020043__le32 *ext4fs_indir3_block;
Uma Shankara1596432012-05-25 21:21:44 +053044int ext4fs_indir3_size;
45int ext4fs_indir3_blkno = -1;
46struct ext2_inode *g_parent_inode;
47static int symlinknest;
48
Stephen Warren03e2ecf2012-10-22 06:43:50 +000049#if defined(CONFIG_EXT4_WRITE)
Michael Walle58a9ecb2016-09-01 11:21:40 +020050static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
51{
52 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
53}
54
55static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
56{
57 sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) - 1);
58}
59
60static inline void ext4fs_bg_free_inodes_dec(struct ext2_block_group *bg)
61{
62 bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) - 1);
63}
64
65static inline void ext4fs_bg_free_blocks_dec(struct ext2_block_group *bg)
66{
67 bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) - 1);
68}
69
70static inline void ext4fs_bg_itable_unused_dec(struct ext2_block_group *bg)
71{
72 bg->bg_itable_unused = cpu_to_le16(le16_to_cpu(bg->bg_itable_unused) - 1);
73}
74
Uma Shankared34f342012-05-25 21:22:49 +053075uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
76{
77 uint32_t res = size / n;
78 if (res * n != size)
79 res++;
80
81 return res;
82}
83
84void put_ext4(uint64_t off, void *buf, uint32_t size)
85{
86 uint64_t startblock;
87 uint64_t remainder;
88 unsigned char *temp_ptr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +053089 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +000090 int log2blksz = fs->dev_desc->log2blksz;
91 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankared34f342012-05-25 21:22:49 +053092
Egbert Eich50ce4c02013-05-01 01:13:19 +000093 startblock = off >> log2blksz;
Uma Shankared34f342012-05-25 21:22:49 +053094 startblock += part_offset;
Egbert Eich50ce4c02013-05-01 01:13:19 +000095 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankared34f342012-05-25 21:22:49 +053096
97 if (fs->dev_desc == NULL)
98 return;
99
Egbert Eich50ce4c02013-05-01 01:13:19 +0000100 if ((startblock + (size >> log2blksz)) >
Uma Shankared34f342012-05-25 21:22:49 +0530101 (part_offset + fs->total_sect)) {
Frederic Leroy04735e92013-06-26 18:11:25 +0200102 printf("part_offset is " LBAFU "\n", part_offset);
Simon Glassaac618a2014-10-15 04:38:32 -0600103 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
Uma Shankared34f342012-05-25 21:22:49 +0530104 printf("error: overflow occurs\n");
105 return;
106 }
107
108 if (remainder) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700109 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
110 temp_ptr = sec_buf;
111 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
112 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530113 } else {
Egbert Eich50ce4c02013-05-01 01:13:19 +0000114 if (size >> log2blksz != 0) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700115 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
116 (unsigned long *)buf);
Uma Shankared34f342012-05-25 21:22:49 +0530117 } else {
Simon Glass2a981dc2016-02-29 15:25:52 -0700118 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530119 temp_ptr = sec_buf;
120 memcpy(temp_ptr, buf, size);
Simon Glass2a981dc2016-02-29 15:25:52 -0700121 blk_dwrite(fs->dev_desc, startblock, 1,
122 (unsigned long *)sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530123 }
124 }
125}
126
127static int _get_new_inode_no(unsigned char *buffer)
128{
129 struct ext_filesystem *fs = get_fs();
130 unsigned char input;
131 int operand, status;
132 int count = 1;
133 int j = 0;
134
135 /* get the blocksize of the filesystem */
136 unsigned char *ptr = buffer;
137 while (*ptr == 255) {
138 ptr++;
139 count += 8;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200140 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankared34f342012-05-25 21:22:49 +0530141 return -1;
142 }
143
144 for (j = 0; j < fs->blksz; j++) {
145 input = *ptr;
146 int i = 0;
147 while (i <= 7) {
148 operand = 1 << i;
149 status = input & operand;
150 if (status) {
151 i++;
152 count++;
153 } else {
154 *ptr |= operand;
155 return count;
156 }
157 }
158 ptr = ptr + 1;
159 }
160
161 return -1;
162}
163
164static int _get_new_blk_no(unsigned char *buffer)
165{
166 unsigned char input;
167 int operand, status;
168 int count = 0;
169 int j = 0;
170 unsigned char *ptr = buffer;
171 struct ext_filesystem *fs = get_fs();
172
173 if (fs->blksz != 1024)
174 count = 0;
175 else
176 count = 1;
177
178 while (*ptr == 255) {
179 ptr++;
180 count += 8;
181 if (count == (fs->blksz * 8))
182 return -1;
183 }
184
185 for (j = 0; j < fs->blksz; j++) {
186 input = *ptr;
187 int i = 0;
188 while (i <= 7) {
189 operand = 1 << i;
190 status = input & operand;
191 if (status) {
192 i++;
193 count++;
194 } else {
195 *ptr |= operand;
196 return count;
197 }
198 }
199 ptr = ptr + 1;
200 }
201
202 return -1;
203}
204
205int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
206{
207 int i, remainder, status;
208 unsigned char *ptr = buffer;
209 unsigned char operand;
210 i = blockno / 8;
211 remainder = blockno % 8;
212 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
213
214 i = i - (index * blocksize);
215 if (blocksize != 1024) {
216 ptr = ptr + i;
217 operand = 1 << remainder;
218 status = *ptr & operand;
219 if (status)
220 return -1;
221
222 *ptr = *ptr | operand;
223 return 0;
224 } else {
225 if (remainder == 0) {
226 ptr = ptr + i - 1;
227 operand = (1 << 7);
228 } else {
229 ptr = ptr + i;
230 operand = (1 << (remainder - 1));
231 }
232 status = *ptr & operand;
233 if (status)
234 return -1;
235
236 *ptr = *ptr | operand;
237 return 0;
238 }
239}
240
241void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
242{
243 int i, remainder, status;
244 unsigned char *ptr = buffer;
245 unsigned char operand;
246 i = blockno / 8;
247 remainder = blockno % 8;
248 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
249
250 i = i - (index * blocksize);
251 if (blocksize != 1024) {
252 ptr = ptr + i;
253 operand = (1 << remainder);
254 status = *ptr & operand;
255 if (status)
256 *ptr = *ptr & ~(operand);
257 } else {
258 if (remainder == 0) {
259 ptr = ptr + i - 1;
260 operand = (1 << 7);
261 } else {
262 ptr = ptr + i;
263 operand = (1 << (remainder - 1));
264 }
265 status = *ptr & operand;
266 if (status)
267 *ptr = *ptr & ~(operand);
268 }
269}
270
271int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
272{
273 int i, remainder, status;
274 unsigned char *ptr = buffer;
275 unsigned char operand;
276
Michael Walle58a9ecb2016-09-01 11:21:40 +0200277 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530278 i = inode_no / 8;
279 remainder = inode_no % 8;
280 if (remainder == 0) {
281 ptr = ptr + i - 1;
282 operand = (1 << 7);
283 } else {
284 ptr = ptr + i;
285 operand = (1 << (remainder - 1));
286 }
287 status = *ptr & operand;
288 if (status)
289 return -1;
290
291 *ptr = *ptr | operand;
292
293 return 0;
294}
295
296void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
297{
298 int i, remainder, status;
299 unsigned char *ptr = buffer;
300 unsigned char operand;
301
Michael Walle58a9ecb2016-09-01 11:21:40 +0200302 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530303 i = inode_no / 8;
304 remainder = inode_no % 8;
305 if (remainder == 0) {
306 ptr = ptr + i - 1;
307 operand = (1 << 7);
308 } else {
309 ptr = ptr + i;
310 operand = (1 << (remainder - 1));
311 }
312 status = *ptr & operand;
313 if (status)
314 *ptr = *ptr & ~(operand);
315}
316
Michael Walle58a9ecb2016-09-01 11:21:40 +0200317uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankared34f342012-05-25 21:22:49 +0530318{
319 struct ext2_block_group *desc;
320 struct ext_filesystem *fs = get_fs();
Michael Walle58a9ecb2016-09-01 11:21:40 +0200321 uint16_t crc = 0;
322 __le32 le32_i = cpu_to_le32(i);
Uma Shankared34f342012-05-25 21:22:49 +0530323
Simon Glass73c15c62012-10-03 11:37:49 +0000324 desc = (struct ext2_block_group *)&fs->bgd[i];
Michael Walle58a9ecb2016-09-01 11:21:40 +0200325 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankared34f342012-05-25 21:22:49 +0530326 int offset = offsetof(struct ext2_block_group, bg_checksum);
327
328 crc = ext2fs_crc16(~0, fs->sb->unique_id,
329 sizeof(fs->sb->unique_id));
Michael Walle58a9ecb2016-09-01 11:21:40 +0200330 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
Uma Shankared34f342012-05-25 21:22:49 +0530331 crc = ext2fs_crc16(crc, desc, offset);
332 offset += sizeof(desc->bg_checksum); /* skip checksum */
333 assert(offset == sizeof(*desc));
334 }
335
336 return crc;
337}
338
339static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
340{
341 int dentry_length;
342 int sizeof_void_space;
343 int new_entry_byte_reqd;
344 short padding_factor = 0;
345
346 if (dir->namelen % 4 != 0)
347 padding_factor = 4 - (dir->namelen % 4);
348
349 dentry_length = sizeof(struct ext2_dirent) +
350 dir->namelen + padding_factor;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200351 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankared34f342012-05-25 21:22:49 +0530352 if (sizeof_void_space == 0)
353 return 0;
354
355 padding_factor = 0;
356 if (strlen(filename) % 4 != 0)
357 padding_factor = 4 - (strlen(filename) % 4);
358
359 new_entry_byte_reqd = strlen(filename) +
360 sizeof(struct ext2_dirent) + padding_factor;
361 if (sizeof_void_space >= new_entry_byte_reqd) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200362 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankared34f342012-05-25 21:22:49 +0530363 return sizeof_void_space;
364 }
365
366 return 0;
367}
368
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200369int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankared34f342012-05-25 21:22:49 +0530370{
371 unsigned int *zero_buffer = NULL;
372 char *root_first_block_buffer = NULL;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200373 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530374 long int first_block_no_of_root = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530375 int totalbytes = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530376 unsigned int new_entry_byte_reqd;
Uma Shankared34f342012-05-25 21:22:49 +0530377 int sizeof_void_space = 0;
378 int templength = 0;
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200379 int inodeno = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530380 int status;
381 struct ext_filesystem *fs = get_fs();
382 /* directory entry */
383 struct ext2_dirent *dir;
Uma Shankared34f342012-05-25 21:22:49 +0530384 char *temp_dir = NULL;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200385 uint32_t new_blk_no;
386 uint32_t new_size;
387 uint32_t new_blockcnt;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200388 uint32_t directory_blocks;
Uma Shankared34f342012-05-25 21:22:49 +0530389
390 zero_buffer = zalloc(fs->blksz);
391 if (!zero_buffer) {
392 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200393 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530394 }
395 root_first_block_buffer = zalloc(fs->blksz);
396 if (!root_first_block_buffer) {
397 free(zero_buffer);
398 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200399 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530400 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200401 new_entry_byte_reqd = ROUND(strlen(filename) +
402 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530403restart:
Stefan Brünsa321abd2016-09-06 04:36:44 +0200404 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
405 LOG2_BLOCK_SIZE(ext4fs_root);
406 blk_idx = directory_blocks - 1;
Uma Shankared34f342012-05-25 21:22:49 +0530407
Stefan Brünsa321abd2016-09-06 04:36:44 +0200408restart_read:
Uma Shankared34f342012-05-25 21:22:49 +0530409 /* read the block no allocated to a file */
Stefan Brünsa321abd2016-09-06 04:36:44 +0200410 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
411 if (first_block_no_of_root <= 0)
412 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530413
Frederic Leroy04735e92013-06-26 18:11:25 +0200414 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankared34f342012-05-25 21:22:49 +0530415 * fs->sect_perblk,
416 0, fs->blksz, root_first_block_buffer);
417 if (status == 0)
418 goto fail;
419
420 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
421 goto fail;
422 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankared34f342012-05-25 21:22:49 +0530423 totalbytes = 0;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200424
Michael Walle58a9ecb2016-09-01 11:21:40 +0200425 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsa321abd2016-09-06 04:36:44 +0200426 unsigned short used_len = ROUND(dir->namelen +
427 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530428
Stefan Brünsa321abd2016-09-06 04:36:44 +0200429 /* last entry of block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200430 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankared34f342012-05-25 21:22:49 +0530431
Stefan Brünsa321abd2016-09-06 04:36:44 +0200432 /* check if new entry fits */
433 if ((used_len + new_entry_byte_reqd) <=
434 le16_to_cpu(dir->direntlen)) {
435 dir->direntlen = cpu_to_le16(used_len);
436 break;
437 } else {
438 if (blk_idx > 0) {
439 printf("Block full, trying previous\n");
440 blk_idx--;
441 goto restart_read;
442 }
443 printf("All blocks full: Allocate new\n");
Uma Shankared34f342012-05-25 21:22:49 +0530444
Stefan Brünsb96c3c72016-09-06 04:36:43 +0200445 if (le32_to_cpu(g_parent_inode->flags) &
446 EXT4_EXTENTS_FL) {
447 printf("Directory uses extents\n");
448 goto fail;
449 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200450 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankared34f342012-05-25 21:22:49 +0530451 printf("Directory exceeds limit\n");
452 goto fail;
453 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200454 new_blk_no = ext4fs_get_new_blk_no();
455 if (new_blk_no == -1) {
Uma Shankared34f342012-05-25 21:22:49 +0530456 printf("no block left to assign\n");
457 goto fail;
458 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200459 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200460 g_parent_inode->b.blocks.
461 dir_blocks[directory_blocks] =
Michael Walle58a9ecb2016-09-01 11:21:40 +0200462 cpu_to_le32(new_blk_no);
463
464 new_size = le32_to_cpu(g_parent_inode->size);
465 new_size += fs->blksz;
466 g_parent_inode->size = cpu_to_le32(new_size);
467
468 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
469 new_blockcnt += fs->sect_perblk;
470 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
471
Uma Shankared34f342012-05-25 21:22:49 +0530472 if (ext4fs_put_metadata
473 (root_first_block_buffer,
474 first_block_no_of_root))
475 goto fail;
476 goto restart;
477 }
Uma Shankared34f342012-05-25 21:22:49 +0530478 }
479
Michael Walle58a9ecb2016-09-01 11:21:40 +0200480 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530481 totalbytes = totalbytes + templength;
482 sizeof_void_space = check_void_in_dentry(dir, filename);
483 if (sizeof_void_space)
484 break;
485
486 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530487 }
488
489 /* make a pointer ready for creating next directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200490 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530491 totalbytes = totalbytes + templength;
492 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530493
494 /* get the next available inode number */
495 inodeno = ext4fs_get_new_inode_no();
496 if (inodeno == -1) {
497 printf("no inode left to assign\n");
498 goto fail;
499 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200500 dir->inode = cpu_to_le32(inodeno);
Uma Shankared34f342012-05-25 21:22:49 +0530501 if (sizeof_void_space)
Michael Walle58a9ecb2016-09-01 11:21:40 +0200502 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankared34f342012-05-25 21:22:49 +0530503 else
Michael Walle58a9ecb2016-09-01 11:21:40 +0200504 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankared34f342012-05-25 21:22:49 +0530505
506 dir->namelen = strlen(filename);
507 dir->filetype = FILETYPE_REG; /* regular file */
508 temp_dir = (char *)dir;
509 temp_dir = temp_dir + sizeof(struct ext2_dirent);
510 memcpy(temp_dir, filename, strlen(filename));
511
Uma Shankared34f342012-05-25 21:22:49 +0530512 /* update or write the 1st block of root inode */
513 if (ext4fs_put_metadata(root_first_block_buffer,
514 first_block_no_of_root))
515 goto fail;
516
517fail:
518 free(zero_buffer);
519 free(root_first_block_buffer);
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200520
521 return inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530522}
523
524static int search_dir(struct ext2_inode *parent_inode, char *dirname)
525{
526 int status;
Stefan Brüns76a29512016-09-06 04:36:41 +0200527 int inodeno = 0;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200528 int offset;
529 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530530 long int blknr;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200531 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530532 struct ext2_dirent *dir = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530533 struct ext_filesystem *fs = get_fs();
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200534 uint32_t directory_blocks;
535 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530536
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200537 directory_blocks = le32_to_cpu(parent_inode->size) >>
538 LOG2_BLOCK_SIZE(ext4fs_root);
539
540 block_buffer = zalloc(fs->blksz);
541 if (!block_buffer)
542 goto fail;
543
544 /* get the block no allocated to a file */
545 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
546 blknr = read_allocated_block(parent_inode, blk_idx);
Uma Shankared34f342012-05-25 21:22:49 +0530547 if (blknr == 0)
548 goto fail;
549
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200550 /* read the directory block */
Frederic Leroy04735e92013-06-26 18:11:25 +0200551 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +0530552 0, fs->blksz, (char *)block_buffer);
553 if (status == 0)
554 goto fail;
555
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200556 offset = 0;
557 do {
558 dir = (struct ext2_dirent *)(block_buffer + offset);
559 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankared34f342012-05-25 21:22:49 +0530560
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200561 int direntlen = le16_to_cpu(dir->direntlen);
562 if (direntlen < sizeof(struct ext2_dirent))
Uma Shankared34f342012-05-25 21:22:49 +0530563 break;
564
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200565 if (dir->inode && (strlen(dirname) == dir->namelen) &&
566 (strncmp(dirname, direntname, dir->namelen) == 0)) {
567 inodeno = le32_to_cpu(dir->inode);
568 break;
569 }
Uma Shankared34f342012-05-25 21:22:49 +0530570
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200571 offset += direntlen;
Stefan Brüns76a29512016-09-06 04:36:41 +0200572
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200573 } while (offset < fs->blksz);
574
575 if (inodeno > 0) {
576 free(block_buffer);
Stefan Brüns76a29512016-09-06 04:36:41 +0200577 return inodeno;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200578 }
Uma Shankared34f342012-05-25 21:22:49 +0530579 }
580
581fail:
582 free(block_buffer);
583
584 return -1;
585}
586
587static int find_dir_depth(char *dirname)
588{
589 char *token = strtok(dirname, "/");
590 int count = 0;
591 while (token != NULL) {
592 token = strtok(NULL, "/");
593 count++;
594 }
595 return count + 1 + 1;
596 /*
597 * for example for string /home/temp
598 * depth=home(1)+temp(1)+1 extra for NULL;
599 * so count is 4;
600 */
601}
602
603static int parse_path(char **arr, char *dirname)
604{
605 char *token = strtok(dirname, "/");
606 int i = 0;
607
608 /* add root */
609 arr[i] = zalloc(strlen("/") + 1);
610 if (!arr[i])
611 return -ENOMEM;
Stephen Warren934b14f2015-09-04 22:03:44 -0600612 memcpy(arr[i++], "/", strlen("/"));
Uma Shankared34f342012-05-25 21:22:49 +0530613
614 /* add each path entry after root */
615 while (token != NULL) {
616 arr[i] = zalloc(strlen(token) + 1);
617 if (!arr[i])
618 return -ENOMEM;
619 memcpy(arr[i++], token, strlen(token));
620 token = strtok(NULL, "/");
621 }
622 arr[i] = NULL;
623
624 return 0;
625}
626
627int ext4fs_iget(int inode_no, struct ext2_inode *inode)
628{
629 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
630 return -1;
631
632 return 0;
633}
634
635/*
636 * Function: ext4fs_get_parent_inode_num
637 * Return Value: inode Number of the parent directory of file/Directory to be
638 * created
639 * dirname : Input parmater, input path name of the file/directory to be created
640 * dname : Output parameter, to be filled with the name of the directory
641 * extracted from dirname
642 */
643int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
644{
645 int i;
646 int depth = 0;
647 int matched_inode_no;
648 int result_inode_no = -1;
649 char **ptr = NULL;
650 char *depth_dirname = NULL;
651 char *parse_dirname = NULL;
652 struct ext2_inode *parent_inode = NULL;
653 struct ext2_inode *first_inode = NULL;
654 struct ext2_inode temp_inode;
655
656 if (*dirname != '/') {
657 printf("Please supply Absolute path\n");
658 return -1;
659 }
660
661 /* TODO: input validation make equivalent to linux */
662 depth_dirname = zalloc(strlen(dirname) + 1);
663 if (!depth_dirname)
664 return -ENOMEM;
665
666 memcpy(depth_dirname, dirname, strlen(dirname));
667 depth = find_dir_depth(depth_dirname);
668 parse_dirname = zalloc(strlen(dirname) + 1);
669 if (!parse_dirname)
670 goto fail;
671 memcpy(parse_dirname, dirname, strlen(dirname));
672
673 /* allocate memory for each directory level */
674 ptr = zalloc((depth) * sizeof(char *));
675 if (!ptr)
676 goto fail;
677 if (parse_path(ptr, parse_dirname))
678 goto fail;
679 parent_inode = zalloc(sizeof(struct ext2_inode));
680 if (!parent_inode)
681 goto fail;
682 first_inode = zalloc(sizeof(struct ext2_inode));
683 if (!first_inode)
684 goto fail;
685 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
686 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
687 if (flags & F_FILE)
688 result_inode_no = EXT2_ROOT_INO;
689 for (i = 1; i < depth; i++) {
690 matched_inode_no = search_dir(parent_inode, ptr[i]);
691 if (matched_inode_no == -1) {
692 if (ptr[i + 1] == NULL && i == 1) {
693 result_inode_no = EXT2_ROOT_INO;
694 goto end;
695 } else {
696 if (ptr[i + 1] == NULL)
697 break;
698 printf("Invalid path\n");
699 result_inode_no = -1;
700 goto fail;
701 }
702 } else {
703 if (ptr[i + 1] != NULL) {
704 memset(parent_inode, '\0',
705 sizeof(struct ext2_inode));
706 if (ext4fs_iget(matched_inode_no,
707 parent_inode)) {
708 result_inode_no = -1;
709 goto fail;
710 }
711 result_inode_no = matched_inode_no;
712 } else {
713 break;
714 }
715 }
716 }
717
718end:
719 if (i == 1)
720 matched_inode_no = search_dir(first_inode, ptr[i]);
721 else
722 matched_inode_no = search_dir(parent_inode, ptr[i]);
723
724 if (matched_inode_no != -1) {
725 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200726 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankared34f342012-05-25 21:22:49 +0530727 printf("It is a Directory\n");
728 result_inode_no = -1;
729 goto fail;
730 }
731 }
732
733 if (strlen(ptr[i]) > 256) {
734 result_inode_no = -1;
735 goto fail;
736 }
737 memcpy(dname, ptr[i], strlen(ptr[i]));
738
739fail:
740 free(depth_dirname);
741 free(parse_dirname);
Stephen Warren934b14f2015-09-04 22:03:44 -0600742 for (i = 0; i < depth; i++) {
743 if (!ptr[i])
744 break;
745 free(ptr[i]);
746 }
Uma Shankared34f342012-05-25 21:22:49 +0530747 free(ptr);
748 free(parent_inode);
749 free(first_inode);
750
751 return result_inode_no;
752}
753
Stefan Brüns76a29512016-09-06 04:36:41 +0200754static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankared34f342012-05-25 21:22:49 +0530755{
Uma Shankared34f342012-05-25 21:22:49 +0530756 int totalbytes = 0;
757 int templength = 0;
758 int status, inodeno;
759 int found = 0;
760 char *root_first_block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530761 struct ext2_dirent *dir = NULL;
762 struct ext2_dirent *previous_dir = NULL;
763 char *ptr = NULL;
764 struct ext_filesystem *fs = get_fs();
Stephen Warrend56b2012015-09-04 22:03:45 -0600765 int ret = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530766
767 /* get the first block of root */
Uma Shankared34f342012-05-25 21:22:49 +0530768 root_first_block_buffer = zalloc(fs->blksz);
769 if (!root_first_block_buffer)
770 return -ENOMEM;
Stefan Brüns76a29512016-09-06 04:36:41 +0200771 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Uma Shankared34f342012-05-25 21:22:49 +0530772 fs->blksz, root_first_block_buffer);
773 if (status == 0)
774 goto fail;
775
Stefan Brüns76a29512016-09-06 04:36:41 +0200776 if (ext4fs_log_journal(root_first_block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530777 goto fail;
778 dir = (struct ext2_dirent *)root_first_block_buffer;
779 ptr = (char *)dir;
780 totalbytes = 0;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200781 while (le16_to_cpu(dir->direntlen) >= 0) {
Uma Shankared34f342012-05-25 21:22:49 +0530782 /*
783 * blocksize-totalbytes because last
784 * directory length i.e., *dir->direntlen
785 * is free availble space in the block that
786 * means it is a last entry of directory entry
787 */
Stefan Brüns76a29512016-09-06 04:36:41 +0200788 if (dir->inode && (strlen(filename) == dir->namelen) &&
789 (strncmp(ptr + sizeof(struct ext2_dirent),
790 filename, dir->namelen) == 0)) {
791 printf("file found, deleting\n");
792 inodeno = le32_to_cpu(dir->inode);
793 if (previous_dir) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200794 uint16_t new_len;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200795 new_len = le16_to_cpu(previous_dir->direntlen);
796 new_len += le16_to_cpu(dir->direntlen);
797 previous_dir->direntlen = cpu_to_le16(new_len);
Stefan Brüns76a29512016-09-06 04:36:41 +0200798 } else {
Uma Shankared34f342012-05-25 21:22:49 +0530799 dir->inode = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530800 }
Stefan Brüns76a29512016-09-06 04:36:41 +0200801 found = 1;
802 break;
Uma Shankared34f342012-05-25 21:22:49 +0530803 }
804
Michael Walle58a9ecb2016-09-01 11:21:40 +0200805 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
Uma Shankared34f342012-05-25 21:22:49 +0530806 break;
807
808 /* traversing the each directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200809 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530810 totalbytes = totalbytes + templength;
811 previous_dir = dir;
812 dir = (struct ext2_dirent *)((char *)dir + templength);
813 ptr = (char *)dir;
814 }
815
816
817 if (found == 1) {
Stefan Brüns76a29512016-09-06 04:36:41 +0200818 if (ext4fs_put_metadata(root_first_block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530819 goto fail;
Stephen Warrend56b2012015-09-04 22:03:45 -0600820 ret = inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530821 }
822fail:
823 free(root_first_block_buffer);
824
Stephen Warrend56b2012015-09-04 22:03:45 -0600825 return ret;
Uma Shankared34f342012-05-25 21:22:49 +0530826}
827
Stefan Brüns76a29512016-09-06 04:36:41 +0200828int ext4fs_filename_unlink(char *filename)
Uma Shankared34f342012-05-25 21:22:49 +0530829{
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200830 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530831 long int blknr = -1;
832 int inodeno = -1;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200833 uint32_t directory_blocks;
834
835 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
836 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankared34f342012-05-25 21:22:49 +0530837
838 /* read the block no allocated to a file */
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200839 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
840 blknr = read_allocated_block(g_parent_inode, blk_idx);
Uma Shankared34f342012-05-25 21:22:49 +0530841 if (blknr == 0)
842 break;
Stefan Brüns76a29512016-09-06 04:36:41 +0200843 inodeno = unlink_filename(filename, blknr);
Uma Shankared34f342012-05-25 21:22:49 +0530844 if (inodeno != -1)
845 return inodeno;
846 }
847
848 return -1;
849}
850
Michael Walle58a9ecb2016-09-01 11:21:40 +0200851uint32_t ext4fs_get_new_blk_no(void)
Uma Shankared34f342012-05-25 21:22:49 +0530852{
853 short i;
854 short status;
855 int remainder;
856 unsigned int bg_idx;
857 static int prev_bg_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200858 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankared34f342012-05-25 21:22:49 +0530859 struct ext_filesystem *fs = get_fs();
860 char *journal_buffer = zalloc(fs->blksz);
861 char *zero_buffer = zalloc(fs->blksz);
862 if (!journal_buffer || !zero_buffer)
863 goto fail;
Simon Glass73c15c62012-10-03 11:37:49 +0000864 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
Uma Shankared34f342012-05-25 21:22:49 +0530865
866 if (fs->first_pass_bbmap == 0) {
867 for (i = 0; i < fs->no_blkgrp; i++) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200868 if (le16_to_cpu(bgd[i].free_blocks)) {
869 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
870 uint16_t new_flags;
871 put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +0530872 zero_buffer, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200873 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
874 bgd[i].bg_flags = cpu_to_le16(new_flags);
Uma Shankared34f342012-05-25 21:22:49 +0530875 memcpy(fs->blk_bmaps[i], zero_buffer,
876 fs->blksz);
877 }
878 fs->curr_blkno =
879 _get_new_blk_no(fs->blk_bmaps[i]);
880 if (fs->curr_blkno == -1)
881 /* if block bitmap is completely fill */
882 continue;
883 fs->curr_blkno = fs->curr_blkno +
884 (i * fs->blksz * 8);
885 fs->first_pass_bbmap++;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200886 ext4fs_bg_free_blocks_dec(&bgd[i]);
887 ext4fs_sb_free_blocks_dec(fs->sb);
888 status = ext4fs_devread(
889 (lbaint_t)le32_to_cpu(bgd[i].block_id) *
Uma Shankared34f342012-05-25 21:22:49 +0530890 fs->sect_perblk, 0,
891 fs->blksz,
892 journal_buffer);
893 if (status == 0)
894 goto fail;
895 if (ext4fs_log_journal(journal_buffer,
Michael Walle58a9ecb2016-09-01 11:21:40 +0200896 le32_to_cpu(bgd[i].block_id)))
Uma Shankared34f342012-05-25 21:22:49 +0530897 goto fail;
898 goto success;
899 } else {
900 debug("no space left on block group %d\n", i);
901 }
902 }
903
904 goto fail;
905 } else {
906restart:
907 fs->curr_blkno++;
908 /* get the blockbitmap index respective to blockno */
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200909 bg_idx = fs->curr_blkno / blk_per_grp;
910 if (fs->blksz == 1024) {
Uma Shankared34f342012-05-25 21:22:49 +0530911 remainder = fs->curr_blkno % blk_per_grp;
912 if (!remainder)
913 bg_idx--;
914 }
915
916 /*
917 * To skip completely filled block group bitmaps
918 * Optimize the block allocation
919 */
920 if (bg_idx >= fs->no_blkgrp)
921 goto fail;
922
Simon Glass73c15c62012-10-03 11:37:49 +0000923 if (bgd[bg_idx].free_blocks == 0) {
Uma Shankared34f342012-05-25 21:22:49 +0530924 debug("block group %u is full. Skipping\n", bg_idx);
925 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
926 fs->curr_blkno--;
927 goto restart;
928 }
929
Michael Walle58a9ecb2016-09-01 11:21:40 +0200930 if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
931 uint16_t new_flags;
Uma Shankared34f342012-05-25 21:22:49 +0530932 memset(zero_buffer, '\0', fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200933 put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
934 zero_buffer, fs->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530935 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200936 new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
937 bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
Uma Shankared34f342012-05-25 21:22:49 +0530938 }
939
940 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
941 bg_idx) != 0) {
942 debug("going for restart for the block no %ld %u\n",
943 fs->curr_blkno, bg_idx);
944 goto restart;
945 }
946
947 /* journal backup */
948 if (prev_bg_bitmap_index != bg_idx) {
949 memset(journal_buffer, '\0', fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200950 status = ext4fs_devread(
951 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
Uma Shankared34f342012-05-25 21:22:49 +0530952 * fs->sect_perblk,
953 0, fs->blksz, journal_buffer);
954 if (status == 0)
955 goto fail;
956 if (ext4fs_log_journal(journal_buffer,
Michael Walle58a9ecb2016-09-01 11:21:40 +0200957 le32_to_cpu(bgd[bg_idx].block_id)))
Uma Shankared34f342012-05-25 21:22:49 +0530958 goto fail;
959
960 prev_bg_bitmap_index = bg_idx;
961 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200962 ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
963 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +0530964 goto success;
965 }
966success:
967 free(journal_buffer);
968 free(zero_buffer);
969
970 return fs->curr_blkno;
971fail:
972 free(journal_buffer);
973 free(zero_buffer);
974
975 return -1;
976}
977
978int ext4fs_get_new_inode_no(void)
979{
980 short i;
981 short status;
982 unsigned int ibmap_idx;
983 static int prev_inode_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200984 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankared34f342012-05-25 21:22:49 +0530985 struct ext_filesystem *fs = get_fs();
986 char *journal_buffer = zalloc(fs->blksz);
987 char *zero_buffer = zalloc(fs->blksz);
988 if (!journal_buffer || !zero_buffer)
989 goto fail;
Simon Glass73c15c62012-10-03 11:37:49 +0000990 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
Uma Shankared34f342012-05-25 21:22:49 +0530991
992 if (fs->first_pass_ibmap == 0) {
993 for (i = 0; i < fs->no_blkgrp; i++) {
Simon Glass73c15c62012-10-03 11:37:49 +0000994 if (bgd[i].free_inodes) {
995 if (bgd[i].bg_itable_unused !=
996 bgd[i].free_inodes)
997 bgd[i].bg_itable_unused =
998 bgd[i].free_inodes;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200999 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
1000 int new_flags;
1001 put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301002 zero_buffer, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001003 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1004 bgd[i].bg_flags = cpu_to_le16(new_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301005 memcpy(fs->inode_bmaps[i],
1006 zero_buffer, fs->blksz);
1007 }
1008 fs->curr_inode_no =
1009 _get_new_inode_no(fs->inode_bmaps[i]);
1010 if (fs->curr_inode_no == -1)
1011 /* if block bitmap is completely fill */
1012 continue;
1013 fs->curr_inode_no = fs->curr_inode_no +
1014 (i * inodes_per_grp);
1015 fs->first_pass_ibmap++;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001016 ext4fs_bg_free_inodes_dec(&bgd[i]);
1017 ext4fs_bg_itable_unused_dec(&bgd[i]);
1018 ext4fs_sb_free_inodes_dec(fs->sb);
1019 status = ext4fs_devread(
1020 (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
Uma Shankared34f342012-05-25 21:22:49 +05301021 fs->sect_perblk, 0,
1022 fs->blksz,
1023 journal_buffer);
1024 if (status == 0)
1025 goto fail;
1026 if (ext4fs_log_journal(journal_buffer,
Michael Walle58a9ecb2016-09-01 11:21:40 +02001027 le32_to_cpu(bgd[i].inode_id)))
Uma Shankared34f342012-05-25 21:22:49 +05301028 goto fail;
1029 goto success;
1030 } else
1031 debug("no inode left on block group %d\n", i);
1032 }
1033 goto fail;
1034 } else {
1035restart:
1036 fs->curr_inode_no++;
1037 /* get the blockbitmap index respective to blockno */
1038 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001039 if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
1040 int new_flags;
Uma Shankared34f342012-05-25 21:22:49 +05301041 memset(zero_buffer, '\0', fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001042 put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
1043 zero_buffer, fs->blksz);
1044 new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1045 bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301046 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1047 fs->blksz);
1048 }
1049
1050 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1051 fs->inode_bmaps[ibmap_idx],
1052 ibmap_idx) != 0) {
1053 debug("going for restart for the block no %d %u\n",
1054 fs->curr_inode_no, ibmap_idx);
1055 goto restart;
1056 }
1057
1058 /* journal backup */
1059 if (prev_inode_bitmap_index != ibmap_idx) {
1060 memset(journal_buffer, '\0', fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001061 status = ext4fs_devread(
1062 (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
Uma Shankared34f342012-05-25 21:22:49 +05301063 * fs->sect_perblk,
1064 0, fs->blksz, journal_buffer);
1065 if (status == 0)
1066 goto fail;
1067 if (ext4fs_log_journal(journal_buffer,
Michael Walle58a9ecb2016-09-01 11:21:40 +02001068 le32_to_cpu(bgd[ibmap_idx].inode_id)))
Uma Shankared34f342012-05-25 21:22:49 +05301069 goto fail;
1070 prev_inode_bitmap_index = ibmap_idx;
1071 }
Simon Glass73c15c62012-10-03 11:37:49 +00001072 if (bgd[ibmap_idx].bg_itable_unused !=
1073 bgd[ibmap_idx].free_inodes)
1074 bgd[ibmap_idx].bg_itable_unused =
1075 bgd[ibmap_idx].free_inodes;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001076 ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
1077 ext4fs_bg_itable_unused_dec(&bgd[ibmap_idx]);
1078 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301079 goto success;
1080 }
1081
1082success:
1083 free(journal_buffer);
1084 free(zero_buffer);
1085
1086 return fs->curr_inode_no;
1087fail:
1088 free(journal_buffer);
1089 free(zero_buffer);
1090
1091 return -1;
1092
1093}
1094
1095
1096static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1097 unsigned int *total_remaining_blocks,
1098 unsigned int *no_blks_reqd)
1099{
1100 short i;
1101 short status;
1102 long int actual_block_no;
1103 long int si_blockno;
1104 /* si :single indirect */
Michael Walle58a9ecb2016-09-01 11:21:40 +02001105 __le32 *si_buffer = NULL;
1106 __le32 *si_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301107 struct ext_filesystem *fs = get_fs();
1108
1109 if (*total_remaining_blocks != 0) {
1110 si_buffer = zalloc(fs->blksz);
1111 if (!si_buffer) {
1112 printf("No Memory\n");
1113 return;
1114 }
1115 si_start_addr = si_buffer;
1116 si_blockno = ext4fs_get_new_blk_no();
1117 if (si_blockno == -1) {
1118 printf("no block left to assign\n");
1119 goto fail;
1120 }
1121 (*no_blks_reqd)++;
1122 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1123
Frederic Leroy04735e92013-06-26 18:11:25 +02001124 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301125 0, fs->blksz, (char *)si_buffer);
1126 memset(si_buffer, '\0', fs->blksz);
1127 if (status == 0)
1128 goto fail;
1129
1130 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1131 actual_block_no = ext4fs_get_new_blk_no();
1132 if (actual_block_no == -1) {
1133 printf("no block left to assign\n");
1134 goto fail;
1135 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001136 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301137 debug("SIAB %u: %u\n", *si_buffer,
1138 *total_remaining_blocks);
1139
1140 si_buffer++;
1141 (*total_remaining_blocks)--;
1142 if (*total_remaining_blocks == 0)
1143 break;
1144 }
1145
1146 /* write the block to disk */
Ma Haijun05508702014-01-08 08:15:33 +08001147 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301148 si_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001149 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301150 }
1151fail:
1152 free(si_start_addr);
1153}
1154
1155static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1156 unsigned int *total_remaining_blocks,
1157 unsigned int *no_blks_reqd)
1158{
1159 short i;
1160 short j;
1161 short status;
1162 long int actual_block_no;
1163 /* di:double indirect */
1164 long int di_blockno_parent;
1165 long int di_blockno_child;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001166 __le32 *di_parent_buffer = NULL;
1167 __le32 *di_child_buff = NULL;
1168 __le32 *di_block_start_addr = NULL;
1169 __le32 *di_child_buff_start = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301170 struct ext_filesystem *fs = get_fs();
1171
1172 if (*total_remaining_blocks != 0) {
1173 /* double indirect parent block connecting to inode */
1174 di_blockno_parent = ext4fs_get_new_blk_no();
1175 if (di_blockno_parent == -1) {
1176 printf("no block left to assign\n");
1177 goto fail;
1178 }
1179 di_parent_buffer = zalloc(fs->blksz);
1180 if (!di_parent_buffer)
1181 goto fail;
1182
1183 di_block_start_addr = di_parent_buffer;
1184 (*no_blks_reqd)++;
1185 debug("DIPB %ld: %u\n", di_blockno_parent,
1186 *total_remaining_blocks);
1187
Frederic Leroy04735e92013-06-26 18:11:25 +02001188 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankared34f342012-05-25 21:22:49 +05301189 fs->sect_perblk, 0,
1190 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001191
1192 if (!status) {
1193 printf("%s: Device read error!\n", __func__);
1194 goto fail;
1195 }
Uma Shankared34f342012-05-25 21:22:49 +05301196 memset(di_parent_buffer, '\0', fs->blksz);
1197
1198 /*
1199 * start:for each double indirect parent
1200 * block create one more block
1201 */
1202 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1203 di_blockno_child = ext4fs_get_new_blk_no();
1204 if (di_blockno_child == -1) {
1205 printf("no block left to assign\n");
1206 goto fail;
1207 }
1208 di_child_buff = zalloc(fs->blksz);
1209 if (!di_child_buff)
1210 goto fail;
1211
1212 di_child_buff_start = di_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001213 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankared34f342012-05-25 21:22:49 +05301214 di_parent_buffer++;
1215 (*no_blks_reqd)++;
1216 debug("DICB %ld: %u\n", di_blockno_child,
1217 *total_remaining_blocks);
1218
Frederic Leroy04735e92013-06-26 18:11:25 +02001219 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankared34f342012-05-25 21:22:49 +05301220 fs->sect_perblk, 0,
1221 fs->blksz,
1222 (char *)di_child_buff);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001223
1224 if (!status) {
1225 printf("%s: Device read error!\n", __func__);
1226 goto fail;
1227 }
Uma Shankared34f342012-05-25 21:22:49 +05301228 memset(di_child_buff, '\0', fs->blksz);
1229 /* filling of actual datablocks for each child */
1230 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1231 actual_block_no = ext4fs_get_new_blk_no();
1232 if (actual_block_no == -1) {
1233 printf("no block left to assign\n");
1234 goto fail;
1235 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001236 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301237 debug("DIAB %ld: %u\n", actual_block_no,
1238 *total_remaining_blocks);
1239
1240 di_child_buff++;
1241 (*total_remaining_blocks)--;
1242 if (*total_remaining_blocks == 0)
1243 break;
1244 }
1245 /* write the block table */
Ma Haijun05508702014-01-08 08:15:33 +08001246 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301247 di_child_buff_start, fs->blksz);
1248 free(di_child_buff_start);
1249 di_child_buff_start = NULL;
1250
1251 if (*total_remaining_blocks == 0)
1252 break;
1253 }
Ma Haijun05508702014-01-08 08:15:33 +08001254 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301255 di_block_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001256 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankared34f342012-05-25 21:22:49 +05301257 }
1258fail:
1259 free(di_block_start_addr);
1260}
1261
1262static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1263 unsigned int *total_remaining_blocks,
1264 unsigned int *no_blks_reqd)
1265{
1266 short i;
1267 short j;
1268 short k;
1269 long int actual_block_no;
1270 /* ti: Triple Indirect */
1271 long int ti_gp_blockno;
1272 long int ti_parent_blockno;
1273 long int ti_child_blockno;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001274 __le32 *ti_gp_buff = NULL;
1275 __le32 *ti_parent_buff = NULL;
1276 __le32 *ti_child_buff = NULL;
1277 __le32 *ti_gp_buff_start_addr = NULL;
1278 __le32 *ti_pbuff_start_addr = NULL;
1279 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301280 struct ext_filesystem *fs = get_fs();
1281 if (*total_remaining_blocks != 0) {
1282 /* triple indirect grand parent block connecting to inode */
1283 ti_gp_blockno = ext4fs_get_new_blk_no();
1284 if (ti_gp_blockno == -1) {
1285 printf("no block left to assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001286 return;
Uma Shankared34f342012-05-25 21:22:49 +05301287 }
1288 ti_gp_buff = zalloc(fs->blksz);
1289 if (!ti_gp_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001290 return;
Uma Shankared34f342012-05-25 21:22:49 +05301291
1292 ti_gp_buff_start_addr = ti_gp_buff;
1293 (*no_blks_reqd)++;
1294 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1295 *total_remaining_blocks);
1296
1297 /* for each 4 byte grand parent entry create one more block */
1298 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1299 ti_parent_blockno = ext4fs_get_new_blk_no();
1300 if (ti_parent_blockno == -1) {
1301 printf("no block left to assign\n");
1302 goto fail;
1303 }
1304 ti_parent_buff = zalloc(fs->blksz);
1305 if (!ti_parent_buff)
1306 goto fail;
1307
1308 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001309 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301310 ti_gp_buff++;
1311 (*no_blks_reqd)++;
1312 debug("TIPB %ld: %u\n", ti_parent_blockno,
1313 *total_remaining_blocks);
1314
1315 /* for each 4 byte entry parent create one more block */
1316 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1317 ti_child_blockno = ext4fs_get_new_blk_no();
1318 if (ti_child_blockno == -1) {
1319 printf("no block left assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001320 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301321 }
1322 ti_child_buff = zalloc(fs->blksz);
1323 if (!ti_child_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001324 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301325
1326 ti_cbuff_start_addr = ti_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001327 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301328 ti_parent_buff++;
1329 (*no_blks_reqd)++;
1330 debug("TICB %ld: %u\n", ti_parent_blockno,
1331 *total_remaining_blocks);
1332
1333 /* fill actual datablocks for each child */
1334 for (k = 0; k < (fs->blksz / sizeof(int));
1335 k++) {
1336 actual_block_no =
1337 ext4fs_get_new_blk_no();
1338 if (actual_block_no == -1) {
1339 printf("no block left\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001340 free(ti_cbuff_start_addr);
1341 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301342 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001343 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301344 debug("TIAB %ld: %u\n", actual_block_no,
1345 *total_remaining_blocks);
1346
1347 ti_child_buff++;
1348 (*total_remaining_blocks)--;
1349 if (*total_remaining_blocks == 0)
1350 break;
1351 }
1352 /* write the child block */
Ma Haijun05508702014-01-08 08:15:33 +08001353 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1354 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301355 ti_cbuff_start_addr, fs->blksz);
1356 free(ti_cbuff_start_addr);
1357
1358 if (*total_remaining_blocks == 0)
1359 break;
1360 }
1361 /* write the parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001362 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301363 ti_pbuff_start_addr, fs->blksz);
1364 free(ti_pbuff_start_addr);
1365
1366 if (*total_remaining_blocks == 0)
1367 break;
1368 }
1369 /* write the grand parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001370 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301371 ti_gp_buff_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001372 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini495c3a12015-12-10 16:42:21 -05001373 free(ti_gp_buff_start_addr);
1374 return;
Uma Shankared34f342012-05-25 21:22:49 +05301375 }
Tom Rini495c3a12015-12-10 16:42:21 -05001376fail1:
1377 free(ti_pbuff_start_addr);
Uma Shankared34f342012-05-25 21:22:49 +05301378fail:
1379 free(ti_gp_buff_start_addr);
1380}
1381
1382void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1383 unsigned int total_remaining_blocks,
1384 unsigned int *total_no_of_block)
1385{
1386 short i;
1387 long int direct_blockno;
1388 unsigned int no_blks_reqd = 0;
1389
1390 /* allocation of direct blocks */
Stephen Warrend0180282014-06-11 12:46:16 -06001391 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankared34f342012-05-25 21:22:49 +05301392 direct_blockno = ext4fs_get_new_blk_no();
1393 if (direct_blockno == -1) {
1394 printf("no block left to assign\n");
1395 return;
1396 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001397 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301398 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1399
1400 total_remaining_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +05301401 }
1402
1403 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1404 &no_blks_reqd);
1405 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1406 &no_blks_reqd);
1407 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1408 &no_blks_reqd);
1409 *total_no_of_block += no_blks_reqd;
1410}
1411
1412#endif
1413
Uma Shankara1596432012-05-25 21:21:44 +05301414static struct ext4_extent_header *ext4fs_get_extent_block
1415 (struct ext2_data *data, char *buf,
1416 struct ext4_extent_header *ext_block,
1417 uint32_t fileblock, int log2_blksz)
1418{
1419 struct ext4_extent_idx *index;
1420 unsigned long long block;
Ionut Nicu47017322014-01-13 11:59:24 +01001421 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankara1596432012-05-25 21:21:44 +05301422 int i;
1423
1424 while (1) {
1425 index = (struct ext4_extent_idx *)(ext_block + 1);
1426
Rommel Custodio8b415f72013-07-21 10:53:25 +02001427 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001428 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301429
1430 if (ext_block->eh_depth == 0)
1431 return ext_block;
1432 i = -1;
1433 do {
1434 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001435 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301436 break;
Ionut Nicub5bbac12014-01-13 12:00:08 +01001437 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankara1596432012-05-25 21:21:44 +05301438
1439 if (--i < 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001440 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301441
Rommel Custodio8b415f72013-07-21 10:53:25 +02001442 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301443 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1444
Ionut Nicu47017322014-01-13 11:59:24 +01001445 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
Frederic Leroy04735e92013-06-26 18:11:25 +02001446 buf))
Uma Shankara1596432012-05-25 21:21:44 +05301447 ext_block = (struct ext4_extent_header *)buf;
1448 else
Michael Walle58a9ecb2016-09-01 11:21:40 +02001449 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301450 }
1451}
1452
1453static int ext4fs_blockgroup
1454 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1455{
1456 long int blkno;
1457 unsigned int blkoff, desc_per_blk;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001458 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301459
1460 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1461
Michael Walle7f101be2016-08-29 10:46:44 +02001462 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankara1596432012-05-25 21:21:44 +05301463 group / desc_per_blk;
1464 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1465
1466 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1467 group, blkno, blkoff);
1468
Frederic Leroy04735e92013-06-26 18:11:25 +02001469 return ext4fs_devread((lbaint_t)blkno <<
1470 (LOG2_BLOCK_SIZE(data) - log2blksz),
Uma Shankara1596432012-05-25 21:21:44 +05301471 blkoff, sizeof(struct ext2_block_group),
1472 (char *)blkgrp);
1473}
1474
1475int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1476{
1477 struct ext2_block_group blkgrp;
1478 struct ext2_sblock *sblock = &data->sblock;
1479 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00001480 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301481 int inodes_per_block, status;
1482 long int blkno;
1483 unsigned int blkoff;
1484
1485 /* It is easier to calculate if the first inode is 0. */
1486 ino--;
Michael Walle7f101be2016-08-29 10:46:44 +02001487 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301488 (sblock->inodes_per_group), &blkgrp);
1489 if (status == 0)
1490 return 0;
1491
1492 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Michael Walle7f101be2016-08-29 10:46:44 +02001493 blkno = le32_to_cpu(blkgrp.inode_table_id) +
1494 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankara1596432012-05-25 21:21:44 +05301495 blkoff = (ino % inodes_per_block) * fs->inodesz;
1496 /* Read the inode. */
Frederic Leroy04735e92013-06-26 18:11:25 +02001497 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1498 log2blksz), blkoff,
Uma Shankara1596432012-05-25 21:21:44 +05301499 sizeof(struct ext2_inode), (char *)inode);
1500 if (status == 0)
1501 return 0;
1502
1503 return 1;
1504}
1505
1506long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1507{
1508 long int blknr;
1509 int blksz;
1510 int log2_blksz;
1511 int status;
1512 long int rblock;
1513 long int perblock_parent;
1514 long int perblock_child;
1515 unsigned long long start;
1516 /* get the blocksize of the filesystem */
1517 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +00001518 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1519 - get_fs()->dev_desc->log2blksz;
1520
Uma Shankara1596432012-05-25 21:21:44 +05301521 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1522 char *buf = zalloc(blksz);
1523 if (!buf)
1524 return -ENOMEM;
1525 struct ext4_extent_header *ext_block;
1526 struct ext4_extent *extent;
1527 int i = -1;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001528 ext_block =
1529 ext4fs_get_extent_block(ext4fs_root, buf,
1530 (struct ext4_extent_header *)
1531 inode->b.blocks.dir_blocks,
1532 fileblock, log2_blksz);
Uma Shankara1596432012-05-25 21:21:44 +05301533 if (!ext_block) {
1534 printf("invalid extent block\n");
1535 free(buf);
1536 return -EINVAL;
1537 }
1538
1539 extent = (struct ext4_extent *)(ext_block + 1);
1540
1541 do {
1542 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001543 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301544 break;
1545 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1546 if (--i >= 0) {
1547 fileblock -= le32_to_cpu(extent[i].ee_block);
Rommel Custodio8b415f72013-07-21 10:53:25 +02001548 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
Uma Shankara1596432012-05-25 21:21:44 +05301549 free(buf);
1550 return 0;
1551 }
1552
Rommel Custodio8b415f72013-07-21 10:53:25 +02001553 start = le16_to_cpu(extent[i].ee_start_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301554 start = (start << 32) +
1555 le32_to_cpu(extent[i].ee_start_lo);
1556 free(buf);
1557 return fileblock + start;
1558 }
1559
1560 printf("Extent Error\n");
1561 free(buf);
1562 return -1;
1563 }
1564
1565 /* Direct blocks. */
1566 if (fileblock < INDIRECT_BLOCKS)
Michael Walle7f101be2016-08-29 10:46:44 +02001567 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301568
1569 /* Indirect. */
1570 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1571 if (ext4fs_indir1_block == NULL) {
1572 ext4fs_indir1_block = zalloc(blksz);
1573 if (ext4fs_indir1_block == NULL) {
1574 printf("** SI ext2fs read block (indir 1)"
1575 "malloc failed. **\n");
1576 return -1;
1577 }
1578 ext4fs_indir1_size = blksz;
1579 ext4fs_indir1_blkno = -1;
1580 }
1581 if (blksz != ext4fs_indir1_size) {
1582 free(ext4fs_indir1_block);
1583 ext4fs_indir1_block = NULL;
1584 ext4fs_indir1_size = 0;
1585 ext4fs_indir1_blkno = -1;
1586 ext4fs_indir1_block = zalloc(blksz);
1587 if (ext4fs_indir1_block == NULL) {
1588 printf("** SI ext2fs read block (indir 1):"
1589 "malloc failed. **\n");
1590 return -1;
1591 }
1592 ext4fs_indir1_size = blksz;
1593 }
Michael Walle7f101be2016-08-29 10:46:44 +02001594 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301595 log2_blksz) != ext4fs_indir1_blkno) {
1596 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001597 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301598 (inode->b.blocks.
1599 indir_block) << log2_blksz, 0,
1600 blksz, (char *)ext4fs_indir1_block);
1601 if (status == 0) {
1602 printf("** SI ext2fs read block (indir 1)"
1603 "failed. **\n");
1604 return 0;
1605 }
1606 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001607 le32_to_cpu(inode->b.blocks.
Uma Shankara1596432012-05-25 21:21:44 +05301608 indir_block) << log2_blksz;
1609 }
Michael Walle7f101be2016-08-29 10:46:44 +02001610 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankara1596432012-05-25 21:21:44 +05301611 [fileblock - INDIRECT_BLOCKS]);
1612 }
1613 /* Double indirect. */
1614 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1615 (blksz / 4 + 1)))) {
1616
1617 long int perblock = blksz / 4;
1618 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1619
1620 if (ext4fs_indir1_block == NULL) {
1621 ext4fs_indir1_block = zalloc(blksz);
1622 if (ext4fs_indir1_block == NULL) {
1623 printf("** DI ext2fs read block (indir 2 1)"
1624 "malloc failed. **\n");
1625 return -1;
1626 }
1627 ext4fs_indir1_size = blksz;
1628 ext4fs_indir1_blkno = -1;
1629 }
1630 if (blksz != ext4fs_indir1_size) {
1631 free(ext4fs_indir1_block);
1632 ext4fs_indir1_block = NULL;
1633 ext4fs_indir1_size = 0;
1634 ext4fs_indir1_blkno = -1;
1635 ext4fs_indir1_block = zalloc(blksz);
1636 if (ext4fs_indir1_block == NULL) {
1637 printf("** DI ext2fs read block (indir 2 1)"
1638 "malloc failed. **\n");
1639 return -1;
1640 }
1641 ext4fs_indir1_size = blksz;
1642 }
Michael Walle7f101be2016-08-29 10:46:44 +02001643 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301644 log2_blksz) != ext4fs_indir1_blkno) {
1645 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001646 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301647 (inode->b.blocks.
1648 double_indir_block) << log2_blksz,
1649 0, blksz,
1650 (char *)ext4fs_indir1_block);
1651 if (status == 0) {
1652 printf("** DI ext2fs read block (indir 2 1)"
1653 "failed. **\n");
1654 return -1;
1655 }
1656 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001657 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301658 log2_blksz;
1659 }
1660
1661 if (ext4fs_indir2_block == NULL) {
1662 ext4fs_indir2_block = zalloc(blksz);
1663 if (ext4fs_indir2_block == NULL) {
1664 printf("** DI ext2fs read block (indir 2 2)"
1665 "malloc failed. **\n");
1666 return -1;
1667 }
1668 ext4fs_indir2_size = blksz;
1669 ext4fs_indir2_blkno = -1;
1670 }
1671 if (blksz != ext4fs_indir2_size) {
1672 free(ext4fs_indir2_block);
1673 ext4fs_indir2_block = NULL;
1674 ext4fs_indir2_size = 0;
1675 ext4fs_indir2_blkno = -1;
1676 ext4fs_indir2_block = zalloc(blksz);
1677 if (ext4fs_indir2_block == NULL) {
1678 printf("** DI ext2fs read block (indir 2 2)"
1679 "malloc failed. **\n");
1680 return -1;
1681 }
1682 ext4fs_indir2_size = blksz;
1683 }
Michael Walle7f101be2016-08-29 10:46:44 +02001684 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankara1596432012-05-25 21:21:44 +05301685 log2_blksz) != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001686 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301687 (ext4fs_indir1_block
1688 [rblock /
1689 perblock]) << log2_blksz, 0,
1690 blksz,
1691 (char *)ext4fs_indir2_block);
1692 if (status == 0) {
1693 printf("** DI ext2fs read block (indir 2 2)"
1694 "failed. **\n");
1695 return -1;
1696 }
1697 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001698 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301699 /
1700 perblock]) <<
1701 log2_blksz;
1702 }
Michael Walle7f101be2016-08-29 10:46:44 +02001703 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301704 }
1705 /* Tripple indirect. */
1706 else {
1707 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1708 (blksz / 4 * blksz / 4));
1709 perblock_child = blksz / 4;
1710 perblock_parent = ((blksz / 4) * (blksz / 4));
1711
1712 if (ext4fs_indir1_block == NULL) {
1713 ext4fs_indir1_block = zalloc(blksz);
1714 if (ext4fs_indir1_block == NULL) {
1715 printf("** TI ext2fs read block (indir 2 1)"
1716 "malloc failed. **\n");
1717 return -1;
1718 }
1719 ext4fs_indir1_size = blksz;
1720 ext4fs_indir1_blkno = -1;
1721 }
1722 if (blksz != ext4fs_indir1_size) {
1723 free(ext4fs_indir1_block);
1724 ext4fs_indir1_block = NULL;
1725 ext4fs_indir1_size = 0;
1726 ext4fs_indir1_blkno = -1;
1727 ext4fs_indir1_block = zalloc(blksz);
1728 if (ext4fs_indir1_block == NULL) {
1729 printf("** TI ext2fs read block (indir 2 1)"
1730 "malloc failed. **\n");
1731 return -1;
1732 }
1733 ext4fs_indir1_size = blksz;
1734 }
Michael Walle7f101be2016-08-29 10:46:44 +02001735 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301736 log2_blksz) != ext4fs_indir1_blkno) {
1737 status = ext4fs_devread
Frederic Leroy04735e92013-06-26 18:11:25 +02001738 ((lbaint_t)
Michael Walle7f101be2016-08-29 10:46:44 +02001739 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankara1596432012-05-25 21:21:44 +05301740 << log2_blksz, 0, blksz,
1741 (char *)ext4fs_indir1_block);
1742 if (status == 0) {
1743 printf("** TI ext2fs read block (indir 2 1)"
1744 "failed. **\n");
1745 return -1;
1746 }
1747 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001748 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301749 log2_blksz;
1750 }
1751
1752 if (ext4fs_indir2_block == NULL) {
1753 ext4fs_indir2_block = zalloc(blksz);
1754 if (ext4fs_indir2_block == NULL) {
1755 printf("** TI ext2fs read block (indir 2 2)"
1756 "malloc failed. **\n");
1757 return -1;
1758 }
1759 ext4fs_indir2_size = blksz;
1760 ext4fs_indir2_blkno = -1;
1761 }
1762 if (blksz != ext4fs_indir2_size) {
1763 free(ext4fs_indir2_block);
1764 ext4fs_indir2_block = NULL;
1765 ext4fs_indir2_size = 0;
1766 ext4fs_indir2_blkno = -1;
1767 ext4fs_indir2_block = zalloc(blksz);
1768 if (ext4fs_indir2_block == NULL) {
1769 printf("** TI ext2fs read block (indir 2 2)"
1770 "malloc failed. **\n");
1771 return -1;
1772 }
1773 ext4fs_indir2_size = blksz;
1774 }
Michael Walle7f101be2016-08-29 10:46:44 +02001775 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301776 perblock_parent]) <<
1777 log2_blksz)
1778 != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001779 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301780 (ext4fs_indir1_block
1781 [rblock /
1782 perblock_parent]) <<
1783 log2_blksz, 0, blksz,
1784 (char *)ext4fs_indir2_block);
1785 if (status == 0) {
1786 printf("** TI ext2fs read block (indir 2 2)"
1787 "failed. **\n");
1788 return -1;
1789 }
1790 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001791 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301792 perblock_parent])
1793 << log2_blksz;
1794 }
1795
1796 if (ext4fs_indir3_block == NULL) {
1797 ext4fs_indir3_block = zalloc(blksz);
1798 if (ext4fs_indir3_block == NULL) {
1799 printf("** TI ext2fs read block (indir 2 2)"
1800 "malloc failed. **\n");
1801 return -1;
1802 }
1803 ext4fs_indir3_size = blksz;
1804 ext4fs_indir3_blkno = -1;
1805 }
1806 if (blksz != ext4fs_indir3_size) {
1807 free(ext4fs_indir3_block);
1808 ext4fs_indir3_block = NULL;
1809 ext4fs_indir3_size = 0;
1810 ext4fs_indir3_blkno = -1;
1811 ext4fs_indir3_block = zalloc(blksz);
1812 if (ext4fs_indir3_block == NULL) {
1813 printf("** TI ext2fs read block (indir 2 2)"
1814 "malloc failed. **\n");
1815 return -1;
1816 }
1817 ext4fs_indir3_size = blksz;
1818 }
Michael Walle7f101be2016-08-29 10:46:44 +02001819 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301820 /
1821 perblock_child]) <<
1822 log2_blksz) != ext4fs_indir3_blkno) {
1823 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001824 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301825 (ext4fs_indir2_block
1826 [(rblock / perblock_child)
1827 % (blksz / 4)]) << log2_blksz, 0,
1828 blksz, (char *)ext4fs_indir3_block);
1829 if (status == 0) {
1830 printf("** TI ext2fs read block (indir 2 2)"
1831 "failed. **\n");
1832 return -1;
1833 }
1834 ext4fs_indir3_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001835 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301836 perblock_child) %
1837 (blksz /
1838 4)]) <<
1839 log2_blksz;
1840 }
1841
Michael Walle7f101be2016-08-29 10:46:44 +02001842 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankara1596432012-05-25 21:21:44 +05301843 [rblock % perblock_child]);
1844 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001845 debug("read_allocated_block %ld\n", blknr);
Uma Shankara1596432012-05-25 21:21:44 +05301846
1847 return blknr;
1848}
1849
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001850/**
1851 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1852 * global pointers
1853 *
1854 * This function assures that for a file with the same name but different size
1855 * the sequential store on the ext4 filesystem will be correct.
1856 *
1857 * In this function the global data, responsible for internal representation
1858 * of the ext4 data are initialized to the reset state. Without this, during
1859 * replacement of the smaller file with the bigger truncation of new file was
1860 * performed.
1861 */
1862void ext4fs_reinit_global(void)
Uma Shankara1596432012-05-25 21:21:44 +05301863{
Uma Shankara1596432012-05-25 21:21:44 +05301864 if (ext4fs_indir1_block != NULL) {
1865 free(ext4fs_indir1_block);
1866 ext4fs_indir1_block = NULL;
1867 ext4fs_indir1_size = 0;
1868 ext4fs_indir1_blkno = -1;
1869 }
1870 if (ext4fs_indir2_block != NULL) {
1871 free(ext4fs_indir2_block);
1872 ext4fs_indir2_block = NULL;
1873 ext4fs_indir2_size = 0;
1874 ext4fs_indir2_blkno = -1;
1875 }
1876 if (ext4fs_indir3_block != NULL) {
1877 free(ext4fs_indir3_block);
1878 ext4fs_indir3_block = NULL;
1879 ext4fs_indir3_size = 0;
1880 ext4fs_indir3_blkno = -1;
1881 }
1882}
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001883void ext4fs_close(void)
1884{
1885 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1886 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1887 ext4fs_file = NULL;
1888 }
1889 if (ext4fs_root != NULL) {
1890 free(ext4fs_root);
1891 ext4fs_root = NULL;
1892 }
1893
1894 ext4fs_reinit_global();
1895}
Uma Shankara1596432012-05-25 21:21:44 +05301896
1897int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1898 struct ext2fs_node **fnode, int *ftype)
1899{
1900 unsigned int fpos = 0;
1901 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001902 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05301903 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1904
1905#ifdef DEBUG
1906 if (name != NULL)
1907 printf("Iterate dir %s\n", name);
1908#endif /* of DEBUG */
1909 if (!diro->inode_read) {
1910 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1911 if (status == 0)
1912 return 0;
1913 }
1914 /* Search the file. */
Michael Walle7f101be2016-08-29 10:46:44 +02001915 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankara1596432012-05-25 21:21:44 +05301916 struct ext2_dirent dirent;
1917
1918 status = ext4fs_read_file(diro, fpos,
1919 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001920 (char *)&dirent, &actread);
1921 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05301922 return 0;
1923
Thomas Fitzsimmons54d68e92015-11-18 12:42:53 -05001924 if (dirent.direntlen == 0) {
1925 printf("Failed to iterate over directory %s\n", name);
1926 return 0;
1927 }
1928
Uma Shankara1596432012-05-25 21:21:44 +05301929 if (dirent.namelen != 0) {
1930 char filename[dirent.namelen + 1];
1931 struct ext2fs_node *fdiro;
1932 int type = FILETYPE_UNKNOWN;
1933
1934 status = ext4fs_read_file(diro,
1935 fpos +
1936 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001937 dirent.namelen, filename,
1938 &actread);
1939 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05301940 return 0;
1941
1942 fdiro = zalloc(sizeof(struct ext2fs_node));
1943 if (!fdiro)
1944 return 0;
1945
1946 fdiro->data = diro->data;
Michael Walle7f101be2016-08-29 10:46:44 +02001947 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankara1596432012-05-25 21:21:44 +05301948
1949 filename[dirent.namelen] = '\0';
1950
1951 if (dirent.filetype != FILETYPE_UNKNOWN) {
1952 fdiro->inode_read = 0;
1953
1954 if (dirent.filetype == FILETYPE_DIRECTORY)
1955 type = FILETYPE_DIRECTORY;
1956 else if (dirent.filetype == FILETYPE_SYMLINK)
1957 type = FILETYPE_SYMLINK;
1958 else if (dirent.filetype == FILETYPE_REG)
1959 type = FILETYPE_REG;
1960 } else {
1961 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02001962 le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301963 (dirent.inode),
1964 &fdiro->inode);
1965 if (status == 0) {
1966 free(fdiro);
1967 return 0;
1968 }
1969 fdiro->inode_read = 1;
1970
Michael Walle7f101be2016-08-29 10:46:44 +02001971 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankara1596432012-05-25 21:21:44 +05301972 FILETYPE_INO_MASK) ==
1973 FILETYPE_INO_DIRECTORY) {
1974 type = FILETYPE_DIRECTORY;
Michael Walle7f101be2016-08-29 10:46:44 +02001975 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05301976 & FILETYPE_INO_MASK) ==
1977 FILETYPE_INO_SYMLINK) {
1978 type = FILETYPE_SYMLINK;
Michael Walle7f101be2016-08-29 10:46:44 +02001979 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05301980 & FILETYPE_INO_MASK) ==
1981 FILETYPE_INO_REG) {
1982 type = FILETYPE_REG;
1983 }
1984 }
1985#ifdef DEBUG
1986 printf("iterate >%s<\n", filename);
1987#endif /* of DEBUG */
1988 if ((name != NULL) && (fnode != NULL)
1989 && (ftype != NULL)) {
1990 if (strcmp(filename, name) == 0) {
1991 *ftype = type;
1992 *fnode = fdiro;
1993 return 1;
1994 }
1995 } else {
1996 if (fdiro->inode_read == 0) {
1997 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02001998 le32_to_cpu(
Uma Shankara1596432012-05-25 21:21:44 +05301999 dirent.inode),
2000 &fdiro->inode);
2001 if (status == 0) {
2002 free(fdiro);
2003 return 0;
2004 }
2005 fdiro->inode_read = 1;
2006 }
2007 switch (type) {
2008 case FILETYPE_DIRECTORY:
2009 printf("<DIR> ");
2010 break;
2011 case FILETYPE_SYMLINK:
2012 printf("<SYM> ");
2013 break;
2014 case FILETYPE_REG:
2015 printf(" ");
2016 break;
2017 default:
2018 printf("< ? > ");
2019 break;
2020 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002021 printf("%10u %s\n",
Michael Walle7f101be2016-08-29 10:46:44 +02002022 le32_to_cpu(fdiro->inode.size),
Uma Shankara1596432012-05-25 21:21:44 +05302023 filename);
2024 }
2025 free(fdiro);
2026 }
Michael Walle7f101be2016-08-29 10:46:44 +02002027 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankara1596432012-05-25 21:21:44 +05302028 }
2029 return 0;
2030}
2031
2032static char *ext4fs_read_symlink(struct ext2fs_node *node)
2033{
2034 char *symlink;
2035 struct ext2fs_node *diro = node;
2036 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002037 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302038
2039 if (!diro->inode_read) {
2040 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2041 if (status == 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002042 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302043 }
Michael Walle7f101be2016-08-29 10:46:44 +02002044 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankara1596432012-05-25 21:21:44 +05302045 if (!symlink)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002046 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302047
Michael Walle7f101be2016-08-29 10:46:44 +02002048 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankara1596432012-05-25 21:21:44 +05302049 strncpy(symlink, diro->inode.b.symlink,
Michael Walle7f101be2016-08-29 10:46:44 +02002050 le32_to_cpu(diro->inode.size));
Uma Shankara1596432012-05-25 21:21:44 +05302051 } else {
2052 status = ext4fs_read_file(diro, 0,
Michael Walle7f101be2016-08-29 10:46:44 +02002053 le32_to_cpu(diro->inode.size),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002054 symlink, &actread);
Gary Bisson9d2f6a92015-09-07 11:20:07 +02002055 if ((status < 0) || (actread == 0)) {
Uma Shankara1596432012-05-25 21:21:44 +05302056 free(symlink);
Michael Walle58a9ecb2016-09-01 11:21:40 +02002057 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302058 }
2059 }
Michael Walle7f101be2016-08-29 10:46:44 +02002060 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankara1596432012-05-25 21:21:44 +05302061 return symlink;
2062}
2063
2064static int ext4fs_find_file1(const char *currpath,
2065 struct ext2fs_node *currroot,
2066 struct ext2fs_node **currfound, int *foundtype)
2067{
2068 char fpath[strlen(currpath) + 1];
2069 char *name = fpath;
2070 char *next;
2071 int status;
2072 int type = FILETYPE_DIRECTORY;
2073 struct ext2fs_node *currnode = currroot;
2074 struct ext2fs_node *oldnode = currroot;
2075
2076 strncpy(fpath, currpath, strlen(currpath) + 1);
2077
2078 /* Remove all leading slashes. */
2079 while (*name == '/')
2080 name++;
2081
2082 if (!*name) {
2083 *currfound = currnode;
2084 return 1;
2085 }
2086
2087 for (;;) {
2088 int found;
2089
2090 /* Extract the actual part from the pathname. */
2091 next = strchr(name, '/');
2092 if (next) {
2093 /* Remove all leading slashes. */
2094 while (*next == '/')
2095 *(next++) = '\0';
2096 }
2097
2098 if (type != FILETYPE_DIRECTORY) {
2099 ext4fs_free_node(currnode, currroot);
2100 return 0;
2101 }
2102
2103 oldnode = currnode;
2104
2105 /* Iterate over the directory. */
2106 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2107 if (found == 0)
2108 return 0;
2109
2110 if (found == -1)
2111 break;
2112
2113 /* Read in the symlink and follow it. */
2114 if (type == FILETYPE_SYMLINK) {
2115 char *symlink;
2116
2117 /* Test if the symlink does not loop. */
2118 if (++symlinknest == 8) {
2119 ext4fs_free_node(currnode, currroot);
2120 ext4fs_free_node(oldnode, currroot);
2121 return 0;
2122 }
2123
2124 symlink = ext4fs_read_symlink(currnode);
2125 ext4fs_free_node(currnode, currroot);
2126
2127 if (!symlink) {
2128 ext4fs_free_node(oldnode, currroot);
2129 return 0;
2130 }
2131
2132 debug("Got symlink >%s<\n", symlink);
2133
2134 if (symlink[0] == '/') {
2135 ext4fs_free_node(oldnode, currroot);
2136 oldnode = &ext4fs_root->diropen;
2137 }
2138
2139 /* Lookup the node the symlink points to. */
2140 status = ext4fs_find_file1(symlink, oldnode,
2141 &currnode, &type);
2142
2143 free(symlink);
2144
2145 if (status == 0) {
2146 ext4fs_free_node(oldnode, currroot);
2147 return 0;
2148 }
2149 }
2150
2151 ext4fs_free_node(oldnode, currroot);
2152
2153 /* Found the node! */
2154 if (!next || *next == '\0') {
2155 *currfound = currnode;
2156 *foundtype = type;
2157 return 1;
2158 }
2159 name = next;
2160 }
2161 return -1;
2162}
2163
2164int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2165 struct ext2fs_node **foundnode, int expecttype)
2166{
2167 int status;
2168 int foundtype = FILETYPE_DIRECTORY;
2169
2170 symlinknest = 0;
2171 if (!path)
2172 return 0;
2173
2174 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2175 if (status == 0)
2176 return 0;
2177
2178 /* Check if the node that was found was of the expected type. */
2179 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2180 return 0;
2181 else if ((expecttype == FILETYPE_DIRECTORY)
2182 && (foundtype != expecttype))
2183 return 0;
2184
2185 return 1;
2186}
2187
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002188int ext4fs_open(const char *filename, loff_t *len)
Uma Shankara1596432012-05-25 21:21:44 +05302189{
2190 struct ext2fs_node *fdiro = NULL;
2191 int status;
Uma Shankara1596432012-05-25 21:21:44 +05302192
2193 if (ext4fs_root == NULL)
2194 return -1;
2195
2196 ext4fs_file = NULL;
2197 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2198 FILETYPE_REG);
2199 if (status == 0)
2200 goto fail;
2201
2202 if (!fdiro->inode_read) {
2203 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2204 &fdiro->inode);
2205 if (status == 0)
2206 goto fail;
2207 }
Michael Walle7f101be2016-08-29 10:46:44 +02002208 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankara1596432012-05-25 21:21:44 +05302209 ext4fs_file = fdiro;
2210
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002211 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05302212fail:
2213 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2214
2215 return -1;
2216}
2217
2218int ext4fs_mount(unsigned part_length)
2219{
2220 struct ext2_data *data;
2221 int status;
2222 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00002223 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankara1596432012-05-25 21:21:44 +05302224 if (!data)
2225 return 0;
2226
2227 /* Read the superblock. */
Egbert Eich50ce4c02013-05-01 01:13:19 +00002228 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankara1596432012-05-25 21:21:44 +05302229
2230 if (status == 0)
2231 goto fail;
2232
2233 /* Make sure this is an ext2 filesystem. */
Michael Walle7f101be2016-08-29 10:46:44 +02002234 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Uma Shankara1596432012-05-25 21:21:44 +05302235 goto fail;
2236
Tom Rini6f94ab62016-07-22 17:59:11 -04002237 /*
2238 * The 64bit feature was enabled when metadata_csum was enabled
2239 * and we do not support metadata_csum (and cannot reliably find
2240 * files when it is set. Refuse to mount.
2241 */
Michael Walle58a9ecb2016-09-01 11:21:40 +02002242 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
Tom Rini6f94ab62016-07-22 17:59:11 -04002243 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2244 goto fail;
2245 }
2246
Michael Walle011bc332016-08-29 10:46:46 +02002247 if (le32_to_cpu(data->sblock.revision_level) == 0)
Uma Shankara1596432012-05-25 21:21:44 +05302248 fs->inodesz = 128;
2249 else
Michael Walle7f101be2016-08-29 10:46:44 +02002250 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
Uma Shankara1596432012-05-25 21:21:44 +05302251
2252 debug("EXT2 rev %d, inode_size %d\n",
Michael Walle7f101be2016-08-29 10:46:44 +02002253 le32_to_cpu(data->sblock.revision_level), fs->inodesz);
Uma Shankara1596432012-05-25 21:21:44 +05302254
2255 data->diropen.data = data;
2256 data->diropen.ino = 2;
2257 data->diropen.inode_read = 1;
2258 data->inode = &data->diropen.inode;
2259
2260 status = ext4fs_read_inode(data, 2, data->inode);
2261 if (status == 0)
2262 goto fail;
2263
2264 ext4fs_root = data;
2265
2266 return 1;
2267fail:
2268 printf("Failed to mount ext2 filesystem...\n");
2269 free(data);
2270 ext4fs_root = NULL;
2271
2272 return 0;
2273}