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