blob: 3937e978e179d0840fb959b902c6df49ad953a0c [file] [log] [blame]
Uma Shankar71014b62012-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 Shankara74a99a2012-05-25 21:22:49 +053017 * ext4write : Based on generic ext4 protocol.
18 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020019 * SPDX-License-Identifier: GPL-2.0+
Uma Shankar71014b62012-05-25 21:21:44 +053020 */
21
22#include <common.h>
23#include <ext_common.h>
24#include <ext4fs.h>
Simon Glassb3384f82014-10-15 04:38:32 -060025#include <inttypes.h>
Uma Shankar71014b62012-05-25 21:21:44 +053026#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060027#include <memalign.h>
Uma Shankar71014b62012-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 Walle13179c22016-09-01 11:21:40 +020036__le32 *ext4fs_indir1_block;
Uma Shankar71014b62012-05-25 21:21:44 +053037int ext4fs_indir1_size;
38int ext4fs_indir1_blkno = -1;
Michael Walle13179c22016-09-01 11:21:40 +020039__le32 *ext4fs_indir2_block;
Uma Shankar71014b62012-05-25 21:21:44 +053040int ext4fs_indir2_size;
41int ext4fs_indir2_blkno = -1;
42
Michael Walle13179c22016-09-01 11:21:40 +020043__le32 *ext4fs_indir3_block;
Uma Shankar71014b62012-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 Warren4f8662d2012-10-22 06:43:50 +000049#if defined(CONFIG_EXT4_WRITE)
Michael Walle13179c22016-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 Shankara74a99a2012-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 Shankara74a99a2012-05-25 21:22:49 +053089 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-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 Shankara74a99a2012-05-25 21:22:49 +053092
Egbert Eich7b1b2552013-05-01 01:13:19 +000093 startblock = off >> log2blksz;
Uma Shankara74a99a2012-05-25 21:22:49 +053094 startblock += part_offset;
Egbert Eich7b1b2552013-05-01 01:13:19 +000095 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankara74a99a2012-05-25 21:22:49 +053096
97 if (fs->dev_desc == NULL)
98 return;
99
Egbert Eich7b1b2552013-05-01 01:13:19 +0000100 if ((startblock + (size >> log2blksz)) >
Uma Shankara74a99a2012-05-25 21:22:49 +0530101 (part_offset + fs->total_sect)) {
Frederic Leroye7ee0282013-06-26 18:11:25 +0200102 printf("part_offset is " LBAFU "\n", part_offset);
Simon Glassb3384f82014-10-15 04:38:32 -0600103 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
Uma Shankara74a99a2012-05-25 21:22:49 +0530104 printf("error: overflow occurs\n");
105 return;
106 }
107
108 if (remainder) {
Simon Glass2ee8ada2016-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 Shankara74a99a2012-05-25 21:22:49 +0530113 } else {
Egbert Eich7b1b2552013-05-01 01:13:19 +0000114 if (size >> log2blksz != 0) {
Simon Glass2ee8ada2016-02-29 15:25:52 -0700115 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
116 (unsigned long *)buf);
Uma Shankara74a99a2012-05-25 21:22:49 +0530117 } else {
Simon Glass2ee8ada2016-02-29 15:25:52 -0700118 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankara74a99a2012-05-25 21:22:49 +0530119 temp_ptr = sec_buf;
120 memcpy(temp_ptr, buf, size);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700121 blk_dwrite(fs->dev_desc, startblock, 1,
122 (unsigned long *)sec_buf);
Uma Shankara74a99a2012-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 Walle13179c22016-09-01 11:21:40 +0200140 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankara74a99a2012-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 Walle13179c22016-09-01 11:21:40 +0200277 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankara74a99a2012-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 Walle13179c22016-09-01 11:21:40 +0200302 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankara74a99a2012-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 Walle13179c22016-09-01 11:21:40 +0200317uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankara74a99a2012-05-25 21:22:49 +0530318{
319 struct ext2_block_group *desc;
320 struct ext_filesystem *fs = get_fs();
Michael Walle13179c22016-09-01 11:21:40 +0200321 uint16_t crc = 0;
322 __le32 le32_i = cpu_to_le32(i);
Uma Shankara74a99a2012-05-25 21:22:49 +0530323
Simon Glassea8e5192012-10-03 11:37:49 +0000324 desc = (struct ext2_block_group *)&fs->bgd[i];
Michael Walle13179c22016-09-01 11:21:40 +0200325 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankara74a99a2012-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 Walle13179c22016-09-01 11:21:40 +0200330 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
Uma Shankara74a99a2012-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 Walle13179c22016-09-01 11:21:40 +0200351 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankara74a99a2012-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 Walle13179c22016-09-01 11:21:40 +0200362 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankara74a99a2012-05-25 21:22:49 +0530363 return sizeof_void_space;
364 }
365
366 return 0;
367}
368
Stefan Brünsc1020682016-09-06 04:36:42 +0200369int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankara74a99a2012-05-25 21:22:49 +0530370{
371 unsigned int *zero_buffer = NULL;
372 char *root_first_block_buffer = NULL;
Stefan Brünsf5772812016-09-06 04:36:44 +0200373 int blk_idx;
Uma Shankara74a99a2012-05-25 21:22:49 +0530374 long int first_block_no_of_root = 0;
Uma Shankara74a99a2012-05-25 21:22:49 +0530375 int totalbytes = 0;
Uma Shankara74a99a2012-05-25 21:22:49 +0530376 unsigned int new_entry_byte_reqd;
Uma Shankara74a99a2012-05-25 21:22:49 +0530377 int sizeof_void_space = 0;
378 int templength = 0;
Stefan Brünsc1020682016-09-06 04:36:42 +0200379 int inodeno = -1;
Uma Shankara74a99a2012-05-25 21:22:49 +0530380 int status;
381 struct ext_filesystem *fs = get_fs();
382 /* directory entry */
383 struct ext2_dirent *dir;
Uma Shankara74a99a2012-05-25 21:22:49 +0530384 char *temp_dir = NULL;
Michael Walle13179c22016-09-01 11:21:40 +0200385 uint32_t new_blk_no;
386 uint32_t new_size;
387 uint32_t new_blockcnt;
Stefan Brünsf5772812016-09-06 04:36:44 +0200388 uint32_t directory_blocks;
Uma Shankara74a99a2012-05-25 21:22:49 +0530389
390 zero_buffer = zalloc(fs->blksz);
391 if (!zero_buffer) {
392 printf("No Memory\n");
Stefan Brünsc1020682016-09-06 04:36:42 +0200393 return -1;
Uma Shankara74a99a2012-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ünsc1020682016-09-06 04:36:42 +0200399 return -1;
Uma Shankara74a99a2012-05-25 21:22:49 +0530400 }
Stefan Brünsf5772812016-09-06 04:36:44 +0200401 new_entry_byte_reqd = ROUND(strlen(filename) +
402 sizeof(struct ext2_dirent), 4);
Uma Shankara74a99a2012-05-25 21:22:49 +0530403restart:
Stefan Brünsf5772812016-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 Shankara74a99a2012-05-25 21:22:49 +0530407
Stefan Brünsf5772812016-09-06 04:36:44 +0200408restart_read:
Uma Shankara74a99a2012-05-25 21:22:49 +0530409 /* read the block no allocated to a file */
Stefan Brünsf5772812016-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 Shankara74a99a2012-05-25 21:22:49 +0530413
Frederic Leroye7ee0282013-06-26 18:11:25 +0200414 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankara74a99a2012-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 Shankara74a99a2012-05-25 21:22:49 +0530423 totalbytes = 0;
Stefan Brünsf5772812016-09-06 04:36:44 +0200424
Michael Walle13179c22016-09-01 11:21:40 +0200425 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsf5772812016-09-06 04:36:44 +0200426 unsigned short used_len = ROUND(dir->namelen +
427 sizeof(struct ext2_dirent), 4);
Uma Shankara74a99a2012-05-25 21:22:49 +0530428
Stefan Brünsf5772812016-09-06 04:36:44 +0200429 /* last entry of block */
Michael Walle13179c22016-09-01 11:21:40 +0200430 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530431
Stefan Brünsf5772812016-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 Shankara74a99a2012-05-25 21:22:49 +0530444
Stefan Brüns2bef6892016-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ünsf5772812016-09-06 04:36:44 +0200450 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530451 printf("Directory exceeds limit\n");
452 goto fail;
453 }
Michael Walle13179c22016-09-01 11:21:40 +0200454 new_blk_no = ext4fs_get_new_blk_no();
455 if (new_blk_no == -1) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530456 printf("no block left to assign\n");
457 goto fail;
458 }
Michael Walle13179c22016-09-01 11:21:40 +0200459 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsf5772812016-09-06 04:36:44 +0200460 g_parent_inode->b.blocks.
461 dir_blocks[directory_blocks] =
Michael Walle13179c22016-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 Shankara74a99a2012-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 Shankara74a99a2012-05-25 21:22:49 +0530478 }
479
Michael Walle13179c22016-09-01 11:21:40 +0200480 templength = le16_to_cpu(dir->direntlen);
Uma Shankara74a99a2012-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 Shankara74a99a2012-05-25 21:22:49 +0530487 }
488
489 /* make a pointer ready for creating next directory entry */
Michael Walle13179c22016-09-01 11:21:40 +0200490 templength = le16_to_cpu(dir->direntlen);
Uma Shankara74a99a2012-05-25 21:22:49 +0530491 totalbytes = totalbytes + templength;
492 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankara74a99a2012-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 Walle13179c22016-09-01 11:21:40 +0200500 dir->inode = cpu_to_le32(inodeno);
Uma Shankara74a99a2012-05-25 21:22:49 +0530501 if (sizeof_void_space)
Michael Walle13179c22016-09-01 11:21:40 +0200502 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankara74a99a2012-05-25 21:22:49 +0530503 else
Michael Walle13179c22016-09-01 11:21:40 +0200504 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankara74a99a2012-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 Shankara74a99a2012-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ünsc1020682016-09-06 04:36:42 +0200520
521 return inodeno;
Uma Shankara74a99a2012-05-25 21:22:49 +0530522}
523
524static int search_dir(struct ext2_inode *parent_inode, char *dirname)
525{
526 int status;
Stefan Brüns278f5d32016-09-06 04:36:41 +0200527 int inodeno = 0;
Uma Shankara74a99a2012-05-25 21:22:49 +0530528 int totalbytes;
529 int templength;
530 int direct_blk_idx;
531 long int blknr;
Uma Shankara74a99a2012-05-25 21:22:49 +0530532 char *ptr = NULL;
533 unsigned char *block_buffer = NULL;
534 struct ext2_dirent *dir = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +0530535 struct ext_filesystem *fs = get_fs();
536
537 /* read the block no allocated to a file */
538 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
539 direct_blk_idx++) {
540 blknr = read_allocated_block(parent_inode, direct_blk_idx);
541 if (blknr == 0)
542 goto fail;
543
Stefan Brüns278f5d32016-09-06 04:36:41 +0200544 /* read the blocks of parent inode */
Uma Shankara74a99a2012-05-25 21:22:49 +0530545 block_buffer = zalloc(fs->blksz);
546 if (!block_buffer)
547 goto fail;
548
Frederic Leroye7ee0282013-06-26 18:11:25 +0200549 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankara74a99a2012-05-25 21:22:49 +0530550 0, fs->blksz, (char *)block_buffer);
551 if (status == 0)
552 goto fail;
553
554 dir = (struct ext2_dirent *)block_buffer;
555 ptr = (char *)dir;
556 totalbytes = 0;
Michael Walle13179c22016-09-01 11:21:40 +0200557 while (le16_to_cpu(dir->direntlen) >= 0) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530558 /*
559 * blocksize-totalbytes because last directory
560 * length i.e.,*dir->direntlen is free availble
561 * space in the block that means
562 * it is a last entry of directory entry
563 */
Stefan Brüns278f5d32016-09-06 04:36:41 +0200564 if (dir->inode && (strlen(dirname) == dir->namelen)) {
Michael Walle13179c22016-09-01 11:21:40 +0200565 if (strncmp(dirname, ptr + sizeof(struct ext2_dirent), dir->namelen) == 0) {
Michael Walle13179c22016-09-01 11:21:40 +0200566 inodeno = le32_to_cpu(dir->inode);
Uma Shankara74a99a2012-05-25 21:22:49 +0530567 break;
568 }
569 }
570
Michael Walle13179c22016-09-01 11:21:40 +0200571 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
Uma Shankara74a99a2012-05-25 21:22:49 +0530572 break;
573
574 /* traversing the each directory entry */
Michael Walle13179c22016-09-01 11:21:40 +0200575 templength = le16_to_cpu(dir->direntlen);
Uma Shankara74a99a2012-05-25 21:22:49 +0530576 totalbytes = totalbytes + templength;
Uma Shankara74a99a2012-05-25 21:22:49 +0530577 dir = (struct ext2_dirent *)((char *)dir + templength);
578 ptr = (char *)dir;
579 }
580
Uma Shankara74a99a2012-05-25 21:22:49 +0530581 free(block_buffer);
582 block_buffer = NULL;
Stefan Brüns278f5d32016-09-06 04:36:41 +0200583
584 if (inodeno > 0)
585 return inodeno;
Uma Shankara74a99a2012-05-25 21:22:49 +0530586 }
587
588fail:
589 free(block_buffer);
590
591 return -1;
592}
593
594static int find_dir_depth(char *dirname)
595{
596 char *token = strtok(dirname, "/");
597 int count = 0;
598 while (token != NULL) {
599 token = strtok(NULL, "/");
600 count++;
601 }
602 return count + 1 + 1;
603 /*
604 * for example for string /home/temp
605 * depth=home(1)+temp(1)+1 extra for NULL;
606 * so count is 4;
607 */
608}
609
610static int parse_path(char **arr, char *dirname)
611{
612 char *token = strtok(dirname, "/");
613 int i = 0;
614
615 /* add root */
616 arr[i] = zalloc(strlen("/") + 1);
617 if (!arr[i])
618 return -ENOMEM;
Stephen Warren5dd30f72015-09-04 22:03:44 -0600619 memcpy(arr[i++], "/", strlen("/"));
Uma Shankara74a99a2012-05-25 21:22:49 +0530620
621 /* add each path entry after root */
622 while (token != NULL) {
623 arr[i] = zalloc(strlen(token) + 1);
624 if (!arr[i])
625 return -ENOMEM;
626 memcpy(arr[i++], token, strlen(token));
627 token = strtok(NULL, "/");
628 }
629 arr[i] = NULL;
630
631 return 0;
632}
633
634int ext4fs_iget(int inode_no, struct ext2_inode *inode)
635{
636 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
637 return -1;
638
639 return 0;
640}
641
642/*
643 * Function: ext4fs_get_parent_inode_num
644 * Return Value: inode Number of the parent directory of file/Directory to be
645 * created
646 * dirname : Input parmater, input path name of the file/directory to be created
647 * dname : Output parameter, to be filled with the name of the directory
648 * extracted from dirname
649 */
650int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
651{
652 int i;
653 int depth = 0;
654 int matched_inode_no;
655 int result_inode_no = -1;
656 char **ptr = NULL;
657 char *depth_dirname = NULL;
658 char *parse_dirname = NULL;
659 struct ext2_inode *parent_inode = NULL;
660 struct ext2_inode *first_inode = NULL;
661 struct ext2_inode temp_inode;
662
663 if (*dirname != '/') {
664 printf("Please supply Absolute path\n");
665 return -1;
666 }
667
668 /* TODO: input validation make equivalent to linux */
669 depth_dirname = zalloc(strlen(dirname) + 1);
670 if (!depth_dirname)
671 return -ENOMEM;
672
673 memcpy(depth_dirname, dirname, strlen(dirname));
674 depth = find_dir_depth(depth_dirname);
675 parse_dirname = zalloc(strlen(dirname) + 1);
676 if (!parse_dirname)
677 goto fail;
678 memcpy(parse_dirname, dirname, strlen(dirname));
679
680 /* allocate memory for each directory level */
681 ptr = zalloc((depth) * sizeof(char *));
682 if (!ptr)
683 goto fail;
684 if (parse_path(ptr, parse_dirname))
685 goto fail;
686 parent_inode = zalloc(sizeof(struct ext2_inode));
687 if (!parent_inode)
688 goto fail;
689 first_inode = zalloc(sizeof(struct ext2_inode));
690 if (!first_inode)
691 goto fail;
692 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
693 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
694 if (flags & F_FILE)
695 result_inode_no = EXT2_ROOT_INO;
696 for (i = 1; i < depth; i++) {
697 matched_inode_no = search_dir(parent_inode, ptr[i]);
698 if (matched_inode_no == -1) {
699 if (ptr[i + 1] == NULL && i == 1) {
700 result_inode_no = EXT2_ROOT_INO;
701 goto end;
702 } else {
703 if (ptr[i + 1] == NULL)
704 break;
705 printf("Invalid path\n");
706 result_inode_no = -1;
707 goto fail;
708 }
709 } else {
710 if (ptr[i + 1] != NULL) {
711 memset(parent_inode, '\0',
712 sizeof(struct ext2_inode));
713 if (ext4fs_iget(matched_inode_no,
714 parent_inode)) {
715 result_inode_no = -1;
716 goto fail;
717 }
718 result_inode_no = matched_inode_no;
719 } else {
720 break;
721 }
722 }
723 }
724
725end:
726 if (i == 1)
727 matched_inode_no = search_dir(first_inode, ptr[i]);
728 else
729 matched_inode_no = search_dir(parent_inode, ptr[i]);
730
731 if (matched_inode_no != -1) {
732 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle13179c22016-09-01 11:21:40 +0200733 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530734 printf("It is a Directory\n");
735 result_inode_no = -1;
736 goto fail;
737 }
738 }
739
740 if (strlen(ptr[i]) > 256) {
741 result_inode_no = -1;
742 goto fail;
743 }
744 memcpy(dname, ptr[i], strlen(ptr[i]));
745
746fail:
747 free(depth_dirname);
748 free(parse_dirname);
Stephen Warren5dd30f72015-09-04 22:03:44 -0600749 for (i = 0; i < depth; i++) {
750 if (!ptr[i])
751 break;
752 free(ptr[i]);
753 }
Uma Shankara74a99a2012-05-25 21:22:49 +0530754 free(ptr);
755 free(parent_inode);
756 free(first_inode);
757
758 return result_inode_no;
759}
760
Stefan Brüns278f5d32016-09-06 04:36:41 +0200761static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankara74a99a2012-05-25 21:22:49 +0530762{
Uma Shankara74a99a2012-05-25 21:22:49 +0530763 int totalbytes = 0;
764 int templength = 0;
765 int status, inodeno;
766 int found = 0;
767 char *root_first_block_buffer = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +0530768 struct ext2_dirent *dir = NULL;
769 struct ext2_dirent *previous_dir = NULL;
770 char *ptr = NULL;
771 struct ext_filesystem *fs = get_fs();
Stephen Warren991201e2015-09-04 22:03:45 -0600772 int ret = -1;
Uma Shankara74a99a2012-05-25 21:22:49 +0530773
774 /* get the first block of root */
Uma Shankara74a99a2012-05-25 21:22:49 +0530775 root_first_block_buffer = zalloc(fs->blksz);
776 if (!root_first_block_buffer)
777 return -ENOMEM;
Stefan Brüns278f5d32016-09-06 04:36:41 +0200778 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Uma Shankara74a99a2012-05-25 21:22:49 +0530779 fs->blksz, root_first_block_buffer);
780 if (status == 0)
781 goto fail;
782
Stefan Brüns278f5d32016-09-06 04:36:41 +0200783 if (ext4fs_log_journal(root_first_block_buffer, blknr))
Uma Shankara74a99a2012-05-25 21:22:49 +0530784 goto fail;
785 dir = (struct ext2_dirent *)root_first_block_buffer;
786 ptr = (char *)dir;
787 totalbytes = 0;
Michael Walle13179c22016-09-01 11:21:40 +0200788 while (le16_to_cpu(dir->direntlen) >= 0) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530789 /*
790 * blocksize-totalbytes because last
791 * directory length i.e., *dir->direntlen
792 * is free availble space in the block that
793 * means it is a last entry of directory entry
794 */
Stefan Brüns278f5d32016-09-06 04:36:41 +0200795 if (dir->inode && (strlen(filename) == dir->namelen) &&
796 (strncmp(ptr + sizeof(struct ext2_dirent),
797 filename, dir->namelen) == 0)) {
798 printf("file found, deleting\n");
799 inodeno = le32_to_cpu(dir->inode);
800 if (previous_dir) {
Michael Walle13179c22016-09-01 11:21:40 +0200801 uint16_t new_len;
Michael Walle13179c22016-09-01 11:21:40 +0200802 new_len = le16_to_cpu(previous_dir->direntlen);
803 new_len += le16_to_cpu(dir->direntlen);
804 previous_dir->direntlen = cpu_to_le16(new_len);
Stefan Brüns278f5d32016-09-06 04:36:41 +0200805 } else {
Uma Shankara74a99a2012-05-25 21:22:49 +0530806 dir->inode = 0;
Uma Shankara74a99a2012-05-25 21:22:49 +0530807 }
Stefan Brüns278f5d32016-09-06 04:36:41 +0200808 found = 1;
809 break;
Uma Shankara74a99a2012-05-25 21:22:49 +0530810 }
811
Michael Walle13179c22016-09-01 11:21:40 +0200812 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
Uma Shankara74a99a2012-05-25 21:22:49 +0530813 break;
814
815 /* traversing the each directory entry */
Michael Walle13179c22016-09-01 11:21:40 +0200816 templength = le16_to_cpu(dir->direntlen);
Uma Shankara74a99a2012-05-25 21:22:49 +0530817 totalbytes = totalbytes + templength;
818 previous_dir = dir;
819 dir = (struct ext2_dirent *)((char *)dir + templength);
820 ptr = (char *)dir;
821 }
822
823
824 if (found == 1) {
Stefan Brüns278f5d32016-09-06 04:36:41 +0200825 if (ext4fs_put_metadata(root_first_block_buffer, blknr))
Uma Shankara74a99a2012-05-25 21:22:49 +0530826 goto fail;
Stephen Warren991201e2015-09-04 22:03:45 -0600827 ret = inodeno;
Uma Shankara74a99a2012-05-25 21:22:49 +0530828 }
829fail:
830 free(root_first_block_buffer);
831
Stephen Warren991201e2015-09-04 22:03:45 -0600832 return ret;
Uma Shankara74a99a2012-05-25 21:22:49 +0530833}
834
Stefan Brüns278f5d32016-09-06 04:36:41 +0200835int ext4fs_filename_unlink(char *filename)
Uma Shankara74a99a2012-05-25 21:22:49 +0530836{
837 short direct_blk_idx = 0;
838 long int blknr = -1;
839 int inodeno = -1;
840
841 /* read the block no allocated to a file */
842 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
843 direct_blk_idx++) {
844 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
845 if (blknr == 0)
846 break;
Stefan Brüns278f5d32016-09-06 04:36:41 +0200847 inodeno = unlink_filename(filename, blknr);
Uma Shankara74a99a2012-05-25 21:22:49 +0530848 if (inodeno != -1)
849 return inodeno;
850 }
851
852 return -1;
853}
854
Michael Walle13179c22016-09-01 11:21:40 +0200855uint32_t ext4fs_get_new_blk_no(void)
Uma Shankara74a99a2012-05-25 21:22:49 +0530856{
857 short i;
858 short status;
859 int remainder;
860 unsigned int bg_idx;
861 static int prev_bg_bitmap_index = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200862 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankara74a99a2012-05-25 21:22:49 +0530863 struct ext_filesystem *fs = get_fs();
864 char *journal_buffer = zalloc(fs->blksz);
865 char *zero_buffer = zalloc(fs->blksz);
866 if (!journal_buffer || !zero_buffer)
867 goto fail;
Simon Glassea8e5192012-10-03 11:37:49 +0000868 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
Uma Shankara74a99a2012-05-25 21:22:49 +0530869
870 if (fs->first_pass_bbmap == 0) {
871 for (i = 0; i < fs->no_blkgrp; i++) {
Michael Walle13179c22016-09-01 11:21:40 +0200872 if (le16_to_cpu(bgd[i].free_blocks)) {
873 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
874 uint16_t new_flags;
875 put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
Uma Shankara74a99a2012-05-25 21:22:49 +0530876 zero_buffer, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +0200877 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
878 bgd[i].bg_flags = cpu_to_le16(new_flags);
Uma Shankara74a99a2012-05-25 21:22:49 +0530879 memcpy(fs->blk_bmaps[i], zero_buffer,
880 fs->blksz);
881 }
882 fs->curr_blkno =
883 _get_new_blk_no(fs->blk_bmaps[i]);
884 if (fs->curr_blkno == -1)
885 /* if block bitmap is completely fill */
886 continue;
887 fs->curr_blkno = fs->curr_blkno +
888 (i * fs->blksz * 8);
889 fs->first_pass_bbmap++;
Michael Walle13179c22016-09-01 11:21:40 +0200890 ext4fs_bg_free_blocks_dec(&bgd[i]);
891 ext4fs_sb_free_blocks_dec(fs->sb);
892 status = ext4fs_devread(
893 (lbaint_t)le32_to_cpu(bgd[i].block_id) *
Uma Shankara74a99a2012-05-25 21:22:49 +0530894 fs->sect_perblk, 0,
895 fs->blksz,
896 journal_buffer);
897 if (status == 0)
898 goto fail;
899 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +0200900 le32_to_cpu(bgd[i].block_id)))
Uma Shankara74a99a2012-05-25 21:22:49 +0530901 goto fail;
902 goto success;
903 } else {
904 debug("no space left on block group %d\n", i);
905 }
906 }
907
908 goto fail;
909 } else {
910restart:
911 fs->curr_blkno++;
912 /* get the blockbitmap index respective to blockno */
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200913 bg_idx = fs->curr_blkno / blk_per_grp;
914 if (fs->blksz == 1024) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530915 remainder = fs->curr_blkno % blk_per_grp;
916 if (!remainder)
917 bg_idx--;
918 }
919
920 /*
921 * To skip completely filled block group bitmaps
922 * Optimize the block allocation
923 */
924 if (bg_idx >= fs->no_blkgrp)
925 goto fail;
926
Simon Glassea8e5192012-10-03 11:37:49 +0000927 if (bgd[bg_idx].free_blocks == 0) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530928 debug("block group %u is full. Skipping\n", bg_idx);
929 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
930 fs->curr_blkno--;
931 goto restart;
932 }
933
Michael Walle13179c22016-09-01 11:21:40 +0200934 if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
935 uint16_t new_flags;
Uma Shankara74a99a2012-05-25 21:22:49 +0530936 memset(zero_buffer, '\0', fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +0200937 put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
938 zero_buffer, fs->blksz);
Uma Shankara74a99a2012-05-25 21:22:49 +0530939 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +0200940 new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
941 bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
Uma Shankara74a99a2012-05-25 21:22:49 +0530942 }
943
944 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
945 bg_idx) != 0) {
946 debug("going for restart for the block no %ld %u\n",
947 fs->curr_blkno, bg_idx);
948 goto restart;
949 }
950
951 /* journal backup */
952 if (prev_bg_bitmap_index != bg_idx) {
953 memset(journal_buffer, '\0', fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +0200954 status = ext4fs_devread(
955 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
Uma Shankara74a99a2012-05-25 21:22:49 +0530956 * fs->sect_perblk,
957 0, fs->blksz, journal_buffer);
958 if (status == 0)
959 goto fail;
960 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +0200961 le32_to_cpu(bgd[bg_idx].block_id)))
Uma Shankara74a99a2012-05-25 21:22:49 +0530962 goto fail;
963
964 prev_bg_bitmap_index = bg_idx;
965 }
Michael Walle13179c22016-09-01 11:21:40 +0200966 ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
967 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankara74a99a2012-05-25 21:22:49 +0530968 goto success;
969 }
970success:
971 free(journal_buffer);
972 free(zero_buffer);
973
974 return fs->curr_blkno;
975fail:
976 free(journal_buffer);
977 free(zero_buffer);
978
979 return -1;
980}
981
982int ext4fs_get_new_inode_no(void)
983{
984 short i;
985 short status;
986 unsigned int ibmap_idx;
987 static int prev_inode_bitmap_index = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200988 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankara74a99a2012-05-25 21:22:49 +0530989 struct ext_filesystem *fs = get_fs();
990 char *journal_buffer = zalloc(fs->blksz);
991 char *zero_buffer = zalloc(fs->blksz);
992 if (!journal_buffer || !zero_buffer)
993 goto fail;
Simon Glassea8e5192012-10-03 11:37:49 +0000994 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
Uma Shankara74a99a2012-05-25 21:22:49 +0530995
996 if (fs->first_pass_ibmap == 0) {
997 for (i = 0; i < fs->no_blkgrp; i++) {
Simon Glassea8e5192012-10-03 11:37:49 +0000998 if (bgd[i].free_inodes) {
999 if (bgd[i].bg_itable_unused !=
1000 bgd[i].free_inodes)
1001 bgd[i].bg_itable_unused =
1002 bgd[i].free_inodes;
Michael Walle13179c22016-09-01 11:21:40 +02001003 if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
1004 int new_flags;
1005 put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
Uma Shankara74a99a2012-05-25 21:22:49 +05301006 zero_buffer, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001007 new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1008 bgd[i].bg_flags = cpu_to_le16(new_flags);
Uma Shankara74a99a2012-05-25 21:22:49 +05301009 memcpy(fs->inode_bmaps[i],
1010 zero_buffer, fs->blksz);
1011 }
1012 fs->curr_inode_no =
1013 _get_new_inode_no(fs->inode_bmaps[i]);
1014 if (fs->curr_inode_no == -1)
1015 /* if block bitmap is completely fill */
1016 continue;
1017 fs->curr_inode_no = fs->curr_inode_no +
1018 (i * inodes_per_grp);
1019 fs->first_pass_ibmap++;
Michael Walle13179c22016-09-01 11:21:40 +02001020 ext4fs_bg_free_inodes_dec(&bgd[i]);
1021 ext4fs_bg_itable_unused_dec(&bgd[i]);
1022 ext4fs_sb_free_inodes_dec(fs->sb);
1023 status = ext4fs_devread(
1024 (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
Uma Shankara74a99a2012-05-25 21:22:49 +05301025 fs->sect_perblk, 0,
1026 fs->blksz,
1027 journal_buffer);
1028 if (status == 0)
1029 goto fail;
1030 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +02001031 le32_to_cpu(bgd[i].inode_id)))
Uma Shankara74a99a2012-05-25 21:22:49 +05301032 goto fail;
1033 goto success;
1034 } else
1035 debug("no inode left on block group %d\n", i);
1036 }
1037 goto fail;
1038 } else {
1039restart:
1040 fs->curr_inode_no++;
1041 /* get the blockbitmap index respective to blockno */
1042 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Michael Walle13179c22016-09-01 11:21:40 +02001043 if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
1044 int new_flags;
Uma Shankara74a99a2012-05-25 21:22:49 +05301045 memset(zero_buffer, '\0', fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001046 put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
1047 zero_buffer, fs->blksz);
1048 new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
1049 bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
Uma Shankara74a99a2012-05-25 21:22:49 +05301050 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1051 fs->blksz);
1052 }
1053
1054 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1055 fs->inode_bmaps[ibmap_idx],
1056 ibmap_idx) != 0) {
1057 debug("going for restart for the block no %d %u\n",
1058 fs->curr_inode_no, ibmap_idx);
1059 goto restart;
1060 }
1061
1062 /* journal backup */
1063 if (prev_inode_bitmap_index != ibmap_idx) {
1064 memset(journal_buffer, '\0', fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001065 status = ext4fs_devread(
1066 (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
Uma Shankara74a99a2012-05-25 21:22:49 +05301067 * fs->sect_perblk,
1068 0, fs->blksz, journal_buffer);
1069 if (status == 0)
1070 goto fail;
1071 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +02001072 le32_to_cpu(bgd[ibmap_idx].inode_id)))
Uma Shankara74a99a2012-05-25 21:22:49 +05301073 goto fail;
1074 prev_inode_bitmap_index = ibmap_idx;
1075 }
Simon Glassea8e5192012-10-03 11:37:49 +00001076 if (bgd[ibmap_idx].bg_itable_unused !=
1077 bgd[ibmap_idx].free_inodes)
1078 bgd[ibmap_idx].bg_itable_unused =
1079 bgd[ibmap_idx].free_inodes;
Michael Walle13179c22016-09-01 11:21:40 +02001080 ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
1081 ext4fs_bg_itable_unused_dec(&bgd[ibmap_idx]);
1082 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankara74a99a2012-05-25 21:22:49 +05301083 goto success;
1084 }
1085
1086success:
1087 free(journal_buffer);
1088 free(zero_buffer);
1089
1090 return fs->curr_inode_no;
1091fail:
1092 free(journal_buffer);
1093 free(zero_buffer);
1094
1095 return -1;
1096
1097}
1098
1099
1100static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1101 unsigned int *total_remaining_blocks,
1102 unsigned int *no_blks_reqd)
1103{
1104 short i;
1105 short status;
1106 long int actual_block_no;
1107 long int si_blockno;
1108 /* si :single indirect */
Michael Walle13179c22016-09-01 11:21:40 +02001109 __le32 *si_buffer = NULL;
1110 __le32 *si_start_addr = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +05301111 struct ext_filesystem *fs = get_fs();
1112
1113 if (*total_remaining_blocks != 0) {
1114 si_buffer = zalloc(fs->blksz);
1115 if (!si_buffer) {
1116 printf("No Memory\n");
1117 return;
1118 }
1119 si_start_addr = si_buffer;
1120 si_blockno = ext4fs_get_new_blk_no();
1121 if (si_blockno == -1) {
1122 printf("no block left to assign\n");
1123 goto fail;
1124 }
1125 (*no_blks_reqd)++;
1126 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1127
Frederic Leroye7ee0282013-06-26 18:11:25 +02001128 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankara74a99a2012-05-25 21:22:49 +05301129 0, fs->blksz, (char *)si_buffer);
1130 memset(si_buffer, '\0', fs->blksz);
1131 if (status == 0)
1132 goto fail;
1133
1134 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1135 actual_block_no = ext4fs_get_new_blk_no();
1136 if (actual_block_no == -1) {
1137 printf("no block left to assign\n");
1138 goto fail;
1139 }
Michael Walle13179c22016-09-01 11:21:40 +02001140 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankara74a99a2012-05-25 21:22:49 +05301141 debug("SIAB %u: %u\n", *si_buffer,
1142 *total_remaining_blocks);
1143
1144 si_buffer++;
1145 (*total_remaining_blocks)--;
1146 if (*total_remaining_blocks == 0)
1147 break;
1148 }
1149
1150 /* write the block to disk */
Ma Haijune0996ca2014-01-08 08:15:33 +08001151 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301152 si_start_addr, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001153 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankara74a99a2012-05-25 21:22:49 +05301154 }
1155fail:
1156 free(si_start_addr);
1157}
1158
1159static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1160 unsigned int *total_remaining_blocks,
1161 unsigned int *no_blks_reqd)
1162{
1163 short i;
1164 short j;
1165 short status;
1166 long int actual_block_no;
1167 /* di:double indirect */
1168 long int di_blockno_parent;
1169 long int di_blockno_child;
Michael Walle13179c22016-09-01 11:21:40 +02001170 __le32 *di_parent_buffer = NULL;
1171 __le32 *di_child_buff = NULL;
1172 __le32 *di_block_start_addr = NULL;
1173 __le32 *di_child_buff_start = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +05301174 struct ext_filesystem *fs = get_fs();
1175
1176 if (*total_remaining_blocks != 0) {
1177 /* double indirect parent block connecting to inode */
1178 di_blockno_parent = ext4fs_get_new_blk_no();
1179 if (di_blockno_parent == -1) {
1180 printf("no block left to assign\n");
1181 goto fail;
1182 }
1183 di_parent_buffer = zalloc(fs->blksz);
1184 if (!di_parent_buffer)
1185 goto fail;
1186
1187 di_block_start_addr = di_parent_buffer;
1188 (*no_blks_reqd)++;
1189 debug("DIPB %ld: %u\n", di_blockno_parent,
1190 *total_remaining_blocks);
1191
Frederic Leroye7ee0282013-06-26 18:11:25 +02001192 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankara74a99a2012-05-25 21:22:49 +05301193 fs->sect_perblk, 0,
1194 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskib55a7932012-12-05 08:06:39 +00001195
1196 if (!status) {
1197 printf("%s: Device read error!\n", __func__);
1198 goto fail;
1199 }
Uma Shankara74a99a2012-05-25 21:22:49 +05301200 memset(di_parent_buffer, '\0', fs->blksz);
1201
1202 /*
1203 * start:for each double indirect parent
1204 * block create one more block
1205 */
1206 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1207 di_blockno_child = ext4fs_get_new_blk_no();
1208 if (di_blockno_child == -1) {
1209 printf("no block left to assign\n");
1210 goto fail;
1211 }
1212 di_child_buff = zalloc(fs->blksz);
1213 if (!di_child_buff)
1214 goto fail;
1215
1216 di_child_buff_start = di_child_buff;
Michael Walle13179c22016-09-01 11:21:40 +02001217 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankara74a99a2012-05-25 21:22:49 +05301218 di_parent_buffer++;
1219 (*no_blks_reqd)++;
1220 debug("DICB %ld: %u\n", di_blockno_child,
1221 *total_remaining_blocks);
1222
Frederic Leroye7ee0282013-06-26 18:11:25 +02001223 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankara74a99a2012-05-25 21:22:49 +05301224 fs->sect_perblk, 0,
1225 fs->blksz,
1226 (char *)di_child_buff);
Łukasz Majewskib55a7932012-12-05 08:06:39 +00001227
1228 if (!status) {
1229 printf("%s: Device read error!\n", __func__);
1230 goto fail;
1231 }
Uma Shankara74a99a2012-05-25 21:22:49 +05301232 memset(di_child_buff, '\0', fs->blksz);
1233 /* filling of actual datablocks for each child */
1234 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1235 actual_block_no = ext4fs_get_new_blk_no();
1236 if (actual_block_no == -1) {
1237 printf("no block left to assign\n");
1238 goto fail;
1239 }
Michael Walle13179c22016-09-01 11:21:40 +02001240 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankara74a99a2012-05-25 21:22:49 +05301241 debug("DIAB %ld: %u\n", actual_block_no,
1242 *total_remaining_blocks);
1243
1244 di_child_buff++;
1245 (*total_remaining_blocks)--;
1246 if (*total_remaining_blocks == 0)
1247 break;
1248 }
1249 /* write the block table */
Ma Haijune0996ca2014-01-08 08:15:33 +08001250 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301251 di_child_buff_start, fs->blksz);
1252 free(di_child_buff_start);
1253 di_child_buff_start = NULL;
1254
1255 if (*total_remaining_blocks == 0)
1256 break;
1257 }
Ma Haijune0996ca2014-01-08 08:15:33 +08001258 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301259 di_block_start_addr, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001260 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankara74a99a2012-05-25 21:22:49 +05301261 }
1262fail:
1263 free(di_block_start_addr);
1264}
1265
1266static void alloc_triple_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 k;
1273 long int actual_block_no;
1274 /* ti: Triple Indirect */
1275 long int ti_gp_blockno;
1276 long int ti_parent_blockno;
1277 long int ti_child_blockno;
Michael Walle13179c22016-09-01 11:21:40 +02001278 __le32 *ti_gp_buff = NULL;
1279 __le32 *ti_parent_buff = NULL;
1280 __le32 *ti_child_buff = NULL;
1281 __le32 *ti_gp_buff_start_addr = NULL;
1282 __le32 *ti_pbuff_start_addr = NULL;
1283 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +05301284 struct ext_filesystem *fs = get_fs();
1285 if (*total_remaining_blocks != 0) {
1286 /* triple indirect grand parent block connecting to inode */
1287 ti_gp_blockno = ext4fs_get_new_blk_no();
1288 if (ti_gp_blockno == -1) {
1289 printf("no block left to assign\n");
Tom Rini9166c3c2015-12-10 16:42:21 -05001290 return;
Uma Shankara74a99a2012-05-25 21:22:49 +05301291 }
1292 ti_gp_buff = zalloc(fs->blksz);
1293 if (!ti_gp_buff)
Tom Rini9166c3c2015-12-10 16:42:21 -05001294 return;
Uma Shankara74a99a2012-05-25 21:22:49 +05301295
1296 ti_gp_buff_start_addr = ti_gp_buff;
1297 (*no_blks_reqd)++;
1298 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1299 *total_remaining_blocks);
1300
1301 /* for each 4 byte grand parent entry create one more block */
1302 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1303 ti_parent_blockno = ext4fs_get_new_blk_no();
1304 if (ti_parent_blockno == -1) {
1305 printf("no block left to assign\n");
1306 goto fail;
1307 }
1308 ti_parent_buff = zalloc(fs->blksz);
1309 if (!ti_parent_buff)
1310 goto fail;
1311
1312 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle13179c22016-09-01 11:21:40 +02001313 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankara74a99a2012-05-25 21:22:49 +05301314 ti_gp_buff++;
1315 (*no_blks_reqd)++;
1316 debug("TIPB %ld: %u\n", ti_parent_blockno,
1317 *total_remaining_blocks);
1318
1319 /* for each 4 byte entry parent create one more block */
1320 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1321 ti_child_blockno = ext4fs_get_new_blk_no();
1322 if (ti_child_blockno == -1) {
1323 printf("no block left assign\n");
Tom Rini9166c3c2015-12-10 16:42:21 -05001324 goto fail1;
Uma Shankara74a99a2012-05-25 21:22:49 +05301325 }
1326 ti_child_buff = zalloc(fs->blksz);
1327 if (!ti_child_buff)
Tom Rini9166c3c2015-12-10 16:42:21 -05001328 goto fail1;
Uma Shankara74a99a2012-05-25 21:22:49 +05301329
1330 ti_cbuff_start_addr = ti_child_buff;
Michael Walle13179c22016-09-01 11:21:40 +02001331 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankara74a99a2012-05-25 21:22:49 +05301332 ti_parent_buff++;
1333 (*no_blks_reqd)++;
1334 debug("TICB %ld: %u\n", ti_parent_blockno,
1335 *total_remaining_blocks);
1336
1337 /* fill actual datablocks for each child */
1338 for (k = 0; k < (fs->blksz / sizeof(int));
1339 k++) {
1340 actual_block_no =
1341 ext4fs_get_new_blk_no();
1342 if (actual_block_no == -1) {
1343 printf("no block left\n");
Tom Rini9166c3c2015-12-10 16:42:21 -05001344 free(ti_cbuff_start_addr);
1345 goto fail1;
Uma Shankara74a99a2012-05-25 21:22:49 +05301346 }
Michael Walle13179c22016-09-01 11:21:40 +02001347 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankara74a99a2012-05-25 21:22:49 +05301348 debug("TIAB %ld: %u\n", actual_block_no,
1349 *total_remaining_blocks);
1350
1351 ti_child_buff++;
1352 (*total_remaining_blocks)--;
1353 if (*total_remaining_blocks == 0)
1354 break;
1355 }
1356 /* write the child block */
Ma Haijune0996ca2014-01-08 08:15:33 +08001357 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1358 (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301359 ti_cbuff_start_addr, fs->blksz);
1360 free(ti_cbuff_start_addr);
1361
1362 if (*total_remaining_blocks == 0)
1363 break;
1364 }
1365 /* write the parent block */
Ma Haijune0996ca2014-01-08 08:15:33 +08001366 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301367 ti_pbuff_start_addr, fs->blksz);
1368 free(ti_pbuff_start_addr);
1369
1370 if (*total_remaining_blocks == 0)
1371 break;
1372 }
1373 /* write the grand parent block */
Ma Haijune0996ca2014-01-08 08:15:33 +08001374 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301375 ti_gp_buff_start_addr, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001376 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini9166c3c2015-12-10 16:42:21 -05001377 free(ti_gp_buff_start_addr);
1378 return;
Uma Shankara74a99a2012-05-25 21:22:49 +05301379 }
Tom Rini9166c3c2015-12-10 16:42:21 -05001380fail1:
1381 free(ti_pbuff_start_addr);
Uma Shankara74a99a2012-05-25 21:22:49 +05301382fail:
1383 free(ti_gp_buff_start_addr);
1384}
1385
1386void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1387 unsigned int total_remaining_blocks,
1388 unsigned int *total_no_of_block)
1389{
1390 short i;
1391 long int direct_blockno;
1392 unsigned int no_blks_reqd = 0;
1393
1394 /* allocation of direct blocks */
Stephen Warrene62f1ba2014-06-11 12:46:16 -06001395 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankara74a99a2012-05-25 21:22:49 +05301396 direct_blockno = ext4fs_get_new_blk_no();
1397 if (direct_blockno == -1) {
1398 printf("no block left to assign\n");
1399 return;
1400 }
Michael Walle13179c22016-09-01 11:21:40 +02001401 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankara74a99a2012-05-25 21:22:49 +05301402 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1403
1404 total_remaining_blocks--;
Uma Shankara74a99a2012-05-25 21:22:49 +05301405 }
1406
1407 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1408 &no_blks_reqd);
1409 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1410 &no_blks_reqd);
1411 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1412 &no_blks_reqd);
1413 *total_no_of_block += no_blks_reqd;
1414}
1415
1416#endif
1417
Uma Shankar71014b62012-05-25 21:21:44 +05301418static struct ext4_extent_header *ext4fs_get_extent_block
1419 (struct ext2_data *data, char *buf,
1420 struct ext4_extent_header *ext_block,
1421 uint32_t fileblock, int log2_blksz)
1422{
1423 struct ext4_extent_idx *index;
1424 unsigned long long block;
Ionut Nicu48b2b592014-01-13 11:59:24 +01001425 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankar71014b62012-05-25 21:21:44 +05301426 int i;
1427
1428 while (1) {
1429 index = (struct ext4_extent_idx *)(ext_block + 1);
1430
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001431 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle13179c22016-09-01 11:21:40 +02001432 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05301433
1434 if (ext_block->eh_depth == 0)
1435 return ext_block;
1436 i = -1;
1437 do {
1438 i++;
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001439 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankar71014b62012-05-25 21:21:44 +05301440 break;
Ionut Nicu01f606c2014-01-13 12:00:08 +01001441 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankar71014b62012-05-25 21:21:44 +05301442
1443 if (--i < 0)
Michael Walle13179c22016-09-01 11:21:40 +02001444 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05301445
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001446 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankar71014b62012-05-25 21:21:44 +05301447 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1448
Ionut Nicu48b2b592014-01-13 11:59:24 +01001449 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
Frederic Leroye7ee0282013-06-26 18:11:25 +02001450 buf))
Uma Shankar71014b62012-05-25 21:21:44 +05301451 ext_block = (struct ext4_extent_header *)buf;
1452 else
Michael Walle13179c22016-09-01 11:21:40 +02001453 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05301454 }
1455}
1456
1457static int ext4fs_blockgroup
1458 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1459{
1460 long int blkno;
1461 unsigned int blkoff, desc_per_blk;
Egbert Eich7b1b2552013-05-01 01:13:19 +00001462 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankar71014b62012-05-25 21:21:44 +05301463
1464 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1465
Michael Wallec07cdcb2016-08-29 10:46:44 +02001466 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankar71014b62012-05-25 21:21:44 +05301467 group / desc_per_blk;
1468 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1469
1470 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1471 group, blkno, blkoff);
1472
Frederic Leroye7ee0282013-06-26 18:11:25 +02001473 return ext4fs_devread((lbaint_t)blkno <<
1474 (LOG2_BLOCK_SIZE(data) - log2blksz),
Uma Shankar71014b62012-05-25 21:21:44 +05301475 blkoff, sizeof(struct ext2_block_group),
1476 (char *)blkgrp);
1477}
1478
1479int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1480{
1481 struct ext2_block_group blkgrp;
1482 struct ext2_sblock *sblock = &data->sblock;
1483 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-05-01 01:13:19 +00001484 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankar71014b62012-05-25 21:21:44 +05301485 int inodes_per_block, status;
1486 long int blkno;
1487 unsigned int blkoff;
1488
1489 /* It is easier to calculate if the first inode is 0. */
1490 ino--;
Michael Wallec07cdcb2016-08-29 10:46:44 +02001491 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301492 (sblock->inodes_per_group), &blkgrp);
1493 if (status == 0)
1494 return 0;
1495
1496 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Michael Wallec07cdcb2016-08-29 10:46:44 +02001497 blkno = le32_to_cpu(blkgrp.inode_table_id) +
1498 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankar71014b62012-05-25 21:21:44 +05301499 blkoff = (ino % inodes_per_block) * fs->inodesz;
1500 /* Read the inode. */
Frederic Leroye7ee0282013-06-26 18:11:25 +02001501 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1502 log2blksz), blkoff,
Uma Shankar71014b62012-05-25 21:21:44 +05301503 sizeof(struct ext2_inode), (char *)inode);
1504 if (status == 0)
1505 return 0;
1506
1507 return 1;
1508}
1509
1510long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1511{
1512 long int blknr;
1513 int blksz;
1514 int log2_blksz;
1515 int status;
1516 long int rblock;
1517 long int perblock_parent;
1518 long int perblock_child;
1519 unsigned long long start;
1520 /* get the blocksize of the filesystem */
1521 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich7b1b2552013-05-01 01:13:19 +00001522 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1523 - get_fs()->dev_desc->log2blksz;
1524
Uma Shankar71014b62012-05-25 21:21:44 +05301525 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1526 char *buf = zalloc(blksz);
1527 if (!buf)
1528 return -ENOMEM;
1529 struct ext4_extent_header *ext_block;
1530 struct ext4_extent *extent;
1531 int i = -1;
Egbert Eich7b1b2552013-05-01 01:13:19 +00001532 ext_block =
1533 ext4fs_get_extent_block(ext4fs_root, buf,
1534 (struct ext4_extent_header *)
1535 inode->b.blocks.dir_blocks,
1536 fileblock, log2_blksz);
Uma Shankar71014b62012-05-25 21:21:44 +05301537 if (!ext_block) {
1538 printf("invalid extent block\n");
1539 free(buf);
1540 return -EINVAL;
1541 }
1542
1543 extent = (struct ext4_extent *)(ext_block + 1);
1544
1545 do {
1546 i++;
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001547 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankar71014b62012-05-25 21:21:44 +05301548 break;
1549 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1550 if (--i >= 0) {
1551 fileblock -= le32_to_cpu(extent[i].ee_block);
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001552 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
Uma Shankar71014b62012-05-25 21:21:44 +05301553 free(buf);
1554 return 0;
1555 }
1556
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001557 start = le16_to_cpu(extent[i].ee_start_hi);
Uma Shankar71014b62012-05-25 21:21:44 +05301558 start = (start << 32) +
1559 le32_to_cpu(extent[i].ee_start_lo);
1560 free(buf);
1561 return fileblock + start;
1562 }
1563
1564 printf("Extent Error\n");
1565 free(buf);
1566 return -1;
1567 }
1568
1569 /* Direct blocks. */
1570 if (fileblock < INDIRECT_BLOCKS)
Michael Wallec07cdcb2016-08-29 10:46:44 +02001571 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankar71014b62012-05-25 21:21:44 +05301572
1573 /* Indirect. */
1574 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1575 if (ext4fs_indir1_block == NULL) {
1576 ext4fs_indir1_block = zalloc(blksz);
1577 if (ext4fs_indir1_block == NULL) {
1578 printf("** SI ext2fs read block (indir 1)"
1579 "malloc failed. **\n");
1580 return -1;
1581 }
1582 ext4fs_indir1_size = blksz;
1583 ext4fs_indir1_blkno = -1;
1584 }
1585 if (blksz != ext4fs_indir1_size) {
1586 free(ext4fs_indir1_block);
1587 ext4fs_indir1_block = NULL;
1588 ext4fs_indir1_size = 0;
1589 ext4fs_indir1_blkno = -1;
1590 ext4fs_indir1_block = zalloc(blksz);
1591 if (ext4fs_indir1_block == NULL) {
1592 printf("** SI ext2fs read block (indir 1):"
1593 "malloc failed. **\n");
1594 return -1;
1595 }
1596 ext4fs_indir1_size = blksz;
1597 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001598 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301599 log2_blksz) != ext4fs_indir1_blkno) {
1600 status =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001601 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301602 (inode->b.blocks.
1603 indir_block) << log2_blksz, 0,
1604 blksz, (char *)ext4fs_indir1_block);
1605 if (status == 0) {
1606 printf("** SI ext2fs read block (indir 1)"
1607 "failed. **\n");
1608 return 0;
1609 }
1610 ext4fs_indir1_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001611 le32_to_cpu(inode->b.blocks.
Uma Shankar71014b62012-05-25 21:21:44 +05301612 indir_block) << log2_blksz;
1613 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001614 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankar71014b62012-05-25 21:21:44 +05301615 [fileblock - INDIRECT_BLOCKS]);
1616 }
1617 /* Double indirect. */
1618 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1619 (blksz / 4 + 1)))) {
1620
1621 long int perblock = blksz / 4;
1622 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1623
1624 if (ext4fs_indir1_block == NULL) {
1625 ext4fs_indir1_block = zalloc(blksz);
1626 if (ext4fs_indir1_block == NULL) {
1627 printf("** DI ext2fs read block (indir 2 1)"
1628 "malloc failed. **\n");
1629 return -1;
1630 }
1631 ext4fs_indir1_size = blksz;
1632 ext4fs_indir1_blkno = -1;
1633 }
1634 if (blksz != ext4fs_indir1_size) {
1635 free(ext4fs_indir1_block);
1636 ext4fs_indir1_block = NULL;
1637 ext4fs_indir1_size = 0;
1638 ext4fs_indir1_blkno = -1;
1639 ext4fs_indir1_block = zalloc(blksz);
1640 if (ext4fs_indir1_block == NULL) {
1641 printf("** DI ext2fs read block (indir 2 1)"
1642 "malloc failed. **\n");
1643 return -1;
1644 }
1645 ext4fs_indir1_size = blksz;
1646 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001647 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301648 log2_blksz) != ext4fs_indir1_blkno) {
1649 status =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001650 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301651 (inode->b.blocks.
1652 double_indir_block) << log2_blksz,
1653 0, blksz,
1654 (char *)ext4fs_indir1_block);
1655 if (status == 0) {
1656 printf("** DI ext2fs read block (indir 2 1)"
1657 "failed. **\n");
1658 return -1;
1659 }
1660 ext4fs_indir1_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001661 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301662 log2_blksz;
1663 }
1664
1665 if (ext4fs_indir2_block == NULL) {
1666 ext4fs_indir2_block = zalloc(blksz);
1667 if (ext4fs_indir2_block == NULL) {
1668 printf("** DI ext2fs read block (indir 2 2)"
1669 "malloc failed. **\n");
1670 return -1;
1671 }
1672 ext4fs_indir2_size = blksz;
1673 ext4fs_indir2_blkno = -1;
1674 }
1675 if (blksz != ext4fs_indir2_size) {
1676 free(ext4fs_indir2_block);
1677 ext4fs_indir2_block = NULL;
1678 ext4fs_indir2_size = 0;
1679 ext4fs_indir2_blkno = -1;
1680 ext4fs_indir2_block = zalloc(blksz);
1681 if (ext4fs_indir2_block == NULL) {
1682 printf("** DI ext2fs read block (indir 2 2)"
1683 "malloc failed. **\n");
1684 return -1;
1685 }
1686 ext4fs_indir2_size = blksz;
1687 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001688 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301689 log2_blksz) != ext4fs_indir2_blkno) {
Michael Wallec07cdcb2016-08-29 10:46:44 +02001690 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301691 (ext4fs_indir1_block
1692 [rblock /
1693 perblock]) << log2_blksz, 0,
1694 blksz,
1695 (char *)ext4fs_indir2_block);
1696 if (status == 0) {
1697 printf("** DI ext2fs read block (indir 2 2)"
1698 "failed. **\n");
1699 return -1;
1700 }
1701 ext4fs_indir2_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001702 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankar71014b62012-05-25 21:21:44 +05301703 /
1704 perblock]) <<
1705 log2_blksz;
1706 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001707 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankar71014b62012-05-25 21:21:44 +05301708 }
1709 /* Tripple indirect. */
1710 else {
1711 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1712 (blksz / 4 * blksz / 4));
1713 perblock_child = blksz / 4;
1714 perblock_parent = ((blksz / 4) * (blksz / 4));
1715
1716 if (ext4fs_indir1_block == NULL) {
1717 ext4fs_indir1_block = zalloc(blksz);
1718 if (ext4fs_indir1_block == NULL) {
1719 printf("** TI ext2fs read block (indir 2 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("** TI ext2fs read block (indir 2 1)"
1734 "malloc failed. **\n");
1735 return -1;
1736 }
1737 ext4fs_indir1_size = blksz;
1738 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001739 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301740 log2_blksz) != ext4fs_indir1_blkno) {
1741 status = ext4fs_devread
Frederic Leroye7ee0282013-06-26 18:11:25 +02001742 ((lbaint_t)
Michael Wallec07cdcb2016-08-29 10:46:44 +02001743 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankar71014b62012-05-25 21:21:44 +05301744 << log2_blksz, 0, blksz,
1745 (char *)ext4fs_indir1_block);
1746 if (status == 0) {
1747 printf("** TI ext2fs read block (indir 2 1)"
1748 "failed. **\n");
1749 return -1;
1750 }
1751 ext4fs_indir1_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001752 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301753 log2_blksz;
1754 }
1755
1756 if (ext4fs_indir2_block == NULL) {
1757 ext4fs_indir2_block = zalloc(blksz);
1758 if (ext4fs_indir2_block == NULL) {
1759 printf("** TI ext2fs read block (indir 2 2)"
1760 "malloc failed. **\n");
1761 return -1;
1762 }
1763 ext4fs_indir2_size = blksz;
1764 ext4fs_indir2_blkno = -1;
1765 }
1766 if (blksz != ext4fs_indir2_size) {
1767 free(ext4fs_indir2_block);
1768 ext4fs_indir2_block = NULL;
1769 ext4fs_indir2_size = 0;
1770 ext4fs_indir2_blkno = -1;
1771 ext4fs_indir2_block = zalloc(blksz);
1772 if (ext4fs_indir2_block == NULL) {
1773 printf("** TI ext2fs read block (indir 2 2)"
1774 "malloc failed. **\n");
1775 return -1;
1776 }
1777 ext4fs_indir2_size = blksz;
1778 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001779 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankar71014b62012-05-25 21:21:44 +05301780 perblock_parent]) <<
1781 log2_blksz)
1782 != ext4fs_indir2_blkno) {
Michael Wallec07cdcb2016-08-29 10:46:44 +02001783 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301784 (ext4fs_indir1_block
1785 [rblock /
1786 perblock_parent]) <<
1787 log2_blksz, 0, blksz,
1788 (char *)ext4fs_indir2_block);
1789 if (status == 0) {
1790 printf("** TI ext2fs read block (indir 2 2)"
1791 "failed. **\n");
1792 return -1;
1793 }
1794 ext4fs_indir2_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001795 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankar71014b62012-05-25 21:21:44 +05301796 perblock_parent])
1797 << log2_blksz;
1798 }
1799
1800 if (ext4fs_indir3_block == NULL) {
1801 ext4fs_indir3_block = zalloc(blksz);
1802 if (ext4fs_indir3_block == NULL) {
1803 printf("** TI ext2fs read block (indir 2 2)"
1804 "malloc failed. **\n");
1805 return -1;
1806 }
1807 ext4fs_indir3_size = blksz;
1808 ext4fs_indir3_blkno = -1;
1809 }
1810 if (blksz != ext4fs_indir3_size) {
1811 free(ext4fs_indir3_block);
1812 ext4fs_indir3_block = NULL;
1813 ext4fs_indir3_size = 0;
1814 ext4fs_indir3_blkno = -1;
1815 ext4fs_indir3_block = zalloc(blksz);
1816 if (ext4fs_indir3_block == NULL) {
1817 printf("** TI ext2fs read block (indir 2 2)"
1818 "malloc failed. **\n");
1819 return -1;
1820 }
1821 ext4fs_indir3_size = blksz;
1822 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001823 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankar71014b62012-05-25 21:21:44 +05301824 /
1825 perblock_child]) <<
1826 log2_blksz) != ext4fs_indir3_blkno) {
1827 status =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001828 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301829 (ext4fs_indir2_block
1830 [(rblock / perblock_child)
1831 % (blksz / 4)]) << log2_blksz, 0,
1832 blksz, (char *)ext4fs_indir3_block);
1833 if (status == 0) {
1834 printf("** TI ext2fs read block (indir 2 2)"
1835 "failed. **\n");
1836 return -1;
1837 }
1838 ext4fs_indir3_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001839 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankar71014b62012-05-25 21:21:44 +05301840 perblock_child) %
1841 (blksz /
1842 4)]) <<
1843 log2_blksz;
1844 }
1845
Michael Wallec07cdcb2016-08-29 10:46:44 +02001846 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankar71014b62012-05-25 21:21:44 +05301847 [rblock % perblock_child]);
1848 }
Egbert Eich7b1b2552013-05-01 01:13:19 +00001849 debug("read_allocated_block %ld\n", blknr);
Uma Shankar71014b62012-05-25 21:21:44 +05301850
1851 return blknr;
1852}
1853
Łukasz Majewski900db5d2014-05-06 09:36:05 +02001854/**
1855 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1856 * global pointers
1857 *
1858 * This function assures that for a file with the same name but different size
1859 * the sequential store on the ext4 filesystem will be correct.
1860 *
1861 * In this function the global data, responsible for internal representation
1862 * of the ext4 data are initialized to the reset state. Without this, during
1863 * replacement of the smaller file with the bigger truncation of new file was
1864 * performed.
1865 */
1866void ext4fs_reinit_global(void)
Uma Shankar71014b62012-05-25 21:21:44 +05301867{
Uma Shankar71014b62012-05-25 21:21:44 +05301868 if (ext4fs_indir1_block != NULL) {
1869 free(ext4fs_indir1_block);
1870 ext4fs_indir1_block = NULL;
1871 ext4fs_indir1_size = 0;
1872 ext4fs_indir1_blkno = -1;
1873 }
1874 if (ext4fs_indir2_block != NULL) {
1875 free(ext4fs_indir2_block);
1876 ext4fs_indir2_block = NULL;
1877 ext4fs_indir2_size = 0;
1878 ext4fs_indir2_blkno = -1;
1879 }
1880 if (ext4fs_indir3_block != NULL) {
1881 free(ext4fs_indir3_block);
1882 ext4fs_indir3_block = NULL;
1883 ext4fs_indir3_size = 0;
1884 ext4fs_indir3_blkno = -1;
1885 }
Łukasz Majewski900db5d2014-05-06 09:36:05 +02001886}
1887void ext4fs_close(void)
1888{
1889 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1890 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1891 ext4fs_file = NULL;
1892 }
1893 if (ext4fs_root != NULL) {
1894 free(ext4fs_root);
1895 ext4fs_root = NULL;
1896 }
1897
1898 ext4fs_reinit_global();
Uma Shankar71014b62012-05-25 21:21:44 +05301899}
1900
1901int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1902 struct ext2fs_node **fnode, int *ftype)
1903{
1904 unsigned int fpos = 0;
1905 int status;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001906 loff_t actread;
Uma Shankar71014b62012-05-25 21:21:44 +05301907 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1908
1909#ifdef DEBUG
1910 if (name != NULL)
1911 printf("Iterate dir %s\n", name);
1912#endif /* of DEBUG */
1913 if (!diro->inode_read) {
1914 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1915 if (status == 0)
1916 return 0;
1917 }
1918 /* Search the file. */
Michael Wallec07cdcb2016-08-29 10:46:44 +02001919 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankar71014b62012-05-25 21:21:44 +05301920 struct ext2_dirent dirent;
1921
1922 status = ext4fs_read_file(diro, fpos,
1923 sizeof(struct ext2_dirent),
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001924 (char *)&dirent, &actread);
1925 if (status < 0)
Uma Shankar71014b62012-05-25 21:21:44 +05301926 return 0;
1927
Thomas Fitzsimmonsc6676e02015-11-18 12:42:53 -05001928 if (dirent.direntlen == 0) {
1929 printf("Failed to iterate over directory %s\n", name);
1930 return 0;
1931 }
1932
Uma Shankar71014b62012-05-25 21:21:44 +05301933 if (dirent.namelen != 0) {
1934 char filename[dirent.namelen + 1];
1935 struct ext2fs_node *fdiro;
1936 int type = FILETYPE_UNKNOWN;
1937
1938 status = ext4fs_read_file(diro,
1939 fpos +
1940 sizeof(struct ext2_dirent),
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001941 dirent.namelen, filename,
1942 &actread);
1943 if (status < 0)
Uma Shankar71014b62012-05-25 21:21:44 +05301944 return 0;
1945
1946 fdiro = zalloc(sizeof(struct ext2fs_node));
1947 if (!fdiro)
1948 return 0;
1949
1950 fdiro->data = diro->data;
Michael Wallec07cdcb2016-08-29 10:46:44 +02001951 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankar71014b62012-05-25 21:21:44 +05301952
1953 filename[dirent.namelen] = '\0';
1954
1955 if (dirent.filetype != FILETYPE_UNKNOWN) {
1956 fdiro->inode_read = 0;
1957
1958 if (dirent.filetype == FILETYPE_DIRECTORY)
1959 type = FILETYPE_DIRECTORY;
1960 else if (dirent.filetype == FILETYPE_SYMLINK)
1961 type = FILETYPE_SYMLINK;
1962 else if (dirent.filetype == FILETYPE_REG)
1963 type = FILETYPE_REG;
1964 } else {
1965 status = ext4fs_read_inode(diro->data,
Michael Wallec07cdcb2016-08-29 10:46:44 +02001966 le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301967 (dirent.inode),
1968 &fdiro->inode);
1969 if (status == 0) {
1970 free(fdiro);
1971 return 0;
1972 }
1973 fdiro->inode_read = 1;
1974
Michael Wallec07cdcb2016-08-29 10:46:44 +02001975 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankar71014b62012-05-25 21:21:44 +05301976 FILETYPE_INO_MASK) ==
1977 FILETYPE_INO_DIRECTORY) {
1978 type = FILETYPE_DIRECTORY;
Michael Wallec07cdcb2016-08-29 10:46:44 +02001979 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankar71014b62012-05-25 21:21:44 +05301980 & FILETYPE_INO_MASK) ==
1981 FILETYPE_INO_SYMLINK) {
1982 type = FILETYPE_SYMLINK;
Michael Wallec07cdcb2016-08-29 10:46:44 +02001983 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankar71014b62012-05-25 21:21:44 +05301984 & FILETYPE_INO_MASK) ==
1985 FILETYPE_INO_REG) {
1986 type = FILETYPE_REG;
1987 }
1988 }
1989#ifdef DEBUG
1990 printf("iterate >%s<\n", filename);
1991#endif /* of DEBUG */
1992 if ((name != NULL) && (fnode != NULL)
1993 && (ftype != NULL)) {
1994 if (strcmp(filename, name) == 0) {
1995 *ftype = type;
1996 *fnode = fdiro;
1997 return 1;
1998 }
1999 } else {
2000 if (fdiro->inode_read == 0) {
2001 status = ext4fs_read_inode(diro->data,
Michael Wallec07cdcb2016-08-29 10:46:44 +02002002 le32_to_cpu(
Uma Shankar71014b62012-05-25 21:21:44 +05302003 dirent.inode),
2004 &fdiro->inode);
2005 if (status == 0) {
2006 free(fdiro);
2007 return 0;
2008 }
2009 fdiro->inode_read = 1;
2010 }
2011 switch (type) {
2012 case FILETYPE_DIRECTORY:
2013 printf("<DIR> ");
2014 break;
2015 case FILETYPE_SYMLINK:
2016 printf("<SYM> ");
2017 break;
2018 case FILETYPE_REG:
2019 printf(" ");
2020 break;
2021 default:
2022 printf("< ? > ");
2023 break;
2024 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002025 printf("%10u %s\n",
Michael Wallec07cdcb2016-08-29 10:46:44 +02002026 le32_to_cpu(fdiro->inode.size),
Uma Shankar71014b62012-05-25 21:21:44 +05302027 filename);
2028 }
2029 free(fdiro);
2030 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02002031 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankar71014b62012-05-25 21:21:44 +05302032 }
2033 return 0;
2034}
2035
2036static char *ext4fs_read_symlink(struct ext2fs_node *node)
2037{
2038 char *symlink;
2039 struct ext2fs_node *diro = node;
2040 int status;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002041 loff_t actread;
Uma Shankar71014b62012-05-25 21:21:44 +05302042
2043 if (!diro->inode_read) {
2044 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2045 if (status == 0)
Michael Walle13179c22016-09-01 11:21:40 +02002046 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05302047 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02002048 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankar71014b62012-05-25 21:21:44 +05302049 if (!symlink)
Michael Walle13179c22016-09-01 11:21:40 +02002050 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05302051
Michael Wallec07cdcb2016-08-29 10:46:44 +02002052 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankar71014b62012-05-25 21:21:44 +05302053 strncpy(symlink, diro->inode.b.symlink,
Michael Wallec07cdcb2016-08-29 10:46:44 +02002054 le32_to_cpu(diro->inode.size));
Uma Shankar71014b62012-05-25 21:21:44 +05302055 } else {
2056 status = ext4fs_read_file(diro, 0,
Michael Wallec07cdcb2016-08-29 10:46:44 +02002057 le32_to_cpu(diro->inode.size),
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002058 symlink, &actread);
Gary Bisson086e0112015-09-07 11:20:07 +02002059 if ((status < 0) || (actread == 0)) {
Uma Shankar71014b62012-05-25 21:21:44 +05302060 free(symlink);
Michael Walle13179c22016-09-01 11:21:40 +02002061 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05302062 }
2063 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02002064 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankar71014b62012-05-25 21:21:44 +05302065 return symlink;
2066}
2067
2068static int ext4fs_find_file1(const char *currpath,
2069 struct ext2fs_node *currroot,
2070 struct ext2fs_node **currfound, int *foundtype)
2071{
2072 char fpath[strlen(currpath) + 1];
2073 char *name = fpath;
2074 char *next;
2075 int status;
2076 int type = FILETYPE_DIRECTORY;
2077 struct ext2fs_node *currnode = currroot;
2078 struct ext2fs_node *oldnode = currroot;
2079
2080 strncpy(fpath, currpath, strlen(currpath) + 1);
2081
2082 /* Remove all leading slashes. */
2083 while (*name == '/')
2084 name++;
2085
2086 if (!*name) {
2087 *currfound = currnode;
2088 return 1;
2089 }
2090
2091 for (;;) {
2092 int found;
2093
2094 /* Extract the actual part from the pathname. */
2095 next = strchr(name, '/');
2096 if (next) {
2097 /* Remove all leading slashes. */
2098 while (*next == '/')
2099 *(next++) = '\0';
2100 }
2101
2102 if (type != FILETYPE_DIRECTORY) {
2103 ext4fs_free_node(currnode, currroot);
2104 return 0;
2105 }
2106
2107 oldnode = currnode;
2108
2109 /* Iterate over the directory. */
2110 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2111 if (found == 0)
2112 return 0;
2113
2114 if (found == -1)
2115 break;
2116
2117 /* Read in the symlink and follow it. */
2118 if (type == FILETYPE_SYMLINK) {
2119 char *symlink;
2120
2121 /* Test if the symlink does not loop. */
2122 if (++symlinknest == 8) {
2123 ext4fs_free_node(currnode, currroot);
2124 ext4fs_free_node(oldnode, currroot);
2125 return 0;
2126 }
2127
2128 symlink = ext4fs_read_symlink(currnode);
2129 ext4fs_free_node(currnode, currroot);
2130
2131 if (!symlink) {
2132 ext4fs_free_node(oldnode, currroot);
2133 return 0;
2134 }
2135
2136 debug("Got symlink >%s<\n", symlink);
2137
2138 if (symlink[0] == '/') {
2139 ext4fs_free_node(oldnode, currroot);
2140 oldnode = &ext4fs_root->diropen;
2141 }
2142
2143 /* Lookup the node the symlink points to. */
2144 status = ext4fs_find_file1(symlink, oldnode,
2145 &currnode, &type);
2146
2147 free(symlink);
2148
2149 if (status == 0) {
2150 ext4fs_free_node(oldnode, currroot);
2151 return 0;
2152 }
2153 }
2154
2155 ext4fs_free_node(oldnode, currroot);
2156
2157 /* Found the node! */
2158 if (!next || *next == '\0') {
2159 *currfound = currnode;
2160 *foundtype = type;
2161 return 1;
2162 }
2163 name = next;
2164 }
2165 return -1;
2166}
2167
2168int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2169 struct ext2fs_node **foundnode, int expecttype)
2170{
2171 int status;
2172 int foundtype = FILETYPE_DIRECTORY;
2173
2174 symlinknest = 0;
2175 if (!path)
2176 return 0;
2177
2178 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2179 if (status == 0)
2180 return 0;
2181
2182 /* Check if the node that was found was of the expected type. */
2183 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2184 return 0;
2185 else if ((expecttype == FILETYPE_DIRECTORY)
2186 && (foundtype != expecttype))
2187 return 0;
2188
2189 return 1;
2190}
2191
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002192int ext4fs_open(const char *filename, loff_t *len)
Uma Shankar71014b62012-05-25 21:21:44 +05302193{
2194 struct ext2fs_node *fdiro = NULL;
2195 int status;
Uma Shankar71014b62012-05-25 21:21:44 +05302196
2197 if (ext4fs_root == NULL)
2198 return -1;
2199
2200 ext4fs_file = NULL;
2201 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2202 FILETYPE_REG);
2203 if (status == 0)
2204 goto fail;
2205
2206 if (!fdiro->inode_read) {
2207 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2208 &fdiro->inode);
2209 if (status == 0)
2210 goto fail;
2211 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02002212 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankar71014b62012-05-25 21:21:44 +05302213 ext4fs_file = fdiro;
2214
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002215 return 0;
Uma Shankar71014b62012-05-25 21:21:44 +05302216fail:
2217 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2218
2219 return -1;
2220}
2221
2222int ext4fs_mount(unsigned part_length)
2223{
2224 struct ext2_data *data;
2225 int status;
2226 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-05-01 01:13:19 +00002227 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankar71014b62012-05-25 21:21:44 +05302228 if (!data)
2229 return 0;
2230
2231 /* Read the superblock. */
Egbert Eich7b1b2552013-05-01 01:13:19 +00002232 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankar71014b62012-05-25 21:21:44 +05302233
2234 if (status == 0)
2235 goto fail;
2236
2237 /* Make sure this is an ext2 filesystem. */
Michael Wallec07cdcb2016-08-29 10:46:44 +02002238 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Uma Shankar71014b62012-05-25 21:21:44 +05302239 goto fail;
2240
Tom Rini44720652016-07-22 17:59:11 -04002241 /*
2242 * The 64bit feature was enabled when metadata_csum was enabled
2243 * and we do not support metadata_csum (and cannot reliably find
2244 * files when it is set. Refuse to mount.
2245 */
Michael Walle13179c22016-09-01 11:21:40 +02002246 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
Tom Rini44720652016-07-22 17:59:11 -04002247 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2248 goto fail;
2249 }
2250
Michael Walle59c9a8b2016-08-29 10:46:46 +02002251 if (le32_to_cpu(data->sblock.revision_level) == 0)
Uma Shankar71014b62012-05-25 21:21:44 +05302252 fs->inodesz = 128;
2253 else
Michael Wallec07cdcb2016-08-29 10:46:44 +02002254 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
Uma Shankar71014b62012-05-25 21:21:44 +05302255
2256 debug("EXT2 rev %d, inode_size %d\n",
Michael Wallec07cdcb2016-08-29 10:46:44 +02002257 le32_to_cpu(data->sblock.revision_level), fs->inodesz);
Uma Shankar71014b62012-05-25 21:21:44 +05302258
2259 data->diropen.data = data;
2260 data->diropen.ino = 2;
2261 data->diropen.inode_read = 1;
2262 data->inode = &data->diropen.inode;
2263
2264 status = ext4fs_read_inode(data, 2, data->inode);
2265 if (status == 0)
2266 goto fail;
2267
2268 ext4fs_root = data;
2269
2270 return 1;
2271fail:
2272 printf("Failed to mount ext2 filesystem...\n");
2273 free(data);
2274 ext4fs_root = NULL;
2275
2276 return 0;
2277}