blob: b2ba9955fa117c3f98812c1138d8b2f0a047fdef [file] [log] [blame]
Alex Tomasa86c6182006-10-11 01:21:03 -07001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
Adam Buchbinderb8a074632016-03-09 23:49:05 -050018 * You should have received a copy of the GNU General Public License
Alex Tomasa86c6182006-10-11 01:21:03 -070019 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 */
22
23/*
24 * Extents support for EXT4
25 *
26 * TODO:
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
30 */
31
Alex Tomasa86c6182006-10-11 01:21:03 -070032#include <linux/fs.h>
33#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040034#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070035#include <linux/highuid.h>
36#include <linux/pagemap.h>
37#include <linux/quotaops.h>
38#include <linux/string.h>
39#include <linux/slab.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070040#include <asm/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040041#include <linux/fiemap.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040042#include <linux/backing-dev.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4_jbd2.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050044#include "ext4_extents.h"
Tao Maf19d5872012-12-10 14:05:51 -050045#include "xattr.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070046
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040047#include <trace/events/ext4.h>
48
Lukas Czerner5f95d212012-03-19 23:03:19 -040049/*
50 * used by extent splitting.
51 */
52#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
53 due to ENOSPC */
Lukas Czerner556615d2014-04-20 23:45:47 -040054#define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
55#define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
Lukas Czerner5f95d212012-03-19 23:03:19 -040056
Dmitry Monakhovdee1f972012-10-10 01:04:58 -040057#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
58#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
59
Darrick J. Wong7ac59902012-04-29 18:37:10 -040060static __le32 ext4_extent_block_csum(struct inode *inode,
61 struct ext4_extent_header *eh)
62{
63 struct ext4_inode_info *ei = EXT4_I(inode);
64 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
65 __u32 csum;
66
67 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
68 EXT4_EXTENT_TAIL_OFFSET(eh));
69 return cpu_to_le32(csum);
70}
71
72static int ext4_extent_block_csum_verify(struct inode *inode,
73 struct ext4_extent_header *eh)
74{
75 struct ext4_extent_tail *et;
76
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -040077 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040078 return 1;
79
80 et = find_ext4_extent_tail(eh);
81 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
82 return 0;
83 return 1;
84}
85
86static void ext4_extent_block_csum_set(struct inode *inode,
87 struct ext4_extent_header *eh)
88{
89 struct ext4_extent_tail *et;
90
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -040091 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040092 return;
93
94 et = find_ext4_extent_tail(eh);
95 et->et_checksum = ext4_extent_block_csum(inode, eh);
96}
97
Allison Hendersond583fb82011-05-25 07:41:43 -040098static int ext4_split_extent(handle_t *handle,
99 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400100 struct ext4_ext_path **ppath,
Allison Hendersond583fb82011-05-25 07:41:43 -0400101 struct ext4_map_blocks *map,
102 int split_flag,
103 int flags);
104
Lukas Czerner5f95d212012-03-19 23:03:19 -0400105static int ext4_split_extent_at(handle_t *handle,
106 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400107 struct ext4_ext_path **ppath,
Lukas Czerner5f95d212012-03-19 23:03:19 -0400108 ext4_lblk_t split,
109 int split_flag,
110 int flags);
111
Lukas Czerner91dd8c12012-11-28 12:32:26 -0500112static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -0500113 struct extent_status *newes);
Lukas Czerner91dd8c12012-11-28 12:32:26 -0500114
Jan Kara487caee2009-08-17 22:17:20 -0400115static int ext4_ext_truncate_extend_restart(handle_t *handle,
116 struct inode *inode,
117 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -0700118{
119 int err;
120
Frank Mayhar03901312009-01-07 00:06:22 -0500121 if (!ext4_handle_valid(handle))
122 return 0;
Theodore Ts'o7b808192016-04-25 23:13:17 -0400123 if (handle->h_buffer_credits >= needed)
Shen Feng9102e4f2008-07-11 19:27:31 -0400124 return 0;
Theodore Ts'o7b808192016-04-25 23:13:17 -0400125 /*
126 * If we need to extend the journal get a few extra blocks
127 * while we're at it for efficiency's sake.
128 */
129 needed += 3;
130 err = ext4_journal_extend(handle, needed - handle->h_buffer_credits);
Theodore Ts'o0123c932008-08-01 20:57:54 -0400131 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -0400132 return err;
Jan Kara487caee2009-08-17 22:17:20 -0400133 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -0400134 if (err == 0)
135 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -0400136
137 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -0700138}
139
140/*
141 * could return:
142 * - EROFS
143 * - ENOMEM
144 */
145static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
146 struct ext4_ext_path *path)
147{
148 if (path->p_bh) {
149 /* path points to block */
liang xie5d601252014-05-12 22:06:43 -0400150 BUFFER_TRACE(path->p_bh, "get_write_access");
Alex Tomasa86c6182006-10-11 01:21:03 -0700151 return ext4_journal_get_write_access(handle, path->p_bh);
152 }
153 /* path points to leaf/index in inode body */
154 /* we use in-core data, no need to protect them */
155 return 0;
156}
157
158/*
159 * could return:
160 * - EROFS
161 * - ENOMEM
162 * - EIO
163 */
Darrick J. Wong26564972013-04-19 14:04:12 -0400164int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
165 struct inode *inode, struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700166{
167 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400168
169 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700170 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400171 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700172 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400173 err = __ext4_handle_dirty_metadata(where, line, handle,
174 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700175 } else {
176 /* path points to leaf/index in inode body */
177 err = ext4_mark_inode_dirty(handle, inode);
178 }
179 return err;
180}
181
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700182static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700183 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500184 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700185{
Alex Tomasa86c6182006-10-11 01:21:03 -0700186 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400187 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700188 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700189
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500190 /*
191 * Try to predict block placement assuming that we are
192 * filling in a file which will eventually be
193 * non-sparse --- i.e., in the case of libbfd writing
194 * an ELF object sections out-of-order but in a way
195 * the eventually results in a contiguous object or
196 * executable file, or some database extending a table
197 * space file. However, this is actually somewhat
198 * non-ideal if we are writing a sparse file such as
199 * qemu or KVM writing a raw image file that is going
200 * to stay fairly sparse, since it will end up
201 * fragmenting the file system's free space. Maybe we
202 * should have some hueristics or some way to allow
203 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800204 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500205 * common.
206 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800207 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500208 if (ex) {
209 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
210 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
211
212 if (block > ext_block)
213 return ext_pblk + (block - ext_block);
214 else
215 return ext_pblk - (ext_block - block);
216 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700217
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700218 /* it looks like index is empty;
219 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700220 if (path[depth].p_bh)
221 return path[depth].p_bh->b_blocknr;
222 }
223
224 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400225 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700226}
227
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400228/*
229 * Allocation for a meta data block
230 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700231static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400232ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700233 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400234 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700235{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700236 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700237
238 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400239 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
240 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700241 return newblock;
242}
243
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400244static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700245{
246 int size;
247
248 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
249 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100250#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400251 if (!check && size > 6)
252 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700253#endif
254 return size;
255}
256
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400257static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700258{
259 int size;
260
261 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
262 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100263#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400264 if (!check && size > 5)
265 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700266#endif
267 return size;
268}
269
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400270static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700271{
272 int size;
273
274 size = sizeof(EXT4_I(inode)->i_data);
275 size -= sizeof(struct ext4_extent_header);
276 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100277#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400278 if (!check && size > 3)
279 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700280#endif
281 return size;
282}
283
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400284static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700285{
286 int size;
287
288 size = sizeof(EXT4_I(inode)->i_data);
289 size -= sizeof(struct ext4_extent_header);
290 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100291#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400292 if (!check && size > 4)
293 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700294#endif
295 return size;
296}
297
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400298static inline int
299ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400300 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400301 int nofail)
302{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400303 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400304 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
305
Theodore Ts'odfe50802014-09-01 14:37:09 -0400306 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400307 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
308 EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
309 (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
310}
311
Mingming Caod2a17632008-07-14 17:52:37 -0400312/*
313 * Calculate the number of metadata blocks needed
314 * to allocate @blocks
315 * Worse case is one block per extent
316 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500317int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400318{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500319 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400320 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400321
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500322 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
323 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400324
325 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500326 * If the new delayed allocation block is contiguous with the
327 * previous da block, it can share index blocks with the
328 * previous block, so we only need to allocate a new index
329 * block every idxs leaf blocks. At ldxs**2 blocks, we need
330 * an additional index block, and at ldxs**3 blocks, yet
331 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400332 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500333 if (ei->i_da_metadata_calc_len &&
334 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400335 int num = 0;
336
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500337 if ((ei->i_da_metadata_calc_len % idxs) == 0)
338 num++;
339 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
340 num++;
341 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
342 num++;
343 ei->i_da_metadata_calc_len = 0;
344 } else
345 ei->i_da_metadata_calc_len++;
346 ei->i_da_metadata_calc_last_lblock++;
347 return num;
348 }
Mingming Caod2a17632008-07-14 17:52:37 -0400349
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500350 /*
351 * In the worst case we need a new set of index blocks at
352 * every level of the inode's extent tree.
353 */
354 ei->i_da_metadata_calc_len = 1;
355 ei->i_da_metadata_calc_last_lblock = lblock;
356 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400357}
358
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400359static int
360ext4_ext_max_entries(struct inode *inode, int depth)
361{
362 int max;
363
364 if (depth == ext_depth(inode)) {
365 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400366 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400367 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400368 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400369 } else {
370 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400371 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400372 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400373 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400374 }
375
376 return max;
377}
378
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400379static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
380{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400381 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400382 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500383 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400384
Vegard Nossumf70749c2016-06-30 11:53:46 -0400385 /*
386 * We allow neither:
387 * - zero length
388 * - overflow/wrap-around
389 */
390 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400391 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400392 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400393}
394
395static int ext4_valid_extent_idx(struct inode *inode,
396 struct ext4_extent_idx *ext_idx)
397{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400398 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400399
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400400 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400401}
402
403static int ext4_valid_extent_entries(struct inode *inode,
404 struct ext4_extent_header *eh,
405 int depth)
406{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400407 unsigned short entries;
408 if (eh->eh_entries == 0)
409 return 1;
410
411 entries = le16_to_cpu(eh->eh_entries);
412
413 if (depth == 0) {
414 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400415 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500416 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
417 ext4_fsblk_t pblock = 0;
418 ext4_lblk_t lblock = 0;
419 ext4_lblk_t prev = 0;
420 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400421 while (entries) {
422 if (!ext4_valid_extent(inode, ext))
423 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500424
425 /* Check for overlapping extents */
426 lblock = le32_to_cpu(ext->ee_block);
427 len = ext4_ext_get_actual_len(ext);
428 if ((lblock <= prev) && prev) {
429 pblock = ext4_ext_pblock(ext);
430 es->s_last_error_block = cpu_to_le64(pblock);
431 return 0;
432 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400433 ext++;
434 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500435 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400436 }
437 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400438 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400439 while (entries) {
440 if (!ext4_valid_extent_idx(inode, ext_idx))
441 return 0;
442 ext_idx++;
443 entries--;
444 }
445 }
446 return 1;
447}
448
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400449static int __ext4_ext_check(const char *function, unsigned int line,
450 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400451 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400452{
453 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400454 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400455
456 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
457 error_msg = "invalid magic";
458 goto corrupted;
459 }
460 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
461 error_msg = "unexpected eh_depth";
462 goto corrupted;
463 }
464 if (unlikely(eh->eh_max == 0)) {
465 error_msg = "invalid eh_max";
466 goto corrupted;
467 }
468 max = ext4_ext_max_entries(inode, depth);
469 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
470 error_msg = "too large eh_max";
471 goto corrupted;
472 }
473 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
474 error_msg = "invalid eh_entries";
475 goto corrupted;
476 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400477 if (!ext4_valid_extent_entries(inode, eh, depth)) {
478 error_msg = "invalid extent entries";
479 goto corrupted;
480 }
Vegard Nossum7bc94912016-07-15 00:22:07 -0400481 if (unlikely(depth > 32)) {
482 error_msg = "too large eh_depth";
483 goto corrupted;
484 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400485 /* Verify checksum on non-root extent tree nodes */
486 if (ext_depth(inode) != depth &&
487 !ext4_extent_block_csum_verify(inode, eh)) {
488 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400489 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400490 goto corrupted;
491 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400492 return 0;
493
494corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400495 ext4_error_inode(inode, function, line, 0,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400496 "pblk %llu bad header/extent: %s - magic %x, "
497 "entries %u, max %u(%u), depth %u(%u)",
498 (unsigned long long) pblk, error_msg,
499 le16_to_cpu(eh->eh_magic),
500 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
501 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400502 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400503}
504
Theodore Ts'oc3491792013-08-16 21:21:41 -0400505#define ext4_ext_check(inode, eh, depth, pblk) \
506 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400507
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400508int ext4_ext_check_inode(struct inode *inode)
509{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400510 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400511}
512
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400513static struct buffer_head *
514__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400515 struct inode *inode, ext4_fsblk_t pblk, int depth,
516 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400517{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400518 struct buffer_head *bh;
519 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400520
Nikolay Borisovc45653c2015-07-02 01:34:07 -0400521 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400522 if (unlikely(!bh))
523 return ERR_PTR(-ENOMEM);
524
525 if (!bh_uptodate_or_lock(bh)) {
526 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
527 err = bh_submit_read(bh);
528 if (err < 0)
529 goto errout;
530 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400531 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400532 return bh;
533 err = __ext4_ext_check(function, line, inode,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400534 ext_block_hdr(bh), depth, pblk);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400535 if (err)
536 goto errout;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400537 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400538 /*
539 * If this is a leaf block, cache all of its entries
540 */
541 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
542 struct ext4_extent_header *eh = ext_block_hdr(bh);
543 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
544 ext4_lblk_t prev = 0;
545 int i;
546
547 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
548 unsigned int status = EXTENT_STATUS_WRITTEN;
549 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
550 int len = ext4_ext_get_actual_len(ex);
551
552 if (prev && (prev != lblk))
553 ext4_es_cache_extent(inode, prev,
554 lblk - prev, ~0,
555 EXTENT_STATUS_HOLE);
556
Lukas Czerner556615d2014-04-20 23:45:47 -0400557 if (ext4_ext_is_unwritten(ex))
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400558 status = EXTENT_STATUS_UNWRITTEN;
559 ext4_es_cache_extent(inode, lblk, len,
560 ext4_ext_pblock(ex), status);
561 prev = lblk + len;
562 }
563 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400564 return bh;
565errout:
566 put_bh(bh);
567 return ERR_PTR(err);
568
Darrick J. Wongf8489122012-04-29 18:21:10 -0400569}
570
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400571#define read_extent_tree_block(inode, pblk, depth, flags) \
572 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
573 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400574
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400575/*
576 * This function is called to cache a file's extent information in the
577 * extent status tree
578 */
579int ext4_ext_precache(struct inode *inode)
580{
581 struct ext4_inode_info *ei = EXT4_I(inode);
582 struct ext4_ext_path *path = NULL;
583 struct buffer_head *bh;
584 int i = 0, depth, ret = 0;
585
586 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
587 return 0; /* not an extent-mapped inode */
588
589 down_read(&ei->i_data_sem);
590 depth = ext_depth(inode);
591
592 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
593 GFP_NOFS);
594 if (path == NULL) {
595 up_read(&ei->i_data_sem);
596 return -ENOMEM;
597 }
598
599 /* Don't cache anything if there are no external extent blocks */
600 if (depth == 0)
601 goto out;
602 path[0].p_hdr = ext_inode_hdr(inode);
603 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
604 if (ret)
605 goto out;
606 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
607 while (i >= 0) {
608 /*
609 * If this is a leaf block or we've reached the end of
610 * the index block, go up
611 */
612 if ((i == depth) ||
613 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
614 brelse(path[i].p_bh);
615 path[i].p_bh = NULL;
616 i--;
617 continue;
618 }
619 bh = read_extent_tree_block(inode,
620 ext4_idx_pblock(path[i].p_idx++),
621 depth - i - 1,
622 EXT4_EX_FORCE_CACHE);
623 if (IS_ERR(bh)) {
624 ret = PTR_ERR(bh);
625 break;
626 }
627 i++;
628 path[i].p_bh = bh;
629 path[i].p_hdr = ext_block_hdr(bh);
630 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
631 }
632 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
633out:
634 up_read(&ei->i_data_sem);
635 ext4_ext_drop_refs(path);
636 kfree(path);
637 return ret;
638}
639
Alex Tomasa86c6182006-10-11 01:21:03 -0700640#ifdef EXT_DEBUG
641static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
642{
643 int k, l = path->p_depth;
644
645 ext_debug("path:");
646 for (k = 0; k <= l; k++, path++) {
647 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700648 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400649 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700650 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400651 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700652 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400653 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400654 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400655 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700656 } else
657 ext_debug(" []");
658 }
659 ext_debug("\n");
660}
661
662static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
663{
664 int depth = ext_depth(inode);
665 struct ext4_extent_header *eh;
666 struct ext4_extent *ex;
667 int i;
668
669 if (!path)
670 return;
671
672 eh = path[depth].p_hdr;
673 ex = EXT_FIRST_EXTENT(eh);
674
Mingming553f9002009-09-18 13:34:55 -0400675 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
676
Alex Tomasa86c6182006-10-11 01:21:03 -0700677 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400678 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400679 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400680 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700681 }
682 ext_debug("\n");
683}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400684
685static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
686 ext4_fsblk_t newblock, int level)
687{
688 int depth = ext_depth(inode);
689 struct ext4_extent *ex;
690
691 if (depth != level) {
692 struct ext4_extent_idx *idx;
693 idx = path[level].p_idx;
694 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
695 ext_debug("%d: move %d:%llu in new index %llu\n", level,
696 le32_to_cpu(idx->ei_block),
697 ext4_idx_pblock(idx),
698 newblock);
699 idx++;
700 }
701
702 return;
703 }
704
705 ex = path[depth].p_ext;
706 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
707 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
708 le32_to_cpu(ex->ee_block),
709 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400710 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400711 ext4_ext_get_actual_len(ex),
712 newblock);
713 ex++;
714 }
715}
716
Alex Tomasa86c6182006-10-11 01:21:03 -0700717#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400718#define ext4_ext_show_path(inode, path)
719#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400720#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700721#endif
722
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500723void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700724{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400725 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700726
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400727 if (!path)
728 return;
729 depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700730 for (i = 0; i <= depth; i++, path++)
731 if (path->p_bh) {
732 brelse(path->p_bh);
733 path->p_bh = NULL;
734 }
735}
736
737/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700738 * ext4_ext_binsearch_idx:
739 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400740 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700741 */
742static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500743ext4_ext_binsearch_idx(struct inode *inode,
744 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700745{
746 struct ext4_extent_header *eh = path->p_hdr;
747 struct ext4_extent_idx *r, *l, *m;
748
Alex Tomasa86c6182006-10-11 01:21:03 -0700749
Eric Sandeenbba90742008-01-28 23:58:27 -0500750 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700751
752 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400753 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700754 while (l <= r) {
755 m = l + (r - l) / 2;
756 if (block < le32_to_cpu(m->ei_block))
757 r = m - 1;
758 else
759 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400760 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
761 m, le32_to_cpu(m->ei_block),
762 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700763 }
764
765 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400766 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400767 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700768
769#ifdef CHECK_BINSEARCH
770 {
771 struct ext4_extent_idx *chix, *ix;
772 int k;
773
774 chix = ix = EXT_FIRST_INDEX(eh);
775 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
776 if (k != 0 &&
777 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400778 printk(KERN_DEBUG "k=%d, ix=0x%p, "
779 "first=0x%p\n", k,
780 ix, EXT_FIRST_INDEX(eh));
781 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700782 le32_to_cpu(ix->ei_block),
783 le32_to_cpu(ix[-1].ei_block));
784 }
785 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400786 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700787 if (block < le32_to_cpu(ix->ei_block))
788 break;
789 chix = ix;
790 }
791 BUG_ON(chix != path->p_idx);
792 }
793#endif
794
795}
796
797/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700798 * ext4_ext_binsearch:
799 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400800 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700801 */
802static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500803ext4_ext_binsearch(struct inode *inode,
804 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700805{
806 struct ext4_extent_header *eh = path->p_hdr;
807 struct ext4_extent *r, *l, *m;
808
Alex Tomasa86c6182006-10-11 01:21:03 -0700809 if (eh->eh_entries == 0) {
810 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700811 * this leaf is empty:
812 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700813 */
814 return;
815 }
816
Eric Sandeenbba90742008-01-28 23:58:27 -0500817 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700818
819 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400820 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700821
822 while (l <= r) {
823 m = l + (r - l) / 2;
824 if (block < le32_to_cpu(m->ee_block))
825 r = m - 1;
826 else
827 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400828 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
829 m, le32_to_cpu(m->ee_block),
830 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700831 }
832
833 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400834 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400835 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400836 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400837 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400838 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700839
840#ifdef CHECK_BINSEARCH
841 {
842 struct ext4_extent *chex, *ex;
843 int k;
844
845 chex = ex = EXT_FIRST_EXTENT(eh);
846 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
847 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400848 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700849 if (block < le32_to_cpu(ex->ee_block))
850 break;
851 chex = ex;
852 }
853 BUG_ON(chex != path->p_ext);
854 }
855#endif
856
857}
858
859int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
860{
861 struct ext4_extent_header *eh;
862
863 eh = ext_inode_hdr(inode);
864 eh->eh_depth = 0;
865 eh->eh_entries = 0;
866 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400867 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700868 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700869 return 0;
870}
871
872struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400873ext4_find_extent(struct inode *inode, ext4_lblk_t block,
874 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700875{
876 struct ext4_extent_header *eh;
877 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400878 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
879 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500880 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700881
882 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400883 depth = ext_depth(inode);
Theodore Ts'o87dad442018-06-14 12:55:10 -0400884 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
885 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
886 depth);
887 ret = -EFSCORRUPTED;
888 goto err;
889 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700890
Theodore Ts'o10809df82014-09-01 14:40:09 -0400891 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400892 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400893 if (depth > path[0].p_maxdepth) {
894 kfree(path);
895 *orig_path = path = NULL;
896 }
897 }
898 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400899 /* account possible depth increase */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800900 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700901 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400902 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700903 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400904 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700905 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400907 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700908
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400909 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 /* walk through the tree */
911 while (i) {
912 ext_debug("depth %d: num %d, max %d\n",
913 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400914
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400916 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700917 path[ppos].p_depth = i;
918 path[ppos].p_ext = NULL;
919
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400920 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
921 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530922 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400923 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700924 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500925 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400926
Alex Tomasa86c6182006-10-11 01:21:03 -0700927 eh = ext_block_hdr(bh);
928 ppos++;
Alex Tomasa86c6182006-10-11 01:21:03 -0700929 path[ppos].p_bh = bh;
930 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700931 }
932
933 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700934 path[ppos].p_ext = NULL;
935 path[ppos].p_idx = NULL;
936
Alex Tomasa86c6182006-10-11 01:21:03 -0700937 /* find extent */
938 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400939 /* if not an empty leaf */
940 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400941 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700942
943 ext4_ext_show_path(inode, path);
944
945 return path;
946
947err:
948 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400949 kfree(path);
950 if (orig_path)
951 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500952 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700953}
954
955/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700956 * ext4_ext_insert_index:
957 * insert new index [@logical;@ptr] into the block at @curp;
958 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700959 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400960static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
961 struct ext4_ext_path *curp,
962 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700963{
964 struct ext4_extent_idx *ix;
965 int len, err;
966
Avantika Mathur7e028972006-12-06 20:41:33 -0800967 err = ext4_ext_get_access(handle, inode, curp);
968 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700969 return err;
970
Frank Mayhar273df552010-03-02 11:46:09 -0500971 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
972 EXT4_ERROR_INODE(inode,
973 "logical %d == ei_block %d!",
974 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400975 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500976 }
Robin Dongd4620312011-07-17 23:43:42 -0400977
978 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
979 >= le16_to_cpu(curp->p_hdr->eh_max))) {
980 EXT4_ERROR_INODE(inode,
981 "eh_entries %d >= eh_max %d!",
982 le16_to_cpu(curp->p_hdr->eh_entries),
983 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400984 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400985 }
986
Alex Tomasa86c6182006-10-11 01:21:03 -0700987 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
988 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400989 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700990 ix = curp->p_idx + 1;
991 } else {
992 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400993 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700994 ix = curp->p_idx;
995 }
996
Eric Gouriou80e675f2011-10-27 11:52:18 -0400997 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
998 BUG_ON(len < 0);
999 if (len > 0) {
1000 ext_debug("insert new index %d: "
1001 "move %d indices from 0x%p to 0x%p\n",
1002 logical, len, ix, ix + 1);
1003 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
1004 }
1005
Tao Maf472e022011-10-17 10:13:46 -04001006 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
1007 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001008 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -04001009 }
1010
Alex Tomasa86c6182006-10-11 01:21:03 -07001011 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001012 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001013 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001014
Frank Mayhar273df552010-03-02 11:46:09 -05001015 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
1016 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001017 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001018 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001019
1020 err = ext4_ext_dirty(handle, inode, curp);
1021 ext4_std_error(inode->i_sb, err);
1022
1023 return err;
1024}
1025
1026/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001027 * ext4_ext_split:
1028 * inserts new subtree into the path, using free index entry
1029 * at depth @at:
1030 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1031 * - makes decision where to split
1032 * - moves remaining extents and index entries (right to the split point)
1033 * into the newly allocated blocks
1034 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001035 */
1036static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001037 unsigned int flags,
1038 struct ext4_ext_path *path,
1039 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001040{
1041 struct buffer_head *bh = NULL;
1042 int depth = ext_depth(inode);
1043 struct ext4_extent_header *neh;
1044 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001045 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001046 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001047 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001048 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001049 int err = 0;
Sriram Rajagopalanab6d14e2019-05-10 19:28:06 -04001050 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001051
1052 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001053 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001054
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001055 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001056 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001057 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1058 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001059 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001060 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001061 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1062 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001063 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001064 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001065 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001066 } else {
1067 border = newext->ee_block;
1068 ext_debug("leaf will be added."
1069 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001070 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001071 }
1072
1073 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001074 * If error occurs, then we break processing
1075 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001076 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001077 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001078 */
1079
1080 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001081 * Get array to track all allocated blocks.
1082 * We need this to handle errors and free blocks
1083 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001084 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -08001085 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001086 if (!ablocks)
1087 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001088
1089 /* allocate all needed blocks */
1090 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1091 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001092 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001093 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001094 if (newblock == 0)
1095 goto cleanup;
1096 ablocks[a] = newblock;
1097 }
1098
1099 /* initialize new leaf */
1100 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001101 if (unlikely(newblock == 0)) {
1102 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001103 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001104 goto cleanup;
1105 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001106 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001107 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001108 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001109 goto cleanup;
1110 }
1111 lock_buffer(bh);
1112
Avantika Mathur7e028972006-12-06 20:41:33 -08001113 err = ext4_journal_get_create_access(handle, bh);
1114 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001115 goto cleanup;
1116
1117 neh = ext_block_hdr(bh);
1118 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001119 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001120 neh->eh_magic = EXT4_EXT_MAGIC;
1121 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001122
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001123 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001124 if (unlikely(path[depth].p_hdr->eh_entries !=
1125 path[depth].p_hdr->eh_max)) {
1126 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1127 path[depth].p_hdr->eh_entries,
1128 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001129 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001130 goto cleanup;
1131 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001132 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001133 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1134 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001135 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001136 struct ext4_extent *ex;
1137 ex = EXT_FIRST_EXTENT(neh);
1138 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001139 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001140 }
1141
Sriram Rajagopalanab6d14e2019-05-10 19:28:06 -04001142 /* zero out unused area in the extent block */
1143 ext_size = sizeof(struct ext4_extent_header) +
1144 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1145 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001146 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001147 set_buffer_uptodate(bh);
1148 unlock_buffer(bh);
1149
Frank Mayhar03901312009-01-07 00:06:22 -05001150 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001151 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001152 goto cleanup;
1153 brelse(bh);
1154 bh = NULL;
1155
1156 /* correct old leaf */
1157 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001158 err = ext4_ext_get_access(handle, inode, path + depth);
1159 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001160 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001161 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001162 err = ext4_ext_dirty(handle, inode, path + depth);
1163 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001164 goto cleanup;
1165
1166 }
1167
1168 /* create intermediate indexes */
1169 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001170 if (unlikely(k < 0)) {
1171 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001172 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001173 goto cleanup;
1174 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001175 if (k)
1176 ext_debug("create %d intermediate indices\n", k);
1177 /* insert new index into current index block */
1178 /* current depth stored in i var */
1179 i = depth - 1;
1180 while (k--) {
1181 oldblock = newblock;
1182 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001183 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001184 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001185 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001186 goto cleanup;
1187 }
1188 lock_buffer(bh);
1189
Avantika Mathur7e028972006-12-06 20:41:33 -08001190 err = ext4_journal_get_create_access(handle, bh);
1191 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001192 goto cleanup;
1193
1194 neh = ext_block_hdr(bh);
1195 neh->eh_entries = cpu_to_le16(1);
1196 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001197 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001198 neh->eh_depth = cpu_to_le16(depth - i);
1199 fidx = EXT_FIRST_INDEX(neh);
1200 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001201 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001202
Eric Sandeenbba90742008-01-28 23:58:27 -05001203 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1204 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001205
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001206 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001207 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1208 EXT_LAST_INDEX(path[i].p_hdr))) {
1209 EXT4_ERROR_INODE(inode,
1210 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1211 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001212 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001213 goto cleanup;
1214 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001215 /* start copy indexes */
1216 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1217 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1218 EXT_MAX_INDEX(path[i].p_hdr));
1219 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001220 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001221 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001222 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001223 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001224 }
Sriram Rajagopalanab6d14e2019-05-10 19:28:06 -04001225 /* zero out unused area in the extent block */
1226 ext_size = sizeof(struct ext4_extent_header) +
1227 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1228 memset(bh->b_data + ext_size, 0,
1229 inode->i_sb->s_blocksize - ext_size);
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001230 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001231 set_buffer_uptodate(bh);
1232 unlock_buffer(bh);
1233
Frank Mayhar03901312009-01-07 00:06:22 -05001234 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001235 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001236 goto cleanup;
1237 brelse(bh);
1238 bh = NULL;
1239
1240 /* correct old index */
1241 if (m) {
1242 err = ext4_ext_get_access(handle, inode, path + i);
1243 if (err)
1244 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001245 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001246 err = ext4_ext_dirty(handle, inode, path + i);
1247 if (err)
1248 goto cleanup;
1249 }
1250
1251 i--;
1252 }
1253
1254 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001255 err = ext4_ext_insert_index(handle, inode, path + at,
1256 le32_to_cpu(border), newblock);
1257
1258cleanup:
1259 if (bh) {
1260 if (buffer_locked(bh))
1261 unlock_buffer(bh);
1262 brelse(bh);
1263 }
1264
1265 if (err) {
1266 /* free all allocated blocks in error case */
1267 for (i = 0; i < depth; i++) {
1268 if (!ablocks[i])
1269 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001270 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001271 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001272 }
1273 }
1274 kfree(ablocks);
1275
1276 return err;
1277}
1278
1279/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001280 * ext4_ext_grow_indepth:
1281 * implements tree growing procedure:
1282 * - allocates new block
1283 * - moves top-level data (index block or leaf) into the new block
1284 * - initializes new top-level, creating index that points to the
1285 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001286 */
1287static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001288 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001289{
Alex Tomasa86c6182006-10-11 01:21:03 -07001290 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001291 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001292 ext4_fsblk_t newblock, goal = 0;
1293 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001294 int err = 0;
Sriram Rajagopalanab6d14e2019-05-10 19:28:06 -04001295 size_t ext_size = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001296
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001297 /* Try to prepend new index to old one */
1298 if (ext_depth(inode))
1299 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1300 if (goal > le32_to_cpu(es->s_first_data_block)) {
1301 flags |= EXT4_MB_HINT_TRY_GOAL;
1302 goal--;
1303 } else
1304 goal = ext4_inode_to_goal_block(inode);
1305 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1306 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001307 if (newblock == 0)
1308 return err;
1309
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001310 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001311 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001312 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001313 lock_buffer(bh);
1314
Avantika Mathur7e028972006-12-06 20:41:33 -08001315 err = ext4_journal_get_create_access(handle, bh);
1316 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001317 unlock_buffer(bh);
1318 goto out;
1319 }
1320
Sriram Rajagopalanab6d14e2019-05-10 19:28:06 -04001321 ext_size = sizeof(EXT4_I(inode)->i_data);
Alex Tomasa86c6182006-10-11 01:21:03 -07001322 /* move top-level index/leaf into new block */
Sriram Rajagopalanab6d14e2019-05-10 19:28:06 -04001323 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1324 /* zero out unused area in the extent block */
1325 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07001326
1327 /* set size of new block */
1328 neh = ext_block_hdr(bh);
1329 /* old root could have indexes or leaves
1330 * so calculate e_max right way */
1331 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001332 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001333 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001334 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001335 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001336 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001337 set_buffer_uptodate(bh);
1338 unlock_buffer(bh);
1339
Frank Mayhar03901312009-01-07 00:06:22 -05001340 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001341 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001342 goto out;
1343
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001344 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001345 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001346 neh->eh_entries = cpu_to_le16(1);
1347 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1348 if (neh->eh_depth == 0) {
1349 /* Root extent block becomes index block */
1350 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1351 EXT_FIRST_INDEX(neh)->ei_block =
1352 EXT_FIRST_EXTENT(neh)->ee_block;
1353 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001354 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001355 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001356 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001357 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001358
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001359 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001360 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001361out:
1362 brelse(bh);
1363
1364 return err;
1365}
1366
1367/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001368 * ext4_ext_create_new_leaf:
1369 * finds empty index and adds new leaf.
1370 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001371 */
1372static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001373 unsigned int mb_flags,
1374 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001375 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001376 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001377{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001378 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001379 struct ext4_ext_path *curp;
1380 int depth, i, err = 0;
1381
1382repeat:
1383 i = depth = ext_depth(inode);
1384
1385 /* walk up to the tree and look for free index entry */
1386 curp = path + depth;
1387 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1388 i--;
1389 curp--;
1390 }
1391
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001392 /* we use already allocated block for index block,
1393 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001394 if (EXT_HAS_FREE_INDEX(curp)) {
1395 /* if we found index with free entry, then use that
1396 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001397 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001398 if (err)
1399 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001400
1401 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001402 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001403 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001404 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001405 if (IS_ERR(path))
1406 err = PTR_ERR(path);
1407 } else {
1408 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001409 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001410 if (err)
1411 goto out;
1412
1413 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001414 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001415 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001416 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001417 if (IS_ERR(path)) {
1418 err = PTR_ERR(path);
1419 goto out;
1420 }
1421
1422 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001423 * only first (depth 0 -> 1) produces free space;
1424 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001425 */
1426 depth = ext_depth(inode);
1427 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001428 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001429 goto repeat;
1430 }
1431 }
1432
1433out:
1434 return err;
1435}
1436
1437/*
Alex Tomas1988b512008-01-28 23:58:27 -05001438 * search the closest allocated block to the left for *logical
1439 * and returns it at @logical + it's physical address at @phys
1440 * if *logical is the smallest allocated block, the function
1441 * returns 0 at @phys
1442 * return value contains 0 (success) or error code
1443 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001444static int ext4_ext_search_left(struct inode *inode,
1445 struct ext4_ext_path *path,
1446 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001447{
1448 struct ext4_extent_idx *ix;
1449 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001450 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001451
Frank Mayhar273df552010-03-02 11:46:09 -05001452 if (unlikely(path == NULL)) {
1453 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001454 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001455 }
Alex Tomas1988b512008-01-28 23:58:27 -05001456 depth = path->p_depth;
1457 *phys = 0;
1458
1459 if (depth == 0 && path->p_ext == NULL)
1460 return 0;
1461
1462 /* usually extent in the path covers blocks smaller
1463 * then *logical, but it can be that extent is the
1464 * first one in the file */
1465
1466 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001467 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001468 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001469 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1470 EXT4_ERROR_INODE(inode,
1471 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1472 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001473 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001474 }
Alex Tomas1988b512008-01-28 23:58:27 -05001475 while (--depth >= 0) {
1476 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001477 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1478 EXT4_ERROR_INODE(inode,
1479 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001480 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001481 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001482 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001483 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001484 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001485 }
Alex Tomas1988b512008-01-28 23:58:27 -05001486 }
1487 return 0;
1488 }
1489
Frank Mayhar273df552010-03-02 11:46:09 -05001490 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1491 EXT4_ERROR_INODE(inode,
1492 "logical %d < ee_block %d + ee_len %d!",
1493 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001494 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001495 }
Alex Tomas1988b512008-01-28 23:58:27 -05001496
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001497 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001498 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001499 return 0;
1500}
1501
1502/*
1503 * search the closest allocated block to the right for *logical
1504 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001505 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001506 * returns 0 at @phys
1507 * return value contains 0 (success) or error code
1508 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001509static int ext4_ext_search_right(struct inode *inode,
1510 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001511 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1512 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001513{
1514 struct buffer_head *bh = NULL;
1515 struct ext4_extent_header *eh;
1516 struct ext4_extent_idx *ix;
1517 struct ext4_extent *ex;
1518 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001519 int depth; /* Note, NOT eh_depth; depth from top of tree */
1520 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001521
Frank Mayhar273df552010-03-02 11:46:09 -05001522 if (unlikely(path == NULL)) {
1523 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001524 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001525 }
Alex Tomas1988b512008-01-28 23:58:27 -05001526 depth = path->p_depth;
1527 *phys = 0;
1528
1529 if (depth == 0 && path->p_ext == NULL)
1530 return 0;
1531
1532 /* usually extent in the path covers blocks smaller
1533 * then *logical, but it can be that extent is the
1534 * first one in the file */
1535
1536 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001537 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001538 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001539 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1540 EXT4_ERROR_INODE(inode,
1541 "first_extent(path[%d].p_hdr) != ex",
1542 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001543 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001544 }
Alex Tomas1988b512008-01-28 23:58:27 -05001545 while (--depth >= 0) {
1546 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001547 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1548 EXT4_ERROR_INODE(inode,
1549 "ix != EXT_FIRST_INDEX *logical %d!",
1550 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001551 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001552 }
Alex Tomas1988b512008-01-28 23:58:27 -05001553 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001554 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001555 }
1556
Frank Mayhar273df552010-03-02 11:46:09 -05001557 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1558 EXT4_ERROR_INODE(inode,
1559 "logical %d < ee_block %d + ee_len %d!",
1560 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001561 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001562 }
Alex Tomas1988b512008-01-28 23:58:27 -05001563
1564 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1565 /* next allocated block in this leaf */
1566 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001567 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001568 }
1569
1570 /* go up and search for index to the right */
1571 while (--depth >= 0) {
1572 ix = path[depth].p_idx;
1573 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001574 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001575 }
1576
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001577 /* we've gone up to the root and found no index to the right */
1578 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001579
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001580got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001581 /* we've found index to the right, let's
1582 * follow it and find the closest allocated
1583 * block to the right */
1584 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001585 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001586 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001587 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001588 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001589 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001590 if (IS_ERR(bh))
1591 return PTR_ERR(bh);
1592 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001593 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001594 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001595 put_bh(bh);
1596 }
1597
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001598 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001599 if (IS_ERR(bh))
1600 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001601 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001602 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001603found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001604 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001605 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001606 *ret_ex = ex;
1607 if (bh)
1608 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001609 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001610}
1611
1612/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001613 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001614 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001615 * NOTE: it considers block number from index entry as
1616 * allocated block. Thus, index entries have to be consistent
1617 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001618 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001619ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001620ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1621{
1622 int depth;
1623
1624 BUG_ON(path == NULL);
1625 depth = path->p_depth;
1626
1627 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001628 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001629
1630 while (depth >= 0) {
1631 if (depth == path->p_depth) {
1632 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001633 if (path[depth].p_ext &&
1634 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001635 EXT_LAST_EXTENT(path[depth].p_hdr))
1636 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1637 } else {
1638 /* index */
1639 if (path[depth].p_idx !=
1640 EXT_LAST_INDEX(path[depth].p_hdr))
1641 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1642 }
1643 depth--;
1644 }
1645
Lukas Czernerf17722f2011-06-06 00:05:17 -04001646 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001647}
1648
1649/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001650 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001651 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001652 */
Robin Dong57187892011-07-23 21:49:07 -04001653static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001654{
1655 int depth;
1656
1657 BUG_ON(path == NULL);
1658 depth = path->p_depth;
1659
1660 /* zero-tree has no leaf blocks at all */
1661 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001662 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001663
1664 /* go to index block */
1665 depth--;
1666
1667 while (depth >= 0) {
1668 if (path[depth].p_idx !=
1669 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001670 return (ext4_lblk_t)
1671 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001672 depth--;
1673 }
1674
Lukas Czernerf17722f2011-06-06 00:05:17 -04001675 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001676}
1677
1678/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001679 * ext4_ext_correct_indexes:
1680 * if leaf gets modified and modified extent is first in the leaf,
1681 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001682 * TODO: do we need to correct tree in all cases?
1683 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001684static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001685 struct ext4_ext_path *path)
1686{
1687 struct ext4_extent_header *eh;
1688 int depth = ext_depth(inode);
1689 struct ext4_extent *ex;
1690 __le32 border;
1691 int k, err = 0;
1692
1693 eh = path[depth].p_hdr;
1694 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001695
1696 if (unlikely(ex == NULL || eh == NULL)) {
1697 EXT4_ERROR_INODE(inode,
1698 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001699 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001700 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001701
1702 if (depth == 0) {
1703 /* there is no tree at all */
1704 return 0;
1705 }
1706
1707 if (ex != EXT_FIRST_EXTENT(eh)) {
1708 /* we correct tree if first leaf got modified only */
1709 return 0;
1710 }
1711
1712 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001713 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001714 */
1715 k = depth - 1;
1716 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001717 err = ext4_ext_get_access(handle, inode, path + k);
1718 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001719 return err;
1720 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001721 err = ext4_ext_dirty(handle, inode, path + k);
1722 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001723 return err;
1724
1725 while (k--) {
1726 /* change all left-side indexes */
1727 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1728 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001729 err = ext4_ext_get_access(handle, inode, path + k);
1730 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001731 break;
1732 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001733 err = ext4_ext_dirty(handle, inode, path + k);
1734 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001735 break;
1736 }
1737
1738 return err;
1739}
1740
Akira Fujita748de672009-06-17 19:24:03 -04001741int
Alex Tomasa86c6182006-10-11 01:21:03 -07001742ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1743 struct ext4_extent *ex2)
1744{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001745 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001746
Lukas Czerner556615d2014-04-20 23:45:47 -04001747 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001748 return 0;
1749
1750 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1751 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1752
1753 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001754 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001755 return 0;
1756
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001757 /*
1758 * To allow future support for preallocated extents to be added
1759 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001760 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001761 */
Eric Sandeenda0169b2013-11-04 09:58:26 -05001762 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001763 return 0;
Jan Kara109811c2016-03-08 23:36:46 -05001764 /*
1765 * The check for IO to unwritten extent is somewhat racy as we
1766 * increment i_unwritten / set EXT4_STATE_DIO_UNWRITTEN only after
1767 * dropping i_data_sem. But reserved blocks should save us in that
1768 * case.
1769 */
Lukas Czerner556615d2014-04-20 23:45:47 -04001770 if (ext4_ext_is_unwritten(ex1) &&
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001771 (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) ||
1772 atomic_read(&EXT4_I(inode)->i_unwritten) ||
Lukas Czerner556615d2014-04-20 23:45:47 -04001773 (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)))
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001774 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001775#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001776 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001777 return 0;
1778#endif
1779
Theodore Ts'obf89d162010-10-27 21:30:14 -04001780 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001781 return 1;
1782 return 0;
1783}
1784
1785/*
Amit Arora56055d32007-07-17 21:42:38 -04001786 * This function tries to merge the "ex" extent to the next extent in the tree.
1787 * It always tries to merge towards right. If you want to merge towards
1788 * left, pass "ex - 1" as argument instead of "ex".
1789 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1790 * 1 if they got merged.
1791 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001792static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001793 struct ext4_ext_path *path,
1794 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001795{
1796 struct ext4_extent_header *eh;
1797 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001798 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001799
1800 depth = ext_depth(inode);
1801 BUG_ON(path[depth].p_hdr == NULL);
1802 eh = path[depth].p_hdr;
1803
1804 while (ex < EXT_LAST_EXTENT(eh)) {
1805 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1806 break;
1807 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001808 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001809 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1810 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001811 if (unwritten)
1812 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001813
1814 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1815 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1816 * sizeof(struct ext4_extent);
1817 memmove(ex + 1, ex + 2, len);
1818 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001819 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001820 merge_done = 1;
1821 WARN_ON(eh->eh_entries == 0);
1822 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001823 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001824 }
1825
1826 return merge_done;
1827}
1828
1829/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001830 * This function does a very simple check to see if we can collapse
1831 * an extent tree with a single extent tree leaf block into the inode.
1832 */
1833static void ext4_ext_try_to_merge_up(handle_t *handle,
1834 struct inode *inode,
1835 struct ext4_ext_path *path)
1836{
1837 size_t s;
1838 unsigned max_root = ext4_ext_space_root(inode, 0);
1839 ext4_fsblk_t blk;
1840
1841 if ((path[0].p_depth != 1) ||
1842 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1843 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1844 return;
1845
1846 /*
1847 * We need to modify the block allocation bitmap and the block
1848 * group descriptor to release the extent tree block. If we
1849 * can't get the journal credits, give up.
1850 */
1851 if (ext4_journal_extend(handle, 2))
1852 return;
1853
1854 /*
1855 * Copy the extent data up to the inode
1856 */
1857 blk = ext4_idx_pblock(path[0].p_idx);
1858 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1859 sizeof(struct ext4_extent_idx);
1860 s += sizeof(struct ext4_extent_header);
1861
Theodore Ts'o10809df82014-09-01 14:40:09 -04001862 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001863 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1864 path[0].p_depth = 0;
1865 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1866 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1867 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1868
1869 brelse(path[1].p_bh);
1870 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001871 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001872}
1873
1874/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001875 * This function tries to merge the @ex extent to neighbours in the tree.
1876 * return 1 if merge left else 0.
1877 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001878static void ext4_ext_try_to_merge(handle_t *handle,
1879 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001880 struct ext4_ext_path *path,
1881 struct ext4_extent *ex) {
1882 struct ext4_extent_header *eh;
1883 unsigned int depth;
1884 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001885
1886 depth = ext_depth(inode);
1887 BUG_ON(path[depth].p_hdr == NULL);
1888 eh = path[depth].p_hdr;
1889
1890 if (ex > EXT_FIRST_EXTENT(eh))
1891 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1892
1893 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001894 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001895
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001896 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001897}
1898
1899/*
Amit Arora25d14f92007-05-24 13:04:13 -04001900 * check if a portion of the "newext" extent overlaps with an
1901 * existing extent.
1902 *
1903 * If there is an overlap discovered, it updates the length of the newext
1904 * such that there will be no overlap, and then returns 1.
1905 * If there is no overlap found, it returns 0.
1906 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001907static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1908 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001909 struct ext4_extent *newext,
1910 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001911{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001912 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001913 unsigned int depth, len1;
1914 unsigned int ret = 0;
1915
1916 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001917 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001918 depth = ext_depth(inode);
1919 if (!path[depth].p_ext)
1920 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001921 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001922
1923 /*
1924 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001925 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001926 */
1927 if (b2 < b1) {
1928 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001929 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001930 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001931 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001932 }
1933
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001934 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001935 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001936 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001937 newext->ee_len = cpu_to_le16(len1);
1938 ret = 1;
1939 }
1940
1941 /* check for overlap */
1942 if (b1 + len1 > b2) {
1943 newext->ee_len = cpu_to_le16(b2 - b1);
1944 ret = 1;
1945 }
1946out:
1947 return ret;
1948}
1949
1950/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001951 * ext4_ext_insert_extent:
1952 * tries to merge requsted extent into the existing extent or
1953 * inserts requested extent as new one into the tree,
1954 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001955 */
1956int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001957 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001958 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001959{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001960 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001961 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001962 struct ext4_extent *ex, *fex;
1963 struct ext4_extent *nearex; /* nearest extent */
1964 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001965 int depth, len, err;
1966 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001967 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001968
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001969 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1970 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001971 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1972 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001973 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001974 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001975 depth = ext_depth(inode);
1976 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001977 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001978 if (unlikely(path[depth].p_hdr == NULL)) {
1979 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001980 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001981 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001982
1983 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001984 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001985
1986 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001987 * Try to see whether we should rather test the extent on
1988 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001989 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001990 * left, or on the right from the searched position. This
1991 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001992 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001993 if (ex < EXT_LAST_EXTENT(eh) &&
1994 (le32_to_cpu(ex->ee_block) +
1995 ext4_ext_get_actual_len(ex) <
1996 le32_to_cpu(newext->ee_block))) {
1997 ex += 1;
1998 goto prepend;
1999 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
2000 (le32_to_cpu(newext->ee_block) +
2001 ext4_ext_get_actual_len(newext) <
2002 le32_to_cpu(ex->ee_block)))
2003 ex -= 1;
2004
2005 /* Try to append newex to the ex */
2006 if (ext4_can_extents_be_merged(inode, ex, newext)) {
2007 ext_debug("append [%d]%d block to %u:[%d]%d"
2008 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04002009 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002010 ext4_ext_get_actual_len(newext),
2011 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002012 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002013 ext4_ext_get_actual_len(ex),
2014 ext4_ext_pblock(ex));
2015 err = ext4_ext_get_access(handle, inode,
2016 path + depth);
2017 if (err)
2018 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04002019 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002020 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002021 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002022 if (unwritten)
2023 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002024 eh = path[depth].p_hdr;
2025 nearex = ex;
2026 goto merge;
2027 }
2028
2029prepend:
2030 /* Try to prepend newex to the ex */
2031 if (ext4_can_extents_be_merged(inode, newext, ex)) {
2032 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2033 "(from %llu)\n",
2034 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002035 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002036 ext4_ext_get_actual_len(newext),
2037 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002038 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002039 ext4_ext_get_actual_len(ex),
2040 ext4_ext_pblock(ex));
2041 err = ext4_ext_get_access(handle, inode,
2042 path + depth);
2043 if (err)
2044 return err;
2045
Lukas Czerner556615d2014-04-20 23:45:47 -04002046 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002047 ex->ee_block = newext->ee_block;
2048 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2049 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2050 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002051 if (unwritten)
2052 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002053 eh = path[depth].p_hdr;
2054 nearex = ex;
2055 goto merge;
2056 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002057 }
2058
Alex Tomasa86c6182006-10-11 01:21:03 -07002059 depth = ext_depth(inode);
2060 eh = path[depth].p_hdr;
2061 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2062 goto has_space;
2063
2064 /* probably next leaf has space for us? */
2065 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002066 next = EXT_MAX_BLOCKS;
2067 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002068 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002069 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002070 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002071 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002072 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002073 if (IS_ERR(npath))
2074 return PTR_ERR(npath);
2075 BUG_ON(npath->p_depth != path->p_depth);
2076 eh = npath[depth].p_hdr;
2077 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002078 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002079 le16_to_cpu(eh->eh_entries));
2080 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002081 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002082 }
2083 ext_debug("next leaf has no free space(%d,%d)\n",
2084 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2085 }
2086
2087 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002088 * There is no free space in the found leaf.
2089 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002090 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002091 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002092 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002093 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002094 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002095 if (err)
2096 goto cleanup;
2097 depth = ext_depth(inode);
2098 eh = path[depth].p_hdr;
2099
2100has_space:
2101 nearex = path[depth].p_ext;
2102
Avantika Mathur7e028972006-12-06 20:41:33 -08002103 err = ext4_ext_get_access(handle, inode, path + depth);
2104 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002105 goto cleanup;
2106
2107 if (!nearex) {
2108 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002109 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002110 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002111 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002112 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002113 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002114 nearex = EXT_FIRST_EXTENT(eh);
2115 } else {
2116 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002117 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002118 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002119 ext_debug("insert %u:%llu:[%d]%d before: "
2120 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002121 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002122 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002123 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002124 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002125 nearex);
2126 nearex++;
2127 } else {
2128 /* Insert before */
2129 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002130 ext_debug("insert %u:%llu:[%d]%d after: "
2131 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002132 le32_to_cpu(newext->ee_block),
2133 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002134 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002135 ext4_ext_get_actual_len(newext),
2136 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002137 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002138 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2139 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002140 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002141 "move %d extents from 0x%p to 0x%p\n",
2142 le32_to_cpu(newext->ee_block),
2143 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002144 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002145 ext4_ext_get_actual_len(newext),
2146 len, nearex, nearex + 1);
2147 memmove(nearex + 1, nearex,
2148 len * sizeof(struct ext4_extent));
2149 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002150 }
2151
Marcin Slusarze8546d02008-04-17 10:38:59 -04002152 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002153 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002154 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002155 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002156 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002157
2158merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002159 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002160 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002161 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002162
Alex Tomasa86c6182006-10-11 01:21:03 -07002163
2164 /* time to correct all indexes above */
2165 err = ext4_ext_correct_indexes(handle, inode, path);
2166 if (err)
2167 goto cleanup;
2168
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002169 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002170
2171cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002172 ext4_ext_drop_refs(npath);
2173 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002174 return err;
2175}
2176
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002177static int ext4_fill_fiemap_extents(struct inode *inode,
2178 ext4_lblk_t block, ext4_lblk_t num,
2179 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04002180{
2181 struct ext4_ext_path *path = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002182 struct ext4_extent *ex;
Zheng Liu69eb33d2013-02-18 00:31:07 -05002183 struct extent_status es;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002184 ext4_lblk_t next, next_del, start = 0, end = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002185 ext4_lblk_t last = block + num;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002186 int exists, depth = 0, err = 0;
2187 unsigned int flags = 0;
2188 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002189
Lukas Czernerf17722f2011-06-06 00:05:17 -04002190 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04002191 num = last - block;
2192 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05002193 down_read(&EXT4_I(inode)->i_data_sem);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002194
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002195 path = ext4_find_extent(inode, block, &path, 0);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002196 if (IS_ERR(path)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002197 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002198 err = PTR_ERR(path);
2199 path = NULL;
2200 break;
2201 }
2202
2203 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05002204 if (unlikely(path[depth].p_hdr == NULL)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002205 up_read(&EXT4_I(inode)->i_data_sem);
Frank Mayhar273df552010-03-02 11:46:09 -05002206 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002207 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002208 break;
2209 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002210 ex = path[depth].p_ext;
2211 next = ext4_ext_next_allocated_block(path);
2212
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002213 flags = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002214 exists = 0;
2215 if (!ex) {
2216 /* there is no extent yet, so try to allocate
2217 * all requested space */
2218 start = block;
2219 end = block + num;
2220 } else if (le32_to_cpu(ex->ee_block) > block) {
2221 /* need to allocate space before found extent */
2222 start = block;
2223 end = le32_to_cpu(ex->ee_block);
2224 if (block + num < end)
2225 end = block + num;
2226 } else if (block >= le32_to_cpu(ex->ee_block)
2227 + ext4_ext_get_actual_len(ex)) {
2228 /* need to allocate space after found extent */
2229 start = block;
2230 end = block + num;
2231 if (end >= next)
2232 end = next;
2233 } else if (block >= le32_to_cpu(ex->ee_block)) {
2234 /*
2235 * some part of requested space is covered
2236 * by found extent
2237 */
2238 start = block;
2239 end = le32_to_cpu(ex->ee_block)
2240 + ext4_ext_get_actual_len(ex);
2241 if (block + num < end)
2242 end = block + num;
2243 exists = 1;
2244 } else {
2245 BUG();
2246 }
2247 BUG_ON(end <= start);
2248
2249 if (!exists) {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002250 es.es_lblk = start;
2251 es.es_len = end - start;
2252 es.es_pblk = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002253 } else {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002254 es.es_lblk = le32_to_cpu(ex->ee_block);
2255 es.es_len = ext4_ext_get_actual_len(ex);
2256 es.es_pblk = ext4_ext_pblock(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04002257 if (ext4_ext_is_unwritten(ex))
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002258 flags |= FIEMAP_EXTENT_UNWRITTEN;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002259 }
2260
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002261 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05002262 * Find delayed extent and update es accordingly. We call
2263 * it even in !exists case to find out whether es is the
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002264 * last existing extent or not.
2265 */
Zheng Liu69eb33d2013-02-18 00:31:07 -05002266 next_del = ext4_find_delayed_extent(inode, &es);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002267 if (!exists && next_del) {
2268 exists = 1;
Jie Liu72dac952013-06-12 23:13:59 -04002269 flags |= (FIEMAP_EXTENT_DELALLOC |
2270 FIEMAP_EXTENT_UNKNOWN);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002271 }
2272 up_read(&EXT4_I(inode)->i_data_sem);
2273
Zheng Liu69eb33d2013-02-18 00:31:07 -05002274 if (unlikely(es.es_len == 0)) {
2275 EXT4_ERROR_INODE(inode, "es.es_len == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002276 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002277 break;
2278 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002279
Zheng Liuf7fec032013-02-18 00:28:47 -05002280 /*
2281 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2282 * we need to check next == EXT_MAX_BLOCKS because it is
2283 * possible that an extent is with unwritten and delayed
2284 * status due to when an extent is delayed allocated and
2285 * is allocated by fallocate status tree will track both of
2286 * them in a extent.
2287 *
2288 * So we could return a unwritten and delayed extent, and
2289 * its block is equal to 'next'.
2290 */
2291 if (next == next_del && next == EXT_MAX_BLOCKS) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002292 flags |= FIEMAP_EXTENT_LAST;
2293 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2294 next != EXT_MAX_BLOCKS)) {
2295 EXT4_ERROR_INODE(inode,
2296 "next extent == %u, next "
2297 "delalloc extent = %u",
2298 next, next_del);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002299 err = -EFSCORRUPTED;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002300 break;
2301 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002302 }
2303
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002304 if (exists) {
2305 err = fiemap_fill_next_extent(fieinfo,
Zheng Liu69eb33d2013-02-18 00:31:07 -05002306 (__u64)es.es_lblk << blksize_bits,
2307 (__u64)es.es_pblk << blksize_bits,
2308 (__u64)es.es_len << blksize_bits,
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002309 flags);
2310 if (err < 0)
2311 break;
2312 if (err == 1) {
2313 err = 0;
2314 break;
2315 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002316 }
2317
Zheng Liu69eb33d2013-02-18 00:31:07 -05002318 block = es.es_lblk + es.es_len;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002319 }
2320
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002321 ext4_ext_drop_refs(path);
2322 kfree(path);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002323 return err;
2324}
2325
Alex Tomasa86c6182006-10-11 01:21:03 -07002326/*
Jan Kara140a5252016-03-09 22:46:57 -05002327 * ext4_ext_determine_hole - determine hole around given block
2328 * @inode: inode we lookup in
2329 * @path: path in extent tree to @lblk
2330 * @lblk: pointer to logical block around which we want to determine hole
2331 *
2332 * Determine hole length (and start if easily possible) around given logical
2333 * block. We don't try too hard to find the beginning of the hole but @path
2334 * actually points to extent before @lblk, we provide it.
2335 *
2336 * The function returns the length of a hole starting at @lblk. We update @lblk
2337 * to the beginning of the hole if we managed to find it.
2338 */
2339static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2340 struct ext4_ext_path *path,
2341 ext4_lblk_t *lblk)
2342{
2343 int depth = ext_depth(inode);
2344 struct ext4_extent *ex;
2345 ext4_lblk_t len;
2346
2347 ex = path[depth].p_ext;
2348 if (ex == NULL) {
2349 /* there is no extent yet, so gap is [0;-] */
2350 *lblk = 0;
2351 len = EXT_MAX_BLOCKS;
2352 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2353 len = le32_to_cpu(ex->ee_block) - *lblk;
2354 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2355 + ext4_ext_get_actual_len(ex)) {
2356 ext4_lblk_t next;
2357
2358 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2359 next = ext4_ext_next_allocated_block(path);
2360 BUG_ON(next == *lblk);
2361 len = next - *lblk;
2362 } else {
2363 BUG();
2364 }
2365 return len;
2366}
2367
2368/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002369 * ext4_ext_put_gap_in_cache:
2370 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002371 * and cache this gap
2372 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002373static void
Jan Kara140a5252016-03-09 22:46:57 -05002374ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2375 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002376{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002377 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002378
Jan Kara140a5252016-03-09 22:46:57 -05002379 ext4_es_find_delayed_extent_range(inode, hole_start,
2380 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002381 if (es.es_len) {
2382 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002383 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002384 return;
Jan Kara140a5252016-03-09 22:46:57 -05002385 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002386 }
Jan Kara140a5252016-03-09 22:46:57 -05002387 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2388 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2389 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002390}
2391
2392/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002393 * ext4_ext_rm_idx:
2394 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002395 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002396static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002397 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002398{
Alex Tomasa86c6182006-10-11 01:21:03 -07002399 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002400 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002401
2402 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002403 depth--;
2404 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002405 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002406 if (unlikely(path->p_hdr->eh_entries == 0)) {
2407 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002408 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002409 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002410 err = ext4_ext_get_access(handle, inode, path);
2411 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002412 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002413
2414 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2415 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2416 len *= sizeof(struct ext4_extent_idx);
2417 memmove(path->p_idx, path->p_idx + 1, len);
2418 }
2419
Marcin Slusarze8546d02008-04-17 10:38:59 -04002420 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002421 err = ext4_ext_dirty(handle, inode, path);
2422 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002423 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002424 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002425 trace_ext4_ext_rm_idx(inode, leaf);
2426
Peter Huewe7dc57612011-02-21 21:01:42 -05002427 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002428 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002429
2430 while (--depth >= 0) {
2431 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2432 break;
2433 path--;
2434 err = ext4_ext_get_access(handle, inode, path);
2435 if (err)
2436 break;
2437 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2438 err = ext4_ext_dirty(handle, inode, path);
2439 if (err)
2440 break;
2441 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002442 return err;
2443}
2444
2445/*
Mingming Caoee12b632008-08-19 22:16:05 -04002446 * ext4_ext_calc_credits_for_single_extent:
2447 * This routine returns max. credits that needed to insert an extent
2448 * to the extent tree.
2449 * When pass the actual path, the caller should calculate credits
2450 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002451 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002452int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002453 struct ext4_ext_path *path)
2454{
Alex Tomasa86c6182006-10-11 01:21:03 -07002455 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002456 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002457 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002458
Alex Tomasa86c6182006-10-11 01:21:03 -07002459 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002460 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002461 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2462
2463 /*
2464 * There are some space in the leaf tree, no
2465 * need to account for leaf block credit
2466 *
2467 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002468 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002469 * accounted.
2470 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002471 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002472 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002473 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002474 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002475 }
2476
Mingming Cao525f4ed2008-08-19 22:15:58 -04002477 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002478}
Alex Tomasa86c6182006-10-11 01:21:03 -07002479
Mingming Caoee12b632008-08-19 22:16:05 -04002480/*
Jan Karafffb2732013-06-04 13:01:11 -04002481 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002482 *
Jan Karafffb2732013-06-04 13:01:11 -04002483 * If we add a single extent, then in the worse case, each tree level
2484 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002485 *
Jan Karafffb2732013-06-04 13:01:11 -04002486 * If more extents are inserted, they could cause the whole tree split more
2487 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002488 */
Jan Karafffb2732013-06-04 13:01:11 -04002489int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002490{
2491 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002492 int depth;
2493
2494 /* If we are converting the inline data, only one is needed here. */
2495 if (ext4_has_inline_data(inode))
2496 return 1;
2497
2498 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002499
Jan Karafffb2732013-06-04 13:01:11 -04002500 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002501 index = depth * 2;
2502 else
2503 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002504
Mingming Caoee12b632008-08-19 22:16:05 -04002505 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002506}
2507
Theodore Ts'o981250c2013-06-12 11:48:29 -04002508static inline int get_default_free_blocks_flags(struct inode *inode)
2509{
2510 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2511 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2512 else if (ext4_should_journal_data(inode))
2513 return EXT4_FREE_BLOCKS_FORGET;
2514 return 0;
2515}
2516
Alex Tomasa86c6182006-10-11 01:21:03 -07002517static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002518 struct ext4_extent *ex,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002519 long long *partial_cluster,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002520 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002521{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002522 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002523 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002524 ext4_fsblk_t pblk;
Theodore Ts'o981250c2013-06-12 11:48:29 -04002525 int flags = get_default_free_blocks_flags(inode);
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002526
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002527 /*
2528 * For bigalloc file systems, we never free a partial cluster
2529 * at the beginning of the extent. Instead, we make a note
2530 * that we tried freeing the cluster, and check to see if we
2531 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002532 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002533 */
2534 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2535
Aditya Kalid8990242011-09-09 19:18:51 -04002536 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002537 /*
2538 * If we have a partial cluster, and it's different from the
2539 * cluster of the last block, we need to explicitly free the
2540 * partial cluster here.
2541 */
2542 pblk = ext4_ext_pblock(ex) + ee_len - 1;
Eric Whitney345ee942014-11-23 00:59:39 -05002543 if (*partial_cluster > 0 &&
2544 *partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002545 ext4_free_blocks(handle, inode, NULL,
2546 EXT4_C2B(sbi, *partial_cluster),
2547 sbi->s_cluster_ratio, flags);
2548 *partial_cluster = 0;
2549 }
2550
Alex Tomasa86c6182006-10-11 01:21:03 -07002551#ifdef EXTENTS_STATS
2552 {
2553 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002554 spin_lock(&sbi->s_ext_stats_lock);
2555 sbi->s_ext_blocks += ee_len;
2556 sbi->s_ext_extents++;
2557 if (ee_len < sbi->s_ext_min)
2558 sbi->s_ext_min = ee_len;
2559 if (ee_len > sbi->s_ext_max)
2560 sbi->s_ext_max = ee_len;
2561 if (ext_depth(inode) > sbi->s_depth_max)
2562 sbi->s_depth_max = ext_depth(inode);
2563 spin_unlock(&sbi->s_ext_stats_lock);
2564 }
2565#endif
2566 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002567 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002568 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002569 ext4_lblk_t num;
Eric Whitney345ee942014-11-23 00:59:39 -05002570 long long first_cluster;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002571
Amit Aroraa2df2a62007-07-17 21:42:41 -04002572 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002573 pblk = ext4_ext_pblock(ex) + ee_len - num;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002574 /*
2575 * Usually we want to free partial cluster at the end of the
2576 * extent, except for the situation when the cluster is still
2577 * used by any other extent (partial_cluster is negative).
2578 */
2579 if (*partial_cluster < 0 &&
Eric Whitney345ee942014-11-23 00:59:39 -05002580 *partial_cluster == -(long long) EXT4_B2C(sbi, pblk+num-1))
Lukas Czernerd23142c2013-05-27 23:33:35 -04002581 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
2582
2583 ext_debug("free last %u blocks starting %llu partial %lld\n",
2584 num, pblk, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002585 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2586 /*
2587 * If the block range to be freed didn't start at the
2588 * beginning of a cluster, and we removed the entire
Lukas Czernerd23142c2013-05-27 23:33:35 -04002589 * extent and the cluster is not used by any other extent,
2590 * save the partial cluster here, since we might need to
Eric Whitney345ee942014-11-23 00:59:39 -05002591 * delete if we determine that the truncate or punch hole
2592 * operation has removed all of the blocks in the cluster.
2593 * If that cluster is used by another extent, preserve its
2594 * negative value so it isn't freed later on.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002595 *
Eric Whitney345ee942014-11-23 00:59:39 -05002596 * If the whole extent wasn't freed, we've reached the
2597 * start of the truncated/punched region and have finished
2598 * removing blocks. If there's a partial cluster here it's
2599 * shared with the remainder of the extent and is no longer
2600 * a candidate for removal.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002601 */
Eric Whitney345ee942014-11-23 00:59:39 -05002602 if (EXT4_PBLK_COFF(sbi, pblk) && ee_len == num) {
2603 first_cluster = (long long) EXT4_B2C(sbi, pblk);
2604 if (first_cluster != -*partial_cluster)
2605 *partial_cluster = first_cluster;
2606 } else {
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002607 *partial_cluster = 0;
Eric Whitney345ee942014-11-23 00:59:39 -05002608 }
Lukas Czerner78fb9cd2013-05-27 23:32:35 -04002609 } else
2610 ext4_error(sbi->s_sb, "strange request: removal(2) "
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04002611 "%u-%u from %u:%u",
Lukas Czerner78fb9cd2013-05-27 23:32:35 -04002612 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002613 return 0;
2614}
2615
Allison Hendersond583fb82011-05-25 07:41:43 -04002616
2617/*
2618 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002619 * blocks appearing between "start" and "end". Both "start"
2620 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002621 *
2622 * @handle: The journal handle
2623 * @inode: The files inode
2624 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002625 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002626 * has been released from it. However, if this value is
2627 * negative, it's a cluster just to the right of the
2628 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002629 * @start: The first block to remove
2630 * @end: The last block to remove
2631 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002632static int
2633ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002634 struct ext4_ext_path *path,
2635 long long *partial_cluster,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002636 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002637{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002638 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002639 int err = 0, correct_index = 0;
2640 int depth = ext_depth(inode), credits;
2641 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002642 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002643 unsigned num;
2644 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002645 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002646 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002647 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002648 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002649
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002650 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002651 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002652 if (!path[depth].p_hdr)
2653 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2654 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002655 if (unlikely(path[depth].p_hdr == NULL)) {
2656 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002657 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002658 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002659 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002660 ex = path[depth].p_ext;
2661 if (!ex)
2662 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002663
2664 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002665 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002666
Aditya Kalid8990242011-09-09 19:18:51 -04002667 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2668
Alex Tomasa86c6182006-10-11 01:21:03 -07002669 while (ex >= EXT_FIRST_EXTENT(eh) &&
2670 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002671
Lukas Czerner556615d2014-04-20 23:45:47 -04002672 if (ext4_ext_is_unwritten(ex))
2673 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002674 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002675 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002676
Mingming553f9002009-09-18 13:34:55 -04002677 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002678 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002679 path[depth].p_ext = ex;
2680
2681 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002682 b = ex_ee_block+ex_ee_len - 1 < end ?
2683 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002684
2685 ext_debug(" border %u:%u\n", a, b);
2686
Allison Hendersond583fb82011-05-25 07:41:43 -04002687 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002688 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002689 /*
2690 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002691 * so note that its first cluster is in use to avoid
2692 * freeing it when removing blocks. Eventually, the
2693 * right edge of the truncated/punched region will
2694 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002695 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002696 if (sbi->s_cluster_ratio > 1) {
2697 pblk = ext4_ext_pblock(ex);
Lukas Czernerd23142c2013-05-27 23:33:35 -04002698 *partial_cluster =
Eric Whitneyf4226d92014-11-23 00:55:42 -05002699 -(long long) EXT4_B2C(sbi, pblk);
2700 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002701 ex--;
2702 ex_ee_block = le32_to_cpu(ex->ee_block);
2703 ex_ee_len = ext4_ext_get_actual_len(ex);
2704 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002705 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002706 EXT4_ERROR_INODE(inode,
2707 "can not handle truncate %u:%u "
2708 "on extent %u:%u",
2709 start, end, ex_ee_block,
2710 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002711 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002712 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002713 } else if (a != ex_ee_block) {
2714 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002715 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002716 } else {
2717 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002718 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002719 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002720 /*
2721 * 3 for leaf, sb, and inode plus 2 (bmap and group
2722 * descriptor) for each block group; assume two block
2723 * groups plus ex_ee_len/blocks_per_block_group for
2724 * the worst case
2725 */
2726 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002727 if (ex == EXT_FIRST_EXTENT(eh)) {
2728 correct_index = 1;
2729 credits += (ext_depth(inode)) + 1;
2730 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002731 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002732
Jan Kara487caee2009-08-17 22:17:20 -04002733 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002734 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002735 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002736
2737 err = ext4_ext_get_access(handle, inode, path + depth);
2738 if (err)
2739 goto out;
2740
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002741 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2742 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002743 if (err)
2744 goto out;
2745
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002746 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002747 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002748 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002749
Alex Tomasa86c6182006-10-11 01:21:03 -07002750 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002751 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002752 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002753 * extent have been removed.
2754 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002755 if (unwritten && num)
2756 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002757 /*
2758 * If the extent was completely released,
2759 * we need to remove it from the leaf
2760 */
2761 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002762 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002763 /*
2764 * For hole punching, we need to scoot all the
2765 * extents up when an extent is removed so that
2766 * we dont have blank extents in the middle
2767 */
2768 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2769 sizeof(struct ext4_extent));
2770
2771 /* Now get rid of the one at the end */
2772 memset(EXT_LAST_EXTENT(eh), 0,
2773 sizeof(struct ext4_extent));
2774 }
2775 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002776 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002777
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002778 err = ext4_ext_dirty(handle, inode, path + depth);
2779 if (err)
2780 goto out;
2781
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002782 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002783 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002784 ex--;
2785 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002786 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002787 }
2788
2789 if (correct_index && eh->eh_entries)
2790 err = ext4_ext_correct_indexes(handle, inode, path);
2791
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002792 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002793 * If there's a partial cluster and at least one extent remains in
2794 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002795 * current extent. If it is shared with the current extent
2796 * we zero partial_cluster because we've reached the start of the
2797 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002798 */
Eric Whitney5bf43762014-11-23 00:58:11 -05002799 if (*partial_cluster > 0 && ex >= EXT_FIRST_EXTENT(eh)) {
2800 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
2801 if (*partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
2802 ext4_free_blocks(handle, inode, NULL,
2803 EXT4_C2B(sbi, *partial_cluster),
2804 sbi->s_cluster_ratio,
2805 get_default_free_blocks_flags(inode));
2806 }
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002807 *partial_cluster = 0;
2808 }
2809
Alex Tomasa86c6182006-10-11 01:21:03 -07002810 /* if this leaf is free, then we should
2811 * remove it from index block above */
2812 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002813 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002814
2815out:
2816 return err;
2817}
2818
2819/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002820 * ext4_ext_more_to_rm:
2821 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002822 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002823static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002824ext4_ext_more_to_rm(struct ext4_ext_path *path)
2825{
2826 BUG_ON(path->p_idx == NULL);
2827
2828 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2829 return 0;
2830
2831 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002832 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002833 * so we have to consider current index for truncation
2834 */
2835 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2836 return 0;
2837 return 1;
2838}
2839
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002840int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2841 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002842{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002843 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002844 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002845 struct ext4_ext_path *path = NULL;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002846 long long partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002847 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002848 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002849
Lukas Czerner5f95d212012-03-19 23:03:19 -04002850 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002851
2852 /* probably first extent we're gonna free will be last in block */
Theodore Ts'o9924a922013-02-08 21:59:22 -05002853 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002854 if (IS_ERR(handle))
2855 return PTR_ERR(handle);
2856
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002857again:
Lukas Czerner61801322013-05-27 23:32:35 -04002858 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002859
Alex Tomasa86c6182006-10-11 01:21:03 -07002860 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002861 * Check if we are removing extents inside the extent tree. If that
2862 * is the case, we are going to punch a hole inside the extent tree
2863 * so we have to check whether we need to split the extent covering
2864 * the last block to remove so we can easily remove the part of it
2865 * in ext4_ext_rm_leaf().
2866 */
2867 if (end < EXT_MAX_BLOCKS - 1) {
2868 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002869 ext4_lblk_t ee_block, ex_end, lblk;
2870 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002871
Eric Whitneyf4226d92014-11-23 00:55:42 -05002872 /* find extent for or closest extent to this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002873 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002874 if (IS_ERR(path)) {
2875 ext4_journal_stop(handle);
2876 return PTR_ERR(path);
2877 }
2878 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002879 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002880 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002881 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002882 if (depth) {
2883 EXT4_ERROR_INODE(inode,
2884 "path[%d].p_hdr == NULL",
2885 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002886 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002887 }
2888 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002889 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002890
2891 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05002892 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002893
2894 /*
2895 * See if the last block is inside the extent, if so split
2896 * the extent at 'end' block so we can easily remove the
2897 * tail of the first part of the split extent in
2898 * ext4_ext_rm_leaf().
2899 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002900 if (end >= ee_block && end < ex_end) {
2901
2902 /*
2903 * If we're going to split the extent, note that
2904 * the cluster containing the block after 'end' is
2905 * in use to avoid freeing it when removing blocks.
2906 */
2907 if (sbi->s_cluster_ratio > 1) {
2908 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
2909 partial_cluster =
2910 -(long long) EXT4_B2C(sbi, pblk);
2911 }
2912
Lukas Czerner5f95d212012-03-19 23:03:19 -04002913 /*
2914 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002915 * block in the first new extent. Also we should not
2916 * fail removing space due to ENOSPC so try to use
2917 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002918 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002919 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002920 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002921 if (err < 0)
2922 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002923
2924 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end) {
2925 /*
2926 * If there's an extent to the right its first cluster
2927 * contains the immediate right boundary of the
2928 * truncated/punched region. Set partial_cluster to
2929 * its negative value so it won't be freed if shared
2930 * with the current extent. The end < ee_block case
2931 * is handled in ext4_ext_rm_leaf().
2932 */
2933 lblk = ex_end + 1;
2934 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
2935 &ex);
2936 if (err)
2937 goto out;
2938 if (pblk)
2939 partial_cluster =
2940 -(long long) EXT4_B2C(sbi, pblk);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002941 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002942 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002943 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002944 * We start scanning from right side, freeing all the blocks
2945 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002946 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002947 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002948 if (path) {
2949 int k = i = depth;
2950 while (--k > 0)
2951 path[k].p_block =
2952 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2953 } else {
2954 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2955 GFP_NOFS);
2956 if (path == NULL) {
2957 ext4_journal_stop(handle);
2958 return -ENOMEM;
2959 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04002960 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002961 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002962 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002963
Theodore Ts'oc3491792013-08-16 21:21:41 -04002964 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002965 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002966 goto out;
2967 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002968 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002969 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002970
2971 while (i >= 0 && err == 0) {
2972 if (i == depth) {
2973 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002974 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002975 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002976 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002977 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002978 brelse(path[i].p_bh);
2979 path[i].p_bh = NULL;
2980 i--;
2981 continue;
2982 }
2983
2984 /* this is index block */
2985 if (!path[i].p_hdr) {
2986 ext_debug("initialize header\n");
2987 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002988 }
2989
Alex Tomasa86c6182006-10-11 01:21:03 -07002990 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002991 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002992 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2993 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2994 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2995 path[i].p_hdr,
2996 le16_to_cpu(path[i].p_hdr->eh_entries));
2997 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002998 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002999 path[i].p_idx--;
3000 }
3001
3002 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
3003 i, EXT_FIRST_INDEX(path[i].p_hdr),
3004 path[i].p_idx);
3005 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003006 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003007 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07003008 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04003009 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07003010 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003011 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04003012 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
3013 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003014 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003015 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04003016 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07003017 break;
3018 }
Theodore Ts'o76828c82013-07-15 12:27:47 -04003019 /* Yield here to deal with large extent trees.
3020 * Should be a no-op if we did IO above. */
3021 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003022 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003023 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003024 break;
3025 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003026 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003027
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003028 /* save actual number of indexes since this
3029 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07003030 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
3031 i++;
3032 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003033 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07003034 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003035 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07003036 * handle must be already prepared by the
3037 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05003038 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07003039 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003040 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003041 brelse(path[i].p_bh);
3042 path[i].p_bh = NULL;
3043 i--;
3044 ext_debug("return to level %d\n", i);
3045 }
3046 }
3047
Lukas Czerner61801322013-05-27 23:32:35 -04003048 trace_ext4_ext_remove_space_done(inode, start, end, depth,
3049 partial_cluster, path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04003050
Eric Whitney0756b902014-11-23 00:59:39 -05003051 /*
3052 * If we still have something in the partial cluster and we have removed
Aditya Kali7b415bf2011-09-09 19:04:51 -04003053 * even the first extent, then we should free the blocks in the partial
Eric Whitney0756b902014-11-23 00:59:39 -05003054 * cluster as well. (This code will only run when there are no leaves
3055 * to the immediate left of the truncated/punched region.)
3056 */
3057 if (partial_cluster > 0 && err == 0) {
3058 /* don't zero partial_cluster since it's not used afterwards */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003059 ext4_free_blocks(handle, inode, NULL,
Eric Whitneyf4226d92014-11-23 00:55:42 -05003060 EXT4_C2B(sbi, partial_cluster),
Eric Whitney0756b902014-11-23 00:59:39 -05003061 sbi->s_cluster_ratio,
3062 get_default_free_blocks_flags(inode));
Aditya Kali7b415bf2011-09-09 19:04:51 -04003063 }
3064
Alex Tomasa86c6182006-10-11 01:21:03 -07003065 /* TODO: flexible tree reduction should be here */
3066 if (path->p_hdr->eh_entries == 0) {
3067 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003068 * truncate to zero freed all the tree,
3069 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003070 */
3071 err = ext4_ext_get_access(handle, inode, path);
3072 if (err == 0) {
3073 ext_inode_hdr(inode)->eh_depth = 0;
3074 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003075 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003076 err = ext4_ext_dirty(handle, inode, path);
3077 }
3078 }
3079out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003080 ext4_ext_drop_refs(path);
3081 kfree(path);
3082 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003083 if (err == -EAGAIN)
3084 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003085 ext4_journal_stop(handle);
3086
3087 return err;
3088}
3089
3090/*
3091 * called at mount time
3092 */
3093void ext4_ext_init(struct super_block *sb)
3094{
3095 /*
3096 * possible initialization would be here
3097 */
3098
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003099 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003100#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003101 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003102#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003103 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003104#endif
3105#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003106 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003107#endif
3108#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003109 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003110#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003111 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003112#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003113#ifdef EXTENTS_STATS
3114 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3115 EXT4_SB(sb)->s_ext_min = 1 << 30;
3116 EXT4_SB(sb)->s_ext_max = 0;
3117#endif
3118 }
3119}
3120
3121/*
3122 * called at umount time
3123 */
3124void ext4_ext_release(struct super_block *sb)
3125{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003126 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003127 return;
3128
3129#ifdef EXTENTS_STATS
3130 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3131 struct ext4_sb_info *sbi = EXT4_SB(sb);
3132 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3133 sbi->s_ext_blocks, sbi->s_ext_extents,
3134 sbi->s_ext_blocks / sbi->s_ext_extents);
3135 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3136 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3137 }
3138#endif
3139}
3140
Zheng Liud7b2a002013-08-28 14:47:06 -04003141static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3142{
3143 ext4_lblk_t ee_block;
3144 ext4_fsblk_t ee_pblock;
3145 unsigned int ee_len;
3146
3147 ee_block = le32_to_cpu(ex->ee_block);
3148 ee_len = ext4_ext_get_actual_len(ex);
3149 ee_pblock = ext4_ext_pblock(ex);
3150
3151 if (ee_len == 0)
3152 return 0;
3153
3154 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3155 EXTENT_STATUS_WRITTEN);
3156}
3157
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003158/* FIXME!! we need to try to merge to left or right after zero-out */
3159static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3160{
Lukas Czerner24075182010-10-27 21:30:06 -04003161 ext4_fsblk_t ee_pblock;
3162 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003163
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003164 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003165 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003166 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3167 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003168}
3169
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003170/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003171 * ext4_split_extent_at() splits an extent at given block.
3172 *
3173 * @handle: the journal handle
3174 * @inode: the file inode
3175 * @path: the path to the extent
3176 * @split: the logical block where the extent is splitted.
3177 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003178 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003179 * @flags: flags used to insert new extent to extent tree.
3180 *
3181 *
3182 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3183 * of which are deterimined by split_flag.
3184 *
3185 * There are two cases:
3186 * a> the extent are splitted into two extent.
3187 * b> split is not needed, and just mark the extent.
3188 *
3189 * return 0 on success.
3190 */
3191static int ext4_split_extent_at(handle_t *handle,
3192 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003193 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003194 ext4_lblk_t split,
3195 int split_flag,
3196 int flags)
3197{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003198 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003199 ext4_fsblk_t newblock;
3200 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003201 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003202 struct ext4_extent *ex2 = NULL;
3203 unsigned int ee_len, depth;
3204 int err = 0;
3205
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003206 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3207 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3208
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003209 ext_debug("ext4_split_extents_at: inode %lu, logical"
3210 "block %llu\n", inode->i_ino, (unsigned long long)split);
3211
3212 ext4_ext_show_leaf(inode, path);
3213
3214 depth = ext_depth(inode);
3215 ex = path[depth].p_ext;
3216 ee_block = le32_to_cpu(ex->ee_block);
3217 ee_len = ext4_ext_get_actual_len(ex);
3218 newblock = split - ee_block + ext4_ext_pblock(ex);
3219
3220 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003221 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003222 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003223 EXT4_EXT_MARK_UNWRIT1 |
3224 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003225
3226 err = ext4_ext_get_access(handle, inode, path + depth);
3227 if (err)
3228 goto out;
3229
3230 if (split == ee_block) {
3231 /*
3232 * case b: block @split is the block that the extent begins with
3233 * then we just change the state of the extent, and splitting
3234 * is not needed.
3235 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003236 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3237 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003238 else
3239 ext4_ext_mark_initialized(ex);
3240
3241 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003242 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003243
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003244 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003245 goto out;
3246 }
3247
3248 /* case a */
3249 memcpy(&orig_ex, ex, sizeof(orig_ex));
3250 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003251 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3252 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003253
3254 /*
3255 * path may lead to new leaf, not to original leaf any more
3256 * after ext4_ext_insert_extent() returns,
3257 */
3258 err = ext4_ext_dirty(handle, inode, path + depth);
3259 if (err)
3260 goto fix_extent_len;
3261
3262 ex2 = &newex;
3263 ex2->ee_block = cpu_to_le32(split);
3264 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3265 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003266 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3267 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003268
Theodore Ts'odfe50802014-09-01 14:37:09 -04003269 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003270 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003271 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003272 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003273 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003274 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003275 zero_ex.ee_len = cpu_to_le16(
3276 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003277 ext4_ext_store_pblock(&zero_ex,
3278 ext4_ext_pblock(ex2));
3279 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003280 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003281 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003282 zero_ex.ee_len = cpu_to_le16(
3283 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003284 ext4_ext_store_pblock(&zero_ex,
3285 ext4_ext_pblock(ex));
3286 }
3287 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003288 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003289 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003290 zero_ex.ee_len = cpu_to_le16(
3291 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003292 ext4_ext_store_pblock(&zero_ex,
3293 ext4_ext_pblock(&orig_ex));
3294 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003295
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003296 if (err)
3297 goto fix_extent_len;
3298 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003299 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003300 ext4_ext_try_to_merge(handle, inode, path, ex);
3301 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003302 if (err)
3303 goto fix_extent_len;
3304
3305 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003306 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003307
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003308 goto out;
3309 } else if (err)
3310 goto fix_extent_len;
3311
3312out:
3313 ext4_ext_show_leaf(inode, path);
3314 return err;
3315
3316fix_extent_len:
3317 ex->ee_len = orig_ex.ee_len;
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003318 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003319 return err;
3320}
3321
3322/*
3323 * ext4_split_extents() splits an extent and mark extent which is covered
3324 * by @map as split_flags indicates
3325 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003326 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003327 * There are three possibilities:
3328 * a> There is no split required
3329 * b> Splits in two extents: Split is happening at either end of the extent
3330 * c> Splits in three extents: Somone is splitting in middle of the extent
3331 *
3332 */
3333static int ext4_split_extent(handle_t *handle,
3334 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003335 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003336 struct ext4_map_blocks *map,
3337 int split_flag,
3338 int flags)
3339{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003340 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003341 ext4_lblk_t ee_block;
3342 struct ext4_extent *ex;
3343 unsigned int ee_len, depth;
3344 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003345 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003346 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003347 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003348
3349 depth = ext_depth(inode);
3350 ex = path[depth].p_ext;
3351 ee_block = le32_to_cpu(ex->ee_block);
3352 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003353 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003354
3355 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003356 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003357 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003358 if (unwritten)
3359 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3360 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003361 if (split_flag & EXT4_EXT_DATA_VALID2)
3362 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003363 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003364 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003365 if (err)
3366 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003367 } else {
3368 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003369 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003370 /*
3371 * Update path is required because previous ext4_split_extent_at() may
3372 * result in split of original leaf or extent zeroout.
3373 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003374 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003375 if (IS_ERR(path))
3376 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003377 depth = ext_depth(inode);
3378 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003379 if (!ex) {
3380 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3381 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003382 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003383 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003384 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003385 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003386
3387 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003388 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003389 if (unwritten) {
3390 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003391 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003392 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003393 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003394 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003395 map->m_lblk, split_flag1, flags);
3396 if (err)
3397 goto out;
3398 }
3399
3400 ext4_ext_show_leaf(inode, path);
3401out:
Zheng Liu3a225672013-03-10 21:20:23 -04003402 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003403}
3404
Amit Arora56055d32007-07-17 21:42:38 -04003405/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003406 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003407 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003408 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003409 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003410 * There are three possibilities:
3411 * a> There is no split required: Entire extent should be initialized
3412 * b> Splits in two extents: Write is happening at either end of the extent
3413 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003414 *
3415 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003416 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003417 * - The extent pointed to by 'path' contains a superset
3418 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3419 *
3420 * Post-conditions on success:
3421 * - the returned value is the number of blocks beyond map->l_lblk
3422 * that are allocated and initialized.
3423 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003424 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003425static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003426 struct inode *inode,
3427 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003428 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003429 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003430{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003431 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003432 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003433 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003434 struct ext4_map_blocks split_map;
Jan Kara2e169212017-05-26 17:40:52 -04003435 struct ext4_extent zero_ex1, zero_ex2;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003436 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003437 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003438 unsigned int ee_len, depth, map_len = map->m_len;
3439 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003440 int err = 0;
Jan Kara2e169212017-05-26 17:40:52 -04003441 int split_flag = EXT4_EXT_DATA_VALID2;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003442
3443 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3444 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003445 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003446
Zheng Liu67a5da52012-08-17 09:54:17 -04003447 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003448 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3449 inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003450 if (eof_block < map->m_lblk + map_len)
3451 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003452
3453 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003454 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003455 ex = path[depth].p_ext;
3456 ee_block = le32_to_cpu(ex->ee_block);
3457 ee_len = ext4_ext_get_actual_len(ex);
Jan Kara2e169212017-05-26 17:40:52 -04003458 zero_ex1.ee_len = 0;
3459 zero_ex2.ee_len = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003460
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003461 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3462
3463 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003464 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003465 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003466
3467 /*
3468 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003469 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003470 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003471 * memmove() calls. Transferring to the left is the common case in
3472 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3473 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003474 *
3475 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003476 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003477 * This would require removing the extent if the transfer
3478 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003479 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003480 * same extent tree node.
3481 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003482 if ((map->m_lblk == ee_block) &&
3483 /* See if we can merge left */
3484 (map_len < ee_len) && /*L1*/
3485 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003486 ext4_lblk_t prev_lblk;
3487 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003488 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003489
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003490 abut_ex = ex - 1;
3491 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3492 prev_len = ext4_ext_get_actual_len(abut_ex);
3493 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003494 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003495
3496 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003497 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003498 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003499 * - C1: abut_ex is initialized,
3500 * - C2: abut_ex is logically abutting ex,
3501 * - C3: abut_ex is physically abutting ex,
3502 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003503 * overflowing the (initialized) length limit.
3504 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003505 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003506 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3507 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003508 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003509 err = ext4_ext_get_access(handle, inode, path + depth);
3510 if (err)
3511 goto out;
3512
3513 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003514 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003515
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003516 /* Shift the start of ex by 'map_len' blocks */
3517 ex->ee_block = cpu_to_le32(ee_block + map_len);
3518 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3519 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003520 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003521
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003522 /* Extend abut_ex by 'map_len' blocks */
3523 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003524
3525 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003526 allocated = map_len;
3527 }
3528 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3529 (map_len < ee_len) && /*L1*/
3530 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3531 /* See if we can merge right */
3532 ext4_lblk_t next_lblk;
3533 ext4_fsblk_t next_pblk, ee_pblk;
3534 unsigned int next_len;
3535
3536 abut_ex = ex + 1;
3537 next_lblk = le32_to_cpu(abut_ex->ee_block);
3538 next_len = ext4_ext_get_actual_len(abut_ex);
3539 next_pblk = ext4_ext_pblock(abut_ex);
3540 ee_pblk = ext4_ext_pblock(ex);
3541
3542 /*
3543 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3544 * upon those conditions:
3545 * - C1: abut_ex is initialized,
3546 * - C2: abut_ex is logically abutting ex,
3547 * - C3: abut_ex is physically abutting ex,
3548 * - C4: abut_ex can receive the additional blocks without
3549 * overflowing the (initialized) length limit.
3550 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003551 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003552 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3553 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3554 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3555 err = ext4_ext_get_access(handle, inode, path + depth);
3556 if (err)
3557 goto out;
3558
3559 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3560 map, ex, abut_ex);
3561
3562 /* Shift the start of abut_ex by 'map_len' blocks */
3563 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3564 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3565 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003566 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003567
3568 /* Extend abut_ex by 'map_len' blocks */
3569 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3570
3571 /* Result: number of initialized blocks past m_lblk */
3572 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003573 }
3574 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003575 if (allocated) {
3576 /* Mark the block containing both extents as dirty */
3577 ext4_ext_dirty(handle, inode, path + depth);
3578
3579 /* Update path to point to the right extent */
3580 path[depth].p_ext = abut_ex;
3581 goto out;
3582 } else
3583 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003584
Yongqiang Yang667eff32011-05-03 12:25:07 -04003585 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003586 /*
3587 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003588 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003589 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003590 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003591
Zheng Liu67a5da52012-08-17 09:54:17 -04003592 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3593 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003594 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003595
Theodore Ts'o36086d42015-10-03 10:49:29 -04003596 if (ext4_encrypted_inode(inode))
3597 max_zeroout = 0;
3598
Amit Arora56055d32007-07-17 21:42:38 -04003599 /*
Jan Kara2e169212017-05-26 17:40:52 -04003600 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003601 * 1. split the extent into three extents.
Jan Kara2e169212017-05-26 17:40:52 -04003602 * 2. split the extent into two extents, zeroout the head of the first
3603 * extent.
3604 * 3. split the extent into two extents, zeroout the tail of the second
3605 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003606 * 4. split the extent into two extents with out zeroout.
Jan Kara2e169212017-05-26 17:40:52 -04003607 * 5. no splitting needed, just possibly zeroout the head and / or the
3608 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003609 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003610 split_map.m_lblk = map->m_lblk;
3611 split_map.m_len = map->m_len;
3612
Jan Kara2e169212017-05-26 17:40:52 -04003613 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003614 if (allocated <= max_zeroout) {
Jan Kara2e169212017-05-26 17:40:52 -04003615 /* case 3 or 5 */
3616 zero_ex1.ee_block =
3617 cpu_to_le32(split_map.m_lblk +
3618 split_map.m_len);
3619 zero_ex1.ee_len =
3620 cpu_to_le16(allocated - split_map.m_len);
3621 ext4_ext_store_pblock(&zero_ex1,
3622 ext4_ext_pblock(ex) + split_map.m_lblk +
3623 split_map.m_len - ee_block);
3624 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003625 if (err)
3626 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003627 split_map.m_len = allocated;
Jan Kara2e169212017-05-26 17:40:52 -04003628 }
3629 if (split_map.m_lblk - ee_block + split_map.m_len <
3630 max_zeroout) {
3631 /* case 2 or 5 */
3632 if (split_map.m_lblk != ee_block) {
3633 zero_ex2.ee_block = ex->ee_block;
3634 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003635 ee_block);
Jan Kara2e169212017-05-26 17:40:52 -04003636 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003637 ext4_ext_pblock(ex));
Jan Kara2e169212017-05-26 17:40:52 -04003638 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003639 if (err)
3640 goto out;
3641 }
3642
Jan Kara2e169212017-05-26 17:40:52 -04003643 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003644 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003645 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003646 }
3647 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003648
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003649 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3650 flags);
3651 if (err > 0)
3652 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003653out:
Zheng Liuadb23552013-03-10 21:13:05 -04003654 /* If we have gotten a failure, don't zero out status tree */
Jan Kara2e169212017-05-26 17:40:52 -04003655 if (!err) {
3656 err = ext4_zeroout_es(inode, &zero_ex1);
3657 if (!err)
3658 err = ext4_zeroout_es(inode, &zero_ex2);
3659 }
Amit Arora56055d32007-07-17 21:42:38 -04003660 return err ? err : allocated;
3661}
3662
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003663/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003664 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003665 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003666 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003667 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003668 * Writing to an unwritten extent may result in splitting the unwritten
3669 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003670 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003671 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003672 * b> Splits in two extents: Write is happening at either end of the extent
3673 * c> Splits in three extents: Somone is writing in middle of the extent
3674 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003675 * This works the same way in the case of initialized -> unwritten conversion.
3676 *
Mingming Cao00314622009-09-28 15:49:08 -04003677 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003678 * the unwritten extent split. To prevent ENOSPC occur at the IO
3679 * complete, we need to split the unwritten extent before DIO submit
3680 * the IO. The unwritten extent called at this time will be split
3681 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003682 * being filled will be convert to initialized by the end_io callback function
3683 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003684 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003685 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003686 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003687static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003688 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003689 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003690 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003691 int flags)
3692{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003693 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003694 ext4_lblk_t eof_block;
3695 ext4_lblk_t ee_block;
3696 struct ext4_extent *ex;
3697 unsigned int ee_len;
3698 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003699
Lukas Czernerb8a86842014-03-18 18:05:35 -04003700 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3701 __func__, inode->i_ino,
3702 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003703
3704 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3705 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003706 if (eof_block < map->m_lblk + map->m_len)
3707 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003708 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003709 * It is safe to convert extent to initialized via explicit
3710 * zeroout only if extent is fully insde i_size or new_size.
3711 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003712 depth = ext_depth(inode);
3713 ex = path[depth].p_ext;
3714 ee_block = le32_to_cpu(ex->ee_block);
3715 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003716
Lukas Czernerb8a86842014-03-18 18:05:35 -04003717 /* Convert to unwritten */
3718 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3719 split_flag |= EXT4_EXT_DATA_VALID1;
3720 /* Convert to initialized */
3721 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3722 split_flag |= ee_block + ee_len <= eof_block ?
3723 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003724 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003725 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003726 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003727 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003728}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003729
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003730static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003731 struct inode *inode,
3732 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003733 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003734{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003735 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003736 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003737 ext4_lblk_t ee_block;
3738 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003739 int depth;
3740 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003741
3742 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003743 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003744 ee_block = le32_to_cpu(ex->ee_block);
3745 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003746
Yongqiang Yang197217a2011-05-03 11:45:29 -04003747 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3748 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003749 (unsigned long long)ee_block, ee_len);
3750
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003751 /* If extent is larger than requested it is a clear sign that we still
3752 * have some extent state machine issues left. So extent_split is still
3753 * required.
3754 * TODO: Once all related issues will be fixed this situation should be
3755 * illegal.
3756 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003757 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003758#ifdef EXT4_DEBUG
3759 ext4_warning("Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003760 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003761 inode->i_ino, (unsigned long long)ee_block, ee_len,
3762 (unsigned long long)map->m_lblk, map->m_len);
3763#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003764 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003765 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003766 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003767 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003768 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003769 if (IS_ERR(path))
3770 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003771 depth = ext_depth(inode);
3772 ex = path[depth].p_ext;
3773 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003774
Mingming Cao00314622009-09-28 15:49:08 -04003775 err = ext4_ext_get_access(handle, inode, path + depth);
3776 if (err)
3777 goto out;
3778 /* first mark the extent as initialized */
3779 ext4_ext_mark_initialized(ex);
3780
Yongqiang Yang197217a2011-05-03 11:45:29 -04003781 /* note: ext4_ext_correct_indexes() isn't needed here because
3782 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003783 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003784 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003785
Mingming Cao00314622009-09-28 15:49:08 -04003786 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003787 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003788out:
3789 ext4_ext_show_leaf(inode, path);
3790 return err;
3791}
3792
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003793static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3794 sector_t block, int count)
3795{
3796 int i;
3797 for (i = 0; i < count; i++)
3798 unmap_underlying_metadata(bdev, block + i);
3799}
3800
Theodore Ts'o58590b02010-10-27 21:23:12 -04003801/*
3802 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3803 */
3804static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003805 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003806 struct ext4_ext_path *path,
3807 unsigned int len)
3808{
3809 int i, depth;
3810 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003811 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003812
3813 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3814 return 0;
3815
3816 depth = ext_depth(inode);
3817 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003818
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003819 /*
3820 * We're going to remove EOFBLOCKS_FL entirely in future so we
3821 * do not care for this case anymore. Simply remove the flag
3822 * if there are no extents.
3823 */
3824 if (unlikely(!eh->eh_entries))
3825 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003826 last_ex = EXT_LAST_EXTENT(eh);
3827 /*
3828 * We should clear the EOFBLOCKS_FL flag if we are writing the
3829 * last block in the last extent in the file. We test this by
3830 * first checking to see if the caller to
3831 * ext4_ext_get_blocks() was interested in the last block (or
3832 * a block beyond the last block) in the current extent. If
3833 * this turns out to be false, we can bail out from this
3834 * function immediately.
3835 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003836 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003837 ext4_ext_get_actual_len(last_ex))
3838 return 0;
3839 /*
3840 * If the caller does appear to be planning to write at or
3841 * beyond the end of the current extent, we then test to see
3842 * if the current extent is the last extent in the file, by
3843 * checking to make sure it was reached via the rightmost node
3844 * at each level of the tree.
3845 */
3846 for (i = depth-1; i >= 0; i--)
3847 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3848 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003849out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003850 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3851 return ext4_mark_inode_dirty(handle, inode);
3852}
3853
Aditya Kali7b415bf2011-09-09 19:04:51 -04003854/**
3855 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3856 *
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003857 * Return 1 if there is a delalloc block in the range, otherwise 0.
Aditya Kali7b415bf2011-09-09 19:04:51 -04003858 */
Zheng Liuf7fec032013-02-18 00:28:47 -05003859int ext4_find_delalloc_range(struct inode *inode,
3860 ext4_lblk_t lblk_start,
3861 ext4_lblk_t lblk_end)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003862{
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003863 struct extent_status es;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003864
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04003865 ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es);
Zheng Liu06b0c882013-02-18 00:26:51 -05003866 if (es.es_len == 0)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003867 return 0; /* there is no delay extent in this tree */
Zheng Liu06b0c882013-02-18 00:26:51 -05003868 else if (es.es_lblk <= lblk_start &&
3869 lblk_start < es.es_lblk + es.es_len)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003870 return 1;
Zheng Liu06b0c882013-02-18 00:26:51 -05003871 else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003872 return 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003873 else
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003874 return 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003875}
3876
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003877int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003878{
3879 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3880 ext4_lblk_t lblk_start, lblk_end;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003881 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003882 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3883
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003884 return ext4_find_delalloc_range(inode, lblk_start, lblk_end);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003885}
3886
3887/**
3888 * Determines how many complete clusters (out of those specified by the 'map')
3889 * are under delalloc and were reserved quota for.
3890 * This function is called when we are writing out the blocks that were
3891 * originally written with their allocation delayed, but then the space was
3892 * allocated using fallocate() before the delayed allocation could be resolved.
3893 * The cases to look for are:
3894 * ('=' indicated delayed allocated blocks
3895 * '-' indicates non-delayed allocated blocks)
3896 * (a) partial clusters towards beginning and/or end outside of allocated range
3897 * are not delalloc'ed.
3898 * Ex:
3899 * |----c---=|====c====|====c====|===-c----|
3900 * |++++++ allocated ++++++|
3901 * ==> 4 complete clusters in above example
3902 *
3903 * (b) partial cluster (outside of allocated range) towards either end is
3904 * marked for delayed allocation. In this case, we will exclude that
3905 * cluster.
3906 * Ex:
3907 * |----====c========|========c========|
3908 * |++++++ allocated ++++++|
3909 * ==> 1 complete clusters in above example
3910 *
3911 * Ex:
3912 * |================c================|
3913 * |++++++ allocated ++++++|
3914 * ==> 0 complete clusters in above example
3915 *
3916 * The ext4_da_update_reserve_space will be called only if we
3917 * determine here that there were some "entire" clusters that span
3918 * this 'allocated' range.
3919 * In the non-bigalloc case, this function will just end up returning num_blks
3920 * without ever calling ext4_find_delalloc_range.
3921 */
3922static unsigned int
3923get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3924 unsigned int num_blks)
3925{
3926 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3927 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3928 ext4_lblk_t lblk_from, lblk_to, c_offset;
3929 unsigned int allocated_clusters = 0;
3930
3931 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3932 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3933
3934 /* max possible clusters for this allocation */
3935 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3936
Aditya Kalid8990242011-09-09 19:18:51 -04003937 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3938
Aditya Kali7b415bf2011-09-09 19:04:51 -04003939 /* Check towards left side */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003940 c_offset = EXT4_LBLK_COFF(sbi, lblk_start);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003941 if (c_offset) {
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003942 lblk_from = EXT4_LBLK_CMASK(sbi, lblk_start);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003943 lblk_to = lblk_from + c_offset - 1;
3944
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003945 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003946 allocated_clusters--;
3947 }
3948
3949 /* Now check towards right. */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003950 c_offset = EXT4_LBLK_COFF(sbi, lblk_start + num_blks);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003951 if (allocated_clusters && c_offset) {
3952 lblk_from = lblk_start + num_blks;
3953 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3954
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003955 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003956 allocated_clusters--;
3957 }
3958
3959 return allocated_clusters;
3960}
3961
Mingming Cao00314622009-09-28 15:49:08 -04003962static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003963convert_initialized_extent(handle_t *handle, struct inode *inode,
3964 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003965 struct ext4_ext_path **ppath,
Eryu Guan56263b42016-02-12 01:23:00 -05003966 unsigned int allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003967{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003968 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003969 struct ext4_extent *ex;
3970 ext4_lblk_t ee_block;
3971 unsigned int ee_len;
3972 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003973 int err = 0;
3974
3975 /*
3976 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003977 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003978 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003979 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3980 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003981
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003982 depth = ext_depth(inode);
3983 ex = path[depth].p_ext;
3984 ee_block = le32_to_cpu(ex->ee_block);
3985 ee_len = ext4_ext_get_actual_len(ex);
3986
3987 ext_debug("%s: inode %lu, logical"
3988 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3989 (unsigned long long)ee_block, ee_len);
3990
3991 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003992 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003993 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3994 if (err < 0)
3995 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003996 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003997 if (IS_ERR(path))
3998 return PTR_ERR(path);
3999 depth = ext_depth(inode);
4000 ex = path[depth].p_ext;
4001 if (!ex) {
4002 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
4003 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004004 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004005 }
4006 }
4007
4008 err = ext4_ext_get_access(handle, inode, path + depth);
4009 if (err)
4010 return err;
4011 /* first mark the extent as unwritten */
4012 ext4_ext_mark_unwritten(ex);
4013
4014 /* note: ext4_ext_correct_indexes() isn't needed here because
4015 * borders are not changed
4016 */
4017 ext4_ext_try_to_merge(handle, inode, path, ex);
4018
4019 /* Mark modified extent as dirty */
4020 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
4021 if (err)
4022 return err;
4023 ext4_ext_show_leaf(inode, path);
4024
4025 ext4_update_inode_fsync_trans(handle, inode, 1);
4026 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
4027 if (err)
4028 return err;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004029 map->m_flags |= EXT4_MAP_UNWRITTEN;
4030 if (allocated > map->m_len)
4031 allocated = map->m_len;
4032 map->m_len = allocated;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004033 return allocated;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004034}
4035
4036static int
Lukas Czerner556615d2014-04-20 23:45:47 -04004037ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004038 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004039 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004040 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04004041{
Theodore Ts'odfe50802014-09-01 14:37:09 -04004042 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04004043 int ret = 0;
4044 int err = 0;
4045
Lukas Czerner556615d2014-04-20 23:45:47 -04004046 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05004047 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004048 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04004049 flags, allocated);
4050 ext4_ext_show_leaf(inode, path);
4051
Lukas Czerner27dd4382013-04-09 22:11:22 -04004052 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004053 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04004054 * allocate metadata blocks for the new extent block if needed.
4055 */
4056 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4057
Lukas Czerner556615d2014-04-20 23:45:47 -04004058 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05004059 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04004060
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004061 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004062 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004063 ret = ext4_split_convert_extents(handle, inode, map, ppath,
4064 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004065 if (ret <= 0)
4066 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004067 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004068 goto out;
4069 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004070 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004071 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05004072 if (flags & EXT4_GET_BLOCKS_ZERO) {
4073 if (allocated > map->m_len)
4074 allocated = map->m_len;
4075 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
4076 allocated);
4077 if (err < 0)
4078 goto out2;
4079 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04004080 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004081 ppath);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004082 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05004083 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05004084 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4085 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004086 } else
4087 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04004088 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05004089 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04004090 if (allocated > map->m_len)
4091 allocated = map->m_len;
4092 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004093 goto out2;
4094 }
4095 /* buffered IO case */
4096 /*
4097 * repeat fallocate creation request
4098 * we already have an unwritten extent
4099 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004100 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05004101 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004102 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004103 }
Mingming Cao00314622009-09-28 15:49:08 -04004104
4105 /* buffered READ or buffered write_begin() lookup */
4106 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4107 /*
4108 * We have blocks reserved already. We
4109 * return allocated blocks so that delalloc
4110 * won't do block reservation for us. But
4111 * the buffer head will be unmapped so that
4112 * a read from the block returns 0s.
4113 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004114 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004115 goto out1;
4116 }
4117
4118 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04004119 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004120 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004121 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04004122out:
4123 if (ret <= 0) {
4124 err = ret;
4125 goto out2;
4126 } else
4127 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004128 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004129 /*
4130 * if we allocated more blocks than requested
4131 * we need to make sure we unmap the extra block
4132 * allocated. The actual needed block will get
4133 * unmapped later when we find the buffer_head marked
4134 * new.
4135 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004136 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004137 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004138 newblock + map->m_len,
4139 allocated - map->m_len);
4140 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004141 }
Zheng Liu3a225672013-03-10 21:20:23 -04004142 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004143
4144 /*
4145 * If we have done fallocate with the offset that is already
4146 * delayed allocated, we would have block reservation
4147 * and quota reservation done in the delayed write path.
4148 * But fallocate would have already updated quota and block
4149 * count for this offset. So cancel these reservation
4150 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004151 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4152 unsigned int reserved_clusters;
4153 reserved_clusters = get_reserved_cluster_alloc(inode,
4154 map->m_lblk, map->m_len);
4155 if (reserved_clusters)
4156 ext4_da_update_reserve_space(inode,
4157 reserved_clusters,
4158 0);
4159 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004160
Mingming Cao00314622009-09-28 15:49:08 -04004161map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004162 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004163 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4164 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4165 map->m_len);
4166 if (err < 0)
4167 goto out2;
4168 }
Mingming Cao00314622009-09-28 15:49:08 -04004169out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004170 if (allocated > map->m_len)
4171 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04004172 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004173 map->m_pblk = newblock;
4174 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004175out2:
Mingming Cao00314622009-09-28 15:49:08 -04004176 return err ? err : allocated;
4177}
Theodore Ts'o58590b02010-10-27 21:23:12 -04004178
Mingming Cao00314622009-09-28 15:49:08 -04004179/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004180 * get_implied_cluster_alloc - check to see if the requested
4181 * allocation (in the map structure) overlaps with a cluster already
4182 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04004183 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004184 * @map The requested lblk->pblk mapping
4185 * @ex The extent structure which might contain an implied
4186 * cluster allocation
4187 *
4188 * This function is called by ext4_ext_map_blocks() after we failed to
4189 * find blocks that were already in the inode's extent tree. Hence,
4190 * we know that the beginning of the requested region cannot overlap
4191 * the extent from the inode's extent tree. There are three cases we
4192 * want to catch. The first is this case:
4193 *
4194 * |--- cluster # N--|
4195 * |--- extent ---| |---- requested region ---|
4196 * |==========|
4197 *
4198 * The second case that we need to test for is this one:
4199 *
4200 * |--------- cluster # N ----------------|
4201 * |--- requested region --| |------- extent ----|
4202 * |=======================|
4203 *
4204 * The third case is when the requested region lies between two extents
4205 * within the same cluster:
4206 * |------------- cluster # N-------------|
4207 * |----- ex -----| |---- ex_right ----|
4208 * |------ requested region ------|
4209 * |================|
4210 *
4211 * In each of the above cases, we need to set the map->m_pblk and
4212 * map->m_len so it corresponds to the return the extent labelled as
4213 * "|====|" from cluster #N, since it is already in use for data in
4214 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4215 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4216 * as a new "allocated" block region. Otherwise, we will return 0 and
4217 * ext4_ext_map_blocks() will then allocate one or more new clusters
4218 * by calling ext4_mb_new_blocks().
4219 */
Aditya Kalid8990242011-09-09 19:18:51 -04004220static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004221 struct ext4_map_blocks *map,
4222 struct ext4_extent *ex,
4223 struct ext4_ext_path *path)
4224{
Aditya Kalid8990242011-09-09 19:18:51 -04004225 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004226 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004227 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05004228 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004229 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4230 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4231 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4232
4233 /* The extent passed in that we are trying to match */
4234 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4235 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4236
4237 /* The requested region passed into ext4_map_blocks() */
4238 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004239
4240 if ((rr_cluster_start == ex_cluster_end) ||
4241 (rr_cluster_start == ex_cluster_start)) {
4242 if (rr_cluster_start == ex_cluster_end)
4243 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004244 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004245 map->m_len = min(map->m_len,
4246 (unsigned) sbi->s_cluster_ratio - c_offset);
4247 /*
4248 * Check for and handle this case:
4249 *
4250 * |--------- cluster # N-------------|
4251 * |------- extent ----|
4252 * |--- requested region ---|
4253 * |===========|
4254 */
4255
4256 if (map->m_lblk < ee_block)
4257 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4258
4259 /*
4260 * Check for the case where there is already another allocated
4261 * block to the right of 'ex' but before the end of the cluster.
4262 *
4263 * |------------- cluster # N-------------|
4264 * |----- ex -----| |---- ex_right ----|
4265 * |------ requested region ------|
4266 * |================|
4267 */
4268 if (map->m_lblk > ee_block) {
4269 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4270 map->m_len = min(map->m_len, next - map->m_lblk);
4271 }
Aditya Kalid8990242011-09-09 19:18:51 -04004272
4273 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004274 return 1;
4275 }
Aditya Kalid8990242011-09-09 19:18:51 -04004276
4277 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004278 return 0;
4279}
4280
4281
4282/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004283 * Block allocation/map/preallocation routine for extents based files
4284 *
4285 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004286 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004287 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4288 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004289 *
4290 * return > 0, number of of blocks already mapped/allocated
4291 * if create == 0 and these are pre-allocated blocks
4292 * buffer head is unmapped
4293 * otherwise blocks are mapped
4294 *
4295 * return = 0, if plain look up failed (blocks have not been allocated)
4296 * buffer head is unmapped
4297 *
4298 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004299 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004300int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4301 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004302{
4303 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004304 struct ext4_extent newex, *ex, *ex2;
4305 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004306 ext4_fsblk_t newblock = 0;
Eric Whitneyce37c422014-02-19 18:52:39 -05004307 int free_on_err = 0, err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004308 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004309 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004310 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004311 ext4_lblk_t cluster_offset;
Jan Karacbd75842014-11-25 11:41:49 -05004312 bool map_from_cluster = false;
Alex Tomasa86c6182006-10-11 01:21:03 -07004313
Mingming84fe3be2009-09-01 08:44:37 -04004314 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004315 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004316 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004317
Alex Tomasa86c6182006-10-11 01:21:03 -07004318 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004319 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004320 if (IS_ERR(path)) {
4321 err = PTR_ERR(path);
4322 path = NULL;
4323 goto out2;
4324 }
4325
4326 depth = ext_depth(inode);
4327
4328 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004329 * consistent leaf must not be empty;
4330 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004331 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004332 */
Frank Mayhar273df552010-03-02 11:46:09 -05004333 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4334 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004335 "lblock: %lu, depth: %d pblock %lld",
4336 (unsigned long) map->m_lblk, depth,
4337 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004338 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004339 goto out2;
4340 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004341
Avantika Mathur7e028972006-12-06 20:41:33 -08004342 ex = path[depth].p_ext;
4343 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004344 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004345 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004346 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004347
Lukas Czernerb8a86842014-03-18 18:05:35 -04004348
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004349 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004350 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004351 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004352 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004353 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004354
4355 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4356
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004357 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004358 if (in_range(map->m_lblk, ee_block, ee_len)) {
4359 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004360 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004361 allocated = ee_len - (map->m_lblk - ee_block);
4362 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4363 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004364
Lukas Czernerb8a86842014-03-18 18:05:35 -04004365 /*
4366 * If the extent is initialized check whether the
4367 * caller wants to convert it to unwritten.
4368 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004369 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004370 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004371 allocated = convert_initialized_extent(
Theodore Ts'o4f224b82014-09-01 14:36:09 -04004372 handle, inode, map, &path,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05004373 allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004374 goto out2;
Lukas Czerner556615d2014-04-20 23:45:47 -04004375 } else if (!ext4_ext_is_unwritten(ex))
Lukas Czerner78771912012-03-19 23:05:43 -04004376 goto out;
Zheng Liu69eb33d2013-02-18 00:31:07 -05004377
Lukas Czerner556615d2014-04-20 23:45:47 -04004378 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004379 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004380 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004381 if (ret < 0)
4382 err = ret;
4383 else
4384 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004385 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004386 }
4387 }
4388
4389 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004390 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004391 * we couldn't try to create block if create flag is zero
4392 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004393 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004394 ext4_lblk_t hole_start, hole_len;
4395
Jan Karafacab4d2016-03-09 22:54:00 -05004396 hole_start = map->m_lblk;
4397 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004398 /*
4399 * put just found gap into cache to speed up
4400 * subsequent requests
4401 */
Jan Kara140a5252016-03-09 22:46:57 -05004402 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004403
4404 /* Update hole_len to reflect hole size after map->m_lblk */
4405 if (hole_start != map->m_lblk)
4406 hole_len -= map->m_lblk - hole_start;
4407 map->m_pblk = 0;
4408 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4409
Alex Tomasa86c6182006-10-11 01:21:03 -07004410 goto out2;
4411 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004412
Alex Tomasa86c6182006-10-11 01:21:03 -07004413 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004414 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004415 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004416 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004417 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004418
4419 /*
4420 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004421 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004422 */
4423 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004424 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004425 ar.len = allocated = map->m_len;
4426 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004427 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004428 goto got_allocated_blocks;
4429 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004430
Alex Tomasc9de5602008-01-29 00:19:52 -05004431 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004432 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004433 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4434 if (err)
4435 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004436 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004437 ex2 = NULL;
4438 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004439 if (err)
4440 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004441
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004442 /* Check if the extent after searching to the right implies a
4443 * cluster we can use. */
4444 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004445 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004446 ar.len = allocated = map->m_len;
4447 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004448 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004449 goto got_allocated_blocks;
4450 }
4451
Amit Arora749269f2007-07-18 09:02:56 -04004452 /*
4453 * See if request is beyond maximum number of blocks we can have in
4454 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004455 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4456 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004457 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004458 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004459 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004460 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004461 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4462 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4463 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004464
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004465 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004466 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004467 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004468 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004469 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004470 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004471 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004472
4473 /* allocate new block */
4474 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004475 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4476 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004477 /*
4478 * We calculate the offset from the beginning of the cluster
4479 * for the logical block number, since when we allocate a
4480 * physical cluster, the physical block should start at the
4481 * same offset from the beginning of the cluster. This is
4482 * needed so that future calls to get_implied_cluster_alloc()
4483 * work correctly.
4484 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004485 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004486 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4487 ar.goal -= offset;
4488 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004489 if (S_ISREG(inode->i_mode))
4490 ar.flags = EXT4_MB_HINT_DATA;
4491 else
4492 /* disable in-core preallocation for non-regular files */
4493 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004494 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4495 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004496 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4497 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004498 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4499 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004500 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004501 if (!newblock)
4502 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004503 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004504 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004505 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004506 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004507 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4508 if (ar.len > allocated)
4509 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004510
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004511got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004512 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004513 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004514 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004515 /* Mark unwritten */
4516 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4517 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004518 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004519 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004520
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004521 err = 0;
4522 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4523 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4524 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004525 if (!err)
Theodore Ts'odfe50802014-09-01 14:37:09 -04004526 err = ext4_ext_insert_extent(handle, inode, &path,
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004527 &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004528
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004529 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004530 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4531 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004532 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004533 /* not a good idea to call discard here directly,
4534 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004535 ext4_discard_preallocations(inode);
Theodore Ts'oc8e15132013-07-15 00:09:37 -04004536 ext4_free_blocks(handle, inode, NULL, newblock,
4537 EXT4_C2B(sbi, allocated_clusters), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004538 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004539 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004540
Alex Tomasa86c6182006-10-11 01:21:03 -07004541 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004542 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004543 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004544 if (allocated > map->m_len)
4545 allocated = map->m_len;
4546 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004547
Jan Karab436b9b2009-12-08 23:51:10 -05004548 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004549 * Update reserved blocks/metadata blocks after successful
4550 * block allocation which had been deferred till now.
4551 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004552 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004553 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004554 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004555 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004556 */
4557 reserved_clusters = get_reserved_cluster_alloc(inode,
4558 map->m_lblk, allocated);
Eric Whitney9d21c9f2015-04-03 00:17:31 -04004559 if (!map_from_cluster) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04004560 BUG_ON(allocated_clusters < reserved_clusters);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004561 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004562 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004563 int reservation = allocated_clusters -
4564 reserved_clusters;
4565 /*
4566 * It seems we claimed few clusters outside of
4567 * the range of this allocation. We should give
4568 * it back to the reservation pool. This can
4569 * happen in the following case:
4570 *
4571 * * Suppose s_cluster_ratio is 4 (i.e., each
4572 * cluster has 4 blocks. Thus, the clusters
4573 * are [0-3],[4-7],[8-11]...
4574 * * First comes delayed allocation write for
4575 * logical blocks 10 & 11. Since there were no
4576 * previous delayed allocated blocks in the
4577 * range [8-11], we would reserve 1 cluster
4578 * for this write.
4579 * * Next comes write for logical blocks 3 to 8.
4580 * In this case, we will reserve 2 clusters
4581 * (for [0-3] and [4-7]; and not for [8-11] as
4582 * that range has a delayed allocated blocks.
4583 * Thus total reserved clusters now becomes 3.
4584 * * Now, during the delayed allocation writeout
4585 * time, we will first write blocks [3-8] and
4586 * allocate 3 clusters for writing these
4587 * blocks. Also, we would claim all these
4588 * three clusters above.
4589 * * Now when we come here to writeout the
4590 * blocks [10-11], we would expect to claim
4591 * the reservation of 1 cluster we had made
4592 * (and we would claim it since there are no
4593 * more delayed allocated blocks in the range
4594 * [8-11]. But our reserved cluster count had
4595 * already gone to 0.
4596 *
4597 * Thus, at the step 4 above when we determine
4598 * that there are still some unwritten delayed
4599 * allocated blocks outside of our current
4600 * block range, we should increment the
4601 * reserved clusters count so that when the
4602 * remaining blocks finally gets written, we
4603 * could claim them.
4604 */
Aditya Kali5356f262011-09-09 19:20:51 -04004605 dquot_reserve_block(inode,
4606 EXT4_C2B(sbi, reservation));
4607 spin_lock(&ei->i_block_reservation_lock);
4608 ei->i_reserved_data_blocks += reservation;
4609 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004610 }
Lukas Czerner232ec872013-03-10 22:46:30 -04004611 /*
4612 * We will claim quota for all newly allocated blocks.
4613 * We're updating the reserved space *after* the
4614 * correction above so we do not accidentally free
4615 * all the metadata reservation because we might
4616 * actually need it later on.
4617 */
4618 ext4_da_update_reserve_space(inode, allocated_clusters,
4619 1);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004620 }
4621 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004622
4623 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004624 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004625 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004626 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004627 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004628 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004629 else
Jan Karab436b9b2009-12-08 23:51:10 -05004630 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004631out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004632 if (allocated > map->m_len)
4633 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004634 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004635 map->m_flags |= EXT4_MAP_MAPPED;
4636 map->m_pblk = newblock;
4637 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004638out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004639 ext4_ext_drop_refs(path);
4640 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004641
Theodore Ts'o63b99962013-07-16 10:28:47 -04004642 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4643 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004644 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004645}
4646
Theodore Ts'o819c4922013-04-03 12:47:17 -04004647void ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004648{
Alex Tomasa86c6182006-10-11 01:21:03 -07004649 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004650 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004651 int err = 0;
4652
4653 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004654 * TODO: optimization is possible here.
4655 * Probably we need not scan at all,
4656 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004657 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004658
4659 /* we have to know where to truncate from in crash case */
4660 EXT4_I(inode)->i_disksize = inode->i_size;
4661 ext4_mark_inode_dirty(handle, inode);
4662
4663 last_block = (inode->i_size + sb->s_blocksize - 1)
4664 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004665retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004666 err = ext4_es_remove_extent(inode, last_block,
4667 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004668 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004669 cond_resched();
4670 congestion_wait(BLK_RW_ASYNC, HZ/50);
4671 goto retry;
4672 }
4673 if (err) {
4674 ext4_std_error(inode->i_sb, err);
4675 return;
4676 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04004677 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004678 ext4_std_error(inode->i_sb, err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004679}
4680
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004681static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004682 ext4_lblk_t len, loff_t new_size,
4683 int flags, int mode)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004684{
Al Viro496ad9a2013-01-23 17:07:38 -05004685 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004686 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004687 int ret = 0;
4688 int ret2 = 0;
4689 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004690 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004691 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004692 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004693 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004694
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004695 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004696 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004697 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004698 /*
4699 * Don't normalize the request if it can fit in one extent so
4700 * that it doesn't get unnecessarily split into multiple
4701 * extents.
4702 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004703 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004704 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004705
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004706 /*
4707 * credits to insert 1 extent into extent tree
4708 */
4709 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004710 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004711
Amit Aroraa2df2a62007-07-17 21:42:41 -04004712retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004713 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004714 /*
4715 * Recalculate credits when extent tree depth changes.
4716 */
4717 if (depth >= 0 && depth != ext_depth(inode)) {
4718 credits = ext4_chunk_trans_blocks(inode, len);
4719 depth = ext_depth(inode);
4720 }
4721
Theodore Ts'o9924a922013-02-08 21:59:22 -05004722 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4723 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004724 if (IS_ERR(handle)) {
4725 ret = PTR_ERR(handle);
4726 break;
4727 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004728 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004729 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004730 ext4_debug("inode #%lu: block %u: len %u: "
4731 "ext4_ext_map_blocks returned %d",
4732 inode->i_ino, map.m_lblk,
4733 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004734 ext4_mark_inode_dirty(handle, inode);
4735 ret2 = ext4_journal_stop(handle);
4736 break;
4737 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004738 map.m_lblk += ret;
4739 map.m_len = len = len - ret;
4740 epos = (loff_t)map.m_lblk << inode->i_blkbits;
4741 inode->i_ctime = ext4_current_time(inode);
4742 if (new_size) {
4743 if (epos > new_size)
4744 epos = new_size;
4745 if (ext4_update_inode_size(inode, epos) & 0x1)
4746 inode->i_mtime = inode->i_ctime;
4747 } else {
4748 if (epos > inode->i_size)
4749 ext4_set_inode_flag(inode,
4750 EXT4_INODE_EOFBLOCKS);
4751 }
4752 ext4_mark_inode_dirty(handle, inode);
Eryu Guan6a851bb2017-12-03 22:52:51 -05004753 ext4_update_inode_fsync_trans(handle, inode, 1);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004754 ret2 = ext4_journal_stop(handle);
4755 if (ret2)
4756 break;
4757 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004758 if (ret == -ENOSPC &&
4759 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4760 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004761 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004762 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004763
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004764 return ret > 0 ? ret2 : ret;
4765}
4766
Lukas Czernerb8a86842014-03-18 18:05:35 -04004767static long ext4_zero_range(struct file *file, loff_t offset,
4768 loff_t len, int mode)
4769{
4770 struct inode *inode = file_inode(file);
4771 handle_t *handle = NULL;
4772 unsigned int max_blocks;
4773 loff_t new_size = 0;
4774 int ret = 0;
4775 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004776 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004777 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004778 loff_t start, end;
4779 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004780 unsigned int blkbits = inode->i_blkbits;
4781
4782 trace_ext4_zero_range(inode, offset, len, mode);
4783
jon ernst6c5e73d2014-04-18 11:50:35 -04004784 if (!S_ISREG(inode->i_mode))
4785 return -EINVAL;
4786
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004787 /* Call ext4_force_commit to flush all data in case of data=journal. */
4788 if (ext4_should_journal_data(inode)) {
4789 ret = ext4_force_commit(inode->i_sb);
4790 if (ret)
4791 return ret;
4792 }
4793
Lukas Czernerb8a86842014-03-18 18:05:35 -04004794 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004795 * Round up offset. This is not fallocate, we neet to zero out
4796 * blocks, so convert interior block aligned part of the range to
4797 * unwritten and possibly manually zero out unaligned parts of the
4798 * range.
4799 */
4800 start = round_up(offset, 1 << blkbits);
4801 end = round_down((offset + len), 1 << blkbits);
4802
4803 if (start < offset || end > offset + len)
4804 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004805 partial_begin = offset & ((1 << blkbits) - 1);
4806 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004807
4808 lblk = start >> blkbits;
4809 max_blocks = (end >> blkbits);
4810 if (max_blocks < lblk)
4811 max_blocks = 0;
4812 else
4813 max_blocks -= lblk;
4814
Al Viro59551022016-01-22 15:40:57 -05004815 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004816
4817 /*
4818 * Indirect files do not support unwritten extnets
4819 */
4820 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4821 ret = -EOPNOTSUPP;
4822 goto out_mutex;
4823 }
4824
4825 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o9cdd4bf2017-10-06 23:09:55 -04004826 (offset + len > i_size_read(inode) ||
4827 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004828 new_size = offset + len;
4829 ret = inode_newsize_ok(inode, new_size);
4830 if (ret)
4831 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004832 }
4833
Lukas Czerner0f2af212015-04-03 00:09:13 -04004834 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4835 if (mode & FALLOC_FL_KEEP_SIZE)
4836 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4837
Jan Kara17048e82015-12-07 14:29:17 -05004838 /* Wait all existing dio workers, newcomers will block on i_mutex */
4839 ext4_inode_block_unlocked_dio(inode);
4840 inode_dio_wait(inode);
4841
Lukas Czerner0f2af212015-04-03 00:09:13 -04004842 /* Preallocate the range including the unaligned edges */
4843 if (partial_begin || partial_end) {
4844 ret = ext4_alloc_file_blocks(file,
4845 round_down(offset, 1 << blkbits) >> blkbits,
4846 (round_up((offset + len), 1 << blkbits) -
4847 round_down(offset, 1 << blkbits)) >> blkbits,
4848 new_size, flags, mode);
4849 if (ret)
Jan Kara17048e82015-12-07 14:29:17 -05004850 goto out_dio;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004851
4852 }
4853
4854 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004855 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004856 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4857 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004858
Jan Karaea3d7202015-12-07 14:28:03 -05004859 /*
4860 * Prevent page faults from reinstantiating pages we have
4861 * released from page cache.
4862 */
4863 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara01127842015-12-07 14:34:49 -05004864 ret = ext4_update_disksize_before_punch(inode, offset, len);
4865 if (ret) {
4866 up_write(&EXT4_I(inode)->i_mmap_sem);
4867 goto out_dio;
4868 }
Jan Karaea3d7202015-12-07 14:28:03 -05004869 /* Now release the pages and zero block aligned part of pages */
4870 truncate_pagecache_range(inode, start, end - 1);
4871 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4872
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004873 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4874 flags, mode);
Jan Karaea3d7202015-12-07 14:28:03 -05004875 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004876 if (ret)
4877 goto out_dio;
4878 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004879 if (!partial_begin && !partial_end)
4880 goto out_dio;
4881
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004882 /*
4883 * In worst case we have to writeout two nonadjacent unwritten
4884 * blocks and update the inode
4885 */
4886 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4887 if (ext4_should_journal_data(inode))
4888 credits += 2;
4889 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004890 if (IS_ERR(handle)) {
4891 ret = PTR_ERR(handle);
4892 ext4_std_error(inode->i_sb, ret);
4893 goto out_dio;
4894 }
4895
4896 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Lukas Czernere5b30412014-04-01 00:59:21 -04004897 if (new_size) {
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004898 ext4_update_inode_size(inode, new_size);
Lukas Czernere5b30412014-04-01 00:59:21 -04004899 } else {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004900 /*
4901 * Mark that we allocate beyond EOF so the subsequent truncate
4902 * can proceed even if the new size is the same as i_size.
4903 */
4904 if ((offset + len) > i_size_read(inode))
4905 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4906 }
Lukas Czernerb8a86842014-03-18 18:05:35 -04004907 ext4_mark_inode_dirty(handle, inode);
4908
4909 /* Zero out partial block at the edges of the range */
4910 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Karac404f0d2017-05-29 13:24:55 -04004911 if (ret >= 0)
4912 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004913
4914 if (file->f_flags & O_SYNC)
4915 ext4_handle_sync(handle);
4916
4917 ext4_journal_stop(handle);
4918out_dio:
4919 ext4_inode_resume_unlocked_dio(inode);
4920out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004921 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004922 return ret;
4923}
4924
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004925/*
4926 * preallocate space for a file. This implements ext4's fallocate file
4927 * operation, which gets called from sys_fallocate system call.
4928 * For block-mapped files, posix_fallocate should fall back to the method
4929 * of writing zeroes to the required new blocks (the same behavior which is
4930 * expected for file systems which do not support fallocate() system call).
4931 */
4932long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4933{
4934 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004935 loff_t new_size = 0;
4936 unsigned int max_blocks;
4937 int ret = 0;
4938 int flags;
4939 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004940 unsigned int blkbits = inode->i_blkbits;
4941
Michael Halcrow2058f832015-04-12 00:55:10 -04004942 /*
4943 * Encrypted inodes can't handle collapse range or insert
4944 * range since we would need to re-encrypt blocks with a
4945 * different IV or XTS tweak (which are based on the logical
4946 * block number).
4947 *
4948 * XXX It's not clear why zero range isn't working, but we'll
4949 * leave it disabled for encrypted inodes for now. This is a
4950 * bug we should fix....
4951 */
4952 if (ext4_encrypted_inode(inode) &&
Namjae Jeon331573f2015-06-09 01:55:03 -04004953 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
4954 FALLOC_FL_ZERO_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004955 return -EOPNOTSUPP;
4956
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004957 /* Return error if mode is not supported */
4958 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004959 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4960 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004961 return -EOPNOTSUPP;
4962
4963 if (mode & FALLOC_FL_PUNCH_HOLE)
4964 return ext4_punch_hole(inode, offset, len);
4965
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004966 ret = ext4_convert_inline_data(inode);
4967 if (ret)
4968 return ret;
4969
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004970 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4971 return ext4_collapse_range(inode, offset, len);
4972
Namjae Jeon331573f2015-06-09 01:55:03 -04004973 if (mode & FALLOC_FL_INSERT_RANGE)
4974 return ext4_insert_range(inode, offset, len);
4975
Lukas Czernerb8a86842014-03-18 18:05:35 -04004976 if (mode & FALLOC_FL_ZERO_RANGE)
4977 return ext4_zero_range(file, offset, len, mode);
4978
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004979 trace_ext4_fallocate_enter(inode, offset, len, mode);
4980 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004981
Fabian Frederick518eaa62016-09-15 11:55:01 -04004982 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004983 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004984 if (mode & FALLOC_FL_KEEP_SIZE)
4985 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4986
Al Viro59551022016-01-22 15:40:57 -05004987 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004988
Davide Italiano280227a2015-05-02 23:21:15 -04004989 /*
4990 * We only support preallocation for extent-based files only
4991 */
4992 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4993 ret = -EOPNOTSUPP;
4994 goto out;
4995 }
4996
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004997 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o9cdd4bf2017-10-06 23:09:55 -04004998 (offset + len > i_size_read(inode) ||
4999 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04005000 new_size = offset + len;
5001 ret = inode_newsize_ok(inode, new_size);
5002 if (ret)
5003 goto out;
5004 }
5005
Jan Kara17048e82015-12-07 14:29:17 -05005006 /* Wait all existing dio workers, newcomers will block on i_mutex */
5007 ext4_inode_block_unlocked_dio(inode);
5008 inode_dio_wait(inode);
5009
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04005010 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
5011 flags, mode);
Jan Kara17048e82015-12-07 14:29:17 -05005012 ext4_inode_resume_unlocked_dio(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04005013 if (ret)
5014 goto out;
5015
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04005016 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
5017 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5018 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04005019 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04005020out:
Al Viro59551022016-01-22 15:40:57 -05005021 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04005022 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
5023 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04005024}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005025
5026/*
Mingming Cao00314622009-09-28 15:49:08 -04005027 * This function convert a range of blocks to written extents
5028 * The caller of this function will pass the start offset and the size.
5029 * all unwritten extents within this range will be converted to
5030 * written extents.
5031 *
5032 * This function is called from the direct IO end io call back
5033 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05005034 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04005035 */
Jan Kara6b523df2013-06-04 13:21:11 -04005036int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
5037 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04005038{
Mingming Cao00314622009-09-28 15:49:08 -04005039 unsigned int max_blocks;
5040 int ret = 0;
5041 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005042 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04005043 unsigned int credits, blkbits = inode->i_blkbits;
5044
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005045 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04005046 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
5047
Mingming Cao00314622009-09-28 15:49:08 -04005048 /*
Jan Kara6b523df2013-06-04 13:21:11 -04005049 * This is somewhat ugly but the idea is clear: When transaction is
5050 * reserved, everything goes into it. Otherwise we rather start several
5051 * smaller transactions for conversion of each extent separately.
Mingming Cao00314622009-09-28 15:49:08 -04005052 */
Jan Kara6b523df2013-06-04 13:21:11 -04005053 if (handle) {
5054 handle = ext4_journal_start_reserved(handle,
5055 EXT4_HT_EXT_CONVERT);
5056 if (IS_ERR(handle))
5057 return PTR_ERR(handle);
5058 credits = 0;
5059 } else {
5060 /*
5061 * credits to insert 1 extent into extent tree
5062 */
5063 credits = ext4_chunk_trans_blocks(inode, max_blocks);
5064 }
Mingming Cao00314622009-09-28 15:49:08 -04005065 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005066 map.m_lblk += ret;
5067 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04005068 if (credits) {
5069 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5070 credits);
5071 if (IS_ERR(handle)) {
5072 ret = PTR_ERR(handle);
5073 break;
5074 }
Mingming Cao00314622009-09-28 15:49:08 -04005075 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005076 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05005077 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05005078 if (ret <= 0)
5079 ext4_warning(inode->i_sb,
5080 "inode #%lu: block %u: len %u: "
5081 "ext4_ext_map_blocks returned %d",
5082 inode->i_ino, map.m_lblk,
5083 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04005084 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04005085 if (credits)
5086 ret2 = ext4_journal_stop(handle);
5087 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04005088 break;
5089 }
Jan Kara6b523df2013-06-04 13:21:11 -04005090 if (!credits)
5091 ret2 = ext4_journal_stop(handle);
Mingming Cao00314622009-09-28 15:49:08 -04005092 return ret > 0 ? ret2 : ret;
5093}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005094
Mingming Cao00314622009-09-28 15:49:08 -04005095/*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005096 * If newes is not existing extent (newes->ec_pblk equals zero) find
5097 * delayed extent at start of newes and update newes accordingly and
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005098 * return start of the next delayed extent.
5099 *
Zheng Liu69eb33d2013-02-18 00:31:07 -05005100 * If newes is existing extent (newes->ec_pblk is not equal zero)
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005101 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
Zheng Liu69eb33d2013-02-18 00:31:07 -05005102 * extent found. Leave newes unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005103 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005104static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -05005105 struct extent_status *newes)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005106{
Zheng Liub3aff3e2012-11-08 21:57:37 -05005107 struct extent_status es;
Zheng Liube401362013-02-18 00:27:26 -05005108 ext4_lblk_t block, next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005109
Zheng Liu69eb33d2013-02-18 00:31:07 -05005110 if (newes->es_pblk == 0) {
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005111 ext4_es_find_delayed_extent_range(inode, newes->es_lblk,
5112 newes->es_lblk + newes->es_len - 1, &es);
5113
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005114 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005115 * No extent in extent-tree contains block @newes->es_pblk,
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005116 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005117 */
Zheng Liu06b0c882013-02-18 00:26:51 -05005118 if (es.es_len == 0)
Zheng Liub3aff3e2012-11-08 21:57:37 -05005119 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005120 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005121
Zheng Liu69eb33d2013-02-18 00:31:07 -05005122 if (es.es_lblk > newes->es_lblk) {
Zheng Liub3aff3e2012-11-08 21:57:37 -05005123 /* A hole found. */
Zheng Liu69eb33d2013-02-18 00:31:07 -05005124 newes->es_len = min(es.es_lblk - newes->es_lblk,
5125 newes->es_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005126 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005127 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005128
Zheng Liu69eb33d2013-02-18 00:31:07 -05005129 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005130 }
5131
Zheng Liu69eb33d2013-02-18 00:31:07 -05005132 block = newes->es_lblk + newes->es_len;
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005133 ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es);
Zheng Liube401362013-02-18 00:27:26 -05005134 if (es.es_len == 0)
5135 next_del = EXT_MAX_BLOCKS;
5136 else
5137 next_del = es.es_lblk;
5138
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005139 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005140}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005141/* fiemap flags we can handle specified here */
5142#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
5143
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05005144static int ext4_xattr_fiemap(struct inode *inode,
5145 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005146{
5147 __u64 physical = 0;
5148 __u64 length;
5149 __u32 flags = FIEMAP_EXTENT_LAST;
5150 int blockbits = inode->i_sb->s_blocksize_bits;
5151 int error = 0;
5152
5153 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005154 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04005155 struct ext4_iloc iloc;
5156 int offset; /* offset of xattr in inode */
5157
5158 error = ext4_get_inode_loc(inode, &iloc);
5159 if (error)
5160 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04005161 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005162 offset = EXT4_GOOD_OLD_INODE_SIZE +
5163 EXT4_I(inode)->i_extra_isize;
5164 physical += offset;
5165 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5166 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005167 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005168 } else { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04005169 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005170 length = inode->i_sb->s_blocksize;
5171 }
5172
5173 if (physical)
5174 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5175 length, flags);
5176 return (error < 0 ? error : 0);
5177}
5178
5179int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5180 __u64 start, __u64 len)
5181{
5182 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005183 int error = 0;
5184
Tao Ma94191982012-12-10 14:06:02 -05005185 if (ext4_has_inline_data(inode)) {
5186 int has_inline = 1;
5187
Dmitry Monakhovd952d692014-12-02 16:11:20 -05005188 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5189 start, len);
Tao Ma94191982012-12-10 14:06:02 -05005190
5191 if (has_inline)
5192 return error;
5193 }
5194
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005195 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5196 error = ext4_ext_precache(inode);
5197 if (error)
5198 return error;
5199 }
5200
Eric Sandeen6873fa02008-10-07 00:46:36 -04005201 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005202 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Theodore Ts'oad7fefb2015-01-02 15:16:00 -05005203 return generic_block_fiemap(inode, fieinfo, start, len,
5204 ext4_get_block);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005205
5206 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
5207 return -EBADR;
5208
5209 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5210 error = ext4_xattr_fiemap(inode, fieinfo);
5211 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005212 ext4_lblk_t len_blks;
5213 __u64 last_blk;
5214
Eric Sandeen6873fa02008-10-07 00:46:36 -04005215 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005216 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04005217 if (last_blk >= EXT_MAX_BLOCKS)
5218 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005219 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005220
5221 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005222 * Walk the extent tree gathering extent information
5223 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005224 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005225 error = ext4_fill_fiemap_extents(inode, start_blk,
5226 len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005227 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04005228 return error;
5229}
Namjae Jeon9eb79482014-02-23 15:18:59 -05005230
5231/*
5232 * ext4_access_path:
5233 * Function to access the path buffer for marking it dirty.
5234 * It also checks if there are sufficient credits left in the journal handle
5235 * to update path.
5236 */
5237static int
5238ext4_access_path(handle_t *handle, struct inode *inode,
5239 struct ext4_ext_path *path)
5240{
5241 int credits, err;
5242
5243 if (!ext4_handle_valid(handle))
5244 return 0;
5245
5246 /*
5247 * Check if need to extend journal credits
5248 * 3 for leaf, sb, and inode plus 2 (bmap and group
5249 * descriptor) for each block group; assume two block
5250 * groups
5251 */
5252 if (handle->h_buffer_credits < 7) {
5253 credits = ext4_writepage_trans_blocks(inode);
5254 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
5255 /* EAGAIN is success */
5256 if (err && err != -EAGAIN)
5257 return err;
5258 }
5259
5260 err = ext4_ext_get_access(handle, inode, path);
5261 return err;
5262}
5263
5264/*
5265 * ext4_ext_shift_path_extents:
5266 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04005267 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5268 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005269 */
5270static int
5271ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5272 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005273 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005274{
5275 int depth, err = 0;
5276 struct ext4_extent *ex_start, *ex_last;
5277 bool update = 0;
5278 depth = path->p_depth;
5279
5280 while (depth >= 0) {
5281 if (depth == path->p_depth) {
5282 ex_start = path[depth].p_ext;
5283 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005284 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005285
5286 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005287
5288 err = ext4_access_path(handle, inode, path + depth);
5289 if (err)
5290 goto out;
5291
5292 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5293 update = 1;
5294
Namjae Jeon9eb79482014-02-23 15:18:59 -05005295 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005296 if (SHIFT == SHIFT_LEFT) {
5297 le32_add_cpu(&ex_start->ee_block,
5298 -shift);
5299 /* Try to merge to the left. */
5300 if ((ex_start >
5301 EXT_FIRST_EXTENT(path[depth].p_hdr))
5302 &&
5303 ext4_ext_try_to_merge_right(inode,
5304 path, ex_start - 1))
5305 ex_last--;
5306 else
5307 ex_start++;
5308 } else {
5309 le32_add_cpu(&ex_last->ee_block, shift);
5310 ext4_ext_try_to_merge_right(inode, path,
5311 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005312 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005313 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005314 }
5315 err = ext4_ext_dirty(handle, inode, path + depth);
5316 if (err)
5317 goto out;
5318
5319 if (--depth < 0 || !update)
5320 break;
5321 }
5322
5323 /* Update index too */
5324 err = ext4_access_path(handle, inode, path + depth);
5325 if (err)
5326 goto out;
5327
Namjae Jeon331573f2015-06-09 01:55:03 -04005328 if (SHIFT == SHIFT_LEFT)
5329 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5330 else
5331 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005332 err = ext4_ext_dirty(handle, inode, path + depth);
5333 if (err)
5334 goto out;
5335
5336 /* we are done if current index is not a starting index */
5337 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5338 break;
5339
5340 depth--;
5341 }
5342
5343out:
5344 return err;
5345}
5346
5347/*
5348 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005349 * All the extents which lies in the range from @start to the last allocated
5350 * block for the @inode are shifted either towards left or right (depending
5351 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005352 * On success, 0 is returned, error otherwise.
5353 */
5354static int
5355ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005356 ext4_lblk_t start, ext4_lblk_t shift,
5357 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005358{
5359 struct ext4_ext_path *path;
5360 int ret = 0, depth;
5361 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005362 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005363
5364 /* Let path point to the last extent */
Roman Pene0b53d62017-01-08 21:00:35 -05005365 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5366 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005367 if (IS_ERR(path))
5368 return PTR_ERR(path);
5369
5370 depth = path->p_depth;
5371 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005372 if (!extent)
5373 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005374
Roman Pen72ae4762017-01-08 20:59:35 -05005375 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005376
Namjae Jeon331573f2015-06-09 01:55:03 -04005377 /*
Eric Biggersa405a2a2018-04-12 11:48:09 -04005378 * For left shifts, make sure the hole on the left is big enough to
5379 * accommodate the shift. For right shifts, make sure the last extent
5380 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005381 */
5382 if (SHIFT == SHIFT_LEFT) {
Roman Pene0b53d62017-01-08 21:00:35 -05005383 path = ext4_find_extent(inode, start - 1, &path,
5384 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005385 if (IS_ERR(path))
5386 return PTR_ERR(path);
5387 depth = path->p_depth;
5388 extent = path[depth].p_ext;
5389 if (extent) {
5390 ex_start = le32_to_cpu(extent->ee_block);
5391 ex_end = le32_to_cpu(extent->ee_block) +
5392 ext4_ext_get_actual_len(extent);
5393 } else {
5394 ex_start = 0;
5395 ex_end = 0;
5396 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005397
Namjae Jeon331573f2015-06-09 01:55:03 -04005398 if ((start == ex_start && shift > ex_start) ||
5399 (shift > start - ex_end)) {
Eric Biggersa405a2a2018-04-12 11:48:09 -04005400 ret = -EINVAL;
5401 goto out;
5402 }
5403 } else {
5404 if (shift > EXT_MAX_BLOCKS -
5405 (stop + ext4_ext_get_actual_len(extent))) {
5406 ret = -EINVAL;
5407 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005408 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005409 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005410
Namjae Jeon331573f2015-06-09 01:55:03 -04005411 /*
5412 * In case of left shift, iterator points to start and it is increased
5413 * till we reach stop. In case of right shift, iterator points to stop
5414 * and it is decreased till we reach start.
5415 */
5416 if (SHIFT == SHIFT_LEFT)
5417 iterator = &start;
5418 else
5419 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005420
Roman Pen72ae4762017-01-08 20:59:35 -05005421 /*
5422 * Its safe to start updating extents. Start and stop are unsigned, so
5423 * in case of right shift if extent with 0 block is reached, iterator
5424 * becomes NULL to indicate the end of the loop.
5425 */
5426 while (iterator && start <= stop) {
Roman Pene0b53d62017-01-08 21:00:35 -05005427 path = ext4_find_extent(inode, *iterator, &path,
5428 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005429 if (IS_ERR(path))
5430 return PTR_ERR(path);
5431 depth = path->p_depth;
5432 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005433 if (!extent) {
5434 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005435 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005436 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005437 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005438 if (SHIFT == SHIFT_LEFT && *iterator >
5439 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005440 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005441 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5442 path[depth].p_ext++;
5443 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005444 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005445 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005446 }
5447 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005448
5449 if (SHIFT == SHIFT_LEFT) {
5450 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5451 *iterator = le32_to_cpu(extent->ee_block) +
5452 ext4_ext_get_actual_len(extent);
5453 } else {
5454 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen72ae4762017-01-08 20:59:35 -05005455 if (le32_to_cpu(extent->ee_block) > 0)
5456 *iterator = le32_to_cpu(extent->ee_block) - 1;
5457 else
5458 /* Beginning is reached, end of the loop */
5459 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005460 /* Update path extent in case we need to stop */
5461 while (le32_to_cpu(extent->ee_block) < start)
5462 extent++;
5463 path[depth].p_ext = extent;
5464 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005465 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005466 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005467 if (ret)
5468 break;
5469 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005470out:
5471 ext4_ext_drop_refs(path);
5472 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005473 return ret;
5474}
5475
5476/*
5477 * ext4_collapse_range:
5478 * This implements the fallocate's collapse range functionality for ext4
5479 * Returns: 0 and non-zero on error.
5480 */
5481int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5482{
5483 struct super_block *sb = inode->i_sb;
5484 ext4_lblk_t punch_start, punch_stop;
5485 handle_t *handle;
5486 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005487 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005488 int ret;
5489
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005490 /*
5491 * We need to test this early because xfstests assumes that a
5492 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5493 * system does not support collapse range.
5494 */
5495 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5496 return -EOPNOTSUPP;
5497
Namjae Jeon9eb79482014-02-23 15:18:59 -05005498 /* Collapse range works only on fs block size aligned offsets. */
Namjae Jeonee98fa32014-07-29 10:45:31 -04005499 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5500 len & (EXT4_CLUSTER_SIZE(sb) - 1))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005501 return -EINVAL;
5502
5503 if (!S_ISREG(inode->i_mode))
Theodore Ts'o86f1ca32014-04-18 11:52:11 -04005504 return -EINVAL;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005505
5506 trace_ext4_collapse_range(inode, offset, len);
5507
5508 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5509 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5510
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005511 /* Call ext4_force_commit to flush all data in case of data=journal. */
5512 if (ext4_should_journal_data(inode)) {
5513 ret = ext4_force_commit(inode->i_sb);
5514 if (ret)
5515 return ret;
5516 }
5517
Al Viro59551022016-01-22 15:40:57 -05005518 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005519 /*
5520 * There is no need to overlap collapse range with EOF, in which case
5521 * it is effectively a truncate operation
5522 */
5523 if (offset + len >= i_size_read(inode)) {
5524 ret = -EINVAL;
5525 goto out_mutex;
5526 }
5527
Namjae Jeon9eb79482014-02-23 15:18:59 -05005528 /* Currently just for extent based files */
5529 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5530 ret = -EOPNOTSUPP;
5531 goto out_mutex;
5532 }
5533
Namjae Jeon9eb79482014-02-23 15:18:59 -05005534 /* Wait for existing dio to complete */
5535 ext4_inode_block_unlocked_dio(inode);
5536 inode_dio_wait(inode);
5537
Jan Karaea3d7202015-12-07 14:28:03 -05005538 /*
5539 * Prevent page faults from reinstantiating pages we have released from
5540 * page cache.
5541 */
5542 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara32ebffd2015-12-07 14:31:11 -05005543 /*
5544 * Need to round down offset to be aligned with page size boundary
5545 * for page size > block size.
5546 */
5547 ioffset = round_down(offset, PAGE_SIZE);
5548 /*
5549 * Write tail of the last page before removed range since it will get
5550 * removed from the page cache below.
5551 */
5552 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5553 if (ret)
5554 goto out_mmap;
5555 /*
5556 * Write data that will be shifted to preserve them when discarding
5557 * page cache below. We are also protected from pages becoming dirty
5558 * by i_mmap_sem.
5559 */
5560 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5561 LLONG_MAX);
5562 if (ret)
5563 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005564 truncate_pagecache(inode, ioffset);
5565
Namjae Jeon9eb79482014-02-23 15:18:59 -05005566 credits = ext4_writepage_trans_blocks(inode);
5567 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5568 if (IS_ERR(handle)) {
5569 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005570 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005571 }
5572
5573 down_write(&EXT4_I(inode)->i_data_sem);
5574 ext4_discard_preallocations(inode);
5575
5576 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005577 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005578 if (ret) {
5579 up_write(&EXT4_I(inode)->i_data_sem);
5580 goto out_stop;
5581 }
5582
5583 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5584 if (ret) {
5585 up_write(&EXT4_I(inode)->i_data_sem);
5586 goto out_stop;
5587 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005588 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005589
5590 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005591 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005592 if (ret) {
5593 up_write(&EXT4_I(inode)->i_data_sem);
5594 goto out_stop;
5595 }
5596
5597 new_size = i_size_read(inode) - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005598 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005599 EXT4_I(inode)->i_disksize = new_size;
5600
Namjae Jeon9eb79482014-02-23 15:18:59 -05005601 up_write(&EXT4_I(inode)->i_data_sem);
5602 if (IS_SYNC(inode))
5603 ext4_handle_sync(handle);
5604 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
5605 ext4_mark_inode_dirty(handle, inode);
Jan Karac404f0d2017-05-29 13:24:55 -04005606 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005607
5608out_stop:
5609 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005610out_mmap:
5611 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005612 ext4_inode_resume_unlocked_dio(inode);
5613out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005614 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005615 return ret;
5616}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005617
Namjae Jeon331573f2015-06-09 01:55:03 -04005618/*
5619 * ext4_insert_range:
5620 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5621 * The data blocks starting from @offset to the EOF are shifted by @len
5622 * towards right to create a hole in the @inode. Inode size is increased
5623 * by len bytes.
5624 * Returns 0 on success, error otherwise.
5625 */
5626int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
5627{
5628 struct super_block *sb = inode->i_sb;
5629 handle_t *handle;
5630 struct ext4_ext_path *path;
5631 struct ext4_extent *extent;
5632 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5633 unsigned int credits, ee_len;
5634 int ret = 0, depth, split_flag = 0;
5635 loff_t ioffset;
5636
5637 /*
5638 * We need to test this early because xfstests assumes that an
5639 * insert range of (0, 1) will return EOPNOTSUPP if the file
5640 * system does not support insert range.
5641 */
5642 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5643 return -EOPNOTSUPP;
5644
5645 /* Insert range works only on fs block size aligned offsets. */
5646 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5647 len & (EXT4_CLUSTER_SIZE(sb) - 1))
5648 return -EINVAL;
5649
5650 if (!S_ISREG(inode->i_mode))
5651 return -EOPNOTSUPP;
5652
5653 trace_ext4_insert_range(inode, offset, len);
5654
5655 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5656 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5657
5658 /* Call ext4_force_commit to flush all data in case of data=journal */
5659 if (ext4_should_journal_data(inode)) {
5660 ret = ext4_force_commit(inode->i_sb);
5661 if (ret)
5662 return ret;
5663 }
5664
Al Viro59551022016-01-22 15:40:57 -05005665 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005666 /* Currently just for extent based files */
5667 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5668 ret = -EOPNOTSUPP;
5669 goto out_mutex;
5670 }
5671
5672 /* Check for wrap through zero */
5673 if (inode->i_size + len > inode->i_sb->s_maxbytes) {
5674 ret = -EFBIG;
5675 goto out_mutex;
5676 }
5677
5678 /* Offset should be less than i_size */
5679 if (offset >= i_size_read(inode)) {
5680 ret = -EINVAL;
5681 goto out_mutex;
5682 }
5683
Namjae Jeon331573f2015-06-09 01:55:03 -04005684 /* Wait for existing dio to complete */
5685 ext4_inode_block_unlocked_dio(inode);
5686 inode_dio_wait(inode);
5687
Jan Karaea3d7202015-12-07 14:28:03 -05005688 /*
5689 * Prevent page faults from reinstantiating pages we have released from
5690 * page cache.
5691 */
5692 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara32ebffd2015-12-07 14:31:11 -05005693 /*
5694 * Need to round down to align start offset to page size boundary
5695 * for page size > block size.
5696 */
5697 ioffset = round_down(offset, PAGE_SIZE);
5698 /* Write out all dirty pages */
5699 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5700 LLONG_MAX);
5701 if (ret)
5702 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005703 truncate_pagecache(inode, ioffset);
5704
Namjae Jeon331573f2015-06-09 01:55:03 -04005705 credits = ext4_writepage_trans_blocks(inode);
5706 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5707 if (IS_ERR(handle)) {
5708 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005709 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005710 }
5711
5712 /* Expand file to avoid data loss if there is error while shifting */
5713 inode->i_size += len;
5714 EXT4_I(inode)->i_disksize += len;
5715 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
5716 ret = ext4_mark_inode_dirty(handle, inode);
5717 if (ret)
5718 goto out_stop;
5719
5720 down_write(&EXT4_I(inode)->i_data_sem);
5721 ext4_discard_preallocations(inode);
5722
5723 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5724 if (IS_ERR(path)) {
5725 up_write(&EXT4_I(inode)->i_data_sem);
5726 goto out_stop;
5727 }
5728
5729 depth = ext_depth(inode);
5730 extent = path[depth].p_ext;
5731 if (extent) {
5732 ee_start_lblk = le32_to_cpu(extent->ee_block);
5733 ee_len = ext4_ext_get_actual_len(extent);
5734
5735 /*
5736 * If offset_lblk is not the starting block of extent, split
5737 * the extent @offset_lblk
5738 */
5739 if ((offset_lblk > ee_start_lblk) &&
5740 (offset_lblk < (ee_start_lblk + ee_len))) {
5741 if (ext4_ext_is_unwritten(extent))
5742 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5743 EXT4_EXT_MARK_UNWRIT2;
5744 ret = ext4_split_extent_at(handle, inode, &path,
5745 offset_lblk, split_flag,
5746 EXT4_EX_NOCACHE |
5747 EXT4_GET_BLOCKS_PRE_IO |
5748 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5749 }
5750
5751 ext4_ext_drop_refs(path);
5752 kfree(path);
5753 if (ret < 0) {
5754 up_write(&EXT4_I(inode)->i_data_sem);
5755 goto out_stop;
5756 }
Fabian Frederickedf15aa2016-09-15 11:39:52 -04005757 } else {
5758 ext4_ext_drop_refs(path);
5759 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005760 }
5761
5762 ret = ext4_es_remove_extent(inode, offset_lblk,
5763 EXT_MAX_BLOCKS - offset_lblk);
5764 if (ret) {
5765 up_write(&EXT4_I(inode)->i_data_sem);
5766 goto out_stop;
5767 }
5768
5769 /*
5770 * if offset_lblk lies in a hole which is at start of file, use
5771 * ee_start_lblk to shift extents
5772 */
5773 ret = ext4_ext_shift_extents(inode, handle,
5774 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5775 len_lblk, SHIFT_RIGHT);
5776
5777 up_write(&EXT4_I(inode)->i_data_sem);
5778 if (IS_SYNC(inode))
5779 ext4_handle_sync(handle);
Jan Karac404f0d2017-05-29 13:24:55 -04005780 if (ret >= 0)
5781 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005782
5783out_stop:
5784 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005785out_mmap:
5786 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005787 ext4_inode_resume_unlocked_dio(inode);
5788out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005789 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005790 return ret;
5791}
5792
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005793/**
5794 * ext4_swap_extents - Swap extents between two inodes
5795 *
5796 * @inode1: First inode
5797 * @inode2: Second inode
5798 * @lblk1: Start block for first inode
5799 * @lblk2: Start block for second inode
5800 * @count: Number of blocks to swap
5801 * @mark_unwritten: Mark second inode's extents as unwritten after swap
5802 * @erp: Pointer to save error value
5803 *
5804 * This helper routine does exactly what is promise "swap extents". All other
5805 * stuff such as page-cache locking consistency, bh mapping consistency or
5806 * extent's data copying must be performed by caller.
5807 * Locking:
5808 * i_mutex is held for both inodes
5809 * i_data_sem is locked for write for both inodes
5810 * Assumptions:
5811 * All pages from requested range are locked for both inodes
5812 */
5813int
5814ext4_swap_extents(handle_t *handle, struct inode *inode1,
5815 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
5816 ext4_lblk_t count, int unwritten, int *erp)
5817{
5818 struct ext4_ext_path *path1 = NULL;
5819 struct ext4_ext_path *path2 = NULL;
5820 int replaced_count = 0;
5821
5822 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5823 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005824 BUG_ON(!inode_is_locked(inode1));
5825 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005826
5827 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005828 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005829 return 0;
5830 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005831 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005832 return 0;
5833
5834 while (count) {
5835 struct ext4_extent *ex1, *ex2, tmp_ex;
5836 ext4_lblk_t e1_blk, e2_blk;
5837 int e1_len, e2_len, len;
5838 int split = 0;
5839
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005840 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305841 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005842 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005843 path1 = NULL;
5844 finish:
5845 count = 0;
5846 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005847 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005848 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305849 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005850 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005851 path2 = NULL;
5852 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005853 }
5854 ex1 = path1[path1->p_depth].p_ext;
5855 ex2 = path2[path2->p_depth].p_ext;
5856 /* Do we have somthing to swap ? */
5857 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005858 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005859
5860 e1_blk = le32_to_cpu(ex1->ee_block);
5861 e2_blk = le32_to_cpu(ex2->ee_block);
5862 e1_len = ext4_ext_get_actual_len(ex1);
5863 e2_len = ext4_ext_get_actual_len(ex2);
5864
5865 /* Hole handling */
5866 if (!in_range(lblk1, e1_blk, e1_len) ||
5867 !in_range(lblk2, e2_blk, e2_len)) {
5868 ext4_lblk_t next1, next2;
5869
5870 /* if hole after extent, then go to next extent */
5871 next1 = ext4_ext_next_allocated_block(path1);
5872 next2 = ext4_ext_next_allocated_block(path2);
5873 /* If hole before extent, then shift to that extent */
5874 if (e1_blk > lblk1)
5875 next1 = e1_blk;
5876 if (e2_blk > lblk2)
5877 next2 = e1_blk;
5878 /* Do we have something to swap */
5879 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005880 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005881 /* Move to the rightest boundary */
5882 len = next1 - lblk1;
5883 if (len < next2 - lblk2)
5884 len = next2 - lblk2;
5885 if (len > count)
5886 len = count;
5887 lblk1 += len;
5888 lblk2 += len;
5889 count -= len;
5890 goto repeat;
5891 }
5892
5893 /* Prepare left boundary */
5894 if (e1_blk < lblk1) {
5895 split = 1;
5896 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005897 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005898 if (unlikely(*erp))
5899 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005900 }
5901 if (e2_blk < lblk2) {
5902 split = 1;
5903 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005904 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005905 if (unlikely(*erp))
5906 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005907 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005908 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005909 * path must to be revalidated. */
5910 if (split)
5911 goto repeat;
5912
5913 /* Prepare right boundary */
5914 len = count;
5915 if (len > e1_blk + e1_len - lblk1)
5916 len = e1_blk + e1_len - lblk1;
5917 if (len > e2_blk + e2_len - lblk2)
5918 len = e2_blk + e2_len - lblk2;
5919
5920 if (len != e1_len) {
5921 split = 1;
5922 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005923 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005924 if (unlikely(*erp))
5925 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005926 }
5927 if (len != e2_len) {
5928 split = 1;
5929 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005930 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005931 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005932 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005933 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005934 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005935 * path must to be revalidated. */
5936 if (split)
5937 goto repeat;
5938
5939 BUG_ON(e2_len != e1_len);
5940 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005941 if (unlikely(*erp))
5942 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005943 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005944 if (unlikely(*erp))
5945 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005946
5947 /* Both extents are fully inside boundaries. Swap it now */
5948 tmp_ex = *ex1;
5949 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5950 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5951 ex1->ee_len = cpu_to_le16(e2_len);
5952 ex2->ee_len = cpu_to_le16(e1_len);
5953 if (unwritten)
5954 ext4_ext_mark_unwritten(ex2);
5955 if (ext4_ext_is_unwritten(&tmp_ex))
5956 ext4_ext_mark_unwritten(ex1);
5957
5958 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5959 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5960 *erp = ext4_ext_dirty(handle, inode2, path2 +
5961 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005962 if (unlikely(*erp))
5963 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005964 *erp = ext4_ext_dirty(handle, inode1, path1 +
5965 path1->p_depth);
5966 /*
5967 * Looks scarry ah..? second inode already points to new blocks,
5968 * and it was successfully dirtied. But luckily error may happen
5969 * only due to journal error, so full transaction will be
5970 * aborted anyway.
5971 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005972 if (unlikely(*erp))
5973 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005974 lblk1 += len;
5975 lblk2 += len;
5976 replaced_count += len;
5977 count -= len;
5978
5979 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005980 ext4_ext_drop_refs(path1);
5981 kfree(path1);
5982 ext4_ext_drop_refs(path2);
5983 kfree(path2);
5984 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005985 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005986 return replaced_count;
5987}