blob: 4248ac1dcf500f7ad9b15d4f80c493304c36ce26 [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));
435 }
436
437 return crc;
438}
439
440static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
441{
442 int dentry_length;
443 int sizeof_void_space;
444 int new_entry_byte_reqd;
445 short padding_factor = 0;
446
447 if (dir->namelen % 4 != 0)
448 padding_factor = 4 - (dir->namelen % 4);
449
450 dentry_length = sizeof(struct ext2_dirent) +
451 dir->namelen + padding_factor;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200452 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankared34f342012-05-25 21:22:49 +0530453 if (sizeof_void_space == 0)
454 return 0;
455
456 padding_factor = 0;
457 if (strlen(filename) % 4 != 0)
458 padding_factor = 4 - (strlen(filename) % 4);
459
460 new_entry_byte_reqd = strlen(filename) +
461 sizeof(struct ext2_dirent) + padding_factor;
462 if (sizeof_void_space >= new_entry_byte_reqd) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200463 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankared34f342012-05-25 21:22:49 +0530464 return sizeof_void_space;
465 }
466
467 return 0;
468}
469
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200470int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankared34f342012-05-25 21:22:49 +0530471{
472 unsigned int *zero_buffer = NULL;
473 char *root_first_block_buffer = NULL;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200474 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530475 long int first_block_no_of_root = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530476 int totalbytes = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530477 unsigned int new_entry_byte_reqd;
Uma Shankared34f342012-05-25 21:22:49 +0530478 int sizeof_void_space = 0;
479 int templength = 0;
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200480 int inodeno = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530481 int status;
482 struct ext_filesystem *fs = get_fs();
483 /* directory entry */
484 struct ext2_dirent *dir;
Uma Shankared34f342012-05-25 21:22:49 +0530485 char *temp_dir = NULL;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200486 uint32_t new_blk_no;
487 uint32_t new_size;
488 uint32_t new_blockcnt;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200489 uint32_t directory_blocks;
Uma Shankared34f342012-05-25 21:22:49 +0530490
491 zero_buffer = zalloc(fs->blksz);
492 if (!zero_buffer) {
493 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200494 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530495 }
496 root_first_block_buffer = zalloc(fs->blksz);
497 if (!root_first_block_buffer) {
498 free(zero_buffer);
499 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200500 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530501 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200502 new_entry_byte_reqd = ROUND(strlen(filename) +
503 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530504restart:
Stefan Brünsa321abd2016-09-06 04:36:44 +0200505 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
506 LOG2_BLOCK_SIZE(ext4fs_root);
507 blk_idx = directory_blocks - 1;
Uma Shankared34f342012-05-25 21:22:49 +0530508
Stefan Brünsa321abd2016-09-06 04:36:44 +0200509restart_read:
Uma Shankared34f342012-05-25 21:22:49 +0530510 /* read the block no allocated to a file */
Stefan Brünsa321abd2016-09-06 04:36:44 +0200511 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
512 if (first_block_no_of_root <= 0)
513 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530514
Frederic Leroy04735e92013-06-26 18:11:25 +0200515 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankared34f342012-05-25 21:22:49 +0530516 * fs->sect_perblk,
517 0, fs->blksz, root_first_block_buffer);
518 if (status == 0)
519 goto fail;
520
521 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
522 goto fail;
523 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankared34f342012-05-25 21:22:49 +0530524 totalbytes = 0;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200525
Michael Walle58a9ecb2016-09-01 11:21:40 +0200526 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsa321abd2016-09-06 04:36:44 +0200527 unsigned short used_len = ROUND(dir->namelen +
528 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530529
Stefan Brünsa321abd2016-09-06 04:36:44 +0200530 /* last entry of block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200531 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankared34f342012-05-25 21:22:49 +0530532
Stefan Brünsa321abd2016-09-06 04:36:44 +0200533 /* check if new entry fits */
534 if ((used_len + new_entry_byte_reqd) <=
535 le16_to_cpu(dir->direntlen)) {
536 dir->direntlen = cpu_to_le16(used_len);
537 break;
538 } else {
539 if (blk_idx > 0) {
540 printf("Block full, trying previous\n");
541 blk_idx--;
542 goto restart_read;
543 }
544 printf("All blocks full: Allocate new\n");
Uma Shankared34f342012-05-25 21:22:49 +0530545
Stefan Brünsb96c3c72016-09-06 04:36:43 +0200546 if (le32_to_cpu(g_parent_inode->flags) &
547 EXT4_EXTENTS_FL) {
548 printf("Directory uses extents\n");
549 goto fail;
550 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200551 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankared34f342012-05-25 21:22:49 +0530552 printf("Directory exceeds limit\n");
553 goto fail;
554 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200555 new_blk_no = ext4fs_get_new_blk_no();
556 if (new_blk_no == -1) {
Uma Shankared34f342012-05-25 21:22:49 +0530557 printf("no block left to assign\n");
558 goto fail;
559 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200560 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200561 g_parent_inode->b.blocks.
562 dir_blocks[directory_blocks] =
Michael Walle58a9ecb2016-09-01 11:21:40 +0200563 cpu_to_le32(new_blk_no);
564
565 new_size = le32_to_cpu(g_parent_inode->size);
566 new_size += fs->blksz;
567 g_parent_inode->size = cpu_to_le32(new_size);
568
569 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
570 new_blockcnt += fs->sect_perblk;
571 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
572
Uma Shankared34f342012-05-25 21:22:49 +0530573 if (ext4fs_put_metadata
574 (root_first_block_buffer,
575 first_block_no_of_root))
576 goto fail;
577 goto restart;
578 }
Uma Shankared34f342012-05-25 21:22:49 +0530579 }
580
Michael Walle58a9ecb2016-09-01 11:21:40 +0200581 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530582 totalbytes = totalbytes + templength;
583 sizeof_void_space = check_void_in_dentry(dir, filename);
584 if (sizeof_void_space)
585 break;
586
587 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530588 }
589
590 /* make a pointer ready for creating next directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200591 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530592 totalbytes = totalbytes + templength;
593 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530594
595 /* get the next available inode number */
596 inodeno = ext4fs_get_new_inode_no();
597 if (inodeno == -1) {
598 printf("no inode left to assign\n");
599 goto fail;
600 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200601 dir->inode = cpu_to_le32(inodeno);
Uma Shankared34f342012-05-25 21:22:49 +0530602 if (sizeof_void_space)
Michael Walle58a9ecb2016-09-01 11:21:40 +0200603 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankared34f342012-05-25 21:22:49 +0530604 else
Michael Walle58a9ecb2016-09-01 11:21:40 +0200605 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankared34f342012-05-25 21:22:49 +0530606
607 dir->namelen = strlen(filename);
608 dir->filetype = FILETYPE_REG; /* regular file */
609 temp_dir = (char *)dir;
610 temp_dir = temp_dir + sizeof(struct ext2_dirent);
611 memcpy(temp_dir, filename, strlen(filename));
612
Uma Shankared34f342012-05-25 21:22:49 +0530613 /* update or write the 1st block of root inode */
614 if (ext4fs_put_metadata(root_first_block_buffer,
615 first_block_no_of_root))
616 goto fail;
617
618fail:
619 free(zero_buffer);
620 free(root_first_block_buffer);
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200621
622 return inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530623}
624
625static int search_dir(struct ext2_inode *parent_inode, char *dirname)
626{
627 int status;
Stefan Brüns76a29512016-09-06 04:36:41 +0200628 int inodeno = 0;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200629 int offset;
630 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530631 long int blknr;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200632 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530633 struct ext2_dirent *dir = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530634 struct ext_filesystem *fs = get_fs();
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200635 uint32_t directory_blocks;
636 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530637
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200638 directory_blocks = le32_to_cpu(parent_inode->size) >>
639 LOG2_BLOCK_SIZE(ext4fs_root);
640
641 block_buffer = zalloc(fs->blksz);
642 if (!block_buffer)
643 goto fail;
644
645 /* get the block no allocated to a file */
646 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
647 blknr = read_allocated_block(parent_inode, blk_idx);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200648 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530649 goto fail;
650
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200651 /* read the directory block */
Frederic Leroy04735e92013-06-26 18:11:25 +0200652 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +0530653 0, fs->blksz, (char *)block_buffer);
654 if (status == 0)
655 goto fail;
656
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200657 offset = 0;
658 do {
659 dir = (struct ext2_dirent *)(block_buffer + offset);
660 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankared34f342012-05-25 21:22:49 +0530661
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200662 int direntlen = le16_to_cpu(dir->direntlen);
663 if (direntlen < sizeof(struct ext2_dirent))
Uma Shankared34f342012-05-25 21:22:49 +0530664 break;
665
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200666 if (dir->inode && (strlen(dirname) == dir->namelen) &&
667 (strncmp(dirname, direntname, dir->namelen) == 0)) {
668 inodeno = le32_to_cpu(dir->inode);
669 break;
670 }
Uma Shankared34f342012-05-25 21:22:49 +0530671
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200672 offset += direntlen;
Stefan Brüns76a29512016-09-06 04:36:41 +0200673
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200674 } while (offset < fs->blksz);
675
676 if (inodeno > 0) {
677 free(block_buffer);
Stefan Brüns76a29512016-09-06 04:36:41 +0200678 return inodeno;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200679 }
Uma Shankared34f342012-05-25 21:22:49 +0530680 }
681
682fail:
683 free(block_buffer);
684
685 return -1;
686}
687
688static int find_dir_depth(char *dirname)
689{
690 char *token = strtok(dirname, "/");
691 int count = 0;
692 while (token != NULL) {
693 token = strtok(NULL, "/");
694 count++;
695 }
696 return count + 1 + 1;
697 /*
698 * for example for string /home/temp
699 * depth=home(1)+temp(1)+1 extra for NULL;
700 * so count is 4;
701 */
702}
703
704static int parse_path(char **arr, char *dirname)
705{
706 char *token = strtok(dirname, "/");
707 int i = 0;
708
709 /* add root */
710 arr[i] = zalloc(strlen("/") + 1);
711 if (!arr[i])
712 return -ENOMEM;
Stephen Warren934b14f2015-09-04 22:03:44 -0600713 memcpy(arr[i++], "/", strlen("/"));
Uma Shankared34f342012-05-25 21:22:49 +0530714
715 /* add each path entry after root */
716 while (token != NULL) {
717 arr[i] = zalloc(strlen(token) + 1);
718 if (!arr[i])
719 return -ENOMEM;
720 memcpy(arr[i++], token, strlen(token));
721 token = strtok(NULL, "/");
722 }
723 arr[i] = NULL;
724
725 return 0;
726}
727
728int ext4fs_iget(int inode_no, struct ext2_inode *inode)
729{
730 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
731 return -1;
732
733 return 0;
734}
735
736/*
737 * Function: ext4fs_get_parent_inode_num
738 * Return Value: inode Number of the parent directory of file/Directory to be
739 * created
740 * dirname : Input parmater, input path name of the file/directory to be created
741 * dname : Output parameter, to be filled with the name of the directory
742 * extracted from dirname
743 */
744int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
745{
746 int i;
747 int depth = 0;
748 int matched_inode_no;
749 int result_inode_no = -1;
750 char **ptr = NULL;
751 char *depth_dirname = NULL;
752 char *parse_dirname = NULL;
753 struct ext2_inode *parent_inode = NULL;
754 struct ext2_inode *first_inode = NULL;
755 struct ext2_inode temp_inode;
756
757 if (*dirname != '/') {
758 printf("Please supply Absolute path\n");
759 return -1;
760 }
761
762 /* TODO: input validation make equivalent to linux */
763 depth_dirname = zalloc(strlen(dirname) + 1);
764 if (!depth_dirname)
765 return -ENOMEM;
766
767 memcpy(depth_dirname, dirname, strlen(dirname));
768 depth = find_dir_depth(depth_dirname);
769 parse_dirname = zalloc(strlen(dirname) + 1);
770 if (!parse_dirname)
771 goto fail;
772 memcpy(parse_dirname, dirname, strlen(dirname));
773
774 /* allocate memory for each directory level */
775 ptr = zalloc((depth) * sizeof(char *));
776 if (!ptr)
777 goto fail;
778 if (parse_path(ptr, parse_dirname))
779 goto fail;
780 parent_inode = zalloc(sizeof(struct ext2_inode));
781 if (!parent_inode)
782 goto fail;
783 first_inode = zalloc(sizeof(struct ext2_inode));
784 if (!first_inode)
785 goto fail;
786 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
787 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
788 if (flags & F_FILE)
789 result_inode_no = EXT2_ROOT_INO;
790 for (i = 1; i < depth; i++) {
791 matched_inode_no = search_dir(parent_inode, ptr[i]);
792 if (matched_inode_no == -1) {
793 if (ptr[i + 1] == NULL && i == 1) {
794 result_inode_no = EXT2_ROOT_INO;
795 goto end;
796 } else {
797 if (ptr[i + 1] == NULL)
798 break;
799 printf("Invalid path\n");
800 result_inode_no = -1;
801 goto fail;
802 }
803 } else {
804 if (ptr[i + 1] != NULL) {
805 memset(parent_inode, '\0',
806 sizeof(struct ext2_inode));
807 if (ext4fs_iget(matched_inode_no,
808 parent_inode)) {
809 result_inode_no = -1;
810 goto fail;
811 }
812 result_inode_no = matched_inode_no;
813 } else {
814 break;
815 }
816 }
817 }
818
819end:
820 if (i == 1)
821 matched_inode_no = search_dir(first_inode, ptr[i]);
822 else
823 matched_inode_no = search_dir(parent_inode, ptr[i]);
824
825 if (matched_inode_no != -1) {
826 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200827 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankared34f342012-05-25 21:22:49 +0530828 printf("It is a Directory\n");
829 result_inode_no = -1;
830 goto fail;
831 }
832 }
833
834 if (strlen(ptr[i]) > 256) {
835 result_inode_no = -1;
836 goto fail;
837 }
838 memcpy(dname, ptr[i], strlen(ptr[i]));
839
840fail:
841 free(depth_dirname);
842 free(parse_dirname);
Stephen Warren934b14f2015-09-04 22:03:44 -0600843 for (i = 0; i < depth; i++) {
844 if (!ptr[i])
845 break;
846 free(ptr[i]);
847 }
Uma Shankared34f342012-05-25 21:22:49 +0530848 free(ptr);
849 free(parent_inode);
850 free(first_inode);
851
852 return result_inode_no;
853}
854
Stefan Brüns76a29512016-09-06 04:36:41 +0200855static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankared34f342012-05-25 21:22:49 +0530856{
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200857 int status;
858 int inodeno = 0;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200859 int offset;
860 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530861 struct ext2_dirent *dir = NULL;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200862 struct ext2_dirent *previous_dir;
Uma Shankared34f342012-05-25 21:22:49 +0530863 struct ext_filesystem *fs = get_fs();
Stephen Warrend56b2012015-09-04 22:03:45 -0600864 int ret = -1;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200865 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530866
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200867 block_buffer = zalloc(fs->blksz);
868 if (!block_buffer)
Uma Shankared34f342012-05-25 21:22:49 +0530869 return -ENOMEM;
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200870
871 /* read the directory block */
Stefan Brüns76a29512016-09-06 04:36:41 +0200872 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200873 fs->blksz, block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530874 if (status == 0)
875 goto fail;
876
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200877 offset = 0;
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200878 do {
879 previous_dir = dir;
880 dir = (struct ext2_dirent *)(block_buffer + offset);
881 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
882
883 int direntlen = le16_to_cpu(dir->direntlen);
884 if (direntlen < sizeof(struct ext2_dirent))
885 break;
886
Stefan Brüns76a29512016-09-06 04:36:41 +0200887 if (dir->inode && (strlen(filename) == dir->namelen) &&
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200888 (strncmp(direntname, filename, dir->namelen) == 0)) {
Stefan Brüns76a29512016-09-06 04:36:41 +0200889 inodeno = le32_to_cpu(dir->inode);
Stefan Brüns76a29512016-09-06 04:36:41 +0200890 break;
Uma Shankared34f342012-05-25 21:22:49 +0530891 }
892
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200893 offset += direntlen;
Uma Shankared34f342012-05-25 21:22:49 +0530894
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200895 } while (offset < fs->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530896
Stefan Brünsd1bdf222016-10-09 20:15:27 +0200897 if (inodeno > 0) {
Stefan Brüns805e3e002016-10-09 20:15:28 +0200898 printf("file found, deleting\n");
899 if (ext4fs_log_journal(block_buffer, blknr))
900 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530901
Stefan Brüns805e3e002016-10-09 20:15:28 +0200902 if (previous_dir) {
903 /* merge dir entry with predecessor */
904 uint16_t new_len;
905 new_len = le16_to_cpu(previous_dir->direntlen);
906 new_len += le16_to_cpu(dir->direntlen);
907 previous_dir->direntlen = cpu_to_le16(new_len);
908 } else {
909 /* invalidate dir entry */
910 dir->inode = 0;
911 }
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200912 if (ext4fs_put_metadata(block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530913 goto fail;
Stephen Warrend56b2012015-09-04 22:03:45 -0600914 ret = inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530915 }
916fail:
Stefan Brüns15bf8c42016-10-09 20:15:26 +0200917 free(block_buffer);
Uma Shankared34f342012-05-25 21:22:49 +0530918
Stephen Warrend56b2012015-09-04 22:03:45 -0600919 return ret;
Uma Shankared34f342012-05-25 21:22:49 +0530920}
921
Stefan Brüns76a29512016-09-06 04:36:41 +0200922int ext4fs_filename_unlink(char *filename)
Uma Shankared34f342012-05-25 21:22:49 +0530923{
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200924 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530925 long int blknr = -1;
926 int inodeno = -1;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200927 uint32_t directory_blocks;
928
929 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
930 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankared34f342012-05-25 21:22:49 +0530931
932 /* read the block no allocated to a file */
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200933 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
934 blknr = read_allocated_block(g_parent_inode, blk_idx);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200935 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530936 break;
Stefan Brüns76a29512016-09-06 04:36:41 +0200937 inodeno = unlink_filename(filename, blknr);
Uma Shankared34f342012-05-25 21:22:49 +0530938 if (inodeno != -1)
939 return inodeno;
940 }
941
942 return -1;
943}
944
Michael Walle58a9ecb2016-09-01 11:21:40 +0200945uint32_t ext4fs_get_new_blk_no(void)
Uma Shankared34f342012-05-25 21:22:49 +0530946{
947 short i;
948 short status;
949 int remainder;
950 unsigned int bg_idx;
951 static int prev_bg_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200952 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankared34f342012-05-25 21:22:49 +0530953 struct ext_filesystem *fs = get_fs();
954 char *journal_buffer = zalloc(fs->blksz);
955 char *zero_buffer = zalloc(fs->blksz);
956 if (!journal_buffer || !zero_buffer)
957 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530958
959 if (fs->first_pass_bbmap == 0) {
960 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200961 struct ext2_block_group *bgd = NULL;
962 bgd = ext4fs_get_group_descriptor(fs, i);
963 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
964 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
965 uint64_t b_bitmap_blk =
966 ext4fs_bg_get_block_id(bgd, fs);
967 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +0530968 memcpy(fs->blk_bmaps[i], zero_buffer,
969 fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200970 put_ext4(b_bitmap_blk * fs->blksz,
971 fs->blk_bmaps[i], fs->blksz);
972 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
973 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +0530974 }
975 fs->curr_blkno =
976 _get_new_blk_no(fs->blk_bmaps[i]);
977 if (fs->curr_blkno == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +0200978 /* block bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +0530979 continue;
980 fs->curr_blkno = fs->curr_blkno +
981 (i * fs->blksz * 8);
982 fs->first_pass_bbmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +0200983 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200984 ext4fs_sb_free_blocks_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200985 status = ext4fs_devread(b_bitmap_blk *
986 fs->sect_perblk,
987 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +0530988 journal_buffer);
989 if (status == 0)
990 goto fail;
991 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +0200992 b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +0530993 goto fail;
994 goto success;
995 } else {
996 debug("no space left on block group %d\n", i);
997 }
998 }
999
1000 goto fail;
1001 } else {
Uma Shankared34f342012-05-25 21:22:49 +05301002 fs->curr_blkno++;
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001003restart:
Uma Shankared34f342012-05-25 21:22:49 +05301004 /* get the blockbitmap index respective to blockno */
Łukasz Majewski35dd0552014-05-06 09:36:04 +02001005 bg_idx = fs->curr_blkno / blk_per_grp;
1006 if (fs->blksz == 1024) {
Uma Shankared34f342012-05-25 21:22:49 +05301007 remainder = fs->curr_blkno % blk_per_grp;
1008 if (!remainder)
1009 bg_idx--;
1010 }
1011
1012 /*
1013 * To skip completely filled block group bitmaps
1014 * Optimize the block allocation
1015 */
1016 if (bg_idx >= fs->no_blkgrp)
1017 goto fail;
1018
Stefan Brüns688d0e72016-09-17 02:10:10 +02001019 struct ext2_block_group *bgd = NULL;
1020 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1021 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
Uma Shankared34f342012-05-25 21:22:49 +05301022 debug("block group %u is full. Skipping\n", bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001023 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1024 if (fs->blksz == 1024)
1025 fs->curr_blkno += 1;
Uma Shankared34f342012-05-25 21:22:49 +05301026 goto restart;
1027 }
1028
Stefan Brüns688d0e72016-09-17 02:10:10 +02001029 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1030 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1031 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +05301032 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001033 put_ext4(b_bitmap_blk * fs->blksz,
1034 zero_buffer, fs->blksz);
1035 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1036 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301037 }
1038
1039 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1040 bg_idx) != 0) {
1041 debug("going for restart for the block no %ld %u\n",
1042 fs->curr_blkno, bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001043 fs->curr_blkno++;
Uma Shankared34f342012-05-25 21:22:49 +05301044 goto restart;
1045 }
1046
1047 /* journal backup */
1048 if (prev_bg_bitmap_index != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001049 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301050 0, fs->blksz, journal_buffer);
1051 if (status == 0)
1052 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001053 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301054 goto fail;
1055
1056 prev_bg_bitmap_index = bg_idx;
1057 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001058 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001059 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301060 goto success;
1061 }
1062success:
1063 free(journal_buffer);
1064 free(zero_buffer);
1065
1066 return fs->curr_blkno;
1067fail:
1068 free(journal_buffer);
1069 free(zero_buffer);
1070
1071 return -1;
1072}
1073
1074int ext4fs_get_new_inode_no(void)
1075{
1076 short i;
1077 short status;
1078 unsigned int ibmap_idx;
1079 static int prev_inode_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001080 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankared34f342012-05-25 21:22:49 +05301081 struct ext_filesystem *fs = get_fs();
1082 char *journal_buffer = zalloc(fs->blksz);
1083 char *zero_buffer = zalloc(fs->blksz);
1084 if (!journal_buffer || !zero_buffer)
1085 goto fail;
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001086 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1087 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
Uma Shankared34f342012-05-25 21:22:49 +05301088
1089 if (fs->first_pass_ibmap == 0) {
1090 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001091 uint32_t free_inodes;
1092 struct ext2_block_group *bgd = NULL;
1093 bgd = ext4fs_get_group_descriptor(fs, i);
1094 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1095 if (free_inodes) {
1096 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1097 uint64_t i_bitmap_blk =
1098 ext4fs_bg_get_inode_id(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001099 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001100 bgd->bg_itable_unused = free_inodes;
1101 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1102 put_ext4(i_bitmap_blk * fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301103 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001104 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1105 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301106 memcpy(fs->inode_bmaps[i],
1107 zero_buffer, fs->blksz);
1108 }
1109 fs->curr_inode_no =
1110 _get_new_inode_no(fs->inode_bmaps[i]);
1111 if (fs->curr_inode_no == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001112 /* inode bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +05301113 continue;
1114 fs->curr_inode_no = fs->curr_inode_no +
1115 (i * inodes_per_grp);
1116 fs->first_pass_ibmap++;
Stefan Brüns749e93e2016-09-20 01:13:01 +02001117 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001118 if (has_gdt_chksum)
Stefan Brüns749e93e2016-09-20 01:13:01 +02001119 ext4fs_bg_itable_unused_dec(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001120 ext4fs_sb_free_inodes_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001121 status = ext4fs_devread(i_bitmap_blk *
1122 fs->sect_perblk,
1123 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301124 journal_buffer);
1125 if (status == 0)
1126 goto fail;
1127 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001128 i_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301129 goto fail;
1130 goto success;
1131 } else
1132 debug("no inode left on block group %d\n", i);
1133 }
1134 goto fail;
1135 } else {
1136restart:
1137 fs->curr_inode_no++;
1138 /* get the blockbitmap index respective to blockno */
1139 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001140 struct ext2_block_group *bgd =
1141 ext4fs_get_group_descriptor(fs, ibmap_idx);
1142 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1143 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1144
1145 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1146 put_ext4(i_bitmap_blk * fs->blksz,
Michael Walle58a9ecb2016-09-01 11:21:40 +02001147 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001148 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1149 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301150 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1151 fs->blksz);
1152 }
1153
1154 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1155 fs->inode_bmaps[ibmap_idx],
1156 ibmap_idx) != 0) {
1157 debug("going for restart for the block no %d %u\n",
1158 fs->curr_inode_no, ibmap_idx);
1159 goto restart;
1160 }
1161
1162 /* journal backup */
1163 if (prev_inode_bitmap_index != ibmap_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001164 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301165 0, fs->blksz, journal_buffer);
1166 if (status == 0)
1167 goto fail;
1168 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001169 le32_to_cpu(bgd->inode_id)))
Uma Shankared34f342012-05-25 21:22:49 +05301170 goto fail;
1171 prev_inode_bitmap_index = ibmap_idx;
1172 }
Stefan Brüns749e93e2016-09-20 01:13:01 +02001173 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001174 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001175 bgd->bg_itable_unused = bgd->free_inodes;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001176 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301177 goto success;
1178 }
1179
1180success:
1181 free(journal_buffer);
1182 free(zero_buffer);
1183
1184 return fs->curr_inode_no;
1185fail:
1186 free(journal_buffer);
1187 free(zero_buffer);
1188
1189 return -1;
1190
1191}
1192
1193
1194static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1195 unsigned int *total_remaining_blocks,
1196 unsigned int *no_blks_reqd)
1197{
1198 short i;
1199 short status;
1200 long int actual_block_no;
1201 long int si_blockno;
1202 /* si :single indirect */
Michael Walle58a9ecb2016-09-01 11:21:40 +02001203 __le32 *si_buffer = NULL;
1204 __le32 *si_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301205 struct ext_filesystem *fs = get_fs();
1206
1207 if (*total_remaining_blocks != 0) {
1208 si_buffer = zalloc(fs->blksz);
1209 if (!si_buffer) {
1210 printf("No Memory\n");
1211 return;
1212 }
1213 si_start_addr = si_buffer;
1214 si_blockno = ext4fs_get_new_blk_no();
1215 if (si_blockno == -1) {
1216 printf("no block left to assign\n");
1217 goto fail;
1218 }
1219 (*no_blks_reqd)++;
1220 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1221
Frederic Leroy04735e92013-06-26 18:11:25 +02001222 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301223 0, fs->blksz, (char *)si_buffer);
1224 memset(si_buffer, '\0', fs->blksz);
1225 if (status == 0)
1226 goto fail;
1227
1228 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1229 actual_block_no = ext4fs_get_new_blk_no();
1230 if (actual_block_no == -1) {
1231 printf("no block left to assign\n");
1232 goto fail;
1233 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001234 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301235 debug("SIAB %u: %u\n", *si_buffer,
1236 *total_remaining_blocks);
1237
1238 si_buffer++;
1239 (*total_remaining_blocks)--;
1240 if (*total_remaining_blocks == 0)
1241 break;
1242 }
1243
1244 /* write the block to disk */
Ma Haijun05508702014-01-08 08:15:33 +08001245 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301246 si_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001247 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301248 }
1249fail:
1250 free(si_start_addr);
1251}
1252
1253static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1254 unsigned int *total_remaining_blocks,
1255 unsigned int *no_blks_reqd)
1256{
1257 short i;
1258 short j;
1259 short status;
1260 long int actual_block_no;
1261 /* di:double indirect */
1262 long int di_blockno_parent;
1263 long int di_blockno_child;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001264 __le32 *di_parent_buffer = NULL;
1265 __le32 *di_child_buff = NULL;
1266 __le32 *di_block_start_addr = NULL;
1267 __le32 *di_child_buff_start = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301268 struct ext_filesystem *fs = get_fs();
1269
1270 if (*total_remaining_blocks != 0) {
1271 /* double indirect parent block connecting to inode */
1272 di_blockno_parent = ext4fs_get_new_blk_no();
1273 if (di_blockno_parent == -1) {
1274 printf("no block left to assign\n");
1275 goto fail;
1276 }
1277 di_parent_buffer = zalloc(fs->blksz);
1278 if (!di_parent_buffer)
1279 goto fail;
1280
1281 di_block_start_addr = di_parent_buffer;
1282 (*no_blks_reqd)++;
1283 debug("DIPB %ld: %u\n", di_blockno_parent,
1284 *total_remaining_blocks);
1285
Frederic Leroy04735e92013-06-26 18:11:25 +02001286 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankared34f342012-05-25 21:22:49 +05301287 fs->sect_perblk, 0,
1288 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001289
1290 if (!status) {
1291 printf("%s: Device read error!\n", __func__);
1292 goto fail;
1293 }
Uma Shankared34f342012-05-25 21:22:49 +05301294 memset(di_parent_buffer, '\0', fs->blksz);
1295
1296 /*
1297 * start:for each double indirect parent
1298 * block create one more block
1299 */
1300 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1301 di_blockno_child = ext4fs_get_new_blk_no();
1302 if (di_blockno_child == -1) {
1303 printf("no block left to assign\n");
1304 goto fail;
1305 }
1306 di_child_buff = zalloc(fs->blksz);
1307 if (!di_child_buff)
1308 goto fail;
1309
1310 di_child_buff_start = di_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001311 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankared34f342012-05-25 21:22:49 +05301312 di_parent_buffer++;
1313 (*no_blks_reqd)++;
1314 debug("DICB %ld: %u\n", di_blockno_child,
1315 *total_remaining_blocks);
1316
Frederic Leroy04735e92013-06-26 18:11:25 +02001317 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankared34f342012-05-25 21:22:49 +05301318 fs->sect_perblk, 0,
1319 fs->blksz,
1320 (char *)di_child_buff);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001321
1322 if (!status) {
1323 printf("%s: Device read error!\n", __func__);
1324 goto fail;
1325 }
Uma Shankared34f342012-05-25 21:22:49 +05301326 memset(di_child_buff, '\0', fs->blksz);
1327 /* filling of actual datablocks for each child */
1328 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1329 actual_block_no = ext4fs_get_new_blk_no();
1330 if (actual_block_no == -1) {
1331 printf("no block left to assign\n");
1332 goto fail;
1333 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001334 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301335 debug("DIAB %ld: %u\n", actual_block_no,
1336 *total_remaining_blocks);
1337
1338 di_child_buff++;
1339 (*total_remaining_blocks)--;
1340 if (*total_remaining_blocks == 0)
1341 break;
1342 }
1343 /* write the block table */
Ma Haijun05508702014-01-08 08:15:33 +08001344 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301345 di_child_buff_start, fs->blksz);
1346 free(di_child_buff_start);
1347 di_child_buff_start = NULL;
1348
1349 if (*total_remaining_blocks == 0)
1350 break;
1351 }
Ma Haijun05508702014-01-08 08:15:33 +08001352 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301353 di_block_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001354 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankared34f342012-05-25 21:22:49 +05301355 }
1356fail:
1357 free(di_block_start_addr);
1358}
1359
1360static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1361 unsigned int *total_remaining_blocks,
1362 unsigned int *no_blks_reqd)
1363{
1364 short i;
1365 short j;
1366 short k;
1367 long int actual_block_no;
1368 /* ti: Triple Indirect */
1369 long int ti_gp_blockno;
1370 long int ti_parent_blockno;
1371 long int ti_child_blockno;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001372 __le32 *ti_gp_buff = NULL;
1373 __le32 *ti_parent_buff = NULL;
1374 __le32 *ti_child_buff = NULL;
1375 __le32 *ti_gp_buff_start_addr = NULL;
1376 __le32 *ti_pbuff_start_addr = NULL;
1377 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301378 struct ext_filesystem *fs = get_fs();
1379 if (*total_remaining_blocks != 0) {
1380 /* triple indirect grand parent block connecting to inode */
1381 ti_gp_blockno = ext4fs_get_new_blk_no();
1382 if (ti_gp_blockno == -1) {
1383 printf("no block left to assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001384 return;
Uma Shankared34f342012-05-25 21:22:49 +05301385 }
1386 ti_gp_buff = zalloc(fs->blksz);
1387 if (!ti_gp_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001388 return;
Uma Shankared34f342012-05-25 21:22:49 +05301389
1390 ti_gp_buff_start_addr = ti_gp_buff;
1391 (*no_blks_reqd)++;
1392 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1393 *total_remaining_blocks);
1394
1395 /* for each 4 byte grand parent entry create one more block */
1396 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1397 ti_parent_blockno = ext4fs_get_new_blk_no();
1398 if (ti_parent_blockno == -1) {
1399 printf("no block left to assign\n");
1400 goto fail;
1401 }
1402 ti_parent_buff = zalloc(fs->blksz);
1403 if (!ti_parent_buff)
1404 goto fail;
1405
1406 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001407 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301408 ti_gp_buff++;
1409 (*no_blks_reqd)++;
1410 debug("TIPB %ld: %u\n", ti_parent_blockno,
1411 *total_remaining_blocks);
1412
1413 /* for each 4 byte entry parent create one more block */
1414 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1415 ti_child_blockno = ext4fs_get_new_blk_no();
1416 if (ti_child_blockno == -1) {
1417 printf("no block left assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001418 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301419 }
1420 ti_child_buff = zalloc(fs->blksz);
1421 if (!ti_child_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001422 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301423
1424 ti_cbuff_start_addr = ti_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001425 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301426 ti_parent_buff++;
1427 (*no_blks_reqd)++;
1428 debug("TICB %ld: %u\n", ti_parent_blockno,
1429 *total_remaining_blocks);
1430
1431 /* fill actual datablocks for each child */
1432 for (k = 0; k < (fs->blksz / sizeof(int));
1433 k++) {
1434 actual_block_no =
1435 ext4fs_get_new_blk_no();
1436 if (actual_block_no == -1) {
1437 printf("no block left\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001438 free(ti_cbuff_start_addr);
1439 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301440 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001441 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301442 debug("TIAB %ld: %u\n", actual_block_no,
1443 *total_remaining_blocks);
1444
1445 ti_child_buff++;
1446 (*total_remaining_blocks)--;
1447 if (*total_remaining_blocks == 0)
1448 break;
1449 }
1450 /* write the child block */
Ma Haijun05508702014-01-08 08:15:33 +08001451 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1452 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301453 ti_cbuff_start_addr, fs->blksz);
1454 free(ti_cbuff_start_addr);
1455
1456 if (*total_remaining_blocks == 0)
1457 break;
1458 }
1459 /* write the parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001460 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301461 ti_pbuff_start_addr, fs->blksz);
1462 free(ti_pbuff_start_addr);
1463
1464 if (*total_remaining_blocks == 0)
1465 break;
1466 }
1467 /* write the grand parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001468 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301469 ti_gp_buff_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001470 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini495c3a12015-12-10 16:42:21 -05001471 free(ti_gp_buff_start_addr);
1472 return;
Uma Shankared34f342012-05-25 21:22:49 +05301473 }
Tom Rini495c3a12015-12-10 16:42:21 -05001474fail1:
1475 free(ti_pbuff_start_addr);
Uma Shankared34f342012-05-25 21:22:49 +05301476fail:
1477 free(ti_gp_buff_start_addr);
1478}
1479
1480void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1481 unsigned int total_remaining_blocks,
1482 unsigned int *total_no_of_block)
1483{
1484 short i;
1485 long int direct_blockno;
1486 unsigned int no_blks_reqd = 0;
1487
1488 /* allocation of direct blocks */
Stephen Warrend0180282014-06-11 12:46:16 -06001489 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankared34f342012-05-25 21:22:49 +05301490 direct_blockno = ext4fs_get_new_blk_no();
1491 if (direct_blockno == -1) {
1492 printf("no block left to assign\n");
1493 return;
1494 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001495 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301496 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1497
1498 total_remaining_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +05301499 }
1500
1501 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1502 &no_blks_reqd);
1503 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1504 &no_blks_reqd);
1505 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1506 &no_blks_reqd);
1507 *total_no_of_block += no_blks_reqd;
1508}
1509
1510#endif
1511
Uma Shankara1596432012-05-25 21:21:44 +05301512static struct ext4_extent_header *ext4fs_get_extent_block
1513 (struct ext2_data *data, char *buf,
1514 struct ext4_extent_header *ext_block,
1515 uint32_t fileblock, int log2_blksz)
1516{
1517 struct ext4_extent_idx *index;
1518 unsigned long long block;
Ionut Nicu47017322014-01-13 11:59:24 +01001519 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankara1596432012-05-25 21:21:44 +05301520 int i;
1521
1522 while (1) {
1523 index = (struct ext4_extent_idx *)(ext_block + 1);
1524
Rommel Custodio8b415f72013-07-21 10:53:25 +02001525 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001526 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301527
1528 if (ext_block->eh_depth == 0)
1529 return ext_block;
1530 i = -1;
1531 do {
1532 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001533 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301534 break;
Ionut Nicub5bbac12014-01-13 12:00:08 +01001535 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankara1596432012-05-25 21:21:44 +05301536
1537 if (--i < 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001538 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301539
Rommel Custodio8b415f72013-07-21 10:53:25 +02001540 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301541 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1542
Ionut Nicu47017322014-01-13 11:59:24 +01001543 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
Frederic Leroy04735e92013-06-26 18:11:25 +02001544 buf))
Uma Shankara1596432012-05-25 21:21:44 +05301545 ext_block = (struct ext4_extent_header *)buf;
1546 else
Michael Walle58a9ecb2016-09-01 11:21:40 +02001547 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301548 }
1549}
1550
1551static int ext4fs_blockgroup
1552 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1553{
1554 long int blkno;
1555 unsigned int blkoff, desc_per_blk;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001556 int log2blksz = get_fs()->dev_desc->log2blksz;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001557 int desc_size = get_fs()->gdsize;
Uma Shankara1596432012-05-25 21:21:44 +05301558
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001559 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301560
Michael Walle7f101be2016-08-29 10:46:44 +02001561 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankara1596432012-05-25 21:21:44 +05301562 group / desc_per_blk;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001563 blkoff = (group % desc_per_blk) * desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301564
1565 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1566 group, blkno, blkoff);
1567
Frederic Leroy04735e92013-06-26 18:11:25 +02001568 return ext4fs_devread((lbaint_t)blkno <<
1569 (LOG2_BLOCK_SIZE(data) - log2blksz),
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001570 blkoff, desc_size, (char *)blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301571}
1572
1573int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1574{
1575 struct ext2_block_group blkgrp;
1576 struct ext2_sblock *sblock = &data->sblock;
1577 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00001578 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301579 int inodes_per_block, status;
1580 long int blkno;
1581 unsigned int blkoff;
1582
1583 /* It is easier to calculate if the first inode is 0. */
1584 ino--;
Michael Walle7f101be2016-08-29 10:46:44 +02001585 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301586 (sblock->inodes_per_group), &blkgrp);
1587 if (status == 0)
1588 return 0;
1589
1590 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001591 blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
Michael Walle7f101be2016-08-29 10:46:44 +02001592 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankara1596432012-05-25 21:21:44 +05301593 blkoff = (ino % inodes_per_block) * fs->inodesz;
1594 /* Read the inode. */
Frederic Leroy04735e92013-06-26 18:11:25 +02001595 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1596 log2blksz), blkoff,
Uma Shankara1596432012-05-25 21:21:44 +05301597 sizeof(struct ext2_inode), (char *)inode);
1598 if (status == 0)
1599 return 0;
1600
1601 return 1;
1602}
1603
1604long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1605{
1606 long int blknr;
1607 int blksz;
1608 int log2_blksz;
1609 int status;
1610 long int rblock;
1611 long int perblock_parent;
1612 long int perblock_child;
1613 unsigned long long start;
1614 /* get the blocksize of the filesystem */
1615 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +00001616 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1617 - get_fs()->dev_desc->log2blksz;
1618
Uma Shankara1596432012-05-25 21:21:44 +05301619 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1620 char *buf = zalloc(blksz);
1621 if (!buf)
1622 return -ENOMEM;
1623 struct ext4_extent_header *ext_block;
1624 struct ext4_extent *extent;
1625 int i = -1;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001626 ext_block =
1627 ext4fs_get_extent_block(ext4fs_root, buf,
1628 (struct ext4_extent_header *)
1629 inode->b.blocks.dir_blocks,
1630 fileblock, log2_blksz);
Uma Shankara1596432012-05-25 21:21:44 +05301631 if (!ext_block) {
1632 printf("invalid extent block\n");
1633 free(buf);
1634 return -EINVAL;
1635 }
1636
1637 extent = (struct ext4_extent *)(ext_block + 1);
1638
1639 do {
1640 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001641 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301642 break;
1643 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1644 if (--i >= 0) {
1645 fileblock -= le32_to_cpu(extent[i].ee_block);
Rommel Custodio8b415f72013-07-21 10:53:25 +02001646 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
Uma Shankara1596432012-05-25 21:21:44 +05301647 free(buf);
1648 return 0;
1649 }
1650
Rommel Custodio8b415f72013-07-21 10:53:25 +02001651 start = le16_to_cpu(extent[i].ee_start_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301652 start = (start << 32) +
1653 le32_to_cpu(extent[i].ee_start_lo);
1654 free(buf);
1655 return fileblock + start;
1656 }
1657
1658 printf("Extent Error\n");
1659 free(buf);
1660 return -1;
1661 }
1662
1663 /* Direct blocks. */
1664 if (fileblock < INDIRECT_BLOCKS)
Michael Walle7f101be2016-08-29 10:46:44 +02001665 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301666
1667 /* Indirect. */
1668 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1669 if (ext4fs_indir1_block == NULL) {
1670 ext4fs_indir1_block = zalloc(blksz);
1671 if (ext4fs_indir1_block == NULL) {
1672 printf("** SI ext2fs read block (indir 1)"
1673 "malloc failed. **\n");
1674 return -1;
1675 }
1676 ext4fs_indir1_size = blksz;
1677 ext4fs_indir1_blkno = -1;
1678 }
1679 if (blksz != ext4fs_indir1_size) {
1680 free(ext4fs_indir1_block);
1681 ext4fs_indir1_block = NULL;
1682 ext4fs_indir1_size = 0;
1683 ext4fs_indir1_blkno = -1;
1684 ext4fs_indir1_block = zalloc(blksz);
1685 if (ext4fs_indir1_block == NULL) {
1686 printf("** SI ext2fs read block (indir 1):"
1687 "malloc failed. **\n");
1688 return -1;
1689 }
1690 ext4fs_indir1_size = blksz;
1691 }
Michael Walle7f101be2016-08-29 10:46:44 +02001692 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301693 log2_blksz) != ext4fs_indir1_blkno) {
1694 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001695 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301696 (inode->b.blocks.
1697 indir_block) << log2_blksz, 0,
1698 blksz, (char *)ext4fs_indir1_block);
1699 if (status == 0) {
1700 printf("** SI ext2fs read block (indir 1)"
1701 "failed. **\n");
Stefan Brünsde9e8312016-09-06 04:36:55 +02001702 return -1;
Uma Shankara1596432012-05-25 21:21:44 +05301703 }
1704 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001705 le32_to_cpu(inode->b.blocks.
Uma Shankara1596432012-05-25 21:21:44 +05301706 indir_block) << log2_blksz;
1707 }
Michael Walle7f101be2016-08-29 10:46:44 +02001708 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankara1596432012-05-25 21:21:44 +05301709 [fileblock - INDIRECT_BLOCKS]);
1710 }
1711 /* Double indirect. */
1712 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1713 (blksz / 4 + 1)))) {
1714
1715 long int perblock = blksz / 4;
1716 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1717
1718 if (ext4fs_indir1_block == NULL) {
1719 ext4fs_indir1_block = zalloc(blksz);
1720 if (ext4fs_indir1_block == NULL) {
1721 printf("** DI ext2fs read block (indir 2 1)"
1722 "malloc failed. **\n");
1723 return -1;
1724 }
1725 ext4fs_indir1_size = blksz;
1726 ext4fs_indir1_blkno = -1;
1727 }
1728 if (blksz != ext4fs_indir1_size) {
1729 free(ext4fs_indir1_block);
1730 ext4fs_indir1_block = NULL;
1731 ext4fs_indir1_size = 0;
1732 ext4fs_indir1_blkno = -1;
1733 ext4fs_indir1_block = zalloc(blksz);
1734 if (ext4fs_indir1_block == NULL) {
1735 printf("** DI ext2fs read block (indir 2 1)"
1736 "malloc failed. **\n");
1737 return -1;
1738 }
1739 ext4fs_indir1_size = blksz;
1740 }
Michael Walle7f101be2016-08-29 10:46:44 +02001741 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301742 log2_blksz) != ext4fs_indir1_blkno) {
1743 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001744 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301745 (inode->b.blocks.
1746 double_indir_block) << log2_blksz,
1747 0, blksz,
1748 (char *)ext4fs_indir1_block);
1749 if (status == 0) {
1750 printf("** DI ext2fs read block (indir 2 1)"
1751 "failed. **\n");
1752 return -1;
1753 }
1754 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001755 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301756 log2_blksz;
1757 }
1758
1759 if (ext4fs_indir2_block == NULL) {
1760 ext4fs_indir2_block = zalloc(blksz);
1761 if (ext4fs_indir2_block == NULL) {
1762 printf("** DI ext2fs read block (indir 2 2)"
1763 "malloc failed. **\n");
1764 return -1;
1765 }
1766 ext4fs_indir2_size = blksz;
1767 ext4fs_indir2_blkno = -1;
1768 }
1769 if (blksz != ext4fs_indir2_size) {
1770 free(ext4fs_indir2_block);
1771 ext4fs_indir2_block = NULL;
1772 ext4fs_indir2_size = 0;
1773 ext4fs_indir2_blkno = -1;
1774 ext4fs_indir2_block = zalloc(blksz);
1775 if (ext4fs_indir2_block == NULL) {
1776 printf("** DI ext2fs read block (indir 2 2)"
1777 "malloc failed. **\n");
1778 return -1;
1779 }
1780 ext4fs_indir2_size = blksz;
1781 }
Michael Walle7f101be2016-08-29 10:46:44 +02001782 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankara1596432012-05-25 21:21:44 +05301783 log2_blksz) != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001784 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301785 (ext4fs_indir1_block
1786 [rblock /
1787 perblock]) << log2_blksz, 0,
1788 blksz,
1789 (char *)ext4fs_indir2_block);
1790 if (status == 0) {
1791 printf("** DI ext2fs read block (indir 2 2)"
1792 "failed. **\n");
1793 return -1;
1794 }
1795 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001796 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301797 /
1798 perblock]) <<
1799 log2_blksz;
1800 }
Michael Walle7f101be2016-08-29 10:46:44 +02001801 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301802 }
1803 /* Tripple indirect. */
1804 else {
1805 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1806 (blksz / 4 * blksz / 4));
1807 perblock_child = blksz / 4;
1808 perblock_parent = ((blksz / 4) * (blksz / 4));
1809
1810 if (ext4fs_indir1_block == NULL) {
1811 ext4fs_indir1_block = zalloc(blksz);
1812 if (ext4fs_indir1_block == NULL) {
1813 printf("** TI ext2fs read block (indir 2 1)"
1814 "malloc failed. **\n");
1815 return -1;
1816 }
1817 ext4fs_indir1_size = blksz;
1818 ext4fs_indir1_blkno = -1;
1819 }
1820 if (blksz != ext4fs_indir1_size) {
1821 free(ext4fs_indir1_block);
1822 ext4fs_indir1_block = NULL;
1823 ext4fs_indir1_size = 0;
1824 ext4fs_indir1_blkno = -1;
1825 ext4fs_indir1_block = zalloc(blksz);
1826 if (ext4fs_indir1_block == NULL) {
1827 printf("** TI ext2fs read block (indir 2 1)"
1828 "malloc failed. **\n");
1829 return -1;
1830 }
1831 ext4fs_indir1_size = blksz;
1832 }
Michael Walle7f101be2016-08-29 10:46:44 +02001833 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301834 log2_blksz) != ext4fs_indir1_blkno) {
1835 status = ext4fs_devread
Frederic Leroy04735e92013-06-26 18:11:25 +02001836 ((lbaint_t)
Michael Walle7f101be2016-08-29 10:46:44 +02001837 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankara1596432012-05-25 21:21:44 +05301838 << log2_blksz, 0, blksz,
1839 (char *)ext4fs_indir1_block);
1840 if (status == 0) {
1841 printf("** TI ext2fs read block (indir 2 1)"
1842 "failed. **\n");
1843 return -1;
1844 }
1845 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001846 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301847 log2_blksz;
1848 }
1849
1850 if (ext4fs_indir2_block == NULL) {
1851 ext4fs_indir2_block = zalloc(blksz);
1852 if (ext4fs_indir2_block == NULL) {
1853 printf("** TI ext2fs read block (indir 2 2)"
1854 "malloc failed. **\n");
1855 return -1;
1856 }
1857 ext4fs_indir2_size = blksz;
1858 ext4fs_indir2_blkno = -1;
1859 }
1860 if (blksz != ext4fs_indir2_size) {
1861 free(ext4fs_indir2_block);
1862 ext4fs_indir2_block = NULL;
1863 ext4fs_indir2_size = 0;
1864 ext4fs_indir2_blkno = -1;
1865 ext4fs_indir2_block = zalloc(blksz);
1866 if (ext4fs_indir2_block == NULL) {
1867 printf("** TI ext2fs read block (indir 2 2)"
1868 "malloc failed. **\n");
1869 return -1;
1870 }
1871 ext4fs_indir2_size = blksz;
1872 }
Michael Walle7f101be2016-08-29 10:46:44 +02001873 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301874 perblock_parent]) <<
1875 log2_blksz)
1876 != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001877 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301878 (ext4fs_indir1_block
1879 [rblock /
1880 perblock_parent]) <<
1881 log2_blksz, 0, blksz,
1882 (char *)ext4fs_indir2_block);
1883 if (status == 0) {
1884 printf("** TI ext2fs read block (indir 2 2)"
1885 "failed. **\n");
1886 return -1;
1887 }
1888 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001889 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301890 perblock_parent])
1891 << log2_blksz;
1892 }
1893
1894 if (ext4fs_indir3_block == NULL) {
1895 ext4fs_indir3_block = zalloc(blksz);
1896 if (ext4fs_indir3_block == NULL) {
1897 printf("** TI ext2fs read block (indir 2 2)"
1898 "malloc failed. **\n");
1899 return -1;
1900 }
1901 ext4fs_indir3_size = blksz;
1902 ext4fs_indir3_blkno = -1;
1903 }
1904 if (blksz != ext4fs_indir3_size) {
1905 free(ext4fs_indir3_block);
1906 ext4fs_indir3_block = NULL;
1907 ext4fs_indir3_size = 0;
1908 ext4fs_indir3_blkno = -1;
1909 ext4fs_indir3_block = zalloc(blksz);
1910 if (ext4fs_indir3_block == NULL) {
1911 printf("** TI ext2fs read block (indir 2 2)"
1912 "malloc failed. **\n");
1913 return -1;
1914 }
1915 ext4fs_indir3_size = blksz;
1916 }
Michael Walle7f101be2016-08-29 10:46:44 +02001917 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301918 /
1919 perblock_child]) <<
1920 log2_blksz) != ext4fs_indir3_blkno) {
1921 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001922 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301923 (ext4fs_indir2_block
1924 [(rblock / perblock_child)
1925 % (blksz / 4)]) << log2_blksz, 0,
1926 blksz, (char *)ext4fs_indir3_block);
1927 if (status == 0) {
1928 printf("** TI ext2fs read block (indir 2 2)"
1929 "failed. **\n");
1930 return -1;
1931 }
1932 ext4fs_indir3_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001933 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301934 perblock_child) %
1935 (blksz /
1936 4)]) <<
1937 log2_blksz;
1938 }
1939
Michael Walle7f101be2016-08-29 10:46:44 +02001940 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankara1596432012-05-25 21:21:44 +05301941 [rblock % perblock_child]);
1942 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001943 debug("read_allocated_block %ld\n", blknr);
Uma Shankara1596432012-05-25 21:21:44 +05301944
1945 return blknr;
1946}
1947
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001948/**
1949 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1950 * global pointers
1951 *
1952 * This function assures that for a file with the same name but different size
1953 * the sequential store on the ext4 filesystem will be correct.
1954 *
1955 * In this function the global data, responsible for internal representation
1956 * of the ext4 data are initialized to the reset state. Without this, during
1957 * replacement of the smaller file with the bigger truncation of new file was
1958 * performed.
1959 */
1960void ext4fs_reinit_global(void)
Uma Shankara1596432012-05-25 21:21:44 +05301961{
Uma Shankara1596432012-05-25 21:21:44 +05301962 if (ext4fs_indir1_block != NULL) {
1963 free(ext4fs_indir1_block);
1964 ext4fs_indir1_block = NULL;
1965 ext4fs_indir1_size = 0;
1966 ext4fs_indir1_blkno = -1;
1967 }
1968 if (ext4fs_indir2_block != NULL) {
1969 free(ext4fs_indir2_block);
1970 ext4fs_indir2_block = NULL;
1971 ext4fs_indir2_size = 0;
1972 ext4fs_indir2_blkno = -1;
1973 }
1974 if (ext4fs_indir3_block != NULL) {
1975 free(ext4fs_indir3_block);
1976 ext4fs_indir3_block = NULL;
1977 ext4fs_indir3_size = 0;
1978 ext4fs_indir3_blkno = -1;
1979 }
1980}
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001981void ext4fs_close(void)
1982{
1983 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1984 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1985 ext4fs_file = NULL;
1986 }
1987 if (ext4fs_root != NULL) {
1988 free(ext4fs_root);
1989 ext4fs_root = NULL;
1990 }
1991
1992 ext4fs_reinit_global();
1993}
Uma Shankara1596432012-05-25 21:21:44 +05301994
1995int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1996 struct ext2fs_node **fnode, int *ftype)
1997{
1998 unsigned int fpos = 0;
1999 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002000 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302001 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2002
2003#ifdef DEBUG
2004 if (name != NULL)
2005 printf("Iterate dir %s\n", name);
2006#endif /* of DEBUG */
2007 if (!diro->inode_read) {
2008 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2009 if (status == 0)
2010 return 0;
2011 }
2012 /* Search the file. */
Michael Walle7f101be2016-08-29 10:46:44 +02002013 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankara1596432012-05-25 21:21:44 +05302014 struct ext2_dirent dirent;
2015
2016 status = ext4fs_read_file(diro, fpos,
2017 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002018 (char *)&dirent, &actread);
2019 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302020 return 0;
2021
Thomas Fitzsimmons54d68e92015-11-18 12:42:53 -05002022 if (dirent.direntlen == 0) {
2023 printf("Failed to iterate over directory %s\n", name);
2024 return 0;
2025 }
2026
Uma Shankara1596432012-05-25 21:21:44 +05302027 if (dirent.namelen != 0) {
2028 char filename[dirent.namelen + 1];
2029 struct ext2fs_node *fdiro;
2030 int type = FILETYPE_UNKNOWN;
2031
2032 status = ext4fs_read_file(diro,
2033 fpos +
2034 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002035 dirent.namelen, filename,
2036 &actread);
2037 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302038 return 0;
2039
2040 fdiro = zalloc(sizeof(struct ext2fs_node));
2041 if (!fdiro)
2042 return 0;
2043
2044 fdiro->data = diro->data;
Michael Walle7f101be2016-08-29 10:46:44 +02002045 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankara1596432012-05-25 21:21:44 +05302046
2047 filename[dirent.namelen] = '\0';
2048
2049 if (dirent.filetype != FILETYPE_UNKNOWN) {
2050 fdiro->inode_read = 0;
2051
2052 if (dirent.filetype == FILETYPE_DIRECTORY)
2053 type = FILETYPE_DIRECTORY;
2054 else if (dirent.filetype == FILETYPE_SYMLINK)
2055 type = FILETYPE_SYMLINK;
2056 else if (dirent.filetype == FILETYPE_REG)
2057 type = FILETYPE_REG;
2058 } else {
2059 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002060 le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05302061 (dirent.inode),
2062 &fdiro->inode);
2063 if (status == 0) {
2064 free(fdiro);
2065 return 0;
2066 }
2067 fdiro->inode_read = 1;
2068
Michael Walle7f101be2016-08-29 10:46:44 +02002069 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankara1596432012-05-25 21:21:44 +05302070 FILETYPE_INO_MASK) ==
2071 FILETYPE_INO_DIRECTORY) {
2072 type = FILETYPE_DIRECTORY;
Michael Walle7f101be2016-08-29 10:46:44 +02002073 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302074 & FILETYPE_INO_MASK) ==
2075 FILETYPE_INO_SYMLINK) {
2076 type = FILETYPE_SYMLINK;
Michael Walle7f101be2016-08-29 10:46:44 +02002077 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302078 & FILETYPE_INO_MASK) ==
2079 FILETYPE_INO_REG) {
2080 type = FILETYPE_REG;
2081 }
2082 }
2083#ifdef DEBUG
2084 printf("iterate >%s<\n", filename);
2085#endif /* of DEBUG */
2086 if ((name != NULL) && (fnode != NULL)
2087 && (ftype != NULL)) {
2088 if (strcmp(filename, name) == 0) {
2089 *ftype = type;
2090 *fnode = fdiro;
2091 return 1;
2092 }
2093 } else {
2094 if (fdiro->inode_read == 0) {
2095 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002096 le32_to_cpu(
Uma Shankara1596432012-05-25 21:21:44 +05302097 dirent.inode),
2098 &fdiro->inode);
2099 if (status == 0) {
2100 free(fdiro);
2101 return 0;
2102 }
2103 fdiro->inode_read = 1;
2104 }
2105 switch (type) {
2106 case FILETYPE_DIRECTORY:
2107 printf("<DIR> ");
2108 break;
2109 case FILETYPE_SYMLINK:
2110 printf("<SYM> ");
2111 break;
2112 case FILETYPE_REG:
2113 printf(" ");
2114 break;
2115 default:
2116 printf("< ? > ");
2117 break;
2118 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002119 printf("%10u %s\n",
Michael Walle7f101be2016-08-29 10:46:44 +02002120 le32_to_cpu(fdiro->inode.size),
Uma Shankara1596432012-05-25 21:21:44 +05302121 filename);
2122 }
2123 free(fdiro);
2124 }
Michael Walle7f101be2016-08-29 10:46:44 +02002125 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankara1596432012-05-25 21:21:44 +05302126 }
2127 return 0;
2128}
2129
2130static char *ext4fs_read_symlink(struct ext2fs_node *node)
2131{
2132 char *symlink;
2133 struct ext2fs_node *diro = node;
2134 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002135 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302136
2137 if (!diro->inode_read) {
2138 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2139 if (status == 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002140 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302141 }
Michael Walle7f101be2016-08-29 10:46:44 +02002142 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankara1596432012-05-25 21:21:44 +05302143 if (!symlink)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002144 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302145
Michael Walle7f101be2016-08-29 10:46:44 +02002146 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankara1596432012-05-25 21:21:44 +05302147 strncpy(symlink, diro->inode.b.symlink,
Michael Walle7f101be2016-08-29 10:46:44 +02002148 le32_to_cpu(diro->inode.size));
Uma Shankara1596432012-05-25 21:21:44 +05302149 } else {
2150 status = ext4fs_read_file(diro, 0,
Michael Walle7f101be2016-08-29 10:46:44 +02002151 le32_to_cpu(diro->inode.size),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002152 symlink, &actread);
Gary Bisson9d2f6a92015-09-07 11:20:07 +02002153 if ((status < 0) || (actread == 0)) {
Uma Shankara1596432012-05-25 21:21:44 +05302154 free(symlink);
Michael Walle58a9ecb2016-09-01 11:21:40 +02002155 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302156 }
2157 }
Michael Walle7f101be2016-08-29 10:46:44 +02002158 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankara1596432012-05-25 21:21:44 +05302159 return symlink;
2160}
2161
2162static int ext4fs_find_file1(const char *currpath,
2163 struct ext2fs_node *currroot,
2164 struct ext2fs_node **currfound, int *foundtype)
2165{
2166 char fpath[strlen(currpath) + 1];
2167 char *name = fpath;
2168 char *next;
2169 int status;
2170 int type = FILETYPE_DIRECTORY;
2171 struct ext2fs_node *currnode = currroot;
2172 struct ext2fs_node *oldnode = currroot;
2173
2174 strncpy(fpath, currpath, strlen(currpath) + 1);
2175
2176 /* Remove all leading slashes. */
2177 while (*name == '/')
2178 name++;
2179
2180 if (!*name) {
2181 *currfound = currnode;
2182 return 1;
2183 }
2184
2185 for (;;) {
2186 int found;
2187
2188 /* Extract the actual part from the pathname. */
2189 next = strchr(name, '/');
2190 if (next) {
2191 /* Remove all leading slashes. */
2192 while (*next == '/')
2193 *(next++) = '\0';
2194 }
2195
2196 if (type != FILETYPE_DIRECTORY) {
2197 ext4fs_free_node(currnode, currroot);
2198 return 0;
2199 }
2200
2201 oldnode = currnode;
2202
2203 /* Iterate over the directory. */
2204 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2205 if (found == 0)
2206 return 0;
2207
2208 if (found == -1)
2209 break;
2210
2211 /* Read in the symlink and follow it. */
2212 if (type == FILETYPE_SYMLINK) {
2213 char *symlink;
2214
2215 /* Test if the symlink does not loop. */
2216 if (++symlinknest == 8) {
2217 ext4fs_free_node(currnode, currroot);
2218 ext4fs_free_node(oldnode, currroot);
2219 return 0;
2220 }
2221
2222 symlink = ext4fs_read_symlink(currnode);
2223 ext4fs_free_node(currnode, currroot);
2224
2225 if (!symlink) {
2226 ext4fs_free_node(oldnode, currroot);
2227 return 0;
2228 }
2229
2230 debug("Got symlink >%s<\n", symlink);
2231
2232 if (symlink[0] == '/') {
2233 ext4fs_free_node(oldnode, currroot);
2234 oldnode = &ext4fs_root->diropen;
2235 }
2236
2237 /* Lookup the node the symlink points to. */
2238 status = ext4fs_find_file1(symlink, oldnode,
2239 &currnode, &type);
2240
2241 free(symlink);
2242
2243 if (status == 0) {
2244 ext4fs_free_node(oldnode, currroot);
2245 return 0;
2246 }
2247 }
2248
2249 ext4fs_free_node(oldnode, currroot);
2250
2251 /* Found the node! */
2252 if (!next || *next == '\0') {
2253 *currfound = currnode;
2254 *foundtype = type;
2255 return 1;
2256 }
2257 name = next;
2258 }
2259 return -1;
2260}
2261
2262int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2263 struct ext2fs_node **foundnode, int expecttype)
2264{
2265 int status;
2266 int foundtype = FILETYPE_DIRECTORY;
2267
2268 symlinknest = 0;
2269 if (!path)
2270 return 0;
2271
2272 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2273 if (status == 0)
2274 return 0;
2275
2276 /* Check if the node that was found was of the expected type. */
2277 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2278 return 0;
2279 else if ((expecttype == FILETYPE_DIRECTORY)
2280 && (foundtype != expecttype))
2281 return 0;
2282
2283 return 1;
2284}
2285
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002286int ext4fs_open(const char *filename, loff_t *len)
Uma Shankara1596432012-05-25 21:21:44 +05302287{
2288 struct ext2fs_node *fdiro = NULL;
2289 int status;
Uma Shankara1596432012-05-25 21:21:44 +05302290
2291 if (ext4fs_root == NULL)
2292 return -1;
2293
2294 ext4fs_file = NULL;
2295 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2296 FILETYPE_REG);
2297 if (status == 0)
2298 goto fail;
2299
2300 if (!fdiro->inode_read) {
2301 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2302 &fdiro->inode);
2303 if (status == 0)
2304 goto fail;
2305 }
Michael Walle7f101be2016-08-29 10:46:44 +02002306 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankara1596432012-05-25 21:21:44 +05302307 ext4fs_file = fdiro;
2308
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002309 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05302310fail:
2311 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2312
2313 return -1;
2314}
2315
2316int ext4fs_mount(unsigned part_length)
2317{
2318 struct ext2_data *data;
2319 int status;
2320 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00002321 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankara1596432012-05-25 21:21:44 +05302322 if (!data)
2323 return 0;
2324
2325 /* Read the superblock. */
Egbert Eich50ce4c02013-05-01 01:13:19 +00002326 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankara1596432012-05-25 21:21:44 +05302327
2328 if (status == 0)
2329 goto fail;
2330
2331 /* Make sure this is an ext2 filesystem. */
Michael Walle7f101be2016-08-29 10:46:44 +02002332 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Uma Shankara1596432012-05-25 21:21:44 +05302333 goto fail;
2334
Tom Rini6f94ab62016-07-22 17:59:11 -04002335
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002336 if (le32_to_cpu(data->sblock.revision_level) == 0) {
Uma Shankara1596432012-05-25 21:21:44 +05302337 fs->inodesz = 128;
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002338 } else {
2339 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2340 __le32_to_cpu(data->sblock.feature_compatibility),
2341 __le32_to_cpu(data->sblock.feature_incompat),
2342 __le32_to_cpu(data->sblock.feature_ro_compat));
Uma Shankara1596432012-05-25 21:21:44 +05302343
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002344 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2345 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2346 EXT4_FEATURE_INCOMPAT_64BIT ?
2347 le16_to_cpu(data->sblock.descriptor_size) : 32;
2348 }
2349
2350 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2351 le32_to_cpu(data->sblock.revision_level),
2352 fs->inodesz, fs->gdsize);
Uma Shankara1596432012-05-25 21:21:44 +05302353
2354 data->diropen.data = data;
2355 data->diropen.ino = 2;
2356 data->diropen.inode_read = 1;
2357 data->inode = &data->diropen.inode;
2358
2359 status = ext4fs_read_inode(data, 2, data->inode);
2360 if (status == 0)
2361 goto fail;
2362
2363 ext4fs_root = data;
2364
2365 return 1;
2366fail:
2367 printf("Failed to mount ext2 filesystem...\n");
2368 free(data);
2369 ext4fs_root = NULL;
2370
2371 return 0;
2372}