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 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 23 | #include <blk.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 24 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 25 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 26 | #include <memalign.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 27 | #include <part.h> |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 28 | #include <linux/stat.h> |
| 29 | #include <div64.h> |
| 30 | #include "ext4_common.h" |
| 31 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 32 | static inline void ext4fs_sb_free_inodes_inc(struct ext2_sblock *sb) |
| 33 | { |
| 34 | sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) + 1); |
| 35 | } |
| 36 | |
| 37 | static inline void ext4fs_sb_free_blocks_inc(struct ext2_sblock *sb) |
| 38 | { |
| 39 | sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) + 1); |
| 40 | } |
| 41 | |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 42 | static inline void ext4fs_bg_free_inodes_inc |
| 43 | (struct ext2_block_group *bg, const struct ext_filesystem *fs) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 44 | { |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 45 | uint32_t free_inodes = le16_to_cpu(bg->free_inodes); |
| 46 | if (fs->gdsize == 64) |
| 47 | free_inodes += le16_to_cpu(bg->free_inodes_high) << 16; |
| 48 | free_inodes++; |
| 49 | |
| 50 | bg->free_inodes = cpu_to_le16(free_inodes & 0xffff); |
| 51 | if (fs->gdsize == 64) |
| 52 | bg->free_inodes_high = cpu_to_le16(free_inodes >> 16); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 55 | static inline void ext4fs_bg_free_blocks_inc |
| 56 | (struct ext2_block_group *bg, const struct ext_filesystem *fs) |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 57 | { |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 58 | uint32_t free_blocks = le16_to_cpu(bg->free_blocks); |
| 59 | if (fs->gdsize == 64) |
| 60 | free_blocks += le16_to_cpu(bg->free_blocks_high) << 16; |
| 61 | free_blocks++; |
| 62 | |
| 63 | bg->free_blocks = cpu_to_le16(free_blocks & 0xffff); |
| 64 | if (fs->gdsize == 64) |
| 65 | bg->free_blocks_high = cpu_to_le16(free_blocks >> 16); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 68 | static void ext4fs_update(void) |
| 69 | { |
| 70 | short i; |
| 71 | ext4fs_update_journal(); |
| 72 | struct ext_filesystem *fs = get_fs(); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 73 | struct ext2_block_group *bgd = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 74 | |
| 75 | /* update super block */ |
| 76 | put_ext4((uint64_t)(SUPERBLOCK_SIZE), |
| 77 | (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE); |
| 78 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 79 | /* update block bitmaps */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 80 | for (i = 0; i < fs->no_blkgrp; i++) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 81 | bgd = ext4fs_get_group_descriptor(fs, i); |
| 82 | bgd->bg_checksum = cpu_to_le16(ext4fs_checksum_update(i)); |
| 83 | uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs); |
| 84 | put_ext4(b_bitmap_blk * fs->blksz, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 85 | fs->blk_bmaps[i], fs->blksz); |
| 86 | } |
| 87 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 88 | /* update inode bitmaps */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 89 | for (i = 0; i < fs->no_blkgrp; i++) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 90 | bgd = ext4fs_get_group_descriptor(fs, i); |
| 91 | uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs); |
| 92 | put_ext4(i_bitmap_blk * fs->blksz, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 93 | fs->inode_bmaps[i], fs->blksz); |
| 94 | } |
| 95 | |
| 96 | /* update the block group descriptor table */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 97 | 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] | 98 | (struct ext2_block_group *)fs->gdtable, |
| 99 | (fs->blksz * fs->no_blk_pergdt)); |
| 100 | |
| 101 | ext4fs_dump_metadata(); |
| 102 | |
| 103 | gindex = 0; |
| 104 | gd_index = 0; |
| 105 | } |
| 106 | |
| 107 | int ext4fs_get_bgdtable(void) |
| 108 | { |
| 109 | int status; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 110 | struct ext_filesystem *fs = get_fs(); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 111 | int gdsize_total = ROUND(fs->no_blkgrp * fs->gdsize, fs->blksz); |
| 112 | fs->no_blk_pergdt = gdsize_total / fs->blksz; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 113 | |
| 114 | /* allocate memory for gdtable */ |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 115 | fs->gdtable = zalloc(gdsize_total); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 116 | if (!fs->gdtable) |
| 117 | return -ENOMEM; |
| 118 | /* read the group descriptor table */ |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 119 | status = ext4fs_devread((lbaint_t)fs->gdtable_blkno * fs->sect_perblk, |
| 120 | 0, fs->blksz * fs->no_blk_pergdt, fs->gdtable); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 121 | if (status == 0) |
| 122 | goto fail; |
| 123 | |
| 124 | if (ext4fs_log_gdt(fs->gdtable)) { |
| 125 | printf("Error in ext4fs_log_gdt\n"); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | fail: |
| 131 | free(fs->gdtable); |
| 132 | fs->gdtable = NULL; |
| 133 | |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | static void delete_single_indirect_block(struct ext2_inode *inode) |
| 138 | { |
| 139 | struct ext2_block_group *bgd = NULL; |
| 140 | static int prev_bg_bmap_idx = -1; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 141 | uint32_t blknr; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 142 | int remainder; |
| 143 | int bg_idx; |
| 144 | int status; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 145 | 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] | 146 | struct ext_filesystem *fs = get_fs(); |
| 147 | char *journal_buffer = zalloc(fs->blksz); |
| 148 | if (!journal_buffer) { |
| 149 | printf("No memory\n"); |
| 150 | return; |
| 151 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 152 | |
| 153 | /* deleting the single indirect block associated with inode */ |
| 154 | if (inode->b.blocks.indir_block != 0) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 155 | blknr = le32_to_cpu(inode->b.blocks.indir_block); |
| 156 | debug("SIPB releasing %u\n", blknr); |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 157 | bg_idx = blknr / blk_per_grp; |
| 158 | if (fs->blksz == 1024) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 159 | remainder = blknr % blk_per_grp; |
| 160 | if (!remainder) |
| 161 | bg_idx--; |
| 162 | } |
| 163 | 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] | 164 | /* get block group descriptor table */ |
| 165 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 166 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 167 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 168 | /* journal backup */ |
| 169 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 170 | uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 171 | status = ext4fs_devread( |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 172 | b_bitmap_blk * fs->sect_perblk, |
| 173 | 0, fs->blksz, journal_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 174 | if (status == 0) |
| 175 | goto fail; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 176 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 177 | goto fail; |
| 178 | prev_bg_bmap_idx = bg_idx; |
| 179 | } |
| 180 | } |
| 181 | fail: |
| 182 | free(journal_buffer); |
| 183 | } |
| 184 | |
| 185 | static void delete_double_indirect_block(struct ext2_inode *inode) |
| 186 | { |
| 187 | int i; |
| 188 | short status; |
| 189 | static int prev_bg_bmap_idx = -1; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 190 | uint32_t blknr; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 191 | int remainder; |
| 192 | int bg_idx; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 193 | uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group); |
| 194 | __le32 *di_buffer = NULL; |
| 195 | void *dib_start_addr = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 196 | struct ext2_block_group *bgd = NULL; |
| 197 | struct ext_filesystem *fs = get_fs(); |
| 198 | char *journal_buffer = zalloc(fs->blksz); |
| 199 | if (!journal_buffer) { |
| 200 | printf("No memory\n"); |
| 201 | return; |
| 202 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 203 | |
| 204 | if (inode->b.blocks.double_indir_block != 0) { |
| 205 | di_buffer = zalloc(fs->blksz); |
| 206 | if (!di_buffer) { |
| 207 | printf("No memory\n"); |
| 208 | return; |
| 209 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 210 | dib_start_addr = di_buffer; |
| 211 | blknr = le32_to_cpu(inode->b.blocks.double_indir_block); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 212 | status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, |
| 213 | fs->blksz, (char *)di_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 214 | for (i = 0; i < fs->blksz / sizeof(int); i++) { |
| 215 | if (*di_buffer == 0) |
| 216 | break; |
| 217 | |
| 218 | debug("DICB releasing %u\n", *di_buffer); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 219 | bg_idx = le32_to_cpu(*di_buffer) / blk_per_grp; |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 220 | if (fs->blksz == 1024) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 221 | remainder = le32_to_cpu(*di_buffer) % blk_per_grp; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 222 | if (!remainder) |
| 223 | bg_idx--; |
| 224 | } |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 225 | /* get block group descriptor table */ |
| 226 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 227 | ext4fs_reset_block_bmap(le32_to_cpu(*di_buffer), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 228 | fs->blk_bmaps[bg_idx], bg_idx); |
| 229 | di_buffer++; |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 230 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 231 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 232 | /* journal backup */ |
| 233 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 234 | uint64_t b_bitmap_blk = |
| 235 | ext4fs_bg_get_block_id(bgd, fs); |
| 236 | status = ext4fs_devread(b_bitmap_blk |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 237 | * fs->sect_perblk, 0, |
| 238 | fs->blksz, |
| 239 | journal_buffer); |
| 240 | if (status == 0) |
| 241 | goto fail; |
| 242 | |
| 243 | if (ext4fs_log_journal(journal_buffer, |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 244 | b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 245 | goto fail; |
| 246 | prev_bg_bmap_idx = bg_idx; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* removing the parent double indirect block */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 251 | blknr = le32_to_cpu(inode->b.blocks.double_indir_block); |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 252 | bg_idx = blknr / blk_per_grp; |
| 253 | if (fs->blksz == 1024) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 254 | remainder = blknr % blk_per_grp; |
| 255 | if (!remainder) |
| 256 | bg_idx--; |
| 257 | } |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 258 | /* get block group descriptor table */ |
| 259 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 260 | 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] | 261 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 262 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 263 | /* journal backup */ |
| 264 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 265 | uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs); |
| 266 | status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk, |
| 267 | 0, fs->blksz, journal_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 268 | if (status == 0) |
| 269 | goto fail; |
| 270 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 271 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 272 | goto fail; |
| 273 | prev_bg_bmap_idx = bg_idx; |
| 274 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 275 | debug("DIPB releasing %d\n", blknr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 276 | } |
| 277 | fail: |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 278 | free(dib_start_addr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 279 | free(journal_buffer); |
| 280 | } |
| 281 | |
| 282 | static void delete_triple_indirect_block(struct ext2_inode *inode) |
| 283 | { |
| 284 | int i, j; |
| 285 | short status; |
| 286 | static int prev_bg_bmap_idx = -1; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 287 | uint32_t blknr; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 288 | int remainder; |
| 289 | int bg_idx; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 290 | uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group); |
| 291 | __le32 *tigp_buffer = NULL; |
| 292 | void *tib_start_addr = NULL; |
| 293 | __le32 *tip_buffer = NULL; |
| 294 | void *tipb_start_addr = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 295 | struct ext2_block_group *bgd = NULL; |
| 296 | struct ext_filesystem *fs = get_fs(); |
| 297 | char *journal_buffer = zalloc(fs->blksz); |
| 298 | if (!journal_buffer) { |
| 299 | printf("No memory\n"); |
| 300 | return; |
| 301 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 302 | |
| 303 | if (inode->b.blocks.triple_indir_block != 0) { |
| 304 | tigp_buffer = zalloc(fs->blksz); |
| 305 | if (!tigp_buffer) { |
| 306 | printf("No memory\n"); |
| 307 | return; |
| 308 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 309 | tib_start_addr = tigp_buffer; |
| 310 | blknr = le32_to_cpu(inode->b.blocks.triple_indir_block); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 311 | status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, |
| 312 | fs->blksz, (char *)tigp_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 313 | for (i = 0; i < fs->blksz / sizeof(int); i++) { |
| 314 | if (*tigp_buffer == 0) |
| 315 | break; |
| 316 | debug("tigp buffer releasing %u\n", *tigp_buffer); |
| 317 | |
| 318 | tip_buffer = zalloc(fs->blksz); |
| 319 | if (!tip_buffer) |
| 320 | goto fail; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 321 | tipb_start_addr = tip_buffer; |
| 322 | status = ext4fs_devread((lbaint_t)le32_to_cpu(*tigp_buffer) * |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 323 | fs->sect_perblk, 0, fs->blksz, |
| 324 | (char *)tip_buffer); |
| 325 | for (j = 0; j < fs->blksz / sizeof(int); j++) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 326 | if (le32_to_cpu(*tip_buffer) == 0) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 327 | break; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 328 | bg_idx = le32_to_cpu(*tip_buffer) / blk_per_grp; |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 329 | if (fs->blksz == 1024) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 330 | remainder = le32_to_cpu(*tip_buffer) % blk_per_grp; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 331 | if (!remainder) |
| 332 | bg_idx--; |
| 333 | } |
| 334 | |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 335 | ext4fs_reset_block_bmap(le32_to_cpu(*tip_buffer), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 336 | fs->blk_bmaps[bg_idx], |
| 337 | bg_idx); |
| 338 | |
| 339 | tip_buffer++; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 340 | /* get block group descriptor table */ |
| 341 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 342 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 343 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 344 | /* journal backup */ |
| 345 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 346 | uint64_t b_bitmap_blk = |
| 347 | ext4fs_bg_get_block_id(bgd, fs); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 348 | status = |
| 349 | ext4fs_devread( |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 350 | b_bitmap_blk * |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 351 | fs->sect_perblk, 0, |
| 352 | fs->blksz, |
| 353 | journal_buffer); |
| 354 | if (status == 0) |
| 355 | goto fail; |
| 356 | |
| 357 | if (ext4fs_log_journal(journal_buffer, |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 358 | b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 359 | goto fail; |
| 360 | prev_bg_bmap_idx = bg_idx; |
| 361 | } |
| 362 | } |
| 363 | free(tipb_start_addr); |
| 364 | tipb_start_addr = NULL; |
| 365 | |
| 366 | /* |
| 367 | * removing the grand parent blocks |
| 368 | * which is connected to inode |
| 369 | */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 370 | bg_idx = le32_to_cpu(*tigp_buffer) / blk_per_grp; |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 371 | if (fs->blksz == 1024) { |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 372 | remainder = le32_to_cpu(*tigp_buffer) % blk_per_grp; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 373 | if (!remainder) |
| 374 | bg_idx--; |
| 375 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 376 | ext4fs_reset_block_bmap(le32_to_cpu(*tigp_buffer), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 377 | fs->blk_bmaps[bg_idx], bg_idx); |
| 378 | |
| 379 | tigp_buffer++; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 380 | /* get block group descriptor table */ |
| 381 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 382 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 383 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 384 | /* journal backup */ |
| 385 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 386 | uint64_t b_bitmap_blk = |
| 387 | ext4fs_bg_get_block_id(bgd, fs); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 388 | memset(journal_buffer, '\0', fs->blksz); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 389 | status = ext4fs_devread(b_bitmap_blk * |
| 390 | fs->sect_perblk, 0, |
| 391 | fs->blksz, |
| 392 | journal_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 393 | if (status == 0) |
| 394 | goto fail; |
| 395 | |
| 396 | if (ext4fs_log_journal(journal_buffer, |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 397 | b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 398 | goto fail; |
| 399 | prev_bg_bmap_idx = bg_idx; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* removing the grand parent triple indirect block */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 404 | blknr = le32_to_cpu(inode->b.blocks.triple_indir_block); |
Łukasz Majewski | 5767dc8 | 2014-05-06 09:36:04 +0200 | [diff] [blame] | 405 | bg_idx = blknr / blk_per_grp; |
| 406 | if (fs->blksz == 1024) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 407 | remainder = blknr % blk_per_grp; |
| 408 | if (!remainder) |
| 409 | bg_idx--; |
| 410 | } |
| 411 | 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] | 412 | /* get block group descriptor table */ |
| 413 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 414 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 415 | ext4fs_sb_free_blocks_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 416 | /* journal backup */ |
| 417 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 418 | uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs); |
| 419 | status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk, |
| 420 | 0, fs->blksz, journal_buffer); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 421 | if (status == 0) |
| 422 | goto fail; |
| 423 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 424 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 425 | goto fail; |
| 426 | prev_bg_bmap_idx = bg_idx; |
| 427 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 428 | debug("tigp buffer itself releasing %d\n", blknr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 429 | } |
| 430 | fail: |
| 431 | free(tib_start_addr); |
| 432 | free(tipb_start_addr); |
| 433 | free(journal_buffer); |
| 434 | } |
| 435 | |
| 436 | static int ext4fs_delete_file(int inodeno) |
| 437 | { |
| 438 | struct ext2_inode inode; |
| 439 | short status; |
| 440 | int i; |
| 441 | int remainder; |
| 442 | long int blknr; |
| 443 | int bg_idx; |
| 444 | int ibmap_idx; |
| 445 | char *read_buffer = NULL; |
| 446 | char *start_block_address = NULL; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 447 | uint32_t no_blocks; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 448 | |
| 449 | static int prev_bg_bmap_idx = -1; |
| 450 | unsigned int inodes_per_block; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 451 | uint32_t blkno; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 452 | unsigned int blkoff; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 453 | uint32_t blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group); |
| 454 | 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] | 455 | struct ext2_inode *inode_buffer = NULL; |
| 456 | struct ext2_block_group *bgd = NULL; |
| 457 | struct ext_filesystem *fs = get_fs(); |
| 458 | char *journal_buffer = zalloc(fs->blksz); |
| 459 | if (!journal_buffer) |
| 460 | return -ENOMEM; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 461 | status = ext4fs_read_inode(ext4fs_root, inodeno, &inode); |
| 462 | if (status == 0) |
| 463 | goto fail; |
| 464 | |
| 465 | /* read the block no allocated to a file */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 466 | no_blocks = le32_to_cpu(inode.size) / fs->blksz; |
| 467 | if (le32_to_cpu(inode.size) % fs->blksz) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 468 | no_blocks++; |
| 469 | |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 470 | /* |
| 471 | * special case for symlinks whose target are small enough that |
| 472 | *it fits in struct ext2_inode.b.symlink: no block had been allocated |
| 473 | */ |
Corentin GUILLEVIC | d4620ae | 2023-03-17 13:15:12 +0100 | [diff] [blame] | 474 | if (S_ISLNK(le16_to_cpu(inode.mode)) && |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 475 | le32_to_cpu(inode.size) <= sizeof(inode.b.symlink)) { |
| 476 | no_blocks = 0; |
| 477 | } |
| 478 | |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 479 | if (le32_to_cpu(inode.flags) & EXT4_EXTENTS_FL) { |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 480 | /* FIXME delete extent index blocks, i.e. eh_depth >= 1 */ |
| 481 | struct ext4_extent_header *eh = |
| 482 | (struct ext4_extent_header *) |
| 483 | inode.b.blocks.dir_blocks; |
| 484 | 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] | 485 | } else { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 486 | delete_single_indirect_block(&inode); |
| 487 | delete_double_indirect_block(&inode); |
| 488 | delete_triple_indirect_block(&inode); |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 489 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 490 | |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 491 | /* release data blocks */ |
| 492 | for (i = 0; i < no_blocks; i++) { |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 493 | blknr = read_allocated_block(&inode, i, NULL); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 494 | if (blknr == 0) |
| 495 | continue; |
| 496 | if (blknr < 0) |
| 497 | goto fail; |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 498 | bg_idx = blknr / blk_per_grp; |
| 499 | if (fs->blksz == 1024) { |
| 500 | remainder = blknr % blk_per_grp; |
| 501 | if (!remainder) |
| 502 | bg_idx--; |
| 503 | } |
| 504 | ext4fs_reset_block_bmap(blknr, fs->blk_bmaps[bg_idx], |
| 505 | bg_idx); |
| 506 | debug("EXT4 Block releasing %ld: %d\n", blknr, bg_idx); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 507 | |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 508 | /* get block group descriptor table */ |
| 509 | bgd = ext4fs_get_group_descriptor(fs, bg_idx); |
Stefan Brüns | 05313d1 | 2016-09-20 01:13:01 +0200 | [diff] [blame] | 510 | ext4fs_bg_free_blocks_inc(bgd, fs); |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 511 | ext4fs_sb_free_blocks_inc(fs->sb); |
| 512 | /* journal backup */ |
| 513 | if (prev_bg_bmap_idx != bg_idx) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 514 | uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs); |
| 515 | status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk, |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 516 | 0, fs->blksz, |
| 517 | journal_buffer); |
| 518 | if (status == 0) |
| 519 | goto fail; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 520 | if (ext4fs_log_journal(journal_buffer, b_bitmap_blk)) |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 521 | goto fail; |
| 522 | prev_bg_bmap_idx = bg_idx; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Stefan Brüns | 0352713 | 2016-09-06 04:36:54 +0200 | [diff] [blame] | 526 | /* release inode */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 527 | /* from the inode no to blockno */ |
| 528 | inodes_per_block = fs->blksz / fs->inodesz; |
| 529 | ibmap_idx = inodeno / inode_per_grp; |
| 530 | |
| 531 | /* get the block no */ |
| 532 | inodeno--; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 533 | /* get block group descriptor table */ |
| 534 | bgd = ext4fs_get_group_descriptor(fs, ibmap_idx); |
| 535 | blkno = ext4fs_bg_get_inode_table_id(bgd, fs) + |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 536 | (inodeno % inode_per_grp) / inodes_per_block; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 537 | |
| 538 | /* get the offset of the inode */ |
| 539 | blkoff = ((inodeno) % inodes_per_block) * fs->inodesz; |
| 540 | |
| 541 | /* read the block no containing the inode */ |
| 542 | read_buffer = zalloc(fs->blksz); |
| 543 | if (!read_buffer) |
| 544 | goto fail; |
| 545 | start_block_address = read_buffer; |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 546 | status = ext4fs_devread((lbaint_t)blkno * fs->sect_perblk, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 547 | 0, fs->blksz, read_buffer); |
| 548 | if (status == 0) |
| 549 | goto fail; |
| 550 | |
| 551 | if (ext4fs_log_journal(read_buffer, blkno)) |
| 552 | goto fail; |
| 553 | |
| 554 | read_buffer = read_buffer + blkoff; |
| 555 | inode_buffer = (struct ext2_inode *)read_buffer; |
Stefan Brüns | 7d936db | 2016-09-06 04:36:53 +0200 | [diff] [blame] | 556 | memset(inode_buffer, '\0', fs->inodesz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 557 | |
| 558 | /* write the inode to original position in inode table */ |
| 559 | if (ext4fs_put_metadata(start_block_address, blkno)) |
| 560 | goto fail; |
| 561 | |
| 562 | /* update the respective inode bitmaps */ |
| 563 | inodeno++; |
| 564 | 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] | 565 | ext4fs_bg_free_inodes_inc(bgd, fs); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 566 | ext4fs_sb_free_inodes_inc(fs->sb); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 567 | /* journal backup */ |
| 568 | memset(journal_buffer, '\0', fs->blksz); |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 569 | status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) * |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 570 | fs->sect_perblk, 0, fs->blksz, journal_buffer); |
| 571 | if (status == 0) |
| 572 | goto fail; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 573 | 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] | 574 | goto fail; |
| 575 | |
| 576 | ext4fs_update(); |
| 577 | ext4fs_deinit(); |
Łukasz Majewski | 900db5d | 2014-05-06 09:36:05 +0200 | [diff] [blame] | 578 | ext4fs_reinit_global(); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 579 | |
| 580 | if (ext4fs_init() != 0) { |
| 581 | printf("error in File System init\n"); |
| 582 | goto fail; |
| 583 | } |
| 584 | |
| 585 | free(start_block_address); |
| 586 | free(journal_buffer); |
| 587 | |
| 588 | return 0; |
| 589 | fail: |
| 590 | free(start_block_address); |
| 591 | free(journal_buffer); |
| 592 | |
| 593 | return -1; |
| 594 | } |
| 595 | |
| 596 | int ext4fs_init(void) |
| 597 | { |
| 598 | short status; |
| 599 | int i; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 600 | uint32_t real_free_blocks = 0; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 601 | struct ext_filesystem *fs = get_fs(); |
| 602 | |
| 603 | /* populate fs */ |
| 604 | fs->blksz = EXT2_BLOCK_SIZE(ext4fs_root); |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 605 | fs->sect_perblk = fs->blksz >> fs->dev_desc->log2blksz; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 606 | |
| 607 | /* get the superblock */ |
| 608 | fs->sb = zalloc(SUPERBLOCK_SIZE); |
| 609 | if (!fs->sb) |
| 610 | return -ENOMEM; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 611 | if (!ext4_read_superblock((char *)fs->sb)) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 612 | goto fail; |
| 613 | |
| 614 | /* init journal */ |
| 615 | if (ext4fs_init_journal()) |
| 616 | goto fail; |
| 617 | |
| 618 | /* get total no of blockgroups */ |
| 619 | fs->no_blkgrp = (uint32_t)ext4fs_div_roundup( |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 620 | le32_to_cpu(ext4fs_root->sblock.total_blocks) |
| 621 | - le32_to_cpu(ext4fs_root->sblock.first_data_block), |
| 622 | le32_to_cpu(ext4fs_root->sblock.blocks_per_group)); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 623 | |
| 624 | /* get the block group descriptor table */ |
| 625 | fs->gdtable_blkno = ((EXT2_MIN_BLOCK_SIZE == fs->blksz) + 1); |
| 626 | if (ext4fs_get_bgdtable() == -1) { |
| 627 | printf("Error in getting the block group descriptor table\n"); |
| 628 | goto fail; |
| 629 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 630 | |
| 631 | /* load all the available bitmap block of the partition */ |
| 632 | fs->blk_bmaps = zalloc(fs->no_blkgrp * sizeof(char *)); |
| 633 | if (!fs->blk_bmaps) |
| 634 | goto fail; |
| 635 | for (i = 0; i < fs->no_blkgrp; i++) { |
| 636 | fs->blk_bmaps[i] = zalloc(fs->blksz); |
| 637 | if (!fs->blk_bmaps[i]) |
| 638 | goto fail; |
| 639 | } |
| 640 | |
| 641 | for (i = 0; i < fs->no_blkgrp; i++) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 642 | struct ext2_block_group *bgd = |
| 643 | ext4fs_get_group_descriptor(fs, i); |
| 644 | status = ext4fs_devread(ext4fs_bg_get_block_id(bgd, fs) * |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 645 | fs->sect_perblk, 0, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 646 | fs->blksz, (char *)fs->blk_bmaps[i]); |
| 647 | if (status == 0) |
| 648 | goto fail; |
| 649 | } |
| 650 | |
| 651 | /* load all the available inode bitmap of the partition */ |
| 652 | fs->inode_bmaps = zalloc(fs->no_blkgrp * sizeof(unsigned char *)); |
| 653 | if (!fs->inode_bmaps) |
| 654 | goto fail; |
| 655 | for (i = 0; i < fs->no_blkgrp; i++) { |
| 656 | fs->inode_bmaps[i] = zalloc(fs->blksz); |
| 657 | if (!fs->inode_bmaps[i]) |
| 658 | goto fail; |
| 659 | } |
| 660 | |
| 661 | for (i = 0; i < fs->no_blkgrp; i++) { |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 662 | struct ext2_block_group *bgd = |
| 663 | ext4fs_get_group_descriptor(fs, i); |
| 664 | status = ext4fs_devread(ext4fs_bg_get_inode_id(bgd, fs) * |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 665 | fs->sect_perblk, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 666 | 0, fs->blksz, |
| 667 | (char *)fs->inode_bmaps[i]); |
| 668 | if (status == 0) |
| 669 | goto fail; |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * check filesystem consistency with free blocks of file system |
| 674 | * some time we observed that superblock freeblocks does not match |
| 675 | * with the blockgroups freeblocks when improper |
| 676 | * reboot of a linux kernel |
| 677 | */ |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 678 | for (i = 0; i < fs->no_blkgrp; i++) { |
| 679 | struct ext2_block_group *bgd = |
| 680 | ext4fs_get_group_descriptor(fs, i); |
| 681 | real_free_blocks = real_free_blocks + |
| 682 | ext4fs_bg_get_free_blocks(bgd, fs); |
| 683 | } |
| 684 | if (real_free_blocks != ext4fs_sb_get_free_blocks(fs->sb)) |
| 685 | ext4fs_sb_set_free_blocks(fs->sb, real_free_blocks); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 686 | |
| 687 | return 0; |
| 688 | fail: |
| 689 | ext4fs_deinit(); |
| 690 | |
| 691 | return -1; |
| 692 | } |
| 693 | |
| 694 | void ext4fs_deinit(void) |
| 695 | { |
| 696 | int i; |
| 697 | struct ext2_inode inode_journal; |
| 698 | struct journal_superblock_t *jsb; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 699 | uint32_t blknr; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 700 | struct ext_filesystem *fs = get_fs(); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 701 | uint32_t new_feature_incompat; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 702 | |
| 703 | /* free journal */ |
| 704 | char *temp_buff = zalloc(fs->blksz); |
| 705 | if (temp_buff) { |
| 706 | ext4fs_read_inode(ext4fs_root, EXT2_JOURNAL_INO, |
| 707 | &inode_journal); |
| 708 | blknr = read_allocated_block(&inode_journal, |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 709 | EXT2_JOURNAL_SUPERBLOCK, NULL); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 710 | ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0, fs->blksz, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 711 | temp_buff); |
| 712 | jsb = (struct journal_superblock_t *)temp_buff; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 713 | jsb->s_start = 0; |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 714 | put_ext4((uint64_t) ((uint64_t)blknr * (uint64_t)fs->blksz), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 715 | (struct journal_superblock_t *)temp_buff, fs->blksz); |
| 716 | free(temp_buff); |
| 717 | } |
| 718 | ext4fs_free_journal(); |
| 719 | |
| 720 | /* get the superblock */ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 721 | ext4_read_superblock((char *)fs->sb); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 722 | new_feature_incompat = le32_to_cpu(fs->sb->feature_incompat); |
| 723 | new_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER; |
| 724 | fs->sb->feature_incompat = cpu_to_le32(new_feature_incompat); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 725 | put_ext4((uint64_t)(SUPERBLOCK_SIZE), |
| 726 | (struct ext2_sblock *)fs->sb, (uint32_t)SUPERBLOCK_SIZE); |
| 727 | free(fs->sb); |
| 728 | fs->sb = NULL; |
| 729 | |
| 730 | if (fs->blk_bmaps) { |
| 731 | for (i = 0; i < fs->no_blkgrp; i++) { |
| 732 | free(fs->blk_bmaps[i]); |
| 733 | fs->blk_bmaps[i] = NULL; |
| 734 | } |
| 735 | free(fs->blk_bmaps); |
| 736 | fs->blk_bmaps = NULL; |
| 737 | } |
| 738 | |
| 739 | if (fs->inode_bmaps) { |
| 740 | for (i = 0; i < fs->no_blkgrp; i++) { |
| 741 | free(fs->inode_bmaps[i]); |
| 742 | fs->inode_bmaps[i] = NULL; |
| 743 | } |
| 744 | free(fs->inode_bmaps); |
| 745 | fs->inode_bmaps = NULL; |
| 746 | } |
| 747 | |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 748 | free(fs->gdtable); |
| 749 | fs->gdtable = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 750 | /* |
| 751 | * reinitiliazed the global inode and |
| 752 | * block bitmap first execution check variables |
| 753 | */ |
| 754 | fs->first_pass_ibmap = 0; |
| 755 | fs->first_pass_bbmap = 0; |
| 756 | fs->curr_inode_no = 0; |
| 757 | fs->curr_blkno = 0; |
| 758 | } |
| 759 | |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 760 | /* |
| 761 | * Write data to filesystem blocks. Uses same optimization for |
| 762 | * contigous sectors as ext4fs_read_file |
| 763 | */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 764 | static int ext4fs_write_file(struct ext2_inode *file_inode, |
Jean-Jacques Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 765 | int pos, unsigned int len, const char *buf) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 766 | { |
| 767 | int i; |
| 768 | int blockcnt; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 769 | uint32_t filesize = le32_to_cpu(file_inode->size); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 770 | struct ext_filesystem *fs = get_fs(); |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 771 | int log2blksz = fs->dev_desc->log2blksz; |
| 772 | int log2_fs_blocksize = LOG2_BLOCK_SIZE(ext4fs_root) - log2blksz; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 773 | int previous_block_number = -1; |
| 774 | int delayed_start = 0; |
| 775 | int delayed_extent = 0; |
| 776 | int delayed_next = 0; |
Jean-Jacques Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 777 | const char *delayed_buf = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 778 | |
| 779 | /* Adjust len so it we can't read past the end of the file. */ |
| 780 | if (len > filesize) |
| 781 | len = filesize; |
| 782 | |
| 783 | blockcnt = ((len + pos) + fs->blksz - 1) / fs->blksz; |
| 784 | |
| 785 | for (i = pos / fs->blksz; i < blockcnt; i++) { |
| 786 | long int blknr; |
| 787 | int blockend = fs->blksz; |
| 788 | int skipfirst = 0; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 789 | blknr = read_allocated_block(file_inode, i, NULL); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 790 | if (blknr <= 0) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 791 | return -1; |
| 792 | |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 793 | blknr = blknr << log2_fs_blocksize; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 794 | |
| 795 | if (blknr) { |
| 796 | if (previous_block_number != -1) { |
| 797 | if (delayed_next == blknr) { |
| 798 | delayed_extent += blockend; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 799 | delayed_next += blockend >> log2blksz; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 800 | } else { /* spill */ |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 801 | put_ext4((uint64_t) |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 802 | ((uint64_t)delayed_start << log2blksz), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 803 | delayed_buf, |
| 804 | (uint32_t) delayed_extent); |
| 805 | previous_block_number = blknr; |
| 806 | delayed_start = blknr; |
| 807 | delayed_extent = blockend; |
| 808 | delayed_buf = buf; |
| 809 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 810 | (blockend >> log2blksz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 811 | } |
| 812 | } else { |
| 813 | previous_block_number = blknr; |
| 814 | delayed_start = blknr; |
| 815 | delayed_extent = blockend; |
| 816 | delayed_buf = buf; |
| 817 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 818 | (blockend >> log2blksz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 819 | } |
| 820 | } else { |
| 821 | if (previous_block_number != -1) { |
| 822 | /* spill */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 823 | put_ext4((uint64_t) ((uint64_t)delayed_start << |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 824 | log2blksz), |
| 825 | delayed_buf, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 826 | (uint32_t) delayed_extent); |
| 827 | previous_block_number = -1; |
| 828 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 829 | } |
| 830 | buf += fs->blksz - skipfirst; |
| 831 | } |
| 832 | if (previous_block_number != -1) { |
| 833 | /* spill */ |
Ma Haijun | e0996ca | 2014-01-08 08:15:33 +0800 | [diff] [blame] | 834 | put_ext4((uint64_t) ((uint64_t)delayed_start << log2blksz), |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 835 | delayed_buf, (uint32_t) delayed_extent); |
| 836 | previous_block_number = -1; |
| 837 | } |
| 838 | |
| 839 | return len; |
| 840 | } |
| 841 | |
Jean-Jacques Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 842 | int ext4fs_write(const char *fname, const char *buffer, |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 843 | unsigned long sizebytes, int type) |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 844 | { |
| 845 | int ret = 0; |
| 846 | struct ext2_inode *file_inode = NULL; |
developer | 3a7e1a4 | 2024-03-19 17:20:40 +0800 | [diff] [blame] | 847 | struct ext2_inode *existing_file_inode = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 848 | unsigned char *inode_buffer = NULL; |
| 849 | int parent_inodeno; |
| 850 | int inodeno; |
| 851 | time_t timestamp = 0; |
| 852 | |
| 853 | uint64_t bytes_reqd_for_file; |
| 854 | unsigned int blks_reqd_for_file; |
| 855 | unsigned int blocks_remaining; |
| 856 | int existing_file_inodeno; |
| 857 | char *temp_ptr = NULL; |
| 858 | long int itable_blkno; |
| 859 | long int parent_itable_blkno; |
| 860 | long int blkoff; |
| 861 | struct ext2_sblock *sblock = &(ext4fs_root->sblock); |
| 862 | unsigned int inodes_per_block; |
| 863 | unsigned int ibmap_idx; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 864 | struct ext2_block_group *bgd = NULL; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 865 | struct ext_filesystem *fs = get_fs(); |
| 866 | ALLOC_CACHE_ALIGN_BUFFER(char, filename, 256); |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 867 | bool store_link_in_inode = false; |
Jeroen Hofstee | f978b00 | 2014-06-09 15:29:00 +0200 | [diff] [blame] | 868 | memset(filename, 0x00, 256); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 869 | |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 870 | if (type != FILETYPE_REG && type != FILETYPE_SYMLINK) |
| 871 | return -1; |
| 872 | |
Stefan Brüns | 7d936db | 2016-09-06 04:36:53 +0200 | [diff] [blame] | 873 | g_parent_inode = zalloc(fs->inodesz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 874 | if (!g_parent_inode) |
| 875 | goto fail; |
| 876 | |
| 877 | if (ext4fs_init() != 0) { |
| 878 | printf("error in File System init\n"); |
| 879 | return -1; |
| 880 | } |
Sébastien Szymanski | f7d4968 | 2019-03-22 09:33:52 +0100 | [diff] [blame] | 881 | |
| 882 | if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) { |
| 883 | printf("Unsupported feature metadata_csum found, not writing.\n"); |
| 884 | return -1; |
| 885 | } |
| 886 | |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 887 | inodes_per_block = fs->blksz / fs->inodesz; |
| 888 | parent_inodeno = ext4fs_get_parent_inode_num(fname, filename, F_FILE); |
| 889 | if (parent_inodeno == -1) |
| 890 | goto fail; |
| 891 | if (ext4fs_iget(parent_inodeno, g_parent_inode)) |
| 892 | goto fail; |
Stefan Brüns | 57db899 | 2016-09-06 04:36:45 +0200 | [diff] [blame] | 893 | /* do not mess up a directory using hash trees */ |
| 894 | if (le32_to_cpu(g_parent_inode->flags) & EXT4_INDEX_FL) { |
| 895 | printf("hash tree directory\n"); |
| 896 | goto fail; |
| 897 | } |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 898 | /* check if the filename is already present in root */ |
Stefan Brüns | 278f5d3 | 2016-09-06 04:36:41 +0200 | [diff] [blame] | 899 | existing_file_inodeno = ext4fs_filename_unlink(filename); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 900 | if (existing_file_inodeno != -1) { |
developer | 3a7e1a4 | 2024-03-19 17:20:40 +0800 | [diff] [blame] | 901 | existing_file_inode = (struct ext2_inode *)zalloc(fs->inodesz); |
| 902 | if (!existing_file_inode) |
| 903 | goto fail; |
| 904 | ret = ext4fs_iget(existing_file_inodeno, existing_file_inode); |
| 905 | if (ret) { |
| 906 | free(existing_file_inode); |
| 907 | goto fail; |
| 908 | } |
| 909 | |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 910 | ret = ext4fs_delete_file(existing_file_inodeno); |
| 911 | fs->first_pass_bbmap = 0; |
| 912 | fs->curr_blkno = 0; |
| 913 | |
| 914 | fs->first_pass_ibmap = 0; |
| 915 | fs->curr_inode_no = 0; |
| 916 | if (ret) |
| 917 | goto fail; |
| 918 | } |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 919 | |
| 920 | /* calculate how many blocks required */ |
| 921 | if (type == FILETYPE_SYMLINK && |
| 922 | sizebytes <= sizeof(file_inode->b.symlink)) { |
| 923 | store_link_in_inode = true; |
| 924 | bytes_reqd_for_file = 0; |
| 925 | } else { |
| 926 | bytes_reqd_for_file = sizebytes; |
| 927 | } |
| 928 | |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 929 | blks_reqd_for_file = lldiv(bytes_reqd_for_file, fs->blksz); |
| 930 | if (do_div(bytes_reqd_for_file, fs->blksz) != 0) { |
| 931 | blks_reqd_for_file++; |
| 932 | debug("total bytes for a file %u\n", blks_reqd_for_file); |
| 933 | } |
| 934 | blocks_remaining = blks_reqd_for_file; |
| 935 | /* test for available space in partition */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 936 | if (le32_to_cpu(fs->sb->free_blocks) < blks_reqd_for_file) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 937 | printf("Not enough space on partition !!!\n"); |
| 938 | goto fail; |
| 939 | } |
| 940 | |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 941 | inodeno = ext4fs_update_parent_dentry(filename, type); |
Stefan Brüns | c102068 | 2016-09-06 04:36:42 +0200 | [diff] [blame] | 942 | if (inodeno == -1) |
| 943 | goto fail; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 944 | /* prepare file inode */ |
| 945 | inode_buffer = zalloc(fs->inodesz); |
| 946 | if (!inode_buffer) |
| 947 | goto fail; |
| 948 | file_inode = (struct ext2_inode *)inode_buffer; |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 949 | file_inode->size = cpu_to_le32(sizebytes); |
| 950 | if (type == FILETYPE_SYMLINK) { |
| 951 | file_inode->mode = cpu_to_le16(S_IFLNK | S_IRWXU | S_IRWXG | |
| 952 | S_IRWXO); |
| 953 | if (store_link_in_inode) { |
| 954 | strncpy(file_inode->b.symlink, buffer, sizebytes); |
| 955 | sizebytes = 0; |
| 956 | } |
| 957 | } else { |
developer | 3a7e1a4 | 2024-03-19 17:20:40 +0800 | [diff] [blame] | 958 | if (existing_file_inode) { |
| 959 | file_inode->mode = existing_file_inode->mode; |
| 960 | } else { |
| 961 | file_inode->mode = cpu_to_le16(S_IFREG | S_IRWXU | S_IRGRP | |
| 962 | S_IROTH | S_IXGRP | S_IXOTH); |
| 963 | } |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 964 | } |
developer | 3a7e1a4 | 2024-03-19 17:20:40 +0800 | [diff] [blame] | 965 | if (existing_file_inode) |
| 966 | free(existing_file_inode); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 967 | /* ToDo: Update correct time */ |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 968 | file_inode->mtime = cpu_to_le32(timestamp); |
| 969 | file_inode->atime = cpu_to_le32(timestamp); |
| 970 | file_inode->ctime = cpu_to_le32(timestamp); |
| 971 | file_inode->nlinks = cpu_to_le16(1); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 972 | |
| 973 | /* Allocate data blocks */ |
| 974 | ext4fs_allocate_blocks(file_inode, blocks_remaining, |
| 975 | &blks_reqd_for_file); |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 976 | file_inode->blockcnt = cpu_to_le32((blks_reqd_for_file * fs->blksz) >> |
Marek Szyprowski | 07a39ab | 2019-06-21 15:32:51 +0200 | [diff] [blame] | 977 | LOG2_SECTOR_SIZE); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 978 | |
| 979 | temp_ptr = zalloc(fs->blksz); |
| 980 | if (!temp_ptr) |
| 981 | goto fail; |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 982 | ibmap_idx = inodeno / le32_to_cpu(ext4fs_root->sblock.inodes_per_group); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 983 | inodeno--; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 984 | bgd = ext4fs_get_group_descriptor(fs, ibmap_idx); |
| 985 | itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) + |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 986 | (inodeno % le32_to_cpu(sblock->inodes_per_group)) / |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 987 | inodes_per_block; |
| 988 | blkoff = (inodeno % inodes_per_block) * fs->inodesz; |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 989 | ext4fs_devread((lbaint_t)itable_blkno * fs->sect_perblk, 0, fs->blksz, |
| 990 | temp_ptr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 991 | if (ext4fs_log_journal(temp_ptr, itable_blkno)) |
| 992 | goto fail; |
| 993 | |
| 994 | memcpy(temp_ptr + blkoff, inode_buffer, fs->inodesz); |
| 995 | if (ext4fs_put_metadata(temp_ptr, itable_blkno)) |
| 996 | goto fail; |
| 997 | /* copy the file content into data blocks */ |
Jean-Jacques Hiblot | d192136 | 2019-02-13 12:15:24 +0100 | [diff] [blame] | 998 | if (ext4fs_write_file(file_inode, 0, sizebytes, buffer) == -1) { |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 999 | printf("Error in copying content\n"); |
Stefan Brüns | 74674ed | 2016-09-06 04:36:55 +0200 | [diff] [blame] | 1000 | /* FIXME: Deallocate data blocks */ |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1001 | goto fail; |
| 1002 | } |
Michael Walle | 13179c2 | 2016-09-01 11:21:40 +0200 | [diff] [blame] | 1003 | 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] | 1004 | parent_inodeno--; |
Stefan Brüns | bad7381 | 2016-09-17 02:10:10 +0200 | [diff] [blame] | 1005 | bgd = ext4fs_get_group_descriptor(fs, ibmap_idx); |
| 1006 | parent_itable_blkno = ext4fs_bg_get_inode_table_id(bgd, fs) + |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1007 | (parent_inodeno % |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 1008 | le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1009 | blkoff = (parent_inodeno % inodes_per_block) * fs->inodesz; |
| 1010 | if (parent_itable_blkno != itable_blkno) { |
| 1011 | memset(temp_ptr, '\0', fs->blksz); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 1012 | ext4fs_devread((lbaint_t)parent_itable_blkno * fs->sect_perblk, |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1013 | 0, fs->blksz, temp_ptr); |
| 1014 | if (ext4fs_log_journal(temp_ptr, parent_itable_blkno)) |
| 1015 | goto fail; |
| 1016 | |
Stefan Brüns | 7d936db | 2016-09-06 04:36:53 +0200 | [diff] [blame] | 1017 | memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1018 | if (ext4fs_put_metadata(temp_ptr, parent_itable_blkno)) |
| 1019 | goto fail; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1020 | } else { |
| 1021 | /* |
| 1022 | * If parent and child fall in same inode table block |
| 1023 | * both should be kept in 1 buffer |
| 1024 | */ |
Stefan Brüns | 7d936db | 2016-09-06 04:36:53 +0200 | [diff] [blame] | 1025 | memcpy(temp_ptr + blkoff, g_parent_inode, fs->inodesz); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1026 | gd_index--; |
| 1027 | if (ext4fs_put_metadata(temp_ptr, itable_blkno)) |
| 1028 | goto fail; |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1029 | } |
| 1030 | ext4fs_update(); |
| 1031 | ext4fs_deinit(); |
| 1032 | |
| 1033 | fs->first_pass_bbmap = 0; |
| 1034 | fs->curr_blkno = 0; |
| 1035 | fs->first_pass_ibmap = 0; |
| 1036 | fs->curr_inode_no = 0; |
| 1037 | free(inode_buffer); |
| 1038 | free(g_parent_inode); |
Stefan Brüns | 338a529 | 2016-09-06 04:36:51 +0200 | [diff] [blame] | 1039 | free(temp_ptr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1040 | g_parent_inode = NULL; |
| 1041 | |
| 1042 | return 0; |
| 1043 | fail: |
| 1044 | ext4fs_deinit(); |
| 1045 | free(inode_buffer); |
| 1046 | free(g_parent_inode); |
Stefan Brüns | 338a529 | 2016-09-06 04:36:51 +0200 | [diff] [blame] | 1047 | free(temp_ptr); |
Simon Glass | 6c8f3aa | 2012-12-26 09:53:28 +0000 | [diff] [blame] | 1048 | g_parent_inode = NULL; |
| 1049 | |
| 1050 | return -1; |
| 1051 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1052 | |
| 1053 | int ext4_write_file(const char *filename, void *buf, loff_t offset, |
| 1054 | loff_t len, loff_t *actwrite) |
| 1055 | { |
| 1056 | int ret; |
| 1057 | |
| 1058 | if (offset != 0) { |
| 1059 | printf("** Cannot support non-zero offset **\n"); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 1063 | ret = ext4fs_write(filename, buf, len, FILETYPE_REG); |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1064 | if (ret) { |
| 1065 | printf("** Error ext4fs_write() **\n"); |
| 1066 | goto fail; |
| 1067 | } |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1068 | |
Przemyslaw Marczak | 2b1d6c9 | 2015-02-17 15:31:52 +0100 | [diff] [blame] | 1069 | *actwrite = len; |
| 1070 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1071 | return 0; |
| 1072 | |
| 1073 | fail: |
Przemyslaw Marczak | 2b1d6c9 | 2015-02-17 15:31:52 +0100 | [diff] [blame] | 1074 | *actwrite = 0; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 1075 | |
| 1076 | return -1; |
| 1077 | } |
Jean-Jacques Hiblot | 448dd27 | 2019-02-13 12:15:25 +0100 | [diff] [blame] | 1078 | |
| 1079 | int ext4fs_create_link(const char *target, const char *fname) |
| 1080 | { |
| 1081 | return ext4fs_write(fname, target, strlen(target), FILETYPE_SYMLINK); |
| 1082 | } |