Theodore Ts'o | f516676 | 2017-12-17 22:00:59 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com |
| 4 | * Written by Alex Tomas <alex@clusterfs.com> |
| 5 | * |
| 6 | * Architecture independence: |
| 7 | * Copyright (c) 2005, Bull S.A. |
| 8 | * Written by Pierre Peiffer <pierre.peiffer@bull.net> |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Extents support for EXT4 |
| 13 | * |
| 14 | * TODO: |
| 15 | * - ext4*_error() should be used in some situations |
| 16 | * - analyze all BUG()/BUG_ON(), use -EIO where appropriate |
| 17 | * - smart tree reduction |
| 18 | */ |
| 19 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 20 | #include <linux/fs.h> |
| 21 | #include <linux/time.h> |
Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 22 | #include <linux/jbd2.h> |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 23 | #include <linux/highuid.h> |
| 24 | #include <linux/pagemap.h> |
| 25 | #include <linux/quotaops.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/slab.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 29 | #include <linux/fiemap.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 30 | #include <linux/backing-dev.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 31 | #include "ext4_jbd2.h" |
Theodore Ts'o | 4a092d7 | 2012-11-28 13:03:30 -0500 | [diff] [blame] | 32 | #include "ext4_extents.h" |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 33 | #include "xattr.h" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 34 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 35 | #include <trace/events/ext4.h> |
| 36 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 37 | /* |
| 38 | * used by extent splitting. |
| 39 | */ |
| 40 | #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ |
| 41 | due to ENOSPC */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 42 | #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */ |
| 43 | #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 44 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 45 | #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */ |
| 46 | #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */ |
| 47 | |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 48 | static __le32 ext4_extent_block_csum(struct inode *inode, |
| 49 | struct ext4_extent_header *eh) |
| 50 | { |
| 51 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 52 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 53 | __u32 csum; |
| 54 | |
| 55 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh, |
| 56 | EXT4_EXTENT_TAIL_OFFSET(eh)); |
| 57 | return cpu_to_le32(csum); |
| 58 | } |
| 59 | |
| 60 | static int ext4_extent_block_csum_verify(struct inode *inode, |
| 61 | struct ext4_extent_header *eh) |
| 62 | { |
| 63 | struct ext4_extent_tail *et; |
| 64 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 65 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 66 | return 1; |
| 67 | |
| 68 | et = find_ext4_extent_tail(eh); |
| 69 | if (et->et_checksum != ext4_extent_block_csum(inode, eh)) |
| 70 | return 0; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | static void ext4_extent_block_csum_set(struct inode *inode, |
| 75 | struct ext4_extent_header *eh) |
| 76 | { |
| 77 | struct ext4_extent_tail *et; |
| 78 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 79 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 80 | return; |
| 81 | |
| 82 | et = find_ext4_extent_tail(eh); |
| 83 | et->et_checksum = ext4_extent_block_csum(inode, eh); |
| 84 | } |
| 85 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 86 | static int ext4_split_extent_at(handle_t *handle, |
| 87 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 88 | struct ext4_ext_path **ppath, |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 89 | ext4_lblk_t split, |
| 90 | int split_flag, |
| 91 | int flags); |
| 92 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 93 | static int ext4_find_delayed_extent(struct inode *inode, |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 94 | struct extent_status *newes); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 95 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 96 | static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 97 | { |
Theodore Ts'o | 7b80819 | 2016-04-25 23:13:17 -0400 | [diff] [blame] | 98 | /* |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 99 | * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this |
| 100 | * moment, get_block can be called only for blocks inside i_size since |
| 101 | * page cache has been already dropped and writes are blocked by |
| 102 | * i_mutex. So we can safely drop the i_data_sem here. |
Theodore Ts'o | 7b80819 | 2016-04-25 23:13:17 -0400 | [diff] [blame] | 103 | */ |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 104 | BUG_ON(EXT4_JOURNAL(inode) == NULL); |
| 105 | ext4_discard_preallocations(inode); |
| 106 | up_write(&EXT4_I(inode)->i_data_sem); |
| 107 | *dropped = 1; |
| 108 | return 0; |
| 109 | } |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 110 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 111 | /* |
| 112 | * Make sure 'handle' has at least 'check_cred' credits. If not, restart |
| 113 | * transaction with 'restart_cred' credits. The function drops i_data_sem |
| 114 | * when restarting transaction and gets it after transaction is restarted. |
| 115 | * |
| 116 | * The function returns 0 on success, 1 if transaction had to be restarted, |
| 117 | * and < 0 in case of fatal error. |
| 118 | */ |
| 119 | int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 120 | int check_cred, int restart_cred, |
| 121 | int revoke_cred) |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 122 | { |
| 123 | int ret; |
| 124 | int dropped = 0; |
| 125 | |
| 126 | ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 127 | revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped)); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 128 | if (dropped) |
| 129 | down_write(&EXT4_I(inode)->i_data_sem); |
| 130 | return ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
| 134 | * could return: |
| 135 | * - EROFS |
| 136 | * - ENOMEM |
| 137 | */ |
| 138 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
| 139 | struct ext4_ext_path *path) |
| 140 | { |
| 141 | if (path->p_bh) { |
| 142 | /* path points to block */ |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 143 | BUFFER_TRACE(path->p_bh, "get_write_access"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 144 | return ext4_journal_get_write_access(handle, path->p_bh); |
| 145 | } |
| 146 | /* path points to leaf/index in inode body */ |
| 147 | /* we use in-core data, no need to protect them */ |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * could return: |
| 153 | * - EROFS |
| 154 | * - ENOMEM |
| 155 | * - EIO |
| 156 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 157 | static int __ext4_ext_dirty(const char *where, unsigned int line, |
| 158 | handle_t *handle, struct inode *inode, |
| 159 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 160 | { |
| 161 | int err; |
Dmitry Monakhov | 4b1f166 | 2014-07-27 22:28:15 -0400 | [diff] [blame] | 162 | |
| 163 | WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 164 | if (path->p_bh) { |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 165 | ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 166 | /* path points to block */ |
Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 167 | err = __ext4_handle_dirty_metadata(where, line, handle, |
| 168 | inode, path->p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 169 | } else { |
| 170 | /* path points to leaf/index in inode body */ |
| 171 | err = ext4_mark_inode_dirty(handle, inode); |
| 172 | } |
| 173 | return err; |
| 174 | } |
| 175 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 176 | #define ext4_ext_dirty(handle, inode, path) \ |
| 177 | __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) |
| 178 | |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 179 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 180 | struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 181 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 182 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 183 | if (path) { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 184 | int depth = path->p_depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 185 | struct ext4_extent *ex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 186 | |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 187 | /* |
| 188 | * Try to predict block placement assuming that we are |
| 189 | * filling in a file which will eventually be |
| 190 | * non-sparse --- i.e., in the case of libbfd writing |
| 191 | * an ELF object sections out-of-order but in a way |
| 192 | * the eventually results in a contiguous object or |
| 193 | * executable file, or some database extending a table |
| 194 | * space file. However, this is actually somewhat |
| 195 | * non-ideal if we are writing a sparse file such as |
| 196 | * qemu or KVM writing a raw image file that is going |
| 197 | * to stay fairly sparse, since it will end up |
| 198 | * fragmenting the file system's free space. Maybe we |
| 199 | * should have some hueristics or some way to allow |
| 200 | * userspace to pass a hint to file system, |
Tao Ma | b8d6568 | 2011-01-21 23:21:31 +0800 | [diff] [blame] | 201 | * especially if the latter case turns out to be |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 202 | * common. |
| 203 | */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 204 | ex = path[depth].p_ext; |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 205 | if (ex) { |
| 206 | ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); |
| 207 | ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); |
| 208 | |
| 209 | if (block > ext_block) |
| 210 | return ext_pblk + (block - ext_block); |
| 211 | else |
| 212 | return ext_pblk - (ext_block - block); |
| 213 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 214 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 215 | /* it looks like index is empty; |
| 216 | * try to find starting block from index itself */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 217 | if (path[depth].p_bh) |
| 218 | return path[depth].p_bh->b_blocknr; |
| 219 | } |
| 220 | |
| 221 | /* OK. use inode's group */ |
Eric Sandeen | f86186b | 2011-06-28 10:01:31 -0400 | [diff] [blame] | 222 | return ext4_inode_to_goal_block(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 225 | /* |
| 226 | * Allocation for a meta data block |
| 227 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 228 | static ext4_fsblk_t |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 229 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 230 | struct ext4_ext_path *path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 231 | struct ext4_extent *ex, int *err, unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 232 | { |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 233 | ext4_fsblk_t goal, newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 234 | |
| 235 | 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] | 236 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 237 | NULL, err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 238 | return newblock; |
| 239 | } |
| 240 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 241 | static inline int ext4_ext_space_block(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 242 | { |
| 243 | int size; |
| 244 | |
| 245 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 246 | / sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 247 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 248 | if (!check && size > 6) |
| 249 | size = 6; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 250 | #endif |
| 251 | return size; |
| 252 | } |
| 253 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 254 | 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] | 255 | { |
| 256 | int size; |
| 257 | |
| 258 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 259 | / sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 260 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 261 | if (!check && size > 5) |
| 262 | size = 5; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 263 | #endif |
| 264 | return size; |
| 265 | } |
| 266 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 267 | static inline int ext4_ext_space_root(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 268 | { |
| 269 | int size; |
| 270 | |
| 271 | size = sizeof(EXT4_I(inode)->i_data); |
| 272 | size -= sizeof(struct ext4_extent_header); |
| 273 | size /= sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 274 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 275 | if (!check && size > 3) |
| 276 | size = 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 277 | #endif |
| 278 | return size; |
| 279 | } |
| 280 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 281 | 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] | 282 | { |
| 283 | int size; |
| 284 | |
| 285 | size = sizeof(EXT4_I(inode)->i_data); |
| 286 | size -= sizeof(struct ext4_extent_header); |
| 287 | size /= sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 288 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 289 | if (!check && size > 4) |
| 290 | size = 4; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 291 | #endif |
| 292 | return size; |
| 293 | } |
| 294 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 295 | static inline int |
| 296 | ext4_force_split_extent_at(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 297 | struct ext4_ext_path **ppath, ext4_lblk_t lblk, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 298 | int nofail) |
| 299 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 300 | struct ext4_ext_path *path = *ppath; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 301 | int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext); |
| 302 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 303 | return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ? |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 304 | EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, |
| 305 | EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO | |
| 306 | (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0)); |
| 307 | } |
| 308 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 309 | static int |
| 310 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 311 | { |
| 312 | int max; |
| 313 | |
| 314 | if (depth == ext_depth(inode)) { |
| 315 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 316 | max = ext4_ext_space_root(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 317 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 318 | max = ext4_ext_space_root_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 319 | } else { |
| 320 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 321 | max = ext4_ext_space_block(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 322 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 323 | max = ext4_ext_space_block_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | return max; |
| 327 | } |
| 328 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 329 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
| 330 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 331 | ext4_fsblk_t block = ext4_ext_pblock(ext); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 332 | int len = ext4_ext_get_actual_len(ext); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 333 | ext4_lblk_t lblock = le32_to_cpu(ext->ee_block); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 334 | |
Vegard Nossum | f70749c | 2016-06-30 11:53:46 -0400 | [diff] [blame] | 335 | /* |
| 336 | * We allow neither: |
| 337 | * - zero length |
| 338 | * - overflow/wrap-around |
| 339 | */ |
| 340 | if (lblock + len <= lblock) |
Theodore Ts'o | 31d4f3a | 2012-03-11 23:30:16 -0400 | [diff] [blame] | 341 | return 0; |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 342 | 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] | 343 | } |
| 344 | |
| 345 | static int ext4_valid_extent_idx(struct inode *inode, |
| 346 | struct ext4_extent_idx *ext_idx) |
| 347 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 348 | ext4_fsblk_t block = ext4_idx_pblock(ext_idx); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 349 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 350 | 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] | 351 | } |
| 352 | |
| 353 | static int ext4_valid_extent_entries(struct inode *inode, |
| 354 | struct ext4_extent_header *eh, |
| 355 | int depth) |
| 356 | { |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 357 | unsigned short entries; |
| 358 | if (eh->eh_entries == 0) |
| 359 | return 1; |
| 360 | |
| 361 | entries = le16_to_cpu(eh->eh_entries); |
| 362 | |
| 363 | if (depth == 0) { |
| 364 | /* leaf entries */ |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 365 | struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 366 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
| 367 | ext4_fsblk_t pblock = 0; |
| 368 | ext4_lblk_t lblock = 0; |
| 369 | ext4_lblk_t prev = 0; |
| 370 | int len = 0; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 371 | while (entries) { |
| 372 | if (!ext4_valid_extent(inode, ext)) |
| 373 | return 0; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 374 | |
| 375 | /* Check for overlapping extents */ |
| 376 | lblock = le32_to_cpu(ext->ee_block); |
| 377 | len = ext4_ext_get_actual_len(ext); |
| 378 | if ((lblock <= prev) && prev) { |
| 379 | pblock = ext4_ext_pblock(ext); |
| 380 | es->s_last_error_block = cpu_to_le64(pblock); |
| 381 | return 0; |
| 382 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 383 | ext++; |
| 384 | entries--; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 385 | prev = lblock + len - 1; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 386 | } |
| 387 | } else { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 388 | struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 389 | while (entries) { |
| 390 | if (!ext4_valid_extent_idx(inode, ext_idx)) |
| 391 | return 0; |
| 392 | ext_idx++; |
| 393 | entries--; |
| 394 | } |
| 395 | } |
| 396 | return 1; |
| 397 | } |
| 398 | |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 399 | static int __ext4_ext_check(const char *function, unsigned int line, |
| 400 | struct inode *inode, struct ext4_extent_header *eh, |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 401 | int depth, ext4_fsblk_t pblk) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 402 | { |
| 403 | const char *error_msg; |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 404 | int max = 0, err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 405 | |
| 406 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { |
| 407 | error_msg = "invalid magic"; |
| 408 | goto corrupted; |
| 409 | } |
| 410 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { |
| 411 | error_msg = "unexpected eh_depth"; |
| 412 | goto corrupted; |
| 413 | } |
| 414 | if (unlikely(eh->eh_max == 0)) { |
| 415 | error_msg = "invalid eh_max"; |
| 416 | goto corrupted; |
| 417 | } |
| 418 | max = ext4_ext_max_entries(inode, depth); |
| 419 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { |
| 420 | error_msg = "too large eh_max"; |
| 421 | goto corrupted; |
| 422 | } |
| 423 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { |
| 424 | error_msg = "invalid eh_entries"; |
| 425 | goto corrupted; |
| 426 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 427 | if (!ext4_valid_extent_entries(inode, eh, depth)) { |
| 428 | error_msg = "invalid extent entries"; |
| 429 | goto corrupted; |
| 430 | } |
Vegard Nossum | 7bc9491 | 2016-07-15 00:22:07 -0400 | [diff] [blame] | 431 | if (unlikely(depth > 32)) { |
| 432 | error_msg = "too large eh_depth"; |
| 433 | goto corrupted; |
| 434 | } |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 435 | /* Verify checksum on non-root extent tree nodes */ |
| 436 | if (ext_depth(inode) != depth && |
| 437 | !ext4_extent_block_csum_verify(inode, eh)) { |
| 438 | error_msg = "extent tree corrupted"; |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 439 | err = -EFSBADCRC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 440 | goto corrupted; |
| 441 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 442 | return 0; |
| 443 | |
| 444 | corrupted: |
Theodore Ts'o | 878520a | 2019-11-19 21:54:15 -0500 | [diff] [blame] | 445 | ext4_set_errno(inode->i_sb, -err); |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 446 | ext4_error_inode(inode, function, line, 0, |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 447 | "pblk %llu bad header/extent: %s - magic %x, " |
| 448 | "entries %u, max %u(%u), depth %u(%u)", |
| 449 | (unsigned long long) pblk, error_msg, |
| 450 | le16_to_cpu(eh->eh_magic), |
| 451 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), |
| 452 | max, le16_to_cpu(eh->eh_depth), depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 453 | return err; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 454 | } |
| 455 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 456 | #define ext4_ext_check(inode, eh, depth, pblk) \ |
| 457 | __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk)) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 458 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 459 | int ext4_ext_check_inode(struct inode *inode) |
| 460 | { |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 461 | 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] | 462 | } |
| 463 | |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 464 | static void ext4_cache_extents(struct inode *inode, |
| 465 | struct ext4_extent_header *eh) |
| 466 | { |
| 467 | struct ext4_extent *ex = EXT_FIRST_EXTENT(eh); |
| 468 | ext4_lblk_t prev = 0; |
| 469 | int i; |
| 470 | |
| 471 | for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) { |
| 472 | unsigned int status = EXTENT_STATUS_WRITTEN; |
| 473 | ext4_lblk_t lblk = le32_to_cpu(ex->ee_block); |
| 474 | int len = ext4_ext_get_actual_len(ex); |
| 475 | |
| 476 | if (prev && (prev != lblk)) |
| 477 | ext4_es_cache_extent(inode, prev, lblk - prev, ~0, |
| 478 | EXTENT_STATUS_HOLE); |
| 479 | |
| 480 | if (ext4_ext_is_unwritten(ex)) |
| 481 | status = EXTENT_STATUS_UNWRITTEN; |
| 482 | ext4_es_cache_extent(inode, lblk, len, |
| 483 | ext4_ext_pblock(ex), status); |
| 484 | prev = lblk + len; |
| 485 | } |
| 486 | } |
| 487 | |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 488 | static struct buffer_head * |
| 489 | __read_extent_tree_block(const char *function, unsigned int line, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 490 | struct inode *inode, ext4_fsblk_t pblk, int depth, |
| 491 | int flags) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 492 | { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 493 | struct buffer_head *bh; |
| 494 | int err; |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 495 | |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 496 | 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] | 497 | if (unlikely(!bh)) |
| 498 | return ERR_PTR(-ENOMEM); |
| 499 | |
| 500 | if (!bh_uptodate_or_lock(bh)) { |
| 501 | trace_ext4_ext_load_extent(inode, pblk, _RET_IP_); |
| 502 | err = bh_submit_read(bh); |
| 503 | if (err < 0) |
| 504 | goto errout; |
| 505 | } |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 506 | if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE)) |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 507 | return bh; |
Theodore Ts'o | 0a944e8 | 2019-05-22 10:27:01 -0400 | [diff] [blame] | 508 | if (!ext4_has_feature_journal(inode->i_sb) || |
| 509 | (inode->i_ino != |
| 510 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) { |
| 511 | err = __ext4_ext_check(function, line, inode, |
| 512 | ext_block_hdr(bh), depth, pblk); |
| 513 | if (err) |
| 514 | goto errout; |
| 515 | } |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 516 | set_buffer_verified(bh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 517 | /* |
| 518 | * If this is a leaf block, cache all of its entries |
| 519 | */ |
| 520 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) { |
| 521 | struct ext4_extent_header *eh = ext_block_hdr(bh); |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 522 | ext4_cache_extents(inode, eh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 523 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 524 | return bh; |
| 525 | errout: |
| 526 | put_bh(bh); |
| 527 | return ERR_PTR(err); |
| 528 | |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 531 | #define read_extent_tree_block(inode, pblk, depth, flags) \ |
| 532 | __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \ |
| 533 | (depth), (flags)) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 534 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 535 | /* |
| 536 | * This function is called to cache a file's extent information in the |
| 537 | * extent status tree |
| 538 | */ |
| 539 | int ext4_ext_precache(struct inode *inode) |
| 540 | { |
| 541 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 542 | struct ext4_ext_path *path = NULL; |
| 543 | struct buffer_head *bh; |
| 544 | int i = 0, depth, ret = 0; |
| 545 | |
| 546 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 547 | return 0; /* not an extent-mapped inode */ |
| 548 | |
| 549 | down_read(&ei->i_data_sem); |
| 550 | depth = ext_depth(inode); |
| 551 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 552 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 553 | GFP_NOFS); |
| 554 | if (path == NULL) { |
| 555 | up_read(&ei->i_data_sem); |
| 556 | return -ENOMEM; |
| 557 | } |
| 558 | |
| 559 | /* Don't cache anything if there are no external extent blocks */ |
| 560 | if (depth == 0) |
| 561 | goto out; |
| 562 | path[0].p_hdr = ext_inode_hdr(inode); |
| 563 | ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0); |
| 564 | if (ret) |
| 565 | goto out; |
| 566 | path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr); |
| 567 | while (i >= 0) { |
| 568 | /* |
| 569 | * If this is a leaf block or we've reached the end of |
| 570 | * the index block, go up |
| 571 | */ |
| 572 | if ((i == depth) || |
| 573 | path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) { |
| 574 | brelse(path[i].p_bh); |
| 575 | path[i].p_bh = NULL; |
| 576 | i--; |
| 577 | continue; |
| 578 | } |
| 579 | bh = read_extent_tree_block(inode, |
| 580 | ext4_idx_pblock(path[i].p_idx++), |
| 581 | depth - i - 1, |
| 582 | EXT4_EX_FORCE_CACHE); |
| 583 | if (IS_ERR(bh)) { |
| 584 | ret = PTR_ERR(bh); |
| 585 | break; |
| 586 | } |
| 587 | i++; |
| 588 | path[i].p_bh = bh; |
| 589 | path[i].p_hdr = ext_block_hdr(bh); |
| 590 | path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr); |
| 591 | } |
| 592 | ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED); |
| 593 | out: |
| 594 | up_read(&ei->i_data_sem); |
| 595 | ext4_ext_drop_refs(path); |
| 596 | kfree(path); |
| 597 | return ret; |
| 598 | } |
| 599 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 600 | #ifdef EXT_DEBUG |
| 601 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 602 | { |
| 603 | int k, l = path->p_depth; |
| 604 | |
| 605 | ext_debug("path:"); |
| 606 | for (k = 0; k <= l; k++, path++) { |
| 607 | if (path->p_idx) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 608 | ext_debug(" %d->%llu", |
| 609 | le32_to_cpu(path->p_idx->ei_block), |
| 610 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 611 | } else if (path->p_ext) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 612 | ext_debug(" %d:[%d]%d:%llu ", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 613 | le32_to_cpu(path->p_ext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 614 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 615 | ext4_ext_get_actual_len(path->p_ext), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 616 | ext4_ext_pblock(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 617 | } else |
| 618 | ext_debug(" []"); |
| 619 | } |
| 620 | ext_debug("\n"); |
| 621 | } |
| 622 | |
| 623 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 624 | { |
| 625 | int depth = ext_depth(inode); |
| 626 | struct ext4_extent_header *eh; |
| 627 | struct ext4_extent *ex; |
| 628 | int i; |
| 629 | |
| 630 | if (!path) |
| 631 | return; |
| 632 | |
| 633 | eh = path[depth].p_hdr; |
| 634 | ex = EXT_FIRST_EXTENT(eh); |
| 635 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 636 | ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); |
| 637 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 638 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 639 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 640 | ext4_ext_is_unwritten(ex), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 641 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 642 | } |
| 643 | ext_debug("\n"); |
| 644 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 645 | |
| 646 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, |
| 647 | ext4_fsblk_t newblock, int level) |
| 648 | { |
| 649 | int depth = ext_depth(inode); |
| 650 | struct ext4_extent *ex; |
| 651 | |
| 652 | if (depth != level) { |
| 653 | struct ext4_extent_idx *idx; |
| 654 | idx = path[level].p_idx; |
| 655 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { |
| 656 | ext_debug("%d: move %d:%llu in new index %llu\n", level, |
| 657 | le32_to_cpu(idx->ei_block), |
| 658 | ext4_idx_pblock(idx), |
| 659 | newblock); |
| 660 | idx++; |
| 661 | } |
| 662 | |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | ex = path[depth].p_ext; |
| 667 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 668 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", |
| 669 | le32_to_cpu(ex->ee_block), |
| 670 | ext4_ext_pblock(ex), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 671 | ext4_ext_is_unwritten(ex), |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 672 | ext4_ext_get_actual_len(ex), |
| 673 | newblock); |
| 674 | ex++; |
| 675 | } |
| 676 | } |
| 677 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 678 | #else |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 679 | #define ext4_ext_show_path(inode, path) |
| 680 | #define ext4_ext_show_leaf(inode, path) |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 681 | #define ext4_ext_show_move(inode, path, newblock, level) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 682 | #endif |
| 683 | |
Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 684 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 685 | { |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 686 | int depth, i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 687 | |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 688 | if (!path) |
| 689 | return; |
| 690 | depth = path->p_depth; |
Eric Biggers | de74548 | 2019-12-31 12:04:44 -0600 | [diff] [blame] | 691 | for (i = 0; i <= depth; i++, path++) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 692 | if (path->p_bh) { |
| 693 | brelse(path->p_bh); |
| 694 | path->p_bh = NULL; |
| 695 | } |
Eric Biggers | de74548 | 2019-12-31 12:04:44 -0600 | [diff] [blame] | 696 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 700 | * ext4_ext_binsearch_idx: |
| 701 | * binary search for the closest index of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 702 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 703 | */ |
| 704 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 705 | ext4_ext_binsearch_idx(struct inode *inode, |
| 706 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 707 | { |
| 708 | struct ext4_extent_header *eh = path->p_hdr; |
| 709 | struct ext4_extent_idx *r, *l, *m; |
| 710 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 711 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 712 | ext_debug("binsearch for %u(idx): ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 713 | |
| 714 | l = EXT_FIRST_INDEX(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 715 | r = EXT_LAST_INDEX(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 716 | while (l <= r) { |
| 717 | m = l + (r - l) / 2; |
| 718 | if (block < le32_to_cpu(m->ei_block)) |
| 719 | r = m - 1; |
| 720 | else |
| 721 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 722 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
| 723 | m, le32_to_cpu(m->ei_block), |
| 724 | r, le32_to_cpu(r->ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | path->p_idx = l - 1; |
Zheng Liu | 4a3c3a5 | 2012-05-28 17:55:16 -0400 | [diff] [blame] | 728 | 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] | 729 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 730 | |
| 731 | #ifdef CHECK_BINSEARCH |
| 732 | { |
| 733 | struct ext4_extent_idx *chix, *ix; |
| 734 | int k; |
| 735 | |
| 736 | chix = ix = EXT_FIRST_INDEX(eh); |
| 737 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 738 | if (k != 0 && le32_to_cpu(ix->ei_block) <= |
| 739 | le32_to_cpu(ix[-1].ei_block)) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 740 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
| 741 | "first=0x%p\n", k, |
| 742 | ix, EXT_FIRST_INDEX(eh)); |
| 743 | printk(KERN_DEBUG "%u <= %u\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 744 | le32_to_cpu(ix->ei_block), |
| 745 | le32_to_cpu(ix[-1].ei_block)); |
| 746 | } |
| 747 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 748 | <= le32_to_cpu(ix[-1].ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 749 | if (block < le32_to_cpu(ix->ei_block)) |
| 750 | break; |
| 751 | chix = ix; |
| 752 | } |
| 753 | BUG_ON(chix != path->p_idx); |
| 754 | } |
| 755 | #endif |
| 756 | |
| 757 | } |
| 758 | |
| 759 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 760 | * ext4_ext_binsearch: |
| 761 | * binary search for closest extent of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 762 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 763 | */ |
| 764 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 765 | ext4_ext_binsearch(struct inode *inode, |
| 766 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 767 | { |
| 768 | struct ext4_extent_header *eh = path->p_hdr; |
| 769 | struct ext4_extent *r, *l, *m; |
| 770 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 771 | if (eh->eh_entries == 0) { |
| 772 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 773 | * this leaf is empty: |
| 774 | * we get such a leaf in split/add case |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 775 | */ |
| 776 | return; |
| 777 | } |
| 778 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 779 | ext_debug("binsearch for %u: ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 780 | |
| 781 | l = EXT_FIRST_EXTENT(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 782 | r = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 783 | |
| 784 | while (l <= r) { |
| 785 | m = l + (r - l) / 2; |
| 786 | if (block < le32_to_cpu(m->ee_block)) |
| 787 | r = m - 1; |
| 788 | else |
| 789 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 790 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
| 791 | m, le32_to_cpu(m->ee_block), |
| 792 | r, le32_to_cpu(r->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | path->p_ext = l - 1; |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 796 | ext_debug(" -> %d:%llu:[%d]%d ", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 797 | le32_to_cpu(path->p_ext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 798 | ext4_ext_pblock(path->p_ext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 799 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 800 | ext4_ext_get_actual_len(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 801 | |
| 802 | #ifdef CHECK_BINSEARCH |
| 803 | { |
| 804 | struct ext4_extent *chex, *ex; |
| 805 | int k; |
| 806 | |
| 807 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 808 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 809 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 810 | <= le32_to_cpu(ex[-1].ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 811 | if (block < le32_to_cpu(ex->ee_block)) |
| 812 | break; |
| 813 | chex = ex; |
| 814 | } |
| 815 | BUG_ON(chex != path->p_ext); |
| 816 | } |
| 817 | #endif |
| 818 | |
| 819 | } |
| 820 | |
| 821 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
| 822 | { |
| 823 | struct ext4_extent_header *eh; |
| 824 | |
| 825 | eh = ext_inode_hdr(inode); |
| 826 | eh->eh_depth = 0; |
| 827 | eh->eh_entries = 0; |
| 828 | eh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 829 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 830 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | struct ext4_ext_path * |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 835 | ext4_find_extent(struct inode *inode, ext4_lblk_t block, |
| 836 | struct ext4_ext_path **orig_path, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 837 | { |
| 838 | struct ext4_extent_header *eh; |
| 839 | struct buffer_head *bh; |
Theodore Ts'o | 705912c | 2014-09-01 14:34:09 -0400 | [diff] [blame] | 840 | struct ext4_ext_path *path = orig_path ? *orig_path : NULL; |
| 841 | short int depth, i, ppos = 0; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 842 | int ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 843 | |
| 844 | eh = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 845 | depth = ext_depth(inode); |
Theodore Ts'o | bc890a6 | 2018-06-14 12:55:10 -0400 | [diff] [blame] | 846 | if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) { |
| 847 | EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d", |
| 848 | depth); |
| 849 | ret = -EFSCORRUPTED; |
| 850 | goto err; |
| 851 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 852 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 853 | if (path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 854 | ext4_ext_drop_refs(path); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 855 | if (depth > path[0].p_maxdepth) { |
| 856 | kfree(path); |
| 857 | *orig_path = path = NULL; |
| 858 | } |
| 859 | } |
| 860 | if (!path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 861 | /* account possible depth increase */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 862 | path = kcalloc(depth + 2, sizeof(struct ext4_ext_path), |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 863 | GFP_NOFS); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 864 | if (unlikely(!path)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 865 | return ERR_PTR(-ENOMEM); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 866 | path[0].p_maxdepth = depth + 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 867 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 868 | path[0].p_hdr = eh; |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 869 | path[0].p_bh = NULL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 870 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 871 | i = depth; |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 872 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) |
| 873 | ext4_cache_extents(inode, eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 874 | /* walk through the tree */ |
| 875 | while (i) { |
| 876 | ext_debug("depth %d: num %d, max %d\n", |
| 877 | 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] | 878 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 879 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 880 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 881 | path[ppos].p_depth = i; |
| 882 | path[ppos].p_ext = NULL; |
| 883 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 884 | bh = read_extent_tree_block(inode, path[ppos].p_block, --i, |
| 885 | flags); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 886 | if (IS_ERR(bh)) { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 887 | ret = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 888 | goto err; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 889 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 890 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 891 | eh = ext_block_hdr(bh); |
| 892 | ppos++; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 893 | path[ppos].p_bh = bh; |
| 894 | path[ppos].p_hdr = eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | path[ppos].p_depth = i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 898 | path[ppos].p_ext = NULL; |
| 899 | path[ppos].p_idx = NULL; |
| 900 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 901 | /* find extent */ |
| 902 | ext4_ext_binsearch(inode, path + ppos, block); |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 903 | /* if not an empty leaf */ |
| 904 | if (path[ppos].p_ext) |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 905 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 906 | |
| 907 | ext4_ext_show_path(inode, path); |
| 908 | |
| 909 | return path; |
| 910 | |
| 911 | err: |
| 912 | ext4_ext_drop_refs(path); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 913 | kfree(path); |
| 914 | if (orig_path) |
| 915 | *orig_path = NULL; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 916 | return ERR_PTR(ret); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 920 | * ext4_ext_insert_index: |
| 921 | * insert new index [@logical;@ptr] into the block at @curp; |
| 922 | * check where to insert: before @curp or after @curp |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 923 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 924 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 925 | struct ext4_ext_path *curp, |
| 926 | int logical, ext4_fsblk_t ptr) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 927 | { |
| 928 | struct ext4_extent_idx *ix; |
| 929 | int len, err; |
| 930 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 931 | err = ext4_ext_get_access(handle, inode, curp); |
| 932 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 933 | return err; |
| 934 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 935 | if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { |
| 936 | EXT4_ERROR_INODE(inode, |
| 937 | "logical %d == ei_block %d!", |
| 938 | logical, le32_to_cpu(curp->p_idx->ei_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 939 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 940 | } |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 941 | |
| 942 | if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) |
| 943 | >= le16_to_cpu(curp->p_hdr->eh_max))) { |
| 944 | EXT4_ERROR_INODE(inode, |
| 945 | "eh_entries %d >= eh_max %d!", |
| 946 | le16_to_cpu(curp->p_hdr->eh_entries), |
| 947 | le16_to_cpu(curp->p_hdr->eh_max)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 948 | return -EFSCORRUPTED; |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 949 | } |
| 950 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 951 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 952 | /* insert after */ |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 953 | ext_debug("insert new index %d after: %llu\n", logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 954 | ix = curp->p_idx + 1; |
| 955 | } else { |
| 956 | /* insert before */ |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 957 | ext_debug("insert new index %d before: %llu\n", logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 958 | ix = curp->p_idx; |
| 959 | } |
| 960 | |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 961 | len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; |
| 962 | BUG_ON(len < 0); |
| 963 | if (len > 0) { |
| 964 | ext_debug("insert new index %d: " |
| 965 | "move %d indices from 0x%p to 0x%p\n", |
| 966 | logical, len, ix, ix + 1); |
| 967 | memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); |
| 968 | } |
| 969 | |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 970 | if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { |
| 971 | EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 972 | return -EFSCORRUPTED; |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 973 | } |
| 974 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 975 | ix->ei_block = cpu_to_le32(logical); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 976 | ext4_idx_store_pblock(ix, ptr); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 977 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 978 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 979 | if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { |
| 980 | EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 981 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 982 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 983 | |
| 984 | err = ext4_ext_dirty(handle, inode, curp); |
| 985 | ext4_std_error(inode->i_sb, err); |
| 986 | |
| 987 | return err; |
| 988 | } |
| 989 | |
| 990 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 991 | * ext4_ext_split: |
| 992 | * inserts new subtree into the path, using free index entry |
| 993 | * at depth @at: |
| 994 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 995 | * - makes decision where to split |
| 996 | * - moves remaining extents and index entries (right to the split point) |
| 997 | * into the newly allocated blocks |
| 998 | * - initializes subtree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 999 | */ |
| 1000 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1001 | unsigned int flags, |
| 1002 | struct ext4_ext_path *path, |
| 1003 | struct ext4_extent *newext, int at) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1004 | { |
| 1005 | struct buffer_head *bh = NULL; |
| 1006 | int depth = ext_depth(inode); |
| 1007 | struct ext4_extent_header *neh; |
| 1008 | struct ext4_extent_idx *fidx; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1009 | int i = at, k, m, a; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1010 | ext4_fsblk_t newblock, oldblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1011 | __le32 border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1012 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1013 | int err = 0; |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1014 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1015 | |
| 1016 | /* make decision: where to split? */ |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1017 | /* FIXME: now decision is simplest: at current extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1018 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1019 | /* if current leaf will be split, then we should use |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1020 | * border from split point */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1021 | if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { |
| 1022 | EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1023 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1024 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1025 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 1026 | border = path[depth].p_ext[1].ee_block; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1027 | ext_debug("leaf will be split." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1028 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1029 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1030 | } else { |
| 1031 | border = newext->ee_block; |
| 1032 | ext_debug("leaf will be added." |
| 1033 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1034 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1038 | * If error occurs, then we break processing |
| 1039 | * and mark filesystem read-only. index won't |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1040 | * be inserted and tree will be in consistent |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1041 | * state. Next mount will repair buffers too. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1042 | */ |
| 1043 | |
| 1044 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1045 | * Get array to track all allocated blocks. |
| 1046 | * We need this to handle errors and free blocks |
| 1047 | * upon them. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1048 | */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1049 | ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1050 | if (!ablocks) |
| 1051 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1052 | |
| 1053 | /* allocate all needed blocks */ |
| 1054 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
| 1055 | for (a = 0; a < depth - at; a++) { |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1056 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1057 | newext, &err, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1058 | if (newblock == 0) |
| 1059 | goto cleanup; |
| 1060 | ablocks[a] = newblock; |
| 1061 | } |
| 1062 | |
| 1063 | /* initialize new leaf */ |
| 1064 | newblock = ablocks[--a]; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1065 | if (unlikely(newblock == 0)) { |
| 1066 | EXT4_ERROR_INODE(inode, "newblock == 0!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1067 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1068 | goto cleanup; |
| 1069 | } |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1070 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1071 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1072 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1073 | goto cleanup; |
| 1074 | } |
| 1075 | lock_buffer(bh); |
| 1076 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1077 | err = ext4_journal_get_create_access(handle, bh); |
| 1078 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1079 | goto cleanup; |
| 1080 | |
| 1081 | neh = ext_block_hdr(bh); |
| 1082 | neh->eh_entries = 0; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1083 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1084 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 1085 | neh->eh_depth = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1086 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1087 | /* move remainder of path[depth] to the new leaf */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1088 | if (unlikely(path[depth].p_hdr->eh_entries != |
| 1089 | path[depth].p_hdr->eh_max)) { |
| 1090 | EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", |
| 1091 | path[depth].p_hdr->eh_entries, |
| 1092 | path[depth].p_hdr->eh_max); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1093 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1094 | goto cleanup; |
| 1095 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1096 | /* start copy from next extent */ |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1097 | m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; |
| 1098 | ext4_ext_show_move(inode, path, newblock, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1099 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1100 | struct ext4_extent *ex; |
| 1101 | ex = EXT_FIRST_EXTENT(neh); |
| 1102 | memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1103 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1106 | /* zero out unused area in the extent block */ |
| 1107 | ext_size = sizeof(struct ext4_extent_header) + |
| 1108 | sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries); |
| 1109 | 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] | 1110 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1111 | set_buffer_uptodate(bh); |
| 1112 | unlock_buffer(bh); |
| 1113 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1114 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1115 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1116 | goto cleanup; |
| 1117 | brelse(bh); |
| 1118 | bh = NULL; |
| 1119 | |
| 1120 | /* correct old leaf */ |
| 1121 | if (m) { |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1122 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1123 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1124 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1125 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1126 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1127 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1128 | goto cleanup; |
| 1129 | |
| 1130 | } |
| 1131 | |
| 1132 | /* create intermediate indexes */ |
| 1133 | k = depth - at - 1; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1134 | if (unlikely(k < 0)) { |
| 1135 | EXT4_ERROR_INODE(inode, "k %d < 0!", k); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1136 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1137 | goto cleanup; |
| 1138 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1139 | if (k) |
| 1140 | ext_debug("create %d intermediate indices\n", k); |
| 1141 | /* insert new index into current index block */ |
| 1142 | /* current depth stored in i var */ |
| 1143 | i = depth - 1; |
| 1144 | while (k--) { |
| 1145 | oldblock = newblock; |
| 1146 | newblock = ablocks[--a]; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1147 | bh = sb_getblk(inode->i_sb, newblock); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1148 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1149 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1150 | goto cleanup; |
| 1151 | } |
| 1152 | lock_buffer(bh); |
| 1153 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1154 | err = ext4_journal_get_create_access(handle, bh); |
| 1155 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1156 | goto cleanup; |
| 1157 | |
| 1158 | neh = ext_block_hdr(bh); |
| 1159 | neh->eh_entries = cpu_to_le16(1); |
| 1160 | neh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1161 | 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] | 1162 | neh->eh_depth = cpu_to_le16(depth - i); |
| 1163 | fidx = EXT_FIRST_INDEX(neh); |
| 1164 | fidx->ei_block = border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1165 | ext4_idx_store_pblock(fidx, oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1166 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1167 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
| 1168 | i, newblock, le32_to_cpu(border), oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1169 | |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1170 | /* move remainder of path[i] to the new index block */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1171 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != |
| 1172 | EXT_LAST_INDEX(path[i].p_hdr))) { |
| 1173 | EXT4_ERROR_INODE(inode, |
| 1174 | "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", |
| 1175 | le32_to_cpu(path[i].p_ext->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1176 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1177 | goto cleanup; |
| 1178 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1179 | /* start copy indexes */ |
| 1180 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
| 1181 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
| 1182 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 1183 | ext4_ext_show_move(inode, path, newblock, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1184 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1185 | memmove(++fidx, path[i].p_idx, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1186 | sizeof(struct ext4_extent_idx) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1187 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1188 | } |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1189 | /* zero out unused area in the extent block */ |
| 1190 | ext_size = sizeof(struct ext4_extent_header) + |
| 1191 | (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries)); |
| 1192 | memset(bh->b_data + ext_size, 0, |
| 1193 | inode->i_sb->s_blocksize - ext_size); |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1194 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1195 | set_buffer_uptodate(bh); |
| 1196 | unlock_buffer(bh); |
| 1197 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1198 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1199 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1200 | goto cleanup; |
| 1201 | brelse(bh); |
| 1202 | bh = NULL; |
| 1203 | |
| 1204 | /* correct old index */ |
| 1205 | if (m) { |
| 1206 | err = ext4_ext_get_access(handle, inode, path + i); |
| 1207 | if (err) |
| 1208 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1209 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1210 | err = ext4_ext_dirty(handle, inode, path + i); |
| 1211 | if (err) |
| 1212 | goto cleanup; |
| 1213 | } |
| 1214 | |
| 1215 | i--; |
| 1216 | } |
| 1217 | |
| 1218 | /* insert new index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1219 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 1220 | le32_to_cpu(border), newblock); |
| 1221 | |
| 1222 | cleanup: |
| 1223 | if (bh) { |
| 1224 | if (buffer_locked(bh)) |
| 1225 | unlock_buffer(bh); |
| 1226 | brelse(bh); |
| 1227 | } |
| 1228 | |
| 1229 | if (err) { |
| 1230 | /* free all allocated blocks in error case */ |
| 1231 | for (i = 0; i < depth; i++) { |
| 1232 | if (!ablocks[i]) |
| 1233 | continue; |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1234 | ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1235 | EXT4_FREE_BLOCKS_METADATA); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | kfree(ablocks); |
| 1239 | |
| 1240 | return err; |
| 1241 | } |
| 1242 | |
| 1243 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1244 | * ext4_ext_grow_indepth: |
| 1245 | * implements tree growing procedure: |
| 1246 | * - allocates new block |
| 1247 | * - moves top-level data (index block or leaf) into the new block |
| 1248 | * - initializes new top-level, creating index that points to the |
| 1249 | * just created block |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1250 | */ |
| 1251 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1252 | unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1253 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1254 | struct ext4_extent_header *neh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1255 | struct buffer_head *bh; |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1256 | ext4_fsblk_t newblock, goal = 0; |
| 1257 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1258 | int err = 0; |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1259 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1260 | |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1261 | /* Try to prepend new index to old one */ |
| 1262 | if (ext_depth(inode)) |
| 1263 | goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode))); |
| 1264 | if (goal > le32_to_cpu(es->s_first_data_block)) { |
| 1265 | flags |= EXT4_MB_HINT_TRY_GOAL; |
| 1266 | goal--; |
| 1267 | } else |
| 1268 | goal = ext4_inode_to_goal_block(inode); |
| 1269 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 1270 | NULL, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1271 | if (newblock == 0) |
| 1272 | return err; |
| 1273 | |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1274 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1275 | if (unlikely(!bh)) |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1276 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1277 | lock_buffer(bh); |
| 1278 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1279 | err = ext4_journal_get_create_access(handle, bh); |
| 1280 | if (err) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1281 | unlock_buffer(bh); |
| 1282 | goto out; |
| 1283 | } |
| 1284 | |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1285 | ext_size = sizeof(EXT4_I(inode)->i_data); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1286 | /* move top-level index/leaf into new block */ |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1287 | memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size); |
| 1288 | /* zero out unused area in the extent block */ |
| 1289 | 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] | 1290 | |
| 1291 | /* set size of new block */ |
| 1292 | neh = ext_block_hdr(bh); |
| 1293 | /* old root could have indexes or leaves |
| 1294 | * so calculate e_max right way */ |
| 1295 | if (ext_depth(inode)) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1296 | 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] | 1297 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1298 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1299 | neh->eh_magic = EXT4_EXT_MAGIC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1300 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1301 | set_buffer_uptodate(bh); |
| 1302 | unlock_buffer(bh); |
| 1303 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1304 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1305 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1306 | goto out; |
| 1307 | |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1308 | /* Update top-level index: num,max,pointer */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1309 | neh = ext_inode_hdr(inode); |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1310 | neh->eh_entries = cpu_to_le16(1); |
| 1311 | ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); |
| 1312 | if (neh->eh_depth == 0) { |
| 1313 | /* Root extent block becomes index block */ |
| 1314 | neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); |
| 1315 | EXT_FIRST_INDEX(neh)->ei_block = |
| 1316 | EXT_FIRST_EXTENT(neh)->ee_block; |
| 1317 | } |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1318 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1319 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 1320 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1321 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1322 | |
Wei Yongjun | ba39ebb | 2012-09-27 09:37:53 -0400 | [diff] [blame] | 1323 | le16_add_cpu(&neh->eh_depth, 1); |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1324 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1325 | out: |
| 1326 | brelse(bh); |
| 1327 | |
| 1328 | return err; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1332 | * ext4_ext_create_new_leaf: |
| 1333 | * finds empty index and adds new leaf. |
| 1334 | * if no free index is found, then it requests in-depth growing. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1335 | */ |
| 1336 | 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] | 1337 | unsigned int mb_flags, |
| 1338 | unsigned int gb_flags, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1339 | struct ext4_ext_path **ppath, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1340 | struct ext4_extent *newext) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1341 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1342 | struct ext4_ext_path *path = *ppath; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1343 | struct ext4_ext_path *curp; |
| 1344 | int depth, i, err = 0; |
| 1345 | |
| 1346 | repeat: |
| 1347 | i = depth = ext_depth(inode); |
| 1348 | |
| 1349 | /* walk up to the tree and look for free index entry */ |
| 1350 | curp = path + depth; |
| 1351 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1352 | i--; |
| 1353 | curp--; |
| 1354 | } |
| 1355 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1356 | /* we use already allocated block for index block, |
| 1357 | * so subsequent data blocks should be contiguous */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1358 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1359 | /* if we found index with free entry, then use that |
| 1360 | * entry: create all needed subtree and add new leaf */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1361 | err = ext4_ext_split(handle, inode, mb_flags, path, newext, i); |
Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1362 | if (err) |
| 1363 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1364 | |
| 1365 | /* refill path */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1366 | path = ext4_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1367 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1368 | ppath, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1369 | if (IS_ERR(path)) |
| 1370 | err = PTR_ERR(path); |
| 1371 | } else { |
| 1372 | /* tree is full, time to grow in depth */ |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1373 | err = ext4_ext_grow_indepth(handle, inode, mb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1374 | if (err) |
| 1375 | goto out; |
| 1376 | |
| 1377 | /* refill path */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1378 | path = ext4_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1379 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1380 | ppath, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1381 | if (IS_ERR(path)) { |
| 1382 | err = PTR_ERR(path); |
| 1383 | goto out; |
| 1384 | } |
| 1385 | |
| 1386 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1387 | * only first (depth 0 -> 1) produces free space; |
| 1388 | * in all other cases we have to split the grown tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1389 | */ |
| 1390 | depth = ext_depth(inode); |
| 1391 | 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] | 1392 | /* now we need to split */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1393 | goto repeat; |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | out: |
| 1398 | return err; |
| 1399 | } |
| 1400 | |
| 1401 | /* |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1402 | * search the closest allocated block to the left for *logical |
| 1403 | * and returns it at @logical + it's physical address at @phys |
| 1404 | * if *logical is the smallest allocated block, the function |
| 1405 | * returns 0 at @phys |
| 1406 | * return value contains 0 (success) or error code |
| 1407 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1408 | static int ext4_ext_search_left(struct inode *inode, |
| 1409 | struct ext4_ext_path *path, |
| 1410 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1411 | { |
| 1412 | struct ext4_extent_idx *ix; |
| 1413 | struct ext4_extent *ex; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1414 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1415 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1416 | if (unlikely(path == NULL)) { |
| 1417 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1418 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1419 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1420 | depth = path->p_depth; |
| 1421 | *phys = 0; |
| 1422 | |
| 1423 | if (depth == 0 && path->p_ext == NULL) |
| 1424 | return 0; |
| 1425 | |
| 1426 | /* usually extent in the path covers blocks smaller |
| 1427 | * then *logical, but it can be that extent is the |
| 1428 | * first one in the file */ |
| 1429 | |
| 1430 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1431 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1432 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1433 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1434 | EXT4_ERROR_INODE(inode, |
| 1435 | "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", |
| 1436 | *logical, le32_to_cpu(ex->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1437 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1438 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1439 | while (--depth >= 0) { |
| 1440 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1441 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1442 | EXT4_ERROR_INODE(inode, |
| 1443 | "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1444 | ix != NULL ? le32_to_cpu(ix->ei_block) : 0, |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1445 | EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1446 | 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] | 1447 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1448 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1449 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1450 | } |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1454 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1455 | EXT4_ERROR_INODE(inode, |
| 1456 | "logical %d < ee_block %d + ee_len %d!", |
| 1457 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1458 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1459 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1460 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1461 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1462 | *phys = ext4_ext_pblock(ex) + ee_len - 1; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1463 | return 0; |
| 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | * search the closest allocated block to the right for *logical |
| 1468 | * and returns it at @logical + it's physical address at @phys |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 1469 | * if *logical is the largest allocated block, the function |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1470 | * returns 0 at @phys |
| 1471 | * return value contains 0 (success) or error code |
| 1472 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1473 | static int ext4_ext_search_right(struct inode *inode, |
| 1474 | struct ext4_ext_path *path, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1475 | ext4_lblk_t *logical, ext4_fsblk_t *phys, |
| 1476 | struct ext4_extent **ret_ex) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1477 | { |
| 1478 | struct buffer_head *bh = NULL; |
| 1479 | struct ext4_extent_header *eh; |
| 1480 | struct ext4_extent_idx *ix; |
| 1481 | struct ext4_extent *ex; |
| 1482 | ext4_fsblk_t block; |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1483 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
| 1484 | int ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1485 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1486 | if (unlikely(path == NULL)) { |
| 1487 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1488 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1489 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1490 | depth = path->p_depth; |
| 1491 | *phys = 0; |
| 1492 | |
| 1493 | if (depth == 0 && path->p_ext == NULL) |
| 1494 | return 0; |
| 1495 | |
| 1496 | /* usually extent in the path covers blocks smaller |
| 1497 | * then *logical, but it can be that extent is the |
| 1498 | * first one in the file */ |
| 1499 | |
| 1500 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1501 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1502 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1503 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1504 | EXT4_ERROR_INODE(inode, |
| 1505 | "first_extent(path[%d].p_hdr) != ex", |
| 1506 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1507 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1508 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1509 | while (--depth >= 0) { |
| 1510 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1511 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1512 | EXT4_ERROR_INODE(inode, |
| 1513 | "ix != EXT_FIRST_INDEX *logical %d!", |
| 1514 | *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1515 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1516 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1517 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1518 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1519 | } |
| 1520 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1521 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1522 | EXT4_ERROR_INODE(inode, |
| 1523 | "logical %d < ee_block %d + ee_len %d!", |
| 1524 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1525 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1526 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1527 | |
| 1528 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1529 | /* next allocated block in this leaf */ |
| 1530 | ex++; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1531 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /* go up and search for index to the right */ |
| 1535 | while (--depth >= 0) { |
| 1536 | ix = path[depth].p_idx; |
| 1537 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1538 | goto got_index; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1539 | } |
| 1540 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1541 | /* we've gone up to the root and found no index to the right */ |
| 1542 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1543 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1544 | got_index: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1545 | /* we've found index to the right, let's |
| 1546 | * follow it and find the closest allocated |
| 1547 | * block to the right */ |
| 1548 | ix++; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1549 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1550 | while (++depth < path->p_depth) { |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1551 | /* subtract from p_depth to get proper eh_depth */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1552 | bh = read_extent_tree_block(inode, block, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1553 | path->p_depth - depth, 0); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1554 | if (IS_ERR(bh)) |
| 1555 | return PTR_ERR(bh); |
| 1556 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1557 | ix = EXT_FIRST_INDEX(eh); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1558 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1559 | put_bh(bh); |
| 1560 | } |
| 1561 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1562 | 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] | 1563 | if (IS_ERR(bh)) |
| 1564 | return PTR_ERR(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1565 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1566 | ex = EXT_FIRST_EXTENT(eh); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1567 | found_extent: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1568 | *logical = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1569 | *phys = ext4_ext_pblock(ex); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1570 | *ret_ex = ex; |
| 1571 | if (bh) |
| 1572 | put_bh(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1573 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1577 | * ext4_ext_next_allocated_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1578 | * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1579 | * NOTE: it considers block number from index entry as |
| 1580 | * allocated block. Thus, index entries have to be consistent |
| 1581 | * with leaves. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1582 | */ |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 1583 | ext4_lblk_t |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1584 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1585 | { |
| 1586 | int depth; |
| 1587 | |
| 1588 | BUG_ON(path == NULL); |
| 1589 | depth = path->p_depth; |
| 1590 | |
| 1591 | if (depth == 0 && path->p_ext == NULL) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1592 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1593 | |
| 1594 | while (depth >= 0) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1595 | struct ext4_ext_path *p = &path[depth]; |
| 1596 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1597 | if (depth == path->p_depth) { |
| 1598 | /* leaf */ |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1599 | if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr)) |
| 1600 | return le32_to_cpu(p->p_ext[1].ee_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1601 | } else { |
| 1602 | /* index */ |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1603 | if (p->p_idx != EXT_LAST_INDEX(p->p_hdr)) |
| 1604 | return le32_to_cpu(p->p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1605 | } |
| 1606 | depth--; |
| 1607 | } |
| 1608 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1609 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1613 | * ext4_ext_next_leaf_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1614 | * returns first allocated block from next leaf or EXT_MAX_BLOCKS |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1615 | */ |
Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1616 | 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] | 1617 | { |
| 1618 | int depth; |
| 1619 | |
| 1620 | BUG_ON(path == NULL); |
| 1621 | depth = path->p_depth; |
| 1622 | |
| 1623 | /* zero-tree has no leaf blocks at all */ |
| 1624 | if (depth == 0) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1625 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1626 | |
| 1627 | /* go to index block */ |
| 1628 | depth--; |
| 1629 | |
| 1630 | while (depth >= 0) { |
| 1631 | if (path[depth].p_idx != |
| 1632 | EXT_LAST_INDEX(path[depth].p_hdr)) |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1633 | return (ext4_lblk_t) |
| 1634 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1635 | depth--; |
| 1636 | } |
| 1637 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1638 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1642 | * ext4_ext_correct_indexes: |
| 1643 | * if leaf gets modified and modified extent is first in the leaf, |
| 1644 | * then we have to correct all indexes above. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1645 | * TODO: do we need to correct tree in all cases? |
| 1646 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1647 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1648 | struct ext4_ext_path *path) |
| 1649 | { |
| 1650 | struct ext4_extent_header *eh; |
| 1651 | int depth = ext_depth(inode); |
| 1652 | struct ext4_extent *ex; |
| 1653 | __le32 border; |
| 1654 | int k, err = 0; |
| 1655 | |
| 1656 | eh = path[depth].p_hdr; |
| 1657 | ex = path[depth].p_ext; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1658 | |
| 1659 | if (unlikely(ex == NULL || eh == NULL)) { |
| 1660 | EXT4_ERROR_INODE(inode, |
| 1661 | "ex %p == NULL or eh %p == NULL", ex, eh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1662 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1663 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1664 | |
| 1665 | if (depth == 0) { |
| 1666 | /* there is no tree at all */ |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
| 1670 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1671 | /* we correct tree if first leaf got modified only */ |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1676 | * TODO: we need correction if border is smaller than current one |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1677 | */ |
| 1678 | k = depth - 1; |
| 1679 | border = path[depth].p_ext->ee_block; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1680 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1681 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1682 | return err; |
| 1683 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1684 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1685 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1686 | return err; |
| 1687 | |
| 1688 | while (k--) { |
| 1689 | /* change all left-side indexes */ |
| 1690 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1691 | break; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1692 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1693 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1694 | break; |
| 1695 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1696 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1697 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1698 | break; |
| 1699 | } |
| 1700 | |
| 1701 | return err; |
| 1702 | } |
| 1703 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 1704 | static int ext4_can_extents_be_merged(struct inode *inode, |
| 1705 | struct ext4_extent *ex1, |
| 1706 | struct ext4_extent *ex2) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1707 | { |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1708 | unsigned short ext1_ee_len, ext2_ee_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1709 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1710 | if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2)) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1711 | return 0; |
| 1712 | |
| 1713 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1714 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1715 | |
| 1716 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1717 | le32_to_cpu(ex2->ee_block)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1718 | return 0; |
| 1719 | |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1720 | if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN) |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1721 | return 0; |
Matthew Bobrowski | 378f32b | 2019-11-05 23:02:39 +1100 | [diff] [blame] | 1722 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1723 | if (ext4_ext_is_unwritten(ex1) && |
Matthew Bobrowski | 378f32b | 2019-11-05 23:02:39 +1100 | [diff] [blame] | 1724 | ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN) |
Darrick J. Wong | a9b8241 | 2014-02-20 21:17:35 -0500 | [diff] [blame] | 1725 | return 0; |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1726 | #ifdef AGGRESSIVE_TEST |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1727 | if (ext1_ee_len >= 4) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1728 | return 0; |
| 1729 | #endif |
| 1730 | |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1731 | if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1732 | return 1; |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | /* |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1737 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1738 | * It always tries to merge towards right. If you want to merge towards |
| 1739 | * left, pass "ex - 1" as argument instead of "ex". |
| 1740 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1741 | * 1 if they got merged. |
| 1742 | */ |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1743 | static int ext4_ext_try_to_merge_right(struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1744 | struct ext4_ext_path *path, |
| 1745 | struct ext4_extent *ex) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1746 | { |
| 1747 | struct ext4_extent_header *eh; |
| 1748 | unsigned int depth, len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1749 | int merge_done = 0, unwritten; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1750 | |
| 1751 | depth = ext_depth(inode); |
| 1752 | BUG_ON(path[depth].p_hdr == NULL); |
| 1753 | eh = path[depth].p_hdr; |
| 1754 | |
| 1755 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1756 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1757 | break; |
| 1758 | /* merge with next extent! */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1759 | unwritten = ext4_ext_is_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1760 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1761 | + ext4_ext_get_actual_len(ex + 1)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1762 | if (unwritten) |
| 1763 | ext4_ext_mark_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1764 | |
| 1765 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1766 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1767 | * sizeof(struct ext4_extent); |
| 1768 | memmove(ex + 1, ex + 2, len); |
| 1769 | } |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1770 | le16_add_cpu(&eh->eh_entries, -1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1771 | merge_done = 1; |
| 1772 | WARN_ON(eh->eh_entries == 0); |
| 1773 | if (!eh->eh_entries) |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1774 | EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | return merge_done; |
| 1778 | } |
| 1779 | |
| 1780 | /* |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1781 | * This function does a very simple check to see if we can collapse |
| 1782 | * an extent tree with a single extent tree leaf block into the inode. |
| 1783 | */ |
| 1784 | static void ext4_ext_try_to_merge_up(handle_t *handle, |
| 1785 | struct inode *inode, |
| 1786 | struct ext4_ext_path *path) |
| 1787 | { |
| 1788 | size_t s; |
| 1789 | unsigned max_root = ext4_ext_space_root(inode, 0); |
| 1790 | ext4_fsblk_t blk; |
| 1791 | |
| 1792 | if ((path[0].p_depth != 1) || |
| 1793 | (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) || |
| 1794 | (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root)) |
| 1795 | return; |
| 1796 | |
| 1797 | /* |
| 1798 | * We need to modify the block allocation bitmap and the block |
| 1799 | * group descriptor to release the extent tree block. If we |
| 1800 | * can't get the journal credits, give up. |
| 1801 | */ |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 1802 | if (ext4_journal_extend(handle, 2, |
| 1803 | ext4_free_metadata_revoke_credits(inode->i_sb, 1))) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1804 | return; |
| 1805 | |
| 1806 | /* |
| 1807 | * Copy the extent data up to the inode |
| 1808 | */ |
| 1809 | blk = ext4_idx_pblock(path[0].p_idx); |
| 1810 | s = le16_to_cpu(path[1].p_hdr->eh_entries) * |
| 1811 | sizeof(struct ext4_extent_idx); |
| 1812 | s += sizeof(struct ext4_extent_header); |
| 1813 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 1814 | path[1].p_maxdepth = path[0].p_maxdepth; |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1815 | memcpy(path[0].p_hdr, path[1].p_hdr, s); |
| 1816 | path[0].p_depth = 0; |
| 1817 | path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + |
| 1818 | (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr)); |
| 1819 | path[0].p_hdr->eh_max = cpu_to_le16(max_root); |
| 1820 | |
| 1821 | brelse(path[1].p_bh); |
| 1822 | ext4_free_blocks(handle, inode, NULL, blk, 1, |
Theodore Ts'o | 71d4f7d | 2014-07-15 06:02:38 -0400 | [diff] [blame] | 1823 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | /* |
Eric Biggers | adde81c | 2019-12-31 12:04:41 -0600 | [diff] [blame] | 1827 | * This function tries to merge the @ex extent to neighbours in the tree, then |
| 1828 | * tries to collapse the extent tree into the inode. |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1829 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1830 | static void ext4_ext_try_to_merge(handle_t *handle, |
| 1831 | struct inode *inode, |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1832 | struct ext4_ext_path *path, |
Eric Biggers | adde81c | 2019-12-31 12:04:41 -0600 | [diff] [blame] | 1833 | struct ext4_extent *ex) |
| 1834 | { |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1835 | struct ext4_extent_header *eh; |
| 1836 | unsigned int depth; |
| 1837 | int merge_done = 0; |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1838 | |
| 1839 | depth = ext_depth(inode); |
| 1840 | BUG_ON(path[depth].p_hdr == NULL); |
| 1841 | eh = path[depth].p_hdr; |
| 1842 | |
| 1843 | if (ex > EXT_FIRST_EXTENT(eh)) |
| 1844 | merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); |
| 1845 | |
| 1846 | if (!merge_done) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1847 | (void) ext4_ext_try_to_merge_right(inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1848 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1849 | ext4_ext_try_to_merge_up(handle, inode, path); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /* |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1853 | * check if a portion of the "newext" extent overlaps with an |
| 1854 | * existing extent. |
| 1855 | * |
| 1856 | * If there is an overlap discovered, it updates the length of the newext |
| 1857 | * such that there will be no overlap, and then returns 1. |
| 1858 | * If there is no overlap found, it returns 0. |
| 1859 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1860 | static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, |
| 1861 | struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1862 | struct ext4_extent *newext, |
| 1863 | struct ext4_ext_path *path) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1864 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1865 | ext4_lblk_t b1, b2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1866 | unsigned int depth, len1; |
| 1867 | unsigned int ret = 0; |
| 1868 | |
| 1869 | b1 = le32_to_cpu(newext->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1870 | len1 = ext4_ext_get_actual_len(newext); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1871 | depth = ext_depth(inode); |
| 1872 | if (!path[depth].p_ext) |
| 1873 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1874 | 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] | 1875 | |
| 1876 | /* |
| 1877 | * get the next allocated block if the extent in the path |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1878 | * is before the requested block(s) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1879 | */ |
| 1880 | if (b2 < b1) { |
| 1881 | b2 = ext4_ext_next_allocated_block(path); |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1882 | if (b2 == EXT_MAX_BLOCKS) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1883 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1884 | b2 = EXT4_LBLK_CMASK(sbi, b2); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1885 | } |
| 1886 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1887 | /* check for wrap through zero on extent logical start block*/ |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1888 | if (b1 + len1 < b1) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1889 | len1 = EXT_MAX_BLOCKS - b1; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1890 | newext->ee_len = cpu_to_le16(len1); |
| 1891 | ret = 1; |
| 1892 | } |
| 1893 | |
| 1894 | /* check for overlap */ |
| 1895 | if (b1 + len1 > b2) { |
| 1896 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1897 | ret = 1; |
| 1898 | } |
| 1899 | out: |
| 1900 | return ret; |
| 1901 | } |
| 1902 | |
| 1903 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1904 | * ext4_ext_insert_extent: |
| 1905 | * tries to merge requsted extent into the existing extent or |
| 1906 | * inserts requested extent as new one into the tree, |
| 1907 | * creating new leaf in the no-space case. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1908 | */ |
| 1909 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1910 | struct ext4_ext_path **ppath, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1911 | struct ext4_extent *newext, int gb_flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1912 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1913 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1914 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1915 | struct ext4_extent *ex, *fex; |
| 1916 | struct ext4_extent *nearex; /* nearest extent */ |
| 1917 | struct ext4_ext_path *npath = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1918 | int depth, len, err; |
| 1919 | ext4_lblk_t next; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1920 | int mb_flags = 0, unwritten; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1921 | |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 1922 | if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 1923 | mb_flags |= EXT4_MB_DELALLOC_RESERVED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1924 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
| 1925 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1926 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1927 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1928 | depth = ext_depth(inode); |
| 1929 | ex = path[depth].p_ext; |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1930 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1931 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1932 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1933 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1934 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1935 | |
| 1936 | /* try to insert block into found extent and return */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1937 | if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) { |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1938 | |
| 1939 | /* |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1940 | * Try to see whether we should rather test the extent on |
| 1941 | * 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] | 1942 | * ext4_find_extent() can return either extent on the |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1943 | * left, or on the right from the searched position. This |
| 1944 | * will make merging more effective. |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1945 | */ |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1946 | if (ex < EXT_LAST_EXTENT(eh) && |
| 1947 | (le32_to_cpu(ex->ee_block) + |
| 1948 | ext4_ext_get_actual_len(ex) < |
| 1949 | le32_to_cpu(newext->ee_block))) { |
| 1950 | ex += 1; |
| 1951 | goto prepend; |
| 1952 | } else if ((ex > EXT_FIRST_EXTENT(eh)) && |
| 1953 | (le32_to_cpu(newext->ee_block) + |
| 1954 | ext4_ext_get_actual_len(newext) < |
| 1955 | le32_to_cpu(ex->ee_block))) |
| 1956 | ex -= 1; |
| 1957 | |
| 1958 | /* Try to append newex to the ex */ |
| 1959 | if (ext4_can_extents_be_merged(inode, ex, newext)) { |
| 1960 | ext_debug("append [%d]%d block to %u:[%d]%d" |
| 1961 | "(from %llu)\n", |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1962 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1963 | ext4_ext_get_actual_len(newext), |
| 1964 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1965 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1966 | ext4_ext_get_actual_len(ex), |
| 1967 | ext4_ext_pblock(ex)); |
| 1968 | err = ext4_ext_get_access(handle, inode, |
| 1969 | path + depth); |
| 1970 | if (err) |
| 1971 | return err; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1972 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1973 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1974 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1975 | if (unwritten) |
| 1976 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1977 | eh = path[depth].p_hdr; |
| 1978 | nearex = ex; |
| 1979 | goto merge; |
| 1980 | } |
| 1981 | |
| 1982 | prepend: |
| 1983 | /* Try to prepend newex to the ex */ |
| 1984 | if (ext4_can_extents_be_merged(inode, newext, ex)) { |
| 1985 | ext_debug("prepend %u[%d]%d block to %u:[%d]%d" |
| 1986 | "(from %llu)\n", |
| 1987 | le32_to_cpu(newext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1988 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1989 | ext4_ext_get_actual_len(newext), |
| 1990 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1991 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1992 | ext4_ext_get_actual_len(ex), |
| 1993 | ext4_ext_pblock(ex)); |
| 1994 | err = ext4_ext_get_access(handle, inode, |
| 1995 | path + depth); |
| 1996 | if (err) |
| 1997 | return err; |
| 1998 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1999 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2000 | ex->ee_block = newext->ee_block; |
| 2001 | ext4_ext_store_pblock(ex, ext4_ext_pblock(newext)); |
| 2002 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 2003 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2004 | if (unwritten) |
| 2005 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2006 | eh = path[depth].p_hdr; |
| 2007 | nearex = ex; |
| 2008 | goto merge; |
| 2009 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2012 | depth = ext_depth(inode); |
| 2013 | eh = path[depth].p_hdr; |
| 2014 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 2015 | goto has_space; |
| 2016 | |
| 2017 | /* probably next leaf has space for us? */ |
| 2018 | fex = EXT_LAST_EXTENT(eh); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2019 | next = EXT_MAX_BLOCKS; |
| 2020 | 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] | 2021 | next = ext4_ext_next_leaf_block(path); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2022 | if (next != EXT_MAX_BLOCKS) { |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2023 | ext_debug("next leaf block - %u\n", next); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2024 | BUG_ON(npath != NULL); |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 2025 | npath = ext4_find_extent(inode, next, NULL, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2026 | if (IS_ERR(npath)) |
| 2027 | return PTR_ERR(npath); |
| 2028 | BUG_ON(npath->p_depth != path->p_depth); |
| 2029 | eh = npath[depth].p_hdr; |
| 2030 | 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] | 2031 | ext_debug("next leaf isn't full(%d)\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2032 | le16_to_cpu(eh->eh_entries)); |
| 2033 | path = npath; |
Robin Dong | ffb505f | 2011-07-11 11:43:59 -0400 | [diff] [blame] | 2034 | goto has_space; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2035 | } |
| 2036 | ext_debug("next leaf has no free space(%d,%d)\n", |
| 2037 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 2038 | } |
| 2039 | |
| 2040 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2041 | * There is no free space in the found leaf. |
| 2042 | * We're gonna add a new leaf in the tree. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2043 | */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2044 | if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 2045 | mb_flags |= EXT4_MB_USE_RESERVED; |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2046 | 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] | 2047 | ppath, newext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2048 | if (err) |
| 2049 | goto cleanup; |
| 2050 | depth = ext_depth(inode); |
| 2051 | eh = path[depth].p_hdr; |
| 2052 | |
| 2053 | has_space: |
| 2054 | nearex = path[depth].p_ext; |
| 2055 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2056 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2057 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2058 | goto cleanup; |
| 2059 | |
| 2060 | if (!nearex) { |
| 2061 | /* there is no extent in this leaf, create first one */ |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2062 | ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2063 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2064 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2065 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2066 | ext4_ext_get_actual_len(newext)); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2067 | nearex = EXT_FIRST_EXTENT(eh); |
| 2068 | } else { |
| 2069 | if (le32_to_cpu(newext->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2070 | > le32_to_cpu(nearex->ee_block)) { |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2071 | /* Insert after */ |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2072 | ext_debug("insert %u:%llu:[%d]%d before: " |
| 2073 | "nearest %p\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2074 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2075 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2076 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2077 | ext4_ext_get_actual_len(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2078 | nearex); |
| 2079 | nearex++; |
| 2080 | } else { |
| 2081 | /* Insert before */ |
| 2082 | BUG_ON(newext->ee_block == nearex->ee_block); |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2083 | ext_debug("insert %u:%llu:[%d]%d after: " |
| 2084 | "nearest %p\n", |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2085 | le32_to_cpu(newext->ee_block), |
| 2086 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2087 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2088 | ext4_ext_get_actual_len(newext), |
| 2089 | nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2090 | } |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2091 | len = EXT_LAST_EXTENT(eh) - nearex + 1; |
| 2092 | if (len > 0) { |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2093 | ext_debug("insert %u:%llu:[%d]%d: " |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2094 | "move %d extents from 0x%p to 0x%p\n", |
| 2095 | le32_to_cpu(newext->ee_block), |
| 2096 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2097 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2098 | ext4_ext_get_actual_len(newext), |
| 2099 | len, nearex, nearex + 1); |
| 2100 | memmove(nearex + 1, nearex, |
| 2101 | len * sizeof(struct ext4_extent)); |
| 2102 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2103 | } |
| 2104 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2105 | le16_add_cpu(&eh->eh_entries, 1); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2106 | path[depth].p_ext = nearex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2107 | nearex->ee_block = newext->ee_block; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2108 | ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2109 | nearex->ee_len = newext->ee_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2110 | |
| 2111 | merge: |
HaiboLiu | e7bcf82 | 2012-07-09 16:29:28 -0400 | [diff] [blame] | 2112 | /* try to merge extents */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2113 | if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2114 | ext4_ext_try_to_merge(handle, inode, path, nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2115 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2116 | |
| 2117 | /* time to correct all indexes above */ |
| 2118 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2119 | if (err) |
| 2120 | goto cleanup; |
| 2121 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2122 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2123 | |
| 2124 | cleanup: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 2125 | ext4_ext_drop_refs(npath); |
| 2126 | kfree(npath); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2127 | return err; |
| 2128 | } |
| 2129 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2130 | static int ext4_fill_fiemap_extents(struct inode *inode, |
| 2131 | ext4_lblk_t block, ext4_lblk_t num, |
| 2132 | struct fiemap_extent_info *fieinfo) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2133 | { |
| 2134 | struct ext4_ext_path *path = NULL; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2135 | struct ext4_extent *ex; |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2136 | struct extent_status es; |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2137 | ext4_lblk_t next, next_del, start = 0, end = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2138 | ext4_lblk_t last = block + num; |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2139 | int exists, depth = 0, err = 0; |
| 2140 | unsigned int flags = 0; |
| 2141 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2142 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2143 | while (block < last && block != EXT_MAX_BLOCKS) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2144 | num = last - block; |
| 2145 | /* find extent for this block */ |
Theodore Ts'o | fab3a54 | 2009-12-09 21:30:02 -0500 | [diff] [blame] | 2146 | down_read(&EXT4_I(inode)->i_data_sem); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2147 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 2148 | path = ext4_find_extent(inode, block, &path, 0); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2149 | if (IS_ERR(path)) { |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2150 | up_read(&EXT4_I(inode)->i_data_sem); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2151 | err = PTR_ERR(path); |
| 2152 | path = NULL; |
| 2153 | break; |
| 2154 | } |
| 2155 | |
| 2156 | depth = ext_depth(inode); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2157 | if (unlikely(path[depth].p_hdr == NULL)) { |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2158 | up_read(&EXT4_I(inode)->i_data_sem); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2159 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2160 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2161 | break; |
| 2162 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2163 | ex = path[depth].p_ext; |
| 2164 | next = ext4_ext_next_allocated_block(path); |
| 2165 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2166 | flags = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2167 | exists = 0; |
| 2168 | if (!ex) { |
| 2169 | /* there is no extent yet, so try to allocate |
| 2170 | * all requested space */ |
| 2171 | start = block; |
| 2172 | end = block + num; |
| 2173 | } else if (le32_to_cpu(ex->ee_block) > block) { |
| 2174 | /* need to allocate space before found extent */ |
| 2175 | start = block; |
| 2176 | end = le32_to_cpu(ex->ee_block); |
| 2177 | if (block + num < end) |
| 2178 | end = block + num; |
| 2179 | } else if (block >= le32_to_cpu(ex->ee_block) |
| 2180 | + ext4_ext_get_actual_len(ex)) { |
| 2181 | /* need to allocate space after found extent */ |
| 2182 | start = block; |
| 2183 | end = block + num; |
| 2184 | if (end >= next) |
| 2185 | end = next; |
| 2186 | } else if (block >= le32_to_cpu(ex->ee_block)) { |
| 2187 | /* |
| 2188 | * some part of requested space is covered |
| 2189 | * by found extent |
| 2190 | */ |
| 2191 | start = block; |
| 2192 | end = le32_to_cpu(ex->ee_block) |
| 2193 | + ext4_ext_get_actual_len(ex); |
| 2194 | if (block + num < end) |
| 2195 | end = block + num; |
| 2196 | exists = 1; |
| 2197 | } else { |
| 2198 | BUG(); |
| 2199 | } |
| 2200 | BUG_ON(end <= start); |
| 2201 | |
| 2202 | if (!exists) { |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2203 | es.es_lblk = start; |
| 2204 | es.es_len = end - start; |
| 2205 | es.es_pblk = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2206 | } else { |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2207 | es.es_lblk = le32_to_cpu(ex->ee_block); |
| 2208 | es.es_len = ext4_ext_get_actual_len(ex); |
| 2209 | es.es_pblk = ext4_ext_pblock(ex); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2210 | if (ext4_ext_is_unwritten(ex)) |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2211 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2212 | } |
| 2213 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2214 | /* |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2215 | * Find delayed extent and update es accordingly. We call |
| 2216 | * it even in !exists case to find out whether es is the |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2217 | * last existing extent or not. |
| 2218 | */ |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2219 | next_del = ext4_find_delayed_extent(inode, &es); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2220 | if (!exists && next_del) { |
| 2221 | exists = 1; |
Jie Liu | 72dac95 | 2013-06-12 23:13:59 -0400 | [diff] [blame] | 2222 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 2223 | FIEMAP_EXTENT_UNKNOWN); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2224 | } |
| 2225 | up_read(&EXT4_I(inode)->i_data_sem); |
| 2226 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2227 | if (unlikely(es.es_len == 0)) { |
| 2228 | EXT4_ERROR_INODE(inode, "es.es_len == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2229 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2230 | break; |
| 2231 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2232 | |
Zheng Liu | f7fec03 | 2013-02-18 00:28:47 -0500 | [diff] [blame] | 2233 | /* |
| 2234 | * This is possible iff next == next_del == EXT_MAX_BLOCKS. |
| 2235 | * we need to check next == EXT_MAX_BLOCKS because it is |
| 2236 | * possible that an extent is with unwritten and delayed |
| 2237 | * status due to when an extent is delayed allocated and |
| 2238 | * is allocated by fallocate status tree will track both of |
| 2239 | * them in a extent. |
| 2240 | * |
| 2241 | * So we could return a unwritten and delayed extent, and |
| 2242 | * its block is equal to 'next'. |
| 2243 | */ |
| 2244 | if (next == next_del && next == EXT_MAX_BLOCKS) { |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2245 | flags |= FIEMAP_EXTENT_LAST; |
| 2246 | if (unlikely(next_del != EXT_MAX_BLOCKS || |
| 2247 | next != EXT_MAX_BLOCKS)) { |
| 2248 | EXT4_ERROR_INODE(inode, |
| 2249 | "next extent == %u, next " |
| 2250 | "delalloc extent = %u", |
| 2251 | next, next_del); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2252 | err = -EFSCORRUPTED; |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2253 | break; |
| 2254 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2255 | } |
| 2256 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2257 | if (exists) { |
| 2258 | err = fiemap_fill_next_extent(fieinfo, |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2259 | (__u64)es.es_lblk << blksize_bits, |
| 2260 | (__u64)es.es_pblk << blksize_bits, |
| 2261 | (__u64)es.es_len << blksize_bits, |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 2262 | flags); |
| 2263 | if (err < 0) |
| 2264 | break; |
| 2265 | if (err == 1) { |
| 2266 | err = 0; |
| 2267 | break; |
| 2268 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2269 | } |
| 2270 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 2271 | block = es.es_lblk + es.es_len; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2272 | } |
| 2273 | |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 2274 | ext4_ext_drop_refs(path); |
| 2275 | kfree(path); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 2276 | return err; |
| 2277 | } |
| 2278 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 2279 | static int ext4_fill_es_cache_info(struct inode *inode, |
| 2280 | ext4_lblk_t block, ext4_lblk_t num, |
| 2281 | struct fiemap_extent_info *fieinfo) |
| 2282 | { |
| 2283 | ext4_lblk_t next, end = block + num - 1; |
| 2284 | struct extent_status es; |
| 2285 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; |
| 2286 | unsigned int flags; |
| 2287 | int err; |
| 2288 | |
| 2289 | while (block <= end) { |
| 2290 | next = 0; |
| 2291 | flags = 0; |
| 2292 | if (!ext4_es_lookup_extent(inode, block, &next, &es)) |
| 2293 | break; |
| 2294 | if (ext4_es_is_unwritten(&es)) |
| 2295 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 2296 | if (ext4_es_is_delayed(&es)) |
| 2297 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 2298 | FIEMAP_EXTENT_UNKNOWN); |
| 2299 | if (ext4_es_is_hole(&es)) |
| 2300 | flags |= EXT4_FIEMAP_EXTENT_HOLE; |
| 2301 | if (next == 0) |
| 2302 | flags |= FIEMAP_EXTENT_LAST; |
| 2303 | if (flags & (FIEMAP_EXTENT_DELALLOC| |
| 2304 | EXT4_FIEMAP_EXTENT_HOLE)) |
| 2305 | es.es_pblk = 0; |
| 2306 | else |
| 2307 | es.es_pblk = ext4_es_pblock(&es); |
| 2308 | err = fiemap_fill_next_extent(fieinfo, |
| 2309 | (__u64)es.es_lblk << blksize_bits, |
| 2310 | (__u64)es.es_pblk << blksize_bits, |
| 2311 | (__u64)es.es_len << blksize_bits, |
| 2312 | flags); |
| 2313 | if (next == 0) |
| 2314 | break; |
| 2315 | block = next; |
| 2316 | if (err < 0) |
| 2317 | return err; |
| 2318 | if (err == 1) |
| 2319 | return 0; |
| 2320 | } |
| 2321 | return 0; |
| 2322 | } |
| 2323 | |
| 2324 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2325 | /* |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2326 | * ext4_ext_determine_hole - determine hole around given block |
| 2327 | * @inode: inode we lookup in |
| 2328 | * @path: path in extent tree to @lblk |
| 2329 | * @lblk: pointer to logical block around which we want to determine hole |
| 2330 | * |
| 2331 | * Determine hole length (and start if easily possible) around given logical |
| 2332 | * block. We don't try too hard to find the beginning of the hole but @path |
| 2333 | * actually points to extent before @lblk, we provide it. |
| 2334 | * |
| 2335 | * The function returns the length of a hole starting at @lblk. We update @lblk |
| 2336 | * to the beginning of the hole if we managed to find it. |
| 2337 | */ |
| 2338 | static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode, |
| 2339 | struct ext4_ext_path *path, |
| 2340 | ext4_lblk_t *lblk) |
| 2341 | { |
| 2342 | int depth = ext_depth(inode); |
| 2343 | struct ext4_extent *ex; |
| 2344 | ext4_lblk_t len; |
| 2345 | |
| 2346 | ex = path[depth].p_ext; |
| 2347 | if (ex == NULL) { |
| 2348 | /* there is no extent yet, so gap is [0;-] */ |
| 2349 | *lblk = 0; |
| 2350 | len = EXT_MAX_BLOCKS; |
| 2351 | } else if (*lblk < le32_to_cpu(ex->ee_block)) { |
| 2352 | len = le32_to_cpu(ex->ee_block) - *lblk; |
| 2353 | } else if (*lblk >= le32_to_cpu(ex->ee_block) |
| 2354 | + ext4_ext_get_actual_len(ex)) { |
| 2355 | ext4_lblk_t next; |
| 2356 | |
| 2357 | *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); |
| 2358 | next = ext4_ext_next_allocated_block(path); |
| 2359 | BUG_ON(next == *lblk); |
| 2360 | len = next - *lblk; |
| 2361 | } else { |
| 2362 | BUG(); |
| 2363 | } |
| 2364 | return len; |
| 2365 | } |
| 2366 | |
| 2367 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2368 | * ext4_ext_put_gap_in_cache: |
| 2369 | * calculate boundaries of the gap that the requested block fits into |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2370 | * and cache this gap |
| 2371 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2372 | static void |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2373 | ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start, |
| 2374 | ext4_lblk_t hole_len) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2375 | { |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2376 | struct extent_status es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2377 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 2378 | ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, |
| 2379 | hole_start + hole_len - 1, &es); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2380 | if (es.es_len) { |
| 2381 | /* There's delayed extent containing lblock? */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2382 | if (es.es_lblk <= hole_start) |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2383 | return; |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2384 | hole_len = min(es.es_lblk - hole_start, hole_len); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2385 | } |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2386 | ext_debug(" -> %u:%u\n", hole_start, hole_len); |
| 2387 | ext4_es_insert_extent(inode, hole_start, hole_len, ~0, |
| 2388 | EXTENT_STATUS_HOLE); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2392 | * ext4_ext_rm_idx: |
| 2393 | * removes index from the index block. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2394 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2395 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2396 | struct ext4_ext_path *path, int depth) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2397 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2398 | int err; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2399 | ext4_fsblk_t leaf; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2400 | |
| 2401 | /* free index block */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2402 | depth--; |
| 2403 | path = path + depth; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2404 | leaf = ext4_idx_pblock(path->p_idx); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2405 | if (unlikely(path->p_hdr->eh_entries == 0)) { |
| 2406 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2407 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2408 | } |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2409 | err = ext4_ext_get_access(handle, inode, path); |
| 2410 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2411 | return err; |
Robin Dong | 0e1147b | 2011-07-27 21:29:33 -0400 | [diff] [blame] | 2412 | |
| 2413 | if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { |
| 2414 | int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; |
| 2415 | len *= sizeof(struct ext4_extent_idx); |
| 2416 | memmove(path->p_idx, path->p_idx + 1, len); |
| 2417 | } |
| 2418 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2419 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2420 | err = ext4_ext_dirty(handle, inode, path); |
| 2421 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2422 | return err; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2423 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2424 | trace_ext4_ext_rm_idx(inode, leaf); |
| 2425 | |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 2426 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2427 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2428 | |
| 2429 | while (--depth >= 0) { |
| 2430 | if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr)) |
| 2431 | break; |
| 2432 | path--; |
| 2433 | err = ext4_ext_get_access(handle, inode, path); |
| 2434 | if (err) |
| 2435 | break; |
| 2436 | path->p_idx->ei_block = (path+1)->p_idx->ei_block; |
| 2437 | err = ext4_ext_dirty(handle, inode, path); |
| 2438 | if (err) |
| 2439 | break; |
| 2440 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2441 | return err; |
| 2442 | } |
| 2443 | |
| 2444 | /* |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2445 | * ext4_ext_calc_credits_for_single_extent: |
| 2446 | * This routine returns max. credits that needed to insert an extent |
| 2447 | * to the extent tree. |
| 2448 | * When pass the actual path, the caller should calculate credits |
| 2449 | * under i_data_sem. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2450 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2451 | 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] | 2452 | struct ext4_ext_path *path) |
| 2453 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2454 | if (path) { |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2455 | int depth = ext_depth(inode); |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 2456 | int ret = 0; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2457 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2458 | /* probably there is space in leaf? */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2459 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2460 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
| 2461 | |
| 2462 | /* |
| 2463 | * There are some space in the leaf tree, no |
| 2464 | * need to account for leaf block credit |
| 2465 | * |
| 2466 | * bitmaps and block group descriptor blocks |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 2467 | * and other metadata blocks still need to be |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2468 | * accounted. |
| 2469 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2470 | /* 1 bitmap, 1 block group descriptor */ |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2471 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Aneesh Kumar K.V | 5887e98 | 2009-07-05 23:12:04 -0400 | [diff] [blame] | 2472 | return ret; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2473 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2474 | } |
| 2475 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2476 | return ext4_chunk_trans_blocks(inode, nrblocks); |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2477 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2478 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2479 | /* |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2480 | * 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] | 2481 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2482 | * If we add a single extent, then in the worse case, each tree level |
| 2483 | * index/leaf need to be changed in case of the tree split. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2484 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2485 | * If more extents are inserted, they could cause the whole tree split more |
| 2486 | * than once, but this is really rare. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2487 | */ |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2488 | int ext4_ext_index_trans_blocks(struct inode *inode, int extents) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2489 | { |
| 2490 | int index; |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 2491 | int depth; |
| 2492 | |
| 2493 | /* If we are converting the inline data, only one is needed here. */ |
| 2494 | if (ext4_has_inline_data(inode)) |
| 2495 | return 1; |
| 2496 | |
| 2497 | depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2498 | |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2499 | if (extents <= 1) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2500 | index = depth * 2; |
| 2501 | else |
| 2502 | index = depth * 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2503 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2504 | return index; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2505 | } |
| 2506 | |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2507 | static inline int get_default_free_blocks_flags(struct inode *inode) |
| 2508 | { |
Tahsin Erdogan | ddfa17e | 2017-06-21 21:36:51 -0400 | [diff] [blame] | 2509 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) || |
| 2510 | ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE)) |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 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 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2517 | /* |
| 2518 | * ext4_rereserve_cluster - increment the reserved cluster count when |
| 2519 | * freeing a cluster with a pending reservation |
| 2520 | * |
| 2521 | * @inode - file containing the cluster |
| 2522 | * @lblk - logical block in cluster to be reserved |
| 2523 | * |
| 2524 | * Increments the reserved cluster count and adjusts quota in a bigalloc |
| 2525 | * file system when freeing a partial cluster containing at least one |
| 2526 | * delayed and unwritten block. A partial cluster meeting that |
| 2527 | * requirement will have a pending reservation. If so, the |
| 2528 | * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to |
| 2529 | * defer reserved and allocated space accounting to a subsequent call |
| 2530 | * to this function. |
| 2531 | */ |
| 2532 | static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk) |
| 2533 | { |
| 2534 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2535 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 2536 | |
| 2537 | dquot_reclaim_block(inode, EXT4_C2B(sbi, 1)); |
| 2538 | |
| 2539 | spin_lock(&ei->i_block_reservation_lock); |
| 2540 | ei->i_reserved_data_blocks++; |
| 2541 | percpu_counter_add(&sbi->s_dirtyclusters_counter, 1); |
| 2542 | spin_unlock(&ei->i_block_reservation_lock); |
| 2543 | |
| 2544 | percpu_counter_add(&sbi->s_freeclusters_counter, 1); |
| 2545 | ext4_remove_pending(inode, lblk); |
| 2546 | } |
| 2547 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2548 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2549 | struct ext4_extent *ex, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2550 | struct partial_cluster *partial, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2551 | ext4_lblk_t from, ext4_lblk_t to) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2552 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2553 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2554 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2555 | ext4_fsblk_t last_pblk, pblk; |
| 2556 | ext4_lblk_t num; |
| 2557 | int flags; |
| 2558 | |
| 2559 | /* only extent tail removal is allowed */ |
| 2560 | if (from < le32_to_cpu(ex->ee_block) || |
| 2561 | to != le32_to_cpu(ex->ee_block) + ee_len - 1) { |
| 2562 | ext4_error(sbi->s_sb, |
| 2563 | "strange request: removal(2) %u-%u from %u:%u", |
| 2564 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
| 2565 | return 0; |
| 2566 | } |
| 2567 | |
| 2568 | #ifdef EXTENTS_STATS |
| 2569 | spin_lock(&sbi->s_ext_stats_lock); |
| 2570 | sbi->s_ext_blocks += ee_len; |
| 2571 | sbi->s_ext_extents++; |
| 2572 | if (ee_len < sbi->s_ext_min) |
| 2573 | sbi->s_ext_min = ee_len; |
| 2574 | if (ee_len > sbi->s_ext_max) |
| 2575 | sbi->s_ext_max = ee_len; |
| 2576 | if (ext_depth(inode) > sbi->s_depth_max) |
| 2577 | sbi->s_depth_max = ext_depth(inode); |
| 2578 | spin_unlock(&sbi->s_ext_stats_lock); |
| 2579 | #endif |
| 2580 | |
| 2581 | trace_ext4_remove_blocks(inode, ex, from, to, partial); |
| 2582 | |
| 2583 | /* |
| 2584 | * if we have a partial cluster, and it's different from the |
| 2585 | * cluster of the last block in the extent, we free it |
| 2586 | */ |
| 2587 | last_pblk = ext4_ext_pblock(ex) + ee_len - 1; |
| 2588 | |
| 2589 | if (partial->state != initial && |
| 2590 | partial->pclu != EXT4_B2C(sbi, last_pblk)) { |
| 2591 | if (partial->state == tofree) { |
| 2592 | flags = get_default_free_blocks_flags(inode); |
| 2593 | if (ext4_is_pending(inode, partial->lblk)) |
| 2594 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
| 2595 | ext4_free_blocks(handle, inode, NULL, |
| 2596 | EXT4_C2B(sbi, partial->pclu), |
| 2597 | sbi->s_cluster_ratio, flags); |
| 2598 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2599 | ext4_rereserve_cluster(inode, partial->lblk); |
| 2600 | } |
| 2601 | partial->state = initial; |
| 2602 | } |
| 2603 | |
| 2604 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
| 2605 | pblk = ext4_ext_pblock(ex) + ee_len - num; |
| 2606 | |
| 2607 | /* |
| 2608 | * We free the partial cluster at the end of the extent (if any), |
| 2609 | * unless the cluster is used by another extent (partial_cluster |
| 2610 | * state is nofree). If a partial cluster exists here, it must be |
| 2611 | * shared with the last block in the extent. |
| 2612 | */ |
| 2613 | flags = get_default_free_blocks_flags(inode); |
| 2614 | |
| 2615 | /* partial, left end cluster aligned, right end unaligned */ |
| 2616 | if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) && |
| 2617 | (EXT4_LBLK_CMASK(sbi, to) >= from) && |
| 2618 | (partial->state != nofree)) { |
| 2619 | if (ext4_is_pending(inode, to)) |
| 2620 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
| 2621 | ext4_free_blocks(handle, inode, NULL, |
| 2622 | EXT4_PBLK_CMASK(sbi, last_pblk), |
| 2623 | sbi->s_cluster_ratio, flags); |
| 2624 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2625 | ext4_rereserve_cluster(inode, to); |
| 2626 | partial->state = initial; |
| 2627 | flags = get_default_free_blocks_flags(inode); |
| 2628 | } |
| 2629 | |
| 2630 | flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; |
Andrey Sidorov | 18888cf | 2012-09-19 14:14:53 -0400 | [diff] [blame] | 2631 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2632 | /* |
| 2633 | * For bigalloc file systems, we never free a partial cluster |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2634 | * at the beginning of the extent. Instead, we check to see if we |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2635 | * need to free it on a subsequent call to ext4_remove_blocks, |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2636 | * 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] | 2637 | */ |
| 2638 | flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2639 | ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2640 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2641 | /* reset the partial cluster if we've freed past it */ |
| 2642 | if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk)) |
| 2643 | partial->state = initial; |
| 2644 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2645 | /* |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2646 | * If we've freed the entire extent but the beginning is not left |
| 2647 | * cluster aligned and is not marked as ineligible for freeing we |
| 2648 | * record the partial cluster at the beginning of the extent. It |
| 2649 | * wasn't freed by the preceding ext4_free_blocks() call, and we |
| 2650 | * need to look farther to the left to determine if it's to be freed |
| 2651 | * (not shared with another extent). Else, reset the partial |
| 2652 | * cluster - we're either done freeing or the beginning of the |
| 2653 | * extent is left cluster aligned. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2654 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2655 | if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) { |
| 2656 | if (partial->state == initial) { |
| 2657 | partial->pclu = EXT4_B2C(sbi, pblk); |
| 2658 | partial->lblk = from; |
| 2659 | partial->state = tofree; |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2660 | } |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2661 | } else { |
| 2662 | partial->state = initial; |
| 2663 | } |
| 2664 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2665 | return 0; |
| 2666 | } |
| 2667 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2668 | /* |
| 2669 | * ext4_ext_rm_leaf() Removes the extents associated with the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2670 | * blocks appearing between "start" and "end". Both "start" |
| 2671 | * and "end" must appear in the same extent or EIO is returned. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2672 | * |
| 2673 | * @handle: The journal handle |
| 2674 | * @inode: The files inode |
| 2675 | * @path: The path to the leaf |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2676 | * @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] | 2677 | * has been released from it. However, if this value is |
| 2678 | * negative, it's a cluster just to the right of the |
| 2679 | * punched region and it must not be freed. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2680 | * @start: The first block to remove |
| 2681 | * @end: The last block to remove |
| 2682 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2683 | static int |
| 2684 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2685 | struct ext4_ext_path *path, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2686 | struct partial_cluster *partial, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2687 | ext4_lblk_t start, ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2688 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2689 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2690 | int err = 0, correct_index = 0; |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2691 | int depth = ext_depth(inode), credits, revoke_credits; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2692 | struct ext4_extent_header *eh; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2693 | ext4_lblk_t a, b; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2694 | unsigned num; |
| 2695 | ext4_lblk_t ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2696 | unsigned short ex_ee_len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2697 | unsigned unwritten = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2698 | struct ext4_extent *ex; |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2699 | ext4_fsblk_t pblk; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2700 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2701 | /* the header must be checked already in ext4_ext_remove_space() */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2702 | ext_debug("truncate since %u in leaf to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2703 | if (!path[depth].p_hdr) |
| 2704 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 2705 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2706 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 2707 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2708 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2709 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2710 | /* find where to start removing */ |
Ashish Sangwan | 6ae06ff | 2013-07-01 08:12:41 -0400 | [diff] [blame] | 2711 | ex = path[depth].p_ext; |
| 2712 | if (!ex) |
| 2713 | ex = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2714 | |
| 2715 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2716 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2717 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2718 | trace_ext4_ext_rm_leaf(inode, start, ex, partial); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2719 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2720 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 2721 | ex_ee_block + ex_ee_len > start) { |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2722 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2723 | if (ext4_ext_is_unwritten(ex)) |
| 2724 | unwritten = 1; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2725 | else |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2726 | unwritten = 0; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2727 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 2728 | ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2729 | unwritten, ex_ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2730 | path[depth].p_ext = ex; |
| 2731 | |
| 2732 | a = ex_ee_block > start ? ex_ee_block : start; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2733 | b = ex_ee_block+ex_ee_len - 1 < end ? |
| 2734 | ex_ee_block+ex_ee_len - 1 : end; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2735 | |
| 2736 | ext_debug(" border %u:%u\n", a, b); |
| 2737 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2738 | /* If this extent is beyond the end of the hole, skip it */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2739 | if (end < ex_ee_block) { |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2740 | /* |
| 2741 | * We're going to skip this extent and move to another, |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2742 | * so note that its first cluster is in use to avoid |
| 2743 | * freeing it when removing blocks. Eventually, the |
| 2744 | * right edge of the truncated/punched region will |
| 2745 | * be just to the left. |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2746 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2747 | if (sbi->s_cluster_ratio > 1) { |
| 2748 | pblk = ext4_ext_pblock(ex); |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2749 | partial->pclu = EXT4_B2C(sbi, pblk); |
| 2750 | partial->state = nofree; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2751 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2752 | ex--; |
| 2753 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2754 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2755 | continue; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2756 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
Lukas Czerner | dc1841d | 2012-03-19 23:07:43 -0400 | [diff] [blame] | 2757 | EXT4_ERROR_INODE(inode, |
| 2758 | "can not handle truncate %u:%u " |
| 2759 | "on extent %u:%u", |
| 2760 | start, end, ex_ee_block, |
| 2761 | ex_ee_block + ex_ee_len - 1); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2762 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2763 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2764 | } else if (a != ex_ee_block) { |
| 2765 | /* remove tail of the extent */ |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2766 | num = a - ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2767 | } else { |
| 2768 | /* remove whole extent: excellent! */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2769 | num = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2770 | } |
Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 2771 | /* |
| 2772 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 2773 | * descriptor) for each block group; assume two block |
| 2774 | * groups plus ex_ee_len/blocks_per_block_group for |
| 2775 | * the worst case |
| 2776 | */ |
| 2777 | 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] | 2778 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 2779 | correct_index = 1; |
| 2780 | credits += (ext_depth(inode)) + 1; |
| 2781 | } |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2782 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2783 | /* |
| 2784 | * We may end up freeing some index blocks and data from the |
| 2785 | * punched range. Note that partial clusters are accounted for |
| 2786 | * by ext4_free_data_revoke_credits(). |
| 2787 | */ |
| 2788 | revoke_credits = |
| 2789 | ext4_free_metadata_revoke_credits(inode->i_sb, |
| 2790 | ext_depth(inode)) + |
| 2791 | ext4_free_data_revoke_credits(inode, b - a + 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2792 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2793 | err = ext4_datasem_ensure_credits(handle, inode, credits, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2794 | credits, revoke_credits); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2795 | if (err) { |
| 2796 | if (err > 0) |
| 2797 | err = -EAGAIN; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2798 | goto out; |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2799 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2800 | |
| 2801 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2802 | if (err) |
| 2803 | goto out; |
| 2804 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2805 | err = ext4_remove_blocks(handle, inode, ex, partial, a, b); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2806 | if (err) |
| 2807 | goto out; |
| 2808 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2809 | if (num == 0) |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2810 | /* this extent is removed; mark slot entirely unused */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2811 | ext4_ext_store_pblock(ex, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2812 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2813 | ex->ee_len = cpu_to_le16(num); |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2814 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2815 | * Do not mark unwritten if all the blocks in the |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2816 | * extent have been removed. |
| 2817 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2818 | if (unwritten && num) |
| 2819 | ext4_ext_mark_unwritten(ex); |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2820 | /* |
| 2821 | * If the extent was completely released, |
| 2822 | * we need to remove it from the leaf |
| 2823 | */ |
| 2824 | if (num == 0) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2825 | if (end != EXT_MAX_BLOCKS - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2826 | /* |
| 2827 | * For hole punching, we need to scoot all the |
| 2828 | * extents up when an extent is removed so that |
| 2829 | * we dont have blank extents in the middle |
| 2830 | */ |
| 2831 | memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * |
| 2832 | sizeof(struct ext4_extent)); |
| 2833 | |
| 2834 | /* Now get rid of the one at the end */ |
| 2835 | memset(EXT_LAST_EXTENT(eh), 0, |
| 2836 | sizeof(struct ext4_extent)); |
| 2837 | } |
| 2838 | le16_add_cpu(&eh->eh_entries, -1); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2839 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2840 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2841 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2842 | if (err) |
| 2843 | goto out; |
| 2844 | |
Yongqiang Yang | bf52c6f | 2011-11-01 18:59:26 -0400 | [diff] [blame] | 2845 | 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] | 2846 | ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2847 | ex--; |
| 2848 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2849 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | if (correct_index && eh->eh_entries) |
| 2853 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2854 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2855 | /* |
Eric Whitney | ad6599a | 2014-04-01 19:49:30 -0400 | [diff] [blame] | 2856 | * If there's a partial cluster and at least one extent remains in |
| 2857 | * 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] | 2858 | * current extent. If it is shared with the current extent |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2859 | * we reset the partial cluster because we've reached the start of the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2860 | * truncated/punched region and we're done removing blocks. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2861 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2862 | if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) { |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2863 | pblk = ext4_ext_pblock(ex) + ex_ee_len - 1; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2864 | if (partial->pclu != EXT4_B2C(sbi, pblk)) { |
| 2865 | int flags = get_default_free_blocks_flags(inode); |
| 2866 | |
| 2867 | if (ext4_is_pending(inode, partial->lblk)) |
| 2868 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2869 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2870 | EXT4_C2B(sbi, partial->pclu), |
| 2871 | sbi->s_cluster_ratio, flags); |
| 2872 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2873 | ext4_rereserve_cluster(inode, partial->lblk); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2874 | } |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2875 | partial->state = initial; |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2876 | } |
| 2877 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2878 | /* if this leaf is free, then we should |
| 2879 | * remove it from index block above */ |
| 2880 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2881 | err = ext4_ext_rm_idx(handle, inode, path, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2882 | |
| 2883 | out: |
| 2884 | return err; |
| 2885 | } |
| 2886 | |
| 2887 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2888 | * ext4_ext_more_to_rm: |
| 2889 | * returns 1 if current index has to be freed (even partial) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2890 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2891 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2892 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 2893 | { |
| 2894 | BUG_ON(path->p_idx == NULL); |
| 2895 | |
| 2896 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 2897 | return 0; |
| 2898 | |
| 2899 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2900 | * if truncate on deeper level happened, it wasn't partial, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2901 | * so we have to consider current index for truncation |
| 2902 | */ |
| 2903 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 2904 | return 0; |
| 2905 | return 1; |
| 2906 | } |
| 2907 | |
Theodore Ts'o | 26a4c0c | 2013-04-03 12:45:17 -0400 | [diff] [blame] | 2908 | int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, |
| 2909 | ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2910 | { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2911 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2912 | int depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2913 | struct ext4_ext_path *path = NULL; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2914 | struct partial_cluster partial; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2915 | handle_t *handle; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2916 | int i = 0, err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2917 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2918 | partial.pclu = 0; |
| 2919 | partial.lblk = 0; |
| 2920 | partial.state = initial; |
| 2921 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2922 | ext_debug("truncate since %u to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2923 | |
| 2924 | /* probably first extent we're gonna free will be last in block */ |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2925 | handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE, |
| 2926 | depth + 1, |
| 2927 | ext4_free_metadata_revoke_credits(inode->i_sb, depth)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2928 | if (IS_ERR(handle)) |
| 2929 | return PTR_ERR(handle); |
| 2930 | |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2931 | again: |
Lukas Czerner | 6180132 | 2013-05-27 23:32:35 -0400 | [diff] [blame] | 2932 | trace_ext4_ext_remove_space(inode, start, end, depth); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2933 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2934 | /* |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2935 | * Check if we are removing extents inside the extent tree. If that |
| 2936 | * is the case, we are going to punch a hole inside the extent tree |
| 2937 | * so we have to check whether we need to split the extent covering |
| 2938 | * the last block to remove so we can easily remove the part of it |
| 2939 | * in ext4_ext_rm_leaf(). |
| 2940 | */ |
| 2941 | if (end < EXT_MAX_BLOCKS - 1) { |
| 2942 | struct ext4_extent *ex; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2943 | ext4_lblk_t ee_block, ex_end, lblk; |
| 2944 | ext4_fsblk_t pblk; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2945 | |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2946 | /* find extent for or closest extent to this block */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 2947 | path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2948 | if (IS_ERR(path)) { |
| 2949 | ext4_journal_stop(handle); |
| 2950 | return PTR_ERR(path); |
| 2951 | } |
| 2952 | depth = ext_depth(inode); |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2953 | /* 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] | 2954 | ex = path[depth].p_ext; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2955 | if (!ex) { |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2956 | if (depth) { |
| 2957 | EXT4_ERROR_INODE(inode, |
| 2958 | "path[%d].p_hdr == NULL", |
| 2959 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2960 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2961 | } |
| 2962 | goto out; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2963 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2964 | |
| 2965 | ee_block = le32_to_cpu(ex->ee_block); |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2966 | ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2967 | |
| 2968 | /* |
| 2969 | * See if the last block is inside the extent, if so split |
| 2970 | * the extent at 'end' block so we can easily remove the |
| 2971 | * tail of the first part of the split extent in |
| 2972 | * ext4_ext_rm_leaf(). |
| 2973 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2974 | if (end >= ee_block && end < ex_end) { |
| 2975 | |
| 2976 | /* |
| 2977 | * If we're going to split the extent, note that |
| 2978 | * the cluster containing the block after 'end' is |
| 2979 | * in use to avoid freeing it when removing blocks. |
| 2980 | */ |
| 2981 | if (sbi->s_cluster_ratio > 1) { |
| 2982 | pblk = ext4_ext_pblock(ex) + end - ee_block + 2; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2983 | partial.pclu = EXT4_B2C(sbi, pblk); |
| 2984 | partial.state = nofree; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2985 | } |
| 2986 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2987 | /* |
| 2988 | * Split the extent in two so that 'end' is the last |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 2989 | * block in the first new extent. Also we should not |
| 2990 | * fail removing space due to ENOSPC so try to use |
| 2991 | * reserved block if that happens. |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2992 | */ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 2993 | err = ext4_force_split_extent_at(handle, inode, &path, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 2994 | end + 1, 1); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2995 | if (err < 0) |
| 2996 | goto out; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2997 | |
Eric Whitney | 7bd7523 | 2019-02-28 23:34:11 -0500 | [diff] [blame] | 2998 | } else if (sbi->s_cluster_ratio > 1 && end >= ex_end && |
| 2999 | partial.state == initial) { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 3000 | /* |
Eric Whitney | 7bd7523 | 2019-02-28 23:34:11 -0500 | [diff] [blame] | 3001 | * If we're punching, there's an extent to the right. |
| 3002 | * If the partial cluster hasn't been set, set it to |
| 3003 | * that extent's first cluster and its state to nofree |
| 3004 | * so it won't be freed should it contain blocks to be |
| 3005 | * removed. If it's already set (tofree/nofree), we're |
| 3006 | * retrying and keep the original partial cluster info |
| 3007 | * so a cluster marked tofree as a result of earlier |
| 3008 | * extent removal is not lost. |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 3009 | */ |
| 3010 | lblk = ex_end + 1; |
| 3011 | err = ext4_ext_search_right(inode, path, &lblk, &pblk, |
| 3012 | &ex); |
| 3013 | if (err) |
| 3014 | goto out; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 3015 | if (pblk) { |
| 3016 | partial.pclu = EXT4_B2C(sbi, pblk); |
| 3017 | partial.state = nofree; |
| 3018 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 3019 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 3020 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 3021 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3022 | * We start scanning from right side, freeing all the blocks |
| 3023 | * after i_size and walking into the tree depth-wise. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3024 | */ |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 3025 | depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 3026 | if (path) { |
| 3027 | int k = i = depth; |
| 3028 | while (--k > 0) |
| 3029 | path[k].p_block = |
| 3030 | le16_to_cpu(path[k].p_hdr->eh_entries)+1; |
| 3031 | } else { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 3032 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 3033 | GFP_NOFS); |
| 3034 | if (path == NULL) { |
| 3035 | ext4_journal_stop(handle); |
| 3036 | return -ENOMEM; |
| 3037 | } |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 3038 | path[0].p_maxdepth = path[0].p_depth = depth; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 3039 | path[0].p_hdr = ext_inode_hdr(inode); |
Theodore Ts'o | 89a4e48 | 2012-08-17 08:54:52 -0400 | [diff] [blame] | 3040 | i = 0; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 3041 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 3042 | if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3043 | err = -EFSCORRUPTED; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 3044 | goto out; |
| 3045 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3046 | } |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 3047 | err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3048 | |
| 3049 | while (i >= 0 && err == 0) { |
| 3050 | if (i == depth) { |
| 3051 | /* this is leaf block */ |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 3052 | err = ext4_ext_rm_leaf(handle, inode, path, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 3053 | &partial, start, end); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3054 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3055 | brelse(path[i].p_bh); |
| 3056 | path[i].p_bh = NULL; |
| 3057 | i--; |
| 3058 | continue; |
| 3059 | } |
| 3060 | |
| 3061 | /* this is index block */ |
| 3062 | if (!path[i].p_hdr) { |
| 3063 | ext_debug("initialize header\n"); |
| 3064 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3067 | if (!path[i].p_idx) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3068 | /* this level hasn't been touched yet */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3069 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 3070 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
| 3071 | ext_debug("init index ptr: hdr 0x%p, num %d\n", |
| 3072 | path[i].p_hdr, |
| 3073 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 3074 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3075 | /* we were already here, see at next index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3076 | path[i].p_idx--; |
| 3077 | } |
| 3078 | |
| 3079 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
| 3080 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 3081 | path[i].p_idx); |
| 3082 | if (ext4_ext_more_to_rm(path + i)) { |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 3083 | struct buffer_head *bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3084 | /* go to the next level */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 3085 | ext_debug("move to level %d (block %llu)\n", |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3086 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3087 | memset(path + i + 1, 0, sizeof(*path)); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 3088 | bh = read_extent_tree_block(inode, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 3089 | ext4_idx_pblock(path[i].p_idx), depth - i - 1, |
| 3090 | EXT4_EX_NOCACHE); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 3091 | if (IS_ERR(bh)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3092 | /* should we reset i_size? */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 3093 | err = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3094 | break; |
| 3095 | } |
Theodore Ts'o | 76828c88 | 2013-07-15 12:27:47 -0400 | [diff] [blame] | 3096 | /* Yield here to deal with large extent trees. |
| 3097 | * Should be a no-op if we did IO above. */ |
| 3098 | cond_resched(); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 3099 | if (WARN_ON(i + 1 > depth)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3100 | err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 3101 | break; |
| 3102 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 3103 | path[i + 1].p_bh = bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3104 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3105 | /* save actual number of indexes since this |
| 3106 | * number is changed at the next iteration */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3107 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 3108 | i++; |
| 3109 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3110 | /* we finished processing this index, go up */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3111 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3112 | /* index is empty, remove it; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3113 | * handle must be already prepared by the |
| 3114 | * truncatei_leaf() */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 3115 | err = ext4_ext_rm_idx(handle, inode, path, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3116 | } |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3117 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3118 | brelse(path[i].p_bh); |
| 3119 | path[i].p_bh = NULL; |
| 3120 | i--; |
| 3121 | ext_debug("return to level %d\n", i); |
| 3122 | } |
| 3123 | } |
| 3124 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 3125 | trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial, |
| 3126 | path->p_hdr->eh_entries); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3127 | |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 3128 | /* |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 3129 | * if there's a partial cluster and we have removed the first extent |
| 3130 | * in the file, then we also free the partial cluster, if any |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 3131 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 3132 | if (partial.state == tofree && err == 0) { |
| 3133 | int flags = get_default_free_blocks_flags(inode); |
| 3134 | |
| 3135 | if (ext4_is_pending(inode, partial.lblk)) |
| 3136 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3137 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 3138 | EXT4_C2B(sbi, partial.pclu), |
| 3139 | sbi->s_cluster_ratio, flags); |
| 3140 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 3141 | ext4_rereserve_cluster(inode, partial.lblk); |
| 3142 | partial.state = initial; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3143 | } |
| 3144 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3145 | /* TODO: flexible tree reduction should be here */ |
| 3146 | if (path->p_hdr->eh_entries == 0) { |
| 3147 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3148 | * truncate to zero freed all the tree, |
| 3149 | * so we need to correct eh_depth |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3150 | */ |
| 3151 | err = ext4_ext_get_access(handle, inode, path); |
| 3152 | if (err == 0) { |
| 3153 | ext_inode_hdr(inode)->eh_depth = 0; |
| 3154 | ext_inode_hdr(inode)->eh_max = |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 3155 | cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3156 | err = ext4_ext_dirty(handle, inode, path); |
| 3157 | } |
| 3158 | } |
| 3159 | out: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 3160 | ext4_ext_drop_refs(path); |
| 3161 | kfree(path); |
| 3162 | path = NULL; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3163 | if (err == -EAGAIN) |
| 3164 | goto again; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3165 | ext4_journal_stop(handle); |
| 3166 | |
| 3167 | return err; |
| 3168 | } |
| 3169 | |
| 3170 | /* |
| 3171 | * called at mount time |
| 3172 | */ |
| 3173 | void ext4_ext_init(struct super_block *sb) |
| 3174 | { |
| 3175 | /* |
| 3176 | * possible initialization would be here |
| 3177 | */ |
| 3178 | |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3179 | if (ext4_has_feature_extents(sb)) { |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3180 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3181 | printk(KERN_INFO "EXT4-fs: file extents enabled" |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 3182 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3183 | ", aggressive tests" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3184 | #endif |
| 3185 | #ifdef CHECK_BINSEARCH |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3186 | ", check binsearch" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3187 | #endif |
| 3188 | #ifdef EXTENTS_STATS |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3189 | ", stats" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3190 | #endif |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3191 | "\n"); |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3192 | #endif |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3193 | #ifdef EXTENTS_STATS |
| 3194 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 3195 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 3196 | EXT4_SB(sb)->s_ext_max = 0; |
| 3197 | #endif |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | /* |
| 3202 | * called at umount time |
| 3203 | */ |
| 3204 | void ext4_ext_release(struct super_block *sb) |
| 3205 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3206 | if (!ext4_has_feature_extents(sb)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3207 | return; |
| 3208 | |
| 3209 | #ifdef EXTENTS_STATS |
| 3210 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 3211 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3212 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 3213 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 3214 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 3215 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 3216 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 3217 | } |
| 3218 | #endif |
| 3219 | } |
| 3220 | |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3221 | static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex) |
| 3222 | { |
| 3223 | ext4_lblk_t ee_block; |
| 3224 | ext4_fsblk_t ee_pblock; |
| 3225 | unsigned int ee_len; |
| 3226 | |
| 3227 | ee_block = le32_to_cpu(ex->ee_block); |
| 3228 | ee_len = ext4_ext_get_actual_len(ex); |
| 3229 | ee_pblock = ext4_ext_pblock(ex); |
| 3230 | |
| 3231 | if (ee_len == 0) |
| 3232 | return 0; |
| 3233 | |
| 3234 | return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, |
| 3235 | EXTENT_STATUS_WRITTEN); |
| 3236 | } |
| 3237 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3238 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 3239 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 3240 | { |
Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 3241 | ext4_fsblk_t ee_pblock; |
| 3242 | unsigned int ee_len; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3243 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3244 | ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3245 | ee_pblock = ext4_ext_pblock(ex); |
Jan Kara | 53085fa | 2015-12-07 15:09:35 -0500 | [diff] [blame] | 3246 | return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock, |
| 3247 | ee_len); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3248 | } |
| 3249 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3250 | /* |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3251 | * ext4_split_extent_at() splits an extent at given block. |
| 3252 | * |
| 3253 | * @handle: the journal handle |
| 3254 | * @inode: the file inode |
| 3255 | * @path: the path to the extent |
| 3256 | * @split: the logical block where the extent is splitted. |
| 3257 | * @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] | 3258 | * the states(init or unwritten) of new extents. |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3259 | * @flags: flags used to insert new extent to extent tree. |
| 3260 | * |
| 3261 | * |
| 3262 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states |
| 3263 | * of which are deterimined by split_flag. |
| 3264 | * |
| 3265 | * There are two cases: |
| 3266 | * a> the extent are splitted into two extent. |
| 3267 | * b> split is not needed, and just mark the extent. |
| 3268 | * |
| 3269 | * return 0 on success. |
| 3270 | */ |
| 3271 | static int ext4_split_extent_at(handle_t *handle, |
| 3272 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3273 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3274 | ext4_lblk_t split, |
| 3275 | int split_flag, |
| 3276 | int flags) |
| 3277 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3278 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3279 | ext4_fsblk_t newblock; |
| 3280 | ext4_lblk_t ee_block; |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3281 | struct ext4_extent *ex, newex, orig_ex, zero_ex; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3282 | struct ext4_extent *ex2 = NULL; |
| 3283 | unsigned int ee_len, depth; |
| 3284 | int err = 0; |
| 3285 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3286 | BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) == |
| 3287 | (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)); |
| 3288 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3289 | ext_debug("ext4_split_extents_at: inode %lu, logical" |
| 3290 | "block %llu\n", inode->i_ino, (unsigned long long)split); |
| 3291 | |
| 3292 | ext4_ext_show_leaf(inode, path); |
| 3293 | |
| 3294 | depth = ext_depth(inode); |
| 3295 | ex = path[depth].p_ext; |
| 3296 | ee_block = le32_to_cpu(ex->ee_block); |
| 3297 | ee_len = ext4_ext_get_actual_len(ex); |
| 3298 | newblock = split - ee_block + ext4_ext_pblock(ex); |
| 3299 | |
| 3300 | BUG_ON(split < ee_block || split >= (ee_block + ee_len)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3301 | BUG_ON(!ext4_ext_is_unwritten(ex) && |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3302 | split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3303 | EXT4_EXT_MARK_UNWRIT1 | |
| 3304 | EXT4_EXT_MARK_UNWRIT2)); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3305 | |
| 3306 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3307 | if (err) |
| 3308 | goto out; |
| 3309 | |
| 3310 | if (split == ee_block) { |
| 3311 | /* |
| 3312 | * case b: block @split is the block that the extent begins with |
| 3313 | * then we just change the state of the extent, and splitting |
| 3314 | * is not needed. |
| 3315 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3316 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3317 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3318 | else |
| 3319 | ext4_ext_mark_initialized(ex); |
| 3320 | |
| 3321 | if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3322 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3323 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3324 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3325 | goto out; |
| 3326 | } |
| 3327 | |
| 3328 | /* case a */ |
| 3329 | memcpy(&orig_ex, ex, sizeof(orig_ex)); |
| 3330 | ex->ee_len = cpu_to_le16(split - ee_block); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3331 | if (split_flag & EXT4_EXT_MARK_UNWRIT1) |
| 3332 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3333 | |
| 3334 | /* |
| 3335 | * path may lead to new leaf, not to original leaf any more |
| 3336 | * after ext4_ext_insert_extent() returns, |
| 3337 | */ |
| 3338 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3339 | if (err) |
| 3340 | goto fix_extent_len; |
| 3341 | |
| 3342 | ex2 = &newex; |
| 3343 | ex2->ee_block = cpu_to_le32(split); |
| 3344 | ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); |
| 3345 | ext4_ext_store_pblock(ex2, newblock); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3346 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3347 | ext4_ext_mark_unwritten(ex2); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3348 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3349 | err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3350 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3351 | if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3352 | if (split_flag & EXT4_EXT_DATA_VALID1) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3353 | err = ext4_ext_zeroout(inode, ex2); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3354 | zero_ex.ee_block = ex2->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3355 | zero_ex.ee_len = cpu_to_le16( |
| 3356 | ext4_ext_get_actual_len(ex2)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3357 | ext4_ext_store_pblock(&zero_ex, |
| 3358 | ext4_ext_pblock(ex2)); |
| 3359 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3360 | err = ext4_ext_zeroout(inode, ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3361 | zero_ex.ee_block = ex->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3362 | zero_ex.ee_len = cpu_to_le16( |
| 3363 | ext4_ext_get_actual_len(ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3364 | ext4_ext_store_pblock(&zero_ex, |
| 3365 | ext4_ext_pblock(ex)); |
| 3366 | } |
| 3367 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3368 | err = ext4_ext_zeroout(inode, &orig_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3369 | zero_ex.ee_block = orig_ex.ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3370 | zero_ex.ee_len = cpu_to_le16( |
| 3371 | ext4_ext_get_actual_len(&orig_ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3372 | ext4_ext_store_pblock(&zero_ex, |
| 3373 | ext4_ext_pblock(&orig_ex)); |
| 3374 | } |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3375 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3376 | if (err) |
| 3377 | goto fix_extent_len; |
| 3378 | /* update the extent length and mark as initialized */ |
Al Viro | af1584f | 2012-04-12 20:32:25 -0400 | [diff] [blame] | 3379 | ex->ee_len = cpu_to_le16(ee_len); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3380 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3381 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3382 | if (err) |
| 3383 | goto fix_extent_len; |
| 3384 | |
| 3385 | /* update extent status tree */ |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3386 | err = ext4_zeroout_es(inode, &zero_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3387 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3388 | goto out; |
| 3389 | } else if (err) |
| 3390 | goto fix_extent_len; |
| 3391 | |
| 3392 | out: |
| 3393 | ext4_ext_show_leaf(inode, path); |
| 3394 | return err; |
| 3395 | |
| 3396 | fix_extent_len: |
| 3397 | ex->ee_len = orig_ex.ee_len; |
Dmitry Monakhov | 29faed1 | 2014-07-27 22:30:29 -0400 | [diff] [blame] | 3398 | ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3399 | return err; |
| 3400 | } |
| 3401 | |
| 3402 | /* |
| 3403 | * ext4_split_extents() splits an extent and mark extent which is covered |
| 3404 | * by @map as split_flags indicates |
| 3405 | * |
Anatol Pomozov | 70261f5 | 2013-08-28 14:40:12 -0400 | [diff] [blame] | 3406 | * 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] | 3407 | * There are three possibilities: |
| 3408 | * a> There is no split required |
| 3409 | * b> Splits in two extents: Split is happening at either end of the extent |
| 3410 | * c> Splits in three extents: Somone is splitting in middle of the extent |
| 3411 | * |
| 3412 | */ |
| 3413 | static int ext4_split_extent(handle_t *handle, |
| 3414 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3415 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3416 | struct ext4_map_blocks *map, |
| 3417 | int split_flag, |
| 3418 | int flags) |
| 3419 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3420 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3421 | ext4_lblk_t ee_block; |
| 3422 | struct ext4_extent *ex; |
| 3423 | unsigned int ee_len, depth; |
| 3424 | int err = 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3425 | int unwritten; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3426 | int split_flag1, flags1; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3427 | int allocated = map->m_len; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3428 | |
| 3429 | depth = ext_depth(inode); |
| 3430 | ex = path[depth].p_ext; |
| 3431 | ee_block = le32_to_cpu(ex->ee_block); |
| 3432 | ee_len = ext4_ext_get_actual_len(ex); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3433 | unwritten = ext4_ext_is_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3434 | |
| 3435 | if (map->m_lblk + map->m_len < ee_block + ee_len) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3436 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3437 | flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3438 | if (unwritten) |
| 3439 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1 | |
| 3440 | EXT4_EXT_MARK_UNWRIT2; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3441 | if (split_flag & EXT4_EXT_DATA_VALID2) |
| 3442 | split_flag1 |= EXT4_EXT_DATA_VALID1; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3443 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3444 | map->m_lblk + map->m_len, split_flag1, flags1); |
Yongqiang Yang | 9391741 | 2011-05-22 20:49:12 -0400 | [diff] [blame] | 3445 | if (err) |
| 3446 | goto out; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3447 | } else { |
| 3448 | allocated = ee_len - (map->m_lblk - ee_block); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3449 | } |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3450 | /* |
| 3451 | * Update path is required because previous ext4_split_extent_at() may |
| 3452 | * result in split of original leaf or extent zeroout. |
| 3453 | */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3454 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3455 | if (IS_ERR(path)) |
| 3456 | return PTR_ERR(path); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3457 | depth = ext_depth(inode); |
| 3458 | ex = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3459 | if (!ex) { |
| 3460 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3461 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3462 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3463 | } |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3464 | unwritten = ext4_ext_is_unwritten(ex); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3465 | split_flag1 = 0; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3466 | |
| 3467 | if (map->m_lblk >= ee_block) { |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3468 | split_flag1 = split_flag & EXT4_EXT_DATA_VALID2; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3469 | if (unwritten) { |
| 3470 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1; |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3471 | split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3472 | EXT4_EXT_MARK_UNWRIT2); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3473 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3474 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3475 | map->m_lblk, split_flag1, flags); |
| 3476 | if (err) |
| 3477 | goto out; |
| 3478 | } |
| 3479 | |
| 3480 | ext4_ext_show_leaf(inode, path); |
| 3481 | out: |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3482 | return err ? err : allocated; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3483 | } |
| 3484 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3485 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3486 | * 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] | 3487 | * to an unwritten extent. It may result in splitting the unwritten |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3488 | * extent into multiple extents (up to three - one initialized and two |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3489 | * unwritten). |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3490 | * There are three possibilities: |
| 3491 | * a> There is no split required: Entire extent should be initialized |
| 3492 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3493 | * 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] | 3494 | * |
| 3495 | * Pre-conditions: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3496 | * - The extent pointed to by 'path' is unwritten. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3497 | * - The extent pointed to by 'path' contains a superset |
| 3498 | * of the logical span [map->m_lblk, map->m_lblk + map->m_len). |
| 3499 | * |
| 3500 | * Post-conditions on success: |
| 3501 | * - the returned value is the number of blocks beyond map->l_lblk |
| 3502 | * that are allocated and initialized. |
| 3503 | * It is guaranteed to be >= map->m_len. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3504 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3505 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3506 | struct inode *inode, |
| 3507 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3508 | struct ext4_ext_path **ppath, |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3509 | int flags) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3510 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3511 | struct ext4_ext_path *path = *ppath; |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3512 | struct ext4_sb_info *sbi; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3513 | struct ext4_extent_header *eh; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3514 | struct ext4_map_blocks split_map; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3515 | struct ext4_extent zero_ex1, zero_ex2; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3516 | struct ext4_extent *ex, *abut_ex; |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3517 | ext4_lblk_t ee_block, eof_block; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3518 | unsigned int ee_len, depth, map_len = map->m_len; |
| 3519 | int allocated = 0, max_zeroout = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3520 | int err = 0; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3521 | int split_flag = EXT4_EXT_DATA_VALID2; |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3522 | |
| 3523 | ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" |
| 3524 | "block %llu, max_blocks %u\n", inode->i_ino, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3525 | (unsigned long long)map->m_lblk, map_len); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3526 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3527 | sbi = EXT4_SB(inode->i_sb); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3528 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3529 | inode->i_sb->s_blocksize_bits; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3530 | if (eof_block < map->m_lblk + map_len) |
| 3531 | eof_block = map->m_lblk + map_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3532 | |
| 3533 | depth = ext_depth(inode); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3534 | eh = path[depth].p_hdr; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3535 | ex = path[depth].p_ext; |
| 3536 | ee_block = le32_to_cpu(ex->ee_block); |
| 3537 | ee_len = ext4_ext_get_actual_len(ex); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3538 | zero_ex1.ee_len = 0; |
| 3539 | zero_ex2.ee_len = 0; |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3540 | |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3541 | trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); |
| 3542 | |
| 3543 | /* Pre-conditions */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3544 | BUG_ON(!ext4_ext_is_unwritten(ex)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3545 | BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3546 | |
| 3547 | /* |
| 3548 | * Attempt to transfer newly initialized blocks from the currently |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3549 | * unwritten extent to its neighbor. This is much cheaper |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3550 | * than an insertion followed by a merge as those involve costly |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3551 | * memmove() calls. Transferring to the left is the common case in |
| 3552 | * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE) |
| 3553 | * followed by append writes. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3554 | * |
| 3555 | * Limitations of the current logic: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3556 | * - L1: we do not deal with writes covering the whole extent. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3557 | * This would require removing the extent if the transfer |
| 3558 | * is possible. |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3559 | * - L2: we only attempt to merge with an extent stored in the |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3560 | * same extent tree node. |
| 3561 | */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3562 | if ((map->m_lblk == ee_block) && |
| 3563 | /* See if we can merge left */ |
| 3564 | (map_len < ee_len) && /*L1*/ |
| 3565 | (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3566 | ext4_lblk_t prev_lblk; |
| 3567 | ext4_fsblk_t prev_pblk, ee_pblk; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3568 | unsigned int prev_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3569 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3570 | abut_ex = ex - 1; |
| 3571 | prev_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3572 | prev_len = ext4_ext_get_actual_len(abut_ex); |
| 3573 | prev_pblk = ext4_ext_pblock(abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3574 | ee_pblk = ext4_ext_pblock(ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3575 | |
| 3576 | /* |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3577 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3578 | * upon those conditions: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3579 | * - C1: abut_ex is initialized, |
| 3580 | * - C2: abut_ex is logically abutting ex, |
| 3581 | * - C3: abut_ex is physically abutting ex, |
| 3582 | * - C4: abut_ex can receive the additional blocks without |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3583 | * overflowing the (initialized) length limit. |
| 3584 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3585 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3586 | ((prev_lblk + prev_len) == ee_block) && /*C2*/ |
| 3587 | ((prev_pblk + prev_len) == ee_pblk) && /*C3*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3588 | (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3589 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3590 | if (err) |
| 3591 | goto out; |
| 3592 | |
| 3593 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3594 | map, ex, abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3595 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3596 | /* Shift the start of ex by 'map_len' blocks */ |
| 3597 | ex->ee_block = cpu_to_le32(ee_block + map_len); |
| 3598 | ext4_ext_store_pblock(ex, ee_pblk + map_len); |
| 3599 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3600 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3601 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3602 | /* Extend abut_ex by 'map_len' blocks */ |
| 3603 | abut_ex->ee_len = cpu_to_le16(prev_len + map_len); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3604 | |
| 3605 | /* Result: number of initialized blocks past m_lblk */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3606 | allocated = map_len; |
| 3607 | } |
| 3608 | } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) && |
| 3609 | (map_len < ee_len) && /*L1*/ |
| 3610 | ex < EXT_LAST_EXTENT(eh)) { /*L2*/ |
| 3611 | /* See if we can merge right */ |
| 3612 | ext4_lblk_t next_lblk; |
| 3613 | ext4_fsblk_t next_pblk, ee_pblk; |
| 3614 | unsigned int next_len; |
| 3615 | |
| 3616 | abut_ex = ex + 1; |
| 3617 | next_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3618 | next_len = ext4_ext_get_actual_len(abut_ex); |
| 3619 | next_pblk = ext4_ext_pblock(abut_ex); |
| 3620 | ee_pblk = ext4_ext_pblock(ex); |
| 3621 | |
| 3622 | /* |
| 3623 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
| 3624 | * upon those conditions: |
| 3625 | * - C1: abut_ex is initialized, |
| 3626 | * - C2: abut_ex is logically abutting ex, |
| 3627 | * - C3: abut_ex is physically abutting ex, |
| 3628 | * - C4: abut_ex can receive the additional blocks without |
| 3629 | * overflowing the (initialized) length limit. |
| 3630 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3631 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3632 | ((map->m_lblk + map_len) == next_lblk) && /*C2*/ |
| 3633 | ((ee_pblk + ee_len) == next_pblk) && /*C3*/ |
| 3634 | (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
| 3635 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3636 | if (err) |
| 3637 | goto out; |
| 3638 | |
| 3639 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
| 3640 | map, ex, abut_ex); |
| 3641 | |
| 3642 | /* Shift the start of abut_ex by 'map_len' blocks */ |
| 3643 | abut_ex->ee_block = cpu_to_le32(next_lblk - map_len); |
| 3644 | ext4_ext_store_pblock(abut_ex, next_pblk - map_len); |
| 3645 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3646 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3647 | |
| 3648 | /* Extend abut_ex by 'map_len' blocks */ |
| 3649 | abut_ex->ee_len = cpu_to_le16(next_len + map_len); |
| 3650 | |
| 3651 | /* Result: number of initialized blocks past m_lblk */ |
| 3652 | allocated = map_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3653 | } |
| 3654 | } |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3655 | if (allocated) { |
| 3656 | /* Mark the block containing both extents as dirty */ |
| 3657 | ext4_ext_dirty(handle, inode, path + depth); |
| 3658 | |
| 3659 | /* Update path to point to the right extent */ |
| 3660 | path[depth].p_ext = abut_ex; |
| 3661 | goto out; |
| 3662 | } else |
| 3663 | allocated = ee_len - (map->m_lblk - ee_block); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3664 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3665 | WARN_ON(map->m_lblk < ee_block); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3666 | /* |
| 3667 | * It is safe to convert extent to initialized via explicit |
Yongqiang Yang | 9e74056 | 2014-01-06 14:05:23 -0500 | [diff] [blame] | 3668 | * zeroout only if extent is fully inside i_size or new_size. |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3669 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3670 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3671 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3672 | if (EXT4_EXT_MAY_ZEROOUT & split_flag) |
| 3673 | max_zeroout = sbi->s_extent_max_zeroout_kb >> |
Lukas Czerner | 4f42f80 | 2013-03-12 12:40:04 -0400 | [diff] [blame] | 3674 | (inode->i_sb->s_blocksize_bits - 10); |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3675 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3676 | /* |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3677 | * five cases: |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3678 | * 1. split the extent into three extents. |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3679 | * 2. split the extent into two extents, zeroout the head of the first |
| 3680 | * extent. |
| 3681 | * 3. split the extent into two extents, zeroout the tail of the second |
| 3682 | * extent. |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3683 | * 4. split the extent into two extents with out zeroout. |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3684 | * 5. no splitting needed, just possibly zeroout the head and / or the |
| 3685 | * tail of the extent. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3686 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3687 | split_map.m_lblk = map->m_lblk; |
| 3688 | split_map.m_len = map->m_len; |
| 3689 | |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3690 | if (max_zeroout && (allocated > split_map.m_len)) { |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3691 | if (allocated <= max_zeroout) { |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3692 | /* case 3 or 5 */ |
| 3693 | zero_ex1.ee_block = |
| 3694 | cpu_to_le32(split_map.m_lblk + |
| 3695 | split_map.m_len); |
| 3696 | zero_ex1.ee_len = |
| 3697 | cpu_to_le16(allocated - split_map.m_len); |
| 3698 | ext4_ext_store_pblock(&zero_ex1, |
| 3699 | ext4_ext_pblock(ex) + split_map.m_lblk + |
| 3700 | split_map.m_len - ee_block); |
| 3701 | err = ext4_ext_zeroout(inode, &zero_ex1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3702 | if (err) |
| 3703 | goto out; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3704 | split_map.m_len = allocated; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3705 | } |
| 3706 | if (split_map.m_lblk - ee_block + split_map.m_len < |
| 3707 | max_zeroout) { |
| 3708 | /* case 2 or 5 */ |
| 3709 | if (split_map.m_lblk != ee_block) { |
| 3710 | zero_ex2.ee_block = ex->ee_block; |
| 3711 | zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk - |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3712 | ee_block); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3713 | ext4_ext_store_pblock(&zero_ex2, |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3714 | ext4_ext_pblock(ex)); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3715 | err = ext4_ext_zeroout(inode, &zero_ex2); |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3716 | if (err) |
| 3717 | goto out; |
| 3718 | } |
| 3719 | |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3720 | split_map.m_len += split_map.m_lblk - ee_block; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3721 | split_map.m_lblk = ee_block; |
Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3722 | allocated = map->m_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3723 | } |
| 3724 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3725 | |
Jan Kara | ae9e9c6a | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 3726 | err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag, |
| 3727 | flags); |
| 3728 | if (err > 0) |
| 3729 | err = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3730 | out: |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3731 | /* If we have gotten a failure, don't zero out status tree */ |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3732 | if (!err) { |
| 3733 | err = ext4_zeroout_es(inode, &zero_ex1); |
| 3734 | if (!err) |
| 3735 | err = ext4_zeroout_es(inode, &zero_ex2); |
| 3736 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3737 | return err ? err : allocated; |
| 3738 | } |
| 3739 | |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3740 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3741 | * This function is called by ext4_ext_map_blocks() from |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3742 | * ext4_get_blocks_dio_write() when DIO to write |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3743 | * to an unwritten extent. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3744 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3745 | * Writing to an unwritten extent may result in splitting the unwritten |
| 3746 | * extent into multiple initialized/unwritten extents (up to three) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3747 | * There are three possibilities: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3748 | * a> There is no split required: Entire extent should be unwritten |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3749 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3750 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 3751 | * |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3752 | * This works the same way in the case of initialized -> unwritten conversion. |
| 3753 | * |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3754 | * 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] | 3755 | * the unwritten extent split. To prevent ENOSPC occur at the IO |
| 3756 | * complete, we need to split the unwritten extent before DIO submit |
| 3757 | * the IO. The unwritten extent called at this time will be split |
| 3758 | * into three unwritten extent(at most). After IO complete, the part |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3759 | * being filled will be convert to initialized by the end_io callback function |
| 3760 | * via ext4_convert_unwritten_extents(). |
Mingming | ba230c3 | 2009-11-06 04:01:23 -0500 | [diff] [blame] | 3761 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3762 | * Returns the size of unwritten extent to be written on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3763 | */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3764 | static int ext4_split_convert_extents(handle_t *handle, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3765 | struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3766 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3767 | struct ext4_ext_path **ppath, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3768 | int flags) |
| 3769 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3770 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3771 | ext4_lblk_t eof_block; |
| 3772 | ext4_lblk_t ee_block; |
| 3773 | struct ext4_extent *ex; |
| 3774 | unsigned int ee_len; |
| 3775 | int split_flag = 0, depth; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3776 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3777 | ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n", |
| 3778 | __func__, inode->i_ino, |
| 3779 | (unsigned long long)map->m_lblk, map->m_len); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3780 | |
| 3781 | eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> |
| 3782 | inode->i_sb->s_blocksize_bits; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3783 | if (eof_block < map->m_lblk + map->m_len) |
| 3784 | eof_block = map->m_lblk + map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3785 | /* |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3786 | * It is safe to convert extent to initialized via explicit |
| 3787 | * zeroout only if extent is fully insde i_size or new_size. |
| 3788 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3789 | depth = ext_depth(inode); |
| 3790 | ex = path[depth].p_ext; |
| 3791 | ee_block = le32_to_cpu(ex->ee_block); |
| 3792 | ee_len = ext4_ext_get_actual_len(ex); |
Dmitry Monakhov | 21ca087a | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3793 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3794 | /* Convert to unwritten */ |
| 3795 | if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { |
| 3796 | split_flag |= EXT4_EXT_DATA_VALID1; |
| 3797 | /* Convert to initialized */ |
| 3798 | } else if (flags & EXT4_GET_BLOCKS_CONVERT) { |
| 3799 | split_flag |= ee_block + ee_len <= eof_block ? |
| 3800 | EXT4_EXT_MAY_ZEROOUT : 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3801 | split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3802 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3803 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3804 | return ext4_split_extent(handle, inode, ppath, map, split_flag, flags); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3805 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3806 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3807 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3808 | struct inode *inode, |
| 3809 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3810 | struct ext4_ext_path **ppath) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3811 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3812 | struct ext4_ext_path *path = *ppath; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3813 | struct ext4_extent *ex; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3814 | ext4_lblk_t ee_block; |
| 3815 | unsigned int ee_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3816 | int depth; |
| 3817 | int err = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3818 | |
| 3819 | depth = ext_depth(inode); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3820 | ex = path[depth].p_ext; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3821 | ee_block = le32_to_cpu(ex->ee_block); |
| 3822 | ee_len = ext4_ext_get_actual_len(ex); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3823 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3824 | ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" |
| 3825 | "block %llu, max_blocks %u\n", inode->i_ino, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3826 | (unsigned long long)ee_block, ee_len); |
| 3827 | |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3828 | /* If extent is larger than requested it is a clear sign that we still |
| 3829 | * have some extent state machine issues left. So extent_split is still |
| 3830 | * required. |
| 3831 | * TODO: Once all related issues will be fixed this situation should be |
| 3832 | * illegal. |
| 3833 | */ |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3834 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Rakesh Pandit | e3d550c | 2019-08-22 22:53:46 -0400 | [diff] [blame] | 3835 | #ifdef CONFIG_EXT4_DEBUG |
| 3836 | ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu," |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 3837 | " len %u; IO logical block %llu, len %u", |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3838 | inode->i_ino, (unsigned long long)ee_block, ee_len, |
| 3839 | (unsigned long long)map->m_lblk, map->m_len); |
| 3840 | #endif |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3841 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3842 | EXT4_GET_BLOCKS_CONVERT); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3843 | if (err < 0) |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3844 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3845 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3846 | if (IS_ERR(path)) |
| 3847 | return PTR_ERR(path); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3848 | depth = ext_depth(inode); |
| 3849 | ex = path[depth].p_ext; |
| 3850 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3851 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3852 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3853 | if (err) |
| 3854 | goto out; |
| 3855 | /* first mark the extent as initialized */ |
| 3856 | ext4_ext_mark_initialized(ex); |
| 3857 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3858 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3859 | * borders are not changed |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3860 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3861 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3862 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3863 | /* Mark modified extent as dirty */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3864 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3865 | out: |
| 3866 | ext4_ext_show_leaf(inode, path); |
| 3867 | return err; |
| 3868 | } |
| 3869 | |
| 3870 | static int |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3871 | convert_initialized_extent(handle_t *handle, struct inode *inode, |
| 3872 | struct ext4_map_blocks *map, |
Eric Whitney | 29c6eaf | 2016-02-22 22:58:55 -0500 | [diff] [blame] | 3873 | struct ext4_ext_path **ppath, |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame^] | 3874 | unsigned int *allocated) |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3875 | { |
Theodore Ts'o | 4f224b8 | 2014-09-01 14:36:09 -0400 | [diff] [blame] | 3876 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3877 | struct ext4_extent *ex; |
| 3878 | ext4_lblk_t ee_block; |
| 3879 | unsigned int ee_len; |
| 3880 | int depth; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3881 | int err = 0; |
| 3882 | |
| 3883 | /* |
| 3884 | * Make sure that the extent is no bigger than we support with |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3885 | * unwritten extent |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3886 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3887 | if (map->m_len > EXT_UNWRITTEN_MAX_LEN) |
| 3888 | map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3889 | |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3890 | depth = ext_depth(inode); |
| 3891 | ex = path[depth].p_ext; |
| 3892 | ee_block = le32_to_cpu(ex->ee_block); |
| 3893 | ee_len = ext4_ext_get_actual_len(ex); |
| 3894 | |
| 3895 | ext_debug("%s: inode %lu, logical" |
| 3896 | "block %llu, max_blocks %u\n", __func__, inode->i_ino, |
| 3897 | (unsigned long long)ee_block, ee_len); |
| 3898 | |
| 3899 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3900 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3901 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN); |
| 3902 | if (err < 0) |
| 3903 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3904 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3905 | if (IS_ERR(path)) |
| 3906 | return PTR_ERR(path); |
| 3907 | depth = ext_depth(inode); |
| 3908 | ex = path[depth].p_ext; |
| 3909 | if (!ex) { |
| 3910 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3911 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3912 | return -EFSCORRUPTED; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3917 | if (err) |
| 3918 | return err; |
| 3919 | /* first mark the extent as unwritten */ |
| 3920 | ext4_ext_mark_unwritten(ex); |
| 3921 | |
| 3922 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3923 | * borders are not changed |
| 3924 | */ |
| 3925 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3926 | |
| 3927 | /* Mark modified extent as dirty */ |
| 3928 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
| 3929 | if (err) |
| 3930 | return err; |
| 3931 | ext4_ext_show_leaf(inode, path); |
| 3932 | |
| 3933 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3934 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3935 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame^] | 3936 | if (*allocated > map->m_len) |
| 3937 | *allocated = map->m_len; |
| 3938 | map->m_len = *allocated; |
| 3939 | return 0; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3940 | } |
| 3941 | |
| 3942 | static int |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3943 | ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3944 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3945 | struct ext4_ext_path **ppath, int flags, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3946 | unsigned int allocated, ext4_fsblk_t newblock) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3947 | { |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3948 | #ifdef EXT_DEBUG |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3949 | struct ext4_ext_path *path = *ppath; |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3950 | #endif |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3951 | int ret = 0; |
| 3952 | int err = 0; |
| 3953 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3954 | ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical " |
Zheng Liu | 88635ca | 2011-12-28 19:00:25 -0500 | [diff] [blame] | 3955 | "block %llu, max_blocks %u, flags %x, allocated %u\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3956 | inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3957 | flags, allocated); |
| 3958 | ext4_ext_show_leaf(inode, path); |
| 3959 | |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3960 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3961 | * When writing into unwritten space, we should not fail to |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3962 | * allocate metadata blocks for the new extent block if needed. |
| 3963 | */ |
| 3964 | flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL; |
| 3965 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3966 | trace_ext4_ext_handle_unwritten_extents(inode, map, flags, |
Zheng Liu | b564553 | 2012-11-08 14:33:43 -0500 | [diff] [blame] | 3967 | allocated, newblock); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3968 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3969 | /* get_block() before submit the IO, split the extent */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 3970 | if (flags & EXT4_GET_BLOCKS_PRE_IO) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3971 | ret = ext4_split_convert_extents(handle, inode, map, ppath, |
| 3972 | flags | EXT4_GET_BLOCKS_CONVERT); |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 3973 | if (ret <= 0) |
| 3974 | goto out; |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3975 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3976 | goto out; |
| 3977 | } |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3978 | /* IO end_io complete, convert the filled extent to written */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 3979 | if (flags & EXT4_GET_BLOCKS_CONVERT) { |
Jan Kara | c86d8db | 2015-12-07 15:10:26 -0500 | [diff] [blame] | 3980 | if (flags & EXT4_GET_BLOCKS_ZERO) { |
| 3981 | if (allocated > map->m_len) |
| 3982 | allocated = map->m_len; |
| 3983 | err = ext4_issue_zeroout(inode, map->m_lblk, newblock, |
| 3984 | allocated); |
| 3985 | if (err < 0) |
| 3986 | goto out2; |
| 3987 | } |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3988 | ret = ext4_convert_unwritten_extents_endio(handle, inode, map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3989 | ppath); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3990 | if (ret >= 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3991 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3992 | else |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3993 | err = ret; |
Zheng Liu | cdee784 | 2013-03-10 21:08:52 -0400 | [diff] [blame] | 3994 | map->m_flags |= EXT4_MAP_MAPPED; |
Eric Whitney | 15cc176 | 2014-02-12 10:42:45 -0500 | [diff] [blame] | 3995 | map->m_pblk = newblock; |
Zheng Liu | cdee784 | 2013-03-10 21:08:52 -0400 | [diff] [blame] | 3996 | if (allocated > map->m_len) |
| 3997 | allocated = map->m_len; |
| 3998 | map->m_len = allocated; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3999 | goto out2; |
| 4000 | } |
| 4001 | /* buffered IO case */ |
| 4002 | /* |
| 4003 | * repeat fallocate creation request |
| 4004 | * we already have an unwritten extent |
| 4005 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4006 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4007 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4008 | goto map_out; |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4009 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4010 | |
| 4011 | /* buffered READ or buffered write_begin() lookup */ |
| 4012 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 4013 | /* |
| 4014 | * We have blocks reserved already. We |
| 4015 | * return allocated blocks so that delalloc |
| 4016 | * won't do block reservation for us. But |
| 4017 | * the buffer head will be unmapped so that |
| 4018 | * a read from the block returns 0s. |
| 4019 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4020 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4021 | goto out1; |
| 4022 | } |
| 4023 | |
| 4024 | /* buffered write, writepage time, convert*/ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4025 | ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags); |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4026 | if (ret >= 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4027 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4028 | out: |
| 4029 | if (ret <= 0) { |
| 4030 | err = ret; |
| 4031 | goto out2; |
| 4032 | } else |
| 4033 | allocated = ret; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4034 | map->m_flags |= EXT4_MAP_NEW; |
zhangyi (F) | 16e08b1 | 2019-02-10 23:32:07 -0500 | [diff] [blame] | 4035 | if (allocated > map->m_len) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4036 | allocated = map->m_len; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 4037 | map->m_len = allocated; |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4038 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4039 | map_out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4040 | map->m_flags |= EXT4_MAP_MAPPED; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4041 | out1: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4042 | if (allocated > map->m_len) |
| 4043 | allocated = map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4044 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4045 | map->m_pblk = newblock; |
| 4046 | map->m_len = allocated; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4047 | out2: |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4048 | return err ? err : allocated; |
| 4049 | } |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 4050 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4051 | /* |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4052 | * get_implied_cluster_alloc - check to see if the requested |
| 4053 | * allocation (in the map structure) overlaps with a cluster already |
| 4054 | * allocated in an extent. |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4055 | * @sb The filesystem superblock structure |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4056 | * @map The requested lblk->pblk mapping |
| 4057 | * @ex The extent structure which might contain an implied |
| 4058 | * cluster allocation |
| 4059 | * |
| 4060 | * This function is called by ext4_ext_map_blocks() after we failed to |
| 4061 | * find blocks that were already in the inode's extent tree. Hence, |
| 4062 | * we know that the beginning of the requested region cannot overlap |
| 4063 | * the extent from the inode's extent tree. There are three cases we |
| 4064 | * want to catch. The first is this case: |
| 4065 | * |
| 4066 | * |--- cluster # N--| |
| 4067 | * |--- extent ---| |---- requested region ---| |
| 4068 | * |==========| |
| 4069 | * |
| 4070 | * The second case that we need to test for is this one: |
| 4071 | * |
| 4072 | * |--------- cluster # N ----------------| |
| 4073 | * |--- requested region --| |------- extent ----| |
| 4074 | * |=======================| |
| 4075 | * |
| 4076 | * The third case is when the requested region lies between two extents |
| 4077 | * within the same cluster: |
| 4078 | * |------------- cluster # N-------------| |
| 4079 | * |----- ex -----| |---- ex_right ----| |
| 4080 | * |------ requested region ------| |
| 4081 | * |================| |
| 4082 | * |
| 4083 | * In each of the above cases, we need to set the map->m_pblk and |
| 4084 | * map->m_len so it corresponds to the return the extent labelled as |
| 4085 | * "|====|" from cluster #N, since it is already in use for data in |
| 4086 | * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to |
| 4087 | * signal to ext4_ext_map_blocks() that map->m_pblk should be treated |
| 4088 | * as a new "allocated" block region. Otherwise, we will return 0 and |
| 4089 | * ext4_ext_map_blocks() will then allocate one or more new clusters |
| 4090 | * by calling ext4_mb_new_blocks(). |
| 4091 | */ |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4092 | static int get_implied_cluster_alloc(struct super_block *sb, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4093 | struct ext4_map_blocks *map, |
| 4094 | struct ext4_extent *ex, |
| 4095 | struct ext4_ext_path *path) |
| 4096 | { |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4097 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4098 | 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] | 4099 | ext4_lblk_t ex_cluster_start, ex_cluster_end; |
Curt Wohlgemuth | 14d7f3e | 2011-12-18 17:39:02 -0500 | [diff] [blame] | 4100 | ext4_lblk_t rr_cluster_start; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4101 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
| 4102 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
| 4103 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
| 4104 | |
| 4105 | /* The extent passed in that we are trying to match */ |
| 4106 | ex_cluster_start = EXT4_B2C(sbi, ee_block); |
| 4107 | ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); |
| 4108 | |
| 4109 | /* The requested region passed into ext4_map_blocks() */ |
| 4110 | rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4111 | |
| 4112 | if ((rr_cluster_start == ex_cluster_end) || |
| 4113 | (rr_cluster_start == ex_cluster_start)) { |
| 4114 | if (rr_cluster_start == ex_cluster_end) |
| 4115 | ee_start += ee_len - 1; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4116 | map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4117 | map->m_len = min(map->m_len, |
| 4118 | (unsigned) sbi->s_cluster_ratio - c_offset); |
| 4119 | /* |
| 4120 | * Check for and handle this case: |
| 4121 | * |
| 4122 | * |--------- cluster # N-------------| |
| 4123 | * |------- extent ----| |
| 4124 | * |--- requested region ---| |
| 4125 | * |===========| |
| 4126 | */ |
| 4127 | |
| 4128 | if (map->m_lblk < ee_block) |
| 4129 | map->m_len = min(map->m_len, ee_block - map->m_lblk); |
| 4130 | |
| 4131 | /* |
| 4132 | * Check for the case where there is already another allocated |
| 4133 | * block to the right of 'ex' but before the end of the cluster. |
| 4134 | * |
| 4135 | * |------------- cluster # N-------------| |
| 4136 | * |----- ex -----| |---- ex_right ----| |
| 4137 | * |------ requested region ------| |
| 4138 | * |================| |
| 4139 | */ |
| 4140 | if (map->m_lblk > ee_block) { |
| 4141 | ext4_lblk_t next = ext4_ext_next_allocated_block(path); |
| 4142 | map->m_len = min(map->m_len, next - map->m_lblk); |
| 4143 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4144 | |
| 4145 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4146 | return 1; |
| 4147 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4148 | |
| 4149 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4150 | return 0; |
| 4151 | } |
| 4152 | |
| 4153 | |
| 4154 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 4155 | * Block allocation/map/preallocation routine for extents based files |
| 4156 | * |
| 4157 | * |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4158 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4159 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 4160 | * (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] | 4161 | * |
| 4162 | * return > 0, number of of blocks already mapped/allocated |
| 4163 | * if create == 0 and these are pre-allocated blocks |
| 4164 | * buffer head is unmapped |
| 4165 | * otherwise blocks are mapped |
| 4166 | * |
| 4167 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 4168 | * buffer head is unmapped |
| 4169 | * |
| 4170 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4171 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4172 | int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, |
| 4173 | struct ext4_map_blocks *map, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4174 | { |
| 4175 | struct ext4_ext_path *path = NULL; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4176 | struct ext4_extent newex, *ex, *ex2; |
| 4177 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4178 | ext4_fsblk_t newblock = 0; |
Eric Whitney | ce37c42 | 2014-02-19 18:52:39 -0500 | [diff] [blame] | 4179 | int free_on_err = 0, err = 0, depth, ret; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4180 | unsigned int allocated = 0, offset = 0; |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4181 | unsigned int allocated_clusters = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4182 | struct ext4_allocation_request ar; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4183 | ext4_lblk_t cluster_offset; |
Jan Kara | cbd7584 | 2014-11-25 11:41:49 -0500 | [diff] [blame] | 4184 | bool map_from_cluster = false; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4185 | |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 4186 | ext_debug("blocks %u/%u requested for inode %lu\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4187 | map->m_lblk, map->m_len, inode->i_ino); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4188 | 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] | 4189 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4190 | /* find extent for this block */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4191 | path = ext4_find_extent(inode, map->m_lblk, NULL, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4192 | if (IS_ERR(path)) { |
| 4193 | err = PTR_ERR(path); |
| 4194 | path = NULL; |
| 4195 | goto out2; |
| 4196 | } |
| 4197 | |
| 4198 | depth = ext_depth(inode); |
| 4199 | |
| 4200 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4201 | * consistent leaf must not be empty; |
| 4202 | * this situation is possible, though, _during_ tree modification; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4203 | * this is why assert can't be put in ext4_find_extent() |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4204 | */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 4205 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 4206 | EXT4_ERROR_INODE(inode, "bad extent address " |
Theodore Ts'o | f70f362 | 2010-05-16 23:00:00 -0400 | [diff] [blame] | 4207 | "lblock: %lu, depth: %d pblock %lld", |
| 4208 | (unsigned long) map->m_lblk, depth, |
| 4209 | path[depth].p_block); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4210 | err = -EFSCORRUPTED; |
Surbhi Palande | 034fb4c | 2009-12-14 09:53:52 -0500 | [diff] [blame] | 4211 | goto out2; |
| 4212 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4213 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 4214 | ex = path[depth].p_ext; |
| 4215 | if (ex) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4216 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4217 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4218 | unsigned short ee_len; |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4219 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4220 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4221 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4222 | * unwritten extents are treated as holes, except that |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4223 | * we split out initialized portions during a write. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4224 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4225 | ee_len = ext4_ext_get_actual_len(ex); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4226 | |
| 4227 | trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); |
| 4228 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4229 | /* if found extent covers block, simply return it */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4230 | if (in_range(map->m_lblk, ee_block, ee_len)) { |
| 4231 | newblock = map->m_lblk - ee_block + ee_start; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4232 | /* number of remaining blocks in the extent */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4233 | allocated = ee_len - (map->m_lblk - ee_block); |
| 4234 | ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, |
| 4235 | ee_block, ee_len, newblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4236 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4237 | /* |
| 4238 | * If the extent is initialized check whether the |
| 4239 | * caller wants to convert it to unwritten. |
| 4240 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4241 | if ((!ext4_ext_is_unwritten(ex)) && |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4242 | (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame^] | 4243 | err = convert_initialized_extent(handle, |
| 4244 | inode, map, &path, &allocated); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4245 | goto out2; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame^] | 4246 | } else if (!ext4_ext_is_unwritten(ex)) { |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4247 | goto out; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame^] | 4248 | } |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4249 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4250 | ret = ext4_ext_handle_unwritten_extents( |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4251 | handle, inode, map, &path, flags, |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4252 | allocated, newblock); |
Eric Whitney | ce37c42 | 2014-02-19 18:52:39 -0500 | [diff] [blame] | 4253 | if (ret < 0) |
| 4254 | err = ret; |
| 4255 | else |
| 4256 | allocated = ret; |
Eric Whitney | 31cf0f2 | 2014-03-13 23:14:46 -0400 | [diff] [blame] | 4257 | goto out2; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4258 | } |
| 4259 | } |
| 4260 | |
| 4261 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4262 | * requested block isn't allocated yet; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4263 | * we couldn't try to create block if create flag is zero |
| 4264 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 4265 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4266 | ext4_lblk_t hole_start, hole_len; |
| 4267 | |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4268 | hole_start = map->m_lblk; |
| 4269 | hole_len = ext4_ext_determine_hole(inode, path, &hole_start); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4270 | /* |
| 4271 | * put just found gap into cache to speed up |
| 4272 | * subsequent requests |
| 4273 | */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4274 | ext4_ext_put_gap_in_cache(inode, hole_start, hole_len); |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4275 | |
| 4276 | /* Update hole_len to reflect hole size after map->m_lblk */ |
| 4277 | if (hole_start != map->m_lblk) |
| 4278 | hole_len -= map->m_lblk - hole_start; |
| 4279 | map->m_pblk = 0; |
| 4280 | map->m_len = min_t(unsigned int, map->m_len, hole_len); |
| 4281 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4282 | goto out2; |
| 4283 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4284 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4285 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4286 | * Okay, we need to do block allocation. |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 4287 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4288 | newex.ee_block = cpu_to_le32(map->m_lblk); |
Eric Whitney | d0abafa | 2014-01-06 14:00:23 -0500 | [diff] [blame] | 4289 | cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4290 | |
| 4291 | /* |
| 4292 | * 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] | 4293 | * by ext4_find_extent() implies a cluster we can use. |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4294 | */ |
| 4295 | if (cluster_offset && ex && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4296 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4297 | ar.len = allocated = map->m_len; |
| 4298 | newblock = map->m_pblk; |
Jan Kara | cbd7584 | 2014-11-25 11:41:49 -0500 | [diff] [blame] | 4299 | map_from_cluster = true; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4300 | goto got_allocated_blocks; |
| 4301 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4302 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4303 | /* find neighbour allocated blocks */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4304 | ar.lleft = map->m_lblk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4305 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 4306 | if (err) |
| 4307 | goto out2; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4308 | ar.lright = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4309 | ex2 = NULL; |
| 4310 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4311 | if (err) |
| 4312 | goto out2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4313 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4314 | /* Check if the extent after searching to the right implies a |
| 4315 | * cluster we can use. */ |
| 4316 | if ((sbi->s_cluster_ratio > 1) && ex2 && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4317 | get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4318 | ar.len = allocated = map->m_len; |
| 4319 | newblock = map->m_pblk; |
Jan Kara | cbd7584 | 2014-11-25 11:41:49 -0500 | [diff] [blame] | 4320 | map_from_cluster = true; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4321 | goto got_allocated_blocks; |
| 4322 | } |
| 4323 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4324 | /* |
| 4325 | * See if request is beyond maximum number of blocks we can have in |
| 4326 | * a single extent. For an initialized extent this limit is |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4327 | * EXT_INIT_MAX_LEN and for an unwritten extent this limit is |
| 4328 | * EXT_UNWRITTEN_MAX_LEN. |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4329 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4330 | if (map->m_len > EXT_INIT_MAX_LEN && |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4331 | !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4332 | map->m_len = EXT_INIT_MAX_LEN; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4333 | else if (map->m_len > EXT_UNWRITTEN_MAX_LEN && |
| 4334 | (flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
| 4335 | map->m_len = EXT_UNWRITTEN_MAX_LEN; |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4336 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4337 | /* 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] | 4338 | newex.ee_len = cpu_to_le16(map->m_len); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4339 | err = ext4_ext_check_overlap(sbi, inode, &newex, path); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4340 | if (err) |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4341 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4342 | else |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4343 | allocated = map->m_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4344 | |
| 4345 | /* allocate new block */ |
| 4346 | ar.inode = inode; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4347 | ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); |
| 4348 | ar.logical = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4349 | /* |
| 4350 | * We calculate the offset from the beginning of the cluster |
| 4351 | * for the logical block number, since when we allocate a |
| 4352 | * physical cluster, the physical block should start at the |
| 4353 | * same offset from the beginning of the cluster. This is |
| 4354 | * needed so that future calls to get_implied_cluster_alloc() |
| 4355 | * work correctly. |
| 4356 | */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4357 | offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4358 | ar.len = EXT4_NUM_B2C(sbi, offset+allocated); |
| 4359 | ar.goal -= offset; |
| 4360 | ar.logical -= offset; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4361 | if (S_ISREG(inode->i_mode)) |
| 4362 | ar.flags = EXT4_MB_HINT_DATA; |
| 4363 | else |
| 4364 | /* disable in-core preallocation for non-regular files */ |
| 4365 | ar.flags = 0; |
Vivek Haldar | 556b27a | 2011-05-25 07:41:54 -0400 | [diff] [blame] | 4366 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) |
| 4367 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 4368 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 4369 | ar.flags |= EXT4_MB_DELALLOC_RESERVED; |
Theodore Ts'o | c5e298a | 2015-06-21 01:25:29 -0400 | [diff] [blame] | 4370 | if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
| 4371 | ar.flags |= EXT4_MB_USE_RESERVED; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4372 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4373 | if (!newblock) |
| 4374 | goto out2; |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 4375 | ext_debug("allocate new block: goal %llu, found %llu/%u\n", |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4376 | ar.goal, newblock, allocated); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4377 | free_on_err = 1; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4378 | allocated_clusters = ar.len; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4379 | ar.len = EXT4_C2B(sbi, ar.len) - offset; |
| 4380 | if (ar.len > allocated) |
| 4381 | ar.len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4382 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4383 | got_allocated_blocks: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4384 | /* try to insert new extent into found leaf and return */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4385 | ext4_ext_store_pblock(&newex, newblock + offset); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4386 | newex.ee_len = cpu_to_le16(ar.len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4387 | /* Mark unwritten */ |
| 4388 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){ |
| 4389 | ext4_ext_mark_unwritten(&newex); |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4390 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4391 | } |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 4392 | |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4393 | err = 0; |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 4394 | err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags); |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 4395 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4396 | if (err && free_on_err) { |
Maxim Patlasov | 7132de7 | 2011-07-10 19:37:48 -0400 | [diff] [blame] | 4397 | int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ? |
| 4398 | EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4399 | /* free data blocks we just allocated */ |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4400 | /* not a good idea to call discard here directly, |
| 4401 | * but otherwise we'd need to call it every free() */ |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4402 | ext4_discard_preallocations(inode); |
Theodore Ts'o | c8e1513 | 2013-07-15 00:09:37 -0400 | [diff] [blame] | 4403 | ext4_free_blocks(handle, inode, NULL, newblock, |
| 4404 | EXT4_C2B(sbi, allocated_clusters), fb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4405 | goto out2; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4406 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4407 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4408 | /* previous routine could use block we allocated */ |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4409 | newblock = ext4_ext_pblock(&newex); |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4410 | allocated = ext4_ext_get_actual_len(&newex); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4411 | if (allocated > map->m_len) |
| 4412 | allocated = map->m_len; |
| 4413 | map->m_flags |= EXT4_MAP_NEW; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4414 | |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4415 | /* |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4416 | * Reduce the reserved cluster count to reflect successful deferred |
| 4417 | * allocation of delayed allocated clusters or direct allocation of |
| 4418 | * clusters discovered to be delayed allocated. Once allocated, a |
| 4419 | * cluster is not included in the reserved count. |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4420 | */ |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4421 | if (test_opt(inode->i_sb, DELALLOC) && !map_from_cluster) { |
| 4422 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4423 | /* |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4424 | * When allocating delayed allocated clusters, simply |
| 4425 | * reduce the reserved cluster count and claim quota |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4426 | */ |
| 4427 | ext4_da_update_reserve_space(inode, allocated_clusters, |
| 4428 | 1); |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4429 | } else { |
| 4430 | ext4_lblk_t lblk, len; |
| 4431 | unsigned int n; |
| 4432 | |
| 4433 | /* |
| 4434 | * When allocating non-delayed allocated clusters |
| 4435 | * (from fallocate, filemap, DIO, or clusters |
| 4436 | * allocated when delalloc has been disabled by |
| 4437 | * ext4_nonda_switch), reduce the reserved cluster |
| 4438 | * count by the number of allocated clusters that |
| 4439 | * have previously been delayed allocated. Quota |
| 4440 | * has been claimed by ext4_mb_new_blocks() above, |
| 4441 | * so release the quota reservations made for any |
| 4442 | * previously delayed allocated clusters. |
| 4443 | */ |
| 4444 | lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk); |
| 4445 | len = allocated_clusters << sbi->s_cluster_bits; |
| 4446 | n = ext4_es_delayed_clu(inode, lblk, len); |
| 4447 | if (n > 0) |
| 4448 | ext4_da_update_reserve_space(inode, (int) n, 0); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4449 | } |
| 4450 | } |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4451 | |
| 4452 | /* |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4453 | * Cache the extent and update transaction to commit on fdatasync only |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4454 | * when it is _not_ an unwritten extent. |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4455 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4456 | if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4457 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4458 | else |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4459 | ext4_update_inode_fsync_trans(handle, inode, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4460 | out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4461 | if (allocated > map->m_len) |
| 4462 | allocated = map->m_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4463 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4464 | map->m_flags |= EXT4_MAP_MAPPED; |
| 4465 | map->m_pblk = newblock; |
| 4466 | map->m_len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4467 | out2: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 4468 | ext4_ext_drop_refs(path); |
| 4469 | kfree(path); |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 4470 | |
Theodore Ts'o | 63b9996 | 2013-07-16 10:28:47 -0400 | [diff] [blame] | 4471 | trace_ext4_ext_map_blocks_exit(inode, flags, map, |
| 4472 | err ? err : allocated); |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4473 | return err ? err : allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4474 | } |
| 4475 | |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4476 | int ext4_ext_truncate(handle_t *handle, struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4477 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4478 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4479 | ext4_lblk_t last_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4480 | int err = 0; |
| 4481 | |
| 4482 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4483 | * TODO: optimization is possible here. |
| 4484 | * Probably we need not scan at all, |
| 4485 | * because page truncation is enough. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4486 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4487 | |
| 4488 | /* we have to know where to truncate from in crash case */ |
| 4489 | EXT4_I(inode)->i_disksize = inode->i_size; |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4490 | err = ext4_mark_inode_dirty(handle, inode); |
| 4491 | if (err) |
| 4492 | return err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4493 | |
| 4494 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 4495 | >> EXT4_BLOCK_SIZE_BITS(sb); |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4496 | retry: |
Zheng Liu | 51865fd | 2012-11-08 21:57:32 -0500 | [diff] [blame] | 4497 | err = ext4_es_remove_extent(inode, last_block, |
| 4498 | EXT_MAX_BLOCKS - last_block); |
Theodore Ts'o | 94eec0f | 2013-07-29 12:12:56 -0400 | [diff] [blame] | 4499 | if (err == -ENOMEM) { |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4500 | cond_resched(); |
| 4501 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 4502 | goto retry; |
| 4503 | } |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4504 | if (err) |
| 4505 | return err; |
| 4506 | return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4507 | } |
| 4508 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4509 | 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] | 4510 | ext4_lblk_t len, loff_t new_size, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4511 | int flags) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4512 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4513 | struct inode *inode = file_inode(file); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4514 | handle_t *handle; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4515 | int ret = 0; |
| 4516 | int ret2 = 0; |
| 4517 | int retries = 0; |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4518 | int depth = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4519 | struct ext4_map_blocks map; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4520 | unsigned int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4521 | loff_t epos; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4522 | |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4523 | BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4524 | map.m_lblk = offset; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4525 | map.m_len = len; |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4526 | /* |
| 4527 | * Don't normalize the request if it can fit in one extent so |
| 4528 | * that it doesn't get unnecessarily split into multiple |
| 4529 | * extents. |
| 4530 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4531 | if (len <= EXT_UNWRITTEN_MAX_LEN) |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4532 | flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; |
Dmitry Monakhov | 60d4616 | 2012-10-05 11:32:02 -0400 | [diff] [blame] | 4533 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4534 | /* |
| 4535 | * credits to insert 1 extent into extent tree |
| 4536 | */ |
| 4537 | credits = ext4_chunk_trans_blocks(inode, len); |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4538 | depth = ext_depth(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4539 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4540 | retry: |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4541 | while (ret >= 0 && len) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4542 | /* |
| 4543 | * Recalculate credits when extent tree depth changes. |
| 4544 | */ |
Dan Carpenter | 011c88e | 2016-12-03 16:46:58 -0500 | [diff] [blame] | 4545 | if (depth != ext_depth(inode)) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4546 | credits = ext4_chunk_trans_blocks(inode, len); |
| 4547 | depth = ext_depth(inode); |
| 4548 | } |
| 4549 | |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 4550 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4551 | credits); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4552 | if (IS_ERR(handle)) { |
| 4553 | ret = PTR_ERR(handle); |
| 4554 | break; |
| 4555 | } |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4556 | ret = ext4_map_blocks(handle, inode, &map, flags); |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4557 | if (ret <= 0) { |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4558 | ext4_debug("inode #%lu: block %u: len %u: " |
| 4559 | "ext4_ext_map_blocks returned %d", |
| 4560 | inode->i_ino, map.m_lblk, |
| 4561 | map.m_len, ret); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4562 | ext4_mark_inode_dirty(handle, inode); |
| 4563 | ret2 = ext4_journal_stop(handle); |
| 4564 | break; |
| 4565 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4566 | map.m_lblk += ret; |
| 4567 | map.m_len = len = len - ret; |
| 4568 | epos = (loff_t)map.m_lblk << inode->i_blkbits; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4569 | inode->i_ctime = current_time(inode); |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4570 | if (new_size) { |
| 4571 | if (epos > new_size) |
| 4572 | epos = new_size; |
| 4573 | if (ext4_update_inode_size(inode, epos) & 0x1) |
| 4574 | inode->i_mtime = inode->i_ctime; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4575 | } |
| 4576 | ext4_mark_inode_dirty(handle, inode); |
Eryu Guan | c894aa9 | 2017-12-03 22:52:51 -0500 | [diff] [blame] | 4577 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4578 | ret2 = ext4_journal_stop(handle); |
| 4579 | if (ret2) |
| 4580 | break; |
| 4581 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4582 | if (ret == -ENOSPC && |
| 4583 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 4584 | ret = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4585 | goto retry; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4586 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4587 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4588 | return ret > 0 ? ret2 : ret; |
| 4589 | } |
| 4590 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 4591 | static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len); |
| 4592 | |
| 4593 | static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len); |
| 4594 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4595 | static long ext4_zero_range(struct file *file, loff_t offset, |
| 4596 | loff_t len, int mode) |
| 4597 | { |
| 4598 | struct inode *inode = file_inode(file); |
| 4599 | handle_t *handle = NULL; |
| 4600 | unsigned int max_blocks; |
| 4601 | loff_t new_size = 0; |
| 4602 | int ret = 0; |
| 4603 | int flags; |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4604 | int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4605 | int partial_begin, partial_end; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4606 | loff_t start, end; |
| 4607 | ext4_lblk_t lblk; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4608 | unsigned int blkbits = inode->i_blkbits; |
| 4609 | |
| 4610 | trace_ext4_zero_range(inode, offset, len, mode); |
| 4611 | |
Namjae Jeon | e1ee60f | 2014-05-27 12:48:55 -0400 | [diff] [blame] | 4612 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 4613 | if (ext4_should_journal_data(inode)) { |
| 4614 | ret = ext4_force_commit(inode->i_sb); |
| 4615 | if (ret) |
| 4616 | return ret; |
| 4617 | } |
| 4618 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4619 | /* |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4620 | * Round up offset. This is not fallocate, we neet to zero out |
| 4621 | * blocks, so convert interior block aligned part of the range to |
| 4622 | * unwritten and possibly manually zero out unaligned parts of the |
| 4623 | * range. |
| 4624 | */ |
| 4625 | start = round_up(offset, 1 << blkbits); |
| 4626 | end = round_down((offset + len), 1 << blkbits); |
| 4627 | |
| 4628 | if (start < offset || end > offset + len) |
| 4629 | return -EINVAL; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4630 | partial_begin = offset & ((1 << blkbits) - 1); |
| 4631 | partial_end = (offset + len) & ((1 << blkbits) - 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4632 | |
| 4633 | lblk = start >> blkbits; |
| 4634 | max_blocks = (end >> blkbits); |
| 4635 | if (max_blocks < lblk) |
| 4636 | max_blocks = 0; |
| 4637 | else |
| 4638 | max_blocks -= lblk; |
| 4639 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4640 | inode_lock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4641 | |
| 4642 | /* |
| 4643 | * Indirect files do not support unwritten extnets |
| 4644 | */ |
| 4645 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4646 | ret = -EOPNOTSUPP; |
| 4647 | goto out_mutex; |
| 4648 | } |
| 4649 | |
| 4650 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 4651 | (offset + len > inode->i_size || |
Theodore Ts'o | 51e3ae8 | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4652 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4653 | new_size = offset + len; |
| 4654 | ret = inode_newsize_ok(inode, new_size); |
| 4655 | if (ret) |
| 4656 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4657 | } |
| 4658 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4659 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
| 4660 | if (mode & FALLOC_FL_KEEP_SIZE) |
| 4661 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
| 4662 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4663 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4664 | inode_dio_wait(inode); |
| 4665 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4666 | /* Preallocate the range including the unaligned edges */ |
| 4667 | if (partial_begin || partial_end) { |
| 4668 | ret = ext4_alloc_file_blocks(file, |
| 4669 | round_down(offset, 1 << blkbits) >> blkbits, |
| 4670 | (round_up((offset + len), 1 << blkbits) - |
| 4671 | round_down(offset, 1 << blkbits)) >> blkbits, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4672 | new_size, flags); |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4673 | if (ret) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4674 | goto out_mutex; |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4675 | |
| 4676 | } |
| 4677 | |
| 4678 | /* Zero range excluding the unaligned edges */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4679 | if (max_blocks > 0) { |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4680 | flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | |
| 4681 | EXT4_EX_NOCACHE); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4682 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4683 | /* |
| 4684 | * Prevent page faults from reinstantiating pages we have |
| 4685 | * released from page cache. |
| 4686 | */ |
| 4687 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 4688 | |
| 4689 | ret = ext4_break_layouts(inode); |
| 4690 | if (ret) { |
| 4691 | up_write(&EXT4_I(inode)->i_mmap_sem); |
| 4692 | goto out_mutex; |
| 4693 | } |
| 4694 | |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4695 | ret = ext4_update_disksize_before_punch(inode, offset, len); |
| 4696 | if (ret) { |
| 4697 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4698 | goto out_mutex; |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4699 | } |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4700 | /* Now release the pages and zero block aligned part of pages */ |
| 4701 | truncate_pagecache_range(inode, start, end - 1); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4702 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4703 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4704 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4705 | flags); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4706 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4707 | if (ret) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4708 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4709 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4710 | if (!partial_begin && !partial_end) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4711 | goto out_mutex; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4712 | |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4713 | /* |
| 4714 | * In worst case we have to writeout two nonadjacent unwritten |
| 4715 | * blocks and update the inode |
| 4716 | */ |
| 4717 | credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1; |
| 4718 | if (ext4_should_journal_data(inode)) |
| 4719 | credits += 2; |
| 4720 | handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4721 | if (IS_ERR(handle)) { |
| 4722 | ret = PTR_ERR(handle); |
| 4723 | ext4_std_error(inode->i_sb, ret); |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4724 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4725 | } |
| 4726 | |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4727 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 4728 | if (new_size) |
Dmitry Monakhov | 4631dbf | 2014-08-23 17:48:28 -0400 | [diff] [blame] | 4729 | ext4_update_inode_size(inode, new_size); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4730 | ext4_mark_inode_dirty(handle, inode); |
| 4731 | |
| 4732 | /* Zero out partial block at the edges of the range */ |
| 4733 | ret = ext4_zero_partial_blocks(handle, inode, offset, len); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 4734 | if (ret >= 0) |
| 4735 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4736 | |
| 4737 | if (file->f_flags & O_SYNC) |
| 4738 | ext4_handle_sync(handle); |
| 4739 | |
| 4740 | ext4_journal_stop(handle); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4741 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4742 | inode_unlock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4743 | return ret; |
| 4744 | } |
| 4745 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4746 | /* |
| 4747 | * preallocate space for a file. This implements ext4's fallocate file |
| 4748 | * operation, which gets called from sys_fallocate system call. |
| 4749 | * For block-mapped files, posix_fallocate should fall back to the method |
| 4750 | * of writing zeroes to the required new blocks (the same behavior which is |
| 4751 | * expected for file systems which do not support fallocate() system call). |
| 4752 | */ |
| 4753 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 4754 | { |
| 4755 | struct inode *inode = file_inode(file); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4756 | loff_t new_size = 0; |
| 4757 | unsigned int max_blocks; |
| 4758 | int ret = 0; |
| 4759 | int flags; |
| 4760 | ext4_lblk_t lblk; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4761 | unsigned int blkbits = inode->i_blkbits; |
| 4762 | |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4763 | /* |
| 4764 | * Encrypted inodes can't handle collapse range or insert |
| 4765 | * range since we would need to re-encrypt blocks with a |
| 4766 | * different IV or XTS tweak (which are based on the logical |
| 4767 | * block number). |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4768 | */ |
Chandan Rajendra | 592ddec | 2018-12-12 15:20:10 +0530 | [diff] [blame] | 4769 | if (IS_ENCRYPTED(inode) && |
Eric Biggers | 457b1e3 | 2019-12-26 09:42:16 -0600 | [diff] [blame] | 4770 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4771 | return -EOPNOTSUPP; |
| 4772 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4773 | /* Return error if mode is not supported */ |
| 4774 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4775 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | |
| 4776 | FALLOC_FL_INSERT_RANGE)) |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4777 | return -EOPNOTSUPP; |
| 4778 | |
| 4779 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 4780 | return ext4_punch_hole(inode, offset, len); |
| 4781 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4782 | ret = ext4_convert_inline_data(inode); |
| 4783 | if (ret) |
| 4784 | return ret; |
| 4785 | |
Theodore Ts'o | 40c406c | 2014-04-12 22:53:53 -0400 | [diff] [blame] | 4786 | if (mode & FALLOC_FL_COLLAPSE_RANGE) |
| 4787 | return ext4_collapse_range(inode, offset, len); |
| 4788 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4789 | if (mode & FALLOC_FL_INSERT_RANGE) |
| 4790 | return ext4_insert_range(inode, offset, len); |
| 4791 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4792 | if (mode & FALLOC_FL_ZERO_RANGE) |
| 4793 | return ext4_zero_range(file, offset, len, mode); |
| 4794 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4795 | trace_ext4_fallocate_enter(inode, offset, len, mode); |
| 4796 | lblk = offset >> blkbits; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4797 | |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4798 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4799 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4800 | if (mode & FALLOC_FL_KEEP_SIZE) |
| 4801 | flags |= EXT4_GET_BLOCKS_KEEP_SIZE; |
| 4802 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4803 | inode_lock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4804 | |
Davide Italiano | 280227a | 2015-05-02 23:21:15 -0400 | [diff] [blame] | 4805 | /* |
| 4806 | * We only support preallocation for extent-based files only |
| 4807 | */ |
| 4808 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4809 | ret = -EOPNOTSUPP; |
| 4810 | goto out; |
| 4811 | } |
| 4812 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4813 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 4814 | (offset + len > inode->i_size || |
Theodore Ts'o | 51e3ae8 | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4815 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4816 | new_size = offset + len; |
| 4817 | ret = inode_newsize_ok(inode, new_size); |
| 4818 | if (ret) |
| 4819 | goto out; |
| 4820 | } |
| 4821 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4822 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4823 | inode_dio_wait(inode); |
| 4824 | |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4825 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4826 | if (ret) |
| 4827 | goto out; |
| 4828 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4829 | if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { |
| 4830 | ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, |
| 4831 | EXT4_I(inode)->i_sync_tid); |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4832 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4833 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4834 | inode_unlock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4835 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); |
| 4836 | return ret; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4837 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4838 | |
| 4839 | /* |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4840 | * This function convert a range of blocks to written extents |
| 4841 | * The caller of this function will pass the start offset and the size. |
| 4842 | * all unwritten extents within this range will be converted to |
| 4843 | * written extents. |
| 4844 | * |
| 4845 | * This function is called from the direct IO end io call back |
| 4846 | * function, to convert the fallocated extents after IO is completed. |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 4847 | * Returns 0 on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4848 | */ |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4849 | int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode, |
| 4850 | loff_t offset, ssize_t len) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4851 | { |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4852 | unsigned int max_blocks; |
| 4853 | int ret = 0; |
| 4854 | int ret2 = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4855 | struct ext4_map_blocks map; |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4856 | unsigned int blkbits = inode->i_blkbits; |
| 4857 | unsigned int credits = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4858 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4859 | map.m_lblk = offset >> blkbits; |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4860 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
| 4861 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4862 | if (!handle) { |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4863 | /* |
| 4864 | * credits to insert 1 extent into extent tree |
| 4865 | */ |
| 4866 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 4867 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4868 | while (ret >= 0 && ret < max_blocks) { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4869 | map.m_lblk += ret; |
| 4870 | map.m_len = (max_blocks -= ret); |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4871 | if (credits) { |
| 4872 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4873 | credits); |
| 4874 | if (IS_ERR(handle)) { |
| 4875 | ret = PTR_ERR(handle); |
| 4876 | break; |
| 4877 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4878 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4879 | ret = ext4_map_blocks(handle, inode, &map, |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 4880 | EXT4_GET_BLOCKS_IO_CONVERT_EXT); |
Lukas Czerner | b06acd3 | 2013-01-28 21:21:12 -0500 | [diff] [blame] | 4881 | if (ret <= 0) |
| 4882 | ext4_warning(inode->i_sb, |
| 4883 | "inode #%lu: block %u: len %u: " |
| 4884 | "ext4_ext_map_blocks returned %d", |
| 4885 | inode->i_ino, map.m_lblk, |
| 4886 | map.m_len, ret); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4887 | ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4888 | if (credits) |
| 4889 | ret2 = ext4_journal_stop(handle); |
| 4890 | if (ret <= 0 || ret2) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4891 | break; |
| 4892 | } |
| 4893 | return ret > 0 ? ret2 : ret; |
| 4894 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4895 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4896 | int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end) |
| 4897 | { |
| 4898 | int ret, err = 0; |
Ritesh Harjani | c8cc881 | 2019-10-16 13:07:10 +0530 | [diff] [blame] | 4899 | struct ext4_io_end_vec *io_end_vec; |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4900 | |
| 4901 | /* |
| 4902 | * This is somewhat ugly but the idea is clear: When transaction is |
| 4903 | * reserved, everything goes into it. Otherwise we rather start several |
| 4904 | * smaller transactions for conversion of each extent separately. |
| 4905 | */ |
| 4906 | if (handle) { |
| 4907 | handle = ext4_journal_start_reserved(handle, |
| 4908 | EXT4_HT_EXT_CONVERT); |
| 4909 | if (IS_ERR(handle)) |
| 4910 | return PTR_ERR(handle); |
| 4911 | } |
| 4912 | |
Ritesh Harjani | c8cc881 | 2019-10-16 13:07:10 +0530 | [diff] [blame] | 4913 | list_for_each_entry(io_end_vec, &io_end->list_vec, list) { |
| 4914 | ret = ext4_convert_unwritten_extents(handle, io_end->inode, |
| 4915 | io_end_vec->offset, |
| 4916 | io_end_vec->size); |
| 4917 | if (ret) |
| 4918 | break; |
| 4919 | } |
| 4920 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4921 | if (handle) |
| 4922 | err = ext4_journal_stop(handle); |
| 4923 | |
| 4924 | return ret < 0 ? ret : err; |
| 4925 | } |
| 4926 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4927 | /* |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4928 | * If newes is not existing extent (newes->ec_pblk equals zero) find |
| 4929 | * delayed extent at start of newes and update newes accordingly and |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4930 | * return start of the next delayed extent. |
| 4931 | * |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4932 | * If newes is existing extent (newes->ec_pblk is not equal zero) |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4933 | * 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] | 4934 | * extent found. Leave newes unmodified. |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4935 | */ |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4936 | static int ext4_find_delayed_extent(struct inode *inode, |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4937 | struct extent_status *newes) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4938 | { |
Zheng Liu | b3aff3e | 2012-11-08 21:57:37 -0500 | [diff] [blame] | 4939 | struct extent_status es; |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 4940 | ext4_lblk_t block, next_del; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4941 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4942 | if (newes->es_pblk == 0) { |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 4943 | ext4_es_find_extent_range(inode, &ext4_es_is_delayed, |
| 4944 | newes->es_lblk, |
| 4945 | newes->es_lblk + newes->es_len - 1, |
| 4946 | &es); |
Yan, Zheng | e30b5dc | 2013-05-03 02:15:52 -0400 | [diff] [blame] | 4947 | |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4948 | /* |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4949 | * No extent in extent-tree contains block @newes->es_pblk, |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4950 | * 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] | 4951 | */ |
Zheng Liu | 06b0c88 | 2013-02-18 00:26:51 -0500 | [diff] [blame] | 4952 | if (es.es_len == 0) |
Zheng Liu | b3aff3e | 2012-11-08 21:57:37 -0500 | [diff] [blame] | 4953 | /* A hole found. */ |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4954 | return 0; |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4955 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4956 | if (es.es_lblk > newes->es_lblk) { |
Zheng Liu | b3aff3e | 2012-11-08 21:57:37 -0500 | [diff] [blame] | 4957 | /* A hole found. */ |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4958 | newes->es_len = min(es.es_lblk - newes->es_lblk, |
| 4959 | newes->es_len); |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4960 | return 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4961 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4962 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4963 | newes->es_len = es.es_lblk + es.es_len - newes->es_lblk; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4964 | } |
| 4965 | |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4966 | block = newes->es_lblk + newes->es_len; |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 4967 | ext4_es_find_extent_range(inode, &ext4_es_is_delayed, block, |
| 4968 | EXT_MAX_BLOCKS, &es); |
Zheng Liu | be40136 | 2013-02-18 00:27:26 -0500 | [diff] [blame] | 4969 | if (es.es_len == 0) |
| 4970 | next_del = EXT_MAX_BLOCKS; |
| 4971 | else |
| 4972 | next_del = es.es_lblk; |
| 4973 | |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4974 | return next_del; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4975 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4976 | |
Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 4977 | static int ext4_xattr_fiemap(struct inode *inode, |
| 4978 | struct fiemap_extent_info *fieinfo) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4979 | { |
| 4980 | __u64 physical = 0; |
| 4981 | __u64 length; |
| 4982 | __u32 flags = FIEMAP_EXTENT_LAST; |
| 4983 | int blockbits = inode->i_sb->s_blocksize_bits; |
| 4984 | int error = 0; |
| 4985 | |
| 4986 | /* in-inode? */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4987 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4988 | struct ext4_iloc iloc; |
| 4989 | int offset; /* offset of xattr in inode */ |
| 4990 | |
| 4991 | error = ext4_get_inode_loc(inode, &iloc); |
| 4992 | if (error) |
| 4993 | return error; |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 4994 | physical = (__u64)iloc.bh->b_blocknr << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4995 | offset = EXT4_GOOD_OLD_INODE_SIZE + |
| 4996 | EXT4_I(inode)->i_extra_isize; |
| 4997 | physical += offset; |
| 4998 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
| 4999 | flags |= FIEMAP_EXTENT_DATA_INLINE; |
Curt Wohlgemuth | fd2dd9f | 2010-04-03 17:44:16 -0400 | [diff] [blame] | 5000 | brelse(iloc.bh); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5001 | } else { /* external block */ |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 5002 | physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5003 | length = inode->i_sb->s_blocksize; |
| 5004 | } |
| 5005 | |
| 5006 | if (physical) |
| 5007 | error = fiemap_fill_next_extent(fieinfo, 0, physical, |
| 5008 | length, flags); |
| 5009 | return (error < 0 ? error : 0); |
| 5010 | } |
| 5011 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 5012 | static int _ext4_fiemap(struct inode *inode, |
| 5013 | struct fiemap_extent_info *fieinfo, |
| 5014 | __u64 start, __u64 len, |
| 5015 | int (*fill)(struct inode *, ext4_lblk_t, |
| 5016 | ext4_lblk_t, |
| 5017 | struct fiemap_extent_info *)) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5018 | { |
| 5019 | ext4_lblk_t start_blk; |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 5020 | u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR; |
| 5021 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5022 | int error = 0; |
| 5023 | |
Tao Ma | 9419198 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 5024 | if (ext4_has_inline_data(inode)) { |
| 5025 | int has_inline = 1; |
| 5026 | |
Dmitry Monakhov | d952d69 | 2014-12-02 16:11:20 -0500 | [diff] [blame] | 5027 | error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline, |
| 5028 | start, len); |
Tao Ma | 9419198 | 2012-12-10 14:06:02 -0500 | [diff] [blame] | 5029 | |
| 5030 | if (has_inline) |
| 5031 | return error; |
| 5032 | } |
| 5033 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 5034 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
| 5035 | error = ext4_ext_precache(inode); |
| 5036 | if (error) |
| 5037 | return error; |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 5038 | fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 5039 | } |
| 5040 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5041 | /* fallback to generic here if not in extents fmt */ |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 5042 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && |
| 5043 | fill == ext4_fill_fiemap_extents) |
Theodore Ts'o | ad7fefb | 2015-01-02 15:16:00 -0500 | [diff] [blame] | 5044 | return generic_block_fiemap(inode, fieinfo, start, len, |
| 5045 | ext4_get_block); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5046 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 5047 | if (fill == ext4_fill_es_cache_info) |
| 5048 | ext4_fiemap_flags &= FIEMAP_FLAG_XATTR; |
| 5049 | if (fiemap_check_flags(fieinfo, ext4_fiemap_flags)) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5050 | return -EBADR; |
| 5051 | |
| 5052 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 5053 | error = ext4_xattr_fiemap(inode, fieinfo); |
| 5054 | } else { |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 5055 | ext4_lblk_t len_blks; |
| 5056 | __u64 last_blk; |
| 5057 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5058 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 5059 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 5060 | if (last_blk >= EXT_MAX_BLOCKS) |
| 5061 | last_blk = EXT_MAX_BLOCKS-1; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 5062 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5063 | |
| 5064 | /* |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 5065 | * Walk the extent tree gathering extent information |
| 5066 | * and pushing extents back to the user. |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5067 | */ |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 5068 | error = fill(inode, start_blk, len_blks, fieinfo); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5069 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 5070 | return error; |
| 5071 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5072 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 5073 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 5074 | __u64 start, __u64 len) |
| 5075 | { |
| 5076 | return _ext4_fiemap(inode, fieinfo, start, len, |
| 5077 | ext4_fill_fiemap_extents); |
| 5078 | } |
| 5079 | |
| 5080 | int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 5081 | __u64 start, __u64 len) |
| 5082 | { |
| 5083 | if (ext4_has_inline_data(inode)) { |
| 5084 | int has_inline; |
| 5085 | |
| 5086 | down_read(&EXT4_I(inode)->xattr_sem); |
| 5087 | has_inline = ext4_has_inline_data(inode); |
| 5088 | up_read(&EXT4_I(inode)->xattr_sem); |
| 5089 | if (has_inline) |
| 5090 | return 0; |
| 5091 | } |
| 5092 | |
| 5093 | return _ext4_fiemap(inode, fieinfo, start, len, |
| 5094 | ext4_fill_es_cache_info); |
| 5095 | } |
| 5096 | |
| 5097 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5098 | /* |
| 5099 | * ext4_access_path: |
| 5100 | * Function to access the path buffer for marking it dirty. |
| 5101 | * It also checks if there are sufficient credits left in the journal handle |
| 5102 | * to update path. |
| 5103 | */ |
| 5104 | static int |
| 5105 | ext4_access_path(handle_t *handle, struct inode *inode, |
| 5106 | struct ext4_ext_path *path) |
| 5107 | { |
| 5108 | int credits, err; |
| 5109 | |
| 5110 | if (!ext4_handle_valid(handle)) |
| 5111 | return 0; |
| 5112 | |
| 5113 | /* |
| 5114 | * Check if need to extend journal credits |
| 5115 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 5116 | * descriptor) for each block group; assume two block |
| 5117 | * groups |
| 5118 | */ |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 5119 | credits = ext4_writepage_trans_blocks(inode); |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 5120 | err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 5121 | if (err < 0) |
| 5122 | return err; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5123 | |
| 5124 | err = ext4_ext_get_access(handle, inode, path); |
| 5125 | return err; |
| 5126 | } |
| 5127 | |
| 5128 | /* |
| 5129 | * ext4_ext_shift_path_extents: |
| 5130 | * 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] | 5131 | * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells |
| 5132 | * if it is right shift or left shift operation. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5133 | */ |
| 5134 | static int |
| 5135 | ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift, |
| 5136 | struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5137 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5138 | { |
| 5139 | int depth, err = 0; |
| 5140 | struct ext4_extent *ex_start, *ex_last; |
zhengbin | 4756ee1 | 2019-12-25 10:45:59 +0800 | [diff] [blame] | 5141 | bool update = false; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5142 | depth = path->p_depth; |
| 5143 | |
| 5144 | while (depth >= 0) { |
| 5145 | if (depth == path->p_depth) { |
| 5146 | ex_start = path[depth].p_ext; |
| 5147 | if (!ex_start) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 5148 | return -EFSCORRUPTED; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5149 | |
| 5150 | ex_last = EXT_LAST_EXTENT(path[depth].p_hdr); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5151 | |
| 5152 | err = ext4_access_path(handle, inode, path + depth); |
| 5153 | if (err) |
| 5154 | goto out; |
| 5155 | |
| 5156 | if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) |
zhengbin | 4756ee1 | 2019-12-25 10:45:59 +0800 | [diff] [blame] | 5157 | update = true; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5158 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5159 | while (ex_start <= ex_last) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5160 | if (SHIFT == SHIFT_LEFT) { |
| 5161 | le32_add_cpu(&ex_start->ee_block, |
| 5162 | -shift); |
| 5163 | /* Try to merge to the left. */ |
| 5164 | if ((ex_start > |
| 5165 | EXT_FIRST_EXTENT(path[depth].p_hdr)) |
| 5166 | && |
| 5167 | ext4_ext_try_to_merge_right(inode, |
| 5168 | path, ex_start - 1)) |
| 5169 | ex_last--; |
| 5170 | else |
| 5171 | ex_start++; |
| 5172 | } else { |
| 5173 | le32_add_cpu(&ex_last->ee_block, shift); |
| 5174 | ext4_ext_try_to_merge_right(inode, path, |
| 5175 | ex_last); |
Lukas Czerner | 6dd834e | 2014-04-18 10:55:24 -0400 | [diff] [blame] | 5176 | ex_last--; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5177 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5178 | } |
| 5179 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5180 | if (err) |
| 5181 | goto out; |
| 5182 | |
| 5183 | if (--depth < 0 || !update) |
| 5184 | break; |
| 5185 | } |
| 5186 | |
| 5187 | /* Update index too */ |
| 5188 | err = ext4_access_path(handle, inode, path + depth); |
| 5189 | if (err) |
| 5190 | goto out; |
| 5191 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5192 | if (SHIFT == SHIFT_LEFT) |
| 5193 | le32_add_cpu(&path[depth].p_idx->ei_block, -shift); |
| 5194 | else |
| 5195 | le32_add_cpu(&path[depth].p_idx->ei_block, shift); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5196 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5197 | if (err) |
| 5198 | goto out; |
| 5199 | |
| 5200 | /* we are done if current index is not a starting index */ |
| 5201 | if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr)) |
| 5202 | break; |
| 5203 | |
| 5204 | depth--; |
| 5205 | } |
| 5206 | |
| 5207 | out: |
| 5208 | return err; |
| 5209 | } |
| 5210 | |
| 5211 | /* |
| 5212 | * ext4_ext_shift_extents: |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5213 | * All the extents which lies in the range from @start to the last allocated |
| 5214 | * block for the @inode are shifted either towards left or right (depending |
| 5215 | * upon @SHIFT) by @shift blocks. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5216 | * On success, 0 is returned, error otherwise. |
| 5217 | */ |
| 5218 | static int |
| 5219 | ext4_ext_shift_extents(struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5220 | ext4_lblk_t start, ext4_lblk_t shift, |
| 5221 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5222 | { |
| 5223 | struct ext4_ext_path *path; |
| 5224 | int ret = 0, depth; |
| 5225 | struct ext4_extent *extent; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5226 | ext4_lblk_t stop, *iterator, ex_start, ex_end; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5227 | |
| 5228 | /* Let path point to the last extent */ |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5229 | path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, |
| 5230 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5231 | if (IS_ERR(path)) |
| 5232 | return PTR_ERR(path); |
| 5233 | |
| 5234 | depth = path->p_depth; |
| 5235 | extent = path[depth].p_ext; |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5236 | if (!extent) |
| 5237 | goto out; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5238 | |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5239 | stop = le32_to_cpu(extent->ee_block); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5240 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5241 | /* |
Eric Biggers | 349fa7d | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5242 | * For left shifts, make sure the hole on the left is big enough to |
| 5243 | * accommodate the shift. For right shifts, make sure the last extent |
| 5244 | * won't be shifted beyond EXT_MAX_BLOCKS. |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5245 | */ |
| 5246 | if (SHIFT == SHIFT_LEFT) { |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5247 | path = ext4_find_extent(inode, start - 1, &path, |
| 5248 | EXT4_EX_NOCACHE); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5249 | if (IS_ERR(path)) |
| 5250 | return PTR_ERR(path); |
| 5251 | depth = path->p_depth; |
| 5252 | extent = path[depth].p_ext; |
| 5253 | if (extent) { |
| 5254 | ex_start = le32_to_cpu(extent->ee_block); |
| 5255 | ex_end = le32_to_cpu(extent->ee_block) + |
| 5256 | ext4_ext_get_actual_len(extent); |
| 5257 | } else { |
| 5258 | ex_start = 0; |
| 5259 | ex_end = 0; |
| 5260 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5261 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5262 | if ((start == ex_start && shift > ex_start) || |
| 5263 | (shift > start - ex_end)) { |
Eric Biggers | 349fa7d | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5264 | ret = -EINVAL; |
| 5265 | goto out; |
| 5266 | } |
| 5267 | } else { |
| 5268 | if (shift > EXT_MAX_BLOCKS - |
| 5269 | (stop + ext4_ext_get_actual_len(extent))) { |
| 5270 | ret = -EINVAL; |
| 5271 | goto out; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5272 | } |
Dmitry Monakhov | 8dc79ec | 2014-04-13 15:05:42 -0400 | [diff] [blame] | 5273 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5274 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5275 | /* |
| 5276 | * In case of left shift, iterator points to start and it is increased |
| 5277 | * till we reach stop. In case of right shift, iterator points to stop |
| 5278 | * and it is decreased till we reach start. |
| 5279 | */ |
| 5280 | if (SHIFT == SHIFT_LEFT) |
| 5281 | iterator = &start; |
| 5282 | else |
| 5283 | iterator = &stop; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5284 | |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5285 | /* |
| 5286 | * Its safe to start updating extents. Start and stop are unsigned, so |
| 5287 | * in case of right shift if extent with 0 block is reached, iterator |
| 5288 | * becomes NULL to indicate the end of the loop. |
| 5289 | */ |
| 5290 | while (iterator && start <= stop) { |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5291 | path = ext4_find_extent(inode, *iterator, &path, |
| 5292 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5293 | if (IS_ERR(path)) |
| 5294 | return PTR_ERR(path); |
| 5295 | depth = path->p_depth; |
| 5296 | extent = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5297 | if (!extent) { |
| 5298 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5299 | (unsigned long) *iterator); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 5300 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5301 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5302 | if (SHIFT == SHIFT_LEFT && *iterator > |
| 5303 | le32_to_cpu(extent->ee_block)) { |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5304 | /* Hole, move to the next extent */ |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5305 | if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 5306 | path[depth].p_ext++; |
| 5307 | } else { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5308 | *iterator = ext4_ext_next_allocated_block(path); |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5309 | continue; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5310 | } |
| 5311 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5312 | |
| 5313 | if (SHIFT == SHIFT_LEFT) { |
| 5314 | extent = EXT_LAST_EXTENT(path[depth].p_hdr); |
| 5315 | *iterator = le32_to_cpu(extent->ee_block) + |
| 5316 | ext4_ext_get_actual_len(extent); |
| 5317 | } else { |
| 5318 | extent = EXT_FIRST_EXTENT(path[depth].p_hdr); |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5319 | if (le32_to_cpu(extent->ee_block) > 0) |
| 5320 | *iterator = le32_to_cpu(extent->ee_block) - 1; |
| 5321 | else |
| 5322 | /* Beginning is reached, end of the loop */ |
| 5323 | iterator = NULL; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5324 | /* Update path extent in case we need to stop */ |
| 5325 | while (le32_to_cpu(extent->ee_block) < start) |
| 5326 | extent++; |
| 5327 | path[depth].p_ext = extent; |
| 5328 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5329 | ret = ext4_ext_shift_path_extents(path, shift, inode, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5330 | handle, SHIFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5331 | if (ret) |
| 5332 | break; |
| 5333 | } |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5334 | out: |
| 5335 | ext4_ext_drop_refs(path); |
| 5336 | kfree(path); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5337 | return ret; |
| 5338 | } |
| 5339 | |
| 5340 | /* |
| 5341 | * ext4_collapse_range: |
| 5342 | * This implements the fallocate's collapse range functionality for ext4 |
| 5343 | * Returns: 0 and non-zero on error. |
| 5344 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 5345 | static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5346 | { |
| 5347 | struct super_block *sb = inode->i_sb; |
| 5348 | ext4_lblk_t punch_start, punch_stop; |
| 5349 | handle_t *handle; |
| 5350 | unsigned int credits; |
Namjae Jeon | a8680e0 | 2014-04-19 16:37:31 -0400 | [diff] [blame] | 5351 | loff_t new_size, ioffset; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5352 | int ret; |
| 5353 | |
Theodore Ts'o | b9576fc | 2015-05-15 00:24:10 -0400 | [diff] [blame] | 5354 | /* |
| 5355 | * We need to test this early because xfstests assumes that a |
| 5356 | * collapse range of (0, 1) will return EOPNOTSUPP if the file |
| 5357 | * system does not support collapse range. |
| 5358 | */ |
| 5359 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5360 | return -EOPNOTSUPP; |
| 5361 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5362 | /* Collapse range works only on fs cluster size aligned regions. */ |
| 5363 | if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5364 | return -EINVAL; |
| 5365 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5366 | trace_ext4_collapse_range(inode, offset, len); |
| 5367 | |
| 5368 | punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5369 | punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5370 | |
Namjae Jeon | 1ce01c4 | 2014-04-10 22:58:20 -0400 | [diff] [blame] | 5371 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 5372 | if (ext4_should_journal_data(inode)) { |
| 5373 | ret = ext4_force_commit(inode->i_sb); |
| 5374 | if (ret) |
| 5375 | return ret; |
| 5376 | } |
| 5377 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5378 | inode_lock(inode); |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5379 | /* |
| 5380 | * There is no need to overlap collapse range with EOF, in which case |
| 5381 | * it is effectively a truncate operation |
| 5382 | */ |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5383 | if (offset + len >= inode->i_size) { |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5384 | ret = -EINVAL; |
| 5385 | goto out_mutex; |
| 5386 | } |
| 5387 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5388 | /* Currently just for extent based files */ |
| 5389 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5390 | ret = -EOPNOTSUPP; |
| 5391 | goto out_mutex; |
| 5392 | } |
| 5393 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5394 | /* Wait for existing dio to complete */ |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5395 | inode_dio_wait(inode); |
| 5396 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5397 | /* |
| 5398 | * Prevent page faults from reinstantiating pages we have released from |
| 5399 | * page cache. |
| 5400 | */ |
| 5401 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 5402 | |
| 5403 | ret = ext4_break_layouts(inode); |
| 5404 | if (ret) |
| 5405 | goto out_mmap; |
| 5406 | |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5407 | /* |
| 5408 | * Need to round down offset to be aligned with page size boundary |
| 5409 | * for page size > block size. |
| 5410 | */ |
| 5411 | ioffset = round_down(offset, PAGE_SIZE); |
| 5412 | /* |
| 5413 | * Write tail of the last page before removed range since it will get |
| 5414 | * removed from the page cache below. |
| 5415 | */ |
| 5416 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset); |
| 5417 | if (ret) |
| 5418 | goto out_mmap; |
| 5419 | /* |
| 5420 | * Write data that will be shifted to preserve them when discarding |
| 5421 | * page cache below. We are also protected from pages becoming dirty |
| 5422 | * by i_mmap_sem. |
| 5423 | */ |
| 5424 | ret = filemap_write_and_wait_range(inode->i_mapping, offset + len, |
| 5425 | LLONG_MAX); |
| 5426 | if (ret) |
| 5427 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5428 | truncate_pagecache(inode, ioffset); |
| 5429 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5430 | credits = ext4_writepage_trans_blocks(inode); |
| 5431 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5432 | if (IS_ERR(handle)) { |
| 5433 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5434 | goto out_mmap; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5435 | } |
| 5436 | |
| 5437 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5438 | ext4_discard_preallocations(inode); |
| 5439 | |
| 5440 | ret = ext4_es_remove_extent(inode, punch_start, |
Lukas Czerner | 2c1d232 | 2014-04-18 10:43:21 -0400 | [diff] [blame] | 5441 | EXT_MAX_BLOCKS - punch_start); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5442 | if (ret) { |
| 5443 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5444 | goto out_stop; |
| 5445 | } |
| 5446 | |
| 5447 | ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1); |
| 5448 | if (ret) { |
| 5449 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5450 | goto out_stop; |
| 5451 | } |
Lukas Czerner | ef24f6c | 2014-04-18 10:50:23 -0400 | [diff] [blame] | 5452 | ext4_discard_preallocations(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5453 | |
| 5454 | ret = ext4_ext_shift_extents(inode, handle, punch_stop, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5455 | punch_stop - punch_start, SHIFT_LEFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5456 | if (ret) { |
| 5457 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5458 | goto out_stop; |
| 5459 | } |
| 5460 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5461 | new_size = inode->i_size - len; |
Lukas Czerner | 9337d5d | 2014-04-18 10:48:25 -0400 | [diff] [blame] | 5462 | i_size_write(inode, new_size); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5463 | EXT4_I(inode)->i_disksize = new_size; |
| 5464 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5465 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5466 | if (IS_SYNC(inode)) |
| 5467 | ext4_handle_sync(handle); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 5468 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5469 | ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5470 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5471 | |
| 5472 | out_stop: |
| 5473 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5474 | out_mmap: |
| 5475 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5476 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5477 | inode_unlock(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5478 | return ret; |
| 5479 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5480 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5481 | /* |
| 5482 | * ext4_insert_range: |
| 5483 | * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate. |
| 5484 | * The data blocks starting from @offset to the EOF are shifted by @len |
| 5485 | * towards right to create a hole in the @inode. Inode size is increased |
| 5486 | * by len bytes. |
| 5487 | * Returns 0 on success, error otherwise. |
| 5488 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 5489 | static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len) |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5490 | { |
| 5491 | struct super_block *sb = inode->i_sb; |
| 5492 | handle_t *handle; |
| 5493 | struct ext4_ext_path *path; |
| 5494 | struct ext4_extent *extent; |
| 5495 | ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0; |
| 5496 | unsigned int credits, ee_len; |
| 5497 | int ret = 0, depth, split_flag = 0; |
| 5498 | loff_t ioffset; |
| 5499 | |
| 5500 | /* |
| 5501 | * We need to test this early because xfstests assumes that an |
| 5502 | * insert range of (0, 1) will return EOPNOTSUPP if the file |
| 5503 | * system does not support insert range. |
| 5504 | */ |
| 5505 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5506 | return -EOPNOTSUPP; |
| 5507 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5508 | /* Insert range works only on fs cluster size aligned regions. */ |
| 5509 | if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5510 | return -EINVAL; |
| 5511 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5512 | trace_ext4_insert_range(inode, offset, len); |
| 5513 | |
| 5514 | offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5515 | len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5516 | |
| 5517 | /* Call ext4_force_commit to flush all data in case of data=journal */ |
| 5518 | if (ext4_should_journal_data(inode)) { |
| 5519 | ret = ext4_force_commit(inode->i_sb); |
| 5520 | if (ret) |
| 5521 | return ret; |
| 5522 | } |
| 5523 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5524 | inode_lock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5525 | /* Currently just for extent based files */ |
| 5526 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5527 | ret = -EOPNOTSUPP; |
| 5528 | goto out_mutex; |
| 5529 | } |
| 5530 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5531 | /* Check whether the maximum file size would be exceeded */ |
| 5532 | if (len > inode->i_sb->s_maxbytes - inode->i_size) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5533 | ret = -EFBIG; |
| 5534 | goto out_mutex; |
| 5535 | } |
| 5536 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5537 | /* Offset must be less than i_size */ |
| 5538 | if (offset >= inode->i_size) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5539 | ret = -EINVAL; |
| 5540 | goto out_mutex; |
| 5541 | } |
| 5542 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5543 | /* Wait for existing dio to complete */ |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5544 | inode_dio_wait(inode); |
| 5545 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5546 | /* |
| 5547 | * Prevent page faults from reinstantiating pages we have released from |
| 5548 | * page cache. |
| 5549 | */ |
| 5550 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 5551 | |
| 5552 | ret = ext4_break_layouts(inode); |
| 5553 | if (ret) |
| 5554 | goto out_mmap; |
| 5555 | |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5556 | /* |
| 5557 | * Need to round down to align start offset to page size boundary |
| 5558 | * for page size > block size. |
| 5559 | */ |
| 5560 | ioffset = round_down(offset, PAGE_SIZE); |
| 5561 | /* Write out all dirty pages */ |
| 5562 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, |
| 5563 | LLONG_MAX); |
| 5564 | if (ret) |
| 5565 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5566 | truncate_pagecache(inode, ioffset); |
| 5567 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5568 | credits = ext4_writepage_trans_blocks(inode); |
| 5569 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5570 | if (IS_ERR(handle)) { |
| 5571 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5572 | goto out_mmap; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | /* Expand file to avoid data loss if there is error while shifting */ |
| 5576 | inode->i_size += len; |
| 5577 | EXT4_I(inode)->i_disksize += len; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 5578 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5579 | ret = ext4_mark_inode_dirty(handle, inode); |
| 5580 | if (ret) |
| 5581 | goto out_stop; |
| 5582 | |
| 5583 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5584 | ext4_discard_preallocations(inode); |
| 5585 | |
| 5586 | path = ext4_find_extent(inode, offset_lblk, NULL, 0); |
| 5587 | if (IS_ERR(path)) { |
| 5588 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5589 | goto out_stop; |
| 5590 | } |
| 5591 | |
| 5592 | depth = ext_depth(inode); |
| 5593 | extent = path[depth].p_ext; |
| 5594 | if (extent) { |
| 5595 | ee_start_lblk = le32_to_cpu(extent->ee_block); |
| 5596 | ee_len = ext4_ext_get_actual_len(extent); |
| 5597 | |
| 5598 | /* |
| 5599 | * If offset_lblk is not the starting block of extent, split |
| 5600 | * the extent @offset_lblk |
| 5601 | */ |
| 5602 | if ((offset_lblk > ee_start_lblk) && |
| 5603 | (offset_lblk < (ee_start_lblk + ee_len))) { |
| 5604 | if (ext4_ext_is_unwritten(extent)) |
| 5605 | split_flag = EXT4_EXT_MARK_UNWRIT1 | |
| 5606 | EXT4_EXT_MARK_UNWRIT2; |
| 5607 | ret = ext4_split_extent_at(handle, inode, &path, |
| 5608 | offset_lblk, split_flag, |
| 5609 | EXT4_EX_NOCACHE | |
| 5610 | EXT4_GET_BLOCKS_PRE_IO | |
| 5611 | EXT4_GET_BLOCKS_METADATA_NOFAIL); |
| 5612 | } |
| 5613 | |
| 5614 | ext4_ext_drop_refs(path); |
| 5615 | kfree(path); |
| 5616 | if (ret < 0) { |
| 5617 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5618 | goto out_stop; |
| 5619 | } |
Fabian Frederick | edf15aa1 | 2016-09-15 11:39:52 -0400 | [diff] [blame] | 5620 | } else { |
| 5621 | ext4_ext_drop_refs(path); |
| 5622 | kfree(path); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | ret = ext4_es_remove_extent(inode, offset_lblk, |
| 5626 | EXT_MAX_BLOCKS - offset_lblk); |
| 5627 | if (ret) { |
| 5628 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5629 | goto out_stop; |
| 5630 | } |
| 5631 | |
| 5632 | /* |
| 5633 | * if offset_lblk lies in a hole which is at start of file, use |
| 5634 | * ee_start_lblk to shift extents |
| 5635 | */ |
| 5636 | ret = ext4_ext_shift_extents(inode, handle, |
| 5637 | ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk, |
| 5638 | len_lblk, SHIFT_RIGHT); |
| 5639 | |
| 5640 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5641 | if (IS_SYNC(inode)) |
| 5642 | ext4_handle_sync(handle); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5643 | if (ret >= 0) |
| 5644 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5645 | |
| 5646 | out_stop: |
| 5647 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5648 | out_mmap: |
| 5649 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5650 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5651 | inode_unlock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5652 | return ret; |
| 5653 | } |
| 5654 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5655 | /** |
Theodore Ts'o | c60990b | 2019-06-19 16:30:03 -0400 | [diff] [blame] | 5656 | * ext4_swap_extents() - Swap extents between two inodes |
| 5657 | * @handle: handle for this transaction |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5658 | * @inode1: First inode |
| 5659 | * @inode2: Second inode |
| 5660 | * @lblk1: Start block for first inode |
| 5661 | * @lblk2: Start block for second inode |
| 5662 | * @count: Number of blocks to swap |
zhenwei.pi | dcae058 | 2018-03-26 01:44:03 -0400 | [diff] [blame] | 5663 | * @unwritten: Mark second inode's extents as unwritten after swap |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5664 | * @erp: Pointer to save error value |
| 5665 | * |
| 5666 | * This helper routine does exactly what is promise "swap extents". All other |
| 5667 | * stuff such as page-cache locking consistency, bh mapping consistency or |
| 5668 | * extent's data copying must be performed by caller. |
| 5669 | * Locking: |
| 5670 | * i_mutex is held for both inodes |
| 5671 | * i_data_sem is locked for write for both inodes |
| 5672 | * Assumptions: |
| 5673 | * All pages from requested range are locked for both inodes |
| 5674 | */ |
| 5675 | int |
| 5676 | ext4_swap_extents(handle_t *handle, struct inode *inode1, |
zhenwei.pi | dcae058 | 2018-03-26 01:44:03 -0400 | [diff] [blame] | 5677 | struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5678 | ext4_lblk_t count, int unwritten, int *erp) |
| 5679 | { |
| 5680 | struct ext4_ext_path *path1 = NULL; |
| 5681 | struct ext4_ext_path *path2 = NULL; |
| 5682 | int replaced_count = 0; |
| 5683 | |
| 5684 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); |
| 5685 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5686 | BUG_ON(!inode_is_locked(inode1)); |
| 5687 | BUG_ON(!inode_is_locked(inode2)); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5688 | |
| 5689 | *erp = ext4_es_remove_extent(inode1, lblk1, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5690 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5691 | return 0; |
| 5692 | *erp = ext4_es_remove_extent(inode2, lblk2, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5693 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5694 | return 0; |
| 5695 | |
| 5696 | while (count) { |
| 5697 | struct ext4_extent *ex1, *ex2, tmp_ex; |
| 5698 | ext4_lblk_t e1_blk, e2_blk; |
| 5699 | int e1_len, e2_len, len; |
| 5700 | int split = 0; |
| 5701 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5702 | path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5703 | if (IS_ERR(path1)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5704 | *erp = PTR_ERR(path1); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5705 | path1 = NULL; |
| 5706 | finish: |
| 5707 | count = 0; |
| 5708 | goto repeat; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5709 | } |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5710 | path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5711 | if (IS_ERR(path2)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5712 | *erp = PTR_ERR(path2); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5713 | path2 = NULL; |
| 5714 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5715 | } |
| 5716 | ex1 = path1[path1->p_depth].p_ext; |
| 5717 | ex2 = path2[path2->p_depth].p_ext; |
| 5718 | /* Do we have somthing to swap ? */ |
| 5719 | if (unlikely(!ex2 || !ex1)) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5720 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5721 | |
| 5722 | e1_blk = le32_to_cpu(ex1->ee_block); |
| 5723 | e2_blk = le32_to_cpu(ex2->ee_block); |
| 5724 | e1_len = ext4_ext_get_actual_len(ex1); |
| 5725 | e2_len = ext4_ext_get_actual_len(ex2); |
| 5726 | |
| 5727 | /* Hole handling */ |
| 5728 | if (!in_range(lblk1, e1_blk, e1_len) || |
| 5729 | !in_range(lblk2, e2_blk, e2_len)) { |
| 5730 | ext4_lblk_t next1, next2; |
| 5731 | |
| 5732 | /* if hole after extent, then go to next extent */ |
| 5733 | next1 = ext4_ext_next_allocated_block(path1); |
| 5734 | next2 = ext4_ext_next_allocated_block(path2); |
| 5735 | /* If hole before extent, then shift to that extent */ |
| 5736 | if (e1_blk > lblk1) |
| 5737 | next1 = e1_blk; |
| 5738 | if (e2_blk > lblk2) |
Maninder Singh | 4e56201 | 2017-08-06 01:33:07 -0400 | [diff] [blame] | 5739 | next2 = e2_blk; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5740 | /* Do we have something to swap */ |
| 5741 | if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5742 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5743 | /* Move to the rightest boundary */ |
| 5744 | len = next1 - lblk1; |
| 5745 | if (len < next2 - lblk2) |
| 5746 | len = next2 - lblk2; |
| 5747 | if (len > count) |
| 5748 | len = count; |
| 5749 | lblk1 += len; |
| 5750 | lblk2 += len; |
| 5751 | count -= len; |
| 5752 | goto repeat; |
| 5753 | } |
| 5754 | |
| 5755 | /* Prepare left boundary */ |
| 5756 | if (e1_blk < lblk1) { |
| 5757 | split = 1; |
| 5758 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5759 | &path1, lblk1, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5760 | if (unlikely(*erp)) |
| 5761 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5762 | } |
| 5763 | if (e2_blk < lblk2) { |
| 5764 | split = 1; |
| 5765 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5766 | &path2, lblk2, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5767 | if (unlikely(*erp)) |
| 5768 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5769 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5770 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5771 | * path must to be revalidated. */ |
| 5772 | if (split) |
| 5773 | goto repeat; |
| 5774 | |
| 5775 | /* Prepare right boundary */ |
| 5776 | len = count; |
| 5777 | if (len > e1_blk + e1_len - lblk1) |
| 5778 | len = e1_blk + e1_len - lblk1; |
| 5779 | if (len > e2_blk + e2_len - lblk2) |
| 5780 | len = e2_blk + e2_len - lblk2; |
| 5781 | |
| 5782 | if (len != e1_len) { |
| 5783 | split = 1; |
| 5784 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5785 | &path1, lblk1 + len, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5786 | if (unlikely(*erp)) |
| 5787 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5788 | } |
| 5789 | if (len != e2_len) { |
| 5790 | split = 1; |
| 5791 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5792 | &path2, lblk2 + len, 0); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5793 | if (*erp) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5794 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5795 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5796 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5797 | * path must to be revalidated. */ |
| 5798 | if (split) |
| 5799 | goto repeat; |
| 5800 | |
| 5801 | BUG_ON(e2_len != e1_len); |
| 5802 | *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5803 | if (unlikely(*erp)) |
| 5804 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5805 | *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5806 | if (unlikely(*erp)) |
| 5807 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5808 | |
| 5809 | /* Both extents are fully inside boundaries. Swap it now */ |
| 5810 | tmp_ex = *ex1; |
| 5811 | ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2)); |
| 5812 | ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex)); |
| 5813 | ex1->ee_len = cpu_to_le16(e2_len); |
| 5814 | ex2->ee_len = cpu_to_le16(e1_len); |
| 5815 | if (unwritten) |
| 5816 | ext4_ext_mark_unwritten(ex2); |
| 5817 | if (ext4_ext_is_unwritten(&tmp_ex)) |
| 5818 | ext4_ext_mark_unwritten(ex1); |
| 5819 | |
| 5820 | ext4_ext_try_to_merge(handle, inode2, path2, ex2); |
| 5821 | ext4_ext_try_to_merge(handle, inode1, path1, ex1); |
| 5822 | *erp = ext4_ext_dirty(handle, inode2, path2 + |
| 5823 | path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5824 | if (unlikely(*erp)) |
| 5825 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5826 | *erp = ext4_ext_dirty(handle, inode1, path1 + |
| 5827 | path1->p_depth); |
| 5828 | /* |
| 5829 | * Looks scarry ah..? second inode already points to new blocks, |
| 5830 | * and it was successfully dirtied. But luckily error may happen |
| 5831 | * only due to journal error, so full transaction will be |
| 5832 | * aborted anyway. |
| 5833 | */ |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5834 | if (unlikely(*erp)) |
| 5835 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5836 | lblk1 += len; |
| 5837 | lblk2 += len; |
| 5838 | replaced_count += len; |
| 5839 | count -= len; |
| 5840 | |
| 5841 | repeat: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 5842 | ext4_ext_drop_refs(path1); |
| 5843 | kfree(path1); |
| 5844 | ext4_ext_drop_refs(path2); |
| 5845 | kfree(path2); |
| 5846 | path1 = path2 = NULL; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5847 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5848 | return replaced_count; |
| 5849 | } |
Eric Whitney | 0b02f4c | 2018-10-01 14:19:37 -0400 | [diff] [blame] | 5850 | |
| 5851 | /* |
| 5852 | * ext4_clu_mapped - determine whether any block in a logical cluster has |
| 5853 | * been mapped to a physical cluster |
| 5854 | * |
| 5855 | * @inode - file containing the logical cluster |
| 5856 | * @lclu - logical cluster of interest |
| 5857 | * |
| 5858 | * Returns 1 if any block in the logical cluster is mapped, signifying |
| 5859 | * that a physical cluster has been allocated for it. Otherwise, |
| 5860 | * returns 0. Can also return negative error codes. Derived from |
| 5861 | * ext4_ext_map_blocks(). |
| 5862 | */ |
| 5863 | int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu) |
| 5864 | { |
| 5865 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 5866 | struct ext4_ext_path *path; |
| 5867 | int depth, mapped = 0, err = 0; |
| 5868 | struct ext4_extent *extent; |
| 5869 | ext4_lblk_t first_lblk, first_lclu, last_lclu; |
| 5870 | |
| 5871 | /* search for the extent closest to the first block in the cluster */ |
| 5872 | path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0); |
| 5873 | if (IS_ERR(path)) { |
| 5874 | err = PTR_ERR(path); |
| 5875 | path = NULL; |
| 5876 | goto out; |
| 5877 | } |
| 5878 | |
| 5879 | depth = ext_depth(inode); |
| 5880 | |
| 5881 | /* |
| 5882 | * A consistent leaf must not be empty. This situation is possible, |
| 5883 | * though, _during_ tree modification, and it's why an assert can't |
| 5884 | * be put in ext4_find_extent(). |
| 5885 | */ |
| 5886 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 5887 | EXT4_ERROR_INODE(inode, |
| 5888 | "bad extent address - lblock: %lu, depth: %d, pblock: %lld", |
| 5889 | (unsigned long) EXT4_C2B(sbi, lclu), |
| 5890 | depth, path[depth].p_block); |
| 5891 | err = -EFSCORRUPTED; |
| 5892 | goto out; |
| 5893 | } |
| 5894 | |
| 5895 | extent = path[depth].p_ext; |
| 5896 | |
| 5897 | /* can't be mapped if the extent tree is empty */ |
| 5898 | if (extent == NULL) |
| 5899 | goto out; |
| 5900 | |
| 5901 | first_lblk = le32_to_cpu(extent->ee_block); |
| 5902 | first_lclu = EXT4_B2C(sbi, first_lblk); |
| 5903 | |
| 5904 | /* |
| 5905 | * Three possible outcomes at this point - found extent spanning |
| 5906 | * the target cluster, to the left of the target cluster, or to the |
| 5907 | * right of the target cluster. The first two cases are handled here. |
| 5908 | * The last case indicates the target cluster is not mapped. |
| 5909 | */ |
| 5910 | if (lclu >= first_lclu) { |
| 5911 | last_lclu = EXT4_B2C(sbi, first_lblk + |
| 5912 | ext4_ext_get_actual_len(extent) - 1); |
| 5913 | if (lclu <= last_lclu) { |
| 5914 | mapped = 1; |
| 5915 | } else { |
| 5916 | first_lblk = ext4_ext_next_allocated_block(path); |
| 5917 | first_lclu = EXT4_B2C(sbi, first_lblk); |
| 5918 | if (lclu == first_lclu) |
| 5919 | mapped = 1; |
| 5920 | } |
| 5921 | } |
| 5922 | |
| 5923 | out: |
| 5924 | ext4_ext_drop_refs(path); |
| 5925 | kfree(path); |
| 5926 | |
| 5927 | return err ? err : mapped; |
| 5928 | } |