blob: 38da3923c477cf2c9811e874c795bf0c89532117 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6c8f3aa2012-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 Glass6c8f3aa2012-12-26 09:53:28 +000021 */
22
23
Simon Glass655306c2020-05-10 11:39:58 -060024#include <blk.h>
Simon Glass0f2af882020-05-10 11:40:05 -060025#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070026#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060027#include <memalign.h>
Simon Glass655306c2020-05-10 11:39:58 -060028#include <part.h>
Simon Glass6c8f3aa2012-12-26 09:53:28 +000029#include <linux/stat.h>
30#include <div64.h>
31#include "ext4_common.h"
32
Michael Walle13179c22016-09-01 11:21:40 +020033static inline void ext4fs_sb_free_inodes_inc(struct ext2_sblock *sb)
34{
35 sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) + 1);
36}
37
38static inline void ext4fs_sb_free_blocks_inc(struct ext2_sblock *sb)
39{
40 sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) + 1);
41}
42
Stefan Brüns05313d12016-09-20 01:13:01 +020043static inline void ext4fs_bg_free_inodes_inc
44 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle13179c22016-09-01 11:21:40 +020045{
Stefan Brüns05313d12016-09-20 01:13:01 +020046 uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
47 if (fs->gdsize == 64)
48 free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
49 free_inodes++;
50
51 bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
52 if (fs->gdsize == 64)
53 bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
Michael Walle13179c22016-09-01 11:21:40 +020054}
55
Stefan Brüns05313d12016-09-20 01:13:01 +020056static inline void ext4fs_bg_free_blocks_inc
57 (struct ext2_block_group *bg, const struct ext_filesystem *fs)
Michael Walle13179c22016-09-01 11:21:40 +020058{
Stefan Brüns05313d12016-09-20 01:13:01 +020059 uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
60 if (fs->gdsize == 64)
61 free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
62 free_blocks++;
63
64 bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
65 if (fs->gdsize == 64)
66 bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
Michael Walle13179c22016-09-01 11:21:40 +020067}
68
Simon Glass6c8f3aa2012-12-26 09:53:28 +000069static void ext4fs_update(void)
70{
71 short i;
72 ext4fs_update_journal();
73 struct ext_filesystem *fs = get_fs();
Stefan Brünsbad73812016-09-17 02:10:10 +020074 struct ext2_block_group *bgd = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +000075
76 /* update super block */
77 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
78 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
79
Stefan Brünsbad73812016-09-17 02:10:10 +020080 /* update block bitmaps */
Simon Glass6c8f3aa2012-12-26 09:53:28 +000081 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +020082 bgd = ext4fs_get_group_descriptor(fs, i);
83 bgd->bg_checksum = cpu_to_le16(ext4fs_checksum_update(i));
84 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
85 put_ext4(b_bitmap_blk * fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +000086 fs->blk_bmaps[i], fs->blksz);
87 }
88
Stefan Brünsbad73812016-09-17 02:10:10 +020089 /* update inode bitmaps */
Simon Glass6c8f3aa2012-12-26 09:53:28 +000090 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +020091 bgd = ext4fs_get_group_descriptor(fs, i);
92 uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
93 put_ext4(i_bitmap_blk * fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +000094 fs->inode_bmaps[i], fs->blksz);
95 }
96
97 /* update the block group descriptor table */
Ma Haijune0996ca2014-01-08 08:15:33 +080098 put_ext4((uint64_t)((uint64_t)fs->gdtable_blkno * (uint64_t)fs->blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +000099 (struct ext2_block_group *)fs->gdtable,
100 (fs->blksz * fs->no_blk_pergdt));
101
102 ext4fs_dump_metadata();
103
104 gindex = 0;
105 gd_index = 0;
106}
107
108int ext4fs_get_bgdtable(void)
109{
110 int status;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000111 struct ext_filesystem *fs = get_fs();
Stefan Brünsbad73812016-09-17 02:10:10 +0200112 int gdsize_total = ROUND(fs->no_blkgrp * fs->gdsize, fs->blksz);
113 fs->no_blk_pergdt = gdsize_total / fs->blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000114
115 /* allocate memory for gdtable */
Stefan Brünsbad73812016-09-17 02:10:10 +0200116 fs->gdtable = zalloc(gdsize_total);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000117 if (!fs->gdtable)
118 return -ENOMEM;
119 /* read the group descriptor table */
Frederic Leroye7ee0282013-06-26 18:11:25 +0200120 status = ext4fs_devread((lbaint_t)fs->gdtable_blkno * fs->sect_perblk,
121 0, fs->blksz * fs->no_blk_pergdt, fs->gdtable);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000122 if (status == 0)
123 goto fail;
124
125 if (ext4fs_log_gdt(fs->gdtable)) {
126 printf("Error in ext4fs_log_gdt\n");
127 return -1;
128 }
129
130 return 0;
131fail:
132 free(fs->gdtable);
133 fs->gdtable = NULL;
134
135 return -1;
136}
137
138static void delete_single_indirect_block(struct ext2_inode *inode)
139{
140 struct ext2_block_group *bgd = NULL;
141 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200142 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000143 int remainder;
144 int bg_idx;
145 int status;
Michael Walle13179c22016-09-01 11:21:40 +0200146 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000147 struct ext_filesystem *fs = get_fs();
148 char *journal_buffer = zalloc(fs->blksz);
149 if (!journal_buffer) {
150 printf("No memory\n");
151 return;
152 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000153
154 /* deleting the single indirect block associated with inode */
155 if (inode->b.blocks.indir_block != 0) {
Michael Walle13179c22016-09-01 11:21:40 +0200156 blknr = le32_to_cpu(inode->b.blocks.indir_block);
157 debug("SIPB releasing %u\n", blknr);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200158 bg_idx = blknr / blk_per_grp;
159 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000160 remainder = blknr % blk_per_grp;
161 if (!remainder)
162 bg_idx--;
163 }
164 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brünsbad73812016-09-17 02:10:10 +0200165 /* get block group descriptor table */
166 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200167 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200168 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000169 /* journal backup */
170 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200171 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200172 status = ext4fs_devread(
Stefan Brünsbad73812016-09-17 02:10:10 +0200173 b_bitmap_blk * fs->sect_perblk,
174 0, fs->blksz, journal_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000175 if (status == 0)
176 goto fail;
Stefan Brünsbad73812016-09-17 02:10:10 +0200177 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000178 goto fail;
179 prev_bg_bmap_idx = bg_idx;
180 }
181 }
182fail:
183 free(journal_buffer);
184}
185
186static void delete_double_indirect_block(struct ext2_inode *inode)
187{
188 int i;
189 short status;
190 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200191 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000192 int remainder;
193 int bg_idx;
Michael Walle13179c22016-09-01 11:21:40 +0200194 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
195 __le32 *di_buffer = NULL;
196 void *dib_start_addr = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000197 struct ext2_block_group *bgd = NULL;
198 struct ext_filesystem *fs = get_fs();
199 char *journal_buffer = zalloc(fs->blksz);
200 if (!journal_buffer) {
201 printf("No memory\n");
202 return;
203 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000204
205 if (inode->b.blocks.double_indir_block != 0) {
206 di_buffer = zalloc(fs->blksz);
207 if (!di_buffer) {
208 printf("No memory\n");
209 return;
210 }
Michael Walle13179c22016-09-01 11:21:40 +0200211 dib_start_addr = di_buffer;
212 blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200213 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
214 fs->blksz, (char *)di_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000215 for (i = 0; i < fs->blksz / sizeof(int); i++) {
216 if (*di_buffer == 0)
217 break;
218
219 debug("DICB releasing %u\n", *di_buffer);
Michael Walle13179c22016-09-01 11:21:40 +0200220 bg_idx = le32_to_cpu(*di_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200221 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200222 remainder = le32_to_cpu(*di_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000223 if (!remainder)
224 bg_idx--;
225 }
Stefan Brünsbad73812016-09-17 02:10:10 +0200226 /* get block group descriptor table */
227 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Michael Walle13179c22016-09-01 11:21:40 +0200228 ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000229 fs->blk_bmaps[bg_idx], bg_idx);
230 di_buffer++;
Stefan Brüns05313d12016-09-20 01:13:01 +0200231 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200232 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000233 /* journal backup */
234 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200235 uint64_t b_bitmap_blk =
236 ext4fs_bg_get_block_id(bgd, fs);
237 status = ext4fs_devread(b_bitmap_blk
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000238 * fs->sect_perblk, 0,
239 fs->blksz,
240 journal_buffer);
241 if (status == 0)
242 goto fail;
243
244 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +0200245 b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000246 goto fail;
247 prev_bg_bmap_idx = bg_idx;
248 }
249 }
250
251 /* removing the parent double indirect block */
Michael Walle13179c22016-09-01 11:21:40 +0200252 blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200253 bg_idx = blknr / blk_per_grp;
254 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000255 remainder = blknr % blk_per_grp;
256 if (!remainder)
257 bg_idx--;
258 }
Stefan Brünsbad73812016-09-17 02:10:10 +0200259 /* get block group descriptor table */
260 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000261 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200262 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200263 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000264 /* journal backup */
265 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200266 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
267 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
268 0, fs->blksz, journal_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000269 if (status == 0)
270 goto fail;
271
Stefan Brünsbad73812016-09-17 02:10:10 +0200272 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000273 goto fail;
274 prev_bg_bmap_idx = bg_idx;
275 }
Michael Walle13179c22016-09-01 11:21:40 +0200276 debug("DIPB releasing %d\n", blknr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000277 }
278fail:
Michael Walle13179c22016-09-01 11:21:40 +0200279 free(dib_start_addr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000280 free(journal_buffer);
281}
282
283static void delete_triple_indirect_block(struct ext2_inode *inode)
284{
285 int i, j;
286 short status;
287 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200288 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000289 int remainder;
290 int bg_idx;
Michael Walle13179c22016-09-01 11:21:40 +0200291 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
292 __le32 *tigp_buffer = NULL;
293 void *tib_start_addr = NULL;
294 __le32 *tip_buffer = NULL;
295 void *tipb_start_addr = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000296 struct ext2_block_group *bgd = NULL;
297 struct ext_filesystem *fs = get_fs();
298 char *journal_buffer = zalloc(fs->blksz);
299 if (!journal_buffer) {
300 printf("No memory\n");
301 return;
302 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000303
304 if (inode->b.blocks.triple_indir_block != 0) {
305 tigp_buffer = zalloc(fs->blksz);
306 if (!tigp_buffer) {
307 printf("No memory\n");
308 return;
309 }
Michael Walle13179c22016-09-01 11:21:40 +0200310 tib_start_addr = tigp_buffer;
311 blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200312 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
313 fs->blksz, (char *)tigp_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000314 for (i = 0; i < fs->blksz / sizeof(int); i++) {
315 if (*tigp_buffer == 0)
316 break;
317 debug("tigp buffer releasing %u\n", *tigp_buffer);
318
319 tip_buffer = zalloc(fs->blksz);
320 if (!tip_buffer)
321 goto fail;
Michael Walle13179c22016-09-01 11:21:40 +0200322 tipb_start_addr = tip_buffer;
323 status = ext4fs_devread((lbaint_t)le32_to_cpu(*tigp_buffer) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000324 fs->sect_perblk, 0, fs->blksz,
325 (char *)tip_buffer);
326 for (j = 0; j < fs->blksz / sizeof(int); j++) {
Michael Walle13179c22016-09-01 11:21:40 +0200327 if (le32_to_cpu(*tip_buffer) == 0)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000328 break;
Michael Walle13179c22016-09-01 11:21:40 +0200329 bg_idx = le32_to_cpu(*tip_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200330 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200331 remainder = le32_to_cpu(*tip_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000332 if (!remainder)
333 bg_idx--;
334 }
335
Michael Walle13179c22016-09-01 11:21:40 +0200336 ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000337 fs->blk_bmaps[bg_idx],
338 bg_idx);
339
340 tip_buffer++;
Stefan Brünsbad73812016-09-17 02:10:10 +0200341 /* get block group descriptor table */
342 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200343 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200344 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000345 /* journal backup */
346 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200347 uint64_t b_bitmap_blk =
348 ext4fs_bg_get_block_id(bgd, fs);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000349 status =
350 ext4fs_devread(
Stefan Brünsbad73812016-09-17 02:10:10 +0200351 b_bitmap_blk *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000352 fs->sect_perblk, 0,
353 fs->blksz,
354 journal_buffer);
355 if (status == 0)
356 goto fail;
357
358 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +0200359 b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000360 goto fail;
361 prev_bg_bmap_idx = bg_idx;
362 }
363 }
364 free(tipb_start_addr);
365 tipb_start_addr = NULL;
366
367 /*
368 * removing the grand parent blocks
369 * which is connected to inode
370 */
Michael Walle13179c22016-09-01 11:21:40 +0200371 bg_idx = le32_to_cpu(*tigp_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200372 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200373 remainder = le32_to_cpu(*tigp_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000374 if (!remainder)
375 bg_idx--;
376 }
Michael Walle13179c22016-09-01 11:21:40 +0200377 ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000378 fs->blk_bmaps[bg_idx], bg_idx);
379
380 tigp_buffer++;
Stefan Brünsbad73812016-09-17 02:10:10 +0200381 /* get block group descriptor table */
382 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200383 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200384 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000385 /* journal backup */
386 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200387 uint64_t b_bitmap_blk =
388 ext4fs_bg_get_block_id(bgd, fs);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000389 memset(journal_buffer, '\0', fs->blksz);
Stefan Brünsbad73812016-09-17 02:10:10 +0200390 status = ext4fs_devread(b_bitmap_blk *
391 fs->sect_perblk, 0,
392 fs->blksz,
393 journal_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000394 if (status == 0)
395 goto fail;
396
397 if (ext4fs_log_journal(journal_buffer,
Stefan Brünsbad73812016-09-17 02:10:10 +0200398 b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000399 goto fail;
400 prev_bg_bmap_idx = bg_idx;
401 }
402 }
403
404 /* removing the grand parent triple indirect block */
Michael Walle13179c22016-09-01 11:21:40 +0200405 blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200406 bg_idx = blknr / blk_per_grp;
407 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000408 remainder = blknr % blk_per_grp;
409 if (!remainder)
410 bg_idx--;
411 }
412 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Stefan Brünsbad73812016-09-17 02:10:10 +0200413 /* get block group descriptor table */
414 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200415 ext4fs_bg_free_blocks_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200416 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000417 /* journal backup */
418 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200419 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
420 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
421 0, fs->blksz, journal_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000422 if (status == 0)
423 goto fail;
424
Stefan Brünsbad73812016-09-17 02:10:10 +0200425 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000426 goto fail;
427 prev_bg_bmap_idx = bg_idx;
428 }
Michael Walle13179c22016-09-01 11:21:40 +0200429 debug("tigp buffer itself releasing %d\n", blknr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000430 }
431fail:
432 free(tib_start_addr);
433 free(tipb_start_addr);
434 free(journal_buffer);
435}
436
437static int ext4fs_delete_file(int inodeno)
438{
439 struct ext2_inode inode;
440 short status;
441 int i;
442 int remainder;
443 long int blknr;
444 int bg_idx;
445 int ibmap_idx;
446 char *read_buffer = NULL;
447 char *start_block_address = NULL;
Michael Walle13179c22016-09-01 11:21:40 +0200448 uint32_t no_blocks;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000449
450 static int prev_bg_bmap_idx = -1;
451 unsigned int inodes_per_block;
Michael Walle13179c22016-09-01 11:21:40 +0200452 uint32_t blkno;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000453 unsigned int blkoff;
Michael Walle13179c22016-09-01 11:21:40 +0200454 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
455 uint32_t inode_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000456 struct ext2_inode *inode_buffer = NULL;
457 struct ext2_block_group *bgd = NULL;
458 struct ext_filesystem *fs = get_fs();
459 char *journal_buffer = zalloc(fs->blksz);
460 if (!journal_buffer)
461 return -ENOMEM;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000462 status = ext4fs_read_inode(ext4fs_root, inodeno, &inode);
463 if (status == 0)
464 goto fail;
465
466 /* read the block no allocated to a file */
Michael Walle13179c22016-09-01 11:21:40 +0200467 no_blocks = le32_to_cpu(inode.size) / fs->blksz;
468 if (le32_to_cpu(inode.size) % fs->blksz)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000469 no_blocks++;
470
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100471 /*
472 * special case for symlinks whose target are small enough that
473 *it fits in struct ext2_inode.b.symlink: no block had been allocated
474 */
Corentin GUILLEVICd4620ae2023-03-17 13:15:12 +0100475 if (S_ISLNK(le16_to_cpu(inode.mode)) &&
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100476 le32_to_cpu(inode.size) <= sizeof(inode.b.symlink)) {
477 no_blocks = 0;
478 }
479
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000480 if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) {
Stefan Brüns03527132016-09-06 04:36:54 +0200481 /* FIXME delete extent index blocks, i.e. eh_depth >= 1 */
482 struct ext4_extent_header *eh =
483 (struct ext4_extent_header *)
484 inode.b.blocks.dir_blocks;
485 debug("del: dep=%d entries=%d\n", eh->eh_depth, eh->eh_entries);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000486 } else {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000487 delete_single_indirect_block(&inode);
488 delete_double_indirect_block(&inode);
489 delete_triple_indirect_block(&inode);
Stefan Brüns03527132016-09-06 04:36:54 +0200490 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000491
Stefan Brüns03527132016-09-06 04:36:54 +0200492 /* release data blocks */
493 for (i = 0; i < no_blocks; i++) {
Stephen Warren02d6ca72019-01-30 12:58:05 -0700494 blknr = read_allocated_block(&inode, i, NULL);
Stefan Brüns74674ed2016-09-06 04:36:55 +0200495 if (blknr == 0)
496 continue;
497 if (blknr < 0)
498 goto fail;
Stefan Brüns03527132016-09-06 04:36:54 +0200499 bg_idx = blknr / blk_per_grp;
500 if (fs->blksz == 1024) {
501 remainder = blknr % blk_per_grp;
502 if (!remainder)
503 bg_idx--;
504 }
505 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx],
506 bg_idx);
507 debug("EXT4 Block releasing %ld: %d\n", blknr, bg_idx);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000508
Stefan Brünsbad73812016-09-17 02:10:10 +0200509 /* get block group descriptor table */
510 bgd = ext4fs_get_group_descriptor(fs, bg_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200511 ext4fs_bg_free_blocks_inc(bgd, fs);
Stefan Brüns03527132016-09-06 04:36:54 +0200512 ext4fs_sb_free_blocks_inc(fs->sb);
513 /* journal backup */
514 if (prev_bg_bmap_idx != bg_idx) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200515 uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
516 status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
Stefan Brüns03527132016-09-06 04:36:54 +0200517 0, fs->blksz,
518 journal_buffer);
519 if (status == 0)
520 goto fail;
Stefan Brünsbad73812016-09-17 02:10:10 +0200521 if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
Stefan Brüns03527132016-09-06 04:36:54 +0200522 goto fail;
523 prev_bg_bmap_idx = bg_idx;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000524 }
525 }
526
Stefan Brüns03527132016-09-06 04:36:54 +0200527 /* release inode */
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000528 /* from the inode no to blockno */
529 inodes_per_block = fs->blksz / fs->inodesz;
530 ibmap_idx = inodeno / inode_per_grp;
531
532 /* get the block no */
533 inodeno--;
Stefan Brünsbad73812016-09-17 02:10:10 +0200534 /* get block group descriptor table */
535 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
536 blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Michael Walle13179c22016-09-01 11:21:40 +0200537 (inodeno % inode_per_grp) / inodes_per_block;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000538
539 /* get the offset of the inode */
540 blkoff = ((inodeno) % inodes_per_block) * fs->inodesz;
541
542 /* read the block no containing the inode */
543 read_buffer = zalloc(fs->blksz);
544 if (!read_buffer)
545 goto fail;
546 start_block_address = read_buffer;
Frederic Leroye7ee0282013-06-26 18:11:25 +0200547 status = ext4fs_devread((lbaint_t)blkno * fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000548 0, fs->blksz, read_buffer);
549 if (status == 0)
550 goto fail;
551
552 if (ext4fs_log_journal(read_buffer, blkno))
553 goto fail;
554
555 read_buffer = read_buffer + blkoff;
556 inode_buffer = (struct ext2_inode *)read_buffer;
Stefan Brüns7d936db2016-09-06 04:36:53 +0200557 memset(inode_buffer, '\0', fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000558
559 /* write the inode to original position in inode table */
560 if (ext4fs_put_metadata(start_block_address, blkno))
561 goto fail;
562
563 /* update the respective inode bitmaps */
564 inodeno++;
565 ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx);
Stefan Brüns05313d12016-09-20 01:13:01 +0200566 ext4fs_bg_free_inodes_inc(bgd, fs);
Michael Walle13179c22016-09-01 11:21:40 +0200567 ext4fs_sb_free_inodes_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000568 /* journal backup */
569 memset(journal_buffer, '\0', fs->blksz);
Stefan Brünsbad73812016-09-17 02:10:10 +0200570 status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000571 fs->sect_perblk, 0, fs->blksz, journal_buffer);
572 if (status == 0)
573 goto fail;
Stefan Brünsbad73812016-09-17 02:10:10 +0200574 if (ext4fs_log_journal(journal_buffer, ext4fs_bg_get_inode_id(bgd, fs)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000575 goto fail;
576
577 ext4fs_update();
578 ext4fs_deinit();
Łukasz Majewski900db5d2014-05-06 09:36:05 +0200579 ext4fs_reinit_global();
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000580
581 if (ext4fs_init() != 0) {
582 printf("error in File System init\n");
583 goto fail;
584 }
585
586 free(start_block_address);
587 free(journal_buffer);
588
589 return 0;
590fail:
591 free(start_block_address);
592 free(journal_buffer);
593
594 return -1;
595}
596
597int ext4fs_init(void)
598{
599 short status;
600 int i;
Michael Walle13179c22016-09-01 11:21:40 +0200601 uint32_t real_free_blocks = 0;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000602 struct ext_filesystem *fs = get_fs();
603
604 /* populate fs */
605 fs->blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich7b1b2552013-05-01 01:13:19 +0000606 fs->sect_perblk = fs->blksz >> fs->dev_desc->log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000607
608 /* get the superblock */
609 fs->sb = zalloc(SUPERBLOCK_SIZE);
610 if (!fs->sb)
611 return -ENOMEM;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000612 if (!ext4_read_superblock((char *)fs->sb))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000613 goto fail;
614
615 /* init journal */
616 if (ext4fs_init_journal())
617 goto fail;
618
619 /* get total no of blockgroups */
620 fs->no_blkgrp = (uint32_t)ext4fs_div_roundup(
Michael Walle13179c22016-09-01 11:21:40 +0200621 le32_to_cpu(ext4fs_root->sblock.total_blocks)
622 - le32_to_cpu(ext4fs_root->sblock.first_data_block),
623 le32_to_cpu(ext4fs_root->sblock.blocks_per_group));
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000624
625 /* get the block group descriptor table */
626 fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1);
627 if (ext4fs_get_bgdtable() == -1) {
628 printf("Error in getting the block group descriptor table\n");
629 goto fail;
630 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000631
632 /* load all the available bitmap block of the partition */
633 fs->blk_bmaps = zalloc(fs->no_blkgrp * sizeof(char *));
634 if (!fs->blk_bmaps)
635 goto fail;
636 for (i = 0; i < fs->no_blkgrp; i++) {
637 fs->blk_bmaps[i] = zalloc(fs->blksz);
638 if (!fs->blk_bmaps[i])
639 goto fail;
640 }
641
642 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200643 struct ext2_block_group *bgd =
644 ext4fs_get_group_descriptor(fs, i);
645 status = ext4fs_devread(ext4fs_bg_get_block_id(bgd, fs) *
Frederic Leroye7ee0282013-06-26 18:11:25 +0200646 fs->sect_perblk, 0,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000647 fs->blksz, (char *)fs->blk_bmaps[i]);
648 if (status == 0)
649 goto fail;
650 }
651
652 /* load all the available inode bitmap of the partition */
653 fs->inode_bmaps = zalloc(fs->no_blkgrp * sizeof(unsigned char *));
654 if (!fs->inode_bmaps)
655 goto fail;
656 for (i = 0; i < fs->no_blkgrp; i++) {
657 fs->inode_bmaps[i] = zalloc(fs->blksz);
658 if (!fs->inode_bmaps[i])
659 goto fail;
660 }
661
662 for (i = 0; i < fs->no_blkgrp; i++) {
Stefan Brünsbad73812016-09-17 02:10:10 +0200663 struct ext2_block_group *bgd =
664 ext4fs_get_group_descriptor(fs, i);
665 status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) *
Frederic Leroye7ee0282013-06-26 18:11:25 +0200666 fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000667 0, fs->blksz,
668 (char *)fs->inode_bmaps[i]);
669 if (status == 0)
670 goto fail;
671 }
672
673 /*
674 * check filesystem consistency with free blocks of file system
675 * some time we observed that superblock freeblocks does not match
676 * with the blockgroups freeblocks when improper
677 * reboot of a linux kernel
678 */
Stefan Brünsbad73812016-09-17 02:10:10 +0200679 for (i = 0; i < fs->no_blkgrp; i++) {
680 struct ext2_block_group *bgd =
681 ext4fs_get_group_descriptor(fs, i);
682 real_free_blocks = real_free_blocks +
683 ext4fs_bg_get_free_blocks(bgd, fs);
684 }
685 if (real_free_blocks != ext4fs_sb_get_free_blocks(fs->sb))
686 ext4fs_sb_set_free_blocks(fs->sb, real_free_blocks);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000687
688 return 0;
689fail:
690 ext4fs_deinit();
691
692 return -1;
693}
694
695void ext4fs_deinit(void)
696{
697 int i;
698 struct ext2_inode inode_journal;
699 struct journal_superblock_t *jsb;
Michael Walle13179c22016-09-01 11:21:40 +0200700 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000701 struct ext_filesystem *fs = get_fs();
Michael Walle13179c22016-09-01 11:21:40 +0200702 uint32_t new_feature_incompat;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000703
704 /* free journal */
705 char *temp_buff = zalloc(fs->blksz);
706 if (temp_buff) {
707 ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
708 &inode_journal);
709 blknr = read_allocated_block(&inode_journal,
Stephen Warren02d6ca72019-01-30 12:58:05 -0700710 EXT2_JOURNAL_SUPERBLOCK, NULL);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200711 ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000712 temp_buff);
713 jsb = (struct journal_superblock_t *)temp_buff;
Michael Walle13179c22016-09-01 11:21:40 +0200714 jsb->s_start = 0;
Ma Haijune0996ca2014-01-08 08:15:33 +0800715 put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000716 (struct journal_superblock_t *)temp_buff, fs->blksz);
717 free(temp_buff);
718 }
719 ext4fs_free_journal();
720
721 /* get the superblock */
Egbert Eich7b1b2552013-05-01 01:13:19 +0000722 ext4_read_superblock((char *)fs->sb);
Michael Walle13179c22016-09-01 11:21:40 +0200723 new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat);
724 new_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
725 fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000726 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
727 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
728 free(fs->sb);
729 fs->sb = NULL;
730
731 if (fs->blk_bmaps) {
732 for (i = 0; i < fs->no_blkgrp; i++) {
733 free(fs->blk_bmaps[i]);
734 fs->blk_bmaps[i] = NULL;
735 }
736 free(fs->blk_bmaps);
737 fs->blk_bmaps = NULL;
738 }
739
740 if (fs->inode_bmaps) {
741 for (i = 0; i < fs->no_blkgrp; i++) {
742 free(fs->inode_bmaps[i]);
743 fs->inode_bmaps[i] = NULL;
744 }
745 free(fs->inode_bmaps);
746 fs->inode_bmaps = NULL;
747 }
748
749
750 free(fs->gdtable);
751 fs->gdtable = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000752 /*
753 * reinitiliazed the global inode and
754 * block bitmap first execution check variables
755 */
756 fs->first_pass_ibmap = 0;
757 fs->first_pass_bbmap = 0;
758 fs->curr_inode_no = 0;
759 fs->curr_blkno = 0;
760}
761
Stefan Brüns74674ed2016-09-06 04:36:55 +0200762/*
763 * Write data to filesystem blocks. Uses same optimization for
764 * contigous sectors as ext4fs_read_file
765 */
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000766static int ext4fs_write_file(struct ext2_inode *file_inode,
Jean-Jacques Hiblotd1921362019-02-13 12:15:24 +0100767 int pos, unsigned int len, const char *buf)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000768{
769 int i;
770 int blockcnt;
Michael Walle13179c22016-09-01 11:21:40 +0200771 uint32_t filesize = le32_to_cpu(file_inode->size);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000772 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-05-01 01:13:19 +0000773 int log2blksz = fs->dev_desc->log2blksz;
774 int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000775 int previous_block_number = -1;
776 int delayed_start = 0;
777 int delayed_extent = 0;
778 int delayed_next = 0;
Jean-Jacques Hiblotd1921362019-02-13 12:15:24 +0100779 const char *delayed_buf = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000780
781 /* Adjust len so it we can't read past the end of the file. */
782 if (len > filesize)
783 len = filesize;
784
785 blockcnt = ((len + pos) + fs->blksz - 1) / fs->blksz;
786
787 for (i = pos / fs->blksz; i < blockcnt; i++) {
788 long int blknr;
789 int blockend = fs->blksz;
790 int skipfirst = 0;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700791 blknr = read_allocated_block(file_inode, i, NULL);
Stefan Brüns74674ed2016-09-06 04:36:55 +0200792 if (blknr <= 0)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000793 return -1;
794
Egbert Eich7b1b2552013-05-01 01:13:19 +0000795 blknr = blknr << log2_fs_blocksize;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000796
797 if (blknr) {
798 if (previous_block_number != -1) {
799 if (delayed_next == blknr) {
800 delayed_extent += blockend;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000801 delayed_next += blockend >> log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000802 } else { /* spill */
Egbert Eich7b1b2552013-05-01 01:13:19 +0000803 put_ext4((uint64_t)
Ma Haijune0996ca2014-01-08 08:15:33 +0800804 ((uint64_t)delayed_start << log2blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000805 delayed_buf,
806 (uint32_t) delayed_extent);
807 previous_block_number = blknr;
808 delayed_start = blknr;
809 delayed_extent = blockend;
810 delayed_buf = buf;
811 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000812 (blockend >> log2blksz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000813 }
814 } else {
815 previous_block_number = blknr;
816 delayed_start = blknr;
817 delayed_extent = blockend;
818 delayed_buf = buf;
819 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000820 (blockend >> log2blksz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000821 }
822 } else {
823 if (previous_block_number != -1) {
824 /* spill */
Ma Haijune0996ca2014-01-08 08:15:33 +0800825 put_ext4((uint64_t) ((uint64_t)delayed_start <<
Egbert Eich7b1b2552013-05-01 01:13:19 +0000826 log2blksz),
827 delayed_buf,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000828 (uint32_t) delayed_extent);
829 previous_block_number = -1;
830 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000831 }
832 buf += fs->blksz - skipfirst;
833 }
834 if (previous_block_number != -1) {
835 /* spill */
Ma Haijune0996ca2014-01-08 08:15:33 +0800836 put_ext4((uint64_t) ((uint64_t)delayed_start << log2blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000837 delayed_buf, (uint32_t) delayed_extent);
838 previous_block_number = -1;
839 }
840
841 return len;
842}
843
Jean-Jacques Hiblotd1921362019-02-13 12:15:24 +0100844int ext4fs_write(const char *fname, const char *buffer,
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100845 unsigned long sizebytes, int type)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000846{
847 int ret = 0;
848 struct ext2_inode *file_inode = NULL;
developer3a7e1a42024-03-19 17:20:40 +0800849 struct ext2_inode *existing_file_inode = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000850 unsigned char *inode_buffer = NULL;
851 int parent_inodeno;
852 int inodeno;
853 time_t timestamp = 0;
854
855 uint64_t bytes_reqd_for_file;
856 unsigned int blks_reqd_for_file;
857 unsigned int blocks_remaining;
858 int existing_file_inodeno;
859 char *temp_ptr = NULL;
860 long int itable_blkno;
861 long int parent_itable_blkno;
862 long int blkoff;
863 struct ext2_sblock *sblock = &(ext4fs_root->sblock);
864 unsigned int inodes_per_block;
865 unsigned int ibmap_idx;
Stefan Brünsbad73812016-09-17 02:10:10 +0200866 struct ext2_block_group *bgd = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000867 struct ext_filesystem *fs = get_fs();
868 ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256);
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100869 bool store_link_in_inode = false;
Jeroen Hofsteef978b002014-06-09 15:29:00 +0200870 memset(filename, 0x00, 256);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000871
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100872 if (type != FILETYPE_REG && type != FILETYPE_SYMLINK)
873 return -1;
874
Stefan Brüns7d936db2016-09-06 04:36:53 +0200875 g_parent_inode = zalloc(fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000876 if (!g_parent_inode)
877 goto fail;
878
879 if (ext4fs_init() != 0) {
880 printf("error in File System init\n");
881 return -1;
882 }
Sébastien Szymanskif7d49682019-03-22 09:33:52 +0100883
884 if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
885 printf("Unsupported feature metadata_csum found, not writing.\n");
886 return -1;
887 }
888
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000889 inodes_per_block = fs->blksz / fs->inodesz;
890 parent_inodeno = ext4fs_get_parent_inode_num(fname, filename, F_FILE);
891 if (parent_inodeno == -1)
892 goto fail;
893 if (ext4fs_iget(parent_inodeno, g_parent_inode))
894 goto fail;
Stefan Brüns57db8992016-09-06 04:36:45 +0200895 /* do not mess up a directory using hash trees */
896 if (le32_to_cpu(g_parent_inode->flags) & EXT4_INDEX_FL) {
897 printf("hash tree directory\n");
898 goto fail;
899 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000900 /* check if the filename is already present in root */
Stefan Brüns278f5d32016-09-06 04:36:41 +0200901 existing_file_inodeno = ext4fs_filename_unlink(filename);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000902 if (existing_file_inodeno != -1) {
developer3a7e1a42024-03-19 17:20:40 +0800903 existing_file_inode = (struct ext2_inode *)zalloc(fs->inodesz);
904 if (!existing_file_inode)
905 goto fail;
906 ret = ext4fs_iget(existing_file_inodeno, existing_file_inode);
907 if (ret) {
908 free(existing_file_inode);
909 goto fail;
910 }
911
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000912 ret = ext4fs_delete_file(existing_file_inodeno);
913 fs->first_pass_bbmap = 0;
914 fs->curr_blkno = 0;
915
916 fs->first_pass_ibmap = 0;
917 fs->curr_inode_no = 0;
918 if (ret)
919 goto fail;
920 }
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100921
922 /* calculate how many blocks required */
923 if (type == FILETYPE_SYMLINK &&
924 sizebytes <= sizeof(file_inode->b.symlink)) {
925 store_link_in_inode = true;
926 bytes_reqd_for_file = 0;
927 } else {
928 bytes_reqd_for_file = sizebytes;
929 }
930
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000931 blks_reqd_for_file = lldiv(bytes_reqd_for_file, fs->blksz);
932 if (do_div(bytes_reqd_for_file, fs->blksz) != 0) {
933 blks_reqd_for_file++;
934 debug("total bytes for a file %u\n", blks_reqd_for_file);
935 }
936 blocks_remaining = blks_reqd_for_file;
937 /* test for available space in partition */
Michael Walle13179c22016-09-01 11:21:40 +0200938 if (le32_to_cpu(fs->sb->free_blocks) < blks_reqd_for_file) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000939 printf("Not enough space on partition !!!\n");
940 goto fail;
941 }
942
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100943 inodeno = ext4fs_update_parent_dentry(filename, type);
Stefan Brünsc1020682016-09-06 04:36:42 +0200944 if (inodeno == -1)
945 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000946 /* prepare file inode */
947 inode_buffer = zalloc(fs->inodesz);
948 if (!inode_buffer)
949 goto fail;
950 file_inode = (struct ext2_inode *)inode_buffer;
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100951 file_inode->size = cpu_to_le32(sizebytes);
952 if (type == FILETYPE_SYMLINK) {
953 file_inode->mode = cpu_to_le16(S_IFLNK | S_IRWXU | S_IRWXG |
954 S_IRWXO);
955 if (store_link_in_inode) {
956 strncpy(file_inode->b.symlink, buffer, sizebytes);
957 sizebytes = 0;
958 }
959 } else {
developer3a7e1a42024-03-19 17:20:40 +0800960 if (existing_file_inode) {
961 file_inode->mode = existing_file_inode->mode;
962 } else {
963 file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU | S_IRGRP |
964 S_IROTH | S_IXGRP | S_IXOTH);
965 }
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +0100966 }
developer3a7e1a42024-03-19 17:20:40 +0800967 if (existing_file_inode)
968 free(existing_file_inode);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000969 /* ToDo: Update correct time */
Michael Walle13179c22016-09-01 11:21:40 +0200970 file_inode->mtime = cpu_to_le32(timestamp);
971 file_inode->atime = cpu_to_le32(timestamp);
972 file_inode->ctime = cpu_to_le32(timestamp);
973 file_inode->nlinks = cpu_to_le16(1);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000974
975 /* Allocate data blocks */
976 ext4fs_allocate_blocks(file_inode, blocks_remaining,
977 &blks_reqd_for_file);
Michael Walle13179c22016-09-01 11:21:40 +0200978 file_inode->blockcnt = cpu_to_le32((blks_reqd_for_file * fs->blksz) >>
Marek Szyprowski07a39ab2019-06-21 15:32:51 +0200979 LOG2_SECTOR_SIZE);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000980
981 temp_ptr = zalloc(fs->blksz);
982 if (!temp_ptr)
983 goto fail;
Michael Walle13179c22016-09-01 11:21:40 +0200984 ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000985 inodeno--;
Stefan Brünsbad73812016-09-17 02:10:10 +0200986 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
987 itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Michael Wallec07cdcb2016-08-29 10:46:44 +0200988 (inodeno % le32_to_cpu(sblock->inodes_per_group)) /
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000989 inodes_per_block;
990 blkoff = (inodeno % inodes_per_block) * fs->inodesz;
Frederic Leroye7ee0282013-06-26 18:11:25 +0200991 ext4fs_devread((lbaint_t)itable_blkno * fs->sect_perblk, 0, fs->blksz,
992 temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000993 if (ext4fs_log_journal(temp_ptr, itable_blkno))
994 goto fail;
995
996 memcpy(temp_ptr + blkoff, inode_buffer, fs->inodesz);
997 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
998 goto fail;
999 /* copy the file content into data blocks */
Jean-Jacques Hiblotd1921362019-02-13 12:15:24 +01001000 if (ext4fs_write_file(file_inode, 0, sizebytes, buffer) == -1) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001001 printf("Error in copying content\n");
Stefan Brüns74674ed2016-09-06 04:36:55 +02001002 /* FIXME: Deallocate data blocks */
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001003 goto fail;
1004 }
Michael Walle13179c22016-09-01 11:21:40 +02001005 ibmap_idx = parent_inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001006 parent_inodeno--;
Stefan Brünsbad73812016-09-17 02:10:10 +02001007 bgd = ext4fs_get_group_descriptor(fs, ibmap_idx);
1008 parent_itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) +
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001009 (parent_inodeno %
Michael Wallec07cdcb2016-08-29 10:46:44 +02001010 le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001011 blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz;
1012 if (parent_itable_blkno != itable_blkno) {
1013 memset(temp_ptr, '\0', fs->blksz);
Frederic Leroye7ee0282013-06-26 18:11:25 +02001014 ext4fs_devread((lbaint_t)parent_itable_blkno * fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001015 0, fs->blksz, temp_ptr);
1016 if (ext4fs_log_journal(temp_ptr, parent_itable_blkno))
1017 goto fail;
1018
Stefan Brüns7d936db2016-09-06 04:36:53 +02001019 memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001020 if (ext4fs_put_metadata(temp_ptr, parent_itable_blkno))
1021 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001022 } else {
1023 /*
1024 * If parent and child fall in same inode table block
1025 * both should be kept in 1 buffer
1026 */
Stefan Brüns7d936db2016-09-06 04:36:53 +02001027 memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001028 gd_index--;
1029 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
1030 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001031 }
1032 ext4fs_update();
1033 ext4fs_deinit();
1034
1035 fs->first_pass_bbmap = 0;
1036 fs->curr_blkno = 0;
1037 fs->first_pass_ibmap = 0;
1038 fs->curr_inode_no = 0;
1039 free(inode_buffer);
1040 free(g_parent_inode);
Stefan Brüns338a5292016-09-06 04:36:51 +02001041 free(temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001042 g_parent_inode = NULL;
1043
1044 return 0;
1045fail:
1046 ext4fs_deinit();
1047 free(inode_buffer);
1048 free(g_parent_inode);
Stefan Brüns338a5292016-09-06 04:36:51 +02001049 free(temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +00001050 g_parent_inode = NULL;
1051
1052 return -1;
1053}
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001054
1055int ext4_write_file(const char *filename, void *buf, loff_t offset,
1056 loff_t len, loff_t *actwrite)
1057{
1058 int ret;
1059
1060 if (offset != 0) {
1061 printf("** Cannot support non-zero offset **\n");
1062 return -1;
1063 }
1064
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +01001065 ret = ext4fs_write(filename, buf, len, FILETYPE_REG);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001066 if (ret) {
1067 printf("** Error ext4fs_write() **\n");
1068 goto fail;
1069 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001070
Przemyslaw Marczak2b1d6c92015-02-17 15:31:52 +01001071 *actwrite = len;
1072
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001073 return 0;
1074
1075fail:
Przemyslaw Marczak2b1d6c92015-02-17 15:31:52 +01001076 *actwrite = 0;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -08001077
1078 return -1;
1079}
Jean-Jacques Hiblot448dd272019-02-13 12:15:25 +01001080
1081int ext4fs_create_link(const char *target, const char *fname)
1082{
1083 return ext4fs_write(fname, target, strlen(target), FILETYPE_SYMLINK);
1084}