]> git.sur5r.net Git - u-boot/blobdiff - fs/ext4/ext4_common.c
Merge git://git.denx.de/u-boot-imx
[u-boot] / fs / ext4 / ext4_common.c
index 0c11ddb1b5891d5aa766d2d5f324d1593e490cbb..e3cc30a1e0ae73b3741d26662b90a5efb211ee98 100644 (file)
@@ -432,6 +432,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;
@@ -656,6 +660,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);
 
@@ -874,10 +883,13 @@ static int unlink_filename(char *filename, unsigned int blknr)
        if (status == 0)
                goto fail;
 
-       if (ext4fs_log_journal(block_buffer, blknr))
-               goto fail;
        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);
@@ -889,14 +901,6 @@ static int unlink_filename(char *filename, unsigned int blknr)
                if (dir->inode && (strlen(filename) == dir->namelen) &&
                    (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;
-                       }
                        break;
                }
 
@@ -905,7 +909,20 @@ static int unlink_filename(char *filename, unsigned int blknr)
        } while (offset < fs->blksz);
 
        if (inodeno > 0) {
+               printf("file found, deleting\n");
+               if (ext4fs_log_journal(block_buffer, blknr))
+                       goto fail;
 
+               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;
@@ -1614,12 +1631,13 @@ 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) {
+               long int startblock, endblock;
                char *buf = zalloc(blksz);
                if (!buf)
                        return -ENOMEM;
                struct ext4_extent_header *ext_block;
                struct ext4_extent *extent;
-               int i = -1;
+               int i;
                ext_block =
                        ext4fs_get_extent_block(ext4fs_root, buf,
                                                (struct ext4_extent_header *)
@@ -1633,28 +1651,26 @@ long int read_allocated_block(struct ext2_inode *inode, int fileblock)
 
                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)) {
+               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 */
                                free(buf);
                                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;
+                               free(buf);
+                               return (fileblock - startblock) + start;
+                       }
                }
 
-               printf("Extent Error\n");
                free(buf);
-               return -1;
+               return 0;
        }
 
        /* Direct blocks. */
@@ -2327,11 +2343,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;
 
 
        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),
@@ -2362,6 +2379,7 @@ int ext4fs_mount(unsigned part_length)
        return 1;
 fail:
        printf("Failed to mount ext2 filesystem...\n");
+fail_noerr:
        free(data);
        ext4fs_root = NULL;