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