Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com |
| 3 | * Written by Alex Tomas <alex@clusterfs.com> |
| 4 | * |
| 5 | * Architecture independence: |
| 6 | * Copyright (c) 2005, Bull S.A. |
| 7 | * Written by Pierre Peiffer <pierre.peiffer@bull.net> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public Licens |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Extents support for EXT4 |
| 25 | * |
| 26 | * TODO: |
| 27 | * - ext4*_error() should be used in some situations |
| 28 | * - analyze all BUG()/BUG_ON(), use -EIO where appropriate |
| 29 | * - smart tree reduction |
| 30 | */ |
| 31 | |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/time.h> |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 35 | #include <linux/jbd2.h> |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 36 | #include <linux/highuid.h> |
| 37 | #include <linux/pagemap.h> |
| 38 | #include <linux/quotaops.h> |
| 39 | #include <linux/string.h> |
| 40 | #include <linux/slab.h> |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 41 | #include <linux/falloc.h> |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 43 | #include "ext4_jbd2.h" |
| 44 | #include "ext4_extents.h" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 45 | |
| 46 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 47 | /* |
| 48 | * ext_pblock: |
| 49 | * combine low and high parts of physical block number into ext4_fsblk_t |
| 50 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 51 | static ext4_fsblk_t ext_pblock(struct ext4_extent *ex) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 52 | { |
| 53 | ext4_fsblk_t block; |
| 54 | |
Aneesh Kumar K.V | b377611 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 55 | block = le32_to_cpu(ex->ee_start_lo); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 56 | block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 57 | return block; |
| 58 | } |
| 59 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 60 | /* |
| 61 | * idx_pblock: |
| 62 | * combine low and high parts of a leaf physical block number into ext4_fsblk_t |
| 63 | */ |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 64 | ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 65 | { |
| 66 | ext4_fsblk_t block; |
| 67 | |
Aneesh Kumar K.V | d8dd0b4 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 68 | block = le32_to_cpu(ix->ei_leaf_lo); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 69 | block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 70 | return block; |
| 71 | } |
| 72 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 73 | /* |
| 74 | * ext4_ext_store_pblock: |
| 75 | * stores a large physical block number into an extent struct, |
| 76 | * breaking it into parts |
| 77 | */ |
Aneesh Kumar K.V | c14c6fd | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 78 | void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 79 | { |
Aneesh Kumar K.V | b377611 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 80 | ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 81 | ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 84 | /* |
| 85 | * ext4_idx_store_pblock: |
| 86 | * stores a large physical block number into an index struct, |
| 87 | * breaking it into parts |
| 88 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 89 | static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 90 | { |
Aneesh Kumar K.V | d8dd0b4 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 91 | ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); |
Mingming Cao | 9b8f1f0 | 2006-10-11 01:21:13 -0700 | [diff] [blame] | 92 | ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 95 | static int ext4_ext_journal_restart(handle_t *handle, int needed) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 96 | { |
| 97 | int err; |
| 98 | |
| 99 | if (handle->h_buffer_credits > needed) |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 100 | return 0; |
| 101 | err = ext4_journal_extend(handle, needed); |
Theodore Ts'o | 0123c93 | 2008-08-01 20:57:54 -0400 | [diff] [blame] | 102 | if (err <= 0) |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 103 | return err; |
| 104 | return ext4_journal_restart(handle, needed); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * could return: |
| 109 | * - EROFS |
| 110 | * - ENOMEM |
| 111 | */ |
| 112 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
| 113 | struct ext4_ext_path *path) |
| 114 | { |
| 115 | if (path->p_bh) { |
| 116 | /* path points to block */ |
| 117 | return ext4_journal_get_write_access(handle, path->p_bh); |
| 118 | } |
| 119 | /* path points to leaf/index in inode body */ |
| 120 | /* we use in-core data, no need to protect them */ |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * could return: |
| 126 | * - EROFS |
| 127 | * - ENOMEM |
| 128 | * - EIO |
| 129 | */ |
| 130 | static int ext4_ext_dirty(handle_t *handle, struct inode *inode, |
| 131 | struct ext4_ext_path *path) |
| 132 | { |
| 133 | int err; |
| 134 | if (path->p_bh) { |
| 135 | /* path points to block */ |
| 136 | err = ext4_journal_dirty_metadata(handle, path->p_bh); |
| 137 | } else { |
| 138 | /* path points to leaf/index in inode body */ |
| 139 | err = ext4_mark_inode_dirty(handle, inode); |
| 140 | } |
| 141 | return err; |
| 142 | } |
| 143 | |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 144 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 145 | struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 146 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 147 | { |
| 148 | struct ext4_inode_info *ei = EXT4_I(inode); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 149 | ext4_fsblk_t bg_start; |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 150 | ext4_fsblk_t last_block; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 151 | ext4_grpblk_t colour; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 152 | int depth; |
| 153 | |
| 154 | if (path) { |
| 155 | struct ext4_extent *ex; |
| 156 | depth = path->p_depth; |
| 157 | |
| 158 | /* try to predict block placement */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 159 | ex = path[depth].p_ext; |
| 160 | if (ex) |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 161 | return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 162 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 163 | /* it looks like index is empty; |
| 164 | * try to find starting block from index itself */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 165 | if (path[depth].p_bh) |
| 166 | return path[depth].p_bh->b_blocknr; |
| 167 | } |
| 168 | |
| 169 | /* OK. use inode's group */ |
| 170 | bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + |
| 171 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 172 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; |
| 173 | |
| 174 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) |
| 175 | colour = (current->pid % 16) * |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 176 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); |
Valerie Clement | 74d3487 | 2008-02-15 13:43:07 -0500 | [diff] [blame] | 177 | else |
| 178 | colour = (current->pid % 16) * ((last_block - bg_start) / 16); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 179 | return bg_start + colour + block; |
| 180 | } |
| 181 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 182 | /* |
| 183 | * Allocation for a meta data block |
| 184 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 185 | static ext4_fsblk_t |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 186 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 187 | struct ext4_ext_path *path, |
| 188 | struct ext4_extent *ex, int *err) |
| 189 | { |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 190 | ext4_fsblk_t goal, newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 191 | |
| 192 | goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); |
Aneesh Kumar K.V | 7061eba | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 193 | newblock = ext4_new_meta_block(handle, inode, goal, err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 194 | return newblock; |
| 195 | } |
| 196 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 197 | static int ext4_ext_space_block(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 198 | { |
| 199 | int size; |
| 200 | |
| 201 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 202 | / sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 203 | #ifdef AGGRESSIVE_TEST |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 204 | if (size > 6) |
| 205 | size = 6; |
| 206 | #endif |
| 207 | return size; |
| 208 | } |
| 209 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 210 | static int ext4_ext_space_block_idx(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 211 | { |
| 212 | int size; |
| 213 | |
| 214 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 215 | / sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 216 | #ifdef AGGRESSIVE_TEST |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 217 | if (size > 5) |
| 218 | size = 5; |
| 219 | #endif |
| 220 | return size; |
| 221 | } |
| 222 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 223 | static int ext4_ext_space_root(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 224 | { |
| 225 | int size; |
| 226 | |
| 227 | size = sizeof(EXT4_I(inode)->i_data); |
| 228 | size -= sizeof(struct ext4_extent_header); |
| 229 | size /= sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 230 | #ifdef AGGRESSIVE_TEST |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 231 | if (size > 3) |
| 232 | size = 3; |
| 233 | #endif |
| 234 | return size; |
| 235 | } |
| 236 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 237 | static int ext4_ext_space_root_idx(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 238 | { |
| 239 | int size; |
| 240 | |
| 241 | size = sizeof(EXT4_I(inode)->i_data); |
| 242 | size -= sizeof(struct ext4_extent_header); |
| 243 | size /= sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 244 | #ifdef AGGRESSIVE_TEST |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 245 | if (size > 4) |
| 246 | size = 4; |
| 247 | #endif |
| 248 | return size; |
| 249 | } |
| 250 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 251 | /* |
| 252 | * Calculate the number of metadata blocks needed |
| 253 | * to allocate @blocks |
| 254 | * Worse case is one block per extent |
| 255 | */ |
| 256 | int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks) |
| 257 | { |
| 258 | int lcap, icap, rcap, leafs, idxs, num; |
| 259 | int newextents = blocks; |
| 260 | |
| 261 | rcap = ext4_ext_space_root_idx(inode); |
| 262 | lcap = ext4_ext_space_block(inode); |
| 263 | icap = ext4_ext_space_block_idx(inode); |
| 264 | |
| 265 | /* number of new leaf blocks needed */ |
| 266 | num = leafs = (newextents + lcap - 1) / lcap; |
| 267 | |
| 268 | /* |
| 269 | * Worse case, we need separate index block(s) |
| 270 | * to link all new leaf blocks |
| 271 | */ |
| 272 | idxs = (leafs + icap - 1) / icap; |
| 273 | do { |
| 274 | num += idxs; |
| 275 | idxs = (idxs + icap - 1) / icap; |
| 276 | } while (idxs > rcap); |
| 277 | |
| 278 | return num; |
| 279 | } |
| 280 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 281 | static int |
| 282 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 283 | { |
| 284 | int max; |
| 285 | |
| 286 | if (depth == ext_depth(inode)) { |
| 287 | if (depth == 0) |
| 288 | max = ext4_ext_space_root(inode); |
| 289 | else |
| 290 | max = ext4_ext_space_root_idx(inode); |
| 291 | } else { |
| 292 | if (depth == 0) |
| 293 | max = ext4_ext_space_block(inode); |
| 294 | else |
| 295 | max = ext4_ext_space_block_idx(inode); |
| 296 | } |
| 297 | |
| 298 | return max; |
| 299 | } |
| 300 | |
| 301 | static int __ext4_ext_check_header(const char *function, struct inode *inode, |
| 302 | struct ext4_extent_header *eh, |
| 303 | int depth) |
| 304 | { |
| 305 | const char *error_msg; |
| 306 | int max = 0; |
| 307 | |
| 308 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { |
| 309 | error_msg = "invalid magic"; |
| 310 | goto corrupted; |
| 311 | } |
| 312 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { |
| 313 | error_msg = "unexpected eh_depth"; |
| 314 | goto corrupted; |
| 315 | } |
| 316 | if (unlikely(eh->eh_max == 0)) { |
| 317 | error_msg = "invalid eh_max"; |
| 318 | goto corrupted; |
| 319 | } |
| 320 | max = ext4_ext_max_entries(inode, depth); |
| 321 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { |
| 322 | error_msg = "too large eh_max"; |
| 323 | goto corrupted; |
| 324 | } |
| 325 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { |
| 326 | error_msg = "invalid eh_entries"; |
| 327 | goto corrupted; |
| 328 | } |
| 329 | return 0; |
| 330 | |
| 331 | corrupted: |
| 332 | ext4_error(inode->i_sb, function, |
| 333 | "bad header in inode #%lu: %s - magic %x, " |
| 334 | "entries %u, max %u(%u), depth %u(%u)", |
| 335 | inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), |
| 336 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), |
| 337 | max, le16_to_cpu(eh->eh_depth), depth); |
| 338 | |
| 339 | return -EIO; |
| 340 | } |
| 341 | |
| 342 | #define ext4_ext_check_header(inode, eh, depth) \ |
Harvey Harrison | 46e665e | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 343 | __ext4_ext_check_header(__func__, inode, eh, depth) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 344 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 345 | #ifdef EXT_DEBUG |
| 346 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 347 | { |
| 348 | int k, l = path->p_depth; |
| 349 | |
| 350 | ext_debug("path:"); |
| 351 | for (k = 0; k <= l; k++, path++) { |
| 352 | if (path->p_idx) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 353 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 354 | idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 355 | } else if (path->p_ext) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 356 | ext_debug(" %d:%d:%llu ", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 357 | le32_to_cpu(path->p_ext->ee_block), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 358 | ext4_ext_get_actual_len(path->p_ext), |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 359 | ext_pblock(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 360 | } else |
| 361 | ext_debug(" []"); |
| 362 | } |
| 363 | ext_debug("\n"); |
| 364 | } |
| 365 | |
| 366 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 367 | { |
| 368 | int depth = ext_depth(inode); |
| 369 | struct ext4_extent_header *eh; |
| 370 | struct ext4_extent *ex; |
| 371 | int i; |
| 372 | |
| 373 | if (!path) |
| 374 | return; |
| 375 | |
| 376 | eh = path[depth].p_hdr; |
| 377 | ex = EXT_FIRST_EXTENT(eh); |
| 378 | |
| 379 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 380 | ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 381 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 382 | } |
| 383 | ext_debug("\n"); |
| 384 | } |
| 385 | #else |
| 386 | #define ext4_ext_show_path(inode,path) |
| 387 | #define ext4_ext_show_leaf(inode,path) |
| 388 | #endif |
| 389 | |
Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 390 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 391 | { |
| 392 | int depth = path->p_depth; |
| 393 | int i; |
| 394 | |
| 395 | for (i = 0; i <= depth; i++, path++) |
| 396 | if (path->p_bh) { |
| 397 | brelse(path->p_bh); |
| 398 | path->p_bh = NULL; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 403 | * ext4_ext_binsearch_idx: |
| 404 | * binary search for the closest index of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 405 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 406 | */ |
| 407 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 408 | ext4_ext_binsearch_idx(struct inode *inode, |
| 409 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 410 | { |
| 411 | struct ext4_extent_header *eh = path->p_hdr; |
| 412 | struct ext4_extent_idx *r, *l, *m; |
| 413 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 414 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 415 | ext_debug("binsearch for %u(idx): ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 416 | |
| 417 | l = EXT_FIRST_INDEX(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 418 | r = EXT_LAST_INDEX(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 419 | while (l <= r) { |
| 420 | m = l + (r - l) / 2; |
| 421 | if (block < le32_to_cpu(m->ei_block)) |
| 422 | r = m - 1; |
| 423 | else |
| 424 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 425 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
| 426 | m, le32_to_cpu(m->ei_block), |
| 427 | r, le32_to_cpu(r->ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | path->p_idx = l - 1; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 431 | ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 432 | idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 433 | |
| 434 | #ifdef CHECK_BINSEARCH |
| 435 | { |
| 436 | struct ext4_extent_idx *chix, *ix; |
| 437 | int k; |
| 438 | |
| 439 | chix = ix = EXT_FIRST_INDEX(eh); |
| 440 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
| 441 | if (k != 0 && |
| 442 | le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { |
| 443 | printk("k=%d, ix=0x%p, first=0x%p\n", k, |
| 444 | ix, EXT_FIRST_INDEX(eh)); |
| 445 | printk("%u <= %u\n", |
| 446 | le32_to_cpu(ix->ei_block), |
| 447 | le32_to_cpu(ix[-1].ei_block)); |
| 448 | } |
| 449 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 450 | <= le32_to_cpu(ix[-1].ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 451 | if (block < le32_to_cpu(ix->ei_block)) |
| 452 | break; |
| 453 | chix = ix; |
| 454 | } |
| 455 | BUG_ON(chix != path->p_idx); |
| 456 | } |
| 457 | #endif |
| 458 | |
| 459 | } |
| 460 | |
| 461 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 462 | * ext4_ext_binsearch: |
| 463 | * binary search for closest extent of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 464 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 465 | */ |
| 466 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 467 | ext4_ext_binsearch(struct inode *inode, |
| 468 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 469 | { |
| 470 | struct ext4_extent_header *eh = path->p_hdr; |
| 471 | struct ext4_extent *r, *l, *m; |
| 472 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 473 | if (eh->eh_entries == 0) { |
| 474 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 475 | * this leaf is empty: |
| 476 | * we get such a leaf in split/add case |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 477 | */ |
| 478 | return; |
| 479 | } |
| 480 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 481 | ext_debug("binsearch for %u: ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 482 | |
| 483 | l = EXT_FIRST_EXTENT(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 484 | r = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 485 | |
| 486 | while (l <= r) { |
| 487 | m = l + (r - l) / 2; |
| 488 | if (block < le32_to_cpu(m->ee_block)) |
| 489 | r = m - 1; |
| 490 | else |
| 491 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 492 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
| 493 | m, le32_to_cpu(m->ee_block), |
| 494 | r, le32_to_cpu(r->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | path->p_ext = l - 1; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 498 | ext_debug(" -> %d:%llu:%d ", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 499 | le32_to_cpu(path->p_ext->ee_block), |
| 500 | ext_pblock(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 501 | ext4_ext_get_actual_len(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 502 | |
| 503 | #ifdef CHECK_BINSEARCH |
| 504 | { |
| 505 | struct ext4_extent *chex, *ex; |
| 506 | int k; |
| 507 | |
| 508 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 509 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 510 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 511 | <= le32_to_cpu(ex[-1].ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 512 | if (block < le32_to_cpu(ex->ee_block)) |
| 513 | break; |
| 514 | chex = ex; |
| 515 | } |
| 516 | BUG_ON(chex != path->p_ext); |
| 517 | } |
| 518 | #endif |
| 519 | |
| 520 | } |
| 521 | |
| 522 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
| 523 | { |
| 524 | struct ext4_extent_header *eh; |
| 525 | |
| 526 | eh = ext_inode_hdr(inode); |
| 527 | eh->eh_depth = 0; |
| 528 | eh->eh_entries = 0; |
| 529 | eh->eh_magic = EXT4_EXT_MAGIC; |
| 530 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode)); |
| 531 | ext4_mark_inode_dirty(handle, inode); |
| 532 | ext4_ext_invalidate_cache(inode); |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | struct ext4_ext_path * |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 537 | ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, |
| 538 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 539 | { |
| 540 | struct ext4_extent_header *eh; |
| 541 | struct buffer_head *bh; |
| 542 | short int depth, i, ppos = 0, alloc = 0; |
| 543 | |
| 544 | eh = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 545 | depth = ext_depth(inode); |
| 546 | if (ext4_ext_check_header(inode, eh, depth)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 547 | return ERR_PTR(-EIO); |
| 548 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 549 | |
| 550 | /* account possible depth increase */ |
| 551 | if (!path) { |
Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 552 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 553 | GFP_NOFS); |
| 554 | if (!path) |
| 555 | return ERR_PTR(-ENOMEM); |
| 556 | alloc = 1; |
| 557 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 558 | path[0].p_hdr = eh; |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 559 | path[0].p_bh = NULL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 560 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 561 | i = depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 562 | /* walk through the tree */ |
| 563 | while (i) { |
| 564 | ext_debug("depth %d: num %d, max %d\n", |
| 565 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 566 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 567 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 568 | path[ppos].p_block = idx_pblock(path[ppos].p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 569 | path[ppos].p_depth = i; |
| 570 | path[ppos].p_ext = NULL; |
| 571 | |
| 572 | bh = sb_bread(inode->i_sb, path[ppos].p_block); |
| 573 | if (!bh) |
| 574 | goto err; |
| 575 | |
| 576 | eh = ext_block_hdr(bh); |
| 577 | ppos++; |
| 578 | BUG_ON(ppos > depth); |
| 579 | path[ppos].p_bh = bh; |
| 580 | path[ppos].p_hdr = eh; |
| 581 | i--; |
| 582 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 583 | if (ext4_ext_check_header(inode, eh, i)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 584 | goto err; |
| 585 | } |
| 586 | |
| 587 | path[ppos].p_depth = i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 588 | path[ppos].p_ext = NULL; |
| 589 | path[ppos].p_idx = NULL; |
| 590 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 591 | /* find extent */ |
| 592 | ext4_ext_binsearch(inode, path + ppos, block); |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 593 | /* if not an empty leaf */ |
| 594 | if (path[ppos].p_ext) |
| 595 | path[ppos].p_block = ext_pblock(path[ppos].p_ext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 596 | |
| 597 | ext4_ext_show_path(inode, path); |
| 598 | |
| 599 | return path; |
| 600 | |
| 601 | err: |
| 602 | ext4_ext_drop_refs(path); |
| 603 | if (alloc) |
| 604 | kfree(path); |
| 605 | return ERR_PTR(-EIO); |
| 606 | } |
| 607 | |
| 608 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 609 | * ext4_ext_insert_index: |
| 610 | * insert new index [@logical;@ptr] into the block at @curp; |
| 611 | * check where to insert: before @curp or after @curp |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 612 | */ |
| 613 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 614 | struct ext4_ext_path *curp, |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 615 | int logical, ext4_fsblk_t ptr) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 616 | { |
| 617 | struct ext4_extent_idx *ix; |
| 618 | int len, err; |
| 619 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 620 | err = ext4_ext_get_access(handle, inode, curp); |
| 621 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 622 | return err; |
| 623 | |
| 624 | BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); |
| 625 | len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; |
| 626 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 627 | /* insert after */ |
| 628 | if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { |
| 629 | len = (len - 1) * sizeof(struct ext4_extent_idx); |
| 630 | len = len < 0 ? 0 : len; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 631 | ext_debug("insert new index %d after: %llu. " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 632 | "move %d from 0x%p to 0x%p\n", |
| 633 | logical, ptr, len, |
| 634 | (curp->p_idx + 1), (curp->p_idx + 2)); |
| 635 | memmove(curp->p_idx + 2, curp->p_idx + 1, len); |
| 636 | } |
| 637 | ix = curp->p_idx + 1; |
| 638 | } else { |
| 639 | /* insert before */ |
| 640 | len = len * sizeof(struct ext4_extent_idx); |
| 641 | len = len < 0 ? 0 : len; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 642 | ext_debug("insert new index %d before: %llu. " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 643 | "move %d from 0x%p to 0x%p\n", |
| 644 | logical, ptr, len, |
| 645 | curp->p_idx, (curp->p_idx + 1)); |
| 646 | memmove(curp->p_idx + 1, curp->p_idx, len); |
| 647 | ix = curp->p_idx; |
| 648 | } |
| 649 | |
| 650 | ix->ei_block = cpu_to_le32(logical); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 651 | ext4_idx_store_pblock(ix, ptr); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 652 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 653 | |
| 654 | BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 655 | > le16_to_cpu(curp->p_hdr->eh_max)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 656 | BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr)); |
| 657 | |
| 658 | err = ext4_ext_dirty(handle, inode, curp); |
| 659 | ext4_std_error(inode->i_sb, err); |
| 660 | |
| 661 | return err; |
| 662 | } |
| 663 | |
| 664 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 665 | * ext4_ext_split: |
| 666 | * inserts new subtree into the path, using free index entry |
| 667 | * at depth @at: |
| 668 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 669 | * - makes decision where to split |
| 670 | * - moves remaining extents and index entries (right to the split point) |
| 671 | * into the newly allocated blocks |
| 672 | * - initializes subtree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 673 | */ |
| 674 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
| 675 | struct ext4_ext_path *path, |
| 676 | struct ext4_extent *newext, int at) |
| 677 | { |
| 678 | struct buffer_head *bh = NULL; |
| 679 | int depth = ext_depth(inode); |
| 680 | struct ext4_extent_header *neh; |
| 681 | struct ext4_extent_idx *fidx; |
| 682 | struct ext4_extent *ex; |
| 683 | int i = at, k, m, a; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 684 | ext4_fsblk_t newblock, oldblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 685 | __le32 border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 686 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 687 | int err = 0; |
| 688 | |
| 689 | /* make decision: where to split? */ |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 690 | /* FIXME: now decision is simplest: at current extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 691 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 692 | /* if current leaf will be split, then we should use |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 693 | * border from split point */ |
| 694 | BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr)); |
| 695 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 696 | border = path[depth].p_ext[1].ee_block; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 697 | ext_debug("leaf will be split." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 698 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 699 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 700 | } else { |
| 701 | border = newext->ee_block; |
| 702 | ext_debug("leaf will be added." |
| 703 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 704 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 708 | * If error occurs, then we break processing |
| 709 | * and mark filesystem read-only. index won't |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 710 | * be inserted and tree will be in consistent |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 711 | * state. Next mount will repair buffers too. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 712 | */ |
| 713 | |
| 714 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 715 | * Get array to track all allocated blocks. |
| 716 | * We need this to handle errors and free blocks |
| 717 | * upon them. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 718 | */ |
Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 719 | ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 720 | if (!ablocks) |
| 721 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 722 | |
| 723 | /* allocate all needed blocks */ |
| 724 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
| 725 | for (a = 0; a < depth - at; a++) { |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 726 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
| 727 | newext, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 728 | if (newblock == 0) |
| 729 | goto cleanup; |
| 730 | ablocks[a] = newblock; |
| 731 | } |
| 732 | |
| 733 | /* initialize new leaf */ |
| 734 | newblock = ablocks[--a]; |
| 735 | BUG_ON(newblock == 0); |
| 736 | bh = sb_getblk(inode->i_sb, newblock); |
| 737 | if (!bh) { |
| 738 | err = -EIO; |
| 739 | goto cleanup; |
| 740 | } |
| 741 | lock_buffer(bh); |
| 742 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 743 | err = ext4_journal_get_create_access(handle, bh); |
| 744 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 745 | goto cleanup; |
| 746 | |
| 747 | neh = ext_block_hdr(bh); |
| 748 | neh->eh_entries = 0; |
| 749 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); |
| 750 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 751 | neh->eh_depth = 0; |
| 752 | ex = EXT_FIRST_EXTENT(neh); |
| 753 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 754 | /* move remainder of path[depth] to the new leaf */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 755 | BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max); |
| 756 | /* start copy from next extent */ |
| 757 | /* TODO: we could do it by single memmove */ |
| 758 | m = 0; |
| 759 | path[depth].p_ext++; |
| 760 | while (path[depth].p_ext <= |
| 761 | EXT_MAX_EXTENT(path[depth].p_hdr)) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 762 | ext_debug("move %d:%llu:%d in new leaf %llu\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 763 | le32_to_cpu(path[depth].p_ext->ee_block), |
| 764 | ext_pblock(path[depth].p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 765 | ext4_ext_get_actual_len(path[depth].p_ext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 766 | newblock); |
| 767 | /*memmove(ex++, path[depth].p_ext++, |
| 768 | sizeof(struct ext4_extent)); |
| 769 | neh->eh_entries++;*/ |
| 770 | path[depth].p_ext++; |
| 771 | m++; |
| 772 | } |
| 773 | if (m) { |
| 774 | memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 775 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | set_buffer_uptodate(bh); |
| 779 | unlock_buffer(bh); |
| 780 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 781 | err = ext4_journal_dirty_metadata(handle, bh); |
| 782 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 783 | goto cleanup; |
| 784 | brelse(bh); |
| 785 | bh = NULL; |
| 786 | |
| 787 | /* correct old leaf */ |
| 788 | if (m) { |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 789 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 790 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 791 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 792 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 793 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 794 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 795 | goto cleanup; |
| 796 | |
| 797 | } |
| 798 | |
| 799 | /* create intermediate indexes */ |
| 800 | k = depth - at - 1; |
| 801 | BUG_ON(k < 0); |
| 802 | if (k) |
| 803 | ext_debug("create %d intermediate indices\n", k); |
| 804 | /* insert new index into current index block */ |
| 805 | /* current depth stored in i var */ |
| 806 | i = depth - 1; |
| 807 | while (k--) { |
| 808 | oldblock = newblock; |
| 809 | newblock = ablocks[--a]; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 810 | bh = sb_getblk(inode->i_sb, newblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 811 | if (!bh) { |
| 812 | err = -EIO; |
| 813 | goto cleanup; |
| 814 | } |
| 815 | lock_buffer(bh); |
| 816 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 817 | err = ext4_journal_get_create_access(handle, bh); |
| 818 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 819 | goto cleanup; |
| 820 | |
| 821 | neh = ext_block_hdr(bh); |
| 822 | neh->eh_entries = cpu_to_le16(1); |
| 823 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 824 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); |
| 825 | neh->eh_depth = cpu_to_le16(depth - i); |
| 826 | fidx = EXT_FIRST_INDEX(neh); |
| 827 | fidx->ei_block = border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 828 | ext4_idx_store_pblock(fidx, oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 829 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 830 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
| 831 | i, newblock, le32_to_cpu(border), oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 832 | /* copy indexes */ |
| 833 | m = 0; |
| 834 | path[i].p_idx++; |
| 835 | |
| 836 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
| 837 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 838 | BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) != |
| 839 | EXT_LAST_INDEX(path[i].p_hdr)); |
| 840 | while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 841 | ext_debug("%d: move %d:%llu in new index %llu\n", i, |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 842 | le32_to_cpu(path[i].p_idx->ei_block), |
| 843 | idx_pblock(path[i].p_idx), |
| 844 | newblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 845 | /*memmove(++fidx, path[i].p_idx++, |
| 846 | sizeof(struct ext4_extent_idx)); |
| 847 | neh->eh_entries++; |
| 848 | BUG_ON(neh->eh_entries > neh->eh_max);*/ |
| 849 | path[i].p_idx++; |
| 850 | m++; |
| 851 | } |
| 852 | if (m) { |
| 853 | memmove(++fidx, path[i].p_idx - m, |
| 854 | sizeof(struct ext4_extent_idx) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 855 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 856 | } |
| 857 | set_buffer_uptodate(bh); |
| 858 | unlock_buffer(bh); |
| 859 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 860 | err = ext4_journal_dirty_metadata(handle, bh); |
| 861 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 862 | goto cleanup; |
| 863 | brelse(bh); |
| 864 | bh = NULL; |
| 865 | |
| 866 | /* correct old index */ |
| 867 | if (m) { |
| 868 | err = ext4_ext_get_access(handle, inode, path + i); |
| 869 | if (err) |
| 870 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 871 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 872 | err = ext4_ext_dirty(handle, inode, path + i); |
| 873 | if (err) |
| 874 | goto cleanup; |
| 875 | } |
| 876 | |
| 877 | i--; |
| 878 | } |
| 879 | |
| 880 | /* insert new index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 881 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 882 | le32_to_cpu(border), newblock); |
| 883 | |
| 884 | cleanup: |
| 885 | if (bh) { |
| 886 | if (buffer_locked(bh)) |
| 887 | unlock_buffer(bh); |
| 888 | brelse(bh); |
| 889 | } |
| 890 | |
| 891 | if (err) { |
| 892 | /* free all allocated blocks in error case */ |
| 893 | for (i = 0; i < depth; i++) { |
| 894 | if (!ablocks[i]) |
| 895 | continue; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 896 | ext4_free_blocks(handle, inode, ablocks[i], 1, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | kfree(ablocks); |
| 900 | |
| 901 | return err; |
| 902 | } |
| 903 | |
| 904 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 905 | * ext4_ext_grow_indepth: |
| 906 | * implements tree growing procedure: |
| 907 | * - allocates new block |
| 908 | * - moves top-level data (index block or leaf) into the new block |
| 909 | * - initializes new top-level, creating index that points to the |
| 910 | * just created block |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 911 | */ |
| 912 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
| 913 | struct ext4_ext_path *path, |
| 914 | struct ext4_extent *newext) |
| 915 | { |
| 916 | struct ext4_ext_path *curp = path; |
| 917 | struct ext4_extent_header *neh; |
| 918 | struct ext4_extent_idx *fidx; |
| 919 | struct buffer_head *bh; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 920 | ext4_fsblk_t newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 921 | int err = 0; |
| 922 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 923 | newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 924 | if (newblock == 0) |
| 925 | return err; |
| 926 | |
| 927 | bh = sb_getblk(inode->i_sb, newblock); |
| 928 | if (!bh) { |
| 929 | err = -EIO; |
| 930 | ext4_std_error(inode->i_sb, err); |
| 931 | return err; |
| 932 | } |
| 933 | lock_buffer(bh); |
| 934 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 935 | err = ext4_journal_get_create_access(handle, bh); |
| 936 | if (err) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 937 | unlock_buffer(bh); |
| 938 | goto out; |
| 939 | } |
| 940 | |
| 941 | /* move top-level index/leaf into new block */ |
| 942 | memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); |
| 943 | |
| 944 | /* set size of new block */ |
| 945 | neh = ext_block_hdr(bh); |
| 946 | /* old root could have indexes or leaves |
| 947 | * so calculate e_max right way */ |
| 948 | if (ext_depth(inode)) |
| 949 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); |
| 950 | else |
| 951 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); |
| 952 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 953 | set_buffer_uptodate(bh); |
| 954 | unlock_buffer(bh); |
| 955 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 956 | err = ext4_journal_dirty_metadata(handle, bh); |
| 957 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 958 | goto out; |
| 959 | |
| 960 | /* create index in new top-level index: num,max,pointer */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 961 | err = ext4_ext_get_access(handle, inode, curp); |
| 962 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 963 | goto out; |
| 964 | |
| 965 | curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; |
| 966 | curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode)); |
| 967 | curp->p_hdr->eh_entries = cpu_to_le16(1); |
| 968 | curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 969 | |
| 970 | if (path[0].p_hdr->eh_depth) |
| 971 | curp->p_idx->ei_block = |
| 972 | EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; |
| 973 | else |
| 974 | curp->p_idx->ei_block = |
| 975 | EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 976 | ext4_idx_store_pblock(curp->p_idx, newblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 977 | |
| 978 | neh = ext_inode_hdr(inode); |
| 979 | fidx = EXT_FIRST_INDEX(neh); |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 980 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 981 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 982 | le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 983 | |
| 984 | neh->eh_depth = cpu_to_le16(path->p_depth + 1); |
| 985 | err = ext4_ext_dirty(handle, inode, curp); |
| 986 | out: |
| 987 | brelse(bh); |
| 988 | |
| 989 | return err; |
| 990 | } |
| 991 | |
| 992 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 993 | * ext4_ext_create_new_leaf: |
| 994 | * finds empty index and adds new leaf. |
| 995 | * if no free index is found, then it requests in-depth growing. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 996 | */ |
| 997 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, |
| 998 | struct ext4_ext_path *path, |
| 999 | struct ext4_extent *newext) |
| 1000 | { |
| 1001 | struct ext4_ext_path *curp; |
| 1002 | int depth, i, err = 0; |
| 1003 | |
| 1004 | repeat: |
| 1005 | i = depth = ext_depth(inode); |
| 1006 | |
| 1007 | /* walk up to the tree and look for free index entry */ |
| 1008 | curp = path + depth; |
| 1009 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1010 | i--; |
| 1011 | curp--; |
| 1012 | } |
| 1013 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1014 | /* we use already allocated block for index block, |
| 1015 | * so subsequent data blocks should be contiguous */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1016 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1017 | /* if we found index with free entry, then use that |
| 1018 | * entry: create all needed subtree and add new leaf */ |
| 1019 | err = ext4_ext_split(handle, inode, path, newext, i); |
Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1020 | if (err) |
| 1021 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1022 | |
| 1023 | /* refill path */ |
| 1024 | ext4_ext_drop_refs(path); |
| 1025 | path = ext4_ext_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1026 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
| 1027 | path); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1028 | if (IS_ERR(path)) |
| 1029 | err = PTR_ERR(path); |
| 1030 | } else { |
| 1031 | /* tree is full, time to grow in depth */ |
| 1032 | err = ext4_ext_grow_indepth(handle, inode, path, newext); |
| 1033 | if (err) |
| 1034 | goto out; |
| 1035 | |
| 1036 | /* refill path */ |
| 1037 | ext4_ext_drop_refs(path); |
| 1038 | path = ext4_ext_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1039 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
| 1040 | path); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1041 | if (IS_ERR(path)) { |
| 1042 | err = PTR_ERR(path); |
| 1043 | goto out; |
| 1044 | } |
| 1045 | |
| 1046 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1047 | * only first (depth 0 -> 1) produces free space; |
| 1048 | * in all other cases we have to split the grown tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1049 | */ |
| 1050 | depth = ext_depth(inode); |
| 1051 | if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1052 | /* now we need to split */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1053 | goto repeat; |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | out: |
| 1058 | return err; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1062 | * search the closest allocated block to the left for *logical |
| 1063 | * and returns it at @logical + it's physical address at @phys |
| 1064 | * if *logical is the smallest allocated block, the function |
| 1065 | * returns 0 at @phys |
| 1066 | * return value contains 0 (success) or error code |
| 1067 | */ |
| 1068 | int |
| 1069 | ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, |
| 1070 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
| 1071 | { |
| 1072 | struct ext4_extent_idx *ix; |
| 1073 | struct ext4_extent *ex; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1074 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1075 | |
| 1076 | BUG_ON(path == NULL); |
| 1077 | depth = path->p_depth; |
| 1078 | *phys = 0; |
| 1079 | |
| 1080 | if (depth == 0 && path->p_ext == NULL) |
| 1081 | return 0; |
| 1082 | |
| 1083 | /* usually extent in the path covers blocks smaller |
| 1084 | * then *logical, but it can be that extent is the |
| 1085 | * first one in the file */ |
| 1086 | |
| 1087 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1088 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1089 | if (*logical < le32_to_cpu(ex->ee_block)) { |
| 1090 | BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); |
| 1091 | while (--depth >= 0) { |
| 1092 | ix = path[depth].p_idx; |
| 1093 | BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); |
| 1094 | } |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1098 | BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1099 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1100 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
| 1101 | *phys = ext_pblock(ex) + ee_len - 1; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * search the closest allocated block to the right for *logical |
| 1107 | * and returns it at @logical + it's physical address at @phys |
| 1108 | * if *logical is the smallest allocated block, the function |
| 1109 | * returns 0 at @phys |
| 1110 | * return value contains 0 (success) or error code |
| 1111 | */ |
| 1112 | int |
| 1113 | ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, |
| 1114 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
| 1115 | { |
| 1116 | struct buffer_head *bh = NULL; |
| 1117 | struct ext4_extent_header *eh; |
| 1118 | struct ext4_extent_idx *ix; |
| 1119 | struct ext4_extent *ex; |
| 1120 | ext4_fsblk_t block; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1121 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1122 | |
| 1123 | BUG_ON(path == NULL); |
| 1124 | depth = path->p_depth; |
| 1125 | *phys = 0; |
| 1126 | |
| 1127 | if (depth == 0 && path->p_ext == NULL) |
| 1128 | return 0; |
| 1129 | |
| 1130 | /* usually extent in the path covers blocks smaller |
| 1131 | * then *logical, but it can be that extent is the |
| 1132 | * first one in the file */ |
| 1133 | |
| 1134 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1135 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1136 | if (*logical < le32_to_cpu(ex->ee_block)) { |
| 1137 | BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); |
| 1138 | while (--depth >= 0) { |
| 1139 | ix = path[depth].p_idx; |
| 1140 | BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); |
| 1141 | } |
| 1142 | *logical = le32_to_cpu(ex->ee_block); |
| 1143 | *phys = ext_pblock(ex); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1147 | BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1148 | |
| 1149 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1150 | /* next allocated block in this leaf */ |
| 1151 | ex++; |
| 1152 | *logical = le32_to_cpu(ex->ee_block); |
| 1153 | *phys = ext_pblock(ex); |
| 1154 | return 0; |
| 1155 | } |
| 1156 | |
| 1157 | /* go up and search for index to the right */ |
| 1158 | while (--depth >= 0) { |
| 1159 | ix = path[depth].p_idx; |
| 1160 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
| 1161 | break; |
| 1162 | } |
| 1163 | |
| 1164 | if (depth < 0) { |
| 1165 | /* we've gone up to the root and |
| 1166 | * found no index to the right */ |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
| 1170 | /* we've found index to the right, let's |
| 1171 | * follow it and find the closest allocated |
| 1172 | * block to the right */ |
| 1173 | ix++; |
| 1174 | block = idx_pblock(ix); |
| 1175 | while (++depth < path->p_depth) { |
| 1176 | bh = sb_bread(inode->i_sb, block); |
| 1177 | if (bh == NULL) |
| 1178 | return -EIO; |
| 1179 | eh = ext_block_hdr(bh); |
| 1180 | if (ext4_ext_check_header(inode, eh, depth)) { |
| 1181 | put_bh(bh); |
| 1182 | return -EIO; |
| 1183 | } |
| 1184 | ix = EXT_FIRST_INDEX(eh); |
| 1185 | block = idx_pblock(ix); |
| 1186 | put_bh(bh); |
| 1187 | } |
| 1188 | |
| 1189 | bh = sb_bread(inode->i_sb, block); |
| 1190 | if (bh == NULL) |
| 1191 | return -EIO; |
| 1192 | eh = ext_block_hdr(bh); |
| 1193 | if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) { |
| 1194 | put_bh(bh); |
| 1195 | return -EIO; |
| 1196 | } |
| 1197 | ex = EXT_FIRST_EXTENT(eh); |
| 1198 | *logical = le32_to_cpu(ex->ee_block); |
| 1199 | *phys = ext_pblock(ex); |
| 1200 | put_bh(bh); |
| 1201 | return 0; |
| 1202 | |
| 1203 | } |
| 1204 | |
| 1205 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1206 | * ext4_ext_next_allocated_block: |
| 1207 | * returns allocated block in subsequent extent or EXT_MAX_BLOCK. |
| 1208 | * NOTE: it considers block number from index entry as |
| 1209 | * allocated block. Thus, index entries have to be consistent |
| 1210 | * with leaves. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1211 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1212 | static ext4_lblk_t |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1213 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1214 | { |
| 1215 | int depth; |
| 1216 | |
| 1217 | BUG_ON(path == NULL); |
| 1218 | depth = path->p_depth; |
| 1219 | |
| 1220 | if (depth == 0 && path->p_ext == NULL) |
| 1221 | return EXT_MAX_BLOCK; |
| 1222 | |
| 1223 | while (depth >= 0) { |
| 1224 | if (depth == path->p_depth) { |
| 1225 | /* leaf */ |
| 1226 | if (path[depth].p_ext != |
| 1227 | EXT_LAST_EXTENT(path[depth].p_hdr)) |
| 1228 | return le32_to_cpu(path[depth].p_ext[1].ee_block); |
| 1229 | } else { |
| 1230 | /* index */ |
| 1231 | if (path[depth].p_idx != |
| 1232 | EXT_LAST_INDEX(path[depth].p_hdr)) |
| 1233 | return le32_to_cpu(path[depth].p_idx[1].ei_block); |
| 1234 | } |
| 1235 | depth--; |
| 1236 | } |
| 1237 | |
| 1238 | return EXT_MAX_BLOCK; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1242 | * ext4_ext_next_leaf_block: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1243 | * returns first allocated block from next leaf or EXT_MAX_BLOCK |
| 1244 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1245 | static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1246 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1247 | { |
| 1248 | int depth; |
| 1249 | |
| 1250 | BUG_ON(path == NULL); |
| 1251 | depth = path->p_depth; |
| 1252 | |
| 1253 | /* zero-tree has no leaf blocks at all */ |
| 1254 | if (depth == 0) |
| 1255 | return EXT_MAX_BLOCK; |
| 1256 | |
| 1257 | /* go to index block */ |
| 1258 | depth--; |
| 1259 | |
| 1260 | while (depth >= 0) { |
| 1261 | if (path[depth].p_idx != |
| 1262 | EXT_LAST_INDEX(path[depth].p_hdr)) |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1263 | return (ext4_lblk_t) |
| 1264 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1265 | depth--; |
| 1266 | } |
| 1267 | |
| 1268 | return EXT_MAX_BLOCK; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1272 | * ext4_ext_correct_indexes: |
| 1273 | * if leaf gets modified and modified extent is first in the leaf, |
| 1274 | * then we have to correct all indexes above. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1275 | * TODO: do we need to correct tree in all cases? |
| 1276 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1277 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1278 | struct ext4_ext_path *path) |
| 1279 | { |
| 1280 | struct ext4_extent_header *eh; |
| 1281 | int depth = ext_depth(inode); |
| 1282 | struct ext4_extent *ex; |
| 1283 | __le32 border; |
| 1284 | int k, err = 0; |
| 1285 | |
| 1286 | eh = path[depth].p_hdr; |
| 1287 | ex = path[depth].p_ext; |
| 1288 | BUG_ON(ex == NULL); |
| 1289 | BUG_ON(eh == NULL); |
| 1290 | |
| 1291 | if (depth == 0) { |
| 1292 | /* there is no tree at all */ |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1297 | /* we correct tree if first leaf got modified only */ |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1302 | * TODO: we need correction if border is smaller than current one |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1303 | */ |
| 1304 | k = depth - 1; |
| 1305 | border = path[depth].p_ext->ee_block; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1306 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1307 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1308 | return err; |
| 1309 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1310 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1311 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1312 | return err; |
| 1313 | |
| 1314 | while (k--) { |
| 1315 | /* change all left-side indexes */ |
| 1316 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1317 | break; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1318 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1319 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1320 | break; |
| 1321 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1322 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1323 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1324 | break; |
| 1325 | } |
| 1326 | |
| 1327 | return err; |
| 1328 | } |
| 1329 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1330 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1331 | ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, |
| 1332 | struct ext4_extent *ex2) |
| 1333 | { |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1334 | unsigned short ext1_ee_len, ext2_ee_len, max_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1335 | |
| 1336 | /* |
| 1337 | * Make sure that either both extents are uninitialized, or |
| 1338 | * both are _not_. |
| 1339 | */ |
| 1340 | if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) |
| 1341 | return 0; |
| 1342 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1343 | if (ext4_ext_is_uninitialized(ex1)) |
| 1344 | max_len = EXT_UNINIT_MAX_LEN; |
| 1345 | else |
| 1346 | max_len = EXT_INIT_MAX_LEN; |
| 1347 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1348 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1349 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1350 | |
| 1351 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1352 | le32_to_cpu(ex2->ee_block)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1353 | return 0; |
| 1354 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1355 | /* |
| 1356 | * To allow future support for preallocated extents to be added |
| 1357 | * as an RO_COMPAT feature, refuse to merge to extents if |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1358 | * this can result in the top bit of ee_len being set. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1359 | */ |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1360 | if (ext1_ee_len + ext2_ee_len > max_len) |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1361 | return 0; |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1362 | #ifdef AGGRESSIVE_TEST |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1363 | if (ext1_ee_len >= 4) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1364 | return 0; |
| 1365 | #endif |
| 1366 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1367 | if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1368 | return 1; |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1373 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1374 | * It always tries to merge towards right. If you want to merge towards |
| 1375 | * left, pass "ex - 1" as argument instead of "ex". |
| 1376 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1377 | * 1 if they got merged. |
| 1378 | */ |
| 1379 | int ext4_ext_try_to_merge(struct inode *inode, |
| 1380 | struct ext4_ext_path *path, |
| 1381 | struct ext4_extent *ex) |
| 1382 | { |
| 1383 | struct ext4_extent_header *eh; |
| 1384 | unsigned int depth, len; |
| 1385 | int merge_done = 0; |
| 1386 | int uninitialized = 0; |
| 1387 | |
| 1388 | depth = ext_depth(inode); |
| 1389 | BUG_ON(path[depth].p_hdr == NULL); |
| 1390 | eh = path[depth].p_hdr; |
| 1391 | |
| 1392 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1393 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1394 | break; |
| 1395 | /* merge with next extent! */ |
| 1396 | if (ext4_ext_is_uninitialized(ex)) |
| 1397 | uninitialized = 1; |
| 1398 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1399 | + ext4_ext_get_actual_len(ex + 1)); |
| 1400 | if (uninitialized) |
| 1401 | ext4_ext_mark_uninitialized(ex); |
| 1402 | |
| 1403 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1404 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1405 | * sizeof(struct ext4_extent); |
| 1406 | memmove(ex + 1, ex + 2, len); |
| 1407 | } |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1408 | le16_add_cpu(&eh->eh_entries, -1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1409 | merge_done = 1; |
| 1410 | WARN_ON(eh->eh_entries == 0); |
| 1411 | if (!eh->eh_entries) |
| 1412 | ext4_error(inode->i_sb, "ext4_ext_try_to_merge", |
| 1413 | "inode#%lu, eh->eh_entries = 0!", inode->i_ino); |
| 1414 | } |
| 1415 | |
| 1416 | return merge_done; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1420 | * check if a portion of the "newext" extent overlaps with an |
| 1421 | * existing extent. |
| 1422 | * |
| 1423 | * If there is an overlap discovered, it updates the length of the newext |
| 1424 | * such that there will be no overlap, and then returns 1. |
| 1425 | * If there is no overlap found, it returns 0. |
| 1426 | */ |
| 1427 | unsigned int ext4_ext_check_overlap(struct inode *inode, |
| 1428 | struct ext4_extent *newext, |
| 1429 | struct ext4_ext_path *path) |
| 1430 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1431 | ext4_lblk_t b1, b2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1432 | unsigned int depth, len1; |
| 1433 | unsigned int ret = 0; |
| 1434 | |
| 1435 | b1 = le32_to_cpu(newext->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1436 | len1 = ext4_ext_get_actual_len(newext); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1437 | depth = ext_depth(inode); |
| 1438 | if (!path[depth].p_ext) |
| 1439 | goto out; |
| 1440 | b2 = le32_to_cpu(path[depth].p_ext->ee_block); |
| 1441 | |
| 1442 | /* |
| 1443 | * get the next allocated block if the extent in the path |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame^] | 1444 | * is before the requested block(s) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1445 | */ |
| 1446 | if (b2 < b1) { |
| 1447 | b2 = ext4_ext_next_allocated_block(path); |
| 1448 | if (b2 == EXT_MAX_BLOCK) |
| 1449 | goto out; |
| 1450 | } |
| 1451 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1452 | /* check for wrap through zero on extent logical start block*/ |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1453 | if (b1 + len1 < b1) { |
| 1454 | len1 = EXT_MAX_BLOCK - b1; |
| 1455 | newext->ee_len = cpu_to_le16(len1); |
| 1456 | ret = 1; |
| 1457 | } |
| 1458 | |
| 1459 | /* check for overlap */ |
| 1460 | if (b1 + len1 > b2) { |
| 1461 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1462 | ret = 1; |
| 1463 | } |
| 1464 | out: |
| 1465 | return ret; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1469 | * ext4_ext_insert_extent: |
| 1470 | * tries to merge requsted extent into the existing extent or |
| 1471 | * inserts requested extent as new one into the tree, |
| 1472 | * creating new leaf in the no-space case. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1473 | */ |
| 1474 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
| 1475 | struct ext4_ext_path *path, |
| 1476 | struct ext4_extent *newext) |
| 1477 | { |
| 1478 | struct ext4_extent_header * eh; |
| 1479 | struct ext4_extent *ex, *fex; |
| 1480 | struct ext4_extent *nearex; /* nearest extent */ |
| 1481 | struct ext4_ext_path *npath = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1482 | int depth, len, err; |
| 1483 | ext4_lblk_t next; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1484 | unsigned uninitialized = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1485 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1486 | BUG_ON(ext4_ext_get_actual_len(newext) == 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1487 | depth = ext_depth(inode); |
| 1488 | ex = path[depth].p_ext; |
| 1489 | BUG_ON(path[depth].p_hdr == NULL); |
| 1490 | |
| 1491 | /* try to insert block into found extent and return */ |
| 1492 | if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1493 | ext_debug("append %d block to %d:%d (from %llu)\n", |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1494 | ext4_ext_get_actual_len(newext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1495 | le32_to_cpu(ex->ee_block), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1496 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1497 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1498 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1499 | return err; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1500 | |
| 1501 | /* |
| 1502 | * ext4_can_extents_be_merged should have checked that either |
| 1503 | * both extents are uninitialized, or both aren't. Thus we |
| 1504 | * need to check only one of them here. |
| 1505 | */ |
| 1506 | if (ext4_ext_is_uninitialized(ex)) |
| 1507 | uninitialized = 1; |
| 1508 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1509 | + ext4_ext_get_actual_len(newext)); |
| 1510 | if (uninitialized) |
| 1511 | ext4_ext_mark_uninitialized(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1512 | eh = path[depth].p_hdr; |
| 1513 | nearex = ex; |
| 1514 | goto merge; |
| 1515 | } |
| 1516 | |
| 1517 | repeat: |
| 1518 | depth = ext_depth(inode); |
| 1519 | eh = path[depth].p_hdr; |
| 1520 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 1521 | goto has_space; |
| 1522 | |
| 1523 | /* probably next leaf has space for us? */ |
| 1524 | fex = EXT_LAST_EXTENT(eh); |
| 1525 | next = ext4_ext_next_leaf_block(inode, path); |
| 1526 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) |
| 1527 | && next != EXT_MAX_BLOCK) { |
| 1528 | ext_debug("next leaf block - %d\n", next); |
| 1529 | BUG_ON(npath != NULL); |
| 1530 | npath = ext4_ext_find_extent(inode, next, NULL); |
| 1531 | if (IS_ERR(npath)) |
| 1532 | return PTR_ERR(npath); |
| 1533 | BUG_ON(npath->p_depth != path->p_depth); |
| 1534 | eh = npath[depth].p_hdr; |
| 1535 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { |
| 1536 | ext_debug("next leaf isnt full(%d)\n", |
| 1537 | le16_to_cpu(eh->eh_entries)); |
| 1538 | path = npath; |
| 1539 | goto repeat; |
| 1540 | } |
| 1541 | ext_debug("next leaf has no free space(%d,%d)\n", |
| 1542 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 1543 | } |
| 1544 | |
| 1545 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1546 | * There is no free space in the found leaf. |
| 1547 | * We're gonna add a new leaf in the tree. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1548 | */ |
| 1549 | err = ext4_ext_create_new_leaf(handle, inode, path, newext); |
| 1550 | if (err) |
| 1551 | goto cleanup; |
| 1552 | depth = ext_depth(inode); |
| 1553 | eh = path[depth].p_hdr; |
| 1554 | |
| 1555 | has_space: |
| 1556 | nearex = path[depth].p_ext; |
| 1557 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1558 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1559 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1560 | goto cleanup; |
| 1561 | |
| 1562 | if (!nearex) { |
| 1563 | /* there is no extent in this leaf, create first one */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1564 | ext_debug("first extent in the leaf: %d:%llu:%d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1565 | le32_to_cpu(newext->ee_block), |
| 1566 | ext_pblock(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1567 | ext4_ext_get_actual_len(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1568 | path[depth].p_ext = EXT_FIRST_EXTENT(eh); |
| 1569 | } else if (le32_to_cpu(newext->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1570 | > le32_to_cpu(nearex->ee_block)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1571 | /* BUG_ON(newext->ee_block == nearex->ee_block); */ |
| 1572 | if (nearex != EXT_LAST_EXTENT(eh)) { |
| 1573 | len = EXT_MAX_EXTENT(eh) - nearex; |
| 1574 | len = (len - 1) * sizeof(struct ext4_extent); |
| 1575 | len = len < 0 ? 0 : len; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1576 | ext_debug("insert %d:%llu:%d after: nearest 0x%p, " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1577 | "move %d from 0x%p to 0x%p\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1578 | le32_to_cpu(newext->ee_block), |
| 1579 | ext_pblock(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1580 | ext4_ext_get_actual_len(newext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1581 | nearex, len, nearex + 1, nearex + 2); |
| 1582 | memmove(nearex + 2, nearex + 1, len); |
| 1583 | } |
| 1584 | path[depth].p_ext = nearex + 1; |
| 1585 | } else { |
| 1586 | BUG_ON(newext->ee_block == nearex->ee_block); |
| 1587 | len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); |
| 1588 | len = len < 0 ? 0 : len; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1589 | ext_debug("insert %d:%llu:%d before: nearest 0x%p, " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1590 | "move %d from 0x%p to 0x%p\n", |
| 1591 | le32_to_cpu(newext->ee_block), |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1592 | ext_pblock(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1593 | ext4_ext_get_actual_len(newext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1594 | nearex, len, nearex + 1, nearex + 2); |
| 1595 | memmove(nearex + 1, nearex, len); |
| 1596 | path[depth].p_ext = nearex; |
| 1597 | } |
| 1598 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1599 | le16_add_cpu(&eh->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1600 | nearex = path[depth].p_ext; |
| 1601 | nearex->ee_block = newext->ee_block; |
Aneesh Kumar K.V | b377611 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 1602 | ext4_ext_store_pblock(nearex, ext_pblock(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1603 | nearex->ee_len = newext->ee_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1604 | |
| 1605 | merge: |
| 1606 | /* try to merge extents to the right */ |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1607 | ext4_ext_try_to_merge(inode, path, nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1608 | |
| 1609 | /* try to merge extents to the left */ |
| 1610 | |
| 1611 | /* time to correct all indexes above */ |
| 1612 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 1613 | if (err) |
| 1614 | goto cleanup; |
| 1615 | |
| 1616 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1617 | |
| 1618 | cleanup: |
| 1619 | if (npath) { |
| 1620 | ext4_ext_drop_refs(npath); |
| 1621 | kfree(npath); |
| 1622 | } |
| 1623 | ext4_ext_tree_changed(inode); |
| 1624 | ext4_ext_invalidate_cache(inode); |
| 1625 | return err; |
| 1626 | } |
| 1627 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1628 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1629 | ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, |
Mingming Cao | dd54567 | 2007-07-31 00:37:46 -0700 | [diff] [blame] | 1630 | __u32 len, ext4_fsblk_t start, int type) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1631 | { |
| 1632 | struct ext4_ext_cache *cex; |
| 1633 | BUG_ON(len == 0); |
| 1634 | cex = &EXT4_I(inode)->i_cached_extent; |
| 1635 | cex->ec_type = type; |
| 1636 | cex->ec_block = block; |
| 1637 | cex->ec_len = len; |
| 1638 | cex->ec_start = start; |
| 1639 | } |
| 1640 | |
| 1641 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1642 | * ext4_ext_put_gap_in_cache: |
| 1643 | * calculate boundaries of the gap that the requested block fits into |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1644 | * and cache this gap |
| 1645 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1646 | static void |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1647 | ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1648 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1649 | { |
| 1650 | int depth = ext_depth(inode); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1651 | unsigned long len; |
| 1652 | ext4_lblk_t lblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1653 | struct ext4_extent *ex; |
| 1654 | |
| 1655 | ex = path[depth].p_ext; |
| 1656 | if (ex == NULL) { |
| 1657 | /* there is no extent yet, so gap is [0;-] */ |
| 1658 | lblock = 0; |
| 1659 | len = EXT_MAX_BLOCK; |
| 1660 | ext_debug("cache gap(whole file):"); |
| 1661 | } else if (block < le32_to_cpu(ex->ee_block)) { |
| 1662 | lblock = block; |
| 1663 | len = le32_to_cpu(ex->ee_block) - block; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1664 | ext_debug("cache gap(before): %u [%u:%u]", |
| 1665 | block, |
| 1666 | le32_to_cpu(ex->ee_block), |
| 1667 | ext4_ext_get_actual_len(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1668 | } else if (block >= le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1669 | + ext4_ext_get_actual_len(ex)) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1670 | ext4_lblk_t next; |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1671 | lblock = le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1672 | + ext4_ext_get_actual_len(ex); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1673 | |
| 1674 | next = ext4_ext_next_allocated_block(path); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1675 | ext_debug("cache gap(after): [%u:%u] %u", |
| 1676 | le32_to_cpu(ex->ee_block), |
| 1677 | ext4_ext_get_actual_len(ex), |
| 1678 | block); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1679 | BUG_ON(next == lblock); |
| 1680 | len = next - lblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1681 | } else { |
| 1682 | lblock = len = 0; |
| 1683 | BUG(); |
| 1684 | } |
| 1685 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1686 | ext_debug(" -> %u:%lu\n", lblock, len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1687 | ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); |
| 1688 | } |
| 1689 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1690 | static int |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1691 | ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1692 | struct ext4_extent *ex) |
| 1693 | { |
| 1694 | struct ext4_ext_cache *cex; |
| 1695 | |
| 1696 | cex = &EXT4_I(inode)->i_cached_extent; |
| 1697 | |
| 1698 | /* has cache valid data? */ |
| 1699 | if (cex->ec_type == EXT4_EXT_CACHE_NO) |
| 1700 | return EXT4_EXT_CACHE_NO; |
| 1701 | |
| 1702 | BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && |
| 1703 | cex->ec_type != EXT4_EXT_CACHE_EXTENT); |
| 1704 | if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) { |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1705 | ex->ee_block = cpu_to_le32(cex->ec_block); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1706 | ext4_ext_store_pblock(ex, cex->ec_start); |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1707 | ex->ee_len = cpu_to_le16(cex->ec_len); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1708 | ext_debug("%u cached by %u:%u:%llu\n", |
| 1709 | block, |
| 1710 | cex->ec_block, cex->ec_len, cex->ec_start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1711 | return cex->ec_type; |
| 1712 | } |
| 1713 | |
| 1714 | /* not in cache */ |
| 1715 | return EXT4_EXT_CACHE_NO; |
| 1716 | } |
| 1717 | |
| 1718 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1719 | * ext4_ext_rm_idx: |
| 1720 | * removes index from the index block. |
| 1721 | * It's used in truncate case only, thus all requests are for |
| 1722 | * last index in the block only. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1723 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1724 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1725 | struct ext4_ext_path *path) |
| 1726 | { |
| 1727 | struct buffer_head *bh; |
| 1728 | int err; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1729 | ext4_fsblk_t leaf; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1730 | |
| 1731 | /* free index block */ |
| 1732 | path--; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1733 | leaf = idx_pblock(path->p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1734 | BUG_ON(path->p_hdr->eh_entries == 0); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1735 | err = ext4_ext_get_access(handle, inode, path); |
| 1736 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1737 | return err; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1738 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1739 | err = ext4_ext_dirty(handle, inode, path); |
| 1740 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1741 | return err; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1742 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1743 | bh = sb_find_get_block(inode->i_sb, leaf); |
| 1744 | ext4_forget(handle, 1, inode, bh, leaf); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1745 | ext4_free_blocks(handle, inode, leaf, 1, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1746 | return err; |
| 1747 | } |
| 1748 | |
| 1749 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1750 | * ext4_ext_calc_credits_for_insert: |
| 1751 | * This routine returns max. credits that the extent tree can consume. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1752 | * It should be OK for low-performance paths like ->writepage() |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1753 | * To allow many writing processes to fit into a single transaction, |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 1754 | * the caller should calculate credits under i_data_sem and |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1755 | * pass the actual path. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1756 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1757 | int ext4_ext_calc_credits_for_insert(struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1758 | struct ext4_ext_path *path) |
| 1759 | { |
| 1760 | int depth, needed; |
| 1761 | |
| 1762 | if (path) { |
| 1763 | /* probably there is space in leaf? */ |
| 1764 | depth = ext_depth(inode); |
| 1765 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
| 1766 | < le16_to_cpu(path[depth].p_hdr->eh_max)) |
| 1767 | return 1; |
| 1768 | } |
| 1769 | |
| 1770 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1771 | * given 32-bit logical block (4294967296 blocks), max. tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1772 | * can be 4 levels in depth -- 4 * 340^4 == 53453440000. |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1773 | * Let's also add one more level for imbalance. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1774 | */ |
| 1775 | depth = 5; |
| 1776 | |
| 1777 | /* allocation of new data block(s) */ |
| 1778 | needed = 2; |
| 1779 | |
| 1780 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1781 | * tree can be full, so it would need to grow in depth: |
Johann Lombardi | feb1892 | 2006-12-06 20:40:46 -0800 | [diff] [blame] | 1782 | * we need one credit to modify old root, credits for |
| 1783 | * new root will be added in split accounting |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1784 | */ |
Johann Lombardi | feb1892 | 2006-12-06 20:40:46 -0800 | [diff] [blame] | 1785 | needed += 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1786 | |
| 1787 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1788 | * Index split can happen, we would need: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1789 | * allocate intermediate indexes (bitmap + group) |
| 1790 | * + change two blocks at each level, but root (already included) |
| 1791 | */ |
Johann Lombardi | feb1892 | 2006-12-06 20:40:46 -0800 | [diff] [blame] | 1792 | needed += (depth * 2) + (depth * 2); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1793 | |
| 1794 | /* any allocation modifies superblock */ |
| 1795 | needed += 1; |
| 1796 | |
| 1797 | return needed; |
| 1798 | } |
| 1799 | |
| 1800 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
| 1801 | struct ext4_extent *ex, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1802 | ext4_lblk_t from, ext4_lblk_t to) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1803 | { |
| 1804 | struct buffer_head *bh; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1805 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1806 | int i, metadata = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1807 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1808 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 1809 | metadata = 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1810 | #ifdef EXTENTS_STATS |
| 1811 | { |
| 1812 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1813 | spin_lock(&sbi->s_ext_stats_lock); |
| 1814 | sbi->s_ext_blocks += ee_len; |
| 1815 | sbi->s_ext_extents++; |
| 1816 | if (ee_len < sbi->s_ext_min) |
| 1817 | sbi->s_ext_min = ee_len; |
| 1818 | if (ee_len > sbi->s_ext_max) |
| 1819 | sbi->s_ext_max = ee_len; |
| 1820 | if (ext_depth(inode) > sbi->s_depth_max) |
| 1821 | sbi->s_depth_max = ext_depth(inode); |
| 1822 | spin_unlock(&sbi->s_ext_stats_lock); |
| 1823 | } |
| 1824 | #endif |
| 1825 | if (from >= le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1826 | && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1827 | /* tail removal */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1828 | ext4_lblk_t num; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1829 | ext4_fsblk_t start; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1830 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1831 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
| 1832 | start = ext_pblock(ex) + ee_len - num; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1833 | ext_debug("free last %u blocks starting %llu\n", num, start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1834 | for (i = 0; i < num; i++) { |
| 1835 | bh = sb_find_get_block(inode->i_sb, start + i); |
| 1836 | ext4_forget(handle, 0, inode, bh, start + i); |
| 1837 | } |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 1838 | ext4_free_blocks(handle, inode, start, num, metadata); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1839 | } else if (from == le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1840 | && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1841 | printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1842 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1843 | } else { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1844 | printk(KERN_INFO "strange request: removal(2) " |
| 1845 | "%u-%u from %u:%u\n", |
| 1846 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1847 | } |
| 1848 | return 0; |
| 1849 | } |
| 1850 | |
| 1851 | static int |
| 1852 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1853 | struct ext4_ext_path *path, ext4_lblk_t start) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1854 | { |
| 1855 | int err = 0, correct_index = 0; |
| 1856 | int depth = ext_depth(inode), credits; |
| 1857 | struct ext4_extent_header *eh; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1858 | ext4_lblk_t a, b, block; |
| 1859 | unsigned num; |
| 1860 | ext4_lblk_t ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1861 | unsigned short ex_ee_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1862 | unsigned uninitialized = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1863 | struct ext4_extent *ex; |
| 1864 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 1865 | /* the header must be checked already in ext4_ext_remove_space() */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1866 | ext_debug("truncate since %u in leaf\n", start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1867 | if (!path[depth].p_hdr) |
| 1868 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 1869 | eh = path[depth].p_hdr; |
| 1870 | BUG_ON(eh == NULL); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1871 | |
| 1872 | /* find where to start removing */ |
| 1873 | ex = EXT_LAST_EXTENT(eh); |
| 1874 | |
| 1875 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1876 | if (ext4_ext_is_uninitialized(ex)) |
| 1877 | uninitialized = 1; |
| 1878 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1879 | |
| 1880 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 1881 | ex_ee_block + ex_ee_len > start) { |
| 1882 | ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len); |
| 1883 | path[depth].p_ext = ex; |
| 1884 | |
| 1885 | a = ex_ee_block > start ? ex_ee_block : start; |
| 1886 | b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? |
| 1887 | ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; |
| 1888 | |
| 1889 | ext_debug(" border %u:%u\n", a, b); |
| 1890 | |
| 1891 | if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { |
| 1892 | block = 0; |
| 1893 | num = 0; |
| 1894 | BUG(); |
| 1895 | } else if (a != ex_ee_block) { |
| 1896 | /* remove tail of the extent */ |
| 1897 | block = ex_ee_block; |
| 1898 | num = a - block; |
| 1899 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
| 1900 | /* remove head of the extent */ |
| 1901 | block = a; |
| 1902 | num = b - a; |
| 1903 | /* there is no "make a hole" API yet */ |
| 1904 | BUG(); |
| 1905 | } else { |
| 1906 | /* remove whole extent: excellent! */ |
| 1907 | block = ex_ee_block; |
| 1908 | num = 0; |
| 1909 | BUG_ON(a != ex_ee_block); |
| 1910 | BUG_ON(b != ex_ee_block + ex_ee_len - 1); |
| 1911 | } |
| 1912 | |
Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 1913 | /* |
| 1914 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 1915 | * descriptor) for each block group; assume two block |
| 1916 | * groups plus ex_ee_len/blocks_per_block_group for |
| 1917 | * the worst case |
| 1918 | */ |
| 1919 | credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1920 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 1921 | correct_index = 1; |
| 1922 | credits += (ext_depth(inode)) + 1; |
| 1923 | } |
| 1924 | #ifdef CONFIG_QUOTA |
| 1925 | credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); |
| 1926 | #endif |
| 1927 | |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1928 | err = ext4_ext_journal_restart(handle, credits); |
| 1929 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1930 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1931 | |
| 1932 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1933 | if (err) |
| 1934 | goto out; |
| 1935 | |
| 1936 | err = ext4_remove_blocks(handle, inode, ex, a, b); |
| 1937 | if (err) |
| 1938 | goto out; |
| 1939 | |
| 1940 | if (num == 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1941 | /* this extent is removed; mark slot entirely unused */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1942 | ext4_ext_store_pblock(ex, 0); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1943 | le16_add_cpu(&eh->eh_entries, -1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | ex->ee_block = cpu_to_le32(block); |
| 1947 | ex->ee_len = cpu_to_le16(num); |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1948 | /* |
| 1949 | * Do not mark uninitialized if all the blocks in the |
| 1950 | * extent have been removed. |
| 1951 | */ |
| 1952 | if (uninitialized && num) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1953 | ext4_ext_mark_uninitialized(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1954 | |
| 1955 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1956 | if (err) |
| 1957 | goto out; |
| 1958 | |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1959 | ext_debug("new extent: %u:%u:%llu\n", block, num, |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1960 | ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1961 | ex--; |
| 1962 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1963 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | if (correct_index && eh->eh_entries) |
| 1967 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 1968 | |
| 1969 | /* if this leaf is free, then we should |
| 1970 | * remove it from index block above */ |
| 1971 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
| 1972 | err = ext4_ext_rm_idx(handle, inode, path + depth); |
| 1973 | |
| 1974 | out: |
| 1975 | return err; |
| 1976 | } |
| 1977 | |
| 1978 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1979 | * ext4_ext_more_to_rm: |
| 1980 | * returns 1 if current index has to be freed (even partial) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1981 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1982 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1983 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 1984 | { |
| 1985 | BUG_ON(path->p_idx == NULL); |
| 1986 | |
| 1987 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 1988 | return 0; |
| 1989 | |
| 1990 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1991 | * if truncate on deeper level happened, it wasn't partial, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1992 | * so we have to consider current index for truncation |
| 1993 | */ |
| 1994 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 1995 | return 0; |
| 1996 | return 1; |
| 1997 | } |
| 1998 | |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1999 | static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2000 | { |
| 2001 | struct super_block *sb = inode->i_sb; |
| 2002 | int depth = ext_depth(inode); |
| 2003 | struct ext4_ext_path *path; |
| 2004 | handle_t *handle; |
| 2005 | int i = 0, err = 0; |
| 2006 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2007 | ext_debug("truncate since %u\n", start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2008 | |
| 2009 | /* probably first extent we're gonna free will be last in block */ |
| 2010 | handle = ext4_journal_start(inode, depth + 1); |
| 2011 | if (IS_ERR(handle)) |
| 2012 | return PTR_ERR(handle); |
| 2013 | |
| 2014 | ext4_ext_invalidate_cache(inode); |
| 2015 | |
| 2016 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2017 | * We start scanning from right side, freeing all the blocks |
| 2018 | * after i_size and walking into the tree depth-wise. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2019 | */ |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 2020 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2021 | if (path == NULL) { |
| 2022 | ext4_journal_stop(handle); |
| 2023 | return -ENOMEM; |
| 2024 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2025 | path[0].p_hdr = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2026 | if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2027 | err = -EIO; |
| 2028 | goto out; |
| 2029 | } |
| 2030 | path[0].p_depth = depth; |
| 2031 | |
| 2032 | while (i >= 0 && err == 0) { |
| 2033 | if (i == depth) { |
| 2034 | /* this is leaf block */ |
| 2035 | err = ext4_ext_rm_leaf(handle, inode, path, start); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2036 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2037 | brelse(path[i].p_bh); |
| 2038 | path[i].p_bh = NULL; |
| 2039 | i--; |
| 2040 | continue; |
| 2041 | } |
| 2042 | |
| 2043 | /* this is index block */ |
| 2044 | if (!path[i].p_hdr) { |
| 2045 | ext_debug("initialize header\n"); |
| 2046 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2049 | if (!path[i].p_idx) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2050 | /* this level hasn't been touched yet */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2051 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 2052 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
| 2053 | ext_debug("init index ptr: hdr 0x%p, num %d\n", |
| 2054 | path[i].p_hdr, |
| 2055 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 2056 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2057 | /* we were already here, see at next index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2058 | path[i].p_idx--; |
| 2059 | } |
| 2060 | |
| 2061 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
| 2062 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 2063 | path[i].p_idx); |
| 2064 | if (ext4_ext_more_to_rm(path + i)) { |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2065 | struct buffer_head *bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2066 | /* go to the next level */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2067 | ext_debug("move to level %d (block %llu)\n", |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2068 | i + 1, idx_pblock(path[i].p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2069 | memset(path + i + 1, 0, sizeof(*path)); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2070 | bh = sb_bread(sb, idx_pblock(path[i].p_idx)); |
| 2071 | if (!bh) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2072 | /* should we reset i_size? */ |
| 2073 | err = -EIO; |
| 2074 | break; |
| 2075 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2076 | if (WARN_ON(i + 1 > depth)) { |
| 2077 | err = -EIO; |
| 2078 | break; |
| 2079 | } |
| 2080 | if (ext4_ext_check_header(inode, ext_block_hdr(bh), |
| 2081 | depth - i - 1)) { |
| 2082 | err = -EIO; |
| 2083 | break; |
| 2084 | } |
| 2085 | path[i + 1].p_bh = bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2086 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2087 | /* save actual number of indexes since this |
| 2088 | * number is changed at the next iteration */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2089 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 2090 | i++; |
| 2091 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2092 | /* we finished processing this index, go up */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2093 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2094 | /* index is empty, remove it; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2095 | * handle must be already prepared by the |
| 2096 | * truncatei_leaf() */ |
| 2097 | err = ext4_ext_rm_idx(handle, inode, path + i); |
| 2098 | } |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2099 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2100 | brelse(path[i].p_bh); |
| 2101 | path[i].p_bh = NULL; |
| 2102 | i--; |
| 2103 | ext_debug("return to level %d\n", i); |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | /* TODO: flexible tree reduction should be here */ |
| 2108 | if (path->p_hdr->eh_entries == 0) { |
| 2109 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2110 | * truncate to zero freed all the tree, |
| 2111 | * so we need to correct eh_depth |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2112 | */ |
| 2113 | err = ext4_ext_get_access(handle, inode, path); |
| 2114 | if (err == 0) { |
| 2115 | ext_inode_hdr(inode)->eh_depth = 0; |
| 2116 | ext_inode_hdr(inode)->eh_max = |
| 2117 | cpu_to_le16(ext4_ext_space_root(inode)); |
| 2118 | err = ext4_ext_dirty(handle, inode, path); |
| 2119 | } |
| 2120 | } |
| 2121 | out: |
| 2122 | ext4_ext_tree_changed(inode); |
| 2123 | ext4_ext_drop_refs(path); |
| 2124 | kfree(path); |
| 2125 | ext4_journal_stop(handle); |
| 2126 | |
| 2127 | return err; |
| 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * called at mount time |
| 2132 | */ |
| 2133 | void ext4_ext_init(struct super_block *sb) |
| 2134 | { |
| 2135 | /* |
| 2136 | * possible initialization would be here |
| 2137 | */ |
| 2138 | |
| 2139 | if (test_opt(sb, EXTENTS)) { |
| 2140 | printk("EXT4-fs: file extents enabled"); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 2141 | #ifdef AGGRESSIVE_TEST |
| 2142 | printk(", aggressive tests"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2143 | #endif |
| 2144 | #ifdef CHECK_BINSEARCH |
| 2145 | printk(", check binsearch"); |
| 2146 | #endif |
| 2147 | #ifdef EXTENTS_STATS |
| 2148 | printk(", stats"); |
| 2149 | #endif |
| 2150 | printk("\n"); |
| 2151 | #ifdef EXTENTS_STATS |
| 2152 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 2153 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 2154 | EXT4_SB(sb)->s_ext_max = 0; |
| 2155 | #endif |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * called at umount time |
| 2161 | */ |
| 2162 | void ext4_ext_release(struct super_block *sb) |
| 2163 | { |
| 2164 | if (!test_opt(sb, EXTENTS)) |
| 2165 | return; |
| 2166 | |
| 2167 | #ifdef EXTENTS_STATS |
| 2168 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 2169 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2170 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 2171 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 2172 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 2173 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 2174 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 2175 | } |
| 2176 | #endif |
| 2177 | } |
| 2178 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2179 | static void bi_complete(struct bio *bio, int error) |
| 2180 | { |
| 2181 | complete((struct completion *)bio->bi_private); |
| 2182 | } |
| 2183 | |
| 2184 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 2185 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 2186 | { |
| 2187 | int ret = -EIO; |
| 2188 | struct bio *bio; |
| 2189 | int blkbits, blocksize; |
| 2190 | sector_t ee_pblock; |
| 2191 | struct completion event; |
| 2192 | unsigned int ee_len, len, done, offset; |
| 2193 | |
| 2194 | |
| 2195 | blkbits = inode->i_blkbits; |
| 2196 | blocksize = inode->i_sb->s_blocksize; |
| 2197 | ee_len = ext4_ext_get_actual_len(ex); |
| 2198 | ee_pblock = ext_pblock(ex); |
| 2199 | |
| 2200 | /* convert ee_pblock to 512 byte sectors */ |
| 2201 | ee_pblock = ee_pblock << (blkbits - 9); |
| 2202 | |
| 2203 | while (ee_len > 0) { |
| 2204 | |
| 2205 | if (ee_len > BIO_MAX_PAGES) |
| 2206 | len = BIO_MAX_PAGES; |
| 2207 | else |
| 2208 | len = ee_len; |
| 2209 | |
| 2210 | bio = bio_alloc(GFP_NOIO, len); |
| 2211 | if (!bio) |
| 2212 | return -ENOMEM; |
| 2213 | bio->bi_sector = ee_pblock; |
| 2214 | bio->bi_bdev = inode->i_sb->s_bdev; |
| 2215 | |
| 2216 | done = 0; |
| 2217 | offset = 0; |
| 2218 | while (done < len) { |
| 2219 | ret = bio_add_page(bio, ZERO_PAGE(0), |
| 2220 | blocksize, offset); |
| 2221 | if (ret != blocksize) { |
| 2222 | /* |
| 2223 | * We can't add any more pages because of |
| 2224 | * hardware limitations. Start a new bio. |
| 2225 | */ |
| 2226 | break; |
| 2227 | } |
| 2228 | done++; |
| 2229 | offset += blocksize; |
| 2230 | if (offset >= PAGE_CACHE_SIZE) |
| 2231 | offset = 0; |
| 2232 | } |
| 2233 | |
| 2234 | init_completion(&event); |
| 2235 | bio->bi_private = &event; |
| 2236 | bio->bi_end_io = bi_complete; |
| 2237 | submit_bio(WRITE, bio); |
| 2238 | wait_for_completion(&event); |
| 2239 | |
| 2240 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 2241 | ret = 0; |
| 2242 | else { |
| 2243 | ret = -EIO; |
| 2244 | break; |
| 2245 | } |
| 2246 | bio_put(bio); |
| 2247 | ee_len -= done; |
| 2248 | ee_pblock += done << (blkbits - 9); |
| 2249 | } |
| 2250 | return ret; |
| 2251 | } |
| 2252 | |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2253 | #define EXT4_EXT_ZERO_LEN 7 |
| 2254 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2255 | /* |
| 2256 | * This function is called by ext4_ext_get_blocks() if someone tries to write |
| 2257 | * to an uninitialized extent. It may result in splitting the uninitialized |
| 2258 | * extent into multiple extents (upto three - one initialized and two |
| 2259 | * uninitialized). |
| 2260 | * There are three possibilities: |
| 2261 | * a> There is no split required: Entire extent should be initialized |
| 2262 | * b> Splits in two extents: Write is happening at either end of the extent |
| 2263 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 2264 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2265 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
| 2266 | struct inode *inode, |
| 2267 | struct ext4_ext_path *path, |
| 2268 | ext4_lblk_t iblock, |
| 2269 | unsigned long max_blocks) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2270 | { |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2271 | struct ext4_extent *ex, newex, orig_ex; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2272 | struct ext4_extent *ex1 = NULL; |
| 2273 | struct ext4_extent *ex2 = NULL; |
| 2274 | struct ext4_extent *ex3 = NULL; |
| 2275 | struct ext4_extent_header *eh; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2276 | ext4_lblk_t ee_block; |
| 2277 | unsigned int allocated, ee_len, depth; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2278 | ext4_fsblk_t newblock; |
| 2279 | int err = 0; |
| 2280 | int ret = 0; |
| 2281 | |
| 2282 | depth = ext_depth(inode); |
| 2283 | eh = path[depth].p_hdr; |
| 2284 | ex = path[depth].p_ext; |
| 2285 | ee_block = le32_to_cpu(ex->ee_block); |
| 2286 | ee_len = ext4_ext_get_actual_len(ex); |
| 2287 | allocated = ee_len - (iblock - ee_block); |
| 2288 | newblock = iblock - ee_block + ext_pblock(ex); |
| 2289 | ex2 = ex; |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2290 | orig_ex.ee_block = ex->ee_block; |
| 2291 | orig_ex.ee_len = cpu_to_le16(ee_len); |
| 2292 | ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2293 | |
Aneesh Kumar K.V | 9df5643 | 2008-02-22 06:17:31 -0500 | [diff] [blame] | 2294 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2295 | if (err) |
| 2296 | goto out; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2297 | /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ |
| 2298 | if (ee_len <= 2*EXT4_EXT_ZERO_LEN) { |
| 2299 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2300 | if (err) |
| 2301 | goto fix_extent_len; |
| 2302 | /* update the extent length and mark as initialized */ |
| 2303 | ex->ee_block = orig_ex.ee_block; |
| 2304 | ex->ee_len = orig_ex.ee_len; |
| 2305 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2306 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2307 | /* zeroed the full extent */ |
| 2308 | return allocated; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2309 | } |
Aneesh Kumar K.V | 9df5643 | 2008-02-22 06:17:31 -0500 | [diff] [blame] | 2310 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2311 | /* ex1: ee_block to iblock - 1 : uninitialized */ |
| 2312 | if (iblock > ee_block) { |
| 2313 | ex1 = ex; |
| 2314 | ex1->ee_len = cpu_to_le16(iblock - ee_block); |
| 2315 | ext4_ext_mark_uninitialized(ex1); |
| 2316 | ex2 = &newex; |
| 2317 | } |
| 2318 | /* |
| 2319 | * for sanity, update the length of the ex2 extent before |
| 2320 | * we insert ex3, if ex1 is NULL. This is to avoid temporary |
| 2321 | * overlap of blocks. |
| 2322 | */ |
| 2323 | if (!ex1 && allocated > max_blocks) |
| 2324 | ex2->ee_len = cpu_to_le16(max_blocks); |
| 2325 | /* ex3: to ee_block + ee_len : uninitialised */ |
| 2326 | if (allocated > max_blocks) { |
| 2327 | unsigned int newdepth; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2328 | /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ |
| 2329 | if (allocated <= EXT4_EXT_ZERO_LEN) { |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2330 | /* |
| 2331 | * iblock == ee_block is handled by the zerouout |
| 2332 | * at the beginning. |
| 2333 | * Mark first half uninitialized. |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2334 | * Mark second half initialized and zero out the |
| 2335 | * initialized extent |
| 2336 | */ |
| 2337 | ex->ee_block = orig_ex.ee_block; |
| 2338 | ex->ee_len = cpu_to_le16(ee_len - allocated); |
| 2339 | ext4_ext_mark_uninitialized(ex); |
| 2340 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2341 | ext4_ext_dirty(handle, inode, path + depth); |
| 2342 | |
| 2343 | ex3 = &newex; |
| 2344 | ex3->ee_block = cpu_to_le32(iblock); |
| 2345 | ext4_ext_store_pblock(ex3, newblock); |
| 2346 | ex3->ee_len = cpu_to_le16(allocated); |
| 2347 | err = ext4_ext_insert_extent(handle, inode, path, ex3); |
| 2348 | if (err == -ENOSPC) { |
| 2349 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2350 | if (err) |
| 2351 | goto fix_extent_len; |
| 2352 | ex->ee_block = orig_ex.ee_block; |
| 2353 | ex->ee_len = orig_ex.ee_len; |
| 2354 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2355 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2356 | /* blocks available from iblock */ |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2357 | return allocated; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2358 | |
| 2359 | } else if (err) |
| 2360 | goto fix_extent_len; |
| 2361 | |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2362 | /* |
| 2363 | * We need to zero out the second half because |
| 2364 | * an fallocate request can update file size and |
| 2365 | * converting the second half to initialized extent |
| 2366 | * implies that we can leak some junk data to user |
| 2367 | * space. |
| 2368 | */ |
| 2369 | err = ext4_ext_zeroout(inode, ex3); |
| 2370 | if (err) { |
| 2371 | /* |
| 2372 | * We should actually mark the |
| 2373 | * second half as uninit and return error |
| 2374 | * Insert would have changed the extent |
| 2375 | */ |
| 2376 | depth = ext_depth(inode); |
| 2377 | ext4_ext_drop_refs(path); |
| 2378 | path = ext4_ext_find_extent(inode, |
| 2379 | iblock, path); |
| 2380 | if (IS_ERR(path)) { |
| 2381 | err = PTR_ERR(path); |
| 2382 | return err; |
| 2383 | } |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2384 | /* get the second half extent details */ |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2385 | ex = path[depth].p_ext; |
| 2386 | err = ext4_ext_get_access(handle, inode, |
| 2387 | path + depth); |
| 2388 | if (err) |
| 2389 | return err; |
| 2390 | ext4_ext_mark_uninitialized(ex); |
| 2391 | ext4_ext_dirty(handle, inode, path + depth); |
| 2392 | return err; |
| 2393 | } |
| 2394 | |
| 2395 | /* zeroed the second half */ |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2396 | return allocated; |
| 2397 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2398 | ex3 = &newex; |
| 2399 | ex3->ee_block = cpu_to_le32(iblock + max_blocks); |
| 2400 | ext4_ext_store_pblock(ex3, newblock + max_blocks); |
| 2401 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); |
| 2402 | ext4_ext_mark_uninitialized(ex3); |
| 2403 | err = ext4_ext_insert_extent(handle, inode, path, ex3); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2404 | if (err == -ENOSPC) { |
| 2405 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2406 | if (err) |
| 2407 | goto fix_extent_len; |
| 2408 | /* update the extent length and mark as initialized */ |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2409 | ex->ee_block = orig_ex.ee_block; |
| 2410 | ex->ee_len = orig_ex.ee_len; |
| 2411 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2412 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2413 | /* zeroed the full extent */ |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2414 | /* blocks available from iblock */ |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2415 | return allocated; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2416 | |
| 2417 | } else if (err) |
| 2418 | goto fix_extent_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2419 | /* |
| 2420 | * The depth, and hence eh & ex might change |
| 2421 | * as part of the insert above. |
| 2422 | */ |
| 2423 | newdepth = ext_depth(inode); |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2424 | /* |
| 2425 | * update the extent length after successfull insert of the |
| 2426 | * split extent |
| 2427 | */ |
| 2428 | orig_ex.ee_len = cpu_to_le16(ee_len - |
| 2429 | ext4_ext_get_actual_len(ex3)); |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2430 | depth = newdepth; |
| 2431 | ext4_ext_drop_refs(path); |
| 2432 | path = ext4_ext_find_extent(inode, iblock, path); |
| 2433 | if (IS_ERR(path)) { |
| 2434 | err = PTR_ERR(path); |
| 2435 | goto out; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2436 | } |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2437 | eh = path[depth].p_hdr; |
| 2438 | ex = path[depth].p_ext; |
| 2439 | if (ex2 != &newex) |
| 2440 | ex2 = ex; |
| 2441 | |
| 2442 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2443 | if (err) |
| 2444 | goto out; |
| 2445 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2446 | allocated = max_blocks; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2447 | |
| 2448 | /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying |
| 2449 | * to insert a extent in the middle zerout directly |
| 2450 | * otherwise give the extent a chance to merge to left |
| 2451 | */ |
| 2452 | if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && |
| 2453 | iblock != ee_block) { |
| 2454 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2455 | if (err) |
| 2456 | goto fix_extent_len; |
| 2457 | /* update the extent length and mark as initialized */ |
| 2458 | ex->ee_block = orig_ex.ee_block; |
| 2459 | ex->ee_len = orig_ex.ee_len; |
| 2460 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2461 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2462 | /* zero out the first half */ |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 2463 | /* blocks available from iblock */ |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2464 | return allocated; |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2465 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2466 | } |
| 2467 | /* |
| 2468 | * If there was a change of depth as part of the |
| 2469 | * insertion of ex3 above, we need to update the length |
| 2470 | * of the ex1 extent again here |
| 2471 | */ |
| 2472 | if (ex1 && ex1 != ex) { |
| 2473 | ex1 = ex; |
| 2474 | ex1->ee_len = cpu_to_le16(iblock - ee_block); |
| 2475 | ext4_ext_mark_uninitialized(ex1); |
| 2476 | ex2 = &newex; |
| 2477 | } |
| 2478 | /* ex2: iblock to iblock + maxblocks-1 : initialised */ |
| 2479 | ex2->ee_block = cpu_to_le32(iblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2480 | ext4_ext_store_pblock(ex2, newblock); |
| 2481 | ex2->ee_len = cpu_to_le16(allocated); |
| 2482 | if (ex2 != ex) |
| 2483 | goto insert; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2484 | /* |
| 2485 | * New (initialized) extent starts from the first block |
| 2486 | * in the current extent. i.e., ex2 == ex |
| 2487 | * We have to see if it can be merged with the extent |
| 2488 | * on the left. |
| 2489 | */ |
| 2490 | if (ex2 > EXT_FIRST_EXTENT(eh)) { |
| 2491 | /* |
| 2492 | * To merge left, pass "ex2 - 1" to try_to_merge(), |
| 2493 | * since it merges towards right _only_. |
| 2494 | */ |
| 2495 | ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); |
| 2496 | if (ret) { |
| 2497 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2498 | if (err) |
| 2499 | goto out; |
| 2500 | depth = ext_depth(inode); |
| 2501 | ex2--; |
| 2502 | } |
| 2503 | } |
| 2504 | /* |
| 2505 | * Try to Merge towards right. This might be required |
| 2506 | * only when the whole extent is being written to. |
| 2507 | * i.e. ex2 == ex and ex3 == NULL. |
| 2508 | */ |
| 2509 | if (!ex3) { |
| 2510 | ret = ext4_ext_try_to_merge(inode, path, ex2); |
| 2511 | if (ret) { |
| 2512 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2513 | if (err) |
| 2514 | goto out; |
| 2515 | } |
| 2516 | } |
| 2517 | /* Mark modified extent as dirty */ |
| 2518 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2519 | goto out; |
| 2520 | insert: |
| 2521 | err = ext4_ext_insert_extent(handle, inode, path, &newex); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2522 | if (err == -ENOSPC) { |
| 2523 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2524 | if (err) |
| 2525 | goto fix_extent_len; |
| 2526 | /* update the extent length and mark as initialized */ |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2527 | ex->ee_block = orig_ex.ee_block; |
| 2528 | ex->ee_len = orig_ex.ee_len; |
| 2529 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
Aneesh Kumar K.V | 95c3889 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2530 | ext4_ext_dirty(handle, inode, path + depth); |
Aneesh Kumar K.V | 161e7b7 | 2008-04-29 22:03:59 -0400 | [diff] [blame] | 2531 | /* zero out the first half */ |
| 2532 | return allocated; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2533 | } else if (err) |
| 2534 | goto fix_extent_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2535 | out: |
| 2536 | return err ? err : allocated; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2537 | |
| 2538 | fix_extent_len: |
| 2539 | ex->ee_block = orig_ex.ee_block; |
| 2540 | ex->ee_len = orig_ex.ee_len; |
| 2541 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); |
| 2542 | ext4_ext_mark_uninitialized(ex); |
| 2543 | ext4_ext_dirty(handle, inode, path + depth); |
| 2544 | return err; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2545 | } |
| 2546 | |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2547 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 2548 | * Block allocation/map/preallocation routine for extents based files |
| 2549 | * |
| 2550 | * |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2551 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 2552 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 2553 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 2554 | * |
| 2555 | * return > 0, number of of blocks already mapped/allocated |
| 2556 | * if create == 0 and these are pre-allocated blocks |
| 2557 | * buffer head is unmapped |
| 2558 | * otherwise blocks are mapped |
| 2559 | * |
| 2560 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 2561 | * buffer head is unmapped |
| 2562 | * |
| 2563 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2564 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2565 | int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2566 | ext4_lblk_t iblock, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2567 | unsigned long max_blocks, struct buffer_head *bh_result, |
| 2568 | int create, int extend_disksize) |
| 2569 | { |
| 2570 | struct ext4_ext_path *path = NULL; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2571 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2572 | struct ext4_extent newex, *ex; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2573 | ext4_fsblk_t goal, newblock; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2574 | int err = 0, depth, ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2575 | unsigned long allocated = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2576 | struct ext4_allocation_request ar; |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2577 | loff_t disksize; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2578 | |
| 2579 | __clear_bit(BH_New, &bh_result->b_state); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2580 | ext_debug("blocks %u/%lu requested for inode %u\n", |
| 2581 | iblock, max_blocks, inode->i_ino); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2582 | |
| 2583 | /* check in cache */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2584 | goal = ext4_ext_in_cache(inode, iblock, &newex); |
| 2585 | if (goal) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2586 | if (goal == EXT4_EXT_CACHE_GAP) { |
| 2587 | if (!create) { |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2588 | /* |
| 2589 | * block isn't allocated yet and |
| 2590 | * user doesn't want to allocate it |
| 2591 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2592 | goto out2; |
| 2593 | } |
| 2594 | /* we should allocate requested block */ |
| 2595 | } else if (goal == EXT4_EXT_CACHE_EXTENT) { |
| 2596 | /* block is already allocated */ |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2597 | newblock = iblock |
| 2598 | - le32_to_cpu(newex.ee_block) |
| 2599 | + ext_pblock(&newex); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2600 | /* number of remaining blocks in the extent */ |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2601 | allocated = ext4_ext_get_actual_len(&newex) - |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2602 | (iblock - le32_to_cpu(newex.ee_block)); |
| 2603 | goto out; |
| 2604 | } else { |
| 2605 | BUG(); |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | /* find extent for this block */ |
| 2610 | path = ext4_ext_find_extent(inode, iblock, NULL); |
| 2611 | if (IS_ERR(path)) { |
| 2612 | err = PTR_ERR(path); |
| 2613 | path = NULL; |
| 2614 | goto out2; |
| 2615 | } |
| 2616 | |
| 2617 | depth = ext_depth(inode); |
| 2618 | |
| 2619 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2620 | * consistent leaf must not be empty; |
| 2621 | * this situation is possible, though, _during_ tree modification; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2622 | * this is why assert can't be put in ext4_ext_find_extent() |
| 2623 | */ |
| 2624 | BUG_ON(path[depth].p_ext == NULL && depth != 0); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2625 | eh = path[depth].p_hdr; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2626 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2627 | ex = path[depth].p_ext; |
| 2628 | if (ex) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2629 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2630 | ext4_fsblk_t ee_start = ext_pblock(ex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2631 | unsigned short ee_len; |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 2632 | |
| 2633 | /* |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 2634 | * Uninitialized extents are treated as holes, except that |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2635 | * we split out initialized portions during a write. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 2636 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2637 | ee_len = ext4_ext_get_actual_len(ex); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2638 | /* if found extent covers block, simply return it */ |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2639 | if (iblock >= ee_block && iblock < ee_block + ee_len) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2640 | newblock = iblock - ee_block + ee_start; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2641 | /* number of remaining blocks in the extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2642 | allocated = ee_len - (iblock - ee_block); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2643 | ext_debug("%u fit into %lu:%d -> %llu\n", iblock, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2644 | ee_block, ee_len, newblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2645 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2646 | /* Do not put uninitialized extent in the cache */ |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2647 | if (!ext4_ext_is_uninitialized(ex)) { |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2648 | ext4_ext_put_in_cache(inode, ee_block, |
| 2649 | ee_len, ee_start, |
| 2650 | EXT4_EXT_CACHE_EXTENT); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2651 | goto out; |
| 2652 | } |
| 2653 | if (create == EXT4_CREATE_UNINITIALIZED_EXT) |
| 2654 | goto out; |
Aneesh Kumar K.V | e067ba0 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2655 | if (!create) { |
| 2656 | /* |
| 2657 | * We have blocks reserved already. We |
| 2658 | * return allocated blocks so that delalloc |
| 2659 | * won't do block reservation for us. But |
| 2660 | * the buffer head will be unmapped so that |
| 2661 | * a read from the block returns 0s. |
| 2662 | */ |
| 2663 | if (allocated > max_blocks) |
| 2664 | allocated = max_blocks; |
Eric Sandeen | 953e622 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2665 | set_buffer_unwritten(bh_result); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2666 | goto out2; |
Aneesh Kumar K.V | e067ba0 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2667 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2668 | |
| 2669 | ret = ext4_ext_convert_to_initialized(handle, inode, |
| 2670 | path, iblock, |
| 2671 | max_blocks); |
Dmitry Monakhov | dbf9d7d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2672 | if (ret <= 0) { |
| 2673 | err = ret; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2674 | goto out2; |
Dmitry Monakhov | dbf9d7d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2675 | } else |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2676 | allocated = ret; |
| 2677 | goto outnew; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2682 | * requested block isn't allocated yet; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2683 | * we couldn't try to create block if create flag is zero |
| 2684 | */ |
| 2685 | if (!create) { |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2686 | /* |
| 2687 | * put just found gap into cache to speed up |
| 2688 | * subsequent requests |
| 2689 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2690 | ext4_ext_put_gap_in_cache(inode, path, iblock); |
| 2691 | goto out2; |
| 2692 | } |
| 2693 | /* |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 2694 | * Okay, we need to do block allocation. Lazily initialize the block |
| 2695 | * allocation info here if necessary. |
| 2696 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2697 | if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info)) |
| 2698 | ext4_init_block_alloc_info(inode); |
| 2699 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2700 | /* find neighbour allocated blocks */ |
| 2701 | ar.lleft = iblock; |
| 2702 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 2703 | if (err) |
| 2704 | goto out2; |
| 2705 | ar.lright = iblock; |
| 2706 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); |
| 2707 | if (err) |
| 2708 | goto out2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 2709 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2710 | /* |
| 2711 | * See if request is beyond maximum number of blocks we can have in |
| 2712 | * a single extent. For an initialized extent this limit is |
| 2713 | * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is |
| 2714 | * EXT_UNINIT_MAX_LEN. |
| 2715 | */ |
| 2716 | if (max_blocks > EXT_INIT_MAX_LEN && |
| 2717 | create != EXT4_CREATE_UNINITIALIZED_EXT) |
| 2718 | max_blocks = EXT_INIT_MAX_LEN; |
| 2719 | else if (max_blocks > EXT_UNINIT_MAX_LEN && |
| 2720 | create == EXT4_CREATE_UNINITIALIZED_EXT) |
| 2721 | max_blocks = EXT_UNINIT_MAX_LEN; |
| 2722 | |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 2723 | /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */ |
| 2724 | newex.ee_block = cpu_to_le32(iblock); |
| 2725 | newex.ee_len = cpu_to_le16(max_blocks); |
| 2726 | err = ext4_ext_check_overlap(inode, &newex, path); |
| 2727 | if (err) |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2728 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 2729 | else |
| 2730 | allocated = max_blocks; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2731 | |
| 2732 | /* allocate new block */ |
| 2733 | ar.inode = inode; |
| 2734 | ar.goal = ext4_ext_find_goal(inode, path, iblock); |
| 2735 | ar.logical = iblock; |
| 2736 | ar.len = allocated; |
| 2737 | if (S_ISREG(inode->i_mode)) |
| 2738 | ar.flags = EXT4_MB_HINT_DATA; |
| 2739 | else |
| 2740 | /* disable in-core preallocation for non-regular files */ |
| 2741 | ar.flags = 0; |
| 2742 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2743 | if (!newblock) |
| 2744 | goto out2; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2745 | ext_debug("allocate new block: goal %llu, found %llu/%lu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2746 | goal, newblock, allocated); |
| 2747 | |
| 2748 | /* try to insert new extent into found leaf and return */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2749 | ext4_ext_store_pblock(&newex, newblock); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2750 | newex.ee_len = cpu_to_le16(ar.len); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2751 | if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */ |
| 2752 | ext4_ext_mark_uninitialized(&newex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2753 | err = ext4_ext_insert_extent(handle, inode, path, &newex); |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 2754 | if (err) { |
| 2755 | /* free data blocks we just allocated */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2756 | /* not a good idea to call discard here directly, |
| 2757 | * but otherwise we'd need to call it every free() */ |
| 2758 | ext4_mb_discard_inode_preallocations(inode); |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 2759 | ext4_free_blocks(handle, inode, ext_pblock(&newex), |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2760 | ext4_ext_get_actual_len(&newex), 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2761 | goto out2; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 2762 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2763 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2764 | /* previous routine could use block we allocated */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2765 | newblock = ext_pblock(&newex); |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2766 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2767 | outnew: |
Mingming Cao | 61628a3 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2768 | if (extend_disksize) { |
| 2769 | disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits; |
| 2770 | if (disksize > i_size_read(inode)) |
| 2771 | disksize = i_size_read(inode); |
| 2772 | if (disksize > EXT4_I(inode)->i_disksize) |
| 2773 | EXT4_I(inode)->i_disksize = disksize; |
| 2774 | } |
Aneesh Kumar K.V | a379cd1 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2775 | |
Eric Sandeen | 953e622 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2776 | set_buffer_new(bh_result); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2777 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2778 | /* Cache only when it is _not_ an uninitialized extent */ |
| 2779 | if (create != EXT4_CREATE_UNINITIALIZED_EXT) |
| 2780 | ext4_ext_put_in_cache(inode, iblock, allocated, newblock, |
| 2781 | EXT4_EXT_CACHE_EXTENT); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2782 | out: |
| 2783 | if (allocated > max_blocks) |
| 2784 | allocated = max_blocks; |
| 2785 | ext4_ext_show_leaf(inode, path); |
Eric Sandeen | 953e622 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2786 | set_buffer_mapped(bh_result); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2787 | bh_result->b_bdev = inode->i_sb->s_bdev; |
| 2788 | bh_result->b_blocknr = newblock; |
| 2789 | out2: |
| 2790 | if (path) { |
| 2791 | ext4_ext_drop_refs(path); |
| 2792 | kfree(path); |
| 2793 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2794 | return err ? err : allocated; |
| 2795 | } |
| 2796 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2797 | void ext4_ext_truncate(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2798 | { |
| 2799 | struct address_space *mapping = inode->i_mapping; |
| 2800 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2801 | ext4_lblk_t last_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2802 | handle_t *handle; |
| 2803 | int err = 0; |
| 2804 | |
| 2805 | /* |
| 2806 | * probably first extent we're gonna free will be last in block |
| 2807 | */ |
| 2808 | err = ext4_writepage_trans_blocks(inode) + 3; |
| 2809 | handle = ext4_journal_start(inode, err); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2810 | if (IS_ERR(handle)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2811 | return; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2812 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2813 | if (inode->i_size & (sb->s_blocksize - 1)) |
| 2814 | ext4_block_truncate_page(handle, mapping, inode->i_size); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2815 | |
Jan Kara | 9ddfc3d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2816 | if (ext4_orphan_add(handle, inode)) |
| 2817 | goto out_stop; |
| 2818 | |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 2819 | down_write(&EXT4_I(inode)->i_data_sem); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2820 | ext4_ext_invalidate_cache(inode); |
| 2821 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2822 | ext4_mb_discard_inode_preallocations(inode); |
| 2823 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2824 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2825 | * TODO: optimization is possible here. |
| 2826 | * Probably we need not scan at all, |
| 2827 | * because page truncation is enough. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2828 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2829 | |
| 2830 | /* we have to know where to truncate from in crash case */ |
| 2831 | EXT4_I(inode)->i_disksize = inode->i_size; |
| 2832 | ext4_mark_inode_dirty(handle, inode); |
| 2833 | |
| 2834 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 2835 | >> EXT4_BLOCK_SIZE_BITS(sb); |
| 2836 | err = ext4_ext_remove_space(inode, last_block); |
| 2837 | |
| 2838 | /* In a multi-transaction truncate, we only make the final |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2839 | * transaction synchronous. |
| 2840 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2841 | if (IS_SYNC(inode)) |
| 2842 | handle->h_sync = 1; |
| 2843 | |
| 2844 | out_stop: |
Jan Kara | 9ddfc3d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2845 | up_write(&EXT4_I(inode)->i_data_sem); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2846 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2847 | * If this was a simple ftruncate() and the file will remain alive, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2848 | * then we need to clear up the orphan record which we created above. |
| 2849 | * However, if this was a real unlink then we were called by |
| 2850 | * ext4_delete_inode(), and we allow that function to clean up the |
| 2851 | * orphan info for us. |
| 2852 | */ |
| 2853 | if (inode->i_nlink) |
| 2854 | ext4_orphan_del(handle, inode); |
| 2855 | |
Solofo Ramangalahy | ef73772 | 2008-04-29 22:00:41 -0400 | [diff] [blame] | 2856 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 2857 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2858 | ext4_journal_stop(handle); |
| 2859 | } |
| 2860 | |
| 2861 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2862 | * ext4_ext_writepage_trans_blocks: |
| 2863 | * calculate max number of blocks we could modify |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2864 | * in order to allocate new block for an inode |
| 2865 | */ |
| 2866 | int ext4_ext_writepage_trans_blocks(struct inode *inode, int num) |
| 2867 | { |
| 2868 | int needed; |
| 2869 | |
| 2870 | needed = ext4_ext_calc_credits_for_insert(inode, NULL); |
| 2871 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2872 | /* caller wants to allocate num blocks, but note it includes sb */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2873 | needed = needed * num - (num - 1); |
| 2874 | |
| 2875 | #ifdef CONFIG_QUOTA |
| 2876 | needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); |
| 2877 | #endif |
| 2878 | |
| 2879 | return needed; |
| 2880 | } |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2881 | |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2882 | static void ext4_falloc_update_inode(struct inode *inode, |
| 2883 | int mode, loff_t new_size, int update_ctime) |
| 2884 | { |
| 2885 | struct timespec now; |
| 2886 | |
| 2887 | if (update_ctime) { |
| 2888 | now = current_fs_time(inode->i_sb); |
| 2889 | if (!timespec_equal(&inode->i_ctime, &now)) |
| 2890 | inode->i_ctime = now; |
| 2891 | } |
| 2892 | /* |
| 2893 | * Update only when preallocation was requested beyond |
| 2894 | * the file size. |
| 2895 | */ |
| 2896 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 2897 | new_size > i_size_read(inode)) { |
| 2898 | i_size_write(inode, new_size); |
| 2899 | EXT4_I(inode)->i_disksize = new_size; |
| 2900 | } |
| 2901 | |
| 2902 | } |
| 2903 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2904 | /* |
| 2905 | * preallocate space for a file. This implements ext4's fallocate inode |
| 2906 | * operation, which gets called from sys_fallocate system call. |
| 2907 | * For block-mapped files, posix_fallocate should fall back to the method |
| 2908 | * of writing zeroes to the required new blocks (the same behavior which is |
| 2909 | * expected for file systems which do not support fallocate() system call). |
| 2910 | */ |
| 2911 | long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) |
| 2912 | { |
| 2913 | handle_t *handle; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2914 | ext4_lblk_t block; |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2915 | loff_t new_size; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2916 | unsigned long max_blocks; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2917 | int ret = 0; |
| 2918 | int ret2 = 0; |
| 2919 | int retries = 0; |
| 2920 | struct buffer_head map_bh; |
| 2921 | unsigned int credits, blkbits = inode->i_blkbits; |
| 2922 | |
| 2923 | /* |
| 2924 | * currently supporting (pre)allocate mode for extent-based |
| 2925 | * files _only_ |
| 2926 | */ |
| 2927 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) |
| 2928 | return -EOPNOTSUPP; |
| 2929 | |
| 2930 | /* preallocation to directories is currently not supported */ |
| 2931 | if (S_ISDIR(inode->i_mode)) |
| 2932 | return -ENODEV; |
| 2933 | |
| 2934 | block = offset >> blkbits; |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2935 | /* |
| 2936 | * We can't just convert len to max_blocks because |
| 2937 | * If blocksize = 4096 offset = 3072 and len = 2048 |
| 2938 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2939 | max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2940 | - block; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2941 | /* |
| 2942 | * credits to insert 1 extent into extent tree + buffers to be able to |
| 2943 | * modify 1 super block, 1 block bitmap and 1 group descriptor. |
| 2944 | */ |
| 2945 | credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3; |
Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 2946 | mutex_lock(&inode->i_mutex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2947 | retry: |
| 2948 | while (ret >= 0 && ret < max_blocks) { |
| 2949 | block = block + ret; |
| 2950 | max_blocks = max_blocks - ret; |
| 2951 | handle = ext4_journal_start(inode, credits); |
| 2952 | if (IS_ERR(handle)) { |
| 2953 | ret = PTR_ERR(handle); |
| 2954 | break; |
| 2955 | } |
Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 2956 | ret = ext4_get_blocks_wrap(handle, inode, block, |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2957 | max_blocks, &map_bh, |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 2958 | EXT4_CREATE_UNINITIALIZED_EXT, 0, 0); |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2959 | if (ret <= 0) { |
Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 2960 | #ifdef EXT4FS_DEBUG |
| 2961 | WARN_ON(ret <= 0); |
| 2962 | printk(KERN_ERR "%s: ext4_ext_get_blocks " |
| 2963 | "returned error inode#%lu, block=%u, " |
| 2964 | "max_blocks=%lu", __func__, |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2965 | inode->i_ino, block, max_blocks); |
Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 2966 | #endif |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2967 | ext4_mark_inode_dirty(handle, inode); |
| 2968 | ret2 = ext4_journal_stop(handle); |
| 2969 | break; |
| 2970 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2971 | if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len, |
| 2972 | blkbits) >> blkbits)) |
| 2973 | new_size = offset + len; |
| 2974 | else |
| 2975 | new_size = (block + ret) << blkbits; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2976 | |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2977 | ext4_falloc_update_inode(inode, mode, new_size, |
| 2978 | buffer_new(&map_bh)); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2979 | ext4_mark_inode_dirty(handle, inode); |
| 2980 | ret2 = ext4_journal_stop(handle); |
| 2981 | if (ret2) |
| 2982 | break; |
| 2983 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2984 | if (ret == -ENOSPC && |
| 2985 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 2986 | ret = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2987 | goto retry; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2988 | } |
Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 2989 | mutex_unlock(&inode->i_mutex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2990 | return ret > 0 ? ret2 : ret; |
| 2991 | } |