blob: 25b524c993e9071783e16e61ab06070012267c28 [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;
Kyle Yanadd58562017-06-16 11:01:48 -07003460 zero_ex1.ee_start_lo = 0;
3461 zero_ex2.ee_start_lo = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003462
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003463 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3464
3465 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003466 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003467 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003468
3469 /*
3470 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003471 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003472 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003473 * memmove() calls. Transferring to the left is the common case in
3474 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3475 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003476 *
3477 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003478 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003479 * This would require removing the extent if the transfer
3480 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003481 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003482 * same extent tree node.
3483 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003484 if ((map->m_lblk == ee_block) &&
3485 /* See if we can merge left */
3486 (map_len < ee_len) && /*L1*/
3487 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003488 ext4_lblk_t prev_lblk;
3489 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003490 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003491
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003492 abut_ex = ex - 1;
3493 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3494 prev_len = ext4_ext_get_actual_len(abut_ex);
3495 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003496 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003497
3498 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003499 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003500 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003501 * - C1: abut_ex is initialized,
3502 * - C2: abut_ex is logically abutting ex,
3503 * - C3: abut_ex is physically abutting ex,
3504 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003505 * overflowing the (initialized) length limit.
3506 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003507 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003508 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3509 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003510 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003511 err = ext4_ext_get_access(handle, inode, path + depth);
3512 if (err)
3513 goto out;
3514
3515 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003516 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003517
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003518 /* Shift the start of ex by 'map_len' blocks */
3519 ex->ee_block = cpu_to_le32(ee_block + map_len);
3520 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3521 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003522 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003523
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003524 /* Extend abut_ex by 'map_len' blocks */
3525 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003526
3527 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003528 allocated = map_len;
3529 }
3530 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3531 (map_len < ee_len) && /*L1*/
3532 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3533 /* See if we can merge right */
3534 ext4_lblk_t next_lblk;
3535 ext4_fsblk_t next_pblk, ee_pblk;
3536 unsigned int next_len;
3537
3538 abut_ex = ex + 1;
3539 next_lblk = le32_to_cpu(abut_ex->ee_block);
3540 next_len = ext4_ext_get_actual_len(abut_ex);
3541 next_pblk = ext4_ext_pblock(abut_ex);
3542 ee_pblk = ext4_ext_pblock(ex);
3543
3544 /*
3545 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3546 * upon those conditions:
3547 * - C1: abut_ex is initialized,
3548 * - C2: abut_ex is logically abutting ex,
3549 * - C3: abut_ex is physically abutting ex,
3550 * - C4: abut_ex can receive the additional blocks without
3551 * overflowing the (initialized) length limit.
3552 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003553 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003554 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3555 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3556 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3557 err = ext4_ext_get_access(handle, inode, path + depth);
3558 if (err)
3559 goto out;
3560
3561 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3562 map, ex, abut_ex);
3563
3564 /* Shift the start of abut_ex by 'map_len' blocks */
3565 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3566 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3567 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003568 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003569
3570 /* Extend abut_ex by 'map_len' blocks */
3571 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3572
3573 /* Result: number of initialized blocks past m_lblk */
3574 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003575 }
3576 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003577 if (allocated) {
3578 /* Mark the block containing both extents as dirty */
3579 ext4_ext_dirty(handle, inode, path + depth);
3580
3581 /* Update path to point to the right extent */
3582 path[depth].p_ext = abut_ex;
3583 goto out;
3584 } else
3585 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003586
Yongqiang Yang667eff32011-05-03 12:25:07 -04003587 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003588 /*
3589 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003590 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003591 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003592 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003593
Zheng Liu67a5da52012-08-17 09:54:17 -04003594 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3595 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003596 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003597
Chandan Rajendrae68f6b82018-12-12 15:20:10 +05303598 if (IS_ENCRYPTED(inode))
Theodore Ts'o36086d42015-10-03 10:49:29 -04003599 max_zeroout = 0;
3600
Amit Arora56055d32007-07-17 21:42:38 -04003601 /*
Jan Kara2e169212017-05-26 17:40:52 -04003602 * five cases:
Yongqiang Yang667eff32011-05-03 12:25:07 -04003603 * 1. split the extent into three extents.
Jan Kara2e169212017-05-26 17:40:52 -04003604 * 2. split the extent into two extents, zeroout the head of the first
3605 * extent.
3606 * 3. split the extent into two extents, zeroout the tail of the second
3607 * extent.
Yongqiang Yang667eff32011-05-03 12:25:07 -04003608 * 4. split the extent into two extents with out zeroout.
Jan Kara2e169212017-05-26 17:40:52 -04003609 * 5. no splitting needed, just possibly zeroout the head and / or the
3610 * tail of the extent.
Amit Arora56055d32007-07-17 21:42:38 -04003611 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003612 split_map.m_lblk = map->m_lblk;
3613 split_map.m_len = map->m_len;
3614
Jan Kara2e169212017-05-26 17:40:52 -04003615 if (max_zeroout && (allocated > split_map.m_len)) {
Zheng Liu67a5da52012-08-17 09:54:17 -04003616 if (allocated <= max_zeroout) {
Jan Kara2e169212017-05-26 17:40:52 -04003617 /* case 3 or 5 */
3618 zero_ex1.ee_block =
3619 cpu_to_le32(split_map.m_lblk +
3620 split_map.m_len);
3621 zero_ex1.ee_len =
3622 cpu_to_le16(allocated - split_map.m_len);
3623 ext4_ext_store_pblock(&zero_ex1,
3624 ext4_ext_pblock(ex) + split_map.m_lblk +
3625 split_map.m_len - ee_block);
3626 err = ext4_ext_zeroout(inode, &zero_ex1);
Amit Arora56055d32007-07-17 21:42:38 -04003627 if (err)
3628 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003629 split_map.m_len = allocated;
Jan Kara2e169212017-05-26 17:40:52 -04003630 }
3631 if (split_map.m_lblk - ee_block + split_map.m_len <
3632 max_zeroout) {
3633 /* case 2 or 5 */
3634 if (split_map.m_lblk != ee_block) {
3635 zero_ex2.ee_block = ex->ee_block;
3636 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
Yongqiang Yang667eff32011-05-03 12:25:07 -04003637 ee_block);
Jan Kara2e169212017-05-26 17:40:52 -04003638 ext4_ext_store_pblock(&zero_ex2,
Yongqiang Yang667eff32011-05-03 12:25:07 -04003639 ext4_ext_pblock(ex));
Jan Kara2e169212017-05-26 17:40:52 -04003640 err = ext4_ext_zeroout(inode, &zero_ex2);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003641 if (err)
3642 goto out;
3643 }
3644
Jan Kara2e169212017-05-26 17:40:52 -04003645 split_map.m_len += split_map.m_lblk - ee_block;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003646 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003647 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003648 }
3649 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003650
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003651 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3652 flags);
3653 if (err > 0)
3654 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003655out:
Zheng Liuadb23552013-03-10 21:13:05 -04003656 /* If we have gotten a failure, don't zero out status tree */
Jan Kara2e169212017-05-26 17:40:52 -04003657 if (!err) {
3658 err = ext4_zeroout_es(inode, &zero_ex1);
3659 if (!err)
3660 err = ext4_zeroout_es(inode, &zero_ex2);
3661 }
Amit Arora56055d32007-07-17 21:42:38 -04003662 return err ? err : allocated;
3663}
3664
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003665/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003666 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003667 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003668 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003669 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003670 * Writing to an unwritten extent may result in splitting the unwritten
3671 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003672 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003673 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003674 * b> Splits in two extents: Write is happening at either end of the extent
3675 * c> Splits in three extents: Somone is writing in middle of the extent
3676 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003677 * This works the same way in the case of initialized -> unwritten conversion.
3678 *
Mingming Cao00314622009-09-28 15:49:08 -04003679 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003680 * the unwritten extent split. To prevent ENOSPC occur at the IO
3681 * complete, we need to split the unwritten extent before DIO submit
3682 * the IO. The unwritten extent called at this time will be split
3683 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003684 * being filled will be convert to initialized by the end_io callback function
3685 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003686 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003687 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003688 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003689static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003690 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003691 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003692 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003693 int flags)
3694{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003695 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003696 ext4_lblk_t eof_block;
3697 ext4_lblk_t ee_block;
3698 struct ext4_extent *ex;
3699 unsigned int ee_len;
3700 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003701
Lukas Czernerb8a86842014-03-18 18:05:35 -04003702 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3703 __func__, inode->i_ino,
3704 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003705
3706 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3707 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003708 if (eof_block < map->m_lblk + map->m_len)
3709 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003710 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003711 * It is safe to convert extent to initialized via explicit
3712 * zeroout only if extent is fully insde i_size or new_size.
3713 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003714 depth = ext_depth(inode);
3715 ex = path[depth].p_ext;
3716 ee_block = le32_to_cpu(ex->ee_block);
3717 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003718
Lukas Czernerb8a86842014-03-18 18:05:35 -04003719 /* Convert to unwritten */
3720 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3721 split_flag |= EXT4_EXT_DATA_VALID1;
3722 /* Convert to initialized */
3723 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3724 split_flag |= ee_block + ee_len <= eof_block ?
3725 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003726 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003727 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003728 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003729 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003730}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003731
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003732static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003733 struct inode *inode,
3734 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003735 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003736{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003737 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003738 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003739 ext4_lblk_t ee_block;
3740 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003741 int depth;
3742 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003743
3744 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003745 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003746 ee_block = le32_to_cpu(ex->ee_block);
3747 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003748
Yongqiang Yang197217a2011-05-03 11:45:29 -04003749 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3750 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003751 (unsigned long long)ee_block, ee_len);
3752
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003753 /* If extent is larger than requested it is a clear sign that we still
3754 * have some extent state machine issues left. So extent_split is still
3755 * required.
3756 * TODO: Once all related issues will be fixed this situation should be
3757 * illegal.
3758 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003759 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Rakesh Panditf6245492019-08-22 22:53:46 -04003760#ifdef CONFIG_EXT4_DEBUG
3761 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
Jakub Wilk8d2ae1c2016-04-27 01:11:21 -04003762 " len %u; IO logical block %llu, len %u",
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003763 inode->i_ino, (unsigned long long)ee_block, ee_len,
3764 (unsigned long long)map->m_lblk, map->m_len);
3765#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003766 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003767 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003768 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003769 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003770 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003771 if (IS_ERR(path))
3772 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003773 depth = ext_depth(inode);
3774 ex = path[depth].p_ext;
3775 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003776
Mingming Cao00314622009-09-28 15:49:08 -04003777 err = ext4_ext_get_access(handle, inode, path + depth);
3778 if (err)
3779 goto out;
3780 /* first mark the extent as initialized */
3781 ext4_ext_mark_initialized(ex);
3782
Yongqiang Yang197217a2011-05-03 11:45:29 -04003783 /* note: ext4_ext_correct_indexes() isn't needed here because
3784 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003785 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003786 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003787
Mingming Cao00314622009-09-28 15:49:08 -04003788 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003789 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003790out:
3791 ext4_ext_show_leaf(inode, path);
3792 return err;
3793}
3794
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003795static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3796 sector_t block, int count)
3797{
3798 int i;
3799 for (i = 0; i < count; i++)
3800 unmap_underlying_metadata(bdev, block + i);
3801}
3802
Theodore Ts'o58590b02010-10-27 21:23:12 -04003803/*
3804 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3805 */
3806static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003807 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003808 struct ext4_ext_path *path,
3809 unsigned int len)
3810{
3811 int i, depth;
3812 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003813 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003814
3815 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3816 return 0;
3817
3818 depth = ext_depth(inode);
3819 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003820
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003821 /*
3822 * We're going to remove EOFBLOCKS_FL entirely in future so we
3823 * do not care for this case anymore. Simply remove the flag
3824 * if there are no extents.
3825 */
3826 if (unlikely(!eh->eh_entries))
3827 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003828 last_ex = EXT_LAST_EXTENT(eh);
3829 /*
3830 * We should clear the EOFBLOCKS_FL flag if we are writing the
3831 * last block in the last extent in the file. We test this by
3832 * first checking to see if the caller to
3833 * ext4_ext_get_blocks() was interested in the last block (or
3834 * a block beyond the last block) in the current extent. If
3835 * this turns out to be false, we can bail out from this
3836 * function immediately.
3837 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003838 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003839 ext4_ext_get_actual_len(last_ex))
3840 return 0;
3841 /*
3842 * If the caller does appear to be planning to write at or
3843 * beyond the end of the current extent, we then test to see
3844 * if the current extent is the last extent in the file, by
3845 * checking to make sure it was reached via the rightmost node
3846 * at each level of the tree.
3847 */
3848 for (i = depth-1; i >= 0; i--)
3849 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3850 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003851out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003852 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3853 return ext4_mark_inode_dirty(handle, inode);
3854}
3855
Aditya Kali7b415bf2011-09-09 19:04:51 -04003856/**
3857 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3858 *
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003859 * Return 1 if there is a delalloc block in the range, otherwise 0.
Aditya Kali7b415bf2011-09-09 19:04:51 -04003860 */
Zheng Liuf7fec032013-02-18 00:28:47 -05003861int ext4_find_delalloc_range(struct inode *inode,
3862 ext4_lblk_t lblk_start,
3863 ext4_lblk_t lblk_end)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003864{
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003865 struct extent_status es;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003866
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04003867 ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es);
Zheng Liu06b0c882013-02-18 00:26:51 -05003868 if (es.es_len == 0)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003869 return 0; /* there is no delay extent in this tree */
Zheng Liu06b0c882013-02-18 00:26:51 -05003870 else if (es.es_lblk <= lblk_start &&
3871 lblk_start < es.es_lblk + es.es_len)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003872 return 1;
Zheng Liu06b0c882013-02-18 00:26:51 -05003873 else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003874 return 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003875 else
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003876 return 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003877}
3878
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003879int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003880{
3881 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3882 ext4_lblk_t lblk_start, lblk_end;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003883 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003884 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3885
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003886 return ext4_find_delalloc_range(inode, lblk_start, lblk_end);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003887}
3888
3889/**
3890 * Determines how many complete clusters (out of those specified by the 'map')
3891 * are under delalloc and were reserved quota for.
3892 * This function is called when we are writing out the blocks that were
3893 * originally written with their allocation delayed, but then the space was
3894 * allocated using fallocate() before the delayed allocation could be resolved.
3895 * The cases to look for are:
3896 * ('=' indicated delayed allocated blocks
3897 * '-' indicates non-delayed allocated blocks)
3898 * (a) partial clusters towards beginning and/or end outside of allocated range
3899 * are not delalloc'ed.
3900 * Ex:
3901 * |----c---=|====c====|====c====|===-c----|
3902 * |++++++ allocated ++++++|
3903 * ==> 4 complete clusters in above example
3904 *
3905 * (b) partial cluster (outside of allocated range) towards either end is
3906 * marked for delayed allocation. In this case, we will exclude that
3907 * cluster.
3908 * Ex:
3909 * |----====c========|========c========|
3910 * |++++++ allocated ++++++|
3911 * ==> 1 complete clusters in above example
3912 *
3913 * Ex:
3914 * |================c================|
3915 * |++++++ allocated ++++++|
3916 * ==> 0 complete clusters in above example
3917 *
3918 * The ext4_da_update_reserve_space will be called only if we
3919 * determine here that there were some "entire" clusters that span
3920 * this 'allocated' range.
3921 * In the non-bigalloc case, this function will just end up returning num_blks
3922 * without ever calling ext4_find_delalloc_range.
3923 */
3924static unsigned int
3925get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3926 unsigned int num_blks)
3927{
3928 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3929 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3930 ext4_lblk_t lblk_from, lblk_to, c_offset;
3931 unsigned int allocated_clusters = 0;
3932
3933 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3934 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3935
3936 /* max possible clusters for this allocation */
3937 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3938
Aditya Kalid8990242011-09-09 19:18:51 -04003939 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3940
Aditya Kali7b415bf2011-09-09 19:04:51 -04003941 /* Check towards left side */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003942 c_offset = EXT4_LBLK_COFF(sbi, lblk_start);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003943 if (c_offset) {
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003944 lblk_from = EXT4_LBLK_CMASK(sbi, lblk_start);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003945 lblk_to = lblk_from + c_offset - 1;
3946
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003947 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003948 allocated_clusters--;
3949 }
3950
3951 /* Now check towards right. */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003952 c_offset = EXT4_LBLK_COFF(sbi, lblk_start + num_blks);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003953 if (allocated_clusters && c_offset) {
3954 lblk_from = lblk_start + num_blks;
3955 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3956
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003957 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003958 allocated_clusters--;
3959 }
3960
3961 return allocated_clusters;
3962}
3963
Mingming Cao00314622009-09-28 15:49:08 -04003964static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003965convert_initialized_extent(handle_t *handle, struct inode *inode,
3966 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003967 struct ext4_ext_path **ppath,
Eryu Guan56263b42016-02-12 01:23:00 -05003968 unsigned int allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003969{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003970 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003971 struct ext4_extent *ex;
3972 ext4_lblk_t ee_block;
3973 unsigned int ee_len;
3974 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003975 int err = 0;
3976
3977 /*
3978 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003979 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003980 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003981 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3982 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003983
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003984 depth = ext_depth(inode);
3985 ex = path[depth].p_ext;
3986 ee_block = le32_to_cpu(ex->ee_block);
3987 ee_len = ext4_ext_get_actual_len(ex);
3988
3989 ext_debug("%s: inode %lu, logical"
3990 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3991 (unsigned long long)ee_block, ee_len);
3992
3993 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003994 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003995 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3996 if (err < 0)
3997 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003998 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003999 if (IS_ERR(path))
4000 return PTR_ERR(path);
4001 depth = ext_depth(inode);
4002 ex = path[depth].p_ext;
4003 if (!ex) {
4004 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
4005 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004006 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004007 }
4008 }
4009
4010 err = ext4_ext_get_access(handle, inode, path + depth);
4011 if (err)
4012 return err;
4013 /* first mark the extent as unwritten */
4014 ext4_ext_mark_unwritten(ex);
4015
4016 /* note: ext4_ext_correct_indexes() isn't needed here because
4017 * borders are not changed
4018 */
4019 ext4_ext_try_to_merge(handle, inode, path, ex);
4020
4021 /* Mark modified extent as dirty */
4022 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
4023 if (err)
4024 return err;
4025 ext4_ext_show_leaf(inode, path);
4026
4027 ext4_update_inode_fsync_trans(handle, inode, 1);
4028 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
4029 if (err)
4030 return err;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004031 map->m_flags |= EXT4_MAP_UNWRITTEN;
4032 if (allocated > map->m_len)
4033 allocated = map->m_len;
4034 map->m_len = allocated;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004035 return allocated;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004036}
4037
4038static int
Lukas Czerner556615d2014-04-20 23:45:47 -04004039ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004040 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004041 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004042 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04004043{
Theodore Ts'odfe50802014-09-01 14:37:09 -04004044 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04004045 int ret = 0;
4046 int err = 0;
4047
Lukas Czerner556615d2014-04-20 23:45:47 -04004048 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05004049 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004050 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04004051 flags, allocated);
4052 ext4_ext_show_leaf(inode, path);
4053
Lukas Czerner27dd4382013-04-09 22:11:22 -04004054 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004055 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04004056 * allocate metadata blocks for the new extent block if needed.
4057 */
4058 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4059
Lukas Czerner556615d2014-04-20 23:45:47 -04004060 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05004061 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04004062
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004063 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004064 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004065 ret = ext4_split_convert_extents(handle, inode, map, ppath,
4066 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004067 if (ret <= 0)
4068 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004069 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004070 goto out;
4071 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004072 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004073 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05004074 if (flags & EXT4_GET_BLOCKS_ZERO) {
4075 if (allocated > map->m_len)
4076 allocated = map->m_len;
4077 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
4078 allocated);
4079 if (err < 0)
4080 goto out2;
4081 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04004082 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004083 ppath);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004084 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05004085 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05004086 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4087 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004088 } else
4089 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04004090 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05004091 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04004092 if (allocated > map->m_len)
4093 allocated = map->m_len;
4094 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004095 goto out2;
4096 }
4097 /* buffered IO case */
4098 /*
4099 * repeat fallocate creation request
4100 * we already have an unwritten extent
4101 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004102 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05004103 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004104 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004105 }
Mingming Cao00314622009-09-28 15:49:08 -04004106
4107 /* buffered READ or buffered write_begin() lookup */
4108 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4109 /*
4110 * We have blocks reserved already. We
4111 * return allocated blocks so that delalloc
4112 * won't do block reservation for us. But
4113 * the buffer head will be unmapped so that
4114 * a read from the block returns 0s.
4115 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004116 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004117 goto out1;
4118 }
4119
4120 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04004121 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004122 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004123 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04004124out:
4125 if (ret <= 0) {
4126 err = ret;
4127 goto out2;
4128 } else
4129 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004130 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004131 /*
4132 * if we allocated more blocks than requested
4133 * we need to make sure we unmap the extra block
4134 * allocated. The actual needed block will get
4135 * unmapped later when we find the buffer_head marked
4136 * new.
4137 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004138 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004139 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004140 newblock + map->m_len,
4141 allocated - map->m_len);
4142 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004143 }
Zheng Liu3a225672013-03-10 21:20:23 -04004144 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004145
4146 /*
4147 * If we have done fallocate with the offset that is already
4148 * delayed allocated, we would have block reservation
4149 * and quota reservation done in the delayed write path.
4150 * But fallocate would have already updated quota and block
4151 * count for this offset. So cancel these reservation
4152 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004153 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4154 unsigned int reserved_clusters;
4155 reserved_clusters = get_reserved_cluster_alloc(inode,
4156 map->m_lblk, map->m_len);
4157 if (reserved_clusters)
4158 ext4_da_update_reserve_space(inode,
4159 reserved_clusters,
4160 0);
4161 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004162
Mingming Cao00314622009-09-28 15:49:08 -04004163map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004164 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004165 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4166 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4167 map->m_len);
4168 if (err < 0)
4169 goto out2;
4170 }
Mingming Cao00314622009-09-28 15:49:08 -04004171out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004172 if (allocated > map->m_len)
4173 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04004174 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004175 map->m_pblk = newblock;
4176 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004177out2:
Mingming Cao00314622009-09-28 15:49:08 -04004178 return err ? err : allocated;
4179}
Theodore Ts'o58590b02010-10-27 21:23:12 -04004180
Mingming Cao00314622009-09-28 15:49:08 -04004181/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004182 * get_implied_cluster_alloc - check to see if the requested
4183 * allocation (in the map structure) overlaps with a cluster already
4184 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04004185 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004186 * @map The requested lblk->pblk mapping
4187 * @ex The extent structure which might contain an implied
4188 * cluster allocation
4189 *
4190 * This function is called by ext4_ext_map_blocks() after we failed to
4191 * find blocks that were already in the inode's extent tree. Hence,
4192 * we know that the beginning of the requested region cannot overlap
4193 * the extent from the inode's extent tree. There are three cases we
4194 * want to catch. The first is this case:
4195 *
4196 * |--- cluster # N--|
4197 * |--- extent ---| |---- requested region ---|
4198 * |==========|
4199 *
4200 * The second case that we need to test for is this one:
4201 *
4202 * |--------- cluster # N ----------------|
4203 * |--- requested region --| |------- extent ----|
4204 * |=======================|
4205 *
4206 * The third case is when the requested region lies between two extents
4207 * within the same cluster:
4208 * |------------- cluster # N-------------|
4209 * |----- ex -----| |---- ex_right ----|
4210 * |------ requested region ------|
4211 * |================|
4212 *
4213 * In each of the above cases, we need to set the map->m_pblk and
4214 * map->m_len so it corresponds to the return the extent labelled as
4215 * "|====|" from cluster #N, since it is already in use for data in
4216 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4217 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4218 * as a new "allocated" block region. Otherwise, we will return 0 and
4219 * ext4_ext_map_blocks() will then allocate one or more new clusters
4220 * by calling ext4_mb_new_blocks().
4221 */
Aditya Kalid8990242011-09-09 19:18:51 -04004222static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004223 struct ext4_map_blocks *map,
4224 struct ext4_extent *ex,
4225 struct ext4_ext_path *path)
4226{
Aditya Kalid8990242011-09-09 19:18:51 -04004227 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004228 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004229 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05004230 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004231 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4232 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4233 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4234
4235 /* The extent passed in that we are trying to match */
4236 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4237 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4238
4239 /* The requested region passed into ext4_map_blocks() */
4240 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004241
4242 if ((rr_cluster_start == ex_cluster_end) ||
4243 (rr_cluster_start == ex_cluster_start)) {
4244 if (rr_cluster_start == ex_cluster_end)
4245 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004246 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004247 map->m_len = min(map->m_len,
4248 (unsigned) sbi->s_cluster_ratio - c_offset);
4249 /*
4250 * Check for and handle this case:
4251 *
4252 * |--------- cluster # N-------------|
4253 * |------- extent ----|
4254 * |--- requested region ---|
4255 * |===========|
4256 */
4257
4258 if (map->m_lblk < ee_block)
4259 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4260
4261 /*
4262 * Check for the case where there is already another allocated
4263 * block to the right of 'ex' but before the end of the cluster.
4264 *
4265 * |------------- cluster # N-------------|
4266 * |----- ex -----| |---- ex_right ----|
4267 * |------ requested region ------|
4268 * |================|
4269 */
4270 if (map->m_lblk > ee_block) {
4271 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4272 map->m_len = min(map->m_len, next - map->m_lblk);
4273 }
Aditya Kalid8990242011-09-09 19:18:51 -04004274
4275 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004276 return 1;
4277 }
Aditya Kalid8990242011-09-09 19:18:51 -04004278
4279 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004280 return 0;
4281}
4282
4283
4284/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004285 * Block allocation/map/preallocation routine for extents based files
4286 *
4287 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004288 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004289 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4290 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004291 *
4292 * return > 0, number of of blocks already mapped/allocated
4293 * if create == 0 and these are pre-allocated blocks
4294 * buffer head is unmapped
4295 * otherwise blocks are mapped
4296 *
4297 * return = 0, if plain look up failed (blocks have not been allocated)
4298 * buffer head is unmapped
4299 *
4300 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004301 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004302int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4303 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004304{
4305 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004306 struct ext4_extent newex, *ex, *ex2;
4307 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004308 ext4_fsblk_t newblock = 0;
Eric Whitneyce37c422014-02-19 18:52:39 -05004309 int free_on_err = 0, err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004310 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004311 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004312 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004313 ext4_lblk_t cluster_offset;
Jan Karacbd75842014-11-25 11:41:49 -05004314 bool map_from_cluster = false;
Alex Tomasa86c6182006-10-11 01:21:03 -07004315
Mingming84fe3be2009-09-01 08:44:37 -04004316 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004317 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004318 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004319
Alex Tomasa86c6182006-10-11 01:21:03 -07004320 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004321 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004322 if (IS_ERR(path)) {
4323 err = PTR_ERR(path);
4324 path = NULL;
4325 goto out2;
4326 }
4327
4328 depth = ext_depth(inode);
4329
4330 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004331 * consistent leaf must not be empty;
4332 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004333 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004334 */
Frank Mayhar273df552010-03-02 11:46:09 -05004335 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4336 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004337 "lblock: %lu, depth: %d pblock %lld",
4338 (unsigned long) map->m_lblk, depth,
4339 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004340 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004341 goto out2;
4342 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004343
Avantika Mathur7e028972006-12-06 20:41:33 -08004344 ex = path[depth].p_ext;
4345 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004346 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004347 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004348 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004349
Lukas Czernerb8a86842014-03-18 18:05:35 -04004350
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004351 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004352 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004353 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004354 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004355 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004356
4357 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4358
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004359 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004360 if (in_range(map->m_lblk, ee_block, ee_len)) {
4361 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004362 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004363 allocated = ee_len - (map->m_lblk - ee_block);
4364 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4365 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004366
Lukas Czernerb8a86842014-03-18 18:05:35 -04004367 /*
4368 * If the extent is initialized check whether the
4369 * caller wants to convert it to unwritten.
4370 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004371 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004372 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004373 allocated = convert_initialized_extent(
Theodore Ts'o4f224b82014-09-01 14:36:09 -04004374 handle, inode, map, &path,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05004375 allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004376 goto out2;
Lukas Czerner556615d2014-04-20 23:45:47 -04004377 } else if (!ext4_ext_is_unwritten(ex))
Lukas Czerner78771912012-03-19 23:05:43 -04004378 goto out;
Zheng Liu69eb33d2013-02-18 00:31:07 -05004379
Lukas Czerner556615d2014-04-20 23:45:47 -04004380 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004381 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004382 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004383 if (ret < 0)
4384 err = ret;
4385 else
4386 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004387 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004388 }
4389 }
4390
4391 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004392 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004393 * we couldn't try to create block if create flag is zero
4394 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004395 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004396 ext4_lblk_t hole_start, hole_len;
4397
Jan Karafacab4d2016-03-09 22:54:00 -05004398 hole_start = map->m_lblk;
4399 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004400 /*
4401 * put just found gap into cache to speed up
4402 * subsequent requests
4403 */
Jan Kara140a5252016-03-09 22:46:57 -05004404 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004405
4406 /* Update hole_len to reflect hole size after map->m_lblk */
4407 if (hole_start != map->m_lblk)
4408 hole_len -= map->m_lblk - hole_start;
4409 map->m_pblk = 0;
4410 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4411
Alex Tomasa86c6182006-10-11 01:21:03 -07004412 goto out2;
4413 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004414
Alex Tomasa86c6182006-10-11 01:21:03 -07004415 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004416 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004417 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004418 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004419 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004420
4421 /*
4422 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004423 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004424 */
4425 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004426 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004427 ar.len = allocated = map->m_len;
4428 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004429 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004430 goto got_allocated_blocks;
4431 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004432
Alex Tomasc9de5602008-01-29 00:19:52 -05004433 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004434 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004435 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4436 if (err)
4437 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004438 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004439 ex2 = NULL;
4440 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004441 if (err)
4442 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004443
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004444 /* Check if the extent after searching to the right implies a
4445 * cluster we can use. */
4446 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004447 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004448 ar.len = allocated = map->m_len;
4449 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004450 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004451 goto got_allocated_blocks;
4452 }
4453
Amit Arora749269f2007-07-18 09:02:56 -04004454 /*
4455 * See if request is beyond maximum number of blocks we can have in
4456 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004457 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4458 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004459 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004460 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004461 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004462 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004463 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4464 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4465 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004466
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004467 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004468 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004469 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004470 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004471 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004472 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004473 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004474
4475 /* allocate new block */
4476 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004477 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4478 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004479 /*
4480 * We calculate the offset from the beginning of the cluster
4481 * for the logical block number, since when we allocate a
4482 * physical cluster, the physical block should start at the
4483 * same offset from the beginning of the cluster. This is
4484 * needed so that future calls to get_implied_cluster_alloc()
4485 * work correctly.
4486 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004487 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004488 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4489 ar.goal -= offset;
4490 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004491 if (S_ISREG(inode->i_mode))
4492 ar.flags = EXT4_MB_HINT_DATA;
4493 else
4494 /* disable in-core preallocation for non-regular files */
4495 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004496 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4497 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004498 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4499 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004500 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4501 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004502 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004503 if (!newblock)
4504 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004505 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004506 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004507 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004508 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004509 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4510 if (ar.len > allocated)
4511 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004512
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004513got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004514 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004515 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004516 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004517 /* Mark unwritten */
4518 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4519 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004520 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004521 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004522
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004523 err = 0;
4524 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4525 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4526 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004527 if (!err)
Theodore Ts'odfe50802014-09-01 14:37:09 -04004528 err = ext4_ext_insert_extent(handle, inode, &path,
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004529 &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004530
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004531 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004532 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4533 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004534 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004535 /* not a good idea to call discard here directly,
4536 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004537 ext4_discard_preallocations(inode);
Theodore Ts'oc8e15132013-07-15 00:09:37 -04004538 ext4_free_blocks(handle, inode, NULL, newblock,
4539 EXT4_C2B(sbi, allocated_clusters), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004540 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004541 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004542
Alex Tomasa86c6182006-10-11 01:21:03 -07004543 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004544 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004545 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004546 if (allocated > map->m_len)
4547 allocated = map->m_len;
4548 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004549
Jan Karab436b9b2009-12-08 23:51:10 -05004550 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004551 * Update reserved blocks/metadata blocks after successful
4552 * block allocation which had been deferred till now.
4553 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004554 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004555 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004556 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004557 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004558 */
4559 reserved_clusters = get_reserved_cluster_alloc(inode,
4560 map->m_lblk, allocated);
Eric Whitney9d21c9f2015-04-03 00:17:31 -04004561 if (!map_from_cluster) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04004562 BUG_ON(allocated_clusters < reserved_clusters);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004563 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004564 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004565 int reservation = allocated_clusters -
4566 reserved_clusters;
4567 /*
4568 * It seems we claimed few clusters outside of
4569 * the range of this allocation. We should give
4570 * it back to the reservation pool. This can
4571 * happen in the following case:
4572 *
4573 * * Suppose s_cluster_ratio is 4 (i.e., each
4574 * cluster has 4 blocks. Thus, the clusters
4575 * are [0-3],[4-7],[8-11]...
4576 * * First comes delayed allocation write for
4577 * logical blocks 10 & 11. Since there were no
4578 * previous delayed allocated blocks in the
4579 * range [8-11], we would reserve 1 cluster
4580 * for this write.
4581 * * Next comes write for logical blocks 3 to 8.
4582 * In this case, we will reserve 2 clusters
4583 * (for [0-3] and [4-7]; and not for [8-11] as
4584 * that range has a delayed allocated blocks.
4585 * Thus total reserved clusters now becomes 3.
4586 * * Now, during the delayed allocation writeout
4587 * time, we will first write blocks [3-8] and
4588 * allocate 3 clusters for writing these
4589 * blocks. Also, we would claim all these
4590 * three clusters above.
4591 * * Now when we come here to writeout the
4592 * blocks [10-11], we would expect to claim
4593 * the reservation of 1 cluster we had made
4594 * (and we would claim it since there are no
4595 * more delayed allocated blocks in the range
4596 * [8-11]. But our reserved cluster count had
4597 * already gone to 0.
4598 *
4599 * Thus, at the step 4 above when we determine
4600 * that there are still some unwritten delayed
4601 * allocated blocks outside of our current
4602 * block range, we should increment the
4603 * reserved clusters count so that when the
4604 * remaining blocks finally gets written, we
4605 * could claim them.
4606 */
Aditya Kali5356f262011-09-09 19:20:51 -04004607 dquot_reserve_block(inode,
4608 EXT4_C2B(sbi, reservation));
4609 spin_lock(&ei->i_block_reservation_lock);
4610 ei->i_reserved_data_blocks += reservation;
4611 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004612 }
Lukas Czerner232ec872013-03-10 22:46:30 -04004613 /*
4614 * We will claim quota for all newly allocated blocks.
4615 * We're updating the reserved space *after* the
4616 * correction above so we do not accidentally free
4617 * all the metadata reservation because we might
4618 * actually need it later on.
4619 */
4620 ext4_da_update_reserve_space(inode, allocated_clusters,
4621 1);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004622 }
4623 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004624
4625 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004626 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004627 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004628 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004629 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004630 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004631 else
Jan Karab436b9b2009-12-08 23:51:10 -05004632 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004633out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004634 if (allocated > map->m_len)
4635 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004636 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004637 map->m_flags |= EXT4_MAP_MAPPED;
4638 map->m_pblk = newblock;
4639 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004640out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004641 ext4_ext_drop_refs(path);
4642 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004643
Theodore Ts'o63b99962013-07-16 10:28:47 -04004644 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4645 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004646 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004647}
4648
Theodore Ts'o819c4922013-04-03 12:47:17 -04004649void ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004650{
Alex Tomasa86c6182006-10-11 01:21:03 -07004651 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004652 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004653 int err = 0;
4654
4655 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004656 * TODO: optimization is possible here.
4657 * Probably we need not scan at all,
4658 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004659 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004660
4661 /* we have to know where to truncate from in crash case */
4662 EXT4_I(inode)->i_disksize = inode->i_size;
4663 ext4_mark_inode_dirty(handle, inode);
4664
4665 last_block = (inode->i_size + sb->s_blocksize - 1)
4666 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004667retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004668 err = ext4_es_remove_extent(inode, last_block,
4669 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004670 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004671 cond_resched();
4672 congestion_wait(BLK_RW_ASYNC, HZ/50);
4673 goto retry;
4674 }
4675 if (err) {
4676 ext4_std_error(inode->i_sb, err);
4677 return;
4678 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04004679 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004680 ext4_std_error(inode->i_sb, err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004681}
4682
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004683static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004684 ext4_lblk_t len, loff_t new_size,
4685 int flags, int mode)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004686{
Al Viro496ad9a2013-01-23 17:07:38 -05004687 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004688 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004689 int ret = 0;
4690 int ret2 = 0;
4691 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004692 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004693 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004694 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004695 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004696
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004697 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004698 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004699 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004700 /*
4701 * Don't normalize the request if it can fit in one extent so
4702 * that it doesn't get unnecessarily split into multiple
4703 * extents.
4704 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004705 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004706 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004707
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004708 /*
4709 * credits to insert 1 extent into extent tree
4710 */
4711 credits = ext4_chunk_trans_blocks(inode, len);
Fabian Frederickc3fe4932016-09-15 11:52:07 -04004712 depth = ext_depth(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004713
Amit Aroraa2df2a62007-07-17 21:42:41 -04004714retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004715 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004716 /*
4717 * Recalculate credits when extent tree depth changes.
4718 */
4719 if (depth >= 0 && depth != ext_depth(inode)) {
4720 credits = ext4_chunk_trans_blocks(inode, len);
4721 depth = ext_depth(inode);
4722 }
4723
Theodore Ts'o9924a922013-02-08 21:59:22 -05004724 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4725 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004726 if (IS_ERR(handle)) {
4727 ret = PTR_ERR(handle);
4728 break;
4729 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004730 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004731 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004732 ext4_debug("inode #%lu: block %u: len %u: "
4733 "ext4_ext_map_blocks returned %d",
4734 inode->i_ino, map.m_lblk,
4735 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004736 ext4_mark_inode_dirty(handle, inode);
4737 ret2 = ext4_journal_stop(handle);
4738 break;
4739 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004740 map.m_lblk += ret;
4741 map.m_len = len = len - ret;
4742 epos = (loff_t)map.m_lblk << inode->i_blkbits;
4743 inode->i_ctime = ext4_current_time(inode);
4744 if (new_size) {
4745 if (epos > new_size)
4746 epos = new_size;
4747 if (ext4_update_inode_size(inode, epos) & 0x1)
4748 inode->i_mtime = inode->i_ctime;
4749 } else {
4750 if (epos > inode->i_size)
4751 ext4_set_inode_flag(inode,
4752 EXT4_INODE_EOFBLOCKS);
4753 }
4754 ext4_mark_inode_dirty(handle, inode);
Eryu Guan6a851bb2017-12-03 22:52:51 -05004755 ext4_update_inode_fsync_trans(handle, inode, 1);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004756 ret2 = ext4_journal_stop(handle);
4757 if (ret2)
4758 break;
4759 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004760 if (ret == -ENOSPC &&
4761 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4762 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004763 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004764 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004765
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004766 return ret > 0 ? ret2 : ret;
4767}
4768
Lukas Czernerb8a86842014-03-18 18:05:35 -04004769static long ext4_zero_range(struct file *file, loff_t offset,
4770 loff_t len, int mode)
4771{
4772 struct inode *inode = file_inode(file);
4773 handle_t *handle = NULL;
4774 unsigned int max_blocks;
4775 loff_t new_size = 0;
4776 int ret = 0;
4777 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004778 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004779 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004780 loff_t start, end;
4781 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004782 unsigned int blkbits = inode->i_blkbits;
4783
4784 trace_ext4_zero_range(inode, offset, len, mode);
4785
jon ernst6c5e73d2014-04-18 11:50:35 -04004786 if (!S_ISREG(inode->i_mode))
4787 return -EINVAL;
4788
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004789 /* Call ext4_force_commit to flush all data in case of data=journal. */
4790 if (ext4_should_journal_data(inode)) {
4791 ret = ext4_force_commit(inode->i_sb);
4792 if (ret)
4793 return ret;
4794 }
4795
Lukas Czernerb8a86842014-03-18 18:05:35 -04004796 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004797 * Round up offset. This is not fallocate, we neet to zero out
4798 * blocks, so convert interior block aligned part of the range to
4799 * unwritten and possibly manually zero out unaligned parts of the
4800 * range.
4801 */
4802 start = round_up(offset, 1 << blkbits);
4803 end = round_down((offset + len), 1 << blkbits);
4804
4805 if (start < offset || end > offset + len)
4806 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004807 partial_begin = offset & ((1 << blkbits) - 1);
4808 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004809
4810 lblk = start >> blkbits;
4811 max_blocks = (end >> blkbits);
4812 if (max_blocks < lblk)
4813 max_blocks = 0;
4814 else
4815 max_blocks -= lblk;
4816
Al Viro59551022016-01-22 15:40:57 -05004817 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004818
4819 /*
4820 * Indirect files do not support unwritten extnets
4821 */
4822 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4823 ret = -EOPNOTSUPP;
4824 goto out_mutex;
4825 }
4826
4827 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o9cdd4bf2017-10-06 23:09:55 -04004828 (offset + len > i_size_read(inode) ||
4829 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004830 new_size = offset + len;
4831 ret = inode_newsize_ok(inode, new_size);
4832 if (ret)
4833 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004834 }
4835
Lukas Czerner0f2af212015-04-03 00:09:13 -04004836 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4837 if (mode & FALLOC_FL_KEEP_SIZE)
4838 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4839
Jan Kara17048e82015-12-07 14:29:17 -05004840 /* Wait all existing dio workers, newcomers will block on i_mutex */
4841 ext4_inode_block_unlocked_dio(inode);
4842 inode_dio_wait(inode);
4843
Lukas Czerner0f2af212015-04-03 00:09:13 -04004844 /* Preallocate the range including the unaligned edges */
4845 if (partial_begin || partial_end) {
4846 ret = ext4_alloc_file_blocks(file,
4847 round_down(offset, 1 << blkbits) >> blkbits,
4848 (round_up((offset + len), 1 << blkbits) -
4849 round_down(offset, 1 << blkbits)) >> blkbits,
4850 new_size, flags, mode);
4851 if (ret)
Jan Kara17048e82015-12-07 14:29:17 -05004852 goto out_dio;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004853
4854 }
4855
4856 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004857 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004858 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4859 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004860
Jan Karaea3d7202015-12-07 14:28:03 -05004861 /*
4862 * Prevent page faults from reinstantiating pages we have
4863 * released from page cache.
4864 */
4865 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara01127842015-12-07 14:34:49 -05004866 ret = ext4_update_disksize_before_punch(inode, offset, len);
4867 if (ret) {
4868 up_write(&EXT4_I(inode)->i_mmap_sem);
4869 goto out_dio;
4870 }
Jan Karaea3d7202015-12-07 14:28:03 -05004871 /* Now release the pages and zero block aligned part of pages */
4872 truncate_pagecache_range(inode, start, end - 1);
4873 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4874
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004875 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4876 flags, mode);
Jan Karaea3d7202015-12-07 14:28:03 -05004877 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004878 if (ret)
4879 goto out_dio;
4880 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004881 if (!partial_begin && !partial_end)
4882 goto out_dio;
4883
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004884 /*
4885 * In worst case we have to writeout two nonadjacent unwritten
4886 * blocks and update the inode
4887 */
4888 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4889 if (ext4_should_journal_data(inode))
4890 credits += 2;
4891 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004892 if (IS_ERR(handle)) {
4893 ret = PTR_ERR(handle);
4894 ext4_std_error(inode->i_sb, ret);
4895 goto out_dio;
4896 }
4897
4898 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Lukas Czernere5b30412014-04-01 00:59:21 -04004899 if (new_size) {
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004900 ext4_update_inode_size(inode, new_size);
Lukas Czernere5b30412014-04-01 00:59:21 -04004901 } else {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004902 /*
4903 * Mark that we allocate beyond EOF so the subsequent truncate
4904 * can proceed even if the new size is the same as i_size.
4905 */
4906 if ((offset + len) > i_size_read(inode))
4907 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4908 }
Lukas Czernerb8a86842014-03-18 18:05:35 -04004909 ext4_mark_inode_dirty(handle, inode);
4910
4911 /* Zero out partial block at the edges of the range */
4912 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
Jan Karac404f0d2017-05-29 13:24:55 -04004913 if (ret >= 0)
4914 ext4_update_inode_fsync_trans(handle, inode, 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004915
4916 if (file->f_flags & O_SYNC)
4917 ext4_handle_sync(handle);
4918
4919 ext4_journal_stop(handle);
4920out_dio:
4921 ext4_inode_resume_unlocked_dio(inode);
4922out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004923 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004924 return ret;
4925}
4926
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004927/*
4928 * preallocate space for a file. This implements ext4's fallocate file
4929 * operation, which gets called from sys_fallocate system call.
4930 * For block-mapped files, posix_fallocate should fall back to the method
4931 * of writing zeroes to the required new blocks (the same behavior which is
4932 * expected for file systems which do not support fallocate() system call).
4933 */
4934long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4935{
4936 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004937 loff_t new_size = 0;
4938 unsigned int max_blocks;
4939 int ret = 0;
4940 int flags;
4941 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004942 unsigned int blkbits = inode->i_blkbits;
4943
Michael Halcrow2058f832015-04-12 00:55:10 -04004944 /*
4945 * Encrypted inodes can't handle collapse range or insert
4946 * range since we would need to re-encrypt blocks with a
4947 * different IV or XTS tweak (which are based on the logical
4948 * block number).
4949 *
4950 * XXX It's not clear why zero range isn't working, but we'll
4951 * leave it disabled for encrypted inodes for now. This is a
4952 * bug we should fix....
4953 */
Chandan Rajendrae68f6b82018-12-12 15:20:10 +05304954 if (IS_ENCRYPTED(inode) &&
Namjae Jeon331573f2015-06-09 01:55:03 -04004955 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
4956 FALLOC_FL_ZERO_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004957 return -EOPNOTSUPP;
4958
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004959 /* Return error if mode is not supported */
4960 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004961 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4962 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004963 return -EOPNOTSUPP;
4964
4965 if (mode & FALLOC_FL_PUNCH_HOLE)
4966 return ext4_punch_hole(inode, offset, len);
4967
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004968 ret = ext4_convert_inline_data(inode);
4969 if (ret)
4970 return ret;
4971
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004972 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4973 return ext4_collapse_range(inode, offset, len);
4974
Namjae Jeon331573f2015-06-09 01:55:03 -04004975 if (mode & FALLOC_FL_INSERT_RANGE)
4976 return ext4_insert_range(inode, offset, len);
4977
Lukas Czernerb8a86842014-03-18 18:05:35 -04004978 if (mode & FALLOC_FL_ZERO_RANGE)
4979 return ext4_zero_range(file, offset, len, mode);
4980
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004981 trace_ext4_fallocate_enter(inode, offset, len, mode);
4982 lblk = offset >> blkbits;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004983
Fabian Frederick518eaa62016-09-15 11:55:01 -04004984 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
Lukas Czerner556615d2014-04-20 23:45:47 -04004985 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004986 if (mode & FALLOC_FL_KEEP_SIZE)
4987 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4988
Al Viro59551022016-01-22 15:40:57 -05004989 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004990
Davide Italiano280227a2015-05-02 23:21:15 -04004991 /*
4992 * We only support preallocation for extent-based files only
4993 */
4994 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4995 ret = -EOPNOTSUPP;
4996 goto out;
4997 }
4998
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004999 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Theodore Ts'o9cdd4bf2017-10-06 23:09:55 -04005000 (offset + len > i_size_read(inode) ||
5001 offset + len > EXT4_I(inode)->i_disksize)) {
Lukas Czerner0e8b6872014-03-18 18:03:51 -04005002 new_size = offset + len;
5003 ret = inode_newsize_ok(inode, new_size);
5004 if (ret)
5005 goto out;
5006 }
5007
Jan Kara17048e82015-12-07 14:29:17 -05005008 /* Wait all existing dio workers, newcomers will block on i_mutex */
5009 ext4_inode_block_unlocked_dio(inode);
5010 inode_dio_wait(inode);
5011
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04005012 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
5013 flags, mode);
Jan Kara17048e82015-12-07 14:29:17 -05005014 ext4_inode_resume_unlocked_dio(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04005015 if (ret)
5016 goto out;
5017
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04005018 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
5019 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5020 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04005021 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04005022out:
Al Viro59551022016-01-22 15:40:57 -05005023 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04005024 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
5025 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04005026}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005027
5028/*
Mingming Cao00314622009-09-28 15:49:08 -04005029 * This function convert a range of blocks to written extents
5030 * The caller of this function will pass the start offset and the size.
5031 * all unwritten extents within this range will be converted to
5032 * written extents.
5033 *
5034 * This function is called from the direct IO end io call back
5035 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05005036 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04005037 */
Jan Kara6b523df2013-06-04 13:21:11 -04005038int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
5039 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04005040{
Mingming Cao00314622009-09-28 15:49:08 -04005041 unsigned int max_blocks;
5042 int ret = 0;
5043 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005044 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04005045 unsigned int credits, blkbits = inode->i_blkbits;
5046
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005047 map.m_lblk = offset >> blkbits;
Fabian Frederick518eaa62016-09-15 11:55:01 -04005048 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
5049
Mingming Cao00314622009-09-28 15:49:08 -04005050 /*
Jan Kara6b523df2013-06-04 13:21:11 -04005051 * This is somewhat ugly but the idea is clear: When transaction is
5052 * reserved, everything goes into it. Otherwise we rather start several
5053 * smaller transactions for conversion of each extent separately.
Mingming Cao00314622009-09-28 15:49:08 -04005054 */
Jan Kara6b523df2013-06-04 13:21:11 -04005055 if (handle) {
5056 handle = ext4_journal_start_reserved(handle,
5057 EXT4_HT_EXT_CONVERT);
5058 if (IS_ERR(handle))
5059 return PTR_ERR(handle);
5060 credits = 0;
5061 } else {
5062 /*
5063 * credits to insert 1 extent into extent tree
5064 */
5065 credits = ext4_chunk_trans_blocks(inode, max_blocks);
5066 }
Mingming Cao00314622009-09-28 15:49:08 -04005067 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005068 map.m_lblk += ret;
5069 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04005070 if (credits) {
5071 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5072 credits);
5073 if (IS_ERR(handle)) {
5074 ret = PTR_ERR(handle);
5075 break;
5076 }
Mingming Cao00314622009-09-28 15:49:08 -04005077 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005078 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05005079 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05005080 if (ret <= 0)
5081 ext4_warning(inode->i_sb,
5082 "inode #%lu: block %u: len %u: "
5083 "ext4_ext_map_blocks returned %d",
5084 inode->i_ino, map.m_lblk,
5085 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04005086 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04005087 if (credits)
5088 ret2 = ext4_journal_stop(handle);
5089 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04005090 break;
5091 }
Jan Kara6b523df2013-06-04 13:21:11 -04005092 if (!credits)
5093 ret2 = ext4_journal_stop(handle);
Mingming Cao00314622009-09-28 15:49:08 -04005094 return ret > 0 ? ret2 : ret;
5095}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005096
Mingming Cao00314622009-09-28 15:49:08 -04005097/*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005098 * If newes is not existing extent (newes->ec_pblk equals zero) find
5099 * delayed extent at start of newes and update newes accordingly and
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005100 * return start of the next delayed extent.
5101 *
Zheng Liu69eb33d2013-02-18 00:31:07 -05005102 * If newes is existing extent (newes->ec_pblk is not equal zero)
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005103 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
Zheng Liu69eb33d2013-02-18 00:31:07 -05005104 * extent found. Leave newes unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005105 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005106static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -05005107 struct extent_status *newes)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005108{
Zheng Liub3aff3e2012-11-08 21:57:37 -05005109 struct extent_status es;
Zheng Liube401362013-02-18 00:27:26 -05005110 ext4_lblk_t block, next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005111
Zheng Liu69eb33d2013-02-18 00:31:07 -05005112 if (newes->es_pblk == 0) {
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005113 ext4_es_find_delayed_extent_range(inode, newes->es_lblk,
5114 newes->es_lblk + newes->es_len - 1, &es);
5115
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005116 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005117 * No extent in extent-tree contains block @newes->es_pblk,
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005118 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005119 */
Zheng Liu06b0c882013-02-18 00:26:51 -05005120 if (es.es_len == 0)
Zheng Liub3aff3e2012-11-08 21:57:37 -05005121 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005122 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005123
Zheng Liu69eb33d2013-02-18 00:31:07 -05005124 if (es.es_lblk > newes->es_lblk) {
Zheng Liub3aff3e2012-11-08 21:57:37 -05005125 /* A hole found. */
Zheng Liu69eb33d2013-02-18 00:31:07 -05005126 newes->es_len = min(es.es_lblk - newes->es_lblk,
5127 newes->es_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005128 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005129 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005130
Zheng Liu69eb33d2013-02-18 00:31:07 -05005131 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005132 }
5133
Zheng Liu69eb33d2013-02-18 00:31:07 -05005134 block = newes->es_lblk + newes->es_len;
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005135 ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es);
Zheng Liube401362013-02-18 00:27:26 -05005136 if (es.es_len == 0)
5137 next_del = EXT_MAX_BLOCKS;
5138 else
5139 next_del = es.es_lblk;
5140
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005141 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005142}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005143/* fiemap flags we can handle specified here */
5144#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
5145
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05005146static int ext4_xattr_fiemap(struct inode *inode,
5147 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005148{
5149 __u64 physical = 0;
5150 __u64 length;
5151 __u32 flags = FIEMAP_EXTENT_LAST;
5152 int blockbits = inode->i_sb->s_blocksize_bits;
5153 int error = 0;
5154
5155 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005156 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04005157 struct ext4_iloc iloc;
5158 int offset; /* offset of xattr in inode */
5159
5160 error = ext4_get_inode_loc(inode, &iloc);
5161 if (error)
5162 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04005163 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005164 offset = EXT4_GOOD_OLD_INODE_SIZE +
5165 EXT4_I(inode)->i_extra_isize;
5166 physical += offset;
5167 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5168 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005169 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005170 } else { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04005171 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005172 length = inode->i_sb->s_blocksize;
5173 }
5174
5175 if (physical)
5176 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5177 length, flags);
5178 return (error < 0 ? error : 0);
5179}
5180
5181int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5182 __u64 start, __u64 len)
5183{
5184 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005185 int error = 0;
5186
Tao Ma94191982012-12-10 14:06:02 -05005187 if (ext4_has_inline_data(inode)) {
5188 int has_inline = 1;
5189
Dmitry Monakhovd952d692014-12-02 16:11:20 -05005190 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5191 start, len);
Tao Ma94191982012-12-10 14:06:02 -05005192
5193 if (has_inline)
5194 return error;
5195 }
5196
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005197 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5198 error = ext4_ext_precache(inode);
5199 if (error)
5200 return error;
5201 }
5202
Eric Sandeen6873fa02008-10-07 00:46:36 -04005203 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005204 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Theodore Ts'oad7fefb2015-01-02 15:16:00 -05005205 return generic_block_fiemap(inode, fieinfo, start, len,
5206 ext4_get_block);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005207
5208 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
5209 return -EBADR;
5210
5211 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5212 error = ext4_xattr_fiemap(inode, fieinfo);
5213 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005214 ext4_lblk_t len_blks;
5215 __u64 last_blk;
5216
Eric Sandeen6873fa02008-10-07 00:46:36 -04005217 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005218 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04005219 if (last_blk >= EXT_MAX_BLOCKS)
5220 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005221 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005222
5223 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005224 * Walk the extent tree gathering extent information
5225 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005226 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005227 error = ext4_fill_fiemap_extents(inode, start_blk,
5228 len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005229 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04005230 return error;
5231}
Namjae Jeon9eb79482014-02-23 15:18:59 -05005232
5233/*
5234 * ext4_access_path:
5235 * Function to access the path buffer for marking it dirty.
5236 * It also checks if there are sufficient credits left in the journal handle
5237 * to update path.
5238 */
5239static int
5240ext4_access_path(handle_t *handle, struct inode *inode,
5241 struct ext4_ext_path *path)
5242{
5243 int credits, err;
5244
5245 if (!ext4_handle_valid(handle))
5246 return 0;
5247
5248 /*
5249 * Check if need to extend journal credits
5250 * 3 for leaf, sb, and inode plus 2 (bmap and group
5251 * descriptor) for each block group; assume two block
5252 * groups
5253 */
5254 if (handle->h_buffer_credits < 7) {
5255 credits = ext4_writepage_trans_blocks(inode);
5256 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
5257 /* EAGAIN is success */
5258 if (err && err != -EAGAIN)
5259 return err;
5260 }
5261
5262 err = ext4_ext_get_access(handle, inode, path);
5263 return err;
5264}
5265
5266/*
5267 * ext4_ext_shift_path_extents:
5268 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04005269 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5270 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005271 */
5272static int
5273ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5274 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005275 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005276{
5277 int depth, err = 0;
5278 struct ext4_extent *ex_start, *ex_last;
5279 bool update = 0;
5280 depth = path->p_depth;
5281
5282 while (depth >= 0) {
5283 if (depth == path->p_depth) {
5284 ex_start = path[depth].p_ext;
5285 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005286 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005287
5288 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005289
5290 err = ext4_access_path(handle, inode, path + depth);
5291 if (err)
5292 goto out;
5293
5294 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5295 update = 1;
5296
Namjae Jeon9eb79482014-02-23 15:18:59 -05005297 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005298 if (SHIFT == SHIFT_LEFT) {
5299 le32_add_cpu(&ex_start->ee_block,
5300 -shift);
5301 /* Try to merge to the left. */
5302 if ((ex_start >
5303 EXT_FIRST_EXTENT(path[depth].p_hdr))
5304 &&
5305 ext4_ext_try_to_merge_right(inode,
5306 path, ex_start - 1))
5307 ex_last--;
5308 else
5309 ex_start++;
5310 } else {
5311 le32_add_cpu(&ex_last->ee_block, shift);
5312 ext4_ext_try_to_merge_right(inode, path,
5313 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005314 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005315 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005316 }
5317 err = ext4_ext_dirty(handle, inode, path + depth);
5318 if (err)
5319 goto out;
5320
5321 if (--depth < 0 || !update)
5322 break;
5323 }
5324
5325 /* Update index too */
5326 err = ext4_access_path(handle, inode, path + depth);
5327 if (err)
5328 goto out;
5329
Namjae Jeon331573f2015-06-09 01:55:03 -04005330 if (SHIFT == SHIFT_LEFT)
5331 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5332 else
5333 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005334 err = ext4_ext_dirty(handle, inode, path + depth);
5335 if (err)
5336 goto out;
5337
5338 /* we are done if current index is not a starting index */
5339 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5340 break;
5341
5342 depth--;
5343 }
5344
5345out:
5346 return err;
5347}
5348
5349/*
5350 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005351 * All the extents which lies in the range from @start to the last allocated
5352 * block for the @inode are shifted either towards left or right (depending
5353 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005354 * On success, 0 is returned, error otherwise.
5355 */
5356static int
5357ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005358 ext4_lblk_t start, ext4_lblk_t shift,
5359 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005360{
5361 struct ext4_ext_path *path;
5362 int ret = 0, depth;
5363 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005364 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005365
5366 /* Let path point to the last extent */
Roman Pene0b53d62017-01-08 21:00:35 -05005367 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5368 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005369 if (IS_ERR(path))
5370 return PTR_ERR(path);
5371
5372 depth = path->p_depth;
5373 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005374 if (!extent)
5375 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005376
Roman Pen72ae4762017-01-08 20:59:35 -05005377 stop = le32_to_cpu(extent->ee_block);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005378
Namjae Jeon331573f2015-06-09 01:55:03 -04005379 /*
Eric Biggersa405a2a2018-04-12 11:48:09 -04005380 * For left shifts, make sure the hole on the left is big enough to
5381 * accommodate the shift. For right shifts, make sure the last extent
5382 * won't be shifted beyond EXT_MAX_BLOCKS.
Namjae Jeon331573f2015-06-09 01:55:03 -04005383 */
5384 if (SHIFT == SHIFT_LEFT) {
Roman Pene0b53d62017-01-08 21:00:35 -05005385 path = ext4_find_extent(inode, start - 1, &path,
5386 EXT4_EX_NOCACHE);
Namjae Jeon331573f2015-06-09 01:55:03 -04005387 if (IS_ERR(path))
5388 return PTR_ERR(path);
5389 depth = path->p_depth;
5390 extent = path[depth].p_ext;
5391 if (extent) {
5392 ex_start = le32_to_cpu(extent->ee_block);
5393 ex_end = le32_to_cpu(extent->ee_block) +
5394 ext4_ext_get_actual_len(extent);
5395 } else {
5396 ex_start = 0;
5397 ex_end = 0;
5398 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005399
Namjae Jeon331573f2015-06-09 01:55:03 -04005400 if ((start == ex_start && shift > ex_start) ||
5401 (shift > start - ex_end)) {
Eric Biggersa405a2a2018-04-12 11:48:09 -04005402 ret = -EINVAL;
5403 goto out;
5404 }
5405 } else {
5406 if (shift > EXT_MAX_BLOCKS -
5407 (stop + ext4_ext_get_actual_len(extent))) {
5408 ret = -EINVAL;
5409 goto out;
Namjae Jeon331573f2015-06-09 01:55:03 -04005410 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005411 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005412
Namjae Jeon331573f2015-06-09 01:55:03 -04005413 /*
5414 * In case of left shift, iterator points to start and it is increased
5415 * till we reach stop. In case of right shift, iterator points to stop
5416 * and it is decreased till we reach start.
5417 */
5418 if (SHIFT == SHIFT_LEFT)
5419 iterator = &start;
5420 else
5421 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005422
Roman Pen72ae4762017-01-08 20:59:35 -05005423 /*
5424 * Its safe to start updating extents. Start and stop are unsigned, so
5425 * in case of right shift if extent with 0 block is reached, iterator
5426 * becomes NULL to indicate the end of the loop.
5427 */
5428 while (iterator && start <= stop) {
Roman Pene0b53d62017-01-08 21:00:35 -05005429 path = ext4_find_extent(inode, *iterator, &path,
5430 EXT4_EX_NOCACHE);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005431 if (IS_ERR(path))
5432 return PTR_ERR(path);
5433 depth = path->p_depth;
5434 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005435 if (!extent) {
5436 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005437 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005438 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005439 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005440 if (SHIFT == SHIFT_LEFT && *iterator >
5441 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005442 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005443 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5444 path[depth].p_ext++;
5445 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005446 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005447 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005448 }
5449 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005450
5451 if (SHIFT == SHIFT_LEFT) {
5452 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5453 *iterator = le32_to_cpu(extent->ee_block) +
5454 ext4_ext_get_actual_len(extent);
5455 } else {
5456 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
Roman Pen72ae4762017-01-08 20:59:35 -05005457 if (le32_to_cpu(extent->ee_block) > 0)
5458 *iterator = le32_to_cpu(extent->ee_block) - 1;
5459 else
5460 /* Beginning is reached, end of the loop */
5461 iterator = NULL;
Namjae Jeon331573f2015-06-09 01:55:03 -04005462 /* Update path extent in case we need to stop */
5463 while (le32_to_cpu(extent->ee_block) < start)
5464 extent++;
5465 path[depth].p_ext = extent;
5466 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005467 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005468 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005469 if (ret)
5470 break;
5471 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005472out:
5473 ext4_ext_drop_refs(path);
5474 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005475 return ret;
5476}
5477
5478/*
5479 * ext4_collapse_range:
5480 * This implements the fallocate's collapse range functionality for ext4
5481 * Returns: 0 and non-zero on error.
5482 */
5483int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5484{
5485 struct super_block *sb = inode->i_sb;
5486 ext4_lblk_t punch_start, punch_stop;
5487 handle_t *handle;
5488 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005489 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005490 int ret;
5491
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005492 /*
5493 * We need to test this early because xfstests assumes that a
5494 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5495 * system does not support collapse range.
5496 */
5497 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5498 return -EOPNOTSUPP;
5499
Namjae Jeon9eb79482014-02-23 15:18:59 -05005500 /* Collapse range works only on fs block size aligned offsets. */
Namjae Jeonee98fa32014-07-29 10:45:31 -04005501 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5502 len & (EXT4_CLUSTER_SIZE(sb) - 1))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005503 return -EINVAL;
5504
5505 if (!S_ISREG(inode->i_mode))
Theodore Ts'o86f1ca32014-04-18 11:52:11 -04005506 return -EINVAL;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005507
5508 trace_ext4_collapse_range(inode, offset, len);
5509
5510 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5511 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5512
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005513 /* Call ext4_force_commit to flush all data in case of data=journal. */
5514 if (ext4_should_journal_data(inode)) {
5515 ret = ext4_force_commit(inode->i_sb);
5516 if (ret)
5517 return ret;
5518 }
5519
Al Viro59551022016-01-22 15:40:57 -05005520 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005521 /*
5522 * There is no need to overlap collapse range with EOF, in which case
5523 * it is effectively a truncate operation
5524 */
5525 if (offset + len >= i_size_read(inode)) {
5526 ret = -EINVAL;
5527 goto out_mutex;
5528 }
5529
Namjae Jeon9eb79482014-02-23 15:18:59 -05005530 /* Currently just for extent based files */
5531 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5532 ret = -EOPNOTSUPP;
5533 goto out_mutex;
5534 }
5535
Namjae Jeon9eb79482014-02-23 15:18:59 -05005536 /* Wait for existing dio to complete */
5537 ext4_inode_block_unlocked_dio(inode);
5538 inode_dio_wait(inode);
5539
Jan Karaea3d7202015-12-07 14:28:03 -05005540 /*
5541 * Prevent page faults from reinstantiating pages we have released from
5542 * page cache.
5543 */
5544 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara32ebffd2015-12-07 14:31:11 -05005545 /*
5546 * Need to round down offset to be aligned with page size boundary
5547 * for page size > block size.
5548 */
5549 ioffset = round_down(offset, PAGE_SIZE);
5550 /*
5551 * Write tail of the last page before removed range since it will get
5552 * removed from the page cache below.
5553 */
5554 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5555 if (ret)
5556 goto out_mmap;
5557 /*
5558 * Write data that will be shifted to preserve them when discarding
5559 * page cache below. We are also protected from pages becoming dirty
5560 * by i_mmap_sem.
5561 */
5562 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5563 LLONG_MAX);
5564 if (ret)
5565 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005566 truncate_pagecache(inode, ioffset);
5567
Namjae Jeon9eb79482014-02-23 15:18:59 -05005568 credits = ext4_writepage_trans_blocks(inode);
5569 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5570 if (IS_ERR(handle)) {
5571 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005572 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005573 }
5574
5575 down_write(&EXT4_I(inode)->i_data_sem);
5576 ext4_discard_preallocations(inode);
5577
5578 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005579 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005580 if (ret) {
5581 up_write(&EXT4_I(inode)->i_data_sem);
5582 goto out_stop;
5583 }
5584
5585 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5586 if (ret) {
5587 up_write(&EXT4_I(inode)->i_data_sem);
5588 goto out_stop;
5589 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005590 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005591
5592 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005593 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005594 if (ret) {
5595 up_write(&EXT4_I(inode)->i_data_sem);
5596 goto out_stop;
5597 }
5598
5599 new_size = i_size_read(inode) - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005600 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005601 EXT4_I(inode)->i_disksize = new_size;
5602
Namjae Jeon9eb79482014-02-23 15:18:59 -05005603 up_write(&EXT4_I(inode)->i_data_sem);
5604 if (IS_SYNC(inode))
5605 ext4_handle_sync(handle);
5606 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
5607 ext4_mark_inode_dirty(handle, inode);
Jan Karac404f0d2017-05-29 13:24:55 -04005608 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005609
5610out_stop:
5611 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005612out_mmap:
5613 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005614 ext4_inode_resume_unlocked_dio(inode);
5615out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005616 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005617 return ret;
5618}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005619
Namjae Jeon331573f2015-06-09 01:55:03 -04005620/*
5621 * ext4_insert_range:
5622 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5623 * The data blocks starting from @offset to the EOF are shifted by @len
5624 * towards right to create a hole in the @inode. Inode size is increased
5625 * by len bytes.
5626 * Returns 0 on success, error otherwise.
5627 */
5628int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
5629{
5630 struct super_block *sb = inode->i_sb;
5631 handle_t *handle;
5632 struct ext4_ext_path *path;
5633 struct ext4_extent *extent;
5634 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5635 unsigned int credits, ee_len;
5636 int ret = 0, depth, split_flag = 0;
5637 loff_t ioffset;
5638
5639 /*
5640 * We need to test this early because xfstests assumes that an
5641 * insert range of (0, 1) will return EOPNOTSUPP if the file
5642 * system does not support insert range.
5643 */
5644 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5645 return -EOPNOTSUPP;
5646
5647 /* Insert range works only on fs block size aligned offsets. */
5648 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5649 len & (EXT4_CLUSTER_SIZE(sb) - 1))
5650 return -EINVAL;
5651
5652 if (!S_ISREG(inode->i_mode))
5653 return -EOPNOTSUPP;
5654
5655 trace_ext4_insert_range(inode, offset, len);
5656
5657 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5658 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5659
5660 /* Call ext4_force_commit to flush all data in case of data=journal */
5661 if (ext4_should_journal_data(inode)) {
5662 ret = ext4_force_commit(inode->i_sb);
5663 if (ret)
5664 return ret;
5665 }
5666
Al Viro59551022016-01-22 15:40:57 -05005667 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005668 /* Currently just for extent based files */
5669 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5670 ret = -EOPNOTSUPP;
5671 goto out_mutex;
5672 }
5673
5674 /* Check for wrap through zero */
5675 if (inode->i_size + len > inode->i_sb->s_maxbytes) {
5676 ret = -EFBIG;
5677 goto out_mutex;
5678 }
5679
5680 /* Offset should be less than i_size */
5681 if (offset >= i_size_read(inode)) {
5682 ret = -EINVAL;
5683 goto out_mutex;
5684 }
5685
Namjae Jeon331573f2015-06-09 01:55:03 -04005686 /* Wait for existing dio to complete */
5687 ext4_inode_block_unlocked_dio(inode);
5688 inode_dio_wait(inode);
5689
Jan Karaea3d7202015-12-07 14:28:03 -05005690 /*
5691 * Prevent page faults from reinstantiating pages we have released from
5692 * page cache.
5693 */
5694 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara32ebffd2015-12-07 14:31:11 -05005695 /*
5696 * Need to round down to align start offset to page size boundary
5697 * for page size > block size.
5698 */
5699 ioffset = round_down(offset, PAGE_SIZE);
5700 /* Write out all dirty pages */
5701 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5702 LLONG_MAX);
5703 if (ret)
5704 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005705 truncate_pagecache(inode, ioffset);
5706
Namjae Jeon331573f2015-06-09 01:55:03 -04005707 credits = ext4_writepage_trans_blocks(inode);
5708 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5709 if (IS_ERR(handle)) {
5710 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005711 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005712 }
5713
5714 /* Expand file to avoid data loss if there is error while shifting */
5715 inode->i_size += len;
5716 EXT4_I(inode)->i_disksize += len;
5717 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
5718 ret = ext4_mark_inode_dirty(handle, inode);
5719 if (ret)
5720 goto out_stop;
5721
5722 down_write(&EXT4_I(inode)->i_data_sem);
5723 ext4_discard_preallocations(inode);
5724
5725 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5726 if (IS_ERR(path)) {
5727 up_write(&EXT4_I(inode)->i_data_sem);
5728 goto out_stop;
5729 }
5730
5731 depth = ext_depth(inode);
5732 extent = path[depth].p_ext;
5733 if (extent) {
5734 ee_start_lblk = le32_to_cpu(extent->ee_block);
5735 ee_len = ext4_ext_get_actual_len(extent);
5736
5737 /*
5738 * If offset_lblk is not the starting block of extent, split
5739 * the extent @offset_lblk
5740 */
5741 if ((offset_lblk > ee_start_lblk) &&
5742 (offset_lblk < (ee_start_lblk + ee_len))) {
5743 if (ext4_ext_is_unwritten(extent))
5744 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5745 EXT4_EXT_MARK_UNWRIT2;
5746 ret = ext4_split_extent_at(handle, inode, &path,
5747 offset_lblk, split_flag,
5748 EXT4_EX_NOCACHE |
5749 EXT4_GET_BLOCKS_PRE_IO |
5750 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5751 }
5752
5753 ext4_ext_drop_refs(path);
5754 kfree(path);
5755 if (ret < 0) {
5756 up_write(&EXT4_I(inode)->i_data_sem);
5757 goto out_stop;
5758 }
Fabian Frederickedf15aa2016-09-15 11:39:52 -04005759 } else {
5760 ext4_ext_drop_refs(path);
5761 kfree(path);
Namjae Jeon331573f2015-06-09 01:55:03 -04005762 }
5763
5764 ret = ext4_es_remove_extent(inode, offset_lblk,
5765 EXT_MAX_BLOCKS - offset_lblk);
5766 if (ret) {
5767 up_write(&EXT4_I(inode)->i_data_sem);
5768 goto out_stop;
5769 }
5770
5771 /*
5772 * if offset_lblk lies in a hole which is at start of file, use
5773 * ee_start_lblk to shift extents
5774 */
5775 ret = ext4_ext_shift_extents(inode, handle,
5776 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5777 len_lblk, SHIFT_RIGHT);
5778
5779 up_write(&EXT4_I(inode)->i_data_sem);
5780 if (IS_SYNC(inode))
5781 ext4_handle_sync(handle);
Jan Karac404f0d2017-05-29 13:24:55 -04005782 if (ret >= 0)
5783 ext4_update_inode_fsync_trans(handle, inode, 1);
Namjae Jeon331573f2015-06-09 01:55:03 -04005784
5785out_stop:
5786 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005787out_mmap:
5788 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005789 ext4_inode_resume_unlocked_dio(inode);
5790out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005791 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005792 return ret;
5793}
5794
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005795/**
5796 * ext4_swap_extents - Swap extents between two inodes
5797 *
5798 * @inode1: First inode
5799 * @inode2: Second inode
5800 * @lblk1: Start block for first inode
5801 * @lblk2: Start block for second inode
5802 * @count: Number of blocks to swap
5803 * @mark_unwritten: Mark second inode's extents as unwritten after swap
5804 * @erp: Pointer to save error value
5805 *
5806 * This helper routine does exactly what is promise "swap extents". All other
5807 * stuff such as page-cache locking consistency, bh mapping consistency or
5808 * extent's data copying must be performed by caller.
5809 * Locking:
5810 * i_mutex is held for both inodes
5811 * i_data_sem is locked for write for both inodes
5812 * Assumptions:
5813 * All pages from requested range are locked for both inodes
5814 */
5815int
5816ext4_swap_extents(handle_t *handle, struct inode *inode1,
5817 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
5818 ext4_lblk_t count, int unwritten, int *erp)
5819{
5820 struct ext4_ext_path *path1 = NULL;
5821 struct ext4_ext_path *path2 = NULL;
5822 int replaced_count = 0;
5823
5824 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5825 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005826 BUG_ON(!inode_is_locked(inode1));
5827 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005828
5829 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005830 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005831 return 0;
5832 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005833 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005834 return 0;
5835
5836 while (count) {
5837 struct ext4_extent *ex1, *ex2, tmp_ex;
5838 ext4_lblk_t e1_blk, e2_blk;
5839 int e1_len, e2_len, len;
5840 int split = 0;
5841
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005842 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305843 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005844 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005845 path1 = NULL;
5846 finish:
5847 count = 0;
5848 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005849 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005850 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305851 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005852 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005853 path2 = NULL;
5854 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005855 }
5856 ex1 = path1[path1->p_depth].p_ext;
5857 ex2 = path2[path2->p_depth].p_ext;
5858 /* Do we have somthing to swap ? */
5859 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005860 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005861
5862 e1_blk = le32_to_cpu(ex1->ee_block);
5863 e2_blk = le32_to_cpu(ex2->ee_block);
5864 e1_len = ext4_ext_get_actual_len(ex1);
5865 e2_len = ext4_ext_get_actual_len(ex2);
5866
5867 /* Hole handling */
5868 if (!in_range(lblk1, e1_blk, e1_len) ||
5869 !in_range(lblk2, e2_blk, e2_len)) {
5870 ext4_lblk_t next1, next2;
5871
5872 /* if hole after extent, then go to next extent */
5873 next1 = ext4_ext_next_allocated_block(path1);
5874 next2 = ext4_ext_next_allocated_block(path2);
5875 /* If hole before extent, then shift to that extent */
5876 if (e1_blk > lblk1)
5877 next1 = e1_blk;
5878 if (e2_blk > lblk2)
5879 next2 = e1_blk;
5880 /* Do we have something to swap */
5881 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005882 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005883 /* Move to the rightest boundary */
5884 len = next1 - lblk1;
5885 if (len < next2 - lblk2)
5886 len = next2 - lblk2;
5887 if (len > count)
5888 len = count;
5889 lblk1 += len;
5890 lblk2 += len;
5891 count -= len;
5892 goto repeat;
5893 }
5894
5895 /* Prepare left boundary */
5896 if (e1_blk < lblk1) {
5897 split = 1;
5898 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005899 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005900 if (unlikely(*erp))
5901 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005902 }
5903 if (e2_blk < lblk2) {
5904 split = 1;
5905 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005906 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005907 if (unlikely(*erp))
5908 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005909 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005910 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005911 * path must to be revalidated. */
5912 if (split)
5913 goto repeat;
5914
5915 /* Prepare right boundary */
5916 len = count;
5917 if (len > e1_blk + e1_len - lblk1)
5918 len = e1_blk + e1_len - lblk1;
5919 if (len > e2_blk + e2_len - lblk2)
5920 len = e2_blk + e2_len - lblk2;
5921
5922 if (len != e1_len) {
5923 split = 1;
5924 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005925 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005926 if (unlikely(*erp))
5927 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005928 }
5929 if (len != e2_len) {
5930 split = 1;
5931 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005932 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005933 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005934 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005935 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005936 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005937 * path must to be revalidated. */
5938 if (split)
5939 goto repeat;
5940
5941 BUG_ON(e2_len != e1_len);
5942 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005943 if (unlikely(*erp))
5944 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005945 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005946 if (unlikely(*erp))
5947 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005948
5949 /* Both extents are fully inside boundaries. Swap it now */
5950 tmp_ex = *ex1;
5951 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5952 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5953 ex1->ee_len = cpu_to_le16(e2_len);
5954 ex2->ee_len = cpu_to_le16(e1_len);
5955 if (unwritten)
5956 ext4_ext_mark_unwritten(ex2);
5957 if (ext4_ext_is_unwritten(&tmp_ex))
5958 ext4_ext_mark_unwritten(ex1);
5959
5960 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5961 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5962 *erp = ext4_ext_dirty(handle, inode2, path2 +
5963 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005964 if (unlikely(*erp))
5965 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005966 *erp = ext4_ext_dirty(handle, inode1, path1 +
5967 path1->p_depth);
5968 /*
5969 * Looks scarry ah..? second inode already points to new blocks,
5970 * and it was successfully dirtied. But luckily error may happen
5971 * only due to journal error, so full transaction will be
5972 * aborted anyway.
5973 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005974 if (unlikely(*erp))
5975 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005976 lblk1 += len;
5977 lblk2 += len;
5978 replaced_count += len;
5979 count -= len;
5980
5981 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005982 ext4_ext_drop_refs(path1);
5983 kfree(path1);
5984 ext4_ext_drop_refs(path2);
5985 kfree(path2);
5986 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005987 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005988 return replaced_count;
5989}