blob: e4f0905876e7536c49bda8393f3099deb4d8dbe1 [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
41static inline void ext4fs_bg_free_inodes_inc(struct ext2_block_group *bg)
42{
43 bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) + 1);
44}
45
46static inline void ext4fs_bg_free_blocks_inc(struct ext2_block_group *bg)
47{
48 bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) + 1);
49}
50
Simon Glass6c8f3aa2012-12-26 09:53:28 +000051static void ext4fs_update(void)
52{
53 short i;
54 ext4fs_update_journal();
55 struct ext_filesystem *fs = get_fs();
56
57 /* update super block */
58 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
59 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
60
61 /* update block groups */
62 for (i = 0; i < fs->no_blkgrp; i++) {
Michael Walle13179c22016-09-01 11:21:40 +020063 fs->bgd[i].bg_checksum = cpu_to_le16(ext4fs_checksum_update(i));
64 put_ext4((uint64_t)le32_to_cpu(fs->bgd[i].block_id) * fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +000065 fs->blk_bmaps[i], fs->blksz);
66 }
67
68 /* update inode table groups */
69 for (i = 0; i < fs->no_blkgrp; i++) {
Michael Walle13179c22016-09-01 11:21:40 +020070 put_ext4((uint64_t)le32_to_cpu(fs->bgd[i].inode_id) * fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +000071 fs->inode_bmaps[i], fs->blksz);
72 }
73
74 /* update the block group descriptor table */
Ma Haijune0996ca2014-01-08 08:15:33 +080075 put_ext4((uint64_t)((uint64_t)fs->gdtable_blkno * (uint64_t)fs->blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +000076 (struct ext2_block_group *)fs->gdtable,
77 (fs->blksz * fs->no_blk_pergdt));
78
79 ext4fs_dump_metadata();
80
81 gindex = 0;
82 gd_index = 0;
83}
84
85int ext4fs_get_bgdtable(void)
86{
87 int status;
88 int grp_desc_size;
89 struct ext_filesystem *fs = get_fs();
90 grp_desc_size = sizeof(struct ext2_block_group);
91 fs->no_blk_pergdt = (fs->no_blkgrp * grp_desc_size) / fs->blksz;
92 if ((fs->no_blkgrp * grp_desc_size) % fs->blksz)
93 fs->no_blk_pergdt++;
94
95 /* allocate memory for gdtable */
96 fs->gdtable = zalloc(fs->blksz * fs->no_blk_pergdt);
97 if (!fs->gdtable)
98 return -ENOMEM;
99 /* read the group descriptor table */
Frederic Leroye7ee0282013-06-26 18:11:25 +0200100 status = ext4fs_devread((lbaint_t)fs->gdtable_blkno * fs->sect_perblk,
101 0, fs->blksz * fs->no_blk_pergdt, fs->gdtable);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000102 if (status == 0)
103 goto fail;
104
105 if (ext4fs_log_gdt(fs->gdtable)) {
106 printf("Error in ext4fs_log_gdt\n");
107 return -1;
108 }
109
110 return 0;
111fail:
112 free(fs->gdtable);
113 fs->gdtable = NULL;
114
115 return -1;
116}
117
118static void delete_single_indirect_block(struct ext2_inode *inode)
119{
120 struct ext2_block_group *bgd = NULL;
121 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200122 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000123 int remainder;
124 int bg_idx;
125 int status;
Michael Walle13179c22016-09-01 11:21:40 +0200126 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000127 struct ext_filesystem *fs = get_fs();
128 char *journal_buffer = zalloc(fs->blksz);
129 if (!journal_buffer) {
130 printf("No memory\n");
131 return;
132 }
133 /* get block group descriptor table */
134 bgd = (struct ext2_block_group *)fs->gdtable;
135
136 /* deleting the single indirect block associated with inode */
137 if (inode->b.blocks.indir_block != 0) {
Michael Walle13179c22016-09-01 11:21:40 +0200138 blknr = le32_to_cpu(inode->b.blocks.indir_block);
139 debug("SIPB releasing %u\n", blknr);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200140 bg_idx = blknr / blk_per_grp;
141 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000142 remainder = blknr % blk_per_grp;
143 if (!remainder)
144 bg_idx--;
145 }
146 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Michael Walle13179c22016-09-01 11:21:40 +0200147 ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
148 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000149 /* journal backup */
150 if (prev_bg_bmap_idx != bg_idx) {
Michael Walle13179c22016-09-01 11:21:40 +0200151 status = ext4fs_devread(
152 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000153 fs->sect_perblk, 0, fs->blksz,
154 journal_buffer);
155 if (status == 0)
156 goto fail;
157 if (ext4fs_log_journal
Michael Walle13179c22016-09-01 11:21:40 +0200158 (journal_buffer, le32_to_cpu(bgd[bg_idx].block_id)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000159 goto fail;
160 prev_bg_bmap_idx = bg_idx;
161 }
162 }
163fail:
164 free(journal_buffer);
165}
166
167static void delete_double_indirect_block(struct ext2_inode *inode)
168{
169 int i;
170 short status;
171 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200172 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000173 int remainder;
174 int bg_idx;
Michael Walle13179c22016-09-01 11:21:40 +0200175 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
176 __le32 *di_buffer = NULL;
177 void *dib_start_addr = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000178 struct ext2_block_group *bgd = NULL;
179 struct ext_filesystem *fs = get_fs();
180 char *journal_buffer = zalloc(fs->blksz);
181 if (!journal_buffer) {
182 printf("No memory\n");
183 return;
184 }
185 /* get the block group descriptor table */
186 bgd = (struct ext2_block_group *)fs->gdtable;
187
188 if (inode->b.blocks.double_indir_block != 0) {
189 di_buffer = zalloc(fs->blksz);
190 if (!di_buffer) {
191 printf("No memory\n");
192 return;
193 }
Michael Walle13179c22016-09-01 11:21:40 +0200194 dib_start_addr = di_buffer;
195 blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200196 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
197 fs->blksz, (char *)di_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000198 for (i = 0; i < fs->blksz / sizeof(int); i++) {
199 if (*di_buffer == 0)
200 break;
201
202 debug("DICB releasing %u\n", *di_buffer);
Michael Walle13179c22016-09-01 11:21:40 +0200203 bg_idx = le32_to_cpu(*di_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200204 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200205 remainder = le32_to_cpu(*di_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000206 if (!remainder)
207 bg_idx--;
208 }
Michael Walle13179c22016-09-01 11:21:40 +0200209 ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000210 fs->blk_bmaps[bg_idx], bg_idx);
211 di_buffer++;
Michael Walle13179c22016-09-01 11:21:40 +0200212 ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
213 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000214 /* journal backup */
215 if (prev_bg_bmap_idx != bg_idx) {
Michael Walle13179c22016-09-01 11:21:40 +0200216 status = ext4fs_devread(
217 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000218 * fs->sect_perblk, 0,
219 fs->blksz,
220 journal_buffer);
221 if (status == 0)
222 goto fail;
223
224 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +0200225 le32_to_cpu(bgd[bg_idx].block_id)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000226 goto fail;
227 prev_bg_bmap_idx = bg_idx;
228 }
229 }
230
231 /* removing the parent double indirect block */
Michael Walle13179c22016-09-01 11:21:40 +0200232 blknr = le32_to_cpu(inode->b.blocks.double_indir_block);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200233 bg_idx = blknr / blk_per_grp;
234 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000235 remainder = blknr % blk_per_grp;
236 if (!remainder)
237 bg_idx--;
238 }
239 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Michael Walle13179c22016-09-01 11:21:40 +0200240 ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
241 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000242 /* journal backup */
243 if (prev_bg_bmap_idx != bg_idx) {
244 memset(journal_buffer, '\0', fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +0200245 status = ext4fs_devread((lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000246 fs->sect_perblk, 0, fs->blksz,
247 journal_buffer);
248 if (status == 0)
249 goto fail;
250
251 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +0200252 le32_to_cpu(bgd[bg_idx].block_id)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000253 goto fail;
254 prev_bg_bmap_idx = bg_idx;
255 }
Michael Walle13179c22016-09-01 11:21:40 +0200256 debug("DIPB releasing %d\n", blknr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000257 }
258fail:
Michael Walle13179c22016-09-01 11:21:40 +0200259 free(dib_start_addr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000260 free(journal_buffer);
261}
262
263static void delete_triple_indirect_block(struct ext2_inode *inode)
264{
265 int i, j;
266 short status;
267 static int prev_bg_bmap_idx = -1;
Michael Walle13179c22016-09-01 11:21:40 +0200268 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000269 int remainder;
270 int bg_idx;
Michael Walle13179c22016-09-01 11:21:40 +0200271 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
272 __le32 *tigp_buffer = NULL;
273 void *tib_start_addr = NULL;
274 __le32 *tip_buffer = NULL;
275 void *tipb_start_addr = NULL;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000276 struct ext2_block_group *bgd = NULL;
277 struct ext_filesystem *fs = get_fs();
278 char *journal_buffer = zalloc(fs->blksz);
279 if (!journal_buffer) {
280 printf("No memory\n");
281 return;
282 }
283 /* get block group descriptor table */
284 bgd = (struct ext2_block_group *)fs->gdtable;
285
286 if (inode->b.blocks.triple_indir_block != 0) {
287 tigp_buffer = zalloc(fs->blksz);
288 if (!tigp_buffer) {
289 printf("No memory\n");
290 return;
291 }
Michael Walle13179c22016-09-01 11:21:40 +0200292 tib_start_addr = tigp_buffer;
293 blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200294 status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
295 fs->blksz, (char *)tigp_buffer);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000296 for (i = 0; i < fs->blksz / sizeof(int); i++) {
297 if (*tigp_buffer == 0)
298 break;
299 debug("tigp buffer releasing %u\n", *tigp_buffer);
300
301 tip_buffer = zalloc(fs->blksz);
302 if (!tip_buffer)
303 goto fail;
Michael Walle13179c22016-09-01 11:21:40 +0200304 tipb_start_addr = tip_buffer;
305 status = ext4fs_devread((lbaint_t)le32_to_cpu(*tigp_buffer) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000306 fs->sect_perblk, 0, fs->blksz,
307 (char *)tip_buffer);
308 for (j = 0; j < fs->blksz / sizeof(int); j++) {
Michael Walle13179c22016-09-01 11:21:40 +0200309 if (le32_to_cpu(*tip_buffer) == 0)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000310 break;
Michael Walle13179c22016-09-01 11:21:40 +0200311 bg_idx = le32_to_cpu(*tip_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200312 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200313 remainder = le32_to_cpu(*tip_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000314 if (!remainder)
315 bg_idx--;
316 }
317
Michael Walle13179c22016-09-01 11:21:40 +0200318 ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000319 fs->blk_bmaps[bg_idx],
320 bg_idx);
321
322 tip_buffer++;
Michael Walle13179c22016-09-01 11:21:40 +0200323 ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
324 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000325 /* journal backup */
326 if (prev_bg_bmap_idx != bg_idx) {
327 status =
328 ext4fs_devread(
Michael Walle13179c22016-09-01 11:21:40 +0200329 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000330 fs->sect_perblk, 0,
331 fs->blksz,
332 journal_buffer);
333 if (status == 0)
334 goto fail;
335
336 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +0200337 le32_to_cpu(bgd[bg_idx].block_id)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000338 goto fail;
339 prev_bg_bmap_idx = bg_idx;
340 }
341 }
342 free(tipb_start_addr);
343 tipb_start_addr = NULL;
344
345 /*
346 * removing the grand parent blocks
347 * which is connected to inode
348 */
Michael Walle13179c22016-09-01 11:21:40 +0200349 bg_idx = le32_to_cpu(*tigp_buffer) / blk_per_grp;
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200350 if (fs->blksz == 1024) {
Michael Walle13179c22016-09-01 11:21:40 +0200351 remainder = le32_to_cpu(*tigp_buffer) % blk_per_grp;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000352 if (!remainder)
353 bg_idx--;
354 }
Michael Walle13179c22016-09-01 11:21:40 +0200355 ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000356 fs->blk_bmaps[bg_idx], bg_idx);
357
358 tigp_buffer++;
Michael Walle13179c22016-09-01 11:21:40 +0200359 ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
360 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000361 /* journal backup */
362 if (prev_bg_bmap_idx != bg_idx) {
363 memset(journal_buffer, '\0', fs->blksz);
364 status =
Michael Walle13179c22016-09-01 11:21:40 +0200365 ext4fs_devread(
366 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000367 fs->sect_perblk, 0,
368 fs->blksz, journal_buffer);
369 if (status == 0)
370 goto fail;
371
372 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +0200373 le32_to_cpu(bgd[bg_idx].block_id)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000374 goto fail;
375 prev_bg_bmap_idx = bg_idx;
376 }
377 }
378
379 /* removing the grand parent triple indirect block */
Michael Walle13179c22016-09-01 11:21:40 +0200380 blknr = le32_to_cpu(inode->b.blocks.triple_indir_block);
Łukasz Majewski5767dc82014-05-06 09:36:04 +0200381 bg_idx = blknr / blk_per_grp;
382 if (fs->blksz == 1024) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000383 remainder = blknr % blk_per_grp;
384 if (!remainder)
385 bg_idx--;
386 }
387 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx);
Michael Walle13179c22016-09-01 11:21:40 +0200388 ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
389 ext4fs_sb_free_blocks_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000390 /* journal backup */
391 if (prev_bg_bmap_idx != bg_idx) {
392 memset(journal_buffer, '\0', fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +0200393 status = ext4fs_devread(
394 (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000395 fs->sect_perblk, 0, fs->blksz,
396 journal_buffer);
397 if (status == 0)
398 goto fail;
399
400 if (ext4fs_log_journal(journal_buffer,
Michael Walle13179c22016-09-01 11:21:40 +0200401 le32_to_cpu(bgd[bg_idx].block_id)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000402 goto fail;
403 prev_bg_bmap_idx = bg_idx;
404 }
Michael Walle13179c22016-09-01 11:21:40 +0200405 debug("tigp buffer itself releasing %d\n", blknr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000406 }
407fail:
408 free(tib_start_addr);
409 free(tipb_start_addr);
410 free(journal_buffer);
411}
412
413static int ext4fs_delete_file(int inodeno)
414{
415 struct ext2_inode inode;
416 short status;
417 int i;
418 int remainder;
419 long int blknr;
420 int bg_idx;
421 int ibmap_idx;
422 char *read_buffer = NULL;
423 char *start_block_address = NULL;
Michael Walle13179c22016-09-01 11:21:40 +0200424 uint32_t no_blocks;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000425
426 static int prev_bg_bmap_idx = -1;
427 unsigned int inodes_per_block;
Michael Walle13179c22016-09-01 11:21:40 +0200428 uint32_t blkno;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000429 unsigned int blkoff;
Michael Walle13179c22016-09-01 11:21:40 +0200430 uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
431 uint32_t inode_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000432 struct ext2_inode *inode_buffer = NULL;
433 struct ext2_block_group *bgd = NULL;
434 struct ext_filesystem *fs = get_fs();
435 char *journal_buffer = zalloc(fs->blksz);
436 if (!journal_buffer)
437 return -ENOMEM;
438 /* get the block group descriptor table */
439 bgd = (struct ext2_block_group *)fs->gdtable;
440 status = ext4fs_read_inode(ext4fs_root, inodeno, &inode);
441 if (status == 0)
442 goto fail;
443
444 /* read the block no allocated to a file */
Michael Walle13179c22016-09-01 11:21:40 +0200445 no_blocks = le32_to_cpu(inode.size) / fs->blksz;
446 if (le32_to_cpu(inode.size) % fs->blksz)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000447 no_blocks++;
448
449 if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) {
Stefan Brüns03527132016-09-06 04:36:54 +0200450 /* FIXME delete extent index blocks, i.e. eh_depth >= 1 */
451 struct ext4_extent_header *eh =
452 (struct ext4_extent_header *)
453 inode.b.blocks.dir_blocks;
454 debug("del: dep=%d entries=%d\n", eh->eh_depth, eh->eh_entries);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000455 } else {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000456 delete_single_indirect_block(&inode);
457 delete_double_indirect_block(&inode);
458 delete_triple_indirect_block(&inode);
Stefan Brüns03527132016-09-06 04:36:54 +0200459 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000460
Stefan Brüns03527132016-09-06 04:36:54 +0200461 /* release data blocks */
462 for (i = 0; i < no_blocks; i++) {
463 blknr = read_allocated_block(&inode, i);
Stefan Brüns74674ed2016-09-06 04:36:55 +0200464 if (blknr == 0)
465 continue;
466 if (blknr < 0)
467 goto fail;
Stefan Brüns03527132016-09-06 04:36:54 +0200468 bg_idx = blknr / blk_per_grp;
469 if (fs->blksz == 1024) {
470 remainder = blknr % blk_per_grp;
471 if (!remainder)
472 bg_idx--;
473 }
474 ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx],
475 bg_idx);
476 debug("EXT4 Block releasing %ld: %d\n", blknr, bg_idx);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000477
Stefan Brüns03527132016-09-06 04:36:54 +0200478 ext4fs_bg_free_blocks_inc(&bgd[bg_idx]);
479 ext4fs_sb_free_blocks_inc(fs->sb);
480 /* journal backup */
481 if (prev_bg_bmap_idx != bg_idx) {
482 uint32_t bgd_blknr = le32_to_cpu(bgd[bg_idx].block_id);
483 status = ext4fs_devread((lbaint_t)bgd_blknr *
484 fs->sect_perblk,
485 0, fs->blksz,
486 journal_buffer);
487 if (status == 0)
488 goto fail;
489 if (ext4fs_log_journal(journal_buffer, bgd_blknr))
490 goto fail;
491 prev_bg_bmap_idx = bg_idx;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000492 }
493 }
494
Stefan Brüns03527132016-09-06 04:36:54 +0200495 /* release inode */
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000496 /* from the inode no to blockno */
497 inodes_per_block = fs->blksz / fs->inodesz;
498 ibmap_idx = inodeno / inode_per_grp;
499
500 /* get the block no */
501 inodeno--;
Michael Wallec07cdcb2016-08-29 10:46:44 +0200502 blkno = le32_to_cpu(bgd[ibmap_idx].inode_table_id) +
Michael Walle13179c22016-09-01 11:21:40 +0200503 (inodeno % inode_per_grp) / inodes_per_block;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000504
505 /* get the offset of the inode */
506 blkoff = ((inodeno) % inodes_per_block) * fs->inodesz;
507
508 /* read the block no containing the inode */
509 read_buffer = zalloc(fs->blksz);
510 if (!read_buffer)
511 goto fail;
512 start_block_address = read_buffer;
Frederic Leroye7ee0282013-06-26 18:11:25 +0200513 status = ext4fs_devread((lbaint_t)blkno * fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000514 0, fs->blksz, read_buffer);
515 if (status == 0)
516 goto fail;
517
518 if (ext4fs_log_journal(read_buffer, blkno))
519 goto fail;
520
521 read_buffer = read_buffer + blkoff;
522 inode_buffer = (struct ext2_inode *)read_buffer;
Stefan Brüns7d936db2016-09-06 04:36:53 +0200523 memset(inode_buffer, '\0', fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000524
525 /* write the inode to original position in inode table */
526 if (ext4fs_put_metadata(start_block_address, blkno))
527 goto fail;
528
529 /* update the respective inode bitmaps */
530 inodeno++;
531 ext4fs_reset_inode_bmap(inodeno, fs->inode_bmaps[ibmap_idx], ibmap_idx);
Michael Walle13179c22016-09-01 11:21:40 +0200532 ext4fs_bg_free_inodes_inc(&bgd[ibmap_idx]);
533 ext4fs_sb_free_inodes_inc(fs->sb);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000534 /* journal backup */
535 memset(journal_buffer, '\0', fs->blksz);
Michael Walle13179c22016-09-01 11:21:40 +0200536 status = ext4fs_devread((lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id) *
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000537 fs->sect_perblk, 0, fs->blksz, journal_buffer);
538 if (status == 0)
539 goto fail;
Michael Walle13179c22016-09-01 11:21:40 +0200540 if (ext4fs_log_journal(journal_buffer, le32_to_cpu(bgd[ibmap_idx].inode_id)))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000541 goto fail;
542
543 ext4fs_update();
544 ext4fs_deinit();
Łukasz Majewski900db5d2014-05-06 09:36:05 +0200545 ext4fs_reinit_global();
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000546
547 if (ext4fs_init() != 0) {
548 printf("error in File System init\n");
549 goto fail;
550 }
551
552 free(start_block_address);
553 free(journal_buffer);
554
555 return 0;
556fail:
557 free(start_block_address);
558 free(journal_buffer);
559
560 return -1;
561}
562
563int ext4fs_init(void)
564{
565 short status;
566 int i;
Michael Walle13179c22016-09-01 11:21:40 +0200567 uint32_t real_free_blocks = 0;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000568 struct ext_filesystem *fs = get_fs();
569
570 /* populate fs */
571 fs->blksz = EXT2_BLOCK_SIZE(ext4fs_root);
Egbert Eich7b1b2552013-05-01 01:13:19 +0000572 fs->sect_perblk = fs->blksz >> fs->dev_desc->log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000573
574 /* get the superblock */
575 fs->sb = zalloc(SUPERBLOCK_SIZE);
576 if (!fs->sb)
577 return -ENOMEM;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000578 if (!ext4_read_superblock((char *)fs->sb))
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000579 goto fail;
580
581 /* init journal */
582 if (ext4fs_init_journal())
583 goto fail;
584
585 /* get total no of blockgroups */
586 fs->no_blkgrp = (uint32_t)ext4fs_div_roundup(
Michael Walle13179c22016-09-01 11:21:40 +0200587 le32_to_cpu(ext4fs_root->sblock.total_blocks)
588 - le32_to_cpu(ext4fs_root->sblock.first_data_block),
589 le32_to_cpu(ext4fs_root->sblock.blocks_per_group));
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000590
591 /* get the block group descriptor table */
592 fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1);
593 if (ext4fs_get_bgdtable() == -1) {
594 printf("Error in getting the block group descriptor table\n");
595 goto fail;
596 }
597 fs->bgd = (struct ext2_block_group *)fs->gdtable;
598
599 /* load all the available bitmap block of the partition */
600 fs->blk_bmaps = zalloc(fs->no_blkgrp * sizeof(char *));
601 if (!fs->blk_bmaps)
602 goto fail;
603 for (i = 0; i < fs->no_blkgrp; i++) {
604 fs->blk_bmaps[i] = zalloc(fs->blksz);
605 if (!fs->blk_bmaps[i])
606 goto fail;
607 }
608
609 for (i = 0; i < fs->no_blkgrp; i++) {
610 status =
Michael Walle13179c22016-09-01 11:21:40 +0200611 ext4fs_devread(
612 (lbaint_t)le32_to_cpu(fs->bgd[i].block_id) *
Frederic Leroye7ee0282013-06-26 18:11:25 +0200613 fs->sect_perblk, 0,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000614 fs->blksz, (char *)fs->blk_bmaps[i]);
615 if (status == 0)
616 goto fail;
617 }
618
619 /* load all the available inode bitmap of the partition */
620 fs->inode_bmaps = zalloc(fs->no_blkgrp * sizeof(unsigned char *));
621 if (!fs->inode_bmaps)
622 goto fail;
623 for (i = 0; i < fs->no_blkgrp; i++) {
624 fs->inode_bmaps[i] = zalloc(fs->blksz);
625 if (!fs->inode_bmaps[i])
626 goto fail;
627 }
628
629 for (i = 0; i < fs->no_blkgrp; i++) {
Michael Walle13179c22016-09-01 11:21:40 +0200630 status = ext4fs_devread(
631 (lbaint_t)le32_to_cpu(fs->bgd[i].inode_id) *
Frederic Leroye7ee0282013-06-26 18:11:25 +0200632 fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000633 0, fs->blksz,
634 (char *)fs->inode_bmaps[i]);
635 if (status == 0)
636 goto fail;
637 }
638
639 /*
640 * check filesystem consistency with free blocks of file system
641 * some time we observed that superblock freeblocks does not match
642 * with the blockgroups freeblocks when improper
643 * reboot of a linux kernel
644 */
645 for (i = 0; i < fs->no_blkgrp; i++)
Michael Walle13179c22016-09-01 11:21:40 +0200646 real_free_blocks = real_free_blocks + le16_to_cpu(fs->bgd[i].free_blocks);
647 if (real_free_blocks != le32_to_cpu(fs->sb->free_blocks))
648 fs->sb->free_blocks = cpu_to_le32(real_free_blocks);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000649
650 return 0;
651fail:
652 ext4fs_deinit();
653
654 return -1;
655}
656
657void ext4fs_deinit(void)
658{
659 int i;
660 struct ext2_inode inode_journal;
661 struct journal_superblock_t *jsb;
Michael Walle13179c22016-09-01 11:21:40 +0200662 uint32_t blknr;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000663 struct ext_filesystem *fs = get_fs();
Michael Walle13179c22016-09-01 11:21:40 +0200664 uint32_t new_feature_incompat;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000665
666 /* free journal */
667 char *temp_buff = zalloc(fs->blksz);
668 if (temp_buff) {
669 ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO,
670 &inode_journal);
671 blknr = read_allocated_block(&inode_journal,
672 EXT2_JOURNAL_SUPERBLOCK);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200673 ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000674 temp_buff);
675 jsb = (struct journal_superblock_t *)temp_buff;
Michael Walle13179c22016-09-01 11:21:40 +0200676 jsb->s_start = 0;
Ma Haijune0996ca2014-01-08 08:15:33 +0800677 put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000678 (struct journal_superblock_t *)temp_buff, fs->blksz);
679 free(temp_buff);
680 }
681 ext4fs_free_journal();
682
683 /* get the superblock */
Egbert Eich7b1b2552013-05-01 01:13:19 +0000684 ext4_read_superblock((char *)fs->sb);
Michael Walle13179c22016-09-01 11:21:40 +0200685 new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat);
686 new_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
687 fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000688 put_ext4((uint64_t)(SUPERBLOCK_SIZE),
689 (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE);
690 free(fs->sb);
691 fs->sb = NULL;
692
693 if (fs->blk_bmaps) {
694 for (i = 0; i < fs->no_blkgrp; i++) {
695 free(fs->blk_bmaps[i]);
696 fs->blk_bmaps[i] = NULL;
697 }
698 free(fs->blk_bmaps);
699 fs->blk_bmaps = NULL;
700 }
701
702 if (fs->inode_bmaps) {
703 for (i = 0; i < fs->no_blkgrp; i++) {
704 free(fs->inode_bmaps[i]);
705 fs->inode_bmaps[i] = NULL;
706 }
707 free(fs->inode_bmaps);
708 fs->inode_bmaps = NULL;
709 }
710
711
712 free(fs->gdtable);
713 fs->gdtable = NULL;
714 fs->bgd = NULL;
715 /*
716 * reinitiliazed the global inode and
717 * block bitmap first execution check variables
718 */
719 fs->first_pass_ibmap = 0;
720 fs->first_pass_bbmap = 0;
721 fs->curr_inode_no = 0;
722 fs->curr_blkno = 0;
723}
724
Stefan Brüns74674ed2016-09-06 04:36:55 +0200725/*
726 * Write data to filesystem blocks. Uses same optimization for
727 * contigous sectors as ext4fs_read_file
728 */
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000729static int ext4fs_write_file(struct ext2_inode *file_inode,
730 int pos, unsigned int len, char *buf)
731{
732 int i;
733 int blockcnt;
Michael Walle13179c22016-09-01 11:21:40 +0200734 uint32_t filesize = le32_to_cpu(file_inode->size);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000735 struct ext_filesystem *fs = get_fs();
Egbert Eich7b1b2552013-05-01 01:13:19 +0000736 int log2blksz = fs->dev_desc->log2blksz;
737 int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000738 int previous_block_number = -1;
739 int delayed_start = 0;
740 int delayed_extent = 0;
741 int delayed_next = 0;
742 char *delayed_buf = NULL;
743
744 /* Adjust len so it we can't read past the end of the file. */
745 if (len > filesize)
746 len = filesize;
747
748 blockcnt = ((len + pos) + fs->blksz - 1) / fs->blksz;
749
750 for (i = pos / fs->blksz; i < blockcnt; i++) {
751 long int blknr;
752 int blockend = fs->blksz;
753 int skipfirst = 0;
754 blknr = read_allocated_block(file_inode, i);
Stefan Brüns74674ed2016-09-06 04:36:55 +0200755 if (blknr <= 0)
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000756 return -1;
757
Egbert Eich7b1b2552013-05-01 01:13:19 +0000758 blknr = blknr << log2_fs_blocksize;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000759
760 if (blknr) {
761 if (previous_block_number != -1) {
762 if (delayed_next == blknr) {
763 delayed_extent += blockend;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000764 delayed_next += blockend >> log2blksz;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000765 } else { /* spill */
Egbert Eich7b1b2552013-05-01 01:13:19 +0000766 put_ext4((uint64_t)
Ma Haijune0996ca2014-01-08 08:15:33 +0800767 ((uint64_t)delayed_start << log2blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000768 delayed_buf,
769 (uint32_t) delayed_extent);
770 previous_block_number = blknr;
771 delayed_start = blknr;
772 delayed_extent = blockend;
773 delayed_buf = buf;
774 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000775 (blockend >> log2blksz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000776 }
777 } else {
778 previous_block_number = blknr;
779 delayed_start = blknr;
780 delayed_extent = blockend;
781 delayed_buf = buf;
782 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000783 (blockend >> log2blksz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000784 }
785 } else {
786 if (previous_block_number != -1) {
787 /* spill */
Ma Haijune0996ca2014-01-08 08:15:33 +0800788 put_ext4((uint64_t) ((uint64_t)delayed_start <<
Egbert Eich7b1b2552013-05-01 01:13:19 +0000789 log2blksz),
790 delayed_buf,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000791 (uint32_t) delayed_extent);
792 previous_block_number = -1;
793 }
794 memset(buf, 0, fs->blksz - skipfirst);
795 }
796 buf += fs->blksz - skipfirst;
797 }
798 if (previous_block_number != -1) {
799 /* spill */
Ma Haijune0996ca2014-01-08 08:15:33 +0800800 put_ext4((uint64_t) ((uint64_t)delayed_start << log2blksz),
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000801 delayed_buf, (uint32_t) delayed_extent);
802 previous_block_number = -1;
803 }
804
805 return len;
806}
807
808int ext4fs_write(const char *fname, unsigned char *buffer,
809 unsigned long sizebytes)
810{
811 int ret = 0;
812 struct ext2_inode *file_inode = NULL;
813 unsigned char *inode_buffer = NULL;
814 int parent_inodeno;
815 int inodeno;
816 time_t timestamp = 0;
817
818 uint64_t bytes_reqd_for_file;
819 unsigned int blks_reqd_for_file;
820 unsigned int blocks_remaining;
821 int existing_file_inodeno;
822 char *temp_ptr = NULL;
823 long int itable_blkno;
824 long int parent_itable_blkno;
825 long int blkoff;
826 struct ext2_sblock *sblock = &(ext4fs_root->sblock);
827 unsigned int inodes_per_block;
828 unsigned int ibmap_idx;
829 struct ext_filesystem *fs = get_fs();
830 ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256);
Jeroen Hofsteef978b002014-06-09 15:29:00 +0200831 memset(filename, 0x00, 256);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000832
Stefan Brüns7d936db2016-09-06 04:36:53 +0200833 g_parent_inode = zalloc(fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000834 if (!g_parent_inode)
835 goto fail;
836
837 if (ext4fs_init() != 0) {
838 printf("error in File System init\n");
839 return -1;
840 }
841 inodes_per_block = fs->blksz / fs->inodesz;
842 parent_inodeno = ext4fs_get_parent_inode_num(fname, filename, F_FILE);
843 if (parent_inodeno == -1)
844 goto fail;
845 if (ext4fs_iget(parent_inodeno, g_parent_inode))
846 goto fail;
Stefan Brüns57db8992016-09-06 04:36:45 +0200847 /* do not mess up a directory using hash trees */
848 if (le32_to_cpu(g_parent_inode->flags) & EXT4_INDEX_FL) {
849 printf("hash tree directory\n");
850 goto fail;
851 }
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000852 /* check if the filename is already present in root */
Stefan Brüns278f5d32016-09-06 04:36:41 +0200853 existing_file_inodeno = ext4fs_filename_unlink(filename);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000854 if (existing_file_inodeno != -1) {
855 ret = ext4fs_delete_file(existing_file_inodeno);
856 fs->first_pass_bbmap = 0;
857 fs->curr_blkno = 0;
858
859 fs->first_pass_ibmap = 0;
860 fs->curr_inode_no = 0;
861 if (ret)
862 goto fail;
863 }
864 /* calucalate how many blocks required */
865 bytes_reqd_for_file = sizebytes;
866 blks_reqd_for_file = lldiv(bytes_reqd_for_file, fs->blksz);
867 if (do_div(bytes_reqd_for_file, fs->blksz) != 0) {
868 blks_reqd_for_file++;
869 debug("total bytes for a file %u\n", blks_reqd_for_file);
870 }
871 blocks_remaining = blks_reqd_for_file;
872 /* test for available space in partition */
Michael Walle13179c22016-09-01 11:21:40 +0200873 if (le32_to_cpu(fs->sb->free_blocks) < blks_reqd_for_file) {
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000874 printf("Not enough space on partition !!!\n");
875 goto fail;
876 }
877
Stefan Brünsc1020682016-09-06 04:36:42 +0200878 inodeno = ext4fs_update_parent_dentry(filename, FILETYPE_REG);
879 if (inodeno == -1)
880 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000881 /* prepare file inode */
882 inode_buffer = zalloc(fs->inodesz);
883 if (!inode_buffer)
884 goto fail;
885 file_inode = (struct ext2_inode *)inode_buffer;
Michael Walle13179c22016-09-01 11:21:40 +0200886 file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU |
887 S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000888 /* ToDo: Update correct time */
Michael Walle13179c22016-09-01 11:21:40 +0200889 file_inode->mtime = cpu_to_le32(timestamp);
890 file_inode->atime = cpu_to_le32(timestamp);
891 file_inode->ctime = cpu_to_le32(timestamp);
892 file_inode->nlinks = cpu_to_le16(1);
893 file_inode->size = cpu_to_le32(sizebytes);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000894
895 /* Allocate data blocks */
896 ext4fs_allocate_blocks(file_inode, blocks_remaining,
897 &blks_reqd_for_file);
Michael Walle13179c22016-09-01 11:21:40 +0200898 file_inode->blockcnt = cpu_to_le32((blks_reqd_for_file * fs->blksz) >>
899 fs->dev_desc->log2blksz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000900
901 temp_ptr = zalloc(fs->blksz);
902 if (!temp_ptr)
903 goto fail;
Michael Walle13179c22016-09-01 11:21:40 +0200904 ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000905 inodeno--;
Michael Wallec07cdcb2016-08-29 10:46:44 +0200906 itable_blkno = le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) +
907 (inodeno % le32_to_cpu(sblock->inodes_per_group)) /
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000908 inodes_per_block;
909 blkoff = (inodeno % inodes_per_block) * fs->inodesz;
Frederic Leroye7ee0282013-06-26 18:11:25 +0200910 ext4fs_devread((lbaint_t)itable_blkno * fs->sect_perblk, 0, fs->blksz,
911 temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000912 if (ext4fs_log_journal(temp_ptr, itable_blkno))
913 goto fail;
914
915 memcpy(temp_ptr + blkoff, inode_buffer, fs->inodesz);
916 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
917 goto fail;
918 /* copy the file content into data blocks */
919 if (ext4fs_write_file(file_inode, 0, sizebytes, (char *)buffer) == -1) {
920 printf("Error in copying content\n");
Stefan Brüns74674ed2016-09-06 04:36:55 +0200921 /* FIXME: Deallocate data blocks */
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000922 goto fail;
923 }
Michael Walle13179c22016-09-01 11:21:40 +0200924 ibmap_idx = parent_inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000925 parent_inodeno--;
Michael Wallec07cdcb2016-08-29 10:46:44 +0200926 parent_itable_blkno = le32_to_cpu(fs->bgd[ibmap_idx].inode_table_id) +
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000927 (parent_inodeno %
Michael Wallec07cdcb2016-08-29 10:46:44 +0200928 le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000929 blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz;
930 if (parent_itable_blkno != itable_blkno) {
931 memset(temp_ptr, '\0', fs->blksz);
Frederic Leroye7ee0282013-06-26 18:11:25 +0200932 ext4fs_devread((lbaint_t)parent_itable_blkno * fs->sect_perblk,
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000933 0, fs->blksz, temp_ptr);
934 if (ext4fs_log_journal(temp_ptr, parent_itable_blkno))
935 goto fail;
936
Stefan Brüns7d936db2016-09-06 04:36:53 +0200937 memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000938 if (ext4fs_put_metadata(temp_ptr, parent_itable_blkno))
939 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000940 } else {
941 /*
942 * If parent and child fall in same inode table block
943 * both should be kept in 1 buffer
944 */
Stefan Brüns7d936db2016-09-06 04:36:53 +0200945 memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000946 gd_index--;
947 if (ext4fs_put_metadata(temp_ptr, itable_blkno))
948 goto fail;
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000949 }
950 ext4fs_update();
951 ext4fs_deinit();
952
953 fs->first_pass_bbmap = 0;
954 fs->curr_blkno = 0;
955 fs->first_pass_ibmap = 0;
956 fs->curr_inode_no = 0;
957 free(inode_buffer);
958 free(g_parent_inode);
Stefan Brüns338a5292016-09-06 04:36:51 +0200959 free(temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000960 g_parent_inode = NULL;
961
962 return 0;
963fail:
964 ext4fs_deinit();
965 free(inode_buffer);
966 free(g_parent_inode);
Stefan Brüns338a5292016-09-06 04:36:51 +0200967 free(temp_ptr);
Simon Glass6c8f3aa2012-12-26 09:53:28 +0000968 g_parent_inode = NULL;
969
970 return -1;
971}
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800972
973int ext4_write_file(const char *filename, void *buf, loff_t offset,
974 loff_t len, loff_t *actwrite)
975{
976 int ret;
977
978 if (offset != 0) {
979 printf("** Cannot support non-zero offset **\n");
980 return -1;
981 }
982
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800983 ret = ext4fs_write(filename, buf, len);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800984 if (ret) {
985 printf("** Error ext4fs_write() **\n");
986 goto fail;
987 }
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800988
Przemyslaw Marczak2b1d6c92015-02-17 15:31:52 +0100989 *actwrite = len;
990
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800991 return 0;
992
993fail:
Przemyslaw Marczak2b1d6c92015-02-17 15:31:52 +0100994 *actwrite = 0;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800995
996 return -1;
997}