fs: fat: get_contents() always returns -1 for errors
[oweals/u-boot.git] / fs / ext4 / ext4_common.c
index 90e7602e2aa9711ed7072b56a2c677f7bd6d72ab..5bf78b530a989108e9298f4c374f7794de10a9a5 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2011 - 2012 Samsung Electronics
  * EXT4 filesystem implementation in Uboot by
  * Copyright (C) 2003, 2004  Free Software Foundation, Inc.
  *
  * ext4write : Based on generic ext4 protocol.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <ext_common.h>
 #include <ext4fs.h>
-#include <inttypes.h>
 #include <malloc.h>
 #include <memalign.h>
 #include <stddef.h>
@@ -60,22 +58,51 @@ static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
 
 static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
 {
-       sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) - 1);
+       uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
+       free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
+       free_blocks--;
+
+       sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
+       sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
 }
 
-static inline void ext4fs_bg_free_inodes_dec(struct ext2_block_group *bg)
+static inline void ext4fs_bg_free_inodes_dec
+       (struct ext2_block_group *bg, const struct ext_filesystem *fs)
 {
-       bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) - 1);
+       uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
+       if (fs->gdsize == 64)
+               free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
+       free_inodes--;
+
+       bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
+       if (fs->gdsize == 64)
+               bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
 }
 
-static inline void ext4fs_bg_free_blocks_dec(struct ext2_block_group *bg)
+static inline void ext4fs_bg_free_blocks_dec
+       (struct ext2_block_group *bg, const struct ext_filesystem *fs)
 {
-       bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) - 1);
+       uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
+       if (fs->gdsize == 64)
+               free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
+       free_blocks--;
+
+       bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
+       if (fs->gdsize == 64)
+               bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
 }
 
-static inline void ext4fs_bg_itable_unused_dec(struct ext2_block_group *bg)
+static inline void ext4fs_bg_itable_unused_dec
+       (struct ext2_block_group *bg, const struct ext_filesystem *fs)
 {
-       bg->bg_itable_unused = cpu_to_le16(le16_to_cpu(bg->bg_itable_unused) - 1);
+       uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
+       if (fs->gdsize == 64)
+               free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
+       free_inodes--;
+
+       bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
+       if (fs->gdsize == 64)
+               bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
 }
 
 uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
@@ -163,7 +190,7 @@ uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
        return res;
 }
 
-void put_ext4(uint64_t off, void *buf, uint32_t size)
+void put_ext4(uint64_t off, const void *buf, uint32_t size)
 {
        uint64_t startblock;
        uint64_t remainder;
@@ -182,7 +209,7 @@ void put_ext4(uint64_t off, void *buf, uint32_t size)
        if ((startblock + (size >> log2blksz)) >
            (part_offset + fs->total_sect)) {
                printf("part_offset is " LBAFU "\n", part_offset);
-               printf("total_sector is %" PRIu64 "\n", fs->total_sect);
+               printf("total_sector is %llu\n", fs->total_sect);
                printf("error: overflow occurs\n");
                return;
        }
@@ -403,6 +430,10 @@ uint16_t ext4fs_checksum_update(uint32_t i)
                crc = ext2fs_crc16(crc, desc, offset);
                offset += sizeof(desc->bg_checksum);    /* skip checksum */
                assert(offset == sizeof(*desc));
+               if (offset < fs->gdsize) {
+                       crc = ext2fs_crc16(crc, (__u8 *)desc + offset,
+                                          fs->gdsize - offset);
+               }
        }
 
        return crc;
@@ -479,7 +510,8 @@ restart:
 
 restart_read:
        /* read the block no allocated to a file */
-       first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
+       first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx,
+                                                     NULL);
        if (first_block_no_of_root <= 0)
                goto fail;
 
@@ -538,7 +570,7 @@ restart_read:
                                g_parent_inode->size = cpu_to_le32(new_size);
 
                                new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
-                               new_blockcnt += fs->sect_perblk;
+                               new_blockcnt += fs->blksz >> LOG2_SECTOR_SIZE;
                                g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
 
                                if (ext4fs_put_metadata
@@ -576,7 +608,7 @@ restart_read:
                dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
 
        dir->namelen = strlen(filename);
-       dir->filetype = FILETYPE_REG;   /* regular file */
+       dir->filetype = file_type;
        temp_dir = (char *)dir;
        temp_dir = temp_dir + sizeof(struct ext2_dirent);
        memcpy(temp_dir, filename, strlen(filename));
@@ -615,7 +647,7 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname)
 
        /* get the block no allocated to a file */
        for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
-               blknr = read_allocated_block(parent_inode, blk_idx);
+               blknr = read_allocated_block(parent_inode, blk_idx, NULL);
                if (blknr <= 0)
                        goto fail;
 
@@ -627,6 +659,11 @@ static int search_dir(struct ext2_inode *parent_inode, char *dirname)
 
                offset = 0;
                do {
+                       if (offset & 3) {
+                               printf("Badly aligned ext2_dirent\n");
+                               break;
+                       }
+
                        dir = (struct ext2_dirent *)(block_buffer + offset);
                        direntname = (char*)(dir) + sizeof(struct ext2_dirent);
 
@@ -825,74 +862,72 @@ fail:
 
 static int unlink_filename(char *filename, unsigned int blknr)
 {
-       int totalbytes = 0;
-       int templength = 0;
-       int status, inodeno;
-       int found = 0;
-       char *root_first_block_buffer = NULL;
+       int status;
+       int inodeno = 0;
+       int offset;
+       char *block_buffer = NULL;
        struct ext2_dirent *dir = NULL;
-       struct ext2_dirent *previous_dir = NULL;
-       char *ptr = NULL;
+       struct ext2_dirent *previous_dir;
        struct ext_filesystem *fs = get_fs();
        int ret = -1;
+       char *direntname;
 
-       /* get the first block of root */
-       root_first_block_buffer = zalloc(fs->blksz);
-       if (!root_first_block_buffer)
+       block_buffer = zalloc(fs->blksz);
+       if (!block_buffer)
                return -ENOMEM;
+
+       /* read the directory block */
        status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
-                               fs->blksz, root_first_block_buffer);
+                               fs->blksz, block_buffer);
        if (status == 0)
                goto fail;
 
-       if (ext4fs_log_journal(root_first_block_buffer, blknr))
-               goto fail;
-       dir = (struct ext2_dirent *)root_first_block_buffer;
-       ptr = (char *)dir;
-       totalbytes = 0;
-       while (le16_to_cpu(dir->direntlen) >= 0) {
-               /*
-                * blocksize-totalbytes because last
-                * directory length i.e., *dir->direntlen
-                * is free availble space in the block that
-                * means it is a last entry of directory entry
-                */
+       offset = 0;
+       do {
+               if (offset & 3) {
+                       printf("Badly aligned ext2_dirent\n");
+                       break;
+               }
+
+               previous_dir = dir;
+               dir = (struct ext2_dirent *)(block_buffer + offset);
+               direntname = (char *)(dir) + sizeof(struct ext2_dirent);
+
+               int direntlen = le16_to_cpu(dir->direntlen);
+               if (direntlen < sizeof(struct ext2_dirent))
+                       break;
+
                if (dir->inode && (strlen(filename) == dir->namelen) &&
-                   (strncmp(ptr + sizeof(struct ext2_dirent),
-                            filename, dir->namelen) == 0)) {
-                       printf("file found, deleting\n");
+                   (strncmp(direntname, filename, dir->namelen) == 0)) {
                        inodeno = le32_to_cpu(dir->inode);
-                       if (previous_dir) {
-                               uint16_t new_len;
-                               new_len = le16_to_cpu(previous_dir->direntlen);
-                               new_len += le16_to_cpu(dir->direntlen);
-                               previous_dir->direntlen = cpu_to_le16(new_len);
-                       } else {
-                               dir->inode = 0;
-                       }
-                       found = 1;
                        break;
                }
 
-               if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
-                       break;
+               offset += direntlen;
 
-               /* traversing the each directory entry */
-               templength = le16_to_cpu(dir->direntlen);
-               totalbytes = totalbytes + templength;
-               previous_dir = dir;
-               dir = (struct ext2_dirent *)((char *)dir + templength);
-               ptr = (char *)dir;
-       }
+       } while (offset < fs->blksz);
 
+       if (inodeno > 0) {
+               printf("file found, deleting\n");
+               if (ext4fs_log_journal(block_buffer, blknr))
+                       goto fail;
 
-       if (found == 1) {
-               if (ext4fs_put_metadata(root_first_block_buffer, blknr))
+               if (previous_dir) {
+                       /* merge dir entry with predecessor */
+                       uint16_t new_len;
+                       new_len = le16_to_cpu(previous_dir->direntlen);
+                       new_len += le16_to_cpu(dir->direntlen);
+                       previous_dir->direntlen = cpu_to_le16(new_len);
+               } else {
+                       /* invalidate dir entry */
+                       dir->inode = 0;
+               }
+               if (ext4fs_put_metadata(block_buffer, blknr))
                        goto fail;
                ret = inodeno;
        }
 fail:
-       free(root_first_block_buffer);
+       free(block_buffer);
 
        return ret;
 }
@@ -909,7 +944,7 @@ int ext4fs_filename_unlink(char *filename)
 
        /* read the block no allocated to a file */
        for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
-               blknr = read_allocated_block(g_parent_inode, blk_idx);
+               blknr = read_allocated_block(g_parent_inode, blk_idx, NULL);
                if (blknr <= 0)
                        break;
                inodeno = unlink_filename(filename, blknr);
@@ -958,7 +993,7 @@ uint32_t ext4fs_get_new_blk_no(void)
                                fs->curr_blkno = fs->curr_blkno +
                                                (i * fs->blksz * 8);
                                fs->first_pass_bbmap++;
-                               ext4fs_bg_free_blocks_dec(bgd);
+                               ext4fs_bg_free_blocks_dec(bgd, fs);
                                ext4fs_sb_free_blocks_dec(fs->sb);
                                status = ext4fs_devread(b_bitmap_blk *
                                                        fs->sect_perblk,
@@ -1033,7 +1068,7 @@ restart:
 
                        prev_bg_bitmap_index = bg_idx;
                }
-               ext4fs_bg_free_blocks_dec(bgd);
+               ext4fs_bg_free_blocks_dec(bgd, fs);
                ext4fs_sb_free_blocks_dec(fs->sb);
                goto success;
        }
@@ -1092,9 +1127,9 @@ int ext4fs_get_new_inode_no(void)
                                fs->curr_inode_no = fs->curr_inode_no +
                                                        (i * inodes_per_grp);
                                fs->first_pass_ibmap++;
-                               ext4fs_bg_free_inodes_dec(bgd);
+                               ext4fs_bg_free_inodes_dec(bgd, fs);
                                if (has_gdt_chksum)
-                                       ext4fs_bg_itable_unused_dec(bgd);
+                                       ext4fs_bg_itable_unused_dec(bgd, fs);
                                ext4fs_sb_free_inodes_dec(fs->sb);
                                status = ext4fs_devread(i_bitmap_blk *
                                                        fs->sect_perblk,
@@ -1148,7 +1183,7 @@ restart:
                                goto fail;
                        prev_inode_bitmap_index = ibmap_idx;
                }
-               ext4fs_bg_free_inodes_dec(bgd);
+               ext4fs_bg_free_inodes_dec(bgd, fs);
                if (has_gdt_chksum)
                        bgd->bg_itable_unused = bgd->free_inodes;
                ext4fs_sb_free_inodes_dec(fs->sb);
@@ -1488,7 +1523,7 @@ void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
 #endif
 
 static struct ext4_extent_header *ext4fs_get_extent_block
-       (struct ext2_data *data, char *buf,
+       (struct ext2_data *data, struct ext_block_cache *cache,
                struct ext4_extent_header *ext_block,
                uint32_t fileblock, int log2_blksz)
 {
@@ -1512,17 +1547,19 @@ static struct ext4_extent_header *ext4fs_get_extent_block
                                break;
                } while (fileblock >= le32_to_cpu(index[i].ei_block));
 
-               if (--i < 0)
-                       return NULL;
+               /*
+                * If first logical block number is higher than requested fileblock,
+                * it is a sparse file. This is handled on upper layer.
+                */
+               if (i > 0)
+                       i--;
 
                block = le16_to_cpu(index[i].ei_leaf_hi);
                block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
-
-               if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
-                                  buf))
-                       ext_block = (struct ext4_extent_header *)buf;
-               else
+               block <<= log2_blksz;
+               if (!ext_cache_read(cache, (lbaint_t)block, blksz))
                        return NULL;
+               ext_block = (struct ext4_extent_header *)cache->buf;
        }
 }
 
@@ -1534,8 +1571,12 @@ static int ext4fs_blockgroup
        int log2blksz = get_fs()->dev_desc->log2blksz;
        int desc_size = get_fs()->gdsize;
 
+       if (desc_size == 0)
+               return 0;
        desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
 
+       if (desc_per_blk == 0)
+               return 0;
        blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
                        group / desc_per_blk;
        blkoff = (group % desc_per_blk) * desc_size;
@@ -1550,7 +1591,7 @@ static int ext4fs_blockgroup
 
 int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
 {
-       struct ext2_block_group blkgrp;
+       struct ext2_block_group *blkgrp;
        struct ext2_sblock *sblock = &data->sblock;
        struct ext_filesystem *fs = get_fs();
        int log2blksz = get_fs()->dev_desc->log2blksz;
@@ -1558,17 +1599,36 @@ int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
        long int blkno;
        unsigned int blkoff;
 
+       /* Allocate blkgrp based on gdsize (for 64-bit support). */
+       blkgrp = zalloc(get_fs()->gdsize);
+       if (!blkgrp)
+               return 0;
+
        /* It is easier to calculate if the first inode is 0. */
        ino--;
+       if ( le32_to_cpu(sblock->inodes_per_group) == 0 || fs->inodesz == 0) {
+               free(blkgrp);
+               return 0;
+       }
        status = ext4fs_blockgroup(data, ino / le32_to_cpu
-                                  (sblock->inodes_per_group), &blkgrp);
-       if (status == 0)
+                                  (sblock->inodes_per_group), blkgrp);
+       if (status == 0) {
+               free(blkgrp);
                return 0;
+       }
 
        inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
-       blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
+       if ( inodes_per_block == 0 ) {
+               free(blkgrp);
+               return 0;
+       }
+       blkno = ext4fs_bg_get_inode_table_id(blkgrp, fs) +
            (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
        blkoff = (ino % inodes_per_block) * fs->inodesz;
+
+       /* Free blkgrp as it is no longer required. */
+       free(blkgrp);
+
        /* Read the inode. */
        status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
                                log2blksz), blkoff,
@@ -1579,7 +1639,8 @@ int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
        return 1;
 }
 
-long int read_allocated_block(struct ext2_inode *inode, int fileblock)
+long int read_allocated_block(struct ext2_inode *inode, int fileblock,
+                             struct ext_block_cache *cache)
 {
        long int blknr;
        int blksz;
@@ -1595,47 +1656,55 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)
                - get_fs()->dev_desc->log2blksz;
 
        if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
-               char *buf = zalloc(blksz);
-               if (!buf)
-                       return -ENOMEM;
+               long int startblock, endblock;
+               struct ext_block_cache *c, cd;
                struct ext4_extent_header *ext_block;
                struct ext4_extent *extent;
-               int i = -1;
+               int i;
+
+               if (cache) {
+                       c = cache;
+               } else {
+                       c = &cd;
+                       ext_cache_init(c);
+               }
                ext_block =
-                       ext4fs_get_extent_block(ext4fs_root, buf,
+                       ext4fs_get_extent_block(ext4fs_root, c,
                                                (struct ext4_extent_header *)
                                                inode->b.blocks.dir_blocks,
                                                fileblock, log2_blksz);
                if (!ext_block) {
                        printf("invalid extent block\n");
-                       free(buf);
+                       if (!cache)
+                               ext_cache_fini(c);
                        return -EINVAL;
                }
 
                extent = (struct ext4_extent *)(ext_block + 1);
 
-               do {
-                       i++;
-                       if (i >= le16_to_cpu(ext_block->eh_entries))
-                               break;
-               } while (fileblock >= le32_to_cpu(extent[i].ee_block));
-               if (--i >= 0) {
-                       fileblock -= le32_to_cpu(extent[i].ee_block);
-                       if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
-                               free(buf);
+               for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
+                       startblock = le32_to_cpu(extent[i].ee_block);
+                       endblock = startblock + le16_to_cpu(extent[i].ee_len);
+
+                       if (startblock > fileblock) {
+                               /* Sparse file */
+                               if (!cache)
+                                       ext_cache_fini(c);
                                return 0;
-                       }
 
-                       start = le16_to_cpu(extent[i].ee_start_hi);
-                       start = (start << 32) +
+                       } else if (fileblock < endblock) {
+                               start = le16_to_cpu(extent[i].ee_start_hi);
+                               start = (start << 32) +
                                        le32_to_cpu(extent[i].ee_start_lo);
-                       free(buf);
-                       return fileblock + start;
+                               if (!cache)
+                                       ext_cache_fini(c);
+                               return (fileblock - startblock) + start;
+                       }
                }
 
-               printf("Extent Error\n");
-               free(buf);
-               return -1;
+               if (!cache)
+                       ext_cache_fini(c);
+               return 0;
        }
 
        /* Direct blocks. */
@@ -2308,20 +2377,12 @@ int ext4fs_mount(unsigned part_length)
 
        /* Make sure this is an ext2 filesystem. */
        if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
-               goto fail;
+               goto fail_noerr;
 
-       /*
-        * The 64bit feature was enabled when metadata_csum was enabled
-        * and we do not support metadata_csum (and cannot reliably find
-        * files when it is set.  Refuse to mount.
-        */
-       if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
-               printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
-               goto fail;
-       }
 
        if (le32_to_cpu(data->sblock.revision_level) == 0) {
                fs->inodesz = 128;
+               fs->gdsize = 32;
        } else {
                debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
                      __le32_to_cpu(data->sblock.feature_compatibility),
@@ -2352,6 +2413,7 @@ int ext4fs_mount(unsigned part_length)
        return 1;
 fail:
        printf("Failed to mount ext2 filesystem...\n");
+fail_noerr:
        free(data);
        ext4fs_root = NULL;