/src/barebox/fs/ext4/ext4fs.c
Line | Count | Source |
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 | | * |
20 | | * This program is free software; you can redistribute it and/or modify |
21 | | * it under the terms of the GNU General Public License as published by |
22 | | * the Free Software Foundation; either version 2 of the License, or |
23 | | * (at your option) any later version. |
24 | | * |
25 | | * This program is distributed in the hope that it will be useful, |
26 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
27 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28 | | * GNU General Public License for more details. |
29 | | * |
30 | | */ |
31 | | |
32 | | #include <common.h> |
33 | | #include <malloc.h> |
34 | | #include <linux/stat.h> |
35 | | #include <linux/time.h> |
36 | | #include <asm/byteorder.h> |
37 | | #include "ext4_common.h" |
38 | | |
39 | | void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot) |
40 | 0 | { |
41 | 0 | if ((node != &node->data->diropen) && (node != currroot)) |
42 | 0 | free(node); |
43 | 0 | } |
44 | | |
45 | | /* |
46 | | * Taken from openmoko-kernel mailing list: By Andy green |
47 | | * Optimized read file API : collects and defers contiguous sector |
48 | | * reads into one potentially more efficient larger sequential read action |
49 | | */ |
50 | | loff_t ext4fs_read_file(struct ext2fs_node *node, loff_t pos, |
51 | | unsigned int len, char *buf) |
52 | 392k | { |
53 | 392k | loff_t i; |
54 | 392k | blkcnt_t blockcnt; |
55 | 392k | int log2blocksize = LOG2_EXT2_BLOCK_SIZE(node->data); |
56 | 392k | const int blockshift = log2blocksize + DISK_SECTOR_BITS; |
57 | 392k | const int blocksize = 1 << blockshift; |
58 | 392k | loff_t filesize = ext4_isize(node); |
59 | 392k | ssize_t ret; |
60 | 392k | struct ext_filesystem *fs = node->data->fs; |
61 | | |
62 | | /* Adjust len so it we can't read past the end of the file. */ |
63 | 392k | if (len + pos > filesize) |
64 | 4.59k | len = filesize - pos; |
65 | | |
66 | 392k | if (filesize <= pos) |
67 | 527 | return -EINVAL; |
68 | | |
69 | 391k | blockcnt = ((len + pos) + blocksize - 1) >> blockshift; |
70 | | |
71 | 1.35M | for (i = pos >> blockshift; i < blockcnt; i++) { |
72 | 984k | int64_t blk; |
73 | 984k | sector_t blknr; |
74 | 984k | loff_t blockoff = pos - (blocksize * i); |
75 | 984k | loff_t blockend = blocksize; |
76 | 984k | loff_t skipfirst = 0; |
77 | | |
78 | 984k | blk = read_allocated_block(node, i); |
79 | 984k | if (blk < 0) |
80 | 17.0k | return blk; |
81 | | |
82 | 967k | blknr = (sector_t)blk << log2blocksize; |
83 | | |
84 | | /* Last block. */ |
85 | 967k | if (i == blockcnt - 1) { |
86 | 370k | blockend = (len + pos) - (blocksize * i); |
87 | | |
88 | | /* The last portion is exactly blocksize. */ |
89 | 370k | if (!blockend) |
90 | 0 | blockend = blocksize; |
91 | 370k | } |
92 | | |
93 | | /* First block. */ |
94 | 967k | if (i == pos >> blockshift) { |
95 | 375k | skipfirst = blockoff; |
96 | 375k | blockend -= skipfirst; |
97 | 375k | } |
98 | | |
99 | 967k | if (blknr) { |
100 | 179k | ret = ext4fs_devread(fs, blknr, skipfirst, blockend, buf); |
101 | 179k | if (ret) |
102 | 7.95k | return ret; |
103 | 788k | } else { |
104 | 788k | memset(buf, 0, blockend); |
105 | 788k | } |
106 | | |
107 | 959k | buf += blocksize - skipfirst; |
108 | 959k | } |
109 | | |
110 | 366k | return len; |
111 | 391k | } |