blob: 90e7602e2aa9711ed7072b56a2c677f7bd6d72ab [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{
63 sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) - 1);
64}
65
66static inline void ext4fs_bg_free_inodes_dec(struct ext2_block_group *bg)
67{
68 bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) - 1);
69}
70
71static inline void ext4fs_bg_free_blocks_dec(struct ext2_block_group *bg)
72{
73 bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) - 1);
74}
75
76static inline void ext4fs_bg_itable_unused_dec(struct ext2_block_group *bg)
77{
78 bg->bg_itable_unused = cpu_to_le16(le16_to_cpu(bg->bg_itable_unused) - 1);
79}
80
Stefan Brüns9f5dd8b2016-09-20 01:12:42 +020081uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
82{
83 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
84 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
85 return free_blocks;
86}
87
88void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
89{
90 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
91 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
92}
93
94uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
95 const struct ext_filesystem *fs)
96{
97 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
98 if (fs->gdsize == 64)
99 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
100 return free_blocks;
101}
102
103static inline
104uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
105 const struct ext_filesystem *fs)
106{
107 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
108 if (fs->gdsize == 64)
109 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
110 return free_inodes;
111}
112
113static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
114{
115 return le16_to_cpu(bg->bg_flags);
116}
117
118static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
119 uint16_t flags)
120{
121 bg->bg_flags = cpu_to_le16(flags);
122}
123
124/* Block number of the block bitmap */
125uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
126 const struct ext_filesystem *fs)
127{
128 uint64_t block_nr = le32_to_cpu(bg->block_id);
129 if (fs->gdsize == 64)
130 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
131 return block_nr;
132}
133
134/* Block number of the inode bitmap */
135uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
136 const struct ext_filesystem *fs)
137{
138 uint64_t block_nr = le32_to_cpu(bg->inode_id);
139 if (fs->gdsize == 64)
140 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
141 return block_nr;
142}
143#endif
144
145/* Block number of the inode table */
146uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
147 const struct ext_filesystem *fs)
148{
149 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
150 if (fs->gdsize == 64)
151 block_nr +=
152 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
153 return block_nr;
154}
155
156#if defined(CONFIG_EXT4_WRITE)
Uma Shankared34f342012-05-25 21:22:49 +0530157uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
158{
159 uint32_t res = size / n;
160 if (res * n != size)
161 res++;
162
163 return res;
164}
165
166void put_ext4(uint64_t off, void *buf, uint32_t size)
167{
168 uint64_t startblock;
169 uint64_t remainder;
170 unsigned char *temp_ptr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530171 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +0000172 int log2blksz = fs->dev_desc->log2blksz;
173 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankared34f342012-05-25 21:22:49 +0530174
Egbert Eich50ce4c02013-05-01 01:13:19 +0000175 startblock = off >> log2blksz;
Uma Shankared34f342012-05-25 21:22:49 +0530176 startblock += part_offset;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000177 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankared34f342012-05-25 21:22:49 +0530178
179 if (fs->dev_desc == NULL)
180 return;
181
Egbert Eich50ce4c02013-05-01 01:13:19 +0000182 if ((startblock + (size >> log2blksz)) >
Uma Shankared34f342012-05-25 21:22:49 +0530183 (part_offset + fs->total_sect)) {
Frederic Leroy04735e92013-06-26 18:11:25 +0200184 printf("part_offset is " LBAFU "\n", part_offset);
Simon Glassaac618a2014-10-15 04:38:32 -0600185 printf("total_sector is %" PRIu64 "\n", fs->total_sect);
Uma Shankared34f342012-05-25 21:22:49 +0530186 printf("error: overflow occurs\n");
187 return;
188 }
189
190 if (remainder) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700191 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
192 temp_ptr = sec_buf;
193 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
194 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530195 } else {
Egbert Eich50ce4c02013-05-01 01:13:19 +0000196 if (size >> log2blksz != 0) {
Simon Glass2a981dc2016-02-29 15:25:52 -0700197 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
198 (unsigned long *)buf);
Uma Shankared34f342012-05-25 21:22:49 +0530199 } else {
Simon Glass2a981dc2016-02-29 15:25:52 -0700200 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530201 temp_ptr = sec_buf;
202 memcpy(temp_ptr, buf, size);
Simon Glass2a981dc2016-02-29 15:25:52 -0700203 blk_dwrite(fs->dev_desc, startblock, 1,
204 (unsigned long *)sec_buf);
Uma Shankared34f342012-05-25 21:22:49 +0530205 }
206 }
207}
208
209static int _get_new_inode_no(unsigned char *buffer)
210{
211 struct ext_filesystem *fs = get_fs();
212 unsigned char input;
213 int operand, status;
214 int count = 1;
215 int j = 0;
216
217 /* get the blocksize of the filesystem */
218 unsigned char *ptr = buffer;
219 while (*ptr == 255) {
220 ptr++;
221 count += 8;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200222 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankared34f342012-05-25 21:22:49 +0530223 return -1;
224 }
225
226 for (j = 0; j < fs->blksz; j++) {
227 input = *ptr;
228 int i = 0;
229 while (i <= 7) {
230 operand = 1 << i;
231 status = input & operand;
232 if (status) {
233 i++;
234 count++;
235 } else {
236 *ptr |= operand;
237 return count;
238 }
239 }
240 ptr = ptr + 1;
241 }
242
243 return -1;
244}
245
246static int _get_new_blk_no(unsigned char *buffer)
247{
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200248 int operand;
Uma Shankared34f342012-05-25 21:22:49 +0530249 int count = 0;
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200250 int i;
Uma Shankared34f342012-05-25 21:22:49 +0530251 unsigned char *ptr = buffer;
252 struct ext_filesystem *fs = get_fs();
253
Uma Shankared34f342012-05-25 21:22:49 +0530254 while (*ptr == 255) {
255 ptr++;
256 count += 8;
257 if (count == (fs->blksz * 8))
258 return -1;
259 }
260
Stefan Brüns0ceef3d2016-09-06 04:36:50 +0200261 if (fs->blksz == 1024)
262 count += 1;
263
264 for (i = 0; i <= 7; i++) {
265 operand = 1 << i;
266 if (*ptr & operand) {
267 count++;
268 } else {
269 *ptr |= operand;
270 return count;
Uma Shankared34f342012-05-25 21:22:49 +0530271 }
Uma Shankared34f342012-05-25 21:22:49 +0530272 }
273
274 return -1;
275}
276
277int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
278{
279 int i, remainder, status;
280 unsigned char *ptr = buffer;
281 unsigned char operand;
282 i = blockno / 8;
283 remainder = blockno % 8;
284 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
285
286 i = i - (index * blocksize);
287 if (blocksize != 1024) {
288 ptr = ptr + i;
289 operand = 1 << remainder;
290 status = *ptr & operand;
291 if (status)
292 return -1;
293
294 *ptr = *ptr | operand;
295 return 0;
296 } else {
297 if (remainder == 0) {
298 ptr = ptr + i - 1;
299 operand = (1 << 7);
300 } else {
301 ptr = ptr + i;
302 operand = (1 << (remainder - 1));
303 }
304 status = *ptr & operand;
305 if (status)
306 return -1;
307
308 *ptr = *ptr | operand;
309 return 0;
310 }
311}
312
313void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
314{
315 int i, remainder, status;
316 unsigned char *ptr = buffer;
317 unsigned char operand;
318 i = blockno / 8;
319 remainder = blockno % 8;
320 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
321
322 i = i - (index * blocksize);
323 if (blocksize != 1024) {
324 ptr = ptr + i;
325 operand = (1 << remainder);
326 status = *ptr & operand;
327 if (status)
328 *ptr = *ptr & ~(operand);
329 } else {
330 if (remainder == 0) {
331 ptr = ptr + i - 1;
332 operand = (1 << 7);
333 } else {
334 ptr = ptr + i;
335 operand = (1 << (remainder - 1));
336 }
337 status = *ptr & operand;
338 if (status)
339 *ptr = *ptr & ~(operand);
340 }
341}
342
343int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
344{
345 int i, remainder, status;
346 unsigned char *ptr = buffer;
347 unsigned char operand;
348
Michael Walle58a9ecb2016-09-01 11:21:40 +0200349 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530350 i = inode_no / 8;
351 remainder = inode_no % 8;
352 if (remainder == 0) {
353 ptr = ptr + i - 1;
354 operand = (1 << 7);
355 } else {
356 ptr = ptr + i;
357 operand = (1 << (remainder - 1));
358 }
359 status = *ptr & operand;
360 if (status)
361 return -1;
362
363 *ptr = *ptr | operand;
364
365 return 0;
366}
367
368void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
369{
370 int i, remainder, status;
371 unsigned char *ptr = buffer;
372 unsigned char operand;
373
Michael Walle58a9ecb2016-09-01 11:21:40 +0200374 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankared34f342012-05-25 21:22:49 +0530375 i = inode_no / 8;
376 remainder = inode_no % 8;
377 if (remainder == 0) {
378 ptr = ptr + i - 1;
379 operand = (1 << 7);
380 } else {
381 ptr = ptr + i;
382 operand = (1 << (remainder - 1));
383 }
384 status = *ptr & operand;
385 if (status)
386 *ptr = *ptr & ~(operand);
387}
388
Michael Walle58a9ecb2016-09-01 11:21:40 +0200389uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankared34f342012-05-25 21:22:49 +0530390{
391 struct ext2_block_group *desc;
392 struct ext_filesystem *fs = get_fs();
Michael Walle58a9ecb2016-09-01 11:21:40 +0200393 uint16_t crc = 0;
394 __le32 le32_i = cpu_to_le32(i);
Uma Shankared34f342012-05-25 21:22:49 +0530395
Stefan Brüns688d0e72016-09-17 02:10:10 +0200396 desc = ext4fs_get_group_descriptor(fs, i);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200397 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankared34f342012-05-25 21:22:49 +0530398 int offset = offsetof(struct ext2_block_group, bg_checksum);
399
400 crc = ext2fs_crc16(~0, fs->sb->unique_id,
401 sizeof(fs->sb->unique_id));
Michael Walle58a9ecb2016-09-01 11:21:40 +0200402 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
Uma Shankared34f342012-05-25 21:22:49 +0530403 crc = ext2fs_crc16(crc, desc, offset);
404 offset += sizeof(desc->bg_checksum); /* skip checksum */
405 assert(offset == sizeof(*desc));
406 }
407
408 return crc;
409}
410
411static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
412{
413 int dentry_length;
414 int sizeof_void_space;
415 int new_entry_byte_reqd;
416 short padding_factor = 0;
417
418 if (dir->namelen % 4 != 0)
419 padding_factor = 4 - (dir->namelen % 4);
420
421 dentry_length = sizeof(struct ext2_dirent) +
422 dir->namelen + padding_factor;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200423 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankared34f342012-05-25 21:22:49 +0530424 if (sizeof_void_space == 0)
425 return 0;
426
427 padding_factor = 0;
428 if (strlen(filename) % 4 != 0)
429 padding_factor = 4 - (strlen(filename) % 4);
430
431 new_entry_byte_reqd = strlen(filename) +
432 sizeof(struct ext2_dirent) + padding_factor;
433 if (sizeof_void_space >= new_entry_byte_reqd) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200434 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankared34f342012-05-25 21:22:49 +0530435 return sizeof_void_space;
436 }
437
438 return 0;
439}
440
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200441int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankared34f342012-05-25 21:22:49 +0530442{
443 unsigned int *zero_buffer = NULL;
444 char *root_first_block_buffer = NULL;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200445 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530446 long int first_block_no_of_root = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530447 int totalbytes = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530448 unsigned int new_entry_byte_reqd;
Uma Shankared34f342012-05-25 21:22:49 +0530449 int sizeof_void_space = 0;
450 int templength = 0;
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200451 int inodeno = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530452 int status;
453 struct ext_filesystem *fs = get_fs();
454 /* directory entry */
455 struct ext2_dirent *dir;
Uma Shankared34f342012-05-25 21:22:49 +0530456 char *temp_dir = NULL;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200457 uint32_t new_blk_no;
458 uint32_t new_size;
459 uint32_t new_blockcnt;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200460 uint32_t directory_blocks;
Uma Shankared34f342012-05-25 21:22:49 +0530461
462 zero_buffer = zalloc(fs->blksz);
463 if (!zero_buffer) {
464 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200465 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530466 }
467 root_first_block_buffer = zalloc(fs->blksz);
468 if (!root_first_block_buffer) {
469 free(zero_buffer);
470 printf("No Memory\n");
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200471 return -1;
Uma Shankared34f342012-05-25 21:22:49 +0530472 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200473 new_entry_byte_reqd = ROUND(strlen(filename) +
474 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530475restart:
Stefan Brünsa321abd2016-09-06 04:36:44 +0200476 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
477 LOG2_BLOCK_SIZE(ext4fs_root);
478 blk_idx = directory_blocks - 1;
Uma Shankared34f342012-05-25 21:22:49 +0530479
Stefan Brünsa321abd2016-09-06 04:36:44 +0200480restart_read:
Uma Shankared34f342012-05-25 21:22:49 +0530481 /* read the block no allocated to a file */
Stefan Brünsa321abd2016-09-06 04:36:44 +0200482 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
483 if (first_block_no_of_root <= 0)
484 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530485
Frederic Leroy04735e92013-06-26 18:11:25 +0200486 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankared34f342012-05-25 21:22:49 +0530487 * fs->sect_perblk,
488 0, fs->blksz, root_first_block_buffer);
489 if (status == 0)
490 goto fail;
491
492 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
493 goto fail;
494 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankared34f342012-05-25 21:22:49 +0530495 totalbytes = 0;
Stefan Brünsa321abd2016-09-06 04:36:44 +0200496
Michael Walle58a9ecb2016-09-01 11:21:40 +0200497 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsa321abd2016-09-06 04:36:44 +0200498 unsigned short used_len = ROUND(dir->namelen +
499 sizeof(struct ext2_dirent), 4);
Uma Shankared34f342012-05-25 21:22:49 +0530500
Stefan Brünsa321abd2016-09-06 04:36:44 +0200501 /* last entry of block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200502 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankared34f342012-05-25 21:22:49 +0530503
Stefan Brünsa321abd2016-09-06 04:36:44 +0200504 /* check if new entry fits */
505 if ((used_len + new_entry_byte_reqd) <=
506 le16_to_cpu(dir->direntlen)) {
507 dir->direntlen = cpu_to_le16(used_len);
508 break;
509 } else {
510 if (blk_idx > 0) {
511 printf("Block full, trying previous\n");
512 blk_idx--;
513 goto restart_read;
514 }
515 printf("All blocks full: Allocate new\n");
Uma Shankared34f342012-05-25 21:22:49 +0530516
Stefan Brünsb96c3c72016-09-06 04:36:43 +0200517 if (le32_to_cpu(g_parent_inode->flags) &
518 EXT4_EXTENTS_FL) {
519 printf("Directory uses extents\n");
520 goto fail;
521 }
Stefan Brünsa321abd2016-09-06 04:36:44 +0200522 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankared34f342012-05-25 21:22:49 +0530523 printf("Directory exceeds limit\n");
524 goto fail;
525 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200526 new_blk_no = ext4fs_get_new_blk_no();
527 if (new_blk_no == -1) {
Uma Shankared34f342012-05-25 21:22:49 +0530528 printf("no block left to assign\n");
529 goto fail;
530 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200531 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsa321abd2016-09-06 04:36:44 +0200532 g_parent_inode->b.blocks.
533 dir_blocks[directory_blocks] =
Michael Walle58a9ecb2016-09-01 11:21:40 +0200534 cpu_to_le32(new_blk_no);
535
536 new_size = le32_to_cpu(g_parent_inode->size);
537 new_size += fs->blksz;
538 g_parent_inode->size = cpu_to_le32(new_size);
539
540 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
541 new_blockcnt += fs->sect_perblk;
542 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
543
Uma Shankared34f342012-05-25 21:22:49 +0530544 if (ext4fs_put_metadata
545 (root_first_block_buffer,
546 first_block_no_of_root))
547 goto fail;
548 goto restart;
549 }
Uma Shankared34f342012-05-25 21:22:49 +0530550 }
551
Michael Walle58a9ecb2016-09-01 11:21:40 +0200552 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530553 totalbytes = totalbytes + templength;
554 sizeof_void_space = check_void_in_dentry(dir, filename);
555 if (sizeof_void_space)
556 break;
557
558 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530559 }
560
561 /* make a pointer ready for creating next directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200562 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530563 totalbytes = totalbytes + templength;
564 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankared34f342012-05-25 21:22:49 +0530565
566 /* get the next available inode number */
567 inodeno = ext4fs_get_new_inode_no();
568 if (inodeno == -1) {
569 printf("no inode left to assign\n");
570 goto fail;
571 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200572 dir->inode = cpu_to_le32(inodeno);
Uma Shankared34f342012-05-25 21:22:49 +0530573 if (sizeof_void_space)
Michael Walle58a9ecb2016-09-01 11:21:40 +0200574 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankared34f342012-05-25 21:22:49 +0530575 else
Michael Walle58a9ecb2016-09-01 11:21:40 +0200576 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankared34f342012-05-25 21:22:49 +0530577
578 dir->namelen = strlen(filename);
579 dir->filetype = FILETYPE_REG; /* regular file */
580 temp_dir = (char *)dir;
581 temp_dir = temp_dir + sizeof(struct ext2_dirent);
582 memcpy(temp_dir, filename, strlen(filename));
583
Uma Shankared34f342012-05-25 21:22:49 +0530584 /* update or write the 1st block of root inode */
585 if (ext4fs_put_metadata(root_first_block_buffer,
586 first_block_no_of_root))
587 goto fail;
588
589fail:
590 free(zero_buffer);
591 free(root_first_block_buffer);
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200592
593 return inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530594}
595
596static int search_dir(struct ext2_inode *parent_inode, char *dirname)
597{
598 int status;
Stefan Brüns76a29512016-09-06 04:36:41 +0200599 int inodeno = 0;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200600 int offset;
601 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530602 long int blknr;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200603 char *block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530604 struct ext2_dirent *dir = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530605 struct ext_filesystem *fs = get_fs();
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200606 uint32_t directory_blocks;
607 char *direntname;
Uma Shankared34f342012-05-25 21:22:49 +0530608
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200609 directory_blocks = le32_to_cpu(parent_inode->size) >>
610 LOG2_BLOCK_SIZE(ext4fs_root);
611
612 block_buffer = zalloc(fs->blksz);
613 if (!block_buffer)
614 goto fail;
615
616 /* get the block no allocated to a file */
617 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
618 blknr = read_allocated_block(parent_inode, blk_idx);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200619 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530620 goto fail;
621
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200622 /* read the directory block */
Frederic Leroy04735e92013-06-26 18:11:25 +0200623 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +0530624 0, fs->blksz, (char *)block_buffer);
625 if (status == 0)
626 goto fail;
627
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200628 offset = 0;
629 do {
630 dir = (struct ext2_dirent *)(block_buffer + offset);
631 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankared34f342012-05-25 21:22:49 +0530632
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200633 int direntlen = le16_to_cpu(dir->direntlen);
634 if (direntlen < sizeof(struct ext2_dirent))
Uma Shankared34f342012-05-25 21:22:49 +0530635 break;
636
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200637 if (dir->inode && (strlen(dirname) == dir->namelen) &&
638 (strncmp(dirname, direntname, dir->namelen) == 0)) {
639 inodeno = le32_to_cpu(dir->inode);
640 break;
641 }
Uma Shankared34f342012-05-25 21:22:49 +0530642
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200643 offset += direntlen;
Stefan Brüns76a29512016-09-06 04:36:41 +0200644
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200645 } while (offset < fs->blksz);
646
647 if (inodeno > 0) {
648 free(block_buffer);
Stefan Brüns76a29512016-09-06 04:36:41 +0200649 return inodeno;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200650 }
Uma Shankared34f342012-05-25 21:22:49 +0530651 }
652
653fail:
654 free(block_buffer);
655
656 return -1;
657}
658
659static int find_dir_depth(char *dirname)
660{
661 char *token = strtok(dirname, "/");
662 int count = 0;
663 while (token != NULL) {
664 token = strtok(NULL, "/");
665 count++;
666 }
667 return count + 1 + 1;
668 /*
669 * for example for string /home/temp
670 * depth=home(1)+temp(1)+1 extra for NULL;
671 * so count is 4;
672 */
673}
674
675static int parse_path(char **arr, char *dirname)
676{
677 char *token = strtok(dirname, "/");
678 int i = 0;
679
680 /* add root */
681 arr[i] = zalloc(strlen("/") + 1);
682 if (!arr[i])
683 return -ENOMEM;
Stephen Warren934b14f2015-09-04 22:03:44 -0600684 memcpy(arr[i++], "/", strlen("/"));
Uma Shankared34f342012-05-25 21:22:49 +0530685
686 /* add each path entry after root */
687 while (token != NULL) {
688 arr[i] = zalloc(strlen(token) + 1);
689 if (!arr[i])
690 return -ENOMEM;
691 memcpy(arr[i++], token, strlen(token));
692 token = strtok(NULL, "/");
693 }
694 arr[i] = NULL;
695
696 return 0;
697}
698
699int ext4fs_iget(int inode_no, struct ext2_inode *inode)
700{
701 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
702 return -1;
703
704 return 0;
705}
706
707/*
708 * Function: ext4fs_get_parent_inode_num
709 * Return Value: inode Number of the parent directory of file/Directory to be
710 * created
711 * dirname : Input parmater, input path name of the file/directory to be created
712 * dname : Output parameter, to be filled with the name of the directory
713 * extracted from dirname
714 */
715int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
716{
717 int i;
718 int depth = 0;
719 int matched_inode_no;
720 int result_inode_no = -1;
721 char **ptr = NULL;
722 char *depth_dirname = NULL;
723 char *parse_dirname = NULL;
724 struct ext2_inode *parent_inode = NULL;
725 struct ext2_inode *first_inode = NULL;
726 struct ext2_inode temp_inode;
727
728 if (*dirname != '/') {
729 printf("Please supply Absolute path\n");
730 return -1;
731 }
732
733 /* TODO: input validation make equivalent to linux */
734 depth_dirname = zalloc(strlen(dirname) + 1);
735 if (!depth_dirname)
736 return -ENOMEM;
737
738 memcpy(depth_dirname, dirname, strlen(dirname));
739 depth = find_dir_depth(depth_dirname);
740 parse_dirname = zalloc(strlen(dirname) + 1);
741 if (!parse_dirname)
742 goto fail;
743 memcpy(parse_dirname, dirname, strlen(dirname));
744
745 /* allocate memory for each directory level */
746 ptr = zalloc((depth) * sizeof(char *));
747 if (!ptr)
748 goto fail;
749 if (parse_path(ptr, parse_dirname))
750 goto fail;
751 parent_inode = zalloc(sizeof(struct ext2_inode));
752 if (!parent_inode)
753 goto fail;
754 first_inode = zalloc(sizeof(struct ext2_inode));
755 if (!first_inode)
756 goto fail;
757 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
758 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
759 if (flags & F_FILE)
760 result_inode_no = EXT2_ROOT_INO;
761 for (i = 1; i < depth; i++) {
762 matched_inode_no = search_dir(parent_inode, ptr[i]);
763 if (matched_inode_no == -1) {
764 if (ptr[i + 1] == NULL && i == 1) {
765 result_inode_no = EXT2_ROOT_INO;
766 goto end;
767 } else {
768 if (ptr[i + 1] == NULL)
769 break;
770 printf("Invalid path\n");
771 result_inode_no = -1;
772 goto fail;
773 }
774 } else {
775 if (ptr[i + 1] != NULL) {
776 memset(parent_inode, '\0',
777 sizeof(struct ext2_inode));
778 if (ext4fs_iget(matched_inode_no,
779 parent_inode)) {
780 result_inode_no = -1;
781 goto fail;
782 }
783 result_inode_no = matched_inode_no;
784 } else {
785 break;
786 }
787 }
788 }
789
790end:
791 if (i == 1)
792 matched_inode_no = search_dir(first_inode, ptr[i]);
793 else
794 matched_inode_no = search_dir(parent_inode, ptr[i]);
795
796 if (matched_inode_no != -1) {
797 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200798 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankared34f342012-05-25 21:22:49 +0530799 printf("It is a Directory\n");
800 result_inode_no = -1;
801 goto fail;
802 }
803 }
804
805 if (strlen(ptr[i]) > 256) {
806 result_inode_no = -1;
807 goto fail;
808 }
809 memcpy(dname, ptr[i], strlen(ptr[i]));
810
811fail:
812 free(depth_dirname);
813 free(parse_dirname);
Stephen Warren934b14f2015-09-04 22:03:44 -0600814 for (i = 0; i < depth; i++) {
815 if (!ptr[i])
816 break;
817 free(ptr[i]);
818 }
Uma Shankared34f342012-05-25 21:22:49 +0530819 free(ptr);
820 free(parent_inode);
821 free(first_inode);
822
823 return result_inode_no;
824}
825
Stefan Brüns76a29512016-09-06 04:36:41 +0200826static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankared34f342012-05-25 21:22:49 +0530827{
Uma Shankared34f342012-05-25 21:22:49 +0530828 int totalbytes = 0;
829 int templength = 0;
830 int status, inodeno;
831 int found = 0;
832 char *root_first_block_buffer = NULL;
Uma Shankared34f342012-05-25 21:22:49 +0530833 struct ext2_dirent *dir = NULL;
834 struct ext2_dirent *previous_dir = NULL;
835 char *ptr = NULL;
836 struct ext_filesystem *fs = get_fs();
Stephen Warrend56b2012015-09-04 22:03:45 -0600837 int ret = -1;
Uma Shankared34f342012-05-25 21:22:49 +0530838
839 /* get the first block of root */
Uma Shankared34f342012-05-25 21:22:49 +0530840 root_first_block_buffer = zalloc(fs->blksz);
841 if (!root_first_block_buffer)
842 return -ENOMEM;
Stefan Brüns76a29512016-09-06 04:36:41 +0200843 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Uma Shankared34f342012-05-25 21:22:49 +0530844 fs->blksz, root_first_block_buffer);
845 if (status == 0)
846 goto fail;
847
Stefan Brüns76a29512016-09-06 04:36:41 +0200848 if (ext4fs_log_journal(root_first_block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530849 goto fail;
850 dir = (struct ext2_dirent *)root_first_block_buffer;
851 ptr = (char *)dir;
852 totalbytes = 0;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200853 while (le16_to_cpu(dir->direntlen) >= 0) {
Uma Shankared34f342012-05-25 21:22:49 +0530854 /*
855 * blocksize-totalbytes because last
856 * directory length i.e., *dir->direntlen
857 * is free availble space in the block that
858 * means it is a last entry of directory entry
859 */
Stefan Brüns76a29512016-09-06 04:36:41 +0200860 if (dir->inode && (strlen(filename) == dir->namelen) &&
861 (strncmp(ptr + sizeof(struct ext2_dirent),
862 filename, dir->namelen) == 0)) {
863 printf("file found, deleting\n");
864 inodeno = le32_to_cpu(dir->inode);
865 if (previous_dir) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200866 uint16_t new_len;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200867 new_len = le16_to_cpu(previous_dir->direntlen);
868 new_len += le16_to_cpu(dir->direntlen);
869 previous_dir->direntlen = cpu_to_le16(new_len);
Stefan Brüns76a29512016-09-06 04:36:41 +0200870 } else {
Uma Shankared34f342012-05-25 21:22:49 +0530871 dir->inode = 0;
Uma Shankared34f342012-05-25 21:22:49 +0530872 }
Stefan Brüns76a29512016-09-06 04:36:41 +0200873 found = 1;
874 break;
Uma Shankared34f342012-05-25 21:22:49 +0530875 }
876
Michael Walle58a9ecb2016-09-01 11:21:40 +0200877 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
Uma Shankared34f342012-05-25 21:22:49 +0530878 break;
879
880 /* traversing the each directory entry */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200881 templength = le16_to_cpu(dir->direntlen);
Uma Shankared34f342012-05-25 21:22:49 +0530882 totalbytes = totalbytes + templength;
883 previous_dir = dir;
884 dir = (struct ext2_dirent *)((char *)dir + templength);
885 ptr = (char *)dir;
886 }
887
888
889 if (found == 1) {
Stefan Brüns76a29512016-09-06 04:36:41 +0200890 if (ext4fs_put_metadata(root_first_block_buffer, blknr))
Uma Shankared34f342012-05-25 21:22:49 +0530891 goto fail;
Stephen Warrend56b2012015-09-04 22:03:45 -0600892 ret = inodeno;
Uma Shankared34f342012-05-25 21:22:49 +0530893 }
894fail:
895 free(root_first_block_buffer);
896
Stephen Warrend56b2012015-09-04 22:03:45 -0600897 return ret;
Uma Shankared34f342012-05-25 21:22:49 +0530898}
899
Stefan Brüns76a29512016-09-06 04:36:41 +0200900int ext4fs_filename_unlink(char *filename)
Uma Shankared34f342012-05-25 21:22:49 +0530901{
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200902 int blk_idx;
Uma Shankared34f342012-05-25 21:22:49 +0530903 long int blknr = -1;
904 int inodeno = -1;
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200905 uint32_t directory_blocks;
906
907 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
908 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankared34f342012-05-25 21:22:49 +0530909
910 /* read the block no allocated to a file */
Stefan Brünsb7dd40d2016-09-06 04:36:46 +0200911 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
912 blknr = read_allocated_block(g_parent_inode, blk_idx);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200913 if (blknr <= 0)
Uma Shankared34f342012-05-25 21:22:49 +0530914 break;
Stefan Brüns76a29512016-09-06 04:36:41 +0200915 inodeno = unlink_filename(filename, blknr);
Uma Shankared34f342012-05-25 21:22:49 +0530916 if (inodeno != -1)
917 return inodeno;
918 }
919
920 return -1;
921}
922
Michael Walle58a9ecb2016-09-01 11:21:40 +0200923uint32_t ext4fs_get_new_blk_no(void)
Uma Shankared34f342012-05-25 21:22:49 +0530924{
925 short i;
926 short status;
927 int remainder;
928 unsigned int bg_idx;
929 static int prev_bg_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200930 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankared34f342012-05-25 21:22:49 +0530931 struct ext_filesystem *fs = get_fs();
932 char *journal_buffer = zalloc(fs->blksz);
933 char *zero_buffer = zalloc(fs->blksz);
934 if (!journal_buffer || !zero_buffer)
935 goto fail;
Uma Shankared34f342012-05-25 21:22:49 +0530936
937 if (fs->first_pass_bbmap == 0) {
938 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200939 struct ext2_block_group *bgd = NULL;
940 bgd = ext4fs_get_group_descriptor(fs, i);
941 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
942 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
943 uint64_t b_bitmap_blk =
944 ext4fs_bg_get_block_id(bgd, fs);
945 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +0530946 memcpy(fs->blk_bmaps[i], zero_buffer,
947 fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200948 put_ext4(b_bitmap_blk * fs->blksz,
949 fs->blk_bmaps[i], fs->blksz);
950 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
951 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +0530952 }
953 fs->curr_blkno =
954 _get_new_blk_no(fs->blk_bmaps[i]);
955 if (fs->curr_blkno == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +0200956 /* block bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +0530957 continue;
958 fs->curr_blkno = fs->curr_blkno +
959 (i * fs->blksz * 8);
960 fs->first_pass_bbmap++;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200961 ext4fs_bg_free_blocks_dec(bgd);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200962 ext4fs_sb_free_blocks_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200963 status = ext4fs_devread(b_bitmap_blk *
964 fs->sect_perblk,
965 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +0530966 journal_buffer);
967 if (status == 0)
968 goto fail;
969 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +0200970 b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +0530971 goto fail;
972 goto success;
973 } else {
974 debug("no space left on block group %d\n", i);
975 }
976 }
977
978 goto fail;
979 } else {
Uma Shankared34f342012-05-25 21:22:49 +0530980 fs->curr_blkno++;
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +0200981restart:
Uma Shankared34f342012-05-25 21:22:49 +0530982 /* get the blockbitmap index respective to blockno */
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200983 bg_idx = fs->curr_blkno / blk_per_grp;
984 if (fs->blksz == 1024) {
Uma Shankared34f342012-05-25 21:22:49 +0530985 remainder = fs->curr_blkno % blk_per_grp;
986 if (!remainder)
987 bg_idx--;
988 }
989
990 /*
991 * To skip completely filled block group bitmaps
992 * Optimize the block allocation
993 */
994 if (bg_idx >= fs->no_blkgrp)
995 goto fail;
996
Stefan Brüns688d0e72016-09-17 02:10:10 +0200997 struct ext2_block_group *bgd = NULL;
998 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
999 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
Uma Shankared34f342012-05-25 21:22:49 +05301000 debug("block group %u is full. Skipping\n", bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001001 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1002 if (fs->blksz == 1024)
1003 fs->curr_blkno += 1;
Uma Shankared34f342012-05-25 21:22:49 +05301004 goto restart;
1005 }
1006
Stefan Brüns688d0e72016-09-17 02:10:10 +02001007 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1008 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1009 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankared34f342012-05-25 21:22:49 +05301010 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001011 put_ext4(b_bitmap_blk * fs->blksz,
1012 zero_buffer, fs->blksz);
1013 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1014 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301015 }
1016
1017 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1018 bg_idx) != 0) {
1019 debug("going for restart for the block no %ld %u\n",
1020 fs->curr_blkno, bg_idx);
Stefan Brünsa9fa0ed2016-09-06 04:36:49 +02001021 fs->curr_blkno++;
Uma Shankared34f342012-05-25 21:22:49 +05301022 goto restart;
1023 }
1024
1025 /* journal backup */
1026 if (prev_bg_bitmap_index != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001027 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301028 0, fs->blksz, journal_buffer);
1029 if (status == 0)
1030 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001031 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301032 goto fail;
1033
1034 prev_bg_bitmap_index = bg_idx;
1035 }
Stefan Brüns688d0e72016-09-17 02:10:10 +02001036 ext4fs_bg_free_blocks_dec(bgd);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001037 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301038 goto success;
1039 }
1040success:
1041 free(journal_buffer);
1042 free(zero_buffer);
1043
1044 return fs->curr_blkno;
1045fail:
1046 free(journal_buffer);
1047 free(zero_buffer);
1048
1049 return -1;
1050}
1051
1052int ext4fs_get_new_inode_no(void)
1053{
1054 short i;
1055 short status;
1056 unsigned int ibmap_idx;
1057 static int prev_inode_bitmap_index = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001058 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankared34f342012-05-25 21:22:49 +05301059 struct ext_filesystem *fs = get_fs();
1060 char *journal_buffer = zalloc(fs->blksz);
1061 char *zero_buffer = zalloc(fs->blksz);
1062 if (!journal_buffer || !zero_buffer)
1063 goto fail;
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001064 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1065 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
Uma Shankared34f342012-05-25 21:22:49 +05301066
1067 if (fs->first_pass_ibmap == 0) {
1068 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001069 uint32_t free_inodes;
1070 struct ext2_block_group *bgd = NULL;
1071 bgd = ext4fs_get_group_descriptor(fs, i);
1072 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1073 if (free_inodes) {
1074 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1075 uint64_t i_bitmap_blk =
1076 ext4fs_bg_get_inode_id(bgd, fs);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001077 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001078 bgd->bg_itable_unused = free_inodes;
1079 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1080 put_ext4(i_bitmap_blk * fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301081 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001082 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1083 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301084 memcpy(fs->inode_bmaps[i],
1085 zero_buffer, fs->blksz);
1086 }
1087 fs->curr_inode_no =
1088 _get_new_inode_no(fs->inode_bmaps[i]);
1089 if (fs->curr_inode_no == -1)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001090 /* inode bitmap is completely filled */
Uma Shankared34f342012-05-25 21:22:49 +05301091 continue;
1092 fs->curr_inode_no = fs->curr_inode_no +
1093 (i * inodes_per_grp);
1094 fs->first_pass_ibmap++;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001095 ext4fs_bg_free_inodes_dec(bgd);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001096 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001097 ext4fs_bg_itable_unused_dec(bgd);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001098 ext4fs_sb_free_inodes_dec(fs->sb);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001099 status = ext4fs_devread(i_bitmap_blk *
1100 fs->sect_perblk,
1101 0, fs->blksz,
Uma Shankared34f342012-05-25 21:22:49 +05301102 journal_buffer);
1103 if (status == 0)
1104 goto fail;
1105 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001106 i_bitmap_blk))
Uma Shankared34f342012-05-25 21:22:49 +05301107 goto fail;
1108 goto success;
1109 } else
1110 debug("no inode left on block group %d\n", i);
1111 }
1112 goto fail;
1113 } else {
1114restart:
1115 fs->curr_inode_no++;
1116 /* get the blockbitmap index respective to blockno */
1117 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001118 struct ext2_block_group *bgd =
1119 ext4fs_get_group_descriptor(fs, ibmap_idx);
1120 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1121 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1122
1123 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1124 put_ext4(i_bitmap_blk * fs->blksz,
Michael Walle58a9ecb2016-09-01 11:21:40 +02001125 zero_buffer, fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +02001126 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1127 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankared34f342012-05-25 21:22:49 +05301128 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1129 fs->blksz);
1130 }
1131
1132 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1133 fs->inode_bmaps[ibmap_idx],
1134 ibmap_idx) != 0) {
1135 debug("going for restart for the block no %d %u\n",
1136 fs->curr_inode_no, ibmap_idx);
1137 goto restart;
1138 }
1139
1140 /* journal backup */
1141 if (prev_inode_bitmap_index != ibmap_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +02001142 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301143 0, fs->blksz, journal_buffer);
1144 if (status == 0)
1145 goto fail;
1146 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +02001147 le32_to_cpu(bgd->inode_id)))
Uma Shankared34f342012-05-25 21:22:49 +05301148 goto fail;
1149 prev_inode_bitmap_index = ibmap_idx;
1150 }
Stefan Brüns688d0e72016-09-17 02:10:10 +02001151 ext4fs_bg_free_inodes_dec(bgd);
Stefan Brüns398d6fa2016-09-06 04:36:47 +02001152 if (has_gdt_chksum)
Stefan Brüns688d0e72016-09-17 02:10:10 +02001153 bgd->bg_itable_unused = bgd->free_inodes;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001154 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankared34f342012-05-25 21:22:49 +05301155 goto success;
1156 }
1157
1158success:
1159 free(journal_buffer);
1160 free(zero_buffer);
1161
1162 return fs->curr_inode_no;
1163fail:
1164 free(journal_buffer);
1165 free(zero_buffer);
1166
1167 return -1;
1168
1169}
1170
1171
1172static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1173 unsigned int *total_remaining_blocks,
1174 unsigned int *no_blks_reqd)
1175{
1176 short i;
1177 short status;
1178 long int actual_block_no;
1179 long int si_blockno;
1180 /* si :single indirect */
Michael Walle58a9ecb2016-09-01 11:21:40 +02001181 __le32 *si_buffer = NULL;
1182 __le32 *si_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301183 struct ext_filesystem *fs = get_fs();
1184
1185 if (*total_remaining_blocks != 0) {
1186 si_buffer = zalloc(fs->blksz);
1187 if (!si_buffer) {
1188 printf("No Memory\n");
1189 return;
1190 }
1191 si_start_addr = si_buffer;
1192 si_blockno = ext4fs_get_new_blk_no();
1193 if (si_blockno == -1) {
1194 printf("no block left to assign\n");
1195 goto fail;
1196 }
1197 (*no_blks_reqd)++;
1198 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1199
Frederic Leroy04735e92013-06-26 18:11:25 +02001200 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankared34f342012-05-25 21:22:49 +05301201 0, fs->blksz, (char *)si_buffer);
1202 memset(si_buffer, '\0', fs->blksz);
1203 if (status == 0)
1204 goto fail;
1205
1206 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1207 actual_block_no = ext4fs_get_new_blk_no();
1208 if (actual_block_no == -1) {
1209 printf("no block left to assign\n");
1210 goto fail;
1211 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001212 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301213 debug("SIAB %u: %u\n", *si_buffer,
1214 *total_remaining_blocks);
1215
1216 si_buffer++;
1217 (*total_remaining_blocks)--;
1218 if (*total_remaining_blocks == 0)
1219 break;
1220 }
1221
1222 /* write the block to disk */
Ma Haijun05508702014-01-08 08:15:33 +08001223 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301224 si_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001225 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301226 }
1227fail:
1228 free(si_start_addr);
1229}
1230
1231static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1232 unsigned int *total_remaining_blocks,
1233 unsigned int *no_blks_reqd)
1234{
1235 short i;
1236 short j;
1237 short status;
1238 long int actual_block_no;
1239 /* di:double indirect */
1240 long int di_blockno_parent;
1241 long int di_blockno_child;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001242 __le32 *di_parent_buffer = NULL;
1243 __le32 *di_child_buff = NULL;
1244 __le32 *di_block_start_addr = NULL;
1245 __le32 *di_child_buff_start = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301246 struct ext_filesystem *fs = get_fs();
1247
1248 if (*total_remaining_blocks != 0) {
1249 /* double indirect parent block connecting to inode */
1250 di_blockno_parent = ext4fs_get_new_blk_no();
1251 if (di_blockno_parent == -1) {
1252 printf("no block left to assign\n");
1253 goto fail;
1254 }
1255 di_parent_buffer = zalloc(fs->blksz);
1256 if (!di_parent_buffer)
1257 goto fail;
1258
1259 di_block_start_addr = di_parent_buffer;
1260 (*no_blks_reqd)++;
1261 debug("DIPB %ld: %u\n", di_blockno_parent,
1262 *total_remaining_blocks);
1263
Frederic Leroy04735e92013-06-26 18:11:25 +02001264 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankared34f342012-05-25 21:22:49 +05301265 fs->sect_perblk, 0,
1266 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001267
1268 if (!status) {
1269 printf("%s: Device read error!\n", __func__);
1270 goto fail;
1271 }
Uma Shankared34f342012-05-25 21:22:49 +05301272 memset(di_parent_buffer, '\0', fs->blksz);
1273
1274 /*
1275 * start:for each double indirect parent
1276 * block create one more block
1277 */
1278 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1279 di_blockno_child = ext4fs_get_new_blk_no();
1280 if (di_blockno_child == -1) {
1281 printf("no block left to assign\n");
1282 goto fail;
1283 }
1284 di_child_buff = zalloc(fs->blksz);
1285 if (!di_child_buff)
1286 goto fail;
1287
1288 di_child_buff_start = di_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001289 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankared34f342012-05-25 21:22:49 +05301290 di_parent_buffer++;
1291 (*no_blks_reqd)++;
1292 debug("DICB %ld: %u\n", di_blockno_child,
1293 *total_remaining_blocks);
1294
Frederic Leroy04735e92013-06-26 18:11:25 +02001295 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankared34f342012-05-25 21:22:49 +05301296 fs->sect_perblk, 0,
1297 fs->blksz,
1298 (char *)di_child_buff);
Łukasz Majewskid429ca02012-12-05 08:06:39 +00001299
1300 if (!status) {
1301 printf("%s: Device read error!\n", __func__);
1302 goto fail;
1303 }
Uma Shankared34f342012-05-25 21:22:49 +05301304 memset(di_child_buff, '\0', fs->blksz);
1305 /* filling of actual datablocks for each child */
1306 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1307 actual_block_no = ext4fs_get_new_blk_no();
1308 if (actual_block_no == -1) {
1309 printf("no block left to assign\n");
1310 goto fail;
1311 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001312 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301313 debug("DIAB %ld: %u\n", actual_block_no,
1314 *total_remaining_blocks);
1315
1316 di_child_buff++;
1317 (*total_remaining_blocks)--;
1318 if (*total_remaining_blocks == 0)
1319 break;
1320 }
1321 /* write the block table */
Ma Haijun05508702014-01-08 08:15:33 +08001322 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301323 di_child_buff_start, fs->blksz);
1324 free(di_child_buff_start);
1325 di_child_buff_start = NULL;
1326
1327 if (*total_remaining_blocks == 0)
1328 break;
1329 }
Ma Haijun05508702014-01-08 08:15:33 +08001330 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301331 di_block_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001332 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankared34f342012-05-25 21:22:49 +05301333 }
1334fail:
1335 free(di_block_start_addr);
1336}
1337
1338static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1339 unsigned int *total_remaining_blocks,
1340 unsigned int *no_blks_reqd)
1341{
1342 short i;
1343 short j;
1344 short k;
1345 long int actual_block_no;
1346 /* ti: Triple Indirect */
1347 long int ti_gp_blockno;
1348 long int ti_parent_blockno;
1349 long int ti_child_blockno;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001350 __le32 *ti_gp_buff = NULL;
1351 __le32 *ti_parent_buff = NULL;
1352 __le32 *ti_child_buff = NULL;
1353 __le32 *ti_gp_buff_start_addr = NULL;
1354 __le32 *ti_pbuff_start_addr = NULL;
1355 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankared34f342012-05-25 21:22:49 +05301356 struct ext_filesystem *fs = get_fs();
1357 if (*total_remaining_blocks != 0) {
1358 /* triple indirect grand parent block connecting to inode */
1359 ti_gp_blockno = ext4fs_get_new_blk_no();
1360 if (ti_gp_blockno == -1) {
1361 printf("no block left to assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001362 return;
Uma Shankared34f342012-05-25 21:22:49 +05301363 }
1364 ti_gp_buff = zalloc(fs->blksz);
1365 if (!ti_gp_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001366 return;
Uma Shankared34f342012-05-25 21:22:49 +05301367
1368 ti_gp_buff_start_addr = ti_gp_buff;
1369 (*no_blks_reqd)++;
1370 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1371 *total_remaining_blocks);
1372
1373 /* for each 4 byte grand parent entry create one more block */
1374 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1375 ti_parent_blockno = ext4fs_get_new_blk_no();
1376 if (ti_parent_blockno == -1) {
1377 printf("no block left to assign\n");
1378 goto fail;
1379 }
1380 ti_parent_buff = zalloc(fs->blksz);
1381 if (!ti_parent_buff)
1382 goto fail;
1383
1384 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001385 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301386 ti_gp_buff++;
1387 (*no_blks_reqd)++;
1388 debug("TIPB %ld: %u\n", ti_parent_blockno,
1389 *total_remaining_blocks);
1390
1391 /* for each 4 byte entry parent create one more block */
1392 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1393 ti_child_blockno = ext4fs_get_new_blk_no();
1394 if (ti_child_blockno == -1) {
1395 printf("no block left assign\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001396 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301397 }
1398 ti_child_buff = zalloc(fs->blksz);
1399 if (!ti_child_buff)
Tom Rini495c3a12015-12-10 16:42:21 -05001400 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301401
1402 ti_cbuff_start_addr = ti_child_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +02001403 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301404 ti_parent_buff++;
1405 (*no_blks_reqd)++;
1406 debug("TICB %ld: %u\n", ti_parent_blockno,
1407 *total_remaining_blocks);
1408
1409 /* fill actual datablocks for each child */
1410 for (k = 0; k < (fs->blksz / sizeof(int));
1411 k++) {
1412 actual_block_no =
1413 ext4fs_get_new_blk_no();
1414 if (actual_block_no == -1) {
1415 printf("no block left\n");
Tom Rini495c3a12015-12-10 16:42:21 -05001416 free(ti_cbuff_start_addr);
1417 goto fail1;
Uma Shankared34f342012-05-25 21:22:49 +05301418 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001419 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankared34f342012-05-25 21:22:49 +05301420 debug("TIAB %ld: %u\n", actual_block_no,
1421 *total_remaining_blocks);
1422
1423 ti_child_buff++;
1424 (*total_remaining_blocks)--;
1425 if (*total_remaining_blocks == 0)
1426 break;
1427 }
1428 /* write the child block */
Ma Haijun05508702014-01-08 08:15:33 +08001429 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1430 (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301431 ti_cbuff_start_addr, fs->blksz);
1432 free(ti_cbuff_start_addr);
1433
1434 if (*total_remaining_blocks == 0)
1435 break;
1436 }
1437 /* write the parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001438 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301439 ti_pbuff_start_addr, fs->blksz);
1440 free(ti_pbuff_start_addr);
1441
1442 if (*total_remaining_blocks == 0)
1443 break;
1444 }
1445 /* write the grand parent block */
Ma Haijun05508702014-01-08 08:15:33 +08001446 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankared34f342012-05-25 21:22:49 +05301447 ti_gp_buff_start_addr, fs->blksz);
Michael Walle58a9ecb2016-09-01 11:21:40 +02001448 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini495c3a12015-12-10 16:42:21 -05001449 free(ti_gp_buff_start_addr);
1450 return;
Uma Shankared34f342012-05-25 21:22:49 +05301451 }
Tom Rini495c3a12015-12-10 16:42:21 -05001452fail1:
1453 free(ti_pbuff_start_addr);
Uma Shankared34f342012-05-25 21:22:49 +05301454fail:
1455 free(ti_gp_buff_start_addr);
1456}
1457
1458void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1459 unsigned int total_remaining_blocks,
1460 unsigned int *total_no_of_block)
1461{
1462 short i;
1463 long int direct_blockno;
1464 unsigned int no_blks_reqd = 0;
1465
1466 /* allocation of direct blocks */
Stephen Warrend0180282014-06-11 12:46:16 -06001467 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankared34f342012-05-25 21:22:49 +05301468 direct_blockno = ext4fs_get_new_blk_no();
1469 if (direct_blockno == -1) {
1470 printf("no block left to assign\n");
1471 return;
1472 }
Michael Walle58a9ecb2016-09-01 11:21:40 +02001473 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankared34f342012-05-25 21:22:49 +05301474 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1475
1476 total_remaining_blocks--;
Uma Shankared34f342012-05-25 21:22:49 +05301477 }
1478
1479 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1480 &no_blks_reqd);
1481 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1482 &no_blks_reqd);
1483 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1484 &no_blks_reqd);
1485 *total_no_of_block += no_blks_reqd;
1486}
1487
1488#endif
1489
Uma Shankara1596432012-05-25 21:21:44 +05301490static struct ext4_extent_header *ext4fs_get_extent_block
1491 (struct ext2_data *data, char *buf,
1492 struct ext4_extent_header *ext_block,
1493 uint32_t fileblock, int log2_blksz)
1494{
1495 struct ext4_extent_idx *index;
1496 unsigned long long block;
Ionut Nicu47017322014-01-13 11:59:24 +01001497 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankara1596432012-05-25 21:21:44 +05301498 int i;
1499
1500 while (1) {
1501 index = (struct ext4_extent_idx *)(ext_block + 1);
1502
Rommel Custodio8b415f72013-07-21 10:53:25 +02001503 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001504 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301505
1506 if (ext_block->eh_depth == 0)
1507 return ext_block;
1508 i = -1;
1509 do {
1510 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001511 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301512 break;
Ionut Nicub5bbac12014-01-13 12:00:08 +01001513 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankara1596432012-05-25 21:21:44 +05301514
1515 if (--i < 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02001516 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301517
Rommel Custodio8b415f72013-07-21 10:53:25 +02001518 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301519 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1520
Ionut Nicu47017322014-01-13 11:59:24 +01001521 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
Frederic Leroy04735e92013-06-26 18:11:25 +02001522 buf))
Uma Shankara1596432012-05-25 21:21:44 +05301523 ext_block = (struct ext4_extent_header *)buf;
1524 else
Michael Walle58a9ecb2016-09-01 11:21:40 +02001525 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05301526 }
1527}
1528
1529static int ext4fs_blockgroup
1530 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1531{
1532 long int blkno;
1533 unsigned int blkoff, desc_per_blk;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001534 int log2blksz = get_fs()->dev_desc->log2blksz;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001535 int desc_size = get_fs()->gdsize;
Uma Shankara1596432012-05-25 21:21:44 +05301536
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001537 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301538
Michael Walle7f101be2016-08-29 10:46:44 +02001539 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankara1596432012-05-25 21:21:44 +05301540 group / desc_per_blk;
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001541 blkoff = (group % desc_per_blk) * desc_size;
Uma Shankara1596432012-05-25 21:21:44 +05301542
1543 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1544 group, blkno, blkoff);
1545
Frederic Leroy04735e92013-06-26 18:11:25 +02001546 return ext4fs_devread((lbaint_t)blkno <<
1547 (LOG2_BLOCK_SIZE(data) - log2blksz),
Stefan Brünsf798b1d2016-09-17 02:10:09 +02001548 blkoff, desc_size, (char *)blkgrp);
Uma Shankara1596432012-05-25 21:21:44 +05301549}
1550
1551int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1552{
1553 struct ext2_block_group blkgrp;
1554 struct ext2_sblock *sblock = &data->sblock;
1555 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00001556 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +05301557 int inodes_per_block, status;
1558 long int blkno;
1559 unsigned int blkoff;
1560
1561 /* It is easier to calculate if the first inode is 0. */
1562 ino--;
Michael Walle7f101be2016-08-29 10:46:44 +02001563 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301564 (sblock->inodes_per_group), &blkgrp);
1565 if (status == 0)
1566 return 0;
1567
1568 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Stefan Brüns688d0e72016-09-17 02:10:10 +02001569 blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
Michael Walle7f101be2016-08-29 10:46:44 +02001570 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankara1596432012-05-25 21:21:44 +05301571 blkoff = (ino % inodes_per_block) * fs->inodesz;
1572 /* Read the inode. */
Frederic Leroy04735e92013-06-26 18:11:25 +02001573 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1574 log2blksz), blkoff,
Uma Shankara1596432012-05-25 21:21:44 +05301575 sizeof(struct ext2_inode), (char *)inode);
1576 if (status == 0)
1577 return 0;
1578
1579 return 1;
1580}
1581
1582long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1583{
1584 long int blknr;
1585 int blksz;
1586 int log2_blksz;
1587 int status;
1588 long int rblock;
1589 long int perblock_parent;
1590 long int perblock_child;
1591 unsigned long long start;
1592 /* get the blocksize of the filesystem */
1593 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +00001594 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1595 - get_fs()->dev_desc->log2blksz;
1596
Uma Shankara1596432012-05-25 21:21:44 +05301597 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1598 char *buf = zalloc(blksz);
1599 if (!buf)
1600 return -ENOMEM;
1601 struct ext4_extent_header *ext_block;
1602 struct ext4_extent *extent;
1603 int i = -1;
Egbert Eich50ce4c02013-05-01 01:13:19 +00001604 ext_block =
1605 ext4fs_get_extent_block(ext4fs_root, buf,
1606 (struct ext4_extent_header *)
1607 inode->b.blocks.dir_blocks,
1608 fileblock, log2_blksz);
Uma Shankara1596432012-05-25 21:21:44 +05301609 if (!ext_block) {
1610 printf("invalid extent block\n");
1611 free(buf);
1612 return -EINVAL;
1613 }
1614
1615 extent = (struct ext4_extent *)(ext_block + 1);
1616
1617 do {
1618 i++;
Rommel Custodio8b415f72013-07-21 10:53:25 +02001619 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankara1596432012-05-25 21:21:44 +05301620 break;
1621 } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1622 if (--i >= 0) {
1623 fileblock -= le32_to_cpu(extent[i].ee_block);
Rommel Custodio8b415f72013-07-21 10:53:25 +02001624 if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
Uma Shankara1596432012-05-25 21:21:44 +05301625 free(buf);
1626 return 0;
1627 }
1628
Rommel Custodio8b415f72013-07-21 10:53:25 +02001629 start = le16_to_cpu(extent[i].ee_start_hi);
Uma Shankara1596432012-05-25 21:21:44 +05301630 start = (start << 32) +
1631 le32_to_cpu(extent[i].ee_start_lo);
1632 free(buf);
1633 return fileblock + start;
1634 }
1635
1636 printf("Extent Error\n");
1637 free(buf);
1638 return -1;
1639 }
1640
1641 /* Direct blocks. */
1642 if (fileblock < INDIRECT_BLOCKS)
Michael Walle7f101be2016-08-29 10:46:44 +02001643 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301644
1645 /* Indirect. */
1646 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1647 if (ext4fs_indir1_block == NULL) {
1648 ext4fs_indir1_block = zalloc(blksz);
1649 if (ext4fs_indir1_block == NULL) {
1650 printf("** SI ext2fs read block (indir 1)"
1651 "malloc failed. **\n");
1652 return -1;
1653 }
1654 ext4fs_indir1_size = blksz;
1655 ext4fs_indir1_blkno = -1;
1656 }
1657 if (blksz != ext4fs_indir1_size) {
1658 free(ext4fs_indir1_block);
1659 ext4fs_indir1_block = NULL;
1660 ext4fs_indir1_size = 0;
1661 ext4fs_indir1_blkno = -1;
1662 ext4fs_indir1_block = zalloc(blksz);
1663 if (ext4fs_indir1_block == NULL) {
1664 printf("** SI ext2fs read block (indir 1):"
1665 "malloc failed. **\n");
1666 return -1;
1667 }
1668 ext4fs_indir1_size = blksz;
1669 }
Michael Walle7f101be2016-08-29 10:46:44 +02001670 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301671 log2_blksz) != ext4fs_indir1_blkno) {
1672 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001673 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301674 (inode->b.blocks.
1675 indir_block) << log2_blksz, 0,
1676 blksz, (char *)ext4fs_indir1_block);
1677 if (status == 0) {
1678 printf("** SI ext2fs read block (indir 1)"
1679 "failed. **\n");
Stefan Brünsde9e8312016-09-06 04:36:55 +02001680 return -1;
Uma Shankara1596432012-05-25 21:21:44 +05301681 }
1682 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001683 le32_to_cpu(inode->b.blocks.
Uma Shankara1596432012-05-25 21:21:44 +05301684 indir_block) << log2_blksz;
1685 }
Michael Walle7f101be2016-08-29 10:46:44 +02001686 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankara1596432012-05-25 21:21:44 +05301687 [fileblock - INDIRECT_BLOCKS]);
1688 }
1689 /* Double indirect. */
1690 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1691 (blksz / 4 + 1)))) {
1692
1693 long int perblock = blksz / 4;
1694 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1695
1696 if (ext4fs_indir1_block == NULL) {
1697 ext4fs_indir1_block = zalloc(blksz);
1698 if (ext4fs_indir1_block == NULL) {
1699 printf("** DI ext2fs read block (indir 2 1)"
1700 "malloc failed. **\n");
1701 return -1;
1702 }
1703 ext4fs_indir1_size = blksz;
1704 ext4fs_indir1_blkno = -1;
1705 }
1706 if (blksz != ext4fs_indir1_size) {
1707 free(ext4fs_indir1_block);
1708 ext4fs_indir1_block = NULL;
1709 ext4fs_indir1_size = 0;
1710 ext4fs_indir1_blkno = -1;
1711 ext4fs_indir1_block = zalloc(blksz);
1712 if (ext4fs_indir1_block == NULL) {
1713 printf("** DI ext2fs read block (indir 2 1)"
1714 "malloc failed. **\n");
1715 return -1;
1716 }
1717 ext4fs_indir1_size = blksz;
1718 }
Michael Walle7f101be2016-08-29 10:46:44 +02001719 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301720 log2_blksz) != ext4fs_indir1_blkno) {
1721 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001722 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301723 (inode->b.blocks.
1724 double_indir_block) << log2_blksz,
1725 0, blksz,
1726 (char *)ext4fs_indir1_block);
1727 if (status == 0) {
1728 printf("** DI ext2fs read block (indir 2 1)"
1729 "failed. **\n");
1730 return -1;
1731 }
1732 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001733 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301734 log2_blksz;
1735 }
1736
1737 if (ext4fs_indir2_block == NULL) {
1738 ext4fs_indir2_block = zalloc(blksz);
1739 if (ext4fs_indir2_block == NULL) {
1740 printf("** DI ext2fs read block (indir 2 2)"
1741 "malloc failed. **\n");
1742 return -1;
1743 }
1744 ext4fs_indir2_size = blksz;
1745 ext4fs_indir2_blkno = -1;
1746 }
1747 if (blksz != ext4fs_indir2_size) {
1748 free(ext4fs_indir2_block);
1749 ext4fs_indir2_block = NULL;
1750 ext4fs_indir2_size = 0;
1751 ext4fs_indir2_blkno = -1;
1752 ext4fs_indir2_block = zalloc(blksz);
1753 if (ext4fs_indir2_block == NULL) {
1754 printf("** DI ext2fs read block (indir 2 2)"
1755 "malloc failed. **\n");
1756 return -1;
1757 }
1758 ext4fs_indir2_size = blksz;
1759 }
Michael Walle7f101be2016-08-29 10:46:44 +02001760 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankara1596432012-05-25 21:21:44 +05301761 log2_blksz) != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001762 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301763 (ext4fs_indir1_block
1764 [rblock /
1765 perblock]) << log2_blksz, 0,
1766 blksz,
1767 (char *)ext4fs_indir2_block);
1768 if (status == 0) {
1769 printf("** DI ext2fs read block (indir 2 2)"
1770 "failed. **\n");
1771 return -1;
1772 }
1773 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001774 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301775 /
1776 perblock]) <<
1777 log2_blksz;
1778 }
Michael Walle7f101be2016-08-29 10:46:44 +02001779 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankara1596432012-05-25 21:21:44 +05301780 }
1781 /* Tripple indirect. */
1782 else {
1783 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1784 (blksz / 4 * blksz / 4));
1785 perblock_child = blksz / 4;
1786 perblock_parent = ((blksz / 4) * (blksz / 4));
1787
1788 if (ext4fs_indir1_block == NULL) {
1789 ext4fs_indir1_block = zalloc(blksz);
1790 if (ext4fs_indir1_block == NULL) {
1791 printf("** TI ext2fs read block (indir 2 1)"
1792 "malloc failed. **\n");
1793 return -1;
1794 }
1795 ext4fs_indir1_size = blksz;
1796 ext4fs_indir1_blkno = -1;
1797 }
1798 if (blksz != ext4fs_indir1_size) {
1799 free(ext4fs_indir1_block);
1800 ext4fs_indir1_block = NULL;
1801 ext4fs_indir1_size = 0;
1802 ext4fs_indir1_blkno = -1;
1803 ext4fs_indir1_block = zalloc(blksz);
1804 if (ext4fs_indir1_block == NULL) {
1805 printf("** TI ext2fs read block (indir 2 1)"
1806 "malloc failed. **\n");
1807 return -1;
1808 }
1809 ext4fs_indir1_size = blksz;
1810 }
Michael Walle7f101be2016-08-29 10:46:44 +02001811 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301812 log2_blksz) != ext4fs_indir1_blkno) {
1813 status = ext4fs_devread
Frederic Leroy04735e92013-06-26 18:11:25 +02001814 ((lbaint_t)
Michael Walle7f101be2016-08-29 10:46:44 +02001815 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankara1596432012-05-25 21:21:44 +05301816 << log2_blksz, 0, blksz,
1817 (char *)ext4fs_indir1_block);
1818 if (status == 0) {
1819 printf("** TI ext2fs read block (indir 2 1)"
1820 "failed. **\n");
1821 return -1;
1822 }
1823 ext4fs_indir1_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001824 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankara1596432012-05-25 21:21:44 +05301825 log2_blksz;
1826 }
1827
1828 if (ext4fs_indir2_block == NULL) {
1829 ext4fs_indir2_block = zalloc(blksz);
1830 if (ext4fs_indir2_block == NULL) {
1831 printf("** TI ext2fs read block (indir 2 2)"
1832 "malloc failed. **\n");
1833 return -1;
1834 }
1835 ext4fs_indir2_size = blksz;
1836 ext4fs_indir2_blkno = -1;
1837 }
1838 if (blksz != ext4fs_indir2_size) {
1839 free(ext4fs_indir2_block);
1840 ext4fs_indir2_block = NULL;
1841 ext4fs_indir2_size = 0;
1842 ext4fs_indir2_blkno = -1;
1843 ext4fs_indir2_block = zalloc(blksz);
1844 if (ext4fs_indir2_block == NULL) {
1845 printf("** TI ext2fs read block (indir 2 2)"
1846 "malloc failed. **\n");
1847 return -1;
1848 }
1849 ext4fs_indir2_size = blksz;
1850 }
Michael Walle7f101be2016-08-29 10:46:44 +02001851 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301852 perblock_parent]) <<
1853 log2_blksz)
1854 != ext4fs_indir2_blkno) {
Michael Walle7f101be2016-08-29 10:46:44 +02001855 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301856 (ext4fs_indir1_block
1857 [rblock /
1858 perblock_parent]) <<
1859 log2_blksz, 0, blksz,
1860 (char *)ext4fs_indir2_block);
1861 if (status == 0) {
1862 printf("** TI ext2fs read block (indir 2 2)"
1863 "failed. **\n");
1864 return -1;
1865 }
1866 ext4fs_indir2_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001867 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301868 perblock_parent])
1869 << log2_blksz;
1870 }
1871
1872 if (ext4fs_indir3_block == NULL) {
1873 ext4fs_indir3_block = zalloc(blksz);
1874 if (ext4fs_indir3_block == NULL) {
1875 printf("** TI ext2fs read block (indir 2 2)"
1876 "malloc failed. **\n");
1877 return -1;
1878 }
1879 ext4fs_indir3_size = blksz;
1880 ext4fs_indir3_blkno = -1;
1881 }
1882 if (blksz != ext4fs_indir3_size) {
1883 free(ext4fs_indir3_block);
1884 ext4fs_indir3_block = NULL;
1885 ext4fs_indir3_size = 0;
1886 ext4fs_indir3_blkno = -1;
1887 ext4fs_indir3_block = zalloc(blksz);
1888 if (ext4fs_indir3_block == NULL) {
1889 printf("** TI ext2fs read block (indir 2 2)"
1890 "malloc failed. **\n");
1891 return -1;
1892 }
1893 ext4fs_indir3_size = blksz;
1894 }
Michael Walle7f101be2016-08-29 10:46:44 +02001895 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankara1596432012-05-25 21:21:44 +05301896 /
1897 perblock_child]) <<
1898 log2_blksz) != ext4fs_indir3_blkno) {
1899 status =
Michael Walle7f101be2016-08-29 10:46:44 +02001900 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05301901 (ext4fs_indir2_block
1902 [(rblock / perblock_child)
1903 % (blksz / 4)]) << log2_blksz, 0,
1904 blksz, (char *)ext4fs_indir3_block);
1905 if (status == 0) {
1906 printf("** TI ext2fs read block (indir 2 2)"
1907 "failed. **\n");
1908 return -1;
1909 }
1910 ext4fs_indir3_blkno =
Michael Walle7f101be2016-08-29 10:46:44 +02001911 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankara1596432012-05-25 21:21:44 +05301912 perblock_child) %
1913 (blksz /
1914 4)]) <<
1915 log2_blksz;
1916 }
1917
Michael Walle7f101be2016-08-29 10:46:44 +02001918 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankara1596432012-05-25 21:21:44 +05301919 [rblock % perblock_child]);
1920 }
Egbert Eich50ce4c02013-05-01 01:13:19 +00001921 debug("read_allocated_block %ld\n", blknr);
Uma Shankara1596432012-05-25 21:21:44 +05301922
1923 return blknr;
1924}
1925
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001926/**
1927 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1928 * global pointers
1929 *
1930 * This function assures that for a file with the same name but different size
1931 * the sequential store on the ext4 filesystem will be correct.
1932 *
1933 * In this function the global data, responsible for internal representation
1934 * of the ext4 data are initialized to the reset state. Without this, during
1935 * replacement of the smaller file with the bigger truncation of new file was
1936 * performed.
1937 */
1938void ext4fs_reinit_global(void)
Uma Shankara1596432012-05-25 21:21:44 +05301939{
Uma Shankara1596432012-05-25 21:21:44 +05301940 if (ext4fs_indir1_block != NULL) {
1941 free(ext4fs_indir1_block);
1942 ext4fs_indir1_block = NULL;
1943 ext4fs_indir1_size = 0;
1944 ext4fs_indir1_blkno = -1;
1945 }
1946 if (ext4fs_indir2_block != NULL) {
1947 free(ext4fs_indir2_block);
1948 ext4fs_indir2_block = NULL;
1949 ext4fs_indir2_size = 0;
1950 ext4fs_indir2_blkno = -1;
1951 }
1952 if (ext4fs_indir3_block != NULL) {
1953 free(ext4fs_indir3_block);
1954 ext4fs_indir3_block = NULL;
1955 ext4fs_indir3_size = 0;
1956 ext4fs_indir3_blkno = -1;
1957 }
1958}
Łukasz Majewski8b454ee2014-05-06 09:36:05 +02001959void ext4fs_close(void)
1960{
1961 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1962 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1963 ext4fs_file = NULL;
1964 }
1965 if (ext4fs_root != NULL) {
1966 free(ext4fs_root);
1967 ext4fs_root = NULL;
1968 }
1969
1970 ext4fs_reinit_global();
1971}
Uma Shankara1596432012-05-25 21:21:44 +05301972
1973int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
1974 struct ext2fs_node **fnode, int *ftype)
1975{
1976 unsigned int fpos = 0;
1977 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001978 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05301979 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
1980
1981#ifdef DEBUG
1982 if (name != NULL)
1983 printf("Iterate dir %s\n", name);
1984#endif /* of DEBUG */
1985 if (!diro->inode_read) {
1986 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
1987 if (status == 0)
1988 return 0;
1989 }
1990 /* Search the file. */
Michael Walle7f101be2016-08-29 10:46:44 +02001991 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankara1596432012-05-25 21:21:44 +05301992 struct ext2_dirent dirent;
1993
1994 status = ext4fs_read_file(diro, fpos,
1995 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001996 (char *)&dirent, &actread);
1997 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05301998 return 0;
1999
Thomas Fitzsimmons54d68e92015-11-18 12:42:53 -05002000 if (dirent.direntlen == 0) {
2001 printf("Failed to iterate over directory %s\n", name);
2002 return 0;
2003 }
2004
Uma Shankara1596432012-05-25 21:21:44 +05302005 if (dirent.namelen != 0) {
2006 char filename[dirent.namelen + 1];
2007 struct ext2fs_node *fdiro;
2008 int type = FILETYPE_UNKNOWN;
2009
2010 status = ext4fs_read_file(diro,
2011 fpos +
2012 sizeof(struct ext2_dirent),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002013 dirent.namelen, filename,
2014 &actread);
2015 if (status < 0)
Uma Shankara1596432012-05-25 21:21:44 +05302016 return 0;
2017
2018 fdiro = zalloc(sizeof(struct ext2fs_node));
2019 if (!fdiro)
2020 return 0;
2021
2022 fdiro->data = diro->data;
Michael Walle7f101be2016-08-29 10:46:44 +02002023 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankara1596432012-05-25 21:21:44 +05302024
2025 filename[dirent.namelen] = '\0';
2026
2027 if (dirent.filetype != FILETYPE_UNKNOWN) {
2028 fdiro->inode_read = 0;
2029
2030 if (dirent.filetype == FILETYPE_DIRECTORY)
2031 type = FILETYPE_DIRECTORY;
2032 else if (dirent.filetype == FILETYPE_SYMLINK)
2033 type = FILETYPE_SYMLINK;
2034 else if (dirent.filetype == FILETYPE_REG)
2035 type = FILETYPE_REG;
2036 } else {
2037 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002038 le32_to_cpu
Uma Shankara1596432012-05-25 21:21:44 +05302039 (dirent.inode),
2040 &fdiro->inode);
2041 if (status == 0) {
2042 free(fdiro);
2043 return 0;
2044 }
2045 fdiro->inode_read = 1;
2046
Michael Walle7f101be2016-08-29 10:46:44 +02002047 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankara1596432012-05-25 21:21:44 +05302048 FILETYPE_INO_MASK) ==
2049 FILETYPE_INO_DIRECTORY) {
2050 type = FILETYPE_DIRECTORY;
Michael Walle7f101be2016-08-29 10:46:44 +02002051 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302052 & FILETYPE_INO_MASK) ==
2053 FILETYPE_INO_SYMLINK) {
2054 type = FILETYPE_SYMLINK;
Michael Walle7f101be2016-08-29 10:46:44 +02002055 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankara1596432012-05-25 21:21:44 +05302056 & FILETYPE_INO_MASK) ==
2057 FILETYPE_INO_REG) {
2058 type = FILETYPE_REG;
2059 }
2060 }
2061#ifdef DEBUG
2062 printf("iterate >%s<\n", filename);
2063#endif /* of DEBUG */
2064 if ((name != NULL) && (fnode != NULL)
2065 && (ftype != NULL)) {
2066 if (strcmp(filename, name) == 0) {
2067 *ftype = type;
2068 *fnode = fdiro;
2069 return 1;
2070 }
2071 } else {
2072 if (fdiro->inode_read == 0) {
2073 status = ext4fs_read_inode(diro->data,
Michael Walle7f101be2016-08-29 10:46:44 +02002074 le32_to_cpu(
Uma Shankara1596432012-05-25 21:21:44 +05302075 dirent.inode),
2076 &fdiro->inode);
2077 if (status == 0) {
2078 free(fdiro);
2079 return 0;
2080 }
2081 fdiro->inode_read = 1;
2082 }
2083 switch (type) {
2084 case FILETYPE_DIRECTORY:
2085 printf("<DIR> ");
2086 break;
2087 case FILETYPE_SYMLINK:
2088 printf("<SYM> ");
2089 break;
2090 case FILETYPE_REG:
2091 printf(" ");
2092 break;
2093 default:
2094 printf("< ? > ");
2095 break;
2096 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002097 printf("%10u %s\n",
Michael Walle7f101be2016-08-29 10:46:44 +02002098 le32_to_cpu(fdiro->inode.size),
Uma Shankara1596432012-05-25 21:21:44 +05302099 filename);
2100 }
2101 free(fdiro);
2102 }
Michael Walle7f101be2016-08-29 10:46:44 +02002103 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankara1596432012-05-25 21:21:44 +05302104 }
2105 return 0;
2106}
2107
2108static char *ext4fs_read_symlink(struct ext2fs_node *node)
2109{
2110 char *symlink;
2111 struct ext2fs_node *diro = node;
2112 int status;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002113 loff_t actread;
Uma Shankara1596432012-05-25 21:21:44 +05302114
2115 if (!diro->inode_read) {
2116 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2117 if (status == 0)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002118 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302119 }
Michael Walle7f101be2016-08-29 10:46:44 +02002120 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankara1596432012-05-25 21:21:44 +05302121 if (!symlink)
Michael Walle58a9ecb2016-09-01 11:21:40 +02002122 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302123
Michael Walle7f101be2016-08-29 10:46:44 +02002124 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankara1596432012-05-25 21:21:44 +05302125 strncpy(symlink, diro->inode.b.symlink,
Michael Walle7f101be2016-08-29 10:46:44 +02002126 le32_to_cpu(diro->inode.size));
Uma Shankara1596432012-05-25 21:21:44 +05302127 } else {
2128 status = ext4fs_read_file(diro, 0,
Michael Walle7f101be2016-08-29 10:46:44 +02002129 le32_to_cpu(diro->inode.size),
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002130 symlink, &actread);
Gary Bisson9d2f6a92015-09-07 11:20:07 +02002131 if ((status < 0) || (actread == 0)) {
Uma Shankara1596432012-05-25 21:21:44 +05302132 free(symlink);
Michael Walle58a9ecb2016-09-01 11:21:40 +02002133 return NULL;
Uma Shankara1596432012-05-25 21:21:44 +05302134 }
2135 }
Michael Walle7f101be2016-08-29 10:46:44 +02002136 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankara1596432012-05-25 21:21:44 +05302137 return symlink;
2138}
2139
2140static int ext4fs_find_file1(const char *currpath,
2141 struct ext2fs_node *currroot,
2142 struct ext2fs_node **currfound, int *foundtype)
2143{
2144 char fpath[strlen(currpath) + 1];
2145 char *name = fpath;
2146 char *next;
2147 int status;
2148 int type = FILETYPE_DIRECTORY;
2149 struct ext2fs_node *currnode = currroot;
2150 struct ext2fs_node *oldnode = currroot;
2151
2152 strncpy(fpath, currpath, strlen(currpath) + 1);
2153
2154 /* Remove all leading slashes. */
2155 while (*name == '/')
2156 name++;
2157
2158 if (!*name) {
2159 *currfound = currnode;
2160 return 1;
2161 }
2162
2163 for (;;) {
2164 int found;
2165
2166 /* Extract the actual part from the pathname. */
2167 next = strchr(name, '/');
2168 if (next) {
2169 /* Remove all leading slashes. */
2170 while (*next == '/')
2171 *(next++) = '\0';
2172 }
2173
2174 if (type != FILETYPE_DIRECTORY) {
2175 ext4fs_free_node(currnode, currroot);
2176 return 0;
2177 }
2178
2179 oldnode = currnode;
2180
2181 /* Iterate over the directory. */
2182 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2183 if (found == 0)
2184 return 0;
2185
2186 if (found == -1)
2187 break;
2188
2189 /* Read in the symlink and follow it. */
2190 if (type == FILETYPE_SYMLINK) {
2191 char *symlink;
2192
2193 /* Test if the symlink does not loop. */
2194 if (++symlinknest == 8) {
2195 ext4fs_free_node(currnode, currroot);
2196 ext4fs_free_node(oldnode, currroot);
2197 return 0;
2198 }
2199
2200 symlink = ext4fs_read_symlink(currnode);
2201 ext4fs_free_node(currnode, currroot);
2202
2203 if (!symlink) {
2204 ext4fs_free_node(oldnode, currroot);
2205 return 0;
2206 }
2207
2208 debug("Got symlink >%s<\n", symlink);
2209
2210 if (symlink[0] == '/') {
2211 ext4fs_free_node(oldnode, currroot);
2212 oldnode = &ext4fs_root->diropen;
2213 }
2214
2215 /* Lookup the node the symlink points to. */
2216 status = ext4fs_find_file1(symlink, oldnode,
2217 &currnode, &type);
2218
2219 free(symlink);
2220
2221 if (status == 0) {
2222 ext4fs_free_node(oldnode, currroot);
2223 return 0;
2224 }
2225 }
2226
2227 ext4fs_free_node(oldnode, currroot);
2228
2229 /* Found the node! */
2230 if (!next || *next == '\0') {
2231 *currfound = currnode;
2232 *foundtype = type;
2233 return 1;
2234 }
2235 name = next;
2236 }
2237 return -1;
2238}
2239
2240int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2241 struct ext2fs_node **foundnode, int expecttype)
2242{
2243 int status;
2244 int foundtype = FILETYPE_DIRECTORY;
2245
2246 symlinknest = 0;
2247 if (!path)
2248 return 0;
2249
2250 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2251 if (status == 0)
2252 return 0;
2253
2254 /* Check if the node that was found was of the expected type. */
2255 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2256 return 0;
2257 else if ((expecttype == FILETYPE_DIRECTORY)
2258 && (foundtype != expecttype))
2259 return 0;
2260
2261 return 1;
2262}
2263
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002264int ext4fs_open(const char *filename, loff_t *len)
Uma Shankara1596432012-05-25 21:21:44 +05302265{
2266 struct ext2fs_node *fdiro = NULL;
2267 int status;
Uma Shankara1596432012-05-25 21:21:44 +05302268
2269 if (ext4fs_root == NULL)
2270 return -1;
2271
2272 ext4fs_file = NULL;
2273 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2274 FILETYPE_REG);
2275 if (status == 0)
2276 goto fail;
2277
2278 if (!fdiro->inode_read) {
2279 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2280 &fdiro->inode);
2281 if (status == 0)
2282 goto fail;
2283 }
Michael Walle7f101be2016-08-29 10:46:44 +02002284 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankara1596432012-05-25 21:21:44 +05302285 ext4fs_file = fdiro;
2286
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08002287 return 0;
Uma Shankara1596432012-05-25 21:21:44 +05302288fail:
2289 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2290
2291 return -1;
2292}
2293
2294int ext4fs_mount(unsigned part_length)
2295{
2296 struct ext2_data *data;
2297 int status;
2298 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +00002299 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankara1596432012-05-25 21:21:44 +05302300 if (!data)
2301 return 0;
2302
2303 /* Read the superblock. */
Egbert Eich50ce4c02013-05-01 01:13:19 +00002304 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankara1596432012-05-25 21:21:44 +05302305
2306 if (status == 0)
2307 goto fail;
2308
2309 /* Make sure this is an ext2 filesystem. */
Michael Walle7f101be2016-08-29 10:46:44 +02002310 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Uma Shankara1596432012-05-25 21:21:44 +05302311 goto fail;
2312
Tom Rini6f94ab62016-07-22 17:59:11 -04002313 /*
2314 * The 64bit feature was enabled when metadata_csum was enabled
2315 * and we do not support metadata_csum (and cannot reliably find
2316 * files when it is set. Refuse to mount.
2317 */
Michael Walle58a9ecb2016-09-01 11:21:40 +02002318 if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
Tom Rini6f94ab62016-07-22 17:59:11 -04002319 printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
2320 goto fail;
2321 }
2322
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002323 if (le32_to_cpu(data->sblock.revision_level) == 0) {
Uma Shankara1596432012-05-25 21:21:44 +05302324 fs->inodesz = 128;
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002325 } else {
2326 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2327 __le32_to_cpu(data->sblock.feature_compatibility),
2328 __le32_to_cpu(data->sblock.feature_incompat),
2329 __le32_to_cpu(data->sblock.feature_ro_compat));
Uma Shankara1596432012-05-25 21:21:44 +05302330
Stefan Brünsfc214ef2016-09-17 02:10:07 +02002331 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
2332 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2333 EXT4_FEATURE_INCOMPAT_64BIT ?
2334 le16_to_cpu(data->sblock.descriptor_size) : 32;
2335 }
2336
2337 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2338 le32_to_cpu(data->sblock.revision_level),
2339 fs->inodesz, fs->gdsize);
Uma Shankara1596432012-05-25 21:21:44 +05302340
2341 data->diropen.data = data;
2342 data->diropen.ino = 2;
2343 data->diropen.inode_read = 1;
2344 data->inode = &data->diropen.inode;
2345
2346 status = ext4fs_read_inode(data, 2, data->inode);
2347 if (status == 0)
2348 goto fail;
2349
2350 ext4fs_root = data;
2351
2352 return 1;
2353fail:
2354 printf("Failed to mount ext2 filesystem...\n");
2355 free(data);
2356 ext4fs_root = NULL;
2357
2358 return 0;
2359}