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