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