blob: 67e2471bd3885f016a226d435d4cb886581db987 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Uma Shankar71014b62012-05-25 21:21:44 +05302/*
3 * (C) Copyright 2011 - 2012 Samsung Electronics
4 * EXT4 filesystem implementation in Uboot by
5 * Uma Shankar <uma.shankar@samsung.com>
6 * Manjunatha C Achar <a.manjunatha@samsung.com>
7 *
8 * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
9 *
10 * (C) Copyright 2004
11 * esd gmbh <www.esd-electronics.com>
12 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
13 *
14 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
15 * GRUB -- GRand Unified Bootloader
16 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
17 *
Uma Shankara74a99a2012-05-25 21:22:49 +053018 * ext4write : Based on generic ext4 protocol.
Uma Shankar71014b62012-05-25 21:21:44 +053019 */
20
21#include <common.h>
22#include <ext_common.h>
23#include <ext4fs.h>
24#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060025#include <memalign.h>
Uma Shankar71014b62012-05-25 21:21:44 +053026#include <stddef.h>
27#include <linux/stat.h>
28#include <linux/time.h>
29#include <asm/byteorder.h>
30#include "ext4_common.h"
31
32struct ext2_data *ext4fs_root;
33struct ext2fs_node *ext4fs_file;
Michael Walle13179c22016-09-01 11:21:40 +020034__le32 *ext4fs_indir1_block;
Uma Shankar71014b62012-05-25 21:21:44 +053035int ext4fs_indir1_size;
36int ext4fs_indir1_blkno = -1;
Michael Walle13179c22016-09-01 11:21:40 +020037__le32 *ext4fs_indir2_block;
Uma Shankar71014b62012-05-25 21:21:44 +053038int ext4fs_indir2_size;
39int ext4fs_indir2_blkno = -1;
40
Michael Walle13179c22016-09-01 11:21:40 +020041__le32 *ext4fs_indir3_block;
Uma Shankar71014b62012-05-25 21:21:44 +053042int ext4fs_indir3_size;
43int ext4fs_indir3_blkno = -1;
44struct ext2_inode *g_parent_inode;
45static int symlinknest;
46
Stephen Warren4f8662d2012-10-22 06:43:50 +000047#if defined(CONFIG_EXT4_WRITE)
Stefan Brüns387f8662016-09-20 01:12:42 +020048struct ext2_block_group *ext4fs_get_group_descriptor
49 (const struct ext_filesystem *fs, uint32_t bg_idx)
50{
51 return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
52}
53
Michael Walle13179c22016-09-01 11:21:40 +020054static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
55{
56 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
57}
58
59static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
60{
Stefan Brüns05313d12016-09-20 01:13:01 +020061 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
62 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
63 free_blocks--;
64
65 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
66 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
Michael Walle13179c22016-09-01 11:21:40 +020067}
68
Stefan Brüns05313d12016-09-20 01:13:01 +020069static inline void ext4fs_bg_free_inodes_dec
70 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle13179c22016-09-01 11:21:40 +020071{
Stefan Brüns05313d12016-09-20 01:13:01 +020072 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
73 if (fs->gdsize == 64)
74 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
75 free_inodes--;
76
77 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
78 if (fs->gdsize == 64)
79 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
Michael Walle13179c22016-09-01 11:21:40 +020080}
81
Stefan Brüns05313d12016-09-20 01:13:01 +020082static inline void ext4fs_bg_free_blocks_dec
83 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle13179c22016-09-01 11:21:40 +020084{
Stefan Brüns05313d12016-09-20 01:13:01 +020085 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
86 if (fs->gdsize == 64)
87 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
88 free_blocks--;
89
90 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
91 if (fs->gdsize == 64)
92 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
Michael Walle13179c22016-09-01 11:21:40 +020093}
94
Stefan Brüns05313d12016-09-20 01:13:01 +020095static inline void ext4fs_bg_itable_unused_dec
96 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle13179c22016-09-01 11:21:40 +020097{
Stefan Brüns05313d12016-09-20 01:13:01 +020098 uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
99 if (fs->gdsize == 64)
100 free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
101 free_inodes--;
102
103 bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
104 if (fs->gdsize == 64)
105 bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
Michael Walle13179c22016-09-01 11:21:40 +0200106}
107
Stefan Brüns387f8662016-09-20 01:12:42 +0200108uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
109{
110 uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
111 free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
112 return free_blocks;
113}
114
115void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
116{
117 sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
118 sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
119}
120
121uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
122 const struct ext_filesystem *fs)
123{
124 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
125 if (fs->gdsize == 64)
126 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
127 return free_blocks;
128}
129
130static inline
131uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
132 const struct ext_filesystem *fs)
133{
134 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
135 if (fs->gdsize == 64)
136 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
137 return free_inodes;
138}
139
140static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
141{
142 return le16_to_cpu(bg->bg_flags);
143}
144
145static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
146 uint16_t flags)
147{
148 bg->bg_flags = cpu_to_le16(flags);
149}
150
151/* Block number of the block bitmap */
152uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
153 const struct ext_filesystem *fs)
154{
155 uint64_t block_nr = le32_to_cpu(bg->block_id);
156 if (fs->gdsize == 64)
157 block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
158 return block_nr;
159}
160
161/* Block number of the inode bitmap */
162uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
163 const struct ext_filesystem *fs)
164{
165 uint64_t block_nr = le32_to_cpu(bg->inode_id);
166 if (fs->gdsize == 64)
167 block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
168 return block_nr;
169}
170#endif
171
172/* Block number of the inode table */
173uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
174 const struct ext_filesystem *fs)
175{
176 uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
177 if (fs->gdsize == 64)
178 block_nr +=
179 (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
180 return block_nr;
181}
182
183#if defined(CONFIG_EXT4_WRITE)
Uma Shankara74a99a2012-05-25 21:22:49 +0530184uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
185{
186 uint32_t res = size / n;
187 if (res * n != size)
188 res++;
189
190 return res;
191}
192
193void put_ext4(uint64_t off, void *buf, uint32_t size)
194{
195 uint64_t startblock;
196 uint64_t remainder;
197 unsigned char *temp_ptr = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +0530198 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-05-01 01:13:19 +0000199 int log2blksz = fs->dev_desc->log2blksz;
200 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
Uma Shankara74a99a2012-05-25 21:22:49 +0530201
Egbert Eich7b1b2552013-05-01 01:13:19 +0000202 startblock = off >> log2blksz;
Uma Shankara74a99a2012-05-25 21:22:49 +0530203 startblock += part_offset;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000204 remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
Uma Shankara74a99a2012-05-25 21:22:49 +0530205
206 if (fs->dev_desc == NULL)
207 return;
208
Egbert Eich7b1b2552013-05-01 01:13:19 +0000209 if ((startblock + (size >> log2blksz)) >
Uma Shankara74a99a2012-05-25 21:22:49 +0530210 (part_offset + fs->total_sect)) {
Frederic Leroye7ee0282013-06-26 18:11:25 +0200211 printf("part_offset is " LBAFU "\n", part_offset);
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900212 printf("total_sector is %llu\n", fs->total_sect);
Uma Shankara74a99a2012-05-25 21:22:49 +0530213 printf("error: overflow occurs\n");
214 return;
215 }
216
217 if (remainder) {
Simon Glass2ee8ada2016-02-29 15:25:52 -0700218 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
219 temp_ptr = sec_buf;
220 memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
221 blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankara74a99a2012-05-25 21:22:49 +0530222 } else {
Egbert Eich7b1b2552013-05-01 01:13:19 +0000223 if (size >> log2blksz != 0) {
Simon Glass2ee8ada2016-02-29 15:25:52 -0700224 blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
225 (unsigned long *)buf);
Uma Shankara74a99a2012-05-25 21:22:49 +0530226 } else {
Simon Glass2ee8ada2016-02-29 15:25:52 -0700227 blk_dread(fs->dev_desc, startblock, 1, sec_buf);
Uma Shankara74a99a2012-05-25 21:22:49 +0530228 temp_ptr = sec_buf;
229 memcpy(temp_ptr, buf, size);
Simon Glass2ee8ada2016-02-29 15:25:52 -0700230 blk_dwrite(fs->dev_desc, startblock, 1,
231 (unsigned long *)sec_buf);
Uma Shankara74a99a2012-05-25 21:22:49 +0530232 }
233 }
234}
235
236static int _get_new_inode_no(unsigned char *buffer)
237{
238 struct ext_filesystem *fs = get_fs();
239 unsigned char input;
240 int operand, status;
241 int count = 1;
242 int j = 0;
243
244 /* get the blocksize of the filesystem */
245 unsigned char *ptr = buffer;
246 while (*ptr == 255) {
247 ptr++;
248 count += 8;
Michael Walle13179c22016-09-01 11:21:40 +0200249 if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
Uma Shankara74a99a2012-05-25 21:22:49 +0530250 return -1;
251 }
252
253 for (j = 0; j < fs->blksz; j++) {
254 input = *ptr;
255 int i = 0;
256 while (i <= 7) {
257 operand = 1 << i;
258 status = input & operand;
259 if (status) {
260 i++;
261 count++;
262 } else {
263 *ptr |= operand;
264 return count;
265 }
266 }
267 ptr = ptr + 1;
268 }
269
270 return -1;
271}
272
273static int _get_new_blk_no(unsigned char *buffer)
274{
Stefan Brüns63031912016-09-06 04:36:50 +0200275 int operand;
Uma Shankara74a99a2012-05-25 21:22:49 +0530276 int count = 0;
Stefan Brüns63031912016-09-06 04:36:50 +0200277 int i;
Uma Shankara74a99a2012-05-25 21:22:49 +0530278 unsigned char *ptr = buffer;
279 struct ext_filesystem *fs = get_fs();
280
Uma Shankara74a99a2012-05-25 21:22:49 +0530281 while (*ptr == 255) {
282 ptr++;
283 count += 8;
284 if (count == (fs->blksz * 8))
285 return -1;
286 }
287
Stefan Brüns63031912016-09-06 04:36:50 +0200288 if (fs->blksz == 1024)
289 count += 1;
290
291 for (i = 0; i <= 7; i++) {
292 operand = 1 << i;
293 if (*ptr & operand) {
294 count++;
295 } else {
296 *ptr |= operand;
297 return count;
Uma Shankara74a99a2012-05-25 21:22:49 +0530298 }
Uma Shankara74a99a2012-05-25 21:22:49 +0530299 }
300
301 return -1;
302}
303
304int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
305{
306 int i, remainder, status;
307 unsigned char *ptr = buffer;
308 unsigned char operand;
309 i = blockno / 8;
310 remainder = blockno % 8;
311 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
312
313 i = i - (index * blocksize);
314 if (blocksize != 1024) {
315 ptr = ptr + i;
316 operand = 1 << remainder;
317 status = *ptr & operand;
318 if (status)
319 return -1;
320
321 *ptr = *ptr | operand;
322 return 0;
323 } else {
324 if (remainder == 0) {
325 ptr = ptr + i - 1;
326 operand = (1 << 7);
327 } else {
328 ptr = ptr + i;
329 operand = (1 << (remainder - 1));
330 }
331 status = *ptr & operand;
332 if (status)
333 return -1;
334
335 *ptr = *ptr | operand;
336 return 0;
337 }
338}
339
340void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
341{
342 int i, remainder, status;
343 unsigned char *ptr = buffer;
344 unsigned char operand;
345 i = blockno / 8;
346 remainder = blockno % 8;
347 int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
348
349 i = i - (index * blocksize);
350 if (blocksize != 1024) {
351 ptr = ptr + i;
352 operand = (1 << remainder);
353 status = *ptr & operand;
354 if (status)
355 *ptr = *ptr & ~(operand);
356 } else {
357 if (remainder == 0) {
358 ptr = ptr + i - 1;
359 operand = (1 << 7);
360 } else {
361 ptr = ptr + i;
362 operand = (1 << (remainder - 1));
363 }
364 status = *ptr & operand;
365 if (status)
366 *ptr = *ptr & ~(operand);
367 }
368}
369
370int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
371{
372 int i, remainder, status;
373 unsigned char *ptr = buffer;
374 unsigned char operand;
375
Michael Walle13179c22016-09-01 11:21:40 +0200376 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankara74a99a2012-05-25 21:22:49 +0530377 i = inode_no / 8;
378 remainder = inode_no % 8;
379 if (remainder == 0) {
380 ptr = ptr + i - 1;
381 operand = (1 << 7);
382 } else {
383 ptr = ptr + i;
384 operand = (1 << (remainder - 1));
385 }
386 status = *ptr & operand;
387 if (status)
388 return -1;
389
390 *ptr = *ptr | operand;
391
392 return 0;
393}
394
395void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
396{
397 int i, remainder, status;
398 unsigned char *ptr = buffer;
399 unsigned char operand;
400
Michael Walle13179c22016-09-01 11:21:40 +0200401 inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
Uma Shankara74a99a2012-05-25 21:22:49 +0530402 i = inode_no / 8;
403 remainder = inode_no % 8;
404 if (remainder == 0) {
405 ptr = ptr + i - 1;
406 operand = (1 << 7);
407 } else {
408 ptr = ptr + i;
409 operand = (1 << (remainder - 1));
410 }
411 status = *ptr & operand;
412 if (status)
413 *ptr = *ptr & ~(operand);
414}
415
Michael Walle13179c22016-09-01 11:21:40 +0200416uint16_t ext4fs_checksum_update(uint32_t i)
Uma Shankara74a99a2012-05-25 21:22:49 +0530417{
418 struct ext2_block_group *desc;
419 struct ext_filesystem *fs = get_fs();
Michael Walle13179c22016-09-01 11:21:40 +0200420 uint16_t crc = 0;
421 __le32 le32_i = cpu_to_le32(i);
Uma Shankara74a99a2012-05-25 21:22:49 +0530422
Stefan Brünsbad73812016-09-17 02:10:10 +0200423 desc = ext4fs_get_group_descriptor(fs, i);
Michael Walle13179c22016-09-01 11:21:40 +0200424 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530425 int offset = offsetof(struct ext2_block_group, bg_checksum);
426
427 crc = ext2fs_crc16(~0, fs->sb->unique_id,
428 sizeof(fs->sb->unique_id));
Michael Walle13179c22016-09-01 11:21:40 +0200429 crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
Uma Shankara74a99a2012-05-25 21:22:49 +0530430 crc = ext2fs_crc16(crc, desc, offset);
431 offset += sizeof(desc->bg_checksum); /* skip checksum */
432 assert(offset == sizeof(*desc));
Tuomas Tynkkynen48fbf252017-09-25 22:06:31 +0300433 if (offset < fs->gdsize) {
434 crc = ext2fs_crc16(crc, (__u8 *)desc + offset,
435 fs->gdsize - offset);
436 }
Uma Shankara74a99a2012-05-25 21:22:49 +0530437 }
438
439 return crc;
440}
441
442static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
443{
444 int dentry_length;
445 int sizeof_void_space;
446 int new_entry_byte_reqd;
447 short padding_factor = 0;
448
449 if (dir->namelen % 4 != 0)
450 padding_factor = 4 - (dir->namelen % 4);
451
452 dentry_length = sizeof(struct ext2_dirent) +
453 dir->namelen + padding_factor;
Michael Walle13179c22016-09-01 11:21:40 +0200454 sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
Uma Shankara74a99a2012-05-25 21:22:49 +0530455 if (sizeof_void_space == 0)
456 return 0;
457
458 padding_factor = 0;
459 if (strlen(filename) % 4 != 0)
460 padding_factor = 4 - (strlen(filename) % 4);
461
462 new_entry_byte_reqd = strlen(filename) +
463 sizeof(struct ext2_dirent) + padding_factor;
464 if (sizeof_void_space >= new_entry_byte_reqd) {
Michael Walle13179c22016-09-01 11:21:40 +0200465 dir->direntlen = cpu_to_le16(dentry_length);
Uma Shankara74a99a2012-05-25 21:22:49 +0530466 return sizeof_void_space;
467 }
468
469 return 0;
470}
471
Stefan Brünsc1020682016-09-06 04:36:42 +0200472int ext4fs_update_parent_dentry(char *filename, int file_type)
Uma Shankara74a99a2012-05-25 21:22:49 +0530473{
474 unsigned int *zero_buffer = NULL;
475 char *root_first_block_buffer = NULL;
Stefan Brünsf5772812016-09-06 04:36:44 +0200476 int blk_idx;
Uma Shankara74a99a2012-05-25 21:22:49 +0530477 long int first_block_no_of_root = 0;
Uma Shankara74a99a2012-05-25 21:22:49 +0530478 int totalbytes = 0;
Uma Shankara74a99a2012-05-25 21:22:49 +0530479 unsigned int new_entry_byte_reqd;
Uma Shankara74a99a2012-05-25 21:22:49 +0530480 int sizeof_void_space = 0;
481 int templength = 0;
Stefan Brünsc1020682016-09-06 04:36:42 +0200482 int inodeno = -1;
Uma Shankara74a99a2012-05-25 21:22:49 +0530483 int status;
484 struct ext_filesystem *fs = get_fs();
485 /* directory entry */
486 struct ext2_dirent *dir;
Uma Shankara74a99a2012-05-25 21:22:49 +0530487 char *temp_dir = NULL;
Michael Walle13179c22016-09-01 11:21:40 +0200488 uint32_t new_blk_no;
489 uint32_t new_size;
490 uint32_t new_blockcnt;
Stefan Brünsf5772812016-09-06 04:36:44 +0200491 uint32_t directory_blocks;
Uma Shankara74a99a2012-05-25 21:22:49 +0530492
493 zero_buffer = zalloc(fs->blksz);
494 if (!zero_buffer) {
495 printf("No Memory\n");
Stefan Brünsc1020682016-09-06 04:36:42 +0200496 return -1;
Uma Shankara74a99a2012-05-25 21:22:49 +0530497 }
498 root_first_block_buffer = zalloc(fs->blksz);
499 if (!root_first_block_buffer) {
500 free(zero_buffer);
501 printf("No Memory\n");
Stefan Brünsc1020682016-09-06 04:36:42 +0200502 return -1;
Uma Shankara74a99a2012-05-25 21:22:49 +0530503 }
Stefan Brünsf5772812016-09-06 04:36:44 +0200504 new_entry_byte_reqd = ROUND(strlen(filename) +
505 sizeof(struct ext2_dirent), 4);
Uma Shankara74a99a2012-05-25 21:22:49 +0530506restart:
Stefan Brünsf5772812016-09-06 04:36:44 +0200507 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
508 LOG2_BLOCK_SIZE(ext4fs_root);
509 blk_idx = directory_blocks - 1;
Uma Shankara74a99a2012-05-25 21:22:49 +0530510
Stefan Brünsf5772812016-09-06 04:36:44 +0200511restart_read:
Uma Shankara74a99a2012-05-25 21:22:49 +0530512 /* read the block no allocated to a file */
Stefan Brünsf5772812016-09-06 04:36:44 +0200513 first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
514 if (first_block_no_of_root <= 0)
515 goto fail;
Uma Shankara74a99a2012-05-25 21:22:49 +0530516
Frederic Leroye7ee0282013-06-26 18:11:25 +0200517 status = ext4fs_devread((lbaint_t)first_block_no_of_root
Uma Shankara74a99a2012-05-25 21:22:49 +0530518 * fs->sect_perblk,
519 0, fs->blksz, root_first_block_buffer);
520 if (status == 0)
521 goto fail;
522
523 if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
524 goto fail;
525 dir = (struct ext2_dirent *)root_first_block_buffer;
Uma Shankara74a99a2012-05-25 21:22:49 +0530526 totalbytes = 0;
Stefan Brünsf5772812016-09-06 04:36:44 +0200527
Michael Walle13179c22016-09-01 11:21:40 +0200528 while (le16_to_cpu(dir->direntlen) > 0) {
Stefan Brünsf5772812016-09-06 04:36:44 +0200529 unsigned short used_len = ROUND(dir->namelen +
530 sizeof(struct ext2_dirent), 4);
Uma Shankara74a99a2012-05-25 21:22:49 +0530531
Stefan Brünsf5772812016-09-06 04:36:44 +0200532 /* last entry of block */
Michael Walle13179c22016-09-01 11:21:40 +0200533 if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530534
Stefan Brünsf5772812016-09-06 04:36:44 +0200535 /* check if new entry fits */
536 if ((used_len + new_entry_byte_reqd) <=
537 le16_to_cpu(dir->direntlen)) {
538 dir->direntlen = cpu_to_le16(used_len);
539 break;
540 } else {
541 if (blk_idx > 0) {
542 printf("Block full, trying previous\n");
543 blk_idx--;
544 goto restart_read;
545 }
546 printf("All blocks full: Allocate new\n");
Uma Shankara74a99a2012-05-25 21:22:49 +0530547
Stefan Brüns2bef6892016-09-06 04:36:43 +0200548 if (le32_to_cpu(g_parent_inode->flags) &
549 EXT4_EXTENTS_FL) {
550 printf("Directory uses extents\n");
551 goto fail;
552 }
Stefan Brünsf5772812016-09-06 04:36:44 +0200553 if (directory_blocks >= INDIRECT_BLOCKS) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530554 printf("Directory exceeds limit\n");
555 goto fail;
556 }
Michael Walle13179c22016-09-01 11:21:40 +0200557 new_blk_no = ext4fs_get_new_blk_no();
558 if (new_blk_no == -1) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530559 printf("no block left to assign\n");
560 goto fail;
561 }
Michael Walle13179c22016-09-01 11:21:40 +0200562 put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
Stefan Brünsf5772812016-09-06 04:36:44 +0200563 g_parent_inode->b.blocks.
564 dir_blocks[directory_blocks] =
Michael Walle13179c22016-09-01 11:21:40 +0200565 cpu_to_le32(new_blk_no);
566
567 new_size = le32_to_cpu(g_parent_inode->size);
568 new_size += fs->blksz;
569 g_parent_inode->size = cpu_to_le32(new_size);
570
571 new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
572 new_blockcnt += fs->sect_perblk;
573 g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
574
Uma Shankara74a99a2012-05-25 21:22:49 +0530575 if (ext4fs_put_metadata
576 (root_first_block_buffer,
577 first_block_no_of_root))
578 goto fail;
579 goto restart;
580 }
Uma Shankara74a99a2012-05-25 21:22:49 +0530581 }
582
Michael Walle13179c22016-09-01 11:21:40 +0200583 templength = le16_to_cpu(dir->direntlen);
Uma Shankara74a99a2012-05-25 21:22:49 +0530584 totalbytes = totalbytes + templength;
585 sizeof_void_space = check_void_in_dentry(dir, filename);
586 if (sizeof_void_space)
587 break;
588
589 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankara74a99a2012-05-25 21:22:49 +0530590 }
591
592 /* make a pointer ready for creating next directory entry */
Michael Walle13179c22016-09-01 11:21:40 +0200593 templength = le16_to_cpu(dir->direntlen);
Uma Shankara74a99a2012-05-25 21:22:49 +0530594 totalbytes = totalbytes + templength;
595 dir = (struct ext2_dirent *)((char *)dir + templength);
Uma Shankara74a99a2012-05-25 21:22:49 +0530596
597 /* get the next available inode number */
598 inodeno = ext4fs_get_new_inode_no();
599 if (inodeno == -1) {
600 printf("no inode left to assign\n");
601 goto fail;
602 }
Michael Walle13179c22016-09-01 11:21:40 +0200603 dir->inode = cpu_to_le32(inodeno);
Uma Shankara74a99a2012-05-25 21:22:49 +0530604 if (sizeof_void_space)
Michael Walle13179c22016-09-01 11:21:40 +0200605 dir->direntlen = cpu_to_le16(sizeof_void_space);
Uma Shankara74a99a2012-05-25 21:22:49 +0530606 else
Michael Walle13179c22016-09-01 11:21:40 +0200607 dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
Uma Shankara74a99a2012-05-25 21:22:49 +0530608
609 dir->namelen = strlen(filename);
610 dir->filetype = FILETYPE_REG; /* regular file */
611 temp_dir = (char *)dir;
612 temp_dir = temp_dir + sizeof(struct ext2_dirent);
613 memcpy(temp_dir, filename, strlen(filename));
614
Uma Shankara74a99a2012-05-25 21:22:49 +0530615 /* update or write the 1st block of root inode */
616 if (ext4fs_put_metadata(root_first_block_buffer,
617 first_block_no_of_root))
618 goto fail;
619
620fail:
621 free(zero_buffer);
622 free(root_first_block_buffer);
Stefan Brünsc1020682016-09-06 04:36:42 +0200623
624 return inodeno;
Uma Shankara74a99a2012-05-25 21:22:49 +0530625}
626
627static int search_dir(struct ext2_inode *parent_inode, char *dirname)
628{
629 int status;
Stefan Brüns278f5d32016-09-06 04:36:41 +0200630 int inodeno = 0;
Stefan Brüns91397152016-09-06 04:36:46 +0200631 int offset;
632 int blk_idx;
Uma Shankara74a99a2012-05-25 21:22:49 +0530633 long int blknr;
Stefan Brüns91397152016-09-06 04:36:46 +0200634 char *block_buffer = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +0530635 struct ext2_dirent *dir = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +0530636 struct ext_filesystem *fs = get_fs();
Stefan Brüns91397152016-09-06 04:36:46 +0200637 uint32_t directory_blocks;
638 char *direntname;
Uma Shankara74a99a2012-05-25 21:22:49 +0530639
Stefan Brüns91397152016-09-06 04:36:46 +0200640 directory_blocks = le32_to_cpu(parent_inode->size) >>
641 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankara74a99a2012-05-25 21:22:49 +0530642
Stefan Brüns91397152016-09-06 04:36:46 +0200643 block_buffer = zalloc(fs->blksz);
644 if (!block_buffer)
645 goto fail;
646
647 /* get the block no allocated to a file */
648 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
649 blknr = read_allocated_block(parent_inode, blk_idx);
Stefan Brüns74674ed2016-09-06 04:36:55 +0200650 if (blknr <= 0)
Uma Shankara74a99a2012-05-25 21:22:49 +0530651 goto fail;
652
Stefan Brüns91397152016-09-06 04:36:46 +0200653 /* read the directory block */
Frederic Leroye7ee0282013-06-26 18:11:25 +0200654 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
Uma Shankara74a99a2012-05-25 21:22:49 +0530655 0, fs->blksz, (char *)block_buffer);
656 if (status == 0)
657 goto fail;
658
Stefan Brüns91397152016-09-06 04:36:46 +0200659 offset = 0;
660 do {
Ian Rayc45fcd52017-11-08 15:35:10 +0000661 if (offset & 3) {
662 printf("Badly aligned ext2_dirent\n");
663 break;
664 }
665
Stefan Brüns91397152016-09-06 04:36:46 +0200666 dir = (struct ext2_dirent *)(block_buffer + offset);
667 direntname = (char*)(dir) + sizeof(struct ext2_dirent);
Uma Shankara74a99a2012-05-25 21:22:49 +0530668
Stefan Brüns91397152016-09-06 04:36:46 +0200669 int direntlen = le16_to_cpu(dir->direntlen);
670 if (direntlen < sizeof(struct ext2_dirent))
Uma Shankara74a99a2012-05-25 21:22:49 +0530671 break;
672
Stefan Brüns91397152016-09-06 04:36:46 +0200673 if (dir->inode && (strlen(dirname) == dir->namelen) &&
674 (strncmp(dirname, direntname, dir->namelen) == 0)) {
675 inodeno = le32_to_cpu(dir->inode);
676 break;
677 }
678
679 offset += direntlen;
Uma Shankara74a99a2012-05-25 21:22:49 +0530680
Stefan Brüns91397152016-09-06 04:36:46 +0200681 } while (offset < fs->blksz);
Stefan Brüns278f5d32016-09-06 04:36:41 +0200682
Stefan Brüns91397152016-09-06 04:36:46 +0200683 if (inodeno > 0) {
684 free(block_buffer);
Stefan Brüns278f5d32016-09-06 04:36:41 +0200685 return inodeno;
Stefan Brüns91397152016-09-06 04:36:46 +0200686 }
Uma Shankara74a99a2012-05-25 21:22:49 +0530687 }
688
689fail:
690 free(block_buffer);
691
692 return -1;
693}
694
695static int find_dir_depth(char *dirname)
696{
697 char *token = strtok(dirname, "/");
698 int count = 0;
699 while (token != NULL) {
700 token = strtok(NULL, "/");
701 count++;
702 }
703 return count + 1 + 1;
704 /*
705 * for example for string /home/temp
706 * depth=home(1)+temp(1)+1 extra for NULL;
707 * so count is 4;
708 */
709}
710
711static int parse_path(char **arr, char *dirname)
712{
713 char *token = strtok(dirname, "/");
714 int i = 0;
715
716 /* add root */
717 arr[i] = zalloc(strlen("/") + 1);
718 if (!arr[i])
719 return -ENOMEM;
Stephen Warren5dd30f72015-09-04 22:03:44 -0600720 memcpy(arr[i++], "/", strlen("/"));
Uma Shankara74a99a2012-05-25 21:22:49 +0530721
722 /* add each path entry after root */
723 while (token != NULL) {
724 arr[i] = zalloc(strlen(token) + 1);
725 if (!arr[i])
726 return -ENOMEM;
727 memcpy(arr[i++], token, strlen(token));
728 token = strtok(NULL, "/");
729 }
730 arr[i] = NULL;
731
732 return 0;
733}
734
735int ext4fs_iget(int inode_no, struct ext2_inode *inode)
736{
737 if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
738 return -1;
739
740 return 0;
741}
742
743/*
744 * Function: ext4fs_get_parent_inode_num
745 * Return Value: inode Number of the parent directory of file/Directory to be
746 * created
747 * dirname : Input parmater, input path name of the file/directory to be created
748 * dname : Output parameter, to be filled with the name of the directory
749 * extracted from dirname
750 */
751int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
752{
753 int i;
754 int depth = 0;
755 int matched_inode_no;
756 int result_inode_no = -1;
757 char **ptr = NULL;
758 char *depth_dirname = NULL;
759 char *parse_dirname = NULL;
760 struct ext2_inode *parent_inode = NULL;
761 struct ext2_inode *first_inode = NULL;
762 struct ext2_inode temp_inode;
763
764 if (*dirname != '/') {
765 printf("Please supply Absolute path\n");
766 return -1;
767 }
768
769 /* TODO: input validation make equivalent to linux */
770 depth_dirname = zalloc(strlen(dirname) + 1);
771 if (!depth_dirname)
772 return -ENOMEM;
773
774 memcpy(depth_dirname, dirname, strlen(dirname));
775 depth = find_dir_depth(depth_dirname);
776 parse_dirname = zalloc(strlen(dirname) + 1);
777 if (!parse_dirname)
778 goto fail;
779 memcpy(parse_dirname, dirname, strlen(dirname));
780
781 /* allocate memory for each directory level */
782 ptr = zalloc((depth) * sizeof(char *));
783 if (!ptr)
784 goto fail;
785 if (parse_path(ptr, parse_dirname))
786 goto fail;
787 parent_inode = zalloc(sizeof(struct ext2_inode));
788 if (!parent_inode)
789 goto fail;
790 first_inode = zalloc(sizeof(struct ext2_inode));
791 if (!first_inode)
792 goto fail;
793 memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
794 memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
795 if (flags & F_FILE)
796 result_inode_no = EXT2_ROOT_INO;
797 for (i = 1; i < depth; i++) {
798 matched_inode_no = search_dir(parent_inode, ptr[i]);
799 if (matched_inode_no == -1) {
800 if (ptr[i + 1] == NULL && i == 1) {
801 result_inode_no = EXT2_ROOT_INO;
802 goto end;
803 } else {
804 if (ptr[i + 1] == NULL)
805 break;
806 printf("Invalid path\n");
807 result_inode_no = -1;
808 goto fail;
809 }
810 } else {
811 if (ptr[i + 1] != NULL) {
812 memset(parent_inode, '\0',
813 sizeof(struct ext2_inode));
814 if (ext4fs_iget(matched_inode_no,
815 parent_inode)) {
816 result_inode_no = -1;
817 goto fail;
818 }
819 result_inode_no = matched_inode_no;
820 } else {
821 break;
822 }
823 }
824 }
825
826end:
827 if (i == 1)
828 matched_inode_no = search_dir(first_inode, ptr[i]);
829 else
830 matched_inode_no = search_dir(parent_inode, ptr[i]);
831
832 if (matched_inode_no != -1) {
833 ext4fs_iget(matched_inode_no, &temp_inode);
Michael Walle13179c22016-09-01 11:21:40 +0200834 if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530835 printf("It is a Directory\n");
836 result_inode_no = -1;
837 goto fail;
838 }
839 }
840
841 if (strlen(ptr[i]) > 256) {
842 result_inode_no = -1;
843 goto fail;
844 }
845 memcpy(dname, ptr[i], strlen(ptr[i]));
846
847fail:
848 free(depth_dirname);
849 free(parse_dirname);
Stephen Warren5dd30f72015-09-04 22:03:44 -0600850 for (i = 0; i < depth; i++) {
851 if (!ptr[i])
852 break;
853 free(ptr[i]);
854 }
Uma Shankara74a99a2012-05-25 21:22:49 +0530855 free(ptr);
856 free(parent_inode);
857 free(first_inode);
858
859 return result_inode_no;
860}
861
Stefan Brüns278f5d32016-09-06 04:36:41 +0200862static int unlink_filename(char *filename, unsigned int blknr)
Uma Shankara74a99a2012-05-25 21:22:49 +0530863{
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200864 int status;
865 int inodeno = 0;
Stefan Brüns59283ff2016-10-09 20:15:26 +0200866 int offset;
867 char *block_buffer = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +0530868 struct ext2_dirent *dir = NULL;
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200869 struct ext2_dirent *previous_dir;
Uma Shankara74a99a2012-05-25 21:22:49 +0530870 struct ext_filesystem *fs = get_fs();
Stephen Warren991201e2015-09-04 22:03:45 -0600871 int ret = -1;
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200872 char *direntname;
Uma Shankara74a99a2012-05-25 21:22:49 +0530873
Stefan Brüns59283ff2016-10-09 20:15:26 +0200874 block_buffer = zalloc(fs->blksz);
875 if (!block_buffer)
Uma Shankara74a99a2012-05-25 21:22:49 +0530876 return -ENOMEM;
Stefan Brüns59283ff2016-10-09 20:15:26 +0200877
878 /* read the directory block */
Stefan Brüns278f5d32016-09-06 04:36:41 +0200879 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
Stefan Brüns59283ff2016-10-09 20:15:26 +0200880 fs->blksz, block_buffer);
Uma Shankara74a99a2012-05-25 21:22:49 +0530881 if (status == 0)
882 goto fail;
883
Stefan Brüns59283ff2016-10-09 20:15:26 +0200884 offset = 0;
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200885 do {
Ian Rayc45fcd52017-11-08 15:35:10 +0000886 if (offset & 3) {
887 printf("Badly aligned ext2_dirent\n");
888 break;
889 }
890
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200891 previous_dir = dir;
892 dir = (struct ext2_dirent *)(block_buffer + offset);
893 direntname = (char *)(dir) + sizeof(struct ext2_dirent);
894
895 int direntlen = le16_to_cpu(dir->direntlen);
896 if (direntlen < sizeof(struct ext2_dirent))
897 break;
898
Stefan Brüns278f5d32016-09-06 04:36:41 +0200899 if (dir->inode && (strlen(filename) == dir->namelen) &&
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200900 (strncmp(direntname, filename, dir->namelen) == 0)) {
Stefan Brüns278f5d32016-09-06 04:36:41 +0200901 inodeno = le32_to_cpu(dir->inode);
Stefan Brüns278f5d32016-09-06 04:36:41 +0200902 break;
Uma Shankara74a99a2012-05-25 21:22:49 +0530903 }
904
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200905 offset += direntlen;
Uma Shankara74a99a2012-05-25 21:22:49 +0530906
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200907 } while (offset < fs->blksz);
Uma Shankara74a99a2012-05-25 21:22:49 +0530908
Stefan Brüns4c491fe2016-10-09 20:15:27 +0200909 if (inodeno > 0) {
Stefan Brüns7fbe42e2016-10-09 20:15:28 +0200910 printf("file found, deleting\n");
911 if (ext4fs_log_journal(block_buffer, blknr))
912 goto fail;
Uma Shankara74a99a2012-05-25 21:22:49 +0530913
Stefan Brüns7fbe42e2016-10-09 20:15:28 +0200914 if (previous_dir) {
915 /* merge dir entry with predecessor */
916 uint16_t new_len;
917 new_len = le16_to_cpu(previous_dir->direntlen);
918 new_len += le16_to_cpu(dir->direntlen);
919 previous_dir->direntlen = cpu_to_le16(new_len);
920 } else {
921 /* invalidate dir entry */
922 dir->inode = 0;
923 }
Stefan Brüns59283ff2016-10-09 20:15:26 +0200924 if (ext4fs_put_metadata(block_buffer, blknr))
Uma Shankara74a99a2012-05-25 21:22:49 +0530925 goto fail;
Stephen Warren991201e2015-09-04 22:03:45 -0600926 ret = inodeno;
Uma Shankara74a99a2012-05-25 21:22:49 +0530927 }
928fail:
Stefan Brüns59283ff2016-10-09 20:15:26 +0200929 free(block_buffer);
Uma Shankara74a99a2012-05-25 21:22:49 +0530930
Stephen Warren991201e2015-09-04 22:03:45 -0600931 return ret;
Uma Shankara74a99a2012-05-25 21:22:49 +0530932}
933
Stefan Brüns278f5d32016-09-06 04:36:41 +0200934int ext4fs_filename_unlink(char *filename)
Uma Shankara74a99a2012-05-25 21:22:49 +0530935{
Stefan Brüns91397152016-09-06 04:36:46 +0200936 int blk_idx;
Uma Shankara74a99a2012-05-25 21:22:49 +0530937 long int blknr = -1;
938 int inodeno = -1;
Stefan Brüns91397152016-09-06 04:36:46 +0200939 uint32_t directory_blocks;
940
941 directory_blocks = le32_to_cpu(g_parent_inode->size) >>
942 LOG2_BLOCK_SIZE(ext4fs_root);
Uma Shankara74a99a2012-05-25 21:22:49 +0530943
944 /* read the block no allocated to a file */
Stefan Brüns91397152016-09-06 04:36:46 +0200945 for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
946 blknr = read_allocated_block(g_parent_inode, blk_idx);
Stefan Brüns74674ed2016-09-06 04:36:55 +0200947 if (blknr <= 0)
Uma Shankara74a99a2012-05-25 21:22:49 +0530948 break;
Stefan Brüns278f5d32016-09-06 04:36:41 +0200949 inodeno = unlink_filename(filename, blknr);
Uma Shankara74a99a2012-05-25 21:22:49 +0530950 if (inodeno != -1)
951 return inodeno;
952 }
953
954 return -1;
955}
956
Michael Walle13179c22016-09-01 11:21:40 +0200957uint32_t ext4fs_get_new_blk_no(void)
Uma Shankara74a99a2012-05-25 21:22:49 +0530958{
959 short i;
960 short status;
961 int remainder;
962 unsigned int bg_idx;
963 static int prev_bg_bitmap_index = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200964 unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Uma Shankara74a99a2012-05-25 21:22:49 +0530965 struct ext_filesystem *fs = get_fs();
966 char *journal_buffer = zalloc(fs->blksz);
967 char *zero_buffer = zalloc(fs->blksz);
968 if (!journal_buffer || !zero_buffer)
969 goto fail;
Uma Shankara74a99a2012-05-25 21:22:49 +0530970
971 if (fs->first_pass_bbmap == 0) {
972 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200973 struct ext2_block_group *bgd = NULL;
974 bgd = ext4fs_get_group_descriptor(fs, i);
975 if (ext4fs_bg_get_free_blocks(bgd, fs)) {
976 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
977 uint64_t b_bitmap_blk =
978 ext4fs_bg_get_block_id(bgd, fs);
979 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankara74a99a2012-05-25 21:22:49 +0530980 memcpy(fs->blk_bmaps[i], zero_buffer,
981 fs->blksz);
Stefan Brünsbad73812016-09-17 02:10:10 +0200982 put_ext4(b_bitmap_blk * fs->blksz,
983 fs->blk_bmaps[i], fs->blksz);
984 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
985 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankara74a99a2012-05-25 21:22:49 +0530986 }
987 fs->curr_blkno =
988 _get_new_blk_no(fs->blk_bmaps[i]);
989 if (fs->curr_blkno == -1)
Stefan Brünsbad73812016-09-17 02:10:10 +0200990 /* block bitmap is completely filled */
Uma Shankara74a99a2012-05-25 21:22:49 +0530991 continue;
992 fs->curr_blkno = fs->curr_blkno +
993 (i * fs->blksz * 8);
994 fs->first_pass_bbmap++;
Stefan Brüns05313d12016-09-20 01:13:01 +0200995 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200996 ext4fs_sb_free_blocks_dec(fs->sb);
Stefan Brünsbad73812016-09-17 02:10:10 +0200997 status = ext4fs_devread(b_bitmap_blk *
998 fs->sect_perblk,
999 0, fs->blksz,
Uma Shankara74a99a2012-05-25 21:22:49 +05301000 journal_buffer);
1001 if (status == 0)
1002 goto fail;
1003 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +02001004 b_bitmap_blk))
Uma Shankara74a99a2012-05-25 21:22:49 +05301005 goto fail;
1006 goto success;
1007 } else {
1008 debug("no space left on block group %d\n", i);
1009 }
1010 }
1011
1012 goto fail;
1013 } else {
Uma Shankara74a99a2012-05-25 21:22:49 +05301014 fs->curr_blkno++;
Stefan Brüns693f7912016-09-06 04:36:49 +02001015restart:
Uma Shankara74a99a2012-05-25 21:22:49 +05301016 /* get the blockbitmap index respective to blockno */
Łukasz Majewski5767dc82014-05-06 09:36:04 +02001017 bg_idx = fs->curr_blkno / blk_per_grp;
1018 if (fs->blksz == 1024) {
Uma Shankara74a99a2012-05-25 21:22:49 +05301019 remainder = fs->curr_blkno % blk_per_grp;
1020 if (!remainder)
1021 bg_idx--;
1022 }
1023
1024 /*
1025 * To skip completely filled block group bitmaps
1026 * Optimize the block allocation
1027 */
1028 if (bg_idx >= fs->no_blkgrp)
1029 goto fail;
1030
Stefan Brünsbad73812016-09-17 02:10:10 +02001031 struct ext2_block_group *bgd = NULL;
1032 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
1033 if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
Uma Shankara74a99a2012-05-25 21:22:49 +05301034 debug("block group %u is full. Skipping\n", bg_idx);
Stefan Brüns693f7912016-09-06 04:36:49 +02001035 fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
1036 if (fs->blksz == 1024)
1037 fs->curr_blkno += 1;
Uma Shankara74a99a2012-05-25 21:22:49 +05301038 goto restart;
1039 }
1040
Stefan Brünsbad73812016-09-17 02:10:10 +02001041 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1042 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
1043 if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
Uma Shankara74a99a2012-05-25 21:22:49 +05301044 memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
Stefan Brünsbad73812016-09-17 02:10:10 +02001045 put_ext4(b_bitmap_blk * fs->blksz,
1046 zero_buffer, fs->blksz);
1047 bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
1048 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankara74a99a2012-05-25 21:22:49 +05301049 }
1050
1051 if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
1052 bg_idx) != 0) {
1053 debug("going for restart for the block no %ld %u\n",
1054 fs->curr_blkno, bg_idx);
Stefan Brüns693f7912016-09-06 04:36:49 +02001055 fs->curr_blkno++;
Uma Shankara74a99a2012-05-25 21:22:49 +05301056 goto restart;
1057 }
1058
1059 /* journal backup */
1060 if (prev_bg_bitmap_index != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +02001061 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Uma Shankara74a99a2012-05-25 21:22:49 +05301062 0, fs->blksz, journal_buffer);
1063 if (status == 0)
1064 goto fail;
Stefan Brünsbad73812016-09-17 02:10:10 +02001065 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Uma Shankara74a99a2012-05-25 21:22:49 +05301066 goto fail;
1067
1068 prev_bg_bitmap_index = bg_idx;
1069 }
Stefan Brüns05313d12016-09-20 01:13:01 +02001070 ext4fs_bg_free_blocks_dec(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +02001071 ext4fs_sb_free_blocks_dec(fs->sb);
Uma Shankara74a99a2012-05-25 21:22:49 +05301072 goto success;
1073 }
1074success:
1075 free(journal_buffer);
1076 free(zero_buffer);
1077
1078 return fs->curr_blkno;
1079fail:
1080 free(journal_buffer);
1081 free(zero_buffer);
1082
1083 return -1;
1084}
1085
1086int ext4fs_get_new_inode_no(void)
1087{
1088 short i;
1089 short status;
1090 unsigned int ibmap_idx;
1091 static int prev_inode_bitmap_index = -1;
Michael Walle13179c22016-09-01 11:21:40 +02001092 unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Uma Shankara74a99a2012-05-25 21:22:49 +05301093 struct ext_filesystem *fs = get_fs();
1094 char *journal_buffer = zalloc(fs->blksz);
1095 char *zero_buffer = zalloc(fs->blksz);
1096 if (!journal_buffer || !zero_buffer)
1097 goto fail;
Stefan Brünsb7cdac02016-09-06 04:36:47 +02001098 int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
1099 EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
Uma Shankara74a99a2012-05-25 21:22:49 +05301100
1101 if (fs->first_pass_ibmap == 0) {
1102 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +02001103 uint32_t free_inodes;
1104 struct ext2_block_group *bgd = NULL;
1105 bgd = ext4fs_get_group_descriptor(fs, i);
1106 free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
1107 if (free_inodes) {
1108 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1109 uint64_t i_bitmap_blk =
1110 ext4fs_bg_get_inode_id(bgd, fs);
Stefan Brünsb7cdac02016-09-06 04:36:47 +02001111 if (has_gdt_chksum)
Stefan Brünsbad73812016-09-17 02:10:10 +02001112 bgd->bg_itable_unused = free_inodes;
1113 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1114 put_ext4(i_bitmap_blk * fs->blksz,
Uma Shankara74a99a2012-05-25 21:22:49 +05301115 zero_buffer, fs->blksz);
Stefan Brünsbad73812016-09-17 02:10:10 +02001116 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1117 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankara74a99a2012-05-25 21:22:49 +05301118 memcpy(fs->inode_bmaps[i],
1119 zero_buffer, fs->blksz);
1120 }
1121 fs->curr_inode_no =
1122 _get_new_inode_no(fs->inode_bmaps[i]);
1123 if (fs->curr_inode_no == -1)
Stefan Brünsbad73812016-09-17 02:10:10 +02001124 /* inode bitmap is completely filled */
Uma Shankara74a99a2012-05-25 21:22:49 +05301125 continue;
1126 fs->curr_inode_no = fs->curr_inode_no +
1127 (i * inodes_per_grp);
1128 fs->first_pass_ibmap++;
Stefan Brüns05313d12016-09-20 01:13:01 +02001129 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brünsb7cdac02016-09-06 04:36:47 +02001130 if (has_gdt_chksum)
Stefan Brüns05313d12016-09-20 01:13:01 +02001131 ext4fs_bg_itable_unused_dec(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +02001132 ext4fs_sb_free_inodes_dec(fs->sb);
Stefan Brünsbad73812016-09-17 02:10:10 +02001133 status = ext4fs_devread(i_bitmap_blk *
1134 fs->sect_perblk,
1135 0, fs->blksz,
Uma Shankara74a99a2012-05-25 21:22:49 +05301136 journal_buffer);
1137 if (status == 0)
1138 goto fail;
1139 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +02001140 i_bitmap_blk))
Uma Shankara74a99a2012-05-25 21:22:49 +05301141 goto fail;
1142 goto success;
1143 } else
1144 debug("no inode left on block group %d\n", i);
1145 }
1146 goto fail;
1147 } else {
1148restart:
1149 fs->curr_inode_no++;
1150 /* get the blockbitmap index respective to blockno */
1151 ibmap_idx = fs->curr_inode_no / inodes_per_grp;
Stefan Brünsbad73812016-09-17 02:10:10 +02001152 struct ext2_block_group *bgd =
1153 ext4fs_get_group_descriptor(fs, ibmap_idx);
1154 uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
1155 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
1156
1157 if (bg_flags & EXT4_BG_INODE_UNINIT) {
1158 put_ext4(i_bitmap_blk * fs->blksz,
Michael Walle13179c22016-09-01 11:21:40 +02001159 zero_buffer, fs->blksz);
Stefan Brünsbad73812016-09-17 02:10:10 +02001160 bg_flags &= ~EXT4_BG_INODE_UNINIT;
1161 ext4fs_bg_set_flags(bgd, bg_flags);
Uma Shankara74a99a2012-05-25 21:22:49 +05301162 memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
1163 fs->blksz);
1164 }
1165
1166 if (ext4fs_set_inode_bmap(fs->curr_inode_no,
1167 fs->inode_bmaps[ibmap_idx],
1168 ibmap_idx) != 0) {
1169 debug("going for restart for the block no %d %u\n",
1170 fs->curr_inode_no, ibmap_idx);
1171 goto restart;
1172 }
1173
1174 /* journal backup */
1175 if (prev_inode_bitmap_index != ibmap_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +02001176 status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
Uma Shankara74a99a2012-05-25 21:22:49 +05301177 0, fs->blksz, journal_buffer);
1178 if (status == 0)
1179 goto fail;
1180 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +02001181 le32_to_cpu(bgd->inode_id)))
Uma Shankara74a99a2012-05-25 21:22:49 +05301182 goto fail;
1183 prev_inode_bitmap_index = ibmap_idx;
1184 }
Stefan Brüns05313d12016-09-20 01:13:01 +02001185 ext4fs_bg_free_inodes_dec(bgd, fs);
Stefan Brünsb7cdac02016-09-06 04:36:47 +02001186 if (has_gdt_chksum)
Stefan Brünsbad73812016-09-17 02:10:10 +02001187 bgd->bg_itable_unused = bgd->free_inodes;
Michael Walle13179c22016-09-01 11:21:40 +02001188 ext4fs_sb_free_inodes_dec(fs->sb);
Uma Shankara74a99a2012-05-25 21:22:49 +05301189 goto success;
1190 }
1191
1192success:
1193 free(journal_buffer);
1194 free(zero_buffer);
1195
1196 return fs->curr_inode_no;
1197fail:
1198 free(journal_buffer);
1199 free(zero_buffer);
1200
1201 return -1;
1202
1203}
1204
1205
1206static void alloc_single_indirect_block(struct ext2_inode *file_inode,
1207 unsigned int *total_remaining_blocks,
1208 unsigned int *no_blks_reqd)
1209{
1210 short i;
1211 short status;
1212 long int actual_block_no;
1213 long int si_blockno;
1214 /* si :single indirect */
Michael Walle13179c22016-09-01 11:21:40 +02001215 __le32 *si_buffer = NULL;
1216 __le32 *si_start_addr = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +05301217 struct ext_filesystem *fs = get_fs();
1218
1219 if (*total_remaining_blocks != 0) {
1220 si_buffer = zalloc(fs->blksz);
1221 if (!si_buffer) {
1222 printf("No Memory\n");
1223 return;
1224 }
1225 si_start_addr = si_buffer;
1226 si_blockno = ext4fs_get_new_blk_no();
1227 if (si_blockno == -1) {
1228 printf("no block left to assign\n");
1229 goto fail;
1230 }
1231 (*no_blks_reqd)++;
1232 debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
1233
Frederic Leroye7ee0282013-06-26 18:11:25 +02001234 status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
Uma Shankara74a99a2012-05-25 21:22:49 +05301235 0, fs->blksz, (char *)si_buffer);
1236 memset(si_buffer, '\0', fs->blksz);
1237 if (status == 0)
1238 goto fail;
1239
1240 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1241 actual_block_no = ext4fs_get_new_blk_no();
1242 if (actual_block_no == -1) {
1243 printf("no block left to assign\n");
1244 goto fail;
1245 }
Michael Walle13179c22016-09-01 11:21:40 +02001246 *si_buffer = cpu_to_le32(actual_block_no);
Uma Shankara74a99a2012-05-25 21:22:49 +05301247 debug("SIAB %u: %u\n", *si_buffer,
1248 *total_remaining_blocks);
1249
1250 si_buffer++;
1251 (*total_remaining_blocks)--;
1252 if (*total_remaining_blocks == 0)
1253 break;
1254 }
1255
1256 /* write the block to disk */
Ma Haijune0996ca2014-01-08 08:15:33 +08001257 put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301258 si_start_addr, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001259 file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
Uma Shankara74a99a2012-05-25 21:22:49 +05301260 }
1261fail:
1262 free(si_start_addr);
1263}
1264
1265static void alloc_double_indirect_block(struct ext2_inode *file_inode,
1266 unsigned int *total_remaining_blocks,
1267 unsigned int *no_blks_reqd)
1268{
1269 short i;
1270 short j;
1271 short status;
1272 long int actual_block_no;
1273 /* di:double indirect */
1274 long int di_blockno_parent;
1275 long int di_blockno_child;
Michael Walle13179c22016-09-01 11:21:40 +02001276 __le32 *di_parent_buffer = NULL;
1277 __le32 *di_child_buff = NULL;
1278 __le32 *di_block_start_addr = NULL;
1279 __le32 *di_child_buff_start = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +05301280 struct ext_filesystem *fs = get_fs();
1281
1282 if (*total_remaining_blocks != 0) {
1283 /* double indirect parent block connecting to inode */
1284 di_blockno_parent = ext4fs_get_new_blk_no();
1285 if (di_blockno_parent == -1) {
1286 printf("no block left to assign\n");
1287 goto fail;
1288 }
1289 di_parent_buffer = zalloc(fs->blksz);
1290 if (!di_parent_buffer)
1291 goto fail;
1292
1293 di_block_start_addr = di_parent_buffer;
1294 (*no_blks_reqd)++;
1295 debug("DIPB %ld: %u\n", di_blockno_parent,
1296 *total_remaining_blocks);
1297
Frederic Leroye7ee0282013-06-26 18:11:25 +02001298 status = ext4fs_devread((lbaint_t)di_blockno_parent *
Uma Shankara74a99a2012-05-25 21:22:49 +05301299 fs->sect_perblk, 0,
1300 fs->blksz, (char *)di_parent_buffer);
Łukasz Majewskib55a7932012-12-05 08:06:39 +00001301
1302 if (!status) {
1303 printf("%s: Device read error!\n", __func__);
1304 goto fail;
1305 }
Uma Shankara74a99a2012-05-25 21:22:49 +05301306 memset(di_parent_buffer, '\0', fs->blksz);
1307
1308 /*
1309 * start:for each double indirect parent
1310 * block create one more block
1311 */
1312 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1313 di_blockno_child = ext4fs_get_new_blk_no();
1314 if (di_blockno_child == -1) {
1315 printf("no block left to assign\n");
1316 goto fail;
1317 }
1318 di_child_buff = zalloc(fs->blksz);
1319 if (!di_child_buff)
1320 goto fail;
1321
1322 di_child_buff_start = di_child_buff;
Michael Walle13179c22016-09-01 11:21:40 +02001323 *di_parent_buffer = cpu_to_le32(di_blockno_child);
Uma Shankara74a99a2012-05-25 21:22:49 +05301324 di_parent_buffer++;
1325 (*no_blks_reqd)++;
1326 debug("DICB %ld: %u\n", di_blockno_child,
1327 *total_remaining_blocks);
1328
Frederic Leroye7ee0282013-06-26 18:11:25 +02001329 status = ext4fs_devread((lbaint_t)di_blockno_child *
Uma Shankara74a99a2012-05-25 21:22:49 +05301330 fs->sect_perblk, 0,
1331 fs->blksz,
1332 (char *)di_child_buff);
Łukasz Majewskib55a7932012-12-05 08:06:39 +00001333
1334 if (!status) {
1335 printf("%s: Device read error!\n", __func__);
1336 goto fail;
1337 }
Uma Shankara74a99a2012-05-25 21:22:49 +05301338 memset(di_child_buff, '\0', fs->blksz);
1339 /* filling of actual datablocks for each child */
1340 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1341 actual_block_no = ext4fs_get_new_blk_no();
1342 if (actual_block_no == -1) {
1343 printf("no block left to assign\n");
1344 goto fail;
1345 }
Michael Walle13179c22016-09-01 11:21:40 +02001346 *di_child_buff = cpu_to_le32(actual_block_no);
Uma Shankara74a99a2012-05-25 21:22:49 +05301347 debug("DIAB %ld: %u\n", actual_block_no,
1348 *total_remaining_blocks);
1349
1350 di_child_buff++;
1351 (*total_remaining_blocks)--;
1352 if (*total_remaining_blocks == 0)
1353 break;
1354 }
1355 /* write the block table */
Ma Haijune0996ca2014-01-08 08:15:33 +08001356 put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301357 di_child_buff_start, fs->blksz);
1358 free(di_child_buff_start);
1359 di_child_buff_start = NULL;
1360
1361 if (*total_remaining_blocks == 0)
1362 break;
1363 }
Ma Haijune0996ca2014-01-08 08:15:33 +08001364 put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301365 di_block_start_addr, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001366 file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
Uma Shankara74a99a2012-05-25 21:22:49 +05301367 }
1368fail:
1369 free(di_block_start_addr);
1370}
1371
1372static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
1373 unsigned int *total_remaining_blocks,
1374 unsigned int *no_blks_reqd)
1375{
1376 short i;
1377 short j;
1378 short k;
1379 long int actual_block_no;
1380 /* ti: Triple Indirect */
1381 long int ti_gp_blockno;
1382 long int ti_parent_blockno;
1383 long int ti_child_blockno;
Michael Walle13179c22016-09-01 11:21:40 +02001384 __le32 *ti_gp_buff = NULL;
1385 __le32 *ti_parent_buff = NULL;
1386 __le32 *ti_child_buff = NULL;
1387 __le32 *ti_gp_buff_start_addr = NULL;
1388 __le32 *ti_pbuff_start_addr = NULL;
1389 __le32 *ti_cbuff_start_addr = NULL;
Uma Shankara74a99a2012-05-25 21:22:49 +05301390 struct ext_filesystem *fs = get_fs();
1391 if (*total_remaining_blocks != 0) {
1392 /* triple indirect grand parent block connecting to inode */
1393 ti_gp_blockno = ext4fs_get_new_blk_no();
1394 if (ti_gp_blockno == -1) {
1395 printf("no block left to assign\n");
Tom Rini9166c3c2015-12-10 16:42:21 -05001396 return;
Uma Shankara74a99a2012-05-25 21:22:49 +05301397 }
1398 ti_gp_buff = zalloc(fs->blksz);
1399 if (!ti_gp_buff)
Tom Rini9166c3c2015-12-10 16:42:21 -05001400 return;
Uma Shankara74a99a2012-05-25 21:22:49 +05301401
1402 ti_gp_buff_start_addr = ti_gp_buff;
1403 (*no_blks_reqd)++;
1404 debug("TIGPB %ld: %u\n", ti_gp_blockno,
1405 *total_remaining_blocks);
1406
1407 /* for each 4 byte grand parent entry create one more block */
1408 for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
1409 ti_parent_blockno = ext4fs_get_new_blk_no();
1410 if (ti_parent_blockno == -1) {
1411 printf("no block left to assign\n");
1412 goto fail;
1413 }
1414 ti_parent_buff = zalloc(fs->blksz);
1415 if (!ti_parent_buff)
1416 goto fail;
1417
1418 ti_pbuff_start_addr = ti_parent_buff;
Michael Walle13179c22016-09-01 11:21:40 +02001419 *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
Uma Shankara74a99a2012-05-25 21:22:49 +05301420 ti_gp_buff++;
1421 (*no_blks_reqd)++;
1422 debug("TIPB %ld: %u\n", ti_parent_blockno,
1423 *total_remaining_blocks);
1424
1425 /* for each 4 byte entry parent create one more block */
1426 for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
1427 ti_child_blockno = ext4fs_get_new_blk_no();
1428 if (ti_child_blockno == -1) {
1429 printf("no block left assign\n");
Tom Rini9166c3c2015-12-10 16:42:21 -05001430 goto fail1;
Uma Shankara74a99a2012-05-25 21:22:49 +05301431 }
1432 ti_child_buff = zalloc(fs->blksz);
1433 if (!ti_child_buff)
Tom Rini9166c3c2015-12-10 16:42:21 -05001434 goto fail1;
Uma Shankara74a99a2012-05-25 21:22:49 +05301435
1436 ti_cbuff_start_addr = ti_child_buff;
Michael Walle13179c22016-09-01 11:21:40 +02001437 *ti_parent_buff = cpu_to_le32(ti_child_blockno);
Uma Shankara74a99a2012-05-25 21:22:49 +05301438 ti_parent_buff++;
1439 (*no_blks_reqd)++;
1440 debug("TICB %ld: %u\n", ti_parent_blockno,
1441 *total_remaining_blocks);
1442
1443 /* fill actual datablocks for each child */
1444 for (k = 0; k < (fs->blksz / sizeof(int));
1445 k++) {
1446 actual_block_no =
1447 ext4fs_get_new_blk_no();
1448 if (actual_block_no == -1) {
1449 printf("no block left\n");
Tom Rini9166c3c2015-12-10 16:42:21 -05001450 free(ti_cbuff_start_addr);
1451 goto fail1;
Uma Shankara74a99a2012-05-25 21:22:49 +05301452 }
Michael Walle13179c22016-09-01 11:21:40 +02001453 *ti_child_buff = cpu_to_le32(actual_block_no);
Uma Shankara74a99a2012-05-25 21:22:49 +05301454 debug("TIAB %ld: %u\n", actual_block_no,
1455 *total_remaining_blocks);
1456
1457 ti_child_buff++;
1458 (*total_remaining_blocks)--;
1459 if (*total_remaining_blocks == 0)
1460 break;
1461 }
1462 /* write the child block */
Ma Haijune0996ca2014-01-08 08:15:33 +08001463 put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
1464 (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301465 ti_cbuff_start_addr, fs->blksz);
1466 free(ti_cbuff_start_addr);
1467
1468 if (*total_remaining_blocks == 0)
1469 break;
1470 }
1471 /* write the parent block */
Ma Haijune0996ca2014-01-08 08:15:33 +08001472 put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301473 ti_pbuff_start_addr, fs->blksz);
1474 free(ti_pbuff_start_addr);
1475
1476 if (*total_remaining_blocks == 0)
1477 break;
1478 }
1479 /* write the grand parent block */
Ma Haijune0996ca2014-01-08 08:15:33 +08001480 put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
Uma Shankara74a99a2012-05-25 21:22:49 +05301481 ti_gp_buff_start_addr, fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +02001482 file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
Tom Rini9166c3c2015-12-10 16:42:21 -05001483 free(ti_gp_buff_start_addr);
1484 return;
Uma Shankara74a99a2012-05-25 21:22:49 +05301485 }
Tom Rini9166c3c2015-12-10 16:42:21 -05001486fail1:
1487 free(ti_pbuff_start_addr);
Uma Shankara74a99a2012-05-25 21:22:49 +05301488fail:
1489 free(ti_gp_buff_start_addr);
1490}
1491
1492void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
1493 unsigned int total_remaining_blocks,
1494 unsigned int *total_no_of_block)
1495{
1496 short i;
1497 long int direct_blockno;
1498 unsigned int no_blks_reqd = 0;
1499
1500 /* allocation of direct blocks */
Stephen Warrene62f1ba2014-06-11 12:46:16 -06001501 for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
Uma Shankara74a99a2012-05-25 21:22:49 +05301502 direct_blockno = ext4fs_get_new_blk_no();
1503 if (direct_blockno == -1) {
1504 printf("no block left to assign\n");
1505 return;
1506 }
Michael Walle13179c22016-09-01 11:21:40 +02001507 file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
Uma Shankara74a99a2012-05-25 21:22:49 +05301508 debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
1509
1510 total_remaining_blocks--;
Uma Shankara74a99a2012-05-25 21:22:49 +05301511 }
1512
1513 alloc_single_indirect_block(file_inode, &total_remaining_blocks,
1514 &no_blks_reqd);
1515 alloc_double_indirect_block(file_inode, &total_remaining_blocks,
1516 &no_blks_reqd);
1517 alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
1518 &no_blks_reqd);
1519 *total_no_of_block += no_blks_reqd;
1520}
1521
1522#endif
1523
Uma Shankar71014b62012-05-25 21:21:44 +05301524static struct ext4_extent_header *ext4fs_get_extent_block
1525 (struct ext2_data *data, char *buf,
1526 struct ext4_extent_header *ext_block,
1527 uint32_t fileblock, int log2_blksz)
1528{
1529 struct ext4_extent_idx *index;
1530 unsigned long long block;
Ionut Nicu48b2b592014-01-13 11:59:24 +01001531 int blksz = EXT2_BLOCK_SIZE(data);
Uma Shankar71014b62012-05-25 21:21:44 +05301532 int i;
1533
1534 while (1) {
1535 index = (struct ext4_extent_idx *)(ext_block + 1);
1536
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001537 if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
Michael Walle13179c22016-09-01 11:21:40 +02001538 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05301539
1540 if (ext_block->eh_depth == 0)
1541 return ext_block;
1542 i = -1;
1543 do {
1544 i++;
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001545 if (i >= le16_to_cpu(ext_block->eh_entries))
Uma Shankar71014b62012-05-25 21:21:44 +05301546 break;
Ionut Nicu01f606c2014-01-13 12:00:08 +01001547 } while (fileblock >= le32_to_cpu(index[i].ei_block));
Uma Shankar71014b62012-05-25 21:21:44 +05301548
1549 if (--i < 0)
Michael Walle13179c22016-09-01 11:21:40 +02001550 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05301551
Rommel Custodio5a4f74d2013-07-21 10:53:25 +02001552 block = le16_to_cpu(index[i].ei_leaf_hi);
Uma Shankar71014b62012-05-25 21:21:44 +05301553 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1554
Ionut Nicu48b2b592014-01-13 11:59:24 +01001555 if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
Frederic Leroye7ee0282013-06-26 18:11:25 +02001556 buf))
Uma Shankar71014b62012-05-25 21:21:44 +05301557 ext_block = (struct ext4_extent_header *)buf;
1558 else
Michael Walle13179c22016-09-01 11:21:40 +02001559 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05301560 }
1561}
1562
1563static int ext4fs_blockgroup
1564 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1565{
1566 long int blkno;
1567 unsigned int blkoff, desc_per_blk;
Egbert Eich7b1b2552013-05-01 01:13:19 +00001568 int log2blksz = get_fs()->dev_desc->log2blksz;
Stefan Brüns8f48fd52016-09-17 02:10:09 +02001569 int desc_size = get_fs()->gdsize;
Uma Shankar71014b62012-05-25 21:21:44 +05301570
Stefan Brüns8f48fd52016-09-17 02:10:09 +02001571 desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
Uma Shankar71014b62012-05-25 21:21:44 +05301572
Michael Wallec07cdcb2016-08-29 10:46:44 +02001573 blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
Uma Shankar71014b62012-05-25 21:21:44 +05301574 group / desc_per_blk;
Stefan Brüns8f48fd52016-09-17 02:10:09 +02001575 blkoff = (group % desc_per_blk) * desc_size;
Uma Shankar71014b62012-05-25 21:21:44 +05301576
1577 debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
1578 group, blkno, blkoff);
1579
Frederic Leroye7ee0282013-06-26 18:11:25 +02001580 return ext4fs_devread((lbaint_t)blkno <<
1581 (LOG2_BLOCK_SIZE(data) - log2blksz),
Stefan Brüns8f48fd52016-09-17 02:10:09 +02001582 blkoff, desc_size, (char *)blkgrp);
Uma Shankar71014b62012-05-25 21:21:44 +05301583}
1584
1585int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
1586{
1587 struct ext2_block_group blkgrp;
1588 struct ext2_sblock *sblock = &data->sblock;
1589 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-05-01 01:13:19 +00001590 int log2blksz = get_fs()->dev_desc->log2blksz;
Uma Shankar71014b62012-05-25 21:21:44 +05301591 int inodes_per_block, status;
1592 long int blkno;
1593 unsigned int blkoff;
1594
1595 /* It is easier to calculate if the first inode is 0. */
1596 ino--;
Michael Wallec07cdcb2016-08-29 10:46:44 +02001597 status = ext4fs_blockgroup(data, ino / le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301598 (sblock->inodes_per_group), &blkgrp);
1599 if (status == 0)
1600 return 0;
1601
1602 inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
Stefan Brünsbad73812016-09-17 02:10:10 +02001603 blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
Michael Wallec07cdcb2016-08-29 10:46:44 +02001604 (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Uma Shankar71014b62012-05-25 21:21:44 +05301605 blkoff = (ino % inodes_per_block) * fs->inodesz;
1606 /* Read the inode. */
Frederic Leroye7ee0282013-06-26 18:11:25 +02001607 status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
1608 log2blksz), blkoff,
Uma Shankar71014b62012-05-25 21:21:44 +05301609 sizeof(struct ext2_inode), (char *)inode);
1610 if (status == 0)
1611 return 0;
1612
1613 return 1;
1614}
1615
1616long int read_allocated_block(struct ext2_inode *inode, int fileblock)
1617{
1618 long int blknr;
1619 int blksz;
1620 int log2_blksz;
1621 int status;
1622 long int rblock;
1623 long int perblock_parent;
1624 long int perblock_child;
1625 unsigned long long start;
1626 /* get the blocksize of the filesystem */
1627 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich7b1b2552013-05-01 01:13:19 +00001628 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1629 - get_fs()->dev_desc->log2blksz;
1630
Uma Shankar71014b62012-05-25 21:21:44 +05301631 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
Stefan Brüns551fb6c2016-11-05 22:17:14 +01001632 long int startblock, endblock;
Uma Shankar71014b62012-05-25 21:21:44 +05301633 char *buf = zalloc(blksz);
1634 if (!buf)
1635 return -ENOMEM;
1636 struct ext4_extent_header *ext_block;
1637 struct ext4_extent *extent;
Stefan Brüns551fb6c2016-11-05 22:17:14 +01001638 int i;
Egbert Eich7b1b2552013-05-01 01:13:19 +00001639 ext_block =
1640 ext4fs_get_extent_block(ext4fs_root, buf,
1641 (struct ext4_extent_header *)
1642 inode->b.blocks.dir_blocks,
1643 fileblock, log2_blksz);
Uma Shankar71014b62012-05-25 21:21:44 +05301644 if (!ext_block) {
1645 printf("invalid extent block\n");
1646 free(buf);
1647 return -EINVAL;
1648 }
1649
1650 extent = (struct ext4_extent *)(ext_block + 1);
1651
Stefan Brüns551fb6c2016-11-05 22:17:14 +01001652 for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
1653 startblock = le32_to_cpu(extent[i].ee_block);
1654 endblock = startblock + le16_to_cpu(extent[i].ee_len);
1655
1656 if (startblock > fileblock) {
1657 /* Sparse file */
Uma Shankar71014b62012-05-25 21:21:44 +05301658 free(buf);
1659 return 0;
Uma Shankar71014b62012-05-25 21:21:44 +05301660
Stefan Brüns551fb6c2016-11-05 22:17:14 +01001661 } else if (fileblock < endblock) {
1662 start = le16_to_cpu(extent[i].ee_start_hi);
1663 start = (start << 32) +
Uma Shankar71014b62012-05-25 21:21:44 +05301664 le32_to_cpu(extent[i].ee_start_lo);
Stefan Brüns551fb6c2016-11-05 22:17:14 +01001665 free(buf);
1666 return (fileblock - startblock) + start;
1667 }
Uma Shankar71014b62012-05-25 21:21:44 +05301668 }
1669
Uma Shankar71014b62012-05-25 21:21:44 +05301670 free(buf);
Stefan Brüns551fb6c2016-11-05 22:17:14 +01001671 return 0;
Uma Shankar71014b62012-05-25 21:21:44 +05301672 }
1673
1674 /* Direct blocks. */
1675 if (fileblock < INDIRECT_BLOCKS)
Michael Wallec07cdcb2016-08-29 10:46:44 +02001676 blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
Uma Shankar71014b62012-05-25 21:21:44 +05301677
1678 /* Indirect. */
1679 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
1680 if (ext4fs_indir1_block == NULL) {
1681 ext4fs_indir1_block = zalloc(blksz);
1682 if (ext4fs_indir1_block == NULL) {
1683 printf("** SI ext2fs read block (indir 1)"
1684 "malloc failed. **\n");
1685 return -1;
1686 }
1687 ext4fs_indir1_size = blksz;
1688 ext4fs_indir1_blkno = -1;
1689 }
1690 if (blksz != ext4fs_indir1_size) {
1691 free(ext4fs_indir1_block);
1692 ext4fs_indir1_block = NULL;
1693 ext4fs_indir1_size = 0;
1694 ext4fs_indir1_blkno = -1;
1695 ext4fs_indir1_block = zalloc(blksz);
1696 if (ext4fs_indir1_block == NULL) {
1697 printf("** SI ext2fs read block (indir 1):"
1698 "malloc failed. **\n");
1699 return -1;
1700 }
1701 ext4fs_indir1_size = blksz;
1702 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001703 if ((le32_to_cpu(inode->b.blocks.indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301704 log2_blksz) != ext4fs_indir1_blkno) {
1705 status =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001706 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301707 (inode->b.blocks.
1708 indir_block) << log2_blksz, 0,
1709 blksz, (char *)ext4fs_indir1_block);
1710 if (status == 0) {
1711 printf("** SI ext2fs read block (indir 1)"
1712 "failed. **\n");
Stefan Brüns74674ed2016-09-06 04:36:55 +02001713 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +05301714 }
1715 ext4fs_indir1_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001716 le32_to_cpu(inode->b.blocks.
Uma Shankar71014b62012-05-25 21:21:44 +05301717 indir_block) << log2_blksz;
1718 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001719 blknr = le32_to_cpu(ext4fs_indir1_block
Uma Shankar71014b62012-05-25 21:21:44 +05301720 [fileblock - INDIRECT_BLOCKS]);
1721 }
1722 /* Double indirect. */
1723 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
1724 (blksz / 4 + 1)))) {
1725
1726 long int perblock = blksz / 4;
1727 long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
1728
1729 if (ext4fs_indir1_block == NULL) {
1730 ext4fs_indir1_block = zalloc(blksz);
1731 if (ext4fs_indir1_block == NULL) {
1732 printf("** DI ext2fs read block (indir 2 1)"
1733 "malloc failed. **\n");
1734 return -1;
1735 }
1736 ext4fs_indir1_size = blksz;
1737 ext4fs_indir1_blkno = -1;
1738 }
1739 if (blksz != ext4fs_indir1_size) {
1740 free(ext4fs_indir1_block);
1741 ext4fs_indir1_block = NULL;
1742 ext4fs_indir1_size = 0;
1743 ext4fs_indir1_blkno = -1;
1744 ext4fs_indir1_block = zalloc(blksz);
1745 if (ext4fs_indir1_block == NULL) {
1746 printf("** DI ext2fs read block (indir 2 1)"
1747 "malloc failed. **\n");
1748 return -1;
1749 }
1750 ext4fs_indir1_size = blksz;
1751 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001752 if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301753 log2_blksz) != ext4fs_indir1_blkno) {
1754 status =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001755 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301756 (inode->b.blocks.
1757 double_indir_block) << log2_blksz,
1758 0, blksz,
1759 (char *)ext4fs_indir1_block);
1760 if (status == 0) {
1761 printf("** DI ext2fs read block (indir 2 1)"
1762 "failed. **\n");
1763 return -1;
1764 }
1765 ext4fs_indir1_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001766 le32_to_cpu(inode->b.blocks.double_indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301767 log2_blksz;
1768 }
1769
1770 if (ext4fs_indir2_block == NULL) {
1771 ext4fs_indir2_block = zalloc(blksz);
1772 if (ext4fs_indir2_block == NULL) {
1773 printf("** DI ext2fs read block (indir 2 2)"
1774 "malloc failed. **\n");
1775 return -1;
1776 }
1777 ext4fs_indir2_size = blksz;
1778 ext4fs_indir2_blkno = -1;
1779 }
1780 if (blksz != ext4fs_indir2_size) {
1781 free(ext4fs_indir2_block);
1782 ext4fs_indir2_block = NULL;
1783 ext4fs_indir2_size = 0;
1784 ext4fs_indir2_blkno = -1;
1785 ext4fs_indir2_block = zalloc(blksz);
1786 if (ext4fs_indir2_block == NULL) {
1787 printf("** DI ext2fs read block (indir 2 2)"
1788 "malloc failed. **\n");
1789 return -1;
1790 }
1791 ext4fs_indir2_size = blksz;
1792 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001793 if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301794 log2_blksz) != ext4fs_indir2_blkno) {
Michael Wallec07cdcb2016-08-29 10:46:44 +02001795 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301796 (ext4fs_indir1_block
1797 [rblock /
1798 perblock]) << log2_blksz, 0,
1799 blksz,
1800 (char *)ext4fs_indir2_block);
1801 if (status == 0) {
1802 printf("** DI ext2fs read block (indir 2 2)"
1803 "failed. **\n");
1804 return -1;
1805 }
1806 ext4fs_indir2_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001807 le32_to_cpu(ext4fs_indir1_block[rblock
Uma Shankar71014b62012-05-25 21:21:44 +05301808 /
1809 perblock]) <<
1810 log2_blksz;
1811 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001812 blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
Uma Shankar71014b62012-05-25 21:21:44 +05301813 }
1814 /* Tripple indirect. */
1815 else {
1816 rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
1817 (blksz / 4 * blksz / 4));
1818 perblock_child = blksz / 4;
1819 perblock_parent = ((blksz / 4) * (blksz / 4));
1820
1821 if (ext4fs_indir1_block == NULL) {
1822 ext4fs_indir1_block = zalloc(blksz);
1823 if (ext4fs_indir1_block == NULL) {
1824 printf("** TI ext2fs read block (indir 2 1)"
1825 "malloc failed. **\n");
1826 return -1;
1827 }
1828 ext4fs_indir1_size = blksz;
1829 ext4fs_indir1_blkno = -1;
1830 }
1831 if (blksz != ext4fs_indir1_size) {
1832 free(ext4fs_indir1_block);
1833 ext4fs_indir1_block = NULL;
1834 ext4fs_indir1_size = 0;
1835 ext4fs_indir1_blkno = -1;
1836 ext4fs_indir1_block = zalloc(blksz);
1837 if (ext4fs_indir1_block == NULL) {
1838 printf("** TI ext2fs read block (indir 2 1)"
1839 "malloc failed. **\n");
1840 return -1;
1841 }
1842 ext4fs_indir1_size = blksz;
1843 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001844 if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301845 log2_blksz) != ext4fs_indir1_blkno) {
1846 status = ext4fs_devread
Frederic Leroye7ee0282013-06-26 18:11:25 +02001847 ((lbaint_t)
Michael Wallec07cdcb2016-08-29 10:46:44 +02001848 le32_to_cpu(inode->b.blocks.triple_indir_block)
Uma Shankar71014b62012-05-25 21:21:44 +05301849 << log2_blksz, 0, blksz,
1850 (char *)ext4fs_indir1_block);
1851 if (status == 0) {
1852 printf("** TI ext2fs read block (indir 2 1)"
1853 "failed. **\n");
1854 return -1;
1855 }
1856 ext4fs_indir1_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001857 le32_to_cpu(inode->b.blocks.triple_indir_block) <<
Uma Shankar71014b62012-05-25 21:21:44 +05301858 log2_blksz;
1859 }
1860
1861 if (ext4fs_indir2_block == NULL) {
1862 ext4fs_indir2_block = zalloc(blksz);
1863 if (ext4fs_indir2_block == NULL) {
1864 printf("** TI ext2fs read block (indir 2 2)"
1865 "malloc failed. **\n");
1866 return -1;
1867 }
1868 ext4fs_indir2_size = blksz;
1869 ext4fs_indir2_blkno = -1;
1870 }
1871 if (blksz != ext4fs_indir2_size) {
1872 free(ext4fs_indir2_block);
1873 ext4fs_indir2_block = NULL;
1874 ext4fs_indir2_size = 0;
1875 ext4fs_indir2_blkno = -1;
1876 ext4fs_indir2_block = zalloc(blksz);
1877 if (ext4fs_indir2_block == NULL) {
1878 printf("** TI ext2fs read block (indir 2 2)"
1879 "malloc failed. **\n");
1880 return -1;
1881 }
1882 ext4fs_indir2_size = blksz;
1883 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001884 if ((le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankar71014b62012-05-25 21:21:44 +05301885 perblock_parent]) <<
1886 log2_blksz)
1887 != ext4fs_indir2_blkno) {
Michael Wallec07cdcb2016-08-29 10:46:44 +02001888 status = ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301889 (ext4fs_indir1_block
1890 [rblock /
1891 perblock_parent]) <<
1892 log2_blksz, 0, blksz,
1893 (char *)ext4fs_indir2_block);
1894 if (status == 0) {
1895 printf("** TI ext2fs read block (indir 2 2)"
1896 "failed. **\n");
1897 return -1;
1898 }
1899 ext4fs_indir2_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001900 le32_to_cpu(ext4fs_indir1_block[rblock /
Uma Shankar71014b62012-05-25 21:21:44 +05301901 perblock_parent])
1902 << log2_blksz;
1903 }
1904
1905 if (ext4fs_indir3_block == NULL) {
1906 ext4fs_indir3_block = zalloc(blksz);
1907 if (ext4fs_indir3_block == NULL) {
1908 printf("** TI ext2fs read block (indir 2 2)"
1909 "malloc failed. **\n");
1910 return -1;
1911 }
1912 ext4fs_indir3_size = blksz;
1913 ext4fs_indir3_blkno = -1;
1914 }
1915 if (blksz != ext4fs_indir3_size) {
1916 free(ext4fs_indir3_block);
1917 ext4fs_indir3_block = NULL;
1918 ext4fs_indir3_size = 0;
1919 ext4fs_indir3_blkno = -1;
1920 ext4fs_indir3_block = zalloc(blksz);
1921 if (ext4fs_indir3_block == NULL) {
1922 printf("** TI ext2fs read block (indir 2 2)"
1923 "malloc failed. **\n");
1924 return -1;
1925 }
1926 ext4fs_indir3_size = blksz;
1927 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02001928 if ((le32_to_cpu(ext4fs_indir2_block[rblock
Uma Shankar71014b62012-05-25 21:21:44 +05301929 /
1930 perblock_child]) <<
1931 log2_blksz) != ext4fs_indir3_blkno) {
1932 status =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001933 ext4fs_devread((lbaint_t)le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05301934 (ext4fs_indir2_block
1935 [(rblock / perblock_child)
1936 % (blksz / 4)]) << log2_blksz, 0,
1937 blksz, (char *)ext4fs_indir3_block);
1938 if (status == 0) {
1939 printf("** TI ext2fs read block (indir 2 2)"
1940 "failed. **\n");
1941 return -1;
1942 }
1943 ext4fs_indir3_blkno =
Michael Wallec07cdcb2016-08-29 10:46:44 +02001944 le32_to_cpu(ext4fs_indir2_block[(rblock /
Uma Shankar71014b62012-05-25 21:21:44 +05301945 perblock_child) %
1946 (blksz /
1947 4)]) <<
1948 log2_blksz;
1949 }
1950
Michael Wallec07cdcb2016-08-29 10:46:44 +02001951 blknr = le32_to_cpu(ext4fs_indir3_block
Uma Shankar71014b62012-05-25 21:21:44 +05301952 [rblock % perblock_child]);
1953 }
Egbert Eich7b1b2552013-05-01 01:13:19 +00001954 debug("read_allocated_block %ld\n", blknr);
Uma Shankar71014b62012-05-25 21:21:44 +05301955
1956 return blknr;
1957}
1958
Łukasz Majewski900db5d2014-05-06 09:36:05 +02001959/**
1960 * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
1961 * global pointers
1962 *
1963 * This function assures that for a file with the same name but different size
1964 * the sequential store on the ext4 filesystem will be correct.
1965 *
1966 * In this function the global data, responsible for internal representation
1967 * of the ext4 data are initialized to the reset state. Without this, during
1968 * replacement of the smaller file with the bigger truncation of new file was
1969 * performed.
1970 */
1971void ext4fs_reinit_global(void)
Uma Shankar71014b62012-05-25 21:21:44 +05301972{
Uma Shankar71014b62012-05-25 21:21:44 +05301973 if (ext4fs_indir1_block != NULL) {
1974 free(ext4fs_indir1_block);
1975 ext4fs_indir1_block = NULL;
1976 ext4fs_indir1_size = 0;
1977 ext4fs_indir1_blkno = -1;
1978 }
1979 if (ext4fs_indir2_block != NULL) {
1980 free(ext4fs_indir2_block);
1981 ext4fs_indir2_block = NULL;
1982 ext4fs_indir2_size = 0;
1983 ext4fs_indir2_blkno = -1;
1984 }
1985 if (ext4fs_indir3_block != NULL) {
1986 free(ext4fs_indir3_block);
1987 ext4fs_indir3_block = NULL;
1988 ext4fs_indir3_size = 0;
1989 ext4fs_indir3_blkno = -1;
1990 }
Łukasz Majewski900db5d2014-05-06 09:36:05 +02001991}
1992void ext4fs_close(void)
1993{
1994 if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
1995 ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
1996 ext4fs_file = NULL;
1997 }
1998 if (ext4fs_root != NULL) {
1999 free(ext4fs_root);
2000 ext4fs_root = NULL;
2001 }
2002
2003 ext4fs_reinit_global();
Uma Shankar71014b62012-05-25 21:21:44 +05302004}
2005
2006int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
2007 struct ext2fs_node **fnode, int *ftype)
2008{
2009 unsigned int fpos = 0;
2010 int status;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002011 loff_t actread;
Uma Shankar71014b62012-05-25 21:21:44 +05302012 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
2013
2014#ifdef DEBUG
2015 if (name != NULL)
2016 printf("Iterate dir %s\n", name);
2017#endif /* of DEBUG */
2018 if (!diro->inode_read) {
2019 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2020 if (status == 0)
2021 return 0;
2022 }
2023 /* Search the file. */
Michael Wallec07cdcb2016-08-29 10:46:44 +02002024 while (fpos < le32_to_cpu(diro->inode.size)) {
Uma Shankar71014b62012-05-25 21:21:44 +05302025 struct ext2_dirent dirent;
2026
2027 status = ext4fs_read_file(diro, fpos,
2028 sizeof(struct ext2_dirent),
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002029 (char *)&dirent, &actread);
2030 if (status < 0)
Uma Shankar71014b62012-05-25 21:21:44 +05302031 return 0;
2032
Thomas Fitzsimmonsc6676e02015-11-18 12:42:53 -05002033 if (dirent.direntlen == 0) {
2034 printf("Failed to iterate over directory %s\n", name);
2035 return 0;
2036 }
2037
Uma Shankar71014b62012-05-25 21:21:44 +05302038 if (dirent.namelen != 0) {
2039 char filename[dirent.namelen + 1];
2040 struct ext2fs_node *fdiro;
2041 int type = FILETYPE_UNKNOWN;
2042
2043 status = ext4fs_read_file(diro,
2044 fpos +
2045 sizeof(struct ext2_dirent),
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002046 dirent.namelen, filename,
2047 &actread);
2048 if (status < 0)
Uma Shankar71014b62012-05-25 21:21:44 +05302049 return 0;
2050
2051 fdiro = zalloc(sizeof(struct ext2fs_node));
2052 if (!fdiro)
2053 return 0;
2054
2055 fdiro->data = diro->data;
Michael Wallec07cdcb2016-08-29 10:46:44 +02002056 fdiro->ino = le32_to_cpu(dirent.inode);
Uma Shankar71014b62012-05-25 21:21:44 +05302057
2058 filename[dirent.namelen] = '\0';
2059
2060 if (dirent.filetype != FILETYPE_UNKNOWN) {
2061 fdiro->inode_read = 0;
2062
2063 if (dirent.filetype == FILETYPE_DIRECTORY)
2064 type = FILETYPE_DIRECTORY;
2065 else if (dirent.filetype == FILETYPE_SYMLINK)
2066 type = FILETYPE_SYMLINK;
2067 else if (dirent.filetype == FILETYPE_REG)
2068 type = FILETYPE_REG;
2069 } else {
2070 status = ext4fs_read_inode(diro->data,
Michael Wallec07cdcb2016-08-29 10:46:44 +02002071 le32_to_cpu
Uma Shankar71014b62012-05-25 21:21:44 +05302072 (dirent.inode),
2073 &fdiro->inode);
2074 if (status == 0) {
2075 free(fdiro);
2076 return 0;
2077 }
2078 fdiro->inode_read = 1;
2079
Michael Wallec07cdcb2016-08-29 10:46:44 +02002080 if ((le16_to_cpu(fdiro->inode.mode) &
Uma Shankar71014b62012-05-25 21:21:44 +05302081 FILETYPE_INO_MASK) ==
2082 FILETYPE_INO_DIRECTORY) {
2083 type = FILETYPE_DIRECTORY;
Michael Wallec07cdcb2016-08-29 10:46:44 +02002084 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankar71014b62012-05-25 21:21:44 +05302085 & FILETYPE_INO_MASK) ==
2086 FILETYPE_INO_SYMLINK) {
2087 type = FILETYPE_SYMLINK;
Michael Wallec07cdcb2016-08-29 10:46:44 +02002088 } else if ((le16_to_cpu(fdiro->inode.mode)
Uma Shankar71014b62012-05-25 21:21:44 +05302089 & FILETYPE_INO_MASK) ==
2090 FILETYPE_INO_REG) {
2091 type = FILETYPE_REG;
2092 }
2093 }
2094#ifdef DEBUG
2095 printf("iterate >%s<\n", filename);
2096#endif /* of DEBUG */
2097 if ((name != NULL) && (fnode != NULL)
2098 && (ftype != NULL)) {
2099 if (strcmp(filename, name) == 0) {
2100 *ftype = type;
2101 *fnode = fdiro;
2102 return 1;
2103 }
2104 } else {
2105 if (fdiro->inode_read == 0) {
2106 status = ext4fs_read_inode(diro->data,
Michael Wallec07cdcb2016-08-29 10:46:44 +02002107 le32_to_cpu(
Uma Shankar71014b62012-05-25 21:21:44 +05302108 dirent.inode),
2109 &fdiro->inode);
2110 if (status == 0) {
2111 free(fdiro);
2112 return 0;
2113 }
2114 fdiro->inode_read = 1;
2115 }
2116 switch (type) {
2117 case FILETYPE_DIRECTORY:
2118 printf("<DIR> ");
2119 break;
2120 case FILETYPE_SYMLINK:
2121 printf("<SYM> ");
2122 break;
2123 case FILETYPE_REG:
2124 printf(" ");
2125 break;
2126 default:
2127 printf("< ? > ");
2128 break;
2129 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002130 printf("%10u %s\n",
Michael Wallec07cdcb2016-08-29 10:46:44 +02002131 le32_to_cpu(fdiro->inode.size),
Uma Shankar71014b62012-05-25 21:21:44 +05302132 filename);
2133 }
2134 free(fdiro);
2135 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02002136 fpos += le16_to_cpu(dirent.direntlen);
Uma Shankar71014b62012-05-25 21:21:44 +05302137 }
2138 return 0;
2139}
2140
2141static char *ext4fs_read_symlink(struct ext2fs_node *node)
2142{
2143 char *symlink;
2144 struct ext2fs_node *diro = node;
2145 int status;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002146 loff_t actread;
Uma Shankar71014b62012-05-25 21:21:44 +05302147
2148 if (!diro->inode_read) {
2149 status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
2150 if (status == 0)
Michael Walle13179c22016-09-01 11:21:40 +02002151 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05302152 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02002153 symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
Uma Shankar71014b62012-05-25 21:21:44 +05302154 if (!symlink)
Michael Walle13179c22016-09-01 11:21:40 +02002155 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05302156
Michael Wallec07cdcb2016-08-29 10:46:44 +02002157 if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
Uma Shankar71014b62012-05-25 21:21:44 +05302158 strncpy(symlink, diro->inode.b.symlink,
Michael Wallec07cdcb2016-08-29 10:46:44 +02002159 le32_to_cpu(diro->inode.size));
Uma Shankar71014b62012-05-25 21:21:44 +05302160 } else {
2161 status = ext4fs_read_file(diro, 0,
Michael Wallec07cdcb2016-08-29 10:46:44 +02002162 le32_to_cpu(diro->inode.size),
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002163 symlink, &actread);
Gary Bisson086e0112015-09-07 11:20:07 +02002164 if ((status < 0) || (actread == 0)) {
Uma Shankar71014b62012-05-25 21:21:44 +05302165 free(symlink);
Michael Walle13179c22016-09-01 11:21:40 +02002166 return NULL;
Uma Shankar71014b62012-05-25 21:21:44 +05302167 }
2168 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02002169 symlink[le32_to_cpu(diro->inode.size)] = '\0';
Uma Shankar71014b62012-05-25 21:21:44 +05302170 return symlink;
2171}
2172
2173static int ext4fs_find_file1(const char *currpath,
2174 struct ext2fs_node *currroot,
2175 struct ext2fs_node **currfound, int *foundtype)
2176{
2177 char fpath[strlen(currpath) + 1];
2178 char *name = fpath;
2179 char *next;
2180 int status;
2181 int type = FILETYPE_DIRECTORY;
2182 struct ext2fs_node *currnode = currroot;
2183 struct ext2fs_node *oldnode = currroot;
2184
2185 strncpy(fpath, currpath, strlen(currpath) + 1);
2186
2187 /* Remove all leading slashes. */
2188 while (*name == '/')
2189 name++;
2190
2191 if (!*name) {
2192 *currfound = currnode;
2193 return 1;
2194 }
2195
2196 for (;;) {
2197 int found;
2198
2199 /* Extract the actual part from the pathname. */
2200 next = strchr(name, '/');
2201 if (next) {
2202 /* Remove all leading slashes. */
2203 while (*next == '/')
2204 *(next++) = '\0';
2205 }
2206
2207 if (type != FILETYPE_DIRECTORY) {
2208 ext4fs_free_node(currnode, currroot);
2209 return 0;
2210 }
2211
2212 oldnode = currnode;
2213
2214 /* Iterate over the directory. */
2215 found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
2216 if (found == 0)
2217 return 0;
2218
2219 if (found == -1)
2220 break;
2221
2222 /* Read in the symlink and follow it. */
2223 if (type == FILETYPE_SYMLINK) {
2224 char *symlink;
2225
2226 /* Test if the symlink does not loop. */
2227 if (++symlinknest == 8) {
2228 ext4fs_free_node(currnode, currroot);
2229 ext4fs_free_node(oldnode, currroot);
2230 return 0;
2231 }
2232
2233 symlink = ext4fs_read_symlink(currnode);
2234 ext4fs_free_node(currnode, currroot);
2235
2236 if (!symlink) {
2237 ext4fs_free_node(oldnode, currroot);
2238 return 0;
2239 }
2240
2241 debug("Got symlink >%s<\n", symlink);
2242
2243 if (symlink[0] == '/') {
2244 ext4fs_free_node(oldnode, currroot);
2245 oldnode = &ext4fs_root->diropen;
2246 }
2247
2248 /* Lookup the node the symlink points to. */
2249 status = ext4fs_find_file1(symlink, oldnode,
2250 &currnode, &type);
2251
2252 free(symlink);
2253
2254 if (status == 0) {
2255 ext4fs_free_node(oldnode, currroot);
2256 return 0;
2257 }
2258 }
2259
2260 ext4fs_free_node(oldnode, currroot);
2261
2262 /* Found the node! */
2263 if (!next || *next == '\0') {
2264 *currfound = currnode;
2265 *foundtype = type;
2266 return 1;
2267 }
2268 name = next;
2269 }
2270 return -1;
2271}
2272
2273int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
2274 struct ext2fs_node **foundnode, int expecttype)
2275{
2276 int status;
2277 int foundtype = FILETYPE_DIRECTORY;
2278
2279 symlinknest = 0;
2280 if (!path)
2281 return 0;
2282
2283 status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
2284 if (status == 0)
2285 return 0;
2286
2287 /* Check if the node that was found was of the expected type. */
2288 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
2289 return 0;
2290 else if ((expecttype == FILETYPE_DIRECTORY)
2291 && (foundtype != expecttype))
2292 return 0;
2293
2294 return 1;
2295}
2296
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002297int ext4fs_open(const char *filename, loff_t *len)
Uma Shankar71014b62012-05-25 21:21:44 +05302298{
2299 struct ext2fs_node *fdiro = NULL;
2300 int status;
Uma Shankar71014b62012-05-25 21:21:44 +05302301
2302 if (ext4fs_root == NULL)
2303 return -1;
2304
2305 ext4fs_file = NULL;
2306 status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
2307 FILETYPE_REG);
2308 if (status == 0)
2309 goto fail;
2310
2311 if (!fdiro->inode_read) {
2312 status = ext4fs_read_inode(fdiro->data, fdiro->ino,
2313 &fdiro->inode);
2314 if (status == 0)
2315 goto fail;
2316 }
Michael Wallec07cdcb2016-08-29 10:46:44 +02002317 *len = le32_to_cpu(fdiro->inode.size);
Uma Shankar71014b62012-05-25 21:21:44 +05302318 ext4fs_file = fdiro;
2319
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08002320 return 0;
Uma Shankar71014b62012-05-25 21:21:44 +05302321fail:
2322 ext4fs_free_node(fdiro, &ext4fs_root->diropen);
2323
2324 return -1;
2325}
2326
2327int ext4fs_mount(unsigned part_length)
2328{
2329 struct ext2_data *data;
2330 int status;
2331 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-05-01 01:13:19 +00002332 data = zalloc(SUPERBLOCK_SIZE);
Uma Shankar71014b62012-05-25 21:21:44 +05302333 if (!data)
2334 return 0;
2335
2336 /* Read the superblock. */
Egbert Eich7b1b2552013-05-01 01:13:19 +00002337 status = ext4_read_superblock((char *)&data->sblock);
Uma Shankar71014b62012-05-25 21:21:44 +05302338
2339 if (status == 0)
2340 goto fail;
2341
2342 /* Make sure this is an ext2 filesystem. */
Michael Wallec07cdcb2016-08-29 10:46:44 +02002343 if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
Marek Behúnfe614ef2018-03-08 00:26:08 +01002344 goto fail_noerr;
Uma Shankar71014b62012-05-25 21:21:44 +05302345
Tom Rini44720652016-07-22 17:59:11 -04002346
Stefan Brünsb2c28a22016-09-17 02:10:07 +02002347 if (le32_to_cpu(data->sblock.revision_level) == 0) {
Uma Shankar71014b62012-05-25 21:21:44 +05302348 fs->inodesz = 128;
Stefan Brüns44632b72016-12-27 02:35:08 +01002349 fs->gdsize = 32;
Stefan Brünsb2c28a22016-09-17 02:10:07 +02002350 } else {
2351 debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
2352 __le32_to_cpu(data->sblock.feature_compatibility),
2353 __le32_to_cpu(data->sblock.feature_incompat),
2354 __le32_to_cpu(data->sblock.feature_ro_compat));
2355
Michael Wallec07cdcb2016-08-29 10:46:44 +02002356 fs->inodesz = le16_to_cpu(data->sblock.inode_size);
Stefan Brünsb2c28a22016-09-17 02:10:07 +02002357 fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
2358 EXT4_FEATURE_INCOMPAT_64BIT ?
2359 le16_to_cpu(data->sblock.descriptor_size) : 32;
2360 }
Uma Shankar71014b62012-05-25 21:21:44 +05302361
Stefan Brünsb2c28a22016-09-17 02:10:07 +02002362 debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
2363 le32_to_cpu(data->sblock.revision_level),
2364 fs->inodesz, fs->gdsize);
Uma Shankar71014b62012-05-25 21:21:44 +05302365
2366 data->diropen.data = data;
2367 data->diropen.ino = 2;
2368 data->diropen.inode_read = 1;
2369 data->inode = &data->diropen.inode;
2370
2371 status = ext4fs_read_inode(data, 2, data->inode);
2372 if (status == 0)
2373 goto fail;
2374
2375 ext4fs_root = data;
2376
2377 return 1;
2378fail:
2379 printf("Failed to mount ext2 filesystem...\n");
Marek Behúnfe614ef2018-03-08 00:26:08 +01002380fail_noerr:
Uma Shankar71014b62012-05-25 21:21:44 +05302381 free(data);
2382 ext4fs_root = NULL;
2383
2384 return 0;
2385}