More patch description fixups. Standardize case.
[ext4-patch-queue.git] / new-extent-function.patch
blob503d064d38a40d40ad7f999f86fa92fda23fb195
1 ext4: Add new functions for searching extent tree
3 From: Alex Tomas <alex@clusterfs.com>
5 Add the functions ext4_ext_search_left() and ext4_ext_search_right(),
6 which are used by mballoc during ext4_ext_get_blocks to decided whether
7 to merge extent information.
9 Signed-off-by: Alex Tomas <alex@clusterfs.com>
10 Signed-off-by: Andreas Dilger <adilger@clusterfs.com>
11 Signed-off-by: Johann Lombardi <johann@clusterfs.com>
12 Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
13 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
14 ---
16 fs/ext4/extents.c | 142 +++++++++++++++++++++++++++++++++++++++
17 include/linux/ext4_fs_extents.h | 4 +
18 2 files changed, 146 insertions(+), 0 deletions(-)
21 diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
22 index 6fd9763..97a197e 100644
23 --- a/fs/ext4/extents.c
24 +++ b/fs/ext4/extents.c
25 @@ -1017,6 +1017,148 @@ out:
29 + * search the closest allocated block to the left for *logical
30 + * and returns it at @logical + it's physical address at @phys
31 + * if *logical is the smallest allocated block, the function
32 + * returns 0 at @phys
33 + * return value contains 0 (success) or error code
34 + */
35 +int
36 +ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
37 + ext4_lblk_t *logical, ext4_fsblk_t *phys)
39 + struct ext4_extent_idx *ix;
40 + struct ext4_extent *ex;
41 + int depth;
43 + BUG_ON(path == NULL);
44 + depth = path->p_depth;
45 + *phys = 0;
47 + if (depth == 0 && path->p_ext == NULL)
48 + return 0;
50 + /* usually extent in the path covers blocks smaller
51 + * then *logical, but it can be that extent is the
52 + * first one in the file */
54 + ex = path[depth].p_ext;
55 + if (*logical < le32_to_cpu(ex->ee_block)) {
56 + BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
57 + while (--depth >= 0) {
58 + ix = path[depth].p_idx;
59 + BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
60 + }
61 + return 0;
62 + }
64 + BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
66 + *logical = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1;
67 + *phys = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - 1;
68 + return 0;
71 +/*
72 + * search the closest allocated block to the right for *logical
73 + * and returns it at @logical + it's physical address at @phys
74 + * if *logical is the smallest allocated block, the function
75 + * returns 0 at @phys
76 + * return value contains 0 (success) or error code
77 + */
78 +int
79 +ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
80 + ext4_lblk_t *logical, ext4_fsblk_t *phys)
82 + struct buffer_head *bh = NULL;
83 + struct ext4_extent_header *eh;
84 + struct ext4_extent_idx *ix;
85 + struct ext4_extent *ex;
86 + ext4_fsblk_t block;
87 + int depth;
89 + BUG_ON(path == NULL);
90 + depth = path->p_depth;
91 + *phys = 0;
93 + if (depth == 0 && path->p_ext == NULL)
94 + return 0;
96 + /* usually extent in the path covers blocks smaller
97 + * then *logical, but it can be that extent is the
98 + * first one in the file */
100 + ex = path[depth].p_ext;
101 + if (*logical < le32_to_cpu(ex->ee_block)) {
102 + BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
103 + while (--depth >= 0) {
104 + ix = path[depth].p_idx;
105 + BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
107 + *logical = le32_to_cpu(ex->ee_block);
108 + *phys = ext_pblock(ex);
109 + return 0;
112 + BUG_ON(*logical < le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len));
114 + if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
115 + /* next allocated block in this leaf */
116 + ex++;
117 + *logical = le32_to_cpu(ex->ee_block);
118 + *phys = ext_pblock(ex);
119 + return 0;
122 + /* go up and search for index to the right */
123 + while (--depth >= 0) {
124 + ix = path[depth].p_idx;
125 + if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
126 + break;
129 + if (depth < 0) {
130 + /* we've gone up to the root and
131 + * found no index to the right */
132 + return 0;
135 + /* we've found index to the right, let's
136 + * follow it and find the closest allocated
137 + * block to the right */
138 + ix++;
139 + block = idx_pblock(ix);
140 + while (++depth < path->p_depth) {
141 + bh = sb_bread(inode->i_sb, block);
142 + if (bh == NULL)
143 + return -EIO;
144 + eh = ext_block_hdr(bh);
145 + if (ext4_ext_check_header(inode, eh, depth)) {
146 + brelse(bh);
147 + return -EIO;
149 + ix = EXT_FIRST_INDEX(eh);
150 + block = idx_pblock(ix);
151 + brelse(bh);
154 + bh = sb_bread(inode->i_sb, block);
155 + if (bh == NULL)
156 + return -EIO;
157 + eh = ext_block_hdr(bh);
158 + if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
159 + brelse(bh);
160 + return -EIO;
162 + ex = EXT_FIRST_EXTENT(eh);
163 + *logical = le32_to_cpu(ex->ee_block);
164 + *phys = ext_pblock(ex);
165 + brelse(bh);
166 + return 0;
171 * ext4_ext_next_allocated_block:
172 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
173 * NOTE: it considers block number from index entry as
174 diff --git a/include/linux/ext4_fs_extents.h b/include/linux/ext4_fs_extents.h
175 index 023683b..56d0ec6 100644
176 --- a/include/linux/ext4_fs_extents.h
177 +++ b/include/linux/ext4_fs_extents.h
178 @@ -221,5 +221,9 @@ extern unsigned int ext4_ext_check_overlap(struct inode *, struct ext4_extent *,
179 extern int ext4_ext_insert_extent(handle_t *, struct inode *, struct ext4_ext_path *, struct ext4_extent *);
180 extern struct ext4_ext_path *ext4_ext_find_extent(struct inode *, ext4_lblk_t,
181 struct ext4_ext_path *);
182 +extern int ext4_ext_search_left(struct inode *, struct ext4_ext_path *,
183 + ext4_lblk_t *, ext4_fsblk_t *);
184 +extern int ext4_ext_search_right(struct inode *, struct ext4_ext_path *,
185 + ext4_lblk_t *, ext4_fsblk_t *);
186 #endif /* _LINUX_EXT4_EXTENTS */