blob: ba2be53a61d242765ae4c9535281007eeb00fe86 [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 Monakhov9aa5d32b2014-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 Monakhov9aa5d32b2014-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);
384 ext4_lblk_t last = lblock + len - 1;
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400385
Eryu Guan2f974862015-05-14 19:00:45 -0400386 if (len == 0 || lblock > last)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400387 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400388 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400389}
390
391static int ext4_valid_extent_idx(struct inode *inode,
392 struct ext4_extent_idx *ext_idx)
393{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400394 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400395
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400396 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400397}
398
399static int ext4_valid_extent_entries(struct inode *inode,
400 struct ext4_extent_header *eh,
401 int depth)
402{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400403 unsigned short entries;
404 if (eh->eh_entries == 0)
405 return 1;
406
407 entries = le16_to_cpu(eh->eh_entries);
408
409 if (depth == 0) {
410 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400411 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500412 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
413 ext4_fsblk_t pblock = 0;
414 ext4_lblk_t lblock = 0;
415 ext4_lblk_t prev = 0;
416 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400417 while (entries) {
418 if (!ext4_valid_extent(inode, ext))
419 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500420
421 /* Check for overlapping extents */
422 lblock = le32_to_cpu(ext->ee_block);
423 len = ext4_ext_get_actual_len(ext);
424 if ((lblock <= prev) && prev) {
425 pblock = ext4_ext_pblock(ext);
426 es->s_last_error_block = cpu_to_le64(pblock);
427 return 0;
428 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400429 ext++;
430 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500431 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400432 }
433 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400434 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400435 while (entries) {
436 if (!ext4_valid_extent_idx(inode, ext_idx))
437 return 0;
438 ext_idx++;
439 entries--;
440 }
441 }
442 return 1;
443}
444
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400445static int __ext4_ext_check(const char *function, unsigned int line,
446 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400447 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400448{
449 const char *error_msg;
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400450 int max = 0, err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400451
452 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
453 error_msg = "invalid magic";
454 goto corrupted;
455 }
456 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
457 error_msg = "unexpected eh_depth";
458 goto corrupted;
459 }
460 if (unlikely(eh->eh_max == 0)) {
461 error_msg = "invalid eh_max";
462 goto corrupted;
463 }
464 max = ext4_ext_max_entries(inode, depth);
465 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
466 error_msg = "too large eh_max";
467 goto corrupted;
468 }
469 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
470 error_msg = "invalid eh_entries";
471 goto corrupted;
472 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400473 if (!ext4_valid_extent_entries(inode, eh, depth)) {
474 error_msg = "invalid extent entries";
475 goto corrupted;
476 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400477 /* Verify checksum on non-root extent tree nodes */
478 if (ext_depth(inode) != depth &&
479 !ext4_extent_block_csum_verify(inode, eh)) {
480 error_msg = "extent tree corrupted";
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400481 err = -EFSBADCRC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400482 goto corrupted;
483 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400484 return 0;
485
486corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400487 ext4_error_inode(inode, function, line, 0,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400488 "pblk %llu bad header/extent: %s - magic %x, "
489 "entries %u, max %u(%u), depth %u(%u)",
490 (unsigned long long) pblk, error_msg,
491 le16_to_cpu(eh->eh_magic),
492 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
493 max, le16_to_cpu(eh->eh_depth), depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400494 return err;
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400495}
496
Theodore Ts'oc3491792013-08-16 21:21:41 -0400497#define ext4_ext_check(inode, eh, depth, pblk) \
498 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400499
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400500int ext4_ext_check_inode(struct inode *inode)
501{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400502 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400503}
504
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400505static struct buffer_head *
506__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400507 struct inode *inode, ext4_fsblk_t pblk, int depth,
508 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400509{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400510 struct buffer_head *bh;
511 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400512
Nikolay Borisovc45653c2015-07-02 01:34:07 -0400513 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400514 if (unlikely(!bh))
515 return ERR_PTR(-ENOMEM);
516
517 if (!bh_uptodate_or_lock(bh)) {
518 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
519 err = bh_submit_read(bh);
520 if (err < 0)
521 goto errout;
522 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400523 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400524 return bh;
525 err = __ext4_ext_check(function, line, inode,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400526 ext_block_hdr(bh), depth, pblk);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400527 if (err)
528 goto errout;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400529 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400530 /*
531 * If this is a leaf block, cache all of its entries
532 */
533 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
534 struct ext4_extent_header *eh = ext_block_hdr(bh);
535 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
536 ext4_lblk_t prev = 0;
537 int i;
538
539 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
540 unsigned int status = EXTENT_STATUS_WRITTEN;
541 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
542 int len = ext4_ext_get_actual_len(ex);
543
544 if (prev && (prev != lblk))
545 ext4_es_cache_extent(inode, prev,
546 lblk - prev, ~0,
547 EXTENT_STATUS_HOLE);
548
Lukas Czerner556615d2014-04-20 23:45:47 -0400549 if (ext4_ext_is_unwritten(ex))
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400550 status = EXTENT_STATUS_UNWRITTEN;
551 ext4_es_cache_extent(inode, lblk, len,
552 ext4_ext_pblock(ex), status);
553 prev = lblk + len;
554 }
555 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400556 return bh;
557errout:
558 put_bh(bh);
559 return ERR_PTR(err);
560
Darrick J. Wongf8489122012-04-29 18:21:10 -0400561}
562
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400563#define read_extent_tree_block(inode, pblk, depth, flags) \
564 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
565 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400566
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400567/*
568 * This function is called to cache a file's extent information in the
569 * extent status tree
570 */
571int ext4_ext_precache(struct inode *inode)
572{
573 struct ext4_inode_info *ei = EXT4_I(inode);
574 struct ext4_ext_path *path = NULL;
575 struct buffer_head *bh;
576 int i = 0, depth, ret = 0;
577
578 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
579 return 0; /* not an extent-mapped inode */
580
581 down_read(&ei->i_data_sem);
582 depth = ext_depth(inode);
583
584 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
585 GFP_NOFS);
586 if (path == NULL) {
587 up_read(&ei->i_data_sem);
588 return -ENOMEM;
589 }
590
591 /* Don't cache anything if there are no external extent blocks */
592 if (depth == 0)
593 goto out;
594 path[0].p_hdr = ext_inode_hdr(inode);
595 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
596 if (ret)
597 goto out;
598 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
599 while (i >= 0) {
600 /*
601 * If this is a leaf block or we've reached the end of
602 * the index block, go up
603 */
604 if ((i == depth) ||
605 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
606 brelse(path[i].p_bh);
607 path[i].p_bh = NULL;
608 i--;
609 continue;
610 }
611 bh = read_extent_tree_block(inode,
612 ext4_idx_pblock(path[i].p_idx++),
613 depth - i - 1,
614 EXT4_EX_FORCE_CACHE);
615 if (IS_ERR(bh)) {
616 ret = PTR_ERR(bh);
617 break;
618 }
619 i++;
620 path[i].p_bh = bh;
621 path[i].p_hdr = ext_block_hdr(bh);
622 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
623 }
624 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
625out:
626 up_read(&ei->i_data_sem);
627 ext4_ext_drop_refs(path);
628 kfree(path);
629 return ret;
630}
631
Alex Tomasa86c6182006-10-11 01:21:03 -0700632#ifdef EXT_DEBUG
633static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
634{
635 int k, l = path->p_depth;
636
637 ext_debug("path:");
638 for (k = 0; k <= l; k++, path++) {
639 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700640 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400641 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700642 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400643 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700644 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400645 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400646 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400647 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700648 } else
649 ext_debug(" []");
650 }
651 ext_debug("\n");
652}
653
654static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
655{
656 int depth = ext_depth(inode);
657 struct ext4_extent_header *eh;
658 struct ext4_extent *ex;
659 int i;
660
661 if (!path)
662 return;
663
664 eh = path[depth].p_hdr;
665 ex = EXT_FIRST_EXTENT(eh);
666
Mingming553f9002009-09-18 13:34:55 -0400667 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
668
Alex Tomasa86c6182006-10-11 01:21:03 -0700669 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400670 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400671 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400672 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700673 }
674 ext_debug("\n");
675}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400676
677static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
678 ext4_fsblk_t newblock, int level)
679{
680 int depth = ext_depth(inode);
681 struct ext4_extent *ex;
682
683 if (depth != level) {
684 struct ext4_extent_idx *idx;
685 idx = path[level].p_idx;
686 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
687 ext_debug("%d: move %d:%llu in new index %llu\n", level,
688 le32_to_cpu(idx->ei_block),
689 ext4_idx_pblock(idx),
690 newblock);
691 idx++;
692 }
693
694 return;
695 }
696
697 ex = path[depth].p_ext;
698 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
699 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
700 le32_to_cpu(ex->ee_block),
701 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400702 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400703 ext4_ext_get_actual_len(ex),
704 newblock);
705 ex++;
706 }
707}
708
Alex Tomasa86c6182006-10-11 01:21:03 -0700709#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400710#define ext4_ext_show_path(inode, path)
711#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400712#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700713#endif
714
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500715void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700716{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400717 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700718
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400719 if (!path)
720 return;
721 depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700722 for (i = 0; i <= depth; i++, path++)
723 if (path->p_bh) {
724 brelse(path->p_bh);
725 path->p_bh = NULL;
726 }
727}
728
729/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700730 * ext4_ext_binsearch_idx:
731 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400732 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700733 */
734static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500735ext4_ext_binsearch_idx(struct inode *inode,
736 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700737{
738 struct ext4_extent_header *eh = path->p_hdr;
739 struct ext4_extent_idx *r, *l, *m;
740
Alex Tomasa86c6182006-10-11 01:21:03 -0700741
Eric Sandeenbba90742008-01-28 23:58:27 -0500742 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700743
744 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400745 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700746 while (l <= r) {
747 m = l + (r - l) / 2;
748 if (block < le32_to_cpu(m->ei_block))
749 r = m - 1;
750 else
751 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400752 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
753 m, le32_to_cpu(m->ei_block),
754 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700755 }
756
757 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400758 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400759 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700760
761#ifdef CHECK_BINSEARCH
762 {
763 struct ext4_extent_idx *chix, *ix;
764 int k;
765
766 chix = ix = EXT_FIRST_INDEX(eh);
767 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
768 if (k != 0 &&
769 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400770 printk(KERN_DEBUG "k=%d, ix=0x%p, "
771 "first=0x%p\n", k,
772 ix, EXT_FIRST_INDEX(eh));
773 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700774 le32_to_cpu(ix->ei_block),
775 le32_to_cpu(ix[-1].ei_block));
776 }
777 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400778 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700779 if (block < le32_to_cpu(ix->ei_block))
780 break;
781 chix = ix;
782 }
783 BUG_ON(chix != path->p_idx);
784 }
785#endif
786
787}
788
789/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700790 * ext4_ext_binsearch:
791 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400792 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700793 */
794static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500795ext4_ext_binsearch(struct inode *inode,
796 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700797{
798 struct ext4_extent_header *eh = path->p_hdr;
799 struct ext4_extent *r, *l, *m;
800
Alex Tomasa86c6182006-10-11 01:21:03 -0700801 if (eh->eh_entries == 0) {
802 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700803 * this leaf is empty:
804 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700805 */
806 return;
807 }
808
Eric Sandeenbba90742008-01-28 23:58:27 -0500809 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700810
811 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400812 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700813
814 while (l <= r) {
815 m = l + (r - l) / 2;
816 if (block < le32_to_cpu(m->ee_block))
817 r = m - 1;
818 else
819 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400820 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
821 m, le32_to_cpu(m->ee_block),
822 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700823 }
824
825 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400826 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400827 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400828 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400829 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400830 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700831
832#ifdef CHECK_BINSEARCH
833 {
834 struct ext4_extent *chex, *ex;
835 int k;
836
837 chex = ex = EXT_FIRST_EXTENT(eh);
838 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
839 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400840 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700841 if (block < le32_to_cpu(ex->ee_block))
842 break;
843 chex = ex;
844 }
845 BUG_ON(chex != path->p_ext);
846 }
847#endif
848
849}
850
851int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
852{
853 struct ext4_extent_header *eh;
854
855 eh = ext_inode_hdr(inode);
856 eh->eh_depth = 0;
857 eh->eh_entries = 0;
858 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400859 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700860 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700861 return 0;
862}
863
864struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400865ext4_find_extent(struct inode *inode, ext4_lblk_t block,
866 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700867{
868 struct ext4_extent_header *eh;
869 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400870 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
871 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500872 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700873
874 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400875 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700876
Theodore Ts'o10809df82014-09-01 14:40:09 -0400877 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400878 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400879 if (depth > path[0].p_maxdepth) {
880 kfree(path);
881 *orig_path = path = NULL;
882 }
883 }
884 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400885 /* account possible depth increase */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800886 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700887 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400888 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700889 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400890 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700891 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700892 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400893 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700894
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400895 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700896 /* walk through the tree */
897 while (i) {
898 ext_debug("depth %d: num %d, max %d\n",
899 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400900
Alex Tomasa86c6182006-10-11 01:21:03 -0700901 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400902 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700903 path[ppos].p_depth = i;
904 path[ppos].p_ext = NULL;
905
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400906 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
907 flags);
Viresh Kumara1c83682015-08-12 15:59:44 +0530908 if (IS_ERR(bh)) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400909 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500911 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400912
Alex Tomasa86c6182006-10-11 01:21:03 -0700913 eh = ext_block_hdr(bh);
914 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500915 if (unlikely(ppos > depth)) {
916 put_bh(bh);
917 EXT4_ERROR_INODE(inode,
918 "ppos %d > depth %d", ppos, depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400919 ret = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500920 goto err;
921 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700922 path[ppos].p_bh = bh;
923 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700924 }
925
926 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700927 path[ppos].p_ext = NULL;
928 path[ppos].p_idx = NULL;
929
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 /* find extent */
931 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400932 /* if not an empty leaf */
933 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400934 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700935
936 ext4_ext_show_path(inode, path);
937
938 return path;
939
940err:
941 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400942 kfree(path);
943 if (orig_path)
944 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500945 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700946}
947
948/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700949 * ext4_ext_insert_index:
950 * insert new index [@logical;@ptr] into the block at @curp;
951 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700952 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400953static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
954 struct ext4_ext_path *curp,
955 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700956{
957 struct ext4_extent_idx *ix;
958 int len, err;
959
Avantika Mathur7e028972006-12-06 20:41:33 -0800960 err = ext4_ext_get_access(handle, inode, curp);
961 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700962 return err;
963
Frank Mayhar273df552010-03-02 11:46:09 -0500964 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
965 EXT4_ERROR_INODE(inode,
966 "logical %d == ei_block %d!",
967 logical, le32_to_cpu(curp->p_idx->ei_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400968 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -0500969 }
Robin Dongd4620312011-07-17 23:43:42 -0400970
971 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
972 >= le16_to_cpu(curp->p_hdr->eh_max))) {
973 EXT4_ERROR_INODE(inode,
974 "eh_entries %d >= eh_max %d!",
975 le16_to_cpu(curp->p_hdr->eh_entries),
976 le16_to_cpu(curp->p_hdr->eh_max));
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400977 return -EFSCORRUPTED;
Robin Dongd4620312011-07-17 23:43:42 -0400978 }
979
Alex Tomasa86c6182006-10-11 01:21:03 -0700980 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
981 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400982 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700983 ix = curp->p_idx + 1;
984 } else {
985 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400986 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700987 ix = curp->p_idx;
988 }
989
Eric Gouriou80e675f2011-10-27 11:52:18 -0400990 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
991 BUG_ON(len < 0);
992 if (len > 0) {
993 ext_debug("insert new index %d: "
994 "move %d indices from 0x%p to 0x%p\n",
995 logical, len, ix, ix + 1);
996 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
997 }
998
Tao Maf472e022011-10-17 10:13:46 -0400999 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
1000 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001001 return -EFSCORRUPTED;
Tao Maf472e022011-10-17 10:13:46 -04001002 }
1003
Alex Tomasa86c6182006-10-11 01:21:03 -07001004 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001005 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001006 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001007
Frank Mayhar273df552010-03-02 11:46:09 -05001008 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
1009 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001010 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001011 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001012
1013 err = ext4_ext_dirty(handle, inode, curp);
1014 ext4_std_error(inode->i_sb, err);
1015
1016 return err;
1017}
1018
1019/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001020 * ext4_ext_split:
1021 * inserts new subtree into the path, using free index entry
1022 * at depth @at:
1023 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1024 * - makes decision where to split
1025 * - moves remaining extents and index entries (right to the split point)
1026 * into the newly allocated blocks
1027 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001028 */
1029static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001030 unsigned int flags,
1031 struct ext4_ext_path *path,
1032 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001033{
1034 struct buffer_head *bh = NULL;
1035 int depth = ext_depth(inode);
1036 struct ext4_extent_header *neh;
1037 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001038 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001039 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001040 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001041 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001042 int err = 0;
1043
1044 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001045 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001046
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001047 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001048 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001049 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1050 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001051 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001052 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001053 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1054 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001055 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001056 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001057 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001058 } else {
1059 border = newext->ee_block;
1060 ext_debug("leaf will be added."
1061 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001062 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001063 }
1064
1065 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001066 * If error occurs, then we break processing
1067 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001068 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001069 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001070 */
1071
1072 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001073 * Get array to track all allocated blocks.
1074 * We need this to handle errors and free blocks
1075 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001076 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -08001077 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001078 if (!ablocks)
1079 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001080
1081 /* allocate all needed blocks */
1082 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1083 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001084 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001085 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001086 if (newblock == 0)
1087 goto cleanup;
1088 ablocks[a] = newblock;
1089 }
1090
1091 /* initialize new leaf */
1092 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001093 if (unlikely(newblock == 0)) {
1094 EXT4_ERROR_INODE(inode, "newblock == 0!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001095 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001096 goto cleanup;
1097 }
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001098 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001099 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001100 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001101 goto cleanup;
1102 }
1103 lock_buffer(bh);
1104
Avantika Mathur7e028972006-12-06 20:41:33 -08001105 err = ext4_journal_get_create_access(handle, bh);
1106 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001107 goto cleanup;
1108
1109 neh = ext_block_hdr(bh);
1110 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001111 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001112 neh->eh_magic = EXT4_EXT_MAGIC;
1113 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001114
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001115 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001116 if (unlikely(path[depth].p_hdr->eh_entries !=
1117 path[depth].p_hdr->eh_max)) {
1118 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1119 path[depth].p_hdr->eh_entries,
1120 path[depth].p_hdr->eh_max);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001121 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001122 goto cleanup;
1123 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001124 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001125 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1126 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001127 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001128 struct ext4_extent *ex;
1129 ex = EXT_FIRST_EXTENT(neh);
1130 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001131 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001132 }
1133
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001134 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001135 set_buffer_uptodate(bh);
1136 unlock_buffer(bh);
1137
Frank Mayhar03901312009-01-07 00:06:22 -05001138 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001139 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001140 goto cleanup;
1141 brelse(bh);
1142 bh = NULL;
1143
1144 /* correct old leaf */
1145 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001146 err = ext4_ext_get_access(handle, inode, path + depth);
1147 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001148 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001149 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001150 err = ext4_ext_dirty(handle, inode, path + depth);
1151 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001152 goto cleanup;
1153
1154 }
1155
1156 /* create intermediate indexes */
1157 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001158 if (unlikely(k < 0)) {
1159 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001160 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001161 goto cleanup;
1162 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001163 if (k)
1164 ext_debug("create %d intermediate indices\n", k);
1165 /* insert new index into current index block */
1166 /* current depth stored in i var */
1167 i = depth - 1;
1168 while (k--) {
1169 oldblock = newblock;
1170 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001171 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001172 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001173 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001174 goto cleanup;
1175 }
1176 lock_buffer(bh);
1177
Avantika Mathur7e028972006-12-06 20:41:33 -08001178 err = ext4_journal_get_create_access(handle, bh);
1179 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001180 goto cleanup;
1181
1182 neh = ext_block_hdr(bh);
1183 neh->eh_entries = cpu_to_le16(1);
1184 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001185 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001186 neh->eh_depth = cpu_to_le16(depth - i);
1187 fidx = EXT_FIRST_INDEX(neh);
1188 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001189 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001190
Eric Sandeenbba90742008-01-28 23:58:27 -05001191 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1192 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001193
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001194 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001195 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1196 EXT_LAST_INDEX(path[i].p_hdr))) {
1197 EXT4_ERROR_INODE(inode,
1198 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1199 le32_to_cpu(path[i].p_ext->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001200 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001201 goto cleanup;
1202 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001203 /* start copy indexes */
1204 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1205 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1206 EXT_MAX_INDEX(path[i].p_hdr));
1207 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001208 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001209 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001210 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001211 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001212 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001213 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001214 set_buffer_uptodate(bh);
1215 unlock_buffer(bh);
1216
Frank Mayhar03901312009-01-07 00:06:22 -05001217 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001218 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001219 goto cleanup;
1220 brelse(bh);
1221 bh = NULL;
1222
1223 /* correct old index */
1224 if (m) {
1225 err = ext4_ext_get_access(handle, inode, path + i);
1226 if (err)
1227 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001228 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001229 err = ext4_ext_dirty(handle, inode, path + i);
1230 if (err)
1231 goto cleanup;
1232 }
1233
1234 i--;
1235 }
1236
1237 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001238 err = ext4_ext_insert_index(handle, inode, path + at,
1239 le32_to_cpu(border), newblock);
1240
1241cleanup:
1242 if (bh) {
1243 if (buffer_locked(bh))
1244 unlock_buffer(bh);
1245 brelse(bh);
1246 }
1247
1248 if (err) {
1249 /* free all allocated blocks in error case */
1250 for (i = 0; i < depth; i++) {
1251 if (!ablocks[i])
1252 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001253 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001254 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001255 }
1256 }
1257 kfree(ablocks);
1258
1259 return err;
1260}
1261
1262/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001263 * ext4_ext_grow_indepth:
1264 * implements tree growing procedure:
1265 * - allocates new block
1266 * - moves top-level data (index block or leaf) into the new block
1267 * - initializes new top-level, creating index that points to the
1268 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001269 */
1270static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001271 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001272{
Alex Tomasa86c6182006-10-11 01:21:03 -07001273 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001274 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001275 ext4_fsblk_t newblock, goal = 0;
1276 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001277 int err = 0;
1278
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001279 /* Try to prepend new index to old one */
1280 if (ext_depth(inode))
1281 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1282 if (goal > le32_to_cpu(es->s_first_data_block)) {
1283 flags |= EXT4_MB_HINT_TRY_GOAL;
1284 goal--;
1285 } else
1286 goal = ext4_inode_to_goal_block(inode);
1287 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1288 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001289 if (newblock == 0)
1290 return err;
1291
Nikolay Borisovc45653c2015-07-02 01:34:07 -04001292 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
Wang Shilongaebf0242013-01-12 16:28:47 -05001293 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001294 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001295 lock_buffer(bh);
1296
Avantika Mathur7e028972006-12-06 20:41:33 -08001297 err = ext4_journal_get_create_access(handle, bh);
1298 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001299 unlock_buffer(bh);
1300 goto out;
1301 }
1302
1303 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001304 memmove(bh->b_data, EXT4_I(inode)->i_data,
1305 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001306
1307 /* set size of new block */
1308 neh = ext_block_hdr(bh);
1309 /* old root could have indexes or leaves
1310 * so calculate e_max right way */
1311 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001312 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001313 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001314 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001315 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001316 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001317 set_buffer_uptodate(bh);
1318 unlock_buffer(bh);
1319
Frank Mayhar03901312009-01-07 00:06:22 -05001320 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001321 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001322 goto out;
1323
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001324 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001325 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001326 neh->eh_entries = cpu_to_le16(1);
1327 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1328 if (neh->eh_depth == 0) {
1329 /* Root extent block becomes index block */
1330 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1331 EXT_FIRST_INDEX(neh)->ei_block =
1332 EXT_FIRST_EXTENT(neh)->ee_block;
1333 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001334 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001335 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001336 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001337 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001338
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001339 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001340 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001341out:
1342 brelse(bh);
1343
1344 return err;
1345}
1346
1347/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001348 * ext4_ext_create_new_leaf:
1349 * finds empty index and adds new leaf.
1350 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001351 */
1352static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001353 unsigned int mb_flags,
1354 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001355 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001356 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001357{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001358 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001359 struct ext4_ext_path *curp;
1360 int depth, i, err = 0;
1361
1362repeat:
1363 i = depth = ext_depth(inode);
1364
1365 /* walk up to the tree and look for free index entry */
1366 curp = path + depth;
1367 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1368 i--;
1369 curp--;
1370 }
1371
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001372 /* we use already allocated block for index block,
1373 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001374 if (EXT_HAS_FREE_INDEX(curp)) {
1375 /* if we found index with free entry, then use that
1376 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001377 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001378 if (err)
1379 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001380
1381 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001382 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001383 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001384 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001385 if (IS_ERR(path))
1386 err = PTR_ERR(path);
1387 } else {
1388 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001389 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001390 if (err)
1391 goto out;
1392
1393 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001394 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001395 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001396 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001397 if (IS_ERR(path)) {
1398 err = PTR_ERR(path);
1399 goto out;
1400 }
1401
1402 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001403 * only first (depth 0 -> 1) produces free space;
1404 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001405 */
1406 depth = ext_depth(inode);
1407 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001408 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001409 goto repeat;
1410 }
1411 }
1412
1413out:
1414 return err;
1415}
1416
1417/*
Alex Tomas1988b512008-01-28 23:58:27 -05001418 * search the closest allocated block to the left for *logical
1419 * and returns it at @logical + it's physical address at @phys
1420 * if *logical is the smallest allocated block, the function
1421 * returns 0 at @phys
1422 * return value contains 0 (success) or error code
1423 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001424static int ext4_ext_search_left(struct inode *inode,
1425 struct ext4_ext_path *path,
1426 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001427{
1428 struct ext4_extent_idx *ix;
1429 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001430 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001431
Frank Mayhar273df552010-03-02 11:46:09 -05001432 if (unlikely(path == NULL)) {
1433 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001434 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001435 }
Alex Tomas1988b512008-01-28 23:58:27 -05001436 depth = path->p_depth;
1437 *phys = 0;
1438
1439 if (depth == 0 && path->p_ext == NULL)
1440 return 0;
1441
1442 /* usually extent in the path covers blocks smaller
1443 * then *logical, but it can be that extent is the
1444 * first one in the file */
1445
1446 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001447 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001448 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001449 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1450 EXT4_ERROR_INODE(inode,
1451 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1452 *logical, le32_to_cpu(ex->ee_block));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001453 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001454 }
Alex Tomas1988b512008-01-28 23:58:27 -05001455 while (--depth >= 0) {
1456 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001457 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1458 EXT4_ERROR_INODE(inode,
1459 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001460 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001461 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001462 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001463 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001464 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001465 }
Alex Tomas1988b512008-01-28 23:58:27 -05001466 }
1467 return 0;
1468 }
1469
Frank Mayhar273df552010-03-02 11:46:09 -05001470 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1471 EXT4_ERROR_INODE(inode,
1472 "logical %d < ee_block %d + ee_len %d!",
1473 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001474 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001475 }
Alex Tomas1988b512008-01-28 23:58:27 -05001476
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001477 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001478 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001479 return 0;
1480}
1481
1482/*
1483 * search the closest allocated block to the right for *logical
1484 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001485 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001486 * returns 0 at @phys
1487 * return value contains 0 (success) or error code
1488 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001489static int ext4_ext_search_right(struct inode *inode,
1490 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001491 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1492 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001493{
1494 struct buffer_head *bh = NULL;
1495 struct ext4_extent_header *eh;
1496 struct ext4_extent_idx *ix;
1497 struct ext4_extent *ex;
1498 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001499 int depth; /* Note, NOT eh_depth; depth from top of tree */
1500 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001501
Frank Mayhar273df552010-03-02 11:46:09 -05001502 if (unlikely(path == NULL)) {
1503 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001504 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001505 }
Alex Tomas1988b512008-01-28 23:58:27 -05001506 depth = path->p_depth;
1507 *phys = 0;
1508
1509 if (depth == 0 && path->p_ext == NULL)
1510 return 0;
1511
1512 /* usually extent in the path covers blocks smaller
1513 * then *logical, but it can be that extent is the
1514 * first one in the file */
1515
1516 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001517 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001518 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001519 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1520 EXT4_ERROR_INODE(inode,
1521 "first_extent(path[%d].p_hdr) != ex",
1522 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001523 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001524 }
Alex Tomas1988b512008-01-28 23:58:27 -05001525 while (--depth >= 0) {
1526 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001527 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1528 EXT4_ERROR_INODE(inode,
1529 "ix != EXT_FIRST_INDEX *logical %d!",
1530 *logical);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001531 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001532 }
Alex Tomas1988b512008-01-28 23:58:27 -05001533 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001534 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001535 }
1536
Frank Mayhar273df552010-03-02 11:46:09 -05001537 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1538 EXT4_ERROR_INODE(inode,
1539 "logical %d < ee_block %d + ee_len %d!",
1540 *logical, le32_to_cpu(ex->ee_block), ee_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001541 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001542 }
Alex Tomas1988b512008-01-28 23:58:27 -05001543
1544 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1545 /* next allocated block in this leaf */
1546 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001547 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001548 }
1549
1550 /* go up and search for index to the right */
1551 while (--depth >= 0) {
1552 ix = path[depth].p_idx;
1553 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001554 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001555 }
1556
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001557 /* we've gone up to the root and found no index to the right */
1558 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001559
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001560got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001561 /* we've found index to the right, let's
1562 * follow it and find the closest allocated
1563 * block to the right */
1564 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001565 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001566 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001567 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001568 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001569 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001570 if (IS_ERR(bh))
1571 return PTR_ERR(bh);
1572 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001573 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001574 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001575 put_bh(bh);
1576 }
1577
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001578 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001579 if (IS_ERR(bh))
1580 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001581 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001582 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001583found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001584 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001585 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001586 *ret_ex = ex;
1587 if (bh)
1588 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001589 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001590}
1591
1592/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001593 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001594 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001595 * NOTE: it considers block number from index entry as
1596 * allocated block. Thus, index entries have to be consistent
1597 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001598 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001599ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001600ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1601{
1602 int depth;
1603
1604 BUG_ON(path == NULL);
1605 depth = path->p_depth;
1606
1607 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001608 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001609
1610 while (depth >= 0) {
1611 if (depth == path->p_depth) {
1612 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001613 if (path[depth].p_ext &&
1614 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001615 EXT_LAST_EXTENT(path[depth].p_hdr))
1616 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1617 } else {
1618 /* index */
1619 if (path[depth].p_idx !=
1620 EXT_LAST_INDEX(path[depth].p_hdr))
1621 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1622 }
1623 depth--;
1624 }
1625
Lukas Czernerf17722f2011-06-06 00:05:17 -04001626 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001627}
1628
1629/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001630 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001631 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001632 */
Robin Dong57187892011-07-23 21:49:07 -04001633static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001634{
1635 int depth;
1636
1637 BUG_ON(path == NULL);
1638 depth = path->p_depth;
1639
1640 /* zero-tree has no leaf blocks at all */
1641 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001642 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001643
1644 /* go to index block */
1645 depth--;
1646
1647 while (depth >= 0) {
1648 if (path[depth].p_idx !=
1649 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001650 return (ext4_lblk_t)
1651 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001652 depth--;
1653 }
1654
Lukas Czernerf17722f2011-06-06 00:05:17 -04001655 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001656}
1657
1658/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001659 * ext4_ext_correct_indexes:
1660 * if leaf gets modified and modified extent is first in the leaf,
1661 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001662 * TODO: do we need to correct tree in all cases?
1663 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001664static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001665 struct ext4_ext_path *path)
1666{
1667 struct ext4_extent_header *eh;
1668 int depth = ext_depth(inode);
1669 struct ext4_extent *ex;
1670 __le32 border;
1671 int k, err = 0;
1672
1673 eh = path[depth].p_hdr;
1674 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001675
1676 if (unlikely(ex == NULL || eh == NULL)) {
1677 EXT4_ERROR_INODE(inode,
1678 "ex %p == NULL or eh %p == NULL", ex, eh);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001679 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001680 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001681
1682 if (depth == 0) {
1683 /* there is no tree at all */
1684 return 0;
1685 }
1686
1687 if (ex != EXT_FIRST_EXTENT(eh)) {
1688 /* we correct tree if first leaf got modified only */
1689 return 0;
1690 }
1691
1692 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001693 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001694 */
1695 k = depth - 1;
1696 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001697 err = ext4_ext_get_access(handle, inode, path + k);
1698 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001699 return err;
1700 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001701 err = ext4_ext_dirty(handle, inode, path + k);
1702 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001703 return err;
1704
1705 while (k--) {
1706 /* change all left-side indexes */
1707 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1708 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001709 err = ext4_ext_get_access(handle, inode, path + k);
1710 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001711 break;
1712 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001713 err = ext4_ext_dirty(handle, inode, path + k);
1714 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001715 break;
1716 }
1717
1718 return err;
1719}
1720
Akira Fujita748de672009-06-17 19:24:03 -04001721int
Alex Tomasa86c6182006-10-11 01:21:03 -07001722ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1723 struct ext4_extent *ex2)
1724{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001725 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001726
Lukas Czerner556615d2014-04-20 23:45:47 -04001727 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001728 return 0;
1729
1730 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1731 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1732
1733 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001734 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001735 return 0;
1736
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001737 /*
1738 * To allow future support for preallocated extents to be added
1739 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001740 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001741 */
Eric Sandeenda0169b2013-11-04 09:58:26 -05001742 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001743 return 0;
Jan Kara109811c2016-03-08 23:36:46 -05001744 /*
1745 * The check for IO to unwritten extent is somewhat racy as we
1746 * increment i_unwritten / set EXT4_STATE_DIO_UNWRITTEN only after
1747 * dropping i_data_sem. But reserved blocks should save us in that
1748 * case.
1749 */
Lukas Czerner556615d2014-04-20 23:45:47 -04001750 if (ext4_ext_is_unwritten(ex1) &&
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001751 (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) ||
1752 atomic_read(&EXT4_I(inode)->i_unwritten) ||
Lukas Czerner556615d2014-04-20 23:45:47 -04001753 (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)))
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001754 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001755#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001756 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001757 return 0;
1758#endif
1759
Theodore Ts'obf89d162010-10-27 21:30:14 -04001760 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001761 return 1;
1762 return 0;
1763}
1764
1765/*
Amit Arora56055d32007-07-17 21:42:38 -04001766 * This function tries to merge the "ex" extent to the next extent in the tree.
1767 * It always tries to merge towards right. If you want to merge towards
1768 * left, pass "ex - 1" as argument instead of "ex".
1769 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1770 * 1 if they got merged.
1771 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001772static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001773 struct ext4_ext_path *path,
1774 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001775{
1776 struct ext4_extent_header *eh;
1777 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001778 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001779
1780 depth = ext_depth(inode);
1781 BUG_ON(path[depth].p_hdr == NULL);
1782 eh = path[depth].p_hdr;
1783
1784 while (ex < EXT_LAST_EXTENT(eh)) {
1785 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1786 break;
1787 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001788 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001789 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1790 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001791 if (unwritten)
1792 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001793
1794 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1795 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1796 * sizeof(struct ext4_extent);
1797 memmove(ex + 1, ex + 2, len);
1798 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001799 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001800 merge_done = 1;
1801 WARN_ON(eh->eh_entries == 0);
1802 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001803 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001804 }
1805
1806 return merge_done;
1807}
1808
1809/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001810 * This function does a very simple check to see if we can collapse
1811 * an extent tree with a single extent tree leaf block into the inode.
1812 */
1813static void ext4_ext_try_to_merge_up(handle_t *handle,
1814 struct inode *inode,
1815 struct ext4_ext_path *path)
1816{
1817 size_t s;
1818 unsigned max_root = ext4_ext_space_root(inode, 0);
1819 ext4_fsblk_t blk;
1820
1821 if ((path[0].p_depth != 1) ||
1822 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1823 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1824 return;
1825
1826 /*
1827 * We need to modify the block allocation bitmap and the block
1828 * group descriptor to release the extent tree block. If we
1829 * can't get the journal credits, give up.
1830 */
1831 if (ext4_journal_extend(handle, 2))
1832 return;
1833
1834 /*
1835 * Copy the extent data up to the inode
1836 */
1837 blk = ext4_idx_pblock(path[0].p_idx);
1838 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1839 sizeof(struct ext4_extent_idx);
1840 s += sizeof(struct ext4_extent_header);
1841
Theodore Ts'o10809df82014-09-01 14:40:09 -04001842 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001843 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1844 path[0].p_depth = 0;
1845 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1846 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1847 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1848
1849 brelse(path[1].p_bh);
1850 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001851 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001852}
1853
1854/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001855 * This function tries to merge the @ex extent to neighbours in the tree.
1856 * return 1 if merge left else 0.
1857 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001858static void ext4_ext_try_to_merge(handle_t *handle,
1859 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001860 struct ext4_ext_path *path,
1861 struct ext4_extent *ex) {
1862 struct ext4_extent_header *eh;
1863 unsigned int depth;
1864 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001865
1866 depth = ext_depth(inode);
1867 BUG_ON(path[depth].p_hdr == NULL);
1868 eh = path[depth].p_hdr;
1869
1870 if (ex > EXT_FIRST_EXTENT(eh))
1871 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1872
1873 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001874 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001875
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001876 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001877}
1878
1879/*
Amit Arora25d14f92007-05-24 13:04:13 -04001880 * check if a portion of the "newext" extent overlaps with an
1881 * existing extent.
1882 *
1883 * If there is an overlap discovered, it updates the length of the newext
1884 * such that there will be no overlap, and then returns 1.
1885 * If there is no overlap found, it returns 0.
1886 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001887static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1888 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001889 struct ext4_extent *newext,
1890 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001891{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001892 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001893 unsigned int depth, len1;
1894 unsigned int ret = 0;
1895
1896 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001897 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001898 depth = ext_depth(inode);
1899 if (!path[depth].p_ext)
1900 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001901 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001902
1903 /*
1904 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001905 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001906 */
1907 if (b2 < b1) {
1908 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001909 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001910 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001911 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001912 }
1913
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001914 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001915 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001916 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001917 newext->ee_len = cpu_to_le16(len1);
1918 ret = 1;
1919 }
1920
1921 /* check for overlap */
1922 if (b1 + len1 > b2) {
1923 newext->ee_len = cpu_to_le16(b2 - b1);
1924 ret = 1;
1925 }
1926out:
1927 return ret;
1928}
1929
1930/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001931 * ext4_ext_insert_extent:
1932 * tries to merge requsted extent into the existing extent or
1933 * inserts requested extent as new one into the tree,
1934 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001935 */
1936int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001937 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001938 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001939{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001940 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001941 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001942 struct ext4_extent *ex, *fex;
1943 struct ext4_extent *nearex; /* nearest extent */
1944 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001945 int depth, len, err;
1946 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001947 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001948
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001949 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1950 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001951 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1952 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001953 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001954 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001955 depth = ext_depth(inode);
1956 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001957 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001958 if (unlikely(path[depth].p_hdr == NULL)) {
1959 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04001960 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05001961 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001962
1963 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001964 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001965
1966 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001967 * Try to see whether we should rather test the extent on
1968 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001969 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001970 * left, or on the right from the searched position. This
1971 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001972 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001973 if (ex < EXT_LAST_EXTENT(eh) &&
1974 (le32_to_cpu(ex->ee_block) +
1975 ext4_ext_get_actual_len(ex) <
1976 le32_to_cpu(newext->ee_block))) {
1977 ex += 1;
1978 goto prepend;
1979 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1980 (le32_to_cpu(newext->ee_block) +
1981 ext4_ext_get_actual_len(newext) <
1982 le32_to_cpu(ex->ee_block)))
1983 ex -= 1;
1984
1985 /* Try to append newex to the ex */
1986 if (ext4_can_extents_be_merged(inode, ex, newext)) {
1987 ext_debug("append [%d]%d block to %u:[%d]%d"
1988 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04001989 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001990 ext4_ext_get_actual_len(newext),
1991 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001992 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001993 ext4_ext_get_actual_len(ex),
1994 ext4_ext_pblock(ex));
1995 err = ext4_ext_get_access(handle, inode,
1996 path + depth);
1997 if (err)
1998 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04001999 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002000 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002001 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002002 if (unwritten)
2003 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002004 eh = path[depth].p_hdr;
2005 nearex = ex;
2006 goto merge;
2007 }
2008
2009prepend:
2010 /* Try to prepend newex to the ex */
2011 if (ext4_can_extents_be_merged(inode, newext, ex)) {
2012 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2013 "(from %llu)\n",
2014 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002015 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002016 ext4_ext_get_actual_len(newext),
2017 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002018 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002019 ext4_ext_get_actual_len(ex),
2020 ext4_ext_pblock(ex));
2021 err = ext4_ext_get_access(handle, inode,
2022 path + depth);
2023 if (err)
2024 return err;
2025
Lukas Czerner556615d2014-04-20 23:45:47 -04002026 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002027 ex->ee_block = newext->ee_block;
2028 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2029 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2030 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002031 if (unwritten)
2032 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002033 eh = path[depth].p_hdr;
2034 nearex = ex;
2035 goto merge;
2036 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002037 }
2038
Alex Tomasa86c6182006-10-11 01:21:03 -07002039 depth = ext_depth(inode);
2040 eh = path[depth].p_hdr;
2041 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2042 goto has_space;
2043
2044 /* probably next leaf has space for us? */
2045 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002046 next = EXT_MAX_BLOCKS;
2047 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002048 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002049 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002050 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002051 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002052 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002053 if (IS_ERR(npath))
2054 return PTR_ERR(npath);
2055 BUG_ON(npath->p_depth != path->p_depth);
2056 eh = npath[depth].p_hdr;
2057 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002058 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002059 le16_to_cpu(eh->eh_entries));
2060 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002061 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002062 }
2063 ext_debug("next leaf has no free space(%d,%d)\n",
2064 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2065 }
2066
2067 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002068 * There is no free space in the found leaf.
2069 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002070 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002071 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002072 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002073 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002074 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002075 if (err)
2076 goto cleanup;
2077 depth = ext_depth(inode);
2078 eh = path[depth].p_hdr;
2079
2080has_space:
2081 nearex = path[depth].p_ext;
2082
Avantika Mathur7e028972006-12-06 20:41:33 -08002083 err = ext4_ext_get_access(handle, inode, path + depth);
2084 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002085 goto cleanup;
2086
2087 if (!nearex) {
2088 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002089 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002090 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002091 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002092 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002093 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002094 nearex = EXT_FIRST_EXTENT(eh);
2095 } else {
2096 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002097 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002098 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002099 ext_debug("insert %u:%llu:[%d]%d before: "
2100 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002101 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002102 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002103 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002104 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002105 nearex);
2106 nearex++;
2107 } else {
2108 /* Insert before */
2109 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002110 ext_debug("insert %u:%llu:[%d]%d after: "
2111 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002112 le32_to_cpu(newext->ee_block),
2113 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002114 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002115 ext4_ext_get_actual_len(newext),
2116 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002117 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002118 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2119 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002120 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002121 "move %d extents from 0x%p to 0x%p\n",
2122 le32_to_cpu(newext->ee_block),
2123 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002124 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002125 ext4_ext_get_actual_len(newext),
2126 len, nearex, nearex + 1);
2127 memmove(nearex + 1, nearex,
2128 len * sizeof(struct ext4_extent));
2129 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002130 }
2131
Marcin Slusarze8546d02008-04-17 10:38:59 -04002132 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002133 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002134 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002135 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002136 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002137
2138merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002139 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002140 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002141 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002142
Alex Tomasa86c6182006-10-11 01:21:03 -07002143
2144 /* time to correct all indexes above */
2145 err = ext4_ext_correct_indexes(handle, inode, path);
2146 if (err)
2147 goto cleanup;
2148
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002149 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002150
2151cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002152 ext4_ext_drop_refs(npath);
2153 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002154 return err;
2155}
2156
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002157static int ext4_fill_fiemap_extents(struct inode *inode,
2158 ext4_lblk_t block, ext4_lblk_t num,
2159 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04002160{
2161 struct ext4_ext_path *path = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002162 struct ext4_extent *ex;
Zheng Liu69eb33d2013-02-18 00:31:07 -05002163 struct extent_status es;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002164 ext4_lblk_t next, next_del, start = 0, end = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002165 ext4_lblk_t last = block + num;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002166 int exists, depth = 0, err = 0;
2167 unsigned int flags = 0;
2168 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002169
Lukas Czernerf17722f2011-06-06 00:05:17 -04002170 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04002171 num = last - block;
2172 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05002173 down_read(&EXT4_I(inode)->i_data_sem);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002174
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002175 path = ext4_find_extent(inode, block, &path, 0);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002176 if (IS_ERR(path)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002177 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002178 err = PTR_ERR(path);
2179 path = NULL;
2180 break;
2181 }
2182
2183 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05002184 if (unlikely(path[depth].p_hdr == NULL)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002185 up_read(&EXT4_I(inode)->i_data_sem);
Frank Mayhar273df552010-03-02 11:46:09 -05002186 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002187 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002188 break;
2189 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002190 ex = path[depth].p_ext;
2191 next = ext4_ext_next_allocated_block(path);
2192
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002193 flags = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002194 exists = 0;
2195 if (!ex) {
2196 /* there is no extent yet, so try to allocate
2197 * all requested space */
2198 start = block;
2199 end = block + num;
2200 } else if (le32_to_cpu(ex->ee_block) > block) {
2201 /* need to allocate space before found extent */
2202 start = block;
2203 end = le32_to_cpu(ex->ee_block);
2204 if (block + num < end)
2205 end = block + num;
2206 } else if (block >= le32_to_cpu(ex->ee_block)
2207 + ext4_ext_get_actual_len(ex)) {
2208 /* need to allocate space after found extent */
2209 start = block;
2210 end = block + num;
2211 if (end >= next)
2212 end = next;
2213 } else if (block >= le32_to_cpu(ex->ee_block)) {
2214 /*
2215 * some part of requested space is covered
2216 * by found extent
2217 */
2218 start = block;
2219 end = le32_to_cpu(ex->ee_block)
2220 + ext4_ext_get_actual_len(ex);
2221 if (block + num < end)
2222 end = block + num;
2223 exists = 1;
2224 } else {
2225 BUG();
2226 }
2227 BUG_ON(end <= start);
2228
2229 if (!exists) {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002230 es.es_lblk = start;
2231 es.es_len = end - start;
2232 es.es_pblk = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002233 } else {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002234 es.es_lblk = le32_to_cpu(ex->ee_block);
2235 es.es_len = ext4_ext_get_actual_len(ex);
2236 es.es_pblk = ext4_ext_pblock(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04002237 if (ext4_ext_is_unwritten(ex))
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002238 flags |= FIEMAP_EXTENT_UNWRITTEN;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002239 }
2240
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002241 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05002242 * Find delayed extent and update es accordingly. We call
2243 * it even in !exists case to find out whether es is the
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002244 * last existing extent or not.
2245 */
Zheng Liu69eb33d2013-02-18 00:31:07 -05002246 next_del = ext4_find_delayed_extent(inode, &es);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002247 if (!exists && next_del) {
2248 exists = 1;
Jie Liu72dac952013-06-12 23:13:59 -04002249 flags |= (FIEMAP_EXTENT_DELALLOC |
2250 FIEMAP_EXTENT_UNKNOWN);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002251 }
2252 up_read(&EXT4_I(inode)->i_data_sem);
2253
Zheng Liu69eb33d2013-02-18 00:31:07 -05002254 if (unlikely(es.es_len == 0)) {
2255 EXT4_ERROR_INODE(inode, "es.es_len == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002256 err = -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002257 break;
2258 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002259
Zheng Liuf7fec032013-02-18 00:28:47 -05002260 /*
2261 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2262 * we need to check next == EXT_MAX_BLOCKS because it is
2263 * possible that an extent is with unwritten and delayed
2264 * status due to when an extent is delayed allocated and
2265 * is allocated by fallocate status tree will track both of
2266 * them in a extent.
2267 *
2268 * So we could return a unwritten and delayed extent, and
2269 * its block is equal to 'next'.
2270 */
2271 if (next == next_del && next == EXT_MAX_BLOCKS) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002272 flags |= FIEMAP_EXTENT_LAST;
2273 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2274 next != EXT_MAX_BLOCKS)) {
2275 EXT4_ERROR_INODE(inode,
2276 "next extent == %u, next "
2277 "delalloc extent = %u",
2278 next, next_del);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002279 err = -EFSCORRUPTED;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002280 break;
2281 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002282 }
2283
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002284 if (exists) {
2285 err = fiemap_fill_next_extent(fieinfo,
Zheng Liu69eb33d2013-02-18 00:31:07 -05002286 (__u64)es.es_lblk << blksize_bits,
2287 (__u64)es.es_pblk << blksize_bits,
2288 (__u64)es.es_len << blksize_bits,
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002289 flags);
2290 if (err < 0)
2291 break;
2292 if (err == 1) {
2293 err = 0;
2294 break;
2295 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002296 }
2297
Zheng Liu69eb33d2013-02-18 00:31:07 -05002298 block = es.es_lblk + es.es_len;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002299 }
2300
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002301 ext4_ext_drop_refs(path);
2302 kfree(path);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002303 return err;
2304}
2305
Alex Tomasa86c6182006-10-11 01:21:03 -07002306/*
Jan Kara140a5252016-03-09 22:46:57 -05002307 * ext4_ext_determine_hole - determine hole around given block
2308 * @inode: inode we lookup in
2309 * @path: path in extent tree to @lblk
2310 * @lblk: pointer to logical block around which we want to determine hole
2311 *
2312 * Determine hole length (and start if easily possible) around given logical
2313 * block. We don't try too hard to find the beginning of the hole but @path
2314 * actually points to extent before @lblk, we provide it.
2315 *
2316 * The function returns the length of a hole starting at @lblk. We update @lblk
2317 * to the beginning of the hole if we managed to find it.
2318 */
2319static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2320 struct ext4_ext_path *path,
2321 ext4_lblk_t *lblk)
2322{
2323 int depth = ext_depth(inode);
2324 struct ext4_extent *ex;
2325 ext4_lblk_t len;
2326
2327 ex = path[depth].p_ext;
2328 if (ex == NULL) {
2329 /* there is no extent yet, so gap is [0;-] */
2330 *lblk = 0;
2331 len = EXT_MAX_BLOCKS;
2332 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2333 len = le32_to_cpu(ex->ee_block) - *lblk;
2334 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2335 + ext4_ext_get_actual_len(ex)) {
2336 ext4_lblk_t next;
2337
2338 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2339 next = ext4_ext_next_allocated_block(path);
2340 BUG_ON(next == *lblk);
2341 len = next - *lblk;
2342 } else {
2343 BUG();
2344 }
2345 return len;
2346}
2347
2348/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002349 * ext4_ext_put_gap_in_cache:
2350 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002351 * and cache this gap
2352 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002353static void
Jan Kara140a5252016-03-09 22:46:57 -05002354ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2355 ext4_lblk_t hole_len)
Alex Tomasa86c6182006-10-11 01:21:03 -07002356{
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002357 struct extent_status es;
Alex Tomasa86c6182006-10-11 01:21:03 -07002358
Jan Kara140a5252016-03-09 22:46:57 -05002359 ext4_es_find_delayed_extent_range(inode, hole_start,
2360 hole_start + hole_len - 1, &es);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002361 if (es.es_len) {
2362 /* There's delayed extent containing lblock? */
Jan Kara140a5252016-03-09 22:46:57 -05002363 if (es.es_lblk <= hole_start)
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002364 return;
Jan Kara140a5252016-03-09 22:46:57 -05002365 hole_len = min(es.es_lblk - hole_start, hole_len);
Zheng Liu2f8e0a72014-11-25 11:44:37 -05002366 }
Jan Kara140a5252016-03-09 22:46:57 -05002367 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2368 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2369 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002370}
2371
2372/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002373 * ext4_ext_rm_idx:
2374 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002375 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002376static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002377 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002378{
Alex Tomasa86c6182006-10-11 01:21:03 -07002379 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002380 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002381
2382 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002383 depth--;
2384 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002385 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002386 if (unlikely(path->p_hdr->eh_entries == 0)) {
2387 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002388 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002389 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002390 err = ext4_ext_get_access(handle, inode, path);
2391 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002392 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002393
2394 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2395 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2396 len *= sizeof(struct ext4_extent_idx);
2397 memmove(path->p_idx, path->p_idx + 1, len);
2398 }
2399
Marcin Slusarze8546d02008-04-17 10:38:59 -04002400 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002401 err = ext4_ext_dirty(handle, inode, path);
2402 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002403 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002404 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002405 trace_ext4_ext_rm_idx(inode, leaf);
2406
Peter Huewe7dc57612011-02-21 21:01:42 -05002407 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002408 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002409
2410 while (--depth >= 0) {
2411 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2412 break;
2413 path--;
2414 err = ext4_ext_get_access(handle, inode, path);
2415 if (err)
2416 break;
2417 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2418 err = ext4_ext_dirty(handle, inode, path);
2419 if (err)
2420 break;
2421 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002422 return err;
2423}
2424
2425/*
Mingming Caoee12b632008-08-19 22:16:05 -04002426 * ext4_ext_calc_credits_for_single_extent:
2427 * This routine returns max. credits that needed to insert an extent
2428 * to the extent tree.
2429 * When pass the actual path, the caller should calculate credits
2430 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002431 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002432int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002433 struct ext4_ext_path *path)
2434{
Alex Tomasa86c6182006-10-11 01:21:03 -07002435 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002436 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002437 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002438
Alex Tomasa86c6182006-10-11 01:21:03 -07002439 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002440 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002441 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2442
2443 /*
2444 * There are some space in the leaf tree, no
2445 * need to account for leaf block credit
2446 *
2447 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002448 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002449 * accounted.
2450 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002451 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002452 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002453 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002454 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002455 }
2456
Mingming Cao525f4ed2008-08-19 22:15:58 -04002457 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002458}
Alex Tomasa86c6182006-10-11 01:21:03 -07002459
Mingming Caoee12b632008-08-19 22:16:05 -04002460/*
Jan Karafffb2732013-06-04 13:01:11 -04002461 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002462 *
Jan Karafffb2732013-06-04 13:01:11 -04002463 * If we add a single extent, then in the worse case, each tree level
2464 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002465 *
Jan Karafffb2732013-06-04 13:01:11 -04002466 * If more extents are inserted, they could cause the whole tree split more
2467 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002468 */
Jan Karafffb2732013-06-04 13:01:11 -04002469int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002470{
2471 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002472 int depth;
2473
2474 /* If we are converting the inline data, only one is needed here. */
2475 if (ext4_has_inline_data(inode))
2476 return 1;
2477
2478 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002479
Jan Karafffb2732013-06-04 13:01:11 -04002480 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002481 index = depth * 2;
2482 else
2483 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002484
Mingming Caoee12b632008-08-19 22:16:05 -04002485 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002486}
2487
Theodore Ts'o981250c2013-06-12 11:48:29 -04002488static inline int get_default_free_blocks_flags(struct inode *inode)
2489{
2490 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2491 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2492 else if (ext4_should_journal_data(inode))
2493 return EXT4_FREE_BLOCKS_FORGET;
2494 return 0;
2495}
2496
Alex Tomasa86c6182006-10-11 01:21:03 -07002497static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002498 struct ext4_extent *ex,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002499 long long *partial_cluster,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002500 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002501{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002502 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Eric Whitney345ee942014-11-23 00:59:39 -05002503 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002504 ext4_fsblk_t pblk;
Theodore Ts'o981250c2013-06-12 11:48:29 -04002505 int flags = get_default_free_blocks_flags(inode);
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002506
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002507 /*
2508 * For bigalloc file systems, we never free a partial cluster
2509 * at the beginning of the extent. Instead, we make a note
2510 * that we tried freeing the cluster, and check to see if we
2511 * need to free it on a subsequent call to ext4_remove_blocks,
Eric Whitney345ee942014-11-23 00:59:39 -05002512 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002513 */
2514 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2515
Aditya Kalid8990242011-09-09 19:18:51 -04002516 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002517 /*
2518 * If we have a partial cluster, and it's different from the
2519 * cluster of the last block, we need to explicitly free the
2520 * partial cluster here.
2521 */
2522 pblk = ext4_ext_pblock(ex) + ee_len - 1;
Eric Whitney345ee942014-11-23 00:59:39 -05002523 if (*partial_cluster > 0 &&
2524 *partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002525 ext4_free_blocks(handle, inode, NULL,
2526 EXT4_C2B(sbi, *partial_cluster),
2527 sbi->s_cluster_ratio, flags);
2528 *partial_cluster = 0;
2529 }
2530
Alex Tomasa86c6182006-10-11 01:21:03 -07002531#ifdef EXTENTS_STATS
2532 {
2533 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002534 spin_lock(&sbi->s_ext_stats_lock);
2535 sbi->s_ext_blocks += ee_len;
2536 sbi->s_ext_extents++;
2537 if (ee_len < sbi->s_ext_min)
2538 sbi->s_ext_min = ee_len;
2539 if (ee_len > sbi->s_ext_max)
2540 sbi->s_ext_max = ee_len;
2541 if (ext_depth(inode) > sbi->s_depth_max)
2542 sbi->s_depth_max = ext_depth(inode);
2543 spin_unlock(&sbi->s_ext_stats_lock);
2544 }
2545#endif
2546 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002547 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002548 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002549 ext4_lblk_t num;
Eric Whitney345ee942014-11-23 00:59:39 -05002550 long long first_cluster;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002551
Amit Aroraa2df2a62007-07-17 21:42:41 -04002552 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002553 pblk = ext4_ext_pblock(ex) + ee_len - num;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002554 /*
2555 * Usually we want to free partial cluster at the end of the
2556 * extent, except for the situation when the cluster is still
2557 * used by any other extent (partial_cluster is negative).
2558 */
2559 if (*partial_cluster < 0 &&
Eric Whitney345ee942014-11-23 00:59:39 -05002560 *partial_cluster == -(long long) EXT4_B2C(sbi, pblk+num-1))
Lukas Czernerd23142c2013-05-27 23:33:35 -04002561 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
2562
2563 ext_debug("free last %u blocks starting %llu partial %lld\n",
2564 num, pblk, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002565 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2566 /*
2567 * If the block range to be freed didn't start at the
2568 * beginning of a cluster, and we removed the entire
Lukas Czernerd23142c2013-05-27 23:33:35 -04002569 * extent and the cluster is not used by any other extent,
2570 * save the partial cluster here, since we might need to
Eric Whitney345ee942014-11-23 00:59:39 -05002571 * delete if we determine that the truncate or punch hole
2572 * operation has removed all of the blocks in the cluster.
2573 * If that cluster is used by another extent, preserve its
2574 * negative value so it isn't freed later on.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002575 *
Eric Whitney345ee942014-11-23 00:59:39 -05002576 * If the whole extent wasn't freed, we've reached the
2577 * start of the truncated/punched region and have finished
2578 * removing blocks. If there's a partial cluster here it's
2579 * shared with the remainder of the extent and is no longer
2580 * a candidate for removal.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002581 */
Eric Whitney345ee942014-11-23 00:59:39 -05002582 if (EXT4_PBLK_COFF(sbi, pblk) && ee_len == num) {
2583 first_cluster = (long long) EXT4_B2C(sbi, pblk);
2584 if (first_cluster != -*partial_cluster)
2585 *partial_cluster = first_cluster;
2586 } else {
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002587 *partial_cluster = 0;
Eric Whitney345ee942014-11-23 00:59:39 -05002588 }
Lukas Czerner78fb9cd2013-05-27 23:32:35 -04002589 } else
2590 ext4_error(sbi->s_sb, "strange request: removal(2) "
2591 "%u-%u from %u:%u\n",
2592 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002593 return 0;
2594}
2595
Allison Hendersond583fb82011-05-25 07:41:43 -04002596
2597/*
2598 * ext4_ext_rm_leaf() Removes the extents associated with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002599 * blocks appearing between "start" and "end". Both "start"
2600 * and "end" must appear in the same extent or EIO is returned.
Allison Hendersond583fb82011-05-25 07:41:43 -04002601 *
2602 * @handle: The journal handle
2603 * @inode: The files inode
2604 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002605 * @partial_cluster: The cluster which we'll have to free if all extents
Eric Whitney5bf43762014-11-23 00:58:11 -05002606 * has been released from it. However, if this value is
2607 * negative, it's a cluster just to the right of the
2608 * punched region and it must not be freed.
Allison Hendersond583fb82011-05-25 07:41:43 -04002609 * @start: The first block to remove
2610 * @end: The last block to remove
2611 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002612static int
2613ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002614 struct ext4_ext_path *path,
2615 long long *partial_cluster,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002616 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002617{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002618 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002619 int err = 0, correct_index = 0;
2620 int depth = ext_depth(inode), credits;
2621 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002622 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002623 unsigned num;
2624 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002625 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002626 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002627 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002628 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002629
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002630 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002631 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002632 if (!path[depth].p_hdr)
2633 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2634 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002635 if (unlikely(path[depth].p_hdr == NULL)) {
2636 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002637 return -EFSCORRUPTED;
Frank Mayhar273df552010-03-02 11:46:09 -05002638 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002639 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002640 ex = path[depth].p_ext;
2641 if (!ex)
2642 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002643
2644 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002645 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002646
Aditya Kalid8990242011-09-09 19:18:51 -04002647 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2648
Alex Tomasa86c6182006-10-11 01:21:03 -07002649 while (ex >= EXT_FIRST_EXTENT(eh) &&
2650 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002651
Lukas Czerner556615d2014-04-20 23:45:47 -04002652 if (ext4_ext_is_unwritten(ex))
2653 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002654 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002655 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002656
Mingming553f9002009-09-18 13:34:55 -04002657 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002658 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002659 path[depth].p_ext = ex;
2660
2661 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002662 b = ex_ee_block+ex_ee_len - 1 < end ?
2663 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002664
2665 ext_debug(" border %u:%u\n", a, b);
2666
Allison Hendersond583fb82011-05-25 07:41:43 -04002667 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002668 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002669 /*
2670 * We're going to skip this extent and move to another,
Eric Whitneyf4226d92014-11-23 00:55:42 -05002671 * so note that its first cluster is in use to avoid
2672 * freeing it when removing blocks. Eventually, the
2673 * right edge of the truncated/punched region will
2674 * be just to the left.
Lukas Czernerd23142c2013-05-27 23:33:35 -04002675 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002676 if (sbi->s_cluster_ratio > 1) {
2677 pblk = ext4_ext_pblock(ex);
Lukas Czernerd23142c2013-05-27 23:33:35 -04002678 *partial_cluster =
Eric Whitneyf4226d92014-11-23 00:55:42 -05002679 -(long long) EXT4_B2C(sbi, pblk);
2680 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002681 ex--;
2682 ex_ee_block = le32_to_cpu(ex->ee_block);
2683 ex_ee_len = ext4_ext_get_actual_len(ex);
2684 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002685 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002686 EXT4_ERROR_INODE(inode,
2687 "can not handle truncate %u:%u "
2688 "on extent %u:%u",
2689 start, end, ex_ee_block,
2690 ex_ee_block + ex_ee_len - 1);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002691 err = -EFSCORRUPTED;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002692 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002693 } else if (a != ex_ee_block) {
2694 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002695 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002696 } else {
2697 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002698 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002699 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002700 /*
2701 * 3 for leaf, sb, and inode plus 2 (bmap and group
2702 * descriptor) for each block group; assume two block
2703 * groups plus ex_ee_len/blocks_per_block_group for
2704 * the worst case
2705 */
2706 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002707 if (ex == EXT_FIRST_EXTENT(eh)) {
2708 correct_index = 1;
2709 credits += (ext_depth(inode)) + 1;
2710 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002711 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002712
Jan Kara487caee2009-08-17 22:17:20 -04002713 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002714 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002715 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002716
2717 err = ext4_ext_get_access(handle, inode, path + depth);
2718 if (err)
2719 goto out;
2720
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002721 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2722 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002723 if (err)
2724 goto out;
2725
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002726 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002727 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002728 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002729
Alex Tomasa86c6182006-10-11 01:21:03 -07002730 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002731 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04002732 * Do not mark unwritten if all the blocks in the
Amit Arora749269f2007-07-18 09:02:56 -04002733 * extent have been removed.
2734 */
Lukas Czerner556615d2014-04-20 23:45:47 -04002735 if (unwritten && num)
2736 ext4_ext_mark_unwritten(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002737 /*
2738 * If the extent was completely released,
2739 * we need to remove it from the leaf
2740 */
2741 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002742 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002743 /*
2744 * For hole punching, we need to scoot all the
2745 * extents up when an extent is removed so that
2746 * we dont have blank extents in the middle
2747 */
2748 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2749 sizeof(struct ext4_extent));
2750
2751 /* Now get rid of the one at the end */
2752 memset(EXT_LAST_EXTENT(eh), 0,
2753 sizeof(struct ext4_extent));
2754 }
2755 le16_add_cpu(&eh->eh_entries, -1);
Eric Whitney5bf43762014-11-23 00:58:11 -05002756 }
Allison Hendersond583fb82011-05-25 07:41:43 -04002757
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002758 err = ext4_ext_dirty(handle, inode, path + depth);
2759 if (err)
2760 goto out;
2761
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002762 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002763 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002764 ex--;
2765 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002766 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002767 }
2768
2769 if (correct_index && eh->eh_entries)
2770 err = ext4_ext_correct_indexes(handle, inode, path);
2771
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002772 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002773 * If there's a partial cluster and at least one extent remains in
2774 * the leaf, free the partial cluster if it isn't shared with the
Eric Whitney5bf43762014-11-23 00:58:11 -05002775 * current extent. If it is shared with the current extent
2776 * we zero partial_cluster because we've reached the start of the
2777 * truncated/punched region and we're done removing blocks.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002778 */
Eric Whitney5bf43762014-11-23 00:58:11 -05002779 if (*partial_cluster > 0 && ex >= EXT_FIRST_EXTENT(eh)) {
2780 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
2781 if (*partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
2782 ext4_free_blocks(handle, inode, NULL,
2783 EXT4_C2B(sbi, *partial_cluster),
2784 sbi->s_cluster_ratio,
2785 get_default_free_blocks_flags(inode));
2786 }
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002787 *partial_cluster = 0;
2788 }
2789
Alex Tomasa86c6182006-10-11 01:21:03 -07002790 /* if this leaf is free, then we should
2791 * remove it from index block above */
2792 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002793 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002794
2795out:
2796 return err;
2797}
2798
2799/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002800 * ext4_ext_more_to_rm:
2801 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002802 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002803static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002804ext4_ext_more_to_rm(struct ext4_ext_path *path)
2805{
2806 BUG_ON(path->p_idx == NULL);
2807
2808 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2809 return 0;
2810
2811 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002812 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002813 * so we have to consider current index for truncation
2814 */
2815 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2816 return 0;
2817 return 1;
2818}
2819
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002820int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2821 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002822{
Eric Whitneyf4226d92014-11-23 00:55:42 -05002823 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002824 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002825 struct ext4_ext_path *path = NULL;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002826 long long partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002827 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002828 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002829
Lukas Czerner5f95d212012-03-19 23:03:19 -04002830 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002831
2832 /* probably first extent we're gonna free will be last in block */
Theodore Ts'o9924a922013-02-08 21:59:22 -05002833 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002834 if (IS_ERR(handle))
2835 return PTR_ERR(handle);
2836
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002837again:
Lukas Czerner61801322013-05-27 23:32:35 -04002838 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002839
Alex Tomasa86c6182006-10-11 01:21:03 -07002840 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002841 * Check if we are removing extents inside the extent tree. If that
2842 * is the case, we are going to punch a hole inside the extent tree
2843 * so we have to check whether we need to split the extent covering
2844 * the last block to remove so we can easily remove the part of it
2845 * in ext4_ext_rm_leaf().
2846 */
2847 if (end < EXT_MAX_BLOCKS - 1) {
2848 struct ext4_extent *ex;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002849 ext4_lblk_t ee_block, ex_end, lblk;
2850 ext4_fsblk_t pblk;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002851
Eric Whitneyf4226d92014-11-23 00:55:42 -05002852 /* find extent for or closest extent to this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002853 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002854 if (IS_ERR(path)) {
2855 ext4_journal_stop(handle);
2856 return PTR_ERR(path);
2857 }
2858 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002859 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002860 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002861 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002862 if (depth) {
2863 EXT4_ERROR_INODE(inode,
2864 "path[%d].p_hdr == NULL",
2865 depth);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002866 err = -EFSCORRUPTED;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002867 }
2868 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002869 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002870
2871 ee_block = le32_to_cpu(ex->ee_block);
Eric Whitneyf4226d92014-11-23 00:55:42 -05002872 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002873
2874 /*
2875 * See if the last block is inside the extent, if so split
2876 * the extent at 'end' block so we can easily remove the
2877 * tail of the first part of the split extent in
2878 * ext4_ext_rm_leaf().
2879 */
Eric Whitneyf4226d92014-11-23 00:55:42 -05002880 if (end >= ee_block && end < ex_end) {
2881
2882 /*
2883 * If we're going to split the extent, note that
2884 * the cluster containing the block after 'end' is
2885 * in use to avoid freeing it when removing blocks.
2886 */
2887 if (sbi->s_cluster_ratio > 1) {
2888 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
2889 partial_cluster =
2890 -(long long) EXT4_B2C(sbi, pblk);
2891 }
2892
Lukas Czerner5f95d212012-03-19 23:03:19 -04002893 /*
2894 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002895 * block in the first new extent. Also we should not
2896 * fail removing space due to ENOSPC so try to use
2897 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002898 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002899 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002900 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002901 if (err < 0)
2902 goto out;
Eric Whitneyf4226d92014-11-23 00:55:42 -05002903
2904 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end) {
2905 /*
2906 * If there's an extent to the right its first cluster
2907 * contains the immediate right boundary of the
2908 * truncated/punched region. Set partial_cluster to
2909 * its negative value so it won't be freed if shared
2910 * with the current extent. The end < ee_block case
2911 * is handled in ext4_ext_rm_leaf().
2912 */
2913 lblk = ex_end + 1;
2914 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
2915 &ex);
2916 if (err)
2917 goto out;
2918 if (pblk)
2919 partial_cluster =
2920 -(long long) EXT4_B2C(sbi, pblk);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002921 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002922 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002923 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002924 * We start scanning from right side, freeing all the blocks
2925 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002926 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002927 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002928 if (path) {
2929 int k = i = depth;
2930 while (--k > 0)
2931 path[k].p_block =
2932 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2933 } else {
2934 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2935 GFP_NOFS);
2936 if (path == NULL) {
2937 ext4_journal_stop(handle);
2938 return -ENOMEM;
2939 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04002940 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002941 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002942 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002943
Theodore Ts'oc3491792013-08-16 21:21:41 -04002944 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04002945 err = -EFSCORRUPTED;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002946 goto out;
2947 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002948 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002949 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002950
2951 while (i >= 0 && err == 0) {
2952 if (i == depth) {
2953 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002954 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002955 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002956 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002957 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002958 brelse(path[i].p_bh);
2959 path[i].p_bh = NULL;
2960 i--;
2961 continue;
2962 }
2963
2964 /* this is index block */
2965 if (!path[i].p_hdr) {
2966 ext_debug("initialize header\n");
2967 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002968 }
2969
Alex Tomasa86c6182006-10-11 01:21:03 -07002970 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002971 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002972 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2973 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2974 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2975 path[i].p_hdr,
2976 le16_to_cpu(path[i].p_hdr->eh_entries));
2977 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002978 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002979 path[i].p_idx--;
2980 }
2981
2982 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2983 i, EXT_FIRST_INDEX(path[i].p_hdr),
2984 path[i].p_idx);
2985 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002986 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002987 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002988 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002989 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002990 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002991 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002992 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2993 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002994 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002995 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002996 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002997 break;
2998 }
Theodore Ts'o76828c882013-07-15 12:27:47 -04002999 /* Yield here to deal with large extent trees.
3000 * Should be a no-op if we did IO above. */
3001 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003002 if (WARN_ON(i + 1 > depth)) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003003 err = -EFSCORRUPTED;
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003004 break;
3005 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04003006 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07003007
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003008 /* save actual number of indexes since this
3009 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07003010 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
3011 i++;
3012 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003013 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07003014 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003015 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07003016 * handle must be already prepared by the
3017 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05003018 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07003019 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003020 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07003021 brelse(path[i].p_bh);
3022 path[i].p_bh = NULL;
3023 i--;
3024 ext_debug("return to level %d\n", i);
3025 }
3026 }
3027
Lukas Czerner61801322013-05-27 23:32:35 -04003028 trace_ext4_ext_remove_space_done(inode, start, end, depth,
3029 partial_cluster, path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04003030
Eric Whitney0756b902014-11-23 00:59:39 -05003031 /*
3032 * If we still have something in the partial cluster and we have removed
Aditya Kali7b415bf2011-09-09 19:04:51 -04003033 * even the first extent, then we should free the blocks in the partial
Eric Whitney0756b902014-11-23 00:59:39 -05003034 * cluster as well. (This code will only run when there are no leaves
3035 * to the immediate left of the truncated/punched region.)
3036 */
3037 if (partial_cluster > 0 && err == 0) {
3038 /* don't zero partial_cluster since it's not used afterwards */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003039 ext4_free_blocks(handle, inode, NULL,
Eric Whitneyf4226d92014-11-23 00:55:42 -05003040 EXT4_C2B(sbi, partial_cluster),
Eric Whitney0756b902014-11-23 00:59:39 -05003041 sbi->s_cluster_ratio,
3042 get_default_free_blocks_flags(inode));
Aditya Kali7b415bf2011-09-09 19:04:51 -04003043 }
3044
Alex Tomasa86c6182006-10-11 01:21:03 -07003045 /* TODO: flexible tree reduction should be here */
3046 if (path->p_hdr->eh_entries == 0) {
3047 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003048 * truncate to zero freed all the tree,
3049 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003050 */
3051 err = ext4_ext_get_access(handle, inode, path);
3052 if (err == 0) {
3053 ext_inode_hdr(inode)->eh_depth = 0;
3054 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003055 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003056 err = ext4_ext_dirty(handle, inode, path);
3057 }
3058 }
3059out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003060 ext4_ext_drop_refs(path);
3061 kfree(path);
3062 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003063 if (err == -EAGAIN)
3064 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003065 ext4_journal_stop(handle);
3066
3067 return err;
3068}
3069
3070/*
3071 * called at mount time
3072 */
3073void ext4_ext_init(struct super_block *sb)
3074{
3075 /*
3076 * possible initialization would be here
3077 */
3078
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003079 if (ext4_has_feature_extents(sb)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003080#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003081 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003082#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003083 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003084#endif
3085#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003086 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003087#endif
3088#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003089 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003090#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003091 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003092#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003093#ifdef EXTENTS_STATS
3094 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3095 EXT4_SB(sb)->s_ext_min = 1 << 30;
3096 EXT4_SB(sb)->s_ext_max = 0;
3097#endif
3098 }
3099}
3100
3101/*
3102 * called at umount time
3103 */
3104void ext4_ext_release(struct super_block *sb)
3105{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003106 if (!ext4_has_feature_extents(sb))
Alex Tomasa86c6182006-10-11 01:21:03 -07003107 return;
3108
3109#ifdef EXTENTS_STATS
3110 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3111 struct ext4_sb_info *sbi = EXT4_SB(sb);
3112 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3113 sbi->s_ext_blocks, sbi->s_ext_extents,
3114 sbi->s_ext_blocks / sbi->s_ext_extents);
3115 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3116 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3117 }
3118#endif
3119}
3120
Zheng Liud7b2a002013-08-28 14:47:06 -04003121static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3122{
3123 ext4_lblk_t ee_block;
3124 ext4_fsblk_t ee_pblock;
3125 unsigned int ee_len;
3126
3127 ee_block = le32_to_cpu(ex->ee_block);
3128 ee_len = ext4_ext_get_actual_len(ex);
3129 ee_pblock = ext4_ext_pblock(ex);
3130
3131 if (ee_len == 0)
3132 return 0;
3133
3134 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3135 EXTENT_STATUS_WRITTEN);
3136}
3137
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003138/* FIXME!! we need to try to merge to left or right after zero-out */
3139static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3140{
Lukas Czerner24075182010-10-27 21:30:06 -04003141 ext4_fsblk_t ee_pblock;
3142 unsigned int ee_len;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003143
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003144 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003145 ee_pblock = ext4_ext_pblock(ex);
Jan Kara53085fa2015-12-07 15:09:35 -05003146 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3147 ee_len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003148}
3149
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003150/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003151 * ext4_split_extent_at() splits an extent at given block.
3152 *
3153 * @handle: the journal handle
3154 * @inode: the file inode
3155 * @path: the path to the extent
3156 * @split: the logical block where the extent is splitted.
3157 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003158 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003159 * @flags: flags used to insert new extent to extent tree.
3160 *
3161 *
3162 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3163 * of which are deterimined by split_flag.
3164 *
3165 * There are two cases:
3166 * a> the extent are splitted into two extent.
3167 * b> split is not needed, and just mark the extent.
3168 *
3169 * return 0 on success.
3170 */
3171static int ext4_split_extent_at(handle_t *handle,
3172 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003173 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003174 ext4_lblk_t split,
3175 int split_flag,
3176 int flags)
3177{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003178 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003179 ext4_fsblk_t newblock;
3180 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003181 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003182 struct ext4_extent *ex2 = NULL;
3183 unsigned int ee_len, depth;
3184 int err = 0;
3185
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003186 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3187 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3188
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003189 ext_debug("ext4_split_extents_at: inode %lu, logical"
3190 "block %llu\n", inode->i_ino, (unsigned long long)split);
3191
3192 ext4_ext_show_leaf(inode, path);
3193
3194 depth = ext_depth(inode);
3195 ex = path[depth].p_ext;
3196 ee_block = le32_to_cpu(ex->ee_block);
3197 ee_len = ext4_ext_get_actual_len(ex);
3198 newblock = split - ee_block + ext4_ext_pblock(ex);
3199
3200 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003201 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003202 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003203 EXT4_EXT_MARK_UNWRIT1 |
3204 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003205
3206 err = ext4_ext_get_access(handle, inode, path + depth);
3207 if (err)
3208 goto out;
3209
3210 if (split == ee_block) {
3211 /*
3212 * case b: block @split is the block that the extent begins with
3213 * then we just change the state of the extent, and splitting
3214 * is not needed.
3215 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003216 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3217 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003218 else
3219 ext4_ext_mark_initialized(ex);
3220
3221 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003222 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003223
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003224 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003225 goto out;
3226 }
3227
3228 /* case a */
3229 memcpy(&orig_ex, ex, sizeof(orig_ex));
3230 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003231 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3232 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003233
3234 /*
3235 * path may lead to new leaf, not to original leaf any more
3236 * after ext4_ext_insert_extent() returns,
3237 */
3238 err = ext4_ext_dirty(handle, inode, path + depth);
3239 if (err)
3240 goto fix_extent_len;
3241
3242 ex2 = &newex;
3243 ex2->ee_block = cpu_to_le32(split);
3244 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3245 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003246 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3247 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003248
Theodore Ts'odfe50802014-09-01 14:37:09 -04003249 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003250 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003251 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003252 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003253 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003254 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003255 zero_ex.ee_len = cpu_to_le16(
3256 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003257 ext4_ext_store_pblock(&zero_ex,
3258 ext4_ext_pblock(ex2));
3259 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003260 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003261 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003262 zero_ex.ee_len = cpu_to_le16(
3263 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003264 ext4_ext_store_pblock(&zero_ex,
3265 ext4_ext_pblock(ex));
3266 }
3267 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003268 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003269 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003270 zero_ex.ee_len = cpu_to_le16(
3271 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003272 ext4_ext_store_pblock(&zero_ex,
3273 ext4_ext_pblock(&orig_ex));
3274 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003275
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003276 if (err)
3277 goto fix_extent_len;
3278 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003279 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003280 ext4_ext_try_to_merge(handle, inode, path, ex);
3281 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003282 if (err)
3283 goto fix_extent_len;
3284
3285 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003286 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003287
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003288 goto out;
3289 } else if (err)
3290 goto fix_extent_len;
3291
3292out:
3293 ext4_ext_show_leaf(inode, path);
3294 return err;
3295
3296fix_extent_len:
3297 ex->ee_len = orig_ex.ee_len;
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003298 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003299 return err;
3300}
3301
3302/*
3303 * ext4_split_extents() splits an extent and mark extent which is covered
3304 * by @map as split_flags indicates
3305 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003306 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003307 * There are three possibilities:
3308 * a> There is no split required
3309 * b> Splits in two extents: Split is happening at either end of the extent
3310 * c> Splits in three extents: Somone is splitting in middle of the extent
3311 *
3312 */
3313static int ext4_split_extent(handle_t *handle,
3314 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003315 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003316 struct ext4_map_blocks *map,
3317 int split_flag,
3318 int flags)
3319{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003320 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003321 ext4_lblk_t ee_block;
3322 struct ext4_extent *ex;
3323 unsigned int ee_len, depth;
3324 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003325 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003326 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003327 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003328
3329 depth = ext_depth(inode);
3330 ex = path[depth].p_ext;
3331 ee_block = le32_to_cpu(ex->ee_block);
3332 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003333 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003334
3335 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003336 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003337 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003338 if (unwritten)
3339 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3340 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003341 if (split_flag & EXT4_EXT_DATA_VALID2)
3342 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003343 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003344 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003345 if (err)
3346 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003347 } else {
3348 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003349 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003350 /*
3351 * Update path is required because previous ext4_split_extent_at() may
3352 * result in split of original leaf or extent zeroout.
3353 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003354 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003355 if (IS_ERR(path))
3356 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003357 depth = ext_depth(inode);
3358 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003359 if (!ex) {
3360 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3361 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003362 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003363 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003364 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003365 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003366
3367 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003368 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003369 if (unwritten) {
3370 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003371 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003372 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003373 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003374 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003375 map->m_lblk, split_flag1, flags);
3376 if (err)
3377 goto out;
3378 }
3379
3380 ext4_ext_show_leaf(inode, path);
3381out:
Zheng Liu3a225672013-03-10 21:20:23 -04003382 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003383}
3384
Amit Arora56055d32007-07-17 21:42:38 -04003385/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003386 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003387 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003388 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003389 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003390 * There are three possibilities:
3391 * a> There is no split required: Entire extent should be initialized
3392 * b> Splits in two extents: Write is happening at either end of the extent
3393 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003394 *
3395 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003396 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003397 * - The extent pointed to by 'path' contains a superset
3398 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3399 *
3400 * Post-conditions on success:
3401 * - the returned value is the number of blocks beyond map->l_lblk
3402 * that are allocated and initialized.
3403 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003404 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003405static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003406 struct inode *inode,
3407 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003408 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003409 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003410{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003411 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003412 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003413 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003414 struct ext4_map_blocks split_map;
3415 struct ext4_extent zero_ex;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003416 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003417 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003418 unsigned int ee_len, depth, map_len = map->m_len;
3419 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003420 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003421 int split_flag = 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003422
3423 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3424 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003425 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003426
Zheng Liu67a5da52012-08-17 09:54:17 -04003427 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003428 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3429 inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003430 if (eof_block < map->m_lblk + map_len)
3431 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003432
3433 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003434 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003435 ex = path[depth].p_ext;
3436 ee_block = le32_to_cpu(ex->ee_block);
3437 ee_len = ext4_ext_get_actual_len(ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003438 zero_ex.ee_len = 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003439
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003440 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3441
3442 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003443 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003444 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003445
3446 /*
3447 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003448 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003449 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003450 * memmove() calls. Transferring to the left is the common case in
3451 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3452 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003453 *
3454 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003455 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003456 * This would require removing the extent if the transfer
3457 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003458 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003459 * same extent tree node.
3460 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003461 if ((map->m_lblk == ee_block) &&
3462 /* See if we can merge left */
3463 (map_len < ee_len) && /*L1*/
3464 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003465 ext4_lblk_t prev_lblk;
3466 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003467 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003468
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003469 abut_ex = ex - 1;
3470 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3471 prev_len = ext4_ext_get_actual_len(abut_ex);
3472 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003473 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003474
3475 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003476 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003477 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003478 * - C1: abut_ex is initialized,
3479 * - C2: abut_ex is logically abutting ex,
3480 * - C3: abut_ex is physically abutting ex,
3481 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003482 * overflowing the (initialized) length limit.
3483 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003484 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003485 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3486 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003487 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003488 err = ext4_ext_get_access(handle, inode, path + depth);
3489 if (err)
3490 goto out;
3491
3492 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003493 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003494
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003495 /* Shift the start of ex by 'map_len' blocks */
3496 ex->ee_block = cpu_to_le32(ee_block + map_len);
3497 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3498 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003499 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003500
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003501 /* Extend abut_ex by 'map_len' blocks */
3502 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003503
3504 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003505 allocated = map_len;
3506 }
3507 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3508 (map_len < ee_len) && /*L1*/
3509 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3510 /* See if we can merge right */
3511 ext4_lblk_t next_lblk;
3512 ext4_fsblk_t next_pblk, ee_pblk;
3513 unsigned int next_len;
3514
3515 abut_ex = ex + 1;
3516 next_lblk = le32_to_cpu(abut_ex->ee_block);
3517 next_len = ext4_ext_get_actual_len(abut_ex);
3518 next_pblk = ext4_ext_pblock(abut_ex);
3519 ee_pblk = ext4_ext_pblock(ex);
3520
3521 /*
3522 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3523 * upon those conditions:
3524 * - C1: abut_ex is initialized,
3525 * - C2: abut_ex is logically abutting ex,
3526 * - C3: abut_ex is physically abutting ex,
3527 * - C4: abut_ex can receive the additional blocks without
3528 * overflowing the (initialized) length limit.
3529 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003530 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003531 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3532 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3533 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3534 err = ext4_ext_get_access(handle, inode, path + depth);
3535 if (err)
3536 goto out;
3537
3538 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3539 map, ex, abut_ex);
3540
3541 /* Shift the start of abut_ex by 'map_len' blocks */
3542 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3543 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3544 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003545 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003546
3547 /* Extend abut_ex by 'map_len' blocks */
3548 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3549
3550 /* Result: number of initialized blocks past m_lblk */
3551 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003552 }
3553 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003554 if (allocated) {
3555 /* Mark the block containing both extents as dirty */
3556 ext4_ext_dirty(handle, inode, path + depth);
3557
3558 /* Update path to point to the right extent */
3559 path[depth].p_ext = abut_ex;
3560 goto out;
3561 } else
3562 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003563
Yongqiang Yang667eff32011-05-03 12:25:07 -04003564 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003565 /*
3566 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003567 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003568 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003569 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003570
Zheng Liu67a5da52012-08-17 09:54:17 -04003571 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3572 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003573 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003574
Theodore Ts'o36086d42015-10-03 10:49:29 -04003575 if (ext4_encrypted_inode(inode))
3576 max_zeroout = 0;
3577
Zheng Liu67a5da52012-08-17 09:54:17 -04003578 /* If extent is less than s_max_zeroout_kb, zeroout directly */
3579 if (max_zeroout && (ee_len <= max_zeroout)) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003580 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003581 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003582 goto out;
Zheng Liuadb23552013-03-10 21:13:05 -04003583 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003584 zero_ex.ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003585 ext4_ext_store_pblock(&zero_ex, ext4_ext_pblock(ex));
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003586
3587 err = ext4_ext_get_access(handle, inode, path + depth);
3588 if (err)
3589 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003590 ext4_ext_mark_initialized(ex);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003591 ext4_ext_try_to_merge(handle, inode, path, ex);
3592 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003593 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003594 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003595
Amit Arora56055d32007-07-17 21:42:38 -04003596 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003597 * four cases:
3598 * 1. split the extent into three extents.
3599 * 2. split the extent into two extents, zeroout the first half.
3600 * 3. split the extent into two extents, zeroout the second half.
3601 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003602 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003603 split_map.m_lblk = map->m_lblk;
3604 split_map.m_len = map->m_len;
3605
Zheng Liu67a5da52012-08-17 09:54:17 -04003606 if (max_zeroout && (allocated > map->m_len)) {
3607 if (allocated <= max_zeroout) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003608 /* case 3 */
3609 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003610 cpu_to_le32(map->m_lblk);
3611 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003612 ext4_ext_store_pblock(&zero_ex,
3613 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3614 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003615 if (err)
3616 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003617 split_map.m_lblk = map->m_lblk;
3618 split_map.m_len = allocated;
Zheng Liu67a5da52012-08-17 09:54:17 -04003619 } else if (map->m_lblk - ee_block + map->m_len < max_zeroout) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003620 /* case 2 */
3621 if (map->m_lblk != ee_block) {
3622 zero_ex.ee_block = ex->ee_block;
3623 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3624 ee_block);
3625 ext4_ext_store_pblock(&zero_ex,
3626 ext4_ext_pblock(ex));
3627 err = ext4_ext_zeroout(inode, &zero_ex);
3628 if (err)
3629 goto out;
3630 }
3631
Yongqiang Yang667eff32011-05-03 12:25:07 -04003632 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003633 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3634 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003635 }
3636 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003637
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003638 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3639 flags);
3640 if (err > 0)
3641 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003642out:
Zheng Liuadb23552013-03-10 21:13:05 -04003643 /* If we have gotten a failure, don't zero out status tree */
3644 if (!err)
Zheng Liud7b2a002013-08-28 14:47:06 -04003645 err = ext4_zeroout_es(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003646 return err ? err : allocated;
3647}
3648
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003649/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003650 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003651 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003652 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003653 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003654 * Writing to an unwritten extent may result in splitting the unwritten
3655 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003656 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003657 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003658 * b> Splits in two extents: Write is happening at either end of the extent
3659 * c> Splits in three extents: Somone is writing in middle of the extent
3660 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003661 * This works the same way in the case of initialized -> unwritten conversion.
3662 *
Mingming Cao00314622009-09-28 15:49:08 -04003663 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003664 * the unwritten extent split. To prevent ENOSPC occur at the IO
3665 * complete, we need to split the unwritten extent before DIO submit
3666 * the IO. The unwritten extent called at this time will be split
3667 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003668 * being filled will be convert to initialized by the end_io callback function
3669 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003670 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003671 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003672 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003673static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003674 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003675 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003676 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003677 int flags)
3678{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003679 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003680 ext4_lblk_t eof_block;
3681 ext4_lblk_t ee_block;
3682 struct ext4_extent *ex;
3683 unsigned int ee_len;
3684 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003685
Lukas Czernerb8a86842014-03-18 18:05:35 -04003686 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3687 __func__, inode->i_ino,
3688 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003689
3690 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3691 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003692 if (eof_block < map->m_lblk + map->m_len)
3693 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003694 /*
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003695 * It is safe to convert extent to initialized via explicit
3696 * zeroout only if extent is fully insde i_size or new_size.
3697 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003698 depth = ext_depth(inode);
3699 ex = path[depth].p_ext;
3700 ee_block = le32_to_cpu(ex->ee_block);
3701 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca087a2010-05-16 06:00:00 -04003702
Lukas Czernerb8a86842014-03-18 18:05:35 -04003703 /* Convert to unwritten */
3704 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3705 split_flag |= EXT4_EXT_DATA_VALID1;
3706 /* Convert to initialized */
3707 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3708 split_flag |= ee_block + ee_len <= eof_block ?
3709 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003710 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003711 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003712 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003713 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003714}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003715
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003716static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003717 struct inode *inode,
3718 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003719 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003720{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003721 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003722 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003723 ext4_lblk_t ee_block;
3724 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003725 int depth;
3726 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003727
3728 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003729 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003730 ee_block = le32_to_cpu(ex->ee_block);
3731 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003732
Yongqiang Yang197217a2011-05-03 11:45:29 -04003733 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3734 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003735 (unsigned long long)ee_block, ee_len);
3736
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003737 /* If extent is larger than requested it is a clear sign that we still
3738 * have some extent state machine issues left. So extent_split is still
3739 * required.
3740 * TODO: Once all related issues will be fixed this situation should be
3741 * illegal.
3742 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003743 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003744#ifdef EXT4_DEBUG
3745 ext4_warning("Inode (%ld) finished: extent logical block %llu,"
3746 " len %u; IO logical block %llu, len %u\n",
3747 inode->i_ino, (unsigned long long)ee_block, ee_len,
3748 (unsigned long long)map->m_lblk, map->m_len);
3749#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003750 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003751 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003752 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003753 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003754 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003755 if (IS_ERR(path))
3756 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003757 depth = ext_depth(inode);
3758 ex = path[depth].p_ext;
3759 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003760
Mingming Cao00314622009-09-28 15:49:08 -04003761 err = ext4_ext_get_access(handle, inode, path + depth);
3762 if (err)
3763 goto out;
3764 /* first mark the extent as initialized */
3765 ext4_ext_mark_initialized(ex);
3766
Yongqiang Yang197217a2011-05-03 11:45:29 -04003767 /* note: ext4_ext_correct_indexes() isn't needed here because
3768 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003769 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003770 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003771
Mingming Cao00314622009-09-28 15:49:08 -04003772 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003773 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003774out:
3775 ext4_ext_show_leaf(inode, path);
3776 return err;
3777}
3778
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003779static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3780 sector_t block, int count)
3781{
3782 int i;
3783 for (i = 0; i < count; i++)
3784 unmap_underlying_metadata(bdev, block + i);
3785}
3786
Theodore Ts'o58590b02010-10-27 21:23:12 -04003787/*
3788 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3789 */
3790static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003791 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003792 struct ext4_ext_path *path,
3793 unsigned int len)
3794{
3795 int i, depth;
3796 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003797 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003798
3799 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3800 return 0;
3801
3802 depth = ext_depth(inode);
3803 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003804
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003805 /*
3806 * We're going to remove EOFBLOCKS_FL entirely in future so we
3807 * do not care for this case anymore. Simply remove the flag
3808 * if there are no extents.
3809 */
3810 if (unlikely(!eh->eh_entries))
3811 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003812 last_ex = EXT_LAST_EXTENT(eh);
3813 /*
3814 * We should clear the EOFBLOCKS_FL flag if we are writing the
3815 * last block in the last extent in the file. We test this by
3816 * first checking to see if the caller to
3817 * ext4_ext_get_blocks() was interested in the last block (or
3818 * a block beyond the last block) in the current extent. If
3819 * this turns out to be false, we can bail out from this
3820 * function immediately.
3821 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003822 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003823 ext4_ext_get_actual_len(last_ex))
3824 return 0;
3825 /*
3826 * If the caller does appear to be planning to write at or
3827 * beyond the end of the current extent, we then test to see
3828 * if the current extent is the last extent in the file, by
3829 * checking to make sure it was reached via the rightmost node
3830 * at each level of the tree.
3831 */
3832 for (i = depth-1; i >= 0; i--)
3833 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3834 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003835out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003836 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3837 return ext4_mark_inode_dirty(handle, inode);
3838}
3839
Aditya Kali7b415bf2011-09-09 19:04:51 -04003840/**
3841 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3842 *
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003843 * Return 1 if there is a delalloc block in the range, otherwise 0.
Aditya Kali7b415bf2011-09-09 19:04:51 -04003844 */
Zheng Liuf7fec032013-02-18 00:28:47 -05003845int ext4_find_delalloc_range(struct inode *inode,
3846 ext4_lblk_t lblk_start,
3847 ext4_lblk_t lblk_end)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003848{
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003849 struct extent_status es;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003850
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04003851 ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es);
Zheng Liu06b0c882013-02-18 00:26:51 -05003852 if (es.es_len == 0)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003853 return 0; /* there is no delay extent in this tree */
Zheng Liu06b0c882013-02-18 00:26:51 -05003854 else if (es.es_lblk <= lblk_start &&
3855 lblk_start < es.es_lblk + es.es_len)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003856 return 1;
Zheng Liu06b0c882013-02-18 00:26:51 -05003857 else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003858 return 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003859 else
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003860 return 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003861}
3862
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003863int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003864{
3865 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3866 ext4_lblk_t lblk_start, lblk_end;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003867 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003868 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3869
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003870 return ext4_find_delalloc_range(inode, lblk_start, lblk_end);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003871}
3872
3873/**
3874 * Determines how many complete clusters (out of those specified by the 'map')
3875 * are under delalloc and were reserved quota for.
3876 * This function is called when we are writing out the blocks that were
3877 * originally written with their allocation delayed, but then the space was
3878 * allocated using fallocate() before the delayed allocation could be resolved.
3879 * The cases to look for are:
3880 * ('=' indicated delayed allocated blocks
3881 * '-' indicates non-delayed allocated blocks)
3882 * (a) partial clusters towards beginning and/or end outside of allocated range
3883 * are not delalloc'ed.
3884 * Ex:
3885 * |----c---=|====c====|====c====|===-c----|
3886 * |++++++ allocated ++++++|
3887 * ==> 4 complete clusters in above example
3888 *
3889 * (b) partial cluster (outside of allocated range) towards either end is
3890 * marked for delayed allocation. In this case, we will exclude that
3891 * cluster.
3892 * Ex:
3893 * |----====c========|========c========|
3894 * |++++++ allocated ++++++|
3895 * ==> 1 complete clusters in above example
3896 *
3897 * Ex:
3898 * |================c================|
3899 * |++++++ allocated ++++++|
3900 * ==> 0 complete clusters in above example
3901 *
3902 * The ext4_da_update_reserve_space will be called only if we
3903 * determine here that there were some "entire" clusters that span
3904 * this 'allocated' range.
3905 * In the non-bigalloc case, this function will just end up returning num_blks
3906 * without ever calling ext4_find_delalloc_range.
3907 */
3908static unsigned int
3909get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3910 unsigned int num_blks)
3911{
3912 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3913 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3914 ext4_lblk_t lblk_from, lblk_to, c_offset;
3915 unsigned int allocated_clusters = 0;
3916
3917 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3918 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3919
3920 /* max possible clusters for this allocation */
3921 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3922
Aditya Kalid8990242011-09-09 19:18:51 -04003923 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3924
Aditya Kali7b415bf2011-09-09 19:04:51 -04003925 /* Check towards left side */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003926 c_offset = EXT4_LBLK_COFF(sbi, lblk_start);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003927 if (c_offset) {
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003928 lblk_from = EXT4_LBLK_CMASK(sbi, lblk_start);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003929 lblk_to = lblk_from + c_offset - 1;
3930
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003931 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003932 allocated_clusters--;
3933 }
3934
3935 /* Now check towards right. */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003936 c_offset = EXT4_LBLK_COFF(sbi, lblk_start + num_blks);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003937 if (allocated_clusters && c_offset) {
3938 lblk_from = lblk_start + num_blks;
3939 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3940
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003941 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003942 allocated_clusters--;
3943 }
3944
3945 return allocated_clusters;
3946}
3947
Mingming Cao00314622009-09-28 15:49:08 -04003948static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003949convert_initialized_extent(handle_t *handle, struct inode *inode,
3950 struct ext4_map_blocks *map,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05003951 struct ext4_ext_path **ppath,
Eryu Guan56263b42016-02-12 01:23:00 -05003952 unsigned int allocated)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003953{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003954 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003955 struct ext4_extent *ex;
3956 ext4_lblk_t ee_block;
3957 unsigned int ee_len;
3958 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003959 int err = 0;
3960
3961 /*
3962 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003963 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003964 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003965 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3966 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003967
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003968 depth = ext_depth(inode);
3969 ex = path[depth].p_ext;
3970 ee_block = le32_to_cpu(ex->ee_block);
3971 ee_len = ext4_ext_get_actual_len(ex);
3972
3973 ext_debug("%s: inode %lu, logical"
3974 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3975 (unsigned long long)ee_block, ee_len);
3976
3977 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003978 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003979 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3980 if (err < 0)
3981 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003982 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003983 if (IS_ERR(path))
3984 return PTR_ERR(path);
3985 depth = ext_depth(inode);
3986 ex = path[depth].p_ext;
3987 if (!ex) {
3988 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3989 (unsigned long) map->m_lblk);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003990 return -EFSCORRUPTED;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003991 }
3992 }
3993
3994 err = ext4_ext_get_access(handle, inode, path + depth);
3995 if (err)
3996 return err;
3997 /* first mark the extent as unwritten */
3998 ext4_ext_mark_unwritten(ex);
3999
4000 /* note: ext4_ext_correct_indexes() isn't needed here because
4001 * borders are not changed
4002 */
4003 ext4_ext_try_to_merge(handle, inode, path, ex);
4004
4005 /* Mark modified extent as dirty */
4006 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
4007 if (err)
4008 return err;
4009 ext4_ext_show_leaf(inode, path);
4010
4011 ext4_update_inode_fsync_trans(handle, inode, 1);
4012 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
4013 if (err)
4014 return err;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004015 map->m_flags |= EXT4_MAP_UNWRITTEN;
4016 if (allocated > map->m_len)
4017 allocated = map->m_len;
4018 map->m_len = allocated;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004019 return allocated;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004020}
4021
4022static int
Lukas Czerner556615d2014-04-20 23:45:47 -04004023ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004024 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004025 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004026 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04004027{
Theodore Ts'odfe50802014-09-01 14:37:09 -04004028 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04004029 int ret = 0;
4030 int err = 0;
4031
Lukas Czerner556615d2014-04-20 23:45:47 -04004032 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05004033 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004034 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04004035 flags, allocated);
4036 ext4_ext_show_leaf(inode, path);
4037
Lukas Czerner27dd4382013-04-09 22:11:22 -04004038 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004039 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04004040 * allocate metadata blocks for the new extent block if needed.
4041 */
4042 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4043
Lukas Czerner556615d2014-04-20 23:45:47 -04004044 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05004045 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04004046
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004047 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004048 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004049 ret = ext4_split_convert_extents(handle, inode, map, ppath,
4050 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004051 if (ret <= 0)
4052 goto out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004053 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004054 goto out;
4055 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004056 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004057 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Jan Karac86d8db2015-12-07 15:10:26 -05004058 if (flags & EXT4_GET_BLOCKS_ZERO) {
4059 if (allocated > map->m_len)
4060 allocated = map->m_len;
4061 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
4062 allocated);
4063 if (err < 0)
4064 goto out2;
4065 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04004066 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004067 ppath);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004068 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05004069 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05004070 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4071 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004072 } else
4073 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04004074 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05004075 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04004076 if (allocated > map->m_len)
4077 allocated = map->m_len;
4078 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004079 goto out2;
4080 }
4081 /* buffered IO case */
4082 /*
4083 * repeat fallocate creation request
4084 * we already have an unwritten extent
4085 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004086 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05004087 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004088 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004089 }
Mingming Cao00314622009-09-28 15:49:08 -04004090
4091 /* buffered READ or buffered write_begin() lookup */
4092 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4093 /*
4094 * We have blocks reserved already. We
4095 * return allocated blocks so that delalloc
4096 * won't do block reservation for us. But
4097 * the buffer head will be unmapped so that
4098 * a read from the block returns 0s.
4099 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004100 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004101 goto out1;
4102 }
4103
4104 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04004105 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004106 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004107 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04004108out:
4109 if (ret <= 0) {
4110 err = ret;
4111 goto out2;
4112 } else
4113 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004114 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004115 /*
4116 * if we allocated more blocks than requested
4117 * we need to make sure we unmap the extra block
4118 * allocated. The actual needed block will get
4119 * unmapped later when we find the buffer_head marked
4120 * new.
4121 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004122 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004123 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004124 newblock + map->m_len,
4125 allocated - map->m_len);
4126 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004127 }
Zheng Liu3a225672013-03-10 21:20:23 -04004128 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004129
4130 /*
4131 * If we have done fallocate with the offset that is already
4132 * delayed allocated, we would have block reservation
4133 * and quota reservation done in the delayed write path.
4134 * But fallocate would have already updated quota and block
4135 * count for this offset. So cancel these reservation
4136 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004137 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4138 unsigned int reserved_clusters;
4139 reserved_clusters = get_reserved_cluster_alloc(inode,
4140 map->m_lblk, map->m_len);
4141 if (reserved_clusters)
4142 ext4_da_update_reserve_space(inode,
4143 reserved_clusters,
4144 0);
4145 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004146
Mingming Cao00314622009-09-28 15:49:08 -04004147map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004148 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004149 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4150 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4151 map->m_len);
4152 if (err < 0)
4153 goto out2;
4154 }
Mingming Cao00314622009-09-28 15:49:08 -04004155out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004156 if (allocated > map->m_len)
4157 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04004158 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004159 map->m_pblk = newblock;
4160 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004161out2:
Mingming Cao00314622009-09-28 15:49:08 -04004162 return err ? err : allocated;
4163}
Theodore Ts'o58590b02010-10-27 21:23:12 -04004164
Mingming Cao00314622009-09-28 15:49:08 -04004165/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004166 * get_implied_cluster_alloc - check to see if the requested
4167 * allocation (in the map structure) overlaps with a cluster already
4168 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04004169 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004170 * @map The requested lblk->pblk mapping
4171 * @ex The extent structure which might contain an implied
4172 * cluster allocation
4173 *
4174 * This function is called by ext4_ext_map_blocks() after we failed to
4175 * find blocks that were already in the inode's extent tree. Hence,
4176 * we know that the beginning of the requested region cannot overlap
4177 * the extent from the inode's extent tree. There are three cases we
4178 * want to catch. The first is this case:
4179 *
4180 * |--- cluster # N--|
4181 * |--- extent ---| |---- requested region ---|
4182 * |==========|
4183 *
4184 * The second case that we need to test for is this one:
4185 *
4186 * |--------- cluster # N ----------------|
4187 * |--- requested region --| |------- extent ----|
4188 * |=======================|
4189 *
4190 * The third case is when the requested region lies between two extents
4191 * within the same cluster:
4192 * |------------- cluster # N-------------|
4193 * |----- ex -----| |---- ex_right ----|
4194 * |------ requested region ------|
4195 * |================|
4196 *
4197 * In each of the above cases, we need to set the map->m_pblk and
4198 * map->m_len so it corresponds to the return the extent labelled as
4199 * "|====|" from cluster #N, since it is already in use for data in
4200 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4201 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4202 * as a new "allocated" block region. Otherwise, we will return 0 and
4203 * ext4_ext_map_blocks() will then allocate one or more new clusters
4204 * by calling ext4_mb_new_blocks().
4205 */
Aditya Kalid8990242011-09-09 19:18:51 -04004206static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004207 struct ext4_map_blocks *map,
4208 struct ext4_extent *ex,
4209 struct ext4_ext_path *path)
4210{
Aditya Kalid8990242011-09-09 19:18:51 -04004211 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004212 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004213 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05004214 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004215 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4216 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4217 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4218
4219 /* The extent passed in that we are trying to match */
4220 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4221 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4222
4223 /* The requested region passed into ext4_map_blocks() */
4224 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004225
4226 if ((rr_cluster_start == ex_cluster_end) ||
4227 (rr_cluster_start == ex_cluster_start)) {
4228 if (rr_cluster_start == ex_cluster_end)
4229 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004230 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004231 map->m_len = min(map->m_len,
4232 (unsigned) sbi->s_cluster_ratio - c_offset);
4233 /*
4234 * Check for and handle this case:
4235 *
4236 * |--------- cluster # N-------------|
4237 * |------- extent ----|
4238 * |--- requested region ---|
4239 * |===========|
4240 */
4241
4242 if (map->m_lblk < ee_block)
4243 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4244
4245 /*
4246 * Check for the case where there is already another allocated
4247 * block to the right of 'ex' but before the end of the cluster.
4248 *
4249 * |------------- cluster # N-------------|
4250 * |----- ex -----| |---- ex_right ----|
4251 * |------ requested region ------|
4252 * |================|
4253 */
4254 if (map->m_lblk > ee_block) {
4255 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4256 map->m_len = min(map->m_len, next - map->m_lblk);
4257 }
Aditya Kalid8990242011-09-09 19:18:51 -04004258
4259 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004260 return 1;
4261 }
Aditya Kalid8990242011-09-09 19:18:51 -04004262
4263 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004264 return 0;
4265}
4266
4267
4268/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004269 * Block allocation/map/preallocation routine for extents based files
4270 *
4271 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004272 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004273 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4274 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004275 *
4276 * return > 0, number of of blocks already mapped/allocated
4277 * if create == 0 and these are pre-allocated blocks
4278 * buffer head is unmapped
4279 * otherwise blocks are mapped
4280 *
4281 * return = 0, if plain look up failed (blocks have not been allocated)
4282 * buffer head is unmapped
4283 *
4284 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004285 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004286int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4287 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004288{
4289 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004290 struct ext4_extent newex, *ex, *ex2;
4291 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004292 ext4_fsblk_t newblock = 0;
Eric Whitneyce37c422014-02-19 18:52:39 -05004293 int free_on_err = 0, err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004294 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004295 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004296 struct ext4_allocation_request ar;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004297 ext4_lblk_t cluster_offset;
Jan Karacbd75842014-11-25 11:41:49 -05004298 bool map_from_cluster = false;
Alex Tomasa86c6182006-10-11 01:21:03 -07004299
Mingming84fe3be2009-09-01 08:44:37 -04004300 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004301 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004302 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004303
Alex Tomasa86c6182006-10-11 01:21:03 -07004304 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004305 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004306 if (IS_ERR(path)) {
4307 err = PTR_ERR(path);
4308 path = NULL;
4309 goto out2;
4310 }
4311
4312 depth = ext_depth(inode);
4313
4314 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004315 * consistent leaf must not be empty;
4316 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004317 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004318 */
Frank Mayhar273df552010-03-02 11:46:09 -05004319 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4320 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004321 "lblock: %lu, depth: %d pblock %lld",
4322 (unsigned long) map->m_lblk, depth,
4323 path[depth].p_block);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004324 err = -EFSCORRUPTED;
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004325 goto out2;
4326 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004327
Avantika Mathur7e028972006-12-06 20:41:33 -08004328 ex = path[depth].p_ext;
4329 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004330 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004331 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004332 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004333
Lukas Czernerb8a86842014-03-18 18:05:35 -04004334
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004335 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004336 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004337 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004338 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004339 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004340
4341 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4342
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004343 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004344 if (in_range(map->m_lblk, ee_block, ee_len)) {
4345 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004346 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004347 allocated = ee_len - (map->m_lblk - ee_block);
4348 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4349 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004350
Lukas Czernerb8a86842014-03-18 18:05:35 -04004351 /*
4352 * If the extent is initialized check whether the
4353 * caller wants to convert it to unwritten.
4354 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004355 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004356 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004357 allocated = convert_initialized_extent(
Theodore Ts'o4f224b82014-09-01 14:36:09 -04004358 handle, inode, map, &path,
Eric Whitney29c6eaf2016-02-22 22:58:55 -05004359 allocated);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004360 goto out2;
Lukas Czerner556615d2014-04-20 23:45:47 -04004361 } else if (!ext4_ext_is_unwritten(ex))
Lukas Czerner78771912012-03-19 23:05:43 -04004362 goto out;
Zheng Liu69eb33d2013-02-18 00:31:07 -05004363
Lukas Czerner556615d2014-04-20 23:45:47 -04004364 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004365 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004366 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004367 if (ret < 0)
4368 err = ret;
4369 else
4370 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004371 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004372 }
4373 }
4374
4375 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004376 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004377 * we couldn't try to create block if create flag is zero
4378 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004379 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Jan Kara140a5252016-03-09 22:46:57 -05004380 ext4_lblk_t hole_start, hole_len;
4381
Jan Karafacab4d2016-03-09 22:54:00 -05004382 hole_start = map->m_lblk;
4383 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
Amit Arora56055d32007-07-17 21:42:38 -04004384 /*
4385 * put just found gap into cache to speed up
4386 * subsequent requests
4387 */
Jan Kara140a5252016-03-09 22:46:57 -05004388 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
Jan Karafacab4d2016-03-09 22:54:00 -05004389
4390 /* Update hole_len to reflect hole size after map->m_lblk */
4391 if (hole_start != map->m_lblk)
4392 hole_len -= map->m_lblk - hole_start;
4393 map->m_pblk = 0;
4394 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4395
Alex Tomasa86c6182006-10-11 01:21:03 -07004396 goto out2;
4397 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004398
Alex Tomasa86c6182006-10-11 01:21:03 -07004399 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004400 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004401 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004402 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004403 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004404
4405 /*
4406 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004407 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004408 */
4409 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004410 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004411 ar.len = allocated = map->m_len;
4412 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004413 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004414 goto got_allocated_blocks;
4415 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004416
Alex Tomasc9de5602008-01-29 00:19:52 -05004417 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004418 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004419 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4420 if (err)
4421 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004422 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004423 ex2 = NULL;
4424 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004425 if (err)
4426 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004427
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004428 /* Check if the extent after searching to the right implies a
4429 * cluster we can use. */
4430 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004431 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004432 ar.len = allocated = map->m_len;
4433 newblock = map->m_pblk;
Jan Karacbd75842014-11-25 11:41:49 -05004434 map_from_cluster = true;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004435 goto got_allocated_blocks;
4436 }
4437
Amit Arora749269f2007-07-18 09:02:56 -04004438 /*
4439 * See if request is beyond maximum number of blocks we can have in
4440 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004441 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4442 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004443 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004444 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004445 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004446 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004447 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4448 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4449 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004450
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004451 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004452 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004453 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004454 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004455 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004456 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004457 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004458
4459 /* allocate new block */
4460 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004461 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4462 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004463 /*
4464 * We calculate the offset from the beginning of the cluster
4465 * for the logical block number, since when we allocate a
4466 * physical cluster, the physical block should start at the
4467 * same offset from the beginning of the cluster. This is
4468 * needed so that future calls to get_implied_cluster_alloc()
4469 * work correctly.
4470 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004471 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004472 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4473 ar.goal -= offset;
4474 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004475 if (S_ISREG(inode->i_mode))
4476 ar.flags = EXT4_MB_HINT_DATA;
4477 else
4478 /* disable in-core preallocation for non-regular files */
4479 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004480 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4481 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004482 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4483 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04004484 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4485 ar.flags |= EXT4_MB_USE_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004486 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004487 if (!newblock)
4488 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004489 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004490 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004491 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004492 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004493 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4494 if (ar.len > allocated)
4495 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004496
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004497got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004498 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004499 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004500 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004501 /* Mark unwritten */
4502 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4503 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004504 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004505 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004506
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004507 err = 0;
4508 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4509 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4510 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004511 if (!err)
Theodore Ts'odfe50802014-09-01 14:37:09 -04004512 err = ext4_ext_insert_extent(handle, inode, &path,
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004513 &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004514
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004515 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004516 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4517 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004518 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004519 /* not a good idea to call discard here directly,
4520 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004521 ext4_discard_preallocations(inode);
Theodore Ts'oc8e15132013-07-15 00:09:37 -04004522 ext4_free_blocks(handle, inode, NULL, newblock,
4523 EXT4_C2B(sbi, allocated_clusters), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004524 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004525 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004526
Alex Tomasa86c6182006-10-11 01:21:03 -07004527 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004528 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004529 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004530 if (allocated > map->m_len)
4531 allocated = map->m_len;
4532 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004533
Jan Karab436b9b2009-12-08 23:51:10 -05004534 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004535 * Update reserved blocks/metadata blocks after successful
4536 * block allocation which had been deferred till now.
4537 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004538 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004539 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004540 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004541 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004542 */
4543 reserved_clusters = get_reserved_cluster_alloc(inode,
4544 map->m_lblk, allocated);
Eric Whitney9d21c9f2015-04-03 00:17:31 -04004545 if (!map_from_cluster) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04004546 BUG_ON(allocated_clusters < reserved_clusters);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004547 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f2612011-09-09 19:20:51 -04004548 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004549 int reservation = allocated_clusters -
4550 reserved_clusters;
4551 /*
4552 * It seems we claimed few clusters outside of
4553 * the range of this allocation. We should give
4554 * it back to the reservation pool. This can
4555 * happen in the following case:
4556 *
4557 * * Suppose s_cluster_ratio is 4 (i.e., each
4558 * cluster has 4 blocks. Thus, the clusters
4559 * are [0-3],[4-7],[8-11]...
4560 * * First comes delayed allocation write for
4561 * logical blocks 10 & 11. Since there were no
4562 * previous delayed allocated blocks in the
4563 * range [8-11], we would reserve 1 cluster
4564 * for this write.
4565 * * Next comes write for logical blocks 3 to 8.
4566 * In this case, we will reserve 2 clusters
4567 * (for [0-3] and [4-7]; and not for [8-11] as
4568 * that range has a delayed allocated blocks.
4569 * Thus total reserved clusters now becomes 3.
4570 * * Now, during the delayed allocation writeout
4571 * time, we will first write blocks [3-8] and
4572 * allocate 3 clusters for writing these
4573 * blocks. Also, we would claim all these
4574 * three clusters above.
4575 * * Now when we come here to writeout the
4576 * blocks [10-11], we would expect to claim
4577 * the reservation of 1 cluster we had made
4578 * (and we would claim it since there are no
4579 * more delayed allocated blocks in the range
4580 * [8-11]. But our reserved cluster count had
4581 * already gone to 0.
4582 *
4583 * Thus, at the step 4 above when we determine
4584 * that there are still some unwritten delayed
4585 * allocated blocks outside of our current
4586 * block range, we should increment the
4587 * reserved clusters count so that when the
4588 * remaining blocks finally gets written, we
4589 * could claim them.
4590 */
Aditya Kali5356f2612011-09-09 19:20:51 -04004591 dquot_reserve_block(inode,
4592 EXT4_C2B(sbi, reservation));
4593 spin_lock(&ei->i_block_reservation_lock);
4594 ei->i_reserved_data_blocks += reservation;
4595 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004596 }
Lukas Czerner232ec872013-03-10 22:46:30 -04004597 /*
4598 * We will claim quota for all newly allocated blocks.
4599 * We're updating the reserved space *after* the
4600 * correction above so we do not accidentally free
4601 * all the metadata reservation because we might
4602 * actually need it later on.
4603 */
4604 ext4_da_update_reserve_space(inode, allocated_clusters,
4605 1);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004606 }
4607 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004608
4609 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004610 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004611 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004612 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004613 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004614 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004615 else
Jan Karab436b9b2009-12-08 23:51:10 -05004616 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004617out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004618 if (allocated > map->m_len)
4619 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004620 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004621 map->m_flags |= EXT4_MAP_MAPPED;
4622 map->m_pblk = newblock;
4623 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004624out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004625 ext4_ext_drop_refs(path);
4626 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004627
Theodore Ts'o63b99962013-07-16 10:28:47 -04004628 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4629 err ? err : allocated);
Lukas Czerner78771912012-03-19 23:05:43 -04004630 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004631}
4632
Theodore Ts'o819c4922013-04-03 12:47:17 -04004633void ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004634{
Alex Tomasa86c6182006-10-11 01:21:03 -07004635 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004636 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004637 int err = 0;
4638
4639 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004640 * TODO: optimization is possible here.
4641 * Probably we need not scan at all,
4642 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004643 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004644
4645 /* we have to know where to truncate from in crash case */
4646 EXT4_I(inode)->i_disksize = inode->i_size;
4647 ext4_mark_inode_dirty(handle, inode);
4648
4649 last_block = (inode->i_size + sb->s_blocksize - 1)
4650 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004651retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004652 err = ext4_es_remove_extent(inode, last_block,
4653 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004654 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004655 cond_resched();
4656 congestion_wait(BLK_RW_ASYNC, HZ/50);
4657 goto retry;
4658 }
4659 if (err) {
4660 ext4_std_error(inode->i_sb, err);
4661 return;
4662 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04004663 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004664 ext4_std_error(inode->i_sb, err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004665}
4666
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004667static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004668 ext4_lblk_t len, loff_t new_size,
4669 int flags, int mode)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004670{
Al Viro496ad9a2013-01-23 17:07:38 -05004671 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004672 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004673 int ret = 0;
4674 int ret2 = 0;
4675 int retries = 0;
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004676 int depth = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004677 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004678 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004679 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004680
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004681 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004682 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004683 /*
4684 * Don't normalize the request if it can fit in one extent so
4685 * that it doesn't get unnecessarily split into multiple
4686 * extents.
4687 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004688 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004689 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004690
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004691 /*
4692 * credits to insert 1 extent into extent tree
4693 */
4694 credits = ext4_chunk_trans_blocks(inode, len);
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004695 /*
4696 * We can only call ext_depth() on extent based inodes
4697 */
4698 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4699 depth = ext_depth(inode);
4700 else
4701 depth = -1;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004702
Amit Aroraa2df2a62007-07-17 21:42:41 -04004703retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004704 while (ret >= 0 && len) {
Lukas Czerner4134f5c2015-06-15 00:20:46 -04004705 /*
4706 * Recalculate credits when extent tree depth changes.
4707 */
4708 if (depth >= 0 && depth != ext_depth(inode)) {
4709 credits = ext4_chunk_trans_blocks(inode, len);
4710 depth = ext_depth(inode);
4711 }
4712
Theodore Ts'o9924a922013-02-08 21:59:22 -05004713 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4714 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004715 if (IS_ERR(handle)) {
4716 ret = PTR_ERR(handle);
4717 break;
4718 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004719 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004720 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004721 ext4_debug("inode #%lu: block %u: len %u: "
4722 "ext4_ext_map_blocks returned %d",
4723 inode->i_ino, map.m_lblk,
4724 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004725 ext4_mark_inode_dirty(handle, inode);
4726 ret2 = ext4_journal_stop(handle);
4727 break;
4728 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004729 map.m_lblk += ret;
4730 map.m_len = len = len - ret;
4731 epos = (loff_t)map.m_lblk << inode->i_blkbits;
4732 inode->i_ctime = ext4_current_time(inode);
4733 if (new_size) {
4734 if (epos > new_size)
4735 epos = new_size;
4736 if (ext4_update_inode_size(inode, epos) & 0x1)
4737 inode->i_mtime = inode->i_ctime;
4738 } else {
4739 if (epos > inode->i_size)
4740 ext4_set_inode_flag(inode,
4741 EXT4_INODE_EOFBLOCKS);
4742 }
4743 ext4_mark_inode_dirty(handle, inode);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004744 ret2 = ext4_journal_stop(handle);
4745 if (ret2)
4746 break;
4747 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004748 if (ret == -ENOSPC &&
4749 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4750 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004751 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004752 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004753
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004754 return ret > 0 ? ret2 : ret;
4755}
4756
Lukas Czernerb8a86842014-03-18 18:05:35 -04004757static long ext4_zero_range(struct file *file, loff_t offset,
4758 loff_t len, int mode)
4759{
4760 struct inode *inode = file_inode(file);
4761 handle_t *handle = NULL;
4762 unsigned int max_blocks;
4763 loff_t new_size = 0;
4764 int ret = 0;
4765 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004766 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004767 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004768 loff_t start, end;
4769 ext4_lblk_t lblk;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004770 unsigned int blkbits = inode->i_blkbits;
4771
4772 trace_ext4_zero_range(inode, offset, len, mode);
4773
jon ernst6c5e73d2014-04-18 11:50:35 -04004774 if (!S_ISREG(inode->i_mode))
4775 return -EINVAL;
4776
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004777 /* Call ext4_force_commit to flush all data in case of data=journal. */
4778 if (ext4_should_journal_data(inode)) {
4779 ret = ext4_force_commit(inode->i_sb);
4780 if (ret)
4781 return ret;
4782 }
4783
Lukas Czernerb8a86842014-03-18 18:05:35 -04004784 /*
Lukas Czernerb8a86842014-03-18 18:05:35 -04004785 * Round up offset. This is not fallocate, we neet to zero out
4786 * blocks, so convert interior block aligned part of the range to
4787 * unwritten and possibly manually zero out unaligned parts of the
4788 * range.
4789 */
4790 start = round_up(offset, 1 << blkbits);
4791 end = round_down((offset + len), 1 << blkbits);
4792
4793 if (start < offset || end > offset + len)
4794 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004795 partial_begin = offset & ((1 << blkbits) - 1);
4796 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004797
4798 lblk = start >> blkbits;
4799 max_blocks = (end >> blkbits);
4800 if (max_blocks < lblk)
4801 max_blocks = 0;
4802 else
4803 max_blocks -= lblk;
4804
Al Viro59551022016-01-22 15:40:57 -05004805 inode_lock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004806
4807 /*
4808 * Indirect files do not support unwritten extnets
4809 */
4810 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4811 ret = -EOPNOTSUPP;
4812 goto out_mutex;
4813 }
4814
4815 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4816 offset + len > i_size_read(inode)) {
4817 new_size = offset + len;
4818 ret = inode_newsize_ok(inode, new_size);
4819 if (ret)
4820 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004821 }
4822
Lukas Czerner0f2af212015-04-03 00:09:13 -04004823 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4824 if (mode & FALLOC_FL_KEEP_SIZE)
4825 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4826
Jan Kara17048e82015-12-07 14:29:17 -05004827 /* Wait all existing dio workers, newcomers will block on i_mutex */
4828 ext4_inode_block_unlocked_dio(inode);
4829 inode_dio_wait(inode);
4830
Lukas Czerner0f2af212015-04-03 00:09:13 -04004831 /* Preallocate the range including the unaligned edges */
4832 if (partial_begin || partial_end) {
4833 ret = ext4_alloc_file_blocks(file,
4834 round_down(offset, 1 << blkbits) >> blkbits,
4835 (round_up((offset + len), 1 << blkbits) -
4836 round_down(offset, 1 << blkbits)) >> blkbits,
4837 new_size, flags, mode);
4838 if (ret)
Jan Kara17048e82015-12-07 14:29:17 -05004839 goto out_dio;
Lukas Czerner0f2af212015-04-03 00:09:13 -04004840
4841 }
4842
4843 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004844 if (max_blocks > 0) {
Lukas Czerner0f2af212015-04-03 00:09:13 -04004845 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4846 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004847
Jan Karaea3d7202015-12-07 14:28:03 -05004848 /*
4849 * Prevent page faults from reinstantiating pages we have
4850 * released from page cache.
4851 */
4852 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara01127842015-12-07 14:34:49 -05004853 ret = ext4_update_disksize_before_punch(inode, offset, len);
4854 if (ret) {
4855 up_write(&EXT4_I(inode)->i_mmap_sem);
4856 goto out_dio;
4857 }
Jan Karaea3d7202015-12-07 14:28:03 -05004858 /* Now release the pages and zero block aligned part of pages */
4859 truncate_pagecache_range(inode, start, end - 1);
4860 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4861
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004862 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4863 flags, mode);
Jan Karaea3d7202015-12-07 14:28:03 -05004864 up_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004865 if (ret)
4866 goto out_dio;
4867 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004868 if (!partial_begin && !partial_end)
4869 goto out_dio;
4870
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004871 /*
4872 * In worst case we have to writeout two nonadjacent unwritten
4873 * blocks and update the inode
4874 */
4875 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4876 if (ext4_should_journal_data(inode))
4877 credits += 2;
4878 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004879 if (IS_ERR(handle)) {
4880 ret = PTR_ERR(handle);
4881 ext4_std_error(inode->i_sb, ret);
4882 goto out_dio;
4883 }
4884
4885 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Lukas Czernere5b30412014-04-01 00:59:21 -04004886 if (new_size) {
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004887 ext4_update_inode_size(inode, new_size);
Lukas Czernere5b30412014-04-01 00:59:21 -04004888 } else {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004889 /*
4890 * Mark that we allocate beyond EOF so the subsequent truncate
4891 * can proceed even if the new size is the same as i_size.
4892 */
4893 if ((offset + len) > i_size_read(inode))
4894 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4895 }
Lukas Czernerb8a86842014-03-18 18:05:35 -04004896 ext4_mark_inode_dirty(handle, inode);
4897
4898 /* Zero out partial block at the edges of the range */
4899 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
4900
4901 if (file->f_flags & O_SYNC)
4902 ext4_handle_sync(handle);
4903
4904 ext4_journal_stop(handle);
4905out_dio:
4906 ext4_inode_resume_unlocked_dio(inode);
4907out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004908 inode_unlock(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004909 return ret;
4910}
4911
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004912/*
4913 * preallocate space for a file. This implements ext4's fallocate file
4914 * operation, which gets called from sys_fallocate system call.
4915 * For block-mapped files, posix_fallocate should fall back to the method
4916 * of writing zeroes to the required new blocks (the same behavior which is
4917 * expected for file systems which do not support fallocate() system call).
4918 */
4919long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4920{
4921 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004922 loff_t new_size = 0;
4923 unsigned int max_blocks;
4924 int ret = 0;
4925 int flags;
4926 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004927 unsigned int blkbits = inode->i_blkbits;
4928
Michael Halcrow2058f832015-04-12 00:55:10 -04004929 /*
4930 * Encrypted inodes can't handle collapse range or insert
4931 * range since we would need to re-encrypt blocks with a
4932 * different IV or XTS tweak (which are based on the logical
4933 * block number).
4934 *
4935 * XXX It's not clear why zero range isn't working, but we'll
4936 * leave it disabled for encrypted inodes for now. This is a
4937 * bug we should fix....
4938 */
4939 if (ext4_encrypted_inode(inode) &&
Namjae Jeon331573f2015-06-09 01:55:03 -04004940 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
4941 FALLOC_FL_ZERO_RANGE)))
Michael Halcrow2058f832015-04-12 00:55:10 -04004942 return -EOPNOTSUPP;
4943
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004944 /* Return error if mode is not supported */
4945 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Namjae Jeon331573f2015-06-09 01:55:03 -04004946 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4947 FALLOC_FL_INSERT_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004948 return -EOPNOTSUPP;
4949
4950 if (mode & FALLOC_FL_PUNCH_HOLE)
4951 return ext4_punch_hole(inode, offset, len);
4952
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004953 ret = ext4_convert_inline_data(inode);
4954 if (ret)
4955 return ret;
4956
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004957 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4958 return ext4_collapse_range(inode, offset, len);
4959
Namjae Jeon331573f2015-06-09 01:55:03 -04004960 if (mode & FALLOC_FL_INSERT_RANGE)
4961 return ext4_insert_range(inode, offset, len);
4962
Lukas Czernerb8a86842014-03-18 18:05:35 -04004963 if (mode & FALLOC_FL_ZERO_RANGE)
4964 return ext4_zero_range(file, offset, len, mode);
4965
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004966 trace_ext4_fallocate_enter(inode, offset, len, mode);
4967 lblk = offset >> blkbits;
4968 /*
4969 * We can't just convert len to max_blocks because
4970 * If blocksize = 4096 offset = 3072 and len = 2048
4971 */
4972 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
4973 - lblk;
4974
Lukas Czerner556615d2014-04-20 23:45:47 -04004975 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004976 if (mode & FALLOC_FL_KEEP_SIZE)
4977 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4978
Al Viro59551022016-01-22 15:40:57 -05004979 inode_lock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004980
Davide Italiano280227a2015-05-02 23:21:15 -04004981 /*
4982 * We only support preallocation for extent-based files only
4983 */
4984 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4985 ret = -EOPNOTSUPP;
4986 goto out;
4987 }
4988
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004989 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4990 offset + len > i_size_read(inode)) {
4991 new_size = offset + len;
4992 ret = inode_newsize_ok(inode, new_size);
4993 if (ret)
4994 goto out;
4995 }
4996
Jan Kara17048e82015-12-07 14:29:17 -05004997 /* Wait all existing dio workers, newcomers will block on i_mutex */
4998 ext4_inode_block_unlocked_dio(inode);
4999 inode_dio_wait(inode);
5000
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04005001 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
5002 flags, mode);
Jan Kara17048e82015-12-07 14:29:17 -05005003 ext4_inode_resume_unlocked_dio(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04005004 if (ret)
5005 goto out;
5006
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04005007 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
5008 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5009 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04005010 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04005011out:
Al Viro59551022016-01-22 15:40:57 -05005012 inode_unlock(inode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04005013 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
5014 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04005015}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005016
5017/*
Mingming Cao00314622009-09-28 15:49:08 -04005018 * This function convert a range of blocks to written extents
5019 * The caller of this function will pass the start offset and the size.
5020 * all unwritten extents within this range will be converted to
5021 * written extents.
5022 *
5023 * This function is called from the direct IO end io call back
5024 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05005025 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04005026 */
Jan Kara6b523df2013-06-04 13:21:11 -04005027int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
5028 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04005029{
Mingming Cao00314622009-09-28 15:49:08 -04005030 unsigned int max_blocks;
5031 int ret = 0;
5032 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005033 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04005034 unsigned int credits, blkbits = inode->i_blkbits;
5035
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005036 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04005037 /*
5038 * We can't just convert len to max_blocks because
5039 * If blocksize = 4096 offset = 3072 and len = 2048
5040 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005041 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
5042 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04005043 /*
Jan Kara6b523df2013-06-04 13:21:11 -04005044 * This is somewhat ugly but the idea is clear: When transaction is
5045 * reserved, everything goes into it. Otherwise we rather start several
5046 * smaller transactions for conversion of each extent separately.
Mingming Cao00314622009-09-28 15:49:08 -04005047 */
Jan Kara6b523df2013-06-04 13:21:11 -04005048 if (handle) {
5049 handle = ext4_journal_start_reserved(handle,
5050 EXT4_HT_EXT_CONVERT);
5051 if (IS_ERR(handle))
5052 return PTR_ERR(handle);
5053 credits = 0;
5054 } else {
5055 /*
5056 * credits to insert 1 extent into extent tree
5057 */
5058 credits = ext4_chunk_trans_blocks(inode, max_blocks);
5059 }
Mingming Cao00314622009-09-28 15:49:08 -04005060 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005061 map.m_lblk += ret;
5062 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04005063 if (credits) {
5064 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5065 credits);
5066 if (IS_ERR(handle)) {
5067 ret = PTR_ERR(handle);
5068 break;
5069 }
Mingming Cao00314622009-09-28 15:49:08 -04005070 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005071 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05005072 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05005073 if (ret <= 0)
5074 ext4_warning(inode->i_sb,
5075 "inode #%lu: block %u: len %u: "
5076 "ext4_ext_map_blocks returned %d",
5077 inode->i_ino, map.m_lblk,
5078 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04005079 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04005080 if (credits)
5081 ret2 = ext4_journal_stop(handle);
5082 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04005083 break;
5084 }
Jan Kara6b523df2013-06-04 13:21:11 -04005085 if (!credits)
5086 ret2 = ext4_journal_stop(handle);
Mingming Cao00314622009-09-28 15:49:08 -04005087 return ret > 0 ? ret2 : ret;
5088}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005089
Mingming Cao00314622009-09-28 15:49:08 -04005090/*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005091 * If newes is not existing extent (newes->ec_pblk equals zero) find
5092 * delayed extent at start of newes and update newes accordingly and
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005093 * return start of the next delayed extent.
5094 *
Zheng Liu69eb33d2013-02-18 00:31:07 -05005095 * If newes is existing extent (newes->ec_pblk is not equal zero)
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005096 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
Zheng Liu69eb33d2013-02-18 00:31:07 -05005097 * extent found. Leave newes unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005098 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005099static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -05005100 struct extent_status *newes)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005101{
Zheng Liub3aff3e2012-11-08 21:57:37 -05005102 struct extent_status es;
Zheng Liube401362013-02-18 00:27:26 -05005103 ext4_lblk_t block, next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005104
Zheng Liu69eb33d2013-02-18 00:31:07 -05005105 if (newes->es_pblk == 0) {
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005106 ext4_es_find_delayed_extent_range(inode, newes->es_lblk,
5107 newes->es_lblk + newes->es_len - 1, &es);
5108
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005109 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005110 * No extent in extent-tree contains block @newes->es_pblk,
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005111 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005112 */
Zheng Liu06b0c882013-02-18 00:26:51 -05005113 if (es.es_len == 0)
Zheng Liub3aff3e2012-11-08 21:57:37 -05005114 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005115 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005116
Zheng Liu69eb33d2013-02-18 00:31:07 -05005117 if (es.es_lblk > newes->es_lblk) {
Zheng Liub3aff3e2012-11-08 21:57:37 -05005118 /* A hole found. */
Zheng Liu69eb33d2013-02-18 00:31:07 -05005119 newes->es_len = min(es.es_lblk - newes->es_lblk,
5120 newes->es_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005121 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005122 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005123
Zheng Liu69eb33d2013-02-18 00:31:07 -05005124 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005125 }
5126
Zheng Liu69eb33d2013-02-18 00:31:07 -05005127 block = newes->es_lblk + newes->es_len;
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005128 ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es);
Zheng Liube401362013-02-18 00:27:26 -05005129 if (es.es_len == 0)
5130 next_del = EXT_MAX_BLOCKS;
5131 else
5132 next_del = es.es_lblk;
5133
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005134 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005135}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005136/* fiemap flags we can handle specified here */
5137#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
5138
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05005139static int ext4_xattr_fiemap(struct inode *inode,
5140 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005141{
5142 __u64 physical = 0;
5143 __u64 length;
5144 __u32 flags = FIEMAP_EXTENT_LAST;
5145 int blockbits = inode->i_sb->s_blocksize_bits;
5146 int error = 0;
5147
5148 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005149 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04005150 struct ext4_iloc iloc;
5151 int offset; /* offset of xattr in inode */
5152
5153 error = ext4_get_inode_loc(inode, &iloc);
5154 if (error)
5155 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04005156 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005157 offset = EXT4_GOOD_OLD_INODE_SIZE +
5158 EXT4_I(inode)->i_extra_isize;
5159 physical += offset;
5160 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5161 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005162 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005163 } else { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04005164 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005165 length = inode->i_sb->s_blocksize;
5166 }
5167
5168 if (physical)
5169 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5170 length, flags);
5171 return (error < 0 ? error : 0);
5172}
5173
5174int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5175 __u64 start, __u64 len)
5176{
5177 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005178 int error = 0;
5179
Tao Ma94191982012-12-10 14:06:02 -05005180 if (ext4_has_inline_data(inode)) {
5181 int has_inline = 1;
5182
Dmitry Monakhovd952d692014-12-02 16:11:20 -05005183 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5184 start, len);
Tao Ma94191982012-12-10 14:06:02 -05005185
5186 if (has_inline)
5187 return error;
5188 }
5189
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005190 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5191 error = ext4_ext_precache(inode);
5192 if (error)
5193 return error;
5194 }
5195
Eric Sandeen6873fa02008-10-07 00:46:36 -04005196 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005197 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Theodore Ts'oad7fefb2015-01-02 15:16:00 -05005198 return generic_block_fiemap(inode, fieinfo, start, len,
5199 ext4_get_block);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005200
5201 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
5202 return -EBADR;
5203
5204 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5205 error = ext4_xattr_fiemap(inode, fieinfo);
5206 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005207 ext4_lblk_t len_blks;
5208 __u64 last_blk;
5209
Eric Sandeen6873fa02008-10-07 00:46:36 -04005210 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005211 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04005212 if (last_blk >= EXT_MAX_BLOCKS)
5213 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005214 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005215
5216 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005217 * Walk the extent tree gathering extent information
5218 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005219 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005220 error = ext4_fill_fiemap_extents(inode, start_blk,
5221 len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005222 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04005223 return error;
5224}
Namjae Jeon9eb79482014-02-23 15:18:59 -05005225
5226/*
5227 * ext4_access_path:
5228 * Function to access the path buffer for marking it dirty.
5229 * It also checks if there are sufficient credits left in the journal handle
5230 * to update path.
5231 */
5232static int
5233ext4_access_path(handle_t *handle, struct inode *inode,
5234 struct ext4_ext_path *path)
5235{
5236 int credits, err;
5237
5238 if (!ext4_handle_valid(handle))
5239 return 0;
5240
5241 /*
5242 * Check if need to extend journal credits
5243 * 3 for leaf, sb, and inode plus 2 (bmap and group
5244 * descriptor) for each block group; assume two block
5245 * groups
5246 */
5247 if (handle->h_buffer_credits < 7) {
5248 credits = ext4_writepage_trans_blocks(inode);
5249 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
5250 /* EAGAIN is success */
5251 if (err && err != -EAGAIN)
5252 return err;
5253 }
5254
5255 err = ext4_ext_get_access(handle, inode, path);
5256 return err;
5257}
5258
5259/*
5260 * ext4_ext_shift_path_extents:
5261 * Shift the extents of a path structure lying between path[depth].p_ext
Namjae Jeon331573f2015-06-09 01:55:03 -04005262 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells
5263 * if it is right shift or left shift operation.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005264 */
5265static int
5266ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5267 struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005268 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005269{
5270 int depth, err = 0;
5271 struct ext4_extent *ex_start, *ex_last;
5272 bool update = 0;
5273 depth = path->p_depth;
5274
5275 while (depth >= 0) {
5276 if (depth == path->p_depth) {
5277 ex_start = path[depth].p_ext;
5278 if (!ex_start)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005279 return -EFSCORRUPTED;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005280
5281 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005282
5283 err = ext4_access_path(handle, inode, path + depth);
5284 if (err)
5285 goto out;
5286
5287 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5288 update = 1;
5289
Namjae Jeon9eb79482014-02-23 15:18:59 -05005290 while (ex_start <= ex_last) {
Namjae Jeon331573f2015-06-09 01:55:03 -04005291 if (SHIFT == SHIFT_LEFT) {
5292 le32_add_cpu(&ex_start->ee_block,
5293 -shift);
5294 /* Try to merge to the left. */
5295 if ((ex_start >
5296 EXT_FIRST_EXTENT(path[depth].p_hdr))
5297 &&
5298 ext4_ext_try_to_merge_right(inode,
5299 path, ex_start - 1))
5300 ex_last--;
5301 else
5302 ex_start++;
5303 } else {
5304 le32_add_cpu(&ex_last->ee_block, shift);
5305 ext4_ext_try_to_merge_right(inode, path,
5306 ex_last);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005307 ex_last--;
Namjae Jeon331573f2015-06-09 01:55:03 -04005308 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005309 }
5310 err = ext4_ext_dirty(handle, inode, path + depth);
5311 if (err)
5312 goto out;
5313
5314 if (--depth < 0 || !update)
5315 break;
5316 }
5317
5318 /* Update index too */
5319 err = ext4_access_path(handle, inode, path + depth);
5320 if (err)
5321 goto out;
5322
Namjae Jeon331573f2015-06-09 01:55:03 -04005323 if (SHIFT == SHIFT_LEFT)
5324 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5325 else
5326 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005327 err = ext4_ext_dirty(handle, inode, path + depth);
5328 if (err)
5329 goto out;
5330
5331 /* we are done if current index is not a starting index */
5332 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5333 break;
5334
5335 depth--;
5336 }
5337
5338out:
5339 return err;
5340}
5341
5342/*
5343 * ext4_ext_shift_extents:
Namjae Jeon331573f2015-06-09 01:55:03 -04005344 * All the extents which lies in the range from @start to the last allocated
5345 * block for the @inode are shifted either towards left or right (depending
5346 * upon @SHIFT) by @shift blocks.
Namjae Jeon9eb79482014-02-23 15:18:59 -05005347 * On success, 0 is returned, error otherwise.
5348 */
5349static int
5350ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
Namjae Jeon331573f2015-06-09 01:55:03 -04005351 ext4_lblk_t start, ext4_lblk_t shift,
5352 enum SHIFT_DIRECTION SHIFT)
Namjae Jeon9eb79482014-02-23 15:18:59 -05005353{
5354 struct ext4_ext_path *path;
5355 int ret = 0, depth;
5356 struct ext4_extent *extent;
Namjae Jeon331573f2015-06-09 01:55:03 -04005357 ext4_lblk_t stop, *iterator, ex_start, ex_end;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005358
5359 /* Let path point to the last extent */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005360 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 0);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005361 if (IS_ERR(path))
5362 return PTR_ERR(path);
5363
5364 depth = path->p_depth;
5365 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005366 if (!extent)
5367 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005368
Namjae Jeon331573f2015-06-09 01:55:03 -04005369 stop = le32_to_cpu(extent->ee_block) +
Zheng Liu847c6c42014-04-12 12:45:55 -04005370 ext4_ext_get_actual_len(extent);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005371
Namjae Jeon331573f2015-06-09 01:55:03 -04005372 /*
5373 * In case of left shift, Don't start shifting extents until we make
5374 * sure the hole is big enough to accommodate the shift.
5375 */
5376 if (SHIFT == SHIFT_LEFT) {
5377 path = ext4_find_extent(inode, start - 1, &path, 0);
5378 if (IS_ERR(path))
5379 return PTR_ERR(path);
5380 depth = path->p_depth;
5381 extent = path[depth].p_ext;
5382 if (extent) {
5383 ex_start = le32_to_cpu(extent->ee_block);
5384 ex_end = le32_to_cpu(extent->ee_block) +
5385 ext4_ext_get_actual_len(extent);
5386 } else {
5387 ex_start = 0;
5388 ex_end = 0;
5389 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005390
Namjae Jeon331573f2015-06-09 01:55:03 -04005391 if ((start == ex_start && shift > ex_start) ||
5392 (shift > start - ex_end)) {
5393 ext4_ext_drop_refs(path);
5394 kfree(path);
5395 return -EINVAL;
5396 }
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005397 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005398
Namjae Jeon331573f2015-06-09 01:55:03 -04005399 /*
5400 * In case of left shift, iterator points to start and it is increased
5401 * till we reach stop. In case of right shift, iterator points to stop
5402 * and it is decreased till we reach start.
5403 */
5404 if (SHIFT == SHIFT_LEFT)
5405 iterator = &start;
5406 else
5407 iterator = &stop;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005408
5409 /* Its safe to start updating extents */
Namjae Jeon331573f2015-06-09 01:55:03 -04005410 while (start < stop) {
5411 path = ext4_find_extent(inode, *iterator, &path, 0);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005412 if (IS_ERR(path))
5413 return PTR_ERR(path);
5414 depth = path->p_depth;
5415 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005416 if (!extent) {
5417 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
Namjae Jeon331573f2015-06-09 01:55:03 -04005418 (unsigned long) *iterator);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005419 return -EFSCORRUPTED;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005420 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005421 if (SHIFT == SHIFT_LEFT && *iterator >
5422 le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005423 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005424 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5425 path[depth].p_ext++;
5426 } else {
Namjae Jeon331573f2015-06-09 01:55:03 -04005427 *iterator = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005428 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005429 }
5430 }
Namjae Jeon331573f2015-06-09 01:55:03 -04005431
5432 if (SHIFT == SHIFT_LEFT) {
5433 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5434 *iterator = le32_to_cpu(extent->ee_block) +
5435 ext4_ext_get_actual_len(extent);
5436 } else {
5437 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
5438 *iterator = le32_to_cpu(extent->ee_block) > 0 ?
5439 le32_to_cpu(extent->ee_block) - 1 : 0;
5440 /* Update path extent in case we need to stop */
5441 while (le32_to_cpu(extent->ee_block) < start)
5442 extent++;
5443 path[depth].p_ext = extent;
5444 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005445 ret = ext4_ext_shift_path_extents(path, shift, inode,
Namjae Jeon331573f2015-06-09 01:55:03 -04005446 handle, SHIFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005447 if (ret)
5448 break;
5449 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005450out:
5451 ext4_ext_drop_refs(path);
5452 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005453 return ret;
5454}
5455
5456/*
5457 * ext4_collapse_range:
5458 * This implements the fallocate's collapse range functionality for ext4
5459 * Returns: 0 and non-zero on error.
5460 */
5461int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5462{
5463 struct super_block *sb = inode->i_sb;
5464 ext4_lblk_t punch_start, punch_stop;
5465 handle_t *handle;
5466 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005467 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005468 int ret;
5469
Theodore Ts'ob9576fc2015-05-15 00:24:10 -04005470 /*
5471 * We need to test this early because xfstests assumes that a
5472 * collapse range of (0, 1) will return EOPNOTSUPP if the file
5473 * system does not support collapse range.
5474 */
5475 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5476 return -EOPNOTSUPP;
5477
Namjae Jeon9eb79482014-02-23 15:18:59 -05005478 /* Collapse range works only on fs block size aligned offsets. */
Namjae Jeonee98fa32014-07-29 10:45:31 -04005479 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5480 len & (EXT4_CLUSTER_SIZE(sb) - 1))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005481 return -EINVAL;
5482
5483 if (!S_ISREG(inode->i_mode))
Theodore Ts'o86f1ca32014-04-18 11:52:11 -04005484 return -EINVAL;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005485
5486 trace_ext4_collapse_range(inode, offset, len);
5487
5488 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5489 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5490
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005491 /* Call ext4_force_commit to flush all data in case of data=journal. */
5492 if (ext4_should_journal_data(inode)) {
5493 ret = ext4_force_commit(inode->i_sb);
5494 if (ret)
5495 return ret;
5496 }
5497
Al Viro59551022016-01-22 15:40:57 -05005498 inode_lock(inode);
Lukas Czerner23fffa92014-04-12 09:56:41 -04005499 /*
5500 * There is no need to overlap collapse range with EOF, in which case
5501 * it is effectively a truncate operation
5502 */
5503 if (offset + len >= i_size_read(inode)) {
5504 ret = -EINVAL;
5505 goto out_mutex;
5506 }
5507
Namjae Jeon9eb79482014-02-23 15:18:59 -05005508 /* Currently just for extent based files */
5509 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5510 ret = -EOPNOTSUPP;
5511 goto out_mutex;
5512 }
5513
Namjae Jeon9eb79482014-02-23 15:18:59 -05005514 /* Wait for existing dio to complete */
5515 ext4_inode_block_unlocked_dio(inode);
5516 inode_dio_wait(inode);
5517
Jan Karaea3d7202015-12-07 14:28:03 -05005518 /*
5519 * Prevent page faults from reinstantiating pages we have released from
5520 * page cache.
5521 */
5522 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara32ebffd2015-12-07 14:31:11 -05005523 /*
5524 * Need to round down offset to be aligned with page size boundary
5525 * for page size > block size.
5526 */
5527 ioffset = round_down(offset, PAGE_SIZE);
5528 /*
5529 * Write tail of the last page before removed range since it will get
5530 * removed from the page cache below.
5531 */
5532 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5533 if (ret)
5534 goto out_mmap;
5535 /*
5536 * Write data that will be shifted to preserve them when discarding
5537 * page cache below. We are also protected from pages becoming dirty
5538 * by i_mmap_sem.
5539 */
5540 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5541 LLONG_MAX);
5542 if (ret)
5543 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005544 truncate_pagecache(inode, ioffset);
5545
Namjae Jeon9eb79482014-02-23 15:18:59 -05005546 credits = ext4_writepage_trans_blocks(inode);
5547 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5548 if (IS_ERR(handle)) {
5549 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005550 goto out_mmap;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005551 }
5552
5553 down_write(&EXT4_I(inode)->i_data_sem);
5554 ext4_discard_preallocations(inode);
5555
5556 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005557 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005558 if (ret) {
5559 up_write(&EXT4_I(inode)->i_data_sem);
5560 goto out_stop;
5561 }
5562
5563 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5564 if (ret) {
5565 up_write(&EXT4_I(inode)->i_data_sem);
5566 goto out_stop;
5567 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005568 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005569
5570 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
Namjae Jeon331573f2015-06-09 01:55:03 -04005571 punch_stop - punch_start, SHIFT_LEFT);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005572 if (ret) {
5573 up_write(&EXT4_I(inode)->i_data_sem);
5574 goto out_stop;
5575 }
5576
5577 new_size = i_size_read(inode) - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005578 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005579 EXT4_I(inode)->i_disksize = new_size;
5580
Namjae Jeon9eb79482014-02-23 15:18:59 -05005581 up_write(&EXT4_I(inode)->i_data_sem);
5582 if (IS_SYNC(inode))
5583 ext4_handle_sync(handle);
5584 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
5585 ext4_mark_inode_dirty(handle, inode);
5586
5587out_stop:
5588 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005589out_mmap:
5590 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005591 ext4_inode_resume_unlocked_dio(inode);
5592out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005593 inode_unlock(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005594 return ret;
5595}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005596
Namjae Jeon331573f2015-06-09 01:55:03 -04005597/*
5598 * ext4_insert_range:
5599 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate.
5600 * The data blocks starting from @offset to the EOF are shifted by @len
5601 * towards right to create a hole in the @inode. Inode size is increased
5602 * by len bytes.
5603 * Returns 0 on success, error otherwise.
5604 */
5605int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
5606{
5607 struct super_block *sb = inode->i_sb;
5608 handle_t *handle;
5609 struct ext4_ext_path *path;
5610 struct ext4_extent *extent;
5611 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5612 unsigned int credits, ee_len;
5613 int ret = 0, depth, split_flag = 0;
5614 loff_t ioffset;
5615
5616 /*
5617 * We need to test this early because xfstests assumes that an
5618 * insert range of (0, 1) will return EOPNOTSUPP if the file
5619 * system does not support insert range.
5620 */
5621 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5622 return -EOPNOTSUPP;
5623
5624 /* Insert range works only on fs block size aligned offsets. */
5625 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5626 len & (EXT4_CLUSTER_SIZE(sb) - 1))
5627 return -EINVAL;
5628
5629 if (!S_ISREG(inode->i_mode))
5630 return -EOPNOTSUPP;
5631
5632 trace_ext4_insert_range(inode, offset, len);
5633
5634 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5635 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5636
5637 /* Call ext4_force_commit to flush all data in case of data=journal */
5638 if (ext4_should_journal_data(inode)) {
5639 ret = ext4_force_commit(inode->i_sb);
5640 if (ret)
5641 return ret;
5642 }
5643
Al Viro59551022016-01-22 15:40:57 -05005644 inode_lock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005645 /* Currently just for extent based files */
5646 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5647 ret = -EOPNOTSUPP;
5648 goto out_mutex;
5649 }
5650
5651 /* Check for wrap through zero */
5652 if (inode->i_size + len > inode->i_sb->s_maxbytes) {
5653 ret = -EFBIG;
5654 goto out_mutex;
5655 }
5656
5657 /* Offset should be less than i_size */
5658 if (offset >= i_size_read(inode)) {
5659 ret = -EINVAL;
5660 goto out_mutex;
5661 }
5662
Namjae Jeon331573f2015-06-09 01:55:03 -04005663 /* Wait for existing dio to complete */
5664 ext4_inode_block_unlocked_dio(inode);
5665 inode_dio_wait(inode);
5666
Jan Karaea3d7202015-12-07 14:28:03 -05005667 /*
5668 * Prevent page faults from reinstantiating pages we have released from
5669 * page cache.
5670 */
5671 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara32ebffd2015-12-07 14:31:11 -05005672 /*
5673 * Need to round down to align start offset to page size boundary
5674 * for page size > block size.
5675 */
5676 ioffset = round_down(offset, PAGE_SIZE);
5677 /* Write out all dirty pages */
5678 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5679 LLONG_MAX);
5680 if (ret)
5681 goto out_mmap;
Jan Karaea3d7202015-12-07 14:28:03 -05005682 truncate_pagecache(inode, ioffset);
5683
Namjae Jeon331573f2015-06-09 01:55:03 -04005684 credits = ext4_writepage_trans_blocks(inode);
5685 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5686 if (IS_ERR(handle)) {
5687 ret = PTR_ERR(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005688 goto out_mmap;
Namjae Jeon331573f2015-06-09 01:55:03 -04005689 }
5690
5691 /* Expand file to avoid data loss if there is error while shifting */
5692 inode->i_size += len;
5693 EXT4_I(inode)->i_disksize += len;
5694 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
5695 ret = ext4_mark_inode_dirty(handle, inode);
5696 if (ret)
5697 goto out_stop;
5698
5699 down_write(&EXT4_I(inode)->i_data_sem);
5700 ext4_discard_preallocations(inode);
5701
5702 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5703 if (IS_ERR(path)) {
5704 up_write(&EXT4_I(inode)->i_data_sem);
5705 goto out_stop;
5706 }
5707
5708 depth = ext_depth(inode);
5709 extent = path[depth].p_ext;
5710 if (extent) {
5711 ee_start_lblk = le32_to_cpu(extent->ee_block);
5712 ee_len = ext4_ext_get_actual_len(extent);
5713
5714 /*
5715 * If offset_lblk is not the starting block of extent, split
5716 * the extent @offset_lblk
5717 */
5718 if ((offset_lblk > ee_start_lblk) &&
5719 (offset_lblk < (ee_start_lblk + ee_len))) {
5720 if (ext4_ext_is_unwritten(extent))
5721 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5722 EXT4_EXT_MARK_UNWRIT2;
5723 ret = ext4_split_extent_at(handle, inode, &path,
5724 offset_lblk, split_flag,
5725 EXT4_EX_NOCACHE |
5726 EXT4_GET_BLOCKS_PRE_IO |
5727 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5728 }
5729
5730 ext4_ext_drop_refs(path);
5731 kfree(path);
5732 if (ret < 0) {
5733 up_write(&EXT4_I(inode)->i_data_sem);
5734 goto out_stop;
5735 }
5736 }
5737
5738 ret = ext4_es_remove_extent(inode, offset_lblk,
5739 EXT_MAX_BLOCKS - offset_lblk);
5740 if (ret) {
5741 up_write(&EXT4_I(inode)->i_data_sem);
5742 goto out_stop;
5743 }
5744
5745 /*
5746 * if offset_lblk lies in a hole which is at start of file, use
5747 * ee_start_lblk to shift extents
5748 */
5749 ret = ext4_ext_shift_extents(inode, handle,
5750 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5751 len_lblk, SHIFT_RIGHT);
5752
5753 up_write(&EXT4_I(inode)->i_data_sem);
5754 if (IS_SYNC(inode))
5755 ext4_handle_sync(handle);
5756
5757out_stop:
5758 ext4_journal_stop(handle);
Jan Karaea3d7202015-12-07 14:28:03 -05005759out_mmap:
5760 up_write(&EXT4_I(inode)->i_mmap_sem);
Namjae Jeon331573f2015-06-09 01:55:03 -04005761 ext4_inode_resume_unlocked_dio(inode);
5762out_mutex:
Al Viro59551022016-01-22 15:40:57 -05005763 inode_unlock(inode);
Namjae Jeon331573f2015-06-09 01:55:03 -04005764 return ret;
5765}
5766
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005767/**
5768 * ext4_swap_extents - Swap extents between two inodes
5769 *
5770 * @inode1: First inode
5771 * @inode2: Second inode
5772 * @lblk1: Start block for first inode
5773 * @lblk2: Start block for second inode
5774 * @count: Number of blocks to swap
5775 * @mark_unwritten: Mark second inode's extents as unwritten after swap
5776 * @erp: Pointer to save error value
5777 *
5778 * This helper routine does exactly what is promise "swap extents". All other
5779 * stuff such as page-cache locking consistency, bh mapping consistency or
5780 * extent's data copying must be performed by caller.
5781 * Locking:
5782 * i_mutex is held for both inodes
5783 * i_data_sem is locked for write for both inodes
5784 * Assumptions:
5785 * All pages from requested range are locked for both inodes
5786 */
5787int
5788ext4_swap_extents(handle_t *handle, struct inode *inode1,
5789 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
5790 ext4_lblk_t count, int unwritten, int *erp)
5791{
5792 struct ext4_ext_path *path1 = NULL;
5793 struct ext4_ext_path *path2 = NULL;
5794 int replaced_count = 0;
5795
5796 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5797 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
Al Viro59551022016-01-22 15:40:57 -05005798 BUG_ON(!inode_is_locked(inode1));
5799 BUG_ON(!inode_is_locked(inode2));
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005800
5801 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005802 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005803 return 0;
5804 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005805 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005806 return 0;
5807
5808 while (count) {
5809 struct ext4_extent *ex1, *ex2, tmp_ex;
5810 ext4_lblk_t e1_blk, e2_blk;
5811 int e1_len, e2_len, len;
5812 int split = 0;
5813
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005814 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305815 if (IS_ERR(path1)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005816 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005817 path1 = NULL;
5818 finish:
5819 count = 0;
5820 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005821 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005822 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Viresh Kumara1c83682015-08-12 15:59:44 +05305823 if (IS_ERR(path2)) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005824 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005825 path2 = NULL;
5826 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005827 }
5828 ex1 = path1[path1->p_depth].p_ext;
5829 ex2 = path2[path2->p_depth].p_ext;
5830 /* Do we have somthing to swap ? */
5831 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005832 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005833
5834 e1_blk = le32_to_cpu(ex1->ee_block);
5835 e2_blk = le32_to_cpu(ex2->ee_block);
5836 e1_len = ext4_ext_get_actual_len(ex1);
5837 e2_len = ext4_ext_get_actual_len(ex2);
5838
5839 /* Hole handling */
5840 if (!in_range(lblk1, e1_blk, e1_len) ||
5841 !in_range(lblk2, e2_blk, e2_len)) {
5842 ext4_lblk_t next1, next2;
5843
5844 /* if hole after extent, then go to next extent */
5845 next1 = ext4_ext_next_allocated_block(path1);
5846 next2 = ext4_ext_next_allocated_block(path2);
5847 /* If hole before extent, then shift to that extent */
5848 if (e1_blk > lblk1)
5849 next1 = e1_blk;
5850 if (e2_blk > lblk2)
5851 next2 = e1_blk;
5852 /* Do we have something to swap */
5853 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005854 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005855 /* Move to the rightest boundary */
5856 len = next1 - lblk1;
5857 if (len < next2 - lblk2)
5858 len = next2 - lblk2;
5859 if (len > count)
5860 len = count;
5861 lblk1 += len;
5862 lblk2 += len;
5863 count -= len;
5864 goto repeat;
5865 }
5866
5867 /* Prepare left boundary */
5868 if (e1_blk < lblk1) {
5869 split = 1;
5870 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005871 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005872 if (unlikely(*erp))
5873 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005874 }
5875 if (e2_blk < lblk2) {
5876 split = 1;
5877 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005878 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005879 if (unlikely(*erp))
5880 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005881 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005882 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005883 * path must to be revalidated. */
5884 if (split)
5885 goto repeat;
5886
5887 /* Prepare right boundary */
5888 len = count;
5889 if (len > e1_blk + e1_len - lblk1)
5890 len = e1_blk + e1_len - lblk1;
5891 if (len > e2_blk + e2_len - lblk2)
5892 len = e2_blk + e2_len - lblk2;
5893
5894 if (len != e1_len) {
5895 split = 1;
5896 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005897 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005898 if (unlikely(*erp))
5899 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005900 }
5901 if (len != e2_len) {
5902 split = 1;
5903 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005904 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005905 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005906 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005907 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005908 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005909 * path must to be revalidated. */
5910 if (split)
5911 goto repeat;
5912
5913 BUG_ON(e2_len != e1_len);
5914 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005915 if (unlikely(*erp))
5916 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005917 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005918 if (unlikely(*erp))
5919 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005920
5921 /* Both extents are fully inside boundaries. Swap it now */
5922 tmp_ex = *ex1;
5923 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5924 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5925 ex1->ee_len = cpu_to_le16(e2_len);
5926 ex2->ee_len = cpu_to_le16(e1_len);
5927 if (unwritten)
5928 ext4_ext_mark_unwritten(ex2);
5929 if (ext4_ext_is_unwritten(&tmp_ex))
5930 ext4_ext_mark_unwritten(ex1);
5931
5932 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5933 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5934 *erp = ext4_ext_dirty(handle, inode2, path2 +
5935 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005936 if (unlikely(*erp))
5937 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005938 *erp = ext4_ext_dirty(handle, inode1, path1 +
5939 path1->p_depth);
5940 /*
5941 * Looks scarry ah..? second inode already points to new blocks,
5942 * and it was successfully dirtied. But luckily error may happen
5943 * only due to journal error, so full transaction will be
5944 * aborted anyway.
5945 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005946 if (unlikely(*erp))
5947 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005948 lblk1 += len;
5949 lblk2 += len;
5950 replaced_count += len;
5951 count -= len;
5952
5953 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005954 ext4_ext_drop_refs(path1);
5955 kfree(path1);
5956 ext4_ext_drop_refs(path2);
5957 kfree(path2);
5958 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005959 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005960 return replaced_count;
5961}