blob: b5b7ee813369a546d6ec0f04be044fd115696c0c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass293d7fb2012-12-26 09:53:28 +00002/*
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 and load support in Uboot.
9 * Ext4 read optimization taken from Open-Moko
10 * Qi bootloader
11 *
12 * (C) Copyright 2004
13 * esd gmbh <www.esd-electronics.com>
14 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
15 *
16 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
17 * GRUB -- GRand Unified Bootloader
18 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
19 *
20 * ext4write : Based on generic ext4 protocol.
Simon Glass293d7fb2012-12-26 09:53:28 +000021 */
22
23
24#include <common.h>
Simon Glasscf92e052015-09-02 17:24:58 -060025#include <memalign.h>
Simon Glass293d7fb2012-12-26 09:53:28 +000026#include <linux/stat.h>
27#include <div64.h>
28#include "ext4_common.h"
29
Michael Walle58a9ecb2016-09-01 11:21:40 +020030static inline void ext4fs_sb_free_inodes_inc(struct ext2_sblock *sb)
31{
32 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) + 1);
33}
34
35static inline void ext4fs_sb_free_blocks_inc(struct ext2_sblock *sb)
36{
37 sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) + 1);
38}
39
Stefan Brüns749e93e2016-09-20 01:13:01 +020040static inline void ext4fs_bg_free_inodes_inc
41 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020042{
Stefan Brüns749e93e2016-09-20 01:13:01 +020043 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
44 if (fs->gdsize == 64)
45 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
46 free_inodes++;
47
48 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
49 if (fs->gdsize == 64)
50 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020051}
52
Stefan Brüns749e93e2016-09-20 01:13:01 +020053static inline void ext4fs_bg_free_blocks_inc
54 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle58a9ecb2016-09-01 11:21:40 +020055{
Stefan Brüns749e93e2016-09-20 01:13:01 +020056 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
57 if (fs->gdsize == 64)
58 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
59 free_blocks++;
60
61 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
62 if (fs->gdsize == 64)
63 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
Michael Walle58a9ecb2016-09-01 11:21:40 +020064}
65
Simon Glass293d7fb2012-12-26 09:53:28 +000066static void ext4fs_update(void)
67{
68 short i;
69 ext4fs_update_journal();
70 struct ext_filesystem *fs = get_fs();
Stefan Brüns688d0e72016-09-17 02:10:10 +020071 struct ext2_block_group *bgd = NULL;
Simon Glass293d7fb2012-12-26 09:53:28 +000072
73 /* update super block */
74 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
75 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
76
Stefan Brüns688d0e72016-09-17 02:10:10 +020077 /* update block bitmaps */
Simon Glass293d7fb2012-12-26 09:53:28 +000078 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +020079 bgd = ext4fs_get_group_descriptor(fs, i);
80 bgd->bg_checksum = cpu_to_le16(ext4fs_checksum_update(i));
81 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
82 put_ext4(b_bitmap_blk * fs->blksz,
Simon Glass293d7fb2012-12-26 09:53:28 +000083 fs->blk_bmaps[i], fs->blksz);
84 }
85
Stefan Brüns688d0e72016-09-17 02:10:10 +020086 /* update inode bitmaps */
Simon Glass293d7fb2012-12-26 09:53:28 +000087 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +020088 bgd = ext4fs_get_group_descriptor(fs, i);
89 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
90 put_ext4(i_bitmap_blk * fs->blksz,
Simon Glass293d7fb2012-12-26 09:53:28 +000091 fs->inode_bmaps[i], fs->blksz);
92 }
93
94 /* update the block group descriptor table */
Ma Haijun05508702014-01-08 08:15:33 +080095 put_ext4((uint64_t)((uint64_t)fs->gdtable_blkno * (uint64_t)fs->blksz),
Simon Glass293d7fb2012-12-26 09:53:28 +000096 (struct ext2_block_group *)fs->gdtable,
97 (fs->blksz * fs->no_blk_pergdt));
98
99 ext4fs_dump_metadata();
100
101 gindex = 0;
102 gd_index = 0;
103}
104
105int ext4fs_get_bgdtable(void)
106{
107 int status;
Simon Glass293d7fb2012-12-26 09:53:28 +0000108 struct ext_filesystem *fs = get_fs();
Stefan Brüns688d0e72016-09-17 02:10:10 +0200109 int gdsize_total = ROUND(fs->no_blkgrp * fs->gdsize, fs->blksz);
110 fs->no_blk_pergdt = gdsize_total / fs->blksz;
Simon Glass293d7fb2012-12-26 09:53:28 +0000111
112 /* allocate memory for gdtable */
Stefan Brüns688d0e72016-09-17 02:10:10 +0200113 fs->gdtable = zalloc(gdsize_total);
Simon Glass293d7fb2012-12-26 09:53:28 +0000114 if (!fs->gdtable)
115 return -ENOMEM;
116 /* read the group descriptor table */
Frederic Leroy04735e92013-06-26 18:11:25 +0200117 status = ext4fs_devread((lbaint_t)fs->gdtable_blkno * fs->sect_perblk,
118 0, fs->blksz * fs->no_blk_pergdt, fs->gdtable);
Simon Glass293d7fb2012-12-26 09:53:28 +0000119 if (status == 0)
120 goto fail;
121
122 if (ext4fs_log_gdt(fs->gdtable)) {
123 printf("Error in ext4fs_log_gdt\n");
124 return -1;
125 }
126
127 return 0;
128fail:
129 free(fs->gdtable);
130 fs->gdtable = NULL;
131
132 return -1;
133}
134
135static void delete_single_indirect_block(struct ext2_inode *inode)
136{
137 struct ext2_block_group *bgd = NULL;
138 static int prev_bg_bmap_idx = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200139 uint32_t blknr;
Simon Glass293d7fb2012-12-26 09:53:28 +0000140 int remainder;
141 int bg_idx;
142 int status;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200143 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Simon Glass293d7fb2012-12-26 09:53:28 +0000144 struct ext_filesystem *fs = get_fs();
145 char *journal_buffer = zalloc(fs->blksz);
146 if (!journal_buffer) {
147 printf("No memory\n");
148 return;
149 }
Simon Glass293d7fb2012-12-26 09:53:28 +0000150
151 /* deleting the single indirect block associated with inode */
152 if (inode->b.blocks.indir_block != 0) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200153 blknr = le32_to_cpu(inode->b.blocks.indir_block);
154 debug("SIPB releasing %u\n", blknr);
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200155 bg_idx = blknr / blk_per_grp;
156 if (fs->blksz == 1024) {
Simon Glass293d7fb2012-12-26 09:53:28 +0000157 remainder = blknr % blk_per_grp;
158 if (!remainder)
159 bg_idx--;
160 }
161 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200162 /* get block group descriptor table */
163 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns749e93e2016-09-20 01:13:01 +0200164 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200165 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass293d7fb2012-12-26 09:53:28 +0000166 /* journal backup */
167 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200168 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200169 status = ext4fs_devread(
Stefan Brüns688d0e72016-09-17 02:10:10 +0200170 b_bitmap_blk * fs->sect_perblk,
171 0, fs->blksz, journal_buffer);
Simon Glass293d7fb2012-12-26 09:53:28 +0000172 if (status == 0)
173 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200174 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass293d7fb2012-12-26 09:53:28 +0000175 goto fail;
176 prev_bg_bmap_idx = bg_idx;
177 }
178 }
179fail:
180 free(journal_buffer);
181}
182
183static void delete_double_indirect_block(struct ext2_inode *inode)
184{
185 int i;
186 short status;
187 static int prev_bg_bmap_idx = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200188 uint32_t blknr;
Simon Glass293d7fb2012-12-26 09:53:28 +0000189 int remainder;
190 int bg_idx;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200191 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
192 __le32 *di_buffer = NULL;
193 void *dib_start_addr = NULL;
Simon Glass293d7fb2012-12-26 09:53:28 +0000194 struct ext2_block_group *bgd = NULL;
195 struct ext_filesystem *fs = get_fs();
196 char *journal_buffer = zalloc(fs->blksz);
197 if (!journal_buffer) {
198 printf("No memory\n");
199 return;
200 }
Simon Glass293d7fb2012-12-26 09:53:28 +0000201
202 if (inode->b.blocks.double_indir_block != 0) {
203 di_buffer = zalloc(fs->blksz);
204 if (!di_buffer) {
205 printf("No memory\n");
206 return;
207 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200208 dib_start_addr = di_buffer;
209 blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
Frederic Leroy04735e92013-06-26 18:11:25 +0200210 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
211 fs->blksz, (char *)di_buffer);
Simon Glass293d7fb2012-12-26 09:53:28 +0000212 for (i = 0; i < fs->blksz / sizeof(int); i++) {
213 if (*di_buffer == 0)
214 break;
215
216 debug("DICB releasing %u\n", *di_buffer);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200217 bg_idx = le32_to_cpu(*di_buffer) / blk_per_grp;
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200218 if (fs->blksz == 1024) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200219 remainder = le32_to_cpu(*di_buffer) % blk_per_grp;
Simon Glass293d7fb2012-12-26 09:53:28 +0000220 if (!remainder)
221 bg_idx--;
222 }
Stefan Brüns688d0e72016-09-17 02:10:10 +0200223 /* get block group descriptor table */
224 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200225 ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer),
Simon Glass293d7fb2012-12-26 09:53:28 +0000226 fs->blk_bmaps[bg_idx], bg_idx);
227 di_buffer++;
Stefan Brüns749e93e2016-09-20 01:13:01 +0200228 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200229 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass293d7fb2012-12-26 09:53:28 +0000230 /* journal backup */
231 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200232 uint64_t b_bitmap_blk =
233 ext4fs_bg_get_block_id(bgd, fs);
234 status = ext4fs_devread(b_bitmap_blk
Simon Glass293d7fb2012-12-26 09:53:28 +0000235 * fs->sect_perblk, 0,
236 fs->blksz,
237 journal_buffer);
238 if (status == 0)
239 goto fail;
240
241 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +0200242 b_bitmap_blk))
Simon Glass293d7fb2012-12-26 09:53:28 +0000243 goto fail;
244 prev_bg_bmap_idx = bg_idx;
245 }
246 }
247
248 /* removing the parent double indirect block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200249 blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200250 bg_idx = blknr / blk_per_grp;
251 if (fs->blksz == 1024) {
Simon Glass293d7fb2012-12-26 09:53:28 +0000252 remainder = blknr % blk_per_grp;
253 if (!remainder)
254 bg_idx--;
255 }
Stefan Brüns688d0e72016-09-17 02:10:10 +0200256 /* get block group descriptor table */
257 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Simon Glass293d7fb2012-12-26 09:53:28 +0000258 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brüns749e93e2016-09-20 01:13:01 +0200259 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200260 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass293d7fb2012-12-26 09:53:28 +0000261 /* journal backup */
262 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200263 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
264 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
265 0, fs->blksz, journal_buffer);
Simon Glass293d7fb2012-12-26 09:53:28 +0000266 if (status == 0)
267 goto fail;
268
Stefan Brüns688d0e72016-09-17 02:10:10 +0200269 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass293d7fb2012-12-26 09:53:28 +0000270 goto fail;
271 prev_bg_bmap_idx = bg_idx;
272 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200273 debug("DIPB releasing %d\n", blknr);
Simon Glass293d7fb2012-12-26 09:53:28 +0000274 }
275fail:
Michael Walle58a9ecb2016-09-01 11:21:40 +0200276 free(dib_start_addr);
Simon Glass293d7fb2012-12-26 09:53:28 +0000277 free(journal_buffer);
278}
279
280static void delete_triple_indirect_block(struct ext2_inode *inode)
281{
282 int i, j;
283 short status;
284 static int prev_bg_bmap_idx = -1;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200285 uint32_t blknr;
Simon Glass293d7fb2012-12-26 09:53:28 +0000286 int remainder;
287 int bg_idx;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200288 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
289 __le32 *tigp_buffer = NULL;
290 void *tib_start_addr = NULL;
291 __le32 *tip_buffer = NULL;
292 void *tipb_start_addr = NULL;
Simon Glass293d7fb2012-12-26 09:53:28 +0000293 struct ext2_block_group *bgd = NULL;
294 struct ext_filesystem *fs = get_fs();
295 char *journal_buffer = zalloc(fs->blksz);
296 if (!journal_buffer) {
297 printf("No memory\n");
298 return;
299 }
Simon Glass293d7fb2012-12-26 09:53:28 +0000300
301 if (inode->b.blocks.triple_indir_block != 0) {
302 tigp_buffer = zalloc(fs->blksz);
303 if (!tigp_buffer) {
304 printf("No memory\n");
305 return;
306 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200307 tib_start_addr = tigp_buffer;
308 blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
Frederic Leroy04735e92013-06-26 18:11:25 +0200309 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
310 fs->blksz, (char *)tigp_buffer);
Simon Glass293d7fb2012-12-26 09:53:28 +0000311 for (i = 0; i < fs->blksz / sizeof(int); i++) {
312 if (*tigp_buffer == 0)
313 break;
314 debug("tigp buffer releasing %u\n", *tigp_buffer);
315
316 tip_buffer = zalloc(fs->blksz);
317 if (!tip_buffer)
318 goto fail;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200319 tipb_start_addr = tip_buffer;
320 status = ext4fs_devread((lbaint_t)le32_to_cpu(*tigp_buffer) *
Simon Glass293d7fb2012-12-26 09:53:28 +0000321 fs->sect_perblk, 0, fs->blksz,
322 (char *)tip_buffer);
323 for (j = 0; j < fs->blksz / sizeof(int); j++) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200324 if (le32_to_cpu(*tip_buffer) == 0)
Simon Glass293d7fb2012-12-26 09:53:28 +0000325 break;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200326 bg_idx = le32_to_cpu(*tip_buffer) / blk_per_grp;
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200327 if (fs->blksz == 1024) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200328 remainder = le32_to_cpu(*tip_buffer) % blk_per_grp;
Simon Glass293d7fb2012-12-26 09:53:28 +0000329 if (!remainder)
330 bg_idx--;
331 }
332
Michael Walle58a9ecb2016-09-01 11:21:40 +0200333 ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer),
Simon Glass293d7fb2012-12-26 09:53:28 +0000334 fs->blk_bmaps[bg_idx],
335 bg_idx);
336
337 tip_buffer++;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200338 /* get block group descriptor table */
339 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns749e93e2016-09-20 01:13:01 +0200340 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200341 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass293d7fb2012-12-26 09:53:28 +0000342 /* journal backup */
343 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200344 uint64_t b_bitmap_blk =
345 ext4fs_bg_get_block_id(bgd, fs);
Simon Glass293d7fb2012-12-26 09:53:28 +0000346 status =
347 ext4fs_devread(
Stefan Brüns688d0e72016-09-17 02:10:10 +0200348 b_bitmap_blk *
Simon Glass293d7fb2012-12-26 09:53:28 +0000349 fs->sect_perblk, 0,
350 fs->blksz,
351 journal_buffer);
352 if (status == 0)
353 goto fail;
354
355 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +0200356 b_bitmap_blk))
Simon Glass293d7fb2012-12-26 09:53:28 +0000357 goto fail;
358 prev_bg_bmap_idx = bg_idx;
359 }
360 }
361 free(tipb_start_addr);
362 tipb_start_addr = NULL;
363
364 /*
365 * removing the grand parent blocks
366 * which is connected to inode
367 */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200368 bg_idx = le32_to_cpu(*tigp_buffer) / blk_per_grp;
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200369 if (fs->blksz == 1024) {
Michael Walle58a9ecb2016-09-01 11:21:40 +0200370 remainder = le32_to_cpu(*tigp_buffer) % blk_per_grp;
Simon Glass293d7fb2012-12-26 09:53:28 +0000371 if (!remainder)
372 bg_idx--;
373 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200374 ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer),
Simon Glass293d7fb2012-12-26 09:53:28 +0000375 fs->blk_bmaps[bg_idx], bg_idx);
376
377 tigp_buffer++;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200378 /* get block group descriptor table */
379 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns749e93e2016-09-20 01:13:01 +0200380 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200381 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass293d7fb2012-12-26 09:53:28 +0000382 /* journal backup */
383 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200384 uint64_t b_bitmap_blk =
385 ext4fs_bg_get_block_id(bgd, fs);
Simon Glass293d7fb2012-12-26 09:53:28 +0000386 memset(journal_buffer, '\0', fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200387 status = ext4fs_devread(b_bitmap_blk *
388 fs->sect_perblk, 0,
389 fs->blksz,
390 journal_buffer);
Simon Glass293d7fb2012-12-26 09:53:28 +0000391 if (status == 0)
392 goto fail;
393
394 if (ext4fs_log_journal(journal_buffer,
Stefan Brüns688d0e72016-09-17 02:10:10 +0200395 b_bitmap_blk))
Simon Glass293d7fb2012-12-26 09:53:28 +0000396 goto fail;
397 prev_bg_bmap_idx = bg_idx;
398 }
399 }
400
401 /* removing the grand parent triple indirect block */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200402 blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
Łukasz Majewski35dd0552014-05-06 09:36:04 +0200403 bg_idx = blknr / blk_per_grp;
404 if (fs->blksz == 1024) {
Simon Glass293d7fb2012-12-26 09:53:28 +0000405 remainder = blknr % blk_per_grp;
406 if (!remainder)
407 bg_idx--;
408 }
409 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200410 /* get block group descriptor table */
411 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns749e93e2016-09-20 01:13:01 +0200412 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200413 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass293d7fb2012-12-26 09:53:28 +0000414 /* journal backup */
415 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200416 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
417 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
418 0, fs->blksz, journal_buffer);
Simon Glass293d7fb2012-12-26 09:53:28 +0000419 if (status == 0)
420 goto fail;
421
Stefan Brüns688d0e72016-09-17 02:10:10 +0200422 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass293d7fb2012-12-26 09:53:28 +0000423 goto fail;
424 prev_bg_bmap_idx = bg_idx;
425 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200426 debug("tigp buffer itself releasing %d\n", blknr);
Simon Glass293d7fb2012-12-26 09:53:28 +0000427 }
428fail:
429 free(tib_start_addr);
430 free(tipb_start_addr);
431 free(journal_buffer);
432}
433
434static int ext4fs_delete_file(int inodeno)
435{
436 struct ext2_inode inode;
437 short status;
438 int i;
439 int remainder;
440 long int blknr;
441 int bg_idx;
442 int ibmap_idx;
443 char *read_buffer = NULL;
444 char *start_block_address = NULL;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200445 uint32_t no_blocks;
Simon Glass293d7fb2012-12-26 09:53:28 +0000446
447 static int prev_bg_bmap_idx = -1;
448 unsigned int inodes_per_block;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200449 uint32_t blkno;
Simon Glass293d7fb2012-12-26 09:53:28 +0000450 unsigned int blkoff;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200451 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
452 uint32_t inode_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass293d7fb2012-12-26 09:53:28 +0000453 struct ext2_inode *inode_buffer = NULL;
454 struct ext2_block_group *bgd = NULL;
455 struct ext_filesystem *fs = get_fs();
456 char *journal_buffer = zalloc(fs->blksz);
457 if (!journal_buffer)
458 return -ENOMEM;
Simon Glass293d7fb2012-12-26 09:53:28 +0000459 status = ext4fs_read_inode(ext4fs_root, inodeno, &inode);
460 if (status == 0)
461 goto fail;
462
463 /* read the block no allocated to a file */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200464 no_blocks = le32_to_cpu(inode.size) / fs->blksz;
465 if (le32_to_cpu(inode.size) % fs->blksz)
Simon Glass293d7fb2012-12-26 09:53:28 +0000466 no_blocks++;
467
468 if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) {
Stefan Brünsb779e022016-09-06 04:36:54 +0200469 /* FIXME delete extent index blocks, i.e. eh_depth >= 1 */
470 struct ext4_extent_header *eh =
471 (struct ext4_extent_header *)
472 inode.b.blocks.dir_blocks;
473 debug("del: dep=%d entries=%d\n", eh->eh_depth, eh->eh_entries);
Simon Glass293d7fb2012-12-26 09:53:28 +0000474 } else {
Simon Glass293d7fb2012-12-26 09:53:28 +0000475 delete_single_indirect_block(&inode);
476 delete_double_indirect_block(&inode);
477 delete_triple_indirect_block(&inode);
Stefan Brünsb779e022016-09-06 04:36:54 +0200478 }
Simon Glass293d7fb2012-12-26 09:53:28 +0000479
Stefan Brünsb779e022016-09-06 04:36:54 +0200480 /* release data blocks */
481 for (i = 0; i < no_blocks; i++) {
Stephen Warrend5aee652019-01-30 12:58:05 -0700482 blknr = read_allocated_block(&inode, i, NULL);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200483 if (blknr == 0)
484 continue;
485 if (blknr < 0)
486 goto fail;
Stefan Brünsb779e022016-09-06 04:36:54 +0200487 bg_idx = blknr / blk_per_grp;
488 if (fs->blksz == 1024) {
489 remainder = blknr % blk_per_grp;
490 if (!remainder)
491 bg_idx--;
492 }
493 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx],
494 bg_idx);
495 debug("EXT4 Block releasing %ld: %d\n", blknr, bg_idx);
Simon Glass293d7fb2012-12-26 09:53:28 +0000496
Stefan Brüns688d0e72016-09-17 02:10:10 +0200497 /* get block group descriptor table */
498 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns749e93e2016-09-20 01:13:01 +0200499 ext4fs_bg_free_blocks_inc(bgd, fs);
Stefan Brünsb779e022016-09-06 04:36:54 +0200500 ext4fs_sb_free_blocks_inc(fs->sb);
501 /* journal backup */
502 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200503 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
504 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Stefan Brünsb779e022016-09-06 04:36:54 +0200505 0, fs->blksz,
506 journal_buffer);
507 if (status == 0)
508 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200509 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Stefan Brünsb779e022016-09-06 04:36:54 +0200510 goto fail;
511 prev_bg_bmap_idx = bg_idx;
Simon Glass293d7fb2012-12-26 09:53:28 +0000512 }
513 }
514
Stefan Brünsb779e022016-09-06 04:36:54 +0200515 /* release inode */
Simon Glass293d7fb2012-12-26 09:53:28 +0000516 /* from the inode no to blockno */
517 inodes_per_block = fs->blksz / fs->inodesz;
518 ibmap_idx = inodeno / inode_per_grp;
519
520 /* get the block no */
521 inodeno--;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200522 /* get block group descriptor table */
523 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
524 blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Michael Walle58a9ecb2016-09-01 11:21:40 +0200525 (inodeno % inode_per_grp) / inodes_per_block;
Simon Glass293d7fb2012-12-26 09:53:28 +0000526
527 /* get the offset of the inode */
528 blkoff = ((inodeno) % inodes_per_block) * fs->inodesz;
529
530 /* read the block no containing the inode */
531 read_buffer = zalloc(fs->blksz);
532 if (!read_buffer)
533 goto fail;
534 start_block_address = read_buffer;
Frederic Leroy04735e92013-06-26 18:11:25 +0200535 status = ext4fs_devread((lbaint_t)blkno * fs->sect_perblk,
Simon Glass293d7fb2012-12-26 09:53:28 +0000536 0, fs->blksz, read_buffer);
537 if (status == 0)
538 goto fail;
539
540 if (ext4fs_log_journal(read_buffer, blkno))
541 goto fail;
542
543 read_buffer = read_buffer + blkoff;
544 inode_buffer = (struct ext2_inode *)read_buffer;
Stefan Brüns87f9fdc2016-09-06 04:36:53 +0200545 memset(inode_buffer, '\0', fs->inodesz);
Simon Glass293d7fb2012-12-26 09:53:28 +0000546
547 /* write the inode to original position in inode table */
548 if (ext4fs_put_metadata(start_block_address, blkno))
549 goto fail;
550
551 /* update the respective inode bitmaps */
552 inodeno++;
553 ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx);
Stefan Brüns749e93e2016-09-20 01:13:01 +0200554 ext4fs_bg_free_inodes_inc(bgd, fs);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200555 ext4fs_sb_free_inodes_inc(fs->sb);
Simon Glass293d7fb2012-12-26 09:53:28 +0000556 /* journal backup */
557 memset(journal_buffer, '\0', fs->blksz);
Stefan Brüns688d0e72016-09-17 02:10:10 +0200558 status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) *
Simon Glass293d7fb2012-12-26 09:53:28 +0000559 fs->sect_perblk, 0, fs->blksz, journal_buffer);
560 if (status == 0)
561 goto fail;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200562 if (ext4fs_log_journal(journal_buffer, ext4fs_bg_get_inode_id(bgd, fs)))
Simon Glass293d7fb2012-12-26 09:53:28 +0000563 goto fail;
564
565 ext4fs_update();
566 ext4fs_deinit();
Łukasz Majewski8b454ee2014-05-06 09:36:05 +0200567 ext4fs_reinit_global();
Simon Glass293d7fb2012-12-26 09:53:28 +0000568
569 if (ext4fs_init() != 0) {
570 printf("error in File System init\n");
571 goto fail;
572 }
573
574 free(start_block_address);
575 free(journal_buffer);
576
577 return 0;
578fail:
579 free(start_block_address);
580 free(journal_buffer);
581
582 return -1;
583}
584
585int ext4fs_init(void)
586{
587 short status;
588 int i;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200589 uint32_t real_free_blocks = 0;
Simon Glass293d7fb2012-12-26 09:53:28 +0000590 struct ext_filesystem *fs = get_fs();
591
592 /* populate fs */
593 fs->blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich50ce4c02013-05-01 01:13:19 +0000594 fs->sect_perblk = fs->blksz >> fs->dev_desc->log2blksz;
Simon Glass293d7fb2012-12-26 09:53:28 +0000595
596 /* get the superblock */
597 fs->sb = zalloc(SUPERBLOCK_SIZE);
598 if (!fs->sb)
599 return -ENOMEM;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000600 if (!ext4_read_superblock((char *)fs->sb))
Simon Glass293d7fb2012-12-26 09:53:28 +0000601 goto fail;
602
603 /* init journal */
604 if (ext4fs_init_journal())
605 goto fail;
606
607 /* get total no of blockgroups */
608 fs->no_blkgrp = (uint32_t)ext4fs_div_roundup(
Michael Walle58a9ecb2016-09-01 11:21:40 +0200609 le32_to_cpu(ext4fs_root->sblock.total_blocks)
610 - le32_to_cpu(ext4fs_root->sblock.first_data_block),
611 le32_to_cpu(ext4fs_root->sblock.blocks_per_group));
Simon Glass293d7fb2012-12-26 09:53:28 +0000612
613 /* get the block group descriptor table */
614 fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1);
615 if (ext4fs_get_bgdtable() == -1) {
616 printf("Error in getting the block group descriptor table\n");
617 goto fail;
618 }
Simon Glass293d7fb2012-12-26 09:53:28 +0000619
620 /* load all the available bitmap block of the partition */
621 fs->blk_bmaps = zalloc(fs->no_blkgrp * sizeof(char *));
622 if (!fs->blk_bmaps)
623 goto fail;
624 for (i = 0; i < fs->no_blkgrp; i++) {
625 fs->blk_bmaps[i] = zalloc(fs->blksz);
626 if (!fs->blk_bmaps[i])
627 goto fail;
628 }
629
630 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200631 struct ext2_block_group *bgd =
632 ext4fs_get_group_descriptor(fs, i);
633 status = ext4fs_devread(ext4fs_bg_get_block_id(bgd, fs) *
Frederic Leroy04735e92013-06-26 18:11:25 +0200634 fs->sect_perblk, 0,
Simon Glass293d7fb2012-12-26 09:53:28 +0000635 fs->blksz, (char *)fs->blk_bmaps[i]);
636 if (status == 0)
637 goto fail;
638 }
639
640 /* load all the available inode bitmap of the partition */
641 fs->inode_bmaps = zalloc(fs->no_blkgrp * sizeof(unsigned char *));
642 if (!fs->inode_bmaps)
643 goto fail;
644 for (i = 0; i < fs->no_blkgrp; i++) {
645 fs->inode_bmaps[i] = zalloc(fs->blksz);
646 if (!fs->inode_bmaps[i])
647 goto fail;
648 }
649
650 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brüns688d0e72016-09-17 02:10:10 +0200651 struct ext2_block_group *bgd =
652 ext4fs_get_group_descriptor(fs, i);
653 status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) *
Frederic Leroy04735e92013-06-26 18:11:25 +0200654 fs->sect_perblk,
Simon Glass293d7fb2012-12-26 09:53:28 +0000655 0, fs->blksz,
656 (char *)fs->inode_bmaps[i]);
657 if (status == 0)
658 goto fail;
659 }
660
661 /*
662 * check filesystem consistency with free blocks of file system
663 * some time we observed that superblock freeblocks does not match
664 * with the blockgroups freeblocks when improper
665 * reboot of a linux kernel
666 */
Stefan Brüns688d0e72016-09-17 02:10:10 +0200667 for (i = 0; i < fs->no_blkgrp; i++) {
668 struct ext2_block_group *bgd =
669 ext4fs_get_group_descriptor(fs, i);
670 real_free_blocks = real_free_blocks +
671 ext4fs_bg_get_free_blocks(bgd, fs);
672 }
673 if (real_free_blocks != ext4fs_sb_get_free_blocks(fs->sb))
674 ext4fs_sb_set_free_blocks(fs->sb, real_free_blocks);
Simon Glass293d7fb2012-12-26 09:53:28 +0000675
676 return 0;
677fail:
678 ext4fs_deinit();
679
680 return -1;
681}
682
683void ext4fs_deinit(void)
684{
685 int i;
686 struct ext2_inode inode_journal;
687 struct journal_superblock_t *jsb;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200688 uint32_t blknr;
Simon Glass293d7fb2012-12-26 09:53:28 +0000689 struct ext_filesystem *fs = get_fs();
Michael Walle58a9ecb2016-09-01 11:21:40 +0200690 uint32_t new_feature_incompat;
Simon Glass293d7fb2012-12-26 09:53:28 +0000691
692 /* free journal */
693 char *temp_buff = zalloc(fs->blksz);
694 if (temp_buff) {
695 ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
696 &inode_journal);
697 blknr = read_allocated_block(&inode_journal,
Stephen Warrend5aee652019-01-30 12:58:05 -0700698 EXT2_JOURNAL_SUPERBLOCK, NULL);
Frederic Leroy04735e92013-06-26 18:11:25 +0200699 ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
Simon Glass293d7fb2012-12-26 09:53:28 +0000700 temp_buff);
701 jsb = (struct journal_superblock_t *)temp_buff;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200702 jsb->s_start = 0;
Ma Haijun05508702014-01-08 08:15:33 +0800703 put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
Simon Glass293d7fb2012-12-26 09:53:28 +0000704 (struct journal_superblock_t *)temp_buff, fs->blksz);
705 free(temp_buff);
706 }
707 ext4fs_free_journal();
708
709 /* get the superblock */
Egbert Eich50ce4c02013-05-01 01:13:19 +0000710 ext4_read_superblock((char *)fs->sb);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200711 new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat);
712 new_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
713 fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat);
Simon Glass293d7fb2012-12-26 09:53:28 +0000714 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
715 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
716 free(fs->sb);
717 fs->sb = NULL;
718
719 if (fs->blk_bmaps) {
720 for (i = 0; i < fs->no_blkgrp; i++) {
721 free(fs->blk_bmaps[i]);
722 fs->blk_bmaps[i] = NULL;
723 }
724 free(fs->blk_bmaps);
725 fs->blk_bmaps = NULL;
726 }
727
728 if (fs->inode_bmaps) {
729 for (i = 0; i < fs->no_blkgrp; i++) {
730 free(fs->inode_bmaps[i]);
731 fs->inode_bmaps[i] = NULL;
732 }
733 free(fs->inode_bmaps);
734 fs->inode_bmaps = NULL;
735 }
736
737
738 free(fs->gdtable);
739 fs->gdtable = NULL;
Simon Glass293d7fb2012-12-26 09:53:28 +0000740 /*
741 * reinitiliazed the global inode and
742 * block bitmap first execution check variables
743 */
744 fs->first_pass_ibmap = 0;
745 fs->first_pass_bbmap = 0;
746 fs->curr_inode_no = 0;
747 fs->curr_blkno = 0;
748}
749
Stefan Brünsde9e8312016-09-06 04:36:55 +0200750/*
751 * Write data to filesystem blocks. Uses same optimization for
752 * contigous sectors as ext4fs_read_file
753 */
Simon Glass293d7fb2012-12-26 09:53:28 +0000754static int ext4fs_write_file(struct ext2_inode *file_inode,
Jean-Jacques Hiblotb0001802019-02-13 12:15:24 +0100755 int pos, unsigned int len, const char *buf)
Simon Glass293d7fb2012-12-26 09:53:28 +0000756{
757 int i;
758 int blockcnt;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200759 uint32_t filesize = le32_to_cpu(file_inode->size);
Simon Glass293d7fb2012-12-26 09:53:28 +0000760 struct ext_filesystem *fs = get_fs();
Egbert Eich50ce4c02013-05-01 01:13:19 +0000761 int log2blksz = fs->dev_desc->log2blksz;
762 int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz;
Simon Glass293d7fb2012-12-26 09:53:28 +0000763 int previous_block_number = -1;
764 int delayed_start = 0;
765 int delayed_extent = 0;
766 int delayed_next = 0;
Jean-Jacques Hiblotb0001802019-02-13 12:15:24 +0100767 const char *delayed_buf = NULL;
Simon Glass293d7fb2012-12-26 09:53:28 +0000768
769 /* Adjust len so it we can't read past the end of the file. */
770 if (len > filesize)
771 len = filesize;
772
773 blockcnt = ((len + pos) + fs->blksz - 1) / fs->blksz;
774
775 for (i = pos / fs->blksz; i < blockcnt; i++) {
776 long int blknr;
777 int blockend = fs->blksz;
778 int skipfirst = 0;
Stephen Warrend5aee652019-01-30 12:58:05 -0700779 blknr = read_allocated_block(file_inode, i, NULL);
Stefan Brünsde9e8312016-09-06 04:36:55 +0200780 if (blknr <= 0)
Simon Glass293d7fb2012-12-26 09:53:28 +0000781 return -1;
782
Egbert Eich50ce4c02013-05-01 01:13:19 +0000783 blknr = blknr << log2_fs_blocksize;
Simon Glass293d7fb2012-12-26 09:53:28 +0000784
785 if (blknr) {
786 if (previous_block_number != -1) {
787 if (delayed_next == blknr) {
788 delayed_extent += blockend;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000789 delayed_next += blockend >> log2blksz;
Simon Glass293d7fb2012-12-26 09:53:28 +0000790 } else { /* spill */
Egbert Eich50ce4c02013-05-01 01:13:19 +0000791 put_ext4((uint64_t)
Ma Haijun05508702014-01-08 08:15:33 +0800792 ((uint64_t)delayed_start << log2blksz),
Simon Glass293d7fb2012-12-26 09:53:28 +0000793 delayed_buf,
794 (uint32_t) delayed_extent);
795 previous_block_number = blknr;
796 delayed_start = blknr;
797 delayed_extent = blockend;
798 delayed_buf = buf;
799 delayed_next = blknr +
Egbert Eich50ce4c02013-05-01 01:13:19 +0000800 (blockend >> log2blksz);
Simon Glass293d7fb2012-12-26 09:53:28 +0000801 }
802 } else {
803 previous_block_number = blknr;
804 delayed_start = blknr;
805 delayed_extent = blockend;
806 delayed_buf = buf;
807 delayed_next = blknr +
Egbert Eich50ce4c02013-05-01 01:13:19 +0000808 (blockend >> log2blksz);
Simon Glass293d7fb2012-12-26 09:53:28 +0000809 }
810 } else {
811 if (previous_block_number != -1) {
812 /* spill */
Ma Haijun05508702014-01-08 08:15:33 +0800813 put_ext4((uint64_t) ((uint64_t)delayed_start <<
Egbert Eich50ce4c02013-05-01 01:13:19 +0000814 log2blksz),
815 delayed_buf,
Simon Glass293d7fb2012-12-26 09:53:28 +0000816 (uint32_t) delayed_extent);
817 previous_block_number = -1;
818 }
Simon Glass293d7fb2012-12-26 09:53:28 +0000819 }
820 buf += fs->blksz - skipfirst;
821 }
822 if (previous_block_number != -1) {
823 /* spill */
Ma Haijun05508702014-01-08 08:15:33 +0800824 put_ext4((uint64_t) ((uint64_t)delayed_start << log2blksz),
Simon Glass293d7fb2012-12-26 09:53:28 +0000825 delayed_buf, (uint32_t) delayed_extent);
826 previous_block_number = -1;
827 }
828
829 return len;
830}
831
Jean-Jacques Hiblotb0001802019-02-13 12:15:24 +0100832int ext4fs_write(const char *fname, const char *buffer,
833 unsigned long sizebytes)
Simon Glass293d7fb2012-12-26 09:53:28 +0000834{
835 int ret = 0;
836 struct ext2_inode *file_inode = NULL;
837 unsigned char *inode_buffer = NULL;
838 int parent_inodeno;
839 int inodeno;
840 time_t timestamp = 0;
841
842 uint64_t bytes_reqd_for_file;
843 unsigned int blks_reqd_for_file;
844 unsigned int blocks_remaining;
845 int existing_file_inodeno;
846 char *temp_ptr = NULL;
847 long int itable_blkno;
848 long int parent_itable_blkno;
849 long int blkoff;
850 struct ext2_sblock *sblock = &(ext4fs_root->sblock);
851 unsigned int inodes_per_block;
852 unsigned int ibmap_idx;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200853 struct ext2_block_group *bgd = NULL;
Simon Glass293d7fb2012-12-26 09:53:28 +0000854 struct ext_filesystem *fs = get_fs();
855 ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256);
Jeroen Hofstee46a57072014-06-09 15:29:00 +0200856 memset(filename, 0x00, 256);
Simon Glass293d7fb2012-12-26 09:53:28 +0000857
Stefan Brüns87f9fdc2016-09-06 04:36:53 +0200858 g_parent_inode = zalloc(fs->inodesz);
Simon Glass293d7fb2012-12-26 09:53:28 +0000859 if (!g_parent_inode)
860 goto fail;
861
862 if (ext4fs_init() != 0) {
863 printf("error in File System init\n");
864 return -1;
865 }
Sébastien Szymanski2e736552019-03-22 09:33:52 +0100866
867 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
868 printf("Unsupported feature metadata_csum found, not writing.\n");
869 return -1;
870 }
871
Simon Glass293d7fb2012-12-26 09:53:28 +0000872 inodes_per_block = fs->blksz / fs->inodesz;
873 parent_inodeno = ext4fs_get_parent_inode_num(fname, filename, F_FILE);
874 if (parent_inodeno == -1)
875 goto fail;
876 if (ext4fs_iget(parent_inodeno, g_parent_inode))
877 goto fail;
Stefan Brüns10a7a1b2016-09-06 04:36:45 +0200878 /* do not mess up a directory using hash trees */
879 if (le32_to_cpu(g_parent_inode->flags) & EXT4_INDEX_FL) {
880 printf("hash tree directory\n");
881 goto fail;
882 }
Simon Glass293d7fb2012-12-26 09:53:28 +0000883 /* check if the filename is already present in root */
Stefan Brüns76a29512016-09-06 04:36:41 +0200884 existing_file_inodeno = ext4fs_filename_unlink(filename);
Simon Glass293d7fb2012-12-26 09:53:28 +0000885 if (existing_file_inodeno != -1) {
886 ret = ext4fs_delete_file(existing_file_inodeno);
887 fs->first_pass_bbmap = 0;
888 fs->curr_blkno = 0;
889
890 fs->first_pass_ibmap = 0;
891 fs->curr_inode_no = 0;
892 if (ret)
893 goto fail;
894 }
895 /* calucalate how many blocks required */
896 bytes_reqd_for_file = sizebytes;
897 blks_reqd_for_file = lldiv(bytes_reqd_for_file, fs->blksz);
898 if (do_div(bytes_reqd_for_file, fs->blksz) != 0) {
899 blks_reqd_for_file++;
900 debug("total bytes for a file %u\n", blks_reqd_for_file);
901 }
902 blocks_remaining = blks_reqd_for_file;
903 /* test for available space in partition */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200904 if (le32_to_cpu(fs->sb->free_blocks) < blks_reqd_for_file) {
Simon Glass293d7fb2012-12-26 09:53:28 +0000905 printf("Not enough space on partition !!!\n");
906 goto fail;
907 }
908
Stefan Brünsa0d767e2016-09-06 04:36:42 +0200909 inodeno = ext4fs_update_parent_dentry(filename, FILETYPE_REG);
910 if (inodeno == -1)
911 goto fail;
Simon Glass293d7fb2012-12-26 09:53:28 +0000912 /* prepare file inode */
913 inode_buffer = zalloc(fs->inodesz);
914 if (!inode_buffer)
915 goto fail;
916 file_inode = (struct ext2_inode *)inode_buffer;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200917 file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU |
918 S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
Simon Glass293d7fb2012-12-26 09:53:28 +0000919 /* ToDo: Update correct time */
Michael Walle58a9ecb2016-09-01 11:21:40 +0200920 file_inode->mtime = cpu_to_le32(timestamp);
921 file_inode->atime = cpu_to_le32(timestamp);
922 file_inode->ctime = cpu_to_le32(timestamp);
923 file_inode->nlinks = cpu_to_le16(1);
924 file_inode->size = cpu_to_le32(sizebytes);
Simon Glass293d7fb2012-12-26 09:53:28 +0000925
926 /* Allocate data blocks */
927 ext4fs_allocate_blocks(file_inode, blocks_remaining,
928 &blks_reqd_for_file);
Michael Walle58a9ecb2016-09-01 11:21:40 +0200929 file_inode->blockcnt = cpu_to_le32((blks_reqd_for_file * fs->blksz) >>
930 fs->dev_desc->log2blksz);
Simon Glass293d7fb2012-12-26 09:53:28 +0000931
932 temp_ptr = zalloc(fs->blksz);
933 if (!temp_ptr)
934 goto fail;
Michael Walle58a9ecb2016-09-01 11:21:40 +0200935 ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass293d7fb2012-12-26 09:53:28 +0000936 inodeno--;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200937 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
938 itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Michael Walle7f101be2016-08-29 10:46:44 +0200939 (inodeno % le32_to_cpu(sblock->inodes_per_group)) /
Simon Glass293d7fb2012-12-26 09:53:28 +0000940 inodes_per_block;
941 blkoff = (inodeno % inodes_per_block) * fs->inodesz;
Frederic Leroy04735e92013-06-26 18:11:25 +0200942 ext4fs_devread((lbaint_t)itable_blkno * fs->sect_perblk, 0, fs->blksz,
943 temp_ptr);
Simon Glass293d7fb2012-12-26 09:53:28 +0000944 if (ext4fs_log_journal(temp_ptr, itable_blkno))
945 goto fail;
946
947 memcpy(temp_ptr + blkoff, inode_buffer, fs->inodesz);
948 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
949 goto fail;
950 /* copy the file content into data blocks */
Jean-Jacques Hiblotb0001802019-02-13 12:15:24 +0100951 if (ext4fs_write_file(file_inode, 0, sizebytes, buffer) == -1) {
Simon Glass293d7fb2012-12-26 09:53:28 +0000952 printf("Error in copying content\n");
Stefan Brünsde9e8312016-09-06 04:36:55 +0200953 /* FIXME: Deallocate data blocks */
Simon Glass293d7fb2012-12-26 09:53:28 +0000954 goto fail;
955 }
Michael Walle58a9ecb2016-09-01 11:21:40 +0200956 ibmap_idx = parent_inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass293d7fb2012-12-26 09:53:28 +0000957 parent_inodeno--;
Stefan Brüns688d0e72016-09-17 02:10:10 +0200958 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
959 parent_itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Simon Glass293d7fb2012-12-26 09:53:28 +0000960 (parent_inodeno %
Michael Walle7f101be2016-08-29 10:46:44 +0200961 le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Simon Glass293d7fb2012-12-26 09:53:28 +0000962 blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz;
963 if (parent_itable_blkno != itable_blkno) {
964 memset(temp_ptr, '\0', fs->blksz);
Frederic Leroy04735e92013-06-26 18:11:25 +0200965 ext4fs_devread((lbaint_t)parent_itable_blkno * fs->sect_perblk,
Simon Glass293d7fb2012-12-26 09:53:28 +0000966 0, fs->blksz, temp_ptr);
967 if (ext4fs_log_journal(temp_ptr, parent_itable_blkno))
968 goto fail;
969
Stefan Brüns87f9fdc2016-09-06 04:36:53 +0200970 memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz);
Simon Glass293d7fb2012-12-26 09:53:28 +0000971 if (ext4fs_put_metadata(temp_ptr, parent_itable_blkno))
972 goto fail;
Simon Glass293d7fb2012-12-26 09:53:28 +0000973 } else {
974 /*
975 * If parent and child fall in same inode table block
976 * both should be kept in 1 buffer
977 */
Stefan Brüns87f9fdc2016-09-06 04:36:53 +0200978 memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz);
Simon Glass293d7fb2012-12-26 09:53:28 +0000979 gd_index--;
980 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
981 goto fail;
Simon Glass293d7fb2012-12-26 09:53:28 +0000982 }
983 ext4fs_update();
984 ext4fs_deinit();
985
986 fs->first_pass_bbmap = 0;
987 fs->curr_blkno = 0;
988 fs->first_pass_ibmap = 0;
989 fs->curr_inode_no = 0;
990 free(inode_buffer);
991 free(g_parent_inode);
Stefan Brüns87a40b62016-09-06 04:36:51 +0200992 free(temp_ptr);
Simon Glass293d7fb2012-12-26 09:53:28 +0000993 g_parent_inode = NULL;
994
995 return 0;
996fail:
997 ext4fs_deinit();
998 free(inode_buffer);
999 free(g_parent_inode);
Stefan Brüns87a40b62016-09-06 04:36:51 +02001000 free(temp_ptr);
Simon Glass293d7fb2012-12-26 09:53:28 +00001001 g_parent_inode = NULL;
1002
1003 return -1;
1004}
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001005
1006int ext4_write_file(const char *filename, void *buf, loff_t offset,
1007 loff_t len, loff_t *actwrite)
1008{
1009 int ret;
1010
1011 if (offset != 0) {
1012 printf("** Cannot support non-zero offset **\n");
1013 return -1;
1014 }
1015
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001016 ret = ext4fs_write(filename, buf, len);
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001017 if (ret) {
1018 printf("** Error ext4fs_write() **\n");
1019 goto fail;
1020 }
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001021
Przemyslaw Marczak22b75092015-02-17 15:31:52 +01001022 *actwrite = len;
1023
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001024 return 0;
1025
1026fail:
Przemyslaw Marczak22b75092015-02-17 15:31:52 +01001027 *actwrite = 0;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -08001028
1029 return -1;
1030}