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 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 47 | #include <trace/events/ext4.h> |
| 48 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 49 | static int ext4_split_extent(handle_t *handle, |
| 50 | struct inode *inode, |
| 51 | struct ext4_ext_path *path, |
| 52 | struct ext4_map_blocks *map, |
| 53 | int split_flag, |
| 54 | int flags); |
| 55 | |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 56 | static int ext4_ext_truncate_extend_restart(handle_t *handle, |
| 57 | struct inode *inode, |
| 58 | int needed) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 59 | { |
| 60 | int err; |
| 61 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 62 | if (!ext4_handle_valid(handle)) |
| 63 | return 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 64 | if (handle->h_buffer_credits > needed) |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 65 | return 0; |
| 66 | err = ext4_journal_extend(handle, needed); |
Theodore Ts'o | 0123c93 | 2008-08-01 20:57:54 -0400 | [diff] [blame] | 67 | if (err <= 0) |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 68 | return err; |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 69 | err = ext4_truncate_restart_trans(handle, inode, needed); |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 70 | if (err == 0) |
| 71 | err = -EAGAIN; |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 72 | |
| 73 | return err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * could return: |
| 78 | * - EROFS |
| 79 | * - ENOMEM |
| 80 | */ |
| 81 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
| 82 | struct ext4_ext_path *path) |
| 83 | { |
| 84 | if (path->p_bh) { |
| 85 | /* path points to block */ |
| 86 | return ext4_journal_get_write_access(handle, path->p_bh); |
| 87 | } |
| 88 | /* path points to leaf/index in inode body */ |
| 89 | /* we use in-core data, no need to protect them */ |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * could return: |
| 95 | * - EROFS |
| 96 | * - ENOMEM |
| 97 | * - EIO |
| 98 | */ |
Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 99 | #define ext4_ext_dirty(handle, inode, path) \ |
| 100 | __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) |
| 101 | static int __ext4_ext_dirty(const char *where, unsigned int line, |
| 102 | handle_t *handle, struct inode *inode, |
| 103 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 104 | { |
| 105 | int err; |
| 106 | if (path->p_bh) { |
| 107 | /* path points to block */ |
Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 108 | err = __ext4_handle_dirty_metadata(where, line, handle, |
| 109 | inode, path->p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 110 | } else { |
| 111 | /* path points to leaf/index in inode body */ |
| 112 | err = ext4_mark_inode_dirty(handle, inode); |
| 113 | } |
| 114 | return err; |
| 115 | } |
| 116 | |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 117 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 118 | struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 119 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 120 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 121 | int depth; |
| 122 | |
| 123 | if (path) { |
| 124 | struct ext4_extent *ex; |
| 125 | depth = path->p_depth; |
| 126 | |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 127 | /* |
| 128 | * Try to predict block placement assuming that we are |
| 129 | * filling in a file which will eventually be |
| 130 | * non-sparse --- i.e., in the case of libbfd writing |
| 131 | * an ELF object sections out-of-order but in a way |
| 132 | * the eventually results in a contiguous object or |
| 133 | * executable file, or some database extending a table |
| 134 | * space file. However, this is actually somewhat |
| 135 | * non-ideal if we are writing a sparse file such as |
| 136 | * qemu or KVM writing a raw image file that is going |
| 137 | * to stay fairly sparse, since it will end up |
| 138 | * fragmenting the file system's free space. Maybe we |
| 139 | * should have some hueristics or some way to allow |
| 140 | * userspace to pass a hint to file system, |
Tao Ma | b8d6568 | 2011-01-21 23:21:31 +0800 | [diff] [blame] | 141 | * especially if the latter case turns out to be |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 142 | * common. |
| 143 | */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 144 | ex = path[depth].p_ext; |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 145 | if (ex) { |
| 146 | ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); |
| 147 | ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); |
| 148 | |
| 149 | if (block > ext_block) |
| 150 | return ext_pblk + (block - ext_block); |
| 151 | else |
| 152 | return ext_pblk - (ext_block - block); |
| 153 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 154 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 155 | /* it looks like index is empty; |
| 156 | * try to find starting block from index itself */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 157 | if (path[depth].p_bh) |
| 158 | return path[depth].p_bh->b_blocknr; |
| 159 | } |
| 160 | |
| 161 | /* OK. use inode's group */ |
Eric Sandeen | f86186b | 2011-06-28 10:01:31 -0400 | [diff] [blame] | 162 | return ext4_inode_to_goal_block(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 165 | /* |
| 166 | * Allocation for a meta data block |
| 167 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 168 | static ext4_fsblk_t |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 169 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 170 | struct ext4_ext_path *path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 171 | struct ext4_extent *ex, int *err, unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 172 | { |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 173 | ext4_fsblk_t goal, newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 174 | |
| 175 | goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 176 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 177 | NULL, err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 178 | return newblock; |
| 179 | } |
| 180 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 181 | static inline int ext4_ext_space_block(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 182 | { |
| 183 | int size; |
| 184 | |
| 185 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 186 | / sizeof(struct ext4_extent); |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 187 | if (!check) { |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 188 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 189 | if (size > 6) |
| 190 | size = 6; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 191 | #endif |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 192 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 193 | return size; |
| 194 | } |
| 195 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 196 | 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] | 197 | { |
| 198 | int size; |
| 199 | |
| 200 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 201 | / sizeof(struct ext4_extent_idx); |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 202 | if (!check) { |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 203 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 204 | if (size > 5) |
| 205 | size = 5; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 206 | #endif |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 207 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 208 | return size; |
| 209 | } |
| 210 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 211 | static inline int ext4_ext_space_root(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 212 | { |
| 213 | int size; |
| 214 | |
| 215 | size = sizeof(EXT4_I(inode)->i_data); |
| 216 | size -= sizeof(struct ext4_extent_header); |
| 217 | size /= sizeof(struct ext4_extent); |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 218 | if (!check) { |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 219 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 220 | if (size > 3) |
| 221 | size = 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 222 | #endif |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 223 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 224 | return size; |
| 225 | } |
| 226 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 227 | 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] | 228 | { |
| 229 | int size; |
| 230 | |
| 231 | size = sizeof(EXT4_I(inode)->i_data); |
| 232 | size -= sizeof(struct ext4_extent_header); |
| 233 | size /= sizeof(struct ext4_extent_idx); |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 234 | if (!check) { |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 235 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 236 | if (size > 4) |
| 237 | size = 4; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 238 | #endif |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 239 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 240 | return size; |
| 241 | } |
| 242 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 243 | /* |
| 244 | * Calculate the number of metadata blocks needed |
| 245 | * to allocate @blocks |
| 246 | * Worse case is one block per extent |
| 247 | */ |
Theodore Ts'o | 01f49d0 | 2011-01-10 12:13:03 -0500 | [diff] [blame] | 248 | int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 249 | { |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 250 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 251 | int idxs, num = 0; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 252 | |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 253 | idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 254 | / sizeof(struct ext4_extent_idx)); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 255 | |
| 256 | /* |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 257 | * If the new delayed allocation block is contiguous with the |
| 258 | * previous da block, it can share index blocks with the |
| 259 | * previous block, so we only need to allocate a new index |
| 260 | * block every idxs leaf blocks. At ldxs**2 blocks, we need |
| 261 | * an additional index block, and at ldxs**3 blocks, yet |
| 262 | * another index blocks. |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 263 | */ |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 264 | if (ei->i_da_metadata_calc_len && |
| 265 | ei->i_da_metadata_calc_last_lblock+1 == lblock) { |
| 266 | if ((ei->i_da_metadata_calc_len % idxs) == 0) |
| 267 | num++; |
| 268 | if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0) |
| 269 | num++; |
| 270 | if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) { |
| 271 | num++; |
| 272 | ei->i_da_metadata_calc_len = 0; |
| 273 | } else |
| 274 | ei->i_da_metadata_calc_len++; |
| 275 | ei->i_da_metadata_calc_last_lblock++; |
| 276 | return num; |
| 277 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 278 | |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 279 | /* |
| 280 | * In the worst case we need a new set of index blocks at |
| 281 | * every level of the inode's extent tree. |
| 282 | */ |
| 283 | ei->i_da_metadata_calc_len = 1; |
| 284 | ei->i_da_metadata_calc_last_lblock = lblock; |
| 285 | return ext_depth(inode) + 1; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 288 | static int |
| 289 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 290 | { |
| 291 | int max; |
| 292 | |
| 293 | if (depth == ext_depth(inode)) { |
| 294 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 295 | max = ext4_ext_space_root(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 296 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 297 | max = ext4_ext_space_root_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 298 | } else { |
| 299 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 300 | max = ext4_ext_space_block(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 301 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 302 | max = ext4_ext_space_block_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | return max; |
| 306 | } |
| 307 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 308 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
| 309 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 310 | ext4_fsblk_t block = ext4_ext_pblock(ext); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 311 | int len = ext4_ext_get_actual_len(ext); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 312 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 313 | 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] | 314 | } |
| 315 | |
| 316 | static int ext4_valid_extent_idx(struct inode *inode, |
| 317 | struct ext4_extent_idx *ext_idx) |
| 318 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 319 | ext4_fsblk_t block = ext4_idx_pblock(ext_idx); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 320 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 321 | 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] | 322 | } |
| 323 | |
| 324 | static int ext4_valid_extent_entries(struct inode *inode, |
| 325 | struct ext4_extent_header *eh, |
| 326 | int depth) |
| 327 | { |
| 328 | struct ext4_extent *ext; |
| 329 | struct ext4_extent_idx *ext_idx; |
| 330 | unsigned short entries; |
| 331 | if (eh->eh_entries == 0) |
| 332 | return 1; |
| 333 | |
| 334 | entries = le16_to_cpu(eh->eh_entries); |
| 335 | |
| 336 | if (depth == 0) { |
| 337 | /* leaf entries */ |
| 338 | ext = EXT_FIRST_EXTENT(eh); |
| 339 | while (entries) { |
| 340 | if (!ext4_valid_extent(inode, ext)) |
| 341 | return 0; |
| 342 | ext++; |
| 343 | entries--; |
| 344 | } |
| 345 | } else { |
| 346 | ext_idx = EXT_FIRST_INDEX(eh); |
| 347 | while (entries) { |
| 348 | if (!ext4_valid_extent_idx(inode, ext_idx)) |
| 349 | return 0; |
| 350 | ext_idx++; |
| 351 | entries--; |
| 352 | } |
| 353 | } |
| 354 | return 1; |
| 355 | } |
| 356 | |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 357 | static int __ext4_ext_check(const char *function, unsigned int line, |
| 358 | struct inode *inode, struct ext4_extent_header *eh, |
| 359 | int depth) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 360 | { |
| 361 | const char *error_msg; |
| 362 | int max = 0; |
| 363 | |
| 364 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { |
| 365 | error_msg = "invalid magic"; |
| 366 | goto corrupted; |
| 367 | } |
| 368 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { |
| 369 | error_msg = "unexpected eh_depth"; |
| 370 | goto corrupted; |
| 371 | } |
| 372 | if (unlikely(eh->eh_max == 0)) { |
| 373 | error_msg = "invalid eh_max"; |
| 374 | goto corrupted; |
| 375 | } |
| 376 | max = ext4_ext_max_entries(inode, depth); |
| 377 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { |
| 378 | error_msg = "too large eh_max"; |
| 379 | goto corrupted; |
| 380 | } |
| 381 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { |
| 382 | error_msg = "invalid eh_entries"; |
| 383 | goto corrupted; |
| 384 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 385 | if (!ext4_valid_extent_entries(inode, eh, depth)) { |
| 386 | error_msg = "invalid extent entries"; |
| 387 | goto corrupted; |
| 388 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 389 | return 0; |
| 390 | |
| 391 | corrupted: |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 392 | ext4_error_inode(inode, function, line, 0, |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 393 | "bad header/extent: %s - magic %x, " |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 394 | "entries %u, max %u(%u), depth %u(%u)", |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 395 | error_msg, le16_to_cpu(eh->eh_magic), |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 396 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), |
| 397 | max, le16_to_cpu(eh->eh_depth), depth); |
| 398 | |
| 399 | return -EIO; |
| 400 | } |
| 401 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 402 | #define ext4_ext_check(inode, eh, depth) \ |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 403 | __ext4_ext_check(__func__, __LINE__, inode, eh, depth) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 404 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 405 | int ext4_ext_check_inode(struct inode *inode) |
| 406 | { |
| 407 | return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); |
| 408 | } |
| 409 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 410 | #ifdef EXT_DEBUG |
| 411 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 412 | { |
| 413 | int k, l = path->p_depth; |
| 414 | |
| 415 | ext_debug("path:"); |
| 416 | for (k = 0; k <= l; k++, path++) { |
| 417 | if (path->p_idx) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 418 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 419 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 420 | } else if (path->p_ext) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 421 | ext_debug(" %d:[%d]%d:%llu ", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 422 | le32_to_cpu(path->p_ext->ee_block), |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 423 | ext4_ext_is_uninitialized(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 424 | ext4_ext_get_actual_len(path->p_ext), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 425 | ext4_ext_pblock(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 426 | } else |
| 427 | ext_debug(" []"); |
| 428 | } |
| 429 | ext_debug("\n"); |
| 430 | } |
| 431 | |
| 432 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 433 | { |
| 434 | int depth = ext_depth(inode); |
| 435 | struct ext4_extent_header *eh; |
| 436 | struct ext4_extent *ex; |
| 437 | int i; |
| 438 | |
| 439 | if (!path) |
| 440 | return; |
| 441 | |
| 442 | eh = path[depth].p_hdr; |
| 443 | ex = EXT_FIRST_EXTENT(eh); |
| 444 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 445 | ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); |
| 446 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 447 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 448 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
| 449 | ext4_ext_is_uninitialized(ex), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 450 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 451 | } |
| 452 | ext_debug("\n"); |
| 453 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 454 | |
| 455 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, |
| 456 | ext4_fsblk_t newblock, int level) |
| 457 | { |
| 458 | int depth = ext_depth(inode); |
| 459 | struct ext4_extent *ex; |
| 460 | |
| 461 | if (depth != level) { |
| 462 | struct ext4_extent_idx *idx; |
| 463 | idx = path[level].p_idx; |
| 464 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { |
| 465 | ext_debug("%d: move %d:%llu in new index %llu\n", level, |
| 466 | le32_to_cpu(idx->ei_block), |
| 467 | ext4_idx_pblock(idx), |
| 468 | newblock); |
| 469 | idx++; |
| 470 | } |
| 471 | |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | ex = path[depth].p_ext; |
| 476 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 477 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", |
| 478 | le32_to_cpu(ex->ee_block), |
| 479 | ext4_ext_pblock(ex), |
| 480 | ext4_ext_is_uninitialized(ex), |
| 481 | ext4_ext_get_actual_len(ex), |
| 482 | newblock); |
| 483 | ex++; |
| 484 | } |
| 485 | } |
| 486 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 487 | #else |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 488 | #define ext4_ext_show_path(inode, path) |
| 489 | #define ext4_ext_show_leaf(inode, path) |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 490 | #define ext4_ext_show_move(inode, path, newblock, level) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 491 | #endif |
| 492 | |
Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 493 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 494 | { |
| 495 | int depth = path->p_depth; |
| 496 | int i; |
| 497 | |
| 498 | for (i = 0; i <= depth; i++, path++) |
| 499 | if (path->p_bh) { |
| 500 | brelse(path->p_bh); |
| 501 | path->p_bh = NULL; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 506 | * ext4_ext_binsearch_idx: |
| 507 | * binary search for the closest index of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 508 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 509 | */ |
| 510 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 511 | ext4_ext_binsearch_idx(struct inode *inode, |
| 512 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 513 | { |
| 514 | struct ext4_extent_header *eh = path->p_hdr; |
| 515 | struct ext4_extent_idx *r, *l, *m; |
| 516 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 517 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 518 | ext_debug("binsearch for %u(idx): ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 519 | |
| 520 | l = EXT_FIRST_INDEX(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 521 | r = EXT_LAST_INDEX(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 522 | while (l <= r) { |
| 523 | m = l + (r - l) / 2; |
| 524 | if (block < le32_to_cpu(m->ei_block)) |
| 525 | r = m - 1; |
| 526 | else |
| 527 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 528 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
| 529 | m, le32_to_cpu(m->ei_block), |
| 530 | r, le32_to_cpu(r->ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | path->p_idx = l - 1; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 534 | ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 535 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 536 | |
| 537 | #ifdef CHECK_BINSEARCH |
| 538 | { |
| 539 | struct ext4_extent_idx *chix, *ix; |
| 540 | int k; |
| 541 | |
| 542 | chix = ix = EXT_FIRST_INDEX(eh); |
| 543 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
| 544 | if (k != 0 && |
| 545 | le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 546 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
| 547 | "first=0x%p\n", k, |
| 548 | ix, EXT_FIRST_INDEX(eh)); |
| 549 | printk(KERN_DEBUG "%u <= %u\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 550 | le32_to_cpu(ix->ei_block), |
| 551 | le32_to_cpu(ix[-1].ei_block)); |
| 552 | } |
| 553 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 554 | <= le32_to_cpu(ix[-1].ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 555 | if (block < le32_to_cpu(ix->ei_block)) |
| 556 | break; |
| 557 | chix = ix; |
| 558 | } |
| 559 | BUG_ON(chix != path->p_idx); |
| 560 | } |
| 561 | #endif |
| 562 | |
| 563 | } |
| 564 | |
| 565 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 566 | * ext4_ext_binsearch: |
| 567 | * binary search for closest extent of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 568 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 569 | */ |
| 570 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 571 | ext4_ext_binsearch(struct inode *inode, |
| 572 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 573 | { |
| 574 | struct ext4_extent_header *eh = path->p_hdr; |
| 575 | struct ext4_extent *r, *l, *m; |
| 576 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 577 | if (eh->eh_entries == 0) { |
| 578 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 579 | * this leaf is empty: |
| 580 | * we get such a leaf in split/add case |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 581 | */ |
| 582 | return; |
| 583 | } |
| 584 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 585 | ext_debug("binsearch for %u: ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 586 | |
| 587 | l = EXT_FIRST_EXTENT(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 588 | r = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 589 | |
| 590 | while (l <= r) { |
| 591 | m = l + (r - l) / 2; |
| 592 | if (block < le32_to_cpu(m->ee_block)) |
| 593 | r = m - 1; |
| 594 | else |
| 595 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 596 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
| 597 | m, le32_to_cpu(m->ee_block), |
| 598 | r, le32_to_cpu(r->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | path->p_ext = l - 1; |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 602 | ext_debug(" -> %d:%llu:[%d]%d ", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 603 | le32_to_cpu(path->p_ext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 604 | ext4_ext_pblock(path->p_ext), |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 605 | ext4_ext_is_uninitialized(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 606 | ext4_ext_get_actual_len(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 607 | |
| 608 | #ifdef CHECK_BINSEARCH |
| 609 | { |
| 610 | struct ext4_extent *chex, *ex; |
| 611 | int k; |
| 612 | |
| 613 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 614 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 615 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 616 | <= le32_to_cpu(ex[-1].ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 617 | if (block < le32_to_cpu(ex->ee_block)) |
| 618 | break; |
| 619 | chex = ex; |
| 620 | } |
| 621 | BUG_ON(chex != path->p_ext); |
| 622 | } |
| 623 | #endif |
| 624 | |
| 625 | } |
| 626 | |
| 627 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
| 628 | { |
| 629 | struct ext4_extent_header *eh; |
| 630 | |
| 631 | eh = ext_inode_hdr(inode); |
| 632 | eh->eh_depth = 0; |
| 633 | eh->eh_entries = 0; |
| 634 | eh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 635 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 636 | ext4_mark_inode_dirty(handle, inode); |
| 637 | ext4_ext_invalidate_cache(inode); |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | struct ext4_ext_path * |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 642 | ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, |
| 643 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 644 | { |
| 645 | struct ext4_extent_header *eh; |
| 646 | struct buffer_head *bh; |
| 647 | short int depth, i, ppos = 0, alloc = 0; |
| 648 | |
| 649 | eh = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 650 | depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 651 | |
| 652 | /* account possible depth increase */ |
| 653 | if (!path) { |
Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 654 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 655 | GFP_NOFS); |
| 656 | if (!path) |
| 657 | return ERR_PTR(-ENOMEM); |
| 658 | alloc = 1; |
| 659 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 660 | path[0].p_hdr = eh; |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 661 | path[0].p_bh = NULL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 662 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 663 | i = depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 664 | /* walk through the tree */ |
| 665 | while (i) { |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 666 | int need_to_validate = 0; |
| 667 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 668 | ext_debug("depth %d: num %d, max %d\n", |
| 669 | 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] | 670 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 671 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 672 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 673 | path[ppos].p_depth = i; |
| 674 | path[ppos].p_ext = NULL; |
| 675 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 676 | bh = sb_getblk(inode->i_sb, path[ppos].p_block); |
| 677 | if (unlikely(!bh)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 678 | goto err; |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 679 | if (!bh_uptodate_or_lock(bh)) { |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 680 | trace_ext4_ext_load_extent(inode, block, |
| 681 | path[ppos].p_block); |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 682 | if (bh_submit_read(bh) < 0) { |
| 683 | put_bh(bh); |
| 684 | goto err; |
| 685 | } |
| 686 | /* validate the extent entries */ |
| 687 | need_to_validate = 1; |
| 688 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 689 | eh = ext_block_hdr(bh); |
| 690 | ppos++; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 691 | if (unlikely(ppos > depth)) { |
| 692 | put_bh(bh); |
| 693 | EXT4_ERROR_INODE(inode, |
| 694 | "ppos %d > depth %d", ppos, depth); |
| 695 | goto err; |
| 696 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 697 | path[ppos].p_bh = bh; |
| 698 | path[ppos].p_hdr = eh; |
| 699 | i--; |
| 700 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 701 | if (need_to_validate && ext4_ext_check(inode, eh, i)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 702 | goto err; |
| 703 | } |
| 704 | |
| 705 | path[ppos].p_depth = i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 706 | path[ppos].p_ext = NULL; |
| 707 | path[ppos].p_idx = NULL; |
| 708 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 709 | /* find extent */ |
| 710 | ext4_ext_binsearch(inode, path + ppos, block); |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 711 | /* if not an empty leaf */ |
| 712 | if (path[ppos].p_ext) |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 713 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 714 | |
| 715 | ext4_ext_show_path(inode, path); |
| 716 | |
| 717 | return path; |
| 718 | |
| 719 | err: |
| 720 | ext4_ext_drop_refs(path); |
| 721 | if (alloc) |
| 722 | kfree(path); |
| 723 | return ERR_PTR(-EIO); |
| 724 | } |
| 725 | |
| 726 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 727 | * ext4_ext_insert_index: |
| 728 | * insert new index [@logical;@ptr] into the block at @curp; |
| 729 | * check where to insert: before @curp or after @curp |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 730 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 731 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 732 | struct ext4_ext_path *curp, |
| 733 | int logical, ext4_fsblk_t ptr) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 734 | { |
| 735 | struct ext4_extent_idx *ix; |
| 736 | int len, err; |
| 737 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 738 | err = ext4_ext_get_access(handle, inode, curp); |
| 739 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 740 | return err; |
| 741 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 742 | if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { |
| 743 | EXT4_ERROR_INODE(inode, |
| 744 | "logical %d == ei_block %d!", |
| 745 | logical, le32_to_cpu(curp->p_idx->ei_block)); |
| 746 | return -EIO; |
| 747 | } |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 748 | |
| 749 | if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) |
| 750 | >= le16_to_cpu(curp->p_hdr->eh_max))) { |
| 751 | EXT4_ERROR_INODE(inode, |
| 752 | "eh_entries %d >= eh_max %d!", |
| 753 | le16_to_cpu(curp->p_hdr->eh_entries), |
| 754 | le16_to_cpu(curp->p_hdr->eh_max)); |
| 755 | return -EIO; |
| 756 | } |
| 757 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 758 | len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; |
| 759 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 760 | /* insert after */ |
| 761 | if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { |
| 762 | len = (len - 1) * sizeof(struct ext4_extent_idx); |
| 763 | len = len < 0 ? 0 : len; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 764 | ext_debug("insert new index %d after: %llu. " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 765 | "move %d from 0x%p to 0x%p\n", |
| 766 | logical, ptr, len, |
| 767 | (curp->p_idx + 1), (curp->p_idx + 2)); |
| 768 | memmove(curp->p_idx + 2, curp->p_idx + 1, len); |
| 769 | } |
| 770 | ix = curp->p_idx + 1; |
| 771 | } else { |
| 772 | /* insert before */ |
| 773 | len = len * sizeof(struct ext4_extent_idx); |
| 774 | len = len < 0 ? 0 : len; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 775 | ext_debug("insert new index %d before: %llu. " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 776 | "move %d from 0x%p to 0x%p\n", |
| 777 | logical, ptr, len, |
| 778 | curp->p_idx, (curp->p_idx + 1)); |
| 779 | memmove(curp->p_idx + 1, curp->p_idx, len); |
| 780 | ix = curp->p_idx; |
| 781 | } |
| 782 | |
| 783 | ix->ei_block = cpu_to_le32(logical); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 784 | ext4_idx_store_pblock(ix, ptr); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 785 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 786 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 787 | if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { |
| 788 | EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); |
| 789 | return -EIO; |
| 790 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 791 | |
| 792 | err = ext4_ext_dirty(handle, inode, curp); |
| 793 | ext4_std_error(inode->i_sb, err); |
| 794 | |
| 795 | return err; |
| 796 | } |
| 797 | |
| 798 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 799 | * ext4_ext_split: |
| 800 | * inserts new subtree into the path, using free index entry |
| 801 | * at depth @at: |
| 802 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 803 | * - makes decision where to split |
| 804 | * - moves remaining extents and index entries (right to the split point) |
| 805 | * into the newly allocated blocks |
| 806 | * - initializes subtree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 807 | */ |
| 808 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 809 | unsigned int flags, |
| 810 | struct ext4_ext_path *path, |
| 811 | struct ext4_extent *newext, int at) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 812 | { |
| 813 | struct buffer_head *bh = NULL; |
| 814 | int depth = ext_depth(inode); |
| 815 | struct ext4_extent_header *neh; |
| 816 | struct ext4_extent_idx *fidx; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 817 | int i = at, k, m, a; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 818 | ext4_fsblk_t newblock, oldblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 819 | __le32 border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 820 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 821 | int err = 0; |
| 822 | |
| 823 | /* make decision: where to split? */ |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 824 | /* FIXME: now decision is simplest: at current extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 825 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 826 | /* if current leaf will be split, then we should use |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 827 | * border from split point */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 828 | if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { |
| 829 | EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); |
| 830 | return -EIO; |
| 831 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 832 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 833 | border = path[depth].p_ext[1].ee_block; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 834 | ext_debug("leaf will be split." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 835 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 836 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 837 | } else { |
| 838 | border = newext->ee_block; |
| 839 | ext_debug("leaf will be added." |
| 840 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 841 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 845 | * If error occurs, then we break processing |
| 846 | * and mark filesystem read-only. index won't |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 847 | * be inserted and tree will be in consistent |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 848 | * state. Next mount will repair buffers too. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 849 | */ |
| 850 | |
| 851 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 852 | * Get array to track all allocated blocks. |
| 853 | * We need this to handle errors and free blocks |
| 854 | * upon them. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 855 | */ |
Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 856 | ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 857 | if (!ablocks) |
| 858 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 859 | |
| 860 | /* allocate all needed blocks */ |
| 861 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
| 862 | for (a = 0; a < depth - at; a++) { |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 863 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 864 | newext, &err, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 865 | if (newblock == 0) |
| 866 | goto cleanup; |
| 867 | ablocks[a] = newblock; |
| 868 | } |
| 869 | |
| 870 | /* initialize new leaf */ |
| 871 | newblock = ablocks[--a]; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 872 | if (unlikely(newblock == 0)) { |
| 873 | EXT4_ERROR_INODE(inode, "newblock == 0!"); |
| 874 | err = -EIO; |
| 875 | goto cleanup; |
| 876 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 877 | bh = sb_getblk(inode->i_sb, newblock); |
| 878 | if (!bh) { |
| 879 | err = -EIO; |
| 880 | goto cleanup; |
| 881 | } |
| 882 | lock_buffer(bh); |
| 883 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 884 | err = ext4_journal_get_create_access(handle, bh); |
| 885 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 886 | goto cleanup; |
| 887 | |
| 888 | neh = ext_block_hdr(bh); |
| 889 | neh->eh_entries = 0; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 890 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 891 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 892 | neh->eh_depth = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 893 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 894 | /* move remainder of path[depth] to the new leaf */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 895 | if (unlikely(path[depth].p_hdr->eh_entries != |
| 896 | path[depth].p_hdr->eh_max)) { |
| 897 | EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", |
| 898 | path[depth].p_hdr->eh_entries, |
| 899 | path[depth].p_hdr->eh_max); |
| 900 | err = -EIO; |
| 901 | goto cleanup; |
| 902 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 903 | /* start copy from next extent */ |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 904 | m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; |
| 905 | ext4_ext_show_move(inode, path, newblock, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 906 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 907 | struct ext4_extent *ex; |
| 908 | ex = EXT_FIRST_EXTENT(neh); |
| 909 | memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 910 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | set_buffer_uptodate(bh); |
| 914 | unlock_buffer(bh); |
| 915 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 916 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 917 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 918 | goto cleanup; |
| 919 | brelse(bh); |
| 920 | bh = NULL; |
| 921 | |
| 922 | /* correct old leaf */ |
| 923 | if (m) { |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 924 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 925 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 926 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 927 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 928 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 929 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 930 | goto cleanup; |
| 931 | |
| 932 | } |
| 933 | |
| 934 | /* create intermediate indexes */ |
| 935 | k = depth - at - 1; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 936 | if (unlikely(k < 0)) { |
| 937 | EXT4_ERROR_INODE(inode, "k %d < 0!", k); |
| 938 | err = -EIO; |
| 939 | goto cleanup; |
| 940 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 941 | if (k) |
| 942 | ext_debug("create %d intermediate indices\n", k); |
| 943 | /* insert new index into current index block */ |
| 944 | /* current depth stored in i var */ |
| 945 | i = depth - 1; |
| 946 | while (k--) { |
| 947 | oldblock = newblock; |
| 948 | newblock = ablocks[--a]; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 949 | bh = sb_getblk(inode->i_sb, newblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 950 | if (!bh) { |
| 951 | err = -EIO; |
| 952 | goto cleanup; |
| 953 | } |
| 954 | lock_buffer(bh); |
| 955 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 956 | err = ext4_journal_get_create_access(handle, bh); |
| 957 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 958 | goto cleanup; |
| 959 | |
| 960 | neh = ext_block_hdr(bh); |
| 961 | neh->eh_entries = cpu_to_le16(1); |
| 962 | neh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 963 | 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] | 964 | neh->eh_depth = cpu_to_le16(depth - i); |
| 965 | fidx = EXT_FIRST_INDEX(neh); |
| 966 | fidx->ei_block = border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 967 | ext4_idx_store_pblock(fidx, oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 968 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 969 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
| 970 | i, newblock, le32_to_cpu(border), oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 971 | |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 972 | /* move remainder of path[i] to the new index block */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 973 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != |
| 974 | EXT_LAST_INDEX(path[i].p_hdr))) { |
| 975 | EXT4_ERROR_INODE(inode, |
| 976 | "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", |
| 977 | le32_to_cpu(path[i].p_ext->ee_block)); |
| 978 | err = -EIO; |
| 979 | goto cleanup; |
| 980 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 981 | /* start copy indexes */ |
| 982 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
| 983 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
| 984 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 985 | ext4_ext_show_move(inode, path, newblock, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 986 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 987 | memmove(++fidx, path[i].p_idx, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 988 | sizeof(struct ext4_extent_idx) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 989 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 990 | } |
| 991 | set_buffer_uptodate(bh); |
| 992 | unlock_buffer(bh); |
| 993 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 994 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 995 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 996 | goto cleanup; |
| 997 | brelse(bh); |
| 998 | bh = NULL; |
| 999 | |
| 1000 | /* correct old index */ |
| 1001 | if (m) { |
| 1002 | err = ext4_ext_get_access(handle, inode, path + i); |
| 1003 | if (err) |
| 1004 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1005 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1006 | err = ext4_ext_dirty(handle, inode, path + i); |
| 1007 | if (err) |
| 1008 | goto cleanup; |
| 1009 | } |
| 1010 | |
| 1011 | i--; |
| 1012 | } |
| 1013 | |
| 1014 | /* insert new index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1015 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 1016 | le32_to_cpu(border), newblock); |
| 1017 | |
| 1018 | cleanup: |
| 1019 | if (bh) { |
| 1020 | if (buffer_locked(bh)) |
| 1021 | unlock_buffer(bh); |
| 1022 | brelse(bh); |
| 1023 | } |
| 1024 | |
| 1025 | if (err) { |
| 1026 | /* free all allocated blocks in error case */ |
| 1027 | for (i = 0; i < depth; i++) { |
| 1028 | if (!ablocks[i]) |
| 1029 | continue; |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1030 | ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1031 | EXT4_FREE_BLOCKS_METADATA); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | kfree(ablocks); |
| 1035 | |
| 1036 | return err; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1040 | * ext4_ext_grow_indepth: |
| 1041 | * implements tree growing procedure: |
| 1042 | * - allocates new block |
| 1043 | * - moves top-level data (index block or leaf) into the new block |
| 1044 | * - initializes new top-level, creating index that points to the |
| 1045 | * just created block |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1046 | */ |
| 1047 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1048 | unsigned int flags, |
| 1049 | struct ext4_ext_path *path, |
| 1050 | struct ext4_extent *newext) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1051 | { |
| 1052 | struct ext4_ext_path *curp = path; |
| 1053 | struct ext4_extent_header *neh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1054 | struct buffer_head *bh; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1055 | ext4_fsblk_t newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1056 | int err = 0; |
| 1057 | |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1058 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
| 1059 | newext, &err, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1060 | if (newblock == 0) |
| 1061 | return err; |
| 1062 | |
| 1063 | bh = sb_getblk(inode->i_sb, newblock); |
| 1064 | if (!bh) { |
| 1065 | err = -EIO; |
| 1066 | ext4_std_error(inode->i_sb, err); |
| 1067 | return err; |
| 1068 | } |
| 1069 | lock_buffer(bh); |
| 1070 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1071 | err = ext4_journal_get_create_access(handle, bh); |
| 1072 | if (err) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1073 | unlock_buffer(bh); |
| 1074 | goto out; |
| 1075 | } |
| 1076 | |
| 1077 | /* move top-level index/leaf into new block */ |
| 1078 | memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); |
| 1079 | |
| 1080 | /* set size of new block */ |
| 1081 | neh = ext_block_hdr(bh); |
| 1082 | /* old root could have indexes or leaves |
| 1083 | * so calculate e_max right way */ |
| 1084 | if (ext_depth(inode)) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1085 | 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] | 1086 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1087 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1088 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 1089 | set_buffer_uptodate(bh); |
| 1090 | unlock_buffer(bh); |
| 1091 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1092 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1093 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1094 | goto out; |
| 1095 | |
| 1096 | /* create index in new top-level index: num,max,pointer */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1097 | err = ext4_ext_get_access(handle, inode, curp); |
| 1098 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1099 | goto out; |
| 1100 | |
| 1101 | curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1102 | 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] | 1103 | curp->p_hdr->eh_entries = cpu_to_le16(1); |
| 1104 | curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 1105 | |
| 1106 | if (path[0].p_hdr->eh_depth) |
| 1107 | curp->p_idx->ei_block = |
| 1108 | EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; |
| 1109 | else |
| 1110 | curp->p_idx->ei_block = |
| 1111 | EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1112 | ext4_idx_store_pblock(curp->p_idx, newblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1113 | |
| 1114 | neh = ext_inode_hdr(inode); |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1115 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1116 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 1117 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1118 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1119 | |
| 1120 | neh->eh_depth = cpu_to_le16(path->p_depth + 1); |
| 1121 | err = ext4_ext_dirty(handle, inode, curp); |
| 1122 | out: |
| 1123 | brelse(bh); |
| 1124 | |
| 1125 | return err; |
| 1126 | } |
| 1127 | |
| 1128 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1129 | * ext4_ext_create_new_leaf: |
| 1130 | * finds empty index and adds new leaf. |
| 1131 | * if no free index is found, then it requests in-depth growing. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1132 | */ |
| 1133 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1134 | unsigned int flags, |
| 1135 | struct ext4_ext_path *path, |
| 1136 | struct ext4_extent *newext) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1137 | { |
| 1138 | struct ext4_ext_path *curp; |
| 1139 | int depth, i, err = 0; |
| 1140 | |
| 1141 | repeat: |
| 1142 | i = depth = ext_depth(inode); |
| 1143 | |
| 1144 | /* walk up to the tree and look for free index entry */ |
| 1145 | curp = path + depth; |
| 1146 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1147 | i--; |
| 1148 | curp--; |
| 1149 | } |
| 1150 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1151 | /* we use already allocated block for index block, |
| 1152 | * so subsequent data blocks should be contiguous */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1153 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1154 | /* if we found index with free entry, then use that |
| 1155 | * entry: create all needed subtree and add new leaf */ |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1156 | err = ext4_ext_split(handle, inode, flags, path, newext, i); |
Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1157 | if (err) |
| 1158 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1159 | |
| 1160 | /* refill path */ |
| 1161 | ext4_ext_drop_refs(path); |
| 1162 | path = ext4_ext_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1163 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
| 1164 | path); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1165 | if (IS_ERR(path)) |
| 1166 | err = PTR_ERR(path); |
| 1167 | } else { |
| 1168 | /* tree is full, time to grow in depth */ |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1169 | err = ext4_ext_grow_indepth(handle, inode, flags, |
| 1170 | path, newext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1171 | if (err) |
| 1172 | goto out; |
| 1173 | |
| 1174 | /* refill path */ |
| 1175 | ext4_ext_drop_refs(path); |
| 1176 | path = ext4_ext_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1177 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
| 1178 | path); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1179 | if (IS_ERR(path)) { |
| 1180 | err = PTR_ERR(path); |
| 1181 | goto out; |
| 1182 | } |
| 1183 | |
| 1184 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1185 | * only first (depth 0 -> 1) produces free space; |
| 1186 | * in all other cases we have to split the grown tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1187 | */ |
| 1188 | depth = ext_depth(inode); |
| 1189 | 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] | 1190 | /* now we need to split */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1191 | goto repeat; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | out: |
| 1196 | return err; |
| 1197 | } |
| 1198 | |
| 1199 | /* |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1200 | * search the closest allocated block to the left for *logical |
| 1201 | * and returns it at @logical + it's physical address at @phys |
| 1202 | * if *logical is the smallest allocated block, the function |
| 1203 | * returns 0 at @phys |
| 1204 | * return value contains 0 (success) or error code |
| 1205 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1206 | static int ext4_ext_search_left(struct inode *inode, |
| 1207 | struct ext4_ext_path *path, |
| 1208 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1209 | { |
| 1210 | struct ext4_extent_idx *ix; |
| 1211 | struct ext4_extent *ex; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1212 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1213 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1214 | if (unlikely(path == NULL)) { |
| 1215 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
| 1216 | return -EIO; |
| 1217 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1218 | depth = path->p_depth; |
| 1219 | *phys = 0; |
| 1220 | |
| 1221 | if (depth == 0 && path->p_ext == NULL) |
| 1222 | return 0; |
| 1223 | |
| 1224 | /* usually extent in the path covers blocks smaller |
| 1225 | * then *logical, but it can be that extent is the |
| 1226 | * first one in the file */ |
| 1227 | |
| 1228 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1229 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1230 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1231 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1232 | EXT4_ERROR_INODE(inode, |
| 1233 | "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", |
| 1234 | *logical, le32_to_cpu(ex->ee_block)); |
| 1235 | return -EIO; |
| 1236 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1237 | while (--depth >= 0) { |
| 1238 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1239 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1240 | EXT4_ERROR_INODE(inode, |
| 1241 | "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", |
| 1242 | ix != NULL ? ix->ei_block : 0, |
| 1243 | EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? |
| 1244 | EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0, |
| 1245 | depth); |
| 1246 | return -EIO; |
| 1247 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1248 | } |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1252 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1253 | EXT4_ERROR_INODE(inode, |
| 1254 | "logical %d < ee_block %d + ee_len %d!", |
| 1255 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
| 1256 | return -EIO; |
| 1257 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1258 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1259 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1260 | *phys = ext4_ext_pblock(ex) + ee_len - 1; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * search the closest allocated block to the right for *logical |
| 1266 | * and returns it at @logical + it's physical address at @phys |
| 1267 | * if *logical is the smallest allocated block, the function |
| 1268 | * returns 0 at @phys |
| 1269 | * return value contains 0 (success) or error code |
| 1270 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1271 | static int ext4_ext_search_right(struct inode *inode, |
| 1272 | struct ext4_ext_path *path, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1273 | ext4_lblk_t *logical, ext4_fsblk_t *phys, |
| 1274 | struct ext4_extent **ret_ex) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1275 | { |
| 1276 | struct buffer_head *bh = NULL; |
| 1277 | struct ext4_extent_header *eh; |
| 1278 | struct ext4_extent_idx *ix; |
| 1279 | struct ext4_extent *ex; |
| 1280 | ext4_fsblk_t block; |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1281 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
| 1282 | int ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1283 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1284 | if (unlikely(path == NULL)) { |
| 1285 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
| 1286 | return -EIO; |
| 1287 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1288 | depth = path->p_depth; |
| 1289 | *phys = 0; |
| 1290 | |
| 1291 | if (depth == 0 && path->p_ext == NULL) |
| 1292 | return 0; |
| 1293 | |
| 1294 | /* usually extent in the path covers blocks smaller |
| 1295 | * then *logical, but it can be that extent is the |
| 1296 | * first one in the file */ |
| 1297 | |
| 1298 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1299 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1300 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1301 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1302 | EXT4_ERROR_INODE(inode, |
| 1303 | "first_extent(path[%d].p_hdr) != ex", |
| 1304 | depth); |
| 1305 | return -EIO; |
| 1306 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1307 | while (--depth >= 0) { |
| 1308 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1309 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1310 | EXT4_ERROR_INODE(inode, |
| 1311 | "ix != EXT_FIRST_INDEX *logical %d!", |
| 1312 | *logical); |
| 1313 | return -EIO; |
| 1314 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1315 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1316 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1317 | } |
| 1318 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1319 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1320 | EXT4_ERROR_INODE(inode, |
| 1321 | "logical %d < ee_block %d + ee_len %d!", |
| 1322 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
| 1323 | return -EIO; |
| 1324 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1325 | |
| 1326 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1327 | /* next allocated block in this leaf */ |
| 1328 | ex++; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1329 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* go up and search for index to the right */ |
| 1333 | while (--depth >= 0) { |
| 1334 | ix = path[depth].p_idx; |
| 1335 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1336 | goto got_index; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1337 | } |
| 1338 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1339 | /* we've gone up to the root and found no index to the right */ |
| 1340 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1341 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1342 | got_index: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1343 | /* we've found index to the right, let's |
| 1344 | * follow it and find the closest allocated |
| 1345 | * block to the right */ |
| 1346 | ix++; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1347 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1348 | while (++depth < path->p_depth) { |
| 1349 | bh = sb_bread(inode->i_sb, block); |
| 1350 | if (bh == NULL) |
| 1351 | return -EIO; |
| 1352 | eh = ext_block_hdr(bh); |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1353 | /* subtract from p_depth to get proper eh_depth */ |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 1354 | if (ext4_ext_check(inode, eh, path->p_depth - depth)) { |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1355 | put_bh(bh); |
| 1356 | return -EIO; |
| 1357 | } |
| 1358 | ix = EXT_FIRST_INDEX(eh); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1359 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1360 | put_bh(bh); |
| 1361 | } |
| 1362 | |
| 1363 | bh = sb_bread(inode->i_sb, block); |
| 1364 | if (bh == NULL) |
| 1365 | return -EIO; |
| 1366 | eh = ext_block_hdr(bh); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 1367 | if (ext4_ext_check(inode, eh, path->p_depth - depth)) { |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1368 | put_bh(bh); |
| 1369 | return -EIO; |
| 1370 | } |
| 1371 | ex = EXT_FIRST_EXTENT(eh); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1372 | found_extent: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1373 | *logical = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1374 | *phys = ext4_ext_pblock(ex); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1375 | *ret_ex = ex; |
| 1376 | if (bh) |
| 1377 | put_bh(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1378 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1382 | * ext4_ext_next_allocated_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1383 | * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1384 | * NOTE: it considers block number from index entry as |
| 1385 | * allocated block. Thus, index entries have to be consistent |
| 1386 | * with leaves. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1387 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1388 | static ext4_lblk_t |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1389 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1390 | { |
| 1391 | int depth; |
| 1392 | |
| 1393 | BUG_ON(path == NULL); |
| 1394 | depth = path->p_depth; |
| 1395 | |
| 1396 | if (depth == 0 && path->p_ext == NULL) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1397 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1398 | |
| 1399 | while (depth >= 0) { |
| 1400 | if (depth == path->p_depth) { |
| 1401 | /* leaf */ |
| 1402 | if (path[depth].p_ext != |
| 1403 | EXT_LAST_EXTENT(path[depth].p_hdr)) |
| 1404 | return le32_to_cpu(path[depth].p_ext[1].ee_block); |
| 1405 | } else { |
| 1406 | /* index */ |
| 1407 | if (path[depth].p_idx != |
| 1408 | EXT_LAST_INDEX(path[depth].p_hdr)) |
| 1409 | return le32_to_cpu(path[depth].p_idx[1].ei_block); |
| 1410 | } |
| 1411 | depth--; |
| 1412 | } |
| 1413 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1414 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1418 | * ext4_ext_next_leaf_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1419 | * returns first allocated block from next leaf or EXT_MAX_BLOCKS |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1420 | */ |
Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1421 | static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1422 | { |
| 1423 | int depth; |
| 1424 | |
| 1425 | BUG_ON(path == NULL); |
| 1426 | depth = path->p_depth; |
| 1427 | |
| 1428 | /* zero-tree has no leaf blocks at all */ |
| 1429 | if (depth == 0) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1430 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1431 | |
| 1432 | /* go to index block */ |
| 1433 | depth--; |
| 1434 | |
| 1435 | while (depth >= 0) { |
| 1436 | if (path[depth].p_idx != |
| 1437 | EXT_LAST_INDEX(path[depth].p_hdr)) |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1438 | return (ext4_lblk_t) |
| 1439 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1440 | depth--; |
| 1441 | } |
| 1442 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1443 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1447 | * ext4_ext_correct_indexes: |
| 1448 | * if leaf gets modified and modified extent is first in the leaf, |
| 1449 | * then we have to correct all indexes above. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1450 | * TODO: do we need to correct tree in all cases? |
| 1451 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1452 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1453 | struct ext4_ext_path *path) |
| 1454 | { |
| 1455 | struct ext4_extent_header *eh; |
| 1456 | int depth = ext_depth(inode); |
| 1457 | struct ext4_extent *ex; |
| 1458 | __le32 border; |
| 1459 | int k, err = 0; |
| 1460 | |
| 1461 | eh = path[depth].p_hdr; |
| 1462 | ex = path[depth].p_ext; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1463 | |
| 1464 | if (unlikely(ex == NULL || eh == NULL)) { |
| 1465 | EXT4_ERROR_INODE(inode, |
| 1466 | "ex %p == NULL or eh %p == NULL", ex, eh); |
| 1467 | return -EIO; |
| 1468 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1469 | |
| 1470 | if (depth == 0) { |
| 1471 | /* there is no tree at all */ |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1476 | /* we correct tree if first leaf got modified only */ |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1481 | * TODO: we need correction if border is smaller than current one |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1482 | */ |
| 1483 | k = depth - 1; |
| 1484 | border = path[depth].p_ext->ee_block; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1485 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1486 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1487 | return err; |
| 1488 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1489 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1490 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1491 | return err; |
| 1492 | |
| 1493 | while (k--) { |
| 1494 | /* change all left-side indexes */ |
| 1495 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1496 | break; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1497 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1498 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1499 | break; |
| 1500 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1501 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1502 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1503 | break; |
| 1504 | } |
| 1505 | |
| 1506 | return err; |
| 1507 | } |
| 1508 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 1509 | int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1510 | ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, |
| 1511 | struct ext4_extent *ex2) |
| 1512 | { |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1513 | unsigned short ext1_ee_len, ext2_ee_len, max_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1514 | |
| 1515 | /* |
| 1516 | * Make sure that either both extents are uninitialized, or |
| 1517 | * both are _not_. |
| 1518 | */ |
| 1519 | if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) |
| 1520 | return 0; |
| 1521 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1522 | if (ext4_ext_is_uninitialized(ex1)) |
| 1523 | max_len = EXT_UNINIT_MAX_LEN; |
| 1524 | else |
| 1525 | max_len = EXT_INIT_MAX_LEN; |
| 1526 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1527 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1528 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1529 | |
| 1530 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1531 | le32_to_cpu(ex2->ee_block)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1532 | return 0; |
| 1533 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1534 | /* |
| 1535 | * To allow future support for preallocated extents to be added |
| 1536 | * as an RO_COMPAT feature, refuse to merge to extents if |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1537 | * this can result in the top bit of ee_len being set. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1538 | */ |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1539 | if (ext1_ee_len + ext2_ee_len > max_len) |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1540 | return 0; |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1541 | #ifdef AGGRESSIVE_TEST |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1542 | if (ext1_ee_len >= 4) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1543 | return 0; |
| 1544 | #endif |
| 1545 | |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1546 | if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1547 | return 1; |
| 1548 | return 0; |
| 1549 | } |
| 1550 | |
| 1551 | /* |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1552 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1553 | * It always tries to merge towards right. If you want to merge towards |
| 1554 | * left, pass "ex - 1" as argument instead of "ex". |
| 1555 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1556 | * 1 if they got merged. |
| 1557 | */ |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1558 | static int ext4_ext_try_to_merge_right(struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1559 | struct ext4_ext_path *path, |
| 1560 | struct ext4_extent *ex) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1561 | { |
| 1562 | struct ext4_extent_header *eh; |
| 1563 | unsigned int depth, len; |
| 1564 | int merge_done = 0; |
| 1565 | int uninitialized = 0; |
| 1566 | |
| 1567 | depth = ext_depth(inode); |
| 1568 | BUG_ON(path[depth].p_hdr == NULL); |
| 1569 | eh = path[depth].p_hdr; |
| 1570 | |
| 1571 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1572 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1573 | break; |
| 1574 | /* merge with next extent! */ |
| 1575 | if (ext4_ext_is_uninitialized(ex)) |
| 1576 | uninitialized = 1; |
| 1577 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1578 | + ext4_ext_get_actual_len(ex + 1)); |
| 1579 | if (uninitialized) |
| 1580 | ext4_ext_mark_uninitialized(ex); |
| 1581 | |
| 1582 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1583 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1584 | * sizeof(struct ext4_extent); |
| 1585 | memmove(ex + 1, ex + 2, len); |
| 1586 | } |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1587 | le16_add_cpu(&eh->eh_entries, -1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1588 | merge_done = 1; |
| 1589 | WARN_ON(eh->eh_entries == 0); |
| 1590 | if (!eh->eh_entries) |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1591 | EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | return merge_done; |
| 1595 | } |
| 1596 | |
| 1597 | /* |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1598 | * This function tries to merge the @ex extent to neighbours in the tree. |
| 1599 | * return 1 if merge left else 0. |
| 1600 | */ |
| 1601 | static int ext4_ext_try_to_merge(struct inode *inode, |
| 1602 | struct ext4_ext_path *path, |
| 1603 | struct ext4_extent *ex) { |
| 1604 | struct ext4_extent_header *eh; |
| 1605 | unsigned int depth; |
| 1606 | int merge_done = 0; |
| 1607 | int ret = 0; |
| 1608 | |
| 1609 | depth = ext_depth(inode); |
| 1610 | BUG_ON(path[depth].p_hdr == NULL); |
| 1611 | eh = path[depth].p_hdr; |
| 1612 | |
| 1613 | if (ex > EXT_FIRST_EXTENT(eh)) |
| 1614 | merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); |
| 1615 | |
| 1616 | if (!merge_done) |
| 1617 | ret = ext4_ext_try_to_merge_right(inode, path, ex); |
| 1618 | |
| 1619 | return ret; |
| 1620 | } |
| 1621 | |
| 1622 | /* |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1623 | * check if a portion of the "newext" extent overlaps with an |
| 1624 | * existing extent. |
| 1625 | * |
| 1626 | * If there is an overlap discovered, it updates the length of the newext |
| 1627 | * such that there will be no overlap, and then returns 1. |
| 1628 | * If there is no overlap found, it returns 0. |
| 1629 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1630 | static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, |
| 1631 | struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1632 | struct ext4_extent *newext, |
| 1633 | struct ext4_ext_path *path) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1634 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1635 | ext4_lblk_t b1, b2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1636 | unsigned int depth, len1; |
| 1637 | unsigned int ret = 0; |
| 1638 | |
| 1639 | b1 = le32_to_cpu(newext->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1640 | len1 = ext4_ext_get_actual_len(newext); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1641 | depth = ext_depth(inode); |
| 1642 | if (!path[depth].p_ext) |
| 1643 | goto out; |
| 1644 | b2 = le32_to_cpu(path[depth].p_ext->ee_block); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1645 | b2 &= ~(sbi->s_cluster_ratio - 1); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1646 | |
| 1647 | /* |
| 1648 | * get the next allocated block if the extent in the path |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1649 | * is before the requested block(s) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1650 | */ |
| 1651 | if (b2 < b1) { |
| 1652 | b2 = ext4_ext_next_allocated_block(path); |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1653 | if (b2 == EXT_MAX_BLOCKS) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1654 | goto out; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1655 | b2 &= ~(sbi->s_cluster_ratio - 1); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1656 | } |
| 1657 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1658 | /* check for wrap through zero on extent logical start block*/ |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1659 | if (b1 + len1 < b1) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1660 | len1 = EXT_MAX_BLOCKS - b1; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1661 | newext->ee_len = cpu_to_le16(len1); |
| 1662 | ret = 1; |
| 1663 | } |
| 1664 | |
| 1665 | /* check for overlap */ |
| 1666 | if (b1 + len1 > b2) { |
| 1667 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1668 | ret = 1; |
| 1669 | } |
| 1670 | out: |
| 1671 | return ret; |
| 1672 | } |
| 1673 | |
| 1674 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1675 | * ext4_ext_insert_extent: |
| 1676 | * tries to merge requsted extent into the existing extent or |
| 1677 | * inserts requested extent as new one into the tree, |
| 1678 | * creating new leaf in the no-space case. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1679 | */ |
| 1680 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
| 1681 | struct ext4_ext_path *path, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 1682 | struct ext4_extent *newext, int flag) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1683 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1684 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1685 | struct ext4_extent *ex, *fex; |
| 1686 | struct ext4_extent *nearex; /* nearest extent */ |
| 1687 | struct ext4_ext_path *npath = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1688 | int depth, len, err; |
| 1689 | ext4_lblk_t next; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1690 | unsigned uninitialized = 0; |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1691 | int flags = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1692 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1693 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
| 1694 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
| 1695 | return -EIO; |
| 1696 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1697 | depth = ext_depth(inode); |
| 1698 | ex = path[depth].p_ext; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1699 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1700 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
| 1701 | return -EIO; |
| 1702 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1703 | |
| 1704 | /* try to insert block into found extent and return */ |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 1705 | if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 1706 | && ext4_can_extents_be_merged(inode, ex, newext)) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1707 | ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1708 | ext4_ext_is_uninitialized(newext), |
| 1709 | ext4_ext_get_actual_len(newext), |
| 1710 | le32_to_cpu(ex->ee_block), |
| 1711 | ext4_ext_is_uninitialized(ex), |
| 1712 | ext4_ext_get_actual_len(ex), |
| 1713 | ext4_ext_pblock(ex)); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1714 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1715 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1716 | return err; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1717 | |
| 1718 | /* |
| 1719 | * ext4_can_extents_be_merged should have checked that either |
| 1720 | * both extents are uninitialized, or both aren't. Thus we |
| 1721 | * need to check only one of them here. |
| 1722 | */ |
| 1723 | if (ext4_ext_is_uninitialized(ex)) |
| 1724 | uninitialized = 1; |
| 1725 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1726 | + ext4_ext_get_actual_len(newext)); |
| 1727 | if (uninitialized) |
| 1728 | ext4_ext_mark_uninitialized(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1729 | eh = path[depth].p_hdr; |
| 1730 | nearex = ex; |
| 1731 | goto merge; |
| 1732 | } |
| 1733 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1734 | depth = ext_depth(inode); |
| 1735 | eh = path[depth].p_hdr; |
| 1736 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 1737 | goto has_space; |
| 1738 | |
| 1739 | /* probably next leaf has space for us? */ |
| 1740 | fex = EXT_LAST_EXTENT(eh); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 1741 | next = EXT_MAX_BLOCKS; |
| 1742 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)) |
Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1743 | next = ext4_ext_next_leaf_block(path); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 1744 | if (next != EXT_MAX_BLOCKS) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1745 | ext_debug("next leaf block - %d\n", next); |
| 1746 | BUG_ON(npath != NULL); |
| 1747 | npath = ext4_ext_find_extent(inode, next, NULL); |
| 1748 | if (IS_ERR(npath)) |
| 1749 | return PTR_ERR(npath); |
| 1750 | BUG_ON(npath->p_depth != path->p_depth); |
| 1751 | eh = npath[depth].p_hdr; |
| 1752 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1753 | ext_debug("next leaf isn't full(%d)\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1754 | le16_to_cpu(eh->eh_entries)); |
| 1755 | path = npath; |
Robin Dong | ffb505f | 2011-07-11 11:43:59 -0400 | [diff] [blame] | 1756 | goto has_space; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1757 | } |
| 1758 | ext_debug("next leaf has no free space(%d,%d)\n", |
| 1759 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 1760 | } |
| 1761 | |
| 1762 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1763 | * There is no free space in the found leaf. |
| 1764 | * We're gonna add a new leaf in the tree. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1765 | */ |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1766 | if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) |
| 1767 | flags = EXT4_MB_USE_ROOT_BLOCKS; |
| 1768 | err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1769 | if (err) |
| 1770 | goto cleanup; |
| 1771 | depth = ext_depth(inode); |
| 1772 | eh = path[depth].p_hdr; |
| 1773 | |
| 1774 | has_space: |
| 1775 | nearex = path[depth].p_ext; |
| 1776 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1777 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1778 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1779 | goto cleanup; |
| 1780 | |
| 1781 | if (!nearex) { |
| 1782 | /* there is no extent in this leaf, create first one */ |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1783 | ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1784 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1785 | ext4_ext_pblock(newext), |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1786 | ext4_ext_is_uninitialized(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1787 | ext4_ext_get_actual_len(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1788 | path[depth].p_ext = EXT_FIRST_EXTENT(eh); |
| 1789 | } else if (le32_to_cpu(newext->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1790 | > le32_to_cpu(nearex->ee_block)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1791 | /* BUG_ON(newext->ee_block == nearex->ee_block); */ |
| 1792 | if (nearex != EXT_LAST_EXTENT(eh)) { |
| 1793 | len = EXT_MAX_EXTENT(eh) - nearex; |
| 1794 | len = (len - 1) * sizeof(struct ext4_extent); |
| 1795 | len = len < 0 ? 0 : len; |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1796 | ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1797 | "move %d from 0x%p to 0x%p\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1798 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1799 | ext4_ext_pblock(newext), |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1800 | ext4_ext_is_uninitialized(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1801 | ext4_ext_get_actual_len(newext), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1802 | nearex, len, nearex + 1, nearex + 2); |
| 1803 | memmove(nearex + 2, nearex + 1, len); |
| 1804 | } |
| 1805 | path[depth].p_ext = nearex + 1; |
| 1806 | } else { |
| 1807 | BUG_ON(newext->ee_block == nearex->ee_block); |
| 1808 | len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); |
| 1809 | len = len < 0 ? 0 : len; |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1810 | ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, " |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1811 | "move %d from 0x%p to 0x%p\n", |
| 1812 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1813 | ext4_ext_pblock(newext), |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1814 | ext4_ext_is_uninitialized(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1815 | ext4_ext_get_actual_len(newext), |
Robin Dong | 0737964 | 2011-07-23 21:51:07 -0400 | [diff] [blame] | 1816 | nearex, len, nearex, nearex + 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1817 | memmove(nearex + 1, nearex, len); |
| 1818 | path[depth].p_ext = nearex; |
| 1819 | } |
| 1820 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1821 | le16_add_cpu(&eh->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1822 | nearex = path[depth].p_ext; |
| 1823 | nearex->ee_block = newext->ee_block; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1824 | ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1825 | nearex->ee_len = newext->ee_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1826 | |
| 1827 | merge: |
| 1828 | /* try to merge extents to the right */ |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 1829 | if (!(flag & EXT4_GET_BLOCKS_PRE_IO)) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 1830 | ext4_ext_try_to_merge(inode, path, nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1831 | |
| 1832 | /* try to merge extents to the left */ |
| 1833 | |
| 1834 | /* time to correct all indexes above */ |
| 1835 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 1836 | if (err) |
| 1837 | goto cleanup; |
| 1838 | |
| 1839 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1840 | |
| 1841 | cleanup: |
| 1842 | if (npath) { |
| 1843 | ext4_ext_drop_refs(npath); |
| 1844 | kfree(npath); |
| 1845 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1846 | ext4_ext_invalidate_cache(inode); |
| 1847 | return err; |
| 1848 | } |
| 1849 | |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1850 | static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, |
| 1851 | ext4_lblk_t num, ext_prepare_callback func, |
| 1852 | void *cbdata) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1853 | { |
| 1854 | struct ext4_ext_path *path = NULL; |
| 1855 | struct ext4_ext_cache cbex; |
| 1856 | struct ext4_extent *ex; |
| 1857 | ext4_lblk_t next, start = 0, end = 0; |
| 1858 | ext4_lblk_t last = block + num; |
| 1859 | int depth, exists, err = 0; |
| 1860 | |
| 1861 | BUG_ON(func == NULL); |
| 1862 | BUG_ON(inode == NULL); |
| 1863 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1864 | while (block < last && block != EXT_MAX_BLOCKS) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1865 | num = last - block; |
| 1866 | /* find extent for this block */ |
Theodore Ts'o | fab3a54 | 2009-12-09 21:30:02 -0500 | [diff] [blame] | 1867 | down_read(&EXT4_I(inode)->i_data_sem); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1868 | path = ext4_ext_find_extent(inode, block, path); |
Theodore Ts'o | fab3a54 | 2009-12-09 21:30:02 -0500 | [diff] [blame] | 1869 | up_read(&EXT4_I(inode)->i_data_sem); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1870 | if (IS_ERR(path)) { |
| 1871 | err = PTR_ERR(path); |
| 1872 | path = NULL; |
| 1873 | break; |
| 1874 | } |
| 1875 | |
| 1876 | depth = ext_depth(inode); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1877 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1878 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
| 1879 | err = -EIO; |
| 1880 | break; |
| 1881 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1882 | ex = path[depth].p_ext; |
| 1883 | next = ext4_ext_next_allocated_block(path); |
| 1884 | |
| 1885 | exists = 0; |
| 1886 | if (!ex) { |
| 1887 | /* there is no extent yet, so try to allocate |
| 1888 | * all requested space */ |
| 1889 | start = block; |
| 1890 | end = block + num; |
| 1891 | } else if (le32_to_cpu(ex->ee_block) > block) { |
| 1892 | /* need to allocate space before found extent */ |
| 1893 | start = block; |
| 1894 | end = le32_to_cpu(ex->ee_block); |
| 1895 | if (block + num < end) |
| 1896 | end = block + num; |
| 1897 | } else if (block >= le32_to_cpu(ex->ee_block) |
| 1898 | + ext4_ext_get_actual_len(ex)) { |
| 1899 | /* need to allocate space after found extent */ |
| 1900 | start = block; |
| 1901 | end = block + num; |
| 1902 | if (end >= next) |
| 1903 | end = next; |
| 1904 | } else if (block >= le32_to_cpu(ex->ee_block)) { |
| 1905 | /* |
| 1906 | * some part of requested space is covered |
| 1907 | * by found extent |
| 1908 | */ |
| 1909 | start = block; |
| 1910 | end = le32_to_cpu(ex->ee_block) |
| 1911 | + ext4_ext_get_actual_len(ex); |
| 1912 | if (block + num < end) |
| 1913 | end = block + num; |
| 1914 | exists = 1; |
| 1915 | } else { |
| 1916 | BUG(); |
| 1917 | } |
| 1918 | BUG_ON(end <= start); |
| 1919 | |
| 1920 | if (!exists) { |
| 1921 | cbex.ec_block = start; |
| 1922 | cbex.ec_len = end - start; |
| 1923 | cbex.ec_start = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1924 | } else { |
| 1925 | cbex.ec_block = le32_to_cpu(ex->ee_block); |
| 1926 | cbex.ec_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1927 | cbex.ec_start = ext4_ext_pblock(ex); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1928 | } |
| 1929 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1930 | if (unlikely(cbex.ec_len == 0)) { |
| 1931 | EXT4_ERROR_INODE(inode, "cbex.ec_len == 0"); |
| 1932 | err = -EIO; |
| 1933 | break; |
| 1934 | } |
Lukas Czerner | c03f8aa | 2011-06-06 00:06:52 -0400 | [diff] [blame] | 1935 | err = func(inode, next, &cbex, ex, cbdata); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1936 | ext4_ext_drop_refs(path); |
| 1937 | |
| 1938 | if (err < 0) |
| 1939 | break; |
| 1940 | |
| 1941 | if (err == EXT_REPEAT) |
| 1942 | continue; |
| 1943 | else if (err == EXT_BREAK) { |
| 1944 | err = 0; |
| 1945 | break; |
| 1946 | } |
| 1947 | |
| 1948 | if (ext_depth(inode) != depth) { |
| 1949 | /* depth was changed. we have to realloc path */ |
| 1950 | kfree(path); |
| 1951 | path = NULL; |
| 1952 | } |
| 1953 | |
| 1954 | block = cbex.ec_block + cbex.ec_len; |
| 1955 | } |
| 1956 | |
| 1957 | if (path) { |
| 1958 | ext4_ext_drop_refs(path); |
| 1959 | kfree(path); |
| 1960 | } |
| 1961 | |
| 1962 | return err; |
| 1963 | } |
| 1964 | |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1965 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1966 | ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 1967 | __u32 len, ext4_fsblk_t start) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1968 | { |
| 1969 | struct ext4_ext_cache *cex; |
| 1970 | BUG_ON(len == 0); |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1971 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1972 | cex = &EXT4_I(inode)->i_cached_extent; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1973 | cex->ec_block = block; |
| 1974 | cex->ec_len = len; |
| 1975 | cex->ec_start = start; |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1976 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1980 | * ext4_ext_put_gap_in_cache: |
| 1981 | * calculate boundaries of the gap that the requested block fits into |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1982 | * and cache this gap |
| 1983 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1984 | static void |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1985 | 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] | 1986 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1987 | { |
| 1988 | int depth = ext_depth(inode); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1989 | unsigned long len; |
| 1990 | ext4_lblk_t lblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1991 | struct ext4_extent *ex; |
| 1992 | |
| 1993 | ex = path[depth].p_ext; |
| 1994 | if (ex == NULL) { |
| 1995 | /* there is no extent yet, so gap is [0;-] */ |
| 1996 | lblock = 0; |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1997 | len = EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1998 | ext_debug("cache gap(whole file):"); |
| 1999 | } else if (block < le32_to_cpu(ex->ee_block)) { |
| 2000 | lblock = block; |
| 2001 | len = le32_to_cpu(ex->ee_block) - block; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2002 | ext_debug("cache gap(before): %u [%u:%u]", |
| 2003 | block, |
| 2004 | le32_to_cpu(ex->ee_block), |
| 2005 | ext4_ext_get_actual_len(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2006 | } else if (block >= le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2007 | + ext4_ext_get_actual_len(ex)) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2008 | ext4_lblk_t next; |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2009 | lblock = le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2010 | + ext4_ext_get_actual_len(ex); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2011 | |
| 2012 | next = ext4_ext_next_allocated_block(path); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2013 | ext_debug("cache gap(after): [%u:%u] %u", |
| 2014 | le32_to_cpu(ex->ee_block), |
| 2015 | ext4_ext_get_actual_len(ex), |
| 2016 | block); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2017 | BUG_ON(next == lblock); |
| 2018 | len = next - lblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2019 | } else { |
| 2020 | lblock = len = 0; |
| 2021 | BUG(); |
| 2022 | } |
| 2023 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2024 | ext_debug(" -> %u:%lu\n", lblock, len); |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2025 | ext4_ext_put_in_cache(inode, lblock, len, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2028 | /* |
Robin Dong | b7ca1e8 | 2011-07-23 21:53:25 -0400 | [diff] [blame] | 2029 | * ext4_ext_check_cache() |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 2030 | * Checks to see if the given block is in the cache. |
| 2031 | * If it is, the cached extent is stored in the given |
| 2032 | * cache extent pointer. If the cached extent is a hole, |
| 2033 | * this routine should be used instead of |
| 2034 | * ext4_ext_in_cache if the calling function needs to |
| 2035 | * know the size of the hole. |
| 2036 | * |
| 2037 | * @inode: The files inode |
| 2038 | * @block: The block to look for in the cache |
| 2039 | * @ex: Pointer where the cached extent will be stored |
| 2040 | * if it contains block |
| 2041 | * |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2042 | * Return 0 if cache is invalid; 1 if the cache is valid |
| 2043 | */ |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 2044 | static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block, |
| 2045 | struct ext4_ext_cache *ex){ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2046 | struct ext4_ext_cache *cex; |
Vivek Haldar | 77f4135 | 2011-05-22 21:24:16 -0400 | [diff] [blame] | 2047 | struct ext4_sb_info *sbi; |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2048 | int ret = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2049 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 2050 | /* |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 2051 | * We borrow i_block_reservation_lock to protect i_cached_extent |
| 2052 | */ |
| 2053 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2054 | cex = &EXT4_I(inode)->i_cached_extent; |
Vivek Haldar | 77f4135 | 2011-05-22 21:24:16 -0400 | [diff] [blame] | 2055 | sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2056 | |
| 2057 | /* has cache valid data? */ |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2058 | if (cex->ec_len == 0) |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 2059 | goto errout; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2060 | |
Akinobu Mita | 731eb1a | 2010-03-03 23:55:01 -0500 | [diff] [blame] | 2061 | if (in_range(block, cex->ec_block, cex->ec_len)) { |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 2062 | memcpy(ex, cex, sizeof(struct ext4_ext_cache)); |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2063 | ext_debug("%u cached by %u:%u:%llu\n", |
| 2064 | block, |
| 2065 | cex->ec_block, cex->ec_len, cex->ec_start); |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2066 | ret = 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2067 | } |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 2068 | errout: |
Vivek Haldar | 77f4135 | 2011-05-22 21:24:16 -0400 | [diff] [blame] | 2069 | if (!ret) |
| 2070 | sbi->extent_cache_misses++; |
| 2071 | else |
| 2072 | sbi->extent_cache_hits++; |
Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 2073 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
| 2074 | return ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | /* |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 2078 | * ext4_ext_in_cache() |
| 2079 | * Checks to see if the given block is in the cache. |
| 2080 | * If it is, the cached extent is stored in the given |
| 2081 | * extent pointer. |
| 2082 | * |
| 2083 | * @inode: The files inode |
| 2084 | * @block: The block to look for in the cache |
| 2085 | * @ex: Pointer where the cached extent will be stored |
| 2086 | * if it contains block |
| 2087 | * |
| 2088 | * Return 0 if cache is invalid; 1 if the cache is valid |
| 2089 | */ |
| 2090 | static int |
| 2091 | ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, |
| 2092 | struct ext4_extent *ex) |
| 2093 | { |
| 2094 | struct ext4_ext_cache cex; |
| 2095 | int ret = 0; |
| 2096 | |
| 2097 | if (ext4_ext_check_cache(inode, block, &cex)) { |
| 2098 | ex->ee_block = cpu_to_le32(cex.ec_block); |
| 2099 | ext4_ext_store_pblock(ex, cex.ec_start); |
| 2100 | ex->ee_len = cpu_to_le16(cex.ec_len); |
| 2101 | ret = 1; |
| 2102 | } |
| 2103 | |
| 2104 | return ret; |
| 2105 | } |
| 2106 | |
| 2107 | |
| 2108 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2109 | * ext4_ext_rm_idx: |
| 2110 | * removes index from the index block. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2111 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2112 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2113 | struct ext4_ext_path *path) |
| 2114 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2115 | int err; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2116 | ext4_fsblk_t leaf; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2117 | |
| 2118 | /* free index block */ |
| 2119 | path--; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2120 | leaf = ext4_idx_pblock(path->p_idx); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2121 | if (unlikely(path->p_hdr->eh_entries == 0)) { |
| 2122 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); |
| 2123 | return -EIO; |
| 2124 | } |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2125 | err = ext4_ext_get_access(handle, inode, path); |
| 2126 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2127 | return err; |
Robin Dong | 0e1147b | 2011-07-27 21:29:33 -0400 | [diff] [blame] | 2128 | |
| 2129 | if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { |
| 2130 | int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; |
| 2131 | len *= sizeof(struct ext4_extent_idx); |
| 2132 | memmove(path->p_idx, path->p_idx + 1, len); |
| 2133 | } |
| 2134 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2135 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2136 | err = ext4_ext_dirty(handle, inode, path); |
| 2137 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2138 | return err; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2139 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 2140 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2141 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2142 | return err; |
| 2143 | } |
| 2144 | |
| 2145 | /* |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2146 | * ext4_ext_calc_credits_for_single_extent: |
| 2147 | * This routine returns max. credits that needed to insert an extent |
| 2148 | * to the extent tree. |
| 2149 | * When pass the actual path, the caller should calculate credits |
| 2150 | * under i_data_sem. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2151 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2152 | 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] | 2153 | struct ext4_ext_path *path) |
| 2154 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2155 | if (path) { |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2156 | int depth = ext_depth(inode); |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 2157 | int ret = 0; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2158 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2159 | /* probably there is space in leaf? */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2160 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2161 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
| 2162 | |
| 2163 | /* |
| 2164 | * There are some space in the leaf tree, no |
| 2165 | * need to account for leaf block credit |
| 2166 | * |
| 2167 | * bitmaps and block group descriptor blocks |
| 2168 | * and other metadat blocks still need to be |
| 2169 | * accounted. |
| 2170 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2171 | /* 1 bitmap, 1 block group descriptor */ |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2172 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Aneesh Kumar K.V | 5887e98 | 2009-07-05 23:12:04 -0400 | [diff] [blame] | 2173 | return ret; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2174 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2177 | return ext4_chunk_trans_blocks(inode, nrblocks); |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2178 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2179 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2180 | /* |
| 2181 | * How many index/leaf blocks need to change/allocate to modify nrblocks? |
| 2182 | * |
| 2183 | * if nrblocks are fit in a single extent (chunk flag is 1), then |
| 2184 | * in the worse case, each tree level index/leaf need to be changed |
| 2185 | * if the tree split due to insert a new extent, then the old tree |
| 2186 | * index/leaf need to be updated too |
| 2187 | * |
| 2188 | * If the nrblocks are discontiguous, they could cause |
| 2189 | * the whole tree split more than once, but this is really rare. |
| 2190 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2191 | 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] | 2192 | { |
| 2193 | int index; |
| 2194 | int depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2195 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2196 | if (chunk) |
| 2197 | index = depth * 2; |
| 2198 | else |
| 2199 | index = depth * 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2200 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2201 | return index; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2205 | struct ext4_extent *ex, |
| 2206 | ext4_fsblk_t *partial_cluster, |
| 2207 | ext4_lblk_t from, ext4_lblk_t to) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2208 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2209 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2210 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2211 | ext4_fsblk_t pblk; |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2212 | int flags = EXT4_FREE_BLOCKS_FORGET; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2213 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2214 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2215 | flags |= EXT4_FREE_BLOCKS_METADATA; |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2216 | /* |
| 2217 | * For bigalloc file systems, we never free a partial cluster |
| 2218 | * at the beginning of the extent. Instead, we make a note |
| 2219 | * that we tried freeing the cluster, and check to see if we |
| 2220 | * need to free it on a subsequent call to ext4_remove_blocks, |
| 2221 | * or at the end of the ext4_truncate() operation. |
| 2222 | */ |
| 2223 | flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; |
| 2224 | |
| 2225 | /* |
| 2226 | * If we have a partial cluster, and it's different from the |
| 2227 | * cluster of the last block, we need to explicitly free the |
| 2228 | * partial cluster here. |
| 2229 | */ |
| 2230 | pblk = ext4_ext_pblock(ex) + ee_len - 1; |
| 2231 | if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) { |
| 2232 | ext4_free_blocks(handle, inode, NULL, |
| 2233 | EXT4_C2B(sbi, *partial_cluster), |
| 2234 | sbi->s_cluster_ratio, flags); |
| 2235 | *partial_cluster = 0; |
| 2236 | } |
| 2237 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2238 | #ifdef EXTENTS_STATS |
| 2239 | { |
| 2240 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2241 | spin_lock(&sbi->s_ext_stats_lock); |
| 2242 | sbi->s_ext_blocks += ee_len; |
| 2243 | sbi->s_ext_extents++; |
| 2244 | if (ee_len < sbi->s_ext_min) |
| 2245 | sbi->s_ext_min = ee_len; |
| 2246 | if (ee_len > sbi->s_ext_max) |
| 2247 | sbi->s_ext_max = ee_len; |
| 2248 | if (ext_depth(inode) > sbi->s_depth_max) |
| 2249 | sbi->s_depth_max = ext_depth(inode); |
| 2250 | spin_unlock(&sbi->s_ext_stats_lock); |
| 2251 | } |
| 2252 | #endif |
| 2253 | if (from >= le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2254 | && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2255 | /* tail removal */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2256 | ext4_lblk_t num; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2257 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2258 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2259 | pblk = ext4_ext_pblock(ex) + ee_len - num; |
| 2260 | ext_debug("free last %u blocks starting %llu\n", num, pblk); |
| 2261 | ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
| 2262 | /* |
| 2263 | * If the block range to be freed didn't start at the |
| 2264 | * beginning of a cluster, and we removed the entire |
| 2265 | * extent, save the partial cluster here, since we |
| 2266 | * might need to delete if we determine that the |
| 2267 | * truncate operation has removed all of the blocks in |
| 2268 | * the cluster. |
| 2269 | */ |
| 2270 | if (pblk & (sbi->s_cluster_ratio - 1) && |
| 2271 | (ee_len == num)) |
| 2272 | *partial_cluster = EXT4_B2C(sbi, pblk); |
| 2273 | else |
| 2274 | *partial_cluster = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2275 | } else if (from == le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2276 | && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2277 | /* head removal */ |
| 2278 | ext4_lblk_t num; |
| 2279 | ext4_fsblk_t start; |
| 2280 | |
| 2281 | num = to - from; |
| 2282 | start = ext4_ext_pblock(ex); |
| 2283 | |
| 2284 | ext_debug("free first %u blocks starting %llu\n", num, start); |
| 2285 | ext4_free_blocks(handle, inode, 0, start, num, flags); |
| 2286 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2287 | } else { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2288 | printk(KERN_INFO "strange request: removal(2) " |
| 2289 | "%u-%u from %u:%u\n", |
| 2290 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2291 | } |
| 2292 | return 0; |
| 2293 | } |
| 2294 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2295 | |
| 2296 | /* |
| 2297 | * ext4_ext_rm_leaf() Removes the extents associated with the |
| 2298 | * blocks appearing between "start" and "end", and splits the extents |
| 2299 | * if "start" and "end" appear in the same extent |
| 2300 | * |
| 2301 | * @handle: The journal handle |
| 2302 | * @inode: The files inode |
| 2303 | * @path: The path to the leaf |
| 2304 | * @start: The first block to remove |
| 2305 | * @end: The last block to remove |
| 2306 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2307 | static int |
| 2308 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2309 | struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster, |
| 2310 | ext4_lblk_t start, ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2311 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2312 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2313 | int err = 0, correct_index = 0; |
| 2314 | int depth = ext_depth(inode), credits; |
| 2315 | struct ext4_extent_header *eh; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2316 | ext4_lblk_t a, b, block; |
| 2317 | unsigned num; |
| 2318 | ext4_lblk_t ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2319 | unsigned short ex_ee_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2320 | unsigned uninitialized = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2321 | struct ext4_extent *ex; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2322 | struct ext4_map_blocks map; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2323 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2324 | /* 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] | 2325 | ext_debug("truncate since %u in leaf\n", start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2326 | if (!path[depth].p_hdr) |
| 2327 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 2328 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2329 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 2330 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
| 2331 | return -EIO; |
| 2332 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2333 | /* find where to start removing */ |
| 2334 | ex = EXT_LAST_EXTENT(eh); |
| 2335 | |
| 2336 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2337 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2338 | |
| 2339 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 2340 | ex_ee_block + ex_ee_len > start) { |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2341 | |
| 2342 | if (ext4_ext_is_uninitialized(ex)) |
| 2343 | uninitialized = 1; |
| 2344 | else |
| 2345 | uninitialized = 0; |
| 2346 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 2347 | ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, |
| 2348 | uninitialized, ex_ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2349 | path[depth].p_ext = ex; |
| 2350 | |
| 2351 | a = ex_ee_block > start ? ex_ee_block : start; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2352 | b = ex_ee_block+ex_ee_len - 1 < end ? |
| 2353 | ex_ee_block+ex_ee_len - 1 : end; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2354 | |
| 2355 | ext_debug(" border %u:%u\n", a, b); |
| 2356 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2357 | /* If this extent is beyond the end of the hole, skip it */ |
| 2358 | if (end <= ex_ee_block) { |
| 2359 | ex--; |
| 2360 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2361 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2362 | continue; |
| 2363 | } else if (a != ex_ee_block && |
| 2364 | b != ex_ee_block + ex_ee_len - 1) { |
| 2365 | /* |
| 2366 | * If this is a truncate, then this condition should |
| 2367 | * never happen because at least one of the end points |
| 2368 | * needs to be on the edge of the extent. |
| 2369 | */ |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2370 | if (end == EXT_MAX_BLOCKS - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2371 | ext_debug(" bad truncate %u:%u\n", |
| 2372 | start, end); |
| 2373 | block = 0; |
| 2374 | num = 0; |
| 2375 | err = -EIO; |
| 2376 | goto out; |
| 2377 | } |
| 2378 | /* |
| 2379 | * else this is a hole punch, so the extent needs to |
| 2380 | * be split since neither edge of the hole is on the |
| 2381 | * extent edge |
| 2382 | */ |
| 2383 | else{ |
| 2384 | map.m_pblk = ext4_ext_pblock(ex); |
| 2385 | map.m_lblk = ex_ee_block; |
| 2386 | map.m_len = b - ex_ee_block; |
| 2387 | |
| 2388 | err = ext4_split_extent(handle, |
| 2389 | inode, path, &map, 0, |
| 2390 | EXT4_GET_BLOCKS_PUNCH_OUT_EXT | |
| 2391 | EXT4_GET_BLOCKS_PRE_IO); |
| 2392 | |
| 2393 | if (err < 0) |
| 2394 | goto out; |
| 2395 | |
| 2396 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2397 | |
| 2398 | b = ex_ee_block+ex_ee_len - 1 < end ? |
| 2399 | ex_ee_block+ex_ee_len - 1 : end; |
| 2400 | |
| 2401 | /* Then remove tail of this extent */ |
| 2402 | block = ex_ee_block; |
| 2403 | num = a - block; |
| 2404 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2405 | } else if (a != ex_ee_block) { |
| 2406 | /* remove tail of the extent */ |
| 2407 | block = ex_ee_block; |
| 2408 | num = a - block; |
| 2409 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
| 2410 | /* remove head of the extent */ |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2411 | block = b; |
| 2412 | num = ex_ee_block + ex_ee_len - b; |
| 2413 | |
| 2414 | /* |
| 2415 | * If this is a truncate, this condition |
| 2416 | * should never happen |
| 2417 | */ |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2418 | if (end == EXT_MAX_BLOCKS - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2419 | ext_debug(" bad truncate %u:%u\n", |
| 2420 | start, end); |
| 2421 | err = -EIO; |
| 2422 | goto out; |
| 2423 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2424 | } else { |
| 2425 | /* remove whole extent: excellent! */ |
| 2426 | block = ex_ee_block; |
| 2427 | num = 0; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2428 | if (a != ex_ee_block) { |
| 2429 | ext_debug(" bad truncate %u:%u\n", |
| 2430 | start, end); |
| 2431 | err = -EIO; |
| 2432 | goto out; |
| 2433 | } |
| 2434 | |
| 2435 | if (b != ex_ee_block + ex_ee_len - 1) { |
| 2436 | ext_debug(" bad truncate %u:%u\n", |
| 2437 | start, end); |
| 2438 | err = -EIO; |
| 2439 | goto out; |
| 2440 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 2443 | /* |
| 2444 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 2445 | * descriptor) for each block group; assume two block |
| 2446 | * groups plus ex_ee_len/blocks_per_block_group for |
| 2447 | * the worst case |
| 2448 | */ |
| 2449 | 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] | 2450 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 2451 | correct_index = 1; |
| 2452 | credits += (ext_depth(inode)) + 1; |
| 2453 | } |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2454 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2455 | |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 2456 | err = ext4_ext_truncate_extend_restart(handle, inode, credits); |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2457 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2458 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2459 | |
| 2460 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2461 | if (err) |
| 2462 | goto out; |
| 2463 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2464 | err = ext4_remove_blocks(handle, inode, ex, partial_cluster, |
| 2465 | a, b); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2466 | if (err) |
| 2467 | goto out; |
| 2468 | |
| 2469 | if (num == 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2470 | /* this extent is removed; mark slot entirely unused */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2471 | ext4_ext_store_pblock(ex, 0); |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2472 | } else if (block != ex_ee_block) { |
| 2473 | /* |
| 2474 | * If this was a head removal, then we need to update |
| 2475 | * the physical block since it is now at a different |
| 2476 | * location |
| 2477 | */ |
| 2478 | ext4_ext_store_pblock(ex, ext4_ext_pblock(ex) + (b-a)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | ex->ee_block = cpu_to_le32(block); |
| 2482 | ex->ee_len = cpu_to_le16(num); |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2483 | /* |
| 2484 | * Do not mark uninitialized if all the blocks in the |
| 2485 | * extent have been removed. |
| 2486 | */ |
| 2487 | if (uninitialized && num) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2488 | ext4_ext_mark_uninitialized(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2489 | |
| 2490 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2491 | if (err) |
| 2492 | goto out; |
| 2493 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2494 | /* |
| 2495 | * If the extent was completely released, |
| 2496 | * we need to remove it from the leaf |
| 2497 | */ |
| 2498 | if (num == 0) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2499 | if (end != EXT_MAX_BLOCKS - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2500 | /* |
| 2501 | * For hole punching, we need to scoot all the |
| 2502 | * extents up when an extent is removed so that |
| 2503 | * we dont have blank extents in the middle |
| 2504 | */ |
| 2505 | memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * |
| 2506 | sizeof(struct ext4_extent)); |
| 2507 | |
| 2508 | /* Now get rid of the one at the end */ |
| 2509 | memset(EXT_LAST_EXTENT(eh), 0, |
| 2510 | sizeof(struct ext4_extent)); |
| 2511 | } |
| 2512 | le16_add_cpu(&eh->eh_entries, -1); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2513 | } else |
| 2514 | *partial_cluster = 0; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2515 | |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2516 | ext_debug("new extent: %u:%u:%llu\n", block, num, |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2517 | ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2518 | ex--; |
| 2519 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2520 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | if (correct_index && eh->eh_entries) |
| 2524 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2525 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2526 | /* |
| 2527 | * If there is still a entry in the leaf node, check to see if |
| 2528 | * it references the partial cluster. This is the only place |
| 2529 | * where it could; if it doesn't, we can free the cluster. |
| 2530 | */ |
| 2531 | if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) && |
| 2532 | (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) != |
| 2533 | *partial_cluster)) { |
| 2534 | int flags = EXT4_FREE_BLOCKS_FORGET; |
| 2535 | |
| 2536 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 2537 | flags |= EXT4_FREE_BLOCKS_METADATA; |
| 2538 | |
| 2539 | ext4_free_blocks(handle, inode, NULL, |
| 2540 | EXT4_C2B(sbi, *partial_cluster), |
| 2541 | sbi->s_cluster_ratio, flags); |
| 2542 | *partial_cluster = 0; |
| 2543 | } |
| 2544 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2545 | /* if this leaf is free, then we should |
| 2546 | * remove it from index block above */ |
| 2547 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
| 2548 | err = ext4_ext_rm_idx(handle, inode, path + depth); |
| 2549 | |
| 2550 | out: |
| 2551 | return err; |
| 2552 | } |
| 2553 | |
| 2554 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2555 | * ext4_ext_more_to_rm: |
| 2556 | * returns 1 if current index has to be freed (even partial) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2557 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2558 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2559 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 2560 | { |
| 2561 | BUG_ON(path->p_idx == NULL); |
| 2562 | |
| 2563 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 2564 | return 0; |
| 2565 | |
| 2566 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2567 | * if truncate on deeper level happened, it wasn't partial, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2568 | * so we have to consider current index for truncation |
| 2569 | */ |
| 2570 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 2571 | return 0; |
| 2572 | return 1; |
| 2573 | } |
| 2574 | |
Allison Henderson | c6a0371 | 2011-07-17 23:21:03 -0400 | [diff] [blame] | 2575 | 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] | 2576 | { |
| 2577 | struct super_block *sb = inode->i_sb; |
| 2578 | int depth = ext_depth(inode); |
| 2579 | struct ext4_ext_path *path; |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2580 | ext4_fsblk_t partial_cluster = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2581 | handle_t *handle; |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2582 | int i, err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2583 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2584 | ext_debug("truncate since %u\n", start); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2585 | |
| 2586 | /* probably first extent we're gonna free will be last in block */ |
| 2587 | handle = ext4_journal_start(inode, depth + 1); |
| 2588 | if (IS_ERR(handle)) |
| 2589 | return PTR_ERR(handle); |
| 2590 | |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2591 | again: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2592 | ext4_ext_invalidate_cache(inode); |
| 2593 | |
| 2594 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2595 | * We start scanning from right side, freeing all the blocks |
| 2596 | * after i_size and walking into the tree depth-wise. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2597 | */ |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2598 | depth = ext_depth(inode); |
Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 2599 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2600 | if (path == NULL) { |
| 2601 | ext4_journal_stop(handle); |
| 2602 | return -ENOMEM; |
| 2603 | } |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2604 | path[0].p_depth = depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2605 | path[0].p_hdr = ext_inode_hdr(inode); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 2606 | if (ext4_ext_check(inode, path[0].p_hdr, depth)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2607 | err = -EIO; |
| 2608 | goto out; |
| 2609 | } |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2610 | i = err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2611 | |
| 2612 | while (i >= 0 && err == 0) { |
| 2613 | if (i == depth) { |
| 2614 | /* this is leaf block */ |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2615 | err = ext4_ext_rm_leaf(handle, inode, path, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 2616 | &partial_cluster, start, |
| 2617 | EXT_MAX_BLOCKS - 1); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2618 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2619 | brelse(path[i].p_bh); |
| 2620 | path[i].p_bh = NULL; |
| 2621 | i--; |
| 2622 | continue; |
| 2623 | } |
| 2624 | |
| 2625 | /* this is index block */ |
| 2626 | if (!path[i].p_hdr) { |
| 2627 | ext_debug("initialize header\n"); |
| 2628 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2629 | } |
| 2630 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2631 | if (!path[i].p_idx) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2632 | /* this level hasn't been touched yet */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2633 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 2634 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
| 2635 | ext_debug("init index ptr: hdr 0x%p, num %d\n", |
| 2636 | path[i].p_hdr, |
| 2637 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 2638 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2639 | /* we were already here, see at next index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2640 | path[i].p_idx--; |
| 2641 | } |
| 2642 | |
| 2643 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
| 2644 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 2645 | path[i].p_idx); |
| 2646 | if (ext4_ext_more_to_rm(path + i)) { |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2647 | struct buffer_head *bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2648 | /* go to the next level */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2649 | ext_debug("move to level %d (block %llu)\n", |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2650 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2651 | memset(path + i + 1, 0, sizeof(*path)); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2652 | bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx)); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2653 | if (!bh) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2654 | /* should we reset i_size? */ |
| 2655 | err = -EIO; |
| 2656 | break; |
| 2657 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2658 | if (WARN_ON(i + 1 > depth)) { |
| 2659 | err = -EIO; |
| 2660 | break; |
| 2661 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 2662 | if (ext4_ext_check(inode, ext_block_hdr(bh), |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2663 | depth - i - 1)) { |
| 2664 | err = -EIO; |
| 2665 | break; |
| 2666 | } |
| 2667 | path[i + 1].p_bh = bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2668 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2669 | /* save actual number of indexes since this |
| 2670 | * number is changed at the next iteration */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2671 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 2672 | i++; |
| 2673 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2674 | /* we finished processing this index, go up */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2675 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2676 | /* index is empty, remove it; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2677 | * handle must be already prepared by the |
| 2678 | * truncatei_leaf() */ |
| 2679 | err = ext4_ext_rm_idx(handle, inode, path + i); |
| 2680 | } |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2681 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2682 | brelse(path[i].p_bh); |
| 2683 | path[i].p_bh = NULL; |
| 2684 | i--; |
| 2685 | ext_debug("return to level %d\n", i); |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | /* TODO: flexible tree reduction should be here */ |
| 2690 | if (path->p_hdr->eh_entries == 0) { |
| 2691 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2692 | * truncate to zero freed all the tree, |
| 2693 | * so we need to correct eh_depth |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2694 | */ |
| 2695 | err = ext4_ext_get_access(handle, inode, path); |
| 2696 | if (err == 0) { |
| 2697 | ext_inode_hdr(inode)->eh_depth = 0; |
| 2698 | ext_inode_hdr(inode)->eh_max = |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 2699 | cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2700 | err = ext4_ext_dirty(handle, inode, path); |
| 2701 | } |
| 2702 | } |
| 2703 | out: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2704 | ext4_ext_drop_refs(path); |
| 2705 | kfree(path); |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2706 | if (err == -EAGAIN) |
| 2707 | goto again; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2708 | ext4_journal_stop(handle); |
| 2709 | |
| 2710 | return err; |
| 2711 | } |
| 2712 | |
| 2713 | /* |
| 2714 | * called at mount time |
| 2715 | */ |
| 2716 | void ext4_ext_init(struct super_block *sb) |
| 2717 | { |
| 2718 | /* |
| 2719 | * possible initialization would be here |
| 2720 | */ |
| 2721 | |
Theodore Ts'o | 83982b6 | 2009-01-06 14:53:16 -0500 | [diff] [blame] | 2722 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 2723 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2724 | printk(KERN_INFO "EXT4-fs: file extents enabled"); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 2725 | #ifdef AGGRESSIVE_TEST |
| 2726 | printk(", aggressive tests"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2727 | #endif |
| 2728 | #ifdef CHECK_BINSEARCH |
| 2729 | printk(", check binsearch"); |
| 2730 | #endif |
| 2731 | #ifdef EXTENTS_STATS |
| 2732 | printk(", stats"); |
| 2733 | #endif |
| 2734 | printk("\n"); |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 2735 | #endif |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2736 | #ifdef EXTENTS_STATS |
| 2737 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 2738 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 2739 | EXT4_SB(sb)->s_ext_max = 0; |
| 2740 | #endif |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | /* |
| 2745 | * called at umount time |
| 2746 | */ |
| 2747 | void ext4_ext_release(struct super_block *sb) |
| 2748 | { |
Theodore Ts'o | 83982b6 | 2009-01-06 14:53:16 -0500 | [diff] [blame] | 2749 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2750 | return; |
| 2751 | |
| 2752 | #ifdef EXTENTS_STATS |
| 2753 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 2754 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 2755 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 2756 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 2757 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 2758 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 2759 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 2760 | } |
| 2761 | #endif |
| 2762 | } |
| 2763 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2764 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 2765 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 2766 | { |
Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 2767 | ext4_fsblk_t ee_pblock; |
| 2768 | unsigned int ee_len; |
Jing Zhang | b720303 | 2010-05-12 00:00:00 -0400 | [diff] [blame] | 2769 | int ret; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2770 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2771 | ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2772 | ee_pblock = ext4_ext_pblock(ex); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2773 | |
Theodore Ts'o | a107e5a | 2010-10-27 23:44:47 -0400 | [diff] [blame] | 2774 | ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS); |
Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 2775 | if (ret > 0) |
| 2776 | ret = 0; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2777 | |
Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 2778 | return ret; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2779 | } |
| 2780 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 2781 | /* |
| 2782 | * used by extent splitting. |
| 2783 | */ |
| 2784 | #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ |
| 2785 | due to ENOSPC */ |
| 2786 | #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */ |
| 2787 | #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */ |
| 2788 | |
| 2789 | /* |
| 2790 | * ext4_split_extent_at() splits an extent at given block. |
| 2791 | * |
| 2792 | * @handle: the journal handle |
| 2793 | * @inode: the file inode |
| 2794 | * @path: the path to the extent |
| 2795 | * @split: the logical block where the extent is splitted. |
| 2796 | * @split_flags: indicates if the extent could be zeroout if split fails, and |
| 2797 | * the states(init or uninit) of new extents. |
| 2798 | * @flags: flags used to insert new extent to extent tree. |
| 2799 | * |
| 2800 | * |
| 2801 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states |
| 2802 | * of which are deterimined by split_flag. |
| 2803 | * |
| 2804 | * There are two cases: |
| 2805 | * a> the extent are splitted into two extent. |
| 2806 | * b> split is not needed, and just mark the extent. |
| 2807 | * |
| 2808 | * return 0 on success. |
| 2809 | */ |
| 2810 | static int ext4_split_extent_at(handle_t *handle, |
| 2811 | struct inode *inode, |
| 2812 | struct ext4_ext_path *path, |
| 2813 | ext4_lblk_t split, |
| 2814 | int split_flag, |
| 2815 | int flags) |
| 2816 | { |
| 2817 | ext4_fsblk_t newblock; |
| 2818 | ext4_lblk_t ee_block; |
| 2819 | struct ext4_extent *ex, newex, orig_ex; |
| 2820 | struct ext4_extent *ex2 = NULL; |
| 2821 | unsigned int ee_len, depth; |
| 2822 | int err = 0; |
| 2823 | |
| 2824 | ext_debug("ext4_split_extents_at: inode %lu, logical" |
| 2825 | "block %llu\n", inode->i_ino, (unsigned long long)split); |
| 2826 | |
| 2827 | ext4_ext_show_leaf(inode, path); |
| 2828 | |
| 2829 | depth = ext_depth(inode); |
| 2830 | ex = path[depth].p_ext; |
| 2831 | ee_block = le32_to_cpu(ex->ee_block); |
| 2832 | ee_len = ext4_ext_get_actual_len(ex); |
| 2833 | newblock = split - ee_block + ext4_ext_pblock(ex); |
| 2834 | |
| 2835 | BUG_ON(split < ee_block || split >= (ee_block + ee_len)); |
| 2836 | |
| 2837 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2838 | if (err) |
| 2839 | goto out; |
| 2840 | |
| 2841 | if (split == ee_block) { |
| 2842 | /* |
| 2843 | * case b: block @split is the block that the extent begins with |
| 2844 | * then we just change the state of the extent, and splitting |
| 2845 | * is not needed. |
| 2846 | */ |
| 2847 | if (split_flag & EXT4_EXT_MARK_UNINIT2) |
| 2848 | ext4_ext_mark_uninitialized(ex); |
| 2849 | else |
| 2850 | ext4_ext_mark_initialized(ex); |
| 2851 | |
| 2852 | if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) |
| 2853 | ext4_ext_try_to_merge(inode, path, ex); |
| 2854 | |
| 2855 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2856 | goto out; |
| 2857 | } |
| 2858 | |
| 2859 | /* case a */ |
| 2860 | memcpy(&orig_ex, ex, sizeof(orig_ex)); |
| 2861 | ex->ee_len = cpu_to_le16(split - ee_block); |
| 2862 | if (split_flag & EXT4_EXT_MARK_UNINIT1) |
| 2863 | ext4_ext_mark_uninitialized(ex); |
| 2864 | |
| 2865 | /* |
| 2866 | * path may lead to new leaf, not to original leaf any more |
| 2867 | * after ext4_ext_insert_extent() returns, |
| 2868 | */ |
| 2869 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2870 | if (err) |
| 2871 | goto fix_extent_len; |
| 2872 | |
| 2873 | ex2 = &newex; |
| 2874 | ex2->ee_block = cpu_to_le32(split); |
| 2875 | ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); |
| 2876 | ext4_ext_store_pblock(ex2, newblock); |
| 2877 | if (split_flag & EXT4_EXT_MARK_UNINIT2) |
| 2878 | ext4_ext_mark_uninitialized(ex2); |
| 2879 | |
| 2880 | err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); |
| 2881 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
| 2882 | err = ext4_ext_zeroout(inode, &orig_ex); |
| 2883 | if (err) |
| 2884 | goto fix_extent_len; |
| 2885 | /* update the extent length and mark as initialized */ |
| 2886 | ex->ee_len = cpu_to_le32(ee_len); |
| 2887 | ext4_ext_try_to_merge(inode, path, ex); |
| 2888 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2889 | goto out; |
| 2890 | } else if (err) |
| 2891 | goto fix_extent_len; |
| 2892 | |
| 2893 | out: |
| 2894 | ext4_ext_show_leaf(inode, path); |
| 2895 | return err; |
| 2896 | |
| 2897 | fix_extent_len: |
| 2898 | ex->ee_len = orig_ex.ee_len; |
| 2899 | ext4_ext_dirty(handle, inode, path + depth); |
| 2900 | return err; |
| 2901 | } |
| 2902 | |
| 2903 | /* |
| 2904 | * ext4_split_extents() splits an extent and mark extent which is covered |
| 2905 | * by @map as split_flags indicates |
| 2906 | * |
| 2907 | * It may result in splitting the extent into multiple extents (upto three) |
| 2908 | * There are three possibilities: |
| 2909 | * a> There is no split required |
| 2910 | * b> Splits in two extents: Split is happening at either end of the extent |
| 2911 | * c> Splits in three extents: Somone is splitting in middle of the extent |
| 2912 | * |
| 2913 | */ |
| 2914 | static int ext4_split_extent(handle_t *handle, |
| 2915 | struct inode *inode, |
| 2916 | struct ext4_ext_path *path, |
| 2917 | struct ext4_map_blocks *map, |
| 2918 | int split_flag, |
| 2919 | int flags) |
| 2920 | { |
| 2921 | ext4_lblk_t ee_block; |
| 2922 | struct ext4_extent *ex; |
| 2923 | unsigned int ee_len, depth; |
| 2924 | int err = 0; |
| 2925 | int uninitialized; |
| 2926 | int split_flag1, flags1; |
| 2927 | |
| 2928 | depth = ext_depth(inode); |
| 2929 | ex = path[depth].p_ext; |
| 2930 | ee_block = le32_to_cpu(ex->ee_block); |
| 2931 | ee_len = ext4_ext_get_actual_len(ex); |
| 2932 | uninitialized = ext4_ext_is_uninitialized(ex); |
| 2933 | |
| 2934 | if (map->m_lblk + map->m_len < ee_block + ee_len) { |
| 2935 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ? |
| 2936 | EXT4_EXT_MAY_ZEROOUT : 0; |
| 2937 | flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; |
| 2938 | if (uninitialized) |
| 2939 | split_flag1 |= EXT4_EXT_MARK_UNINIT1 | |
| 2940 | EXT4_EXT_MARK_UNINIT2; |
| 2941 | err = ext4_split_extent_at(handle, inode, path, |
| 2942 | map->m_lblk + map->m_len, split_flag1, flags1); |
Yongqiang Yang | 9391741 | 2011-05-22 20:49:12 -0400 | [diff] [blame] | 2943 | if (err) |
| 2944 | goto out; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | ext4_ext_drop_refs(path); |
| 2948 | path = ext4_ext_find_extent(inode, map->m_lblk, path); |
| 2949 | if (IS_ERR(path)) |
| 2950 | return PTR_ERR(path); |
| 2951 | |
| 2952 | if (map->m_lblk >= ee_block) { |
| 2953 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ? |
| 2954 | EXT4_EXT_MAY_ZEROOUT : 0; |
| 2955 | if (uninitialized) |
| 2956 | split_flag1 |= EXT4_EXT_MARK_UNINIT1; |
| 2957 | if (split_flag & EXT4_EXT_MARK_UNINIT2) |
| 2958 | split_flag1 |= EXT4_EXT_MARK_UNINIT2; |
| 2959 | err = ext4_split_extent_at(handle, inode, path, |
| 2960 | map->m_lblk, split_flag1, flags); |
| 2961 | if (err) |
| 2962 | goto out; |
| 2963 | } |
| 2964 | |
| 2965 | ext4_ext_show_leaf(inode, path); |
| 2966 | out: |
| 2967 | return err ? err : map->m_len; |
| 2968 | } |
| 2969 | |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2970 | #define EXT4_EXT_ZERO_LEN 7 |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2971 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 2972 | * 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] | 2973 | * to an uninitialized extent. It may result in splitting the uninitialized |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2974 | * extent into multiple extents (up to three - one initialized and two |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2975 | * uninitialized). |
| 2976 | * There are three possibilities: |
| 2977 | * a> There is no split required: Entire extent should be initialized |
| 2978 | * b> Splits in two extents: Write is happening at either end of the extent |
| 2979 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 2980 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2981 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 2982 | struct inode *inode, |
| 2983 | struct ext4_map_blocks *map, |
| 2984 | struct ext4_ext_path *path) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2985 | { |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 2986 | struct ext4_map_blocks split_map; |
| 2987 | struct ext4_extent zero_ex; |
| 2988 | struct ext4_extent *ex; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 2989 | ext4_lblk_t ee_block, eof_block; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2990 | unsigned int allocated, ee_len, depth; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2991 | int err = 0; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 2992 | int split_flag = 0; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 2993 | |
| 2994 | ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" |
| 2995 | "block %llu, max_blocks %u\n", inode->i_ino, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 2996 | (unsigned long long)map->m_lblk, map->m_len); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 2997 | |
| 2998 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 2999 | inode->i_sb->s_blocksize_bits; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3000 | if (eof_block < map->m_lblk + map->m_len) |
| 3001 | eof_block = map->m_lblk + map->m_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3002 | |
| 3003 | depth = ext_depth(inode); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3004 | ex = path[depth].p_ext; |
| 3005 | ee_block = le32_to_cpu(ex->ee_block); |
| 3006 | ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3007 | allocated = ee_len - (map->m_lblk - ee_block); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3008 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3009 | WARN_ON(map->m_lblk < ee_block); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3010 | /* |
| 3011 | * It is safe to convert extent to initialized via explicit |
| 3012 | * zeroout only if extent is fully insde i_size or new_size. |
| 3013 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3014 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3015 | |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3016 | /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3017 | if (ee_len <= 2*EXT4_EXT_ZERO_LEN && |
| 3018 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
| 3019 | err = ext4_ext_zeroout(inode, ex); |
Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3020 | if (err) |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 3021 | goto out; |
Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 3022 | |
| 3023 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3024 | if (err) |
| 3025 | goto out; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3026 | ext4_ext_mark_initialized(ex); |
| 3027 | ext4_ext_try_to_merge(inode, path, ex); |
| 3028 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3029 | goto out; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3030 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3031 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3032 | /* |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3033 | * four cases: |
| 3034 | * 1. split the extent into three extents. |
| 3035 | * 2. split the extent into two extents, zeroout the first half. |
| 3036 | * 3. split the extent into two extents, zeroout the second half. |
| 3037 | * 4. split the extent into two extents with out zeroout. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3038 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3039 | split_map.m_lblk = map->m_lblk; |
| 3040 | split_map.m_len = map->m_len; |
| 3041 | |
| 3042 | if (allocated > map->m_len) { |
| 3043 | if (allocated <= EXT4_EXT_ZERO_LEN && |
| 3044 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
| 3045 | /* case 3 */ |
| 3046 | zero_ex.ee_block = |
Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3047 | cpu_to_le32(map->m_lblk); |
| 3048 | zero_ex.ee_len = cpu_to_le16(allocated); |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3049 | ext4_ext_store_pblock(&zero_ex, |
| 3050 | ext4_ext_pblock(ex) + map->m_lblk - ee_block); |
| 3051 | err = ext4_ext_zeroout(inode, &zero_ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3052 | if (err) |
| 3053 | goto out; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3054 | split_map.m_lblk = map->m_lblk; |
| 3055 | split_map.m_len = allocated; |
| 3056 | } else if ((map->m_lblk - ee_block + map->m_len < |
| 3057 | EXT4_EXT_ZERO_LEN) && |
| 3058 | (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
| 3059 | /* case 2 */ |
| 3060 | if (map->m_lblk != ee_block) { |
| 3061 | zero_ex.ee_block = ex->ee_block; |
| 3062 | zero_ex.ee_len = cpu_to_le16(map->m_lblk - |
| 3063 | ee_block); |
| 3064 | ext4_ext_store_pblock(&zero_ex, |
| 3065 | ext4_ext_pblock(ex)); |
| 3066 | err = ext4_ext_zeroout(inode, &zero_ex); |
| 3067 | if (err) |
| 3068 | goto out; |
| 3069 | } |
| 3070 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3071 | split_map.m_lblk = ee_block; |
Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3072 | split_map.m_len = map->m_lblk - ee_block + map->m_len; |
| 3073 | allocated = map->m_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3074 | } |
| 3075 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3076 | |
| 3077 | allocated = ext4_split_extent(handle, inode, path, |
| 3078 | &split_map, split_flag, 0); |
| 3079 | if (allocated < 0) |
| 3080 | err = allocated; |
| 3081 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3082 | out: |
| 3083 | return err ? err : allocated; |
| 3084 | } |
| 3085 | |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3086 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3087 | * This function is called by ext4_ext_map_blocks() from |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3088 | * ext4_get_blocks_dio_write() when DIO to write |
| 3089 | * to an uninitialized extent. |
| 3090 | * |
Paul Bolle | fd018fe | 2011-02-15 00:05:43 +0100 | [diff] [blame] | 3091 | * Writing to an uninitialized extent may result in splitting the uninitialized |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 3092 | * extent into multiple /initialized uninitialized extents (up to three) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3093 | * There are three possibilities: |
| 3094 | * a> There is no split required: Entire extent should be uninitialized |
| 3095 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3096 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 3097 | * |
| 3098 | * One of more index blocks maybe needed if the extent tree grow after |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 3099 | * the uninitialized extent split. To prevent ENOSPC occur at the IO |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3100 | * complete, we need to split the uninitialized extent before DIO submit |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 3101 | * the IO. The uninitialized extent called at this time will be split |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3102 | * into three uninitialized extent(at most). After IO complete, the part |
| 3103 | * being filled will be convert to initialized by the end_io callback function |
| 3104 | * via ext4_convert_unwritten_extents(). |
Mingming | ba230c3 | 2009-11-06 04:01:23 -0500 | [diff] [blame] | 3105 | * |
| 3106 | * Returns the size of uninitialized extent to be written on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3107 | */ |
| 3108 | static int ext4_split_unwritten_extents(handle_t *handle, |
| 3109 | struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3110 | struct ext4_map_blocks *map, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3111 | struct ext4_ext_path *path, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3112 | int flags) |
| 3113 | { |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3114 | ext4_lblk_t eof_block; |
| 3115 | ext4_lblk_t ee_block; |
| 3116 | struct ext4_extent *ex; |
| 3117 | unsigned int ee_len; |
| 3118 | int split_flag = 0, depth; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3119 | |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3120 | ext_debug("ext4_split_unwritten_extents: inode %lu, logical" |
| 3121 | "block %llu, max_blocks %u\n", inode->i_ino, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3122 | (unsigned long long)map->m_lblk, map->m_len); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3123 | |
| 3124 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3125 | inode->i_sb->s_blocksize_bits; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3126 | if (eof_block < map->m_lblk + map->m_len) |
| 3127 | eof_block = map->m_lblk + map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3128 | /* |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3129 | * It is safe to convert extent to initialized via explicit |
| 3130 | * zeroout only if extent is fully insde i_size or new_size. |
| 3131 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3132 | depth = ext_depth(inode); |
| 3133 | ex = path[depth].p_ext; |
| 3134 | ee_block = le32_to_cpu(ex->ee_block); |
| 3135 | ee_len = ext4_ext_get_actual_len(ex); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3136 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3137 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
| 3138 | split_flag |= EXT4_EXT_MARK_UNINIT2; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3139 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3140 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
| 3141 | return ext4_split_extent(handle, inode, path, map, split_flag, flags); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3142 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3143 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3144 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3145 | struct inode *inode, |
| 3146 | struct ext4_ext_path *path) |
| 3147 | { |
| 3148 | struct ext4_extent *ex; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3149 | int depth; |
| 3150 | int err = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3151 | |
| 3152 | depth = ext_depth(inode); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3153 | ex = path[depth].p_ext; |
| 3154 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3155 | ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" |
| 3156 | "block %llu, max_blocks %u\n", inode->i_ino, |
| 3157 | (unsigned long long)le32_to_cpu(ex->ee_block), |
| 3158 | ext4_ext_get_actual_len(ex)); |
| 3159 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3160 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3161 | if (err) |
| 3162 | goto out; |
| 3163 | /* first mark the extent as initialized */ |
| 3164 | ext4_ext_mark_initialized(ex); |
| 3165 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3166 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3167 | * borders are not changed |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3168 | */ |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3169 | ext4_ext_try_to_merge(inode, path, ex); |
| 3170 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3171 | /* Mark modified extent as dirty */ |
| 3172 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3173 | out: |
| 3174 | ext4_ext_show_leaf(inode, path); |
| 3175 | return err; |
| 3176 | } |
| 3177 | |
Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3178 | static void unmap_underlying_metadata_blocks(struct block_device *bdev, |
| 3179 | sector_t block, int count) |
| 3180 | { |
| 3181 | int i; |
| 3182 | for (i = 0; i < count; i++) |
| 3183 | unmap_underlying_metadata(bdev, block + i); |
| 3184 | } |
| 3185 | |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3186 | /* |
| 3187 | * Handle EOFBLOCKS_FL flag, clearing it if necessary |
| 3188 | */ |
| 3189 | static int check_eofblocks_fl(handle_t *handle, struct inode *inode, |
Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3190 | ext4_lblk_t lblk, |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3191 | struct ext4_ext_path *path, |
| 3192 | unsigned int len) |
| 3193 | { |
| 3194 | int i, depth; |
| 3195 | struct ext4_extent_header *eh; |
Sergey Senozhatsky | 65922cb | 2011-03-23 14:08:27 -0400 | [diff] [blame] | 3196 | struct ext4_extent *last_ex; |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3197 | |
| 3198 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) |
| 3199 | return 0; |
| 3200 | |
| 3201 | depth = ext_depth(inode); |
| 3202 | eh = path[depth].p_hdr; |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3203 | |
| 3204 | if (unlikely(!eh->eh_entries)) { |
| 3205 | EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and " |
| 3206 | "EOFBLOCKS_FL set"); |
| 3207 | return -EIO; |
| 3208 | } |
| 3209 | last_ex = EXT_LAST_EXTENT(eh); |
| 3210 | /* |
| 3211 | * We should clear the EOFBLOCKS_FL flag if we are writing the |
| 3212 | * last block in the last extent in the file. We test this by |
| 3213 | * first checking to see if the caller to |
| 3214 | * ext4_ext_get_blocks() was interested in the last block (or |
| 3215 | * a block beyond the last block) in the current extent. If |
| 3216 | * this turns out to be false, we can bail out from this |
| 3217 | * function immediately. |
| 3218 | */ |
Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3219 | if (lblk + len < le32_to_cpu(last_ex->ee_block) + |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3220 | ext4_ext_get_actual_len(last_ex)) |
| 3221 | return 0; |
| 3222 | /* |
| 3223 | * If the caller does appear to be planning to write at or |
| 3224 | * beyond the end of the current extent, we then test to see |
| 3225 | * if the current extent is the last extent in the file, by |
| 3226 | * checking to make sure it was reached via the rightmost node |
| 3227 | * at each level of the tree. |
| 3228 | */ |
| 3229 | for (i = depth-1; i >= 0; i--) |
| 3230 | if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr)) |
| 3231 | return 0; |
| 3232 | ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
| 3233 | return ext4_mark_inode_dirty(handle, inode); |
| 3234 | } |
| 3235 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3236 | static int |
| 3237 | ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3238 | struct ext4_map_blocks *map, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3239 | struct ext4_ext_path *path, int flags, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3240 | unsigned int allocated, ext4_fsblk_t newblock) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3241 | { |
| 3242 | int ret = 0; |
| 3243 | int err = 0; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3244 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3245 | |
| 3246 | ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical" |
| 3247 | "block %llu, max_blocks %u, flags %d, allocated %u", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3248 | inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3249 | flags, allocated); |
| 3250 | ext4_ext_show_leaf(inode, path); |
| 3251 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3252 | /* get_block() before submit the IO, split the extent */ |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3253 | if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3254 | ret = ext4_split_unwritten_extents(handle, inode, map, |
| 3255 | path, flags); |
Mingming | 5f52495 | 2009-11-10 10:48:04 -0500 | [diff] [blame] | 3256 | /* |
| 3257 | * Flag the inode(non aio case) or end_io struct (aio case) |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3258 | * that this IO needs to conversion to written when IO is |
Mingming | 5f52495 | 2009-11-10 10:48:04 -0500 | [diff] [blame] | 3259 | * completed |
| 3260 | */ |
Eric Sandeen | e9e3bce | 2011-02-12 08:17:34 -0500 | [diff] [blame] | 3261 | if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) { |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 3262 | io->flag = EXT4_IO_END_UNWRITTEN; |
Eric Sandeen | e9e3bce | 2011-02-12 08:17:34 -0500 | [diff] [blame] | 3263 | atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); |
| 3264 | } else |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 3265 | ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3266 | if (ext4_should_dioread_nolock(inode)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3267 | map->m_flags |= EXT4_MAP_UNINIT; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3268 | goto out; |
| 3269 | } |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3270 | /* IO end_io complete, convert the filled extent to written */ |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3271 | if ((flags & EXT4_GET_BLOCKS_CONVERT)) { |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3272 | ret = ext4_convert_unwritten_extents_endio(handle, inode, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3273 | path); |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3274 | if (ret >= 0) { |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3275 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3276 | err = check_eofblocks_fl(handle, inode, map->m_lblk, |
| 3277 | path, map->m_len); |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3278 | } else |
| 3279 | err = ret; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3280 | goto out2; |
| 3281 | } |
| 3282 | /* buffered IO case */ |
| 3283 | /* |
| 3284 | * repeat fallocate creation request |
| 3285 | * we already have an unwritten extent |
| 3286 | */ |
| 3287 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) |
| 3288 | goto map_out; |
| 3289 | |
| 3290 | /* buffered READ or buffered write_begin() lookup */ |
| 3291 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 3292 | /* |
| 3293 | * We have blocks reserved already. We |
| 3294 | * return allocated blocks so that delalloc |
| 3295 | * won't do block reservation for us. But |
| 3296 | * the buffer head will be unmapped so that |
| 3297 | * a read from the block returns 0s. |
| 3298 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3299 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3300 | goto out1; |
| 3301 | } |
| 3302 | |
| 3303 | /* buffered write, writepage time, convert*/ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3304 | ret = ext4_ext_convert_to_initialized(handle, inode, map, path); |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3305 | if (ret >= 0) { |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3306 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3307 | err = check_eofblocks_fl(handle, inode, map->m_lblk, path, |
| 3308 | map->m_len); |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3309 | if (err < 0) |
| 3310 | goto out2; |
| 3311 | } |
| 3312 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3313 | out: |
| 3314 | if (ret <= 0) { |
| 3315 | err = ret; |
| 3316 | goto out2; |
| 3317 | } else |
| 3318 | allocated = ret; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3319 | map->m_flags |= EXT4_MAP_NEW; |
Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3320 | /* |
| 3321 | * if we allocated more blocks than requested |
| 3322 | * we need to make sure we unmap the extra block |
| 3323 | * allocated. The actual needed block will get |
| 3324 | * unmapped later when we find the buffer_head marked |
| 3325 | * new. |
| 3326 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3327 | if (allocated > map->m_len) { |
Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3328 | unmap_underlying_metadata_blocks(inode->i_sb->s_bdev, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3329 | newblock + map->m_len, |
| 3330 | allocated - map->m_len); |
| 3331 | allocated = map->m_len; |
Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3332 | } |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 3333 | |
| 3334 | /* |
| 3335 | * If we have done fallocate with the offset that is already |
| 3336 | * delayed allocated, we would have block reservation |
| 3337 | * and quota reservation done in the delayed write path. |
| 3338 | * But fallocate would have already updated quota and block |
| 3339 | * count for this offset. So cancel these reservation |
| 3340 | */ |
Aneesh Kumar K.V | 1296cc8 | 2010-01-15 01:27:59 -0500 | [diff] [blame] | 3341 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 3342 | ext4_da_update_reserve_space(inode, allocated, 0); |
| 3343 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3344 | map_out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3345 | map->m_flags |= EXT4_MAP_MAPPED; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3346 | out1: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3347 | if (allocated > map->m_len) |
| 3348 | allocated = map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3349 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3350 | map->m_pblk = newblock; |
| 3351 | map->m_len = allocated; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3352 | out2: |
| 3353 | if (path) { |
| 3354 | ext4_ext_drop_refs(path); |
| 3355 | kfree(path); |
| 3356 | } |
| 3357 | return err ? err : allocated; |
| 3358 | } |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3359 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3360 | /* |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3361 | * get_implied_cluster_alloc - check to see if the requested |
| 3362 | * allocation (in the map structure) overlaps with a cluster already |
| 3363 | * allocated in an extent. |
| 3364 | * @sbi The ext4-specific superblock structure |
| 3365 | * @map The requested lblk->pblk mapping |
| 3366 | * @ex The extent structure which might contain an implied |
| 3367 | * cluster allocation |
| 3368 | * |
| 3369 | * This function is called by ext4_ext_map_blocks() after we failed to |
| 3370 | * find blocks that were already in the inode's extent tree. Hence, |
| 3371 | * we know that the beginning of the requested region cannot overlap |
| 3372 | * the extent from the inode's extent tree. There are three cases we |
| 3373 | * want to catch. The first is this case: |
| 3374 | * |
| 3375 | * |--- cluster # N--| |
| 3376 | * |--- extent ---| |---- requested region ---| |
| 3377 | * |==========| |
| 3378 | * |
| 3379 | * The second case that we need to test for is this one: |
| 3380 | * |
| 3381 | * |--------- cluster # N ----------------| |
| 3382 | * |--- requested region --| |------- extent ----| |
| 3383 | * |=======================| |
| 3384 | * |
| 3385 | * The third case is when the requested region lies between two extents |
| 3386 | * within the same cluster: |
| 3387 | * |------------- cluster # N-------------| |
| 3388 | * |----- ex -----| |---- ex_right ----| |
| 3389 | * |------ requested region ------| |
| 3390 | * |================| |
| 3391 | * |
| 3392 | * In each of the above cases, we need to set the map->m_pblk and |
| 3393 | * map->m_len so it corresponds to the return the extent labelled as |
| 3394 | * "|====|" from cluster #N, since it is already in use for data in |
| 3395 | * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to |
| 3396 | * signal to ext4_ext_map_blocks() that map->m_pblk should be treated |
| 3397 | * as a new "allocated" block region. Otherwise, we will return 0 and |
| 3398 | * ext4_ext_map_blocks() will then allocate one or more new clusters |
| 3399 | * by calling ext4_mb_new_blocks(). |
| 3400 | */ |
| 3401 | static int get_implied_cluster_alloc(struct ext4_sb_info *sbi, |
| 3402 | struct ext4_map_blocks *map, |
| 3403 | struct ext4_extent *ex, |
| 3404 | struct ext4_ext_path *path) |
| 3405 | { |
| 3406 | ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1); |
| 3407 | ext4_lblk_t ex_cluster_start, ex_cluster_end; |
| 3408 | ext4_lblk_t rr_cluster_start, rr_cluster_end; |
| 3409 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
| 3410 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
| 3411 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
| 3412 | |
| 3413 | /* The extent passed in that we are trying to match */ |
| 3414 | ex_cluster_start = EXT4_B2C(sbi, ee_block); |
| 3415 | ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); |
| 3416 | |
| 3417 | /* The requested region passed into ext4_map_blocks() */ |
| 3418 | rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); |
| 3419 | rr_cluster_end = EXT4_B2C(sbi, map->m_lblk + map->m_len - 1); |
| 3420 | |
| 3421 | if ((rr_cluster_start == ex_cluster_end) || |
| 3422 | (rr_cluster_start == ex_cluster_start)) { |
| 3423 | if (rr_cluster_start == ex_cluster_end) |
| 3424 | ee_start += ee_len - 1; |
| 3425 | map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) + |
| 3426 | c_offset; |
| 3427 | map->m_len = min(map->m_len, |
| 3428 | (unsigned) sbi->s_cluster_ratio - c_offset); |
| 3429 | /* |
| 3430 | * Check for and handle this case: |
| 3431 | * |
| 3432 | * |--------- cluster # N-------------| |
| 3433 | * |------- extent ----| |
| 3434 | * |--- requested region ---| |
| 3435 | * |===========| |
| 3436 | */ |
| 3437 | |
| 3438 | if (map->m_lblk < ee_block) |
| 3439 | map->m_len = min(map->m_len, ee_block - map->m_lblk); |
| 3440 | |
| 3441 | /* |
| 3442 | * Check for the case where there is already another allocated |
| 3443 | * block to the right of 'ex' but before the end of the cluster. |
| 3444 | * |
| 3445 | * |------------- cluster # N-------------| |
| 3446 | * |----- ex -----| |---- ex_right ----| |
| 3447 | * |------ requested region ------| |
| 3448 | * |================| |
| 3449 | */ |
| 3450 | if (map->m_lblk > ee_block) { |
| 3451 | ext4_lblk_t next = ext4_ext_next_allocated_block(path); |
| 3452 | map->m_len = min(map->m_len, next - map->m_lblk); |
| 3453 | } |
| 3454 | return 1; |
| 3455 | } |
| 3456 | return 0; |
| 3457 | } |
| 3458 | |
| 3459 | |
| 3460 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 3461 | * Block allocation/map/preallocation routine for extents based files |
| 3462 | * |
| 3463 | * |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3464 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3465 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 3466 | * (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] | 3467 | * |
| 3468 | * return > 0, number of of blocks already mapped/allocated |
| 3469 | * if create == 0 and these are pre-allocated blocks |
| 3470 | * buffer head is unmapped |
| 3471 | * otherwise blocks are mapped |
| 3472 | * |
| 3473 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 3474 | * buffer head is unmapped |
| 3475 | * |
| 3476 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3477 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3478 | int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, |
| 3479 | struct ext4_map_blocks *map, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3480 | { |
| 3481 | struct ext4_ext_path *path = NULL; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3482 | struct ext4_extent newex, *ex, *ex2; |
| 3483 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 3484 | ext4_fsblk_t newblock = 0; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3485 | int free_on_err = 0, err = 0, depth, ret; |
| 3486 | unsigned int allocated = 0, offset = 0; |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3487 | unsigned int punched_out = 0; |
| 3488 | unsigned int result = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3489 | struct ext4_allocation_request ar; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3490 | ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3491 | ext4_lblk_t cluster_offset; |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3492 | struct ext4_map_blocks punch_map; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3493 | |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 3494 | ext_debug("blocks %u/%u requested for inode %lu\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3495 | map->m_lblk, map->m_len, inode->i_ino); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 3496 | trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3497 | |
| 3498 | /* check in cache */ |
Robin Dong | 015861b | 2011-07-17 23:27:43 -0400 | [diff] [blame] | 3499 | if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) && |
| 3500 | ext4_ext_in_cache(inode, map->m_lblk, &newex)) { |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 3501 | if (!newex.ee_start_lo && !newex.ee_start_hi) { |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3502 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3503 | /* |
| 3504 | * block isn't allocated yet and |
| 3505 | * user doesn't want to allocate it |
| 3506 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3507 | goto out2; |
| 3508 | } |
| 3509 | /* we should allocate requested block */ |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 3510 | } else { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3511 | /* block is already allocated */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3512 | newblock = map->m_lblk |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 3513 | - le32_to_cpu(newex.ee_block) |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3514 | + ext4_ext_pblock(&newex); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3515 | /* number of remaining blocks in the extent */ |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3516 | allocated = ext4_ext_get_actual_len(&newex) - |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3517 | (map->m_lblk - le32_to_cpu(newex.ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3518 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | /* find extent for this block */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3523 | path = ext4_ext_find_extent(inode, map->m_lblk, NULL); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3524 | if (IS_ERR(path)) { |
| 3525 | err = PTR_ERR(path); |
| 3526 | path = NULL; |
| 3527 | goto out2; |
| 3528 | } |
| 3529 | |
| 3530 | depth = ext_depth(inode); |
| 3531 | |
| 3532 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3533 | * consistent leaf must not be empty; |
| 3534 | * this situation is possible, though, _during_ tree modification; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3535 | * this is why assert can't be put in ext4_ext_find_extent() |
| 3536 | */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 3537 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 3538 | EXT4_ERROR_INODE(inode, "bad extent address " |
Theodore Ts'o | f70f362 | 2010-05-16 23:00:00 -0400 | [diff] [blame] | 3539 | "lblock: %lu, depth: %d pblock %lld", |
| 3540 | (unsigned long) map->m_lblk, depth, |
| 3541 | path[depth].p_block); |
Surbhi Palande | 034fb4c | 2009-12-14 09:53:52 -0500 | [diff] [blame] | 3542 | err = -EIO; |
| 3543 | goto out2; |
| 3544 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3545 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 3546 | ex = path[depth].p_ext; |
| 3547 | if (ex) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3548 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3549 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3550 | unsigned short ee_len; |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 3551 | |
| 3552 | /* |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 3553 | * Uninitialized extents are treated as holes, except that |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3554 | * we split out initialized portions during a write. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 3555 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3556 | ee_len = ext4_ext_get_actual_len(ex); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3557 | /* if found extent covers block, simply return it */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3558 | if (in_range(map->m_lblk, ee_block, ee_len)) { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 3559 | ext4_fsblk_t partial_cluster = 0; |
| 3560 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3561 | newblock = map->m_lblk - ee_block + ee_start; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3562 | /* number of remaining blocks in the extent */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3563 | allocated = ee_len - (map->m_lblk - ee_block); |
| 3564 | ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, |
| 3565 | ee_block, ee_len, newblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3566 | |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3567 | if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) { |
| 3568 | /* |
| 3569 | * Do not put uninitialized extent |
| 3570 | * in the cache |
| 3571 | */ |
| 3572 | if (!ext4_ext_is_uninitialized(ex)) { |
| 3573 | ext4_ext_put_in_cache(inode, ee_block, |
| 3574 | ee_len, ee_start); |
| 3575 | goto out; |
| 3576 | } |
| 3577 | ret = ext4_ext_handle_uninitialized_extents( |
| 3578 | handle, inode, map, path, flags, |
| 3579 | allocated, newblock); |
| 3580 | return ret; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3581 | } |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3582 | |
| 3583 | /* |
| 3584 | * Punch out the map length, but only to the |
| 3585 | * end of the extent |
| 3586 | */ |
| 3587 | punched_out = allocated < map->m_len ? |
| 3588 | allocated : map->m_len; |
| 3589 | |
| 3590 | /* |
| 3591 | * Sense extents need to be converted to |
| 3592 | * uninitialized, they must fit in an |
| 3593 | * uninitialized extent |
| 3594 | */ |
| 3595 | if (punched_out > EXT_UNINIT_MAX_LEN) |
| 3596 | punched_out = EXT_UNINIT_MAX_LEN; |
| 3597 | |
| 3598 | punch_map.m_lblk = map->m_lblk; |
| 3599 | punch_map.m_pblk = newblock; |
| 3600 | punch_map.m_len = punched_out; |
| 3601 | punch_map.m_flags = 0; |
| 3602 | |
| 3603 | /* Check to see if the extent needs to be split */ |
| 3604 | if (punch_map.m_len != ee_len || |
| 3605 | punch_map.m_lblk != ee_block) { |
| 3606 | |
| 3607 | ret = ext4_split_extent(handle, inode, |
| 3608 | path, &punch_map, 0, |
| 3609 | EXT4_GET_BLOCKS_PUNCH_OUT_EXT | |
| 3610 | EXT4_GET_BLOCKS_PRE_IO); |
| 3611 | |
| 3612 | if (ret < 0) { |
| 3613 | err = ret; |
| 3614 | goto out2; |
| 3615 | } |
| 3616 | /* |
| 3617 | * find extent for the block at |
| 3618 | * the start of the hole |
| 3619 | */ |
| 3620 | ext4_ext_drop_refs(path); |
| 3621 | kfree(path); |
| 3622 | |
| 3623 | path = ext4_ext_find_extent(inode, |
| 3624 | map->m_lblk, NULL); |
| 3625 | if (IS_ERR(path)) { |
| 3626 | err = PTR_ERR(path); |
| 3627 | path = NULL; |
| 3628 | goto out2; |
| 3629 | } |
| 3630 | |
| 3631 | depth = ext_depth(inode); |
| 3632 | ex = path[depth].p_ext; |
| 3633 | ee_len = ext4_ext_get_actual_len(ex); |
| 3634 | ee_block = le32_to_cpu(ex->ee_block); |
| 3635 | ee_start = ext4_ext_pblock(ex); |
| 3636 | |
| 3637 | } |
| 3638 | |
| 3639 | ext4_ext_mark_uninitialized(ex); |
| 3640 | |
Allison Henderson | f7d0d37 | 2011-07-17 23:17:02 -0400 | [diff] [blame] | 3641 | ext4_ext_invalidate_cache(inode); |
| 3642 | |
| 3643 | err = ext4_ext_rm_leaf(handle, inode, path, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame^] | 3644 | &partial_cluster, map->m_lblk, |
| 3645 | map->m_lblk + punched_out); |
Allison Henderson | f7d0d37 | 2011-07-17 23:17:02 -0400 | [diff] [blame] | 3646 | |
| 3647 | if (!err && path->p_hdr->eh_entries == 0) { |
| 3648 | /* |
| 3649 | * Punch hole freed all of this sub tree, |
| 3650 | * so we need to correct eh_depth |
| 3651 | */ |
| 3652 | err = ext4_ext_get_access(handle, inode, path); |
| 3653 | if (err == 0) { |
| 3654 | ext_inode_hdr(inode)->eh_depth = 0; |
| 3655 | ext_inode_hdr(inode)->eh_max = |
| 3656 | cpu_to_le16(ext4_ext_space_root( |
| 3657 | inode, 0)); |
| 3658 | |
| 3659 | err = ext4_ext_dirty( |
| 3660 | handle, inode, path); |
| 3661 | } |
| 3662 | } |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3663 | |
| 3664 | goto out2; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3669 | * requested block isn't allocated yet; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3670 | * we couldn't try to create block if create flag is zero |
| 3671 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3672 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3673 | /* |
| 3674 | * put just found gap into cache to speed up |
| 3675 | * subsequent requests |
| 3676 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3677 | ext4_ext_put_gap_in_cache(inode, path, map->m_lblk); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3678 | goto out2; |
| 3679 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3680 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3681 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3682 | * Okay, we need to do block allocation. |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 3683 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3684 | newex.ee_block = cpu_to_le32(map->m_lblk); |
| 3685 | cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1); |
| 3686 | |
| 3687 | /* |
| 3688 | * If we are doing bigalloc, check to see if the extent returned |
| 3689 | * by ext4_ext_find_extent() implies a cluster we can use. |
| 3690 | */ |
| 3691 | if (cluster_offset && ex && |
| 3692 | get_implied_cluster_alloc(sbi, map, ex, path)) { |
| 3693 | ar.len = allocated = map->m_len; |
| 3694 | newblock = map->m_pblk; |
| 3695 | goto got_allocated_blocks; |
| 3696 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3697 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3698 | /* find neighbour allocated blocks */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3699 | ar.lleft = map->m_lblk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3700 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 3701 | if (err) |
| 3702 | goto out2; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3703 | ar.lright = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3704 | ex2 = NULL; |
| 3705 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3706 | if (err) |
| 3707 | goto out2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 3708 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3709 | /* Check if the extent after searching to the right implies a |
| 3710 | * cluster we can use. */ |
| 3711 | if ((sbi->s_cluster_ratio > 1) && ex2 && |
| 3712 | get_implied_cluster_alloc(sbi, map, ex2, path)) { |
| 3713 | ar.len = allocated = map->m_len; |
| 3714 | newblock = map->m_pblk; |
| 3715 | goto got_allocated_blocks; |
| 3716 | } |
| 3717 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 3718 | /* |
| 3719 | * See if request is beyond maximum number of blocks we can have in |
| 3720 | * a single extent. For an initialized extent this limit is |
| 3721 | * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is |
| 3722 | * EXT_UNINIT_MAX_LEN. |
| 3723 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3724 | if (map->m_len > EXT_INIT_MAX_LEN && |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3725 | !(flags & EXT4_GET_BLOCKS_UNINIT_EXT)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3726 | map->m_len = EXT_INIT_MAX_LEN; |
| 3727 | else if (map->m_len > EXT_UNINIT_MAX_LEN && |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3728 | (flags & EXT4_GET_BLOCKS_UNINIT_EXT)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3729 | map->m_len = EXT_UNINIT_MAX_LEN; |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 3730 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3731 | /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3732 | newex.ee_len = cpu_to_le16(map->m_len); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3733 | err = ext4_ext_check_overlap(sbi, inode, &newex, path); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 3734 | if (err) |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3735 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 3736 | else |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3737 | allocated = map->m_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3738 | |
| 3739 | /* allocate new block */ |
| 3740 | ar.inode = inode; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3741 | ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); |
| 3742 | ar.logical = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3743 | /* |
| 3744 | * We calculate the offset from the beginning of the cluster |
| 3745 | * for the logical block number, since when we allocate a |
| 3746 | * physical cluster, the physical block should start at the |
| 3747 | * same offset from the beginning of the cluster. This is |
| 3748 | * needed so that future calls to get_implied_cluster_alloc() |
| 3749 | * work correctly. |
| 3750 | */ |
| 3751 | offset = map->m_lblk & (sbi->s_cluster_ratio - 1); |
| 3752 | ar.len = EXT4_NUM_B2C(sbi, offset+allocated); |
| 3753 | ar.goal -= offset; |
| 3754 | ar.logical -= offset; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3755 | if (S_ISREG(inode->i_mode)) |
| 3756 | ar.flags = EXT4_MB_HINT_DATA; |
| 3757 | else |
| 3758 | /* disable in-core preallocation for non-regular files */ |
| 3759 | ar.flags = 0; |
Vivek Haldar | 556b27a | 2011-05-25 07:41:54 -0400 | [diff] [blame] | 3760 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) |
| 3761 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3762 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3763 | if (!newblock) |
| 3764 | goto out2; |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 3765 | ext_debug("allocate new block: goal %llu, found %llu/%u\n", |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3766 | ar.goal, newblock, allocated); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3767 | free_on_err = 1; |
| 3768 | ar.len = EXT4_C2B(sbi, ar.len) - offset; |
| 3769 | if (ar.len > allocated) |
| 3770 | ar.len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3771 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3772 | got_allocated_blocks: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3773 | /* try to insert new extent into found leaf and return */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3774 | ext4_ext_store_pblock(&newex, newblock + offset); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3775 | newex.ee_len = cpu_to_le16(ar.len); |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3776 | /* Mark uninitialized */ |
| 3777 | if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3778 | ext4_ext_mark_uninitialized(&newex); |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3779 | /* |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3780 | * io_end structure was created for every IO write to an |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3781 | * uninitialized extent. To avoid unnecessary conversion, |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3782 | * here we flag the IO that really needs the conversion. |
Mingming | 5f52495 | 2009-11-10 10:48:04 -0500 | [diff] [blame] | 3783 | * For non asycn direct IO case, flag the inode state |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3784 | * that we need to perform conversion when IO is done. |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3785 | */ |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3786 | if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { |
Eric Sandeen | e9e3bce | 2011-02-12 08:17:34 -0500 | [diff] [blame] | 3787 | if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) { |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 3788 | io->flag = EXT4_IO_END_UNWRITTEN; |
Eric Sandeen | e9e3bce | 2011-02-12 08:17:34 -0500 | [diff] [blame] | 3789 | atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); |
| 3790 | } else |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 3791 | ext4_set_inode_state(inode, |
| 3792 | EXT4_STATE_DIO_UNWRITTEN); |
Mingming | 5f52495 | 2009-11-10 10:48:04 -0500 | [diff] [blame] | 3793 | } |
Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3794 | if (ext4_should_dioread_nolock(inode)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3795 | map->m_flags |= EXT4_MAP_UNINIT; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3796 | } |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 3797 | |
Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3798 | err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len); |
Jiaying Zhang | 575a1d4 | 2011-07-10 20:07:25 -0400 | [diff] [blame] | 3799 | if (!err) |
| 3800 | err = ext4_ext_insert_extent(handle, inode, path, |
| 3801 | &newex, flags); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3802 | if (err && free_on_err) { |
Maxim Patlasov | 7132de7 | 2011-07-10 19:37:48 -0400 | [diff] [blame] | 3803 | int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ? |
| 3804 | EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 3805 | /* free data blocks we just allocated */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3806 | /* not a good idea to call discard here directly, |
| 3807 | * but otherwise we'd need to call it every free() */ |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3808 | ext4_discard_preallocations(inode); |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 3809 | ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex), |
Maxim Patlasov | 7132de7 | 2011-07-10 19:37:48 -0400 | [diff] [blame] | 3810 | ext4_ext_get_actual_len(&newex), fb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3811 | goto out2; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 3812 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3813 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3814 | /* previous routine could use block we allocated */ |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3815 | newblock = ext4_ext_pblock(&newex); |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3816 | allocated = ext4_ext_get_actual_len(&newex); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3817 | if (allocated > map->m_len) |
| 3818 | allocated = map->m_len; |
| 3819 | map->m_flags |= EXT4_MAP_NEW; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3820 | |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3821 | /* |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 3822 | * Update reserved blocks/metadata blocks after successful |
| 3823 | * block allocation which had been deferred till now. |
| 3824 | */ |
Aneesh Kumar K.V | 1296cc8 | 2010-01-15 01:27:59 -0500 | [diff] [blame] | 3825 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 3826 | ext4_da_update_reserve_space(inode, allocated, 1); |
| 3827 | |
| 3828 | /* |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3829 | * Cache the extent and update transaction to commit on fdatasync only |
| 3830 | * when it is _not_ an uninitialized extent. |
| 3831 | */ |
| 3832 | if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) { |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 3833 | ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock); |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3834 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 3835 | } else |
| 3836 | ext4_update_inode_fsync_trans(handle, inode, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3837 | out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3838 | if (allocated > map->m_len) |
| 3839 | allocated = map->m_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3840 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3841 | map->m_flags |= EXT4_MAP_MAPPED; |
| 3842 | map->m_pblk = newblock; |
| 3843 | map->m_len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3844 | out2: |
| 3845 | if (path) { |
| 3846 | ext4_ext_drop_refs(path); |
| 3847 | kfree(path); |
| 3848 | } |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 3849 | trace_ext4_ext_map_blocks_exit(inode, map->m_lblk, |
| 3850 | newblock, map->m_len, err ? err : allocated); |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3851 | |
| 3852 | result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ? |
| 3853 | punched_out : allocated; |
| 3854 | |
| 3855 | return err ? err : result; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3856 | } |
| 3857 | |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3858 | void ext4_ext_truncate(struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3859 | { |
| 3860 | struct address_space *mapping = inode->i_mapping; |
| 3861 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3862 | ext4_lblk_t last_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3863 | handle_t *handle; |
Allison Henderson | 189e868 | 2011-09-06 21:49:44 -0400 | [diff] [blame] | 3864 | loff_t page_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3865 | int err = 0; |
| 3866 | |
| 3867 | /* |
Jiaying Zhang | 3889fd5 | 2011-01-10 12:47:05 -0500 | [diff] [blame] | 3868 | * finish any pending end_io work so we won't run the risk of |
| 3869 | * converting any truncated blocks to initialized later |
| 3870 | */ |
| 3871 | ext4_flush_completed_IO(inode); |
| 3872 | |
| 3873 | /* |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3874 | * probably first extent we're gonna free will be last in block |
| 3875 | */ |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 3876 | err = ext4_writepage_trans_blocks(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3877 | handle = ext4_journal_start(inode, err); |
Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3878 | if (IS_ERR(handle)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3879 | return; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3880 | |
Allison Henderson | 189e868 | 2011-09-06 21:49:44 -0400 | [diff] [blame] | 3881 | if (inode->i_size % PAGE_CACHE_SIZE != 0) { |
| 3882 | page_len = PAGE_CACHE_SIZE - |
| 3883 | (inode->i_size & (PAGE_CACHE_SIZE - 1)); |
| 3884 | |
| 3885 | err = ext4_discard_partial_page_buffers(handle, |
| 3886 | mapping, inode->i_size, page_len, 0); |
| 3887 | |
| 3888 | if (err) |
| 3889 | goto out_stop; |
| 3890 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3891 | |
Jan Kara | 9ddfc3d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3892 | if (ext4_orphan_add(handle, inode)) |
| 3893 | goto out_stop; |
| 3894 | |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3895 | down_write(&EXT4_I(inode)->i_data_sem); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3896 | ext4_ext_invalidate_cache(inode); |
| 3897 | |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3898 | ext4_discard_preallocations(inode); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3899 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3900 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3901 | * TODO: optimization is possible here. |
| 3902 | * Probably we need not scan at all, |
| 3903 | * because page truncation is enough. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3904 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3905 | |
| 3906 | /* we have to know where to truncate from in crash case */ |
| 3907 | EXT4_I(inode)->i_disksize = inode->i_size; |
| 3908 | ext4_mark_inode_dirty(handle, inode); |
| 3909 | |
| 3910 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 3911 | >> EXT4_BLOCK_SIZE_BITS(sb); |
Allison Henderson | c6a0371 | 2011-07-17 23:21:03 -0400 | [diff] [blame] | 3912 | err = ext4_ext_remove_space(inode, last_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3913 | |
| 3914 | /* In a multi-transaction truncate, we only make the final |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3915 | * transaction synchronous. |
| 3916 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3917 | if (IS_SYNC(inode)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3918 | ext4_handle_sync(handle); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3919 | |
Jan Kara | 9ddfc3d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 3920 | up_write(&EXT4_I(inode)->i_data_sem); |
Eric Gouriou | f6d2f6b | 2011-05-22 21:33:00 -0400 | [diff] [blame] | 3921 | |
| 3922 | out_stop: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3923 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3924 | * If this was a simple ftruncate() and the file will remain alive, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3925 | * then we need to clear up the orphan record which we created above. |
| 3926 | * However, if this was a real unlink then we were called by |
| 3927 | * ext4_delete_inode(), and we allow that function to clean up the |
| 3928 | * orphan info for us. |
| 3929 | */ |
| 3930 | if (inode->i_nlink) |
| 3931 | ext4_orphan_del(handle, inode); |
| 3932 | |
Solofo Ramangalahy | ef73772 | 2008-04-29 22:00:41 -0400 | [diff] [blame] | 3933 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 3934 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3935 | ext4_journal_stop(handle); |
| 3936 | } |
| 3937 | |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3938 | static void ext4_falloc_update_inode(struct inode *inode, |
| 3939 | int mode, loff_t new_size, int update_ctime) |
| 3940 | { |
| 3941 | struct timespec now; |
| 3942 | |
| 3943 | if (update_ctime) { |
| 3944 | now = current_fs_time(inode->i_sb); |
| 3945 | if (!timespec_equal(&inode->i_ctime, &now)) |
| 3946 | inode->i_ctime = now; |
| 3947 | } |
| 3948 | /* |
| 3949 | * Update only when preallocation was requested beyond |
| 3950 | * the file size. |
| 3951 | */ |
Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 3952 | if (!(mode & FALLOC_FL_KEEP_SIZE)) { |
| 3953 | if (new_size > i_size_read(inode)) |
| 3954 | i_size_write(inode, new_size); |
| 3955 | if (new_size > EXT4_I(inode)->i_disksize) |
| 3956 | ext4_update_i_disksize(inode, new_size); |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 3957 | } else { |
| 3958 | /* |
| 3959 | * Mark that we allocate beyond EOF so the subsequent truncate |
| 3960 | * can proceed even if the new size is the same as i_size. |
| 3961 | */ |
| 3962 | if (new_size > i_size_read(inode)) |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 3963 | ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | } |
| 3967 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3968 | /* |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 3969 | * preallocate space for a file. This implements ext4's fallocate file |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3970 | * operation, which gets called from sys_fallocate system call. |
| 3971 | * For block-mapped files, posix_fallocate should fall back to the method |
| 3972 | * of writing zeroes to the required new blocks (the same behavior which is |
| 3973 | * expected for file systems which do not support fallocate() system call). |
| 3974 | */ |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 3975 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3976 | { |
Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 3977 | struct inode *inode = file->f_path.dentry->d_inode; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3978 | handle_t *handle; |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3979 | loff_t new_size; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 3980 | unsigned int max_blocks; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3981 | int ret = 0; |
| 3982 | int ret2 = 0; |
| 3983 | int retries = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 3984 | struct ext4_map_blocks map; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3985 | unsigned int credits, blkbits = inode->i_blkbits; |
| 3986 | |
| 3987 | /* |
| 3988 | * currently supporting (pre)allocate mode for extent-based |
| 3989 | * files _only_ |
| 3990 | */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 3991 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3992 | return -EOPNOTSUPP; |
| 3993 | |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 3994 | /* Return error if mode is not supported */ |
| 3995 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 3996 | return -EOPNOTSUPP; |
| 3997 | |
| 3998 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 3999 | return ext4_punch_hole(file, offset, len); |
| 4000 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4001 | trace_ext4_fallocate_enter(inode, offset, len, mode); |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4002 | map.m_lblk = offset >> blkbits; |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4003 | /* |
| 4004 | * We can't just convert len to max_blocks because |
| 4005 | * If blocksize = 4096 offset = 3072 and len = 2048 |
| 4006 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4007 | max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4008 | - map.m_lblk; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4009 | /* |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4010 | * credits to insert 1 extent into extent tree |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4011 | */ |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4012 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 4013 | mutex_lock(&inode->i_mutex); |
Nikanth Karthikesan | 6d19c42 | 2010-05-16 14:00:00 -0400 | [diff] [blame] | 4014 | ret = inode_newsize_ok(inode, (len + offset)); |
| 4015 | if (ret) { |
| 4016 | mutex_unlock(&inode->i_mutex); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4017 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); |
Nikanth Karthikesan | 6d19c42 | 2010-05-16 14:00:00 -0400 | [diff] [blame] | 4018 | return ret; |
| 4019 | } |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4020 | retry: |
| 4021 | while (ret >= 0 && ret < max_blocks) { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4022 | map.m_lblk = map.m_lblk + ret; |
| 4023 | map.m_len = max_blocks = max_blocks - ret; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4024 | handle = ext4_journal_start(inode, credits); |
| 4025 | if (IS_ERR(handle)) { |
| 4026 | ret = PTR_ERR(handle); |
| 4027 | break; |
| 4028 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4029 | ret = ext4_map_blocks(handle, inode, &map, |
Vivek Haldar | 556b27a | 2011-05-25 07:41:54 -0400 | [diff] [blame] | 4030 | EXT4_GET_BLOCKS_CREATE_UNINIT_EXT | |
| 4031 | EXT4_GET_BLOCKS_NO_NORMALIZE); |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4032 | if (ret <= 0) { |
Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 4033 | #ifdef EXT4FS_DEBUG |
| 4034 | WARN_ON(ret <= 0); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4035 | printk(KERN_ERR "%s: ext4_ext_map_blocks " |
Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 4036 | "returned error inode#%lu, block=%u, " |
Thadeu Lima de Souza Cascardo | 9fd9784 | 2009-01-26 19:26:26 -0500 | [diff] [blame] | 4037 | "max_blocks=%u", __func__, |
Kazuya Mio | a6371b6 | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 4038 | inode->i_ino, map.m_lblk, max_blocks); |
Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 4039 | #endif |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4040 | ext4_mark_inode_dirty(handle, inode); |
| 4041 | ret2 = ext4_journal_stop(handle); |
| 4042 | break; |
| 4043 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4044 | if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len, |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4045 | blkbits) >> blkbits)) |
| 4046 | new_size = offset + len; |
| 4047 | else |
Utako Kusaka | 29ae07b | 2011-07-27 22:11:20 -0400 | [diff] [blame] | 4048 | new_size = ((loff_t) map.m_lblk + ret) << blkbits; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4049 | |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4050 | ext4_falloc_update_inode(inode, mode, new_size, |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4051 | (map.m_flags & EXT4_MAP_NEW)); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4052 | ext4_mark_inode_dirty(handle, inode); |
| 4053 | ret2 = ext4_journal_stop(handle); |
| 4054 | if (ret2) |
| 4055 | break; |
| 4056 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4057 | if (ret == -ENOSPC && |
| 4058 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 4059 | ret = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4060 | goto retry; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4061 | } |
Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 4062 | mutex_unlock(&inode->i_mutex); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4063 | trace_ext4_fallocate_exit(inode, offset, max_blocks, |
| 4064 | ret > 0 ? ret2 : ret); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4065 | return ret > 0 ? ret2 : ret; |
| 4066 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4067 | |
| 4068 | /* |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4069 | * This function convert a range of blocks to written extents |
| 4070 | * The caller of this function will pass the start offset and the size. |
| 4071 | * all unwritten extents within this range will be converted to |
| 4072 | * written extents. |
| 4073 | * |
| 4074 | * This function is called from the direct IO end io call back |
| 4075 | * function, to convert the fallocated extents after IO is completed. |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 4076 | * Returns 0 on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4077 | */ |
| 4078 | int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, |
Eric Sandeen | a1de02d | 2010-02-04 23:58:38 -0500 | [diff] [blame] | 4079 | ssize_t len) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4080 | { |
| 4081 | handle_t *handle; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4082 | unsigned int max_blocks; |
| 4083 | int ret = 0; |
| 4084 | int ret2 = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4085 | struct ext4_map_blocks map; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4086 | unsigned int credits, blkbits = inode->i_blkbits; |
| 4087 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4088 | map.m_lblk = offset >> blkbits; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4089 | /* |
| 4090 | * We can't just convert len to max_blocks because |
| 4091 | * If blocksize = 4096 offset = 3072 and len = 2048 |
| 4092 | */ |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4093 | max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) - |
| 4094 | map.m_lblk); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4095 | /* |
| 4096 | * credits to insert 1 extent into extent tree |
| 4097 | */ |
| 4098 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 4099 | while (ret >= 0 && ret < max_blocks) { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4100 | map.m_lblk += ret; |
| 4101 | map.m_len = (max_blocks -= ret); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4102 | handle = ext4_journal_start(inode, credits); |
| 4103 | if (IS_ERR(handle)) { |
| 4104 | ret = PTR_ERR(handle); |
| 4105 | break; |
| 4106 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4107 | ret = ext4_map_blocks(handle, inode, &map, |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 4108 | EXT4_GET_BLOCKS_IO_CONVERT_EXT); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4109 | if (ret <= 0) { |
| 4110 | WARN_ON(ret <= 0); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4111 | printk(KERN_ERR "%s: ext4_ext_map_blocks " |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4112 | "returned error inode#%lu, block=%u, " |
| 4113 | "max_blocks=%u", __func__, |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4114 | inode->i_ino, map.m_lblk, map.m_len); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4115 | } |
| 4116 | ext4_mark_inode_dirty(handle, inode); |
| 4117 | ret2 = ext4_journal_stop(handle); |
| 4118 | if (ret <= 0 || ret2 ) |
| 4119 | break; |
| 4120 | } |
| 4121 | return ret > 0 ? ret2 : ret; |
| 4122 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4123 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4124 | /* |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4125 | * Callback function called for each extent to gather FIEMAP information. |
| 4126 | */ |
Lukas Czerner | c03f8aa | 2011-06-06 00:06:52 -0400 | [diff] [blame] | 4127 | static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next, |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4128 | struct ext4_ext_cache *newex, struct ext4_extent *ex, |
| 4129 | void *data) |
| 4130 | { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4131 | __u64 logical; |
| 4132 | __u64 physical; |
| 4133 | __u64 length; |
| 4134 | __u32 flags = 0; |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4135 | int ret = 0; |
| 4136 | struct fiemap_extent_info *fieinfo = data; |
| 4137 | unsigned char blksize_bits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4138 | |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4139 | blksize_bits = inode->i_sb->s_blocksize_bits; |
| 4140 | logical = (__u64)newex->ec_block << blksize_bits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4141 | |
Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 4142 | if (newex->ec_start == 0) { |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4143 | /* |
| 4144 | * No extent in extent-tree contains block @newex->ec_start, |
| 4145 | * then the block may stay in 1)a hole or 2)delayed-extent. |
| 4146 | * |
| 4147 | * Holes or delayed-extents are processed as follows. |
| 4148 | * 1. lookup dirty pages with specified range in pagecache. |
| 4149 | * If no page is got, then there is no delayed-extent and |
| 4150 | * return with EXT_CONTINUE. |
| 4151 | * 2. find the 1st mapped buffer, |
| 4152 | * 3. check if the mapped buffer is both in the request range |
| 4153 | * and a delayed buffer. If not, there is no delayed-extent, |
| 4154 | * then return. |
| 4155 | * 4. a delayed-extent is found, the extent will be collected. |
| 4156 | */ |
| 4157 | ext4_lblk_t end = 0; |
| 4158 | pgoff_t last_offset; |
| 4159 | pgoff_t offset; |
| 4160 | pgoff_t index; |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4161 | pgoff_t start_index = 0; |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4162 | struct page **pages = NULL; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4163 | struct buffer_head *bh = NULL; |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4164 | struct buffer_head *head = NULL; |
| 4165 | unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *); |
| 4166 | |
| 4167 | pages = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4168 | if (pages == NULL) |
| 4169 | return -ENOMEM; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4170 | |
| 4171 | offset = logical >> PAGE_SHIFT; |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4172 | repeat: |
| 4173 | last_offset = offset; |
| 4174 | head = NULL; |
| 4175 | ret = find_get_pages_tag(inode->i_mapping, &offset, |
| 4176 | PAGECACHE_TAG_DIRTY, nr_pages, pages); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4177 | |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4178 | if (!(flags & FIEMAP_EXTENT_DELALLOC)) { |
| 4179 | /* First time, try to find a mapped buffer. */ |
| 4180 | if (ret == 0) { |
| 4181 | out: |
| 4182 | for (index = 0; index < ret; index++) |
| 4183 | page_cache_release(pages[index]); |
| 4184 | /* just a hole. */ |
| 4185 | kfree(pages); |
| 4186 | return EXT_CONTINUE; |
| 4187 | } |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4188 | index = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4189 | |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4190 | next_page: |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4191 | /* Try to find the 1st mapped buffer. */ |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4192 | end = ((__u64)pages[index]->index << PAGE_SHIFT) >> |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4193 | blksize_bits; |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4194 | if (!page_has_buffers(pages[index])) |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4195 | goto out; |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4196 | head = page_buffers(pages[index]); |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4197 | if (!head) |
| 4198 | goto out; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4199 | |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4200 | index++; |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4201 | bh = head; |
| 4202 | do { |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4203 | if (end >= newex->ec_block + |
| 4204 | newex->ec_len) |
| 4205 | /* The buffer is out of |
| 4206 | * the request range. |
| 4207 | */ |
| 4208 | goto out; |
| 4209 | |
| 4210 | if (buffer_mapped(bh) && |
| 4211 | end >= newex->ec_block) { |
| 4212 | start_index = index - 1; |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4213 | /* get the 1st mapped buffer. */ |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4214 | goto found_mapped_buffer; |
| 4215 | } |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4216 | |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4217 | bh = bh->b_this_page; |
| 4218 | end++; |
| 4219 | } while (bh != head); |
| 4220 | |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4221 | /* No mapped buffer in the range found in this page, |
| 4222 | * We need to look up next page. |
| 4223 | */ |
| 4224 | if (index >= ret) { |
| 4225 | /* There is no page left, but we need to limit |
| 4226 | * newex->ec_len. |
| 4227 | */ |
| 4228 | newex->ec_len = end - newex->ec_block; |
| 4229 | goto out; |
| 4230 | } |
| 4231 | goto next_page; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4232 | } else { |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4233 | /*Find contiguous delayed buffers. */ |
| 4234 | if (ret > 0 && pages[0]->index == last_offset) |
| 4235 | head = page_buffers(pages[0]); |
| 4236 | bh = head; |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4237 | index = 1; |
| 4238 | start_index = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4239 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4240 | |
| 4241 | found_mapped_buffer: |
| 4242 | if (bh != NULL && buffer_delay(bh)) { |
| 4243 | /* 1st or contiguous delayed buffer found. */ |
| 4244 | if (!(flags & FIEMAP_EXTENT_DELALLOC)) { |
| 4245 | /* |
| 4246 | * 1st delayed buffer found, record |
| 4247 | * the start of extent. |
| 4248 | */ |
| 4249 | flags |= FIEMAP_EXTENT_DELALLOC; |
| 4250 | newex->ec_block = end; |
| 4251 | logical = (__u64)end << blksize_bits; |
| 4252 | } |
| 4253 | /* Find contiguous delayed buffers. */ |
| 4254 | do { |
| 4255 | if (!buffer_delay(bh)) |
| 4256 | goto found_delayed_extent; |
| 4257 | bh = bh->b_this_page; |
| 4258 | end++; |
| 4259 | } while (bh != head); |
| 4260 | |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4261 | for (; index < ret; index++) { |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4262 | if (!page_has_buffers(pages[index])) { |
| 4263 | bh = NULL; |
| 4264 | break; |
| 4265 | } |
| 4266 | head = page_buffers(pages[index]); |
| 4267 | if (!head) { |
| 4268 | bh = NULL; |
| 4269 | break; |
| 4270 | } |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4271 | |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4272 | if (pages[index]->index != |
Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4273 | pages[start_index]->index + index |
| 4274 | - start_index) { |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4275 | /* Blocks are not contiguous. */ |
| 4276 | bh = NULL; |
| 4277 | break; |
| 4278 | } |
| 4279 | bh = head; |
| 4280 | do { |
| 4281 | if (!buffer_delay(bh)) |
| 4282 | /* Delayed-extent ends. */ |
| 4283 | goto found_delayed_extent; |
| 4284 | bh = bh->b_this_page; |
| 4285 | end++; |
| 4286 | } while (bh != head); |
| 4287 | } |
| 4288 | } else if (!(flags & FIEMAP_EXTENT_DELALLOC)) |
| 4289 | /* a hole found. */ |
| 4290 | goto out; |
| 4291 | |
| 4292 | found_delayed_extent: |
| 4293 | newex->ec_len = min(end - newex->ec_block, |
| 4294 | (ext4_lblk_t)EXT_INIT_MAX_LEN); |
| 4295 | if (ret == nr_pages && bh != NULL && |
| 4296 | newex->ec_len < EXT_INIT_MAX_LEN && |
| 4297 | buffer_delay(bh)) { |
| 4298 | /* Have not collected an extent and continue. */ |
| 4299 | for (index = 0; index < ret; index++) |
| 4300 | page_cache_release(pages[index]); |
| 4301 | goto repeat; |
| 4302 | } |
| 4303 | |
| 4304 | for (index = 0; index < ret; index++) |
| 4305 | page_cache_release(pages[index]); |
| 4306 | kfree(pages); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | physical = (__u64)newex->ec_start << blksize_bits; |
| 4310 | length = (__u64)newex->ec_len << blksize_bits; |
| 4311 | |
| 4312 | if (ex && ext4_ext_is_uninitialized(ex)) |
| 4313 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 4314 | |
Lukas Czerner | c03f8aa | 2011-06-06 00:06:52 -0400 | [diff] [blame] | 4315 | if (next == EXT_MAX_BLOCKS) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4316 | flags |= FIEMAP_EXTENT_LAST; |
| 4317 | |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4318 | ret = fiemap_fill_next_extent(fieinfo, logical, physical, |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4319 | length, flags); |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4320 | if (ret < 0) |
| 4321 | return ret; |
| 4322 | if (ret == 1) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4323 | return EXT_BREAK; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4324 | return EXT_CONTINUE; |
| 4325 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4326 | /* fiemap flags we can handle specified here */ |
| 4327 | #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) |
| 4328 | |
Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 4329 | static int ext4_xattr_fiemap(struct inode *inode, |
| 4330 | struct fiemap_extent_info *fieinfo) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4331 | { |
| 4332 | __u64 physical = 0; |
| 4333 | __u64 length; |
| 4334 | __u32 flags = FIEMAP_EXTENT_LAST; |
| 4335 | int blockbits = inode->i_sb->s_blocksize_bits; |
| 4336 | int error = 0; |
| 4337 | |
| 4338 | /* in-inode? */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4339 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4340 | struct ext4_iloc iloc; |
| 4341 | int offset; /* offset of xattr in inode */ |
| 4342 | |
| 4343 | error = ext4_get_inode_loc(inode, &iloc); |
| 4344 | if (error) |
| 4345 | return error; |
| 4346 | physical = iloc.bh->b_blocknr << blockbits; |
| 4347 | offset = EXT4_GOOD_OLD_INODE_SIZE + |
| 4348 | EXT4_I(inode)->i_extra_isize; |
| 4349 | physical += offset; |
| 4350 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
| 4351 | flags |= FIEMAP_EXTENT_DATA_INLINE; |
Curt Wohlgemuth | fd2dd9f | 2010-04-03 17:44:16 -0400 | [diff] [blame] | 4352 | brelse(iloc.bh); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4353 | } else { /* external block */ |
| 4354 | physical = EXT4_I(inode)->i_file_acl << blockbits; |
| 4355 | length = inode->i_sb->s_blocksize; |
| 4356 | } |
| 4357 | |
| 4358 | if (physical) |
| 4359 | error = fiemap_fill_next_extent(fieinfo, 0, physical, |
| 4360 | length, flags); |
| 4361 | return (error < 0 ? error : 0); |
| 4362 | } |
| 4363 | |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4364 | /* |
| 4365 | * ext4_ext_punch_hole |
| 4366 | * |
| 4367 | * Punches a hole of "length" bytes in a file starting |
| 4368 | * at byte "offset" |
| 4369 | * |
| 4370 | * @inode: The inode of the file to punch a hole in |
| 4371 | * @offset: The starting byte offset of the hole |
| 4372 | * @length: The length of the hole |
| 4373 | * |
| 4374 | * Returns the number of blocks removed or negative on err |
| 4375 | */ |
| 4376 | int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length) |
| 4377 | { |
| 4378 | struct inode *inode = file->f_path.dentry->d_inode; |
| 4379 | struct super_block *sb = inode->i_sb; |
| 4380 | struct ext4_ext_cache cache_ex; |
| 4381 | ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks; |
| 4382 | struct address_space *mapping = inode->i_mapping; |
| 4383 | struct ext4_map_blocks map; |
| 4384 | handle_t *handle; |
Allison Henderson | ba06208 | 2011-09-03 11:55:59 -0400 | [diff] [blame] | 4385 | loff_t first_page, last_page, page_len; |
| 4386 | loff_t first_page_offset, last_page_offset; |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4387 | int ret, credits, blocks_released, err = 0; |
| 4388 | |
Allison Henderson | 2be4751 | 2011-09-03 11:56:52 -0400 | [diff] [blame] | 4389 | /* No need to punch hole beyond i_size */ |
| 4390 | if (offset >= inode->i_size) |
| 4391 | return 0; |
| 4392 | |
| 4393 | /* |
| 4394 | * If the hole extends beyond i_size, set the hole |
| 4395 | * to end after the page that contains i_size |
| 4396 | */ |
| 4397 | if (offset + length > inode->i_size) { |
| 4398 | length = inode->i_size + |
| 4399 | PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) - |
| 4400 | offset; |
| 4401 | } |
| 4402 | |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4403 | first_block = (offset + sb->s_blocksize - 1) >> |
| 4404 | EXT4_BLOCK_SIZE_BITS(sb); |
| 4405 | last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); |
| 4406 | |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4407 | first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 4408 | last_page = (offset + length) >> PAGE_CACHE_SHIFT; |
| 4409 | |
| 4410 | first_page_offset = first_page << PAGE_CACHE_SHIFT; |
| 4411 | last_page_offset = last_page << PAGE_CACHE_SHIFT; |
| 4412 | |
| 4413 | /* |
| 4414 | * Write out all dirty pages to avoid race conditions |
| 4415 | * Then release them. |
| 4416 | */ |
| 4417 | if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { |
| 4418 | err = filemap_write_and_wait_range(mapping, |
Allison Henderson | 2be4751 | 2011-09-03 11:56:52 -0400 | [diff] [blame] | 4419 | offset, offset + length - 1); |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4420 | |
Allison Henderson | 2be4751 | 2011-09-03 11:56:52 -0400 | [diff] [blame] | 4421 | if (err) |
| 4422 | return err; |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | /* Now release the pages */ |
| 4426 | if (last_page_offset > first_page_offset) { |
| 4427 | truncate_inode_pages_range(mapping, first_page_offset, |
| 4428 | last_page_offset-1); |
| 4429 | } |
| 4430 | |
| 4431 | /* finish any pending end_io work */ |
| 4432 | ext4_flush_completed_IO(inode); |
| 4433 | |
| 4434 | credits = ext4_writepage_trans_blocks(inode); |
| 4435 | handle = ext4_journal_start(inode, credits); |
| 4436 | if (IS_ERR(handle)) |
| 4437 | return PTR_ERR(handle); |
| 4438 | |
| 4439 | err = ext4_orphan_add(handle, inode); |
| 4440 | if (err) |
| 4441 | goto out; |
| 4442 | |
| 4443 | /* |
Allison Henderson | ba06208 | 2011-09-03 11:55:59 -0400 | [diff] [blame] | 4444 | * Now we need to zero out the non-page-aligned data in the |
| 4445 | * pages at the start and tail of the hole, and unmap the buffer |
| 4446 | * heads for the block aligned regions of the page that were |
| 4447 | * completely zeroed. |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4448 | */ |
Allison Henderson | ba06208 | 2011-09-03 11:55:59 -0400 | [diff] [blame] | 4449 | if (first_page > last_page) { |
| 4450 | /* |
| 4451 | * If the file space being truncated is contained within a page |
| 4452 | * just zero out and unmap the middle of that page |
| 4453 | */ |
| 4454 | err = ext4_discard_partial_page_buffers(handle, |
| 4455 | mapping, offset, length, 0); |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4456 | |
Allison Henderson | ba06208 | 2011-09-03 11:55:59 -0400 | [diff] [blame] | 4457 | if (err) |
| 4458 | goto out; |
| 4459 | } else { |
| 4460 | /* |
| 4461 | * zero out and unmap the partial page that contains |
| 4462 | * the start of the hole |
| 4463 | */ |
| 4464 | page_len = first_page_offset - offset; |
| 4465 | if (page_len > 0) { |
| 4466 | err = ext4_discard_partial_page_buffers(handle, mapping, |
| 4467 | offset, page_len, 0); |
| 4468 | if (err) |
| 4469 | goto out; |
| 4470 | } |
| 4471 | |
| 4472 | /* |
| 4473 | * zero out and unmap the partial page that contains |
| 4474 | * the end of the hole |
| 4475 | */ |
| 4476 | page_len = offset + length - last_page_offset; |
| 4477 | if (page_len > 0) { |
| 4478 | err = ext4_discard_partial_page_buffers(handle, mapping, |
| 4479 | last_page_offset, page_len, 0); |
| 4480 | if (err) |
| 4481 | goto out; |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4482 | } |
| 4483 | } |
| 4484 | |
Allison Henderson | 2be4751 | 2011-09-03 11:56:52 -0400 | [diff] [blame] | 4485 | |
| 4486 | /* |
| 4487 | * If i_size is contained in the last page, we need to |
| 4488 | * unmap and zero the partial page after i_size |
| 4489 | */ |
| 4490 | if (inode->i_size >> PAGE_CACHE_SHIFT == last_page && |
| 4491 | inode->i_size % PAGE_CACHE_SIZE != 0) { |
| 4492 | |
| 4493 | page_len = PAGE_CACHE_SIZE - |
| 4494 | (inode->i_size & (PAGE_CACHE_SIZE - 1)); |
| 4495 | |
| 4496 | if (page_len > 0) { |
| 4497 | err = ext4_discard_partial_page_buffers(handle, |
| 4498 | mapping, inode->i_size, page_len, 0); |
| 4499 | |
| 4500 | if (err) |
| 4501 | goto out; |
| 4502 | } |
| 4503 | } |
| 4504 | |
Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4505 | /* If there are no blocks to remove, return now */ |
| 4506 | if (first_block >= last_block) |
| 4507 | goto out; |
| 4508 | |
| 4509 | down_write(&EXT4_I(inode)->i_data_sem); |
| 4510 | ext4_ext_invalidate_cache(inode); |
| 4511 | ext4_discard_preallocations(inode); |
| 4512 | |
| 4513 | /* |
| 4514 | * Loop over all the blocks and identify blocks |
| 4515 | * that need to be punched out |
| 4516 | */ |
| 4517 | iblock = first_block; |
| 4518 | blocks_released = 0; |
| 4519 | while (iblock < last_block) { |
| 4520 | max_blocks = last_block - iblock; |
| 4521 | num_blocks = 1; |
| 4522 | memset(&map, 0, sizeof(map)); |
| 4523 | map.m_lblk = iblock; |
| 4524 | map.m_len = max_blocks; |
| 4525 | ret = ext4_ext_map_blocks(handle, inode, &map, |
| 4526 | EXT4_GET_BLOCKS_PUNCH_OUT_EXT); |
| 4527 | |
| 4528 | if (ret > 0) { |
| 4529 | blocks_released += ret; |
| 4530 | num_blocks = ret; |
| 4531 | } else if (ret == 0) { |
| 4532 | /* |
| 4533 | * If map blocks could not find the block, |
| 4534 | * then it is in a hole. If the hole was |
| 4535 | * not already cached, then map blocks should |
| 4536 | * put it in the cache. So we can get the hole |
| 4537 | * out of the cache |
| 4538 | */ |
| 4539 | memset(&cache_ex, 0, sizeof(cache_ex)); |
| 4540 | if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) && |
| 4541 | !cache_ex.ec_start) { |
| 4542 | |
| 4543 | /* The hole is cached */ |
| 4544 | num_blocks = cache_ex.ec_block + |
| 4545 | cache_ex.ec_len - iblock; |
| 4546 | |
| 4547 | } else { |
| 4548 | /* The block could not be identified */ |
| 4549 | err = -EIO; |
| 4550 | break; |
| 4551 | } |
| 4552 | } else { |
| 4553 | /* Map blocks error */ |
| 4554 | err = ret; |
| 4555 | break; |
| 4556 | } |
| 4557 | |
| 4558 | if (num_blocks == 0) { |
| 4559 | /* This condition should never happen */ |
| 4560 | ext_debug("Block lookup failed"); |
| 4561 | err = -EIO; |
| 4562 | break; |
| 4563 | } |
| 4564 | |
| 4565 | iblock += num_blocks; |
| 4566 | } |
| 4567 | |
| 4568 | if (blocks_released > 0) { |
| 4569 | ext4_ext_invalidate_cache(inode); |
| 4570 | ext4_discard_preallocations(inode); |
| 4571 | } |
| 4572 | |
| 4573 | if (IS_SYNC(inode)) |
| 4574 | ext4_handle_sync(handle); |
| 4575 | |
| 4576 | up_write(&EXT4_I(inode)->i_data_sem); |
| 4577 | |
| 4578 | out: |
| 4579 | ext4_orphan_del(handle, inode); |
| 4580 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 4581 | ext4_mark_inode_dirty(handle, inode); |
| 4582 | ext4_journal_stop(handle); |
| 4583 | return err; |
| 4584 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4585 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4586 | __u64 start, __u64 len) |
| 4587 | { |
| 4588 | ext4_lblk_t start_blk; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4589 | int error = 0; |
| 4590 | |
| 4591 | /* fallback to generic here if not in extents fmt */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 4592 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4593 | return generic_block_fiemap(inode, fieinfo, start, len, |
| 4594 | ext4_get_block); |
| 4595 | |
| 4596 | if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) |
| 4597 | return -EBADR; |
| 4598 | |
| 4599 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 4600 | error = ext4_xattr_fiemap(inode, fieinfo); |
| 4601 | } else { |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4602 | ext4_lblk_t len_blks; |
| 4603 | __u64 last_blk; |
| 4604 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4605 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4606 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 4607 | if (last_blk >= EXT_MAX_BLOCKS) |
| 4608 | last_blk = EXT_MAX_BLOCKS-1; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4609 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4610 | |
| 4611 | /* |
| 4612 | * Walk the extent tree gathering extent information. |
| 4613 | * ext4_ext_fiemap_cb will push extents back to user. |
| 4614 | */ |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4615 | error = ext4_ext_walk_space(inode, start_blk, len_blks, |
| 4616 | ext4_ext_fiemap_cb, fieinfo); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | return error; |
| 4620 | } |