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