]> Git Repo - J-u-boot.git/blobdiff - fs/ext4/ext4fs.c
dm: core: Create a new header file for 'compat' features
[J-u-boot.git] / fs / ext4 / ext4fs.c
index 081509dbb4db8927b6a8fa26342c39be8bb22284..1c616a26a272ba7c15736b317bbd9f50a2524dbe 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2011 - 2012 Samsung Electronics
  * EXT4 filesystem implementation in Uboot by
@@ -17,8 +18,6 @@
  * Copyright (C) 2003, 2004  Free Software Foundation, Inc.
  *
  * ext4write : Based on generic ext4 protocol.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -26,6 +25,7 @@
 #include <ext4fs.h>
 #include "ext4_common.h"
 #include <div64.h>
+#include <malloc.h>
 
 int ext4fs_symlinknest;
 struct ext_filesystem ext_fs;
@@ -62,12 +62,21 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
        lbaint_t delayed_skipfirst = 0;
        lbaint_t delayed_next = 0;
        char *delayed_buf = NULL;
+       char *start_buf = buf;
        short status;
+       struct ext_block_cache cache;
+
+       ext_cache_init(&cache);
 
        /* Adjust len so it we can't read past the end of the file. */
        if (len + pos > filesize)
                len = (filesize - pos);
 
+       if (blocksize <= 0 || len <= 0) {
+               ext_cache_fini(&cache);
+               return -1;
+       }
+
        blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
 
        for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
@@ -75,9 +84,11 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
                int blockoff = pos - (blocksize * i);
                int blockend = blocksize;
                int skipfirst = 0;
-               blknr = read_allocated_block(&(node->inode), i);
-               if (blknr < 0)
+               blknr = read_allocated_block(&node->inode, i, &cache);
+               if (blknr < 0) {
+                       ext_cache_fini(&cache);
                        return -1;
+               }
 
                blknr = blknr << log2_fs_blocksize;
 
@@ -107,8 +118,10 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
                                                        delayed_skipfirst,
                                                        delayed_extent,
                                                        delayed_buf);
-                                       if (status == 0)
+                                       if (status == 0) {
+                                               ext_cache_fini(&cache);
                                                return -1;
+                                       }
                                        previous_block_number = blknr;
                                        delayed_start = blknr;
                                        delayed_extent = blockend;
@@ -127,17 +140,26 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
                                        (blockend >> log2blksz);
                        }
                } else {
+                       int n;
+                       int n_left;
                        if (previous_block_number != -1) {
                                /* spill */
                                status = ext4fs_devread(delayed_start,
                                                        delayed_skipfirst,
                                                        delayed_extent,
                                                        delayed_buf);
-                               if (status == 0)
+                               if (status == 0) {
+                                       ext_cache_fini(&cache);
                                        return -1;
+                               }
                                previous_block_number = -1;
                        }
-                       memset(buf, 0, blocksize - skipfirst);
+                       /* Zero no more than `len' bytes. */
+                       n = blocksize - skipfirst;
+                       n_left = len - ( buf - start_buf );
+                       if (n > n_left)
+                               n = n_left;
+                       memset(buf, 0, n);
                }
                buf += blocksize - skipfirst;
        }
@@ -146,18 +168,21 @@ int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
                status = ext4fs_devread(delayed_start,
                                        delayed_skipfirst, delayed_extent,
                                        delayed_buf);
-               if (status == 0)
+               if (status == 0) {
+                       ext_cache_fini(&cache);
                        return -1;
+               }
                previous_block_number = -1;
        }
 
        *actread  = len;
+       ext_cache_fini(&cache);
        return 0;
 }
 
 int ext4fs_ls(const char *dirname)
 {
-       struct ext2fs_node *dirnode;
+       struct ext2fs_node *dirnode = NULL;
        int status;
 
        if (dirname == NULL)
@@ -167,6 +192,8 @@ int ext4fs_ls(const char *dirname)
                                  FILETYPE_DIRECTORY);
        if (status != 1) {
                printf("** Can not find directory. **\n");
+               if (dirnode)
+                       ext4fs_free_node(dirnode, &ext4fs_root->diropen);
                return 1;
        }
 
@@ -243,3 +270,32 @@ int ext4fs_uuid(char *uuid_str)
        return -ENOSYS;
 #endif
 }
+
+void ext_cache_init(struct ext_block_cache *cache)
+{
+       memset(cache, 0, sizeof(*cache));
+}
+
+void ext_cache_fini(struct ext_block_cache *cache)
+{
+       free(cache->buf);
+       ext_cache_init(cache);
+}
+
+int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
+{
+       /* This could be more lenient, but this is simple and enough for now */
+       if (cache->buf && cache->block == block && cache->size == size)
+               return 1;
+       ext_cache_fini(cache);
+       cache->buf = malloc(size);
+       if (!cache->buf)
+               return 0;
+       if (!ext4fs_devread(block, 0, size, cache->buf)) {
+               ext_cache_fini(cache);
+               return 0;
+       }
+       cache->block = block;
+       cache->size = size;
+       return 1;
+}
This page took 0.035873 seconds and 4 git commands to generate.