Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 2 | /* |
| 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 24 | #include <blk.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 25 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 26 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 27 | #include <memalign.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 28 | #include <part.h> |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 29 | #include <linux/stat.h> |
| 30 | #include <div64.h> |
| 31 | #include "ext4_common.h" |
| 32 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 33 | static 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 | |
| 38 | static 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üns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 43 | static inline void ext4fs_bg_free_inodes_inc |
| 44 | (struct ext2_block_group *bg, const struct ext_filesystem *fs) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 45 | { |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 46 | 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 56 | static inline void ext4fs_bg_free_blocks_inc |
| 57 | (struct ext2_block_group *bg, const struct ext_filesystem *fs) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 58 | { |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 59 | 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 69 | static void ext4fs_update(void) |
| 70 | { |
| 71 | short i; |
| 72 | ext4fs_update_journal(); |
| 73 | struct ext_filesystem *fs = get_fs(); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 74 | struct ext2_block_group *bgd = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 75 | |
| 76 | /* update super block */ |
| 77 | put_ext4((uint64_t)(SUPERBLOCK_SIZE), |
| 78 | (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE); |
| 79 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 80 | /* update block bitmaps */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 81 | for (i = 0; i < fs->no_blkgrp; i++) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 82 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 86 | fs->blk_bmaps[i], fs->blksz); |
| 87 | } |
| 88 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 89 | /* update inode bitmaps */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 90 | for (i = 0; i < fs->no_blkgrp; i++) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 91 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 94 | fs->inode_bmaps[i], fs->blksz); |
| 95 | } |
| 96 | |
| 97 | /* update the block group descriptor table */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 98 | put_ext4((uint64_t)((uint64_t)fs->gdtable_blkno * (uint64_t)fs->blksz), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 99 | (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 | |
| 108 | int ext4fs_get_bgdtable(void) |
| 109 | { |
| 110 | int status; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 111 | struct ext_filesystem *fs = get_fs(); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 112 | int gdsize_total = ROUND(fs->no_blkgrp * fs->gdsize, fs->blksz); |
| 113 | fs->no_blk_pergdt = gdsize_total / fs->blksz; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 114 | |
| 115 | /* allocate memory for gdtable */ |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 116 | fs->gdtable = zalloc(gdsize_total); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 117 | if (!fs->gdtable) |
| 118 | return -ENOMEM; |
| 119 | /* read the group descriptor table */ |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 120 | status = ext4fs_devread((lbaint_t)fs->gdtable_blkno * fs->sect_perblk, |
| 121 | 0, fs->blksz * fs->no_blk_pergdt, fs->gdtable); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 122 | 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; |
| 131 | fail: |
| 132 | free(fs->gdtable); |
| 133 | fs->gdtable = NULL; |
| 134 | |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | static 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 142 | uint32_t blknr; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 143 | int remainder; |
| 144 | int bg_idx; |
| 145 | int status; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 146 | uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 147 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 153 | |
| 154 | /* deleting the single indirect block associated with inode */ |
| 155 | if (inode->b.blocks.indir_block != 0) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 156 | blknr = le32_to_cpu(inode->b.blocks.indir_block); |
| 157 | debug("SIPB releasing %u\n", blknr); |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 158 | bg_idx = blknr / blk_per_grp; |
| 159 | if (fs->blksz == 1024) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 160 | 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 165 | /* get block group descriptor table */ |
| 166 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 167 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 168 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 169 | /* journal backup */ |
| 170 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 171 | uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 172 | status = ext4fs_devread( |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 173 | b_bitmap_blk * fs->sect_perblk, |
| 174 | 0, fs->blksz, journal_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 175 | if (status == 0) |
| 176 | goto fail; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 177 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 178 | goto fail; |
| 179 | prev_bg_bmap_idx = bg_idx; |
| 180 | } |
| 181 | } |
| 182 | fail: |
| 183 | free(journal_buffer); |
| 184 | } |
| 185 | |
| 186 | static 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 191 | uint32_t blknr; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 192 | int remainder; |
| 193 | int bg_idx; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 194 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 197 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 204 | |
| 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 211 | dib_start_addr = di_buffer; |
| 212 | blknr = le32_to_cpu(inode->b.blocks.double_indir_block); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 213 | status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, |
| 214 | fs->blksz, (char *)di_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 215 | 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 220 | bg_idx = le32_to_cpu(*di_buffer) / blk_per_grp; |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 221 | if (fs->blksz == 1024) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 222 | remainder = le32_to_cpu(*di_buffer) % blk_per_grp; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 223 | if (!remainder) |
| 224 | bg_idx--; |
| 225 | } |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 226 | /* get block group descriptor table */ |
| 227 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 228 | ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 229 | fs->blk_bmaps[bg_idx], bg_idx); |
| 230 | di_buffer++; |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 231 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 232 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 233 | /* journal backup */ |
| 234 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 235 | uint64_t b_bitmap_blk = |
| 236 | ext4fs_bg_get_block_id(bgd, fs); |
| 237 | status = ext4fs_devread(b_bitmap_blk |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 238 | * 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 245 | b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 246 | goto fail; |
| 247 | prev_bg_bmap_idx = bg_idx; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* removing the parent double indirect block */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 252 | blknr = le32_to_cpu(inode->b.blocks.double_indir_block); |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 253 | bg_idx = blknr / blk_per_grp; |
| 254 | if (fs->blksz == 1024) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 255 | remainder = blknr % blk_per_grp; |
| 256 | if (!remainder) |
| 257 | bg_idx--; |
| 258 | } |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 259 | /* get block group descriptor table */ |
| 260 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 261 | ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 262 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 263 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 264 | /* journal backup */ |
| 265 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 266 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 269 | if (status == 0) |
| 270 | goto fail; |
| 271 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 272 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 273 | goto fail; |
| 274 | prev_bg_bmap_idx = bg_idx; |
| 275 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 276 | debug("DIPB releasing %d\n", blknr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 277 | } |
| 278 | fail: |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 279 | free(dib_start_addr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 280 | free(journal_buffer); |
| 281 | } |
| 282 | |
| 283 | static 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 288 | uint32_t blknr; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 289 | int remainder; |
| 290 | int bg_idx; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 291 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 296 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 303 | |
| 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 310 | tib_start_addr = tigp_buffer; |
| 311 | blknr = le32_to_cpu(inode->b.blocks.triple_indir_block); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 312 | status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, |
| 313 | fs->blksz, (char *)tigp_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 314 | 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 322 | tipb_start_addr = tip_buffer; |
| 323 | status = ext4fs_devread((lbaint_t)le32_to_cpu(*tigp_buffer) * |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 324 | fs->sect_perblk, 0, fs->blksz, |
| 325 | (char *)tip_buffer); |
| 326 | for (j = 0; j < fs->blksz / sizeof(int); j++) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 327 | if (le32_to_cpu(*tip_buffer) == 0) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 328 | break; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 329 | bg_idx = le32_to_cpu(*tip_buffer) / blk_per_grp; |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 330 | if (fs->blksz == 1024) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 331 | remainder = le32_to_cpu(*tip_buffer) % blk_per_grp; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 332 | if (!remainder) |
| 333 | bg_idx--; |
| 334 | } |
| 335 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 336 | ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 337 | fs->blk_bmaps[bg_idx], |
| 338 | bg_idx); |
| 339 | |
| 340 | tip_buffer++; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 341 | /* get block group descriptor table */ |
| 342 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 343 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 344 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 345 | /* journal backup */ |
| 346 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 347 | uint64_t b_bitmap_blk = |
| 348 | ext4fs_bg_get_block_id(bgd, fs); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 349 | status = |
| 350 | ext4fs_devread( |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 351 | b_bitmap_blk * |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 352 | 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 359 | b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 360 | 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 371 | bg_idx = le32_to_cpu(*tigp_buffer) / blk_per_grp; |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 372 | if (fs->blksz == 1024) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 373 | remainder = le32_to_cpu(*tigp_buffer) % blk_per_grp; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 374 | if (!remainder) |
| 375 | bg_idx--; |
| 376 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 377 | ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 378 | fs->blk_bmaps[bg_idx], bg_idx); |
| 379 | |
| 380 | tigp_buffer++; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 381 | /* get block group descriptor table */ |
| 382 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 383 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 384 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 385 | /* journal backup */ |
| 386 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 387 | uint64_t b_bitmap_blk = |
| 388 | ext4fs_bg_get_block_id(bgd, fs); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 389 | memset(journal_buffer, '\0', fs->blksz); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 390 | status = ext4fs_devread(b_bitmap_blk * |
| 391 | fs->sect_perblk, 0, |
| 392 | fs->blksz, |
| 393 | journal_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 394 | if (status == 0) |
| 395 | goto fail; |
| 396 | |
| 397 | if (ext4fs_log_journal(journal_buffer, |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 398 | b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 399 | goto fail; |
| 400 | prev_bg_bmap_idx = bg_idx; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* removing the grand parent triple indirect block */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 405 | blknr = le32_to_cpu(inode->b.blocks.triple_indir_block); |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 406 | bg_idx = blknr / blk_per_grp; |
| 407 | if (fs->blksz == 1024) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 408 | 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 413 | /* get block group descriptor table */ |
| 414 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 415 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 416 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 417 | /* journal backup */ |
| 418 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 419 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 422 | if (status == 0) |
| 423 | goto fail; |
| 424 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 425 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 426 | goto fail; |
| 427 | prev_bg_bmap_idx = bg_idx; |
| 428 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 429 | debug("tigp buffer itself releasing %d\n", blknr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 430 | } |
| 431 | fail: |
| 432 | free(tib_start_addr); |
| 433 | free(tipb_start_addr); |
| 434 | free(journal_buffer); |
| 435 | } |
| 436 | |
| 437 | static 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 448 | uint32_t no_blocks; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 449 | |
| 450 | static int prev_bg_bmap_idx = -1; |
| 451 | unsigned int inodes_per_block; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 452 | uint32_t blkno; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 453 | unsigned int blkoff; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 454 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 456 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 462 | 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 467 | no_blocks = le32_to_cpu(inode.size) / fs->blksz; |
| 468 | if (le32_to_cpu(inode.size) % fs->blksz) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 469 | no_blocks++; |
| 470 | |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 471 | /* |
| 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 GUILLEVIC | d4620ae | 2023-03-17 13:15:12 +0100 | [diff] [blame] | 475 | if (S_ISLNK(le16_to_cpu(inode.mode)) && |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 476 | le32_to_cpu(inode.size) <= sizeof(inode.b.symlink)) { |
| 477 | no_blocks = 0; |
| 478 | } |
| 479 | |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 480 | if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) { |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 481 | /* 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 486 | } else { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 487 | delete_single_indirect_block(&inode); |
| 488 | delete_double_indirect_block(&inode); |
| 489 | delete_triple_indirect_block(&inode); |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 490 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 491 | |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 492 | /* release data blocks */ |
| 493 | for (i = 0; i < no_blocks; i++) { |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 494 | blknr = read_allocated_block(&inode, i, NULL); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 495 | if (blknr == 0) |
| 496 | continue; |
| 497 | if (blknr < 0) |
| 498 | goto fail; |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 499 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 508 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 509 | /* get block group descriptor table */ |
| 510 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 511 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 512 | ext4fs_sb_free_blocks_inc(fs->sb); |
| 513 | /* journal backup */ |
| 514 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 515 | uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs); |
| 516 | status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk, |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 517 | 0, fs->blksz, |
| 518 | journal_buffer); |
| 519 | if (status == 0) |
| 520 | goto fail; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 521 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 522 | goto fail; |
| 523 | prev_bg_bmap_idx = bg_idx; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 527 | /* release inode */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 528 | /* 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 534 | /* 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 537 | (inodeno % inode_per_grp) / inodes_per_block; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 538 | |
| 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 Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 547 | status = ext4fs_devread((lbaint_t)blkno * fs->sect_perblk, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 548 | 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üns | 7d936db | 2016-09-06 04:36:53 +0200 | [diff] [blame] | 557 | memset(inode_buffer, '\0', fs->inodesz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 558 | |
| 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üns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 566 | ext4fs_bg_free_inodes_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 567 | ext4fs_sb_free_inodes_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 568 | /* journal backup */ |
| 569 | memset(journal_buffer, '\0', fs->blksz); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 570 | status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) * |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 571 | fs->sect_perblk, 0, fs->blksz, journal_buffer); |
| 572 | if (status == 0) |
| 573 | goto fail; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 574 | if (ext4fs_log_journal(journal_buffer, ext4fs_bg_get_inode_id(bgd, fs))) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 575 | goto fail; |
| 576 | |
| 577 | ext4fs_update(); |
| 578 | ext4fs_deinit(); |
Łukasz Majewski | 900db5d | 2014-05-06 09:36:05 +0200 | [diff] [blame] | 579 | ext4fs_reinit_global(); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 580 | |
| 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; |
| 590 | fail: |
| 591 | free(start_block_address); |
| 592 | free(journal_buffer); |
| 593 | |
| 594 | return -1; |
| 595 | } |
| 596 | |
| 597 | int ext4fs_init(void) |
| 598 | { |
| 599 | short status; |
| 600 | int i; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 601 | uint32_t real_free_blocks = 0; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 602 | struct ext_filesystem *fs = get_fs(); |
| 603 | |
| 604 | /* populate fs */ |
| 605 | fs->blksz = EXT2_BLOCK_SIZE(ext4fs_root); |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 606 | fs->sect_perblk = fs->blksz >> fs->dev_desc->log2blksz; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 607 | |
| 608 | /* get the superblock */ |
| 609 | fs->sb = zalloc(SUPERBLOCK_SIZE); |
| 610 | if (!fs->sb) |
| 611 | return -ENOMEM; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 612 | if (!ext4_read_superblock((char *)fs->sb)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 613 | 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 621 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 624 | |
| 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 631 | |
| 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 643 | struct ext2_block_group *bgd = |
| 644 | ext4fs_get_group_descriptor(fs, i); |
| 645 | status = ext4fs_devread(ext4fs_bg_get_block_id(bgd, fs) * |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 646 | fs->sect_perblk, 0, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 647 | 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 663 | struct ext2_block_group *bgd = |
| 664 | ext4fs_get_group_descriptor(fs, i); |
| 665 | status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) * |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 666 | fs->sect_perblk, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 667 | 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 679 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 687 | |
| 688 | return 0; |
| 689 | fail: |
| 690 | ext4fs_deinit(); |
| 691 | |
| 692 | return -1; |
| 693 | } |
| 694 | |
| 695 | void ext4fs_deinit(void) |
| 696 | { |
| 697 | int i; |
| 698 | struct ext2_inode inode_journal; |
| 699 | struct journal_superblock_t *jsb; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 700 | uint32_t blknr; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 701 | struct ext_filesystem *fs = get_fs(); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 702 | uint32_t new_feature_incompat; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 703 | |
| 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 Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 710 | EXT2_JOURNAL_SUPERBLOCK, NULL); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 711 | ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 712 | temp_buff); |
| 713 | jsb = (struct journal_superblock_t *)temp_buff; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 714 | jsb->s_start = 0; |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 715 | put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 716 | (struct journal_superblock_t *)temp_buff, fs->blksz); |
| 717 | free(temp_buff); |
| 718 | } |
| 719 | ext4fs_free_journal(); |
| 720 | |
| 721 | /* get the superblock */ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 722 | ext4_read_superblock((char *)fs->sb); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 723 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 726 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 752 | /* |
| 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üns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 762 | /* |
| 763 | * Write data to filesystem blocks. Uses same optimization for |
| 764 | * contigous sectors as ext4fs_read_file |
| 765 | */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 766 | static int ext4fs_write_file(struct ext2_inode *file_inode, |
Jean-Jacques Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 767 | int pos, unsigned int len, const char *buf) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 768 | { |
| 769 | int i; |
| 770 | int blockcnt; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 771 | uint32_t filesize = le32_to_cpu(file_inode->size); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 772 | struct ext_filesystem *fs = get_fs(); |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 773 | int log2blksz = fs->dev_desc->log2blksz; |
| 774 | int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 775 | int previous_block_number = -1; |
| 776 | int delayed_start = 0; |
| 777 | int delayed_extent = 0; |
| 778 | int delayed_next = 0; |
Jean-Jacques Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 779 | const char *delayed_buf = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 780 | |
| 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 Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 791 | blknr = read_allocated_block(file_inode, i, NULL); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 792 | if (blknr <= 0) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 793 | return -1; |
| 794 | |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 795 | blknr = blknr << log2_fs_blocksize; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 796 | |
| 797 | if (blknr) { |
| 798 | if (previous_block_number != -1) { |
| 799 | if (delayed_next == blknr) { |
| 800 | delayed_extent += blockend; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 801 | delayed_next += blockend >> log2blksz; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 802 | } else { /* spill */ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 803 | put_ext4((uint64_t) |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 804 | ((uint64_t)delayed_start << log2blksz), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 805 | 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 Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 812 | (blockend >> log2blksz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 813 | } |
| 814 | } else { |
| 815 | previous_block_number = blknr; |
| 816 | delayed_start = blknr; |
| 817 | delayed_extent = blockend; |
| 818 | delayed_buf = buf; |
| 819 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 820 | (blockend >> log2blksz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 821 | } |
| 822 | } else { |
| 823 | if (previous_block_number != -1) { |
| 824 | /* spill */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 825 | put_ext4((uint64_t) ((uint64_t)delayed_start << |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 826 | log2blksz), |
| 827 | delayed_buf, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 828 | (uint32_t) delayed_extent); |
| 829 | previous_block_number = -1; |
| 830 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 831 | } |
| 832 | buf += fs->blksz - skipfirst; |
| 833 | } |
| 834 | if (previous_block_number != -1) { |
| 835 | /* spill */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 836 | put_ext4((uint64_t) ((uint64_t)delayed_start << log2blksz), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 837 | delayed_buf, (uint32_t) delayed_extent); |
| 838 | previous_block_number = -1; |
| 839 | } |
| 840 | |
| 841 | return len; |
| 842 | } |
| 843 | |
Jean-Jacques Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 844 | int ext4fs_write(const char *fname, const char *buffer, |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 845 | unsigned long sizebytes, int type) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 846 | { |
| 847 | int ret = 0; |
| 848 | struct ext2_inode *file_inode = NULL; |
developer | 3a7e1a4 | 2024-03-19 17:20:40 +0800 | [diff] [blame] | 849 | struct ext2_inode *existing_file_inode = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 850 | 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üns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 866 | struct ext2_block_group *bgd = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 867 | struct ext_filesystem *fs = get_fs(); |
| 868 | ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256); |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 869 | bool store_link_in_inode = false; |
Jeroen Hofstee | f978b00 | 2014-06-09 15:29:00 +0200 | [diff] [blame] | 870 | memset(filename, 0x00, 256); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 871 | |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 872 | if (type != FILETYPE_REG && type != FILETYPE_SYMLINK) |
| 873 | return -1; |
| 874 | |
Stefan Brüns | 7d936db | 2016-09-06 04:36:53 +0200 | [diff] [blame] | 875 | g_parent_inode = zalloc(fs->inodesz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 876 | 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 Szymanski | f7d4968 | 2019-03-22 09:33:52 +0100 | [diff] [blame] | 883 | |
| 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 889 | 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üns | 57db899 | 2016-09-06 04:36:45 +0200 | [diff] [blame] | 895 | /* 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 900 | /* check if the filename is already present in root */ |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 901 | existing_file_inodeno = ext4fs_filename_unlink(filename); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 902 | if (existing_file_inodeno != -1) { |
developer | 3a7e1a4 | 2024-03-19 17:20:40 +0800 | [diff] [blame] | 903 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 912 | 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 Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 921 | |
| 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 931 | 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 Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 938 | if (le32_to_cpu(fs->sb->free_blocks) < blks_reqd_for_file) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 939 | printf("Not enough space on partition !!!\n"); |
| 940 | goto fail; |
| 941 | } |
| 942 | |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 943 | inodeno = ext4fs_update_parent_dentry(filename, type); |
Stefan Brüns | c102068 | 2016-09-06 04:36:42 +0200 | [diff] [blame] | 944 | if (inodeno == -1) |
| 945 | goto fail; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 946 | /* 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 Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 951 | 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 { |
developer | 3a7e1a4 | 2024-03-19 17:20:40 +0800 | [diff] [blame] | 960 | 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 Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 966 | } |
developer | 3a7e1a4 | 2024-03-19 17:20:40 +0800 | [diff] [blame] | 967 | if (existing_file_inode) |
| 968 | free(existing_file_inode); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 969 | /* ToDo: Update correct time */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 970 | 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 Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 974 | |
| 975 | /* Allocate data blocks */ |
| 976 | ext4fs_allocate_blocks(file_inode, blocks_remaining, |
| 977 | &blks_reqd_for_file); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 978 | file_inode->blockcnt = cpu_to_le32((blks_reqd_for_file * fs->blksz) >> |
Marek Szyprowski | 07a39ab | 2019-06-21 15:32:51 +0200 | [diff] [blame] | 979 | LOG2_SECTOR_SIZE); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 980 | |
| 981 | temp_ptr = zalloc(fs->blksz); |
| 982 | if (!temp_ptr) |
| 983 | goto fail; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 984 | ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 985 | inodeno--; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 986 | bgd = ext4fs_get_group_descriptor(fs, ibmap_idx); |
| 987 | itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) + |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 988 | (inodeno % le32_to_cpu(sblock->inodes_per_group)) / |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 989 | inodes_per_block; |
| 990 | blkoff = (inodeno % inodes_per_block) * fs->inodesz; |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 991 | ext4fs_devread((lbaint_t)itable_blkno * fs->sect_perblk, 0, fs->blksz, |
| 992 | temp_ptr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 993 | 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 Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 1000 | if (ext4fs_write_file(file_inode, 0, sizebytes, buffer) == -1) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1001 | printf("Error in copying content\n"); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 1002 | /* FIXME: Deallocate data blocks */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1003 | goto fail; |
| 1004 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1005 | ibmap_idx = parent_inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1006 | parent_inodeno--; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1007 | bgd = ext4fs_get_group_descriptor(fs, ibmap_idx); |
| 1008 | parent_itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) + |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1009 | (parent_inodeno % |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1010 | le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1011 | blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz; |
| 1012 | if (parent_itable_blkno != itable_blkno) { |
| 1013 | memset(temp_ptr, '\0', fs->blksz); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 1014 | ext4fs_devread((lbaint_t)parent_itable_blkno * fs->sect_perblk, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1015 | 0, fs->blksz, temp_ptr); |
| 1016 | if (ext4fs_log_journal(temp_ptr, parent_itable_blkno)) |
| 1017 | goto fail; |
| 1018 | |
Stefan Brüns | 7d936db | 2016-09-06 04:36:53 +0200 | [diff] [blame] | 1019 | memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1020 | if (ext4fs_put_metadata(temp_ptr, parent_itable_blkno)) |
| 1021 | goto fail; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1022 | } else { |
| 1023 | /* |
| 1024 | * If parent and child fall in same inode table block |
| 1025 | * both should be kept in 1 buffer |
| 1026 | */ |
Stefan Brüns | 7d936db | 2016-09-06 04:36:53 +0200 | [diff] [blame] | 1027 | memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1028 | gd_index--; |
| 1029 | if (ext4fs_put_metadata(temp_ptr, itable_blkno)) |
| 1030 | goto fail; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1031 | } |
| 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üns | 338a529 | 2016-09-06 04:36:51 +0200 | [diff] [blame] | 1041 | free(temp_ptr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1042 | g_parent_inode = NULL; |
| 1043 | |
| 1044 | return 0; |
| 1045 | fail: |
| 1046 | ext4fs_deinit(); |
| 1047 | free(inode_buffer); |
| 1048 | free(g_parent_inode); |
Stefan Brüns | 338a529 | 2016-09-06 04:36:51 +0200 | [diff] [blame] | 1049 | free(temp_ptr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1050 | g_parent_inode = NULL; |
| 1051 | |
| 1052 | return -1; |
| 1053 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1054 | |
| 1055 | int 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 Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 1065 | ret = ext4fs_write(filename, buf, len, FILETYPE_REG); |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1066 | if (ret) { |
| 1067 | printf("** Error ext4fs_write() **\n"); |
| 1068 | goto fail; |
| 1069 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1070 | |
Przemyslaw Marczak | 2b1d6c9 | 2015-02-17 15:31:52 +0100 | [diff] [blame] | 1071 | *actwrite = len; |
| 1072 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1073 | return 0; |
| 1074 | |
| 1075 | fail: |
Przemyslaw Marczak | 2b1d6c9 | 2015-02-17 15:31:52 +0100 | [diff] [blame] | 1076 | *actwrite = 0; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1077 | |
| 1078 | return -1; |
| 1079 | } |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 1080 | |
| 1081 | int ext4fs_create_link(const char *target, const char *fname) |
| 1082 | { |
| 1083 | return ext4fs_write(fname, target, strlen(target), FILETYPE_SYMLINK); |
| 1084 | } |