blob: 727a2f753df391457010925aa237a46330b5cf5f [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;
36uint32_t *ext4fs_indir1_block;
37int ext4fs_indir1_size;
38int ext4fs_indir1_blkno = -1;
39uint32_t *ext4fs_indir2_block;
40int ext4fs_indir2_size;
41int ext4fs_indir2_blkno = -1;
42
43uint32_t *ext4fs_indir3_block;
44int 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)
Uma Shankared34f342012-05-25 21:22:49 +053050uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
51{
52 uint32_t res = size / n;
53 if (res * n != size)
54 res++;
55
56 return res;
57}
58
59void put_ext4(uint64_t off, void *buf, uint32_t size)
60{
61 uint64_t startblock;
62 uint64_t remainder;
63 unsigned char *temp_ptr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +053064 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +000065 int log2blksz = fs->dev_desc->log2blksz;
66 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankared34f342012-05-25 21:22:49 +053067
Egbert Eich50ce4c02013-05-01 01:13:19 +000068 startblock = off >> log2blksz;
Uma Shankared34f342012-05-25 21:22:49 +053069 startblock += part_offset;
Egbert Eich50ce4c02013-05-01 01:13:19 +000070 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankared34f342012-05-25 21:22:49 +053071
72 if (fs->dev_desc == NULL)
73 return;
74
Egbert Eich50ce4c02013-05-01 01:13:19 +000075 if ((startblock + (size >> log2blksz)) >
Uma Shankared34f342012-05-25 21:22:49 +053076 (part_offset + fs->total_sect)) {
Frederic Leroy04735e92013-06-26 18:11:25 +020077 printf("part_offset is " LBAFU "\n", part_offset);
Simon Glassaac618a2014-10-15 04:38:32 -060078 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
Uma Shankared34f342012-05-25 21:22:49 +053079 printf("error: overflow occurs\n");
80 return;
81 }
82
83 if (remainder) {
84 if (fs->dev_desc->block_read) {
85 fs->dev_desc->block_read(fs->dev_desc->dev,
86 startblock, 1, sec_buf);
87 temp_ptr = sec_buf;
88 memcpy((temp_ptr + remainder),
89 (unsigned char *)buf, size);
90 fs->dev_desc->block_write(fs->dev_desc->dev,
91 startblock, 1, sec_buf);
92 }
93 } else {
Egbert Eich50ce4c02013-05-01 01:13:19 +000094 if (size >> log2blksz != 0) {
Uma Shankared34f342012-05-25 21:22:49 +053095 fs->dev_desc->block_write(fs->dev_desc->dev,
96 startblock,
Egbert Eich50ce4c02013-05-01 01:13:19 +000097 size >> log2blksz,
Uma Shankared34f342012-05-25 21:22:49 +053098 (unsigned long *)buf);
99 } else {
100 fs->dev_desc->block_read(fs->dev_desc->dev,
101 startblock, 1, sec_buf);
102 temp_ptr = sec_buf;
103 memcpy(temp_ptr, buf, size);
104 fs->dev_desc->block_write(fs->dev_desc->dev,
105 startblock, 1,
106 (unsigned long *)sec_buf);
107 }
108 }
109}
110
111static int _get_new_inode_no(unsigned char *buffer)
112{
113 struct ext_filesystem *fs = get_fs();
114 unsigned char input;
115 int operand, status;
116 int count = 1;
117 int j = 0;
118
119 /* get the blocksize of the filesystem */
120 unsigned char *ptr = buffer;
121 while (*ptr == 255) {
122 ptr++;
123 count += 8;
124 if (count > ext4fs_root->sblock.inodes_per_group)
125 return -1;
126 }
127
128 for (j = 0; j < fs->blksz; j++) {
129 input = *ptr;
130 int i = 0;
131 while (i <= 7) {
132 operand = 1 << i;
133 status = input & operand;
134 if (status) {
135 i++;
136 count++;
137 } else {
138 *ptr |= operand;
139 return count;
140 }
141 }
142 ptr = ptr + 1;
143 }
144
145 return -1;
146}
147
148static int _get_new_blk_no(unsigned char *buffer)
149{
150 unsigned char input;
151 int operand, status;
152 int count = 0;
153 int j = 0;
154 unsigned char *ptr = buffer;
155 struct ext_filesystem *fs = get_fs();
156
157 if (fs->blksz != 1024)
158 count = 0;
159 else
160 count = 1;
161
162 while (*ptr == 255) {
163 ptr++;
164 count += 8;
165 if (count == (fs->blksz * 8))
166 return -1;
167 }
168
169 for (j = 0; j < fs->blksz; j++) {
170 input = *ptr;
171 int i = 0;
172 while (i <= 7) {
173 operand = 1 << i;
174 status = input & operand;
175 if (status) {
176 i++;
177 count++;
178 } else {
179 *ptr |= operand;
180 return count;
181 }
182 }
183 ptr = ptr + 1;
184 }
185
186 return -1;
187}
188
189int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
190{
191 int i, remainder, status;
192 unsigned char *ptr = buffer;
193 unsigned char operand;
194 i = blockno / 8;
195 remainder = blockno % 8;
196 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
197
198 i = i - (index * blocksize);
199 if (blocksize != 1024) {
200 ptr = ptr + i;
201 operand = 1 << remainder;
202 status = *ptr & operand;
203 if (status)
204 return -1;
205
206 *ptr = *ptr | operand;
207 return 0;
208 } else {
209 if (remainder == 0) {
210 ptr = ptr + i - 1;
211 operand = (1 << 7);
212 } else {
213 ptr = ptr + i;
214 operand = (1 << (remainder - 1));
215 }
216 status = *ptr & operand;
217 if (status)
218 return -1;
219
220 *ptr = *ptr | operand;
221 return 0;
222 }
223}
224
225void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
226{
227 int i, remainder, status;
228 unsigned char *ptr = buffer;
229 unsigned char operand;
230 i = blockno / 8;
231 remainder = blockno % 8;
232 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
233
234 i = i - (index * blocksize);
235 if (blocksize != 1024) {
236 ptr = ptr + i;
237 operand = (1 << remainder);
238 status = *ptr & operand;
239 if (status)
240 *ptr = *ptr & ~(operand);
241 } else {
242 if (remainder == 0) {
243 ptr = ptr + i - 1;
244 operand = (1 << 7);
245 } else {
246 ptr = ptr + i;
247 operand = (1 << (remainder - 1));
248 }
249 status = *ptr & operand;
250 if (status)
251 *ptr = *ptr & ~(operand);
252 }
253}
254
255int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
256{
257 int i, remainder, status;
258 unsigned char *ptr = buffer;
259 unsigned char operand;
260
261 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
262 i = inode_no / 8;
263 remainder = inode_no % 8;
264 if (remainder == 0) {
265 ptr = ptr + i - 1;
266 operand = (1 << 7);
267 } else {
268 ptr = ptr + i;
269 operand = (1 << (remainder - 1));
270 }
271 status = *ptr & operand;
272 if (status)
273 return -1;
274
275 *ptr = *ptr | operand;
276
277 return 0;
278}
279
280void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
281{
282 int i, remainder, status;
283 unsigned char *ptr = buffer;
284 unsigned char operand;
285
286 inode_no -= (index * ext4fs_root->sblock.inodes_per_group);
287 i = inode_no / 8;
288 remainder = inode_no % 8;
289 if (remainder == 0) {
290 ptr = ptr + i - 1;
291 operand = (1 << 7);
292 } else {
293 ptr = ptr + i;
294 operand = (1 << (remainder - 1));
295 }
296 status = *ptr & operand;
297 if (status)
298 *ptr = *ptr & ~(operand);
299}
300
301int ext4fs_checksum_update(unsigned int i)
302{
303 struct ext2_block_group *desc;
304 struct ext_filesystem *fs = get_fs();
305 __u16 crc = 0;
306
Simon Glass73c15c62012-10-03 11:37:49 +0000307 desc = (struct ext2_block_group *)&fs->bgd[i];
Uma Shankared34f342012-05-25 21:22:49 +0530308 if (fs->sb->feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
309 int offset = offsetof(struct ext2_block_group, bg_checksum);
310
311 crc = ext2fs_crc16(~0, fs->sb->unique_id,
312 sizeof(fs->sb->unique_id));
313 crc = ext2fs_crc16(crc, &i, sizeof(i));
314 crc = ext2fs_crc16(crc, desc, offset);
315 offset += sizeof(desc->bg_checksum); /* skip checksum */
316 assert(offset == sizeof(*desc));
317 }
318
319 return crc;
320}
321
322static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
323{
324 int dentry_length;
325 int sizeof_void_space;
326 int new_entry_byte_reqd;
327 short padding_factor = 0;
328
329 if (dir->namelen % 4 != 0)
330 padding_factor = 4 - (dir->namelen % 4);
331
332 dentry_length = sizeof(struct ext2_dirent) +
333 dir->namelen + padding_factor;
334 sizeof_void_space = dir->direntlen - dentry_length;
335 if (sizeof_void_space == 0)
336 return 0;
337
338 padding_factor = 0;
339 if (strlen(filename) % 4 != 0)
340 padding_factor = 4 - (strlen(filename) % 4);
341
342 new_entry_byte_reqd = strlen(filename) +
343 sizeof(struct ext2_dirent) + padding_factor;
344 if (sizeof_void_space >= new_entry_byte_reqd) {
345 dir->direntlen = dentry_length;
346 return sizeof_void_space;
347 }
348
349 return 0;
350}
351
352void ext4fs_update_parent_dentry(char *filename, int *p_ino, int file_type)
353{
354 unsigned int *zero_buffer = NULL;
355 char *root_first_block_buffer = NULL;
356 int direct_blk_idx;
357 long int root_blknr;
358 long int first_block_no_of_root = 0;
359 long int previous_blknr = -1;
360 int totalbytes = 0;
361 short int padding_factor = 0;
362 unsigned int new_entry_byte_reqd;
363 unsigned int last_entry_dirlen;
364 int sizeof_void_space = 0;
365 int templength = 0;
366 int inodeno;
367 int status;
368 struct ext_filesystem *fs = get_fs();
369 /* directory entry */
370 struct ext2_dirent *dir;
Uma Shankared34f342012-05-25 21:22:49 +0530371 char *temp_dir = NULL;
372
373 zero_buffer = zalloc(fs->blksz);
374 if (!zero_buffer) {
375 printf("No Memory\n");
376 return;
377 }
378 root_first_block_buffer = zalloc(fs->blksz);
379 if (!root_first_block_buffer) {
380 free(zero_buffer);
381 printf("No Memory\n");
382 return;
383 }
384restart:
385
386 /* read the block no allocated to a file */
387 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
388 direct_blk_idx++) {
389 root_blknr = read_allocated_block(g_parent_inode,
390 direct_blk_idx);
391 if (root_blknr == 0) {
392 first_block_no_of_root = previous_blknr;
393 break;
394 }
395 previous_blknr = root_blknr;
396 }
397
Frederic Leroy04735e92013-06-26 18:11:25 +0200398 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankared34f342012-05-25 21:22:49 +0530399 * fs->sect_perblk,
400 0, fs->blksz, root_first_block_buffer);
401 if (status == 0)
402 goto fail;
403
404 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
405 goto fail;
406 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankared34f342012-05-25 21:22:49 +0530407 totalbytes = 0;
408 while (dir->direntlen > 0) {
409 /*
410 * blocksize-totalbytes because last directory length
411 * i.e. dir->direntlen is free availble space in the
412 * block that means it is a last entry of directory
413 * entry
414 */
415
416 /* traversing the each directory entry */
417 if (fs->blksz - totalbytes == dir->direntlen) {
418 if (strlen(filename) % 4 != 0)
419 padding_factor = 4 - (strlen(filename) % 4);
420
421 new_entry_byte_reqd = strlen(filename) +
422 sizeof(struct ext2_dirent) + padding_factor;
423 padding_factor = 0;
424 /*
425 * update last directory entry length to its
426 * length because we are creating new directory
427 * entry
428 */
429 if (dir->namelen % 4 != 0)
430 padding_factor = 4 - (dir->namelen % 4);
431
432 last_entry_dirlen = dir->namelen +
433 sizeof(struct ext2_dirent) + padding_factor;
434 if ((fs->blksz - totalbytes - last_entry_dirlen) <
435 new_entry_byte_reqd) {
436 printf("1st Block Full:Allocate new block\n");
437
438 if (direct_blk_idx == INDIRECT_BLOCKS - 1) {
439 printf("Directory exceeds limit\n");
440 goto fail;
441 }
442 g_parent_inode->b.blocks.dir_blocks
443 [direct_blk_idx] = ext4fs_get_new_blk_no();
444 if (g_parent_inode->b.blocks.dir_blocks
445 [direct_blk_idx] == -1) {
446 printf("no block left to assign\n");
447 goto fail;
448 }
449 put_ext4(((uint64_t)
Ma Haijun05508702014-01-08 08:15:33 +0800450 ((uint64_t)g_parent_inode->b.
Uma Shankared34f342012-05-25 21:22:49 +0530451 blocks.dir_blocks[direct_blk_idx] *
Ma Haijun05508702014-01-08 08:15:33 +0800452 (uint64_t)fs->blksz)), zero_buffer, fs->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530453 g_parent_inode->size =
454 g_parent_inode->size + fs->blksz;
455 g_parent_inode->blockcnt =
456 g_parent_inode->blockcnt + fs->sect_perblk;
457 if (ext4fs_put_metadata
458 (root_first_block_buffer,
459 first_block_no_of_root))
460 goto fail;
461 goto restart;
462 }
463 dir->direntlen = last_entry_dirlen;
464 break;
465 }
466
467 templength = dir->direntlen;
468 totalbytes = totalbytes + templength;
469 sizeof_void_space = check_void_in_dentry(dir, filename);
470 if (sizeof_void_space)
471 break;
472
473 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530474 }
475
476 /* make a pointer ready for creating next directory entry */
477 templength = dir->direntlen;
478 totalbytes = totalbytes + templength;
479 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530480
481 /* get the next available inode number */
482 inodeno = ext4fs_get_new_inode_no();
483 if (inodeno == -1) {
484 printf("no inode left to assign\n");
485 goto fail;
486 }
487 dir->inode = inodeno;
488 if (sizeof_void_space)
489 dir->direntlen = sizeof_void_space;
490 else
491 dir->direntlen = fs->blksz - totalbytes;
492
493 dir->namelen = strlen(filename);
494 dir->filetype = FILETYPE_REG; /* regular file */
495 temp_dir = (char *)dir;
496 temp_dir = temp_dir + sizeof(struct ext2_dirent);
497 memcpy(temp_dir, filename, strlen(filename));
498
499 *p_ino = inodeno;
500
501 /* update or write the 1st block of root inode */
502 if (ext4fs_put_metadata(root_first_block_buffer,
503 first_block_no_of_root))
504 goto fail;
505
506fail:
507 free(zero_buffer);
508 free(root_first_block_buffer);
509}
510
511static int search_dir(struct ext2_inode *parent_inode, char *dirname)
512{
513 int status;
514 int inodeno;
515 int totalbytes;
516 int templength;
517 int direct_blk_idx;
518 long int blknr;
519 int found = 0;
520 char *ptr = NULL;
521 unsigned char *block_buffer = NULL;
522 struct ext2_dirent *dir = NULL;
523 struct ext2_dirent *previous_dir = NULL;
524 struct ext_filesystem *fs = get_fs();
525
526 /* read the block no allocated to a file */
527 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
528 direct_blk_idx++) {
529 blknr = read_allocated_block(parent_inode, direct_blk_idx);
530 if (blknr == 0)
531 goto fail;
532
533 /* read the blocks of parenet inode */
534 block_buffer = zalloc(fs->blksz);
535 if (!block_buffer)
536 goto fail;
537
Frederic Leroy04735e92013-06-26 18:11:25 +0200538 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +0530539 0, fs->blksz, (char *)block_buffer);
540 if (status == 0)
541 goto fail;
542
543 dir = (struct ext2_dirent *)block_buffer;
544 ptr = (char *)dir;
545 totalbytes = 0;
546 while (dir->direntlen >= 0) {
547 /*
548 * blocksize-totalbytes because last directory
549 * length i.e.,*dir->direntlen is free availble
550 * space in the block that means
551 * it is a last entry of directory entry
552 */
553 if (strlen(dirname) == dir->namelen) {
554 if (strncmp(dirname, ptr +
555 sizeof(struct ext2_dirent),
556 dir->namelen) == 0) {
557 previous_dir->direntlen +=
558 dir->direntlen;
559 inodeno = dir->inode;
560 dir->inode = 0;
561 found = 1;
562 break;
563 }
564 }
565
566 if (fs->blksz - totalbytes == dir->direntlen)
567 break;
568
569 /* traversing the each directory entry */
570 templength = dir->direntlen;
571 totalbytes = totalbytes + templength;
572 previous_dir = dir;
573 dir = (struct ext2_dirent *)((char *)dir + templength);
574 ptr = (char *)dir;
575 }
576
577 if (found == 1) {
578 free(block_buffer);
579 block_buffer = NULL;
580 return inodeno;
581 }
582
583 free(block_buffer);
584 block_buffer = NULL;
585 }
586
587fail:
588 free(block_buffer);
589
590 return -1;
591}
592
593static int find_dir_depth(char *dirname)
594{
595 char *token = strtok(dirname, "/");
596 int count = 0;
597 while (token != NULL) {
598 token = strtok(NULL, "/");
599 count++;
600 }
601 return count + 1 + 1;
602 /*
603 * for example for string /home/temp
604 * depth=home(1)+temp(1)+1 extra for NULL;
605 * so count is 4;
606 */
607}
608
609static int parse_path(char **arr, char *dirname)
610{
611 char *token = strtok(dirname, "/");
612 int i = 0;
613
614 /* add root */
615 arr[i] = zalloc(strlen("/") + 1);
616 if (!arr[i])
617 return -ENOMEM;
Stephen Warren934b14f2015-09-04 22:03:44 -0600618 memcpy(arr[i++], "/", strlen("/"));
Uma Shankared34f342012-05-25 21:22:49 +0530619
620 /* add each path entry after root */
621 while (token != NULL) {
622 arr[i] = zalloc(strlen(token) + 1);
623 if (!arr[i])
624 return -ENOMEM;
625 memcpy(arr[i++], token, strlen(token));
626 token = strtok(NULL, "/");
627 }
628 arr[i] = NULL;
629
630 return 0;
631}
632
633int ext4fs_iget(int inode_no, struct ext2_inode *inode)
634{
635 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
636 return -1;
637
638 return 0;
639}
640
641/*
642 * Function: ext4fs_get_parent_inode_num
643 * Return Value: inode Number of the parent directory of file/Directory to be
644 * created
645 * dirname : Input parmater, input path name of the file/directory to be created
646 * dname : Output parameter, to be filled with the name of the directory
647 * extracted from dirname
648 */
649int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
650{
651 int i;
652 int depth = 0;
653 int matched_inode_no;
654 int result_inode_no = -1;
655 char **ptr = NULL;
656 char *depth_dirname = NULL;
657 char *parse_dirname = NULL;
658 struct ext2_inode *parent_inode = NULL;
659 struct ext2_inode *first_inode = NULL;
660 struct ext2_inode temp_inode;
661
662 if (*dirname != '/') {
663 printf("Please supply Absolute path\n");
664 return -1;
665 }
666
667 /* TODO: input validation make equivalent to linux */
668 depth_dirname = zalloc(strlen(dirname) + 1);
669 if (!depth_dirname)
670 return -ENOMEM;
671
672 memcpy(depth_dirname, dirname, strlen(dirname));
673 depth = find_dir_depth(depth_dirname);
674 parse_dirname = zalloc(strlen(dirname) + 1);
675 if (!parse_dirname)
676 goto fail;
677 memcpy(parse_dirname, dirname, strlen(dirname));
678
679 /* allocate memory for each directory level */
680 ptr = zalloc((depth) * sizeof(char *));
681 if (!ptr)
682 goto fail;
683 if (parse_path(ptr, parse_dirname))
684 goto fail;
685 parent_inode = zalloc(sizeof(struct ext2_inode));
686 if (!parent_inode)
687 goto fail;
688 first_inode = zalloc(sizeof(struct ext2_inode));
689 if (!first_inode)
690 goto fail;
691 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
692 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
693 if (flags & F_FILE)
694 result_inode_no = EXT2_ROOT_INO;
695 for (i = 1; i < depth; i++) {
696 matched_inode_no = search_dir(parent_inode, ptr[i]);
697 if (matched_inode_no == -1) {
698 if (ptr[i + 1] == NULL && i == 1) {
699 result_inode_no = EXT2_ROOT_INO;
700 goto end;
701 } else {
702 if (ptr[i + 1] == NULL)
703 break;
704 printf("Invalid path\n");
705 result_inode_no = -1;
706 goto fail;
707 }
708 } else {
709 if (ptr[i + 1] != NULL) {
710 memset(parent_inode, '\0',
711 sizeof(struct ext2_inode));
712 if (ext4fs_iget(matched_inode_no,
713 parent_inode)) {
714 result_inode_no = -1;
715 goto fail;
716 }
717 result_inode_no = matched_inode_no;
718 } else {
719 break;
720 }
721 }
722 }
723
724end:
725 if (i == 1)
726 matched_inode_no = search_dir(first_inode, ptr[i]);
727 else
728 matched_inode_no = search_dir(parent_inode, ptr[i]);
729
730 if (matched_inode_no != -1) {
731 ext4fs_iget(matched_inode_no, &temp_inode);
732 if (temp_inode.mode & S_IFDIR) {
733 printf("It is a Directory\n");
734 result_inode_no = -1;
735 goto fail;
736 }
737 }
738
739 if (strlen(ptr[i]) > 256) {
740 result_inode_no = -1;
741 goto fail;
742 }
743 memcpy(dname, ptr[i], strlen(ptr[i]));
744
745fail:
746 free(depth_dirname);
747 free(parse_dirname);
Stephen Warren934b14f2015-09-04 22:03:44 -0600748 for (i = 0; i < depth; i++) {
749 if (!ptr[i])
750 break;
751 free(ptr[i]);
752 }
Uma Shankared34f342012-05-25 21:22:49 +0530753 free(ptr);
754 free(parent_inode);
755 free(first_inode);
756
757 return result_inode_no;
758}
759
760static int check_filename(char *filename, unsigned int blknr)
761{
762 unsigned int first_block_no_of_root;
763 int totalbytes = 0;
764 int templength = 0;
765 int status, inodeno;
766 int found = 0;
767 char *root_first_block_buffer = NULL;
768 char *root_first_block_addr = NULL;
769 struct ext2_dirent *dir = NULL;
770 struct ext2_dirent *previous_dir = NULL;
771 char *ptr = NULL;
772 struct ext_filesystem *fs = get_fs();
Stephen Warrend56b2012015-09-04 22:03:45 -0600773 int ret = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530774
775 /* get the first block of root */
776 first_block_no_of_root = blknr;
777 root_first_block_buffer = zalloc(fs->blksz);
778 if (!root_first_block_buffer)
779 return -ENOMEM;
780 root_first_block_addr = root_first_block_buffer;
Frederic Leroy04735e92013-06-26 18:11:25 +0200781 status = ext4fs_devread((lbaint_t)first_block_no_of_root *
Uma Shankared34f342012-05-25 21:22:49 +0530782 fs->sect_perblk, 0,
783 fs->blksz, root_first_block_buffer);
784 if (status == 0)
785 goto fail;
786
787 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
788 goto fail;
789 dir = (struct ext2_dirent *)root_first_block_buffer;
790 ptr = (char *)dir;
791 totalbytes = 0;
792 while (dir->direntlen >= 0) {
793 /*
794 * blocksize-totalbytes because last
795 * directory length i.e., *dir->direntlen
796 * is free availble space in the block that
797 * means it is a last entry of directory entry
798 */
799 if (strlen(filename) == dir->namelen) {
800 if (strncmp(filename, ptr + sizeof(struct ext2_dirent),
801 dir->namelen) == 0) {
802 printf("file found deleting\n");
803 previous_dir->direntlen += dir->direntlen;
804 inodeno = dir->inode;
805 dir->inode = 0;
806 found = 1;
807 break;
808 }
809 }
810
811 if (fs->blksz - totalbytes == dir->direntlen)
812 break;
813
814 /* traversing the each directory entry */
815 templength = dir->direntlen;
816 totalbytes = totalbytes + templength;
817 previous_dir = dir;
818 dir = (struct ext2_dirent *)((char *)dir + templength);
819 ptr = (char *)dir;
820 }
821
822
823 if (found == 1) {
824 if (ext4fs_put_metadata(root_first_block_addr,
825 first_block_no_of_root))
826 goto fail;
Stephen Warrend56b2012015-09-04 22:03:45 -0600827 ret = inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530828 }
829fail:
830 free(root_first_block_buffer);
831
Stephen Warrend56b2012015-09-04 22:03:45 -0600832 return ret;
Uma Shankared34f342012-05-25 21:22:49 +0530833}
834
835int ext4fs_filename_check(char *filename)
836{
837 short direct_blk_idx = 0;
838 long int blknr = -1;
839 int inodeno = -1;
840
841 /* read the block no allocated to a file */
842 for (direct_blk_idx = 0; direct_blk_idx < INDIRECT_BLOCKS;
843 direct_blk_idx++) {
844 blknr = read_allocated_block(g_parent_inode, direct_blk_idx);
845 if (blknr == 0)
846 break;
847 inodeno = check_filename(filename, blknr);
848 if (inodeno != -1)
849 return inodeno;
850 }
851
852 return -1;
853}
854
855long int ext4fs_get_new_blk_no(void)
856{
857 short i;
858 short status;
859 int remainder;
860 unsigned int bg_idx;
861 static int prev_bg_bitmap_index = -1;
862 unsigned int blk_per_grp = ext4fs_root->sblock.blocks_per_group;
863 struct ext_filesystem *fs = get_fs();
864 char *journal_buffer = zalloc(fs->blksz);
865 char *zero_buffer = zalloc(fs->blksz);
866 if (!journal_buffer || !zero_buffer)
867 goto fail;
Simon Glass73c15c62012-10-03 11:37:49 +0000868 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
Uma Shankared34f342012-05-25 21:22:49 +0530869
870 if (fs->first_pass_bbmap == 0) {
871 for (i = 0; i < fs->no_blkgrp; i++) {
Simon Glass73c15c62012-10-03 11:37:49 +0000872 if (bgd[i].free_blocks) {
873 if (bgd[i].bg_flags & EXT4_BG_BLOCK_UNINIT) {
Ma Haijun05508702014-01-08 08:15:33 +0800874 put_ext4(((uint64_t) ((uint64_t)bgd[i].block_id *
875 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +0530876 zero_buffer, fs->blksz);
Simon Glass73c15c62012-10-03 11:37:49 +0000877 bgd[i].bg_flags =
878 bgd[i].
Uma Shankared34f342012-05-25 21:22:49 +0530879 bg_flags & ~EXT4_BG_BLOCK_UNINIT;
880 memcpy(fs->blk_bmaps[i], zero_buffer,
881 fs->blksz);
882 }
883 fs->curr_blkno =
884 _get_new_blk_no(fs->blk_bmaps[i]);
885 if (fs->curr_blkno == -1)
886 /* if block bitmap is completely fill */
887 continue;
888 fs->curr_blkno = fs->curr_blkno +
889 (i * fs->blksz * 8);
890 fs->first_pass_bbmap++;
Simon Glass73c15c62012-10-03 11:37:49 +0000891 bgd[i].free_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +0530892 fs->sb->free_blocks--;
Frederic Leroy04735e92013-06-26 18:11:25 +0200893 status = ext4fs_devread((lbaint_t)
894 bgd[i].block_id *
Uma Shankared34f342012-05-25 21:22:49 +0530895 fs->sect_perblk, 0,
896 fs->blksz,
897 journal_buffer);
898 if (status == 0)
899 goto fail;
900 if (ext4fs_log_journal(journal_buffer,
Simon Glass73c15c62012-10-03 11:37:49 +0000901 bgd[i].block_id))
Uma Shankared34f342012-05-25 21:22:49 +0530902 goto fail;
903 goto success;
904 } else {
905 debug("no space left on block group %d\n", i);
906 }
907 }
908
909 goto fail;
910 } else {
911restart:
912 fs->curr_blkno++;
913 /* get the blockbitmap index respective to blockno */
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200914 bg_idx = fs->curr_blkno / blk_per_grp;
915 if (fs->blksz == 1024) {
Uma Shankared34f342012-05-25 21:22:49 +0530916 remainder = fs->curr_blkno % blk_per_grp;
917 if (!remainder)
918 bg_idx--;
919 }
920
921 /*
922 * To skip completely filled block group bitmaps
923 * Optimize the block allocation
924 */
925 if (bg_idx >= fs->no_blkgrp)
926 goto fail;
927
Simon Glass73c15c62012-10-03 11:37:49 +0000928 if (bgd[bg_idx].free_blocks == 0) {
Uma Shankared34f342012-05-25 21:22:49 +0530929 debug("block group %u is full. Skipping\n", bg_idx);
930 fs->curr_blkno = fs->curr_blkno + blk_per_grp;
931 fs->curr_blkno--;
932 goto restart;
933 }
934
Simon Glass73c15c62012-10-03 11:37:49 +0000935 if (bgd[bg_idx].bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +0530936 memset(zero_buffer, '\0', fs->blksz);
Ma Haijun05508702014-01-08 08:15:33 +0800937 put_ext4(((uint64_t) ((uint64_t)bgd[bg_idx].block_id *
938 (uint64_t)fs->blksz)), zero_buffer, fs->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530939 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Simon Glass73c15c62012-10-03 11:37:49 +0000940 bgd[bg_idx].bg_flags = bgd[bg_idx].bg_flags &
Uma Shankared34f342012-05-25 21:22:49 +0530941 ~EXT4_BG_BLOCK_UNINIT;
942 }
943
944 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
945 bg_idx) != 0) {
946 debug("going for restart for the block no %ld %u\n",
947 fs->curr_blkno, bg_idx);
948 goto restart;
949 }
950
951 /* journal backup */
952 if (prev_bg_bitmap_index != bg_idx) {
953 memset(journal_buffer, '\0', fs->blksz);
Frederic Leroy04735e92013-06-26 18:11:25 +0200954 status = ext4fs_devread((lbaint_t)bgd[bg_idx].block_id
Uma Shankared34f342012-05-25 21:22:49 +0530955 * fs->sect_perblk,
956 0, fs->blksz, journal_buffer);
957 if (status == 0)
958 goto fail;
959 if (ext4fs_log_journal(journal_buffer,
Simon Glass73c15c62012-10-03 11:37:49 +0000960 bgd[bg_idx].block_id))
Uma Shankared34f342012-05-25 21:22:49 +0530961 goto fail;
962
963 prev_bg_bitmap_index = bg_idx;
964 }
Simon Glass73c15c62012-10-03 11:37:49 +0000965 bgd[bg_idx].free_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +0530966 fs->sb->free_blocks--;
967 goto success;
968 }
969success:
970 free(journal_buffer);
971 free(zero_buffer);
972
973 return fs->curr_blkno;
974fail:
975 free(journal_buffer);
976 free(zero_buffer);
977
978 return -1;
979}
980
981int ext4fs_get_new_inode_no(void)
982{
983 short i;
984 short status;
985 unsigned int ibmap_idx;
986 static int prev_inode_bitmap_index = -1;
987 unsigned int inodes_per_grp = ext4fs_root->sblock.inodes_per_group;
988 struct ext_filesystem *fs = get_fs();
989 char *journal_buffer = zalloc(fs->blksz);
990 char *zero_buffer = zalloc(fs->blksz);
991 if (!journal_buffer || !zero_buffer)
992 goto fail;
Simon Glass73c15c62012-10-03 11:37:49 +0000993 struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
Uma Shankared34f342012-05-25 21:22:49 +0530994
995 if (fs->first_pass_ibmap == 0) {
996 for (i = 0; i < fs->no_blkgrp; i++) {
Simon Glass73c15c62012-10-03 11:37:49 +0000997 if (bgd[i].free_inodes) {
998 if (bgd[i].bg_itable_unused !=
999 bgd[i].free_inodes)
1000 bgd[i].bg_itable_unused =
1001 bgd[i].free_inodes;
1002 if (bgd[i].bg_flags & EXT4_BG_INODE_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +05301003 put_ext4(((uint64_t)
Ma Haijun05508702014-01-08 08:15:33 +08001004 ((uint64_t)bgd[i].inode_id *
1005 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301006 zero_buffer, fs->blksz);
Simon Glass73c15c62012-10-03 11:37:49 +00001007 bgd[i].bg_flags = bgd[i].bg_flags &
Uma Shankared34f342012-05-25 21:22:49 +05301008 ~EXT4_BG_INODE_UNINIT;
1009 memcpy(fs->inode_bmaps[i],
1010 zero_buffer, fs->blksz);
1011 }
1012 fs->curr_inode_no =
1013 _get_new_inode_no(fs->inode_bmaps[i]);
1014 if (fs->curr_inode_no == -1)
1015 /* if block bitmap is completely fill */
1016 continue;
1017 fs->curr_inode_no = fs->curr_inode_no +
1018 (i * inodes_per_grp);
1019 fs->first_pass_ibmap++;
Simon Glass73c15c62012-10-03 11:37:49 +00001020 bgd[i].free_inodes--;
1021 bgd[i].bg_itable_unused--;
Uma Shankared34f342012-05-25 21:22:49 +05301022 fs->sb->free_inodes--;
Frederic Leroy04735e92013-06-26 18:11:25 +02001023 status = ext4fs_devread((lbaint_t)
1024 bgd[i].inode_id *
Uma Shankared34f342012-05-25 21:22:49 +05301025 fs->sect_perblk, 0,
1026 fs->blksz,
1027 journal_buffer);
1028 if (status == 0)
1029 goto fail;
1030 if (ext4fs_log_journal(journal_buffer,
Simon Glass73c15c62012-10-03 11:37:49 +00001031 bgd[i].inode_id))
Uma Shankared34f342012-05-25 21:22:49 +05301032 goto fail;
1033 goto success;
1034 } else
1035 debug("no inode left on block group %d\n", i);
1036 }
1037 goto fail;
1038 } else {
1039restart:
1040 fs->curr_inode_no++;
1041 /* get the blockbitmap index respective to blockno */
1042 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Simon Glass73c15c62012-10-03 11:37:49 +00001043 if (bgd[ibmap_idx].bg_flags & EXT4_BG_INODE_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +05301044 memset(zero_buffer, '\0', fs->blksz);
Ma Haijun05508702014-01-08 08:15:33 +08001045 put_ext4(((uint64_t) ((uint64_t)bgd[ibmap_idx].inode_id *
1046 (uint64_t)fs->blksz)), zero_buffer,
Uma Shankared34f342012-05-25 21:22:49 +05301047 fs->blksz);
Simon Glass73c15c62012-10-03 11:37:49 +00001048 bgd[ibmap_idx].bg_flags =
1049 bgd[ibmap_idx].bg_flags & ~EXT4_BG_INODE_UNINIT;
Uma Shankared34f342012-05-25 21:22:49 +05301050 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1051 fs->blksz);
1052 }
1053
1054 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1055 fs->inode_bmaps[ibmap_idx],
1056 ibmap_idx) != 0) {
1057 debug("going for restart for the block no %d %u\n",
1058 fs->curr_inode_no, ibmap_idx);
1059 goto restart;
1060 }
1061
1062 /* journal backup */
1063 if (prev_inode_bitmap_index != ibmap_idx) {
1064 memset(journal_buffer, '\0', fs->blksz);
Frederic Leroy04735e92013-06-26 18:11:25 +02001065 status = ext4fs_devread((lbaint_t)
1066 bgd[ibmap_idx].inode_id
Uma Shankared34f342012-05-25 21:22:49 +05301067 * fs->sect_perblk,
1068 0, fs->blksz, journal_buffer);
1069 if (status == 0)
1070 goto fail;
1071 if (ext4fs_log_journal(journal_buffer,
Simon Glass73c15c62012-10-03 11:37:49 +00001072 bgd[ibmap_idx].inode_id))
Uma Shankared34f342012-05-25 21:22:49 +05301073 goto fail;
1074 prev_inode_bitmap_index = ibmap_idx;
1075 }
Simon Glass73c15c62012-10-03 11:37:49 +00001076 if (bgd[ibmap_idx].bg_itable_unused !=
1077 bgd[ibmap_idx].free_inodes)
1078 bgd[ibmap_idx].bg_itable_unused =
1079 bgd[ibmap_idx].free_inodes;
1080 bgd[ibmap_idx].free_inodes--;
1081 bgd[ibmap_idx].bg_itable_unused--;
Uma Shankared34f342012-05-25 21:22:49 +05301082 fs->sb->free_inodes--;
1083 goto success;
1084 }
1085
1086success:
1087 free(journal_buffer);
1088 free(zero_buffer);
1089
1090 return fs->curr_inode_no;
1091fail:
1092 free(journal_buffer);
1093 free(zero_buffer);
1094
1095 return -1;
1096
1097}
1098
1099
1100static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1101 unsigned int *total_remaining_blocks,
1102 unsigned int *no_blks_reqd)
1103{
1104 short i;
1105 short status;
1106 long int actual_block_no;
1107 long int si_blockno;
1108 /* si :single indirect */
1109 unsigned int *si_buffer = NULL;
1110 unsigned int *si_start_addr = NULL;
1111 struct ext_filesystem *fs = get_fs();
1112
1113 if (*total_remaining_blocks != 0) {
1114 si_buffer = zalloc(fs->blksz);
1115 if (!si_buffer) {
1116 printf("No Memory\n");
1117 return;
1118 }
1119 si_start_addr = si_buffer;
1120 si_blockno = ext4fs_get_new_blk_no();
1121 if (si_blockno == -1) {
1122 printf("no block left to assign\n");
1123 goto fail;
1124 }
1125 (*no_blks_reqd)++;
1126 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1127
Frederic Leroy04735e92013-06-26 18:11:25 +02001128 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301129 0, fs->blksz, (char *)si_buffer);
1130 memset(si_buffer, '\0', fs->blksz);
1131 if (status == 0)
1132 goto fail;
1133
1134 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1135 actual_block_no = ext4fs_get_new_blk_no();
1136 if (actual_block_no == -1) {
1137 printf("no block left to assign\n");
1138 goto fail;
1139 }
1140 *si_buffer = actual_block_no;
1141 debug("SIAB %u: %u\n", *si_buffer,
1142 *total_remaining_blocks);
1143
1144 si_buffer++;
1145 (*total_remaining_blocks)--;
1146 if (*total_remaining_blocks == 0)
1147 break;
1148 }
1149
1150 /* write the block to disk */
Ma Haijun05508702014-01-08 08:15:33 +08001151 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301152 si_start_addr, fs->blksz);
1153 file_inode->b.blocks.indir_block = si_blockno;
1154 }
1155fail:
1156 free(si_start_addr);
1157}
1158
1159static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1160 unsigned int *total_remaining_blocks,
1161 unsigned int *no_blks_reqd)
1162{
1163 short i;
1164 short j;
1165 short status;
1166 long int actual_block_no;
1167 /* di:double indirect */
1168 long int di_blockno_parent;
1169 long int di_blockno_child;
1170 unsigned int *di_parent_buffer = NULL;
1171 unsigned int *di_child_buff = NULL;
1172 unsigned int *di_block_start_addr = NULL;
1173 unsigned int *di_child_buff_start = NULL;
1174 struct ext_filesystem *fs = get_fs();
1175
1176 if (*total_remaining_blocks != 0) {
1177 /* double indirect parent block connecting to inode */
1178 di_blockno_parent = ext4fs_get_new_blk_no();
1179 if (di_blockno_parent == -1) {
1180 printf("no block left to assign\n");
1181 goto fail;
1182 }
1183 di_parent_buffer = zalloc(fs->blksz);
1184 if (!di_parent_buffer)
1185 goto fail;
1186
1187 di_block_start_addr = di_parent_buffer;
1188 (*no_blks_reqd)++;
1189 debug("DIPB %ld: %u\n", di_blockno_parent,
1190 *total_remaining_blocks);
1191
Frederic Leroy04735e92013-06-26 18:11:25 +02001192 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankared34f342012-05-25 21:22:49 +05301193 fs->sect_perblk, 0,
1194 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001195
1196 if (!status) {
1197 printf("%s: Device read error!\n", __func__);
1198 goto fail;
1199 }
Uma Shankared34f342012-05-25 21:22:49 +05301200 memset(di_parent_buffer, '\0', fs->blksz);
1201
1202 /*
1203 * start:for each double indirect parent
1204 * block create one more block
1205 */
1206 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1207 di_blockno_child = ext4fs_get_new_blk_no();
1208 if (di_blockno_child == -1) {
1209 printf("no block left to assign\n");
1210 goto fail;
1211 }
1212 di_child_buff = zalloc(fs->blksz);
1213 if (!di_child_buff)
1214 goto fail;
1215
1216 di_child_buff_start = di_child_buff;
1217 *di_parent_buffer = di_blockno_child;
1218 di_parent_buffer++;
1219 (*no_blks_reqd)++;
1220 debug("DICB %ld: %u\n", di_blockno_child,
1221 *total_remaining_blocks);
1222
Frederic Leroy04735e92013-06-26 18:11:25 +02001223 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankared34f342012-05-25 21:22:49 +05301224 fs->sect_perblk, 0,
1225 fs->blksz,
1226 (char *)di_child_buff);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001227
1228 if (!status) {
1229 printf("%s: Device read error!\n", __func__);
1230 goto fail;
1231 }
Uma Shankared34f342012-05-25 21:22:49 +05301232 memset(di_child_buff, '\0', fs->blksz);
1233 /* filling of actual datablocks for each child */
1234 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1235 actual_block_no = ext4fs_get_new_blk_no();
1236 if (actual_block_no == -1) {
1237 printf("no block left to assign\n");
1238 goto fail;
1239 }
1240 *di_child_buff = actual_block_no;
1241 debug("DIAB %ld: %u\n", actual_block_no,
1242 *total_remaining_blocks);
1243
1244 di_child_buff++;
1245 (*total_remaining_blocks)--;
1246 if (*total_remaining_blocks == 0)
1247 break;
1248 }
1249 /* write the block table */
Ma Haijun05508702014-01-08 08:15:33 +08001250 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301251 di_child_buff_start, fs->blksz);
1252 free(di_child_buff_start);
1253 di_child_buff_start = NULL;
1254
1255 if (*total_remaining_blocks == 0)
1256 break;
1257 }
Ma Haijun05508702014-01-08 08:15:33 +08001258 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301259 di_block_start_addr, fs->blksz);
1260 file_inode->b.blocks.double_indir_block = di_blockno_parent;
1261 }
1262fail:
1263 free(di_block_start_addr);
1264}
1265
1266static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1267 unsigned int *total_remaining_blocks,
1268 unsigned int *no_blks_reqd)
1269{
1270 short i;
1271 short j;
1272 short k;
1273 long int actual_block_no;
1274 /* ti: Triple Indirect */
1275 long int ti_gp_blockno;
1276 long int ti_parent_blockno;
1277 long int ti_child_blockno;
1278 unsigned int *ti_gp_buff = NULL;
1279 unsigned int *ti_parent_buff = NULL;
1280 unsigned int *ti_child_buff = NULL;
1281 unsigned int *ti_gp_buff_start_addr = NULL;
1282 unsigned int *ti_pbuff_start_addr = NULL;
1283 unsigned int *ti_cbuff_start_addr = NULL;
1284 struct ext_filesystem *fs = get_fs();
1285 if (*total_remaining_blocks != 0) {
1286 /* triple indirect grand parent block connecting to inode */
1287 ti_gp_blockno = ext4fs_get_new_blk_no();
1288 if (ti_gp_blockno == -1) {
1289 printf("no block left to assign\n");
1290 goto fail;
1291 }
1292 ti_gp_buff = zalloc(fs->blksz);
1293 if (!ti_gp_buff)
1294 goto fail;
1295
1296 ti_gp_buff_start_addr = ti_gp_buff;
1297 (*no_blks_reqd)++;
1298 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1299 *total_remaining_blocks);
1300
1301 /* for each 4 byte grand parent entry create one more block */
1302 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1303 ti_parent_blockno = ext4fs_get_new_blk_no();
1304 if (ti_parent_blockno == -1) {
1305 printf("no block left to assign\n");
1306 goto fail;
1307 }
1308 ti_parent_buff = zalloc(fs->blksz);
1309 if (!ti_parent_buff)
1310 goto fail;
1311
1312 ti_pbuff_start_addr = ti_parent_buff;
1313 *ti_gp_buff = ti_parent_blockno;
1314 ti_gp_buff++;
1315 (*no_blks_reqd)++;
1316 debug("TIPB %ld: %u\n", ti_parent_blockno,
1317 *total_remaining_blocks);
1318
1319 /* for each 4 byte entry parent create one more block */
1320 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1321 ti_child_blockno = ext4fs_get_new_blk_no();
1322 if (ti_child_blockno == -1) {
1323 printf("no block left assign\n");
1324 goto fail;
1325 }
1326 ti_child_buff = zalloc(fs->blksz);
1327 if (!ti_child_buff)
1328 goto fail;
1329
1330 ti_cbuff_start_addr = ti_child_buff;
1331 *ti_parent_buff = ti_child_blockno;
1332 ti_parent_buff++;
1333 (*no_blks_reqd)++;
1334 debug("TICB %ld: %u\n", ti_parent_blockno,
1335 *total_remaining_blocks);
1336
1337 /* fill actual datablocks for each child */
1338 for (k = 0; k < (fs->blksz / sizeof(int));
1339 k++) {
1340 actual_block_no =
1341 ext4fs_get_new_blk_no();
1342 if (actual_block_no == -1) {
1343 printf("no block left\n");
1344 goto fail;
1345 }
1346 *ti_child_buff = actual_block_no;
1347 debug("TIAB %ld: %u\n", actual_block_no,
1348 *total_remaining_blocks);
1349
1350 ti_child_buff++;
1351 (*total_remaining_blocks)--;
1352 if (*total_remaining_blocks == 0)
1353 break;
1354 }
1355 /* write the child block */
Ma Haijun05508702014-01-08 08:15:33 +08001356 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1357 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301358 ti_cbuff_start_addr, fs->blksz);
1359 free(ti_cbuff_start_addr);
1360
1361 if (*total_remaining_blocks == 0)
1362 break;
1363 }
1364 /* write the parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001365 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301366 ti_pbuff_start_addr, fs->blksz);
1367 free(ti_pbuff_start_addr);
1368
1369 if (*total_remaining_blocks == 0)
1370 break;
1371 }
1372 /* write the grand parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001373 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301374 ti_gp_buff_start_addr, fs->blksz);
1375 file_inode->b.blocks.triple_indir_block = ti_gp_blockno;
1376 }
1377fail:
1378 free(ti_gp_buff_start_addr);
1379}
1380
1381void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1382 unsigned int total_remaining_blocks,
1383 unsigned int *total_no_of_block)
1384{
1385 short i;
1386 long int direct_blockno;
1387 unsigned int no_blks_reqd = 0;
1388
1389 /* allocation of direct blocks */
Stephen Warrend0180282014-06-11 12:46:16 -06001390 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankared34f342012-05-25 21:22:49 +05301391 direct_blockno = ext4fs_get_new_blk_no();
1392 if (direct_blockno == -1) {
1393 printf("no block left to assign\n");
1394 return;
1395 }
1396 file_inode->b.blocks.dir_blocks[i] = direct_blockno;
1397 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1398
1399 total_remaining_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +05301400 }
1401
1402 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1403 &no_blks_reqd);
1404 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1405 &no_blks_reqd);
1406 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1407 &no_blks_reqd);
1408 *total_no_of_block += no_blks_reqd;
1409}
1410
1411#endif
1412
Uma Shankara1596432012-05-25 21:21:44 +05301413static struct ext4_extent_header *ext4fs_get_extent_block
1414 (struct ext2_data *data, char *buf,
1415 struct ext4_extent_header *ext_block,
1416 uint32_t fileblock, int log2_blksz)
1417{
1418 struct ext4_extent_idx *index;
1419 unsigned long long block;
Ionut Nicu47017322014-01-13 11:59:24 +01001420 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankara1596432012-05-25 21:21:44 +05301421 int i;
1422
1423 while (1) {
1424 index = (struct ext4_extent_idx *)(ext_block + 1);
1425
Rommel Custodio8b415f72013-07-21 10:53:25 +02001426 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Uma Shankara1596432012-05-25 21:21:44 +05301427 return 0;
1428
1429 if (ext_block->eh_depth == 0)
1430 return ext_block;
1431 i = -1;
1432 do {
1433 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001434 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301435 break;
Ionut Nicub5bbac12014-01-13 12:00:08 +01001436 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankara1596432012-05-25 21:21:44 +05301437
1438 if (--i < 0)
1439 return 0;
1440
Rommel Custodio8b415f72013-07-21 10:53:25 +02001441 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301442 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1443
Ionut Nicu47017322014-01-13 11:59:24 +01001444 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
Frederic Leroy04735e92013-06-26 18:11:25 +02001445 buf))
Uma Shankara1596432012-05-25 21:21:44 +05301446 ext_block = (struct ext4_extent_header *)buf;
1447 else
1448 return 0;
1449 }
1450}
1451
1452static int ext4fs_blockgroup
1453 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1454{
1455 long int blkno;
1456 unsigned int blkoff, desc_per_blk;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001457 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301458
1459 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
1460
1461 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
1462 group / desc_per_blk;
1463 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
1464
1465 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1466 group, blkno, blkoff);
1467
Frederic Leroy04735e92013-06-26 18:11:25 +02001468 return ext4fs_devread((lbaint_t)blkno <<
1469 (LOG2_BLOCK_SIZE(data) - log2blksz),
Uma Shankara1596432012-05-25 21:21:44 +05301470 blkoff, sizeof(struct ext2_block_group),
1471 (char *)blkgrp);
1472}
1473
1474int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1475{
1476 struct ext2_block_group blkgrp;
1477 struct ext2_sblock *sblock = &data->sblock;
1478 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00001479 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301480 int inodes_per_block, status;
1481 long int blkno;
1482 unsigned int blkoff;
1483
1484 /* It is easier to calculate if the first inode is 0. */
1485 ino--;
1486 status = ext4fs_blockgroup(data, ino / __le32_to_cpu
1487 (sblock->inodes_per_group), &blkgrp);
1488 if (status == 0)
1489 return 0;
1490
1491 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
1492 blkno = __le32_to_cpu(blkgrp.inode_table_id) +
1493 (ino % __le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
1494 blkoff = (ino % inodes_per_block) * fs->inodesz;
1495 /* Read the inode. */
Frederic Leroy04735e92013-06-26 18:11:25 +02001496 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1497 log2blksz), blkoff,
Uma Shankara1596432012-05-25 21:21:44 +05301498 sizeof(struct ext2_inode), (char *)inode);
1499 if (status == 0)
1500 return 0;
1501
1502 return 1;
1503}
1504
1505long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1506{
1507 long int blknr;
1508 int blksz;
1509 int log2_blksz;
1510 int status;
1511 long int rblock;
1512 long int perblock_parent;
1513 long int perblock_child;
1514 unsigned long long start;
1515 /* get the blocksize of the filesystem */
1516 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +00001517 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1518 - get_fs()->dev_desc->log2blksz;
1519
Uma Shankara1596432012-05-25 21:21:44 +05301520 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1521 char *buf = zalloc(blksz);
1522 if (!buf)
1523 return -ENOMEM;
1524 struct ext4_extent_header *ext_block;
1525 struct ext4_extent *extent;
1526 int i = -1;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001527 ext_block =
1528 ext4fs_get_extent_block(ext4fs_root, buf,
1529 (struct ext4_extent_header *)
1530 inode->b.blocks.dir_blocks,
1531 fileblock, log2_blksz);
Uma Shankara1596432012-05-25 21:21:44 +05301532 if (!ext_block) {
1533 printf("invalid extent block\n");
1534 free(buf);
1535 return -EINVAL;
1536 }
1537
1538 extent = (struct ext4_extent *)(ext_block + 1);
1539
1540 do {
1541 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001542 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301543 break;
1544 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1545 if (--i >= 0) {
1546 fileblock -= le32_to_cpu(extent[i].ee_block);
Rommel Custodio8b415f72013-07-21 10:53:25 +02001547 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
Uma Shankara1596432012-05-25 21:21:44 +05301548 free(buf);
1549 return 0;
1550 }
1551
Rommel Custodio8b415f72013-07-21 10:53:25 +02001552 start = le16_to_cpu(extent[i].ee_start_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301553 start = (start << 32) +
1554 le32_to_cpu(extent[i].ee_start_lo);
1555 free(buf);
1556 return fileblock + start;
1557 }
1558
1559 printf("Extent Error\n");
1560 free(buf);
1561 return -1;
1562 }
1563
1564 /* Direct blocks. */
1565 if (fileblock < INDIRECT_BLOCKS)
1566 blknr = __le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
1567
1568 /* Indirect. */
1569 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1570 if (ext4fs_indir1_block == NULL) {
1571 ext4fs_indir1_block = zalloc(blksz);
1572 if (ext4fs_indir1_block == NULL) {
1573 printf("** SI ext2fs read block (indir 1)"
1574 "malloc failed. **\n");
1575 return -1;
1576 }
1577 ext4fs_indir1_size = blksz;
1578 ext4fs_indir1_blkno = -1;
1579 }
1580 if (blksz != ext4fs_indir1_size) {
1581 free(ext4fs_indir1_block);
1582 ext4fs_indir1_block = NULL;
1583 ext4fs_indir1_size = 0;
1584 ext4fs_indir1_blkno = -1;
1585 ext4fs_indir1_block = zalloc(blksz);
1586 if (ext4fs_indir1_block == NULL) {
1587 printf("** SI ext2fs read block (indir 1):"
1588 "malloc failed. **\n");
1589 return -1;
1590 }
1591 ext4fs_indir1_size = blksz;
1592 }
1593 if ((__le32_to_cpu(inode->b.blocks.indir_block) <<
1594 log2_blksz) != ext4fs_indir1_blkno) {
1595 status =
Frederic Leroy04735e92013-06-26 18:11:25 +02001596 ext4fs_devread((lbaint_t)__le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301597 (inode->b.blocks.
1598 indir_block) << log2_blksz, 0,
1599 blksz, (char *)ext4fs_indir1_block);
1600 if (status == 0) {
1601 printf("** SI ext2fs read block (indir 1)"
1602 "failed. **\n");
1603 return 0;
1604 }
1605 ext4fs_indir1_blkno =
1606 __le32_to_cpu(inode->b.blocks.
1607 indir_block) << log2_blksz;
1608 }
1609 blknr = __le32_to_cpu(ext4fs_indir1_block
1610 [fileblock - INDIRECT_BLOCKS]);
1611 }
1612 /* Double indirect. */
1613 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1614 (blksz / 4 + 1)))) {
1615
1616 long int perblock = blksz / 4;
1617 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1618
1619 if (ext4fs_indir1_block == NULL) {
1620 ext4fs_indir1_block = zalloc(blksz);
1621 if (ext4fs_indir1_block == NULL) {
1622 printf("** DI ext2fs read block (indir 2 1)"
1623 "malloc failed. **\n");
1624 return -1;
1625 }
1626 ext4fs_indir1_size = blksz;
1627 ext4fs_indir1_blkno = -1;
1628 }
1629 if (blksz != ext4fs_indir1_size) {
1630 free(ext4fs_indir1_block);
1631 ext4fs_indir1_block = NULL;
1632 ext4fs_indir1_size = 0;
1633 ext4fs_indir1_blkno = -1;
1634 ext4fs_indir1_block = zalloc(blksz);
1635 if (ext4fs_indir1_block == NULL) {
1636 printf("** DI ext2fs read block (indir 2 1)"
1637 "malloc failed. **\n");
1638 return -1;
1639 }
1640 ext4fs_indir1_size = blksz;
1641 }
1642 if ((__le32_to_cpu(inode->b.blocks.double_indir_block) <<
1643 log2_blksz) != ext4fs_indir1_blkno) {
1644 status =
Frederic Leroy04735e92013-06-26 18:11:25 +02001645 ext4fs_devread((lbaint_t)__le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301646 (inode->b.blocks.
1647 double_indir_block) << log2_blksz,
1648 0, blksz,
1649 (char *)ext4fs_indir1_block);
1650 if (status == 0) {
1651 printf("** DI ext2fs read block (indir 2 1)"
1652 "failed. **\n");
1653 return -1;
1654 }
1655 ext4fs_indir1_blkno =
1656 __le32_to_cpu(inode->b.blocks.double_indir_block) <<
1657 log2_blksz;
1658 }
1659
1660 if (ext4fs_indir2_block == NULL) {
1661 ext4fs_indir2_block = zalloc(blksz);
1662 if (ext4fs_indir2_block == NULL) {
1663 printf("** DI ext2fs read block (indir 2 2)"
1664 "malloc failed. **\n");
1665 return -1;
1666 }
1667 ext4fs_indir2_size = blksz;
1668 ext4fs_indir2_blkno = -1;
1669 }
1670 if (blksz != ext4fs_indir2_size) {
1671 free(ext4fs_indir2_block);
1672 ext4fs_indir2_block = NULL;
1673 ext4fs_indir2_size = 0;
1674 ext4fs_indir2_blkno = -1;
1675 ext4fs_indir2_block = zalloc(blksz);
1676 if (ext4fs_indir2_block == NULL) {
1677 printf("** DI ext2fs read block (indir 2 2)"
1678 "malloc failed. **\n");
1679 return -1;
1680 }
1681 ext4fs_indir2_size = blksz;
1682 }
1683 if ((__le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
1684 log2_blksz) != ext4fs_indir2_blkno) {
Frederic Leroy04735e92013-06-26 18:11:25 +02001685 status = ext4fs_devread((lbaint_t)__le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301686 (ext4fs_indir1_block
1687 [rblock /
1688 perblock]) << log2_blksz, 0,
1689 blksz,
1690 (char *)ext4fs_indir2_block);
1691 if (status == 0) {
1692 printf("** DI ext2fs read block (indir 2 2)"
1693 "failed. **\n");
1694 return -1;
1695 }
1696 ext4fs_indir2_blkno =
1697 __le32_to_cpu(ext4fs_indir1_block[rblock
1698 /
1699 perblock]) <<
1700 log2_blksz;
1701 }
1702 blknr = __le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
1703 }
1704 /* Tripple indirect. */
1705 else {
1706 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1707 (blksz / 4 * blksz / 4));
1708 perblock_child = blksz / 4;
1709 perblock_parent = ((blksz / 4) * (blksz / 4));
1710
1711 if (ext4fs_indir1_block == NULL) {
1712 ext4fs_indir1_block = zalloc(blksz);
1713 if (ext4fs_indir1_block == NULL) {
1714 printf("** TI ext2fs read block (indir 2 1)"
1715 "malloc failed. **\n");
1716 return -1;
1717 }
1718 ext4fs_indir1_size = blksz;
1719 ext4fs_indir1_blkno = -1;
1720 }
1721 if (blksz != ext4fs_indir1_size) {
1722 free(ext4fs_indir1_block);
1723 ext4fs_indir1_block = NULL;
1724 ext4fs_indir1_size = 0;
1725 ext4fs_indir1_blkno = -1;
1726 ext4fs_indir1_block = zalloc(blksz);
1727 if (ext4fs_indir1_block == NULL) {
1728 printf("** TI ext2fs read block (indir 2 1)"
1729 "malloc failed. **\n");
1730 return -1;
1731 }
1732 ext4fs_indir1_size = blksz;
1733 }
1734 if ((__le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1735 log2_blksz) != ext4fs_indir1_blkno) {
1736 status = ext4fs_devread
Frederic Leroy04735e92013-06-26 18:11:25 +02001737 ((lbaint_t)
1738 __le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankara1596432012-05-25 21:21:44 +05301739 << log2_blksz, 0, blksz,
1740 (char *)ext4fs_indir1_block);
1741 if (status == 0) {
1742 printf("** TI ext2fs read block (indir 2 1)"
1743 "failed. **\n");
1744 return -1;
1745 }
1746 ext4fs_indir1_blkno =
1747 __le32_to_cpu(inode->b.blocks.triple_indir_block) <<
1748 log2_blksz;
1749 }
1750
1751 if (ext4fs_indir2_block == NULL) {
1752 ext4fs_indir2_block = zalloc(blksz);
1753 if (ext4fs_indir2_block == NULL) {
1754 printf("** TI ext2fs read block (indir 2 2)"
1755 "malloc failed. **\n");
1756 return -1;
1757 }
1758 ext4fs_indir2_size = blksz;
1759 ext4fs_indir2_blkno = -1;
1760 }
1761 if (blksz != ext4fs_indir2_size) {
1762 free(ext4fs_indir2_block);
1763 ext4fs_indir2_block = NULL;
1764 ext4fs_indir2_size = 0;
1765 ext4fs_indir2_blkno = -1;
1766 ext4fs_indir2_block = zalloc(blksz);
1767 if (ext4fs_indir2_block == NULL) {
1768 printf("** TI ext2fs read block (indir 2 2)"
1769 "malloc failed. **\n");
1770 return -1;
1771 }
1772 ext4fs_indir2_size = blksz;
1773 }
1774 if ((__le32_to_cpu(ext4fs_indir1_block[rblock /
1775 perblock_parent]) <<
1776 log2_blksz)
1777 != ext4fs_indir2_blkno) {
Frederic Leroy04735e92013-06-26 18:11:25 +02001778 status = ext4fs_devread((lbaint_t)__le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301779 (ext4fs_indir1_block
1780 [rblock /
1781 perblock_parent]) <<
1782 log2_blksz, 0, blksz,
1783 (char *)ext4fs_indir2_block);
1784 if (status == 0) {
1785 printf("** TI ext2fs read block (indir 2 2)"
1786 "failed. **\n");
1787 return -1;
1788 }
1789 ext4fs_indir2_blkno =
1790 __le32_to_cpu(ext4fs_indir1_block[rblock /
1791 perblock_parent])
1792 << log2_blksz;
1793 }
1794
1795 if (ext4fs_indir3_block == NULL) {
1796 ext4fs_indir3_block = zalloc(blksz);
1797 if (ext4fs_indir3_block == NULL) {
1798 printf("** TI ext2fs read block (indir 2 2)"
1799 "malloc failed. **\n");
1800 return -1;
1801 }
1802 ext4fs_indir3_size = blksz;
1803 ext4fs_indir3_blkno = -1;
1804 }
1805 if (blksz != ext4fs_indir3_size) {
1806 free(ext4fs_indir3_block);
1807 ext4fs_indir3_block = NULL;
1808 ext4fs_indir3_size = 0;
1809 ext4fs_indir3_blkno = -1;
1810 ext4fs_indir3_block = zalloc(blksz);
1811 if (ext4fs_indir3_block == NULL) {
1812 printf("** TI ext2fs read block (indir 2 2)"
1813 "malloc failed. **\n");
1814 return -1;
1815 }
1816 ext4fs_indir3_size = blksz;
1817 }
1818 if ((__le32_to_cpu(ext4fs_indir2_block[rblock
1819 /
1820 perblock_child]) <<
1821 log2_blksz) != ext4fs_indir3_blkno) {
1822 status =
Frederic Leroy04735e92013-06-26 18:11:25 +02001823 ext4fs_devread((lbaint_t)__le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301824 (ext4fs_indir2_block
1825 [(rblock / perblock_child)
1826 % (blksz / 4)]) << log2_blksz, 0,
1827 blksz, (char *)ext4fs_indir3_block);
1828 if (status == 0) {
1829 printf("** TI ext2fs read block (indir 2 2)"
1830 "failed. **\n");
1831 return -1;
1832 }
1833 ext4fs_indir3_blkno =
1834 __le32_to_cpu(ext4fs_indir2_block[(rblock /
1835 perblock_child) %
1836 (blksz /
1837 4)]) <<
1838 log2_blksz;
1839 }
1840
1841 blknr = __le32_to_cpu(ext4fs_indir3_block
1842 [rblock % perblock_child]);
1843 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001844 debug("read_allocated_block %ld\n", blknr);
Uma Shankara1596432012-05-25 21:21:44 +05301845
1846 return blknr;
1847}
1848
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001849/**
1850 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1851 * global pointers
1852 *
1853 * This function assures that for a file with the same name but different size
1854 * the sequential store on the ext4 filesystem will be correct.
1855 *
1856 * In this function the global data, responsible for internal representation
1857 * of the ext4 data are initialized to the reset state. Without this, during
1858 * replacement of the smaller file with the bigger truncation of new file was
1859 * performed.
1860 */
1861void ext4fs_reinit_global(void)
Uma Shankara1596432012-05-25 21:21:44 +05301862{
Uma Shankara1596432012-05-25 21:21:44 +05301863 if (ext4fs_indir1_block != NULL) {
1864 free(ext4fs_indir1_block);
1865 ext4fs_indir1_block = NULL;
1866 ext4fs_indir1_size = 0;
1867 ext4fs_indir1_blkno = -1;
1868 }
1869 if (ext4fs_indir2_block != NULL) {
1870 free(ext4fs_indir2_block);
1871 ext4fs_indir2_block = NULL;
1872 ext4fs_indir2_size = 0;
1873 ext4fs_indir2_blkno = -1;
1874 }
1875 if (ext4fs_indir3_block != NULL) {
1876 free(ext4fs_indir3_block);
1877 ext4fs_indir3_block = NULL;
1878 ext4fs_indir3_size = 0;
1879 ext4fs_indir3_blkno = -1;
1880 }
1881}
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001882void ext4fs_close(void)
1883{
1884 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1885 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1886 ext4fs_file = NULL;
1887 }
1888 if (ext4fs_root != NULL) {
1889 free(ext4fs_root);
1890 ext4fs_root = NULL;
1891 }
1892
1893 ext4fs_reinit_global();
1894}
Uma Shankara1596432012-05-25 21:21:44 +05301895
1896int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1897 struct ext2fs_node **fnode, int *ftype)
1898{
1899 unsigned int fpos = 0;
1900 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001901 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05301902 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1903
1904#ifdef DEBUG
1905 if (name != NULL)
1906 printf("Iterate dir %s\n", name);
1907#endif /* of DEBUG */
1908 if (!diro->inode_read) {
1909 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1910 if (status == 0)
1911 return 0;
1912 }
1913 /* Search the file. */
1914 while (fpos < __le32_to_cpu(diro->inode.size)) {
1915 struct ext2_dirent dirent;
1916
1917 status = ext4fs_read_file(diro, fpos,
1918 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001919 (char *)&dirent, &actread);
1920 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05301921 return 0;
1922
1923 if (dirent.namelen != 0) {
1924 char filename[dirent.namelen + 1];
1925 struct ext2fs_node *fdiro;
1926 int type = FILETYPE_UNKNOWN;
1927
1928 status = ext4fs_read_file(diro,
1929 fpos +
1930 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001931 dirent.namelen, filename,
1932 &actread);
1933 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05301934 return 0;
1935
1936 fdiro = zalloc(sizeof(struct ext2fs_node));
1937 if (!fdiro)
1938 return 0;
1939
1940 fdiro->data = diro->data;
1941 fdiro->ino = __le32_to_cpu(dirent.inode);
1942
1943 filename[dirent.namelen] = '\0';
1944
1945 if (dirent.filetype != FILETYPE_UNKNOWN) {
1946 fdiro->inode_read = 0;
1947
1948 if (dirent.filetype == FILETYPE_DIRECTORY)
1949 type = FILETYPE_DIRECTORY;
1950 else if (dirent.filetype == FILETYPE_SYMLINK)
1951 type = FILETYPE_SYMLINK;
1952 else if (dirent.filetype == FILETYPE_REG)
1953 type = FILETYPE_REG;
1954 } else {
1955 status = ext4fs_read_inode(diro->data,
1956 __le32_to_cpu
1957 (dirent.inode),
1958 &fdiro->inode);
1959 if (status == 0) {
1960 free(fdiro);
1961 return 0;
1962 }
1963 fdiro->inode_read = 1;
1964
1965 if ((__le16_to_cpu(fdiro->inode.mode) &
1966 FILETYPE_INO_MASK) ==
1967 FILETYPE_INO_DIRECTORY) {
1968 type = FILETYPE_DIRECTORY;
1969 } else if ((__le16_to_cpu(fdiro->inode.mode)
1970 & FILETYPE_INO_MASK) ==
1971 FILETYPE_INO_SYMLINK) {
1972 type = FILETYPE_SYMLINK;
1973 } else if ((__le16_to_cpu(fdiro->inode.mode)
1974 & FILETYPE_INO_MASK) ==
1975 FILETYPE_INO_REG) {
1976 type = FILETYPE_REG;
1977 }
1978 }
1979#ifdef DEBUG
1980 printf("iterate >%s<\n", filename);
1981#endif /* of DEBUG */
1982 if ((name != NULL) && (fnode != NULL)
1983 && (ftype != NULL)) {
1984 if (strcmp(filename, name) == 0) {
1985 *ftype = type;
1986 *fnode = fdiro;
1987 return 1;
1988 }
1989 } else {
1990 if (fdiro->inode_read == 0) {
1991 status = ext4fs_read_inode(diro->data,
1992 __le32_to_cpu(
1993 dirent.inode),
1994 &fdiro->inode);
1995 if (status == 0) {
1996 free(fdiro);
1997 return 0;
1998 }
1999 fdiro->inode_read = 1;
2000 }
2001 switch (type) {
2002 case FILETYPE_DIRECTORY:
2003 printf("<DIR> ");
2004 break;
2005 case FILETYPE_SYMLINK:
2006 printf("<SYM> ");
2007 break;
2008 case FILETYPE_REG:
2009 printf(" ");
2010 break;
2011 default:
2012 printf("< ? > ");
2013 break;
2014 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002015 printf("%10u %s\n",
2016 __le32_to_cpu(fdiro->inode.size),
Uma Shankara1596432012-05-25 21:21:44 +05302017 filename);
2018 }
2019 free(fdiro);
2020 }
2021 fpos += __le16_to_cpu(dirent.direntlen);
2022 }
2023 return 0;
2024}
2025
2026static char *ext4fs_read_symlink(struct ext2fs_node *node)
2027{
2028 char *symlink;
2029 struct ext2fs_node *diro = node;
2030 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002031 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302032
2033 if (!diro->inode_read) {
2034 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2035 if (status == 0)
2036 return 0;
2037 }
2038 symlink = zalloc(__le32_to_cpu(diro->inode.size) + 1);
2039 if (!symlink)
2040 return 0;
2041
2042 if (__le32_to_cpu(diro->inode.size) <= 60) {
2043 strncpy(symlink, diro->inode.b.symlink,
2044 __le32_to_cpu(diro->inode.size));
2045 } else {
2046 status = ext4fs_read_file(diro, 0,
2047 __le32_to_cpu(diro->inode.size),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002048 symlink, &actread);
Gary Bisson9d2f6a92015-09-07 11:20:07 +02002049 if ((status < 0) || (actread == 0)) {
Uma Shankara1596432012-05-25 21:21:44 +05302050 free(symlink);
2051 return 0;
2052 }
2053 }
2054 symlink[__le32_to_cpu(diro->inode.size)] = '\0';
2055 return symlink;
2056}
2057
2058static int ext4fs_find_file1(const char *currpath,
2059 struct ext2fs_node *currroot,
2060 struct ext2fs_node **currfound, int *foundtype)
2061{
2062 char fpath[strlen(currpath) + 1];
2063 char *name = fpath;
2064 char *next;
2065 int status;
2066 int type = FILETYPE_DIRECTORY;
2067 struct ext2fs_node *currnode = currroot;
2068 struct ext2fs_node *oldnode = currroot;
2069
2070 strncpy(fpath, currpath, strlen(currpath) + 1);
2071
2072 /* Remove all leading slashes. */
2073 while (*name == '/')
2074 name++;
2075
2076 if (!*name) {
2077 *currfound = currnode;
2078 return 1;
2079 }
2080
2081 for (;;) {
2082 int found;
2083
2084 /* Extract the actual part from the pathname. */
2085 next = strchr(name, '/');
2086 if (next) {
2087 /* Remove all leading slashes. */
2088 while (*next == '/')
2089 *(next++) = '\0';
2090 }
2091
2092 if (type != FILETYPE_DIRECTORY) {
2093 ext4fs_free_node(currnode, currroot);
2094 return 0;
2095 }
2096
2097 oldnode = currnode;
2098
2099 /* Iterate over the directory. */
2100 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2101 if (found == 0)
2102 return 0;
2103
2104 if (found == -1)
2105 break;
2106
2107 /* Read in the symlink and follow it. */
2108 if (type == FILETYPE_SYMLINK) {
2109 char *symlink;
2110
2111 /* Test if the symlink does not loop. */
2112 if (++symlinknest == 8) {
2113 ext4fs_free_node(currnode, currroot);
2114 ext4fs_free_node(oldnode, currroot);
2115 return 0;
2116 }
2117
2118 symlink = ext4fs_read_symlink(currnode);
2119 ext4fs_free_node(currnode, currroot);
2120
2121 if (!symlink) {
2122 ext4fs_free_node(oldnode, currroot);
2123 return 0;
2124 }
2125
2126 debug("Got symlink >%s<\n", symlink);
2127
2128 if (symlink[0] == '/') {
2129 ext4fs_free_node(oldnode, currroot);
2130 oldnode = &ext4fs_root->diropen;
2131 }
2132
2133 /* Lookup the node the symlink points to. */
2134 status = ext4fs_find_file1(symlink, oldnode,
2135 &currnode, &type);
2136
2137 free(symlink);
2138
2139 if (status == 0) {
2140 ext4fs_free_node(oldnode, currroot);
2141 return 0;
2142 }
2143 }
2144
2145 ext4fs_free_node(oldnode, currroot);
2146
2147 /* Found the node! */
2148 if (!next || *next == '\0') {
2149 *currfound = currnode;
2150 *foundtype = type;
2151 return 1;
2152 }
2153 name = next;
2154 }
2155 return -1;
2156}
2157
2158int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2159 struct ext2fs_node **foundnode, int expecttype)
2160{
2161 int status;
2162 int foundtype = FILETYPE_DIRECTORY;
2163
2164 symlinknest = 0;
2165 if (!path)
2166 return 0;
2167
2168 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2169 if (status == 0)
2170 return 0;
2171
2172 /* Check if the node that was found was of the expected type. */
2173 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2174 return 0;
2175 else if ((expecttype == FILETYPE_DIRECTORY)
2176 && (foundtype != expecttype))
2177 return 0;
2178
2179 return 1;
2180}
2181
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002182int ext4fs_open(const char *filename, loff_t *len)
Uma Shankara1596432012-05-25 21:21:44 +05302183{
2184 struct ext2fs_node *fdiro = NULL;
2185 int status;
Uma Shankara1596432012-05-25 21:21:44 +05302186
2187 if (ext4fs_root == NULL)
2188 return -1;
2189
2190 ext4fs_file = NULL;
2191 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2192 FILETYPE_REG);
2193 if (status == 0)
2194 goto fail;
2195
2196 if (!fdiro->inode_read) {
2197 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2198 &fdiro->inode);
2199 if (status == 0)
2200 goto fail;
2201 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002202 *len = __le32_to_cpu(fdiro->inode.size);
Uma Shankara1596432012-05-25 21:21:44 +05302203 ext4fs_file = fdiro;
2204
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002205 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05302206fail:
2207 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2208
2209 return -1;
2210}
2211
2212int ext4fs_mount(unsigned part_length)
2213{
2214 struct ext2_data *data;
2215 int status;
2216 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00002217 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankara1596432012-05-25 21:21:44 +05302218 if (!data)
2219 return 0;
2220
2221 /* Read the superblock. */
Egbert Eich50ce4c02013-05-01 01:13:19 +00002222 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankara1596432012-05-25 21:21:44 +05302223
2224 if (status == 0)
2225 goto fail;
2226
2227 /* Make sure this is an ext2 filesystem. */
2228 if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
2229 goto fail;
2230
2231 if (__le32_to_cpu(data->sblock.revision_level == 0))
2232 fs->inodesz = 128;
2233 else
2234 fs->inodesz = __le16_to_cpu(data->sblock.inode_size);
2235
2236 debug("EXT2 rev %d, inode_size %d\n",
2237 __le32_to_cpu(data->sblock.revision_level), fs->inodesz);
2238
2239 data->diropen.data = data;
2240 data->diropen.ino = 2;
2241 data->diropen.inode_read = 1;
2242 data->inode = &data->diropen.inode;
2243
2244 status = ext4fs_read_inode(data, 2, data->inode);
2245 if (status == 0)
2246 goto fail;
2247
2248 ext4fs_root = data;
2249
2250 return 1;
2251fail:
2252 printf("Failed to mount ext2 filesystem...\n");
2253 free(data);
2254 ext4fs_root = NULL;
2255
2256 return 0;
2257}