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