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