blob: d710a86d59c263eb0e2492b920b7f4917db1d9f8 [file] [log] [blame]
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001/*
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 and load support in Uboot.
8 * Ext4 read optimization taken from Open-Moko
9 * Qi bootloader
10 *
11 * (C) Copyright 2004
12 * esd gmbh <www.esd-electronics.com>
13 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
14 *
15 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
16 * GRUB -- GRand Unified Bootloader
17 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
18 *
19 * ext4write : Based on generic ext4 protocol.
20 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020021 * SPDX-License-Identifier: GPL-2.0+
Simon Glass6c8f3aa2012-12-26 09:53:28 +000022 */
23
24
25#include <common.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060026#include <memalign.h>
Simon Glass6c8f3aa2012-12-26 09:53:28 +000027#include <linux/stat.h>
28#include <div64.h>
29#include "ext4_common.h"
30
Michael Walle13179c22016-09-01 11:21:40 +020031static inline void ext4fs_sb_free_inodes_inc(struct ext2_sblock *sb)
32{
33 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) + 1);
34}
35
36static inline void ext4fs_sb_free_blocks_inc(struct ext2_sblock *sb)
37{
38 sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) + 1);
39}
40
Stefan Brüns05313d12016-09-20 01:13:01 +020041static inline void ext4fs_bg_free_inodes_inc
42 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle13179c22016-09-01 11:21:40 +020043{
Stefan Brüns05313d12016-09-20 01:13:01 +020044 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
45 if (fs->gdsize == 64)
46 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
47 free_inodes++;
48
49 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
50 if (fs->gdsize == 64)
51 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
Michael Walle13179c22016-09-01 11:21:40 +020052}
53
Stefan Brüns05313d12016-09-20 01:13:01 +020054static inline void ext4fs_bg_free_blocks_inc
55 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle13179c22016-09-01 11:21:40 +020056{
Stefan Brüns05313d12016-09-20 01:13:01 +020057 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
58 if (fs->gdsize == 64)
59 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
60 free_blocks++;
61
62 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
63 if (fs->gdsize == 64)
64 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
Michael Walle13179c22016-09-01 11:21:40 +020065}
66
Simon Glass6c8f3aa2012-12-26 09:53:28 +000067static void ext4fs_update(void)
68{
69 short i;
70 ext4fs_update_journal();
71 struct ext_filesystem *fs = get_fs();
Stefan Brünsbad73812016-09-17 02:10:10 +020072 struct ext2_block_group *bgd = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +000073
74 /* update super block */
75 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
76 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
77
Stefan Brünsbad73812016-09-17 02:10:10 +020078 /* update block bitmaps */
Simon Glass6c8f3aa2012-12-26 09:53:28 +000079 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +020080 bgd = ext4fs_get_group_descriptor(fs, i);
81 bgd->bg_checksum = cpu_to_le16(ext4fs_checksum_update(i));
82 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
83 put_ext4(b_bitmap_blk * fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +000084 fs->blk_bmaps[i], fs->blksz);
85 }
86
Stefan Brünsbad73812016-09-17 02:10:10 +020087 /* update inode bitmaps */
Simon Glass6c8f3aa2012-12-26 09:53:28 +000088 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +020089 bgd = ext4fs_get_group_descriptor(fs, i);
90 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
91 put_ext4(i_bitmap_blk * fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +000092 fs->inode_bmaps[i], fs->blksz);
93 }
94
95 /* update the block group descriptor table */
Ma Haijune0996ca2014-01-08 08:15:33 +080096 put_ext4((uint64_t)((uint64_t)fs->gdtable_blkno * (uint64_t)fs->blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +000097 (struct ext2_block_group *)fs->gdtable,
98 (fs->blksz * fs->no_blk_pergdt));
99
100 ext4fs_dump_metadata();
101
102 gindex = 0;
103 gd_index = 0;
104}
105
106int ext4fs_get_bgdtable(void)
107{
108 int status;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000109 struct ext_filesystem *fs = get_fs();
Stefan Brünsbad73812016-09-17 02:10:10 +0200110 int gdsize_total = ROUND(fs->no_blkgrp * fs->gdsize, fs->blksz);
111 fs->no_blk_pergdt = gdsize_total / fs->blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000112
113 /* allocate memory for gdtable */
Stefan Brünsbad73812016-09-17 02:10:10 +0200114 fs->gdtable = zalloc(gdsize_total);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000115 if (!fs->gdtable)
116 return -ENOMEM;
117 /* read the group descriptor table */
Frederic Leroye7ee0282013-06-26 18:11:25 +0200118 status = ext4fs_devread((lbaint_t)fs->gdtable_blkno * fs->sect_perblk,
119 0, fs->blksz * fs->no_blk_pergdt, fs->gdtable);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000120 if (status == 0)
121 goto fail;
122
123 if (ext4fs_log_gdt(fs->gdtable)) {
124 printf("Error in ext4fs_log_gdt\n");
125 return -1;
126 }
127
128 return 0;
129fail:
130 free(fs->gdtable);
131 fs->gdtable = NULL;
132
133 return -1;
134}
135
136static void delete_single_indirect_block(struct ext2_inode *inode)
137{
138 struct ext2_block_group *bgd = NULL;
139 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200140 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000141 int remainder;
142 int bg_idx;
143 int status;
Michael Walle13179c22016-09-01 11:21:40 +0200144 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000145 struct ext_filesystem *fs = get_fs();
146 char *journal_buffer = zalloc(fs->blksz);
147 if (!journal_buffer) {
148 printf("No memory\n");
149 return;
150 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000151
152 /* deleting the single indirect block associated with inode */
153 if (inode->b.blocks.indir_block != 0) {
Michael Walle13179c22016-09-01 11:21:40 +0200154 blknr = le32_to_cpu(inode->b.blocks.indir_block);
155 debug("SIPB releasing %u\n", blknr);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200156 bg_idx = blknr / blk_per_grp;
157 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000158 remainder = blknr % blk_per_grp;
159 if (!remainder)
160 bg_idx--;
161 }
162 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brünsbad73812016-09-17 02:10:10 +0200163 /* get block group descriptor table */
164 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200165 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200166 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000167 /* journal backup */
168 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200169 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200170 status = ext4fs_devread(
Stefan Brünsbad73812016-09-17 02:10:10 +0200171 b_bitmap_blk * fs->sect_perblk,
172 0, fs->blksz, journal_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000173 if (status == 0)
174 goto fail;
Stefan Brünsbad73812016-09-17 02:10:10 +0200175 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000176 goto fail;
177 prev_bg_bmap_idx = bg_idx;
178 }
179 }
180fail:
181 free(journal_buffer);
182}
183
184static void delete_double_indirect_block(struct ext2_inode *inode)
185{
186 int i;
187 short status;
188 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200189 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000190 int remainder;
191 int bg_idx;
Michael Walle13179c22016-09-01 11:21:40 +0200192 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
193 __le32 *di_buffer = NULL;
194 void *dib_start_addr = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000195 struct ext2_block_group *bgd = NULL;
196 struct ext_filesystem *fs = get_fs();
197 char *journal_buffer = zalloc(fs->blksz);
198 if (!journal_buffer) {
199 printf("No memory\n");
200 return;
201 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000202
203 if (inode->b.blocks.double_indir_block != 0) {
204 di_buffer = zalloc(fs->blksz);
205 if (!di_buffer) {
206 printf("No memory\n");
207 return;
208 }
Michael Walle13179c22016-09-01 11:21:40 +0200209 dib_start_addr = di_buffer;
210 blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200211 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
212 fs->blksz, (char *)di_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000213 for (i = 0; i < fs->blksz / sizeof(int); i++) {
214 if (*di_buffer == 0)
215 break;
216
217 debug("DICB releasing %u\n", *di_buffer);
Michael Walle13179c22016-09-01 11:21:40 +0200218 bg_idx = le32_to_cpu(*di_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200219 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200220 remainder = le32_to_cpu(*di_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000221 if (!remainder)
222 bg_idx--;
223 }
Stefan Brünsbad73812016-09-17 02:10:10 +0200224 /* get block group descriptor table */
225 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Michael Walle13179c22016-09-01 11:21:40 +0200226 ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000227 fs->blk_bmaps[bg_idx], bg_idx);
228 di_buffer++;
Stefan Brüns05313d12016-09-20 01:13:01 +0200229 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200230 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000231 /* journal backup */
232 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200233 uint64_t b_bitmap_blk =
234 ext4fs_bg_get_block_id(bgd, fs);
235 status = ext4fs_devread(b_bitmap_blk
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000236 * fs->sect_perblk, 0,
237 fs->blksz,
238 journal_buffer);
239 if (status == 0)
240 goto fail;
241
242 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +0200243 b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000244 goto fail;
245 prev_bg_bmap_idx = bg_idx;
246 }
247 }
248
249 /* removing the parent double indirect block */
Michael Walle13179c22016-09-01 11:21:40 +0200250 blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200251 bg_idx = blknr / blk_per_grp;
252 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000253 remainder = blknr % blk_per_grp;
254 if (!remainder)
255 bg_idx--;
256 }
Stefan Brünsbad73812016-09-17 02:10:10 +0200257 /* get block group descriptor table */
258 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000259 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200260 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200261 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000262 /* journal backup */
263 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200264 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
265 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
266 0, fs->blksz, journal_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000267 if (status == 0)
268 goto fail;
269
Stefan Brünsbad73812016-09-17 02:10:10 +0200270 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000271 goto fail;
272 prev_bg_bmap_idx = bg_idx;
273 }
Michael Walle13179c22016-09-01 11:21:40 +0200274 debug("DIPB releasing %d\n", blknr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000275 }
276fail:
Michael Walle13179c22016-09-01 11:21:40 +0200277 free(dib_start_addr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000278 free(journal_buffer);
279}
280
281static void delete_triple_indirect_block(struct ext2_inode *inode)
282{
283 int i, j;
284 short status;
285 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200286 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000287 int remainder;
288 int bg_idx;
Michael Walle13179c22016-09-01 11:21:40 +0200289 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
290 __le32 *tigp_buffer = NULL;
291 void *tib_start_addr = NULL;
292 __le32 *tip_buffer = NULL;
293 void *tipb_start_addr = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000294 struct ext2_block_group *bgd = NULL;
295 struct ext_filesystem *fs = get_fs();
296 char *journal_buffer = zalloc(fs->blksz);
297 if (!journal_buffer) {
298 printf("No memory\n");
299 return;
300 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000301
302 if (inode->b.blocks.triple_indir_block != 0) {
303 tigp_buffer = zalloc(fs->blksz);
304 if (!tigp_buffer) {
305 printf("No memory\n");
306 return;
307 }
Michael Walle13179c22016-09-01 11:21:40 +0200308 tib_start_addr = tigp_buffer;
309 blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200310 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
311 fs->blksz, (char *)tigp_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000312 for (i = 0; i < fs->blksz / sizeof(int); i++) {
313 if (*tigp_buffer == 0)
314 break;
315 debug("tigp buffer releasing %u\n", *tigp_buffer);
316
317 tip_buffer = zalloc(fs->blksz);
318 if (!tip_buffer)
319 goto fail;
Michael Walle13179c22016-09-01 11:21:40 +0200320 tipb_start_addr = tip_buffer;
321 status = ext4fs_devread((lbaint_t)le32_to_cpu(*tigp_buffer) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000322 fs->sect_perblk, 0, fs->blksz,
323 (char *)tip_buffer);
324 for (j = 0; j < fs->blksz / sizeof(int); j++) {
Michael Walle13179c22016-09-01 11:21:40 +0200325 if (le32_to_cpu(*tip_buffer) == 0)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000326 break;
Michael Walle13179c22016-09-01 11:21:40 +0200327 bg_idx = le32_to_cpu(*tip_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200328 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200329 remainder = le32_to_cpu(*tip_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000330 if (!remainder)
331 bg_idx--;
332 }
333
Michael Walle13179c22016-09-01 11:21:40 +0200334 ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000335 fs->blk_bmaps[bg_idx],
336 bg_idx);
337
338 tip_buffer++;
Stefan Brünsbad73812016-09-17 02:10:10 +0200339 /* get block group descriptor table */
340 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200341 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200342 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000343 /* journal backup */
344 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200345 uint64_t b_bitmap_blk =
346 ext4fs_bg_get_block_id(bgd, fs);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000347 status =
348 ext4fs_devread(
Stefan Brünsbad73812016-09-17 02:10:10 +0200349 b_bitmap_blk *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000350 fs->sect_perblk, 0,
351 fs->blksz,
352 journal_buffer);
353 if (status == 0)
354 goto fail;
355
356 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +0200357 b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000358 goto fail;
359 prev_bg_bmap_idx = bg_idx;
360 }
361 }
362 free(tipb_start_addr);
363 tipb_start_addr = NULL;
364
365 /*
366 * removing the grand parent blocks
367 * which is connected to inode
368 */
Michael Walle13179c22016-09-01 11:21:40 +0200369 bg_idx = le32_to_cpu(*tigp_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200370 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200371 remainder = le32_to_cpu(*tigp_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000372 if (!remainder)
373 bg_idx--;
374 }
Michael Walle13179c22016-09-01 11:21:40 +0200375 ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000376 fs->blk_bmaps[bg_idx], bg_idx);
377
378 tigp_buffer++;
Stefan Brünsbad73812016-09-17 02:10:10 +0200379 /* get block group descriptor table */
380 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200381 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200382 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000383 /* journal backup */
384 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200385 uint64_t b_bitmap_blk =
386 ext4fs_bg_get_block_id(bgd, fs);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000387 memset(journal_buffer, '\0', fs->blksz);
Stefan Brünsbad73812016-09-17 02:10:10 +0200388 status = ext4fs_devread(b_bitmap_blk *
389 fs->sect_perblk, 0,
390 fs->blksz,
391 journal_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000392 if (status == 0)
393 goto fail;
394
395 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +0200396 b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000397 goto fail;
398 prev_bg_bmap_idx = bg_idx;
399 }
400 }
401
402 /* removing the grand parent triple indirect block */
Michael Walle13179c22016-09-01 11:21:40 +0200403 blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200404 bg_idx = blknr / blk_per_grp;
405 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000406 remainder = blknr % blk_per_grp;
407 if (!remainder)
408 bg_idx--;
409 }
410 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brünsbad73812016-09-17 02:10:10 +0200411 /* get block group descriptor table */
412 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200413 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200414 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000415 /* journal backup */
416 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200417 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
418 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
419 0, fs->blksz, journal_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000420 if (status == 0)
421 goto fail;
422
Stefan Brünsbad73812016-09-17 02:10:10 +0200423 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000424 goto fail;
425 prev_bg_bmap_idx = bg_idx;
426 }
Michael Walle13179c22016-09-01 11:21:40 +0200427 debug("tigp buffer itself releasing %d\n", blknr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000428 }
429fail:
430 free(tib_start_addr);
431 free(tipb_start_addr);
432 free(journal_buffer);
433}
434
435static int ext4fs_delete_file(int inodeno)
436{
437 struct ext2_inode inode;
438 short status;
439 int i;
440 int remainder;
441 long int blknr;
442 int bg_idx;
443 int ibmap_idx;
444 char *read_buffer = NULL;
445 char *start_block_address = NULL;
Michael Walle13179c22016-09-01 11:21:40 +0200446 uint32_t no_blocks;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000447
448 static int prev_bg_bmap_idx = -1;
449 unsigned int inodes_per_block;
Michael Walle13179c22016-09-01 11:21:40 +0200450 uint32_t blkno;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000451 unsigned int blkoff;
Michael Walle13179c22016-09-01 11:21:40 +0200452 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
453 uint32_t inode_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000454 struct ext2_inode *inode_buffer = NULL;
455 struct ext2_block_group *bgd = NULL;
456 struct ext_filesystem *fs = get_fs();
457 char *journal_buffer = zalloc(fs->blksz);
458 if (!journal_buffer)
459 return -ENOMEM;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000460 status = ext4fs_read_inode(ext4fs_root, inodeno, &inode);
461 if (status == 0)
462 goto fail;
463
464 /* read the block no allocated to a file */
Michael Walle13179c22016-09-01 11:21:40 +0200465 no_blocks = le32_to_cpu(inode.size) / fs->blksz;
466 if (le32_to_cpu(inode.size) % fs->blksz)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000467 no_blocks++;
468
469 if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) {
Stefan Brüns03527132016-09-06 04:36:54 +0200470 /* FIXME delete extent index blocks, i.e. eh_depth >= 1 */
471 struct ext4_extent_header *eh =
472 (struct ext4_extent_header *)
473 inode.b.blocks.dir_blocks;
474 debug("del: dep=%d entries=%d\n", eh->eh_depth, eh->eh_entries);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000475 } else {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000476 delete_single_indirect_block(&inode);
477 delete_double_indirect_block(&inode);
478 delete_triple_indirect_block(&inode);
Stefan Brüns03527132016-09-06 04:36:54 +0200479 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000480
Stefan Brüns03527132016-09-06 04:36:54 +0200481 /* release data blocks */
482 for (i = 0; i < no_blocks; i++) {
483 blknr = read_allocated_block(&inode, i);
Stefan Brüns74674ed2016-09-06 04:36:55 +0200484 if (blknr == 0)
485 continue;
486 if (blknr < 0)
487 goto fail;
Stefan Brüns03527132016-09-06 04:36:54 +0200488 bg_idx = blknr / blk_per_grp;
489 if (fs->blksz == 1024) {
490 remainder = blknr % blk_per_grp;
491 if (!remainder)
492 bg_idx--;
493 }
494 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx],
495 bg_idx);
496 debug("EXT4 Block releasing %ld: %d\n", blknr, bg_idx);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000497
Stefan Brünsbad73812016-09-17 02:10:10 +0200498 /* get block group descriptor table */
499 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200500 ext4fs_bg_free_blocks_inc(bgd, fs);
Stefan Brüns03527132016-09-06 04:36:54 +0200501 ext4fs_sb_free_blocks_inc(fs->sb);
502 /* journal backup */
503 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200504 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
505 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Stefan Brüns03527132016-09-06 04:36:54 +0200506 0, fs->blksz,
507 journal_buffer);
508 if (status == 0)
509 goto fail;
Stefan Brünsbad73812016-09-17 02:10:10 +0200510 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Stefan Brüns03527132016-09-06 04:36:54 +0200511 goto fail;
512 prev_bg_bmap_idx = bg_idx;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000513 }
514 }
515
Stefan Brüns03527132016-09-06 04:36:54 +0200516 /* release inode */
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000517 /* from the inode no to blockno */
518 inodes_per_block = fs->blksz / fs->inodesz;
519 ibmap_idx = inodeno / inode_per_grp;
520
521 /* get the block no */
522 inodeno--;
Stefan Brünsbad73812016-09-17 02:10:10 +0200523 /* get block group descriptor table */
524 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
525 blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Michael Walle13179c22016-09-01 11:21:40 +0200526 (inodeno % inode_per_grp) / inodes_per_block;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000527
528 /* get the offset of the inode */
529 blkoff = ((inodeno) % inodes_per_block) * fs->inodesz;
530
531 /* read the block no containing the inode */
532 read_buffer = zalloc(fs->blksz);
533 if (!read_buffer)
534 goto fail;
535 start_block_address = read_buffer;
Frederic Leroye7ee0282013-06-26 18:11:25 +0200536 status = ext4fs_devread((lbaint_t)blkno * fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000537 0, fs->blksz, read_buffer);
538 if (status == 0)
539 goto fail;
540
541 if (ext4fs_log_journal(read_buffer, blkno))
542 goto fail;
543
544 read_buffer = read_buffer + blkoff;
545 inode_buffer = (struct ext2_inode *)read_buffer;
Stefan Brüns7d936db2016-09-06 04:36:53 +0200546 memset(inode_buffer, '\0', fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000547
548 /* write the inode to original position in inode table */
549 if (ext4fs_put_metadata(start_block_address, blkno))
550 goto fail;
551
552 /* update the respective inode bitmaps */
553 inodeno++;
554 ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200555 ext4fs_bg_free_inodes_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200556 ext4fs_sb_free_inodes_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000557 /* journal backup */
558 memset(journal_buffer, '\0', fs->blksz);
Stefan Brünsbad73812016-09-17 02:10:10 +0200559 status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000560 fs->sect_perblk, 0, fs->blksz, journal_buffer);
561 if (status == 0)
562 goto fail;
Stefan Brünsbad73812016-09-17 02:10:10 +0200563 if (ext4fs_log_journal(journal_buffer, ext4fs_bg_get_inode_id(bgd, fs)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000564 goto fail;
565
566 ext4fs_update();
567 ext4fs_deinit();
Łukasz Majewski900db5d2014-05-06 09:36:05 +0200568 ext4fs_reinit_global();
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000569
570 if (ext4fs_init() != 0) {
571 printf("error in File System init\n");
572 goto fail;
573 }
574
575 free(start_block_address);
576 free(journal_buffer);
577
578 return 0;
579fail:
580 free(start_block_address);
581 free(journal_buffer);
582
583 return -1;
584}
585
586int ext4fs_init(void)
587{
588 short status;
589 int i;
Michael Walle13179c22016-09-01 11:21:40 +0200590 uint32_t real_free_blocks = 0;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000591 struct ext_filesystem *fs = get_fs();
592
593 /* populate fs */
594 fs->blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich7b1b2552013-05-01 01:13:19 +0000595 fs->sect_perblk = fs->blksz >> fs->dev_desc->log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000596
597 /* get the superblock */
598 fs->sb = zalloc(SUPERBLOCK_SIZE);
599 if (!fs->sb)
600 return -ENOMEM;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000601 if (!ext4_read_superblock((char *)fs->sb))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000602 goto fail;
603
604 /* init journal */
605 if (ext4fs_init_journal())
606 goto fail;
607
608 /* get total no of blockgroups */
609 fs->no_blkgrp = (uint32_t)ext4fs_div_roundup(
Michael Walle13179c22016-09-01 11:21:40 +0200610 le32_to_cpu(ext4fs_root->sblock.total_blocks)
611 - le32_to_cpu(ext4fs_root->sblock.first_data_block),
612 le32_to_cpu(ext4fs_root->sblock.blocks_per_group));
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000613
614 /* get the block group descriptor table */
615 fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1);
616 if (ext4fs_get_bgdtable() == -1) {
617 printf("Error in getting the block group descriptor table\n");
618 goto fail;
619 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000620
621 /* load all the available bitmap block of the partition */
622 fs->blk_bmaps = zalloc(fs->no_blkgrp * sizeof(char *));
623 if (!fs->blk_bmaps)
624 goto fail;
625 for (i = 0; i < fs->no_blkgrp; i++) {
626 fs->blk_bmaps[i] = zalloc(fs->blksz);
627 if (!fs->blk_bmaps[i])
628 goto fail;
629 }
630
631 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200632 struct ext2_block_group *bgd =
633 ext4fs_get_group_descriptor(fs, i);
634 status = ext4fs_devread(ext4fs_bg_get_block_id(bgd, fs) *
Frederic Leroye7ee0282013-06-26 18:11:25 +0200635 fs->sect_perblk, 0,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000636 fs->blksz, (char *)fs->blk_bmaps[i]);
637 if (status == 0)
638 goto fail;
639 }
640
641 /* load all the available inode bitmap of the partition */
642 fs->inode_bmaps = zalloc(fs->no_blkgrp * sizeof(unsigned char *));
643 if (!fs->inode_bmaps)
644 goto fail;
645 for (i = 0; i < fs->no_blkgrp; i++) {
646 fs->inode_bmaps[i] = zalloc(fs->blksz);
647 if (!fs->inode_bmaps[i])
648 goto fail;
649 }
650
651 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200652 struct ext2_block_group *bgd =
653 ext4fs_get_group_descriptor(fs, i);
654 status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) *
Frederic Leroye7ee0282013-06-26 18:11:25 +0200655 fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000656 0, fs->blksz,
657 (char *)fs->inode_bmaps[i]);
658 if (status == 0)
659 goto fail;
660 }
661
662 /*
663 * check filesystem consistency with free blocks of file system
664 * some time we observed that superblock freeblocks does not match
665 * with the blockgroups freeblocks when improper
666 * reboot of a linux kernel
667 */
Stefan Brünsbad73812016-09-17 02:10:10 +0200668 for (i = 0; i < fs->no_blkgrp; i++) {
669 struct ext2_block_group *bgd =
670 ext4fs_get_group_descriptor(fs, i);
671 real_free_blocks = real_free_blocks +
672 ext4fs_bg_get_free_blocks(bgd, fs);
673 }
674 if (real_free_blocks != ext4fs_sb_get_free_blocks(fs->sb))
675 ext4fs_sb_set_free_blocks(fs->sb, real_free_blocks);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000676
677 return 0;
678fail:
679 ext4fs_deinit();
680
681 return -1;
682}
683
684void ext4fs_deinit(void)
685{
686 int i;
687 struct ext2_inode inode_journal;
688 struct journal_superblock_t *jsb;
Michael Walle13179c22016-09-01 11:21:40 +0200689 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000690 struct ext_filesystem *fs = get_fs();
Michael Walle13179c22016-09-01 11:21:40 +0200691 uint32_t new_feature_incompat;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000692
693 /* free journal */
694 char *temp_buff = zalloc(fs->blksz);
695 if (temp_buff) {
696 ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
697 &inode_journal);
698 blknr = read_allocated_block(&inode_journal,
699 EXT2_JOURNAL_SUPERBLOCK);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200700 ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000701 temp_buff);
702 jsb = (struct journal_superblock_t *)temp_buff;
Michael Walle13179c22016-09-01 11:21:40 +0200703 jsb->s_start = 0;
Ma Haijune0996ca2014-01-08 08:15:33 +0800704 put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000705 (struct journal_superblock_t *)temp_buff, fs->blksz);
706 free(temp_buff);
707 }
708 ext4fs_free_journal();
709
710 /* get the superblock */
Egbert Eich7b1b2552013-05-01 01:13:19 +0000711 ext4_read_superblock((char *)fs->sb);
Michael Walle13179c22016-09-01 11:21:40 +0200712 new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat);
713 new_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
714 fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000715 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
716 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
717 free(fs->sb);
718 fs->sb = NULL;
719
720 if (fs->blk_bmaps) {
721 for (i = 0; i < fs->no_blkgrp; i++) {
722 free(fs->blk_bmaps[i]);
723 fs->blk_bmaps[i] = NULL;
724 }
725 free(fs->blk_bmaps);
726 fs->blk_bmaps = NULL;
727 }
728
729 if (fs->inode_bmaps) {
730 for (i = 0; i < fs->no_blkgrp; i++) {
731 free(fs->inode_bmaps[i]);
732 fs->inode_bmaps[i] = NULL;
733 }
734 free(fs->inode_bmaps);
735 fs->inode_bmaps = NULL;
736 }
737
738
739 free(fs->gdtable);
740 fs->gdtable = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000741 /*
742 * reinitiliazed the global inode and
743 * block bitmap first execution check variables
744 */
745 fs->first_pass_ibmap = 0;
746 fs->first_pass_bbmap = 0;
747 fs->curr_inode_no = 0;
748 fs->curr_blkno = 0;
749}
750
Stefan Brüns74674ed2016-09-06 04:36:55 +0200751/*
752 * Write data to filesystem blocks. Uses same optimization for
753 * contigous sectors as ext4fs_read_file
754 */
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000755static int ext4fs_write_file(struct ext2_inode *file_inode,
756 int pos, unsigned int len, char *buf)
757{
758 int i;
759 int blockcnt;
Michael Walle13179c22016-09-01 11:21:40 +0200760 uint32_t filesize = le32_to_cpu(file_inode->size);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000761 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-05-01 01:13:19 +0000762 int log2blksz = fs->dev_desc->log2blksz;
763 int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000764 int previous_block_number = -1;
765 int delayed_start = 0;
766 int delayed_extent = 0;
767 int delayed_next = 0;
768 char *delayed_buf = NULL;
769
770 /* Adjust len so it we can't read past the end of the file. */
771 if (len > filesize)
772 len = filesize;
773
774 blockcnt = ((len + pos) + fs->blksz - 1) / fs->blksz;
775
776 for (i = pos / fs->blksz; i < blockcnt; i++) {
777 long int blknr;
778 int blockend = fs->blksz;
779 int skipfirst = 0;
780 blknr = read_allocated_block(file_inode, i);
Stefan Brüns74674ed2016-09-06 04:36:55 +0200781 if (blknr <= 0)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000782 return -1;
783
Egbert Eich7b1b2552013-05-01 01:13:19 +0000784 blknr = blknr << log2_fs_blocksize;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000785
786 if (blknr) {
787 if (previous_block_number != -1) {
788 if (delayed_next == blknr) {
789 delayed_extent += blockend;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000790 delayed_next += blockend >> log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000791 } else { /* spill */
Egbert Eich7b1b2552013-05-01 01:13:19 +0000792 put_ext4((uint64_t)
Ma Haijune0996ca2014-01-08 08:15:33 +0800793 ((uint64_t)delayed_start << log2blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000794 delayed_buf,
795 (uint32_t) delayed_extent);
796 previous_block_number = blknr;
797 delayed_start = blknr;
798 delayed_extent = blockend;
799 delayed_buf = buf;
800 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000801 (blockend >> log2blksz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000802 }
803 } else {
804 previous_block_number = blknr;
805 delayed_start = blknr;
806 delayed_extent = blockend;
807 delayed_buf = buf;
808 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000809 (blockend >> log2blksz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000810 }
811 } else {
812 if (previous_block_number != -1) {
813 /* spill */
Ma Haijune0996ca2014-01-08 08:15:33 +0800814 put_ext4((uint64_t) ((uint64_t)delayed_start <<
Egbert Eich7b1b2552013-05-01 01:13:19 +0000815 log2blksz),
816 delayed_buf,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000817 (uint32_t) delayed_extent);
818 previous_block_number = -1;
819 }
820 memset(buf, 0, fs->blksz - skipfirst);
821 }
822 buf += fs->blksz - skipfirst;
823 }
824 if (previous_block_number != -1) {
825 /* spill */
Ma Haijune0996ca2014-01-08 08:15:33 +0800826 put_ext4((uint64_t) ((uint64_t)delayed_start << log2blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000827 delayed_buf, (uint32_t) delayed_extent);
828 previous_block_number = -1;
829 }
830
831 return len;
832}
833
834int ext4fs_write(const char *fname, unsigned char *buffer,
835 unsigned long sizebytes)
836{
837 int ret = 0;
838 struct ext2_inode *file_inode = NULL;
839 unsigned char *inode_buffer = NULL;
840 int parent_inodeno;
841 int inodeno;
842 time_t timestamp = 0;
843
844 uint64_t bytes_reqd_for_file;
845 unsigned int blks_reqd_for_file;
846 unsigned int blocks_remaining;
847 int existing_file_inodeno;
848 char *temp_ptr = NULL;
849 long int itable_blkno;
850 long int parent_itable_blkno;
851 long int blkoff;
852 struct ext2_sblock *sblock = &(ext4fs_root->sblock);
853 unsigned int inodes_per_block;
854 unsigned int ibmap_idx;
Stefan Brünsbad73812016-09-17 02:10:10 +0200855 struct ext2_block_group *bgd = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000856 struct ext_filesystem *fs = get_fs();
857 ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256);
Jeroen Hofsteef978b002014-06-09 15:29:00 +0200858 memset(filename, 0x00, 256);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000859
Stefan Brüns7d936db2016-09-06 04:36:53 +0200860 g_parent_inode = zalloc(fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000861 if (!g_parent_inode)
862 goto fail;
863
864 if (ext4fs_init() != 0) {
865 printf("error in File System init\n");
866 return -1;
867 }
868 inodes_per_block = fs->blksz / fs->inodesz;
869 parent_inodeno = ext4fs_get_parent_inode_num(fname, filename, F_FILE);
870 if (parent_inodeno == -1)
871 goto fail;
872 if (ext4fs_iget(parent_inodeno, g_parent_inode))
873 goto fail;
Stefan Brüns57db8992016-09-06 04:36:45 +0200874 /* do not mess up a directory using hash trees */
875 if (le32_to_cpu(g_parent_inode->flags) & EXT4_INDEX_FL) {
876 printf("hash tree directory\n");
877 goto fail;
878 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000879 /* check if the filename is already present in root */
Stefan Brüns278f5d32016-09-06 04:36:41 +0200880 existing_file_inodeno = ext4fs_filename_unlink(filename);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000881 if (existing_file_inodeno != -1) {
882 ret = ext4fs_delete_file(existing_file_inodeno);
883 fs->first_pass_bbmap = 0;
884 fs->curr_blkno = 0;
885
886 fs->first_pass_ibmap = 0;
887 fs->curr_inode_no = 0;
888 if (ret)
889 goto fail;
890 }
891 /* calucalate how many blocks required */
892 bytes_reqd_for_file = sizebytes;
893 blks_reqd_for_file = lldiv(bytes_reqd_for_file, fs->blksz);
894 if (do_div(bytes_reqd_for_file, fs->blksz) != 0) {
895 blks_reqd_for_file++;
896 debug("total bytes for a file %u\n", blks_reqd_for_file);
897 }
898 blocks_remaining = blks_reqd_for_file;
899 /* test for available space in partition */
Michael Walle13179c22016-09-01 11:21:40 +0200900 if (le32_to_cpu(fs->sb->free_blocks) < blks_reqd_for_file) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000901 printf("Not enough space on partition !!!\n");
902 goto fail;
903 }
904
Stefan Brünsc1020682016-09-06 04:36:42 +0200905 inodeno = ext4fs_update_parent_dentry(filename, FILETYPE_REG);
906 if (inodeno == -1)
907 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000908 /* prepare file inode */
909 inode_buffer = zalloc(fs->inodesz);
910 if (!inode_buffer)
911 goto fail;
912 file_inode = (struct ext2_inode *)inode_buffer;
Michael Walle13179c22016-09-01 11:21:40 +0200913 file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU |
914 S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000915 /* ToDo: Update correct time */
Michael Walle13179c22016-09-01 11:21:40 +0200916 file_inode->mtime = cpu_to_le32(timestamp);
917 file_inode->atime = cpu_to_le32(timestamp);
918 file_inode->ctime = cpu_to_le32(timestamp);
919 file_inode->nlinks = cpu_to_le16(1);
920 file_inode->size = cpu_to_le32(sizebytes);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000921
922 /* Allocate data blocks */
923 ext4fs_allocate_blocks(file_inode, blocks_remaining,
924 &blks_reqd_for_file);
Michael Walle13179c22016-09-01 11:21:40 +0200925 file_inode->blockcnt = cpu_to_le32((blks_reqd_for_file * fs->blksz) >>
926 fs->dev_desc->log2blksz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000927
928 temp_ptr = zalloc(fs->blksz);
929 if (!temp_ptr)
930 goto fail;
Michael Walle13179c22016-09-01 11:21:40 +0200931 ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000932 inodeno--;
Stefan Brünsbad73812016-09-17 02:10:10 +0200933 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
934 itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Michael Wallec07cdcb2016-08-29 10:46:44 +0200935 (inodeno % le32_to_cpu(sblock->inodes_per_group)) /
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000936 inodes_per_block;
937 blkoff = (inodeno % inodes_per_block) * fs->inodesz;
Frederic Leroye7ee0282013-06-26 18:11:25 +0200938 ext4fs_devread((lbaint_t)itable_blkno * fs->sect_perblk, 0, fs->blksz,
939 temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000940 if (ext4fs_log_journal(temp_ptr, itable_blkno))
941 goto fail;
942
943 memcpy(temp_ptr + blkoff, inode_buffer, fs->inodesz);
944 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
945 goto fail;
946 /* copy the file content into data blocks */
947 if (ext4fs_write_file(file_inode, 0, sizebytes, (char *)buffer) == -1) {
948 printf("Error in copying content\n");
Stefan Brüns74674ed2016-09-06 04:36:55 +0200949 /* FIXME: Deallocate data blocks */
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000950 goto fail;
951 }
Michael Walle13179c22016-09-01 11:21:40 +0200952 ibmap_idx = parent_inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000953 parent_inodeno--;
Stefan Brünsbad73812016-09-17 02:10:10 +0200954 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
955 parent_itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000956 (parent_inodeno %
Michael Wallec07cdcb2016-08-29 10:46:44 +0200957 le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000958 blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz;
959 if (parent_itable_blkno != itable_blkno) {
960 memset(temp_ptr, '\0', fs->blksz);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200961 ext4fs_devread((lbaint_t)parent_itable_blkno * fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000962 0, fs->blksz, temp_ptr);
963 if (ext4fs_log_journal(temp_ptr, parent_itable_blkno))
964 goto fail;
965
Stefan Brüns7d936db2016-09-06 04:36:53 +0200966 memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000967 if (ext4fs_put_metadata(temp_ptr, parent_itable_blkno))
968 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000969 } else {
970 /*
971 * If parent and child fall in same inode table block
972 * both should be kept in 1 buffer
973 */
Stefan Brüns7d936db2016-09-06 04:36:53 +0200974 memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000975 gd_index--;
976 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
977 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000978 }
979 ext4fs_update();
980 ext4fs_deinit();
981
982 fs->first_pass_bbmap = 0;
983 fs->curr_blkno = 0;
984 fs->first_pass_ibmap = 0;
985 fs->curr_inode_no = 0;
986 free(inode_buffer);
987 free(g_parent_inode);
Stefan Brüns338a5292016-09-06 04:36:51 +0200988 free(temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000989 g_parent_inode = NULL;
990
991 return 0;
992fail:
993 ext4fs_deinit();
994 free(inode_buffer);
995 free(g_parent_inode);
Stefan Brüns338a5292016-09-06 04:36:51 +0200996 free(temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000997 g_parent_inode = NULL;
998
999 return -1;
1000}
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001001
1002int ext4_write_file(const char *filename, void *buf, loff_t offset,
1003 loff_t len, loff_t *actwrite)
1004{
1005 int ret;
1006
1007 if (offset != 0) {
1008 printf("** Cannot support non-zero offset **\n");
1009 return -1;
1010 }
1011
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001012 ret = ext4fs_write(filename, buf, len);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001013 if (ret) {
1014 printf("** Error ext4fs_write() **\n");
1015 goto fail;
1016 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001017
Przemyslaw Marczak2b1d6c92015-02-17 15:31:52 +01001018 *actwrite = len;
1019
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001020 return 0;
1021
1022fail:
Przemyslaw Marczak2b1d6c92015-02-17 15:31:52 +01001023 *actwrite = 0;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001024
1025 return -1;
1026}