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