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 | * |
Adam Buchbinder | b8a07463 | 2016-03-09 23:49:05 -0500 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 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> |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 40 | #include <asm/uaccess.h> |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 41 | #include <linux/fiemap.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 42 | #include <linux/backing-dev.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 43 | #include "ext4_jbd2.h" |
Theodore Ts'o | 4a092d7 | 2012-11-28 13:03:30 -0500 | [diff] [blame] | 44 | #include "ext4_extents.h" |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 45 | #include "xattr.h" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 46 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 47 | #include <trace/events/ext4.h> |
| 48 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 49 | /* |
| 50 | * used by extent splitting. |
| 51 | */ |
| 52 | #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ |
| 53 | due to ENOSPC */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 54 | #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */ |
| 55 | #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 56 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 57 | #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */ |
| 58 | #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */ |
| 59 | |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 60 | static __le32 ext4_extent_block_csum(struct inode *inode, |
| 61 | struct ext4_extent_header *eh) |
| 62 | { |
| 63 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 64 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 65 | __u32 csum; |
| 66 | |
| 67 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh, |
| 68 | EXT4_EXTENT_TAIL_OFFSET(eh)); |
| 69 | return cpu_to_le32(csum); |
| 70 | } |
| 71 | |
| 72 | static int ext4_extent_block_csum_verify(struct inode *inode, |
| 73 | struct ext4_extent_header *eh) |
| 74 | { |
| 75 | struct ext4_extent_tail *et; |
| 76 | |
Dmitry Monakhov | 9aa5d32 | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 77 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 78 | return 1; |
| 79 | |
| 80 | et = find_ext4_extent_tail(eh); |
| 81 | if (et->et_checksum != ext4_extent_block_csum(inode, eh)) |
| 82 | return 0; |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | static void ext4_extent_block_csum_set(struct inode *inode, |
| 87 | struct ext4_extent_header *eh) |
| 88 | { |
| 89 | struct ext4_extent_tail *et; |
| 90 | |
Dmitry Monakhov | 9aa5d32 | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 91 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 92 | return; |
| 93 | |
| 94 | et = find_ext4_extent_tail(eh); |
| 95 | et->et_checksum = ext4_extent_block_csum(inode, eh); |
| 96 | } |
| 97 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 98 | static int ext4_split_extent(handle_t *handle, |
| 99 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 100 | struct ext4_ext_path **ppath, |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 101 | struct ext4_map_blocks *map, |
| 102 | int split_flag, |
| 103 | int flags); |
| 104 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 105 | static int ext4_split_extent_at(handle_t *handle, |
| 106 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 107 | struct ext4_ext_path **ppath, |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 108 | ext4_lblk_t split, |
| 109 | int split_flag, |
| 110 | int flags); |
| 111 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 112 | static int ext4_find_delayed_extent(struct inode *inode, |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 113 | struct extent_status *newes); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 114 | |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 115 | static int ext4_ext_truncate_extend_restart(handle_t *handle, |
| 116 | struct inode *inode, |
| 117 | int needed) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 118 | { |
| 119 | int err; |
| 120 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 121 | if (!ext4_handle_valid(handle)) |
| 122 | return 0; |
Theodore Ts'o | 7b80819 | 2016-04-25 23:13:17 -0400 | [diff] [blame] | 123 | if (handle->h_buffer_credits >= needed) |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 124 | return 0; |
Theodore Ts'o | 7b80819 | 2016-04-25 23:13:17 -0400 | [diff] [blame] | 125 | /* |
| 126 | * If we need to extend the journal get a few extra blocks |
| 127 | * while we're at it for efficiency's sake. |
| 128 | */ |
| 129 | needed += 3; |
| 130 | err = ext4_journal_extend(handle, needed - handle->h_buffer_credits); |
Theodore Ts'o | 0123c93 | 2008-08-01 20:57:54 -0400 | [diff] [blame] | 131 | if (err <= 0) |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 132 | return err; |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 133 | err = ext4_truncate_restart_trans(handle, inode, needed); |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 134 | if (err == 0) |
| 135 | err = -EAGAIN; |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 136 | |
| 137 | return err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
| 141 | * could return: |
| 142 | * - EROFS |
| 143 | * - ENOMEM |
| 144 | */ |
| 145 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
| 146 | struct ext4_ext_path *path) |
| 147 | { |
| 148 | if (path->p_bh) { |
| 149 | /* path points to block */ |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 150 | BUFFER_TRACE(path->p_bh, "get_write_access"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 151 | return ext4_journal_get_write_access(handle, path->p_bh); |
| 152 | } |
| 153 | /* path points to leaf/index in inode body */ |
| 154 | /* we use in-core data, no need to protect them */ |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * could return: |
| 160 | * - EROFS |
| 161 | * - ENOMEM |
| 162 | * - EIO |
| 163 | */ |
Darrick J. Wong | 2656497 | 2013-04-19 14:04:12 -0400 | [diff] [blame] | 164 | int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle, |
| 165 | struct inode *inode, struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 166 | { |
| 167 | int err; |
Dmitry Monakhov | 4b1f166 | 2014-07-27 22:28:15 -0400 | [diff] [blame] | 168 | |
| 169 | WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 170 | if (path->p_bh) { |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 171 | ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 172 | /* path points to block */ |
Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 173 | err = __ext4_handle_dirty_metadata(where, line, handle, |
| 174 | inode, path->p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 175 | } else { |
| 176 | /* path points to leaf/index in inode body */ |
| 177 | err = ext4_mark_inode_dirty(handle, inode); |
| 178 | } |
| 179 | return err; |
| 180 | } |
| 181 | |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 182 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 183 | struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 184 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 185 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 186 | if (path) { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 187 | int depth = path->p_depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 188 | struct ext4_extent *ex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 189 | |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 190 | /* |
| 191 | * Try to predict block placement assuming that we are |
| 192 | * filling in a file which will eventually be |
| 193 | * non-sparse --- i.e., in the case of libbfd writing |
| 194 | * an ELF object sections out-of-order but in a way |
| 195 | * the eventually results in a contiguous object or |
| 196 | * executable file, or some database extending a table |
| 197 | * space file. However, this is actually somewhat |
| 198 | * non-ideal if we are writing a sparse file such as |
| 199 | * qemu or KVM writing a raw image file that is going |
| 200 | * to stay fairly sparse, since it will end up |
| 201 | * fragmenting the file system's free space. Maybe we |
| 202 | * should have some hueristics or some way to allow |
| 203 | * userspace to pass a hint to file system, |
Tao Ma | b8d6568 | 2011-01-21 23:21:31 +0800 | [diff] [blame] | 204 | * especially if the latter case turns out to be |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 205 | * common. |
| 206 | */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 207 | ex = path[depth].p_ext; |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 208 | if (ex) { |
| 209 | ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); |
| 210 | ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); |
| 211 | |
| 212 | if (block > ext_block) |
| 213 | return ext_pblk + (block - ext_block); |
| 214 | else |
| 215 | return ext_pblk - (ext_block - block); |
| 216 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 217 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 218 | /* it looks like index is empty; |
| 219 | * try to find starting block from index itself */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 220 | if (path[depth].p_bh) |
| 221 | return path[depth].p_bh->b_blocknr; |
| 222 | } |
| 223 | |
| 224 | /* OK. use inode's group */ |
Eric Sandeen | f86186b | 2011-06-28 10:01:31 -0400 | [diff] [blame] | 225 | return ext4_inode_to_goal_block(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 228 | /* |
| 229 | * Allocation for a meta data block |
| 230 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 231 | static ext4_fsblk_t |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 232 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 233 | struct ext4_ext_path *path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 234 | struct ext4_extent *ex, int *err, unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 235 | { |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 236 | ext4_fsblk_t goal, newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 237 | |
| 238 | 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] | 239 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 240 | NULL, err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 241 | return newblock; |
| 242 | } |
| 243 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 244 | static inline int ext4_ext_space_block(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 245 | { |
| 246 | int size; |
| 247 | |
| 248 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 249 | / sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 250 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 251 | if (!check && size > 6) |
| 252 | size = 6; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 253 | #endif |
| 254 | return size; |
| 255 | } |
| 256 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 257 | 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] | 258 | { |
| 259 | int size; |
| 260 | |
| 261 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 262 | / sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 263 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 264 | if (!check && size > 5) |
| 265 | size = 5; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 266 | #endif |
| 267 | return size; |
| 268 | } |
| 269 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 270 | static inline int ext4_ext_space_root(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 271 | { |
| 272 | int size; |
| 273 | |
| 274 | size = sizeof(EXT4_I(inode)->i_data); |
| 275 | size -= sizeof(struct ext4_extent_header); |
| 276 | size /= sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 277 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 278 | if (!check && size > 3) |
| 279 | size = 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 280 | #endif |
| 281 | return size; |
| 282 | } |
| 283 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 284 | 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] | 285 | { |
| 286 | int size; |
| 287 | |
| 288 | size = sizeof(EXT4_I(inode)->i_data); |
| 289 | size -= sizeof(struct ext4_extent_header); |
| 290 | size /= sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 291 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 292 | if (!check && size > 4) |
| 293 | size = 4; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 294 | #endif |
| 295 | return size; |
| 296 | } |
| 297 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 298 | static inline int |
| 299 | ext4_force_split_extent_at(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 300 | struct ext4_ext_path **ppath, ext4_lblk_t lblk, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 301 | int nofail) |
| 302 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 303 | struct ext4_ext_path *path = *ppath; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 304 | int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext); |
| 305 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 306 | return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ? |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 307 | EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, |
| 308 | EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO | |
| 309 | (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0)); |
| 310 | } |
| 311 | |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 312 | /* |
| 313 | * Calculate the number of metadata blocks needed |
| 314 | * to allocate @blocks |
| 315 | * Worse case is one block per extent |
| 316 | */ |
Theodore Ts'o | 01f49d0 | 2011-01-10 12:13:03 -0500 | [diff] [blame] | 317 | 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] | 318 | { |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 319 | struct ext4_inode_info *ei = EXT4_I(inode); |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 320 | int idxs; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 321 | |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 322 | idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 323 | / sizeof(struct ext4_extent_idx)); |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 324 | |
| 325 | /* |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 326 | * If the new delayed allocation block is contiguous with the |
| 327 | * previous da block, it can share index blocks with the |
| 328 | * previous block, so we only need to allocate a new index |
| 329 | * block every idxs leaf blocks. At ldxs**2 blocks, we need |
| 330 | * an additional index block, and at ldxs**3 blocks, yet |
| 331 | * another index blocks. |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 332 | */ |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 333 | if (ei->i_da_metadata_calc_len && |
| 334 | ei->i_da_metadata_calc_last_lblock+1 == lblock) { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 335 | int num = 0; |
| 336 | |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 337 | if ((ei->i_da_metadata_calc_len % idxs) == 0) |
| 338 | num++; |
| 339 | if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0) |
| 340 | num++; |
| 341 | if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) { |
| 342 | num++; |
| 343 | ei->i_da_metadata_calc_len = 0; |
| 344 | } else |
| 345 | ei->i_da_metadata_calc_len++; |
| 346 | ei->i_da_metadata_calc_last_lblock++; |
| 347 | return num; |
| 348 | } |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 349 | |
Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 350 | /* |
| 351 | * In the worst case we need a new set of index blocks at |
| 352 | * every level of the inode's extent tree. |
| 353 | */ |
| 354 | ei->i_da_metadata_calc_len = 1; |
| 355 | ei->i_da_metadata_calc_last_lblock = lblock; |
| 356 | return ext_depth(inode) + 1; |
Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 359 | static int |
| 360 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 361 | { |
| 362 | int max; |
| 363 | |
| 364 | if (depth == ext_depth(inode)) { |
| 365 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 366 | max = ext4_ext_space_root(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 367 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 368 | max = ext4_ext_space_root_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 369 | } else { |
| 370 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 371 | max = ext4_ext_space_block(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 372 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 373 | max = ext4_ext_space_block_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | return max; |
| 377 | } |
| 378 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 379 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
| 380 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 381 | ext4_fsblk_t block = ext4_ext_pblock(ext); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 382 | int len = ext4_ext_get_actual_len(ext); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 383 | ext4_lblk_t lblock = le32_to_cpu(ext->ee_block); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 384 | |
Vegard Nossum | f70749c | 2016-06-30 11:53:46 -0400 | [diff] [blame] | 385 | /* |
| 386 | * We allow neither: |
| 387 | * - zero length |
| 388 | * - overflow/wrap-around |
| 389 | */ |
| 390 | if (lblock + len <= lblock) |
Theodore Ts'o | 31d4f3a | 2012-03-11 23:30:16 -0400 | [diff] [blame] | 391 | return 0; |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 392 | 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] | 393 | } |
| 394 | |
| 395 | static int ext4_valid_extent_idx(struct inode *inode, |
| 396 | struct ext4_extent_idx *ext_idx) |
| 397 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 398 | ext4_fsblk_t block = ext4_idx_pblock(ext_idx); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 399 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 400 | 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] | 401 | } |
| 402 | |
| 403 | static int ext4_valid_extent_entries(struct inode *inode, |
| 404 | struct ext4_extent_header *eh, |
| 405 | int depth) |
| 406 | { |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 407 | unsigned short entries; |
| 408 | if (eh->eh_entries == 0) |
| 409 | return 1; |
| 410 | |
| 411 | entries = le16_to_cpu(eh->eh_entries); |
| 412 | |
| 413 | if (depth == 0) { |
| 414 | /* leaf entries */ |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 415 | struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 416 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
| 417 | ext4_fsblk_t pblock = 0; |
| 418 | ext4_lblk_t lblock = 0; |
| 419 | ext4_lblk_t prev = 0; |
| 420 | int len = 0; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 421 | while (entries) { |
| 422 | if (!ext4_valid_extent(inode, ext)) |
| 423 | return 0; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 424 | |
| 425 | /* Check for overlapping extents */ |
| 426 | lblock = le32_to_cpu(ext->ee_block); |
| 427 | len = ext4_ext_get_actual_len(ext); |
| 428 | if ((lblock <= prev) && prev) { |
| 429 | pblock = ext4_ext_pblock(ext); |
| 430 | es->s_last_error_block = cpu_to_le64(pblock); |
| 431 | return 0; |
| 432 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 433 | ext++; |
| 434 | entries--; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 435 | prev = lblock + len - 1; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 436 | } |
| 437 | } else { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 438 | struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 439 | while (entries) { |
| 440 | if (!ext4_valid_extent_idx(inode, ext_idx)) |
| 441 | return 0; |
| 442 | ext_idx++; |
| 443 | entries--; |
| 444 | } |
| 445 | } |
| 446 | return 1; |
| 447 | } |
| 448 | |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 449 | static int __ext4_ext_check(const char *function, unsigned int line, |
| 450 | struct inode *inode, struct ext4_extent_header *eh, |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 451 | int depth, ext4_fsblk_t pblk) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 452 | { |
| 453 | const char *error_msg; |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 454 | int max = 0, err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 455 | |
| 456 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { |
| 457 | error_msg = "invalid magic"; |
| 458 | goto corrupted; |
| 459 | } |
| 460 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { |
| 461 | error_msg = "unexpected eh_depth"; |
| 462 | goto corrupted; |
| 463 | } |
| 464 | if (unlikely(eh->eh_max == 0)) { |
| 465 | error_msg = "invalid eh_max"; |
| 466 | goto corrupted; |
| 467 | } |
| 468 | max = ext4_ext_max_entries(inode, depth); |
| 469 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { |
| 470 | error_msg = "too large eh_max"; |
| 471 | goto corrupted; |
| 472 | } |
| 473 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { |
| 474 | error_msg = "invalid eh_entries"; |
| 475 | goto corrupted; |
| 476 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 477 | if (!ext4_valid_extent_entries(inode, eh, depth)) { |
| 478 | error_msg = "invalid extent entries"; |
| 479 | goto corrupted; |
| 480 | } |
Vegard Nossum | 7bc9491 | 2016-07-15 00:22:07 -0400 | [diff] [blame] | 481 | if (unlikely(depth > 32)) { |
| 482 | error_msg = "too large eh_depth"; |
| 483 | goto corrupted; |
| 484 | } |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 485 | /* Verify checksum on non-root extent tree nodes */ |
| 486 | if (ext_depth(inode) != depth && |
| 487 | !ext4_extent_block_csum_verify(inode, eh)) { |
| 488 | error_msg = "extent tree corrupted"; |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 489 | err = -EFSBADCRC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 490 | goto corrupted; |
| 491 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 492 | return 0; |
| 493 | |
| 494 | corrupted: |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 495 | ext4_error_inode(inode, function, line, 0, |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 496 | "pblk %llu bad header/extent: %s - magic %x, " |
| 497 | "entries %u, max %u(%u), depth %u(%u)", |
| 498 | (unsigned long long) pblk, error_msg, |
| 499 | le16_to_cpu(eh->eh_magic), |
| 500 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), |
| 501 | max, le16_to_cpu(eh->eh_depth), depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 502 | return err; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 503 | } |
| 504 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 505 | #define ext4_ext_check(inode, eh, depth, pblk) \ |
| 506 | __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk)) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 507 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 508 | int ext4_ext_check_inode(struct inode *inode) |
| 509 | { |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 510 | return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0); |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 511 | } |
| 512 | |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 513 | static struct buffer_head * |
| 514 | __read_extent_tree_block(const char *function, unsigned int line, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 515 | struct inode *inode, ext4_fsblk_t pblk, int depth, |
| 516 | int flags) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 517 | { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 518 | struct buffer_head *bh; |
| 519 | int err; |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 520 | |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 521 | bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 522 | if (unlikely(!bh)) |
| 523 | return ERR_PTR(-ENOMEM); |
| 524 | |
| 525 | if (!bh_uptodate_or_lock(bh)) { |
| 526 | trace_ext4_ext_load_extent(inode, pblk, _RET_IP_); |
| 527 | err = bh_submit_read(bh); |
| 528 | if (err < 0) |
| 529 | goto errout; |
| 530 | } |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 531 | if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE)) |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 532 | return bh; |
| 533 | err = __ext4_ext_check(function, line, inode, |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 534 | ext_block_hdr(bh), depth, pblk); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 535 | if (err) |
| 536 | goto errout; |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 537 | set_buffer_verified(bh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 538 | /* |
| 539 | * If this is a leaf block, cache all of its entries |
| 540 | */ |
| 541 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) { |
| 542 | struct ext4_extent_header *eh = ext_block_hdr(bh); |
| 543 | struct ext4_extent *ex = EXT_FIRST_EXTENT(eh); |
| 544 | ext4_lblk_t prev = 0; |
| 545 | int i; |
| 546 | |
| 547 | for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) { |
| 548 | unsigned int status = EXTENT_STATUS_WRITTEN; |
| 549 | ext4_lblk_t lblk = le32_to_cpu(ex->ee_block); |
| 550 | int len = ext4_ext_get_actual_len(ex); |
| 551 | |
| 552 | if (prev && (prev != lblk)) |
| 553 | ext4_es_cache_extent(inode, prev, |
| 554 | lblk - prev, ~0, |
| 555 | EXTENT_STATUS_HOLE); |
| 556 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 557 | if (ext4_ext_is_unwritten(ex)) |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 558 | status = EXTENT_STATUS_UNWRITTEN; |
| 559 | ext4_es_cache_extent(inode, lblk, len, |
| 560 | ext4_ext_pblock(ex), status); |
| 561 | prev = lblk + len; |
| 562 | } |
| 563 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 564 | return bh; |
| 565 | errout: |
| 566 | put_bh(bh); |
| 567 | return ERR_PTR(err); |
| 568 | |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 569 | } |
| 570 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 571 | #define read_extent_tree_block(inode, pblk, depth, flags) \ |
| 572 | __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \ |
| 573 | (depth), (flags)) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 574 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 575 | /* |
| 576 | * This function is called to cache a file's extent information in the |
| 577 | * extent status tree |
| 578 | */ |
| 579 | int ext4_ext_precache(struct inode *inode) |
| 580 | { |
| 581 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 582 | struct ext4_ext_path *path = NULL; |
| 583 | struct buffer_head *bh; |
| 584 | int i = 0, depth, ret = 0; |
| 585 | |
| 586 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 587 | return 0; /* not an extent-mapped inode */ |
| 588 | |
| 589 | down_read(&ei->i_data_sem); |
| 590 | depth = ext_depth(inode); |
| 591 | |
| 592 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), |
| 593 | GFP_NOFS); |
| 594 | if (path == NULL) { |
| 595 | up_read(&ei->i_data_sem); |
| 596 | return -ENOMEM; |
| 597 | } |
| 598 | |
| 599 | /* Don't cache anything if there are no external extent blocks */ |
| 600 | if (depth == 0) |
| 601 | goto out; |
| 602 | path[0].p_hdr = ext_inode_hdr(inode); |
| 603 | ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0); |
| 604 | if (ret) |
| 605 | goto out; |
| 606 | path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr); |
| 607 | while (i >= 0) { |
| 608 | /* |
| 609 | * If this is a leaf block or we've reached the end of |
| 610 | * the index block, go up |
| 611 | */ |
| 612 | if ((i == depth) || |
| 613 | path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) { |
| 614 | brelse(path[i].p_bh); |
| 615 | path[i].p_bh = NULL; |
| 616 | i--; |
| 617 | continue; |
| 618 | } |
| 619 | bh = read_extent_tree_block(inode, |
| 620 | ext4_idx_pblock(path[i].p_idx++), |
| 621 | depth - i - 1, |
| 622 | EXT4_EX_FORCE_CACHE); |
| 623 | if (IS_ERR(bh)) { |
| 624 | ret = PTR_ERR(bh); |
| 625 | break; |
| 626 | } |
| 627 | i++; |
| 628 | path[i].p_bh = bh; |
| 629 | path[i].p_hdr = ext_block_hdr(bh); |
| 630 | path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr); |
| 631 | } |
| 632 | ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED); |
| 633 | out: |
| 634 | up_read(&ei->i_data_sem); |
| 635 | ext4_ext_drop_refs(path); |
| 636 | kfree(path); |
| 637 | return ret; |
| 638 | } |
| 639 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 640 | #ifdef EXT_DEBUG |
| 641 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 642 | { |
| 643 | int k, l = path->p_depth; |
| 644 | |
| 645 | ext_debug("path:"); |
| 646 | for (k = 0; k <= l; k++, path++) { |
| 647 | if (path->p_idx) { |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 648 | 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] | 649 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 650 | } else if (path->p_ext) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 651 | ext_debug(" %d:[%d]%d:%llu ", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 652 | le32_to_cpu(path->p_ext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 653 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 654 | ext4_ext_get_actual_len(path->p_ext), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 655 | ext4_ext_pblock(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 656 | } else |
| 657 | ext_debug(" []"); |
| 658 | } |
| 659 | ext_debug("\n"); |
| 660 | } |
| 661 | |
| 662 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 663 | { |
| 664 | int depth = ext_depth(inode); |
| 665 | struct ext4_extent_header *eh; |
| 666 | struct ext4_extent *ex; |
| 667 | int i; |
| 668 | |
| 669 | if (!path) |
| 670 | return; |
| 671 | |
| 672 | eh = path[depth].p_hdr; |
| 673 | ex = EXT_FIRST_EXTENT(eh); |
| 674 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 675 | ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); |
| 676 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 677 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 678 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 679 | ext4_ext_is_unwritten(ex), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 680 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 681 | } |
| 682 | ext_debug("\n"); |
| 683 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 684 | |
| 685 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, |
| 686 | ext4_fsblk_t newblock, int level) |
| 687 | { |
| 688 | int depth = ext_depth(inode); |
| 689 | struct ext4_extent *ex; |
| 690 | |
| 691 | if (depth != level) { |
| 692 | struct ext4_extent_idx *idx; |
| 693 | idx = path[level].p_idx; |
| 694 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { |
| 695 | ext_debug("%d: move %d:%llu in new index %llu\n", level, |
| 696 | le32_to_cpu(idx->ei_block), |
| 697 | ext4_idx_pblock(idx), |
| 698 | newblock); |
| 699 | idx++; |
| 700 | } |
| 701 | |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | ex = path[depth].p_ext; |
| 706 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 707 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", |
| 708 | le32_to_cpu(ex->ee_block), |
| 709 | ext4_ext_pblock(ex), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 710 | ext4_ext_is_unwritten(ex), |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 711 | ext4_ext_get_actual_len(ex), |
| 712 | newblock); |
| 713 | ex++; |
| 714 | } |
| 715 | } |
| 716 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 717 | #else |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 718 | #define ext4_ext_show_path(inode, path) |
| 719 | #define ext4_ext_show_leaf(inode, path) |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 720 | #define ext4_ext_show_move(inode, path, newblock, level) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 721 | #endif |
| 722 | |
Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 723 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 724 | { |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 725 | int depth, i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 726 | |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 727 | if (!path) |
| 728 | return; |
| 729 | depth = path->p_depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 730 | for (i = 0; i <= depth; i++, path++) |
| 731 | if (path->p_bh) { |
| 732 | brelse(path->p_bh); |
| 733 | path->p_bh = NULL; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 738 | * ext4_ext_binsearch_idx: |
| 739 | * binary search for the closest index of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 740 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 741 | */ |
| 742 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 743 | ext4_ext_binsearch_idx(struct inode *inode, |
| 744 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 745 | { |
| 746 | struct ext4_extent_header *eh = path->p_hdr; |
| 747 | struct ext4_extent_idx *r, *l, *m; |
| 748 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 749 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 750 | ext_debug("binsearch for %u(idx): ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 751 | |
| 752 | l = EXT_FIRST_INDEX(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 753 | r = EXT_LAST_INDEX(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 754 | while (l <= r) { |
| 755 | m = l + (r - l) / 2; |
| 756 | if (block < le32_to_cpu(m->ei_block)) |
| 757 | r = m - 1; |
| 758 | else |
| 759 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 760 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
| 761 | m, le32_to_cpu(m->ei_block), |
| 762 | r, le32_to_cpu(r->ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | path->p_idx = l - 1; |
Zheng Liu | 4a3c3a5 | 2012-05-28 17:55:16 -0400 | [diff] [blame] | 766 | ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 767 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 768 | |
| 769 | #ifdef CHECK_BINSEARCH |
| 770 | { |
| 771 | struct ext4_extent_idx *chix, *ix; |
| 772 | int k; |
| 773 | |
| 774 | chix = ix = EXT_FIRST_INDEX(eh); |
| 775 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
| 776 | if (k != 0 && |
| 777 | 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] | 778 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
| 779 | "first=0x%p\n", k, |
| 780 | ix, EXT_FIRST_INDEX(eh)); |
| 781 | printk(KERN_DEBUG "%u <= %u\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 782 | le32_to_cpu(ix->ei_block), |
| 783 | le32_to_cpu(ix[-1].ei_block)); |
| 784 | } |
| 785 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 786 | <= le32_to_cpu(ix[-1].ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 787 | if (block < le32_to_cpu(ix->ei_block)) |
| 788 | break; |
| 789 | chix = ix; |
| 790 | } |
| 791 | BUG_ON(chix != path->p_idx); |
| 792 | } |
| 793 | #endif |
| 794 | |
| 795 | } |
| 796 | |
| 797 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 798 | * ext4_ext_binsearch: |
| 799 | * binary search for closest extent of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 800 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 801 | */ |
| 802 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 803 | ext4_ext_binsearch(struct inode *inode, |
| 804 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 805 | { |
| 806 | struct ext4_extent_header *eh = path->p_hdr; |
| 807 | struct ext4_extent *r, *l, *m; |
| 808 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 809 | if (eh->eh_entries == 0) { |
| 810 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 811 | * this leaf is empty: |
| 812 | * we get such a leaf in split/add case |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 813 | */ |
| 814 | return; |
| 815 | } |
| 816 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 817 | ext_debug("binsearch for %u: ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 818 | |
| 819 | l = EXT_FIRST_EXTENT(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 820 | r = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 821 | |
| 822 | while (l <= r) { |
| 823 | m = l + (r - l) / 2; |
| 824 | if (block < le32_to_cpu(m->ee_block)) |
| 825 | r = m - 1; |
| 826 | else |
| 827 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 828 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
| 829 | m, le32_to_cpu(m->ee_block), |
| 830 | r, le32_to_cpu(r->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | path->p_ext = l - 1; |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 834 | ext_debug(" -> %d:%llu:[%d]%d ", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 835 | le32_to_cpu(path->p_ext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 836 | ext4_ext_pblock(path->p_ext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 837 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 838 | ext4_ext_get_actual_len(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 839 | |
| 840 | #ifdef CHECK_BINSEARCH |
| 841 | { |
| 842 | struct ext4_extent *chex, *ex; |
| 843 | int k; |
| 844 | |
| 845 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 846 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 847 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 848 | <= le32_to_cpu(ex[-1].ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 849 | if (block < le32_to_cpu(ex->ee_block)) |
| 850 | break; |
| 851 | chex = ex; |
| 852 | } |
| 853 | BUG_ON(chex != path->p_ext); |
| 854 | } |
| 855 | #endif |
| 856 | |
| 857 | } |
| 858 | |
| 859 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
| 860 | { |
| 861 | struct ext4_extent_header *eh; |
| 862 | |
| 863 | eh = ext_inode_hdr(inode); |
| 864 | eh->eh_depth = 0; |
| 865 | eh->eh_entries = 0; |
| 866 | eh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 867 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 868 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | struct ext4_ext_path * |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 873 | ext4_find_extent(struct inode *inode, ext4_lblk_t block, |
| 874 | struct ext4_ext_path **orig_path, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 875 | { |
| 876 | struct ext4_extent_header *eh; |
| 877 | struct buffer_head *bh; |
Theodore Ts'o | 705912c | 2014-09-01 14:34:09 -0400 | [diff] [blame] | 878 | struct ext4_ext_path *path = orig_path ? *orig_path : NULL; |
| 879 | short int depth, i, ppos = 0; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 880 | int ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 881 | |
| 882 | eh = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 883 | depth = ext_depth(inode); |
Theodore Ts'o | 87dad44 | 2018-06-14 12:55:10 -0400 | [diff] [blame] | 884 | if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) { |
| 885 | EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d", |
| 886 | depth); |
| 887 | ret = -EFSCORRUPTED; |
| 888 | goto err; |
| 889 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 890 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 891 | if (path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 892 | ext4_ext_drop_refs(path); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 893 | if (depth > path[0].p_maxdepth) { |
| 894 | kfree(path); |
| 895 | *orig_path = path = NULL; |
| 896 | } |
| 897 | } |
| 898 | if (!path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 899 | /* account possible depth increase */ |
Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 900 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 901 | GFP_NOFS); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 902 | if (unlikely(!path)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 903 | return ERR_PTR(-ENOMEM); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 904 | path[0].p_maxdepth = depth + 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 905 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 906 | path[0].p_hdr = eh; |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 907 | path[0].p_bh = NULL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 908 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 909 | i = depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 910 | /* walk through the tree */ |
| 911 | while (i) { |
| 912 | ext_debug("depth %d: num %d, max %d\n", |
| 913 | 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] | 914 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 915 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 916 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 917 | path[ppos].p_depth = i; |
| 918 | path[ppos].p_ext = NULL; |
| 919 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 920 | bh = read_extent_tree_block(inode, path[ppos].p_block, --i, |
| 921 | flags); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 922 | if (IS_ERR(bh)) { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 923 | ret = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 924 | goto err; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 925 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 926 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 927 | eh = ext_block_hdr(bh); |
| 928 | ppos++; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 929 | path[ppos].p_bh = bh; |
| 930 | path[ppos].p_hdr = eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | path[ppos].p_depth = i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 934 | path[ppos].p_ext = NULL; |
| 935 | path[ppos].p_idx = NULL; |
| 936 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 937 | /* find extent */ |
| 938 | ext4_ext_binsearch(inode, path + ppos, block); |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 939 | /* if not an empty leaf */ |
| 940 | if (path[ppos].p_ext) |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 941 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 942 | |
| 943 | ext4_ext_show_path(inode, path); |
| 944 | |
| 945 | return path; |
| 946 | |
| 947 | err: |
| 948 | ext4_ext_drop_refs(path); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 949 | kfree(path); |
| 950 | if (orig_path) |
| 951 | *orig_path = NULL; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 952 | return ERR_PTR(ret); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 956 | * ext4_ext_insert_index: |
| 957 | * insert new index [@logical;@ptr] into the block at @curp; |
| 958 | * check where to insert: before @curp or after @curp |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 959 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 960 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 961 | struct ext4_ext_path *curp, |
| 962 | int logical, ext4_fsblk_t ptr) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 963 | { |
| 964 | struct ext4_extent_idx *ix; |
| 965 | int len, err; |
| 966 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 967 | err = ext4_ext_get_access(handle, inode, curp); |
| 968 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 969 | return err; |
| 970 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 971 | if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { |
| 972 | EXT4_ERROR_INODE(inode, |
| 973 | "logical %d == ei_block %d!", |
| 974 | logical, le32_to_cpu(curp->p_idx->ei_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 975 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 976 | } |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 977 | |
| 978 | if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) |
| 979 | >= le16_to_cpu(curp->p_hdr->eh_max))) { |
| 980 | EXT4_ERROR_INODE(inode, |
| 981 | "eh_entries %d >= eh_max %d!", |
| 982 | le16_to_cpu(curp->p_hdr->eh_entries), |
| 983 | le16_to_cpu(curp->p_hdr->eh_max)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 984 | return -EFSCORRUPTED; |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 985 | } |
| 986 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 987 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 988 | /* insert after */ |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 989 | ext_debug("insert new index %d after: %llu\n", logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 990 | ix = curp->p_idx + 1; |
| 991 | } else { |
| 992 | /* insert before */ |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 993 | ext_debug("insert new index %d before: %llu\n", logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 994 | ix = curp->p_idx; |
| 995 | } |
| 996 | |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 997 | len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; |
| 998 | BUG_ON(len < 0); |
| 999 | if (len > 0) { |
| 1000 | ext_debug("insert new index %d: " |
| 1001 | "move %d indices from 0x%p to 0x%p\n", |
| 1002 | logical, len, ix, ix + 1); |
| 1003 | memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); |
| 1004 | } |
| 1005 | |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 1006 | if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { |
| 1007 | EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1008 | return -EFSCORRUPTED; |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 1009 | } |
| 1010 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1011 | ix->ei_block = cpu_to_le32(logical); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1012 | ext4_idx_store_pblock(ix, ptr); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1013 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1014 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1015 | if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { |
| 1016 | EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1017 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1018 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1019 | |
| 1020 | err = ext4_ext_dirty(handle, inode, curp); |
| 1021 | ext4_std_error(inode->i_sb, err); |
| 1022 | |
| 1023 | return err; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1027 | * ext4_ext_split: |
| 1028 | * inserts new subtree into the path, using free index entry |
| 1029 | * at depth @at: |
| 1030 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 1031 | * - makes decision where to split |
| 1032 | * - moves remaining extents and index entries (right to the split point) |
| 1033 | * into the newly allocated blocks |
| 1034 | * - initializes subtree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1035 | */ |
| 1036 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1037 | unsigned int flags, |
| 1038 | struct ext4_ext_path *path, |
| 1039 | struct ext4_extent *newext, int at) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1040 | { |
| 1041 | struct buffer_head *bh = NULL; |
| 1042 | int depth = ext_depth(inode); |
| 1043 | struct ext4_extent_header *neh; |
| 1044 | struct ext4_extent_idx *fidx; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1045 | int i = at, k, m, a; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1046 | ext4_fsblk_t newblock, oldblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1047 | __le32 border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1048 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1049 | int err = 0; |
Sriram Rajagopalan | ab6d14e | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1050 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1051 | |
| 1052 | /* make decision: where to split? */ |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1053 | /* FIXME: now decision is simplest: at current extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1054 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1055 | /* if current leaf will be split, then we should use |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1056 | * border from split point */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1057 | if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { |
| 1058 | EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1059 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1060 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1061 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 1062 | border = path[depth].p_ext[1].ee_block; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1063 | ext_debug("leaf will be split." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1064 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1065 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1066 | } else { |
| 1067 | border = newext->ee_block; |
| 1068 | ext_debug("leaf will be added." |
| 1069 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1070 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1074 | * If error occurs, then we break processing |
| 1075 | * and mark filesystem read-only. index won't |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1076 | * be inserted and tree will be in consistent |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1077 | * state. Next mount will repair buffers too. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1078 | */ |
| 1079 | |
| 1080 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1081 | * Get array to track all allocated blocks. |
| 1082 | * We need this to handle errors and free blocks |
| 1083 | * upon them. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1084 | */ |
Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 1085 | ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1086 | if (!ablocks) |
| 1087 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1088 | |
| 1089 | /* allocate all needed blocks */ |
| 1090 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
| 1091 | for (a = 0; a < depth - at; a++) { |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1092 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1093 | newext, &err, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1094 | if (newblock == 0) |
| 1095 | goto cleanup; |
| 1096 | ablocks[a] = newblock; |
| 1097 | } |
| 1098 | |
| 1099 | /* initialize new leaf */ |
| 1100 | newblock = ablocks[--a]; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1101 | if (unlikely(newblock == 0)) { |
| 1102 | EXT4_ERROR_INODE(inode, "newblock == 0!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1103 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1104 | goto cleanup; |
| 1105 | } |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1106 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1107 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1108 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1109 | goto cleanup; |
| 1110 | } |
| 1111 | lock_buffer(bh); |
| 1112 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1113 | err = ext4_journal_get_create_access(handle, bh); |
| 1114 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1115 | goto cleanup; |
| 1116 | |
| 1117 | neh = ext_block_hdr(bh); |
| 1118 | neh->eh_entries = 0; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1119 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1120 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 1121 | neh->eh_depth = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1122 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1123 | /* move remainder of path[depth] to the new leaf */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1124 | if (unlikely(path[depth].p_hdr->eh_entries != |
| 1125 | path[depth].p_hdr->eh_max)) { |
| 1126 | EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", |
| 1127 | path[depth].p_hdr->eh_entries, |
| 1128 | path[depth].p_hdr->eh_max); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1129 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1130 | goto cleanup; |
| 1131 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1132 | /* start copy from next extent */ |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1133 | m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; |
| 1134 | ext4_ext_show_move(inode, path, newblock, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1135 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1136 | struct ext4_extent *ex; |
| 1137 | ex = EXT_FIRST_EXTENT(neh); |
| 1138 | memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1139 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Sriram Rajagopalan | ab6d14e | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1142 | /* zero out unused area in the extent block */ |
| 1143 | ext_size = sizeof(struct ext4_extent_header) + |
| 1144 | sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries); |
| 1145 | memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size); |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1146 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1147 | set_buffer_uptodate(bh); |
| 1148 | unlock_buffer(bh); |
| 1149 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1150 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1151 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1152 | goto cleanup; |
| 1153 | brelse(bh); |
| 1154 | bh = NULL; |
| 1155 | |
| 1156 | /* correct old leaf */ |
| 1157 | if (m) { |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1158 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1159 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1160 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1161 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1162 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1163 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1164 | goto cleanup; |
| 1165 | |
| 1166 | } |
| 1167 | |
| 1168 | /* create intermediate indexes */ |
| 1169 | k = depth - at - 1; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1170 | if (unlikely(k < 0)) { |
| 1171 | EXT4_ERROR_INODE(inode, "k %d < 0!", k); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1172 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1173 | goto cleanup; |
| 1174 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1175 | if (k) |
| 1176 | ext_debug("create %d intermediate indices\n", k); |
| 1177 | /* insert new index into current index block */ |
| 1178 | /* current depth stored in i var */ |
| 1179 | i = depth - 1; |
| 1180 | while (k--) { |
| 1181 | oldblock = newblock; |
| 1182 | newblock = ablocks[--a]; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1183 | bh = sb_getblk(inode->i_sb, newblock); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1184 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1185 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1186 | goto cleanup; |
| 1187 | } |
| 1188 | lock_buffer(bh); |
| 1189 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1190 | err = ext4_journal_get_create_access(handle, bh); |
| 1191 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1192 | goto cleanup; |
| 1193 | |
| 1194 | neh = ext_block_hdr(bh); |
| 1195 | neh->eh_entries = cpu_to_le16(1); |
| 1196 | neh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1197 | 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] | 1198 | neh->eh_depth = cpu_to_le16(depth - i); |
| 1199 | fidx = EXT_FIRST_INDEX(neh); |
| 1200 | fidx->ei_block = border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1201 | ext4_idx_store_pblock(fidx, oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1202 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1203 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
| 1204 | i, newblock, le32_to_cpu(border), oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1205 | |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1206 | /* move remainder of path[i] to the new index block */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1207 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != |
| 1208 | EXT_LAST_INDEX(path[i].p_hdr))) { |
| 1209 | EXT4_ERROR_INODE(inode, |
| 1210 | "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", |
| 1211 | le32_to_cpu(path[i].p_ext->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1212 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1213 | goto cleanup; |
| 1214 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1215 | /* start copy indexes */ |
| 1216 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
| 1217 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
| 1218 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 1219 | ext4_ext_show_move(inode, path, newblock, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1220 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1221 | memmove(++fidx, path[i].p_idx, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1222 | sizeof(struct ext4_extent_idx) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1223 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1224 | } |
Sriram Rajagopalan | ab6d14e | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1225 | /* zero out unused area in the extent block */ |
| 1226 | ext_size = sizeof(struct ext4_extent_header) + |
| 1227 | (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries)); |
| 1228 | memset(bh->b_data + ext_size, 0, |
| 1229 | inode->i_sb->s_blocksize - ext_size); |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1230 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1231 | set_buffer_uptodate(bh); |
| 1232 | unlock_buffer(bh); |
| 1233 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1234 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1235 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1236 | goto cleanup; |
| 1237 | brelse(bh); |
| 1238 | bh = NULL; |
| 1239 | |
| 1240 | /* correct old index */ |
| 1241 | if (m) { |
| 1242 | err = ext4_ext_get_access(handle, inode, path + i); |
| 1243 | if (err) |
| 1244 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1245 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1246 | err = ext4_ext_dirty(handle, inode, path + i); |
| 1247 | if (err) |
| 1248 | goto cleanup; |
| 1249 | } |
| 1250 | |
| 1251 | i--; |
| 1252 | } |
| 1253 | |
| 1254 | /* insert new index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1255 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 1256 | le32_to_cpu(border), newblock); |
| 1257 | |
| 1258 | cleanup: |
| 1259 | if (bh) { |
| 1260 | if (buffer_locked(bh)) |
| 1261 | unlock_buffer(bh); |
| 1262 | brelse(bh); |
| 1263 | } |
| 1264 | |
| 1265 | if (err) { |
| 1266 | /* free all allocated blocks in error case */ |
| 1267 | for (i = 0; i < depth; i++) { |
| 1268 | if (!ablocks[i]) |
| 1269 | continue; |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1270 | ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1271 | EXT4_FREE_BLOCKS_METADATA); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | kfree(ablocks); |
| 1275 | |
| 1276 | return err; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1280 | * ext4_ext_grow_indepth: |
| 1281 | * implements tree growing procedure: |
| 1282 | * - allocates new block |
| 1283 | * - moves top-level data (index block or leaf) into the new block |
| 1284 | * - initializes new top-level, creating index that points to the |
| 1285 | * just created block |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1286 | */ |
| 1287 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1288 | unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1289 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1290 | struct ext4_extent_header *neh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1291 | struct buffer_head *bh; |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1292 | ext4_fsblk_t newblock, goal = 0; |
| 1293 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1294 | int err = 0; |
Sriram Rajagopalan | ab6d14e | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1295 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1296 | |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1297 | /* Try to prepend new index to old one */ |
| 1298 | if (ext_depth(inode)) |
| 1299 | goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode))); |
| 1300 | if (goal > le32_to_cpu(es->s_first_data_block)) { |
| 1301 | flags |= EXT4_MB_HINT_TRY_GOAL; |
| 1302 | goal--; |
| 1303 | } else |
| 1304 | goal = ext4_inode_to_goal_block(inode); |
| 1305 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 1306 | NULL, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1307 | if (newblock == 0) |
| 1308 | return err; |
| 1309 | |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1310 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1311 | if (unlikely(!bh)) |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1312 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1313 | lock_buffer(bh); |
| 1314 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1315 | err = ext4_journal_get_create_access(handle, bh); |
| 1316 | if (err) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1317 | unlock_buffer(bh); |
| 1318 | goto out; |
| 1319 | } |
| 1320 | |
Sriram Rajagopalan | ab6d14e | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1321 | ext_size = sizeof(EXT4_I(inode)->i_data); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1322 | /* move top-level index/leaf into new block */ |
Sriram Rajagopalan | ab6d14e | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1323 | memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size); |
| 1324 | /* zero out unused area in the extent block */ |
| 1325 | memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1326 | |
| 1327 | /* set size of new block */ |
| 1328 | neh = ext_block_hdr(bh); |
| 1329 | /* old root could have indexes or leaves |
| 1330 | * so calculate e_max right way */ |
| 1331 | if (ext_depth(inode)) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1332 | 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] | 1333 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1334 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1335 | neh->eh_magic = EXT4_EXT_MAGIC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1336 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1337 | set_buffer_uptodate(bh); |
| 1338 | unlock_buffer(bh); |
| 1339 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1340 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1341 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1342 | goto out; |
| 1343 | |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1344 | /* Update top-level index: num,max,pointer */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1345 | neh = ext_inode_hdr(inode); |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1346 | neh->eh_entries = cpu_to_le16(1); |
| 1347 | ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); |
| 1348 | if (neh->eh_depth == 0) { |
| 1349 | /* Root extent block becomes index block */ |
| 1350 | neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); |
| 1351 | EXT_FIRST_INDEX(neh)->ei_block = |
| 1352 | EXT_FIRST_EXTENT(neh)->ee_block; |
| 1353 | } |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1354 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1355 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 1356 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1357 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1358 | |
Wei Yongjun | ba39ebb | 2012-09-27 09:37:53 -0400 | [diff] [blame] | 1359 | le16_add_cpu(&neh->eh_depth, 1); |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1360 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1361 | out: |
| 1362 | brelse(bh); |
| 1363 | |
| 1364 | return err; |
| 1365 | } |
| 1366 | |
| 1367 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1368 | * ext4_ext_create_new_leaf: |
| 1369 | * finds empty index and adds new leaf. |
| 1370 | * if no free index is found, then it requests in-depth growing. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1371 | */ |
| 1372 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1373 | unsigned int mb_flags, |
| 1374 | unsigned int gb_flags, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1375 | struct ext4_ext_path **ppath, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1376 | struct ext4_extent *newext) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1377 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1378 | struct ext4_ext_path *path = *ppath; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1379 | struct ext4_ext_path *curp; |
| 1380 | int depth, i, err = 0; |
| 1381 | |
| 1382 | repeat: |
| 1383 | i = depth = ext_depth(inode); |
| 1384 | |
| 1385 | /* walk up to the tree and look for free index entry */ |
| 1386 | curp = path + depth; |
| 1387 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1388 | i--; |
| 1389 | curp--; |
| 1390 | } |
| 1391 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1392 | /* we use already allocated block for index block, |
| 1393 | * so subsequent data blocks should be contiguous */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1394 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1395 | /* if we found index with free entry, then use that |
| 1396 | * entry: create all needed subtree and add new leaf */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1397 | err = ext4_ext_split(handle, inode, mb_flags, path, newext, i); |
Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1398 | if (err) |
| 1399 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1400 | |
| 1401 | /* refill path */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1402 | path = ext4_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1403 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1404 | ppath, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1405 | if (IS_ERR(path)) |
| 1406 | err = PTR_ERR(path); |
| 1407 | } else { |
| 1408 | /* tree is full, time to grow in depth */ |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1409 | err = ext4_ext_grow_indepth(handle, inode, mb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1410 | if (err) |
| 1411 | goto out; |
| 1412 | |
| 1413 | /* refill path */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1414 | path = ext4_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1415 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1416 | ppath, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1417 | if (IS_ERR(path)) { |
| 1418 | err = PTR_ERR(path); |
| 1419 | goto out; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1423 | * only first (depth 0 -> 1) produces free space; |
| 1424 | * in all other cases we have to split the grown tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1425 | */ |
| 1426 | depth = ext_depth(inode); |
| 1427 | 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] | 1428 | /* now we need to split */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1429 | goto repeat; |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | out: |
| 1434 | return err; |
| 1435 | } |
| 1436 | |
| 1437 | /* |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1438 | * search the closest allocated block to the left for *logical |
| 1439 | * and returns it at @logical + it's physical address at @phys |
| 1440 | * if *logical is the smallest allocated block, the function |
| 1441 | * returns 0 at @phys |
| 1442 | * return value contains 0 (success) or error code |
| 1443 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1444 | static int ext4_ext_search_left(struct inode *inode, |
| 1445 | struct ext4_ext_path *path, |
| 1446 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1447 | { |
| 1448 | struct ext4_extent_idx *ix; |
| 1449 | struct ext4_extent *ex; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1450 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1451 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1452 | if (unlikely(path == NULL)) { |
| 1453 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1454 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1455 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1456 | depth = path->p_depth; |
| 1457 | *phys = 0; |
| 1458 | |
| 1459 | if (depth == 0 && path->p_ext == NULL) |
| 1460 | return 0; |
| 1461 | |
| 1462 | /* usually extent in the path covers blocks smaller |
| 1463 | * then *logical, but it can be that extent is the |
| 1464 | * first one in the file */ |
| 1465 | |
| 1466 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1467 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1468 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1469 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1470 | EXT4_ERROR_INODE(inode, |
| 1471 | "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", |
| 1472 | *logical, le32_to_cpu(ex->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1473 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1474 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1475 | while (--depth >= 0) { |
| 1476 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1477 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1478 | EXT4_ERROR_INODE(inode, |
| 1479 | "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1480 | ix != NULL ? le32_to_cpu(ix->ei_block) : 0, |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1481 | EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1482 | 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] | 1483 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1484 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1485 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1486 | } |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1490 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1491 | EXT4_ERROR_INODE(inode, |
| 1492 | "logical %d < ee_block %d + ee_len %d!", |
| 1493 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1494 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1495 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1496 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1497 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1498 | *phys = ext4_ext_pblock(ex) + ee_len - 1; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | /* |
| 1503 | * search the closest allocated block to the right for *logical |
| 1504 | * and returns it at @logical + it's physical address at @phys |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 1505 | * if *logical is the largest allocated block, the function |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1506 | * returns 0 at @phys |
| 1507 | * return value contains 0 (success) or error code |
| 1508 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1509 | static int ext4_ext_search_right(struct inode *inode, |
| 1510 | struct ext4_ext_path *path, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1511 | ext4_lblk_t *logical, ext4_fsblk_t *phys, |
| 1512 | struct ext4_extent **ret_ex) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1513 | { |
| 1514 | struct buffer_head *bh = NULL; |
| 1515 | struct ext4_extent_header *eh; |
| 1516 | struct ext4_extent_idx *ix; |
| 1517 | struct ext4_extent *ex; |
| 1518 | ext4_fsblk_t block; |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1519 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
| 1520 | int ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1521 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1522 | if (unlikely(path == NULL)) { |
| 1523 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1524 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1525 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1526 | depth = path->p_depth; |
| 1527 | *phys = 0; |
| 1528 | |
| 1529 | if (depth == 0 && path->p_ext == NULL) |
| 1530 | return 0; |
| 1531 | |
| 1532 | /* usually extent in the path covers blocks smaller |
| 1533 | * then *logical, but it can be that extent is the |
| 1534 | * first one in the file */ |
| 1535 | |
| 1536 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1537 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1538 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1539 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1540 | EXT4_ERROR_INODE(inode, |
| 1541 | "first_extent(path[%d].p_hdr) != ex", |
| 1542 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1543 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1544 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1545 | while (--depth >= 0) { |
| 1546 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1547 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1548 | EXT4_ERROR_INODE(inode, |
| 1549 | "ix != EXT_FIRST_INDEX *logical %d!", |
| 1550 | *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1551 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1552 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1553 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1554 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1555 | } |
| 1556 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1557 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1558 | EXT4_ERROR_INODE(inode, |
| 1559 | "logical %d < ee_block %d + ee_len %d!", |
| 1560 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1561 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1562 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1563 | |
| 1564 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1565 | /* next allocated block in this leaf */ |
| 1566 | ex++; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1567 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /* go up and search for index to the right */ |
| 1571 | while (--depth >= 0) { |
| 1572 | ix = path[depth].p_idx; |
| 1573 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1574 | goto got_index; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1575 | } |
| 1576 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1577 | /* we've gone up to the root and found no index to the right */ |
| 1578 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1579 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1580 | got_index: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1581 | /* we've found index to the right, let's |
| 1582 | * follow it and find the closest allocated |
| 1583 | * block to the right */ |
| 1584 | ix++; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1585 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1586 | while (++depth < path->p_depth) { |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1587 | /* subtract from p_depth to get proper eh_depth */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1588 | bh = read_extent_tree_block(inode, block, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1589 | path->p_depth - depth, 0); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1590 | if (IS_ERR(bh)) |
| 1591 | return PTR_ERR(bh); |
| 1592 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1593 | ix = EXT_FIRST_INDEX(eh); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1594 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1595 | put_bh(bh); |
| 1596 | } |
| 1597 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1598 | bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1599 | if (IS_ERR(bh)) |
| 1600 | return PTR_ERR(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1601 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1602 | ex = EXT_FIRST_EXTENT(eh); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1603 | found_extent: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1604 | *logical = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1605 | *phys = ext4_ext_pblock(ex); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1606 | *ret_ex = ex; |
| 1607 | if (bh) |
| 1608 | put_bh(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1609 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1613 | * ext4_ext_next_allocated_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1614 | * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1615 | * NOTE: it considers block number from index entry as |
| 1616 | * allocated block. Thus, index entries have to be consistent |
| 1617 | * with leaves. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1618 | */ |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 1619 | ext4_lblk_t |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1620 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1621 | { |
| 1622 | int depth; |
| 1623 | |
| 1624 | BUG_ON(path == NULL); |
| 1625 | depth = path->p_depth; |
| 1626 | |
| 1627 | if (depth == 0 && path->p_ext == NULL) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1628 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1629 | |
| 1630 | while (depth >= 0) { |
| 1631 | if (depth == path->p_depth) { |
| 1632 | /* leaf */ |
Curt Wohlgemuth | 6f8ff53 | 2011-10-26 04:38:59 -0400 | [diff] [blame] | 1633 | if (path[depth].p_ext && |
| 1634 | path[depth].p_ext != |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1635 | EXT_LAST_EXTENT(path[depth].p_hdr)) |
| 1636 | return le32_to_cpu(path[depth].p_ext[1].ee_block); |
| 1637 | } else { |
| 1638 | /* index */ |
| 1639 | if (path[depth].p_idx != |
| 1640 | EXT_LAST_INDEX(path[depth].p_hdr)) |
| 1641 | return le32_to_cpu(path[depth].p_idx[1].ei_block); |
| 1642 | } |
| 1643 | depth--; |
| 1644 | } |
| 1645 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1646 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1650 | * ext4_ext_next_leaf_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1651 | * returns first allocated block from next leaf or EXT_MAX_BLOCKS |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1652 | */ |
Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1653 | 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] | 1654 | { |
| 1655 | int depth; |
| 1656 | |
| 1657 | BUG_ON(path == NULL); |
| 1658 | depth = path->p_depth; |
| 1659 | |
| 1660 | /* zero-tree has no leaf blocks at all */ |
| 1661 | if (depth == 0) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1662 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1663 | |
| 1664 | /* go to index block */ |
| 1665 | depth--; |
| 1666 | |
| 1667 | while (depth >= 0) { |
| 1668 | if (path[depth].p_idx != |
| 1669 | EXT_LAST_INDEX(path[depth].p_hdr)) |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1670 | return (ext4_lblk_t) |
| 1671 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1672 | depth--; |
| 1673 | } |
| 1674 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1675 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1679 | * ext4_ext_correct_indexes: |
| 1680 | * if leaf gets modified and modified extent is first in the leaf, |
| 1681 | * then we have to correct all indexes above. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1682 | * TODO: do we need to correct tree in all cases? |
| 1683 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1684 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1685 | struct ext4_ext_path *path) |
| 1686 | { |
| 1687 | struct ext4_extent_header *eh; |
| 1688 | int depth = ext_depth(inode); |
| 1689 | struct ext4_extent *ex; |
| 1690 | __le32 border; |
| 1691 | int k, err = 0; |
| 1692 | |
| 1693 | eh = path[depth].p_hdr; |
| 1694 | ex = path[depth].p_ext; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1695 | |
| 1696 | if (unlikely(ex == NULL || eh == NULL)) { |
| 1697 | EXT4_ERROR_INODE(inode, |
| 1698 | "ex %p == NULL or eh %p == NULL", ex, eh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1699 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1700 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1701 | |
| 1702 | if (depth == 0) { |
| 1703 | /* there is no tree at all */ |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
| 1707 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1708 | /* we correct tree if first leaf got modified only */ |
| 1709 | return 0; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1713 | * TODO: we need correction if border is smaller than current one |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1714 | */ |
| 1715 | k = depth - 1; |
| 1716 | border = path[depth].p_ext->ee_block; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1717 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1718 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1719 | return err; |
| 1720 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1721 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1722 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1723 | return err; |
| 1724 | |
| 1725 | while (k--) { |
| 1726 | /* change all left-side indexes */ |
| 1727 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1728 | break; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1729 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1730 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1731 | break; |
| 1732 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1733 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1734 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | return err; |
| 1739 | } |
| 1740 | |
Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 1741 | int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1742 | ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, |
| 1743 | struct ext4_extent *ex2) |
| 1744 | { |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1745 | unsigned short ext1_ee_len, ext2_ee_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1746 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1747 | if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2)) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1748 | return 0; |
| 1749 | |
| 1750 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1751 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1752 | |
| 1753 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1754 | le32_to_cpu(ex2->ee_block)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1755 | return 0; |
| 1756 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1757 | /* |
| 1758 | * To allow future support for preallocated extents to be added |
| 1759 | * as an RO_COMPAT feature, refuse to merge to extents if |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1760 | * this can result in the top bit of ee_len being set. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1761 | */ |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1762 | if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN) |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1763 | return 0; |
Jan Kara | 109811c | 2016-03-08 23:36:46 -0500 | [diff] [blame] | 1764 | /* |
| 1765 | * The check for IO to unwritten extent is somewhat racy as we |
| 1766 | * increment i_unwritten / set EXT4_STATE_DIO_UNWRITTEN only after |
| 1767 | * dropping i_data_sem. But reserved blocks should save us in that |
| 1768 | * case. |
| 1769 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1770 | if (ext4_ext_is_unwritten(ex1) && |
Darrick J. Wong | a9b8241 | 2014-02-20 21:17:35 -0500 | [diff] [blame] | 1771 | (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) || |
| 1772 | atomic_read(&EXT4_I(inode)->i_unwritten) || |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1773 | (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN))) |
Darrick J. Wong | a9b8241 | 2014-02-20 21:17:35 -0500 | [diff] [blame] | 1774 | return 0; |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1775 | #ifdef AGGRESSIVE_TEST |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1776 | if (ext1_ee_len >= 4) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1777 | return 0; |
| 1778 | #endif |
| 1779 | |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1780 | if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1781 | return 1; |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | /* |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1786 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1787 | * It always tries to merge towards right. If you want to merge towards |
| 1788 | * left, pass "ex - 1" as argument instead of "ex". |
| 1789 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1790 | * 1 if they got merged. |
| 1791 | */ |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1792 | static int ext4_ext_try_to_merge_right(struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1793 | struct ext4_ext_path *path, |
| 1794 | struct ext4_extent *ex) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1795 | { |
| 1796 | struct ext4_extent_header *eh; |
| 1797 | unsigned int depth, len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1798 | int merge_done = 0, unwritten; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1799 | |
| 1800 | depth = ext_depth(inode); |
| 1801 | BUG_ON(path[depth].p_hdr == NULL); |
| 1802 | eh = path[depth].p_hdr; |
| 1803 | |
| 1804 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1805 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1806 | break; |
| 1807 | /* merge with next extent! */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1808 | unwritten = ext4_ext_is_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1809 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1810 | + ext4_ext_get_actual_len(ex + 1)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1811 | if (unwritten) |
| 1812 | ext4_ext_mark_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1813 | |
| 1814 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1815 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1816 | * sizeof(struct ext4_extent); |
| 1817 | memmove(ex + 1, ex + 2, len); |
| 1818 | } |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1819 | le16_add_cpu(&eh->eh_entries, -1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1820 | merge_done = 1; |
| 1821 | WARN_ON(eh->eh_entries == 0); |
| 1822 | if (!eh->eh_entries) |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1823 | EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | return merge_done; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1830 | * This function does a very simple check to see if we can collapse |
| 1831 | * an extent tree with a single extent tree leaf block into the inode. |
| 1832 | */ |
| 1833 | static void ext4_ext_try_to_merge_up(handle_t *handle, |
| 1834 | struct inode *inode, |
| 1835 | struct ext4_ext_path *path) |
| 1836 | { |
| 1837 | size_t s; |
| 1838 | unsigned max_root = ext4_ext_space_root(inode, 0); |
| 1839 | ext4_fsblk_t blk; |
| 1840 | |
| 1841 | if ((path[0].p_depth != 1) || |
| 1842 | (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) || |
| 1843 | (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root)) |
| 1844 | return; |
| 1845 | |
| 1846 | /* |
| 1847 | * We need to modify the block allocation bitmap and the block |
| 1848 | * group descriptor to release the extent tree block. If we |
| 1849 | * can't get the journal credits, give up. |
| 1850 | */ |
| 1851 | if (ext4_journal_extend(handle, 2)) |
| 1852 | return; |
| 1853 | |
| 1854 | /* |
| 1855 | * Copy the extent data up to the inode |
| 1856 | */ |
| 1857 | blk = ext4_idx_pblock(path[0].p_idx); |
| 1858 | s = le16_to_cpu(path[1].p_hdr->eh_entries) * |
| 1859 | sizeof(struct ext4_extent_idx); |
| 1860 | s += sizeof(struct ext4_extent_header); |
| 1861 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 1862 | path[1].p_maxdepth = path[0].p_maxdepth; |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1863 | memcpy(path[0].p_hdr, path[1].p_hdr, s); |
| 1864 | path[0].p_depth = 0; |
| 1865 | path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + |
| 1866 | (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr)); |
| 1867 | path[0].p_hdr->eh_max = cpu_to_le16(max_root); |
| 1868 | |
| 1869 | brelse(path[1].p_bh); |
| 1870 | ext4_free_blocks(handle, inode, NULL, blk, 1, |
Theodore Ts'o | 71d4f7d | 2014-07-15 06:02:38 -0400 | [diff] [blame] | 1871 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | /* |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1875 | * This function tries to merge the @ex extent to neighbours in the tree. |
| 1876 | * return 1 if merge left else 0. |
| 1877 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1878 | static void ext4_ext_try_to_merge(handle_t *handle, |
| 1879 | struct inode *inode, |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1880 | struct ext4_ext_path *path, |
| 1881 | struct ext4_extent *ex) { |
| 1882 | struct ext4_extent_header *eh; |
| 1883 | unsigned int depth; |
| 1884 | int merge_done = 0; |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1885 | |
| 1886 | depth = ext_depth(inode); |
| 1887 | BUG_ON(path[depth].p_hdr == NULL); |
| 1888 | eh = path[depth].p_hdr; |
| 1889 | |
| 1890 | if (ex > EXT_FIRST_EXTENT(eh)) |
| 1891 | merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); |
| 1892 | |
| 1893 | if (!merge_done) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1894 | (void) ext4_ext_try_to_merge_right(inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1895 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1896 | ext4_ext_try_to_merge_up(handle, inode, path); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | /* |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1900 | * check if a portion of the "newext" extent overlaps with an |
| 1901 | * existing extent. |
| 1902 | * |
| 1903 | * If there is an overlap discovered, it updates the length of the newext |
| 1904 | * such that there will be no overlap, and then returns 1. |
| 1905 | * If there is no overlap found, it returns 0. |
| 1906 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1907 | static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, |
| 1908 | struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1909 | struct ext4_extent *newext, |
| 1910 | struct ext4_ext_path *path) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1911 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1912 | ext4_lblk_t b1, b2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1913 | unsigned int depth, len1; |
| 1914 | unsigned int ret = 0; |
| 1915 | |
| 1916 | b1 = le32_to_cpu(newext->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1917 | len1 = ext4_ext_get_actual_len(newext); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1918 | depth = ext_depth(inode); |
| 1919 | if (!path[depth].p_ext) |
| 1920 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1921 | b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block)); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1922 | |
| 1923 | /* |
| 1924 | * get the next allocated block if the extent in the path |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1925 | * is before the requested block(s) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1926 | */ |
| 1927 | if (b2 < b1) { |
| 1928 | b2 = ext4_ext_next_allocated_block(path); |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1929 | if (b2 == EXT_MAX_BLOCKS) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1930 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1931 | b2 = EXT4_LBLK_CMASK(sbi, b2); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1932 | } |
| 1933 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1934 | /* check for wrap through zero on extent logical start block*/ |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1935 | if (b1 + len1 < b1) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1936 | len1 = EXT_MAX_BLOCKS - b1; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1937 | newext->ee_len = cpu_to_le16(len1); |
| 1938 | ret = 1; |
| 1939 | } |
| 1940 | |
| 1941 | /* check for overlap */ |
| 1942 | if (b1 + len1 > b2) { |
| 1943 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1944 | ret = 1; |
| 1945 | } |
| 1946 | out: |
| 1947 | return ret; |
| 1948 | } |
| 1949 | |
| 1950 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1951 | * ext4_ext_insert_extent: |
| 1952 | * tries to merge requsted extent into the existing extent or |
| 1953 | * inserts requested extent as new one into the tree, |
| 1954 | * creating new leaf in the no-space case. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1955 | */ |
| 1956 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1957 | struct ext4_ext_path **ppath, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1958 | struct ext4_extent *newext, int gb_flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1959 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1960 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1961 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1962 | struct ext4_extent *ex, *fex; |
| 1963 | struct ext4_extent *nearex; /* nearest extent */ |
| 1964 | struct ext4_ext_path *npath = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1965 | int depth, len, err; |
| 1966 | ext4_lblk_t next; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1967 | int mb_flags = 0, unwritten; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1968 | |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 1969 | if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 1970 | mb_flags |= EXT4_MB_DELALLOC_RESERVED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1971 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
| 1972 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1973 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1974 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1975 | depth = ext_depth(inode); |
| 1976 | ex = path[depth].p_ext; |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1977 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1978 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1979 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1980 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1981 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1982 | |
| 1983 | /* try to insert block into found extent and return */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1984 | if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) { |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1985 | |
| 1986 | /* |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1987 | * Try to see whether we should rather test the extent on |
| 1988 | * right from ex, or from the left of ex. This is because |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1989 | * ext4_find_extent() can return either extent on the |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1990 | * left, or on the right from the searched position. This |
| 1991 | * will make merging more effective. |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1992 | */ |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1993 | if (ex < EXT_LAST_EXTENT(eh) && |
| 1994 | (le32_to_cpu(ex->ee_block) + |
| 1995 | ext4_ext_get_actual_len(ex) < |
| 1996 | le32_to_cpu(newext->ee_block))) { |
| 1997 | ex += 1; |
| 1998 | goto prepend; |
| 1999 | } else if ((ex > EXT_FIRST_EXTENT(eh)) && |
| 2000 | (le32_to_cpu(newext->ee_block) + |
| 2001 | ext4_ext_get_actual_len(newext) < |
| 2002 | le32_to_cpu(ex->ee_block))) |
| 2003 | ex -= 1; |
| 2004 | |
| 2005 | /* Try to append newex to the ex */ |
| 2006 | if (ext4_can_extents_be_merged(inode, ex, newext)) { |
| 2007 | ext_debug("append [%d]%d block to %u:[%d]%d" |
| 2008 | "(from %llu)\n", |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2009 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2010 | ext4_ext_get_actual_len(newext), |
| 2011 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2012 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2013 | ext4_ext_get_actual_len(ex), |
| 2014 | ext4_ext_pblock(ex)); |
| 2015 | err = ext4_ext_get_access(handle, inode, |
| 2016 | path + depth); |
| 2017 | if (err) |
| 2018 | return err; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2019 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2020 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2021 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2022 | if (unwritten) |
| 2023 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2024 | eh = path[depth].p_hdr; |
| 2025 | nearex = ex; |
| 2026 | goto merge; |
| 2027 | } |
| 2028 | |
| 2029 | prepend: |
| 2030 | /* Try to prepend newex to the ex */ |
| 2031 | if (ext4_can_extents_be_merged(inode, newext, ex)) { |
| 2032 | ext_debug("prepend %u[%d]%d block to %u:[%d]%d" |
| 2033 | "(from %llu)\n", |
| 2034 | le32_to_cpu(newext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2035 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2036 | ext4_ext_get_actual_len(newext), |
| 2037 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2038 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2039 | ext4_ext_get_actual_len(ex), |
| 2040 | ext4_ext_pblock(ex)); |
| 2041 | err = ext4_ext_get_access(handle, inode, |
| 2042 | path + depth); |
| 2043 | if (err) |
| 2044 | return err; |
| 2045 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2046 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2047 | ex->ee_block = newext->ee_block; |
| 2048 | ext4_ext_store_pblock(ex, ext4_ext_pblock(newext)); |
| 2049 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 2050 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2051 | if (unwritten) |
| 2052 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2053 | eh = path[depth].p_hdr; |
| 2054 | nearex = ex; |
| 2055 | goto merge; |
| 2056 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2057 | } |
| 2058 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2059 | depth = ext_depth(inode); |
| 2060 | eh = path[depth].p_hdr; |
| 2061 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 2062 | goto has_space; |
| 2063 | |
| 2064 | /* probably next leaf has space for us? */ |
| 2065 | fex = EXT_LAST_EXTENT(eh); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2066 | next = EXT_MAX_BLOCKS; |
| 2067 | 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] | 2068 | next = ext4_ext_next_leaf_block(path); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2069 | if (next != EXT_MAX_BLOCKS) { |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2070 | ext_debug("next leaf block - %u\n", next); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2071 | BUG_ON(npath != NULL); |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 2072 | npath = ext4_find_extent(inode, next, NULL, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2073 | if (IS_ERR(npath)) |
| 2074 | return PTR_ERR(npath); |
| 2075 | BUG_ON(npath->p_depth != path->p_depth); |
| 2076 | eh = npath[depth].p_hdr; |
| 2077 | 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] | 2078 | ext_debug("next leaf isn't full(%d)\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2079 | le16_to_cpu(eh->eh_entries)); |
| 2080 | path = npath; |
Robin Dong | ffb505f | 2011-07-11 11:43:59 -0400 | [diff] [blame] | 2081 | goto has_space; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2082 | } |
| 2083 | ext_debug("next leaf has no free space(%d,%d)\n", |
| 2084 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 2085 | } |
| 2086 | |
| 2087 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2088 | * There is no free space in the found leaf. |
| 2089 | * We're gonna add a new leaf in the tree. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2090 | */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2091 | if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 2092 | mb_flags |= EXT4_MB_USE_RESERVED; |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2093 | err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 2094 | ppath, newext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2095 | if (err) |
| 2096 | goto cleanup; |
| 2097 | depth = ext_depth(inode); |
| 2098 | eh = path[depth].p_hdr; |
| 2099 | |
| 2100 | has_space: |
| 2101 | nearex = path[depth].p_ext; |
| 2102 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2103 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2104 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2105 | goto cleanup; |
| 2106 | |
| 2107 | if (!nearex) { |
| 2108 | /* there is no extent in this leaf, create first one */ |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2109 | ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2110 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2111 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2112 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2113 | ext4_ext_get_actual_len(newext)); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2114 | nearex = EXT_FIRST_EXTENT(eh); |
| 2115 | } else { |
| 2116 | if (le32_to_cpu(newext->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2117 | > le32_to_cpu(nearex->ee_block)) { |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2118 | /* Insert after */ |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2119 | ext_debug("insert %u:%llu:[%d]%d before: " |
| 2120 | "nearest %p\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2121 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2122 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2123 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2124 | ext4_ext_get_actual_len(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2125 | nearex); |
| 2126 | nearex++; |
| 2127 | } else { |
| 2128 | /* Insert before */ |
| 2129 | BUG_ON(newext->ee_block == nearex->ee_block); |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2130 | ext_debug("insert %u:%llu:[%d]%d after: " |
| 2131 | "nearest %p\n", |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2132 | le32_to_cpu(newext->ee_block), |
| 2133 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2134 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2135 | ext4_ext_get_actual_len(newext), |
| 2136 | nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2137 | } |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2138 | len = EXT_LAST_EXTENT(eh) - nearex + 1; |
| 2139 | if (len > 0) { |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2140 | ext_debug("insert %u:%llu:[%d]%d: " |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2141 | "move %d extents from 0x%p to 0x%p\n", |
| 2142 | le32_to_cpu(newext->ee_block), |
| 2143 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2144 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2145 | ext4_ext_get_actual_len(newext), |
| 2146 | len, nearex, nearex + 1); |
| 2147 | memmove(nearex + 1, nearex, |
| 2148 | len * sizeof(struct ext4_extent)); |
| 2149 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2152 | le16_add_cpu(&eh->eh_entries, 1); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2153 | path[depth].p_ext = nearex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2154 | nearex->ee_block = newext->ee_block; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2155 | ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2156 | nearex->ee_len = newext->ee_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2157 | |
| 2158 | merge: |
HaiboLiu | e7bcf82 | 2012-07-09 16:29:28 -0400 | [diff] [blame] | 2159 | /* try to merge extents */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2160 | if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2161 | ext4_ext_try_to_merge(handle, inode, path, nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2162 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2163 | |
| 2164 | /* time to correct all indexes above */ |
| 2165 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2166 | if (err) |
| 2167 | goto cleanup; |
| 2168 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2169 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2170 | |
| 2171 | cleanup: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 2172 | ext4_ext_drop_refs(npath); |
| 2173 | kfree(npath); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2174 | return err; |
| 2175 | } |
| 2176 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2177 | static int ext4_fill_fiemap_extents(struct inode *inode, |
| 2178 | ext4_lblk_t block, ext4_lblk_t num, |
| 2179 | struct fiemap_extent_info *fieinfo) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2180 | { |
| 2181 | struct ext4_ext_path *path = NULL; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2182 | struct ext4_extent *ex; |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2183 | struct extent_status es; |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2184 | ext4_lblk_t next, next_del, start = 0, end = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2185 | ext4_lblk_t last = block + num; |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2186 | int exists, depth = 0, err = 0; |
| 2187 | unsigned int flags = 0; |
| 2188 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2189 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2190 | while (block < last && block != EXT_MAX_BLOCKS) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2191 | num = last - block; |
| 2192 | /* find extent for this block */ |
Theodore Ts'o | fab3a54 | 2009-12-09 21:30:02 -0500 | [diff] [blame] | 2193 | down_read(&EXT4_I(inode)->i_data_sem); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2194 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 2195 | path = ext4_find_extent(inode, block, &path, 0); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2196 | if (IS_ERR(path)) { |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2197 | up_read(&EXT4_I(inode)->i_data_sem); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2198 | err = PTR_ERR(path); |
| 2199 | path = NULL; |
| 2200 | break; |
| 2201 | } |
| 2202 | |
| 2203 | depth = ext_depth(inode); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2204 | if (unlikely(path[depth].p_hdr == NULL)) { |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2205 | up_read(&EXT4_I(inode)->i_data_sem); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2206 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2207 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2208 | break; |
| 2209 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2210 | ex = path[depth].p_ext; |
| 2211 | next = ext4_ext_next_allocated_block(path); |
| 2212 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2213 | flags = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2214 | exists = 0; |
| 2215 | if (!ex) { |
| 2216 | /* there is no extent yet, so try to allocate |
| 2217 | * all requested space */ |
| 2218 | start = block; |
| 2219 | end = block + num; |
| 2220 | } else if (le32_to_cpu(ex->ee_block) > block) { |
| 2221 | /* need to allocate space before found extent */ |
| 2222 | start = block; |
| 2223 | end = le32_to_cpu(ex->ee_block); |
| 2224 | if (block + num < end) |
| 2225 | end = block + num; |
| 2226 | } else if (block >= le32_to_cpu(ex->ee_block) |
| 2227 | + ext4_ext_get_actual_len(ex)) { |
| 2228 | /* need to allocate space after found extent */ |
| 2229 | start = block; |
| 2230 | end = block + num; |
| 2231 | if (end >= next) |
| 2232 | end = next; |
| 2233 | } else if (block >= le32_to_cpu(ex->ee_block)) { |
| 2234 | /* |
| 2235 | * some part of requested space is covered |
| 2236 | * by found extent |
| 2237 | */ |
| 2238 | start = block; |
| 2239 | end = le32_to_cpu(ex->ee_block) |
| 2240 | + ext4_ext_get_actual_len(ex); |
| 2241 | if (block + num < end) |
| 2242 | end = block + num; |
| 2243 | exists = 1; |
| 2244 | } else { |
| 2245 | BUG(); |
| 2246 | } |
| 2247 | BUG_ON(end <= start); |
| 2248 | |
| 2249 | if (!exists) { |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2250 | es.es_lblk = start; |
| 2251 | es.es_len = end - start; |
| 2252 | es.es_pblk = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2253 | } else { |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2254 | es.es_lblk = le32_to_cpu(ex->ee_block); |
| 2255 | es.es_len = ext4_ext_get_actual_len(ex); |
| 2256 | es.es_pblk = ext4_ext_pblock(ex); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2257 | if (ext4_ext_is_unwritten(ex)) |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2258 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2259 | } |
| 2260 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2261 | /* |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2262 | * Find delayed extent and update es accordingly. We call |
| 2263 | * it even in !exists case to find out whether es is the |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2264 | * last existing extent or not. |
| 2265 | */ |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2266 | next_del = ext4_find_delayed_extent(inode, &es); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2267 | if (!exists && next_del) { |
| 2268 | exists = 1; |
Jie Liu | 72dac95 | 2013-06-12 23:13:59 -0400 | [diff] [blame] | 2269 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 2270 | FIEMAP_EXTENT_UNKNOWN); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2271 | } |
| 2272 | up_read(&EXT4_I(inode)->i_data_sem); |
| 2273 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2274 | if (unlikely(es.es_len == 0)) { |
| 2275 | EXT4_ERROR_INODE(inode, "es.es_len == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2276 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2277 | break; |
| 2278 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2279 | |
Zheng Liu | f7fec03 | 2013-02-18 00:28:47 -0500 | [diff] [blame] | 2280 | /* |
| 2281 | * This is possible iff next == next_del == EXT_MAX_BLOCKS. |
| 2282 | * we need to check next == EXT_MAX_BLOCKS because it is |
| 2283 | * possible that an extent is with unwritten and delayed |
| 2284 | * status due to when an extent is delayed allocated and |
| 2285 | * is allocated by fallocate status tree will track both of |
| 2286 | * them in a extent. |
| 2287 | * |
| 2288 | * So we could return a unwritten and delayed extent, and |
| 2289 | * its block is equal to 'next'. |
| 2290 | */ |
| 2291 | if (next == next_del && next == EXT_MAX_BLOCKS) { |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2292 | flags |= FIEMAP_EXTENT_LAST; |
| 2293 | if (unlikely(next_del != EXT_MAX_BLOCKS || |
| 2294 | next != EXT_MAX_BLOCKS)) { |
| 2295 | EXT4_ERROR_INODE(inode, |
| 2296 | "next extent == %u, next " |
| 2297 | "delalloc extent = %u", |
| 2298 | next, next_del); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2299 | err = -EFSCORRUPTED; |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2300 | break; |
| 2301 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2302 | } |
| 2303 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2304 | if (exists) { |
| 2305 | err = fiemap_fill_next_extent(fieinfo, |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2306 | (__u64)es.es_lblk << blksize_bits, |
| 2307 | (__u64)es.es_pblk << blksize_bits, |
| 2308 | (__u64)es.es_len << blksize_bits, |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2309 | flags); |
| 2310 | if (err < 0) |
| 2311 | break; |
| 2312 | if (err == 1) { |
| 2313 | err = 0; |
| 2314 | break; |
| 2315 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2316 | } |
| 2317 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2318 | block = es.es_lblk + es.es_len; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2319 | } |
| 2320 | |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 2321 | ext4_ext_drop_refs(path); |
| 2322 | kfree(path); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2323 | return err; |
| 2324 | } |
| 2325 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2326 | /* |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2327 | * ext4_ext_determine_hole - determine hole around given block |
| 2328 | * @inode: inode we lookup in |
| 2329 | * @path: path in extent tree to @lblk |
| 2330 | * @lblk: pointer to logical block around which we want to determine hole |
| 2331 | * |
| 2332 | * Determine hole length (and start if easily possible) around given logical |
| 2333 | * block. We don't try too hard to find the beginning of the hole but @path |
| 2334 | * actually points to extent before @lblk, we provide it. |
| 2335 | * |
| 2336 | * The function returns the length of a hole starting at @lblk. We update @lblk |
| 2337 | * to the beginning of the hole if we managed to find it. |
| 2338 | */ |
| 2339 | static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode, |
| 2340 | struct ext4_ext_path *path, |
| 2341 | ext4_lblk_t *lblk) |
| 2342 | { |
| 2343 | int depth = ext_depth(inode); |
| 2344 | struct ext4_extent *ex; |
| 2345 | ext4_lblk_t len; |
| 2346 | |
| 2347 | ex = path[depth].p_ext; |
| 2348 | if (ex == NULL) { |
| 2349 | /* there is no extent yet, so gap is [0;-] */ |
| 2350 | *lblk = 0; |
| 2351 | len = EXT_MAX_BLOCKS; |
| 2352 | } else if (*lblk < le32_to_cpu(ex->ee_block)) { |
| 2353 | len = le32_to_cpu(ex->ee_block) - *lblk; |
| 2354 | } else if (*lblk >= le32_to_cpu(ex->ee_block) |
| 2355 | + ext4_ext_get_actual_len(ex)) { |
| 2356 | ext4_lblk_t next; |
| 2357 | |
| 2358 | *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); |
| 2359 | next = ext4_ext_next_allocated_block(path); |
| 2360 | BUG_ON(next == *lblk); |
| 2361 | len = next - *lblk; |
| 2362 | } else { |
| 2363 | BUG(); |
| 2364 | } |
| 2365 | return len; |
| 2366 | } |
| 2367 | |
| 2368 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2369 | * ext4_ext_put_gap_in_cache: |
| 2370 | * calculate boundaries of the gap that the requested block fits into |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2371 | * and cache this gap |
| 2372 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2373 | static void |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2374 | ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start, |
| 2375 | ext4_lblk_t hole_len) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2376 | { |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2377 | struct extent_status es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2378 | |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2379 | ext4_es_find_delayed_extent_range(inode, hole_start, |
| 2380 | hole_start + hole_len - 1, &es); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2381 | if (es.es_len) { |
| 2382 | /* There's delayed extent containing lblock? */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2383 | if (es.es_lblk <= hole_start) |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2384 | return; |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2385 | hole_len = min(es.es_lblk - hole_start, hole_len); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2386 | } |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2387 | ext_debug(" -> %u:%u\n", hole_start, hole_len); |
| 2388 | ext4_es_insert_extent(inode, hole_start, hole_len, ~0, |
| 2389 | EXTENT_STATUS_HOLE); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2393 | * ext4_ext_rm_idx: |
| 2394 | * removes index from the index block. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2395 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2396 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2397 | struct ext4_ext_path *path, int depth) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2398 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2399 | int err; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2400 | ext4_fsblk_t leaf; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2401 | |
| 2402 | /* free index block */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2403 | depth--; |
| 2404 | path = path + depth; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2405 | leaf = ext4_idx_pblock(path->p_idx); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2406 | if (unlikely(path->p_hdr->eh_entries == 0)) { |
| 2407 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2408 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2409 | } |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2410 | err = ext4_ext_get_access(handle, inode, path); |
| 2411 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2412 | return err; |
Robin Dong | 0e1147b | 2011-07-27 21:29:33 -0400 | [diff] [blame] | 2413 | |
| 2414 | if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { |
| 2415 | int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; |
| 2416 | len *= sizeof(struct ext4_extent_idx); |
| 2417 | memmove(path->p_idx, path->p_idx + 1, len); |
| 2418 | } |
| 2419 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2420 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2421 | err = ext4_ext_dirty(handle, inode, path); |
| 2422 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2423 | return err; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2424 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2425 | trace_ext4_ext_rm_idx(inode, leaf); |
| 2426 | |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 2427 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2428 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2429 | |
| 2430 | while (--depth >= 0) { |
| 2431 | if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr)) |
| 2432 | break; |
| 2433 | path--; |
| 2434 | err = ext4_ext_get_access(handle, inode, path); |
| 2435 | if (err) |
| 2436 | break; |
| 2437 | path->p_idx->ei_block = (path+1)->p_idx->ei_block; |
| 2438 | err = ext4_ext_dirty(handle, inode, path); |
| 2439 | if (err) |
| 2440 | break; |
| 2441 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2442 | return err; |
| 2443 | } |
| 2444 | |
| 2445 | /* |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2446 | * ext4_ext_calc_credits_for_single_extent: |
| 2447 | * This routine returns max. credits that needed to insert an extent |
| 2448 | * to the extent tree. |
| 2449 | * When pass the actual path, the caller should calculate credits |
| 2450 | * under i_data_sem. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2451 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2452 | 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] | 2453 | struct ext4_ext_path *path) |
| 2454 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2455 | if (path) { |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2456 | int depth = ext_depth(inode); |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 2457 | int ret = 0; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2458 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2459 | /* probably there is space in leaf? */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2460 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2461 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
| 2462 | |
| 2463 | /* |
| 2464 | * There are some space in the leaf tree, no |
| 2465 | * need to account for leaf block credit |
| 2466 | * |
| 2467 | * bitmaps and block group descriptor blocks |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 2468 | * and other metadata blocks still need to be |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2469 | * accounted. |
| 2470 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2471 | /* 1 bitmap, 1 block group descriptor */ |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2472 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Aneesh Kumar K.V | 5887e98 | 2009-07-05 23:12:04 -0400 | [diff] [blame] | 2473 | return ret; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2474 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2477 | return ext4_chunk_trans_blocks(inode, nrblocks); |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2478 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2479 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2480 | /* |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2481 | * How many index/leaf blocks need to change/allocate to add @extents extents? |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2482 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2483 | * If we add a single extent, then in the worse case, each tree level |
| 2484 | * index/leaf need to be changed in case of the tree split. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2485 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2486 | * If more extents are inserted, they could cause the whole tree split more |
| 2487 | * than once, but this is really rare. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2488 | */ |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2489 | int ext4_ext_index_trans_blocks(struct inode *inode, int extents) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2490 | { |
| 2491 | int index; |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 2492 | int depth; |
| 2493 | |
| 2494 | /* If we are converting the inline data, only one is needed here. */ |
| 2495 | if (ext4_has_inline_data(inode)) |
| 2496 | return 1; |
| 2497 | |
| 2498 | depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2499 | |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2500 | if (extents <= 1) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2501 | index = depth * 2; |
| 2502 | else |
| 2503 | index = depth * 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2504 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2505 | return index; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2508 | static inline int get_default_free_blocks_flags(struct inode *inode) |
| 2509 | { |
| 2510 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
| 2511 | return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET; |
| 2512 | else if (ext4_should_journal_data(inode)) |
| 2513 | return EXT4_FREE_BLOCKS_FORGET; |
| 2514 | return 0; |
| 2515 | } |
| 2516 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2517 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2518 | struct ext4_extent *ex, |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2519 | long long *partial_cluster, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2520 | ext4_lblk_t from, ext4_lblk_t to) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2521 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2522 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2523 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2524 | ext4_fsblk_t pblk; |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2525 | int flags = get_default_free_blocks_flags(inode); |
Andrey Sidorov | 18888cf | 2012-09-19 14:14:53 -0400 | [diff] [blame] | 2526 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2527 | /* |
| 2528 | * For bigalloc file systems, we never free a partial cluster |
| 2529 | * at the beginning of the extent. Instead, we make a note |
| 2530 | * that we tried freeing the cluster, and check to see if we |
| 2531 | * need to free it on a subsequent call to ext4_remove_blocks, |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2532 | * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2533 | */ |
| 2534 | flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; |
| 2535 | |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2536 | trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2537 | /* |
| 2538 | * If we have a partial cluster, and it's different from the |
| 2539 | * cluster of the last block, we need to explicitly free the |
| 2540 | * partial cluster here. |
| 2541 | */ |
| 2542 | pblk = ext4_ext_pblock(ex) + ee_len - 1; |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2543 | if (*partial_cluster > 0 && |
| 2544 | *partial_cluster != (long long) EXT4_B2C(sbi, pblk)) { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2545 | ext4_free_blocks(handle, inode, NULL, |
| 2546 | EXT4_C2B(sbi, *partial_cluster), |
| 2547 | sbi->s_cluster_ratio, flags); |
| 2548 | *partial_cluster = 0; |
| 2549 | } |
| 2550 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2551 | #ifdef EXTENTS_STATS |
| 2552 | { |
| 2553 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2554 | spin_lock(&sbi->s_ext_stats_lock); |
| 2555 | sbi->s_ext_blocks += ee_len; |
| 2556 | sbi->s_ext_extents++; |
| 2557 | if (ee_len < sbi->s_ext_min) |
| 2558 | sbi->s_ext_min = ee_len; |
| 2559 | if (ee_len > sbi->s_ext_max) |
| 2560 | sbi->s_ext_max = ee_len; |
| 2561 | if (ext_depth(inode) > sbi->s_depth_max) |
| 2562 | sbi->s_depth_max = ext_depth(inode); |
| 2563 | spin_unlock(&sbi->s_ext_stats_lock); |
| 2564 | } |
| 2565 | #endif |
| 2566 | if (from >= le32_to_cpu(ex->ee_block) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2567 | && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2568 | /* tail removal */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2569 | ext4_lblk_t num; |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2570 | long long first_cluster; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2571 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2572 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2573 | pblk = ext4_ext_pblock(ex) + ee_len - num; |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2574 | /* |
| 2575 | * Usually we want to free partial cluster at the end of the |
| 2576 | * extent, except for the situation when the cluster is still |
| 2577 | * used by any other extent (partial_cluster is negative). |
| 2578 | */ |
| 2579 | if (*partial_cluster < 0 && |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2580 | *partial_cluster == -(long long) EXT4_B2C(sbi, pblk+num-1)) |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2581 | flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; |
| 2582 | |
| 2583 | ext_debug("free last %u blocks starting %llu partial %lld\n", |
| 2584 | num, pblk, *partial_cluster); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2585 | ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
| 2586 | /* |
| 2587 | * If the block range to be freed didn't start at the |
| 2588 | * beginning of a cluster, and we removed the entire |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2589 | * extent and the cluster is not used by any other extent, |
| 2590 | * save the partial cluster here, since we might need to |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2591 | * delete if we determine that the truncate or punch hole |
| 2592 | * operation has removed all of the blocks in the cluster. |
| 2593 | * If that cluster is used by another extent, preserve its |
| 2594 | * negative value so it isn't freed later on. |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2595 | * |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2596 | * If the whole extent wasn't freed, we've reached the |
| 2597 | * start of the truncated/punched region and have finished |
| 2598 | * removing blocks. If there's a partial cluster here it's |
| 2599 | * shared with the remainder of the extent and is no longer |
| 2600 | * a candidate for removal. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2601 | */ |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2602 | if (EXT4_PBLK_COFF(sbi, pblk) && ee_len == num) { |
| 2603 | first_cluster = (long long) EXT4_B2C(sbi, pblk); |
| 2604 | if (first_cluster != -*partial_cluster) |
| 2605 | *partial_cluster = first_cluster; |
| 2606 | } else { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2607 | *partial_cluster = 0; |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2608 | } |
Lukas Czerner | 78fb9cd | 2013-05-27 23:32:35 -0400 | [diff] [blame] | 2609 | } else |
| 2610 | ext4_error(sbi->s_sb, "strange request: removal(2) " |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 2611 | "%u-%u from %u:%u", |
Lukas Czerner | 78fb9cd | 2013-05-27 23:32:35 -0400 | [diff] [blame] | 2612 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2613 | return 0; |
| 2614 | } |
| 2615 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2616 | |
| 2617 | /* |
| 2618 | * ext4_ext_rm_leaf() Removes the extents associated with the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2619 | * blocks appearing between "start" and "end". Both "start" |
| 2620 | * and "end" must appear in the same extent or EIO is returned. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2621 | * |
| 2622 | * @handle: The journal handle |
| 2623 | * @inode: The files inode |
| 2624 | * @path: The path to the leaf |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2625 | * @partial_cluster: The cluster which we'll have to free if all extents |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2626 | * has been released from it. However, if this value is |
| 2627 | * negative, it's a cluster just to the right of the |
| 2628 | * punched region and it must not be freed. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2629 | * @start: The first block to remove |
| 2630 | * @end: The last block to remove |
| 2631 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2632 | static int |
| 2633 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2634 | struct ext4_ext_path *path, |
| 2635 | long long *partial_cluster, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2636 | ext4_lblk_t start, ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2637 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2638 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2639 | int err = 0, correct_index = 0; |
| 2640 | int depth = ext_depth(inode), credits; |
| 2641 | struct ext4_extent_header *eh; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2642 | ext4_lblk_t a, b; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2643 | unsigned num; |
| 2644 | ext4_lblk_t ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2645 | unsigned short ex_ee_len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2646 | unsigned unwritten = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2647 | struct ext4_extent *ex; |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2648 | ext4_fsblk_t pblk; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2649 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2650 | /* the header must be checked already in ext4_ext_remove_space() */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2651 | ext_debug("truncate since %u in leaf to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2652 | if (!path[depth].p_hdr) |
| 2653 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 2654 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2655 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 2656 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2657 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2658 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2659 | /* find where to start removing */ |
Ashish Sangwan | 6ae06ff | 2013-07-01 08:12:41 -0400 | [diff] [blame] | 2660 | ex = path[depth].p_ext; |
| 2661 | if (!ex) |
| 2662 | ex = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2663 | |
| 2664 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2665 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2666 | |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2667 | trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster); |
| 2668 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2669 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 2670 | ex_ee_block + ex_ee_len > start) { |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2671 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2672 | if (ext4_ext_is_unwritten(ex)) |
| 2673 | unwritten = 1; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2674 | else |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2675 | unwritten = 0; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2676 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 2677 | ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2678 | unwritten, ex_ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2679 | path[depth].p_ext = ex; |
| 2680 | |
| 2681 | a = ex_ee_block > start ? ex_ee_block : start; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2682 | b = ex_ee_block+ex_ee_len - 1 < end ? |
| 2683 | ex_ee_block+ex_ee_len - 1 : end; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2684 | |
| 2685 | ext_debug(" border %u:%u\n", a, b); |
| 2686 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2687 | /* If this extent is beyond the end of the hole, skip it */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2688 | if (end < ex_ee_block) { |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2689 | /* |
| 2690 | * We're going to skip this extent and move to another, |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2691 | * so note that its first cluster is in use to avoid |
| 2692 | * freeing it when removing blocks. Eventually, the |
| 2693 | * right edge of the truncated/punched region will |
| 2694 | * be just to the left. |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2695 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2696 | if (sbi->s_cluster_ratio > 1) { |
| 2697 | pblk = ext4_ext_pblock(ex); |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2698 | *partial_cluster = |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2699 | -(long long) EXT4_B2C(sbi, pblk); |
| 2700 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2701 | ex--; |
| 2702 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2703 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2704 | continue; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2705 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
Lukas Czerner | dc1841d | 2012-03-19 23:07:43 -0400 | [diff] [blame] | 2706 | EXT4_ERROR_INODE(inode, |
| 2707 | "can not handle truncate %u:%u " |
| 2708 | "on extent %u:%u", |
| 2709 | start, end, ex_ee_block, |
| 2710 | ex_ee_block + ex_ee_len - 1); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2711 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2712 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2713 | } else if (a != ex_ee_block) { |
| 2714 | /* remove tail of the extent */ |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2715 | num = a - ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2716 | } else { |
| 2717 | /* remove whole extent: excellent! */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2718 | num = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2719 | } |
Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 2720 | /* |
| 2721 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 2722 | * descriptor) for each block group; assume two block |
| 2723 | * groups plus ex_ee_len/blocks_per_block_group for |
| 2724 | * the worst case |
| 2725 | */ |
| 2726 | 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] | 2727 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 2728 | correct_index = 1; |
| 2729 | credits += (ext_depth(inode)) + 1; |
| 2730 | } |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2731 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2732 | |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 2733 | err = ext4_ext_truncate_extend_restart(handle, inode, credits); |
Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2734 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2735 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2736 | |
| 2737 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2738 | if (err) |
| 2739 | goto out; |
| 2740 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2741 | err = ext4_remove_blocks(handle, inode, ex, partial_cluster, |
| 2742 | a, b); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2743 | if (err) |
| 2744 | goto out; |
| 2745 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2746 | if (num == 0) |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2747 | /* this extent is removed; mark slot entirely unused */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2748 | ext4_ext_store_pblock(ex, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2749 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2750 | ex->ee_len = cpu_to_le16(num); |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2751 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2752 | * Do not mark unwritten if all the blocks in the |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2753 | * extent have been removed. |
| 2754 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2755 | if (unwritten && num) |
| 2756 | ext4_ext_mark_unwritten(ex); |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2757 | /* |
| 2758 | * If the extent was completely released, |
| 2759 | * we need to remove it from the leaf |
| 2760 | */ |
| 2761 | if (num == 0) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2762 | if (end != EXT_MAX_BLOCKS - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2763 | /* |
| 2764 | * For hole punching, we need to scoot all the |
| 2765 | * extents up when an extent is removed so that |
| 2766 | * we dont have blank extents in the middle |
| 2767 | */ |
| 2768 | memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * |
| 2769 | sizeof(struct ext4_extent)); |
| 2770 | |
| 2771 | /* Now get rid of the one at the end */ |
| 2772 | memset(EXT_LAST_EXTENT(eh), 0, |
| 2773 | sizeof(struct ext4_extent)); |
| 2774 | } |
| 2775 | le16_add_cpu(&eh->eh_entries, -1); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2776 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2777 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2778 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2779 | if (err) |
| 2780 | goto out; |
| 2781 | |
Yongqiang Yang | bf52c6f | 2011-11-01 18:59:26 -0400 | [diff] [blame] | 2782 | 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] | 2783 | ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2784 | ex--; |
| 2785 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2786 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | if (correct_index && eh->eh_entries) |
| 2790 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2791 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2792 | /* |
Eric Whitney | ad6599a | 2014-04-01 19:49:30 -0400 | [diff] [blame] | 2793 | * If there's a partial cluster and at least one extent remains in |
| 2794 | * the leaf, free the partial cluster if it isn't shared with the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2795 | * current extent. If it is shared with the current extent |
| 2796 | * we zero partial_cluster because we've reached the start of the |
| 2797 | * truncated/punched region and we're done removing blocks. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2798 | */ |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2799 | if (*partial_cluster > 0 && ex >= EXT_FIRST_EXTENT(eh)) { |
| 2800 | pblk = ext4_ext_pblock(ex) + ex_ee_len - 1; |
| 2801 | if (*partial_cluster != (long long) EXT4_B2C(sbi, pblk)) { |
| 2802 | ext4_free_blocks(handle, inode, NULL, |
| 2803 | EXT4_C2B(sbi, *partial_cluster), |
| 2804 | sbi->s_cluster_ratio, |
| 2805 | get_default_free_blocks_flags(inode)); |
| 2806 | } |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2807 | *partial_cluster = 0; |
| 2808 | } |
| 2809 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2810 | /* if this leaf is free, then we should |
| 2811 | * remove it from index block above */ |
| 2812 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2813 | err = ext4_ext_rm_idx(handle, inode, path, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2814 | |
| 2815 | out: |
| 2816 | return err; |
| 2817 | } |
| 2818 | |
| 2819 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2820 | * ext4_ext_more_to_rm: |
| 2821 | * returns 1 if current index has to be freed (even partial) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2822 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2823 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2824 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 2825 | { |
| 2826 | BUG_ON(path->p_idx == NULL); |
| 2827 | |
| 2828 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 2829 | return 0; |
| 2830 | |
| 2831 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2832 | * if truncate on deeper level happened, it wasn't partial, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2833 | * so we have to consider current index for truncation |
| 2834 | */ |
| 2835 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 2836 | return 0; |
| 2837 | return 1; |
| 2838 | } |
| 2839 | |
Theodore Ts'o | 26a4c0c | 2013-04-03 12:45:17 -0400 | [diff] [blame] | 2840 | int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, |
| 2841 | ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2842 | { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2843 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2844 | int depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2845 | struct ext4_ext_path *path = NULL; |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2846 | long long partial_cluster = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2847 | handle_t *handle; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2848 | int i = 0, err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2849 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2850 | ext_debug("truncate since %u to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2851 | |
| 2852 | /* probably first extent we're gonna free will be last in block */ |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 2853 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2854 | if (IS_ERR(handle)) |
| 2855 | return PTR_ERR(handle); |
| 2856 | |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2857 | again: |
Lukas Czerner | 6180132 | 2013-05-27 23:32:35 -0400 | [diff] [blame] | 2858 | trace_ext4_ext_remove_space(inode, start, end, depth); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2859 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2860 | /* |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2861 | * Check if we are removing extents inside the extent tree. If that |
| 2862 | * is the case, we are going to punch a hole inside the extent tree |
| 2863 | * so we have to check whether we need to split the extent covering |
| 2864 | * the last block to remove so we can easily remove the part of it |
| 2865 | * in ext4_ext_rm_leaf(). |
| 2866 | */ |
| 2867 | if (end < EXT_MAX_BLOCKS - 1) { |
| 2868 | struct ext4_extent *ex; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2869 | ext4_lblk_t ee_block, ex_end, lblk; |
| 2870 | ext4_fsblk_t pblk; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2871 | |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2872 | /* find extent for or closest extent to this block */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 2873 | path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2874 | if (IS_ERR(path)) { |
| 2875 | ext4_journal_stop(handle); |
| 2876 | return PTR_ERR(path); |
| 2877 | } |
| 2878 | depth = ext_depth(inode); |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2879 | /* Leaf not may not exist only if inode has no blocks at all */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2880 | ex = path[depth].p_ext; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2881 | if (!ex) { |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2882 | if (depth) { |
| 2883 | EXT4_ERROR_INODE(inode, |
| 2884 | "path[%d].p_hdr == NULL", |
| 2885 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2886 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2887 | } |
| 2888 | goto out; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2889 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2890 | |
| 2891 | ee_block = le32_to_cpu(ex->ee_block); |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2892 | ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2893 | |
| 2894 | /* |
| 2895 | * See if the last block is inside the extent, if so split |
| 2896 | * the extent at 'end' block so we can easily remove the |
| 2897 | * tail of the first part of the split extent in |
| 2898 | * ext4_ext_rm_leaf(). |
| 2899 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2900 | if (end >= ee_block && end < ex_end) { |
| 2901 | |
| 2902 | /* |
| 2903 | * If we're going to split the extent, note that |
| 2904 | * the cluster containing the block after 'end' is |
| 2905 | * in use to avoid freeing it when removing blocks. |
| 2906 | */ |
| 2907 | if (sbi->s_cluster_ratio > 1) { |
| 2908 | pblk = ext4_ext_pblock(ex) + end - ee_block + 2; |
| 2909 | partial_cluster = |
| 2910 | -(long long) EXT4_B2C(sbi, pblk); |
| 2911 | } |
| 2912 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2913 | /* |
| 2914 | * Split the extent in two so that 'end' is the last |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 2915 | * block in the first new extent. Also we should not |
| 2916 | * fail removing space due to ENOSPC so try to use |
| 2917 | * reserved block if that happens. |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2918 | */ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 2919 | err = ext4_force_split_extent_at(handle, inode, &path, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 2920 | end + 1, 1); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2921 | if (err < 0) |
| 2922 | goto out; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2923 | |
| 2924 | } else if (sbi->s_cluster_ratio > 1 && end >= ex_end) { |
| 2925 | /* |
| 2926 | * If there's an extent to the right its first cluster |
| 2927 | * contains the immediate right boundary of the |
| 2928 | * truncated/punched region. Set partial_cluster to |
| 2929 | * its negative value so it won't be freed if shared |
| 2930 | * with the current extent. The end < ee_block case |
| 2931 | * is handled in ext4_ext_rm_leaf(). |
| 2932 | */ |
| 2933 | lblk = ex_end + 1; |
| 2934 | err = ext4_ext_search_right(inode, path, &lblk, &pblk, |
| 2935 | &ex); |
| 2936 | if (err) |
| 2937 | goto out; |
| 2938 | if (pblk) |
| 2939 | partial_cluster = |
| 2940 | -(long long) EXT4_B2C(sbi, pblk); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2941 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2942 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2943 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2944 | * We start scanning from right side, freeing all the blocks |
| 2945 | * after i_size and walking into the tree depth-wise. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2946 | */ |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2947 | depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2948 | if (path) { |
| 2949 | int k = i = depth; |
| 2950 | while (--k > 0) |
| 2951 | path[k].p_block = |
| 2952 | le16_to_cpu(path[k].p_hdr->eh_entries)+1; |
| 2953 | } else { |
| 2954 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), |
| 2955 | GFP_NOFS); |
| 2956 | if (path == NULL) { |
| 2957 | ext4_journal_stop(handle); |
| 2958 | return -ENOMEM; |
| 2959 | } |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 2960 | path[0].p_maxdepth = path[0].p_depth = depth; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2961 | path[0].p_hdr = ext_inode_hdr(inode); |
Theodore Ts'o | 89a4e48 | 2012-08-17 08:54:52 -0400 | [diff] [blame] | 2962 | i = 0; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2963 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 2964 | if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2965 | err = -EFSCORRUPTED; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2966 | goto out; |
| 2967 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2968 | } |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2969 | err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2970 | |
| 2971 | while (i >= 0 && err == 0) { |
| 2972 | if (i == depth) { |
| 2973 | /* this is leaf block */ |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2974 | err = ext4_ext_rm_leaf(handle, inode, path, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2975 | &partial_cluster, start, |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2976 | end); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2977 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2978 | brelse(path[i].p_bh); |
| 2979 | path[i].p_bh = NULL; |
| 2980 | i--; |
| 2981 | continue; |
| 2982 | } |
| 2983 | |
| 2984 | /* this is index block */ |
| 2985 | if (!path[i].p_hdr) { |
| 2986 | ext_debug("initialize header\n"); |
| 2987 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2990 | if (!path[i].p_idx) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2991 | /* this level hasn't been touched yet */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2992 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 2993 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
| 2994 | ext_debug("init index ptr: hdr 0x%p, num %d\n", |
| 2995 | path[i].p_hdr, |
| 2996 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 2997 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2998 | /* we were already here, see at next index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2999 | path[i].p_idx--; |
| 3000 | } |
| 3001 | |
| 3002 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
| 3003 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 3004 | path[i].p_idx); |
| 3005 | if (ext4_ext_more_to_rm(path + i)) { |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 3006 | struct buffer_head *bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3007 | /* go to the next level */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 3008 | ext_debug("move to level %d (block %llu)\n", |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3009 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3010 | memset(path + i + 1, 0, sizeof(*path)); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 3011 | bh = read_extent_tree_block(inode, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 3012 | ext4_idx_pblock(path[i].p_idx), depth - i - 1, |
| 3013 | EXT4_EX_NOCACHE); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 3014 | if (IS_ERR(bh)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3015 | /* should we reset i_size? */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 3016 | err = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3017 | break; |
| 3018 | } |
Theodore Ts'o | 76828c8 | 2013-07-15 12:27:47 -0400 | [diff] [blame] | 3019 | /* Yield here to deal with large extent trees. |
| 3020 | * Should be a no-op if we did IO above. */ |
| 3021 | cond_resched(); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 3022 | if (WARN_ON(i + 1 > depth)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3023 | err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 3024 | break; |
| 3025 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 3026 | path[i + 1].p_bh = bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3027 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3028 | /* save actual number of indexes since this |
| 3029 | * number is changed at the next iteration */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3030 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 3031 | i++; |
| 3032 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3033 | /* we finished processing this index, go up */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3034 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3035 | /* index is empty, remove it; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3036 | * handle must be already prepared by the |
| 3037 | * truncatei_leaf() */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 3038 | err = ext4_ext_rm_idx(handle, inode, path, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3039 | } |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3040 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3041 | brelse(path[i].p_bh); |
| 3042 | path[i].p_bh = NULL; |
| 3043 | i--; |
| 3044 | ext_debug("return to level %d\n", i); |
| 3045 | } |
| 3046 | } |
| 3047 | |
Lukas Czerner | 6180132 | 2013-05-27 23:32:35 -0400 | [diff] [blame] | 3048 | trace_ext4_ext_remove_space_done(inode, start, end, depth, |
| 3049 | partial_cluster, path->p_hdr->eh_entries); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3050 | |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 3051 | /* |
| 3052 | * If we still have something in the partial cluster and we have removed |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3053 | * even the first extent, then we should free the blocks in the partial |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 3054 | * cluster as well. (This code will only run when there are no leaves |
| 3055 | * to the immediate left of the truncated/punched region.) |
| 3056 | */ |
| 3057 | if (partial_cluster > 0 && err == 0) { |
| 3058 | /* don't zero partial_cluster since it's not used afterwards */ |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3059 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 3060 | EXT4_C2B(sbi, partial_cluster), |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 3061 | sbi->s_cluster_ratio, |
| 3062 | get_default_free_blocks_flags(inode)); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3063 | } |
| 3064 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3065 | /* TODO: flexible tree reduction should be here */ |
| 3066 | if (path->p_hdr->eh_entries == 0) { |
| 3067 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3068 | * truncate to zero freed all the tree, |
| 3069 | * so we need to correct eh_depth |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3070 | */ |
| 3071 | err = ext4_ext_get_access(handle, inode, path); |
| 3072 | if (err == 0) { |
| 3073 | ext_inode_hdr(inode)->eh_depth = 0; |
| 3074 | ext_inode_hdr(inode)->eh_max = |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 3075 | cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3076 | err = ext4_ext_dirty(handle, inode, path); |
| 3077 | } |
| 3078 | } |
| 3079 | out: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 3080 | ext4_ext_drop_refs(path); |
| 3081 | kfree(path); |
| 3082 | path = NULL; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3083 | if (err == -EAGAIN) |
| 3084 | goto again; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3085 | ext4_journal_stop(handle); |
| 3086 | |
| 3087 | return err; |
| 3088 | } |
| 3089 | |
| 3090 | /* |
| 3091 | * called at mount time |
| 3092 | */ |
| 3093 | void ext4_ext_init(struct super_block *sb) |
| 3094 | { |
| 3095 | /* |
| 3096 | * possible initialization would be here |
| 3097 | */ |
| 3098 | |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3099 | if (ext4_has_feature_extents(sb)) { |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3100 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3101 | printk(KERN_INFO "EXT4-fs: file extents enabled" |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 3102 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3103 | ", aggressive tests" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3104 | #endif |
| 3105 | #ifdef CHECK_BINSEARCH |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3106 | ", check binsearch" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3107 | #endif |
| 3108 | #ifdef EXTENTS_STATS |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3109 | ", stats" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3110 | #endif |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3111 | "\n"); |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3112 | #endif |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3113 | #ifdef EXTENTS_STATS |
| 3114 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 3115 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 3116 | EXT4_SB(sb)->s_ext_max = 0; |
| 3117 | #endif |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | /* |
| 3122 | * called at umount time |
| 3123 | */ |
| 3124 | void ext4_ext_release(struct super_block *sb) |
| 3125 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3126 | if (!ext4_has_feature_extents(sb)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3127 | return; |
| 3128 | |
| 3129 | #ifdef EXTENTS_STATS |
| 3130 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 3131 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3132 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 3133 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 3134 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 3135 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 3136 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 3137 | } |
| 3138 | #endif |
| 3139 | } |
| 3140 | |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3141 | static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex) |
| 3142 | { |
| 3143 | ext4_lblk_t ee_block; |
| 3144 | ext4_fsblk_t ee_pblock; |
| 3145 | unsigned int ee_len; |
| 3146 | |
| 3147 | ee_block = le32_to_cpu(ex->ee_block); |
| 3148 | ee_len = ext4_ext_get_actual_len(ex); |
| 3149 | ee_pblock = ext4_ext_pblock(ex); |
| 3150 | |
| 3151 | if (ee_len == 0) |
| 3152 | return 0; |
| 3153 | |
| 3154 | return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, |
| 3155 | EXTENT_STATUS_WRITTEN); |
| 3156 | } |
| 3157 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3158 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 3159 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 3160 | { |
Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 3161 | ext4_fsblk_t ee_pblock; |
| 3162 | unsigned int ee_len; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3163 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3164 | ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3165 | ee_pblock = ext4_ext_pblock(ex); |
Jan Kara | 53085fa | 2015-12-07 15:09:35 -0500 | [diff] [blame] | 3166 | return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock, |
| 3167 | ee_len); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3168 | } |
| 3169 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3170 | /* |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3171 | * ext4_split_extent_at() splits an extent at given block. |
| 3172 | * |
| 3173 | * @handle: the journal handle |
| 3174 | * @inode: the file inode |
| 3175 | * @path: the path to the extent |
| 3176 | * @split: the logical block where the extent is splitted. |
| 3177 | * @split_flags: indicates if the extent could be zeroout if split fails, and |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3178 | * the states(init or unwritten) of new extents. |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3179 | * @flags: flags used to insert new extent to extent tree. |
| 3180 | * |
| 3181 | * |
| 3182 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states |
| 3183 | * of which are deterimined by split_flag. |
| 3184 | * |
| 3185 | * There are two cases: |
| 3186 | * a> the extent are splitted into two extent. |
| 3187 | * b> split is not needed, and just mark the extent. |
| 3188 | * |
| 3189 | * return 0 on success. |
| 3190 | */ |
| 3191 | static int ext4_split_extent_at(handle_t *handle, |
| 3192 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3193 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3194 | ext4_lblk_t split, |
| 3195 | int split_flag, |
| 3196 | int flags) |
| 3197 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3198 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3199 | ext4_fsblk_t newblock; |
| 3200 | ext4_lblk_t ee_block; |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3201 | struct ext4_extent *ex, newex, orig_ex, zero_ex; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3202 | struct ext4_extent *ex2 = NULL; |
| 3203 | unsigned int ee_len, depth; |
| 3204 | int err = 0; |
| 3205 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3206 | BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) == |
| 3207 | (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)); |
| 3208 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3209 | ext_debug("ext4_split_extents_at: inode %lu, logical" |
| 3210 | "block %llu\n", inode->i_ino, (unsigned long long)split); |
| 3211 | |
| 3212 | ext4_ext_show_leaf(inode, path); |
| 3213 | |
| 3214 | depth = ext_depth(inode); |
| 3215 | ex = path[depth].p_ext; |
| 3216 | ee_block = le32_to_cpu(ex->ee_block); |
| 3217 | ee_len = ext4_ext_get_actual_len(ex); |
| 3218 | newblock = split - ee_block + ext4_ext_pblock(ex); |
| 3219 | |
| 3220 | BUG_ON(split < ee_block || split >= (ee_block + ee_len)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3221 | BUG_ON(!ext4_ext_is_unwritten(ex) && |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3222 | split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3223 | EXT4_EXT_MARK_UNWRIT1 | |
| 3224 | EXT4_EXT_MARK_UNWRIT2)); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3225 | |
| 3226 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3227 | if (err) |
| 3228 | goto out; |
| 3229 | |
| 3230 | if (split == ee_block) { |
| 3231 | /* |
| 3232 | * case b: block @split is the block that the extent begins with |
| 3233 | * then we just change the state of the extent, and splitting |
| 3234 | * is not needed. |
| 3235 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3236 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3237 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3238 | else |
| 3239 | ext4_ext_mark_initialized(ex); |
| 3240 | |
| 3241 | if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3242 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3243 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3244 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3245 | goto out; |
| 3246 | } |
| 3247 | |
| 3248 | /* case a */ |
| 3249 | memcpy(&orig_ex, ex, sizeof(orig_ex)); |
| 3250 | ex->ee_len = cpu_to_le16(split - ee_block); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3251 | if (split_flag & EXT4_EXT_MARK_UNWRIT1) |
| 3252 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3253 | |
| 3254 | /* |
| 3255 | * path may lead to new leaf, not to original leaf any more |
| 3256 | * after ext4_ext_insert_extent() returns, |
| 3257 | */ |
| 3258 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3259 | if (err) |
| 3260 | goto fix_extent_len; |
| 3261 | |
| 3262 | ex2 = &newex; |
| 3263 | ex2->ee_block = cpu_to_le32(split); |
| 3264 | ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); |
| 3265 | ext4_ext_store_pblock(ex2, newblock); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3266 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3267 | ext4_ext_mark_unwritten(ex2); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3268 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3269 | err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3270 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3271 | if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3272 | if (split_flag & EXT4_EXT_DATA_VALID1) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3273 | err = ext4_ext_zeroout(inode, ex2); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3274 | zero_ex.ee_block = ex2->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3275 | zero_ex.ee_len = cpu_to_le16( |
| 3276 | ext4_ext_get_actual_len(ex2)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3277 | ext4_ext_store_pblock(&zero_ex, |
| 3278 | ext4_ext_pblock(ex2)); |
| 3279 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3280 | err = ext4_ext_zeroout(inode, ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3281 | zero_ex.ee_block = ex->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3282 | zero_ex.ee_len = cpu_to_le16( |
| 3283 | ext4_ext_get_actual_len(ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3284 | ext4_ext_store_pblock(&zero_ex, |
| 3285 | ext4_ext_pblock(ex)); |
| 3286 | } |
| 3287 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3288 | err = ext4_ext_zeroout(inode, &orig_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3289 | zero_ex.ee_block = orig_ex.ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3290 | zero_ex.ee_len = cpu_to_le16( |
| 3291 | ext4_ext_get_actual_len(&orig_ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3292 | ext4_ext_store_pblock(&zero_ex, |
| 3293 | ext4_ext_pblock(&orig_ex)); |
| 3294 | } |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3295 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3296 | if (err) |
| 3297 | goto fix_extent_len; |
| 3298 | /* update the extent length and mark as initialized */ |
Al Viro | af1584f | 2012-04-12 20:32:25 -0400 | [diff] [blame] | 3299 | ex->ee_len = cpu_to_le16(ee_len); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3300 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3301 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3302 | if (err) |
| 3303 | goto fix_extent_len; |
| 3304 | |
| 3305 | /* update extent status tree */ |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3306 | err = ext4_zeroout_es(inode, &zero_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3307 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3308 | goto out; |
| 3309 | } else if (err) |
| 3310 | goto fix_extent_len; |
| 3311 | |
| 3312 | out: |
| 3313 | ext4_ext_show_leaf(inode, path); |
| 3314 | return err; |
| 3315 | |
| 3316 | fix_extent_len: |
| 3317 | ex->ee_len = orig_ex.ee_len; |
Dmitry Monakhov | 29faed1 | 2014-07-27 22:30:29 -0400 | [diff] [blame] | 3318 | ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3319 | return err; |
| 3320 | } |
| 3321 | |
| 3322 | /* |
| 3323 | * ext4_split_extents() splits an extent and mark extent which is covered |
| 3324 | * by @map as split_flags indicates |
| 3325 | * |
Anatol Pomozov | 70261f5 | 2013-08-28 14:40:12 -0400 | [diff] [blame] | 3326 | * It may result in splitting the extent into multiple extents (up to three) |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3327 | * There are three possibilities: |
| 3328 | * a> There is no split required |
| 3329 | * b> Splits in two extents: Split is happening at either end of the extent |
| 3330 | * c> Splits in three extents: Somone is splitting in middle of the extent |
| 3331 | * |
| 3332 | */ |
| 3333 | static int ext4_split_extent(handle_t *handle, |
| 3334 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3335 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3336 | struct ext4_map_blocks *map, |
| 3337 | int split_flag, |
| 3338 | int flags) |
| 3339 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3340 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3341 | ext4_lblk_t ee_block; |
| 3342 | struct ext4_extent *ex; |
| 3343 | unsigned int ee_len, depth; |
| 3344 | int err = 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3345 | int unwritten; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3346 | int split_flag1, flags1; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3347 | int allocated = map->m_len; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3348 | |
| 3349 | depth = ext_depth(inode); |
| 3350 | ex = path[depth].p_ext; |
| 3351 | ee_block = le32_to_cpu(ex->ee_block); |
| 3352 | ee_len = ext4_ext_get_actual_len(ex); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3353 | unwritten = ext4_ext_is_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3354 | |
| 3355 | if (map->m_lblk + map->m_len < ee_block + ee_len) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3356 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3357 | flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3358 | if (unwritten) |
| 3359 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1 | |
| 3360 | EXT4_EXT_MARK_UNWRIT2; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3361 | if (split_flag & EXT4_EXT_DATA_VALID2) |
| 3362 | split_flag1 |= EXT4_EXT_DATA_VALID1; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3363 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3364 | map->m_lblk + map->m_len, split_flag1, flags1); |
Yongqiang Yang | 9391741 | 2011-05-22 20:49:12 -0400 | [diff] [blame] | 3365 | if (err) |
| 3366 | goto out; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3367 | } else { |
| 3368 | allocated = ee_len - (map->m_lblk - ee_block); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3369 | } |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3370 | /* |
| 3371 | * Update path is required because previous ext4_split_extent_at() may |
| 3372 | * result in split of original leaf or extent zeroout. |
| 3373 | */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3374 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3375 | if (IS_ERR(path)) |
| 3376 | return PTR_ERR(path); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3377 | depth = ext_depth(inode); |
| 3378 | ex = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3379 | if (!ex) { |
| 3380 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3381 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3382 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3383 | } |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3384 | unwritten = ext4_ext_is_unwritten(ex); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3385 | split_flag1 = 0; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3386 | |
| 3387 | if (map->m_lblk >= ee_block) { |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3388 | split_flag1 = split_flag & EXT4_EXT_DATA_VALID2; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3389 | if (unwritten) { |
| 3390 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1; |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3391 | split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3392 | EXT4_EXT_MARK_UNWRIT2); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3393 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3394 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3395 | map->m_lblk, split_flag1, flags); |
| 3396 | if (err) |
| 3397 | goto out; |
| 3398 | } |
| 3399 | |
| 3400 | ext4_ext_show_leaf(inode, path); |
| 3401 | out: |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3402 | return err ? err : allocated; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3403 | } |
| 3404 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3405 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3406 | * This function is called by ext4_ext_map_blocks() if someone tries to write |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3407 | * to an unwritten extent. It may result in splitting the unwritten |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3408 | * extent into multiple extents (up to three - one initialized and two |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3409 | * unwritten). |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3410 | * There are three possibilities: |
| 3411 | * a> There is no split required: Entire extent should be initialized |
| 3412 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3413 | * 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] | 3414 | * |
| 3415 | * Pre-conditions: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3416 | * - The extent pointed to by 'path' is unwritten. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3417 | * - The extent pointed to by 'path' contains a superset |
| 3418 | * of the logical span [map->m_lblk, map->m_lblk + map->m_len). |
| 3419 | * |
| 3420 | * Post-conditions on success: |
| 3421 | * - the returned value is the number of blocks beyond map->l_lblk |
| 3422 | * that are allocated and initialized. |
| 3423 | * It is guaranteed to be >= map->m_len. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3424 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3425 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3426 | struct inode *inode, |
| 3427 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3428 | struct ext4_ext_path **ppath, |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3429 | int flags) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3430 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3431 | struct ext4_ext_path *path = *ppath; |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3432 | struct ext4_sb_info *sbi; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3433 | struct ext4_extent_header *eh; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3434 | struct ext4_map_blocks split_map; |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3435 | struct ext4_extent zero_ex1, zero_ex2; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3436 | struct ext4_extent *ex, *abut_ex; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3437 | ext4_lblk_t ee_block, eof_block; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3438 | unsigned int ee_len, depth, map_len = map->m_len; |
| 3439 | int allocated = 0, max_zeroout = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3440 | int err = 0; |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3441 | int split_flag = EXT4_EXT_DATA_VALID2; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3442 | |
| 3443 | ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" |
| 3444 | "block %llu, max_blocks %u\n", inode->i_ino, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3445 | (unsigned long long)map->m_lblk, map_len); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3446 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3447 | sbi = EXT4_SB(inode->i_sb); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3448 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3449 | inode->i_sb->s_blocksize_bits; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3450 | if (eof_block < map->m_lblk + map_len) |
| 3451 | eof_block = map->m_lblk + map_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3452 | |
| 3453 | depth = ext_depth(inode); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3454 | eh = path[depth].p_hdr; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3455 | ex = path[depth].p_ext; |
| 3456 | ee_block = le32_to_cpu(ex->ee_block); |
| 3457 | ee_len = ext4_ext_get_actual_len(ex); |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3458 | zero_ex1.ee_len = 0; |
| 3459 | zero_ex2.ee_len = 0; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3460 | |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3461 | trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); |
| 3462 | |
| 3463 | /* Pre-conditions */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3464 | BUG_ON(!ext4_ext_is_unwritten(ex)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3465 | BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3466 | |
| 3467 | /* |
| 3468 | * Attempt to transfer newly initialized blocks from the currently |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3469 | * unwritten extent to its neighbor. This is much cheaper |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3470 | * than an insertion followed by a merge as those involve costly |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3471 | * memmove() calls. Transferring to the left is the common case in |
| 3472 | * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE) |
| 3473 | * followed by append writes. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3474 | * |
| 3475 | * Limitations of the current logic: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3476 | * - L1: we do not deal with writes covering the whole extent. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3477 | * This would require removing the extent if the transfer |
| 3478 | * is possible. |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3479 | * - L2: we only attempt to merge with an extent stored in the |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3480 | * same extent tree node. |
| 3481 | */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3482 | if ((map->m_lblk == ee_block) && |
| 3483 | /* See if we can merge left */ |
| 3484 | (map_len < ee_len) && /*L1*/ |
| 3485 | (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3486 | ext4_lblk_t prev_lblk; |
| 3487 | ext4_fsblk_t prev_pblk, ee_pblk; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3488 | unsigned int prev_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3489 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3490 | abut_ex = ex - 1; |
| 3491 | prev_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3492 | prev_len = ext4_ext_get_actual_len(abut_ex); |
| 3493 | prev_pblk = ext4_ext_pblock(abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3494 | ee_pblk = ext4_ext_pblock(ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3495 | |
| 3496 | /* |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3497 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3498 | * upon those conditions: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3499 | * - C1: abut_ex is initialized, |
| 3500 | * - C2: abut_ex is logically abutting ex, |
| 3501 | * - C3: abut_ex is physically abutting ex, |
| 3502 | * - C4: abut_ex can receive the additional blocks without |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3503 | * overflowing the (initialized) length limit. |
| 3504 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3505 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3506 | ((prev_lblk + prev_len) == ee_block) && /*C2*/ |
| 3507 | ((prev_pblk + prev_len) == ee_pblk) && /*C3*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3508 | (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3509 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3510 | if (err) |
| 3511 | goto out; |
| 3512 | |
| 3513 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3514 | map, ex, abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3515 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3516 | /* Shift the start of ex by 'map_len' blocks */ |
| 3517 | ex->ee_block = cpu_to_le32(ee_block + map_len); |
| 3518 | ext4_ext_store_pblock(ex, ee_pblk + map_len); |
| 3519 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3520 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3521 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3522 | /* Extend abut_ex by 'map_len' blocks */ |
| 3523 | abut_ex->ee_len = cpu_to_le16(prev_len + map_len); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3524 | |
| 3525 | /* Result: number of initialized blocks past m_lblk */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3526 | allocated = map_len; |
| 3527 | } |
| 3528 | } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) && |
| 3529 | (map_len < ee_len) && /*L1*/ |
| 3530 | ex < EXT_LAST_EXTENT(eh)) { /*L2*/ |
| 3531 | /* See if we can merge right */ |
| 3532 | ext4_lblk_t next_lblk; |
| 3533 | ext4_fsblk_t next_pblk, ee_pblk; |
| 3534 | unsigned int next_len; |
| 3535 | |
| 3536 | abut_ex = ex + 1; |
| 3537 | next_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3538 | next_len = ext4_ext_get_actual_len(abut_ex); |
| 3539 | next_pblk = ext4_ext_pblock(abut_ex); |
| 3540 | ee_pblk = ext4_ext_pblock(ex); |
| 3541 | |
| 3542 | /* |
| 3543 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
| 3544 | * upon those conditions: |
| 3545 | * - C1: abut_ex is initialized, |
| 3546 | * - C2: abut_ex is logically abutting ex, |
| 3547 | * - C3: abut_ex is physically abutting ex, |
| 3548 | * - C4: abut_ex can receive the additional blocks without |
| 3549 | * overflowing the (initialized) length limit. |
| 3550 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3551 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3552 | ((map->m_lblk + map_len) == next_lblk) && /*C2*/ |
| 3553 | ((ee_pblk + ee_len) == next_pblk) && /*C3*/ |
| 3554 | (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
| 3555 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3556 | if (err) |
| 3557 | goto out; |
| 3558 | |
| 3559 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
| 3560 | map, ex, abut_ex); |
| 3561 | |
| 3562 | /* Shift the start of abut_ex by 'map_len' blocks */ |
| 3563 | abut_ex->ee_block = cpu_to_le32(next_lblk - map_len); |
| 3564 | ext4_ext_store_pblock(abut_ex, next_pblk - map_len); |
| 3565 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3566 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3567 | |
| 3568 | /* Extend abut_ex by 'map_len' blocks */ |
| 3569 | abut_ex->ee_len = cpu_to_le16(next_len + map_len); |
| 3570 | |
| 3571 | /* Result: number of initialized blocks past m_lblk */ |
| 3572 | allocated = map_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3573 | } |
| 3574 | } |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3575 | if (allocated) { |
| 3576 | /* Mark the block containing both extents as dirty */ |
| 3577 | ext4_ext_dirty(handle, inode, path + depth); |
| 3578 | |
| 3579 | /* Update path to point to the right extent */ |
| 3580 | path[depth].p_ext = abut_ex; |
| 3581 | goto out; |
| 3582 | } else |
| 3583 | allocated = ee_len - (map->m_lblk - ee_block); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3584 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3585 | WARN_ON(map->m_lblk < ee_block); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3586 | /* |
| 3587 | * It is safe to convert extent to initialized via explicit |
Yongqiang Yang | 9e74056 | 2014-01-06 14:05:23 -0500 | [diff] [blame] | 3588 | * zeroout only if extent is fully inside i_size or new_size. |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3589 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3590 | 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] | 3591 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3592 | if (EXT4_EXT_MAY_ZEROOUT & split_flag) |
| 3593 | max_zeroout = sbi->s_extent_max_zeroout_kb >> |
Lukas Czerner | 4f42f80 | 2013-03-12 12:40:04 -0400 | [diff] [blame] | 3594 | (inode->i_sb->s_blocksize_bits - 10); |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3595 | |
Theodore Ts'o | 36086d4 | 2015-10-03 10:49:29 -0400 | [diff] [blame] | 3596 | if (ext4_encrypted_inode(inode)) |
| 3597 | max_zeroout = 0; |
| 3598 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3599 | /* |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3600 | * five cases: |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3601 | * 1. split the extent into three extents. |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3602 | * 2. split the extent into two extents, zeroout the head of the first |
| 3603 | * extent. |
| 3604 | * 3. split the extent into two extents, zeroout the tail of the second |
| 3605 | * extent. |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3606 | * 4. split the extent into two extents with out zeroout. |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3607 | * 5. no splitting needed, just possibly zeroout the head and / or the |
| 3608 | * tail of the extent. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3609 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3610 | split_map.m_lblk = map->m_lblk; |
| 3611 | split_map.m_len = map->m_len; |
| 3612 | |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3613 | if (max_zeroout && (allocated > split_map.m_len)) { |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3614 | if (allocated <= max_zeroout) { |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3615 | /* case 3 or 5 */ |
| 3616 | zero_ex1.ee_block = |
| 3617 | cpu_to_le32(split_map.m_lblk + |
| 3618 | split_map.m_len); |
| 3619 | zero_ex1.ee_len = |
| 3620 | cpu_to_le16(allocated - split_map.m_len); |
| 3621 | ext4_ext_store_pblock(&zero_ex1, |
| 3622 | ext4_ext_pblock(ex) + split_map.m_lblk + |
| 3623 | split_map.m_len - ee_block); |
| 3624 | err = ext4_ext_zeroout(inode, &zero_ex1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3625 | if (err) |
| 3626 | goto out; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3627 | split_map.m_len = allocated; |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3628 | } |
| 3629 | if (split_map.m_lblk - ee_block + split_map.m_len < |
| 3630 | max_zeroout) { |
| 3631 | /* case 2 or 5 */ |
| 3632 | if (split_map.m_lblk != ee_block) { |
| 3633 | zero_ex2.ee_block = ex->ee_block; |
| 3634 | zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk - |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3635 | ee_block); |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3636 | ext4_ext_store_pblock(&zero_ex2, |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3637 | ext4_ext_pblock(ex)); |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3638 | err = ext4_ext_zeroout(inode, &zero_ex2); |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3639 | if (err) |
| 3640 | goto out; |
| 3641 | } |
| 3642 | |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3643 | split_map.m_len += split_map.m_lblk - ee_block; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3644 | split_map.m_lblk = ee_block; |
Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3645 | allocated = map->m_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3646 | } |
| 3647 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3648 | |
Jan Kara | ae9e9c6a | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 3649 | err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag, |
| 3650 | flags); |
| 3651 | if (err > 0) |
| 3652 | err = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3653 | out: |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3654 | /* If we have gotten a failure, don't zero out status tree */ |
Jan Kara | 2e16921 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3655 | if (!err) { |
| 3656 | err = ext4_zeroout_es(inode, &zero_ex1); |
| 3657 | if (!err) |
| 3658 | err = ext4_zeroout_es(inode, &zero_ex2); |
| 3659 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3660 | return err ? err : allocated; |
| 3661 | } |
| 3662 | |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3663 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3664 | * This function is called by ext4_ext_map_blocks() from |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3665 | * ext4_get_blocks_dio_write() when DIO to write |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3666 | * to an unwritten extent. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3667 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3668 | * Writing to an unwritten extent may result in splitting the unwritten |
| 3669 | * extent into multiple initialized/unwritten extents (up to three) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3670 | * There are three possibilities: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3671 | * a> There is no split required: Entire extent should be unwritten |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3672 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3673 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 3674 | * |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3675 | * This works the same way in the case of initialized -> unwritten conversion. |
| 3676 | * |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3677 | * One of more index blocks maybe needed if the extent tree grow after |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3678 | * the unwritten extent split. To prevent ENOSPC occur at the IO |
| 3679 | * complete, we need to split the unwritten extent before DIO submit |
| 3680 | * the IO. The unwritten extent called at this time will be split |
| 3681 | * into three unwritten extent(at most). After IO complete, the part |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3682 | * being filled will be convert to initialized by the end_io callback function |
| 3683 | * via ext4_convert_unwritten_extents(). |
Mingming | ba230c3 | 2009-11-06 04:01:23 -0500 | [diff] [blame] | 3684 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3685 | * Returns the size of unwritten extent to be written on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3686 | */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3687 | static int ext4_split_convert_extents(handle_t *handle, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3688 | struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3689 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3690 | struct ext4_ext_path **ppath, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3691 | int flags) |
| 3692 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3693 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3694 | ext4_lblk_t eof_block; |
| 3695 | ext4_lblk_t ee_block; |
| 3696 | struct ext4_extent *ex; |
| 3697 | unsigned int ee_len; |
| 3698 | int split_flag = 0, depth; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3699 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3700 | ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n", |
| 3701 | __func__, inode->i_ino, |
| 3702 | (unsigned long long)map->m_lblk, map->m_len); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3703 | |
| 3704 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3705 | inode->i_sb->s_blocksize_bits; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3706 | if (eof_block < map->m_lblk + map->m_len) |
| 3707 | eof_block = map->m_lblk + map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3708 | /* |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3709 | * It is safe to convert extent to initialized via explicit |
| 3710 | * zeroout only if extent is fully insde i_size or new_size. |
| 3711 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3712 | depth = ext_depth(inode); |
| 3713 | ex = path[depth].p_ext; |
| 3714 | ee_block = le32_to_cpu(ex->ee_block); |
| 3715 | ee_len = ext4_ext_get_actual_len(ex); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3716 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3717 | /* Convert to unwritten */ |
| 3718 | if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { |
| 3719 | split_flag |= EXT4_EXT_DATA_VALID1; |
| 3720 | /* Convert to initialized */ |
| 3721 | } else if (flags & EXT4_GET_BLOCKS_CONVERT) { |
| 3722 | split_flag |= ee_block + ee_len <= eof_block ? |
| 3723 | EXT4_EXT_MAY_ZEROOUT : 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3724 | split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3725 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3726 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3727 | return ext4_split_extent(handle, inode, ppath, map, split_flag, flags); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3728 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3729 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3730 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3731 | struct inode *inode, |
| 3732 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3733 | struct ext4_ext_path **ppath) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3734 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3735 | struct ext4_ext_path *path = *ppath; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3736 | struct ext4_extent *ex; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3737 | ext4_lblk_t ee_block; |
| 3738 | unsigned int ee_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3739 | int depth; |
| 3740 | int err = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3741 | |
| 3742 | depth = ext_depth(inode); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3743 | ex = path[depth].p_ext; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3744 | ee_block = le32_to_cpu(ex->ee_block); |
| 3745 | ee_len = ext4_ext_get_actual_len(ex); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3746 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3747 | ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" |
| 3748 | "block %llu, max_blocks %u\n", inode->i_ino, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3749 | (unsigned long long)ee_block, ee_len); |
| 3750 | |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3751 | /* If extent is larger than requested it is a clear sign that we still |
| 3752 | * have some extent state machine issues left. So extent_split is still |
| 3753 | * required. |
| 3754 | * TODO: Once all related issues will be fixed this situation should be |
| 3755 | * illegal. |
| 3756 | */ |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3757 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3758 | #ifdef EXT4_DEBUG |
| 3759 | ext4_warning("Inode (%ld) finished: extent logical block %llu," |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 3760 | " len %u; IO logical block %llu, len %u", |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3761 | inode->i_ino, (unsigned long long)ee_block, ee_len, |
| 3762 | (unsigned long long)map->m_lblk, map->m_len); |
| 3763 | #endif |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3764 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3765 | EXT4_GET_BLOCKS_CONVERT); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3766 | if (err < 0) |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3767 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3768 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3769 | if (IS_ERR(path)) |
| 3770 | return PTR_ERR(path); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3771 | depth = ext_depth(inode); |
| 3772 | ex = path[depth].p_ext; |
| 3773 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3774 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3775 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3776 | if (err) |
| 3777 | goto out; |
| 3778 | /* first mark the extent as initialized */ |
| 3779 | ext4_ext_mark_initialized(ex); |
| 3780 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3781 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3782 | * borders are not changed |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3783 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3784 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3785 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3786 | /* Mark modified extent as dirty */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3787 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3788 | out: |
| 3789 | ext4_ext_show_leaf(inode, path); |
| 3790 | return err; |
| 3791 | } |
| 3792 | |
Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3793 | static void unmap_underlying_metadata_blocks(struct block_device *bdev, |
| 3794 | sector_t block, int count) |
| 3795 | { |
| 3796 | int i; |
| 3797 | for (i = 0; i < count; i++) |
| 3798 | unmap_underlying_metadata(bdev, block + i); |
| 3799 | } |
| 3800 | |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3801 | /* |
| 3802 | * Handle EOFBLOCKS_FL flag, clearing it if necessary |
| 3803 | */ |
| 3804 | static int check_eofblocks_fl(handle_t *handle, struct inode *inode, |
Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3805 | ext4_lblk_t lblk, |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3806 | struct ext4_ext_path *path, |
| 3807 | unsigned int len) |
| 3808 | { |
| 3809 | int i, depth; |
| 3810 | struct ext4_extent_header *eh; |
Sergey Senozhatsky | 65922cb | 2011-03-23 14:08:27 -0400 | [diff] [blame] | 3811 | struct ext4_extent *last_ex; |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3812 | |
| 3813 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) |
| 3814 | return 0; |
| 3815 | |
| 3816 | depth = ext_depth(inode); |
| 3817 | eh = path[depth].p_hdr; |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3818 | |
Lukas Czerner | afcff5d | 2012-03-21 21:47:55 -0400 | [diff] [blame] | 3819 | /* |
| 3820 | * We're going to remove EOFBLOCKS_FL entirely in future so we |
| 3821 | * do not care for this case anymore. Simply remove the flag |
| 3822 | * if there are no extents. |
| 3823 | */ |
| 3824 | if (unlikely(!eh->eh_entries)) |
| 3825 | goto out; |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3826 | last_ex = EXT_LAST_EXTENT(eh); |
| 3827 | /* |
| 3828 | * We should clear the EOFBLOCKS_FL flag if we are writing the |
| 3829 | * last block in the last extent in the file. We test this by |
| 3830 | * first checking to see if the caller to |
| 3831 | * ext4_ext_get_blocks() was interested in the last block (or |
| 3832 | * a block beyond the last block) in the current extent. If |
| 3833 | * this turns out to be false, we can bail out from this |
| 3834 | * function immediately. |
| 3835 | */ |
Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3836 | if (lblk + len < le32_to_cpu(last_ex->ee_block) + |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3837 | ext4_ext_get_actual_len(last_ex)) |
| 3838 | return 0; |
| 3839 | /* |
| 3840 | * If the caller does appear to be planning to write at or |
| 3841 | * beyond the end of the current extent, we then test to see |
| 3842 | * if the current extent is the last extent in the file, by |
| 3843 | * checking to make sure it was reached via the rightmost node |
| 3844 | * at each level of the tree. |
| 3845 | */ |
| 3846 | for (i = depth-1; i >= 0; i--) |
| 3847 | if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr)) |
| 3848 | return 0; |
Lukas Czerner | afcff5d | 2012-03-21 21:47:55 -0400 | [diff] [blame] | 3849 | out: |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3850 | ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
| 3851 | return ext4_mark_inode_dirty(handle, inode); |
| 3852 | } |
| 3853 | |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3854 | /** |
| 3855 | * ext4_find_delalloc_range: find delayed allocated block in the given range. |
| 3856 | * |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3857 | * Return 1 if there is a delalloc block in the range, otherwise 0. |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3858 | */ |
Zheng Liu | f7fec03 | 2013-02-18 00:28:47 -0500 | [diff] [blame] | 3859 | int ext4_find_delalloc_range(struct inode *inode, |
| 3860 | ext4_lblk_t lblk_start, |
| 3861 | ext4_lblk_t lblk_end) |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3862 | { |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3863 | struct extent_status es; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3864 | |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 3865 | ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es); |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 3866 | if (es.es_len == 0) |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3867 | return 0; /* there is no delay extent in this tree */ |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 3868 | else if (es.es_lblk <= lblk_start && |
| 3869 | lblk_start < es.es_lblk + es.es_len) |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3870 | return 1; |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 3871 | else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end) |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3872 | return 1; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3873 | else |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3874 | return 0; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3875 | } |
| 3876 | |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3877 | int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk) |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3878 | { |
| 3879 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 3880 | ext4_lblk_t lblk_start, lblk_end; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3881 | lblk_start = EXT4_LBLK_CMASK(sbi, lblk); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3882 | lblk_end = lblk_start + sbi->s_cluster_ratio - 1; |
| 3883 | |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3884 | return ext4_find_delalloc_range(inode, lblk_start, lblk_end); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | /** |
| 3888 | * Determines how many complete clusters (out of those specified by the 'map') |
| 3889 | * are under delalloc and were reserved quota for. |
| 3890 | * This function is called when we are writing out the blocks that were |
| 3891 | * originally written with their allocation delayed, but then the space was |
| 3892 | * allocated using fallocate() before the delayed allocation could be resolved. |
| 3893 | * The cases to look for are: |
| 3894 | * ('=' indicated delayed allocated blocks |
| 3895 | * '-' indicates non-delayed allocated blocks) |
| 3896 | * (a) partial clusters towards beginning and/or end outside of allocated range |
| 3897 | * are not delalloc'ed. |
| 3898 | * Ex: |
| 3899 | * |----c---=|====c====|====c====|===-c----| |
| 3900 | * |++++++ allocated ++++++| |
| 3901 | * ==> 4 complete clusters in above example |
| 3902 | * |
| 3903 | * (b) partial cluster (outside of allocated range) towards either end is |
| 3904 | * marked for delayed allocation. In this case, we will exclude that |
| 3905 | * cluster. |
| 3906 | * Ex: |
| 3907 | * |----====c========|========c========| |
| 3908 | * |++++++ allocated ++++++| |
| 3909 | * ==> 1 complete clusters in above example |
| 3910 | * |
| 3911 | * Ex: |
| 3912 | * |================c================| |
| 3913 | * |++++++ allocated ++++++| |
| 3914 | * ==> 0 complete clusters in above example |
| 3915 | * |
| 3916 | * The ext4_da_update_reserve_space will be called only if we |
| 3917 | * determine here that there were some "entire" clusters that span |
| 3918 | * this 'allocated' range. |
| 3919 | * In the non-bigalloc case, this function will just end up returning num_blks |
| 3920 | * without ever calling ext4_find_delalloc_range. |
| 3921 | */ |
| 3922 | static unsigned int |
| 3923 | get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start, |
| 3924 | unsigned int num_blks) |
| 3925 | { |
| 3926 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 3927 | ext4_lblk_t alloc_cluster_start, alloc_cluster_end; |
| 3928 | ext4_lblk_t lblk_from, lblk_to, c_offset; |
| 3929 | unsigned int allocated_clusters = 0; |
| 3930 | |
| 3931 | alloc_cluster_start = EXT4_B2C(sbi, lblk_start); |
| 3932 | alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1); |
| 3933 | |
| 3934 | /* max possible clusters for this allocation */ |
| 3935 | allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1; |
| 3936 | |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3937 | trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks); |
| 3938 | |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3939 | /* Check towards left side */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3940 | c_offset = EXT4_LBLK_COFF(sbi, lblk_start); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3941 | if (c_offset) { |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3942 | lblk_from = EXT4_LBLK_CMASK(sbi, lblk_start); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3943 | lblk_to = lblk_from + c_offset - 1; |
| 3944 | |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3945 | if (ext4_find_delalloc_range(inode, lblk_from, lblk_to)) |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3946 | allocated_clusters--; |
| 3947 | } |
| 3948 | |
| 3949 | /* Now check towards right. */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3950 | c_offset = EXT4_LBLK_COFF(sbi, lblk_start + num_blks); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3951 | if (allocated_clusters && c_offset) { |
| 3952 | lblk_from = lblk_start + num_blks; |
| 3953 | lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1; |
| 3954 | |
Zheng Liu | 7d1b1fb | 2012-11-08 21:57:35 -0500 | [diff] [blame] | 3955 | if (ext4_find_delalloc_range(inode, lblk_from, lblk_to)) |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3956 | allocated_clusters--; |
| 3957 | } |
| 3958 | |
| 3959 | return allocated_clusters; |
| 3960 | } |
| 3961 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3962 | static int |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3963 | convert_initialized_extent(handle_t *handle, struct inode *inode, |
| 3964 | struct ext4_map_blocks *map, |
Eric Whitney | 29c6eaf | 2016-02-22 22:58:55 -0500 | [diff] [blame] | 3965 | struct ext4_ext_path **ppath, |
Eryu Guan | 56263b4 | 2016-02-12 01:23:00 -0500 | [diff] [blame] | 3966 | unsigned int allocated) |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3967 | { |
Theodore Ts'o | 4f224b8 | 2014-09-01 14:36:09 -0400 | [diff] [blame] | 3968 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3969 | struct ext4_extent *ex; |
| 3970 | ext4_lblk_t ee_block; |
| 3971 | unsigned int ee_len; |
| 3972 | int depth; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3973 | int err = 0; |
| 3974 | |
| 3975 | /* |
| 3976 | * Make sure that the extent is no bigger than we support with |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3977 | * unwritten extent |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3978 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3979 | if (map->m_len > EXT_UNWRITTEN_MAX_LEN) |
| 3980 | map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3981 | |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3982 | depth = ext_depth(inode); |
| 3983 | ex = path[depth].p_ext; |
| 3984 | ee_block = le32_to_cpu(ex->ee_block); |
| 3985 | ee_len = ext4_ext_get_actual_len(ex); |
| 3986 | |
| 3987 | ext_debug("%s: inode %lu, logical" |
| 3988 | "block %llu, max_blocks %u\n", __func__, inode->i_ino, |
| 3989 | (unsigned long long)ee_block, ee_len); |
| 3990 | |
| 3991 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3992 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3993 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN); |
| 3994 | if (err < 0) |
| 3995 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3996 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3997 | if (IS_ERR(path)) |
| 3998 | return PTR_ERR(path); |
| 3999 | depth = ext_depth(inode); |
| 4000 | ex = path[depth].p_ext; |
| 4001 | if (!ex) { |
| 4002 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 4003 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4004 | return -EFSCORRUPTED; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 4009 | if (err) |
| 4010 | return err; |
| 4011 | /* first mark the extent as unwritten */ |
| 4012 | ext4_ext_mark_unwritten(ex); |
| 4013 | |
| 4014 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 4015 | * borders are not changed |
| 4016 | */ |
| 4017 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 4018 | |
| 4019 | /* Mark modified extent as dirty */ |
| 4020 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
| 4021 | if (err) |
| 4022 | return err; |
| 4023 | ext4_ext_show_leaf(inode, path); |
| 4024 | |
| 4025 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 4026 | err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len); |
| 4027 | if (err) |
| 4028 | return err; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4029 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
| 4030 | if (allocated > map->m_len) |
| 4031 | allocated = map->m_len; |
| 4032 | map->m_len = allocated; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 4033 | return allocated; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | static int |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4037 | ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4038 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4039 | struct ext4_ext_path **ppath, int flags, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4040 | unsigned int allocated, ext4_fsblk_t newblock) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4041 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4042 | struct ext4_ext_path *path = *ppath; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4043 | int ret = 0; |
| 4044 | int err = 0; |
| 4045 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4046 | ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical " |
Zheng Liu | 88635ca | 2011-12-28 19:00:25 -0500 | [diff] [blame] | 4047 | "block %llu, max_blocks %u, flags %x, allocated %u\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4048 | inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4049 | flags, allocated); |
| 4050 | ext4_ext_show_leaf(inode, path); |
| 4051 | |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 4052 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4053 | * When writing into unwritten space, we should not fail to |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 4054 | * allocate metadata blocks for the new extent block if needed. |
| 4055 | */ |
| 4056 | flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL; |
| 4057 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4058 | trace_ext4_ext_handle_unwritten_extents(inode, map, flags, |
Zheng Liu | b564553 | 2012-11-08 14:33:43 -0500 | [diff] [blame] | 4059 | allocated, newblock); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4060 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 4061 | /* get_block() before submit the IO, split the extent */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 4062 | if (flags & EXT4_GET_BLOCKS_PRE_IO) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4063 | ret = ext4_split_convert_extents(handle, inode, map, ppath, |
| 4064 | flags | EXT4_GET_BLOCKS_CONVERT); |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 4065 | if (ret <= 0) |
| 4066 | goto out; |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4067 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4068 | goto out; |
| 4069 | } |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 4070 | /* IO end_io complete, convert the filled extent to written */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 4071 | if (flags & EXT4_GET_BLOCKS_CONVERT) { |
Jan Kara | c86d8db | 2015-12-07 15:10:26 -0500 | [diff] [blame] | 4072 | if (flags & EXT4_GET_BLOCKS_ZERO) { |
| 4073 | if (allocated > map->m_len) |
| 4074 | allocated = map->m_len; |
| 4075 | err = ext4_issue_zeroout(inode, map->m_lblk, newblock, |
| 4076 | allocated); |
| 4077 | if (err < 0) |
| 4078 | goto out2; |
| 4079 | } |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 4080 | ret = ext4_convert_unwritten_extents_endio(handle, inode, map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4081 | ppath); |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 4082 | if (ret >= 0) { |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4083 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 4084 | err = check_eofblocks_fl(handle, inode, map->m_lblk, |
| 4085 | path, map->m_len); |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 4086 | } else |
| 4087 | err = ret; |
Zheng Liu | cdee784 | 2013-03-10 21:08:52 -0400 | [diff] [blame] | 4088 | map->m_flags |= EXT4_MAP_MAPPED; |
Eric Whitney | 15cc176 | 2014-02-12 10:42:45 -0500 | [diff] [blame] | 4089 | map->m_pblk = newblock; |
Zheng Liu | cdee784 | 2013-03-10 21:08:52 -0400 | [diff] [blame] | 4090 | if (allocated > map->m_len) |
| 4091 | allocated = map->m_len; |
| 4092 | map->m_len = allocated; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4093 | goto out2; |
| 4094 | } |
| 4095 | /* buffered IO case */ |
| 4096 | /* |
| 4097 | * repeat fallocate creation request |
| 4098 | * we already have an unwritten extent |
| 4099 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4100 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4101 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4102 | goto map_out; |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4103 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4104 | |
| 4105 | /* buffered READ or buffered write_begin() lookup */ |
| 4106 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 4107 | /* |
| 4108 | * We have blocks reserved already. We |
| 4109 | * return allocated blocks so that delalloc |
| 4110 | * won't do block reservation for us. But |
| 4111 | * the buffer head will be unmapped so that |
| 4112 | * a read from the block returns 0s. |
| 4113 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4114 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4115 | goto out1; |
| 4116 | } |
| 4117 | |
| 4118 | /* buffered write, writepage time, convert*/ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4119 | ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags); |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4120 | if (ret >= 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4121 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4122 | out: |
| 4123 | if (ret <= 0) { |
| 4124 | err = ret; |
| 4125 | goto out2; |
| 4126 | } else |
| 4127 | allocated = ret; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4128 | map->m_flags |= EXT4_MAP_NEW; |
Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 4129 | /* |
| 4130 | * if we allocated more blocks than requested |
| 4131 | * we need to make sure we unmap the extra block |
| 4132 | * allocated. The actual needed block will get |
| 4133 | * unmapped later when we find the buffer_head marked |
| 4134 | * new. |
| 4135 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4136 | if (allocated > map->m_len) { |
Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 4137 | unmap_underlying_metadata_blocks(inode->i_sb->s_bdev, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4138 | newblock + map->m_len, |
| 4139 | allocated - map->m_len); |
| 4140 | allocated = map->m_len; |
Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 4141 | } |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 4142 | map->m_len = allocated; |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4143 | |
| 4144 | /* |
| 4145 | * If we have done fallocate with the offset that is already |
| 4146 | * delayed allocated, we would have block reservation |
| 4147 | * and quota reservation done in the delayed write path. |
| 4148 | * But fallocate would have already updated quota and block |
| 4149 | * count for this offset. So cancel these reservation |
| 4150 | */ |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4151 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
| 4152 | unsigned int reserved_clusters; |
| 4153 | reserved_clusters = get_reserved_cluster_alloc(inode, |
| 4154 | map->m_lblk, map->m_len); |
| 4155 | if (reserved_clusters) |
| 4156 | ext4_da_update_reserve_space(inode, |
| 4157 | reserved_clusters, |
| 4158 | 0); |
| 4159 | } |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4160 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4161 | map_out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4162 | map->m_flags |= EXT4_MAP_MAPPED; |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4163 | if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) { |
| 4164 | err = check_eofblocks_fl(handle, inode, map->m_lblk, path, |
| 4165 | map->m_len); |
| 4166 | if (err < 0) |
| 4167 | goto out2; |
| 4168 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4169 | out1: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4170 | if (allocated > map->m_len) |
| 4171 | allocated = map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4172 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4173 | map->m_pblk = newblock; |
| 4174 | map->m_len = allocated; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4175 | out2: |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4176 | return err ? err : allocated; |
| 4177 | } |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 4178 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4179 | /* |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4180 | * get_implied_cluster_alloc - check to see if the requested |
| 4181 | * allocation (in the map structure) overlaps with a cluster already |
| 4182 | * allocated in an extent. |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4183 | * @sb The filesystem superblock structure |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4184 | * @map The requested lblk->pblk mapping |
| 4185 | * @ex The extent structure which might contain an implied |
| 4186 | * cluster allocation |
| 4187 | * |
| 4188 | * This function is called by ext4_ext_map_blocks() after we failed to |
| 4189 | * find blocks that were already in the inode's extent tree. Hence, |
| 4190 | * we know that the beginning of the requested region cannot overlap |
| 4191 | * the extent from the inode's extent tree. There are three cases we |
| 4192 | * want to catch. The first is this case: |
| 4193 | * |
| 4194 | * |--- cluster # N--| |
| 4195 | * |--- extent ---| |---- requested region ---| |
| 4196 | * |==========| |
| 4197 | * |
| 4198 | * The second case that we need to test for is this one: |
| 4199 | * |
| 4200 | * |--------- cluster # N ----------------| |
| 4201 | * |--- requested region --| |------- extent ----| |
| 4202 | * |=======================| |
| 4203 | * |
| 4204 | * The third case is when the requested region lies between two extents |
| 4205 | * within the same cluster: |
| 4206 | * |------------- cluster # N-------------| |
| 4207 | * |----- ex -----| |---- ex_right ----| |
| 4208 | * |------ requested region ------| |
| 4209 | * |================| |
| 4210 | * |
| 4211 | * In each of the above cases, we need to set the map->m_pblk and |
| 4212 | * map->m_len so it corresponds to the return the extent labelled as |
| 4213 | * "|====|" from cluster #N, since it is already in use for data in |
| 4214 | * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to |
| 4215 | * signal to ext4_ext_map_blocks() that map->m_pblk should be treated |
| 4216 | * as a new "allocated" block region. Otherwise, we will return 0 and |
| 4217 | * ext4_ext_map_blocks() will then allocate one or more new clusters |
| 4218 | * by calling ext4_mb_new_blocks(). |
| 4219 | */ |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4220 | static int get_implied_cluster_alloc(struct super_block *sb, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4221 | struct ext4_map_blocks *map, |
| 4222 | struct ext4_extent *ex, |
| 4223 | struct ext4_ext_path *path) |
| 4224 | { |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4225 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4226 | ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4227 | ext4_lblk_t ex_cluster_start, ex_cluster_end; |
Curt Wohlgemuth | 14d7f3e | 2011-12-18 17:39:02 -0500 | [diff] [blame] | 4228 | ext4_lblk_t rr_cluster_start; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4229 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
| 4230 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
| 4231 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
| 4232 | |
| 4233 | /* The extent passed in that we are trying to match */ |
| 4234 | ex_cluster_start = EXT4_B2C(sbi, ee_block); |
| 4235 | ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); |
| 4236 | |
| 4237 | /* The requested region passed into ext4_map_blocks() */ |
| 4238 | rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4239 | |
| 4240 | if ((rr_cluster_start == ex_cluster_end) || |
| 4241 | (rr_cluster_start == ex_cluster_start)) { |
| 4242 | if (rr_cluster_start == ex_cluster_end) |
| 4243 | ee_start += ee_len - 1; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4244 | map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4245 | map->m_len = min(map->m_len, |
| 4246 | (unsigned) sbi->s_cluster_ratio - c_offset); |
| 4247 | /* |
| 4248 | * Check for and handle this case: |
| 4249 | * |
| 4250 | * |--------- cluster # N-------------| |
| 4251 | * |------- extent ----| |
| 4252 | * |--- requested region ---| |
| 4253 | * |===========| |
| 4254 | */ |
| 4255 | |
| 4256 | if (map->m_lblk < ee_block) |
| 4257 | map->m_len = min(map->m_len, ee_block - map->m_lblk); |
| 4258 | |
| 4259 | /* |
| 4260 | * Check for the case where there is already another allocated |
| 4261 | * block to the right of 'ex' but before the end of the cluster. |
| 4262 | * |
| 4263 | * |------------- cluster # N-------------| |
| 4264 | * |----- ex -----| |---- ex_right ----| |
| 4265 | * |------ requested region ------| |
| 4266 | * |================| |
| 4267 | */ |
| 4268 | if (map->m_lblk > ee_block) { |
| 4269 | ext4_lblk_t next = ext4_ext_next_allocated_block(path); |
| 4270 | map->m_len = min(map->m_len, next - map->m_lblk); |
| 4271 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4272 | |
| 4273 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4274 | return 1; |
| 4275 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4276 | |
| 4277 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4278 | return 0; |
| 4279 | } |
| 4280 | |
| 4281 | |
| 4282 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 4283 | * Block allocation/map/preallocation routine for extents based files |
| 4284 | * |
| 4285 | * |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4286 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4287 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 4288 | * (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] | 4289 | * |
| 4290 | * return > 0, number of of blocks already mapped/allocated |
| 4291 | * if create == 0 and these are pre-allocated blocks |
| 4292 | * buffer head is unmapped |
| 4293 | * otherwise blocks are mapped |
| 4294 | * |
| 4295 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 4296 | * buffer head is unmapped |
| 4297 | * |
| 4298 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4299 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4300 | int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, |
| 4301 | struct ext4_map_blocks *map, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4302 | { |
| 4303 | struct ext4_ext_path *path = NULL; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4304 | struct ext4_extent newex, *ex, *ex2; |
| 4305 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4306 | ext4_fsblk_t newblock = 0; |
Eric Whitney | ce37c42 | 2014-02-19 18:52:39 -0500 | [diff] [blame] | 4307 | int free_on_err = 0, err = 0, depth, ret; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4308 | unsigned int allocated = 0, offset = 0; |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4309 | unsigned int allocated_clusters = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4310 | struct ext4_allocation_request ar; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4311 | ext4_lblk_t cluster_offset; |
Jan Kara | cbd7584 | 2014-11-25 11:41:49 -0500 | [diff] [blame] | 4312 | bool map_from_cluster = false; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4313 | |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 4314 | ext_debug("blocks %u/%u requested for inode %lu\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4315 | map->m_lblk, map->m_len, inode->i_ino); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4316 | 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] | 4317 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4318 | /* find extent for this block */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4319 | path = ext4_find_extent(inode, map->m_lblk, NULL, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4320 | if (IS_ERR(path)) { |
| 4321 | err = PTR_ERR(path); |
| 4322 | path = NULL; |
| 4323 | goto out2; |
| 4324 | } |
| 4325 | |
| 4326 | depth = ext_depth(inode); |
| 4327 | |
| 4328 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4329 | * consistent leaf must not be empty; |
| 4330 | * this situation is possible, though, _during_ tree modification; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4331 | * this is why assert can't be put in ext4_find_extent() |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4332 | */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 4333 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 4334 | EXT4_ERROR_INODE(inode, "bad extent address " |
Theodore Ts'o | f70f362 | 2010-05-16 23:00:00 -0400 | [diff] [blame] | 4335 | "lblock: %lu, depth: %d pblock %lld", |
| 4336 | (unsigned long) map->m_lblk, depth, |
| 4337 | path[depth].p_block); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4338 | err = -EFSCORRUPTED; |
Surbhi Palande | 034fb4c | 2009-12-14 09:53:52 -0500 | [diff] [blame] | 4339 | goto out2; |
| 4340 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4341 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 4342 | ex = path[depth].p_ext; |
| 4343 | if (ex) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4344 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4345 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4346 | unsigned short ee_len; |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4347 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4348 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4349 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4350 | * unwritten extents are treated as holes, except that |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4351 | * we split out initialized portions during a write. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4352 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4353 | ee_len = ext4_ext_get_actual_len(ex); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4354 | |
| 4355 | trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); |
| 4356 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4357 | /* if found extent covers block, simply return it */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4358 | if (in_range(map->m_lblk, ee_block, ee_len)) { |
| 4359 | newblock = map->m_lblk - ee_block + ee_start; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4360 | /* number of remaining blocks in the extent */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4361 | allocated = ee_len - (map->m_lblk - ee_block); |
| 4362 | ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, |
| 4363 | ee_block, ee_len, newblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4364 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4365 | /* |
| 4366 | * If the extent is initialized check whether the |
| 4367 | * caller wants to convert it to unwritten. |
| 4368 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4369 | if ((!ext4_ext_is_unwritten(ex)) && |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4370 | (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 4371 | allocated = convert_initialized_extent( |
Theodore Ts'o | 4f224b8 | 2014-09-01 14:36:09 -0400 | [diff] [blame] | 4372 | handle, inode, map, &path, |
Eric Whitney | 29c6eaf | 2016-02-22 22:58:55 -0500 | [diff] [blame] | 4373 | allocated); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4374 | goto out2; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4375 | } else if (!ext4_ext_is_unwritten(ex)) |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4376 | goto out; |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4377 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4378 | ret = ext4_ext_handle_unwritten_extents( |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4379 | handle, inode, map, &path, flags, |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4380 | allocated, newblock); |
Eric Whitney | ce37c42 | 2014-02-19 18:52:39 -0500 | [diff] [blame] | 4381 | if (ret < 0) |
| 4382 | err = ret; |
| 4383 | else |
| 4384 | allocated = ret; |
Eric Whitney | 31cf0f2 | 2014-03-13 23:14:46 -0400 | [diff] [blame] | 4385 | goto out2; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4390 | * requested block isn't allocated yet; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4391 | * we couldn't try to create block if create flag is zero |
| 4392 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 4393 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4394 | ext4_lblk_t hole_start, hole_len; |
| 4395 | |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4396 | hole_start = map->m_lblk; |
| 4397 | hole_len = ext4_ext_determine_hole(inode, path, &hole_start); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4398 | /* |
| 4399 | * put just found gap into cache to speed up |
| 4400 | * subsequent requests |
| 4401 | */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4402 | ext4_ext_put_gap_in_cache(inode, hole_start, hole_len); |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4403 | |
| 4404 | /* Update hole_len to reflect hole size after map->m_lblk */ |
| 4405 | if (hole_start != map->m_lblk) |
| 4406 | hole_len -= map->m_lblk - hole_start; |
| 4407 | map->m_pblk = 0; |
| 4408 | map->m_len = min_t(unsigned int, map->m_len, hole_len); |
| 4409 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4410 | goto out2; |
| 4411 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4412 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4413 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4414 | * Okay, we need to do block allocation. |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 4415 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4416 | newex.ee_block = cpu_to_le32(map->m_lblk); |
Eric Whitney | d0abafa | 2014-01-06 14:00:23 -0500 | [diff] [blame] | 4417 | cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4418 | |
| 4419 | /* |
| 4420 | * If we are doing bigalloc, check to see if the extent returned |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4421 | * by ext4_find_extent() implies a cluster we can use. |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4422 | */ |
| 4423 | if (cluster_offset && ex && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4424 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4425 | ar.len = allocated = map->m_len; |
| 4426 | newblock = map->m_pblk; |
Jan Kara | cbd7584 | 2014-11-25 11:41:49 -0500 | [diff] [blame] | 4427 | map_from_cluster = true; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4428 | goto got_allocated_blocks; |
| 4429 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4430 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4431 | /* find neighbour allocated blocks */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4432 | ar.lleft = map->m_lblk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4433 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 4434 | if (err) |
| 4435 | goto out2; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4436 | ar.lright = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4437 | ex2 = NULL; |
| 4438 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4439 | if (err) |
| 4440 | goto out2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4441 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4442 | /* Check if the extent after searching to the right implies a |
| 4443 | * cluster we can use. */ |
| 4444 | if ((sbi->s_cluster_ratio > 1) && ex2 && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4445 | get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4446 | ar.len = allocated = map->m_len; |
| 4447 | newblock = map->m_pblk; |
Jan Kara | cbd7584 | 2014-11-25 11:41:49 -0500 | [diff] [blame] | 4448 | map_from_cluster = true; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4449 | goto got_allocated_blocks; |
| 4450 | } |
| 4451 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4452 | /* |
| 4453 | * See if request is beyond maximum number of blocks we can have in |
| 4454 | * a single extent. For an initialized extent this limit is |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4455 | * EXT_INIT_MAX_LEN and for an unwritten extent this limit is |
| 4456 | * EXT_UNWRITTEN_MAX_LEN. |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4457 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4458 | if (map->m_len > EXT_INIT_MAX_LEN && |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4459 | !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4460 | map->m_len = EXT_INIT_MAX_LEN; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4461 | else if (map->m_len > EXT_UNWRITTEN_MAX_LEN && |
| 4462 | (flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
| 4463 | map->m_len = EXT_UNWRITTEN_MAX_LEN; |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4464 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4465 | /* 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] | 4466 | newex.ee_len = cpu_to_le16(map->m_len); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4467 | err = ext4_ext_check_overlap(sbi, inode, &newex, path); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4468 | if (err) |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4469 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4470 | else |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4471 | allocated = map->m_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4472 | |
| 4473 | /* allocate new block */ |
| 4474 | ar.inode = inode; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4475 | ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); |
| 4476 | ar.logical = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4477 | /* |
| 4478 | * We calculate the offset from the beginning of the cluster |
| 4479 | * for the logical block number, since when we allocate a |
| 4480 | * physical cluster, the physical block should start at the |
| 4481 | * same offset from the beginning of the cluster. This is |
| 4482 | * needed so that future calls to get_implied_cluster_alloc() |
| 4483 | * work correctly. |
| 4484 | */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4485 | offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4486 | ar.len = EXT4_NUM_B2C(sbi, offset+allocated); |
| 4487 | ar.goal -= offset; |
| 4488 | ar.logical -= offset; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4489 | if (S_ISREG(inode->i_mode)) |
| 4490 | ar.flags = EXT4_MB_HINT_DATA; |
| 4491 | else |
| 4492 | /* disable in-core preallocation for non-regular files */ |
| 4493 | ar.flags = 0; |
Vivek Haldar | 556b27a | 2011-05-25 07:41:54 -0400 | [diff] [blame] | 4494 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) |
| 4495 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 4496 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 4497 | ar.flags |= EXT4_MB_DELALLOC_RESERVED; |
Theodore Ts'o | c5e298a | 2015-06-21 01:25:29 -0400 | [diff] [blame] | 4498 | if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
| 4499 | ar.flags |= EXT4_MB_USE_RESERVED; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4500 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4501 | if (!newblock) |
| 4502 | goto out2; |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 4503 | ext_debug("allocate new block: goal %llu, found %llu/%u\n", |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4504 | ar.goal, newblock, allocated); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4505 | free_on_err = 1; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4506 | allocated_clusters = ar.len; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4507 | ar.len = EXT4_C2B(sbi, ar.len) - offset; |
| 4508 | if (ar.len > allocated) |
| 4509 | ar.len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4510 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4511 | got_allocated_blocks: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4512 | /* try to insert new extent into found leaf and return */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4513 | ext4_ext_store_pblock(&newex, newblock + offset); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4514 | newex.ee_len = cpu_to_le16(ar.len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4515 | /* Mark unwritten */ |
| 4516 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){ |
| 4517 | ext4_ext_mark_unwritten(&newex); |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4518 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4519 | } |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 4520 | |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4521 | err = 0; |
| 4522 | if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) |
| 4523 | err = check_eofblocks_fl(handle, inode, map->m_lblk, |
| 4524 | path, ar.len); |
Jiaying Zhang | 575a1d4 | 2011-07-10 20:07:25 -0400 | [diff] [blame] | 4525 | if (!err) |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4526 | err = ext4_ext_insert_extent(handle, inode, &path, |
Jiaying Zhang | 575a1d4 | 2011-07-10 20:07:25 -0400 | [diff] [blame] | 4527 | &newex, flags); |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 4528 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4529 | if (err && free_on_err) { |
Maxim Patlasov | 7132de7 | 2011-07-10 19:37:48 -0400 | [diff] [blame] | 4530 | int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ? |
| 4531 | EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4532 | /* free data blocks we just allocated */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4533 | /* not a good idea to call discard here directly, |
| 4534 | * but otherwise we'd need to call it every free() */ |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4535 | ext4_discard_preallocations(inode); |
Theodore Ts'o | c8e1513 | 2013-07-15 00:09:37 -0400 | [diff] [blame] | 4536 | ext4_free_blocks(handle, inode, NULL, newblock, |
| 4537 | EXT4_C2B(sbi, allocated_clusters), fb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4538 | goto out2; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4539 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4540 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4541 | /* previous routine could use block we allocated */ |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4542 | newblock = ext4_ext_pblock(&newex); |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4543 | allocated = ext4_ext_get_actual_len(&newex); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4544 | if (allocated > map->m_len) |
| 4545 | allocated = map->m_len; |
| 4546 | map->m_flags |= EXT4_MAP_NEW; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4547 | |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4548 | /* |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4549 | * Update reserved blocks/metadata blocks after successful |
| 4550 | * block allocation which had been deferred till now. |
| 4551 | */ |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4552 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4553 | unsigned int reserved_clusters; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4554 | /* |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4555 | * Check how many clusters we had reserved this allocated range |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4556 | */ |
| 4557 | reserved_clusters = get_reserved_cluster_alloc(inode, |
| 4558 | map->m_lblk, allocated); |
Eric Whitney | 9d21c9f | 2015-04-03 00:17:31 -0400 | [diff] [blame] | 4559 | if (!map_from_cluster) { |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4560 | BUG_ON(allocated_clusters < reserved_clusters); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4561 | if (reserved_clusters < allocated_clusters) { |
Aditya Kali | 5356f26 | 2011-09-09 19:20:51 -0400 | [diff] [blame] | 4562 | struct ext4_inode_info *ei = EXT4_I(inode); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4563 | int reservation = allocated_clusters - |
| 4564 | reserved_clusters; |
| 4565 | /* |
| 4566 | * It seems we claimed few clusters outside of |
| 4567 | * the range of this allocation. We should give |
| 4568 | * it back to the reservation pool. This can |
| 4569 | * happen in the following case: |
| 4570 | * |
| 4571 | * * Suppose s_cluster_ratio is 4 (i.e., each |
| 4572 | * cluster has 4 blocks. Thus, the clusters |
| 4573 | * are [0-3],[4-7],[8-11]... |
| 4574 | * * First comes delayed allocation write for |
| 4575 | * logical blocks 10 & 11. Since there were no |
| 4576 | * previous delayed allocated blocks in the |
| 4577 | * range [8-11], we would reserve 1 cluster |
| 4578 | * for this write. |
| 4579 | * * Next comes write for logical blocks 3 to 8. |
| 4580 | * In this case, we will reserve 2 clusters |
| 4581 | * (for [0-3] and [4-7]; and not for [8-11] as |
| 4582 | * that range has a delayed allocated blocks. |
| 4583 | * Thus total reserved clusters now becomes 3. |
| 4584 | * * Now, during the delayed allocation writeout |
| 4585 | * time, we will first write blocks [3-8] and |
| 4586 | * allocate 3 clusters for writing these |
| 4587 | * blocks. Also, we would claim all these |
| 4588 | * three clusters above. |
| 4589 | * * Now when we come here to writeout the |
| 4590 | * blocks [10-11], we would expect to claim |
| 4591 | * the reservation of 1 cluster we had made |
| 4592 | * (and we would claim it since there are no |
| 4593 | * more delayed allocated blocks in the range |
| 4594 | * [8-11]. But our reserved cluster count had |
| 4595 | * already gone to 0. |
| 4596 | * |
| 4597 | * Thus, at the step 4 above when we determine |
| 4598 | * that there are still some unwritten delayed |
| 4599 | * allocated blocks outside of our current |
| 4600 | * block range, we should increment the |
| 4601 | * reserved clusters count so that when the |
| 4602 | * remaining blocks finally gets written, we |
| 4603 | * could claim them. |
| 4604 | */ |
Aditya Kali | 5356f26 | 2011-09-09 19:20:51 -0400 | [diff] [blame] | 4605 | dquot_reserve_block(inode, |
| 4606 | EXT4_C2B(sbi, reservation)); |
| 4607 | spin_lock(&ei->i_block_reservation_lock); |
| 4608 | ei->i_reserved_data_blocks += reservation; |
| 4609 | spin_unlock(&ei->i_block_reservation_lock); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4610 | } |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4611 | /* |
| 4612 | * We will claim quota for all newly allocated blocks. |
| 4613 | * We're updating the reserved space *after* the |
| 4614 | * correction above so we do not accidentally free |
| 4615 | * all the metadata reservation because we might |
| 4616 | * actually need it later on. |
| 4617 | */ |
| 4618 | ext4_da_update_reserve_space(inode, allocated_clusters, |
| 4619 | 1); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4620 | } |
| 4621 | } |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4622 | |
| 4623 | /* |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4624 | * Cache the extent and update transaction to commit on fdatasync only |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4625 | * when it is _not_ an unwritten extent. |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4626 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4627 | if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4628 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4629 | else |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4630 | ext4_update_inode_fsync_trans(handle, inode, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4631 | out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4632 | if (allocated > map->m_len) |
| 4633 | allocated = map->m_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4634 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4635 | map->m_flags |= EXT4_MAP_MAPPED; |
| 4636 | map->m_pblk = newblock; |
| 4637 | map->m_len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4638 | out2: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 4639 | ext4_ext_drop_refs(path); |
| 4640 | kfree(path); |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 4641 | |
Theodore Ts'o | 63b9996 | 2013-07-16 10:28:47 -0400 | [diff] [blame] | 4642 | trace_ext4_ext_map_blocks_exit(inode, flags, map, |
| 4643 | err ? err : allocated); |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4644 | return err ? err : allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4645 | } |
| 4646 | |
Theodore Ts'o | 819c492 | 2013-04-03 12:47:17 -0400 | [diff] [blame] | 4647 | void ext4_ext_truncate(handle_t *handle, struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4648 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4649 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4650 | ext4_lblk_t last_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4651 | int err = 0; |
| 4652 | |
| 4653 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4654 | * TODO: optimization is possible here. |
| 4655 | * Probably we need not scan at all, |
| 4656 | * because page truncation is enough. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4657 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4658 | |
| 4659 | /* we have to know where to truncate from in crash case */ |
| 4660 | EXT4_I(inode)->i_disksize = inode->i_size; |
| 4661 | ext4_mark_inode_dirty(handle, inode); |
| 4662 | |
| 4663 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 4664 | >> EXT4_BLOCK_SIZE_BITS(sb); |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4665 | retry: |
Zheng Liu | 51865fd | 2012-11-08 21:57:32 -0500 | [diff] [blame] | 4666 | err = ext4_es_remove_extent(inode, last_block, |
| 4667 | EXT_MAX_BLOCKS - last_block); |
Theodore Ts'o | 94eec0f | 2013-07-29 12:12:56 -0400 | [diff] [blame] | 4668 | if (err == -ENOMEM) { |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4669 | cond_resched(); |
| 4670 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 4671 | goto retry; |
| 4672 | } |
| 4673 | if (err) { |
| 4674 | ext4_std_error(inode->i_sb, err); |
| 4675 | return; |
| 4676 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 4677 | err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4678 | ext4_std_error(inode->i_sb, err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4681 | static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset, |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4682 | ext4_lblk_t len, loff_t new_size, |
| 4683 | int flags, int mode) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4684 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4685 | struct inode *inode = file_inode(file); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4686 | handle_t *handle; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4687 | int ret = 0; |
| 4688 | int ret2 = 0; |
| 4689 | int retries = 0; |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4690 | int depth = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4691 | struct ext4_map_blocks map; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4692 | unsigned int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4693 | loff_t epos; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4694 | |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4695 | BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4696 | map.m_lblk = offset; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4697 | map.m_len = len; |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4698 | /* |
| 4699 | * Don't normalize the request if it can fit in one extent so |
| 4700 | * that it doesn't get unnecessarily split into multiple |
| 4701 | * extents. |
| 4702 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4703 | if (len <= EXT_UNWRITTEN_MAX_LEN) |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4704 | flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; |
Dmitry Monakhov | 60d4616 | 2012-10-05 11:32:02 -0400 | [diff] [blame] | 4705 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4706 | /* |
| 4707 | * credits to insert 1 extent into extent tree |
| 4708 | */ |
| 4709 | credits = ext4_chunk_trans_blocks(inode, len); |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4710 | depth = ext_depth(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4711 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4712 | retry: |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4713 | while (ret >= 0 && len) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4714 | /* |
| 4715 | * Recalculate credits when extent tree depth changes. |
| 4716 | */ |
| 4717 | if (depth >= 0 && depth != ext_depth(inode)) { |
| 4718 | credits = ext4_chunk_trans_blocks(inode, len); |
| 4719 | depth = ext_depth(inode); |
| 4720 | } |
| 4721 | |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 4722 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4723 | credits); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4724 | if (IS_ERR(handle)) { |
| 4725 | ret = PTR_ERR(handle); |
| 4726 | break; |
| 4727 | } |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4728 | ret = ext4_map_blocks(handle, inode, &map, flags); |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4729 | if (ret <= 0) { |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4730 | ext4_debug("inode #%lu: block %u: len %u: " |
| 4731 | "ext4_ext_map_blocks returned %d", |
| 4732 | inode->i_ino, map.m_lblk, |
| 4733 | map.m_len, ret); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4734 | ext4_mark_inode_dirty(handle, inode); |
| 4735 | ret2 = ext4_journal_stop(handle); |
| 4736 | break; |
| 4737 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4738 | map.m_lblk += ret; |
| 4739 | map.m_len = len = len - ret; |
| 4740 | epos = (loff_t)map.m_lblk << inode->i_blkbits; |
| 4741 | inode->i_ctime = ext4_current_time(inode); |
| 4742 | if (new_size) { |
| 4743 | if (epos > new_size) |
| 4744 | epos = new_size; |
| 4745 | if (ext4_update_inode_size(inode, epos) & 0x1) |
| 4746 | inode->i_mtime = inode->i_ctime; |
| 4747 | } else { |
| 4748 | if (epos > inode->i_size) |
| 4749 | ext4_set_inode_flag(inode, |
| 4750 | EXT4_INODE_EOFBLOCKS); |
| 4751 | } |
| 4752 | ext4_mark_inode_dirty(handle, inode); |
Eryu Guan | 6a851bb | 2017-12-03 22:52:51 -0500 | [diff] [blame] | 4753 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4754 | ret2 = ext4_journal_stop(handle); |
| 4755 | if (ret2) |
| 4756 | break; |
| 4757 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4758 | if (ret == -ENOSPC && |
| 4759 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 4760 | ret = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4761 | goto retry; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4762 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4763 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4764 | return ret > 0 ? ret2 : ret; |
| 4765 | } |
| 4766 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4767 | static long ext4_zero_range(struct file *file, loff_t offset, |
| 4768 | loff_t len, int mode) |
| 4769 | { |
| 4770 | struct inode *inode = file_inode(file); |
| 4771 | handle_t *handle = NULL; |
| 4772 | unsigned int max_blocks; |
| 4773 | loff_t new_size = 0; |
| 4774 | int ret = 0; |
| 4775 | int flags; |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4776 | int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4777 | int partial_begin, partial_end; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4778 | loff_t start, end; |
| 4779 | ext4_lblk_t lblk; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4780 | unsigned int blkbits = inode->i_blkbits; |
| 4781 | |
| 4782 | trace_ext4_zero_range(inode, offset, len, mode); |
| 4783 | |
jon ernst | 6c5e73d | 2014-04-18 11:50:35 -0400 | [diff] [blame] | 4784 | if (!S_ISREG(inode->i_mode)) |
| 4785 | return -EINVAL; |
| 4786 | |
Namjae Jeon | e1ee60f | 2014-05-27 12:48:55 -0400 | [diff] [blame] | 4787 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 4788 | if (ext4_should_journal_data(inode)) { |
| 4789 | ret = ext4_force_commit(inode->i_sb); |
| 4790 | if (ret) |
| 4791 | return ret; |
| 4792 | } |
| 4793 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4794 | /* |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4795 | * Round up offset. This is not fallocate, we neet to zero out |
| 4796 | * blocks, so convert interior block aligned part of the range to |
| 4797 | * unwritten and possibly manually zero out unaligned parts of the |
| 4798 | * range. |
| 4799 | */ |
| 4800 | start = round_up(offset, 1 << blkbits); |
| 4801 | end = round_down((offset + len), 1 << blkbits); |
| 4802 | |
| 4803 | if (start < offset || end > offset + len) |
| 4804 | return -EINVAL; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4805 | partial_begin = offset & ((1 << blkbits) - 1); |
| 4806 | partial_end = (offset + len) & ((1 << blkbits) - 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4807 | |
| 4808 | lblk = start >> blkbits; |
| 4809 | max_blocks = (end >> blkbits); |
| 4810 | if (max_blocks < lblk) |
| 4811 | max_blocks = 0; |
| 4812 | else |
| 4813 | max_blocks -= lblk; |
| 4814 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4815 | inode_lock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4816 | |
| 4817 | /* |
| 4818 | * Indirect files do not support unwritten extnets |
| 4819 | */ |
| 4820 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4821 | ret = -EOPNOTSUPP; |
| 4822 | goto out_mutex; |
| 4823 | } |
| 4824 | |
| 4825 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Theodore Ts'o | 9cdd4bf | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4826 | (offset + len > i_size_read(inode) || |
| 4827 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4828 | new_size = offset + len; |
| 4829 | ret = inode_newsize_ok(inode, new_size); |
| 4830 | if (ret) |
| 4831 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4832 | } |
| 4833 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4834 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
| 4835 | if (mode & FALLOC_FL_KEEP_SIZE) |
| 4836 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
| 4837 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4838 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
| 4839 | ext4_inode_block_unlocked_dio(inode); |
| 4840 | inode_dio_wait(inode); |
| 4841 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4842 | /* Preallocate the range including the unaligned edges */ |
| 4843 | if (partial_begin || partial_end) { |
| 4844 | ret = ext4_alloc_file_blocks(file, |
| 4845 | round_down(offset, 1 << blkbits) >> blkbits, |
| 4846 | (round_up((offset + len), 1 << blkbits) - |
| 4847 | round_down(offset, 1 << blkbits)) >> blkbits, |
| 4848 | new_size, flags, mode); |
| 4849 | if (ret) |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4850 | goto out_dio; |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4851 | |
| 4852 | } |
| 4853 | |
| 4854 | /* Zero range excluding the unaligned edges */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4855 | if (max_blocks > 0) { |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4856 | flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | |
| 4857 | EXT4_EX_NOCACHE); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4858 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4859 | /* |
| 4860 | * Prevent page faults from reinstantiating pages we have |
| 4861 | * released from page cache. |
| 4862 | */ |
| 4863 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4864 | ret = ext4_update_disksize_before_punch(inode, offset, len); |
| 4865 | if (ret) { |
| 4866 | up_write(&EXT4_I(inode)->i_mmap_sem); |
| 4867 | goto out_dio; |
| 4868 | } |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4869 | /* Now release the pages and zero block aligned part of pages */ |
| 4870 | truncate_pagecache_range(inode, start, end - 1); |
| 4871 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 4872 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4873 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, |
| 4874 | flags, mode); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4875 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4876 | if (ret) |
| 4877 | goto out_dio; |
| 4878 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4879 | if (!partial_begin && !partial_end) |
| 4880 | goto out_dio; |
| 4881 | |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4882 | /* |
| 4883 | * In worst case we have to writeout two nonadjacent unwritten |
| 4884 | * blocks and update the inode |
| 4885 | */ |
| 4886 | credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1; |
| 4887 | if (ext4_should_journal_data(inode)) |
| 4888 | credits += 2; |
| 4889 | handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4890 | if (IS_ERR(handle)) { |
| 4891 | ret = PTR_ERR(handle); |
| 4892 | ext4_std_error(inode->i_sb, ret); |
| 4893 | goto out_dio; |
| 4894 | } |
| 4895 | |
| 4896 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
Lukas Czerner | e5b3041 | 2014-04-01 00:59:21 -0400 | [diff] [blame] | 4897 | if (new_size) { |
Dmitry Monakhov | 4631dbf | 2014-08-23 17:48:28 -0400 | [diff] [blame] | 4898 | ext4_update_inode_size(inode, new_size); |
Lukas Czerner | e5b3041 | 2014-04-01 00:59:21 -0400 | [diff] [blame] | 4899 | } else { |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4900 | /* |
| 4901 | * Mark that we allocate beyond EOF so the subsequent truncate |
| 4902 | * can proceed even if the new size is the same as i_size. |
| 4903 | */ |
| 4904 | if ((offset + len) > i_size_read(inode)) |
| 4905 | ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
| 4906 | } |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4907 | ext4_mark_inode_dirty(handle, inode); |
| 4908 | |
| 4909 | /* Zero out partial block at the edges of the range */ |
| 4910 | ret = ext4_zero_partial_blocks(handle, inode, offset, len); |
Jan Kara | c404f0d | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 4911 | if (ret >= 0) |
| 4912 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4913 | |
| 4914 | if (file->f_flags & O_SYNC) |
| 4915 | ext4_handle_sync(handle); |
| 4916 | |
| 4917 | ext4_journal_stop(handle); |
| 4918 | out_dio: |
| 4919 | ext4_inode_resume_unlocked_dio(inode); |
| 4920 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4921 | inode_unlock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4922 | return ret; |
| 4923 | } |
| 4924 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4925 | /* |
| 4926 | * preallocate space for a file. This implements ext4's fallocate file |
| 4927 | * operation, which gets called from sys_fallocate system call. |
| 4928 | * For block-mapped files, posix_fallocate should fall back to the method |
| 4929 | * of writing zeroes to the required new blocks (the same behavior which is |
| 4930 | * expected for file systems which do not support fallocate() system call). |
| 4931 | */ |
| 4932 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 4933 | { |
| 4934 | struct inode *inode = file_inode(file); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4935 | loff_t new_size = 0; |
| 4936 | unsigned int max_blocks; |
| 4937 | int ret = 0; |
| 4938 | int flags; |
| 4939 | ext4_lblk_t lblk; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4940 | unsigned int blkbits = inode->i_blkbits; |
| 4941 | |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4942 | /* |
| 4943 | * Encrypted inodes can't handle collapse range or insert |
| 4944 | * range since we would need to re-encrypt blocks with a |
| 4945 | * different IV or XTS tweak (which are based on the logical |
| 4946 | * block number). |
| 4947 | * |
| 4948 | * XXX It's not clear why zero range isn't working, but we'll |
| 4949 | * leave it disabled for encrypted inodes for now. This is a |
| 4950 | * bug we should fix.... |
| 4951 | */ |
| 4952 | if (ext4_encrypted_inode(inode) && |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4953 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE | |
| 4954 | FALLOC_FL_ZERO_RANGE))) |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4955 | return -EOPNOTSUPP; |
| 4956 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4957 | /* Return error if mode is not supported */ |
| 4958 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4959 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | |
| 4960 | FALLOC_FL_INSERT_RANGE)) |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4961 | return -EOPNOTSUPP; |
| 4962 | |
| 4963 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 4964 | return ext4_punch_hole(inode, offset, len); |
| 4965 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4966 | ret = ext4_convert_inline_data(inode); |
| 4967 | if (ret) |
| 4968 | return ret; |
| 4969 | |
Theodore Ts'o | 40c406c | 2014-04-12 22:53:53 -0400 | [diff] [blame] | 4970 | if (mode & FALLOC_FL_COLLAPSE_RANGE) |
| 4971 | return ext4_collapse_range(inode, offset, len); |
| 4972 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4973 | if (mode & FALLOC_FL_INSERT_RANGE) |
| 4974 | return ext4_insert_range(inode, offset, len); |
| 4975 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4976 | if (mode & FALLOC_FL_ZERO_RANGE) |
| 4977 | return ext4_zero_range(file, offset, len, mode); |
| 4978 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4979 | trace_ext4_fallocate_enter(inode, offset, len, mode); |
| 4980 | lblk = offset >> blkbits; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4981 | |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4982 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4983 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4984 | if (mode & FALLOC_FL_KEEP_SIZE) |
| 4985 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
| 4986 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4987 | inode_lock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4988 | |
Davide Italiano | 280227a | 2015-05-02 23:21:15 -0400 | [diff] [blame] | 4989 | /* |
| 4990 | * We only support preallocation for extent-based files only |
| 4991 | */ |
| 4992 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4993 | ret = -EOPNOTSUPP; |
| 4994 | goto out; |
| 4995 | } |
| 4996 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4997 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Theodore Ts'o | 9cdd4bf | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4998 | (offset + len > i_size_read(inode) || |
| 4999 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 5000 | new_size = offset + len; |
| 5001 | ret = inode_newsize_ok(inode, new_size); |
| 5002 | if (ret) |
| 5003 | goto out; |
| 5004 | } |
| 5005 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 5006 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
| 5007 | ext4_inode_block_unlocked_dio(inode); |
| 5008 | inode_dio_wait(inode); |
| 5009 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 5010 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, |
| 5011 | flags, mode); |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 5012 | ext4_inode_resume_unlocked_dio(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 5013 | if (ret) |
| 5014 | goto out; |
| 5015 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 5016 | if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { |
| 5017 | ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, |
| 5018 | EXT4_I(inode)->i_sync_tid); |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 5019 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 5020 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5021 | inode_unlock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 5022 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); |
| 5023 | return ret; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 5024 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5025 | |
| 5026 | /* |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5027 | * This function convert a range of blocks to written extents |
| 5028 | * The caller of this function will pass the start offset and the size. |
| 5029 | * all unwritten extents within this range will be converted to |
| 5030 | * written extents. |
| 5031 | * |
| 5032 | * This function is called from the direct IO end io call back |
| 5033 | * function, to convert the fallocated extents after IO is completed. |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 5034 | * Returns 0 on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5035 | */ |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 5036 | int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode, |
| 5037 | loff_t offset, ssize_t len) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5038 | { |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5039 | unsigned int max_blocks; |
| 5040 | int ret = 0; |
| 5041 | int ret2 = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 5042 | struct ext4_map_blocks map; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5043 | unsigned int credits, blkbits = inode->i_blkbits; |
| 5044 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 5045 | map.m_lblk = offset >> blkbits; |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 5046 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
| 5047 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5048 | /* |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 5049 | * This is somewhat ugly but the idea is clear: When transaction is |
| 5050 | * reserved, everything goes into it. Otherwise we rather start several |
| 5051 | * smaller transactions for conversion of each extent separately. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5052 | */ |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 5053 | if (handle) { |
| 5054 | handle = ext4_journal_start_reserved(handle, |
| 5055 | EXT4_HT_EXT_CONVERT); |
| 5056 | if (IS_ERR(handle)) |
| 5057 | return PTR_ERR(handle); |
| 5058 | credits = 0; |
| 5059 | } else { |
| 5060 | /* |
| 5061 | * credits to insert 1 extent into extent tree |
| 5062 | */ |
| 5063 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 5064 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5065 | while (ret >= 0 && ret < max_blocks) { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 5066 | map.m_lblk += ret; |
| 5067 | map.m_len = (max_blocks -= ret); |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 5068 | if (credits) { |
| 5069 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 5070 | credits); |
| 5071 | if (IS_ERR(handle)) { |
| 5072 | ret = PTR_ERR(handle); |
| 5073 | break; |
| 5074 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5075 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 5076 | ret = ext4_map_blocks(handle, inode, &map, |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 5077 | EXT4_GET_BLOCKS_IO_CONVERT_EXT); |
Lukas Czerner | b06acd3 | 2013-01-28 21:21:12 -0500 | [diff] [blame] | 5078 | if (ret <= 0) |
| 5079 | ext4_warning(inode->i_sb, |
| 5080 | "inode #%lu: block %u: len %u: " |
| 5081 | "ext4_ext_map_blocks returned %d", |
| 5082 | inode->i_ino, map.m_lblk, |
| 5083 | map.m_len, ret); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5084 | ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 5085 | if (credits) |
| 5086 | ret2 = ext4_journal_stop(handle); |
| 5087 | if (ret <= 0 || ret2) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5088 | break; |
| 5089 | } |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 5090 | if (!credits) |
| 5091 | ret2 = ext4_journal_stop(handle); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5092 | return ret > 0 ? ret2 : ret; |
| 5093 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 5094 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 5095 | /* |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5096 | * If newes is not existing extent (newes->ec_pblk equals zero) find |
| 5097 | * delayed extent at start of newes and update newes accordingly and |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5098 | * return start of the next delayed extent. |
| 5099 | * |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5100 | * If newes is existing extent (newes->ec_pblk is not equal zero) |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5101 | * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5102 | * extent found. Leave newes unmodified. |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5103 | */ |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5104 | static int ext4_find_delayed_extent(struct inode *inode, |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5105 | struct extent_status *newes) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5106 | { |
Zheng Liu | b3aff3e | 2012-11-08 21:57:37 -0500 | [diff] [blame] | 5107 | struct extent_status es; |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 5108 | ext4_lblk_t block, next_del; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5109 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5110 | if (newes->es_pblk == 0) { |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 5111 | ext4_es_find_delayed_extent_range(inode, newes->es_lblk, |
| 5112 | newes->es_lblk + newes->es_len - 1, &es); |
| 5113 | |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 5114 | /* |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5115 | * No extent in extent-tree contains block @newes->es_pblk, |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 5116 | * then the block may stay in 1)a hole or 2)delayed-extent. |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 5117 | */ |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 5118 | if (es.es_len == 0) |
Zheng Liu | b3aff3e | 2012-11-08 21:57:37 -0500 | [diff] [blame] | 5119 | /* A hole found. */ |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5120 | return 0; |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 5121 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5122 | if (es.es_lblk > newes->es_lblk) { |
Zheng Liu | b3aff3e | 2012-11-08 21:57:37 -0500 | [diff] [blame] | 5123 | /* A hole found. */ |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5124 | newes->es_len = min(es.es_lblk - newes->es_lblk, |
| 5125 | newes->es_len); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5126 | return 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5127 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 5128 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5129 | newes->es_len = es.es_lblk + es.es_len - newes->es_lblk; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5130 | } |
| 5131 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 5132 | block = newes->es_lblk + newes->es_len; |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 5133 | ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es); |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 5134 | if (es.es_len == 0) |
| 5135 | next_del = EXT_MAX_BLOCKS; |
| 5136 | else |
| 5137 | next_del = es.es_lblk; |
| 5138 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5139 | return next_del; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5140 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5141 | /* fiemap flags we can handle specified here */ |
| 5142 | #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) |
| 5143 | |
Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 5144 | static int ext4_xattr_fiemap(struct inode *inode, |
| 5145 | struct fiemap_extent_info *fieinfo) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5146 | { |
| 5147 | __u64 physical = 0; |
| 5148 | __u64 length; |
| 5149 | __u32 flags = FIEMAP_EXTENT_LAST; |
| 5150 | int blockbits = inode->i_sb->s_blocksize_bits; |
| 5151 | int error = 0; |
| 5152 | |
| 5153 | /* in-inode? */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 5154 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5155 | struct ext4_iloc iloc; |
| 5156 | int offset; /* offset of xattr in inode */ |
| 5157 | |
| 5158 | error = ext4_get_inode_loc(inode, &iloc); |
| 5159 | if (error) |
| 5160 | return error; |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 5161 | physical = (__u64)iloc.bh->b_blocknr << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5162 | offset = EXT4_GOOD_OLD_INODE_SIZE + |
| 5163 | EXT4_I(inode)->i_extra_isize; |
| 5164 | physical += offset; |
| 5165 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
| 5166 | flags |= FIEMAP_EXTENT_DATA_INLINE; |
Curt Wohlgemuth | fd2dd9f | 2010-04-03 17:44:16 -0400 | [diff] [blame] | 5167 | brelse(iloc.bh); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5168 | } else { /* external block */ |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 5169 | physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5170 | length = inode->i_sb->s_blocksize; |
| 5171 | } |
| 5172 | |
| 5173 | if (physical) |
| 5174 | error = fiemap_fill_next_extent(fieinfo, 0, physical, |
| 5175 | length, flags); |
| 5176 | return (error < 0 ? error : 0); |
| 5177 | } |
| 5178 | |
| 5179 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 5180 | __u64 start, __u64 len) |
| 5181 | { |
| 5182 | ext4_lblk_t start_blk; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5183 | int error = 0; |
| 5184 | |
Tao Ma | 9419198 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 5185 | if (ext4_has_inline_data(inode)) { |
| 5186 | int has_inline = 1; |
| 5187 | |
Dmitry Monakhov | d952d69 | 2014-12-02 16:11:20 -0500 | [diff] [blame] | 5188 | error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline, |
| 5189 | start, len); |
Tao Ma | 9419198 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 5190 | |
| 5191 | if (has_inline) |
| 5192 | return error; |
| 5193 | } |
| 5194 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 5195 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
| 5196 | error = ext4_ext_precache(inode); |
| 5197 | if (error) |
| 5198 | return error; |
| 5199 | } |
| 5200 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5201 | /* fallback to generic here if not in extents fmt */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 5202 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
Theodore Ts'o | ad7fefb | 2015-01-02 15:16:00 -0500 | [diff] [blame] | 5203 | return generic_block_fiemap(inode, fieinfo, start, len, |
| 5204 | ext4_get_block); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5205 | |
| 5206 | if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) |
| 5207 | return -EBADR; |
| 5208 | |
| 5209 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 5210 | error = ext4_xattr_fiemap(inode, fieinfo); |
| 5211 | } else { |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 5212 | ext4_lblk_t len_blks; |
| 5213 | __u64 last_blk; |
| 5214 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5215 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 5216 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 5217 | if (last_blk >= EXT_MAX_BLOCKS) |
| 5218 | last_blk = EXT_MAX_BLOCKS-1; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 5219 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5220 | |
| 5221 | /* |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5222 | * Walk the extent tree gathering extent information |
| 5223 | * and pushing extents back to the user. |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5224 | */ |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5225 | error = ext4_fill_fiemap_extents(inode, start_blk, |
| 5226 | len_blks, fieinfo); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5227 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5228 | return error; |
| 5229 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5230 | |
| 5231 | /* |
| 5232 | * ext4_access_path: |
| 5233 | * Function to access the path buffer for marking it dirty. |
| 5234 | * It also checks if there are sufficient credits left in the journal handle |
| 5235 | * to update path. |
| 5236 | */ |
| 5237 | static int |
| 5238 | ext4_access_path(handle_t *handle, struct inode *inode, |
| 5239 | struct ext4_ext_path *path) |
| 5240 | { |
| 5241 | int credits, err; |
| 5242 | |
| 5243 | if (!ext4_handle_valid(handle)) |
| 5244 | return 0; |
| 5245 | |
| 5246 | /* |
| 5247 | * Check if need to extend journal credits |
| 5248 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 5249 | * descriptor) for each block group; assume two block |
| 5250 | * groups |
| 5251 | */ |
| 5252 | if (handle->h_buffer_credits < 7) { |
| 5253 | credits = ext4_writepage_trans_blocks(inode); |
| 5254 | err = ext4_ext_truncate_extend_restart(handle, inode, credits); |
| 5255 | /* EAGAIN is success */ |
| 5256 | if (err && err != -EAGAIN) |
| 5257 | return err; |
| 5258 | } |
| 5259 | |
| 5260 | err = ext4_ext_get_access(handle, inode, path); |
| 5261 | return err; |
| 5262 | } |
| 5263 | |
| 5264 | /* |
| 5265 | * ext4_ext_shift_path_extents: |
| 5266 | * Shift the extents of a path structure lying between path[depth].p_ext |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5267 | * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells |
| 5268 | * if it is right shift or left shift operation. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5269 | */ |
| 5270 | static int |
| 5271 | ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift, |
| 5272 | struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5273 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5274 | { |
| 5275 | int depth, err = 0; |
| 5276 | struct ext4_extent *ex_start, *ex_last; |
| 5277 | bool update = 0; |
| 5278 | depth = path->p_depth; |
| 5279 | |
| 5280 | while (depth >= 0) { |
| 5281 | if (depth == path->p_depth) { |
| 5282 | ex_start = path[depth].p_ext; |
| 5283 | if (!ex_start) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 5284 | return -EFSCORRUPTED; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5285 | |
| 5286 | ex_last = EXT_LAST_EXTENT(path[depth].p_hdr); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5287 | |
| 5288 | err = ext4_access_path(handle, inode, path + depth); |
| 5289 | if (err) |
| 5290 | goto out; |
| 5291 | |
| 5292 | if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) |
| 5293 | update = 1; |
| 5294 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5295 | while (ex_start <= ex_last) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5296 | if (SHIFT == SHIFT_LEFT) { |
| 5297 | le32_add_cpu(&ex_start->ee_block, |
| 5298 | -shift); |
| 5299 | /* Try to merge to the left. */ |
| 5300 | if ((ex_start > |
| 5301 | EXT_FIRST_EXTENT(path[depth].p_hdr)) |
| 5302 | && |
| 5303 | ext4_ext_try_to_merge_right(inode, |
| 5304 | path, ex_start - 1)) |
| 5305 | ex_last--; |
| 5306 | else |
| 5307 | ex_start++; |
| 5308 | } else { |
| 5309 | le32_add_cpu(&ex_last->ee_block, shift); |
| 5310 | ext4_ext_try_to_merge_right(inode, path, |
| 5311 | ex_last); |
Lukas Czerner | 6dd834e | 2014-04-18 10:55:24 -0400 | [diff] [blame] | 5312 | ex_last--; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5313 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5314 | } |
| 5315 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5316 | if (err) |
| 5317 | goto out; |
| 5318 | |
| 5319 | if (--depth < 0 || !update) |
| 5320 | break; |
| 5321 | } |
| 5322 | |
| 5323 | /* Update index too */ |
| 5324 | err = ext4_access_path(handle, inode, path + depth); |
| 5325 | if (err) |
| 5326 | goto out; |
| 5327 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5328 | if (SHIFT == SHIFT_LEFT) |
| 5329 | le32_add_cpu(&path[depth].p_idx->ei_block, -shift); |
| 5330 | else |
| 5331 | le32_add_cpu(&path[depth].p_idx->ei_block, shift); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5332 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5333 | if (err) |
| 5334 | goto out; |
| 5335 | |
| 5336 | /* we are done if current index is not a starting index */ |
| 5337 | if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr)) |
| 5338 | break; |
| 5339 | |
| 5340 | depth--; |
| 5341 | } |
| 5342 | |
| 5343 | out: |
| 5344 | return err; |
| 5345 | } |
| 5346 | |
| 5347 | /* |
| 5348 | * ext4_ext_shift_extents: |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5349 | * All the extents which lies in the range from @start to the last allocated |
| 5350 | * block for the @inode are shifted either towards left or right (depending |
| 5351 | * upon @SHIFT) by @shift blocks. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5352 | * On success, 0 is returned, error otherwise. |
| 5353 | */ |
| 5354 | static int |
| 5355 | ext4_ext_shift_extents(struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5356 | ext4_lblk_t start, ext4_lblk_t shift, |
| 5357 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5358 | { |
| 5359 | struct ext4_ext_path *path; |
| 5360 | int ret = 0, depth; |
| 5361 | struct ext4_extent *extent; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5362 | ext4_lblk_t stop, *iterator, ex_start, ex_end; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5363 | |
| 5364 | /* Let path point to the last extent */ |
Roman Pen | e0b53d6 | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5365 | path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, |
| 5366 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5367 | if (IS_ERR(path)) |
| 5368 | return PTR_ERR(path); |
| 5369 | |
| 5370 | depth = path->p_depth; |
| 5371 | extent = path[depth].p_ext; |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5372 | if (!extent) |
| 5373 | goto out; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5374 | |
Roman Pen | 72ae476 | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5375 | stop = le32_to_cpu(extent->ee_block); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5376 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5377 | /* |
Eric Biggers | a405a2a | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5378 | * For left shifts, make sure the hole on the left is big enough to |
| 5379 | * accommodate the shift. For right shifts, make sure the last extent |
| 5380 | * won't be shifted beyond EXT_MAX_BLOCKS. |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5381 | */ |
| 5382 | if (SHIFT == SHIFT_LEFT) { |
Roman Pen | e0b53d6 | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5383 | path = ext4_find_extent(inode, start - 1, &path, |
| 5384 | EXT4_EX_NOCACHE); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5385 | if (IS_ERR(path)) |
| 5386 | return PTR_ERR(path); |
| 5387 | depth = path->p_depth; |
| 5388 | extent = path[depth].p_ext; |
| 5389 | if (extent) { |
| 5390 | ex_start = le32_to_cpu(extent->ee_block); |
| 5391 | ex_end = le32_to_cpu(extent->ee_block) + |
| 5392 | ext4_ext_get_actual_len(extent); |
| 5393 | } else { |
| 5394 | ex_start = 0; |
| 5395 | ex_end = 0; |
| 5396 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5397 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5398 | if ((start == ex_start && shift > ex_start) || |
| 5399 | (shift > start - ex_end)) { |
Eric Biggers | a405a2a | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5400 | ret = -EINVAL; |
| 5401 | goto out; |
| 5402 | } |
| 5403 | } else { |
| 5404 | if (shift > EXT_MAX_BLOCKS - |
| 5405 | (stop + ext4_ext_get_actual_len(extent))) { |
| 5406 | ret = -EINVAL; |
| 5407 | goto out; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5408 | } |
Dmitry Monakhov | 8dc79ec | 2014-04-13 15:05:42 -0400 | [diff] [blame] | 5409 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5410 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5411 | /* |
| 5412 | * In case of left shift, iterator points to start and it is increased |
| 5413 | * till we reach stop. In case of right shift, iterator points to stop |
| 5414 | * and it is decreased till we reach start. |
| 5415 | */ |
| 5416 | if (SHIFT == SHIFT_LEFT) |
| 5417 | iterator = &start; |
| 5418 | else |
| 5419 | iterator = &stop; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5420 | |
Roman Pen | 72ae476 | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5421 | /* |
| 5422 | * Its safe to start updating extents. Start and stop are unsigned, so |
| 5423 | * in case of right shift if extent with 0 block is reached, iterator |
| 5424 | * becomes NULL to indicate the end of the loop. |
| 5425 | */ |
| 5426 | while (iterator && start <= stop) { |
Roman Pen | e0b53d6 | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5427 | path = ext4_find_extent(inode, *iterator, &path, |
| 5428 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5429 | if (IS_ERR(path)) |
| 5430 | return PTR_ERR(path); |
| 5431 | depth = path->p_depth; |
| 5432 | extent = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5433 | if (!extent) { |
| 5434 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5435 | (unsigned long) *iterator); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 5436 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5437 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5438 | if (SHIFT == SHIFT_LEFT && *iterator > |
| 5439 | le32_to_cpu(extent->ee_block)) { |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5440 | /* Hole, move to the next extent */ |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5441 | if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 5442 | path[depth].p_ext++; |
| 5443 | } else { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5444 | *iterator = ext4_ext_next_allocated_block(path); |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5445 | continue; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5446 | } |
| 5447 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5448 | |
| 5449 | if (SHIFT == SHIFT_LEFT) { |
| 5450 | extent = EXT_LAST_EXTENT(path[depth].p_hdr); |
| 5451 | *iterator = le32_to_cpu(extent->ee_block) + |
| 5452 | ext4_ext_get_actual_len(extent); |
| 5453 | } else { |
| 5454 | extent = EXT_FIRST_EXTENT(path[depth].p_hdr); |
Roman Pen | 72ae476 | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5455 | if (le32_to_cpu(extent->ee_block) > 0) |
| 5456 | *iterator = le32_to_cpu(extent->ee_block) - 1; |
| 5457 | else |
| 5458 | /* Beginning is reached, end of the loop */ |
| 5459 | iterator = NULL; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5460 | /* Update path extent in case we need to stop */ |
| 5461 | while (le32_to_cpu(extent->ee_block) < start) |
| 5462 | extent++; |
| 5463 | path[depth].p_ext = extent; |
| 5464 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5465 | ret = ext4_ext_shift_path_extents(path, shift, inode, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5466 | handle, SHIFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5467 | if (ret) |
| 5468 | break; |
| 5469 | } |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5470 | out: |
| 5471 | ext4_ext_drop_refs(path); |
| 5472 | kfree(path); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5473 | return ret; |
| 5474 | } |
| 5475 | |
| 5476 | /* |
| 5477 | * ext4_collapse_range: |
| 5478 | * This implements the fallocate's collapse range functionality for ext4 |
| 5479 | * Returns: 0 and non-zero on error. |
| 5480 | */ |
| 5481 | int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len) |
| 5482 | { |
| 5483 | struct super_block *sb = inode->i_sb; |
| 5484 | ext4_lblk_t punch_start, punch_stop; |
| 5485 | handle_t *handle; |
| 5486 | unsigned int credits; |
Namjae Jeon | a8680e0 | 2014-04-19 16:37:31 -0400 | [diff] [blame] | 5487 | loff_t new_size, ioffset; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5488 | int ret; |
| 5489 | |
Theodore Ts'o | b9576fc | 2015-05-15 00:24:10 -0400 | [diff] [blame] | 5490 | /* |
| 5491 | * We need to test this early because xfstests assumes that a |
| 5492 | * collapse range of (0, 1) will return EOPNOTSUPP if the file |
| 5493 | * system does not support collapse range. |
| 5494 | */ |
| 5495 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5496 | return -EOPNOTSUPP; |
| 5497 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5498 | /* Collapse range works only on fs block size aligned offsets. */ |
Namjae Jeon | ee98fa3 | 2014-07-29 10:45:31 -0400 | [diff] [blame] | 5499 | if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) || |
| 5500 | len & (EXT4_CLUSTER_SIZE(sb) - 1)) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5501 | return -EINVAL; |
| 5502 | |
| 5503 | if (!S_ISREG(inode->i_mode)) |
Theodore Ts'o | 86f1ca3 | 2014-04-18 11:52:11 -0400 | [diff] [blame] | 5504 | return -EINVAL; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5505 | |
| 5506 | trace_ext4_collapse_range(inode, offset, len); |
| 5507 | |
| 5508 | punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5509 | punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5510 | |
Namjae Jeon | 1ce01c4 | 2014-04-10 22:58:20 -0400 | [diff] [blame] | 5511 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 5512 | if (ext4_should_journal_data(inode)) { |
| 5513 | ret = ext4_force_commit(inode->i_sb); |
| 5514 | if (ret) |
| 5515 | return ret; |
| 5516 | } |
| 5517 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5518 | inode_lock(inode); |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5519 | /* |
| 5520 | * There is no need to overlap collapse range with EOF, in which case |
| 5521 | * it is effectively a truncate operation |
| 5522 | */ |
| 5523 | if (offset + len >= i_size_read(inode)) { |
| 5524 | ret = -EINVAL; |
| 5525 | goto out_mutex; |
| 5526 | } |
| 5527 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5528 | /* Currently just for extent based files */ |
| 5529 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5530 | ret = -EOPNOTSUPP; |
| 5531 | goto out_mutex; |
| 5532 | } |
| 5533 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5534 | /* Wait for existing dio to complete */ |
| 5535 | ext4_inode_block_unlocked_dio(inode); |
| 5536 | inode_dio_wait(inode); |
| 5537 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5538 | /* |
| 5539 | * Prevent page faults from reinstantiating pages we have released from |
| 5540 | * page cache. |
| 5541 | */ |
| 5542 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5543 | /* |
| 5544 | * Need to round down offset to be aligned with page size boundary |
| 5545 | * for page size > block size. |
| 5546 | */ |
| 5547 | ioffset = round_down(offset, PAGE_SIZE); |
| 5548 | /* |
| 5549 | * Write tail of the last page before removed range since it will get |
| 5550 | * removed from the page cache below. |
| 5551 | */ |
| 5552 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset); |
| 5553 | if (ret) |
| 5554 | goto out_mmap; |
| 5555 | /* |
| 5556 | * Write data that will be shifted to preserve them when discarding |
| 5557 | * page cache below. We are also protected from pages becoming dirty |
| 5558 | * by i_mmap_sem. |
| 5559 | */ |
| 5560 | ret = filemap_write_and_wait_range(inode->i_mapping, offset + len, |
| 5561 | LLONG_MAX); |
| 5562 | if (ret) |
| 5563 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5564 | truncate_pagecache(inode, ioffset); |
| 5565 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5566 | credits = ext4_writepage_trans_blocks(inode); |
| 5567 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5568 | if (IS_ERR(handle)) { |
| 5569 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5570 | goto out_mmap; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5574 | ext4_discard_preallocations(inode); |
| 5575 | |
| 5576 | ret = ext4_es_remove_extent(inode, punch_start, |
Lukas Czerner | 2c1d232 | 2014-04-18 10:43:21 -0400 | [diff] [blame] | 5577 | EXT_MAX_BLOCKS - punch_start); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5578 | if (ret) { |
| 5579 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5580 | goto out_stop; |
| 5581 | } |
| 5582 | |
| 5583 | ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1); |
| 5584 | if (ret) { |
| 5585 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5586 | goto out_stop; |
| 5587 | } |
Lukas Czerner | ef24f6c | 2014-04-18 10:50:23 -0400 | [diff] [blame] | 5588 | ext4_discard_preallocations(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5589 | |
| 5590 | ret = ext4_ext_shift_extents(inode, handle, punch_stop, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5591 | punch_stop - punch_start, SHIFT_LEFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5592 | if (ret) { |
| 5593 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5594 | goto out_stop; |
| 5595 | } |
| 5596 | |
| 5597 | new_size = i_size_read(inode) - len; |
Lukas Czerner | 9337d5d | 2014-04-18 10:48:25 -0400 | [diff] [blame] | 5598 | i_size_write(inode, new_size); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5599 | EXT4_I(inode)->i_disksize = new_size; |
| 5600 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5601 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5602 | if (IS_SYNC(inode)) |
| 5603 | ext4_handle_sync(handle); |
| 5604 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 5605 | ext4_mark_inode_dirty(handle, inode); |
Jan Kara | c404f0d | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5606 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5607 | |
| 5608 | out_stop: |
| 5609 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5610 | out_mmap: |
| 5611 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5612 | ext4_inode_resume_unlocked_dio(inode); |
| 5613 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5614 | inode_unlock(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5615 | return ret; |
| 5616 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5617 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5618 | /* |
| 5619 | * ext4_insert_range: |
| 5620 | * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate. |
| 5621 | * The data blocks starting from @offset to the EOF are shifted by @len |
| 5622 | * towards right to create a hole in the @inode. Inode size is increased |
| 5623 | * by len bytes. |
| 5624 | * Returns 0 on success, error otherwise. |
| 5625 | */ |
| 5626 | int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len) |
| 5627 | { |
| 5628 | struct super_block *sb = inode->i_sb; |
| 5629 | handle_t *handle; |
| 5630 | struct ext4_ext_path *path; |
| 5631 | struct ext4_extent *extent; |
| 5632 | ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0; |
| 5633 | unsigned int credits, ee_len; |
| 5634 | int ret = 0, depth, split_flag = 0; |
| 5635 | loff_t ioffset; |
| 5636 | |
| 5637 | /* |
| 5638 | * We need to test this early because xfstests assumes that an |
| 5639 | * insert range of (0, 1) will return EOPNOTSUPP if the file |
| 5640 | * system does not support insert range. |
| 5641 | */ |
| 5642 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5643 | return -EOPNOTSUPP; |
| 5644 | |
| 5645 | /* Insert range works only on fs block size aligned offsets. */ |
| 5646 | if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) || |
| 5647 | len & (EXT4_CLUSTER_SIZE(sb) - 1)) |
| 5648 | return -EINVAL; |
| 5649 | |
| 5650 | if (!S_ISREG(inode->i_mode)) |
| 5651 | return -EOPNOTSUPP; |
| 5652 | |
| 5653 | trace_ext4_insert_range(inode, offset, len); |
| 5654 | |
| 5655 | offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5656 | len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5657 | |
| 5658 | /* Call ext4_force_commit to flush all data in case of data=journal */ |
| 5659 | if (ext4_should_journal_data(inode)) { |
| 5660 | ret = ext4_force_commit(inode->i_sb); |
| 5661 | if (ret) |
| 5662 | return ret; |
| 5663 | } |
| 5664 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5665 | inode_lock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5666 | /* Currently just for extent based files */ |
| 5667 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5668 | ret = -EOPNOTSUPP; |
| 5669 | goto out_mutex; |
| 5670 | } |
| 5671 | |
| 5672 | /* Check for wrap through zero */ |
| 5673 | if (inode->i_size + len > inode->i_sb->s_maxbytes) { |
| 5674 | ret = -EFBIG; |
| 5675 | goto out_mutex; |
| 5676 | } |
| 5677 | |
| 5678 | /* Offset should be less than i_size */ |
| 5679 | if (offset >= i_size_read(inode)) { |
| 5680 | ret = -EINVAL; |
| 5681 | goto out_mutex; |
| 5682 | } |
| 5683 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5684 | /* Wait for existing dio to complete */ |
| 5685 | ext4_inode_block_unlocked_dio(inode); |
| 5686 | inode_dio_wait(inode); |
| 5687 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5688 | /* |
| 5689 | * Prevent page faults from reinstantiating pages we have released from |
| 5690 | * page cache. |
| 5691 | */ |
| 5692 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5693 | /* |
| 5694 | * Need to round down to align start offset to page size boundary |
| 5695 | * for page size > block size. |
| 5696 | */ |
| 5697 | ioffset = round_down(offset, PAGE_SIZE); |
| 5698 | /* Write out all dirty pages */ |
| 5699 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, |
| 5700 | LLONG_MAX); |
| 5701 | if (ret) |
| 5702 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5703 | truncate_pagecache(inode, ioffset); |
| 5704 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5705 | credits = ext4_writepage_trans_blocks(inode); |
| 5706 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5707 | if (IS_ERR(handle)) { |
| 5708 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5709 | goto out_mmap; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | /* Expand file to avoid data loss if there is error while shifting */ |
| 5713 | inode->i_size += len; |
| 5714 | EXT4_I(inode)->i_disksize += len; |
| 5715 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
| 5716 | ret = ext4_mark_inode_dirty(handle, inode); |
| 5717 | if (ret) |
| 5718 | goto out_stop; |
| 5719 | |
| 5720 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5721 | ext4_discard_preallocations(inode); |
| 5722 | |
| 5723 | path = ext4_find_extent(inode, offset_lblk, NULL, 0); |
| 5724 | if (IS_ERR(path)) { |
| 5725 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5726 | goto out_stop; |
| 5727 | } |
| 5728 | |
| 5729 | depth = ext_depth(inode); |
| 5730 | extent = path[depth].p_ext; |
| 5731 | if (extent) { |
| 5732 | ee_start_lblk = le32_to_cpu(extent->ee_block); |
| 5733 | ee_len = ext4_ext_get_actual_len(extent); |
| 5734 | |
| 5735 | /* |
| 5736 | * If offset_lblk is not the starting block of extent, split |
| 5737 | * the extent @offset_lblk |
| 5738 | */ |
| 5739 | if ((offset_lblk > ee_start_lblk) && |
| 5740 | (offset_lblk < (ee_start_lblk + ee_len))) { |
| 5741 | if (ext4_ext_is_unwritten(extent)) |
| 5742 | split_flag = EXT4_EXT_MARK_UNWRIT1 | |
| 5743 | EXT4_EXT_MARK_UNWRIT2; |
| 5744 | ret = ext4_split_extent_at(handle, inode, &path, |
| 5745 | offset_lblk, split_flag, |
| 5746 | EXT4_EX_NOCACHE | |
| 5747 | EXT4_GET_BLOCKS_PRE_IO | |
| 5748 | EXT4_GET_BLOCKS_METADATA_NOFAIL); |
| 5749 | } |
| 5750 | |
| 5751 | ext4_ext_drop_refs(path); |
| 5752 | kfree(path); |
| 5753 | if (ret < 0) { |
| 5754 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5755 | goto out_stop; |
| 5756 | } |
Fabian Frederick | edf15aa | 2016-09-15 11:39:52 -0400 | [diff] [blame] | 5757 | } else { |
| 5758 | ext4_ext_drop_refs(path); |
| 5759 | kfree(path); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | ret = ext4_es_remove_extent(inode, offset_lblk, |
| 5763 | EXT_MAX_BLOCKS - offset_lblk); |
| 5764 | if (ret) { |
| 5765 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5766 | goto out_stop; |
| 5767 | } |
| 5768 | |
| 5769 | /* |
| 5770 | * if offset_lblk lies in a hole which is at start of file, use |
| 5771 | * ee_start_lblk to shift extents |
| 5772 | */ |
| 5773 | ret = ext4_ext_shift_extents(inode, handle, |
| 5774 | ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk, |
| 5775 | len_lblk, SHIFT_RIGHT); |
| 5776 | |
| 5777 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5778 | if (IS_SYNC(inode)) |
| 5779 | ext4_handle_sync(handle); |
Jan Kara | c404f0d | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5780 | if (ret >= 0) |
| 5781 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5782 | |
| 5783 | out_stop: |
| 5784 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5785 | out_mmap: |
| 5786 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5787 | ext4_inode_resume_unlocked_dio(inode); |
| 5788 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5789 | inode_unlock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5790 | return ret; |
| 5791 | } |
| 5792 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5793 | /** |
| 5794 | * ext4_swap_extents - Swap extents between two inodes |
| 5795 | * |
| 5796 | * @inode1: First inode |
| 5797 | * @inode2: Second inode |
| 5798 | * @lblk1: Start block for first inode |
| 5799 | * @lblk2: Start block for second inode |
| 5800 | * @count: Number of blocks to swap |
| 5801 | * @mark_unwritten: Mark second inode's extents as unwritten after swap |
| 5802 | * @erp: Pointer to save error value |
| 5803 | * |
| 5804 | * This helper routine does exactly what is promise "swap extents". All other |
| 5805 | * stuff such as page-cache locking consistency, bh mapping consistency or |
| 5806 | * extent's data copying must be performed by caller. |
| 5807 | * Locking: |
| 5808 | * i_mutex is held for both inodes |
| 5809 | * i_data_sem is locked for write for both inodes |
| 5810 | * Assumptions: |
| 5811 | * All pages from requested range are locked for both inodes |
| 5812 | */ |
| 5813 | int |
| 5814 | ext4_swap_extents(handle_t *handle, struct inode *inode1, |
| 5815 | struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2, |
| 5816 | ext4_lblk_t count, int unwritten, int *erp) |
| 5817 | { |
| 5818 | struct ext4_ext_path *path1 = NULL; |
| 5819 | struct ext4_ext_path *path2 = NULL; |
| 5820 | int replaced_count = 0; |
| 5821 | |
| 5822 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); |
| 5823 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5824 | BUG_ON(!inode_is_locked(inode1)); |
| 5825 | BUG_ON(!inode_is_locked(inode2)); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5826 | |
| 5827 | *erp = ext4_es_remove_extent(inode1, lblk1, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5828 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5829 | return 0; |
| 5830 | *erp = ext4_es_remove_extent(inode2, lblk2, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5831 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5832 | return 0; |
| 5833 | |
| 5834 | while (count) { |
| 5835 | struct ext4_extent *ex1, *ex2, tmp_ex; |
| 5836 | ext4_lblk_t e1_blk, e2_blk; |
| 5837 | int e1_len, e2_len, len; |
| 5838 | int split = 0; |
| 5839 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5840 | path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5841 | if (IS_ERR(path1)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5842 | *erp = PTR_ERR(path1); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5843 | path1 = NULL; |
| 5844 | finish: |
| 5845 | count = 0; |
| 5846 | goto repeat; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5847 | } |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5848 | path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5849 | if (IS_ERR(path2)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5850 | *erp = PTR_ERR(path2); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5851 | path2 = NULL; |
| 5852 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5853 | } |
| 5854 | ex1 = path1[path1->p_depth].p_ext; |
| 5855 | ex2 = path2[path2->p_depth].p_ext; |
| 5856 | /* Do we have somthing to swap ? */ |
| 5857 | if (unlikely(!ex2 || !ex1)) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5858 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5859 | |
| 5860 | e1_blk = le32_to_cpu(ex1->ee_block); |
| 5861 | e2_blk = le32_to_cpu(ex2->ee_block); |
| 5862 | e1_len = ext4_ext_get_actual_len(ex1); |
| 5863 | e2_len = ext4_ext_get_actual_len(ex2); |
| 5864 | |
| 5865 | /* Hole handling */ |
| 5866 | if (!in_range(lblk1, e1_blk, e1_len) || |
| 5867 | !in_range(lblk2, e2_blk, e2_len)) { |
| 5868 | ext4_lblk_t next1, next2; |
| 5869 | |
| 5870 | /* if hole after extent, then go to next extent */ |
| 5871 | next1 = ext4_ext_next_allocated_block(path1); |
| 5872 | next2 = ext4_ext_next_allocated_block(path2); |
| 5873 | /* If hole before extent, then shift to that extent */ |
| 5874 | if (e1_blk > lblk1) |
| 5875 | next1 = e1_blk; |
| 5876 | if (e2_blk > lblk2) |
| 5877 | next2 = e1_blk; |
| 5878 | /* Do we have something to swap */ |
| 5879 | if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5880 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5881 | /* Move to the rightest boundary */ |
| 5882 | len = next1 - lblk1; |
| 5883 | if (len < next2 - lblk2) |
| 5884 | len = next2 - lblk2; |
| 5885 | if (len > count) |
| 5886 | len = count; |
| 5887 | lblk1 += len; |
| 5888 | lblk2 += len; |
| 5889 | count -= len; |
| 5890 | goto repeat; |
| 5891 | } |
| 5892 | |
| 5893 | /* Prepare left boundary */ |
| 5894 | if (e1_blk < lblk1) { |
| 5895 | split = 1; |
| 5896 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5897 | &path1, lblk1, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5898 | if (unlikely(*erp)) |
| 5899 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5900 | } |
| 5901 | if (e2_blk < lblk2) { |
| 5902 | split = 1; |
| 5903 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5904 | &path2, lblk2, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5905 | if (unlikely(*erp)) |
| 5906 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5907 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5908 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5909 | * path must to be revalidated. */ |
| 5910 | if (split) |
| 5911 | goto repeat; |
| 5912 | |
| 5913 | /* Prepare right boundary */ |
| 5914 | len = count; |
| 5915 | if (len > e1_blk + e1_len - lblk1) |
| 5916 | len = e1_blk + e1_len - lblk1; |
| 5917 | if (len > e2_blk + e2_len - lblk2) |
| 5918 | len = e2_blk + e2_len - lblk2; |
| 5919 | |
| 5920 | if (len != e1_len) { |
| 5921 | split = 1; |
| 5922 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5923 | &path1, lblk1 + len, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5924 | if (unlikely(*erp)) |
| 5925 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5926 | } |
| 5927 | if (len != e2_len) { |
| 5928 | split = 1; |
| 5929 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5930 | &path2, lblk2 + len, 0); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5931 | if (*erp) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5932 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5933 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5934 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5935 | * path must to be revalidated. */ |
| 5936 | if (split) |
| 5937 | goto repeat; |
| 5938 | |
| 5939 | BUG_ON(e2_len != e1_len); |
| 5940 | *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5941 | if (unlikely(*erp)) |
| 5942 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5943 | *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5944 | if (unlikely(*erp)) |
| 5945 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5946 | |
| 5947 | /* Both extents are fully inside boundaries. Swap it now */ |
| 5948 | tmp_ex = *ex1; |
| 5949 | ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2)); |
| 5950 | ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex)); |
| 5951 | ex1->ee_len = cpu_to_le16(e2_len); |
| 5952 | ex2->ee_len = cpu_to_le16(e1_len); |
| 5953 | if (unwritten) |
| 5954 | ext4_ext_mark_unwritten(ex2); |
| 5955 | if (ext4_ext_is_unwritten(&tmp_ex)) |
| 5956 | ext4_ext_mark_unwritten(ex1); |
| 5957 | |
| 5958 | ext4_ext_try_to_merge(handle, inode2, path2, ex2); |
| 5959 | ext4_ext_try_to_merge(handle, inode1, path1, ex1); |
| 5960 | *erp = ext4_ext_dirty(handle, inode2, path2 + |
| 5961 | path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5962 | if (unlikely(*erp)) |
| 5963 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5964 | *erp = ext4_ext_dirty(handle, inode1, path1 + |
| 5965 | path1->p_depth); |
| 5966 | /* |
| 5967 | * Looks scarry ah..? second inode already points to new blocks, |
| 5968 | * and it was successfully dirtied. But luckily error may happen |
| 5969 | * only due to journal error, so full transaction will be |
| 5970 | * aborted anyway. |
| 5971 | */ |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5972 | if (unlikely(*erp)) |
| 5973 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5974 | lblk1 += len; |
| 5975 | lblk2 += len; |
| 5976 | replaced_count += len; |
| 5977 | count -= len; |
| 5978 | |
| 5979 | repeat: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 5980 | ext4_ext_drop_refs(path1); |
| 5981 | kfree(path1); |
| 5982 | ext4_ext_drop_refs(path2); |
| 5983 | kfree(path2); |
| 5984 | path1 = path2 = NULL; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5985 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5986 | return replaced_count; |
| 5987 | } |