blob: e3cc30a1e0ae73b3741d26662b90a5efb211ee98 [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)
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +020050struct ext2_block_group *ext4fs_get_group_descriptor
51 (const struct ext_filesystem *fs, uint32_t bg_idx)
52{
53 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
54}
55
Michael Walle58a9ecb2016-09-01 11:21:40 +020056static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
57{
58 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
59}
60
61static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
62{
Stefan Brüns749e93e2016-09-20 01:13:01 +020063 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
64 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
65 free_blocks--;
66
67 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
68 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
Michael Walle58a9ecb2016-09-01 11:21:40 +020069}
70
Stefan Brüns749e93e2016-09-20 01:13:01 +020071static inline void ext4fs_bg_free_inodes_dec
72 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020073{
Stefan Brüns749e93e2016-09-20 01:13:01 +020074 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
75 if (fs->gdsize == 64)
76 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
77 free_inodes--;
78
79 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
80 if (fs->gdsize == 64)
81 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020082}
83
Stefan Brüns749e93e2016-09-20 01:13:01 +020084static inline void ext4fs_bg_free_blocks_dec
85 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020086{
Stefan Brüns749e93e2016-09-20 01:13:01 +020087 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
88 if (fs->gdsize == 64)
89 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
90 free_blocks--;
91
92 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
93 if (fs->gdsize == 64)
94 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020095}
96
Stefan Brüns749e93e2016-09-20 01:13:01 +020097static inline void ext4fs_bg_itable_unused_dec
98 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020099{
Stefan Brüns749e93e2016-09-20 01:13:01 +0200100 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
101 if (fs->gdsize == 64)
102 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
103 free_inodes--;
104
105 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
106 if (fs->gdsize == 64)
107 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200108}
109
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +0200110uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
111{
112 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
113 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
114 return free_blocks;
115}
116
117void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
118{
119 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
120 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
121}
122
123uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
124 const struct ext_filesystem *fs)
125{
126 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
127 if (fs->gdsize == 64)
128 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
129 return free_blocks;
130}
131
132static inline
133uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
134 const struct ext_filesystem *fs)
135{
136 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
137 if (fs->gdsize == 64)
138 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
139 return free_inodes;
140}
141
142static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
143{
144 return le16_to_cpu(bg->bg_flags);
145}
146
147static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
148 uint16_t flags)
149{
150 bg->bg_flags = cpu_to_le16(flags);
151}
152
153/* Block number of the block bitmap */
154uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
155 const struct ext_filesystem *fs)
156{
157 uint64_t block_nr = le32_to_cpu(bg->block_id);
158 if (fs->gdsize == 64)
159 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
160 return block_nr;
161}
162
163/* Block number of the inode bitmap */
164uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
165 const struct ext_filesystem *fs)
166{
167 uint64_t block_nr = le32_to_cpu(bg->inode_id);
168 if (fs->gdsize == 64)
169 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
170 return block_nr;
171}
172#endif
173
174/* Block number of the inode table */
175uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
176 const struct ext_filesystem *fs)
177{
178 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
179 if (fs->gdsize == 64)
180 block_nr +=
181 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
182 return block_nr;
183}
184
185#if defined(CONFIG_EXT4_WRITE)
Uma Shankared34f342012-05-25 21:22:49 +0530186uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
187{
188 uint32_t res = size / n;
189 if (res * n != size)
190 res++;
191
192 return res;
193}
194
195void put_ext4(uint64_t off, void *buf, uint32_t size)
196{
197 uint64_t startblock;
198 uint64_t remainder;
199 unsigned char *temp_ptr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530200 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +0000201 int log2blksz = fs->dev_desc->log2blksz;
202 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530203
Egbert Eich50ce4c02013-05-01 01:13:19 +0000204 startblock = off >> log2blksz;
Uma Shankared34f342012-05-25 21:22:49 +0530205 startblock += part_offset;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000206 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankared34f342012-05-25 21:22:49 +0530207
208 if (fs->dev_desc == NULL)
209 return;
210
Egbert Eich50ce4c02013-05-01 01:13:19 +0000211 if ((startblock + (size >> log2blksz)) >
Uma Shankared34f342012-05-25 21:22:49 +0530212 (part_offset + fs->total_sect)) {
Frederic Leroy04735e92013-06-26 18:11:25 +0200213 printf("part_offset is " LBAFU "\n", part_offset);
Simon Glassaac618a2014-10-15 04:38:32 -0600214 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
Uma Shankared34f342012-05-25 21:22:49 +0530215 printf("error: overflow occurs\n");
216 return;
217 }
218
219 if (remainder) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700220 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
221 temp_ptr = sec_buf;
222 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
223 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530224 } else {
Egbert Eich50ce4c02013-05-01 01:13:19 +0000225 if (size >> log2blksz != 0) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700226 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
227 (unsigned long *)buf);
Uma Shankared34f342012-05-25 21:22:49 +0530228 } else {
Simon Glass2a981dc2016-02-29 15:25:52 -0700229 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530230 temp_ptr = sec_buf;
231 memcpy(temp_ptr, buf, size);
Simon Glass2a981dc2016-02-29 15:25:52 -0700232 blk_dwrite(fs->dev_desc, startblock, 1,
233 (unsigned long *)sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530234 }
235 }
236}
237
238static int _get_new_inode_no(unsigned char *buffer)
239{
240 struct ext_filesystem *fs = get_fs();
241 unsigned char input;
242 int operand, status;
243 int count = 1;
244 int j = 0;
245
246 /* get the blocksize of the filesystem */
247 unsigned char *ptr = buffer;
248 while (*ptr == 255) {
249 ptr++;
250 count += 8;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200251 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankared34f342012-05-25 21:22:49 +0530252 return -1;
253 }
254
255 for (j = 0; j < fs->blksz; j++) {
256 input = *ptr;
257 int i = 0;
258 while (i <= 7) {
259 operand = 1 << i;
260 status = input & operand;
261 if (status) {
262 i++;
263 count++;
264 } else {
265 *ptr |= operand;
266 return count;
267 }
268 }
269 ptr = ptr + 1;
270 }
271
272 return -1;
273}
274
275static int _get_new_blk_no(unsigned char *buffer)
276{
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200277 int operand;
Uma Shankared34f342012-05-25 21:22:49 +0530278 int count = 0;
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200279 int i;
Uma Shankared34f342012-05-25 21:22:49 +0530280 unsigned char *ptr = buffer;
281 struct ext_filesystem *fs = get_fs();
282
Uma Shankared34f342012-05-25 21:22:49 +0530283 while (*ptr == 255) {
284 ptr++;
285 count += 8;
286 if (count == (fs->blksz * 8))
287 return -1;
288 }
289
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200290 if (fs->blksz == 1024)
291 count += 1;
292
293 for (i = 0; i <= 7; i++) {
294 operand = 1 << i;
295 if (*ptr & operand) {
296 count++;
297 } else {
298 *ptr |= operand;
299 return count;
Uma Shankared34f342012-05-25 21:22:49 +0530300 }
Uma Shankared34f342012-05-25 21:22:49 +0530301 }
302
303 return -1;
304}
305
306int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
307{
308 int i, remainder, status;
309 unsigned char *ptr = buffer;
310 unsigned char operand;
311 i = blockno / 8;
312 remainder = blockno % 8;
313 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
314
315 i = i - (index * blocksize);
316 if (blocksize != 1024) {
317 ptr = ptr + i;
318 operand = 1 << remainder;
319 status = *ptr & operand;
320 if (status)
321 return -1;
322
323 *ptr = *ptr | operand;
324 return 0;
325 } else {
326 if (remainder == 0) {
327 ptr = ptr + i - 1;
328 operand = (1 << 7);
329 } else {
330 ptr = ptr + i;
331 operand = (1 << (remainder - 1));
332 }
333 status = *ptr & operand;
334 if (status)
335 return -1;
336
337 *ptr = *ptr | operand;
338 return 0;
339 }
340}
341
342void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
343{
344 int i, remainder, status;
345 unsigned char *ptr = buffer;
346 unsigned char operand;
347 i = blockno / 8;
348 remainder = blockno % 8;
349 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
350
351 i = i - (index * blocksize);
352 if (blocksize != 1024) {
353 ptr = ptr + i;
354 operand = (1 << remainder);
355 status = *ptr & operand;
356 if (status)
357 *ptr = *ptr & ~(operand);
358 } else {
359 if (remainder == 0) {
360 ptr = ptr + i - 1;
361 operand = (1 << 7);
362 } else {
363 ptr = ptr + i;
364 operand = (1 << (remainder - 1));
365 }
366 status = *ptr & operand;
367 if (status)
368 *ptr = *ptr & ~(operand);
369 }
370}
371
372int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
373{
374 int i, remainder, status;
375 unsigned char *ptr = buffer;
376 unsigned char operand;
377
Michael Walle58a9ecb2016-09-01 11:21:40 +0200378 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530379 i = inode_no / 8;
380 remainder = inode_no % 8;
381 if (remainder == 0) {
382 ptr = ptr + i - 1;
383 operand = (1 << 7);
384 } else {
385 ptr = ptr + i;
386 operand = (1 << (remainder - 1));
387 }
388 status = *ptr & operand;
389 if (status)
390 return -1;
391
392 *ptr = *ptr | operand;
393
394 return 0;
395}
396
397void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
398{
399 int i, remainder, status;
400 unsigned char *ptr = buffer;
401 unsigned char operand;
402
Michael Walle58a9ecb2016-09-01 11:21:40 +0200403 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530404 i = inode_no / 8;
405 remainder = inode_no % 8;
406 if (remainder == 0) {
407 ptr = ptr + i - 1;
408 operand = (1 << 7);
409 } else {
410 ptr = ptr + i;
411 operand = (1 << (remainder - 1));
412 }
413 status = *ptr & operand;
414 if (status)
415 *ptr = *ptr & ~(operand);
416}
417
Michael Walle58a9ecb2016-09-01 11:21:40 +0200418uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankared34f342012-05-25 21:22:49 +0530419{
420 struct ext2_block_group *desc;
421 struct ext_filesystem *fs = get_fs();
Michael Walle58a9ecb2016-09-01 11:21:40 +0200422 uint16_t crc = 0;
423 __le32 le32_i = cpu_to_le32(i);
Uma Shankared34f342012-05-25 21:22:49 +0530424
Stefan Brüns688d0e72016-09-17 02:10:10 +0200425 desc = ext4fs_get_group_descriptor(fs, i);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200426 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankared34f342012-05-25 21:22:49 +0530427 int offset = offsetof(struct ext2_block_group, bg_checksum);
428
429 crc = ext2fs_crc16(~0, fs->sb->unique_id,
430 sizeof(fs->sb->unique_id));
Michael Walle58a9ecb2016-09-01 11:21:40 +0200431 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
Uma Shankared34f342012-05-25 21:22:49 +0530432 crc = ext2fs_crc16(crc, desc, offset);
433 offset += sizeof(desc->bg_checksum); /* skip checksum */
434 assert(offset == sizeof(*desc));
Tuomas Tynkkynen385b7312017-09-25 22:06:31 +0300435 if (offset < fs->gdsize) {
436 crc = ext2fs_crc16(crc, (__u8 *)desc + offset,
437 fs->gdsize - offset);
438 }
Uma Shankared34f342012-05-25 21:22:49 +0530439 }
440
441 return crc;
442}
443
444static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
445{
446 int dentry_length;
447 int sizeof_void_space;
448 int new_entry_byte_reqd;
449 short padding_factor = 0;
450
451 if (dir->namelen % 4 != 0)
452 padding_factor = 4 - (dir->namelen % 4);
453
454 dentry_length = sizeof(struct ext2_dirent) +
455 dir->namelen + padding_factor;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200456 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankared34f342012-05-25 21:22:49 +0530457 if (sizeof_void_space == 0)
458 return 0;
459
460 padding_factor = 0;
461 if (strlen(filename) % 4 != 0)
462 padding_factor = 4 - (strlen(filename) % 4);
463
464 new_entry_byte_reqd = strlen(filename) +
465 sizeof(struct ext2_dirent) + padding_factor;
466 if (sizeof_void_space >= new_entry_byte_reqd) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200467 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankared34f342012-05-25 21:22:49 +0530468 return sizeof_void_space;
469 }
470
471 return 0;
472}
473
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200474int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankared34f342012-05-25 21:22:49 +0530475{
476 unsigned int *zero_buffer = NULL;
477 char *root_first_block_buffer = NULL;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200478 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530479 long int first_block_no_of_root = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530480 int totalbytes = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530481 unsigned int new_entry_byte_reqd;
Uma Shankared34f342012-05-25 21:22:49 +0530482 int sizeof_void_space = 0;
483 int templength = 0;
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200484 int inodeno = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530485 int status;
486 struct ext_filesystem *fs = get_fs();
487 /* directory entry */
488 struct ext2_dirent *dir;
Uma Shankared34f342012-05-25 21:22:49 +0530489 char *temp_dir = NULL;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200490 uint32_t new_blk_no;
491 uint32_t new_size;
492 uint32_t new_blockcnt;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200493 uint32_t directory_blocks;
Uma Shankared34f342012-05-25 21:22:49 +0530494
495 zero_buffer = zalloc(fs->blksz);
496 if (!zero_buffer) {
497 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200498 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530499 }
500 root_first_block_buffer = zalloc(fs->blksz);
501 if (!root_first_block_buffer) {
502 free(zero_buffer);
503 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200504 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530505 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200506 new_entry_byte_reqd = ROUND(strlen(filename) +
507 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530508restart:
Stefan Brünsa321abd2016-09-06 04:36:44 +0200509 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
510 LOG2_BLOCK_SIZE(ext4fs_root);
511 blk_idx = directory_blocks - 1;
Uma Shankared34f342012-05-25 21:22:49 +0530512
Stefan Brünsa321abd2016-09-06 04:36:44 +0200513restart_read:
Uma Shankared34f342012-05-25 21:22:49 +0530514 /* read the block no allocated to a file */
Stefan Brünsa321abd2016-09-06 04:36:44 +0200515 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
516 if (first_block_no_of_root <= 0)
517 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530518
Frederic Leroy04735e92013-06-26 18:11:25 +0200519 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankared34f342012-05-25 21:22:49 +0530520 * fs->sect_perblk,
521 0, fs->blksz, root_first_block_buffer);
522 if (status == 0)
523 goto fail;
524
525 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
526 goto fail;
527 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankared34f342012-05-25 21:22:49 +0530528 totalbytes = 0;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200529
Michael Walle58a9ecb2016-09-01 11:21:40 +0200530 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsa321abd2016-09-06 04:36:44 +0200531 unsigned short used_len = ROUND(dir->namelen +
532 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530533
Stefan Brünsa321abd2016-09-06 04:36:44 +0200534 /* last entry of block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200535 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankared34f342012-05-25 21:22:49 +0530536
Stefan Brünsa321abd2016-09-06 04:36:44 +0200537 /* check if new entry fits */
538 if ((used_len + new_entry_byte_reqd) <=
539 le16_to_cpu(dir->direntlen)) {
540 dir->direntlen = cpu_to_le16(used_len);
541 break;
542 } else {
543 if (blk_idx > 0) {
544 printf("Block full, trying previous\n");
545 blk_idx--;
546 goto restart_read;
547 }
548 printf("All blocks full: Allocate new\n");
Uma Shankared34f342012-05-25 21:22:49 +0530549
Stefan Brünsb96c3c72016-09-06 04:36:43 +0200550 if (le32_to_cpu(g_parent_inode->flags) &
551 EXT4_EXTENTS_FL) {
552 printf("Directory uses extents\n");
553 goto fail;
554 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200555 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankared34f342012-05-25 21:22:49 +0530556 printf("Directory exceeds limit\n");
557 goto fail;
558 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200559 new_blk_no = ext4fs_get_new_blk_no();
560 if (new_blk_no == -1) {
Uma Shankared34f342012-05-25 21:22:49 +0530561 printf("no block left to assign\n");
562 goto fail;
563 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200564 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200565 g_parent_inode->b.blocks.
566 dir_blocks[directory_blocks] =
Michael Walle58a9ecb2016-09-01 11:21:40 +0200567 cpu_to_le32(new_blk_no);
568
569 new_size = le32_to_cpu(g_parent_inode->size);
570 new_size += fs->blksz;
571 g_parent_inode->size = cpu_to_le32(new_size);
572
573 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
574 new_blockcnt += fs->sect_perblk;
575 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
576
Uma Shankared34f342012-05-25 21:22:49 +0530577 if (ext4fs_put_metadata
578 (root_first_block_buffer,
579 first_block_no_of_root))
580 goto fail;
581 goto restart;
582 }
Uma Shankared34f342012-05-25 21:22:49 +0530583 }
584
Michael Walle58a9ecb2016-09-01 11:21:40 +0200585 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530586 totalbytes = totalbytes + templength;
587 sizeof_void_space = check_void_in_dentry(dir, filename);
588 if (sizeof_void_space)
589 break;
590
591 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530592 }
593
594 /* make a pointer ready for creating next directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200595 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530596 totalbytes = totalbytes + templength;
597 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530598
599 /* get the next available inode number */
600 inodeno = ext4fs_get_new_inode_no();
601 if (inodeno == -1) {
602 printf("no inode left to assign\n");
603 goto fail;
604 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200605 dir->inode = cpu_to_le32(inodeno);
Uma Shankared34f342012-05-25 21:22:49 +0530606 if (sizeof_void_space)
Michael Walle58a9ecb2016-09-01 11:21:40 +0200607 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankared34f342012-05-25 21:22:49 +0530608 else
Michael Walle58a9ecb2016-09-01 11:21:40 +0200609 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankared34f342012-05-25 21:22:49 +0530610
611 dir->namelen = strlen(filename);
612 dir->filetype = FILETYPE_REG; /* regular file */
613 temp_dir = (char *)dir;
614 temp_dir = temp_dir + sizeof(struct ext2_dirent);
615 memcpy(temp_dir, filename, strlen(filename));
616
Uma Shankared34f342012-05-25 21:22:49 +0530617 /* update or write the 1st block of root inode */
618 if (ext4fs_put_metadata(root_first_block_buffer,
619 first_block_no_of_root))
620 goto fail;
621
622fail:
623 free(zero_buffer);
624 free(root_first_block_buffer);
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200625
626 return inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530627}
628
629static int search_dir(struct ext2_inode *parent_inode, char *dirname)
630{
631 int status;
Stefan Brüns76a29512016-09-06 04:36:41 +0200632 int inodeno = 0;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200633 int offset;
634 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530635 long int blknr;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200636 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530637 struct ext2_dirent *dir = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530638 struct ext_filesystem *fs = get_fs();
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200639 uint32_t directory_blocks;
640 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530641
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200642 directory_blocks = le32_to_cpu(parent_inode->size) >>
643 LOG2_BLOCK_SIZE(ext4fs_root);
644
645 block_buffer = zalloc(fs->blksz);
646 if (!block_buffer)
647 goto fail;
648
649 /* get the block no allocated to a file */
650 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
651 blknr = read_allocated_block(parent_inode, blk_idx);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200652 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530653 goto fail;
654
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200655 /* read the directory block */
Frederic Leroy04735e92013-06-26 18:11:25 +0200656 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +0530657 0, fs->blksz, (char *)block_buffer);
658 if (status == 0)
659 goto fail;
660
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200661 offset = 0;
662 do {
Ian Rayecdfb412017-11-08 15:35:10 +0000663 if (offset & 3) {
664 printf("Badly aligned ext2_dirent\n");
665 break;
666 }
667
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200668 dir = (struct ext2_dirent *)(block_buffer + offset);
669 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankared34f342012-05-25 21:22:49 +0530670
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200671 int direntlen = le16_to_cpu(dir->direntlen);
672 if (direntlen < sizeof(struct ext2_dirent))
Uma Shankared34f342012-05-25 21:22:49 +0530673 break;
674
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200675 if (dir->inode && (strlen(dirname) == dir->namelen) &&
676 (strncmp(dirname, direntname, dir->namelen) == 0)) {
677 inodeno = le32_to_cpu(dir->inode);
678 break;
679 }
Uma Shankared34f342012-05-25 21:22:49 +0530680
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200681 offset += direntlen;
Stefan Brüns76a29512016-09-06 04:36:41 +0200682
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200683 } while (offset < fs->blksz);
684
685 if (inodeno > 0) {
686 free(block_buffer);
Stefan Brüns76a29512016-09-06 04:36:41 +0200687 return inodeno;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200688 }
Uma Shankared34f342012-05-25 21:22:49 +0530689 }
690
691fail:
692 free(block_buffer);
693
694 return -1;
695}
696
697static int find_dir_depth(char *dirname)
698{
699 char *token = strtok(dirname, "/");
700 int count = 0;
701 while (token != NULL) {
702 token = strtok(NULL, "/");
703 count++;
704 }
705 return count + 1 + 1;
706 /*
707 * for example for string /home/temp
708 * depth=home(1)+temp(1)+1 extra for NULL;
709 * so count is 4;
710 */
711}
712
713static int parse_path(char **arr, char *dirname)
714{
715 char *token = strtok(dirname, "/");
716 int i = 0;
717
718 /* add root */
719 arr[i] = zalloc(strlen("/") + 1);
720 if (!arr[i])
721 return -ENOMEM;
Stephen Warren934b14f2015-09-04 22:03:44 -0600722 memcpy(arr[i++], "/", strlen("/"));
Uma Shankared34f342012-05-25 21:22:49 +0530723
724 /* add each path entry after root */
725 while (token != NULL) {
726 arr[i] = zalloc(strlen(token) + 1);
727 if (!arr[i])
728 return -ENOMEM;
729 memcpy(arr[i++], token, strlen(token));
730 token = strtok(NULL, "/");
731 }
732 arr[i] = NULL;
733
734 return 0;
735}
736
737int ext4fs_iget(int inode_no, struct ext2_inode *inode)
738{
739 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
740 return -1;
741
742 return 0;
743}
744
745/*
746 * Function: ext4fs_get_parent_inode_num
747 * Return Value: inode Number of the parent directory of file/Directory to be
748 * created
749 * dirname : Input parmater, input path name of the file/directory to be created
750 * dname : Output parameter, to be filled with the name of the directory
751 * extracted from dirname
752 */
753int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
754{
755 int i;
756 int depth = 0;
757 int matched_inode_no;
758 int result_inode_no = -1;
759 char **ptr = NULL;
760 char *depth_dirname = NULL;
761 char *parse_dirname = NULL;
762 struct ext2_inode *parent_inode = NULL;
763 struct ext2_inode *first_inode = NULL;
764 struct ext2_inode temp_inode;
765
766 if (*dirname != '/') {
767 printf("Please supply Absolute path\n");
768 return -1;
769 }
770
771 /* TODO: input validation make equivalent to linux */
772 depth_dirname = zalloc(strlen(dirname) + 1);
773 if (!depth_dirname)
774 return -ENOMEM;
775
776 memcpy(depth_dirname, dirname, strlen(dirname));
777 depth = find_dir_depth(depth_dirname);
778 parse_dirname = zalloc(strlen(dirname) + 1);
779 if (!parse_dirname)
780 goto fail;
781 memcpy(parse_dirname, dirname, strlen(dirname));
782
783 /* allocate memory for each directory level */
784 ptr = zalloc((depth) * sizeof(char *));
785 if (!ptr)
786 goto fail;
787 if (parse_path(ptr, parse_dirname))
788 goto fail;
789 parent_inode = zalloc(sizeof(struct ext2_inode));
790 if (!parent_inode)
791 goto fail;
792 first_inode = zalloc(sizeof(struct ext2_inode));
793 if (!first_inode)
794 goto fail;
795 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
796 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
797 if (flags & F_FILE)
798 result_inode_no = EXT2_ROOT_INO;
799 for (i = 1; i < depth; i++) {
800 matched_inode_no = search_dir(parent_inode, ptr[i]);
801 if (matched_inode_no == -1) {
802 if (ptr[i + 1] == NULL && i == 1) {
803 result_inode_no = EXT2_ROOT_INO;
804 goto end;
805 } else {
806 if (ptr[i + 1] == NULL)
807 break;
808 printf("Invalid path\n");
809 result_inode_no = -1;
810 goto fail;
811 }
812 } else {
813 if (ptr[i + 1] != NULL) {
814 memset(parent_inode, '\0',
815 sizeof(struct ext2_inode));
816 if (ext4fs_iget(matched_inode_no,
817 parent_inode)) {
818 result_inode_no = -1;
819 goto fail;
820 }
821 result_inode_no = matched_inode_no;
822 } else {
823 break;
824 }
825 }
826 }
827
828end:
829 if (i == 1)
830 matched_inode_no = search_dir(first_inode, ptr[i]);
831 else
832 matched_inode_no = search_dir(parent_inode, ptr[i]);
833
834 if (matched_inode_no != -1) {
835 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200836 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankared34f342012-05-25 21:22:49 +0530837 printf("It is a Directory\n");
838 result_inode_no = -1;
839 goto fail;
840 }
841 }
842
843 if (strlen(ptr[i]) > 256) {
844 result_inode_no = -1;
845 goto fail;
846 }
847 memcpy(dname, ptr[i], strlen(ptr[i]));
848
849fail:
850 free(depth_dirname);
851 free(parse_dirname);
Stephen Warren934b14f2015-09-04 22:03:44 -0600852 for (i = 0; i < depth; i++) {
853 if (!ptr[i])
854 break;
855 free(ptr[i]);
856 }
Uma Shankared34f342012-05-25 21:22:49 +0530857 free(ptr);
858 free(parent_inode);
859 free(first_inode);
860
861 return result_inode_no;
862}
863
Stefan Brüns76a29512016-09-06 04:36:41 +0200864static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankared34f342012-05-25 21:22:49 +0530865{
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200866 int status;
867 int inodeno = 0;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200868 int offset;
869 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530870 struct ext2_dirent *dir = NULL;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200871 struct ext2_dirent *previous_dir;
Uma Shankared34f342012-05-25 21:22:49 +0530872 struct ext_filesystem *fs = get_fs();
Stephen Warrend56b2012015-09-04 22:03:45 -0600873 int ret = -1;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200874 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530875
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200876 block_buffer = zalloc(fs->blksz);
877 if (!block_buffer)
Uma Shankared34f342012-05-25 21:22:49 +0530878 return -ENOMEM;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200879
880 /* read the directory block */
Stefan Brüns76a29512016-09-06 04:36:41 +0200881 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200882 fs->blksz, block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530883 if (status == 0)
884 goto fail;
885
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200886 offset = 0;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200887 do {
Ian Rayecdfb412017-11-08 15:35:10 +0000888 if (offset & 3) {
889 printf("Badly aligned ext2_dirent\n");
890 break;
891 }
892
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200893 previous_dir = dir;
894 dir = (struct ext2_dirent *)(block_buffer + offset);
895 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
896
897 int direntlen = le16_to_cpu(dir->direntlen);
898 if (direntlen < sizeof(struct ext2_dirent))
899 break;
900
Stefan Brüns76a29512016-09-06 04:36:41 +0200901 if (dir->inode && (strlen(filename) == dir->namelen) &&
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200902 (strncmp(direntname, filename, dir->namelen) == 0)) {
Stefan Brüns76a29512016-09-06 04:36:41 +0200903 inodeno = le32_to_cpu(dir->inode);
Stefan Brüns76a29512016-09-06 04:36:41 +0200904 break;
Uma Shankared34f342012-05-25 21:22:49 +0530905 }
906
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200907 offset += direntlen;
Uma Shankared34f342012-05-25 21:22:49 +0530908
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200909 } while (offset < fs->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530910
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200911 if (inodeno > 0) {
Stefan Brüns805e3e002016-10-09 20:15:28 +0200912 printf("file found, deleting\n");
913 if (ext4fs_log_journal(block_buffer, blknr))
914 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530915
Stefan Brüns805e3e002016-10-09 20:15:28 +0200916 if (previous_dir) {
917 /* merge dir entry with predecessor */
918 uint16_t new_len;
919 new_len = le16_to_cpu(previous_dir->direntlen);
920 new_len += le16_to_cpu(dir->direntlen);
921 previous_dir->direntlen = cpu_to_le16(new_len);
922 } else {
923 /* invalidate dir entry */
924 dir->inode = 0;
925 }
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200926 if (ext4fs_put_metadata(block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530927 goto fail;
Stephen Warrend56b2012015-09-04 22:03:45 -0600928 ret = inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530929 }
930fail:
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200931 free(block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530932
Stephen Warrend56b2012015-09-04 22:03:45 -0600933 return ret;
Uma Shankared34f342012-05-25 21:22:49 +0530934}
935
Stefan Brüns76a29512016-09-06 04:36:41 +0200936int ext4fs_filename_unlink(char *filename)
Uma Shankared34f342012-05-25 21:22:49 +0530937{
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200938 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530939 long int blknr = -1;
940 int inodeno = -1;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200941 uint32_t directory_blocks;
942
943 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
944 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankared34f342012-05-25 21:22:49 +0530945
946 /* read the block no allocated to a file */
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200947 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
948 blknr = read_allocated_block(g_parent_inode, blk_idx);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200949 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530950 break;
Stefan Brüns76a29512016-09-06 04:36:41 +0200951 inodeno = unlink_filename(filename, blknr);
Uma Shankared34f342012-05-25 21:22:49 +0530952 if (inodeno != -1)
953 return inodeno;
954 }
955
956 return -1;
957}
958
Michael Walle58a9ecb2016-09-01 11:21:40 +0200959uint32_t ext4fs_get_new_blk_no(void)
Uma Shankared34f342012-05-25 21:22:49 +0530960{
961 short i;
962 short status;
963 int remainder;
964 unsigned int bg_idx;
965 static int prev_bg_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200966 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankared34f342012-05-25 21:22:49 +0530967 struct ext_filesystem *fs = get_fs();
968 char *journal_buffer = zalloc(fs->blksz);
969 char *zero_buffer = zalloc(fs->blksz);
970 if (!journal_buffer || !zero_buffer)
971 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530972
973 if (fs->first_pass_bbmap == 0) {
974 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200975 struct ext2_block_group *bgd = NULL;
976 bgd = ext4fs_get_group_descriptor(fs, i);
977 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
978 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
979 uint64_t b_bitmap_blk =
980 ext4fs_bg_get_block_id(bgd, fs);
981 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +0530982 memcpy(fs->blk_bmaps[i], zero_buffer,
983 fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200984 put_ext4(b_bitmap_blk * fs->blksz,
985 fs->blk_bmaps[i], fs->blksz);
986 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
987 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +0530988 }
989 fs->curr_blkno =
990 _get_new_blk_no(fs->blk_bmaps[i]);
991 if (fs->curr_blkno == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +0200992 /* block bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +0530993 continue;
994 fs->curr_blkno = fs->curr_blkno +
995 (i * fs->blksz * 8);
996 fs->first_pass_bbmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +0200997 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200998 ext4fs_sb_free_blocks_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200999 status = ext4fs_devread(b_bitmap_blk *
1000 fs->sect_perblk,
1001 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301002 journal_buffer);
1003 if (status == 0)
1004 goto fail;
1005 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001006 b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301007 goto fail;
1008 goto success;
1009 } else {
1010 debug("no space left on block group %d\n", i);
1011 }
1012 }
1013
1014 goto fail;
1015 } else {
Uma Shankared34f342012-05-25 21:22:49 +05301016 fs->curr_blkno++;
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001017restart:
Uma Shankared34f342012-05-25 21:22:49 +05301018 /* get the blockbitmap index respective to blockno */
Łukasz Majewski35dd0552014-05-06 09:36:04 +02001019 bg_idx = fs->curr_blkno / blk_per_grp;
1020 if (fs->blksz == 1024) {
Uma Shankared34f342012-05-25 21:22:49 +05301021 remainder = fs->curr_blkno % blk_per_grp;
1022 if (!remainder)
1023 bg_idx--;
1024 }
1025
1026 /*
1027 * To skip completely filled block group bitmaps
1028 * Optimize the block allocation
1029 */
1030 if (bg_idx >= fs->no_blkgrp)
1031 goto fail;
1032
Stefan Brüns688d0e72016-09-17 02:10:10 +02001033 struct ext2_block_group *bgd = NULL;
1034 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1035 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
Uma Shankared34f342012-05-25 21:22:49 +05301036 debug("block group %u is full. Skipping\n", bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001037 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1038 if (fs->blksz == 1024)
1039 fs->curr_blkno += 1;
Uma Shankared34f342012-05-25 21:22:49 +05301040 goto restart;
1041 }
1042
Stefan Brüns688d0e72016-09-17 02:10:10 +02001043 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1044 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1045 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +05301046 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001047 put_ext4(b_bitmap_blk * fs->blksz,
1048 zero_buffer, fs->blksz);
1049 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1050 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301051 }
1052
1053 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1054 bg_idx) != 0) {
1055 debug("going for restart for the block no %ld %u\n",
1056 fs->curr_blkno, bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001057 fs->curr_blkno++;
Uma Shankared34f342012-05-25 21:22:49 +05301058 goto restart;
1059 }
1060
1061 /* journal backup */
1062 if (prev_bg_bitmap_index != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001063 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301064 0, fs->blksz, journal_buffer);
1065 if (status == 0)
1066 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001067 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301068 goto fail;
1069
1070 prev_bg_bitmap_index = bg_idx;
1071 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001072 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001073 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301074 goto success;
1075 }
1076success:
1077 free(journal_buffer);
1078 free(zero_buffer);
1079
1080 return fs->curr_blkno;
1081fail:
1082 free(journal_buffer);
1083 free(zero_buffer);
1084
1085 return -1;
1086}
1087
1088int ext4fs_get_new_inode_no(void)
1089{
1090 short i;
1091 short status;
1092 unsigned int ibmap_idx;
1093 static int prev_inode_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001094 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankared34f342012-05-25 21:22:49 +05301095 struct ext_filesystem *fs = get_fs();
1096 char *journal_buffer = zalloc(fs->blksz);
1097 char *zero_buffer = zalloc(fs->blksz);
1098 if (!journal_buffer || !zero_buffer)
1099 goto fail;
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001100 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1101 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
Uma Shankared34f342012-05-25 21:22:49 +05301102
1103 if (fs->first_pass_ibmap == 0) {
1104 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001105 uint32_t free_inodes;
1106 struct ext2_block_group *bgd = NULL;
1107 bgd = ext4fs_get_group_descriptor(fs, i);
1108 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1109 if (free_inodes) {
1110 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1111 uint64_t i_bitmap_blk =
1112 ext4fs_bg_get_inode_id(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001113 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001114 bgd->bg_itable_unused = free_inodes;
1115 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1116 put_ext4(i_bitmap_blk * fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301117 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001118 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1119 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301120 memcpy(fs->inode_bmaps[i],
1121 zero_buffer, fs->blksz);
1122 }
1123 fs->curr_inode_no =
1124 _get_new_inode_no(fs->inode_bmaps[i]);
1125 if (fs->curr_inode_no == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001126 /* inode bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +05301127 continue;
1128 fs->curr_inode_no = fs->curr_inode_no +
1129 (i * inodes_per_grp);
1130 fs->first_pass_ibmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +02001131 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001132 if (has_gdt_chksum)
Stefan Brüns749e93e2016-09-20 01:13:01 +02001133 ext4fs_bg_itable_unused_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001134 ext4fs_sb_free_inodes_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001135 status = ext4fs_devread(i_bitmap_blk *
1136 fs->sect_perblk,
1137 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301138 journal_buffer);
1139 if (status == 0)
1140 goto fail;
1141 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001142 i_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301143 goto fail;
1144 goto success;
1145 } else
1146 debug("no inode left on block group %d\n", i);
1147 }
1148 goto fail;
1149 } else {
1150restart:
1151 fs->curr_inode_no++;
1152 /* get the blockbitmap index respective to blockno */
1153 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001154 struct ext2_block_group *bgd =
1155 ext4fs_get_group_descriptor(fs, ibmap_idx);
1156 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1157 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1158
1159 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1160 put_ext4(i_bitmap_blk * fs->blksz,
Michael Walle58a9ecb2016-09-01 11:21:40 +02001161 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001162 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1163 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301164 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1165 fs->blksz);
1166 }
1167
1168 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1169 fs->inode_bmaps[ibmap_idx],
1170 ibmap_idx) != 0) {
1171 debug("going for restart for the block no %d %u\n",
1172 fs->curr_inode_no, ibmap_idx);
1173 goto restart;
1174 }
1175
1176 /* journal backup */
1177 if (prev_inode_bitmap_index != ibmap_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001178 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301179 0, fs->blksz, journal_buffer);
1180 if (status == 0)
1181 goto fail;
1182 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001183 le32_to_cpu(bgd->inode_id)))
Uma Shankared34f342012-05-25 21:22:49 +05301184 goto fail;
1185 prev_inode_bitmap_index = ibmap_idx;
1186 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001187 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001188 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001189 bgd->bg_itable_unused = bgd->free_inodes;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001190 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301191 goto success;
1192 }
1193
1194success:
1195 free(journal_buffer);
1196 free(zero_buffer);
1197
1198 return fs->curr_inode_no;
1199fail:
1200 free(journal_buffer);
1201 free(zero_buffer);
1202
1203 return -1;
1204
1205}
1206
1207
1208static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1209 unsigned int *total_remaining_blocks,
1210 unsigned int *no_blks_reqd)
1211{
1212 short i;
1213 short status;
1214 long int actual_block_no;
1215 long int si_blockno;
1216 /* si :single indirect */
Michael Walle58a9ecb2016-09-01 11:21:40 +02001217 __le32 *si_buffer = NULL;
1218 __le32 *si_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301219 struct ext_filesystem *fs = get_fs();
1220
1221 if (*total_remaining_blocks != 0) {
1222 si_buffer = zalloc(fs->blksz);
1223 if (!si_buffer) {
1224 printf("No Memory\n");
1225 return;
1226 }
1227 si_start_addr = si_buffer;
1228 si_blockno = ext4fs_get_new_blk_no();
1229 if (si_blockno == -1) {
1230 printf("no block left to assign\n");
1231 goto fail;
1232 }
1233 (*no_blks_reqd)++;
1234 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1235
Frederic Leroy04735e92013-06-26 18:11:25 +02001236 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301237 0, fs->blksz, (char *)si_buffer);
1238 memset(si_buffer, '\0', fs->blksz);
1239 if (status == 0)
1240 goto fail;
1241
1242 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1243 actual_block_no = ext4fs_get_new_blk_no();
1244 if (actual_block_no == -1) {
1245 printf("no block left to assign\n");
1246 goto fail;
1247 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001248 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301249 debug("SIAB %u: %u\n", *si_buffer,
1250 *total_remaining_blocks);
1251
1252 si_buffer++;
1253 (*total_remaining_blocks)--;
1254 if (*total_remaining_blocks == 0)
1255 break;
1256 }
1257
1258 /* write the block to disk */
Ma Haijun05508702014-01-08 08:15:33 +08001259 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301260 si_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001261 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301262 }
1263fail:
1264 free(si_start_addr);
1265}
1266
1267static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1268 unsigned int *total_remaining_blocks,
1269 unsigned int *no_blks_reqd)
1270{
1271 short i;
1272 short j;
1273 short status;
1274 long int actual_block_no;
1275 /* di:double indirect */
1276 long int di_blockno_parent;
1277 long int di_blockno_child;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001278 __le32 *di_parent_buffer = NULL;
1279 __le32 *di_child_buff = NULL;
1280 __le32 *di_block_start_addr = NULL;
1281 __le32 *di_child_buff_start = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301282 struct ext_filesystem *fs = get_fs();
1283
1284 if (*total_remaining_blocks != 0) {
1285 /* double indirect parent block connecting to inode */
1286 di_blockno_parent = ext4fs_get_new_blk_no();
1287 if (di_blockno_parent == -1) {
1288 printf("no block left to assign\n");
1289 goto fail;
1290 }
1291 di_parent_buffer = zalloc(fs->blksz);
1292 if (!di_parent_buffer)
1293 goto fail;
1294
1295 di_block_start_addr = di_parent_buffer;
1296 (*no_blks_reqd)++;
1297 debug("DIPB %ld: %u\n", di_blockno_parent,
1298 *total_remaining_blocks);
1299
Frederic Leroy04735e92013-06-26 18:11:25 +02001300 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankared34f342012-05-25 21:22:49 +05301301 fs->sect_perblk, 0,
1302 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001303
1304 if (!status) {
1305 printf("%s: Device read error!\n", __func__);
1306 goto fail;
1307 }
Uma Shankared34f342012-05-25 21:22:49 +05301308 memset(di_parent_buffer, '\0', fs->blksz);
1309
1310 /*
1311 * start:for each double indirect parent
1312 * block create one more block
1313 */
1314 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1315 di_blockno_child = ext4fs_get_new_blk_no();
1316 if (di_blockno_child == -1) {
1317 printf("no block left to assign\n");
1318 goto fail;
1319 }
1320 di_child_buff = zalloc(fs->blksz);
1321 if (!di_child_buff)
1322 goto fail;
1323
1324 di_child_buff_start = di_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001325 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankared34f342012-05-25 21:22:49 +05301326 di_parent_buffer++;
1327 (*no_blks_reqd)++;
1328 debug("DICB %ld: %u\n", di_blockno_child,
1329 *total_remaining_blocks);
1330
Frederic Leroy04735e92013-06-26 18:11:25 +02001331 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankared34f342012-05-25 21:22:49 +05301332 fs->sect_perblk, 0,
1333 fs->blksz,
1334 (char *)di_child_buff);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001335
1336 if (!status) {
1337 printf("%s: Device read error!\n", __func__);
1338 goto fail;
1339 }
Uma Shankared34f342012-05-25 21:22:49 +05301340 memset(di_child_buff, '\0', fs->blksz);
1341 /* filling of actual datablocks for each child */
1342 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1343 actual_block_no = ext4fs_get_new_blk_no();
1344 if (actual_block_no == -1) {
1345 printf("no block left to assign\n");
1346 goto fail;
1347 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001348 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301349 debug("DIAB %ld: %u\n", actual_block_no,
1350 *total_remaining_blocks);
1351
1352 di_child_buff++;
1353 (*total_remaining_blocks)--;
1354 if (*total_remaining_blocks == 0)
1355 break;
1356 }
1357 /* write the block table */
Ma Haijun05508702014-01-08 08:15:33 +08001358 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301359 di_child_buff_start, fs->blksz);
1360 free(di_child_buff_start);
1361 di_child_buff_start = NULL;
1362
1363 if (*total_remaining_blocks == 0)
1364 break;
1365 }
Ma Haijun05508702014-01-08 08:15:33 +08001366 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301367 di_block_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001368 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankared34f342012-05-25 21:22:49 +05301369 }
1370fail:
1371 free(di_block_start_addr);
1372}
1373
1374static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1375 unsigned int *total_remaining_blocks,
1376 unsigned int *no_blks_reqd)
1377{
1378 short i;
1379 short j;
1380 short k;
1381 long int actual_block_no;
1382 /* ti: Triple Indirect */
1383 long int ti_gp_blockno;
1384 long int ti_parent_blockno;
1385 long int ti_child_blockno;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001386 __le32 *ti_gp_buff = NULL;
1387 __le32 *ti_parent_buff = NULL;
1388 __le32 *ti_child_buff = NULL;
1389 __le32 *ti_gp_buff_start_addr = NULL;
1390 __le32 *ti_pbuff_start_addr = NULL;
1391 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301392 struct ext_filesystem *fs = get_fs();
1393 if (*total_remaining_blocks != 0) {
1394 /* triple indirect grand parent block connecting to inode */
1395 ti_gp_blockno = ext4fs_get_new_blk_no();
1396 if (ti_gp_blockno == -1) {
1397 printf("no block left to assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001398 return;
Uma Shankared34f342012-05-25 21:22:49 +05301399 }
1400 ti_gp_buff = zalloc(fs->blksz);
1401 if (!ti_gp_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001402 return;
Uma Shankared34f342012-05-25 21:22:49 +05301403
1404 ti_gp_buff_start_addr = ti_gp_buff;
1405 (*no_blks_reqd)++;
1406 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1407 *total_remaining_blocks);
1408
1409 /* for each 4 byte grand parent entry create one more block */
1410 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1411 ti_parent_blockno = ext4fs_get_new_blk_no();
1412 if (ti_parent_blockno == -1) {
1413 printf("no block left to assign\n");
1414 goto fail;
1415 }
1416 ti_parent_buff = zalloc(fs->blksz);
1417 if (!ti_parent_buff)
1418 goto fail;
1419
1420 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001421 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301422 ti_gp_buff++;
1423 (*no_blks_reqd)++;
1424 debug("TIPB %ld: %u\n", ti_parent_blockno,
1425 *total_remaining_blocks);
1426
1427 /* for each 4 byte entry parent create one more block */
1428 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1429 ti_child_blockno = ext4fs_get_new_blk_no();
1430 if (ti_child_blockno == -1) {
1431 printf("no block left assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001432 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301433 }
1434 ti_child_buff = zalloc(fs->blksz);
1435 if (!ti_child_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001436 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301437
1438 ti_cbuff_start_addr = ti_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001439 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301440 ti_parent_buff++;
1441 (*no_blks_reqd)++;
1442 debug("TICB %ld: %u\n", ti_parent_blockno,
1443 *total_remaining_blocks);
1444
1445 /* fill actual datablocks for each child */
1446 for (k = 0; k < (fs->blksz / sizeof(int));
1447 k++) {
1448 actual_block_no =
1449 ext4fs_get_new_blk_no();
1450 if (actual_block_no == -1) {
1451 printf("no block left\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001452 free(ti_cbuff_start_addr);
1453 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301454 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001455 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301456 debug("TIAB %ld: %u\n", actual_block_no,
1457 *total_remaining_blocks);
1458
1459 ti_child_buff++;
1460 (*total_remaining_blocks)--;
1461 if (*total_remaining_blocks == 0)
1462 break;
1463 }
1464 /* write the child block */
Ma Haijun05508702014-01-08 08:15:33 +08001465 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1466 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301467 ti_cbuff_start_addr, fs->blksz);
1468 free(ti_cbuff_start_addr);
1469
1470 if (*total_remaining_blocks == 0)
1471 break;
1472 }
1473 /* write the parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001474 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301475 ti_pbuff_start_addr, fs->blksz);
1476 free(ti_pbuff_start_addr);
1477
1478 if (*total_remaining_blocks == 0)
1479 break;
1480 }
1481 /* write the grand parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001482 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301483 ti_gp_buff_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001484 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini495c3a12015-12-10 16:42:21 -05001485 free(ti_gp_buff_start_addr);
1486 return;
Uma Shankared34f342012-05-25 21:22:49 +05301487 }
Tom Rini495c3a12015-12-10 16:42:21 -05001488fail1:
1489 free(ti_pbuff_start_addr);
Uma Shankared34f342012-05-25 21:22:49 +05301490fail:
1491 free(ti_gp_buff_start_addr);
1492}
1493
1494void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1495 unsigned int total_remaining_blocks,
1496 unsigned int *total_no_of_block)
1497{
1498 short i;
1499 long int direct_blockno;
1500 unsigned int no_blks_reqd = 0;
1501
1502 /* allocation of direct blocks */
Stephen Warrend0180282014-06-11 12:46:16 -06001503 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankared34f342012-05-25 21:22:49 +05301504 direct_blockno = ext4fs_get_new_blk_no();
1505 if (direct_blockno == -1) {
1506 printf("no block left to assign\n");
1507 return;
1508 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001509 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301510 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1511
1512 total_remaining_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +05301513 }
1514
1515 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1516 &no_blks_reqd);
1517 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1518 &no_blks_reqd);
1519 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1520 &no_blks_reqd);
1521 *total_no_of_block += no_blks_reqd;
1522}
1523
1524#endif
1525
Uma Shankara1596432012-05-25 21:21:44 +05301526static struct ext4_extent_header *ext4fs_get_extent_block
1527 (struct ext2_data *data, char *buf,
1528 struct ext4_extent_header *ext_block,
1529 uint32_t fileblock, int log2_blksz)
1530{
1531 struct ext4_extent_idx *index;
1532 unsigned long long block;
Ionut Nicu47017322014-01-13 11:59:24 +01001533 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankara1596432012-05-25 21:21:44 +05301534 int i;
1535
1536 while (1) {
1537 index = (struct ext4_extent_idx *)(ext_block + 1);
1538
Rommel Custodio8b415f72013-07-21 10:53:25 +02001539 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001540 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301541
1542 if (ext_block->eh_depth == 0)
1543 return ext_block;
1544 i = -1;
1545 do {
1546 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001547 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301548 break;
Ionut Nicub5bbac12014-01-13 12:00:08 +01001549 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankara1596432012-05-25 21:21:44 +05301550
1551 if (--i < 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001552 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301553
Rommel Custodio8b415f72013-07-21 10:53:25 +02001554 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301555 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1556
Ionut Nicu47017322014-01-13 11:59:24 +01001557 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
Frederic Leroy04735e92013-06-26 18:11:25 +02001558 buf))
Uma Shankara1596432012-05-25 21:21:44 +05301559 ext_block = (struct ext4_extent_header *)buf;
1560 else
Michael Walle58a9ecb2016-09-01 11:21:40 +02001561 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301562 }
1563}
1564
1565static int ext4fs_blockgroup
1566 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1567{
1568 long int blkno;
1569 unsigned int blkoff, desc_per_blk;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001570 int log2blksz = get_fs()->dev_desc->log2blksz;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001571 int desc_size = get_fs()->gdsize;
Uma Shankara1596432012-05-25 21:21:44 +05301572
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001573 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301574
Michael Walle7f101be2016-08-29 10:46:44 +02001575 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankara1596432012-05-25 21:21:44 +05301576 group / desc_per_blk;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001577 blkoff = (group % desc_per_blk) * desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301578
1579 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1580 group, blkno, blkoff);
1581
Frederic Leroy04735e92013-06-26 18:11:25 +02001582 return ext4fs_devread((lbaint_t)blkno <<
1583 (LOG2_BLOCK_SIZE(data) - log2blksz),
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001584 blkoff, desc_size, (char *)blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301585}
1586
1587int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1588{
1589 struct ext2_block_group blkgrp;
1590 struct ext2_sblock *sblock = &data->sblock;
1591 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00001592 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301593 int inodes_per_block, status;
1594 long int blkno;
1595 unsigned int blkoff;
1596
1597 /* It is easier to calculate if the first inode is 0. */
1598 ino--;
Michael Walle7f101be2016-08-29 10:46:44 +02001599 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301600 (sblock->inodes_per_group), &blkgrp);
1601 if (status == 0)
1602 return 0;
1603
1604 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001605 blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
Michael Walle7f101be2016-08-29 10:46:44 +02001606 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankara1596432012-05-25 21:21:44 +05301607 blkoff = (ino % inodes_per_block) * fs->inodesz;
1608 /* Read the inode. */
Frederic Leroy04735e92013-06-26 18:11:25 +02001609 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1610 log2blksz), blkoff,
Uma Shankara1596432012-05-25 21:21:44 +05301611 sizeof(struct ext2_inode), (char *)inode);
1612 if (status == 0)
1613 return 0;
1614
1615 return 1;
1616}
1617
1618long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1619{
1620 long int blknr;
1621 int blksz;
1622 int log2_blksz;
1623 int status;
1624 long int rblock;
1625 long int perblock_parent;
1626 long int perblock_child;
1627 unsigned long long start;
1628 /* get the blocksize of the filesystem */
1629 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +00001630 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1631 - get_fs()->dev_desc->log2blksz;
1632
Uma Shankara1596432012-05-25 21:21:44 +05301633 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
Stefan Brünsf81db562016-11-05 22:17:14 +01001634 long int startblock, endblock;
Uma Shankara1596432012-05-25 21:21:44 +05301635 char *buf = zalloc(blksz);
1636 if (!buf)
1637 return -ENOMEM;
1638 struct ext4_extent_header *ext_block;
1639 struct ext4_extent *extent;
Stefan Brünsf81db562016-11-05 22:17:14 +01001640 int i;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001641 ext_block =
1642 ext4fs_get_extent_block(ext4fs_root, buf,
1643 (struct ext4_extent_header *)
1644 inode->b.blocks.dir_blocks,
1645 fileblock, log2_blksz);
Uma Shankara1596432012-05-25 21:21:44 +05301646 if (!ext_block) {
1647 printf("invalid extent block\n");
1648 free(buf);
1649 return -EINVAL;
1650 }
1651
1652 extent = (struct ext4_extent *)(ext_block + 1);
1653
Stefan Brünsf81db562016-11-05 22:17:14 +01001654 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1655 startblock = le32_to_cpu(extent[i].ee_block);
1656 endblock = startblock + le16_to_cpu(extent[i].ee_len);
1657
1658 if (startblock > fileblock) {
1659 /* Sparse file */
Uma Shankara1596432012-05-25 21:21:44 +05301660 free(buf);
1661 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05301662
Stefan Brünsf81db562016-11-05 22:17:14 +01001663 } else if (fileblock < endblock) {
1664 start = le16_to_cpu(extent[i].ee_start_hi);
1665 start = (start << 32) +
Uma Shankara1596432012-05-25 21:21:44 +05301666 le32_to_cpu(extent[i].ee_start_lo);
Stefan Brünsf81db562016-11-05 22:17:14 +01001667 free(buf);
1668 return (fileblock - startblock) + start;
1669 }
Uma Shankara1596432012-05-25 21:21:44 +05301670 }
1671
Uma Shankara1596432012-05-25 21:21:44 +05301672 free(buf);
Stefan Brünsf81db562016-11-05 22:17:14 +01001673 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05301674 }
1675
1676 /* Direct blocks. */
1677 if (fileblock < INDIRECT_BLOCKS)
Michael Walle7f101be2016-08-29 10:46:44 +02001678 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301679
1680 /* Indirect. */
1681 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1682 if (ext4fs_indir1_block == NULL) {
1683 ext4fs_indir1_block = zalloc(blksz);
1684 if (ext4fs_indir1_block == NULL) {
1685 printf("** SI ext2fs read block (indir 1)"
1686 "malloc failed. **\n");
1687 return -1;
1688 }
1689 ext4fs_indir1_size = blksz;
1690 ext4fs_indir1_blkno = -1;
1691 }
1692 if (blksz != ext4fs_indir1_size) {
1693 free(ext4fs_indir1_block);
1694 ext4fs_indir1_block = NULL;
1695 ext4fs_indir1_size = 0;
1696 ext4fs_indir1_blkno = -1;
1697 ext4fs_indir1_block = zalloc(blksz);
1698 if (ext4fs_indir1_block == NULL) {
1699 printf("** SI ext2fs read block (indir 1):"
1700 "malloc failed. **\n");
1701 return -1;
1702 }
1703 ext4fs_indir1_size = blksz;
1704 }
Michael Walle7f101be2016-08-29 10:46:44 +02001705 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301706 log2_blksz) != ext4fs_indir1_blkno) {
1707 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001708 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301709 (inode->b.blocks.
1710 indir_block) << log2_blksz, 0,
1711 blksz, (char *)ext4fs_indir1_block);
1712 if (status == 0) {
1713 printf("** SI ext2fs read block (indir 1)"
1714 "failed. **\n");
Stefan Brünsde9e8312016-09-06 04:36:55 +02001715 return -1;
Uma Shankara1596432012-05-25 21:21:44 +05301716 }
1717 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001718 le32_to_cpu(inode->b.blocks.
Uma Shankara1596432012-05-25 21:21:44 +05301719 indir_block) << log2_blksz;
1720 }
Michael Walle7f101be2016-08-29 10:46:44 +02001721 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankara1596432012-05-25 21:21:44 +05301722 [fileblock - INDIRECT_BLOCKS]);
1723 }
1724 /* Double indirect. */
1725 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1726 (blksz / 4 + 1)))) {
1727
1728 long int perblock = blksz / 4;
1729 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1730
1731 if (ext4fs_indir1_block == NULL) {
1732 ext4fs_indir1_block = zalloc(blksz);
1733 if (ext4fs_indir1_block == NULL) {
1734 printf("** DI ext2fs read block (indir 2 1)"
1735 "malloc failed. **\n");
1736 return -1;
1737 }
1738 ext4fs_indir1_size = blksz;
1739 ext4fs_indir1_blkno = -1;
1740 }
1741 if (blksz != ext4fs_indir1_size) {
1742 free(ext4fs_indir1_block);
1743 ext4fs_indir1_block = NULL;
1744 ext4fs_indir1_size = 0;
1745 ext4fs_indir1_blkno = -1;
1746 ext4fs_indir1_block = zalloc(blksz);
1747 if (ext4fs_indir1_block == NULL) {
1748 printf("** DI ext2fs read block (indir 2 1)"
1749 "malloc failed. **\n");
1750 return -1;
1751 }
1752 ext4fs_indir1_size = blksz;
1753 }
Michael Walle7f101be2016-08-29 10:46:44 +02001754 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301755 log2_blksz) != ext4fs_indir1_blkno) {
1756 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001757 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301758 (inode->b.blocks.
1759 double_indir_block) << log2_blksz,
1760 0, blksz,
1761 (char *)ext4fs_indir1_block);
1762 if (status == 0) {
1763 printf("** DI ext2fs read block (indir 2 1)"
1764 "failed. **\n");
1765 return -1;
1766 }
1767 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001768 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301769 log2_blksz;
1770 }
1771
1772 if (ext4fs_indir2_block == NULL) {
1773 ext4fs_indir2_block = zalloc(blksz);
1774 if (ext4fs_indir2_block == NULL) {
1775 printf("** DI ext2fs read block (indir 2 2)"
1776 "malloc failed. **\n");
1777 return -1;
1778 }
1779 ext4fs_indir2_size = blksz;
1780 ext4fs_indir2_blkno = -1;
1781 }
1782 if (blksz != ext4fs_indir2_size) {
1783 free(ext4fs_indir2_block);
1784 ext4fs_indir2_block = NULL;
1785 ext4fs_indir2_size = 0;
1786 ext4fs_indir2_blkno = -1;
1787 ext4fs_indir2_block = zalloc(blksz);
1788 if (ext4fs_indir2_block == NULL) {
1789 printf("** DI ext2fs read block (indir 2 2)"
1790 "malloc failed. **\n");
1791 return -1;
1792 }
1793 ext4fs_indir2_size = blksz;
1794 }
Michael Walle7f101be2016-08-29 10:46:44 +02001795 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankara1596432012-05-25 21:21:44 +05301796 log2_blksz) != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001797 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301798 (ext4fs_indir1_block
1799 [rblock /
1800 perblock]) << log2_blksz, 0,
1801 blksz,
1802 (char *)ext4fs_indir2_block);
1803 if (status == 0) {
1804 printf("** DI ext2fs read block (indir 2 2)"
1805 "failed. **\n");
1806 return -1;
1807 }
1808 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001809 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301810 /
1811 perblock]) <<
1812 log2_blksz;
1813 }
Michael Walle7f101be2016-08-29 10:46:44 +02001814 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301815 }
1816 /* Tripple indirect. */
1817 else {
1818 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1819 (blksz / 4 * blksz / 4));
1820 perblock_child = blksz / 4;
1821 perblock_parent = ((blksz / 4) * (blksz / 4));
1822
1823 if (ext4fs_indir1_block == NULL) {
1824 ext4fs_indir1_block = zalloc(blksz);
1825 if (ext4fs_indir1_block == NULL) {
1826 printf("** TI ext2fs read block (indir 2 1)"
1827 "malloc failed. **\n");
1828 return -1;
1829 }
1830 ext4fs_indir1_size = blksz;
1831 ext4fs_indir1_blkno = -1;
1832 }
1833 if (blksz != ext4fs_indir1_size) {
1834 free(ext4fs_indir1_block);
1835 ext4fs_indir1_block = NULL;
1836 ext4fs_indir1_size = 0;
1837 ext4fs_indir1_blkno = -1;
1838 ext4fs_indir1_block = zalloc(blksz);
1839 if (ext4fs_indir1_block == NULL) {
1840 printf("** TI ext2fs read block (indir 2 1)"
1841 "malloc failed. **\n");
1842 return -1;
1843 }
1844 ext4fs_indir1_size = blksz;
1845 }
Michael Walle7f101be2016-08-29 10:46:44 +02001846 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301847 log2_blksz) != ext4fs_indir1_blkno) {
1848 status = ext4fs_devread
Frederic Leroy04735e92013-06-26 18:11:25 +02001849 ((lbaint_t)
Michael Walle7f101be2016-08-29 10:46:44 +02001850 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankara1596432012-05-25 21:21:44 +05301851 << log2_blksz, 0, blksz,
1852 (char *)ext4fs_indir1_block);
1853 if (status == 0) {
1854 printf("** TI ext2fs read block (indir 2 1)"
1855 "failed. **\n");
1856 return -1;
1857 }
1858 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001859 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301860 log2_blksz;
1861 }
1862
1863 if (ext4fs_indir2_block == NULL) {
1864 ext4fs_indir2_block = zalloc(blksz);
1865 if (ext4fs_indir2_block == NULL) {
1866 printf("** TI ext2fs read block (indir 2 2)"
1867 "malloc failed. **\n");
1868 return -1;
1869 }
1870 ext4fs_indir2_size = blksz;
1871 ext4fs_indir2_blkno = -1;
1872 }
1873 if (blksz != ext4fs_indir2_size) {
1874 free(ext4fs_indir2_block);
1875 ext4fs_indir2_block = NULL;
1876 ext4fs_indir2_size = 0;
1877 ext4fs_indir2_blkno = -1;
1878 ext4fs_indir2_block = zalloc(blksz);
1879 if (ext4fs_indir2_block == NULL) {
1880 printf("** TI ext2fs read block (indir 2 2)"
1881 "malloc failed. **\n");
1882 return -1;
1883 }
1884 ext4fs_indir2_size = blksz;
1885 }
Michael Walle7f101be2016-08-29 10:46:44 +02001886 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301887 perblock_parent]) <<
1888 log2_blksz)
1889 != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001890 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301891 (ext4fs_indir1_block
1892 [rblock /
1893 perblock_parent]) <<
1894 log2_blksz, 0, blksz,
1895 (char *)ext4fs_indir2_block);
1896 if (status == 0) {
1897 printf("** TI ext2fs read block (indir 2 2)"
1898 "failed. **\n");
1899 return -1;
1900 }
1901 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001902 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301903 perblock_parent])
1904 << log2_blksz;
1905 }
1906
1907 if (ext4fs_indir3_block == NULL) {
1908 ext4fs_indir3_block = zalloc(blksz);
1909 if (ext4fs_indir3_block == NULL) {
1910 printf("** TI ext2fs read block (indir 2 2)"
1911 "malloc failed. **\n");
1912 return -1;
1913 }
1914 ext4fs_indir3_size = blksz;
1915 ext4fs_indir3_blkno = -1;
1916 }
1917 if (blksz != ext4fs_indir3_size) {
1918 free(ext4fs_indir3_block);
1919 ext4fs_indir3_block = NULL;
1920 ext4fs_indir3_size = 0;
1921 ext4fs_indir3_blkno = -1;
1922 ext4fs_indir3_block = zalloc(blksz);
1923 if (ext4fs_indir3_block == NULL) {
1924 printf("** TI ext2fs read block (indir 2 2)"
1925 "malloc failed. **\n");
1926 return -1;
1927 }
1928 ext4fs_indir3_size = blksz;
1929 }
Michael Walle7f101be2016-08-29 10:46:44 +02001930 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301931 /
1932 perblock_child]) <<
1933 log2_blksz) != ext4fs_indir3_blkno) {
1934 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001935 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301936 (ext4fs_indir2_block
1937 [(rblock / perblock_child)
1938 % (blksz / 4)]) << log2_blksz, 0,
1939 blksz, (char *)ext4fs_indir3_block);
1940 if (status == 0) {
1941 printf("** TI ext2fs read block (indir 2 2)"
1942 "failed. **\n");
1943 return -1;
1944 }
1945 ext4fs_indir3_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001946 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301947 perblock_child) %
1948 (blksz /
1949 4)]) <<
1950 log2_blksz;
1951 }
1952
Michael Walle7f101be2016-08-29 10:46:44 +02001953 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankara1596432012-05-25 21:21:44 +05301954 [rblock % perblock_child]);
1955 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001956 debug("read_allocated_block %ld\n", blknr);
Uma Shankara1596432012-05-25 21:21:44 +05301957
1958 return blknr;
1959}
1960
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001961/**
1962 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1963 * global pointers
1964 *
1965 * This function assures that for a file with the same name but different size
1966 * the sequential store on the ext4 filesystem will be correct.
1967 *
1968 * In this function the global data, responsible for internal representation
1969 * of the ext4 data are initialized to the reset state. Without this, during
1970 * replacement of the smaller file with the bigger truncation of new file was
1971 * performed.
1972 */
1973void ext4fs_reinit_global(void)
Uma Shankara1596432012-05-25 21:21:44 +05301974{
Uma Shankara1596432012-05-25 21:21:44 +05301975 if (ext4fs_indir1_block != NULL) {
1976 free(ext4fs_indir1_block);
1977 ext4fs_indir1_block = NULL;
1978 ext4fs_indir1_size = 0;
1979 ext4fs_indir1_blkno = -1;
1980 }
1981 if (ext4fs_indir2_block != NULL) {
1982 free(ext4fs_indir2_block);
1983 ext4fs_indir2_block = NULL;
1984 ext4fs_indir2_size = 0;
1985 ext4fs_indir2_blkno = -1;
1986 }
1987 if (ext4fs_indir3_block != NULL) {
1988 free(ext4fs_indir3_block);
1989 ext4fs_indir3_block = NULL;
1990 ext4fs_indir3_size = 0;
1991 ext4fs_indir3_blkno = -1;
1992 }
1993}
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001994void ext4fs_close(void)
1995{
1996 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1997 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1998 ext4fs_file = NULL;
1999 }
2000 if (ext4fs_root != NULL) {
2001 free(ext4fs_root);
2002 ext4fs_root = NULL;
2003 }
2004
2005 ext4fs_reinit_global();
2006}
Uma Shankara1596432012-05-25 21:21:44 +05302007
2008int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
2009 struct ext2fs_node **fnode, int *ftype)
2010{
2011 unsigned int fpos = 0;
2012 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002013 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302014 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2015
2016#ifdef DEBUG
2017 if (name != NULL)
2018 printf("Iterate dir %s\n", name);
2019#endif /* of DEBUG */
2020 if (!diro->inode_read) {
2021 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2022 if (status == 0)
2023 return 0;
2024 }
2025 /* Search the file. */
Michael Walle7f101be2016-08-29 10:46:44 +02002026 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankara1596432012-05-25 21:21:44 +05302027 struct ext2_dirent dirent;
2028
2029 status = ext4fs_read_file(diro, fpos,
2030 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002031 (char *)&dirent, &actread);
2032 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302033 return 0;
2034
Thomas Fitzsimmons54d68e92015-11-18 12:42:53 -05002035 if (dirent.direntlen == 0) {
2036 printf("Failed to iterate over directory %s\n", name);
2037 return 0;
2038 }
2039
Uma Shankara1596432012-05-25 21:21:44 +05302040 if (dirent.namelen != 0) {
2041 char filename[dirent.namelen + 1];
2042 struct ext2fs_node *fdiro;
2043 int type = FILETYPE_UNKNOWN;
2044
2045 status = ext4fs_read_file(diro,
2046 fpos +
2047 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002048 dirent.namelen, filename,
2049 &actread);
2050 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302051 return 0;
2052
2053 fdiro = zalloc(sizeof(struct ext2fs_node));
2054 if (!fdiro)
2055 return 0;
2056
2057 fdiro->data = diro->data;
Michael Walle7f101be2016-08-29 10:46:44 +02002058 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankara1596432012-05-25 21:21:44 +05302059
2060 filename[dirent.namelen] = '\0';
2061
2062 if (dirent.filetype != FILETYPE_UNKNOWN) {
2063 fdiro->inode_read = 0;
2064
2065 if (dirent.filetype == FILETYPE_DIRECTORY)
2066 type = FILETYPE_DIRECTORY;
2067 else if (dirent.filetype == FILETYPE_SYMLINK)
2068 type = FILETYPE_SYMLINK;
2069 else if (dirent.filetype == FILETYPE_REG)
2070 type = FILETYPE_REG;
2071 } else {
2072 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002073 le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05302074 (dirent.inode),
2075 &fdiro->inode);
2076 if (status == 0) {
2077 free(fdiro);
2078 return 0;
2079 }
2080 fdiro->inode_read = 1;
2081
Michael Walle7f101be2016-08-29 10:46:44 +02002082 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankara1596432012-05-25 21:21:44 +05302083 FILETYPE_INO_MASK) ==
2084 FILETYPE_INO_DIRECTORY) {
2085 type = FILETYPE_DIRECTORY;
Michael Walle7f101be2016-08-29 10:46:44 +02002086 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302087 & FILETYPE_INO_MASK) ==
2088 FILETYPE_INO_SYMLINK) {
2089 type = FILETYPE_SYMLINK;
Michael Walle7f101be2016-08-29 10:46:44 +02002090 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302091 & FILETYPE_INO_MASK) ==
2092 FILETYPE_INO_REG) {
2093 type = FILETYPE_REG;
2094 }
2095 }
2096#ifdef DEBUG
2097 printf("iterate >%s<\n", filename);
2098#endif /* of DEBUG */
2099 if ((name != NULL) && (fnode != NULL)
2100 && (ftype != NULL)) {
2101 if (strcmp(filename, name) == 0) {
2102 *ftype = type;
2103 *fnode = fdiro;
2104 return 1;
2105 }
2106 } else {
2107 if (fdiro->inode_read == 0) {
2108 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002109 le32_to_cpu(
Uma Shankara1596432012-05-25 21:21:44 +05302110 dirent.inode),
2111 &fdiro->inode);
2112 if (status == 0) {
2113 free(fdiro);
2114 return 0;
2115 }
2116 fdiro->inode_read = 1;
2117 }
2118 switch (type) {
2119 case FILETYPE_DIRECTORY:
2120 printf("<DIR> ");
2121 break;
2122 case FILETYPE_SYMLINK:
2123 printf("<SYM> ");
2124 break;
2125 case FILETYPE_REG:
2126 printf(" ");
2127 break;
2128 default:
2129 printf("< ? > ");
2130 break;
2131 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002132 printf("%10u %s\n",
Michael Walle7f101be2016-08-29 10:46:44 +02002133 le32_to_cpu(fdiro->inode.size),
Uma Shankara1596432012-05-25 21:21:44 +05302134 filename);
2135 }
2136 free(fdiro);
2137 }
Michael Walle7f101be2016-08-29 10:46:44 +02002138 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankara1596432012-05-25 21:21:44 +05302139 }
2140 return 0;
2141}
2142
2143static char *ext4fs_read_symlink(struct ext2fs_node *node)
2144{
2145 char *symlink;
2146 struct ext2fs_node *diro = node;
2147 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002148 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302149
2150 if (!diro->inode_read) {
2151 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2152 if (status == 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002153 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302154 }
Michael Walle7f101be2016-08-29 10:46:44 +02002155 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankara1596432012-05-25 21:21:44 +05302156 if (!symlink)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002157 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302158
Michael Walle7f101be2016-08-29 10:46:44 +02002159 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankara1596432012-05-25 21:21:44 +05302160 strncpy(symlink, diro->inode.b.symlink,
Michael Walle7f101be2016-08-29 10:46:44 +02002161 le32_to_cpu(diro->inode.size));
Uma Shankara1596432012-05-25 21:21:44 +05302162 } else {
2163 status = ext4fs_read_file(diro, 0,
Michael Walle7f101be2016-08-29 10:46:44 +02002164 le32_to_cpu(diro->inode.size),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002165 symlink, &actread);
Gary Bisson9d2f6a92015-09-07 11:20:07 +02002166 if ((status < 0) || (actread == 0)) {
Uma Shankara1596432012-05-25 21:21:44 +05302167 free(symlink);
Michael Walle58a9ecb2016-09-01 11:21:40 +02002168 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302169 }
2170 }
Michael Walle7f101be2016-08-29 10:46:44 +02002171 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankara1596432012-05-25 21:21:44 +05302172 return symlink;
2173}
2174
2175static int ext4fs_find_file1(const char *currpath,
2176 struct ext2fs_node *currroot,
2177 struct ext2fs_node **currfound, int *foundtype)
2178{
2179 char fpath[strlen(currpath) + 1];
2180 char *name = fpath;
2181 char *next;
2182 int status;
2183 int type = FILETYPE_DIRECTORY;
2184 struct ext2fs_node *currnode = currroot;
2185 struct ext2fs_node *oldnode = currroot;
2186
2187 strncpy(fpath, currpath, strlen(currpath) + 1);
2188
2189 /* Remove all leading slashes. */
2190 while (*name == '/')
2191 name++;
2192
2193 if (!*name) {
2194 *currfound = currnode;
2195 return 1;
2196 }
2197
2198 for (;;) {
2199 int found;
2200
2201 /* Extract the actual part from the pathname. */
2202 next = strchr(name, '/');
2203 if (next) {
2204 /* Remove all leading slashes. */
2205 while (*next == '/')
2206 *(next++) = '\0';
2207 }
2208
2209 if (type != FILETYPE_DIRECTORY) {
2210 ext4fs_free_node(currnode, currroot);
2211 return 0;
2212 }
2213
2214 oldnode = currnode;
2215
2216 /* Iterate over the directory. */
2217 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2218 if (found == 0)
2219 return 0;
2220
2221 if (found == -1)
2222 break;
2223
2224 /* Read in the symlink and follow it. */
2225 if (type == FILETYPE_SYMLINK) {
2226 char *symlink;
2227
2228 /* Test if the symlink does not loop. */
2229 if (++symlinknest == 8) {
2230 ext4fs_free_node(currnode, currroot);
2231 ext4fs_free_node(oldnode, currroot);
2232 return 0;
2233 }
2234
2235 symlink = ext4fs_read_symlink(currnode);
2236 ext4fs_free_node(currnode, currroot);
2237
2238 if (!symlink) {
2239 ext4fs_free_node(oldnode, currroot);
2240 return 0;
2241 }
2242
2243 debug("Got symlink >%s<\n", symlink);
2244
2245 if (symlink[0] == '/') {
2246 ext4fs_free_node(oldnode, currroot);
2247 oldnode = &ext4fs_root->diropen;
2248 }
2249
2250 /* Lookup the node the symlink points to. */
2251 status = ext4fs_find_file1(symlink, oldnode,
2252 &currnode, &type);
2253
2254 free(symlink);
2255
2256 if (status == 0) {
2257 ext4fs_free_node(oldnode, currroot);
2258 return 0;
2259 }
2260 }
2261
2262 ext4fs_free_node(oldnode, currroot);
2263
2264 /* Found the node! */
2265 if (!next || *next == '\0') {
2266 *currfound = currnode;
2267 *foundtype = type;
2268 return 1;
2269 }
2270 name = next;
2271 }
2272 return -1;
2273}
2274
2275int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2276 struct ext2fs_node **foundnode, int expecttype)
2277{
2278 int status;
2279 int foundtype = FILETYPE_DIRECTORY;
2280
2281 symlinknest = 0;
2282 if (!path)
2283 return 0;
2284
2285 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2286 if (status == 0)
2287 return 0;
2288
2289 /* Check if the node that was found was of the expected type. */
2290 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2291 return 0;
2292 else if ((expecttype == FILETYPE_DIRECTORY)
2293 && (foundtype != expecttype))
2294 return 0;
2295
2296 return 1;
2297}
2298
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002299int ext4fs_open(const char *filename, loff_t *len)
Uma Shankara1596432012-05-25 21:21:44 +05302300{
2301 struct ext2fs_node *fdiro = NULL;
2302 int status;
Uma Shankara1596432012-05-25 21:21:44 +05302303
2304 if (ext4fs_root == NULL)
2305 return -1;
2306
2307 ext4fs_file = NULL;
2308 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2309 FILETYPE_REG);
2310 if (status == 0)
2311 goto fail;
2312
2313 if (!fdiro->inode_read) {
2314 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2315 &fdiro->inode);
2316 if (status == 0)
2317 goto fail;
2318 }
Michael Walle7f101be2016-08-29 10:46:44 +02002319 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankara1596432012-05-25 21:21:44 +05302320 ext4fs_file = fdiro;
2321
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002322 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05302323fail:
2324 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2325
2326 return -1;
2327}
2328
2329int ext4fs_mount(unsigned part_length)
2330{
2331 struct ext2_data *data;
2332 int status;
2333 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00002334 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankara1596432012-05-25 21:21:44 +05302335 if (!data)
2336 return 0;
2337
2338 /* Read the superblock. */
Egbert Eich50ce4c02013-05-01 01:13:19 +00002339 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankara1596432012-05-25 21:21:44 +05302340
2341 if (status == 0)
2342 goto fail;
2343
2344 /* Make sure this is an ext2 filesystem. */
Michael Walle7f101be2016-08-29 10:46:44 +02002345 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Marek Behún51be4712018-03-08 00:26:08 +01002346 goto fail_noerr;
Uma Shankara1596432012-05-25 21:21:44 +05302347
Tom Rini6f94ab62016-07-22 17:59:11 -04002348
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002349 if (le32_to_cpu(data->sblock.revision_level) == 0) {
Uma Shankara1596432012-05-25 21:21:44 +05302350 fs->inodesz = 128;
Stefan Brüns3cc5bbb2016-12-27 02:35:08 +01002351 fs->gdsize = 32;
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002352 } else {
2353 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2354 __le32_to_cpu(data->sblock.feature_compatibility),
2355 __le32_to_cpu(data->sblock.feature_incompat),
2356 __le32_to_cpu(data->sblock.feature_ro_compat));
Uma Shankara1596432012-05-25 21:21:44 +05302357
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002358 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2359 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2360 EXT4_FEATURE_INCOMPAT_64BIT ?
2361 le16_to_cpu(data->sblock.descriptor_size) : 32;
2362 }
2363
2364 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2365 le32_to_cpu(data->sblock.revision_level),
2366 fs->inodesz, fs->gdsize);
Uma Shankara1596432012-05-25 21:21:44 +05302367
2368 data->diropen.data = data;
2369 data->diropen.ino = 2;
2370 data->diropen.inode_read = 1;
2371 data->inode = &data->diropen.inode;
2372
2373 status = ext4fs_read_inode(data, 2, data->inode);
2374 if (status == 0)
2375 goto fail;
2376
2377 ext4fs_root = data;
2378
2379 return 1;
2380fail:
2381 printf("Failed to mount ext2 filesystem...\n");
Marek Behún51be4712018-03-08 00:26:08 +01002382fail_noerr:
Uma Shankara1596432012-05-25 21:21:44 +05302383 free(data);
2384 ext4fs_root = NULL;
2385
2386 return 0;
2387}