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> |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 31 | #include <linux/iomap.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 32 | #include "ext4_jbd2.h" |
Theodore Ts'o | 4a092d7 | 2012-11-28 13:03:30 -0500 | [diff] [blame] | 33 | #include "ext4_extents.h" |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 34 | #include "xattr.h" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 35 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 36 | #include <trace/events/ext4.h> |
| 37 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 38 | /* |
| 39 | * used by extent splitting. |
| 40 | */ |
| 41 | #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ |
| 42 | due to ENOSPC */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 43 | #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */ |
| 44 | #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 45 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 46 | #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */ |
| 47 | #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */ |
| 48 | |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 49 | static __le32 ext4_extent_block_csum(struct inode *inode, |
| 50 | struct ext4_extent_header *eh) |
| 51 | { |
| 52 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 53 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 54 | __u32 csum; |
| 55 | |
| 56 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh, |
| 57 | EXT4_EXTENT_TAIL_OFFSET(eh)); |
| 58 | return cpu_to_le32(csum); |
| 59 | } |
| 60 | |
| 61 | static int ext4_extent_block_csum_verify(struct inode *inode, |
| 62 | struct ext4_extent_header *eh) |
| 63 | { |
| 64 | struct ext4_extent_tail *et; |
| 65 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 66 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 67 | return 1; |
| 68 | |
| 69 | et = find_ext4_extent_tail(eh); |
| 70 | if (et->et_checksum != ext4_extent_block_csum(inode, eh)) |
| 71 | return 0; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static void ext4_extent_block_csum_set(struct inode *inode, |
| 76 | struct ext4_extent_header *eh) |
| 77 | { |
| 78 | struct ext4_extent_tail *et; |
| 79 | |
Dmitry Monakhov | 9aa5d32b | 2014-10-13 03:36:16 -0400 | [diff] [blame] | 80 | if (!ext4_has_metadata_csum(inode->i_sb)) |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 81 | return; |
| 82 | |
| 83 | et = find_ext4_extent_tail(eh); |
| 84 | et->et_checksum = ext4_extent_block_csum(inode, eh); |
| 85 | } |
| 86 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 87 | static int ext4_split_extent_at(handle_t *handle, |
| 88 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 89 | struct ext4_ext_path **ppath, |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 90 | ext4_lblk_t split, |
| 91 | int split_flag, |
| 92 | int flags); |
| 93 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 94 | static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 95 | { |
Theodore Ts'o | 7b80819 | 2016-04-25 23:13:17 -0400 | [diff] [blame] | 96 | /* |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 97 | * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this |
| 98 | * moment, get_block can be called only for blocks inside i_size since |
| 99 | * page cache has been already dropped and writes are blocked by |
| 100 | * 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] | 101 | */ |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 102 | BUG_ON(EXT4_JOURNAL(inode) == NULL); |
| 103 | ext4_discard_preallocations(inode); |
| 104 | up_write(&EXT4_I(inode)->i_data_sem); |
| 105 | *dropped = 1; |
| 106 | return 0; |
| 107 | } |
Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 108 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 109 | /* |
| 110 | * Make sure 'handle' has at least 'check_cred' credits. If not, restart |
| 111 | * transaction with 'restart_cred' credits. The function drops i_data_sem |
| 112 | * when restarting transaction and gets it after transaction is restarted. |
| 113 | * |
| 114 | * The function returns 0 on success, 1 if transaction had to be restarted, |
| 115 | * and < 0 in case of fatal error. |
| 116 | */ |
| 117 | int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 118 | int check_cred, int restart_cred, |
| 119 | int revoke_cred) |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 120 | { |
| 121 | int ret; |
| 122 | int dropped = 0; |
| 123 | |
| 124 | ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 125 | revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped)); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 126 | if (dropped) |
| 127 | down_write(&EXT4_I(inode)->i_data_sem); |
| 128 | return ret; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * could return: |
| 133 | * - EROFS |
| 134 | * - ENOMEM |
| 135 | */ |
| 136 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, |
| 137 | struct ext4_ext_path *path) |
| 138 | { |
| 139 | if (path->p_bh) { |
| 140 | /* path points to block */ |
liang xie | 5d60125 | 2014-05-12 22:06:43 -0400 | [diff] [blame] | 141 | BUFFER_TRACE(path->p_bh, "get_write_access"); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 142 | return ext4_journal_get_write_access(handle, path->p_bh); |
| 143 | } |
| 144 | /* path points to leaf/index in inode body */ |
| 145 | /* we use in-core data, no need to protect them */ |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * could return: |
| 151 | * - EROFS |
| 152 | * - ENOMEM |
| 153 | * - EIO |
| 154 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 155 | static int __ext4_ext_dirty(const char *where, unsigned int line, |
| 156 | handle_t *handle, struct inode *inode, |
| 157 | struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 158 | { |
| 159 | int err; |
Dmitry Monakhov | 4b1f166 | 2014-07-27 22:28:15 -0400 | [diff] [blame] | 160 | |
| 161 | WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 162 | if (path->p_bh) { |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 163 | ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 164 | /* path points to block */ |
Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 165 | err = __ext4_handle_dirty_metadata(where, line, handle, |
| 166 | inode, path->p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 167 | } else { |
| 168 | /* path points to leaf/index in inode body */ |
| 169 | err = ext4_mark_inode_dirty(handle, inode); |
| 170 | } |
| 171 | return err; |
| 172 | } |
| 173 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 174 | #define ext4_ext_dirty(handle, inode, path) \ |
| 175 | __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) |
| 176 | |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 177 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 178 | struct ext4_ext_path *path, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 179 | ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 180 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 181 | if (path) { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 182 | int depth = path->p_depth; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 183 | struct ext4_extent *ex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 184 | |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 185 | /* |
| 186 | * Try to predict block placement assuming that we are |
| 187 | * filling in a file which will eventually be |
| 188 | * non-sparse --- i.e., in the case of libbfd writing |
| 189 | * an ELF object sections out-of-order but in a way |
| 190 | * the eventually results in a contiguous object or |
| 191 | * executable file, or some database extending a table |
| 192 | * space file. However, this is actually somewhat |
| 193 | * non-ideal if we are writing a sparse file such as |
| 194 | * qemu or KVM writing a raw image file that is going |
| 195 | * to stay fairly sparse, since it will end up |
| 196 | * fragmenting the file system's free space. Maybe we |
| 197 | * should have some hueristics or some way to allow |
| 198 | * userspace to pass a hint to file system, |
Tao Ma | b8d6568 | 2011-01-21 23:21:31 +0800 | [diff] [blame] | 199 | * especially if the latter case turns out to be |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 200 | * common. |
| 201 | */ |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 202 | ex = path[depth].p_ext; |
Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 203 | if (ex) { |
| 204 | ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); |
| 205 | ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); |
| 206 | |
| 207 | if (block > ext_block) |
| 208 | return ext_pblk + (block - ext_block); |
| 209 | else |
| 210 | return ext_pblk - (ext_block - block); |
| 211 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 212 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 213 | /* it looks like index is empty; |
| 214 | * try to find starting block from index itself */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 215 | if (path[depth].p_bh) |
| 216 | return path[depth].p_bh->b_blocknr; |
| 217 | } |
| 218 | |
| 219 | /* OK. use inode's group */ |
Eric Sandeen | f86186b | 2011-06-28 10:01:31 -0400 | [diff] [blame] | 220 | return ext4_inode_to_goal_block(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 223 | /* |
| 224 | * Allocation for a meta data block |
| 225 | */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 226 | static ext4_fsblk_t |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 227 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 228 | struct ext4_ext_path *path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 229 | struct ext4_extent *ex, int *err, unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 230 | { |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 231 | ext4_fsblk_t goal, newblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 232 | |
| 233 | 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] | 234 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 235 | NULL, err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 236 | return newblock; |
| 237 | } |
| 238 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 239 | static inline int ext4_ext_space_block(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 240 | { |
| 241 | int size; |
| 242 | |
| 243 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 244 | / sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 245 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 246 | if (!check && size > 6) |
| 247 | size = 6; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 248 | #endif |
| 249 | return size; |
| 250 | } |
| 251 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 252 | 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] | 253 | { |
| 254 | int size; |
| 255 | |
| 256 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) |
| 257 | / sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 258 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 259 | if (!check && size > 5) |
| 260 | size = 5; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 261 | #endif |
| 262 | return size; |
| 263 | } |
| 264 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 265 | static inline int ext4_ext_space_root(struct inode *inode, int check) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 266 | { |
| 267 | int size; |
| 268 | |
| 269 | size = sizeof(EXT4_I(inode)->i_data); |
| 270 | size -= sizeof(struct ext4_extent_header); |
| 271 | size /= sizeof(struct ext4_extent); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 272 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 273 | if (!check && size > 3) |
| 274 | size = 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 275 | #endif |
| 276 | return size; |
| 277 | } |
| 278 | |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 279 | 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] | 280 | { |
| 281 | int size; |
| 282 | |
| 283 | size = sizeof(EXT4_I(inode)->i_data); |
| 284 | size -= sizeof(struct ext4_extent_header); |
| 285 | size /= sizeof(struct ext4_extent_idx); |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 286 | #ifdef AGGRESSIVE_TEST |
Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 287 | if (!check && size > 4) |
| 288 | size = 4; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 289 | #endif |
| 290 | return size; |
| 291 | } |
| 292 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 293 | static inline int |
| 294 | ext4_force_split_extent_at(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 295 | struct ext4_ext_path **ppath, ext4_lblk_t lblk, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 296 | int nofail) |
| 297 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 298 | struct ext4_ext_path *path = *ppath; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 299 | int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext); |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 300 | int flags = EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO; |
| 301 | |
| 302 | if (nofail) |
| 303 | flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 304 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 305 | return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ? |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 306 | EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 307 | flags); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 310 | static int |
| 311 | ext4_ext_max_entries(struct inode *inode, int depth) |
| 312 | { |
| 313 | int max; |
| 314 | |
| 315 | if (depth == ext_depth(inode)) { |
| 316 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 317 | max = ext4_ext_space_root(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 318 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 319 | max = ext4_ext_space_root_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 320 | } else { |
| 321 | if (depth == 0) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 322 | max = ext4_ext_space_block(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 323 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 324 | max = ext4_ext_space_block_idx(inode, 1); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | return max; |
| 328 | } |
| 329 | |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 330 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
| 331 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 332 | ext4_fsblk_t block = ext4_ext_pblock(ext); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 333 | int len = ext4_ext_get_actual_len(ext); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 334 | ext4_lblk_t lblock = le32_to_cpu(ext->ee_block); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 335 | |
Vegard Nossum | f70749c | 2016-06-30 11:53:46 -0400 | [diff] [blame] | 336 | /* |
| 337 | * We allow neither: |
| 338 | * - zero length |
| 339 | * - overflow/wrap-around |
| 340 | */ |
| 341 | if (lblock + len <= lblock) |
Theodore Ts'o | 31d4f3a | 2012-03-11 23:30:16 -0400 | [diff] [blame] | 342 | return 0; |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 343 | 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] | 344 | } |
| 345 | |
| 346 | static int ext4_valid_extent_idx(struct inode *inode, |
| 347 | struct ext4_extent_idx *ext_idx) |
| 348 | { |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 349 | ext4_fsblk_t block = ext4_idx_pblock(ext_idx); |
Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 350 | |
Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 351 | 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] | 352 | } |
| 353 | |
| 354 | static int ext4_valid_extent_entries(struct inode *inode, |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 355 | struct ext4_extent_header *eh, |
| 356 | ext4_fsblk_t *pblk, int depth) |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 357 | { |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 358 | unsigned short entries; |
| 359 | if (eh->eh_entries == 0) |
| 360 | return 1; |
| 361 | |
| 362 | entries = le16_to_cpu(eh->eh_entries); |
| 363 | |
| 364 | if (depth == 0) { |
| 365 | /* leaf entries */ |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 366 | struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 367 | ext4_lblk_t lblock = 0; |
| 368 | ext4_lblk_t prev = 0; |
| 369 | int len = 0; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 370 | while (entries) { |
| 371 | if (!ext4_valid_extent(inode, ext)) |
| 372 | return 0; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 373 | |
| 374 | /* Check for overlapping extents */ |
| 375 | lblock = le32_to_cpu(ext->ee_block); |
| 376 | len = ext4_ext_get_actual_len(ext); |
| 377 | if ((lblock <= prev) && prev) { |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 378 | *pblk = ext4_ext_pblock(ext); |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 379 | return 0; |
| 380 | } |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 381 | ext++; |
| 382 | entries--; |
Eryu Guan | 5946d08 | 2013-12-03 21:22:21 -0500 | [diff] [blame] | 383 | prev = lblock + len - 1; |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 384 | } |
| 385 | } else { |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 386 | struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 387 | while (entries) { |
| 388 | if (!ext4_valid_extent_idx(inode, ext_idx)) |
| 389 | return 0; |
| 390 | ext_idx++; |
| 391 | entries--; |
| 392 | } |
| 393 | } |
| 394 | return 1; |
| 395 | } |
| 396 | |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 397 | static int __ext4_ext_check(const char *function, unsigned int line, |
| 398 | struct inode *inode, struct ext4_extent_header *eh, |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 399 | int depth, ext4_fsblk_t pblk) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 400 | { |
| 401 | const char *error_msg; |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 402 | int max = 0, err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 403 | |
| 404 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { |
| 405 | error_msg = "invalid magic"; |
| 406 | goto corrupted; |
| 407 | } |
| 408 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { |
| 409 | error_msg = "unexpected eh_depth"; |
| 410 | goto corrupted; |
| 411 | } |
| 412 | if (unlikely(eh->eh_max == 0)) { |
| 413 | error_msg = "invalid eh_max"; |
| 414 | goto corrupted; |
| 415 | } |
| 416 | max = ext4_ext_max_entries(inode, depth); |
| 417 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { |
| 418 | error_msg = "too large eh_max"; |
| 419 | goto corrupted; |
| 420 | } |
| 421 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { |
| 422 | error_msg = "invalid eh_entries"; |
| 423 | goto corrupted; |
| 424 | } |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 425 | if (!ext4_valid_extent_entries(inode, eh, &pblk, depth)) { |
Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 426 | error_msg = "invalid extent entries"; |
| 427 | goto corrupted; |
| 428 | } |
Vegard Nossum | 7bc9491 | 2016-07-15 00:22:07 -0400 | [diff] [blame] | 429 | if (unlikely(depth > 32)) { |
| 430 | error_msg = "too large eh_depth"; |
| 431 | goto corrupted; |
| 432 | } |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 433 | /* Verify checksum on non-root extent tree nodes */ |
| 434 | if (ext_depth(inode) != depth && |
| 435 | !ext4_extent_block_csum_verify(inode, eh)) { |
| 436 | error_msg = "extent tree corrupted"; |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 437 | err = -EFSBADCRC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 438 | goto corrupted; |
| 439 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 440 | return 0; |
| 441 | |
| 442 | corrupted: |
Theodore Ts'o | 54d3adb | 2020-03-28 19:33:43 -0400 | [diff] [blame] | 443 | ext4_error_inode_err(inode, function, line, 0, -err, |
| 444 | "pblk %llu bad header/extent: %s - magic %x, " |
| 445 | "entries %u, max %u(%u), depth %u(%u)", |
| 446 | (unsigned long long) pblk, error_msg, |
| 447 | le16_to_cpu(eh->eh_magic), |
| 448 | le16_to_cpu(eh->eh_entries), |
| 449 | le16_to_cpu(eh->eh_max), |
| 450 | max, le16_to_cpu(eh->eh_depth), depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 451 | return err; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 454 | #define ext4_ext_check(inode, eh, depth, pblk) \ |
| 455 | __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk)) |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 456 | |
Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 457 | int ext4_ext_check_inode(struct inode *inode) |
| 458 | { |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 459 | 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] | 460 | } |
| 461 | |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 462 | static void ext4_cache_extents(struct inode *inode, |
| 463 | struct ext4_extent_header *eh) |
| 464 | { |
| 465 | struct ext4_extent *ex = EXT_FIRST_EXTENT(eh); |
| 466 | ext4_lblk_t prev = 0; |
| 467 | int i; |
| 468 | |
| 469 | for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) { |
| 470 | unsigned int status = EXTENT_STATUS_WRITTEN; |
| 471 | ext4_lblk_t lblk = le32_to_cpu(ex->ee_block); |
| 472 | int len = ext4_ext_get_actual_len(ex); |
| 473 | |
| 474 | if (prev && (prev != lblk)) |
| 475 | ext4_es_cache_extent(inode, prev, lblk - prev, ~0, |
| 476 | EXTENT_STATUS_HOLE); |
| 477 | |
| 478 | if (ext4_ext_is_unwritten(ex)) |
| 479 | status = EXTENT_STATUS_UNWRITTEN; |
| 480 | ext4_es_cache_extent(inode, lblk, len, |
| 481 | ext4_ext_pblock(ex), status); |
| 482 | prev = lblk + len; |
| 483 | } |
| 484 | } |
| 485 | |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 486 | static struct buffer_head * |
| 487 | __read_extent_tree_block(const char *function, unsigned int line, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 488 | struct inode *inode, ext4_fsblk_t pblk, int depth, |
| 489 | int flags) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 490 | { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 491 | struct buffer_head *bh; |
| 492 | int err; |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 493 | gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS; |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 494 | |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 495 | if (flags & EXT4_EX_NOFAIL) |
| 496 | gfp_flags |= __GFP_NOFAIL; |
| 497 | |
| 498 | bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 499 | if (unlikely(!bh)) |
| 500 | return ERR_PTR(-ENOMEM); |
| 501 | |
| 502 | if (!bh_uptodate_or_lock(bh)) { |
| 503 | trace_ext4_ext_load_extent(inode, pblk, _RET_IP_); |
| 504 | err = bh_submit_read(bh); |
| 505 | if (err < 0) |
| 506 | goto errout; |
| 507 | } |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 508 | if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE)) |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 509 | return bh; |
Theodore Ts'o | 0a944e8 | 2019-05-22 10:27:01 -0400 | [diff] [blame] | 510 | if (!ext4_has_feature_journal(inode->i_sb) || |
| 511 | (inode->i_ino != |
| 512 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) { |
| 513 | err = __ext4_ext_check(function, line, inode, |
| 514 | ext_block_hdr(bh), depth, pblk); |
| 515 | if (err) |
| 516 | goto errout; |
| 517 | } |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 518 | set_buffer_verified(bh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 519 | /* |
| 520 | * If this is a leaf block, cache all of its entries |
| 521 | */ |
| 522 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) { |
| 523 | struct ext4_extent_header *eh = ext_block_hdr(bh); |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 524 | ext4_cache_extents(inode, eh); |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 525 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 526 | return bh; |
| 527 | errout: |
| 528 | put_bh(bh); |
| 529 | return ERR_PTR(err); |
| 530 | |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 533 | #define read_extent_tree_block(inode, pblk, depth, flags) \ |
| 534 | __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \ |
| 535 | (depth), (flags)) |
Darrick J. Wong | f848912 | 2012-04-29 18:21:10 -0400 | [diff] [blame] | 536 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 537 | /* |
| 538 | * This function is called to cache a file's extent information in the |
| 539 | * extent status tree |
| 540 | */ |
| 541 | int ext4_ext_precache(struct inode *inode) |
| 542 | { |
| 543 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 544 | struct ext4_ext_path *path = NULL; |
| 545 | struct buffer_head *bh; |
| 546 | int i = 0, depth, ret = 0; |
| 547 | |
| 548 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 549 | return 0; /* not an extent-mapped inode */ |
| 550 | |
| 551 | down_read(&ei->i_data_sem); |
| 552 | depth = ext_depth(inode); |
| 553 | |
Ritesh Harjani | 2f424a5 | 2020-02-28 14:56:55 +0530 | [diff] [blame] | 554 | /* Don't cache anything if there are no external extent blocks */ |
| 555 | if (!depth) { |
| 556 | up_read(&ei->i_data_sem); |
| 557 | return ret; |
| 558 | } |
| 559 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 560 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 561 | GFP_NOFS); |
| 562 | if (path == NULL) { |
| 563 | up_read(&ei->i_data_sem); |
| 564 | return -ENOMEM; |
| 565 | } |
| 566 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 567 | path[0].p_hdr = ext_inode_hdr(inode); |
| 568 | ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0); |
| 569 | if (ret) |
| 570 | goto out; |
| 571 | path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr); |
| 572 | while (i >= 0) { |
| 573 | /* |
| 574 | * If this is a leaf block or we've reached the end of |
| 575 | * the index block, go up |
| 576 | */ |
| 577 | if ((i == depth) || |
| 578 | path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) { |
| 579 | brelse(path[i].p_bh); |
| 580 | path[i].p_bh = NULL; |
| 581 | i--; |
| 582 | continue; |
| 583 | } |
| 584 | bh = read_extent_tree_block(inode, |
| 585 | ext4_idx_pblock(path[i].p_idx++), |
| 586 | depth - i - 1, |
| 587 | EXT4_EX_FORCE_CACHE); |
| 588 | if (IS_ERR(bh)) { |
| 589 | ret = PTR_ERR(bh); |
| 590 | break; |
| 591 | } |
| 592 | i++; |
| 593 | path[i].p_bh = bh; |
| 594 | path[i].p_hdr = ext_block_hdr(bh); |
| 595 | path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr); |
| 596 | } |
| 597 | ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED); |
| 598 | out: |
| 599 | up_read(&ei->i_data_sem); |
| 600 | ext4_ext_drop_refs(path); |
| 601 | kfree(path); |
| 602 | return ret; |
| 603 | } |
| 604 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 605 | #ifdef EXT_DEBUG |
| 606 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
| 607 | { |
| 608 | int k, l = path->p_depth; |
| 609 | |
| 610 | ext_debug("path:"); |
| 611 | for (k = 0; k <= l; k++, path++) { |
| 612 | if (path->p_idx) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 613 | ext_debug(" %d->%llu", |
| 614 | le32_to_cpu(path->p_idx->ei_block), |
| 615 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 616 | } else if (path->p_ext) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 617 | ext_debug(" %d:[%d]%d:%llu ", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 618 | le32_to_cpu(path->p_ext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 619 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 620 | ext4_ext_get_actual_len(path->p_ext), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 621 | ext4_ext_pblock(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 622 | } else |
| 623 | ext_debug(" []"); |
| 624 | } |
| 625 | ext_debug("\n"); |
| 626 | } |
| 627 | |
| 628 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) |
| 629 | { |
| 630 | int depth = ext_depth(inode); |
| 631 | struct ext4_extent_header *eh; |
| 632 | struct ext4_extent *ex; |
| 633 | int i; |
| 634 | |
| 635 | if (!path) |
| 636 | return; |
| 637 | |
| 638 | eh = path[depth].p_hdr; |
| 639 | ex = EXT_FIRST_EXTENT(eh); |
| 640 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 641 | ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); |
| 642 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 643 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 644 | ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 645 | ext4_ext_is_unwritten(ex), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 646 | ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 647 | } |
| 648 | ext_debug("\n"); |
| 649 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 650 | |
| 651 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, |
| 652 | ext4_fsblk_t newblock, int level) |
| 653 | { |
| 654 | int depth = ext_depth(inode); |
| 655 | struct ext4_extent *ex; |
| 656 | |
| 657 | if (depth != level) { |
| 658 | struct ext4_extent_idx *idx; |
| 659 | idx = path[level].p_idx; |
| 660 | while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { |
| 661 | ext_debug("%d: move %d:%llu in new index %llu\n", level, |
| 662 | le32_to_cpu(idx->ei_block), |
| 663 | ext4_idx_pblock(idx), |
| 664 | newblock); |
| 665 | idx++; |
| 666 | } |
| 667 | |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | ex = path[depth].p_ext; |
| 672 | while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 673 | ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", |
| 674 | le32_to_cpu(ex->ee_block), |
| 675 | ext4_ext_pblock(ex), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 676 | ext4_ext_is_unwritten(ex), |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 677 | ext4_ext_get_actual_len(ex), |
| 678 | newblock); |
| 679 | ex++; |
| 680 | } |
| 681 | } |
| 682 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 683 | #else |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 684 | #define ext4_ext_show_path(inode, path) |
| 685 | #define ext4_ext_show_leaf(inode, path) |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 686 | #define ext4_ext_show_move(inode, path, newblock, level) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 687 | #endif |
| 688 | |
Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 689 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 690 | { |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 691 | int depth, i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 692 | |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 693 | if (!path) |
| 694 | return; |
| 695 | depth = path->p_depth; |
Eric Biggers | de74548 | 2019-12-31 12:04:44 -0600 | [diff] [blame] | 696 | for (i = 0; i <= depth; i++, path++) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 697 | if (path->p_bh) { |
| 698 | brelse(path->p_bh); |
| 699 | path->p_bh = NULL; |
| 700 | } |
Eric Biggers | de74548 | 2019-12-31 12:04:44 -0600 | [diff] [blame] | 701 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 705 | * ext4_ext_binsearch_idx: |
| 706 | * binary search for the closest index of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 707 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 708 | */ |
| 709 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 710 | ext4_ext_binsearch_idx(struct inode *inode, |
| 711 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 712 | { |
| 713 | struct ext4_extent_header *eh = path->p_hdr; |
| 714 | struct ext4_extent_idx *r, *l, *m; |
| 715 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 716 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 717 | ext_debug("binsearch for %u(idx): ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 718 | |
| 719 | l = EXT_FIRST_INDEX(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 720 | r = EXT_LAST_INDEX(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 721 | while (l <= r) { |
| 722 | m = l + (r - l) / 2; |
| 723 | if (block < le32_to_cpu(m->ei_block)) |
| 724 | r = m - 1; |
| 725 | else |
| 726 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 727 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
| 728 | m, le32_to_cpu(m->ei_block), |
| 729 | r, le32_to_cpu(r->ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | path->p_idx = l - 1; |
Zheng Liu | 4a3c3a5 | 2012-05-28 17:55:16 -0400 | [diff] [blame] | 733 | 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] | 734 | ext4_idx_pblock(path->p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 735 | |
| 736 | #ifdef CHECK_BINSEARCH |
| 737 | { |
| 738 | struct ext4_extent_idx *chix, *ix; |
| 739 | int k; |
| 740 | |
| 741 | chix = ix = EXT_FIRST_INDEX(eh); |
| 742 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 743 | if (k != 0 && le32_to_cpu(ix->ei_block) <= |
| 744 | le32_to_cpu(ix[-1].ei_block)) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 745 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
| 746 | "first=0x%p\n", k, |
| 747 | ix, EXT_FIRST_INDEX(eh)); |
| 748 | printk(KERN_DEBUG "%u <= %u\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 749 | le32_to_cpu(ix->ei_block), |
| 750 | le32_to_cpu(ix[-1].ei_block)); |
| 751 | } |
| 752 | BUG_ON(k && le32_to_cpu(ix->ei_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 753 | <= le32_to_cpu(ix[-1].ei_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 754 | if (block < le32_to_cpu(ix->ei_block)) |
| 755 | break; |
| 756 | chix = ix; |
| 757 | } |
| 758 | BUG_ON(chix != path->p_idx); |
| 759 | } |
| 760 | #endif |
| 761 | |
| 762 | } |
| 763 | |
| 764 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 765 | * ext4_ext_binsearch: |
| 766 | * binary search for closest extent of the given block |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 767 | * the header must be checked before calling this |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 768 | */ |
| 769 | static void |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 770 | ext4_ext_binsearch(struct inode *inode, |
| 771 | struct ext4_ext_path *path, ext4_lblk_t block) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 772 | { |
| 773 | struct ext4_extent_header *eh = path->p_hdr; |
| 774 | struct ext4_extent *r, *l, *m; |
| 775 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 776 | if (eh->eh_entries == 0) { |
| 777 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 778 | * this leaf is empty: |
| 779 | * we get such a leaf in split/add case |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 780 | */ |
| 781 | return; |
| 782 | } |
| 783 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 784 | ext_debug("binsearch for %u: ", block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 785 | |
| 786 | l = EXT_FIRST_EXTENT(eh) + 1; |
Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 787 | r = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 788 | |
| 789 | while (l <= r) { |
| 790 | m = l + (r - l) / 2; |
| 791 | if (block < le32_to_cpu(m->ee_block)) |
| 792 | r = m - 1; |
| 793 | else |
| 794 | l = m + 1; |
Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 795 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
| 796 | m, le32_to_cpu(m->ee_block), |
| 797 | r, le32_to_cpu(r->ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | path->p_ext = l - 1; |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 801 | ext_debug(" -> %d:%llu:[%d]%d ", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 802 | le32_to_cpu(path->p_ext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 803 | ext4_ext_pblock(path->p_ext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 804 | ext4_ext_is_unwritten(path->p_ext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 805 | ext4_ext_get_actual_len(path->p_ext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 806 | |
| 807 | #ifdef CHECK_BINSEARCH |
| 808 | { |
| 809 | struct ext4_extent *chex, *ex; |
| 810 | int k; |
| 811 | |
| 812 | chex = ex = EXT_FIRST_EXTENT(eh); |
| 813 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { |
| 814 | BUG_ON(k && le32_to_cpu(ex->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 815 | <= le32_to_cpu(ex[-1].ee_block)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 816 | if (block < le32_to_cpu(ex->ee_block)) |
| 817 | break; |
| 818 | chex = ex; |
| 819 | } |
| 820 | BUG_ON(chex != path->p_ext); |
| 821 | } |
| 822 | #endif |
| 823 | |
| 824 | } |
| 825 | |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 826 | void ext4_ext_tree_init(handle_t *handle, struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 827 | { |
| 828 | struct ext4_extent_header *eh; |
| 829 | |
| 830 | eh = ext_inode_hdr(inode); |
| 831 | eh->eh_depth = 0; |
| 832 | eh->eh_entries = 0; |
| 833 | eh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 834 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 835 | ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | struct ext4_ext_path * |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 839 | ext4_find_extent(struct inode *inode, ext4_lblk_t block, |
| 840 | struct ext4_ext_path **orig_path, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 841 | { |
| 842 | struct ext4_extent_header *eh; |
| 843 | struct buffer_head *bh; |
Theodore Ts'o | 705912c | 2014-09-01 14:34:09 -0400 | [diff] [blame] | 844 | struct ext4_ext_path *path = orig_path ? *orig_path : NULL; |
| 845 | short int depth, i, ppos = 0; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 846 | int ret; |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 847 | gfp_t gfp_flags = GFP_NOFS; |
| 848 | |
| 849 | if (flags & EXT4_EX_NOFAIL) |
| 850 | gfp_flags |= __GFP_NOFAIL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 851 | |
| 852 | eh = ext_inode_hdr(inode); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 853 | depth = ext_depth(inode); |
Theodore Ts'o | bc890a6 | 2018-06-14 12:55:10 -0400 | [diff] [blame] | 854 | if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) { |
| 855 | EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d", |
| 856 | depth); |
| 857 | ret = -EFSCORRUPTED; |
| 858 | goto err; |
| 859 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 860 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 861 | if (path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 862 | ext4_ext_drop_refs(path); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 863 | if (depth > path[0].p_maxdepth) { |
| 864 | kfree(path); |
| 865 | *orig_path = path = NULL; |
| 866 | } |
| 867 | } |
| 868 | if (!path) { |
Theodore Ts'o | 523f431 | 2014-09-01 14:38:09 -0400 | [diff] [blame] | 869 | /* account possible depth increase */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 870 | path = kcalloc(depth + 2, sizeof(struct ext4_ext_path), |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 871 | gfp_flags); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 872 | if (unlikely(!path)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 873 | return ERR_PTR(-ENOMEM); |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 874 | path[0].p_maxdepth = depth + 1; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 875 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 876 | path[0].p_hdr = eh; |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 877 | path[0].p_bh = NULL; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 878 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 879 | i = depth; |
Dmitry Monakhov | 4068664 | 2019-11-06 12:25:02 +0000 | [diff] [blame] | 880 | if (!(flags & EXT4_EX_NOCACHE) && depth == 0) |
| 881 | ext4_cache_extents(inode, eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 882 | /* walk through the tree */ |
| 883 | while (i) { |
| 884 | ext_debug("depth %d: num %d, max %d\n", |
| 885 | 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] | 886 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 887 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 888 | path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 889 | path[ppos].p_depth = i; |
| 890 | path[ppos].p_ext = NULL; |
| 891 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 892 | bh = read_extent_tree_block(inode, path[ppos].p_block, --i, |
| 893 | flags); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 894 | if (IS_ERR(bh)) { |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 895 | ret = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 896 | goto err; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 897 | } |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 898 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 899 | eh = ext_block_hdr(bh); |
| 900 | ppos++; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 901 | path[ppos].p_bh = bh; |
| 902 | path[ppos].p_hdr = eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | path[ppos].p_depth = i; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 906 | path[ppos].p_ext = NULL; |
| 907 | path[ppos].p_idx = NULL; |
| 908 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 909 | /* find extent */ |
| 910 | ext4_ext_binsearch(inode, path + ppos, block); |
Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 911 | /* if not an empty leaf */ |
| 912 | if (path[ppos].p_ext) |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 913 | path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 914 | |
| 915 | ext4_ext_show_path(inode, path); |
| 916 | |
| 917 | return path; |
| 918 | |
| 919 | err: |
| 920 | ext4_ext_drop_refs(path); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 921 | kfree(path); |
| 922 | if (orig_path) |
| 923 | *orig_path = NULL; |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 924 | return ERR_PTR(ret); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 928 | * ext4_ext_insert_index: |
| 929 | * insert new index [@logical;@ptr] into the block at @curp; |
| 930 | * check where to insert: before @curp or after @curp |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 931 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 932 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, |
| 933 | struct ext4_ext_path *curp, |
| 934 | int logical, ext4_fsblk_t ptr) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 935 | { |
| 936 | struct ext4_extent_idx *ix; |
| 937 | int len, err; |
| 938 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 939 | err = ext4_ext_get_access(handle, inode, curp); |
| 940 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 941 | return err; |
| 942 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 943 | if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { |
| 944 | EXT4_ERROR_INODE(inode, |
| 945 | "logical %d == ei_block %d!", |
| 946 | logical, le32_to_cpu(curp->p_idx->ei_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 947 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 948 | } |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 949 | |
| 950 | if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) |
| 951 | >= le16_to_cpu(curp->p_hdr->eh_max))) { |
| 952 | EXT4_ERROR_INODE(inode, |
| 953 | "eh_entries %d >= eh_max %d!", |
| 954 | le16_to_cpu(curp->p_hdr->eh_entries), |
| 955 | le16_to_cpu(curp->p_hdr->eh_max)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 956 | return -EFSCORRUPTED; |
Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 957 | } |
| 958 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 959 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { |
| 960 | /* insert after */ |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 961 | ext_debug("insert new index %d after: %llu\n", logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 962 | ix = curp->p_idx + 1; |
| 963 | } else { |
| 964 | /* insert before */ |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 965 | ext_debug("insert new index %d before: %llu\n", logical, ptr); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 966 | ix = curp->p_idx; |
| 967 | } |
| 968 | |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 969 | len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; |
| 970 | BUG_ON(len < 0); |
| 971 | if (len > 0) { |
| 972 | ext_debug("insert new index %d: " |
| 973 | "move %d indices from 0x%p to 0x%p\n", |
| 974 | logical, len, ix, ix + 1); |
| 975 | memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); |
| 976 | } |
| 977 | |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 978 | if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { |
| 979 | EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 980 | return -EFSCORRUPTED; |
Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 981 | } |
| 982 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 983 | ix->ei_block = cpu_to_le32(logical); |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 984 | ext4_idx_store_pblock(ix, ptr); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 985 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 986 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 987 | if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { |
| 988 | EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 989 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 990 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 991 | |
| 992 | err = ext4_ext_dirty(handle, inode, curp); |
| 993 | ext4_std_error(inode->i_sb, err); |
| 994 | |
| 995 | return err; |
| 996 | } |
| 997 | |
| 998 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 999 | * ext4_ext_split: |
| 1000 | * inserts new subtree into the path, using free index entry |
| 1001 | * at depth @at: |
| 1002 | * - allocates all needed blocks (new leaf and all intermediate index blocks) |
| 1003 | * - makes decision where to split |
| 1004 | * - moves remaining extents and index entries (right to the split point) |
| 1005 | * into the newly allocated blocks |
| 1006 | * - initializes subtree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1007 | */ |
| 1008 | static int ext4_ext_split(handle_t *handle, struct inode *inode, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1009 | unsigned int flags, |
| 1010 | struct ext4_ext_path *path, |
| 1011 | struct ext4_extent *newext, int at) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1012 | { |
| 1013 | struct buffer_head *bh = NULL; |
| 1014 | int depth = ext_depth(inode); |
| 1015 | struct ext4_extent_header *neh; |
| 1016 | struct ext4_extent_idx *fidx; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1017 | int i = at, k, m, a; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1018 | ext4_fsblk_t newblock, oldblock; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1019 | __le32 border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1020 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 1021 | gfp_t gfp_flags = GFP_NOFS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1022 | int err = 0; |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1023 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1024 | |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 1025 | if (flags & EXT4_EX_NOFAIL) |
| 1026 | gfp_flags |= __GFP_NOFAIL; |
| 1027 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1028 | /* make decision: where to split? */ |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1029 | /* FIXME: now decision is simplest: at current extent */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1030 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1031 | /* if current leaf will be split, then we should use |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1032 | * border from split point */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1033 | if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { |
| 1034 | EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1035 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1036 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1037 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { |
| 1038 | border = path[depth].p_ext[1].ee_block; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1039 | ext_debug("leaf will be split." |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1040 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1041 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1042 | } else { |
| 1043 | border = newext->ee_block; |
| 1044 | ext_debug("leaf will be added." |
| 1045 | " next leaf starts at %d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1046 | le32_to_cpu(border)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1050 | * If error occurs, then we break processing |
| 1051 | * and mark filesystem read-only. index won't |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1052 | * be inserted and tree will be in consistent |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1053 | * state. Next mount will repair buffers too. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1054 | */ |
| 1055 | |
| 1056 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1057 | * Get array to track all allocated blocks. |
| 1058 | * We need this to handle errors and free blocks |
| 1059 | * upon them. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1060 | */ |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 1061 | ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), gfp_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1062 | if (!ablocks) |
| 1063 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1064 | |
| 1065 | /* allocate all needed blocks */ |
| 1066 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); |
| 1067 | for (a = 0; a < depth - at; a++) { |
Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1068 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1069 | newext, &err, flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1070 | if (newblock == 0) |
| 1071 | goto cleanup; |
| 1072 | ablocks[a] = newblock; |
| 1073 | } |
| 1074 | |
| 1075 | /* initialize new leaf */ |
| 1076 | newblock = ablocks[--a]; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1077 | if (unlikely(newblock == 0)) { |
| 1078 | EXT4_ERROR_INODE(inode, "newblock == 0!"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1079 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1080 | goto cleanup; |
| 1081 | } |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1082 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1083 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1084 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1085 | goto cleanup; |
| 1086 | } |
| 1087 | lock_buffer(bh); |
| 1088 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1089 | err = ext4_journal_get_create_access(handle, bh); |
| 1090 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1091 | goto cleanup; |
| 1092 | |
| 1093 | neh = ext_block_hdr(bh); |
| 1094 | neh->eh_entries = 0; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1095 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1096 | neh->eh_magic = EXT4_EXT_MAGIC; |
| 1097 | neh->eh_depth = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1098 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1099 | /* move remainder of path[depth] to the new leaf */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1100 | if (unlikely(path[depth].p_hdr->eh_entries != |
| 1101 | path[depth].p_hdr->eh_max)) { |
| 1102 | EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", |
| 1103 | path[depth].p_hdr->eh_entries, |
| 1104 | path[depth].p_hdr->eh_max); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1105 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1106 | goto cleanup; |
| 1107 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1108 | /* start copy from next extent */ |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1109 | m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; |
| 1110 | ext4_ext_show_move(inode, path, newblock, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1111 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1112 | struct ext4_extent *ex; |
| 1113 | ex = EXT_FIRST_EXTENT(neh); |
| 1114 | memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1115 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1118 | /* zero out unused area in the extent block */ |
| 1119 | ext_size = sizeof(struct ext4_extent_header) + |
| 1120 | sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries); |
| 1121 | 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] | 1122 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1123 | set_buffer_uptodate(bh); |
| 1124 | unlock_buffer(bh); |
| 1125 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1126 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1127 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1128 | goto cleanup; |
| 1129 | brelse(bh); |
| 1130 | bh = NULL; |
| 1131 | |
| 1132 | /* correct old leaf */ |
| 1133 | if (m) { |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1134 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 1135 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1136 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1137 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1138 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 1139 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1140 | goto cleanup; |
| 1141 | |
| 1142 | } |
| 1143 | |
| 1144 | /* create intermediate indexes */ |
| 1145 | k = depth - at - 1; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1146 | if (unlikely(k < 0)) { |
| 1147 | EXT4_ERROR_INODE(inode, "k %d < 0!", k); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1148 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1149 | goto cleanup; |
| 1150 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1151 | if (k) |
| 1152 | ext_debug("create %d intermediate indices\n", k); |
| 1153 | /* insert new index into current index block */ |
| 1154 | /* current depth stored in i var */ |
| 1155 | i = depth - 1; |
| 1156 | while (k--) { |
| 1157 | oldblock = newblock; |
| 1158 | newblock = ablocks[--a]; |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1159 | bh = sb_getblk(inode->i_sb, newblock); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1160 | if (unlikely(!bh)) { |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1161 | err = -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1162 | goto cleanup; |
| 1163 | } |
| 1164 | lock_buffer(bh); |
| 1165 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1166 | err = ext4_journal_get_create_access(handle, bh); |
| 1167 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1168 | goto cleanup; |
| 1169 | |
| 1170 | neh = ext_block_hdr(bh); |
| 1171 | neh->eh_entries = cpu_to_le16(1); |
| 1172 | neh->eh_magic = EXT4_EXT_MAGIC; |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1173 | 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] | 1174 | neh->eh_depth = cpu_to_le16(depth - i); |
| 1175 | fidx = EXT_FIRST_INDEX(neh); |
| 1176 | fidx->ei_block = border; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1177 | ext4_idx_store_pblock(fidx, oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1178 | |
Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1179 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
| 1180 | i, newblock, le32_to_cpu(border), oldblock); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1181 | |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1182 | /* move remainder of path[i] to the new index block */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1183 | if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != |
| 1184 | EXT_LAST_INDEX(path[i].p_hdr))) { |
| 1185 | EXT4_ERROR_INODE(inode, |
| 1186 | "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", |
| 1187 | le32_to_cpu(path[i].p_ext->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1188 | err = -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1189 | goto cleanup; |
| 1190 | } |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1191 | /* start copy indexes */ |
| 1192 | m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; |
| 1193 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, |
| 1194 | EXT_MAX_INDEX(path[i].p_hdr)); |
| 1195 | ext4_ext_show_move(inode, path, newblock, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1196 | if (m) { |
Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 1197 | memmove(++fidx, path[i].p_idx, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1198 | sizeof(struct ext4_extent_idx) * m); |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1199 | le16_add_cpu(&neh->eh_entries, m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1200 | } |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1201 | /* zero out unused area in the extent block */ |
| 1202 | ext_size = sizeof(struct ext4_extent_header) + |
| 1203 | (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries)); |
| 1204 | memset(bh->b_data + ext_size, 0, |
| 1205 | inode->i_sb->s_blocksize - ext_size); |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1206 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1207 | set_buffer_uptodate(bh); |
| 1208 | unlock_buffer(bh); |
| 1209 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1210 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1211 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1212 | goto cleanup; |
| 1213 | brelse(bh); |
| 1214 | bh = NULL; |
| 1215 | |
| 1216 | /* correct old index */ |
| 1217 | if (m) { |
| 1218 | err = ext4_ext_get_access(handle, inode, path + i); |
| 1219 | if (err) |
| 1220 | goto cleanup; |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1221 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1222 | err = ext4_ext_dirty(handle, inode, path + i); |
| 1223 | if (err) |
| 1224 | goto cleanup; |
| 1225 | } |
| 1226 | |
| 1227 | i--; |
| 1228 | } |
| 1229 | |
| 1230 | /* insert new index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1231 | err = ext4_ext_insert_index(handle, inode, path + at, |
| 1232 | le32_to_cpu(border), newblock); |
| 1233 | |
| 1234 | cleanup: |
| 1235 | if (bh) { |
| 1236 | if (buffer_locked(bh)) |
| 1237 | unlock_buffer(bh); |
| 1238 | brelse(bh); |
| 1239 | } |
| 1240 | |
| 1241 | if (err) { |
| 1242 | /* free all allocated blocks in error case */ |
| 1243 | for (i = 0; i < depth; i++) { |
| 1244 | if (!ablocks[i]) |
| 1245 | continue; |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1246 | ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1247 | EXT4_FREE_BLOCKS_METADATA); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | kfree(ablocks); |
| 1251 | |
| 1252 | return err; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1256 | * ext4_ext_grow_indepth: |
| 1257 | * implements tree growing procedure: |
| 1258 | * - allocates new block |
| 1259 | * - moves top-level data (index block or leaf) into the new block |
| 1260 | * - initializes new top-level, creating index that points to the |
| 1261 | * just created block |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1262 | */ |
| 1263 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1264 | unsigned int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1265 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1266 | struct ext4_extent_header *neh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1267 | struct buffer_head *bh; |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1268 | ext4_fsblk_t newblock, goal = 0; |
| 1269 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1270 | int err = 0; |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1271 | size_t ext_size = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1272 | |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1273 | /* Try to prepend new index to old one */ |
| 1274 | if (ext_depth(inode)) |
| 1275 | goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode))); |
| 1276 | if (goal > le32_to_cpu(es->s_first_data_block)) { |
| 1277 | flags |= EXT4_MB_HINT_TRY_GOAL; |
| 1278 | goal--; |
| 1279 | } else |
| 1280 | goal = ext4_inode_to_goal_block(inode); |
| 1281 | newblock = ext4_new_meta_blocks(handle, inode, goal, flags, |
| 1282 | NULL, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1283 | if (newblock == 0) |
| 1284 | return err; |
| 1285 | |
Nikolay Borisov | c45653c | 2015-07-02 01:34:07 -0400 | [diff] [blame] | 1286 | bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); |
Wang Shilong | aebf024 | 2013-01-12 16:28:47 -0500 | [diff] [blame] | 1287 | if (unlikely(!bh)) |
Theodore Ts'o | 860d21e | 2013-01-12 16:19:36 -0500 | [diff] [blame] | 1288 | return -ENOMEM; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1289 | lock_buffer(bh); |
| 1290 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1291 | err = ext4_journal_get_create_access(handle, bh); |
| 1292 | if (err) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1293 | unlock_buffer(bh); |
| 1294 | goto out; |
| 1295 | } |
| 1296 | |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1297 | ext_size = sizeof(EXT4_I(inode)->i_data); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1298 | /* move top-level index/leaf into new block */ |
Sriram Rajagopalan | 592acbf | 2019-05-10 19:28:06 -0400 | [diff] [blame] | 1299 | memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size); |
| 1300 | /* zero out unused area in the extent block */ |
| 1301 | 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] | 1302 | |
| 1303 | /* set size of new block */ |
| 1304 | neh = ext_block_hdr(bh); |
| 1305 | /* old root could have indexes or leaves |
| 1306 | * so calculate e_max right way */ |
| 1307 | if (ext_depth(inode)) |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1308 | 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] | 1309 | else |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1310 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1311 | neh->eh_magic = EXT4_EXT_MAGIC; |
Darrick J. Wong | 7ac5990 | 2012-04-29 18:37:10 -0400 | [diff] [blame] | 1312 | ext4_extent_block_csum_set(inode, neh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1313 | set_buffer_uptodate(bh); |
| 1314 | unlock_buffer(bh); |
| 1315 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1316 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1317 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1318 | goto out; |
| 1319 | |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1320 | /* Update top-level index: num,max,pointer */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1321 | neh = ext_inode_hdr(inode); |
Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1322 | neh->eh_entries = cpu_to_le16(1); |
| 1323 | ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); |
| 1324 | if (neh->eh_depth == 0) { |
| 1325 | /* Root extent block becomes index block */ |
| 1326 | neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); |
| 1327 | EXT_FIRST_INDEX(neh)->ei_block = |
| 1328 | EXT_FIRST_EXTENT(neh)->ee_block; |
| 1329 | } |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1330 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1331 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 1332 | le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1333 | ext4_idx_pblock(EXT_FIRST_INDEX(neh))); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1334 | |
Wei Yongjun | ba39ebb | 2012-09-27 09:37:53 -0400 | [diff] [blame] | 1335 | le16_add_cpu(&neh->eh_depth, 1); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 1336 | err = ext4_mark_inode_dirty(handle, inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1337 | out: |
| 1338 | brelse(bh); |
| 1339 | |
| 1340 | return err; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1344 | * ext4_ext_create_new_leaf: |
| 1345 | * finds empty index and adds new leaf. |
| 1346 | * if no free index is found, then it requests in-depth growing. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1347 | */ |
| 1348 | 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] | 1349 | unsigned int mb_flags, |
| 1350 | unsigned int gb_flags, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1351 | struct ext4_ext_path **ppath, |
Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1352 | struct ext4_extent *newext) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1353 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1354 | struct ext4_ext_path *path = *ppath; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1355 | struct ext4_ext_path *curp; |
| 1356 | int depth, i, err = 0; |
| 1357 | |
| 1358 | repeat: |
| 1359 | i = depth = ext_depth(inode); |
| 1360 | |
| 1361 | /* walk up to the tree and look for free index entry */ |
| 1362 | curp = path + depth; |
| 1363 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { |
| 1364 | i--; |
| 1365 | curp--; |
| 1366 | } |
| 1367 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1368 | /* we use already allocated block for index block, |
| 1369 | * so subsequent data blocks should be contiguous */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1370 | if (EXT_HAS_FREE_INDEX(curp)) { |
| 1371 | /* if we found index with free entry, then use that |
| 1372 | * entry: create all needed subtree and add new leaf */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1373 | err = ext4_ext_split(handle, inode, mb_flags, path, newext, i); |
Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1374 | if (err) |
| 1375 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 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 | } else { |
| 1384 | /* tree is full, time to grow in depth */ |
Dmitry Monakhov | be5cd90 | 2014-10-01 22:57:09 -0400 | [diff] [blame] | 1385 | err = ext4_ext_grow_indepth(handle, inode, mb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1386 | if (err) |
| 1387 | goto out; |
| 1388 | |
| 1389 | /* refill path */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 1390 | path = ext4_find_extent(inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1391 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1392 | ppath, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1393 | if (IS_ERR(path)) { |
| 1394 | err = PTR_ERR(path); |
| 1395 | goto out; |
| 1396 | } |
| 1397 | |
| 1398 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1399 | * only first (depth 0 -> 1) produces free space; |
| 1400 | * in all other cases we have to split the grown tree |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1401 | */ |
| 1402 | depth = ext_depth(inode); |
| 1403 | 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] | 1404 | /* now we need to split */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1405 | goto repeat; |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | out: |
| 1410 | return err; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1414 | * search the closest allocated block to the left for *logical |
| 1415 | * and returns it at @logical + it's physical address at @phys |
| 1416 | * if *logical is the smallest allocated block, the function |
| 1417 | * returns 0 at @phys |
| 1418 | * return value contains 0 (success) or error code |
| 1419 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1420 | static int ext4_ext_search_left(struct inode *inode, |
| 1421 | struct ext4_ext_path *path, |
| 1422 | ext4_lblk_t *logical, ext4_fsblk_t *phys) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1423 | { |
| 1424 | struct ext4_extent_idx *ix; |
| 1425 | struct ext4_extent *ex; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1426 | int depth, ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1427 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1428 | if (unlikely(path == NULL)) { |
| 1429 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1430 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1431 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1432 | depth = path->p_depth; |
| 1433 | *phys = 0; |
| 1434 | |
| 1435 | if (depth == 0 && path->p_ext == NULL) |
| 1436 | return 0; |
| 1437 | |
| 1438 | /* usually extent in the path covers blocks smaller |
| 1439 | * then *logical, but it can be that extent is the |
| 1440 | * first one in the file */ |
| 1441 | |
| 1442 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1443 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1444 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1445 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1446 | EXT4_ERROR_INODE(inode, |
| 1447 | "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", |
| 1448 | *logical, le32_to_cpu(ex->ee_block)); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1449 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1450 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1451 | while (--depth >= 0) { |
| 1452 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1453 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1454 | EXT4_ERROR_INODE(inode, |
| 1455 | "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1456 | ix != NULL ? le32_to_cpu(ix->ei_block) : 0, |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1457 | EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? |
Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1458 | 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] | 1459 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1460 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1461 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1462 | } |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1466 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1467 | EXT4_ERROR_INODE(inode, |
| 1468 | "logical %d < ee_block %d + ee_len %d!", |
| 1469 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1470 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1471 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1472 | |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1473 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1474 | *phys = ext4_ext_pblock(ex) + ee_len - 1; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | /* |
| 1479 | * search the closest allocated block to the right for *logical |
| 1480 | * and returns it at @logical + it's physical address at @phys |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 1481 | * if *logical is the largest allocated block, the function |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1482 | * returns 0 at @phys |
| 1483 | * return value contains 0 (success) or error code |
| 1484 | */ |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1485 | static int ext4_ext_search_right(struct inode *inode, |
| 1486 | struct ext4_ext_path *path, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1487 | ext4_lblk_t *logical, ext4_fsblk_t *phys, |
| 1488 | struct ext4_extent **ret_ex) |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1489 | { |
| 1490 | struct buffer_head *bh = NULL; |
| 1491 | struct ext4_extent_header *eh; |
| 1492 | struct ext4_extent_idx *ix; |
| 1493 | struct ext4_extent *ex; |
| 1494 | ext4_fsblk_t block; |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1495 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
| 1496 | int ee_len; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1497 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1498 | if (unlikely(path == NULL)) { |
| 1499 | EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1500 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1501 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1502 | depth = path->p_depth; |
| 1503 | *phys = 0; |
| 1504 | |
| 1505 | if (depth == 0 && path->p_ext == NULL) |
| 1506 | return 0; |
| 1507 | |
| 1508 | /* usually extent in the path covers blocks smaller |
| 1509 | * then *logical, but it can be that extent is the |
| 1510 | * first one in the file */ |
| 1511 | |
| 1512 | ex = path[depth].p_ext; |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1513 | ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1514 | if (*logical < le32_to_cpu(ex->ee_block)) { |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1515 | if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { |
| 1516 | EXT4_ERROR_INODE(inode, |
| 1517 | "first_extent(path[%d].p_hdr) != ex", |
| 1518 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1519 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1520 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1521 | while (--depth >= 0) { |
| 1522 | ix = path[depth].p_idx; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1523 | if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { |
| 1524 | EXT4_ERROR_INODE(inode, |
| 1525 | "ix != EXT_FIRST_INDEX *logical %d!", |
| 1526 | *logical); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1527 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1528 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1529 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1530 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1531 | } |
| 1532 | |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1533 | if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { |
| 1534 | EXT4_ERROR_INODE(inode, |
| 1535 | "logical %d < ee_block %d + ee_len %d!", |
| 1536 | *logical, le32_to_cpu(ex->ee_block), ee_len); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1537 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1538 | } |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1539 | |
| 1540 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 1541 | /* next allocated block in this leaf */ |
| 1542 | ex++; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1543 | goto found_extent; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | /* go up and search for index to the right */ |
| 1547 | while (--depth >= 0) { |
| 1548 | ix = path[depth].p_idx; |
| 1549 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1550 | goto got_index; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1551 | } |
| 1552 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1553 | /* we've gone up to the root and found no index to the right */ |
| 1554 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1555 | |
Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1556 | got_index: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1557 | /* we've found index to the right, let's |
| 1558 | * follow it and find the closest allocated |
| 1559 | * block to the right */ |
| 1560 | ix++; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1561 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1562 | while (++depth < path->p_depth) { |
Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1563 | /* subtract from p_depth to get proper eh_depth */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1564 | bh = read_extent_tree_block(inode, block, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1565 | path->p_depth - depth, 0); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 1566 | if (IS_ERR(bh)) |
| 1567 | return PTR_ERR(bh); |
| 1568 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1569 | ix = EXT_FIRST_INDEX(eh); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1570 | block = ext4_idx_pblock(ix); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1571 | put_bh(bh); |
| 1572 | } |
| 1573 | |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1574 | 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] | 1575 | if (IS_ERR(bh)) |
| 1576 | return PTR_ERR(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1577 | eh = ext_block_hdr(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1578 | ex = EXT_FIRST_EXTENT(eh); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1579 | found_extent: |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1580 | *logical = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1581 | *phys = ext4_ext_pblock(ex); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1582 | *ret_ex = ex; |
| 1583 | if (bh) |
| 1584 | put_bh(bh); |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1585 | return 0; |
Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1589 | * ext4_ext_next_allocated_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1590 | * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1591 | * NOTE: it considers block number from index entry as |
| 1592 | * allocated block. Thus, index entries have to be consistent |
| 1593 | * with leaves. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1594 | */ |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 1595 | ext4_lblk_t |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1596 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
| 1597 | { |
| 1598 | int depth; |
| 1599 | |
| 1600 | BUG_ON(path == NULL); |
| 1601 | depth = path->p_depth; |
| 1602 | |
| 1603 | if (depth == 0 && path->p_ext == NULL) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1604 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1605 | |
| 1606 | while (depth >= 0) { |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1607 | struct ext4_ext_path *p = &path[depth]; |
| 1608 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1609 | if (depth == path->p_depth) { |
| 1610 | /* leaf */ |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1611 | if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr)) |
| 1612 | return le32_to_cpu(p->p_ext[1].ee_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1613 | } else { |
| 1614 | /* index */ |
Eric Biggers | 6e89bbb | 2019-12-31 12:04:43 -0600 | [diff] [blame] | 1615 | if (p->p_idx != EXT_LAST_INDEX(p->p_hdr)) |
| 1616 | return le32_to_cpu(p->p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1617 | } |
| 1618 | depth--; |
| 1619 | } |
| 1620 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1621 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1625 | * ext4_ext_next_leaf_block: |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1626 | * returns first allocated block from next leaf or EXT_MAX_BLOCKS |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1627 | */ |
Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1628 | 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] | 1629 | { |
| 1630 | int depth; |
| 1631 | |
| 1632 | BUG_ON(path == NULL); |
| 1633 | depth = path->p_depth; |
| 1634 | |
| 1635 | /* zero-tree has no leaf blocks at all */ |
| 1636 | if (depth == 0) |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1637 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1638 | |
| 1639 | /* go to index block */ |
| 1640 | depth--; |
| 1641 | |
| 1642 | while (depth >= 0) { |
| 1643 | if (path[depth].p_idx != |
| 1644 | EXT_LAST_INDEX(path[depth].p_hdr)) |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1645 | return (ext4_lblk_t) |
| 1646 | le32_to_cpu(path[depth].p_idx[1].ei_block); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1647 | depth--; |
| 1648 | } |
| 1649 | |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1650 | return EXT_MAX_BLOCKS; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1654 | * ext4_ext_correct_indexes: |
| 1655 | * if leaf gets modified and modified extent is first in the leaf, |
| 1656 | * then we have to correct all indexes above. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1657 | * TODO: do we need to correct tree in all cases? |
| 1658 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1659 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1660 | struct ext4_ext_path *path) |
| 1661 | { |
| 1662 | struct ext4_extent_header *eh; |
| 1663 | int depth = ext_depth(inode); |
| 1664 | struct ext4_extent *ex; |
| 1665 | __le32 border; |
| 1666 | int k, err = 0; |
| 1667 | |
| 1668 | eh = path[depth].p_hdr; |
| 1669 | ex = path[depth].p_ext; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1670 | |
| 1671 | if (unlikely(ex == NULL || eh == NULL)) { |
| 1672 | EXT4_ERROR_INODE(inode, |
| 1673 | "ex %p == NULL or eh %p == NULL", ex, eh); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1674 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1675 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1676 | |
| 1677 | if (depth == 0) { |
| 1678 | /* there is no tree at all */ |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | if (ex != EXT_FIRST_EXTENT(eh)) { |
| 1683 | /* we correct tree if first leaf got modified only */ |
| 1684 | return 0; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1688 | * TODO: we need correction if border is smaller than current one |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1689 | */ |
| 1690 | k = depth - 1; |
| 1691 | border = path[depth].p_ext->ee_block; |
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 | return err; |
| 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 | return err; |
| 1699 | |
| 1700 | while (k--) { |
| 1701 | /* change all left-side indexes */ |
| 1702 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) |
| 1703 | break; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1704 | err = ext4_ext_get_access(handle, inode, path + k); |
| 1705 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1706 | break; |
| 1707 | path[k].p_idx->ei_block = border; |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1708 | err = ext4_ext_dirty(handle, inode, path + k); |
| 1709 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1710 | break; |
| 1711 | } |
| 1712 | |
| 1713 | return err; |
| 1714 | } |
| 1715 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 1716 | static int ext4_can_extents_be_merged(struct inode *inode, |
| 1717 | struct ext4_extent *ex1, |
| 1718 | struct ext4_extent *ex2) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1719 | { |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1720 | unsigned short ext1_ee_len, ext2_ee_len; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1721 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1722 | if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2)) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1723 | return 0; |
| 1724 | |
| 1725 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
| 1726 | ext2_ee_len = ext4_ext_get_actual_len(ex2); |
| 1727 | |
| 1728 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1729 | le32_to_cpu(ex2->ee_block)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1730 | return 0; |
| 1731 | |
Eric Sandeen | da0169b | 2013-11-04 09:58:26 -0500 | [diff] [blame] | 1732 | if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN) |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1733 | return 0; |
Matthew Bobrowski | 378f32b | 2019-11-05 23:02:39 +1100 | [diff] [blame] | 1734 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1735 | if (ext4_ext_is_unwritten(ex1) && |
Matthew Bobrowski | 378f32b | 2019-11-05 23:02:39 +1100 | [diff] [blame] | 1736 | ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN) |
Darrick J. Wong | a9b8241 | 2014-02-20 21:17:35 -0500 | [diff] [blame] | 1737 | return 0; |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1738 | #ifdef AGGRESSIVE_TEST |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1739 | if (ext1_ee_len >= 4) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1740 | return 0; |
| 1741 | #endif |
| 1742 | |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1743 | if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1744 | return 1; |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
| 1748 | /* |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1749 | * This function tries to merge the "ex" extent to the next extent in the tree. |
| 1750 | * It always tries to merge towards right. If you want to merge towards |
| 1751 | * left, pass "ex - 1" as argument instead of "ex". |
| 1752 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns |
| 1753 | * 1 if they got merged. |
| 1754 | */ |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1755 | static int ext4_ext_try_to_merge_right(struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1756 | struct ext4_ext_path *path, |
| 1757 | struct ext4_extent *ex) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1758 | { |
| 1759 | struct ext4_extent_header *eh; |
| 1760 | unsigned int depth, len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1761 | int merge_done = 0, unwritten; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1762 | |
| 1763 | depth = ext_depth(inode); |
| 1764 | BUG_ON(path[depth].p_hdr == NULL); |
| 1765 | eh = path[depth].p_hdr; |
| 1766 | |
| 1767 | while (ex < EXT_LAST_EXTENT(eh)) { |
| 1768 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) |
| 1769 | break; |
| 1770 | /* merge with next extent! */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1771 | unwritten = ext4_ext_is_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1772 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 1773 | + ext4_ext_get_actual_len(ex + 1)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1774 | if (unwritten) |
| 1775 | ext4_ext_mark_unwritten(ex); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1776 | |
| 1777 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { |
| 1778 | len = (EXT_LAST_EXTENT(eh) - ex - 1) |
| 1779 | * sizeof(struct ext4_extent); |
| 1780 | memmove(ex + 1, ex + 2, len); |
| 1781 | } |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1782 | le16_add_cpu(&eh->eh_entries, -1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1783 | merge_done = 1; |
| 1784 | WARN_ON(eh->eh_entries == 0); |
| 1785 | if (!eh->eh_entries) |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1786 | EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | return merge_done; |
| 1790 | } |
| 1791 | |
| 1792 | /* |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1793 | * This function does a very simple check to see if we can collapse |
| 1794 | * an extent tree with a single extent tree leaf block into the inode. |
| 1795 | */ |
| 1796 | static void ext4_ext_try_to_merge_up(handle_t *handle, |
| 1797 | struct inode *inode, |
| 1798 | struct ext4_ext_path *path) |
| 1799 | { |
| 1800 | size_t s; |
| 1801 | unsigned max_root = ext4_ext_space_root(inode, 0); |
| 1802 | ext4_fsblk_t blk; |
| 1803 | |
| 1804 | if ((path[0].p_depth != 1) || |
| 1805 | (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) || |
| 1806 | (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root)) |
| 1807 | return; |
| 1808 | |
| 1809 | /* |
| 1810 | * We need to modify the block allocation bitmap and the block |
| 1811 | * group descriptor to release the extent tree block. If we |
| 1812 | * can't get the journal credits, give up. |
| 1813 | */ |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 1814 | if (ext4_journal_extend(handle, 2, |
| 1815 | ext4_free_metadata_revoke_credits(inode->i_sb, 1))) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1816 | return; |
| 1817 | |
| 1818 | /* |
| 1819 | * Copy the extent data up to the inode |
| 1820 | */ |
| 1821 | blk = ext4_idx_pblock(path[0].p_idx); |
| 1822 | s = le16_to_cpu(path[1].p_hdr->eh_entries) * |
| 1823 | sizeof(struct ext4_extent_idx); |
| 1824 | s += sizeof(struct ext4_extent_header); |
| 1825 | |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 1826 | path[1].p_maxdepth = path[0].p_maxdepth; |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1827 | memcpy(path[0].p_hdr, path[1].p_hdr, s); |
| 1828 | path[0].p_depth = 0; |
| 1829 | path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + |
| 1830 | (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr)); |
| 1831 | path[0].p_hdr->eh_max = cpu_to_le16(max_root); |
| 1832 | |
| 1833 | brelse(path[1].p_bh); |
| 1834 | ext4_free_blocks(handle, inode, NULL, blk, 1, |
Theodore Ts'o | 71d4f7d | 2014-07-15 06:02:38 -0400 | [diff] [blame] | 1835 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | /* |
Eric Biggers | adde81c | 2019-12-31 12:04:41 -0600 | [diff] [blame] | 1839 | * This function tries to merge the @ex extent to neighbours in the tree, then |
| 1840 | * tries to collapse the extent tree into the inode. |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1841 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1842 | static void ext4_ext_try_to_merge(handle_t *handle, |
| 1843 | struct inode *inode, |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1844 | struct ext4_ext_path *path, |
Eric Biggers | adde81c | 2019-12-31 12:04:41 -0600 | [diff] [blame] | 1845 | struct ext4_extent *ex) |
| 1846 | { |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1847 | struct ext4_extent_header *eh; |
| 1848 | unsigned int depth; |
| 1849 | int merge_done = 0; |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1850 | |
| 1851 | depth = ext_depth(inode); |
| 1852 | BUG_ON(path[depth].p_hdr == NULL); |
| 1853 | eh = path[depth].p_hdr; |
| 1854 | |
| 1855 | if (ex > EXT_FIRST_EXTENT(eh)) |
| 1856 | merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); |
| 1857 | |
| 1858 | if (!merge_done) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1859 | (void) ext4_ext_try_to_merge_right(inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1860 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 1861 | ext4_ext_try_to_merge_up(handle, inode, path); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | /* |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1865 | * check if a portion of the "newext" extent overlaps with an |
| 1866 | * existing extent. |
| 1867 | * |
| 1868 | * If there is an overlap discovered, it updates the length of the newext |
| 1869 | * such that there will be no overlap, and then returns 1. |
| 1870 | * If there is no overlap found, it returns 0. |
| 1871 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1872 | static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, |
| 1873 | struct inode *inode, |
Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1874 | struct ext4_extent *newext, |
| 1875 | struct ext4_ext_path *path) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1876 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1877 | ext4_lblk_t b1, b2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1878 | unsigned int depth, len1; |
| 1879 | unsigned int ret = 0; |
| 1880 | |
| 1881 | b1 = le32_to_cpu(newext->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1882 | len1 = ext4_ext_get_actual_len(newext); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1883 | depth = ext_depth(inode); |
| 1884 | if (!path[depth].p_ext) |
| 1885 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1886 | 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] | 1887 | |
| 1888 | /* |
| 1889 | * get the next allocated block if the extent in the path |
Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1890 | * is before the requested block(s) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1891 | */ |
| 1892 | if (b2 < b1) { |
| 1893 | b2 = ext4_ext_next_allocated_block(path); |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1894 | if (b2 == EXT_MAX_BLOCKS) |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1895 | goto out; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 1896 | b2 = EXT4_LBLK_CMASK(sbi, b2); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1897 | } |
| 1898 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1899 | /* check for wrap through zero on extent logical start block*/ |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1900 | if (b1 + len1 < b1) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1901 | len1 = EXT_MAX_BLOCKS - b1; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1902 | newext->ee_len = cpu_to_le16(len1); |
| 1903 | ret = 1; |
| 1904 | } |
| 1905 | |
| 1906 | /* check for overlap */ |
| 1907 | if (b1 + len1 > b2) { |
| 1908 | newext->ee_len = cpu_to_le16(b2 - b1); |
| 1909 | ret = 1; |
| 1910 | } |
| 1911 | out: |
| 1912 | return ret; |
| 1913 | } |
| 1914 | |
| 1915 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1916 | * ext4_ext_insert_extent: |
| 1917 | * tries to merge requsted extent into the existing extent or |
| 1918 | * inserts requested extent as new one into the tree, |
| 1919 | * creating new leaf in the no-space case. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1920 | */ |
| 1921 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1922 | struct ext4_ext_path **ppath, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1923 | struct ext4_extent *newext, int gb_flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1924 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 1925 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1926 | struct ext4_extent_header *eh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1927 | struct ext4_extent *ex, *fex; |
| 1928 | struct ext4_extent *nearex; /* nearest extent */ |
| 1929 | struct ext4_ext_path *npath = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1930 | int depth, len, err; |
| 1931 | ext4_lblk_t next; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1932 | int mb_flags = 0, unwritten; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1933 | |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 1934 | if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 1935 | mb_flags |= EXT4_MB_DELALLOC_RESERVED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1936 | if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { |
| 1937 | EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1938 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1939 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1940 | depth = ext_depth(inode); |
| 1941 | ex = path[depth].p_ext; |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1942 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1943 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 1944 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 1945 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1946 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1947 | |
| 1948 | /* try to insert block into found extent and return */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 1949 | if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) { |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1950 | |
| 1951 | /* |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1952 | * Try to see whether we should rather test the extent on |
| 1953 | * 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] | 1954 | * ext4_find_extent() can return either extent on the |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1955 | * left, or on the right from the searched position. This |
| 1956 | * will make merging more effective. |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1957 | */ |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1958 | if (ex < EXT_LAST_EXTENT(eh) && |
| 1959 | (le32_to_cpu(ex->ee_block) + |
| 1960 | ext4_ext_get_actual_len(ex) < |
| 1961 | le32_to_cpu(newext->ee_block))) { |
| 1962 | ex += 1; |
| 1963 | goto prepend; |
| 1964 | } else if ((ex > EXT_FIRST_EXTENT(eh)) && |
| 1965 | (le32_to_cpu(newext->ee_block) + |
| 1966 | ext4_ext_get_actual_len(newext) < |
| 1967 | le32_to_cpu(ex->ee_block))) |
| 1968 | ex -= 1; |
| 1969 | |
| 1970 | /* Try to append newex to the ex */ |
| 1971 | if (ext4_can_extents_be_merged(inode, ex, newext)) { |
| 1972 | ext_debug("append [%d]%d block to %u:[%d]%d" |
| 1973 | "(from %llu)\n", |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1974 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1975 | ext4_ext_get_actual_len(newext), |
| 1976 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1977 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1978 | ext4_ext_get_actual_len(ex), |
| 1979 | ext4_ext_pblock(ex)); |
| 1980 | err = ext4_ext_get_access(handle, inode, |
| 1981 | path + depth); |
| 1982 | if (err) |
| 1983 | return err; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1984 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1985 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1986 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 1987 | if (unwritten) |
| 1988 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 1989 | eh = path[depth].p_hdr; |
| 1990 | nearex = ex; |
| 1991 | goto merge; |
| 1992 | } |
| 1993 | |
| 1994 | prepend: |
| 1995 | /* Try to prepend newex to the ex */ |
| 1996 | if (ext4_can_extents_be_merged(inode, newext, ex)) { |
| 1997 | ext_debug("prepend %u[%d]%d block to %u:[%d]%d" |
| 1998 | "(from %llu)\n", |
| 1999 | le32_to_cpu(newext->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2000 | ext4_ext_is_unwritten(newext), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2001 | ext4_ext_get_actual_len(newext), |
| 2002 | le32_to_cpu(ex->ee_block), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2003 | ext4_ext_is_unwritten(ex), |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2004 | ext4_ext_get_actual_len(ex), |
| 2005 | ext4_ext_pblock(ex)); |
| 2006 | err = ext4_ext_get_access(handle, inode, |
| 2007 | path + depth); |
| 2008 | if (err) |
| 2009 | return err; |
| 2010 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2011 | unwritten = ext4_ext_is_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2012 | ex->ee_block = newext->ee_block; |
| 2013 | ext4_ext_store_pblock(ex, ext4_ext_pblock(newext)); |
| 2014 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) |
| 2015 | + ext4_ext_get_actual_len(newext)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2016 | if (unwritten) |
| 2017 | ext4_ext_mark_unwritten(ex); |
Lukas Czerner | be8981b | 2013-04-03 23:33:28 -0400 | [diff] [blame] | 2018 | eh = path[depth].p_hdr; |
| 2019 | nearex = ex; |
| 2020 | goto merge; |
| 2021 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2024 | depth = ext_depth(inode); |
| 2025 | eh = path[depth].p_hdr; |
| 2026 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) |
| 2027 | goto has_space; |
| 2028 | |
| 2029 | /* probably next leaf has space for us? */ |
| 2030 | fex = EXT_LAST_EXTENT(eh); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2031 | next = EXT_MAX_BLOCKS; |
| 2032 | 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] | 2033 | next = ext4_ext_next_leaf_block(path); |
Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 2034 | if (next != EXT_MAX_BLOCKS) { |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2035 | ext_debug("next leaf block - %u\n", next); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2036 | BUG_ON(npath != NULL); |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 2037 | npath = ext4_find_extent(inode, next, NULL, gb_flags); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2038 | if (IS_ERR(npath)) |
| 2039 | return PTR_ERR(npath); |
| 2040 | BUG_ON(npath->p_depth != path->p_depth); |
| 2041 | eh = npath[depth].p_hdr; |
| 2042 | 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] | 2043 | ext_debug("next leaf isn't full(%d)\n", |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2044 | le16_to_cpu(eh->eh_entries)); |
| 2045 | path = npath; |
Robin Dong | ffb505f | 2011-07-11 11:43:59 -0400 | [diff] [blame] | 2046 | goto has_space; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2047 | } |
| 2048 | ext_debug("next leaf has no free space(%d,%d)\n", |
| 2049 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
| 2050 | } |
| 2051 | |
| 2052 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2053 | * There is no free space in the found leaf. |
| 2054 | * We're gonna add a new leaf in the tree. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2055 | */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2056 | if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 2057 | mb_flags |= EXT4_MB_USE_RESERVED; |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2058 | 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] | 2059 | ppath, newext); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2060 | if (err) |
| 2061 | goto cleanup; |
| 2062 | depth = ext_depth(inode); |
| 2063 | eh = path[depth].p_hdr; |
| 2064 | |
| 2065 | has_space: |
| 2066 | nearex = path[depth].p_ext; |
| 2067 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2068 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2069 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2070 | goto cleanup; |
| 2071 | |
| 2072 | if (!nearex) { |
| 2073 | /* there is no extent in this leaf, create first one */ |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2074 | ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2075 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2076 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2077 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2078 | ext4_ext_get_actual_len(newext)); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2079 | nearex = EXT_FIRST_EXTENT(eh); |
| 2080 | } else { |
| 2081 | if (le32_to_cpu(newext->ee_block) |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2082 | > le32_to_cpu(nearex->ee_block)) { |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2083 | /* Insert after */ |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2084 | ext_debug("insert %u:%llu:[%d]%d before: " |
| 2085 | "nearest %p\n", |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 2086 | le32_to_cpu(newext->ee_block), |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2087 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2088 | ext4_ext_is_unwritten(newext), |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2089 | ext4_ext_get_actual_len(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2090 | nearex); |
| 2091 | nearex++; |
| 2092 | } else { |
| 2093 | /* Insert before */ |
| 2094 | BUG_ON(newext->ee_block == nearex->ee_block); |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2095 | ext_debug("insert %u:%llu:[%d]%d after: " |
| 2096 | "nearest %p\n", |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2097 | le32_to_cpu(newext->ee_block), |
| 2098 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2099 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2100 | ext4_ext_get_actual_len(newext), |
| 2101 | nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2102 | } |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2103 | len = EXT_LAST_EXTENT(eh) - nearex + 1; |
| 2104 | if (len > 0) { |
Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 2105 | ext_debug("insert %u:%llu:[%d]%d: " |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2106 | "move %d extents from 0x%p to 0x%p\n", |
| 2107 | le32_to_cpu(newext->ee_block), |
| 2108 | ext4_ext_pblock(newext), |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2109 | ext4_ext_is_unwritten(newext), |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2110 | ext4_ext_get_actual_len(newext), |
| 2111 | len, nearex, nearex + 1); |
| 2112 | memmove(nearex + 1, nearex, |
| 2113 | len * sizeof(struct ext4_extent)); |
| 2114 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2115 | } |
| 2116 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2117 | le16_add_cpu(&eh->eh_entries, 1); |
Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 2118 | path[depth].p_ext = nearex; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2119 | nearex->ee_block = newext->ee_block; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2120 | ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2121 | nearex->ee_len = newext->ee_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2122 | |
| 2123 | merge: |
HaiboLiu | e7bcf82 | 2012-07-09 16:29:28 -0400 | [diff] [blame] | 2124 | /* try to merge extents */ |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2125 | if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2126 | ext4_ext_try_to_merge(handle, inode, path, nearex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2127 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2128 | |
| 2129 | /* time to correct all indexes above */ |
| 2130 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2131 | if (err) |
| 2132 | goto cleanup; |
| 2133 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 2134 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2135 | |
| 2136 | cleanup: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 2137 | ext4_ext_drop_refs(npath); |
| 2138 | kfree(npath); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2139 | return err; |
| 2140 | } |
| 2141 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 2142 | static int ext4_fill_es_cache_info(struct inode *inode, |
| 2143 | ext4_lblk_t block, ext4_lblk_t num, |
| 2144 | struct fiemap_extent_info *fieinfo) |
| 2145 | { |
| 2146 | ext4_lblk_t next, end = block + num - 1; |
| 2147 | struct extent_status es; |
| 2148 | unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; |
| 2149 | unsigned int flags; |
| 2150 | int err; |
| 2151 | |
| 2152 | while (block <= end) { |
| 2153 | next = 0; |
| 2154 | flags = 0; |
| 2155 | if (!ext4_es_lookup_extent(inode, block, &next, &es)) |
| 2156 | break; |
| 2157 | if (ext4_es_is_unwritten(&es)) |
| 2158 | flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 2159 | if (ext4_es_is_delayed(&es)) |
| 2160 | flags |= (FIEMAP_EXTENT_DELALLOC | |
| 2161 | FIEMAP_EXTENT_UNKNOWN); |
| 2162 | if (ext4_es_is_hole(&es)) |
| 2163 | flags |= EXT4_FIEMAP_EXTENT_HOLE; |
| 2164 | if (next == 0) |
| 2165 | flags |= FIEMAP_EXTENT_LAST; |
| 2166 | if (flags & (FIEMAP_EXTENT_DELALLOC| |
| 2167 | EXT4_FIEMAP_EXTENT_HOLE)) |
| 2168 | es.es_pblk = 0; |
| 2169 | else |
| 2170 | es.es_pblk = ext4_es_pblock(&es); |
| 2171 | err = fiemap_fill_next_extent(fieinfo, |
| 2172 | (__u64)es.es_lblk << blksize_bits, |
| 2173 | (__u64)es.es_pblk << blksize_bits, |
| 2174 | (__u64)es.es_len << blksize_bits, |
| 2175 | flags); |
| 2176 | if (next == 0) |
| 2177 | break; |
| 2178 | block = next; |
| 2179 | if (err < 0) |
| 2180 | return err; |
| 2181 | if (err == 1) |
| 2182 | return 0; |
| 2183 | } |
| 2184 | return 0; |
| 2185 | } |
| 2186 | |
| 2187 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2188 | /* |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2189 | * ext4_ext_determine_hole - determine hole around given block |
| 2190 | * @inode: inode we lookup in |
| 2191 | * @path: path in extent tree to @lblk |
| 2192 | * @lblk: pointer to logical block around which we want to determine hole |
| 2193 | * |
| 2194 | * Determine hole length (and start if easily possible) around given logical |
| 2195 | * block. We don't try too hard to find the beginning of the hole but @path |
| 2196 | * actually points to extent before @lblk, we provide it. |
| 2197 | * |
| 2198 | * The function returns the length of a hole starting at @lblk. We update @lblk |
| 2199 | * to the beginning of the hole if we managed to find it. |
| 2200 | */ |
| 2201 | static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode, |
| 2202 | struct ext4_ext_path *path, |
| 2203 | ext4_lblk_t *lblk) |
| 2204 | { |
| 2205 | int depth = ext_depth(inode); |
| 2206 | struct ext4_extent *ex; |
| 2207 | ext4_lblk_t len; |
| 2208 | |
| 2209 | ex = path[depth].p_ext; |
| 2210 | if (ex == NULL) { |
| 2211 | /* there is no extent yet, so gap is [0;-] */ |
| 2212 | *lblk = 0; |
| 2213 | len = EXT_MAX_BLOCKS; |
| 2214 | } else if (*lblk < le32_to_cpu(ex->ee_block)) { |
| 2215 | len = le32_to_cpu(ex->ee_block) - *lblk; |
| 2216 | } else if (*lblk >= le32_to_cpu(ex->ee_block) |
| 2217 | + ext4_ext_get_actual_len(ex)) { |
| 2218 | ext4_lblk_t next; |
| 2219 | |
| 2220 | *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); |
| 2221 | next = ext4_ext_next_allocated_block(path); |
| 2222 | BUG_ON(next == *lblk); |
| 2223 | len = next - *lblk; |
| 2224 | } else { |
| 2225 | BUG(); |
| 2226 | } |
| 2227 | return len; |
| 2228 | } |
| 2229 | |
| 2230 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2231 | * ext4_ext_put_gap_in_cache: |
| 2232 | * calculate boundaries of the gap that the requested block fits into |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2233 | * and cache this gap |
| 2234 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2235 | static void |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2236 | ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start, |
| 2237 | ext4_lblk_t hole_len) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2238 | { |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2239 | struct extent_status es; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2240 | |
Eric Whitney | ad43102 | 2018-10-01 14:10:39 -0400 | [diff] [blame] | 2241 | ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, |
| 2242 | hole_start + hole_len - 1, &es); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2243 | if (es.es_len) { |
| 2244 | /* There's delayed extent containing lblock? */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2245 | if (es.es_lblk <= hole_start) |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2246 | return; |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2247 | hole_len = min(es.es_lblk - hole_start, hole_len); |
Zheng Liu | 2f8e0a7 | 2014-11-25 11:44:37 -0500 | [diff] [blame] | 2248 | } |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 2249 | ext_debug(" -> %u:%u\n", hole_start, hole_len); |
| 2250 | ext4_es_insert_extent(inode, hole_start, hole_len, ~0, |
| 2251 | EXTENT_STATUS_HOLE); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2255 | * ext4_ext_rm_idx: |
| 2256 | * removes index from the index block. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2257 | */ |
Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2258 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2259 | struct ext4_ext_path *path, int depth) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2260 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2261 | int err; |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2262 | ext4_fsblk_t leaf; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2263 | |
| 2264 | /* free index block */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2265 | depth--; |
| 2266 | path = path + depth; |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2267 | leaf = ext4_idx_pblock(path->p_idx); |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2268 | if (unlikely(path->p_hdr->eh_entries == 0)) { |
| 2269 | EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2270 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2271 | } |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2272 | err = ext4_ext_get_access(handle, inode, path); |
| 2273 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2274 | return err; |
Robin Dong | 0e1147b | 2011-07-27 21:29:33 -0400 | [diff] [blame] | 2275 | |
| 2276 | if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { |
| 2277 | int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; |
| 2278 | len *= sizeof(struct ext4_extent_idx); |
| 2279 | memmove(path->p_idx, path->p_idx + 1, len); |
| 2280 | } |
| 2281 | |
Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2282 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2283 | err = ext4_ext_dirty(handle, inode, path); |
| 2284 | if (err) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2285 | return err; |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2286 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2287 | trace_ext4_ext_rm_idx(inode, leaf); |
| 2288 | |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 2289 | ext4_free_blocks(handle, inode, NULL, leaf, 1, |
Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2290 | EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2291 | |
| 2292 | while (--depth >= 0) { |
| 2293 | if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr)) |
| 2294 | break; |
| 2295 | path--; |
| 2296 | err = ext4_ext_get_access(handle, inode, path); |
| 2297 | if (err) |
| 2298 | break; |
| 2299 | path->p_idx->ei_block = (path+1)->p_idx->ei_block; |
| 2300 | err = ext4_ext_dirty(handle, inode, path); |
| 2301 | if (err) |
| 2302 | break; |
| 2303 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2304 | return err; |
| 2305 | } |
| 2306 | |
| 2307 | /* |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2308 | * ext4_ext_calc_credits_for_single_extent: |
| 2309 | * This routine returns max. credits that needed to insert an extent |
| 2310 | * to the extent tree. |
| 2311 | * When pass the actual path, the caller should calculate credits |
| 2312 | * under i_data_sem. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2313 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2314 | 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] | 2315 | struct ext4_ext_path *path) |
| 2316 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2317 | if (path) { |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2318 | int depth = ext_depth(inode); |
Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 2319 | int ret = 0; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2320 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2321 | /* probably there is space in leaf? */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2322 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2323 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
| 2324 | |
| 2325 | /* |
| 2326 | * There are some space in the leaf tree, no |
| 2327 | * need to account for leaf block credit |
| 2328 | * |
| 2329 | * bitmaps and block group descriptor blocks |
Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 2330 | * and other metadata blocks still need to be |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2331 | * accounted. |
| 2332 | */ |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2333 | /* 1 bitmap, 1 block group descriptor */ |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2334 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
Aneesh Kumar K.V | 5887e98 | 2009-07-05 23:12:04 -0400 | [diff] [blame] | 2335 | return ret; |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2336 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2339 | return ext4_chunk_trans_blocks(inode, nrblocks); |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2340 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2341 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2342 | /* |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2343 | * 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] | 2344 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2345 | * If we add a single extent, then in the worse case, each tree level |
| 2346 | * index/leaf need to be changed in case of the tree split. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2347 | * |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2348 | * If more extents are inserted, they could cause the whole tree split more |
| 2349 | * than once, but this is really rare. |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2350 | */ |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2351 | int ext4_ext_index_trans_blocks(struct inode *inode, int extents) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2352 | { |
| 2353 | int index; |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 2354 | int depth; |
| 2355 | |
| 2356 | /* If we are converting the inline data, only one is needed here. */ |
| 2357 | if (ext4_has_inline_data(inode)) |
| 2358 | return 1; |
| 2359 | |
| 2360 | depth = ext_depth(inode); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2361 | |
Jan Kara | fffb273 | 2013-06-04 13:01:11 -0400 | [diff] [blame] | 2362 | if (extents <= 1) |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2363 | index = depth * 2; |
| 2364 | else |
| 2365 | index = depth * 3; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2366 | |
Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2367 | return index; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2368 | } |
| 2369 | |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2370 | static inline int get_default_free_blocks_flags(struct inode *inode) |
| 2371 | { |
Tahsin Erdogan | ddfa17e | 2017-06-21 21:36:51 -0400 | [diff] [blame] | 2372 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) || |
| 2373 | ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE)) |
Theodore Ts'o | 981250c | 2013-06-12 11:48:29 -0400 | [diff] [blame] | 2374 | return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET; |
| 2375 | else if (ext4_should_journal_data(inode)) |
| 2376 | return EXT4_FREE_BLOCKS_FORGET; |
| 2377 | return 0; |
| 2378 | } |
| 2379 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2380 | /* |
| 2381 | * ext4_rereserve_cluster - increment the reserved cluster count when |
| 2382 | * freeing a cluster with a pending reservation |
| 2383 | * |
| 2384 | * @inode - file containing the cluster |
| 2385 | * @lblk - logical block in cluster to be reserved |
| 2386 | * |
| 2387 | * Increments the reserved cluster count and adjusts quota in a bigalloc |
| 2388 | * file system when freeing a partial cluster containing at least one |
| 2389 | * delayed and unwritten block. A partial cluster meeting that |
| 2390 | * requirement will have a pending reservation. If so, the |
| 2391 | * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to |
| 2392 | * defer reserved and allocated space accounting to a subsequent call |
| 2393 | * to this function. |
| 2394 | */ |
| 2395 | static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk) |
| 2396 | { |
| 2397 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 2398 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 2399 | |
| 2400 | dquot_reclaim_block(inode, EXT4_C2B(sbi, 1)); |
| 2401 | |
| 2402 | spin_lock(&ei->i_block_reservation_lock); |
| 2403 | ei->i_reserved_data_blocks++; |
| 2404 | percpu_counter_add(&sbi->s_dirtyclusters_counter, 1); |
| 2405 | spin_unlock(&ei->i_block_reservation_lock); |
| 2406 | |
| 2407 | percpu_counter_add(&sbi->s_freeclusters_counter, 1); |
| 2408 | ext4_remove_pending(inode, lblk); |
| 2409 | } |
| 2410 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2411 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2412 | struct ext4_extent *ex, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2413 | struct partial_cluster *partial, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2414 | ext4_lblk_t from, ext4_lblk_t to) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2415 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2416 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2417 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2418 | ext4_fsblk_t last_pblk, pblk; |
| 2419 | ext4_lblk_t num; |
| 2420 | int flags; |
| 2421 | |
| 2422 | /* only extent tail removal is allowed */ |
| 2423 | if (from < le32_to_cpu(ex->ee_block) || |
| 2424 | to != le32_to_cpu(ex->ee_block) + ee_len - 1) { |
| 2425 | ext4_error(sbi->s_sb, |
| 2426 | "strange request: removal(2) %u-%u from %u:%u", |
| 2427 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
| 2428 | return 0; |
| 2429 | } |
| 2430 | |
| 2431 | #ifdef EXTENTS_STATS |
| 2432 | spin_lock(&sbi->s_ext_stats_lock); |
| 2433 | sbi->s_ext_blocks += ee_len; |
| 2434 | sbi->s_ext_extents++; |
| 2435 | if (ee_len < sbi->s_ext_min) |
| 2436 | sbi->s_ext_min = ee_len; |
| 2437 | if (ee_len > sbi->s_ext_max) |
| 2438 | sbi->s_ext_max = ee_len; |
| 2439 | if (ext_depth(inode) > sbi->s_depth_max) |
| 2440 | sbi->s_depth_max = ext_depth(inode); |
| 2441 | spin_unlock(&sbi->s_ext_stats_lock); |
| 2442 | #endif |
| 2443 | |
| 2444 | trace_ext4_remove_blocks(inode, ex, from, to, partial); |
| 2445 | |
| 2446 | /* |
| 2447 | * if we have a partial cluster, and it's different from the |
| 2448 | * cluster of the last block in the extent, we free it |
| 2449 | */ |
| 2450 | last_pblk = ext4_ext_pblock(ex) + ee_len - 1; |
| 2451 | |
| 2452 | if (partial->state != initial && |
| 2453 | partial->pclu != EXT4_B2C(sbi, last_pblk)) { |
| 2454 | if (partial->state == tofree) { |
| 2455 | flags = get_default_free_blocks_flags(inode); |
| 2456 | if (ext4_is_pending(inode, partial->lblk)) |
| 2457 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
| 2458 | ext4_free_blocks(handle, inode, NULL, |
| 2459 | EXT4_C2B(sbi, partial->pclu), |
| 2460 | sbi->s_cluster_ratio, flags); |
| 2461 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2462 | ext4_rereserve_cluster(inode, partial->lblk); |
| 2463 | } |
| 2464 | partial->state = initial; |
| 2465 | } |
| 2466 | |
| 2467 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
| 2468 | pblk = ext4_ext_pblock(ex) + ee_len - num; |
| 2469 | |
| 2470 | /* |
| 2471 | * We free the partial cluster at the end of the extent (if any), |
| 2472 | * unless the cluster is used by another extent (partial_cluster |
| 2473 | * state is nofree). If a partial cluster exists here, it must be |
| 2474 | * shared with the last block in the extent. |
| 2475 | */ |
| 2476 | flags = get_default_free_blocks_flags(inode); |
| 2477 | |
| 2478 | /* partial, left end cluster aligned, right end unaligned */ |
| 2479 | if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) && |
| 2480 | (EXT4_LBLK_CMASK(sbi, to) >= from) && |
| 2481 | (partial->state != nofree)) { |
| 2482 | if (ext4_is_pending(inode, to)) |
| 2483 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
| 2484 | ext4_free_blocks(handle, inode, NULL, |
| 2485 | EXT4_PBLK_CMASK(sbi, last_pblk), |
| 2486 | sbi->s_cluster_ratio, flags); |
| 2487 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2488 | ext4_rereserve_cluster(inode, to); |
| 2489 | partial->state = initial; |
| 2490 | flags = get_default_free_blocks_flags(inode); |
| 2491 | } |
| 2492 | |
| 2493 | flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; |
Andrey Sidorov | 18888cf | 2012-09-19 14:14:53 -0400 | [diff] [blame] | 2494 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2495 | /* |
| 2496 | * For bigalloc file systems, we never free a partial cluster |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2497 | * 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] | 2498 | * need to free it on a subsequent call to ext4_remove_blocks, |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2499 | * 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] | 2500 | */ |
| 2501 | flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2502 | ext4_free_blocks(handle, inode, NULL, pblk, num, flags); |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2503 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2504 | /* reset the partial cluster if we've freed past it */ |
| 2505 | if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk)) |
| 2506 | partial->state = initial; |
| 2507 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2508 | /* |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2509 | * If we've freed the entire extent but the beginning is not left |
| 2510 | * cluster aligned and is not marked as ineligible for freeing we |
| 2511 | * record the partial cluster at the beginning of the extent. It |
| 2512 | * wasn't freed by the preceding ext4_free_blocks() call, and we |
| 2513 | * need to look farther to the left to determine if it's to be freed |
| 2514 | * (not shared with another extent). Else, reset the partial |
| 2515 | * cluster - we're either done freeing or the beginning of the |
| 2516 | * extent is left cluster aligned. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2517 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2518 | if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) { |
| 2519 | if (partial->state == initial) { |
| 2520 | partial->pclu = EXT4_B2C(sbi, pblk); |
| 2521 | partial->lblk = from; |
| 2522 | partial->state = tofree; |
Eric Whitney | 345ee94 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2523 | } |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2524 | } else { |
| 2525 | partial->state = initial; |
| 2526 | } |
| 2527 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2528 | return 0; |
| 2529 | } |
| 2530 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2531 | /* |
| 2532 | * ext4_ext_rm_leaf() Removes the extents associated with the |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2533 | * blocks appearing between "start" and "end". Both "start" |
| 2534 | * and "end" must appear in the same extent or EIO is returned. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2535 | * |
| 2536 | * @handle: The journal handle |
| 2537 | * @inode: The files inode |
| 2538 | * @path: The path to the leaf |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2539 | * @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] | 2540 | * has been released from it. However, if this value is |
| 2541 | * negative, it's a cluster just to the right of the |
| 2542 | * punched region and it must not be freed. |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2543 | * @start: The first block to remove |
| 2544 | * @end: The last block to remove |
| 2545 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2546 | static int |
| 2547 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2548 | struct ext4_ext_path *path, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2549 | struct partial_cluster *partial, |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2550 | ext4_lblk_t start, ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2551 | { |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2552 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2553 | int err = 0, correct_index = 0; |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2554 | int depth = ext_depth(inode), credits, revoke_credits; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2555 | struct ext4_extent_header *eh; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2556 | ext4_lblk_t a, b; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2557 | unsigned num; |
| 2558 | ext4_lblk_t ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2559 | unsigned short ex_ee_len; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2560 | unsigned unwritten = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2561 | struct ext4_extent *ex; |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2562 | ext4_fsblk_t pblk; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2563 | |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2564 | /* the header must be checked already in ext4_ext_remove_space() */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2565 | ext_debug("truncate since %u in leaf to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2566 | if (!path[depth].p_hdr) |
| 2567 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); |
| 2568 | eh = path[depth].p_hdr; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2569 | if (unlikely(path[depth].p_hdr == NULL)) { |
| 2570 | EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2571 | return -EFSCORRUPTED; |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2572 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2573 | /* find where to start removing */ |
Ashish Sangwan | 6ae06ff | 2013-07-01 08:12:41 -0400 | [diff] [blame] | 2574 | ex = path[depth].p_ext; |
| 2575 | if (!ex) |
| 2576 | ex = EXT_LAST_EXTENT(eh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2577 | |
| 2578 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2579 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2580 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2581 | trace_ext4_ext_rm_leaf(inode, start, ex, partial); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2582 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2583 | while (ex >= EXT_FIRST_EXTENT(eh) && |
| 2584 | ex_ee_block + ex_ee_len > start) { |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2585 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2586 | if (ext4_ext_is_unwritten(ex)) |
| 2587 | unwritten = 1; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2588 | else |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2589 | unwritten = 0; |
Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2590 | |
Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 2591 | ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2592 | unwritten, ex_ee_len); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2593 | path[depth].p_ext = ex; |
| 2594 | |
| 2595 | a = ex_ee_block > start ? ex_ee_block : start; |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2596 | b = ex_ee_block+ex_ee_len - 1 < end ? |
| 2597 | ex_ee_block+ex_ee_len - 1 : end; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2598 | |
| 2599 | ext_debug(" border %u:%u\n", a, b); |
| 2600 | |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2601 | /* If this extent is beyond the end of the hole, skip it */ |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2602 | if (end < ex_ee_block) { |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2603 | /* |
| 2604 | * We're going to skip this extent and move to another, |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2605 | * so note that its first cluster is in use to avoid |
| 2606 | * freeing it when removing blocks. Eventually, the |
| 2607 | * right edge of the truncated/punched region will |
| 2608 | * be just to the left. |
Lukas Czerner | d23142c | 2013-05-27 23:33:35 -0400 | [diff] [blame] | 2609 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2610 | if (sbi->s_cluster_ratio > 1) { |
| 2611 | pblk = ext4_ext_pblock(ex); |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2612 | partial->pclu = EXT4_B2C(sbi, pblk); |
| 2613 | partial->state = nofree; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2614 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2615 | ex--; |
| 2616 | ex_ee_block = le32_to_cpu(ex->ee_block); |
| 2617 | ex_ee_len = ext4_ext_get_actual_len(ex); |
| 2618 | continue; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2619 | } else if (b != ex_ee_block + ex_ee_len - 1) { |
Lukas Czerner | dc1841d | 2012-03-19 23:07:43 -0400 | [diff] [blame] | 2620 | EXT4_ERROR_INODE(inode, |
| 2621 | "can not handle truncate %u:%u " |
| 2622 | "on extent %u:%u", |
| 2623 | start, end, ex_ee_block, |
| 2624 | ex_ee_block + ex_ee_len - 1); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2625 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2626 | goto out; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2627 | } else if (a != ex_ee_block) { |
| 2628 | /* remove tail of the extent */ |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2629 | num = a - ex_ee_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2630 | } else { |
| 2631 | /* remove whole extent: excellent! */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2632 | num = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2633 | } |
Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 2634 | /* |
| 2635 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 2636 | * descriptor) for each block group; assume two block |
| 2637 | * groups plus ex_ee_len/blocks_per_block_group for |
| 2638 | * the worst case |
| 2639 | */ |
| 2640 | 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] | 2641 | if (ex == EXT_FIRST_EXTENT(eh)) { |
| 2642 | correct_index = 1; |
| 2643 | credits += (ext_depth(inode)) + 1; |
| 2644 | } |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2645 | credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2646 | /* |
| 2647 | * We may end up freeing some index blocks and data from the |
| 2648 | * punched range. Note that partial clusters are accounted for |
| 2649 | * by ext4_free_data_revoke_credits(). |
| 2650 | */ |
| 2651 | revoke_credits = |
| 2652 | ext4_free_metadata_revoke_credits(inode->i_sb, |
| 2653 | ext_depth(inode)) + |
| 2654 | ext4_free_data_revoke_credits(inode, b - a + 1); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2655 | |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2656 | err = ext4_datasem_ensure_credits(handle, inode, credits, |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2657 | credits, revoke_credits); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2658 | if (err) { |
| 2659 | if (err > 0) |
| 2660 | err = -EAGAIN; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2661 | goto out; |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 2662 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2663 | |
| 2664 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 2665 | if (err) |
| 2666 | goto out; |
| 2667 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2668 | err = ext4_remove_blocks(handle, inode, ex, partial, a, b); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2669 | if (err) |
| 2670 | goto out; |
| 2671 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2672 | if (num == 0) |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2673 | /* this extent is removed; mark slot entirely unused */ |
Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2674 | ext4_ext_store_pblock(ex, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2675 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2676 | ex->ee_len = cpu_to_le16(num); |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2677 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2678 | * Do not mark unwritten if all the blocks in the |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2679 | * extent have been removed. |
| 2680 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 2681 | if (unwritten && num) |
| 2682 | ext4_ext_mark_unwritten(ex); |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2683 | /* |
| 2684 | * If the extent was completely released, |
| 2685 | * we need to remove it from the leaf |
| 2686 | */ |
| 2687 | if (num == 0) { |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2688 | if (end != EXT_MAX_BLOCKS - 1) { |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2689 | /* |
| 2690 | * For hole punching, we need to scoot all the |
| 2691 | * extents up when an extent is removed so that |
| 2692 | * we dont have blank extents in the middle |
| 2693 | */ |
| 2694 | memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * |
| 2695 | sizeof(struct ext4_extent)); |
| 2696 | |
| 2697 | /* Now get rid of the one at the end */ |
| 2698 | memset(EXT_LAST_EXTENT(eh), 0, |
| 2699 | sizeof(struct ext4_extent)); |
| 2700 | } |
| 2701 | le16_add_cpu(&eh->eh_entries, -1); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2702 | } |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2703 | |
Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2704 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 2705 | if (err) |
| 2706 | goto out; |
| 2707 | |
Yongqiang Yang | bf52c6f | 2011-11-01 18:59:26 -0400 | [diff] [blame] | 2708 | 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] | 2709 | ext4_ext_pblock(ex)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2710 | ex--; |
| 2711 | ex_ee_block = le32_to_cpu(ex->ee_block); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2712 | ex_ee_len = ext4_ext_get_actual_len(ex); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | if (correct_index && eh->eh_entries) |
| 2716 | err = ext4_ext_correct_indexes(handle, inode, path); |
| 2717 | |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2718 | /* |
Eric Whitney | ad6599a | 2014-04-01 19:49:30 -0400 | [diff] [blame] | 2719 | * If there's a partial cluster and at least one extent remains in |
| 2720 | * 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] | 2721 | * current extent. If it is shared with the current extent |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2722 | * 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] | 2723 | * truncated/punched region and we're done removing blocks. |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2724 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2725 | if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) { |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2726 | pblk = ext4_ext_pblock(ex) + ex_ee_len - 1; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2727 | if (partial->pclu != EXT4_B2C(sbi, pblk)) { |
| 2728 | int flags = get_default_free_blocks_flags(inode); |
| 2729 | |
| 2730 | if (ext4_is_pending(inode, partial->lblk)) |
| 2731 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2732 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2733 | EXT4_C2B(sbi, partial->pclu), |
| 2734 | sbi->s_cluster_ratio, flags); |
| 2735 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 2736 | ext4_rereserve_cluster(inode, partial->lblk); |
Eric Whitney | 5bf4376 | 2014-11-23 00:58:11 -0500 | [diff] [blame] | 2737 | } |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2738 | partial->state = initial; |
Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2739 | } |
| 2740 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2741 | /* if this leaf is free, then we should |
| 2742 | * remove it from index block above */ |
| 2743 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2744 | err = ext4_ext_rm_idx(handle, inode, path, depth); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2745 | |
| 2746 | out: |
| 2747 | return err; |
| 2748 | } |
| 2749 | |
| 2750 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2751 | * ext4_ext_more_to_rm: |
| 2752 | * returns 1 if current index has to be freed (even partial) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2753 | */ |
Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2754 | static int |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2755 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
| 2756 | { |
| 2757 | BUG_ON(path->p_idx == NULL); |
| 2758 | |
| 2759 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) |
| 2760 | return 0; |
| 2761 | |
| 2762 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2763 | * if truncate on deeper level happened, it wasn't partial, |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2764 | * so we have to consider current index for truncation |
| 2765 | */ |
| 2766 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) |
| 2767 | return 0; |
| 2768 | return 1; |
| 2769 | } |
| 2770 | |
Theodore Ts'o | 26a4c0c | 2013-04-03 12:45:17 -0400 | [diff] [blame] | 2771 | int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, |
| 2772 | ext4_lblk_t end) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2773 | { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2774 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2775 | int depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2776 | struct ext4_ext_path *path = NULL; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2777 | struct partial_cluster partial; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2778 | handle_t *handle; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2779 | int i = 0, err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2780 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2781 | partial.pclu = 0; |
| 2782 | partial.lblk = 0; |
| 2783 | partial.state = initial; |
| 2784 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2785 | ext_debug("truncate since %u to %u\n", start, end); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2786 | |
| 2787 | /* probably first extent we're gonna free will be last in block */ |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 2788 | handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE, |
| 2789 | depth + 1, |
| 2790 | ext4_free_metadata_revoke_credits(inode->i_sb, depth)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2791 | if (IS_ERR(handle)) |
| 2792 | return PTR_ERR(handle); |
| 2793 | |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2794 | again: |
Lukas Czerner | 6180132 | 2013-05-27 23:32:35 -0400 | [diff] [blame] | 2795 | trace_ext4_ext_remove_space(inode, start, end, depth); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2796 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2797 | /* |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2798 | * Check if we are removing extents inside the extent tree. If that |
| 2799 | * is the case, we are going to punch a hole inside the extent tree |
| 2800 | * so we have to check whether we need to split the extent covering |
| 2801 | * the last block to remove so we can easily remove the part of it |
| 2802 | * in ext4_ext_rm_leaf(). |
| 2803 | */ |
| 2804 | if (end < EXT_MAX_BLOCKS - 1) { |
| 2805 | struct ext4_extent *ex; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2806 | ext4_lblk_t ee_block, ex_end, lblk; |
| 2807 | ext4_fsblk_t pblk; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2808 | |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2809 | /* find extent for or closest extent to this block */ |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 2810 | path = ext4_find_extent(inode, end, NULL, |
| 2811 | EXT4_EX_NOCACHE | EXT4_EX_NOFAIL); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2812 | if (IS_ERR(path)) { |
| 2813 | ext4_journal_stop(handle); |
| 2814 | return PTR_ERR(path); |
| 2815 | } |
| 2816 | depth = ext_depth(inode); |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2817 | /* 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] | 2818 | ex = path[depth].p_ext; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2819 | if (!ex) { |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2820 | if (depth) { |
| 2821 | EXT4_ERROR_INODE(inode, |
| 2822 | "path[%d].p_hdr == NULL", |
| 2823 | depth); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2824 | err = -EFSCORRUPTED; |
Dmitry Monakhov | 6f2080e | 2012-09-30 23:03:50 -0400 | [diff] [blame] | 2825 | } |
| 2826 | goto out; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2827 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2828 | |
| 2829 | ee_block = le32_to_cpu(ex->ee_block); |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2830 | ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2831 | |
| 2832 | /* |
| 2833 | * See if the last block is inside the extent, if so split |
| 2834 | * the extent at 'end' block so we can easily remove the |
| 2835 | * tail of the first part of the split extent in |
| 2836 | * ext4_ext_rm_leaf(). |
| 2837 | */ |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2838 | if (end >= ee_block && end < ex_end) { |
| 2839 | |
| 2840 | /* |
| 2841 | * If we're going to split the extent, note that |
| 2842 | * the cluster containing the block after 'end' is |
| 2843 | * in use to avoid freeing it when removing blocks. |
| 2844 | */ |
| 2845 | if (sbi->s_cluster_ratio > 1) { |
| 2846 | pblk = ext4_ext_pblock(ex) + end - ee_block + 2; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2847 | partial.pclu = EXT4_B2C(sbi, pblk); |
| 2848 | partial.state = nofree; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2849 | } |
| 2850 | |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2851 | /* |
| 2852 | * Split the extent in two so that 'end' is the last |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 2853 | * block in the first new extent. Also we should not |
| 2854 | * fail removing space due to ENOSPC so try to use |
| 2855 | * reserved block if that happens. |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2856 | */ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 2857 | err = ext4_force_split_extent_at(handle, inode, &path, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 2858 | end + 1, 1); |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2859 | if (err < 0) |
| 2860 | goto out; |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2861 | |
Eric Whitney | 7bd7523 | 2019-02-28 23:34:11 -0500 | [diff] [blame] | 2862 | } else if (sbi->s_cluster_ratio > 1 && end >= ex_end && |
| 2863 | partial.state == initial) { |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2864 | /* |
Eric Whitney | 7bd7523 | 2019-02-28 23:34:11 -0500 | [diff] [blame] | 2865 | * If we're punching, there's an extent to the right. |
| 2866 | * If the partial cluster hasn't been set, set it to |
| 2867 | * that extent's first cluster and its state to nofree |
| 2868 | * so it won't be freed should it contain blocks to be |
| 2869 | * removed. If it's already set (tofree/nofree), we're |
| 2870 | * retrying and keep the original partial cluster info |
| 2871 | * so a cluster marked tofree as a result of earlier |
| 2872 | * extent removal is not lost. |
Eric Whitney | f4226d9 | 2014-11-23 00:55:42 -0500 | [diff] [blame] | 2873 | */ |
| 2874 | lblk = ex_end + 1; |
| 2875 | err = ext4_ext_search_right(inode, path, &lblk, &pblk, |
| 2876 | &ex); |
| 2877 | if (err) |
| 2878 | goto out; |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2879 | if (pblk) { |
| 2880 | partial.pclu = EXT4_B2C(sbi, pblk); |
| 2881 | partial.state = nofree; |
| 2882 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2883 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2884 | } |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2885 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2886 | * We start scanning from right side, freeing all the blocks |
| 2887 | * after i_size and walking into the tree depth-wise. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2888 | */ |
Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2889 | depth = ext_depth(inode); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2890 | if (path) { |
| 2891 | int k = i = depth; |
| 2892 | while (--k > 0) |
| 2893 | path[k].p_block = |
| 2894 | le16_to_cpu(path[k].p_hdr->eh_entries)+1; |
| 2895 | } else { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2896 | path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 2897 | GFP_NOFS | __GFP_NOFAIL); |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2898 | if (path == NULL) { |
| 2899 | ext4_journal_stop(handle); |
| 2900 | return -ENOMEM; |
| 2901 | } |
Theodore Ts'o | 10809df8 | 2014-09-01 14:40:09 -0400 | [diff] [blame] | 2902 | path[0].p_maxdepth = path[0].p_depth = depth; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2903 | path[0].p_hdr = ext_inode_hdr(inode); |
Theodore Ts'o | 89a4e48 | 2012-08-17 08:54:52 -0400 | [diff] [blame] | 2904 | i = 0; |
Lukas Czerner | 5f95d21 | 2012-03-19 23:03:19 -0400 | [diff] [blame] | 2905 | |
Theodore Ts'o | c349179 | 2013-08-16 21:21:41 -0400 | [diff] [blame] | 2906 | if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2907 | err = -EFSCORRUPTED; |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2908 | goto out; |
| 2909 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2910 | } |
Ashish Sangwan | 968dee7 | 2012-07-22 22:49:08 -0400 | [diff] [blame] | 2911 | err = 0; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2912 | |
| 2913 | while (i >= 0 && err == 0) { |
| 2914 | if (i == depth) { |
| 2915 | /* this is leaf block */ |
Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2916 | err = ext4_ext_rm_leaf(handle, inode, path, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2917 | &partial, start, end); |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2918 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2919 | brelse(path[i].p_bh); |
| 2920 | path[i].p_bh = NULL; |
| 2921 | i--; |
| 2922 | continue; |
| 2923 | } |
| 2924 | |
| 2925 | /* this is index block */ |
| 2926 | if (!path[i].p_hdr) { |
| 2927 | ext_debug("initialize header\n"); |
| 2928 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2929 | } |
| 2930 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2931 | if (!path[i].p_idx) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2932 | /* this level hasn't been touched yet */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2933 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
| 2934 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; |
| 2935 | ext_debug("init index ptr: hdr 0x%p, num %d\n", |
| 2936 | path[i].p_hdr, |
| 2937 | le16_to_cpu(path[i].p_hdr->eh_entries)); |
| 2938 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2939 | /* we were already here, see at next index */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2940 | path[i].p_idx--; |
| 2941 | } |
| 2942 | |
| 2943 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", |
| 2944 | i, EXT_FIRST_INDEX(path[i].p_hdr), |
| 2945 | path[i].p_idx); |
| 2946 | if (ext4_ext_more_to_rm(path + i)) { |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2947 | struct buffer_head *bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2948 | /* go to the next level */ |
Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2949 | ext_debug("move to level %d (block %llu)\n", |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2950 | i + 1, ext4_idx_pblock(path[i].p_idx)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2951 | memset(path + i + 1, 0, sizeof(*path)); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2952 | bh = read_extent_tree_block(inode, |
Theodore Ts'o | 107a7bd | 2013-08-16 21:23:41 -0400 | [diff] [blame] | 2953 | ext4_idx_pblock(path[i].p_idx), depth - i - 1, |
| 2954 | EXT4_EX_NOCACHE); |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2955 | if (IS_ERR(bh)) { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2956 | /* should we reset i_size? */ |
Theodore Ts'o | 7d7ea89 | 2013-08-16 21:20:41 -0400 | [diff] [blame] | 2957 | err = PTR_ERR(bh); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2958 | break; |
| 2959 | } |
Theodore Ts'o | 76828c88 | 2013-07-15 12:27:47 -0400 | [diff] [blame] | 2960 | /* Yield here to deal with large extent trees. |
| 2961 | * Should be a no-op if we did IO above. */ |
| 2962 | cond_resched(); |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2963 | if (WARN_ON(i + 1 > depth)) { |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 2964 | err = -EFSCORRUPTED; |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2965 | break; |
| 2966 | } |
Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2967 | path[i + 1].p_bh = bh; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2968 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2969 | /* save actual number of indexes since this |
| 2970 | * number is changed at the next iteration */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2971 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
| 2972 | i++; |
| 2973 | } else { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2974 | /* we finished processing this index, go up */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2975 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2976 | /* index is empty, remove it; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2977 | * handle must be already prepared by the |
| 2978 | * truncatei_leaf() */ |
Forrest Liu | c36575e | 2012-12-17 09:55:39 -0500 | [diff] [blame] | 2979 | err = ext4_ext_rm_idx(handle, inode, path, i); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2980 | } |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2981 | /* root level has p_bh == NULL, brelse() eats this */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2982 | brelse(path[i].p_bh); |
| 2983 | path[i].p_bh = NULL; |
| 2984 | i--; |
| 2985 | ext_debug("return to level %d\n", i); |
| 2986 | } |
| 2987 | } |
| 2988 | |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2989 | trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial, |
| 2990 | path->p_hdr->eh_entries); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2991 | |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2992 | /* |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2993 | * if there's a partial cluster and we have removed the first extent |
| 2994 | * in the file, then we also free the partial cluster, if any |
Eric Whitney | 0756b90 | 2014-11-23 00:59:39 -0500 | [diff] [blame] | 2995 | */ |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 2996 | if (partial.state == tofree && err == 0) { |
| 2997 | int flags = get_default_free_blocks_flags(inode); |
| 2998 | |
| 2999 | if (ext4_is_pending(inode, partial.lblk)) |
| 3000 | flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3001 | ext4_free_blocks(handle, inode, NULL, |
Eric Whitney | 9fe6714 | 2018-10-01 14:25:08 -0400 | [diff] [blame] | 3002 | EXT4_C2B(sbi, partial.pclu), |
| 3003 | sbi->s_cluster_ratio, flags); |
| 3004 | if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) |
| 3005 | ext4_rereserve_cluster(inode, partial.lblk); |
| 3006 | partial.state = initial; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3007 | } |
| 3008 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3009 | /* TODO: flexible tree reduction should be here */ |
| 3010 | if (path->p_hdr->eh_entries == 0) { |
| 3011 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3012 | * truncate to zero freed all the tree, |
| 3013 | * so we need to correct eh_depth |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3014 | */ |
| 3015 | err = ext4_ext_get_access(handle, inode, path); |
| 3016 | if (err == 0) { |
| 3017 | ext_inode_hdr(inode)->eh_depth = 0; |
| 3018 | ext_inode_hdr(inode)->eh_max = |
Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 3019 | cpu_to_le16(ext4_ext_space_root(inode, 0)); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3020 | err = ext4_ext_dirty(handle, inode, path); |
| 3021 | } |
| 3022 | } |
| 3023 | out: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 3024 | ext4_ext_drop_refs(path); |
| 3025 | kfree(path); |
| 3026 | path = NULL; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3027 | if (err == -EAGAIN) |
| 3028 | goto again; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3029 | ext4_journal_stop(handle); |
| 3030 | |
| 3031 | return err; |
| 3032 | } |
| 3033 | |
| 3034 | /* |
| 3035 | * called at mount time |
| 3036 | */ |
| 3037 | void ext4_ext_init(struct super_block *sb) |
| 3038 | { |
| 3039 | /* |
| 3040 | * possible initialization would be here |
| 3041 | */ |
| 3042 | |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3043 | if (ext4_has_feature_extents(sb)) { |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3044 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3045 | printk(KERN_INFO "EXT4-fs: file extents enabled" |
Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 3046 | #ifdef AGGRESSIVE_TEST |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3047 | ", aggressive tests" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3048 | #endif |
| 3049 | #ifdef CHECK_BINSEARCH |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3050 | ", check binsearch" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3051 | #endif |
| 3052 | #ifdef EXTENTS_STATS |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3053 | ", stats" |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3054 | #endif |
Theodore Ts'o | 92b9781 | 2012-03-19 23:41:49 -0400 | [diff] [blame] | 3055 | "\n"); |
Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 3056 | #endif |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3057 | #ifdef EXTENTS_STATS |
| 3058 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); |
| 3059 | EXT4_SB(sb)->s_ext_min = 1 << 30; |
| 3060 | EXT4_SB(sb)->s_ext_max = 0; |
| 3061 | #endif |
| 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | /* |
| 3066 | * called at umount time |
| 3067 | */ |
| 3068 | void ext4_ext_release(struct super_block *sb) |
| 3069 | { |
Darrick J. Wong | e2b911c | 2015-10-17 16:18:43 -0400 | [diff] [blame] | 3070 | if (!ext4_has_feature_extents(sb)) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3071 | return; |
| 3072 | |
| 3073 | #ifdef EXTENTS_STATS |
| 3074 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { |
| 3075 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
| 3076 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", |
| 3077 | sbi->s_ext_blocks, sbi->s_ext_extents, |
| 3078 | sbi->s_ext_blocks / sbi->s_ext_extents); |
| 3079 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", |
| 3080 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); |
| 3081 | } |
| 3082 | #endif |
| 3083 | } |
| 3084 | |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3085 | static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex) |
| 3086 | { |
| 3087 | ext4_lblk_t ee_block; |
| 3088 | ext4_fsblk_t ee_pblock; |
| 3089 | unsigned int ee_len; |
| 3090 | |
| 3091 | ee_block = le32_to_cpu(ex->ee_block); |
| 3092 | ee_len = ext4_ext_get_actual_len(ex); |
| 3093 | ee_pblock = ext4_ext_pblock(ex); |
| 3094 | |
| 3095 | if (ee_len == 0) |
| 3096 | return 0; |
| 3097 | |
| 3098 | return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, |
| 3099 | EXTENT_STATUS_WRITTEN); |
| 3100 | } |
| 3101 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3102 | /* FIXME!! we need to try to merge to left or right after zero-out */ |
| 3103 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) |
| 3104 | { |
Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 3105 | ext4_fsblk_t ee_pblock; |
| 3106 | unsigned int ee_len; |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3107 | |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3108 | ee_len = ext4_ext_get_actual_len(ex); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3109 | ee_pblock = ext4_ext_pblock(ex); |
Jan Kara | 53085fa | 2015-12-07 15:09:35 -0500 | [diff] [blame] | 3110 | return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock, |
| 3111 | ee_len); |
Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 3112 | } |
| 3113 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3114 | /* |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3115 | * ext4_split_extent_at() splits an extent at given block. |
| 3116 | * |
| 3117 | * @handle: the journal handle |
| 3118 | * @inode: the file inode |
| 3119 | * @path: the path to the extent |
| 3120 | * @split: the logical block where the extent is splitted. |
| 3121 | * @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] | 3122 | * the states(init or unwritten) of new extents. |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3123 | * @flags: flags used to insert new extent to extent tree. |
| 3124 | * |
| 3125 | * |
| 3126 | * Splits extent [a, b] into two extents [a, @split) and [@split, b], states |
| 3127 | * of which are deterimined by split_flag. |
| 3128 | * |
| 3129 | * There are two cases: |
| 3130 | * a> the extent are splitted into two extent. |
| 3131 | * b> split is not needed, and just mark the extent. |
| 3132 | * |
| 3133 | * return 0 on success. |
| 3134 | */ |
| 3135 | static int ext4_split_extent_at(handle_t *handle, |
| 3136 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3137 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3138 | ext4_lblk_t split, |
| 3139 | int split_flag, |
| 3140 | int flags) |
| 3141 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3142 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3143 | ext4_fsblk_t newblock; |
| 3144 | ext4_lblk_t ee_block; |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3145 | struct ext4_extent *ex, newex, orig_ex, zero_ex; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3146 | struct ext4_extent *ex2 = NULL; |
| 3147 | unsigned int ee_len, depth; |
| 3148 | int err = 0; |
| 3149 | |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3150 | BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) == |
| 3151 | (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)); |
| 3152 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3153 | ext_debug("ext4_split_extents_at: inode %lu, logical" |
| 3154 | "block %llu\n", inode->i_ino, (unsigned long long)split); |
| 3155 | |
| 3156 | ext4_ext_show_leaf(inode, path); |
| 3157 | |
| 3158 | depth = ext_depth(inode); |
| 3159 | ex = path[depth].p_ext; |
| 3160 | ee_block = le32_to_cpu(ex->ee_block); |
| 3161 | ee_len = ext4_ext_get_actual_len(ex); |
| 3162 | newblock = split - ee_block + ext4_ext_pblock(ex); |
| 3163 | |
| 3164 | BUG_ON(split < ee_block || split >= (ee_block + ee_len)); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3165 | BUG_ON(!ext4_ext_is_unwritten(ex) && |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3166 | split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3167 | EXT4_EXT_MARK_UNWRIT1 | |
| 3168 | EXT4_EXT_MARK_UNWRIT2)); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3169 | |
| 3170 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3171 | if (err) |
| 3172 | goto out; |
| 3173 | |
| 3174 | if (split == ee_block) { |
| 3175 | /* |
| 3176 | * case b: block @split is the block that the extent begins with |
| 3177 | * then we just change the state of the extent, and splitting |
| 3178 | * is not needed. |
| 3179 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3180 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3181 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3182 | else |
| 3183 | ext4_ext_mark_initialized(ex); |
| 3184 | |
| 3185 | if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3186 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3187 | |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3188 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3189 | goto out; |
| 3190 | } |
| 3191 | |
| 3192 | /* case a */ |
| 3193 | memcpy(&orig_ex, ex, sizeof(orig_ex)); |
| 3194 | ex->ee_len = cpu_to_le16(split - ee_block); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3195 | if (split_flag & EXT4_EXT_MARK_UNWRIT1) |
| 3196 | ext4_ext_mark_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3197 | |
| 3198 | /* |
| 3199 | * path may lead to new leaf, not to original leaf any more |
| 3200 | * after ext4_ext_insert_extent() returns, |
| 3201 | */ |
| 3202 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 3203 | if (err) |
| 3204 | goto fix_extent_len; |
| 3205 | |
| 3206 | ex2 = &newex; |
| 3207 | ex2->ee_block = cpu_to_le32(split); |
| 3208 | ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); |
| 3209 | ext4_ext_store_pblock(ex2, newblock); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3210 | if (split_flag & EXT4_EXT_MARK_UNWRIT2) |
| 3211 | ext4_ext_mark_unwritten(ex2); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3212 | |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3213 | err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3214 | if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3215 | if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3216 | if (split_flag & EXT4_EXT_DATA_VALID1) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3217 | err = ext4_ext_zeroout(inode, ex2); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3218 | zero_ex.ee_block = ex2->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3219 | zero_ex.ee_len = cpu_to_le16( |
| 3220 | ext4_ext_get_actual_len(ex2)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3221 | ext4_ext_store_pblock(&zero_ex, |
| 3222 | ext4_ext_pblock(ex2)); |
| 3223 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3224 | err = ext4_ext_zeroout(inode, ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3225 | zero_ex.ee_block = ex->ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3226 | zero_ex.ee_len = cpu_to_le16( |
| 3227 | ext4_ext_get_actual_len(ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3228 | ext4_ext_store_pblock(&zero_ex, |
| 3229 | ext4_ext_pblock(ex)); |
| 3230 | } |
| 3231 | } else { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3232 | err = ext4_ext_zeroout(inode, &orig_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3233 | zero_ex.ee_block = orig_ex.ee_block; |
Zheng Liu | 8cde7ad | 2013-04-03 12:27:18 -0400 | [diff] [blame] | 3234 | zero_ex.ee_len = cpu_to_le16( |
| 3235 | ext4_ext_get_actual_len(&orig_ex)); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3236 | ext4_ext_store_pblock(&zero_ex, |
| 3237 | ext4_ext_pblock(&orig_ex)); |
| 3238 | } |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3239 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3240 | if (err) |
| 3241 | goto fix_extent_len; |
| 3242 | /* update the extent length and mark as initialized */ |
Al Viro | af1584f | 2012-04-12 20:32:25 -0400 | [diff] [blame] | 3243 | ex->ee_len = cpu_to_le16(ee_len); |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3244 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3245 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3246 | if (err) |
| 3247 | goto fix_extent_len; |
| 3248 | |
| 3249 | /* update extent status tree */ |
Zheng Liu | d7b2a00 | 2013-08-28 14:47:06 -0400 | [diff] [blame] | 3250 | err = ext4_zeroout_es(inode, &zero_ex); |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3251 | |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3252 | goto out; |
| 3253 | } else if (err) |
| 3254 | goto fix_extent_len; |
| 3255 | |
| 3256 | out: |
| 3257 | ext4_ext_show_leaf(inode, path); |
| 3258 | return err; |
| 3259 | |
| 3260 | fix_extent_len: |
| 3261 | ex->ee_len = orig_ex.ee_len; |
Harshad Shirwadkar | b60ca33 | 2020-04-26 18:34:38 -0700 | [diff] [blame] | 3262 | /* |
| 3263 | * Ignore ext4_ext_dirty return value since we are already in error path |
| 3264 | * and err is a non-zero error code. |
| 3265 | */ |
Dmitry Monakhov | 29faed1 | 2014-07-27 22:30:29 -0400 | [diff] [blame] | 3266 | ext4_ext_dirty(handle, inode, path + path->p_depth); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3267 | return err; |
| 3268 | } |
| 3269 | |
| 3270 | /* |
| 3271 | * ext4_split_extents() splits an extent and mark extent which is covered |
| 3272 | * by @map as split_flags indicates |
| 3273 | * |
Anatol Pomozov | 70261f5 | 2013-08-28 14:40:12 -0400 | [diff] [blame] | 3274 | * 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] | 3275 | * There are three possibilities: |
| 3276 | * a> There is no split required |
| 3277 | * b> Splits in two extents: Split is happening at either end of the extent |
| 3278 | * c> Splits in three extents: Somone is splitting in middle of the extent |
| 3279 | * |
| 3280 | */ |
| 3281 | static int ext4_split_extent(handle_t *handle, |
| 3282 | struct inode *inode, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3283 | struct ext4_ext_path **ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3284 | struct ext4_map_blocks *map, |
| 3285 | int split_flag, |
| 3286 | int flags) |
| 3287 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3288 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3289 | ext4_lblk_t ee_block; |
| 3290 | struct ext4_extent *ex; |
| 3291 | unsigned int ee_len, depth; |
| 3292 | int err = 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3293 | int unwritten; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3294 | int split_flag1, flags1; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3295 | int allocated = map->m_len; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3296 | |
| 3297 | depth = ext_depth(inode); |
| 3298 | ex = path[depth].p_ext; |
| 3299 | ee_block = le32_to_cpu(ex->ee_block); |
| 3300 | ee_len = ext4_ext_get_actual_len(ex); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3301 | unwritten = ext4_ext_is_unwritten(ex); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3302 | |
| 3303 | if (map->m_lblk + map->m_len < ee_block + ee_len) { |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3304 | split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3305 | flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3306 | if (unwritten) |
| 3307 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1 | |
| 3308 | EXT4_EXT_MARK_UNWRIT2; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3309 | if (split_flag & EXT4_EXT_DATA_VALID2) |
| 3310 | split_flag1 |= EXT4_EXT_DATA_VALID1; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3311 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3312 | map->m_lblk + map->m_len, split_flag1, flags1); |
Yongqiang Yang | 9391741 | 2011-05-22 20:49:12 -0400 | [diff] [blame] | 3313 | if (err) |
| 3314 | goto out; |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3315 | } else { |
| 3316 | allocated = ee_len - (map->m_lblk - ee_block); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3317 | } |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3318 | /* |
| 3319 | * Update path is required because previous ext4_split_extent_at() may |
| 3320 | * result in split of original leaf or extent zeroout. |
| 3321 | */ |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 3322 | path = ext4_find_extent(inode, map->m_lblk, ppath, flags); |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3323 | if (IS_ERR(path)) |
| 3324 | return PTR_ERR(path); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3325 | depth = ext_depth(inode); |
| 3326 | ex = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3327 | if (!ex) { |
| 3328 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3329 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3330 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 3331 | } |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3332 | unwritten = ext4_ext_is_unwritten(ex); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3333 | split_flag1 = 0; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3334 | |
| 3335 | if (map->m_lblk >= ee_block) { |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3336 | split_flag1 = split_flag & EXT4_EXT_DATA_VALID2; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3337 | if (unwritten) { |
| 3338 | split_flag1 |= EXT4_EXT_MARK_UNWRIT1; |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3339 | split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3340 | EXT4_EXT_MARK_UNWRIT2); |
Dmitry Monakhov | 357b66f | 2013-03-04 00:34:34 -0500 | [diff] [blame] | 3341 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3342 | err = ext4_split_extent_at(handle, inode, ppath, |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3343 | map->m_lblk, split_flag1, flags); |
| 3344 | if (err) |
| 3345 | goto out; |
| 3346 | } |
| 3347 | |
| 3348 | ext4_ext_show_leaf(inode, path); |
| 3349 | out: |
Zheng Liu | 3a22567 | 2013-03-10 21:20:23 -0400 | [diff] [blame] | 3350 | return err ? err : allocated; |
Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 3351 | } |
| 3352 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3353 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3354 | * 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] | 3355 | * to an unwritten extent. It may result in splitting the unwritten |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3356 | * extent into multiple extents (up to three - one initialized and two |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3357 | * unwritten). |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3358 | * There are three possibilities: |
| 3359 | * a> There is no split required: Entire extent should be initialized |
| 3360 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3361 | * 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] | 3362 | * |
| 3363 | * Pre-conditions: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3364 | * - The extent pointed to by 'path' is unwritten. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3365 | * - The extent pointed to by 'path' contains a superset |
| 3366 | * of the logical span [map->m_lblk, map->m_lblk + map->m_len). |
| 3367 | * |
| 3368 | * Post-conditions on success: |
| 3369 | * - the returned value is the number of blocks beyond map->l_lblk |
| 3370 | * that are allocated and initialized. |
| 3371 | * It is guaranteed to be >= map->m_len. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3372 | */ |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3373 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3374 | struct inode *inode, |
| 3375 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3376 | struct ext4_ext_path **ppath, |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3377 | int flags) |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3378 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3379 | struct ext4_ext_path *path = *ppath; |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3380 | struct ext4_sb_info *sbi; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3381 | struct ext4_extent_header *eh; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3382 | struct ext4_map_blocks split_map; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3383 | struct ext4_extent zero_ex1, zero_ex2; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3384 | struct ext4_extent *ex, *abut_ex; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3385 | ext4_lblk_t ee_block, eof_block; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3386 | unsigned int ee_len, depth, map_len = map->m_len; |
| 3387 | int allocated = 0, max_zeroout = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3388 | int err = 0; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3389 | int split_flag = EXT4_EXT_DATA_VALID2; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3390 | |
| 3391 | ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" |
| 3392 | "block %llu, max_blocks %u\n", inode->i_ino, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3393 | (unsigned long long)map->m_lblk, map_len); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3394 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3395 | sbi = EXT4_SB(inode->i_sb); |
Jan Kara | 801674f | 2020-03-31 12:50:16 +0200 | [diff] [blame] | 3396 | eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) |
| 3397 | >> inode->i_sb->s_blocksize_bits; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3398 | if (eof_block < map->m_lblk + map_len) |
| 3399 | eof_block = map->m_lblk + map_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3400 | |
| 3401 | depth = ext_depth(inode); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3402 | eh = path[depth].p_hdr; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3403 | ex = path[depth].p_ext; |
| 3404 | ee_block = le32_to_cpu(ex->ee_block); |
| 3405 | ee_len = ext4_ext_get_actual_len(ex); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3406 | zero_ex1.ee_len = 0; |
| 3407 | zero_ex2.ee_len = 0; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3408 | |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3409 | trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); |
| 3410 | |
| 3411 | /* Pre-conditions */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3412 | BUG_ON(!ext4_ext_is_unwritten(ex)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3413 | BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3414 | |
| 3415 | /* |
| 3416 | * Attempt to transfer newly initialized blocks from the currently |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3417 | * unwritten extent to its neighbor. This is much cheaper |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3418 | * than an insertion followed by a merge as those involve costly |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3419 | * memmove() calls. Transferring to the left is the common case in |
| 3420 | * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE) |
| 3421 | * followed by append writes. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3422 | * |
| 3423 | * Limitations of the current logic: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3424 | * - L1: we do not deal with writes covering the whole extent. |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3425 | * This would require removing the extent if the transfer |
| 3426 | * is possible. |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3427 | * - L2: we only attempt to merge with an extent stored in the |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3428 | * same extent tree node. |
| 3429 | */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3430 | if ((map->m_lblk == ee_block) && |
| 3431 | /* See if we can merge left */ |
| 3432 | (map_len < ee_len) && /*L1*/ |
| 3433 | (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3434 | ext4_lblk_t prev_lblk; |
| 3435 | ext4_fsblk_t prev_pblk, ee_pblk; |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3436 | unsigned int prev_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3437 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3438 | abut_ex = ex - 1; |
| 3439 | prev_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3440 | prev_len = ext4_ext_get_actual_len(abut_ex); |
| 3441 | prev_pblk = ext4_ext_pblock(abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3442 | ee_pblk = ext4_ext_pblock(ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3443 | |
| 3444 | /* |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3445 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3446 | * upon those conditions: |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3447 | * - C1: abut_ex is initialized, |
| 3448 | * - C2: abut_ex is logically abutting ex, |
| 3449 | * - C3: abut_ex is physically abutting ex, |
| 3450 | * - C4: abut_ex can receive the additional blocks without |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3451 | * overflowing the (initialized) length limit. |
| 3452 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3453 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3454 | ((prev_lblk + prev_len) == ee_block) && /*C2*/ |
| 3455 | ((prev_pblk + prev_len) == ee_pblk) && /*C3*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3456 | (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3457 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3458 | if (err) |
| 3459 | goto out; |
| 3460 | |
| 3461 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3462 | map, ex, abut_ex); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3463 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3464 | /* Shift the start of ex by 'map_len' blocks */ |
| 3465 | ex->ee_block = cpu_to_le32(ee_block + map_len); |
| 3466 | ext4_ext_store_pblock(ex, ee_pblk + map_len); |
| 3467 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3468 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3469 | |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3470 | /* Extend abut_ex by 'map_len' blocks */ |
| 3471 | abut_ex->ee_len = cpu_to_le16(prev_len + map_len); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3472 | |
| 3473 | /* Result: number of initialized blocks past m_lblk */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3474 | allocated = map_len; |
| 3475 | } |
| 3476 | } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) && |
| 3477 | (map_len < ee_len) && /*L1*/ |
| 3478 | ex < EXT_LAST_EXTENT(eh)) { /*L2*/ |
| 3479 | /* See if we can merge right */ |
| 3480 | ext4_lblk_t next_lblk; |
| 3481 | ext4_fsblk_t next_pblk, ee_pblk; |
| 3482 | unsigned int next_len; |
| 3483 | |
| 3484 | abut_ex = ex + 1; |
| 3485 | next_lblk = le32_to_cpu(abut_ex->ee_block); |
| 3486 | next_len = ext4_ext_get_actual_len(abut_ex); |
| 3487 | next_pblk = ext4_ext_pblock(abut_ex); |
| 3488 | ee_pblk = ext4_ext_pblock(ex); |
| 3489 | |
| 3490 | /* |
| 3491 | * A transfer of blocks from 'ex' to 'abut_ex' is allowed |
| 3492 | * upon those conditions: |
| 3493 | * - C1: abut_ex is initialized, |
| 3494 | * - C2: abut_ex is logically abutting ex, |
| 3495 | * - C3: abut_ex is physically abutting ex, |
| 3496 | * - C4: abut_ex can receive the additional blocks without |
| 3497 | * overflowing the (initialized) length limit. |
| 3498 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3499 | if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3500 | ((map->m_lblk + map_len) == next_lblk) && /*C2*/ |
| 3501 | ((ee_pblk + ee_len) == next_pblk) && /*C3*/ |
| 3502 | (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ |
| 3503 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3504 | if (err) |
| 3505 | goto out; |
| 3506 | |
| 3507 | trace_ext4_ext_convert_to_initialized_fastpath(inode, |
| 3508 | map, ex, abut_ex); |
| 3509 | |
| 3510 | /* Shift the start of abut_ex by 'map_len' blocks */ |
| 3511 | abut_ex->ee_block = cpu_to_le32(next_lblk - map_len); |
| 3512 | ext4_ext_store_pblock(abut_ex, next_pblk - map_len); |
| 3513 | ex->ee_len = cpu_to_le16(ee_len - map_len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3514 | ext4_ext_mark_unwritten(ex); /* Restore the flag */ |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3515 | |
| 3516 | /* Extend abut_ex by 'map_len' blocks */ |
| 3517 | abut_ex->ee_len = cpu_to_le16(next_len + map_len); |
| 3518 | |
| 3519 | /* Result: number of initialized blocks past m_lblk */ |
| 3520 | allocated = map_len; |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3521 | } |
| 3522 | } |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3523 | if (allocated) { |
| 3524 | /* Mark the block containing both extents as dirty */ |
Harshad Shirwadkar | b60ca33 | 2020-04-26 18:34:38 -0700 | [diff] [blame] | 3525 | err = ext4_ext_dirty(handle, inode, path + depth); |
Lukas Czerner | bc2d9db | 2013-04-03 23:33:27 -0400 | [diff] [blame] | 3526 | |
| 3527 | /* Update path to point to the right extent */ |
| 3528 | path[depth].p_ext = abut_ex; |
| 3529 | goto out; |
| 3530 | } else |
| 3531 | allocated = ee_len - (map->m_lblk - ee_block); |
Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 3532 | |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3533 | WARN_ON(map->m_lblk < ee_block); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3534 | /* |
| 3535 | * It is safe to convert extent to initialized via explicit |
Yongqiang Yang | 9e74056 | 2014-01-06 14:05:23 -0500 | [diff] [blame] | 3536 | * zeroout only if extent is fully inside i_size or new_size. |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3537 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3538 | split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3539 | |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3540 | if (EXT4_EXT_MAY_ZEROOUT & split_flag) |
| 3541 | max_zeroout = sbi->s_extent_max_zeroout_kb >> |
Lukas Czerner | 4f42f80 | 2013-03-12 12:40:04 -0400 | [diff] [blame] | 3542 | (inode->i_sb->s_blocksize_bits - 10); |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3543 | |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3544 | /* |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3545 | * five cases: |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3546 | * 1. split the extent into three extents. |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3547 | * 2. split the extent into two extents, zeroout the head of the first |
| 3548 | * extent. |
| 3549 | * 3. split the extent into two extents, zeroout the tail of the second |
| 3550 | * extent. |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3551 | * 4. split the extent into two extents with out zeroout. |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3552 | * 5. no splitting needed, just possibly zeroout the head and / or the |
| 3553 | * tail of the extent. |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3554 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3555 | split_map.m_lblk = map->m_lblk; |
| 3556 | split_map.m_len = map->m_len; |
| 3557 | |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3558 | if (max_zeroout && (allocated > split_map.m_len)) { |
Zheng Liu | 67a5da5 | 2012-08-17 09:54:17 -0400 | [diff] [blame] | 3559 | if (allocated <= max_zeroout) { |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3560 | /* case 3 or 5 */ |
| 3561 | zero_ex1.ee_block = |
| 3562 | cpu_to_le32(split_map.m_lblk + |
| 3563 | split_map.m_len); |
| 3564 | zero_ex1.ee_len = |
| 3565 | cpu_to_le16(allocated - split_map.m_len); |
| 3566 | ext4_ext_store_pblock(&zero_ex1, |
| 3567 | ext4_ext_pblock(ex) + split_map.m_lblk + |
| 3568 | split_map.m_len - ee_block); |
| 3569 | err = ext4_ext_zeroout(inode, &zero_ex1); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3570 | if (err) |
| 3571 | goto out; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3572 | split_map.m_len = allocated; |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3573 | } |
| 3574 | if (split_map.m_lblk - ee_block + split_map.m_len < |
| 3575 | max_zeroout) { |
| 3576 | /* case 2 or 5 */ |
| 3577 | if (split_map.m_lblk != ee_block) { |
| 3578 | zero_ex2.ee_block = ex->ee_block; |
| 3579 | zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk - |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3580 | ee_block); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3581 | ext4_ext_store_pblock(&zero_ex2, |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3582 | ext4_ext_pblock(ex)); |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3583 | err = ext4_ext_zeroout(inode, &zero_ex2); |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3584 | if (err) |
| 3585 | goto out; |
| 3586 | } |
| 3587 | |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3588 | split_map.m_len += split_map.m_lblk - ee_block; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3589 | split_map.m_lblk = ee_block; |
Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3590 | allocated = map->m_len; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3591 | } |
| 3592 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3593 | |
Jan Kara | ae9e9c6a | 2014-10-30 10:53:17 -0400 | [diff] [blame] | 3594 | err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag, |
| 3595 | flags); |
| 3596 | if (err > 0) |
| 3597 | err = 0; |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3598 | out: |
Zheng Liu | adb2355 | 2013-03-10 21:13:05 -0400 | [diff] [blame] | 3599 | /* If we have gotten a failure, don't zero out status tree */ |
Jan Kara | 4f8caa6 | 2017-05-26 17:40:52 -0400 | [diff] [blame] | 3600 | if (!err) { |
| 3601 | err = ext4_zeroout_es(inode, &zero_ex1); |
| 3602 | if (!err) |
| 3603 | err = ext4_zeroout_es(inode, &zero_ex2); |
| 3604 | } |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3605 | return err ? err : allocated; |
| 3606 | } |
| 3607 | |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3608 | /* |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3609 | * This function is called by ext4_ext_map_blocks() from |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3610 | * ext4_get_blocks_dio_write() when DIO to write |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3611 | * to an unwritten extent. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3612 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3613 | * Writing to an unwritten extent may result in splitting the unwritten |
| 3614 | * extent into multiple initialized/unwritten extents (up to three) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3615 | * There are three possibilities: |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3616 | * a> There is no split required: Entire extent should be unwritten |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3617 | * b> Splits in two extents: Write is happening at either end of the extent |
| 3618 | * c> Splits in three extents: Somone is writing in middle of the extent |
| 3619 | * |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3620 | * This works the same way in the case of initialized -> unwritten conversion. |
| 3621 | * |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3622 | * 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] | 3623 | * the unwritten extent split. To prevent ENOSPC occur at the IO |
| 3624 | * complete, we need to split the unwritten extent before DIO submit |
| 3625 | * the IO. The unwritten extent called at this time will be split |
| 3626 | * into three unwritten extent(at most). After IO complete, the part |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3627 | * being filled will be convert to initialized by the end_io callback function |
| 3628 | * via ext4_convert_unwritten_extents(). |
Mingming | ba230c3 | 2009-11-06 04:01:23 -0500 | [diff] [blame] | 3629 | * |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3630 | * Returns the size of unwritten extent to be written on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3631 | */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3632 | static int ext4_split_convert_extents(handle_t *handle, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3633 | struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3634 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3635 | struct ext4_ext_path **ppath, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3636 | int flags) |
| 3637 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3638 | struct ext4_ext_path *path = *ppath; |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3639 | ext4_lblk_t eof_block; |
| 3640 | ext4_lblk_t ee_block; |
| 3641 | struct ext4_extent *ex; |
| 3642 | unsigned int ee_len; |
| 3643 | int split_flag = 0, depth; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3644 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3645 | ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n", |
| 3646 | __func__, inode->i_ino, |
| 3647 | (unsigned long long)map->m_lblk, map->m_len); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3648 | |
Jan Kara | 801674f | 2020-03-31 12:50:16 +0200 | [diff] [blame] | 3649 | eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) |
| 3650 | >> inode->i_sb->s_blocksize_bits; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3651 | if (eof_block < map->m_lblk + map->m_len) |
| 3652 | eof_block = map->m_lblk + map->m_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3653 | /* |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3654 | * It is safe to convert extent to initialized via explicit |
| 3655 | * zeroout only if extent is fully insde i_size or new_size. |
| 3656 | */ |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3657 | depth = ext_depth(inode); |
| 3658 | ex = path[depth].p_ext; |
| 3659 | ee_block = le32_to_cpu(ex->ee_block); |
| 3660 | ee_len = ext4_ext_get_actual_len(ex); |
Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3661 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3662 | /* Convert to unwritten */ |
| 3663 | if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { |
| 3664 | split_flag |= EXT4_EXT_DATA_VALID1; |
| 3665 | /* Convert to initialized */ |
| 3666 | } else if (flags & EXT4_GET_BLOCKS_CONVERT) { |
| 3667 | split_flag |= ee_block + ee_len <= eof_block ? |
| 3668 | EXT4_EXT_MAY_ZEROOUT : 0; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3669 | split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3670 | } |
Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3671 | flags |= EXT4_GET_BLOCKS_PRE_IO; |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3672 | return ext4_split_extent(handle, inode, ppath, map, split_flag, flags); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3673 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3674 | |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3675 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3676 | struct inode *inode, |
| 3677 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3678 | struct ext4_ext_path **ppath) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3679 | { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3680 | struct ext4_ext_path *path = *ppath; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3681 | struct ext4_extent *ex; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3682 | ext4_lblk_t ee_block; |
| 3683 | unsigned int ee_len; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3684 | int depth; |
| 3685 | int err = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3686 | |
| 3687 | depth = ext_depth(inode); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3688 | ex = path[depth].p_ext; |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3689 | ee_block = le32_to_cpu(ex->ee_block); |
| 3690 | ee_len = ext4_ext_get_actual_len(ex); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3691 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3692 | ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" |
| 3693 | "block %llu, max_blocks %u\n", inode->i_ino, |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3694 | (unsigned long long)ee_block, ee_len); |
| 3695 | |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3696 | /* If extent is larger than requested it is a clear sign that we still |
| 3697 | * have some extent state machine issues left. So extent_split is still |
| 3698 | * required. |
| 3699 | * TODO: Once all related issues will be fixed this situation should be |
| 3700 | * illegal. |
| 3701 | */ |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3702 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Rakesh Pandit | e3d550c | 2019-08-22 22:53:46 -0400 | [diff] [blame] | 3703 | #ifdef CONFIG_EXT4_DEBUG |
| 3704 | ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu," |
Jakub Wilk | 8d2ae1c | 2016-04-27 01:11:21 -0400 | [diff] [blame] | 3705 | " len %u; IO logical block %llu, len %u", |
Dmitry Monakhov | ff95ec2 | 2013-03-04 00:41:05 -0500 | [diff] [blame] | 3706 | inode->i_ino, (unsigned long long)ee_block, ee_len, |
| 3707 | (unsigned long long)map->m_lblk, map->m_len); |
| 3708 | #endif |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3709 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3710 | EXT4_GET_BLOCKS_CONVERT); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3711 | if (err < 0) |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3712 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3713 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3714 | if (IS_ERR(path)) |
| 3715 | return PTR_ERR(path); |
Dmitry Monakhov | dee1f97 | 2012-10-10 01:04:58 -0400 | [diff] [blame] | 3716 | depth = ext_depth(inode); |
| 3717 | ex = path[depth].p_ext; |
| 3718 | } |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3719 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3720 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3721 | if (err) |
| 3722 | goto out; |
| 3723 | /* first mark the extent as initialized */ |
| 3724 | ext4_ext_mark_initialized(ex); |
| 3725 | |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3726 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3727 | * borders are not changed |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3728 | */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3729 | ext4_ext_try_to_merge(handle, inode, path, ex); |
Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3730 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3731 | /* Mark modified extent as dirty */ |
Theodore Ts'o | ecb94f5 | 2012-08-17 09:44:17 -0400 | [diff] [blame] | 3732 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3733 | out: |
| 3734 | ext4_ext_show_leaf(inode, path); |
| 3735 | return err; |
| 3736 | } |
| 3737 | |
| 3738 | static int |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3739 | convert_initialized_extent(handle_t *handle, struct inode *inode, |
| 3740 | struct ext4_map_blocks *map, |
Eric Whitney | 29c6eaf | 2016-02-22 22:58:55 -0500 | [diff] [blame] | 3741 | struct ext4_ext_path **ppath, |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 3742 | unsigned int *allocated) |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3743 | { |
Theodore Ts'o | 4f224b8 | 2014-09-01 14:36:09 -0400 | [diff] [blame] | 3744 | struct ext4_ext_path *path = *ppath; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3745 | struct ext4_extent *ex; |
| 3746 | ext4_lblk_t ee_block; |
| 3747 | unsigned int ee_len; |
| 3748 | int depth; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3749 | int err = 0; |
| 3750 | |
| 3751 | /* |
| 3752 | * Make sure that the extent is no bigger than we support with |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3753 | * unwritten extent |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3754 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3755 | if (map->m_len > EXT_UNWRITTEN_MAX_LEN) |
| 3756 | map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3757 | |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3758 | depth = ext_depth(inode); |
| 3759 | ex = path[depth].p_ext; |
| 3760 | ee_block = le32_to_cpu(ex->ee_block); |
| 3761 | ee_len = ext4_ext_get_actual_len(ex); |
| 3762 | |
| 3763 | ext_debug("%s: inode %lu, logical" |
| 3764 | "block %llu, max_blocks %u\n", __func__, inode->i_ino, |
| 3765 | (unsigned long long)ee_block, ee_len); |
| 3766 | |
| 3767 | if (ee_block != map->m_lblk || ee_len > map->m_len) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3768 | err = ext4_split_convert_extents(handle, inode, map, ppath, |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3769 | EXT4_GET_BLOCKS_CONVERT_UNWRITTEN); |
| 3770 | if (err < 0) |
| 3771 | return err; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 3772 | path = ext4_find_extent(inode, map->m_lblk, ppath, 0); |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3773 | if (IS_ERR(path)) |
| 3774 | return PTR_ERR(path); |
| 3775 | depth = ext_depth(inode); |
| 3776 | ex = path[depth].p_ext; |
| 3777 | if (!ex) { |
| 3778 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
| 3779 | (unsigned long) map->m_lblk); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 3780 | return -EFSCORRUPTED; |
Theodore Ts'o | e8b83d93 | 2014-09-01 14:35:09 -0400 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | err = ext4_ext_get_access(handle, inode, path + depth); |
| 3785 | if (err) |
| 3786 | return err; |
| 3787 | /* first mark the extent as unwritten */ |
| 3788 | ext4_ext_mark_unwritten(ex); |
| 3789 | |
| 3790 | /* note: ext4_ext_correct_indexes() isn't needed here because |
| 3791 | * borders are not changed |
| 3792 | */ |
| 3793 | ext4_ext_try_to_merge(handle, inode, path, ex); |
| 3794 | |
| 3795 | /* Mark modified extent as dirty */ |
| 3796 | err = ext4_ext_dirty(handle, inode, path + path->p_depth); |
| 3797 | if (err) |
| 3798 | return err; |
| 3799 | ext4_ext_show_leaf(inode, path); |
| 3800 | |
| 3801 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 3802 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3803 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 3804 | if (*allocated > map->m_len) |
| 3805 | *allocated = map->m_len; |
| 3806 | map->m_len = *allocated; |
| 3807 | return 0; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | static int |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3811 | ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3812 | struct ext4_map_blocks *map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3813 | struct ext4_ext_path **ppath, int flags, |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3814 | unsigned int allocated, ext4_fsblk_t newblock) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3815 | { |
Ritesh Harjani | 8ec2d31 | 2020-05-10 11:54:53 +0530 | [diff] [blame^] | 3816 | struct ext4_ext_path __maybe_unused *path = *ppath; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3817 | int ret = 0; |
| 3818 | int err = 0; |
| 3819 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3820 | ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical " |
Zheng Liu | 88635ca | 2011-12-28 19:00:25 -0500 | [diff] [blame] | 3821 | "block %llu, max_blocks %u, flags %x, allocated %u\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3822 | inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3823 | flags, allocated); |
| 3824 | ext4_ext_show_leaf(inode, path); |
| 3825 | |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3826 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3827 | * When writing into unwritten space, we should not fail to |
Lukas Czerner | 27dd438 | 2013-04-09 22:11:22 -0400 | [diff] [blame] | 3828 | * allocate metadata blocks for the new extent block if needed. |
| 3829 | */ |
| 3830 | flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL; |
| 3831 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3832 | trace_ext4_ext_handle_unwritten_extents(inode, map, flags, |
Zheng Liu | b564553 | 2012-11-08 14:33:43 -0500 | [diff] [blame] | 3833 | allocated, newblock); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3834 | |
Eric Whitney | 779e265 | 2020-04-30 14:53:19 -0400 | [diff] [blame] | 3835 | /* get_block() before submitting IO, split the extent */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 3836 | if (flags & EXT4_GET_BLOCKS_PRE_IO) { |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3837 | ret = ext4_split_convert_extents(handle, inode, map, ppath, |
| 3838 | flags | EXT4_GET_BLOCKS_CONVERT); |
Eric Whitney | 779e265 | 2020-04-30 14:53:19 -0400 | [diff] [blame] | 3839 | if (ret < 0) { |
| 3840 | err = ret; |
| 3841 | goto out2; |
| 3842 | } |
| 3843 | /* |
| 3844 | * shouldn't get a 0 return when splitting an extent unless |
| 3845 | * m_len is 0 (bug) or extent has been corrupted |
| 3846 | */ |
| 3847 | if (unlikely(ret == 0)) { |
| 3848 | EXT4_ERROR_INODE(inode, |
| 3849 | "unexpected ret == 0, m_len = %u", |
| 3850 | map->m_len); |
| 3851 | err = -EFSCORRUPTED; |
| 3852 | goto out2; |
| 3853 | } |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3854 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3855 | goto out; |
| 3856 | } |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3857 | /* IO end_io complete, convert the filled extent to written */ |
Lukas Czerner | c8b459f | 2014-05-12 12:55:07 -0400 | [diff] [blame] | 3858 | if (flags & EXT4_GET_BLOCKS_CONVERT) { |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3859 | err = ext4_convert_unwritten_extents_endio(handle, inode, map, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3860 | ppath); |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3861 | if (err < 0) |
| 3862 | goto out2; |
| 3863 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 3864 | goto map_out; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3865 | } |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3866 | /* buffered IO cases */ |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3867 | /* |
| 3868 | * repeat fallocate creation request |
| 3869 | * we already have an unwritten extent |
| 3870 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 3871 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3872 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3873 | goto map_out; |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 3874 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3875 | |
| 3876 | /* buffered READ or buffered write_begin() lookup */ |
| 3877 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
| 3878 | /* |
| 3879 | * We have blocks reserved already. We |
| 3880 | * return allocated blocks so that delalloc |
| 3881 | * won't do block reservation for us. But |
| 3882 | * the buffer head will be unmapped so that |
| 3883 | * a read from the block returns 0s. |
| 3884 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3885 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3886 | goto out1; |
| 3887 | } |
| 3888 | |
Eric Whitney | be809e1 | 2020-04-30 14:53:20 -0400 | [diff] [blame] | 3889 | /* |
| 3890 | * Default case when (flags & EXT4_GET_BLOCKS_CREATE) == 1. |
| 3891 | * For buffered writes, at writepage time, etc. Convert a |
| 3892 | * discovered unwritten extent to written. |
| 3893 | */ |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 3894 | ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags); |
Eric Whitney | be809e1 | 2020-04-30 14:53:20 -0400 | [diff] [blame] | 3895 | if (ret < 0) { |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3896 | err = ret; |
| 3897 | goto out2; |
Eric Whitney | 779e265 | 2020-04-30 14:53:19 -0400 | [diff] [blame] | 3898 | } |
Eric Whitney | be809e1 | 2020-04-30 14:53:20 -0400 | [diff] [blame] | 3899 | ext4_update_inode_fsync_trans(handle, inode, 1); |
| 3900 | /* |
| 3901 | * shouldn't get a 0 return when converting an unwritten extent |
| 3902 | * unless m_len is 0 (bug) or extent has been corrupted |
| 3903 | */ |
| 3904 | if (unlikely(ret == 0)) { |
| 3905 | EXT4_ERROR_INODE(inode, "unexpected ret == 0, m_len = %u", |
| 3906 | map->m_len); |
| 3907 | err = -EFSCORRUPTED; |
| 3908 | goto out2; |
| 3909 | } |
| 3910 | |
Eric Whitney | 779e265 | 2020-04-30 14:53:19 -0400 | [diff] [blame] | 3911 | out: |
| 3912 | allocated = ret; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3913 | map->m_flags |= EXT4_MAP_NEW; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3914 | map_out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3915 | map->m_flags |= EXT4_MAP_MAPPED; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3916 | out1: |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3917 | map->m_pblk = newblock; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3918 | if (allocated > map->m_len) |
| 3919 | allocated = map->m_len; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3920 | map->m_len = allocated; |
Eric Whitney | bee6cf0 | 2020-04-30 14:53:18 -0400 | [diff] [blame] | 3921 | ext4_ext_show_leaf(inode, path); |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3922 | out2: |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3923 | return err ? err : allocated; |
| 3924 | } |
Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3925 | |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3926 | /* |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3927 | * get_implied_cluster_alloc - check to see if the requested |
| 3928 | * allocation (in the map structure) overlaps with a cluster already |
| 3929 | * allocated in an extent. |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3930 | * @sb The filesystem superblock structure |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3931 | * @map The requested lblk->pblk mapping |
| 3932 | * @ex The extent structure which might contain an implied |
| 3933 | * cluster allocation |
| 3934 | * |
| 3935 | * This function is called by ext4_ext_map_blocks() after we failed to |
| 3936 | * find blocks that were already in the inode's extent tree. Hence, |
| 3937 | * we know that the beginning of the requested region cannot overlap |
| 3938 | * the extent from the inode's extent tree. There are three cases we |
| 3939 | * want to catch. The first is this case: |
| 3940 | * |
| 3941 | * |--- cluster # N--| |
| 3942 | * |--- extent ---| |---- requested region ---| |
| 3943 | * |==========| |
| 3944 | * |
| 3945 | * The second case that we need to test for is this one: |
| 3946 | * |
| 3947 | * |--------- cluster # N ----------------| |
| 3948 | * |--- requested region --| |------- extent ----| |
| 3949 | * |=======================| |
| 3950 | * |
| 3951 | * The third case is when the requested region lies between two extents |
| 3952 | * within the same cluster: |
| 3953 | * |------------- cluster # N-------------| |
| 3954 | * |----- ex -----| |---- ex_right ----| |
| 3955 | * |------ requested region ------| |
| 3956 | * |================| |
| 3957 | * |
| 3958 | * In each of the above cases, we need to set the map->m_pblk and |
| 3959 | * map->m_len so it corresponds to the return the extent labelled as |
| 3960 | * "|====|" from cluster #N, since it is already in use for data in |
| 3961 | * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to |
| 3962 | * signal to ext4_ext_map_blocks() that map->m_pblk should be treated |
| 3963 | * as a new "allocated" block region. Otherwise, we will return 0 and |
| 3964 | * ext4_ext_map_blocks() will then allocate one or more new clusters |
| 3965 | * by calling ext4_mb_new_blocks(). |
| 3966 | */ |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3967 | static int get_implied_cluster_alloc(struct super_block *sb, |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3968 | struct ext4_map_blocks *map, |
| 3969 | struct ext4_extent *ex, |
| 3970 | struct ext4_ext_path *path) |
| 3971 | { |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3972 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3973 | 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] | 3974 | ext4_lblk_t ex_cluster_start, ex_cluster_end; |
Curt Wohlgemuth | 14d7f3e | 2011-12-18 17:39:02 -0500 | [diff] [blame] | 3975 | ext4_lblk_t rr_cluster_start; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3976 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
| 3977 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
| 3978 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
| 3979 | |
| 3980 | /* The extent passed in that we are trying to match */ |
| 3981 | ex_cluster_start = EXT4_B2C(sbi, ee_block); |
| 3982 | ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); |
| 3983 | |
| 3984 | /* The requested region passed into ext4_map_blocks() */ |
| 3985 | rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3986 | |
| 3987 | if ((rr_cluster_start == ex_cluster_end) || |
| 3988 | (rr_cluster_start == ex_cluster_start)) { |
| 3989 | if (rr_cluster_start == ex_cluster_end) |
| 3990 | ee_start += ee_len - 1; |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 3991 | map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3992 | map->m_len = min(map->m_len, |
| 3993 | (unsigned) sbi->s_cluster_ratio - c_offset); |
| 3994 | /* |
| 3995 | * Check for and handle this case: |
| 3996 | * |
| 3997 | * |--------- cluster # N-------------| |
| 3998 | * |------- extent ----| |
| 3999 | * |--- requested region ---| |
| 4000 | * |===========| |
| 4001 | */ |
| 4002 | |
| 4003 | if (map->m_lblk < ee_block) |
| 4004 | map->m_len = min(map->m_len, ee_block - map->m_lblk); |
| 4005 | |
| 4006 | /* |
| 4007 | * Check for the case where there is already another allocated |
| 4008 | * block to the right of 'ex' but before the end of the cluster. |
| 4009 | * |
| 4010 | * |------------- cluster # N-------------| |
| 4011 | * |----- ex -----| |---- ex_right ----| |
| 4012 | * |------ requested region ------| |
| 4013 | * |================| |
| 4014 | */ |
| 4015 | if (map->m_lblk > ee_block) { |
| 4016 | ext4_lblk_t next = ext4_ext_next_allocated_block(path); |
| 4017 | map->m_len = min(map->m_len, next - map->m_lblk); |
| 4018 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4019 | |
| 4020 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4021 | return 1; |
| 4022 | } |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4023 | |
| 4024 | trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4025 | return 0; |
| 4026 | } |
| 4027 | |
| 4028 | |
| 4029 | /* |
Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 4030 | * Block allocation/map/preallocation routine for extents based files |
| 4031 | * |
| 4032 | * |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4033 | * Need to be called with |
Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4034 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
| 4035 | * (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] | 4036 | * |
| 4037 | * return > 0, number of of blocks already mapped/allocated |
| 4038 | * if create == 0 and these are pre-allocated blocks |
| 4039 | * buffer head is unmapped |
| 4040 | * otherwise blocks are mapped |
| 4041 | * |
| 4042 | * return = 0, if plain look up failed (blocks have not been allocated) |
| 4043 | * buffer head is unmapped |
| 4044 | * |
| 4045 | * return < 0, error case. |
Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4046 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4047 | int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, |
| 4048 | struct ext4_map_blocks *map, int flags) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4049 | { |
| 4050 | struct ext4_ext_path *path = NULL; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4051 | struct ext4_extent newex, *ex, *ex2; |
| 4052 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4053 | ext4_fsblk_t newblock = 0; |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4054 | int err = 0, depth, ret; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4055 | unsigned int allocated = 0, offset = 0; |
Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4056 | unsigned int allocated_clusters = 0; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4057 | struct ext4_allocation_request ar; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4058 | ext4_lblk_t cluster_offset; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4059 | |
Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 4060 | ext_debug("blocks %u/%u requested for inode %lu\n", |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4061 | map->m_lblk, map->m_len, inode->i_ino); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4062 | 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] | 4063 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4064 | /* find extent for this block */ |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4065 | path = ext4_find_extent(inode, map->m_lblk, NULL, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4066 | if (IS_ERR(path)) { |
| 4067 | err = PTR_ERR(path); |
| 4068 | path = NULL; |
| 4069 | goto out2; |
| 4070 | } |
| 4071 | |
| 4072 | depth = ext_depth(inode); |
| 4073 | |
| 4074 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4075 | * consistent leaf must not be empty; |
| 4076 | * this situation is possible, though, _during_ tree modification; |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 4077 | * this is why assert can't be put in ext4_find_extent() |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4078 | */ |
Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 4079 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 4080 | EXT4_ERROR_INODE(inode, "bad extent address " |
Theodore Ts'o | f70f362 | 2010-05-16 23:00:00 -0400 | [diff] [blame] | 4081 | "lblock: %lu, depth: %d pblock %lld", |
| 4082 | (unsigned long) map->m_lblk, depth, |
| 4083 | path[depth].p_block); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4084 | err = -EFSCORRUPTED; |
Surbhi Palande | 034fb4c | 2009-12-14 09:53:52 -0500 | [diff] [blame] | 4085 | goto out2; |
| 4086 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4087 | |
Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 4088 | ex = path[depth].p_ext; |
| 4089 | if (ex) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4090 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4091 | ext4_fsblk_t ee_start = ext4_ext_pblock(ex); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4092 | unsigned short ee_len; |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4093 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4094 | |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4095 | /* |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4096 | * unwritten extents are treated as holes, except that |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4097 | * we split out initialized portions during a write. |
Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 4098 | */ |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4099 | ee_len = ext4_ext_get_actual_len(ex); |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4100 | |
| 4101 | trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); |
| 4102 | |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4103 | /* if found extent covers block, simply return it */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4104 | if (in_range(map->m_lblk, ee_block, ee_len)) { |
| 4105 | newblock = map->m_lblk - ee_block + ee_start; |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4106 | /* number of remaining blocks in the extent */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4107 | allocated = ee_len - (map->m_lblk - ee_block); |
| 4108 | ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, |
| 4109 | ee_block, ee_len, newblock); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4110 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4111 | /* |
| 4112 | * If the extent is initialized check whether the |
| 4113 | * caller wants to convert it to unwritten. |
| 4114 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4115 | if ((!ext4_ext_is_unwritten(ex)) && |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4116 | (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4117 | err = convert_initialized_extent(handle, |
| 4118 | inode, map, &path, &allocated); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4119 | goto out2; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4120 | } else if (!ext4_ext_is_unwritten(ex)) { |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4121 | goto out; |
Eric Whitney | f064a9d | 2020-02-18 15:26:56 -0500 | [diff] [blame] | 4122 | } |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4123 | |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4124 | ret = ext4_ext_handle_unwritten_extents( |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 4125 | handle, inode, map, &path, flags, |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4126 | allocated, newblock); |
Eric Whitney | ce37c42 | 2014-02-19 18:52:39 -0500 | [diff] [blame] | 4127 | if (ret < 0) |
| 4128 | err = ret; |
| 4129 | else |
| 4130 | allocated = ret; |
Eric Whitney | 31cf0f2 | 2014-03-13 23:14:46 -0400 | [diff] [blame] | 4131 | goto out2; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4136 | * requested block isn't allocated yet; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4137 | * we couldn't try to create block if create flag is zero |
| 4138 | */ |
Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 4139 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4140 | ext4_lblk_t hole_start, hole_len; |
| 4141 | |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4142 | hole_start = map->m_lblk; |
| 4143 | hole_len = ext4_ext_determine_hole(inode, path, &hole_start); |
Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4144 | /* |
| 4145 | * put just found gap into cache to speed up |
| 4146 | * subsequent requests |
| 4147 | */ |
Jan Kara | 140a525 | 2016-03-09 22:46:57 -0500 | [diff] [blame] | 4148 | ext4_ext_put_gap_in_cache(inode, hole_start, hole_len); |
Jan Kara | facab4d | 2016-03-09 22:54:00 -0500 | [diff] [blame] | 4149 | |
| 4150 | /* Update hole_len to reflect hole size after map->m_lblk */ |
| 4151 | if (hole_start != map->m_lblk) |
| 4152 | hole_len -= map->m_lblk - hole_start; |
| 4153 | map->m_pblk = 0; |
| 4154 | map->m_len = min_t(unsigned int, map->m_len, hole_len); |
| 4155 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4156 | goto out2; |
| 4157 | } |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4158 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4159 | /* |
Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4160 | * Okay, we need to do block allocation. |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 4161 | */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4162 | newex.ee_block = cpu_to_le32(map->m_lblk); |
Eric Whitney | d0abafa | 2014-01-06 14:00:23 -0500 | [diff] [blame] | 4163 | cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4164 | |
| 4165 | /* |
| 4166 | * 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] | 4167 | * by ext4_find_extent() implies a cluster we can use. |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4168 | */ |
| 4169 | if (cluster_offset && ex && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4170 | get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4171 | ar.len = allocated = map->m_len; |
| 4172 | newblock = map->m_pblk; |
| 4173 | goto got_allocated_blocks; |
| 4174 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4175 | |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4176 | /* find neighbour allocated blocks */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4177 | ar.lleft = map->m_lblk; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4178 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); |
| 4179 | if (err) |
| 4180 | goto out2; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4181 | ar.lright = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4182 | ex2 = NULL; |
| 4183 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4184 | if (err) |
| 4185 | goto out2; |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4186 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4187 | /* Check if the extent after searching to the right implies a |
| 4188 | * cluster we can use. */ |
| 4189 | if ((sbi->s_cluster_ratio > 1) && ex2 && |
Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 4190 | get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4191 | ar.len = allocated = map->m_len; |
| 4192 | newblock = map->m_pblk; |
| 4193 | goto got_allocated_blocks; |
| 4194 | } |
| 4195 | |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4196 | /* |
| 4197 | * See if request is beyond maximum number of blocks we can have in |
| 4198 | * a single extent. For an initialized extent this limit is |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4199 | * EXT_INIT_MAX_LEN and for an unwritten extent this limit is |
| 4200 | * EXT_UNWRITTEN_MAX_LEN. |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4201 | */ |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4202 | if (map->m_len > EXT_INIT_MAX_LEN && |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4203 | !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4204 | map->m_len = EXT_INIT_MAX_LEN; |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4205 | else if (map->m_len > EXT_UNWRITTEN_MAX_LEN && |
| 4206 | (flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) |
| 4207 | map->m_len = EXT_UNWRITTEN_MAX_LEN; |
Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 4208 | |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4209 | /* 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] | 4210 | newex.ee_len = cpu_to_le16(map->m_len); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4211 | err = ext4_ext_check_overlap(sbi, inode, &newex, path); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4212 | if (err) |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4213 | allocated = ext4_ext_get_actual_len(&newex); |
Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 4214 | else |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4215 | allocated = map->m_len; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4216 | |
| 4217 | /* allocate new block */ |
| 4218 | ar.inode = inode; |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4219 | ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); |
| 4220 | ar.logical = map->m_lblk; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4221 | /* |
| 4222 | * We calculate the offset from the beginning of the cluster |
| 4223 | * for the logical block number, since when we allocate a |
| 4224 | * physical cluster, the physical block should start at the |
| 4225 | * same offset from the beginning of the cluster. This is |
| 4226 | * needed so that future calls to get_implied_cluster_alloc() |
| 4227 | * work correctly. |
| 4228 | */ |
Theodore Ts'o | f5a44db | 2013-12-20 09:29:35 -0500 | [diff] [blame] | 4229 | offset = EXT4_LBLK_COFF(sbi, map->m_lblk); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4230 | ar.len = EXT4_NUM_B2C(sbi, offset+allocated); |
| 4231 | ar.goal -= offset; |
| 4232 | ar.logical -= offset; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4233 | if (S_ISREG(inode->i_mode)) |
| 4234 | ar.flags = EXT4_MB_HINT_DATA; |
| 4235 | else |
| 4236 | /* disable in-core preallocation for non-regular files */ |
| 4237 | ar.flags = 0; |
Vivek Haldar | 556b27a | 2011-05-25 07:41:54 -0400 | [diff] [blame] | 4238 | if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) |
| 4239 | ar.flags |= EXT4_MB_HINT_NOPREALLOC; |
Theodore Ts'o | e3cf5d5 | 2014-09-04 18:07:25 -0400 | [diff] [blame] | 4240 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 4241 | ar.flags |= EXT4_MB_DELALLOC_RESERVED; |
Theodore Ts'o | c5e298a | 2015-06-21 01:25:29 -0400 | [diff] [blame] | 4242 | if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) |
| 4243 | ar.flags |= EXT4_MB_USE_RESERVED; |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4244 | newblock = ext4_mb_new_blocks(handle, &ar, &err); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4245 | if (!newblock) |
| 4246 | goto out2; |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4247 | allocated_clusters = ar.len; |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4248 | ar.len = EXT4_C2B(sbi, ar.len) - offset; |
Ritesh Harjani | ec8c60b | 2020-05-10 11:54:52 +0530 | [diff] [blame] | 4249 | ext_debug("allocate new block: goal %llu, found %llu/%u, requested %u\n", |
| 4250 | ar.goal, newblock, ar.len, allocated); |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4251 | if (ar.len > allocated) |
| 4252 | ar.len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4253 | |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4254 | got_allocated_blocks: |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4255 | /* try to insert new extent into found leaf and return */ |
Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4256 | ext4_ext_store_pblock(&newex, newblock + offset); |
Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4257 | newex.ee_len = cpu_to_le16(ar.len); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4258 | /* Mark unwritten */ |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4259 | if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4260 | ext4_ext_mark_unwritten(&newex); |
Zheng Liu | a25a4e1 | 2013-02-18 00:28:04 -0500 | [diff] [blame] | 4261 | map->m_flags |= EXT4_MAP_UNWRITTEN; |
Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4262 | } |
Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 4263 | |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 4264 | err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags); |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4265 | if (err) { |
| 4266 | if (allocated_clusters) { |
| 4267 | int fb_flags = 0; |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 4268 | |
Eric Whitney | 3499046 | 2020-03-11 16:50:33 -0400 | [diff] [blame] | 4269 | /* |
| 4270 | * free data blocks we just allocated. |
| 4271 | * not a good idea to call discard here directly, |
| 4272 | * but otherwise we'd need to call it every free(). |
| 4273 | */ |
| 4274 | ext4_discard_preallocations(inode); |
| 4275 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
| 4276 | fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE; |
| 4277 | ext4_free_blocks(handle, inode, NULL, newblock, |
| 4278 | EXT4_C2B(sbi, allocated_clusters), |
| 4279 | fb_flags); |
| 4280 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4281 | goto out2; |
Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4282 | } |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4283 | |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4284 | /* previous routine could use block we allocated */ |
Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4285 | newblock = ext4_ext_pblock(&newex); |
Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4286 | allocated = ext4_ext_get_actual_len(&newex); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4287 | if (allocated > map->m_len) |
| 4288 | allocated = map->m_len; |
| 4289 | map->m_flags |= EXT4_MAP_NEW; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4290 | |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4291 | /* |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4292 | * Reduce the reserved cluster count to reflect successful deferred |
| 4293 | * allocation of delayed allocated clusters or direct allocation of |
| 4294 | * clusters discovered to be delayed allocated. Once allocated, a |
| 4295 | * cluster is not included in the reserved count. |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4296 | */ |
Eric Whitney | 2971148 | 2020-03-11 16:51:25 -0400 | [diff] [blame] | 4297 | if (test_opt(inode->i_sb, DELALLOC) && allocated_clusters) { |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4298 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4299 | /* |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4300 | * When allocating delayed allocated clusters, simply |
| 4301 | * reduce the reserved cluster count and claim quota |
Lukas Czerner | 232ec87 | 2013-03-10 22:46:30 -0400 | [diff] [blame] | 4302 | */ |
| 4303 | ext4_da_update_reserve_space(inode, allocated_clusters, |
| 4304 | 1); |
Eric Whitney | b6bf917 | 2018-10-01 14:24:08 -0400 | [diff] [blame] | 4305 | } else { |
| 4306 | ext4_lblk_t lblk, len; |
| 4307 | unsigned int n; |
| 4308 | |
| 4309 | /* |
| 4310 | * When allocating non-delayed allocated clusters |
| 4311 | * (from fallocate, filemap, DIO, or clusters |
| 4312 | * allocated when delalloc has been disabled by |
| 4313 | * ext4_nonda_switch), reduce the reserved cluster |
| 4314 | * count by the number of allocated clusters that |
| 4315 | * have previously been delayed allocated. Quota |
| 4316 | * has been claimed by ext4_mb_new_blocks() above, |
| 4317 | * so release the quota reservations made for any |
| 4318 | * previously delayed allocated clusters. |
| 4319 | */ |
| 4320 | lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk); |
| 4321 | len = allocated_clusters << sbi->s_cluster_bits; |
| 4322 | n = ext4_es_delayed_clu(inode, lblk, len); |
| 4323 | if (n > 0) |
| 4324 | ext4_da_update_reserve_space(inode, (int) n, 0); |
Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4325 | } |
| 4326 | } |
Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4327 | |
| 4328 | /* |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4329 | * Cache the extent and update transaction to commit on fdatasync only |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4330 | * when it is _not_ an unwritten extent. |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4331 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4332 | if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0) |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4333 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Zheng Liu | 69eb33d | 2013-02-18 00:31:07 -0500 | [diff] [blame] | 4334 | else |
Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4335 | ext4_update_inode_fsync_trans(handle, inode, 0); |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4336 | out: |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4337 | if (allocated > map->m_len) |
| 4338 | allocated = map->m_len; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4339 | ext4_ext_show_leaf(inode, path); |
Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4340 | map->m_flags |= EXT4_MAP_MAPPED; |
| 4341 | map->m_pblk = newblock; |
| 4342 | map->m_len = allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4343 | out2: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 4344 | ext4_ext_drop_refs(path); |
| 4345 | kfree(path); |
Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 4346 | |
Theodore Ts'o | 63b9996 | 2013-07-16 10:28:47 -0400 | [diff] [blame] | 4347 | trace_ext4_ext_map_blocks_exit(inode, flags, map, |
| 4348 | err ? err : allocated); |
Lukas Czerner | 7877191 | 2012-03-19 23:05:43 -0400 | [diff] [blame] | 4349 | return err ? err : allocated; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4350 | } |
| 4351 | |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4352 | int ext4_ext_truncate(handle_t *handle, struct inode *inode) |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4353 | { |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4354 | struct super_block *sb = inode->i_sb; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4355 | ext4_lblk_t last_block; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4356 | int err = 0; |
| 4357 | |
| 4358 | /* |
Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4359 | * TODO: optimization is possible here. |
| 4360 | * Probably we need not scan at all, |
| 4361 | * because page truncation is enough. |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4362 | */ |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4363 | |
| 4364 | /* we have to know where to truncate from in crash case */ |
| 4365 | EXT4_I(inode)->i_disksize = inode->i_size; |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4366 | err = ext4_mark_inode_dirty(handle, inode); |
| 4367 | if (err) |
| 4368 | return err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4369 | |
| 4370 | last_block = (inode->i_size + sb->s_blocksize - 1) |
| 4371 | >> EXT4_BLOCK_SIZE_BITS(sb); |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4372 | retry: |
Zheng Liu | 51865fd | 2012-11-08 21:57:32 -0500 | [diff] [blame] | 4373 | err = ext4_es_remove_extent(inode, last_block, |
| 4374 | EXT_MAX_BLOCKS - last_block); |
Theodore Ts'o | 94eec0f | 2013-07-29 12:12:56 -0400 | [diff] [blame] | 4375 | if (err == -ENOMEM) { |
Theodore Ts'o | 8acd5e9 | 2013-07-15 00:09:19 -0400 | [diff] [blame] | 4376 | cond_resched(); |
| 4377 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 4378 | goto retry; |
| 4379 | } |
Theodore Ts'o | d0abb36 | 2016-11-13 22:02:28 -0500 | [diff] [blame] | 4380 | if (err) |
| 4381 | return err; |
Theodore Ts'o | 73c384c0 | 2020-05-07 10:50:28 -0700 | [diff] [blame] | 4382 | retry_remove_space: |
| 4383 | err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); |
| 4384 | if (err == -ENOMEM) { |
| 4385 | cond_resched(); |
| 4386 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 4387 | goto retry_remove_space; |
| 4388 | } |
| 4389 | return err; |
Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4390 | } |
| 4391 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4392 | 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] | 4393 | ext4_lblk_t len, loff_t new_size, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4394 | int flags) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4395 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4396 | struct inode *inode = file_inode(file); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4397 | handle_t *handle; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4398 | int ret = 0; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4399 | int ret2 = 0, ret3 = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4400 | int retries = 0; |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4401 | int depth = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4402 | struct ext4_map_blocks map; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4403 | unsigned int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4404 | loff_t epos; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4405 | |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4406 | BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4407 | map.m_lblk = offset; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4408 | map.m_len = len; |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4409 | /* |
| 4410 | * Don't normalize the request if it can fit in one extent so |
| 4411 | * that it doesn't get unnecessarily split into multiple |
| 4412 | * extents. |
| 4413 | */ |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4414 | if (len <= EXT_UNWRITTEN_MAX_LEN) |
Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4415 | flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; |
Dmitry Monakhov | 60d4616 | 2012-10-05 11:32:02 -0400 | [diff] [blame] | 4416 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4417 | /* |
| 4418 | * credits to insert 1 extent into extent tree |
| 4419 | */ |
| 4420 | credits = ext4_chunk_trans_blocks(inode, len); |
Fabian Frederick | c3fe493 | 2016-09-15 11:52:07 -0400 | [diff] [blame] | 4421 | depth = ext_depth(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4422 | |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4423 | retry: |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4424 | while (ret >= 0 && len) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4425 | /* |
| 4426 | * Recalculate credits when extent tree depth changes. |
| 4427 | */ |
Dan Carpenter | 011c88e | 2016-12-03 16:46:58 -0500 | [diff] [blame] | 4428 | if (depth != ext_depth(inode)) { |
Lukas Czerner | 4134f5c | 2015-06-15 00:20:46 -0400 | [diff] [blame] | 4429 | credits = ext4_chunk_trans_blocks(inode, len); |
| 4430 | depth = ext_depth(inode); |
| 4431 | } |
| 4432 | |
Theodore Ts'o | 9924a92 | 2013-02-08 21:59:22 -0500 | [diff] [blame] | 4433 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4434 | credits); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4435 | if (IS_ERR(handle)) { |
| 4436 | ret = PTR_ERR(handle); |
| 4437 | break; |
| 4438 | } |
Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4439 | ret = ext4_map_blocks(handle, inode, &map, flags); |
Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4440 | if (ret <= 0) { |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4441 | ext4_debug("inode #%lu: block %u: len %u: " |
| 4442 | "ext4_ext_map_blocks returned %d", |
| 4443 | inode->i_ino, map.m_lblk, |
| 4444 | map.m_len, ret); |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4445 | ext4_mark_inode_dirty(handle, inode); |
| 4446 | ret2 = ext4_journal_stop(handle); |
| 4447 | break; |
| 4448 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4449 | map.m_lblk += ret; |
| 4450 | map.m_len = len = len - ret; |
| 4451 | epos = (loff_t)map.m_lblk << inode->i_blkbits; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4452 | inode->i_ctime = current_time(inode); |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4453 | if (new_size) { |
| 4454 | if (epos > new_size) |
| 4455 | epos = new_size; |
| 4456 | if (ext4_update_inode_size(inode, epos) & 0x1) |
| 4457 | inode->i_mtime = inode->i_ctime; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4458 | } |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4459 | ret2 = ext4_mark_inode_dirty(handle, inode); |
Eryu Guan | c894aa9 | 2017-12-03 22:52:51 -0500 | [diff] [blame] | 4460 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4461 | ret3 = ext4_journal_stop(handle); |
| 4462 | ret2 = ret3 ? ret3 : ret2; |
| 4463 | if (unlikely(ret2)) |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4464 | break; |
| 4465 | } |
Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4466 | if (ret == -ENOSPC && |
| 4467 | ext4_should_retry_alloc(inode->i_sb, &retries)) { |
| 4468 | ret = 0; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4469 | goto retry; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4470 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4471 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4472 | return ret > 0 ? ret2 : ret; |
| 4473 | } |
| 4474 | |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 4475 | static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len); |
| 4476 | |
| 4477 | static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len); |
| 4478 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4479 | static long ext4_zero_range(struct file *file, loff_t offset, |
| 4480 | loff_t len, int mode) |
| 4481 | { |
| 4482 | struct inode *inode = file_inode(file); |
| 4483 | handle_t *handle = NULL; |
| 4484 | unsigned int max_blocks; |
| 4485 | loff_t new_size = 0; |
| 4486 | int ret = 0; |
| 4487 | int flags; |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4488 | int credits; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4489 | int partial_begin, partial_end; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4490 | loff_t start, end; |
| 4491 | ext4_lblk_t lblk; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4492 | unsigned int blkbits = inode->i_blkbits; |
| 4493 | |
| 4494 | trace_ext4_zero_range(inode, offset, len, mode); |
| 4495 | |
Namjae Jeon | e1ee60f | 2014-05-27 12:48:55 -0400 | [diff] [blame] | 4496 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 4497 | if (ext4_should_journal_data(inode)) { |
| 4498 | ret = ext4_force_commit(inode->i_sb); |
| 4499 | if (ret) |
| 4500 | return ret; |
| 4501 | } |
| 4502 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4503 | /* |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4504 | * Round up offset. This is not fallocate, we neet to zero out |
| 4505 | * blocks, so convert interior block aligned part of the range to |
| 4506 | * unwritten and possibly manually zero out unaligned parts of the |
| 4507 | * range. |
| 4508 | */ |
| 4509 | start = round_up(offset, 1 << blkbits); |
| 4510 | end = round_down((offset + len), 1 << blkbits); |
| 4511 | |
| 4512 | if (start < offset || end > offset + len) |
| 4513 | return -EINVAL; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4514 | partial_begin = offset & ((1 << blkbits) - 1); |
| 4515 | partial_end = (offset + len) & ((1 << blkbits) - 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4516 | |
| 4517 | lblk = start >> blkbits; |
| 4518 | max_blocks = (end >> blkbits); |
| 4519 | if (max_blocks < lblk) |
| 4520 | max_blocks = 0; |
| 4521 | else |
| 4522 | max_blocks -= lblk; |
| 4523 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4524 | inode_lock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4525 | |
| 4526 | /* |
Christophe JAILLET | 80dd497 | 2020-05-03 22:06:47 +0200 | [diff] [blame] | 4527 | * Indirect files do not support unwritten extents |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4528 | */ |
| 4529 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4530 | ret = -EOPNOTSUPP; |
| 4531 | goto out_mutex; |
| 4532 | } |
| 4533 | |
| 4534 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 4535 | (offset + len > inode->i_size || |
Theodore Ts'o | 51e3ae8 | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4536 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4537 | new_size = offset + len; |
| 4538 | ret = inode_newsize_ok(inode, new_size); |
| 4539 | if (ret) |
| 4540 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4541 | } |
| 4542 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4543 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4544 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4545 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4546 | inode_dio_wait(inode); |
| 4547 | |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4548 | /* Preallocate the range including the unaligned edges */ |
| 4549 | if (partial_begin || partial_end) { |
| 4550 | ret = ext4_alloc_file_blocks(file, |
| 4551 | round_down(offset, 1 << blkbits) >> blkbits, |
| 4552 | (round_up((offset + len), 1 << blkbits) - |
| 4553 | round_down(offset, 1 << blkbits)) >> blkbits, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4554 | new_size, flags); |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4555 | if (ret) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4556 | goto out_mutex; |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4557 | |
| 4558 | } |
| 4559 | |
| 4560 | /* Zero range excluding the unaligned edges */ |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4561 | if (max_blocks > 0) { |
Lukas Czerner | 0f2af21 | 2015-04-03 00:09:13 -0400 | [diff] [blame] | 4562 | flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | |
| 4563 | EXT4_EX_NOCACHE); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4564 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4565 | /* |
| 4566 | * Prevent page faults from reinstantiating pages we have |
| 4567 | * released from page cache. |
| 4568 | */ |
| 4569 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 4570 | |
| 4571 | ret = ext4_break_layouts(inode); |
| 4572 | if (ret) { |
| 4573 | up_write(&EXT4_I(inode)->i_mmap_sem); |
| 4574 | goto out_mutex; |
| 4575 | } |
| 4576 | |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4577 | ret = ext4_update_disksize_before_punch(inode, offset, len); |
| 4578 | if (ret) { |
| 4579 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4580 | goto out_mutex; |
Jan Kara | 0112784 | 2015-12-07 14:34:49 -0500 | [diff] [blame] | 4581 | } |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4582 | /* Now release the pages and zero block aligned part of pages */ |
| 4583 | truncate_pagecache_range(inode, start, end - 1); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4584 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4585 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4586 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4587 | flags); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 4588 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4589 | if (ret) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4590 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4591 | } |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4592 | if (!partial_begin && !partial_end) |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4593 | goto out_mutex; |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4594 | |
Dmitry Monakhov | 69dc953 | 2014-08-27 18:33:49 -0400 | [diff] [blame] | 4595 | /* |
| 4596 | * In worst case we have to writeout two nonadjacent unwritten |
| 4597 | * blocks and update the inode |
| 4598 | */ |
| 4599 | credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1; |
| 4600 | if (ext4_should_journal_data(inode)) |
| 4601 | credits += 2; |
| 4602 | handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4603 | if (IS_ERR(handle)) { |
| 4604 | ret = PTR_ERR(handle); |
| 4605 | ext4_std_error(inode->i_sb, ret); |
Nikolay Borisov | 1d39834 | 2018-03-22 11:52:10 -0400 | [diff] [blame] | 4606 | goto out_mutex; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4607 | } |
| 4608 | |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 4609 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Eric Whitney | 4337ecd | 2020-02-11 16:02:16 -0500 | [diff] [blame] | 4610 | if (new_size) |
Dmitry Monakhov | 4631dbf | 2014-08-23 17:48:28 -0400 | [diff] [blame] | 4611 | ext4_update_inode_size(inode, new_size); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4612 | ret = ext4_mark_inode_dirty(handle, inode); |
| 4613 | if (unlikely(ret)) |
| 4614 | goto out_handle; |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4615 | |
| 4616 | /* Zero out partial block at the edges of the range */ |
| 4617 | ret = ext4_zero_partial_blocks(handle, inode, offset, len); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 4618 | if (ret >= 0) |
| 4619 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4620 | |
| 4621 | if (file->f_flags & O_SYNC) |
| 4622 | ext4_handle_sync(handle); |
| 4623 | |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4624 | out_handle: |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4625 | ext4_journal_stop(handle); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4626 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4627 | inode_unlock(inode); |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4628 | return ret; |
| 4629 | } |
| 4630 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4631 | /* |
| 4632 | * preallocate space for a file. This implements ext4's fallocate file |
| 4633 | * operation, which gets called from sys_fallocate system call. |
| 4634 | * For block-mapped files, posix_fallocate should fall back to the method |
| 4635 | * of writing zeroes to the required new blocks (the same behavior which is |
| 4636 | * expected for file systems which do not support fallocate() system call). |
| 4637 | */ |
| 4638 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 4639 | { |
| 4640 | struct inode *inode = file_inode(file); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4641 | loff_t new_size = 0; |
| 4642 | unsigned int max_blocks; |
| 4643 | int ret = 0; |
| 4644 | int flags; |
| 4645 | ext4_lblk_t lblk; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4646 | unsigned int blkbits = inode->i_blkbits; |
| 4647 | |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4648 | /* |
| 4649 | * Encrypted inodes can't handle collapse range or insert |
| 4650 | * range since we would need to re-encrypt blocks with a |
| 4651 | * different IV or XTS tweak (which are based on the logical |
| 4652 | * block number). |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4653 | */ |
Chandan Rajendra | 592ddec | 2018-12-12 15:20:10 +0530 | [diff] [blame] | 4654 | if (IS_ENCRYPTED(inode) && |
Eric Biggers | 457b1e3 | 2019-12-26 09:42:16 -0600 | [diff] [blame] | 4655 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) |
Michael Halcrow | 2058f83 | 2015-04-12 00:55:10 -0400 | [diff] [blame] | 4656 | return -EOPNOTSUPP; |
| 4657 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4658 | /* Return error if mode is not supported */ |
| 4659 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4660 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | |
| 4661 | FALLOC_FL_INSERT_RANGE)) |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4662 | return -EOPNOTSUPP; |
| 4663 | |
| 4664 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 4665 | return ext4_punch_hole(inode, offset, len); |
| 4666 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4667 | ret = ext4_convert_inline_data(inode); |
| 4668 | if (ret) |
| 4669 | return ret; |
| 4670 | |
Theodore Ts'o | 40c406c | 2014-04-12 22:53:53 -0400 | [diff] [blame] | 4671 | if (mode & FALLOC_FL_COLLAPSE_RANGE) |
| 4672 | return ext4_collapse_range(inode, offset, len); |
| 4673 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4674 | if (mode & FALLOC_FL_INSERT_RANGE) |
| 4675 | return ext4_insert_range(inode, offset, len); |
| 4676 | |
Lukas Czerner | b8a8684 | 2014-03-18 18:05:35 -0400 | [diff] [blame] | 4677 | if (mode & FALLOC_FL_ZERO_RANGE) |
| 4678 | return ext4_zero_range(file, offset, len, mode); |
| 4679 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4680 | trace_ext4_fallocate_enter(inode, offset, len, mode); |
| 4681 | lblk = offset >> blkbits; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4682 | |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4683 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
Lukas Czerner | 556615d | 2014-04-20 23:45:47 -0400 | [diff] [blame] | 4684 | flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4685 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4686 | inode_lock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4687 | |
Davide Italiano | 280227a | 2015-05-02 23:21:15 -0400 | [diff] [blame] | 4688 | /* |
| 4689 | * We only support preallocation for extent-based files only |
| 4690 | */ |
| 4691 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 4692 | ret = -EOPNOTSUPP; |
| 4693 | goto out; |
| 4694 | } |
| 4695 | |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4696 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 4697 | (offset + len > inode->i_size || |
Theodore Ts'o | 51e3ae8 | 2017-10-06 23:09:55 -0400 | [diff] [blame] | 4698 | offset + len > EXT4_I(inode)->i_disksize)) { |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4699 | new_size = offset + len; |
| 4700 | ret = inode_newsize_ok(inode, new_size); |
| 4701 | if (ret) |
| 4702 | goto out; |
| 4703 | } |
| 4704 | |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4705 | /* Wait all existing dio workers, newcomers will block on i_mutex */ |
Jan Kara | 17048e8 | 2015-12-07 14:29:17 -0500 | [diff] [blame] | 4706 | inode_dio_wait(inode); |
| 4707 | |
Tahsin Erdogan | 77a2e84 | 2017-08-05 22:15:45 -0400 | [diff] [blame] | 4708 | ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4709 | if (ret) |
| 4710 | goto out; |
| 4711 | |
Dmitry Monakhov | c174e6d | 2014-08-27 18:40:00 -0400 | [diff] [blame] | 4712 | if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { |
| 4713 | ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, |
| 4714 | EXT4_I(inode)->i_sync_tid); |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4715 | } |
Lukas Czerner | f282ac1 | 2014-03-18 17:44:35 -0400 | [diff] [blame] | 4716 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 4717 | inode_unlock(inode); |
Lukas Czerner | 0e8b687 | 2014-03-18 18:03:51 -0400 | [diff] [blame] | 4718 | trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); |
| 4719 | return ret; |
Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4720 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4721 | |
| 4722 | /* |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4723 | * This function convert a range of blocks to written extents |
| 4724 | * The caller of this function will pass the start offset and the size. |
| 4725 | * all unwritten extents within this range will be converted to |
| 4726 | * written extents. |
| 4727 | * |
| 4728 | * This function is called from the direct IO end io call back |
| 4729 | * function, to convert the fallocated extents after IO is completed. |
Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 4730 | * Returns 0 on success. |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4731 | */ |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4732 | int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode, |
| 4733 | loff_t offset, ssize_t len) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4734 | { |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4735 | unsigned int max_blocks; |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4736 | int ret = 0, ret2 = 0, ret3 = 0; |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4737 | struct ext4_map_blocks map; |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4738 | unsigned int blkbits = inode->i_blkbits; |
| 4739 | unsigned int credits = 0; |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4740 | |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4741 | map.m_lblk = offset >> blkbits; |
Fabian Frederick | 518eaa6 | 2016-09-15 11:55:01 -0400 | [diff] [blame] | 4742 | max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); |
| 4743 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4744 | if (!handle) { |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4745 | /* |
| 4746 | * credits to insert 1 extent into extent tree |
| 4747 | */ |
| 4748 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
| 4749 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4750 | while (ret >= 0 && ret < max_blocks) { |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4751 | map.m_lblk += ret; |
| 4752 | map.m_len = (max_blocks -= ret); |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4753 | if (credits) { |
| 4754 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
| 4755 | credits); |
| 4756 | if (IS_ERR(handle)) { |
| 4757 | ret = PTR_ERR(handle); |
| 4758 | break; |
| 4759 | } |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4760 | } |
Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4761 | ret = ext4_map_blocks(handle, inode, &map, |
Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 4762 | EXT4_GET_BLOCKS_IO_CONVERT_EXT); |
Lukas Czerner | b06acd3 | 2013-01-28 21:21:12 -0500 | [diff] [blame] | 4763 | if (ret <= 0) |
| 4764 | ext4_warning(inode->i_sb, |
| 4765 | "inode #%lu: block %u: len %u: " |
| 4766 | "ext4_ext_map_blocks returned %d", |
| 4767 | inode->i_ino, map.m_lblk, |
| 4768 | map.m_len, ret); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 4769 | ret2 = ext4_mark_inode_dirty(handle, inode); |
| 4770 | if (credits) { |
| 4771 | ret3 = ext4_journal_stop(handle); |
| 4772 | if (unlikely(ret3)) |
| 4773 | ret2 = ret3; |
| 4774 | } |
| 4775 | |
Jan Kara | 6b523df | 2013-06-04 13:21:11 -0400 | [diff] [blame] | 4776 | if (ret <= 0 || ret2) |
Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4777 | break; |
| 4778 | } |
| 4779 | return ret > 0 ? ret2 : ret; |
| 4780 | } |
Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4781 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4782 | int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end) |
| 4783 | { |
| 4784 | int ret, err = 0; |
Ritesh Harjani | c8cc881 | 2019-10-16 13:07:10 +0530 | [diff] [blame] | 4785 | struct ext4_io_end_vec *io_end_vec; |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4786 | |
| 4787 | /* |
| 4788 | * This is somewhat ugly but the idea is clear: When transaction is |
| 4789 | * reserved, everything goes into it. Otherwise we rather start several |
| 4790 | * smaller transactions for conversion of each extent separately. |
| 4791 | */ |
| 4792 | if (handle) { |
| 4793 | handle = ext4_journal_start_reserved(handle, |
| 4794 | EXT4_HT_EXT_CONVERT); |
| 4795 | if (IS_ERR(handle)) |
| 4796 | return PTR_ERR(handle); |
| 4797 | } |
| 4798 | |
Ritesh Harjani | c8cc881 | 2019-10-16 13:07:10 +0530 | [diff] [blame] | 4799 | list_for_each_entry(io_end_vec, &io_end->list_vec, list) { |
| 4800 | ret = ext4_convert_unwritten_extents(handle, io_end->inode, |
| 4801 | io_end_vec->offset, |
| 4802 | io_end_vec->size); |
| 4803 | if (ret) |
| 4804 | break; |
| 4805 | } |
| 4806 | |
Ritesh Harjani | a00713e | 2019-10-16 13:07:08 +0530 | [diff] [blame] | 4807 | if (handle) |
| 4808 | err = ext4_journal_stop(handle); |
| 4809 | |
| 4810 | return ret < 0 ? ret : err; |
| 4811 | } |
| 4812 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4813 | static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4814 | { |
| 4815 | __u64 physical = 0; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4816 | __u64 length = 0; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4817 | int blockbits = inode->i_sb->s_blocksize_bits; |
| 4818 | int error = 0; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4819 | u16 iomap_type; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4820 | |
| 4821 | /* in-inode? */ |
Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4822 | if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4823 | struct ext4_iloc iloc; |
| 4824 | int offset; /* offset of xattr in inode */ |
| 4825 | |
| 4826 | error = ext4_get_inode_loc(inode, &iloc); |
| 4827 | if (error) |
| 4828 | return error; |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 4829 | physical = (__u64)iloc.bh->b_blocknr << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4830 | offset = EXT4_GOOD_OLD_INODE_SIZE + |
| 4831 | EXT4_I(inode)->i_extra_isize; |
| 4832 | physical += offset; |
| 4833 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; |
Curt Wohlgemuth | fd2dd9f | 2010-04-03 17:44:16 -0400 | [diff] [blame] | 4834 | brelse(iloc.bh); |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4835 | iomap_type = IOMAP_INLINE; |
| 4836 | } else if (EXT4_I(inode)->i_file_acl) { /* external block */ |
Jan Kara | a60697f | 2013-05-31 19:38:56 -0400 | [diff] [blame] | 4837 | physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4838 | length = inode->i_sb->s_blocksize; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4839 | iomap_type = IOMAP_MAPPED; |
| 4840 | } else { |
| 4841 | /* no in-inode or external block for xattr, so return -ENOENT */ |
| 4842 | error = -ENOENT; |
| 4843 | goto out; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4844 | } |
| 4845 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4846 | iomap->addr = physical; |
| 4847 | iomap->offset = 0; |
| 4848 | iomap->length = length; |
| 4849 | iomap->type = iomap_type; |
| 4850 | iomap->flags = 0; |
| 4851 | out: |
| 4852 | return error; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4853 | } |
| 4854 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4855 | static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset, |
| 4856 | loff_t length, unsigned flags, |
| 4857 | struct iomap *iomap, struct iomap *srcmap) |
| 4858 | { |
| 4859 | int error; |
| 4860 | |
| 4861 | error = ext4_iomap_xattr_fiemap(inode, iomap); |
| 4862 | if (error == 0 && (offset >= iomap->length)) |
| 4863 | error = -ENOENT; |
| 4864 | return error; |
| 4865 | } |
| 4866 | |
| 4867 | static const struct iomap_ops ext4_iomap_xattr_ops = { |
| 4868 | .iomap_begin = ext4_iomap_xattr_begin, |
| 4869 | }; |
| 4870 | |
| 4871 | static int _ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4872 | __u64 start, __u64 len, bool from_es_cache) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4873 | { |
| 4874 | ext4_lblk_t start_blk; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4875 | u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4876 | int error = 0; |
| 4877 | |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 4878 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
| 4879 | error = ext4_ext_precache(inode); |
| 4880 | if (error) |
| 4881 | return error; |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4882 | fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; |
Theodore Ts'o | 7869a4a | 2013-08-16 22:05:14 -0400 | [diff] [blame] | 4883 | } |
| 4884 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4885 | if (from_es_cache) |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4886 | ext4_fiemap_flags &= FIEMAP_FLAG_XATTR; |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4887 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4888 | if (fiemap_check_flags(fieinfo, ext4_fiemap_flags)) |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4889 | return -EBADR; |
| 4890 | |
| 4891 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4892 | fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; |
| 4893 | error = iomap_fiemap(inode, fieinfo, start, len, |
| 4894 | &ext4_iomap_xattr_ops); |
| 4895 | } else if (!from_es_cache) { |
| 4896 | error = iomap_fiemap(inode, fieinfo, start, len, |
| 4897 | &ext4_iomap_report_ops); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4898 | } else { |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4899 | ext4_lblk_t len_blks; |
| 4900 | __u64 last_blk; |
| 4901 | |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4902 | start_blk = start >> inode->i_sb->s_blocksize_bits; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4903 | last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; |
Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 4904 | if (last_blk >= EXT_MAX_BLOCKS) |
| 4905 | last_blk = EXT_MAX_BLOCKS-1; |
Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4906 | len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4907 | |
| 4908 | /* |
Lukas Czerner | 91dd8c1 | 2012-11-28 12:32:26 -0500 | [diff] [blame] | 4909 | * Walk the extent tree gathering extent information |
| 4910 | * and pushing extents back to the user. |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4911 | */ |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4912 | error = ext4_fill_es_cache_info(inode, start_blk, len_blks, |
| 4913 | fieinfo); |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4914 | } |
Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4915 | return error; |
| 4916 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4917 | |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4918 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4919 | __u64 start, __u64 len) |
| 4920 | { |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4921 | return _ext4_fiemap(inode, fieinfo, start, len, false); |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 4925 | __u64 start, __u64 len) |
| 4926 | { |
| 4927 | if (ext4_has_inline_data(inode)) { |
| 4928 | int has_inline; |
| 4929 | |
| 4930 | down_read(&EXT4_I(inode)->xattr_sem); |
| 4931 | has_inline = ext4_has_inline_data(inode); |
| 4932 | up_read(&EXT4_I(inode)->xattr_sem); |
| 4933 | if (has_inline) |
| 4934 | return 0; |
| 4935 | } |
| 4936 | |
Ritesh Harjani | d3b6f23 | 2020-02-28 14:56:58 +0530 | [diff] [blame] | 4937 | return _ext4_fiemap(inode, fieinfo, start, len, true); |
Theodore Ts'o | bb5835e | 2019-08-11 16:32:41 -0400 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4941 | /* |
| 4942 | * ext4_access_path: |
| 4943 | * Function to access the path buffer for marking it dirty. |
| 4944 | * It also checks if there are sufficient credits left in the journal handle |
| 4945 | * to update path. |
| 4946 | */ |
| 4947 | static int |
| 4948 | ext4_access_path(handle_t *handle, struct inode *inode, |
| 4949 | struct ext4_ext_path *path) |
| 4950 | { |
| 4951 | int credits, err; |
| 4952 | |
| 4953 | if (!ext4_handle_valid(handle)) |
| 4954 | return 0; |
| 4955 | |
| 4956 | /* |
| 4957 | * Check if need to extend journal credits |
| 4958 | * 3 for leaf, sb, and inode plus 2 (bmap and group |
| 4959 | * descriptor) for each block group; assume two block |
| 4960 | * groups |
| 4961 | */ |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 4962 | credits = ext4_writepage_trans_blocks(inode); |
Jan Kara | 83448bd | 2019-11-05 17:44:29 +0100 | [diff] [blame] | 4963 | err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0); |
Jan Kara | a413036 | 2019-11-05 17:44:16 +0100 | [diff] [blame] | 4964 | if (err < 0) |
| 4965 | return err; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4966 | |
| 4967 | err = ext4_ext_get_access(handle, inode, path); |
| 4968 | return err; |
| 4969 | } |
| 4970 | |
| 4971 | /* |
| 4972 | * ext4_ext_shift_path_extents: |
| 4973 | * 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] | 4974 | * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells |
| 4975 | * if it is right shift or left shift operation. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4976 | */ |
| 4977 | static int |
| 4978 | ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift, |
| 4979 | struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 4980 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4981 | { |
| 4982 | int depth, err = 0; |
| 4983 | struct ext4_extent *ex_start, *ex_last; |
zhengbin | 4756ee1 | 2019-12-25 10:45:59 +0800 | [diff] [blame] | 4984 | bool update = false; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4985 | depth = path->p_depth; |
| 4986 | |
| 4987 | while (depth >= 0) { |
| 4988 | if (depth == path->p_depth) { |
| 4989 | ex_start = path[depth].p_ext; |
| 4990 | if (!ex_start) |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 4991 | return -EFSCORRUPTED; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4992 | |
| 4993 | ex_last = EXT_LAST_EXTENT(path[depth].p_hdr); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 4994 | |
| 4995 | err = ext4_access_path(handle, inode, path + depth); |
| 4996 | if (err) |
| 4997 | goto out; |
| 4998 | |
| 4999 | if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) |
zhengbin | 4756ee1 | 2019-12-25 10:45:59 +0800 | [diff] [blame] | 5000 | update = true; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5001 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5002 | while (ex_start <= ex_last) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5003 | if (SHIFT == SHIFT_LEFT) { |
| 5004 | le32_add_cpu(&ex_start->ee_block, |
| 5005 | -shift); |
| 5006 | /* Try to merge to the left. */ |
| 5007 | if ((ex_start > |
| 5008 | EXT_FIRST_EXTENT(path[depth].p_hdr)) |
| 5009 | && |
| 5010 | ext4_ext_try_to_merge_right(inode, |
| 5011 | path, ex_start - 1)) |
| 5012 | ex_last--; |
| 5013 | else |
| 5014 | ex_start++; |
| 5015 | } else { |
| 5016 | le32_add_cpu(&ex_last->ee_block, shift); |
| 5017 | ext4_ext_try_to_merge_right(inode, path, |
| 5018 | ex_last); |
Lukas Czerner | 6dd834e | 2014-04-18 10:55:24 -0400 | [diff] [blame] | 5019 | ex_last--; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5020 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5021 | } |
| 5022 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5023 | if (err) |
| 5024 | goto out; |
| 5025 | |
| 5026 | if (--depth < 0 || !update) |
| 5027 | break; |
| 5028 | } |
| 5029 | |
| 5030 | /* Update index too */ |
| 5031 | err = ext4_access_path(handle, inode, path + depth); |
| 5032 | if (err) |
| 5033 | goto out; |
| 5034 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5035 | if (SHIFT == SHIFT_LEFT) |
| 5036 | le32_add_cpu(&path[depth].p_idx->ei_block, -shift); |
| 5037 | else |
| 5038 | le32_add_cpu(&path[depth].p_idx->ei_block, shift); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5039 | err = ext4_ext_dirty(handle, inode, path + depth); |
| 5040 | if (err) |
| 5041 | goto out; |
| 5042 | |
| 5043 | /* we are done if current index is not a starting index */ |
| 5044 | if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr)) |
| 5045 | break; |
| 5046 | |
| 5047 | depth--; |
| 5048 | } |
| 5049 | |
| 5050 | out: |
| 5051 | return err; |
| 5052 | } |
| 5053 | |
| 5054 | /* |
| 5055 | * ext4_ext_shift_extents: |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5056 | * All the extents which lies in the range from @start to the last allocated |
| 5057 | * block for the @inode are shifted either towards left or right (depending |
| 5058 | * upon @SHIFT) by @shift blocks. |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5059 | * On success, 0 is returned, error otherwise. |
| 5060 | */ |
| 5061 | static int |
| 5062 | ext4_ext_shift_extents(struct inode *inode, handle_t *handle, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5063 | ext4_lblk_t start, ext4_lblk_t shift, |
| 5064 | enum SHIFT_DIRECTION SHIFT) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5065 | { |
| 5066 | struct ext4_ext_path *path; |
| 5067 | int ret = 0, depth; |
| 5068 | struct ext4_extent *extent; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5069 | ext4_lblk_t stop, *iterator, ex_start, ex_end; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5070 | |
| 5071 | /* Let path point to the last extent */ |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5072 | path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, |
| 5073 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5074 | if (IS_ERR(path)) |
| 5075 | return PTR_ERR(path); |
| 5076 | |
| 5077 | depth = path->p_depth; |
| 5078 | extent = path[depth].p_ext; |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5079 | if (!extent) |
| 5080 | goto out; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5081 | |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5082 | stop = le32_to_cpu(extent->ee_block); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5083 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5084 | /* |
Eric Biggers | 349fa7d | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5085 | * For left shifts, make sure the hole on the left is big enough to |
| 5086 | * accommodate the shift. For right shifts, make sure the last extent |
| 5087 | * won't be shifted beyond EXT_MAX_BLOCKS. |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5088 | */ |
| 5089 | if (SHIFT == SHIFT_LEFT) { |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5090 | path = ext4_find_extent(inode, start - 1, &path, |
| 5091 | EXT4_EX_NOCACHE); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5092 | if (IS_ERR(path)) |
| 5093 | return PTR_ERR(path); |
| 5094 | depth = path->p_depth; |
| 5095 | extent = path[depth].p_ext; |
| 5096 | if (extent) { |
| 5097 | ex_start = le32_to_cpu(extent->ee_block); |
| 5098 | ex_end = le32_to_cpu(extent->ee_block) + |
| 5099 | ext4_ext_get_actual_len(extent); |
| 5100 | } else { |
| 5101 | ex_start = 0; |
| 5102 | ex_end = 0; |
| 5103 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5104 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5105 | if ((start == ex_start && shift > ex_start) || |
| 5106 | (shift > start - ex_end)) { |
Eric Biggers | 349fa7d | 2018-04-12 11:48:09 -0400 | [diff] [blame] | 5107 | ret = -EINVAL; |
| 5108 | goto out; |
| 5109 | } |
| 5110 | } else { |
| 5111 | if (shift > EXT_MAX_BLOCKS - |
| 5112 | (stop + ext4_ext_get_actual_len(extent))) { |
| 5113 | ret = -EINVAL; |
| 5114 | goto out; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5115 | } |
Dmitry Monakhov | 8dc79ec | 2014-04-13 15:05:42 -0400 | [diff] [blame] | 5116 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5117 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5118 | /* |
| 5119 | * In case of left shift, iterator points to start and it is increased |
| 5120 | * till we reach stop. In case of right shift, iterator points to stop |
| 5121 | * and it is decreased till we reach start. |
| 5122 | */ |
| 5123 | if (SHIFT == SHIFT_LEFT) |
| 5124 | iterator = &start; |
| 5125 | else |
| 5126 | iterator = &stop; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5127 | |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5128 | /* |
| 5129 | * Its safe to start updating extents. Start and stop are unsigned, so |
| 5130 | * in case of right shift if extent with 0 block is reached, iterator |
| 5131 | * becomes NULL to indicate the end of the loop. |
| 5132 | */ |
| 5133 | while (iterator && start <= stop) { |
Roman Pen | 03e916f | 2017-01-08 21:00:35 -0500 | [diff] [blame] | 5134 | path = ext4_find_extent(inode, *iterator, &path, |
| 5135 | EXT4_EX_NOCACHE); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5136 | if (IS_ERR(path)) |
| 5137 | return PTR_ERR(path); |
| 5138 | depth = path->p_depth; |
| 5139 | extent = path[depth].p_ext; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5140 | if (!extent) { |
| 5141 | EXT4_ERROR_INODE(inode, "unexpected hole at %lu", |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5142 | (unsigned long) *iterator); |
Darrick J. Wong | 6a797d2 | 2015-10-17 16:16:04 -0400 | [diff] [blame] | 5143 | return -EFSCORRUPTED; |
Dmitry Monakhov | a18ed35 | 2014-04-13 15:41:13 -0400 | [diff] [blame] | 5144 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5145 | if (SHIFT == SHIFT_LEFT && *iterator > |
| 5146 | le32_to_cpu(extent->ee_block)) { |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5147 | /* Hole, move to the next extent */ |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5148 | if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) { |
| 5149 | path[depth].p_ext++; |
| 5150 | } else { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5151 | *iterator = ext4_ext_next_allocated_block(path); |
Dmitry Monakhov | f8fb4f4 | 2014-08-30 23:50:56 -0400 | [diff] [blame] | 5152 | continue; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5153 | } |
| 5154 | } |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5155 | |
| 5156 | if (SHIFT == SHIFT_LEFT) { |
| 5157 | extent = EXT_LAST_EXTENT(path[depth].p_hdr); |
| 5158 | *iterator = le32_to_cpu(extent->ee_block) + |
| 5159 | ext4_ext_get_actual_len(extent); |
| 5160 | } else { |
| 5161 | extent = EXT_FIRST_EXTENT(path[depth].p_hdr); |
Roman Pen | 2a9b8cb | 2017-01-08 20:59:35 -0500 | [diff] [blame] | 5162 | if (le32_to_cpu(extent->ee_block) > 0) |
| 5163 | *iterator = le32_to_cpu(extent->ee_block) - 1; |
| 5164 | else |
| 5165 | /* Beginning is reached, end of the loop */ |
| 5166 | iterator = NULL; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5167 | /* Update path extent in case we need to stop */ |
| 5168 | while (le32_to_cpu(extent->ee_block) < start) |
| 5169 | extent++; |
| 5170 | path[depth].p_ext = extent; |
| 5171 | } |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5172 | ret = ext4_ext_shift_path_extents(path, shift, inode, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5173 | handle, SHIFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5174 | if (ret) |
| 5175 | break; |
| 5176 | } |
Theodore Ts'o | ee4bd0d9 | 2014-09-01 14:41:09 -0400 | [diff] [blame] | 5177 | out: |
| 5178 | ext4_ext_drop_refs(path); |
| 5179 | kfree(path); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5180 | return ret; |
| 5181 | } |
| 5182 | |
| 5183 | /* |
| 5184 | * ext4_collapse_range: |
| 5185 | * This implements the fallocate's collapse range functionality for ext4 |
| 5186 | * Returns: 0 and non-zero on error. |
| 5187 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 5188 | 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] | 5189 | { |
| 5190 | struct super_block *sb = inode->i_sb; |
| 5191 | ext4_lblk_t punch_start, punch_stop; |
| 5192 | handle_t *handle; |
| 5193 | unsigned int credits; |
Namjae Jeon | a8680e0 | 2014-04-19 16:37:31 -0400 | [diff] [blame] | 5194 | loff_t new_size, ioffset; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5195 | int ret; |
| 5196 | |
Theodore Ts'o | b9576fc | 2015-05-15 00:24:10 -0400 | [diff] [blame] | 5197 | /* |
| 5198 | * We need to test this early because xfstests assumes that a |
| 5199 | * collapse range of (0, 1) will return EOPNOTSUPP if the file |
| 5200 | * system does not support collapse range. |
| 5201 | */ |
| 5202 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5203 | return -EOPNOTSUPP; |
| 5204 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5205 | /* Collapse range works only on fs cluster size aligned regions. */ |
| 5206 | if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5207 | return -EINVAL; |
| 5208 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5209 | trace_ext4_collapse_range(inode, offset, len); |
| 5210 | |
| 5211 | punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5212 | punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5213 | |
Namjae Jeon | 1ce01c4 | 2014-04-10 22:58:20 -0400 | [diff] [blame] | 5214 | /* Call ext4_force_commit to flush all data in case of data=journal. */ |
| 5215 | if (ext4_should_journal_data(inode)) { |
| 5216 | ret = ext4_force_commit(inode->i_sb); |
| 5217 | if (ret) |
| 5218 | return ret; |
| 5219 | } |
| 5220 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5221 | inode_lock(inode); |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5222 | /* |
| 5223 | * There is no need to overlap collapse range with EOF, in which case |
| 5224 | * it is effectively a truncate operation |
| 5225 | */ |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5226 | if (offset + len >= inode->i_size) { |
Lukas Czerner | 23fffa9 | 2014-04-12 09:56:41 -0400 | [diff] [blame] | 5227 | ret = -EINVAL; |
| 5228 | goto out_mutex; |
| 5229 | } |
| 5230 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5231 | /* Currently just for extent based files */ |
| 5232 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5233 | ret = -EOPNOTSUPP; |
| 5234 | goto out_mutex; |
| 5235 | } |
| 5236 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5237 | /* Wait for existing dio to complete */ |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5238 | inode_dio_wait(inode); |
| 5239 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5240 | /* |
| 5241 | * Prevent page faults from reinstantiating pages we have released from |
| 5242 | * page cache. |
| 5243 | */ |
| 5244 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 5245 | |
| 5246 | ret = ext4_break_layouts(inode); |
| 5247 | if (ret) |
| 5248 | goto out_mmap; |
| 5249 | |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5250 | /* |
| 5251 | * Need to round down offset to be aligned with page size boundary |
| 5252 | * for page size > block size. |
| 5253 | */ |
| 5254 | ioffset = round_down(offset, PAGE_SIZE); |
| 5255 | /* |
| 5256 | * Write tail of the last page before removed range since it will get |
| 5257 | * removed from the page cache below. |
| 5258 | */ |
| 5259 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset); |
| 5260 | if (ret) |
| 5261 | goto out_mmap; |
| 5262 | /* |
| 5263 | * Write data that will be shifted to preserve them when discarding |
| 5264 | * page cache below. We are also protected from pages becoming dirty |
| 5265 | * by i_mmap_sem. |
| 5266 | */ |
| 5267 | ret = filemap_write_and_wait_range(inode->i_mapping, offset + len, |
| 5268 | LLONG_MAX); |
| 5269 | if (ret) |
| 5270 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5271 | truncate_pagecache(inode, ioffset); |
| 5272 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5273 | credits = ext4_writepage_trans_blocks(inode); |
| 5274 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5275 | if (IS_ERR(handle)) { |
| 5276 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5277 | goto out_mmap; |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5278 | } |
| 5279 | |
| 5280 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5281 | ext4_discard_preallocations(inode); |
| 5282 | |
| 5283 | ret = ext4_es_remove_extent(inode, punch_start, |
Lukas Czerner | 2c1d232 | 2014-04-18 10:43:21 -0400 | [diff] [blame] | 5284 | EXT_MAX_BLOCKS - punch_start); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5285 | if (ret) { |
| 5286 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5287 | goto out_stop; |
| 5288 | } |
| 5289 | |
| 5290 | ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1); |
| 5291 | if (ret) { |
| 5292 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5293 | goto out_stop; |
| 5294 | } |
Lukas Czerner | ef24f6c | 2014-04-18 10:50:23 -0400 | [diff] [blame] | 5295 | ext4_discard_preallocations(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5296 | |
| 5297 | ret = ext4_ext_shift_extents(inode, handle, punch_stop, |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5298 | punch_stop - punch_start, SHIFT_LEFT); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5299 | if (ret) { |
| 5300 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5301 | goto out_stop; |
| 5302 | } |
| 5303 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5304 | new_size = inode->i_size - len; |
Lukas Czerner | 9337d5d | 2014-04-18 10:48:25 -0400 | [diff] [blame] | 5305 | i_size_write(inode, new_size); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5306 | EXT4_I(inode)->i_disksize = new_size; |
| 5307 | |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5308 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5309 | if (IS_SYNC(inode)) |
| 5310 | ext4_handle_sync(handle); |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 5311 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Harshad Shirwadkar | 4209ae1 | 2020-04-26 18:34:37 -0700 | [diff] [blame] | 5312 | ret = ext4_mark_inode_dirty(handle, inode); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5313 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5314 | |
| 5315 | out_stop: |
| 5316 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5317 | out_mmap: |
| 5318 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5319 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5320 | inode_unlock(inode); |
Namjae Jeon | 9eb7948 | 2014-02-23 15:18:59 -0500 | [diff] [blame] | 5321 | return ret; |
| 5322 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5323 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5324 | /* |
| 5325 | * ext4_insert_range: |
| 5326 | * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate. |
| 5327 | * The data blocks starting from @offset to the EOF are shifted by @len |
| 5328 | * towards right to create a hole in the @inode. Inode size is increased |
| 5329 | * by len bytes. |
| 5330 | * Returns 0 on success, error otherwise. |
| 5331 | */ |
Eric Biggers | 43f8167 | 2019-12-31 12:04:40 -0600 | [diff] [blame] | 5332 | 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] | 5333 | { |
| 5334 | struct super_block *sb = inode->i_sb; |
| 5335 | handle_t *handle; |
| 5336 | struct ext4_ext_path *path; |
| 5337 | struct ext4_extent *extent; |
| 5338 | ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0; |
| 5339 | unsigned int credits, ee_len; |
| 5340 | int ret = 0, depth, split_flag = 0; |
| 5341 | loff_t ioffset; |
| 5342 | |
| 5343 | /* |
| 5344 | * We need to test this early because xfstests assumes that an |
| 5345 | * insert range of (0, 1) will return EOPNOTSUPP if the file |
| 5346 | * system does not support insert range. |
| 5347 | */ |
| 5348 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
| 5349 | return -EOPNOTSUPP; |
| 5350 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5351 | /* Insert range works only on fs cluster size aligned regions. */ |
| 5352 | if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5353 | return -EINVAL; |
| 5354 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5355 | trace_ext4_insert_range(inode, offset, len); |
| 5356 | |
| 5357 | offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5358 | len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb); |
| 5359 | |
| 5360 | /* Call ext4_force_commit to flush all data in case of data=journal */ |
| 5361 | if (ext4_should_journal_data(inode)) { |
| 5362 | ret = ext4_force_commit(inode->i_sb); |
| 5363 | if (ret) |
| 5364 | return ret; |
| 5365 | } |
| 5366 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5367 | inode_lock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5368 | /* Currently just for extent based files */ |
| 5369 | if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
| 5370 | ret = -EOPNOTSUPP; |
| 5371 | goto out_mutex; |
| 5372 | } |
| 5373 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5374 | /* Check whether the maximum file size would be exceeded */ |
| 5375 | if (len > inode->i_sb->s_maxbytes - inode->i_size) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5376 | ret = -EFBIG; |
| 5377 | goto out_mutex; |
| 5378 | } |
| 5379 | |
Eric Biggers | 9b02e49 | 2019-12-31 12:04:38 -0600 | [diff] [blame] | 5380 | /* Offset must be less than i_size */ |
| 5381 | if (offset >= inode->i_size) { |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5382 | ret = -EINVAL; |
| 5383 | goto out_mutex; |
| 5384 | } |
| 5385 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5386 | /* Wait for existing dio to complete */ |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5387 | inode_dio_wait(inode); |
| 5388 | |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5389 | /* |
| 5390 | * Prevent page faults from reinstantiating pages we have released from |
| 5391 | * page cache. |
| 5392 | */ |
| 5393 | down_write(&EXT4_I(inode)->i_mmap_sem); |
Ross Zwisler | 430657b | 2018-07-29 17:00:22 -0400 | [diff] [blame] | 5394 | |
| 5395 | ret = ext4_break_layouts(inode); |
| 5396 | if (ret) |
| 5397 | goto out_mmap; |
| 5398 | |
Jan Kara | 32ebffd | 2015-12-07 14:31:11 -0500 | [diff] [blame] | 5399 | /* |
| 5400 | * Need to round down to align start offset to page size boundary |
| 5401 | * for page size > block size. |
| 5402 | */ |
| 5403 | ioffset = round_down(offset, PAGE_SIZE); |
| 5404 | /* Write out all dirty pages */ |
| 5405 | ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, |
| 5406 | LLONG_MAX); |
| 5407 | if (ret) |
| 5408 | goto out_mmap; |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5409 | truncate_pagecache(inode, ioffset); |
| 5410 | |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5411 | credits = ext4_writepage_trans_blocks(inode); |
| 5412 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); |
| 5413 | if (IS_ERR(handle)) { |
| 5414 | ret = PTR_ERR(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5415 | goto out_mmap; |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5416 | } |
| 5417 | |
| 5418 | /* Expand file to avoid data loss if there is error while shifting */ |
| 5419 | inode->i_size += len; |
| 5420 | EXT4_I(inode)->i_disksize += len; |
Deepa Dinamani | eeca7ea | 2016-11-14 21:40:10 -0500 | [diff] [blame] | 5421 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5422 | ret = ext4_mark_inode_dirty(handle, inode); |
| 5423 | if (ret) |
| 5424 | goto out_stop; |
| 5425 | |
| 5426 | down_write(&EXT4_I(inode)->i_data_sem); |
| 5427 | ext4_discard_preallocations(inode); |
| 5428 | |
| 5429 | path = ext4_find_extent(inode, offset_lblk, NULL, 0); |
| 5430 | if (IS_ERR(path)) { |
| 5431 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5432 | goto out_stop; |
| 5433 | } |
| 5434 | |
| 5435 | depth = ext_depth(inode); |
| 5436 | extent = path[depth].p_ext; |
| 5437 | if (extent) { |
| 5438 | ee_start_lblk = le32_to_cpu(extent->ee_block); |
| 5439 | ee_len = ext4_ext_get_actual_len(extent); |
| 5440 | |
| 5441 | /* |
| 5442 | * If offset_lblk is not the starting block of extent, split |
| 5443 | * the extent @offset_lblk |
| 5444 | */ |
| 5445 | if ((offset_lblk > ee_start_lblk) && |
| 5446 | (offset_lblk < (ee_start_lblk + ee_len))) { |
| 5447 | if (ext4_ext_is_unwritten(extent)) |
| 5448 | split_flag = EXT4_EXT_MARK_UNWRIT1 | |
| 5449 | EXT4_EXT_MARK_UNWRIT2; |
| 5450 | ret = ext4_split_extent_at(handle, inode, &path, |
| 5451 | offset_lblk, split_flag, |
| 5452 | EXT4_EX_NOCACHE | |
| 5453 | EXT4_GET_BLOCKS_PRE_IO | |
| 5454 | EXT4_GET_BLOCKS_METADATA_NOFAIL); |
| 5455 | } |
| 5456 | |
| 5457 | ext4_ext_drop_refs(path); |
| 5458 | kfree(path); |
| 5459 | if (ret < 0) { |
| 5460 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5461 | goto out_stop; |
| 5462 | } |
Fabian Frederick | edf15aa1 | 2016-09-15 11:39:52 -0400 | [diff] [blame] | 5463 | } else { |
| 5464 | ext4_ext_drop_refs(path); |
| 5465 | kfree(path); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | ret = ext4_es_remove_extent(inode, offset_lblk, |
| 5469 | EXT_MAX_BLOCKS - offset_lblk); |
| 5470 | if (ret) { |
| 5471 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5472 | goto out_stop; |
| 5473 | } |
| 5474 | |
| 5475 | /* |
| 5476 | * if offset_lblk lies in a hole which is at start of file, use |
| 5477 | * ee_start_lblk to shift extents |
| 5478 | */ |
| 5479 | ret = ext4_ext_shift_extents(inode, handle, |
| 5480 | ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk, |
| 5481 | len_lblk, SHIFT_RIGHT); |
| 5482 | |
| 5483 | up_write(&EXT4_I(inode)->i_data_sem); |
| 5484 | if (IS_SYNC(inode)) |
| 5485 | ext4_handle_sync(handle); |
Jan Kara | 67a7d5f | 2017-05-29 13:24:55 -0400 | [diff] [blame] | 5486 | if (ret >= 0) |
| 5487 | ext4_update_inode_fsync_trans(handle, inode, 1); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5488 | |
| 5489 | out_stop: |
| 5490 | ext4_journal_stop(handle); |
Jan Kara | ea3d720 | 2015-12-07 14:28:03 -0500 | [diff] [blame] | 5491 | out_mmap: |
| 5492 | up_write(&EXT4_I(inode)->i_mmap_sem); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5493 | out_mutex: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5494 | inode_unlock(inode); |
Namjae Jeon | 331573f | 2015-06-09 01:55:03 -0400 | [diff] [blame] | 5495 | return ret; |
| 5496 | } |
| 5497 | |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5498 | /** |
Theodore Ts'o | c60990b | 2019-06-19 16:30:03 -0400 | [diff] [blame] | 5499 | * ext4_swap_extents() - Swap extents between two inodes |
| 5500 | * @handle: handle for this transaction |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5501 | * @inode1: First inode |
| 5502 | * @inode2: Second inode |
| 5503 | * @lblk1: Start block for first inode |
| 5504 | * @lblk2: Start block for second inode |
| 5505 | * @count: Number of blocks to swap |
zhenwei.pi | dcae058 | 2018-03-26 01:44:03 -0400 | [diff] [blame] | 5506 | * @unwritten: Mark second inode's extents as unwritten after swap |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5507 | * @erp: Pointer to save error value |
| 5508 | * |
| 5509 | * This helper routine does exactly what is promise "swap extents". All other |
| 5510 | * stuff such as page-cache locking consistency, bh mapping consistency or |
| 5511 | * extent's data copying must be performed by caller. |
| 5512 | * Locking: |
| 5513 | * i_mutex is held for both inodes |
| 5514 | * i_data_sem is locked for write for both inodes |
| 5515 | * Assumptions: |
| 5516 | * All pages from requested range are locked for both inodes |
| 5517 | */ |
| 5518 | int |
| 5519 | ext4_swap_extents(handle_t *handle, struct inode *inode1, |
zhenwei.pi | dcae058 | 2018-03-26 01:44:03 -0400 | [diff] [blame] | 5520 | struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5521 | ext4_lblk_t count, int unwritten, int *erp) |
| 5522 | { |
| 5523 | struct ext4_ext_path *path1 = NULL; |
| 5524 | struct ext4_ext_path *path2 = NULL; |
| 5525 | int replaced_count = 0; |
| 5526 | |
| 5527 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); |
| 5528 | BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 5529 | BUG_ON(!inode_is_locked(inode1)); |
| 5530 | BUG_ON(!inode_is_locked(inode2)); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5531 | |
| 5532 | *erp = ext4_es_remove_extent(inode1, lblk1, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5533 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5534 | return 0; |
| 5535 | *erp = ext4_es_remove_extent(inode2, lblk2, count); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5536 | if (unlikely(*erp)) |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5537 | return 0; |
| 5538 | |
| 5539 | while (count) { |
| 5540 | struct ext4_extent *ex1, *ex2, tmp_ex; |
| 5541 | ext4_lblk_t e1_blk, e2_blk; |
| 5542 | int e1_len, e2_len, len; |
| 5543 | int split = 0; |
| 5544 | |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5545 | path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5546 | if (IS_ERR(path1)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5547 | *erp = PTR_ERR(path1); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5548 | path1 = NULL; |
| 5549 | finish: |
| 5550 | count = 0; |
| 5551 | goto repeat; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5552 | } |
Theodore Ts'o | ed8a1a7 | 2014-09-01 14:43:09 -0400 | [diff] [blame] | 5553 | path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 5554 | if (IS_ERR(path2)) { |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5555 | *erp = PTR_ERR(path2); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5556 | path2 = NULL; |
| 5557 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5558 | } |
| 5559 | ex1 = path1[path1->p_depth].p_ext; |
| 5560 | ex2 = path2[path2->p_depth].p_ext; |
| 5561 | /* Do we have somthing to swap ? */ |
| 5562 | if (unlikely(!ex2 || !ex1)) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5563 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5564 | |
| 5565 | e1_blk = le32_to_cpu(ex1->ee_block); |
| 5566 | e2_blk = le32_to_cpu(ex2->ee_block); |
| 5567 | e1_len = ext4_ext_get_actual_len(ex1); |
| 5568 | e2_len = ext4_ext_get_actual_len(ex2); |
| 5569 | |
| 5570 | /* Hole handling */ |
| 5571 | if (!in_range(lblk1, e1_blk, e1_len) || |
| 5572 | !in_range(lblk2, e2_blk, e2_len)) { |
| 5573 | ext4_lblk_t next1, next2; |
| 5574 | |
| 5575 | /* if hole after extent, then go to next extent */ |
| 5576 | next1 = ext4_ext_next_allocated_block(path1); |
| 5577 | next2 = ext4_ext_next_allocated_block(path2); |
| 5578 | /* If hole before extent, then shift to that extent */ |
| 5579 | if (e1_blk > lblk1) |
| 5580 | next1 = e1_blk; |
| 5581 | if (e2_blk > lblk2) |
Maninder Singh | 4e56201 | 2017-08-06 01:33:07 -0400 | [diff] [blame] | 5582 | next2 = e2_blk; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5583 | /* Do we have something to swap */ |
| 5584 | if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5585 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5586 | /* Move to the rightest boundary */ |
| 5587 | len = next1 - lblk1; |
| 5588 | if (len < next2 - lblk2) |
| 5589 | len = next2 - lblk2; |
| 5590 | if (len > count) |
| 5591 | len = count; |
| 5592 | lblk1 += len; |
| 5593 | lblk2 += len; |
| 5594 | count -= len; |
| 5595 | goto repeat; |
| 5596 | } |
| 5597 | |
| 5598 | /* Prepare left boundary */ |
| 5599 | if (e1_blk < lblk1) { |
| 5600 | split = 1; |
| 5601 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5602 | &path1, lblk1, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5603 | if (unlikely(*erp)) |
| 5604 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5605 | } |
| 5606 | if (e2_blk < lblk2) { |
| 5607 | split = 1; |
| 5608 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5609 | &path2, lblk2, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5610 | if (unlikely(*erp)) |
| 5611 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5612 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5613 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5614 | * path must to be revalidated. */ |
| 5615 | if (split) |
| 5616 | goto repeat; |
| 5617 | |
| 5618 | /* Prepare right boundary */ |
| 5619 | len = count; |
| 5620 | if (len > e1_blk + e1_len - lblk1) |
| 5621 | len = e1_blk + e1_len - lblk1; |
| 5622 | if (len > e2_blk + e2_len - lblk2) |
| 5623 | len = e2_blk + e2_len - lblk2; |
| 5624 | |
| 5625 | if (len != e1_len) { |
| 5626 | split = 1; |
| 5627 | *erp = ext4_force_split_extent_at(handle, inode1, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5628 | &path1, lblk1 + len, 0); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5629 | if (unlikely(*erp)) |
| 5630 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5631 | } |
| 5632 | if (len != e2_len) { |
| 5633 | split = 1; |
| 5634 | *erp = ext4_force_split_extent_at(handle, inode2, |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5635 | &path2, lblk2 + len, 0); |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5636 | if (*erp) |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5637 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5638 | } |
Theodore Ts'o | dfe5080 | 2014-09-01 14:37:09 -0400 | [diff] [blame] | 5639 | /* ext4_split_extent_at() may result in leaf extent split, |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5640 | * path must to be revalidated. */ |
| 5641 | if (split) |
| 5642 | goto repeat; |
| 5643 | |
| 5644 | BUG_ON(e2_len != e1_len); |
| 5645 | *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5646 | if (unlikely(*erp)) |
| 5647 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5648 | *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5649 | if (unlikely(*erp)) |
| 5650 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5651 | |
| 5652 | /* Both extents are fully inside boundaries. Swap it now */ |
| 5653 | tmp_ex = *ex1; |
| 5654 | ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2)); |
| 5655 | ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex)); |
| 5656 | ex1->ee_len = cpu_to_le16(e2_len); |
| 5657 | ex2->ee_len = cpu_to_le16(e1_len); |
| 5658 | if (unwritten) |
| 5659 | ext4_ext_mark_unwritten(ex2); |
| 5660 | if (ext4_ext_is_unwritten(&tmp_ex)) |
| 5661 | ext4_ext_mark_unwritten(ex1); |
| 5662 | |
| 5663 | ext4_ext_try_to_merge(handle, inode2, path2, ex2); |
| 5664 | ext4_ext_try_to_merge(handle, inode1, path1, ex1); |
| 5665 | *erp = ext4_ext_dirty(handle, inode2, path2 + |
| 5666 | path2->p_depth); |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5667 | if (unlikely(*erp)) |
| 5668 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5669 | *erp = ext4_ext_dirty(handle, inode1, path1 + |
| 5670 | path1->p_depth); |
| 5671 | /* |
| 5672 | * Looks scarry ah..? second inode already points to new blocks, |
| 5673 | * and it was successfully dirtied. But luckily error may happen |
| 5674 | * only due to journal error, so full transaction will be |
| 5675 | * aborted anyway. |
| 5676 | */ |
Theodore Ts'o | 19008f6 | 2014-08-31 15:03:14 -0400 | [diff] [blame] | 5677 | if (unlikely(*erp)) |
| 5678 | goto finish; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5679 | lblk1 += len; |
| 5680 | lblk2 += len; |
| 5681 | replaced_count += len; |
| 5682 | count -= len; |
| 5683 | |
| 5684 | repeat: |
Theodore Ts'o | b7ea89a | 2014-09-01 14:39:09 -0400 | [diff] [blame] | 5685 | ext4_ext_drop_refs(path1); |
| 5686 | kfree(path1); |
| 5687 | ext4_ext_drop_refs(path2); |
| 5688 | kfree(path2); |
| 5689 | path1 = path2 = NULL; |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5690 | } |
Dmitry Monakhov | fcf6b1b7 | 2014-08-30 23:52:19 -0400 | [diff] [blame] | 5691 | return replaced_count; |
| 5692 | } |
Eric Whitney | 0b02f4c | 2018-10-01 14:19:37 -0400 | [diff] [blame] | 5693 | |
| 5694 | /* |
| 5695 | * ext4_clu_mapped - determine whether any block in a logical cluster has |
| 5696 | * been mapped to a physical cluster |
| 5697 | * |
| 5698 | * @inode - file containing the logical cluster |
| 5699 | * @lclu - logical cluster of interest |
| 5700 | * |
| 5701 | * Returns 1 if any block in the logical cluster is mapped, signifying |
| 5702 | * that a physical cluster has been allocated for it. Otherwise, |
| 5703 | * returns 0. Can also return negative error codes. Derived from |
| 5704 | * ext4_ext_map_blocks(). |
| 5705 | */ |
| 5706 | int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu) |
| 5707 | { |
| 5708 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 5709 | struct ext4_ext_path *path; |
| 5710 | int depth, mapped = 0, err = 0; |
| 5711 | struct ext4_extent *extent; |
| 5712 | ext4_lblk_t first_lblk, first_lclu, last_lclu; |
| 5713 | |
| 5714 | /* search for the extent closest to the first block in the cluster */ |
| 5715 | path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0); |
| 5716 | if (IS_ERR(path)) { |
| 5717 | err = PTR_ERR(path); |
| 5718 | path = NULL; |
| 5719 | goto out; |
| 5720 | } |
| 5721 | |
| 5722 | depth = ext_depth(inode); |
| 5723 | |
| 5724 | /* |
| 5725 | * A consistent leaf must not be empty. This situation is possible, |
| 5726 | * though, _during_ tree modification, and it's why an assert can't |
| 5727 | * be put in ext4_find_extent(). |
| 5728 | */ |
| 5729 | if (unlikely(path[depth].p_ext == NULL && depth != 0)) { |
| 5730 | EXT4_ERROR_INODE(inode, |
| 5731 | "bad extent address - lblock: %lu, depth: %d, pblock: %lld", |
| 5732 | (unsigned long) EXT4_C2B(sbi, lclu), |
| 5733 | depth, path[depth].p_block); |
| 5734 | err = -EFSCORRUPTED; |
| 5735 | goto out; |
| 5736 | } |
| 5737 | |
| 5738 | extent = path[depth].p_ext; |
| 5739 | |
| 5740 | /* can't be mapped if the extent tree is empty */ |
| 5741 | if (extent == NULL) |
| 5742 | goto out; |
| 5743 | |
| 5744 | first_lblk = le32_to_cpu(extent->ee_block); |
| 5745 | first_lclu = EXT4_B2C(sbi, first_lblk); |
| 5746 | |
| 5747 | /* |
| 5748 | * Three possible outcomes at this point - found extent spanning |
| 5749 | * the target cluster, to the left of the target cluster, or to the |
| 5750 | * right of the target cluster. The first two cases are handled here. |
| 5751 | * The last case indicates the target cluster is not mapped. |
| 5752 | */ |
| 5753 | if (lclu >= first_lclu) { |
| 5754 | last_lclu = EXT4_B2C(sbi, first_lblk + |
| 5755 | ext4_ext_get_actual_len(extent) - 1); |
| 5756 | if (lclu <= last_lclu) { |
| 5757 | mapped = 1; |
| 5758 | } else { |
| 5759 | first_lblk = ext4_ext_next_allocated_block(path); |
| 5760 | first_lclu = EXT4_B2C(sbi, first_lblk); |
| 5761 | if (lclu == first_lclu) |
| 5762 | mapped = 1; |
| 5763 | } |
| 5764 | } |
| 5765 | |
| 5766 | out: |
| 5767 | ext4_ext_drop_refs(path); |
| 5768 | kfree(path); |
| 5769 | |
| 5770 | return err ? err : mapped; |
| 5771 | } |